2 This file is part of systemd.
4 Copyright 2013 Tom Gundersen <teg@jklm.no>
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 #include <netinet/ether.h>
24 #include "alloc-util.h"
26 #include "dhcp-lease-internal.h"
29 #include "netlink-util.h"
30 #include "network-internal.h"
32 #include "networkd-lldp-tx.h"
34 #include "socket-util.h"
35 #include "stdio-util.h"
36 #include "string-table.h"
37 #include "udev-util.h"
41 static bool link_dhcp6_enabled(Link
*link
) {
44 if (!socket_ipv6_is_supported())
47 if (link
->flags
& IFF_LOOPBACK
)
53 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV6
;
56 static bool link_dhcp4_enabled(Link
*link
) {
59 if (link
->flags
& IFF_LOOPBACK
)
65 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV4
;
68 static bool link_dhcp4_server_enabled(Link
*link
) {
71 if (link
->flags
& IFF_LOOPBACK
)
77 return link
->network
->dhcp_server
;
80 static bool link_ipv4ll_enabled(Link
*link
) {
83 if (link
->flags
& IFF_LOOPBACK
)
89 return link
->network
->link_local
& ADDRESS_FAMILY_IPV4
;
92 static bool link_ipv6ll_enabled(Link
*link
) {
95 if (!socket_ipv6_is_supported())
98 if (link
->flags
& IFF_LOOPBACK
)
104 return link
->network
->link_local
& ADDRESS_FAMILY_IPV6
;
107 static bool link_ipv6_enabled(Link
*link
) {
110 if (!socket_ipv6_is_supported())
113 if (link
->network
->bridge
)
116 /* DHCPv6 client will not be started if no IPv6 link-local address is configured. */
117 return link_ipv6ll_enabled(link
) || network_has_static_ipv6_addresses(link
->network
);
120 static bool link_lldp_rx_enabled(Link
*link
) {
123 if (link
->flags
& IFF_LOOPBACK
)
126 if (link
->iftype
!= ARPHRD_ETHER
)
132 if (link
->network
->bridge
)
135 return link
->network
->lldp_mode
!= LLDP_MODE_NO
;
138 static bool link_lldp_emit_enabled(Link
*link
) {
141 if (link
->flags
& IFF_LOOPBACK
)
144 if (link
->iftype
!= ARPHRD_ETHER
)
150 return link
->network
->lldp_emit
!= LLDP_EMIT_NO
;
153 static bool link_ipv4_forward_enabled(Link
*link
) {
156 if (link
->flags
& IFF_LOOPBACK
)
162 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
165 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV4
;
168 static bool link_ipv6_forward_enabled(Link
*link
) {
171 if (!socket_ipv6_is_supported())
174 if (link
->flags
& IFF_LOOPBACK
)
180 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
183 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV6
;
186 static bool link_proxy_arp_enabled(Link
*link
) {
189 if (link
->flags
& IFF_LOOPBACK
)
195 if (link
->network
->proxy_arp
< 0)
201 static bool link_ipv6_accept_ra_enabled(Link
*link
) {
204 if (!socket_ipv6_is_supported())
207 if (link
->flags
& IFF_LOOPBACK
)
213 /* If unset use system default (enabled if local forwarding is disabled.
214 * disabled if local forwarding is enabled).
215 * If set, ignore or enforce RA independent of local forwarding state.
217 if (link
->network
->ipv6_accept_ra
< 0)
218 /* default to accept RA if ip_forward is disabled and ignore RA if ip_forward is enabled */
219 return !link_ipv6_forward_enabled(link
);
220 else if (link
->network
->ipv6_accept_ra
> 0)
221 /* accept RA even if ip_forward is enabled */
228 static IPv6PrivacyExtensions
link_ipv6_privacy_extensions(Link
*link
) {
231 if (!socket_ipv6_is_supported())
232 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
234 if (link
->flags
& IFF_LOOPBACK
)
235 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
238 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
240 return link
->network
->ipv6_privacy_extensions
;
243 static int link_enable_ipv6(Link
*link
) {
244 const char *p
= NULL
;
248 if (link
->flags
& IFF_LOOPBACK
)
251 disabled
= !link_ipv6_enabled(link
);
253 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/disable_ipv6");
255 r
= write_string_file(p
, one_zero(disabled
), WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
257 log_link_warning_errno(link
, r
, "Cannot %s IPv6 for interface %s: %m", disabled
? "disable" : "enable", link
->ifname
);
260 log_link_info(link
, "IPv6 disabled for interface: %m");
262 log_link_info(link
, "IPv6 enabled for interface: %m");
268 void link_update_operstate(Link
*link
) {
269 LinkOperationalState operstate
;
272 if (link
->kernel_operstate
== IF_OPER_DORMANT
)
273 operstate
= LINK_OPERSTATE_DORMANT
;
274 else if (link_has_carrier(link
)) {
276 uint8_t scope
= RT_SCOPE_NOWHERE
;
279 /* if we have carrier, check what addresses we have */
280 SET_FOREACH(address
, link
->addresses
, i
) {
281 if (!address_is_ready(address
))
284 if (address
->scope
< scope
)
285 scope
= address
->scope
;
288 /* for operstate we also take foreign addresses into account */
289 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
290 if (!address_is_ready(address
))
293 if (address
->scope
< scope
)
294 scope
= address
->scope
;
297 if (scope
< RT_SCOPE_SITE
)
298 /* universally accessible addresses found */
299 operstate
= LINK_OPERSTATE_ROUTABLE
;
300 else if (scope
< RT_SCOPE_HOST
)
301 /* only link or site local addresses found */
302 operstate
= LINK_OPERSTATE_DEGRADED
;
304 /* no useful addresses found */
305 operstate
= LINK_OPERSTATE_CARRIER
;
306 } else if (link
->flags
& IFF_UP
)
307 operstate
= LINK_OPERSTATE_NO_CARRIER
;
309 operstate
= LINK_OPERSTATE_OFF
;
311 if (link
->operstate
!= operstate
) {
312 link
->operstate
= operstate
;
313 link_send_changed(link
, "OperationalState", NULL
);
318 #define FLAG_STRING(string, flag, old, new) \
319 (((old ^ new) & flag) \
320 ? ((old & flag) ? (" -" string) : (" +" string)) \
323 static int link_update_flags(Link
*link
, sd_netlink_message
*m
) {
324 unsigned flags
, unknown_flags_added
, unknown_flags_removed
, unknown_flags
;
330 r
= sd_rtnl_message_link_get_flags(m
, &flags
);
332 return log_link_warning_errno(link
, r
, "Could not get link flags: %m");
334 r
= sd_netlink_message_read_u8(m
, IFLA_OPERSTATE
, &operstate
);
336 /* if we got a message without operstate, take it to mean
337 the state was unchanged */
338 operstate
= link
->kernel_operstate
;
340 if ((link
->flags
== flags
) && (link
->kernel_operstate
== operstate
))
343 if (link
->flags
!= flags
) {
344 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",
345 FLAG_STRING("LOOPBACK", IFF_LOOPBACK
, link
->flags
, flags
),
346 FLAG_STRING("MASTER", IFF_MASTER
, link
->flags
, flags
),
347 FLAG_STRING("SLAVE", IFF_SLAVE
, link
->flags
, flags
),
348 FLAG_STRING("UP", IFF_UP
, link
->flags
, flags
),
349 FLAG_STRING("DORMANT", IFF_DORMANT
, link
->flags
, flags
),
350 FLAG_STRING("LOWER_UP", IFF_LOWER_UP
, link
->flags
, flags
),
351 FLAG_STRING("RUNNING", IFF_RUNNING
, link
->flags
, flags
),
352 FLAG_STRING("MULTICAST", IFF_MULTICAST
, link
->flags
, flags
),
353 FLAG_STRING("BROADCAST", IFF_BROADCAST
, link
->flags
, flags
),
354 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT
, link
->flags
, flags
),
355 FLAG_STRING("PROMISC", IFF_PROMISC
, link
->flags
, flags
),
356 FLAG_STRING("ALLMULTI", IFF_ALLMULTI
, link
->flags
, flags
),
357 FLAG_STRING("PORTSEL", IFF_PORTSEL
, link
->flags
, flags
),
358 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA
, link
->flags
, flags
),
359 FLAG_STRING("DYNAMIC", IFF_DYNAMIC
, link
->flags
, flags
),
360 FLAG_STRING("NOARP", IFF_NOARP
, link
->flags
, flags
),
361 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS
, link
->flags
, flags
),
362 FLAG_STRING("DEBUG", IFF_DEBUG
, link
->flags
, flags
),
363 FLAG_STRING("ECHO", IFF_ECHO
, link
->flags
, flags
));
365 unknown_flags
= ~(IFF_LOOPBACK
| IFF_MASTER
| IFF_SLAVE
| IFF_UP
|
366 IFF_DORMANT
| IFF_LOWER_UP
| IFF_RUNNING
|
367 IFF_MULTICAST
| IFF_BROADCAST
| IFF_POINTOPOINT
|
368 IFF_PROMISC
| IFF_ALLMULTI
| IFF_PORTSEL
|
369 IFF_AUTOMEDIA
| IFF_DYNAMIC
| IFF_NOARP
|
370 IFF_NOTRAILERS
| IFF_DEBUG
| IFF_ECHO
);
371 unknown_flags_added
= ((link
->flags
^ flags
) & flags
& unknown_flags
);
372 unknown_flags_removed
= ((link
->flags
^ flags
) & link
->flags
& unknown_flags
);
374 /* link flags are currently at most 18 bits, let's align to
376 if (unknown_flags_added
)
378 "Unknown link flags gained: %#.5x (ignoring)",
379 unknown_flags_added
);
381 if (unknown_flags_removed
)
383 "Unknown link flags lost: %#.5x (ignoring)",
384 unknown_flags_removed
);
388 link
->kernel_operstate
= operstate
;
390 link_update_operstate(link
);
395 static int link_new(Manager
*manager
, sd_netlink_message
*message
, Link
**ret
) {
396 _cleanup_link_unref_ Link
*link
= NULL
;
400 unsigned short iftype
;
406 r
= sd_netlink_message_get_type(message
, &type
);
409 else if (type
!= RTM_NEWLINK
)
412 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
415 else if (ifindex
<= 0)
418 r
= sd_rtnl_message_link_get_type(message
, &iftype
);
422 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &ifname
);
426 link
= new0(Link
, 1);
431 link
->manager
= manager
;
432 link
->state
= LINK_STATE_PENDING
;
433 link
->rtnl_extended_attrs
= true;
434 link
->ifindex
= ifindex
;
435 link
->iftype
= iftype
;
436 link
->ifname
= strdup(ifname
);
440 r
= sd_netlink_message_read_ether_addr(message
, IFLA_ADDRESS
, &link
->mac
);
442 log_link_debug_errno(link
, r
, "MAC address not found for new device, continuing without");
444 if (asprintf(&link
->state_file
, "/run/systemd/netif/links/%d", link
->ifindex
) < 0)
447 if (asprintf(&link
->lease_file
, "/run/systemd/netif/leases/%d", link
->ifindex
) < 0)
450 if (asprintf(&link
->lldp_file
, "/run/systemd/netif/lldp/%d", link
->ifindex
) < 0)
453 r
= hashmap_ensure_allocated(&manager
->links
, NULL
);
457 r
= hashmap_put(manager
->links
, INT_TO_PTR(link
->ifindex
), link
);
461 r
= link_update_flags(link
, message
);
471 static void link_free(Link
*link
) {
479 while (!set_isempty(link
->addresses
))
480 address_free(set_first(link
->addresses
));
482 while (!set_isempty(link
->addresses_foreign
))
483 address_free(set_first(link
->addresses_foreign
));
485 link
->addresses
= set_free(link
->addresses
);
487 link
->addresses_foreign
= set_free(link
->addresses_foreign
);
489 while ((address
= link
->pool_addresses
)) {
490 LIST_REMOVE(addresses
, link
->pool_addresses
, address
);
491 address_free(address
);
494 sd_dhcp_server_unref(link
->dhcp_server
);
495 sd_dhcp_client_unref(link
->dhcp_client
);
496 sd_dhcp_lease_unref(link
->dhcp_lease
);
498 link_lldp_emit_stop(link
);
500 free(link
->lease_file
);
502 sd_lldp_unref(link
->lldp
);
503 free(link
->lldp_file
);
505 sd_ipv4ll_unref(link
->ipv4ll
);
506 sd_dhcp6_client_unref(link
->dhcp6_client
);
507 sd_ndisc_unref(link
->ndisc_router_discovery
);
510 hashmap_remove(link
->manager
->links
, INT_TO_PTR(link
->ifindex
));
514 (void)unlink(link
->state_file
);
515 free(link
->state_file
);
517 udev_device_unref(link
->udev_device
);
519 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
)
520 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(carrier
->ifindex
));
521 hashmap_free(link
->bound_to_links
);
523 HASHMAP_FOREACH (carrier
, link
->bound_by_links
, i
)
524 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(carrier
->ifindex
));
525 hashmap_free(link
->bound_by_links
);
530 Link
*link_unref(Link
*link
) {
534 assert(link
->n_ref
> 0);
546 Link
*link_ref(Link
*link
) {
550 assert(link
->n_ref
> 0);
557 int link_get(Manager
*m
, int ifindex
, Link
**ret
) {
564 link
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
573 static void link_set_state(Link
*link
, LinkState state
) {
576 if (link
->state
== state
)
581 link_send_changed(link
, "AdministrativeState", NULL
);
584 static void link_enter_unmanaged(Link
*link
) {
587 log_link_debug(link
, "Unmanaged");
589 link_set_state(link
, LINK_STATE_UNMANAGED
);
594 static int link_stop_clients(Link
*link
) {
598 assert(link
->manager
);
599 assert(link
->manager
->event
);
601 if (link
->dhcp_client
) {
602 k
= sd_dhcp_client_stop(link
->dhcp_client
);
604 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv4 client: %m");
608 k
= sd_ipv4ll_stop(link
->ipv4ll
);
610 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 link-local: %m");
613 if (link
->dhcp6_client
) {
614 k
= sd_dhcp6_client_stop(link
->dhcp6_client
);
616 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv6 client: %m");
619 if (link
->ndisc_router_discovery
) {
620 k
= sd_ndisc_stop(link
->ndisc_router_discovery
);
622 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Discovery: %m");
625 link_lldp_emit_stop(link
);
629 void link_enter_failed(Link
*link
) {
632 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
635 log_link_warning(link
, "Failed");
637 link_set_state(link
, LINK_STATE_FAILED
);
639 link_stop_clients(link
);
644 static Address
* link_find_dhcp_server_address(Link
*link
) {
648 assert(link
->network
);
650 /* The first statically configured address if there is any */
651 LIST_FOREACH(addresses
, address
, link
->network
->static_addresses
) {
653 if (address
->family
!= AF_INET
)
656 if (in_addr_is_null(address
->family
, &address
->in_addr
))
662 /* If that didn't work, find a suitable address we got from the pool */
663 LIST_FOREACH(addresses
, address
, link
->pool_addresses
) {
664 if (address
->family
!= AF_INET
)
673 static int link_enter_configured(Link
*link
) {
675 assert(link
->network
);
676 assert(link
->state
== LINK_STATE_SETTING_ROUTES
);
678 log_link_info(link
, "Configured");
680 link_set_state(link
, LINK_STATE_CONFIGURED
);
687 void link_check_ready(Link
*link
) {
693 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
699 if (!link
->static_configured
)
702 if (link_ipv4ll_enabled(link
))
703 if (!link
->ipv4ll_address
||
707 if (link_ipv6ll_enabled(link
))
708 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) > 0)
711 if ((link_dhcp4_enabled(link
) && !link_dhcp6_enabled(link
) &&
712 !link
->dhcp4_configured
) ||
713 (link_dhcp6_enabled(link
) && !link_dhcp4_enabled(link
) &&
714 !link
->dhcp6_configured
) ||
715 (link_dhcp4_enabled(link
) && link_dhcp6_enabled(link
) &&
716 !link
->dhcp4_configured
&& !link
->dhcp6_configured
))
719 if (link_ipv6_accept_ra_enabled(link
) && !link
->ndisc_configured
)
722 SET_FOREACH(a
, link
->addresses
, i
)
723 if (!address_is_ready(a
))
726 if (link
->state
!= LINK_STATE_CONFIGURED
)
727 link_enter_configured(link
);
732 static int route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
733 _cleanup_link_unref_ Link
*link
= userdata
;
736 assert(link
->link_messages
> 0);
737 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
738 LINK_STATE_SETTING_ROUTES
, LINK_STATE_FAILED
,
741 link
->link_messages
--;
743 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
746 r
= sd_netlink_message_get_errno(m
);
747 if (r
< 0 && r
!= -EEXIST
)
748 log_link_warning_errno(link
, r
, "Could not set route: %m");
750 if (link
->link_messages
== 0) {
751 log_link_debug(link
, "Routes set");
752 link
->static_configured
= true;
753 link_check_ready(link
);
759 static int link_enter_set_routes(Link
*link
) {
764 assert(link
->network
);
765 assert(link
->state
== LINK_STATE_SETTING_ADDRESSES
);
767 link_set_state(link
, LINK_STATE_SETTING_ROUTES
);
769 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
770 r
= route_configure(rt
, link
, route_handler
);
772 log_link_warning_errno(link
, r
, "Could not set routes: %m");
773 link_enter_failed(link
);
777 link
->link_messages
++;
780 if (link
->link_messages
== 0) {
781 link
->static_configured
= true;
782 link_check_ready(link
);
784 log_link_debug(link
, "Setting routes");
789 int link_route_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
790 _cleanup_link_unref_ Link
*link
= userdata
;
795 assert(link
->ifname
);
797 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
800 r
= sd_netlink_message_get_errno(m
);
801 if (r
< 0 && r
!= -ESRCH
)
802 log_link_warning_errno(link
, r
, "Could not drop route: %m");
807 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
808 _cleanup_link_unref_ Link
*link
= userdata
;
814 assert(link
->ifname
);
815 assert(link
->link_messages
> 0);
816 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
817 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
819 link
->link_messages
--;
821 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
824 r
= sd_netlink_message_get_errno(m
);
825 if (r
< 0 && r
!= -EEXIST
)
826 log_link_warning_errno(link
, r
, "could not set address: %m");
828 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
830 if (link
->link_messages
== 0) {
831 log_link_debug(link
, "Addresses set");
832 link_enter_set_routes(link
);
838 static int link_push_dns_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
839 _cleanup_free_
struct in_addr
*addresses
= NULL
;
840 size_t n_addresses
= 0, n_allocated
= 0;
843 log_debug("Copying DNS server information from %s", link
->ifname
);
848 STRV_FOREACH(a
, link
->network
->dns
) {
851 /* Only look for IPv4 addresses */
852 if (inet_pton(AF_INET
, *a
, &ia
) <= 0)
855 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
858 addresses
[n_addresses
++] = ia
;
861 if (link
->network
->dhcp_use_dns
&&
863 const struct in_addr
*da
= NULL
;
866 n
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &da
);
869 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
872 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
877 if (n_addresses
<= 0)
880 return sd_dhcp_server_set_dns(s
, addresses
, n_addresses
);
883 static int link_push_ntp_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
884 _cleanup_free_
struct in_addr
*addresses
= NULL
;
885 size_t n_addresses
= 0, n_allocated
= 0;
891 log_debug("Copying NTP server information from %s", link
->ifname
);
893 STRV_FOREACH(a
, link
->network
->ntp
) {
896 /* Only look for IPv4 addresses */
897 if (inet_pton(AF_INET
, *a
, &ia
) <= 0)
900 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
903 addresses
[n_addresses
++] = ia
;
906 if (link
->network
->dhcp_use_ntp
&&
908 const struct in_addr
*da
= NULL
;
911 n
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &da
);
914 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
917 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
922 if (n_addresses
<= 0)
925 return sd_dhcp_server_set_ntp(s
, addresses
, n_addresses
);
928 static int link_enter_set_addresses(Link
*link
) {
933 assert(link
->network
);
934 assert(link
->state
!= _LINK_STATE_INVALID
);
936 link_set_state(link
, LINK_STATE_SETTING_ADDRESSES
);
938 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
939 r
= address_configure(ad
, link
, address_handler
, false);
941 log_link_warning_errno(link
, r
, "Could not set addresses: %m");
942 link_enter_failed(link
);
946 link
->link_messages
++;
949 /* now that we can figure out a default address for the dhcp server,
951 if (link_dhcp4_server_enabled(link
)) {
954 bool acquired_uplink
= false;
956 address
= link_find_dhcp_server_address(link
);
958 log_link_warning(link
, "Failed to find suitable address for DHCPv4 server instance.");
959 link_enter_failed(link
);
963 /* use the server address' subnet as the pool */
964 r
= sd_dhcp_server_configure_pool(link
->dhcp_server
, &address
->in_addr
.in
, address
->prefixlen
,
965 link
->network
->dhcp_server_pool_offset
, link
->network
->dhcp_server_pool_size
);
970 r = sd_dhcp_server_set_router(link->dhcp_server,
971 &main_address->in_addr.in);
976 if (link
->network
->dhcp_server_max_lease_time_usec
> 0) {
977 r
= sd_dhcp_server_set_max_lease_time(
979 DIV_ROUND_UP(link
->network
->dhcp_server_max_lease_time_usec
, USEC_PER_SEC
));
984 if (link
->network
->dhcp_server_default_lease_time_usec
> 0) {
985 r
= sd_dhcp_server_set_default_lease_time(
987 DIV_ROUND_UP(link
->network
->dhcp_server_default_lease_time_usec
, USEC_PER_SEC
));
992 if (link
->network
->dhcp_server_emit_dns
) {
994 if (link
->network
->n_dhcp_server_dns
> 0)
995 r
= sd_dhcp_server_set_dns(link
->dhcp_server
, link
->network
->dhcp_server_dns
, link
->network
->n_dhcp_server_dns
);
997 uplink
= manager_find_uplink(link
->manager
, link
);
998 acquired_uplink
= true;
1001 log_link_debug(link
, "Not emitting DNS server information on link, couldn't find suitable uplink.");
1004 r
= link_push_dns_to_dhcp_server(uplink
, link
->dhcp_server
);
1007 log_link_warning_errno(link
, r
, "Failed to set DNS server for DHCP server, ignoring: %m");
1011 if (link
->network
->dhcp_server_emit_ntp
) {
1013 if (link
->network
->n_dhcp_server_ntp
> 0)
1014 r
= sd_dhcp_server_set_ntp(link
->dhcp_server
, link
->network
->dhcp_server_ntp
, link
->network
->n_dhcp_server_ntp
);
1016 if (!acquired_uplink
)
1017 uplink
= manager_find_uplink(link
->manager
, link
);
1020 log_link_debug(link
, "Not emitting NTP server information on link, couldn't find suitable uplink.");
1023 r
= link_push_ntp_to_dhcp_server(uplink
, link
->dhcp_server
);
1027 log_link_warning_errno(link
, r
, "Failed to set NTP server for DHCP server, ignoring: %m");
1030 r
= sd_dhcp_server_set_emit_router(link
->dhcp_server
, link
->network
->dhcp_server_emit_router
);
1032 log_link_warning_errno(link
, r
, "Failed to set router emission for DHCP server: %m");
1036 if (link
->network
->dhcp_server_emit_timezone
) {
1037 _cleanup_free_
char *buffer
= NULL
;
1038 const char *tz
= NULL
;
1040 if (link
->network
->dhcp_server_timezone
)
1041 tz
= link
->network
->dhcp_server_timezone
;
1043 r
= get_timezone(&buffer
);
1045 log_warning_errno(r
, "Failed to determine timezone: %m");
1051 r
= sd_dhcp_server_set_timezone(link
->dhcp_server
, tz
);
1057 r
= sd_dhcp_server_start(link
->dhcp_server
);
1059 log_link_warning_errno(link
, r
, "Could not start DHCPv4 server instance: %m");
1061 link_enter_failed(link
);
1066 log_link_debug(link
, "Offering DHCPv4 leases");
1069 if (link
->link_messages
== 0)
1070 link_enter_set_routes(link
);
1072 log_link_debug(link
, "Setting addresses");
1077 int link_address_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1078 _cleanup_link_unref_ Link
*link
= userdata
;
1083 assert(link
->ifname
);
1085 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1088 r
= sd_netlink_message_get_errno(m
);
1089 if (r
< 0 && r
!= -EADDRNOTAVAIL
)
1090 log_link_warning_errno(link
, r
, "Could not drop address: %m");
1095 static int link_set_bridge_fdb(Link
*const link
) {
1096 FdbEntry
*fdb_entry
;
1099 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
1100 r
= fdb_entry_configure(link
, fdb_entry
);
1102 log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
1110 static int link_set_proxy_arp(Link
*const link
) {
1111 const char *p
= NULL
;
1114 if (!link_proxy_arp_enabled(link
))
1117 p
= strjoina("/proc/sys/net/ipv4/conf/", link
->ifname
, "/proxy_arp");
1119 r
= write_string_file(p
, one_zero(link
->network
->proxy_arp
), WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
1121 log_link_warning_errno(link
, r
, "Cannot configure proxy ARP for interface: %m");
1126 static int link_set_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1127 _cleanup_link_unref_ Link
*link
= userdata
;
1130 log_link_debug(link
, "Set link");
1132 r
= sd_netlink_message_get_errno(m
);
1133 if (r
< 0 && r
!= -EEXIST
) {
1134 log_link_error_errno(link
, r
, "Could not join netdev: %m");
1135 link_enter_failed(link
);
1142 static int set_hostname_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
1143 _cleanup_link_unref_ Link
*link
= userdata
;
1144 const sd_bus_error
*e
;
1149 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1152 e
= sd_bus_message_get_error(m
);
1154 log_link_warning_errno(link
, sd_bus_error_get_errno(e
), "Could not set hostname: %s", e
->message
);
1159 int link_set_hostname(Link
*link
, const char *hostname
) {
1163 assert(link
->manager
);
1165 log_link_debug(link
, "Setting transient hostname: '%s'", strna(hostname
));
1167 if (!link
->manager
->bus
) {
1168 /* TODO: replace by assert when we can rely on kdbus */
1169 log_link_info(link
, "Not connected to system bus, ignoring transient hostname.");
1173 r
= sd_bus_call_method_async(
1176 "org.freedesktop.hostname1",
1177 "/org/freedesktop/hostname1",
1178 "org.freedesktop.hostname1",
1180 set_hostname_handler
,
1187 return log_link_error_errno(link
, r
, "Could not set transient hostname: %m");
1194 static int set_timezone_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
1195 _cleanup_link_unref_ Link
*link
= userdata
;
1196 const sd_bus_error
*e
;
1201 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1204 e
= sd_bus_message_get_error(m
);
1206 log_link_warning_errno(link
, sd_bus_error_get_errno(e
), "Could not set timezone: %s", e
->message
);
1211 int link_set_timezone(Link
*link
, const char *tz
) {
1215 assert(link
->manager
);
1218 log_link_debug(link
, "Setting system timezone: '%s'", tz
);
1220 if (!link
->manager
->bus
) {
1221 log_link_info(link
, "Not connected to system bus, ignoring timezone.");
1225 r
= sd_bus_call_method_async(
1228 "org.freedesktop.timedate1",
1229 "/org/freedesktop/timedate1",
1230 "org.freedesktop.timedate1",
1232 set_timezone_handler
,
1238 return log_link_error_errno(link
, r
, "Could not set timezone: %m");
1245 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1246 _cleanup_link_unref_ Link
*link
= userdata
;
1251 assert(link
->ifname
);
1253 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1256 r
= sd_netlink_message_get_errno(m
);
1258 log_link_warning_errno(link
, r
, "Could not set MTU: %m");
1263 int link_set_mtu(Link
*link
, uint32_t mtu
) {
1264 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1268 assert(link
->manager
);
1269 assert(link
->manager
->rtnl
);
1271 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1273 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1275 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1277 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1279 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1281 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, set_mtu_handler
, link
, 0, NULL
);
1283 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1290 static int link_set_bridge(Link
*link
) {
1291 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1295 assert(link
->network
);
1297 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1299 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1301 r
= sd_rtnl_message_link_set_family(req
, PF_BRIDGE
);
1303 return log_link_error_errno(link
, r
, "Could not set message family: %m");
1305 r
= sd_netlink_message_open_container(req
, IFLA_PROTINFO
);
1307 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1309 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_GUARD
, !link
->network
->use_bpdu
);
1311 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_GUARD attribute: %m");
1313 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MODE
, link
->network
->hairpin
);
1315 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MODE attribute: %m");
1317 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_FAST_LEAVE
, link
->network
->fast_leave
);
1319 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_FAST_LEAVE attribute: %m");
1321 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_PROTECT
, !link
->network
->allow_port_to_be_root
);
1323 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PROTECT attribute: %m");
1325 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_UNICAST_FLOOD
, link
->network
->unicast_flood
);
1327 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_UNICAST_FLOOD attribute: %m");
1329 if (link
->network
->cost
!= 0) {
1330 r
= sd_netlink_message_append_u32(req
, IFLA_BRPORT_COST
, link
->network
->cost
);
1332 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_COST attribute: %m");
1335 r
= sd_netlink_message_close_container(req
);
1337 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1339 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_set_handler
, link
, 0, NULL
);
1341 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1348 static int link_lldp_save(Link
*link
) {
1349 _cleanup_free_
char *temp_path
= NULL
;
1350 _cleanup_fclose_
FILE *f
= NULL
;
1351 sd_lldp_neighbor
**l
= NULL
;
1355 assert(link
->lldp_file
);
1358 (void) unlink(link
->lldp_file
);
1362 r
= sd_lldp_get_neighbors(link
->lldp
, &l
);
1366 (void) unlink(link
->lldp_file
);
1372 r
= fopen_temporary(link
->lldp_file
, &f
, &temp_path
);
1376 fchmod(fileno(f
), 0644);
1378 for (i
= 0; i
< n
; i
++) {
1383 r
= sd_lldp_neighbor_get_raw(l
[i
], &p
, &sz
);
1388 (void) fwrite(&u
, 1, sizeof(u
), f
);
1389 (void) fwrite(p
, 1, sz
, f
);
1392 r
= fflush_and_check(f
);
1396 if (rename(temp_path
, link
->lldp_file
) < 0) {
1403 (void) unlink(link
->lldp_file
);
1405 (void) unlink(temp_path
);
1407 log_link_error_errno(link
, r
, "Failed to save LLDP data to %s: %m", link
->lldp_file
);
1411 for (i
= 0; i
< n
; i
++)
1412 sd_lldp_neighbor_unref(l
[i
]);
1419 static void lldp_handler(sd_lldp
*lldp
, sd_lldp_event event
, sd_lldp_neighbor
*n
, void *userdata
) {
1420 Link
*link
= userdata
;
1425 (void) link_lldp_save(link
);
1427 if (link_lldp_emit_enabled(link
) && event
== SD_LLDP_EVENT_ADDED
) {
1428 /* If we received information about a new neighbor, restart the LLDP "fast" logic */
1430 log_link_debug(link
, "Received LLDP datagram from previously unknown neighbor, restarting 'fast' LLDP transmission.");
1432 r
= link_lldp_emit_start(link
);
1434 log_link_warning_errno(link
, r
, "Failed to restart LLDP transmission: %m");
1438 static int link_acquire_ipv6_conf(Link
*link
) {
1443 if (link_dhcp6_enabled(link
)) {
1444 assert(link
->dhcp6_client
);
1445 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1447 log_link_debug(link
, "Acquiring DHCPv6 lease");
1449 r
= sd_dhcp6_client_set_local_address(link
->dhcp6_client
, &link
->ipv6ll_address
);
1450 if (r
< 0 && r
!= -EBUSY
)
1451 return log_link_warning_errno(link
, r
, "Could not set IPv6LL address in DHCP client: %m");
1453 r
= sd_dhcp6_client_start(link
->dhcp6_client
);
1454 if (r
< 0 && r
!= -EBUSY
)
1455 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv6 lease: %m");
1458 if (link_ipv6_accept_ra_enabled(link
)) {
1459 assert(link
->ndisc_router_discovery
);
1461 log_link_debug(link
, "Discovering IPv6 routers");
1463 r
= sd_ndisc_router_discovery_start(link
->ndisc_router_discovery
);
1464 if (r
< 0 && r
!= -EBUSY
)
1465 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1471 static int link_acquire_ipv4_conf(Link
*link
) {
1475 assert(link
->network
);
1476 assert(link
->manager
);
1477 assert(link
->manager
->event
);
1479 if (link_ipv4ll_enabled(link
)) {
1480 assert(link
->ipv4ll
);
1482 log_link_debug(link
, "Acquiring IPv4 link-local address");
1484 r
= sd_ipv4ll_start(link
->ipv4ll
);
1486 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1489 if (link_dhcp4_enabled(link
)) {
1490 assert(link
->dhcp_client
);
1492 log_link_debug(link
, "Acquiring DHCPv4 lease");
1494 r
= sd_dhcp_client_start(link
->dhcp_client
);
1496 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1502 static int link_acquire_conf(Link
*link
) {
1507 r
= link_acquire_ipv4_conf(link
);
1511 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) == 0) {
1512 r
= link_acquire_ipv6_conf(link
);
1517 if (link_lldp_emit_enabled(link
)) {
1518 r
= link_lldp_emit_start(link
);
1520 return log_link_warning_errno(link
, r
, "Failed to start LLDP transmission: %m");
1526 bool link_has_carrier(Link
*link
) {
1527 /* see Documentation/networking/operstates.txt in the kernel sources */
1529 if (link
->kernel_operstate
== IF_OPER_UP
)
1532 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1533 /* operstate may not be implemented, so fall back to flags */
1534 if ((link
->flags
& IFF_LOWER_UP
) && !(link
->flags
& IFF_DORMANT
))
1540 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1541 _cleanup_link_unref_ Link
*link
= userdata
;
1546 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1549 r
= sd_netlink_message_get_errno(m
);
1551 /* we warn but don't fail the link, as it may be
1553 log_link_warning_errno(link
, r
, "Could not bring up interface: %m");
1558 static int link_up(Link
*link
) {
1559 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1560 uint8_t ipv6ll_mode
;
1564 assert(link
->network
);
1565 assert(link
->manager
);
1566 assert(link
->manager
->rtnl
);
1568 log_link_debug(link
, "Bringing link up");
1570 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1572 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1574 /* set it free if not enslaved with networkd */
1575 if (!link
->network
->bridge
&& !link
->network
->bond
) {
1576 r
= sd_netlink_message_append_u32(req
, IFLA_MASTER
, 0);
1578 return log_link_error_errno(link
, r
, "Could not append IFLA_MASTER attribute: %m");
1581 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1583 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1585 if (link
->network
->mac
) {
1586 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
1588 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
1591 /* If IPv6 not configured (no static IPv6 address and IPv6LL autoconfiguration is disabled)
1592 for this interface, or if it is a bridge slave, then disable IPv6 else enable it. */
1593 (void) link_enable_ipv6(link
);
1595 if (link
->network
->mtu
) {
1596 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
1597 on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */
1598 if (link_ipv6_enabled(link
) && link
->network
->mtu
< IPV6_MIN_MTU
) {
1600 log_link_warning(link
, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU
) ", as "
1601 "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU
) " bytes: %m");
1603 link
->network
->mtu
= IPV6_MIN_MTU
;
1606 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, link
->network
->mtu
);
1608 return log_link_error_errno(link
, r
, "Could not set MTU: %m");
1611 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1613 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1615 if (link_ipv6_enabled(link
)) {
1616 /* if the kernel lacks ipv6 support setting IFF_UP fails if any ipv6 options are passed */
1617 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1619 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1621 if (!link_ipv6ll_enabled(link
))
1622 ipv6ll_mode
= IN6_ADDR_GEN_MODE_NONE
;
1624 const char *p
= NULL
;
1625 _cleanup_free_
char *stable_secret
= NULL
;
1627 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/stable_secret");
1628 r
= read_one_line_file(p
, &stable_secret
);
1631 ipv6ll_mode
= IN6_ADDR_GEN_MODE_EUI64
;
1633 ipv6ll_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
1635 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1637 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1639 if (!in_addr_is_null(AF_INET6
, &link
->network
->ipv6_token
)) {
1640 r
= sd_netlink_message_append_in6_addr(req
, IFLA_INET6_TOKEN
, &link
->network
->ipv6_token
.in6
);
1642 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_TOKEN: %m");
1645 r
= sd_netlink_message_close_container(req
);
1647 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1650 r
= sd_netlink_message_close_container(req
);
1652 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1654 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_up_handler
, link
, 0, NULL
);
1656 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1663 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1664 _cleanup_link_unref_ Link
*link
= userdata
;
1669 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1672 r
= sd_netlink_message_get_errno(m
);
1674 log_link_warning_errno(link
, r
, "Could not bring down interface: %m");
1679 static int link_down(Link
*link
) {
1680 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1684 assert(link
->manager
);
1685 assert(link
->manager
->rtnl
);
1687 log_link_debug(link
, "Bringing link down");
1689 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
1690 RTM_SETLINK
, link
->ifindex
);
1692 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1694 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
1696 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1698 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_down_handler
, link
, 0, NULL
);
1700 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1707 static int link_handle_bound_to_list(Link
*link
) {
1711 bool required_up
= false;
1712 bool link_is_up
= false;
1716 if (hashmap_isempty(link
->bound_to_links
))
1719 if (link
->flags
& IFF_UP
)
1722 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
1723 if (link_has_carrier(l
)) {
1728 if (!required_up
&& link_is_up
) {
1729 r
= link_down(link
);
1732 } else if (required_up
&& !link_is_up
) {
1741 static int link_handle_bound_by_list(Link
*link
) {
1748 if (hashmap_isempty(link
->bound_by_links
))
1751 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
1752 r
= link_handle_bound_to_list(l
);
1760 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
1766 if (link
== carrier
)
1769 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
1772 r
= hashmap_ensure_allocated(h
, NULL
);
1776 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
1783 static int link_new_bound_by_list(Link
*link
) {
1788 bool list_updated
= false;
1791 assert(link
->manager
);
1795 HASHMAP_FOREACH(carrier
, m
->links
, i
) {
1796 if (!carrier
->network
)
1799 if (strv_isempty(carrier
->network
->bind_carrier
))
1802 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
, 0)) {
1803 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
1807 list_updated
= true;
1814 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
1815 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
1819 link_dirty(carrier
);
1825 static int link_new_bound_to_list(Link
*link
) {
1830 bool list_updated
= false;
1833 assert(link
->manager
);
1838 if (strv_isempty(link
->network
->bind_carrier
))
1843 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
1844 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
, 0)) {
1845 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
1849 list_updated
= true;
1856 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
1857 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
1861 link_dirty(carrier
);
1867 static int link_new_carrier_maps(Link
*link
) {
1870 r
= link_new_bound_by_list(link
);
1874 r
= link_handle_bound_by_list(link
);
1878 r
= link_new_bound_to_list(link
);
1882 r
= link_handle_bound_to_list(link
);
1889 static void link_free_bound_to_list(Link
*link
) {
1893 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
1894 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
1896 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
1897 link_dirty(bound_to
);
1903 static void link_free_bound_by_list(Link
*link
) {
1907 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
1908 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
1910 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
1911 link_dirty(bound_by
);
1912 link_handle_bound_to_list(bound_by
);
1919 static void link_free_carrier_maps(Link
*link
) {
1920 bool list_updated
= false;
1924 if (!hashmap_isempty(link
->bound_to_links
)) {
1925 link_free_bound_to_list(link
);
1926 list_updated
= true;
1929 if (!hashmap_isempty(link
->bound_by_links
)) {
1930 link_free_bound_by_list(link
);
1931 list_updated
= true;
1940 void link_drop(Link
*link
) {
1941 if (!link
|| link
->state
== LINK_STATE_LINGER
)
1944 link_set_state(link
, LINK_STATE_LINGER
);
1946 link_free_carrier_maps(link
);
1948 log_link_debug(link
, "Link removed");
1950 (void)unlink(link
->state_file
);
1956 static int link_joined(Link
*link
) {
1960 assert(link
->network
);
1962 if (!hashmap_isempty(link
->bound_to_links
)) {
1963 r
= link_handle_bound_to_list(link
);
1966 } else if (!(link
->flags
& IFF_UP
)) {
1969 link_enter_failed(link
);
1974 if (link
->network
->bridge
) {
1975 r
= link_set_bridge(link
);
1977 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
1980 return link_enter_set_addresses(link
);
1983 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1984 _cleanup_link_unref_ Link
*link
= userdata
;
1988 assert(link
->network
);
1992 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1995 r
= sd_netlink_message_get_errno(m
);
1996 if (r
< 0 && r
!= -EEXIST
) {
1997 log_link_error_errno(link
, r
, "Could not join netdev: %m");
1998 link_enter_failed(link
);
2001 log_link_debug(link
, "Joined netdev");
2003 if (link
->enslaving
<= 0)
2009 static int link_enter_join_netdev(Link
*link
) {
2015 assert(link
->network
);
2016 assert(link
->state
== LINK_STATE_PENDING
);
2018 link_set_state(link
, LINK_STATE_ENSLAVING
);
2022 if (!link
->network
->bridge
&&
2023 !link
->network
->bond
&&
2024 hashmap_isempty(link
->network
->stacked_netdevs
))
2025 return link_joined(link
);
2027 if (link
->network
->bond
) {
2028 log_struct(LOG_DEBUG
,
2029 LOG_LINK_INTERFACE(link
),
2030 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2031 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
),
2034 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
2036 log_struct_errno(LOG_WARNING
, r
,
2037 LOG_LINK_INTERFACE(link
),
2038 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2039 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
),
2042 link_enter_failed(link
);
2049 if (link
->network
->bridge
) {
2050 log_struct(LOG_DEBUG
,
2051 LOG_LINK_INTERFACE(link
),
2052 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2053 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
),
2056 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
2058 log_struct_errno(LOG_WARNING
, r
,
2059 LOG_LINK_INTERFACE(link
),
2060 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2061 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
),
2063 link_enter_failed(link
);
2070 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
2072 log_struct(LOG_DEBUG
,
2073 LOG_LINK_INTERFACE(link
),
2074 LOG_NETDEV_INTERFACE(netdev
),
2075 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
),
2078 r
= netdev_join(netdev
, link
, netdev_join_handler
);
2080 log_struct_errno(LOG_WARNING
, r
,
2081 LOG_LINK_INTERFACE(link
),
2082 LOG_NETDEV_INTERFACE(netdev
),
2083 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
),
2085 link_enter_failed(link
);
2095 static int link_set_ipv4_forward(Link
*link
) {
2098 if (!link_ipv4_forward_enabled(link
))
2101 /* We propagate the forwarding flag from one interface to the
2102 * global setting one way. This means: as long as at least one
2103 * interface was configured at any time that had IP forwarding
2104 * enabled the setting will stay on for good. We do this
2105 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
2106 * somewhat in sync (see below). */
2108 r
= write_string_file("/proc/sys/net/ipv4/ip_forward", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2110 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
2115 static int link_set_ipv6_forward(Link
*link
) {
2118 if (!link_ipv6_forward_enabled(link
))
2121 /* On Linux, the IPv6 stack does not not know a per-interface
2122 * packet forwarding setting: either packet forwarding is on
2123 * for all, or off for all. We hence don't bother with a
2124 * per-interface setting, but simply propagate the interface
2125 * flag, if it is set, to the global flag, one-way. Note that
2126 * while IPv4 would allow a per-interface flag, we expose the
2127 * same behaviour there and also propagate the setting from
2128 * one to all, to keep things simple (see above). */
2130 r
= write_string_file("/proc/sys/net/ipv6/conf/all/forwarding", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2132 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
2137 static int link_set_ipv6_privacy_extensions(Link
*link
) {
2138 char buf
[DECIMAL_STR_MAX(unsigned) + 1];
2139 IPv6PrivacyExtensions s
;
2140 const char *p
= NULL
;
2143 s
= link_ipv6_privacy_extensions(link
);
2147 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/use_tempaddr");
2148 xsprintf(buf
, "%u", (unsigned) link
->network
->ipv6_privacy_extensions
);
2150 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2152 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
2157 static int link_set_ipv6_accept_ra(Link
*link
) {
2158 const char *p
= NULL
;
2161 /* Make this a NOP if IPv6 is not available */
2162 if (!socket_ipv6_is_supported())
2165 if (link
->flags
& IFF_LOOPBACK
)
2171 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/accept_ra");
2173 /* We handle router advertisements ourselves, tell the kernel to GTFO */
2174 r
= write_string_file(p
, "0", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2176 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
2181 static int link_set_ipv6_dad_transmits(Link
*link
) {
2182 char buf
[DECIMAL_STR_MAX(int) + 1];
2183 const char *p
= NULL
;
2186 /* Make this a NOP if IPv6 is not available */
2187 if (!socket_ipv6_is_supported())
2190 if (link
->flags
& IFF_LOOPBACK
)
2196 if (link
->network
->ipv6_dad_transmits
< 0)
2199 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/dad_transmits");
2200 xsprintf(buf
, "%i", link
->network
->ipv6_dad_transmits
);
2202 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2204 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
2209 static int link_set_ipv6_hop_limit(Link
*link
) {
2210 char buf
[DECIMAL_STR_MAX(int) + 1];
2211 const char *p
= NULL
;
2214 /* Make this a NOP if IPv6 is not available */
2215 if (!socket_ipv6_is_supported())
2218 if (link
->flags
& IFF_LOOPBACK
)
2224 if (link
->network
->ipv6_hop_limit
< 0)
2227 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/hop_limit");
2228 xsprintf(buf
, "%i", link
->network
->ipv6_hop_limit
);
2230 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2232 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2237 static int link_drop_foreign_config(Link
*link
) {
2243 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
2244 /* we consider IPv6LL addresses to be managed by the kernel */
2245 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2248 r
= address_remove(address
, link
, link_address_remove_handler
);
2253 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2254 /* do not touch routes managed by the kernel */
2255 if (route
->protocol
== RTPROT_KERNEL
)
2258 r
= route_remove(route
, link
, link_address_remove_handler
);
2266 static int link_update_lldp(Link
*link
) {
2274 if (link
->flags
& IFF_UP
) {
2275 r
= sd_lldp_start(link
->lldp
);
2277 log_link_debug(link
, "Started LLDP.");
2279 r
= sd_lldp_stop(link
->lldp
);
2281 log_link_debug(link
, "Stopped LLDP.");
2287 static int link_configure(Link
*link
) {
2291 assert(link
->network
);
2292 assert(link
->state
== LINK_STATE_PENDING
);
2294 /* Drop foreign config, but ignore loopback or critical devices.
2295 * We do not want to remove loopback address or addresses used for root NFS. */
2296 if (!(link
->flags
& IFF_LOOPBACK
) && !(link
->network
->dhcp_critical
)) {
2297 r
= link_drop_foreign_config(link
);
2302 r
= link_set_bridge_fdb(link
);
2306 r
= link_set_proxy_arp(link
);
2310 r
= link_set_ipv4_forward(link
);
2314 r
= link_set_ipv6_forward(link
);
2318 r
= link_set_ipv6_privacy_extensions(link
);
2322 r
= link_set_ipv6_accept_ra(link
);
2326 r
= link_set_ipv6_dad_transmits(link
);
2330 r
= link_set_ipv6_hop_limit(link
);
2334 if (link_ipv4ll_enabled(link
)) {
2335 r
= ipv4ll_configure(link
);
2340 if (link_dhcp4_enabled(link
)) {
2341 r
= dhcp4_configure(link
);
2346 if (link_dhcp4_server_enabled(link
)) {
2347 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
2351 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
2356 if (link_dhcp6_enabled(link
) ||
2357 link_ipv6_accept_ra_enabled(link
)) {
2358 r
= dhcp6_configure(link
);
2363 if (link_ipv6_accept_ra_enabled(link
)) {
2364 r
= ndisc_configure(link
);
2369 if (link_lldp_rx_enabled(link
)) {
2370 r
= sd_lldp_new(&link
->lldp
, link
->ifindex
);
2374 r
= sd_lldp_match_capabilities(link
->lldp
,
2375 link
->network
->lldp_mode
== LLDP_MODE_ROUTERS_ONLY
?
2376 SD_LLDP_SYSTEM_CAPABILITIES_ALL_ROUTERS
:
2377 SD_LLDP_SYSTEM_CAPABILITIES_ALL
);
2381 r
= sd_lldp_set_filter_address(link
->lldp
, &link
->mac
);
2385 r
= sd_lldp_attach_event(link
->lldp
, NULL
, 0);
2389 r
= sd_lldp_set_callback(link
->lldp
, lldp_handler
, link
);
2393 r
= link_update_lldp(link
);
2398 if (link_has_carrier(link
)) {
2399 r
= link_acquire_conf(link
);
2404 return link_enter_join_netdev(link
);
2407 static int link_initialized_and_synced(sd_netlink
*rtnl
, sd_netlink_message
*m
,
2409 _cleanup_link_unref_ Link
*link
= userdata
;
2414 assert(link
->ifname
);
2415 assert(link
->manager
);
2417 if (link
->state
!= LINK_STATE_PENDING
)
2420 log_link_debug(link
, "Link state is up-to-date");
2422 r
= link_new_bound_by_list(link
);
2426 r
= link_handle_bound_by_list(link
);
2430 if (!link
->network
) {
2431 r
= network_get(link
->manager
, link
->udev_device
, link
->ifname
,
2432 &link
->mac
, &network
);
2434 link_enter_unmanaged(link
);
2439 if (link
->flags
& IFF_LOOPBACK
) {
2440 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
2441 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
2443 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
2444 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
2446 if (network
->dhcp_server
)
2447 log_link_debug(link
, "Ignoring DHCP server for loopback link");
2450 r
= network_apply(link
->manager
, network
, link
);
2455 r
= link_new_bound_to_list(link
);
2459 r
= link_configure(link
);
2466 int link_initialized(Link
*link
, struct udev_device
*device
) {
2467 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2471 assert(link
->manager
);
2472 assert(link
->manager
->rtnl
);
2475 if (link
->state
!= LINK_STATE_PENDING
)
2478 if (link
->udev_device
)
2481 log_link_debug(link
, "udev initialized link");
2483 link
->udev_device
= udev_device_ref(device
);
2485 /* udev has initialized the link, but we don't know if we have yet
2486 * processed the NEWLINK messages with the latest state. Do a GETLINK,
2487 * when it returns we know that the pending NEWLINKs have already been
2488 * processed and that we are up-to-date */
2490 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
2495 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
,
2496 link_initialized_and_synced
, link
, 0, NULL
);
2505 static int link_load(Link
*link
) {
2506 _cleanup_free_
char *network_file
= NULL
,
2509 *dhcp4_address
= NULL
,
2510 *ipv4ll_address
= NULL
;
2511 union in_addr_union address
;
2512 union in_addr_union route_dst
;
2518 r
= parse_env_file(link
->state_file
, NEWLINE
,
2519 "NETWORK_FILE", &network_file
,
2520 "ADDRESSES", &addresses
,
2522 "DHCP4_ADDRESS", &dhcp4_address
,
2523 "IPV4LL_ADDRESS", &ipv4ll_address
,
2525 if (r
< 0 && r
!= -ENOENT
)
2526 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
2533 suffix
= strrchr(network_file
, '.');
2535 log_link_debug(link
, "Failed to get network name from %s", network_file
);
2536 goto network_file_fail
;
2540 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
2542 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
2543 goto network_file_fail
;
2546 r
= network_apply(link
->manager
, network
, link
);
2548 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
2557 _cleanup_free_
char *address_str
= NULL
;
2558 char *prefixlen_str
;
2560 unsigned char prefixlen
;
2562 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
2564 log_link_debug_errno(link
, r
, "Failed to extract next address string: %m");
2570 prefixlen_str
= strchr(address_str
, '/');
2571 if (!prefixlen_str
) {
2572 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
2576 *prefixlen_str
++ = '\0';
2578 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
2580 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
2584 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
2586 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
2590 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
2592 return log_link_error_errno(link
, r
, "Failed to add address: %m");
2601 _cleanup_free_
char *route_str
= NULL
;
2602 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
2604 char *prefixlen_str
;
2606 unsigned char prefixlen
, tos
, table
;
2609 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
2611 log_link_debug_errno(link
, r
, "Failed to extract next route string: %m");
2617 prefixlen_str
= strchr(route_str
, '/');
2618 if (!prefixlen_str
) {
2619 log_link_debug(link
, "Failed to parse route %s", route_str
);
2623 *prefixlen_str
++ = '\0';
2625 r
= sscanf(prefixlen_str
, "%hhu/%hhu/%"SCNu32
"/%hhu/"USEC_FMT
, &prefixlen
, &tos
, &priority
, &table
, &lifetime
);
2627 log_link_debug(link
,
2628 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
2633 r
= in_addr_from_string_auto(route_str
, &family
, &route_dst
);
2635 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
2639 r
= route_add(link
, family
, &route_dst
, prefixlen
, tos
, priority
, table
, &route
);
2641 return log_link_error_errno(link
, r
, "Failed to add route: %m");
2643 if (lifetime
!= USEC_INFINITY
) {
2644 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(), lifetime
,
2645 0, route_expire_handler
, route
);
2647 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
2650 route
->lifetime
= lifetime
;
2651 sd_event_source_unref(route
->expire
);
2652 route
->expire
= expire
;
2657 if (dhcp4_address
) {
2658 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
2660 log_link_debug_errno(link
, r
, "Falied to parse DHCPv4 address %s: %m", dhcp4_address
);
2661 goto dhcp4_address_fail
;
2664 r
= sd_dhcp_client_new(&link
->dhcp_client
);
2666 return log_link_error_errno(link
, r
, "Falied to create DHCPv4 client: %m");
2668 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
2670 return log_link_error_errno(link
, r
, "Falied to set inital DHCPv4 address %s: %m", dhcp4_address
);
2675 if (ipv4ll_address
) {
2676 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
2678 log_link_debug_errno(link
, r
, "Falied to parse IPv4LL address %s: %m", ipv4ll_address
);
2679 goto ipv4ll_address_fail
;
2682 r
= sd_ipv4ll_new(&link
->ipv4ll
);
2684 return log_link_error_errno(link
, r
, "Falied to create IPv4LL client: %m");
2686 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
2688 return log_link_error_errno(link
, r
, "Falied to set inital IPv4LL address %s: %m", ipv4ll_address
);
2691 ipv4ll_address_fail
:
2696 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
2698 _cleanup_udev_device_unref_
struct udev_device
*device
= NULL
;
2699 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
2707 r
= link_new(m
, message
, ret
);
2713 log_link_debug(link
, "Link %d added", link
->ifindex
);
2715 r
= link_load(link
);
2719 if (detect_container() <= 0) {
2720 /* not in a container, udev will be around */
2721 sprintf(ifindex_str
, "n%d", link
->ifindex
);
2722 device
= udev_device_new_from_device_id(m
->udev
, ifindex_str
);
2724 r
= log_link_warning_errno(link
, errno
, "Could not find udev device: %m");
2728 if (udev_device_get_is_initialized(device
) <= 0) {
2730 log_link_debug(link
, "link pending udev initialization...");
2734 r
= link_initialized(link
, device
);
2738 /* we are calling a callback directly, so must take a ref */
2741 r
= link_initialized_and_synced(m
->rtnl
, NULL
, link
);
2748 link_enter_failed(link
);
2752 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
2757 log_link_info(link
, "Gained IPv6LL");
2759 link
->ipv6ll_address
= *address
;
2760 link_check_ready(link
);
2762 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
2763 r
= link_acquire_ipv6_conf(link
);
2765 link_enter_failed(link
);
2773 static int link_carrier_gained(Link
*link
) {
2778 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
2779 r
= link_acquire_conf(link
);
2781 link_enter_failed(link
);
2785 r
= link_enter_set_addresses(link
);
2790 r
= link_handle_bound_by_list(link
);
2797 static int link_carrier_lost(Link
*link
) {
2802 r
= link_stop_clients(link
);
2804 link_enter_failed(link
);
2808 r
= link_handle_bound_by_list(link
);
2815 int link_carrier_reset(Link
*link
) {
2820 if (link_has_carrier(link
)) {
2821 r
= link_carrier_lost(link
);
2825 r
= link_carrier_gained(link
);
2829 log_link_info(link
, "Reset carrier");
2835 int link_update(Link
*link
, sd_netlink_message
*m
) {
2836 struct ether_addr mac
;
2839 bool had_carrier
, carrier_gained
, carrier_lost
;
2843 assert(link
->ifname
);
2846 if (link
->state
== LINK_STATE_LINGER
) {
2848 log_link_info(link
, "Link readded");
2849 link_set_state(link
, LINK_STATE_ENSLAVING
);
2851 r
= link_new_carrier_maps(link
);
2856 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
2857 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
2858 log_link_info(link
, "Renamed to %s", ifname
);
2860 link_free_carrier_maps(link
);
2862 r
= free_and_strdup(&link
->ifname
, ifname
);
2866 r
= link_new_carrier_maps(link
);
2871 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
2872 if (r
>= 0 && mtu
> 0) {
2874 if (!link
->original_mtu
) {
2875 link
->original_mtu
= mtu
;
2876 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
2879 if (link
->dhcp_client
) {
2880 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
2883 log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
2889 /* The kernel may broadcast NEWLINK messages without the MAC address
2890 set, simply ignore them. */
2891 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
2893 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
2896 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
2899 log_link_debug(link
, "MAC address: "
2900 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
2901 mac
.ether_addr_octet
[0],
2902 mac
.ether_addr_octet
[1],
2903 mac
.ether_addr_octet
[2],
2904 mac
.ether_addr_octet
[3],
2905 mac
.ether_addr_octet
[4],
2906 mac
.ether_addr_octet
[5]);
2909 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
2911 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
2914 if (link
->dhcp_client
) {
2915 const DUID
*duid
= link_duid(link
);
2917 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
2918 (const uint8_t *) &link
->mac
,
2922 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
2924 r
= sd_dhcp_client_set_iaid_duid(link
->dhcp_client
,
2925 link
->network
->iaid
,
2927 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
2928 duid
->raw_data_len
);
2930 return log_link_warning_errno(link
, r
, "Could not update DUID/IAID in DHCP client: %m");
2933 if (link
->dhcp6_client
) {
2934 const DUID
* duid
= link_duid(link
);
2936 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
2937 (const uint8_t *) &link
->mac
,
2941 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
2943 r
= sd_dhcp6_client_set_iaid(link
->dhcp6_client
,
2944 link
->network
->iaid
);
2946 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 IAID: %m");
2948 r
= sd_dhcp6_client_set_duid(link
->dhcp6_client
,
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 DHCPv6 DUID: %m");
2958 had_carrier
= link_has_carrier(link
);
2960 r
= link_update_flags(link
, m
);
2964 r
= link_update_lldp(link
);
2968 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
2969 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
2971 if (carrier_gained
) {
2972 log_link_info(link
, "Gained carrier");
2974 r
= link_carrier_gained(link
);
2977 } else if (carrier_lost
) {
2978 log_link_info(link
, "Lost carrier");
2980 r
= link_carrier_lost(link
);
2988 static void print_link_hashmap(FILE *f
, const char *prefix
, Hashmap
* h
) {
2996 if (hashmap_isempty(h
))
3000 HASHMAP_FOREACH(link
, h
, i
) {
3004 fprintf(f
, "%i", link
->ifindex
);
3011 int link_save(Link
*link
) {
3012 _cleanup_free_
char *temp_path
= NULL
;
3013 _cleanup_fclose_
FILE *f
= NULL
;
3014 const char *admin_state
, *oper_state
;
3021 assert(link
->state_file
);
3022 assert(link
->lease_file
);
3023 assert(link
->manager
);
3025 if (link
->state
== LINK_STATE_LINGER
) {
3026 unlink(link
->state_file
);
3030 link_lldp_save(link
);
3032 admin_state
= link_state_to_string(link
->state
);
3033 assert(admin_state
);
3035 oper_state
= link_operstate_to_string(link
->operstate
);
3038 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
3042 fchmod(fileno(f
), 0644);
3045 "# This is private data. Do not parse.\n"
3048 admin_state
, oper_state
);
3050 if (link
->network
) {
3052 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
3053 const char *dhcp_domainname
= NULL
;
3054 char **dhcp6_domains
= NULL
;
3056 if (link
->dhcp6_client
) {
3057 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
, &dhcp6_lease
);
3058 if (r
< 0 && r
!= -ENOMSG
)
3059 log_link_debug(link
, "No DHCPv6 lease");
3062 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
3066 fputstrv(f
, link
->network
->dns
, NULL
, &space
);
3068 if (link
->network
->dhcp_use_dns
&&
3070 const struct in_addr
*addresses
;
3072 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
3076 serialize_in_addrs(f
, addresses
, r
);
3081 if (link
->network
->dhcp_use_dns
&& dhcp6_lease
) {
3082 struct in6_addr
*in6_addrs
;
3084 r
= sd_dhcp6_lease_get_dns(dhcp6_lease
, &in6_addrs
);
3088 serialize_in6_addrs(f
, in6_addrs
, r
);
3096 fputstrv(f
, link
->network
->ntp
, NULL
, &space
);
3098 if (link
->network
->dhcp_use_ntp
&&
3100 const struct in_addr
*addresses
;
3102 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
3106 serialize_in_addrs(f
, addresses
, r
);
3111 if (link
->network
->dhcp_use_ntp
&& dhcp6_lease
) {
3112 struct in6_addr
*in6_addrs
;
3115 r
= sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease
,
3120 serialize_in6_addrs(f
, in6_addrs
, r
);
3124 r
= sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease
, &hosts
);
3126 fputstrv(f
, hosts
, NULL
, &space
);
3131 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
3132 if (link
->dhcp_lease
)
3133 (void) sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &dhcp_domainname
);
3136 (void) sd_dhcp6_lease_get_domains(dhcp6_lease
, &dhcp6_domains
);
3139 fputs("DOMAINS=", f
);
3140 fputstrv(f
, link
->network
->search_domains
, NULL
, &space
);
3142 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
&& dhcp_domainname
)
3143 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3145 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
&& dhcp6_domains
)
3146 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3150 fputs("ROUTE_DOMAINS=", f
);
3151 fputstrv(f
, link
->network
->route_domains
, NULL
, NULL
);
3153 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
&& dhcp_domainname
)
3154 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3156 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
&& dhcp6_domains
)
3157 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3161 fprintf(f
, "LLMNR=%s\n",
3162 resolve_support_to_string(link
->network
->llmnr
));
3163 fprintf(f
, "MDNS=%s\n",
3164 resolve_support_to_string(link
->network
->mdns
));
3166 if (link
->network
->dnssec_mode
!= _DNSSEC_MODE_INVALID
)
3167 fprintf(f
, "DNSSEC=%s\n",
3168 dnssec_mode_to_string(link
->network
->dnssec_mode
));
3170 if (!set_isempty(link
->network
->dnssec_negative_trust_anchors
)) {
3173 fputs("DNSSEC_NTA=", f
);
3175 SET_FOREACH(n
, link
->network
->dnssec_negative_trust_anchors
, i
)
3176 fputs_with_space(f
, n
, NULL
, &space
);
3180 fputs("ADDRESSES=", f
);
3182 SET_FOREACH(a
, link
->addresses
, i
) {
3183 _cleanup_free_
char *address_str
= NULL
;
3185 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
3189 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
3194 fputs("ROUTES=", f
);
3196 SET_FOREACH(route
, link
->routes
, i
) {
3197 _cleanup_free_
char *route_str
= NULL
;
3199 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
3203 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%hhu/"USEC_FMT
, space
? " " : "", route_str
,
3204 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
3211 print_link_hashmap(f
, "CARRIER_BOUND_TO=", link
->bound_to_links
);
3212 print_link_hashmap(f
, "CARRIER_BOUND_BY=", link
->bound_by_links
);
3214 if (link
->dhcp_lease
) {
3215 struct in_addr address
;
3216 const char *tz
= NULL
;
3218 assert(link
->network
);
3220 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
3222 fprintf(f
, "TIMEZONE=%s\n", tz
);
3224 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
3226 fputs("DHCP4_ADDRESS=", f
);
3227 serialize_in_addrs(f
, &address
, 1);
3231 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
3239 unlink(link
->lease_file
);
3242 struct in_addr address
;
3244 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
3246 fputs("IPV4LL_ADDRESS=", f
);
3247 serialize_in_addrs(f
, &address
, 1);
3252 r
= fflush_and_check(f
);
3256 if (rename(temp_path
, link
->state_file
) < 0) {
3264 (void) unlink(link
->state_file
);
3266 (void) unlink(temp_path
);
3268 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
3271 /* The serialized state in /run is no longer up-to-date. */
3272 void link_dirty(Link
*link
) {
3277 /* mark manager dirty as link is dirty */
3278 manager_dirty(link
->manager
);
3280 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
3282 /* allocation errors are ignored */
3285 r
= set_put(link
->manager
->dirty_links
, link
);
3287 /* don't take another ref if the link was already dirty */
3293 /* The serialized state in /run is up-to-date */
3294 void link_clean(Link
*link
) {
3296 assert(link
->manager
);
3298 set_remove(link
->manager
->dirty_links
, link
);
3302 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
3303 [LINK_STATE_PENDING
] = "pending",
3304 [LINK_STATE_ENSLAVING
] = "configuring",
3305 [LINK_STATE_SETTING_ADDRESSES
] = "configuring",
3306 [LINK_STATE_SETTING_ROUTES
] = "configuring",
3307 [LINK_STATE_CONFIGURED
] = "configured",
3308 [LINK_STATE_UNMANAGED
] = "unmanaged",
3309 [LINK_STATE_FAILED
] = "failed",
3310 [LINK_STATE_LINGER
] = "linger",
3313 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);
3315 static const char* const link_operstate_table
[_LINK_OPERSTATE_MAX
] = {
3316 [LINK_OPERSTATE_OFF
] = "off",
3317 [LINK_OPERSTATE_NO_CARRIER
] = "no-carrier",
3318 [LINK_OPERSTATE_DORMANT
] = "dormant",
3319 [LINK_OPERSTATE_CARRIER
] = "carrier",
3320 [LINK_OPERSTATE_DEGRADED
] = "degraded",
3321 [LINK_OPERSTATE_ROUTABLE
] = "routable",
3324 DEFINE_STRING_TABLE_LOOKUP(link_operstate
, LinkOperationalState
);