| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <net/if_arp.h> |
| 4 |
|
| 5 |
#include "sd-event.h" |
| 6 |
|
| 7 |
#include "dhcp-message.h" |
| 8 |
#include "dhcp-relay-internal.h" |
| 9 |
#include "hashmap.h" |
| 10 |
#include "in-addr-util.h" |
| 11 |
#include "prioq.h" |
| 12 |
|
| 13 |
int sd_dhcp_relay_upstream_set_priority(sd_dhcp_relay_interface *interface, int priority) { |
| 14 |
assert_return(interface, -EINVAL); |
| 15 |
assert_return(interface->upstream, -EINVAL); |
| 16 |
assert_return(!sd_dhcp_relay_interface_is_running(interface), -EBUSY); |
| 17 |
|
| 18 |
interface->priority = priority; |
| 19 |
return 0; |
| 20 |
} |
| 21 |
|
| 22 |
static int upstream_compare_func(const sd_dhcp_relay_interface *a, const sd_dhcp_relay_interface *b) { |
| 23 |
assert(a); |
| 24 |
assert(a->upstream); |
| 25 |
assert(b); |
| 26 |
assert(b->upstream); |
| 27 |
|
| 28 |
/* Higher priority first */ |
| 29 |
return CMP(b->priority, a->priority); |
| 30 |
} |
| 31 |
|
| 32 |
int upstream_register(sd_dhcp_relay_interface *interface) { |
| 33 |
assert(interface); |
| 34 |
assert(interface->relay); |
| 35 |
assert(interface->upstream); |
| 36 |
assert(!sd_dhcp_relay_interface_is_running(interface)); |
| 37 |
|
| 38 |
interface->priority_idx = PRIOQ_IDX_NULL; |
| 39 |
return prioq_ensure_put(&interface->relay->upstream_interfaces, upstream_compare_func, interface, &interface->priority_idx); |
| 40 |
} |
| 41 |
|
| 42 |
void upstream_unregister(sd_dhcp_relay_interface *interface) { |
| 43 |
assert(interface); |
| 44 |
assert(interface->relay); |
| 45 |
assert(interface->upstream); |
| 46 |
|
| 47 |
(void) prioq_remove(interface->relay->upstream_interfaces, interface, &interface->priority_idx); |
| 48 |
} |
| 49 |
|
| 50 |
void upstream_done(sd_dhcp_relay_interface *interface) { |
| 51 |
upstream_unregister(interface); |
| 52 |
} |
| 53 |
|
| 54 |
int upstream_get(sd_dhcp_relay *relay, sd_dhcp_relay_interface **ret) { |
| 55 |
sd_dhcp_relay_interface *interface = prioq_peek(relay->upstream_interfaces); |
| 56 |
if (!interface) |
| 57 |
return -ENETDOWN; |
| 58 |
|
| 59 |
assert(interface->upstream); |
| 60 |
assert(interface->io_event_source); |
| 61 |
|
| 62 |
if (ret) |
| 63 |
*ret = interface; |
| 64 |
return 0; |
| 65 |
} |
| 66 |
|
| 67 |
int upstream_process_message( |
| 68 |
sd_dhcp_relay_interface *interface, |
| 69 |
const struct iovec *iov, |
| 70 |
const struct in_pktinfo *pktinfo) { |
| 71 |
|
| 72 |
int r; |
| 73 |
|
| 74 |
assert(interface); |
| 75 |
assert(interface->relay); |
| 76 |
assert(interface->upstream); |
| 77 |
assert(iov); |
| 78 |
|
| 79 |
if (pktinfo && pktinfo->ipi_ifindex > 0 && |
| 80 |
interface->ifindex < 0 && |
| 81 |
hashmap_contains(interface->relay->interfaces, INT_TO_PTR(pktinfo->ipi_ifindex))) |
| 82 |
return 0; /* This message is not for the catch-all interface. */ |
| 83 |
|
| 84 |
_cleanup_(sd_dhcp_message_unrefp) sd_dhcp_message *message = NULL; |
| 85 |
r = dhcp_message_parse( |
| 86 |
iov, |
| 87 |
BOOTREPLY, |
| 88 |
/* xid= */ NULL, |
| 89 |
ARPHRD_NONE, |
| 90 |
/* hw_addr= */ NULL, |
| 91 |
&message); |
| 92 |
if (r < 0) |
| 93 |
return r; |
| 94 |
|
| 95 |
if (message->header.giaddr == INADDR_ANY) |
| 96 |
return 0; /* Not a relay message, so it is probably not for us. */ |
| 97 |
|
| 98 |
if (pktinfo && pktinfo->ipi_addr.s_addr != message->header.giaddr) |
| 99 |
return -EBADMSG; |
| 100 |
|
| 101 |
log_dhcp_relay_interface(interface, "Received BOOTREPLY (0x%"PRIx32")", be32toh(message->header.xid)); |
| 102 |
|
| 103 |
sd_dhcp_relay_interface *downstream; |
| 104 |
r = downstream_get(interface->relay, message, &downstream); |
| 105 |
if (r < 0) |
| 106 |
return r; |
| 107 |
|
| 108 |
/* RFC 3046 abstract: |
| 109 |
* The DHCP Server echoes the option back verbatim to the relay agent in server-to-client |
| 110 |
* replies, and the relay agent strips the option before forwarding the reply to the client. |
| 111 |
* |
| 112 |
* RFC 3046 section 2.1: |
| 113 |
* The Relay Agent Information option echoed by a server MUST be removed by either the relay |
| 114 |
* agent or the trusted downstream network element which added it when forwarding a |
| 115 |
* server-to-client response back to the client. |
| 116 |
* |
| 117 |
* Here, we do not check the contents of the option, and unconditionally remove it. */ |
| 118 |
dhcp_message_remove_option(message, SD_DHCP_OPTION_RELAY_AGENT_INFORMATION); |
| 119 |
|
| 120 |
return downstream_send_message(downstream, message); |
| 121 |
} |
| 122 |
|
| 123 |
int upstream_send_message(sd_dhcp_relay_interface *interface, sd_dhcp_message *message) { |
| 124 |
int r; |
| 125 |
|
| 126 |
assert(interface); |
| 127 |
assert(interface->upstream); |
| 128 |
assert(message); |
| 129 |
assert(message->header.op == BOOTREQUEST); |
| 130 |
assert(message->header.giaddr != INADDR_ANY); |
| 131 |
|
| 132 |
int fd = sd_event_source_get_io_fd(interface->io_event_source); |
| 133 |
if (fd < 0) |
| 134 |
return fd; |
| 135 |
|
| 136 |
r = dhcp_message_send_udp( |
| 137 |
message, |
| 138 |
fd, |
| 139 |
interface->address.s_addr, |
| 140 |
interface->relay->server_address.s_addr, |
| 141 |
interface->relay->server_port); |
| 142 |
if (r < 0) |
| 143 |
return r; |
| 144 |
|
| 145 |
log_dhcp_relay_interface(interface, "Forwarded BOOTREQUEST (0x%"PRIx32") to %s (UDP).", |
| 146 |
be32toh(message->header.xid), |
| 147 |
IN4_ADDR_TO_STRING(&interface->relay->server_address)); |
| 148 |
return 0; |
| 149 |
} |