]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/libsystemd-network/network-internal.c
network: drop unnecessary strdup()
[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
TG
109 const char *dev_path,
110 const char *dev_driver,
111 const char *dev_type,
32bc8adc 112 const char *dev_name) {
be32eb9b 113
2cb62395 114 if (match_host && condition_test(match_host) <= 0)
7eb08da4 115 return false;
2cc412b5 116
2cb62395 117 if (match_virt && condition_test(match_virt) <= 0)
7eb08da4 118 return false;
2cc412b5 119
5022f08a
LP
120 if (match_kernel_cmdline && condition_test(match_kernel_cmdline) <= 0)
121 return false;
122
123 if (match_kernel_version && condition_test(match_kernel_version) <= 0)
7eb08da4 124 return false;
2cc412b5 125
2cb62395 126 if (match_arch && condition_test(match_arch) <= 0)
7eb08da4 127 return false;
edbb03e9 128
25ea58d3 129 if (match_mac && (!dev_mac || !set_contains(match_mac, dev_mac)))
7eb08da4 130 return false;
be32eb9b 131
618b196e 132 if (!net_condition_test_strv(match_paths, dev_path))
ee5de57b 133 return false;
5256e00e 134
618b196e 135 if (!net_condition_test_strv(match_drivers, dev_driver))
ee5de57b 136 return false;
5256e00e 137
618b196e 138 if (!net_condition_test_strv(match_types, dev_type))
ee5de57b 139 return false;
5256e00e 140
618b196e 141 if (!net_condition_test_strv(match_names, dev_name))
ee5de57b 142 return false;
5256e00e 143
7eb08da4 144 return true;
be32eb9b 145}
5fde13d7 146
2cc412b5
TG
147int config_parse_net_condition(const char *unit,
148 const char *filename,
149 unsigned line,
150 const char *section,
151 unsigned section_line,
152 const char *lvalue,
153 int ltype,
154 const char *rvalue,
155 void *data,
156 void *userdata) {
157
158 ConditionType cond = ltype;
159 Condition **ret = data;
160 bool negate;
161 Condition *c;
2cc412b5
TG
162
163 assert(filename);
164 assert(lvalue);
165 assert(rvalue);
166 assert(data);
167
168 negate = rvalue[0] == '!';
169 if (negate)
170 rvalue++;
171
2bd0da7a 172 c = condition_new(cond, rvalue, false, negate);
2cc412b5
TG
173 if (!c)
174 return log_oom();
175
176 if (*ret)
177 condition_free(*ret);
178
179 *ret = c;
180 return 0;
181}
182
d31645ad
LP
183int config_parse_ifnames(
184 const char *unit,
185 const char *filename,
186 unsigned line,
187 const char *section,
188 unsigned section_line,
189 const char *lvalue,
190 int ltype,
191 const char *rvalue,
192 void *data,
193 void *userdata) {
5256e00e
TG
194
195 char ***sv = data;
5256e00e
TG
196 int r;
197
198 assert(filename);
199 assert(lvalue);
200 assert(rvalue);
201 assert(data);
202
93e28226
SS
203 for (;;) {
204 _cleanup_free_ char *word = NULL;
5256e00e 205
93e28226 206 r = extract_first_word(&rvalue, &word, NULL, 0);
a9dd908d
LP
207 if (r < 0) {
208 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse interface name list: %s", rvalue);
209 return 0;
210 }
93e28226
SS
211 if (r == 0)
212 break;
5256e00e 213
d31645ad
LP
214 if (!ifname_valid(word)) {
215 log_syntax(unit, LOG_ERR, filename, line, 0, "Interface name is not valid or too long, ignoring assignment: %s", rvalue);
5256e00e
TG
216 return 0;
217 }
218
93e28226 219 r = strv_push(sv, word);
5256e00e
TG
220 if (r < 0)
221 return log_oom();
93e28226
SS
222
223 word = NULL;
5256e00e
TG
224 }
225
226 return 0;
227}
228
d2df0d0e
TG
229int config_parse_ifalias(const char *unit,
230 const char *filename,
231 unsigned line,
232 const char *section,
71a61510 233 unsigned section_line,
d2df0d0e
TG
234 const char *lvalue,
235 int ltype,
236 const char *rvalue,
237 void *data,
238 void *userdata) {
239
240 char **s = data;
9c39eb5c 241 _cleanup_free_ char *n = NULL;
d2df0d0e
TG
242
243 assert(filename);
244 assert(lvalue);
245 assert(rvalue);
246 assert(data);
247
248 n = strdup(rvalue);
249 if (!n)
250 return log_oom();
251
252 if (!ascii_is_valid(n) || strlen(n) >= IFALIASZ) {
12ca818f 253 log_syntax(unit, LOG_ERR, filename, line, 0, "Interface alias is not ASCII clean or is too long, ignoring assignment: %s", rvalue);
d2df0d0e
TG
254 return 0;
255 }
256
44386b44
YW
257 if (isempty(n))
258 *s = mfree(*s);
ae2a15bc 259 else
44386b44 260 free_and_replace(*s, n);
d2df0d0e
TG
261
262 return 0;
263}
264
5fde13d7
TG
265int config_parse_hwaddr(const char *unit,
266 const char *filename,
267 unsigned line,
268 const char *section,
71a61510 269 unsigned section_line,
5fde13d7
TG
270 const char *lvalue,
271 int ltype,
272 const char *rvalue,
273 void *data,
274 void *userdata) {
e5c1be89
YW
275
276 _cleanup_free_ struct ether_addr *n = NULL;
5fde13d7 277 struct ether_addr **hwaddr = data;
5fde13d7
TG
278 int r;
279
280 assert(filename);
281 assert(lvalue);
282 assert(rvalue);
283 assert(data);
284
a12fa420 285 n = new0(struct ether_addr, 1);
5fde13d7
TG
286 if (!n)
287 return log_oom();
288
e5c1be89
YW
289 r = ether_addr_from_string(rvalue, n);
290 if (r < 0) {
291 log_syntax(unit, LOG_ERR, filename, line, r, "Not a valid MAC address, ignoring assignment: %s", rvalue);
5fde13d7
TG
292 return 0;
293 }
294
899f0d25 295 free_and_replace(*hwaddr, n);
5fde13d7
TG
296
297 return 0;
298}
f5284182 299
206b63ee
YW
300int config_parse_hwaddrs(const char *unit,
301 const char *filename,
302 unsigned line,
303 const char *section,
304 unsigned section_line,
305 const char *lvalue,
306 int ltype,
307 const char *rvalue,
308 void *data,
309 void *userdata) {
310
311 _cleanup_set_free_free_ Set *s = NULL;
312 const char *p = rvalue;
313 Set **hwaddrs = data;
314 int r;
315
316 assert(filename);
317 assert(lvalue);
318 assert(rvalue);
319 assert(data);
320
321 if (isempty(rvalue)) {
322 /* Empty assignment resets the list */
e90d0374 323 *hwaddrs = set_free_free(*hwaddrs);
206b63ee
YW
324 return 0;
325 }
326
327 s = set_new(&ether_addr_hash_ops);
328 if (!s)
329 return log_oom();
330
331 for (;;) {
332 _cleanup_free_ char *word = NULL;
333 _cleanup_free_ struct ether_addr *n = NULL;
334
335 r = extract_first_word(&p, &word, NULL, 0);
336 if (r == 0)
337 break;
338 if (r == -ENOMEM)
339 return log_oom();
340 if (r < 0) {
341 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid syntax, ignoring: %s", rvalue);
342 return 0;
343 }
344
e90d0374 345 n = new(struct ether_addr, 1);
206b63ee
YW
346 if (!n)
347 return log_oom();
348
349 r = ether_addr_from_string(word, n);
350 if (r < 0) {
351 log_syntax(unit, LOG_ERR, filename, line, 0, "Not a valid MAC address, ignoring: %s", word);
352 continue;
353 }
354
355 r = set_put(s, n);
356 if (r < 0)
357 return log_oom();
358 if (r > 0)
359 n = NULL; /* avoid cleanup */
360 }
361
362 r = set_ensure_allocated(hwaddrs, &ether_addr_hash_ops);
363 if (r < 0)
364 return log_oom();
365
366 r = set_move(*hwaddrs, s);
367 if (r < 0)
368 return log_oom();
369
370 return 0;
371}
372
f00ff0de
DJL
373int config_parse_bridge_port_priority(
374 const char *unit,
375 const char *filename,
376 unsigned line,
377 const char *section,
378 unsigned section_line,
379 const char *lvalue,
380 int ltype,
381 const char *rvalue,
382 void *data,
383 void *userdata) {
384
385 uint16_t i;
386 int r;
387
388 assert(filename);
389 assert(lvalue);
390 assert(rvalue);
391 assert(data);
392
393 r = safe_atou16(rvalue, &i);
394 if (r < 0) {
395 log_syntax(unit, LOG_ERR, filename, line, r,
396 "Failed to parse bridge port priority, ignoring: %s", rvalue);
397 return 0;
398 }
399
400 if (i > LINK_BRIDGE_PORT_PRIORITY_MAX) {
401 log_syntax(unit, LOG_ERR, filename, line, r,
402 "Bridge port priority is larger than maximum %u, ignoring: %s", LINK_BRIDGE_PORT_PRIORITY_MAX, rvalue);
403 return 0;
404 }
405
406 *((uint16_t *)data) = i;
407
408 return 0;
409}
410
072320ea
TH
411size_t serialize_in_addrs(FILE *f,
412 const struct in_addr *addresses,
413 size_t size,
414 bool with_leading_space,
415 bool (*predicate)(const struct in_addr *addr)) {
416 size_t count;
417 size_t i;
09bee74d
TG
418
419 assert(f);
09bee74d 420 assert(addresses);
09bee74d 421
072320ea
TH
422 count = 0;
423
424 for (i = 0; i < size; i++) {
189255d2
TH
425 char sbuf[INET_ADDRSTRLEN];
426
072320ea
TH
427 if (predicate && !predicate(&addresses[i]))
428 continue;
429 if (with_leading_space)
430 fputc(' ', f);
431 else
432 with_leading_space = true;
189255d2 433 fputs(inet_ntop(AF_INET, &addresses[i], sbuf, sizeof(sbuf)), f);
072320ea
TH
434 count++;
435 }
436
437 return count;
09bee74d
TG
438}
439
a2ba62c7 440int deserialize_in_addrs(struct in_addr **ret, const char *string) {
09bee74d 441 _cleanup_free_ struct in_addr *addresses = NULL;
a2ba62c7 442 int size = 0;
09bee74d
TG
443
444 assert(ret);
09bee74d
TG
445 assert(string);
446
93e28226
SS
447 for (;;) {
448 _cleanup_free_ char *word = NULL;
09bee74d
TG
449 struct in_addr *new_addresses;
450 int r;
451
93e28226
SS
452 r = extract_first_word(&string, &word, NULL, 0);
453 if (r < 0)
454 return r;
455 if (r == 0)
456 break;
457
62d74c78 458 new_addresses = reallocarray(addresses, size + 1, sizeof(struct in_addr));
09bee74d
TG
459 if (!new_addresses)
460 return -ENOMEM;
461 else
462 addresses = new_addresses;
463
93e28226 464 r = inet_pton(AF_INET, word, &(addresses[size]));
09bee74d
TG
465 if (r <= 0)
466 continue;
467
313cefa1 468 size++;
09bee74d
TG
469 }
470
c24b6821 471 *ret = size > 0 ? TAKE_PTR(addresses) : NULL;
09bee74d 472
a2ba62c7 473 return size;
09bee74d
TG
474}
475
1f152e4b 476void serialize_in6_addrs(FILE *f, const struct in6_addr *addresses, size_t size) {
b729fa14
PF
477 unsigned i;
478
479 assert(f);
480 assert(addresses);
481 assert(size);
482
1f152e4b
LP
483 for (i = 0; i < size; i++) {
484 char buffer[INET6_ADDRSTRLEN];
485
486 fputs(inet_ntop(AF_INET6, addresses+i, buffer, sizeof(buffer)), f);
487
488 if (i < size - 1)
489 fputc(' ', f);
490 }
b729fa14
PF
491}
492
a2ba62c7 493int deserialize_in6_addrs(struct in6_addr **ret, const char *string) {
09bee74d 494 _cleanup_free_ struct in6_addr *addresses = NULL;
a2ba62c7 495 int size = 0;
09bee74d
TG
496
497 assert(ret);
09bee74d
TG
498 assert(string);
499
93e28226
SS
500 for (;;) {
501 _cleanup_free_ char *word = NULL;
09bee74d
TG
502 struct in6_addr *new_addresses;
503 int r;
504
93e28226
SS
505 r = extract_first_word(&string, &word, NULL, 0);
506 if (r < 0)
507 return r;
508 if (r == 0)
509 break;
510
62d74c78 511 new_addresses = reallocarray(addresses, size + 1, sizeof(struct in6_addr));
09bee74d
TG
512 if (!new_addresses)
513 return -ENOMEM;
514 else
515 addresses = new_addresses;
516
93e28226 517 r = inet_pton(AF_INET6, word, &(addresses[size]));
09bee74d
TG
518 if (r <= 0)
519 continue;
520
521 size++;
522 }
523
ae2a15bc 524 *ret = TAKE_PTR(addresses);
09bee74d 525
a2ba62c7 526 return size;
09bee74d 527}
e1ea665e 528
f8693fc7 529void serialize_dhcp_routes(FILE *f, const char *key, sd_dhcp_route **routes, size_t size) {
e1ea665e
EY
530 unsigned i;
531
532 assert(f);
533 assert(key);
534 assert(routes);
535 assert(size);
536
537 fprintf(f, "%s=", key);
538
fbf7dcb5 539 for (i = 0; i < size; i++) {
189255d2 540 char sbuf[INET_ADDRSTRLEN];
f8693fc7
BG
541 struct in_addr dest, gw;
542 uint8_t length;
543
544 assert_se(sd_dhcp_route_get_destination(routes[i], &dest) >= 0);
545 assert_se(sd_dhcp_route_get_gateway(routes[i], &gw) >= 0);
546 assert_se(sd_dhcp_route_get_destination_prefix_length(routes[i], &length) >= 0);
547
189255d2
TH
548 fprintf(f, "%s/%" PRIu8, inet_ntop(AF_INET, &dest, sbuf, sizeof(sbuf)), length);
549 fprintf(f, ",%s%s", inet_ntop(AF_INET, &gw, sbuf, sizeof(sbuf)), (i < (size - 1)) ? " ": "");
fbf7dcb5 550 }
e1ea665e
EY
551
552 fputs("\n", f);
553}
554
555int deserialize_dhcp_routes(struct sd_dhcp_route **ret, size_t *ret_size, size_t *ret_allocated, const char *string) {
556 _cleanup_free_ struct sd_dhcp_route *routes = NULL;
557 size_t size = 0, allocated = 0;
e1ea665e
EY
558
559 assert(ret);
560 assert(ret_size);
561 assert(ret_allocated);
562 assert(string);
563
93e28226
SS
564 /* WORD FORMAT: dst_ip/dst_prefixlen,gw_ip */
565 for (;;) {
566 _cleanup_free_ char *word = NULL;
e1ea665e
EY
567 char *tok, *tok_end;
568 unsigned n;
569 int r;
570
93e28226
SS
571 r = extract_first_word(&string, &word, NULL, 0);
572 if (r < 0)
573 return r;
574 if (r == 0)
575 break;
e1ea665e 576
93e28226 577 if (!GREEDY_REALLOC(routes, allocated, size + 1))
31db0120 578 return -ENOMEM;
e1ea665e 579
93e28226 580 tok = word;
e1ea665e
EY
581
582 /* get the subnet */
583 tok_end = strchr(tok, '/');
584 if (!tok_end)
585 continue;
586 *tok_end = '\0';
587
588 r = inet_aton(tok, &routes[size].dst_addr);
589 if (r == 0)
590 continue;
591
592 tok = tok_end + 1;
593
594 /* get the prefixlen */
595 tok_end = strchr(tok, ',');
596 if (!tok_end)
597 continue;
598
599 *tok_end = '\0';
600
601 r = safe_atou(tok, &n);
602 if (r < 0 || n > 32)
603 continue;
604
605 routes[size].dst_prefixlen = (uint8_t) n;
606 tok = tok_end + 1;
607
608 /* get the gateway */
609 r = inet_aton(tok, &routes[size].gw_addr);
610 if (r == 0)
611 continue;
612
613 size++;
614 }
615
616 *ret_size = size;
617 *ret_allocated = allocated;
ae2a15bc 618 *ret = TAKE_PTR(routes);
e1ea665e
EY
619
620 return 0;
621}
a073309f 622
e4735228 623int serialize_dhcp_option(FILE *f, const char *key, const void *data, size_t size) {
a073309f
AC
624 _cleanup_free_ char *hex_buf = NULL;
625
626 assert(f);
627 assert(key);
628 assert(data);
629
630 hex_buf = hexmem(data, size);
631 if (hex_buf == NULL)
632 return -ENOMEM;
633
634 fprintf(f, "%s=%s\n", key, hex_buf);
635
636 return 0;
637}