1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 #include <netinet/ether.h>
10 #include "alloc-util.h"
11 #include "condition.h"
12 #include "conf-parser.h"
13 #include "device-util.h"
14 #include "dhcp-lease-internal.h"
15 #include "ether-addr-util.h"
16 #include "hexdecoct.h"
18 #include "network-internal.h"
19 #include "parse-util.h"
20 #include "siphash24.h"
21 #include "socket-util.h"
22 #include "string-util.h"
27 const char *net_get_name_persistent(sd_device
*device
) {
28 const char *name
, *field
;
32 /* fetch some persistent data unique (on this machine) to this device */
33 FOREACH_STRING(field
, "ID_NET_NAME_ONBOARD", "ID_NET_NAME_SLOT", "ID_NET_NAME_PATH", "ID_NET_NAME_MAC")
34 if (sd_device_get_property_value(device
, field
, &name
) >= 0)
40 #define HASH_KEY SD_ID128_MAKE(d3,1e,48,fa,90,fe,4b,4c,9d,af,d5,d7,a1,b1,2e,8a)
42 int net_get_unique_predictable_data(sd_device
*device
, bool use_sysname
, uint64_t *result
) {
50 /* net_get_name_persistent() will return one of the device names based on stable information about
51 * the device. If this is not available, we fall back to using the actual device name. */
52 name
= net_get_name_persistent(device
);
53 if (!name
&& use_sysname
)
54 (void) sd_device_get_sysname(device
, &name
);
56 return log_device_debug_errno(device
, SYNTHETIC_ERRNO(ENODATA
),
57 "No stable identifying information found");
59 log_device_debug(device
, "Using \"%s\" as stable identifying information", name
);
61 sz
= sizeof(sd_id128_t
) + l
;
62 v
= newa(uint8_t, sz
);
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 use
71 * a fixed, but originally randomly created hash key here. */
72 *result
= htole64(siphash24(v
, sz
, HASH_KEY
.bytes
));
76 static bool net_condition_test_strv(char * const *raw_patterns
,
78 if (strv_isempty(raw_patterns
))
81 /* If the patterns begin with "!", edit it out and negate the test. */
82 if (raw_patterns
[0][0] == '!') {
86 length
= strv_length(raw_patterns
) + 1; /* Include the NULL. */
87 patterns
= newa(char*, length
);
88 patterns
[0] = raw_patterns
[0] + 1; /* Skip the "!". */
89 for (i
= 1; i
< length
; i
++)
90 patterns
[i
] = raw_patterns
[i
];
92 return !string
|| !strv_fnmatch(patterns
, string
, 0);
95 return string
&& strv_fnmatch(raw_patterns
, string
, 0);
98 bool net_match_config(Set
*match_mac
,
99 char * const *match_paths
,
100 char * const *match_drivers
,
101 char * const *match_types
,
102 char * const *match_names
,
103 const struct ether_addr
*dev_mac
,
104 const char *dev_path
,
105 const char *dev_driver
,
106 const char *dev_type
,
107 const char *dev_name
) {
109 if (match_mac
&& (!dev_mac
|| !set_contains(match_mac
, dev_mac
)))
112 if (!net_condition_test_strv(match_paths
, dev_path
))
115 if (!net_condition_test_strv(match_drivers
, dev_driver
))
118 if (!net_condition_test_strv(match_types
, dev_type
))
121 if (!net_condition_test_strv(match_names
, dev_name
))
127 int config_parse_net_condition(const char *unit
,
128 const char *filename
,
131 unsigned section_line
,
138 ConditionType cond
= ltype
;
139 Condition
**list
= data
, *c
;
147 if (isempty(rvalue
)) {
148 *list
= condition_free_list_type(*list
, cond
);
152 negate
= rvalue
[0] == '!';
156 c
= condition_new(cond
, rvalue
, false, negate
);
160 /* Drop previous assignment. */
161 *list
= condition_free_list_type(*list
, cond
);
163 LIST_PREPEND(conditions
, *list
, c
);
167 int config_parse_ifnames(
169 const char *filename
,
172 unsigned section_line
,
188 _cleanup_free_
char *word
= NULL
;
190 r
= extract_first_word(&rvalue
, &word
, NULL
, 0);
192 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse interface name list: %s", rvalue
);
198 if (!ifname_valid(word
)) {
199 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Interface name is not valid or too long, ignoring assignment: %s", rvalue
);
203 r
= strv_push(sv
, word
);
213 int config_parse_ifalias(const char *unit
,
214 const char *filename
,
217 unsigned section_line
,
225 _cleanup_free_
char *n
= NULL
;
236 if (!ascii_is_valid(n
) || strlen(n
) >= IFALIASZ
) {
237 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Interface alias is not ASCII clean or is too long, ignoring assignment: %s", rvalue
);
244 free_and_replace(*s
, n
);
249 int config_parse_hwaddr(const char *unit
,
250 const char *filename
,
253 unsigned section_line
,
260 _cleanup_free_
struct ether_addr
*n
= NULL
;
261 struct ether_addr
**hwaddr
= data
;
269 n
= new0(struct ether_addr
, 1);
273 r
= ether_addr_from_string(rvalue
, n
);
275 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Not a valid MAC address, ignoring assignment: %s", rvalue
);
279 free_and_replace(*hwaddr
, n
);
284 int config_parse_hwaddrs(const char *unit
,
285 const char *filename
,
288 unsigned section_line
,
295 _cleanup_set_free_free_ Set
*s
= NULL
;
296 const char *p
= rvalue
;
297 Set
**hwaddrs
= data
;
305 if (isempty(rvalue
)) {
306 /* Empty assignment resets the list */
307 *hwaddrs
= set_free_free(*hwaddrs
);
311 s
= set_new(ðer_addr_hash_ops
);
316 _cleanup_free_
char *word
= NULL
;
317 _cleanup_free_
struct ether_addr
*n
= NULL
;
319 r
= extract_first_word(&p
, &word
, NULL
, 0);
325 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Invalid syntax, ignoring: %s", rvalue
);
329 n
= new(struct ether_addr
, 1);
333 r
= ether_addr_from_string(word
, n
);
335 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Not a valid MAC address, ignoring: %s", word
);
343 n
= NULL
; /* avoid cleanup */
346 r
= set_ensure_allocated(hwaddrs
, ðer_addr_hash_ops
);
350 r
= set_move(*hwaddrs
, s
);
357 int config_parse_bridge_port_priority(
359 const char *filename
,
362 unsigned section_line
,
377 r
= safe_atou16(rvalue
, &i
);
379 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
380 "Failed to parse bridge port priority, ignoring: %s", rvalue
);
384 if (i
> LINK_BRIDGE_PORT_PRIORITY_MAX
) {
385 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
386 "Bridge port priority is larger than maximum %u, ignoring: %s", LINK_BRIDGE_PORT_PRIORITY_MAX
, rvalue
);
390 *((uint16_t *)data
) = i
;
395 size_t serialize_in_addrs(FILE *f
,
396 const struct in_addr
*addresses
,
398 bool with_leading_space
,
399 bool (*predicate
)(const struct in_addr
*addr
)) {
408 for (i
= 0; i
< size
; i
++) {
409 char sbuf
[INET_ADDRSTRLEN
];
411 if (predicate
&& !predicate(&addresses
[i
]))
413 if (with_leading_space
)
416 with_leading_space
= true;
417 fputs(inet_ntop(AF_INET
, &addresses
[i
], sbuf
, sizeof(sbuf
)), f
);
424 int deserialize_in_addrs(struct in_addr
**ret
, const char *string
) {
425 _cleanup_free_
struct in_addr
*addresses
= NULL
;
432 _cleanup_free_
char *word
= NULL
;
433 struct in_addr
*new_addresses
;
436 r
= extract_first_word(&string
, &word
, NULL
, 0);
442 new_addresses
= reallocarray(addresses
, size
+ 1, sizeof(struct in_addr
));
446 addresses
= new_addresses
;
448 r
= inet_pton(AF_INET
, word
, &(addresses
[size
]));
455 *ret
= size
> 0 ? TAKE_PTR(addresses
) : NULL
;
460 void serialize_in6_addrs(FILE *f
, const struct in6_addr
*addresses
, size_t size
) {
467 for (i
= 0; i
< size
; i
++) {
468 char buffer
[INET6_ADDRSTRLEN
];
470 fputs(inet_ntop(AF_INET6
, addresses
+i
, buffer
, sizeof(buffer
)), f
);
477 int deserialize_in6_addrs(struct in6_addr
**ret
, const char *string
) {
478 _cleanup_free_
struct in6_addr
*addresses
= NULL
;
485 _cleanup_free_
char *word
= NULL
;
486 struct in6_addr
*new_addresses
;
489 r
= extract_first_word(&string
, &word
, NULL
, 0);
495 new_addresses
= reallocarray(addresses
, size
+ 1, sizeof(struct in6_addr
));
499 addresses
= new_addresses
;
501 r
= inet_pton(AF_INET6
, word
, &(addresses
[size
]));
508 *ret
= TAKE_PTR(addresses
);
513 void serialize_dhcp_routes(FILE *f
, const char *key
, sd_dhcp_route
**routes
, size_t size
) {
521 fprintf(f
, "%s=", key
);
523 for (i
= 0; i
< size
; i
++) {
524 char sbuf
[INET_ADDRSTRLEN
];
525 struct in_addr dest
, gw
;
528 assert_se(sd_dhcp_route_get_destination(routes
[i
], &dest
) >= 0);
529 assert_se(sd_dhcp_route_get_gateway(routes
[i
], &gw
) >= 0);
530 assert_se(sd_dhcp_route_get_destination_prefix_length(routes
[i
], &length
) >= 0);
532 fprintf(f
, "%s/%" PRIu8
, inet_ntop(AF_INET
, &dest
, sbuf
, sizeof(sbuf
)), length
);
533 fprintf(f
, ",%s%s", inet_ntop(AF_INET
, &gw
, sbuf
, sizeof(sbuf
)), (i
< (size
- 1)) ? " ": "");
539 int deserialize_dhcp_routes(struct sd_dhcp_route
**ret
, size_t *ret_size
, size_t *ret_allocated
, const char *string
) {
540 _cleanup_free_
struct sd_dhcp_route
*routes
= NULL
;
541 size_t size
= 0, allocated
= 0;
545 assert(ret_allocated
);
548 /* WORD FORMAT: dst_ip/dst_prefixlen,gw_ip */
550 _cleanup_free_
char *word
= NULL
;
555 r
= extract_first_word(&string
, &word
, NULL
, 0);
561 if (!GREEDY_REALLOC(routes
, allocated
, size
+ 1))
567 tok_end
= strchr(tok
, '/');
572 r
= inet_aton(tok
, &routes
[size
].dst_addr
);
578 /* get the prefixlen */
579 tok_end
= strchr(tok
, ',');
585 r
= safe_atou(tok
, &n
);
589 routes
[size
].dst_prefixlen
= (uint8_t) n
;
592 /* get the gateway */
593 r
= inet_aton(tok
, &routes
[size
].gw_addr
);
601 *ret_allocated
= allocated
;
602 *ret
= TAKE_PTR(routes
);
607 int serialize_dhcp_option(FILE *f
, const char *key
, const void *data
, size_t size
) {
608 _cleanup_free_
char *hex_buf
= NULL
;
614 hex_buf
= hexmem(data
, size
);
618 fprintf(f
, "%s=%s\n", key
, hex_buf
);