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
);
261 log_link_info(link
, "IPv6 disabled for interface: %m");
263 log_link_info(link
, "IPv6 enabled for interface: %m");
269 void link_update_operstate(Link
*link
) {
270 LinkOperationalState operstate
;
273 if (link
->kernel_operstate
== IF_OPER_DORMANT
)
274 operstate
= LINK_OPERSTATE_DORMANT
;
275 else if (link_has_carrier(link
)) {
277 uint8_t scope
= RT_SCOPE_NOWHERE
;
280 /* if we have carrier, check what addresses we have */
281 SET_FOREACH(address
, link
->addresses
, i
) {
282 if (!address_is_ready(address
))
285 if (address
->scope
< scope
)
286 scope
= address
->scope
;
289 /* for operstate we also take foreign addresses into account */
290 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
291 if (!address_is_ready(address
))
294 if (address
->scope
< scope
)
295 scope
= address
->scope
;
298 if (scope
< RT_SCOPE_SITE
)
299 /* universally accessible addresses found */
300 operstate
= LINK_OPERSTATE_ROUTABLE
;
301 else if (scope
< RT_SCOPE_HOST
)
302 /* only link or site local addresses found */
303 operstate
= LINK_OPERSTATE_DEGRADED
;
305 /* no useful addresses found */
306 operstate
= LINK_OPERSTATE_CARRIER
;
307 } else if (link
->flags
& IFF_UP
)
308 operstate
= LINK_OPERSTATE_NO_CARRIER
;
310 operstate
= LINK_OPERSTATE_OFF
;
312 if (link
->operstate
!= operstate
) {
313 link
->operstate
= operstate
;
314 link_send_changed(link
, "OperationalState", NULL
);
319 #define FLAG_STRING(string, flag, old, new) \
320 (((old ^ new) & flag) \
321 ? ((old & flag) ? (" -" string) : (" +" string)) \
324 static int link_update_flags(Link
*link
, sd_netlink_message
*m
) {
325 unsigned flags
, unknown_flags_added
, unknown_flags_removed
, unknown_flags
;
331 r
= sd_rtnl_message_link_get_flags(m
, &flags
);
333 return log_link_warning_errno(link
, r
, "Could not get link flags: %m");
335 r
= sd_netlink_message_read_u8(m
, IFLA_OPERSTATE
, &operstate
);
337 /* if we got a message without operstate, take it to mean
338 the state was unchanged */
339 operstate
= link
->kernel_operstate
;
341 if ((link
->flags
== flags
) && (link
->kernel_operstate
== operstate
))
344 if (link
->flags
!= flags
) {
345 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",
346 FLAG_STRING("LOOPBACK", IFF_LOOPBACK
, link
->flags
, flags
),
347 FLAG_STRING("MASTER", IFF_MASTER
, link
->flags
, flags
),
348 FLAG_STRING("SLAVE", IFF_SLAVE
, link
->flags
, flags
),
349 FLAG_STRING("UP", IFF_UP
, link
->flags
, flags
),
350 FLAG_STRING("DORMANT", IFF_DORMANT
, link
->flags
, flags
),
351 FLAG_STRING("LOWER_UP", IFF_LOWER_UP
, link
->flags
, flags
),
352 FLAG_STRING("RUNNING", IFF_RUNNING
, link
->flags
, flags
),
353 FLAG_STRING("MULTICAST", IFF_MULTICAST
, link
->flags
, flags
),
354 FLAG_STRING("BROADCAST", IFF_BROADCAST
, link
->flags
, flags
),
355 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT
, link
->flags
, flags
),
356 FLAG_STRING("PROMISC", IFF_PROMISC
, link
->flags
, flags
),
357 FLAG_STRING("ALLMULTI", IFF_ALLMULTI
, link
->flags
, flags
),
358 FLAG_STRING("PORTSEL", IFF_PORTSEL
, link
->flags
, flags
),
359 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA
, link
->flags
, flags
),
360 FLAG_STRING("DYNAMIC", IFF_DYNAMIC
, link
->flags
, flags
),
361 FLAG_STRING("NOARP", IFF_NOARP
, link
->flags
, flags
),
362 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS
, link
->flags
, flags
),
363 FLAG_STRING("DEBUG", IFF_DEBUG
, link
->flags
, flags
),
364 FLAG_STRING("ECHO", IFF_ECHO
, link
->flags
, flags
));
366 unknown_flags
= ~(IFF_LOOPBACK
| IFF_MASTER
| IFF_SLAVE
| IFF_UP
|
367 IFF_DORMANT
| IFF_LOWER_UP
| IFF_RUNNING
|
368 IFF_MULTICAST
| IFF_BROADCAST
| IFF_POINTOPOINT
|
369 IFF_PROMISC
| IFF_ALLMULTI
| IFF_PORTSEL
|
370 IFF_AUTOMEDIA
| IFF_DYNAMIC
| IFF_NOARP
|
371 IFF_NOTRAILERS
| IFF_DEBUG
| IFF_ECHO
);
372 unknown_flags_added
= ((link
->flags
^ flags
) & flags
& unknown_flags
);
373 unknown_flags_removed
= ((link
->flags
^ flags
) & link
->flags
& unknown_flags
);
375 /* link flags are currently at most 18 bits, let's align to
377 if (unknown_flags_added
)
379 "Unknown link flags gained: %#.5x (ignoring)",
380 unknown_flags_added
);
382 if (unknown_flags_removed
)
384 "Unknown link flags lost: %#.5x (ignoring)",
385 unknown_flags_removed
);
389 link
->kernel_operstate
= operstate
;
391 link_update_operstate(link
);
396 static int link_new(Manager
*manager
, sd_netlink_message
*message
, Link
**ret
) {
397 _cleanup_link_unref_ Link
*link
= NULL
;
399 const char *ifname
, *kind
= NULL
;
401 unsigned short iftype
;
407 /* check for link kind */
408 r
= sd_netlink_message_enter_container(message
, IFLA_LINKINFO
);
410 (void)sd_netlink_message_read_string(message
, IFLA_INFO_KIND
, &kind
);
411 r
= sd_netlink_message_exit_container(message
);
416 r
= sd_netlink_message_get_type(message
, &type
);
419 else if (type
!= RTM_NEWLINK
)
422 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
425 else if (ifindex
<= 0)
428 r
= sd_rtnl_message_link_get_type(message
, &iftype
);
432 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &ifname
);
436 link
= new0(Link
, 1);
441 link
->manager
= manager
;
442 link
->state
= LINK_STATE_PENDING
;
443 link
->rtnl_extended_attrs
= true;
444 link
->ifindex
= ifindex
;
445 link
->iftype
= iftype
;
446 link
->ifname
= strdup(ifname
);
451 link
->kind
= strdup(kind
);
456 r
= sd_netlink_message_read_ether_addr(message
, IFLA_ADDRESS
, &link
->mac
);
458 log_link_debug_errno(link
, r
, "MAC address not found for new device, continuing without");
460 if (asprintf(&link
->state_file
, "/run/systemd/netif/links/%d", link
->ifindex
) < 0)
463 if (asprintf(&link
->lease_file
, "/run/systemd/netif/leases/%d", link
->ifindex
) < 0)
466 if (asprintf(&link
->lldp_file
, "/run/systemd/netif/lldp/%d", link
->ifindex
) < 0)
469 r
= hashmap_ensure_allocated(&manager
->links
, NULL
);
473 r
= hashmap_put(manager
->links
, INT_TO_PTR(link
->ifindex
), link
);
477 r
= link_update_flags(link
, message
);
487 static void link_free(Link
*link
) {
495 while (!set_isempty(link
->addresses
))
496 address_free(set_first(link
->addresses
));
498 while (!set_isempty(link
->addresses_foreign
))
499 address_free(set_first(link
->addresses_foreign
));
501 link
->addresses
= set_free(link
->addresses
);
503 link
->addresses_foreign
= set_free(link
->addresses_foreign
);
505 while ((address
= link
->pool_addresses
)) {
506 LIST_REMOVE(addresses
, link
->pool_addresses
, address
);
507 address_free(address
);
510 sd_dhcp_server_unref(link
->dhcp_server
);
511 sd_dhcp_client_unref(link
->dhcp_client
);
512 sd_dhcp_lease_unref(link
->dhcp_lease
);
514 link_lldp_emit_stop(link
);
516 free(link
->lease_file
);
518 sd_lldp_unref(link
->lldp
);
519 free(link
->lldp_file
);
521 sd_ipv4ll_unref(link
->ipv4ll
);
522 sd_dhcp6_client_unref(link
->dhcp6_client
);
523 sd_ndisc_unref(link
->ndisc
);
525 set_free_free(link
->ndisc_rdnss
);
526 set_free_free(link
->ndisc_dnssl
);
529 hashmap_remove(link
->manager
->links
, INT_TO_PTR(link
->ifindex
));
536 (void)unlink(link
->state_file
);
537 free(link
->state_file
);
539 udev_device_unref(link
->udev_device
);
541 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
)
542 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(carrier
->ifindex
));
543 hashmap_free(link
->bound_to_links
);
545 HASHMAP_FOREACH (carrier
, link
->bound_by_links
, i
)
546 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(carrier
->ifindex
));
547 hashmap_free(link
->bound_by_links
);
552 Link
*link_unref(Link
*link
) {
556 assert(link
->n_ref
> 0);
568 Link
*link_ref(Link
*link
) {
572 assert(link
->n_ref
> 0);
579 int link_get(Manager
*m
, int ifindex
, Link
**ret
) {
586 link
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
595 static void link_set_state(Link
*link
, LinkState state
) {
598 if (link
->state
== state
)
603 link_send_changed(link
, "AdministrativeState", NULL
);
606 static void link_enter_unmanaged(Link
*link
) {
609 log_link_debug(link
, "Unmanaged");
611 link_set_state(link
, LINK_STATE_UNMANAGED
);
616 static int link_stop_clients(Link
*link
) {
620 assert(link
->manager
);
621 assert(link
->manager
->event
);
623 if (link
->dhcp_client
) {
624 k
= sd_dhcp_client_stop(link
->dhcp_client
);
626 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv4 client: %m");
630 k
= sd_ipv4ll_stop(link
->ipv4ll
);
632 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 link-local: %m");
635 if (link
->dhcp6_client
) {
636 k
= sd_dhcp6_client_stop(link
->dhcp6_client
);
638 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv6 client: %m");
642 k
= sd_ndisc_stop(link
->ndisc
);
644 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Discovery: %m");
647 link_lldp_emit_stop(link
);
651 void link_enter_failed(Link
*link
) {
654 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
657 log_link_warning(link
, "Failed");
659 link_set_state(link
, LINK_STATE_FAILED
);
661 link_stop_clients(link
);
666 static Address
* link_find_dhcp_server_address(Link
*link
) {
670 assert(link
->network
);
672 /* The first statically configured address if there is any */
673 LIST_FOREACH(addresses
, address
, link
->network
->static_addresses
) {
675 if (address
->family
!= AF_INET
)
678 if (in_addr_is_null(address
->family
, &address
->in_addr
))
684 /* If that didn't work, find a suitable address we got from the pool */
685 LIST_FOREACH(addresses
, address
, link
->pool_addresses
) {
686 if (address
->family
!= AF_INET
)
695 static int link_enter_configured(Link
*link
) {
697 assert(link
->network
);
698 assert(link
->state
== LINK_STATE_SETTING_ROUTES
);
700 log_link_info(link
, "Configured");
702 link_set_state(link
, LINK_STATE_CONFIGURED
);
709 void link_check_ready(Link
*link
) {
715 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
721 if (!link
->static_configured
)
724 if (link_ipv4ll_enabled(link
))
725 if (!link
->ipv4ll_address
||
729 if (link_ipv6ll_enabled(link
))
730 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) > 0)
733 if ((link_dhcp4_enabled(link
) && !link_dhcp6_enabled(link
) &&
734 !link
->dhcp4_configured
) ||
735 (link_dhcp6_enabled(link
) && !link_dhcp4_enabled(link
) &&
736 !link
->dhcp6_configured
) ||
737 (link_dhcp4_enabled(link
) && link_dhcp6_enabled(link
) &&
738 !link
->dhcp4_configured
&& !link
->dhcp6_configured
))
741 if (link_ipv6_accept_ra_enabled(link
) && !link
->ndisc_configured
)
744 SET_FOREACH(a
, link
->addresses
, i
)
745 if (!address_is_ready(a
))
748 if (link
->state
!= LINK_STATE_CONFIGURED
)
749 link_enter_configured(link
);
754 static int route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
755 _cleanup_link_unref_ Link
*link
= userdata
;
758 assert(link
->link_messages
> 0);
759 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
760 LINK_STATE_SETTING_ROUTES
, LINK_STATE_FAILED
,
763 link
->link_messages
--;
765 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
768 r
= sd_netlink_message_get_errno(m
);
769 if (r
< 0 && r
!= -EEXIST
)
770 log_link_warning_errno(link
, r
, "Could not set route: %m");
772 if (link
->link_messages
== 0) {
773 log_link_debug(link
, "Routes set");
774 link
->static_configured
= true;
775 link_check_ready(link
);
781 static int link_enter_set_routes(Link
*link
) {
786 assert(link
->network
);
787 assert(link
->state
== LINK_STATE_SETTING_ADDRESSES
);
789 link_set_state(link
, LINK_STATE_SETTING_ROUTES
);
791 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
792 r
= route_configure(rt
, link
, route_handler
);
794 log_link_warning_errno(link
, r
, "Could not set routes: %m");
795 link_enter_failed(link
);
799 link
->link_messages
++;
802 if (link
->link_messages
== 0) {
803 link
->static_configured
= true;
804 link_check_ready(link
);
806 log_link_debug(link
, "Setting routes");
811 int link_route_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
812 _cleanup_link_unref_ Link
*link
= userdata
;
817 assert(link
->ifname
);
819 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
822 r
= sd_netlink_message_get_errno(m
);
823 if (r
< 0 && r
!= -ESRCH
)
824 log_link_warning_errno(link
, r
, "Could not drop route: %m");
829 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
830 _cleanup_link_unref_ Link
*link
= userdata
;
836 assert(link
->ifname
);
837 assert(link
->link_messages
> 0);
838 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
839 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
841 link
->link_messages
--;
843 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
846 r
= sd_netlink_message_get_errno(m
);
847 if (r
< 0 && r
!= -EEXIST
)
848 log_link_warning_errno(link
, r
, "could not set address: %m");
850 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
852 if (link
->link_messages
== 0) {
853 log_link_debug(link
, "Addresses set");
854 link_enter_set_routes(link
);
860 static int link_push_dns_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
861 _cleanup_free_
struct in_addr
*addresses
= NULL
;
862 size_t n_addresses
= 0, n_allocated
= 0;
865 log_debug("Copying DNS server information from %s", link
->ifname
);
870 STRV_FOREACH(a
, link
->network
->dns
) {
873 /* Only look for IPv4 addresses */
874 if (inet_pton(AF_INET
, *a
, &ia
) <= 0)
877 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
880 addresses
[n_addresses
++] = ia
;
883 if (link
->network
->dhcp_use_dns
&&
885 const struct in_addr
*da
= NULL
;
888 n
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &da
);
891 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
894 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
899 if (n_addresses
<= 0)
902 return sd_dhcp_server_set_dns(s
, addresses
, n_addresses
);
905 static int link_push_ntp_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
906 _cleanup_free_
struct in_addr
*addresses
= NULL
;
907 size_t n_addresses
= 0, n_allocated
= 0;
913 log_debug("Copying NTP server information from %s", link
->ifname
);
915 STRV_FOREACH(a
, link
->network
->ntp
) {
918 /* Only look for IPv4 addresses */
919 if (inet_pton(AF_INET
, *a
, &ia
) <= 0)
922 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
925 addresses
[n_addresses
++] = ia
;
928 if (link
->network
->dhcp_use_ntp
&&
930 const struct in_addr
*da
= NULL
;
933 n
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &da
);
936 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
939 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
944 if (n_addresses
<= 0)
947 return sd_dhcp_server_set_ntp(s
, addresses
, n_addresses
);
950 static int link_enter_set_addresses(Link
*link
) {
955 assert(link
->network
);
956 assert(link
->state
!= _LINK_STATE_INVALID
);
958 link_set_state(link
, LINK_STATE_SETTING_ADDRESSES
);
960 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
961 r
= address_configure(ad
, link
, address_handler
, false);
963 log_link_warning_errno(link
, r
, "Could not set addresses: %m");
964 link_enter_failed(link
);
968 link
->link_messages
++;
971 /* now that we can figure out a default address for the dhcp server,
973 if (link_dhcp4_server_enabled(link
)) {
976 bool acquired_uplink
= false;
978 address
= link_find_dhcp_server_address(link
);
980 log_link_warning(link
, "Failed to find suitable address for DHCPv4 server instance.");
981 link_enter_failed(link
);
985 /* use the server address' subnet as the pool */
986 r
= sd_dhcp_server_configure_pool(link
->dhcp_server
, &address
->in_addr
.in
, address
->prefixlen
,
987 link
->network
->dhcp_server_pool_offset
, link
->network
->dhcp_server_pool_size
);
992 r = sd_dhcp_server_set_router(link->dhcp_server,
993 &main_address->in_addr.in);
998 if (link
->network
->dhcp_server_max_lease_time_usec
> 0) {
999 r
= sd_dhcp_server_set_max_lease_time(
1001 DIV_ROUND_UP(link
->network
->dhcp_server_max_lease_time_usec
, USEC_PER_SEC
));
1006 if (link
->network
->dhcp_server_default_lease_time_usec
> 0) {
1007 r
= sd_dhcp_server_set_default_lease_time(
1009 DIV_ROUND_UP(link
->network
->dhcp_server_default_lease_time_usec
, USEC_PER_SEC
));
1014 if (link
->network
->dhcp_server_emit_dns
) {
1016 if (link
->network
->n_dhcp_server_dns
> 0)
1017 r
= sd_dhcp_server_set_dns(link
->dhcp_server
, link
->network
->dhcp_server_dns
, link
->network
->n_dhcp_server_dns
);
1019 uplink
= manager_find_uplink(link
->manager
, link
);
1020 acquired_uplink
= true;
1023 log_link_debug(link
, "Not emitting DNS server information on link, couldn't find suitable uplink.");
1026 r
= link_push_dns_to_dhcp_server(uplink
, link
->dhcp_server
);
1029 log_link_warning_errno(link
, r
, "Failed to set DNS server for DHCP server, ignoring: %m");
1033 if (link
->network
->dhcp_server_emit_ntp
) {
1035 if (link
->network
->n_dhcp_server_ntp
> 0)
1036 r
= sd_dhcp_server_set_ntp(link
->dhcp_server
, link
->network
->dhcp_server_ntp
, link
->network
->n_dhcp_server_ntp
);
1038 if (!acquired_uplink
)
1039 uplink
= manager_find_uplink(link
->manager
, link
);
1042 log_link_debug(link
, "Not emitting NTP server information on link, couldn't find suitable uplink.");
1045 r
= link_push_ntp_to_dhcp_server(uplink
, link
->dhcp_server
);
1049 log_link_warning_errno(link
, r
, "Failed to set NTP server for DHCP server, ignoring: %m");
1052 r
= sd_dhcp_server_set_emit_router(link
->dhcp_server
, link
->network
->dhcp_server_emit_router
);
1054 log_link_warning_errno(link
, r
, "Failed to set router emission for DHCP server: %m");
1058 if (link
->network
->dhcp_server_emit_timezone
) {
1059 _cleanup_free_
char *buffer
= NULL
;
1060 const char *tz
= NULL
;
1062 if (link
->network
->dhcp_server_timezone
)
1063 tz
= link
->network
->dhcp_server_timezone
;
1065 r
= get_timezone(&buffer
);
1067 log_warning_errno(r
, "Failed to determine timezone: %m");
1073 r
= sd_dhcp_server_set_timezone(link
->dhcp_server
, tz
);
1079 r
= sd_dhcp_server_start(link
->dhcp_server
);
1081 log_link_warning_errno(link
, r
, "Could not start DHCPv4 server instance: %m");
1083 link_enter_failed(link
);
1088 log_link_debug(link
, "Offering DHCPv4 leases");
1091 if (link
->link_messages
== 0)
1092 link_enter_set_routes(link
);
1094 log_link_debug(link
, "Setting addresses");
1099 int link_address_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1100 _cleanup_link_unref_ Link
*link
= userdata
;
1105 assert(link
->ifname
);
1107 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1110 r
= sd_netlink_message_get_errno(m
);
1111 if (r
< 0 && r
!= -EADDRNOTAVAIL
)
1112 log_link_warning_errno(link
, r
, "Could not drop address: %m");
1117 static int link_set_bridge_fdb(Link
*link
) {
1118 FdbEntry
*fdb_entry
;
1121 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
1122 r
= fdb_entry_configure(link
, fdb_entry
);
1124 log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
1132 static int link_set_proxy_arp(Link
*link
) {
1133 const char *p
= NULL
;
1136 if (!link_proxy_arp_enabled(link
))
1139 p
= strjoina("/proc/sys/net/ipv4/conf/", link
->ifname
, "/proxy_arp");
1141 r
= write_string_file(p
, one_zero(link
->network
->proxy_arp
), WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
1143 log_link_warning_errno(link
, r
, "Cannot configure proxy ARP for interface: %m");
1148 static int link_set_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1149 _cleanup_link_unref_ Link
*link
= userdata
;
1152 log_link_debug(link
, "Set link");
1154 r
= sd_netlink_message_get_errno(m
);
1155 if (r
< 0 && r
!= -EEXIST
) {
1156 log_link_error_errno(link
, r
, "Could not join netdev: %m");
1157 link_enter_failed(link
);
1164 static int set_hostname_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
1165 _cleanup_link_unref_ Link
*link
= userdata
;
1166 const sd_bus_error
*e
;
1171 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1174 e
= sd_bus_message_get_error(m
);
1176 log_link_warning_errno(link
, sd_bus_error_get_errno(e
), "Could not set hostname: %s", e
->message
);
1181 int link_set_hostname(Link
*link
, const char *hostname
) {
1185 assert(link
->manager
);
1187 log_link_debug(link
, "Setting transient hostname: '%s'", strna(hostname
));
1189 if (!link
->manager
->bus
) {
1190 /* TODO: replace by assert when we can rely on kdbus */
1191 log_link_info(link
, "Not connected to system bus, ignoring transient hostname.");
1195 r
= sd_bus_call_method_async(
1198 "org.freedesktop.hostname1",
1199 "/org/freedesktop/hostname1",
1200 "org.freedesktop.hostname1",
1202 set_hostname_handler
,
1209 return log_link_error_errno(link
, r
, "Could not set transient hostname: %m");
1216 static int set_timezone_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
1217 _cleanup_link_unref_ Link
*link
= userdata
;
1218 const sd_bus_error
*e
;
1223 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1226 e
= sd_bus_message_get_error(m
);
1228 log_link_warning_errno(link
, sd_bus_error_get_errno(e
), "Could not set timezone: %s", e
->message
);
1233 int link_set_timezone(Link
*link
, const char *tz
) {
1237 assert(link
->manager
);
1240 log_link_debug(link
, "Setting system timezone: '%s'", tz
);
1242 if (!link
->manager
->bus
) {
1243 log_link_info(link
, "Not connected to system bus, ignoring timezone.");
1247 r
= sd_bus_call_method_async(
1250 "org.freedesktop.timedate1",
1251 "/org/freedesktop/timedate1",
1252 "org.freedesktop.timedate1",
1254 set_timezone_handler
,
1260 return log_link_error_errno(link
, r
, "Could not set timezone: %m");
1267 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1268 _cleanup_link_unref_ Link
*link
= userdata
;
1273 assert(link
->ifname
);
1275 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1278 r
= sd_netlink_message_get_errno(m
);
1280 log_link_warning_errno(link
, r
, "Could not set MTU: %m");
1285 int link_set_mtu(Link
*link
, uint32_t mtu
) {
1286 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1290 assert(link
->manager
);
1291 assert(link
->manager
->rtnl
);
1293 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1295 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1297 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1299 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1301 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1303 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, set_mtu_handler
, link
, 0, NULL
);
1305 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1312 static int link_set_bridge(Link
*link
) {
1313 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1317 assert(link
->network
);
1319 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1321 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1323 r
= sd_rtnl_message_link_set_family(req
, PF_BRIDGE
);
1325 return log_link_error_errno(link
, r
, "Could not set message family: %m");
1327 r
= sd_netlink_message_open_container(req
, IFLA_PROTINFO
);
1329 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1331 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_GUARD
, !link
->network
->use_bpdu
);
1333 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_GUARD attribute: %m");
1335 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MODE
, link
->network
->hairpin
);
1337 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MODE attribute: %m");
1339 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_FAST_LEAVE
, link
->network
->fast_leave
);
1341 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_FAST_LEAVE attribute: %m");
1343 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_PROTECT
, !link
->network
->allow_port_to_be_root
);
1345 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PROTECT attribute: %m");
1347 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_UNICAST_FLOOD
, link
->network
->unicast_flood
);
1349 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_UNICAST_FLOOD attribute: %m");
1351 if (link
->network
->cost
!= 0) {
1352 r
= sd_netlink_message_append_u32(req
, IFLA_BRPORT_COST
, link
->network
->cost
);
1354 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_COST attribute: %m");
1357 r
= sd_netlink_message_close_container(req
);
1359 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1361 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_set_handler
, link
, 0, NULL
);
1363 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1370 static int link_lldp_save(Link
*link
) {
1371 _cleanup_free_
char *temp_path
= NULL
;
1372 _cleanup_fclose_
FILE *f
= NULL
;
1373 sd_lldp_neighbor
**l
= NULL
;
1377 assert(link
->lldp_file
);
1380 (void) unlink(link
->lldp_file
);
1384 r
= sd_lldp_get_neighbors(link
->lldp
, &l
);
1388 (void) unlink(link
->lldp_file
);
1394 r
= fopen_temporary(link
->lldp_file
, &f
, &temp_path
);
1398 fchmod(fileno(f
), 0644);
1400 for (i
= 0; i
< n
; i
++) {
1405 r
= sd_lldp_neighbor_get_raw(l
[i
], &p
, &sz
);
1410 (void) fwrite(&u
, 1, sizeof(u
), f
);
1411 (void) fwrite(p
, 1, sz
, f
);
1414 r
= fflush_and_check(f
);
1418 if (rename(temp_path
, link
->lldp_file
) < 0) {
1425 (void) unlink(link
->lldp_file
);
1427 (void) unlink(temp_path
);
1429 log_link_error_errno(link
, r
, "Failed to save LLDP data to %s: %m", link
->lldp_file
);
1433 for (i
= 0; i
< n
; i
++)
1434 sd_lldp_neighbor_unref(l
[i
]);
1441 static void lldp_handler(sd_lldp
*lldp
, sd_lldp_event event
, sd_lldp_neighbor
*n
, void *userdata
) {
1442 Link
*link
= userdata
;
1447 (void) link_lldp_save(link
);
1449 if (link_lldp_emit_enabled(link
) && event
== SD_LLDP_EVENT_ADDED
) {
1450 /* If we received information about a new neighbor, restart the LLDP "fast" logic */
1452 log_link_debug(link
, "Received LLDP datagram from previously unknown neighbor, restarting 'fast' LLDP transmission.");
1454 r
= link_lldp_emit_start(link
);
1456 log_link_warning_errno(link
, r
, "Failed to restart LLDP transmission: %m");
1460 static int link_acquire_ipv6_conf(Link
*link
) {
1465 if (link_dhcp6_enabled(link
)) {
1466 assert(link
->dhcp6_client
);
1467 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1469 /* start DHCPv6 client in stateless mode */
1470 r
= dhcp6_request_address(link
, true);
1471 if (r
< 0 && r
!= -EBUSY
)
1472 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv6 lease: %m");
1474 log_link_debug(link
, "Acquiring DHCPv6 lease");
1477 if (link_ipv6_accept_ra_enabled(link
)) {
1478 assert(link
->ndisc
);
1480 log_link_debug(link
, "Discovering IPv6 routers");
1482 r
= sd_ndisc_start(link
->ndisc
);
1483 if (r
< 0 && r
!= -EBUSY
)
1484 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1490 static int link_acquire_ipv4_conf(Link
*link
) {
1494 assert(link
->network
);
1495 assert(link
->manager
);
1496 assert(link
->manager
->event
);
1498 if (link_ipv4ll_enabled(link
)) {
1499 assert(link
->ipv4ll
);
1501 log_link_debug(link
, "Acquiring IPv4 link-local address");
1503 r
= sd_ipv4ll_start(link
->ipv4ll
);
1505 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1508 if (link_dhcp4_enabled(link
)) {
1509 assert(link
->dhcp_client
);
1511 log_link_debug(link
, "Acquiring DHCPv4 lease");
1513 r
= sd_dhcp_client_start(link
->dhcp_client
);
1515 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1521 static int link_acquire_conf(Link
*link
) {
1526 r
= link_acquire_ipv4_conf(link
);
1530 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) == 0) {
1531 r
= link_acquire_ipv6_conf(link
);
1536 if (link_lldp_emit_enabled(link
)) {
1537 r
= link_lldp_emit_start(link
);
1539 return log_link_warning_errno(link
, r
, "Failed to start LLDP transmission: %m");
1545 bool link_has_carrier(Link
*link
) {
1546 /* see Documentation/networking/operstates.txt in the kernel sources */
1548 if (link
->kernel_operstate
== IF_OPER_UP
)
1551 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1552 /* operstate may not be implemented, so fall back to flags */
1553 if ((link
->flags
& IFF_LOWER_UP
) && !(link
->flags
& IFF_DORMANT
))
1559 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1560 _cleanup_link_unref_ Link
*link
= userdata
;
1565 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1568 r
= sd_netlink_message_get_errno(m
);
1570 /* we warn but don't fail the link, as it may be
1572 log_link_warning_errno(link
, r
, "Could not bring up interface: %m");
1577 static int link_up(Link
*link
) {
1578 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1579 uint8_t ipv6ll_mode
;
1583 assert(link
->network
);
1584 assert(link
->manager
);
1585 assert(link
->manager
->rtnl
);
1587 log_link_debug(link
, "Bringing link up");
1589 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1591 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1593 /* set it free if not enslaved with networkd */
1594 if (!link
->network
->bridge
&& !link
->network
->bond
) {
1595 r
= sd_netlink_message_append_u32(req
, IFLA_MASTER
, 0);
1597 return log_link_error_errno(link
, r
, "Could not append IFLA_MASTER attribute: %m");
1600 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1602 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1604 if (link
->network
->mac
) {
1605 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
1607 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
1610 /* If IPv6 not configured (no static IPv6 address and IPv6LL autoconfiguration is disabled)
1611 for this interface, or if it is a bridge slave, then disable IPv6 else enable it. */
1612 (void) link_enable_ipv6(link
);
1614 if (link
->network
->mtu
) {
1615 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
1616 on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */
1617 if (link_ipv6_enabled(link
) && link
->network
->mtu
< IPV6_MIN_MTU
) {
1619 log_link_warning(link
, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU
) ", as "
1620 "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU
) " bytes: %m");
1622 link
->network
->mtu
= IPV6_MIN_MTU
;
1625 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, link
->network
->mtu
);
1627 return log_link_error_errno(link
, r
, "Could not set MTU: %m");
1630 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1632 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1634 if (link_ipv6_enabled(link
)) {
1635 /* if the kernel lacks ipv6 support setting IFF_UP fails if any ipv6 options are passed */
1636 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1638 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1640 if (!link_ipv6ll_enabled(link
))
1641 ipv6ll_mode
= IN6_ADDR_GEN_MODE_NONE
;
1643 const char *p
= NULL
;
1644 _cleanup_free_
char *stable_secret
= NULL
;
1646 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/stable_secret");
1647 r
= read_one_line_file(p
, &stable_secret
);
1650 ipv6ll_mode
= IN6_ADDR_GEN_MODE_EUI64
;
1652 ipv6ll_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
1654 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1656 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1658 if (!in_addr_is_null(AF_INET6
, &link
->network
->ipv6_token
)) {
1659 r
= sd_netlink_message_append_in6_addr(req
, IFLA_INET6_TOKEN
, &link
->network
->ipv6_token
.in6
);
1661 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_TOKEN: %m");
1664 r
= sd_netlink_message_close_container(req
);
1666 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1669 r
= sd_netlink_message_close_container(req
);
1671 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1673 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_up_handler
, link
, 0, NULL
);
1675 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1682 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1683 _cleanup_link_unref_ Link
*link
= userdata
;
1688 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1691 r
= sd_netlink_message_get_errno(m
);
1693 log_link_warning_errno(link
, r
, "Could not bring down interface: %m");
1698 static int link_down(Link
*link
) {
1699 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1703 assert(link
->manager
);
1704 assert(link
->manager
->rtnl
);
1706 log_link_debug(link
, "Bringing link down");
1708 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
1709 RTM_SETLINK
, link
->ifindex
);
1711 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1713 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
1715 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1717 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_down_handler
, link
, 0, NULL
);
1719 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1726 static int link_handle_bound_to_list(Link
*link
) {
1730 bool required_up
= false;
1731 bool link_is_up
= false;
1735 if (hashmap_isempty(link
->bound_to_links
))
1738 if (link
->flags
& IFF_UP
)
1741 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
1742 if (link_has_carrier(l
)) {
1747 if (!required_up
&& link_is_up
) {
1748 r
= link_down(link
);
1751 } else if (required_up
&& !link_is_up
) {
1760 static int link_handle_bound_by_list(Link
*link
) {
1767 if (hashmap_isempty(link
->bound_by_links
))
1770 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
1771 r
= link_handle_bound_to_list(l
);
1779 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
1785 if (link
== carrier
)
1788 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
1791 r
= hashmap_ensure_allocated(h
, NULL
);
1795 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
1802 static int link_new_bound_by_list(Link
*link
) {
1807 bool list_updated
= false;
1810 assert(link
->manager
);
1814 HASHMAP_FOREACH(carrier
, m
->links
, i
) {
1815 if (!carrier
->network
)
1818 if (strv_isempty(carrier
->network
->bind_carrier
))
1821 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
, 0)) {
1822 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
1826 list_updated
= true;
1833 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
1834 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
1838 link_dirty(carrier
);
1844 static int link_new_bound_to_list(Link
*link
) {
1849 bool list_updated
= false;
1852 assert(link
->manager
);
1857 if (strv_isempty(link
->network
->bind_carrier
))
1862 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
1863 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
, 0)) {
1864 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
1868 list_updated
= true;
1875 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
1876 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
1880 link_dirty(carrier
);
1886 static int link_new_carrier_maps(Link
*link
) {
1889 r
= link_new_bound_by_list(link
);
1893 r
= link_handle_bound_by_list(link
);
1897 r
= link_new_bound_to_list(link
);
1901 r
= link_handle_bound_to_list(link
);
1908 static void link_free_bound_to_list(Link
*link
) {
1912 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
1913 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
1915 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
1916 link_dirty(bound_to
);
1922 static void link_free_bound_by_list(Link
*link
) {
1926 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
1927 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
1929 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
1930 link_dirty(bound_by
);
1931 link_handle_bound_to_list(bound_by
);
1938 static void link_free_carrier_maps(Link
*link
) {
1939 bool list_updated
= false;
1943 if (!hashmap_isempty(link
->bound_to_links
)) {
1944 link_free_bound_to_list(link
);
1945 list_updated
= true;
1948 if (!hashmap_isempty(link
->bound_by_links
)) {
1949 link_free_bound_by_list(link
);
1950 list_updated
= true;
1959 void link_drop(Link
*link
) {
1960 if (!link
|| link
->state
== LINK_STATE_LINGER
)
1963 link_set_state(link
, LINK_STATE_LINGER
);
1965 link_free_carrier_maps(link
);
1967 log_link_debug(link
, "Link removed");
1969 (void)unlink(link
->state_file
);
1975 static int link_joined(Link
*link
) {
1979 assert(link
->network
);
1981 if (!hashmap_isempty(link
->bound_to_links
)) {
1982 r
= link_handle_bound_to_list(link
);
1985 } else if (!(link
->flags
& IFF_UP
)) {
1988 link_enter_failed(link
);
1993 if (link
->network
->bridge
) {
1994 r
= link_set_bridge(link
);
1996 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
1999 return link_enter_set_addresses(link
);
2002 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
2003 _cleanup_link_unref_ Link
*link
= userdata
;
2007 assert(link
->network
);
2011 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2014 r
= sd_netlink_message_get_errno(m
);
2015 if (r
< 0 && r
!= -EEXIST
) {
2016 log_link_error_errno(link
, r
, "Could not join netdev: %m");
2017 link_enter_failed(link
);
2020 log_link_debug(link
, "Joined netdev");
2022 if (link
->enslaving
<= 0)
2028 static int link_enter_join_netdev(Link
*link
) {
2034 assert(link
->network
);
2035 assert(link
->state
== LINK_STATE_PENDING
);
2037 link_set_state(link
, LINK_STATE_ENSLAVING
);
2041 if (!link
->network
->bridge
&&
2042 !link
->network
->bond
&&
2043 hashmap_isempty(link
->network
->stacked_netdevs
))
2044 return link_joined(link
);
2046 if (link
->network
->bond
) {
2047 log_struct(LOG_DEBUG
,
2048 LOG_LINK_INTERFACE(link
),
2049 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2050 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
),
2053 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
2055 log_struct_errno(LOG_WARNING
, r
,
2056 LOG_LINK_INTERFACE(link
),
2057 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2058 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
),
2061 link_enter_failed(link
);
2068 if (link
->network
->bridge
) {
2069 log_struct(LOG_DEBUG
,
2070 LOG_LINK_INTERFACE(link
),
2071 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2072 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
),
2075 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
2077 log_struct_errno(LOG_WARNING
, r
,
2078 LOG_LINK_INTERFACE(link
),
2079 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2080 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
),
2082 link_enter_failed(link
);
2089 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
2091 log_struct(LOG_DEBUG
,
2092 LOG_LINK_INTERFACE(link
),
2093 LOG_NETDEV_INTERFACE(netdev
),
2094 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
),
2097 r
= netdev_join(netdev
, link
, netdev_join_handler
);
2099 log_struct_errno(LOG_WARNING
, r
,
2100 LOG_LINK_INTERFACE(link
),
2101 LOG_NETDEV_INTERFACE(netdev
),
2102 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
),
2104 link_enter_failed(link
);
2114 static int link_set_ipv4_forward(Link
*link
) {
2117 if (!link_ipv4_forward_enabled(link
))
2120 /* We propagate the forwarding flag from one interface to the
2121 * global setting one way. This means: as long as at least one
2122 * interface was configured at any time that had IP forwarding
2123 * enabled the setting will stay on for good. We do this
2124 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
2125 * somewhat in sync (see below). */
2127 r
= write_string_file("/proc/sys/net/ipv4/ip_forward", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2129 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
2134 static int link_set_ipv6_forward(Link
*link
) {
2137 if (!link_ipv6_forward_enabled(link
))
2140 /* On Linux, the IPv6 stack does not not know a per-interface
2141 * packet forwarding setting: either packet forwarding is on
2142 * for all, or off for all. We hence don't bother with a
2143 * per-interface setting, but simply propagate the interface
2144 * flag, if it is set, to the global flag, one-way. Note that
2145 * while IPv4 would allow a per-interface flag, we expose the
2146 * same behaviour there and also propagate the setting from
2147 * one to all, to keep things simple (see above). */
2149 r
= write_string_file("/proc/sys/net/ipv6/conf/all/forwarding", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2151 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
2156 static int link_set_ipv6_privacy_extensions(Link
*link
) {
2157 char buf
[DECIMAL_STR_MAX(unsigned) + 1];
2158 IPv6PrivacyExtensions s
;
2159 const char *p
= NULL
;
2162 s
= link_ipv6_privacy_extensions(link
);
2166 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/use_tempaddr");
2167 xsprintf(buf
, "%u", (unsigned) link
->network
->ipv6_privacy_extensions
);
2169 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2171 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
2176 static int link_set_ipv6_accept_ra(Link
*link
) {
2177 const char *p
= NULL
;
2180 /* Make this a NOP if IPv6 is not available */
2181 if (!socket_ipv6_is_supported())
2184 if (link
->flags
& IFF_LOOPBACK
)
2190 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/accept_ra");
2192 /* We handle router advertisements ourselves, tell the kernel to GTFO */
2193 r
= write_string_file(p
, "0", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2195 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
2200 static int link_set_ipv6_dad_transmits(Link
*link
) {
2201 char buf
[DECIMAL_STR_MAX(int) + 1];
2202 const char *p
= NULL
;
2205 /* Make this a NOP if IPv6 is not available */
2206 if (!socket_ipv6_is_supported())
2209 if (link
->flags
& IFF_LOOPBACK
)
2215 if (link
->network
->ipv6_dad_transmits
< 0)
2218 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/dad_transmits");
2219 xsprintf(buf
, "%i", link
->network
->ipv6_dad_transmits
);
2221 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2223 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
2228 static int link_set_ipv6_hop_limit(Link
*link
) {
2229 char buf
[DECIMAL_STR_MAX(int) + 1];
2230 const char *p
= NULL
;
2233 /* Make this a NOP if IPv6 is not available */
2234 if (!socket_ipv6_is_supported())
2237 if (link
->flags
& IFF_LOOPBACK
)
2243 if (link
->network
->ipv6_hop_limit
< 0)
2246 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/hop_limit");
2247 xsprintf(buf
, "%i", link
->network
->ipv6_hop_limit
);
2249 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2251 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2256 static int link_drop_foreign_config(Link
*link
) {
2262 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
2263 /* we consider IPv6LL addresses to be managed by the kernel */
2264 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2267 r
= address_remove(address
, link
, link_address_remove_handler
);
2272 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2273 /* do not touch routes managed by the kernel */
2274 if (route
->protocol
== RTPROT_KERNEL
)
2277 r
= route_remove(route
, link
, link_route_remove_handler
);
2285 static int link_update_lldp(Link
*link
) {
2293 if (link
->flags
& IFF_UP
) {
2294 r
= sd_lldp_start(link
->lldp
);
2296 log_link_debug(link
, "Started LLDP.");
2298 r
= sd_lldp_stop(link
->lldp
);
2300 log_link_debug(link
, "Stopped LLDP.");
2306 static int link_configure(Link
*link
) {
2310 assert(link
->network
);
2311 assert(link
->state
== LINK_STATE_PENDING
);
2313 /* Drop foreign config, but ignore loopback or critical devices.
2314 * We do not want to remove loopback address or addresses used for root NFS. */
2315 if (!(link
->flags
& IFF_LOOPBACK
) && !(link
->network
->dhcp_critical
)) {
2316 r
= link_drop_foreign_config(link
);
2321 r
= link_set_bridge_fdb(link
);
2325 r
= link_set_proxy_arp(link
);
2329 r
= link_set_ipv4_forward(link
);
2333 r
= link_set_ipv6_forward(link
);
2337 r
= link_set_ipv6_privacy_extensions(link
);
2341 r
= link_set_ipv6_accept_ra(link
);
2345 r
= link_set_ipv6_dad_transmits(link
);
2349 r
= link_set_ipv6_hop_limit(link
);
2353 if (link_ipv4ll_enabled(link
)) {
2354 r
= ipv4ll_configure(link
);
2359 if (link_dhcp4_enabled(link
)) {
2360 r
= dhcp4_configure(link
);
2365 if (link_dhcp4_server_enabled(link
)) {
2366 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
2370 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
2375 if (link_dhcp6_enabled(link
) ||
2376 link_ipv6_accept_ra_enabled(link
)) {
2377 r
= dhcp6_configure(link
);
2382 if (link_ipv6_accept_ra_enabled(link
)) {
2383 r
= ndisc_configure(link
);
2388 if (link_lldp_rx_enabled(link
)) {
2389 r
= sd_lldp_new(&link
->lldp
);
2393 r
= sd_lldp_set_ifindex(link
->lldp
, link
->ifindex
);
2397 r
= sd_lldp_match_capabilities(link
->lldp
,
2398 link
->network
->lldp_mode
== LLDP_MODE_ROUTERS_ONLY
?
2399 SD_LLDP_SYSTEM_CAPABILITIES_ALL_ROUTERS
:
2400 SD_LLDP_SYSTEM_CAPABILITIES_ALL
);
2404 r
= sd_lldp_set_filter_address(link
->lldp
, &link
->mac
);
2408 r
= sd_lldp_attach_event(link
->lldp
, NULL
, 0);
2412 r
= sd_lldp_set_callback(link
->lldp
, lldp_handler
, link
);
2416 r
= link_update_lldp(link
);
2421 if (link_has_carrier(link
)) {
2422 r
= link_acquire_conf(link
);
2427 return link_enter_join_netdev(link
);
2430 static int link_initialized_and_synced(sd_netlink
*rtnl
, sd_netlink_message
*m
,
2432 _cleanup_link_unref_ Link
*link
= userdata
;
2437 assert(link
->ifname
);
2438 assert(link
->manager
);
2440 if (link
->state
!= LINK_STATE_PENDING
)
2443 log_link_debug(link
, "Link state is up-to-date");
2445 r
= link_new_bound_by_list(link
);
2449 r
= link_handle_bound_by_list(link
);
2453 if (!link
->network
) {
2454 r
= network_get(link
->manager
, link
->udev_device
, link
->ifname
,
2455 &link
->mac
, &network
);
2457 link_enter_unmanaged(link
);
2462 if (link
->flags
& IFF_LOOPBACK
) {
2463 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
2464 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
2466 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
2467 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
2469 if (network
->dhcp_server
)
2470 log_link_debug(link
, "Ignoring DHCP server for loopback link");
2473 r
= network_apply(link
->manager
, network
, link
);
2478 r
= link_new_bound_to_list(link
);
2482 r
= link_configure(link
);
2489 int link_initialized(Link
*link
, struct udev_device
*device
) {
2490 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2494 assert(link
->manager
);
2495 assert(link
->manager
->rtnl
);
2498 if (link
->state
!= LINK_STATE_PENDING
)
2501 if (link
->udev_device
)
2504 log_link_debug(link
, "udev initialized link");
2506 link
->udev_device
= udev_device_ref(device
);
2508 /* udev has initialized the link, but we don't know if we have yet
2509 * processed the NEWLINK messages with the latest state. Do a GETLINK,
2510 * when it returns we know that the pending NEWLINKs have already been
2511 * processed and that we are up-to-date */
2513 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
2518 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
,
2519 link_initialized_and_synced
, link
, 0, NULL
);
2528 static int link_load(Link
*link
) {
2529 _cleanup_free_
char *network_file
= NULL
,
2532 *dhcp4_address
= NULL
,
2533 *ipv4ll_address
= NULL
;
2534 union in_addr_union address
;
2535 union in_addr_union route_dst
;
2541 r
= parse_env_file(link
->state_file
, NEWLINE
,
2542 "NETWORK_FILE", &network_file
,
2543 "ADDRESSES", &addresses
,
2545 "DHCP4_ADDRESS", &dhcp4_address
,
2546 "IPV4LL_ADDRESS", &ipv4ll_address
,
2548 if (r
< 0 && r
!= -ENOENT
)
2549 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
2556 suffix
= strrchr(network_file
, '.');
2558 log_link_debug(link
, "Failed to get network name from %s", network_file
);
2559 goto network_file_fail
;
2563 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
2565 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
2566 goto network_file_fail
;
2569 r
= network_apply(link
->manager
, network
, link
);
2571 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
2580 _cleanup_free_
char *address_str
= NULL
;
2581 char *prefixlen_str
;
2583 unsigned char prefixlen
;
2585 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
2587 log_link_debug_errno(link
, r
, "Failed to extract next address string: %m");
2593 prefixlen_str
= strchr(address_str
, '/');
2594 if (!prefixlen_str
) {
2595 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
2599 *prefixlen_str
++ = '\0';
2601 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
2603 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
2607 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
2609 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
2613 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
2615 return log_link_error_errno(link
, r
, "Failed to add address: %m");
2624 _cleanup_free_
char *route_str
= NULL
;
2625 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
2627 char *prefixlen_str
;
2629 unsigned char prefixlen
, tos
, table
;
2632 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
2634 log_link_debug_errno(link
, r
, "Failed to extract next route string: %m");
2640 prefixlen_str
= strchr(route_str
, '/');
2641 if (!prefixlen_str
) {
2642 log_link_debug(link
, "Failed to parse route %s", route_str
);
2646 *prefixlen_str
++ = '\0';
2648 r
= sscanf(prefixlen_str
, "%hhu/%hhu/%"SCNu32
"/%hhu/"USEC_FMT
, &prefixlen
, &tos
, &priority
, &table
, &lifetime
);
2650 log_link_debug(link
,
2651 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
2656 r
= in_addr_from_string_auto(route_str
, &family
, &route_dst
);
2658 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
2662 r
= route_add(link
, family
, &route_dst
, prefixlen
, tos
, priority
, table
, &route
);
2664 return log_link_error_errno(link
, r
, "Failed to add route: %m");
2666 if (lifetime
!= USEC_INFINITY
) {
2667 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(), lifetime
,
2668 0, route_expire_handler
, route
);
2670 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
2673 route
->lifetime
= lifetime
;
2674 sd_event_source_unref(route
->expire
);
2675 route
->expire
= expire
;
2680 if (dhcp4_address
) {
2681 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
2683 log_link_debug_errno(link
, r
, "Falied to parse DHCPv4 address %s: %m", dhcp4_address
);
2684 goto dhcp4_address_fail
;
2687 r
= sd_dhcp_client_new(&link
->dhcp_client
);
2689 return log_link_error_errno(link
, r
, "Falied to create DHCPv4 client: %m");
2691 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
2693 return log_link_error_errno(link
, r
, "Falied to set inital DHCPv4 address %s: %m", dhcp4_address
);
2698 if (ipv4ll_address
) {
2699 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
2701 log_link_debug_errno(link
, r
, "Falied to parse IPv4LL address %s: %m", ipv4ll_address
);
2702 goto ipv4ll_address_fail
;
2705 r
= sd_ipv4ll_new(&link
->ipv4ll
);
2707 return log_link_error_errno(link
, r
, "Falied to create IPv4LL client: %m");
2709 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
2711 return log_link_error_errno(link
, r
, "Falied to set inital IPv4LL address %s: %m", ipv4ll_address
);
2714 ipv4ll_address_fail
:
2719 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
2721 _cleanup_udev_device_unref_
struct udev_device
*device
= NULL
;
2722 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
2730 r
= link_new(m
, message
, ret
);
2736 log_link_debug(link
, "Link %d added", link
->ifindex
);
2738 r
= link_load(link
);
2742 if (detect_container() <= 0) {
2743 /* not in a container, udev will be around */
2744 sprintf(ifindex_str
, "n%d", link
->ifindex
);
2745 device
= udev_device_new_from_device_id(m
->udev
, ifindex_str
);
2747 r
= log_link_warning_errno(link
, errno
, "Could not find udev device: %m");
2751 if (udev_device_get_is_initialized(device
) <= 0) {
2753 log_link_debug(link
, "link pending udev initialization...");
2757 r
= link_initialized(link
, device
);
2761 /* we are calling a callback directly, so must take a ref */
2764 r
= link_initialized_and_synced(m
->rtnl
, NULL
, link
);
2771 link_enter_failed(link
);
2775 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
2780 log_link_info(link
, "Gained IPv6LL");
2782 link
->ipv6ll_address
= *address
;
2783 link_check_ready(link
);
2785 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
2786 r
= link_acquire_ipv6_conf(link
);
2788 link_enter_failed(link
);
2796 static int link_carrier_gained(Link
*link
) {
2801 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
2802 r
= link_acquire_conf(link
);
2804 link_enter_failed(link
);
2808 r
= link_enter_set_addresses(link
);
2813 r
= link_handle_bound_by_list(link
);
2820 static int link_carrier_lost(Link
*link
) {
2825 r
= link_stop_clients(link
);
2827 link_enter_failed(link
);
2831 r
= link_handle_bound_by_list(link
);
2838 int link_carrier_reset(Link
*link
) {
2843 if (link_has_carrier(link
)) {
2844 r
= link_carrier_lost(link
);
2848 r
= link_carrier_gained(link
);
2852 log_link_info(link
, "Reset carrier");
2858 int link_update(Link
*link
, sd_netlink_message
*m
) {
2859 struct ether_addr mac
;
2862 bool had_carrier
, carrier_gained
, carrier_lost
;
2866 assert(link
->ifname
);
2869 if (link
->state
== LINK_STATE_LINGER
) {
2871 log_link_info(link
, "Link readded");
2872 link_set_state(link
, LINK_STATE_ENSLAVING
);
2874 r
= link_new_carrier_maps(link
);
2879 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
2880 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
2881 log_link_info(link
, "Renamed to %s", ifname
);
2883 link_free_carrier_maps(link
);
2885 r
= free_and_strdup(&link
->ifname
, ifname
);
2889 r
= link_new_carrier_maps(link
);
2894 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
2895 if (r
>= 0 && mtu
> 0) {
2897 if (!link
->original_mtu
) {
2898 link
->original_mtu
= mtu
;
2899 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
2902 if (link
->dhcp_client
) {
2903 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
2906 log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
2912 /* The kernel may broadcast NEWLINK messages without the MAC address
2913 set, simply ignore them. */
2914 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
2916 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
2919 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
2922 log_link_debug(link
, "MAC address: "
2923 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
2924 mac
.ether_addr_octet
[0],
2925 mac
.ether_addr_octet
[1],
2926 mac
.ether_addr_octet
[2],
2927 mac
.ether_addr_octet
[3],
2928 mac
.ether_addr_octet
[4],
2929 mac
.ether_addr_octet
[5]);
2932 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
2934 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
2937 if (link
->dhcp_client
) {
2938 const DUID
*duid
= link_duid(link
);
2940 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
2941 (const uint8_t *) &link
->mac
,
2945 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
2947 r
= sd_dhcp_client_set_iaid_duid(link
->dhcp_client
,
2948 link
->network
->iaid
,
2950 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
2951 duid
->raw_data_len
);
2953 return log_link_warning_errno(link
, r
, "Could not update DUID/IAID in DHCP client: %m");
2956 if (link
->dhcp6_client
) {
2957 const DUID
* duid
= link_duid(link
);
2959 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
2960 (const uint8_t *) &link
->mac
,
2964 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
2966 r
= sd_dhcp6_client_set_iaid(link
->dhcp6_client
,
2967 link
->network
->iaid
);
2969 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 IAID: %m");
2971 r
= sd_dhcp6_client_set_duid(link
->dhcp6_client
,
2973 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
2974 duid
->raw_data_len
);
2976 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 DUID: %m");
2981 had_carrier
= link_has_carrier(link
);
2983 r
= link_update_flags(link
, m
);
2987 r
= link_update_lldp(link
);
2991 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
2992 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
2994 if (carrier_gained
) {
2995 log_link_info(link
, "Gained carrier");
2997 r
= link_carrier_gained(link
);
3000 } else if (carrier_lost
) {
3001 log_link_info(link
, "Lost carrier");
3003 r
= link_carrier_lost(link
);
3011 static void print_link_hashmap(FILE *f
, const char *prefix
, Hashmap
* h
) {
3019 if (hashmap_isempty(h
))
3023 HASHMAP_FOREACH(link
, h
, i
) {
3027 fprintf(f
, "%i", link
->ifindex
);
3034 int link_save(Link
*link
) {
3035 _cleanup_free_
char *temp_path
= NULL
;
3036 _cleanup_fclose_
FILE *f
= NULL
;
3037 const char *admin_state
, *oper_state
;
3044 assert(link
->state_file
);
3045 assert(link
->lease_file
);
3046 assert(link
->manager
);
3048 if (link
->state
== LINK_STATE_LINGER
) {
3049 unlink(link
->state_file
);
3053 link_lldp_save(link
);
3055 admin_state
= link_state_to_string(link
->state
);
3056 assert(admin_state
);
3058 oper_state
= link_operstate_to_string(link
->operstate
);
3061 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
3065 fchmod(fileno(f
), 0644);
3068 "# This is private data. Do not parse.\n"
3071 admin_state
, oper_state
);
3073 if (link
->network
) {
3075 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
3076 const char *dhcp_domainname
= NULL
;
3077 char **dhcp6_domains
= NULL
;
3079 if (link
->dhcp6_client
) {
3080 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
, &dhcp6_lease
);
3081 if (r
< 0 && r
!= -ENOMSG
)
3082 log_link_debug(link
, "No DHCPv6 lease");
3085 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
3089 fputstrv(f
, link
->network
->dns
, NULL
, &space
);
3091 if (link
->network
->dhcp_use_dns
&&
3093 const struct in_addr
*addresses
;
3095 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
3099 serialize_in_addrs(f
, addresses
, r
);
3104 if (link
->network
->dhcp_use_dns
&& dhcp6_lease
) {
3105 struct in6_addr
*in6_addrs
;
3107 r
= sd_dhcp6_lease_get_dns(dhcp6_lease
, &in6_addrs
);
3111 serialize_in6_addrs(f
, in6_addrs
, r
);
3116 /* Make sure to flush out old entries before we use the NDISC data */
3119 if (link
->network
->dhcp_use_dns
&& link
->ndisc_rdnss
) {
3122 SET_FOREACH(dd
, link
->ndisc_rdnss
, i
) {
3126 serialize_in6_addrs(f
, &dd
->address
, 1);
3135 fputstrv(f
, link
->network
->ntp
, NULL
, &space
);
3137 if (link
->network
->dhcp_use_ntp
&&
3139 const struct in_addr
*addresses
;
3141 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
3145 serialize_in_addrs(f
, addresses
, r
);
3150 if (link
->network
->dhcp_use_ntp
&& dhcp6_lease
) {
3151 struct in6_addr
*in6_addrs
;
3154 r
= sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease
,
3159 serialize_in6_addrs(f
, in6_addrs
, r
);
3163 r
= sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease
, &hosts
);
3165 fputstrv(f
, hosts
, NULL
, &space
);
3170 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
3171 if (link
->dhcp_lease
)
3172 (void) sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &dhcp_domainname
);
3174 (void) sd_dhcp6_lease_get_domains(dhcp6_lease
, &dhcp6_domains
);
3177 fputs("DOMAINS=", f
);
3178 fputstrv(f
, link
->network
->search_domains
, NULL
, &space
);
3180 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
) {
3183 if (dhcp_domainname
)
3184 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3186 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3188 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3189 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3194 fputs("ROUTE_DOMAINS=", f
);
3195 fputstrv(f
, link
->network
->route_domains
, NULL
, NULL
);
3197 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
) {
3200 if (dhcp_domainname
)
3201 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3203 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3205 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3206 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3211 fprintf(f
, "LLMNR=%s\n",
3212 resolve_support_to_string(link
->network
->llmnr
));
3213 fprintf(f
, "MDNS=%s\n",
3214 resolve_support_to_string(link
->network
->mdns
));
3216 if (link
->network
->dnssec_mode
!= _DNSSEC_MODE_INVALID
)
3217 fprintf(f
, "DNSSEC=%s\n",
3218 dnssec_mode_to_string(link
->network
->dnssec_mode
));
3220 if (!set_isempty(link
->network
->dnssec_negative_trust_anchors
)) {
3223 fputs("DNSSEC_NTA=", f
);
3225 SET_FOREACH(n
, link
->network
->dnssec_negative_trust_anchors
, i
)
3226 fputs_with_space(f
, n
, NULL
, &space
);
3230 fputs("ADDRESSES=", f
);
3232 SET_FOREACH(a
, link
->addresses
, i
) {
3233 _cleanup_free_
char *address_str
= NULL
;
3235 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
3239 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
3244 fputs("ROUTES=", f
);
3246 SET_FOREACH(route
, link
->routes
, i
) {
3247 _cleanup_free_
char *route_str
= NULL
;
3249 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
3253 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%hhu/"USEC_FMT
, space
? " " : "", route_str
,
3254 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
3261 print_link_hashmap(f
, "CARRIER_BOUND_TO=", link
->bound_to_links
);
3262 print_link_hashmap(f
, "CARRIER_BOUND_BY=", link
->bound_by_links
);
3264 if (link
->dhcp_lease
) {
3265 struct in_addr address
;
3266 const char *tz
= NULL
;
3268 assert(link
->network
);
3270 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
3272 fprintf(f
, "TIMEZONE=%s\n", tz
);
3274 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
3276 fputs("DHCP4_ADDRESS=", f
);
3277 serialize_in_addrs(f
, &address
, 1);
3281 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
3289 unlink(link
->lease_file
);
3292 struct in_addr address
;
3294 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
3296 fputs("IPV4LL_ADDRESS=", f
);
3297 serialize_in_addrs(f
, &address
, 1);
3302 r
= fflush_and_check(f
);
3306 if (rename(temp_path
, link
->state_file
) < 0) {
3314 (void) unlink(link
->state_file
);
3316 (void) unlink(temp_path
);
3318 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
3321 /* The serialized state in /run is no longer up-to-date. */
3322 void link_dirty(Link
*link
) {
3327 /* mark manager dirty as link is dirty */
3328 manager_dirty(link
->manager
);
3330 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
3332 /* allocation errors are ignored */
3335 r
= set_put(link
->manager
->dirty_links
, link
);
3337 /* don't take another ref if the link was already dirty */
3343 /* The serialized state in /run is up-to-date */
3344 void link_clean(Link
*link
) {
3346 assert(link
->manager
);
3348 set_remove(link
->manager
->dirty_links
, link
);
3352 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
3353 [LINK_STATE_PENDING
] = "pending",
3354 [LINK_STATE_ENSLAVING
] = "configuring",
3355 [LINK_STATE_SETTING_ADDRESSES
] = "configuring",
3356 [LINK_STATE_SETTING_ROUTES
] = "configuring",
3357 [LINK_STATE_CONFIGURED
] = "configured",
3358 [LINK_STATE_UNMANAGED
] = "unmanaged",
3359 [LINK_STATE_FAILED
] = "failed",
3360 [LINK_STATE_LINGER
] = "linger",
3363 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);
3365 static const char* const link_operstate_table
[_LINK_OPERSTATE_MAX
] = {
3366 [LINK_OPERSTATE_OFF
] = "off",
3367 [LINK_OPERSTATE_NO_CARRIER
] = "no-carrier",
3368 [LINK_OPERSTATE_DORMANT
] = "dormant",
3369 [LINK_OPERSTATE_CARRIER
] = "carrier",
3370 [LINK_OPERSTATE_DEGRADED
] = "degraded",
3371 [LINK_OPERSTATE_ROUTABLE
] = "routable",
3374 DEFINE_STRING_TABLE_LOOKUP(link_operstate
, LinkOperationalState
);