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
= userdata
;
135 const sd_bus_error
*e
;
143 /* To avoid calling GetProductUUID() bus method so frequently, set the flag below
144 * even if the method fails. */
145 manager
->has_product_uuid
= true;
147 e
= sd_bus_message_get_error(m
);
149 r
= sd_bus_error_get_errno(e
);
150 log_warning_errno(r
, "Could not get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %s",
151 bus_error_message(e
, r
));
155 r
= sd_bus_message_read_array(m
, 'y', &a
, &sz
);
157 log_warning_errno(r
, "Failed to get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %m");
161 if (sz
!= sizeof(sd_id128_t
)) {
162 log_warning("Invalid product UUID. Falling back to use machine-app-specific ID as DUID-UUID.");
166 log_debug("Successfully obtained product UUID");
168 memcpy(&manager
->duid_product_uuid
.raw_data
, a
, sz
);
169 manager
->duid_product_uuid
.raw_data_len
= sz
;
174 int manager_request_product_uuid(Manager
*m
) {
175 static bool bus_method_is_called
= false;
180 if (bus_method_is_called
)
183 if (sd_bus_is_ready(m
->bus
) <= 0 && !m
->product_uuid_requested
) {
184 log_debug("Not connected to system bus, requesting product UUID later.");
185 m
->product_uuid_requested
= true;
189 m
->product_uuid_requested
= false;
191 r
= bus_call_method_async(
196 get_product_uuid_handler
,
201 return log_warning_errno(r
, "Failed to get product UUID: %m");
203 log_debug("Requesting product UUID.");
205 bus_method_is_called
= true;
210 int dhcp_configure_duid(Link
*link
, const DUID
*duid
) {
215 assert(link
->manager
);
220 if (!duid_needs_product_uuid(duid
))
223 if (m
->has_product_uuid
)
226 r
= manager_request_product_uuid(m
);
228 log_link_warning_errno(link
, r
,
229 "Failed to get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %m");
231 m
->has_product_uuid
= true; /* Do not request UUID again on failure. */
238 bool address_is_filtered(int family
, const union in_addr_union
*address
, uint8_t prefixlen
, Set
*allow_list
, Set
*deny_list
) {
239 struct in_addr_prefix
*p
;
241 assert(IN_SET(family
, AF_INET
, AF_INET6
));
245 SET_FOREACH(p
, allow_list
)
246 if (p
->family
== family
&&
247 p
->prefixlen
<= prefixlen
&&
248 in_addr_prefix_covers(family
, &p
->address
, p
->prefixlen
, address
) > 0)
254 SET_FOREACH(p
, deny_list
)
255 if (p
->family
== family
&&
256 in_addr_prefix_intersect(family
, &p
->address
, p
->prefixlen
, address
, prefixlen
) > 0)
262 int config_parse_dhcp(
264 const char *filename
,
267 unsigned section_line
,
274 AddressFamily
*dhcp
= data
, s
;
281 /* Note that this is mostly like
282 * config_parse_address_family(), except that it
283 * understands some old names for the enum values */
285 s
= address_family_from_string(rvalue
);
288 /* Previously, we had a slightly different enum here,
289 * support its values for compatibility. */
291 s
= dhcp_deprecated_address_family_from_string(rvalue
);
293 log_syntax(unit
, LOG_WARNING
, filename
, line
, s
,
294 "Failed to parse DHCP option, ignoring: %s", rvalue
);
298 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
299 "DHCP=%s is deprecated, please use DHCP=%s instead.",
300 rvalue
, address_family_to_string(s
));
307 int config_parse_dhcp_or_ra_route_metric(
309 const char *filename
,
312 unsigned section_line
,
319 Network
*network
= userdata
;
325 assert(IN_SET(ltype
, AF_UNSPEC
, AF_INET
, AF_INET6
));
329 r
= safe_atou32(rvalue
, &metric
);
331 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
332 "Failed to parse RouteMetric=%s, ignoring assignment: %m", rvalue
);
338 network
->dhcp_route_metric
= metric
;
339 network
->dhcp_route_metric_set
= true;
342 network
->ipv6_accept_ra_route_metric
= metric
;
343 network
->ipv6_accept_ra_route_metric_set
= true;
346 /* For backward compatibility. */
347 if (!network
->dhcp_route_metric_set
)
348 network
->dhcp_route_metric
= metric
;
349 if (!network
->ipv6_accept_ra_route_metric_set
)
350 network
->ipv6_accept_ra_route_metric
= metric
;
353 assert_not_reached();
359 int config_parse_dhcp_use_dns(
361 const char *filename
,
364 unsigned section_line
,
371 Network
*network
= userdata
;
376 assert(IN_SET(ltype
, AF_UNSPEC
, AF_INET
, AF_INET6
));
380 r
= parse_boolean(rvalue
);
382 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
383 "Failed to parse UseDNS=%s, ignoring assignment: %m", rvalue
);
389 network
->dhcp_use_dns
= r
;
390 network
->dhcp_use_dns_set
= true;
393 network
->dhcp6_use_dns
= r
;
394 network
->dhcp6_use_dns_set
= true;
397 /* For backward compatibility. */
398 if (!network
->dhcp_use_dns_set
)
399 network
->dhcp_use_dns
= r
;
400 if (!network
->dhcp6_use_dns_set
)
401 network
->dhcp6_use_dns
= r
;
404 assert_not_reached();
410 int config_parse_dhcp_use_domains(
412 const char *filename
,
415 unsigned section_line
,
422 Network
*network
= userdata
;
427 assert(IN_SET(ltype
, AF_UNSPEC
, AF_INET
, AF_INET6
));
431 d
= dhcp_use_domains_from_string(rvalue
);
433 log_syntax(unit
, LOG_WARNING
, filename
, line
, d
,
434 "Failed to parse %s=%s, ignoring assignment: %m", lvalue
, rvalue
);
440 network
->dhcp_use_domains
= d
;
441 network
->dhcp_use_domains_set
= true;
444 network
->dhcp6_use_domains
= d
;
445 network
->dhcp6_use_domains_set
= true;
448 /* For backward compatibility. */
449 if (!network
->dhcp_use_domains_set
)
450 network
->dhcp_use_domains
= d
;
451 if (!network
->dhcp6_use_domains_set
)
452 network
->dhcp6_use_domains
= d
;
455 assert_not_reached();
461 int config_parse_dhcp_use_ntp(
463 const char *filename
,
466 unsigned section_line
,
473 Network
*network
= userdata
;
478 assert(IN_SET(ltype
, AF_UNSPEC
, AF_INET
, AF_INET6
));
482 r
= parse_boolean(rvalue
);
484 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
485 "Failed to parse UseNTP=%s, ignoring assignment: %m", rvalue
);
491 network
->dhcp_use_ntp
= r
;
492 network
->dhcp_use_ntp_set
= true;
495 network
->dhcp6_use_ntp
= r
;
496 network
->dhcp6_use_ntp_set
= true;
499 /* For backward compatibility. */
500 if (!network
->dhcp_use_ntp_set
)
501 network
->dhcp_use_ntp
= r
;
502 if (!network
->dhcp6_use_ntp_set
)
503 network
->dhcp6_use_ntp
= r
;
506 assert_not_reached();
512 int config_parse_dhcp_or_ra_route_table(
514 const char *filename
,
517 unsigned section_line
,
524 Network
*network
= userdata
;
530 assert(IN_SET(ltype
, AF_INET
, AF_INET6
));
534 r
= safe_atou32(rvalue
, &rt
);
536 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
537 "Failed to parse RouteTable=%s, ignoring assignment: %m", rvalue
);
543 network
->dhcp_route_table
= rt
;
544 network
->dhcp_route_table_set
= true;
547 network
->ipv6_accept_ra_route_table
= rt
;
548 network
->ipv6_accept_ra_route_table_set
= true;
551 assert_not_reached();
557 int config_parse_iaid(
559 const char *filename
,
562 unsigned section_line
,
569 Network
*network
= userdata
;
577 assert(IN_SET(ltype
, AF_INET
, AF_INET6
));
579 r
= safe_atou32(rvalue
, &iaid
);
581 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
582 "Unable to read IAID, ignoring assignment: %s", rvalue
);
586 if (ltype
== AF_INET
) {
587 network
->dhcp_iaid
= iaid
;
588 network
->dhcp_iaid_set
= true;
589 if (!network
->dhcp6_iaid_set_explicitly
) {
590 /* Backward compatibility. Previously, IAID is shared by DHCPv4 and DHCPv6.
591 * If DHCPv6 IAID is not specified explicitly, then use DHCPv4 IAID for DHCPv6. */
592 network
->dhcp6_iaid
= iaid
;
593 network
->dhcp6_iaid_set
= true;
596 assert(ltype
== AF_INET6
);
597 network
->dhcp6_iaid
= iaid
;
598 network
->dhcp6_iaid_set
= true;
599 network
->dhcp6_iaid_set_explicitly
= true;
605 int config_parse_dhcp_user_or_vendor_class(
607 const char *filename
,
610 unsigned section_line
,
623 assert(IN_SET(ltype
, AF_INET
, AF_INET6
));
625 if (isempty(rvalue
)) {
630 for (const char *p
= rvalue
;;) {
631 _cleanup_free_
char *w
= NULL
;
634 r
= extract_first_word(&p
, &w
, NULL
, EXTRACT_CUNESCAPE
|EXTRACT_UNQUOTE
);
638 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
639 "Failed to split user classes option, ignoring: %s", rvalue
);
646 if (ltype
== AF_INET
) {
647 if (len
> UINT8_MAX
|| len
== 0) {
648 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
649 "%s length is not in the range 1…255, ignoring.", w
);
653 if (len
> UINT16_MAX
|| len
== 0) {
654 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
655 "%s length is not in the range 1…65535, ignoring.", w
);
660 r
= strv_consume(l
, TAKE_PTR(w
));
666 int config_parse_dhcp_send_option(
668 const char *filename
,
671 unsigned section_line
,
678 _cleanup_(sd_dhcp_option_unrefp
) sd_dhcp_option
*opt4
= NULL
;
679 _cleanup_(sd_dhcp6_option_unrefp
) sd_dhcp6_option
*opt6
= NULL
;
680 _unused_
_cleanup_(sd_dhcp_option_unrefp
) sd_dhcp_option
*old4
= NULL
;
681 _unused_
_cleanup_(sd_dhcp6_option_unrefp
) sd_dhcp6_option
*old6
= NULL
;
682 uint32_t uint32_data
, enterprise_identifier
= 0;
683 _cleanup_free_
char *word
= NULL
, *q
= NULL
;
684 OrderedHashmap
**options
= data
;
685 uint16_t u16
, uint16_data
;
686 union in_addr_union addr
;
687 DHCPOptionDataType type
;
688 uint8_t u8
, uint8_data
;
699 if (isempty(rvalue
)) {
700 *options
= ordered_hashmap_free(*options
);
705 if (ltype
== AF_INET6
&& streq(lvalue
, "SendVendorOption")) {
706 r
= extract_first_word(&p
, &word
, ":", 0);
709 if (r
<= 0 || isempty(p
)) {
710 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
711 "Invalid DHCP option, ignoring assignment: %s", rvalue
);
715 r
= safe_atou32(word
, &enterprise_identifier
);
717 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
718 "Failed to parse DHCPv6 enterprise identifier data, ignoring assignment: %s", p
);
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 if (ltype
== AF_INET6
) {
734 r
= safe_atou16(word
, &u16
);
736 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
737 "Invalid DHCP option, ignoring assignment: %s", rvalue
);
740 if (u16
< 1 || u16
>= UINT16_MAX
) {
741 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
742 "Invalid DHCP option, valid range is 1-65535, ignoring assignment: %s", rvalue
);
746 r
= safe_atou8(word
, &u8
);
748 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
749 "Invalid DHCP option, ignoring assignment: %s", rvalue
);
752 if (u8
< 1 || u8
>= UINT8_MAX
) {
753 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
754 "Invalid DHCP option, valid range is 1-254, ignoring assignment: %s", rvalue
);
760 r
= extract_first_word(&p
, &word
, ":", 0);
763 if (r
<= 0 || isempty(p
)) {
764 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
765 "Invalid DHCP option, ignoring assignment: %s", rvalue
);
769 type
= dhcp_option_data_type_from_string(word
);
771 log_syntax(unit
, LOG_WARNING
, filename
, line
, type
,
772 "Invalid DHCP option data type, ignoring assignment: %s", p
);
777 case DHCP_OPTION_DATA_UINT8
:{
778 r
= safe_atou8(p
, &uint8_data
);
780 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
781 "Failed to parse DHCP uint8 data, ignoring assignment: %s", p
);
786 sz
= sizeof(uint8_t);
789 case DHCP_OPTION_DATA_UINT16
:{
792 r
= safe_atou16(p
, &k
);
794 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
795 "Failed to parse DHCP uint16 data, ignoring assignment: %s", p
);
799 uint16_data
= htobe16(k
);
800 udata
= &uint16_data
;
801 sz
= sizeof(uint16_t);
804 case DHCP_OPTION_DATA_UINT32
: {
807 r
= safe_atou32(p
, &k
);
809 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
810 "Failed to parse DHCP uint32 data, ignoring assignment: %s", p
);
814 uint32_data
= htobe32(k
);
815 udata
= &uint32_data
;
816 sz
= sizeof(uint32_t);
820 case DHCP_OPTION_DATA_IPV4ADDRESS
: {
821 r
= in_addr_from_string(AF_INET
, p
, &addr
);
823 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
824 "Failed to parse DHCP ipv4address data, ignoring assignment: %s", p
);
829 sz
= sizeof(addr
.in
.s_addr
);
832 case DHCP_OPTION_DATA_IPV6ADDRESS
: {
833 r
= in_addr_from_string(AF_INET6
, p
, &addr
);
835 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
836 "Failed to parse DHCP ipv6address data, ignoring assignment: %s", p
);
841 sz
= sizeof(addr
.in6
.s6_addr
);
844 case DHCP_OPTION_DATA_STRING
:
845 sz
= cunescape(p
, UNESCAPE_ACCEPT_NUL
, &q
);
847 log_syntax(unit
, LOG_WARNING
, filename
, line
, sz
,
848 "Failed to decode DHCP option data, ignoring assignment: %s", p
);
856 if (ltype
== AF_INET6
) {
857 r
= sd_dhcp6_option_new(u16
, udata
, sz
, enterprise_identifier
, &opt6
);
859 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
860 "Failed to store DHCP option '%s', ignoring assignment: %m", rvalue
);
864 r
= ordered_hashmap_ensure_allocated(options
, &dhcp6_option_hash_ops
);
868 /* Overwrite existing option */
869 old6
= ordered_hashmap_get(*options
, UINT_TO_PTR(u16
));
870 r
= ordered_hashmap_replace(*options
, UINT_TO_PTR(u16
), opt6
);
872 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
873 "Failed to store DHCP option '%s', ignoring assignment: %m", rvalue
);
878 r
= sd_dhcp_option_new(u8
, udata
, sz
, &opt4
);
880 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
881 "Failed to store DHCP option '%s', ignoring assignment: %m", rvalue
);
885 r
= ordered_hashmap_ensure_allocated(options
, &dhcp_option_hash_ops
);
889 /* Overwrite existing option */
890 old4
= ordered_hashmap_get(*options
, UINT_TO_PTR(u8
));
891 r
= ordered_hashmap_replace(*options
, UINT_TO_PTR(u8
), opt4
);
893 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
894 "Failed to store DHCP option '%s', ignoring assignment: %m", rvalue
);
902 int config_parse_dhcp_request_options(
904 const char *filename
,
907 unsigned section_line
,
914 Network
*network
= userdata
;
922 if (isempty(rvalue
)) {
923 if (ltype
== AF_INET
)
924 network
->dhcp_request_options
= set_free(network
->dhcp_request_options
);
926 network
->dhcp6_request_options
= set_free(network
->dhcp6_request_options
);
931 for (const char *p
= rvalue
;;) {
932 _cleanup_free_
char *n
= NULL
;
935 r
= extract_first_word(&p
, &n
, NULL
, 0);
939 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
940 "Failed to parse DHCP request option, ignoring assignment: %s",
947 r
= safe_atou32(n
, &i
);
949 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
950 "DHCP request option is invalid, ignoring assignment: %s", n
);
954 if (i
< 1 || i
>= UINT8_MAX
) {
955 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
956 "DHCP request option is invalid, valid range is 1-254, ignoring assignment: %s", n
);
960 r
= set_ensure_put(ltype
== AF_INET
? &network
->dhcp_request_options
: &network
->dhcp6_request_options
,
961 NULL
, UINT32_TO_PTR(i
));
963 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
964 "Failed to store DHCP request option '%s', ignoring assignment: %m", n
);
968 static const char* const dhcp_use_domains_table
[_DHCP_USE_DOMAINS_MAX
] = {
969 [DHCP_USE_DOMAINS_NO
] = "no",
970 [DHCP_USE_DOMAINS_ROUTE
] = "route",
971 [DHCP_USE_DOMAINS_YES
] = "yes",
974 DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(dhcp_use_domains
, DHCPUseDomains
, DHCP_USE_DOMAINS_YES
);
976 static const char * const dhcp_option_data_type_table
[_DHCP_OPTION_DATA_MAX
] = {
977 [DHCP_OPTION_DATA_UINT8
] = "uint8",
978 [DHCP_OPTION_DATA_UINT16
] = "uint16",
979 [DHCP_OPTION_DATA_UINT32
] = "uint32",
980 [DHCP_OPTION_DATA_STRING
] = "string",
981 [DHCP_OPTION_DATA_IPV4ADDRESS
] = "ipv4address",
982 [DHCP_OPTION_DATA_IPV6ADDRESS
] = "ipv6address",
985 DEFINE_STRING_TABLE_LOOKUP(dhcp_option_data_type
, DHCPOptionDataType
);
987 static const char* const duid_type_table
[_DUID_TYPE_MAX
] = {
988 [DUID_TYPE_LLT
] = "link-layer-time",
989 [DUID_TYPE_EN
] = "vendor",
990 [DUID_TYPE_LL
] = "link-layer",
991 [DUID_TYPE_UUID
] = "uuid",
993 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(duid_type
, DUIDType
);
995 int config_parse_duid_type(
997 const char *filename
,
1000 unsigned section_line
,
1007 _cleanup_free_
char *type_string
= NULL
;
1008 const char *p
= rvalue
;
1019 if (!force
&& duid
->set
)
1022 r
= extract_first_word(&p
, &type_string
, ":", 0);
1026 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1027 "Invalid syntax, ignoring: %s", rvalue
);
1031 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1032 "Failed to extract DUID type from '%s', ignoring.", rvalue
);
1036 type
= duid_type_from_string(type_string
);
1038 log_syntax(unit
, LOG_WARNING
, filename
, line
, type
,
1039 "Failed to parse DUID type '%s', ignoring.", type_string
);
1046 if (type
!= DUID_TYPE_LLT
) {
1047 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1048 "Invalid syntax, ignoring: %s", rvalue
);
1052 r
= parse_timestamp(p
, &u
);
1054 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1055 "Failed to parse timestamp, ignoring: %s", p
);
1068 int config_parse_manager_duid_type(
1070 const char *filename
,
1072 const char *section
,
1073 unsigned section_line
,
1080 Manager
*manager
= userdata
;
1085 /* For backward compatibility. Setting both DHCPv4 and DHCPv6 DUID if they are not specified explicitly. */
1087 r
= config_parse_duid_type(unit
, filename
, line
, section
, section_line
, lvalue
, false, rvalue
, &manager
->dhcp_duid
, manager
);
1091 return config_parse_duid_type(unit
, filename
, line
, section
, section_line
, lvalue
, false, rvalue
, &manager
->dhcp6_duid
, manager
);
1094 int config_parse_network_duid_type(
1096 const char *filename
,
1098 const char *section
,
1099 unsigned section_line
,
1106 Network
*network
= userdata
;
1111 r
= config_parse_duid_type(unit
, filename
, line
, section
, section_line
, lvalue
, true, rvalue
, &network
->dhcp_duid
, network
);
1115 /* For backward compatibility, also set DHCPv6 DUID if not specified explicitly. */
1116 return config_parse_duid_type(unit
, filename
, line
, section
, section_line
, lvalue
, false, rvalue
, &network
->dhcp6_duid
, network
);
1119 int config_parse_duid_rawdata(
1121 const char *filename
,
1123 const char *section
,
1124 unsigned section_line
,
1131 uint8_t raw_data
[MAX_DUID_LEN
];
1141 if (!force
&& duid
->set
)
1144 /* RawData contains DUID in format "NN:NN:NN..." */
1145 for (const char *p
= rvalue
;;) {
1148 _cleanup_free_
char *cbyte
= NULL
;
1150 r
= extract_first_word(&p
, &cbyte
, ":", 0);
1154 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Failed to read DUID, ignoring assignment: %s.", rvalue
);
1160 if (count
>= MAX_DUID_LEN
) {
1161 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "Max DUID length exceeded, ignoring assignment: %s.", rvalue
);
1165 len
= strlen(cbyte
);
1166 if (!IN_SET(len
, 1, 2)) {
1167 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "Invalid length - DUID byte: %s, ignoring assignment: %s.", cbyte
, rvalue
);
1170 n1
= unhexchar(cbyte
[0]);
1172 n2
= unhexchar(cbyte
[1]);
1176 if (n1
< 0 || n2
< 0) {
1177 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "Invalid DUID byte: %s. Ignoring assignment: %s.", cbyte
, rvalue
);
1181 byte
= ((uint8_t) n1
<< (4 * (len
-1))) | (uint8_t) n2
;
1182 raw_data
[count
++] = byte
;
1185 assert_cc(sizeof(raw_data
) == sizeof(duid
->raw_data
));
1186 memcpy(duid
->raw_data
, raw_data
, count
);
1187 duid
->raw_data_len
= count
;
1193 int config_parse_manager_duid_rawdata(
1195 const char *filename
,
1197 const char *section
,
1198 unsigned section_line
,
1205 Manager
*manager
= userdata
;
1210 /* For backward compatibility. Setting both DHCPv4 and DHCPv6 DUID if they are not specified explicitly. */
1212 r
= config_parse_duid_rawdata(unit
, filename
, line
, section
, section_line
, lvalue
, false, rvalue
, &manager
->dhcp_duid
, manager
);
1216 return config_parse_duid_rawdata(unit
, filename
, line
, section
, section_line
, lvalue
, false, rvalue
, &manager
->dhcp6_duid
, manager
);
1219 int config_parse_network_duid_rawdata(
1221 const char *filename
,
1223 const char *section
,
1224 unsigned section_line
,
1231 Network
*network
= userdata
;
1236 r
= config_parse_duid_rawdata(unit
, filename
, line
, section
, section_line
, lvalue
, true, rvalue
, &network
->dhcp_duid
, network
);
1240 /* For backward compatibility, also set DHCPv6 DUID if not specified explicitly. */
1241 return config_parse_duid_rawdata(unit
, filename
, line
, section
, section_line
, lvalue
, false, rvalue
, &network
->dhcp6_duid
, network
);
1244 int config_parse_uplink(
1246 const char *filename
,
1248 const char *section
,
1249 unsigned section_line
,
1256 Network
*network
= userdata
;
1257 bool accept_none
= true;
1267 if (streq(section
, "DHCPServer")) {
1268 index
= &network
->dhcp_server_uplink_index
;
1269 name
= &network
->dhcp_server_uplink_name
;
1270 } else if (streq(section
, "IPv6SendRA")) {
1271 index
= &network
->router_uplink_index
;
1272 name
= &network
->router_uplink_name
;
1273 } else if (STR_IN_SET(section
, "DHCPv6PrefixDelegation", "DHCPPrefixDelegation")) {
1274 index
= &network
->dhcp_pd_uplink_index
;
1275 name
= &network
->dhcp_pd_uplink_name
;
1276 accept_none
= false;
1278 assert_not_reached();
1280 if (isempty(rvalue
) || streq(rvalue
, ":auto")) {
1281 *index
= UPLINK_INDEX_AUTO
;
1282 *name
= mfree(*name
);
1286 if (accept_none
&& streq(rvalue
, ":none")) {
1287 *index
= UPLINK_INDEX_NONE
;
1288 *name
= mfree(*name
);
1292 if (!accept_none
&& streq(rvalue
, ":self")) {
1293 *index
= UPLINK_INDEX_SELF
;
1294 *name
= mfree(*name
);
1298 r
= parse_ifindex(rvalue
);
1301 *name
= mfree(*name
);
1305 if (!ifname_valid_full(rvalue
, IFNAME_VALID_ALTERNATIVE
)) {
1306 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1307 "Invalid interface name in %s=, ignoring assignment: %s", lvalue
, rvalue
);
1311 /* The interface name will be resolved later. */
1312 r
= free_and_strdup_warn(name
, rvalue
);
1316 /* Note, if uplink_name is set, then uplink_index will be ignored. So, the below does not mean
1317 * an uplink interface will be selected automatically. */
1318 *index
= UPLINK_INDEX_AUTO
;