]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/networkd-address.c
network: allocate hashmap objects when they are required
[thirdparty/systemd.git] / src / network / networkd-address.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <net/if.h>
4
5 #include "alloc-util.h"
6 #include "conf-parser.h"
7 #include "firewall-util.h"
8 #include "netlink-util.h"
9 #include "networkd-address.h"
10 #include "networkd-manager.h"
11 #include "parse-util.h"
12 #include "set.h"
13 #include "socket-util.h"
14 #include "string-util.h"
15 #include "strv.h"
16 #include "utf8.h"
17 #include "util.h"
18
19 #define ADDRESSES_PER_LINK_MAX 2048U
20 #define STATIC_ADDRESSES_PER_NETWORK_MAX 1024U
21
22 int address_new(Address **ret) {
23 _cleanup_(address_freep) Address *address = NULL;
24
25 address = new(Address, 1);
26 if (!address)
27 return -ENOMEM;
28
29 *address = (Address) {
30 .family = AF_UNSPEC,
31 .scope = RT_SCOPE_UNIVERSE,
32 .cinfo.ifa_prefered = CACHE_INFO_INFINITY_LIFE_TIME,
33 .cinfo.ifa_valid = CACHE_INFO_INFINITY_LIFE_TIME,
34 };
35
36 *ret = TAKE_PTR(address);
37
38 return 0;
39 }
40
41 int address_new_static(Network *network, const char *filename, unsigned section_line, Address **ret) {
42 _cleanup_(network_config_section_freep) NetworkConfigSection *n = NULL;
43 _cleanup_(address_freep) Address *address = NULL;
44 int r;
45
46 assert(network);
47 assert(ret);
48 assert(!!filename == (section_line > 0));
49
50 if (filename) {
51 r = network_config_section_new(filename, section_line, &n);
52 if (r < 0)
53 return r;
54
55 address = hashmap_get(network->addresses_by_section, n);
56 if (address) {
57 *ret = TAKE_PTR(address);
58
59 return 0;
60 }
61 }
62
63 if (network->n_static_addresses >= STATIC_ADDRESSES_PER_NETWORK_MAX)
64 return -E2BIG;
65
66 r = address_new(&address);
67 if (r < 0)
68 return r;
69
70 address->network = network;
71 LIST_APPEND(addresses, network->static_addresses, address);
72 network->n_static_addresses++;
73
74 if (filename) {
75 address->section = TAKE_PTR(n);
76
77 r = hashmap_ensure_allocated(&network->addresses_by_section, &network_config_hash_ops);
78 if (r < 0)
79 return r;
80
81 r = hashmap_put(network->addresses_by_section, address->section, address);
82 if (r < 0)
83 return r;
84 }
85
86 *ret = TAKE_PTR(address);
87
88 return 0;
89 }
90
91 void address_free(Address *address) {
92 if (!address)
93 return;
94
95 if (address->network) {
96 LIST_REMOVE(addresses, address->network->static_addresses, address);
97 assert(address->network->n_static_addresses > 0);
98 address->network->n_static_addresses--;
99
100 if (address->section)
101 hashmap_remove(address->network->addresses_by_section, address->section);
102 }
103
104 if (address->link) {
105 set_remove(address->link->addresses, address);
106 set_remove(address->link->addresses_foreign, address);
107
108 if (in_addr_equal(AF_INET6, &address->in_addr, (const union in_addr_union *) &address->link->ipv6ll_address))
109 memzero(&address->link->ipv6ll_address, sizeof(struct in6_addr));
110 }
111
112 network_config_section_free(address->section);
113 free(address->label);
114 free(address);
115 }
116
117 static void address_hash_func(const void *b, struct siphash *state) {
118 const Address *a = b;
119
120 assert(a);
121
122 siphash24_compress(&a->family, sizeof(a->family), state);
123
124 switch (a->family) {
125 case AF_INET:
126 siphash24_compress(&a->prefixlen, sizeof(a->prefixlen), state);
127
128 /* peer prefix */
129 if (a->prefixlen != 0) {
130 uint32_t prefix;
131
132 if (a->in_addr_peer.in.s_addr != 0)
133 prefix = be32toh(a->in_addr_peer.in.s_addr) >> (32 - a->prefixlen);
134 else
135 prefix = be32toh(a->in_addr.in.s_addr) >> (32 - a->prefixlen);
136
137 siphash24_compress(&prefix, sizeof(prefix), state);
138 }
139
140 _fallthrough_;
141 case AF_INET6:
142 /* local address */
143 siphash24_compress(&a->in_addr, FAMILY_ADDRESS_SIZE(a->family), state);
144
145 break;
146 default:
147 /* treat any other address family as AF_UNSPEC */
148 break;
149 }
150 }
151
152 static int address_compare_func(const void *c1, const void *c2) {
153 const Address *a1 = c1, *a2 = c2;
154 int r;
155
156 r = CMP(a1->family, a2->family);
157 if (r != 0)
158 return r;
159
160 switch (a1->family) {
161 /* use the same notion of equality as the kernel does */
162 case AF_INET:
163 r = CMP(a1->prefixlen, a2->prefixlen);
164 if (r != 0)
165 return r;
166
167 /* compare the peer prefixes */
168 if (a1->prefixlen != 0) {
169 /* make sure we don't try to shift by 32.
170 * See ISO/IEC 9899:TC3 ยง 6.5.7.3. */
171 uint32_t b1, b2;
172
173 if (a1->in_addr_peer.in.s_addr != 0)
174 b1 = be32toh(a1->in_addr_peer.in.s_addr) >> (32 - a1->prefixlen);
175 else
176 b1 = be32toh(a1->in_addr.in.s_addr) >> (32 - a1->prefixlen);
177
178 if (a2->in_addr_peer.in.s_addr != 0)
179 b2 = be32toh(a2->in_addr_peer.in.s_addr) >> (32 - a1->prefixlen);
180 else
181 b2 = be32toh(a2->in_addr.in.s_addr) >> (32 - a1->prefixlen);
182
183 r = CMP(b1, b2);
184 if (r != 0)
185 return r;
186 }
187
188 _fallthrough_;
189 case AF_INET6:
190 return memcmp(&a1->in_addr, &a2->in_addr, FAMILY_ADDRESS_SIZE(a1->family));
191 default:
192 /* treat any other address family as AF_UNSPEC */
193 return 0;
194 }
195 }
196
197 static const struct hash_ops address_hash_ops = {
198 .hash = address_hash_func,
199 .compare = address_compare_func
200 };
201
202 bool address_equal(Address *a1, Address *a2) {
203 if (a1 == a2)
204 return true;
205
206 if (!a1 || !a2)
207 return false;
208
209 return address_compare_func(a1, a2) == 0;
210 }
211
212 static int address_establish(Address *address, Link *link) {
213 bool masq;
214 int r;
215
216 assert(address);
217 assert(link);
218
219 masq = link->network &&
220 link->network->ip_masquerade &&
221 address->family == AF_INET &&
222 address->scope < RT_SCOPE_LINK;
223
224 /* Add firewall entry if this is requested */
225 if (address->ip_masquerade_done != masq) {
226 union in_addr_union masked = address->in_addr;
227 in_addr_mask(address->family, &masked, address->prefixlen);
228
229 r = fw_add_masquerade(masq, AF_INET, 0, &masked, address->prefixlen, NULL, NULL, 0);
230 if (r < 0)
231 log_link_warning_errno(link, r, "Could not enable IP masquerading: %m");
232
233 address->ip_masquerade_done = masq;
234 }
235
236 return 0;
237 }
238
239 static int address_add_internal(Link *link, Set **addresses,
240 int family,
241 const union in_addr_union *in_addr,
242 unsigned char prefixlen,
243 Address **ret) {
244 _cleanup_(address_freep) Address *address = NULL;
245 int r;
246
247 assert(link);
248 assert(addresses);
249 assert(in_addr);
250
251 r = address_new(&address);
252 if (r < 0)
253 return r;
254
255 address->family = family;
256 address->in_addr = *in_addr;
257 address->prefixlen = prefixlen;
258 /* Consider address tentative until we get the real flags from the kernel */
259 address->flags = IFA_F_TENTATIVE;
260
261 r = set_ensure_allocated(addresses, &address_hash_ops);
262 if (r < 0)
263 return r;
264
265 r = set_put(*addresses, address);
266 if (r < 0)
267 return r;
268
269 address->link = link;
270
271 if (ret)
272 *ret = address;
273
274 address = NULL;
275
276 return 0;
277 }
278
279 int address_add_foreign(Link *link, int family, const union in_addr_union *in_addr, unsigned char prefixlen, Address **ret) {
280 return address_add_internal(link, &link->addresses_foreign, family, in_addr, prefixlen, ret);
281 }
282
283 int address_add(Link *link, int family, const union in_addr_union *in_addr, unsigned char prefixlen, Address **ret) {
284 Address *address;
285 int r;
286
287 r = address_get(link, family, in_addr, prefixlen, &address);
288 if (r == -ENOENT) {
289 /* Address does not exist, create a new one */
290 r = address_add_internal(link, &link->addresses, family, in_addr, prefixlen, &address);
291 if (r < 0)
292 return r;
293 } else if (r == 0) {
294 /* Take over a foreign address */
295 r = set_ensure_allocated(&link->addresses, &address_hash_ops);
296 if (r < 0)
297 return r;
298
299 r = set_put(link->addresses, address);
300 if (r < 0)
301 return r;
302
303 set_remove(link->addresses_foreign, address);
304 } else if (r == 1) {
305 /* Already exists, do nothing */
306 ;
307 } else
308 return r;
309
310 if (ret)
311 *ret = address;
312
313 return 0;
314 }
315
316 static int address_release(Address *address) {
317 int r;
318
319 assert(address);
320 assert(address->link);
321
322 /* Remove masquerading firewall entry if it was added */
323 if (address->ip_masquerade_done) {
324 union in_addr_union masked = address->in_addr;
325 in_addr_mask(address->family, &masked, address->prefixlen);
326
327 r = fw_add_masquerade(false, AF_INET, 0, &masked, address->prefixlen, NULL, NULL, 0);
328 if (r < 0)
329 log_link_warning_errno(address->link, r, "Failed to disable IP masquerading: %m");
330
331 address->ip_masquerade_done = false;
332 }
333
334 return 0;
335 }
336
337 int address_update(
338 Address *address,
339 unsigned char flags,
340 unsigned char scope,
341 const struct ifa_cacheinfo *cinfo) {
342
343 bool ready;
344 int r;
345
346 assert(address);
347 assert(cinfo);
348 assert_return(address->link, 1);
349
350 if (IN_SET(address->link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
351 return 1;
352
353 ready = address_is_ready(address);
354
355 address->flags = flags;
356 address->scope = scope;
357 address->cinfo = *cinfo;
358
359 link_update_operstate(address->link);
360
361 if (!ready && address_is_ready(address)) {
362 link_check_ready(address->link);
363
364 if (address->family == AF_INET6 &&
365 in_addr_is_link_local(AF_INET6, &address->in_addr) > 0 &&
366 in_addr_is_null(AF_INET6, (const union in_addr_union*) &address->link->ipv6ll_address) > 0) {
367
368 r = link_ipv6ll_gained(address->link, &address->in_addr.in6);
369 if (r < 0)
370 return r;
371 }
372 }
373
374 return 0;
375 }
376
377 int address_drop(Address *address) {
378 Link *link;
379 bool ready;
380
381 assert(address);
382
383 ready = address_is_ready(address);
384 link = address->link;
385
386 address_release(address);
387 address_free(address);
388
389 link_update_operstate(link);
390
391 if (link && !ready)
392 link_check_ready(link);
393
394 return 0;
395 }
396
397 int address_get(Link *link,
398 int family,
399 const union in_addr_union *in_addr,
400 unsigned char prefixlen,
401 Address **ret) {
402
403 Address address, *existing;
404
405 assert(link);
406 assert(in_addr);
407
408 address = (Address) {
409 .family = family,
410 .in_addr = *in_addr,
411 .prefixlen = prefixlen,
412 };
413
414 existing = set_get(link->addresses, &address);
415 if (existing) {
416 if (ret)
417 *ret = existing;
418 return 1;
419 }
420
421 existing = set_get(link->addresses_foreign, &address);
422 if (existing) {
423 if (ret)
424 *ret = existing;
425 return 0;
426 }
427
428 return -ENOENT;
429 }
430
431 int address_remove(
432 Address *address,
433 Link *link,
434 sd_netlink_message_handler_t callback) {
435
436 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
437 _cleanup_free_ char *b = NULL;
438 int r;
439
440 assert(address);
441 assert(IN_SET(address->family, AF_INET, AF_INET6));
442 assert(link);
443 assert(link->ifindex > 0);
444 assert(link->manager);
445 assert(link->manager->rtnl);
446
447 if (DEBUG_LOGGING) {
448 if (in_addr_to_string(address->family, &address->in_addr, &b) >= 0)
449 log_link_debug(link, "Removing address %s", b);
450 }
451
452 r = sd_rtnl_message_new_addr(link->manager->rtnl, &req, RTM_DELADDR,
453 link->ifindex, address->family);
454 if (r < 0)
455 return log_error_errno(r, "Could not allocate RTM_DELADDR message: %m");
456
457 r = sd_rtnl_message_addr_set_prefixlen(req, address->prefixlen);
458 if (r < 0)
459 return log_error_errno(r, "Could not set prefixlen: %m");
460
461 if (address->family == AF_INET)
462 r = sd_netlink_message_append_in_addr(req, IFA_LOCAL, &address->in_addr.in);
463 else if (address->family == AF_INET6)
464 r = sd_netlink_message_append_in6_addr(req, IFA_LOCAL, &address->in_addr.in6);
465 if (r < 0)
466 return log_error_errno(r, "Could not append IFA_LOCAL attribute: %m");
467
468 r = sd_netlink_call_async(link->manager->rtnl, NULL, req, callback,
469 link_netlink_destroy_callback, link, 0, __func__);
470 if (r < 0)
471 return log_error_errno(r, "Could not send rtnetlink message: %m");
472
473 link_ref(link);
474
475 return 0;
476 }
477
478 static int address_acquire(Link *link, Address *original, Address **ret) {
479 union in_addr_union in_addr = {};
480 struct in_addr broadcast = {};
481 _cleanup_(address_freep) Address *na = NULL;
482 int r;
483
484 assert(link);
485 assert(original);
486 assert(ret);
487
488 /* Something useful was configured? just use it */
489 if (in_addr_is_null(original->family, &original->in_addr) <= 0)
490 return 0;
491
492 /* The address is configured to be 0.0.0.0 or [::] by the user?
493 * Then let's acquire something more useful from the pool. */
494 r = manager_address_pool_acquire(link->manager, original->family, original->prefixlen, &in_addr);
495 if (r < 0)
496 return log_link_error_errno(link, r, "Failed to acquire address from pool: %m");
497 if (r == 0) {
498 log_link_error(link, "Couldn't find free address for interface, all taken.");
499 return -EBUSY;
500 }
501
502 if (original->family == AF_INET) {
503 /* Pick first address in range for ourselves ... */
504 in_addr.in.s_addr = in_addr.in.s_addr | htobe32(1);
505
506 /* .. and use last as broadcast address */
507 if (original->prefixlen > 30)
508 broadcast.s_addr = 0;
509 else
510 broadcast.s_addr = in_addr.in.s_addr | htobe32(0xFFFFFFFFUL >> original->prefixlen);
511 } else if (original->family == AF_INET6)
512 in_addr.in6.s6_addr[15] |= 1;
513
514 r = address_new(&na);
515 if (r < 0)
516 return r;
517
518 na->family = original->family;
519 na->prefixlen = original->prefixlen;
520 na->scope = original->scope;
521 na->cinfo = original->cinfo;
522
523 if (original->label) {
524 na->label = strdup(original->label);
525 if (!na->label)
526 return -ENOMEM;
527 }
528
529 na->broadcast = broadcast;
530 na->in_addr = in_addr;
531
532 LIST_PREPEND(addresses, link->pool_addresses, na);
533
534 *ret = TAKE_PTR(na);
535
536 return 0;
537 }
538
539 int address_configure(
540 Address *address,
541 Link *link,
542 sd_netlink_message_handler_t callback,
543 bool update) {
544
545 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
546 int r;
547
548 assert(address);
549 assert(IN_SET(address->family, AF_INET, AF_INET6));
550 assert(link);
551 assert(link->ifindex > 0);
552 assert(link->manager);
553 assert(link->manager->rtnl);
554
555 /* If this is a new address, then refuse adding more than the limit */
556 if (address_get(link, address->family, &address->in_addr, address->prefixlen, NULL) <= 0 &&
557 set_size(link->addresses) >= ADDRESSES_PER_LINK_MAX)
558 return -E2BIG;
559
560 r = address_acquire(link, address, &address);
561 if (r < 0)
562 return r;
563
564 if (update)
565 r = sd_rtnl_message_new_addr_update(link->manager->rtnl, &req,
566 link->ifindex, address->family);
567 else
568 r = sd_rtnl_message_new_addr(link->manager->rtnl, &req, RTM_NEWADDR,
569 link->ifindex, address->family);
570 if (r < 0)
571 return log_error_errno(r, "Could not allocate RTM_NEWADDR message: %m");
572
573 r = sd_rtnl_message_addr_set_prefixlen(req, address->prefixlen);
574 if (r < 0)
575 return log_error_errno(r, "Could not set prefixlen: %m");
576
577 address->flags |= IFA_F_PERMANENT;
578
579 if (address->home_address)
580 address->flags |= IFA_F_HOMEADDRESS;
581
582 if (address->duplicate_address_detection)
583 address->flags |= IFA_F_NODAD;
584
585 if (address->manage_temporary_address)
586 address->flags |= IFA_F_MANAGETEMPADDR;
587
588 if (address->prefix_route)
589 address->flags |= IFA_F_NOPREFIXROUTE;
590
591 if (address->autojoin)
592 address->flags |= IFA_F_MCAUTOJOIN;
593
594 r = sd_rtnl_message_addr_set_flags(req, (address->flags & 0xff));
595 if (r < 0)
596 return log_error_errno(r, "Could not set flags: %m");
597
598 if (address->flags & ~0xff) {
599 r = sd_netlink_message_append_u32(req, IFA_FLAGS, address->flags);
600 if (r < 0)
601 return log_error_errno(r, "Could not set extended flags: %m");
602 }
603
604 r = sd_rtnl_message_addr_set_scope(req, address->scope);
605 if (r < 0)
606 return log_error_errno(r, "Could not set scope: %m");
607
608 if (address->family == AF_INET)
609 r = sd_netlink_message_append_in_addr(req, IFA_LOCAL, &address->in_addr.in);
610 else if (address->family == AF_INET6)
611 r = sd_netlink_message_append_in6_addr(req, IFA_LOCAL, &address->in_addr.in6);
612 if (r < 0)
613 return log_error_errno(r, "Could not append IFA_LOCAL attribute: %m");
614
615 if (!in_addr_is_null(address->family, &address->in_addr_peer)) {
616 if (address->family == AF_INET)
617 r = sd_netlink_message_append_in_addr(req, IFA_ADDRESS, &address->in_addr_peer.in);
618 else if (address->family == AF_INET6)
619 r = sd_netlink_message_append_in6_addr(req, IFA_ADDRESS, &address->in_addr_peer.in6);
620 if (r < 0)
621 return log_error_errno(r, "Could not append IFA_ADDRESS attribute: %m");
622 } else {
623 if (address->family == AF_INET) {
624 if (address->prefixlen <= 30) {
625 r = sd_netlink_message_append_in_addr(req, IFA_BROADCAST, &address->broadcast);
626 if (r < 0)
627 return log_error_errno(r, "Could not append IFA_BROADCAST attribute: %m");
628 }
629 }
630 }
631
632 if (address->label) {
633 r = sd_netlink_message_append_string(req, IFA_LABEL, address->label);
634 if (r < 0)
635 return log_error_errno(r, "Could not append IFA_LABEL attribute: %m");
636 }
637
638 r = sd_netlink_message_append_cache_info(req, IFA_CACHEINFO,
639 &address->cinfo);
640 if (r < 0)
641 return log_error_errno(r, "Could not append IFA_CACHEINFO attribute: %m");
642
643 r = address_establish(address, link);
644 if (r < 0)
645 return r;
646
647 r = sd_netlink_call_async(link->manager->rtnl, NULL, req, callback,
648 link_netlink_destroy_callback, link, 0, __func__);
649 if (r < 0) {
650 address_release(address);
651 return log_error_errno(r, "Could not send rtnetlink message: %m");
652 }
653
654 link_ref(link);
655
656 r = address_add(link, address->family, &address->in_addr, address->prefixlen, NULL);
657 if (r < 0) {
658 address_release(address);
659 return log_error_errno(r, "Could not add address: %m");
660 }
661
662 return 0;
663 }
664
665 int config_parse_broadcast(
666 const char *unit,
667 const char *filename,
668 unsigned line,
669 const char *section,
670 unsigned section_line,
671 const char *lvalue,
672 int ltype,
673 const char *rvalue,
674 void *data,
675 void *userdata) {
676
677 Network *network = userdata;
678 _cleanup_(address_freep) Address *n = NULL;
679 int r;
680
681 assert(filename);
682 assert(section);
683 assert(lvalue);
684 assert(rvalue);
685 assert(data);
686
687 r = address_new_static(network, filename, section_line, &n);
688 if (r < 0)
689 return r;
690
691 if (n->family == AF_INET6) {
692 log_syntax(unit, LOG_ERR, filename, line, 0, "Broadcast is not valid for IPv6 addresses, ignoring assignment: %s", rvalue);
693 return 0;
694 }
695
696 r = in_addr_from_string(AF_INET, rvalue, (union in_addr_union*) &n->broadcast);
697 if (r < 0) {
698 log_syntax(unit, LOG_ERR, filename, line, r, "Broadcast is invalid, ignoring assignment: %s", rvalue);
699 return 0;
700 }
701
702 n->family = AF_INET;
703 n = NULL;
704
705 return 0;
706 }
707
708 int config_parse_address(const char *unit,
709 const char *filename,
710 unsigned line,
711 const char *section,
712 unsigned section_line,
713 const char *lvalue,
714 int ltype,
715 const char *rvalue,
716 void *data,
717 void *userdata) {
718
719 Network *network = userdata;
720 _cleanup_(address_freep) Address *n = NULL;
721 union in_addr_union buffer;
722 unsigned char prefixlen;
723 int r, f;
724
725 assert(filename);
726 assert(section);
727 assert(lvalue);
728 assert(rvalue);
729 assert(data);
730
731 if (streq(section, "Network")) {
732 /* we are not in an Address section, so treat
733 * this as the special '0' section */
734 r = address_new_static(network, NULL, 0, &n);
735 } else
736 r = address_new_static(network, filename, section_line, &n);
737
738 if (r < 0)
739 return r;
740
741 /* Address=address/prefixlen */
742 r = in_addr_default_prefix_from_string_auto(rvalue, &f, &buffer, &prefixlen);
743 if (r < 0) {
744 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid address '%s', ignoring assignment: %m", rvalue);
745 return 0;
746 }
747
748 if (n->family != AF_UNSPEC && f != n->family) {
749 log_syntax(unit, LOG_ERR, filename, line, 0, "Address is incompatible, ignoring assignment: %s", rvalue);
750 return 0;
751 }
752
753 n->family = f;
754 n->prefixlen = prefixlen;
755
756 if (streq(lvalue, "Address"))
757 n->in_addr = buffer;
758 else
759 n->in_addr_peer = buffer;
760
761 if (n->family == AF_INET && n->broadcast.s_addr == 0)
762 n->broadcast.s_addr = n->in_addr.in.s_addr | htonl(0xfffffffflu >> n->prefixlen);
763
764 n = NULL;
765
766 return 0;
767 }
768
769 int config_parse_label(
770 const char *unit,
771 const char *filename,
772 unsigned line,
773 const char *section,
774 unsigned section_line,
775 const char *lvalue,
776 int ltype,
777 const char *rvalue,
778 void *data,
779 void *userdata) {
780
781 _cleanup_(address_freep) Address *n = NULL;
782 Network *network = userdata;
783 int r;
784
785 assert(filename);
786 assert(section);
787 assert(lvalue);
788 assert(rvalue);
789 assert(data);
790
791 r = address_new_static(network, filename, section_line, &n);
792 if (r < 0)
793 return r;
794
795 if (!address_label_valid(rvalue)) {
796 log_syntax(unit, LOG_ERR, filename, line, 0, "Interface label is too long or invalid, ignoring assignment: %s", rvalue);
797 return 0;
798 }
799
800 r = free_and_strdup(&n->label, rvalue);
801 if (r < 0)
802 return log_oom();
803
804 n = NULL;
805
806 return 0;
807 }
808
809 int config_parse_lifetime(const char *unit,
810 const char *filename,
811 unsigned line,
812 const char *section,
813 unsigned section_line,
814 const char *lvalue,
815 int ltype,
816 const char *rvalue,
817 void *data,
818 void *userdata) {
819 Network *network = userdata;
820 _cleanup_(address_freep) Address *n = NULL;
821 unsigned k;
822 int r;
823
824 assert(filename);
825 assert(section);
826 assert(lvalue);
827 assert(rvalue);
828 assert(data);
829
830 r = address_new_static(network, filename, section_line, &n);
831 if (r < 0)
832 return r;
833
834 if (STR_IN_SET(rvalue, "forever", "infinity")) {
835 n->cinfo.ifa_prefered = CACHE_INFO_INFINITY_LIFE_TIME;
836 n = NULL;
837
838 return 0;
839 }
840
841 r = safe_atou(rvalue, &k);
842 if (r < 0) {
843 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse PreferredLifetime, ignoring: %s", rvalue);
844 return 0;
845 }
846
847 if (k != 0)
848 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid PreferredLifetime value, ignoring: %d", k);
849 else {
850 n->cinfo.ifa_prefered = k;
851 n = NULL;
852 }
853
854 return 0;
855 }
856
857 int config_parse_address_flags(const char *unit,
858 const char *filename,
859 unsigned line,
860 const char *section,
861 unsigned section_line,
862 const char *lvalue,
863 int ltype,
864 const char *rvalue,
865 void *data,
866 void *userdata) {
867 Network *network = userdata;
868 _cleanup_(address_freep) Address *n = NULL;
869 int r;
870
871 assert(filename);
872 assert(section);
873 assert(lvalue);
874 assert(rvalue);
875 assert(data);
876
877 r = address_new_static(network, filename, section_line, &n);
878 if (r < 0)
879 return r;
880
881 r = parse_boolean(rvalue);
882 if (r < 0) {
883 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse address flag, ignoring: %s", rvalue);
884 return 0;
885 }
886
887 if (streq(lvalue, "HomeAddress"))
888 n->home_address = r;
889 else if (streq(lvalue, "DuplicateAddressDetection"))
890 n->duplicate_address_detection = r;
891 else if (streq(lvalue, "ManageTemporaryAddress"))
892 n->manage_temporary_address = r;
893 else if (streq(lvalue, "PrefixRoute"))
894 n->prefix_route = r;
895 else if (streq(lvalue, "AutoJoin"))
896 n->autojoin = r;
897
898 return 0;
899 }
900
901 int config_parse_address_scope(const char *unit,
902 const char *filename,
903 unsigned line,
904 const char *section,
905 unsigned section_line,
906 const char *lvalue,
907 int ltype,
908 const char *rvalue,
909 void *data,
910 void *userdata) {
911 Network *network = userdata;
912 _cleanup_(address_freep) Address *n = NULL;
913 int r;
914
915 assert(filename);
916 assert(section);
917 assert(lvalue);
918 assert(rvalue);
919 assert(data);
920
921 r = address_new_static(network, filename, section_line, &n);
922 if (r < 0)
923 return r;
924
925 if (streq(rvalue, "host"))
926 n->scope = RT_SCOPE_HOST;
927 else if (streq(rvalue, "link"))
928 n->scope = RT_SCOPE_LINK;
929 else if (streq(rvalue, "global"))
930 n->scope = RT_SCOPE_UNIVERSE;
931 else {
932 r = safe_atou8(rvalue , &n->scope);
933 if (r < 0) {
934 log_syntax(unit, LOG_ERR, filename, line, r, "Could not parse address scope \"%s\", ignoring assignment: %m", rvalue);
935 return 0;
936 }
937 }
938
939 n = NULL;
940
941 return 0;
942 }
943
944 bool address_is_ready(const Address *a) {
945 assert(a);
946
947 if (a->family == AF_INET6)
948 return !(a->flags & IFA_F_TENTATIVE);
949 else
950 return !(a->flags & (IFA_F_TENTATIVE | IFA_F_DEPRECATED));
951 }