| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
#pragma once |
| 3 |
|
| 4 |
#include <netinet/in.h> |
| 5 |
#include <sys/uio.h> |
| 6 |
|
| 7 |
#include "sd-dhcp-relay.h" |
| 8 |
|
| 9 |
#include "dhcp-forward.h" |
| 10 |
#include "network-common.h" |
| 11 |
|
| 12 |
#define DHCP_RELAY_IFINDEX_UNBOUND (-100) |
| 13 |
|
| 14 |
struct sd_dhcp_relay { |
| 15 |
unsigned n_ref; |
| 16 |
|
| 17 |
sd_event *event; |
| 18 |
int event_priority; |
| 19 |
|
| 20 |
Hashmap *interfaces; /* All interfaces by their ifindex. */ |
| 21 |
Prioq *upstream_interfaces; /* Active upstream interfaces by their priorities. */ |
| 22 |
Hashmap *downstream_interfaces; /* Active downstream interfaces by their gateway address, circuit ID, and VSS. */ |
| 23 |
|
| 24 |
struct in_addr server_address; |
| 25 |
uint16_t server_port; |
| 26 |
|
| 27 |
/* Global Relay Agent Information option (82) */ |
| 28 |
struct iovec remote_id; /* Agent Remote ID Sub-option (2) */ |
| 29 |
bool server_identifier_override; /* Relay Agent Flags (10) and Server Identifier Override Sub-option (11) */ |
| 30 |
TLV *extra_options; |
| 31 |
}; |
| 32 |
|
| 33 |
struct sd_dhcp_relay_interface { |
| 34 |
unsigned n_ref; |
| 35 |
|
| 36 |
sd_dhcp_relay *relay; |
| 37 |
bool upstream; |
| 38 |
|
| 39 |
int ifindex; |
| 40 |
char *ifname; |
| 41 |
|
| 42 |
/* The address used for: |
| 43 |
* - the source IP of forwarded packets (both downstream and upstream), |
| 44 |
* - the Server Identifier Override Sub-option (when sd_dhcp_relay.server_identifier_override is true), |
| 45 |
* - the Link Selection Sub-option (when address != gateway_address). |
| 46 |
* Typically, this is an address of the interface itself, but we can specify an address of another |
| 47 |
* interface (e.g., for IP unnumbered setups). */ |
| 48 |
struct in_addr address; |
| 49 |
uint8_t address_prefixlen; |
| 50 |
/* subnet-directed broadcast address, e.g. 192.0.2.255 when address is 192.0.2.1/24. */ |
| 51 |
struct in_addr subnet_broadcast; |
| 52 |
uint16_t port; |
| 53 |
|
| 54 |
uint8_t ip_service_type; /* a.k.a. TOS */ |
| 55 |
int socket_fd; /* socket fd set externally, used by unit tests */ |
| 56 |
int raw_socket_fd; /* send-only raw socket fd, used on sending L2 unicast message. */ |
| 57 |
sd_event_source *io_event_source; |
| 58 |
|
| 59 |
/* Mutually exclusive fields depending on the 'upstream' boolean */ |
| 60 |
union { |
| 61 |
/* Upstream specific */ |
| 62 |
struct { |
| 63 |
int priority; |
| 64 |
unsigned priority_idx; |
| 65 |
}; |
| 66 |
|
| 67 |
/* Downstream specific */ |
| 68 |
struct { |
| 69 |
/* The address set in the giaddr field of the DHCP message header. Typically, it is |
| 70 |
* the same as 'address' above, but we can specify a different address, and it does |
| 71 |
* not need to be an address assigned to the interface. */ |
| 72 |
struct in_addr gateway_address; |
| 73 |
|
| 74 |
/* Per-interface Relay Agent Information option (82) */ |
| 75 |
struct iovec circuit_id; /* Agent Circuit ID Sub-option (1) */ |
| 76 |
struct iovec vss; /* DHCPv4 Virtual Subnet Selection Sub-Option (151) */ |
| 77 |
TLV *extra_options; |
| 78 |
}; |
| 79 |
}; |
| 80 |
}; |
| 81 |
|
| 82 |
int dhcp_relay_set_extra_options(sd_dhcp_relay *relay, TLV *options); |
| 83 |
|
| 84 |
int downstream_set_extra_options(sd_dhcp_relay_interface *interface, TLV *options); |
| 85 |
int downstream_register(sd_dhcp_relay_interface *interface); |
| 86 |
void downstream_unregister(sd_dhcp_relay_interface *interface); |
| 87 |
void downstream_done(sd_dhcp_relay_interface *interface); |
| 88 |
int downstream_get(sd_dhcp_relay *relay, sd_dhcp_message *message, sd_dhcp_relay_interface **ret); |
| 89 |
int downstream_process_message( |
| 90 |
sd_dhcp_relay_interface *interface, |
| 91 |
const struct iovec *iov, |
| 92 |
const struct in_pktinfo *pktinfo); |
| 93 |
int downstream_send_message(sd_dhcp_relay_interface *interface, sd_dhcp_message *message); |
| 94 |
|
| 95 |
int upstream_register(sd_dhcp_relay_interface *interface); |
| 96 |
void upstream_unregister(sd_dhcp_relay_interface *interface); |
| 97 |
void upstream_done(sd_dhcp_relay_interface *interface); |
| 98 |
int upstream_get(sd_dhcp_relay *relay, sd_dhcp_relay_interface **ret); |
| 99 |
int upstream_process_message( |
| 100 |
sd_dhcp_relay_interface *interface, |
| 101 |
const struct iovec *iov, |
| 102 |
const struct in_pktinfo *pktinfo); |
| 103 |
int upstream_send_message(sd_dhcp_relay_interface *interface, sd_dhcp_message *message); |
| 104 |
|
| 105 |
#define log_dhcp_relay_interface_errno(interface, error, fmt, ...) \ |
| 106 |
log_interface_prefix_full_errno( \ |
| 107 |
"DHCPv4 relay: ", \ |
| 108 |
sd_dhcp_relay_interface, interface, \ |
| 109 |
error, fmt, ##__VA_ARGS__) |
| 110 |
#define log_dhcp_relay_interface(interface, fmt, ...) \ |
| 111 |
log_interface_prefix_full_errno_zerook( \ |
| 112 |
"DHCPv4 relay: ", \ |
| 113 |
sd_dhcp_relay_interface, interface, \ |
| 114 |
0, fmt, ##__VA_ARGS__) |