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"
18 #define ADDRESSES_PER_LINK_MAX 2048U
19 #define STATIC_ADDRESSES_PER_NETWORK_MAX 1024U
21 int address_new(Address
**ret
) {
22 _cleanup_(address_freep
) Address
*address
= NULL
;
24 address
= new0(Address
, 1);
28 address
->family
= AF_UNSPEC
;
29 address
->scope
= RT_SCOPE_UNIVERSE
;
30 address
->cinfo
.ifa_prefered
= CACHE_INFO_INFINITY_LIFE_TIME
;
31 address
->cinfo
.ifa_valid
= CACHE_INFO_INFINITY_LIFE_TIME
;
33 *ret
= TAKE_PTR(address
);
38 int address_new_static(Network
*network
, const char *filename
, unsigned section_line
, Address
**ret
) {
39 _cleanup_(network_config_section_freep
) NetworkConfigSection
*n
= NULL
;
40 _cleanup_(address_freep
) Address
*address
= NULL
;
45 assert(!!filename
== (section_line
> 0));
48 r
= network_config_section_new(filename
, section_line
, &n
);
52 address
= hashmap_get(network
->addresses_by_section
, n
);
54 *ret
= TAKE_PTR(address
);
60 if (network
->n_static_addresses
>= STATIC_ADDRESSES_PER_NETWORK_MAX
)
63 r
= address_new(&address
);
68 address
->section
= TAKE_PTR(n
);
70 r
= hashmap_put(network
->addresses_by_section
, address
->section
, address
);
75 address
->network
= network
;
76 LIST_APPEND(addresses
, network
->static_addresses
, address
);
77 network
->n_static_addresses
++;
79 *ret
= TAKE_PTR(address
);
84 void address_free(Address
*address
) {
88 if (address
->network
) {
89 LIST_REMOVE(addresses
, address
->network
->static_addresses
, address
);
90 assert(address
->network
->n_static_addresses
> 0);
91 address
->network
->n_static_addresses
--;
93 if (address
->section
) {
94 hashmap_remove(address
->network
->addresses_by_section
, address
->section
);
95 network_config_section_free(address
->section
);
100 set_remove(address
->link
->addresses
, address
);
101 set_remove(address
->link
->addresses_foreign
, address
);
103 if (in_addr_equal(AF_INET6
, &address
->in_addr
, (const union in_addr_union
*) &address
->link
->ipv6ll_address
))
104 memzero(&address
->link
->ipv6ll_address
, sizeof(struct in6_addr
));
110 static void address_hash_func(const void *b
, struct siphash
*state
) {
111 const Address
*a
= b
;
115 siphash24_compress(&a
->family
, sizeof(a
->family
), state
);
119 siphash24_compress(&a
->prefixlen
, sizeof(a
->prefixlen
), state
);
122 if (a
->prefixlen
!= 0) {
125 if (a
->in_addr_peer
.in
.s_addr
!= 0)
126 prefix
= be32toh(a
->in_addr_peer
.in
.s_addr
) >> (32 - a
->prefixlen
);
128 prefix
= be32toh(a
->in_addr
.in
.s_addr
) >> (32 - a
->prefixlen
);
130 siphash24_compress(&prefix
, sizeof(prefix
), state
);
136 siphash24_compress(&a
->in_addr
, FAMILY_ADDRESS_SIZE(a
->family
), state
);
140 /* treat any other address family as AF_UNSPEC */
145 static int address_compare_func(const void *c1
, const void *c2
) {
146 const Address
*a1
= c1
, *a2
= c2
;
148 if (a1
->family
< a2
->family
)
150 if (a1
->family
> a2
->family
)
153 switch (a1
->family
) {
154 /* use the same notion of equality as the kernel does */
156 if (a1
->prefixlen
< a2
->prefixlen
)
158 if (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
);
185 return memcmp(&a1
->in_addr
, &a2
->in_addr
, FAMILY_ADDRESS_SIZE(a1
->family
));
187 /* treat any other address family as AF_UNSPEC */
192 static const struct hash_ops address_hash_ops
= {
193 .hash
= address_hash_func
,
194 .compare
= address_compare_func
197 bool address_equal(Address
*a1
, Address
*a2
) {
204 return address_compare_func(a1
, a2
) == 0;
207 static int address_establish(Address
*address
, Link
*link
) {
214 masq
= link
->network
&&
215 link
->network
->ip_masquerade
&&
216 address
->family
== AF_INET
&&
217 address
->scope
< RT_SCOPE_LINK
;
219 /* Add firewall entry if this is requested */
220 if (address
->ip_masquerade_done
!= masq
) {
221 union in_addr_union masked
= address
->in_addr
;
222 in_addr_mask(address
->family
, &masked
, address
->prefixlen
);
224 r
= fw_add_masquerade(masq
, AF_INET
, 0, &masked
, address
->prefixlen
, NULL
, NULL
, 0);
226 log_link_warning_errno(link
, r
, "Could not enable IP masquerading: %m");
228 address
->ip_masquerade_done
= masq
;
234 static int address_add_internal(Link
*link
, Set
**addresses
,
236 const union in_addr_union
*in_addr
,
237 unsigned char prefixlen
,
239 _cleanup_(address_freep
) Address
*address
= NULL
;
246 r
= address_new(&address
);
250 address
->family
= family
;
251 address
->in_addr
= *in_addr
;
252 address
->prefixlen
= prefixlen
;
253 /* Consider address tentative until we get the real flags from the kernel */
254 address
->flags
= IFA_F_TENTATIVE
;
256 r
= set_ensure_allocated(addresses
, &address_hash_ops
);
260 r
= set_put(*addresses
, address
);
264 address
->link
= link
;
274 int address_add_foreign(Link
*link
, int family
, const union in_addr_union
*in_addr
, unsigned char prefixlen
, Address
**ret
) {
275 return address_add_internal(link
, &link
->addresses_foreign
, family
, in_addr
, prefixlen
, ret
);
278 int address_add(Link
*link
, int family
, const union in_addr_union
*in_addr
, unsigned char prefixlen
, Address
**ret
) {
282 r
= address_get(link
, family
, in_addr
, prefixlen
, &address
);
284 /* Address does not exist, create a new one */
285 r
= address_add_internal(link
, &link
->addresses
, family
, in_addr
, prefixlen
, &address
);
289 /* Take over a foreign address */
290 r
= set_ensure_allocated(&link
->addresses
, &address_hash_ops
);
294 r
= set_put(link
->addresses
, address
);
298 set_remove(link
->addresses_foreign
, address
);
300 /* Already exists, do nothing */
311 static int address_release(Address
*address
) {
315 assert(address
->link
);
317 /* Remove masquerading firewall entry if it was added */
318 if (address
->ip_masquerade_done
) {
319 union in_addr_union masked
= address
->in_addr
;
320 in_addr_mask(address
->family
, &masked
, address
->prefixlen
);
322 r
= fw_add_masquerade(false, AF_INET
, 0, &masked
, address
->prefixlen
, NULL
, NULL
, 0);
324 log_link_warning_errno(address
->link
, r
, "Failed to disable IP masquerading: %m");
326 address
->ip_masquerade_done
= false;
336 const struct ifa_cacheinfo
*cinfo
) {
343 assert_return(address
->link
, 1);
345 if (IN_SET(address
->link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
348 ready
= address_is_ready(address
);
350 address
->flags
= flags
;
351 address
->scope
= scope
;
352 address
->cinfo
= *cinfo
;
354 link_update_operstate(address
->link
);
356 if (!ready
&& address_is_ready(address
)) {
357 link_check_ready(address
->link
);
359 if (address
->family
== AF_INET6
&&
360 in_addr_is_link_local(AF_INET6
, &address
->in_addr
) > 0 &&
361 in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &address
->link
->ipv6ll_address
) > 0) {
363 r
= link_ipv6ll_gained(address
->link
, &address
->in_addr
.in6
);
372 int address_drop(Address
*address
) {
378 ready
= address_is_ready(address
);
379 link
= address
->link
;
381 address_release(address
);
382 address_free(address
);
384 link_update_operstate(link
);
387 link_check_ready(link
);
392 int address_get(Link
*link
,
394 const union in_addr_union
*in_addr
,
395 unsigned char prefixlen
,
398 Address address
, *existing
;
403 address
= (Address
) {
406 .prefixlen
= prefixlen
,
409 existing
= set_get(link
->addresses
, &address
);
416 existing
= set_get(link
->addresses_foreign
, &address
);
429 sd_netlink_message_handler_t callback
) {
431 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
435 assert(IN_SET(address
->family
, AF_INET
, AF_INET6
));
437 assert(link
->ifindex
> 0);
438 assert(link
->manager
);
439 assert(link
->manager
->rtnl
);
441 r
= sd_rtnl_message_new_addr(link
->manager
->rtnl
, &req
, RTM_DELADDR
,
442 link
->ifindex
, address
->family
);
444 return log_error_errno(r
, "Could not allocate RTM_DELADDR message: %m");
446 r
= sd_rtnl_message_addr_set_prefixlen(req
, address
->prefixlen
);
448 return log_error_errno(r
, "Could not set prefixlen: %m");
450 if (address
->family
== AF_INET
)
451 r
= sd_netlink_message_append_in_addr(req
, IFA_LOCAL
, &address
->in_addr
.in
);
452 else if (address
->family
== AF_INET6
)
453 r
= sd_netlink_message_append_in6_addr(req
, IFA_LOCAL
, &address
->in_addr
.in6
);
455 return log_error_errno(r
, "Could not append IFA_LOCAL attribute: %m");
457 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, callback
, link
, 0, NULL
);
459 return log_error_errno(r
, "Could not send rtnetlink message: %m");
466 static int address_acquire(Link
*link
, Address
*original
, Address
**ret
) {
467 union in_addr_union in_addr
= {};
468 struct in_addr broadcast
= {};
469 _cleanup_(address_freep
) Address
*na
= NULL
;
476 /* Something useful was configured? just use it */
477 if (in_addr_is_null(original
->family
, &original
->in_addr
) <= 0)
480 /* The address is configured to be 0.0.0.0 or [::] by the user?
481 * Then let's acquire something more useful from the pool. */
482 r
= manager_address_pool_acquire(link
->manager
, original
->family
, original
->prefixlen
, &in_addr
);
484 return log_link_error_errno(link
, r
, "Failed to acquire address from pool: %m");
486 log_link_error(link
, "Couldn't find free address for interface, all taken.");
490 if (original
->family
== AF_INET
) {
491 /* Pick first address in range for ourselves ... */
492 in_addr
.in
.s_addr
= in_addr
.in
.s_addr
| htobe32(1);
494 /* .. and use last as broadcast address */
495 if (original
->prefixlen
> 30)
496 broadcast
.s_addr
= 0;
498 broadcast
.s_addr
= in_addr
.in
.s_addr
| htobe32(0xFFFFFFFFUL
>> original
->prefixlen
);
499 } else if (original
->family
== AF_INET6
)
500 in_addr
.in6
.s6_addr
[15] |= 1;
502 r
= address_new(&na
);
506 na
->family
= original
->family
;
507 na
->prefixlen
= original
->prefixlen
;
508 na
->scope
= original
->scope
;
509 na
->cinfo
= original
->cinfo
;
511 if (original
->label
) {
512 na
->label
= strdup(original
->label
);
517 na
->broadcast
= broadcast
;
518 na
->in_addr
= in_addr
;
520 LIST_PREPEND(addresses
, link
->pool_addresses
, na
);
527 int address_configure(
530 sd_netlink_message_handler_t callback
,
533 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
537 assert(IN_SET(address
->family
, AF_INET
, AF_INET6
));
539 assert(link
->ifindex
> 0);
540 assert(link
->manager
);
541 assert(link
->manager
->rtnl
);
543 /* If this is a new address, then refuse adding more than the limit */
544 if (address_get(link
, address
->family
, &address
->in_addr
, address
->prefixlen
, NULL
) <= 0 &&
545 set_size(link
->addresses
) >= ADDRESSES_PER_LINK_MAX
)
548 r
= address_acquire(link
, address
, &address
);
553 r
= sd_rtnl_message_new_addr_update(link
->manager
->rtnl
, &req
,
554 link
->ifindex
, address
->family
);
556 r
= sd_rtnl_message_new_addr(link
->manager
->rtnl
, &req
, RTM_NEWADDR
,
557 link
->ifindex
, address
->family
);
559 return log_error_errno(r
, "Could not allocate RTM_NEWADDR message: %m");
561 r
= sd_rtnl_message_addr_set_prefixlen(req
, address
->prefixlen
);
563 return log_error_errno(r
, "Could not set prefixlen: %m");
565 address
->flags
|= IFA_F_PERMANENT
;
567 if (address
->home_address
)
568 address
->flags
|= IFA_F_HOMEADDRESS
;
570 if (address
->duplicate_address_detection
)
571 address
->flags
|= IFA_F_NODAD
;
573 if (address
->manage_temporary_address
)
574 address
->flags
|= IFA_F_MANAGETEMPADDR
;
576 if (address
->prefix_route
)
577 address
->flags
|= IFA_F_NOPREFIXROUTE
;
579 if (address
->autojoin
)
580 address
->flags
|= IFA_F_MCAUTOJOIN
;
582 r
= sd_rtnl_message_addr_set_flags(req
, (address
->flags
& 0xff));
584 return log_error_errno(r
, "Could not set flags: %m");
586 if (address
->flags
& ~0xff) {
587 r
= sd_netlink_message_append_u32(req
, IFA_FLAGS
, address
->flags
);
589 return log_error_errno(r
, "Could not set extended flags: %m");
592 r
= sd_rtnl_message_addr_set_scope(req
, address
->scope
);
594 return log_error_errno(r
, "Could not set scope: %m");
596 if (address
->family
== AF_INET
)
597 r
= sd_netlink_message_append_in_addr(req
, IFA_LOCAL
, &address
->in_addr
.in
);
598 else if (address
->family
== AF_INET6
)
599 r
= sd_netlink_message_append_in6_addr(req
, IFA_LOCAL
, &address
->in_addr
.in6
);
601 return log_error_errno(r
, "Could not append IFA_LOCAL attribute: %m");
603 if (!in_addr_is_null(address
->family
, &address
->in_addr_peer
)) {
604 if (address
->family
== AF_INET
)
605 r
= sd_netlink_message_append_in_addr(req
, IFA_ADDRESS
, &address
->in_addr_peer
.in
);
606 else if (address
->family
== AF_INET6
)
607 r
= sd_netlink_message_append_in6_addr(req
, IFA_ADDRESS
, &address
->in_addr_peer
.in6
);
609 return log_error_errno(r
, "Could not append IFA_ADDRESS attribute: %m");
611 if (address
->family
== AF_INET
) {
612 if (address
->prefixlen
<= 30) {
613 r
= sd_netlink_message_append_in_addr(req
, IFA_BROADCAST
, &address
->broadcast
);
615 return log_error_errno(r
, "Could not append IFA_BROADCAST attribute: %m");
620 if (address
->label
) {
621 r
= sd_netlink_message_append_string(req
, IFA_LABEL
, address
->label
);
623 return log_error_errno(r
, "Could not append IFA_LABEL attribute: %m");
626 r
= sd_netlink_message_append_cache_info(req
, IFA_CACHEINFO
,
629 return log_error_errno(r
, "Could not append IFA_CACHEINFO attribute: %m");
631 r
= address_establish(address
, link
);
635 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, callback
, link
, 0, NULL
);
637 address_release(address
);
638 return log_error_errno(r
, "Could not send rtnetlink message: %m");
643 r
= address_add(link
, address
->family
, &address
->in_addr
, address
->prefixlen
, NULL
);
645 address_release(address
);
646 return log_error_errno(r
, "Could not add address: %m");
652 int config_parse_broadcast(
654 const char *filename
,
657 unsigned section_line
,
664 Network
*network
= userdata
;
665 _cleanup_(address_freep
) Address
*n
= NULL
;
674 r
= address_new_static(network
, filename
, section_line
, &n
);
678 if (n
->family
== AF_INET6
) {
679 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Broadcast is not valid for IPv6 addresses, ignoring assignment: %s", rvalue
);
683 r
= in_addr_from_string(AF_INET
, rvalue
, (union in_addr_union
*) &n
->broadcast
);
685 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Broadcast is invalid, ignoring assignment: %s", rvalue
);
695 int config_parse_address(const char *unit
,
696 const char *filename
,
699 unsigned section_line
,
706 Network
*network
= userdata
;
707 _cleanup_(address_freep
) Address
*n
= NULL
;
708 const char *address
, *e
;
709 union in_addr_union buffer
;
718 if (streq(section
, "Network")) {
719 /* we are not in an Address section, so treat
720 * this as the special '0' section */
721 r
= address_new_static(network
, NULL
, 0, &n
);
723 r
= address_new_static(network
, filename
, section_line
, &n
);
728 /* Address=address/prefixlen */
731 e
= strchr(rvalue
, '/');
735 r
= safe_atou(e
+ 1, &i
);
737 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Prefix length is invalid, ignoring assignment: %s", e
+ 1);
741 n
->prefixlen
= (unsigned char) i
;
743 address
= strndupa(rvalue
, e
- rvalue
);
747 r
= in_addr_from_string_auto(address
, &f
, &buffer
);
749 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Address is invalid, ignoring assignment: %s", address
);
753 if (!e
&& f
== AF_INET
) {
754 r
= in4_addr_default_prefixlen(&buffer
.in
, &n
->prefixlen
);
756 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Prefix length not specified, and a default one cannot be deduced for '%s', ignoring assignment", address
);
761 if (n
->family
!= AF_UNSPEC
&& f
!= n
->family
) {
762 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Address is incompatible, ignoring assignment: %s", address
);
768 if (streq(lvalue
, "Address"))
771 n
->in_addr_peer
= buffer
;
773 if (n
->family
== AF_INET
&& n
->broadcast
.s_addr
== 0)
774 n
->broadcast
.s_addr
= n
->in_addr
.in
.s_addr
| htonl(0xfffffffflu
>> n
->prefixlen
);
781 int config_parse_label(
783 const char *filename
,
786 unsigned section_line
,
793 _cleanup_(address_freep
) Address
*n
= NULL
;
794 Network
*network
= userdata
;
803 r
= address_new_static(network
, filename
, section_line
, &n
);
807 if (!address_label_valid(rvalue
)) {
808 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Interface label is too long or invalid, ignoring assignment: %s", rvalue
);
812 r
= free_and_strdup(&n
->label
, rvalue
);
821 int config_parse_lifetime(const char *unit
,
822 const char *filename
,
825 unsigned section_line
,
831 Network
*network
= userdata
;
832 _cleanup_(address_freep
) Address
*n
= NULL
;
842 r
= address_new_static(network
, filename
, section_line
, &n
);
846 if (STR_IN_SET(rvalue
, "forever", "infinity")) {
847 n
->cinfo
.ifa_prefered
= CACHE_INFO_INFINITY_LIFE_TIME
;
853 r
= safe_atou(rvalue
, &k
);
855 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse PreferredLifetime, ignoring: %s", rvalue
);
860 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid PreferredLifetime value, ignoring: %d", k
);
862 n
->cinfo
.ifa_prefered
= k
;
869 int config_parse_address_flags(const char *unit
,
870 const char *filename
,
873 unsigned section_line
,
879 Network
*network
= userdata
;
880 _cleanup_(address_freep
) Address
*n
= NULL
;
889 r
= address_new_static(network
, filename
, section_line
, &n
);
893 r
= parse_boolean(rvalue
);
895 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse address flag, ignoring: %s", rvalue
);
899 if (streq(lvalue
, "HomeAddress"))
901 else if (streq(lvalue
, "DuplicateAddressDetection"))
902 n
->duplicate_address_detection
= r
;
903 else if (streq(lvalue
, "ManageTemporaryAddress"))
904 n
->manage_temporary_address
= r
;
905 else if (streq(lvalue
, "PrefixRoute"))
907 else if (streq(lvalue
, "AutoJoin"))
913 int config_parse_address_scope(const char *unit
,
914 const char *filename
,
917 unsigned section_line
,
923 Network
*network
= userdata
;
924 _cleanup_(address_freep
) Address
*n
= NULL
;
933 r
= address_new_static(network
, filename
, section_line
, &n
);
937 if (streq(rvalue
, "host"))
938 n
->scope
= RT_SCOPE_HOST
;
939 else if (streq(rvalue
, "link"))
940 n
->scope
= RT_SCOPE_LINK
;
941 else if (streq(rvalue
, "global"))
942 n
->scope
= RT_SCOPE_UNIVERSE
;
944 r
= safe_atou8(rvalue
, &n
->scope
);
946 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Could not parse address scope \"%s\", ignoring assignment: %m", rvalue
);
956 bool address_is_ready(const Address
*a
) {
959 if (a
->family
== AF_INET6
)
960 return !(a
->flags
& IFA_F_TENTATIVE
);
962 return !(a
->flags
& (IFA_F_TENTATIVE
| IFA_F_DEPRECATED
));