1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2013 Tom Gundersen <teg@jklm.no>
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <netinet/ether.h>
26 #include "alloc-util.h"
28 #include "dhcp-lease-internal.h"
29 #include "event-util.h"
32 #include "netlink-util.h"
33 #include "network-internal.h"
34 #include "networkd-link.h"
35 #include "networkd-netdev.h"
37 #include "socket-util.h"
38 #include "stdio-util.h"
39 #include "string-table.h"
40 #include "udev-util.h"
44 bool link_dhcp6_enabled(Link
*link
) {
45 if (link
->flags
& IFF_LOOPBACK
)
51 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV6
;
54 bool link_dhcp4_enabled(Link
*link
) {
55 if (link
->flags
& IFF_LOOPBACK
)
61 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV4
;
64 bool link_dhcp4_server_enabled(Link
*link
) {
65 if (link
->flags
& IFF_LOOPBACK
)
71 return link
->network
->dhcp_server
;
74 bool link_ipv4ll_enabled(Link
*link
) {
75 if (link
->flags
& IFF_LOOPBACK
)
81 return link
->network
->link_local
& ADDRESS_FAMILY_IPV4
;
84 bool link_ipv6ll_enabled(Link
*link
) {
85 if (link
->flags
& IFF_LOOPBACK
)
91 return link
->network
->link_local
& ADDRESS_FAMILY_IPV6
;
94 bool link_lldp_enabled(Link
*link
) {
95 if (link
->flags
& IFF_LOOPBACK
)
101 if (link
->network
->bridge
)
104 return link
->network
->lldp
;
107 static bool link_ipv4_forward_enabled(Link
*link
) {
108 if (link
->flags
& IFF_LOOPBACK
)
114 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
117 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV4
;
120 static bool link_ipv6_forward_enabled(Link
*link
) {
122 if (!socket_ipv6_is_supported())
125 if (link
->flags
& IFF_LOOPBACK
)
131 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
134 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV6
;
137 bool link_ipv6_accept_ra_enabled(Link
*link
) {
138 if (link
->flags
& IFF_LOOPBACK
)
144 /* If unset use system default (enabled if local forwarding is disabled.
145 * disabled if local forwarding is enabled).
146 * If set, ignore or enforce RA independent of local forwarding state.
148 if (link
->network
->ipv6_accept_ra
< 0)
149 /* default to accept RA if ip_forward is disabled and ignore RA if ip_forward is enabled */
150 return !link_ipv6_forward_enabled(link
);
151 else if (link
->network
->ipv6_accept_ra
> 0)
152 /* accept RA even if ip_forward is enabled */
159 static IPv6PrivacyExtensions
link_ipv6_privacy_extensions(Link
*link
) {
161 if (!socket_ipv6_is_supported())
162 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
164 if (link
->flags
& IFF_LOOPBACK
)
165 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
168 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
170 return link
->network
->ipv6_privacy_extensions
;
173 void link_update_operstate(Link
*link
) {
174 LinkOperationalState operstate
;
177 if (link
->kernel_operstate
== IF_OPER_DORMANT
)
178 operstate
= LINK_OPERSTATE_DORMANT
;
179 else if (link_has_carrier(link
)) {
181 uint8_t scope
= RT_SCOPE_NOWHERE
;
184 /* if we have carrier, check what addresses we have */
185 SET_FOREACH(address
, link
->addresses
, i
) {
186 if (!address_is_ready(address
))
189 if (address
->scope
< scope
)
190 scope
= address
->scope
;
193 /* for operstate we also take foreign addresses into account */
194 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
195 if (!address_is_ready(address
))
198 if (address
->scope
< scope
)
199 scope
= address
->scope
;
202 if (scope
< RT_SCOPE_SITE
)
203 /* universally accessible addresses found */
204 operstate
= LINK_OPERSTATE_ROUTABLE
;
205 else if (scope
< RT_SCOPE_HOST
)
206 /* only link or site local addresses found */
207 operstate
= LINK_OPERSTATE_DEGRADED
;
209 /* no useful addresses found */
210 operstate
= LINK_OPERSTATE_CARRIER
;
211 } else if (link
->flags
& IFF_UP
)
212 operstate
= LINK_OPERSTATE_NO_CARRIER
;
214 operstate
= LINK_OPERSTATE_OFF
;
216 if (link
->operstate
!= operstate
) {
217 link
->operstate
= operstate
;
218 link_send_changed(link
, "OperationalState", NULL
);
220 manager_dirty(link
->manager
);
224 #define FLAG_STRING(string, flag, old, new) \
225 (((old ^ new) & flag) \
226 ? ((old & flag) ? (" -" string) : (" +" string)) \
229 static int link_update_flags(Link
*link
, sd_netlink_message
*m
) {
230 unsigned flags
, unknown_flags_added
, unknown_flags_removed
, unknown_flags
;
236 r
= sd_rtnl_message_link_get_flags(m
, &flags
);
238 return log_link_warning_errno(link
, r
, "Could not get link flags: %m");
240 r
= sd_netlink_message_read_u8(m
, IFLA_OPERSTATE
, &operstate
);
242 /* if we got a message without operstate, take it to mean
243 the state was unchanged */
244 operstate
= link
->kernel_operstate
;
246 if ((link
->flags
== flags
) && (link
->kernel_operstate
== operstate
))
249 if (link
->flags
!= flags
) {
250 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",
251 FLAG_STRING("LOOPBACK", IFF_LOOPBACK
, link
->flags
, flags
),
252 FLAG_STRING("MASTER", IFF_MASTER
, link
->flags
, flags
),
253 FLAG_STRING("SLAVE", IFF_SLAVE
, link
->flags
, flags
),
254 FLAG_STRING("UP", IFF_UP
, link
->flags
, flags
),
255 FLAG_STRING("DORMANT", IFF_DORMANT
, link
->flags
, flags
),
256 FLAG_STRING("LOWER_UP", IFF_LOWER_UP
, link
->flags
, flags
),
257 FLAG_STRING("RUNNING", IFF_RUNNING
, link
->flags
, flags
),
258 FLAG_STRING("MULTICAST", IFF_MULTICAST
, link
->flags
, flags
),
259 FLAG_STRING("BROADCAST", IFF_BROADCAST
, link
->flags
, flags
),
260 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT
, link
->flags
, flags
),
261 FLAG_STRING("PROMISC", IFF_PROMISC
, link
->flags
, flags
),
262 FLAG_STRING("ALLMULTI", IFF_ALLMULTI
, link
->flags
, flags
),
263 FLAG_STRING("PORTSEL", IFF_PORTSEL
, link
->flags
, flags
),
264 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA
, link
->flags
, flags
),
265 FLAG_STRING("DYNAMIC", IFF_DYNAMIC
, link
->flags
, flags
),
266 FLAG_STRING("NOARP", IFF_NOARP
, link
->flags
, flags
),
267 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS
, link
->flags
, flags
),
268 FLAG_STRING("DEBUG", IFF_DEBUG
, link
->flags
, flags
),
269 FLAG_STRING("ECHO", IFF_ECHO
, link
->flags
, flags
));
271 unknown_flags
= ~(IFF_LOOPBACK
| IFF_MASTER
| IFF_SLAVE
| IFF_UP
|
272 IFF_DORMANT
| IFF_LOWER_UP
| IFF_RUNNING
|
273 IFF_MULTICAST
| IFF_BROADCAST
| IFF_POINTOPOINT
|
274 IFF_PROMISC
| IFF_ALLMULTI
| IFF_PORTSEL
|
275 IFF_AUTOMEDIA
| IFF_DYNAMIC
| IFF_NOARP
|
276 IFF_NOTRAILERS
| IFF_DEBUG
| IFF_ECHO
);
277 unknown_flags_added
= ((link
->flags
^ flags
) & flags
& unknown_flags
);
278 unknown_flags_removed
= ((link
->flags
^ flags
) & link
->flags
& unknown_flags
);
280 /* link flags are currently at most 18 bits, let's align to
282 if (unknown_flags_added
)
284 "Unknown link flags gained: %#.5x (ignoring)",
285 unknown_flags_added
);
287 if (unknown_flags_removed
)
289 "Unknown link flags lost: %#.5x (ignoring)",
290 unknown_flags_removed
);
294 link
->kernel_operstate
= operstate
;
296 link_update_operstate(link
);
301 static int link_new(Manager
*manager
, sd_netlink_message
*message
, Link
**ret
) {
302 _cleanup_link_unref_ Link
*link
= NULL
;
311 r
= sd_netlink_message_get_type(message
, &type
);
314 else if (type
!= RTM_NEWLINK
)
317 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
320 else if (ifindex
<= 0)
323 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &ifname
);
327 link
= new0(Link
, 1);
332 link
->manager
= manager
;
333 link
->state
= LINK_STATE_PENDING
;
334 link
->rtnl_extended_attrs
= true;
335 link
->ifindex
= ifindex
;
336 link
->ifname
= strdup(ifname
);
340 r
= sd_netlink_message_read_ether_addr(message
, IFLA_ADDRESS
, &link
->mac
);
342 log_link_debug(link
, "MAC address not found for new device, continuing without");
344 r
= asprintf(&link
->state_file
, "/run/systemd/netif/links/%d",
349 r
= asprintf(&link
->lease_file
, "/run/systemd/netif/leases/%d",
354 r
= asprintf(&link
->lldp_file
, "/run/systemd/netif/lldp/%d",
360 r
= hashmap_ensure_allocated(&manager
->links
, NULL
);
364 r
= hashmap_put(manager
->links
, INT_TO_PTR(link
->ifindex
), link
);
368 r
= link_update_flags(link
, message
);
378 static void link_free(Link
*link
) {
386 while (!set_isempty(link
->addresses
))
387 address_free(set_first(link
->addresses
));
389 while (!set_isempty(link
->addresses_foreign
))
390 address_free(set_first(link
->addresses_foreign
));
392 link
->addresses
= set_free(link
->addresses
);
394 link
->addresses_foreign
= set_free(link
->addresses_foreign
);
396 while ((address
= link
->pool_addresses
)) {
397 LIST_REMOVE(addresses
, link
->pool_addresses
, address
);
398 address_free(address
);
401 sd_dhcp_server_unref(link
->dhcp_server
);
402 sd_dhcp_client_unref(link
->dhcp_client
);
403 sd_dhcp_lease_unref(link
->dhcp_lease
);
405 free(link
->lease_file
);
407 sd_lldp_free(link
->lldp
);
409 free(link
->lldp_file
);
411 sd_ipv4ll_unref(link
->ipv4ll
);
412 sd_dhcp6_client_unref(link
->dhcp6_client
);
413 sd_ndisc_unref(link
->ndisc_router_discovery
);
416 hashmap_remove(link
->manager
->links
, INT_TO_PTR(link
->ifindex
));
420 (void)unlink(link
->state_file
);
421 free(link
->state_file
);
423 udev_device_unref(link
->udev_device
);
425 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
)
426 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(carrier
->ifindex
));
427 hashmap_free(link
->bound_to_links
);
429 HASHMAP_FOREACH (carrier
, link
->bound_by_links
, i
)
430 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(carrier
->ifindex
));
431 hashmap_free(link
->bound_by_links
);
436 Link
*link_unref(Link
*link
) {
440 assert(link
->n_ref
> 0);
452 Link
*link_ref(Link
*link
) {
456 assert(link
->n_ref
> 0);
463 int link_get(Manager
*m
, int ifindex
, Link
**ret
) {
470 link
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
479 static void link_set_state(Link
*link
, LinkState state
) {
482 if (link
->state
== state
)
487 link_send_changed(link
, "AdministrativeState", NULL
);
492 static void link_enter_unmanaged(Link
*link
) {
495 log_link_debug(link
, "Unmanaged");
497 link_set_state(link
, LINK_STATE_UNMANAGED
);
502 static int link_stop_clients(Link
*link
) {
506 assert(link
->manager
);
507 assert(link
->manager
->event
);
509 if (link
->dhcp_client
) {
510 k
= sd_dhcp_client_stop(link
->dhcp_client
);
512 r
= log_link_warning_errno(link
, r
, "Could not stop DHCPv4 client: %m");
516 k
= sd_ipv4ll_stop(link
->ipv4ll
);
518 r
= log_link_warning_errno(link
, r
, "Could not stop IPv4 link-local: %m");
521 if (link
->dhcp6_client
) {
522 k
= sd_dhcp6_client_stop(link
->dhcp6_client
);
524 r
= log_link_warning_errno(link
, r
, "Could not stop DHCPv6 client: %m");
527 if (link
->ndisc_router_discovery
) {
528 k
= sd_ndisc_stop(link
->ndisc_router_discovery
);
530 r
= log_link_warning_errno(link
, r
, "Could not stop IPv6 Router Discovery: %m");
534 k
= sd_lldp_stop(link
->lldp
);
536 r
= log_link_warning_errno(link
, r
, "Could not stop LLDP: %m");
542 void link_enter_failed(Link
*link
) {
545 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
548 log_link_warning(link
, "Failed");
550 link_set_state(link
, LINK_STATE_FAILED
);
552 link_stop_clients(link
);
557 static Address
* link_find_dhcp_server_address(Link
*link
) {
561 assert(link
->network
);
563 /* The first statically configured address if there is any */
564 LIST_FOREACH(addresses
, address
, link
->network
->static_addresses
) {
566 if (address
->family
!= AF_INET
)
569 if (in_addr_is_null(address
->family
, &address
->in_addr
))
575 /* If that didn't work, find a suitable address we got from the pool */
576 LIST_FOREACH(addresses
, address
, link
->pool_addresses
) {
577 if (address
->family
!= AF_INET
)
586 static int link_enter_configured(Link
*link
) {
588 assert(link
->network
);
589 assert(link
->state
== LINK_STATE_SETTING_ROUTES
);
591 log_link_info(link
, "Configured");
593 link_set_state(link
, LINK_STATE_CONFIGURED
);
600 void link_check_ready(Link
*link
) {
609 if (!link
->static_configured
)
612 if (link_ipv4ll_enabled(link
))
613 if (!link
->ipv4ll_address
||
617 if (link_ipv6ll_enabled(link
))
618 if (!link
->ipv6ll_address
)
621 if ((link_dhcp4_enabled(link
) && !link_dhcp6_enabled(link
) &&
622 !link
->dhcp4_configured
) ||
623 (link_dhcp6_enabled(link
) && !link_dhcp4_enabled(link
) &&
624 !link
->dhcp6_configured
) ||
625 (link_dhcp4_enabled(link
) && link_dhcp6_enabled(link
) &&
626 !link
->dhcp4_configured
&& !link
->dhcp6_configured
))
629 if (link_ipv6_accept_ra_enabled(link
) && !link
->ndisc_configured
)
632 SET_FOREACH(a
, link
->addresses
, i
)
633 if (!address_is_ready(a
))
636 if (link
->state
!= LINK_STATE_CONFIGURED
)
637 link_enter_configured(link
);
642 static int route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
643 _cleanup_link_unref_ Link
*link
= userdata
;
646 assert(link
->link_messages
> 0);
647 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
648 LINK_STATE_SETTING_ROUTES
, LINK_STATE_FAILED
,
651 link
->link_messages
--;
653 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
656 r
= sd_netlink_message_get_errno(m
);
657 if (r
< 0 && r
!= -EEXIST
)
658 log_link_warning_errno(link
, r
, "Could not set route: %m");
660 if (link
->link_messages
== 0) {
661 log_link_debug(link
, "Routes set");
662 link
->static_configured
= true;
663 link_check_ready(link
);
669 static int link_enter_set_routes(Link
*link
) {
674 assert(link
->network
);
675 assert(link
->state
== LINK_STATE_SETTING_ADDRESSES
);
677 link_set_state(link
, LINK_STATE_SETTING_ROUTES
);
679 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
680 r
= route_configure(rt
, link
, &route_handler
);
682 log_link_warning_errno(link
, r
, "Could not set routes: %m");
683 link_enter_failed(link
);
687 link
->link_messages
++;
690 if (link
->link_messages
== 0) {
691 link
->static_configured
= true;
692 link_check_ready(link
);
694 log_link_debug(link
, "Setting routes");
699 int link_route_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
700 _cleanup_link_unref_ Link
*link
= userdata
;
705 assert(link
->ifname
);
707 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
710 r
= sd_netlink_message_get_errno(m
);
711 if (r
< 0 && r
!= -ESRCH
)
712 log_link_warning_errno(link
, r
, "Could not drop route: %m");
717 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
718 _cleanup_link_unref_ Link
*link
= userdata
;
724 assert(link
->ifname
);
725 assert(link
->link_messages
> 0);
726 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
727 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
729 link
->link_messages
--;
731 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
734 r
= sd_netlink_message_get_errno(m
);
735 if (r
< 0 && r
!= -EEXIST
)
736 log_link_warning_errno(link
, r
, "could not set address: %m");
738 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
740 if (link
->link_messages
== 0) {
741 log_link_debug(link
, "Addresses set");
742 link_enter_set_routes(link
);
748 static int link_push_dns_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
749 _cleanup_free_
struct in_addr
*addresses
= NULL
;
750 size_t n_addresses
= 0, n_allocated
= 0;
753 log_debug("Copying DNS server information from %s", link
->ifname
);
758 STRV_FOREACH(a
, link
->network
->dns
) {
761 /* Only look for IPv4 addresses */
762 if (inet_pton(AF_INET
, *a
, &ia
) <= 0)
765 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
768 addresses
[n_addresses
++] = ia
;
771 if (link
->network
->dhcp_dns
&&
773 const struct in_addr
*da
= NULL
;
776 n
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &da
);
779 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
782 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
787 if (n_addresses
<= 0)
790 return sd_dhcp_server_set_dns(s
, addresses
, n_addresses
);
793 static int link_push_ntp_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
794 _cleanup_free_
struct in_addr
*addresses
= NULL
;
795 size_t n_addresses
= 0, n_allocated
= 0;
801 log_debug("Copying NTP server information from %s", link
->ifname
);
803 STRV_FOREACH(a
, link
->network
->ntp
) {
806 /* Only look for IPv4 addresses */
807 if (inet_pton(AF_INET
, *a
, &ia
) <= 0)
810 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
813 addresses
[n_addresses
++] = ia
;
816 if (link
->network
->dhcp_ntp
&&
818 const struct in_addr
*da
= NULL
;
821 n
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &da
);
824 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
827 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
832 if (n_addresses
<= 0)
835 return sd_dhcp_server_set_ntp(s
, addresses
, n_addresses
);
838 static int link_enter_set_addresses(Link
*link
) {
843 assert(link
->network
);
844 assert(link
->state
!= _LINK_STATE_INVALID
);
846 link_set_state(link
, LINK_STATE_SETTING_ADDRESSES
);
848 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
849 r
= address_configure(ad
, link
, &address_handler
, false);
851 log_link_warning_errno(link
, r
, "Could not set addresses: %m");
852 link_enter_failed(link
);
856 link
->link_messages
++;
859 /* now that we can figure out a default address for the dhcp server,
861 if (link_dhcp4_server_enabled(link
)) {
864 bool acquired_uplink
= false;
866 address
= link_find_dhcp_server_address(link
);
868 log_link_warning(link
, "Failed to find suitable address for DHCPv4 server instance.");
869 link_enter_failed(link
);
873 /* use the server address' subnet as the pool */
874 r
= sd_dhcp_server_configure_pool(link
->dhcp_server
, &address
->in_addr
.in
, address
->prefixlen
,
875 link
->network
->dhcp_server_pool_offset
, link
->network
->dhcp_server_pool_size
);
880 r = sd_dhcp_server_set_router(link->dhcp_server,
881 &main_address->in_addr.in);
886 if (link
->network
->dhcp_server_max_lease_time_usec
> 0) {
887 r
= sd_dhcp_server_set_max_lease_time(
889 DIV_ROUND_UP(link
->network
->dhcp_server_max_lease_time_usec
, USEC_PER_SEC
));
894 if (link
->network
->dhcp_server_default_lease_time_usec
> 0) {
895 r
= sd_dhcp_server_set_default_lease_time(
897 DIV_ROUND_UP(link
->network
->dhcp_server_default_lease_time_usec
, USEC_PER_SEC
));
902 if (link
->network
->dhcp_server_emit_dns
) {
904 if (link
->network
->n_dhcp_server_dns
> 0)
905 r
= sd_dhcp_server_set_dns(link
->dhcp_server
, link
->network
->dhcp_server_dns
, link
->network
->n_dhcp_server_dns
);
907 uplink
= manager_find_uplink(link
->manager
, link
);
908 acquired_uplink
= true;
911 log_link_debug(link
, "Not emitting DNS server information on link, couldn't find suitable uplink.");
914 r
= link_push_dns_to_dhcp_server(uplink
, link
->dhcp_server
);
917 log_link_warning_errno(link
, r
, "Failed to set DNS server for DHCP server, ignoring: %m");
921 if (link
->network
->dhcp_server_emit_ntp
) {
923 if (link
->network
->n_dhcp_server_ntp
> 0)
924 r
= sd_dhcp_server_set_ntp(link
->dhcp_server
, link
->network
->dhcp_server_ntp
, link
->network
->n_dhcp_server_ntp
);
926 if (!acquired_uplink
)
927 uplink
= manager_find_uplink(link
->manager
, link
);
930 log_link_debug(link
, "Not emitting NTP server information on link, couldn't find suitable uplink.");
933 r
= link_push_ntp_to_dhcp_server(uplink
, link
->dhcp_server
);
937 log_link_warning_errno(link
, r
, "Failed to set NTP server for DHCP server, ignoring: %m");
940 if (link
->network
->dhcp_server_emit_timezone
) {
941 _cleanup_free_
char *buffer
= NULL
;
942 const char *tz
= NULL
;
944 if (link
->network
->dhcp_server_timezone
)
945 tz
= link
->network
->dhcp_server_timezone
;
947 r
= get_timezone(&buffer
);
949 log_warning_errno(r
, "Failed to determine timezone: %m");
955 r
= sd_dhcp_server_set_timezone(link
->dhcp_server
, tz
);
961 r
= sd_dhcp_server_start(link
->dhcp_server
);
963 log_link_warning_errno(link
, r
, "Could not start DHCPv4 server instance: %m");
965 link_enter_failed(link
);
970 log_link_debug(link
, "Offering DHCPv4 leases");
973 if (link
->link_messages
== 0)
974 link_enter_set_routes(link
);
976 log_link_debug(link
, "Setting addresses");
981 int link_address_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
982 _cleanup_link_unref_ Link
*link
= userdata
;
987 assert(link
->ifname
);
989 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
992 r
= sd_netlink_message_get_errno(m
);
993 if (r
< 0 && r
!= -EADDRNOTAVAIL
)
994 log_link_warning_errno(link
, r
, "Could not drop address: %m");
999 static int link_set_bridge_fdb(Link
*const link
) {
1000 FdbEntry
*fdb_entry
;
1003 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
1004 r
= fdb_entry_configure(link
, fdb_entry
);
1006 log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
1014 static int link_set_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1015 _cleanup_link_unref_ Link
*link
= userdata
;
1018 log_link_debug(link
, "Set link");
1020 r
= sd_netlink_message_get_errno(m
);
1021 if (r
< 0 && r
!= -EEXIST
) {
1022 log_link_error_errno(link
, r
, "Could not join netdev: %m");
1023 link_enter_failed(link
);
1030 static int set_hostname_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
1031 _cleanup_link_unref_ Link
*link
= userdata
;
1032 const sd_bus_error
*e
;
1037 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1040 e
= sd_bus_message_get_error(m
);
1042 log_link_warning_errno(link
, sd_bus_error_get_errno(e
), "Could not set hostname: %s", e
->message
);
1047 int link_set_hostname(Link
*link
, const char *hostname
) {
1051 assert(link
->manager
);
1053 log_link_debug(link
, "Setting transient hostname: '%s'", strna(hostname
));
1055 if (!link
->manager
->bus
) {
1056 /* TODO: replace by assert when we can rely on kdbus */
1057 log_link_info(link
, "Not connected to system bus, ignoring transient hostname.");
1061 r
= sd_bus_call_method_async(
1064 "org.freedesktop.hostname1",
1065 "/org/freedesktop/hostname1",
1066 "org.freedesktop.hostname1",
1068 set_hostname_handler
,
1075 return log_link_error_errno(link
, r
, "Could not set transient hostname: %m");
1082 static int set_timezone_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
1083 _cleanup_link_unref_ Link
*link
= userdata
;
1084 const sd_bus_error
*e
;
1089 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1092 e
= sd_bus_message_get_error(m
);
1094 log_link_warning_errno(link
, sd_bus_error_get_errno(e
), "Could not set timezone: %s", e
->message
);
1099 int link_set_timezone(Link
*link
, const char *tz
) {
1103 assert(link
->manager
);
1106 log_link_debug(link
, "Setting system timezone: '%s'", tz
);
1108 if (!link
->manager
->bus
) {
1109 log_link_info(link
, "Not connected to system bus, ignoring timezone.");
1113 r
= sd_bus_call_method_async(
1116 "org.freedesktop.timedate1",
1117 "/org/freedesktop/timedate1",
1118 "org.freedesktop.timedate1",
1120 set_timezone_handler
,
1126 return log_link_error_errno(link
, r
, "Could not set timezone: %m");
1133 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1134 _cleanup_link_unref_ Link
*link
= userdata
;
1139 assert(link
->ifname
);
1141 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1144 r
= sd_netlink_message_get_errno(m
);
1146 log_link_warning_errno(link
, r
, "Could not set MTU: %m");
1151 int link_set_mtu(Link
*link
, uint32_t mtu
) {
1152 _cleanup_netlink_message_unref_ sd_netlink_message
*req
= NULL
;
1156 assert(link
->manager
);
1157 assert(link
->manager
->rtnl
);
1159 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1161 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1163 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1165 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1167 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1169 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, set_mtu_handler
, link
, 0, NULL
);
1171 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1178 static int link_set_bridge(Link
*link
) {
1179 _cleanup_netlink_message_unref_ sd_netlink_message
*req
= NULL
;
1183 assert(link
->network
);
1185 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1187 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1189 r
= sd_rtnl_message_link_set_family(req
, PF_BRIDGE
);
1191 return log_link_error_errno(link
, r
, "Could not set message family: %m");
1193 r
= sd_netlink_message_open_container(req
, IFLA_PROTINFO
);
1195 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1197 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_GUARD
, !link
->network
->use_bpdu
);
1199 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_GUARD attribute: %m");
1201 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MODE
, link
->network
->hairpin
);
1203 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MODE attribute: %m");
1205 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_FAST_LEAVE
, link
->network
->fast_leave
);
1207 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_FAST_LEAVE attribute: %m");
1209 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_PROTECT
, !link
->network
->allow_port_to_be_root
);
1211 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PROTECT attribute: %m");
1213 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_UNICAST_FLOOD
, link
->network
->unicast_flood
);
1215 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_UNICAST_FLOOD attribute: %m");
1217 if(link
->network
->cost
!= 0) {
1218 r
= sd_netlink_message_append_u32(req
, IFLA_BRPORT_COST
, link
->network
->cost
);
1220 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_COST attribute: %m");
1223 r
= sd_netlink_message_close_container(req
);
1225 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1227 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_set_handler
, link
, 0, NULL
);
1229 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1236 static void lldp_handler(sd_lldp
*lldp
, int event
, void *userdata
) {
1237 Link
*link
= userdata
;
1241 assert(link
->network
);
1242 assert(link
->manager
);
1245 case SD_LLDP_EVENT_UPDATE_INFO
:
1246 r
= sd_lldp_save(link
->lldp
, link
->lldp_file
);
1248 log_link_warning_errno(link
, r
, "Could not save LLDP: %m");
1256 static int link_acquire_ipv6_conf(Link
*link
) {
1261 if (link_dhcp6_enabled(link
)) {
1262 assert(link
->dhcp6_client
);
1264 log_link_debug(link
, "Acquiring DHCPv6 lease");
1266 r
= sd_dhcp6_client_start(link
->dhcp6_client
);
1267 if (r
< 0 && r
!= -EBUSY
)
1268 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv6 lease: %m");
1271 if (link_ipv6_accept_ra_enabled(link
)) {
1272 assert(link
->ndisc_router_discovery
);
1274 log_link_debug(link
, "Discovering IPv6 routers");
1276 r
= sd_ndisc_router_discovery_start(link
->ndisc_router_discovery
);
1277 if (r
< 0 && r
!= -EBUSY
)
1278 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1284 static int link_acquire_conf(Link
*link
) {
1288 assert(link
->network
);
1289 assert(link
->manager
);
1290 assert(link
->manager
->event
);
1292 if (link_ipv4ll_enabled(link
)) {
1293 assert(link
->ipv4ll
);
1295 log_link_debug(link
, "Acquiring IPv4 link-local address");
1297 r
= sd_ipv4ll_start(link
->ipv4ll
);
1299 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1302 if (link_dhcp4_enabled(link
)) {
1303 assert(link
->dhcp_client
);
1305 log_link_debug(link
, "Acquiring DHCPv4 lease");
1307 r
= sd_dhcp_client_start(link
->dhcp_client
);
1309 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1312 if (link_lldp_enabled(link
)) {
1315 log_link_debug(link
, "Starting LLDP");
1317 r
= sd_lldp_start(link
->lldp
);
1319 return log_link_warning_errno(link
, r
, "Could not start LLDP: %m");
1325 bool link_has_carrier(Link
*link
) {
1326 /* see Documentation/networking/operstates.txt in the kernel sources */
1328 if (link
->kernel_operstate
== IF_OPER_UP
)
1331 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1332 /* operstate may not be implemented, so fall back to flags */
1333 if ((link
->flags
& IFF_LOWER_UP
) && !(link
->flags
& IFF_DORMANT
))
1339 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1340 _cleanup_link_unref_ Link
*link
= userdata
;
1345 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1348 r
= sd_netlink_message_get_errno(m
);
1350 /* we warn but don't fail the link, as it may be
1352 log_link_warning_errno(link
, r
, "Could not bring up interface: %m");
1357 static int link_up(Link
*link
) {
1358 _cleanup_netlink_message_unref_ sd_netlink_message
*req
= NULL
;
1359 uint8_t ipv6ll_mode
;
1363 assert(link
->network
);
1364 assert(link
->manager
);
1365 assert(link
->manager
->rtnl
);
1367 log_link_debug(link
, "Bringing link up");
1369 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1371 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1373 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1375 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1377 if (link
->network
->mac
) {
1378 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
1380 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
1383 if (link
->network
->mtu
) {
1384 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, link
->network
->mtu
);
1386 return log_link_error_errno(link
, r
, "Could not set MTU: %m");
1389 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1391 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1393 if (socket_ipv6_is_supported()) {
1394 /* if the kernel lacks ipv6 support setting IFF_UP fails if any ipv6 options are passed */
1395 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1397 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1399 ipv6ll_mode
= link_ipv6ll_enabled(link
) ? IN6_ADDR_GEN_MODE_EUI64
: IN6_ADDR_GEN_MODE_NONE
;
1400 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1402 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1404 if (!in_addr_is_null(AF_INET6
, &link
->network
->ipv6_token
)) {
1405 r
= sd_netlink_message_append_in6_addr(req
, IFLA_INET6_TOKEN
, &link
->network
->ipv6_token
.in6
);
1407 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_TOKEN: %m");
1410 r
= sd_netlink_message_close_container(req
);
1412 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1415 r
= sd_netlink_message_close_container(req
);
1417 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1419 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_up_handler
, link
, 0, NULL
);
1421 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1428 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1429 _cleanup_link_unref_ Link
*link
= userdata
;
1434 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1437 r
= sd_netlink_message_get_errno(m
);
1439 log_link_warning_errno(link
, r
, "Could not bring down interface: %m");
1444 static int link_down(Link
*link
) {
1445 _cleanup_netlink_message_unref_ sd_netlink_message
*req
= NULL
;
1449 assert(link
->manager
);
1450 assert(link
->manager
->rtnl
);
1452 log_link_debug(link
, "Bringing link down");
1454 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
1455 RTM_SETLINK
, link
->ifindex
);
1457 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1459 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
1461 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1463 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_down_handler
, link
, 0, NULL
);
1465 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1472 static int link_handle_bound_to_list(Link
*link
) {
1476 bool required_up
= false;
1477 bool link_is_up
= false;
1481 if (hashmap_isempty(link
->bound_to_links
))
1484 if (link
->flags
& IFF_UP
)
1487 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
1488 if (link_has_carrier(l
)) {
1493 if (!required_up
&& link_is_up
) {
1494 r
= link_down(link
);
1497 } else if (required_up
&& !link_is_up
) {
1506 static int link_handle_bound_by_list(Link
*link
) {
1513 if (hashmap_isempty(link
->bound_by_links
))
1516 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
1517 r
= link_handle_bound_to_list(l
);
1525 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
1531 if (link
== carrier
)
1534 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
1537 r
= hashmap_ensure_allocated(h
, NULL
);
1541 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
1548 static int link_new_bound_by_list(Link
*link
) {
1553 bool list_updated
= false;
1556 assert(link
->manager
);
1560 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
1561 if (!carrier
->network
)
1564 if (strv_isempty(carrier
->network
->bind_carrier
))
1567 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
, 0)) {
1568 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
1572 list_updated
= true;
1579 HASHMAP_FOREACH (carrier
, link
->bound_by_links
, i
) {
1580 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
1584 link_dirty(carrier
);
1590 static int link_new_bound_to_list(Link
*link
) {
1595 bool list_updated
= false;
1598 assert(link
->manager
);
1603 if (strv_isempty(link
->network
->bind_carrier
))
1608 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
1609 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
, 0)) {
1610 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
1614 list_updated
= true;
1621 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
1622 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
1626 link_dirty(carrier
);
1632 static int link_new_carrier_maps(Link
*link
) {
1635 r
= link_new_bound_by_list(link
);
1639 r
= link_handle_bound_by_list(link
);
1643 r
= link_new_bound_to_list(link
);
1647 r
= link_handle_bound_to_list(link
);
1654 static void link_free_bound_to_list(Link
*link
) {
1658 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
1659 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
1661 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
1662 link_dirty(bound_to
);
1668 static void link_free_bound_by_list(Link
*link
) {
1672 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
1673 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
1675 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
1676 link_dirty(bound_by
);
1677 link_handle_bound_to_list(bound_by
);
1684 static void link_free_carrier_maps(Link
*link
) {
1685 bool list_updated
= false;
1689 if (!hashmap_isempty(link
->bound_to_links
)) {
1690 link_free_bound_to_list(link
);
1691 list_updated
= true;
1694 if (!hashmap_isempty(link
->bound_by_links
)) {
1695 link_free_bound_by_list(link
);
1696 list_updated
= true;
1705 void link_drop(Link
*link
) {
1706 if (!link
|| link
->state
== LINK_STATE_LINGER
)
1709 link_set_state(link
, LINK_STATE_LINGER
);
1711 link_free_carrier_maps(link
);
1713 log_link_debug(link
, "Link removed");
1715 (void)unlink(link
->state_file
);
1721 static int link_joined(Link
*link
) {
1725 assert(link
->network
);
1727 if (!hashmap_isempty(link
->bound_to_links
)) {
1728 r
= link_handle_bound_to_list(link
);
1731 } else if (!(link
->flags
& IFF_UP
)) {
1734 link_enter_failed(link
);
1739 if(link
->network
->bridge
) {
1740 r
= link_set_bridge(link
);
1742 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
1745 return link_enter_set_addresses(link
);
1748 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1749 _cleanup_link_unref_ Link
*link
= userdata
;
1753 assert(link
->network
);
1757 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1760 r
= sd_netlink_message_get_errno(m
);
1761 if (r
< 0 && r
!= -EEXIST
) {
1762 log_link_error_errno(link
, r
, "Could not join netdev: %m");
1763 link_enter_failed(link
);
1766 log_link_debug(link
, "Joined netdev");
1768 if (link
->enslaving
<= 0)
1774 static int link_enter_join_netdev(Link
*link
) {
1780 assert(link
->network
);
1781 assert(link
->state
== LINK_STATE_PENDING
);
1783 link_set_state(link
, LINK_STATE_ENSLAVING
);
1787 if (!link
->network
->bridge
&&
1788 !link
->network
->bond
&&
1789 hashmap_isempty(link
->network
->stacked_netdevs
))
1790 return link_joined(link
);
1792 if (link
->network
->bond
) {
1793 log_struct(LOG_DEBUG
,
1794 LOG_LINK_INTERFACE(link
),
1795 LOG_NETDEV_INTERFACE(link
->network
->bond
),
1796 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
),
1799 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
1801 log_struct_errno(LOG_WARNING
, r
,
1802 LOG_LINK_INTERFACE(link
),
1803 LOG_NETDEV_INTERFACE(link
->network
->bond
),
1804 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
),
1807 link_enter_failed(link
);
1814 if (link
->network
->bridge
) {
1815 log_struct(LOG_DEBUG
,
1816 LOG_LINK_INTERFACE(link
),
1817 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
1818 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
),
1821 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
1823 log_struct_errno(LOG_WARNING
, r
,
1824 LOG_LINK_INTERFACE(link
),
1825 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
1826 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
),
1828 link_enter_failed(link
);
1835 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
1837 log_struct(LOG_DEBUG
,
1838 LOG_LINK_INTERFACE(link
),
1839 LOG_NETDEV_INTERFACE(netdev
),
1840 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
),
1843 r
= netdev_join(netdev
, link
, netdev_join_handler
);
1845 log_struct_errno(LOG_WARNING
, r
,
1846 LOG_LINK_INTERFACE(link
),
1847 LOG_NETDEV_INTERFACE(netdev
),
1848 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
),
1850 link_enter_failed(link
);
1860 static int link_set_ipv4_forward(Link
*link
) {
1863 if (!link_ipv4_forward_enabled(link
))
1866 /* We propagate the forwarding flag from one interface to the
1867 * global setting one way. This means: as long as at least one
1868 * interface was configured at any time that had IP forwarding
1869 * enabled the setting will stay on for good. We do this
1870 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
1871 * somewhat in sync (see below). */
1873 r
= write_string_file("/proc/sys/net/ipv4/ip_forward", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
1875 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
1880 static int link_set_ipv6_forward(Link
*link
) {
1883 if (!link_ipv6_forward_enabled(link
))
1886 /* On Linux, the IPv6 stack does not not know a per-interface
1887 * packet forwarding setting: either packet forwarding is on
1888 * for all, or off for all. We hence don't bother with a
1889 * per-interface setting, but simply propagate the interface
1890 * flag, if it is set, to the global flag, one-way. Note that
1891 * while IPv4 would allow a per-interface flag, we expose the
1892 * same behaviour there and also propagate the setting from
1893 * one to all, to keep things simple (see above). */
1895 r
= write_string_file("/proc/sys/net/ipv6/conf/all/forwarding", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
1897 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
1902 static int link_set_ipv6_privacy_extensions(Link
*link
) {
1903 char buf
[DECIMAL_STR_MAX(unsigned) + 1];
1904 IPv6PrivacyExtensions s
;
1905 const char *p
= NULL
;
1908 s
= link_ipv6_privacy_extensions(link
);
1912 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/use_tempaddr");
1913 xsprintf(buf
, "%u", (unsigned) link
->network
->ipv6_privacy_extensions
);
1915 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
1917 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
1922 static int link_set_ipv6_accept_ra(Link
*link
) {
1923 const char *p
= NULL
;
1926 /* Make this a NOP if IPv6 is not available */
1927 if (!socket_ipv6_is_supported())
1930 if (link
->flags
& IFF_LOOPBACK
)
1936 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/accept_ra");
1938 /* We handle router advertisments ourselves, tell the kernel to GTFO */
1939 r
= write_string_file(p
, "0", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
1941 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
1946 static int link_set_ipv6_dad_transmits(Link
*link
) {
1947 char buf
[DECIMAL_STR_MAX(int) + 1];
1948 const char *p
= NULL
;
1951 /* Make this a NOP if IPv6 is not available */
1952 if (!socket_ipv6_is_supported())
1955 if (link
->flags
& IFF_LOOPBACK
)
1961 if (link
->network
->ipv6_dad_transmits
< 0)
1964 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/dad_transmits");
1965 xsprintf(buf
, "%i", link
->network
->ipv6_dad_transmits
);
1967 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
1969 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
1974 static int link_set_ipv6_hop_limit(Link
*link
) {
1975 char buf
[DECIMAL_STR_MAX(int) + 1];
1976 const char *p
= NULL
;
1979 /* Make this a NOP if IPv6 is not available */
1980 if (!socket_ipv6_is_supported())
1983 if (link
->flags
& IFF_LOOPBACK
)
1989 if (link
->network
->ipv6_hop_limit
< 0)
1992 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/hop_limit");
1993 xsprintf(buf
, "%i", link
->network
->ipv6_hop_limit
);
1995 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
1997 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2002 static int link_drop_foreign_config(Link
*link
) {
2008 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
2009 /* we consider IPv6LL addresses to be managed by the kernel */
2010 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2013 r
= address_remove(address
, link
, link_address_remove_handler
);
2018 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2019 /* do not touch routes managed by the kernel */
2020 if (route
->protocol
== RTPROT_KERNEL
)
2023 r
= route_remove(route
, link
, link_address_remove_handler
);
2031 static int link_configure(Link
*link
) {
2035 assert(link
->network
);
2036 assert(link
->state
== LINK_STATE_PENDING
);
2038 r
= link_drop_foreign_config(link
);
2042 r
= link_set_bridge_fdb(link
);
2046 r
= link_set_ipv4_forward(link
);
2050 r
= link_set_ipv6_forward(link
);
2054 r
= link_set_ipv6_privacy_extensions(link
);
2058 r
= link_set_ipv6_accept_ra(link
);
2062 r
= link_set_ipv6_dad_transmits(link
);
2066 r
= link_set_ipv6_hop_limit(link
);
2070 if (link_ipv4ll_enabled(link
)) {
2071 r
= ipv4ll_configure(link
);
2076 if (link_dhcp4_enabled(link
)) {
2077 r
= dhcp4_configure(link
);
2082 if (link_dhcp4_server_enabled(link
)) {
2083 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
2087 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
2092 if (link_dhcp6_enabled(link
)) {
2093 r
= dhcp6_configure(link
);
2098 if (link_ipv6_accept_ra_enabled(link
)) {
2099 r
= ndisc_configure(link
);
2104 if (link_lldp_enabled(link
)) {
2105 r
= sd_lldp_new(link
->ifindex
, link
->ifname
, &link
->mac
, &link
->lldp
);
2109 r
= sd_lldp_attach_event(link
->lldp
, NULL
, 0);
2113 r
= sd_lldp_set_callback(link
->lldp
,
2114 lldp_handler
, link
);
2119 if (link_has_carrier(link
)) {
2120 r
= link_acquire_conf(link
);
2124 if (link
->ipv6ll_address
) {
2125 r
= link_acquire_ipv6_conf(link
);
2131 return link_enter_join_netdev(link
);
2134 static int link_initialized_and_synced(sd_netlink
*rtnl
, sd_netlink_message
*m
,
2136 _cleanup_link_unref_ Link
*link
= userdata
;
2141 assert(link
->ifname
);
2142 assert(link
->manager
);
2144 if (link
->state
!= LINK_STATE_PENDING
)
2147 log_link_debug(link
, "Link state is up-to-date");
2149 r
= link_new_bound_by_list(link
);
2153 r
= link_handle_bound_by_list(link
);
2157 if (!link
->network
) {
2158 r
= network_get(link
->manager
, link
->udev_device
, link
->ifname
,
2159 &link
->mac
, &network
);
2161 link_enter_unmanaged(link
);
2166 if (link
->flags
& IFF_LOOPBACK
) {
2167 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
2168 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
2170 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
2171 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
2173 if (network
->dhcp_server
)
2174 log_link_debug(link
, "Ignoring DHCP server for loopback link");
2177 r
= network_apply(link
->manager
, network
, link
);
2182 r
= link_new_bound_to_list(link
);
2186 r
= link_configure(link
);
2193 int link_initialized(Link
*link
, struct udev_device
*device
) {
2194 _cleanup_netlink_message_unref_ sd_netlink_message
*req
= NULL
;
2198 assert(link
->manager
);
2199 assert(link
->manager
->rtnl
);
2202 if (link
->state
!= LINK_STATE_PENDING
)
2205 if (link
->udev_device
)
2208 log_link_debug(link
, "udev initialized link");
2210 link
->udev_device
= udev_device_ref(device
);
2212 /* udev has initialized the link, but we don't know if we have yet
2213 * processed the NEWLINK messages with the latest state. Do a GETLINK,
2214 * when it returns we know that the pending NEWLINKs have already been
2215 * processed and that we are up-to-date */
2217 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
2222 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
,
2223 link_initialized_and_synced
, link
, 0, NULL
);
2232 static int link_load(Link
*link
) {
2233 _cleanup_free_
char *network_file
= NULL
,
2236 *dhcp4_address
= NULL
,
2237 *ipv4ll_address
= NULL
;
2238 union in_addr_union address
;
2239 union in_addr_union route_dst
;
2245 r
= parse_env_file(link
->state_file
, NEWLINE
,
2246 "NETWORK_FILE", &network_file
,
2247 "ADDRESSES", &addresses
,
2249 "DHCP4_ADDRESS", &dhcp4_address
,
2250 "IPV4LL_ADDRESS", &ipv4ll_address
,
2252 if (r
< 0 && r
!= -ENOENT
)
2253 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
2260 suffix
= strrchr(network_file
, '.');
2262 log_link_debug(link
, "Failed to get network name from %s", network_file
);
2263 goto network_file_fail
;
2267 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
2269 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
2270 goto network_file_fail
;
2273 r
= network_apply(link
->manager
, network
, link
);
2275 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
2284 _cleanup_free_
char *address_str
= NULL
;
2285 char *prefixlen_str
;
2287 unsigned char prefixlen
;
2289 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
2291 log_link_debug_errno(link
, r
, "Failed to extract next address string: %m");
2296 prefixlen_str
= strchr(address_str
, '/');
2297 if (!prefixlen_str
) {
2298 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
2302 *prefixlen_str
++ = '\0';
2304 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
2306 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
2310 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
2312 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
2316 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
2318 return log_link_error_errno(link
, r
, "Failed to add address: %m");
2325 _cleanup_free_
char *route_str
= NULL
;
2326 _cleanup_event_source_unref_ sd_event_source
*expire
= NULL
;
2328 char *prefixlen_str
;
2330 unsigned char prefixlen
, tos
, table
;
2333 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
2335 log_link_debug_errno(link
, r
, "Failed to extract next route string: %m");
2340 prefixlen_str
= strchr(route_str
, '/');
2341 if (!prefixlen_str
) {
2342 log_link_debug(link
, "Failed to parse route %s", route_str
);
2346 *prefixlen_str
++ = '\0';
2348 r
= sscanf(prefixlen_str
, "%hhu/%hhu/%"SCNu32
"/%hhu/"USEC_FMT
, &prefixlen
, &tos
, &priority
, &table
, &lifetime
);
2350 log_link_debug(link
,
2351 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
2356 r
= in_addr_from_string_auto(route_str
, &family
, &route_dst
);
2358 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
2362 r
= route_add(link
, family
, &route_dst
, prefixlen
, tos
, priority
, table
, &route
);
2364 return log_link_error_errno(link
, r
, "Failed to add route: %m");
2366 if (lifetime
!= USEC_INFINITY
) {
2367 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(), lifetime
,
2368 0, route_expire_handler
, route
);
2370 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
2373 route
->lifetime
= lifetime
;
2374 sd_event_source_unref(route
->expire
);
2375 route
->expire
= expire
;
2380 if (dhcp4_address
) {
2381 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
2383 log_link_debug_errno(link
, r
, "Falied to parse DHCPv4 address %s: %m", dhcp4_address
);
2384 goto dhcp4_address_fail
;
2387 r
= sd_dhcp_client_new(&link
->dhcp_client
);
2389 return log_link_error_errno(link
, r
, "Falied to create DHCPv4 client: %m");
2391 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
2393 return log_link_error_errno(link
, r
, "Falied to set inital DHCPv4 address %s: %m", dhcp4_address
);
2398 if (ipv4ll_address
) {
2399 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
2401 log_link_debug_errno(link
, r
, "Falied to parse IPv4LL address %s: %m", ipv4ll_address
);
2402 goto ipv4ll_address_fail
;
2405 r
= sd_ipv4ll_new(&link
->ipv4ll
);
2407 return log_link_error_errno(link
, r
, "Falied to create IPv4LL client: %m");
2409 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
2411 return log_link_error_errno(link
, r
, "Falied to set inital IPv4LL address %s: %m", ipv4ll_address
);
2414 ipv4ll_address_fail
:
2419 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
2421 _cleanup_udev_device_unref_
struct udev_device
*device
= NULL
;
2422 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
2430 r
= link_new(m
, message
, ret
);
2436 log_link_debug(link
, "Link %d added", link
->ifindex
);
2438 r
= link_load(link
);
2442 if (detect_container() <= 0) {
2443 /* not in a container, udev will be around */
2444 sprintf(ifindex_str
, "n%d", link
->ifindex
);
2445 device
= udev_device_new_from_device_id(m
->udev
, ifindex_str
);
2447 r
= log_link_warning_errno(link
, errno
, "Could not find udev device: %m");
2451 if (udev_device_get_is_initialized(device
) <= 0) {
2453 log_link_debug(link
, "link pending udev initialization...");
2457 r
= link_initialized(link
, device
);
2461 /* we are calling a callback directly, so must take a ref */
2464 r
= link_initialized_and_synced(m
->rtnl
, NULL
, link
);
2471 link_enter_failed(link
);
2475 int link_ipv6ll_gained(Link
*link
) {
2480 log_link_info(link
, "Gained IPv6LL");
2482 link
->ipv6ll_address
= true;
2483 link_check_ready(link
);
2485 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
2486 r
= link_acquire_ipv6_conf(link
);
2488 link_enter_failed(link
);
2496 static int link_carrier_gained(Link
*link
) {
2501 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
2502 r
= link_acquire_conf(link
);
2504 link_enter_failed(link
);
2509 r
= link_handle_bound_by_list(link
);
2516 static int link_carrier_lost(Link
*link
) {
2521 r
= link_stop_clients(link
);
2523 link_enter_failed(link
);
2527 r
= link_handle_bound_by_list(link
);
2534 int link_carrier_reset(Link
*link
) {
2539 if (link_has_carrier(link
)) {
2540 r
= link_carrier_lost(link
);
2544 r
= link_carrier_gained(link
);
2548 log_link_info(link
, "Reset carrier");
2555 int link_update(Link
*link
, sd_netlink_message
*m
) {
2556 struct ether_addr mac
;
2559 bool had_carrier
, carrier_gained
, carrier_lost
;
2563 assert(link
->ifname
);
2566 if (link
->state
== LINK_STATE_LINGER
) {
2568 log_link_info(link
, "Link readded");
2569 link_set_state(link
, LINK_STATE_ENSLAVING
);
2571 r
= link_new_carrier_maps(link
);
2576 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
2577 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
2578 log_link_info(link
, "Renamed to %s", ifname
);
2580 link_free_carrier_maps(link
);
2582 r
= free_and_strdup(&link
->ifname
, ifname
);
2586 r
= link_new_carrier_maps(link
);
2591 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
2592 if (r
>= 0 && mtu
> 0) {
2594 if (!link
->original_mtu
) {
2595 link
->original_mtu
= mtu
;
2596 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
2599 if (link
->dhcp_client
) {
2600 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
2603 log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
2609 /* The kernel may broadcast NEWLINK messages without the MAC address
2610 set, simply ignore them. */
2611 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
2613 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
2616 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
2619 log_link_debug(link
, "MAC address: "
2620 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
2621 mac
.ether_addr_octet
[0],
2622 mac
.ether_addr_octet
[1],
2623 mac
.ether_addr_octet
[2],
2624 mac
.ether_addr_octet
[3],
2625 mac
.ether_addr_octet
[4],
2626 mac
.ether_addr_octet
[5]);
2629 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
2631 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
2634 if (link
->dhcp_client
) {
2635 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
2636 (const uint8_t *) &link
->mac
,
2640 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
2643 if (link
->dhcp6_client
) {
2644 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
2645 (const uint8_t *) &link
->mac
,
2649 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
2654 had_carrier
= link_has_carrier(link
);
2656 r
= link_update_flags(link
, m
);
2660 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
2661 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
2663 if (carrier_gained
) {
2664 log_link_info(link
, "Gained carrier");
2666 r
= link_carrier_gained(link
);
2669 } else if (carrier_lost
) {
2670 log_link_info(link
, "Lost carrier");
2672 r
= link_carrier_lost(link
);
2681 int link_save(Link
*link
) {
2682 _cleanup_free_
char *temp_path
= NULL
;
2683 _cleanup_fclose_
FILE *f
= NULL
;
2684 const char *admin_state
, *oper_state
;
2691 assert(link
->state_file
);
2692 assert(link
->lease_file
);
2693 assert(link
->manager
);
2695 if (link
->state
== LINK_STATE_LINGER
) {
2696 unlink(link
->state_file
);
2700 admin_state
= link_state_to_string(link
->state
);
2701 assert(admin_state
);
2703 oper_state
= link_operstate_to_string(link
->operstate
);
2706 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
2710 fchmod(fileno(f
), 0644);
2713 "# This is private data. Do not parse.\n"
2716 admin_state
, oper_state
);
2718 if (link
->network
) {
2719 char **address
, **domain
;
2721 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
2723 if (link
->dhcp6_client
) {
2724 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
, &dhcp6_lease
);
2725 if (r
< 0 && r
!= -ENOMSG
)
2726 log_link_debug(link
, "No DHCPv6 lease");
2729 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
2733 STRV_FOREACH(address
, link
->network
->dns
) {
2740 if (link
->network
->dhcp_dns
&&
2742 const struct in_addr
*addresses
;
2744 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
2748 serialize_in_addrs(f
, addresses
, r
);
2753 if (link
->network
->dhcp_dns
&& dhcp6_lease
) {
2754 struct in6_addr
*in6_addrs
;
2756 r
= sd_dhcp6_lease_get_dns(dhcp6_lease
, &in6_addrs
);
2760 serialize_in6_addrs(f
, in6_addrs
, r
);
2768 STRV_FOREACH(address
, link
->network
->ntp
) {
2775 if (link
->network
->dhcp_ntp
&&
2777 const struct in_addr
*addresses
;
2779 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
2783 serialize_in_addrs(f
, addresses
, r
);
2788 if (link
->network
->dhcp_ntp
&& dhcp6_lease
) {
2789 struct in6_addr
*in6_addrs
;
2793 r
= sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease
,
2798 serialize_in6_addrs(f
, in6_addrs
, r
);
2802 r
= sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease
, &hosts
);
2804 STRV_FOREACH(hostname
, hosts
) {
2807 fputs(*hostname
, f
);
2815 fputs("DOMAINS=", f
);
2817 STRV_FOREACH(domain
, link
->network
->domains
) {
2824 if (link
->network
->dhcp_domains
&&
2826 const char *domainname
;
2828 r
= sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &domainname
);
2832 fputs(domainname
, f
);
2837 if (link
->network
->dhcp_domains
&& dhcp6_lease
) {
2840 r
= sd_dhcp6_lease_get_domains(dhcp6_lease
, &domains
);
2842 STRV_FOREACH(domain
, domains
) {
2853 fprintf(f
, "WILDCARD_DOMAIN=%s\n",
2854 yes_no(link
->network
->wildcard_domain
));
2856 fprintf(f
, "LLMNR=%s\n",
2857 resolve_support_to_string(link
->network
->llmnr
));
2859 fputs("ADDRESSES=", f
);
2861 SET_FOREACH(a
, link
->addresses
, i
) {
2862 _cleanup_free_
char *address_str
= NULL
;
2864 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
2868 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
2874 fputs("ROUTES=", f
);
2876 SET_FOREACH(route
, link
->routes
, i
) {
2877 _cleanup_free_
char *route_str
= NULL
;
2879 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
2883 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%hhu/"USEC_FMT
, space
? " " : "", route_str
,
2884 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
2891 if (!hashmap_isempty(link
->bound_to_links
)) {
2895 fputs("CARRIER_BOUND_TO=", f
);
2896 HASHMAP_FOREACH(carrier
, link
->bound_to_links
, i
) {
2899 fputs(carrier
->ifname
, f
);
2906 if (!hashmap_isempty(link
->bound_by_links
)) {
2910 fputs("CARRIER_BOUND_BY=", f
);
2911 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
2914 fputs(carrier
->ifname
, f
);
2921 if (link
->dhcp_lease
) {
2922 struct in_addr address
;
2923 const char *tz
= NULL
;
2925 assert(link
->network
);
2927 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
2929 fprintf(f
, "TIMEZONE=%s\n", tz
);
2931 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
2933 fputs("DHCP4_ADDRESS=", f
);
2934 serialize_in_addrs(f
, &address
, 1);
2938 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
2946 unlink(link
->lease_file
);
2949 struct in_addr address
;
2951 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
2953 fputs("IPV4LL_ADDRESS=", f
);
2954 serialize_in_addrs(f
, &address
, 1);
2960 assert(link
->network
);
2962 r
= sd_lldp_save(link
->lldp
, link
->lldp_file
);
2970 unlink(link
->lldp_file
);
2972 r
= fflush_and_check(f
);
2976 if (rename(temp_path
, link
->state_file
) < 0) {
2984 (void) unlink(link
->state_file
);
2986 (void) unlink(temp_path
);
2988 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
2991 /* The serialized state in /run is no longer up-to-date. */
2992 void link_dirty(Link
*link
) {
2997 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
2999 /* allocation errors are ignored */
3002 r
= set_put(link
->manager
->dirty_links
, link
);
3004 /* allocation errors are ignored */
3010 /* The serialized state in /run is up-to-date */
3011 void link_clean(Link
*link
) {
3013 assert(link
->manager
);
3015 set_remove(link
->manager
->dirty_links
, link
);
3019 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
3020 [LINK_STATE_PENDING
] = "pending",
3021 [LINK_STATE_ENSLAVING
] = "configuring",
3022 [LINK_STATE_SETTING_ADDRESSES
] = "configuring",
3023 [LINK_STATE_SETTING_ROUTES
] = "configuring",
3024 [LINK_STATE_CONFIGURED
] = "configured",
3025 [LINK_STATE_UNMANAGED
] = "unmanaged",
3026 [LINK_STATE_FAILED
] = "failed",
3027 [LINK_STATE_LINGER
] = "linger",
3030 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);
3032 static const char* const link_operstate_table
[_LINK_OPERSTATE_MAX
] = {
3033 [LINK_OPERSTATE_OFF
] = "off",
3034 [LINK_OPERSTATE_NO_CARRIER
] = "no-carrier",
3035 [LINK_OPERSTATE_DORMANT
] = "dormant",
3036 [LINK_OPERSTATE_CARRIER
] = "carrier",
3037 [LINK_OPERSTATE_DEGRADED
] = "degraded",
3038 [LINK_OPERSTATE_ROUTABLE
] = "routable",
3041 DEFINE_STRING_TABLE_LOOKUP(link_operstate
, LinkOperationalState
);