Debian Codegraph @

src/network/networkd-address.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <net/if.h>
4 #include <stdio.h>
5
6 #include "sd-event.h"
7 #include "sd-netlink.h"
8
9 #include "af-list.h"
10 #include "alloc-util.h"
11 #include "bitfield.h"
12 #include "conf-parser.h"
13 #include "errno-util.h"
14 #include "firewall-util.h"
15 #include "in-addr-prefix-util.h"
16 #include "logarithm.h"
17 #include "netlink-util.h"
18 #include "networkd-address-generation.h"
19 #include "networkd-address.h"
20 #include "networkd-address-pool.h"
21 #include "networkd-dhcp-prefix-delegation.h"
22 #include "networkd-dhcp-relay.h"
23 #include "networkd-dhcp-server.h"
24 #include "networkd-ipv4acd.h"
25 #include "networkd-link.h"
26 #include "networkd-manager.h"
27 #include "networkd-ndisc.h"
28 #include "networkd-netlabel.h"
29 #include "networkd-network.h"
30 #include "networkd-queue.h"
31 #include "networkd-route.h"
32 #include "networkd-route-util.h"
33 #include "ordered-set.h"
34 #include "parse-util.h"
35 #include "set.h"
36 #include "siphash24.h"
37 #include "socket-util.h"
38 #include "string-util.h"
39 #include "strv.h"
40
41 #define ADDRESSES_PER_LINK_MAX 16384U
42 #define STATIC_ADDRESSES_PER_NETWORK_MAX 8192U
43
44 #define KNOWN_FLAGS \
45 (IFA_F_SECONDARY | \
46 IFA_F_NODAD | \
47 IFA_F_OPTIMISTIC | \
48 IFA_F_DADFAILED | \
49 IFA_F_HOMEADDRESS | \
50 IFA_F_DEPRECATED | \
51 IFA_F_TENTATIVE | \
52 IFA_F_PERMANENT | \
53 IFA_F_MANAGETEMPADDR | \
54 IFA_F_NOPREFIXROUTE | \
55 IFA_F_MCAUTOJOIN | \
56 IFA_F_STABLE_PRIVACY)
57
58 /* From net/ipv4/devinet.c */
59 #define IPV6ONLY_FLAGS \
60 (IFA_F_NODAD | \
61 IFA_F_OPTIMISTIC | \
62 IFA_F_DADFAILED | \
63 IFA_F_HOMEADDRESS | \
64 IFA_F_TENTATIVE | \
65 IFA_F_MANAGETEMPADDR | \
66 IFA_F_STABLE_PRIVACY)
67
68 /* We do not control the following flags. */
69 #define UNMANAGED_FLAGS \
70 (IFA_F_SECONDARY | \
71 IFA_F_DADFAILED | \
72 IFA_F_DEPRECATED | \
73 IFA_F_TENTATIVE | \
74 IFA_F_PERMANENT | \
75 IFA_F_STABLE_PRIVACY)
76
77 int address_flags_to_string_alloc(uint32_t flags, int family, char **ret) {
78 _cleanup_free_ char *str = NULL;
79 static const char* map[] = {
80 [LOG2U(IFA_F_SECONDARY)] = "secondary", /* This is also called "temporary" for ipv6. */
81 [LOG2U(IFA_F_NODAD)] = "nodad",
82 [LOG2U(IFA_F_OPTIMISTIC)] = "optimistic",
83 [LOG2U(IFA_F_DADFAILED)] = "dadfailed",
84 [LOG2U(IFA_F_HOMEADDRESS)] = "home-address",
85 [LOG2U(IFA_F_DEPRECATED)] = "deprecated",
86 [LOG2U(IFA_F_TENTATIVE)] = "tentative",
87 [LOG2U(IFA_F_PERMANENT)] = "permanent",
88 [LOG2U(IFA_F_MANAGETEMPADDR)] = "manage-temporary-address",
89 [LOG2U(IFA_F_NOPREFIXROUTE)] = "no-prefixroute",
90 [LOG2U(IFA_F_MCAUTOJOIN)] = "auto-join",
91 [LOG2U(IFA_F_STABLE_PRIVACY)] = "stable-privacy",
92 };
93
94 assert(IN_SET(family, AF_INET, AF_INET6));
95 assert(ret);
96
97 for (size_t i = 0; i < ELEMENTSOF(map); i++)
98 if (BIT_SET(flags, i) && map[i])
99 if (!strextend_with_separator(
100 &str, ",",
101 family == AF_INET6 && (1 << i) == IFA_F_SECONDARY ? "temporary" : map[i]))
102 return -ENOMEM;
103
104 *ret = TAKE_PTR(str);
105 return 0;
106 }
107
108 static LinkAddressState address_state_from_scope(uint8_t scope) {
109 if (scope < RT_SCOPE_SITE)
110 /* universally accessible addresses found */
111 return LINK_ADDRESS_STATE_ROUTABLE;
112
113 if (scope < RT_SCOPE_HOST)
114 /* only link or site local addresses found */
115 return LINK_ADDRESS_STATE_DEGRADED;
116
117 /* no useful addresses found */
118 return LINK_ADDRESS_STATE_OFF;
119 }
120
121 void link_get_address_states(
122 Link *link,
123 LinkAddressState *ret_ipv4,
124 LinkAddressState *ret_ipv6,
125 LinkAddressState *ret_all) {
126
127 uint8_t ipv4_scope = RT_SCOPE_NOWHERE, ipv6_scope = RT_SCOPE_NOWHERE;
128 Address *address;
129
130 assert(link);
131
132 SET_FOREACH(address, link->addresses) {
133 if (!address_is_ready(address))
134 continue;
135
136 if (address->family == AF_INET)
137 ipv4_scope = MIN(ipv4_scope, address->scope);
138
139 if (address->family == AF_INET6)
140 ipv6_scope = MIN(ipv6_scope, address->scope);
141 }
142
143 if (ret_ipv4)
144 *ret_ipv4 = address_state_from_scope(ipv4_scope);
145 if (ret_ipv6)
146 *ret_ipv6 = address_state_from_scope(ipv6_scope);
147 if (ret_all)
148 *ret_all = address_state_from_scope(MIN(ipv4_scope, ipv6_scope));
149 }
150
151 static void address_detach(Address *address);
152
153 DEFINE_PRIVATE_HASH_OPS_WITH_KEY_DESTRUCTOR(
154 address_hash_ops_detach,
155 Address,
156 address_hash_func,
157 address_compare_func,
158 address_detach);
159
160 DEFINE_HASH_OPS(
161 address_hash_ops,
162 Address,
163 address_hash_func,
164 address_compare_func);
165
166 DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
167 address_section_hash_ops,
168 ConfigSection,
169 config_section_hash_func,
170 config_section_compare_func,
171 Address,
172 address_detach);
173
174 int address_new(Address **ret) {
175 _cleanup_(address_unrefp) Address *address = NULL;
176
177 assert(ret);
178
179 address = new(Address, 1);
180 if (!address)
181 return -ENOMEM;
182
183 *address = (Address) {
184 .n_ref = 1,
185 .family = AF_UNSPEC,
186 .scope = RT_SCOPE_UNIVERSE,
187 .lifetime_valid_usec = USEC_INFINITY,
188 .lifetime_preferred_usec = USEC_INFINITY,
189 .set_broadcast = -1,
190 };
191
192 *ret = TAKE_PTR(address);
193
194 return 0;
195 }
196
197 int address_new_static(Network *network, const char *filename, unsigned section_line, Address **ret) {
198 _cleanup_(config_section_freep) ConfigSection *n = NULL;
199 _cleanup_(address_unrefp) Address *address = NULL;
200 int r;
201
202 assert(network);
203 assert(ret);
204 assert(filename);
205 assert(section_line > 0);
206
207 r = config_section_new(filename, section_line, &n);
208 if (r < 0)
209 return r;
210
211 address = ordered_hashmap_get(network->addresses_by_section, n);
212 if (address) {
213 *ret = TAKE_PTR(address);
214 return 0;
215 }
216
217 if (ordered_hashmap_size(network->addresses_by_section) >= STATIC_ADDRESSES_PER_NETWORK_MAX)
218 return -E2BIG;
219
220 r = address_new(&address);
221 if (r < 0)
222 return r;
223
224 address->network = network;
225 address->section = TAKE_PTR(n);
226 address->source = NETWORK_CONFIG_SOURCE_STATIC;
227 /* This will be adjusted in address_section_verify(). */
228 address->duplicate_address_detection = _ADDRESS_FAMILY_INVALID;
229
230 r = ordered_hashmap_ensure_put(&network->addresses_by_section, &address_section_hash_ops, address->section, address);
231 if (r < 0)
232 return r;
233
234 *ret = TAKE_PTR(address);
235 return 0;
236 }
237
238 static Address* address_detach_impl(Address *address) {
239 assert(address);
240 assert(!address->link || !address->network);
241
242 if (address->network) {
243 assert(address->section);
244 ordered_hashmap_remove(address->network->addresses_by_section, address->section);
245
246 if (address->network->dhcp_server_address == address)
247 address->network->dhcp_server_address = NULL;
248
249 if (address->network->dhcp_relay_agent_address == address)
250 address->network->dhcp_relay_agent_address = NULL;
251
252 address->network = NULL;
253 return address;
254 }
255
256 if (address->link) {
257 set_remove(address->link->addresses, address);
258
259 address->link = NULL;
260 return address;
261 }
262
263 return NULL;
264 }
265
266 static void address_detach(Address *address) {
267 assert(address);
268
269 address_unref(address_detach_impl(address));
270 }
271
272 static Address* address_free(Address *address) {
273 if (!address)
274 return NULL;
275
276 address_detach_impl(address);
277
278 config_section_free(address->section);
279 free(address->label);
280 free(address->netlabel);
281 ipv6_token_unref(address->token);
282 nft_set_context_clear(&address->nft_set_context);
283 return mfree(address);
284 }
285
286 DEFINE_TRIVIAL_REF_UNREF_FUNC(Address, address, address_free);
287
288 static bool address_lifetime_is_valid(const Address *a) {
289 assert(a);
290
291 return
292 a->lifetime_valid_usec == USEC_INFINITY ||
293 a->lifetime_valid_usec > now(CLOCK_BOOTTIME);
294 }
295
296 bool address_is_ready(const Address *a) {
297 assert(a);
298 assert(a->link);
299
300 if (!ipv4acd_bound(a->link, a))
301 return false;
302
303 if (FLAGS_SET(a->flags, IFA_F_TENTATIVE))
304 return false;
305
306 if (FLAGS_SET(a->state, NETWORK_CONFIG_STATE_REMOVING))
307 return false;
308
309 if (!FLAGS_SET(a->state, NETWORK_CONFIG_STATE_CONFIGURED))
310 return false;
311
312 return address_lifetime_is_valid(a);
313 }
314
315 bool link_check_addresses_ready(Link *link, NetworkConfigSource source) {
316 Address *a;
317 bool has = false;
318
319 assert(link);
320
321 /* Check if all addresses on the interface are ready. If there is no address, this will return false. */
322
323 SET_FOREACH(a, link->addresses) {
324 if (source >= 0 && a->source != source)
325 continue;
326 if (address_is_marked(a))
327 continue;
328 if (!address_exists(a))
329 continue;
330 if (!address_is_ready(a))
331 return false;
332 has = true;
333 }
334
335 if (has || source != NETWORK_CONFIG_SOURCE_DHCP6)
336 return has;
337
338 /* If there is no DHCPv6 addresses, but all conflicting addresses are successfully configured, then
339 * let's handle the DHCPv6 client successfully configured addresses. Otherwise, if the conflicted
340 * addresses are static or foreign, and there is no other dynamic addressing protocol enabled, then
341 * the link will never enter the configured state. See also link_check_ready(). */
342 SET_FOREACH(a, link->addresses) {
343 if (source >= 0 && a->source == NETWORK_CONFIG_SOURCE_DHCP6)
344 continue;
345 if (!a->also_requested_by_dhcp6)
346 continue;
347 if (address_is_marked(a))
348 continue;
349 if (!address_exists(a))
350 continue;
351 if (!address_is_ready(a))
352 return false;
353 has = true;
354 }
355
356 return has;
357 }
358
359 void link_mark_addresses(Link *link, NetworkConfigSource source) {
360 Address *a;
361
362 assert(link);
363
364 SET_FOREACH(a, link->addresses) {
365 if (a->source != source)
366 continue;
367
368 address_mark(a);
369 }
370 }
371
372 static int address_get_broadcast(const Address *a, Link *link, struct in_addr *ret) {
373 struct in_addr b_addr = {};
374
375 assert(a);
376 assert(link);
377
378 /* Returns 0 when broadcast address is null, 1 when non-null broadcast address, -EAGAIN when the main
379 * address is null. */
380
381 /* broadcast is only for IPv4. */
382 if (a->family != AF_INET)
383 goto finalize;
384
385 /* broadcast address cannot be used when peer address is specified. */
386 if (in4_addr_is_set(&a->in_addr_peer.in))
387 goto finalize;
388
389 /* A /31 or /32 IPv4 address does not have a broadcast address.
390 * See https://tools.ietf.org/html/rfc3021 */
391 if (a->prefixlen > 30)
392 goto finalize;
393
394 /* If explicitly configured, use the address as is. */
395 if (in4_addr_is_set(&a->broadcast)) {
396 b_addr = a->broadcast;
397 goto finalize;
398 }
399
400 /* If explicitly disabled, then return null address. */
401 if (a->set_broadcast == 0)
402 goto finalize;
403
404 /* For wireguard interfaces, broadcast is disabled by default. */
405 if (a->set_broadcast < 0 && streq_ptr(link->kind, "wireguard"))
406 goto finalize;
407
408 /* If the main address is null, e.g. Address=0.0.0.0/24, the broadcast address will be automatically
409 * determined after an address is acquired. */
410 if (!in4_addr_is_set(&a->in_addr.in))
411 return -EAGAIN;
412
413 /* Otherwise, generate a broadcast address from the main address and prefix length. */
414 b_addr.s_addr = a->in_addr.in.s_addr | htobe32(UINT32_C(0xffffffff) >> a->prefixlen);
415
416 finalize:
417 if (ret)
418 *ret = b_addr;
419
420 return in4_addr_is_set(&b_addr);
421 }
422
423 static void address_set_broadcast(Address *a, Link *link) {
424 assert(a);
425 assert_se(address_get_broadcast(a, link, &a->broadcast) >= 0);
426 }
427
428 static void address_set_cinfo(Manager *m, const Address *a, struct ifa_cacheinfo *cinfo) {
429 usec_t now_usec;
430
431 assert(m);
432 assert(a);
433 assert(cinfo);
434
435 assert_se(sd_event_now(m->event, CLOCK_BOOTTIME, &now_usec) >= 0);
436
437 *cinfo = (struct ifa_cacheinfo) {
438 .ifa_valid = usec_to_sec(a->lifetime_valid_usec, now_usec),
439 .ifa_prefered = usec_to_sec(a->lifetime_preferred_usec, now_usec),
440 };
441 }
442
443 static void address_set_lifetime(Manager *m, Address *a, const struct ifa_cacheinfo *cinfo) {
444 usec_t now_usec;
445
446 assert(m);
447 assert(a);
448 assert(cinfo);
449
450 assert_se(sd_event_now(m->event, CLOCK_BOOTTIME, &now_usec) >= 0);
451
452 a->lifetime_valid_usec = sec_to_usec(cinfo->ifa_valid, now_usec);
453 a->lifetime_preferred_usec = sec_to_usec(cinfo->ifa_prefered, now_usec);
454 }
455
456 static bool address_is_static_null(const Address *address) {
457 assert(address);
458
459 if (!address->network)
460 return false;
461
462 if (!address->requested_as_null)
463 return false;
464
465 assert(!in_addr_is_set(address->family, &address->in_addr));
466 return true;
467 }
468
469 static int address_ipv4_prefix(const Address *a, struct in_addr *ret) {
470 struct in_addr p;
471 int r;
472
473 assert(a);
474 assert(a->family == AF_INET);
475 assert(ret);
476
477 p = in4_addr_is_set(&a->in_addr_peer.in) ? a->in_addr_peer.in : a->in_addr.in;
478 r = in4_addr_mask(&p, a->prefixlen);
479 if (r < 0)
480 return r;
481
482 *ret = p;
483 return 0;
484 }
485
486 void address_hash_func(const Address *a, struct siphash *state) {
487 assert(a);
488
489 siphash24_compress_typesafe(a->family, state);
490
491 switch (a->family) {
492 case AF_INET: {
493 struct in_addr prefix;
494
495 siphash24_compress_typesafe(a->prefixlen, state);
496
497 assert_se(address_ipv4_prefix(a, &prefix) >= 0);
498 siphash24_compress_typesafe(prefix, state);
499
500 siphash24_compress_typesafe(a->in_addr.in, state);
501 break;
502 }
503 case AF_INET6:
504 siphash24_compress_typesafe(a->in_addr.in6, state);
505
506 if (in6_addr_is_null(&a->in_addr.in6))
507 siphash24_compress_typesafe(a->prefixlen, state);
508 break;
509
510 default:
511 assert_not_reached();
512 }
513 }
514
515 int address_compare_func(const Address *a1, const Address *a2) {
516 int r;
517
518 r = CMP(a1->family, a2->family);
519 if (r != 0)
520 return r;
521
522 switch (a1->family) {
523 case AF_INET: {
524 struct in_addr p1, p2;
525
526 /* See kernel's find_matching_ifa() in net/ipv4/devinet.c */
527 r = CMP(a1->prefixlen, a2->prefixlen);
528 if (r != 0)
529 return r;
530
531 assert_se(address_ipv4_prefix(a1, &p1) >= 0);
532 assert_se(address_ipv4_prefix(a2, &p2) >= 0);
533 r = memcmp(&p1, &p2, sizeof(p1));
534 if (r != 0)
535 return r;
536
537 return memcmp(&a1->in_addr.in, &a2->in_addr.in, sizeof(a1->in_addr.in));
538 }
539 case AF_INET6:
540 /* See kernel's ipv6_get_ifaddr() in net/ipv6/addrconf.c */
541 r = memcmp(&a1->in_addr.in6, &a2->in_addr.in6, sizeof(a1->in_addr.in6));
542 if (r != 0)
543 return r;
544
545 /* To distinguish IPv6 null addresses with different prefixlen, e.g. ::48 vs ::64, let's
546 * compare the prefix length. */
547 if (in6_addr_is_null(&a1->in_addr.in6))
548 r = CMP(a1->prefixlen, a2->prefixlen);
549
550 return r;
551
552 default:
553 assert_not_reached();
554 }
555 }
556
557 bool address_can_update(const Address *existing, const Address *requesting) {
558 assert(existing);
559 assert(requesting);
560
561 /*
562 * property | IPv4 | IPv6
563 * -----------------------------------------
564 * family | ✗ | ✗
565 * prefixlen | ✗ | ✗
566 * address | ✗ | ✗
567 * scope | ✗ | -
568 * label | ✗ | -
569 * broadcast | ✗ | -
570 * peer | ✗ | ✓
571 * flags | ✗ | ✓
572 * lifetime | ✓ | ✓
573 * route metric | ✓ | ✓
574 * protocol | ✓ | ✓
575 *
576 * ✗ : cannot be changed
577 * ✓ : can be changed
578 * - : unused
579 *
580 * IPv4 : See inet_rtm_newaddr() in net/ipv4/devinet.c.
581 * IPv6 : See inet6_addr_modify() in net/ipv6/addrconf.c.
582 */
583
584 if (existing->family != requesting->family)
585 return false;
586
587 if (existing->prefixlen != requesting->prefixlen)
588 return false;
589
590 /* When a null address is requested, the address to be assigned/updated will be determined later. */
591 if (!address_is_static_null(requesting) &&
592 in_addr_equal(existing->family, &existing->in_addr, &requesting->in_addr) <= 0)
593 return false;
594
595 switch (existing->family) {
596 case AF_INET: {
597 struct in_addr bcast;
598
599 if (existing->scope != requesting->scope)
600 return false;
601 if (((existing->flags ^ requesting->flags) & KNOWN_FLAGS & ~IPV6ONLY_FLAGS & ~UNMANAGED_FLAGS) != 0)
602 return false;
603 if (!streq_ptr(existing->label, requesting->label))
604 return false;
605 if (!in4_addr_equal(&existing->in_addr_peer.in, &requesting->in_addr_peer.in))
606 return false;
607 if (existing->link && address_get_broadcast(requesting, existing->link, &bcast) >= 0) {
608 /* If the broadcast address can be determined now, check if they match. */
609 if (!in4_addr_equal(&existing->broadcast, &bcast))
610 return false;
611 } else {
612 /* When a null address is requested, then the broadcast address will be
613 * automatically calculated from the acquired address, e.g.
614 * 192.168.0.10/24 -> 192.168.0.255
615 * So, here let's only check if the broadcast is the last address in the range, e.g.
616 * 0.0.0.0/24 -> 0.0.0.255 */
617 if (!FLAGS_SET(existing->broadcast.s_addr, htobe32(UINT32_C(0xffffffff) >> existing->prefixlen)))
618 return false;
619 }
620 break;
621 }
622 case AF_INET6:
623 break;
624
625 default:
626 assert_not_reached();
627 }
628
629 return true;
630 }
631
632 int address_dup(const Address *src, Address **ret) {
633 _cleanup_(address_unrefp) Address *dest = NULL;
634 int r;
635
636 assert(src);
637 assert(ret);
638
639 dest = newdup(Address, src, 1);
640 if (!dest)
641 return -ENOMEM;
642
643 /* clear the reference counter and all pointers */
644 dest->n_ref = 1;
645 dest->network = NULL;
646 dest->section = NULL;
647 dest->link = NULL;
648 dest->label = NULL;
649 dest->token = ipv6_token_ref(src->token);
650 dest->netlabel = NULL;
651 dest->nft_set_context.sets = NULL;
652 dest->nft_set_context.n_sets = 0;
653
654 if (src->family == AF_INET) {
655 r = strdup_to(&dest->label, src->label);
656 if (r < 0)
657 return r;
658 }
659
660 r = strdup_to(&dest->netlabel, src->netlabel);
661 if (r < 0)
662 return r;
663
664 r = nft_set_context_dup(&src->nft_set_context, &dest->nft_set_context);
665 if (r < 0)
666 return r;
667
668 *ret = TAKE_PTR(dest);
669 return 0;
670 }
671
672 static int address_set_masquerade(Address *address, bool add) {
673 union in_addr_union masked;
674 int r;
675
676 assert(address);
677 assert(address->link);
678 assert(address->link->manager);
679
680 if (!address->link->manager->nfnl)
681 return 0;
682
683 if (!address->link->network)
684 return 0;
685
686 if (!FLAGS_SET(address->link->network->ip_masquerade, AF_TO_ADDRESS_FAMILY(address->family)))
687 return 0;
688
689 if (address->scope >= RT_SCOPE_LINK)
690 return 0;
691
692 if (address->ip_masquerade_done == add)
693 return 0;
694
695 masked = address->in_addr;
696 r = in_addr_mask(address->family, &masked, address->prefixlen);
697 if (r < 0)
698 return r;
699
700 r = fw_nftables_add_masquerade(address->link->manager->nfnl, add, address->family, &masked, address->prefixlen);
701 if (r < 0)
702 return r;
703
704 address->ip_masquerade_done = add;
705
706 return 0;
707 }
708
709 static void address_modify_nft_set_context(Address *address, bool add, NFTSetContext *nft_set_context) {
710 int r;
711
712 assert(address);
713 assert(address->link);
714 assert(address->link->manager);
715 assert(address->link->manager->nfnl);
716 assert(nft_set_context);
717
718 FOREACH_ARRAY(nft_set, nft_set_context->sets, nft_set_context->n_sets) {
719 uint32_t ifindex;
720
721 assert(nft_set);
722
723 switch (nft_set->source) {
724 case NFT_SET_SOURCE_ADDRESS:
725 r = nft_set_element_modify_ip(address->link->manager->nfnl, add, nft_set->nfproto, address->family, nft_set->table, nft_set->set,
726 &address->in_addr);
727 break;
728 case NFT_SET_SOURCE_PREFIX:
729 r = nft_set_element_modify_iprange(address->link->manager->nfnl, add, nft_set->nfproto, address->family, nft_set->table, nft_set->set,
730 &address->in_addr, address->prefixlen);
731 break;
732 case NFT_SET_SOURCE_IFINDEX:
733 ifindex = address->link->ifindex;
734 r = nft_set_element_modify_any(address->link->manager->nfnl, add, nft_set->nfproto, nft_set->table, nft_set->set,
735 &ifindex, sizeof(ifindex));
736 break;
737 default:
738 assert_not_reached();
739 }
740
741 if (r < 0)
742 log_warning_errno(r, "Failed to %s NFT set entry: family %s, table %s, set %s, IP address %s, ignoring: %m",
743 add ? "add" : "delete",
744 nfproto_to_string(nft_set->nfproto), nft_set->table, nft_set->set,
745 IN_ADDR_PREFIX_TO_STRING(address->family, &address->in_addr, address->prefixlen));
746 else
747 log_debug("%s NFT set entry: family %s, table %s, set %s, IP address %s",
748 add ? "Added" : "Deleted",
749 nfproto_to_string(nft_set->nfproto), nft_set->table, nft_set->set,
750 IN_ADDR_PREFIX_TO_STRING(address->family, &address->in_addr, address->prefixlen));
751 }
752 }
753
754 static void address_modify_nft_set(Address *address, bool add) {
755 assert(address);
756 assert(address->link);
757 assert(address->link->manager);
758
759 if (!address->link->manager->nfnl)
760 return;
761
762 if (!IN_SET(address->family, AF_INET, AF_INET6))
763 return;
764
765 if (!address->link->network)
766 return;
767
768 switch (address->source) {
769 case NETWORK_CONFIG_SOURCE_DHCP4:
770 return address_modify_nft_set_context(address, add, &address->link->network->dhcp_nft_set_context);
771 case NETWORK_CONFIG_SOURCE_DHCP6:
772 return address_modify_nft_set_context(address, add, &address->link->network->dhcp6_nft_set_context);
773 case NETWORK_CONFIG_SOURCE_DHCP_PD:
774 return address_modify_nft_set_context(address, add, &address->link->network->dhcp_pd_nft_set_context);
775 case NETWORK_CONFIG_SOURCE_NDISC:
776 return address_modify_nft_set_context(address, add, &address->link->network->ndisc_nft_set_context);
777 case NETWORK_CONFIG_SOURCE_STATIC:
778 return address_modify_nft_set_context(address, add, &address->nft_set_context);
779 default:
780 return;
781 }
782 }
783
784 static int address_attach(Link *link, Address *address) {
785 int r;
786
787 assert(link);
788 assert(address);
789 assert(!address->link);
790
791 r = set_ensure_put(&link->addresses, &address_hash_ops_detach, address);
792 if (r < 0)
793 return r;
794 if (r == 0)
795 return -EEXIST;
796
797 address->link = link;
798 address_ref(address);
799 return 0;
800 }
801
802 static int address_update(Address *address) {
803 Link *link = ASSERT_PTR(ASSERT_PTR(address)->link);
804 int r;
805
806 if (address_is_ready(address) &&
807 address->family == AF_INET6 &&
808 in6_addr_is_link_local(&address->in_addr.in6) &&
809 in6_addr_is_null(&link->ipv6ll_address)) {
810
811 link->ipv6ll_address = address->in_addr.in6;
812
813 r = link_ipv6ll_gained(link);
814 if (r < 0)
815 return r;
816 }
817
818 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
819 return 0;
820
821 r = address_set_masquerade(address, /* add= */ true);
822 if (r < 0)
823 return log_link_warning_errno(link, r, "Could not enable IP masquerading: %m");
824
825 address_add_netlabel(address);
826
827 address_modify_nft_set(address, /* add= */ true);
828
829 if (address_is_ready(address) && address->callback) {
830 r = address->callback(address);
831 if (r < 0)
832 return r;
833 }
834
835 link_update_operstate(link, /* also_update_master= */ true);
836 link_check_ready(link);
837 return 0;
838 }
839
840 static int address_removed_maybe_kernel_dad(Link *link, Address *address) {
841 int r;
842
843 assert(link);
844 assert(address);
845
846 if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED))
847 return 0;
848
849 if (address->family != AF_INET6)
850 return 0;
851
852 if (!FLAGS_SET(address->flags, IFA_F_TENTATIVE))
853 return 0;
854
855 log_link_info(link, "Address %s with tentative flag is removed, maybe a duplicated address is assigned on another node or link?",
856 IN6_ADDR_TO_STRING(&address->in_addr.in6));
857
858 /* Reset the address state, as the object may be reused in the below. */
859 address->state = 0;
860
861 switch (address->source) {
862 case NETWORK_CONFIG_SOURCE_STATIC:
863 r = link_reconfigure_radv_address(address, link);
864 break;
865 case NETWORK_CONFIG_SOURCE_DHCP_PD:
866 r = dhcp_pd_reconfigure_address(address, link);
867 break;
868 case NETWORK_CONFIG_SOURCE_NDISC:
869 r = ndisc_reconfigure_address(address, link);
870 break;
871 default:
872 r = 0;
873 }
874 if (r < 0)
875 return log_link_warning_errno(link, r, "Failed to configure an alternative address: %m");
876
877 return 0;
878 }
879
880 static int address_drop(Address *in, bool removed_by_us) {
881 _cleanup_(address_unrefp) Address *address = address_ref(ASSERT_PTR(in));
882 Link *link = ASSERT_PTR(address->link);
883 int r;
884
885 r = address_set_masquerade(address, /* add= */ false);
886 if (r < 0)
887 log_link_warning_errno(link, r, "Failed to disable IP masquerading, ignoring: %m");
888
889 address_modify_nft_set(address, /* add= */ false);
890
891 address_del_netlabel(address);
892
893 if (address->family == AF_INET6 &&
894 in6_addr_equal(&address->in_addr.in6, &link->ipv6ll_address)) {
895 link->ipv6ll_address = (const struct in6_addr) {};
896
897 Address *a;
898 bool has_replacement;
899
900 /* If another ready IPv6LL address exists on this link, use it instead. */
901 SET_FOREACH(a, link->addresses) {
902 if (a == address)
903 continue;
904 if (a->family != AF_INET6)
905 continue;
906 if (!in6_addr_is_link_local(&a->in_addr.in6))
907 continue;
908 if (!address_is_ready(a))
909 continue;
910 link->ipv6ll_address = a->in_addr.in6;
911 break;
912 }
913
914 has_replacement = in6_addr_is_set(&link->ipv6ll_address);
915
916 /* Stop engines bound to the dropped IPv6LL source address. Do not return early on error.
917 * address_detach() and link_update_operstate() must run to keep link state consistent. */
918 r = link_ipv6ll_lost(link, &address->in_addr.in6, has_replacement);
919 if (r < 0)
920 log_link_warning_errno(link, r, "Failed to stop IPv6 services after IPv6LL loss, ignoring: %m");
921
922 /* If another IPv6LL address is available, restart engines with it. */
923 if (has_replacement) {
924 r = link_ipv6ll_gained(link);
925 if (r < 0)
926 log_link_warning_errno(link, r, "Failed to restart IPv6 services with alternate IPv6LL address, ignoring: %m");
927 }
928 }
929
930 ipv4acd_detach(link, address);
931
932 (void) link_dhcp_relay_address_dropped(link, address);
933
934 address_detach(address);
935
936 if (!removed_by_us) {
937 r = address_removed_maybe_kernel_dad(link, address);
938 if (r < 0) {
939 link_enter_failed(link);
940 return r;
941 }
942 }
943
944 link_update_operstate(link, /* also_update_master= */ true);
945 link_check_ready(link);
946 return 0;
947 }
948
949 static bool address_match_null(const Address *a, const Address *null_address) {
950 assert(a);
951 assert(null_address);
952
953 if (!a->requested_as_null)
954 return false;
955
956 /* Currently, null address is supported only by static addresses. Note that static
957 * address may be set as foreign during reconfiguring the interface. */
958 if (!IN_SET(a->source, NETWORK_CONFIG_SOURCE_FOREIGN, NETWORK_CONFIG_SOURCE_STATIC))
959 return false;
960
961 if (a->family != null_address->family)
962 return false;
963
964 if (a->prefixlen != null_address->prefixlen)
965 return false;
966
967 return true;
968 }
969
970 static int address_get_request(Link *link, const Address *address, Request **ret) {
971 Request *req;
972
973 assert(link);
974 assert(link->manager);
975 assert(address);
976
977 req = ordered_set_get(
978 link->manager->request_queue,
979 &(Request) {
980 .link = link,
981 .type = REQUEST_TYPE_ADDRESS,
982 .userdata = (void*) address,
983 .hash_func = (hash_func_t) address_hash_func,
984 .compare_func = (compare_func_t) address_compare_func,
985 });
986 if (req) {
987 if (ret)
988 *ret = req;
989 return 0;
990 }
991
992 if (address_is_static_null(address))
993 ORDERED_SET_FOREACH(req, link->manager->request_queue) {
994 if (req->link != link)
995 continue;
996 if (req->type != REQUEST_TYPE_ADDRESS)
997 continue;
998
999 if (!address_match_null(req->userdata, address))
1000 continue;
1001
1002 if (ret)
1003 *ret = req;
1004
1005 return 0;
1006 }
1007
1008 return -ENOENT;
1009 }
1010
1011 int address_get(Link *link, const Address *in, Address **ret) {
1012 Address *a;
1013
1014 assert(link);
1015 assert(in);
1016
1017 a = set_get(link->addresses, in);
1018 if (a) {
1019 if (ret)
1020 *ret = a;
1021 return 0;
1022 }
1023
1024 /* Find matching address that originally requested as null address. */
1025 if (address_is_static_null(in))
1026 SET_FOREACH(a, link->addresses) {
1027 if (!address_match_null(a, in))
1028 continue;
1029
1030 if (ret)
1031 *ret = a;
1032 return 0;
1033 }
1034
1035 return -ENOENT;
1036 }
1037
1038 int address_get_harder(Link *link, const Address *in, Address **ret) {
1039 Request *req;
1040 int r;
1041
1042 assert(link);
1043 assert(in);
1044
1045 if (address_get(link, in, ret) >= 0)
1046 return 0;
1047
1048 r = address_get_request(link, in, &req);
1049 if (r < 0)
1050 return r;
1051
1052 if (ret)
1053 *ret = ASSERT_PTR(req->userdata);
1054
1055 return 0;
1056 }
1057
1058 int link_get_address_full(
1059 Link *link,
1060 int family,
1061 const union in_addr_union *address,
1062 const union in_addr_union *peer, /* optional, can be NULL */
1063 unsigned char prefixlen, /* optional, can be 0 */
1064 Address **ret) {
1065
1066 Address *a;
1067 int r;
1068
1069 assert(link);
1070 assert(IN_SET(family, AF_INET, AF_INET6));
1071 assert(address);
1072
1073 /* This finds an Address object on the link which matches the given address, peer, and prefix length.
1074 * If the prefixlen is zero, then an Address object with an arbitrary prefixlen will be returned.
1075 * If the peer is NULL, then an Address object with an arbitrary peer will be returned. */
1076
1077 if (family == AF_INET6 || (prefixlen != 0 && peer)) {
1078 _cleanup_(address_unrefp) Address *tmp = NULL;
1079
1080 /* In this case, we can use address_get(). */
1081
1082 r = address_new(&tmp);
1083 if (r < 0)
1084 return r;
1085
1086 tmp->family = family;
1087 tmp->in_addr = *address;
1088 if (peer)
1089 tmp->in_addr_peer = *peer;
1090 tmp->prefixlen = prefixlen;
1091
1092 r = address_get(link, tmp, &a);
1093 if (r < 0)
1094 return r;
1095
1096 if (family == AF_INET6) {
1097 /* IPv6 addresses are managed without peer address and prefix length. Hence, we need
1098 * to check them explicitly. */
1099 if (peer && !in_addr_equal(family, &a->in_addr_peer, peer))
1100 return -ENOENT;
1101 if (prefixlen != 0 && a->prefixlen != prefixlen)
1102 return -ENOENT;
1103 }
1104
1105 if (ret)
1106 *ret = a;
1107
1108 return 0;
1109 }
1110
1111 SET_FOREACH(a, link->addresses) {
1112 if (a->family != family)
1113 continue;
1114
1115 if (!in_addr_equal(family, &a->in_addr, address))
1116 continue;
1117
1118 if (peer && !in_addr_equal(family, &a->in_addr_peer, peer))
1119 continue;
1120
1121 if (prefixlen != 0 && a->prefixlen != prefixlen)
1122 continue;
1123
1124 if (ret)
1125 *ret = a;
1126
1127 return 0;
1128 }
1129
1130 return -ENOENT;
1131 }
1132
1133 int manager_get_address_full(
1134 Manager *manager,
1135 int family,
1136 const union in_addr_union *address,
1137 const union in_addr_union *peer,
1138 unsigned char prefixlen,
1139 Address **ret) {
1140
1141 Link *link;
1142
1143 assert(manager);
1144 assert(IN_SET(family, AF_INET, AF_INET6));
1145 assert(address);
1146
1147 HASHMAP_FOREACH(link, manager->links_by_index) {
1148 if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED))
1149 continue;
1150
1151 if (link_get_address_full(link, family, address, peer, prefixlen, ret) >= 0)
1152 return 0;
1153 }
1154
1155 return -ENOENT;
1156 }
1157
1158 const char* format_lifetime(char *buf, size_t l, usec_t lifetime_usec) {
1159 assert(buf);
1160 assert(l > 4);
1161
1162 if (lifetime_usec == USEC_INFINITY)
1163 return "forever";
1164
1165 sprintf(buf, "for ");
1166 /* format_timespan() never fails */
1167 assert_se(format_timespan(buf + 4, l - 4, usec_sub_unsigned(lifetime_usec, now(CLOCK_BOOTTIME)), USEC_PER_SEC));
1168 return buf;
1169 }
1170
1171 void log_address_debug(const Address *address, const char *str, const Link *link) {
1172 _cleanup_free_ char *state = NULL, *flags_str = NULL, *scope_str = NULL;
1173
1174 assert(address);
1175 assert(str);
1176 assert(link);
1177
1178 if (!DEBUG_LOGGING)
1179 return;
1180
1181 (void) network_config_state_to_string_alloc(address->state, &state);
1182
1183 const char *peer = in_addr_is_set(address->family, &address->in_addr_peer) ?
1184 IN_ADDR_TO_STRING(address->family, &address->in_addr_peer) : NULL;
1185
1186 const char *broadcast = (address->family == AF_INET && in4_addr_is_set(&address->broadcast)) ?
1187 IN4_ADDR_TO_STRING(&address->broadcast) : NULL;
1188
1189 (void) address_flags_to_string_alloc(address->flags, address->family, &flags_str);
1190 (void) route_scope_to_string_alloc(address->scope, &scope_str);
1191
1192 log_link_debug(link, "%s %s address (%s): %s%s%s/%u%s%s (valid %s, preferred %s), flags: %s, scope: %s%s%s",
1193 str, strna(network_config_source_to_string(address->source)), strna(state),
1194 IN_ADDR_TO_STRING(address->family, &address->in_addr),
1195 peer ? " peer " : "", strempty(peer), address->prefixlen,
1196 broadcast ? " broadcast " : "", strempty(broadcast),
1197 FORMAT_LIFETIME(address->lifetime_valid_usec),
1198 FORMAT_LIFETIME(address->lifetime_preferred_usec),
1199 strna(flags_str), strna(scope_str),
1200 address->family == AF_INET ? ", label: " : "",
1201 address->family == AF_INET ? strna(address->label) : "");
1202 }
1203
1204 static void address_forget(Link *link, Address *address, bool removed_by_us, const char *msg) {
1205 assert(link);
1206 assert(address);
1207 assert(msg);
1208
1209 Request *req;
1210 if (address_get_request(link, address, &req) >= 0)
1211 address_enter_removed(req->userdata);
1212
1213 if (!address->link && address_get(link, address, &address) < 0)
1214 return;
1215
1216 address_enter_removed(address);
1217 log_address_debug(address, msg, link);
1218 (void) address_drop(address, removed_by_us);
1219 }
1220
1221 static int address_set_netlink_message(const Address *address, sd_netlink_message *m, Link *link) {
1222 int r;
1223
1224 assert(address);
1225 assert(m);
1226 assert(link);
1227
1228 r = sd_rtnl_message_addr_set_prefixlen(m, address->prefixlen);
1229 if (r < 0)
1230 return r;
1231
1232 /* On remove, only IFA_F_MANAGETEMPADDR flag for IPv6 addresses are used. But anyway, set all
1233 * flags except tentative flag here unconditionally. Without setting the flag, the template
1234 * addresses generated by kernel will not be removed automatically when the main address is
1235 * removed. */
1236 uint32_t flags = address->flags & ~IFA_F_TENTATIVE;
1237 if (flags != 0) {
1238 r = sd_netlink_message_append_u32(m, IFA_FLAGS, flags);
1239 if (r < 0)
1240 return r;
1241 }
1242
1243 r = netlink_message_append_in_addr_union(m, IFA_LOCAL, address->family, &address->in_addr);
1244 if (r < 0)
1245 return r;
1246
1247 return 0;
1248 }
1249
1250 static int address_remove_handler(sd_netlink *rtnl, sd_netlink_message *m, RemoveRequest *rreq) {
1251 int r;
1252
1253 assert(m);
1254 assert(rreq);
1255
1256 Link *link = ASSERT_PTR(rreq->link);
1257 Address *address = ASSERT_PTR(rreq->userdata);
1258
1259 if (link->state == LINK_STATE_LINGER)
1260 return 0;
1261
1262 r = sd_netlink_message_get_errno(m);
1263 if (r < 0) {
1264 log_link_message_full_errno(link, m,
1265 (r == -EADDRNOTAVAIL || !address->link) ? LOG_DEBUG : LOG_WARNING,
1266 r, "Could not drop address");
1267
1268 /* If the address cannot be removed, then assume the address is already removed. */
1269 address_forget(link, address, /* removed_by_us= */ true, "Forgetting");
1270 }
1271
1272 return 1;
1273 }
1274
1275 int address_remove(Address *address, Link *link) {
1276 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
1277 int r;
1278
1279 assert(address);
1280 assert(IN_SET(address->family, AF_INET, AF_INET6));
1281 assert(link);
1282 assert(link->ifindex > 0);
1283 assert(link->manager);
1284 assert(link->manager->rtnl);
1285
1286 /* If the address is remembered, use the remembered object. */
1287 (void) address_get(link, address, &address);
1288
1289 log_address_debug(address, "Removing", link);
1290
1291 r = sd_rtnl_message_new_addr(link->manager->rtnl, &m, RTM_DELADDR,
1292 link->ifindex, address->family);
1293 if (r < 0)
1294 return log_link_warning_errno(link, r, "Could not allocate RTM_DELADDR message: %m");
1295
1296 r = address_set_netlink_message(address, m, link);
1297 if (r < 0)
1298 return log_link_warning_errno(link, r, "Could not set netlink attributes: %m");
1299
1300 r = link_remove_request_add(link, address, address, link->manager->rtnl, m, address_remove_handler);
1301 if (r < 0)
1302 return log_link_warning_errno(link, r, "Could not queue rtnetlink message: %m");
1303
1304 address_enter_removing(address);
1305
1306 /* The operational state is determined by address state and carrier state. Hence, if we remove
1307 * an address, the operational state may be changed. */
1308 link_update_operstate(link, true);
1309 return 0;
1310 }
1311
1312 int address_remove_and_cancel(Address *address, Link *link) {
1313 _cleanup_(request_unrefp) Request *req = NULL;
1314 bool waiting = false;
1315
1316 assert(address);
1317 assert(link);
1318 assert(link->manager);
1319
1320 /* If the address is remembered by the link, then use the remembered object. */
1321 (void) address_get(link, address, &address);
1322
1323 /* Cancel the request for the address. If the request is already called but we have not received the
1324 * notification about the request, then explicitly remove the address. */
1325 if (address_get_request(link, address, &req) >= 0) {
1326 request_ref(req); /* avoid the request freed by request_detach() */
1327 waiting = req->waiting_reply;
1328 request_detach(req);
1329 address_cancel_requesting(address);
1330 }
1331
1332 /* If we know the address will come or already exists, remove it. */
1333 if (waiting || (address->link && address_exists(address)))
1334 return address_remove(address, link);
1335
1336 return 0;
1337 }
1338
1339 bool link_address_is_dynamic(const Link *link, const Address *address) {
1340 Route *route;
1341
1342 assert(link);
1343 assert(link->manager);
1344 assert(address);
1345
1346 if (address->lifetime_preferred_usec != USEC_INFINITY)
1347 return true;
1348
1349 /* There is no way to drtermine if the IPv6 address is dynamic when the lifetime is infinity. */
1350 if (address->family != AF_INET)
1351 return false;
1352
1353 /* Even if an IPv4 address is leased from a DHCP server with a finite lifetime, networkd assign the
1354 * address without lifetime when KeepConfiguration=dynamic. So, let's check that we have
1355 * corresponding routes with RTPROT_DHCP. */
1356 SET_FOREACH(route, link->manager->routes) {
1357 if (route->source != NETWORK_CONFIG_SOURCE_FOREIGN)
1358 continue;
1359
1360 /* The route is not assigned yet, or already being removed. Ignoring. */
1361 if (!route_exists(route))
1362 continue;
1363
1364 if (route->protocol != RTPROT_DHCP)
1365 continue;
1366
1367 if (route->nexthop.ifindex != link->ifindex)
1368 continue;
1369
1370 if (address->family != route->family)
1371 continue;
1372
1373 if (in_addr_equal(address->family, &address->in_addr, &route->prefsrc))
1374 return true;
1375 }
1376
1377 return false;
1378 }
1379
1380 int link_drop_ipv6ll_addresses(Link *link) {
1381 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
1382 int r;
1383
1384 assert(link);
1385 assert(link->manager);
1386 assert(link->manager->rtnl);
1387
1388 /* IPv6LL address may be in the tentative state, and in that case networkd has not received it.
1389 * So, we need to dump all IPv6 addresses. */
1390
1391 if (link_ipv6ll_enabled_harder(link))
1392 return 0;
1393
1394 r = sd_rtnl_message_new_addr(link->manager->rtnl, &req, RTM_GETADDR, link->ifindex, AF_INET6);
1395 if (r < 0)
1396 return r;
1397
1398 r = sd_netlink_message_set_request_dump(req, true);
1399 if (r < 0)
1400 return r;
1401
1402 r = sd_netlink_call(link->manager->rtnl, req, 0, &reply);
1403 if (r < 0)
1404 return r;
1405
1406 for (sd_netlink_message *addr = reply; addr; addr = sd_netlink_message_next(addr)) {
1407 _cleanup_(address_unrefp) Address *a = NULL;
1408 unsigned char prefixlen;
1409 struct in6_addr address;
1410 int ifindex;
1411
1412 /* We set ifindex in the request, and NETLINK_GET_STRICT_CHK socket option is set. Hence the
1413 * check below is redundant, but let's do that for safety. */
1414 r = sd_rtnl_message_addr_get_ifindex(addr, &ifindex);
1415 if (r < 0) {
1416 log_link_debug_errno(link, r, "rtnl: received address message without valid ifindex, ignoring: %m");
1417 continue;
1418 } else if (link->ifindex != ifindex)
1419 continue;
1420
1421 uint32_t flags;
1422 r = sd_netlink_message_read_u32(addr, IFA_FLAGS, &flags);
1423 if (r < 0) {
1424 log_link_debug_errno(link, r, "rtnl: Failed to read IFA_FLAGS attribute, ignoring: %m");
1425 continue;
1426 }
1427
1428 r = sd_rtnl_message_addr_get_prefixlen(addr, &prefixlen);
1429 if (r < 0) {
1430 log_link_debug_errno(link, r, "rtnl: received address message without prefixlen, ignoring: %m");
1431 continue;
1432 }
1433
1434 if (sd_netlink_message_read_in6_addr(addr, IFA_LOCAL, NULL) >= 0)
1435 /* address with peer, ignoring. */
1436 continue;
1437
1438 r = sd_netlink_message_read_in6_addr(addr, IFA_ADDRESS, &address);
1439 if (r < 0) {
1440 log_link_debug_errno(link, r, "rtnl: received address message without valid address, ignoring: %m");
1441 continue;
1442 }
1443
1444 if (!in6_addr_is_link_local(&address))
1445 continue;
1446
1447 r = address_new(&a);
1448 if (r < 0)
1449 return -ENOMEM;
1450
1451 a->family = AF_INET6;
1452 a->in_addr.in6 = address;
1453 a->prefixlen = prefixlen;
1454 a->flags = flags;
1455
1456 r = address_remove(a, link);
1457 if (r < 0)
1458 return r;
1459 }
1460
1461 return 0;
1462 }
1463
1464 int link_drop_unmanaged_addresses(Link *link) {
1465 Address *address;
1466 int r = 0;
1467
1468 assert(link);
1469 assert(link->network);
1470
1471 /* First, mark all addresses. */
1472 SET_FOREACH(address, link->addresses) {
1473 /* We consider IPv6LL addresses to be managed by the kernel, or dropped in link_drop_ipv6ll_addresses() */
1474 if (address->family == AF_INET6 && in6_addr_is_link_local(&address->in_addr.in6))
1475 continue;
1476
1477 /* Do not remove localhost address (127.0.0.1 and ::1) */
1478 if (link->flags & IFF_LOOPBACK && in_addr_is_localhost_one(address->family, &address->in_addr) > 0)
1479 continue;
1480
1481 /* Ignore addresses not assigned yet or already removing. */
1482 if (!address_exists(address))
1483 continue;
1484
1485 if (address->source == NETWORK_CONFIG_SOURCE_FOREIGN) {
1486 if (link->network->keep_configuration == KEEP_CONFIGURATION_YES)
1487 continue;
1488
1489 /* link_address_is_dynamic() is slightly heavy. Let's call the function only when
1490 * KeepConfiguration=dynamic or static. */
1491 if (IN_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DYNAMIC, KEEP_CONFIGURATION_STATIC) &&
1492 link_address_is_dynamic(link, address) == (link->network->keep_configuration == KEEP_CONFIGURATION_DYNAMIC))
1493 continue;
1494
1495 } else if (address->source != NETWORK_CONFIG_SOURCE_STATIC)
1496 continue; /* Ignore dynamically configured addresses. */
1497
1498 address_mark(address);
1499 }
1500
1501 /* Then, unmark requested addresses. */
1502 ORDERED_HASHMAP_FOREACH(address, link->network->addresses_by_section) {
1503 Address *existing;
1504
1505 if (address_get(link, address, &existing) < 0)
1506 continue;
1507
1508 if (!address_can_update(existing, address))
1509 continue;
1510
1511 /* Found matching static configuration. Keep the existing address. */
1512 address_unmark(existing);
1513 }
1514
1515 /* Finally, remove all marked addresses. */
1516 SET_FOREACH(address, link->addresses) {
1517 if (!address_is_marked(address))
1518 continue;
1519
1520 RET_GATHER(r, address_remove(address, link));
1521 }
1522
1523 return r;
1524 }
1525
1526 int link_drop_static_addresses(Link *link) {
1527 Address *address;
1528 int r = 0;
1529
1530 assert(link);
1531
1532 SET_FOREACH(address, link->addresses) {
1533 /* Remove only static addresses here. Dynamic addresses will be removed e.g. on lease
1534 * expiration or stopping the DHCP client. */
1535 if (address->source != NETWORK_CONFIG_SOURCE_STATIC)
1536 continue;
1537
1538 /* Ignore addresses not assigned yet or already removing. */
1539 if (!address_exists(address))
1540 continue;
1541
1542 RET_GATHER(r, address_remove(address, link));
1543 }
1544
1545 return r;
1546 }
1547
1548 int address_configure_handler_internal(sd_netlink_message *m, Link *link, Address *address) {
1549 int r;
1550
1551 assert(m);
1552 assert(link);
1553 assert(address);
1554
1555 r = sd_netlink_message_get_errno(m);
1556 if (r < 0 && r != -EEXIST) {
1557 log_link_message_warning_errno(link, m, r, "Failed to set %s address %s",
1558 network_config_source_to_string(address->source),
1559 IN_ADDR_TO_STRING(address->family, &address->in_addr));
1560 link_enter_failed(link);
1561 return 0;
1562 }
1563
1564 return 1;
1565 }
1566
1567 static int address_configure(const Address *address, const struct ifa_cacheinfo *c, Link *link, Request *req) {
1568 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
1569 int r;
1570
1571 assert(address);
1572 assert(IN_SET(address->family, AF_INET, AF_INET6));
1573 assert(c);
1574 assert(link);
1575 assert(link->ifindex > 0);
1576 assert(link->manager);
1577 assert(link->manager->rtnl);
1578 assert(req);
1579
1580 log_address_debug(address, "Configuring", link);
1581
1582 r = sd_rtnl_message_new_addr_update(link->manager->rtnl, &m, link->ifindex, address->family);
1583 if (r < 0)
1584 return r;
1585
1586 r = address_set_netlink_message(address, m, link);
1587 if (r < 0)
1588 return r;
1589
1590 r = sd_rtnl_message_addr_set_scope(m, address->scope);
1591 if (r < 0)
1592 return r;
1593
1594 if (address->family == AF_INET6 || in_addr_is_set(address->family, &address->in_addr_peer)) {
1595 r = netlink_message_append_in_addr_union(m, IFA_ADDRESS, address->family, &address->in_addr_peer);
1596 if (r < 0)
1597 return r;
1598 } else if (in4_addr_is_set(&address->broadcast)) {
1599 r = sd_netlink_message_append_in_addr(m, IFA_BROADCAST, &address->broadcast);
1600 if (r < 0)
1601 return r;
1602 }
1603
1604 if (address->family == AF_INET && address->label) {
1605 r = sd_netlink_message_append_string(m, IFA_LABEL, address->label);
1606 if (r < 0)
1607 return r;
1608 }
1609
1610 r = sd_netlink_message_append_cache_info(m, IFA_CACHEINFO, c);
1611 if (r < 0)
1612 return r;
1613
1614 r = sd_netlink_message_append_u32(m, IFA_RT_PRIORITY, address->route_metric);
1615 if (r < 0)
1616 return r;
1617
1618 return request_call_netlink_async(link->manager->rtnl, m, req);
1619 }
1620
1621 static int address_acquire(Link *link, const Address *address, union in_addr_union *ret) {
1622 union in_addr_union a;
1623 int r;
1624
1625 assert(link);
1626 assert(address);
1627 assert(ret);
1628
1629 r = address_acquire_from_dhcp_server_leases_file(link, address, ret);
1630 if (!IN_SET(r, -ENOENT, -ENXIO, -EINVAL))
1631 return r;
1632
1633 r = address_pool_acquire(link->manager, address->family, address->prefixlen, &a);
1634 if (r < 0)
1635 return r;
1636 if (r == 0)
1637 return -EBUSY;
1638
1639 /* Pick first address in range for ourselves. */
1640 if (address->family == AF_INET)
1641 a.in.s_addr |= htobe32(1);
1642 else if (address->family == AF_INET6)
1643 a.in6.s6_addr[15] |= 1;
1644 else
1645 assert_not_reached();
1646
1647 *ret = a;
1648 return 0;
1649 }
1650
1651 static int address_requeue_request(Request *req, Link *link, const Address *address) {
1652 int r;
1653
1654 assert(req);
1655 assert(link);
1656 assert(link->manager);
1657 assert(link->network);
1658 assert(address);
1659
1660 /* Something useful was configured? just use it */
1661 if (in_addr_is_set(address->family, &address->in_addr))
1662 return 0;
1663
1664 /* The address is configured to be 0.0.0.0 or [::] by the user?
1665 * Then let's acquire something more useful. */
1666 union in_addr_union a;
1667 r = address_acquire(link, address, &a);
1668 if (r < 0)
1669 return r;
1670
1671 _cleanup_(address_unrefp) Address *tmp = NULL;
1672 r = address_dup(address, &tmp);
1673 if (r < 0)
1674 return r;
1675
1676 tmp->in_addr = a;
1677
1678 r = link_requeue_request(link, req, tmp, NULL);
1679 if (r < 0)
1680 return r;
1681 if (r == 0)
1682 return -EEXIST; /* Already queued?? Strange... */
1683
1684 TAKE_PTR(tmp);
1685 return 1; /* A new request is queued. it is not necessary to process this request anymore. */
1686 }
1687
1688 static int address_process_request(Request *req, Link *link, Address *address) {
1689 Address *existing;
1690 struct ifa_cacheinfo c;
1691 int r;
1692
1693 assert(req);
1694 assert(link);
1695 assert(address);
1696
1697 if (!link_is_ready_to_configure(link, false))
1698 return 0;
1699
1700 /* Refuse adding more than the limit */
1701 if (set_size(link->addresses) >= ADDRESSES_PER_LINK_MAX)
1702 return 0;
1703
1704 r = address_requeue_request(req, link, address);
1705 if (r == -EBUSY)
1706 return 0;
1707 if (r != 0)
1708 return r;
1709
1710 address_set_broadcast(address, link);
1711
1712 r = ipv4acd_configure(link, address);
1713 if (r < 0)
1714 return r;
1715
1716 if (!ipv4acd_bound(link, address))
1717 return 0;
1718
1719 address_set_cinfo(link->manager, address, &c);
1720 if (c.ifa_valid == 0) {
1721 log_link_debug(link, "Refuse to configure %s address %s, as its valid lifetime is zero.",
1722 network_config_source_to_string(address->source),
1723 IN_ADDR_PREFIX_TO_STRING(address->family, &address->in_addr, address->prefixlen));
1724
1725 address_cancel_requesting(address);
1726 if (address_get(link, address, &existing) >= 0)
1727 address_cancel_requesting(existing);
1728 return 1;
1729 }
1730
1731 r = address_configure(address, &c, link, req);
1732 if (r < 0)
1733 return log_link_warning_errno(link, r, "Failed to configure address: %m");
1734
1735 address_enter_configuring(address);
1736 if (address_get(link, address, &existing) >= 0)
1737 address_enter_configuring(existing);
1738
1739 return 1;
1740 }
1741
1742 int link_request_address(
1743 Link *link,
1744 const Address *address,
1745 unsigned *message_counter,
1746 address_netlink_handler_t netlink_handler,
1747 Request **ret) {
1748
1749 _cleanup_(address_unrefp) Address *tmp = NULL;
1750 Address *existing = NULL;
1751 int r;
1752
1753 assert(link);
1754 assert(address);
1755 assert(address->source != NETWORK_CONFIG_SOURCE_FOREIGN);
1756
1757 if (address->lifetime_valid_usec == 0)
1758 /* The requested address is outdated. Let's ignore the request. */
1759 return 0;
1760
1761 if (address_get_request(link, address, NULL) >= 0)
1762 return 0; /* already requested, skipping. */
1763
1764 r = address_dup(address, &tmp);
1765 if (r < 0)
1766 return log_oom();
1767
1768 if (address_get(link, address, &existing) >= 0) {
1769 /* Copy already assigned address when it is requested as a null address. */
1770 if (address_is_static_null(address))
1771 tmp->in_addr = existing->in_addr;
1772
1773 /* Copy state for logging below. */
1774 tmp->state = existing->state;
1775 }
1776
1777 log_address_debug(tmp, "Requesting", link);
1778 r = link_queue_request_safe(link, REQUEST_TYPE_ADDRESS,
1779 tmp,
1780 address_unref,
1781 address_hash_func,
1782 address_compare_func,
1783 address_process_request,
1784 message_counter, netlink_handler, ret);
1785 if (r < 0)
1786 return log_link_warning_errno(link, r, "Failed to request address: %m");
1787 if (r == 0)
1788 return 0;
1789
1790 address_enter_requesting(tmp);
1791 if (existing)
1792 address_enter_requesting(existing);
1793
1794 TAKE_PTR(tmp);
1795 return 1;
1796 }
1797
1798 static int static_address_handler(sd_netlink *rtnl, sd_netlink_message *m, Request *req, Link *link, Address *address) {
1799 int r;
1800
1801 assert(link);
1802 assert(address);
1803
1804 r = address_configure_handler_internal(m, link, address);
1805 if (r <= 0)
1806 return r;
1807
1808 if (link->static_address_messages == 0) {
1809 log_link_debug(link, "Addresses set");
1810 link->static_addresses_configured = true;
1811 link_check_ready(link);
1812 }
1813
1814 return 1;
1815 }
1816
1817 int link_request_static_address(Link *link, const Address *address) {
1818 assert(link);
1819 assert(address);
1820 assert(address->source == NETWORK_CONFIG_SOURCE_STATIC);
1821
1822 return link_request_address(link, address, &link->static_address_messages,
1823 static_address_handler, NULL);
1824 }
1825
1826 int link_request_static_addresses(Link *link) {
1827 Address *a;
1828 int r;
1829
1830 assert(link);
1831 assert(link->network);
1832
1833 link->static_addresses_configured = false;
1834
1835 ORDERED_HASHMAP_FOREACH(a, link->network->addresses_by_section) {
1836 r = link_request_static_address(link, a);
1837 if (r < 0)
1838 return r;
1839 }
1840
1841 r = link_request_radv_addresses(link);
1842 if (r < 0)
1843 return r;
1844
1845 if (link->static_address_messages == 0) {
1846 link->static_addresses_configured = true;
1847 link_check_ready(link);
1848 } else {
1849 log_link_debug(link, "Setting addresses");
1850 link_set_state(link, LINK_STATE_CONFIGURING);
1851 }
1852
1853 return 0;
1854 }
1855
1856 int manager_rtnl_process_address(sd_netlink *rtnl, sd_netlink_message *message, Manager *m) {
1857 int r;
1858
1859 assert(rtnl);
1860 assert(message);
1861 assert(m);
1862
1863 if (sd_netlink_message_is_error(message)) {
1864 r = sd_netlink_message_get_errno(message);
1865 if (r < 0)
1866 log_message_warning_errno(message, r, "rtnl: failed to receive address message, ignoring");
1867
1868 return 0;
1869 }
1870
1871 uint16_t type;
1872 r = sd_netlink_message_get_type(message, &type);
1873 if (r < 0) {
1874 log_warning_errno(r, "rtnl: could not get message type, ignoring: %m");
1875 return 0;
1876 } else if (!IN_SET(type, RTM_NEWADDR, RTM_DELADDR)) {
1877 log_warning("rtnl: received unexpected message type %u when processing address, ignoring.", type);
1878 return 0;
1879 }
1880
1881 int ifindex;
1882 r = sd_rtnl_message_addr_get_ifindex(message, &ifindex);
1883 if (r < 0) {
1884 log_warning_errno(r, "rtnl: could not get ifindex from message, ignoring: %m");
1885 return 0;
1886 } else if (ifindex <= 0) {
1887 log_warning("rtnl: received address message with invalid ifindex %d, ignoring.", ifindex);
1888 return 0;
1889 }
1890
1891 Link *link;
1892 r = link_get_by_index(m, ifindex, &link);
1893 if (r < 0) {
1894 /* when enumerating we might be out of sync, but we will get the address again, so just
1895 * ignore it */
1896 if (!m->enumerating)
1897 log_warning("rtnl: received address for link '%d' we don't know about, ignoring.", ifindex);
1898 return 0;
1899 }
1900
1901 _cleanup_(address_unrefp) Address *tmp = NULL;
1902 r = address_new(&tmp);
1903 if (r < 0)
1904 return log_oom();
1905
1906 /* First, read minimal information to make address_get() work below. */
1907
1908 r = sd_rtnl_message_addr_get_family(message, &tmp->family);
1909 if (r < 0) {
1910 log_link_warning(link, "rtnl: received address message without family, ignoring.");
1911 return 0;
1912 } else if (!IN_SET(tmp->family, AF_INET, AF_INET6)) {
1913 log_link_debug(link, "rtnl: received address message with invalid family '%i', ignoring.", tmp->family);
1914 return 0;
1915 }
1916
1917 r = sd_rtnl_message_addr_get_prefixlen(message, &tmp->prefixlen);
1918 if (r < 0) {
1919 log_link_warning_errno(link, r, "rtnl: received address message without prefixlen, ignoring: %m");
1920 return 0;
1921 }
1922
1923 switch (tmp->family) {
1924 case AF_INET:
1925 r = sd_netlink_message_read_in_addr(message, IFA_LOCAL, &tmp->in_addr.in);
1926 if (r < 0) {
1927 log_link_warning_errno(link, r, "rtnl: received address message without valid address, ignoring: %m");
1928 return 0;
1929 }
1930
1931 r = sd_netlink_message_read_in_addr(message, IFA_ADDRESS, &tmp->in_addr_peer.in);
1932 if (r < 0 && r != -ENODATA) {
1933 log_link_warning_errno(link, r, "rtnl: could not get peer address from address message, ignoring: %m");
1934 return 0;
1935 } else if (r >= 0) {
1936 if (in4_addr_equal(&tmp->in_addr.in, &tmp->in_addr_peer.in))
1937 tmp->in_addr_peer = IN_ADDR_NULL;
1938 }
1939
1940 break;
1941
1942 case AF_INET6:
1943 r = sd_netlink_message_read_in6_addr(message, IFA_LOCAL, &tmp->in_addr.in6);
1944 if (r >= 0) {
1945 /* Have peer address. */
1946 r = sd_netlink_message_read_in6_addr(message, IFA_ADDRESS, &tmp->in_addr_peer.in6);
1947 if (r < 0) {
1948 log_link_warning_errno(link, r, "rtnl: could not get peer address from address message, ignoring: %m");
1949 return 0;
1950 }
1951 } else if (r == -ENODATA) {
1952 /* Does not have peer address. */
1953 r = sd_netlink_message_read_in6_addr(message, IFA_ADDRESS, &tmp->in_addr.in6);
1954 if (r < 0) {
1955 log_link_warning_errno(link, r, "rtnl: received address message without valid address, ignoring: %m");
1956 return 0;
1957 }
1958 } else {
1959 log_link_warning_errno(link, r, "rtnl: could not get local address from address message, ignoring: %m");
1960 return 0;
1961 }
1962
1963 break;
1964
1965 default:
1966 assert_not_reached();
1967 }
1968
1969 /* Then, find the managed Address object corresponding to the received address. */
1970 Address *address = NULL;
1971 (void) address_get(link, tmp, &address);
1972
1973 if (type == RTM_DELADDR) {
1974 if (address)
1975 address_forget(link, address,
1976 /* removed_by_us= */ FLAGS_SET(address->state, NETWORK_CONFIG_STATE_REMOVING),
1977 "Forgetting removed");
1978 else
1979 log_address_debug(tmp, "Kernel removed unknown", link);
1980
1981 goto finalize;
1982 }
1983
1984 bool is_new = false;
1985 if (!address) {
1986 /* If we did not know the address, then save it. */
1987 r = address_attach(link, tmp);
1988 if (r < 0) {
1989 log_link_warning_errno(link, r, "Failed to save received address %s, ignoring: %m",
1990 IN_ADDR_PREFIX_TO_STRING(tmp->family, &tmp->in_addr, tmp->prefixlen));
1991 return 0;
1992 }
1993 address = tmp;
1994
1995 is_new = true;
1996
1997 } else {
1998 /* Otherwise, update the managed Address object with the netlink notification. */
1999 address->prefixlen = tmp->prefixlen;
2000 address->in_addr_peer = tmp->in_addr_peer;
2001 }
2002
2003 /* Also update information that cannot be obtained through netlink notification. */
2004 Request *req = NULL;
2005 (void) address_get_request(link, tmp, &req);
2006 if (req && req->waiting_reply) {
2007 Address *a = ASSERT_PTR(req->userdata);
2008
2009 address->source = a->source;
2010 address->provider = a->provider;
2011 (void) free_and_strdup_warn(&address->netlabel, a->netlabel);
2012 nft_set_context_clear(&address->nft_set_context);
2013 (void) nft_set_context_dup(&a->nft_set_context, &address->nft_set_context);
2014 address->requested_as_null = a->requested_as_null;
2015 address->also_requested_by_dhcp6 = a->also_requested_by_dhcp6;
2016 address->callback = a->callback;
2017
2018 ipv6_token_ref(a->token);
2019 ipv6_token_unref(address->token);
2020 address->token = a->token;
2021 }
2022
2023 /* Then, update miscellaneous info. */
2024 r = sd_rtnl_message_addr_get_scope(message, &address->scope);
2025 if (r < 0)
2026 log_link_debug_errno(link, r, "rtnl: received address message without scope, ignoring: %m");
2027
2028 if (address->family == AF_INET) {
2029 _cleanup_free_ char *label = NULL;
2030
2031 r = sd_netlink_message_read_string_strdup(message, IFA_LABEL, &label);
2032 if (r >= 0) {
2033 if (!streq_ptr(label, link->ifname))
2034 free_and_replace(address->label, label);
2035 } else if (r != -ENODATA)
2036 log_link_debug_errno(link, r, "rtnl: could not get label from address message, ignoring: %m");
2037
2038 r = sd_netlink_message_read_in_addr(message, IFA_BROADCAST, &address->broadcast);
2039 if (r < 0 && r != -ENODATA)
2040 log_link_debug_errno(link, r, "rtnl: could not get broadcast from address message, ignoring: %m");
2041 }
2042
2043 r = sd_netlink_message_read_u32(message, IFA_FLAGS, &address->flags);
2044 if (r < 0)
2045 log_link_debug_errno(link, r, "rtnl: failed to read IFA_FLAGS attribute, ignoring: %m");
2046
2047 struct ifa_cacheinfo cinfo;
2048 r = sd_netlink_message_read_cache_info(message, IFA_CACHEINFO, &cinfo);
2049 if (r >= 0)
2050 address_set_lifetime(m, address, &cinfo);
2051 else if (r != -ENODATA)
2052 log_link_debug_errno(link, r, "rtnl: failed to read IFA_CACHEINFO attribute, ignoring: %m");
2053
2054 r = sd_netlink_message_read_u32(message, IFA_RT_PRIORITY, &address->route_metric);
2055 if (r < 0 && r != -ENODATA)
2056 log_link_debug_errno(link, r, "rtnl: failed to read IFA_RT_PRIORITY attribute, ignoring: %m");
2057
2058 address_enter_configured(address);
2059 if (req)
2060 address_enter_configured(req->userdata);
2061
2062 log_address_debug(address, is_new ? "Received new": "Received updated", link);
2063
2064 /* address_update() logs internally, so we don't need to here. */
2065 r = address_update(address);
2066 if (r < 0)
2067 link_enter_failed(link);
2068
2069 finalize:
2070 if (tmp->family == AF_INET6) {
2071 r = dhcp4_update_ipv6_connectivity(link);
2072 if (r < 0) {
2073 log_link_warning_errno(link, r, "Failed to notify IPv6 connectivity to DHCPv4 client: %m");
2074 link_enter_failed(link);
2075 }
2076 }
2077
2078 return 1;
2079 }
2080
2081 static int config_parse_broadcast(
2082 const char *unit,
2083 const char *filename,
2084 unsigned line,
2085 const char *section,
2086 unsigned section_line,
2087 const char *lvalue,
2088 int ltype,
2089 const char *rvalue,
2090 void *data,
2091 void *userdata) {
2092
2093 Address *address = ASSERT_PTR(userdata);
2094 int r;
2095
2096 /* Do not check or set address->family here. It will be checked later in
2097 * address_section_verify() -> address_section_adjust_broadcast() . */
2098
2099 if (isempty(rvalue)) {
2100 /* The broadcast address will be calculated based on Address=, and set if the link is
2101 * not a wireguard interface. */
2102 address->broadcast = (struct in_addr) {};
2103 address->set_broadcast = -1;
2104 return 1;
2105 }
2106
2107 r = parse_boolean(rvalue);
2108 if (r >= 0) {
2109 /* The broadcast address will be calculated based on Address=. */
2110 address->broadcast = (struct in_addr) {};
2111 address->set_broadcast = r;
2112 return 1;
2113 }
2114
2115 r = config_parse_in_addr_non_null(
2116 unit, filename, line, section, section_line,
2117 lvalue, /* ltype= */ AF_INET, rvalue,
2118 &address->broadcast, /* userdata= */ NULL);
2119 if (r <= 0)
2120 return r;
2121
2122 address->set_broadcast = true;
2123 return 1;
2124 }
2125
2126 static int config_parse_address(
2127 const char *unit,
2128 const char *filename,
2129 unsigned line,
2130 const char *section,
2131 unsigned section_line,
2132 const char *lvalue,
2133 int ltype,
2134 const char *rvalue,
2135 void *data,
2136 void *userdata) {
2137
2138 Address *address = ASSERT_PTR(userdata);
2139 union in_addr_union *a = ASSERT_PTR(data);
2140 struct in_addr_prefix prefix;
2141 int r;
2142
2143 assert(rvalue);
2144
2145 if (isempty(rvalue)) {
2146 /* When an empty string is assigned, clear both Address= and Peer=. */
2147 address->family = AF_UNSPEC;
2148 address->prefixlen = 0;
2149 address->in_addr = IN_ADDR_NULL;
2150 address->in_addr_peer = IN_ADDR_NULL;
2151 return 1;
2152 }
2153
2154 r = config_parse_in_addr_prefix(unit, filename, line, section, section_line, lvalue, /* ltype= */ true, rvalue, &prefix, /* userdata= */ NULL);
2155 if (r <= 0)
2156 return r;
2157
2158 if (address->family != AF_UNSPEC && prefix.family != address->family) {
2159 log_syntax(unit, LOG_WARNING, filename, line, 0, "Address is incompatible, ignoring assignment: %s", rvalue);
2160 return 0;
2161 }
2162
2163 address->family = prefix.family;
2164 address->prefixlen = prefix.prefixlen;
2165 *a = prefix.address;
2166 return 1;
2167 }
2168
2169 static int config_parse_address_label(
2170 const char *unit,
2171 const char *filename,
2172 unsigned line,
2173 const char *section,
2174 unsigned section_line,
2175 const char *lvalue,
2176 int ltype,
2177 const char *rvalue,
2178 void *data,
2179 void *userdata) {
2180
2181 char **label = ASSERT_PTR(data);
2182 int r;
2183
2184 if (!isempty(rvalue) && !address_label_valid(rvalue)) {
2185 log_syntax(unit, LOG_WARNING, filename, line, 0,
2186 "Interface label is too long or invalid, ignoring assignment: %s", rvalue);
2187 return 0;
2188 }
2189
2190 r = free_and_strdup_warn(label, empty_to_null(rvalue));
2191 if (r < 0)
2192 return r;
2193
2194 return 1;
2195 }
2196
2197 static int config_parse_address_lifetime(
2198 const char *unit,
2199 const char *filename,
2200 unsigned line,
2201 const char *section,
2202 unsigned section_line,
2203 const char *lvalue,
2204 int ltype,
2205 const char *rvalue,
2206 void *data,
2207 void *userdata) {
2208
2209 usec_t *usec = ASSERT_PTR(data);
2210
2211 /* We accept only "forever", "infinity", empty, or "0". */
2212 if (isempty(rvalue) || STR_IN_SET(rvalue, "forever", "infinity"))
2213 *usec = USEC_INFINITY;
2214 else if (streq(rvalue, "0"))
2215 *usec = 0;
2216 else {
2217 log_syntax(unit, LOG_WARNING, filename, line, 0,
2218 "Invalid PreferredLifetime= value, ignoring: %s", rvalue);
2219 return 0;
2220 }
2221
2222 return 1;
2223 }
2224
2225 static int config_parse_address_scope(
2226 const char *unit,
2227 const char *filename,
2228 unsigned line,
2229 const char *section,
2230 unsigned section_line,
2231 const char *lvalue,
2232 int ltype,
2233 const char *rvalue,
2234 void *data,
2235 void *userdata) {
2236
2237 Address *address = ASSERT_PTR(userdata);
2238 int r;
2239
2240 if (isempty(rvalue)) {
2241 address->scope = RT_SCOPE_UNIVERSE;
2242 address->scope_set = false;
2243 return 1;
2244 }
2245
2246 r = route_scope_from_string(rvalue);
2247 if (r < 0)
2248 return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
2249
2250 address->scope = r;
2251 address->scope_set = true;
2252 return 1;
2253 }
2254
2255 static int config_parse_address_dad(
2256 const char *unit,
2257 const char *filename,
2258 unsigned line,
2259 const char *section,
2260 unsigned section_line,
2261 const char *lvalue,
2262 int ltype,
2263 const char *rvalue,
2264 void *data,
2265 void *userdata) {
2266
2267 AddressFamily *p = ASSERT_PTR(data);
2268 int r;
2269
2270 if (isempty(rvalue)) {
2271 *p = _ADDRESS_FAMILY_INVALID;
2272 return 1;
2273 }
2274
2275 r = parse_boolean(rvalue);
2276 if (r >= 0) {
2277 log_syntax(unit, LOG_WARNING, filename, line, 0,
2278 "For historical reasons, %s=%s means %s=%s. "
2279 "Please use 'both', 'ipv4', 'ipv6' or 'none' instead.",
2280 lvalue, rvalue, lvalue, r ? "none" : "both");
2281 *p = r ? ADDRESS_FAMILY_NO : ADDRESS_FAMILY_YES;
2282 return 1;
2283 }
2284
2285 AddressFamily a = duplicate_address_detection_address_family_from_string(rvalue);
2286 if (a < 0)
2287 return log_syntax_parse_error(unit, filename, line, a, lvalue, rvalue);
2288
2289 *p = a;
2290 return 1;
2291 }
2292
2293 int config_parse_address_section(
2294 const char *unit,
2295 const char *filename,
2296 unsigned line,
2297 const char *section,
2298 unsigned section_line,
2299 const char *lvalue,
2300 int ltype,
2301 const char *rvalue,
2302 void *data,
2303 void *userdata) {
2304
2305 static const ConfigSectionParser table[_ADDRESS_CONF_PARSER_MAX] = {
2306 [ADDRESS_ADDRESS] = { .parser = config_parse_address, .ltype = 0, .offset = offsetof(Address, in_addr), },
2307 [ADDRESS_PEER] = { .parser = config_parse_address, .ltype = 0, .offset = offsetof(Address, in_addr_peer), },
2308 [ADDRESS_BROADCAST] = { .parser = config_parse_broadcast, .ltype = 0, .offset = 0, },
2309 [ADDRESS_LABEL] = { .parser = config_parse_address_label, .ltype = 0, .offset = offsetof(Address, label), },
2310 [ADDRESS_PREFERRED_LIFETIME] = { .parser = config_parse_address_lifetime, .ltype = 0, .offset = offsetof(Address, lifetime_preferred_usec), },
2311 [ADDRESS_HOME_ADDRESS] = { .parser = config_parse_uint32_flag, .ltype = IFA_F_HOMEADDRESS, .offset = offsetof(Address, flags), },
2312 [ADDRESS_MANAGE_TEMPORARY_ADDRESS] = { .parser = config_parse_uint32_flag, .ltype = IFA_F_MANAGETEMPADDR, .offset = offsetof(Address, flags), },
2313 [ADDRESS_PREFIX_ROUTE] = { .parser = config_parse_uint32_flag, .ltype = IFA_F_NOPREFIXROUTE, .offset = offsetof(Address, flags), },
2314 [ADDRESS_ADD_PREFIX_ROUTE] = { .parser = config_parse_uint32_invert_flag, .ltype = IFA_F_NOPREFIXROUTE, .offset = offsetof(Address, flags), },
2315 [ADDRESS_AUTO_JOIN] = { .parser = config_parse_uint32_flag, .ltype = IFA_F_MCAUTOJOIN, .offset = offsetof(Address, flags), },
2316 [ADDRESS_DAD] = { .parser = config_parse_address_dad, .ltype = 0, .offset = offsetof(Address, duplicate_address_detection), },
2317 [ADDRESS_SCOPE] = { .parser = config_parse_address_scope, .ltype = 0, .offset = 0, },
2318 [ADDRESS_ROUTE_METRIC] = { .parser = config_parse_uint32, .ltype = 0, .offset = offsetof(Address, route_metric), },
2319 [ADDRESS_NET_LABEL] = { .parser = config_parse_string, .ltype = CONFIG_PARSE_STRING_SAFE, .offset = offsetof(Address, netlabel), },
2320 [ADDRESS_NFT_SET] = { .parser = config_parse_nft_set, .ltype = NFT_SET_PARSE_NETWORK, .offset = offsetof(Address, nft_set_context), },
2321 };
2322
2323 _cleanup_(address_unref_or_set_invalidp) Address *address = NULL;
2324 Network *network = ASSERT_PTR(userdata);
2325 int r;
2326
2327 assert(filename);
2328 assert(section);
2329
2330 if (streq(section, "Network")) {
2331 assert(streq(lvalue, "Address"));
2332
2333 if (isempty(rvalue)) {
2334 /* If an empty string specified in [Network] section, clear previously assigned addresses. */
2335 network->addresses_by_section = ordered_hashmap_free(network->addresses_by_section);
2336 return 0;
2337 }
2338
2339 /* we are not in an Address section, so use line number instead. */
2340 r = address_new_static(network, filename, line, &address);
2341 } else
2342 r = address_new_static(network, filename, section_line, &address);
2343 if (r == -ENOMEM)
2344 return log_oom();
2345 if (r < 0) {
2346 log_syntax(unit, LOG_WARNING, filename, line, r,
2347 "Failed to allocate new address, ignoring assignment: %m");
2348 return 0;
2349 }
2350
2351 r = config_section_parse(table, ELEMENTSOF(table),
2352 unit, filename, line, section, section_line, lvalue, ltype, rvalue, address);
2353 if (r <= 0)
2354 return r;
2355
2356 TAKE_PTR(address);
2357 return 0;
2358 }
2359
2360 #define log_broadcast(address, fmt, ...) \
2361 ({ \
2362 const Address *_address = (address); \
2363 log_section_warning( \
2364 _address ? _address->section : NULL, \
2365 fmt " Ignoring Broadcast= setting in the [Address] section.", \
2366 ##__VA_ARGS__); \
2367 })
2368
2369 static void address_section_adjust_broadcast(Address *address) {
2370 assert(address);
2371 assert(address->section);
2372
2373 if (!in4_addr_is_set(&address->broadcast))
2374 return;
2375
2376 if (address->family == AF_INET6)
2377 log_broadcast(address, "Broadcast address is set for an IPv6 address.");
2378 else if (address->prefixlen > 30)
2379 log_broadcast(address, "Broadcast address is set for an IPv4 address with prefix length larger than 30.");
2380 else if (in4_addr_is_set(&address->in_addr_peer.in))
2381 log_broadcast(address, "Broadcast address is set for an IPv4 address with peer address.");
2382 else if (!in4_addr_is_set(&address->in_addr.in))
2383 log_broadcast(address, "Broadcast address is set for an IPv4 address with null address.");
2384 else
2385 /* Otherwise, keep the specified broadcast address. */
2386 return;
2387
2388 address->broadcast.s_addr = 0;
2389 }
2390
2391 #define log_address_section(address, fmt, ...) \
2392 ({ \
2393 const Address *_address = (address); \
2394 log_section_warning_errno( \
2395 _address ? _address->section : NULL, \
2396 SYNTHETIC_ERRNO(EINVAL), \
2397 fmt " Ignoring [Address] section.", \
2398 ##__VA_ARGS__); \
2399 })
2400
2401 int address_section_verify(Address *address) {
2402 assert(address);
2403 assert(address->section);
2404
2405 if (section_is_invalid(address->section))
2406 return -EINVAL;
2407
2408 if (address->family == AF_UNSPEC)
2409 return log_address_section(address, "Address section without Address= field was configured.");
2410 assert(IN_SET(address->family, AF_INET, AF_INET6));
2411
2412 if (address->family == AF_INET6 && !socket_ipv6_is_supported())
2413 return log_address_section(address, "An IPv6 address was configured, but the kernel does not support IPv6.");
2414
2415 if (in_addr_is_null(address->family, &address->in_addr)) {
2416 /* Will use address from address pool. Note that for ipv6 case, prefix of the address
2417 * pool is 8, but 40 bit is used by the global ID and 16 bit by the subnet ID. So,
2418 * let's limit the prefix length to 64 or larger. See RFC4193. */
2419 unsigned min_prefixlen = address->family == AF_INET ? 8 : 64;
2420 if (address->prefixlen < min_prefixlen)
2421 return log_address_section(address, "Prefix length for %s null address must be equal or larger than %u.",
2422 af_to_ipv4_ipv6(address->family), min_prefixlen);
2423
2424 address->requested_as_null = !in_addr_is_set(address->family, &address->in_addr);
2425 }
2426
2427 address_section_adjust_broadcast(address);
2428
2429 if (address->family == AF_INET6 && address->label) {
2430 log_section_warning(address->section, "Address label is set for IPv6 address, ignoring Label= setting.");
2431 address->label = mfree(address->label);
2432 }
2433
2434 if (!address->scope_set) {
2435 if (in_addr_is_localhost(address->family, &address->in_addr) > 0)
2436 address->scope = RT_SCOPE_HOST;
2437 else if (in_addr_is_link_local(address->family, &address->in_addr) > 0)
2438 address->scope = RT_SCOPE_LINK;
2439 }
2440
2441 if (address->duplicate_address_detection < 0) {
2442 if (address->family == AF_INET6)
2443 address->duplicate_address_detection = ADDRESS_FAMILY_IPV6;
2444 else if (in4_addr_is_link_local(&address->in_addr.in))
2445 address->duplicate_address_detection = ADDRESS_FAMILY_IPV4;
2446 else
2447 address->duplicate_address_detection = ADDRESS_FAMILY_NO;
2448 } else if (address->duplicate_address_detection == ADDRESS_FAMILY_IPV6 && address->family == AF_INET)
2449 log_section_warning(address->section, "DuplicateAddressDetection=ipv6 is specified for IPv4 address, ignoring.");
2450 else if (address->duplicate_address_detection == ADDRESS_FAMILY_IPV4 && address->family == AF_INET6)
2451 log_section_warning(address->section, "DuplicateAddressDetection=ipv4 is specified for IPv6 address, ignoring.");
2452
2453 if (address->family == AF_INET6 &&
2454 !FLAGS_SET(address->duplicate_address_detection, ADDRESS_FAMILY_IPV6))
2455 address->flags |= IFA_F_NODAD;
2456
2457 uint32_t filtered_flags = address->family == AF_INET ?
2458 address->flags & KNOWN_FLAGS & ~UNMANAGED_FLAGS & ~IPV6ONLY_FLAGS :
2459 address->flags & KNOWN_FLAGS & ~UNMANAGED_FLAGS;
2460 if (address->flags != filtered_flags) {
2461 _cleanup_free_ char *str = NULL;
2462
2463 (void) address_flags_to_string_alloc(address->flags ^ filtered_flags, address->family, &str);
2464 return log_address_section(address, "unexpected address flags \"%s\" were configured.", strna(str));
2465 }
2466
2467 return 0;
2468 }
2469
2470 DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
2471 trivial_hash_ops_address_detach,
2472 void,
2473 trivial_hash_func,
2474 trivial_compare_func,
2475 Address,
2476 address_detach);
2477
2478 int network_drop_invalid_addresses(Network *network) {
2479 _cleanup_set_free_ Set *addresses = NULL, *duplicated_addresses = NULL;
2480 Address *address;
2481 int r;
2482
2483 assert(network);
2484
2485 ORDERED_HASHMAP_FOREACH(address, network->addresses_by_section) {
2486 if (address_section_verify(address) < 0) {
2487 /* Drop invalid [Address] sections or Address= settings in [Network].
2488 * Note that address_detach() will drop the address from addresses_by_section. */
2489 address_detach(address);
2490 continue;
2491 }
2492
2493 /* Always use the setting specified later. So, remove the previously assigned setting. */
2494 Address *dup = set_remove(addresses, address);
2495 if (dup) {
2496 log_warning("%s: Duplicated address %s is specified at line %u and %u, "
2497 "dropping the address setting specified at line %u.",
2498 dup->section->filename,
2499 IN_ADDR_PREFIX_TO_STRING(address->family, &address->in_addr, address->prefixlen),
2500 address->section->line,
2501 dup->section->line, dup->section->line);
2502
2503 /* Do not call address_detach() for 'dup' now, as we can remove only the current
2504 * entry in the loop. We will drop the address from addresses_by_section later. */
2505 r = set_ensure_put(&duplicated_addresses, &trivial_hash_ops_address_detach, dup);
2506 if (r < 0)
2507 return log_oom();
2508 assert(r > 0);
2509 }
2510
2511 /* Use address_hash_ops, instead of address_hash_ops_detach. Otherwise, the Address objects
2512 * will be detached. */
2513 r = set_ensure_put(&addresses, &address_hash_ops, address);
2514 if (r < 0)
2515 return log_oom();
2516 assert(r > 0);
2517 }
2518
2519 /* Detach duplicated entries now. */
2520 duplicated_addresses = set_free(duplicated_addresses);
2521
2522 r = network_adjust_dhcp_server(network, &addresses);
2523 if (r < 0)
2524 return r;
2525
2526 network_adjust_dhcp_relay(network);
2527 return 0;
2528 }