1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
7 #include "sd-netlink.h"
10 #include "alloc-util.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-server.h"
23 #include "networkd-ipv4acd.h"
24 #include "networkd-link.h"
25 #include "networkd-manager.h"
26 #include "networkd-ndisc.h"
27 #include "networkd-netlabel.h"
28 #include "networkd-network.h"
29 #include "networkd-queue.h"
30 #include "networkd-route.h"
31 #include "networkd-route-util.h"
32 #include "ordered-set.h"
33 #include "parse-util.h"
35 #include "siphash24.h"
36 #include "socket-util.h"
37 #include "string-util.h"
40 #define ADDRESSES_PER_LINK_MAX 16384U
41 #define STATIC_ADDRESSES_PER_NETWORK_MAX 8192U
52 IFA_F_MANAGETEMPADDR | \
53 IFA_F_NOPREFIXROUTE | \
57 /* From net/ipv4/devinet.c */
58 #define IPV6ONLY_FLAGS \
64 IFA_F_MANAGETEMPADDR | \
67 /* We do not control the following flags. */
68 #define UNMANAGED_FLAGS \
76 int address_flags_to_string_alloc(uint32_t flags
, int family
, char **ret
) {
77 _cleanup_free_
char *str
= NULL
;
78 static const char* map
[] = {
79 [LOG2U(IFA_F_SECONDARY
)] = "secondary", /* This is also called "temporary" for ipv6. */
80 [LOG2U(IFA_F_NODAD
)] = "nodad",
81 [LOG2U(IFA_F_OPTIMISTIC
)] = "optimistic",
82 [LOG2U(IFA_F_DADFAILED
)] = "dadfailed",
83 [LOG2U(IFA_F_HOMEADDRESS
)] = "home-address",
84 [LOG2U(IFA_F_DEPRECATED
)] = "deprecated",
85 [LOG2U(IFA_F_TENTATIVE
)] = "tentative",
86 [LOG2U(IFA_F_PERMANENT
)] = "permanent",
87 [LOG2U(IFA_F_MANAGETEMPADDR
)] = "manage-temporary-address",
88 [LOG2U(IFA_F_NOPREFIXROUTE
)] = "no-prefixroute",
89 [LOG2U(IFA_F_MCAUTOJOIN
)] = "auto-join",
90 [LOG2U(IFA_F_STABLE_PRIVACY
)] = "stable-privacy",
93 assert(IN_SET(family
, AF_INET
, AF_INET6
));
96 for (size_t i
= 0; i
< ELEMENTSOF(map
); i
++)
97 if (BIT_SET(flags
, i
) && map
[i
])
98 if (!strextend_with_separator(
100 family
== AF_INET6
&& (1 << i
) == IFA_F_SECONDARY
? "temporary" : map
[i
]))
103 *ret
= TAKE_PTR(str
);
107 static LinkAddressState
address_state_from_scope(uint8_t scope
) {
108 if (scope
< RT_SCOPE_SITE
)
109 /* universally accessible addresses found */
110 return LINK_ADDRESS_STATE_ROUTABLE
;
112 if (scope
< RT_SCOPE_HOST
)
113 /* only link or site local addresses found */
114 return LINK_ADDRESS_STATE_DEGRADED
;
116 /* no useful addresses found */
117 return LINK_ADDRESS_STATE_OFF
;
120 void link_get_address_states(
122 LinkAddressState
*ret_ipv4
,
123 LinkAddressState
*ret_ipv6
,
124 LinkAddressState
*ret_all
) {
126 uint8_t ipv4_scope
= RT_SCOPE_NOWHERE
, ipv6_scope
= RT_SCOPE_NOWHERE
;
131 SET_FOREACH(address
, link
->addresses
) {
132 if (!address_is_ready(address
))
135 if (address
->family
== AF_INET
)
136 ipv4_scope
= MIN(ipv4_scope
, address
->scope
);
138 if (address
->family
== AF_INET6
)
139 ipv6_scope
= MIN(ipv6_scope
, address
->scope
);
143 *ret_ipv4
= address_state_from_scope(ipv4_scope
);
145 *ret_ipv6
= address_state_from_scope(ipv6_scope
);
147 *ret_all
= address_state_from_scope(MIN(ipv4_scope
, ipv6_scope
));
150 static void address_detach(Address
*address
);
152 DEFINE_PRIVATE_HASH_OPS_WITH_KEY_DESTRUCTOR(
153 address_hash_ops_detach
,
156 address_compare_func
,
163 address_compare_func
);
165 DEFINE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
166 address_section_hash_ops
,
168 config_section_hash_func
,
169 config_section_compare_func
,
173 int address_new(Address
**ret
) {
174 _cleanup_(address_unrefp
) Address
*address
= NULL
;
176 address
= new(Address
, 1);
180 *address
= (Address
) {
183 .scope
= RT_SCOPE_UNIVERSE
,
184 .lifetime_valid_usec
= USEC_INFINITY
,
185 .lifetime_preferred_usec
= USEC_INFINITY
,
189 *ret
= TAKE_PTR(address
);
194 int address_new_static(Network
*network
, const char *filename
, unsigned section_line
, Address
**ret
) {
195 _cleanup_(config_section_freep
) ConfigSection
*n
= NULL
;
196 _cleanup_(address_unrefp
) Address
*address
= NULL
;
202 assert(section_line
> 0);
204 r
= config_section_new(filename
, section_line
, &n
);
208 address
= ordered_hashmap_get(network
->addresses_by_section
, n
);
210 *ret
= TAKE_PTR(address
);
214 if (ordered_hashmap_size(network
->addresses_by_section
) >= STATIC_ADDRESSES_PER_NETWORK_MAX
)
217 r
= address_new(&address
);
221 address
->network
= network
;
222 address
->section
= TAKE_PTR(n
);
223 address
->source
= NETWORK_CONFIG_SOURCE_STATIC
;
224 /* This will be adjusted in address_section_verify(). */
225 address
->duplicate_address_detection
= _ADDRESS_FAMILY_INVALID
;
227 r
= ordered_hashmap_ensure_put(&network
->addresses_by_section
, &address_section_hash_ops
, address
->section
, address
);
231 *ret
= TAKE_PTR(address
);
235 static Address
* address_detach_impl(Address
*address
) {
237 assert(!address
->link
|| !address
->network
);
239 if (address
->network
) {
240 assert(address
->section
);
241 ordered_hashmap_remove(address
->network
->addresses_by_section
, address
->section
);
243 if (address
->network
->dhcp_server_address
== address
)
244 address
->network
->dhcp_server_address
= NULL
;
246 address
->network
= NULL
;
251 set_remove(address
->link
->addresses
, address
);
253 address
->link
= NULL
;
260 static void address_detach(Address
*address
) {
263 address_unref(address_detach_impl(address
));
266 static Address
* address_free(Address
*address
) {
270 address_detach_impl(address
);
272 config_section_free(address
->section
);
273 free(address
->label
);
274 free(address
->netlabel
);
275 ipv6_token_unref(address
->token
);
276 nft_set_context_clear(&address
->nft_set_context
);
277 return mfree(address
);
280 DEFINE_TRIVIAL_REF_UNREF_FUNC(Address
, address
, address_free
);
282 static bool address_lifetime_is_valid(const Address
*a
) {
286 a
->lifetime_valid_usec
== USEC_INFINITY
||
287 a
->lifetime_valid_usec
> now(CLOCK_BOOTTIME
);
290 bool address_is_ready(const Address
*a
) {
294 if (!ipv4acd_bound(a
->link
, a
))
297 if (FLAGS_SET(a
->flags
, IFA_F_TENTATIVE
))
300 if (FLAGS_SET(a
->state
, NETWORK_CONFIG_STATE_REMOVING
))
303 if (!FLAGS_SET(a
->state
, NETWORK_CONFIG_STATE_CONFIGURED
))
306 return address_lifetime_is_valid(a
);
309 bool link_check_addresses_ready(Link
*link
, NetworkConfigSource source
) {
315 /* Check if all addresses on the interface are ready. If there is no address, this will return false. */
317 SET_FOREACH(a
, link
->addresses
) {
318 if (source
>= 0 && a
->source
!= source
)
320 if (address_is_marked(a
))
322 if (!address_exists(a
))
324 if (!address_is_ready(a
))
329 if (has
|| source
!= NETWORK_CONFIG_SOURCE_DHCP6
)
332 /* If there is no DHCPv6 addresses, but all conflicting addresses are successfully configured, then
333 * let's handle the DHCPv6 client successfully configured addresses. Otherwise, if the conflicted
334 * addresses are static or foreign, and there is no other dynamic addressing protocol enabled, then
335 * the link will never enter the configured state. See also link_check_ready(). */
336 SET_FOREACH(a
, link
->addresses
) {
337 if (source
>= 0 && a
->source
== NETWORK_CONFIG_SOURCE_DHCP6
)
339 if (!a
->also_requested_by_dhcp6
)
341 if (address_is_marked(a
))
343 if (!address_exists(a
))
345 if (!address_is_ready(a
))
353 void link_mark_addresses(Link
*link
, NetworkConfigSource source
) {
358 SET_FOREACH(a
, link
->addresses
) {
359 if (a
->source
!= source
)
366 static int address_get_broadcast(const Address
*a
, Link
*link
, struct in_addr
*ret
) {
367 struct in_addr b_addr
= {};
372 /* Returns 0 when broadcast address is null, 1 when non-null broadcast address, -EAGAIN when the main
373 * address is null. */
375 /* broadcast is only for IPv4. */
376 if (a
->family
!= AF_INET
)
379 /* broadcast address cannot be used when peer address is specified. */
380 if (in4_addr_is_set(&a
->in_addr_peer
.in
))
383 /* A /31 or /32 IPv4 address does not have a broadcast address.
384 * See https://tools.ietf.org/html/rfc3021 */
385 if (a
->prefixlen
> 30)
388 /* If explicitly configured, use the address as is. */
389 if (in4_addr_is_set(&a
->broadcast
)) {
390 b_addr
= a
->broadcast
;
394 /* If explicitly disabled, then return null address. */
395 if (a
->set_broadcast
== 0)
398 /* For wireguard interfaces, broadcast is disabled by default. */
399 if (a
->set_broadcast
< 0 && streq_ptr(link
->kind
, "wireguard"))
402 /* If the main address is null, e.g. Address=0.0.0.0/24, the broadcast address will be automatically
403 * determined after an address is acquired. */
404 if (!in4_addr_is_set(&a
->in_addr
.in
))
407 /* Otherwise, generate a broadcast address from the main address and prefix length. */
408 b_addr
.s_addr
= a
->in_addr
.in
.s_addr
| htobe32(UINT32_C(0xffffffff) >> a
->prefixlen
);
414 return in4_addr_is_set(&b_addr
);
417 static void address_set_broadcast(Address
*a
, Link
*link
) {
419 assert_se(address_get_broadcast(a
, link
, &a
->broadcast
) >= 0);
422 static void address_set_cinfo(Manager
*m
, const Address
*a
, struct ifa_cacheinfo
*cinfo
) {
429 assert_se(sd_event_now(m
->event
, CLOCK_BOOTTIME
, &now_usec
) >= 0);
431 *cinfo
= (struct ifa_cacheinfo
) {
432 .ifa_valid
= usec_to_sec(a
->lifetime_valid_usec
, now_usec
),
433 .ifa_prefered
= usec_to_sec(a
->lifetime_preferred_usec
, now_usec
),
437 static void address_set_lifetime(Manager
*m
, Address
*a
, const struct ifa_cacheinfo
*cinfo
) {
444 assert_se(sd_event_now(m
->event
, CLOCK_BOOTTIME
, &now_usec
) >= 0);
446 a
->lifetime_valid_usec
= sec_to_usec(cinfo
->ifa_valid
, now_usec
);
447 a
->lifetime_preferred_usec
= sec_to_usec(cinfo
->ifa_prefered
, now_usec
);
450 static bool address_is_static_null(const Address
*address
) {
453 if (!address
->network
)
456 if (!address
->requested_as_null
)
459 assert(!in_addr_is_set(address
->family
, &address
->in_addr
));
463 static int address_ipv4_prefix(const Address
*a
, struct in_addr
*ret
) {
468 assert(a
->family
== AF_INET
);
471 p
= in4_addr_is_set(&a
->in_addr_peer
.in
) ? a
->in_addr_peer
.in
: a
->in_addr
.in
;
472 r
= in4_addr_mask(&p
, a
->prefixlen
);
480 void address_hash_func(const Address
*a
, struct siphash
*state
) {
483 siphash24_compress_typesafe(a
->family
, state
);
487 struct in_addr prefix
;
489 siphash24_compress_typesafe(a
->prefixlen
, state
);
491 assert_se(address_ipv4_prefix(a
, &prefix
) >= 0);
492 siphash24_compress_typesafe(prefix
, state
);
494 siphash24_compress_typesafe(a
->in_addr
.in
, state
);
498 siphash24_compress_typesafe(a
->in_addr
.in6
, state
);
500 if (in6_addr_is_null(&a
->in_addr
.in6
))
501 siphash24_compress_typesafe(a
->prefixlen
, state
);
505 assert_not_reached();
509 int address_compare_func(const Address
*a1
, const Address
*a2
) {
512 r
= CMP(a1
->family
, a2
->family
);
516 switch (a1
->family
) {
518 struct in_addr p1
, p2
;
520 /* See kernel's find_matching_ifa() in net/ipv4/devinet.c */
521 r
= CMP(a1
->prefixlen
, a2
->prefixlen
);
525 assert_se(address_ipv4_prefix(a1
, &p1
) >= 0);
526 assert_se(address_ipv4_prefix(a2
, &p2
) >= 0);
527 r
= memcmp(&p1
, &p2
, sizeof(p1
));
531 return memcmp(&a1
->in_addr
.in
, &a2
->in_addr
.in
, sizeof(a1
->in_addr
.in
));
534 /* See kernel's ipv6_get_ifaddr() in net/ipv6/addrconf.c */
535 r
= memcmp(&a1
->in_addr
.in6
, &a2
->in_addr
.in6
, sizeof(a1
->in_addr
.in6
));
539 /* To distinguish IPv6 null addresses with different prefixlen, e.g. ::48 vs ::64, let's
540 * compare the prefix length. */
541 if (in6_addr_is_null(&a1
->in_addr
.in6
))
542 r
= CMP(a1
->prefixlen
, a2
->prefixlen
);
547 assert_not_reached();
551 bool address_can_update(const Address
*existing
, const Address
*requesting
) {
556 * property | IPv4 | IPv6
557 * -----------------------------------------
559 * prefixlen | ✗ | ✗
560 * address | ✗ | ✗
563 * broadcast | ✗ | -
566 * lifetime | ✓ | ✓
567 * route metric | ✓ | ✓
568 * protocol | ✓ | ✓
570 * ✗ : cannot be changed
571 * ✓ : can be changed
574 * IPv4 : See inet_rtm_newaddr() in net/ipv4/devinet.c.
575 * IPv6 : See inet6_addr_modify() in net/ipv6/addrconf.c.
578 if (existing
->family
!= requesting
->family
)
581 if (existing
->prefixlen
!= requesting
->prefixlen
)
584 /* When a null address is requested, the address to be assigned/updated will be determined later. */
585 if (!address_is_static_null(requesting
) &&
586 in_addr_equal(existing
->family
, &existing
->in_addr
, &requesting
->in_addr
) <= 0)
589 switch (existing
->family
) {
591 struct in_addr bcast
;
593 if (existing
->scope
!= requesting
->scope
)
595 if (((existing
->flags
^ requesting
->flags
) & KNOWN_FLAGS
& ~IPV6ONLY_FLAGS
& ~UNMANAGED_FLAGS
) != 0)
597 if (!streq_ptr(existing
->label
, requesting
->label
))
599 if (!in4_addr_equal(&existing
->in_addr_peer
.in
, &requesting
->in_addr_peer
.in
))
601 if (existing
->link
&& address_get_broadcast(requesting
, existing
->link
, &bcast
) >= 0) {
602 /* If the broadcast address can be determined now, check if they match. */
603 if (!in4_addr_equal(&existing
->broadcast
, &bcast
))
606 /* When a null address is requested, then the broadcast address will be
607 * automatically calculated from the acquired address, e.g.
608 * 192.168.0.10/24 -> 192.168.0.255
609 * So, here let's only check if the broadcast is the last address in the range, e.g.
610 * 0.0.0.0/24 -> 0.0.0.255 */
611 if (!FLAGS_SET(existing
->broadcast
.s_addr
, htobe32(UINT32_C(0xffffffff) >> existing
->prefixlen
)))
620 assert_not_reached();
626 int address_dup(const Address
*src
, Address
**ret
) {
627 _cleanup_(address_unrefp
) Address
*dest
= NULL
;
633 dest
= newdup(Address
, src
, 1);
637 /* clear the reference counter and all pointers */
639 dest
->network
= NULL
;
640 dest
->section
= NULL
;
643 dest
->token
= ipv6_token_ref(src
->token
);
644 dest
->netlabel
= NULL
;
645 dest
->nft_set_context
.sets
= NULL
;
646 dest
->nft_set_context
.n_sets
= 0;
648 if (src
->family
== AF_INET
) {
649 r
= strdup_to(&dest
->label
, src
->label
);
654 r
= strdup_to(&dest
->netlabel
, src
->netlabel
);
658 r
= nft_set_context_dup(&src
->nft_set_context
, &dest
->nft_set_context
);
662 *ret
= TAKE_PTR(dest
);
666 static int address_set_masquerade(Address
*address
, bool add
) {
667 union in_addr_union masked
;
671 assert(address
->link
);
673 if (!address
->link
->network
)
676 if (!FLAGS_SET(address
->link
->network
->ip_masquerade
, AF_TO_ADDRESS_FAMILY(address
->family
)))
679 if (address
->scope
>= RT_SCOPE_LINK
)
682 if (address
->ip_masquerade_done
== add
)
685 masked
= address
->in_addr
;
686 r
= in_addr_mask(address
->family
, &masked
, address
->prefixlen
);
690 r
= fw_add_masquerade(&address
->link
->manager
->fw_ctx
, add
, address
->family
, &masked
, address
->prefixlen
);
694 address
->ip_masquerade_done
= add
;
699 static void address_modify_nft_set_context(Address
*address
, bool add
, NFTSetContext
*nft_set_context
) {
703 assert(address
->link
);
704 assert(address
->link
->manager
);
705 assert(nft_set_context
);
707 if (!address
->link
->manager
->fw_ctx
) {
708 r
= fw_ctx_new_full(&address
->link
->manager
->fw_ctx
, /* init_tables= */ false);
713 FOREACH_ARRAY(nft_set
, nft_set_context
->sets
, nft_set_context
->n_sets
) {
718 switch (nft_set
->source
) {
719 case NFT_SET_SOURCE_ADDRESS
:
720 r
= nft_set_element_modify_ip(address
->link
->manager
->fw_ctx
, add
, nft_set
->nfproto
, address
->family
, nft_set
->table
, nft_set
->set
,
723 case NFT_SET_SOURCE_PREFIX
:
724 r
= nft_set_element_modify_iprange(address
->link
->manager
->fw_ctx
, add
, nft_set
->nfproto
, address
->family
, nft_set
->table
, nft_set
->set
,
725 &address
->in_addr
, address
->prefixlen
);
727 case NFT_SET_SOURCE_IFINDEX
:
728 ifindex
= address
->link
->ifindex
;
729 r
= nft_set_element_modify_any(address
->link
->manager
->fw_ctx
, add
, nft_set
->nfproto
, nft_set
->table
, nft_set
->set
,
730 &ifindex
, sizeof(ifindex
));
733 assert_not_reached();
737 log_warning_errno(r
, "Failed to %s NFT set: family %s, table %s, set %s, IP address %s, ignoring: %m",
738 add
? "add" : "delete",
739 nfproto_to_string(nft_set
->nfproto
), nft_set
->table
, nft_set
->set
,
740 IN_ADDR_PREFIX_TO_STRING(address
->family
, &address
->in_addr
, address
->prefixlen
));
742 log_debug("%s NFT set: family %s, table %s, set %s, IP address %s",
743 add
? "Added" : "Deleted",
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
));
749 static void address_modify_nft_set(Address
*address
, bool add
) {
751 assert(address
->link
);
753 if (!IN_SET(address
->family
, AF_INET
, AF_INET6
))
756 if (!address
->link
->network
)
759 switch (address
->source
) {
760 case NETWORK_CONFIG_SOURCE_DHCP4
:
761 return address_modify_nft_set_context(address
, add
, &address
->link
->network
->dhcp_nft_set_context
);
762 case NETWORK_CONFIG_SOURCE_DHCP6
:
763 return address_modify_nft_set_context(address
, add
, &address
->link
->network
->dhcp6_nft_set_context
);
764 case NETWORK_CONFIG_SOURCE_DHCP_PD
:
765 return address_modify_nft_set_context(address
, add
, &address
->link
->network
->dhcp_pd_nft_set_context
);
766 case NETWORK_CONFIG_SOURCE_NDISC
:
767 return address_modify_nft_set_context(address
, add
, &address
->link
->network
->ndisc_nft_set_context
);
768 case NETWORK_CONFIG_SOURCE_STATIC
:
769 return address_modify_nft_set_context(address
, add
, &address
->nft_set_context
);
775 static int address_attach(Link
*link
, Address
*address
) {
780 assert(!address
->link
);
782 r
= set_ensure_put(&link
->addresses
, &address_hash_ops_detach
, address
);
788 address
->link
= link
;
789 address_ref(address
);
793 static int address_update(Address
*address
) {
794 Link
*link
= ASSERT_PTR(ASSERT_PTR(address
)->link
);
797 if (address_is_ready(address
) &&
798 address
->family
== AF_INET6
&&
799 in6_addr_is_link_local(&address
->in_addr
.in6
) &&
800 in6_addr_is_null(&link
->ipv6ll_address
)) {
802 link
->ipv6ll_address
= address
->in_addr
.in6
;
804 r
= link_ipv6ll_gained(link
);
809 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
812 r
= address_set_masquerade(address
, /* add = */ true);
814 return log_link_warning_errno(link
, r
, "Could not enable IP masquerading: %m");
816 address_add_netlabel(address
);
818 address_modify_nft_set(address
, /* add = */ true);
820 if (address_is_ready(address
) && address
->callback
) {
821 r
= address
->callback(address
);
826 link_update_operstate(link
, /* also_update_bond_master = */ true);
827 link_check_ready(link
);
831 static int address_removed_maybe_kernel_dad(Link
*link
, Address
*address
) {
837 if (!IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
))
840 if (address
->family
!= AF_INET6
)
843 if (!FLAGS_SET(address
->flags
, IFA_F_TENTATIVE
))
846 log_link_info(link
, "Address %s with tentative flag is removed, maybe a duplicated address is assigned on another node or link?",
847 IN6_ADDR_TO_STRING(&address
->in_addr
.in6
));
849 /* Reset the address state, as the object may be reused in the below. */
852 switch (address
->source
) {
853 case NETWORK_CONFIG_SOURCE_STATIC
:
854 r
= link_reconfigure_radv_address(address
, link
);
856 case NETWORK_CONFIG_SOURCE_DHCP_PD
:
857 r
= dhcp_pd_reconfigure_address(address
, link
);
859 case NETWORK_CONFIG_SOURCE_NDISC
:
860 r
= ndisc_reconfigure_address(address
, link
);
866 return log_link_warning_errno(link
, r
, "Failed to configure an alternative address: %m");
871 static int address_drop(Address
*in
, bool removed_by_us
) {
872 _cleanup_(address_unrefp
) Address
*address
= address_ref(ASSERT_PTR(in
));
873 Link
*link
= ASSERT_PTR(address
->link
);
876 r
= address_set_masquerade(address
, /* add = */ false);
878 log_link_warning_errno(link
, r
, "Failed to disable IP masquerading, ignoring: %m");
880 address_modify_nft_set(address
, /* add = */ false);
882 address_del_netlabel(address
);
884 /* FIXME: if the IPv6LL address is dropped, stop DHCPv6, NDISC, RADV. */
885 if (address
->family
== AF_INET6
&&
886 in6_addr_equal(&address
->in_addr
.in6
, &link
->ipv6ll_address
))
887 link
->ipv6ll_address
= (const struct in6_addr
) {};
889 ipv4acd_detach(link
, address
);
891 address_detach(address
);
893 if (!removed_by_us
) {
894 r
= address_removed_maybe_kernel_dad(link
, address
);
896 link_enter_failed(link
);
901 link_update_operstate(link
, /* also_update_bond_master = */ true);
902 link_check_ready(link
);
906 static bool address_match_null(const Address
*a
, const Address
*null_address
) {
908 assert(null_address
);
910 if (!a
->requested_as_null
)
913 /* Currently, null address is supported only by static addresses. Note that static
914 * address may be set as foreign during reconfiguring the interface. */
915 if (!IN_SET(a
->source
, NETWORK_CONFIG_SOURCE_FOREIGN
, NETWORK_CONFIG_SOURCE_STATIC
))
918 if (a
->family
!= null_address
->family
)
921 if (a
->prefixlen
!= null_address
->prefixlen
)
927 static int address_get_request(Link
*link
, const Address
*address
, Request
**ret
) {
931 assert(link
->manager
);
934 req
= ordered_set_get(
935 link
->manager
->request_queue
,
938 .type
= REQUEST_TYPE_ADDRESS
,
939 .userdata
= (void*) address
,
940 .hash_func
= (hash_func_t
) address_hash_func
,
941 .compare_func
= (compare_func_t
) address_compare_func
,
949 if (address_is_static_null(address
))
950 ORDERED_SET_FOREACH(req
, link
->manager
->request_queue
) {
951 if (req
->link
!= link
)
953 if (req
->type
!= REQUEST_TYPE_ADDRESS
)
956 if (!address_match_null(req
->userdata
, address
))
968 int address_get(Link
*link
, const Address
*in
, Address
**ret
) {
974 a
= set_get(link
->addresses
, in
);
981 /* Find matching address that originally requested as null address. */
982 if (address_is_static_null(in
))
983 SET_FOREACH(a
, link
->addresses
) {
984 if (!address_match_null(a
, in
))
995 int address_get_harder(Link
*link
, const Address
*in
, Address
**ret
) {
1002 if (address_get(link
, in
, ret
) >= 0)
1005 r
= address_get_request(link
, in
, &req
);
1010 *ret
= ASSERT_PTR(req
->userdata
);
1015 int link_get_address_full(
1018 const union in_addr_union
*address
,
1019 const union in_addr_union
*peer
, /* optional, can be NULL */
1020 unsigned char prefixlen
, /* optional, can be 0 */
1027 assert(IN_SET(family
, AF_INET
, AF_INET6
));
1030 /* This finds an Address object on the link which matches the given address, peer, and prefix length.
1031 * If the prefixlen is zero, then an Address object with an arbitrary prefixlen will be returned.
1032 * If the peer is NULL, then an Address object with an arbitrary peer will be returned. */
1034 if (family
== AF_INET6
|| (prefixlen
!= 0 && peer
)) {
1035 _cleanup_(address_unrefp
) Address
*tmp
= NULL
;
1037 /* In this case, we can use address_get(). */
1039 r
= address_new(&tmp
);
1043 tmp
->family
= family
;
1044 tmp
->in_addr
= *address
;
1046 tmp
->in_addr_peer
= *peer
;
1047 tmp
->prefixlen
= prefixlen
;
1049 r
= address_get(link
, tmp
, &a
);
1053 if (family
== AF_INET6
) {
1054 /* IPv6 addresses are managed without peer address and prefix length. Hence, we need
1055 * to check them explicitly. */
1056 if (peer
&& !in_addr_equal(family
, &a
->in_addr_peer
, peer
))
1058 if (prefixlen
!= 0 && a
->prefixlen
!= prefixlen
)
1068 SET_FOREACH(a
, link
->addresses
) {
1069 if (a
->family
!= family
)
1072 if (!in_addr_equal(family
, &a
->in_addr
, address
))
1075 if (peer
&& !in_addr_equal(family
, &a
->in_addr_peer
, peer
))
1078 if (prefixlen
!= 0 && a
->prefixlen
!= prefixlen
)
1090 int manager_get_address_full(
1093 const union in_addr_union
*address
,
1094 const union in_addr_union
*peer
,
1095 unsigned char prefixlen
,
1101 assert(IN_SET(family
, AF_INET
, AF_INET6
));
1104 HASHMAP_FOREACH(link
, manager
->links_by_index
) {
1105 if (!IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
))
1108 if (link_get_address_full(link
, family
, address
, peer
, prefixlen
, ret
) >= 0)
1115 const char* format_lifetime(char *buf
, size_t l
, usec_t lifetime_usec
) {
1119 if (lifetime_usec
== USEC_INFINITY
)
1122 sprintf(buf
, "for ");
1123 /* format_timespan() never fails */
1124 assert_se(format_timespan(buf
+ 4, l
- 4, usec_sub_unsigned(lifetime_usec
, now(CLOCK_BOOTTIME
)), USEC_PER_SEC
));
1128 void log_address_debug(const Address
*address
, const char *str
, const Link
*link
) {
1129 _cleanup_free_
char *state
= NULL
, *flags_str
= NULL
, *scope_str
= NULL
;
1138 (void) network_config_state_to_string_alloc(address
->state
, &state
);
1140 const char *peer
= in_addr_is_set(address
->family
, &address
->in_addr_peer
) ?
1141 IN_ADDR_TO_STRING(address
->family
, &address
->in_addr_peer
) : NULL
;
1143 const char *broadcast
= (address
->family
== AF_INET
&& in4_addr_is_set(&address
->broadcast
)) ?
1144 IN4_ADDR_TO_STRING(&address
->broadcast
) : NULL
;
1146 (void) address_flags_to_string_alloc(address
->flags
, address
->family
, &flags_str
);
1147 (void) route_scope_to_string_alloc(address
->scope
, &scope_str
);
1149 log_link_debug(link
, "%s %s address (%s): %s%s%s/%u%s%s (valid %s, preferred %s), flags: %s, scope: %s%s%s",
1150 str
, strna(network_config_source_to_string(address
->source
)), strna(state
),
1151 IN_ADDR_TO_STRING(address
->family
, &address
->in_addr
),
1152 peer
? " peer " : "", strempty(peer
), address
->prefixlen
,
1153 broadcast
? " broadcast " : "", strempty(broadcast
),
1154 FORMAT_LIFETIME(address
->lifetime_valid_usec
),
1155 FORMAT_LIFETIME(address
->lifetime_preferred_usec
),
1156 strna(flags_str
), strna(scope_str
),
1157 address
->family
== AF_INET
? ", label: " : "",
1158 address
->family
== AF_INET
? strna(address
->label
) : "");
1161 static void address_forget(Link
*link
, Address
*address
, bool removed_by_us
, const char *msg
) {
1167 if (address_get_request(link
, address
, &req
) >= 0)
1168 address_enter_removed(req
->userdata
);
1170 if (!address
->link
&& address_get(link
, address
, &address
) < 0)
1173 address_enter_removed(address
);
1174 log_address_debug(address
, msg
, link
);
1175 (void) address_drop(address
, removed_by_us
);
1178 static int address_set_netlink_message(const Address
*address
, sd_netlink_message
*m
, Link
*link
) {
1185 r
= sd_rtnl_message_addr_set_prefixlen(m
, address
->prefixlen
);
1189 /* On remove, only IFA_F_MANAGETEMPADDR flag for IPv6 addresses are used. But anyway, set all
1190 * flags except tentative flag here unconditionally. Without setting the flag, the template
1191 * addresses generated by kernel will not be removed automatically when the main address is
1193 uint32_t flags
= address
->flags
& ~IFA_F_TENTATIVE
;
1195 r
= sd_netlink_message_append_u32(m
, IFA_FLAGS
, flags
);
1200 r
= netlink_message_append_in_addr_union(m
, IFA_LOCAL
, address
->family
, &address
->in_addr
);
1207 static int address_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, RemoveRequest
*rreq
) {
1213 Link
*link
= ASSERT_PTR(rreq
->link
);
1214 Address
*address
= ASSERT_PTR(rreq
->userdata
);
1216 if (link
->state
== LINK_STATE_LINGER
)
1219 r
= sd_netlink_message_get_errno(m
);
1221 log_link_message_full_errno(link
, m
,
1222 (r
== -EADDRNOTAVAIL
|| !address
->link
) ? LOG_DEBUG
: LOG_WARNING
,
1223 r
, "Could not drop address");
1225 /* If the address cannot be removed, then assume the address is already removed. */
1226 address_forget(link
, address
, /* removed_by_us = */ true, "Forgetting");
1232 int address_remove(Address
*address
, Link
*link
) {
1233 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
1237 assert(IN_SET(address
->family
, AF_INET
, AF_INET6
));
1239 assert(link
->ifindex
> 0);
1240 assert(link
->manager
);
1241 assert(link
->manager
->rtnl
);
1243 /* If the address is remembered, use the remembered object. */
1244 (void) address_get(link
, address
, &address
);
1246 log_address_debug(address
, "Removing", link
);
1248 r
= sd_rtnl_message_new_addr(link
->manager
->rtnl
, &m
, RTM_DELADDR
,
1249 link
->ifindex
, address
->family
);
1251 return log_link_warning_errno(link
, r
, "Could not allocate RTM_DELADDR message: %m");
1253 r
= address_set_netlink_message(address
, m
, link
);
1255 return log_link_warning_errno(link
, r
, "Could not set netlink attributes: %m");
1257 r
= link_remove_request_add(link
, address
, address
, link
->manager
->rtnl
, m
, address_remove_handler
);
1259 return log_link_warning_errno(link
, r
, "Could not queue rtnetlink message: %m");
1261 address_enter_removing(address
);
1263 /* The operational state is determined by address state and carrier state. Hence, if we remove
1264 * an address, the operational state may be changed. */
1265 link_update_operstate(link
, true);
1269 int address_remove_and_cancel(Address
*address
, Link
*link
) {
1270 _cleanup_(request_unrefp
) Request
*req
= NULL
;
1271 bool waiting
= false;
1275 assert(link
->manager
);
1277 /* If the address is remembered by the link, then use the remembered object. */
1278 (void) address_get(link
, address
, &address
);
1280 /* Cancel the request for the address. If the request is already called but we have not received the
1281 * notification about the request, then explicitly remove the address. */
1282 if (address_get_request(link
, address
, &req
) >= 0) {
1283 request_ref(req
); /* avoid the request freed by request_detach() */
1284 waiting
= req
->waiting_reply
;
1285 request_detach(req
);
1286 address_cancel_requesting(address
);
1289 /* If we know the address will come or already exists, remove it. */
1290 if (waiting
|| (address
->link
&& address_exists(address
)))
1291 return address_remove(address
, link
);
1296 bool link_address_is_dynamic(const Link
*link
, const Address
*address
) {
1300 assert(link
->manager
);
1303 if (address
->lifetime_preferred_usec
!= USEC_INFINITY
)
1306 /* There is no way to drtermine if the IPv6 address is dynamic when the lifetime is infinity. */
1307 if (address
->family
!= AF_INET
)
1310 /* Even if an IPv4 address is leased from a DHCP server with a finite lifetime, networkd assign the
1311 * address without lifetime when KeepConfiguration=dynamic. So, let's check that we have
1312 * corresponding routes with RTPROT_DHCP. */
1313 SET_FOREACH(route
, link
->manager
->routes
) {
1314 if (route
->source
!= NETWORK_CONFIG_SOURCE_FOREIGN
)
1317 /* The route is not assigned yet, or already being removed. Ignoring. */
1318 if (!route_exists(route
))
1321 if (route
->protocol
!= RTPROT_DHCP
)
1324 if (route
->nexthop
.ifindex
!= link
->ifindex
)
1327 if (address
->family
!= route
->family
)
1330 if (in_addr_equal(address
->family
, &address
->in_addr
, &route
->prefsrc
))
1337 int link_drop_ipv6ll_addresses(Link
*link
) {
1338 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
1342 assert(link
->manager
);
1343 assert(link
->manager
->rtnl
);
1345 /* IPv6LL address may be in the tentative state, and in that case networkd has not received it.
1346 * So, we need to dump all IPv6 addresses. */
1348 if (link_may_have_ipv6ll(link
, /* check_multicast = */ false))
1351 r
= sd_rtnl_message_new_addr(link
->manager
->rtnl
, &req
, RTM_GETADDR
, link
->ifindex
, AF_INET6
);
1355 r
= sd_netlink_message_set_request_dump(req
, true);
1359 r
= sd_netlink_call(link
->manager
->rtnl
, req
, 0, &reply
);
1363 for (sd_netlink_message
*addr
= reply
; addr
; addr
= sd_netlink_message_next(addr
)) {
1364 _cleanup_(address_unrefp
) Address
*a
= NULL
;
1365 unsigned char prefixlen
;
1366 struct in6_addr address
;
1369 /* We set ifindex in the request, and NETLINK_GET_STRICT_CHK socket option is set. Hence the
1370 * check below is redundant, but let's do that for safety. */
1371 r
= sd_rtnl_message_addr_get_ifindex(addr
, &ifindex
);
1373 log_link_debug_errno(link
, r
, "rtnl: received address message without valid ifindex, ignoring: %m");
1375 } else if (link
->ifindex
!= ifindex
)
1379 r
= sd_netlink_message_read_u32(addr
, IFA_FLAGS
, &flags
);
1381 log_link_debug_errno(link
, r
, "rtnl: Failed to read IFA_FLAGS attribute, ignoring: %m");
1385 r
= sd_rtnl_message_addr_get_prefixlen(addr
, &prefixlen
);
1387 log_link_debug_errno(link
, r
, "rtnl: received address message without prefixlen, ignoring: %m");
1391 if (sd_netlink_message_read_in6_addr(addr
, IFA_LOCAL
, NULL
) >= 0)
1392 /* address with peer, ignoring. */
1395 r
= sd_netlink_message_read_in6_addr(addr
, IFA_ADDRESS
, &address
);
1397 log_link_debug_errno(link
, r
, "rtnl: received address message without valid address, ignoring: %m");
1401 if (!in6_addr_is_link_local(&address
))
1404 r
= address_new(&a
);
1408 a
->family
= AF_INET6
;
1409 a
->in_addr
.in6
= address
;
1410 a
->prefixlen
= prefixlen
;
1413 r
= address_remove(a
, link
);
1421 int link_drop_unmanaged_addresses(Link
*link
) {
1426 assert(link
->network
);
1428 /* First, mark all addresses. */
1429 SET_FOREACH(address
, link
->addresses
) {
1430 /* We consider IPv6LL addresses to be managed by the kernel, or dropped in link_drop_ipv6ll_addresses() */
1431 if (address
->family
== AF_INET6
&& in6_addr_is_link_local(&address
->in_addr
.in6
))
1434 /* Do not remove localhost address (127.0.0.1 and ::1) */
1435 if (link
->flags
& IFF_LOOPBACK
&& in_addr_is_localhost_one(address
->family
, &address
->in_addr
) > 0)
1438 /* Ignore addresses not assigned yet or already removing. */
1439 if (!address_exists(address
))
1442 if (address
->source
== NETWORK_CONFIG_SOURCE_FOREIGN
) {
1443 if (link
->network
->keep_configuration
== KEEP_CONFIGURATION_YES
)
1446 /* link_address_is_dynamic() is slightly heavy. Let's call the function only when
1447 * KeepConfiguration=dynamic or static. */
1448 if (IN_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DYNAMIC
, KEEP_CONFIGURATION_STATIC
) &&
1449 link_address_is_dynamic(link
, address
) == (link
->network
->keep_configuration
== KEEP_CONFIGURATION_DYNAMIC
))
1452 } else if (address
->source
!= NETWORK_CONFIG_SOURCE_STATIC
)
1453 continue; /* Ignore dynamically configurad addresses. */
1455 address_mark(address
);
1458 /* Then, unmark requested addresses. */
1459 ORDERED_HASHMAP_FOREACH(address
, link
->network
->addresses_by_section
) {
1462 if (address_get(link
, address
, &existing
) < 0)
1465 if (!address_can_update(existing
, address
))
1468 /* Found matching static configuration. Keep the existing address. */
1469 address_unmark(existing
);
1472 /* Finally, remove all marked addresses. */
1473 SET_FOREACH(address
, link
->addresses
) {
1474 if (!address_is_marked(address
))
1477 RET_GATHER(r
, address_remove(address
, link
));
1483 int link_drop_static_addresses(Link
*link
) {
1489 SET_FOREACH(address
, link
->addresses
) {
1490 /* Remove only static addresses here. Dynamic addresses will be removed e.g. on lease
1491 * expiration or stopping the DHCP client. */
1492 if (address
->source
!= NETWORK_CONFIG_SOURCE_STATIC
)
1495 /* Ignore addresses not assigned yet or already removing. */
1496 if (!address_exists(address
))
1499 RET_GATHER(r
, address_remove(address
, link
));
1505 int address_configure_handler_internal(sd_netlink_message
*m
, Link
*link
, Address
*address
) {
1512 r
= sd_netlink_message_get_errno(m
);
1513 if (r
< 0 && r
!= -EEXIST
) {
1514 log_link_message_warning_errno(link
, m
, r
, "Failed to set %s address %s",
1515 network_config_source_to_string(address
->source
),
1516 IN_ADDR_TO_STRING(address
->family
, &address
->in_addr
));
1517 link_enter_failed(link
);
1524 static int address_configure(const Address
*address
, const struct ifa_cacheinfo
*c
, Link
*link
, Request
*req
) {
1525 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
1529 assert(IN_SET(address
->family
, AF_INET
, AF_INET6
));
1532 assert(link
->ifindex
> 0);
1533 assert(link
->manager
);
1534 assert(link
->manager
->rtnl
);
1537 log_address_debug(address
, "Configuring", link
);
1539 r
= sd_rtnl_message_new_addr_update(link
->manager
->rtnl
, &m
, link
->ifindex
, address
->family
);
1543 r
= address_set_netlink_message(address
, m
, link
);
1547 r
= sd_rtnl_message_addr_set_scope(m
, address
->scope
);
1551 if (address
->family
== AF_INET6
|| in_addr_is_set(address
->family
, &address
->in_addr_peer
)) {
1552 r
= netlink_message_append_in_addr_union(m
, IFA_ADDRESS
, address
->family
, &address
->in_addr_peer
);
1555 } else if (in4_addr_is_set(&address
->broadcast
)) {
1556 r
= sd_netlink_message_append_in_addr(m
, IFA_BROADCAST
, &address
->broadcast
);
1561 if (address
->family
== AF_INET
&& address
->label
) {
1562 r
= sd_netlink_message_append_string(m
, IFA_LABEL
, address
->label
);
1567 r
= sd_netlink_message_append_cache_info(m
, IFA_CACHEINFO
, c
);
1571 r
= sd_netlink_message_append_u32(m
, IFA_RT_PRIORITY
, address
->route_metric
);
1575 return request_call_netlink_async(link
->manager
->rtnl
, m
, req
);
1578 static int address_acquire(Link
*link
, const Address
*address
, union in_addr_union
*ret
) {
1579 union in_addr_union a
;
1586 r
= address_acquire_from_dhcp_server_leases_file(link
, address
, ret
);
1587 if (!IN_SET(r
, -ENOENT
, -ENXIO
, -EINVAL
))
1590 r
= address_pool_acquire(link
->manager
, address
->family
, address
->prefixlen
, &a
);
1596 /* Pick first address in range for ourselves. */
1597 if (address
->family
== AF_INET
)
1598 a
.in
.s_addr
|= htobe32(1);
1599 else if (address
->family
== AF_INET6
)
1600 a
.in6
.s6_addr
[15] |= 1;
1602 assert_not_reached();
1608 static int address_requeue_request(Request
*req
, Link
*link
, const Address
*address
) {
1613 assert(link
->manager
);
1614 assert(link
->network
);
1617 /* Something useful was configured? just use it */
1618 if (in_addr_is_set(address
->family
, &address
->in_addr
))
1621 /* The address is configured to be 0.0.0.0 or [::] by the user?
1622 * Then let's acquire something more useful. */
1623 union in_addr_union a
;
1624 r
= address_acquire(link
, address
, &a
);
1628 _cleanup_(address_unrefp
) Address
*tmp
= NULL
;
1629 r
= address_dup(address
, &tmp
);
1635 r
= link_requeue_request(link
, req
, tmp
, NULL
);
1639 return -EEXIST
; /* Already queued?? Strange... */
1642 return 1; /* A new request is queued. it is not necessary to process this request anymore. */
1645 static int address_process_request(Request
*req
, Link
*link
, Address
*address
) {
1647 struct ifa_cacheinfo c
;
1654 if (!link_is_ready_to_configure(link
, false))
1657 /* Refuse adding more than the limit */
1658 if (set_size(link
->addresses
) >= ADDRESSES_PER_LINK_MAX
)
1661 r
= address_requeue_request(req
, link
, address
);
1667 address_set_broadcast(address
, link
);
1669 r
= ipv4acd_configure(link
, address
);
1673 if (!ipv4acd_bound(link
, address
))
1676 address_set_cinfo(link
->manager
, address
, &c
);
1677 if (c
.ifa_valid
== 0) {
1678 log_link_debug(link
, "Refuse to configure %s address %s, as its valid lifetime is zero.",
1679 network_config_source_to_string(address
->source
),
1680 IN_ADDR_PREFIX_TO_STRING(address
->family
, &address
->in_addr
, address
->prefixlen
));
1682 address_cancel_requesting(address
);
1683 if (address_get(link
, address
, &existing
) >= 0)
1684 address_cancel_requesting(existing
);
1688 r
= address_configure(address
, &c
, link
, req
);
1690 return log_link_warning_errno(link
, r
, "Failed to configure address: %m");
1692 address_enter_configuring(address
);
1693 if (address_get(link
, address
, &existing
) >= 0)
1694 address_enter_configuring(existing
);
1699 int link_request_address(
1701 const Address
*address
,
1702 unsigned *message_counter
,
1703 address_netlink_handler_t netlink_handler
,
1706 _cleanup_(address_unrefp
) Address
*tmp
= NULL
;
1707 Address
*existing
= NULL
;
1712 assert(address
->source
!= NETWORK_CONFIG_SOURCE_FOREIGN
);
1714 if (address
->lifetime_valid_usec
== 0)
1715 /* The requested address is outdated. Let's ignore the request. */
1718 if (address_get_request(link
, address
, NULL
) >= 0)
1719 return 0; /* already requested, skipping. */
1721 r
= address_dup(address
, &tmp
);
1725 if (address_get(link
, address
, &existing
) >= 0) {
1726 /* Copy already assigned address when it is requested as a null address. */
1727 if (address_is_static_null(address
))
1728 tmp
->in_addr
= existing
->in_addr
;
1730 /* Copy state for logging below. */
1731 tmp
->state
= existing
->state
;
1734 log_address_debug(tmp
, "Requesting", link
);
1735 r
= link_queue_request_safe(link
, REQUEST_TYPE_ADDRESS
,
1739 address_compare_func
,
1740 address_process_request
,
1741 message_counter
, netlink_handler
, ret
);
1743 return log_link_warning_errno(link
, r
, "Failed to request address: %m");
1747 address_enter_requesting(tmp
);
1749 address_enter_requesting(existing
);
1755 static int static_address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Request
*req
, Link
*link
, Address
*address
) {
1761 r
= address_configure_handler_internal(m
, link
, address
);
1765 if (link
->static_address_messages
== 0) {
1766 log_link_debug(link
, "Addresses set");
1767 link
->static_addresses_configured
= true;
1768 link_check_ready(link
);
1774 int link_request_static_address(Link
*link
, const Address
*address
) {
1777 assert(address
->source
== NETWORK_CONFIG_SOURCE_STATIC
);
1779 return link_request_address(link
, address
, &link
->static_address_messages
,
1780 static_address_handler
, NULL
);
1783 int link_request_static_addresses(Link
*link
) {
1788 assert(link
->network
);
1790 link
->static_addresses_configured
= false;
1792 ORDERED_HASHMAP_FOREACH(a
, link
->network
->addresses_by_section
) {
1793 r
= link_request_static_address(link
, a
);
1798 r
= link_request_radv_addresses(link
);
1802 if (link
->static_address_messages
== 0) {
1803 link
->static_addresses_configured
= true;
1804 link_check_ready(link
);
1806 log_link_debug(link
, "Setting addresses");
1807 link_set_state(link
, LINK_STATE_CONFIGURING
);
1813 int manager_rtnl_process_address(sd_netlink
*rtnl
, sd_netlink_message
*message
, Manager
*m
) {
1820 if (sd_netlink_message_is_error(message
)) {
1821 r
= sd_netlink_message_get_errno(message
);
1823 log_message_warning_errno(message
, r
, "rtnl: failed to receive address message, ignoring");
1829 r
= sd_netlink_message_get_type(message
, &type
);
1831 log_warning_errno(r
, "rtnl: could not get message type, ignoring: %m");
1833 } else if (!IN_SET(type
, RTM_NEWADDR
, RTM_DELADDR
)) {
1834 log_warning("rtnl: received unexpected message type %u when processing address, ignoring.", type
);
1839 r
= sd_rtnl_message_addr_get_ifindex(message
, &ifindex
);
1841 log_warning_errno(r
, "rtnl: could not get ifindex from message, ignoring: %m");
1843 } else if (ifindex
<= 0) {
1844 log_warning("rtnl: received address message with invalid ifindex %d, ignoring.", ifindex
);
1849 r
= link_get_by_index(m
, ifindex
, &link
);
1851 /* when enumerating we might be out of sync, but we will get the address again, so just
1853 if (!m
->enumerating
)
1854 log_warning("rtnl: received address for link '%d' we don't know about, ignoring.", ifindex
);
1858 _cleanup_(address_unrefp
) Address
*tmp
= NULL
;
1859 r
= address_new(&tmp
);
1863 /* First, read minimal information to make address_get() work below. */
1865 r
= sd_rtnl_message_addr_get_family(message
, &tmp
->family
);
1867 log_link_warning(link
, "rtnl: received address message without family, ignoring.");
1869 } else if (!IN_SET(tmp
->family
, AF_INET
, AF_INET6
)) {
1870 log_link_debug(link
, "rtnl: received address message with invalid family '%i', ignoring.", tmp
->family
);
1874 r
= sd_rtnl_message_addr_get_prefixlen(message
, &tmp
->prefixlen
);
1876 log_link_warning_errno(link
, r
, "rtnl: received address message without prefixlen, ignoring: %m");
1880 switch (tmp
->family
) {
1882 r
= sd_netlink_message_read_in_addr(message
, IFA_LOCAL
, &tmp
->in_addr
.in
);
1884 log_link_warning_errno(link
, r
, "rtnl: received address message without valid address, ignoring: %m");
1888 r
= sd_netlink_message_read_in_addr(message
, IFA_ADDRESS
, &tmp
->in_addr_peer
.in
);
1889 if (r
< 0 && r
!= -ENODATA
) {
1890 log_link_warning_errno(link
, r
, "rtnl: could not get peer address from address message, ignoring: %m");
1892 } else if (r
>= 0) {
1893 if (in4_addr_equal(&tmp
->in_addr
.in
, &tmp
->in_addr_peer
.in
))
1894 tmp
->in_addr_peer
= IN_ADDR_NULL
;
1900 r
= sd_netlink_message_read_in6_addr(message
, IFA_LOCAL
, &tmp
->in_addr
.in6
);
1902 /* Have peer address. */
1903 r
= sd_netlink_message_read_in6_addr(message
, IFA_ADDRESS
, &tmp
->in_addr_peer
.in6
);
1905 log_link_warning_errno(link
, r
, "rtnl: could not get peer address from address message, ignoring: %m");
1908 } else if (r
== -ENODATA
) {
1909 /* Does not have peer address. */
1910 r
= sd_netlink_message_read_in6_addr(message
, IFA_ADDRESS
, &tmp
->in_addr
.in6
);
1912 log_link_warning_errno(link
, r
, "rtnl: received address message without valid address, ignoring: %m");
1916 log_link_warning_errno(link
, r
, "rtnl: could not get local address from address message, ignoring: %m");
1923 assert_not_reached();
1926 /* Then, find the managed Address object corresponding to the received address. */
1927 Address
*address
= NULL
;
1928 (void) address_get(link
, tmp
, &address
);
1930 if (type
== RTM_DELADDR
) {
1932 address_forget(link
, address
,
1933 /* removed_by_us = */ FLAGS_SET(address
->state
, NETWORK_CONFIG_STATE_REMOVING
),
1934 "Forgetting removed");
1936 log_address_debug(tmp
, "Kernel removed unknown", link
);
1941 bool is_new
= false;
1943 /* If we did not know the address, then save it. */
1944 r
= address_attach(link
, tmp
);
1946 log_link_warning_errno(link
, r
, "Failed to save received address %s, ignoring: %m",
1947 IN_ADDR_PREFIX_TO_STRING(tmp
->family
, &tmp
->in_addr
, tmp
->prefixlen
));
1955 /* Otherwise, update the managed Address object with the netlink notification. */
1956 address
->prefixlen
= tmp
->prefixlen
;
1957 address
->in_addr_peer
= tmp
->in_addr_peer
;
1960 /* Also update information that cannot be obtained through netlink notification. */
1961 Request
*req
= NULL
;
1962 (void) address_get_request(link
, tmp
, &req
);
1963 if (req
&& req
->waiting_reply
) {
1964 Address
*a
= ASSERT_PTR(req
->userdata
);
1966 address
->source
= a
->source
;
1967 address
->provider
= a
->provider
;
1968 (void) free_and_strdup_warn(&address
->netlabel
, a
->netlabel
);
1969 nft_set_context_clear(&address
->nft_set_context
);
1970 (void) nft_set_context_dup(&a
->nft_set_context
, &address
->nft_set_context
);
1971 address
->requested_as_null
= a
->requested_as_null
;
1972 address
->also_requested_by_dhcp6
= a
->also_requested_by_dhcp6
;
1973 address
->callback
= a
->callback
;
1975 ipv6_token_ref(a
->token
);
1976 ipv6_token_unref(address
->token
);
1977 address
->token
= a
->token
;
1980 /* Then, update miscellaneous info. */
1981 r
= sd_rtnl_message_addr_get_scope(message
, &address
->scope
);
1983 log_link_debug_errno(link
, r
, "rtnl: received address message without scope, ignoring: %m");
1985 if (address
->family
== AF_INET
) {
1986 _cleanup_free_
char *label
= NULL
;
1988 r
= sd_netlink_message_read_string_strdup(message
, IFA_LABEL
, &label
);
1990 if (!streq_ptr(label
, link
->ifname
))
1991 free_and_replace(address
->label
, label
);
1992 } else if (r
!= -ENODATA
)
1993 log_link_debug_errno(link
, r
, "rtnl: could not get label from address message, ignoring: %m");
1995 r
= sd_netlink_message_read_in_addr(message
, IFA_BROADCAST
, &address
->broadcast
);
1996 if (r
< 0 && r
!= -ENODATA
)
1997 log_link_debug_errno(link
, r
, "rtnl: could not get broadcast from address message, ignoring: %m");
2000 r
= sd_netlink_message_read_u32(message
, IFA_FLAGS
, &address
->flags
);
2002 log_link_debug_errno(link
, r
, "rtnl: failed to read IFA_FLAGS attribute, ignoring: %m");
2004 struct ifa_cacheinfo cinfo
;
2005 r
= sd_netlink_message_read_cache_info(message
, IFA_CACHEINFO
, &cinfo
);
2007 address_set_lifetime(m
, address
, &cinfo
);
2008 else if (r
!= -ENODATA
)
2009 log_link_debug_errno(link
, r
, "rtnl: failed to read IFA_CACHEINFO attribute, ignoring: %m");
2011 r
= sd_netlink_message_read_u32(message
, IFA_RT_PRIORITY
, &address
->route_metric
);
2012 if (r
< 0 && r
!= -ENODATA
)
2013 log_link_debug_errno(link
, r
, "rtnl: failed to read IFA_RT_PRIORITY attribute, ignoring: %m");
2015 address_enter_configured(address
);
2017 address_enter_configured(req
->userdata
);
2019 log_address_debug(address
, is_new
? "Received new": "Received updated", link
);
2021 /* address_update() logs internally, so we don't need to here. */
2022 r
= address_update(address
);
2024 link_enter_failed(link
);
2027 if (tmp
->family
== AF_INET6
) {
2028 r
= dhcp4_update_ipv6_connectivity(link
);
2030 log_link_warning_errno(link
, r
, "Failed to notify IPv6 connectivity to DHCPv4 client: %m");
2031 link_enter_failed(link
);
2038 static int config_parse_broadcast(
2040 const char *filename
,
2042 const char *section
,
2043 unsigned section_line
,
2050 Address
*address
= ASSERT_PTR(userdata
);
2053 /* Do not check or set address->family here. It will be checked later in
2054 * address_section_verify() -> address_section_adjust_broadcast() . */
2056 if (isempty(rvalue
)) {
2057 /* The broadcast address will be calculated based on Address=, and set if the link is
2058 * not a wireguard interface. */
2059 address
->broadcast
= (struct in_addr
) {};
2060 address
->set_broadcast
= -1;
2064 r
= parse_boolean(rvalue
);
2066 /* The broadcast address will be calculated based on Address=. */
2067 address
->broadcast
= (struct in_addr
) {};
2068 address
->set_broadcast
= r
;
2072 r
= config_parse_in_addr_non_null(
2073 unit
, filename
, line
, section
, section_line
,
2074 lvalue
, /* ltype = */ AF_INET
, rvalue
,
2075 &address
->broadcast
, /* userdata = */ NULL
);
2079 address
->set_broadcast
= true;
2083 static int config_parse_address(
2085 const char *filename
,
2087 const char *section
,
2088 unsigned section_line
,
2095 Address
*address
= ASSERT_PTR(userdata
);
2096 union in_addr_union
*a
= ASSERT_PTR(data
);
2097 struct in_addr_prefix prefix
;
2102 if (isempty(rvalue
)) {
2103 /* When an empty string is assigned, clear both Address= and Peer=. */
2104 address
->family
= AF_UNSPEC
;
2105 address
->prefixlen
= 0;
2106 address
->in_addr
= IN_ADDR_NULL
;
2107 address
->in_addr_peer
= IN_ADDR_NULL
;
2111 r
= config_parse_in_addr_prefix(unit
, filename
, line
, section
, section_line
, lvalue
, /* ltype = */ true, rvalue
, &prefix
, /* userdata = */ NULL
);
2115 if (address
->family
!= AF_UNSPEC
&& prefix
.family
!= address
->family
) {
2116 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "Address is incompatible, ignoring assignment: %s", rvalue
);
2120 address
->family
= prefix
.family
;
2121 address
->prefixlen
= prefix
.prefixlen
;
2122 *a
= prefix
.address
;
2126 static int config_parse_address_label(
2128 const char *filename
,
2130 const char *section
,
2131 unsigned section_line
,
2138 char **label
= ASSERT_PTR(data
);
2141 if (!isempty(rvalue
) && !address_label_valid(rvalue
)) {
2142 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
2143 "Interface label is too long or invalid, ignoring assignment: %s", rvalue
);
2147 r
= free_and_strdup_warn(label
, empty_to_null(rvalue
));
2154 static int config_parse_address_lifetime(
2156 const char *filename
,
2158 const char *section
,
2159 unsigned section_line
,
2166 usec_t
*usec
= ASSERT_PTR(data
);
2168 /* We accept only "forever", "infinity", empty, or "0". */
2169 if (isempty(rvalue
) || STR_IN_SET(rvalue
, "forever", "infinity"))
2170 *usec
= USEC_INFINITY
;
2171 else if (streq(rvalue
, "0"))
2174 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
2175 "Invalid PreferredLifetime= value, ignoring: %s", rvalue
);
2182 static int config_parse_address_scope(
2184 const char *filename
,
2186 const char *section
,
2187 unsigned section_line
,
2194 Address
*address
= ASSERT_PTR(userdata
);
2197 if (isempty(rvalue
)) {
2198 address
->scope
= RT_SCOPE_UNIVERSE
;
2199 address
->scope_set
= false;
2203 r
= route_scope_from_string(rvalue
);
2205 return log_syntax_parse_error(unit
, filename
, line
, r
, lvalue
, rvalue
);
2208 address
->scope_set
= true;
2212 static int config_parse_address_dad(
2214 const char *filename
,
2216 const char *section
,
2217 unsigned section_line
,
2224 AddressFamily
*p
= ASSERT_PTR(data
);
2227 if (isempty(rvalue
)) {
2228 *p
= _ADDRESS_FAMILY_INVALID
;
2232 r
= parse_boolean(rvalue
);
2234 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
2235 "For historical reasons, %s=%s means %s=%s. "
2236 "Please use 'both', 'ipv4', 'ipv6' or 'none' instead.",
2237 lvalue
, rvalue
, lvalue
, r
? "none" : "both");
2238 *p
= r
? ADDRESS_FAMILY_NO
: ADDRESS_FAMILY_YES
;
2242 AddressFamily a
= duplicate_address_detection_address_family_from_string(rvalue
);
2244 return log_syntax_parse_error(unit
, filename
, line
, a
, lvalue
, rvalue
);
2250 int config_parse_address_section(
2252 const char *filename
,
2254 const char *section
,
2255 unsigned section_line
,
2262 static const ConfigSectionParser table
[_ADDRESS_CONF_PARSER_MAX
] = {
2263 [ADDRESS_ADDRESS
] = { .parser
= config_parse_address
, .ltype
= 0, .offset
= offsetof(Address
, in_addr
), },
2264 [ADDRESS_PEER
] = { .parser
= config_parse_address
, .ltype
= 0, .offset
= offsetof(Address
, in_addr_peer
), },
2265 [ADDRESS_BROADCAST
] = { .parser
= config_parse_broadcast
, .ltype
= 0, .offset
= 0, },
2266 [ADDRESS_LABEL
] = { .parser
= config_parse_address_label
, .ltype
= 0, .offset
= offsetof(Address
, label
), },
2267 [ADDRESS_PREFERRED_LIFETIME
] = { .parser
= config_parse_address_lifetime
, .ltype
= 0, .offset
= offsetof(Address
, lifetime_preferred_usec
), },
2268 [ADDRESS_HOME_ADDRESS
] = { .parser
= config_parse_uint32_flag
, .ltype
= IFA_F_HOMEADDRESS
, .offset
= offsetof(Address
, flags
), },
2269 [ADDRESS_MANAGE_TEMPORARY_ADDRESS
] = { .parser
= config_parse_uint32_flag
, .ltype
= IFA_F_MANAGETEMPADDR
, .offset
= offsetof(Address
, flags
), },
2270 [ADDRESS_PREFIX_ROUTE
] = { .parser
= config_parse_uint32_flag
, .ltype
= IFA_F_NOPREFIXROUTE
, .offset
= offsetof(Address
, flags
), },
2271 [ADDRESS_ADD_PREFIX_ROUTE
] = { .parser
= config_parse_uint32_invert_flag
, .ltype
= IFA_F_NOPREFIXROUTE
, .offset
= offsetof(Address
, flags
), },
2272 [ADDRESS_AUTO_JOIN
] = { .parser
= config_parse_uint32_flag
, .ltype
= IFA_F_MCAUTOJOIN
, .offset
= offsetof(Address
, flags
), },
2273 [ADDRESS_DAD
] = { .parser
= config_parse_address_dad
, .ltype
= 0, .offset
= offsetof(Address
, duplicate_address_detection
), },
2274 [ADDRESS_SCOPE
] = { .parser
= config_parse_address_scope
, .ltype
= 0, .offset
= 0, },
2275 [ADDRESS_ROUTE_METRIC
] = { .parser
= config_parse_uint32
, .ltype
= 0, .offset
= offsetof(Address
, route_metric
), },
2276 [ADDRESS_NET_LABEL
] = { .parser
= config_parse_string
, .ltype
= CONFIG_PARSE_STRING_SAFE
, .offset
= offsetof(Address
, netlabel
), },
2277 [ADDRESS_NFT_SET
] = { .parser
= config_parse_nft_set
, .ltype
= NFT_SET_PARSE_NETWORK
, .offset
= offsetof(Address
, nft_set_context
), },
2280 _cleanup_(address_unref_or_set_invalidp
) Address
*address
= NULL
;
2281 Network
*network
= ASSERT_PTR(userdata
);
2287 if (streq(section
, "Network")) {
2288 assert(streq(lvalue
, "Address"));
2290 if (isempty(rvalue
)) {
2291 /* If an empty string specified in [Network] section, clear previously assigned addresses. */
2292 network
->addresses_by_section
= ordered_hashmap_free(network
->addresses_by_section
);
2296 /* we are not in an Address section, so use line number instead. */
2297 r
= address_new_static(network
, filename
, line
, &address
);
2299 r
= address_new_static(network
, filename
, section_line
, &address
);
2303 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
2304 "Failed to allocate new address, ignoring assignment: %m");
2308 r
= config_section_parse(table
, ELEMENTSOF(table
),
2309 unit
, filename
, line
, section
, section_line
, lvalue
, ltype
, rvalue
, address
);
2317 #define log_broadcast(address, fmt, ...) \
2319 const Address *_address = (address); \
2320 log_section_warning( \
2321 _address ? _address->section : NULL, \
2322 fmt " Ignoring Broadcast= setting in the [Address] section.", \
2326 static void address_section_adjust_broadcast(Address
*address
) {
2328 assert(address
->section
);
2330 if (!in4_addr_is_set(&address
->broadcast
))
2333 if (address
->family
== AF_INET6
)
2334 log_broadcast(address
, "Broadcast address is set for an IPv6 address.");
2335 else if (address
->prefixlen
> 30)
2336 log_broadcast(address
, "Broadcast address is set for an IPv4 address with prefix length larger than 30.");
2337 else if (in4_addr_is_set(&address
->in_addr_peer
.in
))
2338 log_broadcast(address
, "Broadcast address is set for an IPv4 address with peer address.");
2339 else if (!in4_addr_is_set(&address
->in_addr
.in
))
2340 log_broadcast(address
, "Broadcast address is set for an IPv4 address with null address.");
2342 /* Otherwise, keep the specified broadcast address. */
2345 address
->broadcast
.s_addr
= 0;
2348 #define log_address_section(address, fmt, ...) \
2350 const Address *_address = (address); \
2351 log_section_warning_errno( \
2352 _address ? _address->section : NULL, \
2353 SYNTHETIC_ERRNO(EINVAL), \
2354 fmt " Ignoring [Address] section.", \
2358 int address_section_verify(Address
*address
) {
2360 assert(address
->section
);
2362 if (section_is_invalid(address
->section
))
2365 if (address
->family
== AF_UNSPEC
)
2366 return log_address_section(address
, "Address section without Address= field was configured.");
2367 assert(IN_SET(address
->family
, AF_INET
, AF_INET6
));
2369 if (address
->family
== AF_INET6
&& !socket_ipv6_is_supported())
2370 return log_address_section(address
, "An IPv6 address was configured, but the kernel does not support IPv6.");
2372 if (in_addr_is_null(address
->family
, &address
->in_addr
)) {
2373 /* Will use address from address pool. Note that for ipv6 case, prefix of the address
2374 * pool is 8, but 40 bit is used by the global ID and 16 bit by the subnet ID. So,
2375 * let's limit the prefix length to 64 or larger. See RFC4193. */
2376 unsigned min_prefixlen
= address
->family
== AF_INET
? 8 : 64;
2377 if (address
->prefixlen
< min_prefixlen
)
2378 return log_address_section(address
, "Prefix length for %s null address must be equal or larger than %u.",
2379 af_to_ipv4_ipv6(address
->family
), min_prefixlen
);
2381 address
->requested_as_null
= !in_addr_is_set(address
->family
, &address
->in_addr
);
2384 address_section_adjust_broadcast(address
);
2386 if (address
->family
== AF_INET6
&& address
->label
) {
2387 log_section_warning(address
->section
, "Address label is set for IPv6 address, ignoring Label= setting.");
2388 address
->label
= mfree(address
->label
);
2391 if (!address
->scope_set
) {
2392 if (in_addr_is_localhost(address
->family
, &address
->in_addr
) > 0)
2393 address
->scope
= RT_SCOPE_HOST
;
2394 else if (in_addr_is_link_local(address
->family
, &address
->in_addr
) > 0)
2395 address
->scope
= RT_SCOPE_LINK
;
2398 if (address
->duplicate_address_detection
< 0) {
2399 if (address
->family
== AF_INET6
)
2400 address
->duplicate_address_detection
= ADDRESS_FAMILY_IPV6
;
2401 else if (in4_addr_is_link_local(&address
->in_addr
.in
))
2402 address
->duplicate_address_detection
= ADDRESS_FAMILY_IPV4
;
2404 address
->duplicate_address_detection
= ADDRESS_FAMILY_NO
;
2405 } else if (address
->duplicate_address_detection
== ADDRESS_FAMILY_IPV6
&& address
->family
== AF_INET
)
2406 log_section_warning(address
->section
, "DuplicateAddressDetection=ipv6 is specified for IPv4 address, ignoring.");
2407 else if (address
->duplicate_address_detection
== ADDRESS_FAMILY_IPV4
&& address
->family
== AF_INET6
)
2408 log_section_warning(address
->section
, "DuplicateAddressDetection=ipv4 is specified for IPv6 address, ignoring.");
2410 if (address
->family
== AF_INET6
&&
2411 !FLAGS_SET(address
->duplicate_address_detection
, ADDRESS_FAMILY_IPV6
))
2412 address
->flags
|= IFA_F_NODAD
;
2414 uint32_t filtered_flags
= address
->family
== AF_INET
?
2415 address
->flags
& KNOWN_FLAGS
& ~UNMANAGED_FLAGS
& ~IPV6ONLY_FLAGS
:
2416 address
->flags
& KNOWN_FLAGS
& ~UNMANAGED_FLAGS
;
2417 if (address
->flags
!= filtered_flags
) {
2418 _cleanup_free_
char *str
= NULL
;
2420 (void) address_flags_to_string_alloc(address
->flags
^ filtered_flags
, address
->family
, &str
);
2421 return log_address_section(address
, "unexpected address flags \"%s\" were configured.", strna(str
));
2427 DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
2428 trivial_hash_ops_address_detach
,
2431 trivial_compare_func
,
2435 int network_drop_invalid_addresses(Network
*network
) {
2436 _cleanup_set_free_ Set
*addresses
= NULL
, *duplicated_addresses
= NULL
;
2442 ORDERED_HASHMAP_FOREACH(address
, network
->addresses_by_section
) {
2443 if (address_section_verify(address
) < 0) {
2444 /* Drop invalid [Address] sections or Address= settings in [Network].
2445 * Note that address_detach() will drop the address from addresses_by_section. */
2446 address_detach(address
);
2450 /* Always use the setting specified later. So, remove the previously assigned setting. */
2451 Address
*dup
= set_remove(addresses
, address
);
2453 log_warning("%s: Duplicated address %s is specified at line %u and %u, "
2454 "dropping the address setting specified at line %u.",
2455 dup
->section
->filename
,
2456 IN_ADDR_PREFIX_TO_STRING(address
->family
, &address
->in_addr
, address
->prefixlen
),
2457 address
->section
->line
,
2458 dup
->section
->line
, dup
->section
->line
);
2460 /* Do not call address_detach() for 'dup' now, as we can remove only the current
2461 * entry in the loop. We will drop the address from addresses_by_section later. */
2462 r
= set_ensure_put(&duplicated_addresses
, &trivial_hash_ops_address_detach
, dup
);
2468 /* Use address_hash_ops, instead of address_hash_ops_detach. Otherwise, the Address objects
2469 * will be detached. */
2470 r
= set_ensure_put(&addresses
, &address_hash_ops
, address
);
2476 r
= network_adjust_dhcp_server(network
, &addresses
);