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-link.h"
32 #include "networkd-lldp-tx.h"
33 #include "networkd-netdev.h"
35 #include "socket-util.h"
36 #include "stdio-util.h"
37 #include "string-table.h"
38 #include "udev-util.h"
42 static bool link_dhcp6_enabled(Link
*link
) {
45 if (link
->flags
& IFF_LOOPBACK
)
51 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV6
;
54 static bool link_dhcp4_enabled(Link
*link
) {
57 if (link
->flags
& IFF_LOOPBACK
)
63 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV4
;
66 static bool link_dhcp4_server_enabled(Link
*link
) {
69 if (link
->flags
& IFF_LOOPBACK
)
75 return link
->network
->dhcp_server
;
78 static bool link_ipv4ll_enabled(Link
*link
) {
81 if (link
->flags
& IFF_LOOPBACK
)
87 return link
->network
->link_local
& ADDRESS_FAMILY_IPV4
;
90 static bool link_ipv6ll_enabled(Link
*link
) {
93 if (link
->flags
& IFF_LOOPBACK
)
99 return link
->network
->link_local
& ADDRESS_FAMILY_IPV6
;
102 static bool link_ipv6_enabled(Link
*link
) {
105 if (!socket_ipv6_is_supported())
108 return link_dhcp6_enabled(link
) || link_ipv6ll_enabled(link
) || network_has_static_ipv6_addresses(link
->network
);
111 static bool link_lldp_rx_enabled(Link
*link
) {
114 if (link
->flags
& IFF_LOOPBACK
)
117 if (link
->iftype
!= ARPHRD_ETHER
)
123 if (link
->network
->bridge
)
126 return link
->network
->lldp_mode
!= LLDP_MODE_NO
;
129 static bool link_lldp_tx_enabled(Link
*link
) {
132 if (link
->flags
& IFF_LOOPBACK
)
135 if (link
->iftype
!= ARPHRD_ETHER
)
141 return link
->network
->lldp_emit
;
144 static bool link_ipv4_forward_enabled(Link
*link
) {
147 if (link
->flags
& IFF_LOOPBACK
)
153 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
156 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV4
;
159 static bool link_ipv6_forward_enabled(Link
*link
) {
162 if (!socket_ipv6_is_supported())
165 if (link
->flags
& IFF_LOOPBACK
)
171 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
174 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV6
;
177 static bool link_proxy_arp_enabled(Link
*link
) {
180 if (link
->flags
& IFF_LOOPBACK
)
186 if (link
->network
->proxy_arp
< 0)
192 static bool link_ipv6_accept_ra_enabled(Link
*link
) {
195 if (link
->flags
& IFF_LOOPBACK
)
201 /* If unset use system default (enabled if local forwarding is disabled.
202 * disabled if local forwarding is enabled).
203 * If set, ignore or enforce RA independent of local forwarding state.
205 if (link
->network
->ipv6_accept_ra
< 0)
206 /* default to accept RA if ip_forward is disabled and ignore RA if ip_forward is enabled */
207 return !link_ipv6_forward_enabled(link
);
208 else if (link
->network
->ipv6_accept_ra
> 0)
209 /* accept RA even if ip_forward is enabled */
216 static IPv6PrivacyExtensions
link_ipv6_privacy_extensions(Link
*link
) {
218 if (!socket_ipv6_is_supported())
219 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
221 if (link
->flags
& IFF_LOOPBACK
)
222 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
225 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
227 return link
->network
->ipv6_privacy_extensions
;
230 static int link_enable_ipv6(Link
*link
) {
231 const char *p
= NULL
;
235 if (link
->flags
& IFF_LOOPBACK
)
238 disabled
= !link_ipv6_enabled(link
);
240 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/disable_ipv6");
242 r
= write_string_file(p
, one_zero(disabled
), WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
244 log_link_warning_errno(link
, r
, "Cannot %s IPv6 for interface %s: %m", disabled
? "disable" : "enable", link
->ifname
);
247 log_link_info(link
, "IPv6 disabled for interface: %m");
249 log_link_info(link
, "IPv6 enabled for interface: %m");
255 void link_update_operstate(Link
*link
) {
256 LinkOperationalState operstate
;
259 if (link
->kernel_operstate
== IF_OPER_DORMANT
)
260 operstate
= LINK_OPERSTATE_DORMANT
;
261 else if (link_has_carrier(link
)) {
263 uint8_t scope
= RT_SCOPE_NOWHERE
;
266 /* if we have carrier, check what addresses we have */
267 SET_FOREACH(address
, link
->addresses
, i
) {
268 if (!address_is_ready(address
))
271 if (address
->scope
< scope
)
272 scope
= address
->scope
;
275 /* for operstate we also take foreign addresses into account */
276 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
277 if (!address_is_ready(address
))
280 if (address
->scope
< scope
)
281 scope
= address
->scope
;
284 if (scope
< RT_SCOPE_SITE
)
285 /* universally accessible addresses found */
286 operstate
= LINK_OPERSTATE_ROUTABLE
;
287 else if (scope
< RT_SCOPE_HOST
)
288 /* only link or site local addresses found */
289 operstate
= LINK_OPERSTATE_DEGRADED
;
291 /* no useful addresses found */
292 operstate
= LINK_OPERSTATE_CARRIER
;
293 } else if (link
->flags
& IFF_UP
)
294 operstate
= LINK_OPERSTATE_NO_CARRIER
;
296 operstate
= LINK_OPERSTATE_OFF
;
298 if (link
->operstate
!= operstate
) {
299 link
->operstate
= operstate
;
300 link_send_changed(link
, "OperationalState", NULL
);
305 #define FLAG_STRING(string, flag, old, new) \
306 (((old ^ new) & flag) \
307 ? ((old & flag) ? (" -" string) : (" +" string)) \
310 static int link_update_flags(Link
*link
, sd_netlink_message
*m
) {
311 unsigned flags
, unknown_flags_added
, unknown_flags_removed
, unknown_flags
;
317 r
= sd_rtnl_message_link_get_flags(m
, &flags
);
319 return log_link_warning_errno(link
, r
, "Could not get link flags: %m");
321 r
= sd_netlink_message_read_u8(m
, IFLA_OPERSTATE
, &operstate
);
323 /* if we got a message without operstate, take it to mean
324 the state was unchanged */
325 operstate
= link
->kernel_operstate
;
327 if ((link
->flags
== flags
) && (link
->kernel_operstate
== operstate
))
330 if (link
->flags
!= flags
) {
331 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",
332 FLAG_STRING("LOOPBACK", IFF_LOOPBACK
, link
->flags
, flags
),
333 FLAG_STRING("MASTER", IFF_MASTER
, link
->flags
, flags
),
334 FLAG_STRING("SLAVE", IFF_SLAVE
, link
->flags
, flags
),
335 FLAG_STRING("UP", IFF_UP
, link
->flags
, flags
),
336 FLAG_STRING("DORMANT", IFF_DORMANT
, link
->flags
, flags
),
337 FLAG_STRING("LOWER_UP", IFF_LOWER_UP
, link
->flags
, flags
),
338 FLAG_STRING("RUNNING", IFF_RUNNING
, link
->flags
, flags
),
339 FLAG_STRING("MULTICAST", IFF_MULTICAST
, link
->flags
, flags
),
340 FLAG_STRING("BROADCAST", IFF_BROADCAST
, link
->flags
, flags
),
341 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT
, link
->flags
, flags
),
342 FLAG_STRING("PROMISC", IFF_PROMISC
, link
->flags
, flags
),
343 FLAG_STRING("ALLMULTI", IFF_ALLMULTI
, link
->flags
, flags
),
344 FLAG_STRING("PORTSEL", IFF_PORTSEL
, link
->flags
, flags
),
345 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA
, link
->flags
, flags
),
346 FLAG_STRING("DYNAMIC", IFF_DYNAMIC
, link
->flags
, flags
),
347 FLAG_STRING("NOARP", IFF_NOARP
, link
->flags
, flags
),
348 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS
, link
->flags
, flags
),
349 FLAG_STRING("DEBUG", IFF_DEBUG
, link
->flags
, flags
),
350 FLAG_STRING("ECHO", IFF_ECHO
, link
->flags
, flags
));
352 unknown_flags
= ~(IFF_LOOPBACK
| IFF_MASTER
| IFF_SLAVE
| IFF_UP
|
353 IFF_DORMANT
| IFF_LOWER_UP
| IFF_RUNNING
|
354 IFF_MULTICAST
| IFF_BROADCAST
| IFF_POINTOPOINT
|
355 IFF_PROMISC
| IFF_ALLMULTI
| IFF_PORTSEL
|
356 IFF_AUTOMEDIA
| IFF_DYNAMIC
| IFF_NOARP
|
357 IFF_NOTRAILERS
| IFF_DEBUG
| IFF_ECHO
);
358 unknown_flags_added
= ((link
->flags
^ flags
) & flags
& unknown_flags
);
359 unknown_flags_removed
= ((link
->flags
^ flags
) & link
->flags
& unknown_flags
);
361 /* link flags are currently at most 18 bits, let's align to
363 if (unknown_flags_added
)
365 "Unknown link flags gained: %#.5x (ignoring)",
366 unknown_flags_added
);
368 if (unknown_flags_removed
)
370 "Unknown link flags lost: %#.5x (ignoring)",
371 unknown_flags_removed
);
375 link
->kernel_operstate
= operstate
;
377 link_update_operstate(link
);
382 static int link_new(Manager
*manager
, sd_netlink_message
*message
, Link
**ret
) {
383 _cleanup_link_unref_ Link
*link
= NULL
;
387 unsigned short iftype
;
393 r
= sd_netlink_message_get_type(message
, &type
);
396 else if (type
!= RTM_NEWLINK
)
399 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
402 else if (ifindex
<= 0)
405 r
= sd_rtnl_message_link_get_type(message
, &iftype
);
409 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &ifname
);
413 link
= new0(Link
, 1);
418 link
->manager
= manager
;
419 link
->state
= LINK_STATE_PENDING
;
420 link
->rtnl_extended_attrs
= true;
421 link
->ifindex
= ifindex
;
422 link
->iftype
= iftype
;
423 link
->ifname
= strdup(ifname
);
427 r
= sd_netlink_message_read_ether_addr(message
, IFLA_ADDRESS
, &link
->mac
);
429 log_link_debug_errno(link
, r
, "MAC address not found for new device, continuing without");
431 if (asprintf(&link
->state_file
, "/run/systemd/netif/links/%d", link
->ifindex
) < 0)
434 if (asprintf(&link
->lease_file
, "/run/systemd/netif/leases/%d", link
->ifindex
) < 0)
437 if (asprintf(&link
->lldp_file
, "/run/systemd/netif/lldp/%d", link
->ifindex
) < 0)
440 r
= hashmap_ensure_allocated(&manager
->links
, NULL
);
444 r
= hashmap_put(manager
->links
, INT_TO_PTR(link
->ifindex
), link
);
448 r
= link_update_flags(link
, message
);
458 static void link_free(Link
*link
) {
466 while (!set_isempty(link
->addresses
))
467 address_free(set_first(link
->addresses
));
469 while (!set_isempty(link
->addresses_foreign
))
470 address_free(set_first(link
->addresses_foreign
));
472 link
->addresses
= set_free(link
->addresses
);
474 link
->addresses_foreign
= set_free(link
->addresses_foreign
);
476 while ((address
= link
->pool_addresses
)) {
477 LIST_REMOVE(addresses
, link
->pool_addresses
, address
);
478 address_free(address
);
481 sd_dhcp_server_unref(link
->dhcp_server
);
482 sd_dhcp_client_unref(link
->dhcp_client
);
483 sd_dhcp_lease_unref(link
->dhcp_lease
);
485 link_lldp_tx_stop(link
);
487 free(link
->lease_file
);
489 sd_lldp_unref(link
->lldp
);
490 free(link
->lldp_file
);
492 sd_ipv4ll_unref(link
->ipv4ll
);
493 sd_dhcp6_client_unref(link
->dhcp6_client
);
494 sd_ndisc_unref(link
->ndisc_router_discovery
);
497 hashmap_remove(link
->manager
->links
, INT_TO_PTR(link
->ifindex
));
501 (void)unlink(link
->state_file
);
502 free(link
->state_file
);
504 udev_device_unref(link
->udev_device
);
506 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
)
507 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(carrier
->ifindex
));
508 hashmap_free(link
->bound_to_links
);
510 HASHMAP_FOREACH (carrier
, link
->bound_by_links
, i
)
511 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(carrier
->ifindex
));
512 hashmap_free(link
->bound_by_links
);
517 Link
*link_unref(Link
*link
) {
521 assert(link
->n_ref
> 0);
533 Link
*link_ref(Link
*link
) {
537 assert(link
->n_ref
> 0);
544 int link_get(Manager
*m
, int ifindex
, Link
**ret
) {
551 link
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
560 static void link_set_state(Link
*link
, LinkState state
) {
563 if (link
->state
== state
)
568 link_send_changed(link
, "AdministrativeState", NULL
);
573 static void link_enter_unmanaged(Link
*link
) {
576 log_link_debug(link
, "Unmanaged");
578 link_set_state(link
, LINK_STATE_UNMANAGED
);
583 static int link_stop_clients(Link
*link
) {
587 assert(link
->manager
);
588 assert(link
->manager
->event
);
590 if (link
->dhcp_client
) {
591 k
= sd_dhcp_client_stop(link
->dhcp_client
);
593 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv4 client: %m");
597 k
= sd_ipv4ll_stop(link
->ipv4ll
);
599 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 link-local: %m");
602 if (link
->dhcp6_client
) {
603 k
= sd_dhcp6_client_stop(link
->dhcp6_client
);
605 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv6 client: %m");
608 if (link
->ndisc_router_discovery
) {
609 k
= sd_ndisc_stop(link
->ndisc_router_discovery
);
611 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Discovery: %m");
614 link_lldp_tx_stop(link
);
618 void link_enter_failed(Link
*link
) {
621 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
624 log_link_warning(link
, "Failed");
626 link_set_state(link
, LINK_STATE_FAILED
);
628 link_stop_clients(link
);
633 static Address
* link_find_dhcp_server_address(Link
*link
) {
637 assert(link
->network
);
639 /* The first statically configured address if there is any */
640 LIST_FOREACH(addresses
, address
, link
->network
->static_addresses
) {
642 if (address
->family
!= AF_INET
)
645 if (in_addr_is_null(address
->family
, &address
->in_addr
))
651 /* If that didn't work, find a suitable address we got from the pool */
652 LIST_FOREACH(addresses
, address
, link
->pool_addresses
) {
653 if (address
->family
!= AF_INET
)
662 static int link_enter_configured(Link
*link
) {
664 assert(link
->network
);
665 assert(link
->state
== LINK_STATE_SETTING_ROUTES
);
667 log_link_info(link
, "Configured");
669 link_set_state(link
, LINK_STATE_CONFIGURED
);
676 void link_check_ready(Link
*link
) {
685 if (!link
->static_configured
)
688 if (link_ipv4ll_enabled(link
))
689 if (!link
->ipv4ll_address
||
693 if (link_ipv6ll_enabled(link
))
694 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) > 0)
697 if ((link_dhcp4_enabled(link
) && !link_dhcp6_enabled(link
) &&
698 !link
->dhcp4_configured
) ||
699 (link_dhcp6_enabled(link
) && !link_dhcp4_enabled(link
) &&
700 !link
->dhcp6_configured
) ||
701 (link_dhcp4_enabled(link
) && link_dhcp6_enabled(link
) &&
702 !link
->dhcp4_configured
&& !link
->dhcp6_configured
))
705 if (link_ipv6_accept_ra_enabled(link
) && !link
->ndisc_configured
)
708 SET_FOREACH(a
, link
->addresses
, i
)
709 if (!address_is_ready(a
))
712 if (link
->state
!= LINK_STATE_CONFIGURED
)
713 link_enter_configured(link
);
718 static int route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
719 _cleanup_link_unref_ Link
*link
= userdata
;
722 assert(link
->link_messages
> 0);
723 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
724 LINK_STATE_SETTING_ROUTES
, LINK_STATE_FAILED
,
727 link
->link_messages
--;
729 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
732 r
= sd_netlink_message_get_errno(m
);
733 if (r
< 0 && r
!= -EEXIST
)
734 log_link_warning_errno(link
, r
, "Could not set route: %m");
736 if (link
->link_messages
== 0) {
737 log_link_debug(link
, "Routes set");
738 link
->static_configured
= true;
739 link_check_ready(link
);
745 static int link_enter_set_routes(Link
*link
) {
750 assert(link
->network
);
751 assert(link
->state
== LINK_STATE_SETTING_ADDRESSES
);
753 link_set_state(link
, LINK_STATE_SETTING_ROUTES
);
755 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
756 r
= route_configure(rt
, link
, &route_handler
);
758 log_link_warning_errno(link
, r
, "Could not set routes: %m");
759 link_enter_failed(link
);
763 link
->link_messages
++;
766 if (link
->link_messages
== 0) {
767 link
->static_configured
= true;
768 link_check_ready(link
);
770 log_link_debug(link
, "Setting routes");
775 int link_route_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
776 _cleanup_link_unref_ Link
*link
= userdata
;
781 assert(link
->ifname
);
783 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
786 r
= sd_netlink_message_get_errno(m
);
787 if (r
< 0 && r
!= -ESRCH
)
788 log_link_warning_errno(link
, r
, "Could not drop route: %m");
793 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
794 _cleanup_link_unref_ Link
*link
= userdata
;
800 assert(link
->ifname
);
801 assert(link
->link_messages
> 0);
802 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
803 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
805 link
->link_messages
--;
807 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
810 r
= sd_netlink_message_get_errno(m
);
811 if (r
< 0 && r
!= -EEXIST
)
812 log_link_warning_errno(link
, r
, "could not set address: %m");
814 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
816 if (link
->link_messages
== 0) {
817 log_link_debug(link
, "Addresses set");
818 link_enter_set_routes(link
);
824 static int link_push_dns_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
825 _cleanup_free_
struct in_addr
*addresses
= NULL
;
826 size_t n_addresses
= 0, n_allocated
= 0;
829 log_debug("Copying DNS server information from %s", link
->ifname
);
834 STRV_FOREACH(a
, link
->network
->dns
) {
837 /* Only look for IPv4 addresses */
838 if (inet_pton(AF_INET
, *a
, &ia
) <= 0)
841 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
844 addresses
[n_addresses
++] = ia
;
847 if (link
->network
->dhcp_use_dns
&&
849 const struct in_addr
*da
= NULL
;
852 n
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &da
);
855 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
858 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
863 if (n_addresses
<= 0)
866 return sd_dhcp_server_set_dns(s
, addresses
, n_addresses
);
869 static int link_push_ntp_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
870 _cleanup_free_
struct in_addr
*addresses
= NULL
;
871 size_t n_addresses
= 0, n_allocated
= 0;
877 log_debug("Copying NTP server information from %s", link
->ifname
);
879 STRV_FOREACH(a
, link
->network
->ntp
) {
882 /* Only look for IPv4 addresses */
883 if (inet_pton(AF_INET
, *a
, &ia
) <= 0)
886 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
889 addresses
[n_addresses
++] = ia
;
892 if (link
->network
->dhcp_use_ntp
&&
894 const struct in_addr
*da
= NULL
;
897 n
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &da
);
900 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
903 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
908 if (n_addresses
<= 0)
911 return sd_dhcp_server_set_ntp(s
, addresses
, n_addresses
);
914 static int link_enter_set_addresses(Link
*link
) {
919 assert(link
->network
);
920 assert(link
->state
!= _LINK_STATE_INVALID
);
922 link_set_state(link
, LINK_STATE_SETTING_ADDRESSES
);
924 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
925 r
= address_configure(ad
, link
, &address_handler
, false);
927 log_link_warning_errno(link
, r
, "Could not set addresses: %m");
928 link_enter_failed(link
);
932 link
->link_messages
++;
935 /* now that we can figure out a default address for the dhcp server,
937 if (link_dhcp4_server_enabled(link
)) {
940 bool acquired_uplink
= false;
942 address
= link_find_dhcp_server_address(link
);
944 log_link_warning(link
, "Failed to find suitable address for DHCPv4 server instance.");
945 link_enter_failed(link
);
949 /* use the server address' subnet as the pool */
950 r
= sd_dhcp_server_configure_pool(link
->dhcp_server
, &address
->in_addr
.in
, address
->prefixlen
,
951 link
->network
->dhcp_server_pool_offset
, link
->network
->dhcp_server_pool_size
);
956 r = sd_dhcp_server_set_router(link->dhcp_server,
957 &main_address->in_addr.in);
962 if (link
->network
->dhcp_server_max_lease_time_usec
> 0) {
963 r
= sd_dhcp_server_set_max_lease_time(
965 DIV_ROUND_UP(link
->network
->dhcp_server_max_lease_time_usec
, USEC_PER_SEC
));
970 if (link
->network
->dhcp_server_default_lease_time_usec
> 0) {
971 r
= sd_dhcp_server_set_default_lease_time(
973 DIV_ROUND_UP(link
->network
->dhcp_server_default_lease_time_usec
, USEC_PER_SEC
));
978 if (link
->network
->dhcp_server_emit_dns
) {
980 if (link
->network
->n_dhcp_server_dns
> 0)
981 r
= sd_dhcp_server_set_dns(link
->dhcp_server
, link
->network
->dhcp_server_dns
, link
->network
->n_dhcp_server_dns
);
983 uplink
= manager_find_uplink(link
->manager
, link
);
984 acquired_uplink
= true;
987 log_link_debug(link
, "Not emitting DNS server information on link, couldn't find suitable uplink.");
990 r
= link_push_dns_to_dhcp_server(uplink
, link
->dhcp_server
);
993 log_link_warning_errno(link
, r
, "Failed to set DNS server for DHCP server, ignoring: %m");
997 if (link
->network
->dhcp_server_emit_ntp
) {
999 if (link
->network
->n_dhcp_server_ntp
> 0)
1000 r
= sd_dhcp_server_set_ntp(link
->dhcp_server
, link
->network
->dhcp_server_ntp
, link
->network
->n_dhcp_server_ntp
);
1002 if (!acquired_uplink
)
1003 uplink
= manager_find_uplink(link
->manager
, link
);
1006 log_link_debug(link
, "Not emitting NTP server information on link, couldn't find suitable uplink.");
1009 r
= link_push_ntp_to_dhcp_server(uplink
, link
->dhcp_server
);
1013 log_link_warning_errno(link
, r
, "Failed to set NTP server for DHCP server, ignoring: %m");
1016 if (link
->network
->dhcp_server_emit_timezone
) {
1017 _cleanup_free_
char *buffer
= NULL
;
1018 const char *tz
= NULL
;
1020 if (link
->network
->dhcp_server_timezone
)
1021 tz
= link
->network
->dhcp_server_timezone
;
1023 r
= get_timezone(&buffer
);
1025 log_warning_errno(r
, "Failed to determine timezone: %m");
1031 r
= sd_dhcp_server_set_timezone(link
->dhcp_server
, tz
);
1037 r
= sd_dhcp_server_start(link
->dhcp_server
);
1039 log_link_warning_errno(link
, r
, "Could not start DHCPv4 server instance: %m");
1041 link_enter_failed(link
);
1046 log_link_debug(link
, "Offering DHCPv4 leases");
1049 if (link
->link_messages
== 0)
1050 link_enter_set_routes(link
);
1052 log_link_debug(link
, "Setting addresses");
1057 int link_address_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1058 _cleanup_link_unref_ Link
*link
= userdata
;
1063 assert(link
->ifname
);
1065 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1068 r
= sd_netlink_message_get_errno(m
);
1069 if (r
< 0 && r
!= -EADDRNOTAVAIL
)
1070 log_link_warning_errno(link
, r
, "Could not drop address: %m");
1075 static int link_set_bridge_fdb(Link
*const link
) {
1076 FdbEntry
*fdb_entry
;
1079 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
1080 r
= fdb_entry_configure(link
, fdb_entry
);
1082 log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
1090 static int link_set_proxy_arp(Link
*const link
) {
1091 const char *p
= NULL
;
1094 if (!link_proxy_arp_enabled(link
))
1097 p
= strjoina("/proc/sys/net/ipv4/conf/", link
->ifname
, "/proxy_arp");
1099 r
= write_string_file(p
, one_zero(link
->network
->proxy_arp
), WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
1101 log_link_warning_errno(link
, r
, "Cannot configure proxy ARP for interface: %m");
1106 static int link_set_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1107 _cleanup_link_unref_ Link
*link
= userdata
;
1110 log_link_debug(link
, "Set link");
1112 r
= sd_netlink_message_get_errno(m
);
1113 if (r
< 0 && r
!= -EEXIST
) {
1114 log_link_error_errno(link
, r
, "Could not join netdev: %m");
1115 link_enter_failed(link
);
1122 static int set_hostname_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
1123 _cleanup_link_unref_ Link
*link
= userdata
;
1124 const sd_bus_error
*e
;
1129 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1132 e
= sd_bus_message_get_error(m
);
1134 log_link_warning_errno(link
, sd_bus_error_get_errno(e
), "Could not set hostname: %s", e
->message
);
1139 int link_set_hostname(Link
*link
, const char *hostname
) {
1143 assert(link
->manager
);
1145 log_link_debug(link
, "Setting transient hostname: '%s'", strna(hostname
));
1147 if (!link
->manager
->bus
) {
1148 /* TODO: replace by assert when we can rely on kdbus */
1149 log_link_info(link
, "Not connected to system bus, ignoring transient hostname.");
1153 r
= sd_bus_call_method_async(
1156 "org.freedesktop.hostname1",
1157 "/org/freedesktop/hostname1",
1158 "org.freedesktop.hostname1",
1160 set_hostname_handler
,
1167 return log_link_error_errno(link
, r
, "Could not set transient hostname: %m");
1174 static int set_timezone_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
1175 _cleanup_link_unref_ Link
*link
= userdata
;
1176 const sd_bus_error
*e
;
1181 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1184 e
= sd_bus_message_get_error(m
);
1186 log_link_warning_errno(link
, sd_bus_error_get_errno(e
), "Could not set timezone: %s", e
->message
);
1191 int link_set_timezone(Link
*link
, const char *tz
) {
1195 assert(link
->manager
);
1198 log_link_debug(link
, "Setting system timezone: '%s'", tz
);
1200 if (!link
->manager
->bus
) {
1201 log_link_info(link
, "Not connected to system bus, ignoring timezone.");
1205 r
= sd_bus_call_method_async(
1208 "org.freedesktop.timedate1",
1209 "/org/freedesktop/timedate1",
1210 "org.freedesktop.timedate1",
1212 set_timezone_handler
,
1218 return log_link_error_errno(link
, r
, "Could not set timezone: %m");
1225 static int set_mtu_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 MTU: %m");
1243 int link_set_mtu(Link
*link
, uint32_t mtu
) {
1244 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1248 assert(link
->manager
);
1249 assert(link
->manager
->rtnl
);
1251 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1253 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1255 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1257 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1259 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1261 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, set_mtu_handler
, link
, 0, NULL
);
1263 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1270 static int link_set_bridge(Link
*link
) {
1271 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1275 assert(link
->network
);
1277 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1279 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1281 r
= sd_rtnl_message_link_set_family(req
, PF_BRIDGE
);
1283 return log_link_error_errno(link
, r
, "Could not set message family: %m");
1285 r
= sd_netlink_message_open_container(req
, IFLA_PROTINFO
);
1287 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1289 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_GUARD
, !link
->network
->use_bpdu
);
1291 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_GUARD attribute: %m");
1293 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MODE
, link
->network
->hairpin
);
1295 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MODE attribute: %m");
1297 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_FAST_LEAVE
, link
->network
->fast_leave
);
1299 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_FAST_LEAVE attribute: %m");
1301 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_PROTECT
, !link
->network
->allow_port_to_be_root
);
1303 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PROTECT attribute: %m");
1305 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_UNICAST_FLOOD
, link
->network
->unicast_flood
);
1307 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_UNICAST_FLOOD attribute: %m");
1309 if (link
->network
->cost
!= 0) {
1310 r
= sd_netlink_message_append_u32(req
, IFLA_BRPORT_COST
, link
->network
->cost
);
1312 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_COST attribute: %m");
1315 r
= sd_netlink_message_close_container(req
);
1317 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1319 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_set_handler
, link
, 0, NULL
);
1321 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1328 static int link_lldp_save(Link
*link
) {
1329 _cleanup_free_
char *temp_path
= NULL
;
1330 _cleanup_fclose_
FILE *f
= NULL
;
1331 sd_lldp_neighbor
**l
= NULL
;
1335 assert(link
->lldp_file
);
1338 (void) unlink(link
->lldp_file
);
1342 r
= sd_lldp_get_neighbors(link
->lldp
, &l
);
1346 (void) unlink(link
->lldp_file
);
1352 r
= fopen_temporary(link
->lldp_file
, &f
, &temp_path
);
1356 fchmod(fileno(f
), 0644);
1358 for (i
= 0; i
< n
; i
++) {
1363 r
= sd_lldp_neighbor_get_raw(l
[i
], &p
, &sz
);
1368 (void) fwrite(&u
, 1, sizeof(u
), f
);
1369 (void) fwrite(p
, 1, sz
, f
);
1372 r
= fflush_and_check(f
);
1376 if (rename(temp_path
, link
->lldp_file
) < 0) {
1383 (void) unlink(link
->lldp_file
);
1385 (void) unlink(temp_path
);
1387 log_link_error_errno(link
, r
, "Failed to save LLDP data to %s: %m", link
->lldp_file
);
1391 for (i
= 0; i
< n
; i
++)
1392 sd_lldp_neighbor_unref(l
[i
]);
1399 static void lldp_handler(sd_lldp
*lldp
, sd_lldp_event event
, sd_lldp_neighbor
*n
, void *userdata
) {
1400 Link
*link
= userdata
;
1405 (void) link_lldp_save(link
);
1407 if (link_lldp_tx_enabled(link
) && event
== SD_LLDP_EVENT_ADDED
) {
1408 /* If we received information about a new neighbor, restart the LLDP "fast" logic */
1410 log_link_debug(link
, "Received LLDP datagram from previously unknown neighbor, restarting 'fast' LLDP transmission.");
1412 r
= link_lldp_tx_start(link
);
1414 log_link_warning_errno(link
, r
, "Failed to restart LLDP transmission: %m");
1418 static int link_acquire_ipv6_conf(Link
*link
) {
1423 if (link_dhcp6_enabled(link
)) {
1424 assert(link
->dhcp6_client
);
1425 assert(in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*)&link
->ipv6ll_address
) > 0);
1427 log_link_debug(link
, "Acquiring DHCPv6 lease");
1429 r
= sd_dhcp6_client_set_local_address(link
->dhcp6_client
, &link
->ipv6ll_address
);
1430 if (r
< 0 && r
!= -EBUSY
)
1431 return log_link_warning_errno(link
, r
, "Could not set IPv6LL address in DHCP client: %m");
1433 r
= sd_dhcp6_client_start(link
->dhcp6_client
);
1434 if (r
< 0 && r
!= -EBUSY
)
1435 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv6 lease: %m");
1438 if (link_ipv6_accept_ra_enabled(link
)) {
1439 assert(link
->ndisc_router_discovery
);
1441 log_link_debug(link
, "Discovering IPv6 routers");
1443 r
= sd_ndisc_router_discovery_start(link
->ndisc_router_discovery
);
1444 if (r
< 0 && r
!= -EBUSY
)
1445 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1451 static int link_acquire_conf(Link
*link
) {
1455 assert(link
->network
);
1456 assert(link
->manager
);
1457 assert(link
->manager
->event
);
1459 if (link_ipv4ll_enabled(link
)) {
1460 assert(link
->ipv4ll
);
1462 log_link_debug(link
, "Acquiring IPv4 link-local address");
1464 r
= sd_ipv4ll_start(link
->ipv4ll
);
1466 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1469 if (link_dhcp4_enabled(link
)) {
1470 assert(link
->dhcp_client
);
1472 log_link_debug(link
, "Acquiring DHCPv4 lease");
1474 r
= sd_dhcp_client_start(link
->dhcp_client
);
1476 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1479 if (link_lldp_tx_enabled(link
)) {
1480 r
= link_lldp_tx_start(link
);
1482 return log_link_warning_errno(link
, r
, "Failed to start LLDP transmission: %m");
1488 bool link_has_carrier(Link
*link
) {
1489 /* see Documentation/networking/operstates.txt in the kernel sources */
1491 if (link
->kernel_operstate
== IF_OPER_UP
)
1494 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1495 /* operstate may not be implemented, so fall back to flags */
1496 if ((link
->flags
& IFF_LOWER_UP
) && !(link
->flags
& IFF_DORMANT
))
1502 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1503 _cleanup_link_unref_ Link
*link
= userdata
;
1508 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1511 r
= sd_netlink_message_get_errno(m
);
1513 /* we warn but don't fail the link, as it may be
1515 log_link_warning_errno(link
, r
, "Could not bring up interface: %m");
1520 static int link_up(Link
*link
) {
1521 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1522 uint8_t ipv6ll_mode
;
1526 assert(link
->network
);
1527 assert(link
->manager
);
1528 assert(link
->manager
->rtnl
);
1530 log_link_debug(link
, "Bringing link up");
1532 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1534 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1536 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1538 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1540 if (link
->network
->mac
) {
1541 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
1543 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
1546 /* If IPv6 not configured (no static IPv6 address and neither DHCPv6 nor IPv6LL is enabled)
1547 for this interface then disable IPv6 else enable it. */
1548 (void) link_enable_ipv6(link
);
1550 if (link
->network
->mtu
) {
1551 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes
1552 on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */
1553 if (link_ipv6_enabled(link
) && link
->network
->mtu
< IPV6_MIN_MTU
) {
1555 log_link_warning(link
, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU
) ", as "
1556 "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU
) " bytes: %m");
1558 link
->network
->mtu
= IPV6_MIN_MTU
;
1561 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, link
->network
->mtu
);
1563 return log_link_error_errno(link
, r
, "Could not set MTU: %m");
1566 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1568 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1570 if (link_ipv6_enabled(link
)) {
1571 /* if the kernel lacks ipv6 support setting IFF_UP fails if any ipv6 options are passed */
1572 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1574 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1576 ipv6ll_mode
= link_ipv6ll_enabled(link
) ? IN6_ADDR_GEN_MODE_EUI64
: IN6_ADDR_GEN_MODE_NONE
;
1577 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1579 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1581 if (!in_addr_is_null(AF_INET6
, &link
->network
->ipv6_token
)) {
1582 r
= sd_netlink_message_append_in6_addr(req
, IFLA_INET6_TOKEN
, &link
->network
->ipv6_token
.in6
);
1584 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_TOKEN: %m");
1587 r
= sd_netlink_message_close_container(req
);
1589 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1592 r
= sd_netlink_message_close_container(req
);
1594 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1596 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_up_handler
, link
, 0, NULL
);
1598 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1605 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1606 _cleanup_link_unref_ Link
*link
= userdata
;
1611 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1614 r
= sd_netlink_message_get_errno(m
);
1616 log_link_warning_errno(link
, r
, "Could not bring down interface: %m");
1621 static int link_down(Link
*link
) {
1622 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1626 assert(link
->manager
);
1627 assert(link
->manager
->rtnl
);
1629 log_link_debug(link
, "Bringing link down");
1631 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
1632 RTM_SETLINK
, link
->ifindex
);
1634 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1636 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
1638 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1640 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_down_handler
, link
, 0, NULL
);
1642 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1649 static int link_handle_bound_to_list(Link
*link
) {
1653 bool required_up
= false;
1654 bool link_is_up
= false;
1658 if (hashmap_isempty(link
->bound_to_links
))
1661 if (link
->flags
& IFF_UP
)
1664 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
1665 if (link_has_carrier(l
)) {
1670 if (!required_up
&& link_is_up
) {
1671 r
= link_down(link
);
1674 } else if (required_up
&& !link_is_up
) {
1683 static int link_handle_bound_by_list(Link
*link
) {
1690 if (hashmap_isempty(link
->bound_by_links
))
1693 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
1694 r
= link_handle_bound_to_list(l
);
1702 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
1708 if (link
== carrier
)
1711 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
1714 r
= hashmap_ensure_allocated(h
, NULL
);
1718 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
1725 static int link_new_bound_by_list(Link
*link
) {
1730 bool list_updated
= false;
1733 assert(link
->manager
);
1737 HASHMAP_FOREACH(carrier
, m
->links
, i
) {
1738 if (!carrier
->network
)
1741 if (strv_isempty(carrier
->network
->bind_carrier
))
1744 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
, 0)) {
1745 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
1749 list_updated
= true;
1756 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
1757 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
1761 link_dirty(carrier
);
1767 static int link_new_bound_to_list(Link
*link
) {
1772 bool list_updated
= false;
1775 assert(link
->manager
);
1780 if (strv_isempty(link
->network
->bind_carrier
))
1785 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
1786 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
, 0)) {
1787 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
1791 list_updated
= true;
1798 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
1799 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
1803 link_dirty(carrier
);
1809 static int link_new_carrier_maps(Link
*link
) {
1812 r
= link_new_bound_by_list(link
);
1816 r
= link_handle_bound_by_list(link
);
1820 r
= link_new_bound_to_list(link
);
1824 r
= link_handle_bound_to_list(link
);
1831 static void link_free_bound_to_list(Link
*link
) {
1835 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
1836 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
1838 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
1839 link_dirty(bound_to
);
1845 static void link_free_bound_by_list(Link
*link
) {
1849 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
1850 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
1852 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
1853 link_dirty(bound_by
);
1854 link_handle_bound_to_list(bound_by
);
1861 static void link_free_carrier_maps(Link
*link
) {
1862 bool list_updated
= false;
1866 if (!hashmap_isempty(link
->bound_to_links
)) {
1867 link_free_bound_to_list(link
);
1868 list_updated
= true;
1871 if (!hashmap_isempty(link
->bound_by_links
)) {
1872 link_free_bound_by_list(link
);
1873 list_updated
= true;
1882 void link_drop(Link
*link
) {
1883 if (!link
|| link
->state
== LINK_STATE_LINGER
)
1886 link_set_state(link
, LINK_STATE_LINGER
);
1888 link_free_carrier_maps(link
);
1890 log_link_debug(link
, "Link removed");
1892 (void)unlink(link
->state_file
);
1898 static int link_joined(Link
*link
) {
1902 assert(link
->network
);
1904 if (!hashmap_isempty(link
->bound_to_links
)) {
1905 r
= link_handle_bound_to_list(link
);
1908 } else if (!(link
->flags
& IFF_UP
)) {
1911 link_enter_failed(link
);
1916 if (link
->network
->bridge
) {
1917 r
= link_set_bridge(link
);
1919 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
1922 return link_enter_set_addresses(link
);
1925 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1926 _cleanup_link_unref_ Link
*link
= userdata
;
1930 assert(link
->network
);
1934 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1937 r
= sd_netlink_message_get_errno(m
);
1938 if (r
< 0 && r
!= -EEXIST
) {
1939 log_link_error_errno(link
, r
, "Could not join netdev: %m");
1940 link_enter_failed(link
);
1943 log_link_debug(link
, "Joined netdev");
1945 if (link
->enslaving
<= 0)
1951 static int link_enter_join_netdev(Link
*link
) {
1957 assert(link
->network
);
1958 assert(link
->state
== LINK_STATE_PENDING
);
1960 link_set_state(link
, LINK_STATE_ENSLAVING
);
1964 if (!link
->network
->bridge
&&
1965 !link
->network
->bond
&&
1966 hashmap_isempty(link
->network
->stacked_netdevs
))
1967 return link_joined(link
);
1969 if (link
->network
->bond
) {
1970 log_struct(LOG_DEBUG
,
1971 LOG_LINK_INTERFACE(link
),
1972 LOG_NETDEV_INTERFACE(link
->network
->bond
),
1973 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
),
1976 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
1978 log_struct_errno(LOG_WARNING
, r
,
1979 LOG_LINK_INTERFACE(link
),
1980 LOG_NETDEV_INTERFACE(link
->network
->bond
),
1981 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
),
1984 link_enter_failed(link
);
1991 if (link
->network
->bridge
) {
1992 log_struct(LOG_DEBUG
,
1993 LOG_LINK_INTERFACE(link
),
1994 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
1995 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
),
1998 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
2000 log_struct_errno(LOG_WARNING
, r
,
2001 LOG_LINK_INTERFACE(link
),
2002 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
2003 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
),
2005 link_enter_failed(link
);
2012 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
2014 log_struct(LOG_DEBUG
,
2015 LOG_LINK_INTERFACE(link
),
2016 LOG_NETDEV_INTERFACE(netdev
),
2017 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
),
2020 r
= netdev_join(netdev
, link
, netdev_join_handler
);
2022 log_struct_errno(LOG_WARNING
, r
,
2023 LOG_LINK_INTERFACE(link
),
2024 LOG_NETDEV_INTERFACE(netdev
),
2025 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
),
2027 link_enter_failed(link
);
2037 static int link_set_ipv4_forward(Link
*link
) {
2040 if (!link_ipv4_forward_enabled(link
))
2043 /* We propagate the forwarding flag from one interface to the
2044 * global setting one way. This means: as long as at least one
2045 * interface was configured at any time that had IP forwarding
2046 * enabled the setting will stay on for good. We do this
2047 * primarily to keep IPv4 and IPv6 packet forwarding behaviour
2048 * somewhat in sync (see below). */
2050 r
= write_string_file("/proc/sys/net/ipv4/ip_forward", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2052 log_link_warning_errno(link
, r
, "Cannot turn on IPv4 packet forwarding, ignoring: %m");
2057 static int link_set_ipv6_forward(Link
*link
) {
2060 if (!link_ipv6_forward_enabled(link
))
2063 /* On Linux, the IPv6 stack does not not know a per-interface
2064 * packet forwarding setting: either packet forwarding is on
2065 * for all, or off for all. We hence don't bother with a
2066 * per-interface setting, but simply propagate the interface
2067 * flag, if it is set, to the global flag, one-way. Note that
2068 * while IPv4 would allow a per-interface flag, we expose the
2069 * same behaviour there and also propagate the setting from
2070 * one to all, to keep things simple (see above). */
2072 r
= write_string_file("/proc/sys/net/ipv6/conf/all/forwarding", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2074 log_link_warning_errno(link
, r
, "Cannot configure IPv6 packet forwarding, ignoring: %m");
2079 static int link_set_ipv6_privacy_extensions(Link
*link
) {
2080 char buf
[DECIMAL_STR_MAX(unsigned) + 1];
2081 IPv6PrivacyExtensions s
;
2082 const char *p
= NULL
;
2085 s
= link_ipv6_privacy_extensions(link
);
2089 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/use_tempaddr");
2090 xsprintf(buf
, "%u", (unsigned) link
->network
->ipv6_privacy_extensions
);
2092 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2094 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
2099 static int link_set_ipv6_accept_ra(Link
*link
) {
2100 const char *p
= NULL
;
2103 /* Make this a NOP if IPv6 is not available */
2104 if (!socket_ipv6_is_supported())
2107 if (link
->flags
& IFF_LOOPBACK
)
2113 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/accept_ra");
2115 /* We handle router advertisments ourselves, tell the kernel to GTFO */
2116 r
= write_string_file(p
, "0", WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2118 log_link_warning_errno(link
, r
, "Cannot disable kernel IPv6 accept_ra for interface: %m");
2123 static int link_set_ipv6_dad_transmits(Link
*link
) {
2124 char buf
[DECIMAL_STR_MAX(int) + 1];
2125 const char *p
= NULL
;
2128 /* Make this a NOP if IPv6 is not available */
2129 if (!socket_ipv6_is_supported())
2132 if (link
->flags
& IFF_LOOPBACK
)
2138 if (link
->network
->ipv6_dad_transmits
< 0)
2141 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/dad_transmits");
2142 xsprintf(buf
, "%i", link
->network
->ipv6_dad_transmits
);
2144 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2146 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
2151 static int link_set_ipv6_hop_limit(Link
*link
) {
2152 char buf
[DECIMAL_STR_MAX(int) + 1];
2153 const char *p
= NULL
;
2156 /* Make this a NOP if IPv6 is not available */
2157 if (!socket_ipv6_is_supported())
2160 if (link
->flags
& IFF_LOOPBACK
)
2166 if (link
->network
->ipv6_hop_limit
< 0)
2169 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/hop_limit");
2170 xsprintf(buf
, "%i", link
->network
->ipv6_hop_limit
);
2172 r
= write_string_file(p
, buf
, WRITE_STRING_FILE_VERIFY_ON_FAILURE
);
2174 log_link_warning_errno(link
, r
, "Cannot set IPv6 hop limit for interface: %m");
2179 static int link_drop_foreign_config(Link
*link
) {
2185 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
2186 /* we consider IPv6LL addresses to be managed by the kernel */
2187 if (address
->family
== AF_INET6
&& in_addr_is_link_local(AF_INET6
, &address
->in_addr
) == 1)
2190 r
= address_remove(address
, link
, link_address_remove_handler
);
2195 SET_FOREACH(route
, link
->routes_foreign
, i
) {
2196 /* do not touch routes managed by the kernel */
2197 if (route
->protocol
== RTPROT_KERNEL
)
2200 r
= route_remove(route
, link
, link_address_remove_handler
);
2208 static int link_update_lldp(Link
*link
) {
2216 if (link
->flags
& IFF_UP
) {
2217 r
= sd_lldp_start(link
->lldp
);
2219 log_link_debug(link
, "Started LLDP.");
2221 r
= sd_lldp_stop(link
->lldp
);
2223 log_link_debug(link
, "Stopped LLDP.");
2229 static int link_configure(Link
*link
) {
2233 assert(link
->network
);
2234 assert(link
->state
== LINK_STATE_PENDING
);
2236 /* Drop foreign config, but ignore loopback or critical devices.
2237 * We do not want to remove loopback address or addresses used for root NFS. */
2238 if (!(link
->flags
& IFF_LOOPBACK
) && !(link
->network
->dhcp_critical
)) {
2239 r
= link_drop_foreign_config(link
);
2244 r
= link_set_bridge_fdb(link
);
2248 r
= link_set_proxy_arp(link
);
2252 r
= link_set_ipv4_forward(link
);
2256 r
= link_set_ipv6_forward(link
);
2260 r
= link_set_ipv6_privacy_extensions(link
);
2264 r
= link_set_ipv6_accept_ra(link
);
2268 r
= link_set_ipv6_dad_transmits(link
);
2272 r
= link_set_ipv6_hop_limit(link
);
2276 if (link_ipv4ll_enabled(link
)) {
2277 r
= ipv4ll_configure(link
);
2282 if (link_dhcp4_enabled(link
)) {
2283 r
= dhcp4_configure(link
);
2288 if (link_dhcp4_server_enabled(link
)) {
2289 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
2293 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
2298 if (link_dhcp6_enabled(link
) ||
2299 link_ipv6_accept_ra_enabled(link
)) {
2300 r
= dhcp6_configure(link
);
2305 if (link_ipv6_accept_ra_enabled(link
)) {
2306 r
= ndisc_configure(link
);
2311 if (link_lldp_rx_enabled(link
)) {
2312 r
= sd_lldp_new(&link
->lldp
, link
->ifindex
);
2316 r
= sd_lldp_match_capabilities(link
->lldp
,
2317 link
->network
->lldp_mode
== LLDP_MODE_ROUTERS_ONLY
?
2318 SD_LLDP_SYSTEM_CAPABILITIES_ALL_ROUTERS
:
2319 SD_LLDP_SYSTEM_CAPABILITIES_ALL
);
2323 r
= sd_lldp_set_filter_address(link
->lldp
, &link
->mac
);
2327 r
= sd_lldp_attach_event(link
->lldp
, NULL
, 0);
2331 r
= sd_lldp_set_callback(link
->lldp
, lldp_handler
, link
);
2335 r
= link_update_lldp(link
);
2340 if (link_has_carrier(link
)) {
2341 r
= link_acquire_conf(link
);
2345 if (in_addr_is_null(AF_INET6
, (const union in_addr_union
*) &link
->ipv6ll_address
) == 0) {
2346 r
= link_acquire_ipv6_conf(link
);
2352 return link_enter_join_netdev(link
);
2355 static int link_initialized_and_synced(sd_netlink
*rtnl
, sd_netlink_message
*m
,
2357 _cleanup_link_unref_ Link
*link
= userdata
;
2362 assert(link
->ifname
);
2363 assert(link
->manager
);
2365 if (link
->state
!= LINK_STATE_PENDING
)
2368 log_link_debug(link
, "Link state is up-to-date");
2370 r
= link_new_bound_by_list(link
);
2374 r
= link_handle_bound_by_list(link
);
2378 if (!link
->network
) {
2379 r
= network_get(link
->manager
, link
->udev_device
, link
->ifname
,
2380 &link
->mac
, &network
);
2382 link_enter_unmanaged(link
);
2387 if (link
->flags
& IFF_LOOPBACK
) {
2388 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
2389 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
2391 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
2392 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
2394 if (network
->dhcp_server
)
2395 log_link_debug(link
, "Ignoring DHCP server for loopback link");
2398 r
= network_apply(link
->manager
, network
, link
);
2403 r
= link_new_bound_to_list(link
);
2407 r
= link_configure(link
);
2414 int link_initialized(Link
*link
, struct udev_device
*device
) {
2415 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2419 assert(link
->manager
);
2420 assert(link
->manager
->rtnl
);
2423 if (link
->state
!= LINK_STATE_PENDING
)
2426 if (link
->udev_device
)
2429 log_link_debug(link
, "udev initialized link");
2431 link
->udev_device
= udev_device_ref(device
);
2433 /* udev has initialized the link, but we don't know if we have yet
2434 * processed the NEWLINK messages with the latest state. Do a GETLINK,
2435 * when it returns we know that the pending NEWLINKs have already been
2436 * processed and that we are up-to-date */
2438 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
2443 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
,
2444 link_initialized_and_synced
, link
, 0, NULL
);
2453 static int link_load(Link
*link
) {
2454 _cleanup_free_
char *network_file
= NULL
,
2457 *dhcp4_address
= NULL
,
2458 *ipv4ll_address
= NULL
;
2459 union in_addr_union address
;
2460 union in_addr_union route_dst
;
2466 r
= parse_env_file(link
->state_file
, NEWLINE
,
2467 "NETWORK_FILE", &network_file
,
2468 "ADDRESSES", &addresses
,
2470 "DHCP4_ADDRESS", &dhcp4_address
,
2471 "IPV4LL_ADDRESS", &ipv4ll_address
,
2473 if (r
< 0 && r
!= -ENOENT
)
2474 return log_link_error_errno(link
, r
, "Failed to read %s: %m", link
->state_file
);
2481 suffix
= strrchr(network_file
, '.');
2483 log_link_debug(link
, "Failed to get network name from %s", network_file
);
2484 goto network_file_fail
;
2488 r
= network_get_by_name(link
->manager
, basename(network_file
), &network
);
2490 log_link_debug_errno(link
, r
, "Failed to get network %s: %m", basename(network_file
));
2491 goto network_file_fail
;
2494 r
= network_apply(link
->manager
, network
, link
);
2496 return log_link_error_errno(link
, r
, "Failed to apply network %s: %m", basename(network_file
));
2505 _cleanup_free_
char *address_str
= NULL
;
2506 char *prefixlen_str
;
2508 unsigned char prefixlen
;
2510 r
= extract_first_word(&p
, &address_str
, NULL
, 0);
2512 log_link_debug_errno(link
, r
, "Failed to extract next address string: %m");
2518 prefixlen_str
= strchr(address_str
, '/');
2519 if (!prefixlen_str
) {
2520 log_link_debug(link
, "Failed to parse address and prefix length %s", address_str
);
2524 *prefixlen_str
++ = '\0';
2526 r
= sscanf(prefixlen_str
, "%hhu", &prefixlen
);
2528 log_link_error(link
, "Failed to parse prefixlen %s", prefixlen_str
);
2532 r
= in_addr_from_string_auto(address_str
, &family
, &address
);
2534 log_link_debug_errno(link
, r
, "Failed to parse address %s: %m", address_str
);
2538 r
= address_add(link
, family
, &address
, prefixlen
, NULL
);
2540 return log_link_error_errno(link
, r
, "Failed to add address: %m");
2549 _cleanup_free_
char *route_str
= NULL
;
2550 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
2552 char *prefixlen_str
;
2554 unsigned char prefixlen
, tos
, table
;
2557 r
= extract_first_word(&p
, &route_str
, NULL
, 0);
2559 log_link_debug_errno(link
, r
, "Failed to extract next route string: %m");
2565 prefixlen_str
= strchr(route_str
, '/');
2566 if (!prefixlen_str
) {
2567 log_link_debug(link
, "Failed to parse route %s", route_str
);
2571 *prefixlen_str
++ = '\0';
2573 r
= sscanf(prefixlen_str
, "%hhu/%hhu/%"SCNu32
"/%hhu/"USEC_FMT
, &prefixlen
, &tos
, &priority
, &table
, &lifetime
);
2575 log_link_debug(link
,
2576 "Failed to parse destination prefix length, tos, priority, table or expiration %s",
2581 r
= in_addr_from_string_auto(route_str
, &family
, &route_dst
);
2583 log_link_debug_errno(link
, r
, "Failed to parse route destination %s: %m", route_str
);
2587 r
= route_add(link
, family
, &route_dst
, prefixlen
, tos
, priority
, table
, &route
);
2589 return log_link_error_errno(link
, r
, "Failed to add route: %m");
2591 if (lifetime
!= USEC_INFINITY
) {
2592 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(), lifetime
,
2593 0, route_expire_handler
, route
);
2595 log_link_warning_errno(link
, r
, "Could not arm route expiration handler: %m");
2598 route
->lifetime
= lifetime
;
2599 sd_event_source_unref(route
->expire
);
2600 route
->expire
= expire
;
2605 if (dhcp4_address
) {
2606 r
= in_addr_from_string(AF_INET
, dhcp4_address
, &address
);
2608 log_link_debug_errno(link
, r
, "Falied to parse DHCPv4 address %s: %m", dhcp4_address
);
2609 goto dhcp4_address_fail
;
2612 r
= sd_dhcp_client_new(&link
->dhcp_client
);
2614 return log_link_error_errno(link
, r
, "Falied to create DHCPv4 client: %m");
2616 r
= sd_dhcp_client_set_request_address(link
->dhcp_client
, &address
.in
);
2618 return log_link_error_errno(link
, r
, "Falied to set inital DHCPv4 address %s: %m", dhcp4_address
);
2623 if (ipv4ll_address
) {
2624 r
= in_addr_from_string(AF_INET
, ipv4ll_address
, &address
);
2626 log_link_debug_errno(link
, r
, "Falied to parse IPv4LL address %s: %m", ipv4ll_address
);
2627 goto ipv4ll_address_fail
;
2630 r
= sd_ipv4ll_new(&link
->ipv4ll
);
2632 return log_link_error_errno(link
, r
, "Falied to create IPv4LL client: %m");
2634 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &address
.in
);
2636 return log_link_error_errno(link
, r
, "Falied to set inital IPv4LL address %s: %m", ipv4ll_address
);
2639 ipv4ll_address_fail
:
2644 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
2646 _cleanup_udev_device_unref_
struct udev_device
*device
= NULL
;
2647 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
2655 r
= link_new(m
, message
, ret
);
2661 log_link_debug(link
, "Link %d added", link
->ifindex
);
2663 r
= link_load(link
);
2667 if (detect_container() <= 0) {
2668 /* not in a container, udev will be around */
2669 sprintf(ifindex_str
, "n%d", link
->ifindex
);
2670 device
= udev_device_new_from_device_id(m
->udev
, ifindex_str
);
2672 r
= log_link_warning_errno(link
, errno
, "Could not find udev device: %m");
2676 if (udev_device_get_is_initialized(device
) <= 0) {
2678 log_link_debug(link
, "link pending udev initialization...");
2682 r
= link_initialized(link
, device
);
2686 /* we are calling a callback directly, so must take a ref */
2689 r
= link_initialized_and_synced(m
->rtnl
, NULL
, link
);
2696 link_enter_failed(link
);
2700 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
2705 log_link_info(link
, "Gained IPv6LL");
2707 link
->ipv6ll_address
= *address
;
2708 link_check_ready(link
);
2710 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
2711 r
= link_acquire_ipv6_conf(link
);
2713 link_enter_failed(link
);
2721 static int link_carrier_gained(Link
*link
) {
2726 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
, LINK_STATE_FAILED
)) {
2727 r
= link_acquire_conf(link
);
2729 link_enter_failed(link
);
2734 r
= link_handle_bound_by_list(link
);
2741 static int link_carrier_lost(Link
*link
) {
2746 r
= link_stop_clients(link
);
2748 link_enter_failed(link
);
2752 r
= link_handle_bound_by_list(link
);
2759 int link_carrier_reset(Link
*link
) {
2764 if (link_has_carrier(link
)) {
2765 r
= link_carrier_lost(link
);
2769 r
= link_carrier_gained(link
);
2773 log_link_info(link
, "Reset carrier");
2779 int link_update(Link
*link
, sd_netlink_message
*m
) {
2780 struct ether_addr mac
;
2783 bool had_carrier
, carrier_gained
, carrier_lost
;
2787 assert(link
->ifname
);
2790 if (link
->state
== LINK_STATE_LINGER
) {
2792 log_link_info(link
, "Link readded");
2793 link_set_state(link
, LINK_STATE_ENSLAVING
);
2795 r
= link_new_carrier_maps(link
);
2800 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
2801 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
2802 log_link_info(link
, "Renamed to %s", ifname
);
2804 link_free_carrier_maps(link
);
2806 r
= free_and_strdup(&link
->ifname
, ifname
);
2810 r
= link_new_carrier_maps(link
);
2815 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
2816 if (r
>= 0 && mtu
> 0) {
2818 if (!link
->original_mtu
) {
2819 link
->original_mtu
= mtu
;
2820 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
2823 if (link
->dhcp_client
) {
2824 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
2827 log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
2833 /* The kernel may broadcast NEWLINK messages without the MAC address
2834 set, simply ignore them. */
2835 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
2837 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
2840 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
2843 log_link_debug(link
, "MAC address: "
2844 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
2845 mac
.ether_addr_octet
[0],
2846 mac
.ether_addr_octet
[1],
2847 mac
.ether_addr_octet
[2],
2848 mac
.ether_addr_octet
[3],
2849 mac
.ether_addr_octet
[4],
2850 mac
.ether_addr_octet
[5]);
2853 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
2855 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
2858 if (link
->dhcp_client
) {
2859 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
2860 (const uint8_t *) &link
->mac
,
2864 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
2866 if (link
->network
->duid_type
!= _DUID_TYPE_INVALID
)
2867 r
= sd_dhcp_client_set_iaid_duid(link
->dhcp_client
,
2868 link
->network
->iaid
,
2869 link
->network
->dhcp_duid_type
,
2870 link
->network
->dhcp_duid
,
2871 link
->network
->dhcp_duid_len
);
2873 r
= sd_dhcp_client_set_iaid_duid(link
->dhcp_client
,
2874 link
->network
->iaid
,
2875 link
->manager
->dhcp_duid_type
,
2876 link
->manager
->dhcp_duid
,
2877 link
->manager
->dhcp_duid_len
);
2879 return log_link_warning_errno(link
, r
, "Could not update DUID/IAID in DHCP client: %m");
2882 if (link
->dhcp6_client
) {
2883 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
2884 (const uint8_t *) &link
->mac
,
2888 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
2890 r
= sd_dhcp6_client_set_iaid(link
->dhcp6_client
,
2891 link
->network
->iaid
);
2893 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 IAID: %m");
2895 if (link
->network
->duid_type
!= _DUID_TYPE_INVALID
)
2896 r
= sd_dhcp6_client_set_duid(link
->dhcp6_client
,
2897 link
->network
->dhcp_duid_type
,
2898 link
->network
->dhcp_duid
,
2899 link
->network
->dhcp_duid_len
);
2901 r
= sd_dhcp6_client_set_duid(link
->dhcp6_client
,
2902 link
->manager
->dhcp_duid_type
,
2903 link
->manager
->dhcp_duid
,
2904 link
->manager
->dhcp_duid_len
);
2906 return log_link_warning_errno(link
, r
, "Could not update DHCPv6 DUID: %m");
2911 had_carrier
= link_has_carrier(link
);
2913 r
= link_update_flags(link
, m
);
2917 r
= link_update_lldp(link
);
2921 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
2922 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
2924 if (carrier_gained
) {
2925 log_link_info(link
, "Gained carrier");
2927 r
= link_carrier_gained(link
);
2930 } else if (carrier_lost
) {
2931 log_link_info(link
, "Lost carrier");
2933 r
= link_carrier_lost(link
);
2941 static void print_link_hashmap(FILE *f
, const char *prefix
, Hashmap
* h
) {
2949 if (hashmap_isempty(h
))
2953 HASHMAP_FOREACH(link
, h
, i
) {
2957 fprintf(f
, "%i", link
->ifindex
);
2964 int link_save(Link
*link
) {
2965 _cleanup_free_
char *temp_path
= NULL
;
2966 _cleanup_fclose_
FILE *f
= NULL
;
2967 const char *admin_state
, *oper_state
;
2974 assert(link
->state_file
);
2975 assert(link
->lease_file
);
2976 assert(link
->manager
);
2978 if (link
->state
== LINK_STATE_LINGER
) {
2979 unlink(link
->state_file
);
2983 link_lldp_save(link
);
2985 admin_state
= link_state_to_string(link
->state
);
2986 assert(admin_state
);
2988 oper_state
= link_operstate_to_string(link
->operstate
);
2991 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
2995 fchmod(fileno(f
), 0644);
2998 "# This is private data. Do not parse.\n"
3001 admin_state
, oper_state
);
3003 if (link
->network
) {
3005 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
3006 const char *dhcp_domainname
= NULL
;
3007 char **dhcp6_domains
= NULL
;
3009 if (link
->dhcp6_client
) {
3010 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
, &dhcp6_lease
);
3011 if (r
< 0 && r
!= -ENOMSG
)
3012 log_link_debug(link
, "No DHCPv6 lease");
3015 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
3019 fputstrv(f
, link
->network
->dns
, NULL
, &space
);
3021 if (link
->network
->dhcp_use_dns
&&
3023 const struct in_addr
*addresses
;
3025 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
3029 serialize_in_addrs(f
, addresses
, r
);
3034 if (link
->network
->dhcp_use_dns
&& dhcp6_lease
) {
3035 struct in6_addr
*in6_addrs
;
3037 r
= sd_dhcp6_lease_get_dns(dhcp6_lease
, &in6_addrs
);
3041 serialize_in6_addrs(f
, in6_addrs
, r
);
3049 fputstrv(f
, link
->network
->ntp
, NULL
, &space
);
3051 if (link
->network
->dhcp_use_ntp
&&
3053 const struct in_addr
*addresses
;
3055 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
3059 serialize_in_addrs(f
, addresses
, r
);
3064 if (link
->network
->dhcp_use_ntp
&& dhcp6_lease
) {
3065 struct in6_addr
*in6_addrs
;
3068 r
= sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease
,
3073 serialize_in6_addrs(f
, in6_addrs
, r
);
3077 r
= sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease
, &hosts
);
3079 fputstrv(f
, hosts
, NULL
, &space
);
3084 if (link
->network
->dhcp_use_domains
!= DHCP_USE_DOMAINS_NO
) {
3085 if (link
->dhcp_lease
)
3086 (void) sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &dhcp_domainname
);
3089 (void) sd_dhcp6_lease_get_domains(dhcp6_lease
, &dhcp6_domains
);
3092 fputs("DOMAINS=", f
);
3093 fputstrv(f
, link
->network
->search_domains
, NULL
, &space
);
3095 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
&& dhcp_domainname
)
3096 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3098 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_YES
&& dhcp6_domains
)
3099 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3103 fputs("ROUTE_DOMAINS=", f
);
3104 fputstrv(f
, link
->network
->route_domains
, NULL
, NULL
);
3106 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
&& dhcp_domainname
)
3107 fputs_with_space(f
, dhcp_domainname
, NULL
, &space
);
3109 if (link
->network
->dhcp_use_domains
== DHCP_USE_DOMAINS_ROUTE
&& dhcp6_domains
)
3110 fputstrv(f
, dhcp6_domains
, NULL
, &space
);
3114 fprintf(f
, "LLMNR=%s\n",
3115 resolve_support_to_string(link
->network
->llmnr
));
3116 fprintf(f
, "MDNS=%s\n",
3117 resolve_support_to_string(link
->network
->mdns
));
3119 if (link
->network
->dnssec_mode
!= _DNSSEC_MODE_INVALID
)
3120 fprintf(f
, "DNSSEC=%s\n",
3121 dnssec_mode_to_string(link
->network
->dnssec_mode
));
3123 if (!set_isempty(link
->network
->dnssec_negative_trust_anchors
)) {
3126 fputs("DNSSEC_NTA=", f
);
3128 SET_FOREACH(n
, link
->network
->dnssec_negative_trust_anchors
, i
)
3129 fputs_with_space(f
, n
, NULL
, &space
);
3133 fputs("ADDRESSES=", f
);
3135 SET_FOREACH(a
, link
->addresses
, i
) {
3136 _cleanup_free_
char *address_str
= NULL
;
3138 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
3142 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
3147 fputs("ROUTES=", f
);
3149 SET_FOREACH(route
, link
->routes
, i
) {
3150 _cleanup_free_
char *route_str
= NULL
;
3152 r
= in_addr_to_string(route
->family
, &route
->dst
, &route_str
);
3156 fprintf(f
, "%s%s/%hhu/%hhu/%"PRIu32
"/%hhu/"USEC_FMT
, space
? " " : "", route_str
,
3157 route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, route
->lifetime
);
3164 print_link_hashmap(f
, "CARRIER_BOUND_TO=", link
->bound_to_links
);
3165 print_link_hashmap(f
, "CARRIER_BOUND_BY=", link
->bound_by_links
);
3167 if (link
->dhcp_lease
) {
3168 struct in_addr address
;
3169 const char *tz
= NULL
;
3171 assert(link
->network
);
3173 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
3175 fprintf(f
, "TIMEZONE=%s\n", tz
);
3177 r
= sd_dhcp_lease_get_address(link
->dhcp_lease
, &address
);
3179 fputs("DHCP4_ADDRESS=", f
);
3180 serialize_in_addrs(f
, &address
, 1);
3184 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
3192 unlink(link
->lease_file
);
3195 struct in_addr address
;
3197 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &address
);
3199 fputs("IPV4LL_ADDRESS=", f
);
3200 serialize_in_addrs(f
, &address
, 1);
3205 r
= fflush_and_check(f
);
3209 if (rename(temp_path
, link
->state_file
) < 0) {
3217 (void) unlink(link
->state_file
);
3219 (void) unlink(temp_path
);
3221 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
3224 /* The serialized state in /run is no longer up-to-date. */
3225 void link_dirty(Link
*link
) {
3230 /* mark manager dirty as link is dirty */
3231 manager_dirty(link
->manager
);
3233 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
3235 /* allocation errors are ignored */
3238 r
= set_put(link
->manager
->dirty_links
, link
);
3240 /* don't take another ref if the link was already dirty */
3246 /* The serialized state in /run is up-to-date */
3247 void link_clean(Link
*link
) {
3249 assert(link
->manager
);
3251 set_remove(link
->manager
->dirty_links
, link
);
3255 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
3256 [LINK_STATE_PENDING
] = "pending",
3257 [LINK_STATE_ENSLAVING
] = "configuring",
3258 [LINK_STATE_SETTING_ADDRESSES
] = "configuring",
3259 [LINK_STATE_SETTING_ROUTES
] = "configuring",
3260 [LINK_STATE_CONFIGURED
] = "configured",
3261 [LINK_STATE_UNMANAGED
] = "unmanaged",
3262 [LINK_STATE_FAILED
] = "failed",
3263 [LINK_STATE_LINGER
] = "linger",
3266 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);
3268 static const char* const link_operstate_table
[_LINK_OPERSTATE_MAX
] = {
3269 [LINK_OPERSTATE_OFF
] = "off",
3270 [LINK_OPERSTATE_NO_CARRIER
] = "no-carrier",
3271 [LINK_OPERSTATE_DORMANT
] = "dormant",
3272 [LINK_OPERSTATE_CARRIER
] = "carrier",
3273 [LINK_OPERSTATE_DEGRADED
] = "degraded",
3274 [LINK_OPERSTATE_ROUTABLE
] = "routable",
3277 DEFINE_STRING_TABLE_LOOKUP(link_operstate
, LinkOperationalState
);