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 void link_enter_configured(Link
*link
) {
691 assert(link
->network
);
693 if (link
->state
!= LINK_STATE_SETTING_ROUTES
)
696 log_link_info(link
, "Configured");
698 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_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1179 _cleanup_link_unref_ Link
*link
= userdata
;
1184 assert(link
->ifname
);
1186 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1189 r
= sd_netlink_message_get_errno(m
);
1191 log_link_warning_errno(link
, r
, "Could not set MTU: %m");
1196 int link_set_mtu(Link
*link
, uint32_t mtu
) {
1197 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1201 assert(link
->manager
);
1202 assert(link
->manager
->rtnl
);
1204 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1206 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1208 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1210 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1212 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1214 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, set_mtu_handler
, link
, 0, NULL
);
1216 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1223 static int set_flags_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1224 _cleanup_link_unref_ Link
*link
= userdata
;
1229 assert(link
->ifname
);
1231 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1234 r
= sd_netlink_message_get_errno(m
);
1236 log_link_warning_errno(link
, r
, "Could not set link flags: %m");
1241 static int link_set_flags(Link
*link
) {
1242 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1243 unsigned ifi_change
= 0;
1244 unsigned ifi_flags
= 0;
1248 assert(link
->manager
);
1249 assert(link
->manager
->rtnl
);
1251 if (link
->flags
& IFF_LOOPBACK
)
1257 if (link
->network
->arp
< 0)
1260 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1262 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1264 if (link
->network
->arp
>= 0) {
1265 ifi_change
|= IFF_NOARP
;
1266 ifi_flags
|= link
->network
->arp
? 0 : IFF_NOARP
;
1269 r
= sd_rtnl_message_link_set_flags(req
, ifi_flags
, ifi_change
);
1271 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1273 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, set_flags_handler
, link
, 0, NULL
);
1275 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1282 static int link_set_bridge(Link
*link
) {
1283 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1287 assert(link
->network
);
1289 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1291 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1293 r
= sd_rtnl_message_link_set_family(req
, PF_BRIDGE
);
1295 return log_link_error_errno(link
, r
, "Could not set message family: %m");
1297 r
= sd_netlink_message_open_container(req
, IFLA_PROTINFO
);
1299 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1301 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_GUARD
, !link
->network
->use_bpdu
);
1303 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_GUARD attribute: %m");
1305 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MODE
, link
->network
->hairpin
);
1307 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MODE attribute: %m");
1309 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_FAST_LEAVE
, link
->network
->fast_leave
);
1311 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_FAST_LEAVE attribute: %m");
1313 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_PROTECT
, !link
->network
->allow_port_to_be_root
);
1315 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PROTECT attribute: %m");
1317 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_UNICAST_FLOOD
, link
->network
->unicast_flood
);
1319 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_UNICAST_FLOOD attribute: %m");
1321 if (link
->network
->cost
!= 0) {
1322 r
= sd_netlink_message_append_u32(req
, IFLA_BRPORT_COST
, link
->network
->cost
);
1324 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_COST attribute: %m");
1327 r
= sd_netlink_message_close_container(req
);
1329 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1331 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_set_handler
, link
, 0, NULL
);
1333 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1340 static int link_lldp_save(Link
*link
) {
1341 _cleanup_free_
char *temp_path
= NULL
;
1342 _cleanup_fclose_
FILE *f
= NULL
;
1343 sd_lldp_neighbor
**l
= NULL
;
1347 assert(link
->lldp_file
);
1350 (void) unlink(link
->lldp_file
);
1354 r
= sd_lldp_get_neighbors(link
->lldp
, &l
);
1358 (void) unlink(link
->lldp_file
);
1364 r
= fopen_temporary(link
->lldp_file
, &f
, &temp_path
);
1368 fchmod(fileno(f
), 0644);
1370 for (i
= 0; i
< n
; i
++) {
1375 r
= sd_lldp_neighbor_get_raw(l
[i
], &p
, &sz
);
1380 (void) fwrite(&u
, 1, sizeof(u
), f
);
1381 (void) fwrite(p
, 1, sz
, f
);
1384 r
= fflush_and_check(f
);
1388 if (rename(temp_path
, link
->lldp_file
) < 0) {
1395 (void) unlink(link
->lldp_file
);
1397 (void) unlink(temp_path
);
1399 log_link_error_errno(link
, r
, "Failed to save LLDP data to %s: %m", link
->lldp_file
);
1403 for (i
= 0; i
< n
; i
++)
1404 sd_lldp_neighbor_unref(l
[i
]);
1411 static void lldp_handler(sd_lldp
*lldp
, sd_lldp_event event
, sd_lldp_neighbor
*n
, void *userdata
) {
1412 Link
*link
= userdata
;
1417 (void) link_lldp_save(link
);
1419 if (link_lldp_emit_enabled(link
) && event
== SD_LLDP_EVENT_ADDED
) {
1420 /* If we received information about a new neighbor, restart the LLDP "fast" logic */
1422 log_link_debug(link
, "Received LLDP datagram from previously unknown neighbor, restarting 'fast' LLDP transmission.");
1424 r
= link_lldp_emit_start(link
);
1426 log_link_warning_errno(link
, r
, "Failed to restart LLDP transmission: %m");
1430 static int link_acquire_ipv6_conf(Link
*link
) {
1435 if (link_dhcp6_enabled(link
)) {
1436 assert(link
->dhcp6_client
);
1437 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1439 /* start DHCPv6 client in stateless mode */
1440 r
= dhcp6_request_address(link
, true);
1441 if (r
< 0 && r
!= -EBUSY
)
1442 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv6 lease: %m");
1444 log_link_debug(link
, "Acquiring DHCPv6 lease");
1447 if (link_ipv6_accept_ra_enabled(link
)) {
1448 assert(link
->ndisc
);
1450 log_link_debug(link
, "Discovering IPv6 routers");
1452 r
= sd_ndisc_start(link
->ndisc
);
1453 if (r
< 0 && r
!= -EBUSY
)
1454 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1460 static int link_acquire_ipv4_conf(Link
*link
) {
1464 assert(link
->network
);
1465 assert(link
->manager
);
1466 assert(link
->manager
->event
);
1468 if (link_ipv4ll_enabled(link
)) {
1469 assert(link
->ipv4ll
);
1471 log_link_debug(link
, "Acquiring IPv4 link-local address");
1473 r
= sd_ipv4ll_start(link
->ipv4ll
);
1475 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1478 if (link_dhcp4_enabled(link
)) {
1479 assert(link
->dhcp_client
);
1481 log_link_debug(link
, "Acquiring DHCPv4 lease");
1483 r
= sd_dhcp_client_start(link
->dhcp_client
);
1485 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1491 static int link_acquire_conf(Link
*link
) {
1496 r
= link_acquire_ipv4_conf(link
);
1500 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) == 0) {
1501 r
= link_acquire_ipv6_conf(link
);
1506 if (link_lldp_emit_enabled(link
)) {
1507 r
= link_lldp_emit_start(link
);
1509 return log_link_warning_errno(link
, r
, "Failed to start LLDP transmission: %m");
1515 bool link_has_carrier(Link
*link
) {
1516 /* see Documentation/networking/operstates.txt in the kernel sources */
1518 if (link
->kernel_operstate
== IF_OPER_UP
)
1521 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1522 /* operstate may not be implemented, so fall back to flags */
1523 if ((link
->flags
& IFF_LOWER_UP
) && !(link
->flags
& IFF_DORMANT
))
1529 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1530 _cleanup_link_unref_ Link
*link
= userdata
;
1535 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1538 r
= sd_netlink_message_get_errno(m
);
1540 /* we warn but don't fail the link, as it may be
1542 log_link_warning_errno(link
, r
, "Could not bring up interface: %m");
1547 static int link_up(Link
*link
) {
1548 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1549 uint8_t ipv6ll_mode
;
1553 assert(link
->network
);
1554 assert(link
->manager
);
1555 assert(link
->manager
->rtnl
);
1557 log_link_debug(link
, "Bringing link up");
1559 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1561 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1563 /* set it free if not enslaved with networkd */
1564 if (!link
->network
->bridge
&& !link
->network
->bond
&& !link
->network
->vrf
) {
1565 r
= sd_netlink_message_append_u32(req
, IFLA_MASTER
, 0);
1567 return log_link_error_errno(link
, r
, "Could not append IFLA_MASTER attribute: %m");
1570 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1572 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1574 if (link
->network
->mac
) {
1575 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
1577 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
1580 /* If IPv6 not configured (no static IPv6 address and IPv6LL autoconfiguration is disabled)
1581 for this interface, or if it is a bridge slave, then disable IPv6 else enable it. */
1582 (void) link_enable_ipv6(link
);
1584 if (link
->network
->mtu
) {
1585 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
1586 on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */
1587 if (link_ipv6_enabled(link
) && link
->network
->mtu
< IPV6_MIN_MTU
) {
1589 log_link_warning(link
, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU
) ", as "
1590 "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU
) " bytes: %m");
1592 link
->network
->mtu
= IPV6_MIN_MTU
;
1595 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, link
->network
->mtu
);
1597 return log_link_error_errno(link
, r
, "Could not set MTU: %m");
1600 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1602 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1604 if (link_ipv6_enabled(link
)) {
1605 /* if the kernel lacks ipv6 support setting IFF_UP fails if any ipv6 options are passed */
1606 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1608 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1610 if (!link_ipv6ll_enabled(link
))
1611 ipv6ll_mode
= IN6_ADDR_GEN_MODE_NONE
;
1613 const char *p
= NULL
;
1614 _cleanup_free_
char *stable_secret
= NULL
;
1616 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/stable_secret");
1617 r
= read_one_line_file(p
, &stable_secret
);
1620 ipv6ll_mode
= IN6_ADDR_GEN_MODE_EUI64
;
1622 ipv6ll_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
1624 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1626 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1628 if (!in_addr_is_null(AF_INET6
, &link
->network
->ipv6_token
)) {
1629 r
= sd_netlink_message_append_in6_addr(req
, IFLA_INET6_TOKEN
, &link
->network
->ipv6_token
.in6
);
1631 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_TOKEN: %m");
1634 r
= sd_netlink_message_close_container(req
);
1636 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1639 r
= sd_netlink_message_close_container(req
);
1641 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1643 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_up_handler
, link
, 0, NULL
);
1645 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1652 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1653 _cleanup_link_unref_ Link
*link
= userdata
;
1658 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1661 r
= sd_netlink_message_get_errno(m
);
1663 log_link_warning_errno(link
, r
, "Could not bring down interface: %m");
1668 static int link_down(Link
*link
) {
1669 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1673 assert(link
->manager
);
1674 assert(link
->manager
->rtnl
);
1676 log_link_debug(link
, "Bringing link down");
1678 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
1679 RTM_SETLINK
, link
->ifindex
);
1681 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1683 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
1685 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1687 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_down_handler
, link
, 0, NULL
);
1689 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1696 static int link_up_can(Link
*link
) {
1697 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1702 log_link_debug(link
, "Bringing CAN link up");
1704 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1706 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1708 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1710 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1712 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_up_handler
, link
, 0, NULL
);
1714 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1721 static int link_handle_bound_to_list(Link
*link
) {
1725 bool required_up
= false;
1726 bool link_is_up
= false;
1730 if (hashmap_isempty(link
->bound_to_links
))
1733 if (link
->flags
& IFF_UP
)
1736 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
1737 if (link_has_carrier(l
)) {
1742 if (!required_up
&& link_is_up
) {
1743 r
= link_down(link
);
1746 } else if (required_up
&& !link_is_up
) {
1755 static int link_handle_bound_by_list(Link
*link
) {
1762 if (hashmap_isempty(link
->bound_by_links
))
1765 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
1766 r
= link_handle_bound_to_list(l
);
1774 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
1780 if (link
== carrier
)
1783 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
1786 r
= hashmap_ensure_allocated(h
, NULL
);
1790 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
1797 static int link_new_bound_by_list(Link
*link
) {
1802 bool list_updated
= false;
1805 assert(link
->manager
);
1809 HASHMAP_FOREACH(carrier
, m
->links
, i
) {
1810 if (!carrier
->network
)
1813 if (strv_isempty(carrier
->network
->bind_carrier
))
1816 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
, 0)) {
1817 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
1821 list_updated
= true;
1828 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
1829 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
1833 link_dirty(carrier
);
1839 static int link_new_bound_to_list(Link
*link
) {
1844 bool list_updated
= false;
1847 assert(link
->manager
);
1852 if (strv_isempty(link
->network
->bind_carrier
))
1857 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
1858 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
, 0)) {
1859 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
1863 list_updated
= true;
1870 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
1871 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
1875 link_dirty(carrier
);
1881 static int link_new_carrier_maps(Link
*link
) {
1884 r
= link_new_bound_by_list(link
);
1888 r
= link_handle_bound_by_list(link
);
1892 r
= link_new_bound_to_list(link
);
1896 r
= link_handle_bound_to_list(link
);
1903 static void link_free_bound_to_list(Link
*link
) {
1907 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
1908 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
1910 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
1911 link_dirty(bound_to
);
1917 static void link_free_bound_by_list(Link
*link
) {
1921 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
1922 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
1924 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
1925 link_dirty(bound_by
);
1926 link_handle_bound_to_list(bound_by
);
1933 static void link_free_carrier_maps(Link
*link
) {
1934 bool list_updated
= false;
1938 if (!hashmap_isempty(link
->bound_to_links
)) {
1939 link_free_bound_to_list(link
);
1940 list_updated
= true;
1943 if (!hashmap_isempty(link
->bound_by_links
)) {
1944 link_free_bound_by_list(link
);
1945 list_updated
= true;
1954 void link_drop(Link
*link
) {
1955 if (!link
|| link
->state
== LINK_STATE_LINGER
)
1958 link_set_state(link
, LINK_STATE_LINGER
);
1960 link_free_carrier_maps(link
);
1962 log_link_debug(link
, "Link removed");
1964 (void)unlink(link
->state_file
);
1970 static int link_joined(Link
*link
) {
1974 assert(link
->network
);
1976 if (!hashmap_isempty(link
->bound_to_links
)) {
1977 r
= link_handle_bound_to_list(link
);
1980 } else if (!(link
->flags
& IFF_UP
)) {
1983 link_enter_failed(link
);
1988 if (link
->network
->bridge
) {
1989 r
= link_set_bridge(link
);
1991 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
1994 if (link
->network
->use_br_vlan
&&
1995 (link
->network
->bridge
|| streq_ptr("bridge", link
->kind
))) {
1996 r
= link_set_bridge_vlan(link
);
1998 log_link_error_errno(link
, r
, "Could not set bridge vlan: %m");
2001 return link_enter_set_addresses(link
);
2004 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
2005 _cleanup_link_unref_ Link
*link
= userdata
;
2009 assert(link
->network
);
2013 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2016 r
= sd_netlink_message_get_errno(m
);
2017 if (r
< 0 && r
!= -EEXIST
) {
2018 log_link_error_errno(link
, r
, "Could not join netdev: %m");
2019 link_enter_failed(link
);
2022 log_link_debug(link
, "Joined netdev");
2024 if (link
->enslaving
<= 0)
2030 static int link_enter_join_netdev(Link
*link
) {
2036 assert(link
->network
);
2037 assert(link
->state
== LINK_STATE_PENDING
);
2039 link_set_state(link
, LINK_STATE_ENSLAVING
);
2043 if (!link
->network
->bridge
&&
2044 !link
->network
->bond
&&
2045 !link
->network
->vrf
&&
2046 hashmap_isempty(link
->network
->stacked_netdevs
))
2047 return link_joined(link
);
2049 if (link
->network
->bond
) {
2050 log_struct(LOG_DEBUG
,
2051 LOG_LINK_INTERFACE(link
),
2052 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2053 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
),
2056 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
2058 log_struct_errno(LOG_WARNING
, r
,
2059 LOG_LINK_INTERFACE(link
),
2060 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2061 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
),
2064 link_enter_failed(link
);
2071 if (link
->network
->bridge
) {
2072 log_struct(LOG_DEBUG
,
2073 LOG_LINK_INTERFACE(link
),
2074 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2075 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
),
2078 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
2080 log_struct_errno(LOG_WARNING
, r
,
2081 LOG_LINK_INTERFACE(link
),
2082 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2083 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
),
2085 link_enter_failed(link
);
2092 if (link
->network
->vrf
) {
2093 log_struct(LOG_DEBUG
,
2094 LOG_LINK_INTERFACE(link
),
2095 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2096 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->vrf
->ifname
),
2098 r
= netdev_join(link
->network
->vrf
, link
, netdev_join_handler
);
2100 log_struct_errno(LOG_WARNING
, r
,
2101 LOG_LINK_INTERFACE(link
),
2102 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2103 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->vrf
->ifname
),
2105 link_enter_failed(link
);
2112 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
2114 log_struct(LOG_DEBUG
,
2115 LOG_LINK_INTERFACE(link
),
2116 LOG_NETDEV_INTERFACE(netdev
),
2117 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
),
2120 r
= netdev_join(netdev
, link
, netdev_join_handler
);
2122 log_struct_errno(LOG_WARNING
, r
,
2123 LOG_LINK_INTERFACE(link
),
2124 LOG_NETDEV_INTERFACE(netdev
),
2125 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
),
2127 link_enter_failed(link
);
2137 static int link_set_ipv4_forward(Link
*link
) {
2140 if (!link_ipv4_forward_enabled(link
))
2143 /* We propagate the forwarding flag from one interface to the
2144 * global setting one way. This means: as long as at least one
2145 * interface was configured at any time that had IP forwarding
2146 * enabled the setting will stay on for good. We do this
2147 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
2148 * somewhat in sync (see below). */
2150 r
= write_string_file("/proc/sys/net/ipv4/ip_forward", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2152 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
2157 static int link_set_ipv6_forward(Link
*link
) {
2160 if (!link_ipv6_forward_enabled(link
))
2163 /* On Linux, the IPv6 stack does not know a per-interface
2164 * packet forwarding setting: either packet forwarding is on
2165 * for all, or off for all. We hence don't bother with a
2166 * per-interface setting, but simply propagate the interface
2167 * flag, if it is set, to the global flag, one-way. Note that
2168 * while IPv4 would allow a per-interface flag, we expose the
2169 * same behaviour there and also propagate the setting from
2170 * one to all, to keep things simple (see above). */
2172 r
= write_string_file("/proc/sys/net/ipv6/conf/all/forwarding", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2174 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
2179 static int link_set_ipv6_privacy_extensions(Link
*link
) {
2180 char buf
[DECIMAL_STR_MAX(unsigned) + 1];
2181 IPv6PrivacyExtensions s
;
2182 const char *p
= NULL
;
2185 s
= link_ipv6_privacy_extensions(link
);
2189 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/use_tempaddr");
2190 xsprintf(buf
, "%u", (unsigned) link
->network
->ipv6_privacy_extensions
);
2192 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2194 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
2199 static int link_set_ipv6_accept_ra(Link
*link
) {
2200 const char *p
= NULL
;
2203 /* Make this a NOP if IPv6 is not available */
2204 if (!socket_ipv6_is_supported())
2207 if (link
->flags
& IFF_LOOPBACK
)
2213 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/accept_ra");
2215 /* We handle router advertisements ourselves, tell the kernel to GTFO */
2216 r
= write_string_file(p
, "0", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2218 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
2223 static int link_set_ipv6_dad_transmits(Link
*link
) {
2224 char buf
[DECIMAL_STR_MAX(int) + 1];
2225 const char *p
= NULL
;
2228 /* Make this a NOP if IPv6 is not available */
2229 if (!socket_ipv6_is_supported())
2232 if (link
->flags
& IFF_LOOPBACK
)
2238 if (link
->network
->ipv6_dad_transmits
< 0)
2241 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/dad_transmits");
2242 xsprintf(buf
, "%i", link
->network
->ipv6_dad_transmits
);
2244 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2246 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
2251 static int link_set_ipv6_hop_limit(Link
*link
) {
2252 char buf
[DECIMAL_STR_MAX(int) + 1];
2253 const char *p
= NULL
;
2256 /* Make this a NOP if IPv6 is not available */
2257 if (!socket_ipv6_is_supported())
2260 if (link
->flags
& IFF_LOOPBACK
)
2266 if (link
->network
->ipv6_hop_limit
< 0)
2269 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/hop_limit");
2270 xsprintf(buf
, "%i", link
->network
->ipv6_hop_limit
);
2272 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2274 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2279 static int link_drop_foreign_config(Link
*link
) {
2285 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
2286 /* we consider IPv6LL addresses to be managed by the kernel */
2287 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2290 r
= address_remove(address
, link
, link_address_remove_handler
);
2295 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2296 /* do not touch routes managed by the kernel */
2297 if (route
->protocol
== RTPROT_KERNEL
)
2300 r
= route_remove(route
, link
, link_route_remove_handler
);
2308 static int link_drop_config(Link
*link
) {
2314 SET_FOREACH(address
, link
->addresses
, i
) {
2315 /* we consider IPv6LL addresses to be managed by the kernel */
2316 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2319 r
= address_remove(address
, link
, link_address_remove_handler
);
2324 SET_FOREACH(route
, link
->routes
, i
) {
2325 /* do not touch routes managed by the kernel */
2326 if (route
->protocol
== RTPROT_KERNEL
)
2329 r
= route_remove(route
, link
, link_route_remove_handler
);
2339 static int link_update_lldp(Link
*link
) {
2347 if (link
->flags
& IFF_UP
) {
2348 r
= sd_lldp_start(link
->lldp
);
2350 log_link_debug(link
, "Started LLDP.");
2352 r
= sd_lldp_stop(link
->lldp
);
2354 log_link_debug(link
, "Stopped LLDP.");
2360 static int link_configure(Link
*link
) {
2364 assert(link
->network
);
2365 assert(link
->state
== LINK_STATE_PENDING
);
2367 if (streq_ptr(link
->kind
, "vcan")) {
2369 if (!(link
->flags
& IFF_UP
)) {
2370 r
= link_up_can(link
);
2372 link_enter_failed(link
);
2380 /* Drop foreign config, but ignore loopback or critical devices.
2381 * We do not want to remove loopback address or addresses used for root NFS. */
2382 if (!(link
->flags
& IFF_LOOPBACK
) && !(link
->network
->dhcp_critical
)) {
2383 r
= link_drop_foreign_config(link
);
2388 r
= link_set_proxy_arp(link
);
2392 r
= link_set_ipv4_forward(link
);
2396 r
= link_set_ipv6_forward(link
);
2400 r
= link_set_ipv6_privacy_extensions(link
);
2404 r
= link_set_ipv6_accept_ra(link
);
2408 r
= link_set_ipv6_dad_transmits(link
);
2412 r
= link_set_ipv6_hop_limit(link
);
2416 r
= link_set_flags(link
);
2420 if (link_ipv4ll_enabled(link
)) {
2421 r
= ipv4ll_configure(link
);
2426 if (link_dhcp4_enabled(link
)) {
2427 r
= dhcp4_configure(link
);
2432 if (link_dhcp4_server_enabled(link
)) {
2433 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
2437 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
2442 if (link_dhcp6_enabled(link
) ||
2443 link_ipv6_accept_ra_enabled(link
)) {
2444 r
= dhcp6_configure(link
);
2449 if (link_ipv6_accept_ra_enabled(link
)) {
2450 r
= ndisc_configure(link
);
2455 if (link_lldp_rx_enabled(link
)) {
2456 r
= sd_lldp_new(&link
->lldp
);
2460 r
= sd_lldp_set_ifindex(link
->lldp
, link
->ifindex
);
2464 r
= sd_lldp_match_capabilities(link
->lldp
,
2465 link
->network
->lldp_mode
== LLDP_MODE_ROUTERS_ONLY
?
2466 SD_LLDP_SYSTEM_CAPABILITIES_ALL_ROUTERS
:
2467 SD_LLDP_SYSTEM_CAPABILITIES_ALL
);
2471 r
= sd_lldp_set_filter_address(link
->lldp
, &link
->mac
);
2475 r
= sd_lldp_attach_event(link
->lldp
, NULL
, 0);
2479 r
= sd_lldp_set_callback(link
->lldp
, lldp_handler
, link
);
2483 r
= link_update_lldp(link
);
2488 if (link_has_carrier(link
)) {
2489 r
= link_acquire_conf(link
);
2494 return link_enter_join_netdev(link
);
2497 static int link_initialized_and_synced(sd_netlink
*rtnl
, sd_netlink_message
*m
,
2499 _cleanup_link_unref_ Link
*link
= userdata
;
2504 assert(link
->ifname
);
2505 assert(link
->manager
);
2507 if (link
->state
!= LINK_STATE_PENDING
)
2510 log_link_debug(link
, "Link state is up-to-date");
2512 r
= link_new_bound_by_list(link
);
2516 r
= link_handle_bound_by_list(link
);
2520 if (!link
->network
) {
2521 r
= network_get(link
->manager
, link
->udev_device
, link
->ifname
,
2522 &link
->mac
, &network
);
2524 link_enter_unmanaged(link
);
2526 } else if (r
== 0 && network
->unmanaged
) {
2527 link_enter_unmanaged(link
);
2532 if (link
->flags
& IFF_LOOPBACK
) {
2533 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
2534 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
2536 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
2537 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
2539 if (network
->dhcp_server
)
2540 log_link_debug(link
, "Ignoring DHCP server for loopback link");
2543 r
= network_apply(network
, link
);
2548 r
= link_new_bound_to_list(link
);
2552 r
= link_configure(link
);
2559 int link_initialized(Link
*link
, struct udev_device
*device
) {
2560 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2564 assert(link
->manager
);
2565 assert(link
->manager
->rtnl
);
2568 if (link
->state
!= LINK_STATE_PENDING
)
2571 if (link
->udev_device
)
2574 log_link_debug(link
, "udev initialized link");
2576 link
->udev_device
= udev_device_ref(device
);
2578 /* udev has initialized the link, but we don't know if we have yet
2579 * processed the NEWLINK messages with the latest state. Do a GETLINK,
2580 * when it returns we know that the pending NEWLINKs have already been
2581 * processed and that we are up-to-date */
2583 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
2588 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
,
2589 link_initialized_and_synced
, link
, 0, NULL
);
2598 static int link_load(Link
*link
) {
2599 _cleanup_free_
char *network_file
= NULL
,
2602 *dhcp4_address
= NULL
,
2603 *ipv4ll_address
= NULL
;
2604 union in_addr_union address
;
2605 union in_addr_union route_dst
;
2611 r
= parse_env_file(link
->state_file
, NEWLINE
,
2612 "NETWORK_FILE", &network_file
,
2613 "ADDRESSES", &addresses
,
2615 "DHCP4_ADDRESS", &dhcp4_address
,
2616 "IPV4LL_ADDRESS", &ipv4ll_address
,
2618 if (r
< 0 && r
!= -ENOENT
)
2619 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
2626 suffix
= strrchr(network_file
, '.');
2628 log_link_debug(link
, "Failed to get network name from %s", network_file
);
2629 goto network_file_fail
;
2633 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
2635 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
2636 goto network_file_fail
;
2639 r
= network_apply(network
, link
);
2641 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
2650 _cleanup_free_
char *address_str
= NULL
;
2651 char *prefixlen_str
;
2653 unsigned char prefixlen
;
2655 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
2657 log_link_debug_errno(link
, r
, "Failed to extract next address string: %m");
2663 prefixlen_str
= strchr(address_str
, '/');
2664 if (!prefixlen_str
) {
2665 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
2669 *prefixlen_str
++ = '\0';
2671 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
2673 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
2677 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
2679 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
2683 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
2685 return log_link_error_errno(link
, r
, "Failed to add address: %m");
2694 _cleanup_free_
char *route_str
= NULL
;
2695 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
2697 char *prefixlen_str
;
2699 unsigned char prefixlen
, tos
, table
;
2702 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
2704 log_link_debug_errno(link
, r
, "Failed to extract next route string: %m");
2710 prefixlen_str
= strchr(route_str
, '/');
2711 if (!prefixlen_str
) {
2712 log_link_debug(link
, "Failed to parse route %s", route_str
);
2716 *prefixlen_str
++ = '\0';
2718 r
= sscanf(prefixlen_str
, "%hhu/%hhu/%"SCNu32
"/%hhu/"USEC_FMT
, &prefixlen
, &tos
, &priority
, &table
, &lifetime
);
2720 log_link_debug(link
,
2721 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
2726 r
= in_addr_from_string_auto(route_str
, &family
, &route_dst
);
2728 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
2732 r
= route_add(link
, family
, &route_dst
, prefixlen
, tos
, priority
, table
, &route
);
2734 return log_link_error_errno(link
, r
, "Failed to add route: %m");
2736 if (lifetime
!= USEC_INFINITY
) {
2737 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(), lifetime
,
2738 0, route_expire_handler
, route
);
2740 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
2743 route
->lifetime
= lifetime
;
2744 sd_event_source_unref(route
->expire
);
2745 route
->expire
= expire
;
2750 if (dhcp4_address
) {
2751 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
2753 log_link_debug_errno(link
, r
, "Failed to parse DHCPv4 address %s: %m", dhcp4_address
);
2754 goto dhcp4_address_fail
;
2757 r
= sd_dhcp_client_new(&link
->dhcp_client
);
2759 return log_link_error_errno(link
, r
, "Failed to create DHCPv4 client: %m");
2761 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
2763 return log_link_error_errno(link
, r
, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address
);
2768 if (ipv4ll_address
) {
2769 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
2771 log_link_debug_errno(link
, r
, "Failed to parse IPv4LL address %s: %m", ipv4ll_address
);
2772 goto ipv4ll_address_fail
;
2775 r
= sd_ipv4ll_new(&link
->ipv4ll
);
2777 return log_link_error_errno(link
, r
, "Failed to create IPv4LL client: %m");
2779 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
2781 return log_link_error_errno(link
, r
, "Failed to set initial IPv4LL address %s: %m", ipv4ll_address
);
2784 ipv4ll_address_fail
:
2789 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
2791 _cleanup_udev_device_unref_
struct udev_device
*device
= NULL
;
2792 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
2800 r
= link_new(m
, message
, ret
);
2806 log_link_debug(link
, "Link %d added", link
->ifindex
);
2808 r
= link_load(link
);
2812 if (detect_container() <= 0) {
2813 /* not in a container, udev will be around */
2814 sprintf(ifindex_str
, "n%d", link
->ifindex
);
2815 device
= udev_device_new_from_device_id(m
->udev
, ifindex_str
);
2817 r
= log_link_warning_errno(link
, errno
, "Could not find udev device: %m");
2821 if (udev_device_get_is_initialized(device
) <= 0) {
2823 log_link_debug(link
, "link pending udev initialization...");
2827 r
= link_initialized(link
, device
);
2831 /* we are calling a callback directly, so must take a ref */
2834 r
= link_initialized_and_synced(m
->rtnl
, NULL
, link
);
2841 link_enter_failed(link
);
2845 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
2850 log_link_info(link
, "Gained IPv6LL");
2852 link
->ipv6ll_address
= *address
;
2853 link_check_ready(link
);
2855 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
2856 r
= link_acquire_ipv6_conf(link
);
2858 link_enter_failed(link
);
2866 static int link_carrier_gained(Link
*link
) {
2871 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
2872 r
= link_acquire_conf(link
);
2874 link_enter_failed(link
);
2878 r
= link_enter_set_addresses(link
);
2883 r
= link_handle_bound_by_list(link
);
2890 static int link_carrier_lost(Link
*link
) {
2895 r
= link_stop_clients(link
);
2897 link_enter_failed(link
);
2901 r
= link_drop_config(link
);
2905 if (!IN_SET(link
->state
, LINK_STATE_UNMANAGED
, LINK_STATE_PENDING
)) {
2906 log_link_debug(link
, "State is %s, dropping config", link_state_to_string(link
->state
));
2907 r
= link_drop_foreign_config(link
);
2912 r
= link_handle_bound_by_list(link
);
2919 int link_carrier_reset(Link
*link
) {
2924 if (link_has_carrier(link
)) {
2925 r
= link_carrier_lost(link
);
2929 r
= link_carrier_gained(link
);
2933 log_link_info(link
, "Reset carrier");
2939 int link_update(Link
*link
, sd_netlink_message
*m
) {
2940 struct ether_addr mac
;
2943 bool had_carrier
, carrier_gained
, carrier_lost
;
2947 assert(link
->ifname
);
2950 if (link
->state
== LINK_STATE_LINGER
) {
2952 log_link_info(link
, "Link readded");
2953 link_set_state(link
, LINK_STATE_ENSLAVING
);
2955 r
= link_new_carrier_maps(link
);
2960 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
2961 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
2962 log_link_info(link
, "Renamed to %s", ifname
);
2964 link_free_carrier_maps(link
);
2966 r
= free_and_strdup(&link
->ifname
, ifname
);
2970 r
= link_new_carrier_maps(link
);
2975 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
2976 if (r
>= 0 && mtu
> 0) {
2978 if (!link
->original_mtu
) {
2979 link
->original_mtu
= mtu
;
2980 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
2983 if (link
->dhcp_client
) {
2984 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
2987 log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
2993 /* The kernel may broadcast NEWLINK messages without the MAC address
2994 set, simply ignore them. */
2995 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
2997 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3000 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3003 log_link_debug(link
, "MAC address: "
3004 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
3005 mac
.ether_addr_octet
[0],
3006 mac
.ether_addr_octet
[1],
3007 mac
.ether_addr_octet
[2],
3008 mac
.ether_addr_octet
[3],
3009 mac
.ether_addr_octet
[4],
3010 mac
.ether_addr_octet
[5]);
3013 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
3015 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
3018 if (link
->dhcp_client
) {
3019 const DUID
*duid
= link_duid(link
);
3021 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
3022 (const uint8_t *) &link
->mac
,
3026 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
3028 r
= sd_dhcp_client_set_iaid_duid(link
->dhcp_client
,
3029 link
->network
->iaid
,
3031 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3032 duid
->raw_data_len
);
3034 return log_link_warning_errno(link
, r
, "Could not update DUID/IAID in DHCP client: %m");
3037 if (link
->dhcp6_client
) {
3038 const DUID
* duid
= link_duid(link
);
3040 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
3041 (const uint8_t *) &link
->mac
,
3045 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
3047 r
= sd_dhcp6_client_set_iaid(link
->dhcp6_client
,
3048 link
->network
->iaid
);
3050 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 IAID: %m");
3052 r
= sd_dhcp6_client_set_duid(link
->dhcp6_client
,
3054 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3055 duid
->raw_data_len
);
3057 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 DUID: %m");
3062 had_carrier
= link_has_carrier(link
);
3064 r
= link_update_flags(link
, m
);
3068 r
= link_update_lldp(link
);
3072 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
3073 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
3075 if (carrier_gained
) {
3076 log_link_info(link
, "Gained carrier");
3078 r
= link_carrier_gained(link
);
3081 } else if (carrier_lost
) {
3082 log_link_info(link
, "Lost carrier");
3084 r
= link_carrier_lost(link
);
3092 static void print_link_hashmap(FILE *f
, const char *prefix
, Hashmap
* h
) {
3100 if (hashmap_isempty(h
))
3104 HASHMAP_FOREACH(link
, h
, i
) {
3108 fprintf(f
, "%i", link
->ifindex
);
3115 int link_save(Link
*link
) {
3116 _cleanup_free_
char *temp_path
= NULL
;
3117 _cleanup_fclose_
FILE *f
= NULL
;
3118 const char *admin_state
, *oper_state
;
3125 assert(link
->state_file
);
3126 assert(link
->lease_file
);
3127 assert(link
->manager
);
3129 if (link
->state
== LINK_STATE_LINGER
) {
3130 unlink(link
->state_file
);
3134 link_lldp_save(link
);
3136 admin_state
= link_state_to_string(link
->state
);
3137 assert(admin_state
);
3139 oper_state
= link_operstate_to_string(link
->operstate
);
3142 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
3146 (void) fchmod(fileno(f
), 0644);
3149 "# This is private data. Do not parse.\n"
3152 admin_state
, oper_state
);
3154 if (link
->network
) {
3156 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
3157 const char *dhcp_domainname
= NULL
;
3158 char **dhcp6_domains
= NULL
;
3161 if (link
->dhcp6_client
) {
3162 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
, &dhcp6_lease
);
3163 if (r
< 0 && r
!= -ENOMSG
)
3164 log_link_debug(link
, "No DHCPv6 lease");
3167 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
3172 for (j
= 0; j
< link
->network
->n_dns
; j
++) {
3173 _cleanup_free_
char *b
= NULL
;
3175 r
= in_addr_to_string(link
->network
->dns
[j
].family
,
3176 &link
->network
->dns
[j
].address
, &b
);
3178 log_debug_errno(r
, "Failed to format address, ignoring: %m");
3188 if (link
->network
->dhcp_use_dns
&&
3190 const struct in_addr
*addresses
;
3192 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
3196 serialize_in_addrs(f
, addresses
, r
);
3201 if (link
->network
->dhcp_use_dns
&& dhcp6_lease
) {
3202 struct in6_addr
*in6_addrs
;
3204 r
= sd_dhcp6_lease_get_dns(dhcp6_lease
, &in6_addrs
);
3208 serialize_in6_addrs(f
, in6_addrs
, r
);
3213 /* Make sure to flush out old entries before we use the NDISC data */
3216 if (link
->network
->dhcp_use_dns
&& link
->ndisc_rdnss
) {
3219 SET_FOREACH(dd
, link
->ndisc_rdnss
, i
) {
3223 serialize_in6_addrs(f
, &dd
->address
, 1);
3232 fputstrv(f
, link
->network
->ntp
, NULL
, &space
);
3234 if (link
->network
->dhcp_use_ntp
&&
3236 const struct in_addr
*addresses
;
3238 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
3242 serialize_in_addrs(f
, addresses
, r
);
3247 if (link
->network
->dhcp_use_ntp
&& dhcp6_lease
) {
3248 struct in6_addr
*in6_addrs
;
3251 r
= sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease
,
3256 serialize_in6_addrs(f
, in6_addrs
, r
);
3260 r
= sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease
, &hosts
);
3262 fputstrv(f
, hosts
, NULL
, &space
);
3267 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
3268 if (link
->dhcp_lease
)
3269 (void) sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &dhcp_domainname
);
3271 (void) sd_dhcp6_lease_get_domains(dhcp6_lease
, &dhcp6_domains
);
3274 fputs("DOMAINS=", f
);
3275 fputstrv(f
, link
->network
->search_domains
, NULL
, &space
);
3277 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
) {
3280 if (dhcp_domainname
)
3281 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3283 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3285 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3286 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3291 fputs("ROUTE_DOMAINS=", f
);
3292 fputstrv(f
, link
->network
->route_domains
, NULL
, NULL
);
3294 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
) {
3297 if (dhcp_domainname
)
3298 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3300 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3302 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3303 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3308 fprintf(f
, "LLMNR=%s\n",
3309 resolve_support_to_string(link
->network
->llmnr
));
3310 fprintf(f
, "MDNS=%s\n",
3311 resolve_support_to_string(link
->network
->mdns
));
3313 if (link
->network
->dnssec_mode
!= _DNSSEC_MODE_INVALID
)
3314 fprintf(f
, "DNSSEC=%s\n",
3315 dnssec_mode_to_string(link
->network
->dnssec_mode
));
3317 if (!set_isempty(link
->network
->dnssec_negative_trust_anchors
)) {
3320 fputs("DNSSEC_NTA=", f
);
3322 SET_FOREACH(n
, link
->network
->dnssec_negative_trust_anchors
, i
)
3323 fputs_with_space(f
, n
, NULL
, &space
);
3327 fputs("ADDRESSES=", f
);
3329 SET_FOREACH(a
, link
->addresses
, i
) {
3330 _cleanup_free_
char *address_str
= NULL
;
3332 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
3336 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
3341 fputs("ROUTES=", f
);
3343 SET_FOREACH(route
, link
->routes
, i
) {
3344 _cleanup_free_
char *route_str
= NULL
;
3346 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
3350 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%hhu/"USEC_FMT
, space
? " " : "", route_str
,
3351 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
3358 print_link_hashmap(f
, "CARRIER_BOUND_TO=", link
->bound_to_links
);
3359 print_link_hashmap(f
, "CARRIER_BOUND_BY=", link
->bound_by_links
);
3361 if (link
->dhcp_lease
) {
3362 struct in_addr address
;
3363 const char *tz
= NULL
;
3365 assert(link
->network
);
3367 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
3369 fprintf(f
, "TIMEZONE=%s\n", tz
);
3371 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
3373 fputs("DHCP4_ADDRESS=", f
);
3374 serialize_in_addrs(f
, &address
, 1);
3378 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
3386 unlink(link
->lease_file
);
3389 struct in_addr address
;
3391 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
3393 fputs("IPV4LL_ADDRESS=", f
);
3394 serialize_in_addrs(f
, &address
, 1);
3399 r
= fflush_and_check(f
);
3403 if (rename(temp_path
, link
->state_file
) < 0) {
3411 (void) unlink(link
->state_file
);
3413 (void) unlink(temp_path
);
3415 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
3418 /* The serialized state in /run is no longer up-to-date. */
3419 void link_dirty(Link
*link
) {
3424 /* mark manager dirty as link is dirty */
3425 manager_dirty(link
->manager
);
3427 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
3429 /* allocation errors are ignored */
3432 r
= set_put(link
->manager
->dirty_links
, link
);
3434 /* don't take another ref if the link was already dirty */
3440 /* The serialized state in /run is up-to-date */
3441 void link_clean(Link
*link
) {
3443 assert(link
->manager
);
3445 set_remove(link
->manager
->dirty_links
, link
);
3449 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
3450 [LINK_STATE_PENDING
] = "pending",
3451 [LINK_STATE_ENSLAVING
] = "configuring",
3452 [LINK_STATE_SETTING_ADDRESSES
] = "configuring",
3453 [LINK_STATE_SETTING_ROUTES
] = "configuring",
3454 [LINK_STATE_CONFIGURED
] = "configured",
3455 [LINK_STATE_UNMANAGED
] = "unmanaged",
3456 [LINK_STATE_FAILED
] = "failed",
3457 [LINK_STATE_LINGER
] = "linger",
3460 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);
3462 static const char* const link_operstate_table
[_LINK_OPERSTATE_MAX
] = {
3463 [LINK_OPERSTATE_OFF
] = "off",
3464 [LINK_OPERSTATE_NO_CARRIER
] = "no-carrier",
3465 [LINK_OPERSTATE_DORMANT
] = "dormant",
3466 [LINK_OPERSTATE_CARRIER
] = "carrier",
3467 [LINK_OPERSTATE_DEGRADED
] = "degraded",
3468 [LINK_OPERSTATE_ROUTABLE
] = "routable",
3471 DEFINE_STRING_TABLE_LOOKUP(link_operstate
, LinkOperationalState
);