1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 #include <netinet/ether.h>
10 #include "alloc-util.h"
11 #include "arphrd-list.h"
12 #include "condition.h"
13 #include "conf-parser.h"
14 #include "device-util.h"
15 #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-table.h"
25 #include "string-util.h"
30 const char *net_get_name_persistent(sd_device
*device
) {
31 const char *name
, *field
;
35 /* fetch some persistent data unique (on this machine) to this device */
36 FOREACH_STRING(field
, "ID_NET_NAME_ONBOARD", "ID_NET_NAME_SLOT", "ID_NET_NAME_PATH", "ID_NET_NAME_MAC")
37 if (sd_device_get_property_value(device
, field
, &name
) >= 0)
43 #define HASH_KEY SD_ID128_MAKE(d3,1e,48,fa,90,fe,4b,4c,9d,af,d5,d7,a1,b1,2e,8a)
45 int net_get_unique_predictable_data(sd_device
*device
, bool use_sysname
, uint64_t *result
) {
53 /* net_get_name_persistent() will return one of the device names based on stable information about
54 * the device. If this is not available, we fall back to using the actual device name. */
55 name
= net_get_name_persistent(device
);
56 if (!name
&& use_sysname
)
57 (void) sd_device_get_sysname(device
, &name
);
59 return log_device_debug_errno(device
, SYNTHETIC_ERRNO(ENODATA
),
60 "No stable identifying information found");
62 log_device_debug(device
, "Using \"%s\" as stable identifying information", name
);
64 sz
= sizeof(sd_id128_t
) + l
;
65 v
= newa(uint8_t, sz
);
67 /* Fetch some persistent data unique to this machine */
68 r
= sd_id128_get_machine((sd_id128_t
*) v
);
71 memcpy(v
+ sizeof(sd_id128_t
), name
, l
);
73 /* Let's hash the machine ID plus the device name. We use
74 * a fixed, but originally randomly created hash key here. */
75 *result
= htole64(siphash24(v
, sz
, HASH_KEY
.bytes
));
79 static bool net_condition_test_strv(char * const *patterns
, const char *string
) {
81 bool match
= false, has_positive_rule
= false;
83 if (strv_isempty(patterns
))
86 STRV_FOREACH(p
, patterns
) {
94 has_positive_rule
= true;
96 if (string
&& fnmatch(q
, string
, 0) == 0) {
104 return has_positive_rule
? match
: true;
107 static bool net_condition_test_ifname(char * const *patterns
, const char *ifname
, char * const *alternative_names
) {
108 if (net_condition_test_strv(patterns
, ifname
))
112 STRV_FOREACH(p
, alternative_names
)
113 if (net_condition_test_strv(patterns
, *p
))
119 static int net_condition_test_property(char * const *match_property
, sd_device
*device
) {
122 if (strv_isempty(match_property
))
125 STRV_FOREACH(p
, match_property
) {
126 _cleanup_free_
char *key
= NULL
;
127 const char *val
, *dev_val
;
132 val
= strchr(*p
+ invert
, '=');
136 key
= strndup(*p
+ invert
, val
- *p
- invert
);
143 sd_device_get_property_value(device
, key
, &dev_val
) >= 0 &&
144 fnmatch(val
, dev_val
, 0) == 0;
153 static const char *const wifi_iftype_table
[NL80211_IFTYPE_MAX
+1] = {
154 [NL80211_IFTYPE_ADHOC
] = "ad-hoc",
155 [NL80211_IFTYPE_STATION
] = "station",
156 [NL80211_IFTYPE_AP
] = "ap",
157 [NL80211_IFTYPE_AP_VLAN
] = "ap-vlan",
158 [NL80211_IFTYPE_WDS
] = "wds",
159 [NL80211_IFTYPE_MONITOR
] = "monitor",
160 [NL80211_IFTYPE_MESH_POINT
] = "mesh-point",
161 [NL80211_IFTYPE_P2P_CLIENT
] = "p2p-client",
162 [NL80211_IFTYPE_P2P_GO
] = "p2p-go",
163 [NL80211_IFTYPE_P2P_DEVICE
] = "p2p-device",
164 [NL80211_IFTYPE_OCB
] = "ocb",
165 [NL80211_IFTYPE_NAN
] = "nan",
168 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(wifi_iftype
, enum nl80211_iftype
);
170 char *link_get_type_string(unsigned short iftype
, sd_device
*device
) {
171 const char *t
, *devtype
;
175 sd_device_get_devtype(device
, &devtype
) >= 0 &&
177 return strdup(devtype
);
179 t
= arphrd_to_name(iftype
);
191 bool net_match_config(Set
*match_mac
,
192 Set
*match_permanent_mac
,
193 char * const *match_paths
,
194 char * const *match_drivers
,
195 char * const *match_iftypes
,
196 char * const *match_names
,
197 char * const *match_property
,
198 char * const *match_wifi_iftype
,
199 char * const *match_ssid
,
202 const struct ether_addr
*dev_mac
,
203 const struct ether_addr
*dev_permanent_mac
,
204 const char *dev_driver
,
205 unsigned short dev_iftype
,
206 const char *dev_name
,
207 char * const *alternative_names
,
208 enum nl80211_iftype dev_wifi_iftype
,
209 const char *dev_ssid
,
210 const struct ether_addr
*dev_bssid
) {
212 _cleanup_free_
char *dev_iftype_str
;
213 const char *dev_path
= NULL
;
215 dev_iftype_str
= link_get_type_string(dev_iftype
, device
);
220 (void) sd_device_get_property_value(device
, "ID_PATH", &dev_path
);
222 (void) sd_device_get_property_value(device
, "ID_NET_DRIVER", &dev_driver
);
224 (void) sd_device_get_sysname(device
, &dev_name
);
226 sd_device_get_sysattr_value(device
, "address", &mac_str
) >= 0)
227 dev_mac
= ether_aton(mac_str
);
230 if (match_mac
&& (!dev_mac
|| !set_contains(match_mac
, dev_mac
)))
233 if (match_permanent_mac
&&
234 (!dev_permanent_mac
||
235 ether_addr_is_null(dev_permanent_mac
) ||
236 !set_contains(match_permanent_mac
, dev_permanent_mac
)))
239 if (!net_condition_test_strv(match_paths
, dev_path
))
242 if (!net_condition_test_strv(match_drivers
, dev_driver
))
245 if (!net_condition_test_strv(match_iftypes
, dev_iftype_str
))
248 if (!net_condition_test_ifname(match_names
, dev_name
, alternative_names
))
251 if (!net_condition_test_property(match_property
, device
))
254 if (!net_condition_test_strv(match_wifi_iftype
, wifi_iftype_to_string(dev_wifi_iftype
)))
257 if (!net_condition_test_strv(match_ssid
, dev_ssid
))
260 if (match_bssid
&& (!dev_bssid
|| !set_contains(match_bssid
, dev_bssid
)))
266 int config_parse_net_condition(const char *unit
,
267 const char *filename
,
270 unsigned section_line
,
277 ConditionType cond
= ltype
;
278 Condition
**list
= data
, *c
;
286 if (isempty(rvalue
)) {
287 *list
= condition_free_list_type(*list
, cond
);
291 negate
= rvalue
[0] == '!';
295 c
= condition_new(cond
, rvalue
, false, negate
);
299 /* Drop previous assignment. */
300 *list
= condition_free_list_type(*list
, cond
);
302 LIST_PREPEND(conditions
, *list
, c
);
306 int config_parse_match_strv(
308 const char *filename
,
311 unsigned section_line
,
318 const char *p
= rvalue
;
328 if (isempty(rvalue
)) {
329 *sv
= strv_free(*sv
);
337 _cleanup_free_
char *word
= NULL
, *k
= NULL
;
339 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_UNQUOTE
|EXTRACT_RETAIN_ESCAPE
);
345 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Invalid syntax, ignoring: %s", rvalue
);
350 k
= strjoin("!", word
);
356 r
= strv_consume(sv
, TAKE_PTR(k
));
362 int config_parse_match_ifnames(
364 const char *filename
,
367 unsigned section_line
,
374 const char *p
= rvalue
;
388 _cleanup_free_
char *word
= NULL
, *k
= NULL
;
390 r
= extract_first_word(&p
, &word
, NULL
, 0);
396 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
397 "Failed to parse interface name list: %s", rvalue
);
401 if (!ifname_valid_full(word
, ltype
)) {
402 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
403 "Interface name is not valid or too long, ignoring assignment: %s", word
);
408 k
= strjoin("!", word
);
414 r
= strv_consume(sv
, TAKE_PTR(k
));
420 int config_parse_match_property(
422 const char *filename
,
425 unsigned section_line
,
432 const char *p
= rvalue
;
446 _cleanup_free_
char *word
= NULL
, *k
= NULL
;
448 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_CUNESCAPE
|EXTRACT_UNQUOTE
);
454 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
455 "Invalid syntax, ignoring: %s", rvalue
);
459 if (!env_assignment_is_valid(word
)) {
460 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
461 "Invalid property or value, ignoring assignment: %s", word
);
466 k
= strjoin("!", word
);
472 r
= strv_consume(sv
, TAKE_PTR(k
));
478 int config_parse_ifalias(const char *unit
,
479 const char *filename
,
482 unsigned section_line
,
490 _cleanup_free_
char *n
= NULL
;
501 if (!ascii_is_valid(n
) || strlen(n
) >= IFALIASZ
) {
502 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Interface alias is not ASCII clean or is too long, ignoring assignment: %s", rvalue
);
509 free_and_replace(*s
, n
);
514 int config_parse_hwaddr(const char *unit
,
515 const char *filename
,
518 unsigned section_line
,
525 _cleanup_free_
struct ether_addr
*n
= NULL
;
526 struct ether_addr
**hwaddr
= data
;
534 n
= new0(struct ether_addr
, 1);
538 r
= ether_addr_from_string(rvalue
, n
);
540 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Not a valid MAC address, ignoring assignment: %s", rvalue
);
544 free_and_replace(*hwaddr
, n
);
549 int config_parse_hwaddrs(const char *unit
,
550 const char *filename
,
553 unsigned section_line
,
560 _cleanup_set_free_free_ Set
*s
= NULL
;
561 const char *p
= rvalue
;
562 Set
**hwaddrs
= data
;
570 if (isempty(rvalue
)) {
571 /* Empty assignment resets the list */
572 *hwaddrs
= set_free_free(*hwaddrs
);
576 s
= set_new(ðer_addr_hash_ops
);
581 _cleanup_free_
char *word
= NULL
;
582 _cleanup_free_
struct ether_addr
*n
= NULL
;
584 r
= extract_first_word(&p
, &word
, NULL
, 0);
590 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Invalid syntax, ignoring: %s", rvalue
);
594 n
= new(struct ether_addr
, 1);
598 r
= ether_addr_from_string(word
, n
);
600 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Not a valid MAC address, ignoring: %s", word
);
608 n
= NULL
; /* avoid cleanup */
611 r
= set_ensure_allocated(hwaddrs
, ðer_addr_hash_ops
);
615 r
= set_move(*hwaddrs
, s
);
622 int config_parse_bridge_port_priority(
624 const char *filename
,
627 unsigned section_line
,
642 r
= safe_atou16(rvalue
, &i
);
644 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
645 "Failed to parse bridge port priority, ignoring: %s", rvalue
);
649 if (i
> LINK_BRIDGE_PORT_PRIORITY_MAX
) {
650 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
651 "Bridge port priority is larger than maximum %u, ignoring: %s", LINK_BRIDGE_PORT_PRIORITY_MAX
, rvalue
);
655 *((uint16_t *)data
) = i
;
660 size_t serialize_in_addrs(FILE *f
,
661 const struct in_addr
*addresses
,
663 bool *with_leading_space
,
664 bool (*predicate
)(const struct in_addr
*addr
)) {
670 if (!with_leading_space
)
671 with_leading_space
= &_space
;
673 for (size_t i
= 0; i
< size
; i
++) {
674 char sbuf
[INET_ADDRSTRLEN
];
676 if (predicate
&& !predicate(&addresses
[i
]))
679 if (*with_leading_space
)
681 fputs(inet_ntop(AF_INET
, &addresses
[i
], sbuf
, sizeof(sbuf
)), f
);
683 *with_leading_space
= true;
689 int deserialize_in_addrs(struct in_addr
**ret
, const char *string
) {
690 _cleanup_free_
struct in_addr
*addresses
= NULL
;
697 _cleanup_free_
char *word
= NULL
;
698 struct in_addr
*new_addresses
;
701 r
= extract_first_word(&string
, &word
, NULL
, 0);
707 new_addresses
= reallocarray(addresses
, size
+ 1, sizeof(struct in_addr
));
711 addresses
= new_addresses
;
713 r
= inet_pton(AF_INET
, word
, &(addresses
[size
]));
720 *ret
= size
> 0 ? TAKE_PTR(addresses
) : NULL
;
725 void serialize_in6_addrs(FILE *f
, const struct in6_addr
*addresses
, size_t size
, bool *with_leading_space
) {
731 if (!with_leading_space
)
732 with_leading_space
= &_space
;
734 for (size_t i
= 0; i
< size
; i
++) {
735 char buffer
[INET6_ADDRSTRLEN
];
737 if (*with_leading_space
)
739 fputs(inet_ntop(AF_INET6
, addresses
+i
, buffer
, sizeof(buffer
)), f
);
740 *with_leading_space
= true;
744 int deserialize_in6_addrs(struct in6_addr
**ret
, const char *string
) {
745 _cleanup_free_
struct in6_addr
*addresses
= NULL
;
752 _cleanup_free_
char *word
= NULL
;
753 struct in6_addr
*new_addresses
;
756 r
= extract_first_word(&string
, &word
, NULL
, 0);
762 new_addresses
= reallocarray(addresses
, size
+ 1, sizeof(struct in6_addr
));
766 addresses
= new_addresses
;
768 r
= inet_pton(AF_INET6
, word
, &(addresses
[size
]));
775 *ret
= TAKE_PTR(addresses
);
780 void serialize_dhcp_routes(FILE *f
, const char *key
, sd_dhcp_route
**routes
, size_t size
) {
786 fprintf(f
, "%s=", key
);
788 for (size_t i
= 0; i
< size
; i
++) {
789 char sbuf
[INET_ADDRSTRLEN
];
790 struct in_addr dest
, gw
;
793 assert_se(sd_dhcp_route_get_destination(routes
[i
], &dest
) >= 0);
794 assert_se(sd_dhcp_route_get_gateway(routes
[i
], &gw
) >= 0);
795 assert_se(sd_dhcp_route_get_destination_prefix_length(routes
[i
], &length
) >= 0);
797 fprintf(f
, "%s/%" PRIu8
, inet_ntop(AF_INET
, &dest
, sbuf
, sizeof sbuf
), length
);
798 fprintf(f
, ",%s%s", inet_ntop(AF_INET
, &gw
, sbuf
, sizeof sbuf
), i
< size
- 1 ? " ": "");
804 int deserialize_dhcp_routes(struct sd_dhcp_route
**ret
, size_t *ret_size
, size_t *ret_allocated
, const char *string
) {
805 _cleanup_free_
struct sd_dhcp_route
*routes
= NULL
;
806 size_t size
= 0, allocated
= 0;
810 assert(ret_allocated
);
813 /* WORD FORMAT: dst_ip/dst_prefixlen,gw_ip */
815 _cleanup_free_
char *word
= NULL
;
820 r
= extract_first_word(&string
, &word
, NULL
, 0);
826 if (!GREEDY_REALLOC(routes
, allocated
, size
+ 1))
832 tok_end
= strchr(tok
, '/');
837 r
= inet_aton(tok
, &routes
[size
].dst_addr
);
843 /* get the prefixlen */
844 tok_end
= strchr(tok
, ',');
850 r
= safe_atou(tok
, &n
);
854 routes
[size
].dst_prefixlen
= (uint8_t) n
;
857 /* get the gateway */
858 r
= inet_aton(tok
, &routes
[size
].gw_addr
);
866 *ret_allocated
= allocated
;
867 *ret
= TAKE_PTR(routes
);
872 int serialize_dhcp_option(FILE *f
, const char *key
, const void *data
, size_t size
) {
873 _cleanup_free_
char *hex_buf
= NULL
;
879 hex_buf
= hexmem(data
, size
);
883 fprintf(f
, "%s=%s\n", key
, hex_buf
);