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(address_hash_ops
, Address
, address_hash_func
, address_compare_func
);
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
);
363 address_is_ready(address
) &&
364 address
->family
== AF_INET6
&&
365 in_addr_is_link_local(AF_INET6
, &address
->in_addr
) > 0 &&
366 in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &address
->link
->ipv6ll_address
) > 0) {
368 r
= link_ipv6ll_gained(address
->link
, &address
->in_addr
.in6
);
376 int address_drop(Address
*address
) {
383 ready
= address_is_ready(address
);
384 link
= address
->link
;
386 r
= address_release(address
);
388 log_link_warning_errno(link
, r
, "Failed to disable IP masquerading, ignoring: %m");
390 address_free(address
);
392 link_update_operstate(link
, true);
395 link_check_ready(link
);
400 int address_get(Link
*link
,
402 const union in_addr_union
*in_addr
,
403 unsigned char prefixlen
,
406 Address address
, *existing
;
411 address
= (Address
) {
414 .prefixlen
= prefixlen
,
417 existing
= set_get(link
->addresses
, &address
);
424 existing
= set_get(link
->addresses_foreign
, &address
);
434 static int address_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
439 assert(link
->ifname
);
441 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
444 r
= sd_netlink_message_get_errno(m
);
445 if (r
< 0 && r
!= -EADDRNOTAVAIL
)
446 log_link_message_warning_errno(link
, m
, r
, "Could not drop address");
448 (void) manager_rtnl_process_address(rtnl
, m
, link
->manager
);
456 link_netlink_message_handler_t callback
) {
458 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
462 assert(IN_SET(address
->family
, AF_INET
, AF_INET6
));
464 assert(link
->ifindex
> 0);
465 assert(link
->manager
);
466 assert(link
->manager
->rtnl
);
469 _cleanup_free_
char *b
= NULL
;
471 (void) in_addr_to_string(address
->family
, &address
->in_addr
, &b
);
472 log_link_debug(link
, "Removing address %s", strna(b
));
475 r
= sd_rtnl_message_new_addr(link
->manager
->rtnl
, &req
, RTM_DELADDR
,
476 link
->ifindex
, address
->family
);
478 return log_link_error_errno(link
, r
, "Could not allocate RTM_DELADDR message: %m");
480 r
= sd_rtnl_message_addr_set_prefixlen(req
, address
->prefixlen
);
482 return log_link_error_errno(link
, r
, "Could not set prefixlen: %m");
484 r
= netlink_message_append_in_addr_union(req
, IFA_LOCAL
, address
->family
, &address
->in_addr
);
486 return log_link_error_errno(link
, r
, "Could not append IFA_LOCAL attribute: %m");
488 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
,
489 callback
?: address_remove_handler
,
490 link_netlink_destroy_callback
, link
);
492 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
499 static int address_acquire(Link
*link
, Address
*original
, Address
**ret
) {
500 union in_addr_union in_addr
= IN_ADDR_NULL
;
501 struct in_addr broadcast
= {};
502 _cleanup_(address_freep
) Address
*na
= NULL
;
509 /* Something useful was configured? just use it */
510 r
= in_addr_is_null(original
->family
, &original
->in_addr
);
514 /* The address is configured to be 0.0.0.0 or [::] by the user?
515 * Then let's acquire something more useful from the pool. */
516 r
= manager_address_pool_acquire(link
->manager
, original
->family
, original
->prefixlen
, &in_addr
);
522 if (original
->family
== AF_INET
) {
523 /* Pick first address in range for ourselves ... */
524 in_addr
.in
.s_addr
= in_addr
.in
.s_addr
| htobe32(1);
526 /* .. and use last as broadcast address */
527 if (original
->prefixlen
> 30)
528 broadcast
.s_addr
= 0;
530 broadcast
.s_addr
= in_addr
.in
.s_addr
| htobe32(0xFFFFFFFFUL
>> original
->prefixlen
);
531 } else if (original
->family
== AF_INET6
)
532 in_addr
.in6
.s6_addr
[15] |= 1;
534 r
= address_new(&na
);
538 na
->family
= original
->family
;
539 na
->prefixlen
= original
->prefixlen
;
540 na
->scope
= original
->scope
;
541 na
->cinfo
= original
->cinfo
;
543 if (original
->label
) {
544 na
->label
= strdup(original
->label
);
549 na
->broadcast
= broadcast
;
550 na
->in_addr
= in_addr
;
552 LIST_PREPEND(addresses
, link
->pool_addresses
, na
);
559 int address_configure(
562 link_netlink_message_handler_t callback
,
565 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
569 assert(IN_SET(address
->family
, AF_INET
, AF_INET6
));
571 assert(link
->ifindex
> 0);
572 assert(link
->manager
);
573 assert(link
->manager
->rtnl
);
576 /* If this is a new address, then refuse adding more than the limit */
577 if (address_get(link
, address
->family
, &address
->in_addr
, address
->prefixlen
, NULL
) <= 0 &&
578 set_size(link
->addresses
) >= ADDRESSES_PER_LINK_MAX
)
579 return log_link_error_errno(link
, SYNTHETIC_ERRNO(E2BIG
),
580 "Too many addresses are configured, refusing: %m");
582 r
= address_acquire(link
, address
, &address
);
584 return log_link_error_errno(link
, r
, "Failed to acquire an address from pool: %m");
587 r
= sd_rtnl_message_new_addr_update(link
->manager
->rtnl
, &req
,
588 link
->ifindex
, address
->family
);
590 r
= sd_rtnl_message_new_addr(link
->manager
->rtnl
, &req
, RTM_NEWADDR
,
591 link
->ifindex
, address
->family
);
593 return log_link_error_errno(link
, r
, "Could not allocate RTM_NEWADDR message: %m");
595 r
= sd_rtnl_message_addr_set_prefixlen(req
, address
->prefixlen
);
597 return log_link_error_errno(link
, r
, "Could not set prefixlen: %m");
599 address
->flags
|= IFA_F_PERMANENT
;
601 if (address
->home_address
)
602 address
->flags
|= IFA_F_HOMEADDRESS
;
604 if (!FLAGS_SET(address
->duplicate_address_detection
, ADDRESS_FAMILY_IPV6
))
605 address
->flags
|= IFA_F_NODAD
;
607 if (address
->manage_temporary_address
)
608 address
->flags
|= IFA_F_MANAGETEMPADDR
;
610 if (!address
->prefix_route
)
611 address
->flags
|= IFA_F_NOPREFIXROUTE
;
613 if (address
->autojoin
)
614 address
->flags
|= IFA_F_MCAUTOJOIN
;
616 r
= sd_rtnl_message_addr_set_flags(req
, (address
->flags
& 0xff));
618 return log_link_error_errno(link
, r
, "Could not set flags: %m");
620 if (address
->flags
& ~0xff) {
621 r
= sd_netlink_message_append_u32(req
, IFA_FLAGS
, address
->flags
);
623 return log_link_error_errno(link
, r
, "Could not set extended flags: %m");
626 r
= sd_rtnl_message_addr_set_scope(req
, address
->scope
);
628 return log_link_error_errno(link
, r
, "Could not set scope: %m");
630 r
= netlink_message_append_in_addr_union(req
, IFA_LOCAL
, address
->family
, &address
->in_addr
);
632 return log_link_error_errno(link
, r
, "Could not append IFA_LOCAL attribute: %m");
634 if (in_addr_is_null(address
->family
, &address
->in_addr_peer
) == 0) {
635 r
= netlink_message_append_in_addr_union(req
, IFA_ADDRESS
, address
->family
, &address
->in_addr_peer
);
637 return log_link_error_errno(link
, r
, "Could not append IFA_ADDRESS attribute: %m");
638 } else if (address
->family
== AF_INET
&& address
->prefixlen
<= 30) {
639 r
= sd_netlink_message_append_in_addr(req
, IFA_BROADCAST
, &address
->broadcast
);
641 return log_link_error_errno(link
, r
, "Could not append IFA_BROADCAST attribute: %m");
644 if (address
->label
) {
645 r
= sd_netlink_message_append_string(req
, IFA_LABEL
, address
->label
);
647 return log_link_error_errno(link
, r
, "Could not append IFA_LABEL attribute: %m");
650 r
= sd_netlink_message_append_cache_info(req
, IFA_CACHEINFO
, &address
->cinfo
);
652 return log_link_error_errno(link
, r
, "Could not append IFA_CACHEINFO attribute: %m");
654 r
= address_establish(address
, link
);
656 log_link_warning_errno(link
, r
, "Could not enable IP masquerading, ignoring: %m");
658 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, callback
, link_netlink_destroy_callback
, link
);
660 address_release(address
);
661 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
666 if (address
->family
== AF_INET6
&& !in_addr_is_null(address
->family
, &address
->in_addr_peer
))
667 r
= address_add(link
, address
->family
, &address
->in_addr_peer
, address
->prefixlen
, NULL
);
669 r
= address_add(link
, address
->family
, &address
->in_addr
, address
->prefixlen
, NULL
);
671 address_release(address
);
672 return log_link_error_errno(link
, r
, "Could not add address: %m");
676 assert(address
->family
== AF_INET
);
678 _cleanup_free_
char *pretty
= NULL
;
680 (void) in_addr_to_string(address
->family
, &address
->in_addr
, &pretty
);
681 log_link_debug(link
, "Starting IPv4ACD client. Probing address %s", strna(pretty
));
684 r
= sd_ipv4acd_start(address
->acd
, true);
686 log_link_warning_errno(link
, r
, "Failed to start IPv4ACD client, ignoring: %m");
692 static void static_address_on_acd(sd_ipv4acd
*acd
, int event
, void *userdata
) {
693 _cleanup_free_
char *pretty
= NULL
;
701 address
= (Address
*) userdata
;
702 link
= address
->link
;
704 (void) in_addr_to_string(address
->family
, &address
->in_addr
, &pretty
);
706 case SD_IPV4ACD_EVENT_STOP
:
707 log_link_debug(link
, "Stopping ACD client...");
710 case SD_IPV4ACD_EVENT_BIND
:
711 log_link_debug(link
, "Successfully claimed address %s", strna(pretty
));
712 link_check_ready(link
);
715 case SD_IPV4ACD_EVENT_CONFLICT
:
716 log_link_warning(link
, "DAD conflict. Dropping address %s", strna(pretty
));
717 r
= address_remove(address
, link
, NULL
);
719 log_link_error_errno(link
, r
, "Failed to drop DAD conflicted address %s", strna(pretty
));;
721 link_check_ready(link
);
725 assert_not_reached("Invalid IPv4ACD event.");
728 sd_ipv4acd_stop(acd
);
733 int configure_ipv4_duplicate_address_detection(Link
*link
, Address
*address
) {
738 assert(address
->family
== AF_INET
);
739 assert(!address
->link
&& address
->network
);
741 address
->link
= link
;
743 r
= sd_ipv4acd_new(&address
->acd
);
747 r
= sd_ipv4acd_attach_event(address
->acd
, NULL
, 0);
751 r
= sd_ipv4acd_set_ifindex(address
->acd
, link
->ifindex
);
755 r
= sd_ipv4acd_set_mac(address
->acd
, &link
->mac
);
759 r
= sd_ipv4acd_set_address(address
->acd
, &address
->in_addr
.in
);
763 r
= sd_ipv4acd_set_callback(address
->acd
, static_address_on_acd
, address
);
770 int config_parse_broadcast(
772 const char *filename
,
775 unsigned section_line
,
782 Network
*network
= userdata
;
783 _cleanup_(address_free_or_set_invalidp
) Address
*n
= NULL
;
792 r
= address_new_static(network
, filename
, section_line
, &n
);
796 if (n
->family
== AF_INET6
) {
797 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
798 "Broadcast is not valid for IPv6 addresses, ignoring assignment: %s", rvalue
);
802 r
= in_addr_from_string(AF_INET
, rvalue
, (union in_addr_union
*) &n
->broadcast
);
804 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
805 "Broadcast is invalid, ignoring assignment: %s", rvalue
);
815 int config_parse_address(const char *unit
,
816 const char *filename
,
819 unsigned section_line
,
826 Network
*network
= userdata
;
827 _cleanup_(address_free_or_set_invalidp
) Address
*n
= NULL
;
828 union in_addr_union buffer
;
829 unsigned char prefixlen
;
838 if (streq(section
, "Network")) {
839 /* we are not in an Address section, so treat
840 * this as the special '0' section */
841 r
= address_new_static(network
, NULL
, 0, &n
);
843 r
= address_new_static(network
, filename
, section_line
, &n
);
848 /* Address=address/prefixlen */
849 r
= in_addr_prefix_from_string_auto_internal(rvalue
, PREFIXLEN_REFUSE
, &f
, &buffer
, &prefixlen
);
851 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
852 "An address '%s' is specified without prefix length. "
853 "The behavior of parsing addresses without prefix length will be changed in the future release. "
854 "Please specify prefix length explicitly.", rvalue
);
856 r
= in_addr_prefix_from_string_auto_internal(rvalue
, PREFIXLEN_LEGACY
, &f
, &buffer
, &prefixlen
);
859 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Invalid address '%s', ignoring assignment: %m", rvalue
);
863 if (n
->family
!= AF_UNSPEC
&& f
!= n
->family
) {
864 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Address is incompatible, ignoring assignment: %s", rvalue
);
868 if (in_addr_is_null(f
, &buffer
)) {
869 /* Will use address from address pool. Note that for ipv6 case, prefix of the address
870 * pool is 8, but 40 bit is used by the global ID and 16 bit by the subnet ID. So,
871 * let's limit the prefix length to 64 or larger. See RFC4193. */
872 if ((f
== AF_INET
&& prefixlen
< 8) ||
873 (f
== AF_INET6
&& prefixlen
< 64)) {
874 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
875 "Null address with invalid prefixlen='%u', ignoring assignment: %s",
882 n
->prefixlen
= prefixlen
;
884 if (streq(lvalue
, "Address"))
887 n
->in_addr_peer
= buffer
;
889 if (n
->family
== AF_INET
&& n
->broadcast
.s_addr
== 0 && n
->prefixlen
<= 30)
890 n
->broadcast
.s_addr
= n
->in_addr
.in
.s_addr
| htobe32(0xfffffffflu
>> n
->prefixlen
);
897 int config_parse_label(
899 const char *filename
,
902 unsigned section_line
,
909 _cleanup_(address_free_or_set_invalidp
) Address
*n
= NULL
;
910 Network
*network
= userdata
;
919 r
= address_new_static(network
, filename
, section_line
, &n
);
923 if (!address_label_valid(rvalue
)) {
924 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
925 "Interface label is too long or invalid, ignoring assignment: %s", rvalue
);
929 r
= free_and_strdup(&n
->label
, rvalue
);
937 int config_parse_lifetime(const char *unit
,
938 const char *filename
,
941 unsigned section_line
,
947 Network
*network
= userdata
;
948 _cleanup_(address_free_or_set_invalidp
) Address
*n
= NULL
;
958 r
= address_new_static(network
, filename
, section_line
, &n
);
962 /* We accept only "forever", "infinity", empty, or "0". */
963 if (STR_IN_SET(rvalue
, "forever", "infinity", ""))
964 k
= CACHE_INFO_INFINITY_LIFE_TIME
;
965 else if (streq(rvalue
, "0"))
968 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
969 "Invalid PreferredLifetime= value, ignoring: %s", rvalue
);
973 n
->cinfo
.ifa_prefered
= k
;
979 int config_parse_address_flags(const char *unit
,
980 const char *filename
,
983 unsigned section_line
,
989 Network
*network
= userdata
;
990 _cleanup_(address_free_or_set_invalidp
) Address
*n
= NULL
;
999 r
= address_new_static(network
, filename
, section_line
, &n
);
1003 r
= parse_boolean(rvalue
);
1005 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1006 "Failed to parse %s=, ignoring: %s", lvalue
, rvalue
);
1010 if (streq(lvalue
, "HomeAddress"))
1011 n
->home_address
= r
;
1012 else if (streq(lvalue
, "ManageTemporaryAddress"))
1013 n
->manage_temporary_address
= r
;
1014 else if (streq(lvalue
, "PrefixRoute"))
1015 n
->prefix_route
= !r
;
1016 else if (streq(lvalue
, "AddPrefixRoute"))
1017 n
->prefix_route
= r
;
1018 else if (streq(lvalue
, "AutoJoin"))
1021 assert_not_reached("Invalid address flag type.");
1027 int config_parse_address_scope(const char *unit
,
1028 const char *filename
,
1030 const char *section
,
1031 unsigned section_line
,
1037 Network
*network
= userdata
;
1038 _cleanup_(address_free_or_set_invalidp
) Address
*n
= NULL
;
1047 r
= address_new_static(network
, filename
, section_line
, &n
);
1051 if (streq(rvalue
, "host"))
1052 n
->scope
= RT_SCOPE_HOST
;
1053 else if (streq(rvalue
, "link"))
1054 n
->scope
= RT_SCOPE_LINK
;
1055 else if (streq(rvalue
, "global"))
1056 n
->scope
= RT_SCOPE_UNIVERSE
;
1058 r
= safe_atou8(rvalue
, &n
->scope
);
1060 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1061 "Could not parse address scope \"%s\", ignoring assignment: %m", rvalue
);
1066 n
->scope_set
= true;
1071 int config_parse_duplicate_address_detection(
1073 const char *filename
,
1075 const char *section
,
1076 unsigned section_line
,
1082 Network
*network
= userdata
;
1083 _cleanup_(address_free_or_set_invalidp
) Address
*n
= NULL
;
1093 r
= address_new_static(network
, filename
, section_line
, &n
);
1097 r
= parse_boolean(rvalue
);
1099 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1100 "For historical reasons, %s=%s means %s=%s. "
1101 "Please use 'both', 'ipv4', 'ipv6' or 'none' instead.",
1102 lvalue
, rvalue
, lvalue
, r
? "none" : "both");
1103 n
->duplicate_address_detection
= r
? ADDRESS_FAMILY_NO
: ADDRESS_FAMILY_YES
;
1108 a
= duplicate_address_detection_address_family_from_string(rvalue
);
1110 log_syntax(unit
, LOG_ERR
, filename
, line
, SYNTHETIC_ERRNO(EINVAL
),
1111 "Failed to parse %s=, ignoring: %s", lvalue
, rvalue
);
1115 n
->duplicate_address_detection
= a
;
1120 bool address_is_ready(const Address
*a
) {
1123 return !(a
->flags
& IFA_F_TENTATIVE
);
1126 int address_section_verify(Address
*address
) {
1127 if (section_is_invalid(address
->section
))
1130 if (address
->family
== AF_UNSPEC
) {
1131 assert(address
->section
);
1133 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1134 "%s: Address section without Address= field configured. "
1135 "Ignoring [Address] section from line %u.",
1136 address
->section
->filename
, address
->section
->line
);
1139 if (!address
->scope_set
&& in_addr_is_localhost(address
->family
, &address
->in_addr
) > 0)
1140 address
->scope
= RT_SCOPE_HOST
;