2 This file is part of systemd.
4 Copyright 2013 Tom Gundersen <teg@jklm.no>
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include "alloc-util.h"
23 #include "conf-parser.h"
24 #include "firewall-util.h"
25 #include "netlink-util.h"
26 #include "networkd-address.h"
27 #include "networkd-manager.h"
28 #include "parse-util.h"
30 #include "socket-util.h"
31 #include "string-util.h"
35 #define ADDRESSES_PER_LINK_MAX 2048U
36 #define STATIC_ADDRESSES_PER_NETWORK_MAX 1024U
38 int address_new(Address
**ret
) {
39 _cleanup_address_free_ Address
*address
= NULL
;
41 address
= new0(Address
, 1);
45 address
->family
= AF_UNSPEC
;
46 address
->scope
= RT_SCOPE_UNIVERSE
;
47 address
->cinfo
.ifa_prefered
= CACHE_INFO_INFINITY_LIFE_TIME
;
48 address
->cinfo
.ifa_valid
= CACHE_INFO_INFINITY_LIFE_TIME
;
56 int address_new_static(Network
*network
, const char *filename
, unsigned section_line
, Address
**ret
) {
57 _cleanup_network_config_section_free_ NetworkConfigSection
*n
= NULL
;
58 _cleanup_address_free_ Address
*address
= NULL
;
63 assert(!!filename
== (section_line
> 0));
66 r
= network_config_section_new(filename
, section_line
, &n
);
70 address
= hashmap_get(network
->addresses_by_section
, n
);
79 if (network
->n_static_addresses
>= STATIC_ADDRESSES_PER_NETWORK_MAX
)
82 r
= address_new(&address
);
90 hashmap_put(network
->addresses_by_section
, address
->section
, address
);
93 address
->network
= network
;
94 LIST_APPEND(addresses
, network
->static_addresses
, address
);
95 network
->n_static_addresses
++;
103 void address_free(Address
*address
) {
107 if (address
->network
) {
108 LIST_REMOVE(addresses
, address
->network
->static_addresses
, address
);
109 assert(address
->network
->n_static_addresses
> 0);
110 address
->network
->n_static_addresses
--;
112 if (address
->section
) {
113 hashmap_remove(address
->network
->addresses_by_section
, address
->section
);
114 network_config_section_free(address
->section
);
119 set_remove(address
->link
->addresses
, address
);
120 set_remove(address
->link
->addresses_foreign
, address
);
122 if (in_addr_equal(AF_INET6
, &address
->in_addr
, (const union in_addr_union
*) &address
->link
->ipv6ll_address
))
123 memzero(&address
->link
->ipv6ll_address
, sizeof(struct in6_addr
));
129 static void address_hash_func(const void *b
, struct siphash
*state
) {
130 const Address
*a
= b
;
134 siphash24_compress(&a
->family
, sizeof(a
->family
), state
);
138 siphash24_compress(&a
->prefixlen
, sizeof(a
->prefixlen
), state
);
141 if (a
->prefixlen
!= 0) {
144 if (a
->in_addr_peer
.in
.s_addr
!= 0)
145 prefix
= be32toh(a
->in_addr_peer
.in
.s_addr
) >> (32 - a
->prefixlen
);
147 prefix
= be32toh(a
->in_addr
.in
.s_addr
) >> (32 - a
->prefixlen
);
149 siphash24_compress(&prefix
, sizeof(prefix
), state
);
155 siphash24_compress(&a
->in_addr
, FAMILY_ADDRESS_SIZE(a
->family
), state
);
159 /* treat any other address family as AF_UNSPEC */
164 static int address_compare_func(const void *c1
, const void *c2
) {
165 const Address
*a1
= c1
, *a2
= c2
;
167 if (a1
->family
< a2
->family
)
169 if (a1
->family
> a2
->family
)
172 switch (a1
->family
) {
173 /* use the same notion of equality as the kernel does */
175 if (a1
->prefixlen
< a2
->prefixlen
)
177 if (a1
->prefixlen
> a2
->prefixlen
)
180 /* compare the peer prefixes */
181 if (a1
->prefixlen
!= 0) {
182 /* make sure we don't try to shift by 32.
183 * See ISO/IEC 9899:TC3 ยง 6.5.7.3. */
186 if (a1
->in_addr_peer
.in
.s_addr
!= 0)
187 b1
= be32toh(a1
->in_addr_peer
.in
.s_addr
) >> (32 - a1
->prefixlen
);
189 b1
= be32toh(a1
->in_addr
.in
.s_addr
) >> (32 - a1
->prefixlen
);
191 if (a2
->in_addr_peer
.in
.s_addr
!= 0)
192 b2
= be32toh(a2
->in_addr_peer
.in
.s_addr
) >> (32 - a1
->prefixlen
);
194 b2
= be32toh(a2
->in_addr
.in
.s_addr
) >> (32 - a1
->prefixlen
);
204 return memcmp(&a1
->in_addr
, &a2
->in_addr
, FAMILY_ADDRESS_SIZE(a1
->family
));
206 /* treat any other address family as AF_UNSPEC */
211 static const struct hash_ops address_hash_ops
= {
212 .hash
= address_hash_func
,
213 .compare
= address_compare_func
216 bool address_equal(Address
*a1
, Address
*a2
) {
223 return address_compare_func(a1
, a2
) == 0;
226 static int address_establish(Address
*address
, Link
*link
) {
233 masq
= link
->network
&&
234 link
->network
->ip_masquerade
&&
235 address
->family
== AF_INET
&&
236 address
->scope
< RT_SCOPE_LINK
;
238 /* Add firewall entry if this is requested */
239 if (address
->ip_masquerade_done
!= masq
) {
240 union in_addr_union masked
= address
->in_addr
;
241 in_addr_mask(address
->family
, &masked
, address
->prefixlen
);
243 r
= fw_add_masquerade(masq
, AF_INET
, 0, &masked
, address
->prefixlen
, NULL
, NULL
, 0);
245 log_link_warning_errno(link
, r
, "Could not enable IP masquerading: %m");
247 address
->ip_masquerade_done
= masq
;
253 static int address_add_internal(Link
*link
, Set
**addresses
,
255 const union in_addr_union
*in_addr
,
256 unsigned char prefixlen
,
258 _cleanup_address_free_ Address
*address
= NULL
;
265 r
= address_new(&address
);
269 address
->family
= family
;
270 address
->in_addr
= *in_addr
;
271 address
->prefixlen
= prefixlen
;
272 /* Consider address tentative until we get the real flags from the kernel */
273 address
->flags
= IFA_F_TENTATIVE
;
275 r
= set_ensure_allocated(addresses
, &address_hash_ops
);
279 r
= set_put(*addresses
, address
);
283 address
->link
= link
;
293 int address_add_foreign(Link
*link
, int family
, const union in_addr_union
*in_addr
, unsigned char prefixlen
, Address
**ret
) {
294 return address_add_internal(link
, &link
->addresses_foreign
, family
, in_addr
, prefixlen
, ret
);
297 int address_add(Link
*link
, int family
, const union in_addr_union
*in_addr
, unsigned char prefixlen
, Address
**ret
) {
301 r
= address_get(link
, family
, in_addr
, prefixlen
, &address
);
303 /* Address does not exist, create a new one */
304 r
= address_add_internal(link
, &link
->addresses
, family
, in_addr
, prefixlen
, &address
);
308 /* Take over a foreign address */
309 r
= set_ensure_allocated(&link
->addresses
, &address_hash_ops
);
313 r
= set_put(link
->addresses
, address
);
317 set_remove(link
->addresses_foreign
, address
);
319 /* Already exists, do nothing */
330 static int address_release(Address
*address
) {
334 assert(address
->link
);
336 /* Remove masquerading firewall entry if it was added */
337 if (address
->ip_masquerade_done
) {
338 union in_addr_union masked
= address
->in_addr
;
339 in_addr_mask(address
->family
, &masked
, address
->prefixlen
);
341 r
= fw_add_masquerade(false, AF_INET
, 0, &masked
, address
->prefixlen
, NULL
, NULL
, 0);
343 log_link_warning_errno(address
->link
, r
, "Failed to disable IP masquerading: %m");
345 address
->ip_masquerade_done
= false;
355 const struct ifa_cacheinfo
*cinfo
) {
362 assert_return(address
->link
, 1);
364 if (IN_SET(address
->link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
367 ready
= address_is_ready(address
);
369 address
->flags
= flags
;
370 address
->scope
= scope
;
371 address
->cinfo
= *cinfo
;
373 link_update_operstate(address
->link
);
375 if (!ready
&& address_is_ready(address
)) {
376 link_check_ready(address
->link
);
378 if (address
->family
== AF_INET6
&&
379 in_addr_is_link_local(AF_INET6
, &address
->in_addr
) > 0 &&
380 in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &address
->link
->ipv6ll_address
) > 0) {
382 r
= link_ipv6ll_gained(address
->link
, &address
->in_addr
.in6
);
391 int address_drop(Address
*address
) {
397 ready
= address_is_ready(address
);
398 link
= address
->link
;
400 address_release(address
);
401 address_free(address
);
403 link_update_operstate(link
);
406 link_check_ready(link
);
411 int address_get(Link
*link
,
413 const union in_addr_union
*in_addr
,
414 unsigned char prefixlen
,
417 Address address
, *existing
;
422 address
= (Address
) {
425 .prefixlen
= prefixlen
,
428 existing
= set_get(link
->addresses
, &address
);
435 existing
= set_get(link
->addresses_foreign
, &address
);
448 sd_netlink_message_handler_t callback
) {
450 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
454 assert(address
->family
== AF_INET
|| address
->family
== AF_INET6
);
456 assert(link
->ifindex
> 0);
457 assert(link
->manager
);
458 assert(link
->manager
->rtnl
);
460 r
= sd_rtnl_message_new_addr(link
->manager
->rtnl
, &req
, RTM_DELADDR
,
461 link
->ifindex
, address
->family
);
463 return log_error_errno(r
, "Could not allocate RTM_DELADDR message: %m");
465 r
= sd_rtnl_message_addr_set_prefixlen(req
, address
->prefixlen
);
467 return log_error_errno(r
, "Could not set prefixlen: %m");
469 if (address
->family
== AF_INET
)
470 r
= sd_netlink_message_append_in_addr(req
, IFA_LOCAL
, &address
->in_addr
.in
);
471 else if (address
->family
== AF_INET6
)
472 r
= sd_netlink_message_append_in6_addr(req
, IFA_LOCAL
, &address
->in_addr
.in6
);
474 return log_error_errno(r
, "Could not append IFA_LOCAL attribute: %m");
476 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, callback
, link
, 0, NULL
);
478 return log_error_errno(r
, "Could not send rtnetlink message: %m");
485 static int address_acquire(Link
*link
, Address
*original
, Address
**ret
) {
486 union in_addr_union in_addr
= {};
487 struct in_addr broadcast
= {};
488 _cleanup_address_free_ Address
*na
= NULL
;
495 /* Something useful was configured? just use it */
496 if (in_addr_is_null(original
->family
, &original
->in_addr
) <= 0)
499 /* The address is configured to be 0.0.0.0 or [::] by the user?
500 * Then let's acquire something more useful from the pool. */
501 r
= manager_address_pool_acquire(link
->manager
, original
->family
, original
->prefixlen
, &in_addr
);
503 return log_link_error_errno(link
, r
, "Failed to acquire address from pool: %m");
505 log_link_error(link
, "Couldn't find free address for interface, all taken.");
509 if (original
->family
== AF_INET
) {
510 /* Pick first address in range for ourselves ... */
511 in_addr
.in
.s_addr
= in_addr
.in
.s_addr
| htobe32(1);
513 /* .. and use last as broadcast address */
514 broadcast
.s_addr
= in_addr
.in
.s_addr
| htobe32(0xFFFFFFFFUL
>> original
->prefixlen
);
515 } else if (original
->family
== AF_INET6
)
516 in_addr
.in6
.s6_addr
[15] |= 1;
518 r
= address_new(&na
);
522 na
->family
= original
->family
;
523 na
->prefixlen
= original
->prefixlen
;
524 na
->scope
= original
->scope
;
525 na
->cinfo
= original
->cinfo
;
527 if (original
->label
) {
528 na
->label
= strdup(original
->label
);
533 na
->broadcast
= broadcast
;
534 na
->in_addr
= in_addr
;
536 LIST_PREPEND(addresses
, link
->pool_addresses
, na
);
544 int address_configure(
547 sd_netlink_message_handler_t callback
,
550 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
554 assert(address
->family
== AF_INET
|| address
->family
== AF_INET6
);
556 assert(link
->ifindex
> 0);
557 assert(link
->manager
);
558 assert(link
->manager
->rtnl
);
560 /* If this is a new address, then refuse adding more than the limit */
561 if (address_get(link
, address
->family
, &address
->in_addr
, address
->prefixlen
, NULL
) <= 0 &&
562 set_size(link
->addresses
) >= ADDRESSES_PER_LINK_MAX
)
565 r
= address_acquire(link
, address
, &address
);
570 r
= sd_rtnl_message_new_addr_update(link
->manager
->rtnl
, &req
,
571 link
->ifindex
, address
->family
);
573 r
= sd_rtnl_message_new_addr(link
->manager
->rtnl
, &req
, RTM_NEWADDR
,
574 link
->ifindex
, address
->family
);
576 return log_error_errno(r
, "Could not allocate RTM_NEWADDR message: %m");
578 r
= sd_rtnl_message_addr_set_prefixlen(req
, address
->prefixlen
);
580 return log_error_errno(r
, "Could not set prefixlen: %m");
582 address
->flags
|= IFA_F_PERMANENT
;
584 if (address
->home_address
)
585 address
->flags
|= IFA_F_HOMEADDRESS
;
587 if (address
->duplicate_address_detection
)
588 address
->flags
|= IFA_F_NODAD
;
590 if (address
->manage_temporary_address
)
591 address
->flags
|= IFA_F_MANAGETEMPADDR
;
593 if (address
->prefix_route
)
594 address
->flags
|= IFA_F_NOPREFIXROUTE
;
596 if (address
->autojoin
)
597 address
->flags
|= IFA_F_MCAUTOJOIN
;
599 r
= sd_rtnl_message_addr_set_flags(req
, (address
->flags
& 0xff));
601 return log_error_errno(r
, "Could not set flags: %m");
603 if (address
->flags
& ~0xff) {
604 r
= sd_netlink_message_append_u32(req
, IFA_FLAGS
, address
->flags
);
606 return log_error_errno(r
, "Could not set extended flags: %m");
609 r
= sd_rtnl_message_addr_set_scope(req
, address
->scope
);
611 return log_error_errno(r
, "Could not set scope: %m");
613 if (address
->family
== AF_INET
)
614 r
= sd_netlink_message_append_in_addr(req
, IFA_LOCAL
, &address
->in_addr
.in
);
615 else if (address
->family
== AF_INET6
)
616 r
= sd_netlink_message_append_in6_addr(req
, IFA_LOCAL
, &address
->in_addr
.in6
);
618 return log_error_errno(r
, "Could not append IFA_LOCAL attribute: %m");
620 if (!in_addr_is_null(address
->family
, &address
->in_addr_peer
)) {
621 if (address
->family
== AF_INET
)
622 r
= sd_netlink_message_append_in_addr(req
, IFA_ADDRESS
, &address
->in_addr_peer
.in
);
623 else if (address
->family
== AF_INET6
)
624 r
= sd_netlink_message_append_in6_addr(req
, IFA_ADDRESS
, &address
->in_addr_peer
.in6
);
626 return log_error_errno(r
, "Could not append IFA_ADDRESS attribute: %m");
628 if (address
->family
== AF_INET
) {
629 r
= sd_netlink_message_append_in_addr(req
, IFA_BROADCAST
, &address
->broadcast
);
631 return log_error_errno(r
, "Could not append IFA_BROADCAST attribute: %m");
635 if (address
->label
) {
636 r
= sd_netlink_message_append_string(req
, IFA_LABEL
, address
->label
);
638 return log_error_errno(r
, "Could not append IFA_LABEL attribute: %m");
641 r
= sd_netlink_message_append_cache_info(req
, IFA_CACHEINFO
,
644 return log_error_errno(r
, "Could not append IFA_CACHEINFO attribute: %m");
646 r
= address_establish(address
, link
);
650 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, callback
, link
, 0, NULL
);
652 address_release(address
);
653 return log_error_errno(r
, "Could not send rtnetlink message: %m");
658 r
= address_add(link
, address
->family
, &address
->in_addr
, address
->prefixlen
, NULL
);
660 address_release(address
);
661 return log_error_errno(r
, "Could not add address: %m");
667 int config_parse_broadcast(
669 const char *filename
,
672 unsigned section_line
,
679 Network
*network
= userdata
;
680 _cleanup_address_free_ Address
*n
= NULL
;
689 r
= address_new_static(network
, filename
, section_line
, &n
);
693 if (n
->family
== AF_INET6
) {
694 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Broadcast is not valid for IPv6 addresses, ignoring assignment: %s", rvalue
);
698 r
= in_addr_from_string(AF_INET
, rvalue
, (union in_addr_union
*) &n
->broadcast
);
700 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Broadcast is invalid, ignoring assignment: %s", rvalue
);
710 int config_parse_address(const char *unit
,
711 const char *filename
,
714 unsigned section_line
,
721 Network
*network
= userdata
;
722 _cleanup_address_free_ Address
*n
= NULL
;
723 const char *address
, *e
;
724 union in_addr_union buffer
;
733 if (streq(section
, "Network")) {
734 /* we are not in an Address section, so treat
735 * this as the special '0' section */
736 r
= address_new_static(network
, NULL
, 0, &n
);
738 r
= address_new_static(network
, filename
, section_line
, &n
);
743 /* Address=address/prefixlen */
746 e
= strchr(rvalue
, '/');
750 r
= safe_atou(e
+ 1, &i
);
752 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Prefix length is invalid, ignoring assignment: %s", e
+ 1);
756 n
->prefixlen
= (unsigned char) i
;
758 address
= strndupa(rvalue
, e
- rvalue
);
762 r
= in_addr_from_string_auto(address
, &f
, &buffer
);
764 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Address is invalid, ignoring assignment: %s", address
);
768 if (!e
&& f
== AF_INET
) {
769 r
= in_addr_default_prefixlen(&buffer
.in
, &n
->prefixlen
);
771 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Prefix length not specified, and a default one can not be deduced for '%s', ignoring assignment", address
);
776 if (n
->family
!= AF_UNSPEC
&& f
!= n
->family
) {
777 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Address is incompatible, ignoring assignment: %s", address
);
783 if (streq(lvalue
, "Address"))
786 n
->in_addr_peer
= buffer
;
788 if (n
->family
== AF_INET
&& n
->broadcast
.s_addr
== 0)
789 n
->broadcast
.s_addr
= n
->in_addr
.in
.s_addr
| htonl(0xfffffffflu
>> n
->prefixlen
);
796 int config_parse_label(
798 const char *filename
,
801 unsigned section_line
,
808 _cleanup_address_free_ Address
*n
= NULL
;
809 Network
*network
= userdata
;
818 r
= address_new_static(network
, filename
, section_line
, &n
);
822 if (strlen(rvalue
) >= IFNAMSIZ
) {
823 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Interface label is too long, ignoring assignment: %s", rvalue
);
827 r
= free_and_strdup(&n
->label
, rvalue
);
836 int config_parse_lifetime(const char *unit
,
837 const char *filename
,
840 unsigned section_line
,
846 Network
*network
= userdata
;
847 _cleanup_address_free_ Address
*n
= NULL
;
857 r
= address_new_static(network
, filename
, section_line
, &n
);
861 if (STR_IN_SET(rvalue
, "forever", "infinity")) {
862 n
->cinfo
.ifa_prefered
= CACHE_INFO_INFINITY_LIFE_TIME
;
868 r
= safe_atou(rvalue
, &k
);
870 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse PreferredLifetime, ignoring: %s", rvalue
);
875 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid PreferredLifetime value, ignoring: %d", k
);
877 n
->cinfo
.ifa_prefered
= k
;
884 int config_parse_address_flags(const char *unit
,
885 const char *filename
,
888 unsigned section_line
,
894 Network
*network
= userdata
;
895 _cleanup_address_free_ Address
*n
= NULL
;
904 r
= address_new_static(network
, filename
, section_line
, &n
);
908 r
= parse_boolean(rvalue
);
910 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse address flag, ignoring: %s", rvalue
);
914 if (streq(lvalue
, "HomeAddress"))
916 else if (streq(lvalue
, "DuplicateAddressDetection"))
917 n
->duplicate_address_detection
= r
;
918 else if (streq(lvalue
, "ManageTemporaryAddress"))
919 n
->manage_temporary_address
= r
;
920 else if (streq(lvalue
, "PrefixRoute"))
922 else if (streq(lvalue
, "AutoJoin"))
928 bool address_is_ready(const Address
*a
) {
931 return !(a
->flags
& (IFA_F_TENTATIVE
| IFA_F_DEPRECATED
));