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-ipv6-proxy-ndp.h"
32 #include "networkd-lldp-tx.h"
33 #include "networkd-manager.h"
34 #include "networkd-ndisc.h"
35 #include "networkd-radv.h"
37 #include "socket-util.h"
38 #include "stdio-util.h"
39 #include "string-table.h"
40 #include "udev-util.h"
44 static bool link_dhcp6_enabled(Link
*link
) {
47 if (!socket_ipv6_is_supported())
50 if (link
->flags
& IFF_LOOPBACK
)
56 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV6
;
59 static bool link_dhcp4_enabled(Link
*link
) {
62 if (link
->flags
& IFF_LOOPBACK
)
68 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV4
;
71 static bool link_dhcp4_server_enabled(Link
*link
) {
74 if (link
->flags
& IFF_LOOPBACK
)
80 return link
->network
->dhcp_server
;
83 static bool link_ipv4ll_enabled(Link
*link
) {
86 if (link
->flags
& IFF_LOOPBACK
)
92 return link
->network
->link_local
& ADDRESS_FAMILY_IPV4
;
95 static bool link_ipv6ll_enabled(Link
*link
) {
98 if (!socket_ipv6_is_supported())
101 if (link
->flags
& IFF_LOOPBACK
)
107 return link
->network
->link_local
& ADDRESS_FAMILY_IPV6
;
110 static bool link_ipv6_enabled(Link
*link
) {
113 if (!socket_ipv6_is_supported())
116 if (link
->network
->bridge
)
119 /* DHCPv6 client will not be started if no IPv6 link-local address is configured. */
120 return link_ipv6ll_enabled(link
) || network_has_static_ipv6_addresses(link
->network
);
123 static bool link_radv_enabled(Link
*link
) {
126 if (!link_ipv6ll_enabled(link
))
129 return link
->network
->router_prefix_delegation
;
132 static bool link_lldp_rx_enabled(Link
*link
) {
135 if (link
->flags
& IFF_LOOPBACK
)
138 if (link
->iftype
!= ARPHRD_ETHER
)
144 /* LLDP should be handled on bridge slaves as those have a direct
145 * connection to their peers not on the bridge master. Linux doesn't
146 * even (by default) forward lldp packets to the bridge master.*/
147 if (streq_ptr("bridge", link
->kind
))
150 return link
->network
->lldp_mode
!= LLDP_MODE_NO
;
153 static bool link_lldp_emit_enabled(Link
*link
) {
156 if (link
->flags
& IFF_LOOPBACK
)
159 if (link
->iftype
!= ARPHRD_ETHER
)
165 return link
->network
->lldp_emit
!= LLDP_EMIT_NO
;
168 static bool link_ipv4_forward_enabled(Link
*link
) {
171 if (link
->flags
& IFF_LOOPBACK
)
177 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
180 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV4
;
183 static bool link_ipv6_forward_enabled(Link
*link
) {
186 if (!socket_ipv6_is_supported())
189 if (link
->flags
& IFF_LOOPBACK
)
195 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
198 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV6
;
201 static bool link_proxy_arp_enabled(Link
*link
) {
204 if (link
->flags
& IFF_LOOPBACK
)
210 if (link
->network
->proxy_arp
< 0)
216 static bool link_ipv6_accept_ra_enabled(Link
*link
) {
219 if (!socket_ipv6_is_supported())
222 if (link
->flags
& IFF_LOOPBACK
)
228 /* If unset use system default (enabled if local forwarding is disabled.
229 * disabled if local forwarding is enabled).
230 * If set, ignore or enforce RA independent of local forwarding state.
232 if (link
->network
->ipv6_accept_ra
< 0)
233 /* default to accept RA if ip_forward is disabled and ignore RA if ip_forward is enabled */
234 return !link_ipv6_forward_enabled(link
);
235 else if (link
->network
->ipv6_accept_ra
> 0)
236 /* accept RA even if ip_forward is enabled */
243 static IPv6PrivacyExtensions
link_ipv6_privacy_extensions(Link
*link
) {
246 if (!socket_ipv6_is_supported())
247 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
249 if (link
->flags
& IFF_LOOPBACK
)
250 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
253 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
255 return link
->network
->ipv6_privacy_extensions
;
258 static int link_enable_ipv6(Link
*link
) {
259 const char *p
= NULL
;
263 if (link
->flags
& IFF_LOOPBACK
)
266 disabled
= !link_ipv6_enabled(link
);
268 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/disable_ipv6");
270 r
= write_string_file(p
, one_zero(disabled
), WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
272 log_link_warning_errno(link
, r
, "Cannot %s IPv6 for interface %s: %m",
273 enable_disable(!disabled
), link
->ifname
);
275 log_link_info(link
, "IPv6 successfully %sd", enable_disable(!disabled
));
280 void link_update_operstate(Link
*link
) {
281 LinkOperationalState operstate
;
284 if (link
->kernel_operstate
== IF_OPER_DORMANT
)
285 operstate
= LINK_OPERSTATE_DORMANT
;
286 else if (link_has_carrier(link
)) {
288 uint8_t scope
= RT_SCOPE_NOWHERE
;
291 /* if we have carrier, check what addresses we have */
292 SET_FOREACH(address
, link
->addresses
, i
) {
293 if (!address_is_ready(address
))
296 if (address
->scope
< scope
)
297 scope
= address
->scope
;
300 /* for operstate we also take foreign addresses into account */
301 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
302 if (!address_is_ready(address
))
305 if (address
->scope
< scope
)
306 scope
= address
->scope
;
309 if (scope
< RT_SCOPE_SITE
)
310 /* universally accessible addresses found */
311 operstate
= LINK_OPERSTATE_ROUTABLE
;
312 else if (scope
< RT_SCOPE_HOST
)
313 /* only link or site local addresses found */
314 operstate
= LINK_OPERSTATE_DEGRADED
;
316 /* no useful addresses found */
317 operstate
= LINK_OPERSTATE_CARRIER
;
318 } else if (link
->flags
& IFF_UP
)
319 operstate
= LINK_OPERSTATE_NO_CARRIER
;
321 operstate
= LINK_OPERSTATE_OFF
;
323 if (link
->operstate
!= operstate
) {
324 link
->operstate
= operstate
;
325 link_send_changed(link
, "OperationalState", NULL
);
330 #define FLAG_STRING(string, flag, old, new) \
331 (((old ^ new) & flag) \
332 ? ((old & flag) ? (" -" string) : (" +" string)) \
335 static int link_update_flags(Link
*link
, sd_netlink_message
*m
) {
336 unsigned flags
, unknown_flags_added
, unknown_flags_removed
, unknown_flags
;
342 r
= sd_rtnl_message_link_get_flags(m
, &flags
);
344 return log_link_warning_errno(link
, r
, "Could not get link flags: %m");
346 r
= sd_netlink_message_read_u8(m
, IFLA_OPERSTATE
, &operstate
);
348 /* if we got a message without operstate, take it to mean
349 the state was unchanged */
350 operstate
= link
->kernel_operstate
;
352 if ((link
->flags
== flags
) && (link
->kernel_operstate
== operstate
))
355 if (link
->flags
!= flags
) {
356 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",
357 FLAG_STRING("LOOPBACK", IFF_LOOPBACK
, link
->flags
, flags
),
358 FLAG_STRING("MASTER", IFF_MASTER
, link
->flags
, flags
),
359 FLAG_STRING("SLAVE", IFF_SLAVE
, link
->flags
, flags
),
360 FLAG_STRING("UP", IFF_UP
, link
->flags
, flags
),
361 FLAG_STRING("DORMANT", IFF_DORMANT
, link
->flags
, flags
),
362 FLAG_STRING("LOWER_UP", IFF_LOWER_UP
, link
->flags
, flags
),
363 FLAG_STRING("RUNNING", IFF_RUNNING
, link
->flags
, flags
),
364 FLAG_STRING("MULTICAST", IFF_MULTICAST
, link
->flags
, flags
),
365 FLAG_STRING("BROADCAST", IFF_BROADCAST
, link
->flags
, flags
),
366 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT
, link
->flags
, flags
),
367 FLAG_STRING("PROMISC", IFF_PROMISC
, link
->flags
, flags
),
368 FLAG_STRING("ALLMULTI", IFF_ALLMULTI
, link
->flags
, flags
),
369 FLAG_STRING("PORTSEL", IFF_PORTSEL
, link
->flags
, flags
),
370 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA
, link
->flags
, flags
),
371 FLAG_STRING("DYNAMIC", IFF_DYNAMIC
, link
->flags
, flags
),
372 FLAG_STRING("NOARP", IFF_NOARP
, link
->flags
, flags
),
373 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS
, link
->flags
, flags
),
374 FLAG_STRING("DEBUG", IFF_DEBUG
, link
->flags
, flags
),
375 FLAG_STRING("ECHO", IFF_ECHO
, link
->flags
, flags
));
377 unknown_flags
= ~(IFF_LOOPBACK
| IFF_MASTER
| IFF_SLAVE
| IFF_UP
|
378 IFF_DORMANT
| IFF_LOWER_UP
| IFF_RUNNING
|
379 IFF_MULTICAST
| IFF_BROADCAST
| IFF_POINTOPOINT
|
380 IFF_PROMISC
| IFF_ALLMULTI
| IFF_PORTSEL
|
381 IFF_AUTOMEDIA
| IFF_DYNAMIC
| IFF_NOARP
|
382 IFF_NOTRAILERS
| IFF_DEBUG
| IFF_ECHO
);
383 unknown_flags_added
= ((link
->flags
^ flags
) & flags
& unknown_flags
);
384 unknown_flags_removed
= ((link
->flags
^ flags
) & link
->flags
& unknown_flags
);
386 /* link flags are currently at most 18 bits, let's align to
388 if (unknown_flags_added
)
390 "Unknown link flags gained: %#.5x (ignoring)",
391 unknown_flags_added
);
393 if (unknown_flags_removed
)
395 "Unknown link flags lost: %#.5x (ignoring)",
396 unknown_flags_removed
);
400 link
->kernel_operstate
= operstate
;
402 link_update_operstate(link
);
407 static int link_new(Manager
*manager
, sd_netlink_message
*message
, Link
**ret
) {
408 _cleanup_link_unref_ Link
*link
= NULL
;
410 const char *ifname
, *kind
= NULL
;
412 unsigned short iftype
;
418 /* check for link kind */
419 r
= sd_netlink_message_enter_container(message
, IFLA_LINKINFO
);
421 (void)sd_netlink_message_read_string(message
, IFLA_INFO_KIND
, &kind
);
422 r
= sd_netlink_message_exit_container(message
);
427 r
= sd_netlink_message_get_type(message
, &type
);
430 else if (type
!= RTM_NEWLINK
)
433 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
436 else if (ifindex
<= 0)
439 r
= sd_rtnl_message_link_get_type(message
, &iftype
);
443 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &ifname
);
447 link
= new0(Link
, 1);
452 link
->manager
= manager
;
453 link
->state
= LINK_STATE_PENDING
;
454 link
->rtnl_extended_attrs
= true;
455 link
->ifindex
= ifindex
;
456 link
->iftype
= iftype
;
457 link
->ifname
= strdup(ifname
);
462 link
->kind
= strdup(kind
);
467 r
= sd_netlink_message_read_ether_addr(message
, IFLA_ADDRESS
, &link
->mac
);
469 log_link_debug_errno(link
, r
, "MAC address not found for new device, continuing without");
471 if (asprintf(&link
->state_file
, "/run/systemd/netif/links/%d", link
->ifindex
) < 0)
474 if (asprintf(&link
->lease_file
, "/run/systemd/netif/leases/%d", link
->ifindex
) < 0)
477 if (asprintf(&link
->lldp_file
, "/run/systemd/netif/lldp/%d", link
->ifindex
) < 0)
480 r
= hashmap_ensure_allocated(&manager
->links
, NULL
);
484 r
= hashmap_put(manager
->links
, INT_TO_PTR(link
->ifindex
), link
);
488 r
= link_update_flags(link
, message
);
498 static void link_free(Link
*link
) {
506 while (!set_isempty(link
->addresses
))
507 address_free(set_first(link
->addresses
));
509 while (!set_isempty(link
->addresses_foreign
))
510 address_free(set_first(link
->addresses_foreign
));
512 link
->addresses
= set_free(link
->addresses
);
514 link
->addresses_foreign
= set_free(link
->addresses_foreign
);
516 while ((address
= link
->pool_addresses
)) {
517 LIST_REMOVE(addresses
, link
->pool_addresses
, address
);
518 address_free(address
);
521 sd_dhcp_server_unref(link
->dhcp_server
);
522 sd_dhcp_client_unref(link
->dhcp_client
);
523 sd_dhcp_lease_unref(link
->dhcp_lease
);
525 link_lldp_emit_stop(link
);
527 free(link
->lease_file
);
529 sd_lldp_unref(link
->lldp
);
530 free(link
->lldp_file
);
534 sd_ipv4ll_unref(link
->ipv4ll
);
535 sd_dhcp6_client_unref(link
->dhcp6_client
);
536 sd_ndisc_unref(link
->ndisc
);
537 sd_radv_unref(link
->radv
);
540 hashmap_remove(link
->manager
->links
, INT_TO_PTR(link
->ifindex
));
546 (void)unlink(link
->state_file
);
547 free(link
->state_file
);
549 udev_device_unref(link
->udev_device
);
551 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
)
552 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(carrier
->ifindex
));
553 hashmap_free(link
->bound_to_links
);
555 HASHMAP_FOREACH (carrier
, link
->bound_by_links
, i
)
556 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(carrier
->ifindex
));
557 hashmap_free(link
->bound_by_links
);
562 Link
*link_unref(Link
*link
) {
566 assert(link
->n_ref
> 0);
578 Link
*link_ref(Link
*link
) {
582 assert(link
->n_ref
> 0);
589 int link_get(Manager
*m
, int ifindex
, Link
**ret
) {
596 link
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
605 static void link_set_state(Link
*link
, LinkState state
) {
608 if (link
->state
== state
)
613 link_send_changed(link
, "AdministrativeState", NULL
);
616 static void link_enter_unmanaged(Link
*link
) {
619 log_link_debug(link
, "Unmanaged");
621 link_set_state(link
, LINK_STATE_UNMANAGED
);
626 static int link_stop_clients(Link
*link
) {
630 assert(link
->manager
);
631 assert(link
->manager
->event
);
633 if (link
->dhcp_client
) {
634 k
= sd_dhcp_client_stop(link
->dhcp_client
);
636 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv4 client: %m");
640 k
= sd_ipv4ll_stop(link
->ipv4ll
);
642 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 link-local: %m");
645 if (link
->dhcp6_client
) {
646 k
= sd_dhcp6_client_stop(link
->dhcp6_client
);
648 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv6 client: %m");
652 k
= sd_ndisc_stop(link
->ndisc
);
654 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Discovery: %m");
658 k
= sd_radv_stop(link
->radv
);
660 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Advertisement: %m");
663 link_lldp_emit_stop(link
);
667 void link_enter_failed(Link
*link
) {
670 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
673 log_link_warning(link
, "Failed");
675 link_set_state(link
, LINK_STATE_FAILED
);
677 link_stop_clients(link
);
682 static Address
* link_find_dhcp_server_address(Link
*link
) {
686 assert(link
->network
);
688 /* The first statically configured address if there is any */
689 LIST_FOREACH(addresses
, address
, link
->network
->static_addresses
) {
691 if (address
->family
!= AF_INET
)
694 if (in_addr_is_null(address
->family
, &address
->in_addr
))
700 /* If that didn't work, find a suitable address we got from the pool */
701 LIST_FOREACH(addresses
, address
, link
->pool_addresses
) {
702 if (address
->family
!= AF_INET
)
711 static void link_enter_configured(Link
*link
) {
713 assert(link
->network
);
715 if (link
->state
!= LINK_STATE_SETTING_ROUTES
)
718 log_link_info(link
, "Configured");
720 link_set_state(link
, LINK_STATE_CONFIGURED
);
725 void link_check_ready(Link
*link
) {
731 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
737 if (!link
->static_configured
)
740 if (link_ipv4ll_enabled(link
))
741 if (!link
->ipv4ll_address
||
745 if (link_ipv6ll_enabled(link
))
746 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) > 0)
749 if ((link_dhcp4_enabled(link
) && !link_dhcp6_enabled(link
) &&
750 !link
->dhcp4_configured
) ||
751 (link_dhcp6_enabled(link
) && !link_dhcp4_enabled(link
) &&
752 !link
->dhcp6_configured
) ||
753 (link_dhcp4_enabled(link
) && link_dhcp6_enabled(link
) &&
754 !link
->dhcp4_configured
&& !link
->dhcp6_configured
))
757 if (link_ipv6_accept_ra_enabled(link
) && !link
->ndisc_configured
)
760 SET_FOREACH(a
, link
->addresses
, i
)
761 if (!address_is_ready(a
))
764 if (link
->state
!= LINK_STATE_CONFIGURED
)
765 link_enter_configured(link
);
770 static int route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
771 _cleanup_link_unref_ Link
*link
= userdata
;
774 assert(link
->link_messages
> 0);
775 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
776 LINK_STATE_SETTING_ROUTES
, LINK_STATE_FAILED
,
779 link
->link_messages
--;
781 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
784 r
= sd_netlink_message_get_errno(m
);
785 if (r
< 0 && r
!= -EEXIST
)
786 log_link_warning_errno(link
, r
, "Could not set route: %m");
788 if (link
->link_messages
== 0) {
789 log_link_debug(link
, "Routes set");
790 link
->static_configured
= true;
791 link_check_ready(link
);
797 static int link_enter_set_routes(Link
*link
) {
802 assert(link
->network
);
803 assert(link
->state
== LINK_STATE_SETTING_ADDRESSES
);
805 link_set_state(link
, LINK_STATE_SETTING_ROUTES
);
807 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
808 r
= route_configure(rt
, link
, route_handler
);
810 log_link_warning_errno(link
, r
, "Could not set routes: %m");
811 link_enter_failed(link
);
815 link
->link_messages
++;
818 if (link
->link_messages
== 0) {
819 link
->static_configured
= true;
820 link_check_ready(link
);
822 log_link_debug(link
, "Setting routes");
827 int link_route_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
828 _cleanup_link_unref_ Link
*link
= userdata
;
833 assert(link
->ifname
);
835 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
838 r
= sd_netlink_message_get_errno(m
);
839 if (r
< 0 && r
!= -ESRCH
)
840 log_link_warning_errno(link
, r
, "Could not drop route: %m");
845 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
846 _cleanup_link_unref_ Link
*link
= userdata
;
852 assert(link
->ifname
);
853 assert(link
->link_messages
> 0);
854 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
855 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
857 link
->link_messages
--;
859 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
862 r
= sd_netlink_message_get_errno(m
);
863 if (r
< 0 && r
!= -EEXIST
)
864 log_link_warning_errno(link
, r
, "could not set address: %m");
866 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
868 if (link
->link_messages
== 0) {
869 log_link_debug(link
, "Addresses set");
870 link_enter_set_routes(link
);
876 static int address_label_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
877 _cleanup_link_unref_ Link
*link
= userdata
;
883 assert(link
->ifname
);
884 assert(link
->link_messages
> 0);
886 link
->link_messages
--;
888 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
891 r
= sd_netlink_message_get_errno(m
);
892 if (r
< 0 && r
!= -EEXIST
)
893 log_link_warning_errno(link
, r
, "could not set address label: %m");
895 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
897 if (link
->link_messages
== 0) {
898 log_link_debug(link
, "Addresses label set");
899 link_enter_set_routes(link
);
905 static int link_push_uplink_dns_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
906 _cleanup_free_
struct in_addr
*addresses
= NULL
;
907 size_t n_addresses
= 0, n_allocated
= 0;
910 log_debug("Copying DNS server information from %s", link
->ifname
);
915 for (i
= 0; i
< link
->network
->n_dns
; i
++) {
918 /* Only look for IPv4 addresses */
919 if (link
->network
->dns
[i
].family
!= AF_INET
)
922 ia
= link
->network
->dns
[i
].address
.in
;
924 /* Never propagate obviously borked data */
925 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
928 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
931 addresses
[n_addresses
++] = ia
;
934 if (link
->network
->dhcp_use_dns
&& link
->dhcp_lease
) {
935 const struct in_addr
*da
= NULL
;
938 n
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &da
);
941 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
944 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
949 if (n_addresses
<= 0)
952 return sd_dhcp_server_set_dns(s
, addresses
, n_addresses
);
955 static int link_push_uplink_ntp_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
956 _cleanup_free_
struct in_addr
*addresses
= NULL
;
957 size_t n_addresses
= 0, n_allocated
= 0;
963 log_debug("Copying NTP server information from %s", link
->ifname
);
965 STRV_FOREACH(a
, link
->network
->ntp
) {
968 /* Only look for IPv4 addresses */
969 if (inet_pton(AF_INET
, *a
, &ia
) <= 0)
972 /* Never propagate obviously borked data */
973 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
976 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
979 addresses
[n_addresses
++] = ia
;
982 if (link
->network
->dhcp_use_ntp
&& link
->dhcp_lease
) {
983 const struct in_addr
*da
= NULL
;
986 n
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &da
);
989 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
992 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
997 if (n_addresses
<= 0)
1000 return sd_dhcp_server_set_ntp(s
, addresses
, n_addresses
);
1003 static int link_set_bridge_fdb(Link
*link
) {
1004 FdbEntry
*fdb_entry
;
1007 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
1008 r
= fdb_entry_configure(link
, fdb_entry
);
1010 return log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
1016 static int link_enter_set_addresses(Link
*link
) {
1017 AddressLabel
*label
;
1022 assert(link
->network
);
1023 assert(link
->state
!= _LINK_STATE_INVALID
);
1025 r
= link_set_bridge_fdb(link
);
1029 link_set_state(link
, LINK_STATE_SETTING_ADDRESSES
);
1031 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
1032 r
= address_configure(ad
, link
, address_handler
, false);
1034 log_link_warning_errno(link
, r
, "Could not set addresses: %m");
1035 link_enter_failed(link
);
1039 link
->link_messages
++;
1042 LIST_FOREACH(labels
, label
, link
->network
->address_labels
) {
1043 r
= address_label_configure(label
, link
, address_label_handler
, false);
1045 log_link_warning_errno(link
, r
, "Could not set address label: %m");
1046 link_enter_failed(link
);
1050 link
->link_messages
++;
1053 /* now that we can figure out a default address for the dhcp server,
1055 if (link_dhcp4_server_enabled(link
)) {
1057 Link
*uplink
= NULL
;
1058 bool acquired_uplink
= false;
1060 address
= link_find_dhcp_server_address(link
);
1062 log_link_warning(link
, "Failed to find suitable address for DHCPv4 server instance.");
1063 link_enter_failed(link
);
1067 /* use the server address' subnet as the pool */
1068 r
= sd_dhcp_server_configure_pool(link
->dhcp_server
, &address
->in_addr
.in
, address
->prefixlen
,
1069 link
->network
->dhcp_server_pool_offset
, link
->network
->dhcp_server_pool_size
);
1074 r = sd_dhcp_server_set_router(link->dhcp_server,
1075 &main_address->in_addr.in);
1080 if (link
->network
->dhcp_server_max_lease_time_usec
> 0) {
1081 r
= sd_dhcp_server_set_max_lease_time(
1083 DIV_ROUND_UP(link
->network
->dhcp_server_max_lease_time_usec
, USEC_PER_SEC
));
1088 if (link
->network
->dhcp_server_default_lease_time_usec
> 0) {
1089 r
= sd_dhcp_server_set_default_lease_time(
1091 DIV_ROUND_UP(link
->network
->dhcp_server_default_lease_time_usec
, USEC_PER_SEC
));
1096 if (link
->network
->dhcp_server_emit_dns
) {
1098 if (link
->network
->n_dhcp_server_dns
> 0)
1099 r
= sd_dhcp_server_set_dns(link
->dhcp_server
, link
->network
->dhcp_server_dns
, link
->network
->n_dhcp_server_dns
);
1101 uplink
= manager_find_uplink(link
->manager
, link
);
1102 acquired_uplink
= true;
1105 log_link_debug(link
, "Not emitting DNS server information on link, couldn't find suitable uplink.");
1108 r
= link_push_uplink_dns_to_dhcp_server(uplink
, link
->dhcp_server
);
1111 log_link_warning_errno(link
, r
, "Failed to set DNS server for DHCP server, ignoring: %m");
1115 if (link
->network
->dhcp_server_emit_ntp
) {
1117 if (link
->network
->n_dhcp_server_ntp
> 0)
1118 r
= sd_dhcp_server_set_ntp(link
->dhcp_server
, link
->network
->dhcp_server_ntp
, link
->network
->n_dhcp_server_ntp
);
1120 if (!acquired_uplink
)
1121 uplink
= manager_find_uplink(link
->manager
, link
);
1124 log_link_debug(link
, "Not emitting NTP server information on link, couldn't find suitable uplink.");
1127 r
= link_push_uplink_ntp_to_dhcp_server(uplink
, link
->dhcp_server
);
1131 log_link_warning_errno(link
, r
, "Failed to set NTP server for DHCP server, ignoring: %m");
1134 r
= sd_dhcp_server_set_emit_router(link
->dhcp_server
, link
->network
->dhcp_server_emit_router
);
1136 log_link_warning_errno(link
, r
, "Failed to set router emission for DHCP server: %m");
1140 if (link
->network
->dhcp_server_emit_timezone
) {
1141 _cleanup_free_
char *buffer
= NULL
;
1142 const char *tz
= NULL
;
1144 if (link
->network
->dhcp_server_timezone
)
1145 tz
= link
->network
->dhcp_server_timezone
;
1147 r
= get_timezone(&buffer
);
1149 log_warning_errno(r
, "Failed to determine timezone: %m");
1155 r
= sd_dhcp_server_set_timezone(link
->dhcp_server
, tz
);
1161 r
= sd_dhcp_server_start(link
->dhcp_server
);
1163 log_link_warning_errno(link
, r
, "Could not start DHCPv4 server instance: %m");
1165 link_enter_failed(link
);
1170 log_link_debug(link
, "Offering DHCPv4 leases");
1173 if (link
->link_messages
== 0)
1174 link_enter_set_routes(link
);
1176 log_link_debug(link
, "Setting addresses");
1181 int link_address_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1182 _cleanup_link_unref_ Link
*link
= userdata
;
1187 assert(link
->ifname
);
1189 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1192 r
= sd_netlink_message_get_errno(m
);
1193 if (r
< 0 && r
!= -EADDRNOTAVAIL
)
1194 log_link_warning_errno(link
, r
, "Could not drop address: %m");
1199 static int link_set_bridge_vlan(Link
*link
) {
1202 r
= br_vlan_configure(link
, link
->network
->pvid
, link
->network
->br_vid_bitmap
, link
->network
->br_untagged_bitmap
);
1204 log_link_error_errno(link
, r
, "Failed to assign VLANs to bridge port: %m");
1209 static int link_set_proxy_arp(Link
*link
) {
1210 const char *p
= NULL
;
1213 if (!link_proxy_arp_enabled(link
))
1216 p
= strjoina("/proc/sys/net/ipv4/conf/", link
->ifname
, "/proxy_arp");
1218 r
= write_string_file(p
, one_zero(link
->network
->proxy_arp
), WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
1220 log_link_warning_errno(link
, r
, "Cannot configure proxy ARP for interface: %m");
1225 static int link_set_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1226 _cleanup_link_unref_ Link
*link
= userdata
;
1229 log_link_debug(link
, "Set link");
1231 r
= sd_netlink_message_get_errno(m
);
1232 if (r
< 0 && r
!= -EEXIST
) {
1233 log_link_error_errno(link
, r
, "Could not join netdev: %m");
1234 link_enter_failed(link
);
1241 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1242 _cleanup_link_unref_ Link
*link
= userdata
;
1247 assert(link
->ifname
);
1249 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1252 r
= sd_netlink_message_get_errno(m
);
1254 log_link_warning_errno(link
, r
, "Could not set MTU: %m");
1259 int link_set_mtu(Link
*link
, uint32_t mtu
) {
1260 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1264 assert(link
->manager
);
1265 assert(link
->manager
->rtnl
);
1267 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1269 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1271 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1273 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1275 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1277 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, set_mtu_handler
, link
, 0, NULL
);
1279 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1286 static int set_flags_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1287 _cleanup_link_unref_ Link
*link
= userdata
;
1292 assert(link
->ifname
);
1294 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1297 r
= sd_netlink_message_get_errno(m
);
1299 log_link_warning_errno(link
, r
, "Could not set link flags: %m");
1304 static int link_set_flags(Link
*link
) {
1305 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1306 unsigned ifi_change
= 0;
1307 unsigned ifi_flags
= 0;
1311 assert(link
->manager
);
1312 assert(link
->manager
->rtnl
);
1314 if (link
->flags
& IFF_LOOPBACK
)
1320 if (link
->network
->arp
< 0)
1323 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1325 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1327 if (link
->network
->arp
>= 0) {
1328 ifi_change
|= IFF_NOARP
;
1329 ifi_flags
|= link
->network
->arp
? 0 : IFF_NOARP
;
1332 r
= sd_rtnl_message_link_set_flags(req
, ifi_flags
, ifi_change
);
1334 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1336 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, set_flags_handler
, link
, 0, NULL
);
1338 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1345 static int link_set_bridge(Link
*link
) {
1346 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1350 assert(link
->network
);
1352 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1354 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1356 r
= sd_rtnl_message_link_set_family(req
, PF_BRIDGE
);
1358 return log_link_error_errno(link
, r
, "Could not set message family: %m");
1360 r
= sd_netlink_message_open_container(req
, IFLA_PROTINFO
);
1362 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1364 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_GUARD
, !link
->network
->use_bpdu
);
1366 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_GUARD attribute: %m");
1368 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MODE
, link
->network
->hairpin
);
1370 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MODE attribute: %m");
1372 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_FAST_LEAVE
, link
->network
->fast_leave
);
1374 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_FAST_LEAVE attribute: %m");
1376 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_PROTECT
, !link
->network
->allow_port_to_be_root
);
1378 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PROTECT attribute: %m");
1380 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_UNICAST_FLOOD
, link
->network
->unicast_flood
);
1382 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_UNICAST_FLOOD attribute: %m");
1384 if (link
->network
->cost
!= 0) {
1385 r
= sd_netlink_message_append_u32(req
, IFLA_BRPORT_COST
, link
->network
->cost
);
1387 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_COST attribute: %m");
1389 if (link
->network
->priority
!= LINK_BRIDGE_PORT_PRIORITY_INVALID
) {
1390 r
= sd_netlink_message_append_u16(req
, IFLA_BRPORT_PRIORITY
, link
->network
->priority
);
1392 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PRIORITY attribute: %m");
1395 r
= sd_netlink_message_close_container(req
);
1397 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1399 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_set_handler
, link
, 0, NULL
);
1401 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1408 static int link_bond_set(Link
*link
) {
1409 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1413 assert(link
->network
);
1415 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_NEWLINK
, link
->network
->bond
->ifindex
);
1417 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1419 r
= sd_netlink_message_set_flags(req
, NLM_F_REQUEST
| NLM_F_ACK
);
1421 return log_link_error_errno(link
, r
, "Could not set netlink flags: %m");
1423 r
= sd_netlink_message_open_container(req
, IFLA_LINKINFO
);
1425 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1427 r
= sd_netlink_message_open_container_union(req
, IFLA_INFO_DATA
, "bond");
1429 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1431 if (link
->network
->active_slave
) {
1432 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_ACTIVE_SLAVE
, link
->ifindex
);
1434 return log_link_error_errno(link
, r
, "Could not append IFLA_BOND_ACTIVE_SLAVE attribute: %m");
1437 if (link
->network
->primary_slave
) {
1438 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_PRIMARY
, link
->ifindex
);
1440 return log_link_error_errno(link
, r
, "Could not append IFLA_BOND_PRIMARY attribute: %m");
1443 r
= sd_netlink_message_close_container(req
);
1445 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1447 r
= sd_netlink_message_close_container(req
);
1449 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1451 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, set_flags_handler
, link
, 0, NULL
);
1453 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1460 static int link_lldp_save(Link
*link
) {
1461 _cleanup_free_
char *temp_path
= NULL
;
1462 _cleanup_fclose_
FILE *f
= NULL
;
1463 sd_lldp_neighbor
**l
= NULL
;
1467 assert(link
->lldp_file
);
1470 (void) unlink(link
->lldp_file
);
1474 r
= sd_lldp_get_neighbors(link
->lldp
, &l
);
1478 (void) unlink(link
->lldp_file
);
1484 r
= fopen_temporary(link
->lldp_file
, &f
, &temp_path
);
1488 fchmod(fileno(f
), 0644);
1490 for (i
= 0; i
< n
; i
++) {
1495 r
= sd_lldp_neighbor_get_raw(l
[i
], &p
, &sz
);
1500 (void) fwrite(&u
, 1, sizeof(u
), f
);
1501 (void) fwrite(p
, 1, sz
, f
);
1504 r
= fflush_and_check(f
);
1508 if (rename(temp_path
, link
->lldp_file
) < 0) {
1515 (void) unlink(link
->lldp_file
);
1517 (void) unlink(temp_path
);
1519 log_link_error_errno(link
, r
, "Failed to save LLDP data to %s: %m", link
->lldp_file
);
1523 for (i
= 0; i
< n
; i
++)
1524 sd_lldp_neighbor_unref(l
[i
]);
1531 static void lldp_handler(sd_lldp
*lldp
, sd_lldp_event event
, sd_lldp_neighbor
*n
, void *userdata
) {
1532 Link
*link
= userdata
;
1537 (void) link_lldp_save(link
);
1539 if (link_lldp_emit_enabled(link
) && event
== SD_LLDP_EVENT_ADDED
) {
1540 /* If we received information about a new neighbor, restart the LLDP "fast" logic */
1542 log_link_debug(link
, "Received LLDP datagram from previously unknown neighbor, restarting 'fast' LLDP transmission.");
1544 r
= link_lldp_emit_start(link
);
1546 log_link_warning_errno(link
, r
, "Failed to restart LLDP transmission: %m");
1550 static int link_acquire_ipv6_conf(Link
*link
) {
1555 if (link_dhcp6_enabled(link
)) {
1556 assert(link
->dhcp6_client
);
1557 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1559 /* start DHCPv6 client in stateless mode */
1560 r
= dhcp6_request_address(link
, true);
1561 if (r
< 0 && r
!= -EBUSY
)
1562 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv6 lease: %m");
1564 log_link_debug(link
, "Acquiring DHCPv6 lease");
1567 if (link_ipv6_accept_ra_enabled(link
)) {
1568 assert(link
->ndisc
);
1570 log_link_debug(link
, "Discovering IPv6 routers");
1572 r
= sd_ndisc_start(link
->ndisc
);
1573 if (r
< 0 && r
!= -EBUSY
)
1574 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1577 if (link_radv_enabled(link
)) {
1579 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1581 log_link_debug(link
, "Starting IPv6 Router Advertisements");
1583 r
= sd_radv_start(link
->radv
);
1584 if (r
< 0 && r
!= -EBUSY
)
1585 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Advertisement: %m");
1591 static int link_acquire_ipv4_conf(Link
*link
) {
1595 assert(link
->network
);
1596 assert(link
->manager
);
1597 assert(link
->manager
->event
);
1599 if (link_ipv4ll_enabled(link
)) {
1600 assert(link
->ipv4ll
);
1602 log_link_debug(link
, "Acquiring IPv4 link-local address");
1604 r
= sd_ipv4ll_start(link
->ipv4ll
);
1606 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1609 if (link_dhcp4_enabled(link
)) {
1610 assert(link
->dhcp_client
);
1612 log_link_debug(link
, "Acquiring DHCPv4 lease");
1614 r
= sd_dhcp_client_start(link
->dhcp_client
);
1616 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1622 static int link_acquire_conf(Link
*link
) {
1627 r
= link_acquire_ipv4_conf(link
);
1631 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) == 0) {
1632 r
= link_acquire_ipv6_conf(link
);
1637 if (link_lldp_emit_enabled(link
)) {
1638 r
= link_lldp_emit_start(link
);
1640 return log_link_warning_errno(link
, r
, "Failed to start LLDP transmission: %m");
1646 bool link_has_carrier(Link
*link
) {
1647 /* see Documentation/networking/operstates.txt in the kernel sources */
1649 if (link
->kernel_operstate
== IF_OPER_UP
)
1652 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1653 /* operstate may not be implemented, so fall back to flags */
1654 if ((link
->flags
& IFF_LOWER_UP
) && !(link
->flags
& IFF_DORMANT
))
1660 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1661 _cleanup_link_unref_ Link
*link
= userdata
;
1666 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1669 r
= sd_netlink_message_get_errno(m
);
1671 /* we warn but don't fail the link, as it may be
1673 log_link_warning_errno(link
, r
, "Could not bring up interface: %m");
1678 int link_up(Link
*link
) {
1679 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1680 uint8_t ipv6ll_mode
;
1684 assert(link
->network
);
1685 assert(link
->manager
);
1686 assert(link
->manager
->rtnl
);
1688 log_link_debug(link
, "Bringing link up");
1690 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1692 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1694 /* set it free if not enslaved with networkd */
1695 if (!link
->network
->bridge
&& !link
->network
->bond
&& !link
->network
->vrf
) {
1696 r
= sd_netlink_message_append_u32(req
, IFLA_MASTER
, 0);
1698 return log_link_error_errno(link
, r
, "Could not append IFLA_MASTER attribute: %m");
1701 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1703 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1705 if (link
->network
->mac
) {
1706 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
1708 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
1711 /* If IPv6 not configured (no static IPv6 address and IPv6LL autoconfiguration is disabled)
1712 for this interface, or if it is a bridge slave, then disable IPv6 else enable it. */
1713 (void) link_enable_ipv6(link
);
1715 if (link
->network
->mtu
) {
1716 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
1717 on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */
1718 if (link_ipv6_enabled(link
) && link
->network
->mtu
< IPV6_MIN_MTU
) {
1720 log_link_warning(link
, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU
) ", as "
1721 "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU
) " bytes: %m");
1723 link
->network
->mtu
= IPV6_MIN_MTU
;
1726 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, link
->network
->mtu
);
1728 return log_link_error_errno(link
, r
, "Could not set MTU: %m");
1731 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1733 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1735 if (link_ipv6_enabled(link
)) {
1736 /* if the kernel lacks ipv6 support setting IFF_UP fails if any ipv6 options are passed */
1737 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1739 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1741 if (!link_ipv6ll_enabled(link
))
1742 ipv6ll_mode
= IN6_ADDR_GEN_MODE_NONE
;
1744 const char *p
= NULL
;
1745 _cleanup_free_
char *stable_secret
= NULL
;
1747 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/stable_secret");
1748 r
= read_one_line_file(p
, &stable_secret
);
1751 ipv6ll_mode
= IN6_ADDR_GEN_MODE_EUI64
;
1753 ipv6ll_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
1755 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1757 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1759 if (!in_addr_is_null(AF_INET6
, &link
->network
->ipv6_token
)) {
1760 r
= sd_netlink_message_append_in6_addr(req
, IFLA_INET6_TOKEN
, &link
->network
->ipv6_token
.in6
);
1762 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_TOKEN: %m");
1765 r
= sd_netlink_message_close_container(req
);
1767 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1770 r
= sd_netlink_message_close_container(req
);
1772 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1774 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_up_handler
, link
, 0, NULL
);
1776 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1783 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1784 _cleanup_link_unref_ Link
*link
= userdata
;
1789 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1792 r
= sd_netlink_message_get_errno(m
);
1794 log_link_warning_errno(link
, r
, "Could not bring down interface: %m");
1799 int link_down(Link
*link
) {
1800 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1804 assert(link
->manager
);
1805 assert(link
->manager
->rtnl
);
1807 log_link_debug(link
, "Bringing link down");
1809 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
1810 RTM_SETLINK
, link
->ifindex
);
1812 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1814 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
1816 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1818 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_down_handler
, link
, 0, NULL
);
1820 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1827 static int link_up_can(Link
*link
) {
1828 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1833 log_link_debug(link
, "Bringing CAN link up");
1835 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1837 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1839 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1841 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1843 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_up_handler
, link
, 0, NULL
);
1845 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1852 static int link_handle_bound_to_list(Link
*link
) {
1856 bool required_up
= false;
1857 bool link_is_up
= false;
1861 if (hashmap_isempty(link
->bound_to_links
))
1864 if (link
->flags
& IFF_UP
)
1867 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
1868 if (link_has_carrier(l
)) {
1873 if (!required_up
&& link_is_up
) {
1874 r
= link_down(link
);
1877 } else if (required_up
&& !link_is_up
) {
1886 static int link_handle_bound_by_list(Link
*link
) {
1893 if (hashmap_isempty(link
->bound_by_links
))
1896 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
1897 r
= link_handle_bound_to_list(l
);
1905 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
1911 if (link
== carrier
)
1914 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
1917 r
= hashmap_ensure_allocated(h
, NULL
);
1921 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
1928 static int link_new_bound_by_list(Link
*link
) {
1933 bool list_updated
= false;
1936 assert(link
->manager
);
1940 HASHMAP_FOREACH(carrier
, m
->links
, i
) {
1941 if (!carrier
->network
)
1944 if (strv_isempty(carrier
->network
->bind_carrier
))
1947 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
, 0)) {
1948 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
1952 list_updated
= true;
1959 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
1960 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
1964 link_dirty(carrier
);
1970 static int link_new_bound_to_list(Link
*link
) {
1975 bool list_updated
= false;
1978 assert(link
->manager
);
1983 if (strv_isempty(link
->network
->bind_carrier
))
1988 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
1989 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
, 0)) {
1990 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
1994 list_updated
= true;
2001 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
2002 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
2006 link_dirty(carrier
);
2012 static int link_new_carrier_maps(Link
*link
) {
2015 r
= link_new_bound_by_list(link
);
2019 r
= link_handle_bound_by_list(link
);
2023 r
= link_new_bound_to_list(link
);
2027 r
= link_handle_bound_to_list(link
);
2034 static void link_free_bound_to_list(Link
*link
) {
2038 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
2039 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
2041 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
2042 link_dirty(bound_to
);
2048 static void link_free_bound_by_list(Link
*link
) {
2052 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
2053 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
2055 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
2056 link_dirty(bound_by
);
2057 link_handle_bound_to_list(bound_by
);
2064 static void link_free_carrier_maps(Link
*link
) {
2065 bool list_updated
= false;
2069 if (!hashmap_isempty(link
->bound_to_links
)) {
2070 link_free_bound_to_list(link
);
2071 list_updated
= true;
2074 if (!hashmap_isempty(link
->bound_by_links
)) {
2075 link_free_bound_by_list(link
);
2076 list_updated
= true;
2085 void link_drop(Link
*link
) {
2086 if (!link
|| link
->state
== LINK_STATE_LINGER
)
2089 link_set_state(link
, LINK_STATE_LINGER
);
2091 link_free_carrier_maps(link
);
2093 log_link_debug(link
, "Link removed");
2095 (void)unlink(link
->state_file
);
2101 static int link_joined(Link
*link
) {
2105 assert(link
->network
);
2107 if (!hashmap_isempty(link
->bound_to_links
)) {
2108 r
= link_handle_bound_to_list(link
);
2111 } else if (!(link
->flags
& IFF_UP
)) {
2114 link_enter_failed(link
);
2119 if (link
->network
->bridge
) {
2120 r
= link_set_bridge(link
);
2122 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
2125 if (link
->network
->bond
) {
2126 r
= link_bond_set(link
);
2128 log_link_error_errno(link
, r
, "Could not set bond message: %m");
2131 if (link
->network
->use_br_vlan
&&
2132 (link
->network
->bridge
|| streq_ptr("bridge", link
->kind
))) {
2133 r
= link_set_bridge_vlan(link
);
2135 log_link_error_errno(link
, r
, "Could not set bridge vlan: %m");
2138 /* Skip setting up addresses until it gets carrier,
2139 or it would try to set addresses twice,
2140 which is bad for non-idempotent steps. */
2141 if (!link_has_carrier(link
))
2144 return link_enter_set_addresses(link
);
2147 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
2148 _cleanup_link_unref_ Link
*link
= userdata
;
2152 assert(link
->network
);
2156 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2159 r
= sd_netlink_message_get_errno(m
);
2160 if (r
< 0 && r
!= -EEXIST
) {
2161 log_link_error_errno(link
, r
, "Could not join netdev: %m");
2162 link_enter_failed(link
);
2165 log_link_debug(link
, "Joined netdev");
2167 if (link
->enslaving
<= 0)
2173 static int link_enter_join_netdev(Link
*link
) {
2179 assert(link
->network
);
2180 assert(link
->state
== LINK_STATE_PENDING
);
2182 link_set_state(link
, LINK_STATE_ENSLAVING
);
2186 if (!link
->network
->bridge
&&
2187 !link
->network
->bond
&&
2188 !link
->network
->vrf
&&
2189 hashmap_isempty(link
->network
->stacked_netdevs
))
2190 return link_joined(link
);
2192 if (link
->network
->bond
) {
2193 log_struct(LOG_DEBUG
,
2194 LOG_LINK_INTERFACE(link
),
2195 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2196 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
),
2199 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
2201 log_struct_errno(LOG_WARNING
, r
,
2202 LOG_LINK_INTERFACE(link
),
2203 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2204 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
),
2207 link_enter_failed(link
);
2214 if (link
->network
->bridge
) {
2215 log_struct(LOG_DEBUG
,
2216 LOG_LINK_INTERFACE(link
),
2217 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2218 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
),
2221 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
2223 log_struct_errno(LOG_WARNING
, r
,
2224 LOG_LINK_INTERFACE(link
),
2225 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2226 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
),
2228 link_enter_failed(link
);
2235 if (link
->network
->vrf
) {
2236 log_struct(LOG_DEBUG
,
2237 LOG_LINK_INTERFACE(link
),
2238 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2239 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->vrf
->ifname
),
2241 r
= netdev_join(link
->network
->vrf
, link
, netdev_join_handler
);
2243 log_struct_errno(LOG_WARNING
, r
,
2244 LOG_LINK_INTERFACE(link
),
2245 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2246 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->vrf
->ifname
),
2248 link_enter_failed(link
);
2255 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
2257 log_struct(LOG_DEBUG
,
2258 LOG_LINK_INTERFACE(link
),
2259 LOG_NETDEV_INTERFACE(netdev
),
2260 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
),
2263 r
= netdev_join(netdev
, link
, netdev_join_handler
);
2265 log_struct_errno(LOG_WARNING
, r
,
2266 LOG_LINK_INTERFACE(link
),
2267 LOG_NETDEV_INTERFACE(netdev
),
2268 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
),
2270 link_enter_failed(link
);
2280 static int link_set_ipv4_forward(Link
*link
) {
2283 if (!link_ipv4_forward_enabled(link
))
2286 /* We propagate the forwarding flag from one interface to the
2287 * global setting one way. This means: as long as at least one
2288 * interface was configured at any time that had IP forwarding
2289 * enabled the setting will stay on for good. We do this
2290 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
2291 * somewhat in sync (see below). */
2293 r
= write_string_file("/proc/sys/net/ipv4/ip_forward", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2295 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
2300 static int link_set_ipv6_forward(Link
*link
) {
2303 if (!link_ipv6_forward_enabled(link
))
2306 /* On Linux, the IPv6 stack does not know a per-interface
2307 * packet forwarding setting: either packet forwarding is on
2308 * for all, or off for all. We hence don't bother with a
2309 * per-interface setting, but simply propagate the interface
2310 * flag, if it is set, to the global flag, one-way. Note that
2311 * while IPv4 would allow a per-interface flag, we expose the
2312 * same behaviour there and also propagate the setting from
2313 * one to all, to keep things simple (see above). */
2315 r
= write_string_file("/proc/sys/net/ipv6/conf/all/forwarding", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2317 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
2322 static int link_set_ipv6_privacy_extensions(Link
*link
) {
2323 char buf
[DECIMAL_STR_MAX(unsigned) + 1];
2324 IPv6PrivacyExtensions s
;
2325 const char *p
= NULL
;
2328 s
= link_ipv6_privacy_extensions(link
);
2332 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/use_tempaddr");
2333 xsprintf(buf
, "%u", (unsigned) link
->network
->ipv6_privacy_extensions
);
2335 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2337 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
2342 static int link_set_ipv6_accept_ra(Link
*link
) {
2343 const char *p
= NULL
;
2346 /* Make this a NOP if IPv6 is not available */
2347 if (!socket_ipv6_is_supported())
2350 if (link
->flags
& IFF_LOOPBACK
)
2356 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/accept_ra");
2358 /* We handle router advertisements ourselves, tell the kernel to GTFO */
2359 r
= write_string_file(p
, "0", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2361 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
2366 static int link_set_ipv6_dad_transmits(Link
*link
) {
2367 char buf
[DECIMAL_STR_MAX(int) + 1];
2368 const char *p
= NULL
;
2371 /* Make this a NOP if IPv6 is not available */
2372 if (!socket_ipv6_is_supported())
2375 if (link
->flags
& IFF_LOOPBACK
)
2381 if (link
->network
->ipv6_dad_transmits
< 0)
2384 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/dad_transmits");
2385 xsprintf(buf
, "%i", link
->network
->ipv6_dad_transmits
);
2387 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2389 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
2394 static int link_set_ipv6_hop_limit(Link
*link
) {
2395 char buf
[DECIMAL_STR_MAX(int) + 1];
2396 const char *p
= NULL
;
2399 /* Make this a NOP if IPv6 is not available */
2400 if (!socket_ipv6_is_supported())
2403 if (link
->flags
& IFF_LOOPBACK
)
2409 if (link
->network
->ipv6_hop_limit
< 0)
2412 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/hop_limit");
2413 xsprintf(buf
, "%i", link
->network
->ipv6_hop_limit
);
2415 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2417 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2422 static int link_drop_foreign_config(Link
*link
) {
2428 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
2429 /* we consider IPv6LL addresses to be managed by the kernel */
2430 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2433 r
= address_remove(address
, link
, link_address_remove_handler
);
2438 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2439 /* do not touch routes managed by the kernel */
2440 if (route
->protocol
== RTPROT_KERNEL
)
2443 r
= route_remove(route
, link
, link_route_remove_handler
);
2451 static int link_drop_config(Link
*link
) {
2452 Address
*address
, *pool_address
;
2457 SET_FOREACH(address
, link
->addresses
, i
) {
2458 /* we consider IPv6LL addresses to be managed by the kernel */
2459 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2462 r
= address_remove(address
, link
, link_address_remove_handler
);
2466 /* If this address came from an address pool, clean up the pool */
2467 LIST_FOREACH(addresses
, pool_address
, link
->pool_addresses
) {
2468 if (address_equal(address
, pool_address
)) {
2469 LIST_REMOVE(addresses
, link
->pool_addresses
, pool_address
);
2470 address_free(pool_address
);
2476 SET_FOREACH(route
, link
->routes
, i
) {
2477 /* do not touch routes managed by the kernel */
2478 if (route
->protocol
== RTPROT_KERNEL
)
2481 r
= route_remove(route
, link
, link_route_remove_handler
);
2491 static int link_update_lldp(Link
*link
) {
2499 if (link
->flags
& IFF_UP
) {
2500 r
= sd_lldp_start(link
->lldp
);
2502 log_link_debug(link
, "Started LLDP.");
2504 r
= sd_lldp_stop(link
->lldp
);
2506 log_link_debug(link
, "Stopped LLDP.");
2512 static int link_configure(Link
*link
) {
2516 assert(link
->network
);
2517 assert(link
->state
== LINK_STATE_PENDING
);
2519 if (streq_ptr(link
->kind
, "vcan")) {
2521 if (!(link
->flags
& IFF_UP
)) {
2522 r
= link_up_can(link
);
2524 link_enter_failed(link
);
2532 /* Drop foreign config, but ignore loopback or critical devices.
2533 * We do not want to remove loopback address or addresses used for root NFS. */
2534 if (!(link
->flags
& IFF_LOOPBACK
) && !(link
->network
->dhcp_critical
)) {
2535 r
= link_drop_foreign_config(link
);
2540 r
= link_set_proxy_arp(link
);
2544 r
= ipv6_proxy_ndp_addresses_configure(link
);
2548 r
= link_set_ipv4_forward(link
);
2552 r
= link_set_ipv6_forward(link
);
2556 r
= link_set_ipv6_privacy_extensions(link
);
2560 r
= link_set_ipv6_accept_ra(link
);
2564 r
= link_set_ipv6_dad_transmits(link
);
2568 r
= link_set_ipv6_hop_limit(link
);
2572 r
= link_set_flags(link
);
2576 if (link_ipv4ll_enabled(link
)) {
2577 r
= ipv4ll_configure(link
);
2582 if (link_dhcp4_enabled(link
)) {
2583 r
= dhcp4_configure(link
);
2588 if (link_dhcp4_server_enabled(link
)) {
2589 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
2593 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
2598 if (link_dhcp6_enabled(link
) ||
2599 link_ipv6_accept_ra_enabled(link
)) {
2600 r
= dhcp6_configure(link
);
2605 if (link_ipv6_accept_ra_enabled(link
)) {
2606 r
= ndisc_configure(link
);
2611 if (link_radv_enabled(link
)) {
2612 r
= radv_configure(link
);
2617 if (link_lldp_rx_enabled(link
)) {
2618 r
= sd_lldp_new(&link
->lldp
);
2622 r
= sd_lldp_set_ifindex(link
->lldp
, link
->ifindex
);
2626 r
= sd_lldp_match_capabilities(link
->lldp
,
2627 link
->network
->lldp_mode
== LLDP_MODE_ROUTERS_ONLY
?
2628 SD_LLDP_SYSTEM_CAPABILITIES_ALL_ROUTERS
:
2629 SD_LLDP_SYSTEM_CAPABILITIES_ALL
);
2633 r
= sd_lldp_set_filter_address(link
->lldp
, &link
->mac
);
2637 r
= sd_lldp_attach_event(link
->lldp
, NULL
, 0);
2641 r
= sd_lldp_set_callback(link
->lldp
, lldp_handler
, link
);
2645 r
= link_update_lldp(link
);
2650 if (link_has_carrier(link
)) {
2651 r
= link_acquire_conf(link
);
2656 return link_enter_join_netdev(link
);
2659 static int link_initialized_and_synced(sd_netlink
*rtnl
, sd_netlink_message
*m
,
2661 _cleanup_link_unref_ Link
*link
= userdata
;
2666 assert(link
->ifname
);
2667 assert(link
->manager
);
2669 if (link
->state
!= LINK_STATE_PENDING
)
2672 log_link_debug(link
, "Link state is up-to-date");
2674 r
= link_new_bound_by_list(link
);
2678 r
= link_handle_bound_by_list(link
);
2682 if (!link
->network
) {
2683 r
= network_get(link
->manager
, link
->udev_device
, link
->ifname
,
2684 &link
->mac
, &network
);
2686 link_enter_unmanaged(link
);
2688 } else if (r
== 0 && network
->unmanaged
) {
2689 link_enter_unmanaged(link
);
2694 if (link
->flags
& IFF_LOOPBACK
) {
2695 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
2696 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
2698 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
2699 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
2701 if (network
->dhcp_server
)
2702 log_link_debug(link
, "Ignoring DHCP server for loopback link");
2705 r
= network_apply(network
, link
);
2710 r
= link_new_bound_to_list(link
);
2714 r
= link_configure(link
);
2721 int link_initialized(Link
*link
, struct udev_device
*device
) {
2722 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2726 assert(link
->manager
);
2727 assert(link
->manager
->rtnl
);
2730 if (link
->state
!= LINK_STATE_PENDING
)
2733 if (link
->udev_device
)
2736 log_link_debug(link
, "udev initialized link");
2738 link
->udev_device
= udev_device_ref(device
);
2740 /* udev has initialized the link, but we don't know if we have yet
2741 * processed the NEWLINK messages with the latest state. Do a GETLINK,
2742 * when it returns we know that the pending NEWLINKs have already been
2743 * processed and that we are up-to-date */
2745 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
2750 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
,
2751 link_initialized_and_synced
, link
, 0, NULL
);
2760 static int link_load(Link
*link
) {
2761 _cleanup_free_
char *network_file
= NULL
,
2764 *dhcp4_address
= NULL
,
2765 *ipv4ll_address
= NULL
;
2766 union in_addr_union address
;
2767 union in_addr_union route_dst
;
2773 r
= parse_env_file(link
->state_file
, NEWLINE
,
2774 "NETWORK_FILE", &network_file
,
2775 "ADDRESSES", &addresses
,
2777 "DHCP4_ADDRESS", &dhcp4_address
,
2778 "IPV4LL_ADDRESS", &ipv4ll_address
,
2780 if (r
< 0 && r
!= -ENOENT
)
2781 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
2788 suffix
= strrchr(network_file
, '.');
2790 log_link_debug(link
, "Failed to get network name from %s", network_file
);
2791 goto network_file_fail
;
2795 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
2797 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
2798 goto network_file_fail
;
2801 r
= network_apply(network
, link
);
2803 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
2812 _cleanup_free_
char *address_str
= NULL
;
2813 char *prefixlen_str
;
2815 unsigned char prefixlen
;
2817 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
2819 log_link_debug_errno(link
, r
, "Failed to extract next address string: %m");
2825 prefixlen_str
= strchr(address_str
, '/');
2826 if (!prefixlen_str
) {
2827 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
2831 *prefixlen_str
++ = '\0';
2833 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
2835 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
2839 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
2841 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
2845 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
2847 return log_link_error_errno(link
, r
, "Failed to add address: %m");
2856 _cleanup_free_
char *route_str
= NULL
;
2857 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
2859 char *prefixlen_str
;
2861 unsigned char prefixlen
, tos
, table
;
2864 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
2866 log_link_debug_errno(link
, r
, "Failed to extract next route string: %m");
2872 prefixlen_str
= strchr(route_str
, '/');
2873 if (!prefixlen_str
) {
2874 log_link_debug(link
, "Failed to parse route %s", route_str
);
2878 *prefixlen_str
++ = '\0';
2880 r
= sscanf(prefixlen_str
, "%hhu/%hhu/%"SCNu32
"/%hhu/"USEC_FMT
, &prefixlen
, &tos
, &priority
, &table
, &lifetime
);
2882 log_link_debug(link
,
2883 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
2888 r
= in_addr_from_string_auto(route_str
, &family
, &route_dst
);
2890 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
2894 r
= route_add(link
, family
, &route_dst
, prefixlen
, tos
, priority
, table
, &route
);
2896 return log_link_error_errno(link
, r
, "Failed to add route: %m");
2898 if (lifetime
!= USEC_INFINITY
) {
2899 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(), lifetime
,
2900 0, route_expire_handler
, route
);
2902 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
2905 route
->lifetime
= lifetime
;
2906 sd_event_source_unref(route
->expire
);
2907 route
->expire
= expire
;
2912 if (dhcp4_address
) {
2913 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
2915 log_link_debug_errno(link
, r
, "Failed to parse DHCPv4 address %s: %m", dhcp4_address
);
2916 goto dhcp4_address_fail
;
2919 r
= sd_dhcp_client_new(&link
->dhcp_client
);
2921 return log_link_error_errno(link
, r
, "Failed to create DHCPv4 client: %m");
2923 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
2925 return log_link_error_errno(link
, r
, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address
);
2930 if (ipv4ll_address
) {
2931 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
2933 log_link_debug_errno(link
, r
, "Failed to parse IPv4LL address %s: %m", ipv4ll_address
);
2934 goto ipv4ll_address_fail
;
2937 r
= sd_ipv4ll_new(&link
->ipv4ll
);
2939 return log_link_error_errno(link
, r
, "Failed to create IPv4LL client: %m");
2941 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
2943 return log_link_error_errno(link
, r
, "Failed to set initial IPv4LL address %s: %m", ipv4ll_address
);
2946 ipv4ll_address_fail
:
2951 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
2953 _cleanup_udev_device_unref_
struct udev_device
*device
= NULL
;
2954 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
2962 r
= link_new(m
, message
, ret
);
2968 log_link_debug(link
, "Link %d added", link
->ifindex
);
2970 r
= link_load(link
);
2974 if (detect_container() <= 0) {
2975 /* not in a container, udev will be around */
2976 sprintf(ifindex_str
, "n%d", link
->ifindex
);
2977 device
= udev_device_new_from_device_id(m
->udev
, ifindex_str
);
2979 r
= log_link_warning_errno(link
, errno
, "Could not find udev device: %m");
2983 if (udev_device_get_is_initialized(device
) <= 0) {
2985 log_link_debug(link
, "link pending udev initialization...");
2989 r
= link_initialized(link
, device
);
2993 /* we are calling a callback directly, so must take a ref */
2996 r
= link_initialized_and_synced(m
->rtnl
, NULL
, link
);
3003 link_enter_failed(link
);
3007 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
3012 log_link_info(link
, "Gained IPv6LL");
3014 link
->ipv6ll_address
= *address
;
3015 link_check_ready(link
);
3017 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
3018 r
= link_acquire_ipv6_conf(link
);
3020 link_enter_failed(link
);
3028 static int link_carrier_gained(Link
*link
) {
3033 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
3034 r
= link_acquire_conf(link
);
3036 link_enter_failed(link
);
3040 r
= link_enter_set_addresses(link
);
3045 r
= link_handle_bound_by_list(link
);
3052 static int link_carrier_lost(Link
*link
) {
3057 r
= link_stop_clients(link
);
3059 link_enter_failed(link
);
3063 (void) sd_dhcp_server_stop(link
->dhcp_server
);
3065 r
= link_drop_config(link
);
3069 if (!IN_SET(link
->state
, LINK_STATE_UNMANAGED
, LINK_STATE_PENDING
)) {
3070 log_link_debug(link
, "State is %s, dropping config", link_state_to_string(link
->state
));
3071 r
= link_drop_foreign_config(link
);
3076 r
= link_handle_bound_by_list(link
);
3083 int link_carrier_reset(Link
*link
) {
3088 if (link_has_carrier(link
)) {
3089 r
= link_carrier_lost(link
);
3093 r
= link_carrier_gained(link
);
3097 log_link_info(link
, "Reset carrier");
3103 int link_update(Link
*link
, sd_netlink_message
*m
) {
3104 struct ether_addr mac
;
3107 bool had_carrier
, carrier_gained
, carrier_lost
;
3111 assert(link
->ifname
);
3114 if (link
->state
== LINK_STATE_LINGER
) {
3116 log_link_info(link
, "Link readded");
3117 link_set_state(link
, LINK_STATE_ENSLAVING
);
3119 r
= link_new_carrier_maps(link
);
3124 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
3125 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
3126 log_link_info(link
, "Renamed to %s", ifname
);
3128 link_free_carrier_maps(link
);
3130 r
= free_and_strdup(&link
->ifname
, ifname
);
3134 r
= link_new_carrier_maps(link
);
3139 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
3140 if (r
>= 0 && mtu
> 0) {
3142 if (!link
->original_mtu
) {
3143 link
->original_mtu
= mtu
;
3144 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
3147 if (link
->dhcp_client
) {
3148 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
3151 log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
3157 r
= sd_radv_set_mtu(link
->radv
, link
->mtu
);
3159 return log_link_warning_errno(link
, r
, "Could not set MTU for Router Advertisement: %m");
3163 /* The kernel may broadcast NEWLINK messages without the MAC address
3164 set, simply ignore them. */
3165 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
3167 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3170 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3173 log_link_debug(link
, "MAC address: "
3174 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
3175 mac
.ether_addr_octet
[0],
3176 mac
.ether_addr_octet
[1],
3177 mac
.ether_addr_octet
[2],
3178 mac
.ether_addr_octet
[3],
3179 mac
.ether_addr_octet
[4],
3180 mac
.ether_addr_octet
[5]);
3183 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
3185 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
3188 if (link
->dhcp_client
) {
3189 const DUID
*duid
= link_duid(link
);
3191 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
3192 (const uint8_t *) &link
->mac
,
3196 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
3198 r
= sd_dhcp_client_set_iaid_duid(link
->dhcp_client
,
3199 link
->network
->iaid
,
3201 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3202 duid
->raw_data_len
);
3204 return log_link_warning_errno(link
, r
, "Could not update DUID/IAID in DHCP client: %m");
3207 if (link
->dhcp6_client
) {
3208 const DUID
* duid
= link_duid(link
);
3210 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
3211 (const uint8_t *) &link
->mac
,
3215 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
3217 r
= sd_dhcp6_client_set_iaid(link
->dhcp6_client
,
3218 link
->network
->iaid
);
3220 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 IAID: %m");
3222 r
= sd_dhcp6_client_set_duid(link
->dhcp6_client
,
3224 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3225 duid
->raw_data_len
);
3227 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 DUID: %m");
3231 r
= sd_radv_set_mac(link
->radv
, &link
->mac
);
3233 return log_link_warning_errno(link
, r
, "Could not update MAC for Router Advertisement: %m");
3238 had_carrier
= link_has_carrier(link
);
3240 r
= link_update_flags(link
, m
);
3244 r
= link_update_lldp(link
);
3248 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
3249 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
3251 if (carrier_gained
) {
3252 log_link_info(link
, "Gained carrier");
3254 r
= link_carrier_gained(link
);
3257 } else if (carrier_lost
) {
3258 log_link_info(link
, "Lost carrier");
3260 r
= link_carrier_lost(link
);
3268 static void print_link_hashmap(FILE *f
, const char *prefix
, Hashmap
* h
) {
3276 if (hashmap_isempty(h
))
3280 HASHMAP_FOREACH(link
, h
, i
) {
3284 fprintf(f
, "%i", link
->ifindex
);
3291 int link_save(Link
*link
) {
3292 _cleanup_free_
char *temp_path
= NULL
;
3293 _cleanup_fclose_
FILE *f
= NULL
;
3294 const char *admin_state
, *oper_state
;
3301 assert(link
->state_file
);
3302 assert(link
->lease_file
);
3303 assert(link
->manager
);
3305 if (link
->state
== LINK_STATE_LINGER
) {
3306 unlink(link
->state_file
);
3310 link_lldp_save(link
);
3312 admin_state
= link_state_to_string(link
->state
);
3313 assert(admin_state
);
3315 oper_state
= link_operstate_to_string(link
->operstate
);
3318 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
3322 (void) fchmod(fileno(f
), 0644);
3325 "# This is private data. Do not parse.\n"
3328 admin_state
, oper_state
);
3330 if (link
->network
) {
3332 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
3333 const char *dhcp_domainname
= NULL
;
3334 char **dhcp6_domains
= NULL
;
3335 char **dhcp_domains
= NULL
;
3338 if (link
->dhcp6_client
) {
3339 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
, &dhcp6_lease
);
3340 if (r
< 0 && r
!= -ENOMSG
)
3341 log_link_debug(link
, "No DHCPv6 lease");
3344 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
3349 for (j
= 0; j
< link
->network
->n_dns
; j
++) {
3350 _cleanup_free_
char *b
= NULL
;
3352 r
= in_addr_to_string(link
->network
->dns
[j
].family
,
3353 &link
->network
->dns
[j
].address
, &b
);
3355 log_debug_errno(r
, "Failed to format address, ignoring: %m");
3365 if (link
->network
->dhcp_use_dns
&&
3367 const struct in_addr
*addresses
;
3369 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
3373 serialize_in_addrs(f
, addresses
, r
);
3378 if (link
->network
->dhcp_use_dns
&& dhcp6_lease
) {
3379 struct in6_addr
*in6_addrs
;
3381 r
= sd_dhcp6_lease_get_dns(dhcp6_lease
, &in6_addrs
);
3385 serialize_in6_addrs(f
, in6_addrs
, r
);
3390 /* Make sure to flush out old entries before we use the NDISC data */
3393 if (link
->network
->dhcp_use_dns
&& link
->ndisc_rdnss
) {
3396 SET_FOREACH(dd
, link
->ndisc_rdnss
, i
) {
3400 serialize_in6_addrs(f
, &dd
->address
, 1);
3409 fputstrv(f
, link
->network
->ntp
, NULL
, &space
);
3411 if (link
->network
->dhcp_use_ntp
&&
3413 const struct in_addr
*addresses
;
3415 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
3419 serialize_in_addrs(f
, addresses
, r
);
3424 if (link
->network
->dhcp_use_ntp
&& dhcp6_lease
) {
3425 struct in6_addr
*in6_addrs
;
3428 r
= sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease
,
3433 serialize_in6_addrs(f
, in6_addrs
, r
);
3437 r
= sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease
, &hosts
);
3439 fputstrv(f
, hosts
, NULL
, &space
);
3444 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
3445 if (link
->dhcp_lease
) {
3446 (void) sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &dhcp_domainname
);
3447 (void) sd_dhcp_lease_get_search_domains(link
->dhcp_lease
, &dhcp_domains
);
3450 (void) sd_dhcp6_lease_get_domains(dhcp6_lease
, &dhcp6_domains
);
3453 fputs("DOMAINS=", f
);
3455 fputstrv(f
, link
->network
->search_domains
, NULL
, &space
);
3457 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
) {
3460 if (dhcp_domainname
)
3461 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3463 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3465 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3467 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3468 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3473 fputs("ROUTE_DOMAINS=", f
);
3475 fputstrv(f
, link
->network
->route_domains
, NULL
, &space
);
3477 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
) {
3480 if (dhcp_domainname
)
3481 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3483 fputstrv(f
, dhcp_domains
, NULL
, &space
);
3485 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3487 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3488 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3493 fprintf(f
, "LLMNR=%s\n",
3494 resolve_support_to_string(link
->network
->llmnr
));
3495 fprintf(f
, "MDNS=%s\n",
3496 resolve_support_to_string(link
->network
->mdns
));
3498 if (link
->network
->dnssec_mode
!= _DNSSEC_MODE_INVALID
)
3499 fprintf(f
, "DNSSEC=%s\n",
3500 dnssec_mode_to_string(link
->network
->dnssec_mode
));
3502 if (!set_isempty(link
->network
->dnssec_negative_trust_anchors
)) {
3505 fputs("DNSSEC_NTA=", f
);
3507 SET_FOREACH(n
, link
->network
->dnssec_negative_trust_anchors
, i
)
3508 fputs_with_space(f
, n
, NULL
, &space
);
3512 fputs("ADDRESSES=", f
);
3514 SET_FOREACH(a
, link
->addresses
, i
) {
3515 _cleanup_free_
char *address_str
= NULL
;
3517 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
3521 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
3526 fputs("ROUTES=", f
);
3528 SET_FOREACH(route
, link
->routes
, i
) {
3529 _cleanup_free_
char *route_str
= NULL
;
3531 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
3535 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%hhu/"USEC_FMT
, space
? " " : "", route_str
,
3536 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
3543 print_link_hashmap(f
, "CARRIER_BOUND_TO=", link
->bound_to_links
);
3544 print_link_hashmap(f
, "CARRIER_BOUND_BY=", link
->bound_by_links
);
3546 if (link
->dhcp_lease
) {
3547 struct in_addr address
;
3548 const char *tz
= NULL
;
3550 assert(link
->network
);
3552 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
3554 fprintf(f
, "TIMEZONE=%s\n", tz
);
3556 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
3558 fputs("DHCP4_ADDRESS=", f
);
3559 serialize_in_addrs(f
, &address
, 1);
3563 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
3571 unlink(link
->lease_file
);
3574 struct in_addr address
;
3576 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
3578 fputs("IPV4LL_ADDRESS=", f
);
3579 serialize_in_addrs(f
, &address
, 1);
3584 r
= fflush_and_check(f
);
3588 if (rename(temp_path
, link
->state_file
) < 0) {
3596 (void) unlink(link
->state_file
);
3598 (void) unlink(temp_path
);
3600 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
3603 /* The serialized state in /run is no longer up-to-date. */
3604 void link_dirty(Link
*link
) {
3609 /* mark manager dirty as link is dirty */
3610 manager_dirty(link
->manager
);
3612 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
3614 /* allocation errors are ignored */
3617 r
= set_put(link
->manager
->dirty_links
, link
);
3619 /* don't take another ref if the link was already dirty */
3625 /* The serialized state in /run is up-to-date */
3626 void link_clean(Link
*link
) {
3628 assert(link
->manager
);
3630 set_remove(link
->manager
->dirty_links
, link
);
3634 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
3635 [LINK_STATE_PENDING
] = "pending",
3636 [LINK_STATE_ENSLAVING
] = "configuring",
3637 [LINK_STATE_SETTING_ADDRESSES
] = "configuring",
3638 [LINK_STATE_SETTING_ROUTES
] = "configuring",
3639 [LINK_STATE_CONFIGURED
] = "configured",
3640 [LINK_STATE_UNMANAGED
] = "unmanaged",
3641 [LINK_STATE_FAILED
] = "failed",
3642 [LINK_STATE_LINGER
] = "linger",
3645 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);
3647 static const char* const link_operstate_table
[_LINK_OPERSTATE_MAX
] = {
3648 [LINK_OPERSTATE_OFF
] = "off",
3649 [LINK_OPERSTATE_NO_CARRIER
] = "no-carrier",
3650 [LINK_OPERSTATE_DORMANT
] = "dormant",
3651 [LINK_OPERSTATE_CARRIER
] = "carrier",
3652 [LINK_OPERSTATE_DEGRADED
] = "degraded",
3653 [LINK_OPERSTATE_ROUTABLE
] = "routable",
3656 DEFINE_STRING_TABLE_LOOKUP(link_operstate
, LinkOperationalState
);