]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd-network/network-internal.c
921c3a9dd0d2aa651908d7d4ab39cc31ef9bcbf4
[thirdparty/systemd.git] / src / libsystemd-network / network-internal.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright (C) 2013 Tom Gundersen <teg@jklm.no>
4 ***/
5
6 #include <arpa/inet.h>
7 #include <linux/if.h>
8 #include <netinet/ether.h>
9
10 #include "sd-id128.h"
11 #include "sd-ndisc.h"
12
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"
19 #include "log.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"
25 #include "strv.h"
26 #include "utf8.h"
27 #include "util.h"
28
29 const char *net_get_name(struct udev_device *device) {
30 const char *name, *field;
31
32 assert(device);
33
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);
37 if (name)
38 return name;
39 }
40
41 return NULL;
42 }
43
44 #define HASH_KEY SD_ID128_MAKE(d3,1e,48,fa,90,fe,4b,4c,9d,af,d5,d7,a1,b1,2e,8a)
45
46 int net_get_unique_predictable_data(struct udev_device *device, uint64_t *result) {
47 size_t l, sz = 0;
48 const char *name = NULL;
49 int r;
50 uint8_t *v;
51
52 assert(device);
53
54 name = net_get_name(device);
55 if (!name)
56 return -ENOENT;
57
58 l = strlen(name);
59 sz = sizeof(sd_id128_t) + l;
60 v = alloca(sz);
61
62 /* fetch some persistent data unique to this machine */
63 r = sd_id128_get_machine((sd_id128_t*) v);
64 if (r < 0)
65 return r;
66 memcpy(v + sizeof(sd_id128_t), name, l);
67
68 /* Let's hash the machine ID plus the device name. We
69 * use a fixed, but originally randomly created hash
70 * key here. */
71 *result = htole64(siphash24(v, sz, HASH_KEY.bytes));
72
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 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) {
114
115 if (match_host && condition_test(match_host) <= 0)
116 return false;
117
118 if (match_virt && condition_test(match_virt) <= 0)
119 return false;
120
121 if (match_kernel_cmdline && condition_test(match_kernel_cmdline) <= 0)
122 return false;
123
124 if (match_kernel_version && condition_test(match_kernel_version) <= 0)
125 return false;
126
127 if (match_arch && condition_test(match_arch) <= 0)
128 return false;
129
130 if (match_mac && dev_mac && !set_contains(match_mac, dev_mac))
131 return false;
132
133 if (!net_condition_test_strv(match_paths, dev_path))
134 return false;
135
136 if (!net_condition_test_strv(match_drivers, dev_driver))
137 return false;
138
139 if (!net_condition_test_strv(match_types, dev_type))
140 return false;
141
142 if (!net_condition_test_strv(match_names, dev_name))
143 return false;
144
145 return true;
146 }
147
148 int config_parse_net_condition(const char *unit,
149 const char *filename,
150 unsigned line,
151 const char *section,
152 unsigned section_line,
153 const char *lvalue,
154 int ltype,
155 const char *rvalue,
156 void *data,
157 void *userdata) {
158
159 ConditionType cond = ltype;
160 Condition **ret = data;
161 bool negate;
162 Condition *c;
163 _cleanup_free_ char *s = NULL;
164
165 assert(filename);
166 assert(lvalue);
167 assert(rvalue);
168 assert(data);
169
170 negate = rvalue[0] == '!';
171 if (negate)
172 rvalue++;
173
174 s = strdup(rvalue);
175 if (!s)
176 return log_oom();
177
178 c = condition_new(cond, s, false, negate);
179 if (!c)
180 return log_oom();
181
182 if (*ret)
183 condition_free(*ret);
184
185 *ret = c;
186 return 0;
187 }
188
189 int config_parse_ifnames(
190 const char *unit,
191 const char *filename,
192 unsigned line,
193 const char *section,
194 unsigned section_line,
195 const char *lvalue,
196 int ltype,
197 const char *rvalue,
198 void *data,
199 void *userdata) {
200
201 char ***sv = data;
202 int r;
203
204 assert(filename);
205 assert(lvalue);
206 assert(rvalue);
207 assert(data);
208
209 for (;;) {
210 _cleanup_free_ char *word = NULL;
211
212 r = extract_first_word(&rvalue, &word, NULL, 0);
213 if (r < 0) {
214 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse interface name list: %s", rvalue);
215 return 0;
216 }
217 if (r == 0)
218 break;
219
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);
222 return 0;
223 }
224
225 r = strv_push(sv, word);
226 if (r < 0)
227 return log_oom();
228
229 word = NULL;
230 }
231
232 return 0;
233 }
234
235 int config_parse_ifalias(const char *unit,
236 const char *filename,
237 unsigned line,
238 const char *section,
239 unsigned section_line,
240 const char *lvalue,
241 int ltype,
242 const char *rvalue,
243 void *data,
244 void *userdata) {
245
246 char **s = data;
247 _cleanup_free_ char *n = NULL;
248
249 assert(filename);
250 assert(lvalue);
251 assert(rvalue);
252 assert(data);
253
254 n = strdup(rvalue);
255 if (!n)
256 return log_oom();
257
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);
260 return 0;
261 }
262
263 free(*s);
264 if (*n)
265 *s = TAKE_PTR(n);
266 else
267 *s = NULL;
268
269 return 0;
270 }
271
272 int config_parse_hwaddr(const char *unit,
273 const char *filename,
274 unsigned line,
275 const char *section,
276 unsigned section_line,
277 const char *lvalue,
278 int ltype,
279 const char *rvalue,
280 void *data,
281 void *userdata) {
282
283 _cleanup_free_ struct ether_addr *n = NULL;
284 struct ether_addr **hwaddr = data;
285 int r;
286
287 assert(filename);
288 assert(lvalue);
289 assert(rvalue);
290 assert(data);
291
292 n = new0(struct ether_addr, 1);
293 if (!n)
294 return log_oom();
295
296 r = ether_addr_from_string(rvalue, n);
297 if (r < 0) {
298 log_syntax(unit, LOG_ERR, filename, line, r, "Not a valid MAC address, ignoring assignment: %s", rvalue);
299 return 0;
300 }
301
302 *hwaddr = TAKE_PTR(n);
303
304 return 0;
305 }
306
307 int config_parse_hwaddrs(const char *unit,
308 const char *filename,
309 unsigned line,
310 const char *section,
311 unsigned section_line,
312 const char *lvalue,
313 int ltype,
314 const char *rvalue,
315 void *data,
316 void *userdata) {
317
318 _cleanup_set_free_free_ Set *s = NULL;
319 const char *p = rvalue;
320 Set **hwaddrs = data;
321 int r;
322
323 assert(filename);
324 assert(lvalue);
325 assert(rvalue);
326 assert(data);
327
328 if (isempty(rvalue)) {
329 /* Empty assignment resets the list */
330 *hwaddrs = set_free_free(*hwaddrs);
331 return 0;
332 }
333
334 s = set_new(&ether_addr_hash_ops);
335 if (!s)
336 return log_oom();
337
338 for (;;) {
339 _cleanup_free_ char *word = NULL;
340 _cleanup_free_ struct ether_addr *n = NULL;
341
342 r = extract_first_word(&p, &word, NULL, 0);
343 if (r == 0)
344 break;
345 if (r == -ENOMEM)
346 return log_oom();
347 if (r < 0) {
348 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid syntax, ignoring: %s", rvalue);
349 return 0;
350 }
351
352 n = new(struct ether_addr, 1);
353 if (!n)
354 return log_oom();
355
356 r = ether_addr_from_string(word, n);
357 if (r < 0) {
358 log_syntax(unit, LOG_ERR, filename, line, 0, "Not a valid MAC address, ignoring: %s", word);
359 continue;
360 }
361
362 r = set_put(s, n);
363 if (r < 0)
364 return log_oom();
365 if (r > 0)
366 n = NULL; /* avoid cleanup */
367 }
368
369 r = set_ensure_allocated(hwaddrs, &ether_addr_hash_ops);
370 if (r < 0)
371 return log_oom();
372
373 r = set_move(*hwaddrs, s);
374 if (r < 0)
375 return log_oom();
376
377 return 0;
378 }
379
380 int config_parse_iaid(const char *unit,
381 const char *filename,
382 unsigned line,
383 const char *section,
384 unsigned section_line,
385 const char *lvalue,
386 int ltype,
387 const char *rvalue,
388 void *data,
389 void *userdata) {
390 uint32_t iaid;
391 int r;
392
393 assert(filename);
394 assert(lvalue);
395 assert(rvalue);
396 assert(data);
397
398 r = safe_atou32(rvalue, &iaid);
399 if (r < 0) {
400 log_syntax(unit, LOG_ERR, filename, line, r,
401 "Unable to read IAID, ignoring assignment: %s", rvalue);
402 return 0;
403 }
404
405 *((uint32_t *)data) = iaid;
406
407 return 0;
408 }
409
410 int config_parse_bridge_port_priority(
411 const char *unit,
412 const char *filename,
413 unsigned line,
414 const char *section,
415 unsigned section_line,
416 const char *lvalue,
417 int ltype,
418 const char *rvalue,
419 void *data,
420 void *userdata) {
421
422 uint16_t i;
423 int r;
424
425 assert(filename);
426 assert(lvalue);
427 assert(rvalue);
428 assert(data);
429
430 r = safe_atou16(rvalue, &i);
431 if (r < 0) {
432 log_syntax(unit, LOG_ERR, filename, line, r,
433 "Failed to parse bridge port priority, ignoring: %s", rvalue);
434 return 0;
435 }
436
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);
440 return 0;
441 }
442
443 *((uint16_t *)data) = i;
444
445 return 0;
446 }
447
448 void serialize_in_addrs(FILE *f, const struct in_addr *addresses, size_t size) {
449 unsigned i;
450
451 assert(f);
452 assert(addresses);
453 assert(size);
454
455 for (i = 0; i < size; i++)
456 fprintf(f, "%s%s", inet_ntoa(addresses[i]),
457 (i < (size - 1)) ? " ": "");
458 }
459
460 int deserialize_in_addrs(struct in_addr **ret, const char *string) {
461 _cleanup_free_ struct in_addr *addresses = NULL;
462 int size = 0;
463
464 assert(ret);
465 assert(string);
466
467 for (;;) {
468 _cleanup_free_ char *word = NULL;
469 struct in_addr *new_addresses;
470 int r;
471
472 r = extract_first_word(&string, &word, NULL, 0);
473 if (r < 0)
474 return r;
475 if (r == 0)
476 break;
477
478 new_addresses = reallocarray(addresses, size + 1, sizeof(struct in_addr));
479 if (!new_addresses)
480 return -ENOMEM;
481 else
482 addresses = new_addresses;
483
484 r = inet_pton(AF_INET, word, &(addresses[size]));
485 if (r <= 0)
486 continue;
487
488 size++;
489 }
490
491 *ret = TAKE_PTR(addresses);
492
493 return size;
494 }
495
496 void serialize_in6_addrs(FILE *f, const struct in6_addr *addresses, size_t size) {
497 unsigned i;
498
499 assert(f);
500 assert(addresses);
501 assert(size);
502
503 for (i = 0; i < size; i++) {
504 char buffer[INET6_ADDRSTRLEN];
505
506 fputs(inet_ntop(AF_INET6, addresses+i, buffer, sizeof(buffer)), f);
507
508 if (i < size - 1)
509 fputc(' ', f);
510 }
511 }
512
513 int deserialize_in6_addrs(struct in6_addr **ret, const char *string) {
514 _cleanup_free_ struct in6_addr *addresses = NULL;
515 int size = 0;
516
517 assert(ret);
518 assert(string);
519
520 for (;;) {
521 _cleanup_free_ char *word = NULL;
522 struct in6_addr *new_addresses;
523 int r;
524
525 r = extract_first_word(&string, &word, NULL, 0);
526 if (r < 0)
527 return r;
528 if (r == 0)
529 break;
530
531 new_addresses = reallocarray(addresses, size + 1, sizeof(struct in6_addr));
532 if (!new_addresses)
533 return -ENOMEM;
534 else
535 addresses = new_addresses;
536
537 r = inet_pton(AF_INET6, word, &(addresses[size]));
538 if (r <= 0)
539 continue;
540
541 size++;
542 }
543
544 *ret = TAKE_PTR(addresses);
545
546 return size;
547 }
548
549 void serialize_dhcp_routes(FILE *f, const char *key, sd_dhcp_route **routes, size_t size) {
550 unsigned i;
551
552 assert(f);
553 assert(key);
554 assert(routes);
555 assert(size);
556
557 fprintf(f, "%s=", key);
558
559 for (i = 0; i < size; i++) {
560 struct in_addr dest, gw;
561 uint8_t length;
562
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);
566
567 fprintf(f, "%s/%" PRIu8, inet_ntoa(dest), length);
568 fprintf(f, ",%s%s", inet_ntoa(gw), (i < (size - 1)) ? " ": "");
569 }
570
571 fputs("\n", f);
572 }
573
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;
577
578 assert(ret);
579 assert(ret_size);
580 assert(ret_allocated);
581 assert(string);
582
583 /* WORD FORMAT: dst_ip/dst_prefixlen,gw_ip */
584 for (;;) {
585 _cleanup_free_ char *word = NULL;
586 char *tok, *tok_end;
587 unsigned n;
588 int r;
589
590 r = extract_first_word(&string, &word, NULL, 0);
591 if (r < 0)
592 return r;
593 if (r == 0)
594 break;
595
596 if (!GREEDY_REALLOC(routes, allocated, size + 1))
597 return -ENOMEM;
598
599 tok = word;
600
601 /* get the subnet */
602 tok_end = strchr(tok, '/');
603 if (!tok_end)
604 continue;
605 *tok_end = '\0';
606
607 r = inet_aton(tok, &routes[size].dst_addr);
608 if (r == 0)
609 continue;
610
611 tok = tok_end + 1;
612
613 /* get the prefixlen */
614 tok_end = strchr(tok, ',');
615 if (!tok_end)
616 continue;
617
618 *tok_end = '\0';
619
620 r = safe_atou(tok, &n);
621 if (r < 0 || n > 32)
622 continue;
623
624 routes[size].dst_prefixlen = (uint8_t) n;
625 tok = tok_end + 1;
626
627 /* get the gateway */
628 r = inet_aton(tok, &routes[size].gw_addr);
629 if (r == 0)
630 continue;
631
632 size++;
633 }
634
635 *ret_size = size;
636 *ret_allocated = allocated;
637 *ret = TAKE_PTR(routes);
638
639 return 0;
640 }
641
642 int serialize_dhcp_option(FILE *f, const char *key, const void *data, size_t size) {
643 _cleanup_free_ char *hex_buf = NULL;
644
645 assert(f);
646 assert(key);
647 assert(data);
648
649 hex_buf = hexmem(data, size);
650 if (hex_buf == NULL)
651 return -ENOMEM;
652
653 fprintf(f, "%s=%s\n", key, hex_buf);
654
655 return 0;
656 }