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-link.h"
32 #include "networkd-lldp-tx.h"
33 #include "networkd-netdev.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 return link_dhcp6_enabled(link
) || link_ipv6ll_enabled(link
) || network_has_static_ipv6_addresses(link
->network
);
117 static bool link_lldp_rx_enabled(Link
*link
) {
120 if (link
->flags
& IFF_LOOPBACK
)
123 if (link
->iftype
!= ARPHRD_ETHER
)
129 if (link
->network
->bridge
)
132 return link
->network
->lldp_mode
!= LLDP_MODE_NO
;
135 static bool link_lldp_tx_enabled(Link
*link
) {
138 if (link
->flags
& IFF_LOOPBACK
)
141 if (link
->iftype
!= ARPHRD_ETHER
)
147 return link
->network
->lldp_emit
;
150 static bool link_ipv4_forward_enabled(Link
*link
) {
153 if (link
->flags
& IFF_LOOPBACK
)
159 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
162 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV4
;
165 static bool link_ipv6_forward_enabled(Link
*link
) {
168 if (!socket_ipv6_is_supported())
171 if (link
->flags
& IFF_LOOPBACK
)
177 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
180 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV6
;
183 static bool link_proxy_arp_enabled(Link
*link
) {
186 if (link
->flags
& IFF_LOOPBACK
)
192 if (link
->network
->proxy_arp
< 0)
198 static bool link_ipv6_accept_ra_enabled(Link
*link
) {
201 if (!socket_ipv6_is_supported())
204 if (link
->flags
& IFF_LOOPBACK
)
210 /* If unset use system default (enabled if local forwarding is disabled.
211 * disabled if local forwarding is enabled).
212 * If set, ignore or enforce RA independent of local forwarding state.
214 if (link
->network
->ipv6_accept_ra
< 0)
215 /* default to accept RA if ip_forward is disabled and ignore RA if ip_forward is enabled */
216 return !link_ipv6_forward_enabled(link
);
217 else if (link
->network
->ipv6_accept_ra
> 0)
218 /* accept RA even if ip_forward is enabled */
225 static IPv6PrivacyExtensions
link_ipv6_privacy_extensions(Link
*link
) {
228 if (!socket_ipv6_is_supported())
229 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
231 if (link
->flags
& IFF_LOOPBACK
)
232 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
235 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
237 return link
->network
->ipv6_privacy_extensions
;
240 static int link_enable_ipv6(Link
*link
) {
241 const char *p
= NULL
;
245 if (link
->flags
& IFF_LOOPBACK
)
248 disabled
= !link_ipv6_enabled(link
);
250 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/disable_ipv6");
252 r
= write_string_file(p
, one_zero(disabled
), WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
254 log_link_warning_errno(link
, r
, "Cannot %s IPv6 for interface %s: %m", disabled
? "disable" : "enable", link
->ifname
);
257 log_link_info(link
, "IPv6 disabled for interface: %m");
259 log_link_info(link
, "IPv6 enabled for interface: %m");
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
;
397 unsigned short iftype
;
403 r
= sd_netlink_message_get_type(message
, &type
);
406 else if (type
!= RTM_NEWLINK
)
409 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
412 else if (ifindex
<= 0)
415 r
= sd_rtnl_message_link_get_type(message
, &iftype
);
419 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &ifname
);
423 link
= new0(Link
, 1);
428 link
->manager
= manager
;
429 link
->state
= LINK_STATE_PENDING
;
430 link
->rtnl_extended_attrs
= true;
431 link
->ifindex
= ifindex
;
432 link
->iftype
= iftype
;
433 link
->ifname
= strdup(ifname
);
437 r
= sd_netlink_message_read_ether_addr(message
, IFLA_ADDRESS
, &link
->mac
);
439 log_link_debug_errno(link
, r
, "MAC address not found for new device, continuing without");
441 if (asprintf(&link
->state_file
, "/run/systemd/netif/links/%d", link
->ifindex
) < 0)
444 if (asprintf(&link
->lease_file
, "/run/systemd/netif/leases/%d", link
->ifindex
) < 0)
447 if (asprintf(&link
->lldp_file
, "/run/systemd/netif/lldp/%d", link
->ifindex
) < 0)
450 r
= hashmap_ensure_allocated(&manager
->links
, NULL
);
454 r
= hashmap_put(manager
->links
, INT_TO_PTR(link
->ifindex
), link
);
458 r
= link_update_flags(link
, message
);
468 static void link_free(Link
*link
) {
476 while (!set_isempty(link
->addresses
))
477 address_free(set_first(link
->addresses
));
479 while (!set_isempty(link
->addresses_foreign
))
480 address_free(set_first(link
->addresses_foreign
));
482 link
->addresses
= set_free(link
->addresses
);
484 link
->addresses_foreign
= set_free(link
->addresses_foreign
);
486 while ((address
= link
->pool_addresses
)) {
487 LIST_REMOVE(addresses
, link
->pool_addresses
, address
);
488 address_free(address
);
491 sd_dhcp_server_unref(link
->dhcp_server
);
492 sd_dhcp_client_unref(link
->dhcp_client
);
493 sd_dhcp_lease_unref(link
->dhcp_lease
);
495 link_lldp_tx_stop(link
);
497 free(link
->lease_file
);
499 sd_lldp_unref(link
->lldp
);
500 free(link
->lldp_file
);
502 sd_ipv4ll_unref(link
->ipv4ll
);
503 sd_dhcp6_client_unref(link
->dhcp6_client
);
504 sd_ndisc_unref(link
->ndisc_router_discovery
);
507 hashmap_remove(link
->manager
->links
, INT_TO_PTR(link
->ifindex
));
511 (void)unlink(link
->state_file
);
512 free(link
->state_file
);
514 udev_device_unref(link
->udev_device
);
516 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
)
517 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(carrier
->ifindex
));
518 hashmap_free(link
->bound_to_links
);
520 HASHMAP_FOREACH (carrier
, link
->bound_by_links
, i
)
521 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(carrier
->ifindex
));
522 hashmap_free(link
->bound_by_links
);
527 Link
*link_unref(Link
*link
) {
531 assert(link
->n_ref
> 0);
543 Link
*link_ref(Link
*link
) {
547 assert(link
->n_ref
> 0);
554 int link_get(Manager
*m
, int ifindex
, Link
**ret
) {
561 link
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
570 static void link_set_state(Link
*link
, LinkState state
) {
573 if (link
->state
== state
)
578 link_send_changed(link
, "AdministrativeState", NULL
);
581 static void link_enter_unmanaged(Link
*link
) {
584 log_link_debug(link
, "Unmanaged");
586 link_set_state(link
, LINK_STATE_UNMANAGED
);
591 static int link_stop_clients(Link
*link
) {
595 assert(link
->manager
);
596 assert(link
->manager
->event
);
598 if (link
->dhcp_client
) {
599 k
= sd_dhcp_client_stop(link
->dhcp_client
);
601 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv4 client: %m");
605 k
= sd_ipv4ll_stop(link
->ipv4ll
);
607 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 link-local: %m");
610 if (link
->dhcp6_client
) {
611 k
= sd_dhcp6_client_stop(link
->dhcp6_client
);
613 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv6 client: %m");
616 if (link
->ndisc_router_discovery
) {
617 k
= sd_ndisc_stop(link
->ndisc_router_discovery
);
619 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Discovery: %m");
622 link_lldp_tx_stop(link
);
626 void link_enter_failed(Link
*link
) {
629 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
632 log_link_warning(link
, "Failed");
634 link_set_state(link
, LINK_STATE_FAILED
);
636 link_stop_clients(link
);
641 static Address
* link_find_dhcp_server_address(Link
*link
) {
645 assert(link
->network
);
647 /* The first statically configured address if there is any */
648 LIST_FOREACH(addresses
, address
, link
->network
->static_addresses
) {
650 if (address
->family
!= AF_INET
)
653 if (in_addr_is_null(address
->family
, &address
->in_addr
))
659 /* If that didn't work, find a suitable address we got from the pool */
660 LIST_FOREACH(addresses
, address
, link
->pool_addresses
) {
661 if (address
->family
!= AF_INET
)
670 static int link_enter_configured(Link
*link
) {
672 assert(link
->network
);
673 assert(link
->state
== LINK_STATE_SETTING_ROUTES
);
675 log_link_info(link
, "Configured");
677 link_set_state(link
, LINK_STATE_CONFIGURED
);
684 void link_check_ready(Link
*link
) {
693 if (!link
->static_configured
)
696 if (link_ipv4ll_enabled(link
))
697 if (!link
->ipv4ll_address
||
701 if (link_ipv6ll_enabled(link
))
702 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) > 0)
705 if ((link_dhcp4_enabled(link
) && !link_dhcp6_enabled(link
) &&
706 !link
->dhcp4_configured
) ||
707 (link_dhcp6_enabled(link
) && !link_dhcp4_enabled(link
) &&
708 !link
->dhcp6_configured
) ||
709 (link_dhcp4_enabled(link
) && link_dhcp6_enabled(link
) &&
710 !link
->dhcp4_configured
&& !link
->dhcp6_configured
))
713 if (link_ipv6_accept_ra_enabled(link
) && !link
->ndisc_configured
)
716 SET_FOREACH(a
, link
->addresses
, i
)
717 if (!address_is_ready(a
))
720 if (link
->state
!= LINK_STATE_CONFIGURED
)
721 link_enter_configured(link
);
726 static int route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
727 _cleanup_link_unref_ Link
*link
= userdata
;
730 assert(link
->link_messages
> 0);
731 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
732 LINK_STATE_SETTING_ROUTES
, LINK_STATE_FAILED
,
735 link
->link_messages
--;
737 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
740 r
= sd_netlink_message_get_errno(m
);
741 if (r
< 0 && r
!= -EEXIST
)
742 log_link_warning_errno(link
, r
, "Could not set route: %m");
744 if (link
->link_messages
== 0) {
745 log_link_debug(link
, "Routes set");
746 link
->static_configured
= true;
747 link_check_ready(link
);
753 static int link_enter_set_routes(Link
*link
) {
758 assert(link
->network
);
759 assert(link
->state
== LINK_STATE_SETTING_ADDRESSES
);
761 link_set_state(link
, LINK_STATE_SETTING_ROUTES
);
763 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
764 r
= route_configure(rt
, link
, &route_handler
);
766 log_link_warning_errno(link
, r
, "Could not set routes: %m");
767 link_enter_failed(link
);
771 link
->link_messages
++;
774 if (link
->link_messages
== 0) {
775 link
->static_configured
= true;
776 link_check_ready(link
);
778 log_link_debug(link
, "Setting routes");
783 int link_route_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
784 _cleanup_link_unref_ Link
*link
= userdata
;
789 assert(link
->ifname
);
791 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
794 r
= sd_netlink_message_get_errno(m
);
795 if (r
< 0 && r
!= -ESRCH
)
796 log_link_warning_errno(link
, r
, "Could not drop route: %m");
801 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
802 _cleanup_link_unref_ Link
*link
= userdata
;
808 assert(link
->ifname
);
809 assert(link
->link_messages
> 0);
810 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
811 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
813 link
->link_messages
--;
815 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
818 r
= sd_netlink_message_get_errno(m
);
819 if (r
< 0 && r
!= -EEXIST
)
820 log_link_warning_errno(link
, r
, "could not set address: %m");
822 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
824 if (link
->link_messages
== 0) {
825 log_link_debug(link
, "Addresses set");
826 link_enter_set_routes(link
);
832 static int link_push_dns_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
833 _cleanup_free_
struct in_addr
*addresses
= NULL
;
834 size_t n_addresses
= 0, n_allocated
= 0;
837 log_debug("Copying DNS server information from %s", link
->ifname
);
842 STRV_FOREACH(a
, link
->network
->dns
) {
845 /* Only look for IPv4 addresses */
846 if (inet_pton(AF_INET
, *a
, &ia
) <= 0)
849 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
852 addresses
[n_addresses
++] = ia
;
855 if (link
->network
->dhcp_use_dns
&&
857 const struct in_addr
*da
= NULL
;
860 n
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &da
);
863 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
866 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
871 if (n_addresses
<= 0)
874 return sd_dhcp_server_set_dns(s
, addresses
, n_addresses
);
877 static int link_push_ntp_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
878 _cleanup_free_
struct in_addr
*addresses
= NULL
;
879 size_t n_addresses
= 0, n_allocated
= 0;
885 log_debug("Copying NTP server information from %s", link
->ifname
);
887 STRV_FOREACH(a
, link
->network
->ntp
) {
890 /* Only look for IPv4 addresses */
891 if (inet_pton(AF_INET
, *a
, &ia
) <= 0)
894 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
897 addresses
[n_addresses
++] = ia
;
900 if (link
->network
->dhcp_use_ntp
&&
902 const struct in_addr
*da
= NULL
;
905 n
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &da
);
908 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
911 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
916 if (n_addresses
<= 0)
919 return sd_dhcp_server_set_ntp(s
, addresses
, n_addresses
);
922 static int link_enter_set_addresses(Link
*link
) {
927 assert(link
->network
);
928 assert(link
->state
!= _LINK_STATE_INVALID
);
930 link_set_state(link
, LINK_STATE_SETTING_ADDRESSES
);
932 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
933 r
= address_configure(ad
, link
, &address_handler
, false);
935 log_link_warning_errno(link
, r
, "Could not set addresses: %m");
936 link_enter_failed(link
);
940 link
->link_messages
++;
943 /* now that we can figure out a default address for the dhcp server,
945 if (link_dhcp4_server_enabled(link
)) {
948 bool acquired_uplink
= false;
950 address
= link_find_dhcp_server_address(link
);
952 log_link_warning(link
, "Failed to find suitable address for DHCPv4 server instance.");
953 link_enter_failed(link
);
957 /* use the server address' subnet as the pool */
958 r
= sd_dhcp_server_configure_pool(link
->dhcp_server
, &address
->in_addr
.in
, address
->prefixlen
,
959 link
->network
->dhcp_server_pool_offset
, link
->network
->dhcp_server_pool_size
);
964 r = sd_dhcp_server_set_router(link->dhcp_server,
965 &main_address->in_addr.in);
970 if (link
->network
->dhcp_server_max_lease_time_usec
> 0) {
971 r
= sd_dhcp_server_set_max_lease_time(
973 DIV_ROUND_UP(link
->network
->dhcp_server_max_lease_time_usec
, USEC_PER_SEC
));
978 if (link
->network
->dhcp_server_default_lease_time_usec
> 0) {
979 r
= sd_dhcp_server_set_default_lease_time(
981 DIV_ROUND_UP(link
->network
->dhcp_server_default_lease_time_usec
, USEC_PER_SEC
));
986 if (link
->network
->dhcp_server_emit_dns
) {
988 if (link
->network
->n_dhcp_server_dns
> 0)
989 r
= sd_dhcp_server_set_dns(link
->dhcp_server
, link
->network
->dhcp_server_dns
, link
->network
->n_dhcp_server_dns
);
991 uplink
= manager_find_uplink(link
->manager
, link
);
992 acquired_uplink
= true;
995 log_link_debug(link
, "Not emitting DNS server information on link, couldn't find suitable uplink.");
998 r
= link_push_dns_to_dhcp_server(uplink
, link
->dhcp_server
);
1001 log_link_warning_errno(link
, r
, "Failed to set DNS server for DHCP server, ignoring: %m");
1005 if (link
->network
->dhcp_server_emit_ntp
) {
1007 if (link
->network
->n_dhcp_server_ntp
> 0)
1008 r
= sd_dhcp_server_set_ntp(link
->dhcp_server
, link
->network
->dhcp_server_ntp
, link
->network
->n_dhcp_server_ntp
);
1010 if (!acquired_uplink
)
1011 uplink
= manager_find_uplink(link
->manager
, link
);
1014 log_link_debug(link
, "Not emitting NTP server information on link, couldn't find suitable uplink.");
1017 r
= link_push_ntp_to_dhcp_server(uplink
, link
->dhcp_server
);
1021 log_link_warning_errno(link
, r
, "Failed to set NTP server for DHCP server, ignoring: %m");
1024 if (link
->network
->dhcp_server_emit_timezone
) {
1025 _cleanup_free_
char *buffer
= NULL
;
1026 const char *tz
= NULL
;
1028 if (link
->network
->dhcp_server_timezone
)
1029 tz
= link
->network
->dhcp_server_timezone
;
1031 r
= get_timezone(&buffer
);
1033 log_warning_errno(r
, "Failed to determine timezone: %m");
1039 r
= sd_dhcp_server_set_timezone(link
->dhcp_server
, tz
);
1045 r
= sd_dhcp_server_start(link
->dhcp_server
);
1047 log_link_warning_errno(link
, r
, "Could not start DHCPv4 server instance: %m");
1049 link_enter_failed(link
);
1054 log_link_debug(link
, "Offering DHCPv4 leases");
1057 if (link
->link_messages
== 0)
1058 link_enter_set_routes(link
);
1060 log_link_debug(link
, "Setting addresses");
1065 int link_address_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1066 _cleanup_link_unref_ Link
*link
= userdata
;
1071 assert(link
->ifname
);
1073 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1076 r
= sd_netlink_message_get_errno(m
);
1077 if (r
< 0 && r
!= -EADDRNOTAVAIL
)
1078 log_link_warning_errno(link
, r
, "Could not drop address: %m");
1083 static int link_set_bridge_fdb(Link
*const link
) {
1084 FdbEntry
*fdb_entry
;
1087 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
1088 r
= fdb_entry_configure(link
, fdb_entry
);
1090 log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
1098 static int link_set_proxy_arp(Link
*const link
) {
1099 const char *p
= NULL
;
1102 if (!link_proxy_arp_enabled(link
))
1105 p
= strjoina("/proc/sys/net/ipv4/conf/", link
->ifname
, "/proxy_arp");
1107 r
= write_string_file(p
, one_zero(link
->network
->proxy_arp
), WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
1109 log_link_warning_errno(link
, r
, "Cannot configure proxy ARP for interface: %m");
1114 static int link_set_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1115 _cleanup_link_unref_ Link
*link
= userdata
;
1118 log_link_debug(link
, "Set link");
1120 r
= sd_netlink_message_get_errno(m
);
1121 if (r
< 0 && r
!= -EEXIST
) {
1122 log_link_error_errno(link
, r
, "Could not join netdev: %m");
1123 link_enter_failed(link
);
1130 static int set_hostname_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
1131 _cleanup_link_unref_ Link
*link
= userdata
;
1132 const sd_bus_error
*e
;
1137 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1140 e
= sd_bus_message_get_error(m
);
1142 log_link_warning_errno(link
, sd_bus_error_get_errno(e
), "Could not set hostname: %s", e
->message
);
1147 int link_set_hostname(Link
*link
, const char *hostname
) {
1151 assert(link
->manager
);
1153 log_link_debug(link
, "Setting transient hostname: '%s'", strna(hostname
));
1155 if (!link
->manager
->bus
) {
1156 /* TODO: replace by assert when we can rely on kdbus */
1157 log_link_info(link
, "Not connected to system bus, ignoring transient hostname.");
1161 r
= sd_bus_call_method_async(
1164 "org.freedesktop.hostname1",
1165 "/org/freedesktop/hostname1",
1166 "org.freedesktop.hostname1",
1168 set_hostname_handler
,
1175 return log_link_error_errno(link
, r
, "Could not set transient hostname: %m");
1182 static int set_timezone_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
1183 _cleanup_link_unref_ Link
*link
= userdata
;
1184 const sd_bus_error
*e
;
1189 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1192 e
= sd_bus_message_get_error(m
);
1194 log_link_warning_errno(link
, sd_bus_error_get_errno(e
), "Could not set timezone: %s", e
->message
);
1199 int link_set_timezone(Link
*link
, const char *tz
) {
1203 assert(link
->manager
);
1206 log_link_debug(link
, "Setting system timezone: '%s'", tz
);
1208 if (!link
->manager
->bus
) {
1209 log_link_info(link
, "Not connected to system bus, ignoring timezone.");
1213 r
= sd_bus_call_method_async(
1216 "org.freedesktop.timedate1",
1217 "/org/freedesktop/timedate1",
1218 "org.freedesktop.timedate1",
1220 set_timezone_handler
,
1226 return log_link_error_errno(link
, r
, "Could not set timezone: %m");
1233 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1234 _cleanup_link_unref_ Link
*link
= userdata
;
1239 assert(link
->ifname
);
1241 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1244 r
= sd_netlink_message_get_errno(m
);
1246 log_link_warning_errno(link
, r
, "Could not set MTU: %m");
1251 int link_set_mtu(Link
*link
, uint32_t mtu
) {
1252 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1256 assert(link
->manager
);
1257 assert(link
->manager
->rtnl
);
1259 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1261 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1263 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1265 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1267 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1269 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, set_mtu_handler
, link
, 0, NULL
);
1271 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1278 static int link_set_bridge(Link
*link
) {
1279 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1283 assert(link
->network
);
1285 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1287 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1289 r
= sd_rtnl_message_link_set_family(req
, PF_BRIDGE
);
1291 return log_link_error_errno(link
, r
, "Could not set message family: %m");
1293 r
= sd_netlink_message_open_container(req
, IFLA_PROTINFO
);
1295 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1297 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_GUARD
, !link
->network
->use_bpdu
);
1299 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_GUARD attribute: %m");
1301 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MODE
, link
->network
->hairpin
);
1303 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MODE attribute: %m");
1305 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_FAST_LEAVE
, link
->network
->fast_leave
);
1307 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_FAST_LEAVE attribute: %m");
1309 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_PROTECT
, !link
->network
->allow_port_to_be_root
);
1311 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PROTECT attribute: %m");
1313 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_UNICAST_FLOOD
, link
->network
->unicast_flood
);
1315 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_UNICAST_FLOOD attribute: %m");
1317 if (link
->network
->cost
!= 0) {
1318 r
= sd_netlink_message_append_u32(req
, IFLA_BRPORT_COST
, link
->network
->cost
);
1320 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_COST attribute: %m");
1323 r
= sd_netlink_message_close_container(req
);
1325 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1327 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_set_handler
, link
, 0, NULL
);
1329 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1336 static int link_lldp_save(Link
*link
) {
1337 _cleanup_free_
char *temp_path
= NULL
;
1338 _cleanup_fclose_
FILE *f
= NULL
;
1339 sd_lldp_neighbor
**l
= NULL
;
1343 assert(link
->lldp_file
);
1346 (void) unlink(link
->lldp_file
);
1350 r
= sd_lldp_get_neighbors(link
->lldp
, &l
);
1354 (void) unlink(link
->lldp_file
);
1360 r
= fopen_temporary(link
->lldp_file
, &f
, &temp_path
);
1364 fchmod(fileno(f
), 0644);
1366 for (i
= 0; i
< n
; i
++) {
1371 r
= sd_lldp_neighbor_get_raw(l
[i
], &p
, &sz
);
1376 (void) fwrite(&u
, 1, sizeof(u
), f
);
1377 (void) fwrite(p
, 1, sz
, f
);
1380 r
= fflush_and_check(f
);
1384 if (rename(temp_path
, link
->lldp_file
) < 0) {
1391 (void) unlink(link
->lldp_file
);
1393 (void) unlink(temp_path
);
1395 log_link_error_errno(link
, r
, "Failed to save LLDP data to %s: %m", link
->lldp_file
);
1399 for (i
= 0; i
< n
; i
++)
1400 sd_lldp_neighbor_unref(l
[i
]);
1407 static void lldp_handler(sd_lldp
*lldp
, sd_lldp_event event
, sd_lldp_neighbor
*n
, void *userdata
) {
1408 Link
*link
= userdata
;
1413 (void) link_lldp_save(link
);
1415 if (link_lldp_tx_enabled(link
) && event
== SD_LLDP_EVENT_ADDED
) {
1416 /* If we received information about a new neighbor, restart the LLDP "fast" logic */
1418 log_link_debug(link
, "Received LLDP datagram from previously unknown neighbor, restarting 'fast' LLDP transmission.");
1420 r
= link_lldp_tx_start(link
);
1422 log_link_warning_errno(link
, r
, "Failed to restart LLDP transmission: %m");
1426 static int link_acquire_ipv6_conf(Link
*link
) {
1431 if (link_dhcp6_enabled(link
)) {
1432 assert(link
->dhcp6_client
);
1433 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1435 log_link_debug(link
, "Acquiring DHCPv6 lease");
1437 r
= sd_dhcp6_client_set_local_address(link
->dhcp6_client
, &link
->ipv6ll_address
);
1438 if (r
< 0 && r
!= -EBUSY
)
1439 return log_link_warning_errno(link
, r
, "Could not set IPv6LL address in DHCP client: %m");
1441 r
= sd_dhcp6_client_start(link
->dhcp6_client
);
1442 if (r
< 0 && r
!= -EBUSY
)
1443 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv6 lease: %m");
1446 if (link_ipv6_accept_ra_enabled(link
)) {
1447 assert(link
->ndisc_router_discovery
);
1449 log_link_debug(link
, "Discovering IPv6 routers");
1451 r
= sd_ndisc_router_discovery_start(link
->ndisc_router_discovery
);
1452 if (r
< 0 && r
!= -EBUSY
)
1453 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1459 static int link_acquire_ipv4_conf(Link
*link
) {
1463 assert(link
->network
);
1464 assert(link
->manager
);
1465 assert(link
->manager
->event
);
1467 if (link_ipv4ll_enabled(link
)) {
1468 assert(link
->ipv4ll
);
1470 log_link_debug(link
, "Acquiring IPv4 link-local address");
1472 r
= sd_ipv4ll_start(link
->ipv4ll
);
1474 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1477 if (link_dhcp4_enabled(link
)) {
1478 assert(link
->dhcp_client
);
1480 log_link_debug(link
, "Acquiring DHCPv4 lease");
1482 r
= sd_dhcp_client_start(link
->dhcp_client
);
1484 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1490 static int link_acquire_conf(Link
*link
) {
1495 r
= link_acquire_ipv4_conf(link
);
1499 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) == 0) {
1500 r
= link_acquire_ipv6_conf(link
);
1505 if (link_lldp_tx_enabled(link
)) {
1506 r
= link_lldp_tx_start(link
);
1508 return log_link_warning_errno(link
, r
, "Failed to start LLDP transmission: %m");
1514 bool link_has_carrier(Link
*link
) {
1515 /* see Documentation/networking/operstates.txt in the kernel sources */
1517 if (link
->kernel_operstate
== IF_OPER_UP
)
1520 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1521 /* operstate may not be implemented, so fall back to flags */
1522 if ((link
->flags
& IFF_LOWER_UP
) && !(link
->flags
& IFF_DORMANT
))
1528 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1529 _cleanup_link_unref_ Link
*link
= userdata
;
1534 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1537 r
= sd_netlink_message_get_errno(m
);
1539 /* we warn but don't fail the link, as it may be
1541 log_link_warning_errno(link
, r
, "Could not bring up interface: %m");
1546 static int link_up(Link
*link
) {
1547 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1548 uint8_t ipv6ll_mode
;
1552 assert(link
->network
);
1553 assert(link
->manager
);
1554 assert(link
->manager
->rtnl
);
1556 log_link_debug(link
, "Bringing link up");
1558 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1560 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1562 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1564 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1566 if (link
->network
->mac
) {
1567 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
1569 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
1572 /* If IPv6 not configured (no static IPv6 address and neither DHCPv6 nor IPv6LL is enabled)
1573 for this interface then disable IPv6 else enable it. */
1574 (void) link_enable_ipv6(link
);
1576 if (link
->network
->mtu
) {
1577 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
1578 on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */
1579 if (link_ipv6_enabled(link
) && link
->network
->mtu
< IPV6_MIN_MTU
) {
1581 log_link_warning(link
, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU
) ", as "
1582 "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU
) " bytes: %m");
1584 link
->network
->mtu
= IPV6_MIN_MTU
;
1587 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, link
->network
->mtu
);
1589 return log_link_error_errno(link
, r
, "Could not set MTU: %m");
1592 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1594 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1596 if (link_ipv6_enabled(link
)) {
1597 /* if the kernel lacks ipv6 support setting IFF_UP fails if any ipv6 options are passed */
1598 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1600 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1602 ipv6ll_mode
= link_ipv6ll_enabled(link
) ? IN6_ADDR_GEN_MODE_EUI64
: IN6_ADDR_GEN_MODE_NONE
;
1603 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1605 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1607 if (!in_addr_is_null(AF_INET6
, &link
->network
->ipv6_token
)) {
1608 r
= sd_netlink_message_append_in6_addr(req
, IFLA_INET6_TOKEN
, &link
->network
->ipv6_token
.in6
);
1610 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_TOKEN: %m");
1613 r
= sd_netlink_message_close_container(req
);
1615 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1618 r
= sd_netlink_message_close_container(req
);
1620 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1622 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_up_handler
, link
, 0, NULL
);
1624 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1631 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1632 _cleanup_link_unref_ Link
*link
= userdata
;
1637 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1640 r
= sd_netlink_message_get_errno(m
);
1642 log_link_warning_errno(link
, r
, "Could not bring down interface: %m");
1647 static int link_down(Link
*link
) {
1648 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1652 assert(link
->manager
);
1653 assert(link
->manager
->rtnl
);
1655 log_link_debug(link
, "Bringing link down");
1657 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
1658 RTM_SETLINK
, link
->ifindex
);
1660 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1662 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
1664 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1666 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_down_handler
, link
, 0, NULL
);
1668 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1675 static int link_handle_bound_to_list(Link
*link
) {
1679 bool required_up
= false;
1680 bool link_is_up
= false;
1684 if (hashmap_isempty(link
->bound_to_links
))
1687 if (link
->flags
& IFF_UP
)
1690 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
1691 if (link_has_carrier(l
)) {
1696 if (!required_up
&& link_is_up
) {
1697 r
= link_down(link
);
1700 } else if (required_up
&& !link_is_up
) {
1709 static int link_handle_bound_by_list(Link
*link
) {
1716 if (hashmap_isempty(link
->bound_by_links
))
1719 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
1720 r
= link_handle_bound_to_list(l
);
1728 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
1734 if (link
== carrier
)
1737 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
1740 r
= hashmap_ensure_allocated(h
, NULL
);
1744 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
1751 static int link_new_bound_by_list(Link
*link
) {
1756 bool list_updated
= false;
1759 assert(link
->manager
);
1763 HASHMAP_FOREACH(carrier
, m
->links
, i
) {
1764 if (!carrier
->network
)
1767 if (strv_isempty(carrier
->network
->bind_carrier
))
1770 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
, 0)) {
1771 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
1775 list_updated
= true;
1782 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
1783 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
1787 link_dirty(carrier
);
1793 static int link_new_bound_to_list(Link
*link
) {
1798 bool list_updated
= false;
1801 assert(link
->manager
);
1806 if (strv_isempty(link
->network
->bind_carrier
))
1811 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
1812 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
, 0)) {
1813 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
1817 list_updated
= true;
1824 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
1825 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
1829 link_dirty(carrier
);
1835 static int link_new_carrier_maps(Link
*link
) {
1838 r
= link_new_bound_by_list(link
);
1842 r
= link_handle_bound_by_list(link
);
1846 r
= link_new_bound_to_list(link
);
1850 r
= link_handle_bound_to_list(link
);
1857 static void link_free_bound_to_list(Link
*link
) {
1861 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
1862 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
1864 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
1865 link_dirty(bound_to
);
1871 static void link_free_bound_by_list(Link
*link
) {
1875 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
1876 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
1878 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
1879 link_dirty(bound_by
);
1880 link_handle_bound_to_list(bound_by
);
1887 static void link_free_carrier_maps(Link
*link
) {
1888 bool list_updated
= false;
1892 if (!hashmap_isempty(link
->bound_to_links
)) {
1893 link_free_bound_to_list(link
);
1894 list_updated
= true;
1897 if (!hashmap_isempty(link
->bound_by_links
)) {
1898 link_free_bound_by_list(link
);
1899 list_updated
= true;
1908 void link_drop(Link
*link
) {
1909 if (!link
|| link
->state
== LINK_STATE_LINGER
)
1912 link_set_state(link
, LINK_STATE_LINGER
);
1914 link_free_carrier_maps(link
);
1916 log_link_debug(link
, "Link removed");
1918 (void)unlink(link
->state_file
);
1924 static int link_joined(Link
*link
) {
1928 assert(link
->network
);
1930 if (!hashmap_isempty(link
->bound_to_links
)) {
1931 r
= link_handle_bound_to_list(link
);
1934 } else if (!(link
->flags
& IFF_UP
)) {
1937 link_enter_failed(link
);
1942 if (link
->network
->bridge
) {
1943 r
= link_set_bridge(link
);
1945 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
1948 return link_enter_set_addresses(link
);
1951 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1952 _cleanup_link_unref_ Link
*link
= userdata
;
1956 assert(link
->network
);
1960 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1963 r
= sd_netlink_message_get_errno(m
);
1964 if (r
< 0 && r
!= -EEXIST
) {
1965 log_link_error_errno(link
, r
, "Could not join netdev: %m");
1966 link_enter_failed(link
);
1969 log_link_debug(link
, "Joined netdev");
1971 if (link
->enslaving
<= 0)
1977 static int link_enter_join_netdev(Link
*link
) {
1983 assert(link
->network
);
1984 assert(link
->state
== LINK_STATE_PENDING
);
1986 link_set_state(link
, LINK_STATE_ENSLAVING
);
1990 if (!link
->network
->bridge
&&
1991 !link
->network
->bond
&&
1992 hashmap_isempty(link
->network
->stacked_netdevs
))
1993 return link_joined(link
);
1995 if (link
->network
->bond
) {
1996 log_struct(LOG_DEBUG
,
1997 LOG_LINK_INTERFACE(link
),
1998 LOG_NETDEV_INTERFACE(link
->network
->bond
),
1999 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
),
2002 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
2004 log_struct_errno(LOG_WARNING
, r
,
2005 LOG_LINK_INTERFACE(link
),
2006 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2007 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
),
2010 link_enter_failed(link
);
2017 if (link
->network
->bridge
) {
2018 log_struct(LOG_DEBUG
,
2019 LOG_LINK_INTERFACE(link
),
2020 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2021 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
),
2024 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
2026 log_struct_errno(LOG_WARNING
, r
,
2027 LOG_LINK_INTERFACE(link
),
2028 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2029 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
),
2031 link_enter_failed(link
);
2038 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
2040 log_struct(LOG_DEBUG
,
2041 LOG_LINK_INTERFACE(link
),
2042 LOG_NETDEV_INTERFACE(netdev
),
2043 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
),
2046 r
= netdev_join(netdev
, link
, netdev_join_handler
);
2048 log_struct_errno(LOG_WARNING
, r
,
2049 LOG_LINK_INTERFACE(link
),
2050 LOG_NETDEV_INTERFACE(netdev
),
2051 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
),
2053 link_enter_failed(link
);
2063 static int link_set_ipv4_forward(Link
*link
) {
2066 if (!link_ipv4_forward_enabled(link
))
2069 /* We propagate the forwarding flag from one interface to the
2070 * global setting one way. This means: as long as at least one
2071 * interface was configured at any time that had IP forwarding
2072 * enabled the setting will stay on for good. We do this
2073 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
2074 * somewhat in sync (see below). */
2076 r
= write_string_file("/proc/sys/net/ipv4/ip_forward", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2078 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
2083 static int link_set_ipv6_forward(Link
*link
) {
2086 if (!link_ipv6_forward_enabled(link
))
2089 /* On Linux, the IPv6 stack does not not know a per-interface
2090 * packet forwarding setting: either packet forwarding is on
2091 * for all, or off for all. We hence don't bother with a
2092 * per-interface setting, but simply propagate the interface
2093 * flag, if it is set, to the global flag, one-way. Note that
2094 * while IPv4 would allow a per-interface flag, we expose the
2095 * same behaviour there and also propagate the setting from
2096 * one to all, to keep things simple (see above). */
2098 r
= write_string_file("/proc/sys/net/ipv6/conf/all/forwarding", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2100 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
2105 static int link_set_ipv6_privacy_extensions(Link
*link
) {
2106 char buf
[DECIMAL_STR_MAX(unsigned) + 1];
2107 IPv6PrivacyExtensions s
;
2108 const char *p
= NULL
;
2111 s
= link_ipv6_privacy_extensions(link
);
2115 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/use_tempaddr");
2116 xsprintf(buf
, "%u", (unsigned) link
->network
->ipv6_privacy_extensions
);
2118 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2120 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
2125 static int link_set_ipv6_accept_ra(Link
*link
) {
2126 const char *p
= NULL
;
2129 /* Make this a NOP if IPv6 is not available */
2130 if (!socket_ipv6_is_supported())
2133 if (link
->flags
& IFF_LOOPBACK
)
2139 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/accept_ra");
2141 /* We handle router advertisments ourselves, tell the kernel to GTFO */
2142 r
= write_string_file(p
, "0", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2144 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
2149 static int link_set_ipv6_dad_transmits(Link
*link
) {
2150 char buf
[DECIMAL_STR_MAX(int) + 1];
2151 const char *p
= NULL
;
2154 /* Make this a NOP if IPv6 is not available */
2155 if (!socket_ipv6_is_supported())
2158 if (link
->flags
& IFF_LOOPBACK
)
2164 if (link
->network
->ipv6_dad_transmits
< 0)
2167 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/dad_transmits");
2168 xsprintf(buf
, "%i", link
->network
->ipv6_dad_transmits
);
2170 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2172 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
2177 static int link_set_ipv6_hop_limit(Link
*link
) {
2178 char buf
[DECIMAL_STR_MAX(int) + 1];
2179 const char *p
= NULL
;
2182 /* Make this a NOP if IPv6 is not available */
2183 if (!socket_ipv6_is_supported())
2186 if (link
->flags
& IFF_LOOPBACK
)
2192 if (link
->network
->ipv6_hop_limit
< 0)
2195 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/hop_limit");
2196 xsprintf(buf
, "%i", link
->network
->ipv6_hop_limit
);
2198 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2200 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2205 static int link_drop_foreign_config(Link
*link
) {
2211 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
2212 /* we consider IPv6LL addresses to be managed by the kernel */
2213 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2216 r
= address_remove(address
, link
, link_address_remove_handler
);
2221 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2222 /* do not touch routes managed by the kernel */
2223 if (route
->protocol
== RTPROT_KERNEL
)
2226 r
= route_remove(route
, link
, link_address_remove_handler
);
2234 static int link_update_lldp(Link
*link
) {
2242 if (link
->flags
& IFF_UP
) {
2243 r
= sd_lldp_start(link
->lldp
);
2245 log_link_debug(link
, "Started LLDP.");
2247 r
= sd_lldp_stop(link
->lldp
);
2249 log_link_debug(link
, "Stopped LLDP.");
2255 static int link_configure(Link
*link
) {
2259 assert(link
->network
);
2260 assert(link
->state
== LINK_STATE_PENDING
);
2262 /* Drop foreign config, but ignore loopback or critical devices.
2263 * We do not want to remove loopback address or addresses used for root NFS. */
2264 if (!(link
->flags
& IFF_LOOPBACK
) && !(link
->network
->dhcp_critical
)) {
2265 r
= link_drop_foreign_config(link
);
2270 r
= link_set_bridge_fdb(link
);
2274 r
= link_set_proxy_arp(link
);
2278 r
= link_set_ipv4_forward(link
);
2282 r
= link_set_ipv6_forward(link
);
2286 r
= link_set_ipv6_privacy_extensions(link
);
2290 r
= link_set_ipv6_accept_ra(link
);
2294 r
= link_set_ipv6_dad_transmits(link
);
2298 r
= link_set_ipv6_hop_limit(link
);
2302 if (link_ipv4ll_enabled(link
)) {
2303 r
= ipv4ll_configure(link
);
2308 if (link_dhcp4_enabled(link
)) {
2309 r
= dhcp4_configure(link
);
2314 if (link_dhcp4_server_enabled(link
)) {
2315 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
2319 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
2324 if (link_dhcp6_enabled(link
) ||
2325 link_ipv6_accept_ra_enabled(link
)) {
2326 r
= dhcp6_configure(link
);
2331 if (link_ipv6_accept_ra_enabled(link
)) {
2332 r
= ndisc_configure(link
);
2337 if (link_lldp_rx_enabled(link
)) {
2338 r
= sd_lldp_new(&link
->lldp
, link
->ifindex
);
2342 r
= sd_lldp_match_capabilities(link
->lldp
,
2343 link
->network
->lldp_mode
== LLDP_MODE_ROUTERS_ONLY
?
2344 SD_LLDP_SYSTEM_CAPABILITIES_ALL_ROUTERS
:
2345 SD_LLDP_SYSTEM_CAPABILITIES_ALL
);
2349 r
= sd_lldp_set_filter_address(link
->lldp
, &link
->mac
);
2353 r
= sd_lldp_attach_event(link
->lldp
, NULL
, 0);
2357 r
= sd_lldp_set_callback(link
->lldp
, lldp_handler
, link
);
2361 r
= link_update_lldp(link
);
2366 if (link_has_carrier(link
)) {
2367 r
= link_acquire_conf(link
);
2372 return link_enter_join_netdev(link
);
2375 static int link_initialized_and_synced(sd_netlink
*rtnl
, sd_netlink_message
*m
,
2377 _cleanup_link_unref_ Link
*link
= userdata
;
2382 assert(link
->ifname
);
2383 assert(link
->manager
);
2385 if (link
->state
!= LINK_STATE_PENDING
)
2388 log_link_debug(link
, "Link state is up-to-date");
2390 r
= link_new_bound_by_list(link
);
2394 r
= link_handle_bound_by_list(link
);
2398 if (!link
->network
) {
2399 r
= network_get(link
->manager
, link
->udev_device
, link
->ifname
,
2400 &link
->mac
, &network
);
2402 link_enter_unmanaged(link
);
2407 if (link
->flags
& IFF_LOOPBACK
) {
2408 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
2409 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
2411 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
2412 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
2414 if (network
->dhcp_server
)
2415 log_link_debug(link
, "Ignoring DHCP server for loopback link");
2418 r
= network_apply(link
->manager
, network
, link
);
2423 r
= link_new_bound_to_list(link
);
2427 r
= link_configure(link
);
2434 int link_initialized(Link
*link
, struct udev_device
*device
) {
2435 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2439 assert(link
->manager
);
2440 assert(link
->manager
->rtnl
);
2443 if (link
->state
!= LINK_STATE_PENDING
)
2446 if (link
->udev_device
)
2449 log_link_debug(link
, "udev initialized link");
2451 link
->udev_device
= udev_device_ref(device
);
2453 /* udev has initialized the link, but we don't know if we have yet
2454 * processed the NEWLINK messages with the latest state. Do a GETLINK,
2455 * when it returns we know that the pending NEWLINKs have already been
2456 * processed and that we are up-to-date */
2458 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
2463 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
,
2464 link_initialized_and_synced
, link
, 0, NULL
);
2473 static int link_load(Link
*link
) {
2474 _cleanup_free_
char *network_file
= NULL
,
2477 *dhcp4_address
= NULL
,
2478 *ipv4ll_address
= NULL
;
2479 union in_addr_union address
;
2480 union in_addr_union route_dst
;
2486 r
= parse_env_file(link
->state_file
, NEWLINE
,
2487 "NETWORK_FILE", &network_file
,
2488 "ADDRESSES", &addresses
,
2490 "DHCP4_ADDRESS", &dhcp4_address
,
2491 "IPV4LL_ADDRESS", &ipv4ll_address
,
2493 if (r
< 0 && r
!= -ENOENT
)
2494 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
2501 suffix
= strrchr(network_file
, '.');
2503 log_link_debug(link
, "Failed to get network name from %s", network_file
);
2504 goto network_file_fail
;
2508 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
2510 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
2511 goto network_file_fail
;
2514 r
= network_apply(link
->manager
, network
, link
);
2516 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
2525 _cleanup_free_
char *address_str
= NULL
;
2526 char *prefixlen_str
;
2528 unsigned char prefixlen
;
2530 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
2532 log_link_debug_errno(link
, r
, "Failed to extract next address string: %m");
2538 prefixlen_str
= strchr(address_str
, '/');
2539 if (!prefixlen_str
) {
2540 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
2544 *prefixlen_str
++ = '\0';
2546 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
2548 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
2552 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
2554 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
2558 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
2560 return log_link_error_errno(link
, r
, "Failed to add address: %m");
2569 _cleanup_free_
char *route_str
= NULL
;
2570 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
2572 char *prefixlen_str
;
2574 unsigned char prefixlen
, tos
, table
;
2577 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
2579 log_link_debug_errno(link
, r
, "Failed to extract next route string: %m");
2585 prefixlen_str
= strchr(route_str
, '/');
2586 if (!prefixlen_str
) {
2587 log_link_debug(link
, "Failed to parse route %s", route_str
);
2591 *prefixlen_str
++ = '\0';
2593 r
= sscanf(prefixlen_str
, "%hhu/%hhu/%"SCNu32
"/%hhu/"USEC_FMT
, &prefixlen
, &tos
, &priority
, &table
, &lifetime
);
2595 log_link_debug(link
,
2596 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
2601 r
= in_addr_from_string_auto(route_str
, &family
, &route_dst
);
2603 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
2607 r
= route_add(link
, family
, &route_dst
, prefixlen
, tos
, priority
, table
, &route
);
2609 return log_link_error_errno(link
, r
, "Failed to add route: %m");
2611 if (lifetime
!= USEC_INFINITY
) {
2612 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(), lifetime
,
2613 0, route_expire_handler
, route
);
2615 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
2618 route
->lifetime
= lifetime
;
2619 sd_event_source_unref(route
->expire
);
2620 route
->expire
= expire
;
2625 if (dhcp4_address
) {
2626 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
2628 log_link_debug_errno(link
, r
, "Falied to parse DHCPv4 address %s: %m", dhcp4_address
);
2629 goto dhcp4_address_fail
;
2632 r
= sd_dhcp_client_new(&link
->dhcp_client
);
2634 return log_link_error_errno(link
, r
, "Falied to create DHCPv4 client: %m");
2636 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
2638 return log_link_error_errno(link
, r
, "Falied to set inital DHCPv4 address %s: %m", dhcp4_address
);
2643 if (ipv4ll_address
) {
2644 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
2646 log_link_debug_errno(link
, r
, "Falied to parse IPv4LL address %s: %m", ipv4ll_address
);
2647 goto ipv4ll_address_fail
;
2650 r
= sd_ipv4ll_new(&link
->ipv4ll
);
2652 return log_link_error_errno(link
, r
, "Falied to create IPv4LL client: %m");
2654 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
2656 return log_link_error_errno(link
, r
, "Falied to set inital IPv4LL address %s: %m", ipv4ll_address
);
2659 ipv4ll_address_fail
:
2664 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
2666 _cleanup_udev_device_unref_
struct udev_device
*device
= NULL
;
2667 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
2675 r
= link_new(m
, message
, ret
);
2681 log_link_debug(link
, "Link %d added", link
->ifindex
);
2683 r
= link_load(link
);
2687 if (detect_container() <= 0) {
2688 /* not in a container, udev will be around */
2689 sprintf(ifindex_str
, "n%d", link
->ifindex
);
2690 device
= udev_device_new_from_device_id(m
->udev
, ifindex_str
);
2692 r
= log_link_warning_errno(link
, errno
, "Could not find udev device: %m");
2696 if (udev_device_get_is_initialized(device
) <= 0) {
2698 log_link_debug(link
, "link pending udev initialization...");
2702 r
= link_initialized(link
, device
);
2706 /* we are calling a callback directly, so must take a ref */
2709 r
= link_initialized_and_synced(m
->rtnl
, NULL
, link
);
2716 link_enter_failed(link
);
2720 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
2725 log_link_info(link
, "Gained IPv6LL");
2727 link
->ipv6ll_address
= *address
;
2728 link_check_ready(link
);
2730 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
2731 r
= link_acquire_ipv6_conf(link
);
2733 link_enter_failed(link
);
2741 static int link_carrier_gained(Link
*link
) {
2746 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
2747 r
= link_acquire_conf(link
);
2749 link_enter_failed(link
);
2753 r
= link_enter_set_addresses(link
);
2758 r
= link_handle_bound_by_list(link
);
2765 static int link_carrier_lost(Link
*link
) {
2770 r
= link_stop_clients(link
);
2772 link_enter_failed(link
);
2776 r
= link_handle_bound_by_list(link
);
2783 int link_carrier_reset(Link
*link
) {
2788 if (link_has_carrier(link
)) {
2789 r
= link_carrier_lost(link
);
2793 r
= link_carrier_gained(link
);
2797 log_link_info(link
, "Reset carrier");
2803 int link_update(Link
*link
, sd_netlink_message
*m
) {
2804 struct ether_addr mac
;
2807 bool had_carrier
, carrier_gained
, carrier_lost
;
2811 assert(link
->ifname
);
2814 if (link
->state
== LINK_STATE_LINGER
) {
2816 log_link_info(link
, "Link readded");
2817 link_set_state(link
, LINK_STATE_ENSLAVING
);
2819 r
= link_new_carrier_maps(link
);
2824 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
2825 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
2826 log_link_info(link
, "Renamed to %s", ifname
);
2828 link_free_carrier_maps(link
);
2830 r
= free_and_strdup(&link
->ifname
, ifname
);
2834 r
= link_new_carrier_maps(link
);
2839 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
2840 if (r
>= 0 && mtu
> 0) {
2842 if (!link
->original_mtu
) {
2843 link
->original_mtu
= mtu
;
2844 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
2847 if (link
->dhcp_client
) {
2848 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
2851 log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
2857 /* The kernel may broadcast NEWLINK messages without the MAC address
2858 set, simply ignore them. */
2859 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
2861 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
2864 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
2867 log_link_debug(link
, "MAC address: "
2868 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
2869 mac
.ether_addr_octet
[0],
2870 mac
.ether_addr_octet
[1],
2871 mac
.ether_addr_octet
[2],
2872 mac
.ether_addr_octet
[3],
2873 mac
.ether_addr_octet
[4],
2874 mac
.ether_addr_octet
[5]);
2877 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
2879 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
2882 if (link
->dhcp_client
) {
2883 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
2884 (const uint8_t *) &link
->mac
,
2888 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
2890 if (link
->network
->duid_type
!= _DUID_TYPE_INVALID
)
2891 r
= sd_dhcp_client_set_iaid_duid(link
->dhcp_client
,
2892 link
->network
->iaid
,
2893 link
->network
->dhcp_duid_type
,
2894 link
->network
->dhcp_duid
,
2895 link
->network
->dhcp_duid_len
);
2897 r
= sd_dhcp_client_set_iaid_duid(link
->dhcp_client
,
2898 link
->network
->iaid
,
2899 link
->manager
->dhcp_duid_type
,
2900 link
->manager
->dhcp_duid
,
2901 link
->manager
->dhcp_duid_len
);
2903 return log_link_warning_errno(link
, r
, "Could not update DUID/IAID in DHCP client: %m");
2906 if (link
->dhcp6_client
) {
2907 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
2908 (const uint8_t *) &link
->mac
,
2912 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
2914 r
= sd_dhcp6_client_set_iaid(link
->dhcp6_client
,
2915 link
->network
->iaid
);
2917 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 IAID: %m");
2919 if (link
->network
->duid_type
!= _DUID_TYPE_INVALID
)
2920 r
= sd_dhcp6_client_set_duid(link
->dhcp6_client
,
2921 link
->network
->dhcp_duid_type
,
2922 link
->network
->dhcp_duid
,
2923 link
->network
->dhcp_duid_len
);
2925 r
= sd_dhcp6_client_set_duid(link
->dhcp6_client
,
2926 link
->manager
->dhcp_duid_type
,
2927 link
->manager
->dhcp_duid
,
2928 link
->manager
->dhcp_duid_len
);
2930 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 DUID: %m");
2935 had_carrier
= link_has_carrier(link
);
2937 r
= link_update_flags(link
, m
);
2941 r
= link_update_lldp(link
);
2945 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
2946 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
2948 if (carrier_gained
) {
2949 log_link_info(link
, "Gained carrier");
2951 r
= link_carrier_gained(link
);
2954 } else if (carrier_lost
) {
2955 log_link_info(link
, "Lost carrier");
2957 r
= link_carrier_lost(link
);
2965 static void print_link_hashmap(FILE *f
, const char *prefix
, Hashmap
* h
) {
2973 if (hashmap_isempty(h
))
2977 HASHMAP_FOREACH(link
, h
, i
) {
2981 fprintf(f
, "%i", link
->ifindex
);
2988 int link_save(Link
*link
) {
2989 _cleanup_free_
char *temp_path
= NULL
;
2990 _cleanup_fclose_
FILE *f
= NULL
;
2991 const char *admin_state
, *oper_state
;
2998 assert(link
->state_file
);
2999 assert(link
->lease_file
);
3000 assert(link
->manager
);
3002 if (link
->state
== LINK_STATE_LINGER
) {
3003 unlink(link
->state_file
);
3007 link_lldp_save(link
);
3009 admin_state
= link_state_to_string(link
->state
);
3010 assert(admin_state
);
3012 oper_state
= link_operstate_to_string(link
->operstate
);
3015 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
3019 fchmod(fileno(f
), 0644);
3022 "# This is private data. Do not parse.\n"
3025 admin_state
, oper_state
);
3027 if (link
->network
) {
3029 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
3030 const char *dhcp_domainname
= NULL
;
3031 char **dhcp6_domains
= NULL
;
3033 if (link
->dhcp6_client
) {
3034 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
, &dhcp6_lease
);
3035 if (r
< 0 && r
!= -ENOMSG
)
3036 log_link_debug(link
, "No DHCPv6 lease");
3039 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
3043 fputstrv(f
, link
->network
->dns
, NULL
, &space
);
3045 if (link
->network
->dhcp_use_dns
&&
3047 const struct in_addr
*addresses
;
3049 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
3053 serialize_in_addrs(f
, addresses
, r
);
3058 if (link
->network
->dhcp_use_dns
&& dhcp6_lease
) {
3059 struct in6_addr
*in6_addrs
;
3061 r
= sd_dhcp6_lease_get_dns(dhcp6_lease
, &in6_addrs
);
3065 serialize_in6_addrs(f
, in6_addrs
, r
);
3073 fputstrv(f
, link
->network
->ntp
, NULL
, &space
);
3075 if (link
->network
->dhcp_use_ntp
&&
3077 const struct in_addr
*addresses
;
3079 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
3083 serialize_in_addrs(f
, addresses
, r
);
3088 if (link
->network
->dhcp_use_ntp
&& dhcp6_lease
) {
3089 struct in6_addr
*in6_addrs
;
3092 r
= sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease
,
3097 serialize_in6_addrs(f
, in6_addrs
, r
);
3101 r
= sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease
, &hosts
);
3103 fputstrv(f
, hosts
, NULL
, &space
);
3108 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
3109 if (link
->dhcp_lease
)
3110 (void) sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &dhcp_domainname
);
3113 (void) sd_dhcp6_lease_get_domains(dhcp6_lease
, &dhcp6_domains
);
3116 fputs("DOMAINS=", f
);
3117 fputstrv(f
, link
->network
->search_domains
, NULL
, &space
);
3119 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
&& dhcp_domainname
)
3120 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3122 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
&& dhcp6_domains
)
3123 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3127 fputs("ROUTE_DOMAINS=", f
);
3128 fputstrv(f
, link
->network
->route_domains
, NULL
, NULL
);
3130 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
&& dhcp_domainname
)
3131 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3133 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
&& dhcp6_domains
)
3134 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3138 fprintf(f
, "LLMNR=%s\n",
3139 resolve_support_to_string(link
->network
->llmnr
));
3140 fprintf(f
, "MDNS=%s\n",
3141 resolve_support_to_string(link
->network
->mdns
));
3143 if (link
->network
->dnssec_mode
!= _DNSSEC_MODE_INVALID
)
3144 fprintf(f
, "DNSSEC=%s\n",
3145 dnssec_mode_to_string(link
->network
->dnssec_mode
));
3147 if (!set_isempty(link
->network
->dnssec_negative_trust_anchors
)) {
3150 fputs("DNSSEC_NTA=", f
);
3152 SET_FOREACH(n
, link
->network
->dnssec_negative_trust_anchors
, i
)
3153 fputs_with_space(f
, n
, NULL
, &space
);
3157 fputs("ADDRESSES=", f
);
3159 SET_FOREACH(a
, link
->addresses
, i
) {
3160 _cleanup_free_
char *address_str
= NULL
;
3162 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
3166 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
3171 fputs("ROUTES=", f
);
3173 SET_FOREACH(route
, link
->routes
, i
) {
3174 _cleanup_free_
char *route_str
= NULL
;
3176 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
3180 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%hhu/"USEC_FMT
, space
? " " : "", route_str
,
3181 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
3188 print_link_hashmap(f
, "CARRIER_BOUND_TO=", link
->bound_to_links
);
3189 print_link_hashmap(f
, "CARRIER_BOUND_BY=", link
->bound_by_links
);
3191 if (link
->dhcp_lease
) {
3192 struct in_addr address
;
3193 const char *tz
= NULL
;
3195 assert(link
->network
);
3197 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
3199 fprintf(f
, "TIMEZONE=%s\n", tz
);
3201 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
3203 fputs("DHCP4_ADDRESS=", f
);
3204 serialize_in_addrs(f
, &address
, 1);
3208 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
3216 unlink(link
->lease_file
);
3219 struct in_addr address
;
3221 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
3223 fputs("IPV4LL_ADDRESS=", f
);
3224 serialize_in_addrs(f
, &address
, 1);
3229 r
= fflush_and_check(f
);
3233 if (rename(temp_path
, link
->state_file
) < 0) {
3241 (void) unlink(link
->state_file
);
3243 (void) unlink(temp_path
);
3245 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
3248 /* The serialized state in /run is no longer up-to-date. */
3249 void link_dirty(Link
*link
) {
3254 /* mark manager dirty as link is dirty */
3255 manager_dirty(link
->manager
);
3257 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
3259 /* allocation errors are ignored */
3262 r
= set_put(link
->manager
->dirty_links
, link
);
3264 /* don't take another ref if the link was already dirty */
3270 /* The serialized state in /run is up-to-date */
3271 void link_clean(Link
*link
) {
3273 assert(link
->manager
);
3275 set_remove(link
->manager
->dirty_links
, link
);
3279 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
3280 [LINK_STATE_PENDING
] = "pending",
3281 [LINK_STATE_ENSLAVING
] = "configuring",
3282 [LINK_STATE_SETTING_ADDRESSES
] = "configuring",
3283 [LINK_STATE_SETTING_ROUTES
] = "configuring",
3284 [LINK_STATE_CONFIGURED
] = "configured",
3285 [LINK_STATE_UNMANAGED
] = "unmanaged",
3286 [LINK_STATE_FAILED
] = "failed",
3287 [LINK_STATE_LINGER
] = "linger",
3290 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);
3292 static const char* const link_operstate_table
[_LINK_OPERSTATE_MAX
] = {
3293 [LINK_OPERSTATE_OFF
] = "off",
3294 [LINK_OPERSTATE_NO_CARRIER
] = "no-carrier",
3295 [LINK_OPERSTATE_DORMANT
] = "dormant",
3296 [LINK_OPERSTATE_CARRIER
] = "carrier",
3297 [LINK_OPERSTATE_DEGRADED
] = "degraded",
3298 [LINK_OPERSTATE_ROUTABLE
] = "routable",
3301 DEFINE_STRING_TABLE_LOOKUP(link_operstate
, LinkOperationalState
);