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 (link
->flags
& IFF_LOOPBACK
)
51 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV6
;
54 static bool link_dhcp4_enabled(Link
*link
) {
57 if (link
->flags
& IFF_LOOPBACK
)
63 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV4
;
66 static bool link_dhcp4_server_enabled(Link
*link
) {
69 if (link
->flags
& IFF_LOOPBACK
)
75 return link
->network
->dhcp_server
;
78 static bool link_ipv4ll_enabled(Link
*link
) {
81 if (link
->flags
& IFF_LOOPBACK
)
87 return link
->network
->link_local
& ADDRESS_FAMILY_IPV4
;
90 static bool link_ipv6ll_enabled(Link
*link
) {
93 if (link
->flags
& IFF_LOOPBACK
)
99 return link
->network
->link_local
& ADDRESS_FAMILY_IPV6
;
102 static bool link_lldp_rx_enabled(Link
*link
) {
105 if (link
->flags
& IFF_LOOPBACK
)
108 if (link
->iftype
!= ARPHRD_ETHER
)
114 if (link
->network
->bridge
)
117 return link
->network
->lldp_mode
!= LLDP_MODE_NO
;
120 static bool link_lldp_tx_enabled(Link
*link
) {
123 if (link
->flags
& IFF_LOOPBACK
)
126 if (link
->iftype
!= ARPHRD_ETHER
)
132 return link
->network
->lldp_emit
;
135 static bool link_ipv4_forward_enabled(Link
*link
) {
138 if (link
->flags
& IFF_LOOPBACK
)
144 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
147 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV4
;
150 static bool link_ipv6_forward_enabled(Link
*link
) {
153 if (!socket_ipv6_is_supported())
156 if (link
->flags
& IFF_LOOPBACK
)
162 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
165 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV6
;
168 static bool link_proxy_arp_enabled(Link
*link
) {
171 if (link
->flags
& IFF_LOOPBACK
)
177 if (link
->network
->proxy_arp
< 0)
183 static bool link_ipv6_accept_ra_enabled(Link
*link
) {
186 if (link
->flags
& IFF_LOOPBACK
)
192 /* If unset use system default (enabled if local forwarding is disabled.
193 * disabled if local forwarding is enabled).
194 * If set, ignore or enforce RA independent of local forwarding state.
196 if (link
->network
->ipv6_accept_ra
< 0)
197 /* default to accept RA if ip_forward is disabled and ignore RA if ip_forward is enabled */
198 return !link_ipv6_forward_enabled(link
);
199 else if (link
->network
->ipv6_accept_ra
> 0)
200 /* accept RA even if ip_forward is enabled */
207 static IPv6PrivacyExtensions
link_ipv6_privacy_extensions(Link
*link
) {
209 if (!socket_ipv6_is_supported())
210 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
212 if (link
->flags
& IFF_LOOPBACK
)
213 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
216 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
218 return link
->network
->ipv6_privacy_extensions
;
221 void link_update_operstate(Link
*link
) {
222 LinkOperationalState operstate
;
225 if (link
->kernel_operstate
== IF_OPER_DORMANT
)
226 operstate
= LINK_OPERSTATE_DORMANT
;
227 else if (link_has_carrier(link
)) {
229 uint8_t scope
= RT_SCOPE_NOWHERE
;
232 /* if we have carrier, check what addresses we have */
233 SET_FOREACH(address
, link
->addresses
, i
) {
234 if (!address_is_ready(address
))
237 if (address
->scope
< scope
)
238 scope
= address
->scope
;
241 /* for operstate we also take foreign addresses into account */
242 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
243 if (!address_is_ready(address
))
246 if (address
->scope
< scope
)
247 scope
= address
->scope
;
250 if (scope
< RT_SCOPE_SITE
)
251 /* universally accessible addresses found */
252 operstate
= LINK_OPERSTATE_ROUTABLE
;
253 else if (scope
< RT_SCOPE_HOST
)
254 /* only link or site local addresses found */
255 operstate
= LINK_OPERSTATE_DEGRADED
;
257 /* no useful addresses found */
258 operstate
= LINK_OPERSTATE_CARRIER
;
259 } else if (link
->flags
& IFF_UP
)
260 operstate
= LINK_OPERSTATE_NO_CARRIER
;
262 operstate
= LINK_OPERSTATE_OFF
;
264 if (link
->operstate
!= operstate
) {
265 link
->operstate
= operstate
;
266 link_send_changed(link
, "OperationalState", NULL
);
268 manager_dirty(link
->manager
);
272 #define FLAG_STRING(string, flag, old, new) \
273 (((old ^ new) & flag) \
274 ? ((old & flag) ? (" -" string) : (" +" string)) \
277 static int link_update_flags(Link
*link
, sd_netlink_message
*m
) {
278 unsigned flags
, unknown_flags_added
, unknown_flags_removed
, unknown_flags
;
284 r
= sd_rtnl_message_link_get_flags(m
, &flags
);
286 return log_link_warning_errno(link
, r
, "Could not get link flags: %m");
288 r
= sd_netlink_message_read_u8(m
, IFLA_OPERSTATE
, &operstate
);
290 /* if we got a message without operstate, take it to mean
291 the state was unchanged */
292 operstate
= link
->kernel_operstate
;
294 if ((link
->flags
== flags
) && (link
->kernel_operstate
== operstate
))
297 if (link
->flags
!= flags
) {
298 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",
299 FLAG_STRING("LOOPBACK", IFF_LOOPBACK
, link
->flags
, flags
),
300 FLAG_STRING("MASTER", IFF_MASTER
, link
->flags
, flags
),
301 FLAG_STRING("SLAVE", IFF_SLAVE
, link
->flags
, flags
),
302 FLAG_STRING("UP", IFF_UP
, link
->flags
, flags
),
303 FLAG_STRING("DORMANT", IFF_DORMANT
, link
->flags
, flags
),
304 FLAG_STRING("LOWER_UP", IFF_LOWER_UP
, link
->flags
, flags
),
305 FLAG_STRING("RUNNING", IFF_RUNNING
, link
->flags
, flags
),
306 FLAG_STRING("MULTICAST", IFF_MULTICAST
, link
->flags
, flags
),
307 FLAG_STRING("BROADCAST", IFF_BROADCAST
, link
->flags
, flags
),
308 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT
, link
->flags
, flags
),
309 FLAG_STRING("PROMISC", IFF_PROMISC
, link
->flags
, flags
),
310 FLAG_STRING("ALLMULTI", IFF_ALLMULTI
, link
->flags
, flags
),
311 FLAG_STRING("PORTSEL", IFF_PORTSEL
, link
->flags
, flags
),
312 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA
, link
->flags
, flags
),
313 FLAG_STRING("DYNAMIC", IFF_DYNAMIC
, link
->flags
, flags
),
314 FLAG_STRING("NOARP", IFF_NOARP
, link
->flags
, flags
),
315 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS
, link
->flags
, flags
),
316 FLAG_STRING("DEBUG", IFF_DEBUG
, link
->flags
, flags
),
317 FLAG_STRING("ECHO", IFF_ECHO
, link
->flags
, flags
));
319 unknown_flags
= ~(IFF_LOOPBACK
| IFF_MASTER
| IFF_SLAVE
| IFF_UP
|
320 IFF_DORMANT
| IFF_LOWER_UP
| IFF_RUNNING
|
321 IFF_MULTICAST
| IFF_BROADCAST
| IFF_POINTOPOINT
|
322 IFF_PROMISC
| IFF_ALLMULTI
| IFF_PORTSEL
|
323 IFF_AUTOMEDIA
| IFF_DYNAMIC
| IFF_NOARP
|
324 IFF_NOTRAILERS
| IFF_DEBUG
| IFF_ECHO
);
325 unknown_flags_added
= ((link
->flags
^ flags
) & flags
& unknown_flags
);
326 unknown_flags_removed
= ((link
->flags
^ flags
) & link
->flags
& unknown_flags
);
328 /* link flags are currently at most 18 bits, let's align to
330 if (unknown_flags_added
)
332 "Unknown link flags gained: %#.5x (ignoring)",
333 unknown_flags_added
);
335 if (unknown_flags_removed
)
337 "Unknown link flags lost: %#.5x (ignoring)",
338 unknown_flags_removed
);
342 link
->kernel_operstate
= operstate
;
344 link_update_operstate(link
);
349 static int link_new(Manager
*manager
, sd_netlink_message
*message
, Link
**ret
) {
350 _cleanup_link_unref_ Link
*link
= NULL
;
354 unsigned short iftype
;
360 r
= sd_netlink_message_get_type(message
, &type
);
363 else if (type
!= RTM_NEWLINK
)
366 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
369 else if (ifindex
<= 0)
372 r
= sd_rtnl_message_link_get_type(message
, &iftype
);
376 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &ifname
);
380 link
= new0(Link
, 1);
385 link
->manager
= manager
;
386 link
->state
= LINK_STATE_PENDING
;
387 link
->rtnl_extended_attrs
= true;
388 link
->ifindex
= ifindex
;
389 link
->iftype
= iftype
;
390 link
->ifname
= strdup(ifname
);
394 r
= sd_netlink_message_read_ether_addr(message
, IFLA_ADDRESS
, &link
->mac
);
396 log_link_debug_errno(link
, r
, "MAC address not found for new device, continuing without");
398 if (asprintf(&link
->state_file
, "/run/systemd/netif/links/%d", link
->ifindex
) < 0)
401 if (asprintf(&link
->lease_file
, "/run/systemd/netif/leases/%d", link
->ifindex
) < 0)
404 if (asprintf(&link
->lldp_file
, "/run/systemd/netif/lldp/%d", link
->ifindex
) < 0)
407 r
= hashmap_ensure_allocated(&manager
->links
, NULL
);
411 r
= hashmap_put(manager
->links
, INT_TO_PTR(link
->ifindex
), link
);
415 r
= link_update_flags(link
, message
);
425 static void link_free(Link
*link
) {
433 while (!set_isempty(link
->addresses
))
434 address_free(set_first(link
->addresses
));
436 while (!set_isempty(link
->addresses_foreign
))
437 address_free(set_first(link
->addresses_foreign
));
439 link
->addresses
= set_free(link
->addresses
);
441 link
->addresses_foreign
= set_free(link
->addresses_foreign
);
443 while ((address
= link
->pool_addresses
)) {
444 LIST_REMOVE(addresses
, link
->pool_addresses
, address
);
445 address_free(address
);
448 sd_dhcp_server_unref(link
->dhcp_server
);
449 sd_dhcp_client_unref(link
->dhcp_client
);
450 sd_dhcp_lease_unref(link
->dhcp_lease
);
452 link_lldp_tx_stop(link
);
454 free(link
->lease_file
);
456 sd_lldp_unref(link
->lldp
);
457 free(link
->lldp_file
);
459 sd_ipv4ll_unref(link
->ipv4ll
);
460 sd_dhcp6_client_unref(link
->dhcp6_client
);
461 sd_ndisc_unref(link
->ndisc_router_discovery
);
464 hashmap_remove(link
->manager
->links
, INT_TO_PTR(link
->ifindex
));
468 (void)unlink(link
->state_file
);
469 free(link
->state_file
);
471 udev_device_unref(link
->udev_device
);
473 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
)
474 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(carrier
->ifindex
));
475 hashmap_free(link
->bound_to_links
);
477 HASHMAP_FOREACH (carrier
, link
->bound_by_links
, i
)
478 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(carrier
->ifindex
));
479 hashmap_free(link
->bound_by_links
);
484 Link
*link_unref(Link
*link
) {
488 assert(link
->n_ref
> 0);
500 Link
*link_ref(Link
*link
) {
504 assert(link
->n_ref
> 0);
511 int link_get(Manager
*m
, int ifindex
, Link
**ret
) {
518 link
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
527 static void link_set_state(Link
*link
, LinkState state
) {
530 if (link
->state
== state
)
535 link_send_changed(link
, "AdministrativeState", NULL
);
540 static void link_enter_unmanaged(Link
*link
) {
543 log_link_debug(link
, "Unmanaged");
545 link_set_state(link
, LINK_STATE_UNMANAGED
);
550 static int link_stop_clients(Link
*link
) {
554 assert(link
->manager
);
555 assert(link
->manager
->event
);
557 if (link
->dhcp_client
) {
558 k
= sd_dhcp_client_stop(link
->dhcp_client
);
560 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv4 client: %m");
564 k
= sd_ipv4ll_stop(link
->ipv4ll
);
566 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 link-local: %m");
569 if (link
->dhcp6_client
) {
570 k
= sd_dhcp6_client_stop(link
->dhcp6_client
);
572 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv6 client: %m");
575 if (link
->ndisc_router_discovery
) {
576 k
= sd_ndisc_stop(link
->ndisc_router_discovery
);
578 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Discovery: %m");
581 link_lldp_tx_stop(link
);
585 void link_enter_failed(Link
*link
) {
588 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
591 log_link_warning(link
, "Failed");
593 link_set_state(link
, LINK_STATE_FAILED
);
595 link_stop_clients(link
);
600 static Address
* link_find_dhcp_server_address(Link
*link
) {
604 assert(link
->network
);
606 /* The first statically configured address if there is any */
607 LIST_FOREACH(addresses
, address
, link
->network
->static_addresses
) {
609 if (address
->family
!= AF_INET
)
612 if (in_addr_is_null(address
->family
, &address
->in_addr
))
618 /* If that didn't work, find a suitable address we got from the pool */
619 LIST_FOREACH(addresses
, address
, link
->pool_addresses
) {
620 if (address
->family
!= AF_INET
)
629 static int link_enter_configured(Link
*link
) {
631 assert(link
->network
);
632 assert(link
->state
== LINK_STATE_SETTING_ROUTES
);
634 log_link_info(link
, "Configured");
636 link_set_state(link
, LINK_STATE_CONFIGURED
);
643 void link_check_ready(Link
*link
) {
652 if (!link
->static_configured
)
655 if (link_ipv4ll_enabled(link
))
656 if (!link
->ipv4ll_address
||
660 if (link_ipv6ll_enabled(link
))
661 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) > 0)
664 if ((link_dhcp4_enabled(link
) && !link_dhcp6_enabled(link
) &&
665 !link
->dhcp4_configured
) ||
666 (link_dhcp6_enabled(link
) && !link_dhcp4_enabled(link
) &&
667 !link
->dhcp6_configured
) ||
668 (link_dhcp4_enabled(link
) && link_dhcp6_enabled(link
) &&
669 !link
->dhcp4_configured
&& !link
->dhcp6_configured
))
672 if (link_ipv6_accept_ra_enabled(link
) && !link
->ndisc_configured
)
675 SET_FOREACH(a
, link
->addresses
, i
)
676 if (!address_is_ready(a
))
679 if (link
->state
!= LINK_STATE_CONFIGURED
)
680 link_enter_configured(link
);
685 static int route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
686 _cleanup_link_unref_ Link
*link
= userdata
;
689 assert(link
->link_messages
> 0);
690 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
691 LINK_STATE_SETTING_ROUTES
, LINK_STATE_FAILED
,
694 link
->link_messages
--;
696 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
699 r
= sd_netlink_message_get_errno(m
);
700 if (r
< 0 && r
!= -EEXIST
)
701 log_link_warning_errno(link
, r
, "Could not set route: %m");
703 if (link
->link_messages
== 0) {
704 log_link_debug(link
, "Routes set");
705 link
->static_configured
= true;
706 link_check_ready(link
);
712 static int link_enter_set_routes(Link
*link
) {
717 assert(link
->network
);
718 assert(link
->state
== LINK_STATE_SETTING_ADDRESSES
);
720 link_set_state(link
, LINK_STATE_SETTING_ROUTES
);
722 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
723 r
= route_configure(rt
, link
, &route_handler
);
725 log_link_warning_errno(link
, r
, "Could not set routes: %m");
726 link_enter_failed(link
);
730 link
->link_messages
++;
733 if (link
->link_messages
== 0) {
734 link
->static_configured
= true;
735 link_check_ready(link
);
737 log_link_debug(link
, "Setting routes");
742 int link_route_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
743 _cleanup_link_unref_ Link
*link
= userdata
;
748 assert(link
->ifname
);
750 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
753 r
= sd_netlink_message_get_errno(m
);
754 if (r
< 0 && r
!= -ESRCH
)
755 log_link_warning_errno(link
, r
, "Could not drop route: %m");
760 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
761 _cleanup_link_unref_ Link
*link
= userdata
;
767 assert(link
->ifname
);
768 assert(link
->link_messages
> 0);
769 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
770 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
772 link
->link_messages
--;
774 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
777 r
= sd_netlink_message_get_errno(m
);
778 if (r
< 0 && r
!= -EEXIST
)
779 log_link_warning_errno(link
, r
, "could not set address: %m");
781 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
783 if (link
->link_messages
== 0) {
784 log_link_debug(link
, "Addresses set");
785 link_enter_set_routes(link
);
791 static int link_push_dns_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
792 _cleanup_free_
struct in_addr
*addresses
= NULL
;
793 size_t n_addresses
= 0, n_allocated
= 0;
796 log_debug("Copying DNS server information from %s", link
->ifname
);
801 STRV_FOREACH(a
, link
->network
->dns
) {
804 /* Only look for IPv4 addresses */
805 if (inet_pton(AF_INET
, *a
, &ia
) <= 0)
808 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
811 addresses
[n_addresses
++] = ia
;
814 if (link
->network
->dhcp_use_dns
&&
816 const struct in_addr
*da
= NULL
;
819 n
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &da
);
822 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
825 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
830 if (n_addresses
<= 0)
833 return sd_dhcp_server_set_dns(s
, addresses
, n_addresses
);
836 static int link_push_ntp_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
837 _cleanup_free_
struct in_addr
*addresses
= NULL
;
838 size_t n_addresses
= 0, n_allocated
= 0;
844 log_debug("Copying NTP server information from %s", link
->ifname
);
846 STRV_FOREACH(a
, link
->network
->ntp
) {
849 /* Only look for IPv4 addresses */
850 if (inet_pton(AF_INET
, *a
, &ia
) <= 0)
853 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
856 addresses
[n_addresses
++] = ia
;
859 if (link
->network
->dhcp_use_ntp
&&
861 const struct in_addr
*da
= NULL
;
864 n
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &da
);
867 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
870 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
875 if (n_addresses
<= 0)
878 return sd_dhcp_server_set_ntp(s
, addresses
, n_addresses
);
881 static int link_enter_set_addresses(Link
*link
) {
886 assert(link
->network
);
887 assert(link
->state
!= _LINK_STATE_INVALID
);
889 link_set_state(link
, LINK_STATE_SETTING_ADDRESSES
);
891 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
892 r
= address_configure(ad
, link
, &address_handler
, false);
894 log_link_warning_errno(link
, r
, "Could not set addresses: %m");
895 link_enter_failed(link
);
899 link
->link_messages
++;
902 /* now that we can figure out a default address for the dhcp server,
904 if (link_dhcp4_server_enabled(link
)) {
907 bool acquired_uplink
= false;
909 address
= link_find_dhcp_server_address(link
);
911 log_link_warning(link
, "Failed to find suitable address for DHCPv4 server instance.");
912 link_enter_failed(link
);
916 /* use the server address' subnet as the pool */
917 r
= sd_dhcp_server_configure_pool(link
->dhcp_server
, &address
->in_addr
.in
, address
->prefixlen
,
918 link
->network
->dhcp_server_pool_offset
, link
->network
->dhcp_server_pool_size
);
923 r = sd_dhcp_server_set_router(link->dhcp_server,
924 &main_address->in_addr.in);
929 if (link
->network
->dhcp_server_max_lease_time_usec
> 0) {
930 r
= sd_dhcp_server_set_max_lease_time(
932 DIV_ROUND_UP(link
->network
->dhcp_server_max_lease_time_usec
, USEC_PER_SEC
));
937 if (link
->network
->dhcp_server_default_lease_time_usec
> 0) {
938 r
= sd_dhcp_server_set_default_lease_time(
940 DIV_ROUND_UP(link
->network
->dhcp_server_default_lease_time_usec
, USEC_PER_SEC
));
945 if (link
->network
->dhcp_server_emit_dns
) {
947 if (link
->network
->n_dhcp_server_dns
> 0)
948 r
= sd_dhcp_server_set_dns(link
->dhcp_server
, link
->network
->dhcp_server_dns
, link
->network
->n_dhcp_server_dns
);
950 uplink
= manager_find_uplink(link
->manager
, link
);
951 acquired_uplink
= true;
954 log_link_debug(link
, "Not emitting DNS server information on link, couldn't find suitable uplink.");
957 r
= link_push_dns_to_dhcp_server(uplink
, link
->dhcp_server
);
960 log_link_warning_errno(link
, r
, "Failed to set DNS server for DHCP server, ignoring: %m");
964 if (link
->network
->dhcp_server_emit_ntp
) {
966 if (link
->network
->n_dhcp_server_ntp
> 0)
967 r
= sd_dhcp_server_set_ntp(link
->dhcp_server
, link
->network
->dhcp_server_ntp
, link
->network
->n_dhcp_server_ntp
);
969 if (!acquired_uplink
)
970 uplink
= manager_find_uplink(link
->manager
, link
);
973 log_link_debug(link
, "Not emitting NTP server information on link, couldn't find suitable uplink.");
976 r
= link_push_ntp_to_dhcp_server(uplink
, link
->dhcp_server
);
980 log_link_warning_errno(link
, r
, "Failed to set NTP server for DHCP server, ignoring: %m");
983 if (link
->network
->dhcp_server_emit_timezone
) {
984 _cleanup_free_
char *buffer
= NULL
;
985 const char *tz
= NULL
;
987 if (link
->network
->dhcp_server_timezone
)
988 tz
= link
->network
->dhcp_server_timezone
;
990 r
= get_timezone(&buffer
);
992 log_warning_errno(r
, "Failed to determine timezone: %m");
998 r
= sd_dhcp_server_set_timezone(link
->dhcp_server
, tz
);
1004 r
= sd_dhcp_server_start(link
->dhcp_server
);
1006 log_link_warning_errno(link
, r
, "Could not start DHCPv4 server instance: %m");
1008 link_enter_failed(link
);
1013 log_link_debug(link
, "Offering DHCPv4 leases");
1016 if (link
->link_messages
== 0)
1017 link_enter_set_routes(link
);
1019 log_link_debug(link
, "Setting addresses");
1024 int link_address_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1025 _cleanup_link_unref_ Link
*link
= userdata
;
1030 assert(link
->ifname
);
1032 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1035 r
= sd_netlink_message_get_errno(m
);
1036 if (r
< 0 && r
!= -EADDRNOTAVAIL
)
1037 log_link_warning_errno(link
, r
, "Could not drop address: %m");
1042 static int link_set_bridge_fdb(Link
*const link
) {
1043 FdbEntry
*fdb_entry
;
1046 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
1047 r
= fdb_entry_configure(link
, fdb_entry
);
1049 log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
1057 static int link_set_proxy_arp(Link
*const link
) {
1058 const char *p
= NULL
;
1061 if (!link_proxy_arp_enabled(link
))
1064 p
= strjoina("/proc/sys/net/ipv4/conf/", link
->ifname
, "/proxy_arp");
1066 r
= write_string_file(p
, one_zero(link
->network
->proxy_arp
), WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
1068 log_link_warning_errno(link
, r
, "Cannot configure proxy ARP for interface: %m");
1073 static int link_set_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1074 _cleanup_link_unref_ Link
*link
= userdata
;
1077 log_link_debug(link
, "Set link");
1079 r
= sd_netlink_message_get_errno(m
);
1080 if (r
< 0 && r
!= -EEXIST
) {
1081 log_link_error_errno(link
, r
, "Could not join netdev: %m");
1082 link_enter_failed(link
);
1089 static int set_hostname_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
1090 _cleanup_link_unref_ Link
*link
= userdata
;
1091 const sd_bus_error
*e
;
1096 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1099 e
= sd_bus_message_get_error(m
);
1101 log_link_warning_errno(link
, sd_bus_error_get_errno(e
), "Could not set hostname: %s", e
->message
);
1106 int link_set_hostname(Link
*link
, const char *hostname
) {
1110 assert(link
->manager
);
1112 log_link_debug(link
, "Setting transient hostname: '%s'", strna(hostname
));
1114 if (!link
->manager
->bus
) {
1115 /* TODO: replace by assert when we can rely on kdbus */
1116 log_link_info(link
, "Not connected to system bus, ignoring transient hostname.");
1120 r
= sd_bus_call_method_async(
1123 "org.freedesktop.hostname1",
1124 "/org/freedesktop/hostname1",
1125 "org.freedesktop.hostname1",
1127 set_hostname_handler
,
1134 return log_link_error_errno(link
, r
, "Could not set transient hostname: %m");
1141 static int set_timezone_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
1142 _cleanup_link_unref_ Link
*link
= userdata
;
1143 const sd_bus_error
*e
;
1148 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1151 e
= sd_bus_message_get_error(m
);
1153 log_link_warning_errno(link
, sd_bus_error_get_errno(e
), "Could not set timezone: %s", e
->message
);
1158 int link_set_timezone(Link
*link
, const char *tz
) {
1162 assert(link
->manager
);
1165 log_link_debug(link
, "Setting system timezone: '%s'", tz
);
1167 if (!link
->manager
->bus
) {
1168 log_link_info(link
, "Not connected to system bus, ignoring timezone.");
1172 r
= sd_bus_call_method_async(
1175 "org.freedesktop.timedate1",
1176 "/org/freedesktop/timedate1",
1177 "org.freedesktop.timedate1",
1179 set_timezone_handler
,
1185 return log_link_error_errno(link
, r
, "Could not set timezone: %m");
1192 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1193 _cleanup_link_unref_ Link
*link
= userdata
;
1198 assert(link
->ifname
);
1200 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1203 r
= sd_netlink_message_get_errno(m
);
1205 log_link_warning_errno(link
, r
, "Could not set MTU: %m");
1210 int link_set_mtu(Link
*link
, uint32_t mtu
) {
1211 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1215 assert(link
->manager
);
1216 assert(link
->manager
->rtnl
);
1218 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1220 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1222 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1224 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1226 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1228 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, set_mtu_handler
, link
, 0, NULL
);
1230 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1237 static int link_set_bridge(Link
*link
) {
1238 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1242 assert(link
->network
);
1244 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1246 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1248 r
= sd_rtnl_message_link_set_family(req
, PF_BRIDGE
);
1250 return log_link_error_errno(link
, r
, "Could not set message family: %m");
1252 r
= sd_netlink_message_open_container(req
, IFLA_PROTINFO
);
1254 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1256 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_GUARD
, !link
->network
->use_bpdu
);
1258 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_GUARD attribute: %m");
1260 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MODE
, link
->network
->hairpin
);
1262 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MODE attribute: %m");
1264 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_FAST_LEAVE
, link
->network
->fast_leave
);
1266 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_FAST_LEAVE attribute: %m");
1268 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_PROTECT
, !link
->network
->allow_port_to_be_root
);
1270 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PROTECT attribute: %m");
1272 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_UNICAST_FLOOD
, link
->network
->unicast_flood
);
1274 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_UNICAST_FLOOD attribute: %m");
1276 if (link
->network
->cost
!= 0) {
1277 r
= sd_netlink_message_append_u32(req
, IFLA_BRPORT_COST
, link
->network
->cost
);
1279 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_COST attribute: %m");
1282 r
= sd_netlink_message_close_container(req
);
1284 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1286 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_set_handler
, link
, 0, NULL
);
1288 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1295 static int link_lldp_save(Link
*link
) {
1296 _cleanup_free_
char *temp_path
= NULL
;
1297 _cleanup_fclose_
FILE *f
= NULL
;
1298 sd_lldp_neighbor
**l
= NULL
;
1302 assert(link
->lldp_file
);
1305 (void) unlink(link
->lldp_file
);
1309 r
= sd_lldp_get_neighbors(link
->lldp
, &l
);
1313 (void) unlink(link
->lldp_file
);
1319 r
= fopen_temporary(link
->lldp_file
, &f
, &temp_path
);
1323 fchmod(fileno(f
), 0644);
1325 for (i
= 0; i
< n
; i
++) {
1330 r
= sd_lldp_neighbor_get_raw(l
[i
], &p
, &sz
);
1335 (void) fwrite(&u
, 1, sizeof(u
), f
);
1336 (void) fwrite(p
, 1, sz
, f
);
1339 r
= fflush_and_check(f
);
1343 if (rename(temp_path
, link
->lldp_file
) < 0) {
1350 (void) unlink(link
->lldp_file
);
1352 (void) unlink(temp_path
);
1354 log_link_error_errno(link
, r
, "Failed to save LLDP data to %s: %m", link
->lldp_file
);
1358 for (i
= 0; i
< n
; i
++)
1359 sd_lldp_neighbor_unref(l
[i
]);
1366 static void lldp_handler(sd_lldp
*lldp
, sd_lldp_event event
, sd_lldp_neighbor
*n
, void *userdata
) {
1367 Link
*link
= userdata
;
1372 (void) link_lldp_save(link
);
1374 if (link_lldp_tx_enabled(link
) && event
== SD_LLDP_EVENT_ADDED
) {
1375 /* If we received information about a new neighbor, restart the LLDP "fast" logic */
1377 log_link_debug(link
, "Received LLDP datagram from previously unknown neighbor, restarting 'fast' LLDP transmission.");
1379 r
= link_lldp_tx_start(link
);
1381 log_link_warning_errno(link
, r
, "Failed to restart LLDP transmission: %m");
1385 static int link_acquire_ipv6_conf(Link
*link
) {
1390 if (link_dhcp6_enabled(link
)) {
1391 assert(link
->dhcp6_client
);
1392 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1394 log_link_debug(link
, "Acquiring DHCPv6 lease");
1396 r
= sd_dhcp6_client_set_local_address(link
->dhcp6_client
, &link
->ipv6ll_address
);
1397 if (r
< 0 && r
!= -EBUSY
)
1398 return log_link_warning_errno(link
, r
, "Could not set IPv6LL address in DHCP client: %m");
1400 r
= sd_dhcp6_client_start(link
->dhcp6_client
);
1401 if (r
< 0 && r
!= -EBUSY
)
1402 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv6 lease: %m");
1405 if (link_ipv6_accept_ra_enabled(link
)) {
1406 assert(link
->ndisc_router_discovery
);
1408 log_link_debug(link
, "Discovering IPv6 routers");
1410 r
= sd_ndisc_router_discovery_start(link
->ndisc_router_discovery
);
1411 if (r
< 0 && r
!= -EBUSY
)
1412 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1418 static int link_acquire_conf(Link
*link
) {
1422 assert(link
->network
);
1423 assert(link
->manager
);
1424 assert(link
->manager
->event
);
1426 if (link_ipv4ll_enabled(link
)) {
1427 assert(link
->ipv4ll
);
1429 log_link_debug(link
, "Acquiring IPv4 link-local address");
1431 r
= sd_ipv4ll_start(link
->ipv4ll
);
1433 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1436 if (link_dhcp4_enabled(link
)) {
1437 assert(link
->dhcp_client
);
1439 log_link_debug(link
, "Acquiring DHCPv4 lease");
1441 r
= sd_dhcp_client_start(link
->dhcp_client
);
1443 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1446 if (link_lldp_tx_enabled(link
)) {
1447 r
= link_lldp_tx_start(link
);
1449 return log_link_warning_errno(link
, r
, "Failed to start LLDP transmission: %m");
1455 bool link_has_carrier(Link
*link
) {
1456 /* see Documentation/networking/operstates.txt in the kernel sources */
1458 if (link
->kernel_operstate
== IF_OPER_UP
)
1461 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1462 /* operstate may not be implemented, so fall back to flags */
1463 if ((link
->flags
& IFF_LOWER_UP
) && !(link
->flags
& IFF_DORMANT
))
1469 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1470 _cleanup_link_unref_ Link
*link
= userdata
;
1475 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1478 r
= sd_netlink_message_get_errno(m
);
1480 /* we warn but don't fail the link, as it may be
1482 log_link_warning_errno(link
, r
, "Could not bring up interface: %m");
1487 static int link_up(Link
*link
) {
1488 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1489 uint8_t ipv6ll_mode
;
1493 assert(link
->network
);
1494 assert(link
->manager
);
1495 assert(link
->manager
->rtnl
);
1497 log_link_debug(link
, "Bringing link up");
1499 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1501 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1503 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1505 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1507 if (link
->network
->mac
) {
1508 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
1510 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
1513 if (link
->network
->mtu
) {
1514 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, link
->network
->mtu
);
1516 return log_link_error_errno(link
, r
, "Could not set MTU: %m");
1519 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1521 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1523 if (socket_ipv6_is_supported()) {
1524 /* if the kernel lacks ipv6 support setting IFF_UP fails if any ipv6 options are passed */
1525 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1527 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1529 ipv6ll_mode
= link_ipv6ll_enabled(link
) ? IN6_ADDR_GEN_MODE_EUI64
: IN6_ADDR_GEN_MODE_NONE
;
1530 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1532 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1534 if (!in_addr_is_null(AF_INET6
, &link
->network
->ipv6_token
)) {
1535 r
= sd_netlink_message_append_in6_addr(req
, IFLA_INET6_TOKEN
, &link
->network
->ipv6_token
.in6
);
1537 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_TOKEN: %m");
1540 r
= sd_netlink_message_close_container(req
);
1542 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1545 r
= sd_netlink_message_close_container(req
);
1547 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1549 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_up_handler
, link
, 0, NULL
);
1551 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1558 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1559 _cleanup_link_unref_ Link
*link
= userdata
;
1564 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1567 r
= sd_netlink_message_get_errno(m
);
1569 log_link_warning_errno(link
, r
, "Could not bring down interface: %m");
1574 static int link_down(Link
*link
) {
1575 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1579 assert(link
->manager
);
1580 assert(link
->manager
->rtnl
);
1582 log_link_debug(link
, "Bringing link down");
1584 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
1585 RTM_SETLINK
, link
->ifindex
);
1587 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1589 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
1591 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1593 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_down_handler
, link
, 0, NULL
);
1595 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1602 static int link_handle_bound_to_list(Link
*link
) {
1606 bool required_up
= false;
1607 bool link_is_up
= false;
1611 if (hashmap_isempty(link
->bound_to_links
))
1614 if (link
->flags
& IFF_UP
)
1617 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
1618 if (link_has_carrier(l
)) {
1623 if (!required_up
&& link_is_up
) {
1624 r
= link_down(link
);
1627 } else if (required_up
&& !link_is_up
) {
1636 static int link_handle_bound_by_list(Link
*link
) {
1643 if (hashmap_isempty(link
->bound_by_links
))
1646 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
1647 r
= link_handle_bound_to_list(l
);
1655 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
1661 if (link
== carrier
)
1664 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
1667 r
= hashmap_ensure_allocated(h
, NULL
);
1671 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
1678 static int link_new_bound_by_list(Link
*link
) {
1683 bool list_updated
= false;
1686 assert(link
->manager
);
1690 HASHMAP_FOREACH(carrier
, m
->links
, i
) {
1691 if (!carrier
->network
)
1694 if (strv_isempty(carrier
->network
->bind_carrier
))
1697 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
, 0)) {
1698 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
1702 list_updated
= true;
1709 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
1710 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
1714 link_dirty(carrier
);
1720 static int link_new_bound_to_list(Link
*link
) {
1725 bool list_updated
= false;
1728 assert(link
->manager
);
1733 if (strv_isempty(link
->network
->bind_carrier
))
1738 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
1739 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
, 0)) {
1740 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
1744 list_updated
= true;
1751 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
1752 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
1756 link_dirty(carrier
);
1762 static int link_new_carrier_maps(Link
*link
) {
1765 r
= link_new_bound_by_list(link
);
1769 r
= link_handle_bound_by_list(link
);
1773 r
= link_new_bound_to_list(link
);
1777 r
= link_handle_bound_to_list(link
);
1784 static void link_free_bound_to_list(Link
*link
) {
1788 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
1789 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
1791 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
1792 link_dirty(bound_to
);
1798 static void link_free_bound_by_list(Link
*link
) {
1802 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
1803 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
1805 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
1806 link_dirty(bound_by
);
1807 link_handle_bound_to_list(bound_by
);
1814 static void link_free_carrier_maps(Link
*link
) {
1815 bool list_updated
= false;
1819 if (!hashmap_isempty(link
->bound_to_links
)) {
1820 link_free_bound_to_list(link
);
1821 list_updated
= true;
1824 if (!hashmap_isempty(link
->bound_by_links
)) {
1825 link_free_bound_by_list(link
);
1826 list_updated
= true;
1835 void link_drop(Link
*link
) {
1836 if (!link
|| link
->state
== LINK_STATE_LINGER
)
1839 link_set_state(link
, LINK_STATE_LINGER
);
1841 link_free_carrier_maps(link
);
1843 log_link_debug(link
, "Link removed");
1845 (void)unlink(link
->state_file
);
1851 static int link_joined(Link
*link
) {
1855 assert(link
->network
);
1857 if (!hashmap_isempty(link
->bound_to_links
)) {
1858 r
= link_handle_bound_to_list(link
);
1861 } else if (!(link
->flags
& IFF_UP
)) {
1864 link_enter_failed(link
);
1869 if (link
->network
->bridge
) {
1870 r
= link_set_bridge(link
);
1872 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
1875 return link_enter_set_addresses(link
);
1878 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1879 _cleanup_link_unref_ Link
*link
= userdata
;
1883 assert(link
->network
);
1887 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1890 r
= sd_netlink_message_get_errno(m
);
1891 if (r
< 0 && r
!= -EEXIST
) {
1892 log_link_error_errno(link
, r
, "Could not join netdev: %m");
1893 link_enter_failed(link
);
1896 log_link_debug(link
, "Joined netdev");
1898 if (link
->enslaving
<= 0)
1904 static int link_enter_join_netdev(Link
*link
) {
1910 assert(link
->network
);
1911 assert(link
->state
== LINK_STATE_PENDING
);
1913 link_set_state(link
, LINK_STATE_ENSLAVING
);
1917 if (!link
->network
->bridge
&&
1918 !link
->network
->bond
&&
1919 hashmap_isempty(link
->network
->stacked_netdevs
))
1920 return link_joined(link
);
1922 if (link
->network
->bond
) {
1923 log_struct(LOG_DEBUG
,
1924 LOG_LINK_INTERFACE(link
),
1925 LOG_NETDEV_INTERFACE(link
->network
->bond
),
1926 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
),
1929 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
1931 log_struct_errno(LOG_WARNING
, r
,
1932 LOG_LINK_INTERFACE(link
),
1933 LOG_NETDEV_INTERFACE(link
->network
->bond
),
1934 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
),
1937 link_enter_failed(link
);
1944 if (link
->network
->bridge
) {
1945 log_struct(LOG_DEBUG
,
1946 LOG_LINK_INTERFACE(link
),
1947 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
1948 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
),
1951 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
1953 log_struct_errno(LOG_WARNING
, r
,
1954 LOG_LINK_INTERFACE(link
),
1955 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
1956 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
),
1958 link_enter_failed(link
);
1965 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
1967 log_struct(LOG_DEBUG
,
1968 LOG_LINK_INTERFACE(link
),
1969 LOG_NETDEV_INTERFACE(netdev
),
1970 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
),
1973 r
= netdev_join(netdev
, link
, netdev_join_handler
);
1975 log_struct_errno(LOG_WARNING
, r
,
1976 LOG_LINK_INTERFACE(link
),
1977 LOG_NETDEV_INTERFACE(netdev
),
1978 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
),
1980 link_enter_failed(link
);
1990 static int link_set_ipv4_forward(Link
*link
) {
1993 if (!link_ipv4_forward_enabled(link
))
1996 /* We propagate the forwarding flag from one interface to the
1997 * global setting one way. This means: as long as at least one
1998 * interface was configured at any time that had IP forwarding
1999 * enabled the setting will stay on for good. We do this
2000 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
2001 * somewhat in sync (see below). */
2003 r
= write_string_file("/proc/sys/net/ipv4/ip_forward", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2005 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
2010 static int link_set_ipv6_forward(Link
*link
) {
2013 if (!link_ipv6_forward_enabled(link
))
2016 /* On Linux, the IPv6 stack does not not know a per-interface
2017 * packet forwarding setting: either packet forwarding is on
2018 * for all, or off for all. We hence don't bother with a
2019 * per-interface setting, but simply propagate the interface
2020 * flag, if it is set, to the global flag, one-way. Note that
2021 * while IPv4 would allow a per-interface flag, we expose the
2022 * same behaviour there and also propagate the setting from
2023 * one to all, to keep things simple (see above). */
2025 r
= write_string_file("/proc/sys/net/ipv6/conf/all/forwarding", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2027 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
2032 static int link_set_ipv6_privacy_extensions(Link
*link
) {
2033 char buf
[DECIMAL_STR_MAX(unsigned) + 1];
2034 IPv6PrivacyExtensions s
;
2035 const char *p
= NULL
;
2038 s
= link_ipv6_privacy_extensions(link
);
2042 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/use_tempaddr");
2043 xsprintf(buf
, "%u", (unsigned) link
->network
->ipv6_privacy_extensions
);
2045 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2047 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
2052 static int link_set_ipv6_accept_ra(Link
*link
) {
2053 const char *p
= NULL
;
2056 /* Make this a NOP if IPv6 is not available */
2057 if (!socket_ipv6_is_supported())
2060 if (link
->flags
& IFF_LOOPBACK
)
2066 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/accept_ra");
2068 /* We handle router advertisments ourselves, tell the kernel to GTFO */
2069 r
= write_string_file(p
, "0", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2071 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
2076 static int link_set_ipv6_dad_transmits(Link
*link
) {
2077 char buf
[DECIMAL_STR_MAX(int) + 1];
2078 const char *p
= NULL
;
2081 /* Make this a NOP if IPv6 is not available */
2082 if (!socket_ipv6_is_supported())
2085 if (link
->flags
& IFF_LOOPBACK
)
2091 if (link
->network
->ipv6_dad_transmits
< 0)
2094 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/dad_transmits");
2095 xsprintf(buf
, "%i", link
->network
->ipv6_dad_transmits
);
2097 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2099 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
2104 static int link_set_ipv6_hop_limit(Link
*link
) {
2105 char buf
[DECIMAL_STR_MAX(int) + 1];
2106 const char *p
= NULL
;
2109 /* Make this a NOP if IPv6 is not available */
2110 if (!socket_ipv6_is_supported())
2113 if (link
->flags
& IFF_LOOPBACK
)
2119 if (link
->network
->ipv6_hop_limit
< 0)
2122 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/hop_limit");
2123 xsprintf(buf
, "%i", link
->network
->ipv6_hop_limit
);
2125 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2127 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2132 static int link_drop_foreign_config(Link
*link
) {
2138 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
2139 /* we consider IPv6LL addresses to be managed by the kernel */
2140 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2143 r
= address_remove(address
, link
, link_address_remove_handler
);
2148 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2149 /* do not touch routes managed by the kernel */
2150 if (route
->protocol
== RTPROT_KERNEL
)
2153 r
= route_remove(route
, link
, link_address_remove_handler
);
2161 static int link_update_lldp(Link
*link
) {
2169 if (link
->flags
& IFF_UP
) {
2170 r
= sd_lldp_start(link
->lldp
);
2172 log_link_debug(link
, "Started LLDP.");
2174 r
= sd_lldp_stop(link
->lldp
);
2176 log_link_debug(link
, "Stopped LLDP.");
2182 static int link_configure(Link
*link
) {
2186 assert(link
->network
);
2187 assert(link
->state
== LINK_STATE_PENDING
);
2189 /* Drop foreign config, but ignore loopback or critical devices.
2190 * We do not want to remove loopback address or addresses used for root NFS. */
2191 if (!(link
->flags
& IFF_LOOPBACK
) && !(link
->network
->dhcp_critical
)) {
2192 r
= link_drop_foreign_config(link
);
2197 r
= link_set_bridge_fdb(link
);
2201 r
= link_set_proxy_arp(link
);
2205 r
= link_set_ipv4_forward(link
);
2209 r
= link_set_ipv6_forward(link
);
2213 r
= link_set_ipv6_privacy_extensions(link
);
2217 r
= link_set_ipv6_accept_ra(link
);
2221 r
= link_set_ipv6_dad_transmits(link
);
2225 r
= link_set_ipv6_hop_limit(link
);
2229 if (link_ipv4ll_enabled(link
)) {
2230 r
= ipv4ll_configure(link
);
2235 if (link_dhcp4_enabled(link
)) {
2236 r
= dhcp4_configure(link
);
2241 if (link_dhcp4_server_enabled(link
)) {
2242 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
2246 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
2251 if (link_dhcp6_enabled(link
) ||
2252 link_ipv6_accept_ra_enabled(link
)) {
2253 r
= dhcp6_configure(link
);
2258 if (link_ipv6_accept_ra_enabled(link
)) {
2259 r
= ndisc_configure(link
);
2264 if (link_lldp_rx_enabled(link
)) {
2265 r
= sd_lldp_new(&link
->lldp
, link
->ifindex
);
2269 r
= sd_lldp_match_capabilities(link
->lldp
,
2270 link
->network
->lldp_mode
== LLDP_MODE_ROUTERS_ONLY
?
2271 SD_LLDP_SYSTEM_CAPABILITIES_ALL_ROUTERS
:
2272 SD_LLDP_SYSTEM_CAPABILITIES_ALL
);
2276 r
= sd_lldp_set_filter_address(link
->lldp
, &link
->mac
);
2280 r
= sd_lldp_attach_event(link
->lldp
, NULL
, 0);
2284 r
= sd_lldp_set_callback(link
->lldp
, lldp_handler
, link
);
2288 r
= link_update_lldp(link
);
2293 if (link_has_carrier(link
)) {
2294 r
= link_acquire_conf(link
);
2298 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) == 0) {
2299 r
= link_acquire_ipv6_conf(link
);
2305 return link_enter_join_netdev(link
);
2308 static int link_initialized_and_synced(sd_netlink
*rtnl
, sd_netlink_message
*m
,
2310 _cleanup_link_unref_ Link
*link
= userdata
;
2315 assert(link
->ifname
);
2316 assert(link
->manager
);
2318 if (link
->state
!= LINK_STATE_PENDING
)
2321 log_link_debug(link
, "Link state is up-to-date");
2323 r
= link_new_bound_by_list(link
);
2327 r
= link_handle_bound_by_list(link
);
2331 if (!link
->network
) {
2332 r
= network_get(link
->manager
, link
->udev_device
, link
->ifname
,
2333 &link
->mac
, &network
);
2335 link_enter_unmanaged(link
);
2340 if (link
->flags
& IFF_LOOPBACK
) {
2341 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
2342 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
2344 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
2345 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
2347 if (network
->dhcp_server
)
2348 log_link_debug(link
, "Ignoring DHCP server for loopback link");
2351 r
= network_apply(link
->manager
, network
, link
);
2356 r
= link_new_bound_to_list(link
);
2360 r
= link_configure(link
);
2367 int link_initialized(Link
*link
, struct udev_device
*device
) {
2368 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2372 assert(link
->manager
);
2373 assert(link
->manager
->rtnl
);
2376 if (link
->state
!= LINK_STATE_PENDING
)
2379 if (link
->udev_device
)
2382 log_link_debug(link
, "udev initialized link");
2384 link
->udev_device
= udev_device_ref(device
);
2386 /* udev has initialized the link, but we don't know if we have yet
2387 * processed the NEWLINK messages with the latest state. Do a GETLINK,
2388 * when it returns we know that the pending NEWLINKs have already been
2389 * processed and that we are up-to-date */
2391 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
2396 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
,
2397 link_initialized_and_synced
, link
, 0, NULL
);
2406 static int link_load(Link
*link
) {
2407 _cleanup_free_
char *network_file
= NULL
,
2410 *dhcp4_address
= NULL
,
2411 *ipv4ll_address
= NULL
;
2412 union in_addr_union address
;
2413 union in_addr_union route_dst
;
2419 r
= parse_env_file(link
->state_file
, NEWLINE
,
2420 "NETWORK_FILE", &network_file
,
2421 "ADDRESSES", &addresses
,
2423 "DHCP4_ADDRESS", &dhcp4_address
,
2424 "IPV4LL_ADDRESS", &ipv4ll_address
,
2426 if (r
< 0 && r
!= -ENOENT
)
2427 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
2434 suffix
= strrchr(network_file
, '.');
2436 log_link_debug(link
, "Failed to get network name from %s", network_file
);
2437 goto network_file_fail
;
2441 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
2443 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
2444 goto network_file_fail
;
2447 r
= network_apply(link
->manager
, network
, link
);
2449 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
2458 _cleanup_free_
char *address_str
= NULL
;
2459 char *prefixlen_str
;
2461 unsigned char prefixlen
;
2463 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
2465 log_link_debug_errno(link
, r
, "Failed to extract next address string: %m");
2471 prefixlen_str
= strchr(address_str
, '/');
2472 if (!prefixlen_str
) {
2473 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
2477 *prefixlen_str
++ = '\0';
2479 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
2481 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
2485 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
2487 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
2491 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
2493 return log_link_error_errno(link
, r
, "Failed to add address: %m");
2502 _cleanup_free_
char *route_str
= NULL
;
2503 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
2505 char *prefixlen_str
;
2507 unsigned char prefixlen
, tos
, table
;
2510 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
2512 log_link_debug_errno(link
, r
, "Failed to extract next route string: %m");
2518 prefixlen_str
= strchr(route_str
, '/');
2519 if (!prefixlen_str
) {
2520 log_link_debug(link
, "Failed to parse route %s", route_str
);
2524 *prefixlen_str
++ = '\0';
2526 r
= sscanf(prefixlen_str
, "%hhu/%hhu/%"SCNu32
"/%hhu/"USEC_FMT
, &prefixlen
, &tos
, &priority
, &table
, &lifetime
);
2528 log_link_debug(link
,
2529 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
2534 r
= in_addr_from_string_auto(route_str
, &family
, &route_dst
);
2536 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
2540 r
= route_add(link
, family
, &route_dst
, prefixlen
, tos
, priority
, table
, &route
);
2542 return log_link_error_errno(link
, r
, "Failed to add route: %m");
2544 if (lifetime
!= USEC_INFINITY
) {
2545 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(), lifetime
,
2546 0, route_expire_handler
, route
);
2548 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
2551 route
->lifetime
= lifetime
;
2552 sd_event_source_unref(route
->expire
);
2553 route
->expire
= expire
;
2558 if (dhcp4_address
) {
2559 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
2561 log_link_debug_errno(link
, r
, "Falied to parse DHCPv4 address %s: %m", dhcp4_address
);
2562 goto dhcp4_address_fail
;
2565 r
= sd_dhcp_client_new(&link
->dhcp_client
);
2567 return log_link_error_errno(link
, r
, "Falied to create DHCPv4 client: %m");
2569 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
2571 return log_link_error_errno(link
, r
, "Falied to set inital DHCPv4 address %s: %m", dhcp4_address
);
2576 if (ipv4ll_address
) {
2577 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
2579 log_link_debug_errno(link
, r
, "Falied to parse IPv4LL address %s: %m", ipv4ll_address
);
2580 goto ipv4ll_address_fail
;
2583 r
= sd_ipv4ll_new(&link
->ipv4ll
);
2585 return log_link_error_errno(link
, r
, "Falied to create IPv4LL client: %m");
2587 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
2589 return log_link_error_errno(link
, r
, "Falied to set inital IPv4LL address %s: %m", ipv4ll_address
);
2592 ipv4ll_address_fail
:
2597 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
2599 _cleanup_udev_device_unref_
struct udev_device
*device
= NULL
;
2600 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
2608 r
= link_new(m
, message
, ret
);
2614 log_link_debug(link
, "Link %d added", link
->ifindex
);
2616 r
= link_load(link
);
2620 if (detect_container() <= 0) {
2621 /* not in a container, udev will be around */
2622 sprintf(ifindex_str
, "n%d", link
->ifindex
);
2623 device
= udev_device_new_from_device_id(m
->udev
, ifindex_str
);
2625 r
= log_link_warning_errno(link
, errno
, "Could not find udev device: %m");
2629 if (udev_device_get_is_initialized(device
) <= 0) {
2631 log_link_debug(link
, "link pending udev initialization...");
2635 r
= link_initialized(link
, device
);
2639 /* we are calling a callback directly, so must take a ref */
2642 r
= link_initialized_and_synced(m
->rtnl
, NULL
, link
);
2649 link_enter_failed(link
);
2653 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
2658 log_link_info(link
, "Gained IPv6LL");
2660 link
->ipv6ll_address
= *address
;
2661 link_check_ready(link
);
2663 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
2664 r
= link_acquire_ipv6_conf(link
);
2666 link_enter_failed(link
);
2674 static int link_carrier_gained(Link
*link
) {
2679 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
2680 r
= link_acquire_conf(link
);
2682 link_enter_failed(link
);
2687 r
= link_handle_bound_by_list(link
);
2694 static int link_carrier_lost(Link
*link
) {
2699 r
= link_stop_clients(link
);
2701 link_enter_failed(link
);
2705 r
= link_handle_bound_by_list(link
);
2712 int link_carrier_reset(Link
*link
) {
2717 if (link_has_carrier(link
)) {
2718 r
= link_carrier_lost(link
);
2722 r
= link_carrier_gained(link
);
2726 log_link_info(link
, "Reset carrier");
2732 int link_update(Link
*link
, sd_netlink_message
*m
) {
2733 struct ether_addr mac
;
2736 bool had_carrier
, carrier_gained
, carrier_lost
;
2740 assert(link
->ifname
);
2743 if (link
->state
== LINK_STATE_LINGER
) {
2745 log_link_info(link
, "Link readded");
2746 link_set_state(link
, LINK_STATE_ENSLAVING
);
2748 r
= link_new_carrier_maps(link
);
2753 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
2754 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
2755 log_link_info(link
, "Renamed to %s", ifname
);
2757 link_free_carrier_maps(link
);
2759 r
= free_and_strdup(&link
->ifname
, ifname
);
2763 r
= link_new_carrier_maps(link
);
2768 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
2769 if (r
>= 0 && mtu
> 0) {
2771 if (!link
->original_mtu
) {
2772 link
->original_mtu
= mtu
;
2773 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
2776 if (link
->dhcp_client
) {
2777 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
2780 log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
2786 /* The kernel may broadcast NEWLINK messages without the MAC address
2787 set, simply ignore them. */
2788 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
2790 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
2793 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
2796 log_link_debug(link
, "MAC address: "
2797 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
2798 mac
.ether_addr_octet
[0],
2799 mac
.ether_addr_octet
[1],
2800 mac
.ether_addr_octet
[2],
2801 mac
.ether_addr_octet
[3],
2802 mac
.ether_addr_octet
[4],
2803 mac
.ether_addr_octet
[5]);
2806 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
2808 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
2811 if (link
->dhcp_client
) {
2812 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
2813 (const uint8_t *) &link
->mac
,
2817 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
2819 if (link
->network
->duid_type
!= _DUID_TYPE_INVALID
)
2820 r
= sd_dhcp_client_set_iaid_duid(link
->dhcp_client
,
2821 link
->network
->iaid
,
2822 link
->network
->dhcp_duid_type
,
2823 link
->network
->dhcp_duid
,
2824 link
->network
->dhcp_duid_len
);
2826 r
= sd_dhcp_client_set_iaid_duid(link
->dhcp_client
,
2827 link
->network
->iaid
,
2828 link
->manager
->dhcp_duid_type
,
2829 link
->manager
->dhcp_duid
,
2830 link
->manager
->dhcp_duid_len
);
2832 return log_link_warning_errno(link
, r
, "Could not update DUID/IAID in DHCP client: %m");
2835 if (link
->dhcp6_client
) {
2836 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
2837 (const uint8_t *) &link
->mac
,
2841 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
2843 r
= sd_dhcp6_client_set_iaid(link
->dhcp6_client
,
2844 link
->network
->iaid
);
2846 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 IAID: %m");
2848 if (link
->network
->duid_type
!= _DUID_TYPE_INVALID
)
2849 r
= sd_dhcp6_client_set_duid(link
->dhcp6_client
,
2850 link
->network
->dhcp_duid_type
,
2851 link
->network
->dhcp_duid
,
2852 link
->network
->dhcp_duid_len
);
2854 r
= sd_dhcp6_client_set_duid(link
->dhcp6_client
,
2855 link
->manager
->dhcp_duid_type
,
2856 link
->manager
->dhcp_duid
,
2857 link
->manager
->dhcp_duid_len
);
2859 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 DUID: %m");
2864 had_carrier
= link_has_carrier(link
);
2866 r
= link_update_flags(link
, m
);
2870 r
= link_update_lldp(link
);
2874 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
2875 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
2877 if (carrier_gained
) {
2878 log_link_info(link
, "Gained carrier");
2880 r
= link_carrier_gained(link
);
2883 } else if (carrier_lost
) {
2884 log_link_info(link
, "Lost carrier");
2886 r
= link_carrier_lost(link
);
2894 static void print_link_hashmap(FILE *f
, const char *prefix
, Hashmap
* h
) {
2902 if (hashmap_isempty(h
))
2906 HASHMAP_FOREACH(link
, h
, i
) {
2910 fprintf(f
, "%i", link
->ifindex
);
2917 int link_save(Link
*link
) {
2918 _cleanup_free_
char *temp_path
= NULL
;
2919 _cleanup_fclose_
FILE *f
= NULL
;
2920 const char *admin_state
, *oper_state
;
2927 assert(link
->state_file
);
2928 assert(link
->lease_file
);
2929 assert(link
->manager
);
2931 if (link
->state
== LINK_STATE_LINGER
) {
2932 unlink(link
->state_file
);
2936 link_lldp_save(link
);
2938 admin_state
= link_state_to_string(link
->state
);
2939 assert(admin_state
);
2941 oper_state
= link_operstate_to_string(link
->operstate
);
2944 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
2948 fchmod(fileno(f
), 0644);
2951 "# This is private data. Do not parse.\n"
2954 admin_state
, oper_state
);
2956 if (link
->network
) {
2958 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
2959 const char *dhcp_domainname
= NULL
;
2960 char **dhcp6_domains
= NULL
;
2962 if (link
->dhcp6_client
) {
2963 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
, &dhcp6_lease
);
2964 if (r
< 0 && r
!= -ENOMSG
)
2965 log_link_debug(link
, "No DHCPv6 lease");
2968 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
2972 fputstrv(f
, link
->network
->dns
, NULL
, &space
);
2974 if (link
->network
->dhcp_use_dns
&&
2976 const struct in_addr
*addresses
;
2978 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
2982 serialize_in_addrs(f
, addresses
, r
);
2987 if (link
->network
->dhcp_use_dns
&& dhcp6_lease
) {
2988 struct in6_addr
*in6_addrs
;
2990 r
= sd_dhcp6_lease_get_dns(dhcp6_lease
, &in6_addrs
);
2994 serialize_in6_addrs(f
, in6_addrs
, r
);
3002 fputstrv(f
, link
->network
->ntp
, NULL
, &space
);
3004 if (link
->network
->dhcp_use_ntp
&&
3006 const struct in_addr
*addresses
;
3008 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
3012 serialize_in_addrs(f
, addresses
, r
);
3017 if (link
->network
->dhcp_use_ntp
&& dhcp6_lease
) {
3018 struct in6_addr
*in6_addrs
;
3021 r
= sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease
,
3026 serialize_in6_addrs(f
, in6_addrs
, r
);
3030 r
= sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease
, &hosts
);
3032 fputstrv(f
, hosts
, NULL
, &space
);
3037 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
3038 if (link
->dhcp_lease
)
3039 (void) sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &dhcp_domainname
);
3042 (void) sd_dhcp6_lease_get_domains(dhcp6_lease
, &dhcp6_domains
);
3045 fputs("DOMAINS=", f
);
3046 fputstrv(f
, link
->network
->search_domains
, NULL
, &space
);
3048 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
&& dhcp_domainname
)
3049 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3051 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
&& dhcp6_domains
)
3052 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3056 fputs("ROUTE_DOMAINS=", f
);
3057 fputstrv(f
, link
->network
->route_domains
, NULL
, NULL
);
3059 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
&& dhcp_domainname
)
3060 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3062 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
&& dhcp6_domains
)
3063 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3067 fprintf(f
, "LLMNR=%s\n",
3068 resolve_support_to_string(link
->network
->llmnr
));
3069 fprintf(f
, "MDNS=%s\n",
3070 resolve_support_to_string(link
->network
->mdns
));
3072 if (link
->network
->dnssec_mode
!= _DNSSEC_MODE_INVALID
)
3073 fprintf(f
, "DNSSEC=%s\n",
3074 dnssec_mode_to_string(link
->network
->dnssec_mode
));
3076 if (!set_isempty(link
->network
->dnssec_negative_trust_anchors
)) {
3079 fputs("DNSSEC_NTA=", f
);
3081 SET_FOREACH(n
, link
->network
->dnssec_negative_trust_anchors
, i
)
3082 fputs_with_space(f
, n
, NULL
, &space
);
3086 fputs("ADDRESSES=", f
);
3088 SET_FOREACH(a
, link
->addresses
, i
) {
3089 _cleanup_free_
char *address_str
= NULL
;
3091 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
3095 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
3100 fputs("ROUTES=", f
);
3102 SET_FOREACH(route
, link
->routes
, i
) {
3103 _cleanup_free_
char *route_str
= NULL
;
3105 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
3109 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%hhu/"USEC_FMT
, space
? " " : "", route_str
,
3110 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
3117 print_link_hashmap(f
, "CARRIER_BOUND_TO=", link
->bound_to_links
);
3118 print_link_hashmap(f
, "CARRIER_BOUND_BY=", link
->bound_by_links
);
3120 if (link
->dhcp_lease
) {
3121 struct in_addr address
;
3122 const char *tz
= NULL
;
3124 assert(link
->network
);
3126 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
3128 fprintf(f
, "TIMEZONE=%s\n", tz
);
3130 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
3132 fputs("DHCP4_ADDRESS=", f
);
3133 serialize_in_addrs(f
, &address
, 1);
3137 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
3145 unlink(link
->lease_file
);
3148 struct in_addr address
;
3150 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
3152 fputs("IPV4LL_ADDRESS=", f
);
3153 serialize_in_addrs(f
, &address
, 1);
3158 r
= fflush_and_check(f
);
3162 if (rename(temp_path
, link
->state_file
) < 0) {
3170 (void) unlink(link
->state_file
);
3172 (void) unlink(temp_path
);
3174 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
3177 /* The serialized state in /run is no longer up-to-date. */
3178 void link_dirty(Link
*link
) {
3183 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
3185 /* allocation errors are ignored */
3188 r
= set_put(link
->manager
->dirty_links
, link
);
3190 /* allocation errors are ignored */
3196 /* The serialized state in /run is up-to-date */
3197 void link_clean(Link
*link
) {
3199 assert(link
->manager
);
3201 set_remove(link
->manager
->dirty_links
, link
);
3205 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
3206 [LINK_STATE_PENDING
] = "pending",
3207 [LINK_STATE_ENSLAVING
] = "configuring",
3208 [LINK_STATE_SETTING_ADDRESSES
] = "configuring",
3209 [LINK_STATE_SETTING_ROUTES
] = "configuring",
3210 [LINK_STATE_CONFIGURED
] = "configured",
3211 [LINK_STATE_UNMANAGED
] = "unmanaged",
3212 [LINK_STATE_FAILED
] = "failed",
3213 [LINK_STATE_LINGER
] = "linger",
3216 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);
3218 static const char* const link_operstate_table
[_LINK_OPERSTATE_MAX
] = {
3219 [LINK_OPERSTATE_OFF
] = "off",
3220 [LINK_OPERSTATE_NO_CARRIER
] = "no-carrier",
3221 [LINK_OPERSTATE_DORMANT
] = "dormant",
3222 [LINK_OPERSTATE_CARRIER
] = "carrier",
3223 [LINK_OPERSTATE_DEGRADED
] = "degraded",
3224 [LINK_OPERSTATE_ROUTABLE
] = "routable",
3227 DEFINE_STRING_TABLE_LOOKUP(link_operstate
, LinkOperationalState
);