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/>.
20 #include <netinet/ether.h>
24 #include "alloc-util.h"
26 #include "dhcp-lease-internal.h"
29 #include "netlink-util.h"
30 #include "network-internal.h"
31 #include "networkd-lldp-tx.h"
32 #include "networkd-ndisc.h"
35 #include "socket-util.h"
36 #include "stdio-util.h"
37 #include "string-table.h"
38 #include "udev-util.h"
42 static bool link_dhcp6_enabled(Link
*link
) {
45 if (!socket_ipv6_is_supported())
48 if (link
->flags
& IFF_LOOPBACK
)
54 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV6
;
57 static bool link_dhcp4_enabled(Link
*link
) {
60 if (link
->flags
& IFF_LOOPBACK
)
66 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV4
;
69 static bool link_dhcp4_server_enabled(Link
*link
) {
72 if (link
->flags
& IFF_LOOPBACK
)
78 return link
->network
->dhcp_server
;
81 static bool link_ipv4ll_enabled(Link
*link
) {
84 if (link
->flags
& IFF_LOOPBACK
)
90 return link
->network
->link_local
& ADDRESS_FAMILY_IPV4
;
93 static bool link_ipv6ll_enabled(Link
*link
) {
96 if (!socket_ipv6_is_supported())
99 if (link
->flags
& IFF_LOOPBACK
)
105 return link
->network
->link_local
& ADDRESS_FAMILY_IPV6
;
108 static bool link_ipv6_enabled(Link
*link
) {
111 if (!socket_ipv6_is_supported())
114 if (link
->network
->bridge
)
117 /* DHCPv6 client will not be started if no IPv6 link-local address is configured. */
118 return link_ipv6ll_enabled(link
) || network_has_static_ipv6_addresses(link
->network
);
121 static bool link_lldp_rx_enabled(Link
*link
) {
124 if (link
->flags
& IFF_LOOPBACK
)
127 if (link
->iftype
!= ARPHRD_ETHER
)
133 if (link
->network
->bridge
)
136 return link
->network
->lldp_mode
!= LLDP_MODE_NO
;
139 static bool link_lldp_emit_enabled(Link
*link
) {
142 if (link
->flags
& IFF_LOOPBACK
)
145 if (link
->iftype
!= ARPHRD_ETHER
)
151 return link
->network
->lldp_emit
!= LLDP_EMIT_NO
;
154 static bool link_ipv4_forward_enabled(Link
*link
) {
157 if (link
->flags
& IFF_LOOPBACK
)
163 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
166 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV4
;
169 static bool link_ipv6_forward_enabled(Link
*link
) {
172 if (!socket_ipv6_is_supported())
175 if (link
->flags
& IFF_LOOPBACK
)
181 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
184 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV6
;
187 static bool link_proxy_arp_enabled(Link
*link
) {
190 if (link
->flags
& IFF_LOOPBACK
)
196 if (link
->network
->proxy_arp
< 0)
202 static bool link_ipv6_accept_ra_enabled(Link
*link
) {
205 if (!socket_ipv6_is_supported())
208 if (link
->flags
& IFF_LOOPBACK
)
214 /* If unset use system default (enabled if local forwarding is disabled.
215 * disabled if local forwarding is enabled).
216 * If set, ignore or enforce RA independent of local forwarding state.
218 if (link
->network
->ipv6_accept_ra
< 0)
219 /* default to accept RA if ip_forward is disabled and ignore RA if ip_forward is enabled */
220 return !link_ipv6_forward_enabled(link
);
221 else if (link
->network
->ipv6_accept_ra
> 0)
222 /* accept RA even if ip_forward is enabled */
229 static IPv6PrivacyExtensions
link_ipv6_privacy_extensions(Link
*link
) {
232 if (!socket_ipv6_is_supported())
233 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
235 if (link
->flags
& IFF_LOOPBACK
)
236 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
239 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
241 return link
->network
->ipv6_privacy_extensions
;
244 static int link_enable_ipv6(Link
*link
) {
245 const char *p
= NULL
;
249 if (link
->flags
& IFF_LOOPBACK
)
252 disabled
= !link_ipv6_enabled(link
);
254 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/disable_ipv6");
256 r
= write_string_file(p
, one_zero(disabled
), WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
258 log_link_warning_errno(link
, r
, "Cannot %s IPv6 for interface %s: %m", disabled
? "disable" : "enable", link
->ifname
);
260 log_link_info(link
, "IPv6 %sd for interface: %m", enable_disable(!disabled
));
265 void link_update_operstate(Link
*link
) {
266 LinkOperationalState operstate
;
269 if (link
->kernel_operstate
== IF_OPER_DORMANT
)
270 operstate
= LINK_OPERSTATE_DORMANT
;
271 else if (link_has_carrier(link
)) {
273 uint8_t scope
= RT_SCOPE_NOWHERE
;
276 /* if we have carrier, check what addresses we have */
277 SET_FOREACH(address
, link
->addresses
, i
) {
278 if (!address_is_ready(address
))
281 if (address
->scope
< scope
)
282 scope
= address
->scope
;
285 /* for operstate we also take foreign addresses into account */
286 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
287 if (!address_is_ready(address
))
290 if (address
->scope
< scope
)
291 scope
= address
->scope
;
294 if (scope
< RT_SCOPE_SITE
)
295 /* universally accessible addresses found */
296 operstate
= LINK_OPERSTATE_ROUTABLE
;
297 else if (scope
< RT_SCOPE_HOST
)
298 /* only link or site local addresses found */
299 operstate
= LINK_OPERSTATE_DEGRADED
;
301 /* no useful addresses found */
302 operstate
= LINK_OPERSTATE_CARRIER
;
303 } else if (link
->flags
& IFF_UP
)
304 operstate
= LINK_OPERSTATE_NO_CARRIER
;
306 operstate
= LINK_OPERSTATE_OFF
;
308 if (link
->operstate
!= operstate
) {
309 link
->operstate
= operstate
;
310 link_send_changed(link
, "OperationalState", NULL
);
315 #define FLAG_STRING(string, flag, old, new) \
316 (((old ^ new) & flag) \
317 ? ((old & flag) ? (" -" string) : (" +" string)) \
320 static int link_update_flags(Link
*link
, sd_netlink_message
*m
) {
321 unsigned flags
, unknown_flags_added
, unknown_flags_removed
, unknown_flags
;
327 r
= sd_rtnl_message_link_get_flags(m
, &flags
);
329 return log_link_warning_errno(link
, r
, "Could not get link flags: %m");
331 r
= sd_netlink_message_read_u8(m
, IFLA_OPERSTATE
, &operstate
);
333 /* if we got a message without operstate, take it to mean
334 the state was unchanged */
335 operstate
= link
->kernel_operstate
;
337 if ((link
->flags
== flags
) && (link
->kernel_operstate
== operstate
))
340 if (link
->flags
!= flags
) {
341 log_link_debug(link
, "Flags change:%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
342 FLAG_STRING("LOOPBACK", IFF_LOOPBACK
, link
->flags
, flags
),
343 FLAG_STRING("MASTER", IFF_MASTER
, link
->flags
, flags
),
344 FLAG_STRING("SLAVE", IFF_SLAVE
, link
->flags
, flags
),
345 FLAG_STRING("UP", IFF_UP
, link
->flags
, flags
),
346 FLAG_STRING("DORMANT", IFF_DORMANT
, link
->flags
, flags
),
347 FLAG_STRING("LOWER_UP", IFF_LOWER_UP
, link
->flags
, flags
),
348 FLAG_STRING("RUNNING", IFF_RUNNING
, link
->flags
, flags
),
349 FLAG_STRING("MULTICAST", IFF_MULTICAST
, link
->flags
, flags
),
350 FLAG_STRING("BROADCAST", IFF_BROADCAST
, link
->flags
, flags
),
351 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT
, link
->flags
, flags
),
352 FLAG_STRING("PROMISC", IFF_PROMISC
, link
->flags
, flags
),
353 FLAG_STRING("ALLMULTI", IFF_ALLMULTI
, link
->flags
, flags
),
354 FLAG_STRING("PORTSEL", IFF_PORTSEL
, link
->flags
, flags
),
355 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA
, link
->flags
, flags
),
356 FLAG_STRING("DYNAMIC", IFF_DYNAMIC
, link
->flags
, flags
),
357 FLAG_STRING("NOARP", IFF_NOARP
, link
->flags
, flags
),
358 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS
, link
->flags
, flags
),
359 FLAG_STRING("DEBUG", IFF_DEBUG
, link
->flags
, flags
),
360 FLAG_STRING("ECHO", IFF_ECHO
, link
->flags
, flags
));
362 unknown_flags
= ~(IFF_LOOPBACK
| IFF_MASTER
| IFF_SLAVE
| IFF_UP
|
363 IFF_DORMANT
| IFF_LOWER_UP
| IFF_RUNNING
|
364 IFF_MULTICAST
| IFF_BROADCAST
| IFF_POINTOPOINT
|
365 IFF_PROMISC
| IFF_ALLMULTI
| IFF_PORTSEL
|
366 IFF_AUTOMEDIA
| IFF_DYNAMIC
| IFF_NOARP
|
367 IFF_NOTRAILERS
| IFF_DEBUG
| IFF_ECHO
);
368 unknown_flags_added
= ((link
->flags
^ flags
) & flags
& unknown_flags
);
369 unknown_flags_removed
= ((link
->flags
^ flags
) & link
->flags
& unknown_flags
);
371 /* link flags are currently at most 18 bits, let's align to
373 if (unknown_flags_added
)
375 "Unknown link flags gained: %#.5x (ignoring)",
376 unknown_flags_added
);
378 if (unknown_flags_removed
)
380 "Unknown link flags lost: %#.5x (ignoring)",
381 unknown_flags_removed
);
385 link
->kernel_operstate
= operstate
;
387 link_update_operstate(link
);
392 static int link_new(Manager
*manager
, sd_netlink_message
*message
, Link
**ret
) {
393 _cleanup_link_unref_ Link
*link
= NULL
;
395 const char *ifname
, *kind
= NULL
;
397 unsigned short iftype
;
403 /* check for link kind */
404 r
= sd_netlink_message_enter_container(message
, IFLA_LINKINFO
);
406 (void)sd_netlink_message_read_string(message
, IFLA_INFO_KIND
, &kind
);
407 r
= sd_netlink_message_exit_container(message
);
412 r
= sd_netlink_message_get_type(message
, &type
);
415 else if (type
!= RTM_NEWLINK
)
418 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
421 else if (ifindex
<= 0)
424 r
= sd_rtnl_message_link_get_type(message
, &iftype
);
428 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &ifname
);
432 link
= new0(Link
, 1);
437 link
->manager
= manager
;
438 link
->state
= LINK_STATE_PENDING
;
439 link
->rtnl_extended_attrs
= true;
440 link
->ifindex
= ifindex
;
441 link
->iftype
= iftype
;
442 link
->ifname
= strdup(ifname
);
447 link
->kind
= strdup(kind
);
452 r
= sd_netlink_message_read_ether_addr(message
, IFLA_ADDRESS
, &link
->mac
);
454 log_link_debug_errno(link
, r
, "MAC address not found for new device, continuing without");
456 if (asprintf(&link
->state_file
, "/run/systemd/netif/links/%d", link
->ifindex
) < 0)
459 if (asprintf(&link
->lease_file
, "/run/systemd/netif/leases/%d", link
->ifindex
) < 0)
462 if (asprintf(&link
->lldp_file
, "/run/systemd/netif/lldp/%d", link
->ifindex
) < 0)
465 r
= hashmap_ensure_allocated(&manager
->links
, NULL
);
469 r
= hashmap_put(manager
->links
, INT_TO_PTR(link
->ifindex
), link
);
473 r
= link_update_flags(link
, message
);
483 static void link_free(Link
*link
) {
491 while (!set_isempty(link
->addresses
))
492 address_free(set_first(link
->addresses
));
494 while (!set_isempty(link
->addresses_foreign
))
495 address_free(set_first(link
->addresses_foreign
));
497 link
->addresses
= set_free(link
->addresses
);
499 link
->addresses_foreign
= set_free(link
->addresses_foreign
);
501 while ((address
= link
->pool_addresses
)) {
502 LIST_REMOVE(addresses
, link
->pool_addresses
, address
);
503 address_free(address
);
506 sd_dhcp_server_unref(link
->dhcp_server
);
507 sd_dhcp_client_unref(link
->dhcp_client
);
508 sd_dhcp_lease_unref(link
->dhcp_lease
);
510 link_lldp_emit_stop(link
);
512 free(link
->lease_file
);
514 sd_lldp_unref(link
->lldp
);
515 free(link
->lldp_file
);
517 sd_ipv4ll_unref(link
->ipv4ll
);
518 sd_dhcp6_client_unref(link
->dhcp6_client
);
519 sd_ndisc_unref(link
->ndisc
);
521 set_free_free(link
->ndisc_rdnss
);
522 set_free_free(link
->ndisc_dnssl
);
525 hashmap_remove(link
->manager
->links
, INT_TO_PTR(link
->ifindex
));
531 (void)unlink(link
->state_file
);
532 free(link
->state_file
);
534 udev_device_unref(link
->udev_device
);
536 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
)
537 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(carrier
->ifindex
));
538 hashmap_free(link
->bound_to_links
);
540 HASHMAP_FOREACH (carrier
, link
->bound_by_links
, i
)
541 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(carrier
->ifindex
));
542 hashmap_free(link
->bound_by_links
);
547 Link
*link_unref(Link
*link
) {
551 assert(link
->n_ref
> 0);
563 Link
*link_ref(Link
*link
) {
567 assert(link
->n_ref
> 0);
574 int link_get(Manager
*m
, int ifindex
, Link
**ret
) {
581 link
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
590 static void link_set_state(Link
*link
, LinkState state
) {
593 if (link
->state
== state
)
598 link_send_changed(link
, "AdministrativeState", NULL
);
601 static void link_enter_unmanaged(Link
*link
) {
604 log_link_debug(link
, "Unmanaged");
606 link_set_state(link
, LINK_STATE_UNMANAGED
);
611 static int link_stop_clients(Link
*link
) {
615 assert(link
->manager
);
616 assert(link
->manager
->event
);
618 if (link
->dhcp_client
) {
619 k
= sd_dhcp_client_stop(link
->dhcp_client
);
621 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv4 client: %m");
625 k
= sd_ipv4ll_stop(link
->ipv4ll
);
627 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 link-local: %m");
630 if (link
->dhcp6_client
) {
631 k
= sd_dhcp6_client_stop(link
->dhcp6_client
);
633 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv6 client: %m");
637 k
= sd_ndisc_stop(link
->ndisc
);
639 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Discovery: %m");
642 link_lldp_emit_stop(link
);
646 void link_enter_failed(Link
*link
) {
649 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
652 log_link_warning(link
, "Failed");
654 link_set_state(link
, LINK_STATE_FAILED
);
656 link_stop_clients(link
);
661 static Address
* link_find_dhcp_server_address(Link
*link
) {
665 assert(link
->network
);
667 /* The first statically configured address if there is any */
668 LIST_FOREACH(addresses
, address
, link
->network
->static_addresses
) {
670 if (address
->family
!= AF_INET
)
673 if (in_addr_is_null(address
->family
, &address
->in_addr
))
679 /* If that didn't work, find a suitable address we got from the pool */
680 LIST_FOREACH(addresses
, address
, link
->pool_addresses
) {
681 if (address
->family
!= AF_INET
)
690 static int link_enter_configured(Link
*link
) {
692 assert(link
->network
);
693 assert(link
->state
== LINK_STATE_SETTING_ROUTES
);
695 log_link_info(link
, "Configured");
697 link_set_state(link
, LINK_STATE_CONFIGURED
);
704 void link_check_ready(Link
*link
) {
710 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
716 if (!link
->static_configured
)
719 if (link_ipv4ll_enabled(link
))
720 if (!link
->ipv4ll_address
||
724 if (link_ipv6ll_enabled(link
))
725 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) > 0)
728 if ((link_dhcp4_enabled(link
) && !link_dhcp6_enabled(link
) &&
729 !link
->dhcp4_configured
) ||
730 (link_dhcp6_enabled(link
) && !link_dhcp4_enabled(link
) &&
731 !link
->dhcp6_configured
) ||
732 (link_dhcp4_enabled(link
) && link_dhcp6_enabled(link
) &&
733 !link
->dhcp4_configured
&& !link
->dhcp6_configured
))
736 if (link_ipv6_accept_ra_enabled(link
) && !link
->ndisc_configured
)
739 SET_FOREACH(a
, link
->addresses
, i
)
740 if (!address_is_ready(a
))
743 if (link
->state
!= LINK_STATE_CONFIGURED
)
744 link_enter_configured(link
);
749 static int route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
750 _cleanup_link_unref_ Link
*link
= userdata
;
753 assert(link
->link_messages
> 0);
754 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
755 LINK_STATE_SETTING_ROUTES
, LINK_STATE_FAILED
,
758 link
->link_messages
--;
760 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
763 r
= sd_netlink_message_get_errno(m
);
764 if (r
< 0 && r
!= -EEXIST
)
765 log_link_warning_errno(link
, r
, "Could not set route: %m");
767 if (link
->link_messages
== 0) {
768 log_link_debug(link
, "Routes set");
769 link
->static_configured
= true;
770 link_check_ready(link
);
776 static int link_enter_set_routes(Link
*link
) {
781 assert(link
->network
);
782 assert(link
->state
== LINK_STATE_SETTING_ADDRESSES
);
784 link_set_state(link
, LINK_STATE_SETTING_ROUTES
);
786 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
787 r
= route_configure(rt
, link
, route_handler
);
789 log_link_warning_errno(link
, r
, "Could not set routes: %m");
790 link_enter_failed(link
);
794 link
->link_messages
++;
797 if (link
->link_messages
== 0) {
798 link
->static_configured
= true;
799 link_check_ready(link
);
801 log_link_debug(link
, "Setting routes");
806 int link_route_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
807 _cleanup_link_unref_ Link
*link
= userdata
;
812 assert(link
->ifname
);
814 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
817 r
= sd_netlink_message_get_errno(m
);
818 if (r
< 0 && r
!= -ESRCH
)
819 log_link_warning_errno(link
, r
, "Could not drop route: %m");
824 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
825 _cleanup_link_unref_ Link
*link
= userdata
;
831 assert(link
->ifname
);
832 assert(link
->link_messages
> 0);
833 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
834 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
836 link
->link_messages
--;
838 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
841 r
= sd_netlink_message_get_errno(m
);
842 if (r
< 0 && r
!= -EEXIST
)
843 log_link_warning_errno(link
, r
, "could not set address: %m");
845 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
847 if (link
->link_messages
== 0) {
848 log_link_debug(link
, "Addresses set");
849 link_enter_set_routes(link
);
855 static int link_push_dns_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
856 _cleanup_free_
struct in_addr
*addresses
= NULL
;
857 size_t n_addresses
= 0, n_allocated
= 0;
860 log_debug("Copying DNS server information from %s", link
->ifname
);
865 STRV_FOREACH(a
, link
->network
->dns
) {
868 /* Only look for IPv4 addresses */
869 if (inet_pton(AF_INET
, *a
, &ia
) <= 0)
872 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
875 addresses
[n_addresses
++] = ia
;
878 if (link
->network
->dhcp_use_dns
&&
880 const struct in_addr
*da
= NULL
;
883 n
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &da
);
886 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
889 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
894 if (n_addresses
<= 0)
897 return sd_dhcp_server_set_dns(s
, addresses
, n_addresses
);
900 static int link_push_ntp_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
901 _cleanup_free_
struct in_addr
*addresses
= NULL
;
902 size_t n_addresses
= 0, n_allocated
= 0;
908 log_debug("Copying NTP server information from %s", link
->ifname
);
910 STRV_FOREACH(a
, link
->network
->ntp
) {
913 /* Only look for IPv4 addresses */
914 if (inet_pton(AF_INET
, *a
, &ia
) <= 0)
917 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
920 addresses
[n_addresses
++] = ia
;
923 if (link
->network
->dhcp_use_ntp
&&
925 const struct in_addr
*da
= NULL
;
928 n
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &da
);
931 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
934 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
939 if (n_addresses
<= 0)
942 return sd_dhcp_server_set_ntp(s
, addresses
, n_addresses
);
945 static int link_enter_set_addresses(Link
*link
) {
950 assert(link
->network
);
951 assert(link
->state
!= _LINK_STATE_INVALID
);
953 link_set_state(link
, LINK_STATE_SETTING_ADDRESSES
);
955 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
956 r
= address_configure(ad
, link
, address_handler
, false);
958 log_link_warning_errno(link
, r
, "Could not set addresses: %m");
959 link_enter_failed(link
);
963 link
->link_messages
++;
966 /* now that we can figure out a default address for the dhcp server,
968 if (link_dhcp4_server_enabled(link
)) {
971 bool acquired_uplink
= false;
973 address
= link_find_dhcp_server_address(link
);
975 log_link_warning(link
, "Failed to find suitable address for DHCPv4 server instance.");
976 link_enter_failed(link
);
980 /* use the server address' subnet as the pool */
981 r
= sd_dhcp_server_configure_pool(link
->dhcp_server
, &address
->in_addr
.in
, address
->prefixlen
,
982 link
->network
->dhcp_server_pool_offset
, link
->network
->dhcp_server_pool_size
);
987 r = sd_dhcp_server_set_router(link->dhcp_server,
988 &main_address->in_addr.in);
993 if (link
->network
->dhcp_server_max_lease_time_usec
> 0) {
994 r
= sd_dhcp_server_set_max_lease_time(
996 DIV_ROUND_UP(link
->network
->dhcp_server_max_lease_time_usec
, USEC_PER_SEC
));
1001 if (link
->network
->dhcp_server_default_lease_time_usec
> 0) {
1002 r
= sd_dhcp_server_set_default_lease_time(
1004 DIV_ROUND_UP(link
->network
->dhcp_server_default_lease_time_usec
, USEC_PER_SEC
));
1009 if (link
->network
->dhcp_server_emit_dns
) {
1011 if (link
->network
->n_dhcp_server_dns
> 0)
1012 r
= sd_dhcp_server_set_dns(link
->dhcp_server
, link
->network
->dhcp_server_dns
, link
->network
->n_dhcp_server_dns
);
1014 uplink
= manager_find_uplink(link
->manager
, link
);
1015 acquired_uplink
= true;
1018 log_link_debug(link
, "Not emitting DNS server information on link, couldn't find suitable uplink.");
1021 r
= link_push_dns_to_dhcp_server(uplink
, link
->dhcp_server
);
1024 log_link_warning_errno(link
, r
, "Failed to set DNS server for DHCP server, ignoring: %m");
1028 if (link
->network
->dhcp_server_emit_ntp
) {
1030 if (link
->network
->n_dhcp_server_ntp
> 0)
1031 r
= sd_dhcp_server_set_ntp(link
->dhcp_server
, link
->network
->dhcp_server_ntp
, link
->network
->n_dhcp_server_ntp
);
1033 if (!acquired_uplink
)
1034 uplink
= manager_find_uplink(link
->manager
, link
);
1037 log_link_debug(link
, "Not emitting NTP server information on link, couldn't find suitable uplink.");
1040 r
= link_push_ntp_to_dhcp_server(uplink
, link
->dhcp_server
);
1044 log_link_warning_errno(link
, r
, "Failed to set NTP server for DHCP server, ignoring: %m");
1047 r
= sd_dhcp_server_set_emit_router(link
->dhcp_server
, link
->network
->dhcp_server_emit_router
);
1049 log_link_warning_errno(link
, r
, "Failed to set router emission for DHCP server: %m");
1053 if (link
->network
->dhcp_server_emit_timezone
) {
1054 _cleanup_free_
char *buffer
= NULL
;
1055 const char *tz
= NULL
;
1057 if (link
->network
->dhcp_server_timezone
)
1058 tz
= link
->network
->dhcp_server_timezone
;
1060 r
= get_timezone(&buffer
);
1062 log_warning_errno(r
, "Failed to determine timezone: %m");
1068 r
= sd_dhcp_server_set_timezone(link
->dhcp_server
, tz
);
1074 r
= sd_dhcp_server_start(link
->dhcp_server
);
1076 log_link_warning_errno(link
, r
, "Could not start DHCPv4 server instance: %m");
1078 link_enter_failed(link
);
1083 log_link_debug(link
, "Offering DHCPv4 leases");
1086 if (link
->link_messages
== 0)
1087 link_enter_set_routes(link
);
1089 log_link_debug(link
, "Setting addresses");
1094 int link_address_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1095 _cleanup_link_unref_ Link
*link
= userdata
;
1100 assert(link
->ifname
);
1102 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1105 r
= sd_netlink_message_get_errno(m
);
1106 if (r
< 0 && r
!= -EADDRNOTAVAIL
)
1107 log_link_warning_errno(link
, r
, "Could not drop address: %m");
1112 static int link_set_bridge_vlan(Link
*link
) {
1115 r
= br_vlan_configure(link
, link
->network
->pvid
, link
->network
->br_vid_bitmap
, link
->network
->br_untagged_bitmap
);
1117 log_link_error_errno(link
, r
, "Failed to assign VLANs to bridge port: %m");
1122 static int link_set_bridge_fdb(Link
*link
) {
1123 FdbEntry
*fdb_entry
;
1126 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
1127 r
= fdb_entry_configure(link
, fdb_entry
);
1129 log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
1137 static int link_set_proxy_arp(Link
*link
) {
1138 const char *p
= NULL
;
1141 if (!link_proxy_arp_enabled(link
))
1144 p
= strjoina("/proc/sys/net/ipv4/conf/", link
->ifname
, "/proxy_arp");
1146 r
= write_string_file(p
, one_zero(link
->network
->proxy_arp
), WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
1148 log_link_warning_errno(link
, r
, "Cannot configure proxy ARP for interface: %m");
1153 static int link_set_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1154 _cleanup_link_unref_ Link
*link
= userdata
;
1157 log_link_debug(link
, "Set link");
1159 r
= sd_netlink_message_get_errno(m
);
1160 if (r
< 0 && r
!= -EEXIST
) {
1161 log_link_error_errno(link
, r
, "Could not join netdev: %m");
1162 link_enter_failed(link
);
1169 static int set_hostname_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
1170 _cleanup_link_unref_ Link
*link
= userdata
;
1171 const sd_bus_error
*e
;
1176 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1179 e
= sd_bus_message_get_error(m
);
1181 log_link_warning_errno(link
, sd_bus_error_get_errno(e
), "Could not set hostname: %s", e
->message
);
1186 int link_set_hostname(Link
*link
, const char *hostname
) {
1190 assert(link
->manager
);
1192 log_link_debug(link
, "Setting transient hostname: '%s'", strna(hostname
));
1194 if (!link
->manager
->bus
) {
1195 /* TODO: replace by assert when we can rely on kdbus */
1196 log_link_info(link
, "Not connected to system bus, ignoring transient hostname.");
1200 r
= sd_bus_call_method_async(
1203 "org.freedesktop.hostname1",
1204 "/org/freedesktop/hostname1",
1205 "org.freedesktop.hostname1",
1207 set_hostname_handler
,
1214 return log_link_error_errno(link
, r
, "Could not set transient hostname: %m");
1221 static int set_timezone_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
1222 _cleanup_link_unref_ Link
*link
= userdata
;
1223 const sd_bus_error
*e
;
1228 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1231 e
= sd_bus_message_get_error(m
);
1233 log_link_warning_errno(link
, sd_bus_error_get_errno(e
), "Could not set timezone: %s", e
->message
);
1238 int link_set_timezone(Link
*link
, const char *tz
) {
1242 assert(link
->manager
);
1245 log_link_debug(link
, "Setting system timezone: '%s'", tz
);
1247 if (!link
->manager
->bus
) {
1248 log_link_info(link
, "Not connected to system bus, ignoring timezone.");
1252 r
= sd_bus_call_method_async(
1255 "org.freedesktop.timedate1",
1256 "/org/freedesktop/timedate1",
1257 "org.freedesktop.timedate1",
1259 set_timezone_handler
,
1265 return log_link_error_errno(link
, r
, "Could not set timezone: %m");
1272 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1273 _cleanup_link_unref_ Link
*link
= userdata
;
1278 assert(link
->ifname
);
1280 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1283 r
= sd_netlink_message_get_errno(m
);
1285 log_link_warning_errno(link
, r
, "Could not set MTU: %m");
1290 int link_set_mtu(Link
*link
, uint32_t mtu
) {
1291 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1295 assert(link
->manager
);
1296 assert(link
->manager
->rtnl
);
1298 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1300 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1302 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1304 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1306 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1308 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, set_mtu_handler
, link
, 0, NULL
);
1310 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1317 static int set_flags_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1318 _cleanup_link_unref_ Link
*link
= userdata
;
1323 assert(link
->ifname
);
1325 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1328 r
= sd_netlink_message_get_errno(m
);
1330 log_link_warning_errno(link
, r
, "Could not set link flags: %m");
1335 static int link_set_flags(Link
*link
) {
1336 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1337 unsigned ifi_change
= 0;
1338 unsigned ifi_flags
= 0;
1342 assert(link
->manager
);
1343 assert(link
->manager
->rtnl
);
1345 if (link
->flags
& IFF_LOOPBACK
)
1351 if (link
->network
->arp
< 0)
1354 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1356 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1358 if (link
->network
->arp
>= 0) {
1359 ifi_change
|= IFF_NOARP
;
1360 ifi_flags
|= link
->network
->arp
? 0 : IFF_NOARP
;
1363 r
= sd_rtnl_message_link_set_flags(req
, ifi_flags
, ifi_change
);
1365 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1367 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, set_flags_handler
, link
, 0, NULL
);
1369 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1376 static int link_set_bridge(Link
*link
) {
1377 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1381 assert(link
->network
);
1383 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1385 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1387 r
= sd_rtnl_message_link_set_family(req
, PF_BRIDGE
);
1389 return log_link_error_errno(link
, r
, "Could not set message family: %m");
1391 r
= sd_netlink_message_open_container(req
, IFLA_PROTINFO
);
1393 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1395 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_GUARD
, !link
->network
->use_bpdu
);
1397 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_GUARD attribute: %m");
1399 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MODE
, link
->network
->hairpin
);
1401 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MODE attribute: %m");
1403 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_FAST_LEAVE
, link
->network
->fast_leave
);
1405 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_FAST_LEAVE attribute: %m");
1407 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_PROTECT
, !link
->network
->allow_port_to_be_root
);
1409 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PROTECT attribute: %m");
1411 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_UNICAST_FLOOD
, link
->network
->unicast_flood
);
1413 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_UNICAST_FLOOD attribute: %m");
1415 if (link
->network
->cost
!= 0) {
1416 r
= sd_netlink_message_append_u32(req
, IFLA_BRPORT_COST
, link
->network
->cost
);
1418 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_COST attribute: %m");
1421 r
= sd_netlink_message_close_container(req
);
1423 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1425 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_set_handler
, link
, 0, NULL
);
1427 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1434 static int link_lldp_save(Link
*link
) {
1435 _cleanup_free_
char *temp_path
= NULL
;
1436 _cleanup_fclose_
FILE *f
= NULL
;
1437 sd_lldp_neighbor
**l
= NULL
;
1441 assert(link
->lldp_file
);
1444 (void) unlink(link
->lldp_file
);
1448 r
= sd_lldp_get_neighbors(link
->lldp
, &l
);
1452 (void) unlink(link
->lldp_file
);
1458 r
= fopen_temporary(link
->lldp_file
, &f
, &temp_path
);
1462 fchmod(fileno(f
), 0644);
1464 for (i
= 0; i
< n
; i
++) {
1469 r
= sd_lldp_neighbor_get_raw(l
[i
], &p
, &sz
);
1474 (void) fwrite(&u
, 1, sizeof(u
), f
);
1475 (void) fwrite(p
, 1, sz
, f
);
1478 r
= fflush_and_check(f
);
1482 if (rename(temp_path
, link
->lldp_file
) < 0) {
1489 (void) unlink(link
->lldp_file
);
1491 (void) unlink(temp_path
);
1493 log_link_error_errno(link
, r
, "Failed to save LLDP data to %s: %m", link
->lldp_file
);
1497 for (i
= 0; i
< n
; i
++)
1498 sd_lldp_neighbor_unref(l
[i
]);
1505 static void lldp_handler(sd_lldp
*lldp
, sd_lldp_event event
, sd_lldp_neighbor
*n
, void *userdata
) {
1506 Link
*link
= userdata
;
1511 (void) link_lldp_save(link
);
1513 if (link_lldp_emit_enabled(link
) && event
== SD_LLDP_EVENT_ADDED
) {
1514 /* If we received information about a new neighbor, restart the LLDP "fast" logic */
1516 log_link_debug(link
, "Received LLDP datagram from previously unknown neighbor, restarting 'fast' LLDP transmission.");
1518 r
= link_lldp_emit_start(link
);
1520 log_link_warning_errno(link
, r
, "Failed to restart LLDP transmission: %m");
1524 static int link_acquire_ipv6_conf(Link
*link
) {
1529 if (link_dhcp6_enabled(link
)) {
1530 assert(link
->dhcp6_client
);
1531 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1533 /* start DHCPv6 client in stateless mode */
1534 r
= dhcp6_request_address(link
, true);
1535 if (r
< 0 && r
!= -EBUSY
)
1536 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv6 lease: %m");
1538 log_link_debug(link
, "Acquiring DHCPv6 lease");
1541 if (link_ipv6_accept_ra_enabled(link
)) {
1542 assert(link
->ndisc
);
1544 log_link_debug(link
, "Discovering IPv6 routers");
1546 r
= sd_ndisc_start(link
->ndisc
);
1547 if (r
< 0 && r
!= -EBUSY
)
1548 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1554 static int link_acquire_ipv4_conf(Link
*link
) {
1558 assert(link
->network
);
1559 assert(link
->manager
);
1560 assert(link
->manager
->event
);
1562 if (link_ipv4ll_enabled(link
)) {
1563 assert(link
->ipv4ll
);
1565 log_link_debug(link
, "Acquiring IPv4 link-local address");
1567 r
= sd_ipv4ll_start(link
->ipv4ll
);
1569 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1572 if (link_dhcp4_enabled(link
)) {
1573 assert(link
->dhcp_client
);
1575 log_link_debug(link
, "Acquiring DHCPv4 lease");
1577 r
= sd_dhcp_client_start(link
->dhcp_client
);
1579 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1585 static int link_acquire_conf(Link
*link
) {
1590 r
= link_acquire_ipv4_conf(link
);
1594 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) == 0) {
1595 r
= link_acquire_ipv6_conf(link
);
1600 if (link_lldp_emit_enabled(link
)) {
1601 r
= link_lldp_emit_start(link
);
1603 return log_link_warning_errno(link
, r
, "Failed to start LLDP transmission: %m");
1609 bool link_has_carrier(Link
*link
) {
1610 /* see Documentation/networking/operstates.txt in the kernel sources */
1612 if (link
->kernel_operstate
== IF_OPER_UP
)
1615 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1616 /* operstate may not be implemented, so fall back to flags */
1617 if ((link
->flags
& IFF_LOWER_UP
) && !(link
->flags
& IFF_DORMANT
))
1623 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1624 _cleanup_link_unref_ Link
*link
= userdata
;
1629 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1632 r
= sd_netlink_message_get_errno(m
);
1634 /* we warn but don't fail the link, as it may be
1636 log_link_warning_errno(link
, r
, "Could not bring up interface: %m");
1641 static int link_up(Link
*link
) {
1642 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1643 uint8_t ipv6ll_mode
;
1647 assert(link
->network
);
1648 assert(link
->manager
);
1649 assert(link
->manager
->rtnl
);
1651 log_link_debug(link
, "Bringing link up");
1653 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1655 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1657 /* set it free if not enslaved with networkd */
1658 if (!link
->network
->bridge
&& !link
->network
->bond
&& !link
->network
->vrf
) {
1659 r
= sd_netlink_message_append_u32(req
, IFLA_MASTER
, 0);
1661 return log_link_error_errno(link
, r
, "Could not append IFLA_MASTER attribute: %m");
1664 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1666 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1668 if (link
->network
->mac
) {
1669 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
1671 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
1674 /* If IPv6 not configured (no static IPv6 address and IPv6LL autoconfiguration is disabled)
1675 for this interface, or if it is a bridge slave, then disable IPv6 else enable it. */
1676 (void) link_enable_ipv6(link
);
1678 if (link
->network
->mtu
) {
1679 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
1680 on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */
1681 if (link_ipv6_enabled(link
) && link
->network
->mtu
< IPV6_MIN_MTU
) {
1683 log_link_warning(link
, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU
) ", as "
1684 "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU
) " bytes: %m");
1686 link
->network
->mtu
= IPV6_MIN_MTU
;
1689 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, link
->network
->mtu
);
1691 return log_link_error_errno(link
, r
, "Could not set MTU: %m");
1694 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1696 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1698 if (link_ipv6_enabled(link
)) {
1699 /* if the kernel lacks ipv6 support setting IFF_UP fails if any ipv6 options are passed */
1700 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1702 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1704 if (!link_ipv6ll_enabled(link
))
1705 ipv6ll_mode
= IN6_ADDR_GEN_MODE_NONE
;
1707 const char *p
= NULL
;
1708 _cleanup_free_
char *stable_secret
= NULL
;
1710 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/stable_secret");
1711 r
= read_one_line_file(p
, &stable_secret
);
1714 ipv6ll_mode
= IN6_ADDR_GEN_MODE_EUI64
;
1716 ipv6ll_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
1718 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1720 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1722 if (!in_addr_is_null(AF_INET6
, &link
->network
->ipv6_token
)) {
1723 r
= sd_netlink_message_append_in6_addr(req
, IFLA_INET6_TOKEN
, &link
->network
->ipv6_token
.in6
);
1725 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_TOKEN: %m");
1728 r
= sd_netlink_message_close_container(req
);
1730 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1733 r
= sd_netlink_message_close_container(req
);
1735 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1737 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_up_handler
, link
, 0, NULL
);
1739 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1746 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1747 _cleanup_link_unref_ Link
*link
= userdata
;
1752 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1755 r
= sd_netlink_message_get_errno(m
);
1757 log_link_warning_errno(link
, r
, "Could not bring down interface: %m");
1762 static int link_down(Link
*link
) {
1763 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1767 assert(link
->manager
);
1768 assert(link
->manager
->rtnl
);
1770 log_link_debug(link
, "Bringing link down");
1772 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
1773 RTM_SETLINK
, link
->ifindex
);
1775 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1777 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
1779 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1781 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_down_handler
, link
, 0, NULL
);
1783 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1790 static int link_handle_bound_to_list(Link
*link
) {
1794 bool required_up
= false;
1795 bool link_is_up
= false;
1799 if (hashmap_isempty(link
->bound_to_links
))
1802 if (link
->flags
& IFF_UP
)
1805 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
1806 if (link_has_carrier(l
)) {
1811 if (!required_up
&& link_is_up
) {
1812 r
= link_down(link
);
1815 } else if (required_up
&& !link_is_up
) {
1824 static int link_handle_bound_by_list(Link
*link
) {
1831 if (hashmap_isempty(link
->bound_by_links
))
1834 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
1835 r
= link_handle_bound_to_list(l
);
1843 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
1849 if (link
== carrier
)
1852 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
1855 r
= hashmap_ensure_allocated(h
, NULL
);
1859 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
1866 static int link_new_bound_by_list(Link
*link
) {
1871 bool list_updated
= false;
1874 assert(link
->manager
);
1878 HASHMAP_FOREACH(carrier
, m
->links
, i
) {
1879 if (!carrier
->network
)
1882 if (strv_isempty(carrier
->network
->bind_carrier
))
1885 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
, 0)) {
1886 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
1890 list_updated
= true;
1897 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
1898 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
1902 link_dirty(carrier
);
1908 static int link_new_bound_to_list(Link
*link
) {
1913 bool list_updated
= false;
1916 assert(link
->manager
);
1921 if (strv_isempty(link
->network
->bind_carrier
))
1926 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
1927 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
, 0)) {
1928 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
1932 list_updated
= true;
1939 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
1940 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
1944 link_dirty(carrier
);
1950 static int link_new_carrier_maps(Link
*link
) {
1953 r
= link_new_bound_by_list(link
);
1957 r
= link_handle_bound_by_list(link
);
1961 r
= link_new_bound_to_list(link
);
1965 r
= link_handle_bound_to_list(link
);
1972 static void link_free_bound_to_list(Link
*link
) {
1976 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
1977 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
1979 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
1980 link_dirty(bound_to
);
1986 static void link_free_bound_by_list(Link
*link
) {
1990 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
1991 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
1993 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
1994 link_dirty(bound_by
);
1995 link_handle_bound_to_list(bound_by
);
2002 static void link_free_carrier_maps(Link
*link
) {
2003 bool list_updated
= false;
2007 if (!hashmap_isempty(link
->bound_to_links
)) {
2008 link_free_bound_to_list(link
);
2009 list_updated
= true;
2012 if (!hashmap_isempty(link
->bound_by_links
)) {
2013 link_free_bound_by_list(link
);
2014 list_updated
= true;
2023 void link_drop(Link
*link
) {
2024 if (!link
|| link
->state
== LINK_STATE_LINGER
)
2027 link_set_state(link
, LINK_STATE_LINGER
);
2029 link_free_carrier_maps(link
);
2031 log_link_debug(link
, "Link removed");
2033 (void)unlink(link
->state_file
);
2039 static int link_joined(Link
*link
) {
2043 assert(link
->network
);
2045 if (!hashmap_isempty(link
->bound_to_links
)) {
2046 r
= link_handle_bound_to_list(link
);
2049 } else if (!(link
->flags
& IFF_UP
)) {
2052 link_enter_failed(link
);
2057 if (link
->network
->bridge
) {
2058 r
= link_set_bridge(link
);
2060 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
2063 if (link
->network
->use_br_vlan
&&
2064 (link
->network
->bridge
|| streq_ptr("bridge", link
->kind
))) {
2065 r
= link_set_bridge_vlan(link
);
2067 log_link_error_errno(link
, r
, "Could not set bridge vlan: %m");
2070 return link_enter_set_addresses(link
);
2073 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
2074 _cleanup_link_unref_ Link
*link
= userdata
;
2078 assert(link
->network
);
2082 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2085 r
= sd_netlink_message_get_errno(m
);
2086 if (r
< 0 && r
!= -EEXIST
) {
2087 log_link_error_errno(link
, r
, "Could not join netdev: %m");
2088 link_enter_failed(link
);
2091 log_link_debug(link
, "Joined netdev");
2093 if (link
->enslaving
<= 0)
2099 static int link_enter_join_netdev(Link
*link
) {
2105 assert(link
->network
);
2106 assert(link
->state
== LINK_STATE_PENDING
);
2108 link_set_state(link
, LINK_STATE_ENSLAVING
);
2112 if (!link
->network
->bridge
&&
2113 !link
->network
->bond
&&
2114 !link
->network
->vrf
&&
2115 hashmap_isempty(link
->network
->stacked_netdevs
))
2116 return link_joined(link
);
2118 if (link
->network
->bond
) {
2119 log_struct(LOG_DEBUG
,
2120 LOG_LINK_INTERFACE(link
),
2121 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2122 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
),
2125 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
2127 log_struct_errno(LOG_WARNING
, r
,
2128 LOG_LINK_INTERFACE(link
),
2129 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2130 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
),
2133 link_enter_failed(link
);
2140 if (link
->network
->bridge
) {
2141 log_struct(LOG_DEBUG
,
2142 LOG_LINK_INTERFACE(link
),
2143 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2144 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
),
2147 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
2149 log_struct_errno(LOG_WARNING
, r
,
2150 LOG_LINK_INTERFACE(link
),
2151 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2152 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
),
2154 link_enter_failed(link
);
2161 if (link
->network
->vrf
) {
2162 log_struct(LOG_DEBUG
,
2163 LOG_LINK_INTERFACE(link
),
2164 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2165 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->vrf
->ifname
),
2167 r
= netdev_join(link
->network
->vrf
, link
, netdev_join_handler
);
2169 log_struct_errno(LOG_WARNING
, r
,
2170 LOG_LINK_INTERFACE(link
),
2171 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2172 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->vrf
->ifname
),
2174 link_enter_failed(link
);
2181 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
2183 log_struct(LOG_DEBUG
,
2184 LOG_LINK_INTERFACE(link
),
2185 LOG_NETDEV_INTERFACE(netdev
),
2186 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
),
2189 r
= netdev_join(netdev
, link
, netdev_join_handler
);
2191 log_struct_errno(LOG_WARNING
, r
,
2192 LOG_LINK_INTERFACE(link
),
2193 LOG_NETDEV_INTERFACE(netdev
),
2194 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
),
2196 link_enter_failed(link
);
2206 static int link_set_ipv4_forward(Link
*link
) {
2209 if (!link_ipv4_forward_enabled(link
))
2212 /* We propagate the forwarding flag from one interface to the
2213 * global setting one way. This means: as long as at least one
2214 * interface was configured at any time that had IP forwarding
2215 * enabled the setting will stay on for good. We do this
2216 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
2217 * somewhat in sync (see below). */
2219 r
= write_string_file("/proc/sys/net/ipv4/ip_forward", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2221 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
2226 static int link_set_ipv6_forward(Link
*link
) {
2229 if (!link_ipv6_forward_enabled(link
))
2232 /* On Linux, the IPv6 stack does not know a per-interface
2233 * packet forwarding setting: either packet forwarding is on
2234 * for all, or off for all. We hence don't bother with a
2235 * per-interface setting, but simply propagate the interface
2236 * flag, if it is set, to the global flag, one-way. Note that
2237 * while IPv4 would allow a per-interface flag, we expose the
2238 * same behaviour there and also propagate the setting from
2239 * one to all, to keep things simple (see above). */
2241 r
= write_string_file("/proc/sys/net/ipv6/conf/all/forwarding", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2243 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
2248 static int link_set_ipv6_privacy_extensions(Link
*link
) {
2249 char buf
[DECIMAL_STR_MAX(unsigned) + 1];
2250 IPv6PrivacyExtensions s
;
2251 const char *p
= NULL
;
2254 s
= link_ipv6_privacy_extensions(link
);
2258 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/use_tempaddr");
2259 xsprintf(buf
, "%u", (unsigned) link
->network
->ipv6_privacy_extensions
);
2261 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2263 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
2268 static int link_set_ipv6_accept_ra(Link
*link
) {
2269 const char *p
= NULL
;
2272 /* Make this a NOP if IPv6 is not available */
2273 if (!socket_ipv6_is_supported())
2276 if (link
->flags
& IFF_LOOPBACK
)
2282 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/accept_ra");
2284 /* We handle router advertisements ourselves, tell the kernel to GTFO */
2285 r
= write_string_file(p
, "0", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2287 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
2292 static int link_set_ipv6_dad_transmits(Link
*link
) {
2293 char buf
[DECIMAL_STR_MAX(int) + 1];
2294 const char *p
= NULL
;
2297 /* Make this a NOP if IPv6 is not available */
2298 if (!socket_ipv6_is_supported())
2301 if (link
->flags
& IFF_LOOPBACK
)
2307 if (link
->network
->ipv6_dad_transmits
< 0)
2310 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/dad_transmits");
2311 xsprintf(buf
, "%i", link
->network
->ipv6_dad_transmits
);
2313 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2315 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
2320 static int link_set_ipv6_hop_limit(Link
*link
) {
2321 char buf
[DECIMAL_STR_MAX(int) + 1];
2322 const char *p
= NULL
;
2325 /* Make this a NOP if IPv6 is not available */
2326 if (!socket_ipv6_is_supported())
2329 if (link
->flags
& IFF_LOOPBACK
)
2335 if (link
->network
->ipv6_hop_limit
< 0)
2338 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/hop_limit");
2339 xsprintf(buf
, "%i", link
->network
->ipv6_hop_limit
);
2341 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2343 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2348 static int link_drop_foreign_config(Link
*link
) {
2354 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
2355 /* we consider IPv6LL addresses to be managed by the kernel */
2356 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2359 r
= address_remove(address
, link
, link_address_remove_handler
);
2364 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2365 /* do not touch routes managed by the kernel */
2366 if (route
->protocol
== RTPROT_KERNEL
)
2369 r
= route_remove(route
, link
, link_route_remove_handler
);
2377 static int link_drop_config(Link
*link
) {
2383 SET_FOREACH(address
, link
->addresses
, i
) {
2384 /* we consider IPv6LL addresses to be managed by the kernel */
2385 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2388 r
= address_remove(address
, link
, link_address_remove_handler
);
2393 SET_FOREACH(route
, link
->routes
, i
) {
2394 /* do not touch routes managed by the kernel */
2395 if (route
->protocol
== RTPROT_KERNEL
)
2398 r
= route_remove(route
, link
, link_route_remove_handler
);
2406 static int link_update_lldp(Link
*link
) {
2414 if (link
->flags
& IFF_UP
) {
2415 r
= sd_lldp_start(link
->lldp
);
2417 log_link_debug(link
, "Started LLDP.");
2419 r
= sd_lldp_stop(link
->lldp
);
2421 log_link_debug(link
, "Stopped LLDP.");
2427 static int link_configure(Link
*link
) {
2431 assert(link
->network
);
2432 assert(link
->state
== LINK_STATE_PENDING
);
2434 /* Drop foreign config, but ignore loopback or critical devices.
2435 * We do not want to remove loopback address or addresses used for root NFS. */
2436 if (!(link
->flags
& IFF_LOOPBACK
) && !(link
->network
->dhcp_critical
)) {
2437 r
= link_drop_foreign_config(link
);
2442 r
= link_set_bridge_fdb(link
);
2446 r
= link_set_proxy_arp(link
);
2450 r
= link_set_ipv4_forward(link
);
2454 r
= link_set_ipv6_forward(link
);
2458 r
= link_set_ipv6_privacy_extensions(link
);
2462 r
= link_set_ipv6_accept_ra(link
);
2466 r
= link_set_ipv6_dad_transmits(link
);
2470 r
= link_set_ipv6_hop_limit(link
);
2474 r
= link_set_flags(link
);
2478 if (link_ipv4ll_enabled(link
)) {
2479 r
= ipv4ll_configure(link
);
2484 if (link_dhcp4_enabled(link
)) {
2485 r
= dhcp4_configure(link
);
2490 if (link_dhcp4_server_enabled(link
)) {
2491 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
2495 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
2500 if (link_dhcp6_enabled(link
) ||
2501 link_ipv6_accept_ra_enabled(link
)) {
2502 r
= dhcp6_configure(link
);
2507 if (link_ipv6_accept_ra_enabled(link
)) {
2508 r
= ndisc_configure(link
);
2513 if (link_lldp_rx_enabled(link
)) {
2514 r
= sd_lldp_new(&link
->lldp
);
2518 r
= sd_lldp_set_ifindex(link
->lldp
, link
->ifindex
);
2522 r
= sd_lldp_match_capabilities(link
->lldp
,
2523 link
->network
->lldp_mode
== LLDP_MODE_ROUTERS_ONLY
?
2524 SD_LLDP_SYSTEM_CAPABILITIES_ALL_ROUTERS
:
2525 SD_LLDP_SYSTEM_CAPABILITIES_ALL
);
2529 r
= sd_lldp_set_filter_address(link
->lldp
, &link
->mac
);
2533 r
= sd_lldp_attach_event(link
->lldp
, NULL
, 0);
2537 r
= sd_lldp_set_callback(link
->lldp
, lldp_handler
, link
);
2541 r
= link_update_lldp(link
);
2546 if (link_has_carrier(link
)) {
2547 r
= link_acquire_conf(link
);
2552 return link_enter_join_netdev(link
);
2555 static int link_initialized_and_synced(sd_netlink
*rtnl
, sd_netlink_message
*m
,
2557 _cleanup_link_unref_ Link
*link
= userdata
;
2562 assert(link
->ifname
);
2563 assert(link
->manager
);
2565 if (link
->state
!= LINK_STATE_PENDING
)
2568 log_link_debug(link
, "Link state is up-to-date");
2570 r
= link_new_bound_by_list(link
);
2574 r
= link_handle_bound_by_list(link
);
2578 if (!link
->network
) {
2579 r
= network_get(link
->manager
, link
->udev_device
, link
->ifname
,
2580 &link
->mac
, &network
);
2582 link_enter_unmanaged(link
);
2587 if (link
->flags
& IFF_LOOPBACK
) {
2588 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
2589 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
2591 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
2592 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
2594 if (network
->dhcp_server
)
2595 log_link_debug(link
, "Ignoring DHCP server for loopback link");
2598 r
= network_apply(link
->manager
, network
, link
);
2603 r
= link_new_bound_to_list(link
);
2607 r
= link_configure(link
);
2614 int link_initialized(Link
*link
, struct udev_device
*device
) {
2615 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2619 assert(link
->manager
);
2620 assert(link
->manager
->rtnl
);
2623 if (link
->state
!= LINK_STATE_PENDING
)
2626 if (link
->udev_device
)
2629 log_link_debug(link
, "udev initialized link");
2631 link
->udev_device
= udev_device_ref(device
);
2633 /* udev has initialized the link, but we don't know if we have yet
2634 * processed the NEWLINK messages with the latest state. Do a GETLINK,
2635 * when it returns we know that the pending NEWLINKs have already been
2636 * processed and that we are up-to-date */
2638 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
2643 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
,
2644 link_initialized_and_synced
, link
, 0, NULL
);
2653 static int link_load(Link
*link
) {
2654 _cleanup_free_
char *network_file
= NULL
,
2657 *dhcp4_address
= NULL
,
2658 *ipv4ll_address
= NULL
;
2659 union in_addr_union address
;
2660 union in_addr_union route_dst
;
2666 r
= parse_env_file(link
->state_file
, NEWLINE
,
2667 "NETWORK_FILE", &network_file
,
2668 "ADDRESSES", &addresses
,
2670 "DHCP4_ADDRESS", &dhcp4_address
,
2671 "IPV4LL_ADDRESS", &ipv4ll_address
,
2673 if (r
< 0 && r
!= -ENOENT
)
2674 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
2681 suffix
= strrchr(network_file
, '.');
2683 log_link_debug(link
, "Failed to get network name from %s", network_file
);
2684 goto network_file_fail
;
2688 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
2690 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
2691 goto network_file_fail
;
2694 r
= network_apply(link
->manager
, network
, link
);
2696 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
2705 _cleanup_free_
char *address_str
= NULL
;
2706 char *prefixlen_str
;
2708 unsigned char prefixlen
;
2710 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
2712 log_link_debug_errno(link
, r
, "Failed to extract next address string: %m");
2718 prefixlen_str
= strchr(address_str
, '/');
2719 if (!prefixlen_str
) {
2720 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
2724 *prefixlen_str
++ = '\0';
2726 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
2728 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
2732 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
2734 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
2738 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
2740 return log_link_error_errno(link
, r
, "Failed to add address: %m");
2749 _cleanup_free_
char *route_str
= NULL
;
2750 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
2752 char *prefixlen_str
;
2754 unsigned char prefixlen
, tos
, table
;
2757 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
2759 log_link_debug_errno(link
, r
, "Failed to extract next route string: %m");
2765 prefixlen_str
= strchr(route_str
, '/');
2766 if (!prefixlen_str
) {
2767 log_link_debug(link
, "Failed to parse route %s", route_str
);
2771 *prefixlen_str
++ = '\0';
2773 r
= sscanf(prefixlen_str
, "%hhu/%hhu/%"SCNu32
"/%hhu/"USEC_FMT
, &prefixlen
, &tos
, &priority
, &table
, &lifetime
);
2775 log_link_debug(link
,
2776 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
2781 r
= in_addr_from_string_auto(route_str
, &family
, &route_dst
);
2783 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
2787 r
= route_add(link
, family
, &route_dst
, prefixlen
, tos
, priority
, table
, &route
);
2789 return log_link_error_errno(link
, r
, "Failed to add route: %m");
2791 if (lifetime
!= USEC_INFINITY
) {
2792 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(), lifetime
,
2793 0, route_expire_handler
, route
);
2795 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
2798 route
->lifetime
= lifetime
;
2799 sd_event_source_unref(route
->expire
);
2800 route
->expire
= expire
;
2805 if (dhcp4_address
) {
2806 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
2808 log_link_debug_errno(link
, r
, "Failed to parse DHCPv4 address %s: %m", dhcp4_address
);
2809 goto dhcp4_address_fail
;
2812 r
= sd_dhcp_client_new(&link
->dhcp_client
);
2814 return log_link_error_errno(link
, r
, "Failed to create DHCPv4 client: %m");
2816 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
2818 return log_link_error_errno(link
, r
, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address
);
2823 if (ipv4ll_address
) {
2824 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
2826 log_link_debug_errno(link
, r
, "Failed to parse IPv4LL address %s: %m", ipv4ll_address
);
2827 goto ipv4ll_address_fail
;
2830 r
= sd_ipv4ll_new(&link
->ipv4ll
);
2832 return log_link_error_errno(link
, r
, "Failed to create IPv4LL client: %m");
2834 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
2836 return log_link_error_errno(link
, r
, "Failed to set initial IPv4LL address %s: %m", ipv4ll_address
);
2839 ipv4ll_address_fail
:
2844 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
2846 _cleanup_udev_device_unref_
struct udev_device
*device
= NULL
;
2847 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
2855 r
= link_new(m
, message
, ret
);
2861 log_link_debug(link
, "Link %d added", link
->ifindex
);
2863 r
= link_load(link
);
2867 if (detect_container() <= 0) {
2868 /* not in a container, udev will be around */
2869 sprintf(ifindex_str
, "n%d", link
->ifindex
);
2870 device
= udev_device_new_from_device_id(m
->udev
, ifindex_str
);
2872 r
= log_link_warning_errno(link
, errno
, "Could not find udev device: %m");
2876 if (udev_device_get_is_initialized(device
) <= 0) {
2878 log_link_debug(link
, "link pending udev initialization...");
2882 r
= link_initialized(link
, device
);
2886 /* we are calling a callback directly, so must take a ref */
2889 r
= link_initialized_and_synced(m
->rtnl
, NULL
, link
);
2896 link_enter_failed(link
);
2900 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
2905 log_link_info(link
, "Gained IPv6LL");
2907 link
->ipv6ll_address
= *address
;
2908 link_check_ready(link
);
2910 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
2911 r
= link_acquire_ipv6_conf(link
);
2913 link_enter_failed(link
);
2921 static int link_carrier_gained(Link
*link
) {
2926 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
2927 r
= link_acquire_conf(link
);
2929 link_enter_failed(link
);
2933 r
= link_enter_set_addresses(link
);
2938 r
= link_handle_bound_by_list(link
);
2945 static int link_carrier_lost(Link
*link
) {
2950 r
= link_stop_clients(link
);
2952 link_enter_failed(link
);
2956 r
= link_drop_config(link
);
2960 if (link
->state
!= LINK_STATE_UNMANAGED
) {
2961 r
= link_drop_foreign_config(link
);
2966 r
= link_handle_bound_by_list(link
);
2973 int link_carrier_reset(Link
*link
) {
2978 if (link_has_carrier(link
)) {
2979 r
= link_carrier_lost(link
);
2983 r
= link_carrier_gained(link
);
2987 log_link_info(link
, "Reset carrier");
2993 int link_update(Link
*link
, sd_netlink_message
*m
) {
2994 struct ether_addr mac
;
2997 bool had_carrier
, carrier_gained
, carrier_lost
;
3001 assert(link
->ifname
);
3004 if (link
->state
== LINK_STATE_LINGER
) {
3006 log_link_info(link
, "Link readded");
3007 link_set_state(link
, LINK_STATE_ENSLAVING
);
3009 r
= link_new_carrier_maps(link
);
3014 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
3015 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
3016 log_link_info(link
, "Renamed to %s", ifname
);
3018 link_free_carrier_maps(link
);
3020 r
= free_and_strdup(&link
->ifname
, ifname
);
3024 r
= link_new_carrier_maps(link
);
3029 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
3030 if (r
>= 0 && mtu
> 0) {
3032 if (!link
->original_mtu
) {
3033 link
->original_mtu
= mtu
;
3034 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
3037 if (link
->dhcp_client
) {
3038 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
3041 log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
3047 /* The kernel may broadcast NEWLINK messages without the MAC address
3048 set, simply ignore them. */
3049 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
3051 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3054 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3057 log_link_debug(link
, "MAC address: "
3058 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
3059 mac
.ether_addr_octet
[0],
3060 mac
.ether_addr_octet
[1],
3061 mac
.ether_addr_octet
[2],
3062 mac
.ether_addr_octet
[3],
3063 mac
.ether_addr_octet
[4],
3064 mac
.ether_addr_octet
[5]);
3067 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
3069 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
3072 if (link
->dhcp_client
) {
3073 const DUID
*duid
= link_duid(link
);
3075 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
3076 (const uint8_t *) &link
->mac
,
3080 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
3082 r
= sd_dhcp_client_set_iaid_duid(link
->dhcp_client
,
3083 link
->network
->iaid
,
3085 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3086 duid
->raw_data_len
);
3088 return log_link_warning_errno(link
, r
, "Could not update DUID/IAID in DHCP client: %m");
3091 if (link
->dhcp6_client
) {
3092 const DUID
* duid
= link_duid(link
);
3094 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
3095 (const uint8_t *) &link
->mac
,
3099 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
3101 r
= sd_dhcp6_client_set_iaid(link
->dhcp6_client
,
3102 link
->network
->iaid
);
3104 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 IAID: %m");
3106 r
= sd_dhcp6_client_set_duid(link
->dhcp6_client
,
3108 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3109 duid
->raw_data_len
);
3111 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 DUID: %m");
3116 had_carrier
= link_has_carrier(link
);
3118 r
= link_update_flags(link
, m
);
3122 r
= link_update_lldp(link
);
3126 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
3127 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
3129 if (carrier_gained
) {
3130 log_link_info(link
, "Gained carrier");
3132 r
= link_carrier_gained(link
);
3135 } else if (carrier_lost
) {
3136 log_link_info(link
, "Lost carrier");
3138 r
= link_carrier_lost(link
);
3146 static void print_link_hashmap(FILE *f
, const char *prefix
, Hashmap
* h
) {
3154 if (hashmap_isempty(h
))
3158 HASHMAP_FOREACH(link
, h
, i
) {
3162 fprintf(f
, "%i", link
->ifindex
);
3169 int link_save(Link
*link
) {
3170 _cleanup_free_
char *temp_path
= NULL
;
3171 _cleanup_fclose_
FILE *f
= NULL
;
3172 const char *admin_state
, *oper_state
;
3179 assert(link
->state_file
);
3180 assert(link
->lease_file
);
3181 assert(link
->manager
);
3183 if (link
->state
== LINK_STATE_LINGER
) {
3184 unlink(link
->state_file
);
3188 link_lldp_save(link
);
3190 admin_state
= link_state_to_string(link
->state
);
3191 assert(admin_state
);
3193 oper_state
= link_operstate_to_string(link
->operstate
);
3196 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
3200 fchmod(fileno(f
), 0644);
3203 "# This is private data. Do not parse.\n"
3206 admin_state
, oper_state
);
3208 if (link
->network
) {
3210 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
3211 const char *dhcp_domainname
= NULL
;
3212 char **dhcp6_domains
= NULL
;
3214 if (link
->dhcp6_client
) {
3215 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
, &dhcp6_lease
);
3216 if (r
< 0 && r
!= -ENOMSG
)
3217 log_link_debug(link
, "No DHCPv6 lease");
3220 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
3224 fputstrv(f
, link
->network
->dns
, NULL
, &space
);
3226 if (link
->network
->dhcp_use_dns
&&
3228 const struct in_addr
*addresses
;
3230 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
3234 serialize_in_addrs(f
, addresses
, r
);
3239 if (link
->network
->dhcp_use_dns
&& dhcp6_lease
) {
3240 struct in6_addr
*in6_addrs
;
3242 r
= sd_dhcp6_lease_get_dns(dhcp6_lease
, &in6_addrs
);
3246 serialize_in6_addrs(f
, in6_addrs
, r
);
3251 /* Make sure to flush out old entries before we use the NDISC data */
3254 if (link
->network
->dhcp_use_dns
&& link
->ndisc_rdnss
) {
3257 SET_FOREACH(dd
, link
->ndisc_rdnss
, i
) {
3261 serialize_in6_addrs(f
, &dd
->address
, 1);
3270 fputstrv(f
, link
->network
->ntp
, NULL
, &space
);
3272 if (link
->network
->dhcp_use_ntp
&&
3274 const struct in_addr
*addresses
;
3276 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
3280 serialize_in_addrs(f
, addresses
, r
);
3285 if (link
->network
->dhcp_use_ntp
&& dhcp6_lease
) {
3286 struct in6_addr
*in6_addrs
;
3289 r
= sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease
,
3294 serialize_in6_addrs(f
, in6_addrs
, r
);
3298 r
= sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease
, &hosts
);
3300 fputstrv(f
, hosts
, NULL
, &space
);
3305 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
3306 if (link
->dhcp_lease
)
3307 (void) sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &dhcp_domainname
);
3309 (void) sd_dhcp6_lease_get_domains(dhcp6_lease
, &dhcp6_domains
);
3312 fputs("DOMAINS=", f
);
3313 fputstrv(f
, link
->network
->search_domains
, NULL
, &space
);
3315 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
) {
3318 if (dhcp_domainname
)
3319 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3321 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3323 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3324 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3329 fputs("ROUTE_DOMAINS=", f
);
3330 fputstrv(f
, link
->network
->route_domains
, NULL
, NULL
);
3332 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
) {
3335 if (dhcp_domainname
)
3336 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3338 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3340 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3341 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3346 fprintf(f
, "LLMNR=%s\n",
3347 resolve_support_to_string(link
->network
->llmnr
));
3348 fprintf(f
, "MDNS=%s\n",
3349 resolve_support_to_string(link
->network
->mdns
));
3351 if (link
->network
->dnssec_mode
!= _DNSSEC_MODE_INVALID
)
3352 fprintf(f
, "DNSSEC=%s\n",
3353 dnssec_mode_to_string(link
->network
->dnssec_mode
));
3355 if (!set_isempty(link
->network
->dnssec_negative_trust_anchors
)) {
3358 fputs("DNSSEC_NTA=", f
);
3360 SET_FOREACH(n
, link
->network
->dnssec_negative_trust_anchors
, i
)
3361 fputs_with_space(f
, n
, NULL
, &space
);
3365 fputs("ADDRESSES=", f
);
3367 SET_FOREACH(a
, link
->addresses
, i
) {
3368 _cleanup_free_
char *address_str
= NULL
;
3370 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
3374 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
3379 fputs("ROUTES=", f
);
3381 SET_FOREACH(route
, link
->routes
, i
) {
3382 _cleanup_free_
char *route_str
= NULL
;
3384 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
3388 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%hhu/"USEC_FMT
, space
? " " : "", route_str
,
3389 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
3396 print_link_hashmap(f
, "CARRIER_BOUND_TO=", link
->bound_to_links
);
3397 print_link_hashmap(f
, "CARRIER_BOUND_BY=", link
->bound_by_links
);
3399 if (link
->dhcp_lease
) {
3400 struct in_addr address
;
3401 const char *tz
= NULL
;
3403 assert(link
->network
);
3405 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
3407 fprintf(f
, "TIMEZONE=%s\n", tz
);
3409 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
3411 fputs("DHCP4_ADDRESS=", f
);
3412 serialize_in_addrs(f
, &address
, 1);
3416 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
3424 unlink(link
->lease_file
);
3427 struct in_addr address
;
3429 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
3431 fputs("IPV4LL_ADDRESS=", f
);
3432 serialize_in_addrs(f
, &address
, 1);
3437 r
= fflush_and_check(f
);
3441 if (rename(temp_path
, link
->state_file
) < 0) {
3449 (void) unlink(link
->state_file
);
3451 (void) unlink(temp_path
);
3453 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
3456 /* The serialized state in /run is no longer up-to-date. */
3457 void link_dirty(Link
*link
) {
3462 /* mark manager dirty as link is dirty */
3463 manager_dirty(link
->manager
);
3465 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
3467 /* allocation errors are ignored */
3470 r
= set_put(link
->manager
->dirty_links
, link
);
3472 /* don't take another ref if the link was already dirty */
3478 /* The serialized state in /run is up-to-date */
3479 void link_clean(Link
*link
) {
3481 assert(link
->manager
);
3483 set_remove(link
->manager
->dirty_links
, link
);
3487 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
3488 [LINK_STATE_PENDING
] = "pending",
3489 [LINK_STATE_ENSLAVING
] = "configuring",
3490 [LINK_STATE_SETTING_ADDRESSES
] = "configuring",
3491 [LINK_STATE_SETTING_ROUTES
] = "configuring",
3492 [LINK_STATE_CONFIGURED
] = "configured",
3493 [LINK_STATE_UNMANAGED
] = "unmanaged",
3494 [LINK_STATE_FAILED
] = "failed",
3495 [LINK_STATE_LINGER
] = "linger",
3498 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);
3500 static const char* const link_operstate_table
[_LINK_OPERSTATE_MAX
] = {
3501 [LINK_OPERSTATE_OFF
] = "off",
3502 [LINK_OPERSTATE_NO_CARRIER
] = "no-carrier",
3503 [LINK_OPERSTATE_DORMANT
] = "dormant",
3504 [LINK_OPERSTATE_CARRIER
] = "carrier",
3505 [LINK_OPERSTATE_DEGRADED
] = "degraded",
3506 [LINK_OPERSTATE_ROUTABLE
] = "routable",
3509 DEFINE_STRING_TABLE_LOOKUP(link_operstate
, LinkOperationalState
);