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 "dhcp-lease-internal.h"
14 #include "ether-addr-util.h"
15 #include "hexdecoct.h"
17 #include "network-internal.h"
18 #include "parse-util.h"
19 #include "siphash24.h"
20 #include "socket-util.h"
21 #include "string-util.h"
26 const char *net_get_name(sd_device
*device
) {
27 const char *name
, *field
;
31 /* fetch some persistent data unique (on this machine) to this device */
32 FOREACH_STRING(field
, "ID_NET_NAME_ONBOARD", "ID_NET_NAME_SLOT", "ID_NET_NAME_PATH", "ID_NET_NAME_MAC")
33 if (sd_device_get_property_value(device
, field
, &name
) >= 0)
39 #define HASH_KEY SD_ID128_MAKE(d3,1e,48,fa,90,fe,4b,4c,9d,af,d5,d7,a1,b1,2e,8a)
41 int net_get_unique_predictable_data(sd_device
*device
, uint64_t *result
) {
43 const char *name
= NULL
;
49 name
= net_get_name(device
);
54 sz
= sizeof(sd_id128_t
) + l
;
55 v
= newa(uint8_t, sz
);
57 /* fetch some persistent data unique to this machine */
58 r
= sd_id128_get_machine((sd_id128_t
*) v
);
61 memcpy(v
+ sizeof(sd_id128_t
), name
, l
);
63 /* Let's hash the machine ID plus the device name. We
64 * use a fixed, but originally randomly created hash
66 *result
= htole64(siphash24(v
, sz
, HASH_KEY
.bytes
));
71 static bool net_condition_test_strv(char * const *raw_patterns
,
73 if (strv_isempty(raw_patterns
))
76 /* If the patterns begin with "!", edit it out and negate the test. */
77 if (raw_patterns
[0][0] == '!') {
81 length
= strv_length(raw_patterns
) + 1; /* Include the NULL. */
82 patterns
= newa(char*, length
);
83 patterns
[0] = raw_patterns
[0] + 1; /* Skip the "!". */
84 for (i
= 1; i
< length
; i
++)
85 patterns
[i
] = raw_patterns
[i
];
87 return !string
|| !strv_fnmatch(patterns
, string
, 0);
90 return string
&& strv_fnmatch(raw_patterns
, string
, 0);
93 bool net_match_config(Set
*match_mac
,
94 char * const *match_paths
,
95 char * const *match_drivers
,
96 char * const *match_types
,
97 char * const *match_names
,
98 Condition
*match_host
,
99 Condition
*match_virt
,
100 Condition
*match_kernel_cmdline
,
101 Condition
*match_kernel_version
,
102 Condition
*match_arch
,
103 const struct ether_addr
*dev_mac
,
104 const char *dev_path
,
105 const char *dev_parent_driver
,
106 const char *dev_driver
,
107 const char *dev_type
,
108 const char *dev_name
) {
110 if (match_host
&& condition_test(match_host
) <= 0)
113 if (match_virt
&& condition_test(match_virt
) <= 0)
116 if (match_kernel_cmdline
&& condition_test(match_kernel_cmdline
) <= 0)
119 if (match_kernel_version
&& condition_test(match_kernel_version
) <= 0)
122 if (match_arch
&& condition_test(match_arch
) <= 0)
125 if (match_mac
&& (!dev_mac
|| !set_contains(match_mac
, dev_mac
)))
128 if (!net_condition_test_strv(match_paths
, dev_path
))
131 if (!net_condition_test_strv(match_drivers
, dev_driver
))
134 if (!net_condition_test_strv(match_types
, dev_type
))
137 if (!net_condition_test_strv(match_names
, dev_name
))
143 int config_parse_net_condition(const char *unit
,
144 const char *filename
,
147 unsigned section_line
,
154 ConditionType cond
= ltype
;
155 Condition
**ret
= data
;
158 _cleanup_free_
char *s
= NULL
;
165 negate
= rvalue
[0] == '!';
173 c
= condition_new(cond
, s
, false, negate
);
178 condition_free(*ret
);
184 int config_parse_ifnames(
186 const char *filename
,
189 unsigned section_line
,
205 _cleanup_free_
char *word
= NULL
;
207 r
= extract_first_word(&rvalue
, &word
, NULL
, 0);
209 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse interface name list: %s", rvalue
);
215 if (!ifname_valid(word
)) {
216 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Interface name is not valid or too long, ignoring assignment: %s", rvalue
);
220 r
= strv_push(sv
, word
);
230 int config_parse_ifalias(const char *unit
,
231 const char *filename
,
234 unsigned section_line
,
242 _cleanup_free_
char *n
= NULL
;
253 if (!ascii_is_valid(n
) || strlen(n
) >= IFALIASZ
) {
254 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Interface alias is not ASCII clean or is too long, ignoring assignment: %s", rvalue
);
261 free_and_replace(*s
, n
);
266 int config_parse_hwaddr(const char *unit
,
267 const char *filename
,
270 unsigned section_line
,
277 _cleanup_free_
struct ether_addr
*n
= NULL
;
278 struct ether_addr
**hwaddr
= data
;
286 n
= new0(struct ether_addr
, 1);
290 r
= ether_addr_from_string(rvalue
, n
);
292 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Not a valid MAC address, ignoring assignment: %s", rvalue
);
296 free_and_replace(*hwaddr
, n
);
301 int config_parse_hwaddrs(const char *unit
,
302 const char *filename
,
305 unsigned section_line
,
312 _cleanup_set_free_free_ Set
*s
= NULL
;
313 const char *p
= rvalue
;
314 Set
**hwaddrs
= data
;
322 if (isempty(rvalue
)) {
323 /* Empty assignment resets the list */
324 *hwaddrs
= set_free_free(*hwaddrs
);
328 s
= set_new(ðer_addr_hash_ops
);
333 _cleanup_free_
char *word
= NULL
;
334 _cleanup_free_
struct ether_addr
*n
= NULL
;
336 r
= extract_first_word(&p
, &word
, NULL
, 0);
342 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Invalid syntax, ignoring: %s", rvalue
);
346 n
= new(struct ether_addr
, 1);
350 r
= ether_addr_from_string(word
, n
);
352 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Not a valid MAC address, ignoring: %s", word
);
360 n
= NULL
; /* avoid cleanup */
363 r
= set_ensure_allocated(hwaddrs
, ðer_addr_hash_ops
);
367 r
= set_move(*hwaddrs
, s
);
374 int config_parse_bridge_port_priority(
376 const char *filename
,
379 unsigned section_line
,
394 r
= safe_atou16(rvalue
, &i
);
396 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
397 "Failed to parse bridge port priority, ignoring: %s", rvalue
);
401 if (i
> LINK_BRIDGE_PORT_PRIORITY_MAX
) {
402 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
403 "Bridge port priority is larger than maximum %u, ignoring: %s", LINK_BRIDGE_PORT_PRIORITY_MAX
, rvalue
);
407 *((uint16_t *)data
) = i
;
412 void serialize_in_addrs(FILE *f
, const struct in_addr
*addresses
, size_t size
) {
419 for (i
= 0; i
< size
; i
++)
420 fprintf(f
, "%s%s", inet_ntoa(addresses
[i
]),
421 (i
< (size
- 1)) ? " ": "");
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
= TAKE_PTR(addresses
);
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 struct in_addr dest
, gw
;
527 assert_se(sd_dhcp_route_get_destination(routes
[i
], &dest
) >= 0);
528 assert_se(sd_dhcp_route_get_gateway(routes
[i
], &gw
) >= 0);
529 assert_se(sd_dhcp_route_get_destination_prefix_length(routes
[i
], &length
) >= 0);
531 fprintf(f
, "%s/%" PRIu8
, inet_ntoa(dest
), length
);
532 fprintf(f
, ",%s%s", inet_ntoa(gw
), (i
< (size
- 1)) ? " ": "");
538 int deserialize_dhcp_routes(struct sd_dhcp_route
**ret
, size_t *ret_size
, size_t *ret_allocated
, const char *string
) {
539 _cleanup_free_
struct sd_dhcp_route
*routes
= NULL
;
540 size_t size
= 0, allocated
= 0;
544 assert(ret_allocated
);
547 /* WORD FORMAT: dst_ip/dst_prefixlen,gw_ip */
549 _cleanup_free_
char *word
= NULL
;
554 r
= extract_first_word(&string
, &word
, NULL
, 0);
560 if (!GREEDY_REALLOC(routes
, allocated
, size
+ 1))
566 tok_end
= strchr(tok
, '/');
571 r
= inet_aton(tok
, &routes
[size
].dst_addr
);
577 /* get the prefixlen */
578 tok_end
= strchr(tok
, ',');
584 r
= safe_atou(tok
, &n
);
588 routes
[size
].dst_prefixlen
= (uint8_t) n
;
591 /* get the gateway */
592 r
= inet_aton(tok
, &routes
[size
].gw_addr
);
600 *ret_allocated
= allocated
;
601 *ret
= TAKE_PTR(routes
);
606 int serialize_dhcp_option(FILE *f
, const char *key
, const void *data
, size_t size
) {
607 _cleanup_free_
char *hex_buf
= NULL
;
613 hex_buf
= hexmem(data
, size
);
617 fprintf(f
, "%s=%s\n", key
, hex_buf
);