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"
31 #include "netlink-util.h"
32 #include "network-internal.h"
33 #include "networkd-link.h"
34 #include "networkd-netdev.h"
36 #include "socket-util.h"
37 #include "stdio-util.h"
38 #include "string-table.h"
39 #include "udev-util.h"
43 bool link_dhcp6_enabled(Link
*link
) {
44 if (link
->flags
& IFF_LOOPBACK
)
50 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV6
;
53 bool link_dhcp4_enabled(Link
*link
) {
54 if (link
->flags
& IFF_LOOPBACK
)
60 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV4
;
63 bool link_dhcp4_server_enabled(Link
*link
) {
64 if (link
->flags
& IFF_LOOPBACK
)
70 return link
->network
->dhcp_server
;
73 bool link_ipv4ll_enabled(Link
*link
) {
74 if (link
->flags
& IFF_LOOPBACK
)
80 return link
->network
->link_local
& ADDRESS_FAMILY_IPV4
;
83 bool link_ipv6ll_enabled(Link
*link
) {
84 if (link
->flags
& IFF_LOOPBACK
)
90 return link
->network
->link_local
& ADDRESS_FAMILY_IPV6
;
93 bool link_lldp_enabled(Link
*link
) {
94 if (link
->flags
& IFF_LOOPBACK
)
100 if (link
->network
->bridge
)
103 return link
->network
->lldp
;
106 static bool link_ipv4_forward_enabled(Link
*link
) {
107 if (link
->flags
& IFF_LOOPBACK
)
113 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
116 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV4
;
119 static bool link_ipv6_forward_enabled(Link
*link
) {
121 if (!socket_ipv6_is_supported())
124 if (link
->flags
& IFF_LOOPBACK
)
130 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
133 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV6
;
136 bool link_ipv6_accept_ra_enabled(Link
*link
) {
137 if (link
->flags
& IFF_LOOPBACK
)
143 /* If unset use system default (enabled if local forwarding is disabled.
144 * disabled if local forwarding is enabled).
145 * If set, ignore or enforce RA independent of local forwarding state.
147 if (link
->network
->ipv6_accept_ra
< 0)
148 /* default to accept RA if ip_forward is disabled and ignore RA if ip_forward is enabled */
149 return !link_ipv6_forward_enabled(link
);
150 else if (link
->network
->ipv6_accept_ra
> 0)
151 /* accept RA even if ip_forward is enabled */
158 static IPv6PrivacyExtensions
link_ipv6_privacy_extensions(Link
*link
) {
160 if (!socket_ipv6_is_supported())
161 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
163 if (link
->flags
& IFF_LOOPBACK
)
164 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
167 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
169 return link
->network
->ipv6_privacy_extensions
;
172 void link_update_operstate(Link
*link
) {
173 LinkOperationalState operstate
;
176 if (link
->kernel_operstate
== IF_OPER_DORMANT
)
177 operstate
= LINK_OPERSTATE_DORMANT
;
178 else if (link_has_carrier(link
)) {
180 uint8_t scope
= RT_SCOPE_NOWHERE
;
183 /* if we have carrier, check what addresses we have */
184 SET_FOREACH(address
, link
->addresses
, i
) {
185 if (!address_is_ready(address
))
188 if (address
->scope
< scope
)
189 scope
= address
->scope
;
192 /* for operstate we also take foreign addresses into account */
193 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
194 if (!address_is_ready(address
))
197 if (address
->scope
< scope
)
198 scope
= address
->scope
;
201 if (scope
< RT_SCOPE_SITE
)
202 /* universally accessible addresses found */
203 operstate
= LINK_OPERSTATE_ROUTABLE
;
204 else if (scope
< RT_SCOPE_HOST
)
205 /* only link or site local addresses found */
206 operstate
= LINK_OPERSTATE_DEGRADED
;
208 /* no useful addresses found */
209 operstate
= LINK_OPERSTATE_CARRIER
;
210 } else if (link
->flags
& IFF_UP
)
211 operstate
= LINK_OPERSTATE_NO_CARRIER
;
213 operstate
= LINK_OPERSTATE_OFF
;
215 if (link
->operstate
!= operstate
) {
216 link
->operstate
= operstate
;
217 link_send_changed(link
, "OperationalState", NULL
);
219 manager_dirty(link
->manager
);
223 #define FLAG_STRING(string, flag, old, new) \
224 (((old ^ new) & flag) \
225 ? ((old & flag) ? (" -" string) : (" +" string)) \
228 static int link_update_flags(Link
*link
, sd_netlink_message
*m
) {
229 unsigned flags
, unknown_flags_added
, unknown_flags_removed
, unknown_flags
;
235 r
= sd_rtnl_message_link_get_flags(m
, &flags
);
237 return log_link_warning_errno(link
, r
, "Could not get link flags: %m");
239 r
= sd_netlink_message_read_u8(m
, IFLA_OPERSTATE
, &operstate
);
241 /* if we got a message without operstate, take it to mean
242 the state was unchanged */
243 operstate
= link
->kernel_operstate
;
245 if ((link
->flags
== flags
) && (link
->kernel_operstate
== operstate
))
248 if (link
->flags
!= flags
) {
249 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",
250 FLAG_STRING("LOOPBACK", IFF_LOOPBACK
, link
->flags
, flags
),
251 FLAG_STRING("MASTER", IFF_MASTER
, link
->flags
, flags
),
252 FLAG_STRING("SLAVE", IFF_SLAVE
, link
->flags
, flags
),
253 FLAG_STRING("UP", IFF_UP
, link
->flags
, flags
),
254 FLAG_STRING("DORMANT", IFF_DORMANT
, link
->flags
, flags
),
255 FLAG_STRING("LOWER_UP", IFF_LOWER_UP
, link
->flags
, flags
),
256 FLAG_STRING("RUNNING", IFF_RUNNING
, link
->flags
, flags
),
257 FLAG_STRING("MULTICAST", IFF_MULTICAST
, link
->flags
, flags
),
258 FLAG_STRING("BROADCAST", IFF_BROADCAST
, link
->flags
, flags
),
259 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT
, link
->flags
, flags
),
260 FLAG_STRING("PROMISC", IFF_PROMISC
, link
->flags
, flags
),
261 FLAG_STRING("ALLMULTI", IFF_ALLMULTI
, link
->flags
, flags
),
262 FLAG_STRING("PORTSEL", IFF_PORTSEL
, link
->flags
, flags
),
263 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA
, link
->flags
, flags
),
264 FLAG_STRING("DYNAMIC", IFF_DYNAMIC
, link
->flags
, flags
),
265 FLAG_STRING("NOARP", IFF_NOARP
, link
->flags
, flags
),
266 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS
, link
->flags
, flags
),
267 FLAG_STRING("DEBUG", IFF_DEBUG
, link
->flags
, flags
),
268 FLAG_STRING("ECHO", IFF_ECHO
, link
->flags
, flags
));
270 unknown_flags
= ~(IFF_LOOPBACK
| IFF_MASTER
| IFF_SLAVE
| IFF_UP
|
271 IFF_DORMANT
| IFF_LOWER_UP
| IFF_RUNNING
|
272 IFF_MULTICAST
| IFF_BROADCAST
| IFF_POINTOPOINT
|
273 IFF_PROMISC
| IFF_ALLMULTI
| IFF_PORTSEL
|
274 IFF_AUTOMEDIA
| IFF_DYNAMIC
| IFF_NOARP
|
275 IFF_NOTRAILERS
| IFF_DEBUG
| IFF_ECHO
);
276 unknown_flags_added
= ((link
->flags
^ flags
) & flags
& unknown_flags
);
277 unknown_flags_removed
= ((link
->flags
^ flags
) & link
->flags
& unknown_flags
);
279 /* link flags are currently at most 18 bits, let's align to
281 if (unknown_flags_added
)
283 "Unknown link flags gained: %#.5x (ignoring)",
284 unknown_flags_added
);
286 if (unknown_flags_removed
)
288 "Unknown link flags lost: %#.5x (ignoring)",
289 unknown_flags_removed
);
293 link
->kernel_operstate
= operstate
;
295 link_update_operstate(link
);
300 static int link_new(Manager
*manager
, sd_netlink_message
*message
, Link
**ret
) {
301 _cleanup_link_unref_ Link
*link
= NULL
;
310 r
= sd_netlink_message_get_type(message
, &type
);
313 else if (type
!= RTM_NEWLINK
)
316 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
319 else if (ifindex
<= 0)
322 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &ifname
);
326 link
= new0(Link
, 1);
331 link
->manager
= manager
;
332 link
->state
= LINK_STATE_PENDING
;
333 link
->rtnl_extended_attrs
= true;
334 link
->ifindex
= ifindex
;
335 link
->ifname
= strdup(ifname
);
339 r
= sd_netlink_message_read_ether_addr(message
, IFLA_ADDRESS
, &link
->mac
);
341 log_link_debug(link
, "MAC address not found for new device, continuing without");
343 r
= asprintf(&link
->state_file
, "/run/systemd/netif/links/%d",
348 r
= asprintf(&link
->lease_file
, "/run/systemd/netif/leases/%d",
353 r
= asprintf(&link
->lldp_file
, "/run/systemd/netif/lldp/%d",
359 r
= hashmap_ensure_allocated(&manager
->links
, NULL
);
363 r
= hashmap_put(manager
->links
, INT_TO_PTR(link
->ifindex
), link
);
367 r
= link_update_flags(link
, message
);
377 static void link_free(Link
*link
) {
385 while (!set_isempty(link
->addresses
))
386 address_free(set_first(link
->addresses
));
388 while (!set_isempty(link
->addresses_foreign
))
389 address_free(set_first(link
->addresses_foreign
));
391 link
->addresses
= set_free(link
->addresses
);
393 link
->addresses_foreign
= set_free(link
->addresses_foreign
);
395 while ((address
= link
->pool_addresses
)) {
396 LIST_REMOVE(addresses
, link
->pool_addresses
, address
);
397 address_free(address
);
400 sd_dhcp_server_unref(link
->dhcp_server
);
401 sd_dhcp_client_unref(link
->dhcp_client
);
402 sd_dhcp_lease_unref(link
->dhcp_lease
);
404 free(link
->lease_file
);
406 sd_lldp_unref(link
->lldp
);
408 free(link
->lldp_file
);
410 sd_ipv4ll_unref(link
->ipv4ll
);
411 sd_dhcp6_client_unref(link
->dhcp6_client
);
412 sd_ndisc_unref(link
->ndisc_router_discovery
);
415 hashmap_remove(link
->manager
->links
, INT_TO_PTR(link
->ifindex
));
419 (void)unlink(link
->state_file
);
420 free(link
->state_file
);
422 udev_device_unref(link
->udev_device
);
424 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
)
425 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(carrier
->ifindex
));
426 hashmap_free(link
->bound_to_links
);
428 HASHMAP_FOREACH (carrier
, link
->bound_by_links
, i
)
429 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(carrier
->ifindex
));
430 hashmap_free(link
->bound_by_links
);
435 Link
*link_unref(Link
*link
) {
439 assert(link
->n_ref
> 0);
451 Link
*link_ref(Link
*link
) {
455 assert(link
->n_ref
> 0);
462 int link_get(Manager
*m
, int ifindex
, Link
**ret
) {
469 link
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
478 static void link_set_state(Link
*link
, LinkState state
) {
481 if (link
->state
== state
)
486 link_send_changed(link
, "AdministrativeState", NULL
);
491 static void link_enter_unmanaged(Link
*link
) {
494 log_link_debug(link
, "Unmanaged");
496 link_set_state(link
, LINK_STATE_UNMANAGED
);
501 static int link_stop_clients(Link
*link
) {
505 assert(link
->manager
);
506 assert(link
->manager
->event
);
508 if (link
->dhcp_client
) {
509 k
= sd_dhcp_client_stop(link
->dhcp_client
);
511 r
= log_link_warning_errno(link
, r
, "Could not stop DHCPv4 client: %m");
515 k
= sd_ipv4ll_stop(link
->ipv4ll
);
517 r
= log_link_warning_errno(link
, r
, "Could not stop IPv4 link-local: %m");
520 if (link
->dhcp6_client
) {
521 k
= sd_dhcp6_client_stop(link
->dhcp6_client
);
523 r
= log_link_warning_errno(link
, r
, "Could not stop DHCPv6 client: %m");
526 if (link
->ndisc_router_discovery
) {
527 k
= sd_ndisc_stop(link
->ndisc_router_discovery
);
529 r
= log_link_warning_errno(link
, r
, "Could not stop IPv6 Router Discovery: %m");
533 k
= sd_lldp_stop(link
->lldp
);
535 r
= log_link_warning_errno(link
, r
, "Could not stop LLDP: %m");
541 void link_enter_failed(Link
*link
) {
544 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
547 log_link_warning(link
, "Failed");
549 link_set_state(link
, LINK_STATE_FAILED
);
551 link_stop_clients(link
);
556 static Address
* link_find_dhcp_server_address(Link
*link
) {
560 assert(link
->network
);
562 /* The first statically configured address if there is any */
563 LIST_FOREACH(addresses
, address
, link
->network
->static_addresses
) {
565 if (address
->family
!= AF_INET
)
568 if (in_addr_is_null(address
->family
, &address
->in_addr
))
574 /* If that didn't work, find a suitable address we got from the pool */
575 LIST_FOREACH(addresses
, address
, link
->pool_addresses
) {
576 if (address
->family
!= AF_INET
)
585 static int link_enter_configured(Link
*link
) {
587 assert(link
->network
);
588 assert(link
->state
== LINK_STATE_SETTING_ROUTES
);
590 log_link_info(link
, "Configured");
592 link_set_state(link
, LINK_STATE_CONFIGURED
);
599 void link_check_ready(Link
*link
) {
608 if (!link
->static_configured
)
611 if (link_ipv4ll_enabled(link
))
612 if (!link
->ipv4ll_address
||
616 if (link_ipv6ll_enabled(link
))
617 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) > 0)
620 if ((link_dhcp4_enabled(link
) && !link_dhcp6_enabled(link
) &&
621 !link
->dhcp4_configured
) ||
622 (link_dhcp6_enabled(link
) && !link_dhcp4_enabled(link
) &&
623 !link
->dhcp6_configured
) ||
624 (link_dhcp4_enabled(link
) && link_dhcp6_enabled(link
) &&
625 !link
->dhcp4_configured
&& !link
->dhcp6_configured
))
628 if (link_ipv6_accept_ra_enabled(link
) && !link
->ndisc_configured
)
631 SET_FOREACH(a
, link
->addresses
, i
)
632 if (!address_is_ready(a
))
635 if (link
->state
!= LINK_STATE_CONFIGURED
)
636 link_enter_configured(link
);
641 static int route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
642 _cleanup_link_unref_ Link
*link
= userdata
;
645 assert(link
->link_messages
> 0);
646 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
647 LINK_STATE_SETTING_ROUTES
, LINK_STATE_FAILED
,
650 link
->link_messages
--;
652 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
655 r
= sd_netlink_message_get_errno(m
);
656 if (r
< 0 && r
!= -EEXIST
)
657 log_link_warning_errno(link
, r
, "Could not set route: %m");
659 if (link
->link_messages
== 0) {
660 log_link_debug(link
, "Routes set");
661 link
->static_configured
= true;
662 link_check_ready(link
);
668 static int link_enter_set_routes(Link
*link
) {
673 assert(link
->network
);
674 assert(link
->state
== LINK_STATE_SETTING_ADDRESSES
);
676 link_set_state(link
, LINK_STATE_SETTING_ROUTES
);
678 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
679 r
= route_configure(rt
, link
, &route_handler
);
681 log_link_warning_errno(link
, r
, "Could not set routes: %m");
682 link_enter_failed(link
);
686 link
->link_messages
++;
689 if (link
->link_messages
== 0) {
690 link
->static_configured
= true;
691 link_check_ready(link
);
693 log_link_debug(link
, "Setting routes");
698 int link_route_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
699 _cleanup_link_unref_ Link
*link
= userdata
;
704 assert(link
->ifname
);
706 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
709 r
= sd_netlink_message_get_errno(m
);
710 if (r
< 0 && r
!= -ESRCH
)
711 log_link_warning_errno(link
, r
, "Could not drop route: %m");
716 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
717 _cleanup_link_unref_ Link
*link
= userdata
;
723 assert(link
->ifname
);
724 assert(link
->link_messages
> 0);
725 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
726 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
728 link
->link_messages
--;
730 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
733 r
= sd_netlink_message_get_errno(m
);
734 if (r
< 0 && r
!= -EEXIST
)
735 log_link_warning_errno(link
, r
, "could not set address: %m");
737 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
739 if (link
->link_messages
== 0) {
740 log_link_debug(link
, "Addresses set");
741 link_enter_set_routes(link
);
747 static int link_push_dns_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
748 _cleanup_free_
struct in_addr
*addresses
= NULL
;
749 size_t n_addresses
= 0, n_allocated
= 0;
752 log_debug("Copying DNS server information from %s", link
->ifname
);
757 STRV_FOREACH(a
, link
->network
->dns
) {
760 /* Only look for IPv4 addresses */
761 if (inet_pton(AF_INET
, *a
, &ia
) <= 0)
764 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
767 addresses
[n_addresses
++] = ia
;
770 if (link
->network
->dhcp_dns
&&
772 const struct in_addr
*da
= NULL
;
775 n
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &da
);
778 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
781 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
786 if (n_addresses
<= 0)
789 return sd_dhcp_server_set_dns(s
, addresses
, n_addresses
);
792 static int link_push_ntp_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
793 _cleanup_free_
struct in_addr
*addresses
= NULL
;
794 size_t n_addresses
= 0, n_allocated
= 0;
800 log_debug("Copying NTP server information from %s", link
->ifname
);
802 STRV_FOREACH(a
, link
->network
->ntp
) {
805 /* Only look for IPv4 addresses */
806 if (inet_pton(AF_INET
, *a
, &ia
) <= 0)
809 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
812 addresses
[n_addresses
++] = ia
;
815 if (link
->network
->dhcp_ntp
&&
817 const struct in_addr
*da
= NULL
;
820 n
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &da
);
823 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
826 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
831 if (n_addresses
<= 0)
834 return sd_dhcp_server_set_ntp(s
, addresses
, n_addresses
);
837 static int link_enter_set_addresses(Link
*link
) {
842 assert(link
->network
);
843 assert(link
->state
!= _LINK_STATE_INVALID
);
845 link_set_state(link
, LINK_STATE_SETTING_ADDRESSES
);
847 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
848 r
= address_configure(ad
, link
, &address_handler
, false);
850 log_link_warning_errno(link
, r
, "Could not set addresses: %m");
851 link_enter_failed(link
);
855 link
->link_messages
++;
858 /* now that we can figure out a default address for the dhcp server,
860 if (link_dhcp4_server_enabled(link
)) {
863 bool acquired_uplink
= false;
865 address
= link_find_dhcp_server_address(link
);
867 log_link_warning(link
, "Failed to find suitable address for DHCPv4 server instance.");
868 link_enter_failed(link
);
872 /* use the server address' subnet as the pool */
873 r
= sd_dhcp_server_configure_pool(link
->dhcp_server
, &address
->in_addr
.in
, address
->prefixlen
,
874 link
->network
->dhcp_server_pool_offset
, link
->network
->dhcp_server_pool_size
);
879 r = sd_dhcp_server_set_router(link->dhcp_server,
880 &main_address->in_addr.in);
885 if (link
->network
->dhcp_server_max_lease_time_usec
> 0) {
886 r
= sd_dhcp_server_set_max_lease_time(
888 DIV_ROUND_UP(link
->network
->dhcp_server_max_lease_time_usec
, USEC_PER_SEC
));
893 if (link
->network
->dhcp_server_default_lease_time_usec
> 0) {
894 r
= sd_dhcp_server_set_default_lease_time(
896 DIV_ROUND_UP(link
->network
->dhcp_server_default_lease_time_usec
, USEC_PER_SEC
));
901 if (link
->network
->dhcp_server_emit_dns
) {
903 if (link
->network
->n_dhcp_server_dns
> 0)
904 r
= sd_dhcp_server_set_dns(link
->dhcp_server
, link
->network
->dhcp_server_dns
, link
->network
->n_dhcp_server_dns
);
906 uplink
= manager_find_uplink(link
->manager
, link
);
907 acquired_uplink
= true;
910 log_link_debug(link
, "Not emitting DNS server information on link, couldn't find suitable uplink.");
913 r
= link_push_dns_to_dhcp_server(uplink
, link
->dhcp_server
);
916 log_link_warning_errno(link
, r
, "Failed to set DNS server for DHCP server, ignoring: %m");
920 if (link
->network
->dhcp_server_emit_ntp
) {
922 if (link
->network
->n_dhcp_server_ntp
> 0)
923 r
= sd_dhcp_server_set_ntp(link
->dhcp_server
, link
->network
->dhcp_server_ntp
, link
->network
->n_dhcp_server_ntp
);
925 if (!acquired_uplink
)
926 uplink
= manager_find_uplink(link
->manager
, link
);
929 log_link_debug(link
, "Not emitting NTP server information on link, couldn't find suitable uplink.");
932 r
= link_push_ntp_to_dhcp_server(uplink
, link
->dhcp_server
);
936 log_link_warning_errno(link
, r
, "Failed to set NTP server for DHCP server, ignoring: %m");
939 if (link
->network
->dhcp_server_emit_timezone
) {
940 _cleanup_free_
char *buffer
= NULL
;
941 const char *tz
= NULL
;
943 if (link
->network
->dhcp_server_timezone
)
944 tz
= link
->network
->dhcp_server_timezone
;
946 r
= get_timezone(&buffer
);
948 log_warning_errno(r
, "Failed to determine timezone: %m");
954 r
= sd_dhcp_server_set_timezone(link
->dhcp_server
, tz
);
960 r
= sd_dhcp_server_start(link
->dhcp_server
);
962 log_link_warning_errno(link
, r
, "Could not start DHCPv4 server instance: %m");
964 link_enter_failed(link
);
969 log_link_debug(link
, "Offering DHCPv4 leases");
972 if (link
->link_messages
== 0)
973 link_enter_set_routes(link
);
975 log_link_debug(link
, "Setting addresses");
980 int link_address_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
981 _cleanup_link_unref_ Link
*link
= userdata
;
986 assert(link
->ifname
);
988 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
991 r
= sd_netlink_message_get_errno(m
);
992 if (r
< 0 && r
!= -EADDRNOTAVAIL
)
993 log_link_warning_errno(link
, r
, "Could not drop address: %m");
998 static int link_set_bridge_fdb(Link
*const link
) {
1002 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
1003 r
= fdb_entry_configure(link
, fdb_entry
);
1005 log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
1013 static int link_set_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1014 _cleanup_link_unref_ Link
*link
= userdata
;
1017 log_link_debug(link
, "Set link");
1019 r
= sd_netlink_message_get_errno(m
);
1020 if (r
< 0 && r
!= -EEXIST
) {
1021 log_link_error_errno(link
, r
, "Could not join netdev: %m");
1022 link_enter_failed(link
);
1029 static int set_hostname_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
1030 _cleanup_link_unref_ Link
*link
= userdata
;
1031 const sd_bus_error
*e
;
1036 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1039 e
= sd_bus_message_get_error(m
);
1041 log_link_warning_errno(link
, sd_bus_error_get_errno(e
), "Could not set hostname: %s", e
->message
);
1046 int link_set_hostname(Link
*link
, const char *hostname
) {
1050 assert(link
->manager
);
1052 log_link_debug(link
, "Setting transient hostname: '%s'", strna(hostname
));
1054 if (!link
->manager
->bus
) {
1055 /* TODO: replace by assert when we can rely on kdbus */
1056 log_link_info(link
, "Not connected to system bus, ignoring transient hostname.");
1060 r
= sd_bus_call_method_async(
1063 "org.freedesktop.hostname1",
1064 "/org/freedesktop/hostname1",
1065 "org.freedesktop.hostname1",
1067 set_hostname_handler
,
1074 return log_link_error_errno(link
, r
, "Could not set transient hostname: %m");
1081 static int set_timezone_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
1082 _cleanup_link_unref_ Link
*link
= userdata
;
1083 const sd_bus_error
*e
;
1088 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1091 e
= sd_bus_message_get_error(m
);
1093 log_link_warning_errno(link
, sd_bus_error_get_errno(e
), "Could not set timezone: %s", e
->message
);
1098 int link_set_timezone(Link
*link
, const char *tz
) {
1102 assert(link
->manager
);
1105 log_link_debug(link
, "Setting system timezone: '%s'", tz
);
1107 if (!link
->manager
->bus
) {
1108 log_link_info(link
, "Not connected to system bus, ignoring timezone.");
1112 r
= sd_bus_call_method_async(
1115 "org.freedesktop.timedate1",
1116 "/org/freedesktop/timedate1",
1117 "org.freedesktop.timedate1",
1119 set_timezone_handler
,
1125 return log_link_error_errno(link
, r
, "Could not set timezone: %m");
1132 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1133 _cleanup_link_unref_ Link
*link
= userdata
;
1138 assert(link
->ifname
);
1140 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1143 r
= sd_netlink_message_get_errno(m
);
1145 log_link_warning_errno(link
, r
, "Could not set MTU: %m");
1150 int link_set_mtu(Link
*link
, uint32_t mtu
) {
1151 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1155 assert(link
->manager
);
1156 assert(link
->manager
->rtnl
);
1158 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1160 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1162 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1164 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1166 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1168 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, set_mtu_handler
, link
, 0, NULL
);
1170 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1177 static int link_set_bridge(Link
*link
) {
1178 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1182 assert(link
->network
);
1184 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1186 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1188 r
= sd_rtnl_message_link_set_family(req
, PF_BRIDGE
);
1190 return log_link_error_errno(link
, r
, "Could not set message family: %m");
1192 r
= sd_netlink_message_open_container(req
, IFLA_PROTINFO
);
1194 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1196 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_GUARD
, !link
->network
->use_bpdu
);
1198 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_GUARD attribute: %m");
1200 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MODE
, link
->network
->hairpin
);
1202 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MODE attribute: %m");
1204 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_FAST_LEAVE
, link
->network
->fast_leave
);
1206 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_FAST_LEAVE attribute: %m");
1208 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_PROTECT
, !link
->network
->allow_port_to_be_root
);
1210 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PROTECT attribute: %m");
1212 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_UNICAST_FLOOD
, link
->network
->unicast_flood
);
1214 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_UNICAST_FLOOD attribute: %m");
1216 if(link
->network
->cost
!= 0) {
1217 r
= sd_netlink_message_append_u32(req
, IFLA_BRPORT_COST
, link
->network
->cost
);
1219 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_COST attribute: %m");
1222 r
= sd_netlink_message_close_container(req
);
1224 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1226 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_set_handler
, link
, 0, NULL
);
1228 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1235 static void lldp_handler(sd_lldp
*lldp
, int event
, void *userdata
) {
1236 Link
*link
= userdata
;
1240 assert(link
->network
);
1241 assert(link
->manager
);
1244 case SD_LLDP_EVENT_UPDATE_INFO
:
1245 r
= sd_lldp_save(link
->lldp
, link
->lldp_file
);
1247 log_link_warning_errno(link
, r
, "Could not save LLDP: %m");
1255 static int link_acquire_ipv6_conf(Link
*link
) {
1260 if (link_dhcp6_enabled(link
)) {
1261 assert(link
->dhcp6_client
);
1262 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1264 log_link_debug(link
, "Acquiring DHCPv6 lease");
1266 r
= sd_dhcp6_client_set_local_address(link
->dhcp6_client
, &link
->ipv6ll_address
);
1267 if (r
< 0 && r
!= -EBUSY
)
1268 return log_link_warning_errno(link
, r
, "Could not set IPv6LL address in DHCP client: %m");
1270 r
= sd_dhcp6_client_start(link
->dhcp6_client
);
1271 if (r
< 0 && r
!= -EBUSY
)
1272 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv6 lease: %m");
1275 if (link_ipv6_accept_ra_enabled(link
)) {
1276 assert(link
->ndisc_router_discovery
);
1278 log_link_debug(link
, "Discovering IPv6 routers");
1280 r
= sd_ndisc_router_discovery_start(link
->ndisc_router_discovery
);
1281 if (r
< 0 && r
!= -EBUSY
)
1282 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1288 static int link_acquire_conf(Link
*link
) {
1292 assert(link
->network
);
1293 assert(link
->manager
);
1294 assert(link
->manager
->event
);
1296 if (link_ipv4ll_enabled(link
)) {
1297 assert(link
->ipv4ll
);
1299 log_link_debug(link
, "Acquiring IPv4 link-local address");
1301 r
= sd_ipv4ll_start(link
->ipv4ll
);
1303 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1306 if (link_dhcp4_enabled(link
)) {
1307 assert(link
->dhcp_client
);
1309 log_link_debug(link
, "Acquiring DHCPv4 lease");
1311 r
= sd_dhcp_client_start(link
->dhcp_client
);
1313 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1316 if (link_lldp_enabled(link
)) {
1319 log_link_debug(link
, "Starting LLDP");
1321 r
= sd_lldp_start(link
->lldp
);
1323 return log_link_warning_errno(link
, r
, "Could not start LLDP: %m");
1329 bool link_has_carrier(Link
*link
) {
1330 /* see Documentation/networking/operstates.txt in the kernel sources */
1332 if (link
->kernel_operstate
== IF_OPER_UP
)
1335 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1336 /* operstate may not be implemented, so fall back to flags */
1337 if ((link
->flags
& IFF_LOWER_UP
) && !(link
->flags
& IFF_DORMANT
))
1343 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1344 _cleanup_link_unref_ Link
*link
= userdata
;
1349 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1352 r
= sd_netlink_message_get_errno(m
);
1354 /* we warn but don't fail the link, as it may be
1356 log_link_warning_errno(link
, r
, "Could not bring up interface: %m");
1361 static int link_up(Link
*link
) {
1362 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1363 uint8_t ipv6ll_mode
;
1367 assert(link
->network
);
1368 assert(link
->manager
);
1369 assert(link
->manager
->rtnl
);
1371 log_link_debug(link
, "Bringing link up");
1373 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1375 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1377 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1379 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1381 if (link
->network
->mac
) {
1382 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
1384 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
1387 if (link
->network
->mtu
) {
1388 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, link
->network
->mtu
);
1390 return log_link_error_errno(link
, r
, "Could not set MTU: %m");
1393 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1395 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1397 if (socket_ipv6_is_supported()) {
1398 /* if the kernel lacks ipv6 support setting IFF_UP fails if any ipv6 options are passed */
1399 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1401 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1403 ipv6ll_mode
= link_ipv6ll_enabled(link
) ? IN6_ADDR_GEN_MODE_EUI64
: IN6_ADDR_GEN_MODE_NONE
;
1404 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1406 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1408 if (!in_addr_is_null(AF_INET6
, &link
->network
->ipv6_token
)) {
1409 r
= sd_netlink_message_append_in6_addr(req
, IFLA_INET6_TOKEN
, &link
->network
->ipv6_token
.in6
);
1411 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_TOKEN: %m");
1414 r
= sd_netlink_message_close_container(req
);
1416 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1419 r
= sd_netlink_message_close_container(req
);
1421 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1423 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_up_handler
, link
, 0, NULL
);
1425 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1432 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1433 _cleanup_link_unref_ Link
*link
= userdata
;
1438 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1441 r
= sd_netlink_message_get_errno(m
);
1443 log_link_warning_errno(link
, r
, "Could not bring down interface: %m");
1448 static int link_down(Link
*link
) {
1449 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1453 assert(link
->manager
);
1454 assert(link
->manager
->rtnl
);
1456 log_link_debug(link
, "Bringing link down");
1458 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
1459 RTM_SETLINK
, link
->ifindex
);
1461 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1463 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
1465 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1467 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_down_handler
, link
, 0, NULL
);
1469 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1476 static int link_handle_bound_to_list(Link
*link
) {
1480 bool required_up
= false;
1481 bool link_is_up
= false;
1485 if (hashmap_isempty(link
->bound_to_links
))
1488 if (link
->flags
& IFF_UP
)
1491 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
1492 if (link_has_carrier(l
)) {
1497 if (!required_up
&& link_is_up
) {
1498 r
= link_down(link
);
1501 } else if (required_up
&& !link_is_up
) {
1510 static int link_handle_bound_by_list(Link
*link
) {
1517 if (hashmap_isempty(link
->bound_by_links
))
1520 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
1521 r
= link_handle_bound_to_list(l
);
1529 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
1535 if (link
== carrier
)
1538 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
1541 r
= hashmap_ensure_allocated(h
, NULL
);
1545 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
1552 static int link_new_bound_by_list(Link
*link
) {
1557 bool list_updated
= false;
1560 assert(link
->manager
);
1564 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
1565 if (!carrier
->network
)
1568 if (strv_isempty(carrier
->network
->bind_carrier
))
1571 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
, 0)) {
1572 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
1576 list_updated
= true;
1583 HASHMAP_FOREACH (carrier
, link
->bound_by_links
, i
) {
1584 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
1588 link_dirty(carrier
);
1594 static int link_new_bound_to_list(Link
*link
) {
1599 bool list_updated
= false;
1602 assert(link
->manager
);
1607 if (strv_isempty(link
->network
->bind_carrier
))
1612 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
1613 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
, 0)) {
1614 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
1618 list_updated
= true;
1625 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
1626 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
1630 link_dirty(carrier
);
1636 static int link_new_carrier_maps(Link
*link
) {
1639 r
= link_new_bound_by_list(link
);
1643 r
= link_handle_bound_by_list(link
);
1647 r
= link_new_bound_to_list(link
);
1651 r
= link_handle_bound_to_list(link
);
1658 static void link_free_bound_to_list(Link
*link
) {
1662 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
1663 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
1665 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
1666 link_dirty(bound_to
);
1672 static void link_free_bound_by_list(Link
*link
) {
1676 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
1677 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
1679 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
1680 link_dirty(bound_by
);
1681 link_handle_bound_to_list(bound_by
);
1688 static void link_free_carrier_maps(Link
*link
) {
1689 bool list_updated
= false;
1693 if (!hashmap_isempty(link
->bound_to_links
)) {
1694 link_free_bound_to_list(link
);
1695 list_updated
= true;
1698 if (!hashmap_isempty(link
->bound_by_links
)) {
1699 link_free_bound_by_list(link
);
1700 list_updated
= true;
1709 void link_drop(Link
*link
) {
1710 if (!link
|| link
->state
== LINK_STATE_LINGER
)
1713 link_set_state(link
, LINK_STATE_LINGER
);
1715 link_free_carrier_maps(link
);
1717 log_link_debug(link
, "Link removed");
1719 (void)unlink(link
->state_file
);
1725 static int link_joined(Link
*link
) {
1729 assert(link
->network
);
1731 if (!hashmap_isempty(link
->bound_to_links
)) {
1732 r
= link_handle_bound_to_list(link
);
1735 } else if (!(link
->flags
& IFF_UP
)) {
1738 link_enter_failed(link
);
1743 if(link
->network
->bridge
) {
1744 r
= link_set_bridge(link
);
1746 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
1749 return link_enter_set_addresses(link
);
1752 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1753 _cleanup_link_unref_ Link
*link
= userdata
;
1757 assert(link
->network
);
1761 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1764 r
= sd_netlink_message_get_errno(m
);
1765 if (r
< 0 && r
!= -EEXIST
) {
1766 log_link_error_errno(link
, r
, "Could not join netdev: %m");
1767 link_enter_failed(link
);
1770 log_link_debug(link
, "Joined netdev");
1772 if (link
->enslaving
<= 0)
1778 static int link_enter_join_netdev(Link
*link
) {
1784 assert(link
->network
);
1785 assert(link
->state
== LINK_STATE_PENDING
);
1787 link_set_state(link
, LINK_STATE_ENSLAVING
);
1791 if (!link
->network
->bridge
&&
1792 !link
->network
->bond
&&
1793 hashmap_isempty(link
->network
->stacked_netdevs
))
1794 return link_joined(link
);
1796 if (link
->network
->bond
) {
1797 log_struct(LOG_DEBUG
,
1798 LOG_LINK_INTERFACE(link
),
1799 LOG_NETDEV_INTERFACE(link
->network
->bond
),
1800 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
),
1803 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
1805 log_struct_errno(LOG_WARNING
, r
,
1806 LOG_LINK_INTERFACE(link
),
1807 LOG_NETDEV_INTERFACE(link
->network
->bond
),
1808 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
),
1811 link_enter_failed(link
);
1818 if (link
->network
->bridge
) {
1819 log_struct(LOG_DEBUG
,
1820 LOG_LINK_INTERFACE(link
),
1821 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
1822 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
),
1825 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
1827 log_struct_errno(LOG_WARNING
, r
,
1828 LOG_LINK_INTERFACE(link
),
1829 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
1830 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
),
1832 link_enter_failed(link
);
1839 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
1841 log_struct(LOG_DEBUG
,
1842 LOG_LINK_INTERFACE(link
),
1843 LOG_NETDEV_INTERFACE(netdev
),
1844 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
),
1847 r
= netdev_join(netdev
, link
, netdev_join_handler
);
1849 log_struct_errno(LOG_WARNING
, r
,
1850 LOG_LINK_INTERFACE(link
),
1851 LOG_NETDEV_INTERFACE(netdev
),
1852 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
),
1854 link_enter_failed(link
);
1864 static int link_set_ipv4_forward(Link
*link
) {
1867 if (!link_ipv4_forward_enabled(link
))
1870 /* We propagate the forwarding flag from one interface to the
1871 * global setting one way. This means: as long as at least one
1872 * interface was configured at any time that had IP forwarding
1873 * enabled the setting will stay on for good. We do this
1874 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
1875 * somewhat in sync (see below). */
1877 r
= write_string_file("/proc/sys/net/ipv4/ip_forward", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
1879 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
1884 static int link_set_ipv6_forward(Link
*link
) {
1887 if (!link_ipv6_forward_enabled(link
))
1890 /* On Linux, the IPv6 stack does not not know a per-interface
1891 * packet forwarding setting: either packet forwarding is on
1892 * for all, or off for all. We hence don't bother with a
1893 * per-interface setting, but simply propagate the interface
1894 * flag, if it is set, to the global flag, one-way. Note that
1895 * while IPv4 would allow a per-interface flag, we expose the
1896 * same behaviour there and also propagate the setting from
1897 * one to all, to keep things simple (see above). */
1899 r
= write_string_file("/proc/sys/net/ipv6/conf/all/forwarding", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
1901 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
1906 static int link_set_ipv6_privacy_extensions(Link
*link
) {
1907 char buf
[DECIMAL_STR_MAX(unsigned) + 1];
1908 IPv6PrivacyExtensions s
;
1909 const char *p
= NULL
;
1912 s
= link_ipv6_privacy_extensions(link
);
1916 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/use_tempaddr");
1917 xsprintf(buf
, "%u", (unsigned) link
->network
->ipv6_privacy_extensions
);
1919 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
1921 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
1926 static int link_set_ipv6_accept_ra(Link
*link
) {
1927 const char *p
= NULL
;
1930 /* Make this a NOP if IPv6 is not available */
1931 if (!socket_ipv6_is_supported())
1934 if (link
->flags
& IFF_LOOPBACK
)
1940 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/accept_ra");
1942 /* We handle router advertisments ourselves, tell the kernel to GTFO */
1943 r
= write_string_file(p
, "0", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
1945 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
1950 static int link_set_ipv6_dad_transmits(Link
*link
) {
1951 char buf
[DECIMAL_STR_MAX(int) + 1];
1952 const char *p
= NULL
;
1955 /* Make this a NOP if IPv6 is not available */
1956 if (!socket_ipv6_is_supported())
1959 if (link
->flags
& IFF_LOOPBACK
)
1965 if (link
->network
->ipv6_dad_transmits
< 0)
1968 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/dad_transmits");
1969 xsprintf(buf
, "%i", link
->network
->ipv6_dad_transmits
);
1971 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
1973 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
1978 static int link_set_ipv6_hop_limit(Link
*link
) {
1979 char buf
[DECIMAL_STR_MAX(int) + 1];
1980 const char *p
= NULL
;
1983 /* Make this a NOP if IPv6 is not available */
1984 if (!socket_ipv6_is_supported())
1987 if (link
->flags
& IFF_LOOPBACK
)
1993 if (link
->network
->ipv6_hop_limit
< 0)
1996 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/hop_limit");
1997 xsprintf(buf
, "%i", link
->network
->ipv6_hop_limit
);
1999 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2001 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2006 static int link_drop_foreign_config(Link
*link
) {
2012 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
2013 /* we consider IPv6LL addresses to be managed by the kernel */
2014 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2017 r
= address_remove(address
, link
, link_address_remove_handler
);
2022 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2023 /* do not touch routes managed by the kernel */
2024 if (route
->protocol
== RTPROT_KERNEL
)
2027 r
= route_remove(route
, link
, link_address_remove_handler
);
2035 static int link_configure(Link
*link
) {
2039 assert(link
->network
);
2040 assert(link
->state
== LINK_STATE_PENDING
);
2042 /* Drop foreign config, but ignore loopback device.
2043 * We do not want to remove loopback address. */
2044 if (!(link
->flags
& IFF_LOOPBACK
)) {
2045 r
= link_drop_foreign_config(link
);
2050 r
= link_set_bridge_fdb(link
);
2054 r
= link_set_ipv4_forward(link
);
2058 r
= link_set_ipv6_forward(link
);
2062 r
= link_set_ipv6_privacy_extensions(link
);
2066 r
= link_set_ipv6_accept_ra(link
);
2070 r
= link_set_ipv6_dad_transmits(link
);
2074 r
= link_set_ipv6_hop_limit(link
);
2078 if (link_ipv4ll_enabled(link
)) {
2079 r
= ipv4ll_configure(link
);
2084 if (link_dhcp4_enabled(link
)) {
2085 r
= dhcp4_configure(link
);
2090 if (link_dhcp4_server_enabled(link
)) {
2091 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
2095 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
2100 if (link_dhcp6_enabled(link
) ||
2101 link_ipv6_accept_ra_enabled(link
)) {
2102 r
= dhcp6_configure(link
);
2107 if (link_ipv6_accept_ra_enabled(link
)) {
2108 r
= ndisc_configure(link
);
2113 if (link_lldp_enabled(link
)) {
2114 r
= sd_lldp_new(link
->ifindex
, link
->ifname
, &link
->mac
, &link
->lldp
);
2118 r
= sd_lldp_attach_event(link
->lldp
, NULL
, 0);
2122 r
= sd_lldp_set_callback(link
->lldp
,
2123 lldp_handler
, link
);
2128 if (link_has_carrier(link
)) {
2129 r
= link_acquire_conf(link
);
2133 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) == 0) {
2134 r
= link_acquire_ipv6_conf(link
);
2140 return link_enter_join_netdev(link
);
2143 static int link_initialized_and_synced(sd_netlink
*rtnl
, sd_netlink_message
*m
,
2145 _cleanup_link_unref_ Link
*link
= userdata
;
2150 assert(link
->ifname
);
2151 assert(link
->manager
);
2153 if (link
->state
!= LINK_STATE_PENDING
)
2156 log_link_debug(link
, "Link state is up-to-date");
2158 r
= link_new_bound_by_list(link
);
2162 r
= link_handle_bound_by_list(link
);
2166 if (!link
->network
) {
2167 r
= network_get(link
->manager
, link
->udev_device
, link
->ifname
,
2168 &link
->mac
, &network
);
2170 link_enter_unmanaged(link
);
2175 if (link
->flags
& IFF_LOOPBACK
) {
2176 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
2177 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
2179 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
2180 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
2182 if (network
->dhcp_server
)
2183 log_link_debug(link
, "Ignoring DHCP server for loopback link");
2186 r
= network_apply(link
->manager
, network
, link
);
2191 r
= link_new_bound_to_list(link
);
2195 r
= link_configure(link
);
2202 int link_initialized(Link
*link
, struct udev_device
*device
) {
2203 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2207 assert(link
->manager
);
2208 assert(link
->manager
->rtnl
);
2211 if (link
->state
!= LINK_STATE_PENDING
)
2214 if (link
->udev_device
)
2217 log_link_debug(link
, "udev initialized link");
2219 link
->udev_device
= udev_device_ref(device
);
2221 /* udev has initialized the link, but we don't know if we have yet
2222 * processed the NEWLINK messages with the latest state. Do a GETLINK,
2223 * when it returns we know that the pending NEWLINKs have already been
2224 * processed and that we are up-to-date */
2226 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
2231 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
,
2232 link_initialized_and_synced
, link
, 0, NULL
);
2241 static int link_load(Link
*link
) {
2242 _cleanup_free_
char *network_file
= NULL
,
2245 *dhcp4_address
= NULL
,
2246 *ipv4ll_address
= NULL
;
2247 union in_addr_union address
;
2248 union in_addr_union route_dst
;
2254 r
= parse_env_file(link
->state_file
, NEWLINE
,
2255 "NETWORK_FILE", &network_file
,
2256 "ADDRESSES", &addresses
,
2258 "DHCP4_ADDRESS", &dhcp4_address
,
2259 "IPV4LL_ADDRESS", &ipv4ll_address
,
2261 if (r
< 0 && r
!= -ENOENT
)
2262 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
2269 suffix
= strrchr(network_file
, '.');
2271 log_link_debug(link
, "Failed to get network name from %s", network_file
);
2272 goto network_file_fail
;
2276 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
2278 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
2279 goto network_file_fail
;
2282 r
= network_apply(link
->manager
, network
, link
);
2284 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
2293 _cleanup_free_
char *address_str
= NULL
;
2294 char *prefixlen_str
;
2296 unsigned char prefixlen
;
2298 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
2300 log_link_debug_errno(link
, r
, "Failed to extract next address string: %m");
2306 prefixlen_str
= strchr(address_str
, '/');
2307 if (!prefixlen_str
) {
2308 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
2312 *prefixlen_str
++ = '\0';
2314 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
2316 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
2320 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
2322 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
2326 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
2328 return log_link_error_errno(link
, r
, "Failed to add address: %m");
2337 _cleanup_free_
char *route_str
= NULL
;
2338 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
2340 char *prefixlen_str
;
2342 unsigned char prefixlen
, tos
, table
;
2345 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
2347 log_link_debug_errno(link
, r
, "Failed to extract next route string: %m");
2353 prefixlen_str
= strchr(route_str
, '/');
2354 if (!prefixlen_str
) {
2355 log_link_debug(link
, "Failed to parse route %s", route_str
);
2359 *prefixlen_str
++ = '\0';
2361 r
= sscanf(prefixlen_str
, "%hhu/%hhu/%"SCNu32
"/%hhu/"USEC_FMT
, &prefixlen
, &tos
, &priority
, &table
, &lifetime
);
2363 log_link_debug(link
,
2364 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
2369 r
= in_addr_from_string_auto(route_str
, &family
, &route_dst
);
2371 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
2375 r
= route_add(link
, family
, &route_dst
, prefixlen
, tos
, priority
, table
, &route
);
2377 return log_link_error_errno(link
, r
, "Failed to add route: %m");
2379 if (lifetime
!= USEC_INFINITY
) {
2380 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(), lifetime
,
2381 0, route_expire_handler
, route
);
2383 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
2386 route
->lifetime
= lifetime
;
2387 sd_event_source_unref(route
->expire
);
2388 route
->expire
= expire
;
2393 if (dhcp4_address
) {
2394 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
2396 log_link_debug_errno(link
, r
, "Falied to parse DHCPv4 address %s: %m", dhcp4_address
);
2397 goto dhcp4_address_fail
;
2400 r
= sd_dhcp_client_new(&link
->dhcp_client
);
2402 return log_link_error_errno(link
, r
, "Falied to create DHCPv4 client: %m");
2404 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
2406 return log_link_error_errno(link
, r
, "Falied to set inital DHCPv4 address %s: %m", dhcp4_address
);
2411 if (ipv4ll_address
) {
2412 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
2414 log_link_debug_errno(link
, r
, "Falied to parse IPv4LL address %s: %m", ipv4ll_address
);
2415 goto ipv4ll_address_fail
;
2418 r
= sd_ipv4ll_new(&link
->ipv4ll
);
2420 return log_link_error_errno(link
, r
, "Falied to create IPv4LL client: %m");
2422 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
2424 return log_link_error_errno(link
, r
, "Falied to set inital IPv4LL address %s: %m", ipv4ll_address
);
2427 ipv4ll_address_fail
:
2432 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
2434 _cleanup_udev_device_unref_
struct udev_device
*device
= NULL
;
2435 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
2443 r
= link_new(m
, message
, ret
);
2449 log_link_debug(link
, "Link %d added", link
->ifindex
);
2451 r
= link_load(link
);
2455 if (detect_container() <= 0) {
2456 /* not in a container, udev will be around */
2457 sprintf(ifindex_str
, "n%d", link
->ifindex
);
2458 device
= udev_device_new_from_device_id(m
->udev
, ifindex_str
);
2460 r
= log_link_warning_errno(link
, errno
, "Could not find udev device: %m");
2464 if (udev_device_get_is_initialized(device
) <= 0) {
2466 log_link_debug(link
, "link pending udev initialization...");
2470 r
= link_initialized(link
, device
);
2474 /* we are calling a callback directly, so must take a ref */
2477 r
= link_initialized_and_synced(m
->rtnl
, NULL
, link
);
2484 link_enter_failed(link
);
2488 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
2493 log_link_info(link
, "Gained IPv6LL");
2495 link
->ipv6ll_address
= *address
;
2496 link_check_ready(link
);
2498 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
2499 r
= link_acquire_ipv6_conf(link
);
2501 link_enter_failed(link
);
2509 static int link_carrier_gained(Link
*link
) {
2514 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
2515 r
= link_acquire_conf(link
);
2517 link_enter_failed(link
);
2522 r
= link_handle_bound_by_list(link
);
2529 static int link_carrier_lost(Link
*link
) {
2534 r
= link_stop_clients(link
);
2536 link_enter_failed(link
);
2540 r
= link_handle_bound_by_list(link
);
2547 int link_carrier_reset(Link
*link
) {
2552 if (link_has_carrier(link
)) {
2553 r
= link_carrier_lost(link
);
2557 r
= link_carrier_gained(link
);
2561 log_link_info(link
, "Reset carrier");
2568 int link_update(Link
*link
, sd_netlink_message
*m
) {
2569 struct ether_addr mac
;
2572 bool had_carrier
, carrier_gained
, carrier_lost
;
2576 assert(link
->ifname
);
2579 if (link
->state
== LINK_STATE_LINGER
) {
2581 log_link_info(link
, "Link readded");
2582 link_set_state(link
, LINK_STATE_ENSLAVING
);
2584 r
= link_new_carrier_maps(link
);
2589 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
2590 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
2591 log_link_info(link
, "Renamed to %s", ifname
);
2593 link_free_carrier_maps(link
);
2595 r
= free_and_strdup(&link
->ifname
, ifname
);
2599 r
= link_new_carrier_maps(link
);
2604 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
2605 if (r
>= 0 && mtu
> 0) {
2607 if (!link
->original_mtu
) {
2608 link
->original_mtu
= mtu
;
2609 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
2612 if (link
->dhcp_client
) {
2613 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
2616 log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
2622 /* The kernel may broadcast NEWLINK messages without the MAC address
2623 set, simply ignore them. */
2624 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
2626 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
2629 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
2632 log_link_debug(link
, "MAC address: "
2633 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
2634 mac
.ether_addr_octet
[0],
2635 mac
.ether_addr_octet
[1],
2636 mac
.ether_addr_octet
[2],
2637 mac
.ether_addr_octet
[3],
2638 mac
.ether_addr_octet
[4],
2639 mac
.ether_addr_octet
[5]);
2642 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
2644 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
2647 if (link
->dhcp_client
) {
2648 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
2649 (const uint8_t *) &link
->mac
,
2653 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
2656 if (link
->dhcp6_client
) {
2657 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
2658 (const uint8_t *) &link
->mac
,
2662 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
2667 had_carrier
= link_has_carrier(link
);
2669 r
= link_update_flags(link
, m
);
2673 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
2674 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
2676 if (carrier_gained
) {
2677 log_link_info(link
, "Gained carrier");
2679 r
= link_carrier_gained(link
);
2682 } else if (carrier_lost
) {
2683 log_link_info(link
, "Lost carrier");
2685 r
= link_carrier_lost(link
);
2694 int link_save(Link
*link
) {
2695 _cleanup_free_
char *temp_path
= NULL
;
2696 _cleanup_fclose_
FILE *f
= NULL
;
2697 const char *admin_state
, *oper_state
;
2704 assert(link
->state_file
);
2705 assert(link
->lease_file
);
2706 assert(link
->manager
);
2708 if (link
->state
== LINK_STATE_LINGER
) {
2709 unlink(link
->state_file
);
2713 admin_state
= link_state_to_string(link
->state
);
2714 assert(admin_state
);
2716 oper_state
= link_operstate_to_string(link
->operstate
);
2719 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
2723 fchmod(fileno(f
), 0644);
2726 "# This is private data. Do not parse.\n"
2729 admin_state
, oper_state
);
2731 if (link
->network
) {
2732 char **address
, **domain
;
2734 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
2736 if (link
->dhcp6_client
) {
2737 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
, &dhcp6_lease
);
2738 if (r
< 0 && r
!= -ENOMSG
)
2739 log_link_debug(link
, "No DHCPv6 lease");
2742 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
2746 STRV_FOREACH(address
, link
->network
->dns
) {
2753 if (link
->network
->dhcp_dns
&&
2755 const struct in_addr
*addresses
;
2757 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
2761 serialize_in_addrs(f
, addresses
, r
);
2766 if (link
->network
->dhcp_dns
&& dhcp6_lease
) {
2767 struct in6_addr
*in6_addrs
;
2769 r
= sd_dhcp6_lease_get_dns(dhcp6_lease
, &in6_addrs
);
2773 serialize_in6_addrs(f
, in6_addrs
, r
);
2781 STRV_FOREACH(address
, link
->network
->ntp
) {
2788 if (link
->network
->dhcp_ntp
&&
2790 const struct in_addr
*addresses
;
2792 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
2796 serialize_in_addrs(f
, addresses
, r
);
2801 if (link
->network
->dhcp_ntp
&& dhcp6_lease
) {
2802 struct in6_addr
*in6_addrs
;
2806 r
= sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease
,
2811 serialize_in6_addrs(f
, in6_addrs
, r
);
2815 r
= sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease
, &hosts
);
2817 STRV_FOREACH(hostname
, hosts
) {
2820 fputs(*hostname
, f
);
2828 fputs("DOMAINS=", f
);
2830 STRV_FOREACH(domain
, link
->network
->domains
) {
2837 if (link
->network
->dhcp_domains
&&
2839 const char *domainname
;
2841 r
= sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &domainname
);
2845 fputs(domainname
, f
);
2850 if (link
->network
->dhcp_domains
&& dhcp6_lease
) {
2853 r
= sd_dhcp6_lease_get_domains(dhcp6_lease
, &domains
);
2855 STRV_FOREACH(domain
, domains
) {
2866 fprintf(f
, "WILDCARD_DOMAIN=%s\n",
2867 yes_no(link
->network
->wildcard_domain
));
2869 fprintf(f
, "LLMNR=%s\n",
2870 resolve_support_to_string(link
->network
->llmnr
));
2871 fprintf(f
, "MDNS=%s\n",
2872 resolve_support_to_string(link
->network
->mdns
));
2874 if (link
->network
->dnssec_mode
!= _DNSSEC_MODE_INVALID
)
2875 fprintf(f
, "DNSSEC=%s\n",
2876 dnssec_mode_to_string(link
->network
->dnssec_mode
));
2878 if (!set_isempty(link
->network
->dnssec_negative_trust_anchors
)) {
2881 fputs("DNSSEC_NTA=", f
);
2883 SET_FOREACH(n
, link
->network
->dnssec_negative_trust_anchors
, i
) {
2892 fputs("ADDRESSES=", f
);
2894 SET_FOREACH(a
, link
->addresses
, i
) {
2895 _cleanup_free_
char *address_str
= NULL
;
2897 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
2901 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
2906 fputs("ROUTES=", f
);
2908 SET_FOREACH(route
, link
->routes
, i
) {
2909 _cleanup_free_
char *route_str
= NULL
;
2911 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
2915 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%hhu/"USEC_FMT
, space
? " " : "", route_str
,
2916 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
2923 if (!hashmap_isempty(link
->bound_to_links
)) {
2927 fputs("CARRIER_BOUND_TO=", f
);
2928 HASHMAP_FOREACH(carrier
, link
->bound_to_links
, i
) {
2931 fputs(carrier
->ifname
, f
);
2938 if (!hashmap_isempty(link
->bound_by_links
)) {
2942 fputs("CARRIER_BOUND_BY=", f
);
2943 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
2946 fputs(carrier
->ifname
, f
);
2953 if (link
->dhcp_lease
) {
2954 struct in_addr address
;
2955 const char *tz
= NULL
;
2957 assert(link
->network
);
2959 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
2961 fprintf(f
, "TIMEZONE=%s\n", tz
);
2963 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
2965 fputs("DHCP4_ADDRESS=", f
);
2966 serialize_in_addrs(f
, &address
, 1);
2970 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
2978 unlink(link
->lease_file
);
2981 struct in_addr address
;
2983 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
2985 fputs("IPV4LL_ADDRESS=", f
);
2986 serialize_in_addrs(f
, &address
, 1);
2992 assert(link
->network
);
2994 r
= sd_lldp_save(link
->lldp
, link
->lldp_file
);
3002 unlink(link
->lldp_file
);
3004 r
= fflush_and_check(f
);
3008 if (rename(temp_path
, link
->state_file
) < 0) {
3016 (void) unlink(link
->state_file
);
3018 (void) unlink(temp_path
);
3020 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
3023 /* The serialized state in /run is no longer up-to-date. */
3024 void link_dirty(Link
*link
) {
3029 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
3031 /* allocation errors are ignored */
3034 r
= set_put(link
->manager
->dirty_links
, link
);
3036 /* allocation errors are ignored */
3042 /* The serialized state in /run is up-to-date */
3043 void link_clean(Link
*link
) {
3045 assert(link
->manager
);
3047 set_remove(link
->manager
->dirty_links
, link
);
3051 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
3052 [LINK_STATE_PENDING
] = "pending",
3053 [LINK_STATE_ENSLAVING
] = "configuring",
3054 [LINK_STATE_SETTING_ADDRESSES
] = "configuring",
3055 [LINK_STATE_SETTING_ROUTES
] = "configuring",
3056 [LINK_STATE_CONFIGURED
] = "configured",
3057 [LINK_STATE_UNMANAGED
] = "unmanaged",
3058 [LINK_STATE_FAILED
] = "failed",
3059 [LINK_STATE_LINGER
] = "linger",
3062 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);
3064 static const char* const link_operstate_table
[_LINK_OPERSTATE_MAX
] = {
3065 [LINK_OPERSTATE_OFF
] = "off",
3066 [LINK_OPERSTATE_NO_CARRIER
] = "no-carrier",
3067 [LINK_OPERSTATE_DORMANT
] = "dormant",
3068 [LINK_OPERSTATE_CARRIER
] = "carrier",
3069 [LINK_OPERSTATE_DEGRADED
] = "degraded",
3070 [LINK_OPERSTATE_ROUTABLE
] = "routable",
3073 DEFINE_STRING_TABLE_LOOKUP(link_operstate
, LinkOperationalState
);