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", disabled
? "disable" : "enable", link
->ifname
);
260 log_link_info(link
, "IPv6 %sd for interface: %m", enable_disable(!disabled
));
265 void link_update_operstate(Link
*link
) {
266 LinkOperationalState operstate
;
269 if (link
->kernel_operstate
== IF_OPER_DORMANT
)
270 operstate
= LINK_OPERSTATE_DORMANT
;
271 else if (link_has_carrier(link
)) {
273 uint8_t scope
= RT_SCOPE_NOWHERE
;
276 /* if we have carrier, check what addresses we have */
277 SET_FOREACH(address
, link
->addresses
, i
) {
278 if (!address_is_ready(address
))
281 if (address
->scope
< scope
)
282 scope
= address
->scope
;
285 /* for operstate we also take foreign addresses into account */
286 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
287 if (!address_is_ready(address
))
290 if (address
->scope
< scope
)
291 scope
= address
->scope
;
294 if (scope
< RT_SCOPE_SITE
)
295 /* universally accessible addresses found */
296 operstate
= LINK_OPERSTATE_ROUTABLE
;
297 else if (scope
< RT_SCOPE_HOST
)
298 /* only link or site local addresses found */
299 operstate
= LINK_OPERSTATE_DEGRADED
;
301 /* no useful addresses found */
302 operstate
= LINK_OPERSTATE_CARRIER
;
303 } else if (link
->flags
& IFF_UP
)
304 operstate
= LINK_OPERSTATE_NO_CARRIER
;
306 operstate
= LINK_OPERSTATE_OFF
;
308 if (link
->operstate
!= operstate
) {
309 link
->operstate
= operstate
;
310 link_send_changed(link
, "OperationalState", NULL
);
315 #define FLAG_STRING(string, flag, old, new) \
316 (((old ^ new) & flag) \
317 ? ((old & flag) ? (" -" string) : (" +" string)) \
320 static int link_update_flags(Link
*link
, sd_netlink_message
*m
) {
321 unsigned flags
, unknown_flags_added
, unknown_flags_removed
, unknown_flags
;
327 r
= sd_rtnl_message_link_get_flags(m
, &flags
);
329 return log_link_warning_errno(link
, r
, "Could not get link flags: %m");
331 r
= sd_netlink_message_read_u8(m
, IFLA_OPERSTATE
, &operstate
);
333 /* if we got a message without operstate, take it to mean
334 the state was unchanged */
335 operstate
= link
->kernel_operstate
;
337 if ((link
->flags
== flags
) && (link
->kernel_operstate
== operstate
))
340 if (link
->flags
!= flags
) {
341 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",
342 FLAG_STRING("LOOPBACK", IFF_LOOPBACK
, link
->flags
, flags
),
343 FLAG_STRING("MASTER", IFF_MASTER
, link
->flags
, flags
),
344 FLAG_STRING("SLAVE", IFF_SLAVE
, link
->flags
, flags
),
345 FLAG_STRING("UP", IFF_UP
, link
->flags
, flags
),
346 FLAG_STRING("DORMANT", IFF_DORMANT
, link
->flags
, flags
),
347 FLAG_STRING("LOWER_UP", IFF_LOWER_UP
, link
->flags
, flags
),
348 FLAG_STRING("RUNNING", IFF_RUNNING
, link
->flags
, flags
),
349 FLAG_STRING("MULTICAST", IFF_MULTICAST
, link
->flags
, flags
),
350 FLAG_STRING("BROADCAST", IFF_BROADCAST
, link
->flags
, flags
),
351 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT
, link
->flags
, flags
),
352 FLAG_STRING("PROMISC", IFF_PROMISC
, link
->flags
, flags
),
353 FLAG_STRING("ALLMULTI", IFF_ALLMULTI
, link
->flags
, flags
),
354 FLAG_STRING("PORTSEL", IFF_PORTSEL
, link
->flags
, flags
),
355 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA
, link
->flags
, flags
),
356 FLAG_STRING("DYNAMIC", IFF_DYNAMIC
, link
->flags
, flags
),
357 FLAG_STRING("NOARP", IFF_NOARP
, link
->flags
, flags
),
358 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS
, link
->flags
, flags
),
359 FLAG_STRING("DEBUG", IFF_DEBUG
, link
->flags
, flags
),
360 FLAG_STRING("ECHO", IFF_ECHO
, link
->flags
, flags
));
362 unknown_flags
= ~(IFF_LOOPBACK
| IFF_MASTER
| IFF_SLAVE
| IFF_UP
|
363 IFF_DORMANT
| IFF_LOWER_UP
| IFF_RUNNING
|
364 IFF_MULTICAST
| IFF_BROADCAST
| IFF_POINTOPOINT
|
365 IFF_PROMISC
| IFF_ALLMULTI
| IFF_PORTSEL
|
366 IFF_AUTOMEDIA
| IFF_DYNAMIC
| IFF_NOARP
|
367 IFF_NOTRAILERS
| IFF_DEBUG
| IFF_ECHO
);
368 unknown_flags_added
= ((link
->flags
^ flags
) & flags
& unknown_flags
);
369 unknown_flags_removed
= ((link
->flags
^ flags
) & link
->flags
& unknown_flags
);
371 /* link flags are currently at most 18 bits, let's align to
373 if (unknown_flags_added
)
375 "Unknown link flags gained: %#.5x (ignoring)",
376 unknown_flags_added
);
378 if (unknown_flags_removed
)
380 "Unknown link flags lost: %#.5x (ignoring)",
381 unknown_flags_removed
);
385 link
->kernel_operstate
= operstate
;
387 link_update_operstate(link
);
392 static int link_new(Manager
*manager
, sd_netlink_message
*message
, Link
**ret
) {
393 _cleanup_link_unref_ Link
*link
= NULL
;
395 const char *ifname
, *kind
= NULL
;
397 unsigned short iftype
;
403 /* check for link kind */
404 r
= sd_netlink_message_enter_container(message
, IFLA_LINKINFO
);
406 (void)sd_netlink_message_read_string(message
, IFLA_INFO_KIND
, &kind
);
407 r
= sd_netlink_message_exit_container(message
);
412 r
= sd_netlink_message_get_type(message
, &type
);
415 else if (type
!= RTM_NEWLINK
)
418 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
421 else if (ifindex
<= 0)
424 r
= sd_rtnl_message_link_get_type(message
, &iftype
);
428 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &ifname
);
432 link
= new0(Link
, 1);
437 link
->manager
= manager
;
438 link
->state
= LINK_STATE_PENDING
;
439 link
->rtnl_extended_attrs
= true;
440 link
->ifindex
= ifindex
;
441 link
->iftype
= iftype
;
442 link
->ifname
= strdup(ifname
);
447 link
->kind
= strdup(kind
);
452 r
= sd_netlink_message_read_ether_addr(message
, IFLA_ADDRESS
, &link
->mac
);
454 log_link_debug_errno(link
, r
, "MAC address not found for new device, continuing without");
456 if (asprintf(&link
->state_file
, "/run/systemd/netif/links/%d", link
->ifindex
) < 0)
459 if (asprintf(&link
->lease_file
, "/run/systemd/netif/leases/%d", link
->ifindex
) < 0)
462 if (asprintf(&link
->lldp_file
, "/run/systemd/netif/lldp/%d", link
->ifindex
) < 0)
465 r
= hashmap_ensure_allocated(&manager
->links
, NULL
);
469 r
= hashmap_put(manager
->links
, INT_TO_PTR(link
->ifindex
), link
);
473 r
= link_update_flags(link
, message
);
483 static void link_free(Link
*link
) {
491 while (!set_isempty(link
->addresses
))
492 address_free(set_first(link
->addresses
));
494 while (!set_isempty(link
->addresses_foreign
))
495 address_free(set_first(link
->addresses_foreign
));
497 link
->addresses
= set_free(link
->addresses
);
499 link
->addresses_foreign
= set_free(link
->addresses_foreign
);
501 while ((address
= link
->pool_addresses
)) {
502 LIST_REMOVE(addresses
, link
->pool_addresses
, address
);
503 address_free(address
);
506 sd_dhcp_server_unref(link
->dhcp_server
);
507 sd_dhcp_client_unref(link
->dhcp_client
);
508 sd_dhcp_lease_unref(link
->dhcp_lease
);
510 link_lldp_emit_stop(link
);
512 free(link
->lease_file
);
514 sd_lldp_unref(link
->lldp
);
515 free(link
->lldp_file
);
519 sd_ipv4ll_unref(link
->ipv4ll
);
520 sd_dhcp6_client_unref(link
->dhcp6_client
);
521 sd_ndisc_unref(link
->ndisc
);
524 hashmap_remove(link
->manager
->links
, INT_TO_PTR(link
->ifindex
));
530 (void)unlink(link
->state_file
);
531 free(link
->state_file
);
533 udev_device_unref(link
->udev_device
);
535 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
)
536 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(carrier
->ifindex
));
537 hashmap_free(link
->bound_to_links
);
539 HASHMAP_FOREACH (carrier
, link
->bound_by_links
, i
)
540 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(carrier
->ifindex
));
541 hashmap_free(link
->bound_by_links
);
546 Link
*link_unref(Link
*link
) {
550 assert(link
->n_ref
> 0);
562 Link
*link_ref(Link
*link
) {
566 assert(link
->n_ref
> 0);
573 int link_get(Manager
*m
, int ifindex
, Link
**ret
) {
580 link
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
589 static void link_set_state(Link
*link
, LinkState state
) {
592 if (link
->state
== state
)
597 link_send_changed(link
, "AdministrativeState", NULL
);
600 static void link_enter_unmanaged(Link
*link
) {
603 log_link_debug(link
, "Unmanaged");
605 link_set_state(link
, LINK_STATE_UNMANAGED
);
610 static int link_stop_clients(Link
*link
) {
614 assert(link
->manager
);
615 assert(link
->manager
->event
);
617 if (link
->dhcp_client
) {
618 k
= sd_dhcp_client_stop(link
->dhcp_client
);
620 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv4 client: %m");
624 k
= sd_ipv4ll_stop(link
->ipv4ll
);
626 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 link-local: %m");
629 if (link
->dhcp6_client
) {
630 k
= sd_dhcp6_client_stop(link
->dhcp6_client
);
632 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv6 client: %m");
636 k
= sd_ndisc_stop(link
->ndisc
);
638 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Discovery: %m");
641 link_lldp_emit_stop(link
);
645 void link_enter_failed(Link
*link
) {
648 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
651 log_link_warning(link
, "Failed");
653 link_set_state(link
, LINK_STATE_FAILED
);
655 link_stop_clients(link
);
660 static Address
* link_find_dhcp_server_address(Link
*link
) {
664 assert(link
->network
);
666 /* The first statically configured address if there is any */
667 LIST_FOREACH(addresses
, address
, link
->network
->static_addresses
) {
669 if (address
->family
!= AF_INET
)
672 if (in_addr_is_null(address
->family
, &address
->in_addr
))
678 /* If that didn't work, find a suitable address we got from the pool */
679 LIST_FOREACH(addresses
, address
, link
->pool_addresses
) {
680 if (address
->family
!= AF_INET
)
689 static int link_enter_configured(Link
*link
) {
691 assert(link
->network
);
692 assert(link
->state
== LINK_STATE_SETTING_ROUTES
);
694 log_link_info(link
, "Configured");
696 link_set_state(link
, LINK_STATE_CONFIGURED
);
703 void link_check_ready(Link
*link
) {
709 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
715 if (!link
->static_configured
)
718 if (link_ipv4ll_enabled(link
))
719 if (!link
->ipv4ll_address
||
723 if (link_ipv6ll_enabled(link
))
724 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) > 0)
727 if ((link_dhcp4_enabled(link
) && !link_dhcp6_enabled(link
) &&
728 !link
->dhcp4_configured
) ||
729 (link_dhcp6_enabled(link
) && !link_dhcp4_enabled(link
) &&
730 !link
->dhcp6_configured
) ||
731 (link_dhcp4_enabled(link
) && link_dhcp6_enabled(link
) &&
732 !link
->dhcp4_configured
&& !link
->dhcp6_configured
))
735 if (link_ipv6_accept_ra_enabled(link
) && !link
->ndisc_configured
)
738 SET_FOREACH(a
, link
->addresses
, i
)
739 if (!address_is_ready(a
))
742 if (link
->state
!= LINK_STATE_CONFIGURED
)
743 link_enter_configured(link
);
748 static int route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
749 _cleanup_link_unref_ Link
*link
= userdata
;
752 assert(link
->link_messages
> 0);
753 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
754 LINK_STATE_SETTING_ROUTES
, LINK_STATE_FAILED
,
757 link
->link_messages
--;
759 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
762 r
= sd_netlink_message_get_errno(m
);
763 if (r
< 0 && r
!= -EEXIST
)
764 log_link_warning_errno(link
, r
, "Could not set route: %m");
766 if (link
->link_messages
== 0) {
767 log_link_debug(link
, "Routes set");
768 link
->static_configured
= true;
769 link_check_ready(link
);
775 static int link_enter_set_routes(Link
*link
) {
780 assert(link
->network
);
781 assert(link
->state
== LINK_STATE_SETTING_ADDRESSES
);
783 link_set_state(link
, LINK_STATE_SETTING_ROUTES
);
785 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
786 r
= route_configure(rt
, link
, route_handler
);
788 log_link_warning_errno(link
, r
, "Could not set routes: %m");
789 link_enter_failed(link
);
793 link
->link_messages
++;
796 if (link
->link_messages
== 0) {
797 link
->static_configured
= true;
798 link_check_ready(link
);
800 log_link_debug(link
, "Setting routes");
805 int link_route_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
806 _cleanup_link_unref_ Link
*link
= userdata
;
811 assert(link
->ifname
);
813 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
816 r
= sd_netlink_message_get_errno(m
);
817 if (r
< 0 && r
!= -ESRCH
)
818 log_link_warning_errno(link
, r
, "Could not drop route: %m");
823 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
824 _cleanup_link_unref_ Link
*link
= userdata
;
830 assert(link
->ifname
);
831 assert(link
->link_messages
> 0);
832 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
833 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
835 link
->link_messages
--;
837 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
840 r
= sd_netlink_message_get_errno(m
);
841 if (r
< 0 && r
!= -EEXIST
)
842 log_link_warning_errno(link
, r
, "could not set address: %m");
844 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
846 if (link
->link_messages
== 0) {
847 log_link_debug(link
, "Addresses set");
848 link_enter_set_routes(link
);
854 static int link_push_uplink_dns_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
855 _cleanup_free_
struct in_addr
*addresses
= NULL
;
856 size_t n_addresses
= 0, n_allocated
= 0;
859 log_debug("Copying DNS server information from %s", link
->ifname
);
864 for (i
= 0; i
< link
->network
->n_dns
; i
++) {
867 /* Only look for IPv4 addresses */
868 if (link
->network
->dns
[i
].family
!= AF_INET
)
871 ia
= link
->network
->dns
[i
].address
.in
;
873 /* Never propagate obviously borked data */
874 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
877 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
880 addresses
[n_addresses
++] = ia
;
883 if (link
->network
->dhcp_use_dns
&& link
->dhcp_lease
) {
884 const struct in_addr
*da
= NULL
;
887 n
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &da
);
890 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
893 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
898 if (n_addresses
<= 0)
901 return sd_dhcp_server_set_dns(s
, addresses
, n_addresses
);
904 static int link_push_uplink_ntp_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
905 _cleanup_free_
struct in_addr
*addresses
= NULL
;
906 size_t n_addresses
= 0, n_allocated
= 0;
912 log_debug("Copying NTP server information from %s", link
->ifname
);
914 STRV_FOREACH(a
, link
->network
->ntp
) {
917 /* Only look for IPv4 addresses */
918 if (inet_pton(AF_INET
, *a
, &ia
) <= 0)
921 /* Never propagate obviously borked data */
922 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
925 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
928 addresses
[n_addresses
++] = ia
;
931 if (link
->network
->dhcp_use_ntp
&& link
->dhcp_lease
) {
932 const struct in_addr
*da
= NULL
;
935 n
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &da
);
938 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
941 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
946 if (n_addresses
<= 0)
949 return sd_dhcp_server_set_ntp(s
, addresses
, n_addresses
);
952 static int link_set_bridge_fdb(Link
*link
) {
956 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
957 r
= fdb_entry_configure(link
, fdb_entry
);
959 return log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
965 static int link_enter_set_addresses(Link
*link
) {
970 assert(link
->network
);
971 assert(link
->state
!= _LINK_STATE_INVALID
);
973 r
= link_set_bridge_fdb(link
);
977 link_set_state(link
, LINK_STATE_SETTING_ADDRESSES
);
979 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
980 r
= address_configure(ad
, link
, address_handler
, false);
982 log_link_warning_errno(link
, r
, "Could not set addresses: %m");
983 link_enter_failed(link
);
987 link
->link_messages
++;
990 /* now that we can figure out a default address for the dhcp server,
992 if (link_dhcp4_server_enabled(link
)) {
995 bool acquired_uplink
= false;
997 address
= link_find_dhcp_server_address(link
);
999 log_link_warning(link
, "Failed to find suitable address for DHCPv4 server instance.");
1000 link_enter_failed(link
);
1004 /* use the server address' subnet as the pool */
1005 r
= sd_dhcp_server_configure_pool(link
->dhcp_server
, &address
->in_addr
.in
, address
->prefixlen
,
1006 link
->network
->dhcp_server_pool_offset
, link
->network
->dhcp_server_pool_size
);
1011 r = sd_dhcp_server_set_router(link->dhcp_server,
1012 &main_address->in_addr.in);
1017 if (link
->network
->dhcp_server_max_lease_time_usec
> 0) {
1018 r
= sd_dhcp_server_set_max_lease_time(
1020 DIV_ROUND_UP(link
->network
->dhcp_server_max_lease_time_usec
, USEC_PER_SEC
));
1025 if (link
->network
->dhcp_server_default_lease_time_usec
> 0) {
1026 r
= sd_dhcp_server_set_default_lease_time(
1028 DIV_ROUND_UP(link
->network
->dhcp_server_default_lease_time_usec
, USEC_PER_SEC
));
1033 if (link
->network
->dhcp_server_emit_dns
) {
1035 if (link
->network
->n_dhcp_server_dns
> 0)
1036 r
= sd_dhcp_server_set_dns(link
->dhcp_server
, link
->network
->dhcp_server_dns
, link
->network
->n_dhcp_server_dns
);
1038 uplink
= manager_find_uplink(link
->manager
, link
);
1039 acquired_uplink
= true;
1042 log_link_debug(link
, "Not emitting DNS server information on link, couldn't find suitable uplink.");
1045 r
= link_push_uplink_dns_to_dhcp_server(uplink
, link
->dhcp_server
);
1048 log_link_warning_errno(link
, r
, "Failed to set DNS server for DHCP server, ignoring: %m");
1052 if (link
->network
->dhcp_server_emit_ntp
) {
1054 if (link
->network
->n_dhcp_server_ntp
> 0)
1055 r
= sd_dhcp_server_set_ntp(link
->dhcp_server
, link
->network
->dhcp_server_ntp
, link
->network
->n_dhcp_server_ntp
);
1057 if (!acquired_uplink
)
1058 uplink
= manager_find_uplink(link
->manager
, link
);
1061 log_link_debug(link
, "Not emitting NTP server information on link, couldn't find suitable uplink.");
1064 r
= link_push_uplink_ntp_to_dhcp_server(uplink
, link
->dhcp_server
);
1068 log_link_warning_errno(link
, r
, "Failed to set NTP server for DHCP server, ignoring: %m");
1071 r
= sd_dhcp_server_set_emit_router(link
->dhcp_server
, link
->network
->dhcp_server_emit_router
);
1073 log_link_warning_errno(link
, r
, "Failed to set router emission for DHCP server: %m");
1077 if (link
->network
->dhcp_server_emit_timezone
) {
1078 _cleanup_free_
char *buffer
= NULL
;
1079 const char *tz
= NULL
;
1081 if (link
->network
->dhcp_server_timezone
)
1082 tz
= link
->network
->dhcp_server_timezone
;
1084 r
= get_timezone(&buffer
);
1086 log_warning_errno(r
, "Failed to determine timezone: %m");
1092 r
= sd_dhcp_server_set_timezone(link
->dhcp_server
, tz
);
1098 r
= sd_dhcp_server_start(link
->dhcp_server
);
1100 log_link_warning_errno(link
, r
, "Could not start DHCPv4 server instance: %m");
1102 link_enter_failed(link
);
1107 log_link_debug(link
, "Offering DHCPv4 leases");
1110 if (link
->link_messages
== 0)
1111 link_enter_set_routes(link
);
1113 log_link_debug(link
, "Setting addresses");
1118 int link_address_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1119 _cleanup_link_unref_ Link
*link
= userdata
;
1124 assert(link
->ifname
);
1126 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1129 r
= sd_netlink_message_get_errno(m
);
1130 if (r
< 0 && r
!= -EADDRNOTAVAIL
)
1131 log_link_warning_errno(link
, r
, "Could not drop address: %m");
1136 static int link_set_bridge_vlan(Link
*link
) {
1139 r
= br_vlan_configure(link
, link
->network
->pvid
, link
->network
->br_vid_bitmap
, link
->network
->br_untagged_bitmap
);
1141 log_link_error_errno(link
, r
, "Failed to assign VLANs to bridge port: %m");
1146 static int link_set_proxy_arp(Link
*link
) {
1147 const char *p
= NULL
;
1150 if (!link_proxy_arp_enabled(link
))
1153 p
= strjoina("/proc/sys/net/ipv4/conf/", link
->ifname
, "/proxy_arp");
1155 r
= write_string_file(p
, one_zero(link
->network
->proxy_arp
), WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
1157 log_link_warning_errno(link
, r
, "Cannot configure proxy ARP for interface: %m");
1162 static int link_set_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1163 _cleanup_link_unref_ Link
*link
= userdata
;
1166 log_link_debug(link
, "Set link");
1168 r
= sd_netlink_message_get_errno(m
);
1169 if (r
< 0 && r
!= -EEXIST
) {
1170 log_link_error_errno(link
, r
, "Could not join netdev: %m");
1171 link_enter_failed(link
);
1178 static int set_hostname_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
1179 _cleanup_link_unref_ Link
*link
= userdata
;
1180 const sd_bus_error
*e
;
1185 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1188 e
= sd_bus_message_get_error(m
);
1190 log_link_warning_errno(link
, sd_bus_error_get_errno(e
), "Could not set hostname: %s", e
->message
);
1195 int link_set_hostname(Link
*link
, const char *hostname
) {
1199 assert(link
->manager
);
1201 log_link_debug(link
, "Setting transient hostname: '%s'", strna(hostname
));
1203 if (!link
->manager
->bus
) {
1204 /* TODO: replace by assert when we can rely on kdbus */
1205 log_link_info(link
, "Not connected to system bus, ignoring transient hostname.");
1209 r
= sd_bus_call_method_async(
1212 "org.freedesktop.hostname1",
1213 "/org/freedesktop/hostname1",
1214 "org.freedesktop.hostname1",
1216 set_hostname_handler
,
1223 return log_link_error_errno(link
, r
, "Could not set transient hostname: %m");
1230 static int set_timezone_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
1231 _cleanup_link_unref_ Link
*link
= userdata
;
1232 const sd_bus_error
*e
;
1237 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1240 e
= sd_bus_message_get_error(m
);
1242 log_link_warning_errno(link
, sd_bus_error_get_errno(e
), "Could not set timezone: %s", e
->message
);
1247 int link_set_timezone(Link
*link
, const char *tz
) {
1251 assert(link
->manager
);
1254 log_link_debug(link
, "Setting system timezone: '%s'", tz
);
1256 if (!link
->manager
->bus
) {
1257 log_link_info(link
, "Not connected to system bus, ignoring timezone.");
1261 r
= sd_bus_call_method_async(
1264 "org.freedesktop.timedate1",
1265 "/org/freedesktop/timedate1",
1266 "org.freedesktop.timedate1",
1268 set_timezone_handler
,
1274 return log_link_error_errno(link
, r
, "Could not set timezone: %m");
1281 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1282 _cleanup_link_unref_ Link
*link
= userdata
;
1287 assert(link
->ifname
);
1289 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1292 r
= sd_netlink_message_get_errno(m
);
1294 log_link_warning_errno(link
, r
, "Could not set MTU: %m");
1299 int link_set_mtu(Link
*link
, uint32_t mtu
) {
1300 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1304 assert(link
->manager
);
1305 assert(link
->manager
->rtnl
);
1307 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1309 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1311 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1313 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1315 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1317 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, set_mtu_handler
, link
, 0, NULL
);
1319 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1326 static int set_flags_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1327 _cleanup_link_unref_ Link
*link
= userdata
;
1332 assert(link
->ifname
);
1334 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1337 r
= sd_netlink_message_get_errno(m
);
1339 log_link_warning_errno(link
, r
, "Could not set link flags: %m");
1344 static int link_set_flags(Link
*link
) {
1345 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1346 unsigned ifi_change
= 0;
1347 unsigned ifi_flags
= 0;
1351 assert(link
->manager
);
1352 assert(link
->manager
->rtnl
);
1354 if (link
->flags
& IFF_LOOPBACK
)
1360 if (link
->network
->arp
< 0)
1363 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1365 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1367 if (link
->network
->arp
>= 0) {
1368 ifi_change
|= IFF_NOARP
;
1369 ifi_flags
|= link
->network
->arp
? 0 : IFF_NOARP
;
1372 r
= sd_rtnl_message_link_set_flags(req
, ifi_flags
, ifi_change
);
1374 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1376 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, set_flags_handler
, link
, 0, NULL
);
1378 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1385 static int link_set_bridge(Link
*link
) {
1386 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1390 assert(link
->network
);
1392 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1394 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1396 r
= sd_rtnl_message_link_set_family(req
, PF_BRIDGE
);
1398 return log_link_error_errno(link
, r
, "Could not set message family: %m");
1400 r
= sd_netlink_message_open_container(req
, IFLA_PROTINFO
);
1402 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1404 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_GUARD
, !link
->network
->use_bpdu
);
1406 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_GUARD attribute: %m");
1408 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MODE
, link
->network
->hairpin
);
1410 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MODE attribute: %m");
1412 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_FAST_LEAVE
, link
->network
->fast_leave
);
1414 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_FAST_LEAVE attribute: %m");
1416 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_PROTECT
, !link
->network
->allow_port_to_be_root
);
1418 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PROTECT attribute: %m");
1420 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_UNICAST_FLOOD
, link
->network
->unicast_flood
);
1422 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_UNICAST_FLOOD attribute: %m");
1424 if (link
->network
->cost
!= 0) {
1425 r
= sd_netlink_message_append_u32(req
, IFLA_BRPORT_COST
, link
->network
->cost
);
1427 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_COST attribute: %m");
1430 r
= sd_netlink_message_close_container(req
);
1432 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1434 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_set_handler
, link
, 0, NULL
);
1436 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1443 static int link_lldp_save(Link
*link
) {
1444 _cleanup_free_
char *temp_path
= NULL
;
1445 _cleanup_fclose_
FILE *f
= NULL
;
1446 sd_lldp_neighbor
**l
= NULL
;
1450 assert(link
->lldp_file
);
1453 (void) unlink(link
->lldp_file
);
1457 r
= sd_lldp_get_neighbors(link
->lldp
, &l
);
1461 (void) unlink(link
->lldp_file
);
1467 r
= fopen_temporary(link
->lldp_file
, &f
, &temp_path
);
1471 fchmod(fileno(f
), 0644);
1473 for (i
= 0; i
< n
; i
++) {
1478 r
= sd_lldp_neighbor_get_raw(l
[i
], &p
, &sz
);
1483 (void) fwrite(&u
, 1, sizeof(u
), f
);
1484 (void) fwrite(p
, 1, sz
, f
);
1487 r
= fflush_and_check(f
);
1491 if (rename(temp_path
, link
->lldp_file
) < 0) {
1498 (void) unlink(link
->lldp_file
);
1500 (void) unlink(temp_path
);
1502 log_link_error_errno(link
, r
, "Failed to save LLDP data to %s: %m", link
->lldp_file
);
1506 for (i
= 0; i
< n
; i
++)
1507 sd_lldp_neighbor_unref(l
[i
]);
1514 static void lldp_handler(sd_lldp
*lldp
, sd_lldp_event event
, sd_lldp_neighbor
*n
, void *userdata
) {
1515 Link
*link
= userdata
;
1520 (void) link_lldp_save(link
);
1522 if (link_lldp_emit_enabled(link
) && event
== SD_LLDP_EVENT_ADDED
) {
1523 /* If we received information about a new neighbor, restart the LLDP "fast" logic */
1525 log_link_debug(link
, "Received LLDP datagram from previously unknown neighbor, restarting 'fast' LLDP transmission.");
1527 r
= link_lldp_emit_start(link
);
1529 log_link_warning_errno(link
, r
, "Failed to restart LLDP transmission: %m");
1533 static int link_acquire_ipv6_conf(Link
*link
) {
1538 if (link_dhcp6_enabled(link
)) {
1539 assert(link
->dhcp6_client
);
1540 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1542 /* start DHCPv6 client in stateless mode */
1543 r
= dhcp6_request_address(link
, true);
1544 if (r
< 0 && r
!= -EBUSY
)
1545 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv6 lease: %m");
1547 log_link_debug(link
, "Acquiring DHCPv6 lease");
1550 if (link_ipv6_accept_ra_enabled(link
)) {
1551 assert(link
->ndisc
);
1553 log_link_debug(link
, "Discovering IPv6 routers");
1555 r
= sd_ndisc_start(link
->ndisc
);
1556 if (r
< 0 && r
!= -EBUSY
)
1557 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1563 static int link_acquire_ipv4_conf(Link
*link
) {
1567 assert(link
->network
);
1568 assert(link
->manager
);
1569 assert(link
->manager
->event
);
1571 if (link_ipv4ll_enabled(link
)) {
1572 assert(link
->ipv4ll
);
1574 log_link_debug(link
, "Acquiring IPv4 link-local address");
1576 r
= sd_ipv4ll_start(link
->ipv4ll
);
1578 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1581 if (link_dhcp4_enabled(link
)) {
1582 assert(link
->dhcp_client
);
1584 log_link_debug(link
, "Acquiring DHCPv4 lease");
1586 r
= sd_dhcp_client_start(link
->dhcp_client
);
1588 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1594 static int link_acquire_conf(Link
*link
) {
1599 r
= link_acquire_ipv4_conf(link
);
1603 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) == 0) {
1604 r
= link_acquire_ipv6_conf(link
);
1609 if (link_lldp_emit_enabled(link
)) {
1610 r
= link_lldp_emit_start(link
);
1612 return log_link_warning_errno(link
, r
, "Failed to start LLDP transmission: %m");
1618 bool link_has_carrier(Link
*link
) {
1619 /* see Documentation/networking/operstates.txt in the kernel sources */
1621 if (link
->kernel_operstate
== IF_OPER_UP
)
1624 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1625 /* operstate may not be implemented, so fall back to flags */
1626 if ((link
->flags
& IFF_LOWER_UP
) && !(link
->flags
& IFF_DORMANT
))
1632 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1633 _cleanup_link_unref_ Link
*link
= userdata
;
1638 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1641 r
= sd_netlink_message_get_errno(m
);
1643 /* we warn but don't fail the link, as it may be
1645 log_link_warning_errno(link
, r
, "Could not bring up interface: %m");
1650 static int link_up(Link
*link
) {
1651 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1652 uint8_t ipv6ll_mode
;
1656 assert(link
->network
);
1657 assert(link
->manager
);
1658 assert(link
->manager
->rtnl
);
1660 log_link_debug(link
, "Bringing link up");
1662 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1664 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1666 /* set it free if not enslaved with networkd */
1667 if (!link
->network
->bridge
&& !link
->network
->bond
&& !link
->network
->vrf
) {
1668 r
= sd_netlink_message_append_u32(req
, IFLA_MASTER
, 0);
1670 return log_link_error_errno(link
, r
, "Could not append IFLA_MASTER attribute: %m");
1673 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1675 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1677 if (link
->network
->mac
) {
1678 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
1680 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
1683 /* If IPv6 not configured (no static IPv6 address and IPv6LL autoconfiguration is disabled)
1684 for this interface, or if it is a bridge slave, then disable IPv6 else enable it. */
1685 (void) link_enable_ipv6(link
);
1687 if (link
->network
->mtu
) {
1688 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
1689 on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */
1690 if (link_ipv6_enabled(link
) && link
->network
->mtu
< IPV6_MIN_MTU
) {
1692 log_link_warning(link
, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU
) ", as "
1693 "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU
) " bytes: %m");
1695 link
->network
->mtu
= IPV6_MIN_MTU
;
1698 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, link
->network
->mtu
);
1700 return log_link_error_errno(link
, r
, "Could not set MTU: %m");
1703 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1705 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1707 if (link_ipv6_enabled(link
)) {
1708 /* if the kernel lacks ipv6 support setting IFF_UP fails if any ipv6 options are passed */
1709 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1711 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1713 if (!link_ipv6ll_enabled(link
))
1714 ipv6ll_mode
= IN6_ADDR_GEN_MODE_NONE
;
1716 const char *p
= NULL
;
1717 _cleanup_free_
char *stable_secret
= NULL
;
1719 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/stable_secret");
1720 r
= read_one_line_file(p
, &stable_secret
);
1723 ipv6ll_mode
= IN6_ADDR_GEN_MODE_EUI64
;
1725 ipv6ll_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
1727 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1729 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1731 if (!in_addr_is_null(AF_INET6
, &link
->network
->ipv6_token
)) {
1732 r
= sd_netlink_message_append_in6_addr(req
, IFLA_INET6_TOKEN
, &link
->network
->ipv6_token
.in6
);
1734 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_TOKEN: %m");
1737 r
= sd_netlink_message_close_container(req
);
1739 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1742 r
= sd_netlink_message_close_container(req
);
1744 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1746 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_up_handler
, link
, 0, NULL
);
1748 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1755 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1756 _cleanup_link_unref_ Link
*link
= userdata
;
1761 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1764 r
= sd_netlink_message_get_errno(m
);
1766 log_link_warning_errno(link
, r
, "Could not bring down interface: %m");
1771 static int link_down(Link
*link
) {
1772 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1776 assert(link
->manager
);
1777 assert(link
->manager
->rtnl
);
1779 log_link_debug(link
, "Bringing link down");
1781 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
1782 RTM_SETLINK
, link
->ifindex
);
1784 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1786 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
1788 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1790 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_down_handler
, link
, 0, NULL
);
1792 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1799 static int link_up_can(Link
*link
) {
1800 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1805 log_link_debug(link
, "Bringing CAN link up");
1807 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1809 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1811 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1813 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1815 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_up_handler
, link
, 0, NULL
);
1817 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1824 static int link_handle_bound_to_list(Link
*link
) {
1828 bool required_up
= false;
1829 bool link_is_up
= false;
1833 if (hashmap_isempty(link
->bound_to_links
))
1836 if (link
->flags
& IFF_UP
)
1839 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
1840 if (link_has_carrier(l
)) {
1845 if (!required_up
&& link_is_up
) {
1846 r
= link_down(link
);
1849 } else if (required_up
&& !link_is_up
) {
1858 static int link_handle_bound_by_list(Link
*link
) {
1865 if (hashmap_isempty(link
->bound_by_links
))
1868 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
1869 r
= link_handle_bound_to_list(l
);
1877 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
1883 if (link
== carrier
)
1886 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
1889 r
= hashmap_ensure_allocated(h
, NULL
);
1893 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
1900 static int link_new_bound_by_list(Link
*link
) {
1905 bool list_updated
= false;
1908 assert(link
->manager
);
1912 HASHMAP_FOREACH(carrier
, m
->links
, i
) {
1913 if (!carrier
->network
)
1916 if (strv_isempty(carrier
->network
->bind_carrier
))
1919 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
, 0)) {
1920 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
1924 list_updated
= true;
1931 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
1932 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
1936 link_dirty(carrier
);
1942 static int link_new_bound_to_list(Link
*link
) {
1947 bool list_updated
= false;
1950 assert(link
->manager
);
1955 if (strv_isempty(link
->network
->bind_carrier
))
1960 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
1961 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
, 0)) {
1962 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
1966 list_updated
= true;
1973 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
1974 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
1978 link_dirty(carrier
);
1984 static int link_new_carrier_maps(Link
*link
) {
1987 r
= link_new_bound_by_list(link
);
1991 r
= link_handle_bound_by_list(link
);
1995 r
= link_new_bound_to_list(link
);
1999 r
= link_handle_bound_to_list(link
);
2006 static void link_free_bound_to_list(Link
*link
) {
2010 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
2011 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
2013 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
2014 link_dirty(bound_to
);
2020 static void link_free_bound_by_list(Link
*link
) {
2024 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
2025 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
2027 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
2028 link_dirty(bound_by
);
2029 link_handle_bound_to_list(bound_by
);
2036 static void link_free_carrier_maps(Link
*link
) {
2037 bool list_updated
= false;
2041 if (!hashmap_isempty(link
->bound_to_links
)) {
2042 link_free_bound_to_list(link
);
2043 list_updated
= true;
2046 if (!hashmap_isempty(link
->bound_by_links
)) {
2047 link_free_bound_by_list(link
);
2048 list_updated
= true;
2057 void link_drop(Link
*link
) {
2058 if (!link
|| link
->state
== LINK_STATE_LINGER
)
2061 link_set_state(link
, LINK_STATE_LINGER
);
2063 link_free_carrier_maps(link
);
2065 log_link_debug(link
, "Link removed");
2067 (void)unlink(link
->state_file
);
2073 static int link_joined(Link
*link
) {
2077 assert(link
->network
);
2079 if (!hashmap_isempty(link
->bound_to_links
)) {
2080 r
= link_handle_bound_to_list(link
);
2083 } else if (!(link
->flags
& IFF_UP
)) {
2086 link_enter_failed(link
);
2091 if (link
->network
->bridge
) {
2092 r
= link_set_bridge(link
);
2094 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
2097 if (link
->network
->use_br_vlan
&&
2098 (link
->network
->bridge
|| streq_ptr("bridge", link
->kind
))) {
2099 r
= link_set_bridge_vlan(link
);
2101 log_link_error_errno(link
, r
, "Could not set bridge vlan: %m");
2104 return link_enter_set_addresses(link
);
2107 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
2108 _cleanup_link_unref_ Link
*link
= userdata
;
2112 assert(link
->network
);
2116 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2119 r
= sd_netlink_message_get_errno(m
);
2120 if (r
< 0 && r
!= -EEXIST
) {
2121 log_link_error_errno(link
, r
, "Could not join netdev: %m");
2122 link_enter_failed(link
);
2125 log_link_debug(link
, "Joined netdev");
2127 if (link
->enslaving
<= 0)
2133 static int link_enter_join_netdev(Link
*link
) {
2139 assert(link
->network
);
2140 assert(link
->state
== LINK_STATE_PENDING
);
2142 link_set_state(link
, LINK_STATE_ENSLAVING
);
2146 if (!link
->network
->bridge
&&
2147 !link
->network
->bond
&&
2148 !link
->network
->vrf
&&
2149 hashmap_isempty(link
->network
->stacked_netdevs
))
2150 return link_joined(link
);
2152 if (link
->network
->bond
) {
2153 log_struct(LOG_DEBUG
,
2154 LOG_LINK_INTERFACE(link
),
2155 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2156 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
),
2159 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
2161 log_struct_errno(LOG_WARNING
, r
,
2162 LOG_LINK_INTERFACE(link
),
2163 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2164 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
),
2167 link_enter_failed(link
);
2174 if (link
->network
->bridge
) {
2175 log_struct(LOG_DEBUG
,
2176 LOG_LINK_INTERFACE(link
),
2177 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2178 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
),
2181 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
2183 log_struct_errno(LOG_WARNING
, r
,
2184 LOG_LINK_INTERFACE(link
),
2185 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2186 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
),
2188 link_enter_failed(link
);
2195 if (link
->network
->vrf
) {
2196 log_struct(LOG_DEBUG
,
2197 LOG_LINK_INTERFACE(link
),
2198 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2199 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->vrf
->ifname
),
2201 r
= netdev_join(link
->network
->vrf
, link
, netdev_join_handler
);
2203 log_struct_errno(LOG_WARNING
, r
,
2204 LOG_LINK_INTERFACE(link
),
2205 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2206 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->vrf
->ifname
),
2208 link_enter_failed(link
);
2215 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
2217 log_struct(LOG_DEBUG
,
2218 LOG_LINK_INTERFACE(link
),
2219 LOG_NETDEV_INTERFACE(netdev
),
2220 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
),
2223 r
= netdev_join(netdev
, link
, netdev_join_handler
);
2225 log_struct_errno(LOG_WARNING
, r
,
2226 LOG_LINK_INTERFACE(link
),
2227 LOG_NETDEV_INTERFACE(netdev
),
2228 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
),
2230 link_enter_failed(link
);
2240 static int link_set_ipv4_forward(Link
*link
) {
2243 if (!link_ipv4_forward_enabled(link
))
2246 /* We propagate the forwarding flag from one interface to the
2247 * global setting one way. This means: as long as at least one
2248 * interface was configured at any time that had IP forwarding
2249 * enabled the setting will stay on for good. We do this
2250 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
2251 * somewhat in sync (see below). */
2253 r
= write_string_file("/proc/sys/net/ipv4/ip_forward", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2255 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
2260 static int link_set_ipv6_forward(Link
*link
) {
2263 if (!link_ipv6_forward_enabled(link
))
2266 /* On Linux, the IPv6 stack does not know a per-interface
2267 * packet forwarding setting: either packet forwarding is on
2268 * for all, or off for all. We hence don't bother with a
2269 * per-interface setting, but simply propagate the interface
2270 * flag, if it is set, to the global flag, one-way. Note that
2271 * while IPv4 would allow a per-interface flag, we expose the
2272 * same behaviour there and also propagate the setting from
2273 * one to all, to keep things simple (see above). */
2275 r
= write_string_file("/proc/sys/net/ipv6/conf/all/forwarding", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2277 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
2282 static int link_set_ipv6_privacy_extensions(Link
*link
) {
2283 char buf
[DECIMAL_STR_MAX(unsigned) + 1];
2284 IPv6PrivacyExtensions s
;
2285 const char *p
= NULL
;
2288 s
= link_ipv6_privacy_extensions(link
);
2292 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/use_tempaddr");
2293 xsprintf(buf
, "%u", (unsigned) link
->network
->ipv6_privacy_extensions
);
2295 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2297 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
2302 static int link_set_ipv6_accept_ra(Link
*link
) {
2303 const char *p
= NULL
;
2306 /* Make this a NOP if IPv6 is not available */
2307 if (!socket_ipv6_is_supported())
2310 if (link
->flags
& IFF_LOOPBACK
)
2316 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/accept_ra");
2318 /* We handle router advertisements ourselves, tell the kernel to GTFO */
2319 r
= write_string_file(p
, "0", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2321 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
2326 static int link_set_ipv6_dad_transmits(Link
*link
) {
2327 char buf
[DECIMAL_STR_MAX(int) + 1];
2328 const char *p
= NULL
;
2331 /* Make this a NOP if IPv6 is not available */
2332 if (!socket_ipv6_is_supported())
2335 if (link
->flags
& IFF_LOOPBACK
)
2341 if (link
->network
->ipv6_dad_transmits
< 0)
2344 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/dad_transmits");
2345 xsprintf(buf
, "%i", link
->network
->ipv6_dad_transmits
);
2347 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2349 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
2354 static int link_set_ipv6_hop_limit(Link
*link
) {
2355 char buf
[DECIMAL_STR_MAX(int) + 1];
2356 const char *p
= NULL
;
2359 /* Make this a NOP if IPv6 is not available */
2360 if (!socket_ipv6_is_supported())
2363 if (link
->flags
& IFF_LOOPBACK
)
2369 if (link
->network
->ipv6_hop_limit
< 0)
2372 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/hop_limit");
2373 xsprintf(buf
, "%i", link
->network
->ipv6_hop_limit
);
2375 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2377 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2382 static int link_drop_foreign_config(Link
*link
) {
2388 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
2389 /* we consider IPv6LL addresses to be managed by the kernel */
2390 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2393 r
= address_remove(address
, link
, link_address_remove_handler
);
2398 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2399 /* do not touch routes managed by the kernel */
2400 if (route
->protocol
== RTPROT_KERNEL
)
2403 r
= route_remove(route
, link
, link_route_remove_handler
);
2411 static int link_drop_config(Link
*link
) {
2417 SET_FOREACH(address
, link
->addresses
, i
) {
2418 /* we consider IPv6LL addresses to be managed by the kernel */
2419 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2422 r
= address_remove(address
, link
, link_address_remove_handler
);
2427 SET_FOREACH(route
, link
->routes
, i
) {
2428 /* do not touch routes managed by the kernel */
2429 if (route
->protocol
== RTPROT_KERNEL
)
2432 r
= route_remove(route
, link
, link_route_remove_handler
);
2442 static int link_update_lldp(Link
*link
) {
2450 if (link
->flags
& IFF_UP
) {
2451 r
= sd_lldp_start(link
->lldp
);
2453 log_link_debug(link
, "Started LLDP.");
2455 r
= sd_lldp_stop(link
->lldp
);
2457 log_link_debug(link
, "Stopped LLDP.");
2463 static int link_configure(Link
*link
) {
2467 assert(link
->network
);
2468 assert(link
->state
== LINK_STATE_PENDING
);
2470 if (streq_ptr(link
->kind
, "vcan")) {
2472 if (!(link
->flags
& IFF_UP
)) {
2473 r
= link_up_can(link
);
2475 link_enter_failed(link
);
2483 /* Drop foreign config, but ignore loopback or critical devices.
2484 * We do not want to remove loopback address or addresses used for root NFS. */
2485 if (!(link
->flags
& IFF_LOOPBACK
) && !(link
->network
->dhcp_critical
)) {
2486 r
= link_drop_foreign_config(link
);
2491 r
= link_set_proxy_arp(link
);
2495 r
= link_set_ipv4_forward(link
);
2499 r
= link_set_ipv6_forward(link
);
2503 r
= link_set_ipv6_privacy_extensions(link
);
2507 r
= link_set_ipv6_accept_ra(link
);
2511 r
= link_set_ipv6_dad_transmits(link
);
2515 r
= link_set_ipv6_hop_limit(link
);
2519 r
= link_set_flags(link
);
2523 if (link_ipv4ll_enabled(link
)) {
2524 r
= ipv4ll_configure(link
);
2529 if (link_dhcp4_enabled(link
)) {
2530 r
= dhcp4_configure(link
);
2535 if (link_dhcp4_server_enabled(link
)) {
2536 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
2540 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
2545 if (link_dhcp6_enabled(link
) ||
2546 link_ipv6_accept_ra_enabled(link
)) {
2547 r
= dhcp6_configure(link
);
2552 if (link_ipv6_accept_ra_enabled(link
)) {
2553 r
= ndisc_configure(link
);
2558 if (link_lldp_rx_enabled(link
)) {
2559 r
= sd_lldp_new(&link
->lldp
);
2563 r
= sd_lldp_set_ifindex(link
->lldp
, link
->ifindex
);
2567 r
= sd_lldp_match_capabilities(link
->lldp
,
2568 link
->network
->lldp_mode
== LLDP_MODE_ROUTERS_ONLY
?
2569 SD_LLDP_SYSTEM_CAPABILITIES_ALL_ROUTERS
:
2570 SD_LLDP_SYSTEM_CAPABILITIES_ALL
);
2574 r
= sd_lldp_set_filter_address(link
->lldp
, &link
->mac
);
2578 r
= sd_lldp_attach_event(link
->lldp
, NULL
, 0);
2582 r
= sd_lldp_set_callback(link
->lldp
, lldp_handler
, link
);
2586 r
= link_update_lldp(link
);
2591 if (link_has_carrier(link
)) {
2592 r
= link_acquire_conf(link
);
2597 return link_enter_join_netdev(link
);
2600 static int link_initialized_and_synced(sd_netlink
*rtnl
, sd_netlink_message
*m
,
2602 _cleanup_link_unref_ Link
*link
= userdata
;
2607 assert(link
->ifname
);
2608 assert(link
->manager
);
2610 if (link
->state
!= LINK_STATE_PENDING
)
2613 log_link_debug(link
, "Link state is up-to-date");
2615 r
= link_new_bound_by_list(link
);
2619 r
= link_handle_bound_by_list(link
);
2623 if (!link
->network
) {
2624 r
= network_get(link
->manager
, link
->udev_device
, link
->ifname
,
2625 &link
->mac
, &network
);
2627 link_enter_unmanaged(link
);
2632 if (link
->flags
& IFF_LOOPBACK
) {
2633 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
2634 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
2636 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
2637 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
2639 if (network
->dhcp_server
)
2640 log_link_debug(link
, "Ignoring DHCP server for loopback link");
2643 r
= network_apply(network
, link
);
2648 r
= link_new_bound_to_list(link
);
2652 r
= link_configure(link
);
2659 int link_initialized(Link
*link
, struct udev_device
*device
) {
2660 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2664 assert(link
->manager
);
2665 assert(link
->manager
->rtnl
);
2668 if (link
->state
!= LINK_STATE_PENDING
)
2671 if (link
->udev_device
)
2674 log_link_debug(link
, "udev initialized link");
2676 link
->udev_device
= udev_device_ref(device
);
2678 /* udev has initialized the link, but we don't know if we have yet
2679 * processed the NEWLINK messages with the latest state. Do a GETLINK,
2680 * when it returns we know that the pending NEWLINKs have already been
2681 * processed and that we are up-to-date */
2683 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
2688 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
,
2689 link_initialized_and_synced
, link
, 0, NULL
);
2698 static int link_load(Link
*link
) {
2699 _cleanup_free_
char *network_file
= NULL
,
2702 *dhcp4_address
= NULL
,
2703 *ipv4ll_address
= NULL
;
2704 union in_addr_union address
;
2705 union in_addr_union route_dst
;
2711 r
= parse_env_file(link
->state_file
, NEWLINE
,
2712 "NETWORK_FILE", &network_file
,
2713 "ADDRESSES", &addresses
,
2715 "DHCP4_ADDRESS", &dhcp4_address
,
2716 "IPV4LL_ADDRESS", &ipv4ll_address
,
2718 if (r
< 0 && r
!= -ENOENT
)
2719 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
2726 suffix
= strrchr(network_file
, '.');
2728 log_link_debug(link
, "Failed to get network name from %s", network_file
);
2729 goto network_file_fail
;
2733 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
2735 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
2736 goto network_file_fail
;
2739 r
= network_apply(network
, link
);
2741 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
2750 _cleanup_free_
char *address_str
= NULL
;
2751 char *prefixlen_str
;
2753 unsigned char prefixlen
;
2755 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
2757 log_link_debug_errno(link
, r
, "Failed to extract next address string: %m");
2763 prefixlen_str
= strchr(address_str
, '/');
2764 if (!prefixlen_str
) {
2765 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
2769 *prefixlen_str
++ = '\0';
2771 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
2773 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
2777 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
2779 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
2783 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
2785 return log_link_error_errno(link
, r
, "Failed to add address: %m");
2794 _cleanup_free_
char *route_str
= NULL
;
2795 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
2797 char *prefixlen_str
;
2799 unsigned char prefixlen
, tos
, table
;
2802 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
2804 log_link_debug_errno(link
, r
, "Failed to extract next route string: %m");
2810 prefixlen_str
= strchr(route_str
, '/');
2811 if (!prefixlen_str
) {
2812 log_link_debug(link
, "Failed to parse route %s", route_str
);
2816 *prefixlen_str
++ = '\0';
2818 r
= sscanf(prefixlen_str
, "%hhu/%hhu/%"SCNu32
"/%hhu/"USEC_FMT
, &prefixlen
, &tos
, &priority
, &table
, &lifetime
);
2820 log_link_debug(link
,
2821 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
2826 r
= in_addr_from_string_auto(route_str
, &family
, &route_dst
);
2828 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
2832 r
= route_add(link
, family
, &route_dst
, prefixlen
, tos
, priority
, table
, &route
);
2834 return log_link_error_errno(link
, r
, "Failed to add route: %m");
2836 if (lifetime
!= USEC_INFINITY
) {
2837 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(), lifetime
,
2838 0, route_expire_handler
, route
);
2840 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
2843 route
->lifetime
= lifetime
;
2844 sd_event_source_unref(route
->expire
);
2845 route
->expire
= expire
;
2850 if (dhcp4_address
) {
2851 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
2853 log_link_debug_errno(link
, r
, "Failed to parse DHCPv4 address %s: %m", dhcp4_address
);
2854 goto dhcp4_address_fail
;
2857 r
= sd_dhcp_client_new(&link
->dhcp_client
);
2859 return log_link_error_errno(link
, r
, "Failed to create DHCPv4 client: %m");
2861 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
2863 return log_link_error_errno(link
, r
, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address
);
2868 if (ipv4ll_address
) {
2869 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
2871 log_link_debug_errno(link
, r
, "Failed to parse IPv4LL address %s: %m", ipv4ll_address
);
2872 goto ipv4ll_address_fail
;
2875 r
= sd_ipv4ll_new(&link
->ipv4ll
);
2877 return log_link_error_errno(link
, r
, "Failed to create IPv4LL client: %m");
2879 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
2881 return log_link_error_errno(link
, r
, "Failed to set initial IPv4LL address %s: %m", ipv4ll_address
);
2884 ipv4ll_address_fail
:
2889 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
2891 _cleanup_udev_device_unref_
struct udev_device
*device
= NULL
;
2892 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
2900 r
= link_new(m
, message
, ret
);
2906 log_link_debug(link
, "Link %d added", link
->ifindex
);
2908 r
= link_load(link
);
2912 if (detect_container() <= 0) {
2913 /* not in a container, udev will be around */
2914 sprintf(ifindex_str
, "n%d", link
->ifindex
);
2915 device
= udev_device_new_from_device_id(m
->udev
, ifindex_str
);
2917 r
= log_link_warning_errno(link
, errno
, "Could not find udev device: %m");
2921 if (udev_device_get_is_initialized(device
) <= 0) {
2923 log_link_debug(link
, "link pending udev initialization...");
2927 r
= link_initialized(link
, device
);
2931 /* we are calling a callback directly, so must take a ref */
2934 r
= link_initialized_and_synced(m
->rtnl
, NULL
, link
);
2941 link_enter_failed(link
);
2945 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
2950 log_link_info(link
, "Gained IPv6LL");
2952 link
->ipv6ll_address
= *address
;
2953 link_check_ready(link
);
2955 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
2956 r
= link_acquire_ipv6_conf(link
);
2958 link_enter_failed(link
);
2966 static int link_carrier_gained(Link
*link
) {
2971 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
2972 r
= link_acquire_conf(link
);
2974 link_enter_failed(link
);
2978 r
= link_enter_set_addresses(link
);
2983 r
= link_handle_bound_by_list(link
);
2990 static int link_carrier_lost(Link
*link
) {
2995 r
= link_stop_clients(link
);
2997 link_enter_failed(link
);
3001 r
= link_drop_config(link
);
3005 if (!IN_SET(link
->state
, LINK_STATE_UNMANAGED
, LINK_STATE_PENDING
)) {
3006 log_link_debug(link
, "State is %s, dropping config", link_state_to_string(link
->state
));
3007 r
= link_drop_foreign_config(link
);
3012 r
= link_handle_bound_by_list(link
);
3019 int link_carrier_reset(Link
*link
) {
3024 if (link_has_carrier(link
)) {
3025 r
= link_carrier_lost(link
);
3029 r
= link_carrier_gained(link
);
3033 log_link_info(link
, "Reset carrier");
3039 int link_update(Link
*link
, sd_netlink_message
*m
) {
3040 struct ether_addr mac
;
3043 bool had_carrier
, carrier_gained
, carrier_lost
;
3047 assert(link
->ifname
);
3050 if (link
->state
== LINK_STATE_LINGER
) {
3052 log_link_info(link
, "Link readded");
3053 link_set_state(link
, LINK_STATE_ENSLAVING
);
3055 r
= link_new_carrier_maps(link
);
3060 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
3061 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
3062 log_link_info(link
, "Renamed to %s", ifname
);
3064 link_free_carrier_maps(link
);
3066 r
= free_and_strdup(&link
->ifname
, ifname
);
3070 r
= link_new_carrier_maps(link
);
3075 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
3076 if (r
>= 0 && mtu
> 0) {
3078 if (!link
->original_mtu
) {
3079 link
->original_mtu
= mtu
;
3080 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
3083 if (link
->dhcp_client
) {
3084 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
3087 log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
3093 /* The kernel may broadcast NEWLINK messages without the MAC address
3094 set, simply ignore them. */
3095 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
3097 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3100 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3103 log_link_debug(link
, "MAC address: "
3104 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
3105 mac
.ether_addr_octet
[0],
3106 mac
.ether_addr_octet
[1],
3107 mac
.ether_addr_octet
[2],
3108 mac
.ether_addr_octet
[3],
3109 mac
.ether_addr_octet
[4],
3110 mac
.ether_addr_octet
[5]);
3113 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
3115 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
3118 if (link
->dhcp_client
) {
3119 const DUID
*duid
= link_duid(link
);
3121 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
3122 (const uint8_t *) &link
->mac
,
3126 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
3128 r
= sd_dhcp_client_set_iaid_duid(link
->dhcp_client
,
3129 link
->network
->iaid
,
3131 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3132 duid
->raw_data_len
);
3134 return log_link_warning_errno(link
, r
, "Could not update DUID/IAID in DHCP client: %m");
3137 if (link
->dhcp6_client
) {
3138 const DUID
* duid
= link_duid(link
);
3140 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
3141 (const uint8_t *) &link
->mac
,
3145 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
3147 r
= sd_dhcp6_client_set_iaid(link
->dhcp6_client
,
3148 link
->network
->iaid
);
3150 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 IAID: %m");
3152 r
= sd_dhcp6_client_set_duid(link
->dhcp6_client
,
3154 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3155 duid
->raw_data_len
);
3157 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 DUID: %m");
3162 had_carrier
= link_has_carrier(link
);
3164 r
= link_update_flags(link
, m
);
3168 r
= link_update_lldp(link
);
3172 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
3173 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
3175 if (carrier_gained
) {
3176 log_link_info(link
, "Gained carrier");
3178 r
= link_carrier_gained(link
);
3181 } else if (carrier_lost
) {
3182 log_link_info(link
, "Lost carrier");
3184 r
= link_carrier_lost(link
);
3192 static void print_link_hashmap(FILE *f
, const char *prefix
, Hashmap
* h
) {
3200 if (hashmap_isempty(h
))
3204 HASHMAP_FOREACH(link
, h
, i
) {
3208 fprintf(f
, "%i", link
->ifindex
);
3215 int link_save(Link
*link
) {
3216 _cleanup_free_
char *temp_path
= NULL
;
3217 _cleanup_fclose_
FILE *f
= NULL
;
3218 const char *admin_state
, *oper_state
;
3225 assert(link
->state_file
);
3226 assert(link
->lease_file
);
3227 assert(link
->manager
);
3229 if (link
->state
== LINK_STATE_LINGER
) {
3230 unlink(link
->state_file
);
3234 link_lldp_save(link
);
3236 admin_state
= link_state_to_string(link
->state
);
3237 assert(admin_state
);
3239 oper_state
= link_operstate_to_string(link
->operstate
);
3242 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
3246 (void) fchmod(fileno(f
), 0644);
3249 "# This is private data. Do not parse.\n"
3252 admin_state
, oper_state
);
3254 if (link
->network
) {
3256 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
3257 const char *dhcp_domainname
= NULL
;
3258 char **dhcp6_domains
= NULL
;
3261 if (link
->dhcp6_client
) {
3262 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
, &dhcp6_lease
);
3263 if (r
< 0 && r
!= -ENOMSG
)
3264 log_link_debug(link
, "No DHCPv6 lease");
3267 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
3272 for (j
= 0; j
< link
->network
->n_dns
; j
++) {
3273 _cleanup_free_
char *b
= NULL
;
3275 r
= in_addr_to_string(link
->network
->dns
[j
].family
,
3276 &link
->network
->dns
[j
].address
, &b
);
3278 log_debug_errno(r
, "Failed to format address, ignoring: %m");
3288 if (link
->network
->dhcp_use_dns
&&
3290 const struct in_addr
*addresses
;
3292 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
3296 serialize_in_addrs(f
, addresses
, r
);
3301 if (link
->network
->dhcp_use_dns
&& dhcp6_lease
) {
3302 struct in6_addr
*in6_addrs
;
3304 r
= sd_dhcp6_lease_get_dns(dhcp6_lease
, &in6_addrs
);
3308 serialize_in6_addrs(f
, in6_addrs
, r
);
3313 /* Make sure to flush out old entries before we use the NDISC data */
3316 if (link
->network
->dhcp_use_dns
&& link
->ndisc_rdnss
) {
3319 SET_FOREACH(dd
, link
->ndisc_rdnss
, i
) {
3323 serialize_in6_addrs(f
, &dd
->address
, 1);
3332 fputstrv(f
, link
->network
->ntp
, NULL
, &space
);
3334 if (link
->network
->dhcp_use_ntp
&&
3336 const struct in_addr
*addresses
;
3338 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
3342 serialize_in_addrs(f
, addresses
, r
);
3347 if (link
->network
->dhcp_use_ntp
&& dhcp6_lease
) {
3348 struct in6_addr
*in6_addrs
;
3351 r
= sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease
,
3356 serialize_in6_addrs(f
, in6_addrs
, r
);
3360 r
= sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease
, &hosts
);
3362 fputstrv(f
, hosts
, NULL
, &space
);
3367 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
3368 if (link
->dhcp_lease
)
3369 (void) sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &dhcp_domainname
);
3371 (void) sd_dhcp6_lease_get_domains(dhcp6_lease
, &dhcp6_domains
);
3374 fputs("DOMAINS=", f
);
3375 fputstrv(f
, link
->network
->search_domains
, NULL
, &space
);
3377 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
) {
3380 if (dhcp_domainname
)
3381 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3383 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3385 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3386 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3391 fputs("ROUTE_DOMAINS=", f
);
3392 fputstrv(f
, link
->network
->route_domains
, NULL
, NULL
);
3394 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
) {
3397 if (dhcp_domainname
)
3398 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3400 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3402 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3403 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3408 fprintf(f
, "LLMNR=%s\n",
3409 resolve_support_to_string(link
->network
->llmnr
));
3410 fprintf(f
, "MDNS=%s\n",
3411 resolve_support_to_string(link
->network
->mdns
));
3413 if (link
->network
->dnssec_mode
!= _DNSSEC_MODE_INVALID
)
3414 fprintf(f
, "DNSSEC=%s\n",
3415 dnssec_mode_to_string(link
->network
->dnssec_mode
));
3417 if (!set_isempty(link
->network
->dnssec_negative_trust_anchors
)) {
3420 fputs("DNSSEC_NTA=", f
);
3422 SET_FOREACH(n
, link
->network
->dnssec_negative_trust_anchors
, i
)
3423 fputs_with_space(f
, n
, NULL
, &space
);
3427 fputs("ADDRESSES=", f
);
3429 SET_FOREACH(a
, link
->addresses
, i
) {
3430 _cleanup_free_
char *address_str
= NULL
;
3432 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
3436 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
3441 fputs("ROUTES=", f
);
3443 SET_FOREACH(route
, link
->routes
, i
) {
3444 _cleanup_free_
char *route_str
= NULL
;
3446 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
3450 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%hhu/"USEC_FMT
, space
? " " : "", route_str
,
3451 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
3458 print_link_hashmap(f
, "CARRIER_BOUND_TO=", link
->bound_to_links
);
3459 print_link_hashmap(f
, "CARRIER_BOUND_BY=", link
->bound_by_links
);
3461 if (link
->dhcp_lease
) {
3462 struct in_addr address
;
3463 const char *tz
= NULL
;
3465 assert(link
->network
);
3467 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
3469 fprintf(f
, "TIMEZONE=%s\n", tz
);
3471 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
3473 fputs("DHCP4_ADDRESS=", f
);
3474 serialize_in_addrs(f
, &address
, 1);
3478 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
3486 unlink(link
->lease_file
);
3489 struct in_addr address
;
3491 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
3493 fputs("IPV4LL_ADDRESS=", f
);
3494 serialize_in_addrs(f
, &address
, 1);
3499 r
= fflush_and_check(f
);
3503 if (rename(temp_path
, link
->state_file
) < 0) {
3511 (void) unlink(link
->state_file
);
3513 (void) unlink(temp_path
);
3515 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
3518 /* The serialized state in /run is no longer up-to-date. */
3519 void link_dirty(Link
*link
) {
3524 /* mark manager dirty as link is dirty */
3525 manager_dirty(link
->manager
);
3527 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
3529 /* allocation errors are ignored */
3532 r
= set_put(link
->manager
->dirty_links
, link
);
3534 /* don't take another ref if the link was already dirty */
3540 /* The serialized state in /run is up-to-date */
3541 void link_clean(Link
*link
) {
3543 assert(link
->manager
);
3545 set_remove(link
->manager
->dirty_links
, link
);
3549 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
3550 [LINK_STATE_PENDING
] = "pending",
3551 [LINK_STATE_ENSLAVING
] = "configuring",
3552 [LINK_STATE_SETTING_ADDRESSES
] = "configuring",
3553 [LINK_STATE_SETTING_ROUTES
] = "configuring",
3554 [LINK_STATE_CONFIGURED
] = "configured",
3555 [LINK_STATE_UNMANAGED
] = "unmanaged",
3556 [LINK_STATE_FAILED
] = "failed",
3557 [LINK_STATE_LINGER
] = "linger",
3560 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);
3562 static const char* const link_operstate_table
[_LINK_OPERSTATE_MAX
] = {
3563 [LINK_OPERSTATE_OFF
] = "off",
3564 [LINK_OPERSTATE_NO_CARRIER
] = "no-carrier",
3565 [LINK_OPERSTATE_DORMANT
] = "dormant",
3566 [LINK_OPERSTATE_CARRIER
] = "carrier",
3567 [LINK_OPERSTATE_DEGRADED
] = "degraded",
3568 [LINK_OPERSTATE_ROUTABLE
] = "routable",
3571 DEFINE_STRING_TABLE_LOOKUP(link_operstate
, LinkOperationalState
);