]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd-network/network-internal.c
udev: introduce NAMING_STABLE_VIRTUAL_MACS (retroactively)
[thirdparty/systemd.git] / src / libsystemd-network / network-internal.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <arpa/inet.h>
4 #include <linux/if.h>
5 #include <netinet/ether.h>
6
7 #include "sd-id128.h"
8 #include "sd-ndisc.h"
9
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"
17 #include "log.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"
23 #include "strv.h"
24 #include "utf8.h"
25 #include "util.h"
26
27 const char *net_get_name_persistent(sd_device *device) {
28 const char *name, *field;
29
30 assert(device);
31
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)
35 return name;
36
37 return NULL;
38 }
39
40 #define HASH_KEY SD_ID128_MAKE(d3,1e,48,fa,90,fe,4b,4c,9d,af,d5,d7,a1,b1,2e,8a)
41
42 int net_get_unique_predictable_data(sd_device *device, bool use_sysname, uint64_t *result) {
43 size_t l, sz = 0;
44 const char *name;
45 int r;
46 uint8_t *v;
47
48 assert(device);
49
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);
55 if (!name)
56 return log_device_debug_errno(device, SYNTHETIC_ERRNO(ENODATA),
57 "No stable identifying information found");
58
59 log_device_debug(device, "Using \"%s\" as stable identifying information", name);
60 l = strlen(name);
61 sz = sizeof(sd_id128_t) + l;
62 v = newa(uint8_t, sz);
63
64 /* Fetch some persistent data unique to this machine */
65 r = sd_id128_get_machine((sd_id128_t*) v);
66 if (r < 0)
67 return r;
68 memcpy(v + sizeof(sd_id128_t), name, l);
69
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));
73 return 0;
74 }
75
76 static bool net_condition_test_strv(char * const *raw_patterns,
77 const char *string) {
78 if (strv_isempty(raw_patterns))
79 return true;
80
81 /* If the patterns begin with "!", edit it out and negate the test. */
82 if (raw_patterns[0][0] == '!') {
83 char **patterns;
84 size_t i, length;
85
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];
91
92 return !string || !strv_fnmatch(patterns, string, 0);
93 }
94
95 return string && strv_fnmatch(raw_patterns, string, 0);
96 }
97
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) {
108
109 if (match_mac && (!dev_mac || !set_contains(match_mac, dev_mac)))
110 return false;
111
112 if (!net_condition_test_strv(match_paths, dev_path))
113 return false;
114
115 if (!net_condition_test_strv(match_drivers, dev_driver))
116 return false;
117
118 if (!net_condition_test_strv(match_types, dev_type))
119 return false;
120
121 if (!net_condition_test_strv(match_names, dev_name))
122 return false;
123
124 return true;
125 }
126
127 int config_parse_net_condition(const char *unit,
128 const char *filename,
129 unsigned line,
130 const char *section,
131 unsigned section_line,
132 const char *lvalue,
133 int ltype,
134 const char *rvalue,
135 void *data,
136 void *userdata) {
137
138 ConditionType cond = ltype;
139 Condition **list = data, *c;
140 bool negate;
141
142 assert(filename);
143 assert(lvalue);
144 assert(rvalue);
145 assert(data);
146
147 if (isempty(rvalue)) {
148 *list = condition_free_list_type(*list, cond);
149 return 0;
150 }
151
152 negate = rvalue[0] == '!';
153 if (negate)
154 rvalue++;
155
156 c = condition_new(cond, rvalue, false, negate);
157 if (!c)
158 return log_oom();
159
160 /* Drop previous assignment. */
161 *list = condition_free_list_type(*list, cond);
162
163 LIST_PREPEND(conditions, *list, c);
164 return 0;
165 }
166
167 int config_parse_ifnames(
168 const char *unit,
169 const char *filename,
170 unsigned line,
171 const char *section,
172 unsigned section_line,
173 const char *lvalue,
174 int ltype,
175 const char *rvalue,
176 void *data,
177 void *userdata) {
178
179 char ***sv = data;
180 int r;
181
182 assert(filename);
183 assert(lvalue);
184 assert(rvalue);
185 assert(data);
186
187 for (;;) {
188 _cleanup_free_ char *word = NULL;
189
190 r = extract_first_word(&rvalue, &word, NULL, 0);
191 if (r < 0) {
192 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse interface name list: %s", rvalue);
193 return 0;
194 }
195 if (r == 0)
196 break;
197
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);
200 return 0;
201 }
202
203 r = strv_push(sv, word);
204 if (r < 0)
205 return log_oom();
206
207 word = NULL;
208 }
209
210 return 0;
211 }
212
213 int config_parse_ifalias(const char *unit,
214 const char *filename,
215 unsigned line,
216 const char *section,
217 unsigned section_line,
218 const char *lvalue,
219 int ltype,
220 const char *rvalue,
221 void *data,
222 void *userdata) {
223
224 char **s = data;
225 _cleanup_free_ char *n = NULL;
226
227 assert(filename);
228 assert(lvalue);
229 assert(rvalue);
230 assert(data);
231
232 n = strdup(rvalue);
233 if (!n)
234 return log_oom();
235
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);
238 return 0;
239 }
240
241 if (isempty(n))
242 *s = mfree(*s);
243 else
244 free_and_replace(*s, n);
245
246 return 0;
247 }
248
249 int config_parse_hwaddr(const char *unit,
250 const char *filename,
251 unsigned line,
252 const char *section,
253 unsigned section_line,
254 const char *lvalue,
255 int ltype,
256 const char *rvalue,
257 void *data,
258 void *userdata) {
259
260 _cleanup_free_ struct ether_addr *n = NULL;
261 struct ether_addr **hwaddr = data;
262 int r;
263
264 assert(filename);
265 assert(lvalue);
266 assert(rvalue);
267 assert(data);
268
269 n = new0(struct ether_addr, 1);
270 if (!n)
271 return log_oom();
272
273 r = ether_addr_from_string(rvalue, n);
274 if (r < 0) {
275 log_syntax(unit, LOG_ERR, filename, line, r, "Not a valid MAC address, ignoring assignment: %s", rvalue);
276 return 0;
277 }
278
279 free_and_replace(*hwaddr, n);
280
281 return 0;
282 }
283
284 int config_parse_hwaddrs(const char *unit,
285 const char *filename,
286 unsigned line,
287 const char *section,
288 unsigned section_line,
289 const char *lvalue,
290 int ltype,
291 const char *rvalue,
292 void *data,
293 void *userdata) {
294
295 _cleanup_set_free_free_ Set *s = NULL;
296 const char *p = rvalue;
297 Set **hwaddrs = data;
298 int r;
299
300 assert(filename);
301 assert(lvalue);
302 assert(rvalue);
303 assert(data);
304
305 if (isempty(rvalue)) {
306 /* Empty assignment resets the list */
307 *hwaddrs = set_free_free(*hwaddrs);
308 return 0;
309 }
310
311 s = set_new(&ether_addr_hash_ops);
312 if (!s)
313 return log_oom();
314
315 for (;;) {
316 _cleanup_free_ char *word = NULL;
317 _cleanup_free_ struct ether_addr *n = NULL;
318
319 r = extract_first_word(&p, &word, NULL, 0);
320 if (r == 0)
321 break;
322 if (r == -ENOMEM)
323 return log_oom();
324 if (r < 0) {
325 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid syntax, ignoring: %s", rvalue);
326 return 0;
327 }
328
329 n = new(struct ether_addr, 1);
330 if (!n)
331 return log_oom();
332
333 r = ether_addr_from_string(word, n);
334 if (r < 0) {
335 log_syntax(unit, LOG_ERR, filename, line, 0, "Not a valid MAC address, ignoring: %s", word);
336 continue;
337 }
338
339 r = set_put(s, n);
340 if (r < 0)
341 return log_oom();
342 if (r > 0)
343 n = NULL; /* avoid cleanup */
344 }
345
346 r = set_ensure_allocated(hwaddrs, &ether_addr_hash_ops);
347 if (r < 0)
348 return log_oom();
349
350 r = set_move(*hwaddrs, s);
351 if (r < 0)
352 return log_oom();
353
354 return 0;
355 }
356
357 int config_parse_bridge_port_priority(
358 const char *unit,
359 const char *filename,
360 unsigned line,
361 const char *section,
362 unsigned section_line,
363 const char *lvalue,
364 int ltype,
365 const char *rvalue,
366 void *data,
367 void *userdata) {
368
369 uint16_t i;
370 int r;
371
372 assert(filename);
373 assert(lvalue);
374 assert(rvalue);
375 assert(data);
376
377 r = safe_atou16(rvalue, &i);
378 if (r < 0) {
379 log_syntax(unit, LOG_ERR, filename, line, r,
380 "Failed to parse bridge port priority, ignoring: %s", rvalue);
381 return 0;
382 }
383
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);
387 return 0;
388 }
389
390 *((uint16_t *)data) = i;
391
392 return 0;
393 }
394
395 size_t serialize_in_addrs(FILE *f,
396 const struct in_addr *addresses,
397 size_t size,
398 bool with_leading_space,
399 bool (*predicate)(const struct in_addr *addr)) {
400 size_t count;
401 size_t i;
402
403 assert(f);
404 assert(addresses);
405
406 count = 0;
407
408 for (i = 0; i < size; i++) {
409 char sbuf[INET_ADDRSTRLEN];
410
411 if (predicate && !predicate(&addresses[i]))
412 continue;
413 if (with_leading_space)
414 fputc(' ', f);
415 else
416 with_leading_space = true;
417 fputs(inet_ntop(AF_INET, &addresses[i], sbuf, sizeof(sbuf)), f);
418 count++;
419 }
420
421 return count;
422 }
423
424 int deserialize_in_addrs(struct in_addr **ret, const char *string) {
425 _cleanup_free_ struct in_addr *addresses = NULL;
426 int size = 0;
427
428 assert(ret);
429 assert(string);
430
431 for (;;) {
432 _cleanup_free_ char *word = NULL;
433 struct in_addr *new_addresses;
434 int r;
435
436 r = extract_first_word(&string, &word, NULL, 0);
437 if (r < 0)
438 return r;
439 if (r == 0)
440 break;
441
442 new_addresses = reallocarray(addresses, size + 1, sizeof(struct in_addr));
443 if (!new_addresses)
444 return -ENOMEM;
445 else
446 addresses = new_addresses;
447
448 r = inet_pton(AF_INET, word, &(addresses[size]));
449 if (r <= 0)
450 continue;
451
452 size++;
453 }
454
455 *ret = size > 0 ? TAKE_PTR(addresses) : NULL;
456
457 return size;
458 }
459
460 void serialize_in6_addrs(FILE *f, const struct in6_addr *addresses, size_t size) {
461 unsigned i;
462
463 assert(f);
464 assert(addresses);
465 assert(size);
466
467 for (i = 0; i < size; i++) {
468 char buffer[INET6_ADDRSTRLEN];
469
470 fputs(inet_ntop(AF_INET6, addresses+i, buffer, sizeof(buffer)), f);
471
472 if (i < size - 1)
473 fputc(' ', f);
474 }
475 }
476
477 int deserialize_in6_addrs(struct in6_addr **ret, const char *string) {
478 _cleanup_free_ struct in6_addr *addresses = NULL;
479 int size = 0;
480
481 assert(ret);
482 assert(string);
483
484 for (;;) {
485 _cleanup_free_ char *word = NULL;
486 struct in6_addr *new_addresses;
487 int r;
488
489 r = extract_first_word(&string, &word, NULL, 0);
490 if (r < 0)
491 return r;
492 if (r == 0)
493 break;
494
495 new_addresses = reallocarray(addresses, size + 1, sizeof(struct in6_addr));
496 if (!new_addresses)
497 return -ENOMEM;
498 else
499 addresses = new_addresses;
500
501 r = inet_pton(AF_INET6, word, &(addresses[size]));
502 if (r <= 0)
503 continue;
504
505 size++;
506 }
507
508 *ret = TAKE_PTR(addresses);
509
510 return size;
511 }
512
513 void serialize_dhcp_routes(FILE *f, const char *key, sd_dhcp_route **routes, size_t size) {
514 unsigned i;
515
516 assert(f);
517 assert(key);
518 assert(routes);
519 assert(size);
520
521 fprintf(f, "%s=", key);
522
523 for (i = 0; i < size; i++) {
524 char sbuf[INET_ADDRSTRLEN];
525 struct in_addr dest, gw;
526 uint8_t length;
527
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);
531
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)) ? " ": "");
534 }
535
536 fputs("\n", f);
537 }
538
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;
542
543 assert(ret);
544 assert(ret_size);
545 assert(ret_allocated);
546 assert(string);
547
548 /* WORD FORMAT: dst_ip/dst_prefixlen,gw_ip */
549 for (;;) {
550 _cleanup_free_ char *word = NULL;
551 char *tok, *tok_end;
552 unsigned n;
553 int r;
554
555 r = extract_first_word(&string, &word, NULL, 0);
556 if (r < 0)
557 return r;
558 if (r == 0)
559 break;
560
561 if (!GREEDY_REALLOC(routes, allocated, size + 1))
562 return -ENOMEM;
563
564 tok = word;
565
566 /* get the subnet */
567 tok_end = strchr(tok, '/');
568 if (!tok_end)
569 continue;
570 *tok_end = '\0';
571
572 r = inet_aton(tok, &routes[size].dst_addr);
573 if (r == 0)
574 continue;
575
576 tok = tok_end + 1;
577
578 /* get the prefixlen */
579 tok_end = strchr(tok, ',');
580 if (!tok_end)
581 continue;
582
583 *tok_end = '\0';
584
585 r = safe_atou(tok, &n);
586 if (r < 0 || n > 32)
587 continue;
588
589 routes[size].dst_prefixlen = (uint8_t) n;
590 tok = tok_end + 1;
591
592 /* get the gateway */
593 r = inet_aton(tok, &routes[size].gw_addr);
594 if (r == 0)
595 continue;
596
597 size++;
598 }
599
600 *ret_size = size;
601 *ret_allocated = allocated;
602 *ret = TAKE_PTR(routes);
603
604 return 0;
605 }
606
607 int serialize_dhcp_option(FILE *f, const char *key, const void *data, size_t size) {
608 _cleanup_free_ char *hex_buf = NULL;
609
610 assert(f);
611 assert(key);
612 assert(data);
613
614 hex_buf = hexmem(data, size);
615 if (!hex_buf)
616 return -ENOMEM;
617
618 fprintf(f, "%s=%s\n", key, hex_buf);
619
620 return 0;
621 }