]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/libsystemd-network/network-internal.c
network: read driver name from ethtool
[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"
ef62949a 11#include "arphrd-list.h"
07630cea
LP
12#include "condition.h"
13#include "conf-parser.h"
6d364640 14#include "device-util.h"
e1ea665e 15#include "dhcp-lease-internal.h"
44005bfb 16#include "env-util.h"
aa31ce18 17#include "ether-addr-util.h"
cf0fbc49 18#include "hexdecoct.h"
be32eb9b 19#include "log.h"
6bedfcbb
LP
20#include "network-internal.h"
21#include "parse-util.h"
07630cea 22#include "siphash24.h"
d31645ad 23#include "socket-util.h"
78404d22 24#include "string-table.h"
07630cea
LP
25#include "string-util.h"
26#include "strv.h"
5fde13d7 27#include "utf8.h"
a12fa420 28#include "util.h"
5fde13d7 29
b889a0de 30const char *net_get_name_persistent(sd_device *device) {
44e7b949 31 const char *name, *field;
fc541430
TG
32
33 assert(device);
b5db00e5
UTL
34
35 /* fetch some persistent data unique (on this machine) to this device */
51517f9e
YW
36 FOREACH_STRING(field, "ID_NET_NAME_ONBOARD", "ID_NET_NAME_SLOT", "ID_NET_NAME_PATH", "ID_NET_NAME_MAC")
37 if (sd_device_get_property_value(device, field, &name) >= 0)
44e7b949 38 return name;
b5db00e5 39
44e7b949 40 return NULL;
fc541430
TG
41}
42
43#define HASH_KEY SD_ID128_MAKE(d3,1e,48,fa,90,fe,4b,4c,9d,af,d5,d7,a1,b1,2e,8a)
44
96848152 45int net_get_unique_predictable_data(sd_device *device, bool use_sysname, uint64_t *result) {
fc541430 46 size_t l, sz = 0;
6d364640 47 const char *name;
fc541430
TG
48 int r;
49 uint8_t *v;
50
51 assert(device);
52
b889a0de
ZJS
53 /* net_get_name_persistent() will return one of the device names based on stable information about
54 * the device. If this is not available, we fall back to using the actual device name. */
55 name = net_get_name_persistent(device);
96848152 56 if (!name && use_sysname)
6d364640
ZJS
57 (void) sd_device_get_sysname(device, &name);
58 if (!name)
59 return log_device_debug_errno(device, SYNTHETIC_ERRNO(ENODATA),
60 "No stable identifying information found");
b5db00e5 61
6d364640 62 log_device_debug(device, "Using \"%s\" as stable identifying information", name);
b5db00e5
UTL
63 l = strlen(name);
64 sz = sizeof(sd_id128_t) + l;
6e9417f5 65 v = newa(uint8_t, sz);
b5db00e5 66
6d364640 67 /* Fetch some persistent data unique to this machine */
b5db00e5
UTL
68 r = sd_id128_get_machine((sd_id128_t*) v);
69 if (r < 0)
70 return r;
71 memcpy(v + sizeof(sd_id128_t), name, l);
72
6d364640
ZJS
73 /* Let's hash the machine ID plus the device name. We use
74 * a fixed, but originally randomly created hash key here. */
933f9cae 75 *result = htole64(siphash24(v, sz, HASH_KEY.bytes));
b5db00e5
UTL
76 return 0;
77}
78
54a84237
YW
79static bool net_condition_test_strv(char * const *patterns, const char *string) {
80 char * const *p;
81 bool match = false, has_positive_rule = false;
82
83 if (strv_isempty(patterns))
618b196e
DM
84 return true;
85
54a84237
YW
86 STRV_FOREACH(p, patterns) {
87 const char *q = *p;
88 bool invert;
89
90 invert = *q == '!';
91 q += invert;
618b196e 92
54a84237
YW
93 if (!invert)
94 has_positive_rule = true;
618b196e 95
54a84237
YW
96 if (string && fnmatch(q, string, 0) == 0) {
97 if (invert)
98 return false;
99 else
100 match = true;
101 }
618b196e
DM
102 }
103
54a84237 104 return has_positive_rule ? match : true;
618b196e
DM
105}
106
572b21d9
YW
107static bool net_condition_test_ifname(char * const *patterns, const char *ifname, char * const *alternative_names) {
108 if (net_condition_test_strv(patterns, ifname))
109 return true;
110
111 char * const *p;
112 STRV_FOREACH(p, alternative_names)
113 if (net_condition_test_strv(patterns, *p))
114 return true;
115
116 return false;
117}
118
44005bfb
YW
119static int net_condition_test_property(char * const *match_property, sd_device *device) {
120 char * const *p;
121
122 if (strv_isempty(match_property))
123 return true;
124
125 STRV_FOREACH(p, match_property) {
126 _cleanup_free_ char *key = NULL;
127 const char *val, *dev_val;
128 bool invert, v;
129
130 invert = **p == '!';
131
132 val = strchr(*p + invert, '=');
133 if (!val)
134 return -EINVAL;
135
136 key = strndup(*p + invert, val - *p - invert);
137 if (!key)
138 return -ENOMEM;
139
140 val++;
141
142 v = device &&
143 sd_device_get_property_value(device, key, &dev_val) >= 0 &&
144 fnmatch(val, dev_val, 0) == 0;
145
146 if (invert ? v : !v)
147 return false;
148 }
149
150 return true;
151}
152
78404d22
YW
153static const char *const wifi_iftype_table[NL80211_IFTYPE_MAX+1] = {
154 [NL80211_IFTYPE_ADHOC] = "ad-hoc",
155 [NL80211_IFTYPE_STATION] = "station",
156 [NL80211_IFTYPE_AP] = "ap",
157 [NL80211_IFTYPE_AP_VLAN] = "ap-vlan",
158 [NL80211_IFTYPE_WDS] = "wds",
159 [NL80211_IFTYPE_MONITOR] = "monitor",
160 [NL80211_IFTYPE_MESH_POINT] = "mesh-point",
161 [NL80211_IFTYPE_P2P_CLIENT] = "p2p-client",
162 [NL80211_IFTYPE_P2P_GO] = "p2p-go",
163 [NL80211_IFTYPE_P2P_DEVICE] = "p2p-device",
164 [NL80211_IFTYPE_OCB] = "ocb",
165 [NL80211_IFTYPE_NAN] = "nan",
166};
167
168DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(wifi_iftype, enum nl80211_iftype);
169
ef62949a
YW
170char *link_get_type_string(unsigned short iftype, sd_device *device) {
171 const char *t, *devtype;
172 char *p;
173
174 if (device &&
175 sd_device_get_devtype(device, &devtype) >= 0 &&
176 !isempty(devtype))
177 return strdup(devtype);
178
179 t = arphrd_to_name(iftype);
180 if (!t)
181 return NULL;
182
183 p = strdup(t);
184 if (!p)
185 return NULL;
186
187 ascii_strlower(p);
188 return p;
189}
190
e90d0374 191bool net_match_config(Set *match_mac,
4bb7cc82 192 Set *match_permanent_mac,
5256e00e
TG
193 char * const *match_paths,
194 char * const *match_drivers,
c643bda5 195 char * const *match_iftypes,
5256e00e 196 char * const *match_names,
44005bfb 197 char * const *match_property,
78404d22 198 char * const *match_wifi_iftype,
8d968fdd 199 char * const *match_ssid,
277ba8d1 200 Set *match_bssid,
b38de0e9 201 sd_device *device,
505f8da7 202 const struct ether_addr *dev_mac,
4bb7cc82 203 const struct ether_addr *dev_permanent_mac,
c643bda5
YW
204 const char *dev_driver,
205 unsigned short dev_iftype,
8d968fdd 206 const char *dev_name,
572b21d9 207 char * const *alternative_names,
c643bda5
YW
208 enum nl80211_iftype dev_wifi_iftype,
209 const char *dev_ssid,
210 const struct ether_addr *dev_bssid) {
be32eb9b 211
c643bda5
YW
212 _cleanup_free_ char *dev_iftype_str;
213 const char *dev_path = NULL;
ef62949a 214
c643bda5 215 dev_iftype_str = link_get_type_string(dev_iftype, device);
b38de0e9
YW
216
217 if (device) {
c643bda5
YW
218 const char *mac_str;
219
b38de0e9 220 (void) sd_device_get_property_value(device, "ID_PATH", &dev_path);
c643bda5
YW
221 if (!dev_driver)
222 (void) sd_device_get_property_value(device, "ID_NET_DRIVER", &dev_driver);
b38de0e9
YW
223 if (!dev_name)
224 (void) sd_device_get_sysname(device, &dev_name);
225 if (!dev_mac &&
226 sd_device_get_sysattr_value(device, "address", &mac_str) >= 0)
227 dev_mac = ether_aton(mac_str);
228 }
229
25ea58d3 230 if (match_mac && (!dev_mac || !set_contains(match_mac, dev_mac)))
7eb08da4 231 return false;
be32eb9b 232
4bb7cc82
YW
233 if (match_permanent_mac &&
234 (!dev_permanent_mac ||
235 ether_addr_is_null(dev_permanent_mac) ||
236 !set_contains(match_permanent_mac, dev_permanent_mac)))
237 return false;
238
618b196e 239 if (!net_condition_test_strv(match_paths, dev_path))
ee5de57b 240 return false;
5256e00e 241
618b196e 242 if (!net_condition_test_strv(match_drivers, dev_driver))
ee5de57b 243 return false;
5256e00e 244
c643bda5 245 if (!net_condition_test_strv(match_iftypes, dev_iftype_str))
ee5de57b 246 return false;
5256e00e 247
572b21d9 248 if (!net_condition_test_ifname(match_names, dev_name, alternative_names))
ee5de57b 249 return false;
5256e00e 250
44005bfb
YW
251 if (!net_condition_test_property(match_property, device))
252 return false;
253
c643bda5 254 if (!net_condition_test_strv(match_wifi_iftype, wifi_iftype_to_string(dev_wifi_iftype)))
78404d22
YW
255 return false;
256
c643bda5 257 if (!net_condition_test_strv(match_ssid, dev_ssid))
8d968fdd
YW
258 return false;
259
c643bda5 260 if (match_bssid && (!dev_bssid || !set_contains(match_bssid, dev_bssid)))
277ba8d1
YW
261 return false;
262
7eb08da4 263 return true;
be32eb9b 264}
5fde13d7 265
2cc412b5
TG
266int config_parse_net_condition(const char *unit,
267 const char *filename,
268 unsigned line,
269 const char *section,
270 unsigned section_line,
271 const char *lvalue,
272 int ltype,
273 const char *rvalue,
274 void *data,
275 void *userdata) {
276
277 ConditionType cond = ltype;
c4f58dea 278 Condition **list = data, *c;
2cc412b5 279 bool negate;
2cc412b5
TG
280
281 assert(filename);
282 assert(lvalue);
283 assert(rvalue);
284 assert(data);
285
c4f58dea
YW
286 if (isempty(rvalue)) {
287 *list = condition_free_list_type(*list, cond);
288 return 0;
289 }
290
2cc412b5
TG
291 negate = rvalue[0] == '!';
292 if (negate)
293 rvalue++;
294
2bd0da7a 295 c = condition_new(cond, rvalue, false, negate);
2cc412b5
TG
296 if (!c)
297 return log_oom();
298
c4f58dea
YW
299 /* Drop previous assignment. */
300 *list = condition_free_list_type(*list, cond);
2cc412b5 301
c4f58dea 302 LIST_PREPEND(conditions, *list, c);
2cc412b5
TG
303 return 0;
304}
305
54a84237 306int config_parse_match_strv(
d31645ad
LP
307 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) {
5256e00e 317
54a84237 318 const char *p = rvalue;
5256e00e 319 char ***sv = data;
54a84237 320 bool invert;
5256e00e
TG
321 int r;
322
323 assert(filename);
324 assert(lvalue);
325 assert(rvalue);
326 assert(data);
327
54a84237
YW
328 if (isempty(rvalue)) {
329 *sv = strv_free(*sv);
330 return 0;
331 }
332
333 invert = *p == '!';
334 p += invert;
335
93e28226 336 for (;;) {
54a84237 337 _cleanup_free_ char *word = NULL, *k = NULL;
5256e00e 338
adfafd88 339 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE|EXTRACT_RETAIN_ESCAPE);
54a84237
YW
340 if (r == 0)
341 return 0;
342 if (r == -ENOMEM)
343 return log_oom();
a9dd908d 344 if (r < 0) {
54a84237 345 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid syntax, ignoring: %s", rvalue);
a9dd908d
LP
346 return 0;
347 }
54a84237
YW
348
349 if (invert) {
350 k = strjoin("!", word);
351 if (!k)
352 return log_oom();
353 } else
354 k = TAKE_PTR(word);
355
356 r = strv_consume(sv, TAKE_PTR(k));
357 if (r < 0)
358 return log_oom();
359 }
360}
361
362int config_parse_match_ifnames(
363 const char *unit,
364 const char *filename,
365 unsigned line,
366 const char *section,
367 unsigned section_line,
368 const char *lvalue,
369 int ltype,
370 const char *rvalue,
371 void *data,
372 void *userdata) {
373
374 const char *p = rvalue;
375 char ***sv = data;
376 bool invert;
377 int r;
378
379 assert(filename);
380 assert(lvalue);
381 assert(rvalue);
382 assert(data);
383
384 invert = *p == '!';
385 p += invert;
386
387 for (;;) {
388 _cleanup_free_ char *word = NULL, *k = NULL;
389
390 r = extract_first_word(&p, &word, NULL, 0);
93e28226 391 if (r == 0)
54a84237
YW
392 return 0;
393 if (r == -ENOMEM)
394 return log_oom();
395 if (r < 0) {
396 log_syntax(unit, LOG_ERR, filename, line, 0,
397 "Failed to parse interface name list: %s", rvalue);
398 return 0;
399 }
5256e00e 400
572b21d9 401 if (!ifname_valid_full(word, ltype)) {
54a84237
YW
402 log_syntax(unit, LOG_ERR, filename, line, 0,
403 "Interface name is not valid or too long, ignoring assignment: %s", word);
404 continue;
5256e00e
TG
405 }
406
54a84237
YW
407 if (invert) {
408 k = strjoin("!", word);
409 if (!k)
410 return log_oom();
411 } else
412 k = TAKE_PTR(word);
413
414 r = strv_consume(sv, TAKE_PTR(k));
5256e00e
TG
415 if (r < 0)
416 return log_oom();
417 }
5256e00e
TG
418}
419
44005bfb
YW
420int config_parse_match_property(
421 const char *unit,
422 const char *filename,
423 unsigned line,
424 const char *section,
425 unsigned section_line,
426 const char *lvalue,
427 int ltype,
428 const char *rvalue,
429 void *data,
430 void *userdata) {
431
432 const char *p = rvalue;
433 char ***sv = data;
434 bool invert;
435 int r;
436
437 assert(filename);
438 assert(lvalue);
439 assert(rvalue);
440 assert(data);
441
442 invert = *p == '!';
443 p += invert;
444
445 for (;;) {
446 _cleanup_free_ char *word = NULL, *k = NULL;
447
448 r = extract_first_word(&p, &word, NULL, EXTRACT_CUNESCAPE|EXTRACT_UNQUOTE);
449 if (r == 0)
450 return 0;
451 if (r == -ENOMEM)
452 return log_oom();
453 if (r < 0) {
454 log_syntax(unit, LOG_ERR, filename, line, 0,
455 "Invalid syntax, ignoring: %s", rvalue);
456 return 0;
457 }
458
459 if (!env_assignment_is_valid(word)) {
460 log_syntax(unit, LOG_ERR, filename, line, 0,
461 "Invalid property or value, ignoring assignment: %s", word);
462 continue;
463 }
464
465 if (invert) {
466 k = strjoin("!", word);
467 if (!k)
468 return log_oom();
469 } else
470 k = TAKE_PTR(word);
471
472 r = strv_consume(sv, TAKE_PTR(k));
473 if (r < 0)
474 return log_oom();
475 }
476}
477
d2df0d0e
TG
478int config_parse_ifalias(const char *unit,
479 const char *filename,
480 unsigned line,
481 const char *section,
71a61510 482 unsigned section_line,
d2df0d0e
TG
483 const char *lvalue,
484 int ltype,
485 const char *rvalue,
486 void *data,
487 void *userdata) {
488
489 char **s = data;
9c39eb5c 490 _cleanup_free_ char *n = NULL;
d2df0d0e
TG
491
492 assert(filename);
493 assert(lvalue);
494 assert(rvalue);
495 assert(data);
496
497 n = strdup(rvalue);
498 if (!n)
499 return log_oom();
500
501 if (!ascii_is_valid(n) || strlen(n) >= IFALIASZ) {
12ca818f 502 log_syntax(unit, LOG_ERR, filename, line, 0, "Interface alias is not ASCII clean or is too long, ignoring assignment: %s", rvalue);
d2df0d0e
TG
503 return 0;
504 }
505
44386b44
YW
506 if (isempty(n))
507 *s = mfree(*s);
ae2a15bc 508 else
44386b44 509 free_and_replace(*s, n);
d2df0d0e
TG
510
511 return 0;
512}
513
5fde13d7
TG
514int config_parse_hwaddr(const char *unit,
515 const char *filename,
516 unsigned line,
517 const char *section,
71a61510 518 unsigned section_line,
5fde13d7
TG
519 const char *lvalue,
520 int ltype,
521 const char *rvalue,
522 void *data,
523 void *userdata) {
e5c1be89
YW
524
525 _cleanup_free_ struct ether_addr *n = NULL;
5fde13d7 526 struct ether_addr **hwaddr = data;
5fde13d7
TG
527 int r;
528
529 assert(filename);
530 assert(lvalue);
531 assert(rvalue);
532 assert(data);
533
a12fa420 534 n = new0(struct ether_addr, 1);
5fde13d7
TG
535 if (!n)
536 return log_oom();
537
e5c1be89
YW
538 r = ether_addr_from_string(rvalue, n);
539 if (r < 0) {
540 log_syntax(unit, LOG_ERR, filename, line, r, "Not a valid MAC address, ignoring assignment: %s", rvalue);
5fde13d7
TG
541 return 0;
542 }
543
899f0d25 544 free_and_replace(*hwaddr, n);
5fde13d7
TG
545
546 return 0;
547}
f5284182 548
206b63ee
YW
549int config_parse_hwaddrs(const char *unit,
550 const char *filename,
551 unsigned line,
552 const char *section,
553 unsigned section_line,
554 const char *lvalue,
555 int ltype,
556 const char *rvalue,
557 void *data,
558 void *userdata) {
559
560 _cleanup_set_free_free_ Set *s = NULL;
561 const char *p = rvalue;
562 Set **hwaddrs = data;
563 int r;
564
565 assert(filename);
566 assert(lvalue);
567 assert(rvalue);
568 assert(data);
569
570 if (isempty(rvalue)) {
571 /* Empty assignment resets the list */
e90d0374 572 *hwaddrs = set_free_free(*hwaddrs);
206b63ee
YW
573 return 0;
574 }
575
576 s = set_new(&ether_addr_hash_ops);
577 if (!s)
578 return log_oom();
579
580 for (;;) {
581 _cleanup_free_ char *word = NULL;
582 _cleanup_free_ struct ether_addr *n = NULL;
583
584 r = extract_first_word(&p, &word, NULL, 0);
585 if (r == 0)
586 break;
587 if (r == -ENOMEM)
588 return log_oom();
589 if (r < 0) {
590 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid syntax, ignoring: %s", rvalue);
591 return 0;
592 }
593
e90d0374 594 n = new(struct ether_addr, 1);
206b63ee
YW
595 if (!n)
596 return log_oom();
597
598 r = ether_addr_from_string(word, n);
599 if (r < 0) {
600 log_syntax(unit, LOG_ERR, filename, line, 0, "Not a valid MAC address, ignoring: %s", word);
601 continue;
602 }
603
604 r = set_put(s, n);
605 if (r < 0)
606 return log_oom();
607 if (r > 0)
608 n = NULL; /* avoid cleanup */
609 }
610
611 r = set_ensure_allocated(hwaddrs, &ether_addr_hash_ops);
612 if (r < 0)
613 return log_oom();
614
615 r = set_move(*hwaddrs, s);
616 if (r < 0)
617 return log_oom();
618
619 return 0;
620}
621
f00ff0de
DJL
622int config_parse_bridge_port_priority(
623 const char *unit,
624 const char *filename,
625 unsigned line,
626 const char *section,
627 unsigned section_line,
628 const char *lvalue,
629 int ltype,
630 const char *rvalue,
631 void *data,
632 void *userdata) {
633
634 uint16_t i;
635 int r;
636
637 assert(filename);
638 assert(lvalue);
639 assert(rvalue);
640 assert(data);
641
642 r = safe_atou16(rvalue, &i);
643 if (r < 0) {
644 log_syntax(unit, LOG_ERR, filename, line, r,
645 "Failed to parse bridge port priority, ignoring: %s", rvalue);
646 return 0;
647 }
648
649 if (i > LINK_BRIDGE_PORT_PRIORITY_MAX) {
650 log_syntax(unit, LOG_ERR, filename, line, r,
651 "Bridge port priority is larger than maximum %u, ignoring: %s", LINK_BRIDGE_PORT_PRIORITY_MAX, rvalue);
652 return 0;
653 }
654
655 *((uint16_t *)data) = i;
656
657 return 0;
658}
659
072320ea
TH
660size_t serialize_in_addrs(FILE *f,
661 const struct in_addr *addresses,
662 size_t size,
d8bff5cc 663 bool *with_leading_space,
072320ea 664 bool (*predicate)(const struct in_addr *addr)) {
09bee74d 665 assert(f);
09bee74d 666 assert(addresses);
09bee74d 667
dddc8d1e 668 size_t count = 0;
d8bff5cc
ZJS
669 bool _space = false;
670 if (!with_leading_space)
671 with_leading_space = &_space;
072320ea 672
dddc8d1e 673 for (size_t i = 0; i < size; i++) {
189255d2
TH
674 char sbuf[INET_ADDRSTRLEN];
675
072320ea
TH
676 if (predicate && !predicate(&addresses[i]))
677 continue;
d8bff5cc
ZJS
678
679 if (*with_leading_space)
072320ea 680 fputc(' ', f);
189255d2 681 fputs(inet_ntop(AF_INET, &addresses[i], sbuf, sizeof(sbuf)), f);
072320ea 682 count++;
d8bff5cc 683 *with_leading_space = true;
072320ea
TH
684 }
685
686 return count;
09bee74d
TG
687}
688
a2ba62c7 689int deserialize_in_addrs(struct in_addr **ret, const char *string) {
09bee74d 690 _cleanup_free_ struct in_addr *addresses = NULL;
a2ba62c7 691 int size = 0;
09bee74d
TG
692
693 assert(ret);
09bee74d
TG
694 assert(string);
695
93e28226
SS
696 for (;;) {
697 _cleanup_free_ char *word = NULL;
09bee74d
TG
698 struct in_addr *new_addresses;
699 int r;
700
93e28226
SS
701 r = extract_first_word(&string, &word, NULL, 0);
702 if (r < 0)
703 return r;
704 if (r == 0)
705 break;
706
62d74c78 707 new_addresses = reallocarray(addresses, size + 1, sizeof(struct in_addr));
09bee74d
TG
708 if (!new_addresses)
709 return -ENOMEM;
710 else
711 addresses = new_addresses;
712
93e28226 713 r = inet_pton(AF_INET, word, &(addresses[size]));
09bee74d
TG
714 if (r <= 0)
715 continue;
716
313cefa1 717 size++;
09bee74d
TG
718 }
719
c24b6821 720 *ret = size > 0 ? TAKE_PTR(addresses) : NULL;
09bee74d 721
a2ba62c7 722 return size;
09bee74d
TG
723}
724
d8bff5cc 725void serialize_in6_addrs(FILE *f, const struct in6_addr *addresses, size_t size, bool *with_leading_space) {
b729fa14
PF
726 assert(f);
727 assert(addresses);
728 assert(size);
729
d8bff5cc
ZJS
730 bool _space = false;
731 if (!with_leading_space)
732 with_leading_space = &_space;
733
dddc8d1e 734 for (size_t i = 0; i < size; i++) {
1f152e4b
LP
735 char buffer[INET6_ADDRSTRLEN];
736
d8bff5cc 737 if (*with_leading_space)
1f152e4b 738 fputc(' ', f);
d8bff5cc
ZJS
739 fputs(inet_ntop(AF_INET6, addresses+i, buffer, sizeof(buffer)), f);
740 *with_leading_space = true;
1f152e4b 741 }
b729fa14
PF
742}
743
a2ba62c7 744int deserialize_in6_addrs(struct in6_addr **ret, const char *string) {
09bee74d 745 _cleanup_free_ struct in6_addr *addresses = NULL;
a2ba62c7 746 int size = 0;
09bee74d
TG
747
748 assert(ret);
09bee74d
TG
749 assert(string);
750
93e28226
SS
751 for (;;) {
752 _cleanup_free_ char *word = NULL;
09bee74d
TG
753 struct in6_addr *new_addresses;
754 int r;
755
93e28226
SS
756 r = extract_first_word(&string, &word, NULL, 0);
757 if (r < 0)
758 return r;
759 if (r == 0)
760 break;
761
62d74c78 762 new_addresses = reallocarray(addresses, size + 1, sizeof(struct in6_addr));
09bee74d
TG
763 if (!new_addresses)
764 return -ENOMEM;
765 else
766 addresses = new_addresses;
767
93e28226 768 r = inet_pton(AF_INET6, word, &(addresses[size]));
09bee74d
TG
769 if (r <= 0)
770 continue;
771
772 size++;
773 }
774
ae2a15bc 775 *ret = TAKE_PTR(addresses);
09bee74d 776
a2ba62c7 777 return size;
09bee74d 778}
e1ea665e 779
f8693fc7 780void serialize_dhcp_routes(FILE *f, const char *key, sd_dhcp_route **routes, size_t size) {
e1ea665e
EY
781 assert(f);
782 assert(key);
783 assert(routes);
784 assert(size);
785
786 fprintf(f, "%s=", key);
787
dddc8d1e 788 for (size_t i = 0; i < size; i++) {
189255d2 789 char sbuf[INET_ADDRSTRLEN];
f8693fc7
BG
790 struct in_addr dest, gw;
791 uint8_t length;
792
793 assert_se(sd_dhcp_route_get_destination(routes[i], &dest) >= 0);
794 assert_se(sd_dhcp_route_get_gateway(routes[i], &gw) >= 0);
795 assert_se(sd_dhcp_route_get_destination_prefix_length(routes[i], &length) >= 0);
796
dddc8d1e
ZJS
797 fprintf(f, "%s/%" PRIu8, inet_ntop(AF_INET, &dest, sbuf, sizeof sbuf), length);
798 fprintf(f, ",%s%s", inet_ntop(AF_INET, &gw, sbuf, sizeof sbuf), i < size - 1 ? " ": "");
fbf7dcb5 799 }
e1ea665e
EY
800
801 fputs("\n", f);
802}
803
804int deserialize_dhcp_routes(struct sd_dhcp_route **ret, size_t *ret_size, size_t *ret_allocated, const char *string) {
805 _cleanup_free_ struct sd_dhcp_route *routes = NULL;
806 size_t size = 0, allocated = 0;
e1ea665e
EY
807
808 assert(ret);
809 assert(ret_size);
810 assert(ret_allocated);
811 assert(string);
812
93e28226
SS
813 /* WORD FORMAT: dst_ip/dst_prefixlen,gw_ip */
814 for (;;) {
815 _cleanup_free_ char *word = NULL;
e1ea665e
EY
816 char *tok, *tok_end;
817 unsigned n;
818 int r;
819
93e28226
SS
820 r = extract_first_word(&string, &word, NULL, 0);
821 if (r < 0)
822 return r;
823 if (r == 0)
824 break;
e1ea665e 825
93e28226 826 if (!GREEDY_REALLOC(routes, allocated, size + 1))
31db0120 827 return -ENOMEM;
e1ea665e 828
93e28226 829 tok = word;
e1ea665e
EY
830
831 /* get the subnet */
832 tok_end = strchr(tok, '/');
833 if (!tok_end)
834 continue;
835 *tok_end = '\0';
836
837 r = inet_aton(tok, &routes[size].dst_addr);
838 if (r == 0)
839 continue;
840
841 tok = tok_end + 1;
842
843 /* get the prefixlen */
844 tok_end = strchr(tok, ',');
845 if (!tok_end)
846 continue;
847
848 *tok_end = '\0';
849
850 r = safe_atou(tok, &n);
851 if (r < 0 || n > 32)
852 continue;
853
854 routes[size].dst_prefixlen = (uint8_t) n;
855 tok = tok_end + 1;
856
857 /* get the gateway */
858 r = inet_aton(tok, &routes[size].gw_addr);
859 if (r == 0)
860 continue;
861
862 size++;
863 }
864
865 *ret_size = size;
866 *ret_allocated = allocated;
ae2a15bc 867 *ret = TAKE_PTR(routes);
e1ea665e
EY
868
869 return 0;
870}
a073309f 871
e4735228 872int serialize_dhcp_option(FILE *f, const char *key, const void *data, size_t size) {
a073309f
AC
873 _cleanup_free_ char *hex_buf = NULL;
874
875 assert(f);
876 assert(key);
877 assert(data);
878
879 hex_buf = hexmem(data, size);
4e361acc 880 if (!hex_buf)
a073309f
AC
881 return -ENOMEM;
882
883 fprintf(f, "%s=%s\n", key, hex_buf);
884
885 return 0;
886}