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
);
512 if (link
->dhcp_client
) {
513 k
= sd_dhcp_client_stop(link
->dhcp_client
);
515 r
= log_link_warning_errno(link
, r
, "Could not stop DHCPv4 client: %m");
519 k
= sd_ipv4ll_stop(link
->ipv4ll
);
521 r
= log_link_warning_errno(link
, r
, "Could not stop IPv4 link-local: %m");
524 if (link
->dhcp6_client
) {
525 k
= sd_dhcp6_client_stop(link
->dhcp6_client
);
527 r
= log_link_warning_errno(link
, r
, "Could not stop DHCPv6 client: %m");
530 if (link
->ndisc_router_discovery
) {
531 k
= sd_ndisc_stop(link
->ndisc_router_discovery
);
533 r
= log_link_warning_errno(link
, r
, "Could not stop IPv6 Router Discovery: %m");
537 k
= sd_lldp_stop(link
->lldp
);
539 r
= log_link_warning_errno(link
, r
, "Could not stop LLDP: %m");
545 void link_enter_failed(Link
*link
) {
548 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
551 log_link_warning(link
, "Failed");
553 link_set_state(link
, LINK_STATE_FAILED
);
555 link_stop_clients(link
);
560 static Address
* link_find_dhcp_server_address(Link
*link
) {
564 assert(link
->network
);
566 /* The first statically configured address if there is any */
567 LIST_FOREACH(addresses
, address
, link
->network
->static_addresses
) {
569 if (address
->family
!= AF_INET
)
572 if (in_addr_is_null(address
->family
, &address
->in_addr
))
578 /* If that didn't work, find a suitable address we got from the pool */
579 LIST_FOREACH(addresses
, address
, link
->pool_addresses
) {
580 if (address
->family
!= AF_INET
)
589 static int link_enter_configured(Link
*link
) {
591 assert(link
->network
);
592 assert(link
->state
== LINK_STATE_SETTING_ROUTES
);
594 log_link_info(link
, "Configured");
596 link_set_state(link
, LINK_STATE_CONFIGURED
);
603 void link_check_ready(Link
*link
) {
612 if (!link
->static_configured
)
615 if (link_ipv4ll_enabled(link
))
616 if (!link
->ipv4ll_address
||
620 if (link_ipv6ll_enabled(link
))
621 if (!link
->ipv6ll_address
)
624 if ((link_dhcp4_enabled(link
) && !link_dhcp6_enabled(link
) &&
625 !link
->dhcp4_configured
) ||
626 (link_dhcp6_enabled(link
) && !link_dhcp4_enabled(link
) &&
627 !link
->dhcp6_configured
) ||
628 (link_dhcp4_enabled(link
) && link_dhcp6_enabled(link
) &&
629 !link
->dhcp4_configured
&& !link
->dhcp6_configured
))
632 if (link_ipv6_accept_ra_enabled(link
) && !link
->ndisc_configured
)
635 SET_FOREACH(a
, link
->addresses
, i
)
636 if (!address_is_ready(a
))
639 if (link
->state
!= LINK_STATE_CONFIGURED
)
640 link_enter_configured(link
);
645 static int route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
646 _cleanup_link_unref_ Link
*link
= userdata
;
649 assert(link
->link_messages
> 0);
650 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
651 LINK_STATE_SETTING_ROUTES
, LINK_STATE_FAILED
,
654 link
->link_messages
--;
656 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
659 r
= sd_netlink_message_get_errno(m
);
660 if (r
< 0 && r
!= -EEXIST
)
661 log_link_warning_errno(link
, r
, "Could not set route: %m");
663 if (link
->link_messages
== 0) {
664 log_link_debug(link
, "Routes set");
665 link
->static_configured
= true;
666 link_check_ready(link
);
672 static int link_enter_set_routes(Link
*link
) {
677 assert(link
->network
);
678 assert(link
->state
== LINK_STATE_SETTING_ADDRESSES
);
680 link_set_state(link
, LINK_STATE_SETTING_ROUTES
);
682 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
683 r
= route_configure(rt
, link
, &route_handler
);
685 log_link_warning_errno(link
, r
, "Could not set routes: %m");
686 link_enter_failed(link
);
690 link
->link_messages
++;
693 if (link
->link_messages
== 0) {
694 link
->static_configured
= true;
695 link_check_ready(link
);
697 log_link_debug(link
, "Setting routes");
702 int link_route_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
703 _cleanup_link_unref_ Link
*link
= userdata
;
708 assert(link
->ifname
);
710 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
713 r
= sd_netlink_message_get_errno(m
);
714 if (r
< 0 && r
!= -ESRCH
)
715 log_link_warning_errno(link
, r
, "Could not drop route: %m");
720 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
721 _cleanup_link_unref_ Link
*link
= userdata
;
727 assert(link
->ifname
);
728 assert(link
->link_messages
> 0);
729 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
730 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
732 link
->link_messages
--;
734 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
737 r
= sd_netlink_message_get_errno(m
);
738 if (r
< 0 && r
!= -EEXIST
)
739 log_link_warning_errno(link
, r
, "could not set address: %m");
741 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
743 if (link
->link_messages
== 0) {
744 log_link_debug(link
, "Addresses set");
745 link_enter_set_routes(link
);
751 static int link_push_dns_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
752 _cleanup_free_
struct in_addr
*addresses
= NULL
;
753 size_t n_addresses
= 0, n_allocated
= 0;
756 log_debug("Copying DNS server information from %s", link
->ifname
);
761 STRV_FOREACH(a
, link
->network
->dns
) {
764 /* Only look for IPv4 addresses */
765 if (inet_pton(AF_INET
, *a
, &ia
) <= 0)
768 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
771 addresses
[n_addresses
++] = ia
;
774 if (link
->network
->dhcp_dns
&&
776 const struct in_addr
*da
= NULL
;
779 n
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &da
);
782 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
785 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
790 if (n_addresses
<= 0)
793 return sd_dhcp_server_set_dns(s
, addresses
, n_addresses
);
796 static int link_push_ntp_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
797 _cleanup_free_
struct in_addr
*addresses
= NULL
;
798 size_t n_addresses
= 0, n_allocated
= 0;
804 log_debug("Copying NTP server information from %s", link
->ifname
);
806 STRV_FOREACH(a
, link
->network
->ntp
) {
809 /* Only look for IPv4 addresses */
810 if (inet_pton(AF_INET
, *a
, &ia
) <= 0)
813 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
816 addresses
[n_addresses
++] = ia
;
819 if (link
->network
->dhcp_ntp
&&
821 const struct in_addr
*da
= NULL
;
824 n
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &da
);
827 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
830 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
835 if (n_addresses
<= 0)
838 return sd_dhcp_server_set_ntp(s
, addresses
, n_addresses
);
841 static int link_enter_set_addresses(Link
*link
) {
846 assert(link
->network
);
847 assert(link
->state
!= _LINK_STATE_INVALID
);
849 link_set_state(link
, LINK_STATE_SETTING_ADDRESSES
);
851 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
852 r
= address_configure(ad
, link
, &address_handler
, false);
854 log_link_warning_errno(link
, r
, "Could not set addresses: %m");
855 link_enter_failed(link
);
859 link
->link_messages
++;
862 /* now that we can figure out a default address for the dhcp server,
864 if (link_dhcp4_server_enabled(link
)) {
867 bool acquired_uplink
= false;
869 address
= link_find_dhcp_server_address(link
);
871 log_link_warning(link
, "Failed to find suitable address for DHCPv4 server instance.");
872 link_enter_failed(link
);
876 /* use the server address' subnet as the pool */
877 r
= sd_dhcp_server_configure_pool(link
->dhcp_server
, &address
->in_addr
.in
, address
->prefixlen
,
878 link
->network
->dhcp_server_pool_offset
, link
->network
->dhcp_server_pool_size
);
883 r = sd_dhcp_server_set_router(link->dhcp_server,
884 &main_address->in_addr.in);
889 if (link
->network
->dhcp_server_max_lease_time_usec
> 0) {
890 r
= sd_dhcp_server_set_max_lease_time(
892 DIV_ROUND_UP(link
->network
->dhcp_server_max_lease_time_usec
, USEC_PER_SEC
));
897 if (link
->network
->dhcp_server_default_lease_time_usec
> 0) {
898 r
= sd_dhcp_server_set_default_lease_time(
900 DIV_ROUND_UP(link
->network
->dhcp_server_default_lease_time_usec
, USEC_PER_SEC
));
905 if (link
->network
->dhcp_server_emit_dns
) {
907 if (link
->network
->n_dhcp_server_dns
> 0)
908 r
= sd_dhcp_server_set_dns(link
->dhcp_server
, link
->network
->dhcp_server_dns
, link
->network
->n_dhcp_server_dns
);
910 uplink
= manager_find_uplink(link
->manager
, link
);
911 acquired_uplink
= true;
914 log_link_debug(link
, "Not emitting DNS server information on link, couldn't find suitable uplink.");
917 r
= link_push_dns_to_dhcp_server(uplink
, link
->dhcp_server
);
920 log_link_warning_errno(link
, r
, "Failed to set DNS server for DHCP server, ignoring: %m");
924 if (link
->network
->dhcp_server_emit_ntp
) {
926 if (link
->network
->n_dhcp_server_ntp
> 0)
927 r
= sd_dhcp_server_set_ntp(link
->dhcp_server
, link
->network
->dhcp_server_ntp
, link
->network
->n_dhcp_server_ntp
);
929 if (!acquired_uplink
)
930 uplink
= manager_find_uplink(link
->manager
, link
);
933 log_link_debug(link
, "Not emitting NTP server information on link, couldn't find suitable uplink.");
936 r
= link_push_ntp_to_dhcp_server(uplink
, link
->dhcp_server
);
940 log_link_warning_errno(link
, r
, "Failed to set NTP server for DHCP server, ignoring: %m");
943 if (link
->network
->dhcp_server_emit_timezone
) {
944 _cleanup_free_
char *buffer
= NULL
;
945 const char *tz
= NULL
;
947 if (link
->network
->dhcp_server_timezone
)
948 tz
= link
->network
->dhcp_server_timezone
;
950 r
= get_timezone(&buffer
);
952 log_warning_errno(r
, "Failed to determine timezone: %m");
958 r
= sd_dhcp_server_set_timezone(link
->dhcp_server
, tz
);
964 r
= sd_dhcp_server_start(link
->dhcp_server
);
966 log_link_warning_errno(link
, r
, "Could not start DHCPv4 server instance: %m");
968 link_enter_failed(link
);
973 log_link_debug(link
, "Offering DHCPv4 leases");
976 if (link
->link_messages
== 0)
977 link_enter_set_routes(link
);
979 log_link_debug(link
, "Setting addresses");
984 int link_address_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
985 _cleanup_link_unref_ Link
*link
= userdata
;
990 assert(link
->ifname
);
992 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
995 r
= sd_netlink_message_get_errno(m
);
996 if (r
< 0 && r
!= -EADDRNOTAVAIL
)
997 log_link_warning_errno(link
, r
, "Could not drop address: %m");
1002 static int link_set_bridge_fdb(Link
*const link
) {
1003 FdbEntry
*fdb_entry
;
1006 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
1007 r
= fdb_entry_configure(link
, fdb_entry
);
1009 log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
1017 static int link_set_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1018 _cleanup_link_unref_ Link
*link
= userdata
;
1021 log_link_debug(link
, "Set link");
1023 r
= sd_netlink_message_get_errno(m
);
1024 if (r
< 0 && r
!= -EEXIST
) {
1025 log_link_error_errno(link
, r
, "Could not join netdev: %m");
1026 link_enter_failed(link
);
1033 static int set_hostname_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
1034 _cleanup_link_unref_ Link
*link
= userdata
;
1035 const sd_bus_error
*e
;
1040 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1043 e
= sd_bus_message_get_error(m
);
1045 log_link_warning_errno(link
, sd_bus_error_get_errno(e
), "Could not set hostname: %s", e
->message
);
1050 int link_set_hostname(Link
*link
, const char *hostname
) {
1054 assert(link
->manager
);
1056 log_link_debug(link
, "Setting transient hostname: '%s'", strna(hostname
));
1058 if (!link
->manager
->bus
) {
1059 /* TODO: replace by assert when we can rely on kdbus */
1060 log_link_info(link
, "Not connected to system bus, ignoring transient hostname.");
1064 r
= sd_bus_call_method_async(
1067 "org.freedesktop.hostname1",
1068 "/org/freedesktop/hostname1",
1069 "org.freedesktop.hostname1",
1071 set_hostname_handler
,
1078 return log_link_error_errno(link
, r
, "Could not set transient hostname: %m");
1085 static int set_timezone_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
1086 _cleanup_link_unref_ Link
*link
= userdata
;
1087 const sd_bus_error
*e
;
1092 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1095 e
= sd_bus_message_get_error(m
);
1097 log_link_warning_errno(link
, sd_bus_error_get_errno(e
), "Could not set timezone: %s", e
->message
);
1102 int link_set_timezone(Link
*link
, const char *tz
) {
1106 assert(link
->manager
);
1109 log_link_debug(link
, "Setting system timezone: '%s'", tz
);
1111 if (!link
->manager
->bus
) {
1112 log_link_info(link
, "Not connected to system bus, ignoring timezone.");
1116 r
= sd_bus_call_method_async(
1119 "org.freedesktop.timedate1",
1120 "/org/freedesktop/timedate1",
1121 "org.freedesktop.timedate1",
1123 set_timezone_handler
,
1129 return log_link_error_errno(link
, r
, "Could not set timezone: %m");
1136 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1137 _cleanup_link_unref_ Link
*link
= userdata
;
1142 assert(link
->ifname
);
1144 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1147 r
= sd_netlink_message_get_errno(m
);
1149 log_link_warning_errno(link
, r
, "Could not set MTU: %m");
1154 int link_set_mtu(Link
*link
, uint32_t mtu
) {
1155 _cleanup_netlink_message_unref_ sd_netlink_message
*req
= NULL
;
1159 assert(link
->manager
);
1160 assert(link
->manager
->rtnl
);
1162 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1164 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1166 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1168 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1170 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1172 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, set_mtu_handler
, link
, 0, NULL
);
1174 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1181 static int link_set_bridge(Link
*link
) {
1182 _cleanup_netlink_message_unref_ sd_netlink_message
*req
= NULL
;
1186 assert(link
->network
);
1188 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1190 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1192 r
= sd_rtnl_message_link_set_family(req
, PF_BRIDGE
);
1194 return log_link_error_errno(link
, r
, "Could not set message family: %m");
1196 r
= sd_netlink_message_open_container(req
, IFLA_PROTINFO
);
1198 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1200 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_GUARD
, !link
->network
->use_bpdu
);
1202 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_GUARD attribute: %m");
1204 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MODE
, link
->network
->hairpin
);
1206 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MODE attribute: %m");
1208 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_FAST_LEAVE
, link
->network
->fast_leave
);
1210 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_FAST_LEAVE attribute: %m");
1212 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_PROTECT
, !link
->network
->allow_port_to_be_root
);
1214 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PROTECT attribute: %m");
1216 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_UNICAST_FLOOD
, link
->network
->unicast_flood
);
1218 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_UNICAST_FLOOD attribute: %m");
1220 if(link
->network
->cost
!= 0) {
1221 r
= sd_netlink_message_append_u32(req
, IFLA_BRPORT_COST
, link
->network
->cost
);
1223 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_COST attribute: %m");
1226 r
= sd_netlink_message_close_container(req
);
1228 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1230 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_set_handler
, link
, 0, NULL
);
1232 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1239 static void lldp_handler(sd_lldp
*lldp
, int event
, void *userdata
) {
1240 Link
*link
= userdata
;
1244 assert(link
->network
);
1245 assert(link
->manager
);
1248 case SD_LLDP_EVENT_UPDATE_INFO
:
1249 r
= sd_lldp_save(link
->lldp
, link
->lldp_file
);
1251 log_link_warning_errno(link
, r
, "Could not save LLDP: %m");
1259 static int link_acquire_ipv6_conf(Link
*link
) {
1264 if (link_dhcp6_enabled(link
)) {
1265 assert(link
->dhcp6_client
);
1267 log_link_debug(link
, "Acquiring DHCPv6 lease");
1269 r
= sd_dhcp6_client_start(link
->dhcp6_client
);
1271 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv6 lease: %m");
1274 if (link_ipv6_accept_ra_enabled(link
)) {
1275 assert(link
->ndisc_router_discovery
);
1277 log_link_debug(link
, "Discovering IPv6 routers");
1279 r
= sd_ndisc_router_discovery_start(link
->ndisc_router_discovery
);
1281 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1287 static int link_acquire_conf(Link
*link
) {
1291 assert(link
->network
);
1292 assert(link
->manager
);
1293 assert(link
->manager
->event
);
1295 if (link_ipv4ll_enabled(link
)) {
1296 assert(link
->ipv4ll
);
1298 log_link_debug(link
, "Acquiring IPv4 link-local address");
1300 r
= sd_ipv4ll_start(link
->ipv4ll
);
1302 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1305 if (link_dhcp4_enabled(link
)) {
1306 assert(link
->dhcp_client
);
1308 log_link_debug(link
, "Acquiring DHCPv4 lease");
1310 r
= sd_dhcp_client_start(link
->dhcp_client
);
1312 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1315 if (link_lldp_enabled(link
)) {
1318 log_link_debug(link
, "Starting LLDP");
1320 r
= sd_lldp_start(link
->lldp
);
1322 return log_link_warning_errno(link
, r
, "Could not start LLDP: %m");
1328 bool link_has_carrier(Link
*link
) {
1329 /* see Documentation/networking/operstates.txt in the kernel sources */
1331 if (link
->kernel_operstate
== IF_OPER_UP
)
1334 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1335 /* operstate may not be implemented, so fall back to flags */
1336 if ((link
->flags
& IFF_LOWER_UP
) && !(link
->flags
& IFF_DORMANT
))
1342 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1343 _cleanup_link_unref_ Link
*link
= userdata
;
1348 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1351 r
= sd_netlink_message_get_errno(m
);
1353 /* we warn but don't fail the link, as it may be
1355 log_link_warning_errno(link
, r
, "Could not bring up interface: %m");
1360 static int link_up(Link
*link
) {
1361 _cleanup_netlink_message_unref_ sd_netlink_message
*req
= NULL
;
1362 uint8_t ipv6ll_mode
;
1366 assert(link
->network
);
1367 assert(link
->manager
);
1368 assert(link
->manager
->rtnl
);
1370 log_link_debug(link
, "Bringing link up");
1372 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1374 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1376 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1378 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1380 if (link
->network
->mac
) {
1381 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
1383 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
1386 if (link
->network
->mtu
) {
1387 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, link
->network
->mtu
);
1389 return log_link_error_errno(link
, r
, "Could not set MTU: %m");
1392 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1394 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1396 if (socket_ipv6_is_supported()) {
1397 /* if the kernel lacks ipv6 support setting IFF_UP fails if any ipv6 options are passed */
1398 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1400 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1402 ipv6ll_mode
= link_ipv6ll_enabled(link
) ? IN6_ADDR_GEN_MODE_EUI64
: IN6_ADDR_GEN_MODE_NONE
;
1403 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1405 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1407 if (!in_addr_is_null(AF_INET6
, &link
->network
->ipv6_token
)) {
1408 r
= sd_netlink_message_append_in6_addr(req
, IFLA_INET6_TOKEN
, &link
->network
->ipv6_token
.in6
);
1410 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_TOKEN: %m");
1413 r
= sd_netlink_message_close_container(req
);
1415 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1418 r
= sd_netlink_message_close_container(req
);
1420 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1422 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_up_handler
, link
, 0, NULL
);
1424 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1431 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1432 _cleanup_link_unref_ Link
*link
= userdata
;
1437 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1440 r
= sd_netlink_message_get_errno(m
);
1442 log_link_warning_errno(link
, r
, "Could not bring down interface: %m");
1447 static int link_down(Link
*link
) {
1448 _cleanup_netlink_message_unref_ sd_netlink_message
*req
= NULL
;
1452 assert(link
->manager
);
1453 assert(link
->manager
->rtnl
);
1455 log_link_debug(link
, "Bringing link down");
1457 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
1458 RTM_SETLINK
, link
->ifindex
);
1460 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1462 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
1464 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1466 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_down_handler
, link
, 0, NULL
);
1468 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1475 static int link_handle_bound_to_list(Link
*link
) {
1479 bool required_up
= false;
1480 bool link_is_up
= false;
1484 if (hashmap_isempty(link
->bound_to_links
))
1487 if (link
->flags
& IFF_UP
)
1490 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
1491 if (link_has_carrier(l
)) {
1496 if (!required_up
&& link_is_up
) {
1497 r
= link_down(link
);
1500 } else if (required_up
&& !link_is_up
) {
1509 static int link_handle_bound_by_list(Link
*link
) {
1516 if (hashmap_isempty(link
->bound_by_links
))
1519 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
1520 r
= link_handle_bound_to_list(l
);
1528 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
1534 if (link
== carrier
)
1537 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
1540 r
= hashmap_ensure_allocated(h
, NULL
);
1544 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
1551 static int link_new_bound_by_list(Link
*link
) {
1556 bool list_updated
= false;
1559 assert(link
->manager
);
1563 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
1564 if (!carrier
->network
)
1567 if (strv_isempty(carrier
->network
->bind_carrier
))
1570 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
, 0)) {
1571 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
1575 list_updated
= true;
1582 HASHMAP_FOREACH (carrier
, link
->bound_by_links
, i
) {
1583 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
1587 link_dirty(carrier
);
1593 static int link_new_bound_to_list(Link
*link
) {
1598 bool list_updated
= false;
1601 assert(link
->manager
);
1606 if (strv_isempty(link
->network
->bind_carrier
))
1611 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
1612 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
, 0)) {
1613 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
1617 list_updated
= true;
1624 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
1625 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
1629 link_dirty(carrier
);
1635 static int link_new_carrier_maps(Link
*link
) {
1638 r
= link_new_bound_by_list(link
);
1642 r
= link_handle_bound_by_list(link
);
1646 r
= link_new_bound_to_list(link
);
1650 r
= link_handle_bound_to_list(link
);
1657 static void link_free_bound_to_list(Link
*link
) {
1661 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
1662 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
1664 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
1665 link_dirty(bound_to
);
1671 static void link_free_bound_by_list(Link
*link
) {
1675 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
1676 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
1678 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
1679 link_dirty(bound_by
);
1680 link_handle_bound_to_list(bound_by
);
1687 static void link_free_carrier_maps(Link
*link
) {
1688 bool list_updated
= false;
1692 if (!hashmap_isempty(link
->bound_to_links
)) {
1693 link_free_bound_to_list(link
);
1694 list_updated
= true;
1697 if (!hashmap_isempty(link
->bound_by_links
)) {
1698 link_free_bound_by_list(link
);
1699 list_updated
= true;
1708 void link_drop(Link
*link
) {
1709 if (!link
|| link
->state
== LINK_STATE_LINGER
)
1712 link_set_state(link
, LINK_STATE_LINGER
);
1714 link_free_carrier_maps(link
);
1716 log_link_debug(link
, "Link removed");
1718 (void)unlink(link
->state_file
);
1724 static int link_joined(Link
*link
) {
1728 assert(link
->network
);
1730 if (!hashmap_isempty(link
->bound_to_links
)) {
1731 r
= link_handle_bound_to_list(link
);
1734 } else if (!(link
->flags
& IFF_UP
)) {
1737 link_enter_failed(link
);
1742 if(link
->network
->bridge
) {
1743 r
= link_set_bridge(link
);
1745 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
1748 return link_enter_set_addresses(link
);
1751 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1752 _cleanup_link_unref_ Link
*link
= userdata
;
1756 assert(link
->network
);
1760 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1763 r
= sd_netlink_message_get_errno(m
);
1764 if (r
< 0 && r
!= -EEXIST
) {
1765 log_link_error_errno(link
, r
, "Could not join netdev: %m");
1766 link_enter_failed(link
);
1769 log_link_debug(link
, "Joined netdev");
1771 if (link
->enslaving
<= 0)
1777 static int link_enter_join_netdev(Link
*link
) {
1783 assert(link
->network
);
1784 assert(link
->state
== LINK_STATE_PENDING
);
1786 link_set_state(link
, LINK_STATE_ENSLAVING
);
1790 if (!link
->network
->bridge
&&
1791 !link
->network
->bond
&&
1792 hashmap_isempty(link
->network
->stacked_netdevs
))
1793 return link_joined(link
);
1795 if (link
->network
->bond
) {
1796 log_struct(LOG_DEBUG
,
1797 LOG_LINK_INTERFACE(link
),
1798 LOG_NETDEV_INTERFACE(link
->network
->bond
),
1799 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
),
1802 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
1804 log_struct_errno(LOG_WARNING
, r
,
1805 LOG_LINK_INTERFACE(link
),
1806 LOG_NETDEV_INTERFACE(link
->network
->bond
),
1807 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
),
1810 link_enter_failed(link
);
1817 if (link
->network
->bridge
) {
1818 log_struct(LOG_DEBUG
,
1819 LOG_LINK_INTERFACE(link
),
1820 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
1821 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
),
1824 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
1826 log_struct_errno(LOG_WARNING
, r
,
1827 LOG_LINK_INTERFACE(link
),
1828 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
1829 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
),
1831 link_enter_failed(link
);
1838 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
1840 log_struct(LOG_DEBUG
,
1841 LOG_LINK_INTERFACE(link
),
1842 LOG_NETDEV_INTERFACE(netdev
),
1843 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
),
1846 r
= netdev_join(netdev
, link
, netdev_join_handler
);
1848 log_struct_errno(LOG_WARNING
, r
,
1849 LOG_LINK_INTERFACE(link
),
1850 LOG_NETDEV_INTERFACE(netdev
),
1851 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
),
1853 link_enter_failed(link
);
1863 static int link_set_ipv4_forward(Link
*link
) {
1866 if (!link_ipv4_forward_enabled(link
))
1869 /* We propagate the forwarding flag from one interface to the
1870 * global setting one way. This means: as long as at least one
1871 * interface was configured at any time that had IP forwarding
1872 * enabled the setting will stay on for good. We do this
1873 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
1874 * somewhat in sync (see below). */
1876 r
= write_string_file("/proc/sys/net/ipv4/ip_forward", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
1878 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
1883 static int link_set_ipv6_forward(Link
*link
) {
1886 if (!link_ipv6_forward_enabled(link
))
1889 /* On Linux, the IPv6 stack does not not know a per-interface
1890 * packet forwarding setting: either packet forwarding is on
1891 * for all, or off for all. We hence don't bother with a
1892 * per-interface setting, but simply propagate the interface
1893 * flag, if it is set, to the global flag, one-way. Note that
1894 * while IPv4 would allow a per-interface flag, we expose the
1895 * same behaviour there and also propagate the setting from
1896 * one to all, to keep things simple (see above). */
1898 r
= write_string_file("/proc/sys/net/ipv6/conf/all/forwarding", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
1900 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
1905 static int link_set_ipv6_privacy_extensions(Link
*link
) {
1906 char buf
[DECIMAL_STR_MAX(unsigned) + 1];
1907 IPv6PrivacyExtensions s
;
1908 const char *p
= NULL
;
1911 s
= link_ipv6_privacy_extensions(link
);
1915 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/use_tempaddr");
1916 xsprintf(buf
, "%u", (unsigned) link
->network
->ipv6_privacy_extensions
);
1918 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
1920 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
1925 static int link_set_ipv6_accept_ra(Link
*link
) {
1926 const char *p
= NULL
;
1929 /* Make this a NOP if IPv6 is not available */
1930 if (!socket_ipv6_is_supported())
1933 if (link
->flags
& IFF_LOOPBACK
)
1939 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/accept_ra");
1941 /* We handle router advertisments ourselves, tell the kernel to GTFO */
1942 r
= write_string_file(p
, "0", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
1944 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
1949 static int link_set_ipv6_dad_transmits(Link
*link
) {
1950 char buf
[DECIMAL_STR_MAX(int) + 1];
1951 const char *p
= NULL
;
1954 /* Make this a NOP if IPv6 is not available */
1955 if (!socket_ipv6_is_supported())
1958 if (link
->flags
& IFF_LOOPBACK
)
1964 if (link
->network
->ipv6_dad_transmits
< 0)
1967 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/dad_transmits");
1968 xsprintf(buf
, "%i", link
->network
->ipv6_dad_transmits
);
1970 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
1972 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
1977 static int link_set_ipv6_hop_limit(Link
*link
) {
1978 char buf
[DECIMAL_STR_MAX(int) + 1];
1979 const char *p
= NULL
;
1982 /* Make this a NOP if IPv6 is not available */
1983 if (!socket_ipv6_is_supported())
1986 if (link
->flags
& IFF_LOOPBACK
)
1992 if (link
->network
->ipv6_hop_limit
< 0)
1995 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/hop_limit");
1996 xsprintf(buf
, "%i", link
->network
->ipv6_hop_limit
);
1998 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2000 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2005 static int link_drop_foreign_config(Link
*link
) {
2011 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
2012 /* we consider IPv6LL addresses to be managed by the kernel */
2013 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2016 r
= address_remove(address
, link
, link_address_remove_handler
);
2021 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2022 /* do not touch routes managed by the kernel */
2023 if (route
->protocol
== RTPROT_KERNEL
)
2026 r
= route_remove(route
, link
, link_address_remove_handler
);
2034 static int link_configure(Link
*link
) {
2038 assert(link
->network
);
2039 assert(link
->state
== LINK_STATE_PENDING
);
2041 r
= link_drop_foreign_config(link
);
2045 r
= link_set_bridge_fdb(link
);
2049 r
= link_set_ipv4_forward(link
);
2053 r
= link_set_ipv6_forward(link
);
2057 r
= link_set_ipv6_privacy_extensions(link
);
2061 r
= link_set_ipv6_accept_ra(link
);
2065 r
= link_set_ipv6_dad_transmits(link
);
2069 r
= link_set_ipv6_hop_limit(link
);
2073 if (link_ipv4ll_enabled(link
)) {
2074 r
= ipv4ll_configure(link
);
2079 if (link_dhcp4_enabled(link
)) {
2080 r
= dhcp4_configure(link
);
2085 if (link_dhcp4_server_enabled(link
)) {
2086 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
2090 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
2095 if (link_dhcp6_enabled(link
)) {
2096 r
= dhcp6_configure(link
);
2101 if (link_ipv6_accept_ra_enabled(link
)) {
2102 r
= ndisc_configure(link
);
2107 if (link_lldp_enabled(link
)) {
2108 r
= sd_lldp_new(link
->ifindex
, link
->ifname
, &link
->mac
, &link
->lldp
);
2112 r
= sd_lldp_attach_event(link
->lldp
, NULL
, 0);
2116 r
= sd_lldp_set_callback(link
->lldp
,
2117 lldp_handler
, link
);
2122 if (link_has_carrier(link
)) {
2123 r
= link_acquire_conf(link
);
2127 if (link
->ipv6ll_address
) {
2128 r
= link_acquire_ipv6_conf(link
);
2134 return link_enter_join_netdev(link
);
2137 static int link_initialized_and_synced(sd_netlink
*rtnl
, sd_netlink_message
*m
,
2139 _cleanup_link_unref_ Link
*link
= userdata
;
2144 assert(link
->ifname
);
2145 assert(link
->manager
);
2147 if (link
->state
!= LINK_STATE_PENDING
)
2150 log_link_debug(link
, "Link state is up-to-date");
2152 r
= link_new_bound_by_list(link
);
2156 r
= link_handle_bound_by_list(link
);
2160 if (!link
->network
) {
2161 r
= network_get(link
->manager
, link
->udev_device
, link
->ifname
,
2162 &link
->mac
, &network
);
2164 link_enter_unmanaged(link
);
2169 if (link
->flags
& IFF_LOOPBACK
) {
2170 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
2171 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
2173 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
2174 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
2176 if (network
->dhcp_server
)
2177 log_link_debug(link
, "Ignoring DHCP server for loopback link");
2180 r
= network_apply(link
->manager
, network
, link
);
2185 r
= link_new_bound_to_list(link
);
2189 r
= link_configure(link
);
2196 int link_initialized(Link
*link
, struct udev_device
*device
) {
2197 _cleanup_netlink_message_unref_ sd_netlink_message
*req
= NULL
;
2201 assert(link
->manager
);
2202 assert(link
->manager
->rtnl
);
2205 if (link
->state
!= LINK_STATE_PENDING
)
2208 if (link
->udev_device
)
2211 log_link_debug(link
, "udev initialized link");
2213 link
->udev_device
= udev_device_ref(device
);
2215 /* udev has initialized the link, but we don't know if we have yet
2216 * processed the NEWLINK messages with the latest state. Do a GETLINK,
2217 * when it returns we know that the pending NEWLINKs have already been
2218 * processed and that we are up-to-date */
2220 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
2225 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
,
2226 link_initialized_and_synced
, link
, 0, NULL
);
2235 static int link_load(Link
*link
) {
2236 _cleanup_free_
char *network_file
= NULL
,
2239 *dhcp4_address
= NULL
,
2240 *ipv4ll_address
= NULL
;
2241 union in_addr_union address
;
2242 union in_addr_union route_dst
;
2248 r
= parse_env_file(link
->state_file
, NEWLINE
,
2249 "NETWORK_FILE", &network_file
,
2250 "ADDRESSES", &addresses
,
2252 "DHCP4_ADDRESS", &dhcp4_address
,
2253 "IPV4LL_ADDRESS", &ipv4ll_address
,
2255 if (r
< 0 && r
!= -ENOENT
)
2256 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
2263 suffix
= strrchr(network_file
, '.');
2265 log_link_debug(link
, "Failed to get network name from %s", network_file
);
2266 goto network_file_fail
;
2270 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
2272 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
2273 goto network_file_fail
;
2276 r
= network_apply(link
->manager
, network
, link
);
2278 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
2287 _cleanup_free_
char *address_str
= NULL
;
2288 char *prefixlen_str
;
2290 unsigned char prefixlen
;
2292 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
2294 log_link_debug_errno(link
, r
, "Failed to extract next address string: %m");
2299 prefixlen_str
= strchr(address_str
, '/');
2300 if (!prefixlen_str
) {
2301 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
2305 *prefixlen_str
++ = '\0';
2307 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
2309 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
2313 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
2315 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
2319 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
2321 return log_link_error_errno(link
, r
, "Failed to add address: %m");
2328 _cleanup_free_
char *route_str
= NULL
;
2329 _cleanup_event_source_unref_ sd_event_source
*expire
= NULL
;
2331 char *prefixlen_str
;
2333 unsigned char prefixlen
, tos
, table
;
2336 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
2338 log_link_debug_errno(link
, r
, "Failed to extract next route string: %m");
2343 prefixlen_str
= strchr(route_str
, '/');
2344 if (!prefixlen_str
) {
2345 log_link_debug(link
, "Failed to parse route %s", route_str
);
2349 *prefixlen_str
++ = '\0';
2351 r
= sscanf(prefixlen_str
, "%hhu/%hhu/%"SCNu32
"/%hhu/"USEC_FMT
, &prefixlen
, &tos
, &priority
, &table
, &lifetime
);
2353 log_link_debug(link
,
2354 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
2359 r
= in_addr_from_string_auto(route_str
, &family
, &route_dst
);
2361 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
2365 r
= route_add(link
, family
, &route_dst
, prefixlen
, tos
, priority
, table
, &route
);
2367 return log_link_error_errno(link
, r
, "Failed to add route: %m");
2369 if (lifetime
!= USEC_INFINITY
) {
2370 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(), lifetime
,
2371 0, route_expire_handler
, route
);
2373 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
2376 route
->lifetime
= lifetime
;
2377 sd_event_source_unref(route
->expire
);
2378 route
->expire
= expire
;
2383 if (dhcp4_address
) {
2384 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
2386 log_link_debug_errno(link
, r
, "Falied to parse DHCPv4 address %s: %m", dhcp4_address
);
2387 goto dhcp4_address_fail
;
2390 r
= sd_dhcp_client_new(&link
->dhcp_client
);
2392 return log_link_error_errno(link
, r
, "Falied to create DHCPv4 client: %m");
2394 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
2396 return log_link_error_errno(link
, r
, "Falied to set inital DHCPv4 address %s: %m", dhcp4_address
);
2401 if (ipv4ll_address
) {
2402 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
2404 log_link_debug_errno(link
, r
, "Falied to parse IPv4LL address %s: %m", ipv4ll_address
);
2405 goto ipv4ll_address_fail
;
2408 r
= sd_ipv4ll_new(&link
->ipv4ll
);
2410 return log_link_error_errno(link
, r
, "Falied to create IPv4LL client: %m");
2412 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
2414 return log_link_error_errno(link
, r
, "Falied to set inital IPv4LL address %s: %m", ipv4ll_address
);
2417 ipv4ll_address_fail
:
2422 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
2424 _cleanup_udev_device_unref_
struct udev_device
*device
= NULL
;
2425 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
2433 r
= link_new(m
, message
, ret
);
2439 log_link_debug(link
, "Link %d added", link
->ifindex
);
2441 r
= link_load(link
);
2445 if (detect_container() <= 0) {
2446 /* not in a container, udev will be around */
2447 sprintf(ifindex_str
, "n%d", link
->ifindex
);
2448 device
= udev_device_new_from_device_id(m
->udev
, ifindex_str
);
2450 r
= log_link_warning_errno(link
, errno
, "Could not find udev device: %m");
2454 if (udev_device_get_is_initialized(device
) <= 0) {
2456 log_link_debug(link
, "link pending udev initialization...");
2460 r
= link_initialized(link
, device
);
2464 /* we are calling a callback directly, so must take a ref */
2467 r
= link_initialized_and_synced(m
->rtnl
, NULL
, link
);
2474 link_enter_failed(link
);
2478 int link_ipv6ll_gained(Link
*link
) {
2483 log_link_info(link
, "Gained IPv6LL");
2485 link
->ipv6ll_address
= true;
2486 link_check_ready(link
);
2488 if (link
->network
) {
2489 r
= link_acquire_ipv6_conf(link
);
2491 link_enter_failed(link
);
2499 static int link_carrier_gained(Link
*link
) {
2504 if (link
->network
) {
2505 r
= link_acquire_conf(link
);
2507 link_enter_failed(link
);
2512 r
= link_handle_bound_by_list(link
);
2519 static int link_carrier_lost(Link
*link
) {
2524 r
= link_stop_clients(link
);
2526 link_enter_failed(link
);
2530 r
= link_handle_bound_by_list(link
);
2537 int link_carrier_reset(Link
*link
) {
2542 if (link_has_carrier(link
)) {
2543 r
= link_carrier_lost(link
);
2547 r
= link_carrier_gained(link
);
2551 log_link_info(link
, "Reset carrier");
2558 int link_update(Link
*link
, sd_netlink_message
*m
) {
2559 struct ether_addr mac
;
2562 bool had_carrier
, carrier_gained
, carrier_lost
;
2566 assert(link
->ifname
);
2569 if (link
->state
== LINK_STATE_LINGER
) {
2571 log_link_info(link
, "Link readded");
2572 link_set_state(link
, LINK_STATE_ENSLAVING
);
2574 r
= link_new_carrier_maps(link
);
2579 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
2580 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
2581 log_link_info(link
, "Renamed to %s", ifname
);
2583 link_free_carrier_maps(link
);
2585 r
= free_and_strdup(&link
->ifname
, ifname
);
2589 r
= link_new_carrier_maps(link
);
2594 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
2595 if (r
>= 0 && mtu
> 0) {
2597 if (!link
->original_mtu
) {
2598 link
->original_mtu
= mtu
;
2599 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
2602 if (link
->dhcp_client
) {
2603 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
2606 log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
2612 /* The kernel may broadcast NEWLINK messages without the MAC address
2613 set, simply ignore them. */
2614 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
2616 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
2619 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
2622 log_link_debug(link
, "MAC address: "
2623 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
2624 mac
.ether_addr_octet
[0],
2625 mac
.ether_addr_octet
[1],
2626 mac
.ether_addr_octet
[2],
2627 mac
.ether_addr_octet
[3],
2628 mac
.ether_addr_octet
[4],
2629 mac
.ether_addr_octet
[5]);
2632 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
2634 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
2637 if (link
->dhcp_client
) {
2638 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
2639 (const uint8_t *) &link
->mac
,
2643 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
2646 if (link
->dhcp6_client
) {
2647 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
2648 (const uint8_t *) &link
->mac
,
2652 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
2657 had_carrier
= link_has_carrier(link
);
2659 r
= link_update_flags(link
, m
);
2663 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
2664 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
2666 if (carrier_gained
) {
2667 log_link_info(link
, "Gained carrier");
2669 r
= link_carrier_gained(link
);
2672 } else if (carrier_lost
) {
2673 log_link_info(link
, "Lost carrier");
2675 r
= link_carrier_lost(link
);
2684 int link_save(Link
*link
) {
2685 _cleanup_free_
char *temp_path
= NULL
;
2686 _cleanup_fclose_
FILE *f
= NULL
;
2687 const char *admin_state
, *oper_state
;
2694 assert(link
->state_file
);
2695 assert(link
->lease_file
);
2696 assert(link
->manager
);
2698 if (link
->state
== LINK_STATE_LINGER
) {
2699 unlink(link
->state_file
);
2703 admin_state
= link_state_to_string(link
->state
);
2704 assert(admin_state
);
2706 oper_state
= link_operstate_to_string(link
->operstate
);
2709 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
2713 fchmod(fileno(f
), 0644);
2716 "# This is private data. Do not parse.\n"
2719 admin_state
, oper_state
);
2721 if (link
->network
) {
2722 char **address
, **domain
;
2724 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
2726 if (link
->dhcp6_client
) {
2727 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
, &dhcp6_lease
);
2728 if (r
< 0 && r
!= -ENOMSG
)
2729 log_link_debug(link
, "No DHCPv6 lease");
2732 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
2736 STRV_FOREACH(address
, link
->network
->dns
) {
2743 if (link
->network
->dhcp_dns
&&
2745 const struct in_addr
*addresses
;
2747 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
2751 serialize_in_addrs(f
, addresses
, r
);
2756 if (link
->network
->dhcp_dns
&& dhcp6_lease
) {
2757 struct in6_addr
*in6_addrs
;
2759 r
= sd_dhcp6_lease_get_dns(dhcp6_lease
, &in6_addrs
);
2763 serialize_in6_addrs(f
, in6_addrs
, r
);
2771 STRV_FOREACH(address
, link
->network
->ntp
) {
2778 if (link
->network
->dhcp_ntp
&&
2780 const struct in_addr
*addresses
;
2782 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
2786 serialize_in_addrs(f
, addresses
, r
);
2791 if (link
->network
->dhcp_ntp
&& dhcp6_lease
) {
2792 struct in6_addr
*in6_addrs
;
2796 r
= sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease
,
2801 serialize_in6_addrs(f
, in6_addrs
, r
);
2805 r
= sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease
, &hosts
);
2807 STRV_FOREACH(hostname
, hosts
) {
2810 fputs(*hostname
, f
);
2818 fputs("DOMAINS=", f
);
2820 STRV_FOREACH(domain
, link
->network
->domains
) {
2827 if (link
->network
->dhcp_domains
&&
2829 const char *domainname
;
2831 r
= sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &domainname
);
2835 fputs(domainname
, f
);
2840 if (link
->network
->dhcp_domains
&& dhcp6_lease
) {
2843 r
= sd_dhcp6_lease_get_domains(dhcp6_lease
, &domains
);
2845 STRV_FOREACH(domain
, domains
) {
2856 fprintf(f
, "WILDCARD_DOMAIN=%s\n",
2857 yes_no(link
->network
->wildcard_domain
));
2859 fprintf(f
, "LLMNR=%s\n",
2860 resolve_support_to_string(link
->network
->llmnr
));
2862 fputs("ADDRESSES=", f
);
2864 SET_FOREACH(a
, link
->addresses
, i
) {
2865 _cleanup_free_
char *address_str
= NULL
;
2867 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
2871 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
2877 fputs("ROUTES=", f
);
2879 SET_FOREACH(route
, link
->routes
, i
) {
2880 _cleanup_free_
char *route_str
= NULL
;
2882 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
2886 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%hhu/"USEC_FMT
, space
? " " : "", route_str
,
2887 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
2894 if (!hashmap_isempty(link
->bound_to_links
)) {
2898 fputs("CARRIER_BOUND_TO=", f
);
2899 HASHMAP_FOREACH(carrier
, link
->bound_to_links
, i
) {
2902 fputs(carrier
->ifname
, f
);
2909 if (!hashmap_isempty(link
->bound_by_links
)) {
2913 fputs("CARRIER_BOUND_BY=", f
);
2914 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
2917 fputs(carrier
->ifname
, f
);
2924 if (link
->dhcp_lease
) {
2925 struct in_addr address
;
2926 const char *tz
= NULL
;
2928 assert(link
->network
);
2930 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
2932 fprintf(f
, "TIMEZONE=%s\n", tz
);
2934 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
2936 fputs("DHCP4_ADDRESS=", f
);
2937 serialize_in_addrs(f
, &address
, 1);
2941 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
2949 unlink(link
->lease_file
);
2952 struct in_addr address
;
2954 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
2956 fputs("IPV4LL_ADDRESS=", f
);
2957 serialize_in_addrs(f
, &address
, 1);
2963 assert(link
->network
);
2965 r
= sd_lldp_save(link
->lldp
, link
->lldp_file
);
2973 unlink(link
->lldp_file
);
2975 r
= fflush_and_check(f
);
2979 if (rename(temp_path
, link
->state_file
) < 0) {
2987 (void) unlink(link
->state_file
);
2989 (void) unlink(temp_path
);
2991 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
2994 /* The serialized state in /run is no longer up-to-date. */
2995 void link_dirty(Link
*link
) {
3000 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
3002 /* allocation errors are ignored */
3005 r
= set_put(link
->manager
->dirty_links
, link
);
3007 /* allocation errors are ignored */
3013 /* The serialized state in /run is up-to-date */
3014 void link_clean(Link
*link
) {
3016 assert(link
->manager
);
3018 set_remove(link
->manager
->dirty_links
, link
);
3022 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
3023 [LINK_STATE_PENDING
] = "pending",
3024 [LINK_STATE_ENSLAVING
] = "configuring",
3025 [LINK_STATE_SETTING_ADDRESSES
] = "configuring",
3026 [LINK_STATE_SETTING_ROUTES
] = "configuring",
3027 [LINK_STATE_CONFIGURED
] = "configured",
3028 [LINK_STATE_UNMANAGED
] = "unmanaged",
3029 [LINK_STATE_FAILED
] = "failed",
3030 [LINK_STATE_LINGER
] = "linger",
3033 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);
3035 static const char* const link_operstate_table
[_LINK_OPERSTATE_MAX
] = {
3036 [LINK_OPERSTATE_OFF
] = "off",
3037 [LINK_OPERSTATE_NO_CARRIER
] = "no-carrier",
3038 [LINK_OPERSTATE_DORMANT
] = "dormant",
3039 [LINK_OPERSTATE_CARRIER
] = "carrier",
3040 [LINK_OPERSTATE_DEGRADED
] = "degraded",
3041 [LINK_OPERSTATE_ROUTABLE
] = "routable",
3044 DEFINE_STRING_TABLE_LOOKUP(link_operstate
, LinkOperationalState
);