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"
32 #include "networkd-lldp-tx.h"
34 #include "socket-util.h"
35 #include "stdio-util.h"
36 #include "string-table.h"
37 #include "udev-util.h"
41 static bool link_dhcp6_enabled(Link
*link
) {
44 if (!socket_ipv6_is_supported())
47 if (link
->flags
& IFF_LOOPBACK
)
53 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV6
;
56 static bool link_dhcp4_enabled(Link
*link
) {
59 if (link
->flags
& IFF_LOOPBACK
)
65 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV4
;
68 static bool link_dhcp4_server_enabled(Link
*link
) {
71 if (link
->flags
& IFF_LOOPBACK
)
77 return link
->network
->dhcp_server
;
80 static bool link_ipv4ll_enabled(Link
*link
) {
83 if (link
->flags
& IFF_LOOPBACK
)
89 return link
->network
->link_local
& ADDRESS_FAMILY_IPV4
;
92 static bool link_ipv6ll_enabled(Link
*link
) {
95 if (!socket_ipv6_is_supported())
98 if (link
->flags
& IFF_LOOPBACK
)
104 return link
->network
->link_local
& ADDRESS_FAMILY_IPV6
;
107 static bool link_ipv6_enabled(Link
*link
) {
110 if (!socket_ipv6_is_supported())
113 return link_dhcp6_enabled(link
) || link_ipv6ll_enabled(link
) || network_has_static_ipv6_addresses(link
->network
);
116 static bool link_lldp_rx_enabled(Link
*link
) {
119 if (link
->flags
& IFF_LOOPBACK
)
122 if (link
->iftype
!= ARPHRD_ETHER
)
128 if (link
->network
->bridge
)
131 return link
->network
->lldp_mode
!= LLDP_MODE_NO
;
134 static bool link_lldp_emit_enabled(Link
*link
) {
137 if (link
->flags
& IFF_LOOPBACK
)
140 if (link
->iftype
!= ARPHRD_ETHER
)
146 return link
->network
->lldp_emit
!= LLDP_EMIT_NO
;
149 static bool link_ipv4_forward_enabled(Link
*link
) {
152 if (link
->flags
& IFF_LOOPBACK
)
158 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
161 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV4
;
164 static bool link_ipv6_forward_enabled(Link
*link
) {
167 if (!socket_ipv6_is_supported())
170 if (link
->flags
& IFF_LOOPBACK
)
176 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
179 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV6
;
182 static bool link_proxy_arp_enabled(Link
*link
) {
185 if (link
->flags
& IFF_LOOPBACK
)
191 if (link
->network
->proxy_arp
< 0)
197 static bool link_ipv6_accept_ra_enabled(Link
*link
) {
200 if (!socket_ipv6_is_supported())
203 if (link
->flags
& IFF_LOOPBACK
)
209 /* If unset use system default (enabled if local forwarding is disabled.
210 * disabled if local forwarding is enabled).
211 * If set, ignore or enforce RA independent of local forwarding state.
213 if (link
->network
->ipv6_accept_ra
< 0)
214 /* default to accept RA if ip_forward is disabled and ignore RA if ip_forward is enabled */
215 return !link_ipv6_forward_enabled(link
);
216 else if (link
->network
->ipv6_accept_ra
> 0)
217 /* accept RA even if ip_forward is enabled */
224 static IPv6PrivacyExtensions
link_ipv6_privacy_extensions(Link
*link
) {
227 if (!socket_ipv6_is_supported())
228 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
230 if (link
->flags
& IFF_LOOPBACK
)
231 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
234 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
236 return link
->network
->ipv6_privacy_extensions
;
239 static int link_enable_ipv6(Link
*link
) {
240 const char *p
= NULL
;
244 if (link
->flags
& IFF_LOOPBACK
)
247 disabled
= !link_ipv6_enabled(link
);
249 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/disable_ipv6");
251 r
= write_string_file(p
, one_zero(disabled
), WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
253 log_link_warning_errno(link
, r
, "Cannot %s IPv6 for interface %s: %m", disabled
? "disable" : "enable", link
->ifname
);
256 log_link_info(link
, "IPv6 disabled for interface: %m");
258 log_link_info(link
, "IPv6 enabled for interface: %m");
264 void link_update_operstate(Link
*link
) {
265 LinkOperationalState operstate
;
268 if (link
->kernel_operstate
== IF_OPER_DORMANT
)
269 operstate
= LINK_OPERSTATE_DORMANT
;
270 else if (link_has_carrier(link
)) {
272 uint8_t scope
= RT_SCOPE_NOWHERE
;
275 /* if we have carrier, check what addresses we have */
276 SET_FOREACH(address
, link
->addresses
, i
) {
277 if (!address_is_ready(address
))
280 if (address
->scope
< scope
)
281 scope
= address
->scope
;
284 /* for operstate we also take foreign addresses into account */
285 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
286 if (!address_is_ready(address
))
289 if (address
->scope
< scope
)
290 scope
= address
->scope
;
293 if (scope
< RT_SCOPE_SITE
)
294 /* universally accessible addresses found */
295 operstate
= LINK_OPERSTATE_ROUTABLE
;
296 else if (scope
< RT_SCOPE_HOST
)
297 /* only link or site local addresses found */
298 operstate
= LINK_OPERSTATE_DEGRADED
;
300 /* no useful addresses found */
301 operstate
= LINK_OPERSTATE_CARRIER
;
302 } else if (link
->flags
& IFF_UP
)
303 operstate
= LINK_OPERSTATE_NO_CARRIER
;
305 operstate
= LINK_OPERSTATE_OFF
;
307 if (link
->operstate
!= operstate
) {
308 link
->operstate
= operstate
;
309 link_send_changed(link
, "OperationalState", NULL
);
314 #define FLAG_STRING(string, flag, old, new) \
315 (((old ^ new) & flag) \
316 ? ((old & flag) ? (" -" string) : (" +" string)) \
319 static int link_update_flags(Link
*link
, sd_netlink_message
*m
) {
320 unsigned flags
, unknown_flags_added
, unknown_flags_removed
, unknown_flags
;
326 r
= sd_rtnl_message_link_get_flags(m
, &flags
);
328 return log_link_warning_errno(link
, r
, "Could not get link flags: %m");
330 r
= sd_netlink_message_read_u8(m
, IFLA_OPERSTATE
, &operstate
);
332 /* if we got a message without operstate, take it to mean
333 the state was unchanged */
334 operstate
= link
->kernel_operstate
;
336 if ((link
->flags
== flags
) && (link
->kernel_operstate
== operstate
))
339 if (link
->flags
!= flags
) {
340 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",
341 FLAG_STRING("LOOPBACK", IFF_LOOPBACK
, link
->flags
, flags
),
342 FLAG_STRING("MASTER", IFF_MASTER
, link
->flags
, flags
),
343 FLAG_STRING("SLAVE", IFF_SLAVE
, link
->flags
, flags
),
344 FLAG_STRING("UP", IFF_UP
, link
->flags
, flags
),
345 FLAG_STRING("DORMANT", IFF_DORMANT
, link
->flags
, flags
),
346 FLAG_STRING("LOWER_UP", IFF_LOWER_UP
, link
->flags
, flags
),
347 FLAG_STRING("RUNNING", IFF_RUNNING
, link
->flags
, flags
),
348 FLAG_STRING("MULTICAST", IFF_MULTICAST
, link
->flags
, flags
),
349 FLAG_STRING("BROADCAST", IFF_BROADCAST
, link
->flags
, flags
),
350 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT
, link
->flags
, flags
),
351 FLAG_STRING("PROMISC", IFF_PROMISC
, link
->flags
, flags
),
352 FLAG_STRING("ALLMULTI", IFF_ALLMULTI
, link
->flags
, flags
),
353 FLAG_STRING("PORTSEL", IFF_PORTSEL
, link
->flags
, flags
),
354 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA
, link
->flags
, flags
),
355 FLAG_STRING("DYNAMIC", IFF_DYNAMIC
, link
->flags
, flags
),
356 FLAG_STRING("NOARP", IFF_NOARP
, link
->flags
, flags
),
357 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS
, link
->flags
, flags
),
358 FLAG_STRING("DEBUG", IFF_DEBUG
, link
->flags
, flags
),
359 FLAG_STRING("ECHO", IFF_ECHO
, link
->flags
, flags
));
361 unknown_flags
= ~(IFF_LOOPBACK
| IFF_MASTER
| IFF_SLAVE
| IFF_UP
|
362 IFF_DORMANT
| IFF_LOWER_UP
| IFF_RUNNING
|
363 IFF_MULTICAST
| IFF_BROADCAST
| IFF_POINTOPOINT
|
364 IFF_PROMISC
| IFF_ALLMULTI
| IFF_PORTSEL
|
365 IFF_AUTOMEDIA
| IFF_DYNAMIC
| IFF_NOARP
|
366 IFF_NOTRAILERS
| IFF_DEBUG
| IFF_ECHO
);
367 unknown_flags_added
= ((link
->flags
^ flags
) & flags
& unknown_flags
);
368 unknown_flags_removed
= ((link
->flags
^ flags
) & link
->flags
& unknown_flags
);
370 /* link flags are currently at most 18 bits, let's align to
372 if (unknown_flags_added
)
374 "Unknown link flags gained: %#.5x (ignoring)",
375 unknown_flags_added
);
377 if (unknown_flags_removed
)
379 "Unknown link flags lost: %#.5x (ignoring)",
380 unknown_flags_removed
);
384 link
->kernel_operstate
= operstate
;
386 link_update_operstate(link
);
391 static int link_new(Manager
*manager
, sd_netlink_message
*message
, Link
**ret
) {
392 _cleanup_link_unref_ Link
*link
= NULL
;
396 unsigned short iftype
;
402 r
= sd_netlink_message_get_type(message
, &type
);
405 else if (type
!= RTM_NEWLINK
)
408 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
411 else if (ifindex
<= 0)
414 r
= sd_rtnl_message_link_get_type(message
, &iftype
);
418 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &ifname
);
422 link
= new0(Link
, 1);
427 link
->manager
= manager
;
428 link
->state
= LINK_STATE_PENDING
;
429 link
->rtnl_extended_attrs
= true;
430 link
->ifindex
= ifindex
;
431 link
->iftype
= iftype
;
432 link
->ifname
= strdup(ifname
);
436 r
= sd_netlink_message_read_ether_addr(message
, IFLA_ADDRESS
, &link
->mac
);
438 log_link_debug_errno(link
, r
, "MAC address not found for new device, continuing without");
440 if (asprintf(&link
->state_file
, "/run/systemd/netif/links/%d", link
->ifindex
) < 0)
443 if (asprintf(&link
->lease_file
, "/run/systemd/netif/leases/%d", link
->ifindex
) < 0)
446 if (asprintf(&link
->lldp_file
, "/run/systemd/netif/lldp/%d", link
->ifindex
) < 0)
449 r
= hashmap_ensure_allocated(&manager
->links
, NULL
);
453 r
= hashmap_put(manager
->links
, INT_TO_PTR(link
->ifindex
), link
);
457 r
= link_update_flags(link
, message
);
467 static void link_free(Link
*link
) {
475 while (!set_isempty(link
->addresses
))
476 address_free(set_first(link
->addresses
));
478 while (!set_isempty(link
->addresses_foreign
))
479 address_free(set_first(link
->addresses_foreign
));
481 link
->addresses
= set_free(link
->addresses
);
483 link
->addresses_foreign
= set_free(link
->addresses_foreign
);
485 while ((address
= link
->pool_addresses
)) {
486 LIST_REMOVE(addresses
, link
->pool_addresses
, address
);
487 address_free(address
);
490 sd_dhcp_server_unref(link
->dhcp_server
);
491 sd_dhcp_client_unref(link
->dhcp_client
);
492 sd_dhcp_lease_unref(link
->dhcp_lease
);
494 link_lldp_emit_stop(link
);
496 free(link
->lease_file
);
498 sd_lldp_unref(link
->lldp
);
499 free(link
->lldp_file
);
501 sd_ipv4ll_unref(link
->ipv4ll
);
502 sd_dhcp6_client_unref(link
->dhcp6_client
);
503 sd_ndisc_unref(link
->ndisc_router_discovery
);
506 hashmap_remove(link
->manager
->links
, INT_TO_PTR(link
->ifindex
));
510 (void)unlink(link
->state_file
);
511 free(link
->state_file
);
513 udev_device_unref(link
->udev_device
);
515 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
)
516 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(carrier
->ifindex
));
517 hashmap_free(link
->bound_to_links
);
519 HASHMAP_FOREACH (carrier
, link
->bound_by_links
, i
)
520 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(carrier
->ifindex
));
521 hashmap_free(link
->bound_by_links
);
526 Link
*link_unref(Link
*link
) {
530 assert(link
->n_ref
> 0);
542 Link
*link_ref(Link
*link
) {
546 assert(link
->n_ref
> 0);
553 int link_get(Manager
*m
, int ifindex
, Link
**ret
) {
560 link
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
569 static void link_set_state(Link
*link
, LinkState state
) {
572 if (link
->state
== state
)
577 link_send_changed(link
, "AdministrativeState", NULL
);
580 static void link_enter_unmanaged(Link
*link
) {
583 log_link_debug(link
, "Unmanaged");
585 link_set_state(link
, LINK_STATE_UNMANAGED
);
590 static int link_stop_clients(Link
*link
) {
594 assert(link
->manager
);
595 assert(link
->manager
->event
);
597 if (link
->dhcp_client
) {
598 k
= sd_dhcp_client_stop(link
->dhcp_client
);
600 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv4 client: %m");
604 k
= sd_ipv4ll_stop(link
->ipv4ll
);
606 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 link-local: %m");
609 if (link
->dhcp6_client
) {
610 k
= sd_dhcp6_client_stop(link
->dhcp6_client
);
612 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv6 client: %m");
615 if (link
->ndisc_router_discovery
) {
616 k
= sd_ndisc_stop(link
->ndisc_router_discovery
);
618 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Discovery: %m");
621 link_lldp_emit_stop(link
);
625 void link_enter_failed(Link
*link
) {
628 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
631 log_link_warning(link
, "Failed");
633 link_set_state(link
, LINK_STATE_FAILED
);
635 link_stop_clients(link
);
640 static Address
* link_find_dhcp_server_address(Link
*link
) {
644 assert(link
->network
);
646 /* The first statically configured address if there is any */
647 LIST_FOREACH(addresses
, address
, link
->network
->static_addresses
) {
649 if (address
->family
!= AF_INET
)
652 if (in_addr_is_null(address
->family
, &address
->in_addr
))
658 /* If that didn't work, find a suitable address we got from the pool */
659 LIST_FOREACH(addresses
, address
, link
->pool_addresses
) {
660 if (address
->family
!= AF_INET
)
669 static int link_enter_configured(Link
*link
) {
671 assert(link
->network
);
672 assert(link
->state
== LINK_STATE_SETTING_ROUTES
);
674 log_link_info(link
, "Configured");
676 link_set_state(link
, LINK_STATE_CONFIGURED
);
683 void link_check_ready(Link
*link
) {
692 if (!link
->static_configured
)
695 if (link_ipv4ll_enabled(link
))
696 if (!link
->ipv4ll_address
||
700 if (link_ipv6ll_enabled(link
))
701 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) > 0)
704 if ((link_dhcp4_enabled(link
) && !link_dhcp6_enabled(link
) &&
705 !link
->dhcp4_configured
) ||
706 (link_dhcp6_enabled(link
) && !link_dhcp4_enabled(link
) &&
707 !link
->dhcp6_configured
) ||
708 (link_dhcp4_enabled(link
) && link_dhcp6_enabled(link
) &&
709 !link
->dhcp4_configured
&& !link
->dhcp6_configured
))
712 if (link_ipv6_accept_ra_enabled(link
) && !link
->ndisc_configured
)
715 SET_FOREACH(a
, link
->addresses
, i
)
716 if (!address_is_ready(a
))
719 if (link
->state
!= LINK_STATE_CONFIGURED
)
720 link_enter_configured(link
);
725 static int route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
726 _cleanup_link_unref_ Link
*link
= userdata
;
729 assert(link
->link_messages
> 0);
730 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
731 LINK_STATE_SETTING_ROUTES
, LINK_STATE_FAILED
,
734 link
->link_messages
--;
736 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
739 r
= sd_netlink_message_get_errno(m
);
740 if (r
< 0 && r
!= -EEXIST
)
741 log_link_warning_errno(link
, r
, "Could not set route: %m");
743 if (link
->link_messages
== 0) {
744 log_link_debug(link
, "Routes set");
745 link
->static_configured
= true;
746 link_check_ready(link
);
752 static int link_enter_set_routes(Link
*link
) {
757 assert(link
->network
);
758 assert(link
->state
== LINK_STATE_SETTING_ADDRESSES
);
760 link_set_state(link
, LINK_STATE_SETTING_ROUTES
);
762 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
763 r
= route_configure(rt
, link
, route_handler
);
765 log_link_warning_errno(link
, r
, "Could not set routes: %m");
766 link_enter_failed(link
);
770 link
->link_messages
++;
773 if (link
->link_messages
== 0) {
774 link
->static_configured
= true;
775 link_check_ready(link
);
777 log_link_debug(link
, "Setting routes");
782 int link_route_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
783 _cleanup_link_unref_ Link
*link
= userdata
;
788 assert(link
->ifname
);
790 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
793 r
= sd_netlink_message_get_errno(m
);
794 if (r
< 0 && r
!= -ESRCH
)
795 log_link_warning_errno(link
, r
, "Could not drop route: %m");
800 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
801 _cleanup_link_unref_ Link
*link
= userdata
;
807 assert(link
->ifname
);
808 assert(link
->link_messages
> 0);
809 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
810 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
812 link
->link_messages
--;
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
!= -EEXIST
)
819 log_link_warning_errno(link
, r
, "could not set address: %m");
821 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
823 if (link
->link_messages
== 0) {
824 log_link_debug(link
, "Addresses set");
825 link_enter_set_routes(link
);
831 static int link_push_dns_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
832 _cleanup_free_
struct in_addr
*addresses
= NULL
;
833 size_t n_addresses
= 0, n_allocated
= 0;
836 log_debug("Copying DNS server information from %s", link
->ifname
);
841 STRV_FOREACH(a
, link
->network
->dns
) {
844 /* Only look for IPv4 addresses */
845 if (inet_pton(AF_INET
, *a
, &ia
) <= 0)
848 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
851 addresses
[n_addresses
++] = ia
;
854 if (link
->network
->dhcp_use_dns
&&
856 const struct in_addr
*da
= NULL
;
859 n
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &da
);
862 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
865 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
870 if (n_addresses
<= 0)
873 return sd_dhcp_server_set_dns(s
, addresses
, n_addresses
);
876 static int link_push_ntp_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
877 _cleanup_free_
struct in_addr
*addresses
= NULL
;
878 size_t n_addresses
= 0, n_allocated
= 0;
884 log_debug("Copying NTP server information from %s", link
->ifname
);
886 STRV_FOREACH(a
, link
->network
->ntp
) {
889 /* Only look for IPv4 addresses */
890 if (inet_pton(AF_INET
, *a
, &ia
) <= 0)
893 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
896 addresses
[n_addresses
++] = ia
;
899 if (link
->network
->dhcp_use_ntp
&&
901 const struct in_addr
*da
= NULL
;
904 n
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &da
);
907 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
910 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
915 if (n_addresses
<= 0)
918 return sd_dhcp_server_set_ntp(s
, addresses
, n_addresses
);
921 static int link_enter_set_addresses(Link
*link
) {
926 assert(link
->network
);
927 assert(link
->state
!= _LINK_STATE_INVALID
);
929 link_set_state(link
, LINK_STATE_SETTING_ADDRESSES
);
931 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
932 r
= address_configure(ad
, link
, address_handler
, false);
934 log_link_warning_errno(link
, r
, "Could not set addresses: %m");
935 link_enter_failed(link
);
939 link
->link_messages
++;
942 /* now that we can figure out a default address for the dhcp server,
944 if (link_dhcp4_server_enabled(link
)) {
947 bool acquired_uplink
= false;
949 address
= link_find_dhcp_server_address(link
);
951 log_link_warning(link
, "Failed to find suitable address for DHCPv4 server instance.");
952 link_enter_failed(link
);
956 /* use the server address' subnet as the pool */
957 r
= sd_dhcp_server_configure_pool(link
->dhcp_server
, &address
->in_addr
.in
, address
->prefixlen
,
958 link
->network
->dhcp_server_pool_offset
, link
->network
->dhcp_server_pool_size
);
963 r = sd_dhcp_server_set_router(link->dhcp_server,
964 &main_address->in_addr.in);
969 if (link
->network
->dhcp_server_max_lease_time_usec
> 0) {
970 r
= sd_dhcp_server_set_max_lease_time(
972 DIV_ROUND_UP(link
->network
->dhcp_server_max_lease_time_usec
, USEC_PER_SEC
));
977 if (link
->network
->dhcp_server_default_lease_time_usec
> 0) {
978 r
= sd_dhcp_server_set_default_lease_time(
980 DIV_ROUND_UP(link
->network
->dhcp_server_default_lease_time_usec
, USEC_PER_SEC
));
985 if (link
->network
->dhcp_server_emit_dns
) {
987 if (link
->network
->n_dhcp_server_dns
> 0)
988 r
= sd_dhcp_server_set_dns(link
->dhcp_server
, link
->network
->dhcp_server_dns
, link
->network
->n_dhcp_server_dns
);
990 uplink
= manager_find_uplink(link
->manager
, link
);
991 acquired_uplink
= true;
994 log_link_debug(link
, "Not emitting DNS server information on link, couldn't find suitable uplink.");
997 r
= link_push_dns_to_dhcp_server(uplink
, link
->dhcp_server
);
1000 log_link_warning_errno(link
, r
, "Failed to set DNS server for DHCP server, ignoring: %m");
1004 if (link
->network
->dhcp_server_emit_ntp
) {
1006 if (link
->network
->n_dhcp_server_ntp
> 0)
1007 r
= sd_dhcp_server_set_ntp(link
->dhcp_server
, link
->network
->dhcp_server_ntp
, link
->network
->n_dhcp_server_ntp
);
1009 if (!acquired_uplink
)
1010 uplink
= manager_find_uplink(link
->manager
, link
);
1013 log_link_debug(link
, "Not emitting NTP server information on link, couldn't find suitable uplink.");
1016 r
= link_push_ntp_to_dhcp_server(uplink
, link
->dhcp_server
);
1020 log_link_warning_errno(link
, r
, "Failed to set NTP server for DHCP server, ignoring: %m");
1023 r
= sd_dhcp_server_set_emit_router(link
->dhcp_server
, link
->network
->dhcp_server_emit_router
);
1025 log_link_warning_errno(link
, r
, "Failed to set router emission for DHCP server: %m");
1029 if (link
->network
->dhcp_server_emit_timezone
) {
1030 _cleanup_free_
char *buffer
= NULL
;
1031 const char *tz
= NULL
;
1033 if (link
->network
->dhcp_server_timezone
)
1034 tz
= link
->network
->dhcp_server_timezone
;
1036 r
= get_timezone(&buffer
);
1038 log_warning_errno(r
, "Failed to determine timezone: %m");
1044 r
= sd_dhcp_server_set_timezone(link
->dhcp_server
, tz
);
1050 r
= sd_dhcp_server_start(link
->dhcp_server
);
1052 log_link_warning_errno(link
, r
, "Could not start DHCPv4 server instance: %m");
1054 link_enter_failed(link
);
1059 log_link_debug(link
, "Offering DHCPv4 leases");
1062 if (link
->link_messages
== 0)
1063 link_enter_set_routes(link
);
1065 log_link_debug(link
, "Setting addresses");
1070 int link_address_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1071 _cleanup_link_unref_ Link
*link
= userdata
;
1076 assert(link
->ifname
);
1078 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1081 r
= sd_netlink_message_get_errno(m
);
1082 if (r
< 0 && r
!= -EADDRNOTAVAIL
)
1083 log_link_warning_errno(link
, r
, "Could not drop address: %m");
1088 static int link_set_bridge_fdb(Link
*const link
) {
1089 FdbEntry
*fdb_entry
;
1092 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
1093 r
= fdb_entry_configure(link
, fdb_entry
);
1095 log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
1103 static int link_set_proxy_arp(Link
*const link
) {
1104 const char *p
= NULL
;
1107 if (!link_proxy_arp_enabled(link
))
1110 p
= strjoina("/proc/sys/net/ipv4/conf/", link
->ifname
, "/proxy_arp");
1112 r
= write_string_file(p
, one_zero(link
->network
->proxy_arp
), WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
1114 log_link_warning_errno(link
, r
, "Cannot configure proxy ARP for interface: %m");
1119 static int link_set_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1120 _cleanup_link_unref_ Link
*link
= userdata
;
1123 log_link_debug(link
, "Set link");
1125 r
= sd_netlink_message_get_errno(m
);
1126 if (r
< 0 && r
!= -EEXIST
) {
1127 log_link_error_errno(link
, r
, "Could not join netdev: %m");
1128 link_enter_failed(link
);
1135 static int set_hostname_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
1136 _cleanup_link_unref_ Link
*link
= userdata
;
1137 const sd_bus_error
*e
;
1142 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1145 e
= sd_bus_message_get_error(m
);
1147 log_link_warning_errno(link
, sd_bus_error_get_errno(e
), "Could not set hostname: %s", e
->message
);
1152 int link_set_hostname(Link
*link
, const char *hostname
) {
1156 assert(link
->manager
);
1158 log_link_debug(link
, "Setting transient hostname: '%s'", strna(hostname
));
1160 if (!link
->manager
->bus
) {
1161 /* TODO: replace by assert when we can rely on kdbus */
1162 log_link_info(link
, "Not connected to system bus, ignoring transient hostname.");
1166 r
= sd_bus_call_method_async(
1169 "org.freedesktop.hostname1",
1170 "/org/freedesktop/hostname1",
1171 "org.freedesktop.hostname1",
1173 set_hostname_handler
,
1180 return log_link_error_errno(link
, r
, "Could not set transient hostname: %m");
1187 static int set_timezone_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
1188 _cleanup_link_unref_ Link
*link
= userdata
;
1189 const sd_bus_error
*e
;
1194 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1197 e
= sd_bus_message_get_error(m
);
1199 log_link_warning_errno(link
, sd_bus_error_get_errno(e
), "Could not set timezone: %s", e
->message
);
1204 int link_set_timezone(Link
*link
, const char *tz
) {
1208 assert(link
->manager
);
1211 log_link_debug(link
, "Setting system timezone: '%s'", tz
);
1213 if (!link
->manager
->bus
) {
1214 log_link_info(link
, "Not connected to system bus, ignoring timezone.");
1218 r
= sd_bus_call_method_async(
1221 "org.freedesktop.timedate1",
1222 "/org/freedesktop/timedate1",
1223 "org.freedesktop.timedate1",
1225 set_timezone_handler
,
1231 return log_link_error_errno(link
, r
, "Could not set timezone: %m");
1238 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1239 _cleanup_link_unref_ Link
*link
= userdata
;
1244 assert(link
->ifname
);
1246 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1249 r
= sd_netlink_message_get_errno(m
);
1251 log_link_warning_errno(link
, r
, "Could not set MTU: %m");
1256 int link_set_mtu(Link
*link
, uint32_t mtu
) {
1257 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1261 assert(link
->manager
);
1262 assert(link
->manager
->rtnl
);
1264 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1266 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1268 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1270 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1272 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1274 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, set_mtu_handler
, link
, 0, NULL
);
1276 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1283 static int link_set_bridge(Link
*link
) {
1284 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1288 assert(link
->network
);
1290 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1292 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1294 r
= sd_rtnl_message_link_set_family(req
, PF_BRIDGE
);
1296 return log_link_error_errno(link
, r
, "Could not set message family: %m");
1298 r
= sd_netlink_message_open_container(req
, IFLA_PROTINFO
);
1300 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1302 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_GUARD
, !link
->network
->use_bpdu
);
1304 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_GUARD attribute: %m");
1306 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MODE
, link
->network
->hairpin
);
1308 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MODE attribute: %m");
1310 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_FAST_LEAVE
, link
->network
->fast_leave
);
1312 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_FAST_LEAVE attribute: %m");
1314 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_PROTECT
, !link
->network
->allow_port_to_be_root
);
1316 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PROTECT attribute: %m");
1318 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_UNICAST_FLOOD
, link
->network
->unicast_flood
);
1320 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_UNICAST_FLOOD attribute: %m");
1322 if (link
->network
->cost
!= 0) {
1323 r
= sd_netlink_message_append_u32(req
, IFLA_BRPORT_COST
, link
->network
->cost
);
1325 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_COST attribute: %m");
1328 r
= sd_netlink_message_close_container(req
);
1330 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1332 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_set_handler
, link
, 0, NULL
);
1334 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1341 static int link_lldp_save(Link
*link
) {
1342 _cleanup_free_
char *temp_path
= NULL
;
1343 _cleanup_fclose_
FILE *f
= NULL
;
1344 sd_lldp_neighbor
**l
= NULL
;
1348 assert(link
->lldp_file
);
1351 (void) unlink(link
->lldp_file
);
1355 r
= sd_lldp_get_neighbors(link
->lldp
, &l
);
1359 (void) unlink(link
->lldp_file
);
1365 r
= fopen_temporary(link
->lldp_file
, &f
, &temp_path
);
1369 fchmod(fileno(f
), 0644);
1371 for (i
= 0; i
< n
; i
++) {
1376 r
= sd_lldp_neighbor_get_raw(l
[i
], &p
, &sz
);
1381 (void) fwrite(&u
, 1, sizeof(u
), f
);
1382 (void) fwrite(p
, 1, sz
, f
);
1385 r
= fflush_and_check(f
);
1389 if (rename(temp_path
, link
->lldp_file
) < 0) {
1396 (void) unlink(link
->lldp_file
);
1398 (void) unlink(temp_path
);
1400 log_link_error_errno(link
, r
, "Failed to save LLDP data to %s: %m", link
->lldp_file
);
1404 for (i
= 0; i
< n
; i
++)
1405 sd_lldp_neighbor_unref(l
[i
]);
1412 static void lldp_handler(sd_lldp
*lldp
, sd_lldp_event event
, sd_lldp_neighbor
*n
, void *userdata
) {
1413 Link
*link
= userdata
;
1418 (void) link_lldp_save(link
);
1420 if (link_lldp_emit_enabled(link
) && event
== SD_LLDP_EVENT_ADDED
) {
1421 /* If we received information about a new neighbor, restart the LLDP "fast" logic */
1423 log_link_debug(link
, "Received LLDP datagram from previously unknown neighbor, restarting 'fast' LLDP transmission.");
1425 r
= link_lldp_emit_start(link
);
1427 log_link_warning_errno(link
, r
, "Failed to restart LLDP transmission: %m");
1431 static int link_acquire_ipv6_conf(Link
*link
) {
1436 if (link_dhcp6_enabled(link
)) {
1437 assert(link
->dhcp6_client
);
1438 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1440 log_link_debug(link
, "Acquiring DHCPv6 lease");
1442 r
= sd_dhcp6_client_set_local_address(link
->dhcp6_client
, &link
->ipv6ll_address
);
1443 if (r
< 0 && r
!= -EBUSY
)
1444 return log_link_warning_errno(link
, r
, "Could not set IPv6LL address in DHCP client: %m");
1446 r
= sd_dhcp6_client_start(link
->dhcp6_client
);
1447 if (r
< 0 && r
!= -EBUSY
)
1448 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv6 lease: %m");
1451 if (link_ipv6_accept_ra_enabled(link
)) {
1452 assert(link
->ndisc_router_discovery
);
1454 log_link_debug(link
, "Discovering IPv6 routers");
1456 r
= sd_ndisc_router_discovery_start(link
->ndisc_router_discovery
);
1457 if (r
< 0 && r
!= -EBUSY
)
1458 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1464 static int link_acquire_ipv4_conf(Link
*link
) {
1468 assert(link
->network
);
1469 assert(link
->manager
);
1470 assert(link
->manager
->event
);
1472 if (link_ipv4ll_enabled(link
)) {
1473 assert(link
->ipv4ll
);
1475 log_link_debug(link
, "Acquiring IPv4 link-local address");
1477 r
= sd_ipv4ll_start(link
->ipv4ll
);
1479 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1482 if (link_dhcp4_enabled(link
)) {
1483 assert(link
->dhcp_client
);
1485 log_link_debug(link
, "Acquiring DHCPv4 lease");
1487 r
= sd_dhcp_client_start(link
->dhcp_client
);
1489 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1495 static int link_acquire_conf(Link
*link
) {
1500 r
= link_acquire_ipv4_conf(link
);
1504 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) == 0) {
1505 r
= link_acquire_ipv6_conf(link
);
1510 if (link_lldp_emit_enabled(link
)) {
1511 r
= link_lldp_emit_start(link
);
1513 return log_link_warning_errno(link
, r
, "Failed to start LLDP transmission: %m");
1519 bool link_has_carrier(Link
*link
) {
1520 /* see Documentation/networking/operstates.txt in the kernel sources */
1522 if (link
->kernel_operstate
== IF_OPER_UP
)
1525 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1526 /* operstate may not be implemented, so fall back to flags */
1527 if ((link
->flags
& IFF_LOWER_UP
) && !(link
->flags
& IFF_DORMANT
))
1533 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1534 _cleanup_link_unref_ Link
*link
= userdata
;
1539 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1542 r
= sd_netlink_message_get_errno(m
);
1544 /* we warn but don't fail the link, as it may be
1546 log_link_warning_errno(link
, r
, "Could not bring up interface: %m");
1551 static int link_up(Link
*link
) {
1552 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1553 uint8_t ipv6ll_mode
;
1557 assert(link
->network
);
1558 assert(link
->manager
);
1559 assert(link
->manager
->rtnl
);
1561 log_link_debug(link
, "Bringing link up");
1563 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1565 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1567 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1569 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1571 if (link
->network
->mac
) {
1572 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
1574 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
1577 /* If IPv6 not configured (no static IPv6 address and neither DHCPv6 nor IPv6LL is enabled)
1578 for this interface then disable IPv6 else enable it. */
1579 (void) link_enable_ipv6(link
);
1581 if (link
->network
->mtu
) {
1582 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
1583 on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */
1584 if (link_ipv6_enabled(link
) && link
->network
->mtu
< IPV6_MIN_MTU
) {
1586 log_link_warning(link
, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU
) ", as "
1587 "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU
) " bytes: %m");
1589 link
->network
->mtu
= IPV6_MIN_MTU
;
1592 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, link
->network
->mtu
);
1594 return log_link_error_errno(link
, r
, "Could not set MTU: %m");
1597 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1599 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1601 if (link_ipv6_enabled(link
)) {
1602 /* if the kernel lacks ipv6 support setting IFF_UP fails if any ipv6 options are passed */
1603 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1605 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1607 ipv6ll_mode
= link_ipv6ll_enabled(link
) ? IN6_ADDR_GEN_MODE_EUI64
: IN6_ADDR_GEN_MODE_NONE
;
1608 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1610 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1612 if (!in_addr_is_null(AF_INET6
, &link
->network
->ipv6_token
)) {
1613 r
= sd_netlink_message_append_in6_addr(req
, IFLA_INET6_TOKEN
, &link
->network
->ipv6_token
.in6
);
1615 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_TOKEN: %m");
1618 r
= sd_netlink_message_close_container(req
);
1620 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1623 r
= sd_netlink_message_close_container(req
);
1625 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1627 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_up_handler
, link
, 0, NULL
);
1629 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1636 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1637 _cleanup_link_unref_ Link
*link
= userdata
;
1642 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1645 r
= sd_netlink_message_get_errno(m
);
1647 log_link_warning_errno(link
, r
, "Could not bring down interface: %m");
1652 static int link_down(Link
*link
) {
1653 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1657 assert(link
->manager
);
1658 assert(link
->manager
->rtnl
);
1660 log_link_debug(link
, "Bringing link down");
1662 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
1663 RTM_SETLINK
, link
->ifindex
);
1665 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1667 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
1669 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1671 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_down_handler
, link
, 0, NULL
);
1673 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1680 static int link_handle_bound_to_list(Link
*link
) {
1684 bool required_up
= false;
1685 bool link_is_up
= false;
1689 if (hashmap_isempty(link
->bound_to_links
))
1692 if (link
->flags
& IFF_UP
)
1695 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
1696 if (link_has_carrier(l
)) {
1701 if (!required_up
&& link_is_up
) {
1702 r
= link_down(link
);
1705 } else if (required_up
&& !link_is_up
) {
1714 static int link_handle_bound_by_list(Link
*link
) {
1721 if (hashmap_isempty(link
->bound_by_links
))
1724 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
1725 r
= link_handle_bound_to_list(l
);
1733 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
1739 if (link
== carrier
)
1742 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
1745 r
= hashmap_ensure_allocated(h
, NULL
);
1749 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
1756 static int link_new_bound_by_list(Link
*link
) {
1761 bool list_updated
= false;
1764 assert(link
->manager
);
1768 HASHMAP_FOREACH(carrier
, m
->links
, i
) {
1769 if (!carrier
->network
)
1772 if (strv_isempty(carrier
->network
->bind_carrier
))
1775 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
, 0)) {
1776 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
1780 list_updated
= true;
1787 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
1788 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
1792 link_dirty(carrier
);
1798 static int link_new_bound_to_list(Link
*link
) {
1803 bool list_updated
= false;
1806 assert(link
->manager
);
1811 if (strv_isempty(link
->network
->bind_carrier
))
1816 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
1817 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
, 0)) {
1818 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
1822 list_updated
= true;
1829 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
1830 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
1834 link_dirty(carrier
);
1840 static int link_new_carrier_maps(Link
*link
) {
1843 r
= link_new_bound_by_list(link
);
1847 r
= link_handle_bound_by_list(link
);
1851 r
= link_new_bound_to_list(link
);
1855 r
= link_handle_bound_to_list(link
);
1862 static void link_free_bound_to_list(Link
*link
) {
1866 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
1867 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
1869 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
1870 link_dirty(bound_to
);
1876 static void link_free_bound_by_list(Link
*link
) {
1880 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
1881 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
1883 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
1884 link_dirty(bound_by
);
1885 link_handle_bound_to_list(bound_by
);
1892 static void link_free_carrier_maps(Link
*link
) {
1893 bool list_updated
= false;
1897 if (!hashmap_isempty(link
->bound_to_links
)) {
1898 link_free_bound_to_list(link
);
1899 list_updated
= true;
1902 if (!hashmap_isempty(link
->bound_by_links
)) {
1903 link_free_bound_by_list(link
);
1904 list_updated
= true;
1913 void link_drop(Link
*link
) {
1914 if (!link
|| link
->state
== LINK_STATE_LINGER
)
1917 link_set_state(link
, LINK_STATE_LINGER
);
1919 link_free_carrier_maps(link
);
1921 log_link_debug(link
, "Link removed");
1923 (void)unlink(link
->state_file
);
1929 static int link_joined(Link
*link
) {
1933 assert(link
->network
);
1935 if (!hashmap_isempty(link
->bound_to_links
)) {
1936 r
= link_handle_bound_to_list(link
);
1939 } else if (!(link
->flags
& IFF_UP
)) {
1942 link_enter_failed(link
);
1947 if (link
->network
->bridge
) {
1948 r
= link_set_bridge(link
);
1950 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
1953 return link_enter_set_addresses(link
);
1956 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1957 _cleanup_link_unref_ Link
*link
= userdata
;
1961 assert(link
->network
);
1965 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1968 r
= sd_netlink_message_get_errno(m
);
1969 if (r
< 0 && r
!= -EEXIST
) {
1970 log_link_error_errno(link
, r
, "Could not join netdev: %m");
1971 link_enter_failed(link
);
1974 log_link_debug(link
, "Joined netdev");
1976 if (link
->enslaving
<= 0)
1982 static int link_enter_join_netdev(Link
*link
) {
1988 assert(link
->network
);
1989 assert(link
->state
== LINK_STATE_PENDING
);
1991 link_set_state(link
, LINK_STATE_ENSLAVING
);
1995 if (!link
->network
->bridge
&&
1996 !link
->network
->bond
&&
1997 hashmap_isempty(link
->network
->stacked_netdevs
))
1998 return link_joined(link
);
2000 if (link
->network
->bond
) {
2001 log_struct(LOG_DEBUG
,
2002 LOG_LINK_INTERFACE(link
),
2003 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2004 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
),
2007 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
2009 log_struct_errno(LOG_WARNING
, r
,
2010 LOG_LINK_INTERFACE(link
),
2011 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2012 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
),
2015 link_enter_failed(link
);
2022 if (link
->network
->bridge
) {
2023 log_struct(LOG_DEBUG
,
2024 LOG_LINK_INTERFACE(link
),
2025 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2026 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
),
2029 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
2031 log_struct_errno(LOG_WARNING
, r
,
2032 LOG_LINK_INTERFACE(link
),
2033 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2034 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
),
2036 link_enter_failed(link
);
2043 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
2045 log_struct(LOG_DEBUG
,
2046 LOG_LINK_INTERFACE(link
),
2047 LOG_NETDEV_INTERFACE(netdev
),
2048 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
),
2051 r
= netdev_join(netdev
, link
, netdev_join_handler
);
2053 log_struct_errno(LOG_WARNING
, r
,
2054 LOG_LINK_INTERFACE(link
),
2055 LOG_NETDEV_INTERFACE(netdev
),
2056 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
),
2058 link_enter_failed(link
);
2068 static int link_set_ipv4_forward(Link
*link
) {
2071 if (!link_ipv4_forward_enabled(link
))
2074 /* We propagate the forwarding flag from one interface to the
2075 * global setting one way. This means: as long as at least one
2076 * interface was configured at any time that had IP forwarding
2077 * enabled the setting will stay on for good. We do this
2078 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
2079 * somewhat in sync (see below). */
2081 r
= write_string_file("/proc/sys/net/ipv4/ip_forward", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2083 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
2088 static int link_set_ipv6_forward(Link
*link
) {
2091 if (!link_ipv6_forward_enabled(link
))
2094 /* On Linux, the IPv6 stack does not not know a per-interface
2095 * packet forwarding setting: either packet forwarding is on
2096 * for all, or off for all. We hence don't bother with a
2097 * per-interface setting, but simply propagate the interface
2098 * flag, if it is set, to the global flag, one-way. Note that
2099 * while IPv4 would allow a per-interface flag, we expose the
2100 * same behaviour there and also propagate the setting from
2101 * one to all, to keep things simple (see above). */
2103 r
= write_string_file("/proc/sys/net/ipv6/conf/all/forwarding", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2105 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
2110 static int link_set_ipv6_privacy_extensions(Link
*link
) {
2111 char buf
[DECIMAL_STR_MAX(unsigned) + 1];
2112 IPv6PrivacyExtensions s
;
2113 const char *p
= NULL
;
2116 s
= link_ipv6_privacy_extensions(link
);
2120 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/use_tempaddr");
2121 xsprintf(buf
, "%u", (unsigned) link
->network
->ipv6_privacy_extensions
);
2123 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2125 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
2130 static int link_set_ipv6_accept_ra(Link
*link
) {
2131 const char *p
= NULL
;
2134 /* Make this a NOP if IPv6 is not available */
2135 if (!socket_ipv6_is_supported())
2138 if (link
->flags
& IFF_LOOPBACK
)
2144 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/accept_ra");
2146 /* We handle router advertisments ourselves, tell the kernel to GTFO */
2147 r
= write_string_file(p
, "0", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2149 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
2154 static int link_set_ipv6_dad_transmits(Link
*link
) {
2155 char buf
[DECIMAL_STR_MAX(int) + 1];
2156 const char *p
= NULL
;
2159 /* Make this a NOP if IPv6 is not available */
2160 if (!socket_ipv6_is_supported())
2163 if (link
->flags
& IFF_LOOPBACK
)
2169 if (link
->network
->ipv6_dad_transmits
< 0)
2172 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/dad_transmits");
2173 xsprintf(buf
, "%i", link
->network
->ipv6_dad_transmits
);
2175 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2177 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
2182 static int link_set_ipv6_hop_limit(Link
*link
) {
2183 char buf
[DECIMAL_STR_MAX(int) + 1];
2184 const char *p
= NULL
;
2187 /* Make this a NOP if IPv6 is not available */
2188 if (!socket_ipv6_is_supported())
2191 if (link
->flags
& IFF_LOOPBACK
)
2197 if (link
->network
->ipv6_hop_limit
< 0)
2200 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/hop_limit");
2201 xsprintf(buf
, "%i", link
->network
->ipv6_hop_limit
);
2203 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2205 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2210 static int link_drop_foreign_config(Link
*link
) {
2216 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
2217 /* we consider IPv6LL addresses to be managed by the kernel */
2218 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2221 r
= address_remove(address
, link
, link_address_remove_handler
);
2226 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2227 /* do not touch routes managed by the kernel */
2228 if (route
->protocol
== RTPROT_KERNEL
)
2231 r
= route_remove(route
, link
, link_address_remove_handler
);
2239 static int link_update_lldp(Link
*link
) {
2247 if (link
->flags
& IFF_UP
) {
2248 r
= sd_lldp_start(link
->lldp
);
2250 log_link_debug(link
, "Started LLDP.");
2252 r
= sd_lldp_stop(link
->lldp
);
2254 log_link_debug(link
, "Stopped LLDP.");
2260 static int link_configure(Link
*link
) {
2264 assert(link
->network
);
2265 assert(link
->state
== LINK_STATE_PENDING
);
2267 /* Drop foreign config, but ignore loopback or critical devices.
2268 * We do not want to remove loopback address or addresses used for root NFS. */
2269 if (!(link
->flags
& IFF_LOOPBACK
) && !(link
->network
->dhcp_critical
)) {
2270 r
= link_drop_foreign_config(link
);
2275 r
= link_set_bridge_fdb(link
);
2279 r
= link_set_proxy_arp(link
);
2283 r
= link_set_ipv4_forward(link
);
2287 r
= link_set_ipv6_forward(link
);
2291 r
= link_set_ipv6_privacy_extensions(link
);
2295 r
= link_set_ipv6_accept_ra(link
);
2299 r
= link_set_ipv6_dad_transmits(link
);
2303 r
= link_set_ipv6_hop_limit(link
);
2307 if (link_ipv4ll_enabled(link
)) {
2308 r
= ipv4ll_configure(link
);
2313 if (link_dhcp4_enabled(link
)) {
2314 r
= dhcp4_configure(link
);
2319 if (link_dhcp4_server_enabled(link
)) {
2320 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
2324 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
2329 if (link_dhcp6_enabled(link
) ||
2330 link_ipv6_accept_ra_enabled(link
)) {
2331 r
= dhcp6_configure(link
);
2336 if (link_ipv6_accept_ra_enabled(link
)) {
2337 r
= ndisc_configure(link
);
2342 if (link_lldp_rx_enabled(link
)) {
2343 r
= sd_lldp_new(&link
->lldp
, link
->ifindex
);
2347 r
= sd_lldp_match_capabilities(link
->lldp
,
2348 link
->network
->lldp_mode
== LLDP_MODE_ROUTERS_ONLY
?
2349 SD_LLDP_SYSTEM_CAPABILITIES_ALL_ROUTERS
:
2350 SD_LLDP_SYSTEM_CAPABILITIES_ALL
);
2354 r
= sd_lldp_set_filter_address(link
->lldp
, &link
->mac
);
2358 r
= sd_lldp_attach_event(link
->lldp
, NULL
, 0);
2362 r
= sd_lldp_set_callback(link
->lldp
, lldp_handler
, link
);
2366 r
= link_update_lldp(link
);
2371 if (link_has_carrier(link
)) {
2372 r
= link_acquire_conf(link
);
2377 return link_enter_join_netdev(link
);
2380 static int link_initialized_and_synced(sd_netlink
*rtnl
, sd_netlink_message
*m
,
2382 _cleanup_link_unref_ Link
*link
= userdata
;
2387 assert(link
->ifname
);
2388 assert(link
->manager
);
2390 if (link
->state
!= LINK_STATE_PENDING
)
2393 log_link_debug(link
, "Link state is up-to-date");
2395 r
= link_new_bound_by_list(link
);
2399 r
= link_handle_bound_by_list(link
);
2403 if (!link
->network
) {
2404 r
= network_get(link
->manager
, link
->udev_device
, link
->ifname
,
2405 &link
->mac
, &network
);
2407 link_enter_unmanaged(link
);
2412 if (link
->flags
& IFF_LOOPBACK
) {
2413 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
2414 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
2416 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
2417 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
2419 if (network
->dhcp_server
)
2420 log_link_debug(link
, "Ignoring DHCP server for loopback link");
2423 r
= network_apply(link
->manager
, network
, link
);
2428 r
= link_new_bound_to_list(link
);
2432 r
= link_configure(link
);
2439 int link_initialized(Link
*link
, struct udev_device
*device
) {
2440 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2444 assert(link
->manager
);
2445 assert(link
->manager
->rtnl
);
2448 if (link
->state
!= LINK_STATE_PENDING
)
2451 if (link
->udev_device
)
2454 log_link_debug(link
, "udev initialized link");
2456 link
->udev_device
= udev_device_ref(device
);
2458 /* udev has initialized the link, but we don't know if we have yet
2459 * processed the NEWLINK messages with the latest state. Do a GETLINK,
2460 * when it returns we know that the pending NEWLINKs have already been
2461 * processed and that we are up-to-date */
2463 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
2468 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
,
2469 link_initialized_and_synced
, link
, 0, NULL
);
2478 static int link_load(Link
*link
) {
2479 _cleanup_free_
char *network_file
= NULL
,
2482 *dhcp4_address
= NULL
,
2483 *ipv4ll_address
= NULL
;
2484 union in_addr_union address
;
2485 union in_addr_union route_dst
;
2491 r
= parse_env_file(link
->state_file
, NEWLINE
,
2492 "NETWORK_FILE", &network_file
,
2493 "ADDRESSES", &addresses
,
2495 "DHCP4_ADDRESS", &dhcp4_address
,
2496 "IPV4LL_ADDRESS", &ipv4ll_address
,
2498 if (r
< 0 && r
!= -ENOENT
)
2499 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
2506 suffix
= strrchr(network_file
, '.');
2508 log_link_debug(link
, "Failed to get network name from %s", network_file
);
2509 goto network_file_fail
;
2513 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
2515 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
2516 goto network_file_fail
;
2519 r
= network_apply(link
->manager
, network
, link
);
2521 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
2530 _cleanup_free_
char *address_str
= NULL
;
2531 char *prefixlen_str
;
2533 unsigned char prefixlen
;
2535 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
2537 log_link_debug_errno(link
, r
, "Failed to extract next address string: %m");
2543 prefixlen_str
= strchr(address_str
, '/');
2544 if (!prefixlen_str
) {
2545 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
2549 *prefixlen_str
++ = '\0';
2551 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
2553 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
2557 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
2559 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
2563 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
2565 return log_link_error_errno(link
, r
, "Failed to add address: %m");
2574 _cleanup_free_
char *route_str
= NULL
;
2575 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
2577 char *prefixlen_str
;
2579 unsigned char prefixlen
, tos
, table
;
2582 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
2584 log_link_debug_errno(link
, r
, "Failed to extract next route string: %m");
2590 prefixlen_str
= strchr(route_str
, '/');
2591 if (!prefixlen_str
) {
2592 log_link_debug(link
, "Failed to parse route %s", route_str
);
2596 *prefixlen_str
++ = '\0';
2598 r
= sscanf(prefixlen_str
, "%hhu/%hhu/%"SCNu32
"/%hhu/"USEC_FMT
, &prefixlen
, &tos
, &priority
, &table
, &lifetime
);
2600 log_link_debug(link
,
2601 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
2606 r
= in_addr_from_string_auto(route_str
, &family
, &route_dst
);
2608 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
2612 r
= route_add(link
, family
, &route_dst
, prefixlen
, tos
, priority
, table
, &route
);
2614 return log_link_error_errno(link
, r
, "Failed to add route: %m");
2616 if (lifetime
!= USEC_INFINITY
) {
2617 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(), lifetime
,
2618 0, route_expire_handler
, route
);
2620 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
2623 route
->lifetime
= lifetime
;
2624 sd_event_source_unref(route
->expire
);
2625 route
->expire
= expire
;
2630 if (dhcp4_address
) {
2631 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
2633 log_link_debug_errno(link
, r
, "Falied to parse DHCPv4 address %s: %m", dhcp4_address
);
2634 goto dhcp4_address_fail
;
2637 r
= sd_dhcp_client_new(&link
->dhcp_client
);
2639 return log_link_error_errno(link
, r
, "Falied to create DHCPv4 client: %m");
2641 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
2643 return log_link_error_errno(link
, r
, "Falied to set inital DHCPv4 address %s: %m", dhcp4_address
);
2648 if (ipv4ll_address
) {
2649 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
2651 log_link_debug_errno(link
, r
, "Falied to parse IPv4LL address %s: %m", ipv4ll_address
);
2652 goto ipv4ll_address_fail
;
2655 r
= sd_ipv4ll_new(&link
->ipv4ll
);
2657 return log_link_error_errno(link
, r
, "Falied to create IPv4LL client: %m");
2659 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
2661 return log_link_error_errno(link
, r
, "Falied to set inital IPv4LL address %s: %m", ipv4ll_address
);
2664 ipv4ll_address_fail
:
2669 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
2671 _cleanup_udev_device_unref_
struct udev_device
*device
= NULL
;
2672 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
2680 r
= link_new(m
, message
, ret
);
2686 log_link_debug(link
, "Link %d added", link
->ifindex
);
2688 r
= link_load(link
);
2692 if (detect_container() <= 0) {
2693 /* not in a container, udev will be around */
2694 sprintf(ifindex_str
, "n%d", link
->ifindex
);
2695 device
= udev_device_new_from_device_id(m
->udev
, ifindex_str
);
2697 r
= log_link_warning_errno(link
, errno
, "Could not find udev device: %m");
2701 if (udev_device_get_is_initialized(device
) <= 0) {
2703 log_link_debug(link
, "link pending udev initialization...");
2707 r
= link_initialized(link
, device
);
2711 /* we are calling a callback directly, so must take a ref */
2714 r
= link_initialized_and_synced(m
->rtnl
, NULL
, link
);
2721 link_enter_failed(link
);
2725 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
2730 log_link_info(link
, "Gained IPv6LL");
2732 link
->ipv6ll_address
= *address
;
2733 link_check_ready(link
);
2735 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
2736 r
= link_acquire_ipv6_conf(link
);
2738 link_enter_failed(link
);
2746 static int link_carrier_gained(Link
*link
) {
2751 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
2752 r
= link_acquire_conf(link
);
2754 link_enter_failed(link
);
2758 r
= link_enter_set_addresses(link
);
2763 r
= link_handle_bound_by_list(link
);
2770 static int link_carrier_lost(Link
*link
) {
2775 r
= link_stop_clients(link
);
2777 link_enter_failed(link
);
2781 r
= link_handle_bound_by_list(link
);
2788 int link_carrier_reset(Link
*link
) {
2793 if (link_has_carrier(link
)) {
2794 r
= link_carrier_lost(link
);
2798 r
= link_carrier_gained(link
);
2802 log_link_info(link
, "Reset carrier");
2808 int link_update(Link
*link
, sd_netlink_message
*m
) {
2809 struct ether_addr mac
;
2812 bool had_carrier
, carrier_gained
, carrier_lost
;
2816 assert(link
->ifname
);
2819 if (link
->state
== LINK_STATE_LINGER
) {
2821 log_link_info(link
, "Link readded");
2822 link_set_state(link
, LINK_STATE_ENSLAVING
);
2824 r
= link_new_carrier_maps(link
);
2829 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
2830 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
2831 log_link_info(link
, "Renamed to %s", ifname
);
2833 link_free_carrier_maps(link
);
2835 r
= free_and_strdup(&link
->ifname
, ifname
);
2839 r
= link_new_carrier_maps(link
);
2844 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
2845 if (r
>= 0 && mtu
> 0) {
2847 if (!link
->original_mtu
) {
2848 link
->original_mtu
= mtu
;
2849 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
2852 if (link
->dhcp_client
) {
2853 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
2856 log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
2862 /* The kernel may broadcast NEWLINK messages without the MAC address
2863 set, simply ignore them. */
2864 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
2866 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
2869 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
2872 log_link_debug(link
, "MAC address: "
2873 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
2874 mac
.ether_addr_octet
[0],
2875 mac
.ether_addr_octet
[1],
2876 mac
.ether_addr_octet
[2],
2877 mac
.ether_addr_octet
[3],
2878 mac
.ether_addr_octet
[4],
2879 mac
.ether_addr_octet
[5]);
2882 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
2884 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
2887 if (link
->dhcp_client
) {
2888 const DUID
*duid
= link_duid(link
);
2890 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
2891 (const uint8_t *) &link
->mac
,
2895 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
2897 r
= sd_dhcp_client_set_iaid_duid(link
->dhcp_client
,
2898 link
->network
->iaid
,
2900 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
2901 duid
->raw_data_len
);
2903 return log_link_warning_errno(link
, r
, "Could not update DUID/IAID in DHCP client: %m");
2906 if (link
->dhcp6_client
) {
2907 const DUID
* duid
= link_duid(link
);
2909 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
2910 (const uint8_t *) &link
->mac
,
2914 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
2916 r
= sd_dhcp6_client_set_iaid(link
->dhcp6_client
,
2917 link
->network
->iaid
);
2919 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 IAID: %m");
2921 r
= sd_dhcp6_client_set_duid(link
->dhcp6_client
,
2923 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
2924 duid
->raw_data_len
);
2926 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 DUID: %m");
2931 had_carrier
= link_has_carrier(link
);
2933 r
= link_update_flags(link
, m
);
2937 r
= link_update_lldp(link
);
2941 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
2942 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
2944 if (carrier_gained
) {
2945 log_link_info(link
, "Gained carrier");
2947 r
= link_carrier_gained(link
);
2950 } else if (carrier_lost
) {
2951 log_link_info(link
, "Lost carrier");
2953 r
= link_carrier_lost(link
);
2961 static void print_link_hashmap(FILE *f
, const char *prefix
, Hashmap
* h
) {
2969 if (hashmap_isempty(h
))
2973 HASHMAP_FOREACH(link
, h
, i
) {
2977 fprintf(f
, "%i", link
->ifindex
);
2984 int link_save(Link
*link
) {
2985 _cleanup_free_
char *temp_path
= NULL
;
2986 _cleanup_fclose_
FILE *f
= NULL
;
2987 const char *admin_state
, *oper_state
;
2994 assert(link
->state_file
);
2995 assert(link
->lease_file
);
2996 assert(link
->manager
);
2998 if (link
->state
== LINK_STATE_LINGER
) {
2999 unlink(link
->state_file
);
3003 link_lldp_save(link
);
3005 admin_state
= link_state_to_string(link
->state
);
3006 assert(admin_state
);
3008 oper_state
= link_operstate_to_string(link
->operstate
);
3011 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
3015 fchmod(fileno(f
), 0644);
3018 "# This is private data. Do not parse.\n"
3021 admin_state
, oper_state
);
3023 if (link
->network
) {
3025 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
3026 const char *dhcp_domainname
= NULL
;
3027 char **dhcp6_domains
= NULL
;
3029 if (link
->dhcp6_client
) {
3030 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
, &dhcp6_lease
);
3031 if (r
< 0 && r
!= -ENOMSG
)
3032 log_link_debug(link
, "No DHCPv6 lease");
3035 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
3039 fputstrv(f
, link
->network
->dns
, NULL
, &space
);
3041 if (link
->network
->dhcp_use_dns
&&
3043 const struct in_addr
*addresses
;
3045 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
3049 serialize_in_addrs(f
, addresses
, r
);
3054 if (link
->network
->dhcp_use_dns
&& dhcp6_lease
) {
3055 struct in6_addr
*in6_addrs
;
3057 r
= sd_dhcp6_lease_get_dns(dhcp6_lease
, &in6_addrs
);
3061 serialize_in6_addrs(f
, in6_addrs
, r
);
3069 fputstrv(f
, link
->network
->ntp
, NULL
, &space
);
3071 if (link
->network
->dhcp_use_ntp
&&
3073 const struct in_addr
*addresses
;
3075 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
3079 serialize_in_addrs(f
, addresses
, r
);
3084 if (link
->network
->dhcp_use_ntp
&& dhcp6_lease
) {
3085 struct in6_addr
*in6_addrs
;
3088 r
= sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease
,
3093 serialize_in6_addrs(f
, in6_addrs
, r
);
3097 r
= sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease
, &hosts
);
3099 fputstrv(f
, hosts
, NULL
, &space
);
3104 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
3105 if (link
->dhcp_lease
)
3106 (void) sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &dhcp_domainname
);
3109 (void) sd_dhcp6_lease_get_domains(dhcp6_lease
, &dhcp6_domains
);
3112 fputs("DOMAINS=", f
);
3113 fputstrv(f
, link
->network
->search_domains
, NULL
, &space
);
3115 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
&& dhcp_domainname
)
3116 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3118 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
&& dhcp6_domains
)
3119 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3123 fputs("ROUTE_DOMAINS=", f
);
3124 fputstrv(f
, link
->network
->route_domains
, NULL
, NULL
);
3126 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
&& dhcp_domainname
)
3127 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3129 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
&& dhcp6_domains
)
3130 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3134 fprintf(f
, "LLMNR=%s\n",
3135 resolve_support_to_string(link
->network
->llmnr
));
3136 fprintf(f
, "MDNS=%s\n",
3137 resolve_support_to_string(link
->network
->mdns
));
3139 if (link
->network
->dnssec_mode
!= _DNSSEC_MODE_INVALID
)
3140 fprintf(f
, "DNSSEC=%s\n",
3141 dnssec_mode_to_string(link
->network
->dnssec_mode
));
3143 if (!set_isempty(link
->network
->dnssec_negative_trust_anchors
)) {
3146 fputs("DNSSEC_NTA=", f
);
3148 SET_FOREACH(n
, link
->network
->dnssec_negative_trust_anchors
, i
)
3149 fputs_with_space(f
, n
, NULL
, &space
);
3153 fputs("ADDRESSES=", f
);
3155 SET_FOREACH(a
, link
->addresses
, i
) {
3156 _cleanup_free_
char *address_str
= NULL
;
3158 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
3162 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
3167 fputs("ROUTES=", f
);
3169 SET_FOREACH(route
, link
->routes
, i
) {
3170 _cleanup_free_
char *route_str
= NULL
;
3172 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
3176 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%hhu/"USEC_FMT
, space
? " " : "", route_str
,
3177 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
3184 print_link_hashmap(f
, "CARRIER_BOUND_TO=", link
->bound_to_links
);
3185 print_link_hashmap(f
, "CARRIER_BOUND_BY=", link
->bound_by_links
);
3187 if (link
->dhcp_lease
) {
3188 struct in_addr address
;
3189 const char *tz
= NULL
;
3191 assert(link
->network
);
3193 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
3195 fprintf(f
, "TIMEZONE=%s\n", tz
);
3197 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
3199 fputs("DHCP4_ADDRESS=", f
);
3200 serialize_in_addrs(f
, &address
, 1);
3204 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
3212 unlink(link
->lease_file
);
3215 struct in_addr address
;
3217 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
3219 fputs("IPV4LL_ADDRESS=", f
);
3220 serialize_in_addrs(f
, &address
, 1);
3225 r
= fflush_and_check(f
);
3229 if (rename(temp_path
, link
->state_file
) < 0) {
3237 (void) unlink(link
->state_file
);
3239 (void) unlink(temp_path
);
3241 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
3244 /* The serialized state in /run is no longer up-to-date. */
3245 void link_dirty(Link
*link
) {
3250 /* mark manager dirty as link is dirty */
3251 manager_dirty(link
->manager
);
3253 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
3255 /* allocation errors are ignored */
3258 r
= set_put(link
->manager
->dirty_links
, link
);
3260 /* don't take another ref if the link was already dirty */
3266 /* The serialized state in /run is up-to-date */
3267 void link_clean(Link
*link
) {
3269 assert(link
->manager
);
3271 set_remove(link
->manager
->dirty_links
, link
);
3275 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
3276 [LINK_STATE_PENDING
] = "pending",
3277 [LINK_STATE_ENSLAVING
] = "configuring",
3278 [LINK_STATE_SETTING_ADDRESSES
] = "configuring",
3279 [LINK_STATE_SETTING_ROUTES
] = "configuring",
3280 [LINK_STATE_CONFIGURED
] = "configured",
3281 [LINK_STATE_UNMANAGED
] = "unmanaged",
3282 [LINK_STATE_FAILED
] = "failed",
3283 [LINK_STATE_LINGER
] = "linger",
3286 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);
3288 static const char* const link_operstate_table
[_LINK_OPERSTATE_MAX
] = {
3289 [LINK_OPERSTATE_OFF
] = "off",
3290 [LINK_OPERSTATE_NO_CARRIER
] = "no-carrier",
3291 [LINK_OPERSTATE_DORMANT
] = "dormant",
3292 [LINK_OPERSTATE_CARRIER
] = "carrier",
3293 [LINK_OPERSTATE_DEGRADED
] = "degraded",
3294 [LINK_OPERSTATE_ROUTABLE
] = "routable",
3297 DEFINE_STRING_TABLE_LOOKUP(link_operstate
, LinkOperationalState
);