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-lldp-tx.h"
32 #include "networkd-manager.h"
33 #include "networkd-ndisc.h"
35 #include "socket-util.h"
36 #include "stdio-util.h"
37 #include "string-table.h"
38 #include "udev-util.h"
42 static bool link_dhcp6_enabled(Link
*link
) {
45 if (!socket_ipv6_is_supported())
48 if (link
->flags
& IFF_LOOPBACK
)
54 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV6
;
57 static bool link_dhcp4_enabled(Link
*link
) {
60 if (link
->flags
& IFF_LOOPBACK
)
66 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV4
;
69 static bool link_dhcp4_server_enabled(Link
*link
) {
72 if (link
->flags
& IFF_LOOPBACK
)
78 return link
->network
->dhcp_server
;
81 static bool link_ipv4ll_enabled(Link
*link
) {
84 if (link
->flags
& IFF_LOOPBACK
)
90 return link
->network
->link_local
& ADDRESS_FAMILY_IPV4
;
93 static bool link_ipv6ll_enabled(Link
*link
) {
96 if (!socket_ipv6_is_supported())
99 if (link
->flags
& IFF_LOOPBACK
)
105 return link
->network
->link_local
& ADDRESS_FAMILY_IPV6
;
108 static bool link_ipv6_enabled(Link
*link
) {
111 if (!socket_ipv6_is_supported())
114 if (link
->network
->bridge
)
117 /* DHCPv6 client will not be started if no IPv6 link-local address is configured. */
118 return link_ipv6ll_enabled(link
) || network_has_static_ipv6_addresses(link
->network
);
121 static bool link_lldp_rx_enabled(Link
*link
) {
124 if (link
->flags
& IFF_LOOPBACK
)
127 if (link
->iftype
!= ARPHRD_ETHER
)
133 if (link
->network
->bridge
)
136 return link
->network
->lldp_mode
!= LLDP_MODE_NO
;
139 static bool link_lldp_emit_enabled(Link
*link
) {
142 if (link
->flags
& IFF_LOOPBACK
)
145 if (link
->iftype
!= ARPHRD_ETHER
)
151 return link
->network
->lldp_emit
!= LLDP_EMIT_NO
;
154 static bool link_ipv4_forward_enabled(Link
*link
) {
157 if (link
->flags
& IFF_LOOPBACK
)
163 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
166 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV4
;
169 static bool link_ipv6_forward_enabled(Link
*link
) {
172 if (!socket_ipv6_is_supported())
175 if (link
->flags
& IFF_LOOPBACK
)
181 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
184 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV6
;
187 static bool link_proxy_arp_enabled(Link
*link
) {
190 if (link
->flags
& IFF_LOOPBACK
)
196 if (link
->network
->proxy_arp
< 0)
202 static bool link_ipv6_accept_ra_enabled(Link
*link
) {
205 if (!socket_ipv6_is_supported())
208 if (link
->flags
& IFF_LOOPBACK
)
214 /* If unset use system default (enabled if local forwarding is disabled.
215 * disabled if local forwarding is enabled).
216 * If set, ignore or enforce RA independent of local forwarding state.
218 if (link
->network
->ipv6_accept_ra
< 0)
219 /* default to accept RA if ip_forward is disabled and ignore RA if ip_forward is enabled */
220 return !link_ipv6_forward_enabled(link
);
221 else if (link
->network
->ipv6_accept_ra
> 0)
222 /* accept RA even if ip_forward is enabled */
229 static IPv6PrivacyExtensions
link_ipv6_privacy_extensions(Link
*link
) {
232 if (!socket_ipv6_is_supported())
233 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
235 if (link
->flags
& IFF_LOOPBACK
)
236 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
239 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
241 return link
->network
->ipv6_privacy_extensions
;
244 static int link_enable_ipv6(Link
*link
) {
245 const char *p
= NULL
;
249 if (link
->flags
& IFF_LOOPBACK
)
252 disabled
= !link_ipv6_enabled(link
);
254 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/disable_ipv6");
256 r
= write_string_file(p
, one_zero(disabled
), WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
258 log_link_warning_errno(link
, r
, "Cannot %s IPv6 for interface %s: %m",
259 enable_disable(!disabled
), link
->ifname
);
261 log_link_info(link
, "IPv6 successfully %sd", enable_disable(!disabled
));
266 void link_update_operstate(Link
*link
) {
267 LinkOperationalState operstate
;
270 if (link
->kernel_operstate
== IF_OPER_DORMANT
)
271 operstate
= LINK_OPERSTATE_DORMANT
;
272 else if (link_has_carrier(link
)) {
274 uint8_t scope
= RT_SCOPE_NOWHERE
;
277 /* if we have carrier, check what addresses we have */
278 SET_FOREACH(address
, link
->addresses
, i
) {
279 if (!address_is_ready(address
))
282 if (address
->scope
< scope
)
283 scope
= address
->scope
;
286 /* for operstate we also take foreign addresses into account */
287 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
288 if (!address_is_ready(address
))
291 if (address
->scope
< scope
)
292 scope
= address
->scope
;
295 if (scope
< RT_SCOPE_SITE
)
296 /* universally accessible addresses found */
297 operstate
= LINK_OPERSTATE_ROUTABLE
;
298 else if (scope
< RT_SCOPE_HOST
)
299 /* only link or site local addresses found */
300 operstate
= LINK_OPERSTATE_DEGRADED
;
302 /* no useful addresses found */
303 operstate
= LINK_OPERSTATE_CARRIER
;
304 } else if (link
->flags
& IFF_UP
)
305 operstate
= LINK_OPERSTATE_NO_CARRIER
;
307 operstate
= LINK_OPERSTATE_OFF
;
309 if (link
->operstate
!= operstate
) {
310 link
->operstate
= operstate
;
311 link_send_changed(link
, "OperationalState", NULL
);
316 #define FLAG_STRING(string, flag, old, new) \
317 (((old ^ new) & flag) \
318 ? ((old & flag) ? (" -" string) : (" +" string)) \
321 static int link_update_flags(Link
*link
, sd_netlink_message
*m
) {
322 unsigned flags
, unknown_flags_added
, unknown_flags_removed
, unknown_flags
;
328 r
= sd_rtnl_message_link_get_flags(m
, &flags
);
330 return log_link_warning_errno(link
, r
, "Could not get link flags: %m");
332 r
= sd_netlink_message_read_u8(m
, IFLA_OPERSTATE
, &operstate
);
334 /* if we got a message without operstate, take it to mean
335 the state was unchanged */
336 operstate
= link
->kernel_operstate
;
338 if ((link
->flags
== flags
) && (link
->kernel_operstate
== operstate
))
341 if (link
->flags
!= flags
) {
342 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",
343 FLAG_STRING("LOOPBACK", IFF_LOOPBACK
, link
->flags
, flags
),
344 FLAG_STRING("MASTER", IFF_MASTER
, link
->flags
, flags
),
345 FLAG_STRING("SLAVE", IFF_SLAVE
, link
->flags
, flags
),
346 FLAG_STRING("UP", IFF_UP
, link
->flags
, flags
),
347 FLAG_STRING("DORMANT", IFF_DORMANT
, link
->flags
, flags
),
348 FLAG_STRING("LOWER_UP", IFF_LOWER_UP
, link
->flags
, flags
),
349 FLAG_STRING("RUNNING", IFF_RUNNING
, link
->flags
, flags
),
350 FLAG_STRING("MULTICAST", IFF_MULTICAST
, link
->flags
, flags
),
351 FLAG_STRING("BROADCAST", IFF_BROADCAST
, link
->flags
, flags
),
352 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT
, link
->flags
, flags
),
353 FLAG_STRING("PROMISC", IFF_PROMISC
, link
->flags
, flags
),
354 FLAG_STRING("ALLMULTI", IFF_ALLMULTI
, link
->flags
, flags
),
355 FLAG_STRING("PORTSEL", IFF_PORTSEL
, link
->flags
, flags
),
356 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA
, link
->flags
, flags
),
357 FLAG_STRING("DYNAMIC", IFF_DYNAMIC
, link
->flags
, flags
),
358 FLAG_STRING("NOARP", IFF_NOARP
, link
->flags
, flags
),
359 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS
, link
->flags
, flags
),
360 FLAG_STRING("DEBUG", IFF_DEBUG
, link
->flags
, flags
),
361 FLAG_STRING("ECHO", IFF_ECHO
, link
->flags
, flags
));
363 unknown_flags
= ~(IFF_LOOPBACK
| IFF_MASTER
| IFF_SLAVE
| IFF_UP
|
364 IFF_DORMANT
| IFF_LOWER_UP
| IFF_RUNNING
|
365 IFF_MULTICAST
| IFF_BROADCAST
| IFF_POINTOPOINT
|
366 IFF_PROMISC
| IFF_ALLMULTI
| IFF_PORTSEL
|
367 IFF_AUTOMEDIA
| IFF_DYNAMIC
| IFF_NOARP
|
368 IFF_NOTRAILERS
| IFF_DEBUG
| IFF_ECHO
);
369 unknown_flags_added
= ((link
->flags
^ flags
) & flags
& unknown_flags
);
370 unknown_flags_removed
= ((link
->flags
^ flags
) & link
->flags
& unknown_flags
);
372 /* link flags are currently at most 18 bits, let's align to
374 if (unknown_flags_added
)
376 "Unknown link flags gained: %#.5x (ignoring)",
377 unknown_flags_added
);
379 if (unknown_flags_removed
)
381 "Unknown link flags lost: %#.5x (ignoring)",
382 unknown_flags_removed
);
386 link
->kernel_operstate
= operstate
;
388 link_update_operstate(link
);
393 static int link_new(Manager
*manager
, sd_netlink_message
*message
, Link
**ret
) {
394 _cleanup_link_unref_ Link
*link
= NULL
;
396 const char *ifname
, *kind
= NULL
;
398 unsigned short iftype
;
404 /* check for link kind */
405 r
= sd_netlink_message_enter_container(message
, IFLA_LINKINFO
);
407 (void)sd_netlink_message_read_string(message
, IFLA_INFO_KIND
, &kind
);
408 r
= sd_netlink_message_exit_container(message
);
413 r
= sd_netlink_message_get_type(message
, &type
);
416 else if (type
!= RTM_NEWLINK
)
419 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
422 else if (ifindex
<= 0)
425 r
= sd_rtnl_message_link_get_type(message
, &iftype
);
429 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &ifname
);
433 link
= new0(Link
, 1);
438 link
->manager
= manager
;
439 link
->state
= LINK_STATE_PENDING
;
440 link
->rtnl_extended_attrs
= true;
441 link
->ifindex
= ifindex
;
442 link
->iftype
= iftype
;
443 link
->ifname
= strdup(ifname
);
448 link
->kind
= strdup(kind
);
453 r
= sd_netlink_message_read_ether_addr(message
, IFLA_ADDRESS
, &link
->mac
);
455 log_link_debug_errno(link
, r
, "MAC address not found for new device, continuing without");
457 if (asprintf(&link
->state_file
, "/run/systemd/netif/links/%d", link
->ifindex
) < 0)
460 if (asprintf(&link
->lease_file
, "/run/systemd/netif/leases/%d", link
->ifindex
) < 0)
463 if (asprintf(&link
->lldp_file
, "/run/systemd/netif/lldp/%d", link
->ifindex
) < 0)
466 r
= hashmap_ensure_allocated(&manager
->links
, NULL
);
470 r
= hashmap_put(manager
->links
, INT_TO_PTR(link
->ifindex
), link
);
474 r
= link_update_flags(link
, message
);
484 static void link_free(Link
*link
) {
492 while (!set_isempty(link
->addresses
))
493 address_free(set_first(link
->addresses
));
495 while (!set_isempty(link
->addresses_foreign
))
496 address_free(set_first(link
->addresses_foreign
));
498 link
->addresses
= set_free(link
->addresses
);
500 link
->addresses_foreign
= set_free(link
->addresses_foreign
);
502 while ((address
= link
->pool_addresses
)) {
503 LIST_REMOVE(addresses
, link
->pool_addresses
, address
);
504 address_free(address
);
507 sd_dhcp_server_unref(link
->dhcp_server
);
508 sd_dhcp_client_unref(link
->dhcp_client
);
509 sd_dhcp_lease_unref(link
->dhcp_lease
);
511 link_lldp_emit_stop(link
);
513 free(link
->lease_file
);
515 sd_lldp_unref(link
->lldp
);
516 free(link
->lldp_file
);
520 sd_ipv4ll_unref(link
->ipv4ll
);
521 sd_dhcp6_client_unref(link
->dhcp6_client
);
522 sd_ndisc_unref(link
->ndisc
);
525 hashmap_remove(link
->manager
->links
, INT_TO_PTR(link
->ifindex
));
531 (void)unlink(link
->state_file
);
532 free(link
->state_file
);
534 udev_device_unref(link
->udev_device
);
536 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
)
537 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(carrier
->ifindex
));
538 hashmap_free(link
->bound_to_links
);
540 HASHMAP_FOREACH (carrier
, link
->bound_by_links
, i
)
541 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(carrier
->ifindex
));
542 hashmap_free(link
->bound_by_links
);
547 Link
*link_unref(Link
*link
) {
551 assert(link
->n_ref
> 0);
563 Link
*link_ref(Link
*link
) {
567 assert(link
->n_ref
> 0);
574 int link_get(Manager
*m
, int ifindex
, Link
**ret
) {
581 link
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
590 static void link_set_state(Link
*link
, LinkState state
) {
593 if (link
->state
== state
)
598 link_send_changed(link
, "AdministrativeState", NULL
);
601 static void link_enter_unmanaged(Link
*link
) {
604 log_link_debug(link
, "Unmanaged");
606 link_set_state(link
, LINK_STATE_UNMANAGED
);
611 static int link_stop_clients(Link
*link
) {
615 assert(link
->manager
);
616 assert(link
->manager
->event
);
618 if (link
->dhcp_client
) {
619 k
= sd_dhcp_client_stop(link
->dhcp_client
);
621 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv4 client: %m");
625 k
= sd_ipv4ll_stop(link
->ipv4ll
);
627 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 link-local: %m");
630 if (link
->dhcp6_client
) {
631 k
= sd_dhcp6_client_stop(link
->dhcp6_client
);
633 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv6 client: %m");
637 k
= sd_ndisc_stop(link
->ndisc
);
639 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Discovery: %m");
642 link_lldp_emit_stop(link
);
646 void link_enter_failed(Link
*link
) {
649 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
652 log_link_warning(link
, "Failed");
654 link_set_state(link
, LINK_STATE_FAILED
);
656 link_stop_clients(link
);
661 static Address
* link_find_dhcp_server_address(Link
*link
) {
665 assert(link
->network
);
667 /* The first statically configured address if there is any */
668 LIST_FOREACH(addresses
, address
, link
->network
->static_addresses
) {
670 if (address
->family
!= AF_INET
)
673 if (in_addr_is_null(address
->family
, &address
->in_addr
))
679 /* If that didn't work, find a suitable address we got from the pool */
680 LIST_FOREACH(addresses
, address
, link
->pool_addresses
) {
681 if (address
->family
!= AF_INET
)
690 static void link_enter_configured(Link
*link
) {
692 assert(link
->network
);
694 if (link
->state
!= LINK_STATE_SETTING_ROUTES
)
697 log_link_info(link
, "Configured");
699 link_set_state(link
, LINK_STATE_CONFIGURED
);
704 void link_check_ready(Link
*link
) {
710 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
716 if (!link
->static_configured
)
719 if (link_ipv4ll_enabled(link
))
720 if (!link
->ipv4ll_address
||
724 if (link_ipv6ll_enabled(link
))
725 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) > 0)
728 if ((link_dhcp4_enabled(link
) && !link_dhcp6_enabled(link
) &&
729 !link
->dhcp4_configured
) ||
730 (link_dhcp6_enabled(link
) && !link_dhcp4_enabled(link
) &&
731 !link
->dhcp6_configured
) ||
732 (link_dhcp4_enabled(link
) && link_dhcp6_enabled(link
) &&
733 !link
->dhcp4_configured
&& !link
->dhcp6_configured
))
736 if (link_ipv6_accept_ra_enabled(link
) && !link
->ndisc_configured
)
739 SET_FOREACH(a
, link
->addresses
, i
)
740 if (!address_is_ready(a
))
743 if (link
->state
!= LINK_STATE_CONFIGURED
)
744 link_enter_configured(link
);
749 static int route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
750 _cleanup_link_unref_ Link
*link
= userdata
;
753 assert(link
->link_messages
> 0);
754 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
755 LINK_STATE_SETTING_ROUTES
, LINK_STATE_FAILED
,
758 link
->link_messages
--;
760 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
763 r
= sd_netlink_message_get_errno(m
);
764 if (r
< 0 && r
!= -EEXIST
)
765 log_link_warning_errno(link
, r
, "Could not set route: %m");
767 if (link
->link_messages
== 0) {
768 log_link_debug(link
, "Routes set");
769 link
->static_configured
= true;
770 link_check_ready(link
);
776 static int link_enter_set_routes(Link
*link
) {
781 assert(link
->network
);
782 assert(link
->state
== LINK_STATE_SETTING_ADDRESSES
);
784 link_set_state(link
, LINK_STATE_SETTING_ROUTES
);
786 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
787 r
= route_configure(rt
, link
, route_handler
);
789 log_link_warning_errno(link
, r
, "Could not set routes: %m");
790 link_enter_failed(link
);
794 link
->link_messages
++;
797 if (link
->link_messages
== 0) {
798 link
->static_configured
= true;
799 link_check_ready(link
);
801 log_link_debug(link
, "Setting routes");
806 int link_route_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
807 _cleanup_link_unref_ Link
*link
= userdata
;
812 assert(link
->ifname
);
814 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
817 r
= sd_netlink_message_get_errno(m
);
818 if (r
< 0 && r
!= -ESRCH
)
819 log_link_warning_errno(link
, r
, "Could not drop route: %m");
824 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
825 _cleanup_link_unref_ Link
*link
= userdata
;
831 assert(link
->ifname
);
832 assert(link
->link_messages
> 0);
833 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
834 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
836 link
->link_messages
--;
838 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
841 r
= sd_netlink_message_get_errno(m
);
842 if (r
< 0 && r
!= -EEXIST
)
843 log_link_warning_errno(link
, r
, "could not set address: %m");
845 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
847 if (link
->link_messages
== 0) {
848 log_link_debug(link
, "Addresses set");
849 link_enter_set_routes(link
);
855 static int link_push_uplink_dns_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
856 _cleanup_free_
struct in_addr
*addresses
= NULL
;
857 size_t n_addresses
= 0, n_allocated
= 0;
860 log_debug("Copying DNS server information from %s", link
->ifname
);
865 for (i
= 0; i
< link
->network
->n_dns
; i
++) {
868 /* Only look for IPv4 addresses */
869 if (link
->network
->dns
[i
].family
!= AF_INET
)
872 ia
= link
->network
->dns
[i
].address
.in
;
874 /* Never propagate obviously borked data */
875 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
878 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
881 addresses
[n_addresses
++] = ia
;
884 if (link
->network
->dhcp_use_dns
&& link
->dhcp_lease
) {
885 const struct in_addr
*da
= NULL
;
888 n
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &da
);
891 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
894 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
899 if (n_addresses
<= 0)
902 return sd_dhcp_server_set_dns(s
, addresses
, n_addresses
);
905 static int link_push_uplink_ntp_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;
913 log_debug("Copying NTP server information from %s", link
->ifname
);
915 STRV_FOREACH(a
, link
->network
->ntp
) {
918 /* Only look for IPv4 addresses */
919 if (inet_pton(AF_INET
, *a
, &ia
) <= 0)
922 /* Never propagate obviously borked data */
923 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
926 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
929 addresses
[n_addresses
++] = ia
;
932 if (link
->network
->dhcp_use_ntp
&& link
->dhcp_lease
) {
933 const struct in_addr
*da
= NULL
;
936 n
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &da
);
939 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
942 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
947 if (n_addresses
<= 0)
950 return sd_dhcp_server_set_ntp(s
, addresses
, n_addresses
);
953 static int link_set_bridge_fdb(Link
*link
) {
957 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
958 r
= fdb_entry_configure(link
, fdb_entry
);
960 return log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
966 static int link_enter_set_addresses(Link
*link
) {
971 assert(link
->network
);
972 assert(link
->state
!= _LINK_STATE_INVALID
);
974 r
= link_set_bridge_fdb(link
);
978 link_set_state(link
, LINK_STATE_SETTING_ADDRESSES
);
980 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
981 r
= address_configure(ad
, link
, address_handler
, false);
983 log_link_warning_errno(link
, r
, "Could not set addresses: %m");
984 link_enter_failed(link
);
988 link
->link_messages
++;
991 /* now that we can figure out a default address for the dhcp server,
993 if (link_dhcp4_server_enabled(link
)) {
996 bool acquired_uplink
= false;
998 address
= link_find_dhcp_server_address(link
);
1000 log_link_warning(link
, "Failed to find suitable address for DHCPv4 server instance.");
1001 link_enter_failed(link
);
1005 /* use the server address' subnet as the pool */
1006 r
= sd_dhcp_server_configure_pool(link
->dhcp_server
, &address
->in_addr
.in
, address
->prefixlen
,
1007 link
->network
->dhcp_server_pool_offset
, link
->network
->dhcp_server_pool_size
);
1012 r = sd_dhcp_server_set_router(link->dhcp_server,
1013 &main_address->in_addr.in);
1018 if (link
->network
->dhcp_server_max_lease_time_usec
> 0) {
1019 r
= sd_dhcp_server_set_max_lease_time(
1021 DIV_ROUND_UP(link
->network
->dhcp_server_max_lease_time_usec
, USEC_PER_SEC
));
1026 if (link
->network
->dhcp_server_default_lease_time_usec
> 0) {
1027 r
= sd_dhcp_server_set_default_lease_time(
1029 DIV_ROUND_UP(link
->network
->dhcp_server_default_lease_time_usec
, USEC_PER_SEC
));
1034 if (link
->network
->dhcp_server_emit_dns
) {
1036 if (link
->network
->n_dhcp_server_dns
> 0)
1037 r
= sd_dhcp_server_set_dns(link
->dhcp_server
, link
->network
->dhcp_server_dns
, link
->network
->n_dhcp_server_dns
);
1039 uplink
= manager_find_uplink(link
->manager
, link
);
1040 acquired_uplink
= true;
1043 log_link_debug(link
, "Not emitting DNS server information on link, couldn't find suitable uplink.");
1046 r
= link_push_uplink_dns_to_dhcp_server(uplink
, link
->dhcp_server
);
1049 log_link_warning_errno(link
, r
, "Failed to set DNS server for DHCP server, ignoring: %m");
1053 if (link
->network
->dhcp_server_emit_ntp
) {
1055 if (link
->network
->n_dhcp_server_ntp
> 0)
1056 r
= sd_dhcp_server_set_ntp(link
->dhcp_server
, link
->network
->dhcp_server_ntp
, link
->network
->n_dhcp_server_ntp
);
1058 if (!acquired_uplink
)
1059 uplink
= manager_find_uplink(link
->manager
, link
);
1062 log_link_debug(link
, "Not emitting NTP server information on link, couldn't find suitable uplink.");
1065 r
= link_push_uplink_ntp_to_dhcp_server(uplink
, link
->dhcp_server
);
1069 log_link_warning_errno(link
, r
, "Failed to set NTP server for DHCP server, ignoring: %m");
1072 r
= sd_dhcp_server_set_emit_router(link
->dhcp_server
, link
->network
->dhcp_server_emit_router
);
1074 log_link_warning_errno(link
, r
, "Failed to set router emission for DHCP server: %m");
1078 if (link
->network
->dhcp_server_emit_timezone
) {
1079 _cleanup_free_
char *buffer
= NULL
;
1080 const char *tz
= NULL
;
1082 if (link
->network
->dhcp_server_timezone
)
1083 tz
= link
->network
->dhcp_server_timezone
;
1085 r
= get_timezone(&buffer
);
1087 log_warning_errno(r
, "Failed to determine timezone: %m");
1093 r
= sd_dhcp_server_set_timezone(link
->dhcp_server
, tz
);
1099 r
= sd_dhcp_server_start(link
->dhcp_server
);
1101 log_link_warning_errno(link
, r
, "Could not start DHCPv4 server instance: %m");
1103 link_enter_failed(link
);
1108 log_link_debug(link
, "Offering DHCPv4 leases");
1111 if (link
->link_messages
== 0)
1112 link_enter_set_routes(link
);
1114 log_link_debug(link
, "Setting addresses");
1119 int link_address_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1120 _cleanup_link_unref_ Link
*link
= userdata
;
1125 assert(link
->ifname
);
1127 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1130 r
= sd_netlink_message_get_errno(m
);
1131 if (r
< 0 && r
!= -EADDRNOTAVAIL
)
1132 log_link_warning_errno(link
, r
, "Could not drop address: %m");
1137 static int link_set_bridge_vlan(Link
*link
) {
1140 r
= br_vlan_configure(link
, link
->network
->pvid
, link
->network
->br_vid_bitmap
, link
->network
->br_untagged_bitmap
);
1142 log_link_error_errno(link
, r
, "Failed to assign VLANs to bridge port: %m");
1147 static int link_set_proxy_arp(Link
*link
) {
1148 const char *p
= NULL
;
1151 if (!link_proxy_arp_enabled(link
))
1154 p
= strjoina("/proc/sys/net/ipv4/conf/", link
->ifname
, "/proxy_arp");
1156 r
= write_string_file(p
, one_zero(link
->network
->proxy_arp
), WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
1158 log_link_warning_errno(link
, r
, "Cannot configure proxy ARP for interface: %m");
1163 static int link_set_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1164 _cleanup_link_unref_ Link
*link
= userdata
;
1167 log_link_debug(link
, "Set link");
1169 r
= sd_netlink_message_get_errno(m
);
1170 if (r
< 0 && r
!= -EEXIST
) {
1171 log_link_error_errno(link
, r
, "Could not join netdev: %m");
1172 link_enter_failed(link
);
1179 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1180 _cleanup_link_unref_ Link
*link
= userdata
;
1185 assert(link
->ifname
);
1187 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1190 r
= sd_netlink_message_get_errno(m
);
1192 log_link_warning_errno(link
, r
, "Could not set MTU: %m");
1197 int link_set_mtu(Link
*link
, uint32_t mtu
) {
1198 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1202 assert(link
->manager
);
1203 assert(link
->manager
->rtnl
);
1205 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1207 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1209 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1211 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1213 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1215 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, set_mtu_handler
, link
, 0, NULL
);
1217 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1224 static int set_flags_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1225 _cleanup_link_unref_ Link
*link
= userdata
;
1230 assert(link
->ifname
);
1232 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1235 r
= sd_netlink_message_get_errno(m
);
1237 log_link_warning_errno(link
, r
, "Could not set link flags: %m");
1242 static int link_set_flags(Link
*link
) {
1243 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1244 unsigned ifi_change
= 0;
1245 unsigned ifi_flags
= 0;
1249 assert(link
->manager
);
1250 assert(link
->manager
->rtnl
);
1252 if (link
->flags
& IFF_LOOPBACK
)
1258 if (link
->network
->arp
< 0)
1261 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1263 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1265 if (link
->network
->arp
>= 0) {
1266 ifi_change
|= IFF_NOARP
;
1267 ifi_flags
|= link
->network
->arp
? 0 : IFF_NOARP
;
1270 r
= sd_rtnl_message_link_set_flags(req
, ifi_flags
, ifi_change
);
1272 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1274 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, set_flags_handler
, link
, 0, NULL
);
1276 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1283 static int link_set_bridge(Link
*link
) {
1284 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1288 assert(link
->network
);
1290 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1292 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1294 r
= sd_rtnl_message_link_set_family(req
, PF_BRIDGE
);
1296 return log_link_error_errno(link
, r
, "Could not set message family: %m");
1298 r
= sd_netlink_message_open_container(req
, IFLA_PROTINFO
);
1300 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1302 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_GUARD
, !link
->network
->use_bpdu
);
1304 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_GUARD attribute: %m");
1306 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MODE
, link
->network
->hairpin
);
1308 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MODE attribute: %m");
1310 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_FAST_LEAVE
, link
->network
->fast_leave
);
1312 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_FAST_LEAVE attribute: %m");
1314 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_PROTECT
, !link
->network
->allow_port_to_be_root
);
1316 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PROTECT attribute: %m");
1318 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_UNICAST_FLOOD
, link
->network
->unicast_flood
);
1320 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_UNICAST_FLOOD attribute: %m");
1322 if (link
->network
->cost
!= 0) {
1323 r
= sd_netlink_message_append_u32(req
, IFLA_BRPORT_COST
, link
->network
->cost
);
1325 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_COST attribute: %m");
1328 r
= sd_netlink_message_close_container(req
);
1330 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1332 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_set_handler
, link
, 0, NULL
);
1334 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1341 static int link_lldp_save(Link
*link
) {
1342 _cleanup_free_
char *temp_path
= NULL
;
1343 _cleanup_fclose_
FILE *f
= NULL
;
1344 sd_lldp_neighbor
**l
= NULL
;
1348 assert(link
->lldp_file
);
1351 (void) unlink(link
->lldp_file
);
1355 r
= sd_lldp_get_neighbors(link
->lldp
, &l
);
1359 (void) unlink(link
->lldp_file
);
1365 r
= fopen_temporary(link
->lldp_file
, &f
, &temp_path
);
1369 fchmod(fileno(f
), 0644);
1371 for (i
= 0; i
< n
; i
++) {
1376 r
= sd_lldp_neighbor_get_raw(l
[i
], &p
, &sz
);
1381 (void) fwrite(&u
, 1, sizeof(u
), f
);
1382 (void) fwrite(p
, 1, sz
, f
);
1385 r
= fflush_and_check(f
);
1389 if (rename(temp_path
, link
->lldp_file
) < 0) {
1396 (void) unlink(link
->lldp_file
);
1398 (void) unlink(temp_path
);
1400 log_link_error_errno(link
, r
, "Failed to save LLDP data to %s: %m", link
->lldp_file
);
1404 for (i
= 0; i
< n
; i
++)
1405 sd_lldp_neighbor_unref(l
[i
]);
1412 static void lldp_handler(sd_lldp
*lldp
, sd_lldp_event event
, sd_lldp_neighbor
*n
, void *userdata
) {
1413 Link
*link
= userdata
;
1418 (void) link_lldp_save(link
);
1420 if (link_lldp_emit_enabled(link
) && event
== SD_LLDP_EVENT_ADDED
) {
1421 /* If we received information about a new neighbor, restart the LLDP "fast" logic */
1423 log_link_debug(link
, "Received LLDP datagram from previously unknown neighbor, restarting 'fast' LLDP transmission.");
1425 r
= link_lldp_emit_start(link
);
1427 log_link_warning_errno(link
, r
, "Failed to restart LLDP transmission: %m");
1431 static int link_acquire_ipv6_conf(Link
*link
) {
1436 if (link_dhcp6_enabled(link
)) {
1437 assert(link
->dhcp6_client
);
1438 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1440 /* start DHCPv6 client in stateless mode */
1441 r
= dhcp6_request_address(link
, true);
1442 if (r
< 0 && r
!= -EBUSY
)
1443 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv6 lease: %m");
1445 log_link_debug(link
, "Acquiring DHCPv6 lease");
1448 if (link_ipv6_accept_ra_enabled(link
)) {
1449 assert(link
->ndisc
);
1451 log_link_debug(link
, "Discovering IPv6 routers");
1453 r
= sd_ndisc_start(link
->ndisc
);
1454 if (r
< 0 && r
!= -EBUSY
)
1455 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1461 static int link_acquire_ipv4_conf(Link
*link
) {
1465 assert(link
->network
);
1466 assert(link
->manager
);
1467 assert(link
->manager
->event
);
1469 if (link_ipv4ll_enabled(link
)) {
1470 assert(link
->ipv4ll
);
1472 log_link_debug(link
, "Acquiring IPv4 link-local address");
1474 r
= sd_ipv4ll_start(link
->ipv4ll
);
1476 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1479 if (link_dhcp4_enabled(link
)) {
1480 assert(link
->dhcp_client
);
1482 log_link_debug(link
, "Acquiring DHCPv4 lease");
1484 r
= sd_dhcp_client_start(link
->dhcp_client
);
1486 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1492 static int link_acquire_conf(Link
*link
) {
1497 r
= link_acquire_ipv4_conf(link
);
1501 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) == 0) {
1502 r
= link_acquire_ipv6_conf(link
);
1507 if (link_lldp_emit_enabled(link
)) {
1508 r
= link_lldp_emit_start(link
);
1510 return log_link_warning_errno(link
, r
, "Failed to start LLDP transmission: %m");
1516 bool link_has_carrier(Link
*link
) {
1517 /* see Documentation/networking/operstates.txt in the kernel sources */
1519 if (link
->kernel_operstate
== IF_OPER_UP
)
1522 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1523 /* operstate may not be implemented, so fall back to flags */
1524 if ((link
->flags
& IFF_LOWER_UP
) && !(link
->flags
& IFF_DORMANT
))
1530 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1531 _cleanup_link_unref_ Link
*link
= userdata
;
1536 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1539 r
= sd_netlink_message_get_errno(m
);
1541 /* we warn but don't fail the link, as it may be
1543 log_link_warning_errno(link
, r
, "Could not bring up interface: %m");
1548 static int link_up(Link
*link
) {
1549 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1550 uint8_t ipv6ll_mode
;
1554 assert(link
->network
);
1555 assert(link
->manager
);
1556 assert(link
->manager
->rtnl
);
1558 log_link_debug(link
, "Bringing link up");
1560 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1562 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1564 /* set it free if not enslaved with networkd */
1565 if (!link
->network
->bridge
&& !link
->network
->bond
&& !link
->network
->vrf
) {
1566 r
= sd_netlink_message_append_u32(req
, IFLA_MASTER
, 0);
1568 return log_link_error_errno(link
, r
, "Could not append IFLA_MASTER attribute: %m");
1571 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1573 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1575 if (link
->network
->mac
) {
1576 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
1578 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
1581 /* If IPv6 not configured (no static IPv6 address and IPv6LL autoconfiguration is disabled)
1582 for this interface, or if it is a bridge slave, then disable IPv6 else enable it. */
1583 (void) link_enable_ipv6(link
);
1585 if (link
->network
->mtu
) {
1586 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
1587 on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */
1588 if (link_ipv6_enabled(link
) && link
->network
->mtu
< IPV6_MIN_MTU
) {
1590 log_link_warning(link
, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU
) ", as "
1591 "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU
) " bytes: %m");
1593 link
->network
->mtu
= IPV6_MIN_MTU
;
1596 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, link
->network
->mtu
);
1598 return log_link_error_errno(link
, r
, "Could not set MTU: %m");
1601 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1603 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1605 if (link_ipv6_enabled(link
)) {
1606 /* if the kernel lacks ipv6 support setting IFF_UP fails if any ipv6 options are passed */
1607 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1609 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1611 if (!link_ipv6ll_enabled(link
))
1612 ipv6ll_mode
= IN6_ADDR_GEN_MODE_NONE
;
1614 const char *p
= NULL
;
1615 _cleanup_free_
char *stable_secret
= NULL
;
1617 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/stable_secret");
1618 r
= read_one_line_file(p
, &stable_secret
);
1621 ipv6ll_mode
= IN6_ADDR_GEN_MODE_EUI64
;
1623 ipv6ll_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
1625 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1627 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1629 if (!in_addr_is_null(AF_INET6
, &link
->network
->ipv6_token
)) {
1630 r
= sd_netlink_message_append_in6_addr(req
, IFLA_INET6_TOKEN
, &link
->network
->ipv6_token
.in6
);
1632 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_TOKEN: %m");
1635 r
= sd_netlink_message_close_container(req
);
1637 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1640 r
= sd_netlink_message_close_container(req
);
1642 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1644 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_up_handler
, link
, 0, NULL
);
1646 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1653 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1654 _cleanup_link_unref_ Link
*link
= userdata
;
1659 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1662 r
= sd_netlink_message_get_errno(m
);
1664 log_link_warning_errno(link
, r
, "Could not bring down interface: %m");
1669 static int link_down(Link
*link
) {
1670 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1674 assert(link
->manager
);
1675 assert(link
->manager
->rtnl
);
1677 log_link_debug(link
, "Bringing link down");
1679 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
1680 RTM_SETLINK
, link
->ifindex
);
1682 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1684 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
1686 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1688 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_down_handler
, link
, 0, NULL
);
1690 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1697 static int link_up_can(Link
*link
) {
1698 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1703 log_link_debug(link
, "Bringing CAN link up");
1705 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1707 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1709 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1711 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1713 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_up_handler
, link
, 0, NULL
);
1715 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1722 static int link_handle_bound_to_list(Link
*link
) {
1726 bool required_up
= false;
1727 bool link_is_up
= false;
1731 if (hashmap_isempty(link
->bound_to_links
))
1734 if (link
->flags
& IFF_UP
)
1737 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
1738 if (link_has_carrier(l
)) {
1743 if (!required_up
&& link_is_up
) {
1744 r
= link_down(link
);
1747 } else if (required_up
&& !link_is_up
) {
1756 static int link_handle_bound_by_list(Link
*link
) {
1763 if (hashmap_isempty(link
->bound_by_links
))
1766 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
1767 r
= link_handle_bound_to_list(l
);
1775 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
1781 if (link
== carrier
)
1784 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
1787 r
= hashmap_ensure_allocated(h
, NULL
);
1791 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
1798 static int link_new_bound_by_list(Link
*link
) {
1803 bool list_updated
= false;
1806 assert(link
->manager
);
1810 HASHMAP_FOREACH(carrier
, m
->links
, i
) {
1811 if (!carrier
->network
)
1814 if (strv_isempty(carrier
->network
->bind_carrier
))
1817 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
, 0)) {
1818 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
1822 list_updated
= true;
1829 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
1830 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
1834 link_dirty(carrier
);
1840 static int link_new_bound_to_list(Link
*link
) {
1845 bool list_updated
= false;
1848 assert(link
->manager
);
1853 if (strv_isempty(link
->network
->bind_carrier
))
1858 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
1859 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
, 0)) {
1860 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
1864 list_updated
= true;
1871 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
1872 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
1876 link_dirty(carrier
);
1882 static int link_new_carrier_maps(Link
*link
) {
1885 r
= link_new_bound_by_list(link
);
1889 r
= link_handle_bound_by_list(link
);
1893 r
= link_new_bound_to_list(link
);
1897 r
= link_handle_bound_to_list(link
);
1904 static void link_free_bound_to_list(Link
*link
) {
1908 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
1909 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
1911 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
1912 link_dirty(bound_to
);
1918 static void link_free_bound_by_list(Link
*link
) {
1922 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
1923 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
1925 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
1926 link_dirty(bound_by
);
1927 link_handle_bound_to_list(bound_by
);
1934 static void link_free_carrier_maps(Link
*link
) {
1935 bool list_updated
= false;
1939 if (!hashmap_isempty(link
->bound_to_links
)) {
1940 link_free_bound_to_list(link
);
1941 list_updated
= true;
1944 if (!hashmap_isempty(link
->bound_by_links
)) {
1945 link_free_bound_by_list(link
);
1946 list_updated
= true;
1955 void link_drop(Link
*link
) {
1956 if (!link
|| link
->state
== LINK_STATE_LINGER
)
1959 link_set_state(link
, LINK_STATE_LINGER
);
1961 link_free_carrier_maps(link
);
1963 log_link_debug(link
, "Link removed");
1965 (void)unlink(link
->state_file
);
1971 static int link_joined(Link
*link
) {
1975 assert(link
->network
);
1977 if (!hashmap_isempty(link
->bound_to_links
)) {
1978 r
= link_handle_bound_to_list(link
);
1981 } else if (!(link
->flags
& IFF_UP
)) {
1984 link_enter_failed(link
);
1989 if (link
->network
->bridge
) {
1990 r
= link_set_bridge(link
);
1992 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
1995 if (link
->network
->use_br_vlan
&&
1996 (link
->network
->bridge
|| streq_ptr("bridge", link
->kind
))) {
1997 r
= link_set_bridge_vlan(link
);
1999 log_link_error_errno(link
, r
, "Could not set bridge vlan: %m");
2002 return link_enter_set_addresses(link
);
2005 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
2006 _cleanup_link_unref_ Link
*link
= userdata
;
2010 assert(link
->network
);
2014 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2017 r
= sd_netlink_message_get_errno(m
);
2018 if (r
< 0 && r
!= -EEXIST
) {
2019 log_link_error_errno(link
, r
, "Could not join netdev: %m");
2020 link_enter_failed(link
);
2023 log_link_debug(link
, "Joined netdev");
2025 if (link
->enslaving
<= 0)
2031 static int link_enter_join_netdev(Link
*link
) {
2037 assert(link
->network
);
2038 assert(link
->state
== LINK_STATE_PENDING
);
2040 link_set_state(link
, LINK_STATE_ENSLAVING
);
2044 if (!link
->network
->bridge
&&
2045 !link
->network
->bond
&&
2046 !link
->network
->vrf
&&
2047 hashmap_isempty(link
->network
->stacked_netdevs
))
2048 return link_joined(link
);
2050 if (link
->network
->bond
) {
2051 log_struct(LOG_DEBUG
,
2052 LOG_LINK_INTERFACE(link
),
2053 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2054 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
),
2057 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
2059 log_struct_errno(LOG_WARNING
, r
,
2060 LOG_LINK_INTERFACE(link
),
2061 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2062 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
),
2065 link_enter_failed(link
);
2072 if (link
->network
->bridge
) {
2073 log_struct(LOG_DEBUG
,
2074 LOG_LINK_INTERFACE(link
),
2075 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2076 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
),
2079 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
2081 log_struct_errno(LOG_WARNING
, r
,
2082 LOG_LINK_INTERFACE(link
),
2083 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2084 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
),
2086 link_enter_failed(link
);
2093 if (link
->network
->vrf
) {
2094 log_struct(LOG_DEBUG
,
2095 LOG_LINK_INTERFACE(link
),
2096 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2097 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->vrf
->ifname
),
2099 r
= netdev_join(link
->network
->vrf
, link
, netdev_join_handler
);
2101 log_struct_errno(LOG_WARNING
, r
,
2102 LOG_LINK_INTERFACE(link
),
2103 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2104 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->vrf
->ifname
),
2106 link_enter_failed(link
);
2113 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
2115 log_struct(LOG_DEBUG
,
2116 LOG_LINK_INTERFACE(link
),
2117 LOG_NETDEV_INTERFACE(netdev
),
2118 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
),
2121 r
= netdev_join(netdev
, link
, netdev_join_handler
);
2123 log_struct_errno(LOG_WARNING
, r
,
2124 LOG_LINK_INTERFACE(link
),
2125 LOG_NETDEV_INTERFACE(netdev
),
2126 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
),
2128 link_enter_failed(link
);
2138 static int link_set_ipv4_forward(Link
*link
) {
2141 if (!link_ipv4_forward_enabled(link
))
2144 /* We propagate the forwarding flag from one interface to the
2145 * global setting one way. This means: as long as at least one
2146 * interface was configured at any time that had IP forwarding
2147 * enabled the setting will stay on for good. We do this
2148 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
2149 * somewhat in sync (see below). */
2151 r
= write_string_file("/proc/sys/net/ipv4/ip_forward", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2153 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
2158 static int link_set_ipv6_forward(Link
*link
) {
2161 if (!link_ipv6_forward_enabled(link
))
2164 /* On Linux, the IPv6 stack does not know a per-interface
2165 * packet forwarding setting: either packet forwarding is on
2166 * for all, or off for all. We hence don't bother with a
2167 * per-interface setting, but simply propagate the interface
2168 * flag, if it is set, to the global flag, one-way. Note that
2169 * while IPv4 would allow a per-interface flag, we expose the
2170 * same behaviour there and also propagate the setting from
2171 * one to all, to keep things simple (see above). */
2173 r
= write_string_file("/proc/sys/net/ipv6/conf/all/forwarding", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2175 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
2180 static int link_set_ipv6_privacy_extensions(Link
*link
) {
2181 char buf
[DECIMAL_STR_MAX(unsigned) + 1];
2182 IPv6PrivacyExtensions s
;
2183 const char *p
= NULL
;
2186 s
= link_ipv6_privacy_extensions(link
);
2190 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/use_tempaddr");
2191 xsprintf(buf
, "%u", (unsigned) link
->network
->ipv6_privacy_extensions
);
2193 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2195 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
2200 static int link_set_ipv6_accept_ra(Link
*link
) {
2201 const char *p
= NULL
;
2204 /* Make this a NOP if IPv6 is not available */
2205 if (!socket_ipv6_is_supported())
2208 if (link
->flags
& IFF_LOOPBACK
)
2214 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/accept_ra");
2216 /* We handle router advertisements ourselves, tell the kernel to GTFO */
2217 r
= write_string_file(p
, "0", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2219 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
2224 static int link_set_ipv6_dad_transmits(Link
*link
) {
2225 char buf
[DECIMAL_STR_MAX(int) + 1];
2226 const char *p
= NULL
;
2229 /* Make this a NOP if IPv6 is not available */
2230 if (!socket_ipv6_is_supported())
2233 if (link
->flags
& IFF_LOOPBACK
)
2239 if (link
->network
->ipv6_dad_transmits
< 0)
2242 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/dad_transmits");
2243 xsprintf(buf
, "%i", link
->network
->ipv6_dad_transmits
);
2245 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2247 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
2252 static int link_set_ipv6_hop_limit(Link
*link
) {
2253 char buf
[DECIMAL_STR_MAX(int) + 1];
2254 const char *p
= NULL
;
2257 /* Make this a NOP if IPv6 is not available */
2258 if (!socket_ipv6_is_supported())
2261 if (link
->flags
& IFF_LOOPBACK
)
2267 if (link
->network
->ipv6_hop_limit
< 0)
2270 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/hop_limit");
2271 xsprintf(buf
, "%i", link
->network
->ipv6_hop_limit
);
2273 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2275 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2280 static int link_drop_foreign_config(Link
*link
) {
2286 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
2287 /* we consider IPv6LL addresses to be managed by the kernel */
2288 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2291 r
= address_remove(address
, link
, link_address_remove_handler
);
2296 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2297 /* do not touch routes managed by the kernel */
2298 if (route
->protocol
== RTPROT_KERNEL
)
2301 r
= route_remove(route
, link
, link_route_remove_handler
);
2309 static int link_drop_config(Link
*link
) {
2315 SET_FOREACH(address
, link
->addresses
, i
) {
2316 /* we consider IPv6LL addresses to be managed by the kernel */
2317 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2320 r
= address_remove(address
, link
, link_address_remove_handler
);
2325 SET_FOREACH(route
, link
->routes
, i
) {
2326 /* do not touch routes managed by the kernel */
2327 if (route
->protocol
== RTPROT_KERNEL
)
2330 r
= route_remove(route
, link
, link_route_remove_handler
);
2340 static int link_update_lldp(Link
*link
) {
2348 if (link
->flags
& IFF_UP
) {
2349 r
= sd_lldp_start(link
->lldp
);
2351 log_link_debug(link
, "Started LLDP.");
2353 r
= sd_lldp_stop(link
->lldp
);
2355 log_link_debug(link
, "Stopped LLDP.");
2361 static int link_configure(Link
*link
) {
2365 assert(link
->network
);
2366 assert(link
->state
== LINK_STATE_PENDING
);
2368 if (streq_ptr(link
->kind
, "vcan")) {
2370 if (!(link
->flags
& IFF_UP
)) {
2371 r
= link_up_can(link
);
2373 link_enter_failed(link
);
2381 /* Drop foreign config, but ignore loopback or critical devices.
2382 * We do not want to remove loopback address or addresses used for root NFS. */
2383 if (!(link
->flags
& IFF_LOOPBACK
) && !(link
->network
->dhcp_critical
)) {
2384 r
= link_drop_foreign_config(link
);
2389 r
= link_set_proxy_arp(link
);
2393 r
= link_set_ipv4_forward(link
);
2397 r
= link_set_ipv6_forward(link
);
2401 r
= link_set_ipv6_privacy_extensions(link
);
2405 r
= link_set_ipv6_accept_ra(link
);
2409 r
= link_set_ipv6_dad_transmits(link
);
2413 r
= link_set_ipv6_hop_limit(link
);
2417 r
= link_set_flags(link
);
2421 if (link_ipv4ll_enabled(link
)) {
2422 r
= ipv4ll_configure(link
);
2427 if (link_dhcp4_enabled(link
)) {
2428 r
= dhcp4_configure(link
);
2433 if (link_dhcp4_server_enabled(link
)) {
2434 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
2438 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
2443 if (link_dhcp6_enabled(link
) ||
2444 link_ipv6_accept_ra_enabled(link
)) {
2445 r
= dhcp6_configure(link
);
2450 if (link_ipv6_accept_ra_enabled(link
)) {
2451 r
= ndisc_configure(link
);
2456 if (link_lldp_rx_enabled(link
)) {
2457 r
= sd_lldp_new(&link
->lldp
);
2461 r
= sd_lldp_set_ifindex(link
->lldp
, link
->ifindex
);
2465 r
= sd_lldp_match_capabilities(link
->lldp
,
2466 link
->network
->lldp_mode
== LLDP_MODE_ROUTERS_ONLY
?
2467 SD_LLDP_SYSTEM_CAPABILITIES_ALL_ROUTERS
:
2468 SD_LLDP_SYSTEM_CAPABILITIES_ALL
);
2472 r
= sd_lldp_set_filter_address(link
->lldp
, &link
->mac
);
2476 r
= sd_lldp_attach_event(link
->lldp
, NULL
, 0);
2480 r
= sd_lldp_set_callback(link
->lldp
, lldp_handler
, link
);
2484 r
= link_update_lldp(link
);
2489 if (link_has_carrier(link
)) {
2490 r
= link_acquire_conf(link
);
2495 return link_enter_join_netdev(link
);
2498 static int link_initialized_and_synced(sd_netlink
*rtnl
, sd_netlink_message
*m
,
2500 _cleanup_link_unref_ Link
*link
= userdata
;
2505 assert(link
->ifname
);
2506 assert(link
->manager
);
2508 if (link
->state
!= LINK_STATE_PENDING
)
2511 log_link_debug(link
, "Link state is up-to-date");
2513 r
= link_new_bound_by_list(link
);
2517 r
= link_handle_bound_by_list(link
);
2521 if (!link
->network
) {
2522 r
= network_get(link
->manager
, link
->udev_device
, link
->ifname
,
2523 &link
->mac
, &network
);
2525 link_enter_unmanaged(link
);
2527 } else if (r
== 0 && network
->unmanaged
) {
2528 link_enter_unmanaged(link
);
2533 if (link
->flags
& IFF_LOOPBACK
) {
2534 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
2535 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
2537 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
2538 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
2540 if (network
->dhcp_server
)
2541 log_link_debug(link
, "Ignoring DHCP server for loopback link");
2544 r
= network_apply(network
, link
);
2549 r
= link_new_bound_to_list(link
);
2553 r
= link_configure(link
);
2560 int link_initialized(Link
*link
, struct udev_device
*device
) {
2561 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2565 assert(link
->manager
);
2566 assert(link
->manager
->rtnl
);
2569 if (link
->state
!= LINK_STATE_PENDING
)
2572 if (link
->udev_device
)
2575 log_link_debug(link
, "udev initialized link");
2577 link
->udev_device
= udev_device_ref(device
);
2579 /* udev has initialized the link, but we don't know if we have yet
2580 * processed the NEWLINK messages with the latest state. Do a GETLINK,
2581 * when it returns we know that the pending NEWLINKs have already been
2582 * processed and that we are up-to-date */
2584 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
2589 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
,
2590 link_initialized_and_synced
, link
, 0, NULL
);
2599 static int link_load(Link
*link
) {
2600 _cleanup_free_
char *network_file
= NULL
,
2603 *dhcp4_address
= NULL
,
2604 *ipv4ll_address
= NULL
;
2605 union in_addr_union address
;
2606 union in_addr_union route_dst
;
2612 r
= parse_env_file(link
->state_file
, NEWLINE
,
2613 "NETWORK_FILE", &network_file
,
2614 "ADDRESSES", &addresses
,
2616 "DHCP4_ADDRESS", &dhcp4_address
,
2617 "IPV4LL_ADDRESS", &ipv4ll_address
,
2619 if (r
< 0 && r
!= -ENOENT
)
2620 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
2627 suffix
= strrchr(network_file
, '.');
2629 log_link_debug(link
, "Failed to get network name from %s", network_file
);
2630 goto network_file_fail
;
2634 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
2636 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
2637 goto network_file_fail
;
2640 r
= network_apply(network
, link
);
2642 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
2651 _cleanup_free_
char *address_str
= NULL
;
2652 char *prefixlen_str
;
2654 unsigned char prefixlen
;
2656 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
2658 log_link_debug_errno(link
, r
, "Failed to extract next address string: %m");
2664 prefixlen_str
= strchr(address_str
, '/');
2665 if (!prefixlen_str
) {
2666 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
2670 *prefixlen_str
++ = '\0';
2672 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
2674 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
2678 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
2680 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
2684 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
2686 return log_link_error_errno(link
, r
, "Failed to add address: %m");
2695 _cleanup_free_
char *route_str
= NULL
;
2696 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
2698 char *prefixlen_str
;
2700 unsigned char prefixlen
, tos
, table
;
2703 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
2705 log_link_debug_errno(link
, r
, "Failed to extract next route string: %m");
2711 prefixlen_str
= strchr(route_str
, '/');
2712 if (!prefixlen_str
) {
2713 log_link_debug(link
, "Failed to parse route %s", route_str
);
2717 *prefixlen_str
++ = '\0';
2719 r
= sscanf(prefixlen_str
, "%hhu/%hhu/%"SCNu32
"/%hhu/"USEC_FMT
, &prefixlen
, &tos
, &priority
, &table
, &lifetime
);
2721 log_link_debug(link
,
2722 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
2727 r
= in_addr_from_string_auto(route_str
, &family
, &route_dst
);
2729 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
2733 r
= route_add(link
, family
, &route_dst
, prefixlen
, tos
, priority
, table
, &route
);
2735 return log_link_error_errno(link
, r
, "Failed to add route: %m");
2737 if (lifetime
!= USEC_INFINITY
) {
2738 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(), lifetime
,
2739 0, route_expire_handler
, route
);
2741 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
2744 route
->lifetime
= lifetime
;
2745 sd_event_source_unref(route
->expire
);
2746 route
->expire
= expire
;
2751 if (dhcp4_address
) {
2752 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
2754 log_link_debug_errno(link
, r
, "Failed to parse DHCPv4 address %s: %m", dhcp4_address
);
2755 goto dhcp4_address_fail
;
2758 r
= sd_dhcp_client_new(&link
->dhcp_client
);
2760 return log_link_error_errno(link
, r
, "Failed to create DHCPv4 client: %m");
2762 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
2764 return log_link_error_errno(link
, r
, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address
);
2769 if (ipv4ll_address
) {
2770 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
2772 log_link_debug_errno(link
, r
, "Failed to parse IPv4LL address %s: %m", ipv4ll_address
);
2773 goto ipv4ll_address_fail
;
2776 r
= sd_ipv4ll_new(&link
->ipv4ll
);
2778 return log_link_error_errno(link
, r
, "Failed to create IPv4LL client: %m");
2780 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
2782 return log_link_error_errno(link
, r
, "Failed to set initial IPv4LL address %s: %m", ipv4ll_address
);
2785 ipv4ll_address_fail
:
2790 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
2792 _cleanup_udev_device_unref_
struct udev_device
*device
= NULL
;
2793 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
2801 r
= link_new(m
, message
, ret
);
2807 log_link_debug(link
, "Link %d added", link
->ifindex
);
2809 r
= link_load(link
);
2813 if (detect_container() <= 0) {
2814 /* not in a container, udev will be around */
2815 sprintf(ifindex_str
, "n%d", link
->ifindex
);
2816 device
= udev_device_new_from_device_id(m
->udev
, ifindex_str
);
2818 r
= log_link_warning_errno(link
, errno
, "Could not find udev device: %m");
2822 if (udev_device_get_is_initialized(device
) <= 0) {
2824 log_link_debug(link
, "link pending udev initialization...");
2828 r
= link_initialized(link
, device
);
2832 /* we are calling a callback directly, so must take a ref */
2835 r
= link_initialized_and_synced(m
->rtnl
, NULL
, link
);
2842 link_enter_failed(link
);
2846 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
2851 log_link_info(link
, "Gained IPv6LL");
2853 link
->ipv6ll_address
= *address
;
2854 link_check_ready(link
);
2856 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
2857 r
= link_acquire_ipv6_conf(link
);
2859 link_enter_failed(link
);
2867 static int link_carrier_gained(Link
*link
) {
2872 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
2873 r
= link_acquire_conf(link
);
2875 link_enter_failed(link
);
2879 r
= link_enter_set_addresses(link
);
2884 r
= link_handle_bound_by_list(link
);
2891 static int link_carrier_lost(Link
*link
) {
2896 r
= link_stop_clients(link
);
2898 link_enter_failed(link
);
2902 r
= link_drop_config(link
);
2906 if (!IN_SET(link
->state
, LINK_STATE_UNMANAGED
, LINK_STATE_PENDING
)) {
2907 log_link_debug(link
, "State is %s, dropping config", link_state_to_string(link
->state
));
2908 r
= link_drop_foreign_config(link
);
2913 r
= link_handle_bound_by_list(link
);
2920 int link_carrier_reset(Link
*link
) {
2925 if (link_has_carrier(link
)) {
2926 r
= link_carrier_lost(link
);
2930 r
= link_carrier_gained(link
);
2934 log_link_info(link
, "Reset carrier");
2940 int link_update(Link
*link
, sd_netlink_message
*m
) {
2941 struct ether_addr mac
;
2944 bool had_carrier
, carrier_gained
, carrier_lost
;
2948 assert(link
->ifname
);
2951 if (link
->state
== LINK_STATE_LINGER
) {
2953 log_link_info(link
, "Link readded");
2954 link_set_state(link
, LINK_STATE_ENSLAVING
);
2956 r
= link_new_carrier_maps(link
);
2961 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
2962 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
2963 log_link_info(link
, "Renamed to %s", ifname
);
2965 link_free_carrier_maps(link
);
2967 r
= free_and_strdup(&link
->ifname
, ifname
);
2971 r
= link_new_carrier_maps(link
);
2976 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
2977 if (r
>= 0 && mtu
> 0) {
2979 if (!link
->original_mtu
) {
2980 link
->original_mtu
= mtu
;
2981 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
2984 if (link
->dhcp_client
) {
2985 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
2988 log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
2994 /* The kernel may broadcast NEWLINK messages without the MAC address
2995 set, simply ignore them. */
2996 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
2998 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3001 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3004 log_link_debug(link
, "MAC address: "
3005 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
3006 mac
.ether_addr_octet
[0],
3007 mac
.ether_addr_octet
[1],
3008 mac
.ether_addr_octet
[2],
3009 mac
.ether_addr_octet
[3],
3010 mac
.ether_addr_octet
[4],
3011 mac
.ether_addr_octet
[5]);
3014 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
3016 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
3019 if (link
->dhcp_client
) {
3020 const DUID
*duid
= link_duid(link
);
3022 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
3023 (const uint8_t *) &link
->mac
,
3027 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
3029 r
= sd_dhcp_client_set_iaid_duid(link
->dhcp_client
,
3030 link
->network
->iaid
,
3032 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3033 duid
->raw_data_len
);
3035 return log_link_warning_errno(link
, r
, "Could not update DUID/IAID in DHCP client: %m");
3038 if (link
->dhcp6_client
) {
3039 const DUID
* duid
= link_duid(link
);
3041 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
3042 (const uint8_t *) &link
->mac
,
3046 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
3048 r
= sd_dhcp6_client_set_iaid(link
->dhcp6_client
,
3049 link
->network
->iaid
);
3051 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 IAID: %m");
3053 r
= sd_dhcp6_client_set_duid(link
->dhcp6_client
,
3055 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3056 duid
->raw_data_len
);
3058 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 DUID: %m");
3063 had_carrier
= link_has_carrier(link
);
3065 r
= link_update_flags(link
, m
);
3069 r
= link_update_lldp(link
);
3073 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
3074 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
3076 if (carrier_gained
) {
3077 log_link_info(link
, "Gained carrier");
3079 r
= link_carrier_gained(link
);
3082 } else if (carrier_lost
) {
3083 log_link_info(link
, "Lost carrier");
3085 r
= link_carrier_lost(link
);
3093 static void print_link_hashmap(FILE *f
, const char *prefix
, Hashmap
* h
) {
3101 if (hashmap_isempty(h
))
3105 HASHMAP_FOREACH(link
, h
, i
) {
3109 fprintf(f
, "%i", link
->ifindex
);
3116 int link_save(Link
*link
) {
3117 _cleanup_free_
char *temp_path
= NULL
;
3118 _cleanup_fclose_
FILE *f
= NULL
;
3119 const char *admin_state
, *oper_state
;
3126 assert(link
->state_file
);
3127 assert(link
->lease_file
);
3128 assert(link
->manager
);
3130 if (link
->state
== LINK_STATE_LINGER
) {
3131 unlink(link
->state_file
);
3135 link_lldp_save(link
);
3137 admin_state
= link_state_to_string(link
->state
);
3138 assert(admin_state
);
3140 oper_state
= link_operstate_to_string(link
->operstate
);
3143 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
3147 (void) fchmod(fileno(f
), 0644);
3150 "# This is private data. Do not parse.\n"
3153 admin_state
, oper_state
);
3155 if (link
->network
) {
3157 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
3158 const char *dhcp_domainname
= NULL
;
3159 char **dhcp6_domains
= NULL
;
3162 if (link
->dhcp6_client
) {
3163 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
, &dhcp6_lease
);
3164 if (r
< 0 && r
!= -ENOMSG
)
3165 log_link_debug(link
, "No DHCPv6 lease");
3168 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
3173 for (j
= 0; j
< link
->network
->n_dns
; j
++) {
3174 _cleanup_free_
char *b
= NULL
;
3176 r
= in_addr_to_string(link
->network
->dns
[j
].family
,
3177 &link
->network
->dns
[j
].address
, &b
);
3179 log_debug_errno(r
, "Failed to format address, ignoring: %m");
3189 if (link
->network
->dhcp_use_dns
&&
3191 const struct in_addr
*addresses
;
3193 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
3197 serialize_in_addrs(f
, addresses
, r
);
3202 if (link
->network
->dhcp_use_dns
&& dhcp6_lease
) {
3203 struct in6_addr
*in6_addrs
;
3205 r
= sd_dhcp6_lease_get_dns(dhcp6_lease
, &in6_addrs
);
3209 serialize_in6_addrs(f
, in6_addrs
, r
);
3214 /* Make sure to flush out old entries before we use the NDISC data */
3217 if (link
->network
->dhcp_use_dns
&& link
->ndisc_rdnss
) {
3220 SET_FOREACH(dd
, link
->ndisc_rdnss
, i
) {
3224 serialize_in6_addrs(f
, &dd
->address
, 1);
3233 fputstrv(f
, link
->network
->ntp
, NULL
, &space
);
3235 if (link
->network
->dhcp_use_ntp
&&
3237 const struct in_addr
*addresses
;
3239 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
3243 serialize_in_addrs(f
, addresses
, r
);
3248 if (link
->network
->dhcp_use_ntp
&& dhcp6_lease
) {
3249 struct in6_addr
*in6_addrs
;
3252 r
= sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease
,
3257 serialize_in6_addrs(f
, in6_addrs
, r
);
3261 r
= sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease
, &hosts
);
3263 fputstrv(f
, hosts
, NULL
, &space
);
3268 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
3269 if (link
->dhcp_lease
)
3270 (void) sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &dhcp_domainname
);
3272 (void) sd_dhcp6_lease_get_domains(dhcp6_lease
, &dhcp6_domains
);
3275 fputs("DOMAINS=", f
);
3276 fputstrv(f
, link
->network
->search_domains
, NULL
, &space
);
3278 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
) {
3281 if (dhcp_domainname
)
3282 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3284 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3286 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3287 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3292 fputs("ROUTE_DOMAINS=", f
);
3293 fputstrv(f
, link
->network
->route_domains
, NULL
, NULL
);
3295 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
) {
3298 if (dhcp_domainname
)
3299 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3301 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3303 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3304 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3309 fprintf(f
, "LLMNR=%s\n",
3310 resolve_support_to_string(link
->network
->llmnr
));
3311 fprintf(f
, "MDNS=%s\n",
3312 resolve_support_to_string(link
->network
->mdns
));
3314 if (link
->network
->dnssec_mode
!= _DNSSEC_MODE_INVALID
)
3315 fprintf(f
, "DNSSEC=%s\n",
3316 dnssec_mode_to_string(link
->network
->dnssec_mode
));
3318 if (!set_isempty(link
->network
->dnssec_negative_trust_anchors
)) {
3321 fputs("DNSSEC_NTA=", f
);
3323 SET_FOREACH(n
, link
->network
->dnssec_negative_trust_anchors
, i
)
3324 fputs_with_space(f
, n
, NULL
, &space
);
3328 fputs("ADDRESSES=", f
);
3330 SET_FOREACH(a
, link
->addresses
, i
) {
3331 _cleanup_free_
char *address_str
= NULL
;
3333 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
3337 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
3342 fputs("ROUTES=", f
);
3344 SET_FOREACH(route
, link
->routes
, i
) {
3345 _cleanup_free_
char *route_str
= NULL
;
3347 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
3351 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%hhu/"USEC_FMT
, space
? " " : "", route_str
,
3352 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
3359 print_link_hashmap(f
, "CARRIER_BOUND_TO=", link
->bound_to_links
);
3360 print_link_hashmap(f
, "CARRIER_BOUND_BY=", link
->bound_by_links
);
3362 if (link
->dhcp_lease
) {
3363 struct in_addr address
;
3364 const char *tz
= NULL
;
3366 assert(link
->network
);
3368 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
3370 fprintf(f
, "TIMEZONE=%s\n", tz
);
3372 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
3374 fputs("DHCP4_ADDRESS=", f
);
3375 serialize_in_addrs(f
, &address
, 1);
3379 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
3387 unlink(link
->lease_file
);
3390 struct in_addr address
;
3392 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
3394 fputs("IPV4LL_ADDRESS=", f
);
3395 serialize_in_addrs(f
, &address
, 1);
3400 r
= fflush_and_check(f
);
3404 if (rename(temp_path
, link
->state_file
) < 0) {
3412 (void) unlink(link
->state_file
);
3414 (void) unlink(temp_path
);
3416 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
3419 /* The serialized state in /run is no longer up-to-date. */
3420 void link_dirty(Link
*link
) {
3425 /* mark manager dirty as link is dirty */
3426 manager_dirty(link
->manager
);
3428 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
3430 /* allocation errors are ignored */
3433 r
= set_put(link
->manager
->dirty_links
, link
);
3435 /* don't take another ref if the link was already dirty */
3441 /* The serialized state in /run is up-to-date */
3442 void link_clean(Link
*link
) {
3444 assert(link
->manager
);
3446 set_remove(link
->manager
->dirty_links
, link
);
3450 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
3451 [LINK_STATE_PENDING
] = "pending",
3452 [LINK_STATE_ENSLAVING
] = "configuring",
3453 [LINK_STATE_SETTING_ADDRESSES
] = "configuring",
3454 [LINK_STATE_SETTING_ROUTES
] = "configuring",
3455 [LINK_STATE_CONFIGURED
] = "configured",
3456 [LINK_STATE_UNMANAGED
] = "unmanaged",
3457 [LINK_STATE_FAILED
] = "failed",
3458 [LINK_STATE_LINGER
] = "linger",
3461 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);
3463 static const char* const link_operstate_table
[_LINK_OPERSTATE_MAX
] = {
3464 [LINK_OPERSTATE_OFF
] = "off",
3465 [LINK_OPERSTATE_NO_CARRIER
] = "no-carrier",
3466 [LINK_OPERSTATE_DORMANT
] = "dormant",
3467 [LINK_OPERSTATE_CARRIER
] = "carrier",
3468 [LINK_OPERSTATE_DEGRADED
] = "degraded",
3469 [LINK_OPERSTATE_ROUTABLE
] = "routable",
3472 DEFINE_STRING_TABLE_LOOKUP(link_operstate
, LinkOperationalState
);