1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 #include "alloc-util.h"
6 #include "conf-parser.h"
7 #include "firewall-util.h"
8 #include "memory-util.h"
9 #include "missing_network.h"
10 #include "netlink-util.h"
11 #include "networkd-address.h"
12 #include "networkd-manager.h"
13 #include "parse-util.h"
15 #include "socket-util.h"
16 #include "string-util.h"
20 #define ADDRESSES_PER_LINK_MAX 2048U
21 #define STATIC_ADDRESSES_PER_NETWORK_MAX 1024U
23 int generate_ipv6_eui_64_address(Link
*link
, struct in6_addr
*ret
) {
27 /* see RFC4291 section 2.5.1 */
28 ret
->s6_addr
[8] = link
->mac
.ether_addr_octet
[0];
29 ret
->s6_addr
[8] ^= 1 << 1;
30 ret
->s6_addr
[9] = link
->mac
.ether_addr_octet
[1];
31 ret
->s6_addr
[10] = link
->mac
.ether_addr_octet
[2];
32 ret
->s6_addr
[11] = 0xff;
33 ret
->s6_addr
[12] = 0xfe;
34 ret
->s6_addr
[13] = link
->mac
.ether_addr_octet
[3];
35 ret
->s6_addr
[14] = link
->mac
.ether_addr_octet
[4];
36 ret
->s6_addr
[15] = link
->mac
.ether_addr_octet
[5];
41 int address_new(Address
**ret
) {
42 _cleanup_(address_freep
) Address
*address
= NULL
;
44 address
= new(Address
, 1);
48 *address
= (Address
) {
50 .scope
= RT_SCOPE_UNIVERSE
,
51 .cinfo
.ifa_prefered
= CACHE_INFO_INFINITY_LIFE_TIME
,
52 .cinfo
.ifa_valid
= CACHE_INFO_INFINITY_LIFE_TIME
,
53 .duplicate_address_detection
= ADDRESS_FAMILY_IPV6
,
57 *ret
= TAKE_PTR(address
);
62 static int address_new_static(Network
*network
, const char *filename
, unsigned section_line
, Address
**ret
) {
63 _cleanup_(network_config_section_freep
) NetworkConfigSection
*n
= NULL
;
64 _cleanup_(address_freep
) Address
*address
= NULL
;
69 assert(!!filename
== (section_line
> 0));
72 r
= network_config_section_new(filename
, section_line
, &n
);
76 address
= hashmap_get(network
->addresses_by_section
, n
);
78 *ret
= TAKE_PTR(address
);
84 if (network
->n_static_addresses
>= STATIC_ADDRESSES_PER_NETWORK_MAX
)
87 r
= address_new(&address
);
91 address
->network
= network
;
92 LIST_APPEND(addresses
, network
->static_addresses
, address
);
93 network
->n_static_addresses
++;
96 address
->section
= TAKE_PTR(n
);
98 r
= hashmap_ensure_allocated(&network
->addresses_by_section
, &network_config_hash_ops
);
102 r
= hashmap_put(network
->addresses_by_section
, address
->section
, address
);
107 *ret
= TAKE_PTR(address
);
112 void address_free(Address
*address
) {
116 if (address
->network
) {
117 LIST_REMOVE(addresses
, address
->network
->static_addresses
, address
);
118 assert(address
->network
->n_static_addresses
> 0);
119 address
->network
->n_static_addresses
--;
121 if (address
->section
)
122 hashmap_remove(address
->network
->addresses_by_section
, address
->section
);
125 if (address
->link
&& !address
->acd
) {
126 set_remove(address
->link
->addresses
, address
);
127 set_remove(address
->link
->addresses_foreign
, address
);
129 if (in_addr_equal(AF_INET6
, &address
->in_addr
, (const union in_addr_union
*) &address
->link
->ipv6ll_address
))
130 memzero(&address
->link
->ipv6ll_address
, sizeof(struct in6_addr
));
133 sd_ipv4acd_unref(address
->acd
);
135 network_config_section_free(address
->section
);
136 free(address
->label
);
140 static uint32_t address_prefix(const Address
*a
) {
143 /* make sure we don't try to shift by 32.
144 * See ISO/IEC 9899:TC3 ยง 6.5.7.3. */
145 if (a
->prefixlen
== 0)
148 if (a
->in_addr_peer
.in
.s_addr
!= 0)
149 return be32toh(a
->in_addr_peer
.in
.s_addr
) >> (32 - a
->prefixlen
);
151 return be32toh(a
->in_addr
.in
.s_addr
) >> (32 - a
->prefixlen
);
154 static void address_hash_func(const Address
*a
, struct siphash
*state
) {
157 siphash24_compress(&a
->family
, sizeof(a
->family
), state
);
161 siphash24_compress(&a
->prefixlen
, sizeof(a
->prefixlen
), state
);
164 uint32_t prefix
= address_prefix(a
);
165 siphash24_compress(&prefix
, sizeof(prefix
), state
);
170 siphash24_compress(&a
->in_addr
, FAMILY_ADDRESS_SIZE(a
->family
), state
);
174 /* treat any other address family as AF_UNSPEC */
179 static int address_compare_func(const Address
*a1
, const Address
*a2
) {
182 r
= CMP(a1
->family
, a2
->family
);
186 switch (a1
->family
) {
187 /* use the same notion of equality as the kernel does */
189 r
= CMP(a1
->prefixlen
, a2
->prefixlen
);
193 uint32_t prefix1
= address_prefix(a1
);
194 uint32_t prefix2
= address_prefix(a2
);
195 r
= CMP(prefix1
, prefix2
);
201 return memcmp(&a1
->in_addr
, &a2
->in_addr
, FAMILY_ADDRESS_SIZE(a1
->family
));
203 /* treat any other address family as AF_UNSPEC */
208 DEFINE_HASH_OPS_WITH_KEY_DESTRUCTOR(address_hash_ops
, Address
, address_hash_func
, address_compare_func
, address_free
);
210 bool address_equal(Address
*a1
, Address
*a2
) {
217 return address_compare_func(a1
, a2
) == 0;
220 static int address_establish(Address
*address
, Link
*link
) {
227 masq
= link
->network
&&
228 link
->network
->ip_masquerade
&&
229 address
->family
== AF_INET
&&
230 address
->scope
< RT_SCOPE_LINK
;
232 /* Add firewall entry if this is requested */
233 if (address
->ip_masquerade_done
!= masq
) {
234 union in_addr_union masked
= address
->in_addr
;
235 in_addr_mask(address
->family
, &masked
, address
->prefixlen
);
237 r
= fw_add_masquerade(masq
, AF_INET
, 0, &masked
, address
->prefixlen
, NULL
, NULL
, 0);
241 address
->ip_masquerade_done
= masq
;
247 static int address_add_internal(Link
*link
, Set
**addresses
,
249 const union in_addr_union
*in_addr
,
250 unsigned char prefixlen
,
252 _cleanup_(address_freep
) Address
*address
= NULL
;
259 r
= address_new(&address
);
263 address
->family
= family
;
264 address
->in_addr
= *in_addr
;
265 address
->prefixlen
= prefixlen
;
266 /* Consider address tentative until we get the real flags from the kernel */
267 address
->flags
= IFA_F_TENTATIVE
;
269 r
= set_ensure_put(addresses
, &address_hash_ops
, address
);
275 address
->link
= link
;
283 int address_add_foreign(Link
*link
, int family
, const union in_addr_union
*in_addr
, unsigned char prefixlen
, Address
**ret
) {
284 return address_add_internal(link
, &link
->addresses_foreign
, family
, in_addr
, prefixlen
, ret
);
287 int address_add(Link
*link
, int family
, const union in_addr_union
*in_addr
, unsigned char prefixlen
, Address
**ret
) {
291 r
= address_get(link
, family
, in_addr
, prefixlen
, &address
);
293 /* Address does not exist, create a new one */
294 r
= address_add_internal(link
, &link
->addresses
, family
, in_addr
, prefixlen
, &address
);
298 /* Take over a foreign address */
299 r
= set_ensure_put(&link
->addresses
, &address_hash_ops
, address
);
303 set_remove(link
->addresses_foreign
, address
);
305 /* Already exists, do nothing */
316 static int address_release(Address
*address
) {
320 assert(address
->link
);
322 /* Remove masquerading firewall entry if it was added */
323 if (address
->ip_masquerade_done
) {
324 union in_addr_union masked
= address
->in_addr
;
325 in_addr_mask(address
->family
, &masked
, address
->prefixlen
);
327 r
= fw_add_masquerade(false, AF_INET
, 0, &masked
, address
->prefixlen
, NULL
, NULL
, 0);
331 address
->ip_masquerade_done
= false;
341 const struct ifa_cacheinfo
*cinfo
) {
348 assert_return(address
->link
, 1);
350 if (IN_SET(address
->link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
353 ready
= address_is_ready(address
);
355 address
->flags
= flags
;
356 address
->scope
= scope
;
357 address
->cinfo
= *cinfo
;
359 link_update_operstate(address
->link
, true);
360 link_check_ready(address
->link
);
362 if (!ready
&& address_is_ready(address
)) {
363 if (address
->callback
) {
364 r
= address
->callback(address
);
369 if (address
->family
== AF_INET6
&&
370 in_addr_is_link_local(AF_INET6
, &address
->in_addr
) > 0 &&
371 IN6_IS_ADDR_UNSPECIFIED(&address
->link
->ipv6ll_address
) > 0) {
373 r
= link_ipv6ll_gained(address
->link
, &address
->in_addr
.in6
);
382 int address_drop(Address
*address
) {
389 ready
= address_is_ready(address
);
390 link
= address
->link
;
392 r
= address_release(address
);
394 log_link_warning_errno(link
, r
, "Failed to disable IP masquerading, ignoring: %m");
396 address_free(address
);
398 link_update_operstate(link
, true);
401 link_check_ready(link
);
406 int address_get(Link
*link
,
408 const union in_addr_union
*in_addr
,
409 unsigned char prefixlen
,
412 Address address
, *existing
;
417 address
= (Address
) {
420 .prefixlen
= prefixlen
,
423 existing
= set_get(link
->addresses
, &address
);
430 existing
= set_get(link
->addresses_foreign
, &address
);
440 static bool address_exists_internal(Set
*addresses
, int family
, const union in_addr_union
*in_addr
) {
444 SET_FOREACH(address
, addresses
, i
) {
445 if (address
->family
!= family
)
447 if (in_addr_equal(address
->family
, &address
->in_addr
, in_addr
))
454 bool address_exists(Link
*link
, int family
, const union in_addr_union
*in_addr
) {
456 assert(IN_SET(family
, AF_INET
, AF_INET6
));
459 if (address_exists_internal(link
->addresses
, family
, in_addr
))
461 if (address_exists_internal(link
->addresses_foreign
, family
, in_addr
))
466 static int address_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
471 assert(link
->ifname
);
473 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
476 r
= sd_netlink_message_get_errno(m
);
477 if (r
< 0 && r
!= -EADDRNOTAVAIL
)
478 log_link_message_warning_errno(link
, m
, r
, "Could not drop address");
480 (void) manager_rtnl_process_address(rtnl
, m
, link
->manager
);
488 link_netlink_message_handler_t callback
) {
490 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
494 assert(IN_SET(address
->family
, AF_INET
, AF_INET6
));
496 assert(link
->ifindex
> 0);
497 assert(link
->manager
);
498 assert(link
->manager
->rtnl
);
501 _cleanup_free_
char *b
= NULL
;
503 (void) in_addr_to_string(address
->family
, &address
->in_addr
, &b
);
504 log_link_debug(link
, "Removing address %s", strna(b
));
507 r
= sd_rtnl_message_new_addr(link
->manager
->rtnl
, &req
, RTM_DELADDR
,
508 link
->ifindex
, address
->family
);
510 return log_link_error_errno(link
, r
, "Could not allocate RTM_DELADDR message: %m");
512 r
= sd_rtnl_message_addr_set_prefixlen(req
, address
->prefixlen
);
514 return log_link_error_errno(link
, r
, "Could not set prefixlen: %m");
516 r
= netlink_message_append_in_addr_union(req
, IFA_LOCAL
, address
->family
, &address
->in_addr
);
518 return log_link_error_errno(link
, r
, "Could not append IFA_LOCAL attribute: %m");
520 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
,
521 callback
?: address_remove_handler
,
522 link_netlink_destroy_callback
, link
);
524 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
531 static int address_acquire(Link
*link
, Address
*original
, Address
**ret
) {
532 union in_addr_union in_addr
= IN_ADDR_NULL
;
533 struct in_addr broadcast
= {};
534 _cleanup_(address_freep
) Address
*na
= NULL
;
541 /* Something useful was configured? just use it */
542 r
= in_addr_is_null(original
->family
, &original
->in_addr
);
546 /* The address is configured to be 0.0.0.0 or [::] by the user?
547 * Then let's acquire something more useful from the pool. */
548 r
= manager_address_pool_acquire(link
->manager
, original
->family
, original
->prefixlen
, &in_addr
);
554 if (original
->family
== AF_INET
) {
555 /* Pick first address in range for ourselves ... */
556 in_addr
.in
.s_addr
= in_addr
.in
.s_addr
| htobe32(1);
558 /* .. and use last as broadcast address */
559 if (original
->prefixlen
> 30)
560 broadcast
.s_addr
= 0;
562 broadcast
.s_addr
= in_addr
.in
.s_addr
| htobe32(0xFFFFFFFFUL
>> original
->prefixlen
);
563 } else if (original
->family
== AF_INET6
)
564 in_addr
.in6
.s6_addr
[15] |= 1;
566 r
= address_new(&na
);
570 na
->family
= original
->family
;
571 na
->prefixlen
= original
->prefixlen
;
572 na
->scope
= original
->scope
;
573 na
->cinfo
= original
->cinfo
;
575 if (original
->label
) {
576 na
->label
= strdup(original
->label
);
581 na
->broadcast
= broadcast
;
582 na
->in_addr
= in_addr
;
584 LIST_PREPEND(addresses
, link
->pool_addresses
, na
);
591 int address_configure(
594 link_netlink_message_handler_t callback
,
597 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
601 assert(IN_SET(address
->family
, AF_INET
, AF_INET6
));
603 assert(link
->ifindex
> 0);
604 assert(link
->manager
);
605 assert(link
->manager
->rtnl
);
608 /* If this is a new address, then refuse adding more than the limit */
609 if (address_get(link
, address
->family
, &address
->in_addr
, address
->prefixlen
, NULL
) <= 0 &&
610 set_size(link
->addresses
) >= ADDRESSES_PER_LINK_MAX
)
611 return log_link_error_errno(link
, SYNTHETIC_ERRNO(E2BIG
),
612 "Too many addresses are configured, refusing: %m");
614 r
= address_acquire(link
, address
, &address
);
616 return log_link_error_errno(link
, r
, "Failed to acquire an address from pool: %m");
619 _cleanup_free_
char *str
= NULL
;
621 (void) in_addr_to_string(address
->family
, &address
->in_addr
, &str
);
622 log_link_debug(link
, "%s address: %s", update
? "Updating" : "Configuring", strna(str
));
626 r
= sd_rtnl_message_new_addr_update(link
->manager
->rtnl
, &req
,
627 link
->ifindex
, address
->family
);
629 r
= sd_rtnl_message_new_addr(link
->manager
->rtnl
, &req
, RTM_NEWADDR
,
630 link
->ifindex
, address
->family
);
632 return log_link_error_errno(link
, r
, "Could not allocate RTM_NEWADDR message: %m");
634 r
= sd_rtnl_message_addr_set_prefixlen(req
, address
->prefixlen
);
636 return log_link_error_errno(link
, r
, "Could not set prefixlen: %m");
638 address
->flags
|= IFA_F_PERMANENT
;
640 if (address
->home_address
)
641 address
->flags
|= IFA_F_HOMEADDRESS
;
643 if (!FLAGS_SET(address
->duplicate_address_detection
, ADDRESS_FAMILY_IPV6
))
644 address
->flags
|= IFA_F_NODAD
;
646 if (address
->manage_temporary_address
)
647 address
->flags
|= IFA_F_MANAGETEMPADDR
;
649 if (!address
->prefix_route
)
650 address
->flags
|= IFA_F_NOPREFIXROUTE
;
652 if (address
->autojoin
)
653 address
->flags
|= IFA_F_MCAUTOJOIN
;
655 r
= sd_rtnl_message_addr_set_flags(req
, (address
->flags
& 0xff));
657 return log_link_error_errno(link
, r
, "Could not set flags: %m");
659 if (address
->flags
& ~0xff) {
660 r
= sd_netlink_message_append_u32(req
, IFA_FLAGS
, address
->flags
);
662 return log_link_error_errno(link
, r
, "Could not set extended flags: %m");
665 r
= sd_rtnl_message_addr_set_scope(req
, address
->scope
);
667 return log_link_error_errno(link
, r
, "Could not set scope: %m");
669 r
= netlink_message_append_in_addr_union(req
, IFA_LOCAL
, address
->family
, &address
->in_addr
);
671 return log_link_error_errno(link
, r
, "Could not append IFA_LOCAL attribute: %m");
673 if (in_addr_is_null(address
->family
, &address
->in_addr_peer
) == 0) {
674 r
= netlink_message_append_in_addr_union(req
, IFA_ADDRESS
, address
->family
, &address
->in_addr_peer
);
676 return log_link_error_errno(link
, r
, "Could not append IFA_ADDRESS attribute: %m");
677 } else if (address
->family
== AF_INET
&& address
->prefixlen
<= 30) {
678 r
= sd_netlink_message_append_in_addr(req
, IFA_BROADCAST
, &address
->broadcast
);
680 return log_link_error_errno(link
, r
, "Could not append IFA_BROADCAST attribute: %m");
683 if (address
->label
) {
684 r
= sd_netlink_message_append_string(req
, IFA_LABEL
, address
->label
);
686 return log_link_error_errno(link
, r
, "Could not append IFA_LABEL attribute: %m");
689 r
= sd_netlink_message_append_cache_info(req
, IFA_CACHEINFO
, &address
->cinfo
);
691 return log_link_error_errno(link
, r
, "Could not append IFA_CACHEINFO attribute: %m");
693 r
= address_establish(address
, link
);
695 log_link_warning_errno(link
, r
, "Could not enable IP masquerading, ignoring: %m");
697 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, callback
, link_netlink_destroy_callback
, link
);
699 address_release(address
);
700 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
705 if (address
->family
== AF_INET6
&& !in_addr_is_null(address
->family
, &address
->in_addr_peer
))
706 r
= address_add(link
, address
->family
, &address
->in_addr_peer
, address
->prefixlen
, NULL
);
708 r
= address_add(link
, address
->family
, &address
->in_addr
, address
->prefixlen
, NULL
);
710 address_release(address
);
711 return log_link_error_errno(link
, r
, "Could not add address: %m");
715 assert(address
->family
== AF_INET
);
717 _cleanup_free_
char *pretty
= NULL
;
719 (void) in_addr_to_string(address
->family
, &address
->in_addr
, &pretty
);
720 log_link_debug(link
, "Starting IPv4ACD client. Probing address %s", strna(pretty
));
723 r
= sd_ipv4acd_start(address
->acd
, true);
725 log_link_warning_errno(link
, r
, "Failed to start IPv4ACD client, ignoring: %m");
731 static void static_address_on_acd(sd_ipv4acd
*acd
, int event
, void *userdata
) {
732 _cleanup_free_
char *pretty
= NULL
;
740 address
= (Address
*) userdata
;
741 link
= address
->link
;
743 (void) in_addr_to_string(address
->family
, &address
->in_addr
, &pretty
);
745 case SD_IPV4ACD_EVENT_STOP
:
746 log_link_debug(link
, "Stopping ACD client...");
749 case SD_IPV4ACD_EVENT_BIND
:
750 log_link_debug(link
, "Successfully claimed address %s", strna(pretty
));
751 link_check_ready(link
);
754 case SD_IPV4ACD_EVENT_CONFLICT
:
755 log_link_warning(link
, "DAD conflict. Dropping address %s", strna(pretty
));
756 r
= address_remove(address
, link
, NULL
);
758 log_link_error_errno(link
, r
, "Failed to drop DAD conflicted address %s", strna(pretty
));;
760 link_check_ready(link
);
764 assert_not_reached("Invalid IPv4ACD event.");
767 sd_ipv4acd_stop(acd
);
772 int configure_ipv4_duplicate_address_detection(Link
*link
, Address
*address
) {
777 assert(address
->family
== AF_INET
);
778 assert(!address
->link
&& address
->network
);
780 address
->link
= link
;
782 r
= sd_ipv4acd_new(&address
->acd
);
786 r
= sd_ipv4acd_attach_event(address
->acd
, NULL
, 0);
790 r
= sd_ipv4acd_set_ifindex(address
->acd
, link
->ifindex
);
794 r
= sd_ipv4acd_set_mac(address
->acd
, &link
->mac
);
798 r
= sd_ipv4acd_set_address(address
->acd
, &address
->in_addr
.in
);
802 r
= sd_ipv4acd_set_callback(address
->acd
, static_address_on_acd
, address
);
809 int config_parse_broadcast(
811 const char *filename
,
814 unsigned section_line
,
821 Network
*network
= userdata
;
822 _cleanup_(address_free_or_set_invalidp
) Address
*n
= NULL
;
831 r
= address_new_static(network
, filename
, section_line
, &n
);
835 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
836 "Failed to allocate new address, ignoring assignment: %m");
840 if (n
->family
== AF_INET6
) {
841 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
842 "Broadcast is not valid for IPv6 addresses, ignoring assignment: %s", rvalue
);
846 r
= in_addr_from_string(AF_INET
, rvalue
, (union in_addr_union
*) &n
->broadcast
);
848 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
849 "Broadcast is invalid, ignoring assignment: %s", rvalue
);
859 int config_parse_address(const char *unit
,
860 const char *filename
,
863 unsigned section_line
,
870 Network
*network
= userdata
;
871 _cleanup_(address_free_or_set_invalidp
) Address
*n
= NULL
;
872 union in_addr_union buffer
;
873 unsigned char prefixlen
;
882 if (streq(section
, "Network")) {
883 /* we are not in an Address section, so treat
884 * this as the special '0' section */
885 r
= address_new_static(network
, NULL
, 0, &n
);
887 r
= address_new_static(network
, filename
, section_line
, &n
);
891 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
892 "Failed to allocate new address, ignoring assignment: %m");
896 /* Address=address/prefixlen */
897 r
= in_addr_prefix_from_string_auto_internal(rvalue
, PREFIXLEN_REFUSE
, &f
, &buffer
, &prefixlen
);
899 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
900 "An address '%s' is specified without prefix length. "
901 "The behavior of parsing addresses without prefix length will be changed in the future release. "
902 "Please specify prefix length explicitly.", rvalue
);
904 r
= in_addr_prefix_from_string_auto_internal(rvalue
, PREFIXLEN_LEGACY
, &f
, &buffer
, &prefixlen
);
907 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Invalid address '%s', ignoring assignment: %m", rvalue
);
911 if (n
->family
!= AF_UNSPEC
&& f
!= n
->family
) {
912 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "Address is incompatible, ignoring assignment: %s", rvalue
);
916 if (in_addr_is_null(f
, &buffer
)) {
917 /* Will use address from address pool. Note that for ipv6 case, prefix of the address
918 * pool is 8, but 40 bit is used by the global ID and 16 bit by the subnet ID. So,
919 * let's limit the prefix length to 64 or larger. See RFC4193. */
920 if ((f
== AF_INET
&& prefixlen
< 8) ||
921 (f
== AF_INET6
&& prefixlen
< 64)) {
922 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
923 "Null address with invalid prefixlen='%u', ignoring assignment: %s",
930 n
->prefixlen
= prefixlen
;
932 if (streq(lvalue
, "Address"))
935 n
->in_addr_peer
= buffer
;
937 if (n
->family
== AF_INET
&& n
->broadcast
.s_addr
== 0 && n
->prefixlen
<= 30)
938 n
->broadcast
.s_addr
= n
->in_addr
.in
.s_addr
| htobe32(0xfffffffflu
>> n
->prefixlen
);
945 int config_parse_label(
947 const char *filename
,
950 unsigned section_line
,
957 _cleanup_(address_free_or_set_invalidp
) Address
*n
= NULL
;
958 Network
*network
= userdata
;
967 r
= address_new_static(network
, filename
, section_line
, &n
);
971 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
972 "Failed to allocate new address, ignoring assignment: %m");
976 if (!address_label_valid(rvalue
)) {
977 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
978 "Interface label is too long or invalid, ignoring assignment: %s", rvalue
);
982 r
= free_and_strdup(&n
->label
, rvalue
);
990 int config_parse_lifetime(const char *unit
,
991 const char *filename
,
994 unsigned section_line
,
1000 Network
*network
= userdata
;
1001 _cleanup_(address_free_or_set_invalidp
) Address
*n
= NULL
;
1011 r
= address_new_static(network
, filename
, section_line
, &n
);
1015 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1016 "Failed to allocate new address, ignoring assignment: %m");
1020 /* We accept only "forever", "infinity", empty, or "0". */
1021 if (STR_IN_SET(rvalue
, "forever", "infinity", ""))
1022 k
= CACHE_INFO_INFINITY_LIFE_TIME
;
1023 else if (streq(rvalue
, "0"))
1026 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1027 "Invalid PreferredLifetime= value, ignoring: %s", rvalue
);
1031 n
->cinfo
.ifa_prefered
= k
;
1037 int config_parse_address_flags(const char *unit
,
1038 const char *filename
,
1040 const char *section
,
1041 unsigned section_line
,
1047 Network
*network
= userdata
;
1048 _cleanup_(address_free_or_set_invalidp
) Address
*n
= NULL
;
1057 r
= address_new_static(network
, filename
, section_line
, &n
);
1061 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1062 "Failed to allocate new address, ignoring assignment: %m");
1066 r
= parse_boolean(rvalue
);
1068 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1069 "Failed to parse %s=, ignoring: %s", lvalue
, rvalue
);
1073 if (streq(lvalue
, "HomeAddress"))
1074 n
->home_address
= r
;
1075 else if (streq(lvalue
, "ManageTemporaryAddress"))
1076 n
->manage_temporary_address
= r
;
1077 else if (streq(lvalue
, "PrefixRoute"))
1078 n
->prefix_route
= !r
;
1079 else if (streq(lvalue
, "AddPrefixRoute"))
1080 n
->prefix_route
= r
;
1081 else if (streq(lvalue
, "AutoJoin"))
1084 assert_not_reached("Invalid address flag type.");
1090 int config_parse_address_scope(const char *unit
,
1091 const char *filename
,
1093 const char *section
,
1094 unsigned section_line
,
1100 Network
*network
= userdata
;
1101 _cleanup_(address_free_or_set_invalidp
) Address
*n
= NULL
;
1110 r
= address_new_static(network
, filename
, section_line
, &n
);
1114 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1115 "Failed to allocate new address, ignoring assignment: %m");
1119 if (streq(rvalue
, "host"))
1120 n
->scope
= RT_SCOPE_HOST
;
1121 else if (streq(rvalue
, "link"))
1122 n
->scope
= RT_SCOPE_LINK
;
1123 else if (streq(rvalue
, "global"))
1124 n
->scope
= RT_SCOPE_UNIVERSE
;
1126 r
= safe_atou8(rvalue
, &n
->scope
);
1128 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1129 "Could not parse address scope \"%s\", ignoring assignment: %m", rvalue
);
1134 n
->scope_set
= true;
1139 int config_parse_duplicate_address_detection(
1141 const char *filename
,
1143 const char *section
,
1144 unsigned section_line
,
1150 Network
*network
= userdata
;
1151 _cleanup_(address_free_or_set_invalidp
) Address
*n
= NULL
;
1161 r
= address_new_static(network
, filename
, section_line
, &n
);
1165 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1166 "Failed to allocate new address, ignoring assignment: %m");
1170 r
= parse_boolean(rvalue
);
1172 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1173 "For historical reasons, %s=%s means %s=%s. "
1174 "Please use 'both', 'ipv4', 'ipv6' or 'none' instead.",
1175 lvalue
, rvalue
, lvalue
, r
? "none" : "both");
1176 n
->duplicate_address_detection
= r
? ADDRESS_FAMILY_NO
: ADDRESS_FAMILY_YES
;
1181 a
= duplicate_address_detection_address_family_from_string(rvalue
);
1183 log_syntax(unit
, LOG_WARNING
, filename
, line
, SYNTHETIC_ERRNO(EINVAL
),
1184 "Failed to parse %s=, ignoring: %s", lvalue
, rvalue
);
1188 n
->duplicate_address_detection
= a
;
1193 bool address_is_ready(const Address
*a
) {
1196 return !(a
->flags
& IFA_F_TENTATIVE
);
1199 int address_section_verify(Address
*address
) {
1200 if (section_is_invalid(address
->section
))
1203 if (address
->family
== AF_UNSPEC
) {
1204 assert(address
->section
);
1206 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1207 "%s: Address section without Address= field configured. "
1208 "Ignoring [Address] section from line %u.",
1209 address
->section
->filename
, address
->section
->line
);
1212 if (!address
->scope_set
&& in_addr_is_localhost(address
->family
, &address
->in_addr
) > 0)
1213 address
->scope
= RT_SCOPE_HOST
;