]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/libsystemd-network/network-internal.c
dhcp: move filtering of bogus DNS/NTP addresses out of DHCP client
[thirdparty/systemd.git] / src / libsystemd-network / network-internal.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
5fde13d7 2
f5284182 3#include <arpa/inet.h>
07630cea
LP
4#include <linux/if.h>
5#include <netinet/ether.h>
5fde13d7 6
dccca82b 7#include "sd-id128.h"
07630cea
LP
8#include "sd-ndisc.h"
9
b5efdb8a 10#include "alloc-util.h"
07630cea
LP
11#include "condition.h"
12#include "conf-parser.h"
6d364640 13#include "device-util.h"
e1ea665e 14#include "dhcp-lease-internal.h"
aa31ce18 15#include "ether-addr-util.h"
cf0fbc49 16#include "hexdecoct.h"
be32eb9b 17#include "log.h"
6bedfcbb
LP
18#include "network-internal.h"
19#include "parse-util.h"
07630cea 20#include "siphash24.h"
d31645ad 21#include "socket-util.h"
07630cea
LP
22#include "string-util.h"
23#include "strv.h"
5fde13d7 24#include "utf8.h"
a12fa420 25#include "util.h"
5fde13d7 26
51517f9e 27const char *net_get_name(sd_device *device) {
44e7b949 28 const char *name, *field;
fc541430
TG
29
30 assert(device);
b5db00e5
UTL
31
32 /* fetch some persistent data unique (on this machine) to this device */
51517f9e
YW
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)
44e7b949 35 return name;
b5db00e5 36
44e7b949 37 return NULL;
fc541430
TG
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
51517f9e 42int net_get_unique_predictable_data(sd_device *device, uint64_t *result) {
fc541430 43 size_t l, sz = 0;
6d364640 44 const char *name;
fc541430
TG
45 int r;
46 uint8_t *v;
47
48 assert(device);
49
6d364640
ZJS
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. */
fc541430 52 name = net_get_name(device);
b5db00e5 53 if (!name)
6d364640
ZJS
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");
b5db00e5 58
6d364640 59 log_device_debug(device, "Using \"%s\" as stable identifying information", name);
b5db00e5
UTL
60 l = strlen(name);
61 sz = sizeof(sd_id128_t) + l;
6e9417f5 62 v = newa(uint8_t, sz);
b5db00e5 63
6d364640 64 /* Fetch some persistent data unique to this machine */
b5db00e5
UTL
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
6d364640
ZJS
70 /* Let's hash the machine ID plus the device name. We use
71 * a fixed, but originally randomly created hash key here. */
933f9cae 72 *result = htole64(siphash24(v, sz, HASH_KEY.bytes));
b5db00e5
UTL
73 return 0;
74}
75
1aa68db1
DM
76static bool net_condition_test_strv(char * const *raw_patterns,
77 const char *string) {
618b196e
DM
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;
da6053d0 84 size_t i, length;
618b196e
DM
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
e90d0374 98bool net_match_config(Set *match_mac,
5256e00e
TG
99 char * const *match_paths,
100 char * const *match_drivers,
101 char * const *match_types,
102 char * const *match_names,
2cc412b5
TG
103 Condition *match_host,
104 Condition *match_virt,
5022f08a
LP
105 Condition *match_kernel_cmdline,
106 Condition *match_kernel_version,
edbb03e9 107 Condition *match_arch,
505f8da7 108 const struct ether_addr *dev_mac,
b3e01314 109 const char *dev_path,
bf175aaf 110 const char *dev_parent_driver,
b3e01314
TG
111 const char *dev_driver,
112 const char *dev_type,
32bc8adc 113 const char *dev_name) {
be32eb9b 114
2cb62395 115 if (match_host && condition_test(match_host) <= 0)
7eb08da4 116 return false;
2cc412b5 117
2cb62395 118 if (match_virt && condition_test(match_virt) <= 0)
7eb08da4 119 return false;
2cc412b5 120
5022f08a
LP
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)
7eb08da4 125 return false;
2cc412b5 126
2cb62395 127 if (match_arch && condition_test(match_arch) <= 0)
7eb08da4 128 return false;
edbb03e9 129
25ea58d3 130 if (match_mac && (!dev_mac || !set_contains(match_mac, dev_mac)))
7eb08da4 131 return false;
be32eb9b 132
618b196e 133 if (!net_condition_test_strv(match_paths, dev_path))
ee5de57b 134 return false;
5256e00e 135
618b196e 136 if (!net_condition_test_strv(match_drivers, dev_driver))
ee5de57b 137 return false;
5256e00e 138
618b196e 139 if (!net_condition_test_strv(match_types, dev_type))
ee5de57b 140 return false;
5256e00e 141
618b196e 142 if (!net_condition_test_strv(match_names, dev_name))
ee5de57b 143 return false;
5256e00e 144
7eb08da4 145 return true;
be32eb9b 146}
5fde13d7 147
2cc412b5
TG
148int 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
d31645ad
LP
189int 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) {
5256e00e
TG
200
201 char ***sv = data;
5256e00e
TG
202 int r;
203
204 assert(filename);
205 assert(lvalue);
206 assert(rvalue);
207 assert(data);
208
93e28226
SS
209 for (;;) {
210 _cleanup_free_ char *word = NULL;
5256e00e 211
93e28226 212 r = extract_first_word(&rvalue, &word, NULL, 0);
a9dd908d
LP
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 }
93e28226
SS
217 if (r == 0)
218 break;
5256e00e 219
d31645ad
LP
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);
5256e00e
TG
222 return 0;
223 }
224
93e28226 225 r = strv_push(sv, word);
5256e00e
TG
226 if (r < 0)
227 return log_oom();
93e28226
SS
228
229 word = NULL;
5256e00e
TG
230 }
231
232 return 0;
233}
234
d2df0d0e
TG
235int config_parse_ifalias(const char *unit,
236 const char *filename,
237 unsigned line,
238 const char *section,
71a61510 239 unsigned section_line,
d2df0d0e
TG
240 const char *lvalue,
241 int ltype,
242 const char *rvalue,
243 void *data,
244 void *userdata) {
245
246 char **s = data;
9c39eb5c 247 _cleanup_free_ char *n = NULL;
d2df0d0e
TG
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) {
12ca818f 259 log_syntax(unit, LOG_ERR, filename, line, 0, "Interface alias is not ASCII clean or is too long, ignoring assignment: %s", rvalue);
d2df0d0e
TG
260 return 0;
261 }
262
44386b44
YW
263 if (isempty(n))
264 *s = mfree(*s);
ae2a15bc 265 else
44386b44 266 free_and_replace(*s, n);
d2df0d0e
TG
267
268 return 0;
269}
270
5fde13d7
TG
271int config_parse_hwaddr(const char *unit,
272 const char *filename,
273 unsigned line,
274 const char *section,
71a61510 275 unsigned section_line,
5fde13d7
TG
276 const char *lvalue,
277 int ltype,
278 const char *rvalue,
279 void *data,
280 void *userdata) {
e5c1be89
YW
281
282 _cleanup_free_ struct ether_addr *n = NULL;
5fde13d7 283 struct ether_addr **hwaddr = data;
5fde13d7
TG
284 int r;
285
286 assert(filename);
287 assert(lvalue);
288 assert(rvalue);
289 assert(data);
290
a12fa420 291 n = new0(struct ether_addr, 1);
5fde13d7
TG
292 if (!n)
293 return log_oom();
294
e5c1be89
YW
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);
5fde13d7
TG
298 return 0;
299 }
300
899f0d25 301 free_and_replace(*hwaddr, n);
5fde13d7
TG
302
303 return 0;
304}
f5284182 305
206b63ee
YW
306int 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 */
e90d0374 329 *hwaddrs = set_free_free(*hwaddrs);
206b63ee
YW
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
e90d0374 351 n = new(struct ether_addr, 1);
206b63ee
YW
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
f00ff0de
DJL
379int 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
072320ea
TH
417size_t serialize_in_addrs(FILE *f,
418 const struct in_addr *addresses,
419 size_t size,
420 bool with_leading_space,
421 bool (*predicate)(const struct in_addr *addr)) {
422 size_t count;
423 size_t i;
09bee74d
TG
424
425 assert(f);
09bee74d 426 assert(addresses);
09bee74d 427
072320ea
TH
428 count = 0;
429
430 for (i = 0; i < size; i++) {
431 if (predicate && !predicate(&addresses[i]))
432 continue;
433 if (with_leading_space)
434 fputc(' ', f);
435 else
436 with_leading_space = true;
437 fputs(inet_ntoa(addresses[i]), f);
438 count++;
439 }
440
441 return count;
09bee74d
TG
442}
443
a2ba62c7 444int deserialize_in_addrs(struct in_addr **ret, const char *string) {
09bee74d 445 _cleanup_free_ struct in_addr *addresses = NULL;
a2ba62c7 446 int size = 0;
09bee74d
TG
447
448 assert(ret);
09bee74d
TG
449 assert(string);
450
93e28226
SS
451 for (;;) {
452 _cleanup_free_ char *word = NULL;
09bee74d
TG
453 struct in_addr *new_addresses;
454 int r;
455
93e28226
SS
456 r = extract_first_word(&string, &word, NULL, 0);
457 if (r < 0)
458 return r;
459 if (r == 0)
460 break;
461
62d74c78 462 new_addresses = reallocarray(addresses, size + 1, sizeof(struct in_addr));
09bee74d
TG
463 if (!new_addresses)
464 return -ENOMEM;
465 else
466 addresses = new_addresses;
467
93e28226 468 r = inet_pton(AF_INET, word, &(addresses[size]));
09bee74d
TG
469 if (r <= 0)
470 continue;
471
313cefa1 472 size++;
09bee74d
TG
473 }
474
c24b6821 475 *ret = size > 0 ? TAKE_PTR(addresses) : NULL;
09bee74d 476
a2ba62c7 477 return size;
09bee74d
TG
478}
479
1f152e4b 480void serialize_in6_addrs(FILE *f, const struct in6_addr *addresses, size_t size) {
b729fa14
PF
481 unsigned i;
482
483 assert(f);
484 assert(addresses);
485 assert(size);
486
1f152e4b
LP
487 for (i = 0; i < size; i++) {
488 char buffer[INET6_ADDRSTRLEN];
489
490 fputs(inet_ntop(AF_INET6, addresses+i, buffer, sizeof(buffer)), f);
491
492 if (i < size - 1)
493 fputc(' ', f);
494 }
b729fa14
PF
495}
496
a2ba62c7 497int deserialize_in6_addrs(struct in6_addr **ret, const char *string) {
09bee74d 498 _cleanup_free_ struct in6_addr *addresses = NULL;
a2ba62c7 499 int size = 0;
09bee74d
TG
500
501 assert(ret);
09bee74d
TG
502 assert(string);
503
93e28226
SS
504 for (;;) {
505 _cleanup_free_ char *word = NULL;
09bee74d
TG
506 struct in6_addr *new_addresses;
507 int r;
508
93e28226
SS
509 r = extract_first_word(&string, &word, NULL, 0);
510 if (r < 0)
511 return r;
512 if (r == 0)
513 break;
514
62d74c78 515 new_addresses = reallocarray(addresses, size + 1, sizeof(struct in6_addr));
09bee74d
TG
516 if (!new_addresses)
517 return -ENOMEM;
518 else
519 addresses = new_addresses;
520
93e28226 521 r = inet_pton(AF_INET6, word, &(addresses[size]));
09bee74d
TG
522 if (r <= 0)
523 continue;
524
525 size++;
526 }
527
ae2a15bc 528 *ret = TAKE_PTR(addresses);
09bee74d 529
a2ba62c7 530 return size;
09bee74d 531}
e1ea665e 532
f8693fc7 533void serialize_dhcp_routes(FILE *f, const char *key, sd_dhcp_route **routes, size_t size) {
e1ea665e
EY
534 unsigned i;
535
536 assert(f);
537 assert(key);
538 assert(routes);
539 assert(size);
540
541 fprintf(f, "%s=", key);
542
fbf7dcb5 543 for (i = 0; i < size; i++) {
f8693fc7
BG
544 struct in_addr dest, gw;
545 uint8_t length;
546
547 assert_se(sd_dhcp_route_get_destination(routes[i], &dest) >= 0);
548 assert_se(sd_dhcp_route_get_gateway(routes[i], &gw) >= 0);
549 assert_se(sd_dhcp_route_get_destination_prefix_length(routes[i], &length) >= 0);
550
551 fprintf(f, "%s/%" PRIu8, inet_ntoa(dest), length);
552 fprintf(f, ",%s%s", inet_ntoa(gw), (i < (size - 1)) ? " ": "");
fbf7dcb5 553 }
e1ea665e
EY
554
555 fputs("\n", f);
556}
557
558int deserialize_dhcp_routes(struct sd_dhcp_route **ret, size_t *ret_size, size_t *ret_allocated, const char *string) {
559 _cleanup_free_ struct sd_dhcp_route *routes = NULL;
560 size_t size = 0, allocated = 0;
e1ea665e
EY
561
562 assert(ret);
563 assert(ret_size);
564 assert(ret_allocated);
565 assert(string);
566
93e28226
SS
567 /* WORD FORMAT: dst_ip/dst_prefixlen,gw_ip */
568 for (;;) {
569 _cleanup_free_ char *word = NULL;
e1ea665e
EY
570 char *tok, *tok_end;
571 unsigned n;
572 int r;
573
93e28226
SS
574 r = extract_first_word(&string, &word, NULL, 0);
575 if (r < 0)
576 return r;
577 if (r == 0)
578 break;
e1ea665e 579
93e28226 580 if (!GREEDY_REALLOC(routes, allocated, size + 1))
31db0120 581 return -ENOMEM;
e1ea665e 582
93e28226 583 tok = word;
e1ea665e
EY
584
585 /* get the subnet */
586 tok_end = strchr(tok, '/');
587 if (!tok_end)
588 continue;
589 *tok_end = '\0';
590
591 r = inet_aton(tok, &routes[size].dst_addr);
592 if (r == 0)
593 continue;
594
595 tok = tok_end + 1;
596
597 /* get the prefixlen */
598 tok_end = strchr(tok, ',');
599 if (!tok_end)
600 continue;
601
602 *tok_end = '\0';
603
604 r = safe_atou(tok, &n);
605 if (r < 0 || n > 32)
606 continue;
607
608 routes[size].dst_prefixlen = (uint8_t) n;
609 tok = tok_end + 1;
610
611 /* get the gateway */
612 r = inet_aton(tok, &routes[size].gw_addr);
613 if (r == 0)
614 continue;
615
616 size++;
617 }
618
619 *ret_size = size;
620 *ret_allocated = allocated;
ae2a15bc 621 *ret = TAKE_PTR(routes);
e1ea665e
EY
622
623 return 0;
624}
a073309f 625
e4735228 626int serialize_dhcp_option(FILE *f, const char *key, const void *data, size_t size) {
a073309f
AC
627 _cleanup_free_ char *hex_buf = NULL;
628
629 assert(f);
630 assert(key);
631 assert(data);
632
633 hex_buf = hexmem(data, size);
634 if (hex_buf == NULL)
635 return -ENOMEM;
636
637 fprintf(f, "%s=%s\n", key, hex_buf);
638
639 return 0;
640}