1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 #include <netinet/in.h>
4 #include <linux/if_arp.h>
7 #include "bus-locator.h"
8 #include "dhcp-option.h"
9 #include "dhcp6-internal.h"
11 #include "hexdecoct.h"
12 #include "in-addr-prefix-util.h"
13 #include "networkd-dhcp-common.h"
14 #include "networkd-link.h"
15 #include "networkd-manager.h"
16 #include "networkd-network.h"
17 #include "networkd-route-util.h"
18 #include "parse-util.h"
19 #include "socket-util.h"
20 #include "string-table.h"
24 static uint32_t link_get_vrf_table(Link
*link
) {
26 assert(link
->network
);
28 return link
->network
->vrf
? VRF(link
->network
->vrf
)->table
: RT_TABLE_MAIN
;
31 uint32_t link_get_dhcp4_route_table(Link
*link
) {
33 assert(link
->network
);
35 /* When the interface is part of an VRF use the VRFs routing table, unless
36 * another table is explicitly specified. */
38 if (link
->network
->dhcp_route_table_set
)
39 return link
->network
->dhcp_route_table
;
40 return link_get_vrf_table(link
);
43 uint32_t link_get_ndisc_route_table(Link
*link
) {
45 assert(link
->network
);
47 if (link
->network
->ndisc_route_table_set
)
48 return link
->network
->ndisc_route_table
;
49 return link_get_vrf_table(link
);
52 bool link_dhcp_enabled(Link
*link
, int family
) {
54 assert(IN_SET(family
, AF_INET
, AF_INET6
));
56 /* Currently, sd-dhcp-client supports only ethernet and infiniband. */
57 if (family
== AF_INET
&& !IN_SET(link
->iftype
, ARPHRD_ETHER
, ARPHRD_INFINIBAND
))
60 if (family
== AF_INET6
&& !socket_ipv6_is_supported())
63 if (link
->flags
& IFF_LOOPBACK
)
66 if (link
->iftype
== ARPHRD_CAN
)
72 return link
->network
->dhcp
& (family
== AF_INET
? ADDRESS_FAMILY_IPV4
: ADDRESS_FAMILY_IPV6
);
75 void network_adjust_dhcp(Network
*network
) {
77 assert(network
->dhcp
>= 0);
79 if (network
->dhcp
== ADDRESS_FAMILY_NO
)
82 /* Bonding slave does not support addressing. */
84 log_warning("%s: Cannot enable DHCP= when Bond= is specified, disabling DHCP=.",
86 network
->dhcp
= ADDRESS_FAMILY_NO
;
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);
97 network_adjust_dhcp4(network
);
100 static bool duid_needs_product_uuid(const DUID
*duid
) {
103 return duid
->type
== DUID_TYPE_UUID
&& duid
->raw_data_len
== 0;
106 static const struct DUID fallback_duid
= { .type
= DUID_TYPE_EN
};
108 const DUID
*link_get_duid(Link
*link
, int family
) {
112 assert(IN_SET(family
, AF_INET
, AF_INET6
));
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
;
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
;
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
;
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;
146 e
= sd_bus_message_get_error(m
);
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
));
154 r
= sd_bus_message_read_array(m
, 'y', &a
, &sz
);
156 log_warning_errno(r
, "Failed to get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %m");
160 if (sz
!= sizeof(sd_id128_t
)) {
161 log_warning("Invalid product UUID. Falling back to use machine-app-specific ID as DUID-UUID.");
165 log_debug("Successfully obtained product UUID");
167 memcpy(&manager
->duid_product_uuid
.raw_data
, a
, sz
);
168 manager
->duid_product_uuid
.raw_data_len
= sz
;
173 int manager_request_product_uuid(Manager
*m
) {
174 static bool bus_method_is_called
= false;
179 if (bus_method_is_called
)
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;
188 m
->product_uuid_requested
= false;
190 r
= bus_call_method_async(
195 get_product_uuid_handler
,
200 return log_warning_errno(r
, "Failed to get product UUID: %m");
202 log_debug("Requesting product UUID.");
204 bus_method_is_called
= true;
209 int dhcp_configure_duid(Link
*link
, const DUID
*duid
) {
214 assert(link
->manager
);
219 if (!duid_needs_product_uuid(duid
))
222 if (m
->has_product_uuid
)
225 r
= manager_request_product_uuid(m
);
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");
230 m
->has_product_uuid
= true; /* Do not request UUID again on failure. */
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
;
240 assert(IN_SET(family
, AF_INET
, AF_INET6
));
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)
253 SET_FOREACH(p
, deny_list
)
254 if (p
->family
== family
&&
255 in_addr_prefix_intersect(family
, &p
->address
, p
->prefixlen
, address
, prefixlen
) > 0)
261 int link_get_captive_portal(Link
*link
, const char **ret
) {
262 const char *dhcp4_cp
= NULL
, *dhcp6_cp
= NULL
, *ndisc_cp
= NULL
;
267 if (!link
->network
) {
272 if (link
->network
->dhcp_use_captive_portal
&& link
->dhcp_lease
) {
273 r
= sd_dhcp_lease_get_captive_portal(link
->dhcp_lease
, &dhcp4_cp
);
274 if (r
< 0 && r
!= -ENODATA
)
278 if (link
->network
->dhcp6_use_captive_portal
&& link
->dhcp6_lease
) {
279 r
= sd_dhcp6_lease_get_captive_portal(link
->dhcp6_lease
, &dhcp6_cp
);
280 if (r
< 0 && r
!= -ENODATA
)
284 if (link
->network
->ndisc_use_captive_portal
) {
285 NDiscCaptivePortal
*cp
;
288 /* Use the captive portal with the longest lifetime. */
290 SET_FOREACH(cp
, link
->ndisc_captive_portals
) {
291 if (cp
->lifetime_usec
< usec
)
294 ndisc_cp
= cp
->captive_portal
;
295 usec
= cp
->lifetime_usec
;
298 if (set_size(link
->ndisc_captive_portals
) > 1)
299 log_link_debug(link
, "Multiple captive portals obtained by IPv6RA, using \"%s\" and ignoring others.",
304 if (dhcp6_cp
&& !streq(dhcp4_cp
, dhcp6_cp
))
305 log_link_debug(link
, "DHCPv6 captive portal (%s) does not match DHCPv4 (%s), ignoring DHCPv6 captive portal.",
308 if (ndisc_cp
&& !streq(dhcp4_cp
, ndisc_cp
))
309 log_link_debug(link
, "IPv6RA captive portal (%s) does not match DHCPv4 (%s), ignoring IPv6RA captive portal.",
317 if (ndisc_cp
&& !streq(dhcp6_cp
, ndisc_cp
))
318 log_link_debug(link
, "IPv6RA captive portal (%s) does not match DHCPv6 (%s), ignoring IPv6RA captive portal.",
329 int config_parse_dhcp(
331 const char *filename
,
334 unsigned section_line
,
341 AddressFamily
*dhcp
= data
, s
;
348 /* Note that this is mostly like
349 * config_parse_address_family(), except that it
350 * understands some old names for the enum values */
352 s
= address_family_from_string(rvalue
);
355 /* Previously, we had a slightly different enum here,
356 * support its values for compatibility. */
358 s
= dhcp_deprecated_address_family_from_string(rvalue
);
360 log_syntax(unit
, LOG_WARNING
, filename
, line
, s
,
361 "Failed to parse DHCP option, ignoring: %s", rvalue
);
365 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
366 "DHCP=%s is deprecated, please use DHCP=%s instead.",
367 rvalue
, address_family_to_string(s
));
374 int config_parse_dhcp_route_metric(
376 const char *filename
,
379 unsigned section_line
,
386 Network
*network
= userdata
;
392 assert(IN_SET(ltype
, AF_UNSPEC
, AF_INET
));
396 r
= safe_atou32(rvalue
, &metric
);
398 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
399 "Failed to parse RouteMetric=%s, ignoring assignment: %m", rvalue
);
405 network
->dhcp_route_metric
= metric
;
406 network
->dhcp_route_metric_set
= true;
409 /* For backward compatibility. */
410 if (!network
->dhcp_route_metric_set
)
411 network
->dhcp_route_metric
= metric
;
412 if (!network
->ndisc_route_metric_set
) {
413 network
->ndisc_route_metric_high
= metric
;
414 network
->ndisc_route_metric_medium
= metric
;
415 network
->ndisc_route_metric_low
= metric
;
419 assert_not_reached();
425 int config_parse_ndisc_route_metric(
427 const char *filename
,
430 unsigned section_line
,
437 Network
*network
= ASSERT_PTR(userdata
);
438 uint32_t metric_high
, metric_medium
, metric_low
;
444 if (safe_atou32(rvalue
, &metric_low
) >= 0)
445 metric_high
= metric_medium
= metric_low
;
447 _cleanup_free_
char *high
= NULL
, *medium
= NULL
, *low
= NULL
;
448 const char *p
= rvalue
;
450 r
= extract_many_words(&p
, ":", EXTRACT_DONT_COALESCE_SEPARATORS
, &high
, &medium
, &low
);
453 if (r
!= 3 || !isempty(p
)) {
454 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
< 0 ? r
: 0,
455 "Failed to parse RouteTable=%s, ignoring assignment: %m", rvalue
);
459 r
= safe_atou32(high
, &metric_high
);
460 s
= safe_atou32(medium
, &metric_medium
);
461 t
= safe_atou32(low
, &metric_low
);
462 if (r
< 0 || s
< 0 || t
< 0) {
463 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
< 0 ? r
: s
< 0 ? s
: t
,
464 "Failed to parse RouteTable=%s, ignoring assignment: %m", rvalue
);
468 if (metric_high
>= metric_medium
|| metric_medium
>= metric_low
) {
469 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
470 "Invalid RouteTable=%s, ignoring assignment: %m", rvalue
);
475 network
->ndisc_route_metric_high
= metric_high
;
476 network
->ndisc_route_metric_medium
= metric_medium
;
477 network
->ndisc_route_metric_low
= metric_low
;
478 network
->ndisc_route_metric_set
= true;
483 int config_parse_dhcp_send_hostname(
485 const char *filename
,
488 unsigned section_line
,
495 Network
*network
= userdata
;
500 assert(IN_SET(ltype
, AF_UNSPEC
, AF_INET
, AF_INET6
));
504 r
= parse_boolean(rvalue
);
506 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
507 "Failed to parse SendHostname=%s, ignoring assignment: %m", rvalue
);
513 network
->dhcp_send_hostname
= r
;
514 network
->dhcp_send_hostname_set
= true;
517 network
->dhcp6_send_hostname
= r
;
518 network
->dhcp6_send_hostname_set
= true;
521 /* For backward compatibility. */
522 if (!network
->dhcp_send_hostname_set
)
523 network
->dhcp_send_hostname
= r
;
524 if (!network
->dhcp6_send_hostname_set
)
525 network
->dhcp6_send_hostname
= r
;
528 assert_not_reached();
534 int config_parse_dhcp_or_ra_route_table(
536 const char *filename
,
539 unsigned section_line
,
546 Network
*network
= ASSERT_PTR(userdata
);
552 assert(IN_SET(ltype
, AF_INET
, AF_INET6
));
555 r
= manager_get_route_table_from_string(network
->manager
, rvalue
, &rt
);
557 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
558 "Failed to parse RouteTable=%s, ignoring assignment: %m", rvalue
);
564 network
->dhcp_route_table
= rt
;
565 network
->dhcp_route_table_set
= true;
568 network
->ndisc_route_table
= rt
;
569 network
->ndisc_route_table_set
= true;
572 assert_not_reached();
578 int config_parse_iaid(
580 const char *filename
,
583 unsigned section_line
,
590 Network
*network
= ASSERT_PTR(userdata
);
597 assert(IN_SET(ltype
, AF_INET
, AF_INET6
));
599 r
= safe_atou32(rvalue
, &iaid
);
601 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
602 "Unable to read IAID, ignoring assignment: %s", rvalue
);
606 if (ltype
== AF_INET
) {
607 network
->dhcp_iaid
= iaid
;
608 network
->dhcp_iaid_set
= true;
609 if (!network
->dhcp6_iaid_set_explicitly
) {
610 /* Backward compatibility. Previously, IAID is shared by DHCPv4 and DHCPv6.
611 * If DHCPv6 IAID is not specified explicitly, then use DHCPv4 IAID for DHCPv6. */
612 network
->dhcp6_iaid
= iaid
;
613 network
->dhcp6_iaid_set
= true;
616 assert(ltype
== AF_INET6
);
617 network
->dhcp6_iaid
= iaid
;
618 network
->dhcp6_iaid_set
= true;
619 network
->dhcp6_iaid_set_explicitly
= true;
625 int config_parse_dhcp_user_or_vendor_class(
627 const char *filename
,
630 unsigned section_line
,
637 char ***l
= ASSERT_PTR(data
);
642 assert(IN_SET(ltype
, AF_INET
, AF_INET6
));
644 if (isempty(rvalue
)) {
649 for (const char *p
= rvalue
;;) {
650 _cleanup_free_
char *w
= NULL
;
653 r
= extract_first_word(&p
, &w
, NULL
, EXTRACT_CUNESCAPE
|EXTRACT_UNQUOTE
);
657 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
658 "Failed to split user classes option, ignoring: %s", rvalue
);
665 if (ltype
== AF_INET
) {
666 if (len
> UINT8_MAX
|| len
== 0) {
667 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
668 "%s length is not in the range 1…255, ignoring.", w
);
672 if (len
> UINT16_MAX
|| len
== 0) {
673 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
674 "%s length is not in the range 1…65535, ignoring.", w
);
679 r
= strv_consume(l
, TAKE_PTR(w
));
685 int config_parse_dhcp_send_option(
687 const char *filename
,
690 unsigned section_line
,
697 _cleanup_(sd_dhcp_option_unrefp
) sd_dhcp_option
*opt4
= NULL
;
698 _cleanup_(sd_dhcp6_option_unrefp
) sd_dhcp6_option
*opt6
= NULL
;
699 _unused_
_cleanup_(sd_dhcp_option_unrefp
) sd_dhcp_option
*old4
= NULL
;
700 _unused_
_cleanup_(sd_dhcp6_option_unrefp
) sd_dhcp6_option
*old6
= NULL
;
701 uint32_t uint32_data
, enterprise_identifier
= 0;
702 _cleanup_free_
char *word
= NULL
, *q
= NULL
;
703 OrderedHashmap
**options
= ASSERT_PTR(data
);
704 uint16_t u16
, uint16_data
;
705 union in_addr_union addr
;
706 DHCPOptionDataType type
;
707 uint8_t u8
, uint8_data
;
717 if (isempty(rvalue
)) {
718 *options
= ordered_hashmap_free(*options
);
723 if (ltype
== AF_INET6
&& streq(lvalue
, "SendVendorOption")) {
724 r
= extract_first_word(&p
, &word
, ":", 0);
727 if (r
<= 0 || isempty(p
)) {
728 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
729 "Invalid DHCP option, ignoring assignment: %s", rvalue
);
733 r
= safe_atou32(word
, &enterprise_identifier
);
735 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
736 "Failed to parse DHCPv6 enterprise identifier data, ignoring assignment: %s", p
);
742 r
= extract_first_word(&p
, &word
, ":", 0);
745 if (r
<= 0 || isempty(p
)) {
746 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
747 "Invalid DHCP option, ignoring assignment: %s", rvalue
);
751 if (ltype
== AF_INET6
) {
752 r
= safe_atou16(word
, &u16
);
754 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
755 "Invalid DHCP option, ignoring assignment: %s", rvalue
);
758 if (u16
< 1 || u16
>= UINT16_MAX
) {
759 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
760 "Invalid DHCP option, valid range is 1-65535, ignoring assignment: %s", rvalue
);
764 r
= safe_atou8(word
, &u8
);
766 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
767 "Invalid DHCP option, ignoring assignment: %s", rvalue
);
770 if (u8
< 1 || u8
>= UINT8_MAX
) {
771 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
772 "Invalid DHCP option, valid range is 1-254, ignoring assignment: %s", rvalue
);
778 r
= extract_first_word(&p
, &word
, ":", 0);
781 if (r
<= 0 || isempty(p
)) {
782 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
783 "Invalid DHCP option, ignoring assignment: %s", rvalue
);
787 type
= dhcp_option_data_type_from_string(word
);
789 log_syntax(unit
, LOG_WARNING
, filename
, line
, type
,
790 "Invalid DHCP option data type, ignoring assignment: %s", p
);
795 case DHCP_OPTION_DATA_UINT8
:{
796 r
= safe_atou8(p
, &uint8_data
);
798 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
799 "Failed to parse DHCP uint8 data, ignoring assignment: %s", p
);
804 sz
= sizeof(uint8_t);
807 case DHCP_OPTION_DATA_UINT16
:{
810 r
= safe_atou16(p
, &k
);
812 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
813 "Failed to parse DHCP uint16 data, ignoring assignment: %s", p
);
817 uint16_data
= htobe16(k
);
818 udata
= &uint16_data
;
819 sz
= sizeof(uint16_t);
822 case DHCP_OPTION_DATA_UINT32
: {
825 r
= safe_atou32(p
, &k
);
827 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
828 "Failed to parse DHCP uint32 data, ignoring assignment: %s", p
);
832 uint32_data
= htobe32(k
);
833 udata
= &uint32_data
;
834 sz
= sizeof(uint32_t);
838 case DHCP_OPTION_DATA_IPV4ADDRESS
: {
839 r
= in_addr_from_string(AF_INET
, p
, &addr
);
841 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
842 "Failed to parse DHCP ipv4address data, ignoring assignment: %s", p
);
847 sz
= sizeof(addr
.in
.s_addr
);
850 case DHCP_OPTION_DATA_IPV6ADDRESS
: {
851 r
= in_addr_from_string(AF_INET6
, p
, &addr
);
853 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
854 "Failed to parse DHCP ipv6address data, ignoring assignment: %s", p
);
859 sz
= sizeof(addr
.in6
.s6_addr
);
862 case DHCP_OPTION_DATA_STRING
:
863 sz
= cunescape(p
, UNESCAPE_ACCEPT_NUL
, &q
);
865 log_syntax(unit
, LOG_WARNING
, filename
, line
, sz
,
866 "Failed to decode DHCP option data, ignoring assignment: %s", p
);
876 if (ltype
== AF_INET6
) {
877 r
= sd_dhcp6_option_new(u16
, udata
, sz
, enterprise_identifier
, &opt6
);
879 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
880 "Failed to store DHCP option '%s', ignoring assignment: %m", rvalue
);
884 r
= ordered_hashmap_ensure_allocated(options
, &dhcp6_option_hash_ops
);
888 /* Overwrite existing option */
889 old6
= ordered_hashmap_get(*options
, UINT_TO_PTR(u16
));
890 r
= ordered_hashmap_replace(*options
, UINT_TO_PTR(u16
), opt6
);
892 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
893 "Failed to store DHCP option '%s', ignoring assignment: %m", rvalue
);
898 r
= sd_dhcp_option_new(u8
, udata
, sz
, &opt4
);
900 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
901 "Failed to store DHCP option '%s', ignoring assignment: %m", rvalue
);
905 r
= ordered_hashmap_ensure_allocated(options
, &dhcp_option_hash_ops
);
909 /* Overwrite existing option */
910 old4
= ordered_hashmap_get(*options
, UINT_TO_PTR(u8
));
911 r
= ordered_hashmap_replace(*options
, UINT_TO_PTR(u8
), opt4
);
913 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
914 "Failed to store DHCP option '%s', ignoring assignment: %m", rvalue
);
922 int config_parse_dhcp_request_options(
924 const char *filename
,
927 unsigned section_line
,
934 Network
*network
= userdata
;
942 if (isempty(rvalue
)) {
943 if (ltype
== AF_INET
)
944 network
->dhcp_request_options
= set_free(network
->dhcp_request_options
);
946 network
->dhcp6_request_options
= set_free(network
->dhcp6_request_options
);
951 for (const char *p
= rvalue
;;) {
952 _cleanup_free_
char *n
= NULL
;
955 r
= extract_first_word(&p
, &n
, NULL
, 0);
959 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
960 "Failed to parse DHCP request option, ignoring assignment: %s",
967 r
= safe_atou32(n
, &i
);
969 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
970 "DHCP request option is invalid, ignoring assignment: %s", n
);
974 if (i
< 1 || i
>= UINT8_MAX
) {
975 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
976 "DHCP request option is invalid, valid range is 1-254, ignoring assignment: %s", n
);
980 r
= set_ensure_put(ltype
== AF_INET
? &network
->dhcp_request_options
: &network
->dhcp6_request_options
,
981 NULL
, UINT32_TO_PTR(i
));
983 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
984 "Failed to store DHCP request option '%s', ignoring assignment: %m", n
);
988 static const char * const dhcp_option_data_type_table
[_DHCP_OPTION_DATA_MAX
] = {
989 [DHCP_OPTION_DATA_UINT8
] = "uint8",
990 [DHCP_OPTION_DATA_UINT16
] = "uint16",
991 [DHCP_OPTION_DATA_UINT32
] = "uint32",
992 [DHCP_OPTION_DATA_STRING
] = "string",
993 [DHCP_OPTION_DATA_IPV4ADDRESS
] = "ipv4address",
994 [DHCP_OPTION_DATA_IPV6ADDRESS
] = "ipv6address",
997 DEFINE_STRING_TABLE_LOOKUP(dhcp_option_data_type
, DHCPOptionDataType
);
999 static const char* const duid_type_table
[_DUID_TYPE_MAX
] = {
1000 [DUID_TYPE_LLT
] = "link-layer-time",
1001 [DUID_TYPE_EN
] = "vendor",
1002 [DUID_TYPE_LL
] = "link-layer",
1003 [DUID_TYPE_UUID
] = "uuid",
1005 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(duid_type
, DUIDType
);
1007 int config_parse_duid_type(
1009 const char *filename
,
1011 const char *section
,
1012 unsigned section_line
,
1019 _cleanup_free_
char *type_string
= NULL
;
1020 const char *p
= ASSERT_PTR(rvalue
);
1022 DUID
*duid
= ASSERT_PTR(data
);
1029 if (!force
&& duid
->set
)
1032 r
= extract_first_word(&p
, &type_string
, ":", 0);
1036 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1037 "Invalid syntax, ignoring: %s", rvalue
);
1041 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1042 "Failed to extract DUID type from '%s', ignoring.", rvalue
);
1046 type
= duid_type_from_string(type_string
);
1050 r
= safe_atou16(type_string
, &t
);
1052 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1053 "Failed to parse DUID type '%s', ignoring.", type_string
);
1058 assert(type
== t
); /* Check if type can store uint16_t. */
1064 if (type
!= DUID_TYPE_LLT
) {
1065 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1066 "Invalid syntax, ignoring: %s", rvalue
);
1070 r
= parse_timestamp(p
, &u
);
1072 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1073 "Failed to parse timestamp, ignoring: %s", p
);
1086 int config_parse_manager_duid_type(
1088 const char *filename
,
1090 const char *section
,
1091 unsigned section_line
,
1098 Manager
*manager
= ASSERT_PTR(userdata
);
1101 /* For backward compatibility. Setting both DHCPv4 and DHCPv6 DUID if they are not specified explicitly. */
1103 r
= config_parse_duid_type(unit
, filename
, line
, section
, section_line
, lvalue
, false, rvalue
, &manager
->dhcp_duid
, manager
);
1107 return config_parse_duid_type(unit
, filename
, line
, section
, section_line
, lvalue
, false, rvalue
, &manager
->dhcp6_duid
, manager
);
1110 int config_parse_network_duid_type(
1112 const char *filename
,
1114 const char *section
,
1115 unsigned section_line
,
1122 Network
*network
= ASSERT_PTR(userdata
);
1125 r
= config_parse_duid_type(unit
, filename
, line
, section
, section_line
, lvalue
, true, rvalue
, &network
->dhcp_duid
, network
);
1129 /* For backward compatibility, also set DHCPv6 DUID if not specified explicitly. */
1130 return config_parse_duid_type(unit
, filename
, line
, section
, section_line
, lvalue
, false, rvalue
, &network
->dhcp6_duid
, network
);
1133 int config_parse_duid_rawdata(
1135 const char *filename
,
1137 const char *section
,
1138 unsigned section_line
,
1145 uint8_t raw_data
[MAX_DUID_DATA_LEN
];
1148 DUID
*duid
= ASSERT_PTR(data
);
1154 if (!force
&& duid
->set
)
1157 /* RawData contains DUID in format "NN:NN:NN..." */
1158 for (const char *p
= rvalue
;;) {
1161 _cleanup_free_
char *cbyte
= NULL
;
1163 r
= extract_first_word(&p
, &cbyte
, ":", 0);
1167 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Failed to read DUID, ignoring assignment: %s.", rvalue
);
1173 if (count
>= MAX_DUID_DATA_LEN
) {
1174 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "Max DUID length exceeded, ignoring assignment: %s.", rvalue
);
1178 len
= strlen(cbyte
);
1179 if (!IN_SET(len
, 1, 2)) {
1180 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "Invalid length - DUID byte: %s, ignoring assignment: %s.", cbyte
, rvalue
);
1183 n1
= unhexchar(cbyte
[0]);
1185 n2
= unhexchar(cbyte
[1]);
1189 if (n1
< 0 || n2
< 0) {
1190 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "Invalid DUID byte: %s. Ignoring assignment: %s.", cbyte
, rvalue
);
1194 byte
= ((uint8_t) n1
<< (4 * (len
-1))) | (uint8_t) n2
;
1195 raw_data
[count
++] = byte
;
1198 assert_cc(sizeof(raw_data
) == sizeof(duid
->raw_data
));
1199 memcpy(duid
->raw_data
, raw_data
, count
);
1200 duid
->raw_data_len
= count
;
1206 int config_parse_manager_duid_rawdata(
1208 const char *filename
,
1210 const char *section
,
1211 unsigned section_line
,
1218 Manager
*manager
= ASSERT_PTR(userdata
);
1221 /* For backward compatibility. Setting both DHCPv4 and DHCPv6 DUID if they are not specified explicitly. */
1223 r
= config_parse_duid_rawdata(unit
, filename
, line
, section
, section_line
, lvalue
, false, rvalue
, &manager
->dhcp_duid
, manager
);
1227 return config_parse_duid_rawdata(unit
, filename
, line
, section
, section_line
, lvalue
, false, rvalue
, &manager
->dhcp6_duid
, manager
);
1230 int config_parse_network_duid_rawdata(
1232 const char *filename
,
1234 const char *section
,
1235 unsigned section_line
,
1242 Network
*network
= ASSERT_PTR(userdata
);
1245 r
= config_parse_duid_rawdata(unit
, filename
, line
, section
, section_line
, lvalue
, true, rvalue
, &network
->dhcp_duid
, network
);
1249 /* For backward compatibility, also set DHCPv6 DUID if not specified explicitly. */
1250 return config_parse_duid_rawdata(unit
, filename
, line
, section
, section_line
, lvalue
, false, rvalue
, &network
->dhcp6_duid
, network
);
1253 int config_parse_uplink(
1255 const char *filename
,
1257 const char *section
,
1258 unsigned section_line
,
1265 Network
*network
= ASSERT_PTR(userdata
);
1266 bool accept_none
= true;
1275 if (streq(section
, "DHCPServer")) {
1276 index
= &network
->dhcp_server_uplink_index
;
1277 name
= &network
->dhcp_server_uplink_name
;
1278 } else if (streq(section
, "IPv6SendRA")) {
1279 index
= &network
->router_uplink_index
;
1280 name
= &network
->router_uplink_name
;
1281 } else if (STR_IN_SET(section
, "DHCPv6PrefixDelegation", "DHCPPrefixDelegation")) {
1282 index
= &network
->dhcp_pd_uplink_index
;
1283 name
= &network
->dhcp_pd_uplink_name
;
1284 accept_none
= false;
1286 assert_not_reached();
1288 if (isempty(rvalue
) || streq(rvalue
, ":auto")) {
1289 *index
= UPLINK_INDEX_AUTO
;
1290 *name
= mfree(*name
);
1294 if (accept_none
&& streq(rvalue
, ":none")) {
1295 *index
= UPLINK_INDEX_NONE
;
1296 *name
= mfree(*name
);
1300 if (!accept_none
&& streq(rvalue
, ":self")) {
1301 *index
= UPLINK_INDEX_SELF
;
1302 *name
= mfree(*name
);
1306 r
= parse_ifindex(rvalue
);
1309 *name
= mfree(*name
);
1313 if (!ifname_valid_full(rvalue
, IFNAME_VALID_ALTERNATIVE
)) {
1314 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1315 "Invalid interface name in %s=, ignoring assignment: %s", lvalue
, rvalue
);
1319 /* The interface name will be resolved later. */
1320 r
= free_and_strdup_warn(name
, rvalue
);
1324 /* Note, if uplink_name is set, then uplink_index will be ignored. So, the below does not mean
1325 * an uplink interface will be selected automatically. */
1326 *index
= UPLINK_INDEX_AUTO
;