1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright (C) 2013 Tom Gundersen <teg@jklm.no>
10 #include <netinet/ether.h>
15 #include "alloc-util.h"
16 #include "condition.h"
17 #include "conf-parser.h"
18 #include "dhcp-lease-internal.h"
19 #include "ether-addr-util.h"
20 #include "hexdecoct.h"
22 #include "network-internal.h"
23 #include "parse-util.h"
24 #include "siphash24.h"
25 #include "socket-util.h"
26 #include "string-util.h"
31 const char *net_get_name(struct udev_device
*device
) {
32 const char *name
, *field
;
36 /* fetch some persistent data unique (on this machine) to this device */
37 FOREACH_STRING(field
, "ID_NET_NAME_ONBOARD", "ID_NET_NAME_SLOT", "ID_NET_NAME_PATH", "ID_NET_NAME_MAC") {
38 name
= udev_device_get_property_value(device
, field
);
46 #define HASH_KEY SD_ID128_MAKE(d3,1e,48,fa,90,fe,4b,4c,9d,af,d5,d7,a1,b1,2e,8a)
48 int net_get_unique_predictable_data(struct udev_device
*device
, uint64_t *result
) {
50 const char *name
= NULL
;
56 name
= net_get_name(device
);
61 sz
= sizeof(sd_id128_t
) + l
;
64 /* fetch some persistent data unique to this machine */
65 r
= sd_id128_get_machine((sd_id128_t
*) v
);
68 memcpy(v
+ sizeof(sd_id128_t
), name
, l
);
70 /* Let's hash the machine ID plus the device name. We
71 * use a fixed, but originally randomly created hash
73 *result
= htole64(siphash24(v
, sz
, HASH_KEY
.bytes
));
78 static bool net_condition_test_strv(char * const *raw_patterns
,
80 if (strv_isempty(raw_patterns
))
83 /* If the patterns begin with "!", edit it out and negate the test. */
84 if (raw_patterns
[0][0] == '!') {
88 length
= strv_length(raw_patterns
) + 1; /* Include the NULL. */
89 patterns
= newa(char*, length
);
90 patterns
[0] = raw_patterns
[0] + 1; /* Skip the "!". */
91 for (i
= 1; i
< length
; i
++)
92 patterns
[i
] = raw_patterns
[i
];
94 return !string
|| !strv_fnmatch(patterns
, string
, 0);
97 return string
&& strv_fnmatch(raw_patterns
, string
, 0);
100 bool net_match_config(const struct ether_addr
*match_mac
,
101 char * const *match_paths
,
102 char * const *match_drivers
,
103 char * const *match_types
,
104 char * const *match_names
,
105 Condition
*match_host
,
106 Condition
*match_virt
,
107 Condition
*match_kernel_cmdline
,
108 Condition
*match_kernel_version
,
109 Condition
*match_arch
,
110 const struct ether_addr
*dev_mac
,
111 const char *dev_path
,
112 const char *dev_parent_driver
,
113 const char *dev_driver
,
114 const char *dev_type
,
115 const char *dev_name
) {
117 if (match_host
&& condition_test(match_host
) <= 0)
120 if (match_virt
&& condition_test(match_virt
) <= 0)
123 if (match_kernel_cmdline
&& condition_test(match_kernel_cmdline
) <= 0)
126 if (match_kernel_version
&& condition_test(match_kernel_version
) <= 0)
129 if (match_arch
&& condition_test(match_arch
) <= 0)
132 if (match_mac
&& (!dev_mac
|| memcmp(match_mac
, dev_mac
, ETH_ALEN
)))
135 if (!net_condition_test_strv(match_paths
, dev_path
))
138 if (!net_condition_test_strv(match_drivers
, dev_driver
))
141 if (!net_condition_test_strv(match_types
, dev_type
))
144 if (!net_condition_test_strv(match_names
, dev_name
))
150 int config_parse_net_condition(const char *unit
,
151 const char *filename
,
154 unsigned section_line
,
161 ConditionType cond
= ltype
;
162 Condition
**ret
= data
;
165 _cleanup_free_
char *s
= NULL
;
172 negate
= rvalue
[0] == '!';
180 c
= condition_new(cond
, s
, false, negate
);
185 condition_free(*ret
);
191 int config_parse_ifnames(
193 const char *filename
,
196 unsigned section_line
,
212 _cleanup_free_
char *word
= NULL
;
214 r
= extract_first_word(&rvalue
, &word
, NULL
, 0);
216 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse interface name list: %s", rvalue
);
222 if (!ifname_valid(word
)) {
223 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Interface name is not valid or too long, ignoring assignment: %s", rvalue
);
227 r
= strv_push(sv
, word
);
237 int config_parse_ifalias(const char *unit
,
238 const char *filename
,
241 unsigned section_line
,
249 _cleanup_free_
char *n
= NULL
;
260 if (!ascii_is_valid(n
) || strlen(n
) >= IFALIASZ
) {
261 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Interface alias is not ASCII clean or is too long, ignoring assignment: %s", rvalue
);
274 int config_parse_hwaddr(const char *unit
,
275 const char *filename
,
278 unsigned section_line
,
284 struct ether_addr
**hwaddr
= data
;
285 struct ether_addr
*n
;
295 n
= new0(struct ether_addr
, 1);
299 start
= rvalue
+ strspn(rvalue
, WHITESPACE
);
300 r
= ether_addr_from_string(start
, n
, &offset
);
302 if (r
|| (start
[offset
+ strspn(start
+ offset
, WHITESPACE
)] != '\0')) {
303 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Not a valid MAC address, ignoring assignment: %s", rvalue
);
314 int config_parse_iaid(const char *unit
,
315 const char *filename
,
318 unsigned section_line
,
332 r
= safe_atou32(rvalue
, &iaid
);
334 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
335 "Unable to read IAID, ignoring assignment: %s", rvalue
);
339 *((uint32_t *)data
) = iaid
;
344 int config_parse_bridge_port_priority(
346 const char *filename
,
349 unsigned section_line
,
364 r
= safe_atou16(rvalue
, &i
);
366 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
367 "Failed to parse bridge port priority, ignoring: %s", rvalue
);
371 if (i
> LINK_BRIDGE_PORT_PRIORITY_MAX
) {
372 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
373 "Bridge port priority is larger than maximum %u, ignoring: %s", LINK_BRIDGE_PORT_PRIORITY_MAX
, rvalue
);
377 *((uint16_t *)data
) = i
;
382 void serialize_in_addrs(FILE *f
, const struct in_addr
*addresses
, size_t size
) {
389 for (i
= 0; i
< size
; i
++)
390 fprintf(f
, "%s%s", inet_ntoa(addresses
[i
]),
391 (i
< (size
- 1)) ? " ": "");
394 int deserialize_in_addrs(struct in_addr
**ret
, const char *string
) {
395 _cleanup_free_
struct in_addr
*addresses
= NULL
;
402 _cleanup_free_
char *word
= NULL
;
403 struct in_addr
*new_addresses
;
406 r
= extract_first_word(&string
, &word
, NULL
, 0);
412 new_addresses
= reallocarray(addresses
, size
+ 1, sizeof(struct in_addr
));
416 addresses
= new_addresses
;
418 r
= inet_pton(AF_INET
, word
, &(addresses
[size
]));
425 *ret
= TAKE_PTR(addresses
);
430 void serialize_in6_addrs(FILE *f
, const struct in6_addr
*addresses
, size_t size
) {
437 for (i
= 0; i
< size
; i
++) {
438 char buffer
[INET6_ADDRSTRLEN
];
440 fputs(inet_ntop(AF_INET6
, addresses
+i
, buffer
, sizeof(buffer
)), f
);
447 int deserialize_in6_addrs(struct in6_addr
**ret
, const char *string
) {
448 _cleanup_free_
struct in6_addr
*addresses
= NULL
;
455 _cleanup_free_
char *word
= NULL
;
456 struct in6_addr
*new_addresses
;
459 r
= extract_first_word(&string
, &word
, NULL
, 0);
465 new_addresses
= reallocarray(addresses
, size
+ 1, sizeof(struct in6_addr
));
469 addresses
= new_addresses
;
471 r
= inet_pton(AF_INET6
, word
, &(addresses
[size
]));
478 *ret
= TAKE_PTR(addresses
);
483 void serialize_dhcp_routes(FILE *f
, const char *key
, sd_dhcp_route
**routes
, size_t size
) {
491 fprintf(f
, "%s=", key
);
493 for (i
= 0; i
< size
; i
++) {
494 struct in_addr dest
, gw
;
497 assert_se(sd_dhcp_route_get_destination(routes
[i
], &dest
) >= 0);
498 assert_se(sd_dhcp_route_get_gateway(routes
[i
], &gw
) >= 0);
499 assert_se(sd_dhcp_route_get_destination_prefix_length(routes
[i
], &length
) >= 0);
501 fprintf(f
, "%s/%" PRIu8
, inet_ntoa(dest
), length
);
502 fprintf(f
, ",%s%s", inet_ntoa(gw
), (i
< (size
- 1)) ? " ": "");
508 int deserialize_dhcp_routes(struct sd_dhcp_route
**ret
, size_t *ret_size
, size_t *ret_allocated
, const char *string
) {
509 _cleanup_free_
struct sd_dhcp_route
*routes
= NULL
;
510 size_t size
= 0, allocated
= 0;
514 assert(ret_allocated
);
517 /* WORD FORMAT: dst_ip/dst_prefixlen,gw_ip */
519 _cleanup_free_
char *word
= NULL
;
524 r
= extract_first_word(&string
, &word
, NULL
, 0);
530 if (!GREEDY_REALLOC(routes
, allocated
, size
+ 1))
536 tok_end
= strchr(tok
, '/');
541 r
= inet_aton(tok
, &routes
[size
].dst_addr
);
547 /* get the prefixlen */
548 tok_end
= strchr(tok
, ',');
554 r
= safe_atou(tok
, &n
);
558 routes
[size
].dst_prefixlen
= (uint8_t) n
;
561 /* get the gateway */
562 r
= inet_aton(tok
, &routes
[size
].gw_addr
);
570 *ret_allocated
= allocated
;
571 *ret
= TAKE_PTR(routes
);
576 int serialize_dhcp_option(FILE *f
, const char *key
, const void *data
, size_t size
) {
577 _cleanup_free_
char *hex_buf
= NULL
;
583 hex_buf
= hexmem(data
, size
);
587 fprintf(f
, "%s=%s\n", key
, hex_buf
);
592 int deserialize_dhcp_option(void **data
, size_t *data_len
, const char *string
) {
597 if (strlen(string
) % 2)
600 return unhexmem(string
, strlen(string
), (void **)data
, data_len
);