]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/libsystemd-network/network-internal.c
sd-network: fix inverted error message
[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,
195 char * const *match_types,
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,
ef62949a 201 unsigned short iftype,
b38de0e9 202 sd_device *device,
505f8da7 203 const struct ether_addr *dev_mac,
4bb7cc82 204 const struct ether_addr *dev_permanent_mac,
8d968fdd 205 const char *dev_name,
572b21d9 206 char * const *alternative_names,
78404d22 207 enum nl80211_iftype wifi_iftype,
277ba8d1
YW
208 const char *ssid,
209 const struct ether_addr *bssid) {
be32eb9b 210
ef62949a
YW
211 const char *dev_path = NULL, *dev_driver = NULL, *mac_str;
212 _cleanup_free_ char *dev_type;
213
214 dev_type = link_get_type_string(iftype, device);
b38de0e9
YW
215
216 if (device) {
217 (void) sd_device_get_property_value(device, "ID_PATH", &dev_path);
218 (void) sd_device_get_property_value(device, "ID_NET_DRIVER", &dev_driver);
b38de0e9
YW
219 if (!dev_name)
220 (void) sd_device_get_sysname(device, &dev_name);
221 if (!dev_mac &&
222 sd_device_get_sysattr_value(device, "address", &mac_str) >= 0)
223 dev_mac = ether_aton(mac_str);
224 }
225
25ea58d3 226 if (match_mac && (!dev_mac || !set_contains(match_mac, dev_mac)))
7eb08da4 227 return false;
be32eb9b 228
4bb7cc82
YW
229 if (match_permanent_mac &&
230 (!dev_permanent_mac ||
231 ether_addr_is_null(dev_permanent_mac) ||
232 !set_contains(match_permanent_mac, dev_permanent_mac)))
233 return false;
234
618b196e 235 if (!net_condition_test_strv(match_paths, dev_path))
ee5de57b 236 return false;
5256e00e 237
618b196e 238 if (!net_condition_test_strv(match_drivers, dev_driver))
ee5de57b 239 return false;
5256e00e 240
618b196e 241 if (!net_condition_test_strv(match_types, dev_type))
ee5de57b 242 return false;
5256e00e 243
572b21d9 244 if (!net_condition_test_ifname(match_names, dev_name, alternative_names))
ee5de57b 245 return false;
5256e00e 246
44005bfb
YW
247 if (!net_condition_test_property(match_property, device))
248 return false;
249
78404d22
YW
250 if (!net_condition_test_strv(match_wifi_iftype, wifi_iftype_to_string(wifi_iftype)))
251 return false;
252
8d968fdd
YW
253 if (!net_condition_test_strv(match_ssid, ssid))
254 return false;
255
277ba8d1
YW
256 if (match_bssid && (!bssid || !set_contains(match_bssid, bssid)))
257 return false;
258
7eb08da4 259 return true;
be32eb9b 260}
5fde13d7 261
2cc412b5
TG
262int config_parse_net_condition(const char *unit,
263 const char *filename,
264 unsigned line,
265 const char *section,
266 unsigned section_line,
267 const char *lvalue,
268 int ltype,
269 const char *rvalue,
270 void *data,
271 void *userdata) {
272
273 ConditionType cond = ltype;
c4f58dea 274 Condition **list = data, *c;
2cc412b5 275 bool negate;
2cc412b5
TG
276
277 assert(filename);
278 assert(lvalue);
279 assert(rvalue);
280 assert(data);
281
c4f58dea
YW
282 if (isempty(rvalue)) {
283 *list = condition_free_list_type(*list, cond);
284 return 0;
285 }
286
2cc412b5
TG
287 negate = rvalue[0] == '!';
288 if (negate)
289 rvalue++;
290
2bd0da7a 291 c = condition_new(cond, rvalue, false, negate);
2cc412b5
TG
292 if (!c)
293 return log_oom();
294
c4f58dea
YW
295 /* Drop previous assignment. */
296 *list = condition_free_list_type(*list, cond);
2cc412b5 297
c4f58dea 298 LIST_PREPEND(conditions, *list, c);
2cc412b5
TG
299 return 0;
300}
301
54a84237 302int config_parse_match_strv(
d31645ad
LP
303 const char *unit,
304 const char *filename,
305 unsigned line,
306 const char *section,
307 unsigned section_line,
308 const char *lvalue,
309 int ltype,
310 const char *rvalue,
311 void *data,
312 void *userdata) {
5256e00e 313
54a84237 314 const char *p = rvalue;
5256e00e 315 char ***sv = data;
54a84237 316 bool invert;
5256e00e
TG
317 int r;
318
319 assert(filename);
320 assert(lvalue);
321 assert(rvalue);
322 assert(data);
323
54a84237
YW
324 if (isempty(rvalue)) {
325 *sv = strv_free(*sv);
326 return 0;
327 }
328
329 invert = *p == '!';
330 p += invert;
331
93e28226 332 for (;;) {
54a84237 333 _cleanup_free_ char *word = NULL, *k = NULL;
5256e00e 334
adfafd88 335 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE|EXTRACT_RETAIN_ESCAPE);
54a84237
YW
336 if (r == 0)
337 return 0;
338 if (r == -ENOMEM)
339 return log_oom();
a9dd908d 340 if (r < 0) {
54a84237 341 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid syntax, ignoring: %s", rvalue);
a9dd908d
LP
342 return 0;
343 }
54a84237
YW
344
345 if (invert) {
346 k = strjoin("!", word);
347 if (!k)
348 return log_oom();
349 } else
350 k = TAKE_PTR(word);
351
352 r = strv_consume(sv, TAKE_PTR(k));
353 if (r < 0)
354 return log_oom();
355 }
356}
357
358int config_parse_match_ifnames(
359 const char *unit,
360 const char *filename,
361 unsigned line,
362 const char *section,
363 unsigned section_line,
364 const char *lvalue,
365 int ltype,
366 const char *rvalue,
367 void *data,
368 void *userdata) {
369
370 const char *p = rvalue;
371 char ***sv = data;
372 bool invert;
373 int r;
374
375 assert(filename);
376 assert(lvalue);
377 assert(rvalue);
378 assert(data);
379
380 invert = *p == '!';
381 p += invert;
382
383 for (;;) {
384 _cleanup_free_ char *word = NULL, *k = NULL;
385
386 r = extract_first_word(&p, &word, NULL, 0);
93e28226 387 if (r == 0)
54a84237
YW
388 return 0;
389 if (r == -ENOMEM)
390 return log_oom();
391 if (r < 0) {
392 log_syntax(unit, LOG_ERR, filename, line, 0,
393 "Failed to parse interface name list: %s", rvalue);
394 return 0;
395 }
5256e00e 396
572b21d9 397 if (!ifname_valid_full(word, ltype)) {
54a84237
YW
398 log_syntax(unit, LOG_ERR, filename, line, 0,
399 "Interface name is not valid or too long, ignoring assignment: %s", word);
400 continue;
5256e00e
TG
401 }
402
54a84237
YW
403 if (invert) {
404 k = strjoin("!", word);
405 if (!k)
406 return log_oom();
407 } else
408 k = TAKE_PTR(word);
409
410 r = strv_consume(sv, TAKE_PTR(k));
5256e00e
TG
411 if (r < 0)
412 return log_oom();
413 }
5256e00e
TG
414}
415
44005bfb
YW
416int config_parse_match_property(
417 const char *unit,
418 const char *filename,
419 unsigned line,
420 const char *section,
421 unsigned section_line,
422 const char *lvalue,
423 int ltype,
424 const char *rvalue,
425 void *data,
426 void *userdata) {
427
428 const char *p = rvalue;
429 char ***sv = data;
430 bool invert;
431 int r;
432
433 assert(filename);
434 assert(lvalue);
435 assert(rvalue);
436 assert(data);
437
438 invert = *p == '!';
439 p += invert;
440
441 for (;;) {
442 _cleanup_free_ char *word = NULL, *k = NULL;
443
444 r = extract_first_word(&p, &word, NULL, EXTRACT_CUNESCAPE|EXTRACT_UNQUOTE);
445 if (r == 0)
446 return 0;
447 if (r == -ENOMEM)
448 return log_oom();
449 if (r < 0) {
450 log_syntax(unit, LOG_ERR, filename, line, 0,
451 "Invalid syntax, ignoring: %s", rvalue);
452 return 0;
453 }
454
455 if (!env_assignment_is_valid(word)) {
456 log_syntax(unit, LOG_ERR, filename, line, 0,
457 "Invalid property or value, ignoring assignment: %s", word);
458 continue;
459 }
460
461 if (invert) {
462 k = strjoin("!", word);
463 if (!k)
464 return log_oom();
465 } else
466 k = TAKE_PTR(word);
467
468 r = strv_consume(sv, TAKE_PTR(k));
469 if (r < 0)
470 return log_oom();
471 }
472}
473
d2df0d0e
TG
474int config_parse_ifalias(const char *unit,
475 const char *filename,
476 unsigned line,
477 const char *section,
71a61510 478 unsigned section_line,
d2df0d0e
TG
479 const char *lvalue,
480 int ltype,
481 const char *rvalue,
482 void *data,
483 void *userdata) {
484
485 char **s = data;
9c39eb5c 486 _cleanup_free_ char *n = NULL;
d2df0d0e
TG
487
488 assert(filename);
489 assert(lvalue);
490 assert(rvalue);
491 assert(data);
492
493 n = strdup(rvalue);
494 if (!n)
495 return log_oom();
496
497 if (!ascii_is_valid(n) || strlen(n) >= IFALIASZ) {
12ca818f 498 log_syntax(unit, LOG_ERR, filename, line, 0, "Interface alias is not ASCII clean or is too long, ignoring assignment: %s", rvalue);
d2df0d0e
TG
499 return 0;
500 }
501
44386b44
YW
502 if (isempty(n))
503 *s = mfree(*s);
ae2a15bc 504 else
44386b44 505 free_and_replace(*s, n);
d2df0d0e
TG
506
507 return 0;
508}
509
5fde13d7
TG
510int config_parse_hwaddr(const char *unit,
511 const char *filename,
512 unsigned line,
513 const char *section,
71a61510 514 unsigned section_line,
5fde13d7
TG
515 const char *lvalue,
516 int ltype,
517 const char *rvalue,
518 void *data,
519 void *userdata) {
e5c1be89
YW
520
521 _cleanup_free_ struct ether_addr *n = NULL;
5fde13d7 522 struct ether_addr **hwaddr = data;
5fde13d7
TG
523 int r;
524
525 assert(filename);
526 assert(lvalue);
527 assert(rvalue);
528 assert(data);
529
a12fa420 530 n = new0(struct ether_addr, 1);
5fde13d7
TG
531 if (!n)
532 return log_oom();
533
e5c1be89
YW
534 r = ether_addr_from_string(rvalue, n);
535 if (r < 0) {
536 log_syntax(unit, LOG_ERR, filename, line, r, "Not a valid MAC address, ignoring assignment: %s", rvalue);
5fde13d7
TG
537 return 0;
538 }
539
899f0d25 540 free_and_replace(*hwaddr, n);
5fde13d7
TG
541
542 return 0;
543}
f5284182 544
206b63ee
YW
545int config_parse_hwaddrs(const char *unit,
546 const char *filename,
547 unsigned line,
548 const char *section,
549 unsigned section_line,
550 const char *lvalue,
551 int ltype,
552 const char *rvalue,
553 void *data,
554 void *userdata) {
555
556 _cleanup_set_free_free_ Set *s = NULL;
557 const char *p = rvalue;
558 Set **hwaddrs = data;
559 int r;
560
561 assert(filename);
562 assert(lvalue);
563 assert(rvalue);
564 assert(data);
565
566 if (isempty(rvalue)) {
567 /* Empty assignment resets the list */
e90d0374 568 *hwaddrs = set_free_free(*hwaddrs);
206b63ee
YW
569 return 0;
570 }
571
572 s = set_new(&ether_addr_hash_ops);
573 if (!s)
574 return log_oom();
575
576 for (;;) {
577 _cleanup_free_ char *word = NULL;
578 _cleanup_free_ struct ether_addr *n = NULL;
579
580 r = extract_first_word(&p, &word, NULL, 0);
581 if (r == 0)
582 break;
583 if (r == -ENOMEM)
584 return log_oom();
585 if (r < 0) {
586 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid syntax, ignoring: %s", rvalue);
587 return 0;
588 }
589
e90d0374 590 n = new(struct ether_addr, 1);
206b63ee
YW
591 if (!n)
592 return log_oom();
593
594 r = ether_addr_from_string(word, n);
595 if (r < 0) {
596 log_syntax(unit, LOG_ERR, filename, line, 0, "Not a valid MAC address, ignoring: %s", word);
597 continue;
598 }
599
600 r = set_put(s, n);
601 if (r < 0)
602 return log_oom();
603 if (r > 0)
604 n = NULL; /* avoid cleanup */
605 }
606
607 r = set_ensure_allocated(hwaddrs, &ether_addr_hash_ops);
608 if (r < 0)
609 return log_oom();
610
611 r = set_move(*hwaddrs, s);
612 if (r < 0)
613 return log_oom();
614
615 return 0;
616}
617
f00ff0de
DJL
618int config_parse_bridge_port_priority(
619 const char *unit,
620 const char *filename,
621 unsigned line,
622 const char *section,
623 unsigned section_line,
624 const char *lvalue,
625 int ltype,
626 const char *rvalue,
627 void *data,
628 void *userdata) {
629
630 uint16_t i;
631 int r;
632
633 assert(filename);
634 assert(lvalue);
635 assert(rvalue);
636 assert(data);
637
638 r = safe_atou16(rvalue, &i);
639 if (r < 0) {
640 log_syntax(unit, LOG_ERR, filename, line, r,
641 "Failed to parse bridge port priority, ignoring: %s", rvalue);
642 return 0;
643 }
644
645 if (i > LINK_BRIDGE_PORT_PRIORITY_MAX) {
646 log_syntax(unit, LOG_ERR, filename, line, r,
647 "Bridge port priority is larger than maximum %u, ignoring: %s", LINK_BRIDGE_PORT_PRIORITY_MAX, rvalue);
648 return 0;
649 }
650
651 *((uint16_t *)data) = i;
652
653 return 0;
654}
655
072320ea
TH
656size_t serialize_in_addrs(FILE *f,
657 const struct in_addr *addresses,
658 size_t size,
659 bool with_leading_space,
660 bool (*predicate)(const struct in_addr *addr)) {
661 size_t count;
662 size_t i;
09bee74d
TG
663
664 assert(f);
09bee74d 665 assert(addresses);
09bee74d 666
072320ea
TH
667 count = 0;
668
669 for (i = 0; i < size; i++) {
189255d2
TH
670 char sbuf[INET_ADDRSTRLEN];
671
072320ea
TH
672 if (predicate && !predicate(&addresses[i]))
673 continue;
674 if (with_leading_space)
675 fputc(' ', f);
676 else
677 with_leading_space = true;
189255d2 678 fputs(inet_ntop(AF_INET, &addresses[i], sbuf, sizeof(sbuf)), f);
072320ea
TH
679 count++;
680 }
681
682 return count;
09bee74d
TG
683}
684
a2ba62c7 685int deserialize_in_addrs(struct in_addr **ret, const char *string) {
09bee74d 686 _cleanup_free_ struct in_addr *addresses = NULL;
a2ba62c7 687 int size = 0;
09bee74d
TG
688
689 assert(ret);
09bee74d
TG
690 assert(string);
691
93e28226
SS
692 for (;;) {
693 _cleanup_free_ char *word = NULL;
09bee74d
TG
694 struct in_addr *new_addresses;
695 int r;
696
93e28226
SS
697 r = extract_first_word(&string, &word, NULL, 0);
698 if (r < 0)
699 return r;
700 if (r == 0)
701 break;
702
62d74c78 703 new_addresses = reallocarray(addresses, size + 1, sizeof(struct in_addr));
09bee74d
TG
704 if (!new_addresses)
705 return -ENOMEM;
706 else
707 addresses = new_addresses;
708
93e28226 709 r = inet_pton(AF_INET, word, &(addresses[size]));
09bee74d
TG
710 if (r <= 0)
711 continue;
712
313cefa1 713 size++;
09bee74d
TG
714 }
715
c24b6821 716 *ret = size > 0 ? TAKE_PTR(addresses) : NULL;
09bee74d 717
a2ba62c7 718 return size;
09bee74d
TG
719}
720
1f152e4b 721void serialize_in6_addrs(FILE *f, const struct in6_addr *addresses, size_t size) {
b729fa14
PF
722 unsigned i;
723
724 assert(f);
725 assert(addresses);
726 assert(size);
727
1f152e4b
LP
728 for (i = 0; i < size; i++) {
729 char buffer[INET6_ADDRSTRLEN];
730
731 fputs(inet_ntop(AF_INET6, addresses+i, buffer, sizeof(buffer)), f);
732
733 if (i < size - 1)
734 fputc(' ', f);
735 }
b729fa14
PF
736}
737
a2ba62c7 738int deserialize_in6_addrs(struct in6_addr **ret, const char *string) {
09bee74d 739 _cleanup_free_ struct in6_addr *addresses = NULL;
a2ba62c7 740 int size = 0;
09bee74d
TG
741
742 assert(ret);
09bee74d
TG
743 assert(string);
744
93e28226
SS
745 for (;;) {
746 _cleanup_free_ char *word = NULL;
09bee74d
TG
747 struct in6_addr *new_addresses;
748 int r;
749
93e28226
SS
750 r = extract_first_word(&string, &word, NULL, 0);
751 if (r < 0)
752 return r;
753 if (r == 0)
754 break;
755
62d74c78 756 new_addresses = reallocarray(addresses, size + 1, sizeof(struct in6_addr));
09bee74d
TG
757 if (!new_addresses)
758 return -ENOMEM;
759 else
760 addresses = new_addresses;
761
93e28226 762 r = inet_pton(AF_INET6, word, &(addresses[size]));
09bee74d
TG
763 if (r <= 0)
764 continue;
765
766 size++;
767 }
768
ae2a15bc 769 *ret = TAKE_PTR(addresses);
09bee74d 770
a2ba62c7 771 return size;
09bee74d 772}
e1ea665e 773
f8693fc7 774void serialize_dhcp_routes(FILE *f, const char *key, sd_dhcp_route **routes, size_t size) {
e1ea665e
EY
775 unsigned i;
776
777 assert(f);
778 assert(key);
779 assert(routes);
780 assert(size);
781
782 fprintf(f, "%s=", key);
783
fbf7dcb5 784 for (i = 0; i < size; i++) {
189255d2 785 char sbuf[INET_ADDRSTRLEN];
f8693fc7
BG
786 struct in_addr dest, gw;
787 uint8_t length;
788
789 assert_se(sd_dhcp_route_get_destination(routes[i], &dest) >= 0);
790 assert_se(sd_dhcp_route_get_gateway(routes[i], &gw) >= 0);
791 assert_se(sd_dhcp_route_get_destination_prefix_length(routes[i], &length) >= 0);
792
189255d2
TH
793 fprintf(f, "%s/%" PRIu8, inet_ntop(AF_INET, &dest, sbuf, sizeof(sbuf)), length);
794 fprintf(f, ",%s%s", inet_ntop(AF_INET, &gw, sbuf, sizeof(sbuf)), (i < (size - 1)) ? " ": "");
fbf7dcb5 795 }
e1ea665e
EY
796
797 fputs("\n", f);
798}
799
800int deserialize_dhcp_routes(struct sd_dhcp_route **ret, size_t *ret_size, size_t *ret_allocated, const char *string) {
801 _cleanup_free_ struct sd_dhcp_route *routes = NULL;
802 size_t size = 0, allocated = 0;
e1ea665e
EY
803
804 assert(ret);
805 assert(ret_size);
806 assert(ret_allocated);
807 assert(string);
808
93e28226
SS
809 /* WORD FORMAT: dst_ip/dst_prefixlen,gw_ip */
810 for (;;) {
811 _cleanup_free_ char *word = NULL;
e1ea665e
EY
812 char *tok, *tok_end;
813 unsigned n;
814 int r;
815
93e28226
SS
816 r = extract_first_word(&string, &word, NULL, 0);
817 if (r < 0)
818 return r;
819 if (r == 0)
820 break;
e1ea665e 821
93e28226 822 if (!GREEDY_REALLOC(routes, allocated, size + 1))
31db0120 823 return -ENOMEM;
e1ea665e 824
93e28226 825 tok = word;
e1ea665e
EY
826
827 /* get the subnet */
828 tok_end = strchr(tok, '/');
829 if (!tok_end)
830 continue;
831 *tok_end = '\0';
832
833 r = inet_aton(tok, &routes[size].dst_addr);
834 if (r == 0)
835 continue;
836
837 tok = tok_end + 1;
838
839 /* get the prefixlen */
840 tok_end = strchr(tok, ',');
841 if (!tok_end)
842 continue;
843
844 *tok_end = '\0';
845
846 r = safe_atou(tok, &n);
847 if (r < 0 || n > 32)
848 continue;
849
850 routes[size].dst_prefixlen = (uint8_t) n;
851 tok = tok_end + 1;
852
853 /* get the gateway */
854 r = inet_aton(tok, &routes[size].gw_addr);
855 if (r == 0)
856 continue;
857
858 size++;
859 }
860
861 *ret_size = size;
862 *ret_allocated = allocated;
ae2a15bc 863 *ret = TAKE_PTR(routes);
e1ea665e
EY
864
865 return 0;
866}
a073309f 867
e4735228 868int serialize_dhcp_option(FILE *f, const char *key, const void *data, size_t size) {
a073309f
AC
869 _cleanup_free_ char *hex_buf = NULL;
870
871 assert(f);
872 assert(key);
873 assert(data);
874
875 hex_buf = hexmem(data, size);
4e361acc 876 if (!hex_buf)
a073309f
AC
877 return -ENOMEM;
878
879 fprintf(f, "%s=%s\n", key, hex_buf);
880
881 return 0;
882}