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"
30 #include "netlink-util.h"
31 #include "network-internal.h"
32 #include "networkd-link.h"
33 #include "networkd-lldp-tx.h"
34 #include "networkd-netdev.h"
36 #include "socket-util.h"
37 #include "stdio-util.h"
38 #include "string-table.h"
39 #include "udev-util.h"
43 static bool link_dhcp6_enabled(Link
*link
) {
46 if (link
->flags
& IFF_LOOPBACK
)
52 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV6
;
55 static bool link_dhcp4_enabled(Link
*link
) {
58 if (link
->flags
& IFF_LOOPBACK
)
64 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV4
;
67 static bool link_dhcp4_server_enabled(Link
*link
) {
70 if (link
->flags
& IFF_LOOPBACK
)
76 return link
->network
->dhcp_server
;
79 static bool link_ipv4ll_enabled(Link
*link
) {
82 if (link
->flags
& IFF_LOOPBACK
)
88 return link
->network
->link_local
& ADDRESS_FAMILY_IPV4
;
91 static bool link_ipv6ll_enabled(Link
*link
) {
94 if (link
->flags
& IFF_LOOPBACK
)
100 return link
->network
->link_local
& ADDRESS_FAMILY_IPV6
;
103 static bool link_lldp_rx_enabled(Link
*link
) {
106 if (link
->flags
& IFF_LOOPBACK
)
109 if (link
->iftype
!= ARPHRD_ETHER
)
115 if (link
->network
->bridge
)
118 return link
->network
->lldp_mode
!= LLDP_MODE_NO
;
121 static bool link_lldp_tx_enabled(Link
*link
) {
124 if (link
->flags
& IFF_LOOPBACK
)
127 if (link
->iftype
!= ARPHRD_ETHER
)
133 return link
->network
->lldp_emit
;
136 static bool link_ipv4_forward_enabled(Link
*link
) {
139 if (link
->flags
& IFF_LOOPBACK
)
145 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
148 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV4
;
151 static bool link_ipv6_forward_enabled(Link
*link
) {
154 if (!socket_ipv6_is_supported())
157 if (link
->flags
& IFF_LOOPBACK
)
163 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
166 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV6
;
169 static bool link_ipv6_accept_ra_enabled(Link
*link
) {
172 if (link
->flags
& IFF_LOOPBACK
)
178 /* If unset use system default (enabled if local forwarding is disabled.
179 * disabled if local forwarding is enabled).
180 * If set, ignore or enforce RA independent of local forwarding state.
182 if (link
->network
->ipv6_accept_ra
< 0)
183 /* default to accept RA if ip_forward is disabled and ignore RA if ip_forward is enabled */
184 return !link_ipv6_forward_enabled(link
);
185 else if (link
->network
->ipv6_accept_ra
> 0)
186 /* accept RA even if ip_forward is enabled */
193 static IPv6PrivacyExtensions
link_ipv6_privacy_extensions(Link
*link
) {
195 if (!socket_ipv6_is_supported())
196 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
198 if (link
->flags
& IFF_LOOPBACK
)
199 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
202 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
204 return link
->network
->ipv6_privacy_extensions
;
207 void link_update_operstate(Link
*link
) {
208 LinkOperationalState operstate
;
211 if (link
->kernel_operstate
== IF_OPER_DORMANT
)
212 operstate
= LINK_OPERSTATE_DORMANT
;
213 else if (link_has_carrier(link
)) {
215 uint8_t scope
= RT_SCOPE_NOWHERE
;
218 /* if we have carrier, check what addresses we have */
219 SET_FOREACH(address
, link
->addresses
, i
) {
220 if (!address_is_ready(address
))
223 if (address
->scope
< scope
)
224 scope
= address
->scope
;
227 /* for operstate we also take foreign addresses into account */
228 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
229 if (!address_is_ready(address
))
232 if (address
->scope
< scope
)
233 scope
= address
->scope
;
236 if (scope
< RT_SCOPE_SITE
)
237 /* universally accessible addresses found */
238 operstate
= LINK_OPERSTATE_ROUTABLE
;
239 else if (scope
< RT_SCOPE_HOST
)
240 /* only link or site local addresses found */
241 operstate
= LINK_OPERSTATE_DEGRADED
;
243 /* no useful addresses found */
244 operstate
= LINK_OPERSTATE_CARRIER
;
245 } else if (link
->flags
& IFF_UP
)
246 operstate
= LINK_OPERSTATE_NO_CARRIER
;
248 operstate
= LINK_OPERSTATE_OFF
;
250 if (link
->operstate
!= operstate
) {
251 link
->operstate
= operstate
;
252 link_send_changed(link
, "OperationalState", NULL
);
254 manager_dirty(link
->manager
);
258 #define FLAG_STRING(string, flag, old, new) \
259 (((old ^ new) & flag) \
260 ? ((old & flag) ? (" -" string) : (" +" string)) \
263 static int link_update_flags(Link
*link
, sd_netlink_message
*m
) {
264 unsigned flags
, unknown_flags_added
, unknown_flags_removed
, unknown_flags
;
270 r
= sd_rtnl_message_link_get_flags(m
, &flags
);
272 return log_link_warning_errno(link
, r
, "Could not get link flags: %m");
274 r
= sd_netlink_message_read_u8(m
, IFLA_OPERSTATE
, &operstate
);
276 /* if we got a message without operstate, take it to mean
277 the state was unchanged */
278 operstate
= link
->kernel_operstate
;
280 if ((link
->flags
== flags
) && (link
->kernel_operstate
== operstate
))
283 if (link
->flags
!= flags
) {
284 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",
285 FLAG_STRING("LOOPBACK", IFF_LOOPBACK
, link
->flags
, flags
),
286 FLAG_STRING("MASTER", IFF_MASTER
, link
->flags
, flags
),
287 FLAG_STRING("SLAVE", IFF_SLAVE
, link
->flags
, flags
),
288 FLAG_STRING("UP", IFF_UP
, link
->flags
, flags
),
289 FLAG_STRING("DORMANT", IFF_DORMANT
, link
->flags
, flags
),
290 FLAG_STRING("LOWER_UP", IFF_LOWER_UP
, link
->flags
, flags
),
291 FLAG_STRING("RUNNING", IFF_RUNNING
, link
->flags
, flags
),
292 FLAG_STRING("MULTICAST", IFF_MULTICAST
, link
->flags
, flags
),
293 FLAG_STRING("BROADCAST", IFF_BROADCAST
, link
->flags
, flags
),
294 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT
, link
->flags
, flags
),
295 FLAG_STRING("PROMISC", IFF_PROMISC
, link
->flags
, flags
),
296 FLAG_STRING("ALLMULTI", IFF_ALLMULTI
, link
->flags
, flags
),
297 FLAG_STRING("PORTSEL", IFF_PORTSEL
, link
->flags
, flags
),
298 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA
, link
->flags
, flags
),
299 FLAG_STRING("DYNAMIC", IFF_DYNAMIC
, link
->flags
, flags
),
300 FLAG_STRING("NOARP", IFF_NOARP
, link
->flags
, flags
),
301 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS
, link
->flags
, flags
),
302 FLAG_STRING("DEBUG", IFF_DEBUG
, link
->flags
, flags
),
303 FLAG_STRING("ECHO", IFF_ECHO
, link
->flags
, flags
));
305 unknown_flags
= ~(IFF_LOOPBACK
| IFF_MASTER
| IFF_SLAVE
| IFF_UP
|
306 IFF_DORMANT
| IFF_LOWER_UP
| IFF_RUNNING
|
307 IFF_MULTICAST
| IFF_BROADCAST
| IFF_POINTOPOINT
|
308 IFF_PROMISC
| IFF_ALLMULTI
| IFF_PORTSEL
|
309 IFF_AUTOMEDIA
| IFF_DYNAMIC
| IFF_NOARP
|
310 IFF_NOTRAILERS
| IFF_DEBUG
| IFF_ECHO
);
311 unknown_flags_added
= ((link
->flags
^ flags
) & flags
& unknown_flags
);
312 unknown_flags_removed
= ((link
->flags
^ flags
) & link
->flags
& unknown_flags
);
314 /* link flags are currently at most 18 bits, let's align to
316 if (unknown_flags_added
)
318 "Unknown link flags gained: %#.5x (ignoring)",
319 unknown_flags_added
);
321 if (unknown_flags_removed
)
323 "Unknown link flags lost: %#.5x (ignoring)",
324 unknown_flags_removed
);
328 link
->kernel_operstate
= operstate
;
330 link_update_operstate(link
);
335 static int link_new(Manager
*manager
, sd_netlink_message
*message
, Link
**ret
) {
336 _cleanup_link_unref_ Link
*link
= NULL
;
340 unsigned short iftype
;
346 r
= sd_netlink_message_get_type(message
, &type
);
349 else if (type
!= RTM_NEWLINK
)
352 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
355 else if (ifindex
<= 0)
358 r
= sd_rtnl_message_link_get_type(message
, &iftype
);
362 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &ifname
);
366 link
= new0(Link
, 1);
371 link
->manager
= manager
;
372 link
->state
= LINK_STATE_PENDING
;
373 link
->rtnl_extended_attrs
= true;
374 link
->ifindex
= ifindex
;
375 link
->iftype
= iftype
;
376 link
->ifname
= strdup(ifname
);
380 r
= sd_netlink_message_read_ether_addr(message
, IFLA_ADDRESS
, &link
->mac
);
382 log_link_debug_errno(link
, r
, "MAC address not found for new device, continuing without");
384 if (asprintf(&link
->state_file
, "/run/systemd/netif/links/%d", link
->ifindex
) < 0)
387 if (asprintf(&link
->lease_file
, "/run/systemd/netif/leases/%d", link
->ifindex
) < 0)
390 if (asprintf(&link
->lldp_file
, "/run/systemd/netif/lldp/%d", link
->ifindex
) < 0)
393 r
= hashmap_ensure_allocated(&manager
->links
, NULL
);
397 r
= hashmap_put(manager
->links
, INT_TO_PTR(link
->ifindex
), link
);
401 r
= link_update_flags(link
, message
);
411 static void link_free(Link
*link
) {
419 while (!set_isempty(link
->addresses
))
420 address_free(set_first(link
->addresses
));
422 while (!set_isempty(link
->addresses_foreign
))
423 address_free(set_first(link
->addresses_foreign
));
425 link
->addresses
= set_free(link
->addresses
);
427 link
->addresses_foreign
= set_free(link
->addresses_foreign
);
429 while ((address
= link
->pool_addresses
)) {
430 LIST_REMOVE(addresses
, link
->pool_addresses
, address
);
431 address_free(address
);
434 sd_dhcp_server_unref(link
->dhcp_server
);
435 sd_dhcp_client_unref(link
->dhcp_client
);
436 sd_dhcp_lease_unref(link
->dhcp_lease
);
438 link_lldp_tx_stop(link
);
440 free(link
->lease_file
);
442 sd_lldp_unref(link
->lldp
);
443 free(link
->lldp_file
);
445 sd_ipv4ll_unref(link
->ipv4ll
);
446 sd_dhcp6_client_unref(link
->dhcp6_client
);
447 sd_ndisc_unref(link
->ndisc_router_discovery
);
450 hashmap_remove(link
->manager
->links
, INT_TO_PTR(link
->ifindex
));
454 (void)unlink(link
->state_file
);
455 free(link
->state_file
);
457 udev_device_unref(link
->udev_device
);
459 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
)
460 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(carrier
->ifindex
));
461 hashmap_free(link
->bound_to_links
);
463 HASHMAP_FOREACH (carrier
, link
->bound_by_links
, i
)
464 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(carrier
->ifindex
));
465 hashmap_free(link
->bound_by_links
);
470 Link
*link_unref(Link
*link
) {
474 assert(link
->n_ref
> 0);
486 Link
*link_ref(Link
*link
) {
490 assert(link
->n_ref
> 0);
497 int link_get(Manager
*m
, int ifindex
, Link
**ret
) {
504 link
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
513 static void link_set_state(Link
*link
, LinkState state
) {
516 if (link
->state
== state
)
521 link_send_changed(link
, "AdministrativeState", NULL
);
526 static void link_enter_unmanaged(Link
*link
) {
529 log_link_debug(link
, "Unmanaged");
531 link_set_state(link
, LINK_STATE_UNMANAGED
);
536 static int link_stop_clients(Link
*link
) {
540 assert(link
->manager
);
541 assert(link
->manager
->event
);
543 if (link
->dhcp_client
) {
544 k
= sd_dhcp_client_stop(link
->dhcp_client
);
546 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv4 client: %m");
550 k
= sd_ipv4ll_stop(link
->ipv4ll
);
552 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 link-local: %m");
555 if (link
->dhcp6_client
) {
556 k
= sd_dhcp6_client_stop(link
->dhcp6_client
);
558 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv6 client: %m");
561 if (link
->ndisc_router_discovery
) {
562 k
= sd_ndisc_stop(link
->ndisc_router_discovery
);
564 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Discovery: %m");
567 link_lldp_tx_stop(link
);
571 void link_enter_failed(Link
*link
) {
574 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
577 log_link_warning(link
, "Failed");
579 link_set_state(link
, LINK_STATE_FAILED
);
581 link_stop_clients(link
);
586 static Address
* link_find_dhcp_server_address(Link
*link
) {
590 assert(link
->network
);
592 /* The first statically configured address if there is any */
593 LIST_FOREACH(addresses
, address
, link
->network
->static_addresses
) {
595 if (address
->family
!= AF_INET
)
598 if (in_addr_is_null(address
->family
, &address
->in_addr
))
604 /* If that didn't work, find a suitable address we got from the pool */
605 LIST_FOREACH(addresses
, address
, link
->pool_addresses
) {
606 if (address
->family
!= AF_INET
)
615 static int link_enter_configured(Link
*link
) {
617 assert(link
->network
);
618 assert(link
->state
== LINK_STATE_SETTING_ROUTES
);
620 log_link_info(link
, "Configured");
622 link_set_state(link
, LINK_STATE_CONFIGURED
);
629 void link_check_ready(Link
*link
) {
638 if (!link
->static_configured
)
641 if (link_ipv4ll_enabled(link
))
642 if (!link
->ipv4ll_address
||
646 if (link_ipv6ll_enabled(link
))
647 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) > 0)
650 if ((link_dhcp4_enabled(link
) && !link_dhcp6_enabled(link
) &&
651 !link
->dhcp4_configured
) ||
652 (link_dhcp6_enabled(link
) && !link_dhcp4_enabled(link
) &&
653 !link
->dhcp6_configured
) ||
654 (link_dhcp4_enabled(link
) && link_dhcp6_enabled(link
) &&
655 !link
->dhcp4_configured
&& !link
->dhcp6_configured
))
658 if (link_ipv6_accept_ra_enabled(link
) && !link
->ndisc_configured
)
661 SET_FOREACH(a
, link
->addresses
, i
)
662 if (!address_is_ready(a
))
665 if (link
->state
!= LINK_STATE_CONFIGURED
)
666 link_enter_configured(link
);
671 static int route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
672 _cleanup_link_unref_ Link
*link
= userdata
;
675 assert(link
->link_messages
> 0);
676 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
677 LINK_STATE_SETTING_ROUTES
, LINK_STATE_FAILED
,
680 link
->link_messages
--;
682 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
685 r
= sd_netlink_message_get_errno(m
);
686 if (r
< 0 && r
!= -EEXIST
)
687 log_link_warning_errno(link
, r
, "Could not set route: %m");
689 if (link
->link_messages
== 0) {
690 log_link_debug(link
, "Routes set");
691 link
->static_configured
= true;
692 link_check_ready(link
);
698 static int link_enter_set_routes(Link
*link
) {
703 assert(link
->network
);
704 assert(link
->state
== LINK_STATE_SETTING_ADDRESSES
);
706 link_set_state(link
, LINK_STATE_SETTING_ROUTES
);
708 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
709 r
= route_configure(rt
, link
, &route_handler
);
711 log_link_warning_errno(link
, r
, "Could not set routes: %m");
712 link_enter_failed(link
);
716 link
->link_messages
++;
719 if (link
->link_messages
== 0) {
720 link
->static_configured
= true;
721 link_check_ready(link
);
723 log_link_debug(link
, "Setting routes");
728 int link_route_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
729 _cleanup_link_unref_ Link
*link
= userdata
;
734 assert(link
->ifname
);
736 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
739 r
= sd_netlink_message_get_errno(m
);
740 if (r
< 0 && r
!= -ESRCH
)
741 log_link_warning_errno(link
, r
, "Could not drop route: %m");
746 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
747 _cleanup_link_unref_ Link
*link
= userdata
;
753 assert(link
->ifname
);
754 assert(link
->link_messages
> 0);
755 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
756 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
758 link
->link_messages
--;
760 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
763 r
= sd_netlink_message_get_errno(m
);
764 if (r
< 0 && r
!= -EEXIST
)
765 log_link_warning_errno(link
, r
, "could not set address: %m");
767 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
769 if (link
->link_messages
== 0) {
770 log_link_debug(link
, "Addresses set");
771 link_enter_set_routes(link
);
777 static int link_push_dns_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
778 _cleanup_free_
struct in_addr
*addresses
= NULL
;
779 size_t n_addresses
= 0, n_allocated
= 0;
782 log_debug("Copying DNS server information from %s", link
->ifname
);
787 STRV_FOREACH(a
, link
->network
->dns
) {
790 /* Only look for IPv4 addresses */
791 if (inet_pton(AF_INET
, *a
, &ia
) <= 0)
794 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
797 addresses
[n_addresses
++] = ia
;
800 if (link
->network
->dhcp_use_dns
&&
802 const struct in_addr
*da
= NULL
;
805 n
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &da
);
808 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
811 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
816 if (n_addresses
<= 0)
819 return sd_dhcp_server_set_dns(s
, addresses
, n_addresses
);
822 static int link_push_ntp_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
823 _cleanup_free_
struct in_addr
*addresses
= NULL
;
824 size_t n_addresses
= 0, n_allocated
= 0;
830 log_debug("Copying NTP server information from %s", link
->ifname
);
832 STRV_FOREACH(a
, link
->network
->ntp
) {
835 /* Only look for IPv4 addresses */
836 if (inet_pton(AF_INET
, *a
, &ia
) <= 0)
839 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
842 addresses
[n_addresses
++] = ia
;
845 if (link
->network
->dhcp_use_ntp
&&
847 const struct in_addr
*da
= NULL
;
850 n
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &da
);
853 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
856 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
861 if (n_addresses
<= 0)
864 return sd_dhcp_server_set_ntp(s
, addresses
, n_addresses
);
867 static int link_enter_set_addresses(Link
*link
) {
872 assert(link
->network
);
873 assert(link
->state
!= _LINK_STATE_INVALID
);
875 link_set_state(link
, LINK_STATE_SETTING_ADDRESSES
);
877 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
878 r
= address_configure(ad
, link
, &address_handler
, false);
880 log_link_warning_errno(link
, r
, "Could not set addresses: %m");
881 link_enter_failed(link
);
885 link
->link_messages
++;
888 /* now that we can figure out a default address for the dhcp server,
890 if (link_dhcp4_server_enabled(link
)) {
893 bool acquired_uplink
= false;
895 address
= link_find_dhcp_server_address(link
);
897 log_link_warning(link
, "Failed to find suitable address for DHCPv4 server instance.");
898 link_enter_failed(link
);
902 /* use the server address' subnet as the pool */
903 r
= sd_dhcp_server_configure_pool(link
->dhcp_server
, &address
->in_addr
.in
, address
->prefixlen
,
904 link
->network
->dhcp_server_pool_offset
, link
->network
->dhcp_server_pool_size
);
909 r = sd_dhcp_server_set_router(link->dhcp_server,
910 &main_address->in_addr.in);
915 if (link
->network
->dhcp_server_max_lease_time_usec
> 0) {
916 r
= sd_dhcp_server_set_max_lease_time(
918 DIV_ROUND_UP(link
->network
->dhcp_server_max_lease_time_usec
, USEC_PER_SEC
));
923 if (link
->network
->dhcp_server_default_lease_time_usec
> 0) {
924 r
= sd_dhcp_server_set_default_lease_time(
926 DIV_ROUND_UP(link
->network
->dhcp_server_default_lease_time_usec
, USEC_PER_SEC
));
931 if (link
->network
->dhcp_server_emit_dns
) {
933 if (link
->network
->n_dhcp_server_dns
> 0)
934 r
= sd_dhcp_server_set_dns(link
->dhcp_server
, link
->network
->dhcp_server_dns
, link
->network
->n_dhcp_server_dns
);
936 uplink
= manager_find_uplink(link
->manager
, link
);
937 acquired_uplink
= true;
940 log_link_debug(link
, "Not emitting DNS server information on link, couldn't find suitable uplink.");
943 r
= link_push_dns_to_dhcp_server(uplink
, link
->dhcp_server
);
946 log_link_warning_errno(link
, r
, "Failed to set DNS server for DHCP server, ignoring: %m");
950 if (link
->network
->dhcp_server_emit_ntp
) {
952 if (link
->network
->n_dhcp_server_ntp
> 0)
953 r
= sd_dhcp_server_set_ntp(link
->dhcp_server
, link
->network
->dhcp_server_ntp
, link
->network
->n_dhcp_server_ntp
);
955 if (!acquired_uplink
)
956 uplink
= manager_find_uplink(link
->manager
, link
);
959 log_link_debug(link
, "Not emitting NTP server information on link, couldn't find suitable uplink.");
962 r
= link_push_ntp_to_dhcp_server(uplink
, link
->dhcp_server
);
966 log_link_warning_errno(link
, r
, "Failed to set NTP server for DHCP server, ignoring: %m");
969 if (link
->network
->dhcp_server_emit_timezone
) {
970 _cleanup_free_
char *buffer
= NULL
;
971 const char *tz
= NULL
;
973 if (link
->network
->dhcp_server_timezone
)
974 tz
= link
->network
->dhcp_server_timezone
;
976 r
= get_timezone(&buffer
);
978 log_warning_errno(r
, "Failed to determine timezone: %m");
984 r
= sd_dhcp_server_set_timezone(link
->dhcp_server
, tz
);
990 r
= sd_dhcp_server_start(link
->dhcp_server
);
992 log_link_warning_errno(link
, r
, "Could not start DHCPv4 server instance: %m");
994 link_enter_failed(link
);
999 log_link_debug(link
, "Offering DHCPv4 leases");
1002 if (link
->link_messages
== 0)
1003 link_enter_set_routes(link
);
1005 log_link_debug(link
, "Setting addresses");
1010 int link_address_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1011 _cleanup_link_unref_ Link
*link
= userdata
;
1016 assert(link
->ifname
);
1018 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1021 r
= sd_netlink_message_get_errno(m
);
1022 if (r
< 0 && r
!= -EADDRNOTAVAIL
)
1023 log_link_warning_errno(link
, r
, "Could not drop address: %m");
1028 static int link_set_bridge_fdb(Link
*const link
) {
1029 FdbEntry
*fdb_entry
;
1032 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
1033 r
= fdb_entry_configure(link
, fdb_entry
);
1035 log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
1043 static int link_set_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1044 _cleanup_link_unref_ Link
*link
= userdata
;
1047 log_link_debug(link
, "Set link");
1049 r
= sd_netlink_message_get_errno(m
);
1050 if (r
< 0 && r
!= -EEXIST
) {
1051 log_link_error_errno(link
, r
, "Could not join netdev: %m");
1052 link_enter_failed(link
);
1059 static int set_hostname_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
1060 _cleanup_link_unref_ Link
*link
= userdata
;
1061 const sd_bus_error
*e
;
1066 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1069 e
= sd_bus_message_get_error(m
);
1071 log_link_warning_errno(link
, sd_bus_error_get_errno(e
), "Could not set hostname: %s", e
->message
);
1076 int link_set_hostname(Link
*link
, const char *hostname
) {
1080 assert(link
->manager
);
1082 log_link_debug(link
, "Setting transient hostname: '%s'", strna(hostname
));
1084 if (!link
->manager
->bus
) {
1085 /* TODO: replace by assert when we can rely on kdbus */
1086 log_link_info(link
, "Not connected to system bus, ignoring transient hostname.");
1090 r
= sd_bus_call_method_async(
1093 "org.freedesktop.hostname1",
1094 "/org/freedesktop/hostname1",
1095 "org.freedesktop.hostname1",
1097 set_hostname_handler
,
1104 return log_link_error_errno(link
, r
, "Could not set transient hostname: %m");
1111 static int set_timezone_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
1112 _cleanup_link_unref_ Link
*link
= userdata
;
1113 const sd_bus_error
*e
;
1118 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1121 e
= sd_bus_message_get_error(m
);
1123 log_link_warning_errno(link
, sd_bus_error_get_errno(e
), "Could not set timezone: %s", e
->message
);
1128 int link_set_timezone(Link
*link
, const char *tz
) {
1132 assert(link
->manager
);
1135 log_link_debug(link
, "Setting system timezone: '%s'", tz
);
1137 if (!link
->manager
->bus
) {
1138 log_link_info(link
, "Not connected to system bus, ignoring timezone.");
1142 r
= sd_bus_call_method_async(
1145 "org.freedesktop.timedate1",
1146 "/org/freedesktop/timedate1",
1147 "org.freedesktop.timedate1",
1149 set_timezone_handler
,
1155 return log_link_error_errno(link
, r
, "Could not set timezone: %m");
1162 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1163 _cleanup_link_unref_ Link
*link
= userdata
;
1168 assert(link
->ifname
);
1170 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1173 r
= sd_netlink_message_get_errno(m
);
1175 log_link_warning_errno(link
, r
, "Could not set MTU: %m");
1180 int link_set_mtu(Link
*link
, uint32_t mtu
) {
1181 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1185 assert(link
->manager
);
1186 assert(link
->manager
->rtnl
);
1188 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1190 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1192 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1194 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1196 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1198 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, set_mtu_handler
, link
, 0, NULL
);
1200 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1207 static int link_set_bridge(Link
*link
) {
1208 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1212 assert(link
->network
);
1214 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1216 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1218 r
= sd_rtnl_message_link_set_family(req
, PF_BRIDGE
);
1220 return log_link_error_errno(link
, r
, "Could not set message family: %m");
1222 r
= sd_netlink_message_open_container(req
, IFLA_PROTINFO
);
1224 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1226 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_GUARD
, !link
->network
->use_bpdu
);
1228 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_GUARD attribute: %m");
1230 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MODE
, link
->network
->hairpin
);
1232 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MODE attribute: %m");
1234 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_FAST_LEAVE
, link
->network
->fast_leave
);
1236 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_FAST_LEAVE attribute: %m");
1238 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_PROTECT
, !link
->network
->allow_port_to_be_root
);
1240 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PROTECT attribute: %m");
1242 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_UNICAST_FLOOD
, link
->network
->unicast_flood
);
1244 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_UNICAST_FLOOD attribute: %m");
1246 if (link
->network
->cost
!= 0) {
1247 r
= sd_netlink_message_append_u32(req
, IFLA_BRPORT_COST
, link
->network
->cost
);
1249 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_COST attribute: %m");
1252 r
= sd_netlink_message_close_container(req
);
1254 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1256 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_set_handler
, link
, 0, NULL
);
1258 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1265 static int link_lldp_save(Link
*link
) {
1266 _cleanup_free_
char *temp_path
= NULL
;
1267 _cleanup_fclose_
FILE *f
= NULL
;
1268 sd_lldp_neighbor
**l
= NULL
;
1272 assert(link
->lldp_file
);
1275 (void) unlink(link
->lldp_file
);
1279 r
= sd_lldp_get_neighbors(link
->lldp
, &l
);
1283 (void) unlink(link
->lldp_file
);
1289 r
= fopen_temporary(link
->lldp_file
, &f
, &temp_path
);
1293 fchmod(fileno(f
), 0644);
1295 for (i
= 0; i
< n
; i
++) {
1300 r
= sd_lldp_neighbor_get_raw(l
[i
], &p
, &sz
);
1305 (void) fwrite(&u
, 1, sizeof(u
), f
);
1306 (void) fwrite(p
, 1, sz
, f
);
1309 r
= fflush_and_check(f
);
1313 if (rename(temp_path
, link
->lldp_file
) < 0) {
1320 (void) unlink(link
->lldp_file
);
1322 (void) unlink(temp_path
);
1324 log_link_error_errno(link
, r
, "Failed to save LLDP data to %s: %m", link
->lldp_file
);
1328 for (i
= 0; i
< n
; i
++)
1329 sd_lldp_neighbor_unref(l
[i
]);
1336 static void lldp_handler(sd_lldp
*lldp
, sd_lldp_event event
, sd_lldp_neighbor
*n
, void *userdata
) {
1337 Link
*link
= userdata
;
1342 (void) link_lldp_save(link
);
1344 if (link_lldp_tx_enabled(link
) && event
== SD_LLDP_EVENT_ADDED
) {
1345 /* If we received information about a new neighbor, restart the LLDP "fast" logic */
1347 log_link_debug(link
, "Received LLDP datagram from previously unknown neighbor, restarting 'fast' LLDP transmission.");
1349 r
= link_lldp_tx_start(link
);
1351 log_link_warning_errno(link
, r
, "Failed to restart LLDP transmission: %m");
1355 static int link_acquire_ipv6_conf(Link
*link
) {
1360 if (link_dhcp6_enabled(link
)) {
1361 assert(link
->dhcp6_client
);
1362 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1364 log_link_debug(link
, "Acquiring DHCPv6 lease");
1366 r
= sd_dhcp6_client_set_local_address(link
->dhcp6_client
, &link
->ipv6ll_address
);
1367 if (r
< 0 && r
!= -EBUSY
)
1368 return log_link_warning_errno(link
, r
, "Could not set IPv6LL address in DHCP client: %m");
1370 r
= sd_dhcp6_client_start(link
->dhcp6_client
);
1371 if (r
< 0 && r
!= -EBUSY
)
1372 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv6 lease: %m");
1375 if (link_ipv6_accept_ra_enabled(link
)) {
1376 assert(link
->ndisc_router_discovery
);
1378 log_link_debug(link
, "Discovering IPv6 routers");
1380 r
= sd_ndisc_router_discovery_start(link
->ndisc_router_discovery
);
1381 if (r
< 0 && r
!= -EBUSY
)
1382 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1388 static int link_acquire_conf(Link
*link
) {
1392 assert(link
->network
);
1393 assert(link
->manager
);
1394 assert(link
->manager
->event
);
1396 if (link_ipv4ll_enabled(link
)) {
1397 assert(link
->ipv4ll
);
1399 log_link_debug(link
, "Acquiring IPv4 link-local address");
1401 r
= sd_ipv4ll_start(link
->ipv4ll
);
1403 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1406 if (link_dhcp4_enabled(link
)) {
1407 assert(link
->dhcp_client
);
1409 log_link_debug(link
, "Acquiring DHCPv4 lease");
1411 r
= sd_dhcp_client_start(link
->dhcp_client
);
1413 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1416 if (link_lldp_tx_enabled(link
)) {
1417 r
= link_lldp_tx_start(link
);
1419 return log_link_warning_errno(link
, r
, "Failed to start LLDP transmission: %m");
1425 bool link_has_carrier(Link
*link
) {
1426 /* see Documentation/networking/operstates.txt in the kernel sources */
1428 if (link
->kernel_operstate
== IF_OPER_UP
)
1431 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1432 /* operstate may not be implemented, so fall back to flags */
1433 if ((link
->flags
& IFF_LOWER_UP
) && !(link
->flags
& IFF_DORMANT
))
1439 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1440 _cleanup_link_unref_ Link
*link
= userdata
;
1445 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1448 r
= sd_netlink_message_get_errno(m
);
1450 /* we warn but don't fail the link, as it may be
1452 log_link_warning_errno(link
, r
, "Could not bring up interface: %m");
1457 static int link_up(Link
*link
) {
1458 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1459 uint8_t ipv6ll_mode
;
1463 assert(link
->network
);
1464 assert(link
->manager
);
1465 assert(link
->manager
->rtnl
);
1467 log_link_debug(link
, "Bringing link up");
1469 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1471 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1473 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1475 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1477 if (link
->network
->mac
) {
1478 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
1480 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
1483 if (link
->network
->mtu
) {
1484 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, link
->network
->mtu
);
1486 return log_link_error_errno(link
, r
, "Could not set MTU: %m");
1489 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1491 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1493 if (socket_ipv6_is_supported()) {
1494 /* if the kernel lacks ipv6 support setting IFF_UP fails if any ipv6 options are passed */
1495 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1497 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1499 ipv6ll_mode
= link_ipv6ll_enabled(link
) ? IN6_ADDR_GEN_MODE_EUI64
: IN6_ADDR_GEN_MODE_NONE
;
1500 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1502 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1504 if (!in_addr_is_null(AF_INET6
, &link
->network
->ipv6_token
)) {
1505 r
= sd_netlink_message_append_in6_addr(req
, IFLA_INET6_TOKEN
, &link
->network
->ipv6_token
.in6
);
1507 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_TOKEN: %m");
1510 r
= sd_netlink_message_close_container(req
);
1512 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1515 r
= sd_netlink_message_close_container(req
);
1517 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1519 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_up_handler
, link
, 0, NULL
);
1521 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1528 static int link_down_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 log_link_warning_errno(link
, r
, "Could not bring down interface: %m");
1544 static int link_down(Link
*link
) {
1545 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1549 assert(link
->manager
);
1550 assert(link
->manager
->rtnl
);
1552 log_link_debug(link
, "Bringing link down");
1554 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
1555 RTM_SETLINK
, link
->ifindex
);
1557 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1559 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
1561 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1563 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_down_handler
, link
, 0, NULL
);
1565 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1572 static int link_handle_bound_to_list(Link
*link
) {
1576 bool required_up
= false;
1577 bool link_is_up
= false;
1581 if (hashmap_isempty(link
->bound_to_links
))
1584 if (link
->flags
& IFF_UP
)
1587 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
1588 if (link_has_carrier(l
)) {
1593 if (!required_up
&& link_is_up
) {
1594 r
= link_down(link
);
1597 } else if (required_up
&& !link_is_up
) {
1606 static int link_handle_bound_by_list(Link
*link
) {
1613 if (hashmap_isempty(link
->bound_by_links
))
1616 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
1617 r
= link_handle_bound_to_list(l
);
1625 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
1631 if (link
== carrier
)
1634 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
1637 r
= hashmap_ensure_allocated(h
, NULL
);
1641 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
1648 static int link_new_bound_by_list(Link
*link
) {
1653 bool list_updated
= false;
1656 assert(link
->manager
);
1660 HASHMAP_FOREACH(carrier
, m
->links
, i
) {
1661 if (!carrier
->network
)
1664 if (strv_isempty(carrier
->network
->bind_carrier
))
1667 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
, 0)) {
1668 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
1672 list_updated
= true;
1679 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
1680 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
1684 link_dirty(carrier
);
1690 static int link_new_bound_to_list(Link
*link
) {
1695 bool list_updated
= false;
1698 assert(link
->manager
);
1703 if (strv_isempty(link
->network
->bind_carrier
))
1708 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
1709 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
, 0)) {
1710 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
1714 list_updated
= true;
1721 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
1722 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
1726 link_dirty(carrier
);
1732 static int link_new_carrier_maps(Link
*link
) {
1735 r
= link_new_bound_by_list(link
);
1739 r
= link_handle_bound_by_list(link
);
1743 r
= link_new_bound_to_list(link
);
1747 r
= link_handle_bound_to_list(link
);
1754 static void link_free_bound_to_list(Link
*link
) {
1758 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
1759 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
1761 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
1762 link_dirty(bound_to
);
1768 static void link_free_bound_by_list(Link
*link
) {
1772 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
1773 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
1775 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
1776 link_dirty(bound_by
);
1777 link_handle_bound_to_list(bound_by
);
1784 static void link_free_carrier_maps(Link
*link
) {
1785 bool list_updated
= false;
1789 if (!hashmap_isempty(link
->bound_to_links
)) {
1790 link_free_bound_to_list(link
);
1791 list_updated
= true;
1794 if (!hashmap_isempty(link
->bound_by_links
)) {
1795 link_free_bound_by_list(link
);
1796 list_updated
= true;
1805 void link_drop(Link
*link
) {
1806 if (!link
|| link
->state
== LINK_STATE_LINGER
)
1809 link_set_state(link
, LINK_STATE_LINGER
);
1811 link_free_carrier_maps(link
);
1813 log_link_debug(link
, "Link removed");
1815 (void)unlink(link
->state_file
);
1821 static int link_joined(Link
*link
) {
1825 assert(link
->network
);
1827 if (!hashmap_isempty(link
->bound_to_links
)) {
1828 r
= link_handle_bound_to_list(link
);
1831 } else if (!(link
->flags
& IFF_UP
)) {
1834 link_enter_failed(link
);
1839 if (link
->network
->bridge
) {
1840 r
= link_set_bridge(link
);
1842 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
1845 return link_enter_set_addresses(link
);
1848 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1849 _cleanup_link_unref_ Link
*link
= userdata
;
1853 assert(link
->network
);
1857 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1860 r
= sd_netlink_message_get_errno(m
);
1861 if (r
< 0 && r
!= -EEXIST
) {
1862 log_link_error_errno(link
, r
, "Could not join netdev: %m");
1863 link_enter_failed(link
);
1866 log_link_debug(link
, "Joined netdev");
1868 if (link
->enslaving
<= 0)
1874 static int link_enter_join_netdev(Link
*link
) {
1880 assert(link
->network
);
1881 assert(link
->state
== LINK_STATE_PENDING
);
1883 link_set_state(link
, LINK_STATE_ENSLAVING
);
1887 if (!link
->network
->bridge
&&
1888 !link
->network
->bond
&&
1889 hashmap_isempty(link
->network
->stacked_netdevs
))
1890 return link_joined(link
);
1892 if (link
->network
->bond
) {
1893 log_struct(LOG_DEBUG
,
1894 LOG_LINK_INTERFACE(link
),
1895 LOG_NETDEV_INTERFACE(link
->network
->bond
),
1896 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
),
1899 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
1901 log_struct_errno(LOG_WARNING
, r
,
1902 LOG_LINK_INTERFACE(link
),
1903 LOG_NETDEV_INTERFACE(link
->network
->bond
),
1904 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
),
1907 link_enter_failed(link
);
1914 if (link
->network
->bridge
) {
1915 log_struct(LOG_DEBUG
,
1916 LOG_LINK_INTERFACE(link
),
1917 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
1918 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
),
1921 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
1923 log_struct_errno(LOG_WARNING
, r
,
1924 LOG_LINK_INTERFACE(link
),
1925 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
1926 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
),
1928 link_enter_failed(link
);
1935 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
1937 log_struct(LOG_DEBUG
,
1938 LOG_LINK_INTERFACE(link
),
1939 LOG_NETDEV_INTERFACE(netdev
),
1940 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
),
1943 r
= netdev_join(netdev
, link
, netdev_join_handler
);
1945 log_struct_errno(LOG_WARNING
, r
,
1946 LOG_LINK_INTERFACE(link
),
1947 LOG_NETDEV_INTERFACE(netdev
),
1948 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
),
1950 link_enter_failed(link
);
1960 static int link_set_ipv4_forward(Link
*link
) {
1963 if (!link_ipv4_forward_enabled(link
))
1966 /* We propagate the forwarding flag from one interface to the
1967 * global setting one way. This means: as long as at least one
1968 * interface was configured at any time that had IP forwarding
1969 * enabled the setting will stay on for good. We do this
1970 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
1971 * somewhat in sync (see below). */
1973 r
= write_string_file("/proc/sys/net/ipv4/ip_forward", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
1975 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
1980 static int link_set_ipv6_forward(Link
*link
) {
1983 if (!link_ipv6_forward_enabled(link
))
1986 /* On Linux, the IPv6 stack does not not know a per-interface
1987 * packet forwarding setting: either packet forwarding is on
1988 * for all, or off for all. We hence don't bother with a
1989 * per-interface setting, but simply propagate the interface
1990 * flag, if it is set, to the global flag, one-way. Note that
1991 * while IPv4 would allow a per-interface flag, we expose the
1992 * same behaviour there and also propagate the setting from
1993 * one to all, to keep things simple (see above). */
1995 r
= write_string_file("/proc/sys/net/ipv6/conf/all/forwarding", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
1997 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
2002 static int link_set_ipv6_privacy_extensions(Link
*link
) {
2003 char buf
[DECIMAL_STR_MAX(unsigned) + 1];
2004 IPv6PrivacyExtensions s
;
2005 const char *p
= NULL
;
2008 s
= link_ipv6_privacy_extensions(link
);
2012 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/use_tempaddr");
2013 xsprintf(buf
, "%u", (unsigned) link
->network
->ipv6_privacy_extensions
);
2015 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2017 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
2022 static int link_set_ipv6_accept_ra(Link
*link
) {
2023 const char *p
= NULL
;
2026 /* Make this a NOP if IPv6 is not available */
2027 if (!socket_ipv6_is_supported())
2030 if (link
->flags
& IFF_LOOPBACK
)
2036 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/accept_ra");
2038 /* We handle router advertisments ourselves, tell the kernel to GTFO */
2039 r
= write_string_file(p
, "0", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2041 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
2046 static int link_set_ipv6_dad_transmits(Link
*link
) {
2047 char buf
[DECIMAL_STR_MAX(int) + 1];
2048 const char *p
= NULL
;
2051 /* Make this a NOP if IPv6 is not available */
2052 if (!socket_ipv6_is_supported())
2055 if (link
->flags
& IFF_LOOPBACK
)
2061 if (link
->network
->ipv6_dad_transmits
< 0)
2064 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/dad_transmits");
2065 xsprintf(buf
, "%i", link
->network
->ipv6_dad_transmits
);
2067 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2069 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
2074 static int link_set_ipv6_hop_limit(Link
*link
) {
2075 char buf
[DECIMAL_STR_MAX(int) + 1];
2076 const char *p
= NULL
;
2079 /* Make this a NOP if IPv6 is not available */
2080 if (!socket_ipv6_is_supported())
2083 if (link
->flags
& IFF_LOOPBACK
)
2089 if (link
->network
->ipv6_hop_limit
< 0)
2092 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/hop_limit");
2093 xsprintf(buf
, "%i", link
->network
->ipv6_hop_limit
);
2095 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2097 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2102 static int link_drop_foreign_config(Link
*link
) {
2108 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
2109 /* we consider IPv6LL addresses to be managed by the kernel */
2110 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2113 r
= address_remove(address
, link
, link_address_remove_handler
);
2118 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2119 /* do not touch routes managed by the kernel */
2120 if (route
->protocol
== RTPROT_KERNEL
)
2123 r
= route_remove(route
, link
, link_address_remove_handler
);
2131 static int link_update_lldp(Link
*link
) {
2139 if (link
->flags
& IFF_UP
) {
2140 r
= sd_lldp_start(link
->lldp
);
2142 log_link_debug(link
, "Started LLDP.");
2144 r
= sd_lldp_stop(link
->lldp
);
2146 log_link_debug(link
, "Stopped LLDP.");
2152 static int link_configure(Link
*link
) {
2156 assert(link
->network
);
2157 assert(link
->state
== LINK_STATE_PENDING
);
2159 /* Drop foreign config, but ignore loopback or critical devices.
2160 * We do not want to remove loopback address or addresses used for root NFS. */
2161 if (!(link
->flags
& IFF_LOOPBACK
) && !(link
->network
->dhcp_critical
)) {
2162 r
= link_drop_foreign_config(link
);
2167 r
= link_set_bridge_fdb(link
);
2171 r
= link_set_ipv4_forward(link
);
2175 r
= link_set_ipv6_forward(link
);
2179 r
= link_set_ipv6_privacy_extensions(link
);
2183 r
= link_set_ipv6_accept_ra(link
);
2187 r
= link_set_ipv6_dad_transmits(link
);
2191 r
= link_set_ipv6_hop_limit(link
);
2195 if (link_ipv4ll_enabled(link
)) {
2196 r
= ipv4ll_configure(link
);
2201 if (link_dhcp4_enabled(link
)) {
2202 r
= dhcp4_configure(link
);
2207 if (link_dhcp4_server_enabled(link
)) {
2208 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
2212 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
2217 if (link_dhcp6_enabled(link
) ||
2218 link_ipv6_accept_ra_enabled(link
)) {
2219 r
= dhcp6_configure(link
);
2224 if (link_ipv6_accept_ra_enabled(link
)) {
2225 r
= ndisc_configure(link
);
2230 if (link_lldp_rx_enabled(link
)) {
2231 r
= sd_lldp_new(&link
->lldp
, link
->ifindex
);
2235 r
= sd_lldp_match_capabilities(link
->lldp
,
2236 link
->network
->lldp_mode
== LLDP_MODE_ROUTERS_ONLY
?
2237 _LLDP_SYSTEM_CAPABILITIES_ALL_ROUTERS
:
2238 _LLDP_SYSTEM_CAPABILITIES_ALL
);
2242 r
= sd_lldp_set_filter_address(link
->lldp
, &link
->mac
);
2246 r
= sd_lldp_attach_event(link
->lldp
, NULL
, 0);
2250 r
= sd_lldp_set_callback(link
->lldp
, lldp_handler
, link
);
2254 r
= link_update_lldp(link
);
2259 if (link_has_carrier(link
)) {
2260 r
= link_acquire_conf(link
);
2264 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) == 0) {
2265 r
= link_acquire_ipv6_conf(link
);
2271 return link_enter_join_netdev(link
);
2274 static int link_initialized_and_synced(sd_netlink
*rtnl
, sd_netlink_message
*m
,
2276 _cleanup_link_unref_ Link
*link
= userdata
;
2281 assert(link
->ifname
);
2282 assert(link
->manager
);
2284 if (link
->state
!= LINK_STATE_PENDING
)
2287 log_link_debug(link
, "Link state is up-to-date");
2289 r
= link_new_bound_by_list(link
);
2293 r
= link_handle_bound_by_list(link
);
2297 if (!link
->network
) {
2298 r
= network_get(link
->manager
, link
->udev_device
, link
->ifname
,
2299 &link
->mac
, &network
);
2301 link_enter_unmanaged(link
);
2306 if (link
->flags
& IFF_LOOPBACK
) {
2307 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
2308 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
2310 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
2311 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
2313 if (network
->dhcp_server
)
2314 log_link_debug(link
, "Ignoring DHCP server for loopback link");
2317 r
= network_apply(link
->manager
, network
, link
);
2322 r
= link_new_bound_to_list(link
);
2326 r
= link_configure(link
);
2333 int link_initialized(Link
*link
, struct udev_device
*device
) {
2334 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2338 assert(link
->manager
);
2339 assert(link
->manager
->rtnl
);
2342 if (link
->state
!= LINK_STATE_PENDING
)
2345 if (link
->udev_device
)
2348 log_link_debug(link
, "udev initialized link");
2350 link
->udev_device
= udev_device_ref(device
);
2352 /* udev has initialized the link, but we don't know if we have yet
2353 * processed the NEWLINK messages with the latest state. Do a GETLINK,
2354 * when it returns we know that the pending NEWLINKs have already been
2355 * processed and that we are up-to-date */
2357 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
2362 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
,
2363 link_initialized_and_synced
, link
, 0, NULL
);
2372 static int link_load(Link
*link
) {
2373 _cleanup_free_
char *network_file
= NULL
,
2376 *dhcp4_address
= NULL
,
2377 *ipv4ll_address
= NULL
;
2378 union in_addr_union address
;
2379 union in_addr_union route_dst
;
2385 r
= parse_env_file(link
->state_file
, NEWLINE
,
2386 "NETWORK_FILE", &network_file
,
2387 "ADDRESSES", &addresses
,
2389 "DHCP4_ADDRESS", &dhcp4_address
,
2390 "IPV4LL_ADDRESS", &ipv4ll_address
,
2392 if (r
< 0 && r
!= -ENOENT
)
2393 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
2400 suffix
= strrchr(network_file
, '.');
2402 log_link_debug(link
, "Failed to get network name from %s", network_file
);
2403 goto network_file_fail
;
2407 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
2409 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
2410 goto network_file_fail
;
2413 r
= network_apply(link
->manager
, network
, link
);
2415 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
2424 _cleanup_free_
char *address_str
= NULL
;
2425 char *prefixlen_str
;
2427 unsigned char prefixlen
;
2429 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
2431 log_link_debug_errno(link
, r
, "Failed to extract next address string: %m");
2437 prefixlen_str
= strchr(address_str
, '/');
2438 if (!prefixlen_str
) {
2439 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
2443 *prefixlen_str
++ = '\0';
2445 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
2447 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
2451 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
2453 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
2457 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
2459 return log_link_error_errno(link
, r
, "Failed to add address: %m");
2468 _cleanup_free_
char *route_str
= NULL
;
2469 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
2471 char *prefixlen_str
;
2473 unsigned char prefixlen
, tos
, table
;
2476 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
2478 log_link_debug_errno(link
, r
, "Failed to extract next route string: %m");
2484 prefixlen_str
= strchr(route_str
, '/');
2485 if (!prefixlen_str
) {
2486 log_link_debug(link
, "Failed to parse route %s", route_str
);
2490 *prefixlen_str
++ = '\0';
2492 r
= sscanf(prefixlen_str
, "%hhu/%hhu/%"SCNu32
"/%hhu/"USEC_FMT
, &prefixlen
, &tos
, &priority
, &table
, &lifetime
);
2494 log_link_debug(link
,
2495 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
2500 r
= in_addr_from_string_auto(route_str
, &family
, &route_dst
);
2502 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
2506 r
= route_add(link
, family
, &route_dst
, prefixlen
, tos
, priority
, table
, &route
);
2508 return log_link_error_errno(link
, r
, "Failed to add route: %m");
2510 if (lifetime
!= USEC_INFINITY
) {
2511 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(), lifetime
,
2512 0, route_expire_handler
, route
);
2514 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
2517 route
->lifetime
= lifetime
;
2518 sd_event_source_unref(route
->expire
);
2519 route
->expire
= expire
;
2524 if (dhcp4_address
) {
2525 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
2527 log_link_debug_errno(link
, r
, "Falied to parse DHCPv4 address %s: %m", dhcp4_address
);
2528 goto dhcp4_address_fail
;
2531 r
= sd_dhcp_client_new(&link
->dhcp_client
);
2533 return log_link_error_errno(link
, r
, "Falied to create DHCPv4 client: %m");
2535 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
2537 return log_link_error_errno(link
, r
, "Falied to set inital DHCPv4 address %s: %m", dhcp4_address
);
2542 if (ipv4ll_address
) {
2543 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
2545 log_link_debug_errno(link
, r
, "Falied to parse IPv4LL address %s: %m", ipv4ll_address
);
2546 goto ipv4ll_address_fail
;
2549 r
= sd_ipv4ll_new(&link
->ipv4ll
);
2551 return log_link_error_errno(link
, r
, "Falied to create IPv4LL client: %m");
2553 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
2555 return log_link_error_errno(link
, r
, "Falied to set inital IPv4LL address %s: %m", ipv4ll_address
);
2558 ipv4ll_address_fail
:
2563 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
2565 _cleanup_udev_device_unref_
struct udev_device
*device
= NULL
;
2566 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
2574 r
= link_new(m
, message
, ret
);
2580 log_link_debug(link
, "Link %d added", link
->ifindex
);
2582 r
= link_load(link
);
2586 if (detect_container() <= 0) {
2587 /* not in a container, udev will be around */
2588 sprintf(ifindex_str
, "n%d", link
->ifindex
);
2589 device
= udev_device_new_from_device_id(m
->udev
, ifindex_str
);
2591 r
= log_link_warning_errno(link
, errno
, "Could not find udev device: %m");
2595 if (udev_device_get_is_initialized(device
) <= 0) {
2597 log_link_debug(link
, "link pending udev initialization...");
2601 r
= link_initialized(link
, device
);
2605 /* we are calling a callback directly, so must take a ref */
2608 r
= link_initialized_and_synced(m
->rtnl
, NULL
, link
);
2615 link_enter_failed(link
);
2619 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
2624 log_link_info(link
, "Gained IPv6LL");
2626 link
->ipv6ll_address
= *address
;
2627 link_check_ready(link
);
2629 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
2630 r
= link_acquire_ipv6_conf(link
);
2632 link_enter_failed(link
);
2640 static int link_carrier_gained(Link
*link
) {
2645 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
2646 r
= link_acquire_conf(link
);
2648 link_enter_failed(link
);
2653 r
= link_handle_bound_by_list(link
);
2660 static int link_carrier_lost(Link
*link
) {
2665 r
= link_stop_clients(link
);
2667 link_enter_failed(link
);
2671 r
= link_handle_bound_by_list(link
);
2678 int link_carrier_reset(Link
*link
) {
2683 if (link_has_carrier(link
)) {
2684 r
= link_carrier_lost(link
);
2688 r
= link_carrier_gained(link
);
2692 log_link_info(link
, "Reset carrier");
2698 int link_update(Link
*link
, sd_netlink_message
*m
) {
2699 struct ether_addr mac
;
2702 bool had_carrier
, carrier_gained
, carrier_lost
;
2706 assert(link
->ifname
);
2709 if (link
->state
== LINK_STATE_LINGER
) {
2711 log_link_info(link
, "Link readded");
2712 link_set_state(link
, LINK_STATE_ENSLAVING
);
2714 r
= link_new_carrier_maps(link
);
2719 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
2720 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
2721 log_link_info(link
, "Renamed to %s", ifname
);
2723 link_free_carrier_maps(link
);
2725 r
= free_and_strdup(&link
->ifname
, ifname
);
2729 r
= link_new_carrier_maps(link
);
2734 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
2735 if (r
>= 0 && mtu
> 0) {
2737 if (!link
->original_mtu
) {
2738 link
->original_mtu
= mtu
;
2739 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
2742 if (link
->dhcp_client
) {
2743 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
2746 log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
2752 /* The kernel may broadcast NEWLINK messages without the MAC address
2753 set, simply ignore them. */
2754 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
2756 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
2759 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
2762 log_link_debug(link
, "MAC address: "
2763 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
2764 mac
.ether_addr_octet
[0],
2765 mac
.ether_addr_octet
[1],
2766 mac
.ether_addr_octet
[2],
2767 mac
.ether_addr_octet
[3],
2768 mac
.ether_addr_octet
[4],
2769 mac
.ether_addr_octet
[5]);
2772 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
2774 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
2777 if (link
->dhcp_client
) {
2778 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
2779 (const uint8_t *) &link
->mac
,
2783 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
2786 if (link
->dhcp6_client
) {
2787 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
2788 (const uint8_t *) &link
->mac
,
2792 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
2797 had_carrier
= link_has_carrier(link
);
2799 r
= link_update_flags(link
, m
);
2803 r
= link_update_lldp(link
);
2807 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
2808 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
2810 if (carrier_gained
) {
2811 log_link_info(link
, "Gained carrier");
2813 r
= link_carrier_gained(link
);
2816 } else if (carrier_lost
) {
2817 log_link_info(link
, "Lost carrier");
2819 r
= link_carrier_lost(link
);
2827 static void print_link_hashmap(FILE *f
, const char *prefix
, Hashmap
* h
) {
2835 if (hashmap_isempty(h
))
2839 HASHMAP_FOREACH(link
, h
, i
) {
2843 fprintf(f
, "%i", link
->ifindex
);
2850 int link_save(Link
*link
) {
2851 _cleanup_free_
char *temp_path
= NULL
;
2852 _cleanup_fclose_
FILE *f
= NULL
;
2853 const char *admin_state
, *oper_state
;
2860 assert(link
->state_file
);
2861 assert(link
->lease_file
);
2862 assert(link
->manager
);
2864 if (link
->state
== LINK_STATE_LINGER
) {
2865 unlink(link
->state_file
);
2869 link_lldp_save(link
);
2871 admin_state
= link_state_to_string(link
->state
);
2872 assert(admin_state
);
2874 oper_state
= link_operstate_to_string(link
->operstate
);
2877 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
2881 fchmod(fileno(f
), 0644);
2884 "# This is private data. Do not parse.\n"
2887 admin_state
, oper_state
);
2889 if (link
->network
) {
2891 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
2892 const char *dhcp_domainname
= NULL
;
2893 char **dhcp6_domains
= NULL
;
2895 if (link
->dhcp6_client
) {
2896 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
, &dhcp6_lease
);
2897 if (r
< 0 && r
!= -ENOMSG
)
2898 log_link_debug(link
, "No DHCPv6 lease");
2901 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
2905 fputstrv(f
, link
->network
->dns
, NULL
, &space
);
2907 if (link
->network
->dhcp_use_dns
&&
2909 const struct in_addr
*addresses
;
2911 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
2915 serialize_in_addrs(f
, addresses
, r
);
2920 if (link
->network
->dhcp_use_dns
&& dhcp6_lease
) {
2921 struct in6_addr
*in6_addrs
;
2923 r
= sd_dhcp6_lease_get_dns(dhcp6_lease
, &in6_addrs
);
2927 serialize_in6_addrs(f
, in6_addrs
, r
);
2935 fputstrv(f
, link
->network
->ntp
, NULL
, &space
);
2937 if (link
->network
->dhcp_use_ntp
&&
2939 const struct in_addr
*addresses
;
2941 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
2945 serialize_in_addrs(f
, addresses
, r
);
2950 if (link
->network
->dhcp_use_ntp
&& dhcp6_lease
) {
2951 struct in6_addr
*in6_addrs
;
2954 r
= sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease
,
2959 serialize_in6_addrs(f
, in6_addrs
, r
);
2963 r
= sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease
, &hosts
);
2965 fputstrv(f
, hosts
, NULL
, &space
);
2970 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
2971 if (link
->dhcp_lease
)
2972 (void) sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &dhcp_domainname
);
2975 (void) sd_dhcp6_lease_get_domains(dhcp6_lease
, &dhcp6_domains
);
2978 fputs("DOMAINS=", f
);
2979 fputstrv(f
, link
->network
->search_domains
, NULL
, &space
);
2981 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
&& dhcp_domainname
)
2982 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
2984 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
&& dhcp6_domains
)
2985 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
2989 fputs("ROUTE_DOMAINS=", f
);
2990 fputstrv(f
, link
->network
->route_domains
, NULL
, NULL
);
2992 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
&& dhcp_domainname
)
2993 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
2995 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
&& dhcp6_domains
)
2996 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3000 fprintf(f
, "LLMNR=%s\n",
3001 resolve_support_to_string(link
->network
->llmnr
));
3002 fprintf(f
, "MDNS=%s\n",
3003 resolve_support_to_string(link
->network
->mdns
));
3005 if (link
->network
->dnssec_mode
!= _DNSSEC_MODE_INVALID
)
3006 fprintf(f
, "DNSSEC=%s\n",
3007 dnssec_mode_to_string(link
->network
->dnssec_mode
));
3009 if (!set_isempty(link
->network
->dnssec_negative_trust_anchors
)) {
3012 fputs("DNSSEC_NTA=", f
);
3014 SET_FOREACH(n
, link
->network
->dnssec_negative_trust_anchors
, i
)
3015 fputs_with_space(f
, n
, NULL
, &space
);
3019 fputs("ADDRESSES=", f
);
3021 SET_FOREACH(a
, link
->addresses
, i
) {
3022 _cleanup_free_
char *address_str
= NULL
;
3024 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
3028 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
3033 fputs("ROUTES=", f
);
3035 SET_FOREACH(route
, link
->routes
, i
) {
3036 _cleanup_free_
char *route_str
= NULL
;
3038 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
3042 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%hhu/"USEC_FMT
, space
? " " : "", route_str
,
3043 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
3050 print_link_hashmap(f
, "CARRIER_BOUND_TO=", link
->bound_to_links
);
3051 print_link_hashmap(f
, "CARRIER_BOUND_BY=", link
->bound_by_links
);
3053 if (link
->dhcp_lease
) {
3054 struct in_addr address
;
3055 const char *tz
= NULL
;
3057 assert(link
->network
);
3059 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
3061 fprintf(f
, "TIMEZONE=%s\n", tz
);
3063 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
3065 fputs("DHCP4_ADDRESS=", f
);
3066 serialize_in_addrs(f
, &address
, 1);
3070 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
3078 unlink(link
->lease_file
);
3081 struct in_addr address
;
3083 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
3085 fputs("IPV4LL_ADDRESS=", f
);
3086 serialize_in_addrs(f
, &address
, 1);
3091 r
= fflush_and_check(f
);
3095 if (rename(temp_path
, link
->state_file
) < 0) {
3103 (void) unlink(link
->state_file
);
3105 (void) unlink(temp_path
);
3107 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
3110 /* The serialized state in /run is no longer up-to-date. */
3111 void link_dirty(Link
*link
) {
3116 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
3118 /* allocation errors are ignored */
3121 r
= set_put(link
->manager
->dirty_links
, link
);
3123 /* allocation errors are ignored */
3129 /* The serialized state in /run is up-to-date */
3130 void link_clean(Link
*link
) {
3132 assert(link
->manager
);
3134 set_remove(link
->manager
->dirty_links
, link
);
3138 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
3139 [LINK_STATE_PENDING
] = "pending",
3140 [LINK_STATE_ENSLAVING
] = "configuring",
3141 [LINK_STATE_SETTING_ADDRESSES
] = "configuring",
3142 [LINK_STATE_SETTING_ROUTES
] = "configuring",
3143 [LINK_STATE_CONFIGURED
] = "configured",
3144 [LINK_STATE_UNMANAGED
] = "unmanaged",
3145 [LINK_STATE_FAILED
] = "failed",
3146 [LINK_STATE_LINGER
] = "linger",
3149 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);
3151 static const char* const link_operstate_table
[_LINK_OPERSTATE_MAX
] = {
3152 [LINK_OPERSTATE_OFF
] = "off",
3153 [LINK_OPERSTATE_NO_CARRIER
] = "no-carrier",
3154 [LINK_OPERSTATE_DORMANT
] = "dormant",
3155 [LINK_OPERSTATE_CARRIER
] = "carrier",
3156 [LINK_OPERSTATE_DEGRADED
] = "degraded",
3157 [LINK_OPERSTATE_ROUTABLE
] = "routable",
3160 DEFINE_STRING_TABLE_LOOKUP(link_operstate
, LinkOperationalState
);