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