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(struct udev_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 name
= udev_device_get_property_value(device
, field
);
41 #define HASH_KEY SD_ID128_MAKE(d3,1e,48,fa,90,fe,4b,4c,9d,af,d5,d7,a1,b1,2e,8a)
43 int net_get_unique_predictable_data(struct udev_device
*device
, uint64_t *result
) {
45 const char *name
= NULL
;
51 name
= net_get_name(device
);
56 sz
= sizeof(sd_id128_t
) + l
;
59 /* fetch some persistent data unique to this machine */
60 r
= sd_id128_get_machine((sd_id128_t
*) v
);
63 memcpy(v
+ sizeof(sd_id128_t
), name
, l
);
65 /* Let's hash the machine ID plus the device name. We
66 * use a fixed, but originally randomly created hash
68 *result
= htole64(siphash24(v
, sz
, HASH_KEY
.bytes
));
73 static bool net_condition_test_strv(char * const *raw_patterns
,
75 if (strv_isempty(raw_patterns
))
78 /* If the patterns begin with "!", edit it out and negate the test. */
79 if (raw_patterns
[0][0] == '!') {
83 length
= strv_length(raw_patterns
) + 1; /* Include the NULL. */
84 patterns
= newa(char*, length
);
85 patterns
[0] = raw_patterns
[0] + 1; /* Skip the "!". */
86 for (i
= 1; i
< length
; i
++)
87 patterns
[i
] = raw_patterns
[i
];
89 return !string
|| !strv_fnmatch(patterns
, string
, 0);
92 return string
&& strv_fnmatch(raw_patterns
, string
, 0);
95 bool net_match_config(Set
*match_mac
,
96 char * const *match_paths
,
97 char * const *match_drivers
,
98 char * const *match_types
,
99 char * const *match_names
,
100 Condition
*match_host
,
101 Condition
*match_virt
,
102 Condition
*match_kernel_cmdline
,
103 Condition
*match_kernel_version
,
104 Condition
*match_arch
,
105 const struct ether_addr
*dev_mac
,
106 const char *dev_path
,
107 const char *dev_parent_driver
,
108 const char *dev_driver
,
109 const char *dev_type
,
110 const char *dev_name
) {
112 if (match_host
&& condition_test(match_host
) <= 0)
115 if (match_virt
&& condition_test(match_virt
) <= 0)
118 if (match_kernel_cmdline
&& condition_test(match_kernel_cmdline
) <= 0)
121 if (match_kernel_version
&& condition_test(match_kernel_version
) <= 0)
124 if (match_arch
&& condition_test(match_arch
) <= 0)
127 if (match_mac
&& (!dev_mac
|| !set_contains(match_mac
, dev_mac
)))
130 if (!net_condition_test_strv(match_paths
, dev_path
))
133 if (!net_condition_test_strv(match_drivers
, dev_driver
))
136 if (!net_condition_test_strv(match_types
, dev_type
))
139 if (!net_condition_test_strv(match_names
, dev_name
))
145 int config_parse_net_condition(const char *unit
,
146 const char *filename
,
149 unsigned section_line
,
156 ConditionType cond
= ltype
;
157 Condition
**ret
= data
;
160 _cleanup_free_
char *s
= NULL
;
167 negate
= rvalue
[0] == '!';
175 c
= condition_new(cond
, s
, false, negate
);
180 condition_free(*ret
);
186 int config_parse_ifnames(
188 const char *filename
,
191 unsigned section_line
,
207 _cleanup_free_
char *word
= NULL
;
209 r
= extract_first_word(&rvalue
, &word
, NULL
, 0);
211 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse interface name list: %s", rvalue
);
217 if (!ifname_valid(word
)) {
218 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Interface name is not valid or too long, ignoring assignment: %s", rvalue
);
222 r
= strv_push(sv
, word
);
232 int config_parse_ifalias(const char *unit
,
233 const char *filename
,
236 unsigned section_line
,
244 _cleanup_free_
char *n
= NULL
;
255 if (!ascii_is_valid(n
) || strlen(n
) >= IFALIASZ
) {
256 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Interface alias is not ASCII clean or is too long, ignoring assignment: %s", rvalue
);
269 int config_parse_hwaddr(const char *unit
,
270 const char *filename
,
273 unsigned section_line
,
280 _cleanup_free_
struct ether_addr
*n
= NULL
;
281 struct ether_addr
**hwaddr
= data
;
289 n
= new0(struct ether_addr
, 1);
293 r
= ether_addr_from_string(rvalue
, n
);
295 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Not a valid MAC address, ignoring assignment: %s", rvalue
);
299 *hwaddr
= TAKE_PTR(n
);
304 int config_parse_hwaddrs(const char *unit
,
305 const char *filename
,
308 unsigned section_line
,
315 _cleanup_set_free_free_ Set
*s
= NULL
;
316 const char *p
= rvalue
;
317 Set
**hwaddrs
= data
;
325 if (isempty(rvalue
)) {
326 /* Empty assignment resets the list */
327 *hwaddrs
= set_free_free(*hwaddrs
);
331 s
= set_new(ðer_addr_hash_ops
);
336 _cleanup_free_
char *word
= NULL
;
337 _cleanup_free_
struct ether_addr
*n
= NULL
;
339 r
= extract_first_word(&p
, &word
, NULL
, 0);
345 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Invalid syntax, ignoring: %s", rvalue
);
349 n
= new(struct ether_addr
, 1);
353 r
= ether_addr_from_string(word
, n
);
355 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Not a valid MAC address, ignoring: %s", word
);
363 n
= NULL
; /* avoid cleanup */
366 r
= set_ensure_allocated(hwaddrs
, ðer_addr_hash_ops
);
370 r
= set_move(*hwaddrs
, s
);
377 int config_parse_iaid(const char *unit
,
378 const char *filename
,
381 unsigned section_line
,
395 r
= safe_atou32(rvalue
, &iaid
);
397 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
398 "Unable to read IAID, ignoring assignment: %s", rvalue
);
402 *((uint32_t *)data
) = iaid
;
407 int config_parse_bridge_port_priority(
409 const char *filename
,
412 unsigned section_line
,
427 r
= safe_atou16(rvalue
, &i
);
429 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
430 "Failed to parse bridge port priority, ignoring: %s", rvalue
);
434 if (i
> LINK_BRIDGE_PORT_PRIORITY_MAX
) {
435 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
436 "Bridge port priority is larger than maximum %u, ignoring: %s", LINK_BRIDGE_PORT_PRIORITY_MAX
, rvalue
);
440 *((uint16_t *)data
) = i
;
445 void serialize_in_addrs(FILE *f
, const struct in_addr
*addresses
, size_t size
) {
452 for (i
= 0; i
< size
; i
++)
453 fprintf(f
, "%s%s", inet_ntoa(addresses
[i
]),
454 (i
< (size
- 1)) ? " ": "");
457 int deserialize_in_addrs(struct in_addr
**ret
, const char *string
) {
458 _cleanup_free_
struct in_addr
*addresses
= NULL
;
465 _cleanup_free_
char *word
= NULL
;
466 struct in_addr
*new_addresses
;
469 r
= extract_first_word(&string
, &word
, NULL
, 0);
475 new_addresses
= reallocarray(addresses
, size
+ 1, sizeof(struct in_addr
));
479 addresses
= new_addresses
;
481 r
= inet_pton(AF_INET
, word
, &(addresses
[size
]));
488 *ret
= TAKE_PTR(addresses
);
493 void serialize_in6_addrs(FILE *f
, const struct in6_addr
*addresses
, size_t size
) {
500 for (i
= 0; i
< size
; i
++) {
501 char buffer
[INET6_ADDRSTRLEN
];
503 fputs(inet_ntop(AF_INET6
, addresses
+i
, buffer
, sizeof(buffer
)), f
);
510 int deserialize_in6_addrs(struct in6_addr
**ret
, const char *string
) {
511 _cleanup_free_
struct in6_addr
*addresses
= NULL
;
518 _cleanup_free_
char *word
= NULL
;
519 struct in6_addr
*new_addresses
;
522 r
= extract_first_word(&string
, &word
, NULL
, 0);
528 new_addresses
= reallocarray(addresses
, size
+ 1, sizeof(struct in6_addr
));
532 addresses
= new_addresses
;
534 r
= inet_pton(AF_INET6
, word
, &(addresses
[size
]));
541 *ret
= TAKE_PTR(addresses
);
546 void serialize_dhcp_routes(FILE *f
, const char *key
, sd_dhcp_route
**routes
, size_t size
) {
554 fprintf(f
, "%s=", key
);
556 for (i
= 0; i
< size
; i
++) {
557 struct in_addr dest
, gw
;
560 assert_se(sd_dhcp_route_get_destination(routes
[i
], &dest
) >= 0);
561 assert_se(sd_dhcp_route_get_gateway(routes
[i
], &gw
) >= 0);
562 assert_se(sd_dhcp_route_get_destination_prefix_length(routes
[i
], &length
) >= 0);
564 fprintf(f
, "%s/%" PRIu8
, inet_ntoa(dest
), length
);
565 fprintf(f
, ",%s%s", inet_ntoa(gw
), (i
< (size
- 1)) ? " ": "");
571 int deserialize_dhcp_routes(struct sd_dhcp_route
**ret
, size_t *ret_size
, size_t *ret_allocated
, const char *string
) {
572 _cleanup_free_
struct sd_dhcp_route
*routes
= NULL
;
573 size_t size
= 0, allocated
= 0;
577 assert(ret_allocated
);
580 /* WORD FORMAT: dst_ip/dst_prefixlen,gw_ip */
582 _cleanup_free_
char *word
= NULL
;
587 r
= extract_first_word(&string
, &word
, NULL
, 0);
593 if (!GREEDY_REALLOC(routes
, allocated
, size
+ 1))
599 tok_end
= strchr(tok
, '/');
604 r
= inet_aton(tok
, &routes
[size
].dst_addr
);
610 /* get the prefixlen */
611 tok_end
= strchr(tok
, ',');
617 r
= safe_atou(tok
, &n
);
621 routes
[size
].dst_prefixlen
= (uint8_t) n
;
624 /* get the gateway */
625 r
= inet_aton(tok
, &routes
[size
].gw_addr
);
633 *ret_allocated
= allocated
;
634 *ret
= TAKE_PTR(routes
);
639 int serialize_dhcp_option(FILE *f
, const char *key
, const void *data
, size_t size
) {
640 _cleanup_free_
char *hex_buf
= NULL
;
646 hex_buf
= hexmem(data
, size
);
650 fprintf(f
, "%s=%s\n", key
, hex_buf
);