2 This file is part of systemd.
4 Copyright 2013 Tom Gundersen <teg@jklm.no>
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 #include <netinet/ether.h>
24 #include "alloc-util.h"
26 #include "dhcp-lease-internal.h"
29 #include "netlink-util.h"
30 #include "network-internal.h"
31 #include "networkd-link.h"
32 #include "networkd-netdev.h"
34 #include "socket-util.h"
35 #include "stdio-util.h"
36 #include "string-table.h"
37 #include "udev-util.h"
41 bool link_dhcp6_enabled(Link
*link
) {
42 if (link
->flags
& IFF_LOOPBACK
)
48 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV6
;
51 bool link_dhcp4_enabled(Link
*link
) {
52 if (link
->flags
& IFF_LOOPBACK
)
58 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV4
;
61 bool link_dhcp4_server_enabled(Link
*link
) {
62 if (link
->flags
& IFF_LOOPBACK
)
68 return link
->network
->dhcp_server
;
71 bool link_ipv4ll_enabled(Link
*link
) {
72 if (link
->flags
& IFF_LOOPBACK
)
78 return link
->network
->link_local
& ADDRESS_FAMILY_IPV4
;
81 bool link_ipv6ll_enabled(Link
*link
) {
82 if (link
->flags
& IFF_LOOPBACK
)
88 return link
->network
->link_local
& ADDRESS_FAMILY_IPV6
;
91 bool link_lldp_enabled(Link
*link
) {
92 if (link
->flags
& IFF_LOOPBACK
)
98 if (link
->network
->bridge
)
101 return link
->network
->lldp
;
104 static bool link_ipv4_forward_enabled(Link
*link
) {
105 if (link
->flags
& IFF_LOOPBACK
)
111 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
114 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV4
;
117 static bool link_ipv6_forward_enabled(Link
*link
) {
119 if (!socket_ipv6_is_supported())
122 if (link
->flags
& IFF_LOOPBACK
)
128 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
131 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV6
;
134 bool link_ipv6_accept_ra_enabled(Link
*link
) {
135 if (link
->flags
& IFF_LOOPBACK
)
141 /* If unset use system default (enabled if local forwarding is disabled.
142 * disabled if local forwarding is enabled).
143 * If set, ignore or enforce RA independent of local forwarding state.
145 if (link
->network
->ipv6_accept_ra
< 0)
146 /* default to accept RA if ip_forward is disabled and ignore RA if ip_forward is enabled */
147 return !link_ipv6_forward_enabled(link
);
148 else if (link
->network
->ipv6_accept_ra
> 0)
149 /* accept RA even if ip_forward is enabled */
156 static IPv6PrivacyExtensions
link_ipv6_privacy_extensions(Link
*link
) {
158 if (!socket_ipv6_is_supported())
159 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
161 if (link
->flags
& IFF_LOOPBACK
)
162 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
165 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
167 return link
->network
->ipv6_privacy_extensions
;
170 void link_update_operstate(Link
*link
) {
171 LinkOperationalState operstate
;
174 if (link
->kernel_operstate
== IF_OPER_DORMANT
)
175 operstate
= LINK_OPERSTATE_DORMANT
;
176 else if (link_has_carrier(link
)) {
178 uint8_t scope
= RT_SCOPE_NOWHERE
;
181 /* if we have carrier, check what addresses we have */
182 SET_FOREACH(address
, link
->addresses
, i
) {
183 if (!address_is_ready(address
))
186 if (address
->scope
< scope
)
187 scope
= address
->scope
;
190 /* for operstate we also take foreign addresses into account */
191 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
192 if (!address_is_ready(address
))
195 if (address
->scope
< scope
)
196 scope
= address
->scope
;
199 if (scope
< RT_SCOPE_SITE
)
200 /* universally accessible addresses found */
201 operstate
= LINK_OPERSTATE_ROUTABLE
;
202 else if (scope
< RT_SCOPE_HOST
)
203 /* only link or site local addresses found */
204 operstate
= LINK_OPERSTATE_DEGRADED
;
206 /* no useful addresses found */
207 operstate
= LINK_OPERSTATE_CARRIER
;
208 } else if (link
->flags
& IFF_UP
)
209 operstate
= LINK_OPERSTATE_NO_CARRIER
;
211 operstate
= LINK_OPERSTATE_OFF
;
213 if (link
->operstate
!= operstate
) {
214 link
->operstate
= operstate
;
215 link_send_changed(link
, "OperationalState", NULL
);
217 manager_dirty(link
->manager
);
221 #define FLAG_STRING(string, flag, old, new) \
222 (((old ^ new) & flag) \
223 ? ((old & flag) ? (" -" string) : (" +" string)) \
226 static int link_update_flags(Link
*link
, sd_netlink_message
*m
) {
227 unsigned flags
, unknown_flags_added
, unknown_flags_removed
, unknown_flags
;
233 r
= sd_rtnl_message_link_get_flags(m
, &flags
);
235 return log_link_warning_errno(link
, r
, "Could not get link flags: %m");
237 r
= sd_netlink_message_read_u8(m
, IFLA_OPERSTATE
, &operstate
);
239 /* if we got a message without operstate, take it to mean
240 the state was unchanged */
241 operstate
= link
->kernel_operstate
;
243 if ((link
->flags
== flags
) && (link
->kernel_operstate
== operstate
))
246 if (link
->flags
!= flags
) {
247 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",
248 FLAG_STRING("LOOPBACK", IFF_LOOPBACK
, link
->flags
, flags
),
249 FLAG_STRING("MASTER", IFF_MASTER
, link
->flags
, flags
),
250 FLAG_STRING("SLAVE", IFF_SLAVE
, link
->flags
, flags
),
251 FLAG_STRING("UP", IFF_UP
, link
->flags
, flags
),
252 FLAG_STRING("DORMANT", IFF_DORMANT
, link
->flags
, flags
),
253 FLAG_STRING("LOWER_UP", IFF_LOWER_UP
, link
->flags
, flags
),
254 FLAG_STRING("RUNNING", IFF_RUNNING
, link
->flags
, flags
),
255 FLAG_STRING("MULTICAST", IFF_MULTICAST
, link
->flags
, flags
),
256 FLAG_STRING("BROADCAST", IFF_BROADCAST
, link
->flags
, flags
),
257 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT
, link
->flags
, flags
),
258 FLAG_STRING("PROMISC", IFF_PROMISC
, link
->flags
, flags
),
259 FLAG_STRING("ALLMULTI", IFF_ALLMULTI
, link
->flags
, flags
),
260 FLAG_STRING("PORTSEL", IFF_PORTSEL
, link
->flags
, flags
),
261 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA
, link
->flags
, flags
),
262 FLAG_STRING("DYNAMIC", IFF_DYNAMIC
, link
->flags
, flags
),
263 FLAG_STRING("NOARP", IFF_NOARP
, link
->flags
, flags
),
264 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS
, link
->flags
, flags
),
265 FLAG_STRING("DEBUG", IFF_DEBUG
, link
->flags
, flags
),
266 FLAG_STRING("ECHO", IFF_ECHO
, link
->flags
, flags
));
268 unknown_flags
= ~(IFF_LOOPBACK
| IFF_MASTER
| IFF_SLAVE
| IFF_UP
|
269 IFF_DORMANT
| IFF_LOWER_UP
| IFF_RUNNING
|
270 IFF_MULTICAST
| IFF_BROADCAST
| IFF_POINTOPOINT
|
271 IFF_PROMISC
| IFF_ALLMULTI
| IFF_PORTSEL
|
272 IFF_AUTOMEDIA
| IFF_DYNAMIC
| IFF_NOARP
|
273 IFF_NOTRAILERS
| IFF_DEBUG
| IFF_ECHO
);
274 unknown_flags_added
= ((link
->flags
^ flags
) & flags
& unknown_flags
);
275 unknown_flags_removed
= ((link
->flags
^ flags
) & link
->flags
& unknown_flags
);
277 /* link flags are currently at most 18 bits, let's align to
279 if (unknown_flags_added
)
281 "Unknown link flags gained: %#.5x (ignoring)",
282 unknown_flags_added
);
284 if (unknown_flags_removed
)
286 "Unknown link flags lost: %#.5x (ignoring)",
287 unknown_flags_removed
);
291 link
->kernel_operstate
= operstate
;
293 link_update_operstate(link
);
298 static int link_new(Manager
*manager
, sd_netlink_message
*message
, Link
**ret
) {
299 _cleanup_link_unref_ Link
*link
= NULL
;
308 r
= sd_netlink_message_get_type(message
, &type
);
311 else if (type
!= RTM_NEWLINK
)
314 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
317 else if (ifindex
<= 0)
320 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &ifname
);
324 link
= new0(Link
, 1);
329 link
->manager
= manager
;
330 link
->state
= LINK_STATE_PENDING
;
331 link
->rtnl_extended_attrs
= true;
332 link
->ifindex
= ifindex
;
333 link
->ifname
= strdup(ifname
);
337 r
= sd_netlink_message_read_ether_addr(message
, IFLA_ADDRESS
, &link
->mac
);
339 log_link_debug(link
, "MAC address not found for new device, continuing without");
341 r
= asprintf(&link
->state_file
, "/run/systemd/netif/links/%d",
346 r
= asprintf(&link
->lease_file
, "/run/systemd/netif/leases/%d",
351 r
= asprintf(&link
->lldp_file
, "/run/systemd/netif/lldp/%d",
357 r
= hashmap_ensure_allocated(&manager
->links
, NULL
);
361 r
= hashmap_put(manager
->links
, INT_TO_PTR(link
->ifindex
), link
);
365 r
= link_update_flags(link
, message
);
375 static void link_free(Link
*link
) {
383 while (!set_isempty(link
->addresses
))
384 address_free(set_first(link
->addresses
));
386 while (!set_isempty(link
->addresses_foreign
))
387 address_free(set_first(link
->addresses_foreign
));
389 link
->addresses
= set_free(link
->addresses
);
391 link
->addresses_foreign
= set_free(link
->addresses_foreign
);
393 while ((address
= link
->pool_addresses
)) {
394 LIST_REMOVE(addresses
, link
->pool_addresses
, address
);
395 address_free(address
);
398 sd_dhcp_server_unref(link
->dhcp_server
);
399 sd_dhcp_client_unref(link
->dhcp_client
);
400 sd_dhcp_lease_unref(link
->dhcp_lease
);
402 free(link
->lease_file
);
404 sd_lldp_unref(link
->lldp
);
406 free(link
->lldp_file
);
408 sd_ipv4ll_unref(link
->ipv4ll
);
409 sd_dhcp6_client_unref(link
->dhcp6_client
);
410 sd_ndisc_unref(link
->ndisc_router_discovery
);
413 hashmap_remove(link
->manager
->links
, INT_TO_PTR(link
->ifindex
));
417 (void)unlink(link
->state_file
);
418 free(link
->state_file
);
420 udev_device_unref(link
->udev_device
);
422 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
)
423 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(carrier
->ifindex
));
424 hashmap_free(link
->bound_to_links
);
426 HASHMAP_FOREACH (carrier
, link
->bound_by_links
, i
)
427 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(carrier
->ifindex
));
428 hashmap_free(link
->bound_by_links
);
433 Link
*link_unref(Link
*link
) {
437 assert(link
->n_ref
> 0);
449 Link
*link_ref(Link
*link
) {
453 assert(link
->n_ref
> 0);
460 int link_get(Manager
*m
, int ifindex
, Link
**ret
) {
467 link
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
476 static void link_set_state(Link
*link
, LinkState state
) {
479 if (link
->state
== state
)
484 link_send_changed(link
, "AdministrativeState", NULL
);
489 static void link_enter_unmanaged(Link
*link
) {
492 log_link_debug(link
, "Unmanaged");
494 link_set_state(link
, LINK_STATE_UNMANAGED
);
499 static int link_stop_clients(Link
*link
) {
503 assert(link
->manager
);
504 assert(link
->manager
->event
);
506 if (link
->dhcp_client
) {
507 k
= sd_dhcp_client_stop(link
->dhcp_client
);
509 r
= log_link_warning_errno(link
, r
, "Could not stop DHCPv4 client: %m");
513 k
= sd_ipv4ll_stop(link
->ipv4ll
);
515 r
= log_link_warning_errno(link
, r
, "Could not stop IPv4 link-local: %m");
518 if (link
->dhcp6_client
) {
519 k
= sd_dhcp6_client_stop(link
->dhcp6_client
);
521 r
= log_link_warning_errno(link
, r
, "Could not stop DHCPv6 client: %m");
524 if (link
->ndisc_router_discovery
) {
525 k
= sd_ndisc_stop(link
->ndisc_router_discovery
);
527 r
= log_link_warning_errno(link
, r
, "Could not stop IPv6 Router Discovery: %m");
531 k
= sd_lldp_stop(link
->lldp
);
533 r
= log_link_warning_errno(link
, r
, "Could not stop LLDP: %m");
539 void link_enter_failed(Link
*link
) {
542 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
545 log_link_warning(link
, "Failed");
547 link_set_state(link
, LINK_STATE_FAILED
);
549 link_stop_clients(link
);
554 static Address
* link_find_dhcp_server_address(Link
*link
) {
558 assert(link
->network
);
560 /* The first statically configured address if there is any */
561 LIST_FOREACH(addresses
, address
, link
->network
->static_addresses
) {
563 if (address
->family
!= AF_INET
)
566 if (in_addr_is_null(address
->family
, &address
->in_addr
))
572 /* If that didn't work, find a suitable address we got from the pool */
573 LIST_FOREACH(addresses
, address
, link
->pool_addresses
) {
574 if (address
->family
!= AF_INET
)
583 static int link_enter_configured(Link
*link
) {
585 assert(link
->network
);
586 assert(link
->state
== LINK_STATE_SETTING_ROUTES
);
588 log_link_info(link
, "Configured");
590 link_set_state(link
, LINK_STATE_CONFIGURED
);
597 void link_check_ready(Link
*link
) {
606 if (!link
->static_configured
)
609 if (link_ipv4ll_enabled(link
))
610 if (!link
->ipv4ll_address
||
614 if (link_ipv6ll_enabled(link
))
615 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) > 0)
618 if ((link_dhcp4_enabled(link
) && !link_dhcp6_enabled(link
) &&
619 !link
->dhcp4_configured
) ||
620 (link_dhcp6_enabled(link
) && !link_dhcp4_enabled(link
) &&
621 !link
->dhcp6_configured
) ||
622 (link_dhcp4_enabled(link
) && link_dhcp6_enabled(link
) &&
623 !link
->dhcp4_configured
&& !link
->dhcp6_configured
))
626 if (link_ipv6_accept_ra_enabled(link
) && !link
->ndisc_configured
)
629 SET_FOREACH(a
, link
->addresses
, i
)
630 if (!address_is_ready(a
))
633 if (link
->state
!= LINK_STATE_CONFIGURED
)
634 link_enter_configured(link
);
639 static int route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
640 _cleanup_link_unref_ Link
*link
= userdata
;
643 assert(link
->link_messages
> 0);
644 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
645 LINK_STATE_SETTING_ROUTES
, LINK_STATE_FAILED
,
648 link
->link_messages
--;
650 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
653 r
= sd_netlink_message_get_errno(m
);
654 if (r
< 0 && r
!= -EEXIST
)
655 log_link_warning_errno(link
, r
, "Could not set route: %m");
657 if (link
->link_messages
== 0) {
658 log_link_debug(link
, "Routes set");
659 link
->static_configured
= true;
660 link_check_ready(link
);
666 static int link_enter_set_routes(Link
*link
) {
671 assert(link
->network
);
672 assert(link
->state
== LINK_STATE_SETTING_ADDRESSES
);
674 link_set_state(link
, LINK_STATE_SETTING_ROUTES
);
676 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
677 r
= route_configure(rt
, link
, &route_handler
);
679 log_link_warning_errno(link
, r
, "Could not set routes: %m");
680 link_enter_failed(link
);
684 link
->link_messages
++;
687 if (link
->link_messages
== 0) {
688 link
->static_configured
= true;
689 link_check_ready(link
);
691 log_link_debug(link
, "Setting routes");
696 int link_route_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
697 _cleanup_link_unref_ Link
*link
= userdata
;
702 assert(link
->ifname
);
704 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
707 r
= sd_netlink_message_get_errno(m
);
708 if (r
< 0 && r
!= -ESRCH
)
709 log_link_warning_errno(link
, r
, "Could not drop route: %m");
714 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
715 _cleanup_link_unref_ Link
*link
= userdata
;
721 assert(link
->ifname
);
722 assert(link
->link_messages
> 0);
723 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
724 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
726 link
->link_messages
--;
728 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
731 r
= sd_netlink_message_get_errno(m
);
732 if (r
< 0 && r
!= -EEXIST
)
733 log_link_warning_errno(link
, r
, "could not set address: %m");
735 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
737 if (link
->link_messages
== 0) {
738 log_link_debug(link
, "Addresses set");
739 link_enter_set_routes(link
);
745 static int link_push_dns_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
746 _cleanup_free_
struct in_addr
*addresses
= NULL
;
747 size_t n_addresses
= 0, n_allocated
= 0;
750 log_debug("Copying DNS server information from %s", link
->ifname
);
755 STRV_FOREACH(a
, link
->network
->dns
) {
758 /* Only look for IPv4 addresses */
759 if (inet_pton(AF_INET
, *a
, &ia
) <= 0)
762 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
765 addresses
[n_addresses
++] = ia
;
768 if (link
->network
->dhcp_use_dns
&&
770 const struct in_addr
*da
= NULL
;
773 n
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &da
);
776 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
779 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
784 if (n_addresses
<= 0)
787 return sd_dhcp_server_set_dns(s
, addresses
, n_addresses
);
790 static int link_push_ntp_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
791 _cleanup_free_
struct in_addr
*addresses
= NULL
;
792 size_t n_addresses
= 0, n_allocated
= 0;
798 log_debug("Copying NTP server information from %s", link
->ifname
);
800 STRV_FOREACH(a
, link
->network
->ntp
) {
803 /* Only look for IPv4 addresses */
804 if (inet_pton(AF_INET
, *a
, &ia
) <= 0)
807 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
810 addresses
[n_addresses
++] = ia
;
813 if (link
->network
->dhcp_use_ntp
&&
815 const struct in_addr
*da
= NULL
;
818 n
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &da
);
821 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
824 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
829 if (n_addresses
<= 0)
832 return sd_dhcp_server_set_ntp(s
, addresses
, n_addresses
);
835 static int link_enter_set_addresses(Link
*link
) {
840 assert(link
->network
);
841 assert(link
->state
!= _LINK_STATE_INVALID
);
843 link_set_state(link
, LINK_STATE_SETTING_ADDRESSES
);
845 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
846 r
= address_configure(ad
, link
, &address_handler
, false);
848 log_link_warning_errno(link
, r
, "Could not set addresses: %m");
849 link_enter_failed(link
);
853 link
->link_messages
++;
856 /* now that we can figure out a default address for the dhcp server,
858 if (link_dhcp4_server_enabled(link
)) {
861 bool acquired_uplink
= false;
863 address
= link_find_dhcp_server_address(link
);
865 log_link_warning(link
, "Failed to find suitable address for DHCPv4 server instance.");
866 link_enter_failed(link
);
870 /* use the server address' subnet as the pool */
871 r
= sd_dhcp_server_configure_pool(link
->dhcp_server
, &address
->in_addr
.in
, address
->prefixlen
,
872 link
->network
->dhcp_server_pool_offset
, link
->network
->dhcp_server_pool_size
);
877 r = sd_dhcp_server_set_router(link->dhcp_server,
878 &main_address->in_addr.in);
883 if (link
->network
->dhcp_server_max_lease_time_usec
> 0) {
884 r
= sd_dhcp_server_set_max_lease_time(
886 DIV_ROUND_UP(link
->network
->dhcp_server_max_lease_time_usec
, USEC_PER_SEC
));
891 if (link
->network
->dhcp_server_default_lease_time_usec
> 0) {
892 r
= sd_dhcp_server_set_default_lease_time(
894 DIV_ROUND_UP(link
->network
->dhcp_server_default_lease_time_usec
, USEC_PER_SEC
));
899 if (link
->network
->dhcp_server_emit_dns
) {
901 if (link
->network
->n_dhcp_server_dns
> 0)
902 r
= sd_dhcp_server_set_dns(link
->dhcp_server
, link
->network
->dhcp_server_dns
, link
->network
->n_dhcp_server_dns
);
904 uplink
= manager_find_uplink(link
->manager
, link
);
905 acquired_uplink
= true;
908 log_link_debug(link
, "Not emitting DNS server information on link, couldn't find suitable uplink.");
911 r
= link_push_dns_to_dhcp_server(uplink
, link
->dhcp_server
);
914 log_link_warning_errno(link
, r
, "Failed to set DNS server for DHCP server, ignoring: %m");
918 if (link
->network
->dhcp_server_emit_ntp
) {
920 if (link
->network
->n_dhcp_server_ntp
> 0)
921 r
= sd_dhcp_server_set_ntp(link
->dhcp_server
, link
->network
->dhcp_server_ntp
, link
->network
->n_dhcp_server_ntp
);
923 if (!acquired_uplink
)
924 uplink
= manager_find_uplink(link
->manager
, link
);
927 log_link_debug(link
, "Not emitting NTP server information on link, couldn't find suitable uplink.");
930 r
= link_push_ntp_to_dhcp_server(uplink
, link
->dhcp_server
);
934 log_link_warning_errno(link
, r
, "Failed to set NTP server for DHCP server, ignoring: %m");
937 if (link
->network
->dhcp_server_emit_timezone
) {
938 _cleanup_free_
char *buffer
= NULL
;
939 const char *tz
= NULL
;
941 if (link
->network
->dhcp_server_timezone
)
942 tz
= link
->network
->dhcp_server_timezone
;
944 r
= get_timezone(&buffer
);
946 log_warning_errno(r
, "Failed to determine timezone: %m");
952 r
= sd_dhcp_server_set_timezone(link
->dhcp_server
, tz
);
958 r
= sd_dhcp_server_start(link
->dhcp_server
);
960 log_link_warning_errno(link
, r
, "Could not start DHCPv4 server instance: %m");
962 link_enter_failed(link
);
967 log_link_debug(link
, "Offering DHCPv4 leases");
970 if (link
->link_messages
== 0)
971 link_enter_set_routes(link
);
973 log_link_debug(link
, "Setting addresses");
978 int link_address_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
979 _cleanup_link_unref_ Link
*link
= userdata
;
984 assert(link
->ifname
);
986 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
989 r
= sd_netlink_message_get_errno(m
);
990 if (r
< 0 && r
!= -EADDRNOTAVAIL
)
991 log_link_warning_errno(link
, r
, "Could not drop address: %m");
996 static int link_set_bridge_fdb(Link
*const link
) {
1000 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
1001 r
= fdb_entry_configure(link
, fdb_entry
);
1003 log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
1011 static int link_set_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1012 _cleanup_link_unref_ Link
*link
= userdata
;
1015 log_link_debug(link
, "Set link");
1017 r
= sd_netlink_message_get_errno(m
);
1018 if (r
< 0 && r
!= -EEXIST
) {
1019 log_link_error_errno(link
, r
, "Could not join netdev: %m");
1020 link_enter_failed(link
);
1027 static int set_hostname_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
1028 _cleanup_link_unref_ Link
*link
= userdata
;
1029 const sd_bus_error
*e
;
1034 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1037 e
= sd_bus_message_get_error(m
);
1039 log_link_warning_errno(link
, sd_bus_error_get_errno(e
), "Could not set hostname: %s", e
->message
);
1044 int link_set_hostname(Link
*link
, const char *hostname
) {
1048 assert(link
->manager
);
1050 log_link_debug(link
, "Setting transient hostname: '%s'", strna(hostname
));
1052 if (!link
->manager
->bus
) {
1053 /* TODO: replace by assert when we can rely on kdbus */
1054 log_link_info(link
, "Not connected to system bus, ignoring transient hostname.");
1058 r
= sd_bus_call_method_async(
1061 "org.freedesktop.hostname1",
1062 "/org/freedesktop/hostname1",
1063 "org.freedesktop.hostname1",
1065 set_hostname_handler
,
1072 return log_link_error_errno(link
, r
, "Could not set transient hostname: %m");
1079 static int set_timezone_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
1080 _cleanup_link_unref_ Link
*link
= userdata
;
1081 const sd_bus_error
*e
;
1086 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1089 e
= sd_bus_message_get_error(m
);
1091 log_link_warning_errno(link
, sd_bus_error_get_errno(e
), "Could not set timezone: %s", e
->message
);
1096 int link_set_timezone(Link
*link
, const char *tz
) {
1100 assert(link
->manager
);
1103 log_link_debug(link
, "Setting system timezone: '%s'", tz
);
1105 if (!link
->manager
->bus
) {
1106 log_link_info(link
, "Not connected to system bus, ignoring timezone.");
1110 r
= sd_bus_call_method_async(
1113 "org.freedesktop.timedate1",
1114 "/org/freedesktop/timedate1",
1115 "org.freedesktop.timedate1",
1117 set_timezone_handler
,
1123 return log_link_error_errno(link
, r
, "Could not set timezone: %m");
1130 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1131 _cleanup_link_unref_ Link
*link
= userdata
;
1136 assert(link
->ifname
);
1138 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1141 r
= sd_netlink_message_get_errno(m
);
1143 log_link_warning_errno(link
, r
, "Could not set MTU: %m");
1148 int link_set_mtu(Link
*link
, uint32_t mtu
) {
1149 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1153 assert(link
->manager
);
1154 assert(link
->manager
->rtnl
);
1156 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1158 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1160 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1162 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1164 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1166 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, set_mtu_handler
, link
, 0, NULL
);
1168 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1175 static int link_set_bridge(Link
*link
) {
1176 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1180 assert(link
->network
);
1182 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1184 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1186 r
= sd_rtnl_message_link_set_family(req
, PF_BRIDGE
);
1188 return log_link_error_errno(link
, r
, "Could not set message family: %m");
1190 r
= sd_netlink_message_open_container(req
, IFLA_PROTINFO
);
1192 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1194 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_GUARD
, !link
->network
->use_bpdu
);
1196 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_GUARD attribute: %m");
1198 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MODE
, link
->network
->hairpin
);
1200 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MODE attribute: %m");
1202 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_FAST_LEAVE
, link
->network
->fast_leave
);
1204 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_FAST_LEAVE attribute: %m");
1206 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_PROTECT
, !link
->network
->allow_port_to_be_root
);
1208 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PROTECT attribute: %m");
1210 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_UNICAST_FLOOD
, link
->network
->unicast_flood
);
1212 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_UNICAST_FLOOD attribute: %m");
1214 if(link
->network
->cost
!= 0) {
1215 r
= sd_netlink_message_append_u32(req
, IFLA_BRPORT_COST
, link
->network
->cost
);
1217 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_COST attribute: %m");
1220 r
= sd_netlink_message_close_container(req
);
1222 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1224 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_set_handler
, link
, 0, NULL
);
1226 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1233 static void lldp_handler(sd_lldp
*lldp
, int event
, void *userdata
) {
1234 Link
*link
= userdata
;
1238 assert(link
->network
);
1239 assert(link
->manager
);
1242 case SD_LLDP_EVENT_UPDATE_INFO
:
1243 r
= sd_lldp_save(link
->lldp
, link
->lldp_file
);
1245 log_link_warning_errno(link
, r
, "Could not save LLDP: %m");
1253 static int link_acquire_ipv6_conf(Link
*link
) {
1258 if (link_dhcp6_enabled(link
)) {
1259 assert(link
->dhcp6_client
);
1260 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1262 log_link_debug(link
, "Acquiring DHCPv6 lease");
1264 r
= sd_dhcp6_client_set_local_address(link
->dhcp6_client
, &link
->ipv6ll_address
);
1265 if (r
< 0 && r
!= -EBUSY
)
1266 return log_link_warning_errno(link
, r
, "Could not set IPv6LL address in DHCP client: %m");
1268 r
= sd_dhcp6_client_start(link
->dhcp6_client
);
1269 if (r
< 0 && r
!= -EBUSY
)
1270 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv6 lease: %m");
1273 if (link_ipv6_accept_ra_enabled(link
)) {
1274 assert(link
->ndisc_router_discovery
);
1276 log_link_debug(link
, "Discovering IPv6 routers");
1278 r
= sd_ndisc_router_discovery_start(link
->ndisc_router_discovery
);
1279 if (r
< 0 && r
!= -EBUSY
)
1280 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1286 static int link_acquire_conf(Link
*link
) {
1290 assert(link
->network
);
1291 assert(link
->manager
);
1292 assert(link
->manager
->event
);
1294 if (link_ipv4ll_enabled(link
)) {
1295 assert(link
->ipv4ll
);
1297 log_link_debug(link
, "Acquiring IPv4 link-local address");
1299 r
= sd_ipv4ll_start(link
->ipv4ll
);
1301 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1304 if (link_dhcp4_enabled(link
)) {
1305 assert(link
->dhcp_client
);
1307 log_link_debug(link
, "Acquiring DHCPv4 lease");
1309 r
= sd_dhcp_client_start(link
->dhcp_client
);
1311 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1314 if (link_lldp_enabled(link
)) {
1317 log_link_debug(link
, "Starting LLDP");
1319 r
= sd_lldp_start(link
->lldp
);
1321 return log_link_warning_errno(link
, r
, "Could not start LLDP: %m");
1327 bool link_has_carrier(Link
*link
) {
1328 /* see Documentation/networking/operstates.txt in the kernel sources */
1330 if (link
->kernel_operstate
== IF_OPER_UP
)
1333 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1334 /* operstate may not be implemented, so fall back to flags */
1335 if ((link
->flags
& IFF_LOWER_UP
) && !(link
->flags
& IFF_DORMANT
))
1341 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1342 _cleanup_link_unref_ Link
*link
= userdata
;
1347 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1350 r
= sd_netlink_message_get_errno(m
);
1352 /* we warn but don't fail the link, as it may be
1354 log_link_warning_errno(link
, r
, "Could not bring up interface: %m");
1359 static int link_up(Link
*link
) {
1360 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1361 uint8_t ipv6ll_mode
;
1365 assert(link
->network
);
1366 assert(link
->manager
);
1367 assert(link
->manager
->rtnl
);
1369 log_link_debug(link
, "Bringing link up");
1371 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1373 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1375 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1377 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1379 if (link
->network
->mac
) {
1380 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
1382 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
1385 if (link
->network
->mtu
) {
1386 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, link
->network
->mtu
);
1388 return log_link_error_errno(link
, r
, "Could not set MTU: %m");
1391 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1393 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1395 if (socket_ipv6_is_supported()) {
1396 /* if the kernel lacks ipv6 support setting IFF_UP fails if any ipv6 options are passed */
1397 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1399 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1401 ipv6ll_mode
= link_ipv6ll_enabled(link
) ? IN6_ADDR_GEN_MODE_EUI64
: IN6_ADDR_GEN_MODE_NONE
;
1402 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1404 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1406 if (!in_addr_is_null(AF_INET6
, &link
->network
->ipv6_token
)) {
1407 r
= sd_netlink_message_append_in6_addr(req
, IFLA_INET6_TOKEN
, &link
->network
->ipv6_token
.in6
);
1409 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_TOKEN: %m");
1412 r
= sd_netlink_message_close_container(req
);
1414 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1417 r
= sd_netlink_message_close_container(req
);
1419 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1421 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_up_handler
, link
, 0, NULL
);
1423 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1430 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1431 _cleanup_link_unref_ Link
*link
= userdata
;
1436 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1439 r
= sd_netlink_message_get_errno(m
);
1441 log_link_warning_errno(link
, r
, "Could not bring down interface: %m");
1446 static int link_down(Link
*link
) {
1447 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1451 assert(link
->manager
);
1452 assert(link
->manager
->rtnl
);
1454 log_link_debug(link
, "Bringing link down");
1456 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
1457 RTM_SETLINK
, link
->ifindex
);
1459 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1461 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
1463 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1465 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_down_handler
, link
, 0, NULL
);
1467 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1474 static int link_handle_bound_to_list(Link
*link
) {
1478 bool required_up
= false;
1479 bool link_is_up
= false;
1483 if (hashmap_isempty(link
->bound_to_links
))
1486 if (link
->flags
& IFF_UP
)
1489 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
1490 if (link_has_carrier(l
)) {
1495 if (!required_up
&& link_is_up
) {
1496 r
= link_down(link
);
1499 } else if (required_up
&& !link_is_up
) {
1508 static int link_handle_bound_by_list(Link
*link
) {
1515 if (hashmap_isempty(link
->bound_by_links
))
1518 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
1519 r
= link_handle_bound_to_list(l
);
1527 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
1533 if (link
== carrier
)
1536 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
1539 r
= hashmap_ensure_allocated(h
, NULL
);
1543 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
1550 static int link_new_bound_by_list(Link
*link
) {
1555 bool list_updated
= false;
1558 assert(link
->manager
);
1562 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
1563 if (!carrier
->network
)
1566 if (strv_isempty(carrier
->network
->bind_carrier
))
1569 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
, 0)) {
1570 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
1574 list_updated
= true;
1581 HASHMAP_FOREACH (carrier
, link
->bound_by_links
, i
) {
1582 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
1586 link_dirty(carrier
);
1592 static int link_new_bound_to_list(Link
*link
) {
1597 bool list_updated
= false;
1600 assert(link
->manager
);
1605 if (strv_isempty(link
->network
->bind_carrier
))
1610 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
1611 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
, 0)) {
1612 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
1616 list_updated
= true;
1623 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
1624 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
1628 link_dirty(carrier
);
1634 static int link_new_carrier_maps(Link
*link
) {
1637 r
= link_new_bound_by_list(link
);
1641 r
= link_handle_bound_by_list(link
);
1645 r
= link_new_bound_to_list(link
);
1649 r
= link_handle_bound_to_list(link
);
1656 static void link_free_bound_to_list(Link
*link
) {
1660 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
1661 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
1663 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
1664 link_dirty(bound_to
);
1670 static void link_free_bound_by_list(Link
*link
) {
1674 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
1675 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
1677 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
1678 link_dirty(bound_by
);
1679 link_handle_bound_to_list(bound_by
);
1686 static void link_free_carrier_maps(Link
*link
) {
1687 bool list_updated
= false;
1691 if (!hashmap_isempty(link
->bound_to_links
)) {
1692 link_free_bound_to_list(link
);
1693 list_updated
= true;
1696 if (!hashmap_isempty(link
->bound_by_links
)) {
1697 link_free_bound_by_list(link
);
1698 list_updated
= true;
1707 void link_drop(Link
*link
) {
1708 if (!link
|| link
->state
== LINK_STATE_LINGER
)
1711 link_set_state(link
, LINK_STATE_LINGER
);
1713 link_free_carrier_maps(link
);
1715 log_link_debug(link
, "Link removed");
1717 (void)unlink(link
->state_file
);
1723 static int link_joined(Link
*link
) {
1727 assert(link
->network
);
1729 if (!hashmap_isempty(link
->bound_to_links
)) {
1730 r
= link_handle_bound_to_list(link
);
1733 } else if (!(link
->flags
& IFF_UP
)) {
1736 link_enter_failed(link
);
1741 if(link
->network
->bridge
) {
1742 r
= link_set_bridge(link
);
1744 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
1747 return link_enter_set_addresses(link
);
1750 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1751 _cleanup_link_unref_ Link
*link
= userdata
;
1755 assert(link
->network
);
1759 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1762 r
= sd_netlink_message_get_errno(m
);
1763 if (r
< 0 && r
!= -EEXIST
) {
1764 log_link_error_errno(link
, r
, "Could not join netdev: %m");
1765 link_enter_failed(link
);
1768 log_link_debug(link
, "Joined netdev");
1770 if (link
->enslaving
<= 0)
1776 static int link_enter_join_netdev(Link
*link
) {
1782 assert(link
->network
);
1783 assert(link
->state
== LINK_STATE_PENDING
);
1785 link_set_state(link
, LINK_STATE_ENSLAVING
);
1789 if (!link
->network
->bridge
&&
1790 !link
->network
->bond
&&
1791 hashmap_isempty(link
->network
->stacked_netdevs
))
1792 return link_joined(link
);
1794 if (link
->network
->bond
) {
1795 log_struct(LOG_DEBUG
,
1796 LOG_LINK_INTERFACE(link
),
1797 LOG_NETDEV_INTERFACE(link
->network
->bond
),
1798 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
),
1801 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
1803 log_struct_errno(LOG_WARNING
, r
,
1804 LOG_LINK_INTERFACE(link
),
1805 LOG_NETDEV_INTERFACE(link
->network
->bond
),
1806 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
),
1809 link_enter_failed(link
);
1816 if (link
->network
->bridge
) {
1817 log_struct(LOG_DEBUG
,
1818 LOG_LINK_INTERFACE(link
),
1819 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
1820 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
),
1823 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
1825 log_struct_errno(LOG_WARNING
, r
,
1826 LOG_LINK_INTERFACE(link
),
1827 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
1828 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
),
1830 link_enter_failed(link
);
1837 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
1839 log_struct(LOG_DEBUG
,
1840 LOG_LINK_INTERFACE(link
),
1841 LOG_NETDEV_INTERFACE(netdev
),
1842 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
),
1845 r
= netdev_join(netdev
, link
, netdev_join_handler
);
1847 log_struct_errno(LOG_WARNING
, r
,
1848 LOG_LINK_INTERFACE(link
),
1849 LOG_NETDEV_INTERFACE(netdev
),
1850 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
),
1852 link_enter_failed(link
);
1862 static int link_set_ipv4_forward(Link
*link
) {
1865 if (!link_ipv4_forward_enabled(link
))
1868 /* We propagate the forwarding flag from one interface to the
1869 * global setting one way. This means: as long as at least one
1870 * interface was configured at any time that had IP forwarding
1871 * enabled the setting will stay on for good. We do this
1872 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
1873 * somewhat in sync (see below). */
1875 r
= write_string_file("/proc/sys/net/ipv4/ip_forward", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
1877 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
1882 static int link_set_ipv6_forward(Link
*link
) {
1885 if (!link_ipv6_forward_enabled(link
))
1888 /* On Linux, the IPv6 stack does not not know a per-interface
1889 * packet forwarding setting: either packet forwarding is on
1890 * for all, or off for all. We hence don't bother with a
1891 * per-interface setting, but simply propagate the interface
1892 * flag, if it is set, to the global flag, one-way. Note that
1893 * while IPv4 would allow a per-interface flag, we expose the
1894 * same behaviour there and also propagate the setting from
1895 * one to all, to keep things simple (see above). */
1897 r
= write_string_file("/proc/sys/net/ipv6/conf/all/forwarding", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
1899 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
1904 static int link_set_ipv6_privacy_extensions(Link
*link
) {
1905 char buf
[DECIMAL_STR_MAX(unsigned) + 1];
1906 IPv6PrivacyExtensions s
;
1907 const char *p
= NULL
;
1910 s
= link_ipv6_privacy_extensions(link
);
1914 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/use_tempaddr");
1915 xsprintf(buf
, "%u", (unsigned) link
->network
->ipv6_privacy_extensions
);
1917 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
1919 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
1924 static int link_set_ipv6_accept_ra(Link
*link
) {
1925 const char *p
= NULL
;
1928 /* Make this a NOP if IPv6 is not available */
1929 if (!socket_ipv6_is_supported())
1932 if (link
->flags
& IFF_LOOPBACK
)
1938 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/accept_ra");
1940 /* We handle router advertisments ourselves, tell the kernel to GTFO */
1941 r
= write_string_file(p
, "0", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
1943 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
1948 static int link_set_ipv6_dad_transmits(Link
*link
) {
1949 char buf
[DECIMAL_STR_MAX(int) + 1];
1950 const char *p
= NULL
;
1953 /* Make this a NOP if IPv6 is not available */
1954 if (!socket_ipv6_is_supported())
1957 if (link
->flags
& IFF_LOOPBACK
)
1963 if (link
->network
->ipv6_dad_transmits
< 0)
1966 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/dad_transmits");
1967 xsprintf(buf
, "%i", link
->network
->ipv6_dad_transmits
);
1969 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
1971 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
1976 static int link_set_ipv6_hop_limit(Link
*link
) {
1977 char buf
[DECIMAL_STR_MAX(int) + 1];
1978 const char *p
= NULL
;
1981 /* Make this a NOP if IPv6 is not available */
1982 if (!socket_ipv6_is_supported())
1985 if (link
->flags
& IFF_LOOPBACK
)
1991 if (link
->network
->ipv6_hop_limit
< 0)
1994 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/hop_limit");
1995 xsprintf(buf
, "%i", link
->network
->ipv6_hop_limit
);
1997 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
1999 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2004 static int link_drop_foreign_config(Link
*link
) {
2010 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
2011 /* we consider IPv6LL addresses to be managed by the kernel */
2012 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2015 r
= address_remove(address
, link
, link_address_remove_handler
);
2020 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2021 /* do not touch routes managed by the kernel */
2022 if (route
->protocol
== RTPROT_KERNEL
)
2025 r
= route_remove(route
, link
, link_address_remove_handler
);
2033 static int link_configure(Link
*link
) {
2037 assert(link
->network
);
2038 assert(link
->state
== LINK_STATE_PENDING
);
2040 /* Drop foreign config, but ignore loopback or critical devices.
2041 * We do not want to remove loopback address or addresses used for root NFS. */
2042 if (!(link
->flags
& IFF_LOOPBACK
) && !(link
->network
->dhcp_critical
)) {
2043 r
= link_drop_foreign_config(link
);
2048 r
= link_set_bridge_fdb(link
);
2052 r
= link_set_ipv4_forward(link
);
2056 r
= link_set_ipv6_forward(link
);
2060 r
= link_set_ipv6_privacy_extensions(link
);
2064 r
= link_set_ipv6_accept_ra(link
);
2068 r
= link_set_ipv6_dad_transmits(link
);
2072 r
= link_set_ipv6_hop_limit(link
);
2076 if (link_ipv4ll_enabled(link
)) {
2077 r
= ipv4ll_configure(link
);
2082 if (link_dhcp4_enabled(link
)) {
2083 r
= dhcp4_configure(link
);
2088 if (link_dhcp4_server_enabled(link
)) {
2089 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
2093 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
2098 if (link_dhcp6_enabled(link
) ||
2099 link_ipv6_accept_ra_enabled(link
)) {
2100 r
= dhcp6_configure(link
);
2105 if (link_ipv6_accept_ra_enabled(link
)) {
2106 r
= ndisc_configure(link
);
2111 if (link_lldp_enabled(link
)) {
2112 r
= sd_lldp_new(link
->ifindex
, link
->ifname
, &link
->mac
, &link
->lldp
);
2116 r
= sd_lldp_attach_event(link
->lldp
, NULL
, 0);
2120 r
= sd_lldp_set_callback(link
->lldp
,
2121 lldp_handler
, link
);
2126 if (link_has_carrier(link
)) {
2127 r
= link_acquire_conf(link
);
2131 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) == 0) {
2132 r
= link_acquire_ipv6_conf(link
);
2138 return link_enter_join_netdev(link
);
2141 static int link_initialized_and_synced(sd_netlink
*rtnl
, sd_netlink_message
*m
,
2143 _cleanup_link_unref_ Link
*link
= userdata
;
2148 assert(link
->ifname
);
2149 assert(link
->manager
);
2151 if (link
->state
!= LINK_STATE_PENDING
)
2154 log_link_debug(link
, "Link state is up-to-date");
2156 r
= link_new_bound_by_list(link
);
2160 r
= link_handle_bound_by_list(link
);
2164 if (!link
->network
) {
2165 r
= network_get(link
->manager
, link
->udev_device
, link
->ifname
,
2166 &link
->mac
, &network
);
2168 link_enter_unmanaged(link
);
2173 if (link
->flags
& IFF_LOOPBACK
) {
2174 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
2175 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
2177 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
2178 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
2180 if (network
->dhcp_server
)
2181 log_link_debug(link
, "Ignoring DHCP server for loopback link");
2184 r
= network_apply(link
->manager
, network
, link
);
2189 r
= link_new_bound_to_list(link
);
2193 r
= link_configure(link
);
2200 int link_initialized(Link
*link
, struct udev_device
*device
) {
2201 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2205 assert(link
->manager
);
2206 assert(link
->manager
->rtnl
);
2209 if (link
->state
!= LINK_STATE_PENDING
)
2212 if (link
->udev_device
)
2215 log_link_debug(link
, "udev initialized link");
2217 link
->udev_device
= udev_device_ref(device
);
2219 /* udev has initialized the link, but we don't know if we have yet
2220 * processed the NEWLINK messages with the latest state. Do a GETLINK,
2221 * when it returns we know that the pending NEWLINKs have already been
2222 * processed and that we are up-to-date */
2224 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
2229 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
,
2230 link_initialized_and_synced
, link
, 0, NULL
);
2239 static int link_load(Link
*link
) {
2240 _cleanup_free_
char *network_file
= NULL
,
2243 *dhcp4_address
= NULL
,
2244 *ipv4ll_address
= NULL
;
2245 union in_addr_union address
;
2246 union in_addr_union route_dst
;
2252 r
= parse_env_file(link
->state_file
, NEWLINE
,
2253 "NETWORK_FILE", &network_file
,
2254 "ADDRESSES", &addresses
,
2256 "DHCP4_ADDRESS", &dhcp4_address
,
2257 "IPV4LL_ADDRESS", &ipv4ll_address
,
2259 if (r
< 0 && r
!= -ENOENT
)
2260 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
2267 suffix
= strrchr(network_file
, '.');
2269 log_link_debug(link
, "Failed to get network name from %s", network_file
);
2270 goto network_file_fail
;
2274 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
2276 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
2277 goto network_file_fail
;
2280 r
= network_apply(link
->manager
, network
, link
);
2282 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
2291 _cleanup_free_
char *address_str
= NULL
;
2292 char *prefixlen_str
;
2294 unsigned char prefixlen
;
2296 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
2298 log_link_debug_errno(link
, r
, "Failed to extract next address string: %m");
2304 prefixlen_str
= strchr(address_str
, '/');
2305 if (!prefixlen_str
) {
2306 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
2310 *prefixlen_str
++ = '\0';
2312 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
2314 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
2318 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
2320 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
2324 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
2326 return log_link_error_errno(link
, r
, "Failed to add address: %m");
2335 _cleanup_free_
char *route_str
= NULL
;
2336 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
2338 char *prefixlen_str
;
2340 unsigned char prefixlen
, tos
, table
;
2343 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
2345 log_link_debug_errno(link
, r
, "Failed to extract next route string: %m");
2351 prefixlen_str
= strchr(route_str
, '/');
2352 if (!prefixlen_str
) {
2353 log_link_debug(link
, "Failed to parse route %s", route_str
);
2357 *prefixlen_str
++ = '\0';
2359 r
= sscanf(prefixlen_str
, "%hhu/%hhu/%"SCNu32
"/%hhu/"USEC_FMT
, &prefixlen
, &tos
, &priority
, &table
, &lifetime
);
2361 log_link_debug(link
,
2362 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
2367 r
= in_addr_from_string_auto(route_str
, &family
, &route_dst
);
2369 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
2373 r
= route_add(link
, family
, &route_dst
, prefixlen
, tos
, priority
, table
, &route
);
2375 return log_link_error_errno(link
, r
, "Failed to add route: %m");
2377 if (lifetime
!= USEC_INFINITY
) {
2378 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(), lifetime
,
2379 0, route_expire_handler
, route
);
2381 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
2384 route
->lifetime
= lifetime
;
2385 sd_event_source_unref(route
->expire
);
2386 route
->expire
= expire
;
2391 if (dhcp4_address
) {
2392 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
2394 log_link_debug_errno(link
, r
, "Falied to parse DHCPv4 address %s: %m", dhcp4_address
);
2395 goto dhcp4_address_fail
;
2398 r
= sd_dhcp_client_new(&link
->dhcp_client
);
2400 return log_link_error_errno(link
, r
, "Falied to create DHCPv4 client: %m");
2402 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
2404 return log_link_error_errno(link
, r
, "Falied to set inital DHCPv4 address %s: %m", dhcp4_address
);
2409 if (ipv4ll_address
) {
2410 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
2412 log_link_debug_errno(link
, r
, "Falied to parse IPv4LL address %s: %m", ipv4ll_address
);
2413 goto ipv4ll_address_fail
;
2416 r
= sd_ipv4ll_new(&link
->ipv4ll
);
2418 return log_link_error_errno(link
, r
, "Falied to create IPv4LL client: %m");
2420 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
2422 return log_link_error_errno(link
, r
, "Falied to set inital IPv4LL address %s: %m", ipv4ll_address
);
2425 ipv4ll_address_fail
:
2430 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
2432 _cleanup_udev_device_unref_
struct udev_device
*device
= NULL
;
2433 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
2441 r
= link_new(m
, message
, ret
);
2447 log_link_debug(link
, "Link %d added", link
->ifindex
);
2449 r
= link_load(link
);
2453 if (detect_container() <= 0) {
2454 /* not in a container, udev will be around */
2455 sprintf(ifindex_str
, "n%d", link
->ifindex
);
2456 device
= udev_device_new_from_device_id(m
->udev
, ifindex_str
);
2458 r
= log_link_warning_errno(link
, errno
, "Could not find udev device: %m");
2462 if (udev_device_get_is_initialized(device
) <= 0) {
2464 log_link_debug(link
, "link pending udev initialization...");
2468 r
= link_initialized(link
, device
);
2472 /* we are calling a callback directly, so must take a ref */
2475 r
= link_initialized_and_synced(m
->rtnl
, NULL
, link
);
2482 link_enter_failed(link
);
2486 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
2491 log_link_info(link
, "Gained IPv6LL");
2493 link
->ipv6ll_address
= *address
;
2494 link_check_ready(link
);
2496 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
2497 r
= link_acquire_ipv6_conf(link
);
2499 link_enter_failed(link
);
2507 static int link_carrier_gained(Link
*link
) {
2512 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
2513 r
= link_acquire_conf(link
);
2515 link_enter_failed(link
);
2520 r
= link_handle_bound_by_list(link
);
2527 static int link_carrier_lost(Link
*link
) {
2532 r
= link_stop_clients(link
);
2534 link_enter_failed(link
);
2538 r
= link_handle_bound_by_list(link
);
2545 int link_carrier_reset(Link
*link
) {
2550 if (link_has_carrier(link
)) {
2551 r
= link_carrier_lost(link
);
2555 r
= link_carrier_gained(link
);
2559 log_link_info(link
, "Reset carrier");
2566 int link_update(Link
*link
, sd_netlink_message
*m
) {
2567 struct ether_addr mac
;
2570 bool had_carrier
, carrier_gained
, carrier_lost
;
2574 assert(link
->ifname
);
2577 if (link
->state
== LINK_STATE_LINGER
) {
2579 log_link_info(link
, "Link readded");
2580 link_set_state(link
, LINK_STATE_ENSLAVING
);
2582 r
= link_new_carrier_maps(link
);
2587 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
2588 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
2589 log_link_info(link
, "Renamed to %s", ifname
);
2591 link_free_carrier_maps(link
);
2593 r
= free_and_strdup(&link
->ifname
, ifname
);
2597 r
= link_new_carrier_maps(link
);
2602 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
2603 if (r
>= 0 && mtu
> 0) {
2605 if (!link
->original_mtu
) {
2606 link
->original_mtu
= mtu
;
2607 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
2610 if (link
->dhcp_client
) {
2611 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
2614 log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
2620 /* The kernel may broadcast NEWLINK messages without the MAC address
2621 set, simply ignore them. */
2622 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
2624 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
2627 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
2630 log_link_debug(link
, "MAC address: "
2631 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
2632 mac
.ether_addr_octet
[0],
2633 mac
.ether_addr_octet
[1],
2634 mac
.ether_addr_octet
[2],
2635 mac
.ether_addr_octet
[3],
2636 mac
.ether_addr_octet
[4],
2637 mac
.ether_addr_octet
[5]);
2640 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
2642 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
2645 if (link
->dhcp_client
) {
2646 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
2647 (const uint8_t *) &link
->mac
,
2651 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
2654 if (link
->dhcp6_client
) {
2655 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
2656 (const uint8_t *) &link
->mac
,
2660 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
2665 had_carrier
= link_has_carrier(link
);
2667 r
= link_update_flags(link
, m
);
2671 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
2672 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
2674 if (carrier_gained
) {
2675 log_link_info(link
, "Gained carrier");
2677 r
= link_carrier_gained(link
);
2680 } else if (carrier_lost
) {
2681 log_link_info(link
, "Lost carrier");
2683 r
= link_carrier_lost(link
);
2692 int link_save(Link
*link
) {
2693 _cleanup_free_
char *temp_path
= NULL
;
2694 _cleanup_fclose_
FILE *f
= NULL
;
2695 const char *admin_state
, *oper_state
;
2702 assert(link
->state_file
);
2703 assert(link
->lease_file
);
2704 assert(link
->manager
);
2706 if (link
->state
== LINK_STATE_LINGER
) {
2707 unlink(link
->state_file
);
2711 admin_state
= link_state_to_string(link
->state
);
2712 assert(admin_state
);
2714 oper_state
= link_operstate_to_string(link
->operstate
);
2717 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
2721 fchmod(fileno(f
), 0644);
2724 "# This is private data. Do not parse.\n"
2727 admin_state
, oper_state
);
2729 if (link
->network
) {
2731 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
2732 const char *dhcp_domainname
= NULL
;
2733 char **dhcp6_domains
= NULL
;
2735 if (link
->dhcp6_client
) {
2736 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
, &dhcp6_lease
);
2737 if (r
< 0 && r
!= -ENOMSG
)
2738 log_link_debug(link
, "No DHCPv6 lease");
2741 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
2745 fputstrv(f
, link
->network
->dns
, NULL
, &space
);
2747 if (link
->network
->dhcp_use_dns
&&
2749 const struct in_addr
*addresses
;
2751 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
2755 serialize_in_addrs(f
, addresses
, r
);
2760 if (link
->network
->dhcp_use_dns
&& dhcp6_lease
) {
2761 struct in6_addr
*in6_addrs
;
2763 r
= sd_dhcp6_lease_get_dns(dhcp6_lease
, &in6_addrs
);
2767 serialize_in6_addrs(f
, in6_addrs
, r
);
2775 fputstrv(f
, link
->network
->ntp
, NULL
, &space
);
2777 if (link
->network
->dhcp_use_ntp
&&
2779 const struct in_addr
*addresses
;
2781 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
2785 serialize_in_addrs(f
, addresses
, r
);
2790 if (link
->network
->dhcp_use_ntp
&& dhcp6_lease
) {
2791 struct in6_addr
*in6_addrs
;
2794 r
= sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease
,
2799 serialize_in6_addrs(f
, in6_addrs
, r
);
2803 r
= sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease
, &hosts
);
2805 fputstrv(f
, hosts
, NULL
, &space
);
2810 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
2811 if (link
->dhcp_lease
)
2812 (void) sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &dhcp_domainname
);
2815 (void) sd_dhcp6_lease_get_domains(dhcp6_lease
, &dhcp6_domains
);
2818 fputs("DOMAINS=", f
);
2819 fputstrv(f
, link
->network
->search_domains
, NULL
, &space
);
2821 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
&& dhcp_domainname
)
2822 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
2824 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
&& dhcp6_domains
)
2825 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
2829 fputs("ROUTE_DOMAINS=", f
);
2830 fputstrv(f
, link
->network
->route_domains
, NULL
, NULL
);
2832 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
&& dhcp_domainname
)
2833 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
2835 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
&& dhcp6_domains
)
2836 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
2840 fprintf(f
, "LLMNR=%s\n",
2841 resolve_support_to_string(link
->network
->llmnr
));
2842 fprintf(f
, "MDNS=%s\n",
2843 resolve_support_to_string(link
->network
->mdns
));
2845 if (link
->network
->dnssec_mode
!= _DNSSEC_MODE_INVALID
)
2846 fprintf(f
, "DNSSEC=%s\n",
2847 dnssec_mode_to_string(link
->network
->dnssec_mode
));
2849 if (!set_isempty(link
->network
->dnssec_negative_trust_anchors
)) {
2852 fputs("DNSSEC_NTA=", f
);
2854 SET_FOREACH(n
, link
->network
->dnssec_negative_trust_anchors
, i
)
2855 fputs_with_space(f
, n
, NULL
, &space
);
2859 fputs("ADDRESSES=", f
);
2861 SET_FOREACH(a
, link
->addresses
, i
) {
2862 _cleanup_free_
char *address_str
= NULL
;
2864 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
2868 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
2873 fputs("ROUTES=", f
);
2875 SET_FOREACH(route
, link
->routes
, i
) {
2876 _cleanup_free_
char *route_str
= NULL
;
2878 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
2882 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%hhu/"USEC_FMT
, space
? " " : "", route_str
,
2883 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
2890 if (!hashmap_isempty(link
->bound_to_links
)) {
2894 fputs("CARRIER_BOUND_TO=", f
);
2895 HASHMAP_FOREACH(carrier
, link
->bound_to_links
, i
)
2896 fputs_with_space(f
, carrier
->ifname
, NULL
, &space
);
2901 if (!hashmap_isempty(link
->bound_by_links
)) {
2905 fputs("CARRIER_BOUND_BY=", f
);
2906 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
)
2907 fputs_with_space(f
, carrier
->ifname
, NULL
, &space
);
2912 if (link
->dhcp_lease
) {
2913 struct in_addr address
;
2914 const char *tz
= NULL
;
2916 assert(link
->network
);
2918 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
2920 fprintf(f
, "TIMEZONE=%s\n", tz
);
2922 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
2924 fputs("DHCP4_ADDRESS=", f
);
2925 serialize_in_addrs(f
, &address
, 1);
2929 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
2937 unlink(link
->lease_file
);
2940 struct in_addr address
;
2942 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
2944 fputs("IPV4LL_ADDRESS=", f
);
2945 serialize_in_addrs(f
, &address
, 1);
2951 assert(link
->network
);
2953 r
= sd_lldp_save(link
->lldp
, link
->lldp_file
);
2961 unlink(link
->lldp_file
);
2963 r
= fflush_and_check(f
);
2967 if (rename(temp_path
, link
->state_file
) < 0) {
2975 (void) unlink(link
->state_file
);
2977 (void) unlink(temp_path
);
2979 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
2982 /* The serialized state in /run is no longer up-to-date. */
2983 void link_dirty(Link
*link
) {
2988 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
2990 /* allocation errors are ignored */
2993 r
= set_put(link
->manager
->dirty_links
, link
);
2995 /* allocation errors are ignored */
3001 /* The serialized state in /run is up-to-date */
3002 void link_clean(Link
*link
) {
3004 assert(link
->manager
);
3006 set_remove(link
->manager
->dirty_links
, link
);
3010 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
3011 [LINK_STATE_PENDING
] = "pending",
3012 [LINK_STATE_ENSLAVING
] = "configuring",
3013 [LINK_STATE_SETTING_ADDRESSES
] = "configuring",
3014 [LINK_STATE_SETTING_ROUTES
] = "configuring",
3015 [LINK_STATE_CONFIGURED
] = "configured",
3016 [LINK_STATE_UNMANAGED
] = "unmanaged",
3017 [LINK_STATE_FAILED
] = "failed",
3018 [LINK_STATE_LINGER
] = "linger",
3021 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);
3023 static const char* const link_operstate_table
[_LINK_OPERSTATE_MAX
] = {
3024 [LINK_OPERSTATE_OFF
] = "off",
3025 [LINK_OPERSTATE_NO_CARRIER
] = "no-carrier",
3026 [LINK_OPERSTATE_DORMANT
] = "dormant",
3027 [LINK_OPERSTATE_CARRIER
] = "carrier",
3028 [LINK_OPERSTATE_DEGRADED
] = "degraded",
3029 [LINK_OPERSTATE_ROUTABLE
] = "routable",
3032 DEFINE_STRING_TABLE_LOOKUP(link_operstate
, LinkOperationalState
);