]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd-network/network-internal.c
network: read driver name from ethtool
[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 "arphrd-list.h"
12 #include "condition.h"
13 #include "conf-parser.h"
14 #include "device-util.h"
15 #include "dhcp-lease-internal.h"
16 #include "env-util.h"
17 #include "ether-addr-util.h"
18 #include "hexdecoct.h"
19 #include "log.h"
20 #include "network-internal.h"
21 #include "parse-util.h"
22 #include "siphash24.h"
23 #include "socket-util.h"
24 #include "string-table.h"
25 #include "string-util.h"
26 #include "strv.h"
27 #include "utf8.h"
28 #include "util.h"
29
30 const char *net_get_name_persistent(sd_device *device) {
31 const char *name, *field;
32
33 assert(device);
34
35 /* fetch some persistent data unique (on this machine) to this device */
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)
38 return name;
39
40 return NULL;
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
45 int net_get_unique_predictable_data(sd_device *device, bool use_sysname, uint64_t *result) {
46 size_t l, sz = 0;
47 const char *name;
48 int r;
49 uint8_t *v;
50
51 assert(device);
52
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);
56 if (!name && use_sysname)
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");
61
62 log_device_debug(device, "Using \"%s\" as stable identifying information", name);
63 l = strlen(name);
64 sz = sizeof(sd_id128_t) + l;
65 v = newa(uint8_t, sz);
66
67 /* Fetch some persistent data unique to this machine */
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
73 /* Let's hash the machine ID plus the device name. We use
74 * a fixed, but originally randomly created hash key here. */
75 *result = htole64(siphash24(v, sz, HASH_KEY.bytes));
76 return 0;
77 }
78
79 static 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))
84 return true;
85
86 STRV_FOREACH(p, patterns) {
87 const char *q = *p;
88 bool invert;
89
90 invert = *q == '!';
91 q += invert;
92
93 if (!invert)
94 has_positive_rule = true;
95
96 if (string && fnmatch(q, string, 0) == 0) {
97 if (invert)
98 return false;
99 else
100 match = true;
101 }
102 }
103
104 return has_positive_rule ? match : true;
105 }
106
107 static 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
119 static 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
153 static 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
168 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(wifi_iftype, enum nl80211_iftype);
169
170 char *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
191 bool net_match_config(Set *match_mac,
192 Set *match_permanent_mac,
193 char * const *match_paths,
194 char * const *match_drivers,
195 char * const *match_iftypes,
196 char * const *match_names,
197 char * const *match_property,
198 char * const *match_wifi_iftype,
199 char * const *match_ssid,
200 Set *match_bssid,
201 sd_device *device,
202 const struct ether_addr *dev_mac,
203 const struct ether_addr *dev_permanent_mac,
204 const char *dev_driver,
205 unsigned short dev_iftype,
206 const char *dev_name,
207 char * const *alternative_names,
208 enum nl80211_iftype dev_wifi_iftype,
209 const char *dev_ssid,
210 const struct ether_addr *dev_bssid) {
211
212 _cleanup_free_ char *dev_iftype_str;
213 const char *dev_path = NULL;
214
215 dev_iftype_str = link_get_type_string(dev_iftype, device);
216
217 if (device) {
218 const char *mac_str;
219
220 (void) sd_device_get_property_value(device, "ID_PATH", &dev_path);
221 if (!dev_driver)
222 (void) sd_device_get_property_value(device, "ID_NET_DRIVER", &dev_driver);
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
230 if (match_mac && (!dev_mac || !set_contains(match_mac, dev_mac)))
231 return false;
232
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
239 if (!net_condition_test_strv(match_paths, dev_path))
240 return false;
241
242 if (!net_condition_test_strv(match_drivers, dev_driver))
243 return false;
244
245 if (!net_condition_test_strv(match_iftypes, dev_iftype_str))
246 return false;
247
248 if (!net_condition_test_ifname(match_names, dev_name, alternative_names))
249 return false;
250
251 if (!net_condition_test_property(match_property, device))
252 return false;
253
254 if (!net_condition_test_strv(match_wifi_iftype, wifi_iftype_to_string(dev_wifi_iftype)))
255 return false;
256
257 if (!net_condition_test_strv(match_ssid, dev_ssid))
258 return false;
259
260 if (match_bssid && (!dev_bssid || !set_contains(match_bssid, dev_bssid)))
261 return false;
262
263 return true;
264 }
265
266 int 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;
278 Condition **list = data, *c;
279 bool negate;
280
281 assert(filename);
282 assert(lvalue);
283 assert(rvalue);
284 assert(data);
285
286 if (isempty(rvalue)) {
287 *list = condition_free_list_type(*list, cond);
288 return 0;
289 }
290
291 negate = rvalue[0] == '!';
292 if (negate)
293 rvalue++;
294
295 c = condition_new(cond, rvalue, false, negate);
296 if (!c)
297 return log_oom();
298
299 /* Drop previous assignment. */
300 *list = condition_free_list_type(*list, cond);
301
302 LIST_PREPEND(conditions, *list, c);
303 return 0;
304 }
305
306 int config_parse_match_strv(
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) {
317
318 const char *p = rvalue;
319 char ***sv = data;
320 bool invert;
321 int r;
322
323 assert(filename);
324 assert(lvalue);
325 assert(rvalue);
326 assert(data);
327
328 if (isempty(rvalue)) {
329 *sv = strv_free(*sv);
330 return 0;
331 }
332
333 invert = *p == '!';
334 p += invert;
335
336 for (;;) {
337 _cleanup_free_ char *word = NULL, *k = NULL;
338
339 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE|EXTRACT_RETAIN_ESCAPE);
340 if (r == 0)
341 return 0;
342 if (r == -ENOMEM)
343 return log_oom();
344 if (r < 0) {
345 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid syntax, ignoring: %s", rvalue);
346 return 0;
347 }
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
362 int 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);
391 if (r == 0)
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 }
400
401 if (!ifname_valid_full(word, ltype)) {
402 log_syntax(unit, LOG_ERR, filename, line, 0,
403 "Interface name is not valid or too long, ignoring assignment: %s", word);
404 continue;
405 }
406
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));
415 if (r < 0)
416 return log_oom();
417 }
418 }
419
420 int 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
478 int config_parse_ifalias(const char *unit,
479 const char *filename,
480 unsigned line,
481 const char *section,
482 unsigned section_line,
483 const char *lvalue,
484 int ltype,
485 const char *rvalue,
486 void *data,
487 void *userdata) {
488
489 char **s = data;
490 _cleanup_free_ char *n = NULL;
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) {
502 log_syntax(unit, LOG_ERR, filename, line, 0, "Interface alias is not ASCII clean or is too long, ignoring assignment: %s", rvalue);
503 return 0;
504 }
505
506 if (isempty(n))
507 *s = mfree(*s);
508 else
509 free_and_replace(*s, n);
510
511 return 0;
512 }
513
514 int config_parse_hwaddr(const char *unit,
515 const char *filename,
516 unsigned line,
517 const char *section,
518 unsigned section_line,
519 const char *lvalue,
520 int ltype,
521 const char *rvalue,
522 void *data,
523 void *userdata) {
524
525 _cleanup_free_ struct ether_addr *n = NULL;
526 struct ether_addr **hwaddr = data;
527 int r;
528
529 assert(filename);
530 assert(lvalue);
531 assert(rvalue);
532 assert(data);
533
534 n = new0(struct ether_addr, 1);
535 if (!n)
536 return log_oom();
537
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);
541 return 0;
542 }
543
544 free_and_replace(*hwaddr, n);
545
546 return 0;
547 }
548
549 int 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 */
572 *hwaddrs = set_free_free(*hwaddrs);
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
594 n = new(struct ether_addr, 1);
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
622 int 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
660 size_t serialize_in_addrs(FILE *f,
661 const struct in_addr *addresses,
662 size_t size,
663 bool *with_leading_space,
664 bool (*predicate)(const struct in_addr *addr)) {
665 assert(f);
666 assert(addresses);
667
668 size_t count = 0;
669 bool _space = false;
670 if (!with_leading_space)
671 with_leading_space = &_space;
672
673 for (size_t i = 0; i < size; i++) {
674 char sbuf[INET_ADDRSTRLEN];
675
676 if (predicate && !predicate(&addresses[i]))
677 continue;
678
679 if (*with_leading_space)
680 fputc(' ', f);
681 fputs(inet_ntop(AF_INET, &addresses[i], sbuf, sizeof(sbuf)), f);
682 count++;
683 *with_leading_space = true;
684 }
685
686 return count;
687 }
688
689 int deserialize_in_addrs(struct in_addr **ret, const char *string) {
690 _cleanup_free_ struct in_addr *addresses = NULL;
691 int size = 0;
692
693 assert(ret);
694 assert(string);
695
696 for (;;) {
697 _cleanup_free_ char *word = NULL;
698 struct in_addr *new_addresses;
699 int r;
700
701 r = extract_first_word(&string, &word, NULL, 0);
702 if (r < 0)
703 return r;
704 if (r == 0)
705 break;
706
707 new_addresses = reallocarray(addresses, size + 1, sizeof(struct in_addr));
708 if (!new_addresses)
709 return -ENOMEM;
710 else
711 addresses = new_addresses;
712
713 r = inet_pton(AF_INET, word, &(addresses[size]));
714 if (r <= 0)
715 continue;
716
717 size++;
718 }
719
720 *ret = size > 0 ? TAKE_PTR(addresses) : NULL;
721
722 return size;
723 }
724
725 void serialize_in6_addrs(FILE *f, const struct in6_addr *addresses, size_t size, bool *with_leading_space) {
726 assert(f);
727 assert(addresses);
728 assert(size);
729
730 bool _space = false;
731 if (!with_leading_space)
732 with_leading_space = &_space;
733
734 for (size_t i = 0; i < size; i++) {
735 char buffer[INET6_ADDRSTRLEN];
736
737 if (*with_leading_space)
738 fputc(' ', f);
739 fputs(inet_ntop(AF_INET6, addresses+i, buffer, sizeof(buffer)), f);
740 *with_leading_space = true;
741 }
742 }
743
744 int deserialize_in6_addrs(struct in6_addr **ret, const char *string) {
745 _cleanup_free_ struct in6_addr *addresses = NULL;
746 int size = 0;
747
748 assert(ret);
749 assert(string);
750
751 for (;;) {
752 _cleanup_free_ char *word = NULL;
753 struct in6_addr *new_addresses;
754 int r;
755
756 r = extract_first_word(&string, &word, NULL, 0);
757 if (r < 0)
758 return r;
759 if (r == 0)
760 break;
761
762 new_addresses = reallocarray(addresses, size + 1, sizeof(struct in6_addr));
763 if (!new_addresses)
764 return -ENOMEM;
765 else
766 addresses = new_addresses;
767
768 r = inet_pton(AF_INET6, word, &(addresses[size]));
769 if (r <= 0)
770 continue;
771
772 size++;
773 }
774
775 *ret = TAKE_PTR(addresses);
776
777 return size;
778 }
779
780 void serialize_dhcp_routes(FILE *f, const char *key, sd_dhcp_route **routes, size_t size) {
781 assert(f);
782 assert(key);
783 assert(routes);
784 assert(size);
785
786 fprintf(f, "%s=", key);
787
788 for (size_t i = 0; i < size; i++) {
789 char sbuf[INET_ADDRSTRLEN];
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
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 ? " ": "");
799 }
800
801 fputs("\n", f);
802 }
803
804 int 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;
807
808 assert(ret);
809 assert(ret_size);
810 assert(ret_allocated);
811 assert(string);
812
813 /* WORD FORMAT: dst_ip/dst_prefixlen,gw_ip */
814 for (;;) {
815 _cleanup_free_ char *word = NULL;
816 char *tok, *tok_end;
817 unsigned n;
818 int r;
819
820 r = extract_first_word(&string, &word, NULL, 0);
821 if (r < 0)
822 return r;
823 if (r == 0)
824 break;
825
826 if (!GREEDY_REALLOC(routes, allocated, size + 1))
827 return -ENOMEM;
828
829 tok = word;
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;
867 *ret = TAKE_PTR(routes);
868
869 return 0;
870 }
871
872 int serialize_dhcp_option(FILE *f, const char *key, const void *data, size_t size) {
873 _cleanup_free_ char *hex_buf = NULL;
874
875 assert(f);
876 assert(key);
877 assert(data);
878
879 hex_buf = hexmem(data, size);
880 if (!hex_buf)
881 return -ENOMEM;
882
883 fprintf(f, "%s=%s\n", key, hex_buf);
884
885 return 0;
886 }