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