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-manager.h"
13 #include "networkd-network.h"
14 #include "parse-util.h"
15 #include "string-util.h"
18 #define ADDRESSES_PER_LINK_MAX 2048U
19 #define STATIC_ADDRESSES_PER_NETWORK_MAX 1024U
21 int generate_ipv6_eui_64_address(const Link
*link
, struct in6_addr
*ret
) {
25 if (link
->iftype
== ARPHRD_INFINIBAND
) {
26 /* see RFC4391 section 8 */
27 memcpy(&ret
->s6_addr
[8], &link
->hw_addr
.addr
.infiniband
[12], 8);
28 ret
->s6_addr
[8] ^= 1 << 1;
33 /* see RFC4291 section 2.5.1 */
34 ret
->s6_addr
[8] = link
->hw_addr
.addr
.ether
.ether_addr_octet
[0];
35 ret
->s6_addr
[8] ^= 1 << 1;
36 ret
->s6_addr
[9] = link
->hw_addr
.addr
.ether
.ether_addr_octet
[1];
37 ret
->s6_addr
[10] = link
->hw_addr
.addr
.ether
.ether_addr_octet
[2];
38 ret
->s6_addr
[11] = 0xff;
39 ret
->s6_addr
[12] = 0xfe;
40 ret
->s6_addr
[13] = link
->hw_addr
.addr
.ether
.ether_addr_octet
[3];
41 ret
->s6_addr
[14] = link
->hw_addr
.addr
.ether
.ether_addr_octet
[4];
42 ret
->s6_addr
[15] = link
->hw_addr
.addr
.ether
.ether_addr_octet
[5];
47 int address_new(Address
**ret
) {
48 _cleanup_(address_freep
) Address
*address
= NULL
;
50 address
= new(Address
, 1);
54 *address
= (Address
) {
56 .scope
= RT_SCOPE_UNIVERSE
,
57 .cinfo
.ifa_prefered
= CACHE_INFO_INFINITY_LIFE_TIME
,
58 .cinfo
.ifa_valid
= CACHE_INFO_INFINITY_LIFE_TIME
,
59 .duplicate_address_detection
= ADDRESS_FAMILY_IPV6
,
62 *ret
= TAKE_PTR(address
);
67 static int address_new_static(Network
*network
, const char *filename
, unsigned section_line
, Address
**ret
) {
68 _cleanup_(network_config_section_freep
) NetworkConfigSection
*n
= NULL
;
69 _cleanup_(address_freep
) Address
*address
= NULL
;
75 assert(section_line
> 0);
77 r
= network_config_section_new(filename
, section_line
, &n
);
81 address
= ordered_hashmap_get(network
->addresses_by_section
, n
);
83 *ret
= TAKE_PTR(address
);
87 if (ordered_hashmap_size(network
->addresses_by_section
) >= STATIC_ADDRESSES_PER_NETWORK_MAX
)
90 r
= address_new(&address
);
94 address
->network
= network
;
95 address
->section
= TAKE_PTR(n
);
97 r
= ordered_hashmap_ensure_allocated(&network
->addresses_by_section
, &network_config_hash_ops
);
101 r
= ordered_hashmap_put(network
->addresses_by_section
, address
->section
, address
);
105 *ret
= TAKE_PTR(address
);
109 Address
*address_free(Address
*address
) {
113 if (address
->network
) {
114 assert(address
->section
);
115 ordered_hashmap_remove(address
->network
->addresses_by_section
, address
->section
);
121 set_remove(address
->link
->addresses
, address
);
122 set_remove(address
->link
->addresses_foreign
, address
);
123 set_remove(address
->link
->static_addresses
, address
);
124 if (address
->link
->dhcp_address
== address
)
125 address
->link
->dhcp_address
= NULL
;
126 if (address
->link
->dhcp_address_old
== address
)
127 address
->link
->dhcp_address_old
= NULL
;
128 set_remove(address
->link
->dhcp6_addresses
, address
);
129 set_remove(address
->link
->dhcp6_addresses_old
, address
);
130 set_remove(address
->link
->dhcp6_pd_addresses
, address
);
131 set_remove(address
->link
->dhcp6_pd_addresses_old
, address
);
132 SET_FOREACH(n
, address
->link
->ndisc_addresses
)
133 if (n
->address
== address
)
134 free(set_remove(address
->link
->ndisc_addresses
, n
));
136 if (in_addr_equal(AF_INET6
, &address
->in_addr
, (const union in_addr_union
*) &address
->link
->ipv6ll_address
))
137 memzero(&address
->link
->ipv6ll_address
, sizeof(struct in6_addr
));
140 sd_ipv4acd_unref(address
->acd
);
142 network_config_section_free(address
->section
);
143 free(address
->label
);
144 return mfree(address
);
147 void address_hash_func(const Address
*a
, struct siphash
*state
) {
150 siphash24_compress(&a
->family
, sizeof(a
->family
), state
);
154 siphash24_compress(&a
->broadcast
, sizeof(a
->broadcast
), state
);
155 siphash24_compress_string(a
->label
, state
);
159 siphash24_compress(&a
->prefixlen
, sizeof(a
->prefixlen
), state
);
161 siphash24_compress(&a
->in_addr
, FAMILY_ADDRESS_SIZE(a
->family
), state
);
163 siphash24_compress(&a
->in_addr_peer
, FAMILY_ADDRESS_SIZE(a
->family
), state
);
167 /* treat any other address family as AF_UNSPEC */
172 int address_compare_func(const Address
*a1
, const Address
*a2
) {
175 r
= CMP(a1
->family
, a2
->family
);
179 switch (a1
->family
) {
180 /* use the same notion of equality as the kernel does */
182 r
= CMP(a1
->broadcast
.s_addr
, a2
->broadcast
.s_addr
);
186 r
= strcmp_ptr(a1
->label
, a2
->label
);
192 r
= CMP(a1
->prefixlen
, a2
->prefixlen
);
196 r
= memcmp(&a1
->in_addr
, &a2
->in_addr
, FAMILY_ADDRESS_SIZE(a1
->family
));
200 return memcmp(&a1
->in_addr_peer
, &a2
->in_addr_peer
, FAMILY_ADDRESS_SIZE(a1
->family
));
202 /* treat any other address family as AF_UNSPEC */
207 DEFINE_HASH_OPS_WITH_KEY_DESTRUCTOR(address_hash_ops
, Address
, address_hash_func
, address_compare_func
, address_free
);
209 bool address_equal(const Address
*a1
, const Address
*a2
) {
216 return address_compare_func(a1
, a2
) == 0;
219 static int address_copy(Address
*dest
, const Address
*src
) {
225 r
= free_and_strdup(&dest
->label
, src
->label
);
229 dest
->family
= src
->family
;
230 dest
->prefixlen
= src
->prefixlen
;
231 dest
->scope
= src
->scope
;
232 dest
->flags
= src
->flags
;
233 dest
->broadcast
= src
->broadcast
;
234 dest
->cinfo
= src
->cinfo
;
235 dest
->in_addr
= src
->in_addr
;
236 dest
->in_addr_peer
= src
->in_addr_peer
;
237 dest
->duplicate_address_detection
= src
->duplicate_address_detection
;
242 static int address_set_masquerade(Address
*address
, bool add
) {
243 union in_addr_union masked
;
247 assert(address
->link
);
249 if (!address
->link
->network
)
252 if (!address
->link
->network
->ip_masquerade
)
255 if (address
->family
!= AF_INET
)
258 if (address
->scope
>= RT_SCOPE_LINK
)
261 if (address
->ip_masquerade_done
== add
)
264 masked
= address
->in_addr
;
265 r
= in_addr_mask(address
->family
, &masked
, address
->prefixlen
);
269 r
= fw_add_masquerade(add
, AF_INET
, 0, &masked
, address
->prefixlen
, NULL
, NULL
, 0);
273 address
->ip_masquerade_done
= add
;
278 static int address_add_internal(Link
*link
, Set
**addresses
, const Address
*in
, Address
**ret
) {
279 _cleanup_(address_freep
) Address
*address
= NULL
;
286 r
= address_new(&address
);
290 r
= address_copy(address
, in
);
294 /* Consider address tentative until we get the real flags from the kernel */
295 address
->flags
= IFA_F_TENTATIVE
;
297 r
= set_ensure_put(addresses
, &address_hash_ops
, address
);
303 address
->link
= link
;
311 static int address_add_foreign(Link
*link
, const Address
*in
, Address
**ret
) {
312 return address_add_internal(link
, &link
->addresses_foreign
, in
, ret
);
315 static int address_add(Link
*link
, const Address
*in
, Address
**ret
) {
322 r
= address_get(link
, in
, &address
);
324 /* Address does not exist, create a new one */
325 r
= address_add_internal(link
, &link
->addresses
, in
, &address
);
329 /* Take over a foreign address */
330 r
= set_ensure_put(&link
->addresses
, &address_hash_ops
, address
);
334 set_remove(link
->addresses_foreign
, address
);
336 /* Already exists, do nothing */
347 static int address_update(Address
*address
, const Address
*src
) {
352 assert(address
->link
);
355 ready
= address_is_ready(address
);
357 address
->flags
= src
->flags
;
358 address
->scope
= src
->scope
;
359 address
->cinfo
= src
->cinfo
;
361 if (IN_SET(address
->link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
364 link_update_operstate(address
->link
, true);
365 link_check_ready(address
->link
);
367 if (!ready
&& address_is_ready(address
)) {
368 if (address
->callback
) {
369 r
= address
->callback(address
);
374 if (address
->family
== AF_INET6
&&
375 in_addr_is_link_local(AF_INET6
, &address
->in_addr
) > 0 &&
376 IN6_IS_ADDR_UNSPECIFIED(&address
->link
->ipv6ll_address
) > 0) {
378 r
= link_ipv6ll_gained(address
->link
, &address
->in_addr
.in6
);
387 static int address_drop(Address
*address
) {
394 ready
= address_is_ready(address
);
395 link
= address
->link
;
397 r
= address_set_masquerade(address
, false);
399 log_link_warning_errno(link
, r
, "Failed to disable IP masquerading, ignoring: %m");
401 address_free(address
);
403 link_update_operstate(link
, true);
406 link_check_ready(link
);
411 int address_get(Link
*link
, const Address
*in
, Address
**ret
) {
417 existing
= set_get(link
->addresses
, in
);
424 existing
= set_get(link
->addresses_foreign
, in
);
434 static bool address_exists_internal(Set
*addresses
, int family
, const union in_addr_union
*in_addr
) {
437 SET_FOREACH(address
, addresses
) {
438 if (address
->family
!= family
)
440 if (in_addr_equal(address
->family
, &address
->in_addr
, in_addr
))
447 bool address_exists(Link
*link
, int family
, const union in_addr_union
*in_addr
) {
449 assert(IN_SET(family
, AF_INET
, AF_INET6
));
452 if (address_exists_internal(link
->addresses
, family
, in_addr
))
454 if (address_exists_internal(link
->addresses_foreign
, family
, in_addr
))
459 static int address_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
464 assert(link
->ifname
);
466 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
469 r
= sd_netlink_message_get_errno(m
);
470 if (r
< 0 && r
!= -EADDRNOTAVAIL
)
471 log_link_message_warning_errno(link
, m
, r
, "Could not drop address");
473 (void) manager_rtnl_process_address(rtnl
, m
, link
->manager
);
479 const Address
*address
,
481 link_netlink_message_handler_t callback
) {
483 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
487 assert(IN_SET(address
->family
, AF_INET
, AF_INET6
));
489 assert(link
->ifindex
> 0);
490 assert(link
->manager
);
491 assert(link
->manager
->rtnl
);
494 _cleanup_free_
char *b
= NULL
;
496 (void) in_addr_to_string(address
->family
, &address
->in_addr
, &b
);
497 log_link_debug(link
, "Removing address %s", strna(b
));
500 r
= sd_rtnl_message_new_addr(link
->manager
->rtnl
, &req
, RTM_DELADDR
,
501 link
->ifindex
, address
->family
);
503 return log_link_error_errno(link
, r
, "Could not allocate RTM_DELADDR message: %m");
505 r
= sd_rtnl_message_addr_set_prefixlen(req
, address
->prefixlen
);
507 return log_link_error_errno(link
, r
, "Could not set prefixlen: %m");
509 r
= netlink_message_append_in_addr_union(req
, IFA_LOCAL
, address
->family
, &address
->in_addr
);
511 return log_link_error_errno(link
, r
, "Could not append IFA_LOCAL attribute: %m");
513 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
,
514 callback
?: address_remove_handler
,
515 link_netlink_destroy_callback
, link
);
517 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
524 static bool link_is_static_address_configured(const Link
*link
, const Address
*address
) {
525 Address
*net_address
;
533 ORDERED_HASHMAP_FOREACH(net_address
, link
->network
->addresses_by_section
)
534 if (address_equal(net_address
, address
))
540 static bool link_address_is_dynamic(const Link
*link
, const Address
*address
) {
546 if (address
->cinfo
.ifa_prefered
!= CACHE_INFO_INFINITY_LIFE_TIME
)
549 /* Even when the address is leased from a DHCP server, networkd assign the address
550 * without lifetime when KeepConfiguration=dhcp. So, let's check that we have
551 * corresponding routes with RTPROT_DHCP. */
552 SET_FOREACH(route
, link
->routes_foreign
) {
553 if (route
->protocol
!= RTPROT_DHCP
)
556 if (address
->family
!= route
->family
)
559 if (in_addr_equal(address
->family
, &address
->in_addr
, &route
->prefsrc
))
566 static int link_enumerate_ipv6_tentative_addresses(Link
*link
) {
567 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
568 sd_netlink_message
*addr
;
572 assert(link
->manager
);
573 assert(link
->manager
->rtnl
);
575 r
= sd_rtnl_message_new_addr(link
->manager
->rtnl
, &req
, RTM_GETADDR
, 0, AF_INET6
);
579 r
= sd_netlink_call(link
->manager
->rtnl
, req
, 0, &reply
);
583 for (addr
= reply
; addr
; addr
= sd_netlink_message_next(addr
)) {
587 r
= sd_rtnl_message_addr_get_ifindex(addr
, &ifindex
);
589 log_link_warning_errno(link
, r
, "rtnl: invalid ifindex, ignoring: %m");
591 } else if (link
->ifindex
!= ifindex
)
594 r
= sd_rtnl_message_addr_get_flags(addr
, &flags
);
596 log_link_warning_errno(link
, r
, "rtnl: received address message with invalid flags, ignoring: %m");
598 } else if (!(flags
& IFA_F_TENTATIVE
))
601 log_link_debug(link
, "Found tentative ipv6 link-local address");
602 (void) manager_rtnl_process_address(link
->manager
->rtnl
, addr
, link
->manager
);
608 int link_drop_foreign_addresses(Link
*link
) {
614 /* The kernel doesn't notify us about tentative addresses;
615 * so if ipv6ll is disabled, we need to enumerate them now so we can drop them below */
616 if (!link_ipv6ll_enabled(link
)) {
617 r
= link_enumerate_ipv6_tentative_addresses(link
);
622 SET_FOREACH(address
, link
->addresses_foreign
) {
623 /* we consider IPv6LL addresses to be managed by the kernel */
624 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1 && link_ipv6ll_enabled(link
))
627 if (link_address_is_dynamic(link
, address
)) {
628 if (link
->network
&& FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP
))
630 } else if (link
->network
&& FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_STATIC
))
633 if (link_is_static_address_configured(link
, address
)) {
634 k
= address_add(link
, address
, NULL
);
636 log_link_error_errno(link
, k
, "Failed to add address: %m");
641 k
= address_remove(address
, link
, NULL
);
650 static int remove_static_address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
655 assert(link
->ifname
);
656 assert(link
->address_remove_messages
> 0);
658 link
->address_remove_messages
--;
660 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
663 r
= sd_netlink_message_get_errno(m
);
664 if (r
< 0 && r
!= -EADDRNOTAVAIL
)
665 log_link_message_warning_errno(link
, m
, r
, "Could not drop address");
667 (void) manager_rtnl_process_address(rtnl
, m
, link
->manager
);
669 if (link
->address_remove_messages
== 0 && link
->request_static_addresses
) {
670 link_set_state(link
, LINK_STATE_CONFIGURING
);
671 r
= link_set_addresses(link
);
673 link_enter_failed(link
);
679 int link_drop_addresses(Link
*link
) {
680 Address
*address
, *pool_address
;
685 SET_FOREACH(address
, link
->addresses
) {
686 /* we consider IPv6LL addresses to be managed by the kernel */
687 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1 && link_ipv6ll_enabled(link
))
690 k
= address_remove(address
, link
, remove_static_address_handler
);
691 if (k
< 0 && r
>= 0) {
696 link
->address_remove_messages
++;
698 SET_FOREACH(pool_address
, link
->pool_addresses
)
699 if (address_equal(address
, pool_address
))
700 address_free(set_remove(link
->pool_addresses
, pool_address
));
706 static int address_acquire(Link
*link
, const Address
*original
, Address
**ret
) {
707 union in_addr_union in_addr
= IN_ADDR_NULL
;
708 struct in_addr broadcast
= {};
709 _cleanup_(address_freep
) Address
*na
= NULL
;
716 /* Something useful was configured? just use it */
717 r
= in_addr_is_null(original
->family
, &original
->in_addr
);
725 /* The address is configured to be 0.0.0.0 or [::] by the user?
726 * Then let's acquire something more useful from the pool. */
727 r
= address_pool_acquire(link
->manager
, original
->family
, original
->prefixlen
, &in_addr
);
733 if (original
->family
== AF_INET
) {
734 /* Pick first address in range for ourselves ... */
735 in_addr
.in
.s_addr
= in_addr
.in
.s_addr
| htobe32(1);
737 /* .. and use last as broadcast address */
738 if (original
->prefixlen
> 30)
739 broadcast
.s_addr
= 0;
741 broadcast
.s_addr
= in_addr
.in
.s_addr
| htobe32(0xFFFFFFFFUL
>> original
->prefixlen
);
742 } else if (original
->family
== AF_INET6
)
743 in_addr
.in6
.s6_addr
[15] |= 1;
745 r
= address_new(&na
);
749 r
= address_copy(na
, original
);
753 na
->broadcast
= broadcast
;
754 na
->in_addr
= in_addr
;
756 r
= set_ensure_put(&link
->pool_addresses
, &address_hash_ops
, na
);
766 static int ipv4_dad_configure(Address
*address
);
768 int address_configure(
769 const Address
*address
,
771 link_netlink_message_handler_t callback
,
775 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
776 Address
*acquired_address
, *a
;
781 assert(IN_SET(address
->family
, AF_INET
, AF_INET6
));
783 assert(link
->ifindex
> 0);
784 assert(link
->manager
);
785 assert(link
->manager
->rtnl
);
788 /* If this is a new address, then refuse adding more than the limit */
789 if (address_get(link
, address
, NULL
) <= 0 &&
790 set_size(link
->addresses
) >= ADDRESSES_PER_LINK_MAX
)
791 return log_link_error_errno(link
, SYNTHETIC_ERRNO(E2BIG
),
792 "Too many addresses are configured, refusing: %m");
794 r
= address_acquire(link
, address
, &acquired_address
);
796 return log_link_error_errno(link
, r
, "Failed to acquire an address from pool: %m");
797 if (acquired_address
)
798 address
= acquired_address
;
801 _cleanup_free_
char *str
= NULL
;
803 (void) in_addr_to_string(address
->family
, &address
->in_addr
, &str
);
804 log_link_debug(link
, "%s address: %s", update
? "Updating" : "Configuring", strna(str
));
808 r
= sd_rtnl_message_new_addr_update(link
->manager
->rtnl
, &req
,
809 link
->ifindex
, address
->family
);
811 r
= sd_rtnl_message_new_addr(link
->manager
->rtnl
, &req
, RTM_NEWADDR
,
812 link
->ifindex
, address
->family
);
814 return log_link_error_errno(link
, r
, "Could not allocate RTM_NEWADDR message: %m");
816 r
= sd_rtnl_message_addr_set_prefixlen(req
, address
->prefixlen
);
818 return log_link_error_errno(link
, r
, "Could not set prefixlen: %m");
820 flags
= address
->flags
| IFA_F_PERMANENT
;
821 r
= sd_rtnl_message_addr_set_flags(req
, flags
& 0xff);
823 return log_link_error_errno(link
, r
, "Could not set flags: %m");
826 r
= sd_netlink_message_append_u32(req
, IFA_FLAGS
, flags
);
828 return log_link_error_errno(link
, r
, "Could not set extended flags: %m");
831 r
= sd_rtnl_message_addr_set_scope(req
, address
->scope
);
833 return log_link_error_errno(link
, r
, "Could not set scope: %m");
835 r
= netlink_message_append_in_addr_union(req
, IFA_LOCAL
, address
->family
, &address
->in_addr
);
837 return log_link_error_errno(link
, r
, "Could not append IFA_LOCAL attribute: %m");
839 if (in_addr_is_null(address
->family
, &address
->in_addr_peer
) == 0) {
840 r
= netlink_message_append_in_addr_union(req
, IFA_ADDRESS
, address
->family
, &address
->in_addr_peer
);
842 return log_link_error_errno(link
, r
, "Could not append IFA_ADDRESS attribute: %m");
843 } else if (address
->family
== AF_INET
&& address
->prefixlen
<= 30) {
844 r
= sd_netlink_message_append_in_addr(req
, IFA_BROADCAST
, &address
->broadcast
);
846 return log_link_error_errno(link
, r
, "Could not append IFA_BROADCAST attribute: %m");
849 if (address
->label
) {
850 r
= sd_netlink_message_append_string(req
, IFA_LABEL
, address
->label
);
852 return log_link_error_errno(link
, r
, "Could not append IFA_LABEL attribute: %m");
855 r
= sd_netlink_message_append_cache_info(req
, IFA_CACHEINFO
, &address
->cinfo
);
857 return log_link_error_errno(link
, r
, "Could not append IFA_CACHEINFO attribute: %m");
859 r
= address_add(link
, address
, &a
);
861 return log_link_error_errno(link
, r
, "Could not add address: %m");
863 r
= address_set_masquerade(a
, true);
865 log_link_warning_errno(link
, r
, "Could not enable IP masquerading, ignoring: %m");
867 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, callback
, link_netlink_destroy_callback
, link
);
869 (void) address_set_masquerade(a
, false);
870 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
875 if (FLAGS_SET(address
->duplicate_address_detection
, ADDRESS_FAMILY_IPV4
)) {
876 r
= ipv4_dad_configure(a
);
878 log_link_warning_errno(link
, r
, "Failed to start IPv4ACD client, ignoring: %m");
887 static int static_address_ready_callback(Address
*address
) {
892 assert(address
->link
);
894 link
= address
->link
;
896 if (!link
->addresses_configured
)
899 SET_FOREACH(a
, link
->static_addresses
)
900 if (!address_is_ready(a
)) {
901 _cleanup_free_
char *str
= NULL
;
903 (void) in_addr_to_string(a
->family
, &a
->in_addr
, &str
);
904 log_link_debug(link
, "an address %s/%u is not ready", strnull(str
), a
->prefixlen
);
908 /* This should not be called again */
909 SET_FOREACH(a
, link
->static_addresses
)
912 link
->addresses_ready
= true;
914 return link_set_routes(link
);
917 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
923 assert(link
->ifname
);
924 assert(link
->address_messages
> 0);
925 assert(IN_SET(link
->state
, LINK_STATE_CONFIGURING
,
926 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
928 link
->address_messages
--;
930 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
933 r
= sd_netlink_message_get_errno(m
);
934 if (r
< 0 && r
!= -EEXIST
) {
935 log_link_message_warning_errno(link
, m
, r
, "Could not set address");
936 link_enter_failed(link
);
939 (void) manager_rtnl_process_address(rtnl
, m
, link
->manager
);
941 if (link
->address_messages
== 0) {
944 log_link_debug(link
, "Addresses set");
945 link
->addresses_configured
= true;
947 /* When all static addresses are already ready, then static_address_ready_callback()
948 * will not be called automatically. So, call it here. */
949 a
= set_first(link
->static_addresses
);
951 log_link_warning(link
, "No static address is stored.");
952 link_enter_failed(link
);
956 log_link_warning(link
, "Address ready callback is not set.");
957 link_enter_failed(link
);
962 link_enter_failed(link
);
968 static int static_address_configure(const Address
*address
, Link
*link
, bool update
) {
975 r
= address_configure(address
, link
, address_handler
, update
, &ret
);
977 return log_link_warning_errno(link
, r
, "Could not configure static address: %m");
979 link
->address_messages
++;
981 r
= set_ensure_put(&link
->static_addresses
, &address_hash_ops
, ret
);
983 return log_link_warning_errno(link
, r
, "Failed to store static address: %m");
985 ret
->callback
= static_address_ready_callback
;
990 int link_set_addresses(Link
*link
) {
996 assert(link
->network
);
998 if (link
->address_remove_messages
!= 0) {
999 log_link_debug(link
, "Removing old addresses, new addresses will be configured later.");
1000 link
->request_static_addresses
= true;
1004 ORDERED_HASHMAP_FOREACH(ad
, link
->network
->addresses_by_section
) {
1007 update
= address_get(link
, ad
, NULL
) > 0;
1008 r
= static_address_configure(ad
, link
, update
);
1013 HASHMAP_FOREACH(p
, link
->network
->prefixes_by_section
) {
1014 _cleanup_(address_freep
) Address
*address
= NULL
;
1019 r
= address_new(&address
);
1023 r
= sd_radv_prefix_get_prefix(p
->radv_prefix
, &address
->in_addr
.in6
, &address
->prefixlen
);
1025 return log_link_warning_errno(link
, r
, "Could not get RA prefix: %m");
1027 r
= generate_ipv6_eui_64_address(link
, &address
->in_addr
.in6
);
1029 return log_link_warning_errno(link
, r
, "Could not generate EUI64 address: %m");
1031 address
->family
= AF_INET6
;
1032 r
= static_address_configure(address
, link
, true);
1037 if (link
->address_messages
== 0) {
1038 link
->addresses_configured
= true;
1039 link
->addresses_ready
= true;
1040 r
= link_set_routes(link
);
1044 log_link_debug(link
, "Setting addresses");
1045 link_set_state(link
, LINK_STATE_CONFIGURING
);
1051 int manager_rtnl_process_address(sd_netlink
*rtnl
, sd_netlink_message
*message
, Manager
*m
) {
1052 _cleanup_(address_freep
) Address
*tmp
= NULL
;
1053 _cleanup_free_
char *buf
= NULL
, *buf_peer
= NULL
;
1056 unsigned char flags
;
1057 Address
*address
= NULL
;
1058 char valid_buf
[FORMAT_TIMESPAN_MAX
];
1059 const char *valid_str
= NULL
;
1061 bool has_peer
= false;
1067 if (sd_netlink_message_is_error(message
)) {
1068 r
= sd_netlink_message_get_errno(message
);
1070 log_message_warning_errno(message
, r
, "rtnl: failed to receive address message, ignoring");
1075 r
= sd_netlink_message_get_type(message
, &type
);
1077 log_warning_errno(r
, "rtnl: could not get message type, ignoring: %m");
1079 } else if (!IN_SET(type
, RTM_NEWADDR
, RTM_DELADDR
)) {
1080 log_warning("rtnl: received unexpected message type %u when processing address, ignoring.", type
);
1084 r
= sd_rtnl_message_addr_get_ifindex(message
, &ifindex
);
1086 log_warning_errno(r
, "rtnl: could not get ifindex from message, ignoring: %m");
1088 } else if (ifindex
<= 0) {
1089 log_warning("rtnl: received address message with invalid ifindex %d, ignoring.", ifindex
);
1093 r
= link_get(m
, ifindex
, &link
);
1094 if (r
< 0 || !link
) {
1095 /* when enumerating we might be out of sync, but we will get the address again, so just
1097 if (!m
->enumerating
)
1098 log_warning("rtnl: received address for link '%d' we don't know about, ignoring.", ifindex
);
1102 r
= address_new(&tmp
);
1106 r
= sd_rtnl_message_addr_get_family(message
, &tmp
->family
);
1108 log_link_warning(link
, "rtnl: received address message without family, ignoring.");
1110 } else if (!IN_SET(tmp
->family
, AF_INET
, AF_INET6
)) {
1111 log_link_debug(link
, "rtnl: received address message with invalid family '%i', ignoring.", tmp
->family
);
1115 r
= sd_rtnl_message_addr_get_prefixlen(message
, &tmp
->prefixlen
);
1117 log_link_warning_errno(link
, r
, "rtnl: received address message without prefixlen, ignoring: %m");
1121 r
= sd_rtnl_message_addr_get_scope(message
, &tmp
->scope
);
1123 log_link_warning_errno(link
, r
, "rtnl: received address message without scope, ignoring: %m");
1127 r
= sd_rtnl_message_addr_get_flags(message
, &flags
);
1129 log_link_warning_errno(link
, r
, "rtnl: received address message without flags, ignoring: %m");
1134 switch (tmp
->family
) {
1136 r
= sd_netlink_message_read_in_addr(message
, IFA_LOCAL
, &tmp
->in_addr
.in
);
1138 log_link_warning_errno(link
, r
, "rtnl: received address message without valid address, ignoring: %m");
1142 r
= sd_netlink_message_read_in_addr(message
, IFA_ADDRESS
, &tmp
->in_addr_peer
.in
);
1143 if (r
< 0 && r
!= -ENODATA
) {
1144 log_link_warning_errno(link
, r
, "rtnl: could not get peer address from address message, ignoring: %m");
1146 } else if (r
>= 0) {
1147 if (in4_addr_equal(&tmp
->in_addr
.in
, &tmp
->in_addr_peer
.in
))
1148 tmp
->in_addr_peer
= IN_ADDR_NULL
;
1153 r
= sd_netlink_message_read_in_addr(message
, IFA_BROADCAST
, &tmp
->broadcast
);
1154 if (r
< 0 && r
!= -ENODATA
) {
1155 log_link_warning_errno(link
, r
, "rtnl: could not get broadcast from address message, ignoring: %m");
1159 r
= sd_netlink_message_read_string_strdup(message
, IFA_LABEL
, &tmp
->label
);
1160 if (r
< 0 && r
!= -ENODATA
) {
1161 log_link_warning_errno(link
, r
, "rtnl: could not get label from address message, ignoring: %m");
1163 } else if (r
>= 0 && streq_ptr(tmp
->label
, link
->ifname
))
1164 tmp
->label
= mfree(tmp
->label
);
1169 r
= sd_netlink_message_read_in6_addr(message
, IFA_LOCAL
, &tmp
->in_addr
.in6
);
1171 /* Have peer address. */
1172 r
= sd_netlink_message_read_in6_addr(message
, IFA_ADDRESS
, &tmp
->in_addr_peer
.in6
);
1174 log_link_warning_errno(link
, r
, "rtnl: could not get peer address from address message, ignoring: %m");
1178 } else if (r
== -ENODATA
) {
1179 /* Does not have peer address. */
1180 r
= sd_netlink_message_read_in6_addr(message
, IFA_ADDRESS
, &tmp
->in_addr
.in6
);
1182 log_link_warning_errno(link
, r
, "rtnl: received address message without valid address, ignoring: %m");
1186 log_link_warning_errno(link
, r
, "rtnl: could not get local address from address message, ignoring: %m");
1193 assert_not_reached("Received unsupported address family");
1196 (void) in_addr_to_string(tmp
->family
, &tmp
->in_addr
, &buf
);
1197 (void) in_addr_to_string(tmp
->family
, &tmp
->in_addr_peer
, &buf_peer
);
1199 r
= sd_netlink_message_read_cache_info(message
, IFA_CACHEINFO
, &tmp
->cinfo
);
1200 if (r
< 0 && r
!= -ENODATA
) {
1201 log_link_warning_errno(link
, r
, "rtnl: cannot get IFA_CACHEINFO attribute, ignoring: %m");
1203 } else if (r
>= 0 && tmp
->cinfo
.ifa_valid
!= CACHE_INFO_INFINITY_LIFE_TIME
)
1204 valid_str
= format_timespan(valid_buf
, FORMAT_TIMESPAN_MAX
,
1205 tmp
->cinfo
.ifa_valid
* USEC_PER_SEC
,
1208 (void) address_get(link
, tmp
, &address
);
1213 log_link_debug(link
, "Remembering updated address: %s%s%s/%u (valid %s%s)",
1214 strnull(buf
), has_peer
? " peer " : "",
1215 has_peer
? strnull(buf_peer
) : "", tmp
->prefixlen
,
1216 valid_str
? "for " : "forever", strempty(valid_str
));
1218 /* An address appeared that we did not request */
1219 r
= address_add_foreign(link
, tmp
, &address
);
1221 log_link_warning_errno(link
, r
, "Failed to remember foreign address %s/%u, ignoring: %m",
1222 strnull(buf
), tmp
->prefixlen
);
1225 log_link_debug(link
, "Remembering foreign address: %s%s%s/%u (valid %s%s)",
1226 strnull(buf
), has_peer
? " peer " : "",
1227 has_peer
? strnull(buf_peer
) : "", tmp
->prefixlen
,
1228 valid_str
? "for " : "forever", strempty(valid_str
));
1231 /* address_update() logs internally, so we don't need to here. */
1232 r
= address_update(address
, tmp
);
1234 link_enter_failed(link
);
1240 log_link_debug(link
, "Forgetting address: %s%s%s/%u (valid %s%s)",
1241 strnull(buf
), has_peer
? " peer " : "",
1242 has_peer
? strnull(buf_peer
) : "", tmp
->prefixlen
,
1243 valid_str
? "for " : "forever", strempty(valid_str
));
1244 (void) address_drop(address
);
1246 log_link_debug(link
, "Kernel removed an address we don't remember: %s%s%s/%u (valid %s%s), ignoring.",
1247 strnull(buf
), has_peer
? " peer " : "",
1248 has_peer
? strnull(buf_peer
) : "", tmp
->prefixlen
,
1249 valid_str
? "for " : "forever", strempty(valid_str
));
1254 assert_not_reached("Received invalid RTNL message type");
1260 int link_serialize_addresses(Link
*link
, FILE *f
) {
1266 fputs("ADDRESSES=", f
);
1267 SET_FOREACH(a
, link
->addresses
) {
1268 _cleanup_free_
char *address_str
= NULL
;
1270 if (in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
) < 0)
1273 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
1281 int link_deserialize_addresses(Link
*link
, const char *addresses
) {
1286 for (const char *p
= addresses
;; ) {
1287 _cleanup_(address_freep
) Address
*tmp
= NULL
;
1288 _cleanup_free_
char *address_str
= NULL
;
1290 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
1292 return log_link_debug_errno(link
, r
, "Failed to parse ADDRESSES=: %m");
1296 r
= address_new(&tmp
);
1300 r
= in_addr_prefix_from_string_auto(address_str
, &tmp
->family
, &tmp
->in_addr
, &tmp
->prefixlen
);
1302 log_link_debug_errno(link
, r
, "Failed to parse address, ignoring: %s", address_str
);
1306 r
= address_add(link
, tmp
, NULL
);
1308 log_link_debug_errno(link
, r
, "Failed to add address %s, ignoring: %m", address_str
);
1314 static void static_address_on_acd(sd_ipv4acd
*acd
, int event
, void *userdata
) {
1315 _cleanup_free_
char *pretty
= NULL
;
1323 address
= (Address
*) userdata
;
1324 link
= address
->link
;
1326 (void) in_addr_to_string(address
->family
, &address
->in_addr
, &pretty
);
1328 case SD_IPV4ACD_EVENT_STOP
:
1329 log_link_debug(link
, "Stopping ACD client...");
1332 case SD_IPV4ACD_EVENT_BIND
:
1333 log_link_debug(link
, "Successfully claimed address %s", strna(pretty
));
1334 link_check_ready(link
);
1337 case SD_IPV4ACD_EVENT_CONFLICT
:
1338 log_link_warning(link
, "DAD conflict. Dropping address %s", strna(pretty
));
1339 r
= address_remove(address
, link
, NULL
);
1341 log_link_error_errno(link
, r
, "Failed to drop DAD conflicted address %s", strna(pretty
));;
1343 link_check_ready(link
);
1347 assert_not_reached("Invalid IPv4ACD event.");
1350 (void) sd_ipv4acd_stop(acd
);
1355 static int ipv4_dad_configure(Address
*address
) {
1359 assert(address
->link
);
1361 if (address
->family
!= AF_INET
)
1364 if (DEBUG_LOGGING
) {
1365 _cleanup_free_
char *pretty
= NULL
;
1367 (void) in_addr_to_string(address
->family
, &address
->in_addr
, &pretty
);
1368 log_link_debug(address
->link
, "Starting IPv4ACD client. Probing address %s", strna(pretty
));
1371 if (!address
->acd
) {
1372 r
= sd_ipv4acd_new(&address
->acd
);
1376 r
= sd_ipv4acd_attach_event(address
->acd
, address
->link
->manager
->event
, 0);
1381 r
= sd_ipv4acd_set_ifindex(address
->acd
, address
->link
->ifindex
);
1385 r
= sd_ipv4acd_set_mac(address
->acd
, &address
->link
->hw_addr
.addr
.ether
);
1389 r
= sd_ipv4acd_set_address(address
->acd
, &address
->in_addr
.in
);
1393 r
= sd_ipv4acd_set_callback(address
->acd
, static_address_on_acd
, address
);
1397 return sd_ipv4acd_start(address
->acd
, true);
1400 static int ipv4_dad_update_mac_one(Address
*address
) {
1409 running
= sd_ipv4acd_is_running(address
->acd
);
1411 r
= sd_ipv4acd_stop(address
->acd
);
1415 r
= sd_ipv4acd_set_mac(address
->acd
, &address
->link
->hw_addr
.addr
.ether
);
1420 r
= sd_ipv4acd_start(address
->acd
, true);
1428 int ipv4_dad_update_mac(Link
*link
) {
1434 SET_FOREACH(address
, link
->addresses
) {
1435 k
= ipv4_dad_update_mac_one(address
);
1436 if (k
< 0 && r
>= 0)
1443 int ipv4_dad_stop(Link
*link
) {
1449 SET_FOREACH(address
, link
->addresses
) {
1450 k
= sd_ipv4acd_stop(address
->acd
);
1451 if (k
< 0 && r
>= 0)
1458 void ipv4_dad_unref(Link
*link
) {
1463 SET_FOREACH(address
, link
->addresses
)
1464 address
->acd
= sd_ipv4acd_unref(address
->acd
);
1467 int config_parse_broadcast(
1469 const char *filename
,
1471 const char *section
,
1472 unsigned section_line
,
1479 Network
*network
= userdata
;
1480 _cleanup_(address_free_or_set_invalidp
) Address
*n
= NULL
;
1489 r
= address_new_static(network
, filename
, section_line
, &n
);
1493 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1494 "Failed to allocate new address, ignoring assignment: %m");
1498 if (n
->family
== AF_INET6
) {
1499 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1500 "Broadcast is not valid for IPv6 addresses, ignoring assignment: %s", rvalue
);
1504 r
= in_addr_from_string(AF_INET
, rvalue
, (union in_addr_union
*) &n
->broadcast
);
1506 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1507 "Broadcast is invalid, ignoring assignment: %s", rvalue
);
1511 n
->family
= AF_INET
;
1517 int config_parse_address(
1519 const char *filename
,
1521 const char *section
,
1522 unsigned section_line
,
1529 Network
*network
= userdata
;
1530 _cleanup_(address_free_or_set_invalidp
) Address
*n
= NULL
;
1531 union in_addr_union buffer
;
1532 unsigned char prefixlen
;
1541 if (streq(section
, "Network"))
1542 /* we are not in an Address section, so use line number instead. */
1543 r
= address_new_static(network
, filename
, line
, &n
);
1545 r
= address_new_static(network
, filename
, section_line
, &n
);
1549 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1550 "Failed to allocate new address, ignoring assignment: %m");
1554 /* Address=address/prefixlen */
1555 r
= in_addr_prefix_from_string_auto_internal(rvalue
, PREFIXLEN_REFUSE
, &f
, &buffer
, &prefixlen
);
1557 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1558 "An address '%s' is specified without prefix length. "
1559 "The behavior of parsing addresses without prefix length will be changed in the future release. "
1560 "Please specify prefix length explicitly.", rvalue
);
1562 r
= in_addr_prefix_from_string_auto_internal(rvalue
, PREFIXLEN_LEGACY
, &f
, &buffer
, &prefixlen
);
1565 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Invalid address '%s', ignoring assignment: %m", rvalue
);
1569 if (n
->family
!= AF_UNSPEC
&& f
!= n
->family
) {
1570 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "Address is incompatible, ignoring assignment: %s", rvalue
);
1574 if (in_addr_is_null(f
, &buffer
)) {
1575 /* Will use address from address pool. Note that for ipv6 case, prefix of the address
1576 * pool is 8, but 40 bit is used by the global ID and 16 bit by the subnet ID. So,
1577 * let's limit the prefix length to 64 or larger. See RFC4193. */
1578 if ((f
== AF_INET
&& prefixlen
< 8) ||
1579 (f
== AF_INET6
&& prefixlen
< 64)) {
1580 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1581 "Null address with invalid prefixlen='%u', ignoring assignment: %s",
1588 n
->prefixlen
= prefixlen
;
1590 if (streq(lvalue
, "Address"))
1591 n
->in_addr
= buffer
;
1593 n
->in_addr_peer
= buffer
;
1600 int config_parse_label(
1602 const char *filename
,
1604 const char *section
,
1605 unsigned section_line
,
1612 _cleanup_(address_free_or_set_invalidp
) Address
*n
= NULL
;
1613 Network
*network
= userdata
;
1622 r
= address_new_static(network
, filename
, section_line
, &n
);
1626 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1627 "Failed to allocate new address, ignoring assignment: %m");
1631 if (!address_label_valid(rvalue
)) {
1632 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1633 "Interface label is too long or invalid, ignoring assignment: %s", rvalue
);
1637 r
= free_and_strdup(&n
->label
, rvalue
);
1645 int config_parse_lifetime(
1647 const char *filename
,
1649 const char *section
,
1650 unsigned section_line
,
1657 Network
*network
= userdata
;
1658 _cleanup_(address_free_or_set_invalidp
) Address
*n
= NULL
;
1668 r
= address_new_static(network
, filename
, section_line
, &n
);
1672 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1673 "Failed to allocate new address, ignoring assignment: %m");
1677 /* We accept only "forever", "infinity", empty, or "0". */
1678 if (STR_IN_SET(rvalue
, "forever", "infinity", ""))
1679 k
= CACHE_INFO_INFINITY_LIFE_TIME
;
1680 else if (streq(rvalue
, "0"))
1683 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1684 "Invalid PreferredLifetime= value, ignoring: %s", rvalue
);
1688 n
->cinfo
.ifa_prefered
= k
;
1694 int config_parse_address_flags(
1696 const char *filename
,
1698 const char *section
,
1699 unsigned section_line
,
1706 Network
*network
= userdata
;
1707 _cleanup_(address_free_or_set_invalidp
) Address
*n
= NULL
;
1716 r
= address_new_static(network
, filename
, section_line
, &n
);
1720 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1721 "Failed to allocate new address, ignoring assignment: %m");
1725 r
= parse_boolean(rvalue
);
1727 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1728 "Failed to parse %s=, ignoring: %s", lvalue
, rvalue
);
1732 if (streq(lvalue
, "AddPrefixRoute"))
1735 SET_FLAG(n
->flags
, ltype
, r
);
1741 int config_parse_address_scope(
1743 const char *filename
,
1745 const char *section
,
1746 unsigned section_line
,
1753 Network
*network
= userdata
;
1754 _cleanup_(address_free_or_set_invalidp
) Address
*n
= NULL
;
1763 r
= address_new_static(network
, filename
, section_line
, &n
);
1767 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1768 "Failed to allocate new address, ignoring assignment: %m");
1772 if (streq(rvalue
, "host"))
1773 n
->scope
= RT_SCOPE_HOST
;
1774 else if (streq(rvalue
, "link"))
1775 n
->scope
= RT_SCOPE_LINK
;
1776 else if (streq(rvalue
, "global"))
1777 n
->scope
= RT_SCOPE_UNIVERSE
;
1779 r
= safe_atou8(rvalue
, &n
->scope
);
1781 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1782 "Could not parse address scope \"%s\", ignoring assignment: %m", rvalue
);
1787 n
->scope_set
= true;
1792 int config_parse_duplicate_address_detection(
1794 const char *filename
,
1796 const char *section
,
1797 unsigned section_line
,
1804 Network
*network
= userdata
;
1805 _cleanup_(address_free_or_set_invalidp
) Address
*n
= NULL
;
1815 r
= address_new_static(network
, filename
, section_line
, &n
);
1819 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1820 "Failed to allocate new address, ignoring assignment: %m");
1824 r
= parse_boolean(rvalue
);
1826 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1827 "For historical reasons, %s=%s means %s=%s. "
1828 "Please use 'both', 'ipv4', 'ipv6' or 'none' instead.",
1829 lvalue
, rvalue
, lvalue
, r
? "none" : "both");
1830 n
->duplicate_address_detection
= r
? ADDRESS_FAMILY_NO
: ADDRESS_FAMILY_YES
;
1835 a
= duplicate_address_detection_address_family_from_string(rvalue
);
1837 log_syntax(unit
, LOG_WARNING
, filename
, line
, SYNTHETIC_ERRNO(EINVAL
),
1838 "Failed to parse %s=, ignoring: %s", lvalue
, rvalue
);
1842 n
->duplicate_address_detection
= a
;
1847 bool address_is_ready(const Address
*a
) {
1850 return !(a
->flags
& IFA_F_TENTATIVE
);
1853 static int address_section_verify(Address
*address
) {
1854 if (section_is_invalid(address
->section
))
1857 if (address
->family
== AF_UNSPEC
) {
1858 assert(address
->section
);
1860 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1861 "%s: Address section without Address= field configured. "
1862 "Ignoring [Address] section from line %u.",
1863 address
->section
->filename
, address
->section
->line
);
1866 if (address
->family
== AF_INET
&& in_addr_is_null(address
->family
, &address
->in_addr_peer
) &&
1867 address
->broadcast
.s_addr
== 0 && address
->prefixlen
<= 30)
1868 address
->broadcast
.s_addr
= address
->in_addr
.in
.s_addr
| htobe32(0xfffffffflu
>> address
->prefixlen
);
1869 else if (address
->broadcast
.s_addr
!= 0) {
1870 log_warning("%s: broadcast address is set for IPv6 address or IPv4 address with prefixlength larger than 30. "
1871 "Ignoring Broadcast= setting in the [Address] section from line %u.",
1872 address
->section
->filename
, address
->section
->line
);
1874 address
->broadcast
.s_addr
= 0;
1877 if (address
->family
== AF_INET6
&& address
->label
) {
1878 log_warning("%s: address label is set for IPv6 address in the [Address] section from line %u. "
1879 "Ignoring Label= setting.",
1880 address
->section
->filename
, address
->section
->line
);
1882 address
->label
= mfree(address
->label
);
1885 if (in_addr_is_localhost(address
->family
, &address
->in_addr
) > 0 &&
1886 (address
->family
== AF_INET
|| !address
->scope_set
)) {
1887 /* For IPv4, scope must be always RT_SCOPE_HOST.
1888 * For IPv6, use RT_SCOPE_HOST only when it is not explicitly specified. */
1890 if (address
->scope_set
&& address
->scope
!= RT_SCOPE_HOST
)
1891 log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1892 "%s: non-host scope is set in the [Address] section from line %u. "
1893 "Ignoring Scope= setting.",
1894 address
->section
->filename
, address
->section
->line
);
1896 address
->scope
= RT_SCOPE_HOST
;
1899 if (!FLAGS_SET(address
->duplicate_address_detection
, ADDRESS_FAMILY_IPV6
))
1900 address
->flags
|= IFA_F_NODAD
;
1905 void network_drop_invalid_addresses(Network
*network
) {
1910 ORDERED_HASHMAP_FOREACH(address
, network
->addresses_by_section
)
1911 if (address_section_verify(address
) < 0)
1912 address_free(address
);