Debian Codegraph @

src/libsystemd-network/dhcp-client-send.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <linux/filter.h>
4 #include <net/if_arp.h>
5
6 #include "sd-event.h"
7
8 #include "dhcp-client-internal.h"
9 #include "dhcp-client-send.h"
10 #include "dhcp-lease-internal.h" /* IWYU pragma: keep */
11 #include "dhcp-message.h"
12 #include "errno-util.h"
13 #include "fd-util.h"
14 #include "ip-util.h"
15 #include "socket-util.h"
16 #include "unaligned.h"
17
18 /* The minimal DHCP packet size without IP header:
19 * UDP header (8 bytes) + DHCP header (without options) */
20 #define DHCP_MINIMUM_UDP_SIZE (sizeof(struct udphdr) + sizeof(struct DHCPMessageHeader))
21
22 static int client_set_bpf(sd_dhcp_client *client, int fd) {
23 assert(client);
24 assert(fd >= 0);
25
26 size_t hlen = 0;
27 uint32_t mac_hi = 0;
28 uint16_t mac_lo = 0;
29 if (client->arp_type != ARPHRD_INFINIBAND)
30 hlen = client->hw_addr.length;
31 if (hlen == ETH_ALEN) {
32 mac_hi = unaligned_read_be32(client->hw_addr.bytes);
33 mac_lo = unaligned_read_be16(client->hw_addr.bytes + 4);
34 }
35
36 struct sock_filter filter[] = {
37 /* 1. Basic packet length check.
38 * Check against the minimum possible length.
39 * Note, BPF_MSH extracts the lower 4 bits (IHL) and multiplies by 4 to get the byte length,
40 * it needs to be combined with BPF_LDX. */
41 BPF_STMT(BPF_LDX + BPF_B + BPF_MSH, 0), /* X <- IP header length in bytes */
42 BPF_STMT(BPF_MISC + BPF_TXA, 0), /* A <- X */
43 BPF_STMT(BPF_ALU + BPF_ADD + BPF_K, DHCP_MINIMUM_UDP_SIZE), /* A += UDP header + DHCP header */
44 BPF_STMT(BPF_MISC + BPF_TAX, 0), /* X <- A (minimal DHCP packet size) */
45 BPF_STMT(BPF_LD + BPF_W + BPF_LEN, 0), /* A <- packet length */
46 BPF_JUMP(BPF_JMP + BPF_JGE + BPF_X, 0, 1, 0), /* packet length >= minimal DHCP packet size ? */
47 BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
48
49 /* 2. Protocol check (Fixed offset in IPv4 header) */
50 BPF_STMT(BPF_LD + BPF_B + BPF_ABS, offsetof(struct iphdr, protocol)), /* A <- IP protocol */
51 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_UDP, 1, 0), /* IP protocol == UDP ? */
52 BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
53
54 /* 3. IP Fragmentation checks.
55 * When an IP packet is larger than the MTU, it is fragmented into smaller pieces. The UDP
56 * header is ONLY present in the very first fragment. Since BPF filters are stateless and
57 * cannot reassemble fragments, we must explicitly drop any packet that is part of a
58 * fragmented sequence to avoid parsing raw payload data as if it were a UDP/DHCP header. */
59
60 /* 3a. Check the 'More Fragments' (MF) bit.
61 * If the bit is set, it means there are more fragments following this one. Hence, the packet
62 * must be dropped. */
63 BPF_STMT(BPF_LD + BPF_B + BPF_ABS, offsetof(struct iphdr, frag_off)),
64 BPF_STMT(BPF_ALU + BPF_AND + BPF_K, 0x20), /* A <- A & 0x20 (More Fragments bit) */
65 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, 0, 1, 0), /* A == 0 ? (No more fragments) */
66 BPF_STMT(BPF_RET + BPF_K, 0), /* ignore packet if MF == 1 */
67
68 /* 3b. Check the 'Fragment Offset' field.
69 * This indicates the position of this specific fragment relative to the beginning of the
70 * original, unfragmented packet. If the offset is greater than 0, it means this is a
71 * subsequent fragment (e.g., the 2nd or later piece), hence it must be dropped. */
72 BPF_STMT(BPF_LD + BPF_H + BPF_ABS, offsetof(struct iphdr, frag_off)),
73 BPF_STMT(BPF_ALU + BPF_AND + BPF_K, 0x1fff), /* A <- A & 0x1fff (Fragment offset) */
74 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, 0, 1, 0), /* A == 0 ? (This is the first fragment) */
75 BPF_STMT(BPF_RET + BPF_K, 0), /* ignore packet if Offset != 0 */
76
77 /* 4. Variable Offset Processing (Support for IP Options)
78 * Load the IP header length again. It will be used BPF_IND below. */
79 BPF_STMT(BPF_LDX + BPF_B + BPF_MSH, 0), /* X <- IP header length in bytes */
80
81 /* 4a. Check UDP destination port using indirect load (X + offset) */
82 BPF_STMT(BPF_LD + BPF_H + BPF_IND, offsetof(struct udphdr, dest)), /* A <- (UDP destination port) */
83 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, client->port, 1, 0), /* UDP destination port == 68 ? */
84 BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
85
86 /* 4b. Check DHCP operation code (op) using indirect load (X + UDP header len + op offset) */
87 BPF_STMT(BPF_LD + BPF_B + BPF_IND, sizeof(struct udphdr) + offsetof(DHCPMessageHeader, op)),
88 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, BOOTREPLY, 1, 0), /* op == BOOTREPLY ? */
89 BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
90
91 /* 4c. Check hardware type using indirect load (X + UDP header len + htype offset) */
92 BPF_STMT(BPF_LD + BPF_B + BPF_IND, sizeof(struct udphdr) + offsetof(DHCPMessageHeader, htype)),
93 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, client->arp_type, 1, 0), /* htype == client->arp_type ? */
94 BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
95
96 /* 4d. Check message xid using indirect load (X + UDP header len + xid offset) */
97 BPF_STMT(BPF_LD + BPF_W + BPF_IND, sizeof(struct udphdr) + offsetof(DHCPMessageHeader, xid)),
98 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, client->xid, 1, 0), /* xid == client->xid ? */
99 BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
100
101 /* 4e. Check hardware address length using indirect load (X + UDP header len + hlen offset) */
102 BPF_STMT(BPF_LD + BPF_B + BPF_IND, sizeof(struct udphdr) + offsetof(DHCPMessageHeader, hlen)),
103 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, hlen, 1, 0), /* hlen == expected hlen ? */
104 BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
105
106 /* 4f. Check hardware address when the hardware address length is 6 (ETH_ALEN) */
107 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ETH_ALEN, 0, 6), /* hlen == ETH_ALEN ? */
108 BPF_STMT(BPF_LD + BPF_W + BPF_IND, sizeof(struct udphdr) + offsetof(DHCPMessageHeader, chaddr)),
109 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, mac_hi, 1, 0), /* first 4 bytes of chaddr == mac_hi ? */
110 BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
111 BPF_STMT(BPF_LD + BPF_H + BPF_IND, sizeof(struct udphdr) + offsetof(DHCPMessageHeader, chaddr) + 4),
112 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, mac_lo, 1, 0), /* next 2 bytes of chaddr == mac_lo ? */
113 BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
114
115 /* 4g. Check DHCP magic cookie using indirect load (X + UDP header len + magic cookie offset) */
116 BPF_STMT(BPF_LD + BPF_W + BPF_IND, sizeof(struct udphdr) + offsetof(DHCPMessageHeader, magic)),
117 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, DHCP_MAGIC_COOKIE, 1, 0), /* cookie == DHCP magic cookie ? */
118 BPF_STMT(BPF_RET + BPF_K, 0), /* ignore */
119
120 /* All checks passed, accept the entire packet. */
121 BPF_STMT(BPF_RET + BPF_K, UINT32_MAX), /* accept */
122 };
123
124 struct sock_fprog fprog = {
125 .len = ELEMENTSOF(filter),
126 .filter = filter
127 };
128
129 return RET_NERRNO(setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &fprog, sizeof(fprog)));
130 }
131
132 static int client_open_raw_socket(sd_dhcp_client *client) {
133 int r;
134
135 assert(client);
136
137 _cleanup_close_ int fd = RET_NERRNO(socket(AF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0));
138 if (fd < 0)
139 return fd;
140
141 /* While bind() with sockaddr_ll is strictly sufficient for AF_PACKET, we also set SO_BINDTOIFINDEX
142 * to initialize the kernel's sk_bound_dev_if state. This ensures compatibility with cgroup/eBPF
143 * filters and maintains consistency. */
144 r = socket_bind_to_ifindex(fd, client->ifindex);
145 if (r < 0)
146 return r;
147
148 r = client_set_bpf(client, fd);
149 if (r < 0)
150 return r;
151
152 r = setsockopt_int(fd, SOL_SOCKET, SO_TIMESTAMP, true);
153 if (r < 0)
154 return r;
155
156 r = setsockopt_int(fd, SOL_SOCKET, SO_PRIORITY, client->socket_priority);
157 if (r < 0)
158 return r;
159
160 r = setsockopt_int(fd, SOL_PACKET, PACKET_AUXDATA, true);
161 if (r < 0)
162 return r;
163
164 union sockaddr_union sa = {
165 .ll.sll_family = AF_PACKET,
166 .ll.sll_protocol = htobe16(ETH_P_IP),
167 .ll.sll_ifindex = client->ifindex,
168 };
169
170 if (bind(fd, &sa.sa, sockaddr_ll_len(&sa.ll)) < 0)
171 return -errno;
172
173 return TAKE_FD(fd);
174 }
175
176 static int client_open_udp_socket(sd_dhcp_client *client) {
177 int r;
178
179 assert(client);
180 assert(client->lease);
181
182 _cleanup_close_ int fd = RET_NERRNO(socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0));
183 if (fd < 0)
184 return fd;
185
186 r = socket_bind_to_ifindex(fd, client->ifindex);
187 if (r < 0)
188 return r;
189
190 r = setsockopt_int(fd, SOL_SOCKET, SO_REUSEADDR, true);
191 if (r < 0)
192 return r;
193
194 r = setsockopt_int(fd, SOL_SOCKET, SO_TIMESTAMP, true);
195 if (r < 0)
196 return r;
197
198 r = setsockopt_int(fd, SOL_SOCKET, SO_PRIORITY, client->socket_priority);
199 if (r < 0)
200 return r;
201
202 r = setsockopt_int(fd, IPPROTO_IP, IP_TOS, client->ip_service_type);
203 if (r < 0)
204 return r;
205
206 r = setsockopt_int(fd, IPPROTO_IP, IP_FREEBIND, true);
207 if (r < 0)
208 return r;
209
210 union sockaddr_union sa = {
211 .in.sin_family = AF_INET,
212 .in.sin_port = htobe16(client->port),
213 .in.sin_addr.s_addr = client->lease->address,
214 };
215
216 if (bind(fd, &sa.sa, sizeof(sa.in)) < 0)
217 return -errno;
218
219 return TAKE_FD(fd);
220 }
221
222 static int client_get_socket(sd_dhcp_client *client, int domain) {
223 int r, d, fd;
224
225 assert(client);
226 assert(IN_SET(domain, AF_PACKET, AF_INET));
227
228 /* When a socket fd is given externally, unconditionally use it. */
229 if (client->socket_fd >= 0)
230 return client->socket_fd;
231
232 if (!client->receive_message)
233 return -EBADF;
234
235 fd = sd_event_source_get_io_fd(client->receive_message);
236 if (fd < 0)
237 return fd;
238
239 r = getsockopt_int(fd, SOL_SOCKET, SO_DOMAIN, &d);
240 if (r < 0)
241 return r;
242
243 if (d != domain)
244 return -EBADF;
245
246 return fd;
247 }
248
249 static int client_setup_io_event(
250 sd_dhcp_client *client,
251 int fd,
252 sd_event_io_handler_t callback,
253 const char *description) {
254
255 int r;
256
257 assert(client);
258 assert(fd >= 0);
259 assert(callback);
260 assert(description);
261
262 /* When the socket fd is given externally, the fd is used for both UDP and RAW packet operations.
263 * Hence, first we need to disable the previous event source, otherwise sd_event_add_io() will fail
264 * with -EEXIST. */
265 if (fd == client->socket_fd)
266 client->receive_message = sd_event_source_disable_unref(client->receive_message);
267
268 _cleanup_(sd_event_source_unrefp) sd_event_source *s = NULL;
269 r = sd_event_add_io(client->event, &s, fd, EPOLLIN, callback, client);
270 if (r < 0)
271 return r;
272
273 r = sd_event_source_set_priority(s, client->event_priority);
274 if (r < 0)
275 return r;
276
277 r = sd_event_source_set_description(s, description);
278 if (r < 0)
279 return r;
280
281 /* When the socket fd is given externally, do not close it while we are running. The IO event source
282 * is freed when not necessary, hence the lifetime of the socket fd should not be tied to the one of
283 * the event source in that case. */
284 if (fd != client->socket_fd) {
285 r = sd_event_source_set_io_fd_own(s, true);
286 if (r < 0)
287 return r;
288 }
289
290 sd_event_source_disable_unref(client->receive_message);
291 client->receive_message = TAKE_PTR(s);
292 return 0;
293 }
294
295 static int client_send_raw(
296 sd_dhcp_client *client,
297 sd_dhcp_message *message,
298 bool expect_reply) {
299
300 _cleanup_close_ int fd_close = -EBADF;
301 int r, fd;
302
303 assert(client);
304 assert(message);
305
306 fd = client_get_socket(client, AF_PACKET);
307 if (fd < 0) {
308 fd = fd_close = client_open_raw_socket(client);
309 if (fd < 0)
310 return fd;
311 }
312
313 r = dhcp_message_send_raw(
314 message,
315 fd,
316 client->ifindex,
317 INADDR_ANY,
318 client->port,
319 &client->bcast_addr,
320 INADDR_BROADCAST,
321 client->server_port,
322 client->ip_service_type);
323 if (r < 0)
324 return r;
325
326 if (!expect_reply) {
327 /* We do not expect any replies, hence stop the IO event source if enabled. */
328 client->receive_message = sd_event_source_disable_unref(client->receive_message);
329 return 0;
330 }
331
332 if (fd_close < 0 && fd != client->socket_fd)
333 return 0; /* Already opened socket is reused. Not necessary to setup new IO event source. */
334
335 r = client_setup_io_event(client, fd, client_receive_message_raw, "dhcp4-receive-message-raw");
336 if (r < 0)
337 return r;
338
339 TAKE_FD(fd_close);
340 return 0;
341 }
342
343 static int client_send_udp(
344 sd_dhcp_client *client,
345 sd_dhcp_message *message,
346 bool expect_reply) {
347
348 _cleanup_close_ int fd_close = -EBADF;
349 int r, fd;
350
351 assert(client);
352 assert(message);
353
354 if (!client->lease || client->lease->address == 0)
355 return -EADDRNOTAVAIL;
356
357 fd = client_get_socket(client, AF_INET);
358 if (fd < 0) {
359 fd = fd_close = client_open_udp_socket(client);
360 if (fd < 0)
361 return fd;
362 }
363
364 r = dhcp_message_send_udp(
365 message,
366 fd,
367 INADDR_ANY,
368 client->lease->server_address,
369 client->server_port);
370 if (r < 0)
371 return r;
372
373 if (!expect_reply) {
374 /* We do not expect any replies, hence stop the IO event source if enabled. */
375 client->receive_message = sd_event_source_disable_unref(client->receive_message);
376 return 0;
377 }
378
379 if (fd_close < 0 && fd != client->socket_fd)
380 return 0; /* Already opened socket is reused. Not necessary to setup new IO event source. */
381
382 r = client_setup_io_event(client, fd, client_receive_message_udp, "dhcp4-receive-message-udp");
383 if (r < 0)
384 return r;
385
386 TAKE_FD(fd_close);
387 return 0;
388 }
389
390 static int client_new_message(sd_dhcp_client *client, uint8_t type, sd_dhcp_message **ret) {
391 int r;
392
393 assert(client);
394 assert(IN_SET(type, DHCP_DISCOVER, DHCP_REQUEST, DHCP_RELEASE, DHCP_DECLINE));
395 assert(ret);
396
397 _cleanup_(sd_dhcp_message_unrefp) sd_dhcp_message *message = NULL;
398 r = dhcp_message_new(&message);
399 if (r < 0)
400 return r;
401
402 r = dhcp_message_init_header(
403 message,
404 BOOTREQUEST,
405 client->xid,
406 client->arp_type,
407 &client->hw_addr);
408 if (r < 0)
409 return r;
410
411 /* Although 'secs' field is a SHOULD in RFC 2131, certain DHCP servers refuse to issue a DHCP lease
412 * if 'secs' is set to zero. */
413 usec_t time_now;
414 r = sd_event_now(client->event, CLOCK_BOOTTIME, &time_now);
415 if (r < 0)
416 return r;
417 assert(time_now >= client->start_time);
418
419 /* Seconds between sending first and last DISCOVER must always be strictly positive to deal with
420 * broken servers. */
421 message->header.secs = usec_to_be16_sec(usec_sub_unsigned(time_now, client->start_time) ?: 1 * USEC_PER_SEC);
422
423 /* RFC2131 section 4.1
424 * A client that cannot receive unicast IP datagrams until its protocol software has been configured
425 * with an IP address SHOULD set the BROADCAST bit in the 'flags' field to 1 in any DHCPDISCOVER or
426 * DHCPREQUEST messages that client sends. The BROADCAST bit will provide a hint to the DHCP server
427 * and BOOTP relay agent to broadcast any messages to the client on the client's subnet.
428 *
429 * Note: some interfaces needs this to be enabled, but some networks need this to be disabled as
430 * broadcasts are filtered, so this needs to be configurable. */
431 dhcp_message_set_broadcast_flag(message, client->request_broadcast || client->arp_type != ARPHRD_ETHER);
432
433 /* We append no vendor options on BOOTP mode. */
434 if (client->bootp) {
435 *ret = TAKE_PTR(message);
436 return 0;
437 }
438
439 /* DHCP Message Type (53): Mandatory. */
440 r = dhcp_message_append_option_u8(message, SD_DHCP_OPTION_MESSAGE_TYPE, type);
441 if (r < 0)
442 return r;
443
444 /* Server Identifier (54): mandatory in DHCPREQUEST on REQUESTING state. It is also mandatory when
445 * DHCPRELEASE and DHCPDECLINE. */
446 if ((type == DHCP_REQUEST && client->state == DHCP_STATE_REQUESTING) ||
447 IN_SET(type, DHCP_RELEASE, DHCP_DECLINE)) {
448 r = dhcp_message_append_option_be32(
449 message,
450 SD_DHCP_OPTION_SERVER_IDENTIFIER,
451 ASSERT_PTR(client->lease)->server_address);
452 if (r < 0)
453 return r;
454 }
455
456 /* Client Identifier (61): Not mandatory, but some DHCP servers will reject messages without client
457 * identifier option. Hence, we always set it. */
458 r = dhcp_message_append_option_client_id(message, &client->client_id);
459 if (r < 0)
460 return r;
461
462 /* Requested IP Address option (50) or ciaddr
463 *
464 * See RFC2131 section 4.3.2 (note that there is a typo in the RFC, SELECTING should be REQUESTING). */
465 be32_t addr = INADDR_ANY;
466 switch (type) {
467 case DHCP_DISCOVER:
468 /* the client may suggest values for the network address and lease time in the DHCPDISCOVER
469 * message. The client may include the ’requested IP address’ option to suggest that a
470 * particular IP address be assigned, and may include the ’IP address lease time’ option to
471 * suggest the lease time it would like.
472 *
473 * RFC7844 section 3:
474 * SHOULD NOT contain any other option (when running on anonymize mode). */
475 if (!client->anonymize)
476 addr = client->last_addr;
477 break;
478
479 case DHCP_REQUEST:
480 switch (client->state) {
481
482 case DHCP_STATE_REQUESTING:
483 /* ’ciaddr’ MUST be zero, ’requested IP address’ MUST be filled in with the
484 * yiaddr value from the chosen DHCPOFFER. */
485 addr = ASSERT_PTR(client->lease)->address;
486 break;
487
488 case DHCP_STATE_REBOOTING:
489 /* ’requested IP address’ option MUST be filled in with client’s notion of its
490 * previously assigned address. ’ciaddr’ MUST be zero. */
491 addr = client->last_addr;
492 break;
493
494 case DHCP_STATE_RENEWING:
495 case DHCP_STATE_REBINDING:
496 /* ’requested IP address’ option MUST NOT be filled in, ’ciaddr’ MUST be filled
497 * in with client’s IP address. */
498 message->header.ciaddr = ASSERT_PTR(client->lease)->address;
499 break;
500
501 default:
502 assert_not_reached();
503 }
504 break;
505
506 case DHCP_RELEASE:
507 /* The acquired address must be set in ciaddr. */
508 message->header.ciaddr = ASSERT_PTR(client->lease)->address;
509 break;
510
511 case DHCP_DECLINE:
512 /* The acquired address must be set in Requested IP Address option. */
513 addr = ASSERT_PTR(client->lease)->address;
514 break;
515
516 default:
517 assert_not_reached();
518 }
519
520 if (addr != INADDR_ANY) {
521 r = dhcp_message_append_option_be32(message, SD_DHCP_OPTION_REQUESTED_IP_ADDRESS, addr);
522 if (r < 0)
523 return r;
524 }
525
526 /* DHCPRELEASE and DHCPDECLINE MUST NOT contain any other options. */
527 if (IN_SET(type, DHCP_RELEASE, DHCP_DECLINE)) {
528 *ret = TAKE_PTR(message);
529 return 0;
530 }
531
532 /* Parameter Request List (55)
533 *
534 * RFC2131 section 3.5:
535 * in its initial DHCPDISCOVER or DHCPREQUEST message, a client may provide the server with a list of
536 * specific parameters the client is interested in. If the client includes a list of parameters in a
537 * DHCPDISCOVER message, it MUST include that list in any subsequent DHCPREQUEST messages.
538 *
539 * RFC7844 section 3:
540 * MAY contain the Parameter Request List option.
541 *
542 * RFC7844 section 3.6:
543 * The client intending to protect its privacy SHOULD only request a minimal number of options in the
544 * PRL and SHOULD also randomly shuffle the ordering of option codes in the PRL. If this random
545 * ordering cannot be implemented, the client MAY order the option codes in the PRL by option code
546 * number (lowest to highest).
547 *
548 * NOTE: using PRL options that Windows 10 RFC7844 implementation uses. */
549 if (client->anonymize) {
550 static const uint8_t default_req_opts_anonymize[] = {
551 SD_DHCP_OPTION_SUBNET_MASK, /* 1 */
552 SD_DHCP_OPTION_ROUTER, /* 3 */
553 SD_DHCP_OPTION_DOMAIN_NAME_SERVER, /* 6 */
554 SD_DHCP_OPTION_DOMAIN_NAME, /* 15 */
555 SD_DHCP_OPTION_ROUTER_DISCOVERY, /* 31 */
556 SD_DHCP_OPTION_STATIC_ROUTE, /* 33 */
557 SD_DHCP_OPTION_VENDOR_SPECIFIC_INFORMATION, /* 43 */
558 SD_DHCP_OPTION_NETBIOS_NAME_SERVER, /* 44 */
559 SD_DHCP_OPTION_NETBIOS_NODE_TYPE, /* 46 */
560 SD_DHCP_OPTION_NETBIOS_SCOPE, /* 47 */
561 SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE, /* 121 */
562 SD_DHCP_OPTION_PRIVATE_CLASSLESS_STATIC_ROUTE, /* 249 */
563 SD_DHCP_OPTION_PRIVATE_PROXY_AUTODISCOVERY, /* 252 */
564 };
565
566 r = dhcp_message_append_option(
567 message,
568 SD_DHCP_OPTION_PARAMETER_REQUEST_LIST,
569 ELEMENTSOF(default_req_opts_anonymize),
570 default_req_opts_anonymize);
571 if (r < 0)
572 return r;
573
574 /* RFC7844 section 3:
575 * SHOULD NOT contain any other option (when running on anonymize mode). */
576 *ret = TAKE_PTR(message);
577 return 0;
578 }
579
580 /* When not on anonymized mode, use the default + user requested options. */
581 r = dhcp_message_append_option_parameter_request_list(message, client->req_opts);
582 if (r < 0)
583 return r;
584
585 /* Maximum Message Size (57)
586 *
587 * RFC2131 section 3.5:
588 * The client SHOULD include the ’maximum DHCP message size’ option to let the server know how
589 * large the server may make its DHCP messages.
590 *
591 * Note (from ConnMan): Some DHCP servers will send bigger DHCP packets than the defined default size
592 * unless the Maximum Message Size option is explicitly set.
593 *
594 * RFC3442 "Requirements to Avoid Sizing Constraints":
595 * Because a full routing table can be quite large, the standard 576 octet maximum size for a DHCP
596 * message may be too short to contain some legitimate Classless Static Route options. Because of
597 * this, clients implementing the Classless Static Route option SHOULD send a Maximum DHCP Message
598 * Size [4] option if the DHCP client's TCP/IP stack is capable of receiving larger IP datagrams.
599 * In this case, the client SHOULD set the value of this option to at least the MTU of the interface
600 * that the client is configuring. The client MAY set the value of this option higher, up to the size
601 * of the largest UDP packet it is prepared to accept. (Note that the value specified in the Maximum
602 * DHCP Message Size option is the total maximum packet size, including IP and UDP headers.) */
603 r = dhcp_message_append_option_u16(
604 message,
605 SD_DHCP_OPTION_MAXIMUM_MESSAGE_SIZE,
606 CLAMP(client->mtu, (uint32_t) IPV4_MIN_REASSEMBLY_SIZE, (uint32_t) UINT16_MAX));
607 if (r < 0)
608 return r;
609
610 /* Hostname (12) or FQDN (81)
611 *
612 * Note, it is unclear from RFC 2131 if client should send hostname in DHCPDISCOVER but dhclient does
613 * and so we do as well. */
614 r = dhcp_message_append_option_hostname(
615 message,
616 DHCP_FQDN_FLAG_S, /* Request server to perform A RR DNS updates */
617 /* is_client= */ true,
618 client->hostname);
619 if (r < 0)
620 return r;
621
622 /* Vendor Specific (43) */
623 r = dhcp_message_append_option_sub_tlv(
624 message,
625 SD_DHCP_OPTION_VENDOR_SPECIFIC_INFORMATION,
626 client->vendor_options);
627 if (r < 0)
628 return r;
629
630 /* Vendor Class Identifier (60) */
631 r = dhcp_message_append_option_string(
632 message,
633 SD_DHCP_OPTION_VENDOR_CLASS_IDENTIFIER,
634 client->vendor_class_identifier);
635 if (r < 0)
636 return r;
637
638 /* User Class (77) */
639 r = dhcp_message_append_option_length_prefixed_data(
640 message,
641 SD_DHCP_OPTION_USER_CLASS,
642 /* length_size= */ 1,
643 &client->user_class);
644 if (r < 0)
645 return r;
646
647 /* Rapid Commit (80): only for DHCPDISCOVER */
648 if (client->rapid_commit && type == DHCP_DISCOVER) {
649 r = dhcp_message_append_option_flag(message, SD_DHCP_OPTION_RAPID_COMMIT);
650 if (r < 0)
651 return r;
652 }
653
654 /* MUD URL (161) */
655 r = dhcp_message_append_option_string(message, SD_DHCP_OPTION_MUD_URL, client->mudurl);
656 if (r < 0)
657 return r;
658
659 r = dhcp_message_append_option_tlv(message, client->extra_options);
660 if (r < 0)
661 return r;
662
663 *ret = TAKE_PTR(message);
664 return 0;
665 }
666
667 int dhcp_client_send_message(sd_dhcp_client *client, uint8_t type) {
668 int r;
669
670 assert(client);
671
672 _cleanup_(sd_dhcp_message_unrefp) sd_dhcp_message *message = NULL;
673 r = client_new_message(client, type, &message);
674 if (r < 0)
675 return r;
676
677 switch (type) {
678 case DHCP_DISCOVER:
679 r = client_send_raw(client, message, /* expect_reply= */ true);
680 break;
681 case DHCP_REQUEST:
682 if (client->state == DHCP_STATE_RENEWING)
683 r = client_send_udp(client, message, /* expect_reply= */ true);
684 else
685 r = client_send_raw(client, message, /* expect_reply= */ true);
686 break;
687 case DHCP_RELEASE:
688 r = client_send_udp(client, message, /* expect_reply= */ false);
689 break;
690 case DHCP_DECLINE:
691 r = client_send_raw(client, message, /* expect_reply= */ false);
692 break;
693 default:
694 r = -EINVAL;
695 }
696 if (r < 0)
697 return r;
698
699 if (client->bootp)
700 log_dhcp_client(client, "BOOTREQUEST");
701 else if (type == DHCP_REQUEST)
702 log_dhcp_client(client, "%s (%s)",
703 dhcp_message_type_to_string(type),
704 dhcp_state_to_string(client->state));
705 else
706 log_dhcp_client(client, "%s", dhcp_message_type_to_string(type));
707 return 0;
708 }