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-identifier.h"
9 #include "dhcp-internal.h"
10 #include "dhcp6-internal.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"
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_ipv6_accept_ra_route_table(Link
*link
) {
45 assert(link
->network
);
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
);
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 config_parse_dhcp(
263 const char *filename
,
266 unsigned section_line
,
273 AddressFamily
*dhcp
= data
, s
;
280 /* Note that this is mostly like
281 * config_parse_address_family(), except that it
282 * understands some old names for the enum values */
284 s
= address_family_from_string(rvalue
);
287 /* Previously, we had a slightly different enum here,
288 * support its values for compatibility. */
290 s
= dhcp_deprecated_address_family_from_string(rvalue
);
292 log_syntax(unit
, LOG_WARNING
, filename
, line
, s
,
293 "Failed to parse DHCP option, ignoring: %s", rvalue
);
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
));
306 int config_parse_dhcp_or_ra_route_metric(
308 const char *filename
,
311 unsigned section_line
,
318 Network
*network
= userdata
;
324 assert(IN_SET(ltype
, AF_UNSPEC
, AF_INET
, AF_INET6
));
328 r
= safe_atou32(rvalue
, &metric
);
330 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
331 "Failed to parse RouteMetric=%s, ignoring assignment: %m", rvalue
);
337 network
->dhcp_route_metric
= metric
;
338 network
->dhcp_route_metric_set
= true;
341 network
->ipv6_accept_ra_route_metric
= metric
;
342 network
->ipv6_accept_ra_route_metric_set
= true;
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
;
352 assert_not_reached();
358 int config_parse_dhcp_use_dns(
360 const char *filename
,
363 unsigned section_line
,
370 Network
*network
= userdata
;
375 assert(IN_SET(ltype
, AF_UNSPEC
, AF_INET
, AF_INET6
));
379 r
= parse_boolean(rvalue
);
381 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
382 "Failed to parse UseDNS=%s, ignoring assignment: %m", rvalue
);
388 network
->dhcp_use_dns
= r
;
389 network
->dhcp_use_dns_set
= true;
392 network
->dhcp6_use_dns
= r
;
393 network
->dhcp6_use_dns_set
= true;
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
;
403 assert_not_reached();
409 int config_parse_dhcp_use_domains(
411 const char *filename
,
414 unsigned section_line
,
421 Network
*network
= userdata
;
426 assert(IN_SET(ltype
, AF_UNSPEC
, AF_INET
, AF_INET6
));
430 d
= dhcp_use_domains_from_string(rvalue
);
432 log_syntax(unit
, LOG_WARNING
, filename
, line
, d
,
433 "Failed to parse %s=%s, ignoring assignment: %m", lvalue
, rvalue
);
439 network
->dhcp_use_domains
= d
;
440 network
->dhcp_use_domains_set
= true;
443 network
->dhcp6_use_domains
= d
;
444 network
->dhcp6_use_domains_set
= true;
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
;
454 assert_not_reached();
460 int config_parse_dhcp_use_ntp(
462 const char *filename
,
465 unsigned section_line
,
472 Network
*network
= userdata
;
477 assert(IN_SET(ltype
, AF_UNSPEC
, AF_INET
, AF_INET6
));
481 r
= parse_boolean(rvalue
);
483 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
484 "Failed to parse UseNTP=%s, ignoring assignment: %m", rvalue
);
490 network
->dhcp_use_ntp
= r
;
491 network
->dhcp_use_ntp_set
= true;
494 network
->dhcp6_use_ntp
= r
;
495 network
->dhcp6_use_ntp_set
= true;
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
;
505 assert_not_reached();
511 int config_parse_dhcp_or_ra_route_table(
513 const char *filename
,
516 unsigned section_line
,
523 Network
*network
= ASSERT_PTR(userdata
);
529 assert(IN_SET(ltype
, AF_INET
, AF_INET6
));
532 r
= safe_atou32(rvalue
, &rt
);
534 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
535 "Failed to parse RouteTable=%s, ignoring assignment: %m", rvalue
);
541 network
->dhcp_route_table
= rt
;
542 network
->dhcp_route_table_set
= true;
545 network
->ipv6_accept_ra_route_table
= rt
;
546 network
->ipv6_accept_ra_route_table_set
= true;
549 assert_not_reached();
555 int config_parse_iaid(
557 const char *filename
,
560 unsigned section_line
,
567 Network
*network
= ASSERT_PTR(userdata
);
574 assert(IN_SET(ltype
, AF_INET
, AF_INET6
));
576 r
= safe_atou32(rvalue
, &iaid
);
578 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
579 "Unable to read IAID, ignoring assignment: %s", rvalue
);
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;
593 assert(ltype
== AF_INET6
);
594 network
->dhcp6_iaid
= iaid
;
595 network
->dhcp6_iaid_set
= true;
596 network
->dhcp6_iaid_set_explicitly
= true;
602 int config_parse_dhcp_user_or_vendor_class(
604 const char *filename
,
607 unsigned section_line
,
614 char ***l
= ASSERT_PTR(data
);
619 assert(IN_SET(ltype
, AF_INET
, AF_INET6
));
621 if (isempty(rvalue
)) {
626 for (const char *p
= rvalue
;;) {
627 _cleanup_free_
char *w
= NULL
;
630 r
= extract_first_word(&p
, &w
, NULL
, EXTRACT_CUNESCAPE
|EXTRACT_UNQUOTE
);
634 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
635 "Failed to split user classes option, ignoring: %s", rvalue
);
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
);
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
);
656 r
= strv_consume(l
, TAKE_PTR(w
));
662 int config_parse_dhcp_send_option(
664 const char *filename
,
667 unsigned section_line
,
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
;
694 if (isempty(rvalue
)) {
695 *options
= ordered_hashmap_free(*options
);
700 if (ltype
== AF_INET6
&& streq(lvalue
, "SendVendorOption")) {
701 r
= extract_first_word(&p
, &word
, ":", 0);
704 if (r
<= 0 || isempty(p
)) {
705 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
706 "Invalid DHCP option, ignoring assignment: %s", rvalue
);
710 r
= safe_atou32(word
, &enterprise_identifier
);
712 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
713 "Failed to parse DHCPv6 enterprise identifier data, ignoring assignment: %s", p
);
719 r
= extract_first_word(&p
, &word
, ":", 0);
722 if (r
<= 0 || isempty(p
)) {
723 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
724 "Invalid DHCP option, ignoring assignment: %s", rvalue
);
728 if (ltype
== AF_INET6
) {
729 r
= safe_atou16(word
, &u16
);
731 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
732 "Invalid DHCP option, ignoring assignment: %s", rvalue
);
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
);
741 r
= safe_atou8(word
, &u8
);
743 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
744 "Invalid DHCP option, ignoring assignment: %s", rvalue
);
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
);
755 r
= extract_first_word(&p
, &word
, ":", 0);
758 if (r
<= 0 || isempty(p
)) {
759 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
760 "Invalid DHCP option, ignoring assignment: %s", rvalue
);
764 type
= dhcp_option_data_type_from_string(word
);
766 log_syntax(unit
, LOG_WARNING
, filename
, line
, type
,
767 "Invalid DHCP option data type, ignoring assignment: %s", p
);
772 case DHCP_OPTION_DATA_UINT8
:{
773 r
= safe_atou8(p
, &uint8_data
);
775 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
776 "Failed to parse DHCP uint8 data, ignoring assignment: %s", p
);
781 sz
= sizeof(uint8_t);
784 case DHCP_OPTION_DATA_UINT16
:{
787 r
= safe_atou16(p
, &k
);
789 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
790 "Failed to parse DHCP uint16 data, ignoring assignment: %s", p
);
794 uint16_data
= htobe16(k
);
795 udata
= &uint16_data
;
796 sz
= sizeof(uint16_t);
799 case DHCP_OPTION_DATA_UINT32
: {
802 r
= safe_atou32(p
, &k
);
804 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
805 "Failed to parse DHCP uint32 data, ignoring assignment: %s", p
);
809 uint32_data
= htobe32(k
);
810 udata
= &uint32_data
;
811 sz
= sizeof(uint32_t);
815 case DHCP_OPTION_DATA_IPV4ADDRESS
: {
816 r
= in_addr_from_string(AF_INET
, p
, &addr
);
818 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
819 "Failed to parse DHCP ipv4address data, ignoring assignment: %s", p
);
824 sz
= sizeof(addr
.in
.s_addr
);
827 case DHCP_OPTION_DATA_IPV6ADDRESS
: {
828 r
= in_addr_from_string(AF_INET6
, p
, &addr
);
830 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
831 "Failed to parse DHCP ipv6address data, ignoring assignment: %s", p
);
836 sz
= sizeof(addr
.in6
.s6_addr
);
839 case DHCP_OPTION_DATA_STRING
:
840 sz
= cunescape(p
, UNESCAPE_ACCEPT_NUL
, &q
);
842 log_syntax(unit
, LOG_WARNING
, filename
, line
, sz
,
843 "Failed to decode DHCP option data, ignoring assignment: %s", p
);
851 if (ltype
== AF_INET6
) {
852 r
= sd_dhcp6_option_new(u16
, udata
, sz
, enterprise_identifier
, &opt6
);
854 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
855 "Failed to store DHCP option '%s', ignoring assignment: %m", rvalue
);
859 r
= ordered_hashmap_ensure_allocated(options
, &dhcp6_option_hash_ops
);
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
);
867 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
868 "Failed to store DHCP option '%s', ignoring assignment: %m", rvalue
);
873 r
= sd_dhcp_option_new(u8
, udata
, sz
, &opt4
);
875 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
876 "Failed to store DHCP option '%s', ignoring assignment: %m", rvalue
);
880 r
= ordered_hashmap_ensure_allocated(options
, &dhcp_option_hash_ops
);
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
);
888 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
889 "Failed to store DHCP option '%s', ignoring assignment: %m", rvalue
);
897 int config_parse_dhcp_request_options(
899 const char *filename
,
902 unsigned section_line
,
909 Network
*network
= userdata
;
917 if (isempty(rvalue
)) {
918 if (ltype
== AF_INET
)
919 network
->dhcp_request_options
= set_free(network
->dhcp_request_options
);
921 network
->dhcp6_request_options
= set_free(network
->dhcp6_request_options
);
926 for (const char *p
= rvalue
;;) {
927 _cleanup_free_
char *n
= NULL
;
930 r
= extract_first_word(&p
, &n
, NULL
, 0);
934 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
935 "Failed to parse DHCP request option, ignoring assignment: %s",
942 r
= safe_atou32(n
, &i
);
944 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
945 "DHCP request option is invalid, ignoring assignment: %s", n
);
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
);
955 r
= set_ensure_put(ltype
== AF_INET
? &network
->dhcp_request_options
: &network
->dhcp6_request_options
,
956 NULL
, UINT32_TO_PTR(i
));
958 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
959 "Failed to store DHCP request option '%s', ignoring assignment: %m", n
);
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",
969 DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(dhcp_use_domains
, DHCPUseDomains
, DHCP_USE_DOMAINS_YES
);
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",
980 DEFINE_STRING_TABLE_LOOKUP(dhcp_option_data_type
, DHCPOptionDataType
);
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",
988 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(duid_type
, DUIDType
);
990 int config_parse_duid_type(
992 const char *filename
,
995 unsigned section_line
,
1002 _cleanup_free_
char *type_string
= NULL
;
1003 const char *p
= ASSERT_PTR(rvalue
);
1005 DUID
*duid
= ASSERT_PTR(data
);
1012 if (!force
&& duid
->set
)
1015 r
= extract_first_word(&p
, &type_string
, ":", 0);
1019 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1020 "Invalid syntax, ignoring: %s", rvalue
);
1024 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1025 "Failed to extract DUID type from '%s', ignoring.", rvalue
);
1029 type
= duid_type_from_string(type_string
);
1031 log_syntax(unit
, LOG_WARNING
, filename
, line
, type
,
1032 "Failed to parse DUID type '%s', ignoring.", type_string
);
1039 if (type
!= DUID_TYPE_LLT
) {
1040 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1041 "Invalid syntax, ignoring: %s", rvalue
);
1045 r
= parse_timestamp(p
, &u
);
1047 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1048 "Failed to parse timestamp, ignoring: %s", p
);
1061 int config_parse_manager_duid_type(
1063 const char *filename
,
1065 const char *section
,
1066 unsigned section_line
,
1073 Manager
*manager
= ASSERT_PTR(userdata
);
1076 /* For backward compatibility. Setting both DHCPv4 and DHCPv6 DUID if they are not specified explicitly. */
1078 r
= config_parse_duid_type(unit
, filename
, line
, section
, section_line
, lvalue
, false, rvalue
, &manager
->dhcp_duid
, manager
);
1082 return config_parse_duid_type(unit
, filename
, line
, section
, section_line
, lvalue
, false, rvalue
, &manager
->dhcp6_duid
, manager
);
1085 int config_parse_network_duid_type(
1087 const char *filename
,
1089 const char *section
,
1090 unsigned section_line
,
1097 Network
*network
= ASSERT_PTR(userdata
);
1100 r
= config_parse_duid_type(unit
, filename
, line
, section
, section_line
, lvalue
, true, rvalue
, &network
->dhcp_duid
, network
);
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
);
1108 int config_parse_duid_rawdata(
1110 const char *filename
,
1112 const char *section
,
1113 unsigned section_line
,
1120 uint8_t raw_data
[MAX_DUID_LEN
];
1123 DUID
*duid
= ASSERT_PTR(data
);
1129 if (!force
&& duid
->set
)
1132 /* RawData contains DUID in format "NN:NN:NN..." */
1133 for (const char *p
= rvalue
;;) {
1136 _cleanup_free_
char *cbyte
= NULL
;
1138 r
= extract_first_word(&p
, &cbyte
, ":", 0);
1142 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Failed to read DUID, ignoring assignment: %s.", rvalue
);
1148 if (count
>= MAX_DUID_LEN
) {
1149 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "Max DUID length exceeded, ignoring assignment: %s.", rvalue
);
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
);
1158 n1
= unhexchar(cbyte
[0]);
1160 n2
= unhexchar(cbyte
[1]);
1164 if (n1
< 0 || n2
< 0) {
1165 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "Invalid DUID byte: %s. Ignoring assignment: %s.", cbyte
, rvalue
);
1169 byte
= ((uint8_t) n1
<< (4 * (len
-1))) | (uint8_t) n2
;
1170 raw_data
[count
++] = byte
;
1173 assert_cc(sizeof(raw_data
) == sizeof(duid
->raw_data
));
1174 memcpy(duid
->raw_data
, raw_data
, count
);
1175 duid
->raw_data_len
= count
;
1181 int config_parse_manager_duid_rawdata(
1183 const char *filename
,
1185 const char *section
,
1186 unsigned section_line
,
1193 Manager
*manager
= ASSERT_PTR(userdata
);
1196 /* For backward compatibility. Setting both DHCPv4 and DHCPv6 DUID if they are not specified explicitly. */
1198 r
= config_parse_duid_rawdata(unit
, filename
, line
, section
, section_line
, lvalue
, false, rvalue
, &manager
->dhcp_duid
, manager
);
1202 return config_parse_duid_rawdata(unit
, filename
, line
, section
, section_line
, lvalue
, false, rvalue
, &manager
->dhcp6_duid
, manager
);
1205 int config_parse_network_duid_rawdata(
1207 const char *filename
,
1209 const char *section
,
1210 unsigned section_line
,
1217 Network
*network
= ASSERT_PTR(userdata
);
1220 r
= config_parse_duid_rawdata(unit
, filename
, line
, section
, section_line
, lvalue
, true, rvalue
, &network
->dhcp_duid
, network
);
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
);
1228 int config_parse_uplink(
1230 const char *filename
,
1232 const char *section
,
1233 unsigned section_line
,
1240 Network
*network
= ASSERT_PTR(userdata
);
1241 bool accept_none
= true;
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;
1261 assert_not_reached();
1263 if (isempty(rvalue
) || streq(rvalue
, ":auto")) {
1264 *index
= UPLINK_INDEX_AUTO
;
1265 *name
= mfree(*name
);
1269 if (accept_none
&& streq(rvalue
, ":none")) {
1270 *index
= UPLINK_INDEX_NONE
;
1271 *name
= mfree(*name
);
1275 if (!accept_none
&& streq(rvalue
, ":self")) {
1276 *index
= UPLINK_INDEX_SELF
;
1277 *name
= mfree(*name
);
1281 r
= parse_ifindex(rvalue
);
1284 *name
= mfree(*name
);
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
);
1294 /* The interface name will be resolved later. */
1295 r
= free_and_strdup_warn(name
, rvalue
);
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
;