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 link_push_uplink_dns_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
857 _cleanup_free_
struct in_addr
*addresses
= NULL
;
858 size_t n_addresses
= 0, n_allocated
= 0;
861 log_debug("Copying DNS server information from %s", link
->ifname
);
866 for (i
= 0; i
< link
->network
->n_dns
; i
++) {
869 /* Only look for IPv4 addresses */
870 if (link
->network
->dns
[i
].family
!= AF_INET
)
873 ia
= link
->network
->dns
[i
].address
.in
;
875 /* Never propagate obviously borked data */
876 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
879 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
882 addresses
[n_addresses
++] = ia
;
885 if (link
->network
->dhcp_use_dns
&& link
->dhcp_lease
) {
886 const struct in_addr
*da
= NULL
;
889 n
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &da
);
892 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
895 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
900 if (n_addresses
<= 0)
903 return sd_dhcp_server_set_dns(s
, addresses
, n_addresses
);
906 static int link_push_uplink_ntp_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
907 _cleanup_free_
struct in_addr
*addresses
= NULL
;
908 size_t n_addresses
= 0, n_allocated
= 0;
914 log_debug("Copying NTP server information from %s", link
->ifname
);
916 STRV_FOREACH(a
, link
->network
->ntp
) {
919 /* Only look for IPv4 addresses */
920 if (inet_pton(AF_INET
, *a
, &ia
) <= 0)
923 /* Never propagate obviously borked data */
924 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
927 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
930 addresses
[n_addresses
++] = ia
;
933 if (link
->network
->dhcp_use_ntp
&& link
->dhcp_lease
) {
934 const struct in_addr
*da
= NULL
;
937 n
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &da
);
940 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
943 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
948 if (n_addresses
<= 0)
951 return sd_dhcp_server_set_ntp(s
, addresses
, n_addresses
);
954 static int link_set_bridge_fdb(Link
*link
) {
958 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
959 r
= fdb_entry_configure(link
, fdb_entry
);
961 return log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
967 static int link_enter_set_addresses(Link
*link
) {
972 assert(link
->network
);
973 assert(link
->state
!= _LINK_STATE_INVALID
);
975 r
= link_set_bridge_fdb(link
);
979 link_set_state(link
, LINK_STATE_SETTING_ADDRESSES
);
981 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
982 r
= address_configure(ad
, link
, address_handler
, false);
984 log_link_warning_errno(link
, r
, "Could not set addresses: %m");
985 link_enter_failed(link
);
989 link
->link_messages
++;
992 /* now that we can figure out a default address for the dhcp server,
994 if (link_dhcp4_server_enabled(link
)) {
997 bool acquired_uplink
= false;
999 address
= link_find_dhcp_server_address(link
);
1001 log_link_warning(link
, "Failed to find suitable address for DHCPv4 server instance.");
1002 link_enter_failed(link
);
1006 /* use the server address' subnet as the pool */
1007 r
= sd_dhcp_server_configure_pool(link
->dhcp_server
, &address
->in_addr
.in
, address
->prefixlen
,
1008 link
->network
->dhcp_server_pool_offset
, link
->network
->dhcp_server_pool_size
);
1013 r = sd_dhcp_server_set_router(link->dhcp_server,
1014 &main_address->in_addr.in);
1019 if (link
->network
->dhcp_server_max_lease_time_usec
> 0) {
1020 r
= sd_dhcp_server_set_max_lease_time(
1022 DIV_ROUND_UP(link
->network
->dhcp_server_max_lease_time_usec
, USEC_PER_SEC
));
1027 if (link
->network
->dhcp_server_default_lease_time_usec
> 0) {
1028 r
= sd_dhcp_server_set_default_lease_time(
1030 DIV_ROUND_UP(link
->network
->dhcp_server_default_lease_time_usec
, USEC_PER_SEC
));
1035 if (link
->network
->dhcp_server_emit_dns
) {
1037 if (link
->network
->n_dhcp_server_dns
> 0)
1038 r
= sd_dhcp_server_set_dns(link
->dhcp_server
, link
->network
->dhcp_server_dns
, link
->network
->n_dhcp_server_dns
);
1040 uplink
= manager_find_uplink(link
->manager
, link
);
1041 acquired_uplink
= true;
1044 log_link_debug(link
, "Not emitting DNS server information on link, couldn't find suitable uplink.");
1047 r
= link_push_uplink_dns_to_dhcp_server(uplink
, link
->dhcp_server
);
1050 log_link_warning_errno(link
, r
, "Failed to set DNS server for DHCP server, ignoring: %m");
1054 if (link
->network
->dhcp_server_emit_ntp
) {
1056 if (link
->network
->n_dhcp_server_ntp
> 0)
1057 r
= sd_dhcp_server_set_ntp(link
->dhcp_server
, link
->network
->dhcp_server_ntp
, link
->network
->n_dhcp_server_ntp
);
1059 if (!acquired_uplink
)
1060 uplink
= manager_find_uplink(link
->manager
, link
);
1063 log_link_debug(link
, "Not emitting NTP server information on link, couldn't find suitable uplink.");
1066 r
= link_push_uplink_ntp_to_dhcp_server(uplink
, link
->dhcp_server
);
1070 log_link_warning_errno(link
, r
, "Failed to set NTP server for DHCP server, ignoring: %m");
1073 r
= sd_dhcp_server_set_emit_router(link
->dhcp_server
, link
->network
->dhcp_server_emit_router
);
1075 log_link_warning_errno(link
, r
, "Failed to set router emission for DHCP server: %m");
1079 if (link
->network
->dhcp_server_emit_timezone
) {
1080 _cleanup_free_
char *buffer
= NULL
;
1081 const char *tz
= NULL
;
1083 if (link
->network
->dhcp_server_timezone
)
1084 tz
= link
->network
->dhcp_server_timezone
;
1086 r
= get_timezone(&buffer
);
1088 log_warning_errno(r
, "Failed to determine timezone: %m");
1094 r
= sd_dhcp_server_set_timezone(link
->dhcp_server
, tz
);
1100 r
= sd_dhcp_server_start(link
->dhcp_server
);
1102 log_link_warning_errno(link
, r
, "Could not start DHCPv4 server instance: %m");
1104 link_enter_failed(link
);
1109 log_link_debug(link
, "Offering DHCPv4 leases");
1112 if (link
->link_messages
== 0)
1113 link_enter_set_routes(link
);
1115 log_link_debug(link
, "Setting addresses");
1120 int link_address_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1121 _cleanup_link_unref_ Link
*link
= userdata
;
1126 assert(link
->ifname
);
1128 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1131 r
= sd_netlink_message_get_errno(m
);
1132 if (r
< 0 && r
!= -EADDRNOTAVAIL
)
1133 log_link_warning_errno(link
, r
, "Could not drop address: %m");
1138 static int link_set_bridge_vlan(Link
*link
) {
1141 r
= br_vlan_configure(link
, link
->network
->pvid
, link
->network
->br_vid_bitmap
, link
->network
->br_untagged_bitmap
);
1143 log_link_error_errno(link
, r
, "Failed to assign VLANs to bridge port: %m");
1148 static int link_set_proxy_arp(Link
*link
) {
1149 const char *p
= NULL
;
1152 if (!link_proxy_arp_enabled(link
))
1155 p
= strjoina("/proc/sys/net/ipv4/conf/", link
->ifname
, "/proxy_arp");
1157 r
= write_string_file(p
, one_zero(link
->network
->proxy_arp
), WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
1159 log_link_warning_errno(link
, r
, "Cannot configure proxy ARP for interface: %m");
1164 static int link_set_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1165 _cleanup_link_unref_ Link
*link
= userdata
;
1168 log_link_debug(link
, "Set link");
1170 r
= sd_netlink_message_get_errno(m
);
1171 if (r
< 0 && r
!= -EEXIST
) {
1172 log_link_error_errno(link
, r
, "Could not join netdev: %m");
1173 link_enter_failed(link
);
1180 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1181 _cleanup_link_unref_ Link
*link
= userdata
;
1186 assert(link
->ifname
);
1188 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1191 r
= sd_netlink_message_get_errno(m
);
1193 log_link_warning_errno(link
, r
, "Could not set MTU: %m");
1198 int link_set_mtu(Link
*link
, uint32_t mtu
) {
1199 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1203 assert(link
->manager
);
1204 assert(link
->manager
->rtnl
);
1206 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1208 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1210 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1212 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1214 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1216 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, set_mtu_handler
, link
, 0, NULL
);
1218 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1225 static int set_flags_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1226 _cleanup_link_unref_ Link
*link
= userdata
;
1231 assert(link
->ifname
);
1233 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1236 r
= sd_netlink_message_get_errno(m
);
1238 log_link_warning_errno(link
, r
, "Could not set link flags: %m");
1243 static int link_set_flags(Link
*link
) {
1244 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1245 unsigned ifi_change
= 0;
1246 unsigned ifi_flags
= 0;
1250 assert(link
->manager
);
1251 assert(link
->manager
->rtnl
);
1253 if (link
->flags
& IFF_LOOPBACK
)
1259 if (link
->network
->arp
< 0)
1262 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1264 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1266 if (link
->network
->arp
>= 0) {
1267 ifi_change
|= IFF_NOARP
;
1268 ifi_flags
|= link
->network
->arp
? 0 : IFF_NOARP
;
1271 r
= sd_rtnl_message_link_set_flags(req
, ifi_flags
, ifi_change
);
1273 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1275 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, set_flags_handler
, link
, 0, NULL
);
1277 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1284 static int link_set_bridge(Link
*link
) {
1285 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1289 assert(link
->network
);
1291 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1293 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1295 r
= sd_rtnl_message_link_set_family(req
, PF_BRIDGE
);
1297 return log_link_error_errno(link
, r
, "Could not set message family: %m");
1299 r
= sd_netlink_message_open_container(req
, IFLA_PROTINFO
);
1301 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1303 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_GUARD
, !link
->network
->use_bpdu
);
1305 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_GUARD attribute: %m");
1307 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MODE
, link
->network
->hairpin
);
1309 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MODE attribute: %m");
1311 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_FAST_LEAVE
, link
->network
->fast_leave
);
1313 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_FAST_LEAVE attribute: %m");
1315 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_PROTECT
, !link
->network
->allow_port_to_be_root
);
1317 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PROTECT attribute: %m");
1319 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_UNICAST_FLOOD
, link
->network
->unicast_flood
);
1321 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_UNICAST_FLOOD attribute: %m");
1323 if (link
->network
->cost
!= 0) {
1324 r
= sd_netlink_message_append_u32(req
, IFLA_BRPORT_COST
, link
->network
->cost
);
1326 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_COST attribute: %m");
1328 if (link
->network
->priority
!= LINK_BRIDGE_PORT_PRIORITY_INVALID
) {
1329 r
= sd_netlink_message_append_u16(req
, IFLA_BRPORT_PRIORITY
, link
->network
->priority
);
1331 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PRIORITY attribute: %m");
1334 r
= sd_netlink_message_close_container(req
);
1336 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1338 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_set_handler
, link
, 0, NULL
);
1340 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1347 static int link_bond_set(Link
*link
) {
1348 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1352 assert(link
->network
);
1354 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_NEWLINK
, link
->network
->bond
->ifindex
);
1356 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1358 r
= sd_netlink_message_set_flags(req
, NLM_F_REQUEST
| NLM_F_ACK
);
1360 return log_link_error_errno(link
, r
, "Could not set netlink flags: %m");
1362 r
= sd_netlink_message_open_container(req
, IFLA_LINKINFO
);
1364 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1366 r
= sd_netlink_message_open_container_union(req
, IFLA_INFO_DATA
, "bond");
1368 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1370 if (link
->network
->active_slave
) {
1371 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_ACTIVE_SLAVE
, link
->ifindex
);
1373 return log_link_error_errno(link
, r
, "Could not append IFLA_BOND_ACTIVE_SLAVE attribute: %m");
1376 if (link
->network
->primary_slave
) {
1377 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_PRIMARY
, link
->ifindex
);
1379 return log_link_error_errno(link
, r
, "Could not append IFLA_BOND_PRIMARY attribute: %m");
1382 r
= sd_netlink_message_close_container(req
);
1384 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1386 r
= sd_netlink_message_close_container(req
);
1388 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
1390 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, set_flags_handler
, link
, 0, NULL
);
1392 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1399 static int link_lldp_save(Link
*link
) {
1400 _cleanup_free_
char *temp_path
= NULL
;
1401 _cleanup_fclose_
FILE *f
= NULL
;
1402 sd_lldp_neighbor
**l
= NULL
;
1406 assert(link
->lldp_file
);
1409 (void) unlink(link
->lldp_file
);
1413 r
= sd_lldp_get_neighbors(link
->lldp
, &l
);
1417 (void) unlink(link
->lldp_file
);
1423 r
= fopen_temporary(link
->lldp_file
, &f
, &temp_path
);
1427 fchmod(fileno(f
), 0644);
1429 for (i
= 0; i
< n
; i
++) {
1434 r
= sd_lldp_neighbor_get_raw(l
[i
], &p
, &sz
);
1439 (void) fwrite(&u
, 1, sizeof(u
), f
);
1440 (void) fwrite(p
, 1, sz
, f
);
1443 r
= fflush_and_check(f
);
1447 if (rename(temp_path
, link
->lldp_file
) < 0) {
1454 (void) unlink(link
->lldp_file
);
1456 (void) unlink(temp_path
);
1458 log_link_error_errno(link
, r
, "Failed to save LLDP data to %s: %m", link
->lldp_file
);
1462 for (i
= 0; i
< n
; i
++)
1463 sd_lldp_neighbor_unref(l
[i
]);
1470 static void lldp_handler(sd_lldp
*lldp
, sd_lldp_event event
, sd_lldp_neighbor
*n
, void *userdata
) {
1471 Link
*link
= userdata
;
1476 (void) link_lldp_save(link
);
1478 if (link_lldp_emit_enabled(link
) && event
== SD_LLDP_EVENT_ADDED
) {
1479 /* If we received information about a new neighbor, restart the LLDP "fast" logic */
1481 log_link_debug(link
, "Received LLDP datagram from previously unknown neighbor, restarting 'fast' LLDP transmission.");
1483 r
= link_lldp_emit_start(link
);
1485 log_link_warning_errno(link
, r
, "Failed to restart LLDP transmission: %m");
1489 static int link_acquire_ipv6_conf(Link
*link
) {
1494 if (link_dhcp6_enabled(link
)) {
1495 assert(link
->dhcp6_client
);
1496 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1498 /* start DHCPv6 client in stateless mode */
1499 r
= dhcp6_request_address(link
, true);
1500 if (r
< 0 && r
!= -EBUSY
)
1501 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv6 lease: %m");
1503 log_link_debug(link
, "Acquiring DHCPv6 lease");
1506 if (link_ipv6_accept_ra_enabled(link
)) {
1507 assert(link
->ndisc
);
1509 log_link_debug(link
, "Discovering IPv6 routers");
1511 r
= sd_ndisc_start(link
->ndisc
);
1512 if (r
< 0 && r
!= -EBUSY
)
1513 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1519 static int link_acquire_ipv4_conf(Link
*link
) {
1523 assert(link
->network
);
1524 assert(link
->manager
);
1525 assert(link
->manager
->event
);
1527 if (link_ipv4ll_enabled(link
)) {
1528 assert(link
->ipv4ll
);
1530 log_link_debug(link
, "Acquiring IPv4 link-local address");
1532 r
= sd_ipv4ll_start(link
->ipv4ll
);
1534 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1537 if (link_dhcp4_enabled(link
)) {
1538 assert(link
->dhcp_client
);
1540 log_link_debug(link
, "Acquiring DHCPv4 lease");
1542 r
= sd_dhcp_client_start(link
->dhcp_client
);
1544 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1550 static int link_acquire_conf(Link
*link
) {
1555 r
= link_acquire_ipv4_conf(link
);
1559 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) == 0) {
1560 r
= link_acquire_ipv6_conf(link
);
1565 if (link_lldp_emit_enabled(link
)) {
1566 r
= link_lldp_emit_start(link
);
1568 return log_link_warning_errno(link
, r
, "Failed to start LLDP transmission: %m");
1574 bool link_has_carrier(Link
*link
) {
1575 /* see Documentation/networking/operstates.txt in the kernel sources */
1577 if (link
->kernel_operstate
== IF_OPER_UP
)
1580 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1581 /* operstate may not be implemented, so fall back to flags */
1582 if ((link
->flags
& IFF_LOWER_UP
) && !(link
->flags
& IFF_DORMANT
))
1588 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1589 _cleanup_link_unref_ Link
*link
= userdata
;
1594 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1597 r
= sd_netlink_message_get_errno(m
);
1599 /* we warn but don't fail the link, as it may be
1601 log_link_warning_errno(link
, r
, "Could not bring up interface: %m");
1606 static int link_up(Link
*link
) {
1607 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1608 uint8_t ipv6ll_mode
;
1612 assert(link
->network
);
1613 assert(link
->manager
);
1614 assert(link
->manager
->rtnl
);
1616 log_link_debug(link
, "Bringing link up");
1618 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1620 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1622 /* set it free if not enslaved with networkd */
1623 if (!link
->network
->bridge
&& !link
->network
->bond
&& !link
->network
->vrf
) {
1624 r
= sd_netlink_message_append_u32(req
, IFLA_MASTER
, 0);
1626 return log_link_error_errno(link
, r
, "Could not append IFLA_MASTER attribute: %m");
1629 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1631 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1633 if (link
->network
->mac
) {
1634 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
1636 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
1639 /* If IPv6 not configured (no static IPv6 address and IPv6LL autoconfiguration is disabled)
1640 for this interface, or if it is a bridge slave, then disable IPv6 else enable it. */
1641 (void) link_enable_ipv6(link
);
1643 if (link
->network
->mtu
) {
1644 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
1645 on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */
1646 if (link_ipv6_enabled(link
) && link
->network
->mtu
< IPV6_MIN_MTU
) {
1648 log_link_warning(link
, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU
) ", as "
1649 "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU
) " bytes: %m");
1651 link
->network
->mtu
= IPV6_MIN_MTU
;
1654 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, link
->network
->mtu
);
1656 return log_link_error_errno(link
, r
, "Could not set MTU: %m");
1659 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1661 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1663 if (link_ipv6_enabled(link
)) {
1664 /* if the kernel lacks ipv6 support setting IFF_UP fails if any ipv6 options are passed */
1665 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1667 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1669 if (!link_ipv6ll_enabled(link
))
1670 ipv6ll_mode
= IN6_ADDR_GEN_MODE_NONE
;
1672 const char *p
= NULL
;
1673 _cleanup_free_
char *stable_secret
= NULL
;
1675 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/stable_secret");
1676 r
= read_one_line_file(p
, &stable_secret
);
1679 ipv6ll_mode
= IN6_ADDR_GEN_MODE_EUI64
;
1681 ipv6ll_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
1683 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1685 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1687 if (!in_addr_is_null(AF_INET6
, &link
->network
->ipv6_token
)) {
1688 r
= sd_netlink_message_append_in6_addr(req
, IFLA_INET6_TOKEN
, &link
->network
->ipv6_token
.in6
);
1690 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_TOKEN: %m");
1693 r
= sd_netlink_message_close_container(req
);
1695 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1698 r
= sd_netlink_message_close_container(req
);
1700 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1702 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_up_handler
, link
, 0, NULL
);
1704 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1711 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1712 _cleanup_link_unref_ Link
*link
= userdata
;
1717 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1720 r
= sd_netlink_message_get_errno(m
);
1722 log_link_warning_errno(link
, r
, "Could not bring down interface: %m");
1727 static int link_down(Link
*link
) {
1728 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1732 assert(link
->manager
);
1733 assert(link
->manager
->rtnl
);
1735 log_link_debug(link
, "Bringing link down");
1737 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
1738 RTM_SETLINK
, link
->ifindex
);
1740 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1742 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
1744 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1746 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_down_handler
, link
, 0, NULL
);
1748 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1755 static int link_up_can(Link
*link
) {
1756 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1761 log_link_debug(link
, "Bringing CAN link up");
1763 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1765 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1767 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1769 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1771 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_up_handler
, link
, 0, NULL
);
1773 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1780 static int link_handle_bound_to_list(Link
*link
) {
1784 bool required_up
= false;
1785 bool link_is_up
= false;
1789 if (hashmap_isempty(link
->bound_to_links
))
1792 if (link
->flags
& IFF_UP
)
1795 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
1796 if (link_has_carrier(l
)) {
1801 if (!required_up
&& link_is_up
) {
1802 r
= link_down(link
);
1805 } else if (required_up
&& !link_is_up
) {
1814 static int link_handle_bound_by_list(Link
*link
) {
1821 if (hashmap_isempty(link
->bound_by_links
))
1824 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
1825 r
= link_handle_bound_to_list(l
);
1833 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
1839 if (link
== carrier
)
1842 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
1845 r
= hashmap_ensure_allocated(h
, NULL
);
1849 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
1856 static int link_new_bound_by_list(Link
*link
) {
1861 bool list_updated
= false;
1864 assert(link
->manager
);
1868 HASHMAP_FOREACH(carrier
, m
->links
, i
) {
1869 if (!carrier
->network
)
1872 if (strv_isempty(carrier
->network
->bind_carrier
))
1875 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
, 0)) {
1876 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
1880 list_updated
= true;
1887 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
1888 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
1892 link_dirty(carrier
);
1898 static int link_new_bound_to_list(Link
*link
) {
1903 bool list_updated
= false;
1906 assert(link
->manager
);
1911 if (strv_isempty(link
->network
->bind_carrier
))
1916 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
1917 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
, 0)) {
1918 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
1922 list_updated
= true;
1929 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
1930 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
1934 link_dirty(carrier
);
1940 static int link_new_carrier_maps(Link
*link
) {
1943 r
= link_new_bound_by_list(link
);
1947 r
= link_handle_bound_by_list(link
);
1951 r
= link_new_bound_to_list(link
);
1955 r
= link_handle_bound_to_list(link
);
1962 static void link_free_bound_to_list(Link
*link
) {
1966 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
1967 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
1969 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
1970 link_dirty(bound_to
);
1976 static void link_free_bound_by_list(Link
*link
) {
1980 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
1981 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
1983 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
1984 link_dirty(bound_by
);
1985 link_handle_bound_to_list(bound_by
);
1992 static void link_free_carrier_maps(Link
*link
) {
1993 bool list_updated
= false;
1997 if (!hashmap_isempty(link
->bound_to_links
)) {
1998 link_free_bound_to_list(link
);
1999 list_updated
= true;
2002 if (!hashmap_isempty(link
->bound_by_links
)) {
2003 link_free_bound_by_list(link
);
2004 list_updated
= true;
2013 void link_drop(Link
*link
) {
2014 if (!link
|| link
->state
== LINK_STATE_LINGER
)
2017 link_set_state(link
, LINK_STATE_LINGER
);
2019 link_free_carrier_maps(link
);
2021 log_link_debug(link
, "Link removed");
2023 (void)unlink(link
->state_file
);
2029 static int link_joined(Link
*link
) {
2033 assert(link
->network
);
2035 if (!hashmap_isempty(link
->bound_to_links
)) {
2036 r
= link_handle_bound_to_list(link
);
2039 } else if (!(link
->flags
& IFF_UP
)) {
2042 link_enter_failed(link
);
2047 if (link
->network
->bridge
) {
2048 r
= link_set_bridge(link
);
2050 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
2053 if (link
->network
->bond
) {
2054 r
= link_bond_set(link
);
2056 log_link_error_errno(link
, r
, "Could not set bond message: %m");
2059 if (link
->network
->use_br_vlan
&&
2060 (link
->network
->bridge
|| streq_ptr("bridge", link
->kind
))) {
2061 r
= link_set_bridge_vlan(link
);
2063 log_link_error_errno(link
, r
, "Could not set bridge vlan: %m");
2066 return link_enter_set_addresses(link
);
2069 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
2070 _cleanup_link_unref_ Link
*link
= userdata
;
2074 assert(link
->network
);
2078 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2081 r
= sd_netlink_message_get_errno(m
);
2082 if (r
< 0 && r
!= -EEXIST
) {
2083 log_link_error_errno(link
, r
, "Could not join netdev: %m");
2084 link_enter_failed(link
);
2087 log_link_debug(link
, "Joined netdev");
2089 if (link
->enslaving
<= 0)
2095 static int link_enter_join_netdev(Link
*link
) {
2101 assert(link
->network
);
2102 assert(link
->state
== LINK_STATE_PENDING
);
2104 link_set_state(link
, LINK_STATE_ENSLAVING
);
2108 if (!link
->network
->bridge
&&
2109 !link
->network
->bond
&&
2110 !link
->network
->vrf
&&
2111 hashmap_isempty(link
->network
->stacked_netdevs
))
2112 return link_joined(link
);
2114 if (link
->network
->bond
) {
2115 log_struct(LOG_DEBUG
,
2116 LOG_LINK_INTERFACE(link
),
2117 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2118 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
),
2121 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
2123 log_struct_errno(LOG_WARNING
, r
,
2124 LOG_LINK_INTERFACE(link
),
2125 LOG_NETDEV_INTERFACE(link
->network
->bond
),
2126 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
),
2129 link_enter_failed(link
);
2136 if (link
->network
->bridge
) {
2137 log_struct(LOG_DEBUG
,
2138 LOG_LINK_INTERFACE(link
),
2139 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2140 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
),
2143 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
2145 log_struct_errno(LOG_WARNING
, r
,
2146 LOG_LINK_INTERFACE(link
),
2147 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2148 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
),
2150 link_enter_failed(link
);
2157 if (link
->network
->vrf
) {
2158 log_struct(LOG_DEBUG
,
2159 LOG_LINK_INTERFACE(link
),
2160 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2161 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->vrf
->ifname
),
2163 r
= netdev_join(link
->network
->vrf
, link
, netdev_join_handler
);
2165 log_struct_errno(LOG_WARNING
, r
,
2166 LOG_LINK_INTERFACE(link
),
2167 LOG_NETDEV_INTERFACE(link
->network
->vrf
),
2168 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->vrf
->ifname
),
2170 link_enter_failed(link
);
2177 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
2179 log_struct(LOG_DEBUG
,
2180 LOG_LINK_INTERFACE(link
),
2181 LOG_NETDEV_INTERFACE(netdev
),
2182 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
),
2185 r
= netdev_join(netdev
, link
, netdev_join_handler
);
2187 log_struct_errno(LOG_WARNING
, r
,
2188 LOG_LINK_INTERFACE(link
),
2189 LOG_NETDEV_INTERFACE(netdev
),
2190 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
),
2192 link_enter_failed(link
);
2202 static int link_set_ipv4_forward(Link
*link
) {
2205 if (!link_ipv4_forward_enabled(link
))
2208 /* We propagate the forwarding flag from one interface to the
2209 * global setting one way. This means: as long as at least one
2210 * interface was configured at any time that had IP forwarding
2211 * enabled the setting will stay on for good. We do this
2212 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
2213 * somewhat in sync (see below). */
2215 r
= write_string_file("/proc/sys/net/ipv4/ip_forward", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2217 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
2222 static int link_set_ipv6_forward(Link
*link
) {
2225 if (!link_ipv6_forward_enabled(link
))
2228 /* On Linux, the IPv6 stack does not know a per-interface
2229 * packet forwarding setting: either packet forwarding is on
2230 * for all, or off for all. We hence don't bother with a
2231 * per-interface setting, but simply propagate the interface
2232 * flag, if it is set, to the global flag, one-way. Note that
2233 * while IPv4 would allow a per-interface flag, we expose the
2234 * same behaviour there and also propagate the setting from
2235 * one to all, to keep things simple (see above). */
2237 r
= write_string_file("/proc/sys/net/ipv6/conf/all/forwarding", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2239 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
2244 static int link_set_ipv6_privacy_extensions(Link
*link
) {
2245 char buf
[DECIMAL_STR_MAX(unsigned) + 1];
2246 IPv6PrivacyExtensions s
;
2247 const char *p
= NULL
;
2250 s
= link_ipv6_privacy_extensions(link
);
2254 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/use_tempaddr");
2255 xsprintf(buf
, "%u", (unsigned) link
->network
->ipv6_privacy_extensions
);
2257 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2259 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
2264 static int link_set_ipv6_accept_ra(Link
*link
) {
2265 const char *p
= NULL
;
2268 /* Make this a NOP if IPv6 is not available */
2269 if (!socket_ipv6_is_supported())
2272 if (link
->flags
& IFF_LOOPBACK
)
2278 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/accept_ra");
2280 /* We handle router advertisements ourselves, tell the kernel to GTFO */
2281 r
= write_string_file(p
, "0", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2283 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
2288 static int link_set_ipv6_dad_transmits(Link
*link
) {
2289 char buf
[DECIMAL_STR_MAX(int) + 1];
2290 const char *p
= NULL
;
2293 /* Make this a NOP if IPv6 is not available */
2294 if (!socket_ipv6_is_supported())
2297 if (link
->flags
& IFF_LOOPBACK
)
2303 if (link
->network
->ipv6_dad_transmits
< 0)
2306 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/dad_transmits");
2307 xsprintf(buf
, "%i", link
->network
->ipv6_dad_transmits
);
2309 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2311 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
2316 static int link_set_ipv6_hop_limit(Link
*link
) {
2317 char buf
[DECIMAL_STR_MAX(int) + 1];
2318 const char *p
= NULL
;
2321 /* Make this a NOP if IPv6 is not available */
2322 if (!socket_ipv6_is_supported())
2325 if (link
->flags
& IFF_LOOPBACK
)
2331 if (link
->network
->ipv6_hop_limit
< 0)
2334 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/hop_limit");
2335 xsprintf(buf
, "%i", link
->network
->ipv6_hop_limit
);
2337 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2339 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2344 static int link_drop_foreign_config(Link
*link
) {
2350 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
2351 /* we consider IPv6LL addresses to be managed by the kernel */
2352 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2355 r
= address_remove(address
, link
, link_address_remove_handler
);
2360 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2361 /* do not touch routes managed by the kernel */
2362 if (route
->protocol
== RTPROT_KERNEL
)
2365 r
= route_remove(route
, link
, link_route_remove_handler
);
2373 static int link_drop_config(Link
*link
) {
2379 SET_FOREACH(address
, link
->addresses
, i
) {
2380 /* we consider IPv6LL addresses to be managed by the kernel */
2381 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2384 r
= address_remove(address
, link
, link_address_remove_handler
);
2389 SET_FOREACH(route
, link
->routes
, i
) {
2390 /* do not touch routes managed by the kernel */
2391 if (route
->protocol
== RTPROT_KERNEL
)
2394 r
= route_remove(route
, link
, link_route_remove_handler
);
2404 static int link_update_lldp(Link
*link
) {
2412 if (link
->flags
& IFF_UP
) {
2413 r
= sd_lldp_start(link
->lldp
);
2415 log_link_debug(link
, "Started LLDP.");
2417 r
= sd_lldp_stop(link
->lldp
);
2419 log_link_debug(link
, "Stopped LLDP.");
2425 static int link_configure(Link
*link
) {
2429 assert(link
->network
);
2430 assert(link
->state
== LINK_STATE_PENDING
);
2432 if (streq_ptr(link
->kind
, "vcan")) {
2434 if (!(link
->flags
& IFF_UP
)) {
2435 r
= link_up_can(link
);
2437 link_enter_failed(link
);
2445 /* Drop foreign config, but ignore loopback or critical devices.
2446 * We do not want to remove loopback address or addresses used for root NFS. */
2447 if (!(link
->flags
& IFF_LOOPBACK
) && !(link
->network
->dhcp_critical
)) {
2448 r
= link_drop_foreign_config(link
);
2453 r
= link_set_proxy_arp(link
);
2457 r
= ipv6_proxy_ndp_addresses_configure(link
);
2461 r
= link_set_ipv4_forward(link
);
2465 r
= link_set_ipv6_forward(link
);
2469 r
= link_set_ipv6_privacy_extensions(link
);
2473 r
= link_set_ipv6_accept_ra(link
);
2477 r
= link_set_ipv6_dad_transmits(link
);
2481 r
= link_set_ipv6_hop_limit(link
);
2485 r
= link_set_flags(link
);
2489 if (link_ipv4ll_enabled(link
)) {
2490 r
= ipv4ll_configure(link
);
2495 if (link_dhcp4_enabled(link
)) {
2496 r
= dhcp4_configure(link
);
2501 if (link_dhcp4_server_enabled(link
)) {
2502 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
2506 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
2511 if (link_dhcp6_enabled(link
) ||
2512 link_ipv6_accept_ra_enabled(link
)) {
2513 r
= dhcp6_configure(link
);
2518 if (link_ipv6_accept_ra_enabled(link
)) {
2519 r
= ndisc_configure(link
);
2524 if (link_lldp_rx_enabled(link
)) {
2525 r
= sd_lldp_new(&link
->lldp
);
2529 r
= sd_lldp_set_ifindex(link
->lldp
, link
->ifindex
);
2533 r
= sd_lldp_match_capabilities(link
->lldp
,
2534 link
->network
->lldp_mode
== LLDP_MODE_ROUTERS_ONLY
?
2535 SD_LLDP_SYSTEM_CAPABILITIES_ALL_ROUTERS
:
2536 SD_LLDP_SYSTEM_CAPABILITIES_ALL
);
2540 r
= sd_lldp_set_filter_address(link
->lldp
, &link
->mac
);
2544 r
= sd_lldp_attach_event(link
->lldp
, NULL
, 0);
2548 r
= sd_lldp_set_callback(link
->lldp
, lldp_handler
, link
);
2552 r
= link_update_lldp(link
);
2557 if (link_has_carrier(link
)) {
2558 r
= link_acquire_conf(link
);
2563 return link_enter_join_netdev(link
);
2566 static int link_initialized_and_synced(sd_netlink
*rtnl
, sd_netlink_message
*m
,
2568 _cleanup_link_unref_ Link
*link
= userdata
;
2573 assert(link
->ifname
);
2574 assert(link
->manager
);
2576 if (link
->state
!= LINK_STATE_PENDING
)
2579 log_link_debug(link
, "Link state is up-to-date");
2581 r
= link_new_bound_by_list(link
);
2585 r
= link_handle_bound_by_list(link
);
2589 if (!link
->network
) {
2590 r
= network_get(link
->manager
, link
->udev_device
, link
->ifname
,
2591 &link
->mac
, &network
);
2593 link_enter_unmanaged(link
);
2595 } else if (r
== 0 && network
->unmanaged
) {
2596 link_enter_unmanaged(link
);
2601 if (link
->flags
& IFF_LOOPBACK
) {
2602 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
2603 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
2605 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
2606 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
2608 if (network
->dhcp_server
)
2609 log_link_debug(link
, "Ignoring DHCP server for loopback link");
2612 r
= network_apply(network
, link
);
2617 r
= link_new_bound_to_list(link
);
2621 r
= link_configure(link
);
2628 int link_initialized(Link
*link
, struct udev_device
*device
) {
2629 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2633 assert(link
->manager
);
2634 assert(link
->manager
->rtnl
);
2637 if (link
->state
!= LINK_STATE_PENDING
)
2640 if (link
->udev_device
)
2643 log_link_debug(link
, "udev initialized link");
2645 link
->udev_device
= udev_device_ref(device
);
2647 /* udev has initialized the link, but we don't know if we have yet
2648 * processed the NEWLINK messages with the latest state. Do a GETLINK,
2649 * when it returns we know that the pending NEWLINKs have already been
2650 * processed and that we are up-to-date */
2652 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
2657 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
,
2658 link_initialized_and_synced
, link
, 0, NULL
);
2667 static int link_load(Link
*link
) {
2668 _cleanup_free_
char *network_file
= NULL
,
2671 *dhcp4_address
= NULL
,
2672 *ipv4ll_address
= NULL
;
2673 union in_addr_union address
;
2674 union in_addr_union route_dst
;
2680 r
= parse_env_file(link
->state_file
, NEWLINE
,
2681 "NETWORK_FILE", &network_file
,
2682 "ADDRESSES", &addresses
,
2684 "DHCP4_ADDRESS", &dhcp4_address
,
2685 "IPV4LL_ADDRESS", &ipv4ll_address
,
2687 if (r
< 0 && r
!= -ENOENT
)
2688 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
2695 suffix
= strrchr(network_file
, '.');
2697 log_link_debug(link
, "Failed to get network name from %s", network_file
);
2698 goto network_file_fail
;
2702 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
2704 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
2705 goto network_file_fail
;
2708 r
= network_apply(network
, link
);
2710 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
2719 _cleanup_free_
char *address_str
= NULL
;
2720 char *prefixlen_str
;
2722 unsigned char prefixlen
;
2724 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
2726 log_link_debug_errno(link
, r
, "Failed to extract next address string: %m");
2732 prefixlen_str
= strchr(address_str
, '/');
2733 if (!prefixlen_str
) {
2734 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
2738 *prefixlen_str
++ = '\0';
2740 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
2742 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
2746 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
2748 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
2752 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
2754 return log_link_error_errno(link
, r
, "Failed to add address: %m");
2763 _cleanup_free_
char *route_str
= NULL
;
2764 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
2766 char *prefixlen_str
;
2768 unsigned char prefixlen
, tos
, table
;
2771 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
2773 log_link_debug_errno(link
, r
, "Failed to extract next route string: %m");
2779 prefixlen_str
= strchr(route_str
, '/');
2780 if (!prefixlen_str
) {
2781 log_link_debug(link
, "Failed to parse route %s", route_str
);
2785 *prefixlen_str
++ = '\0';
2787 r
= sscanf(prefixlen_str
, "%hhu/%hhu/%"SCNu32
"/%hhu/"USEC_FMT
, &prefixlen
, &tos
, &priority
, &table
, &lifetime
);
2789 log_link_debug(link
,
2790 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
2795 r
= in_addr_from_string_auto(route_str
, &family
, &route_dst
);
2797 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
2801 r
= route_add(link
, family
, &route_dst
, prefixlen
, tos
, priority
, table
, &route
);
2803 return log_link_error_errno(link
, r
, "Failed to add route: %m");
2805 if (lifetime
!= USEC_INFINITY
) {
2806 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(), lifetime
,
2807 0, route_expire_handler
, route
);
2809 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
2812 route
->lifetime
= lifetime
;
2813 sd_event_source_unref(route
->expire
);
2814 route
->expire
= expire
;
2819 if (dhcp4_address
) {
2820 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
2822 log_link_debug_errno(link
, r
, "Failed to parse DHCPv4 address %s: %m", dhcp4_address
);
2823 goto dhcp4_address_fail
;
2826 r
= sd_dhcp_client_new(&link
->dhcp_client
);
2828 return log_link_error_errno(link
, r
, "Failed to create DHCPv4 client: %m");
2830 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
2832 return log_link_error_errno(link
, r
, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address
);
2837 if (ipv4ll_address
) {
2838 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
2840 log_link_debug_errno(link
, r
, "Failed to parse IPv4LL address %s: %m", ipv4ll_address
);
2841 goto ipv4ll_address_fail
;
2844 r
= sd_ipv4ll_new(&link
->ipv4ll
);
2846 return log_link_error_errno(link
, r
, "Failed to create IPv4LL client: %m");
2848 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
2850 return log_link_error_errno(link
, r
, "Failed to set initial IPv4LL address %s: %m", ipv4ll_address
);
2853 ipv4ll_address_fail
:
2858 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
2860 _cleanup_udev_device_unref_
struct udev_device
*device
= NULL
;
2861 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
2869 r
= link_new(m
, message
, ret
);
2875 log_link_debug(link
, "Link %d added", link
->ifindex
);
2877 r
= link_load(link
);
2881 if (detect_container() <= 0) {
2882 /* not in a container, udev will be around */
2883 sprintf(ifindex_str
, "n%d", link
->ifindex
);
2884 device
= udev_device_new_from_device_id(m
->udev
, ifindex_str
);
2886 r
= log_link_warning_errno(link
, errno
, "Could not find udev device: %m");
2890 if (udev_device_get_is_initialized(device
) <= 0) {
2892 log_link_debug(link
, "link pending udev initialization...");
2896 r
= link_initialized(link
, device
);
2900 /* we are calling a callback directly, so must take a ref */
2903 r
= link_initialized_and_synced(m
->rtnl
, NULL
, link
);
2910 link_enter_failed(link
);
2914 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
2919 log_link_info(link
, "Gained IPv6LL");
2921 link
->ipv6ll_address
= *address
;
2922 link_check_ready(link
);
2924 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
2925 r
= link_acquire_ipv6_conf(link
);
2927 link_enter_failed(link
);
2935 static int link_carrier_gained(Link
*link
) {
2940 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
2941 r
= link_acquire_conf(link
);
2943 link_enter_failed(link
);
2947 r
= link_enter_set_addresses(link
);
2952 r
= link_handle_bound_by_list(link
);
2959 static int link_carrier_lost(Link
*link
) {
2964 r
= link_stop_clients(link
);
2966 link_enter_failed(link
);
2970 r
= link_drop_config(link
);
2974 if (!IN_SET(link
->state
, LINK_STATE_UNMANAGED
, LINK_STATE_PENDING
)) {
2975 log_link_debug(link
, "State is %s, dropping config", link_state_to_string(link
->state
));
2976 r
= link_drop_foreign_config(link
);
2981 r
= link_handle_bound_by_list(link
);
2988 int link_carrier_reset(Link
*link
) {
2993 if (link_has_carrier(link
)) {
2994 r
= link_carrier_lost(link
);
2998 r
= link_carrier_gained(link
);
3002 log_link_info(link
, "Reset carrier");
3008 int link_update(Link
*link
, sd_netlink_message
*m
) {
3009 struct ether_addr mac
;
3012 bool had_carrier
, carrier_gained
, carrier_lost
;
3016 assert(link
->ifname
);
3019 if (link
->state
== LINK_STATE_LINGER
) {
3021 log_link_info(link
, "Link readded");
3022 link_set_state(link
, LINK_STATE_ENSLAVING
);
3024 r
= link_new_carrier_maps(link
);
3029 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
3030 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
3031 log_link_info(link
, "Renamed to %s", ifname
);
3033 link_free_carrier_maps(link
);
3035 r
= free_and_strdup(&link
->ifname
, ifname
);
3039 r
= link_new_carrier_maps(link
);
3044 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
3045 if (r
>= 0 && mtu
> 0) {
3047 if (!link
->original_mtu
) {
3048 link
->original_mtu
= mtu
;
3049 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
3052 if (link
->dhcp_client
) {
3053 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
3056 log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
3062 /* The kernel may broadcast NEWLINK messages without the MAC address
3063 set, simply ignore them. */
3064 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
3066 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3069 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
3072 log_link_debug(link
, "MAC address: "
3073 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
3074 mac
.ether_addr_octet
[0],
3075 mac
.ether_addr_octet
[1],
3076 mac
.ether_addr_octet
[2],
3077 mac
.ether_addr_octet
[3],
3078 mac
.ether_addr_octet
[4],
3079 mac
.ether_addr_octet
[5]);
3082 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
3084 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
3087 if (link
->dhcp_client
) {
3088 const DUID
*duid
= link_duid(link
);
3090 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
3091 (const uint8_t *) &link
->mac
,
3095 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
3097 r
= sd_dhcp_client_set_iaid_duid(link
->dhcp_client
,
3098 link
->network
->iaid
,
3100 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3101 duid
->raw_data_len
);
3103 return log_link_warning_errno(link
, r
, "Could not update DUID/IAID in DHCP client: %m");
3106 if (link
->dhcp6_client
) {
3107 const DUID
* duid
= link_duid(link
);
3109 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
3110 (const uint8_t *) &link
->mac
,
3114 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
3116 r
= sd_dhcp6_client_set_iaid(link
->dhcp6_client
,
3117 link
->network
->iaid
);
3119 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 IAID: %m");
3121 r
= sd_dhcp6_client_set_duid(link
->dhcp6_client
,
3123 duid
->raw_data_len
> 0 ? duid
->raw_data
: NULL
,
3124 duid
->raw_data_len
);
3126 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 DUID: %m");
3131 had_carrier
= link_has_carrier(link
);
3133 r
= link_update_flags(link
, m
);
3137 r
= link_update_lldp(link
);
3141 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
3142 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
3144 if (carrier_gained
) {
3145 log_link_info(link
, "Gained carrier");
3147 r
= link_carrier_gained(link
);
3150 } else if (carrier_lost
) {
3151 log_link_info(link
, "Lost carrier");
3153 r
= link_carrier_lost(link
);
3161 static void print_link_hashmap(FILE *f
, const char *prefix
, Hashmap
* h
) {
3169 if (hashmap_isempty(h
))
3173 HASHMAP_FOREACH(link
, h
, i
) {
3177 fprintf(f
, "%i", link
->ifindex
);
3184 int link_save(Link
*link
) {
3185 _cleanup_free_
char *temp_path
= NULL
;
3186 _cleanup_fclose_
FILE *f
= NULL
;
3187 const char *admin_state
, *oper_state
;
3194 assert(link
->state_file
);
3195 assert(link
->lease_file
);
3196 assert(link
->manager
);
3198 if (link
->state
== LINK_STATE_LINGER
) {
3199 unlink(link
->state_file
);
3203 link_lldp_save(link
);
3205 admin_state
= link_state_to_string(link
->state
);
3206 assert(admin_state
);
3208 oper_state
= link_operstate_to_string(link
->operstate
);
3211 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
3215 (void) fchmod(fileno(f
), 0644);
3218 "# This is private data. Do not parse.\n"
3221 admin_state
, oper_state
);
3223 if (link
->network
) {
3225 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
3226 const char *dhcp_domainname
= NULL
;
3227 char **dhcp6_domains
= NULL
;
3230 if (link
->dhcp6_client
) {
3231 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
, &dhcp6_lease
);
3232 if (r
< 0 && r
!= -ENOMSG
)
3233 log_link_debug(link
, "No DHCPv6 lease");
3236 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
3241 for (j
= 0; j
< link
->network
->n_dns
; j
++) {
3242 _cleanup_free_
char *b
= NULL
;
3244 r
= in_addr_to_string(link
->network
->dns
[j
].family
,
3245 &link
->network
->dns
[j
].address
, &b
);
3247 log_debug_errno(r
, "Failed to format address, ignoring: %m");
3257 if (link
->network
->dhcp_use_dns
&&
3259 const struct in_addr
*addresses
;
3261 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
3265 serialize_in_addrs(f
, addresses
, r
);
3270 if (link
->network
->dhcp_use_dns
&& dhcp6_lease
) {
3271 struct in6_addr
*in6_addrs
;
3273 r
= sd_dhcp6_lease_get_dns(dhcp6_lease
, &in6_addrs
);
3277 serialize_in6_addrs(f
, in6_addrs
, r
);
3282 /* Make sure to flush out old entries before we use the NDISC data */
3285 if (link
->network
->dhcp_use_dns
&& link
->ndisc_rdnss
) {
3288 SET_FOREACH(dd
, link
->ndisc_rdnss
, i
) {
3292 serialize_in6_addrs(f
, &dd
->address
, 1);
3301 fputstrv(f
, link
->network
->ntp
, NULL
, &space
);
3303 if (link
->network
->dhcp_use_ntp
&&
3305 const struct in_addr
*addresses
;
3307 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
3311 serialize_in_addrs(f
, addresses
, r
);
3316 if (link
->network
->dhcp_use_ntp
&& dhcp6_lease
) {
3317 struct in6_addr
*in6_addrs
;
3320 r
= sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease
,
3325 serialize_in6_addrs(f
, in6_addrs
, r
);
3329 r
= sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease
, &hosts
);
3331 fputstrv(f
, hosts
, NULL
, &space
);
3336 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
3337 if (link
->dhcp_lease
)
3338 (void) sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &dhcp_domainname
);
3340 (void) sd_dhcp6_lease_get_domains(dhcp6_lease
, &dhcp6_domains
);
3343 fputs("DOMAINS=", f
);
3344 fputstrv(f
, link
->network
->search_domains
, NULL
, &space
);
3346 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
) {
3349 if (dhcp_domainname
)
3350 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3352 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3354 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3355 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3360 fputs("ROUTE_DOMAINS=", f
);
3361 fputstrv(f
, link
->network
->route_domains
, NULL
, NULL
);
3363 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
) {
3366 if (dhcp_domainname
)
3367 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3369 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3371 SET_FOREACH(dd
, link
->ndisc_dnssl
, i
)
3372 fputs_with_space(f
, NDISC_DNSSL_DOMAIN(dd
), NULL
, &space
);
3377 fprintf(f
, "LLMNR=%s\n",
3378 resolve_support_to_string(link
->network
->llmnr
));
3379 fprintf(f
, "MDNS=%s\n",
3380 resolve_support_to_string(link
->network
->mdns
));
3382 if (link
->network
->dnssec_mode
!= _DNSSEC_MODE_INVALID
)
3383 fprintf(f
, "DNSSEC=%s\n",
3384 dnssec_mode_to_string(link
->network
->dnssec_mode
));
3386 if (!set_isempty(link
->network
->dnssec_negative_trust_anchors
)) {
3389 fputs("DNSSEC_NTA=", f
);
3391 SET_FOREACH(n
, link
->network
->dnssec_negative_trust_anchors
, i
)
3392 fputs_with_space(f
, n
, NULL
, &space
);
3396 fputs("ADDRESSES=", f
);
3398 SET_FOREACH(a
, link
->addresses
, i
) {
3399 _cleanup_free_
char *address_str
= NULL
;
3401 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
3405 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
3410 fputs("ROUTES=", f
);
3412 SET_FOREACH(route
, link
->routes
, i
) {
3413 _cleanup_free_
char *route_str
= NULL
;
3415 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
3419 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%hhu/"USEC_FMT
, space
? " " : "", route_str
,
3420 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
3427 print_link_hashmap(f
, "CARRIER_BOUND_TO=", link
->bound_to_links
);
3428 print_link_hashmap(f
, "CARRIER_BOUND_BY=", link
->bound_by_links
);
3430 if (link
->dhcp_lease
) {
3431 struct in_addr address
;
3432 const char *tz
= NULL
;
3434 assert(link
->network
);
3436 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
3438 fprintf(f
, "TIMEZONE=%s\n", tz
);
3440 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
3442 fputs("DHCP4_ADDRESS=", f
);
3443 serialize_in_addrs(f
, &address
, 1);
3447 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
3455 unlink(link
->lease_file
);
3458 struct in_addr address
;
3460 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
3462 fputs("IPV4LL_ADDRESS=", f
);
3463 serialize_in_addrs(f
, &address
, 1);
3468 r
= fflush_and_check(f
);
3472 if (rename(temp_path
, link
->state_file
) < 0) {
3480 (void) unlink(link
->state_file
);
3482 (void) unlink(temp_path
);
3484 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
3487 /* The serialized state in /run is no longer up-to-date. */
3488 void link_dirty(Link
*link
) {
3493 /* mark manager dirty as link is dirty */
3494 manager_dirty(link
->manager
);
3496 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
3498 /* allocation errors are ignored */
3501 r
= set_put(link
->manager
->dirty_links
, link
);
3503 /* don't take another ref if the link was already dirty */
3509 /* The serialized state in /run is up-to-date */
3510 void link_clean(Link
*link
) {
3512 assert(link
->manager
);
3514 set_remove(link
->manager
->dirty_links
, link
);
3518 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
3519 [LINK_STATE_PENDING
] = "pending",
3520 [LINK_STATE_ENSLAVING
] = "configuring",
3521 [LINK_STATE_SETTING_ADDRESSES
] = "configuring",
3522 [LINK_STATE_SETTING_ROUTES
] = "configuring",
3523 [LINK_STATE_CONFIGURED
] = "configured",
3524 [LINK_STATE_UNMANAGED
] = "unmanaged",
3525 [LINK_STATE_FAILED
] = "failed",
3526 [LINK_STATE_LINGER
] = "linger",
3529 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);
3531 static const char* const link_operstate_table
[_LINK_OPERSTATE_MAX
] = {
3532 [LINK_OPERSTATE_OFF
] = "off",
3533 [LINK_OPERSTATE_NO_CARRIER
] = "no-carrier",
3534 [LINK_OPERSTATE_DORMANT
] = "dormant",
3535 [LINK_OPERSTATE_CARRIER
] = "carrier",
3536 [LINK_OPERSTATE_DEGRADED
] = "degraded",
3537 [LINK_OPERSTATE_ROUTABLE
] = "routable",
3540 DEFINE_STRING_TABLE_LOOKUP(link_operstate
, LinkOperationalState
);