2 This file is part of systemd.
4 Copyright 2013 Tom Gundersen <teg@jklm.no>
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 #include <netinet/ether.h>
24 #include "alloc-util.h"
26 #include "dhcp-lease-internal.h"
29 #include "netlink-util.h"
30 #include "network-internal.h"
31 #include "networkd-lldp-tx.h"
32 #include "networkd-ndisc.h"
35 #include "socket-util.h"
36 #include "stdio-util.h"
37 #include "string-table.h"
38 #include "udev-util.h"
42 static bool link_dhcp6_enabled(Link
*link
) {
45 if (!socket_ipv6_is_supported())
48 if (link
->flags
& IFF_LOOPBACK
)
54 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV6
;
57 static bool link_dhcp4_enabled(Link
*link
) {
60 if (link
->flags
& IFF_LOOPBACK
)
66 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV4
;
69 static bool link_dhcp4_server_enabled(Link
*link
) {
72 if (link
->flags
& IFF_LOOPBACK
)
78 return link
->network
->dhcp_server
;
81 static bool link_ipv4ll_enabled(Link
*link
) {
84 if (link
->flags
& IFF_LOOPBACK
)
90 return link
->network
->link_local
& ADDRESS_FAMILY_IPV4
;
93 static bool link_ipv6ll_enabled(Link
*link
) {
96 if (!socket_ipv6_is_supported())
99 if (link
->flags
& IFF_LOOPBACK
)
105 return link
->network
->link_local
& ADDRESS_FAMILY_IPV6
;
108 static bool link_ipv6_enabled(Link
*link
) {
111 if (!socket_ipv6_is_supported())
114 if (link
->network
->bridge
)
117 /* DHCPv6 client will not be started if no IPv6 link-local address is configured. */
118 return link_ipv6ll_enabled(link
) || network_has_static_ipv6_addresses(link
->network
);
121 static bool link_lldp_rx_enabled(Link
*link
) {
124 if (link
->flags
& IFF_LOOPBACK
)
127 if (link
->iftype
!= ARPHRD_ETHER
)
133 if (link
->network
->bridge
)
136 return link
->network
->lldp_mode
!= LLDP_MODE_NO
;
139 static bool link_lldp_emit_enabled(Link
*link
) {
142 if (link
->flags
& IFF_LOOPBACK
)
145 if (link
->iftype
!= ARPHRD_ETHER
)
151 return link
->network
->lldp_emit
!= LLDP_EMIT_NO
;
154 static bool link_ipv4_forward_enabled(Link
*link
) {
157 if (link
->flags
& IFF_LOOPBACK
)
163 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
166 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV4
;
169 static bool link_ipv6_forward_enabled(Link
*link
) {
172 if (!socket_ipv6_is_supported())
175 if (link
->flags
& IFF_LOOPBACK
)
181 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
184 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV6
;
187 static bool link_proxy_arp_enabled(Link
*link
) {
190 if (link
->flags
& IFF_LOOPBACK
)
196 if (link
->network
->proxy_arp
< 0)
202 static bool link_ipv6_accept_ra_enabled(Link
*link
) {
205 if (!socket_ipv6_is_supported())
208 if (link
->flags
& IFF_LOOPBACK
)
214 /* If unset use system default (enabled if local forwarding is disabled.
215 * disabled if local forwarding is enabled).
216 * If set, ignore or enforce RA independent of local forwarding state.
218 if (link
->network
->ipv6_accept_ra
< 0)
219 /* default to accept RA if ip_forward is disabled and ignore RA if ip_forward is enabled */
220 return !link_ipv6_forward_enabled(link
);
221 else if (link
->network
->ipv6_accept_ra
> 0)
222 /* accept RA even if ip_forward is enabled */
229 static IPv6PrivacyExtensions
link_ipv6_privacy_extensions(Link
*link
) {
232 if (!socket_ipv6_is_supported())
233 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
235 if (link
->flags
& IFF_LOOPBACK
)
236 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
239 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
241 return link
->network
->ipv6_privacy_extensions
;
244 static int link_enable_ipv6(Link
*link
) {
245 const char *p
= NULL
;
249 if (link
->flags
& IFF_LOOPBACK
)
252 disabled
= !link_ipv6_enabled(link
);
254 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/disable_ipv6");
256 r
= write_string_file(p
, one_zero(disabled
), WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
258 log_link_warning_errno(link
, r
, "Cannot %s IPv6 for interface %s: %m", disabled
? "disable" : "enable", link
->ifname
);
260 log_link_info(link
, "IPv6 %sd for interface: %m", enable_disable(!disabled
));
265 void link_update_operstate(Link
*link
) {
266 LinkOperationalState operstate
;
269 if (link
->kernel_operstate
== IF_OPER_DORMANT
)
270 operstate
= LINK_OPERSTATE_DORMANT
;
271 else if (link_has_carrier(link
)) {
273 uint8_t scope
= RT_SCOPE_NOWHERE
;
276 /* if we have carrier, check what addresses we have */
277 SET_FOREACH(address
, link
->addresses
, i
) {
278 if (!address_is_ready(address
))
281 if (address
->scope
< scope
)
282 scope
= address
->scope
;
285 /* for operstate we also take foreign addresses into account */
286 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
287 if (!address_is_ready(address
))
290 if (address
->scope
< scope
)
291 scope
= address
->scope
;
294 if (scope
< RT_SCOPE_SITE
)
295 /* universally accessible addresses found */
296 operstate
= LINK_OPERSTATE_ROUTABLE
;
297 else if (scope
< RT_SCOPE_HOST
)
298 /* only link or site local addresses found */
299 operstate
= LINK_OPERSTATE_DEGRADED
;
301 /* no useful addresses found */
302 operstate
= LINK_OPERSTATE_CARRIER
;
303 } else if (link
->flags
& IFF_UP
)
304 operstate
= LINK_OPERSTATE_NO_CARRIER
;
306 operstate
= LINK_OPERSTATE_OFF
;
308 if (link
->operstate
!= operstate
) {
309 link
->operstate
= operstate
;
310 link_send_changed(link
, "OperationalState", NULL
);
315 #define FLAG_STRING(string, flag, old, new) \
316 (((old ^ new) & flag) \
317 ? ((old & flag) ? (" -" string) : (" +" string)) \
320 static int link_update_flags(Link
*link
, sd_netlink_message
*m
) {
321 unsigned flags
, unknown_flags_added
, unknown_flags_removed
, unknown_flags
;
327 r
= sd_rtnl_message_link_get_flags(m
, &flags
);
329 return log_link_warning_errno(link
, r
, "Could not get link flags: %m");
331 r
= sd_netlink_message_read_u8(m
, IFLA_OPERSTATE
, &operstate
);
333 /* if we got a message without operstate, take it to mean
334 the state was unchanged */
335 operstate
= link
->kernel_operstate
;
337 if ((link
->flags
== flags
) && (link
->kernel_operstate
== operstate
))
340 if (link
->flags
!= flags
) {
341 log_link_debug(link
, "Flags change:%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
342 FLAG_STRING("LOOPBACK", IFF_LOOPBACK
, link
->flags
, flags
),
343 FLAG_STRING("MASTER", IFF_MASTER
, link
->flags
, flags
),
344 FLAG_STRING("SLAVE", IFF_SLAVE
, link
->flags
, flags
),
345 FLAG_STRING("UP", IFF_UP
, link
->flags
, flags
),
346 FLAG_STRING("DORMANT", IFF_DORMANT
, link
->flags
, flags
),
347 FLAG_STRING("LOWER_UP", IFF_LOWER_UP
, link
->flags
, flags
),
348 FLAG_STRING("RUNNING", IFF_RUNNING
, link
->flags
, flags
),
349 FLAG_STRING("MULTICAST", IFF_MULTICAST
, link
->flags
, flags
),
350 FLAG_STRING("BROADCAST", IFF_BROADCAST
, link
->flags
, flags
),
351 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT
, link
->flags
, flags
),
352 FLAG_STRING("PROMISC", IFF_PROMISC
, link
->flags
, flags
),
353 FLAG_STRING("ALLMULTI", IFF_ALLMULTI
, link
->flags
, flags
),
354 FLAG_STRING("PORTSEL", IFF_PORTSEL
, link
->flags
, flags
),
355 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA
, link
->flags
, flags
),
356 FLAG_STRING("DYNAMIC", IFF_DYNAMIC
, link
->flags
, flags
),
357 FLAG_STRING("NOARP", IFF_NOARP
, link
->flags
, flags
),
358 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS
, link
->flags
, flags
),
359 FLAG_STRING("DEBUG", IFF_DEBUG
, link
->flags
, flags
),
360 FLAG_STRING("ECHO", IFF_ECHO
, link
->flags
, flags
));
362 unknown_flags
= ~(IFF_LOOPBACK
| IFF_MASTER
| IFF_SLAVE
| IFF_UP
|
363 IFF_DORMANT
| IFF_LOWER_UP
| IFF_RUNNING
|
364 IFF_MULTICAST
| IFF_BROADCAST
| IFF_POINTOPOINT
|
365 IFF_PROMISC
| IFF_ALLMULTI
| IFF_PORTSEL
|
366 IFF_AUTOMEDIA
| IFF_DYNAMIC
| IFF_NOARP
|
367 IFF_NOTRAILERS
| IFF_DEBUG
| IFF_ECHO
);
368 unknown_flags_added
= ((link
->flags
^ flags
) & flags
& unknown_flags
);
369 unknown_flags_removed
= ((link
->flags
^ flags
) & link
->flags
& unknown_flags
);
371 /* link flags are currently at most 18 bits, let's align to
373 if (unknown_flags_added
)
375 "Unknown link flags gained: %#.5x (ignoring)",
376 unknown_flags_added
);
378 if (unknown_flags_removed
)
380 "Unknown link flags lost: %#.5x (ignoring)",
381 unknown_flags_removed
);
385 link
->kernel_operstate
= operstate
;
387 link_update_operstate(link
);
392 static int link_new(Manager
*manager
, sd_netlink_message
*message
, Link
**ret
) {
393 _cleanup_link_unref_ Link
*link
= NULL
;
395 const char *ifname
, *kind
= NULL
;
397 unsigned short iftype
;
403 /* check for link kind */
404 r
= sd_netlink_message_enter_container(message
, IFLA_LINKINFO
);
406 (void)sd_netlink_message_read_string(message
, IFLA_INFO_KIND
, &kind
);
407 r
= sd_netlink_message_exit_container(message
);
412 r
= sd_netlink_message_get_type(message
, &type
);
415 else if (type
!= RTM_NEWLINK
)
418 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
421 else if (ifindex
<= 0)
424 r
= sd_rtnl_message_link_get_type(message
, &iftype
);
428 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &ifname
);
432 link
= new0(Link
, 1);
437 link
->manager
= manager
;
438 link
->state
= LINK_STATE_PENDING
;
439 link
->rtnl_extended_attrs
= true;
440 link
->ifindex
= ifindex
;
441 link
->iftype
= iftype
;
442 link
->ifname
= strdup(ifname
);
447 link
->kind
= strdup(kind
);
452 r
= sd_netlink_message_read_ether_addr(message
, IFLA_ADDRESS
, &link
->mac
);
454 log_link_debug_errno(link
, r
, "MAC address not found for new device, continuing without");
456 if (asprintf(&link
->state_file
, "/run/systemd/netif/links/%d", link
->ifindex
) < 0)
459 if (asprintf(&link
->lease_file
, "/run/systemd/netif/leases/%d", link
->ifindex
) < 0)
462 if (asprintf(&link
->lldp_file
, "/run/systemd/netif/lldp/%d", link
->ifindex
) < 0)
465 r
= hashmap_ensure_allocated(&manager
->links
, NULL
);
469 r
= hashmap_put(manager
->links
, INT_TO_PTR(link
->ifindex
), link
);
473 r
= link_update_flags(link
, message
);
483 static void link_free(Link
*link
) {
491 while (!set_isempty(link
->addresses
))
492 address_free(set_first(link
->addresses
));
494 while (!set_isempty(link
->addresses_foreign
))
495 address_free(set_first(link
->addresses_foreign
));
497 link
->addresses
= set_free(link
->addresses
);
499 link
->addresses_foreign
= set_free(link
->addresses_foreign
);
501 while ((address
= link
->pool_addresses
)) {
502 LIST_REMOVE(addresses
, link
->pool_addresses
, address
);
503 address_free(address
);
506 sd_dhcp_server_unref(link
->dhcp_server
);
507 sd_dhcp_client_unref(link
->dhcp_client
);
508 sd_dhcp_lease_unref(link
->dhcp_lease
);
510 link_lldp_emit_stop(link
);
512 free(link
->lease_file
);
514 sd_lldp_unref(link
->lldp
);
515 free(link
->lldp_file
);
519 sd_ipv4ll_unref(link
->ipv4ll
);
520 sd_dhcp6_client_unref(link
->dhcp6_client
);
521 sd_ndisc_unref(link
->ndisc
);
524 hashmap_remove(link
->manager
->links
, INT_TO_PTR(link
->ifindex
));
530 (void)unlink(link
->state_file
);
531 free(link
->state_file
);
533 udev_device_unref(link
->udev_device
);
535 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
)
536 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(carrier
->ifindex
));
537 hashmap_free(link
->bound_to_links
);
539 HASHMAP_FOREACH (carrier
, link
->bound_by_links
, i
)
540 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(carrier
->ifindex
));
541 hashmap_free(link
->bound_by_links
);
546 Link
*link_unref(Link
*link
) {
550 assert(link
->n_ref
> 0);
562 Link
*link_ref(Link
*link
) {
566 assert(link
->n_ref
> 0);
573 int link_get(Manager
*m
, int ifindex
, Link
**ret
) {
580 link
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
589 static void link_set_state(Link
*link
, LinkState state
) {
592 if (link
->state
== state
)
597 link_send_changed(link
, "AdministrativeState", NULL
);
600 static void link_enter_unmanaged(Link
*link
) {
603 log_link_debug(link
, "Unmanaged");
605 link_set_state(link
, LINK_STATE_UNMANAGED
);
610 static int link_stop_clients(Link
*link
) {
614 assert(link
->manager
);
615 assert(link
->manager
->event
);
617 if (link
->dhcp_client
) {
618 k
= sd_dhcp_client_stop(link
->dhcp_client
);
620 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv4 client: %m");
624 k
= sd_ipv4ll_stop(link
->ipv4ll
);
626 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 link-local: %m");
629 if (link
->dhcp6_client
) {
630 k
= sd_dhcp6_client_stop(link
->dhcp6_client
);
632 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv6 client: %m");
636 k
= sd_ndisc_stop(link
->ndisc
);
638 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Discovery: %m");
641 link_lldp_emit_stop(link
);
645 void link_enter_failed(Link
*link
) {
648 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
651 log_link_warning(link
, "Failed");
653 link_set_state(link
, LINK_STATE_FAILED
);
655 link_stop_clients(link
);
660 static Address
* link_find_dhcp_server_address(Link
*link
) {
664 assert(link
->network
);
666 /* The first statically configured address if there is any */
667 LIST_FOREACH(addresses
, address
, link
->network
->static_addresses
) {
669 if (address
->family
!= AF_INET
)
672 if (in_addr_is_null(address
->family
, &address
->in_addr
))
678 /* If that didn't work, find a suitable address we got from the pool */
679 LIST_FOREACH(addresses
, address
, link
->pool_addresses
) {
680 if (address
->family
!= AF_INET
)
689 static int link_enter_configured(Link
*link
) {
691 assert(link
->network
);
692 assert(link
->state
== LINK_STATE_SETTING_ROUTES
);
694 log_link_info(link
, "Configured");
696 link_set_state(link
, LINK_STATE_CONFIGURED
);
703 void link_check_ready(Link
*link
) {
709 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
715 if (!link
->static_configured
)
718 if (link_ipv4ll_enabled(link
))
719 if (!link
->ipv4ll_address
||
723 if (link_ipv6ll_enabled(link
))
724 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) > 0)
727 if ((link_dhcp4_enabled(link
) && !link_dhcp6_enabled(link
) &&
728 !link
->dhcp4_configured
) ||
729 (link_dhcp6_enabled(link
) && !link_dhcp4_enabled(link
) &&
730 !link
->dhcp6_configured
) ||
731 (link_dhcp4_enabled(link
) && link_dhcp6_enabled(link
) &&
732 !link
->dhcp4_configured
&& !link
->dhcp6_configured
))
735 if (link_ipv6_accept_ra_enabled(link
) && !link
->ndisc_configured
)
738 SET_FOREACH(a
, link
->addresses
, i
)
739 if (!address_is_ready(a
))
742 if (link
->state
!= LINK_STATE_CONFIGURED
)
743 link_enter_configured(link
);
748 static int route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
749 _cleanup_link_unref_ Link
*link
= userdata
;
752 assert(link
->link_messages
> 0);
753 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
754 LINK_STATE_SETTING_ROUTES
, LINK_STATE_FAILED
,
757 link
->link_messages
--;
759 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
762 r
= sd_netlink_message_get_errno(m
);
763 if (r
< 0 && r
!= -EEXIST
)
764 log_link_warning_errno(link
, r
, "Could not set route: %m");
766 if (link
->link_messages
== 0) {
767 log_link_debug(link
, "Routes set");
768 link
->static_configured
= true;
769 link_check_ready(link
);
775 static int link_enter_set_routes(Link
*link
) {
780 assert(link
->network
);
781 assert(link
->state
== LINK_STATE_SETTING_ADDRESSES
);
783 link_set_state(link
, LINK_STATE_SETTING_ROUTES
);
785 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
786 r
= route_configure(rt
, link
, route_handler
);
788 log_link_warning_errno(link
, r
, "Could not set routes: %m");
789 link_enter_failed(link
);
793 link
->link_messages
++;
796 if (link
->link_messages
== 0) {
797 link
->static_configured
= true;
798 link_check_ready(link
);
800 log_link_debug(link
, "Setting routes");
805 int link_route_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
806 _cleanup_link_unref_ Link
*link
= userdata
;
811 assert(link
->ifname
);
813 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
816 r
= sd_netlink_message_get_errno(m
);
817 if (r
< 0 && r
!= -ESRCH
)
818 log_link_warning_errno(link
, r
, "Could not drop route: %m");
823 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
824 _cleanup_link_unref_ Link
*link
= userdata
;
830 assert(link
->ifname
);
831 assert(link
->link_messages
> 0);
832 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
833 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
835 link
->link_messages
--;
837 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
840 r
= sd_netlink_message_get_errno(m
);
841 if (r
< 0 && r
!= -EEXIST
)
842 log_link_warning_errno(link
, r
, "could not set address: %m");
844 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
846 if (link
->link_messages
== 0) {
847 log_link_debug(link
, "Addresses set");
848 link_enter_set_routes(link
);
854 static int link_push_dns_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
855 _cleanup_free_
struct in_addr
*addresses
= NULL
;
856 size_t n_addresses
= 0, n_allocated
= 0;
859 log_debug("Copying DNS server information from %s", link
->ifname
);
864 STRV_FOREACH(a
, link
->network
->dns
) {
867 /* Only look for IPv4 addresses */
868 if (inet_pton(AF_INET
, *a
, &ia
) <= 0)
871 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
874 addresses
[n_addresses
++] = ia
;
877 if (link
->network
->dhcp_use_dns
&&
879 const struct in_addr
*da
= NULL
;
882 n
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &da
);
885 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
888 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
893 if (n_addresses
<= 0)
896 return sd_dhcp_server_set_dns(s
, addresses
, n_addresses
);
899 static int link_push_ntp_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
900 _cleanup_free_
struct in_addr
*addresses
= NULL
;
901 size_t n_addresses
= 0, n_allocated
= 0;
907 log_debug("Copying NTP server information from %s", link
->ifname
);
909 STRV_FOREACH(a
, link
->network
->ntp
) {
912 /* Only look for IPv4 addresses */
913 if (inet_pton(AF_INET
, *a
, &ia
) <= 0)
916 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
919 addresses
[n_addresses
++] = ia
;
922 if (link
->network
->dhcp_use_ntp
&&
924 const struct in_addr
*da
= NULL
;
927 n
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &da
);
930 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
933 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
938 if (n_addresses
<= 0)
941 return sd_dhcp_server_set_ntp(s
, addresses
, n_addresses
);
944 static int link_set_bridge_fdb(Link
*link
) {
948 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
949 r
= fdb_entry_configure(link
, fdb_entry
);
951 return log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
957 static int link_enter_set_addresses(Link
*link
) {
962 assert(link
->network
);
963 assert(link
->state
!= _LINK_STATE_INVALID
);
965 r
= link_set_bridge_fdb(link
);
969 link_set_state(link
, LINK_STATE_SETTING_ADDRESSES
);
971 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
972 r
= address_configure(ad
, link
, address_handler
, false);
974 log_link_warning_errno(link
, r
, "Could not set addresses: %m");
975 link_enter_failed(link
);
979 link
->link_messages
++;
982 /* now that we can figure out a default address for the dhcp server,
984 if (link_dhcp4_server_enabled(link
)) {
987 bool acquired_uplink
= false;
989 address
= link_find_dhcp_server_address(link
);
991 log_link_warning(link
, "Failed to find suitable address for DHCPv4 server instance.");
992 link_enter_failed(link
);
996 /* use the server address' subnet as the pool */
997 r
= sd_dhcp_server_configure_pool(link
->dhcp_server
, &address
->in_addr
.in
, address
->prefixlen
,
998 link
->network
->dhcp_server_pool_offset
, link
->network
->dhcp_server_pool_size
);
1003 r = sd_dhcp_server_set_router(link->dhcp_server,
1004 &main_address->in_addr.in);
1009 if (link
->network
->dhcp_server_max_lease_time_usec
> 0) {
1010 r
= sd_dhcp_server_set_max_lease_time(
1012 DIV_ROUND_UP(link
->network
->dhcp_server_max_lease_time_usec
, USEC_PER_SEC
));
1017 if (link
->network
->dhcp_server_default_lease_time_usec
> 0) {
1018 r
= sd_dhcp_server_set_default_lease_time(
1020 DIV_ROUND_UP(link
->network
->dhcp_server_default_lease_time_usec
, USEC_PER_SEC
));
1025 if (link
->network
->dhcp_server_emit_dns
) {
1027 if (link
->network
->n_dhcp_server_dns
> 0)
1028 r
= sd_dhcp_server_set_dns(link
->dhcp_server
, link
->network
->dhcp_server_dns
, link
->network
->n_dhcp_server_dns
);
1030 uplink
= manager_find_uplink(link
->manager
, link
);
1031 acquired_uplink
= true;
1034 log_link_debug(link
, "Not emitting DNS server information on link, couldn't find suitable uplink.");
1037 r
= link_push_dns_to_dhcp_server(uplink
, link
->dhcp_server
);
1040 log_link_warning_errno(link
, r
, "Failed to set DNS server for DHCP server, ignoring: %m");
1044 if (link
->network
->dhcp_server_emit_ntp
) {
1046 if (link
->network
->n_dhcp_server_ntp
> 0)
1047 r
= sd_dhcp_server_set_ntp(link
->dhcp_server
, link
->network
->dhcp_server_ntp
, link
->network
->n_dhcp_server_ntp
);
1049 if (!acquired_uplink
)
1050 uplink
= manager_find_uplink(link
->manager
, link
);
1053 log_link_debug(link
, "Not emitting NTP server information on link, couldn't find suitable uplink.");
1056 r
= link_push_ntp_to_dhcp_server(uplink
, link
->dhcp_server
);
1060 log_link_warning_errno(link
, r
, "Failed to set NTP server for DHCP server, ignoring: %m");
1063 r
= sd_dhcp_server_set_emit_router(link
->dhcp_server
, link
->network
->dhcp_server_emit_router
);
1065 log_link_warning_errno(link
, r
, "Failed to set router emission for DHCP server: %m");
1069 if (link
->network
->dhcp_server_emit_timezone
) {
1070 _cleanup_free_
char *buffer
= NULL
;
1071 const char *tz
= NULL
;
1073 if (link
->network
->dhcp_server_timezone
)
1074 tz
= link
->network
->dhcp_server_timezone
;
1076 r
= get_timezone(&buffer
);
1078 log_warning_errno(r
, "Failed to determine timezone: %m");
1084 r
= sd_dhcp_server_set_timezone(link
->dhcp_server
, tz
);
1090 r
= sd_dhcp_server_start(link
->dhcp_server
);
1092 log_link_warning_errno(link
, r
, "Could not start DHCPv4 server instance: %m");
1094 link_enter_failed(link
);
1099 log_link_debug(link
, "Offering DHCPv4 leases");
1102 if (link
->link_messages
== 0)
1103 link_enter_set_routes(link
);
1105 log_link_debug(link
, "Setting addresses");
1110 int link_address_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1111 _cleanup_link_unref_ Link
*link
= userdata
;
1116 assert(link
->ifname
);
1118 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1121 r
= sd_netlink_message_get_errno(m
);
1122 if (r
< 0 && r
!= -EADDRNOTAVAIL
)
1123 log_link_warning_errno(link
, r
, "Could not drop address: %m");
1128 static int link_set_bridge_vlan(Link
*link
) {
1131 r
= br_vlan_configure(link
, link
->network
->pvid
, link
->network
->br_vid_bitmap
, link
->network
->br_untagged_bitmap
);
1133 log_link_error_errno(link
, r
, "Failed to assign VLANs to bridge port: %m");
1138 static int link_set_proxy_arp(Link
*link
) {
1139 const char *p
= NULL
;
1142 if (!link_proxy_arp_enabled(link
))
1145 p
= strjoina("/proc/sys/net/ipv4/conf/", link
->ifname
, "/proxy_arp");
1147 r
= write_string_file(p
, one_zero(link
->network
->proxy_arp
), WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
1149 log_link_warning_errno(link
, r
, "Cannot configure proxy ARP for interface: %m");
1154 static int link_set_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1155 _cleanup_link_unref_ Link
*link
= userdata
;
1158 log_link_debug(link
, "Set link");
1160 r
= sd_netlink_message_get_errno(m
);
1161 if (r
< 0 && r
!= -EEXIST
) {
1162 log_link_error_errno(link
, r
, "Could not join netdev: %m");
1163 link_enter_failed(link
);
1170 static int set_hostname_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
1171 _cleanup_link_unref_ Link
*link
= userdata
;
1172 const sd_bus_error
*e
;
1177 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1180 e
= sd_bus_message_get_error(m
);
1182 log_link_warning_errno(link
, sd_bus_error_get_errno(e
), "Could not set hostname: %s", e
->message
);
1187 int link_set_hostname(Link
*link
, const char *hostname
) {
1191 assert(link
->manager
);
1193 log_link_debug(link
, "Setting transient hostname: '%s'", strna(hostname
));
1195 if (!link
->manager
->bus
) {
1196 /* TODO: replace by assert when we can rely on kdbus */
1197 log_link_info(link
, "Not connected to system bus, ignoring transient hostname.");
1201 r
= sd_bus_call_method_async(
1204 "org.freedesktop.hostname1",
1205 "/org/freedesktop/hostname1",
1206 "org.freedesktop.hostname1",
1208 set_hostname_handler
,
1215 return log_link_error_errno(link
, r
, "Could not set transient hostname: %m");
1222 static int set_timezone_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
1223 _cleanup_link_unref_ Link
*link
= userdata
;
1224 const sd_bus_error
*e
;
1229 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1232 e
= sd_bus_message_get_error(m
);
1234 log_link_warning_errno(link
, sd_bus_error_get_errno(e
), "Could not set timezone: %s", e
->message
);
1239 int link_set_timezone(Link
*link
, const char *tz
) {
1243 assert(link
->manager
);
1246 log_link_debug(link
, "Setting system timezone: '%s'", tz
);
1248 if (!link
->manager
->bus
) {
1249 log_link_info(link
, "Not connected to system bus, ignoring timezone.");
1253 r
= sd_bus_call_method_async(
1256 "org.freedesktop.timedate1",
1257 "/org/freedesktop/timedate1",
1258 "org.freedesktop.timedate1",
1260 set_timezone_handler
,
1266 return log_link_error_errno(link
, r
, "Could not set timezone: %m");
1273 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1274 _cleanup_link_unref_ Link
*link
= userdata
;
1279 assert(link
->ifname
);
1281 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1284 r
= sd_netlink_message_get_errno(m
);
1286 log_link_warning_errno(link
, r
, "Could not set MTU: %m");
1291 int link_set_mtu(Link
*link
, uint32_t mtu
) {
1292 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1296 assert(link
->manager
);
1297 assert(link
->manager
->rtnl
);
1299 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1301 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1303 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1305 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1307 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1309 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, set_mtu_handler
, link
, 0, NULL
);
1311 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1318 static int set_flags_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1319 _cleanup_link_unref_ Link
*link
= userdata
;
1324 assert(link
->ifname
);
1326 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1329 r
= sd_netlink_message_get_errno(m
);
1331 log_link_warning_errno(link
, r
, "Could not set link flags: %m");
1336 static int link_set_flags(Link
*link
) {
1337 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1338 unsigned ifi_change
= 0;
1339 unsigned ifi_flags
= 0;
1343 assert(link
->manager
);
1344 assert(link
->manager
->rtnl
);
1346 if (link
->flags
& IFF_LOOPBACK
)
1352 if (link
->network
->arp
< 0)
1355 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1357 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1359 if (link
->network
->arp
>= 0) {
1360 ifi_change
|= IFF_NOARP
;
1361 ifi_flags
|= link
->network
->arp
? 0 : IFF_NOARP
;
1364 r
= sd_rtnl_message_link_set_flags(req
, ifi_flags
, ifi_change
);
1366 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1368 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, set_flags_handler
, link
, 0, NULL
);
1370 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1377 static int link_set_bridge(Link
*link
) {
1378 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1382 assert(link
->network
);
1384 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1386 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1388 r
= sd_rtnl_message_link_set_family(req
, PF_BRIDGE
);
1390 return log_link_error_errno(link
, r
, "Could not set message family: %m");
1392 r
= sd_netlink_message_open_container(req
, IFLA_PROTINFO
);
1394 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1396 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_GUARD
, !link
->network
->use_bpdu
);
1398 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_GUARD attribute: %m");
1400 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MODE
, link
->network
->hairpin
);
1402 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MODE attribute: %m");
1404 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_FAST_LEAVE
, link
->network
->fast_leave
);
1406 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_FAST_LEAVE attribute: %m");
1408 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_PROTECT
, !link
->network
->allow_port_to_be_root
);
1410 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PROTECT attribute: %m");
1412 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_UNICAST_FLOOD
, link
->network
->unicast_flood
);
1414 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_UNICAST_FLOOD attribute: %m");
1416 if (link
->network
->cost
!= 0) {
1417 r
= sd_netlink_message_append_u32(req
, IFLA_BRPORT_COST
, link
->network
->cost
);
1419 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_COST attribute: %m");
1422 r
= sd_netlink_message_close_container(req
);
1424 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1426 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_set_handler
, link
, 0, NULL
);
1428 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1435 static int link_lldp_save(Link
*link
) {
1436 _cleanup_free_
char *temp_path
= NULL
;
1437 _cleanup_fclose_
FILE *f
= NULL
;
1438 sd_lldp_neighbor
**l
= NULL
;
1442 assert(link
->lldp_file
);
1445 (void) unlink(link
->lldp_file
);
1449 r
= sd_lldp_get_neighbors(link
->lldp
, &l
);
1453 (void) unlink(link
->lldp_file
);
1459 r
= fopen_temporary(link
->lldp_file
, &f
, &temp_path
);
1463 fchmod(fileno(f
), 0644);
1465 for (i
= 0; i
< n
; i
++) {
1470 r
= sd_lldp_neighbor_get_raw(l
[i
], &p
, &sz
);
1475 (void) fwrite(&u
, 1, sizeof(u
), f
);
1476 (void) fwrite(p
, 1, sz
, f
);
1479 r
= fflush_and_check(f
);
1483 if (rename(temp_path
, link
->lldp_file
) < 0) {
1490 (void) unlink(link
->lldp_file
);
1492 (void) unlink(temp_path
);
1494 log_link_error_errno(link
, r
, "Failed to save LLDP data to %s: %m", link
->lldp_file
);
1498 for (i
= 0; i
< n
; i
++)
1499 sd_lldp_neighbor_unref(l
[i
]);
1506 static void lldp_handler(sd_lldp
*lldp
, sd_lldp_event event
, sd_lldp_neighbor
*n
, void *userdata
) {
1507 Link
*link
= userdata
;
1512 (void) link_lldp_save(link
);
1514 if (link_lldp_emit_enabled(link
) && event
== SD_LLDP_EVENT_ADDED
) {
1515 /* If we received information about a new neighbor, restart the LLDP "fast" logic */
1517 log_link_debug(link
, "Received LLDP datagram from previously unknown neighbor, restarting 'fast' LLDP transmission.");
1519 r
= link_lldp_emit_start(link
);
1521 log_link_warning_errno(link
, r
, "Failed to restart LLDP transmission: %m");
1525 static int link_acquire_ipv6_conf(Link
*link
) {
1530 if (link_dhcp6_enabled(link
)) {
1531 assert(link
->dhcp6_client
);
1532 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1534 /* start DHCPv6 client in stateless mode */
1535 r
= dhcp6_request_address(link
, true);
1536 if (r
< 0 && r
!= -EBUSY
)
1537 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv6 lease: %m");
1539 log_link_debug(link
, "Acquiring DHCPv6 lease");
1542 if (link_ipv6_accept_ra_enabled(link
)) {
1543 assert(link
->ndisc
);
1545 log_link_debug(link
, "Discovering IPv6 routers");
1547 r
= sd_ndisc_start(link
->ndisc
);
1548 if (r
< 0 && r
!= -EBUSY
)
1549 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1555 static int link_acquire_ipv4_conf(Link
*link
) {
1559 assert(link
->network
);
1560 assert(link
->manager
);
1561 assert(link
->manager
->event
);
1563 if (link_ipv4ll_enabled(link
)) {
1564 assert(link
->ipv4ll
);
1566 log_link_debug(link
, "Acquiring IPv4 link-local address");
1568 r
= sd_ipv4ll_start(link
->ipv4ll
);
1570 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1573 if (link_dhcp4_enabled(link
)) {
1574 assert(link
->dhcp_client
);
1576 log_link_debug(link
, "Acquiring DHCPv4 lease");
1578 r
= sd_dhcp_client_start(link
->dhcp_client
);
1580 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1586 static int link_acquire_conf(Link
*link
) {
1591 r
= link_acquire_ipv4_conf(link
);
1595 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) == 0) {
1596 r
= link_acquire_ipv6_conf(link
);
1601 if (link_lldp_emit_enabled(link
)) {
1602 r
= link_lldp_emit_start(link
);
1604 return log_link_warning_errno(link
, r
, "Failed to start LLDP transmission: %m");
1610 bool link_has_carrier(Link
*link
) {
1611 /* see Documentation/networking/operstates.txt in the kernel sources */
1613 if (link
->kernel_operstate
== IF_OPER_UP
)
1616 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1617 /* operstate may not be implemented, so fall back to flags */
1618 if ((link
->flags
& IFF_LOWER_UP
) && !(link
->flags
& IFF_DORMANT
))
1624 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1625 _cleanup_link_unref_ Link
*link
= userdata
;
1630 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1633 r
= sd_netlink_message_get_errno(m
);
1635 /* we warn but don't fail the link, as it may be
1637 log_link_warning_errno(link
, r
, "Could not bring up interface: %m");
1642 static int link_up(Link
*link
) {
1643 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1644 uint8_t ipv6ll_mode
;
1648 assert(link
->network
);
1649 assert(link
->manager
);
1650 assert(link
->manager
->rtnl
);
1652 log_link_debug(link
, "Bringing link up");
1654 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1656 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1658 /* set it free if not enslaved with networkd */
1659 if (!link
->network
->bridge
&& !link
->network
->bond
&& !link
->network
->vrf
) {
1660 r
= sd_netlink_message_append_u32(req
, IFLA_MASTER
, 0);
1662 return log_link_error_errno(link
, r
, "Could not append IFLA_MASTER attribute: %m");
1665 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1667 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1669 if (link
->network
->mac
) {
1670 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
1672 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
1675 /* If IPv6 not configured (no static IPv6 address and IPv6LL autoconfiguration is disabled)
1676 for this interface, or if it is a bridge slave, then disable IPv6 else enable it. */
1677 (void) link_enable_ipv6(link
);
1679 if (link
->network
->mtu
) {
1680 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
1681 on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */
1682 if (link_ipv6_enabled(link
) && link
->network
->mtu
< IPV6_MIN_MTU
) {
1684 log_link_warning(link
, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU
) ", as "
1685 "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU
) " bytes: %m");
1687 link
->network
->mtu
= IPV6_MIN_MTU
;
1690 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, link
->network
->mtu
);
1692 return log_link_error_errno(link
, r
, "Could not set MTU: %m");
1695 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1697 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1699 if (link_ipv6_enabled(link
)) {
1700 /* if the kernel lacks ipv6 support setting IFF_UP fails if any ipv6 options are passed */
1701 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1703 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1705 if (!link_ipv6ll_enabled(link
))
1706 ipv6ll_mode
= IN6_ADDR_GEN_MODE_NONE
;
1708 const char *p
= NULL
;
1709 _cleanup_free_
char *stable_secret
= NULL
;
1711 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/stable_secret");
1712 r
= read_one_line_file(p
, &stable_secret
);
1715 ipv6ll_mode
= IN6_ADDR_GEN_MODE_EUI64
;
1717 ipv6ll_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
1719 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1721 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1723 if (!in_addr_is_null(AF_INET6
, &link
->network
->ipv6_token
)) {
1724 r
= sd_netlink_message_append_in6_addr(req
, IFLA_INET6_TOKEN
, &link
->network
->ipv6_token
.in6
);
1726 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_TOKEN: %m");
1729 r
= sd_netlink_message_close_container(req
);
1731 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1734 r
= sd_netlink_message_close_container(req
);
1736 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1738 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_up_handler
, link
, 0, NULL
);
1740 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1747 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1748 _cleanup_link_unref_ Link
*link
= userdata
;
1753 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1756 r
= sd_netlink_message_get_errno(m
);
1758 log_link_warning_errno(link
, r
, "Could not bring down interface: %m");
1763 static int link_down(Link
*link
) {
1764 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1768 assert(link
->manager
);
1769 assert(link
->manager
->rtnl
);
1771 log_link_debug(link
, "Bringing link down");
1773 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
1774 RTM_SETLINK
, link
->ifindex
);
1776 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1778 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
1780 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1782 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_down_handler
, link
, 0, NULL
);
1784 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1791 static int link_up_can(Link
*link
) {
1792 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1797 log_link_debug(link
, "Bringing CAN link up");
1799 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1801 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1803 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1805 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1807 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_up_handler
, link
, 0, NULL
);
1809 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1816 static int link_handle_bound_to_list(Link
*link
) {
1820 bool required_up
= false;
1821 bool link_is_up
= false;
1825 if (hashmap_isempty(link
->bound_to_links
))
1828 if (link
->flags
& IFF_UP
)
1831 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
1832 if (link_has_carrier(l
)) {
1837 if (!required_up
&& link_is_up
) {
1838 r
= link_down(link
);
1841 } else if (required_up
&& !link_is_up
) {
1850 static int link_handle_bound_by_list(Link
*link
) {
1857 if (hashmap_isempty(link
->bound_by_links
))
1860 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
1861 r
= link_handle_bound_to_list(l
);
1869 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
1875 if (link
== carrier
)
1878 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
1881 r
= hashmap_ensure_allocated(h
, NULL
);
1885 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
1892 static int link_new_bound_by_list(Link
*link
) {
1897 bool list_updated
= false;
1900 assert(link
->manager
);
1904 HASHMAP_FOREACH(carrier
, m
->links
, i
) {
1905 if (!carrier
->network
)
1908 if (strv_isempty(carrier
->network
->bind_carrier
))
1911 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
, 0)) {
1912 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
1916 list_updated
= true;
1923 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
1924 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
1928 link_dirty(carrier
);
1934 static int link_new_bound_to_list(Link
*link
) {
1939 bool list_updated
= false;
1942 assert(link
->manager
);
1947 if (strv_isempty(link
->network
->bind_carrier
))
1952 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
1953 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
, 0)) {
1954 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
1958 list_updated
= true;
1965 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
1966 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
1970 link_dirty(carrier
);
1976 static int link_new_carrier_maps(Link
*link
) {
1979 r
= link_new_bound_by_list(link
);
1983 r
= link_handle_bound_by_list(link
);
1987 r
= link_new_bound_to_list(link
);
1991 r
= link_handle_bound_to_list(link
);
1998 static void link_free_bound_to_list(Link
*link
) {
2002 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
2003 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
2005 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
2006 link_dirty(bound_to
);
2012 static void link_free_bound_by_list(Link
*link
) {
2016 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
2017 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
2019 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
2020 link_dirty(bound_by
);
2021 link_handle_bound_to_list(bound_by
);
2028 static void link_free_carrier_maps(Link
*link
) {
2029 bool list_updated
= false;
2033 if (!hashmap_isempty(link
->bound_to_links
)) {
2034 link_free_bound_to_list(link
);
2035 list_updated
= true;
2038 if (!hashmap_isempty(link
->bound_by_links
)) {
2039 link_free_bound_by_list(link
);
2040 list_updated
= true;
2049 void link_drop(Link
*link
) {
2050 if (!link
|| link
->state
== LINK_STATE_LINGER
)
2053 link_set_state(link
, LINK_STATE_LINGER
);
2055 link_free_carrier_maps(link
);
2057 log_link_debug(link
, "Link removed");
2059 (void)unlink(link
->state_file
);
2065 static int link_joined(Link
*link
) {
2069 assert(link
->network
);
2071 if (!hashmap_isempty(link
->bound_to_links
)) {
2072 r
= link_handle_bound_to_list(link
);
2075 } else if (!(link
->flags
& IFF_UP
)) {
2078 link_enter_failed(link
);
2083 if (link
->network
->bridge
) {
2084 r
= link_set_bridge(link
);
2086 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
2089 if (link
->network
->use_br_vlan
&&
2090 (link
->network
->bridge
|| streq_ptr("bridge", link
->kind
))) {
2091 r
= link_set_bridge_vlan(link
);
2093 log_link_error_errno(link
, r
, "Could not set bridge vlan: %m");
2096 return link_enter_set_addresses(link
);
2099 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
2100 _cleanup_link_unref_ Link
*link
= userdata
;
2104 assert(link
->network
);
2108 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2111 r
= sd_netlink_message_get_errno(m
);
2112 if (r
< 0 && r
!= -EEXIST
) {
2113 log_link_error_errno(link
, r
, "Could not join netdev: %m");
2114 link_enter_failed(link
);
2117 log_link_debug(link
, "Joined netdev");
2119 if (link
->enslaving
<= 0)
2125 static int link_enter_join_netdev(Link
*link
) {
2131 assert(link
->network
);
2132 assert(link
->state
== LINK_STATE_PENDING
);
2134 link_set_state(link
, LINK_STATE_ENSLAVING
);
2138 if (!link
->network
->bridge
&&
2139 !link
->network
->bond
&&
2140 !link
->network
->vrf
&&
2141 hashmap_isempty(link
->network
->stacked_netdevs
))
2142 return link_joined(link
);
2144 if (link
->network
->bond
) {
2145 log_struct(LOG_DEBUG
,
2146 LOG_LINK_INTERFACE(link
),
2147 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2148 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
),
2151 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
2153 log_struct_errno(LOG_WARNING
, r
,
2154 LOG_LINK_INTERFACE(link
),
2155 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2156 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
),
2159 link_enter_failed(link
);
2166 if (link
->network
->bridge
) {
2167 log_struct(LOG_DEBUG
,
2168 LOG_LINK_INTERFACE(link
),
2169 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2170 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
),
2173 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
2175 log_struct_errno(LOG_WARNING
, r
,
2176 LOG_LINK_INTERFACE(link
),
2177 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2178 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
),
2180 link_enter_failed(link
);
2187 if (link
->network
->vrf
) {
2188 log_struct(LOG_DEBUG
,
2189 LOG_LINK_INTERFACE(link
),
2190 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2191 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->vrf
->ifname
),
2193 r
= netdev_join(link
->network
->vrf
, link
, netdev_join_handler
);
2195 log_struct_errno(LOG_WARNING
, r
,
2196 LOG_LINK_INTERFACE(link
),
2197 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2198 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->vrf
->ifname
),
2200 link_enter_failed(link
);
2207 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
2209 log_struct(LOG_DEBUG
,
2210 LOG_LINK_INTERFACE(link
),
2211 LOG_NETDEV_INTERFACE(netdev
),
2212 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
),
2215 r
= netdev_join(netdev
, link
, netdev_join_handler
);
2217 log_struct_errno(LOG_WARNING
, r
,
2218 LOG_LINK_INTERFACE(link
),
2219 LOG_NETDEV_INTERFACE(netdev
),
2220 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
),
2222 link_enter_failed(link
);
2232 static int link_set_ipv4_forward(Link
*link
) {
2235 if (!link_ipv4_forward_enabled(link
))
2238 /* We propagate the forwarding flag from one interface to the
2239 * global setting one way. This means: as long as at least one
2240 * interface was configured at any time that had IP forwarding
2241 * enabled the setting will stay on for good. We do this
2242 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
2243 * somewhat in sync (see below). */
2245 r
= write_string_file("/proc/sys/net/ipv4/ip_forward", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2247 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
2252 static int link_set_ipv6_forward(Link
*link
) {
2255 if (!link_ipv6_forward_enabled(link
))
2258 /* On Linux, the IPv6 stack does not know a per-interface
2259 * packet forwarding setting: either packet forwarding is on
2260 * for all, or off for all. We hence don't bother with a
2261 * per-interface setting, but simply propagate the interface
2262 * flag, if it is set, to the global flag, one-way. Note that
2263 * while IPv4 would allow a per-interface flag, we expose the
2264 * same behaviour there and also propagate the setting from
2265 * one to all, to keep things simple (see above). */
2267 r
= write_string_file("/proc/sys/net/ipv6/conf/all/forwarding", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2269 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
2274 static int link_set_ipv6_privacy_extensions(Link
*link
) {
2275 char buf
[DECIMAL_STR_MAX(unsigned) + 1];
2276 IPv6PrivacyExtensions s
;
2277 const char *p
= NULL
;
2280 s
= link_ipv6_privacy_extensions(link
);
2284 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/use_tempaddr");
2285 xsprintf(buf
, "%u", (unsigned) link
->network
->ipv6_privacy_extensions
);
2287 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2289 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
2294 static int link_set_ipv6_accept_ra(Link
*link
) {
2295 const char *p
= NULL
;
2298 /* Make this a NOP if IPv6 is not available */
2299 if (!socket_ipv6_is_supported())
2302 if (link
->flags
& IFF_LOOPBACK
)
2308 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/accept_ra");
2310 /* We handle router advertisements ourselves, tell the kernel to GTFO */
2311 r
= write_string_file(p
, "0", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2313 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
2318 static int link_set_ipv6_dad_transmits(Link
*link
) {
2319 char buf
[DECIMAL_STR_MAX(int) + 1];
2320 const char *p
= NULL
;
2323 /* Make this a NOP if IPv6 is not available */
2324 if (!socket_ipv6_is_supported())
2327 if (link
->flags
& IFF_LOOPBACK
)
2333 if (link
->network
->ipv6_dad_transmits
< 0)
2336 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/dad_transmits");
2337 xsprintf(buf
, "%i", link
->network
->ipv6_dad_transmits
);
2339 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2341 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
2346 static int link_set_ipv6_hop_limit(Link
*link
) {
2347 char buf
[DECIMAL_STR_MAX(int) + 1];
2348 const char *p
= NULL
;
2351 /* Make this a NOP if IPv6 is not available */
2352 if (!socket_ipv6_is_supported())
2355 if (link
->flags
& IFF_LOOPBACK
)
2361 if (link
->network
->ipv6_hop_limit
< 0)
2364 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/hop_limit");
2365 xsprintf(buf
, "%i", link
->network
->ipv6_hop_limit
);
2367 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2369 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2374 static int link_drop_foreign_config(Link
*link
) {
2380 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
2381 /* we consider IPv6LL addresses to be managed by the kernel */
2382 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2385 r
= address_remove(address
, link
, link_address_remove_handler
);
2390 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2391 /* do not touch routes managed by the kernel */
2392 if (route
->protocol
== RTPROT_KERNEL
)
2395 r
= route_remove(route
, link
, link_route_remove_handler
);
2403 static int link_drop_config(Link
*link
) {
2409 SET_FOREACH(address
, link
->addresses
, i
) {
2410 /* we consider IPv6LL addresses to be managed by the kernel */
2411 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2414 r
= address_remove(address
, link
, link_address_remove_handler
);
2419 SET_FOREACH(route
, link
->routes
, i
) {
2420 /* do not touch routes managed by the kernel */
2421 if (route
->protocol
== RTPROT_KERNEL
)
2424 r
= route_remove(route
, link
, link_route_remove_handler
);
2434 static int link_update_lldp(Link
*link
) {
2442 if (link
->flags
& IFF_UP
) {
2443 r
= sd_lldp_start(link
->lldp
);
2445 log_link_debug(link
, "Started LLDP.");
2447 r
= sd_lldp_stop(link
->lldp
);
2449 log_link_debug(link
, "Stopped LLDP.");
2455 static int link_configure(Link
*link
) {
2459 assert(link
->network
);
2460 assert(link
->state
== LINK_STATE_PENDING
);
2462 if (streq_ptr(link
->kind
, "vcan")) {
2464 if (!(link
->flags
& IFF_UP
)) {
2465 r
= link_up_can(link
);
2467 link_enter_failed(link
);
2475 /* Drop foreign config, but ignore loopback or critical devices.
2476 * We do not want to remove loopback address or addresses used for root NFS. */
2477 if (!(link
->flags
& IFF_LOOPBACK
) && !(link
->network
->dhcp_critical
)) {
2478 r
= link_drop_foreign_config(link
);
2483 r
= link_set_proxy_arp(link
);
2487 r
= link_set_ipv4_forward(link
);
2491 r
= link_set_ipv6_forward(link
);
2495 r
= link_set_ipv6_privacy_extensions(link
);
2499 r
= link_set_ipv6_accept_ra(link
);
2503 r
= link_set_ipv6_dad_transmits(link
);
2507 r
= link_set_ipv6_hop_limit(link
);
2511 r
= link_set_flags(link
);
2515 if (link_ipv4ll_enabled(link
)) {
2516 r
= ipv4ll_configure(link
);
2521 if (link_dhcp4_enabled(link
)) {
2522 r
= dhcp4_configure(link
);
2527 if (link_dhcp4_server_enabled(link
)) {
2528 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
2532 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
2537 if (link_dhcp6_enabled(link
) ||
2538 link_ipv6_accept_ra_enabled(link
)) {
2539 r
= dhcp6_configure(link
);
2544 if (link_ipv6_accept_ra_enabled(link
)) {
2545 r
= ndisc_configure(link
);
2550 if (link_lldp_rx_enabled(link
)) {
2551 r
= sd_lldp_new(&link
->lldp
);
2555 r
= sd_lldp_set_ifindex(link
->lldp
, link
->ifindex
);
2559 r
= sd_lldp_match_capabilities(link
->lldp
,
2560 link
->network
->lldp_mode
== LLDP_MODE_ROUTERS_ONLY
?
2561 SD_LLDP_SYSTEM_CAPABILITIES_ALL_ROUTERS
:
2562 SD_LLDP_SYSTEM_CAPABILITIES_ALL
);
2566 r
= sd_lldp_set_filter_address(link
->lldp
, &link
->mac
);
2570 r
= sd_lldp_attach_event(link
->lldp
, NULL
, 0);
2574 r
= sd_lldp_set_callback(link
->lldp
, lldp_handler
, link
);
2578 r
= link_update_lldp(link
);
2583 if (link_has_carrier(link
)) {
2584 r
= link_acquire_conf(link
);
2589 return link_enter_join_netdev(link
);
2592 static int link_initialized_and_synced(sd_netlink
*rtnl
, sd_netlink_message
*m
,
2594 _cleanup_link_unref_ Link
*link
= userdata
;
2599 assert(link
->ifname
);
2600 assert(link
->manager
);
2602 if (link
->state
!= LINK_STATE_PENDING
)
2605 log_link_debug(link
, "Link state is up-to-date");
2607 r
= link_new_bound_by_list(link
);
2611 r
= link_handle_bound_by_list(link
);
2615 if (!link
->network
) {
2616 r
= network_get(link
->manager
, link
->udev_device
, link
->ifname
,
2617 &link
->mac
, &network
);
2619 link_enter_unmanaged(link
);
2624 if (link
->flags
& IFF_LOOPBACK
) {
2625 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
2626 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
2628 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
2629 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
2631 if (network
->dhcp_server
)
2632 log_link_debug(link
, "Ignoring DHCP server for loopback link");
2635 r
= network_apply(network
, link
);
2640 r
= link_new_bound_to_list(link
);
2644 r
= link_configure(link
);
2651 int link_initialized(Link
*link
, struct udev_device
*device
) {
2652 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2656 assert(link
->manager
);
2657 assert(link
->manager
->rtnl
);
2660 if (link
->state
!= LINK_STATE_PENDING
)
2663 if (link
->udev_device
)
2666 log_link_debug(link
, "udev initialized link");
2668 link
->udev_device
= udev_device_ref(device
);
2670 /* udev has initialized the link, but we don't know if we have yet
2671 * processed the NEWLINK messages with the latest state. Do a GETLINK,
2672 * when it returns we know that the pending NEWLINKs have already been
2673 * processed and that we are up-to-date */
2675 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
2680 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
,
2681 link_initialized_and_synced
, link
, 0, NULL
);
2690 static int link_load(Link
*link
) {
2691 _cleanup_free_
char *network_file
= NULL
,
2694 *dhcp4_address
= NULL
,
2695 *ipv4ll_address
= NULL
;
2696 union in_addr_union address
;
2697 union in_addr_union route_dst
;
2703 r
= parse_env_file(link
->state_file
, NEWLINE
,
2704 "NETWORK_FILE", &network_file
,
2705 "ADDRESSES", &addresses
,
2707 "DHCP4_ADDRESS", &dhcp4_address
,
2708 "IPV4LL_ADDRESS", &ipv4ll_address
,
2710 if (r
< 0 && r
!= -ENOENT
)
2711 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
2718 suffix
= strrchr(network_file
, '.');
2720 log_link_debug(link
, "Failed to get network name from %s", network_file
);
2721 goto network_file_fail
;
2725 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
2727 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
2728 goto network_file_fail
;
2731 r
= network_apply(network
, link
);
2733 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
2742 _cleanup_free_
char *address_str
= NULL
;
2743 char *prefixlen_str
;
2745 unsigned char prefixlen
;
2747 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
2749 log_link_debug_errno(link
, r
, "Failed to extract next address string: %m");
2755 prefixlen_str
= strchr(address_str
, '/');
2756 if (!prefixlen_str
) {
2757 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
2761 *prefixlen_str
++ = '\0';
2763 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
2765 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
2769 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
2771 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
2775 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
2777 return log_link_error_errno(link
, r
, "Failed to add address: %m");
2786 _cleanup_free_
char *route_str
= NULL
;
2787 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
2789 char *prefixlen_str
;
2791 unsigned char prefixlen
, tos
, table
;
2794 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
2796 log_link_debug_errno(link
, r
, "Failed to extract next route string: %m");
2802 prefixlen_str
= strchr(route_str
, '/');
2803 if (!prefixlen_str
) {
2804 log_link_debug(link
, "Failed to parse route %s", route_str
);
2808 *prefixlen_str
++ = '\0';
2810 r
= sscanf(prefixlen_str
, "%hhu/%hhu/%"SCNu32
"/%hhu/"USEC_FMT
, &prefixlen
, &tos
, &priority
, &table
, &lifetime
);
2812 log_link_debug(link
,
2813 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
2818 r
= in_addr_from_string_auto(route_str
, &family
, &route_dst
);
2820 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
2824 r
= route_add(link
, family
, &route_dst
, prefixlen
, tos
, priority
, table
, &route
);
2826 return log_link_error_errno(link
, r
, "Failed to add route: %m");
2828 if (lifetime
!= USEC_INFINITY
) {
2829 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(), lifetime
,
2830 0, route_expire_handler
, route
);
2832 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
2835 route
->lifetime
= lifetime
;
2836 sd_event_source_unref(route
->expire
);
2837 route
->expire
= expire
;
2842 if (dhcp4_address
) {
2843 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
2845 log_link_debug_errno(link
, r
, "Failed to parse DHCPv4 address %s: %m", dhcp4_address
);
2846 goto dhcp4_address_fail
;
2849 r
= sd_dhcp_client_new(&link
->dhcp_client
);
2851 return log_link_error_errno(link
, r
, "Failed to create DHCPv4 client: %m");
2853 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
2855 return log_link_error_errno(link
, r
, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address
);
2860 if (ipv4ll_address
) {
2861 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
2863 log_link_debug_errno(link
, r
, "Failed to parse IPv4LL address %s: %m", ipv4ll_address
);
2864 goto ipv4ll_address_fail
;
2867 r
= sd_ipv4ll_new(&link
->ipv4ll
);
2869 return log_link_error_errno(link
, r
, "Failed to create IPv4LL client: %m");
2871 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
2873 return log_link_error_errno(link
, r
, "Failed to set initial IPv4LL address %s: %m", ipv4ll_address
);
2876 ipv4ll_address_fail
:
2881 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
2883 _cleanup_udev_device_unref_
struct udev_device
*device
= NULL
;
2884 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
2892 r
= link_new(m
, message
, ret
);
2898 log_link_debug(link
, "Link %d added", link
->ifindex
);
2900 r
= link_load(link
);
2904 if (detect_container() <= 0) {
2905 /* not in a container, udev will be around */
2906 sprintf(ifindex_str
, "n%d", link
->ifindex
);
2907 device
= udev_device_new_from_device_id(m
->udev
, ifindex_str
);
2909 r
= log_link_warning_errno(link
, errno
, "Could not find udev device: %m");
2913 if (udev_device_get_is_initialized(device
) <= 0) {
2915 log_link_debug(link
, "link pending udev initialization...");
2919 r
= link_initialized(link
, device
);
2923 /* we are calling a callback directly, so must take a ref */
2926 r
= link_initialized_and_synced(m
->rtnl
, NULL
, link
);
2933 link_enter_failed(link
);
2937 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
2942 log_link_info(link
, "Gained IPv6LL");
2944 link
->ipv6ll_address
= *address
;
2945 link_check_ready(link
);
2947 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
2948 r
= link_acquire_ipv6_conf(link
);
2950 link_enter_failed(link
);
2958 static int link_carrier_gained(Link
*link
) {
2963 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
2964 r
= link_acquire_conf(link
);
2966 link_enter_failed(link
);
2970 r
= link_enter_set_addresses(link
);
2975 r
= link_handle_bound_by_list(link
);
2982 static int link_carrier_lost(Link
*link
) {
2987 r
= link_stop_clients(link
);
2989 link_enter_failed(link
);
2993 r
= link_drop_config(link
);
2997 if (!IN_SET(link
->state
, LINK_STATE_UNMANAGED
, LINK_STATE_PENDING
)) {
2998 log_link_debug(link
, "State is %s, dropping config", link_state_to_string(link
->state
));
2999 r
= link_drop_foreign_config(link
);
3004 r
= link_handle_bound_by_list(link
);
3011 int link_carrier_reset(Link
*link
) {
3016 if (link_has_carrier(link
)) {
3017 r
= link_carrier_lost(link
);
3021 r
= link_carrier_gained(link
);
3025 log_link_info(link
, "Reset carrier");
3031 int link_update(Link
*link
, sd_netlink_message
*m
) {
3032 struct ether_addr mac
;
3035 bool had_carrier
, carrier_gained
, carrier_lost
;
3039 assert(link
->ifname
);
3042 if (link
->state
== LINK_STATE_LINGER
) {
3044 log_link_info(link
, "Link readded");
3045 link_set_state(link
, LINK_STATE_ENSLAVING
);
3047 r
= link_new_carrier_maps(link
);
3052 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
3053 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
3054 log_link_info(link
, "Renamed to %s", ifname
);
3056 link_free_carrier_maps(link
);
3058 r
= free_and_strdup(&link
->ifname
, ifname
);
3062 r
= link_new_carrier_maps(link
);
3067 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
3068 if (r
>= 0 && mtu
> 0) {
3070 if (!link
->original_mtu
) {
3071 link
->original_mtu
= mtu
;
3072 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
3075 if (link
->dhcp_client
) {
3076 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
3079 log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
3085 /* The kernel may broadcast NEWLINK messages without the MAC address
3086 set, simply ignore them. */
3087 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
3089 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3092 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3095 log_link_debug(link
, "MAC address: "
3096 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
3097 mac
.ether_addr_octet
[0],
3098 mac
.ether_addr_octet
[1],
3099 mac
.ether_addr_octet
[2],
3100 mac
.ether_addr_octet
[3],
3101 mac
.ether_addr_octet
[4],
3102 mac
.ether_addr_octet
[5]);
3105 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
3107 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
3110 if (link
->dhcp_client
) {
3111 const DUID
*duid
= link_duid(link
);
3113 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
3114 (const uint8_t *) &link
->mac
,
3118 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
3120 r
= sd_dhcp_client_set_iaid_duid(link
->dhcp_client
,
3121 link
->network
->iaid
,
3123 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3124 duid
->raw_data_len
);
3126 return log_link_warning_errno(link
, r
, "Could not update DUID/IAID in DHCP client: %m");
3129 if (link
->dhcp6_client
) {
3130 const DUID
* duid
= link_duid(link
);
3132 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
3133 (const uint8_t *) &link
->mac
,
3137 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
3139 r
= sd_dhcp6_client_set_iaid(link
->dhcp6_client
,
3140 link
->network
->iaid
);
3142 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 IAID: %m");
3144 r
= sd_dhcp6_client_set_duid(link
->dhcp6_client
,
3146 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3147 duid
->raw_data_len
);
3149 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 DUID: %m");
3154 had_carrier
= link_has_carrier(link
);
3156 r
= link_update_flags(link
, m
);
3160 r
= link_update_lldp(link
);
3164 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
3165 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
3167 if (carrier_gained
) {
3168 log_link_info(link
, "Gained carrier");
3170 r
= link_carrier_gained(link
);
3173 } else if (carrier_lost
) {
3174 log_link_info(link
, "Lost carrier");
3176 r
= link_carrier_lost(link
);
3184 static void print_link_hashmap(FILE *f
, const char *prefix
, Hashmap
* h
) {
3192 if (hashmap_isempty(h
))
3196 HASHMAP_FOREACH(link
, h
, i
) {
3200 fprintf(f
, "%i", link
->ifindex
);
3207 int link_save(Link
*link
) {
3208 _cleanup_free_
char *temp_path
= NULL
;
3209 _cleanup_fclose_
FILE *f
= NULL
;
3210 const char *admin_state
, *oper_state
;
3217 assert(link
->state_file
);
3218 assert(link
->lease_file
);
3219 assert(link
->manager
);
3221 if (link
->state
== LINK_STATE_LINGER
) {
3222 unlink(link
->state_file
);
3226 link_lldp_save(link
);
3228 admin_state
= link_state_to_string(link
->state
);
3229 assert(admin_state
);
3231 oper_state
= link_operstate_to_string(link
->operstate
);
3234 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
3238 fchmod(fileno(f
), 0644);
3241 "# This is private data. Do not parse.\n"
3244 admin_state
, oper_state
);
3246 if (link
->network
) {
3248 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
3249 const char *dhcp_domainname
= NULL
;
3250 char **dhcp6_domains
= NULL
;
3252 if (link
->dhcp6_client
) {
3253 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
, &dhcp6_lease
);
3254 if (r
< 0 && r
!= -ENOMSG
)
3255 log_link_debug(link
, "No DHCPv6 lease");
3258 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
3262 fputstrv(f
, link
->network
->dns
, NULL
, &space
);
3264 if (link
->network
->dhcp_use_dns
&&
3266 const struct in_addr
*addresses
;
3268 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
3272 serialize_in_addrs(f
, addresses
, r
);
3277 if (link
->network
->dhcp_use_dns
&& dhcp6_lease
) {
3278 struct in6_addr
*in6_addrs
;
3280 r
= sd_dhcp6_lease_get_dns(dhcp6_lease
, &in6_addrs
);
3284 serialize_in6_addrs(f
, in6_addrs
, r
);
3289 /* Make sure to flush out old entries before we use the NDISC data */
3292 if (link
->network
->dhcp_use_dns
&& link
->ndisc_rdnss
) {
3295 SET_FOREACH(dd
, link
->ndisc_rdnss
, i
) {
3299 serialize_in6_addrs(f
, &dd
->address
, 1);
3308 fputstrv(f
, link
->network
->ntp
, NULL
, &space
);
3310 if (link
->network
->dhcp_use_ntp
&&
3312 const struct in_addr
*addresses
;
3314 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
3318 serialize_in_addrs(f
, addresses
, r
);
3323 if (link
->network
->dhcp_use_ntp
&& dhcp6_lease
) {
3324 struct in6_addr
*in6_addrs
;
3327 r
= sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease
,
3332 serialize_in6_addrs(f
, in6_addrs
, r
);
3336 r
= sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease
, &hosts
);
3338 fputstrv(f
, hosts
, NULL
, &space
);
3343 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
3344 if (link
->dhcp_lease
)
3345 (void) sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &dhcp_domainname
);
3347 (void) sd_dhcp6_lease_get_domains(dhcp6_lease
, &dhcp6_domains
);
3350 fputs("DOMAINS=", f
);
3351 fputstrv(f
, link
->network
->search_domains
, NULL
, &space
);
3353 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
) {
3356 if (dhcp_domainname
)
3357 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3359 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3361 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3362 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3367 fputs("ROUTE_DOMAINS=", f
);
3368 fputstrv(f
, link
->network
->route_domains
, NULL
, NULL
);
3370 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
) {
3373 if (dhcp_domainname
)
3374 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3376 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3378 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3379 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3384 fprintf(f
, "LLMNR=%s\n",
3385 resolve_support_to_string(link
->network
->llmnr
));
3386 fprintf(f
, "MDNS=%s\n",
3387 resolve_support_to_string(link
->network
->mdns
));
3389 if (link
->network
->dnssec_mode
!= _DNSSEC_MODE_INVALID
)
3390 fprintf(f
, "DNSSEC=%s\n",
3391 dnssec_mode_to_string(link
->network
->dnssec_mode
));
3393 if (!set_isempty(link
->network
->dnssec_negative_trust_anchors
)) {
3396 fputs("DNSSEC_NTA=", f
);
3398 SET_FOREACH(n
, link
->network
->dnssec_negative_trust_anchors
, i
)
3399 fputs_with_space(f
, n
, NULL
, &space
);
3403 fputs("ADDRESSES=", f
);
3405 SET_FOREACH(a
, link
->addresses
, i
) {
3406 _cleanup_free_
char *address_str
= NULL
;
3408 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
3412 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
3417 fputs("ROUTES=", f
);
3419 SET_FOREACH(route
, link
->routes
, i
) {
3420 _cleanup_free_
char *route_str
= NULL
;
3422 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
3426 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%hhu/"USEC_FMT
, space
? " " : "", route_str
,
3427 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
3434 print_link_hashmap(f
, "CARRIER_BOUND_TO=", link
->bound_to_links
);
3435 print_link_hashmap(f
, "CARRIER_BOUND_BY=", link
->bound_by_links
);
3437 if (link
->dhcp_lease
) {
3438 struct in_addr address
;
3439 const char *tz
= NULL
;
3441 assert(link
->network
);
3443 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
3445 fprintf(f
, "TIMEZONE=%s\n", tz
);
3447 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
3449 fputs("DHCP4_ADDRESS=", f
);
3450 serialize_in_addrs(f
, &address
, 1);
3454 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
3462 unlink(link
->lease_file
);
3465 struct in_addr address
;
3467 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
3469 fputs("IPV4LL_ADDRESS=", f
);
3470 serialize_in_addrs(f
, &address
, 1);
3475 r
= fflush_and_check(f
);
3479 if (rename(temp_path
, link
->state_file
) < 0) {
3487 (void) unlink(link
->state_file
);
3489 (void) unlink(temp_path
);
3491 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
3494 /* The serialized state in /run is no longer up-to-date. */
3495 void link_dirty(Link
*link
) {
3500 /* mark manager dirty as link is dirty */
3501 manager_dirty(link
->manager
);
3503 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
3505 /* allocation errors are ignored */
3508 r
= set_put(link
->manager
->dirty_links
, link
);
3510 /* don't take another ref if the link was already dirty */
3516 /* The serialized state in /run is up-to-date */
3517 void link_clean(Link
*link
) {
3519 assert(link
->manager
);
3521 set_remove(link
->manager
->dirty_links
, link
);
3525 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
3526 [LINK_STATE_PENDING
] = "pending",
3527 [LINK_STATE_ENSLAVING
] = "configuring",
3528 [LINK_STATE_SETTING_ADDRESSES
] = "configuring",
3529 [LINK_STATE_SETTING_ROUTES
] = "configuring",
3530 [LINK_STATE_CONFIGURED
] = "configured",
3531 [LINK_STATE_UNMANAGED
] = "unmanaged",
3532 [LINK_STATE_FAILED
] = "failed",
3533 [LINK_STATE_LINGER
] = "linger",
3536 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);
3538 static const char* const link_operstate_table
[_LINK_OPERSTATE_MAX
] = {
3539 [LINK_OPERSTATE_OFF
] = "off",
3540 [LINK_OPERSTATE_NO_CARRIER
] = "no-carrier",
3541 [LINK_OPERSTATE_DORMANT
] = "dormant",
3542 [LINK_OPERSTATE_CARRIER
] = "carrier",
3543 [LINK_OPERSTATE_DEGRADED
] = "degraded",
3544 [LINK_OPERSTATE_ROUTABLE
] = "routable",
3547 DEFINE_STRING_TABLE_LOOKUP(link_operstate
, LinkOperationalState
);