]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/networkd-dhcp-common.c
tree-wide: use ASSERT_PTR more
[thirdparty/systemd.git] / src / network / networkd-dhcp-common.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <netinet/in.h>
4 #include <linux/if_arp.h>
5
6 #include "bus-error.h"
7 #include "bus-locator.h"
8 #include "dhcp-identifier.h"
9 #include "dhcp-internal.h"
10 #include "dhcp6-internal.h"
11 #include "escape.h"
12 #include "hexdecoct.h"
13 #include "in-addr-prefix-util.h"
14 #include "networkd-dhcp-common.h"
15 #include "networkd-link.h"
16 #include "networkd-manager.h"
17 #include "networkd-network.h"
18 #include "parse-util.h"
19 #include "socket-util.h"
20 #include "string-table.h"
21 #include "strv.h"
22 #include "vrf.h"
23
24 static uint32_t link_get_vrf_table(Link *link) {
25 assert(link);
26 assert(link->network);
27
28 return link->network->vrf ? VRF(link->network->vrf)->table : RT_TABLE_MAIN;
29 }
30
31 uint32_t link_get_dhcp4_route_table(Link *link) {
32 assert(link);
33 assert(link->network);
34
35 /* When the interface is part of an VRF use the VRFs routing table, unless
36 * another table is explicitly specified. */
37
38 if (link->network->dhcp_route_table_set)
39 return link->network->dhcp_route_table;
40 return link_get_vrf_table(link);
41 }
42
43 uint32_t link_get_ipv6_accept_ra_route_table(Link *link) {
44 assert(link);
45 assert(link->network);
46
47 if (link->network->ipv6_accept_ra_route_table_set)
48 return link->network->ipv6_accept_ra_route_table;
49 return link_get_vrf_table(link);
50 }
51
52 bool link_dhcp_enabled(Link *link, int family) {
53 assert(link);
54 assert(IN_SET(family, AF_INET, AF_INET6));
55
56 /* Currently, sd-dhcp-client supports only ethernet and infiniband. */
57 if (family == AF_INET && !IN_SET(link->iftype, ARPHRD_ETHER, ARPHRD_INFINIBAND))
58 return false;
59
60 if (family == AF_INET6 && !socket_ipv6_is_supported())
61 return false;
62
63 if (link->flags & IFF_LOOPBACK)
64 return false;
65
66 if (link->iftype == ARPHRD_CAN)
67 return false;
68
69 if (!link->network)
70 return false;
71
72 return link->network->dhcp & (family == AF_INET ? ADDRESS_FAMILY_IPV4 : ADDRESS_FAMILY_IPV6);
73 }
74
75 void network_adjust_dhcp(Network *network) {
76 assert(network);
77 assert(network->dhcp >= 0);
78
79 if (network->dhcp == ADDRESS_FAMILY_NO)
80 return;
81
82 /* Bonding slave does not support addressing. */
83 if (network->bond) {
84 log_warning("%s: Cannot enable DHCP= when Bond= is specified, disabling DHCP=.",
85 network->filename);
86 network->dhcp = ADDRESS_FAMILY_NO;
87 return;
88 }
89
90 if (!FLAGS_SET(network->link_local, ADDRESS_FAMILY_IPV6) &&
91 FLAGS_SET(network->dhcp, ADDRESS_FAMILY_IPV6)) {
92 log_warning("%s: DHCPv6 client is enabled but IPv6 link-local addressing is disabled. "
93 "Disabling DHCPv6 client.", network->filename);
94 SET_FLAG(network->dhcp, ADDRESS_FAMILY_IPV6, false);
95 }
96
97 network_adjust_dhcp4(network);
98 }
99
100 static bool duid_needs_product_uuid(const DUID *duid) {
101 assert(duid);
102
103 return duid->type == DUID_TYPE_UUID && duid->raw_data_len == 0;
104 }
105
106 static const struct DUID fallback_duid = { .type = DUID_TYPE_EN };
107
108 const DUID *link_get_duid(Link *link, int family) {
109 const DUID *duid;
110
111 assert(link);
112 assert(IN_SET(family, AF_INET, AF_INET6));
113
114 if (link->network) {
115 duid = family == AF_INET ? &link->network->dhcp_duid : &link->network->dhcp6_duid;
116 if (duid->type != _DUID_TYPE_INVALID) {
117 if (duid_needs_product_uuid(duid))
118 return &link->manager->duid_product_uuid;
119 else
120 return duid;
121 }
122 }
123
124 duid = family == AF_INET ? &link->manager->dhcp_duid : &link->manager->dhcp6_duid;
125 if (link->hw_addr.length == 0 && IN_SET(duid->type, DUID_TYPE_LLT, DUID_TYPE_LL))
126 /* Fallback to DUID that works without MAC address.
127 * This is useful for tunnel devices without MAC address. */
128 return &fallback_duid;
129
130 return duid;
131 }
132
133 static int get_product_uuid_handler(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
134 Manager *manager = ASSERT_PTR(userdata);
135 const sd_bus_error *e;
136 const void *a;
137 size_t sz;
138 int r;
139
140 assert(m);
141
142 /* To avoid calling GetProductUUID() bus method so frequently, set the flag below
143 * even if the method fails. */
144 manager->has_product_uuid = true;
145
146 e = sd_bus_message_get_error(m);
147 if (e) {
148 r = sd_bus_error_get_errno(e);
149 log_warning_errno(r, "Could not get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %s",
150 bus_error_message(e, r));
151 return 0;
152 }
153
154 r = sd_bus_message_read_array(m, 'y', &a, &sz);
155 if (r < 0) {
156 log_warning_errno(r, "Failed to get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %m");
157 return 0;
158 }
159
160 if (sz != sizeof(sd_id128_t)) {
161 log_warning("Invalid product UUID. Falling back to use machine-app-specific ID as DUID-UUID.");
162 return 0;
163 }
164
165 log_debug("Successfully obtained product UUID");
166
167 memcpy(&manager->duid_product_uuid.raw_data, a, sz);
168 manager->duid_product_uuid.raw_data_len = sz;
169
170 return 0;
171 }
172
173 int manager_request_product_uuid(Manager *m) {
174 static bool bus_method_is_called = false;
175 int r;
176
177 assert(m);
178
179 if (bus_method_is_called)
180 return 0;
181
182 if (sd_bus_is_ready(m->bus) <= 0 && !m->product_uuid_requested) {
183 log_debug("Not connected to system bus, requesting product UUID later.");
184 m->product_uuid_requested = true;
185 return 0;
186 }
187
188 m->product_uuid_requested = false;
189
190 r = bus_call_method_async(
191 m->bus,
192 NULL,
193 bus_hostname,
194 "GetProductUUID",
195 get_product_uuid_handler,
196 m,
197 "b",
198 false);
199 if (r < 0)
200 return log_warning_errno(r, "Failed to get product UUID: %m");
201
202 log_debug("Requesting product UUID.");
203
204 bus_method_is_called = true;
205
206 return 0;
207 }
208
209 int dhcp_configure_duid(Link *link, const DUID *duid) {
210 Manager *m;
211 int r;
212
213 assert(link);
214 assert(link->manager);
215 assert(duid);
216
217 m = link->manager;
218
219 if (!duid_needs_product_uuid(duid))
220 return 1;
221
222 if (m->has_product_uuid)
223 return 1;
224
225 r = manager_request_product_uuid(m);
226 if (r < 0) {
227 log_link_warning_errno(link, r,
228 "Failed to get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %m");
229
230 m->has_product_uuid = true; /* Do not request UUID again on failure. */
231 return 1;
232 }
233
234 return 0;
235 }
236
237 bool address_is_filtered(int family, const union in_addr_union *address, uint8_t prefixlen, Set *allow_list, Set *deny_list) {
238 struct in_addr_prefix *p;
239
240 assert(IN_SET(family, AF_INET, AF_INET6));
241 assert(address);
242
243 if (allow_list) {
244 SET_FOREACH(p, allow_list)
245 if (p->family == family &&
246 p->prefixlen <= prefixlen &&
247 in_addr_prefix_covers(family, &p->address, p->prefixlen, address) > 0)
248 return false;
249
250 return true;
251 }
252
253 SET_FOREACH(p, deny_list)
254 if (p->family == family &&
255 in_addr_prefix_intersect(family, &p->address, p->prefixlen, address, prefixlen) > 0)
256 return true;
257
258 return false;
259 }
260
261 int config_parse_dhcp(
262 const char* unit,
263 const char *filename,
264 unsigned line,
265 const char *section,
266 unsigned section_line,
267 const char *lvalue,
268 int ltype,
269 const char *rvalue,
270 void *data,
271 void *userdata) {
272
273 AddressFamily *dhcp = data, s;
274
275 assert(filename);
276 assert(lvalue);
277 assert(rvalue);
278 assert(data);
279
280 /* Note that this is mostly like
281 * config_parse_address_family(), except that it
282 * understands some old names for the enum values */
283
284 s = address_family_from_string(rvalue);
285 if (s < 0) {
286
287 /* Previously, we had a slightly different enum here,
288 * support its values for compatibility. */
289
290 s = dhcp_deprecated_address_family_from_string(rvalue);
291 if (s < 0) {
292 log_syntax(unit, LOG_WARNING, filename, line, s,
293 "Failed to parse DHCP option, ignoring: %s", rvalue);
294 return 0;
295 }
296
297 log_syntax(unit, LOG_WARNING, filename, line, 0,
298 "DHCP=%s is deprecated, please use DHCP=%s instead.",
299 rvalue, address_family_to_string(s));
300 }
301
302 *dhcp = s;
303 return 0;
304 }
305
306 int config_parse_dhcp_or_ra_route_metric(
307 const char* unit,
308 const char *filename,
309 unsigned line,
310 const char *section,
311 unsigned section_line,
312 const char *lvalue,
313 int ltype,
314 const char *rvalue,
315 void *data,
316 void *userdata) {
317
318 Network *network = userdata;
319 uint32_t metric;
320 int r;
321
322 assert(filename);
323 assert(lvalue);
324 assert(IN_SET(ltype, AF_UNSPEC, AF_INET, AF_INET6));
325 assert(rvalue);
326 assert(data);
327
328 r = safe_atou32(rvalue, &metric);
329 if (r < 0) {
330 log_syntax(unit, LOG_WARNING, filename, line, r,
331 "Failed to parse RouteMetric=%s, ignoring assignment: %m", rvalue);
332 return 0;
333 }
334
335 switch (ltype) {
336 case AF_INET:
337 network->dhcp_route_metric = metric;
338 network->dhcp_route_metric_set = true;
339 break;
340 case AF_INET6:
341 network->ipv6_accept_ra_route_metric = metric;
342 network->ipv6_accept_ra_route_metric_set = true;
343 break;
344 case AF_UNSPEC:
345 /* For backward compatibility. */
346 if (!network->dhcp_route_metric_set)
347 network->dhcp_route_metric = metric;
348 if (!network->ipv6_accept_ra_route_metric_set)
349 network->ipv6_accept_ra_route_metric = metric;
350 break;
351 default:
352 assert_not_reached();
353 }
354
355 return 0;
356 }
357
358 int config_parse_dhcp_use_dns(
359 const char* unit,
360 const char *filename,
361 unsigned line,
362 const char *section,
363 unsigned section_line,
364 const char *lvalue,
365 int ltype,
366 const char *rvalue,
367 void *data,
368 void *userdata) {
369
370 Network *network = userdata;
371 int r;
372
373 assert(filename);
374 assert(lvalue);
375 assert(IN_SET(ltype, AF_UNSPEC, AF_INET, AF_INET6));
376 assert(rvalue);
377 assert(data);
378
379 r = parse_boolean(rvalue);
380 if (r < 0) {
381 log_syntax(unit, LOG_WARNING, filename, line, r,
382 "Failed to parse UseDNS=%s, ignoring assignment: %m", rvalue);
383 return 0;
384 }
385
386 switch (ltype) {
387 case AF_INET:
388 network->dhcp_use_dns = r;
389 network->dhcp_use_dns_set = true;
390 break;
391 case AF_INET6:
392 network->dhcp6_use_dns = r;
393 network->dhcp6_use_dns_set = true;
394 break;
395 case AF_UNSPEC:
396 /* For backward compatibility. */
397 if (!network->dhcp_use_dns_set)
398 network->dhcp_use_dns = r;
399 if (!network->dhcp6_use_dns_set)
400 network->dhcp6_use_dns = r;
401 break;
402 default:
403 assert_not_reached();
404 }
405
406 return 0;
407 }
408
409 int config_parse_dhcp_use_domains(
410 const char* unit,
411 const char *filename,
412 unsigned line,
413 const char *section,
414 unsigned section_line,
415 const char *lvalue,
416 int ltype,
417 const char *rvalue,
418 void *data,
419 void *userdata) {
420
421 Network *network = userdata;
422 DHCPUseDomains d;
423
424 assert(filename);
425 assert(lvalue);
426 assert(IN_SET(ltype, AF_UNSPEC, AF_INET, AF_INET6));
427 assert(rvalue);
428 assert(data);
429
430 d = dhcp_use_domains_from_string(rvalue);
431 if (d < 0) {
432 log_syntax(unit, LOG_WARNING, filename, line, d,
433 "Failed to parse %s=%s, ignoring assignment: %m", lvalue, rvalue);
434 return 0;
435 }
436
437 switch (ltype) {
438 case AF_INET:
439 network->dhcp_use_domains = d;
440 network->dhcp_use_domains_set = true;
441 break;
442 case AF_INET6:
443 network->dhcp6_use_domains = d;
444 network->dhcp6_use_domains_set = true;
445 break;
446 case AF_UNSPEC:
447 /* For backward compatibility. */
448 if (!network->dhcp_use_domains_set)
449 network->dhcp_use_domains = d;
450 if (!network->dhcp6_use_domains_set)
451 network->dhcp6_use_domains = d;
452 break;
453 default:
454 assert_not_reached();
455 }
456
457 return 0;
458 }
459
460 int config_parse_dhcp_use_ntp(
461 const char* unit,
462 const char *filename,
463 unsigned line,
464 const char *section,
465 unsigned section_line,
466 const char *lvalue,
467 int ltype,
468 const char *rvalue,
469 void *data,
470 void *userdata) {
471
472 Network *network = userdata;
473 int r;
474
475 assert(filename);
476 assert(lvalue);
477 assert(IN_SET(ltype, AF_UNSPEC, AF_INET, AF_INET6));
478 assert(rvalue);
479 assert(data);
480
481 r = parse_boolean(rvalue);
482 if (r < 0) {
483 log_syntax(unit, LOG_WARNING, filename, line, r,
484 "Failed to parse UseNTP=%s, ignoring assignment: %m", rvalue);
485 return 0;
486 }
487
488 switch (ltype) {
489 case AF_INET:
490 network->dhcp_use_ntp = r;
491 network->dhcp_use_ntp_set = true;
492 break;
493 case AF_INET6:
494 network->dhcp6_use_ntp = r;
495 network->dhcp6_use_ntp_set = true;
496 break;
497 case AF_UNSPEC:
498 /* For backward compatibility. */
499 if (!network->dhcp_use_ntp_set)
500 network->dhcp_use_ntp = r;
501 if (!network->dhcp6_use_ntp_set)
502 network->dhcp6_use_ntp = r;
503 break;
504 default:
505 assert_not_reached();
506 }
507
508 return 0;
509 }
510
511 int config_parse_dhcp_or_ra_route_table(
512 const char *unit,
513 const char *filename,
514 unsigned line,
515 const char *section,
516 unsigned section_line,
517 const char *lvalue,
518 int ltype,
519 const char *rvalue,
520 void *data,
521 void *userdata) {
522
523 Network *network = ASSERT_PTR(userdata);
524 uint32_t rt;
525 int r;
526
527 assert(filename);
528 assert(lvalue);
529 assert(IN_SET(ltype, AF_INET, AF_INET6));
530 assert(rvalue);
531
532 r = safe_atou32(rvalue, &rt);
533 if (r < 0) {
534 log_syntax(unit, LOG_WARNING, filename, line, r,
535 "Failed to parse RouteTable=%s, ignoring assignment: %m", rvalue);
536 return 0;
537 }
538
539 switch (ltype) {
540 case AF_INET:
541 network->dhcp_route_table = rt;
542 network->dhcp_route_table_set = true;
543 break;
544 case AF_INET6:
545 network->ipv6_accept_ra_route_table = rt;
546 network->ipv6_accept_ra_route_table_set = true;
547 break;
548 default:
549 assert_not_reached();
550 }
551
552 return 0;
553 }
554
555 int config_parse_iaid(
556 const char *unit,
557 const char *filename,
558 unsigned line,
559 const char *section,
560 unsigned section_line,
561 const char *lvalue,
562 int ltype,
563 const char *rvalue,
564 void *data,
565 void *userdata) {
566
567 Network *network = ASSERT_PTR(userdata);
568 uint32_t iaid;
569 int r;
570
571 assert(filename);
572 assert(lvalue);
573 assert(rvalue);
574 assert(IN_SET(ltype, AF_INET, AF_INET6));
575
576 r = safe_atou32(rvalue, &iaid);
577 if (r < 0) {
578 log_syntax(unit, LOG_WARNING, filename, line, r,
579 "Unable to read IAID, ignoring assignment: %s", rvalue);
580 return 0;
581 }
582
583 if (ltype == AF_INET) {
584 network->dhcp_iaid = iaid;
585 network->dhcp_iaid_set = true;
586 if (!network->dhcp6_iaid_set_explicitly) {
587 /* Backward compatibility. Previously, IAID is shared by DHCPv4 and DHCPv6.
588 * If DHCPv6 IAID is not specified explicitly, then use DHCPv4 IAID for DHCPv6. */
589 network->dhcp6_iaid = iaid;
590 network->dhcp6_iaid_set = true;
591 }
592 } else {
593 assert(ltype == AF_INET6);
594 network->dhcp6_iaid = iaid;
595 network->dhcp6_iaid_set = true;
596 network->dhcp6_iaid_set_explicitly = true;
597 }
598
599 return 0;
600 }
601
602 int config_parse_dhcp_user_or_vendor_class(
603 const char *unit,
604 const char *filename,
605 unsigned line,
606 const char *section,
607 unsigned section_line,
608 const char *lvalue,
609 int ltype,
610 const char *rvalue,
611 void *data,
612 void *userdata) {
613
614 char ***l = ASSERT_PTR(data);
615 int r;
616
617 assert(lvalue);
618 assert(rvalue);
619 assert(IN_SET(ltype, AF_INET, AF_INET6));
620
621 if (isempty(rvalue)) {
622 *l = strv_free(*l);
623 return 0;
624 }
625
626 for (const char *p = rvalue;;) {
627 _cleanup_free_ char *w = NULL;
628 size_t len;
629
630 r = extract_first_word(&p, &w, NULL, EXTRACT_CUNESCAPE|EXTRACT_UNQUOTE);
631 if (r == -ENOMEM)
632 return log_oom();
633 if (r < 0) {
634 log_syntax(unit, LOG_WARNING, filename, line, r,
635 "Failed to split user classes option, ignoring: %s", rvalue);
636 return 0;
637 }
638 if (r == 0)
639 return 0;
640
641 len = strlen(w);
642 if (ltype == AF_INET) {
643 if (len > UINT8_MAX || len == 0) {
644 log_syntax(unit, LOG_WARNING, filename, line, 0,
645 "%s length is not in the range 1…255, ignoring.", w);
646 continue;
647 }
648 } else {
649 if (len > UINT16_MAX || len == 0) {
650 log_syntax(unit, LOG_WARNING, filename, line, 0,
651 "%s length is not in the range 1…65535, ignoring.", w);
652 continue;
653 }
654 }
655
656 r = strv_consume(l, TAKE_PTR(w));
657 if (r < 0)
658 return log_oom();
659 }
660 }
661
662 int config_parse_dhcp_send_option(
663 const char *unit,
664 const char *filename,
665 unsigned line,
666 const char *section,
667 unsigned section_line,
668 const char *lvalue,
669 int ltype,
670 const char *rvalue,
671 void *data,
672 void *userdata) {
673
674 _cleanup_(sd_dhcp_option_unrefp) sd_dhcp_option *opt4 = NULL;
675 _cleanup_(sd_dhcp6_option_unrefp) sd_dhcp6_option *opt6 = NULL;
676 _unused_ _cleanup_(sd_dhcp_option_unrefp) sd_dhcp_option *old4 = NULL;
677 _unused_ _cleanup_(sd_dhcp6_option_unrefp) sd_dhcp6_option *old6 = NULL;
678 uint32_t uint32_data, enterprise_identifier = 0;
679 _cleanup_free_ char *word = NULL, *q = NULL;
680 OrderedHashmap **options = ASSERT_PTR(data);
681 uint16_t u16, uint16_data;
682 union in_addr_union addr;
683 DHCPOptionDataType type;
684 uint8_t u8, uint8_data;
685 const void *udata;
686 const char *p;
687 ssize_t sz;
688 int r;
689
690 assert(filename);
691 assert(lvalue);
692 assert(rvalue);
693
694 if (isempty(rvalue)) {
695 *options = ordered_hashmap_free(*options);
696 return 0;
697 }
698
699 p = rvalue;
700 if (ltype == AF_INET6 && streq(lvalue, "SendVendorOption")) {
701 r = extract_first_word(&p, &word, ":", 0);
702 if (r == -ENOMEM)
703 return log_oom();
704 if (r <= 0 || isempty(p)) {
705 log_syntax(unit, LOG_WARNING, filename, line, r,
706 "Invalid DHCP option, ignoring assignment: %s", rvalue);
707 return 0;
708 }
709
710 r = safe_atou32(word, &enterprise_identifier);
711 if (r < 0) {
712 log_syntax(unit, LOG_WARNING, filename, line, r,
713 "Failed to parse DHCPv6 enterprise identifier data, ignoring assignment: %s", p);
714 return 0;
715 }
716 word = mfree(word);
717 }
718
719 r = extract_first_word(&p, &word, ":", 0);
720 if (r == -ENOMEM)
721 return log_oom();
722 if (r <= 0 || isempty(p)) {
723 log_syntax(unit, LOG_WARNING, filename, line, r,
724 "Invalid DHCP option, ignoring assignment: %s", rvalue);
725 return 0;
726 }
727
728 if (ltype == AF_INET6) {
729 r = safe_atou16(word, &u16);
730 if (r < 0) {
731 log_syntax(unit, LOG_WARNING, filename, line, r,
732 "Invalid DHCP option, ignoring assignment: %s", rvalue);
733 return 0;
734 }
735 if (u16 < 1 || u16 >= UINT16_MAX) {
736 log_syntax(unit, LOG_WARNING, filename, line, 0,
737 "Invalid DHCP option, valid range is 1-65535, ignoring assignment: %s", rvalue);
738 return 0;
739 }
740 } else {
741 r = safe_atou8(word, &u8);
742 if (r < 0) {
743 log_syntax(unit, LOG_WARNING, filename, line, r,
744 "Invalid DHCP option, ignoring assignment: %s", rvalue);
745 return 0;
746 }
747 if (u8 < 1 || u8 >= UINT8_MAX) {
748 log_syntax(unit, LOG_WARNING, filename, line, 0,
749 "Invalid DHCP option, valid range is 1-254, ignoring assignment: %s", rvalue);
750 return 0;
751 }
752 }
753
754 word = mfree(word);
755 r = extract_first_word(&p, &word, ":", 0);
756 if (r == -ENOMEM)
757 return log_oom();
758 if (r <= 0 || isempty(p)) {
759 log_syntax(unit, LOG_WARNING, filename, line, r,
760 "Invalid DHCP option, ignoring assignment: %s", rvalue);
761 return 0;
762 }
763
764 type = dhcp_option_data_type_from_string(word);
765 if (type < 0) {
766 log_syntax(unit, LOG_WARNING, filename, line, type,
767 "Invalid DHCP option data type, ignoring assignment: %s", p);
768 return 0;
769 }
770
771 switch (type) {
772 case DHCP_OPTION_DATA_UINT8:{
773 r = safe_atou8(p, &uint8_data);
774 if (r < 0) {
775 log_syntax(unit, LOG_WARNING, filename, line, r,
776 "Failed to parse DHCP uint8 data, ignoring assignment: %s", p);
777 return 0;
778 }
779
780 udata = &uint8_data;
781 sz = sizeof(uint8_t);
782 break;
783 }
784 case DHCP_OPTION_DATA_UINT16:{
785 uint16_t k;
786
787 r = safe_atou16(p, &k);
788 if (r < 0) {
789 log_syntax(unit, LOG_WARNING, filename, line, r,
790 "Failed to parse DHCP uint16 data, ignoring assignment: %s", p);
791 return 0;
792 }
793
794 uint16_data = htobe16(k);
795 udata = &uint16_data;
796 sz = sizeof(uint16_t);
797 break;
798 }
799 case DHCP_OPTION_DATA_UINT32: {
800 uint32_t k;
801
802 r = safe_atou32(p, &k);
803 if (r < 0) {
804 log_syntax(unit, LOG_WARNING, filename, line, r,
805 "Failed to parse DHCP uint32 data, ignoring assignment: %s", p);
806 return 0;
807 }
808
809 uint32_data = htobe32(k);
810 udata = &uint32_data;
811 sz = sizeof(uint32_t);
812
813 break;
814 }
815 case DHCP_OPTION_DATA_IPV4ADDRESS: {
816 r = in_addr_from_string(AF_INET, p, &addr);
817 if (r < 0) {
818 log_syntax(unit, LOG_WARNING, filename, line, r,
819 "Failed to parse DHCP ipv4address data, ignoring assignment: %s", p);
820 return 0;
821 }
822
823 udata = &addr.in;
824 sz = sizeof(addr.in.s_addr);
825 break;
826 }
827 case DHCP_OPTION_DATA_IPV6ADDRESS: {
828 r = in_addr_from_string(AF_INET6, p, &addr);
829 if (r < 0) {
830 log_syntax(unit, LOG_WARNING, filename, line, r,
831 "Failed to parse DHCP ipv6address data, ignoring assignment: %s", p);
832 return 0;
833 }
834
835 udata = &addr.in6;
836 sz = sizeof(addr.in6.s6_addr);
837 break;
838 }
839 case DHCP_OPTION_DATA_STRING:
840 sz = cunescape(p, UNESCAPE_ACCEPT_NUL, &q);
841 if (sz < 0)
842 log_syntax(unit, LOG_WARNING, filename, line, sz,
843 "Failed to decode DHCP option data, ignoring assignment: %s", p);
844
845 udata = q;
846 break;
847 default:
848 return -EINVAL;
849 }
850
851 if (ltype == AF_INET6) {
852 r = sd_dhcp6_option_new(u16, udata, sz, enterprise_identifier, &opt6);
853 if (r < 0) {
854 log_syntax(unit, LOG_WARNING, filename, line, r,
855 "Failed to store DHCP option '%s', ignoring assignment: %m", rvalue);
856 return 0;
857 }
858
859 r = ordered_hashmap_ensure_allocated(options, &dhcp6_option_hash_ops);
860 if (r < 0)
861 return log_oom();
862
863 /* Overwrite existing option */
864 old6 = ordered_hashmap_get(*options, UINT_TO_PTR(u16));
865 r = ordered_hashmap_replace(*options, UINT_TO_PTR(u16), opt6);
866 if (r < 0) {
867 log_syntax(unit, LOG_WARNING, filename, line, r,
868 "Failed to store DHCP option '%s', ignoring assignment: %m", rvalue);
869 return 0;
870 }
871 TAKE_PTR(opt6);
872 } else {
873 r = sd_dhcp_option_new(u8, udata, sz, &opt4);
874 if (r < 0) {
875 log_syntax(unit, LOG_WARNING, filename, line, r,
876 "Failed to store DHCP option '%s', ignoring assignment: %m", rvalue);
877 return 0;
878 }
879
880 r = ordered_hashmap_ensure_allocated(options, &dhcp_option_hash_ops);
881 if (r < 0)
882 return log_oom();
883
884 /* Overwrite existing option */
885 old4 = ordered_hashmap_get(*options, UINT_TO_PTR(u8));
886 r = ordered_hashmap_replace(*options, UINT_TO_PTR(u8), opt4);
887 if (r < 0) {
888 log_syntax(unit, LOG_WARNING, filename, line, r,
889 "Failed to store DHCP option '%s', ignoring assignment: %m", rvalue);
890 return 0;
891 }
892 TAKE_PTR(opt4);
893 }
894 return 0;
895 }
896
897 int config_parse_dhcp_request_options(
898 const char *unit,
899 const char *filename,
900 unsigned line,
901 const char *section,
902 unsigned section_line,
903 const char *lvalue,
904 int ltype,
905 const char *rvalue,
906 void *data,
907 void *userdata) {
908
909 Network *network = userdata;
910 int r;
911
912 assert(filename);
913 assert(lvalue);
914 assert(rvalue);
915 assert(data);
916
917 if (isempty(rvalue)) {
918 if (ltype == AF_INET)
919 network->dhcp_request_options = set_free(network->dhcp_request_options);
920 else
921 network->dhcp6_request_options = set_free(network->dhcp6_request_options);
922
923 return 0;
924 }
925
926 for (const char *p = rvalue;;) {
927 _cleanup_free_ char *n = NULL;
928 uint32_t i;
929
930 r = extract_first_word(&p, &n, NULL, 0);
931 if (r == -ENOMEM)
932 return log_oom();
933 if (r < 0) {
934 log_syntax(unit, LOG_WARNING, filename, line, r,
935 "Failed to parse DHCP request option, ignoring assignment: %s",
936 rvalue);
937 return 0;
938 }
939 if (r == 0)
940 return 0;
941
942 r = safe_atou32(n, &i);
943 if (r < 0) {
944 log_syntax(unit, LOG_WARNING, filename, line, r,
945 "DHCP request option is invalid, ignoring assignment: %s", n);
946 continue;
947 }
948
949 if (i < 1 || i >= UINT8_MAX) {
950 log_syntax(unit, LOG_WARNING, filename, line, 0,
951 "DHCP request option is invalid, valid range is 1-254, ignoring assignment: %s", n);
952 continue;
953 }
954
955 r = set_ensure_put(ltype == AF_INET ? &network->dhcp_request_options : &network->dhcp6_request_options,
956 NULL, UINT32_TO_PTR(i));
957 if (r < 0)
958 log_syntax(unit, LOG_WARNING, filename, line, r,
959 "Failed to store DHCP request option '%s', ignoring assignment: %m", n);
960 }
961 }
962
963 static const char* const dhcp_use_domains_table[_DHCP_USE_DOMAINS_MAX] = {
964 [DHCP_USE_DOMAINS_NO] = "no",
965 [DHCP_USE_DOMAINS_ROUTE] = "route",
966 [DHCP_USE_DOMAINS_YES] = "yes",
967 };
968
969 DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(dhcp_use_domains, DHCPUseDomains, DHCP_USE_DOMAINS_YES);
970
971 static const char * const dhcp_option_data_type_table[_DHCP_OPTION_DATA_MAX] = {
972 [DHCP_OPTION_DATA_UINT8] = "uint8",
973 [DHCP_OPTION_DATA_UINT16] = "uint16",
974 [DHCP_OPTION_DATA_UINT32] = "uint32",
975 [DHCP_OPTION_DATA_STRING] = "string",
976 [DHCP_OPTION_DATA_IPV4ADDRESS] = "ipv4address",
977 [DHCP_OPTION_DATA_IPV6ADDRESS] = "ipv6address",
978 };
979
980 DEFINE_STRING_TABLE_LOOKUP(dhcp_option_data_type, DHCPOptionDataType);
981
982 static const char* const duid_type_table[_DUID_TYPE_MAX] = {
983 [DUID_TYPE_LLT] = "link-layer-time",
984 [DUID_TYPE_EN] = "vendor",
985 [DUID_TYPE_LL] = "link-layer",
986 [DUID_TYPE_UUID] = "uuid",
987 };
988 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(duid_type, DUIDType);
989
990 int config_parse_duid_type(
991 const char *unit,
992 const char *filename,
993 unsigned line,
994 const char *section,
995 unsigned section_line,
996 const char *lvalue,
997 int ltype,
998 const char *rvalue,
999 void *data,
1000 void *userdata) {
1001
1002 _cleanup_free_ char *type_string = NULL;
1003 const char *p = ASSERT_PTR(rvalue);
1004 bool force = ltype;
1005 DUID *duid = ASSERT_PTR(data);
1006 DUIDType type;
1007 int r;
1008
1009 assert(filename);
1010 assert(lvalue);
1011
1012 if (!force && duid->set)
1013 return 0;
1014
1015 r = extract_first_word(&p, &type_string, ":", 0);
1016 if (r == -ENOMEM)
1017 return log_oom();
1018 if (r < 0) {
1019 log_syntax(unit, LOG_WARNING, filename, line, r,
1020 "Invalid syntax, ignoring: %s", rvalue);
1021 return 0;
1022 }
1023 if (r == 0) {
1024 log_syntax(unit, LOG_WARNING, filename, line, 0,
1025 "Failed to extract DUID type from '%s', ignoring.", rvalue);
1026 return 0;
1027 }
1028
1029 type = duid_type_from_string(type_string);
1030 if (type < 0) {
1031 log_syntax(unit, LOG_WARNING, filename, line, type,
1032 "Failed to parse DUID type '%s', ignoring.", type_string);
1033 return 0;
1034 }
1035
1036 if (!isempty(p)) {
1037 usec_t u;
1038
1039 if (type != DUID_TYPE_LLT) {
1040 log_syntax(unit, LOG_WARNING, filename, line, r,
1041 "Invalid syntax, ignoring: %s", rvalue);
1042 return 0;
1043 }
1044
1045 r = parse_timestamp(p, &u);
1046 if (r < 0) {
1047 log_syntax(unit, LOG_WARNING, filename, line, r,
1048 "Failed to parse timestamp, ignoring: %s", p);
1049 return 0;
1050 }
1051
1052 duid->llt_time = u;
1053 }
1054
1055 duid->type = type;
1056 duid->set = force;
1057
1058 return 0;
1059 }
1060
1061 int config_parse_manager_duid_type(
1062 const char *unit,
1063 const char *filename,
1064 unsigned line,
1065 const char *section,
1066 unsigned section_line,
1067 const char *lvalue,
1068 int ltype,
1069 const char *rvalue,
1070 void *data,
1071 void *userdata) {
1072
1073 Manager *manager = ASSERT_PTR(userdata);
1074 int r;
1075
1076 /* For backward compatibility. Setting both DHCPv4 and DHCPv6 DUID if they are not specified explicitly. */
1077
1078 r = config_parse_duid_type(unit, filename, line, section, section_line, lvalue, false, rvalue, &manager->dhcp_duid, manager);
1079 if (r < 0)
1080 return r;
1081
1082 return config_parse_duid_type(unit, filename, line, section, section_line, lvalue, false, rvalue, &manager->dhcp6_duid, manager);
1083 }
1084
1085 int config_parse_network_duid_type(
1086 const char *unit,
1087 const char *filename,
1088 unsigned line,
1089 const char *section,
1090 unsigned section_line,
1091 const char *lvalue,
1092 int ltype,
1093 const char *rvalue,
1094 void *data,
1095 void *userdata) {
1096
1097 Network *network = ASSERT_PTR(userdata);
1098 int r;
1099
1100 r = config_parse_duid_type(unit, filename, line, section, section_line, lvalue, true, rvalue, &network->dhcp_duid, network);
1101 if (r < 0)
1102 return r;
1103
1104 /* For backward compatibility, also set DHCPv6 DUID if not specified explicitly. */
1105 return config_parse_duid_type(unit, filename, line, section, section_line, lvalue, false, rvalue, &network->dhcp6_duid, network);
1106 }
1107
1108 int config_parse_duid_rawdata(
1109 const char *unit,
1110 const char *filename,
1111 unsigned line,
1112 const char *section,
1113 unsigned section_line,
1114 const char *lvalue,
1115 int ltype,
1116 const char *rvalue,
1117 void *data,
1118 void *userdata) {
1119
1120 uint8_t raw_data[MAX_DUID_LEN];
1121 unsigned count = 0;
1122 bool force = ltype;
1123 DUID *duid = ASSERT_PTR(data);
1124
1125 assert(filename);
1126 assert(lvalue);
1127 assert(rvalue);
1128
1129 if (!force && duid->set)
1130 return 0;
1131
1132 /* RawData contains DUID in format "NN:NN:NN..." */
1133 for (const char *p = rvalue;;) {
1134 int n1, n2, len, r;
1135 uint32_t byte;
1136 _cleanup_free_ char *cbyte = NULL;
1137
1138 r = extract_first_word(&p, &cbyte, ":", 0);
1139 if (r == -ENOMEM)
1140 return log_oom();
1141 if (r < 0) {
1142 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to read DUID, ignoring assignment: %s.", rvalue);
1143 return 0;
1144 }
1145 if (r == 0)
1146 break;
1147
1148 if (count >= MAX_DUID_LEN) {
1149 log_syntax(unit, LOG_WARNING, filename, line, 0, "Max DUID length exceeded, ignoring assignment: %s.", rvalue);
1150 return 0;
1151 }
1152
1153 len = strlen(cbyte);
1154 if (!IN_SET(len, 1, 2)) {
1155 log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid length - DUID byte: %s, ignoring assignment: %s.", cbyte, rvalue);
1156 return 0;
1157 }
1158 n1 = unhexchar(cbyte[0]);
1159 if (len == 2)
1160 n2 = unhexchar(cbyte[1]);
1161 else
1162 n2 = 0;
1163
1164 if (n1 < 0 || n2 < 0) {
1165 log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid DUID byte: %s. Ignoring assignment: %s.", cbyte, rvalue);
1166 return 0;
1167 }
1168
1169 byte = ((uint8_t) n1 << (4 * (len-1))) | (uint8_t) n2;
1170 raw_data[count++] = byte;
1171 }
1172
1173 assert_cc(sizeof(raw_data) == sizeof(duid->raw_data));
1174 memcpy(duid->raw_data, raw_data, count);
1175 duid->raw_data_len = count;
1176 duid->set = force;
1177
1178 return 0;
1179 }
1180
1181 int config_parse_manager_duid_rawdata(
1182 const char *unit,
1183 const char *filename,
1184 unsigned line,
1185 const char *section,
1186 unsigned section_line,
1187 const char *lvalue,
1188 int ltype,
1189 const char *rvalue,
1190 void *data,
1191 void *userdata) {
1192
1193 Manager *manager = ASSERT_PTR(userdata);
1194 int r;
1195
1196 /* For backward compatibility. Setting both DHCPv4 and DHCPv6 DUID if they are not specified explicitly. */
1197
1198 r = config_parse_duid_rawdata(unit, filename, line, section, section_line, lvalue, false, rvalue, &manager->dhcp_duid, manager);
1199 if (r < 0)
1200 return r;
1201
1202 return config_parse_duid_rawdata(unit, filename, line, section, section_line, lvalue, false, rvalue, &manager->dhcp6_duid, manager);
1203 }
1204
1205 int config_parse_network_duid_rawdata(
1206 const char *unit,
1207 const char *filename,
1208 unsigned line,
1209 const char *section,
1210 unsigned section_line,
1211 const char *lvalue,
1212 int ltype,
1213 const char *rvalue,
1214 void *data,
1215 void *userdata) {
1216
1217 Network *network = ASSERT_PTR(userdata);
1218 int r;
1219
1220 r = config_parse_duid_rawdata(unit, filename, line, section, section_line, lvalue, true, rvalue, &network->dhcp_duid, network);
1221 if (r < 0)
1222 return r;
1223
1224 /* For backward compatibility, also set DHCPv6 DUID if not specified explicitly. */
1225 return config_parse_duid_rawdata(unit, filename, line, section, section_line, lvalue, false, rvalue, &network->dhcp6_duid, network);
1226 }
1227
1228 int config_parse_uplink(
1229 const char *unit,
1230 const char *filename,
1231 unsigned line,
1232 const char *section,
1233 unsigned section_line,
1234 const char *lvalue,
1235 int ltype,
1236 const char *rvalue,
1237 void *data,
1238 void *userdata) {
1239
1240 Network *network = ASSERT_PTR(userdata);
1241 bool accept_none = true;
1242 int *index, r;
1243 char **name;
1244
1245 assert(filename);
1246 assert(section);
1247 assert(lvalue);
1248 assert(rvalue);
1249
1250 if (streq(section, "DHCPServer")) {
1251 index = &network->dhcp_server_uplink_index;
1252 name = &network->dhcp_server_uplink_name;
1253 } else if (streq(section, "IPv6SendRA")) {
1254 index = &network->router_uplink_index;
1255 name = &network->router_uplink_name;
1256 } else if (STR_IN_SET(section, "DHCPv6PrefixDelegation", "DHCPPrefixDelegation")) {
1257 index = &network->dhcp_pd_uplink_index;
1258 name = &network->dhcp_pd_uplink_name;
1259 accept_none = false;
1260 } else
1261 assert_not_reached();
1262
1263 if (isempty(rvalue) || streq(rvalue, ":auto")) {
1264 *index = UPLINK_INDEX_AUTO;
1265 *name = mfree(*name);
1266 return 0;
1267 }
1268
1269 if (accept_none && streq(rvalue, ":none")) {
1270 *index = UPLINK_INDEX_NONE;
1271 *name = mfree(*name);
1272 return 0;
1273 }
1274
1275 if (!accept_none && streq(rvalue, ":self")) {
1276 *index = UPLINK_INDEX_SELF;
1277 *name = mfree(*name);
1278 return 0;
1279 }
1280
1281 r = parse_ifindex(rvalue);
1282 if (r > 0) {
1283 *index = r;
1284 *name = mfree(*name);
1285 return 0;
1286 }
1287
1288 if (!ifname_valid_full(rvalue, IFNAME_VALID_ALTERNATIVE)) {
1289 log_syntax(unit, LOG_WARNING, filename, line, 0,
1290 "Invalid interface name in %s=, ignoring assignment: %s", lvalue, rvalue);
1291 return 0;
1292 }
1293
1294 /* The interface name will be resolved later. */
1295 r = free_and_strdup_warn(name, rvalue);
1296 if (r < 0)
1297 return r;
1298
1299 /* Note, if uplink_name is set, then uplink_index will be ignored. So, the below does not mean
1300 * an uplink interface will be selected automatically. */
1301 *index = UPLINK_INDEX_AUTO;
1302 return 0;
1303 }