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
= new0(Address
, 1);
29 address
->family
= AF_UNSPEC
;
30 address
->scope
= RT_SCOPE_UNIVERSE
;
31 address
->cinfo
.ifa_prefered
= CACHE_INFO_INFINITY_LIFE_TIME
;
32 address
->cinfo
.ifa_valid
= CACHE_INFO_INFINITY_LIFE_TIME
;
34 *ret
= TAKE_PTR(address
);
39 int address_new_static(Network
*network
, const char *filename
, unsigned section_line
, Address
**ret
) {
40 _cleanup_(network_config_section_freep
) NetworkConfigSection
*n
= NULL
;
41 _cleanup_(address_freep
) Address
*address
= NULL
;
46 assert(!!filename
== (section_line
> 0));
49 r
= network_config_section_new(filename
, section_line
, &n
);
53 address
= hashmap_get(network
->addresses_by_section
, n
);
55 *ret
= TAKE_PTR(address
);
61 if (network
->n_static_addresses
>= STATIC_ADDRESSES_PER_NETWORK_MAX
)
64 r
= address_new(&address
);
69 address
->section
= TAKE_PTR(n
);
71 r
= hashmap_put(network
->addresses_by_section
, address
->section
, address
);
76 address
->network
= network
;
77 LIST_APPEND(addresses
, network
->static_addresses
, address
);
78 network
->n_static_addresses
++;
80 *ret
= TAKE_PTR(address
);
85 void address_free(Address
*address
) {
89 if (address
->network
) {
90 LIST_REMOVE(addresses
, address
->network
->static_addresses
, address
);
91 assert(address
->network
->n_static_addresses
> 0);
92 address
->network
->n_static_addresses
--;
94 if (address
->section
) {
95 hashmap_remove(address
->network
->addresses_by_section
, address
->section
);
96 network_config_section_free(address
->section
);
101 set_remove(address
->link
->addresses
, address
);
102 set_remove(address
->link
->addresses_foreign
, address
);
104 if (in_addr_equal(AF_INET6
, &address
->in_addr
, (const union in_addr_union
*) &address
->link
->ipv6ll_address
))
105 memzero(&address
->link
->ipv6ll_address
, sizeof(struct in6_addr
));
111 static void address_hash_func(const void *b
, struct siphash
*state
) {
112 const Address
*a
= b
;
116 siphash24_compress(&a
->family
, sizeof(a
->family
), state
);
120 siphash24_compress(&a
->prefixlen
, sizeof(a
->prefixlen
), state
);
123 if (a
->prefixlen
!= 0) {
126 if (a
->in_addr_peer
.in
.s_addr
!= 0)
127 prefix
= be32toh(a
->in_addr_peer
.in
.s_addr
) >> (32 - a
->prefixlen
);
129 prefix
= be32toh(a
->in_addr
.in
.s_addr
) >> (32 - a
->prefixlen
);
131 siphash24_compress(&prefix
, sizeof(prefix
), state
);
137 siphash24_compress(&a
->in_addr
, FAMILY_ADDRESS_SIZE(a
->family
), state
);
141 /* treat any other address family as AF_UNSPEC */
146 static int address_compare_func(const void *c1
, const void *c2
) {
147 const Address
*a1
= c1
, *a2
= c2
;
150 r
= CMP(a1
->family
, a2
->family
);
154 switch (a1
->family
) {
155 /* use the same notion of equality as the kernel does */
157 r
= CMP(a1
->prefixlen
, a2
->prefixlen
);
161 /* compare the peer prefixes */
162 if (a1
->prefixlen
!= 0) {
163 /* make sure we don't try to shift by 32.
164 * See ISO/IEC 9899:TC3 ยง 6.5.7.3. */
167 if (a1
->in_addr_peer
.in
.s_addr
!= 0)
168 b1
= be32toh(a1
->in_addr_peer
.in
.s_addr
) >> (32 - a1
->prefixlen
);
170 b1
= be32toh(a1
->in_addr
.in
.s_addr
) >> (32 - a1
->prefixlen
);
172 if (a2
->in_addr_peer
.in
.s_addr
!= 0)
173 b2
= be32toh(a2
->in_addr_peer
.in
.s_addr
) >> (32 - a1
->prefixlen
);
175 b2
= be32toh(a2
->in_addr
.in
.s_addr
) >> (32 - a1
->prefixlen
);
184 return memcmp(&a1
->in_addr
, &a2
->in_addr
, FAMILY_ADDRESS_SIZE(a1
->family
));
186 /* treat any other address family as AF_UNSPEC */
191 static const struct hash_ops address_hash_ops
= {
192 .hash
= address_hash_func
,
193 .compare
= address_compare_func
196 bool address_equal(Address
*a1
, Address
*a2
) {
203 return address_compare_func(a1
, a2
) == 0;
206 static int address_establish(Address
*address
, Link
*link
) {
213 masq
= link
->network
&&
214 link
->network
->ip_masquerade
&&
215 address
->family
== AF_INET
&&
216 address
->scope
< RT_SCOPE_LINK
;
218 /* Add firewall entry if this is requested */
219 if (address
->ip_masquerade_done
!= masq
) {
220 union in_addr_union masked
= address
->in_addr
;
221 in_addr_mask(address
->family
, &masked
, address
->prefixlen
);
223 r
= fw_add_masquerade(masq
, AF_INET
, 0, &masked
, address
->prefixlen
, NULL
, NULL
, 0);
225 log_link_warning_errno(link
, r
, "Could not enable IP masquerading: %m");
227 address
->ip_masquerade_done
= masq
;
233 static int address_add_internal(Link
*link
, Set
**addresses
,
235 const union in_addr_union
*in_addr
,
236 unsigned char prefixlen
,
238 _cleanup_(address_freep
) Address
*address
= NULL
;
245 r
= address_new(&address
);
249 address
->family
= family
;
250 address
->in_addr
= *in_addr
;
251 address
->prefixlen
= prefixlen
;
252 /* Consider address tentative until we get the real flags from the kernel */
253 address
->flags
= IFA_F_TENTATIVE
;
255 r
= set_ensure_allocated(addresses
, &address_hash_ops
);
259 r
= set_put(*addresses
, address
);
263 address
->link
= link
;
273 int address_add_foreign(Link
*link
, int family
, const union in_addr_union
*in_addr
, unsigned char prefixlen
, Address
**ret
) {
274 return address_add_internal(link
, &link
->addresses_foreign
, family
, in_addr
, prefixlen
, ret
);
277 int address_add(Link
*link
, int family
, const union in_addr_union
*in_addr
, unsigned char prefixlen
, Address
**ret
) {
281 r
= address_get(link
, family
, in_addr
, prefixlen
, &address
);
283 /* Address does not exist, create a new one */
284 r
= address_add_internal(link
, &link
->addresses
, family
, in_addr
, prefixlen
, &address
);
288 /* Take over a foreign address */
289 r
= set_ensure_allocated(&link
->addresses
, &address_hash_ops
);
293 r
= set_put(link
->addresses
, address
);
297 set_remove(link
->addresses_foreign
, address
);
299 /* Already exists, do nothing */
310 static int address_release(Address
*address
) {
314 assert(address
->link
);
316 /* Remove masquerading firewall entry if it was added */
317 if (address
->ip_masquerade_done
) {
318 union in_addr_union masked
= address
->in_addr
;
319 in_addr_mask(address
->family
, &masked
, address
->prefixlen
);
321 r
= fw_add_masquerade(false, AF_INET
, 0, &masked
, address
->prefixlen
, NULL
, NULL
, 0);
323 log_link_warning_errno(address
->link
, r
, "Failed to disable IP masquerading: %m");
325 address
->ip_masquerade_done
= false;
335 const struct ifa_cacheinfo
*cinfo
) {
342 assert_return(address
->link
, 1);
344 if (IN_SET(address
->link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
347 ready
= address_is_ready(address
);
349 address
->flags
= flags
;
350 address
->scope
= scope
;
351 address
->cinfo
= *cinfo
;
353 link_update_operstate(address
->link
);
355 if (!ready
&& address_is_ready(address
)) {
356 link_check_ready(address
->link
);
358 if (address
->family
== AF_INET6
&&
359 in_addr_is_link_local(AF_INET6
, &address
->in_addr
) > 0 &&
360 in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &address
->link
->ipv6ll_address
) > 0) {
362 r
= link_ipv6ll_gained(address
->link
, &address
->in_addr
.in6
);
371 int address_drop(Address
*address
) {
377 ready
= address_is_ready(address
);
378 link
= address
->link
;
380 address_release(address
);
381 address_free(address
);
383 link_update_operstate(link
);
386 link_check_ready(link
);
391 int address_get(Link
*link
,
393 const union in_addr_union
*in_addr
,
394 unsigned char prefixlen
,
397 Address address
, *existing
;
402 address
= (Address
) {
405 .prefixlen
= prefixlen
,
408 existing
= set_get(link
->addresses
, &address
);
415 existing
= set_get(link
->addresses_foreign
, &address
);
428 sd_netlink_message_handler_t callback
) {
430 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
434 assert(IN_SET(address
->family
, AF_INET
, AF_INET6
));
436 assert(link
->ifindex
> 0);
437 assert(link
->manager
);
438 assert(link
->manager
->rtnl
);
440 r
= sd_rtnl_message_new_addr(link
->manager
->rtnl
, &req
, RTM_DELADDR
,
441 link
->ifindex
, address
->family
);
443 return log_error_errno(r
, "Could not allocate RTM_DELADDR message: %m");
445 r
= sd_rtnl_message_addr_set_prefixlen(req
, address
->prefixlen
);
447 return log_error_errno(r
, "Could not set prefixlen: %m");
449 if (address
->family
== AF_INET
)
450 r
= sd_netlink_message_append_in_addr(req
, IFA_LOCAL
, &address
->in_addr
.in
);
451 else if (address
->family
== AF_INET6
)
452 r
= sd_netlink_message_append_in6_addr(req
, IFA_LOCAL
, &address
->in_addr
.in6
);
454 return log_error_errno(r
, "Could not append IFA_LOCAL attribute: %m");
456 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, callback
, NULL
, link
, 0, NULL
);
458 return log_error_errno(r
, "Could not send rtnetlink message: %m");
465 static int address_acquire(Link
*link
, Address
*original
, Address
**ret
) {
466 union in_addr_union in_addr
= {};
467 struct in_addr broadcast
= {};
468 _cleanup_(address_freep
) Address
*na
= NULL
;
475 /* Something useful was configured? just use it */
476 if (in_addr_is_null(original
->family
, &original
->in_addr
) <= 0)
479 /* The address is configured to be 0.0.0.0 or [::] by the user?
480 * Then let's acquire something more useful from the pool. */
481 r
= manager_address_pool_acquire(link
->manager
, original
->family
, original
->prefixlen
, &in_addr
);
483 return log_link_error_errno(link
, r
, "Failed to acquire address from pool: %m");
485 log_link_error(link
, "Couldn't find free address for interface, all taken.");
489 if (original
->family
== AF_INET
) {
490 /* Pick first address in range for ourselves ... */
491 in_addr
.in
.s_addr
= in_addr
.in
.s_addr
| htobe32(1);
493 /* .. and use last as broadcast address */
494 if (original
->prefixlen
> 30)
495 broadcast
.s_addr
= 0;
497 broadcast
.s_addr
= in_addr
.in
.s_addr
| htobe32(0xFFFFFFFFUL
>> original
->prefixlen
);
498 } else if (original
->family
== AF_INET6
)
499 in_addr
.in6
.s6_addr
[15] |= 1;
501 r
= address_new(&na
);
505 na
->family
= original
->family
;
506 na
->prefixlen
= original
->prefixlen
;
507 na
->scope
= original
->scope
;
508 na
->cinfo
= original
->cinfo
;
510 if (original
->label
) {
511 na
->label
= strdup(original
->label
);
516 na
->broadcast
= broadcast
;
517 na
->in_addr
= in_addr
;
519 LIST_PREPEND(addresses
, link
->pool_addresses
, na
);
526 int address_configure(
529 sd_netlink_message_handler_t callback
,
532 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
536 assert(IN_SET(address
->family
, AF_INET
, AF_INET6
));
538 assert(link
->ifindex
> 0);
539 assert(link
->manager
);
540 assert(link
->manager
->rtnl
);
542 /* If this is a new address, then refuse adding more than the limit */
543 if (address_get(link
, address
->family
, &address
->in_addr
, address
->prefixlen
, NULL
) <= 0 &&
544 set_size(link
->addresses
) >= ADDRESSES_PER_LINK_MAX
)
547 r
= address_acquire(link
, address
, &address
);
552 r
= sd_rtnl_message_new_addr_update(link
->manager
->rtnl
, &req
,
553 link
->ifindex
, address
->family
);
555 r
= sd_rtnl_message_new_addr(link
->manager
->rtnl
, &req
, RTM_NEWADDR
,
556 link
->ifindex
, address
->family
);
558 return log_error_errno(r
, "Could not allocate RTM_NEWADDR message: %m");
560 r
= sd_rtnl_message_addr_set_prefixlen(req
, address
->prefixlen
);
562 return log_error_errno(r
, "Could not set prefixlen: %m");
564 address
->flags
|= IFA_F_PERMANENT
;
566 if (address
->home_address
)
567 address
->flags
|= IFA_F_HOMEADDRESS
;
569 if (address
->duplicate_address_detection
)
570 address
->flags
|= IFA_F_NODAD
;
572 if (address
->manage_temporary_address
)
573 address
->flags
|= IFA_F_MANAGETEMPADDR
;
575 if (address
->prefix_route
)
576 address
->flags
|= IFA_F_NOPREFIXROUTE
;
578 if (address
->autojoin
)
579 address
->flags
|= IFA_F_MCAUTOJOIN
;
581 r
= sd_rtnl_message_addr_set_flags(req
, (address
->flags
& 0xff));
583 return log_error_errno(r
, "Could not set flags: %m");
585 if (address
->flags
& ~0xff) {
586 r
= sd_netlink_message_append_u32(req
, IFA_FLAGS
, address
->flags
);
588 return log_error_errno(r
, "Could not set extended flags: %m");
591 r
= sd_rtnl_message_addr_set_scope(req
, address
->scope
);
593 return log_error_errno(r
, "Could not set scope: %m");
595 if (address
->family
== AF_INET
)
596 r
= sd_netlink_message_append_in_addr(req
, IFA_LOCAL
, &address
->in_addr
.in
);
597 else if (address
->family
== AF_INET6
)
598 r
= sd_netlink_message_append_in6_addr(req
, IFA_LOCAL
, &address
->in_addr
.in6
);
600 return log_error_errno(r
, "Could not append IFA_LOCAL attribute: %m");
602 if (!in_addr_is_null(address
->family
, &address
->in_addr_peer
)) {
603 if (address
->family
== AF_INET
)
604 r
= sd_netlink_message_append_in_addr(req
, IFA_ADDRESS
, &address
->in_addr_peer
.in
);
605 else if (address
->family
== AF_INET6
)
606 r
= sd_netlink_message_append_in6_addr(req
, IFA_ADDRESS
, &address
->in_addr_peer
.in6
);
608 return log_error_errno(r
, "Could not append IFA_ADDRESS attribute: %m");
610 if (address
->family
== AF_INET
) {
611 if (address
->prefixlen
<= 30) {
612 r
= sd_netlink_message_append_in_addr(req
, IFA_BROADCAST
, &address
->broadcast
);
614 return log_error_errno(r
, "Could not append IFA_BROADCAST attribute: %m");
619 if (address
->label
) {
620 r
= sd_netlink_message_append_string(req
, IFA_LABEL
, address
->label
);
622 return log_error_errno(r
, "Could not append IFA_LABEL attribute: %m");
625 r
= sd_netlink_message_append_cache_info(req
, IFA_CACHEINFO
,
628 return log_error_errno(r
, "Could not append IFA_CACHEINFO attribute: %m");
630 r
= address_establish(address
, link
);
634 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, callback
, NULL
, link
, 0, NULL
);
636 address_release(address
);
637 return log_error_errno(r
, "Could not send rtnetlink message: %m");
642 r
= address_add(link
, address
->family
, &address
->in_addr
, address
->prefixlen
, NULL
);
644 address_release(address
);
645 return log_error_errno(r
, "Could not add address: %m");
651 int config_parse_broadcast(
653 const char *filename
,
656 unsigned section_line
,
663 Network
*network
= userdata
;
664 _cleanup_(address_freep
) Address
*n
= NULL
;
673 r
= address_new_static(network
, filename
, section_line
, &n
);
677 if (n
->family
== AF_INET6
) {
678 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Broadcast is not valid for IPv6 addresses, ignoring assignment: %s", rvalue
);
682 r
= in_addr_from_string(AF_INET
, rvalue
, (union in_addr_union
*) &n
->broadcast
);
684 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Broadcast is invalid, ignoring assignment: %s", rvalue
);
694 int config_parse_address(const char *unit
,
695 const char *filename
,
698 unsigned section_line
,
705 Network
*network
= userdata
;
706 _cleanup_(address_freep
) Address
*n
= NULL
;
707 const char *address
, *e
;
708 union in_addr_union buffer
;
717 if (streq(section
, "Network")) {
718 /* we are not in an Address section, so treat
719 * this as the special '0' section */
720 r
= address_new_static(network
, NULL
, 0, &n
);
722 r
= address_new_static(network
, filename
, section_line
, &n
);
727 /* Address=address/prefixlen */
730 e
= strchr(rvalue
, '/');
734 r
= safe_atou(e
+ 1, &i
);
736 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Prefix length is invalid, ignoring assignment: %s", e
+ 1);
740 n
->prefixlen
= (unsigned char) i
;
742 address
= strndupa(rvalue
, e
- rvalue
);
746 r
= in_addr_from_string_auto(address
, &f
, &buffer
);
748 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Address is invalid, ignoring assignment: %s", address
);
752 if (!e
&& f
== AF_INET
) {
753 r
= in4_addr_default_prefixlen(&buffer
.in
, &n
->prefixlen
);
755 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Prefix length not specified, and a default one cannot be deduced for '%s', ignoring assignment", address
);
760 if (n
->family
!= AF_UNSPEC
&& f
!= n
->family
) {
761 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Address is incompatible, ignoring assignment: %s", address
);
767 if (streq(lvalue
, "Address"))
770 n
->in_addr_peer
= buffer
;
772 if (n
->family
== AF_INET
&& n
->broadcast
.s_addr
== 0)
773 n
->broadcast
.s_addr
= n
->in_addr
.in
.s_addr
| htonl(0xfffffffflu
>> n
->prefixlen
);
780 int config_parse_label(
782 const char *filename
,
785 unsigned section_line
,
792 _cleanup_(address_freep
) Address
*n
= NULL
;
793 Network
*network
= userdata
;
802 r
= address_new_static(network
, filename
, section_line
, &n
);
806 if (!address_label_valid(rvalue
)) {
807 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Interface label is too long or invalid, ignoring assignment: %s", rvalue
);
811 r
= free_and_strdup(&n
->label
, rvalue
);
820 int config_parse_lifetime(const char *unit
,
821 const char *filename
,
824 unsigned section_line
,
830 Network
*network
= userdata
;
831 _cleanup_(address_freep
) Address
*n
= NULL
;
841 r
= address_new_static(network
, filename
, section_line
, &n
);
845 if (STR_IN_SET(rvalue
, "forever", "infinity")) {
846 n
->cinfo
.ifa_prefered
= CACHE_INFO_INFINITY_LIFE_TIME
;
852 r
= safe_atou(rvalue
, &k
);
854 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse PreferredLifetime, ignoring: %s", rvalue
);
859 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid PreferredLifetime value, ignoring: %d", k
);
861 n
->cinfo
.ifa_prefered
= k
;
868 int config_parse_address_flags(const char *unit
,
869 const char *filename
,
872 unsigned section_line
,
878 Network
*network
= userdata
;
879 _cleanup_(address_freep
) Address
*n
= NULL
;
888 r
= address_new_static(network
, filename
, section_line
, &n
);
892 r
= parse_boolean(rvalue
);
894 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse address flag, ignoring: %s", rvalue
);
898 if (streq(lvalue
, "HomeAddress"))
900 else if (streq(lvalue
, "DuplicateAddressDetection"))
901 n
->duplicate_address_detection
= r
;
902 else if (streq(lvalue
, "ManageTemporaryAddress"))
903 n
->manage_temporary_address
= r
;
904 else if (streq(lvalue
, "PrefixRoute"))
906 else if (streq(lvalue
, "AutoJoin"))
912 int config_parse_address_scope(const char *unit
,
913 const char *filename
,
916 unsigned section_line
,
922 Network
*network
= userdata
;
923 _cleanup_(address_freep
) Address
*n
= NULL
;
932 r
= address_new_static(network
, filename
, section_line
, &n
);
936 if (streq(rvalue
, "host"))
937 n
->scope
= RT_SCOPE_HOST
;
938 else if (streq(rvalue
, "link"))
939 n
->scope
= RT_SCOPE_LINK
;
940 else if (streq(rvalue
, "global"))
941 n
->scope
= RT_SCOPE_UNIVERSE
;
943 r
= safe_atou8(rvalue
, &n
->scope
);
945 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Could not parse address scope \"%s\", ignoring assignment: %m", rvalue
);
955 bool address_is_ready(const Address
*a
) {
958 if (a
->family
== AF_INET6
)
959 return !(a
->flags
& IFA_F_TENTATIVE
);
961 return !(a
->flags
& (IFA_F_TENTATIVE
| IFA_F_DEPRECATED
));