1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
4 #include <net/if_arp.h>
6 #include "alloc-util.h"
7 #include "firewall-util.h"
8 #include "memory-util.h"
9 #include "netlink-util.h"
10 #include "networkd-address-pool.h"
11 #include "networkd-address.h"
12 #include "networkd-ipv4acd.h"
13 #include "networkd-manager.h"
14 #include "networkd-network.h"
15 #include "networkd-queue.h"
16 #include "parse-util.h"
17 #include "string-util.h"
21 #define ADDRESSES_PER_LINK_MAX 2048U
22 #define STATIC_ADDRESSES_PER_NETWORK_MAX 1024U
24 static int address_flags_to_string_alloc(uint32_t flags
, int family
, char **ret
) {
25 _cleanup_free_
char *str
= NULL
;
30 { IFA_F_SECONDARY
, "secondary" }, /* This is also called "temporary" for ipv6. */
31 { IFA_F_NODAD
, "nodad" },
32 { IFA_F_OPTIMISTIC
, "optimistic" },
33 { IFA_F_DADFAILED
, "dadfailed" },
34 { IFA_F_HOMEADDRESS
, "home-address" },
35 { IFA_F_DEPRECATED
, "deprecated" },
36 { IFA_F_TENTATIVE
, "tentative" },
37 { IFA_F_PERMANENT
, "permanent" },
38 { IFA_F_MANAGETEMPADDR
, "manage-temporary-address" },
39 { IFA_F_NOPREFIXROUTE
, "no-prefixroute" },
40 { IFA_F_MCAUTOJOIN
, "auto-join" },
41 { IFA_F_STABLE_PRIVACY
, "stable-privacy" },
44 assert(IN_SET(family
, AF_INET
, AF_INET6
));
47 for (size_t i
= 0; i
< ELEMENTSOF(map
); i
++)
48 if (flags
& map
[i
].flag
&&
49 !strextend_with_separator(
51 map
[i
].flag
== IFA_F_SECONDARY
&& family
== AF_INET6
? "temporary" : map
[i
].name
))
58 int generate_ipv6_eui_64_address(const Link
*link
, struct in6_addr
*ret
) {
62 if (link
->iftype
== ARPHRD_INFINIBAND
) {
63 /* see RFC4391 section 8 */
64 memcpy(&ret
->s6_addr
[8], &link
->hw_addr
.infiniband
[12], 8);
65 ret
->s6_addr
[8] ^= 1 << 1;
70 /* see RFC4291 section 2.5.1 */
71 ret
->s6_addr
[8] = link
->hw_addr
.ether
.ether_addr_octet
[0];
72 ret
->s6_addr
[8] ^= 1 << 1;
73 ret
->s6_addr
[9] = link
->hw_addr
.ether
.ether_addr_octet
[1];
74 ret
->s6_addr
[10] = link
->hw_addr
.ether
.ether_addr_octet
[2];
75 ret
->s6_addr
[11] = 0xff;
76 ret
->s6_addr
[12] = 0xfe;
77 ret
->s6_addr
[13] = link
->hw_addr
.ether
.ether_addr_octet
[3];
78 ret
->s6_addr
[14] = link
->hw_addr
.ether
.ether_addr_octet
[4];
79 ret
->s6_addr
[15] = link
->hw_addr
.ether
.ether_addr_octet
[5];
84 int address_new(Address
**ret
) {
85 _cleanup_(address_freep
) Address
*address
= NULL
;
87 address
= new(Address
, 1);
91 *address
= (Address
) {
93 .scope
= RT_SCOPE_UNIVERSE
,
94 .cinfo
.ifa_prefered
= CACHE_INFO_INFINITY_LIFE_TIME
,
95 .cinfo
.ifa_valid
= CACHE_INFO_INFINITY_LIFE_TIME
,
97 .duplicate_address_detection
= ADDRESS_FAMILY_IPV6
,
100 *ret
= TAKE_PTR(address
);
105 static int address_new_static(Network
*network
, const char *filename
, unsigned section_line
, Address
**ret
) {
106 _cleanup_(network_config_section_freep
) NetworkConfigSection
*n
= NULL
;
107 _cleanup_(address_freep
) Address
*address
= NULL
;
113 assert(section_line
> 0);
115 r
= network_config_section_new(filename
, section_line
, &n
);
119 address
= ordered_hashmap_get(network
->addresses_by_section
, n
);
121 *ret
= TAKE_PTR(address
);
125 if (ordered_hashmap_size(network
->addresses_by_section
) >= STATIC_ADDRESSES_PER_NETWORK_MAX
)
128 r
= address_new(&address
);
132 address
->network
= network
;
133 address
->section
= TAKE_PTR(n
);
134 address
->is_static
= true;
136 r
= ordered_hashmap_ensure_put(&network
->addresses_by_section
, &network_config_hash_ops
, address
->section
, address
);
140 *ret
= TAKE_PTR(address
);
144 Address
*address_free(Address
*address
) {
148 if (address
->network
) {
149 assert(address
->section
);
150 ordered_hashmap_remove(address
->network
->addresses_by_section
, address
->section
);
156 set_remove(address
->link
->addresses
, address
);
157 set_remove(address
->link
->addresses_foreign
, address
);
158 set_remove(address
->link
->addresses_ipv4acd
, address
);
159 set_remove(address
->link
->static_addresses
, address
);
160 if (address
->link
->dhcp_address
== address
)
161 address
->link
->dhcp_address
= NULL
;
162 if (address
->link
->dhcp_address_old
== address
)
163 address
->link
->dhcp_address_old
= NULL
;
164 set_remove(address
->link
->dhcp6_addresses
, address
);
165 set_remove(address
->link
->dhcp6_addresses_old
, address
);
166 set_remove(address
->link
->dhcp6_pd_addresses
, address
);
167 set_remove(address
->link
->dhcp6_pd_addresses_old
, address
);
168 SET_FOREACH(n
, address
->link
->ndisc_addresses
)
169 if (n
->address
== address
)
170 free(set_remove(address
->link
->ndisc_addresses
, n
));
172 if (address
->family
== AF_INET6
&&
173 in6_addr_equal(&address
->in_addr
.in6
, &address
->link
->ipv6ll_address
))
174 memzero(&address
->link
->ipv6ll_address
, sizeof(struct in6_addr
));
177 sd_ipv4acd_unref(address
->acd
);
179 network_config_section_free(address
->section
);
180 free(address
->label
);
181 return mfree(address
);
184 static bool address_may_have_broadcast(const Address
*a
) {
187 if (a
->family
!= AF_INET
)
190 if (in4_addr_is_set(&a
->in_addr_peer
.in
))
193 /* A /31 or /32 IPv4 address does not have a broadcast address.
194 * See https://tools.ietf.org/html/rfc3021 */
195 if (a
->prefixlen
> 30)
198 if (a
->set_broadcast
>= 0)
199 return a
->set_broadcast
;
201 return true; /* Defaults to true. */
204 void address_set_broadcast(Address
*a
) {
207 if (!address_may_have_broadcast(a
))
210 /* If explicitly configured, do not update the address. */
211 if (in4_addr_is_set(&a
->broadcast
))
214 /* If Address= is 0.0.0.0, then the broadcast address will be set later in address_acquire(). */
215 if (in4_addr_is_null(&a
->in_addr
.in
))
218 a
->broadcast
.s_addr
= a
->in_addr
.in
.s_addr
| htobe32(UINT32_C(0xffffffff) >> a
->prefixlen
);
221 static bool address_may_set_broadcast(const Address
*a
, const Link
*link
) {
225 if (!address_may_have_broadcast(a
))
228 /* Typical configuration for wireguard does not set broadcast. */
229 return !streq_ptr(link
->kind
, "wireguard");
232 static uint32_t address_prefix(const Address
*a
) {
235 /* make sure we don't try to shift by 32.
236 * See ISO/IEC 9899:TC3 § 6.5.7.3. */
237 if (a
->prefixlen
== 0)
240 if (a
->in_addr_peer
.in
.s_addr
!= 0)
241 return be32toh(a
->in_addr_peer
.in
.s_addr
) >> (32 - a
->prefixlen
);
243 return be32toh(a
->in_addr
.in
.s_addr
) >> (32 - a
->prefixlen
);
246 void address_hash_func(const Address
*a
, struct siphash
*state
) {
249 siphash24_compress(&a
->family
, sizeof(a
->family
), state
);
253 siphash24_compress(&a
->prefixlen
, sizeof(a
->prefixlen
), state
);
255 uint32_t prefix
= address_prefix(a
);
256 siphash24_compress(&prefix
, sizeof(prefix
), state
);
260 siphash24_compress(&a
->in_addr
, FAMILY_ADDRESS_SIZE(a
->family
), state
);
263 /* treat any other address family as AF_UNSPEC */
268 int address_compare_func(const Address
*a1
, const Address
*a2
) {
271 r
= CMP(a1
->family
, a2
->family
);
275 switch (a1
->family
) {
277 /* See kernel's find_matching_ifa() in net/ipv4/devinet.c */
278 r
= CMP(a1
->prefixlen
, a2
->prefixlen
);
282 r
= CMP(address_prefix(a1
), address_prefix(a2
));
288 /* See kernel's ipv6_get_ifaddr() in net/ipv6/addrconf.c */
289 return memcmp(&a1
->in_addr
, &a2
->in_addr
, FAMILY_ADDRESS_SIZE(a1
->family
));
291 /* treat any other address family as AF_UNSPEC */
296 DEFINE_HASH_OPS_WITH_KEY_DESTRUCTOR(address_hash_ops
, Address
, address_hash_func
, address_compare_func
, address_free
);
298 bool address_equal(const Address
*a1
, const Address
*a2
) {
305 return address_compare_func(a1
, a2
) == 0;
308 int address_dup(const Address
*src
, Address
**ret
) {
309 _cleanup_(address_freep
) Address
*dest
= NULL
;
315 dest
= newdup(Address
, src
, 1);
319 /* clear all pointers */
320 dest
->network
= NULL
;
321 dest
->section
= NULL
;
326 if (src
->family
== AF_INET
) {
327 r
= free_and_strdup(&dest
->label
, src
->label
);
332 *ret
= TAKE_PTR(dest
);
336 static int address_set_masquerade(Address
*address
, bool add
) {
337 union in_addr_union masked
;
341 assert(address
->link
);
343 if (!address
->link
->network
)
346 if (address
->family
== AF_INET
&&
347 !FLAGS_SET(address
->link
->network
->ip_masquerade
, ADDRESS_FAMILY_IPV4
))
350 if (address
->family
== AF_INET6
&&
351 !FLAGS_SET(address
->link
->network
->ip_masquerade
, ADDRESS_FAMILY_IPV6
))
354 if (address
->scope
>= RT_SCOPE_LINK
)
357 if (address
->ip_masquerade_done
== add
)
360 masked
= address
->in_addr
;
361 r
= in_addr_mask(address
->family
, &masked
, address
->prefixlen
);
365 r
= fw_add_masquerade(&address
->link
->manager
->fw_ctx
, add
, address
->family
, &masked
, address
->prefixlen
);
369 address
->ip_masquerade_done
= add
;
374 static int address_add_internal(Link
*link
, Set
**addresses
, const Address
*in
, Address
**ret
) {
375 _cleanup_(address_freep
) Address
*address
= NULL
;
382 r
= address_dup(in
, &address
);
386 /* Consider address tentative until we get the real flags from the kernel */
387 address
->flags
|= IFA_F_TENTATIVE
;
389 r
= set_ensure_put(addresses
, &address_hash_ops
, address
);
395 address
->link
= link
;
403 static int address_add_foreign(Link
*link
, const Address
*in
, Address
**ret
) {
404 return address_add_internal(link
, &link
->addresses_foreign
, in
, ret
);
407 static int address_add(Link
*link
, const Address
*in
, Address
**ret
) {
414 r
= address_get(link
, in
, &address
);
416 /* Address does not exist, create a new one */
417 r
= address_add_internal(link
, &link
->addresses
, in
, &address
);
421 /* Take over a foreign address */
422 r
= set_ensure_put(&link
->addresses
, &address_hash_ops
, address
);
426 set_remove(link
->addresses_foreign
, address
);
428 /* Already exists, do nothing */
438 static int address_update(Address
*address
, const Address
*src
) {
443 assert(address
->link
);
446 ready
= address_is_ready(address
);
448 address
->flags
= src
->flags
;
449 address
->scope
= src
->scope
;
450 address
->cinfo
= src
->cinfo
;
452 if (IN_SET(address
->link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
455 link_update_operstate(address
->link
, true);
456 link_check_ready(address
->link
);
458 if (!ready
&& address_is_ready(address
)) {
459 if (address
->callback
) {
460 r
= address
->callback(address
);
465 if (address
->family
== AF_INET6
&&
466 in6_addr_is_link_local(&address
->in_addr
.in6
) > 0 &&
467 in6_addr_is_null(&address
->link
->ipv6ll_address
)) {
469 r
= link_ipv6ll_gained(address
->link
, &address
->in_addr
.in6
);
478 static int address_drop(Address
*address
) {
486 ready
= address_is_ready(address
);
487 link
= address
->link
;
489 r
= address_set_masquerade(address
, false);
491 log_link_warning_errno(link
, r
, "Failed to disable IP masquerading, ignoring: %m");
493 address_free(address
);
495 link_update_operstate(link
, true);
498 link_check_ready(link
);
503 int address_get(Link
*link
, const Address
*in
, Address
**ret
) {
509 existing
= set_get(link
->addresses
, in
);
516 existing
= set_get(link
->addresses_foreign
, in
);
526 int link_get_ipv6_address(Link
*link
, const struct in6_addr
*address
, Address
**ret
) {
527 _cleanup_(address_freep
) Address
*a
= NULL
;
537 /* address_compare_func() only compares the local address for IPv6 case. So, it is enough to
538 * set only family and the address. */
539 a
->family
= AF_INET6
;
540 a
->in_addr
.in6
= *address
;
542 return address_get(link
, a
, ret
);
545 static int addresses_get_ipv4_address(Set
*addresses
, const struct in_addr
*address
, Address
**ret
) {
550 SET_FOREACH(a
, addresses
) {
551 if (a
->family
!= AF_INET
)
554 if (!in4_addr_equal(&a
->in_addr
.in
, address
))
566 int link_get_ipv4_address(Link
*link
, const struct in_addr
*address
, unsigned char prefixlen
, Address
**ret
) {
572 if (prefixlen
!= 0) {
573 _cleanup_(address_freep
) Address
*a
= NULL
;
575 /* If prefixlen is set, then we can use address_get(). */
582 a
->in_addr
.in
= *address
;
583 a
->prefixlen
= prefixlen
;
585 return address_get(link
, a
, ret
);
588 if (addresses_get_ipv4_address(link
->addresses
, address
, ret
) >= 0)
590 return addresses_get_ipv4_address(link
->addresses_foreign
, address
, ret
);
593 int manager_has_address(Manager
*manager
, int family
, const union in_addr_union
*address
, bool check_ready
) {
599 assert(IN_SET(family
, AF_INET
, AF_INET6
));
602 if (family
== AF_INET
) {
603 HASHMAP_FOREACH(link
, manager
->links_by_index
)
604 if (link_get_ipv4_address(link
, &address
->in
, 0, &a
) >= 0)
605 return !check_ready
|| address_is_ready(a
);
607 _cleanup_(address_freep
) Address
*tmp
= NULL
;
609 r
= address_new(&tmp
);
613 tmp
->family
= family
;
614 tmp
->in_addr
= *address
;
616 HASHMAP_FOREACH(link
, manager
->links_by_index
)
617 if (address_get(link
, tmp
, &a
) >= 0)
618 return !check_ready
|| address_is_ready(a
);
624 const char* format_lifetime(char *buf
, size_t l
, uint32_t lifetime
) {
628 if (lifetime
== CACHE_INFO_INFINITY_LIFE_TIME
)
631 sprintf(buf
, "for ");
632 /* format_timespan() never fails */
633 assert_se(format_timespan(buf
+ 4, l
- 4, lifetime
* USEC_PER_SEC
, USEC_PER_SEC
));
637 static void log_address_debug(const Address
*address
, const char *str
, const Link
*link
) {
638 _cleanup_free_
char *addr
= NULL
, *peer
= NULL
, *flags_str
= NULL
;
647 (void) in_addr_to_string(address
->family
, &address
->in_addr
, &addr
);
648 if (in_addr_is_set(address
->family
, &address
->in_addr_peer
))
649 (void) in_addr_to_string(address
->family
, &address
->in_addr_peer
, &peer
);
651 (void) address_flags_to_string_alloc(address
->flags
, address
->family
, &flags_str
);
653 log_link_debug(link
, "%s address: %s%s%s/%u (valid %s, preferred %s), flags: %s",
654 str
, strnull(addr
), peer
? " peer " : "", strempty(peer
), address
->prefixlen
,
655 FORMAT_LIFETIME(address
->cinfo
.ifa_valid
),
656 FORMAT_LIFETIME(address
->cinfo
.ifa_prefered
),
660 static int address_set_netlink_message(const Address
*address
, sd_netlink_message
*req
, Link
*link
) {
668 r
= sd_rtnl_message_addr_set_prefixlen(req
, address
->prefixlen
);
670 return log_link_error_errno(link
, r
, "Could not set prefixlen: %m");
672 /* On remove, only IFA_F_MANAGETEMPADDR flag for IPv6 addresses are used. But anyway, set all
673 * flags except tentative flag here unconditionally. Without setting the flag, the template
674 * addresses generated by kernel will not be removed automatically when the main address is
676 flags
= address
->flags
& ~IFA_F_TENTATIVE
;
677 r
= sd_rtnl_message_addr_set_flags(req
, flags
& 0xff);
679 return log_link_error_errno(link
, r
, "Could not set flags: %m");
681 if ((flags
& ~0xff) != 0) {
682 r
= sd_netlink_message_append_u32(req
, IFA_FLAGS
, flags
);
684 return log_link_error_errno(link
, r
, "Could not set extended flags: %m");
687 r
= netlink_message_append_in_addr_union(req
, IFA_LOCAL
, address
->family
, &address
->in_addr
);
689 return log_link_error_errno(link
, r
, "Could not append IFA_LOCAL attribute: %m");
694 static int address_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
700 assert(link
->address_remove_messages
> 0);
702 link
->address_remove_messages
--;
704 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
707 r
= sd_netlink_message_get_errno(m
);
708 if (r
< 0 && r
!= -EADDRNOTAVAIL
)
709 log_link_message_warning_errno(link
, m
, r
, "Could not drop address");
714 int address_remove(const Address
*address
, Link
*link
) {
715 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
719 assert(IN_SET(address
->family
, AF_INET
, AF_INET6
));
721 assert(link
->ifindex
> 0);
722 assert(link
->manager
);
723 assert(link
->manager
->rtnl
);
725 log_address_debug(address
, "Removing", link
);
727 r
= sd_rtnl_message_new_addr(link
->manager
->rtnl
, &req
, RTM_DELADDR
,
728 link
->ifindex
, address
->family
);
730 return log_link_error_errno(link
, r
, "Could not allocate RTM_DELADDR message: %m");
732 r
= address_set_netlink_message(address
, req
, link
);
736 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
,
737 address_remove_handler
,
738 link_netlink_destroy_callback
, link
);
740 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
743 link
->address_remove_messages
++;
748 static bool link_is_static_address_configured(const Link
*link
, const Address
*address
) {
749 Address
*net_address
;
757 ORDERED_HASHMAP_FOREACH(net_address
, link
->network
->addresses_by_section
)
758 if (address_equal(net_address
, address
))
764 bool link_address_is_dynamic(const Link
*link
, const Address
*address
) {
770 if (address
->cinfo
.ifa_prefered
!= CACHE_INFO_INFINITY_LIFE_TIME
)
773 /* Even when the address is leased from a DHCP server, networkd assign the address
774 * without lifetime when KeepConfiguration=dhcp. So, let's check that we have
775 * corresponding routes with RTPROT_DHCP. */
776 SET_FOREACH(route
, link
->routes_foreign
) {
777 if (route
->protocol
!= RTPROT_DHCP
)
780 if (address
->family
!= route
->family
)
783 if (in_addr_equal(address
->family
, &address
->in_addr
, &route
->prefsrc
))
790 int link_drop_ipv6ll_addresses(Link
*link
) {
791 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
795 assert(link
->manager
);
796 assert(link
->manager
->rtnl
);
798 /* IPv6LL address may be in the tentative state, and in that case networkd has not received it.
799 * So, we need to dump all IPv6 addresses. */
801 if (link_ipv6ll_enabled(link
))
804 r
= sd_rtnl_message_new_addr(link
->manager
->rtnl
, &req
, RTM_GETADDR
, link
->ifindex
, AF_INET6
);
808 r
= sd_netlink_message_request_dump(req
, true);
812 r
= sd_netlink_call(link
->manager
->rtnl
, req
, 0, &reply
);
816 for (sd_netlink_message
*addr
= reply
; addr
; addr
= sd_netlink_message_next(addr
)) {
817 _cleanup_(address_freep
) Address
*a
= NULL
;
818 unsigned char flags
, prefixlen
;
819 struct in6_addr address
;
822 /* NETLINK_GET_STRICT_CHK socket option is supported since kernel 4.20. To support
823 * older kernels, we need to check ifindex here. */
824 r
= sd_rtnl_message_addr_get_ifindex(addr
, &ifindex
);
826 log_link_debug_errno(link
, r
, "rtnl: received address message without valid ifindex, ignoring: %m");
828 } else if (link
->ifindex
!= ifindex
)
831 r
= sd_rtnl_message_addr_get_flags(addr
, &flags
);
833 log_link_debug_errno(link
, r
, "rtnl: received address message without valid flags, ignoring: %m");
837 r
= sd_rtnl_message_addr_get_prefixlen(addr
, &prefixlen
);
839 log_link_debug_errno(link
, r
, "rtnl: received address message without prefixlen, ignoring: %m");
843 if (sd_netlink_message_read_in6_addr(addr
, IFA_LOCAL
, NULL
) >= 0)
844 /* address with peer, ignoring. */
847 r
= sd_netlink_message_read_in6_addr(addr
, IFA_ADDRESS
, &address
);
849 log_link_debug_errno(link
, r
, "rtnl: received address message without valid address, ignoring: %m");
853 if (!in6_addr_is_link_local(&address
))
860 a
->family
= AF_INET6
;
861 a
->in_addr
.in6
= address
;
862 a
->prefixlen
= prefixlen
;
865 r
= address_remove(a
, link
);
873 int link_drop_foreign_addresses(Link
*link
) {
879 SET_FOREACH(address
, link
->addresses_foreign
) {
880 /* We consider IPv6LL addresses to be managed by the kernel, or dropped in link_drop_ipv6ll_addresses() */
881 if (address
->family
== AF_INET6
&& in6_addr_is_link_local(&address
->in_addr
.in6
) == 1)
884 if (link_address_is_dynamic(link
, address
)) {
885 if (link
->network
&& FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
))
887 } else if (link
->network
&& FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_STATIC
))
890 if (link_is_static_address_configured(link
, address
)) {
891 k
= address_add(link
, address
, NULL
);
893 log_link_error_errno(link
, k
, "Failed to add address: %m");
898 k
= address_remove(address
, link
);
907 int link_drop_addresses(Link
*link
) {
908 Address
*address
, *pool_address
;
913 SET_FOREACH(address
, link
->addresses
) {
914 /* we consider IPv6LL addresses to be managed by the kernel */
915 if (address
->family
== AF_INET6
&& in6_addr_is_link_local(&address
->in_addr
.in6
) == 1 && link_ipv6ll_enabled(link
))
918 k
= address_remove(address
, link
);
919 if (k
< 0 && r
>= 0) {
924 SET_FOREACH(pool_address
, link
->pool_addresses
)
925 if (address_equal(address
, pool_address
))
926 address_free(set_remove(link
->pool_addresses
, pool_address
));
932 static int address_acquire(Link
*link
, const Address
*original
, Address
**ret
) {
933 union in_addr_union in_addr
= IN_ADDR_NULL
;
934 _cleanup_(address_freep
) Address
*na
= NULL
;
941 /* Something useful was configured? just use it */
942 if (in_addr_is_set(original
->family
, &original
->in_addr
)) {
947 /* The address is configured to be 0.0.0.0 or [::] by the user?
948 * Then let's acquire something more useful from the pool. */
949 r
= address_pool_acquire(link
->manager
, original
->family
, original
->prefixlen
, &in_addr
);
955 /* Pick first address in range for ourselves. */
956 if (original
->family
== AF_INET
)
957 in_addr
.in
.s_addr
= in_addr
.in
.s_addr
| htobe32(1);
958 else if (original
->family
== AF_INET6
)
959 in_addr
.in6
.s6_addr
[15] |= 1;
961 r
= address_dup(original
, &na
);
965 na
->in_addr
= in_addr
;
966 address_set_broadcast(na
);
968 r
= set_ensure_put(&link
->pool_addresses
, &address_hash_ops
, na
);
978 int address_configure_handler_internal(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
, const char *error_msg
) {
986 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
989 r
= sd_netlink_message_get_errno(m
);
990 if (r
< 0 && r
!= -EEXIST
) {
991 log_link_message_warning_errno(link
, m
, r
, error_msg
);
992 link_enter_failed(link
);
999 static int address_configure(
1000 const Address
*address
,
1002 link_netlink_message_handler_t callback
) {
1004 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1008 assert(IN_SET(address
->family
, AF_INET
, AF_INET6
));
1010 assert(link
->ifindex
> 0);
1011 assert(link
->manager
);
1012 assert(link
->manager
->rtnl
);
1015 log_address_debug(address
, "Configuring", link
);
1017 r
= sd_rtnl_message_new_addr_update(link
->manager
->rtnl
, &req
,
1018 link
->ifindex
, address
->family
);
1020 return log_link_error_errno(link
, r
, "Could not allocate RTM_NEWADDR message: %m");
1022 r
= address_set_netlink_message(address
, req
, link
);
1026 r
= sd_rtnl_message_addr_set_scope(req
, address
->scope
);
1028 return log_link_error_errno(link
, r
, "Could not set scope: %m");
1030 if (in_addr_is_set(address
->family
, &address
->in_addr_peer
)) {
1031 r
= netlink_message_append_in_addr_union(req
, IFA_ADDRESS
, address
->family
, &address
->in_addr_peer
);
1033 return log_link_error_errno(link
, r
, "Could not append IFA_ADDRESS attribute: %m");
1034 } else if (address_may_set_broadcast(address
, link
)) {
1035 r
= sd_netlink_message_append_in_addr(req
, IFA_BROADCAST
, &address
->broadcast
);
1037 return log_link_error_errno(link
, r
, "Could not append IFA_BROADCAST attribute: %m");
1040 if (address
->family
== AF_INET
&& address
->label
) {
1041 r
= sd_netlink_message_append_string(req
, IFA_LABEL
, address
->label
);
1043 return log_link_error_errno(link
, r
, "Could not append IFA_LABEL attribute: %m");
1046 r
= sd_netlink_message_append_cache_info(req
, IFA_CACHEINFO
, &address
->cinfo
);
1048 return log_link_error_errno(link
, r
, "Could not append IFA_CACHEINFO attribute: %m");
1050 r
= sd_netlink_message_append_u32(req
, IFA_RT_PRIORITY
, address
->route_metric
);
1052 return log_link_error_errno(link
, r
, "Could not append IFA_RT_PRIORITY attribute: %m");
1054 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, callback
, link_netlink_destroy_callback
, link
);
1056 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1062 static int static_address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1066 assert(link
->static_address_messages
> 0);
1068 link
->static_address_messages
--;
1070 r
= address_configure_handler_internal(rtnl
, m
, link
, "Failed to set static address");
1074 if (link
->static_address_messages
== 0) {
1075 log_link_debug(link
, "Addresses set");
1076 link
->static_addresses_configured
= true;
1077 link_check_ready(link
);
1083 static int static_address_after_configure(Request
*req
, void *object
) {
1084 Address
*address
= object
;
1090 assert(req
->type
== REQUEST_TYPE_ADDRESS
);
1095 r
= set_ensure_put(&link
->static_addresses
, &address_hash_ops
, address
);
1097 return log_link_warning_errno(link
, r
, "Failed to store static address: %m");
1102 int link_request_address(
1105 bool consume_object
,
1106 unsigned *message_counter
,
1107 link_netlink_message_handler_t netlink_handler
,
1116 r
= address_acquire(link
, address
, &acquired
);
1118 return log_link_warning_errno(link
, r
, "Failed to acquire an address from pool: %m");
1120 if (consume_object
) {
1121 address_free(address
);
1122 consume_object
= false; /* address from pool is already managed by Link. */
1127 log_address_debug(address
, "Requesting", link
);
1128 r
= link_queue_request(link
, REQUEST_TYPE_ADDRESS
, address
, consume_object
,
1129 message_counter
, netlink_handler
, ret
);
1131 return log_link_warning_errno(link
, r
, "Failed to request address: %m");
1135 int link_request_static_address(Link
*link
, Address
*address
, bool consume
) {
1142 r
= link_request_address(link
, address
, consume
, &link
->static_address_messages
,
1143 static_address_handler
, &req
);
1147 req
->after_configure
= static_address_after_configure
;
1151 int link_request_static_addresses(Link
*link
) {
1157 assert(link
->network
);
1159 link
->static_addresses_configured
= false;
1161 ORDERED_HASHMAP_FOREACH(a
, link
->network
->addresses_by_section
) {
1162 r
= link_request_static_address(link
, a
, false);
1167 HASHMAP_FOREACH(p
, link
->network
->prefixes_by_section
) {
1168 _cleanup_(address_freep
) Address
*address
= NULL
;
1173 r
= address_new(&address
);
1177 r
= sd_radv_prefix_get_prefix(p
->radv_prefix
, &address
->in_addr
.in6
, &address
->prefixlen
);
1179 return log_link_warning_errno(link
, r
, "Could not get RA prefix: %m");
1181 r
= generate_ipv6_eui_64_address(link
, &address
->in_addr
.in6
);
1183 return log_link_warning_errno(link
, r
, "Could not generate EUI64 address: %m");
1185 address
->family
= AF_INET6
;
1186 address
->route_metric
= p
->route_metric
;
1188 r
= link_request_static_address(link
, TAKE_PTR(address
), true);
1193 if (in4_addr_is_set(&link
->network
->dhcp_server_address
)) {
1194 _cleanup_(address_freep
) Address
*address
= NULL
;
1196 r
= address_new(&address
);
1200 address
->family
= AF_INET
;
1201 address
->in_addr
.in
= link
->network
->dhcp_server_address
;
1202 address
->prefixlen
= link
->network
->dhcp_server_address_prefixlen
;
1203 address_set_broadcast(address
);
1205 /* The same address may be explicitly configured in [Address] or [Network] section.
1206 * Configure the DHCP server address only when it is not. */
1207 if (!link_is_static_address_configured(link
, address
)) {
1208 r
= link_request_static_address(link
, TAKE_PTR(address
), true);
1214 if (link
->static_address_messages
== 0) {
1215 link
->static_addresses_configured
= true;
1216 link_check_ready(link
);
1218 log_link_debug(link
, "Setting addresses");
1219 link_set_state(link
, LINK_STATE_CONFIGURING
);
1225 static int address_is_ready_to_configure(Link
*link
, const Address
*address
) {
1231 if (!link_is_ready_to_configure(link
, false))
1234 if (link
->address_remove_messages
> 0)
1237 if (address_get(link
, address
, NULL
) >= 0)
1240 /* If this is a new address, then refuse adding more than the limit */
1241 if (set_size(link
->addresses
) >= ADDRESSES_PER_LINK_MAX
)
1242 return log_link_warning_errno(link
, SYNTHETIC_ERRNO(E2BIG
),
1243 "Too many addresses are configured, refusing: %m");
1245 if (address
->family
== AF_INET
&&
1246 address
->duplicate_address_detection
& ADDRESS_FAMILY_IPV4
&&
1247 link
->hw_addr
.length
== ETH_ALEN
&&
1248 !ether_addr_is_null(&link
->hw_addr
.ether
))
1249 return ipv4acd_address_is_ready_to_configure(link
, address
);
1251 r
= address_add(link
, address
, NULL
);
1253 return log_link_warning_errno(link
, r
, "Could not add address: %m");;
1258 int request_process_address(Request
*req
) {
1265 assert(req
->address
);
1266 assert(req
->type
== REQUEST_TYPE_ADDRESS
);
1270 r
= address_is_ready_to_configure(link
, req
->address
);
1274 r
= address_configure(req
->address
, link
, req
->netlink_handler
);
1278 /* To prevent a double decrement on failure in after_configure(). */
1279 req
->message_counter
= NULL
;
1281 r
= address_get(link
, req
->address
, &a
);
1285 if (req
->after_configure
) {
1286 r
= req
->after_configure(req
, a
);
1291 r
= address_set_masquerade(a
, true);
1293 log_link_warning_errno(link
, r
, "Could not enable IP masquerading, ignoring: %m");
1298 int manager_rtnl_process_address(sd_netlink
*rtnl
, sd_netlink_message
*message
, Manager
*m
) {
1299 _cleanup_(address_freep
) Address
*tmp
= NULL
;
1302 Address
*address
= NULL
;
1309 if (sd_netlink_message_is_error(message
)) {
1310 r
= sd_netlink_message_get_errno(message
);
1312 log_message_warning_errno(message
, r
, "rtnl: failed to receive address message, ignoring");
1317 r
= sd_netlink_message_get_type(message
, &type
);
1319 log_warning_errno(r
, "rtnl: could not get message type, ignoring: %m");
1321 } else if (!IN_SET(type
, RTM_NEWADDR
, RTM_DELADDR
)) {
1322 log_warning("rtnl: received unexpected message type %u when processing address, ignoring.", type
);
1326 r
= sd_rtnl_message_addr_get_ifindex(message
, &ifindex
);
1328 log_warning_errno(r
, "rtnl: could not get ifindex from message, ignoring: %m");
1330 } else if (ifindex
<= 0) {
1331 log_warning("rtnl: received address message with invalid ifindex %d, ignoring.", ifindex
);
1335 r
= link_get_by_index(m
, ifindex
, &link
);
1336 if (r
< 0 || !link
) {
1337 /* when enumerating we might be out of sync, but we will get the address again, so just
1339 if (!m
->enumerating
)
1340 log_warning("rtnl: received address for link '%d' we don't know about, ignoring.", ifindex
);
1344 r
= address_new(&tmp
);
1348 r
= sd_rtnl_message_addr_get_family(message
, &tmp
->family
);
1350 log_link_warning(link
, "rtnl: received address message without family, ignoring.");
1352 } else if (!IN_SET(tmp
->family
, AF_INET
, AF_INET6
)) {
1353 log_link_debug(link
, "rtnl: received address message with invalid family '%i', ignoring.", tmp
->family
);
1357 r
= sd_rtnl_message_addr_get_prefixlen(message
, &tmp
->prefixlen
);
1359 log_link_warning_errno(link
, r
, "rtnl: received address message without prefixlen, ignoring: %m");
1363 r
= sd_rtnl_message_addr_get_scope(message
, &tmp
->scope
);
1365 log_link_warning_errno(link
, r
, "rtnl: received address message without scope, ignoring: %m");
1369 r
= sd_netlink_message_read_u32(message
, IFA_FLAGS
, &tmp
->flags
);
1371 log_link_warning_errno(link
, r
, "rtnl: received address message without flags, ignoring: %m");
1375 switch (tmp
->family
) {
1377 r
= sd_netlink_message_read_in_addr(message
, IFA_LOCAL
, &tmp
->in_addr
.in
);
1379 log_link_warning_errno(link
, r
, "rtnl: received address message without valid address, ignoring: %m");
1383 r
= sd_netlink_message_read_in_addr(message
, IFA_ADDRESS
, &tmp
->in_addr_peer
.in
);
1384 if (r
< 0 && r
!= -ENODATA
) {
1385 log_link_warning_errno(link
, r
, "rtnl: could not get peer address from address message, ignoring: %m");
1387 } else if (r
>= 0) {
1388 if (in4_addr_equal(&tmp
->in_addr
.in
, &tmp
->in_addr_peer
.in
))
1389 tmp
->in_addr_peer
= IN_ADDR_NULL
;
1392 r
= sd_netlink_message_read_in_addr(message
, IFA_BROADCAST
, &tmp
->broadcast
);
1393 if (r
< 0 && r
!= -ENODATA
) {
1394 log_link_warning_errno(link
, r
, "rtnl: could not get broadcast from address message, ignoring: %m");
1398 r
= sd_netlink_message_read_string_strdup(message
, IFA_LABEL
, &tmp
->label
);
1399 if (r
< 0 && r
!= -ENODATA
) {
1400 log_link_warning_errno(link
, r
, "rtnl: could not get label from address message, ignoring: %m");
1402 } else if (r
>= 0 && streq_ptr(tmp
->label
, link
->ifname
))
1403 tmp
->label
= mfree(tmp
->label
);
1408 r
= sd_netlink_message_read_in6_addr(message
, IFA_LOCAL
, &tmp
->in_addr
.in6
);
1410 /* Have peer address. */
1411 r
= sd_netlink_message_read_in6_addr(message
, IFA_ADDRESS
, &tmp
->in_addr_peer
.in6
);
1413 log_link_warning_errno(link
, r
, "rtnl: could not get peer address from address message, ignoring: %m");
1416 } else if (r
== -ENODATA
) {
1417 /* Does not have peer address. */
1418 r
= sd_netlink_message_read_in6_addr(message
, IFA_ADDRESS
, &tmp
->in_addr
.in6
);
1420 log_link_warning_errno(link
, r
, "rtnl: received address message without valid address, ignoring: %m");
1424 log_link_warning_errno(link
, r
, "rtnl: could not get local address from address message, ignoring: %m");
1431 assert_not_reached();
1434 r
= sd_netlink_message_read_cache_info(message
, IFA_CACHEINFO
, &tmp
->cinfo
);
1435 if (r
< 0 && r
!= -ENODATA
) {
1436 log_link_warning_errno(link
, r
, "rtnl: cannot get IFA_CACHEINFO attribute, ignoring: %m");
1440 (void) address_get(link
, tmp
, &address
);
1444 log_address_debug(tmp
, address
? "Remembering updated" : "Remembering foreign", link
);
1446 /* An address appeared that we did not request */
1447 r
= address_add_foreign(link
, tmp
, &address
);
1449 _cleanup_free_
char *buf
= NULL
;
1451 (void) in_addr_prefix_to_string(tmp
->family
, &tmp
->in_addr
, tmp
->prefixlen
, &buf
);
1452 log_link_warning_errno(link
, r
, "Failed to remember foreign address %s, ignoring: %m",
1458 /* address_update() logs internally, so we don't need to here. */
1459 r
= address_update(address
, tmp
);
1461 link_enter_failed(link
);
1466 log_address_debug(tmp
, address
? "Forgetting" : "Kernel removed unknown", link
);
1467 (void) address_drop(address
);
1472 assert_not_reached();
1478 int config_parse_broadcast(
1480 const char *filename
,
1482 const char *section
,
1483 unsigned section_line
,
1490 Network
*network
= userdata
;
1491 _cleanup_(address_free_or_set_invalidp
) Address
*n
= NULL
;
1492 union in_addr_union u
;
1501 r
= address_new_static(network
, filename
, section_line
, &n
);
1505 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1506 "Failed to allocate new address, ignoring assignment: %m");
1510 if (isempty(rvalue
)) {
1511 /* The broadcast address will be calculated based on Address=, and set if the link is
1512 * not a wireguard interface. Here, we do not check or set n->family. */
1513 n
->broadcast
= (struct in_addr
) {};
1514 n
->set_broadcast
= -1;
1519 r
= parse_boolean(rvalue
);
1521 /* The broadcast address will be calculated based on Address=. Here, we do not check or
1523 n
->broadcast
= (struct in_addr
) {};
1524 n
->set_broadcast
= r
;
1529 if (n
->family
== AF_INET6
) {
1530 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1531 "Broadcast is not valid for IPv6 addresses, ignoring assignment: %s", rvalue
);
1535 r
= in_addr_from_string(AF_INET
, rvalue
, &u
);
1537 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1538 "Broadcast is invalid, ignoring assignment: %s", rvalue
);
1541 if (in4_addr_is_null(&u
.in
)) {
1542 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1543 "Broadcast cannot be ANY address, ignoring assignment: %s", rvalue
);
1547 n
->broadcast
= u
.in
;
1548 n
->set_broadcast
= true;
1549 n
->family
= AF_INET
;
1555 int config_parse_address(
1557 const char *filename
,
1559 const char *section
,
1560 unsigned section_line
,
1567 Network
*network
= userdata
;
1568 _cleanup_(address_free_or_set_invalidp
) Address
*n
= NULL
;
1569 union in_addr_union buffer
;
1570 unsigned char prefixlen
;
1579 if (streq(section
, "Network"))
1580 /* we are not in an Address section, so use line number instead. */
1581 r
= address_new_static(network
, filename
, line
, &n
);
1583 r
= address_new_static(network
, filename
, section_line
, &n
);
1587 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1588 "Failed to allocate new address, ignoring assignment: %m");
1592 /* Address=address/prefixlen */
1593 r
= in_addr_prefix_from_string_auto_internal(rvalue
, PREFIXLEN_REFUSE
, &f
, &buffer
, &prefixlen
);
1595 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1596 "An address '%s' is specified without prefix length. "
1597 "The behavior of parsing addresses without prefix length will be changed in the future release. "
1598 "Please specify prefix length explicitly.", rvalue
);
1600 r
= in_addr_prefix_from_string_auto_internal(rvalue
, PREFIXLEN_LEGACY
, &f
, &buffer
, &prefixlen
);
1603 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Invalid address '%s', ignoring assignment: %m", rvalue
);
1607 if (n
->family
!= AF_UNSPEC
&& f
!= n
->family
) {
1608 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "Address is incompatible, ignoring assignment: %s", rvalue
);
1612 if (in_addr_is_null(f
, &buffer
)) {
1613 /* Will use address from address pool. Note that for ipv6 case, prefix of the address
1614 * pool is 8, but 40 bit is used by the global ID and 16 bit by the subnet ID. So,
1615 * let's limit the prefix length to 64 or larger. See RFC4193. */
1616 if ((f
== AF_INET
&& prefixlen
< 8) ||
1617 (f
== AF_INET6
&& prefixlen
< 64)) {
1618 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1619 "Null address with invalid prefixlen='%u', ignoring assignment: %s",
1626 n
->prefixlen
= prefixlen
;
1628 if (streq(lvalue
, "Address"))
1629 n
->in_addr
= buffer
;
1631 n
->in_addr_peer
= buffer
;
1637 int config_parse_label(
1639 const char *filename
,
1641 const char *section
,
1642 unsigned section_line
,
1649 _cleanup_(address_free_or_set_invalidp
) Address
*n
= NULL
;
1650 Network
*network
= userdata
;
1659 r
= address_new_static(network
, filename
, section_line
, &n
);
1663 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1664 "Failed to allocate new address, ignoring assignment: %m");
1668 if (!address_label_valid(rvalue
)) {
1669 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1670 "Interface label is too long or invalid, ignoring assignment: %s", rvalue
);
1674 r
= free_and_strdup(&n
->label
, rvalue
);
1682 int config_parse_lifetime(
1684 const char *filename
,
1686 const char *section
,
1687 unsigned section_line
,
1694 Network
*network
= userdata
;
1695 _cleanup_(address_free_or_set_invalidp
) Address
*n
= NULL
;
1705 r
= address_new_static(network
, filename
, section_line
, &n
);
1709 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1710 "Failed to allocate new address, ignoring assignment: %m");
1714 /* We accept only "forever", "infinity", empty, or "0". */
1715 if (STR_IN_SET(rvalue
, "forever", "infinity", ""))
1716 k
= CACHE_INFO_INFINITY_LIFE_TIME
;
1717 else if (streq(rvalue
, "0"))
1720 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1721 "Invalid PreferredLifetime= value, ignoring: %s", rvalue
);
1725 n
->cinfo
.ifa_prefered
= k
;
1731 int config_parse_address_flags(
1733 const char *filename
,
1735 const char *section
,
1736 unsigned section_line
,
1743 Network
*network
= userdata
;
1744 _cleanup_(address_free_or_set_invalidp
) Address
*n
= NULL
;
1753 r
= address_new_static(network
, filename
, section_line
, &n
);
1757 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1758 "Failed to allocate new address, ignoring assignment: %m");
1762 r
= parse_boolean(rvalue
);
1764 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1765 "Failed to parse %s=, ignoring: %s", lvalue
, rvalue
);
1769 if (streq(lvalue
, "AddPrefixRoute"))
1772 SET_FLAG(n
->flags
, ltype
, r
);
1778 int config_parse_address_scope(
1780 const char *filename
,
1782 const char *section
,
1783 unsigned section_line
,
1790 Network
*network
= userdata
;
1791 _cleanup_(address_free_or_set_invalidp
) Address
*n
= NULL
;
1800 r
= address_new_static(network
, filename
, section_line
, &n
);
1804 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1805 "Failed to allocate new address, ignoring assignment: %m");
1809 if (streq(rvalue
, "host"))
1810 n
->scope
= RT_SCOPE_HOST
;
1811 else if (streq(rvalue
, "link"))
1812 n
->scope
= RT_SCOPE_LINK
;
1813 else if (streq(rvalue
, "global"))
1814 n
->scope
= RT_SCOPE_UNIVERSE
;
1816 r
= safe_atou8(rvalue
, &n
->scope
);
1818 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1819 "Could not parse address scope \"%s\", ignoring assignment: %m", rvalue
);
1824 n
->scope_set
= true;
1829 int config_parse_address_route_metric(
1831 const char *filename
,
1833 const char *section
,
1834 unsigned section_line
,
1841 Network
*network
= userdata
;
1842 _cleanup_(address_free_or_set_invalidp
) Address
*n
= NULL
;
1851 r
= address_new_static(network
, filename
, section_line
, &n
);
1855 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1856 "Failed to allocate new address, ignoring assignment: %m");
1860 r
= safe_atou32(rvalue
, &n
->route_metric
);
1862 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1863 "Could not parse %s=, ignoring assignment: %s", lvalue
, rvalue
);
1871 int config_parse_duplicate_address_detection(
1873 const char *filename
,
1875 const char *section
,
1876 unsigned section_line
,
1883 Network
*network
= userdata
;
1884 _cleanup_(address_free_or_set_invalidp
) Address
*n
= NULL
;
1893 r
= address_new_static(network
, filename
, section_line
, &n
);
1897 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1898 "Failed to allocate new address, ignoring assignment: %m");
1902 r
= parse_boolean(rvalue
);
1904 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1905 "For historical reasons, %s=%s means %s=%s. "
1906 "Please use 'both', 'ipv4', 'ipv6' or 'none' instead.",
1907 lvalue
, rvalue
, lvalue
, r
? "none" : "both");
1908 n
->duplicate_address_detection
= r
? ADDRESS_FAMILY_NO
: ADDRESS_FAMILY_YES
;
1913 AddressFamily a
= duplicate_address_detection_address_family_from_string(rvalue
);
1915 log_syntax(unit
, LOG_WARNING
, filename
, line
, a
,
1916 "Failed to parse %s=, ignoring: %s", lvalue
, rvalue
);
1919 n
->duplicate_address_detection
= a
;
1925 bool address_is_ready(const Address
*a
) {
1928 return !(a
->flags
& IFA_F_TENTATIVE
);
1931 static int address_section_verify(Address
*address
) {
1932 if (section_is_invalid(address
->section
))
1935 if (address
->family
== AF_UNSPEC
) {
1936 assert(address
->section
);
1938 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1939 "%s: Address section without Address= field configured. "
1940 "Ignoring [Address] section from line %u.",
1941 address
->section
->filename
, address
->section
->line
);
1944 if (address_may_have_broadcast(address
))
1945 address_set_broadcast(address
);
1946 else if (address
->broadcast
.s_addr
!= 0) {
1947 log_warning("%s: broadcast address is set for IPv6 address or IPv4 address with prefixlength larger than 30. "
1948 "Ignoring Broadcast= setting in the [Address] section from line %u.",
1949 address
->section
->filename
, address
->section
->line
);
1951 address
->broadcast
.s_addr
= 0;
1954 if (address
->family
== AF_INET6
&& address
->label
) {
1955 log_warning("%s: address label is set for IPv6 address in the [Address] section from line %u. "
1956 "Ignoring Label= setting.",
1957 address
->section
->filename
, address
->section
->line
);
1959 address
->label
= mfree(address
->label
);
1962 if (in_addr_is_localhost(address
->family
, &address
->in_addr
) > 0 &&
1963 (address
->family
== AF_INET
|| !address
->scope_set
)) {
1964 /* For IPv4, scope must be always RT_SCOPE_HOST.
1965 * For IPv6, use RT_SCOPE_HOST only when it is not explicitly specified. */
1967 if (address
->scope_set
&& address
->scope
!= RT_SCOPE_HOST
)
1968 log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1969 "%s: non-host scope is set in the [Address] section from line %u. "
1970 "Ignoring Scope= setting.",
1971 address
->section
->filename
, address
->section
->line
);
1973 address
->scope
= RT_SCOPE_HOST
;
1976 if (address
->family
== AF_INET6
&&
1977 !FLAGS_SET(address
->duplicate_address_detection
, ADDRESS_FAMILY_IPV6
))
1978 address
->flags
|= IFA_F_NODAD
;
1980 if (address
->family
== AF_INET
&& in4_addr_is_link_local(&address
->in_addr
.in
) &&
1981 !FLAGS_SET(address
->duplicate_address_detection
, ADDRESS_FAMILY_IPV4
)) {
1982 log_debug("%s: An IPv4 link-local address is specified, enabling IPv4 Address Conflict Detection (ACD).",
1983 address
->section
->filename
);
1984 address
->duplicate_address_detection
|= ADDRESS_FAMILY_IPV4
;
1990 void network_drop_invalid_addresses(Network
*network
) {
1995 ORDERED_HASHMAP_FOREACH(address
, network
->addresses_by_section
)
1996 if (address_section_verify(address
) < 0)
1997 address_free(address
);