]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd-network/network-internal.c
Merge pull request #11226 from keszybz/enable-remount-fs-dynamically
[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(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, 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() will return one of the device names based on stable information about the
51 * device. If this is not available, we fall back to using the device name. */
52 name = net_get_name(device);
53 if (!name)
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 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 if (isempty(n))
264 *s = mfree(*s);
265 else
266 free_and_replace(*s, n);
267
268 return 0;
269 }
270
271 int config_parse_hwaddr(const char *unit,
272 const char *filename,
273 unsigned line,
274 const char *section,
275 unsigned section_line,
276 const char *lvalue,
277 int ltype,
278 const char *rvalue,
279 void *data,
280 void *userdata) {
281
282 _cleanup_free_ struct ether_addr *n = NULL;
283 struct ether_addr **hwaddr = data;
284 int r;
285
286 assert(filename);
287 assert(lvalue);
288 assert(rvalue);
289 assert(data);
290
291 n = new0(struct ether_addr, 1);
292 if (!n)
293 return log_oom();
294
295 r = ether_addr_from_string(rvalue, n);
296 if (r < 0) {
297 log_syntax(unit, LOG_ERR, filename, line, r, "Not a valid MAC address, ignoring assignment: %s", rvalue);
298 return 0;
299 }
300
301 free_and_replace(*hwaddr, n);
302
303 return 0;
304 }
305
306 int config_parse_hwaddrs(const char *unit,
307 const char *filename,
308 unsigned line,
309 const char *section,
310 unsigned section_line,
311 const char *lvalue,
312 int ltype,
313 const char *rvalue,
314 void *data,
315 void *userdata) {
316
317 _cleanup_set_free_free_ Set *s = NULL;
318 const char *p = rvalue;
319 Set **hwaddrs = data;
320 int r;
321
322 assert(filename);
323 assert(lvalue);
324 assert(rvalue);
325 assert(data);
326
327 if (isempty(rvalue)) {
328 /* Empty assignment resets the list */
329 *hwaddrs = set_free_free(*hwaddrs);
330 return 0;
331 }
332
333 s = set_new(&ether_addr_hash_ops);
334 if (!s)
335 return log_oom();
336
337 for (;;) {
338 _cleanup_free_ char *word = NULL;
339 _cleanup_free_ struct ether_addr *n = NULL;
340
341 r = extract_first_word(&p, &word, NULL, 0);
342 if (r == 0)
343 break;
344 if (r == -ENOMEM)
345 return log_oom();
346 if (r < 0) {
347 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid syntax, ignoring: %s", rvalue);
348 return 0;
349 }
350
351 n = new(struct ether_addr, 1);
352 if (!n)
353 return log_oom();
354
355 r = ether_addr_from_string(word, n);
356 if (r < 0) {
357 log_syntax(unit, LOG_ERR, filename, line, 0, "Not a valid MAC address, ignoring: %s", word);
358 continue;
359 }
360
361 r = set_put(s, n);
362 if (r < 0)
363 return log_oom();
364 if (r > 0)
365 n = NULL; /* avoid cleanup */
366 }
367
368 r = set_ensure_allocated(hwaddrs, &ether_addr_hash_ops);
369 if (r < 0)
370 return log_oom();
371
372 r = set_move(*hwaddrs, s);
373 if (r < 0)
374 return log_oom();
375
376 return 0;
377 }
378
379 int config_parse_bridge_port_priority(
380 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
391 uint16_t i;
392 int r;
393
394 assert(filename);
395 assert(lvalue);
396 assert(rvalue);
397 assert(data);
398
399 r = safe_atou16(rvalue, &i);
400 if (r < 0) {
401 log_syntax(unit, LOG_ERR, filename, line, r,
402 "Failed to parse bridge port priority, ignoring: %s", rvalue);
403 return 0;
404 }
405
406 if (i > LINK_BRIDGE_PORT_PRIORITY_MAX) {
407 log_syntax(unit, LOG_ERR, filename, line, r,
408 "Bridge port priority is larger than maximum %u, ignoring: %s", LINK_BRIDGE_PORT_PRIORITY_MAX, rvalue);
409 return 0;
410 }
411
412 *((uint16_t *)data) = i;
413
414 return 0;
415 }
416
417 void serialize_in_addrs(FILE *f, const struct in_addr *addresses, size_t size) {
418 unsigned i;
419
420 assert(f);
421 assert(addresses);
422 assert(size);
423
424 for (i = 0; i < size; i++)
425 fprintf(f, "%s%s", inet_ntoa(addresses[i]),
426 (i < (size - 1)) ? " ": "");
427 }
428
429 int deserialize_in_addrs(struct in_addr **ret, const char *string) {
430 _cleanup_free_ struct in_addr *addresses = NULL;
431 int size = 0;
432
433 assert(ret);
434 assert(string);
435
436 for (;;) {
437 _cleanup_free_ char *word = NULL;
438 struct in_addr *new_addresses;
439 int r;
440
441 r = extract_first_word(&string, &word, NULL, 0);
442 if (r < 0)
443 return r;
444 if (r == 0)
445 break;
446
447 new_addresses = reallocarray(addresses, size + 1, sizeof(struct in_addr));
448 if (!new_addresses)
449 return -ENOMEM;
450 else
451 addresses = new_addresses;
452
453 r = inet_pton(AF_INET, word, &(addresses[size]));
454 if (r <= 0)
455 continue;
456
457 size++;
458 }
459
460 *ret = TAKE_PTR(addresses);
461
462 return size;
463 }
464
465 void serialize_in6_addrs(FILE *f, const struct in6_addr *addresses, size_t size) {
466 unsigned i;
467
468 assert(f);
469 assert(addresses);
470 assert(size);
471
472 for (i = 0; i < size; i++) {
473 char buffer[INET6_ADDRSTRLEN];
474
475 fputs(inet_ntop(AF_INET6, addresses+i, buffer, sizeof(buffer)), f);
476
477 if (i < size - 1)
478 fputc(' ', f);
479 }
480 }
481
482 int deserialize_in6_addrs(struct in6_addr **ret, const char *string) {
483 _cleanup_free_ struct in6_addr *addresses = NULL;
484 int size = 0;
485
486 assert(ret);
487 assert(string);
488
489 for (;;) {
490 _cleanup_free_ char *word = NULL;
491 struct in6_addr *new_addresses;
492 int r;
493
494 r = extract_first_word(&string, &word, NULL, 0);
495 if (r < 0)
496 return r;
497 if (r == 0)
498 break;
499
500 new_addresses = reallocarray(addresses, size + 1, sizeof(struct in6_addr));
501 if (!new_addresses)
502 return -ENOMEM;
503 else
504 addresses = new_addresses;
505
506 r = inet_pton(AF_INET6, word, &(addresses[size]));
507 if (r <= 0)
508 continue;
509
510 size++;
511 }
512
513 *ret = TAKE_PTR(addresses);
514
515 return size;
516 }
517
518 void serialize_dhcp_routes(FILE *f, const char *key, sd_dhcp_route **routes, size_t size) {
519 unsigned i;
520
521 assert(f);
522 assert(key);
523 assert(routes);
524 assert(size);
525
526 fprintf(f, "%s=", key);
527
528 for (i = 0; i < size; i++) {
529 struct in_addr dest, gw;
530 uint8_t length;
531
532 assert_se(sd_dhcp_route_get_destination(routes[i], &dest) >= 0);
533 assert_se(sd_dhcp_route_get_gateway(routes[i], &gw) >= 0);
534 assert_se(sd_dhcp_route_get_destination_prefix_length(routes[i], &length) >= 0);
535
536 fprintf(f, "%s/%" PRIu8, inet_ntoa(dest), length);
537 fprintf(f, ",%s%s", inet_ntoa(gw), (i < (size - 1)) ? " ": "");
538 }
539
540 fputs("\n", f);
541 }
542
543 int deserialize_dhcp_routes(struct sd_dhcp_route **ret, size_t *ret_size, size_t *ret_allocated, const char *string) {
544 _cleanup_free_ struct sd_dhcp_route *routes = NULL;
545 size_t size = 0, allocated = 0;
546
547 assert(ret);
548 assert(ret_size);
549 assert(ret_allocated);
550 assert(string);
551
552 /* WORD FORMAT: dst_ip/dst_prefixlen,gw_ip */
553 for (;;) {
554 _cleanup_free_ char *word = NULL;
555 char *tok, *tok_end;
556 unsigned n;
557 int r;
558
559 r = extract_first_word(&string, &word, NULL, 0);
560 if (r < 0)
561 return r;
562 if (r == 0)
563 break;
564
565 if (!GREEDY_REALLOC(routes, allocated, size + 1))
566 return -ENOMEM;
567
568 tok = word;
569
570 /* get the subnet */
571 tok_end = strchr(tok, '/');
572 if (!tok_end)
573 continue;
574 *tok_end = '\0';
575
576 r = inet_aton(tok, &routes[size].dst_addr);
577 if (r == 0)
578 continue;
579
580 tok = tok_end + 1;
581
582 /* get the prefixlen */
583 tok_end = strchr(tok, ',');
584 if (!tok_end)
585 continue;
586
587 *tok_end = '\0';
588
589 r = safe_atou(tok, &n);
590 if (r < 0 || n > 32)
591 continue;
592
593 routes[size].dst_prefixlen = (uint8_t) n;
594 tok = tok_end + 1;
595
596 /* get the gateway */
597 r = inet_aton(tok, &routes[size].gw_addr);
598 if (r == 0)
599 continue;
600
601 size++;
602 }
603
604 *ret_size = size;
605 *ret_allocated = allocated;
606 *ret = TAKE_PTR(routes);
607
608 return 0;
609 }
610
611 int serialize_dhcp_option(FILE *f, const char *key, const void *data, size_t size) {
612 _cleanup_free_ char *hex_buf = NULL;
613
614 assert(f);
615 assert(key);
616 assert(data);
617
618 hex_buf = hexmem(data, size);
619 if (hex_buf == NULL)
620 return -ENOMEM;
621
622 fprintf(f, "%s=%s\n", key, hex_buf);
623
624 return 0;
625 }