Debian Codegraph @

src/libsystemd-network/dhcp-relay-downstream.c

raw
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 "errno-util.h"
10 #include "ether-addr-util.h"
11 #include "fd-util.h"
12 #include "hashmap.h"
13 #include "in-addr-util.h"
14 #include "iovec-util.h"
15 #include "siphash24.h"
16 #include "socket-util.h"
17
18 int sd_dhcp_relay_downstream_set_circuit_id(sd_dhcp_relay_interface *interface, const struct iovec *iov) {
19 assert_return(interface, -EINVAL);
20 assert_return(!interface->upstream, -EINVAL);
21 assert_return(!sd_dhcp_relay_interface_is_running(interface), -EBUSY);
22
23 return iovec_done_and_memdup(&interface->circuit_id, iov);
24 }
25
26 int sd_dhcp_relay_downstream_set_virtual_subnet_selection(sd_dhcp_relay_interface *interface, const struct iovec *iov) {
27 assert_return(interface, -EINVAL);
28 assert_return(!interface->upstream, -EINVAL);
29 assert_return(!sd_dhcp_relay_interface_is_running(interface), -EBUSY);
30
31 return iovec_done_and_memdup(&interface->vss, iov);
32 }
33
34 int downstream_set_extra_options(sd_dhcp_relay_interface *interface, TLV *options) {
35 assert(interface);
36 assert(!interface->upstream);
37 assert(!sd_dhcp_relay_interface_is_running(interface));
38
39 return unref_and_replace_new_ref(interface->extra_options, options, tlv_ref, tlv_unref);
40 }
41
42 int sd_dhcp_relay_downstream_set_gateway_address(sd_dhcp_relay_interface *interface, const struct in_addr *address) {
43 assert_return(interface, -EINVAL);
44 assert_return(!interface->upstream, -EINVAL);
45 assert_return(!sd_dhcp_relay_interface_is_running(interface), -EBUSY);
46
47 if (address)
48 interface->gateway_address = *address;
49 else
50 interface->gateway_address = (struct in_addr) {};
51
52 return 0;
53 }
54
55 static void downstream_hash_func(const sd_dhcp_relay_interface *interface, struct siphash *state) {
56 int b;
57
58 assert(interface);
59 assert(!interface->upstream);
60 assert(state);
61
62 siphash24_compress_typesafe(interface->gateway_address, state);
63
64 b = iovec_is_set(&interface->circuit_id);
65 siphash24_compress_typesafe(b, state);
66 if (b)
67 siphash24_compress_iovec(&interface->circuit_id, state);
68
69 b = iovec_is_set(&interface->vss);
70 siphash24_compress_typesafe(b, state);
71 if (b)
72 siphash24_compress_iovec(&interface->vss, state);
73 }
74
75 static int downstream_compare_func(const sd_dhcp_relay_interface *a, const sd_dhcp_relay_interface *b) {
76 int r;
77
78 assert(a);
79 assert(!a->upstream);
80 assert(b);
81 assert(!b->upstream);
82
83 r = CMP(a->gateway_address.s_addr, b->gateway_address.s_addr);
84 if (r != 0)
85 return r;
86
87 r = iovec_memcmp(&a->circuit_id, &b->circuit_id);
88 if (r != 0)
89 return r;
90
91 return iovec_memcmp(&a->vss, &b->vss);
92 }
93
94 DEFINE_PRIVATE_HASH_OPS(
95 downstream_hash_ops,
96 sd_dhcp_relay_interface,
97 downstream_hash_func,
98 downstream_compare_func);
99
100 int downstream_register(sd_dhcp_relay_interface *interface) {
101 assert(interface);
102 assert(interface->relay);
103 assert(!interface->upstream);
104 assert(in4_addr_is_set(&interface->address));
105 assert(in4_addr_is_set(&interface->gateway_address));
106 assert(!sd_dhcp_relay_interface_is_running(interface));
107
108 /* Do not use a Set; otherwise, we cannot deduplicate entries. */
109 return hashmap_ensure_put(&interface->relay->downstream_interfaces, &downstream_hash_ops, interface, interface);
110 }
111
112 void downstream_unregister(sd_dhcp_relay_interface *interface) {
113 assert(interface);
114 assert(interface->relay);
115 assert(!interface->upstream);
116
117 hashmap_remove_value(interface->relay->downstream_interfaces, interface, interface);
118 }
119
120 void downstream_done(sd_dhcp_relay_interface *interface) {
121 assert(interface);
122 assert(!interface->upstream);
123
124 downstream_unregister(interface);
125 iovec_done(&interface->circuit_id);
126 iovec_done(&interface->vss);
127 interface->extra_options = tlv_unref(interface->extra_options);
128 }
129
130 int downstream_get(sd_dhcp_relay *relay, sd_dhcp_message *message, sd_dhcp_relay_interface **ret) {
131 int r;
132
133 assert(relay);
134 assert(message);
135
136 /* RFC 3046 section 2.2:
137 * DHCP servers claiming to support the Relay Agent Information option SHALL echo the entire contents
138 * of the Relay Agent Information option in all replies.
139 *
140 * So, first try to find the suitable downstream interface by the gateway address and circuit ID in
141 * the reply message. */
142 sd_dhcp_relay_interface key = {
143 .gateway_address.s_addr = message->header.giaddr,
144 };
145
146 _cleanup_(tlv_unrefp) TLV *agent_info = NULL;
147 r = dhcp_message_get_option_sub_tlv(
148 message,
149 SD_DHCP_OPTION_RELAY_AGENT_INFORMATION,
150 TLV_DHCP4_SUBOPTION,
151 &agent_info);
152 if (r < 0 && r != -ENODATA)
153 return r;
154
155 if (agent_info) {
156 r = tlv_get(agent_info, SD_DHCP_RELAY_AGENT_CIRCUIT_ID, &key.circuit_id);
157 if (r < 0 && r != -ENODATA)
158 return r;
159
160 r = tlv_get(agent_info, SD_DHCP_RELAY_AGENT_VIRTUAL_SUBNET_SELECTION, &key.vss);
161 if (r < 0 && r != -ENODATA)
162 return r;
163 }
164
165 sd_dhcp_relay_interface *interface = hashmap_get(relay->downstream_interfaces, &key);
166 if (!interface) {
167 /* Some DHCP servers may not understand the Relay Agent Information option and may not echo
168 * it back. To support this case, we fall back to finding a suitable downstream interface
169 * using only the gateway address. Note that if the downstream network uses VRF or the Link
170 * Selection sub-option, multiple interfaces may share the same gateway address. In such
171 * cases, we cannot reliably determine the correct downstream interface, so we must drop the
172 * packet. */
173 sd_dhcp_relay_interface *i;
174 HASHMAP_FOREACH(i, relay->downstream_interfaces) {
175 if (i->gateway_address.s_addr != message->header.giaddr)
176 continue;
177
178 if (interface)
179 /* multiple interfaces have the same gateway address?? */
180 return -ENOTUNIQ;
181
182 interface = i;
183 }
184 }
185 if (!interface)
186 return -ENODEV;
187
188 assert(!interface->upstream);
189 assert(interface->io_event_source);
190
191 if (ret)
192 *ret = interface;
193 return 0;
194 }
195
196 static int downstream_append_relay_agent_information(
197 sd_dhcp_relay_interface *interface,
198 sd_dhcp_message *message,
199 const struct in_pktinfo *pktinfo) {
200
201 int r;
202
203 assert(interface);
204 assert(interface->relay);
205 assert(!interface->upstream);
206 assert(message);
207
208 _cleanup_(tlv_done) TLV tlv = TLV_INIT(TLV_DHCP4_SUBOPTION);
209
210 /* First, set per-interface options. */
211 if (iovec_is_set(&interface->circuit_id)) {
212 r = tlv_append_iov(&tlv, SD_DHCP_RELAY_AGENT_CIRCUIT_ID, &interface->circuit_id);
213 if (r < 0)
214 return r;
215 }
216
217 if (iovec_is_set(&interface->vss)) {
218 r = tlv_append_iov(&tlv, SD_DHCP_RELAY_AGENT_VIRTUAL_SUBNET_SELECTION, &interface->vss);
219 if (r < 0)
220 return r;
221 }
222
223 if (!in4_addr_equal(&interface->address, &interface->gateway_address)) {
224 /* RFC 3527 section 3
225 * The link-selection sub-option is used by any DHCP relay agent that desires to specify a
226 * subnet/link for a DHCP client request that it is relaying but needs the subnet/link
227 * specification to be different from the IP address the DHCP server should use when
228 * communicating with the relay agent. */
229 r = tlv_append(&tlv, SD_DHCP_RELAY_AGENT_LINK_SELECTION, sizeof(struct in_addr), &interface->address);
230 if (r < 0)
231 return r;
232 }
233
234 r = tlv_append_tlv(&tlv, interface->extra_options);
235 if (r < 0)
236 return r;
237
238 /* Then, set agent-wide options. */
239 if (iovec_is_set(&interface->relay->remote_id)) {
240 r = tlv_append_iov(&tlv, SD_DHCP_RELAY_AGENT_REMOTE_ID, &interface->relay->remote_id);
241 if (r < 0)
242 return r;
243 }
244
245 if (interface->relay->server_identifier_override) {
246 /* RFC 5107 section 1:
247 * This DHCP relay agent suboption, Server Identifier Override, allows the relay agent to
248 * tell the DHCP server what value to place into the Server Identifier option. Using this,
249 * the relay agent can force a host in RENEWING state to send DHCPREQUEST messages to the
250 * relay agent instead of directly to the server. */
251 r = tlv_append(&tlv, SD_DHCP_RELAY_AGENT_SERVER_IDENTIFIER_OVERRIDE, sizeof(struct in_addr), &interface->address);
252 if (r < 0)
253 return r;
254
255 /* RFC 5107 section 4:
256 * DHCP relay agents implementing this suboption SHOULD also implement and use the DHCPv4
257 * Relay Agent Flags Suboption in order to specify whether the DHCP relay agent received the
258 * original message as a broadcast or unicast. */
259 uint8_t flags = 0;
260 SET_FLAG(flags, DHCP_RELAY_AGENT_FLAG_UNICAST,
261 pktinfo &&
262 pktinfo->ipi_addr.s_addr != INADDR_BROADCAST &&
263 pktinfo->ipi_addr.s_addr != interface->subnet_broadcast.s_addr);
264 r = tlv_append(&tlv, SD_DHCP_RELAY_AGENT_FLAGS, sizeof(uint8_t), &flags);
265 if (r < 0)
266 return r;
267 }
268
269 r = tlv_append_tlv(&tlv, interface->relay->extra_options);
270 if (r < 0)
271 return r;
272
273 if (tlv_isempty(&tlv))
274 return 0;
275
276 return dhcp_message_append_option_sub_tlv(message, SD_DHCP_OPTION_RELAY_AGENT_INFORMATION, &tlv);
277 }
278
279 int downstream_process_message(
280 sd_dhcp_relay_interface *interface,
281 const struct iovec *iov,
282 const struct in_pktinfo *pktinfo) {
283
284 int r;
285
286 assert(interface);
287 assert(interface->relay);
288 assert(!interface->upstream);
289 assert(in4_addr_is_set(&interface->address));
290 assert(in4_addr_is_set(&interface->gateway_address));
291 assert(iov);
292
293 _cleanup_(sd_dhcp_message_unrefp) sd_dhcp_message *message = NULL;
294 r = dhcp_message_parse(
295 iov,
296 BOOTREQUEST,
297 /* xid= */ NULL,
298 ARPHRD_NONE,
299 /* hw_addr= */ NULL,
300 &message);
301 if (r < 0)
302 return r;
303
304 /* RFC 1542 section 4.1.1:
305 * The relay agent MUST silently discard BOOTREQUEST messages whose 'hops' field exceeds the value 16. */
306 if (message->header.hops >= 16)
307 return 0;
308 message->header.hops++;
309
310 /* RFC 3046 section 2.1.1:
311 * Relay agents configured to add a Relay Agent option which receive a client DHCP packet with a
312 * nonzero giaddr SHALL discard the packet if the giaddr spoofs a giaddr address implemented by the
313 * local agent itself. */
314 if (message->header.giaddr != INADDR_ANY) {
315 sd_dhcp_relay_interface *i;
316 HASHMAP_FOREACH(i, interface->relay->interfaces) {
317 if (i->upstream)
318 continue;
319 if (message->header.giaddr == i->address.s_addr ||
320 message->header.giaddr == i->gateway_address.s_addr)
321 return -EBADMSG;
322 }
323 }
324
325 /* RFC 1542 section 4.1.1:
326 * If the relay agent does decide to relay the request, it MUST examine the 'giaddr' ("gateway" IP
327 * address) field. If this field is zero, the relay agent MUST fill this field with the IP address of
328 * the interface on which the request was received. (snip) If the 'giaddr' field contains some
329 * non-zero value, the 'giaddr' field MUST NOT be modified.
330 *
331 * RFC 3046 section 2.1.1:
332 * the relay agent SHALL forward any received DHCP packet with a valid non-zero giaddr WITHOUT adding
333 * any relay agent options. Per RFC 2131, it shall also NOT modify the giaddr value.
334 *
335 * Therefore, we set giaddr and the Relay Agent Information option here only when the giaddr in the
336 * received message is zero. */
337 if (message->header.giaddr == INADDR_ANY) {
338 message->header.giaddr = interface->gateway_address.s_addr;
339
340 /* RFC 3046 section 2.1:
341 * Relay agents receiving a DHCP packet from an untrusted circuit with giaddr set to zero
342 * (indicating that they are the first-hop router) but with a Relay Agent Information option
343 * already present in the packet SHALL discard the packet and increment an error count. */
344 if (dhcp_message_has_option(message, SD_DHCP_OPTION_RELAY_AGENT_INFORMATION))
345 return -EBADMSG;
346
347 r = downstream_append_relay_agent_information(interface, message, pktinfo);
348 if (r < 0)
349 return r;
350 }
351
352 log_dhcp_relay_interface(interface, "Received BOOTREQUEST (0x%"PRIx32")", be32toh(message->header.xid));
353
354 sd_dhcp_relay_interface *upstream;
355 r = upstream_get(interface->relay, &upstream);
356 if (r < 0)
357 return r;
358
359 return upstream_send_message(upstream, message);
360 }
361
362 static int downstream_acquire_raw_socket(sd_dhcp_relay_interface *interface) {
363 int r;
364
365 assert(interface);
366 assert(!interface->upstream);
367
368 if (interface->socket_fd >= 0)
369 /* When a socket fd is given externally, unconditionally use it. */
370 return interface->socket_fd;
371
372 if (interface->raw_socket_fd >= 0)
373 /* Already opened. */
374 return interface->raw_socket_fd;
375
376 /* This is a send-only socket, hence it is opened with protocol=0, and do not call bind().
377 * The interface binding will be done on send. */
378 _cleanup_close_ int fd = RET_NERRNO(socket(AF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, /* protocol= */ 0));
379 if (fd < 0)
380 return fd;
381
382 r = setsockopt_int(fd, SOL_SOCKET, SO_PRIORITY, tos_to_priority(interface->ip_service_type));
383 if (r < 0)
384 return r;
385
386 return interface->raw_socket_fd = TAKE_FD(fd);
387 }
388
389 static int downstream_send_l2_unicast(
390 sd_dhcp_relay_interface *interface,
391 sd_dhcp_message *message,
392 const struct hw_addr_data *hw_addr) {
393
394 int r;
395
396 assert(interface);
397 assert(!interface->upstream);
398 assert(message);
399 assert(message->header.yiaddr != INADDR_ANY);
400 assert(!hw_addr_is_null(hw_addr));
401
402 int fd = downstream_acquire_raw_socket(interface);
403 if (fd < 0)
404 return fd;
405
406 r = dhcp_message_send_raw(
407 message,
408 fd,
409 interface->ifindex,
410 interface->address.s_addr,
411 interface->port,
412 hw_addr,
413 message->header.yiaddr,
414 DHCP_PORT_CLIENT,
415 interface->ip_service_type);
416 if (r < 0)
417 return r;
418
419 log_dhcp_relay_interface(interface, "Forwarded BOOTREPLY (0x%"PRIx32") to %s (L2 unicast).",
420 be32toh(message->header.xid),
421 IN4_ADDR_TO_STRING(&(struct in_addr) { .s_addr = message->header.yiaddr }));
422 return 0;
423 }
424
425 static int downstream_send_udp(
426 sd_dhcp_relay_interface *interface,
427 sd_dhcp_message *message,
428 be32_t address) {
429
430 int r;
431
432 assert(interface);
433 assert(!interface->upstream);
434 assert(message);
435 assert(address != INADDR_ANY);
436
437 int fd = sd_event_source_get_io_fd(interface->io_event_source);
438 if (fd < 0)
439 return fd;
440
441 r = dhcp_message_send_udp(
442 message,
443 fd,
444 interface->address.s_addr,
445 address,
446 DHCP_PORT_CLIENT);
447 if (r < 0)
448 return r;
449
450 log_dhcp_relay_interface(interface, "Forwarded BOOTREPLY (0x%"PRIx32") to %s (UDP).",
451 be32toh(message->header.xid),
452 IN4_ADDR_TO_STRING(&(struct in_addr) { .s_addr = address }));
453 return 0;
454 }
455
456 int downstream_send_message(sd_dhcp_relay_interface *interface, sd_dhcp_message *message) {
457 int r;
458
459 assert(interface);
460 assert(!interface->upstream);
461 assert(message);
462 assert(message->header.op == BOOTREPLY);
463
464 /* See RFC 2131 Section 4.1
465 *
466 * (Note, we are a relay agent, hence conditions for giaddr in the statements are ignored.) */
467
468 uint8_t type;
469 r = dhcp_message_get_option_u8(message, SD_DHCP_OPTION_MESSAGE_TYPE, &type);
470 if (r < 0)
471 return r;
472
473 /* the server broadcasts any DHCPNAK messages to 0xffffffff. */
474 if (type == DHCP_NAK)
475 return downstream_send_udp(interface, message, INADDR_BROADCAST);
476
477 /* If (...) the ’ciaddr’ field is nonzero, then the server unicasts DHCPOFFER and DHCPACK messages
478 * to the address in ’ciaddr’. */
479 if (message->header.ciaddr != INADDR_ANY)
480 return downstream_send_udp(interface, message, message->header.ciaddr);
481
482 /* If (...) ’ciaddr’ is zero, and the broadcast bit is set, then the server broadcasts DHCPOFFER
483 * and DHCPACK messages to 0xffffffff.
484 *
485 * (Note, even the broadcast flag is unset, we may not know the client hardware address, e.g.
486 * InfiniBand. In that case, we cannot unicast in the below, so need to broadcast. Also, for other
487 * message types we do not support, also broadcast if 'yiaddr' is zero.) */
488 struct hw_addr_data hw_addr = {};
489 if (!dhcp_message_has_broadcast_flag(message) &&
490 message->header.yiaddr != INADDR_ANY) {
491 r = dhcp_message_get_hw_addr(message, &hw_addr);
492 if (r < 0)
493 return r;
494 }
495
496 if (hw_addr_is_null(&hw_addr))
497 return downstream_send_udp(interface, message, INADDR_BROADCAST);
498
499 /* If the broadcast bit is not set (...) and ’ciaddr’ is zero, then the server unicasts DHCPOFFER
500 * and DHCPACK messages to the client’s hardware address and ’yiaddr’ address. */
501 return downstream_send_l2_unicast(interface, message, &hw_addr);
502 }