1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright (C) 2013 Tom Gundersen <teg@jklm.no>
8 #include <netinet/ether.h>
13 #include "alloc-util.h"
14 #include "condition.h"
15 #include "conf-parser.h"
16 #include "dhcp-lease-internal.h"
17 #include "ether-addr-util.h"
18 #include "hexdecoct.h"
20 #include "network-internal.h"
21 #include "parse-util.h"
22 #include "siphash24.h"
23 #include "socket-util.h"
24 #include "string-util.h"
29 const char *net_get_name(struct udev_device
*device
) {
30 const char *name
, *field
;
34 /* fetch some persistent data unique (on this machine) to this device */
35 FOREACH_STRING(field
, "ID_NET_NAME_ONBOARD", "ID_NET_NAME_SLOT", "ID_NET_NAME_PATH", "ID_NET_NAME_MAC") {
36 name
= udev_device_get_property_value(device
, field
);
44 #define HASH_KEY SD_ID128_MAKE(d3,1e,48,fa,90,fe,4b,4c,9d,af,d5,d7,a1,b1,2e,8a)
46 int net_get_unique_predictable_data(struct udev_device
*device
, uint64_t *result
) {
48 const char *name
= NULL
;
54 name
= net_get_name(device
);
59 sz
= sizeof(sd_id128_t
) + l
;
62 /* fetch some persistent data unique to this machine */
63 r
= sd_id128_get_machine((sd_id128_t
*) v
);
66 memcpy(v
+ sizeof(sd_id128_t
), name
, l
);
68 /* Let's hash the machine ID plus the device name. We
69 * use a fixed, but originally randomly created hash
71 *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 Condition
*match_host
,
104 Condition
*match_virt
,
105 Condition
*match_kernel_cmdline
,
106 Condition
*match_kernel_version
,
107 Condition
*match_arch
,
108 const struct ether_addr
*dev_mac
,
109 const char *dev_path
,
110 const char *dev_parent_driver
,
111 const char *dev_driver
,
112 const char *dev_type
,
113 const char *dev_name
) {
115 if (match_host
&& condition_test(match_host
) <= 0)
118 if (match_virt
&& condition_test(match_virt
) <= 0)
121 if (match_kernel_cmdline
&& condition_test(match_kernel_cmdline
) <= 0)
124 if (match_kernel_version
&& condition_test(match_kernel_version
) <= 0)
127 if (match_arch
&& condition_test(match_arch
) <= 0)
130 if (match_mac
&& dev_mac
&& !set_contains(match_mac
, dev_mac
))
133 if (!net_condition_test_strv(match_paths
, dev_path
))
136 if (!net_condition_test_strv(match_drivers
, dev_driver
))
139 if (!net_condition_test_strv(match_types
, dev_type
))
142 if (!net_condition_test_strv(match_names
, dev_name
))
148 int config_parse_net_condition(const char *unit
,
149 const char *filename
,
152 unsigned section_line
,
159 ConditionType cond
= ltype
;
160 Condition
**ret
= data
;
163 _cleanup_free_
char *s
= NULL
;
170 negate
= rvalue
[0] == '!';
178 c
= condition_new(cond
, s
, false, negate
);
183 condition_free(*ret
);
189 int config_parse_ifnames(
191 const char *filename
,
194 unsigned section_line
,
210 _cleanup_free_
char *word
= NULL
;
212 r
= extract_first_word(&rvalue
, &word
, NULL
, 0);
214 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse interface name list: %s", rvalue
);
220 if (!ifname_valid(word
)) {
221 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Interface name is not valid or too long, ignoring assignment: %s", rvalue
);
225 r
= strv_push(sv
, word
);
235 int config_parse_ifalias(const char *unit
,
236 const char *filename
,
239 unsigned section_line
,
247 _cleanup_free_
char *n
= NULL
;
258 if (!ascii_is_valid(n
) || strlen(n
) >= IFALIASZ
) {
259 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Interface alias is not ASCII clean or is too long, ignoring assignment: %s", rvalue
);
272 int config_parse_hwaddr(const char *unit
,
273 const char *filename
,
276 unsigned section_line
,
283 _cleanup_free_
struct ether_addr
*n
= NULL
;
284 struct ether_addr
**hwaddr
= data
;
292 n
= new0(struct ether_addr
, 1);
296 r
= ether_addr_from_string(rvalue
, n
);
298 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Not a valid MAC address, ignoring assignment: %s", rvalue
);
302 *hwaddr
= TAKE_PTR(n
);
307 int config_parse_hwaddrs(const char *unit
,
308 const char *filename
,
311 unsigned section_line
,
318 _cleanup_set_free_free_ Set
*s
= NULL
;
319 const char *p
= rvalue
;
320 Set
**hwaddrs
= data
;
328 if (isempty(rvalue
)) {
329 /* Empty assignment resets the list */
330 *hwaddrs
= set_free_free(*hwaddrs
);
334 s
= set_new(ðer_addr_hash_ops
);
339 _cleanup_free_
char *word
= NULL
;
340 _cleanup_free_
struct ether_addr
*n
= NULL
;
342 r
= extract_first_word(&p
, &word
, NULL
, 0);
348 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Invalid syntax, ignoring: %s", rvalue
);
352 n
= new(struct ether_addr
, 1);
356 r
= ether_addr_from_string(word
, n
);
358 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Not a valid MAC address, ignoring: %s", word
);
366 n
= NULL
; /* avoid cleanup */
369 r
= set_ensure_allocated(hwaddrs
, ðer_addr_hash_ops
);
373 r
= set_move(*hwaddrs
, s
);
380 int config_parse_iaid(const char *unit
,
381 const char *filename
,
384 unsigned section_line
,
398 r
= safe_atou32(rvalue
, &iaid
);
400 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
401 "Unable to read IAID, ignoring assignment: %s", rvalue
);
405 *((uint32_t *)data
) = iaid
;
410 int config_parse_bridge_port_priority(
412 const char *filename
,
415 unsigned section_line
,
430 r
= safe_atou16(rvalue
, &i
);
432 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
433 "Failed to parse bridge port priority, ignoring: %s", rvalue
);
437 if (i
> LINK_BRIDGE_PORT_PRIORITY_MAX
) {
438 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
439 "Bridge port priority is larger than maximum %u, ignoring: %s", LINK_BRIDGE_PORT_PRIORITY_MAX
, rvalue
);
443 *((uint16_t *)data
) = i
;
448 void serialize_in_addrs(FILE *f
, const struct in_addr
*addresses
, size_t size
) {
455 for (i
= 0; i
< size
; i
++)
456 fprintf(f
, "%s%s", inet_ntoa(addresses
[i
]),
457 (i
< (size
- 1)) ? " ": "");
460 int deserialize_in_addrs(struct in_addr
**ret
, const char *string
) {
461 _cleanup_free_
struct in_addr
*addresses
= NULL
;
468 _cleanup_free_
char *word
= NULL
;
469 struct in_addr
*new_addresses
;
472 r
= extract_first_word(&string
, &word
, NULL
, 0);
478 new_addresses
= reallocarray(addresses
, size
+ 1, sizeof(struct in_addr
));
482 addresses
= new_addresses
;
484 r
= inet_pton(AF_INET
, word
, &(addresses
[size
]));
491 *ret
= TAKE_PTR(addresses
);
496 void serialize_in6_addrs(FILE *f
, const struct in6_addr
*addresses
, size_t size
) {
503 for (i
= 0; i
< size
; i
++) {
504 char buffer
[INET6_ADDRSTRLEN
];
506 fputs(inet_ntop(AF_INET6
, addresses
+i
, buffer
, sizeof(buffer
)), f
);
513 int deserialize_in6_addrs(struct in6_addr
**ret
, const char *string
) {
514 _cleanup_free_
struct in6_addr
*addresses
= NULL
;
521 _cleanup_free_
char *word
= NULL
;
522 struct in6_addr
*new_addresses
;
525 r
= extract_first_word(&string
, &word
, NULL
, 0);
531 new_addresses
= reallocarray(addresses
, size
+ 1, sizeof(struct in6_addr
));
535 addresses
= new_addresses
;
537 r
= inet_pton(AF_INET6
, word
, &(addresses
[size
]));
544 *ret
= TAKE_PTR(addresses
);
549 void serialize_dhcp_routes(FILE *f
, const char *key
, sd_dhcp_route
**routes
, size_t size
) {
557 fprintf(f
, "%s=", key
);
559 for (i
= 0; i
< size
; i
++) {
560 struct in_addr dest
, gw
;
563 assert_se(sd_dhcp_route_get_destination(routes
[i
], &dest
) >= 0);
564 assert_se(sd_dhcp_route_get_gateway(routes
[i
], &gw
) >= 0);
565 assert_se(sd_dhcp_route_get_destination_prefix_length(routes
[i
], &length
) >= 0);
567 fprintf(f
, "%s/%" PRIu8
, inet_ntoa(dest
), length
);
568 fprintf(f
, ",%s%s", inet_ntoa(gw
), (i
< (size
- 1)) ? " ": "");
574 int deserialize_dhcp_routes(struct sd_dhcp_route
**ret
, size_t *ret_size
, size_t *ret_allocated
, const char *string
) {
575 _cleanup_free_
struct sd_dhcp_route
*routes
= NULL
;
576 size_t size
= 0, allocated
= 0;
580 assert(ret_allocated
);
583 /* WORD FORMAT: dst_ip/dst_prefixlen,gw_ip */
585 _cleanup_free_
char *word
= NULL
;
590 r
= extract_first_word(&string
, &word
, NULL
, 0);
596 if (!GREEDY_REALLOC(routes
, allocated
, size
+ 1))
602 tok_end
= strchr(tok
, '/');
607 r
= inet_aton(tok
, &routes
[size
].dst_addr
);
613 /* get the prefixlen */
614 tok_end
= strchr(tok
, ',');
620 r
= safe_atou(tok
, &n
);
624 routes
[size
].dst_prefixlen
= (uint8_t) n
;
627 /* get the gateway */
628 r
= inet_aton(tok
, &routes
[size
].gw_addr
);
636 *ret_allocated
= allocated
;
637 *ret
= TAKE_PTR(routes
);
642 int serialize_dhcp_option(FILE *f
, const char *key
, const void *data
, size_t size
) {
643 _cleanup_free_
char *hex_buf
= NULL
;
649 hex_buf
= hexmem(data
, size
);
653 fprintf(f
, "%s=%s\n", key
, hex_buf
);