2 This file is part of systemd.
4 Copyright 2013 Tom Gundersen <teg@jklm.no>
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 #include <netinet/ether.h>
24 #include "alloc-util.h"
26 #include "dhcp-lease-internal.h"
29 #include "netlink-util.h"
30 #include "network-internal.h"
31 #include "networkd-ipv6-proxy-ndp.h"
32 #include "networkd-lldp-tx.h"
33 #include "networkd-manager.h"
34 #include "networkd-ndisc.h"
36 #include "socket-util.h"
37 #include "stdio-util.h"
38 #include "string-table.h"
39 #include "udev-util.h"
43 static bool link_dhcp6_enabled(Link
*link
) {
46 if (!socket_ipv6_is_supported())
49 if (link
->flags
& IFF_LOOPBACK
)
55 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV6
;
58 static bool link_dhcp4_enabled(Link
*link
) {
61 if (link
->flags
& IFF_LOOPBACK
)
67 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV4
;
70 static bool link_dhcp4_server_enabled(Link
*link
) {
73 if (link
->flags
& IFF_LOOPBACK
)
79 return link
->network
->dhcp_server
;
82 static bool link_ipv4ll_enabled(Link
*link
) {
85 if (link
->flags
& IFF_LOOPBACK
)
91 return link
->network
->link_local
& ADDRESS_FAMILY_IPV4
;
94 static bool link_ipv6ll_enabled(Link
*link
) {
97 if (!socket_ipv6_is_supported())
100 if (link
->flags
& IFF_LOOPBACK
)
106 return link
->network
->link_local
& ADDRESS_FAMILY_IPV6
;
109 static bool link_ipv6_enabled(Link
*link
) {
112 if (!socket_ipv6_is_supported())
115 if (link
->network
->bridge
)
118 /* DHCPv6 client will not be started if no IPv6 link-local address is configured. */
119 return link_ipv6ll_enabled(link
) || network_has_static_ipv6_addresses(link
->network
);
122 static bool link_lldp_rx_enabled(Link
*link
) {
125 if (link
->flags
& IFF_LOOPBACK
)
128 if (link
->iftype
!= ARPHRD_ETHER
)
134 if (link
->network
->bridge
)
137 return link
->network
->lldp_mode
!= LLDP_MODE_NO
;
140 static bool link_lldp_emit_enabled(Link
*link
) {
143 if (link
->flags
& IFF_LOOPBACK
)
146 if (link
->iftype
!= ARPHRD_ETHER
)
152 return link
->network
->lldp_emit
!= LLDP_EMIT_NO
;
155 static bool link_ipv4_forward_enabled(Link
*link
) {
158 if (link
->flags
& IFF_LOOPBACK
)
164 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
167 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV4
;
170 static bool link_ipv6_forward_enabled(Link
*link
) {
173 if (!socket_ipv6_is_supported())
176 if (link
->flags
& IFF_LOOPBACK
)
182 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
185 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV6
;
188 static bool link_proxy_arp_enabled(Link
*link
) {
191 if (link
->flags
& IFF_LOOPBACK
)
197 if (link
->network
->proxy_arp
< 0)
203 static bool link_ipv6_accept_ra_enabled(Link
*link
) {
206 if (!socket_ipv6_is_supported())
209 if (link
->flags
& IFF_LOOPBACK
)
215 /* If unset use system default (enabled if local forwarding is disabled.
216 * disabled if local forwarding is enabled).
217 * If set, ignore or enforce RA independent of local forwarding state.
219 if (link
->network
->ipv6_accept_ra
< 0)
220 /* default to accept RA if ip_forward is disabled and ignore RA if ip_forward is enabled */
221 return !link_ipv6_forward_enabled(link
);
222 else if (link
->network
->ipv6_accept_ra
> 0)
223 /* accept RA even if ip_forward is enabled */
230 static IPv6PrivacyExtensions
link_ipv6_privacy_extensions(Link
*link
) {
233 if (!socket_ipv6_is_supported())
234 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
236 if (link
->flags
& IFF_LOOPBACK
)
237 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
240 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
242 return link
->network
->ipv6_privacy_extensions
;
245 static int link_enable_ipv6(Link
*link
) {
246 const char *p
= NULL
;
250 if (link
->flags
& IFF_LOOPBACK
)
253 disabled
= !link_ipv6_enabled(link
);
255 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/disable_ipv6");
257 r
= write_string_file(p
, one_zero(disabled
), WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
259 log_link_warning_errno(link
, r
, "Cannot %s IPv6 for interface %s: %m",
260 enable_disable(!disabled
), link
->ifname
);
262 log_link_info(link
, "IPv6 successfully %sd", enable_disable(!disabled
));
267 void link_update_operstate(Link
*link
) {
268 LinkOperationalState operstate
;
271 if (link
->kernel_operstate
== IF_OPER_DORMANT
)
272 operstate
= LINK_OPERSTATE_DORMANT
;
273 else if (link_has_carrier(link
)) {
275 uint8_t scope
= RT_SCOPE_NOWHERE
;
278 /* if we have carrier, check what addresses we have */
279 SET_FOREACH(address
, link
->addresses
, i
) {
280 if (!address_is_ready(address
))
283 if (address
->scope
< scope
)
284 scope
= address
->scope
;
287 /* for operstate we also take foreign addresses into account */
288 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
289 if (!address_is_ready(address
))
292 if (address
->scope
< scope
)
293 scope
= address
->scope
;
296 if (scope
< RT_SCOPE_SITE
)
297 /* universally accessible addresses found */
298 operstate
= LINK_OPERSTATE_ROUTABLE
;
299 else if (scope
< RT_SCOPE_HOST
)
300 /* only link or site local addresses found */
301 operstate
= LINK_OPERSTATE_DEGRADED
;
303 /* no useful addresses found */
304 operstate
= LINK_OPERSTATE_CARRIER
;
305 } else if (link
->flags
& IFF_UP
)
306 operstate
= LINK_OPERSTATE_NO_CARRIER
;
308 operstate
= LINK_OPERSTATE_OFF
;
310 if (link
->operstate
!= operstate
) {
311 link
->operstate
= operstate
;
312 link_send_changed(link
, "OperationalState", NULL
);
317 #define FLAG_STRING(string, flag, old, new) \
318 (((old ^ new) & flag) \
319 ? ((old & flag) ? (" -" string) : (" +" string)) \
322 static int link_update_flags(Link
*link
, sd_netlink_message
*m
) {
323 unsigned flags
, unknown_flags_added
, unknown_flags_removed
, unknown_flags
;
329 r
= sd_rtnl_message_link_get_flags(m
, &flags
);
331 return log_link_warning_errno(link
, r
, "Could not get link flags: %m");
333 r
= sd_netlink_message_read_u8(m
, IFLA_OPERSTATE
, &operstate
);
335 /* if we got a message without operstate, take it to mean
336 the state was unchanged */
337 operstate
= link
->kernel_operstate
;
339 if ((link
->flags
== flags
) && (link
->kernel_operstate
== operstate
))
342 if (link
->flags
!= flags
) {
343 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",
344 FLAG_STRING("LOOPBACK", IFF_LOOPBACK
, link
->flags
, flags
),
345 FLAG_STRING("MASTER", IFF_MASTER
, link
->flags
, flags
),
346 FLAG_STRING("SLAVE", IFF_SLAVE
, link
->flags
, flags
),
347 FLAG_STRING("UP", IFF_UP
, link
->flags
, flags
),
348 FLAG_STRING("DORMANT", IFF_DORMANT
, link
->flags
, flags
),
349 FLAG_STRING("LOWER_UP", IFF_LOWER_UP
, link
->flags
, flags
),
350 FLAG_STRING("RUNNING", IFF_RUNNING
, link
->flags
, flags
),
351 FLAG_STRING("MULTICAST", IFF_MULTICAST
, link
->flags
, flags
),
352 FLAG_STRING("BROADCAST", IFF_BROADCAST
, link
->flags
, flags
),
353 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT
, link
->flags
, flags
),
354 FLAG_STRING("PROMISC", IFF_PROMISC
, link
->flags
, flags
),
355 FLAG_STRING("ALLMULTI", IFF_ALLMULTI
, link
->flags
, flags
),
356 FLAG_STRING("PORTSEL", IFF_PORTSEL
, link
->flags
, flags
),
357 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA
, link
->flags
, flags
),
358 FLAG_STRING("DYNAMIC", IFF_DYNAMIC
, link
->flags
, flags
),
359 FLAG_STRING("NOARP", IFF_NOARP
, link
->flags
, flags
),
360 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS
, link
->flags
, flags
),
361 FLAG_STRING("DEBUG", IFF_DEBUG
, link
->flags
, flags
),
362 FLAG_STRING("ECHO", IFF_ECHO
, link
->flags
, flags
));
364 unknown_flags
= ~(IFF_LOOPBACK
| IFF_MASTER
| IFF_SLAVE
| IFF_UP
|
365 IFF_DORMANT
| IFF_LOWER_UP
| IFF_RUNNING
|
366 IFF_MULTICAST
| IFF_BROADCAST
| IFF_POINTOPOINT
|
367 IFF_PROMISC
| IFF_ALLMULTI
| IFF_PORTSEL
|
368 IFF_AUTOMEDIA
| IFF_DYNAMIC
| IFF_NOARP
|
369 IFF_NOTRAILERS
| IFF_DEBUG
| IFF_ECHO
);
370 unknown_flags_added
= ((link
->flags
^ flags
) & flags
& unknown_flags
);
371 unknown_flags_removed
= ((link
->flags
^ flags
) & link
->flags
& unknown_flags
);
373 /* link flags are currently at most 18 bits, let's align to
375 if (unknown_flags_added
)
377 "Unknown link flags gained: %#.5x (ignoring)",
378 unknown_flags_added
);
380 if (unknown_flags_removed
)
382 "Unknown link flags lost: %#.5x (ignoring)",
383 unknown_flags_removed
);
387 link
->kernel_operstate
= operstate
;
389 link_update_operstate(link
);
394 static int link_new(Manager
*manager
, sd_netlink_message
*message
, Link
**ret
) {
395 _cleanup_link_unref_ Link
*link
= NULL
;
397 const char *ifname
, *kind
= NULL
;
399 unsigned short iftype
;
405 /* check for link kind */
406 r
= sd_netlink_message_enter_container(message
, IFLA_LINKINFO
);
408 (void)sd_netlink_message_read_string(message
, IFLA_INFO_KIND
, &kind
);
409 r
= sd_netlink_message_exit_container(message
);
414 r
= sd_netlink_message_get_type(message
, &type
);
417 else if (type
!= RTM_NEWLINK
)
420 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
423 else if (ifindex
<= 0)
426 r
= sd_rtnl_message_link_get_type(message
, &iftype
);
430 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &ifname
);
434 link
= new0(Link
, 1);
439 link
->manager
= manager
;
440 link
->state
= LINK_STATE_PENDING
;
441 link
->rtnl_extended_attrs
= true;
442 link
->ifindex
= ifindex
;
443 link
->iftype
= iftype
;
444 link
->ifname
= strdup(ifname
);
449 link
->kind
= strdup(kind
);
454 r
= sd_netlink_message_read_ether_addr(message
, IFLA_ADDRESS
, &link
->mac
);
456 log_link_debug_errno(link
, r
, "MAC address not found for new device, continuing without");
458 if (asprintf(&link
->state_file
, "/run/systemd/netif/links/%d", link
->ifindex
) < 0)
461 if (asprintf(&link
->lease_file
, "/run/systemd/netif/leases/%d", link
->ifindex
) < 0)
464 if (asprintf(&link
->lldp_file
, "/run/systemd/netif/lldp/%d", link
->ifindex
) < 0)
467 r
= hashmap_ensure_allocated(&manager
->links
, NULL
);
471 r
= hashmap_put(manager
->links
, INT_TO_PTR(link
->ifindex
), link
);
475 r
= link_update_flags(link
, message
);
485 static void link_free(Link
*link
) {
493 while (!set_isempty(link
->addresses
))
494 address_free(set_first(link
->addresses
));
496 while (!set_isempty(link
->addresses_foreign
))
497 address_free(set_first(link
->addresses_foreign
));
499 link
->addresses
= set_free(link
->addresses
);
501 link
->addresses_foreign
= set_free(link
->addresses_foreign
);
503 while ((address
= link
->pool_addresses
)) {
504 LIST_REMOVE(addresses
, link
->pool_addresses
, address
);
505 address_free(address
);
508 sd_dhcp_server_unref(link
->dhcp_server
);
509 sd_dhcp_client_unref(link
->dhcp_client
);
510 sd_dhcp_lease_unref(link
->dhcp_lease
);
512 link_lldp_emit_stop(link
);
514 free(link
->lease_file
);
516 sd_lldp_unref(link
->lldp
);
517 free(link
->lldp_file
);
521 sd_ipv4ll_unref(link
->ipv4ll
);
522 sd_dhcp6_client_unref(link
->dhcp6_client
);
523 sd_ndisc_unref(link
->ndisc
);
526 hashmap_remove(link
->manager
->links
, INT_TO_PTR(link
->ifindex
));
532 (void)unlink(link
->state_file
);
533 free(link
->state_file
);
535 udev_device_unref(link
->udev_device
);
537 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
)
538 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(carrier
->ifindex
));
539 hashmap_free(link
->bound_to_links
);
541 HASHMAP_FOREACH (carrier
, link
->bound_by_links
, i
)
542 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(carrier
->ifindex
));
543 hashmap_free(link
->bound_by_links
);
548 Link
*link_unref(Link
*link
) {
552 assert(link
->n_ref
> 0);
564 Link
*link_ref(Link
*link
) {
568 assert(link
->n_ref
> 0);
575 int link_get(Manager
*m
, int ifindex
, Link
**ret
) {
582 link
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
591 static void link_set_state(Link
*link
, LinkState state
) {
594 if (link
->state
== state
)
599 link_send_changed(link
, "AdministrativeState", NULL
);
602 static void link_enter_unmanaged(Link
*link
) {
605 log_link_debug(link
, "Unmanaged");
607 link_set_state(link
, LINK_STATE_UNMANAGED
);
612 static int link_stop_clients(Link
*link
) {
616 assert(link
->manager
);
617 assert(link
->manager
->event
);
619 if (link
->dhcp_client
) {
620 k
= sd_dhcp_client_stop(link
->dhcp_client
);
622 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv4 client: %m");
626 k
= sd_ipv4ll_stop(link
->ipv4ll
);
628 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 link-local: %m");
631 if (link
->dhcp6_client
) {
632 k
= sd_dhcp6_client_stop(link
->dhcp6_client
);
634 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv6 client: %m");
638 k
= sd_ndisc_stop(link
->ndisc
);
640 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Discovery: %m");
643 link_lldp_emit_stop(link
);
647 void link_enter_failed(Link
*link
) {
650 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
653 log_link_warning(link
, "Failed");
655 link_set_state(link
, LINK_STATE_FAILED
);
657 link_stop_clients(link
);
662 static Address
* link_find_dhcp_server_address(Link
*link
) {
666 assert(link
->network
);
668 /* The first statically configured address if there is any */
669 LIST_FOREACH(addresses
, address
, link
->network
->static_addresses
) {
671 if (address
->family
!= AF_INET
)
674 if (in_addr_is_null(address
->family
, &address
->in_addr
))
680 /* If that didn't work, find a suitable address we got from the pool */
681 LIST_FOREACH(addresses
, address
, link
->pool_addresses
) {
682 if (address
->family
!= AF_INET
)
691 static void link_enter_configured(Link
*link
) {
693 assert(link
->network
);
695 if (link
->state
!= LINK_STATE_SETTING_ROUTES
)
698 log_link_info(link
, "Configured");
700 link_set_state(link
, LINK_STATE_CONFIGURED
);
705 void link_check_ready(Link
*link
) {
711 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
717 if (!link
->static_configured
)
720 if (link_ipv4ll_enabled(link
))
721 if (!link
->ipv4ll_address
||
725 if (link_ipv6ll_enabled(link
))
726 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) > 0)
729 if ((link_dhcp4_enabled(link
) && !link_dhcp6_enabled(link
) &&
730 !link
->dhcp4_configured
) ||
731 (link_dhcp6_enabled(link
) && !link_dhcp4_enabled(link
) &&
732 !link
->dhcp6_configured
) ||
733 (link_dhcp4_enabled(link
) && link_dhcp6_enabled(link
) &&
734 !link
->dhcp4_configured
&& !link
->dhcp6_configured
))
737 if (link_ipv6_accept_ra_enabled(link
) && !link
->ndisc_configured
)
740 SET_FOREACH(a
, link
->addresses
, i
)
741 if (!address_is_ready(a
))
744 if (link
->state
!= LINK_STATE_CONFIGURED
)
745 link_enter_configured(link
);
750 static int route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
751 _cleanup_link_unref_ Link
*link
= userdata
;
754 assert(link
->link_messages
> 0);
755 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
756 LINK_STATE_SETTING_ROUTES
, LINK_STATE_FAILED
,
759 link
->link_messages
--;
761 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
764 r
= sd_netlink_message_get_errno(m
);
765 if (r
< 0 && r
!= -EEXIST
)
766 log_link_warning_errno(link
, r
, "Could not set route: %m");
768 if (link
->link_messages
== 0) {
769 log_link_debug(link
, "Routes set");
770 link
->static_configured
= true;
771 link_check_ready(link
);
777 static int link_enter_set_routes(Link
*link
) {
782 assert(link
->network
);
783 assert(link
->state
== LINK_STATE_SETTING_ADDRESSES
);
785 link_set_state(link
, LINK_STATE_SETTING_ROUTES
);
787 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
788 r
= route_configure(rt
, link
, route_handler
);
790 log_link_warning_errno(link
, r
, "Could not set routes: %m");
791 link_enter_failed(link
);
795 link
->link_messages
++;
798 if (link
->link_messages
== 0) {
799 link
->static_configured
= true;
800 link_check_ready(link
);
802 log_link_debug(link
, "Setting routes");
807 int link_route_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
808 _cleanup_link_unref_ Link
*link
= userdata
;
813 assert(link
->ifname
);
815 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
818 r
= sd_netlink_message_get_errno(m
);
819 if (r
< 0 && r
!= -ESRCH
)
820 log_link_warning_errno(link
, r
, "Could not drop route: %m");
825 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
826 _cleanup_link_unref_ Link
*link
= userdata
;
832 assert(link
->ifname
);
833 assert(link
->link_messages
> 0);
834 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
835 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
837 link
->link_messages
--;
839 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
842 r
= sd_netlink_message_get_errno(m
);
843 if (r
< 0 && r
!= -EEXIST
)
844 log_link_warning_errno(link
, r
, "could not set address: %m");
846 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
848 if (link
->link_messages
== 0) {
849 log_link_debug(link
, "Addresses set");
850 link_enter_set_routes(link
);
856 static int address_label_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
857 _cleanup_link_unref_ Link
*link
= userdata
;
863 assert(link
->ifname
);
864 assert(link
->link_messages
> 0);
866 link
->link_messages
--;
868 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
871 r
= sd_netlink_message_get_errno(m
);
872 if (r
< 0 && r
!= -EEXIST
)
873 log_link_warning_errno(link
, r
, "could not set address label: %m");
875 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
877 if (link
->link_messages
== 0) {
878 log_link_debug(link
, "Addresses label set");
879 link_enter_set_routes(link
);
885 static int link_push_uplink_dns_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
886 _cleanup_free_
struct in_addr
*addresses
= NULL
;
887 size_t n_addresses
= 0, n_allocated
= 0;
890 log_debug("Copying DNS server information from %s", link
->ifname
);
895 for (i
= 0; i
< link
->network
->n_dns
; i
++) {
898 /* Only look for IPv4 addresses */
899 if (link
->network
->dns
[i
].family
!= AF_INET
)
902 ia
= link
->network
->dns
[i
].address
.in
;
904 /* Never propagate obviously borked data */
905 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
908 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
911 addresses
[n_addresses
++] = ia
;
914 if (link
->network
->dhcp_use_dns
&& link
->dhcp_lease
) {
915 const struct in_addr
*da
= NULL
;
918 n
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &da
);
921 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
924 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
929 if (n_addresses
<= 0)
932 return sd_dhcp_server_set_dns(s
, addresses
, n_addresses
);
935 static int link_push_uplink_ntp_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
936 _cleanup_free_
struct in_addr
*addresses
= NULL
;
937 size_t n_addresses
= 0, n_allocated
= 0;
943 log_debug("Copying NTP server information from %s", link
->ifname
);
945 STRV_FOREACH(a
, link
->network
->ntp
) {
948 /* Only look for IPv4 addresses */
949 if (inet_pton(AF_INET
, *a
, &ia
) <= 0)
952 /* Never propagate obviously borked data */
953 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
956 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
959 addresses
[n_addresses
++] = ia
;
962 if (link
->network
->dhcp_use_ntp
&& link
->dhcp_lease
) {
963 const struct in_addr
*da
= NULL
;
966 n
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &da
);
969 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
972 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
977 if (n_addresses
<= 0)
980 return sd_dhcp_server_set_ntp(s
, addresses
, n_addresses
);
983 static int link_set_bridge_fdb(Link
*link
) {
987 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
988 r
= fdb_entry_configure(link
, fdb_entry
);
990 return log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
996 static int link_enter_set_addresses(Link
*link
) {
1002 assert(link
->network
);
1003 assert(link
->state
!= _LINK_STATE_INVALID
);
1005 r
= link_set_bridge_fdb(link
);
1009 link_set_state(link
, LINK_STATE_SETTING_ADDRESSES
);
1011 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
1012 r
= address_configure(ad
, link
, address_handler
, false);
1014 log_link_warning_errno(link
, r
, "Could not set addresses: %m");
1015 link_enter_failed(link
);
1019 link
->link_messages
++;
1022 LIST_FOREACH(labels
, label
, link
->network
->address_labels
) {
1023 r
= address_label_configure(label
, link
, address_label_handler
, false);
1025 log_link_warning_errno(link
, r
, "Could not set address label: %m");
1026 link_enter_failed(link
);
1030 link
->link_messages
++;
1033 /* now that we can figure out a default address for the dhcp server,
1035 if (link_dhcp4_server_enabled(link
)) {
1037 Link
*uplink
= NULL
;
1038 bool acquired_uplink
= false;
1040 address
= link_find_dhcp_server_address(link
);
1042 log_link_warning(link
, "Failed to find suitable address for DHCPv4 server instance.");
1043 link_enter_failed(link
);
1047 /* use the server address' subnet as the pool */
1048 r
= sd_dhcp_server_configure_pool(link
->dhcp_server
, &address
->in_addr
.in
, address
->prefixlen
,
1049 link
->network
->dhcp_server_pool_offset
, link
->network
->dhcp_server_pool_size
);
1054 r = sd_dhcp_server_set_router(link->dhcp_server,
1055 &main_address->in_addr.in);
1060 if (link
->network
->dhcp_server_max_lease_time_usec
> 0) {
1061 r
= sd_dhcp_server_set_max_lease_time(
1063 DIV_ROUND_UP(link
->network
->dhcp_server_max_lease_time_usec
, USEC_PER_SEC
));
1068 if (link
->network
->dhcp_server_default_lease_time_usec
> 0) {
1069 r
= sd_dhcp_server_set_default_lease_time(
1071 DIV_ROUND_UP(link
->network
->dhcp_server_default_lease_time_usec
, USEC_PER_SEC
));
1076 if (link
->network
->dhcp_server_emit_dns
) {
1078 if (link
->network
->n_dhcp_server_dns
> 0)
1079 r
= sd_dhcp_server_set_dns(link
->dhcp_server
, link
->network
->dhcp_server_dns
, link
->network
->n_dhcp_server_dns
);
1081 uplink
= manager_find_uplink(link
->manager
, link
);
1082 acquired_uplink
= true;
1085 log_link_debug(link
, "Not emitting DNS server information on link, couldn't find suitable uplink.");
1088 r
= link_push_uplink_dns_to_dhcp_server(uplink
, link
->dhcp_server
);
1091 log_link_warning_errno(link
, r
, "Failed to set DNS server for DHCP server, ignoring: %m");
1095 if (link
->network
->dhcp_server_emit_ntp
) {
1097 if (link
->network
->n_dhcp_server_ntp
> 0)
1098 r
= sd_dhcp_server_set_ntp(link
->dhcp_server
, link
->network
->dhcp_server_ntp
, link
->network
->n_dhcp_server_ntp
);
1100 if (!acquired_uplink
)
1101 uplink
= manager_find_uplink(link
->manager
, link
);
1104 log_link_debug(link
, "Not emitting NTP server information on link, couldn't find suitable uplink.");
1107 r
= link_push_uplink_ntp_to_dhcp_server(uplink
, link
->dhcp_server
);
1111 log_link_warning_errno(link
, r
, "Failed to set NTP server for DHCP server, ignoring: %m");
1114 r
= sd_dhcp_server_set_emit_router(link
->dhcp_server
, link
->network
->dhcp_server_emit_router
);
1116 log_link_warning_errno(link
, r
, "Failed to set router emission for DHCP server: %m");
1120 if (link
->network
->dhcp_server_emit_timezone
) {
1121 _cleanup_free_
char *buffer
= NULL
;
1122 const char *tz
= NULL
;
1124 if (link
->network
->dhcp_server_timezone
)
1125 tz
= link
->network
->dhcp_server_timezone
;
1127 r
= get_timezone(&buffer
);
1129 log_warning_errno(r
, "Failed to determine timezone: %m");
1135 r
= sd_dhcp_server_set_timezone(link
->dhcp_server
, tz
);
1141 r
= sd_dhcp_server_start(link
->dhcp_server
);
1143 log_link_warning_errno(link
, r
, "Could not start DHCPv4 server instance: %m");
1145 link_enter_failed(link
);
1150 log_link_debug(link
, "Offering DHCPv4 leases");
1153 if (link
->link_messages
== 0)
1154 link_enter_set_routes(link
);
1156 log_link_debug(link
, "Setting addresses");
1161 int link_address_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1162 _cleanup_link_unref_ Link
*link
= userdata
;
1167 assert(link
->ifname
);
1169 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1172 r
= sd_netlink_message_get_errno(m
);
1173 if (r
< 0 && r
!= -EADDRNOTAVAIL
)
1174 log_link_warning_errno(link
, r
, "Could not drop address: %m");
1179 static int link_set_bridge_vlan(Link
*link
) {
1182 r
= br_vlan_configure(link
, link
->network
->pvid
, link
->network
->br_vid_bitmap
, link
->network
->br_untagged_bitmap
);
1184 log_link_error_errno(link
, r
, "Failed to assign VLANs to bridge port: %m");
1189 static int link_set_proxy_arp(Link
*link
) {
1190 const char *p
= NULL
;
1193 if (!link_proxy_arp_enabled(link
))
1196 p
= strjoina("/proc/sys/net/ipv4/conf/", link
->ifname
, "/proxy_arp");
1198 r
= write_string_file(p
, one_zero(link
->network
->proxy_arp
), WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
1200 log_link_warning_errno(link
, r
, "Cannot configure proxy ARP for interface: %m");
1205 static int link_set_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1206 _cleanup_link_unref_ Link
*link
= userdata
;
1209 log_link_debug(link
, "Set link");
1211 r
= sd_netlink_message_get_errno(m
);
1212 if (r
< 0 && r
!= -EEXIST
) {
1213 log_link_error_errno(link
, r
, "Could not join netdev: %m");
1214 link_enter_failed(link
);
1221 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1222 _cleanup_link_unref_ Link
*link
= userdata
;
1227 assert(link
->ifname
);
1229 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1232 r
= sd_netlink_message_get_errno(m
);
1234 log_link_warning_errno(link
, r
, "Could not set MTU: %m");
1239 int link_set_mtu(Link
*link
, uint32_t mtu
) {
1240 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1244 assert(link
->manager
);
1245 assert(link
->manager
->rtnl
);
1247 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1249 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1251 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1253 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1255 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1257 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, set_mtu_handler
, link
, 0, NULL
);
1259 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1266 static int set_flags_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1267 _cleanup_link_unref_ Link
*link
= userdata
;
1272 assert(link
->ifname
);
1274 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1277 r
= sd_netlink_message_get_errno(m
);
1279 log_link_warning_errno(link
, r
, "Could not set link flags: %m");
1284 static int link_set_flags(Link
*link
) {
1285 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1286 unsigned ifi_change
= 0;
1287 unsigned ifi_flags
= 0;
1291 assert(link
->manager
);
1292 assert(link
->manager
->rtnl
);
1294 if (link
->flags
& IFF_LOOPBACK
)
1300 if (link
->network
->arp
< 0)
1303 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1305 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1307 if (link
->network
->arp
>= 0) {
1308 ifi_change
|= IFF_NOARP
;
1309 ifi_flags
|= link
->network
->arp
? 0 : IFF_NOARP
;
1312 r
= sd_rtnl_message_link_set_flags(req
, ifi_flags
, ifi_change
);
1314 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1316 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, set_flags_handler
, link
, 0, NULL
);
1318 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1325 static int link_set_bridge(Link
*link
) {
1326 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1330 assert(link
->network
);
1332 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1334 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1336 r
= sd_rtnl_message_link_set_family(req
, PF_BRIDGE
);
1338 return log_link_error_errno(link
, r
, "Could not set message family: %m");
1340 r
= sd_netlink_message_open_container(req
, IFLA_PROTINFO
);
1342 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1344 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_GUARD
, !link
->network
->use_bpdu
);
1346 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_GUARD attribute: %m");
1348 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MODE
, link
->network
->hairpin
);
1350 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MODE attribute: %m");
1352 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_FAST_LEAVE
, link
->network
->fast_leave
);
1354 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_FAST_LEAVE attribute: %m");
1356 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_PROTECT
, !link
->network
->allow_port_to_be_root
);
1358 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PROTECT attribute: %m");
1360 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_UNICAST_FLOOD
, link
->network
->unicast_flood
);
1362 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_UNICAST_FLOOD attribute: %m");
1364 if (link
->network
->cost
!= 0) {
1365 r
= sd_netlink_message_append_u32(req
, IFLA_BRPORT_COST
, link
->network
->cost
);
1367 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_COST attribute: %m");
1369 if (link
->network
->priority
!= LINK_BRIDGE_PORT_PRIORITY_INVALID
) {
1370 r
= sd_netlink_message_append_u16(req
, IFLA_BRPORT_PRIORITY
, link
->network
->priority
);
1372 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PRIORITY attribute: %m");
1375 r
= sd_netlink_message_close_container(req
);
1377 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1379 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_set_handler
, link
, 0, NULL
);
1381 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1388 static int link_bond_set(Link
*link
) {
1389 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1393 assert(link
->network
);
1395 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_NEWLINK
, link
->network
->bond
->ifindex
);
1397 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1399 r
= sd_netlink_message_set_flags(req
, NLM_F_REQUEST
| NLM_F_ACK
);
1401 return log_link_error_errno(link
, r
, "Could not set netlink flags: %m");
1403 r
= sd_netlink_message_open_container(req
, IFLA_LINKINFO
);
1405 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1407 r
= sd_netlink_message_open_container_union(req
, IFLA_INFO_DATA
, "bond");
1409 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1411 if (link
->network
->active_slave
) {
1412 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_ACTIVE_SLAVE
, link
->ifindex
);
1414 return log_link_error_errno(link
, r
, "Could not append IFLA_BOND_ACTIVE_SLAVE attribute: %m");
1417 if (link
->network
->primary_slave
) {
1418 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_PRIMARY
, link
->ifindex
);
1420 return log_link_error_errno(link
, r
, "Could not append IFLA_BOND_PRIMARY attribute: %m");
1423 r
= sd_netlink_message_close_container(req
);
1425 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1427 r
= sd_netlink_message_close_container(req
);
1429 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1431 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, set_flags_handler
, link
, 0, NULL
);
1433 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1440 static int link_lldp_save(Link
*link
) {
1441 _cleanup_free_
char *temp_path
= NULL
;
1442 _cleanup_fclose_
FILE *f
= NULL
;
1443 sd_lldp_neighbor
**l
= NULL
;
1447 assert(link
->lldp_file
);
1450 (void) unlink(link
->lldp_file
);
1454 r
= sd_lldp_get_neighbors(link
->lldp
, &l
);
1458 (void) unlink(link
->lldp_file
);
1464 r
= fopen_temporary(link
->lldp_file
, &f
, &temp_path
);
1468 fchmod(fileno(f
), 0644);
1470 for (i
= 0; i
< n
; i
++) {
1475 r
= sd_lldp_neighbor_get_raw(l
[i
], &p
, &sz
);
1480 (void) fwrite(&u
, 1, sizeof(u
), f
);
1481 (void) fwrite(p
, 1, sz
, f
);
1484 r
= fflush_and_check(f
);
1488 if (rename(temp_path
, link
->lldp_file
) < 0) {
1495 (void) unlink(link
->lldp_file
);
1497 (void) unlink(temp_path
);
1499 log_link_error_errno(link
, r
, "Failed to save LLDP data to %s: %m", link
->lldp_file
);
1503 for (i
= 0; i
< n
; i
++)
1504 sd_lldp_neighbor_unref(l
[i
]);
1511 static void lldp_handler(sd_lldp
*lldp
, sd_lldp_event event
, sd_lldp_neighbor
*n
, void *userdata
) {
1512 Link
*link
= userdata
;
1517 (void) link_lldp_save(link
);
1519 if (link_lldp_emit_enabled(link
) && event
== SD_LLDP_EVENT_ADDED
) {
1520 /* If we received information about a new neighbor, restart the LLDP "fast" logic */
1522 log_link_debug(link
, "Received LLDP datagram from previously unknown neighbor, restarting 'fast' LLDP transmission.");
1524 r
= link_lldp_emit_start(link
);
1526 log_link_warning_errno(link
, r
, "Failed to restart LLDP transmission: %m");
1530 static int link_acquire_ipv6_conf(Link
*link
) {
1535 if (link_dhcp6_enabled(link
)) {
1536 assert(link
->dhcp6_client
);
1537 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1539 /* start DHCPv6 client in stateless mode */
1540 r
= dhcp6_request_address(link
, true);
1541 if (r
< 0 && r
!= -EBUSY
)
1542 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv6 lease: %m");
1544 log_link_debug(link
, "Acquiring DHCPv6 lease");
1547 if (link_ipv6_accept_ra_enabled(link
)) {
1548 assert(link
->ndisc
);
1550 log_link_debug(link
, "Discovering IPv6 routers");
1552 r
= sd_ndisc_start(link
->ndisc
);
1553 if (r
< 0 && r
!= -EBUSY
)
1554 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1560 static int link_acquire_ipv4_conf(Link
*link
) {
1564 assert(link
->network
);
1565 assert(link
->manager
);
1566 assert(link
->manager
->event
);
1568 if (link_ipv4ll_enabled(link
)) {
1569 assert(link
->ipv4ll
);
1571 log_link_debug(link
, "Acquiring IPv4 link-local address");
1573 r
= sd_ipv4ll_start(link
->ipv4ll
);
1575 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1578 if (link_dhcp4_enabled(link
)) {
1579 assert(link
->dhcp_client
);
1581 log_link_debug(link
, "Acquiring DHCPv4 lease");
1583 r
= sd_dhcp_client_start(link
->dhcp_client
);
1585 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1591 static int link_acquire_conf(Link
*link
) {
1596 r
= link_acquire_ipv4_conf(link
);
1600 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) == 0) {
1601 r
= link_acquire_ipv6_conf(link
);
1606 if (link_lldp_emit_enabled(link
)) {
1607 r
= link_lldp_emit_start(link
);
1609 return log_link_warning_errno(link
, r
, "Failed to start LLDP transmission: %m");
1615 bool link_has_carrier(Link
*link
) {
1616 /* see Documentation/networking/operstates.txt in the kernel sources */
1618 if (link
->kernel_operstate
== IF_OPER_UP
)
1621 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1622 /* operstate may not be implemented, so fall back to flags */
1623 if ((link
->flags
& IFF_LOWER_UP
) && !(link
->flags
& IFF_DORMANT
))
1629 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1630 _cleanup_link_unref_ Link
*link
= userdata
;
1635 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1638 r
= sd_netlink_message_get_errno(m
);
1640 /* we warn but don't fail the link, as it may be
1642 log_link_warning_errno(link
, r
, "Could not bring up interface: %m");
1647 int link_up(Link
*link
) {
1648 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1649 uint8_t ipv6ll_mode
;
1653 assert(link
->network
);
1654 assert(link
->manager
);
1655 assert(link
->manager
->rtnl
);
1657 log_link_debug(link
, "Bringing link up");
1659 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1661 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1663 /* set it free if not enslaved with networkd */
1664 if (!link
->network
->bridge
&& !link
->network
->bond
&& !link
->network
->vrf
) {
1665 r
= sd_netlink_message_append_u32(req
, IFLA_MASTER
, 0);
1667 return log_link_error_errno(link
, r
, "Could not append IFLA_MASTER attribute: %m");
1670 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1672 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1674 if (link
->network
->mac
) {
1675 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
1677 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
1680 /* If IPv6 not configured (no static IPv6 address and IPv6LL autoconfiguration is disabled)
1681 for this interface, or if it is a bridge slave, then disable IPv6 else enable it. */
1682 (void) link_enable_ipv6(link
);
1684 if (link
->network
->mtu
) {
1685 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
1686 on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */
1687 if (link_ipv6_enabled(link
) && link
->network
->mtu
< IPV6_MIN_MTU
) {
1689 log_link_warning(link
, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU
) ", as "
1690 "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU
) " bytes: %m");
1692 link
->network
->mtu
= IPV6_MIN_MTU
;
1695 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, link
->network
->mtu
);
1697 return log_link_error_errno(link
, r
, "Could not set MTU: %m");
1700 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1702 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1704 if (link_ipv6_enabled(link
)) {
1705 /* if the kernel lacks ipv6 support setting IFF_UP fails if any ipv6 options are passed */
1706 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1708 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1710 if (!link_ipv6ll_enabled(link
))
1711 ipv6ll_mode
= IN6_ADDR_GEN_MODE_NONE
;
1713 const char *p
= NULL
;
1714 _cleanup_free_
char *stable_secret
= NULL
;
1716 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/stable_secret");
1717 r
= read_one_line_file(p
, &stable_secret
);
1720 ipv6ll_mode
= IN6_ADDR_GEN_MODE_EUI64
;
1722 ipv6ll_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
1724 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1726 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1728 if (!in_addr_is_null(AF_INET6
, &link
->network
->ipv6_token
)) {
1729 r
= sd_netlink_message_append_in6_addr(req
, IFLA_INET6_TOKEN
, &link
->network
->ipv6_token
.in6
);
1731 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_TOKEN: %m");
1734 r
= sd_netlink_message_close_container(req
);
1736 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1739 r
= sd_netlink_message_close_container(req
);
1741 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1743 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_up_handler
, link
, 0, NULL
);
1745 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1752 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1753 _cleanup_link_unref_ Link
*link
= userdata
;
1758 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1761 r
= sd_netlink_message_get_errno(m
);
1763 log_link_warning_errno(link
, r
, "Could not bring down interface: %m");
1768 int link_down(Link
*link
) {
1769 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1773 assert(link
->manager
);
1774 assert(link
->manager
->rtnl
);
1776 log_link_debug(link
, "Bringing link down");
1778 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
1779 RTM_SETLINK
, link
->ifindex
);
1781 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1783 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
1785 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1787 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_down_handler
, link
, 0, NULL
);
1789 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1796 static int link_up_can(Link
*link
) {
1797 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1802 log_link_debug(link
, "Bringing CAN link up");
1804 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1806 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1808 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1810 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1812 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_up_handler
, link
, 0, NULL
);
1814 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1821 static int link_handle_bound_to_list(Link
*link
) {
1825 bool required_up
= false;
1826 bool link_is_up
= false;
1830 if (hashmap_isempty(link
->bound_to_links
))
1833 if (link
->flags
& IFF_UP
)
1836 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
1837 if (link_has_carrier(l
)) {
1842 if (!required_up
&& link_is_up
) {
1843 r
= link_down(link
);
1846 } else if (required_up
&& !link_is_up
) {
1855 static int link_handle_bound_by_list(Link
*link
) {
1862 if (hashmap_isempty(link
->bound_by_links
))
1865 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
1866 r
= link_handle_bound_to_list(l
);
1874 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
1880 if (link
== carrier
)
1883 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
1886 r
= hashmap_ensure_allocated(h
, NULL
);
1890 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
1897 static int link_new_bound_by_list(Link
*link
) {
1902 bool list_updated
= false;
1905 assert(link
->manager
);
1909 HASHMAP_FOREACH(carrier
, m
->links
, i
) {
1910 if (!carrier
->network
)
1913 if (strv_isempty(carrier
->network
->bind_carrier
))
1916 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
, 0)) {
1917 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
1921 list_updated
= true;
1928 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
1929 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
1933 link_dirty(carrier
);
1939 static int link_new_bound_to_list(Link
*link
) {
1944 bool list_updated
= false;
1947 assert(link
->manager
);
1952 if (strv_isempty(link
->network
->bind_carrier
))
1957 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
1958 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
, 0)) {
1959 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
1963 list_updated
= true;
1970 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
1971 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
1975 link_dirty(carrier
);
1981 static int link_new_carrier_maps(Link
*link
) {
1984 r
= link_new_bound_by_list(link
);
1988 r
= link_handle_bound_by_list(link
);
1992 r
= link_new_bound_to_list(link
);
1996 r
= link_handle_bound_to_list(link
);
2003 static void link_free_bound_to_list(Link
*link
) {
2007 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
2008 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
2010 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
2011 link_dirty(bound_to
);
2017 static void link_free_bound_by_list(Link
*link
) {
2021 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
2022 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
2024 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
2025 link_dirty(bound_by
);
2026 link_handle_bound_to_list(bound_by
);
2033 static void link_free_carrier_maps(Link
*link
) {
2034 bool list_updated
= false;
2038 if (!hashmap_isempty(link
->bound_to_links
)) {
2039 link_free_bound_to_list(link
);
2040 list_updated
= true;
2043 if (!hashmap_isempty(link
->bound_by_links
)) {
2044 link_free_bound_by_list(link
);
2045 list_updated
= true;
2054 void link_drop(Link
*link
) {
2055 if (!link
|| link
->state
== LINK_STATE_LINGER
)
2058 link_set_state(link
, LINK_STATE_LINGER
);
2060 link_free_carrier_maps(link
);
2062 log_link_debug(link
, "Link removed");
2064 (void)unlink(link
->state_file
);
2070 static int link_joined(Link
*link
) {
2074 assert(link
->network
);
2076 if (!hashmap_isempty(link
->bound_to_links
)) {
2077 r
= link_handle_bound_to_list(link
);
2080 } else if (!(link
->flags
& IFF_UP
)) {
2083 link_enter_failed(link
);
2088 if (link
->network
->bridge
) {
2089 r
= link_set_bridge(link
);
2091 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
2094 if (link
->network
->bond
) {
2095 r
= link_bond_set(link
);
2097 log_link_error_errno(link
, r
, "Could not set bond message: %m");
2100 if (link
->network
->use_br_vlan
&&
2101 (link
->network
->bridge
|| streq_ptr("bridge", link
->kind
))) {
2102 r
= link_set_bridge_vlan(link
);
2104 log_link_error_errno(link
, r
, "Could not set bridge vlan: %m");
2107 return link_enter_set_addresses(link
);
2110 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
2111 _cleanup_link_unref_ Link
*link
= userdata
;
2115 assert(link
->network
);
2119 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2122 r
= sd_netlink_message_get_errno(m
);
2123 if (r
< 0 && r
!= -EEXIST
) {
2124 log_link_error_errno(link
, r
, "Could not join netdev: %m");
2125 link_enter_failed(link
);
2128 log_link_debug(link
, "Joined netdev");
2130 if (link
->enslaving
<= 0)
2136 static int link_enter_join_netdev(Link
*link
) {
2142 assert(link
->network
);
2143 assert(link
->state
== LINK_STATE_PENDING
);
2145 link_set_state(link
, LINK_STATE_ENSLAVING
);
2149 if (!link
->network
->bridge
&&
2150 !link
->network
->bond
&&
2151 !link
->network
->vrf
&&
2152 hashmap_isempty(link
->network
->stacked_netdevs
))
2153 return link_joined(link
);
2155 if (link
->network
->bond
) {
2156 log_struct(LOG_DEBUG
,
2157 LOG_LINK_INTERFACE(link
),
2158 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2159 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
),
2162 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
2164 log_struct_errno(LOG_WARNING
, r
,
2165 LOG_LINK_INTERFACE(link
),
2166 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2167 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
),
2170 link_enter_failed(link
);
2177 if (link
->network
->bridge
) {
2178 log_struct(LOG_DEBUG
,
2179 LOG_LINK_INTERFACE(link
),
2180 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2181 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
),
2184 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
2186 log_struct_errno(LOG_WARNING
, r
,
2187 LOG_LINK_INTERFACE(link
),
2188 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2189 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
),
2191 link_enter_failed(link
);
2198 if (link
->network
->vrf
) {
2199 log_struct(LOG_DEBUG
,
2200 LOG_LINK_INTERFACE(link
),
2201 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2202 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->vrf
->ifname
),
2204 r
= netdev_join(link
->network
->vrf
, link
, netdev_join_handler
);
2206 log_struct_errno(LOG_WARNING
, r
,
2207 LOG_LINK_INTERFACE(link
),
2208 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2209 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->vrf
->ifname
),
2211 link_enter_failed(link
);
2218 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
2220 log_struct(LOG_DEBUG
,
2221 LOG_LINK_INTERFACE(link
),
2222 LOG_NETDEV_INTERFACE(netdev
),
2223 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
),
2226 r
= netdev_join(netdev
, link
, netdev_join_handler
);
2228 log_struct_errno(LOG_WARNING
, r
,
2229 LOG_LINK_INTERFACE(link
),
2230 LOG_NETDEV_INTERFACE(netdev
),
2231 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
),
2233 link_enter_failed(link
);
2243 static int link_set_ipv4_forward(Link
*link
) {
2246 if (!link_ipv4_forward_enabled(link
))
2249 /* We propagate the forwarding flag from one interface to the
2250 * global setting one way. This means: as long as at least one
2251 * interface was configured at any time that had IP forwarding
2252 * enabled the setting will stay on for good. We do this
2253 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
2254 * somewhat in sync (see below). */
2256 r
= write_string_file("/proc/sys/net/ipv4/ip_forward", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2258 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
2263 static int link_set_ipv6_forward(Link
*link
) {
2266 if (!link_ipv6_forward_enabled(link
))
2269 /* On Linux, the IPv6 stack does not know a per-interface
2270 * packet forwarding setting: either packet forwarding is on
2271 * for all, or off for all. We hence don't bother with a
2272 * per-interface setting, but simply propagate the interface
2273 * flag, if it is set, to the global flag, one-way. Note that
2274 * while IPv4 would allow a per-interface flag, we expose the
2275 * same behaviour there and also propagate the setting from
2276 * one to all, to keep things simple (see above). */
2278 r
= write_string_file("/proc/sys/net/ipv6/conf/all/forwarding", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2280 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
2285 static int link_set_ipv6_privacy_extensions(Link
*link
) {
2286 char buf
[DECIMAL_STR_MAX(unsigned) + 1];
2287 IPv6PrivacyExtensions s
;
2288 const char *p
= NULL
;
2291 s
= link_ipv6_privacy_extensions(link
);
2295 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/use_tempaddr");
2296 xsprintf(buf
, "%u", (unsigned) link
->network
->ipv6_privacy_extensions
);
2298 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2300 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
2305 static int link_set_ipv6_accept_ra(Link
*link
) {
2306 const char *p
= NULL
;
2309 /* Make this a NOP if IPv6 is not available */
2310 if (!socket_ipv6_is_supported())
2313 if (link
->flags
& IFF_LOOPBACK
)
2319 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/accept_ra");
2321 /* We handle router advertisements ourselves, tell the kernel to GTFO */
2322 r
= write_string_file(p
, "0", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2324 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
2329 static int link_set_ipv6_dad_transmits(Link
*link
) {
2330 char buf
[DECIMAL_STR_MAX(int) + 1];
2331 const char *p
= NULL
;
2334 /* Make this a NOP if IPv6 is not available */
2335 if (!socket_ipv6_is_supported())
2338 if (link
->flags
& IFF_LOOPBACK
)
2344 if (link
->network
->ipv6_dad_transmits
< 0)
2347 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/dad_transmits");
2348 xsprintf(buf
, "%i", link
->network
->ipv6_dad_transmits
);
2350 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2352 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
2357 static int link_set_ipv6_hop_limit(Link
*link
) {
2358 char buf
[DECIMAL_STR_MAX(int) + 1];
2359 const char *p
= NULL
;
2362 /* Make this a NOP if IPv6 is not available */
2363 if (!socket_ipv6_is_supported())
2366 if (link
->flags
& IFF_LOOPBACK
)
2372 if (link
->network
->ipv6_hop_limit
< 0)
2375 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/hop_limit");
2376 xsprintf(buf
, "%i", link
->network
->ipv6_hop_limit
);
2378 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2380 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2385 static int link_drop_foreign_config(Link
*link
) {
2391 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
2392 /* we consider IPv6LL addresses to be managed by the kernel */
2393 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2396 r
= address_remove(address
, link
, link_address_remove_handler
);
2401 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2402 /* do not touch routes managed by the kernel */
2403 if (route
->protocol
== RTPROT_KERNEL
)
2406 r
= route_remove(route
, link
, link_route_remove_handler
);
2414 static int link_drop_config(Link
*link
) {
2420 SET_FOREACH(address
, link
->addresses
, i
) {
2421 /* we consider IPv6LL addresses to be managed by the kernel */
2422 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2425 r
= address_remove(address
, link
, link_address_remove_handler
);
2430 SET_FOREACH(route
, link
->routes
, i
) {
2431 /* do not touch routes managed by the kernel */
2432 if (route
->protocol
== RTPROT_KERNEL
)
2435 r
= route_remove(route
, link
, link_route_remove_handler
);
2445 static int link_update_lldp(Link
*link
) {
2453 if (link
->flags
& IFF_UP
) {
2454 r
= sd_lldp_start(link
->lldp
);
2456 log_link_debug(link
, "Started LLDP.");
2458 r
= sd_lldp_stop(link
->lldp
);
2460 log_link_debug(link
, "Stopped LLDP.");
2466 static int link_configure(Link
*link
) {
2470 assert(link
->network
);
2471 assert(link
->state
== LINK_STATE_PENDING
);
2473 if (streq_ptr(link
->kind
, "vcan")) {
2475 if (!(link
->flags
& IFF_UP
)) {
2476 r
= link_up_can(link
);
2478 link_enter_failed(link
);
2486 /* Drop foreign config, but ignore loopback or critical devices.
2487 * We do not want to remove loopback address or addresses used for root NFS. */
2488 if (!(link
->flags
& IFF_LOOPBACK
) && !(link
->network
->dhcp_critical
)) {
2489 r
= link_drop_foreign_config(link
);
2494 r
= link_set_proxy_arp(link
);
2498 r
= ipv6_proxy_ndp_addresses_configure(link
);
2502 r
= link_set_ipv4_forward(link
);
2506 r
= link_set_ipv6_forward(link
);
2510 r
= link_set_ipv6_privacy_extensions(link
);
2514 r
= link_set_ipv6_accept_ra(link
);
2518 r
= link_set_ipv6_dad_transmits(link
);
2522 r
= link_set_ipv6_hop_limit(link
);
2526 r
= link_set_flags(link
);
2530 if (link_ipv4ll_enabled(link
)) {
2531 r
= ipv4ll_configure(link
);
2536 if (link_dhcp4_enabled(link
)) {
2537 r
= dhcp4_configure(link
);
2542 if (link_dhcp4_server_enabled(link
)) {
2543 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
2547 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
2552 if (link_dhcp6_enabled(link
) ||
2553 link_ipv6_accept_ra_enabled(link
)) {
2554 r
= dhcp6_configure(link
);
2559 if (link_ipv6_accept_ra_enabled(link
)) {
2560 r
= ndisc_configure(link
);
2565 if (link_lldp_rx_enabled(link
)) {
2566 r
= sd_lldp_new(&link
->lldp
);
2570 r
= sd_lldp_set_ifindex(link
->lldp
, link
->ifindex
);
2574 r
= sd_lldp_match_capabilities(link
->lldp
,
2575 link
->network
->lldp_mode
== LLDP_MODE_ROUTERS_ONLY
?
2576 SD_LLDP_SYSTEM_CAPABILITIES_ALL_ROUTERS
:
2577 SD_LLDP_SYSTEM_CAPABILITIES_ALL
);
2581 r
= sd_lldp_set_filter_address(link
->lldp
, &link
->mac
);
2585 r
= sd_lldp_attach_event(link
->lldp
, NULL
, 0);
2589 r
= sd_lldp_set_callback(link
->lldp
, lldp_handler
, link
);
2593 r
= link_update_lldp(link
);
2598 if (link_has_carrier(link
)) {
2599 r
= link_acquire_conf(link
);
2604 return link_enter_join_netdev(link
);
2607 static int link_initialized_and_synced(sd_netlink
*rtnl
, sd_netlink_message
*m
,
2609 _cleanup_link_unref_ Link
*link
= userdata
;
2614 assert(link
->ifname
);
2615 assert(link
->manager
);
2617 if (link
->state
!= LINK_STATE_PENDING
)
2620 log_link_debug(link
, "Link state is up-to-date");
2622 r
= link_new_bound_by_list(link
);
2626 r
= link_handle_bound_by_list(link
);
2630 if (!link
->network
) {
2631 r
= network_get(link
->manager
, link
->udev_device
, link
->ifname
,
2632 &link
->mac
, &network
);
2634 link_enter_unmanaged(link
);
2636 } else if (r
== 0 && network
->unmanaged
) {
2637 link_enter_unmanaged(link
);
2642 if (link
->flags
& IFF_LOOPBACK
) {
2643 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
2644 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
2646 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
2647 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
2649 if (network
->dhcp_server
)
2650 log_link_debug(link
, "Ignoring DHCP server for loopback link");
2653 r
= network_apply(network
, link
);
2658 r
= link_new_bound_to_list(link
);
2662 r
= link_configure(link
);
2669 int link_initialized(Link
*link
, struct udev_device
*device
) {
2670 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2674 assert(link
->manager
);
2675 assert(link
->manager
->rtnl
);
2678 if (link
->state
!= LINK_STATE_PENDING
)
2681 if (link
->udev_device
)
2684 log_link_debug(link
, "udev initialized link");
2686 link
->udev_device
= udev_device_ref(device
);
2688 /* udev has initialized the link, but we don't know if we have yet
2689 * processed the NEWLINK messages with the latest state. Do a GETLINK,
2690 * when it returns we know that the pending NEWLINKs have already been
2691 * processed and that we are up-to-date */
2693 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
2698 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
,
2699 link_initialized_and_synced
, link
, 0, NULL
);
2708 static int link_load(Link
*link
) {
2709 _cleanup_free_
char *network_file
= NULL
,
2712 *dhcp4_address
= NULL
,
2713 *ipv4ll_address
= NULL
;
2714 union in_addr_union address
;
2715 union in_addr_union route_dst
;
2721 r
= parse_env_file(link
->state_file
, NEWLINE
,
2722 "NETWORK_FILE", &network_file
,
2723 "ADDRESSES", &addresses
,
2725 "DHCP4_ADDRESS", &dhcp4_address
,
2726 "IPV4LL_ADDRESS", &ipv4ll_address
,
2728 if (r
< 0 && r
!= -ENOENT
)
2729 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
2736 suffix
= strrchr(network_file
, '.');
2738 log_link_debug(link
, "Failed to get network name from %s", network_file
);
2739 goto network_file_fail
;
2743 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
2745 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
2746 goto network_file_fail
;
2749 r
= network_apply(network
, link
);
2751 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
2760 _cleanup_free_
char *address_str
= NULL
;
2761 char *prefixlen_str
;
2763 unsigned char prefixlen
;
2765 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
2767 log_link_debug_errno(link
, r
, "Failed to extract next address string: %m");
2773 prefixlen_str
= strchr(address_str
, '/');
2774 if (!prefixlen_str
) {
2775 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
2779 *prefixlen_str
++ = '\0';
2781 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
2783 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
2787 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
2789 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
2793 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
2795 return log_link_error_errno(link
, r
, "Failed to add address: %m");
2804 _cleanup_free_
char *route_str
= NULL
;
2805 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
2807 char *prefixlen_str
;
2809 unsigned char prefixlen
, tos
, table
;
2812 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
2814 log_link_debug_errno(link
, r
, "Failed to extract next route string: %m");
2820 prefixlen_str
= strchr(route_str
, '/');
2821 if (!prefixlen_str
) {
2822 log_link_debug(link
, "Failed to parse route %s", route_str
);
2826 *prefixlen_str
++ = '\0';
2828 r
= sscanf(prefixlen_str
, "%hhu/%hhu/%"SCNu32
"/%hhu/"USEC_FMT
, &prefixlen
, &tos
, &priority
, &table
, &lifetime
);
2830 log_link_debug(link
,
2831 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
2836 r
= in_addr_from_string_auto(route_str
, &family
, &route_dst
);
2838 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
2842 r
= route_add(link
, family
, &route_dst
, prefixlen
, tos
, priority
, table
, &route
);
2844 return log_link_error_errno(link
, r
, "Failed to add route: %m");
2846 if (lifetime
!= USEC_INFINITY
) {
2847 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(), lifetime
,
2848 0, route_expire_handler
, route
);
2850 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
2853 route
->lifetime
= lifetime
;
2854 sd_event_source_unref(route
->expire
);
2855 route
->expire
= expire
;
2860 if (dhcp4_address
) {
2861 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
2863 log_link_debug_errno(link
, r
, "Failed to parse DHCPv4 address %s: %m", dhcp4_address
);
2864 goto dhcp4_address_fail
;
2867 r
= sd_dhcp_client_new(&link
->dhcp_client
);
2869 return log_link_error_errno(link
, r
, "Failed to create DHCPv4 client: %m");
2871 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
2873 return log_link_error_errno(link
, r
, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address
);
2878 if (ipv4ll_address
) {
2879 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
2881 log_link_debug_errno(link
, r
, "Failed to parse IPv4LL address %s: %m", ipv4ll_address
);
2882 goto ipv4ll_address_fail
;
2885 r
= sd_ipv4ll_new(&link
->ipv4ll
);
2887 return log_link_error_errno(link
, r
, "Failed to create IPv4LL client: %m");
2889 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
2891 return log_link_error_errno(link
, r
, "Failed to set initial IPv4LL address %s: %m", ipv4ll_address
);
2894 ipv4ll_address_fail
:
2899 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
2901 _cleanup_udev_device_unref_
struct udev_device
*device
= NULL
;
2902 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
2910 r
= link_new(m
, message
, ret
);
2916 log_link_debug(link
, "Link %d added", link
->ifindex
);
2918 r
= link_load(link
);
2922 if (detect_container() <= 0) {
2923 /* not in a container, udev will be around */
2924 sprintf(ifindex_str
, "n%d", link
->ifindex
);
2925 device
= udev_device_new_from_device_id(m
->udev
, ifindex_str
);
2927 r
= log_link_warning_errno(link
, errno
, "Could not find udev device: %m");
2931 if (udev_device_get_is_initialized(device
) <= 0) {
2933 log_link_debug(link
, "link pending udev initialization...");
2937 r
= link_initialized(link
, device
);
2941 /* we are calling a callback directly, so must take a ref */
2944 r
= link_initialized_and_synced(m
->rtnl
, NULL
, link
);
2951 link_enter_failed(link
);
2955 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
2960 log_link_info(link
, "Gained IPv6LL");
2962 link
->ipv6ll_address
= *address
;
2963 link_check_ready(link
);
2965 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
2966 r
= link_acquire_ipv6_conf(link
);
2968 link_enter_failed(link
);
2976 static int link_carrier_gained(Link
*link
) {
2981 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
2982 r
= link_acquire_conf(link
);
2984 link_enter_failed(link
);
2988 r
= link_enter_set_addresses(link
);
2993 r
= link_handle_bound_by_list(link
);
3000 static int link_carrier_lost(Link
*link
) {
3005 r
= link_stop_clients(link
);
3007 link_enter_failed(link
);
3011 r
= link_drop_config(link
);
3015 if (!IN_SET(link
->state
, LINK_STATE_UNMANAGED
, LINK_STATE_PENDING
)) {
3016 log_link_debug(link
, "State is %s, dropping config", link_state_to_string(link
->state
));
3017 r
= link_drop_foreign_config(link
);
3022 r
= link_handle_bound_by_list(link
);
3029 int link_carrier_reset(Link
*link
) {
3034 if (link_has_carrier(link
)) {
3035 r
= link_carrier_lost(link
);
3039 r
= link_carrier_gained(link
);
3043 log_link_info(link
, "Reset carrier");
3049 int link_update(Link
*link
, sd_netlink_message
*m
) {
3050 struct ether_addr mac
;
3053 bool had_carrier
, carrier_gained
, carrier_lost
;
3057 assert(link
->ifname
);
3060 if (link
->state
== LINK_STATE_LINGER
) {
3062 log_link_info(link
, "Link readded");
3063 link_set_state(link
, LINK_STATE_ENSLAVING
);
3065 r
= link_new_carrier_maps(link
);
3070 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
3071 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
3072 log_link_info(link
, "Renamed to %s", ifname
);
3074 link_free_carrier_maps(link
);
3076 r
= free_and_strdup(&link
->ifname
, ifname
);
3080 r
= link_new_carrier_maps(link
);
3085 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
3086 if (r
>= 0 && mtu
> 0) {
3088 if (!link
->original_mtu
) {
3089 link
->original_mtu
= mtu
;
3090 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
3093 if (link
->dhcp_client
) {
3094 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
3097 log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
3103 /* The kernel may broadcast NEWLINK messages without the MAC address
3104 set, simply ignore them. */
3105 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
3107 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3110 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3113 log_link_debug(link
, "MAC address: "
3114 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
3115 mac
.ether_addr_octet
[0],
3116 mac
.ether_addr_octet
[1],
3117 mac
.ether_addr_octet
[2],
3118 mac
.ether_addr_octet
[3],
3119 mac
.ether_addr_octet
[4],
3120 mac
.ether_addr_octet
[5]);
3123 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
3125 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
3128 if (link
->dhcp_client
) {
3129 const DUID
*duid
= link_duid(link
);
3131 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
3132 (const uint8_t *) &link
->mac
,
3136 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
3138 r
= sd_dhcp_client_set_iaid_duid(link
->dhcp_client
,
3139 link
->network
->iaid
,
3141 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3142 duid
->raw_data_len
);
3144 return log_link_warning_errno(link
, r
, "Could not update DUID/IAID in DHCP client: %m");
3147 if (link
->dhcp6_client
) {
3148 const DUID
* duid
= link_duid(link
);
3150 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
3151 (const uint8_t *) &link
->mac
,
3155 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
3157 r
= sd_dhcp6_client_set_iaid(link
->dhcp6_client
,
3158 link
->network
->iaid
);
3160 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 IAID: %m");
3162 r
= sd_dhcp6_client_set_duid(link
->dhcp6_client
,
3164 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3165 duid
->raw_data_len
);
3167 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 DUID: %m");
3172 had_carrier
= link_has_carrier(link
);
3174 r
= link_update_flags(link
, m
);
3178 r
= link_update_lldp(link
);
3182 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
3183 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
3185 if (carrier_gained
) {
3186 log_link_info(link
, "Gained carrier");
3188 r
= link_carrier_gained(link
);
3191 } else if (carrier_lost
) {
3192 log_link_info(link
, "Lost carrier");
3194 r
= link_carrier_lost(link
);
3202 static void print_link_hashmap(FILE *f
, const char *prefix
, Hashmap
* h
) {
3210 if (hashmap_isempty(h
))
3214 HASHMAP_FOREACH(link
, h
, i
) {
3218 fprintf(f
, "%i", link
->ifindex
);
3225 int link_save(Link
*link
) {
3226 _cleanup_free_
char *temp_path
= NULL
;
3227 _cleanup_fclose_
FILE *f
= NULL
;
3228 const char *admin_state
, *oper_state
;
3235 assert(link
->state_file
);
3236 assert(link
->lease_file
);
3237 assert(link
->manager
);
3239 if (link
->state
== LINK_STATE_LINGER
) {
3240 unlink(link
->state_file
);
3244 link_lldp_save(link
);
3246 admin_state
= link_state_to_string(link
->state
);
3247 assert(admin_state
);
3249 oper_state
= link_operstate_to_string(link
->operstate
);
3252 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
3256 (void) fchmod(fileno(f
), 0644);
3259 "# This is private data. Do not parse.\n"
3262 admin_state
, oper_state
);
3264 if (link
->network
) {
3266 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
3267 const char *dhcp_domainname
= NULL
;
3268 char **dhcp6_domains
= NULL
;
3271 if (link
->dhcp6_client
) {
3272 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
, &dhcp6_lease
);
3273 if (r
< 0 && r
!= -ENOMSG
)
3274 log_link_debug(link
, "No DHCPv6 lease");
3277 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
3282 for (j
= 0; j
< link
->network
->n_dns
; j
++) {
3283 _cleanup_free_
char *b
= NULL
;
3285 r
= in_addr_to_string(link
->network
->dns
[j
].family
,
3286 &link
->network
->dns
[j
].address
, &b
);
3288 log_debug_errno(r
, "Failed to format address, ignoring: %m");
3298 if (link
->network
->dhcp_use_dns
&&
3300 const struct in_addr
*addresses
;
3302 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
3306 serialize_in_addrs(f
, addresses
, r
);
3311 if (link
->network
->dhcp_use_dns
&& dhcp6_lease
) {
3312 struct in6_addr
*in6_addrs
;
3314 r
= sd_dhcp6_lease_get_dns(dhcp6_lease
, &in6_addrs
);
3318 serialize_in6_addrs(f
, in6_addrs
, r
);
3323 /* Make sure to flush out old entries before we use the NDISC data */
3326 if (link
->network
->dhcp_use_dns
&& link
->ndisc_rdnss
) {
3329 SET_FOREACH(dd
, link
->ndisc_rdnss
, i
) {
3333 serialize_in6_addrs(f
, &dd
->address
, 1);
3342 fputstrv(f
, link
->network
->ntp
, NULL
, &space
);
3344 if (link
->network
->dhcp_use_ntp
&&
3346 const struct in_addr
*addresses
;
3348 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
3352 serialize_in_addrs(f
, addresses
, r
);
3357 if (link
->network
->dhcp_use_ntp
&& dhcp6_lease
) {
3358 struct in6_addr
*in6_addrs
;
3361 r
= sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease
,
3366 serialize_in6_addrs(f
, in6_addrs
, r
);
3370 r
= sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease
, &hosts
);
3372 fputstrv(f
, hosts
, NULL
, &space
);
3377 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
3378 if (link
->dhcp_lease
)
3379 (void) sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &dhcp_domainname
);
3381 (void) sd_dhcp6_lease_get_domains(dhcp6_lease
, &dhcp6_domains
);
3384 fputs("DOMAINS=", f
);
3385 fputstrv(f
, link
->network
->search_domains
, NULL
, &space
);
3387 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
) {
3390 if (dhcp_domainname
)
3391 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3393 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3395 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3396 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3401 fputs("ROUTE_DOMAINS=", f
);
3402 fputstrv(f
, link
->network
->route_domains
, NULL
, NULL
);
3404 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
) {
3407 if (dhcp_domainname
)
3408 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3410 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3412 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3413 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3418 fprintf(f
, "LLMNR=%s\n",
3419 resolve_support_to_string(link
->network
->llmnr
));
3420 fprintf(f
, "MDNS=%s\n",
3421 resolve_support_to_string(link
->network
->mdns
));
3423 if (link
->network
->dnssec_mode
!= _DNSSEC_MODE_INVALID
)
3424 fprintf(f
, "DNSSEC=%s\n",
3425 dnssec_mode_to_string(link
->network
->dnssec_mode
));
3427 if (!set_isempty(link
->network
->dnssec_negative_trust_anchors
)) {
3430 fputs("DNSSEC_NTA=", f
);
3432 SET_FOREACH(n
, link
->network
->dnssec_negative_trust_anchors
, i
)
3433 fputs_with_space(f
, n
, NULL
, &space
);
3437 fputs("ADDRESSES=", f
);
3439 SET_FOREACH(a
, link
->addresses
, i
) {
3440 _cleanup_free_
char *address_str
= NULL
;
3442 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
3446 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
3451 fputs("ROUTES=", f
);
3453 SET_FOREACH(route
, link
->routes
, i
) {
3454 _cleanup_free_
char *route_str
= NULL
;
3456 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
3460 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%hhu/"USEC_FMT
, space
? " " : "", route_str
,
3461 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
3468 print_link_hashmap(f
, "CARRIER_BOUND_TO=", link
->bound_to_links
);
3469 print_link_hashmap(f
, "CARRIER_BOUND_BY=", link
->bound_by_links
);
3471 if (link
->dhcp_lease
) {
3472 struct in_addr address
;
3473 const char *tz
= NULL
;
3475 assert(link
->network
);
3477 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
3479 fprintf(f
, "TIMEZONE=%s\n", tz
);
3481 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
3483 fputs("DHCP4_ADDRESS=", f
);
3484 serialize_in_addrs(f
, &address
, 1);
3488 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
3496 unlink(link
->lease_file
);
3499 struct in_addr address
;
3501 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
3503 fputs("IPV4LL_ADDRESS=", f
);
3504 serialize_in_addrs(f
, &address
, 1);
3509 r
= fflush_and_check(f
);
3513 if (rename(temp_path
, link
->state_file
) < 0) {
3521 (void) unlink(link
->state_file
);
3523 (void) unlink(temp_path
);
3525 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
3528 /* The serialized state in /run is no longer up-to-date. */
3529 void link_dirty(Link
*link
) {
3534 /* mark manager dirty as link is dirty */
3535 manager_dirty(link
->manager
);
3537 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
3539 /* allocation errors are ignored */
3542 r
= set_put(link
->manager
->dirty_links
, link
);
3544 /* don't take another ref if the link was already dirty */
3550 /* The serialized state in /run is up-to-date */
3551 void link_clean(Link
*link
) {
3553 assert(link
->manager
);
3555 set_remove(link
->manager
->dirty_links
, link
);
3559 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
3560 [LINK_STATE_PENDING
] = "pending",
3561 [LINK_STATE_ENSLAVING
] = "configuring",
3562 [LINK_STATE_SETTING_ADDRESSES
] = "configuring",
3563 [LINK_STATE_SETTING_ROUTES
] = "configuring",
3564 [LINK_STATE_CONFIGURED
] = "configured",
3565 [LINK_STATE_UNMANAGED
] = "unmanaged",
3566 [LINK_STATE_FAILED
] = "failed",
3567 [LINK_STATE_LINGER
] = "linger",
3570 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);
3572 static const char* const link_operstate_table
[_LINK_OPERSTATE_MAX
] = {
3573 [LINK_OPERSTATE_OFF
] = "off",
3574 [LINK_OPERSTATE_NO_CARRIER
] = "no-carrier",
3575 [LINK_OPERSTATE_DORMANT
] = "dormant",
3576 [LINK_OPERSTATE_CARRIER
] = "carrier",
3577 [LINK_OPERSTATE_DEGRADED
] = "degraded",
3578 [LINK_OPERSTATE_ROUTABLE
] = "routable",
3581 DEFINE_STRING_TABLE_LOOKUP(link_operstate
, LinkOperationalState
);