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 (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) > 0)
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
);
1263 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1265 log_link_debug(link
, "Acquiring DHCPv6 lease");
1267 r
= sd_dhcp6_client_set_local_address(link
->dhcp6_client
, &link
->ipv6ll_address
);
1268 if (r
< 0 && r
!= -EBUSY
)
1269 return log_link_warning_errno(link
, r
, "Could not set IPv6LL address in DHCP client: %m");
1271 r
= sd_dhcp6_client_start(link
->dhcp6_client
);
1272 if (r
< 0 && r
!= -EBUSY
)
1273 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv6 lease: %m");
1276 if (link_ipv6_accept_ra_enabled(link
)) {
1277 assert(link
->ndisc_router_discovery
);
1279 log_link_debug(link
, "Discovering IPv6 routers");
1281 r
= sd_ndisc_router_discovery_start(link
->ndisc_router_discovery
);
1282 if (r
< 0 && r
!= -EBUSY
)
1283 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1289 static int link_acquire_conf(Link
*link
) {
1293 assert(link
->network
);
1294 assert(link
->manager
);
1295 assert(link
->manager
->event
);
1297 if (link_ipv4ll_enabled(link
)) {
1298 assert(link
->ipv4ll
);
1300 log_link_debug(link
, "Acquiring IPv4 link-local address");
1302 r
= sd_ipv4ll_start(link
->ipv4ll
);
1304 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1307 if (link_dhcp4_enabled(link
)) {
1308 assert(link
->dhcp_client
);
1310 log_link_debug(link
, "Acquiring DHCPv4 lease");
1312 r
= sd_dhcp_client_start(link
->dhcp_client
);
1314 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1317 if (link_lldp_enabled(link
)) {
1320 log_link_debug(link
, "Starting LLDP");
1322 r
= sd_lldp_start(link
->lldp
);
1324 return log_link_warning_errno(link
, r
, "Could not start LLDP: %m");
1330 bool link_has_carrier(Link
*link
) {
1331 /* see Documentation/networking/operstates.txt in the kernel sources */
1333 if (link
->kernel_operstate
== IF_OPER_UP
)
1336 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1337 /* operstate may not be implemented, so fall back to flags */
1338 if ((link
->flags
& IFF_LOWER_UP
) && !(link
->flags
& IFF_DORMANT
))
1344 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1345 _cleanup_link_unref_ Link
*link
= userdata
;
1350 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1353 r
= sd_netlink_message_get_errno(m
);
1355 /* we warn but don't fail the link, as it may be
1357 log_link_warning_errno(link
, r
, "Could not bring up interface: %m");
1362 static int link_up(Link
*link
) {
1363 _cleanup_netlink_message_unref_ sd_netlink_message
*req
= NULL
;
1364 uint8_t ipv6ll_mode
;
1368 assert(link
->network
);
1369 assert(link
->manager
);
1370 assert(link
->manager
->rtnl
);
1372 log_link_debug(link
, "Bringing link up");
1374 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1376 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1378 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1380 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1382 if (link
->network
->mac
) {
1383 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
1385 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
1388 if (link
->network
->mtu
) {
1389 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, link
->network
->mtu
);
1391 return log_link_error_errno(link
, r
, "Could not set MTU: %m");
1394 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1396 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1398 if (socket_ipv6_is_supported()) {
1399 /* if the kernel lacks ipv6 support setting IFF_UP fails if any ipv6 options are passed */
1400 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1402 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1404 ipv6ll_mode
= link_ipv6ll_enabled(link
) ? IN6_ADDR_GEN_MODE_EUI64
: IN6_ADDR_GEN_MODE_NONE
;
1405 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1407 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1409 if (!in_addr_is_null(AF_INET6
, &link
->network
->ipv6_token
)) {
1410 r
= sd_netlink_message_append_in6_addr(req
, IFLA_INET6_TOKEN
, &link
->network
->ipv6_token
.in6
);
1412 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_TOKEN: %m");
1415 r
= sd_netlink_message_close_container(req
);
1417 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1420 r
= sd_netlink_message_close_container(req
);
1422 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1424 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_up_handler
, link
, 0, NULL
);
1426 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1433 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1434 _cleanup_link_unref_ Link
*link
= userdata
;
1439 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1442 r
= sd_netlink_message_get_errno(m
);
1444 log_link_warning_errno(link
, r
, "Could not bring down interface: %m");
1449 static int link_down(Link
*link
) {
1450 _cleanup_netlink_message_unref_ sd_netlink_message
*req
= NULL
;
1454 assert(link
->manager
);
1455 assert(link
->manager
->rtnl
);
1457 log_link_debug(link
, "Bringing link down");
1459 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
1460 RTM_SETLINK
, link
->ifindex
);
1462 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1464 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
1466 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1468 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_down_handler
, link
, 0, NULL
);
1470 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1477 static int link_handle_bound_to_list(Link
*link
) {
1481 bool required_up
= false;
1482 bool link_is_up
= false;
1486 if (hashmap_isempty(link
->bound_to_links
))
1489 if (link
->flags
& IFF_UP
)
1492 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
1493 if (link_has_carrier(l
)) {
1498 if (!required_up
&& link_is_up
) {
1499 r
= link_down(link
);
1502 } else if (required_up
&& !link_is_up
) {
1511 static int link_handle_bound_by_list(Link
*link
) {
1518 if (hashmap_isempty(link
->bound_by_links
))
1521 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
1522 r
= link_handle_bound_to_list(l
);
1530 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
1536 if (link
== carrier
)
1539 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
1542 r
= hashmap_ensure_allocated(h
, NULL
);
1546 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
1553 static int link_new_bound_by_list(Link
*link
) {
1558 bool list_updated
= false;
1561 assert(link
->manager
);
1565 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
1566 if (!carrier
->network
)
1569 if (strv_isempty(carrier
->network
->bind_carrier
))
1572 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
, 0)) {
1573 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
1577 list_updated
= true;
1584 HASHMAP_FOREACH (carrier
, link
->bound_by_links
, i
) {
1585 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
1589 link_dirty(carrier
);
1595 static int link_new_bound_to_list(Link
*link
) {
1600 bool list_updated
= false;
1603 assert(link
->manager
);
1608 if (strv_isempty(link
->network
->bind_carrier
))
1613 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
1614 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
, 0)) {
1615 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
1619 list_updated
= true;
1626 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
1627 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
1631 link_dirty(carrier
);
1637 static int link_new_carrier_maps(Link
*link
) {
1640 r
= link_new_bound_by_list(link
);
1644 r
= link_handle_bound_by_list(link
);
1648 r
= link_new_bound_to_list(link
);
1652 r
= link_handle_bound_to_list(link
);
1659 static void link_free_bound_to_list(Link
*link
) {
1663 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
1664 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
1666 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
1667 link_dirty(bound_to
);
1673 static void link_free_bound_by_list(Link
*link
) {
1677 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
1678 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
1680 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
1681 link_dirty(bound_by
);
1682 link_handle_bound_to_list(bound_by
);
1689 static void link_free_carrier_maps(Link
*link
) {
1690 bool list_updated
= false;
1694 if (!hashmap_isempty(link
->bound_to_links
)) {
1695 link_free_bound_to_list(link
);
1696 list_updated
= true;
1699 if (!hashmap_isempty(link
->bound_by_links
)) {
1700 link_free_bound_by_list(link
);
1701 list_updated
= true;
1710 void link_drop(Link
*link
) {
1711 if (!link
|| link
->state
== LINK_STATE_LINGER
)
1714 link_set_state(link
, LINK_STATE_LINGER
);
1716 link_free_carrier_maps(link
);
1718 log_link_debug(link
, "Link removed");
1720 (void)unlink(link
->state_file
);
1726 static int link_joined(Link
*link
) {
1730 assert(link
->network
);
1732 if (!hashmap_isempty(link
->bound_to_links
)) {
1733 r
= link_handle_bound_to_list(link
);
1736 } else if (!(link
->flags
& IFF_UP
)) {
1739 link_enter_failed(link
);
1744 if(link
->network
->bridge
) {
1745 r
= link_set_bridge(link
);
1747 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
1750 return link_enter_set_addresses(link
);
1753 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1754 _cleanup_link_unref_ Link
*link
= userdata
;
1758 assert(link
->network
);
1762 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1765 r
= sd_netlink_message_get_errno(m
);
1766 if (r
< 0 && r
!= -EEXIST
) {
1767 log_link_error_errno(link
, r
, "Could not join netdev: %m");
1768 link_enter_failed(link
);
1771 log_link_debug(link
, "Joined netdev");
1773 if (link
->enslaving
<= 0)
1779 static int link_enter_join_netdev(Link
*link
) {
1785 assert(link
->network
);
1786 assert(link
->state
== LINK_STATE_PENDING
);
1788 link_set_state(link
, LINK_STATE_ENSLAVING
);
1792 if (!link
->network
->bridge
&&
1793 !link
->network
->bond
&&
1794 hashmap_isempty(link
->network
->stacked_netdevs
))
1795 return link_joined(link
);
1797 if (link
->network
->bond
) {
1798 log_struct(LOG_DEBUG
,
1799 LOG_LINK_INTERFACE(link
),
1800 LOG_NETDEV_INTERFACE(link
->network
->bond
),
1801 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
),
1804 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
1806 log_struct_errno(LOG_WARNING
, r
,
1807 LOG_LINK_INTERFACE(link
),
1808 LOG_NETDEV_INTERFACE(link
->network
->bond
),
1809 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
),
1812 link_enter_failed(link
);
1819 if (link
->network
->bridge
) {
1820 log_struct(LOG_DEBUG
,
1821 LOG_LINK_INTERFACE(link
),
1822 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
1823 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
),
1826 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
1828 log_struct_errno(LOG_WARNING
, r
,
1829 LOG_LINK_INTERFACE(link
),
1830 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
1831 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
),
1833 link_enter_failed(link
);
1840 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
1842 log_struct(LOG_DEBUG
,
1843 LOG_LINK_INTERFACE(link
),
1844 LOG_NETDEV_INTERFACE(netdev
),
1845 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
),
1848 r
= netdev_join(netdev
, link
, netdev_join_handler
);
1850 log_struct_errno(LOG_WARNING
, r
,
1851 LOG_LINK_INTERFACE(link
),
1852 LOG_NETDEV_INTERFACE(netdev
),
1853 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
),
1855 link_enter_failed(link
);
1865 static int link_set_ipv4_forward(Link
*link
) {
1868 if (!link_ipv4_forward_enabled(link
))
1871 /* We propagate the forwarding flag from one interface to the
1872 * global setting one way. This means: as long as at least one
1873 * interface was configured at any time that had IP forwarding
1874 * enabled the setting will stay on for good. We do this
1875 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
1876 * somewhat in sync (see below). */
1878 r
= write_string_file("/proc/sys/net/ipv4/ip_forward", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
1880 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
1885 static int link_set_ipv6_forward(Link
*link
) {
1888 if (!link_ipv6_forward_enabled(link
))
1891 /* On Linux, the IPv6 stack does not not know a per-interface
1892 * packet forwarding setting: either packet forwarding is on
1893 * for all, or off for all. We hence don't bother with a
1894 * per-interface setting, but simply propagate the interface
1895 * flag, if it is set, to the global flag, one-way. Note that
1896 * while IPv4 would allow a per-interface flag, we expose the
1897 * same behaviour there and also propagate the setting from
1898 * one to all, to keep things simple (see above). */
1900 r
= write_string_file("/proc/sys/net/ipv6/conf/all/forwarding", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
1902 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
1907 static int link_set_ipv6_privacy_extensions(Link
*link
) {
1908 char buf
[DECIMAL_STR_MAX(unsigned) + 1];
1909 IPv6PrivacyExtensions s
;
1910 const char *p
= NULL
;
1913 s
= link_ipv6_privacy_extensions(link
);
1917 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/use_tempaddr");
1918 xsprintf(buf
, "%u", (unsigned) link
->network
->ipv6_privacy_extensions
);
1920 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
1922 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
1927 static int link_set_ipv6_accept_ra(Link
*link
) {
1928 const char *p
= NULL
;
1931 /* Make this a NOP if IPv6 is not available */
1932 if (!socket_ipv6_is_supported())
1935 if (link
->flags
& IFF_LOOPBACK
)
1941 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/accept_ra");
1943 /* We handle router advertisments ourselves, tell the kernel to GTFO */
1944 r
= write_string_file(p
, "0", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
1946 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
1951 static int link_set_ipv6_dad_transmits(Link
*link
) {
1952 char buf
[DECIMAL_STR_MAX(int) + 1];
1953 const char *p
= NULL
;
1956 /* Make this a NOP if IPv6 is not available */
1957 if (!socket_ipv6_is_supported())
1960 if (link
->flags
& IFF_LOOPBACK
)
1966 if (link
->network
->ipv6_dad_transmits
< 0)
1969 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/dad_transmits");
1970 xsprintf(buf
, "%i", link
->network
->ipv6_dad_transmits
);
1972 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
1974 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
1979 static int link_set_ipv6_hop_limit(Link
*link
) {
1980 char buf
[DECIMAL_STR_MAX(int) + 1];
1981 const char *p
= NULL
;
1984 /* Make this a NOP if IPv6 is not available */
1985 if (!socket_ipv6_is_supported())
1988 if (link
->flags
& IFF_LOOPBACK
)
1994 if (link
->network
->ipv6_hop_limit
< 0)
1997 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/hop_limit");
1998 xsprintf(buf
, "%i", link
->network
->ipv6_hop_limit
);
2000 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2002 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2007 static int link_drop_foreign_config(Link
*link
) {
2013 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
2014 /* we consider IPv6LL addresses to be managed by the kernel */
2015 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2018 r
= address_remove(address
, link
, link_address_remove_handler
);
2023 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2024 /* do not touch routes managed by the kernel */
2025 if (route
->protocol
== RTPROT_KERNEL
)
2028 r
= route_remove(route
, link
, link_address_remove_handler
);
2036 static int link_configure(Link
*link
) {
2040 assert(link
->network
);
2041 assert(link
->state
== LINK_STATE_PENDING
);
2043 r
= link_drop_foreign_config(link
);
2047 r
= link_set_bridge_fdb(link
);
2051 r
= link_set_ipv4_forward(link
);
2055 r
= link_set_ipv6_forward(link
);
2059 r
= link_set_ipv6_privacy_extensions(link
);
2063 r
= link_set_ipv6_accept_ra(link
);
2067 r
= link_set_ipv6_dad_transmits(link
);
2071 r
= link_set_ipv6_hop_limit(link
);
2075 if (link_ipv4ll_enabled(link
)) {
2076 r
= ipv4ll_configure(link
);
2081 if (link_dhcp4_enabled(link
)) {
2082 r
= dhcp4_configure(link
);
2087 if (link_dhcp4_server_enabled(link
)) {
2088 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
2092 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
2097 if (link_dhcp6_enabled(link
) ||
2098 link_ipv6_accept_ra_enabled(link
)) {
2099 r
= dhcp6_configure(link
);
2104 if (link_ipv6_accept_ra_enabled(link
)) {
2105 r
= ndisc_configure(link
);
2110 if (link_lldp_enabled(link
)) {
2111 r
= sd_lldp_new(link
->ifindex
, link
->ifname
, &link
->mac
, &link
->lldp
);
2115 r
= sd_lldp_attach_event(link
->lldp
, NULL
, 0);
2119 r
= sd_lldp_set_callback(link
->lldp
,
2120 lldp_handler
, link
);
2125 if (link_has_carrier(link
)) {
2126 r
= link_acquire_conf(link
);
2130 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) == 0) {
2131 r
= link_acquire_ipv6_conf(link
);
2137 return link_enter_join_netdev(link
);
2140 static int link_initialized_and_synced(sd_netlink
*rtnl
, sd_netlink_message
*m
,
2142 _cleanup_link_unref_ Link
*link
= userdata
;
2147 assert(link
->ifname
);
2148 assert(link
->manager
);
2150 if (link
->state
!= LINK_STATE_PENDING
)
2153 log_link_debug(link
, "Link state is up-to-date");
2155 r
= link_new_bound_by_list(link
);
2159 r
= link_handle_bound_by_list(link
);
2163 if (!link
->network
) {
2164 r
= network_get(link
->manager
, link
->udev_device
, link
->ifname
,
2165 &link
->mac
, &network
);
2167 link_enter_unmanaged(link
);
2172 if (link
->flags
& IFF_LOOPBACK
) {
2173 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
2174 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
2176 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
2177 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
2179 if (network
->dhcp_server
)
2180 log_link_debug(link
, "Ignoring DHCP server for loopback link");
2183 r
= network_apply(link
->manager
, network
, link
);
2188 r
= link_new_bound_to_list(link
);
2192 r
= link_configure(link
);
2199 int link_initialized(Link
*link
, struct udev_device
*device
) {
2200 _cleanup_netlink_message_unref_ sd_netlink_message
*req
= NULL
;
2204 assert(link
->manager
);
2205 assert(link
->manager
->rtnl
);
2208 if (link
->state
!= LINK_STATE_PENDING
)
2211 if (link
->udev_device
)
2214 log_link_debug(link
, "udev initialized link");
2216 link
->udev_device
= udev_device_ref(device
);
2218 /* udev has initialized the link, but we don't know if we have yet
2219 * processed the NEWLINK messages with the latest state. Do a GETLINK,
2220 * when it returns we know that the pending NEWLINKs have already been
2221 * processed and that we are up-to-date */
2223 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
2228 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
,
2229 link_initialized_and_synced
, link
, 0, NULL
);
2238 static int link_load(Link
*link
) {
2239 _cleanup_free_
char *network_file
= NULL
,
2242 *dhcp4_address
= NULL
,
2243 *ipv4ll_address
= NULL
;
2244 union in_addr_union address
;
2245 union in_addr_union route_dst
;
2251 r
= parse_env_file(link
->state_file
, NEWLINE
,
2252 "NETWORK_FILE", &network_file
,
2253 "ADDRESSES", &addresses
,
2255 "DHCP4_ADDRESS", &dhcp4_address
,
2256 "IPV4LL_ADDRESS", &ipv4ll_address
,
2258 if (r
< 0 && r
!= -ENOENT
)
2259 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
2266 suffix
= strrchr(network_file
, '.');
2268 log_link_debug(link
, "Failed to get network name from %s", network_file
);
2269 goto network_file_fail
;
2273 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
2275 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
2276 goto network_file_fail
;
2279 r
= network_apply(link
->manager
, network
, link
);
2281 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
2290 _cleanup_free_
char *address_str
= NULL
;
2291 char *prefixlen_str
;
2293 unsigned char prefixlen
;
2295 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
2297 log_link_debug_errno(link
, r
, "Failed to extract next address string: %m");
2303 prefixlen_str
= strchr(address_str
, '/');
2304 if (!prefixlen_str
) {
2305 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
2309 *prefixlen_str
++ = '\0';
2311 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
2313 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
2317 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
2319 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
2323 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
2325 return log_link_error_errno(link
, r
, "Failed to add address: %m");
2332 _cleanup_free_
char *route_str
= NULL
;
2333 _cleanup_event_source_unref_ sd_event_source
*expire
= NULL
;
2335 char *prefixlen_str
;
2337 unsigned char prefixlen
, tos
, table
;
2340 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
2342 log_link_debug_errno(link
, r
, "Failed to extract next route string: %m");
2348 prefixlen_str
= strchr(route_str
, '/');
2349 if (!prefixlen_str
) {
2350 log_link_debug(link
, "Failed to parse route %s", route_str
);
2354 *prefixlen_str
++ = '\0';
2356 r
= sscanf(prefixlen_str
, "%hhu/%hhu/%"SCNu32
"/%hhu/"USEC_FMT
, &prefixlen
, &tos
, &priority
, &table
, &lifetime
);
2358 log_link_debug(link
,
2359 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
2364 r
= in_addr_from_string_auto(route_str
, &family
, &route_dst
);
2366 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
2370 r
= route_add(link
, family
, &route_dst
, prefixlen
, tos
, priority
, table
, &route
);
2372 return log_link_error_errno(link
, r
, "Failed to add route: %m");
2374 if (lifetime
!= USEC_INFINITY
) {
2375 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(), lifetime
,
2376 0, route_expire_handler
, route
);
2378 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
2381 route
->lifetime
= lifetime
;
2382 sd_event_source_unref(route
->expire
);
2383 route
->expire
= expire
;
2388 if (dhcp4_address
) {
2389 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
2391 log_link_debug_errno(link
, r
, "Falied to parse DHCPv4 address %s: %m", dhcp4_address
);
2392 goto dhcp4_address_fail
;
2395 r
= sd_dhcp_client_new(&link
->dhcp_client
);
2397 return log_link_error_errno(link
, r
, "Falied to create DHCPv4 client: %m");
2399 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
2401 return log_link_error_errno(link
, r
, "Falied to set inital DHCPv4 address %s: %m", dhcp4_address
);
2406 if (ipv4ll_address
) {
2407 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
2409 log_link_debug_errno(link
, r
, "Falied to parse IPv4LL address %s: %m", ipv4ll_address
);
2410 goto ipv4ll_address_fail
;
2413 r
= sd_ipv4ll_new(&link
->ipv4ll
);
2415 return log_link_error_errno(link
, r
, "Falied to create IPv4LL client: %m");
2417 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
2419 return log_link_error_errno(link
, r
, "Falied to set inital IPv4LL address %s: %m", ipv4ll_address
);
2422 ipv4ll_address_fail
:
2427 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
2429 _cleanup_udev_device_unref_
struct udev_device
*device
= NULL
;
2430 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
2438 r
= link_new(m
, message
, ret
);
2444 log_link_debug(link
, "Link %d added", link
->ifindex
);
2446 r
= link_load(link
);
2450 if (detect_container() <= 0) {
2451 /* not in a container, udev will be around */
2452 sprintf(ifindex_str
, "n%d", link
->ifindex
);
2453 device
= udev_device_new_from_device_id(m
->udev
, ifindex_str
);
2455 r
= log_link_warning_errno(link
, errno
, "Could not find udev device: %m");
2459 if (udev_device_get_is_initialized(device
) <= 0) {
2461 log_link_debug(link
, "link pending udev initialization...");
2465 r
= link_initialized(link
, device
);
2469 /* we are calling a callback directly, so must take a ref */
2472 r
= link_initialized_and_synced(m
->rtnl
, NULL
, link
);
2479 link_enter_failed(link
);
2483 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
2488 log_link_info(link
, "Gained IPv6LL");
2490 link
->ipv6ll_address
= *address
;
2491 link_check_ready(link
);
2493 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
2494 r
= link_acquire_ipv6_conf(link
);
2496 link_enter_failed(link
);
2504 static int link_carrier_gained(Link
*link
) {
2509 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
2510 r
= link_acquire_conf(link
);
2512 link_enter_failed(link
);
2517 r
= link_handle_bound_by_list(link
);
2524 static int link_carrier_lost(Link
*link
) {
2529 r
= link_stop_clients(link
);
2531 link_enter_failed(link
);
2535 r
= link_handle_bound_by_list(link
);
2542 int link_carrier_reset(Link
*link
) {
2547 if (link_has_carrier(link
)) {
2548 r
= link_carrier_lost(link
);
2552 r
= link_carrier_gained(link
);
2556 log_link_info(link
, "Reset carrier");
2563 int link_update(Link
*link
, sd_netlink_message
*m
) {
2564 struct ether_addr mac
;
2567 bool had_carrier
, carrier_gained
, carrier_lost
;
2571 assert(link
->ifname
);
2574 if (link
->state
== LINK_STATE_LINGER
) {
2576 log_link_info(link
, "Link readded");
2577 link_set_state(link
, LINK_STATE_ENSLAVING
);
2579 r
= link_new_carrier_maps(link
);
2584 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
2585 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
2586 log_link_info(link
, "Renamed to %s", ifname
);
2588 link_free_carrier_maps(link
);
2590 r
= free_and_strdup(&link
->ifname
, ifname
);
2594 r
= link_new_carrier_maps(link
);
2599 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
2600 if (r
>= 0 && mtu
> 0) {
2602 if (!link
->original_mtu
) {
2603 link
->original_mtu
= mtu
;
2604 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
2607 if (link
->dhcp_client
) {
2608 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
2611 log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
2617 /* The kernel may broadcast NEWLINK messages without the MAC address
2618 set, simply ignore them. */
2619 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
2621 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
2624 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
2627 log_link_debug(link
, "MAC address: "
2628 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
2629 mac
.ether_addr_octet
[0],
2630 mac
.ether_addr_octet
[1],
2631 mac
.ether_addr_octet
[2],
2632 mac
.ether_addr_octet
[3],
2633 mac
.ether_addr_octet
[4],
2634 mac
.ether_addr_octet
[5]);
2637 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
2639 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
2642 if (link
->dhcp_client
) {
2643 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
2644 (const uint8_t *) &link
->mac
,
2648 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
2651 if (link
->dhcp6_client
) {
2652 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
2653 (const uint8_t *) &link
->mac
,
2657 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
2662 had_carrier
= link_has_carrier(link
);
2664 r
= link_update_flags(link
, m
);
2668 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
2669 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
2671 if (carrier_gained
) {
2672 log_link_info(link
, "Gained carrier");
2674 r
= link_carrier_gained(link
);
2677 } else if (carrier_lost
) {
2678 log_link_info(link
, "Lost carrier");
2680 r
= link_carrier_lost(link
);
2689 int link_save(Link
*link
) {
2690 _cleanup_free_
char *temp_path
= NULL
;
2691 _cleanup_fclose_
FILE *f
= NULL
;
2692 const char *admin_state
, *oper_state
;
2699 assert(link
->state_file
);
2700 assert(link
->lease_file
);
2701 assert(link
->manager
);
2703 if (link
->state
== LINK_STATE_LINGER
) {
2704 unlink(link
->state_file
);
2708 admin_state
= link_state_to_string(link
->state
);
2709 assert(admin_state
);
2711 oper_state
= link_operstate_to_string(link
->operstate
);
2714 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
2718 fchmod(fileno(f
), 0644);
2721 "# This is private data. Do not parse.\n"
2724 admin_state
, oper_state
);
2726 if (link
->network
) {
2727 char **address
, **domain
;
2729 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
2731 if (link
->dhcp6_client
) {
2732 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
, &dhcp6_lease
);
2733 if (r
< 0 && r
!= -ENOMSG
)
2734 log_link_debug(link
, "No DHCPv6 lease");
2737 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
2741 STRV_FOREACH(address
, link
->network
->dns
) {
2748 if (link
->network
->dhcp_dns
&&
2750 const struct in_addr
*addresses
;
2752 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
2756 serialize_in_addrs(f
, addresses
, r
);
2761 if (link
->network
->dhcp_dns
&& dhcp6_lease
) {
2762 struct in6_addr
*in6_addrs
;
2764 r
= sd_dhcp6_lease_get_dns(dhcp6_lease
, &in6_addrs
);
2768 serialize_in6_addrs(f
, in6_addrs
, r
);
2776 STRV_FOREACH(address
, link
->network
->ntp
) {
2783 if (link
->network
->dhcp_ntp
&&
2785 const struct in_addr
*addresses
;
2787 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
2791 serialize_in_addrs(f
, addresses
, r
);
2796 if (link
->network
->dhcp_ntp
&& dhcp6_lease
) {
2797 struct in6_addr
*in6_addrs
;
2801 r
= sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease
,
2806 serialize_in6_addrs(f
, in6_addrs
, r
);
2810 r
= sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease
, &hosts
);
2812 STRV_FOREACH(hostname
, hosts
) {
2815 fputs(*hostname
, f
);
2823 fputs("DOMAINS=", f
);
2825 STRV_FOREACH(domain
, link
->network
->domains
) {
2832 if (link
->network
->dhcp_domains
&&
2834 const char *domainname
;
2836 r
= sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &domainname
);
2840 fputs(domainname
, f
);
2845 if (link
->network
->dhcp_domains
&& dhcp6_lease
) {
2848 r
= sd_dhcp6_lease_get_domains(dhcp6_lease
, &domains
);
2850 STRV_FOREACH(domain
, domains
) {
2861 fprintf(f
, "WILDCARD_DOMAIN=%s\n",
2862 yes_no(link
->network
->wildcard_domain
));
2864 fprintf(f
, "LLMNR=%s\n",
2865 resolve_support_to_string(link
->network
->llmnr
));
2867 fputs("ADDRESSES=", f
);
2869 SET_FOREACH(a
, link
->addresses
, i
) {
2870 _cleanup_free_
char *address_str
= NULL
;
2872 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
2876 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
2882 fputs("ROUTES=", f
);
2884 SET_FOREACH(route
, link
->routes
, i
) {
2885 _cleanup_free_
char *route_str
= NULL
;
2887 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
2891 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%hhu/"USEC_FMT
, space
? " " : "", route_str
,
2892 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
2899 if (!hashmap_isempty(link
->bound_to_links
)) {
2903 fputs("CARRIER_BOUND_TO=", f
);
2904 HASHMAP_FOREACH(carrier
, link
->bound_to_links
, i
) {
2907 fputs(carrier
->ifname
, f
);
2914 if (!hashmap_isempty(link
->bound_by_links
)) {
2918 fputs("CARRIER_BOUND_BY=", f
);
2919 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
2922 fputs(carrier
->ifname
, f
);
2929 if (link
->dhcp_lease
) {
2930 struct in_addr address
;
2931 const char *tz
= NULL
;
2933 assert(link
->network
);
2935 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
2937 fprintf(f
, "TIMEZONE=%s\n", tz
);
2939 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
2941 fputs("DHCP4_ADDRESS=", f
);
2942 serialize_in_addrs(f
, &address
, 1);
2946 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
2954 unlink(link
->lease_file
);
2957 struct in_addr address
;
2959 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
2961 fputs("IPV4LL_ADDRESS=", f
);
2962 serialize_in_addrs(f
, &address
, 1);
2968 assert(link
->network
);
2970 r
= sd_lldp_save(link
->lldp
, link
->lldp_file
);
2978 unlink(link
->lldp_file
);
2980 r
= fflush_and_check(f
);
2984 if (rename(temp_path
, link
->state_file
) < 0) {
2992 (void) unlink(link
->state_file
);
2994 (void) unlink(temp_path
);
2996 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
2999 /* The serialized state in /run is no longer up-to-date. */
3000 void link_dirty(Link
*link
) {
3005 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
3007 /* allocation errors are ignored */
3010 r
= set_put(link
->manager
->dirty_links
, link
);
3012 /* allocation errors are ignored */
3018 /* The serialized state in /run is up-to-date */
3019 void link_clean(Link
*link
) {
3021 assert(link
->manager
);
3023 set_remove(link
->manager
->dirty_links
, link
);
3027 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
3028 [LINK_STATE_PENDING
] = "pending",
3029 [LINK_STATE_ENSLAVING
] = "configuring",
3030 [LINK_STATE_SETTING_ADDRESSES
] = "configuring",
3031 [LINK_STATE_SETTING_ROUTES
] = "configuring",
3032 [LINK_STATE_CONFIGURED
] = "configured",
3033 [LINK_STATE_UNMANAGED
] = "unmanaged",
3034 [LINK_STATE_FAILED
] = "failed",
3035 [LINK_STATE_LINGER
] = "linger",
3038 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);
3040 static const char* const link_operstate_table
[_LINK_OPERSTATE_MAX
] = {
3041 [LINK_OPERSTATE_OFF
] = "off",
3042 [LINK_OPERSTATE_NO_CARRIER
] = "no-carrier",
3043 [LINK_OPERSTATE_DORMANT
] = "dormant",
3044 [LINK_OPERSTATE_CARRIER
] = "carrier",
3045 [LINK_OPERSTATE_DEGRADED
] = "degraded",
3046 [LINK_OPERSTATE_ROUTABLE
] = "routable",
3049 DEFINE_STRING_TABLE_LOOKUP(link_operstate
, LinkOperationalState
);