1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2013 Tom Gundersen <teg@jklm.no>
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <netinet/ether.h>
26 #include "networkd-link.h"
27 #include "networkd-netdev.h"
28 #include "libudev-private.h"
29 #include "udev-util.h"
33 #include "network-internal.h"
34 #include "conf-parser.h"
36 #include "dhcp-lease-internal.h"
38 #define FLAG_STRING(string, flag, old, new) \
39 (((old ^ new) & flag) \
40 ? ((old & flag) ? (" -" string) : (" +" string)) \
43 static int link_update_flags(Link
*link
, sd_rtnl_message
*m
) {
44 unsigned flags
, unknown_flags_added
, unknown_flags_removed
, unknown_flags
;
50 r
= sd_rtnl_message_link_get_flags(m
, &flags
);
52 log_warning_link(link
, "Could not get link flags");
56 r
= sd_rtnl_message_read_u8(m
, IFLA_OPERSTATE
, &operstate
);
58 /* if we got a message without operstate, take it to mean
59 the state was unchanged */
60 operstate
= link
->kernel_operstate
;
62 if ((link
->flags
== flags
) && (link
->kernel_operstate
== operstate
))
65 if (link
->flags
!= flags
) {
66 log_debug_link(link
, "flags change:%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
67 FLAG_STRING("LOOPBACK", IFF_LOOPBACK
, link
->flags
, flags
),
68 FLAG_STRING("MASTER", IFF_MASTER
, link
->flags
, flags
),
69 FLAG_STRING("SLAVE", IFF_SLAVE
, link
->flags
, flags
),
70 FLAG_STRING("UP", IFF_UP
, link
->flags
, flags
),
71 FLAG_STRING("DORMANT", IFF_DORMANT
, link
->flags
, flags
),
72 FLAG_STRING("LOWER_UP", IFF_LOWER_UP
, link
->flags
, flags
),
73 FLAG_STRING("RUNNING", IFF_RUNNING
, link
->flags
, flags
),
74 FLAG_STRING("MULTICAST", IFF_MULTICAST
, link
->flags
, flags
),
75 FLAG_STRING("BROADCAST", IFF_BROADCAST
, link
->flags
, flags
),
76 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT
, link
->flags
, flags
),
77 FLAG_STRING("PROMISC", IFF_PROMISC
, link
->flags
, flags
),
78 FLAG_STRING("ALLMULTI", IFF_ALLMULTI
, link
->flags
, flags
),
79 FLAG_STRING("PORTSEL", IFF_PORTSEL
, link
->flags
, flags
),
80 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA
, link
->flags
, flags
),
81 FLAG_STRING("DYNAMIC", IFF_DYNAMIC
, link
->flags
, flags
),
82 FLAG_STRING("NOARP", IFF_NOARP
, link
->flags
, flags
),
83 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS
, link
->flags
, flags
),
84 FLAG_STRING("DEBUG", IFF_DEBUG
, link
->flags
, flags
),
85 FLAG_STRING("ECHO", IFF_ECHO
, link
->flags
, flags
));
87 unknown_flags
= ~(IFF_LOOPBACK
| IFF_MASTER
| IFF_SLAVE
| IFF_UP
|
88 IFF_DORMANT
| IFF_LOWER_UP
| IFF_RUNNING
|
89 IFF_MULTICAST
| IFF_BROADCAST
| IFF_POINTOPOINT
|
90 IFF_PROMISC
| IFF_ALLMULTI
| IFF_PORTSEL
|
91 IFF_AUTOMEDIA
| IFF_DYNAMIC
| IFF_NOARP
|
92 IFF_NOTRAILERS
| IFF_DEBUG
| IFF_ECHO
);
93 unknown_flags_added
= ((link
->flags
^ flags
) & flags
& unknown_flags
);
94 unknown_flags_removed
= ((link
->flags
^ flags
) & link
->flags
& unknown_flags
);
96 /* link flags are currently at most 18 bits, let's align to
98 if (unknown_flags_added
)
100 "unknown link flags gained: %#.5x (ignoring)",
101 unknown_flags_added
);
103 if (unknown_flags_removed
)
105 "unknown link flags lost: %#.5x (ignoring)",
106 unknown_flags_removed
);
110 link
->kernel_operstate
= operstate
;
117 static int link_new(Manager
*manager
, sd_rtnl_message
*message
, Link
**ret
) {
118 _cleanup_link_unref_ Link
*link
= NULL
;
127 r
= sd_rtnl_message_get_type(message
, &type
);
130 else if (type
!= RTM_NEWLINK
)
133 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
136 else if (ifindex
<= 0)
139 r
= sd_rtnl_message_read_string(message
, IFLA_IFNAME
, &ifname
);
143 link
= new0(Link
, 1);
148 link
->manager
= manager
;
149 link
->state
= LINK_STATE_PENDING
;
150 link
->ifindex
= ifindex
;
151 link
->ifname
= strdup(ifname
);
155 r
= sd_rtnl_message_read_ether_addr(message
, IFLA_ADDRESS
, &link
->mac
);
157 log_debug_link(link
, "MAC address not found for new device, continuing without");
159 r
= asprintf(&link
->state_file
, "/run/systemd/netif/links/%d",
164 r
= asprintf(&link
->lease_file
, "/run/systemd/netif/leases/%d",
169 r
= hashmap_ensure_allocated(&manager
->links
, NULL
, NULL
);
173 r
= hashmap_put(manager
->links
, INT_TO_PTR(link
->ifindex
), link
);
177 r
= link_update_flags(link
, message
);
187 static void link_free(Link
*link
) {
193 while ((address
= link
->addresses
)) {
194 LIST_REMOVE(addresses
, link
->addresses
, address
);
195 address_free(address
);
198 while ((address
= link
->pool_addresses
)) {
199 LIST_REMOVE(addresses
, link
->pool_addresses
, address
);
200 address_free(address
);
203 sd_dhcp_client_unref(link
->dhcp_client
);
204 sd_dhcp_lease_unref(link
->dhcp_lease
);
206 unlink(link
->lease_file
);
207 free(link
->lease_file
);
209 sd_ipv4ll_unref(link
->ipv4ll
);
210 sd_dhcp6_client_unref(link
->dhcp6_client
);
211 sd_icmp6_nd_unref(link
->icmp6_router_discovery
);
214 hashmap_remove(link
->manager
->links
, INT_TO_PTR(link
->ifindex
));
218 unlink(link
->state_file
);
219 free(link
->state_file
);
221 udev_device_unref(link
->udev_device
);
226 Link
*link_unref(Link
*link
) {
227 if (link
&& (-- link
->n_ref
<= 0))
233 Link
*link_ref(Link
*link
) {
235 assert_se(++ link
->n_ref
>= 2);
240 int link_get(Manager
*m
, int ifindex
, Link
**ret
) {
247 link
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
256 void link_drop(Link
*link
) {
257 if (!link
|| link
->state
== LINK_STATE_LINGER
)
260 link
->state
= LINK_STATE_LINGER
;
262 log_debug_link(link
, "link removed");
269 static void link_enter_unmanaged(Link
*link
) {
272 log_debug_link(link
, "unmanaged");
274 link
->state
= LINK_STATE_UNMANAGED
;
279 static int link_stop_clients(Link
*link
) {
283 assert(link
->manager
);
284 assert(link
->manager
->event
);
289 if (link
->dhcp_client
) {
290 k
= sd_dhcp_client_stop(link
->dhcp_client
);
292 log_warning_link(link
, "Could not stop DHCPv4 client: %s",
299 k
= sd_ipv4ll_stop(link
->ipv4ll
);
301 log_warning_link(link
, "Could not stop IPv4 link-local: %s",
307 if (link
->dhcp_server
) {
308 k
= sd_dhcp_server_stop(link
->dhcp_server
);
310 log_warning_link(link
, "Could not stop DHCPv4 server: %s",
316 if(link
->icmp6_router_discovery
) {
318 if (link
->dhcp6_client
) {
319 k
= sd_dhcp6_client_stop(link
->dhcp6_client
);
321 log_warning_link(link
, "Could not stop DHCPv6 client: %s",
327 k
= sd_icmp6_nd_stop(link
->icmp6_router_discovery
);
329 log_warning_link(link
,
330 "Could not stop ICMPv6 router discovery: %s",
339 void link_enter_failed(Link
*link
) {
342 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
345 log_warning_link(link
, "failed");
347 link
->state
= LINK_STATE_FAILED
;
349 link_stop_clients(link
);
354 static Address
* link_find_dhcp_server_address(Link
*link
) {
358 assert(link
->network
);
360 /* The the first statically configured address if there is any */
361 LIST_FOREACH(addresses
, address
, link
->network
->static_addresses
) {
363 if (address
->family
!= AF_INET
)
366 if (in_addr_is_null(address
->family
, &address
->in_addr
))
372 /* If that didn't work, find a suitable address we got from the pool */
373 LIST_FOREACH(addresses
, address
, link
->pool_addresses
) {
374 if (address
->family
!= AF_INET
)
383 static int link_enter_configured(Link
*link
) {
387 assert(link
->network
);
388 assert(link
->state
== LINK_STATE_SETTING_ROUTES
);
390 if (link
->network
->dhcp_server
&&
391 !sd_dhcp_server_is_running(link
->dhcp_server
)) {
392 struct in_addr pool_start
;
395 address
= link_find_dhcp_server_address(link
);
397 log_warning_link(link
,
398 "Failed to find suitable address for DHCPv4 server instance.");
399 link_enter_failed(link
);
403 log_debug_link(link
, "offering DHCPv4 leases");
405 r
= sd_dhcp_server_set_address(link
->dhcp_server
,
406 &address
->in_addr
.in
,
411 /* offer 32 addresses starting from the address following the server address */
412 pool_start
.s_addr
= htobe32(be32toh(address
->in_addr
.in
.s_addr
) + 1);
413 r
= sd_dhcp_server_set_lease_pool(link
->dhcp_server
,
419 r = sd_dhcp_server_set_router(link->dhcp_server,
420 &main_address->in_addr.in);
424 r = sd_dhcp_server_set_prefixlen(link->dhcp_server,
425 main_address->prefixlen);
430 r
= sd_dhcp_server_start(link
->dhcp_server
);
432 log_warning_link(link
, "could not start DHCPv4 server "
433 "instance: %s", strerror(-r
));
435 link_enter_failed(link
);
441 log_info_link(link
, "link configured");
443 link
->state
= LINK_STATE_CONFIGURED
;
450 void link_client_handler(Link
*link
) {
452 assert(link
->network
);
454 if (!link
->static_configured
)
457 if (link
->network
->ipv4ll
)
458 if (!link
->ipv4ll_address
||
462 if (IN_SET(link
->network
->dhcp
, DHCP_SUPPORT_BOTH
, DHCP_SUPPORT_V4
))
463 if (!link
->dhcp4_configured
)
466 if (link
->state
!= LINK_STATE_CONFIGURED
)
467 link_enter_configured(link
);
472 static int route_handler(sd_rtnl
*rtnl
, sd_rtnl_message
*m
, void *userdata
) {
473 _cleanup_link_unref_ Link
*link
= userdata
;
476 assert(link
->link_messages
> 0);
477 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
478 LINK_STATE_SETTING_ROUTES
, LINK_STATE_FAILED
,
481 link
->link_messages
--;
483 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
486 r
= sd_rtnl_message_get_errno(m
);
487 if (r
< 0 && r
!= -EEXIST
)
488 log_struct_link(LOG_WARNING
, link
,
489 "MESSAGE=%-*s: could not set route: %s",
491 link
->ifname
, strerror(-r
),
495 if (link
->link_messages
== 0) {
496 log_debug_link(link
, "routes set");
497 link
->static_configured
= true;
498 link_client_handler(link
);
504 static int link_enter_set_routes(Link
*link
) {
509 assert(link
->network
);
510 assert(link
->state
== LINK_STATE_SETTING_ADDRESSES
);
512 link
->state
= LINK_STATE_SETTING_ROUTES
;
514 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
515 r
= route_configure(rt
, link
, &route_handler
);
517 log_warning_link(link
,
518 "could not set routes: %s",
520 link_enter_failed(link
);
524 link
->link_messages
++;
527 if (link
->link_messages
== 0) {
528 link
->static_configured
= true;
529 link_client_handler(link
);
531 log_debug_link(link
, "setting routes");
536 int link_route_drop_handler(sd_rtnl
*rtnl
, sd_rtnl_message
*m
, void *userdata
) {
537 _cleanup_link_unref_ Link
*link
= userdata
;
542 assert(link
->ifname
);
544 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
547 r
= sd_rtnl_message_get_errno(m
);
548 if (r
< 0 && r
!= -ESRCH
)
549 log_struct_link(LOG_WARNING
, link
,
550 "MESSAGE=%-*s: could not drop route: %s",
552 link
->ifname
, strerror(-r
),
559 int link_get_address_handler(sd_rtnl
*rtnl
, sd_rtnl_message
*m
, void *userdata
) {
560 _cleanup_link_unref_ Link
*link
= userdata
;
566 assert(link
->manager
);
568 for (; m
; m
= sd_rtnl_message_next(m
)) {
569 r
= sd_rtnl_message_get_errno(m
);
571 log_debug_link(link
, "getting address failed: %s",
576 r
= link_rtnl_process_address(rtnl
, m
, link
->manager
);
578 log_warning_link(link
, "could not process address: %s",
585 static int address_handler(sd_rtnl
*rtnl
, sd_rtnl_message
*m
, void *userdata
) {
586 _cleanup_link_unref_ Link
*link
= userdata
;
592 assert(link
->ifname
);
593 assert(link
->link_messages
> 0);
594 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
595 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
597 link
->link_messages
--;
599 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
602 r
= sd_rtnl_message_get_errno(m
);
603 if (r
< 0 && r
!= -EEXIST
)
604 log_struct_link(LOG_WARNING
, link
,
605 "MESSAGE=%-*s: could not set address: %s",
607 link
->ifname
, strerror(-r
),
611 /* calling handler directly so take a ref */
613 link_get_address_handler(rtnl
, m
, link
);
616 if (link
->link_messages
== 0) {
617 log_debug_link(link
, "addresses set");
618 link_enter_set_routes(link
);
624 static int link_enter_set_addresses(Link
*link
) {
629 assert(link
->network
);
630 assert(link
->state
!= _LINK_STATE_INVALID
);
632 link
->state
= LINK_STATE_SETTING_ADDRESSES
;
634 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
635 r
= address_configure(ad
, link
, &address_handler
);
637 log_warning_link(link
,
638 "could not set addresses: %s",
640 link_enter_failed(link
);
644 link
->link_messages
++;
647 if (link
->link_messages
== 0) {
648 link_enter_set_routes(link
);
650 log_debug_link(link
, "setting addresses");
655 int link_address_drop_handler(sd_rtnl
*rtnl
, sd_rtnl_message
*m
, void *userdata
) {
656 _cleanup_link_unref_ Link
*link
= userdata
;
661 assert(link
->ifname
);
663 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
666 r
= sd_rtnl_message_get_errno(m
);
667 if (r
< 0 && r
!= -EADDRNOTAVAIL
)
668 log_struct_link(LOG_WARNING
, link
,
669 "MESSAGE=%-*s: could not drop address: %s",
671 link
->ifname
, strerror(-r
),
678 static int set_hostname_handler(sd_bus
*bus
, sd_bus_message
*m
, void *userdata
,
679 sd_bus_error
*ret_error
) {
680 _cleanup_link_unref_ Link
*link
= userdata
;
685 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
688 r
= sd_bus_message_get_errno(m
);
692 log_warning_link(link
, "Could not set hostname: %s",
698 int link_set_hostname(Link
*link
, const char *hostname
) {
699 _cleanup_bus_message_unref_ sd_bus_message
*m
= NULL
;
703 assert(link
->manager
);
706 log_debug_link(link
, "Setting transient hostname: '%s'", hostname
);
708 if (!link
->manager
->bus
) {
709 /* TODO: replace by assert when we can rely on kdbus */
711 "Not connected to system bus, ignoring transient hostname.");
715 r
= sd_bus_message_new_method_call(
718 "org.freedesktop.hostname1",
719 "/org/freedesktop/hostname1",
720 "org.freedesktop.hostname1",
725 r
= sd_bus_message_append(m
, "sb", hostname
, false);
729 r
= sd_bus_call_async(link
->manager
->bus
, NULL
, m
, set_hostname_handler
,
732 log_error_link(link
, "Could not set transient hostname: %s",
742 static int set_mtu_handler(sd_rtnl
*rtnl
, sd_rtnl_message
*m
, void *userdata
) {
743 _cleanup_link_unref_ Link
*link
= userdata
;
748 assert(link
->ifname
);
750 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
753 r
= sd_rtnl_message_get_errno(m
);
755 log_struct_link(LOG_WARNING
, link
,
756 "MESSAGE=%-*s: could not set MTU: %s",
757 IFNAMSIZ
, link
->ifname
, strerror(-r
),
764 int link_set_mtu(Link
*link
, uint32_t mtu
) {
765 _cleanup_rtnl_message_unref_ sd_rtnl_message
*req
= NULL
;
769 assert(link
->manager
);
770 assert(link
->manager
->rtnl
);
772 log_debug_link(link
, "setting MTU: %" PRIu32
, mtu
);
774 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
775 RTM_SETLINK
, link
->ifindex
);
777 log_error_link(link
, "Could not allocate RTM_SETLINK message");
781 r
= sd_rtnl_message_append_u32(req
, IFLA_MTU
, mtu
);
783 log_error_link(link
, "Could not append MTU: %s", strerror(-r
));
787 r
= sd_rtnl_call_async(link
->manager
->rtnl
, req
, set_mtu_handler
, link
,
791 "Could not send rtnetlink message: %s",
801 static void dhcp6_handler(sd_dhcp6_client
*client
, int event
, void *userdata
) {
802 Link
*link
= userdata
;
805 assert(link
->network
);
806 assert(link
->manager
);
808 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
812 case DHCP6_EVENT_STOP
:
813 case DHCP6_EVENT_RESEND_EXPIRE
:
814 case DHCP6_EVENT_RETRANS_MAX
:
815 case DHCP6_EVENT_IP_ACQUIRE
:
816 log_debug_link(link
, "DHCPv6 event %d", event
);
822 log_warning_link(link
, "DHCPv6 error: %s",
825 log_warning_link(link
, "DHCPv6 unknown event: %d",
831 static void icmp6_router_handler(sd_icmp6_nd
*nd
, int event
, void *userdata
) {
832 Link
*link
= userdata
;
836 assert(link
->network
);
837 assert(link
->manager
);
839 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
843 case ICMP6_EVENT_ROUTER_ADVERTISMENT_NONE
:
844 case ICMP6_EVENT_ROUTER_ADVERTISMENT_OTHER
:
847 case ICMP6_EVENT_ROUTER_ADVERTISMENT_TIMEOUT
:
848 case ICMP6_EVENT_ROUTER_ADVERTISMENT_MANAGED
:
853 log_warning_link(link
, "ICMPv6 error: %s",
856 log_warning_link(link
, "ICMPv6 unknown event: %d",
862 if (link
->dhcp6_client
)
865 r
= sd_dhcp6_client_new(&link
->dhcp6_client
);
869 r
= sd_dhcp6_client_attach_event(link
->dhcp6_client
, NULL
, 0);
871 link
->dhcp6_client
= sd_dhcp6_client_unref(link
->dhcp6_client
);
875 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
, &link
->mac
);
877 link
->dhcp6_client
= sd_dhcp6_client_unref(link
->dhcp6_client
);
881 r
= sd_dhcp6_client_set_index(link
->dhcp6_client
, link
->ifindex
);
883 link
->dhcp6_client
= sd_dhcp6_client_unref(link
->dhcp6_client
);
887 r
= sd_dhcp6_client_set_callback(link
->dhcp6_client
, dhcp6_handler
,
890 link
->dhcp6_client
= sd_dhcp6_client_unref(link
->dhcp6_client
);
894 r
= sd_dhcp6_client_start(link
->dhcp6_client
);
896 link
->dhcp6_client
= sd_dhcp6_client_unref(link
->dhcp6_client
);
899 static int link_acquire_conf(Link
*link
) {
903 assert(link
->network
);
904 assert(link
->manager
);
905 assert(link
->manager
->event
);
907 if (link
->network
->ipv4ll
) {
908 assert(link
->ipv4ll
);
910 log_debug_link(link
, "acquiring IPv4 link-local address");
912 r
= sd_ipv4ll_start(link
->ipv4ll
);
914 log_warning_link(link
, "could not acquire IPv4 "
915 "link-local address");
920 if (IN_SET(link
->network
->dhcp
, DHCP_SUPPORT_BOTH
, DHCP_SUPPORT_V4
)) {
921 assert(link
->dhcp_client
);
923 log_debug_link(link
, "acquiring DHCPv4 lease");
925 r
= sd_dhcp_client_start(link
->dhcp_client
);
927 log_warning_link(link
, "could not acquire DHCPv4 "
933 if (IN_SET(link
->network
->dhcp
, DHCP_SUPPORT_BOTH
, DHCP_SUPPORT_V6
)) {
934 assert(link
->icmp6_router_discovery
);
936 log_debug_link(link
, "discovering IPv6 routers");
938 r
= sd_icmp6_router_solicitation_start(link
->icmp6_router_discovery
);
940 log_warning_link(link
,
941 "could not start IPv6 router discovery");
949 bool link_has_carrier(Link
*link
) {
950 /* see Documentation/networking/operstates.txt in the kernel sources */
952 if (link
->kernel_operstate
== IF_OPER_UP
)
955 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
956 /* operstate may not be implemented, so fall back to flags */
957 if ((link
->flags
& IFF_LOWER_UP
) && !(link
->flags
& IFF_DORMANT
))
963 static int link_up_handler(sd_rtnl
*rtnl
, sd_rtnl_message
*m
, void *userdata
) {
964 _cleanup_link_unref_ Link
*link
= userdata
;
969 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
972 r
= sd_rtnl_message_get_errno(m
);
974 /* we warn but don't fail the link, as it may
975 be brought up later */
976 log_struct_link(LOG_WARNING
, link
,
977 "MESSAGE=%-*s: could not bring up interface: %s",
979 link
->ifname
, strerror(-r
),
987 static int link_up(Link
*link
) {
988 _cleanup_rtnl_message_unref_ sd_rtnl_message
*req
= NULL
;
992 assert(link
->manager
);
993 assert(link
->manager
->rtnl
);
995 log_debug_link(link
, "bringing link up");
997 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
998 RTM_SETLINK
, link
->ifindex
);
1000 log_error_link(link
, "Could not allocate RTM_SETLINK message");
1004 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1006 log_error_link(link
, "Could not set link flags: %s",
1011 r
= sd_rtnl_call_async(link
->manager
->rtnl
, req
, link_up_handler
, link
,
1014 log_error_link(link
,
1015 "Could not send rtnetlink message: %s",
1025 static int link_joined(Link
*link
) {
1029 assert(link
->network
);
1031 if (!(link
->flags
& IFF_UP
)) {
1034 link_enter_failed(link
);
1039 return link_enter_set_addresses(link
);
1042 static int netdev_join_handler(sd_rtnl
*rtnl
, sd_rtnl_message
*m
,
1044 _cleanup_link_unref_ Link
*link
= userdata
;
1048 assert(link
->network
);
1052 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1055 r
= sd_rtnl_message_get_errno(m
);
1056 if (r
< 0 && r
!= -EEXIST
) {
1057 log_struct_link(LOG_ERR
, link
,
1058 "MESSAGE=%-*s: could not join netdev: %s",
1060 link
->ifname
, strerror(-r
),
1063 link_enter_failed(link
);
1066 log_debug_link(link
, "joined netdev");
1068 if (link
->enslaving
<= 0)
1074 static int link_enter_join_netdev(Link
*link
) {
1080 assert(link
->network
);
1081 assert(link
->state
== LINK_STATE_PENDING
);
1083 link
->state
= LINK_STATE_ENSLAVING
;
1087 if (!link
->network
->bridge
&&
1088 !link
->network
->bond
&&
1089 hashmap_isempty(link
->network
->stacked_netdevs
))
1090 return link_joined(link
);
1092 if (link
->network
->bond
) {
1093 log_struct_link(LOG_DEBUG
, link
,
1094 "MESSAGE=%-*s: enslaving by '%s'",
1096 link
->ifname
, link
->network
->bond
->ifname
,
1097 NETDEVIF(link
->network
->bond
),
1100 r
= netdev_join(link
->network
->bond
, link
, &netdev_join_handler
);
1102 log_struct_link(LOG_WARNING
, link
,
1103 "MESSAGE=%-*s: could not join netdev '%s': %s",
1105 link
->ifname
, link
->network
->bond
->ifname
,
1107 NETDEVIF(link
->network
->bond
),
1109 link_enter_failed(link
);
1116 if (link
->network
->bridge
) {
1117 log_struct_link(LOG_DEBUG
, link
,
1118 "MESSAGE=%-*s: enslaving by '%s'",
1120 link
->ifname
, link
->network
->bridge
->ifname
,
1121 NETDEVIF(link
->network
->bridge
),
1124 r
= netdev_join(link
->network
->bridge
, link
,
1125 &netdev_join_handler
);
1127 log_struct_link(LOG_WARNING
, link
,
1128 "MESSAGE=%-*s: could not join netdev '%s': %s",
1130 link
->ifname
, link
->network
->bridge
->ifname
,
1132 NETDEVIF(link
->network
->bridge
),
1134 link_enter_failed(link
);
1141 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
1142 log_struct_link(LOG_DEBUG
, link
,
1143 "MESSAGE=%-*s: enslaving by '%s'",
1145 link
->ifname
, netdev
->ifname
, NETDEVIF(netdev
),
1148 r
= netdev_join(netdev
, link
, &netdev_join_handler
);
1150 log_struct_link(LOG_WARNING
, link
,
1151 "MESSAGE=%-*s: could not join netdev '%s': %s",
1153 link
->ifname
, netdev
->ifname
,
1155 NETDEVIF(netdev
), NULL
);
1156 link_enter_failed(link
);
1166 static int link_configure(Link
*link
) {
1170 assert(link
->network
);
1171 assert(link
->state
== LINK_STATE_PENDING
);
1173 if (link
->network
->ipv4ll
) {
1174 r
= ipv4ll_configure(link
);
1179 if (IN_SET(link
->network
->dhcp
, DHCP_SUPPORT_BOTH
, DHCP_SUPPORT_V4
)) {
1180 r
= dhcp4_configure(link
);
1185 if (link
->network
->dhcp_server
) {
1186 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
1190 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
1195 if (IN_SET(link
->network
->dhcp
, DHCP_SUPPORT_BOTH
, DHCP_SUPPORT_V6
)) {
1196 r
= sd_icmp6_nd_new(&link
->icmp6_router_discovery
);
1200 r
= sd_icmp6_nd_attach_event(link
->icmp6_router_discovery
,
1205 r
= sd_icmp6_nd_set_mac(link
->icmp6_router_discovery
,
1210 r
= sd_icmp6_nd_set_index(link
->icmp6_router_discovery
,
1215 r
= sd_icmp6_nd_set_callback(link
->icmp6_router_discovery
,
1216 icmp6_router_handler
, link
);
1221 if (link_has_carrier(link
)) {
1222 r
= link_acquire_conf(link
);
1227 return link_enter_join_netdev(link
);
1230 static int link_initialized_and_synced(sd_rtnl
*rtnl
, sd_rtnl_message
*m
,
1232 _cleanup_link_unref_ Link
*link
= userdata
;
1237 assert(link
->ifname
);
1238 assert(link
->manager
);
1240 if (link
->state
!= LINK_STATE_PENDING
)
1243 log_debug_link(link
, "link state is up-to-date");
1245 r
= network_get(link
->manager
, link
->udev_device
, link
->ifname
,
1246 &link
->mac
, &network
);
1248 link_enter_unmanaged(link
);
1253 if (link
->flags
& IFF_LOOPBACK
) {
1254 log_debug_link(link
, "matching network ignored for loopback link");
1255 link_enter_unmanaged(link
);
1259 r
= network_apply(link
->manager
, network
, link
);
1263 r
= link_configure(link
);
1270 int link_initialized(Link
*link
, struct udev_device
*device
) {
1271 _cleanup_rtnl_message_unref_ sd_rtnl_message
*req
= NULL
;
1275 assert(link
->manager
);
1276 assert(link
->manager
->rtnl
);
1279 if (link
->state
!= LINK_STATE_PENDING
)
1282 if (link
->udev_device
)
1285 log_debug_link(link
, "udev initialized link");
1287 link
->udev_device
= udev_device_ref(device
);
1289 /* udev has initialized the link, but we don't know if we have yet
1290 * processed the NEWLINK messages with the latest state. Do a GETLINK,
1291 * when it returns we know that the pending NEWLINKs have already been
1292 * processed and that we are up-to-date */
1294 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
1299 r
= sd_rtnl_call_async(link
->manager
->rtnl
, req
,
1300 link_initialized_and_synced
, link
, 0, NULL
);
1309 int link_rtnl_process_address(sd_rtnl
*rtnl
, sd_rtnl_message
*message
,
1311 Manager
*m
= userdata
;
1314 _cleanup_address_free_ Address
*address
= NULL
;
1316 char buf
[INET6_ADDRSTRLEN
];
1317 char valid_buf
[FORMAT_TIMESPAN_MAX
];
1318 const char *valid_str
= NULL
;
1319 bool address_dropped
= false;
1326 r
= sd_rtnl_message_get_type(message
, &type
);
1328 log_warning("rtnl: could not get message type");
1332 r
= sd_rtnl_message_addr_get_ifindex(message
, &ifindex
);
1333 if (r
< 0 || ifindex
<= 0) {
1334 log_warning("rtnl: received address message without valid ifindex, ignoring");
1337 r
= link_get(m
, ifindex
, &link
);
1338 if (r
< 0 || !link
) {
1339 log_warning("rtnl: received address for a nonexistent link, ignoring");
1344 r
= address_new_dynamic(&address
);
1348 r
= sd_rtnl_message_addr_get_family(message
, &address
->family
);
1349 if (r
< 0 || !IN_SET(address
->family
, AF_INET
, AF_INET6
)) {
1350 log_warning_link(link
,
1351 "rtnl: received address with invalid family, ignoring");
1355 r
= sd_rtnl_message_addr_get_prefixlen(message
, &address
->prefixlen
);
1357 log_warning_link(link
,
1358 "rtnl: received address with invalid prefixlen, ignoring");
1362 r
= sd_rtnl_message_addr_get_scope(message
, &address
->scope
);
1364 log_warning_link(link
,
1365 "rtnl: received address with invalid scope, ignoring");
1369 r
= sd_rtnl_message_addr_get_flags(message
, &address
->flags
);
1371 log_warning_link(link
,
1372 "rtnl: received address with invalid flags, ignoring");
1376 switch (address
->family
) {
1378 r
= sd_rtnl_message_read_in_addr(message
, IFA_LOCAL
,
1379 &address
->in_addr
.in
);
1381 log_warning_link(link
,
1382 "rtnl: received address without valid address, ignoring");
1389 r
= sd_rtnl_message_read_in6_addr(message
, IFA_ADDRESS
,
1390 &address
->in_addr
.in6
);
1392 log_warning_link(link
,
1393 "rtnl: received address without valid address, ignoring");
1400 assert_not_reached("invalid address family");
1403 if (!inet_ntop(address
->family
, &address
->in_addr
, buf
,
1404 INET6_ADDRSTRLEN
)) {
1405 log_warning_link(link
, "could not print address");
1409 r
= sd_rtnl_message_read_cache_info(message
, IFA_CACHEINFO
,
1412 if (address
->cinfo
.ifa_valid
== CACHE_INFO_INFINITY_LIFE_TIME
)
1415 valid_str
= format_timespan(valid_buf
, FORMAT_TIMESPAN_MAX
,
1416 address
->cinfo
.ifa_valid
* USEC_PER_SEC
,
1420 LIST_FOREACH(addresses
, ad
, link
->addresses
) {
1421 if (address_equal(ad
, address
)) {
1422 LIST_REMOVE(addresses
, link
->addresses
, ad
);
1426 address_dropped
= true;
1434 if (!address_dropped
)
1435 log_debug_link(link
, "added address: %s/%u (valid for %s)",
1436 buf
, address
->prefixlen
,
1439 log_debug_link(link
, "updated address: %s/%u (valid for %s)",
1440 buf
, address
->prefixlen
,
1443 LIST_PREPEND(addresses
, link
->addresses
, address
);
1450 if (address_dropped
) {
1451 log_debug_link(link
, "removed address: %s/%u (valid for %s)",
1452 buf
, address
->prefixlen
,
1457 log_warning_link(link
,
1458 "removing non-existent address: %s/%u (valid for %s)",
1459 buf
, address
->prefixlen
,
1464 assert_not_reached("Received invalid RTNL message type");
1470 int link_add(Manager
*m
, sd_rtnl_message
*message
, Link
**ret
) {
1472 _cleanup_rtnl_message_unref_ sd_rtnl_message
*req
= NULL
;
1473 _cleanup_udev_device_unref_
struct udev_device
*device
= NULL
;
1474 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
1482 r
= link_new(m
, message
, ret
);
1488 log_debug_link(link
, "link %d added", link
->ifindex
);
1490 r
= sd_rtnl_message_new_addr(m
->rtnl
, &req
, RTM_GETADDR
, link
->ifindex
,
1495 r
= sd_rtnl_call_async(m
->rtnl
, req
, link_get_address_handler
, link
, 0,
1502 if (detect_container(NULL
) <= 0) {
1503 /* not in a container, udev will be around */
1504 sprintf(ifindex_str
, "n%d", link
->ifindex
);
1505 device
= udev_device_new_from_device_id(m
->udev
, ifindex_str
);
1507 log_warning_link(link
,
1508 "could not find udev device: %m");
1512 if (udev_device_get_is_initialized(device
) <= 0) {
1514 log_debug_link(link
, "link pending udev initialization...");
1518 r
= link_initialized(link
, device
);
1522 /* we are calling a callback directly, so must take a ref */
1525 r
= link_initialized_and_synced(m
->rtnl
, NULL
, link
);
1533 int link_update(Link
*link
, sd_rtnl_message
*m
) {
1534 struct ether_addr mac
;
1537 bool had_carrier
, carrier_gained
, carrier_lost
;
1541 assert(link
->ifname
);
1544 if (link
->state
== LINK_STATE_LINGER
) {
1546 log_info_link(link
, "link readded");
1547 link
->state
= LINK_STATE_ENSLAVING
;
1550 r
= sd_rtnl_message_read_string(m
, IFLA_IFNAME
, &ifname
);
1551 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
1552 log_info_link(link
, "renamed to %s", ifname
);
1555 link
->ifname
= strdup(ifname
);
1560 r
= sd_rtnl_message_read_u32(m
, IFLA_MTU
, &mtu
);
1561 if (r
>= 0 && mtu
> 0) {
1563 if (!link
->original_mtu
) {
1564 link
->original_mtu
= mtu
;
1565 log_debug_link(link
, "saved original MTU: %"
1566 PRIu32
, link
->original_mtu
);
1569 if (link
->dhcp_client
) {
1570 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
1573 log_warning_link(link
,
1574 "Could not update MTU in DHCP client: %s",
1581 /* The kernel may broadcast NEWLINK messages without the MAC address
1582 set, simply ignore them. */
1583 r
= sd_rtnl_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
1585 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
1588 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
1591 log_debug_link(link
, "MAC address: "
1592 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
1593 mac
.ether_addr_octet
[0],
1594 mac
.ether_addr_octet
[1],
1595 mac
.ether_addr_octet
[2],
1596 mac
.ether_addr_octet
[3],
1597 mac
.ether_addr_octet
[4],
1598 mac
.ether_addr_octet
[5]);
1601 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
1603 log_warning_link(link
,
1604 "Could not update MAC address in IPv4LL client: %s",
1610 if (link
->dhcp_client
) {
1611 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
1614 log_warning_link(link
,
1615 "Could not update MAC address in DHCP client: %s",
1621 if (link
->dhcp6_client
) {
1622 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
1625 log_warning_link(link
,
1626 "Could not update MAC address in DHCPv6 client: %s",
1634 had_carrier
= link_has_carrier(link
);
1636 r
= link_update_flags(link
, m
);
1640 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
1641 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
1643 if (carrier_gained
) {
1644 log_info_link(link
, "gained carrier");
1646 if (link
->network
) {
1647 r
= link_acquire_conf(link
);
1649 link_enter_failed(link
);
1653 } else if (carrier_lost
) {
1654 log_info_link(link
, "lost carrier");
1656 r
= link_stop_clients(link
);
1658 link_enter_failed(link
);
1666 static void link_update_operstate(Link
*link
) {
1670 if (link
->kernel_operstate
== IF_OPER_DORMANT
)
1671 link
->operstate
= LINK_OPERSTATE_DORMANT
;
1672 else if (link_has_carrier(link
)) {
1674 uint8_t scope
= RT_SCOPE_NOWHERE
;
1676 /* if we have carrier, check what addresses we have */
1677 LIST_FOREACH(addresses
, address
, link
->addresses
) {
1678 if (address
->flags
& (IFA_F_TENTATIVE
| IFA_F_DEPRECATED
))
1681 if (address
->scope
< scope
)
1682 scope
= address
->scope
;
1685 if (scope
< RT_SCOPE_SITE
)
1686 /* universally accessible addresses found */
1687 link
->operstate
= LINK_OPERSTATE_ROUTABLE
;
1688 else if (scope
< RT_SCOPE_HOST
)
1689 /* only link or site local addresses found */
1690 link
->operstate
= LINK_OPERSTATE_DEGRADED
;
1692 /* no useful addresses found */
1693 link
->operstate
= LINK_OPERSTATE_CARRIER
;
1694 } else if (link
->flags
& IFF_UP
)
1695 link
->operstate
= LINK_OPERSTATE_NO_CARRIER
;
1697 link
->operstate
= LINK_OPERSTATE_OFF
;
1700 int link_save(Link
*link
) {
1701 _cleanup_free_
char *temp_path
= NULL
;
1702 _cleanup_fclose_
FILE *f
= NULL
;
1703 const char *admin_state
, *oper_state
;
1707 assert(link
->state_file
);
1708 assert(link
->lease_file
);
1709 assert(link
->manager
);
1711 link_update_operstate(link
);
1713 r
= manager_save(link
->manager
);
1717 if (link
->state
== LINK_STATE_LINGER
) {
1718 unlink(link
->state_file
);
1722 admin_state
= link_state_to_string(link
->state
);
1723 assert(admin_state
);
1725 oper_state
= link_operstate_to_string(link
->operstate
);
1728 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
1732 fchmod(fileno(f
), 0644);
1735 "# This is private data. Do not parse.\n"
1738 admin_state
, oper_state
);
1740 if (link
->network
) {
1741 char **address
, **domain
;
1746 STRV_FOREACH(address
, link
->network
->dns
) {
1753 if (link
->network
->dhcp_dns
&&
1755 const struct in_addr
*addresses
;
1757 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
1761 serialize_in_addrs(f
, addresses
, r
);
1769 STRV_FOREACH(address
, link
->network
->ntp
) {
1776 if (link
->network
->dhcp_ntp
&&
1778 const struct in_addr
*addresses
;
1780 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
1784 serialize_in_addrs(f
, addresses
, r
);
1790 fprintf(f
, "DOMAINS=");
1792 STRV_FOREACH(domain
, link
->network
->domains
) {
1799 if (link
->network
->dhcp_domains
&&
1801 const char *domainname
;
1803 r
= sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &domainname
);
1807 fputs(domainname
, f
);
1813 fprintf(f
, "WILDCARD_DOMAIN=%s\n",
1814 yes_no(link
->network
->wildcard_domain
));
1816 fprintf(f
, "LLMNR=%s\n",
1817 llmnr_support_to_string(link
->network
->llmnr
));
1820 if (link
->dhcp_lease
) {
1821 assert(link
->network
);
1823 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
1831 unlink(link
->lease_file
);
1833 r
= fflush_and_check(f
);
1837 if (rename(temp_path
, link
->state_file
) < 0) {
1844 log_error_link(link
, "Failed to save link data to %s: %s", link
->state_file
, strerror(-r
));
1845 unlink(link
->state_file
);
1850 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
1851 [LINK_STATE_PENDING
] = "pending",
1852 [LINK_STATE_ENSLAVING
] = "configuring",
1853 [LINK_STATE_SETTING_ADDRESSES
] = "configuring",
1854 [LINK_STATE_SETTING_ROUTES
] = "configuring",
1855 [LINK_STATE_CONFIGURED
] = "configured",
1856 [LINK_STATE_UNMANAGED
] = "unmanaged",
1857 [LINK_STATE_FAILED
] = "failed",
1858 [LINK_STATE_LINGER
] = "linger",
1861 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);
1863 static const char* const link_operstate_table
[_LINK_OPERSTATE_MAX
] = {
1864 [LINK_OPERSTATE_OFF
] = "off",
1865 [LINK_OPERSTATE_NO_CARRIER
] = "no-carrier",
1866 [LINK_OPERSTATE_DORMANT
] = "dormant",
1867 [LINK_OPERSTATE_CARRIER
] = "carrier",
1868 [LINK_OPERSTATE_DEGRADED
] = "degraded",
1869 [LINK_OPERSTATE_ROUTABLE
] = "routable",
1872 DEFINE_STRING_TABLE_LOOKUP(link_operstate
, LinkOperationalState
);