1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 #include "alloc-util.h"
6 #include "conf-parser.h"
7 #include "firewall-util.h"
8 #include "netlink-util.h"
9 #include "networkd-address.h"
10 #include "networkd-manager.h"
11 #include "parse-util.h"
13 #include "socket-util.h"
14 #include "string-util.h"
19 #define ADDRESSES_PER_LINK_MAX 2048U
20 #define STATIC_ADDRESSES_PER_NETWORK_MAX 1024U
22 int address_new(Address
**ret
) {
23 _cleanup_(address_freep
) Address
*address
= NULL
;
25 address
= new(Address
, 1);
29 *address
= (Address
) {
31 .scope
= RT_SCOPE_UNIVERSE
,
32 .cinfo
.ifa_prefered
= CACHE_INFO_INFINITY_LIFE_TIME
,
33 .cinfo
.ifa_valid
= CACHE_INFO_INFINITY_LIFE_TIME
,
36 *ret
= TAKE_PTR(address
);
41 int address_new_static(Network
*network
, const char *filename
, unsigned section_line
, Address
**ret
) {
42 _cleanup_(network_config_section_freep
) NetworkConfigSection
*n
= NULL
;
43 _cleanup_(address_freep
) Address
*address
= NULL
;
48 assert(!!filename
== (section_line
> 0));
51 r
= network_config_section_new(filename
, section_line
, &n
);
55 address
= hashmap_get(network
->addresses_by_section
, n
);
57 *ret
= TAKE_PTR(address
);
63 if (network
->n_static_addresses
>= STATIC_ADDRESSES_PER_NETWORK_MAX
)
66 r
= address_new(&address
);
70 address
->network
= network
;
71 LIST_APPEND(addresses
, network
->static_addresses
, address
);
72 network
->n_static_addresses
++;
75 address
->section
= TAKE_PTR(n
);
77 r
= hashmap_ensure_allocated(&network
->addresses_by_section
, &network_config_hash_ops
);
81 r
= hashmap_put(network
->addresses_by_section
, address
->section
, address
);
86 *ret
= TAKE_PTR(address
);
91 void address_free(Address
*address
) {
95 if (address
->network
) {
96 LIST_REMOVE(addresses
, address
->network
->static_addresses
, address
);
97 assert(address
->network
->n_static_addresses
> 0);
98 address
->network
->n_static_addresses
--;
100 if (address
->section
)
101 hashmap_remove(address
->network
->addresses_by_section
, address
->section
);
105 set_remove(address
->link
->addresses
, address
);
106 set_remove(address
->link
->addresses_foreign
, address
);
108 if (in_addr_equal(AF_INET6
, &address
->in_addr
, (const union in_addr_union
*) &address
->link
->ipv6ll_address
))
109 memzero(&address
->link
->ipv6ll_address
, sizeof(struct in6_addr
));
112 network_config_section_free(address
->section
);
113 free(address
->label
);
117 static void address_hash_func(const void *b
, struct siphash
*state
) {
118 const Address
*a
= b
;
122 siphash24_compress(&a
->family
, sizeof(a
->family
), state
);
126 siphash24_compress(&a
->prefixlen
, sizeof(a
->prefixlen
), state
);
129 if (a
->prefixlen
!= 0) {
132 if (a
->in_addr_peer
.in
.s_addr
!= 0)
133 prefix
= be32toh(a
->in_addr_peer
.in
.s_addr
) >> (32 - a
->prefixlen
);
135 prefix
= be32toh(a
->in_addr
.in
.s_addr
) >> (32 - a
->prefixlen
);
137 siphash24_compress(&prefix
, sizeof(prefix
), state
);
143 siphash24_compress(&a
->in_addr
, FAMILY_ADDRESS_SIZE(a
->family
), state
);
147 /* treat any other address family as AF_UNSPEC */
152 static int address_compare_func(const void *c1
, const void *c2
) {
153 const Address
*a1
= c1
, *a2
= c2
;
156 r
= CMP(a1
->family
, a2
->family
);
160 switch (a1
->family
) {
161 /* use the same notion of equality as the kernel does */
163 r
= CMP(a1
->prefixlen
, a2
->prefixlen
);
167 /* compare the peer prefixes */
168 if (a1
->prefixlen
!= 0) {
169 /* make sure we don't try to shift by 32.
170 * See ISO/IEC 9899:TC3 ยง 6.5.7.3. */
173 if (a1
->in_addr_peer
.in
.s_addr
!= 0)
174 b1
= be32toh(a1
->in_addr_peer
.in
.s_addr
) >> (32 - a1
->prefixlen
);
176 b1
= be32toh(a1
->in_addr
.in
.s_addr
) >> (32 - a1
->prefixlen
);
178 if (a2
->in_addr_peer
.in
.s_addr
!= 0)
179 b2
= be32toh(a2
->in_addr_peer
.in
.s_addr
) >> (32 - a1
->prefixlen
);
181 b2
= be32toh(a2
->in_addr
.in
.s_addr
) >> (32 - a1
->prefixlen
);
190 return memcmp(&a1
->in_addr
, &a2
->in_addr
, FAMILY_ADDRESS_SIZE(a1
->family
));
192 /* treat any other address family as AF_UNSPEC */
197 static const struct hash_ops address_hash_ops
= {
198 .hash
= address_hash_func
,
199 .compare
= address_compare_func
202 bool address_equal(Address
*a1
, Address
*a2
) {
209 return address_compare_func(a1
, a2
) == 0;
212 static int address_establish(Address
*address
, Link
*link
) {
219 masq
= link
->network
&&
220 link
->network
->ip_masquerade
&&
221 address
->family
== AF_INET
&&
222 address
->scope
< RT_SCOPE_LINK
;
224 /* Add firewall entry if this is requested */
225 if (address
->ip_masquerade_done
!= masq
) {
226 union in_addr_union masked
= address
->in_addr
;
227 in_addr_mask(address
->family
, &masked
, address
->prefixlen
);
229 r
= fw_add_masquerade(masq
, AF_INET
, 0, &masked
, address
->prefixlen
, NULL
, NULL
, 0);
231 log_link_warning_errno(link
, r
, "Could not enable IP masquerading: %m");
233 address
->ip_masquerade_done
= masq
;
239 static int address_add_internal(Link
*link
, Set
**addresses
,
241 const union in_addr_union
*in_addr
,
242 unsigned char prefixlen
,
244 _cleanup_(address_freep
) Address
*address
= NULL
;
251 r
= address_new(&address
);
255 address
->family
= family
;
256 address
->in_addr
= *in_addr
;
257 address
->prefixlen
= prefixlen
;
258 /* Consider address tentative until we get the real flags from the kernel */
259 address
->flags
= IFA_F_TENTATIVE
;
261 r
= set_ensure_allocated(addresses
, &address_hash_ops
);
265 r
= set_put(*addresses
, address
);
269 address
->link
= link
;
279 int address_add_foreign(Link
*link
, int family
, const union in_addr_union
*in_addr
, unsigned char prefixlen
, Address
**ret
) {
280 return address_add_internal(link
, &link
->addresses_foreign
, family
, in_addr
, prefixlen
, ret
);
283 int address_add(Link
*link
, int family
, const union in_addr_union
*in_addr
, unsigned char prefixlen
, Address
**ret
) {
287 r
= address_get(link
, family
, in_addr
, prefixlen
, &address
);
289 /* Address does not exist, create a new one */
290 r
= address_add_internal(link
, &link
->addresses
, family
, in_addr
, prefixlen
, &address
);
294 /* Take over a foreign address */
295 r
= set_ensure_allocated(&link
->addresses
, &address_hash_ops
);
299 r
= set_put(link
->addresses
, 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);
329 log_link_warning_errno(address
->link
, r
, "Failed to disable IP masquerading: %m");
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
);
361 if (!ready
&& address_is_ready(address
)) {
362 link_check_ready(address
->link
);
364 if (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
);
377 int address_drop(Address
*address
) {
383 ready
= address_is_ready(address
);
384 link
= address
->link
;
386 address_release(address
);
387 address_free(address
);
389 link_update_operstate(link
);
392 link_check_ready(link
);
397 int address_get(Link
*link
,
399 const union in_addr_union
*in_addr
,
400 unsigned char prefixlen
,
403 Address address
, *existing
;
408 address
= (Address
) {
411 .prefixlen
= prefixlen
,
414 existing
= set_get(link
->addresses
, &address
);
421 existing
= set_get(link
->addresses_foreign
, &address
);
434 sd_netlink_message_handler_t callback
) {
436 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
437 _cleanup_free_
char *b
= NULL
;
441 assert(IN_SET(address
->family
, AF_INET
, AF_INET6
));
443 assert(link
->ifindex
> 0);
444 assert(link
->manager
);
445 assert(link
->manager
->rtnl
);
448 if (in_addr_to_string(address
->family
, &address
->in_addr
, &b
) >= 0)
449 log_link_debug(link
, "Removing address %s", b
);
452 r
= sd_rtnl_message_new_addr(link
->manager
->rtnl
, &req
, RTM_DELADDR
,
453 link
->ifindex
, address
->family
);
455 return log_error_errno(r
, "Could not allocate RTM_DELADDR message: %m");
457 r
= sd_rtnl_message_addr_set_prefixlen(req
, address
->prefixlen
);
459 return log_error_errno(r
, "Could not set prefixlen: %m");
461 if (address
->family
== AF_INET
)
462 r
= sd_netlink_message_append_in_addr(req
, IFA_LOCAL
, &address
->in_addr
.in
);
463 else if (address
->family
== AF_INET6
)
464 r
= sd_netlink_message_append_in6_addr(req
, IFA_LOCAL
, &address
->in_addr
.in6
);
466 return log_error_errno(r
, "Could not append IFA_LOCAL attribute: %m");
468 r
= sd_netlink_call_async(link
->manager
->rtnl
, NULL
, req
, callback
,
469 link_netlink_destroy_callback
, link
, 0, __func__
);
471 return log_error_errno(r
, "Could not send rtnetlink message: %m");
478 static int address_acquire(Link
*link
, Address
*original
, Address
**ret
) {
479 union in_addr_union in_addr
= {};
480 struct in_addr broadcast
= {};
481 _cleanup_(address_freep
) Address
*na
= NULL
;
488 /* Something useful was configured? just use it */
489 if (in_addr_is_null(original
->family
, &original
->in_addr
) <= 0)
492 /* The address is configured to be 0.0.0.0 or [::] by the user?
493 * Then let's acquire something more useful from the pool. */
494 r
= manager_address_pool_acquire(link
->manager
, original
->family
, original
->prefixlen
, &in_addr
);
496 return log_link_error_errno(link
, r
, "Failed to acquire address from pool: %m");
498 log_link_error(link
, "Couldn't find free address for interface, all taken.");
502 if (original
->family
== AF_INET
) {
503 /* Pick first address in range for ourselves ... */
504 in_addr
.in
.s_addr
= in_addr
.in
.s_addr
| htobe32(1);
506 /* .. and use last as broadcast address */
507 if (original
->prefixlen
> 30)
508 broadcast
.s_addr
= 0;
510 broadcast
.s_addr
= in_addr
.in
.s_addr
| htobe32(0xFFFFFFFFUL
>> original
->prefixlen
);
511 } else if (original
->family
== AF_INET6
)
512 in_addr
.in6
.s6_addr
[15] |= 1;
514 r
= address_new(&na
);
518 na
->family
= original
->family
;
519 na
->prefixlen
= original
->prefixlen
;
520 na
->scope
= original
->scope
;
521 na
->cinfo
= original
->cinfo
;
523 if (original
->label
) {
524 na
->label
= strdup(original
->label
);
529 na
->broadcast
= broadcast
;
530 na
->in_addr
= in_addr
;
532 LIST_PREPEND(addresses
, link
->pool_addresses
, na
);
539 int address_configure(
542 sd_netlink_message_handler_t callback
,
545 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
549 assert(IN_SET(address
->family
, AF_INET
, AF_INET6
));
551 assert(link
->ifindex
> 0);
552 assert(link
->manager
);
553 assert(link
->manager
->rtnl
);
555 /* If this is a new address, then refuse adding more than the limit */
556 if (address_get(link
, address
->family
, &address
->in_addr
, address
->prefixlen
, NULL
) <= 0 &&
557 set_size(link
->addresses
) >= ADDRESSES_PER_LINK_MAX
)
560 r
= address_acquire(link
, address
, &address
);
565 r
= sd_rtnl_message_new_addr_update(link
->manager
->rtnl
, &req
,
566 link
->ifindex
, address
->family
);
568 r
= sd_rtnl_message_new_addr(link
->manager
->rtnl
, &req
, RTM_NEWADDR
,
569 link
->ifindex
, address
->family
);
571 return log_error_errno(r
, "Could not allocate RTM_NEWADDR message: %m");
573 r
= sd_rtnl_message_addr_set_prefixlen(req
, address
->prefixlen
);
575 return log_error_errno(r
, "Could not set prefixlen: %m");
577 address
->flags
|= IFA_F_PERMANENT
;
579 if (address
->home_address
)
580 address
->flags
|= IFA_F_HOMEADDRESS
;
582 if (address
->duplicate_address_detection
)
583 address
->flags
|= IFA_F_NODAD
;
585 if (address
->manage_temporary_address
)
586 address
->flags
|= IFA_F_MANAGETEMPADDR
;
588 if (address
->prefix_route
)
589 address
->flags
|= IFA_F_NOPREFIXROUTE
;
591 if (address
->autojoin
)
592 address
->flags
|= IFA_F_MCAUTOJOIN
;
594 r
= sd_rtnl_message_addr_set_flags(req
, (address
->flags
& 0xff));
596 return log_error_errno(r
, "Could not set flags: %m");
598 if (address
->flags
& ~0xff) {
599 r
= sd_netlink_message_append_u32(req
, IFA_FLAGS
, address
->flags
);
601 return log_error_errno(r
, "Could not set extended flags: %m");
604 r
= sd_rtnl_message_addr_set_scope(req
, address
->scope
);
606 return log_error_errno(r
, "Could not set scope: %m");
608 if (address
->family
== AF_INET
)
609 r
= sd_netlink_message_append_in_addr(req
, IFA_LOCAL
, &address
->in_addr
.in
);
610 else if (address
->family
== AF_INET6
)
611 r
= sd_netlink_message_append_in6_addr(req
, IFA_LOCAL
, &address
->in_addr
.in6
);
613 return log_error_errno(r
, "Could not append IFA_LOCAL attribute: %m");
615 if (!in_addr_is_null(address
->family
, &address
->in_addr_peer
)) {
616 if (address
->family
== AF_INET
)
617 r
= sd_netlink_message_append_in_addr(req
, IFA_ADDRESS
, &address
->in_addr_peer
.in
);
618 else if (address
->family
== AF_INET6
)
619 r
= sd_netlink_message_append_in6_addr(req
, IFA_ADDRESS
, &address
->in_addr_peer
.in6
);
621 return log_error_errno(r
, "Could not append IFA_ADDRESS attribute: %m");
623 if (address
->family
== AF_INET
) {
624 if (address
->prefixlen
<= 30) {
625 r
= sd_netlink_message_append_in_addr(req
, IFA_BROADCAST
, &address
->broadcast
);
627 return log_error_errno(r
, "Could not append IFA_BROADCAST attribute: %m");
632 if (address
->label
) {
633 r
= sd_netlink_message_append_string(req
, IFA_LABEL
, address
->label
);
635 return log_error_errno(r
, "Could not append IFA_LABEL attribute: %m");
638 r
= sd_netlink_message_append_cache_info(req
, IFA_CACHEINFO
,
641 return log_error_errno(r
, "Could not append IFA_CACHEINFO attribute: %m");
643 r
= address_establish(address
, link
);
647 r
= sd_netlink_call_async(link
->manager
->rtnl
, NULL
, req
, callback
,
648 link_netlink_destroy_callback
, link
, 0, __func__
);
650 address_release(address
);
651 return log_error_errno(r
, "Could not send rtnetlink message: %m");
656 r
= address_add(link
, address
->family
, &address
->in_addr
, address
->prefixlen
, NULL
);
658 address_release(address
);
659 return log_error_errno(r
, "Could not add address: %m");
665 int config_parse_broadcast(
667 const char *filename
,
670 unsigned section_line
,
677 Network
*network
= userdata
;
678 _cleanup_(address_freep
) Address
*n
= NULL
;
687 r
= address_new_static(network
, filename
, section_line
, &n
);
691 if (n
->family
== AF_INET6
) {
692 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Broadcast is not valid for IPv6 addresses, ignoring assignment: %s", rvalue
);
696 r
= in_addr_from_string(AF_INET
, rvalue
, (union in_addr_union
*) &n
->broadcast
);
698 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Broadcast is invalid, ignoring assignment: %s", rvalue
);
708 int config_parse_address(const char *unit
,
709 const char *filename
,
712 unsigned section_line
,
719 Network
*network
= userdata
;
720 _cleanup_(address_freep
) Address
*n
= NULL
;
721 union in_addr_union buffer
;
722 unsigned char prefixlen
;
731 if (streq(section
, "Network")) {
732 /* we are not in an Address section, so treat
733 * this as the special '0' section */
734 r
= address_new_static(network
, NULL
, 0, &n
);
736 r
= address_new_static(network
, filename
, section_line
, &n
);
741 /* Address=address/prefixlen */
742 r
= in_addr_default_prefix_from_string_auto(rvalue
, &f
, &buffer
, &prefixlen
);
744 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Invalid address '%s', ignoring assignment: %m", rvalue
);
748 if (n
->family
!= AF_UNSPEC
&& f
!= n
->family
) {
749 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Address is incompatible, ignoring assignment: %s", rvalue
);
754 n
->prefixlen
= prefixlen
;
756 if (streq(lvalue
, "Address"))
759 n
->in_addr_peer
= buffer
;
761 if (n
->family
== AF_INET
&& n
->broadcast
.s_addr
== 0)
762 n
->broadcast
.s_addr
= n
->in_addr
.in
.s_addr
| htonl(0xfffffffflu
>> n
->prefixlen
);
769 int config_parse_label(
771 const char *filename
,
774 unsigned section_line
,
781 _cleanup_(address_freep
) Address
*n
= NULL
;
782 Network
*network
= userdata
;
791 r
= address_new_static(network
, filename
, section_line
, &n
);
795 if (!address_label_valid(rvalue
)) {
796 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Interface label is too long or invalid, ignoring assignment: %s", rvalue
);
800 r
= free_and_strdup(&n
->label
, rvalue
);
809 int config_parse_lifetime(const char *unit
,
810 const char *filename
,
813 unsigned section_line
,
819 Network
*network
= userdata
;
820 _cleanup_(address_freep
) Address
*n
= NULL
;
830 r
= address_new_static(network
, filename
, section_line
, &n
);
834 if (STR_IN_SET(rvalue
, "forever", "infinity")) {
835 n
->cinfo
.ifa_prefered
= CACHE_INFO_INFINITY_LIFE_TIME
;
841 r
= safe_atou(rvalue
, &k
);
843 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse PreferredLifetime, ignoring: %s", rvalue
);
848 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid PreferredLifetime value, ignoring: %d", k
);
850 n
->cinfo
.ifa_prefered
= k
;
857 int config_parse_address_flags(const char *unit
,
858 const char *filename
,
861 unsigned section_line
,
867 Network
*network
= userdata
;
868 _cleanup_(address_freep
) Address
*n
= NULL
;
877 r
= address_new_static(network
, filename
, section_line
, &n
);
881 r
= parse_boolean(rvalue
);
883 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse address flag, ignoring: %s", rvalue
);
887 if (streq(lvalue
, "HomeAddress"))
889 else if (streq(lvalue
, "DuplicateAddressDetection"))
890 n
->duplicate_address_detection
= r
;
891 else if (streq(lvalue
, "ManageTemporaryAddress"))
892 n
->manage_temporary_address
= r
;
893 else if (streq(lvalue
, "PrefixRoute"))
895 else if (streq(lvalue
, "AutoJoin"))
901 int config_parse_address_scope(const char *unit
,
902 const char *filename
,
905 unsigned section_line
,
911 Network
*network
= userdata
;
912 _cleanup_(address_freep
) Address
*n
= NULL
;
921 r
= address_new_static(network
, filename
, section_line
, &n
);
925 if (streq(rvalue
, "host"))
926 n
->scope
= RT_SCOPE_HOST
;
927 else if (streq(rvalue
, "link"))
928 n
->scope
= RT_SCOPE_LINK
;
929 else if (streq(rvalue
, "global"))
930 n
->scope
= RT_SCOPE_UNIVERSE
;
932 r
= safe_atou8(rvalue
, &n
->scope
);
934 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Could not parse address scope \"%s\", ignoring assignment: %m", rvalue
);
944 bool address_is_ready(const Address
*a
) {
947 if (a
->family
== AF_INET6
)
948 return !(a
->flags
& IFA_F_TENTATIVE
);
950 return !(a
->flags
& (IFA_F_TENTATIVE
| IFA_F_DEPRECATED
));