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>
27 #include "dhcp-lease-internal.h"
30 #include "netlink-util.h"
31 #include "network-internal.h"
32 #include "networkd-link.h"
33 #include "networkd-netdev.h"
35 #include "socket-util.h"
36 #include "string-table.h"
37 #include "udev-util.h"
41 bool link_dhcp6_enabled(Link
*link
) {
42 if (link
->flags
& IFF_LOOPBACK
)
48 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV6
;
51 bool link_dhcp4_enabled(Link
*link
) {
52 if (link
->flags
& IFF_LOOPBACK
)
58 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV4
;
61 bool link_dhcp4_server_enabled(Link
*link
) {
62 if (link
->flags
& IFF_LOOPBACK
)
68 return link
->network
->dhcp_server
;
71 bool link_ipv4ll_enabled(Link
*link
) {
72 if (link
->flags
& IFF_LOOPBACK
)
78 return link
->network
->link_local
& ADDRESS_FAMILY_IPV4
;
81 bool link_ipv6ll_enabled(Link
*link
) {
82 if (link
->flags
& IFF_LOOPBACK
)
88 return link
->network
->link_local
& ADDRESS_FAMILY_IPV6
;
91 bool link_lldp_enabled(Link
*link
) {
92 if (link
->flags
& IFF_LOOPBACK
)
98 if (link
->network
->bridge
)
101 return link
->network
->lldp
;
104 static bool link_ipv4_forward_enabled(Link
*link
) {
105 if (link
->flags
& IFF_LOOPBACK
)
111 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV4
;
114 static bool link_ipv6_forward_enabled(Link
*link
) {
115 if (link
->flags
& IFF_LOOPBACK
)
121 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV6
;
124 static IPv6PrivacyExtensions
link_ipv6_privacy_extensions(Link
*link
) {
125 if (link
->flags
& IFF_LOOPBACK
)
126 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
129 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
131 return link
->network
->ipv6_privacy_extensions
;
134 void link_update_operstate(Link
*link
) {
135 LinkOperationalState operstate
;
138 if (link
->kernel_operstate
== IF_OPER_DORMANT
)
139 operstate
= LINK_OPERSTATE_DORMANT
;
140 else if (link_has_carrier(link
)) {
142 uint8_t scope
= RT_SCOPE_NOWHERE
;
145 /* if we have carrier, check what addresses we have */
146 SET_FOREACH(address
, link
->addresses
, i
) {
147 if (!address_is_ready(address
))
150 if (address
->scope
< scope
)
151 scope
= address
->scope
;
154 /* for operstate we also take foreign addresses into account */
155 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
156 if (!address_is_ready(address
))
159 if (address
->scope
< scope
)
160 scope
= address
->scope
;
163 if (scope
< RT_SCOPE_SITE
)
164 /* universally accessible addresses found */
165 operstate
= LINK_OPERSTATE_ROUTABLE
;
166 else if (scope
< RT_SCOPE_HOST
)
167 /* only link or site local addresses found */
168 operstate
= LINK_OPERSTATE_DEGRADED
;
170 /* no useful addresses found */
171 operstate
= LINK_OPERSTATE_CARRIER
;
172 } else if (link
->flags
& IFF_UP
)
173 operstate
= LINK_OPERSTATE_NO_CARRIER
;
175 operstate
= LINK_OPERSTATE_OFF
;
177 if (link
->operstate
!= operstate
) {
178 link
->operstate
= operstate
;
179 link_send_changed(link
, "OperationalState", NULL
);
181 manager_dirty(link
->manager
);
185 #define FLAG_STRING(string, flag, old, new) \
186 (((old ^ new) & flag) \
187 ? ((old & flag) ? (" -" string) : (" +" string)) \
190 static int link_update_flags(Link
*link
, sd_netlink_message
*m
) {
191 unsigned flags
, unknown_flags_added
, unknown_flags_removed
, unknown_flags
;
197 r
= sd_rtnl_message_link_get_flags(m
, &flags
);
199 return log_link_warning_errno(link
, r
, "Could not get link flags: %m");
201 r
= sd_netlink_message_read_u8(m
, IFLA_OPERSTATE
, &operstate
);
203 /* if we got a message without operstate, take it to mean
204 the state was unchanged */
205 operstate
= link
->kernel_operstate
;
207 if ((link
->flags
== flags
) && (link
->kernel_operstate
== operstate
))
210 if (link
->flags
!= flags
) {
211 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",
212 FLAG_STRING("LOOPBACK", IFF_LOOPBACK
, link
->flags
, flags
),
213 FLAG_STRING("MASTER", IFF_MASTER
, link
->flags
, flags
),
214 FLAG_STRING("SLAVE", IFF_SLAVE
, link
->flags
, flags
),
215 FLAG_STRING("UP", IFF_UP
, link
->flags
, flags
),
216 FLAG_STRING("DORMANT", IFF_DORMANT
, link
->flags
, flags
),
217 FLAG_STRING("LOWER_UP", IFF_LOWER_UP
, link
->flags
, flags
),
218 FLAG_STRING("RUNNING", IFF_RUNNING
, link
->flags
, flags
),
219 FLAG_STRING("MULTICAST", IFF_MULTICAST
, link
->flags
, flags
),
220 FLAG_STRING("BROADCAST", IFF_BROADCAST
, link
->flags
, flags
),
221 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT
, link
->flags
, flags
),
222 FLAG_STRING("PROMISC", IFF_PROMISC
, link
->flags
, flags
),
223 FLAG_STRING("ALLMULTI", IFF_ALLMULTI
, link
->flags
, flags
),
224 FLAG_STRING("PORTSEL", IFF_PORTSEL
, link
->flags
, flags
),
225 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA
, link
->flags
, flags
),
226 FLAG_STRING("DYNAMIC", IFF_DYNAMIC
, link
->flags
, flags
),
227 FLAG_STRING("NOARP", IFF_NOARP
, link
->flags
, flags
),
228 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS
, link
->flags
, flags
),
229 FLAG_STRING("DEBUG", IFF_DEBUG
, link
->flags
, flags
),
230 FLAG_STRING("ECHO", IFF_ECHO
, link
->flags
, flags
));
232 unknown_flags
= ~(IFF_LOOPBACK
| IFF_MASTER
| IFF_SLAVE
| IFF_UP
|
233 IFF_DORMANT
| IFF_LOWER_UP
| IFF_RUNNING
|
234 IFF_MULTICAST
| IFF_BROADCAST
| IFF_POINTOPOINT
|
235 IFF_PROMISC
| IFF_ALLMULTI
| IFF_PORTSEL
|
236 IFF_AUTOMEDIA
| IFF_DYNAMIC
| IFF_NOARP
|
237 IFF_NOTRAILERS
| IFF_DEBUG
| IFF_ECHO
);
238 unknown_flags_added
= ((link
->flags
^ flags
) & flags
& unknown_flags
);
239 unknown_flags_removed
= ((link
->flags
^ flags
) & link
->flags
& unknown_flags
);
241 /* link flags are currently at most 18 bits, let's align to
243 if (unknown_flags_added
)
245 "Unknown link flags gained: %#.5x (ignoring)",
246 unknown_flags_added
);
248 if (unknown_flags_removed
)
250 "Unknown link flags lost: %#.5x (ignoring)",
251 unknown_flags_removed
);
255 link
->kernel_operstate
= operstate
;
257 link_update_operstate(link
);
262 static int link_new(Manager
*manager
, sd_netlink_message
*message
, Link
**ret
) {
263 _cleanup_link_unref_ Link
*link
= NULL
;
272 r
= sd_netlink_message_get_type(message
, &type
);
275 else if (type
!= RTM_NEWLINK
)
278 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
281 else if (ifindex
<= 0)
284 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &ifname
);
288 link
= new0(Link
, 1);
293 link
->manager
= manager
;
294 link
->state
= LINK_STATE_PENDING
;
295 link
->rtnl_extended_attrs
= true;
296 link
->ifindex
= ifindex
;
297 link
->ifname
= strdup(ifname
);
301 r
= sd_netlink_message_read_ether_addr(message
, IFLA_ADDRESS
, &link
->mac
);
303 log_link_debug(link
, "MAC address not found for new device, continuing without");
305 r
= asprintf(&link
->state_file
, "/run/systemd/netif/links/%d",
310 r
= asprintf(&link
->lease_file
, "/run/systemd/netif/leases/%d",
315 r
= asprintf(&link
->lldp_file
, "/run/systemd/netif/lldp/%d",
321 r
= hashmap_ensure_allocated(&manager
->links
, NULL
);
325 r
= hashmap_put(manager
->links
, INT_TO_PTR(link
->ifindex
), link
);
329 r
= link_update_flags(link
, message
);
339 static void link_free(Link
*link
) {
347 while (!set_isempty(link
->addresses
))
348 address_free(set_first(link
->addresses
));
350 set_free(link
->addresses
);
352 while (!set_isempty(link
->addresses_foreign
))
353 address_free(set_first(link
->addresses_foreign
));
355 set_free(link
->addresses_foreign
);
357 while ((address
= link
->pool_addresses
)) {
358 LIST_REMOVE(addresses
, link
->pool_addresses
, address
);
359 address_free(address
);
362 sd_dhcp_server_unref(link
->dhcp_server
);
363 sd_dhcp_client_unref(link
->dhcp_client
);
364 sd_dhcp_lease_unref(link
->dhcp_lease
);
366 free(link
->lease_file
);
368 sd_lldp_free(link
->lldp
);
370 free(link
->lldp_file
);
372 sd_ipv4ll_unref(link
->ipv4ll
);
373 sd_dhcp6_client_unref(link
->dhcp6_client
);
374 sd_ndisc_unref(link
->ndisc_router_discovery
);
377 hashmap_remove(link
->manager
->links
, INT_TO_PTR(link
->ifindex
));
381 (void)unlink(link
->state_file
);
382 free(link
->state_file
);
384 udev_device_unref(link
->udev_device
);
386 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
)
387 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(carrier
->ifindex
));
388 hashmap_free(link
->bound_to_links
);
390 HASHMAP_FOREACH (carrier
, link
->bound_by_links
, i
)
391 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(carrier
->ifindex
));
392 hashmap_free(link
->bound_by_links
);
397 Link
*link_unref(Link
*link
) {
401 assert(link
->n_ref
> 0);
413 Link
*link_ref(Link
*link
) {
417 assert(link
->n_ref
> 0);
424 int link_get(Manager
*m
, int ifindex
, Link
**ret
) {
431 link
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
440 static void link_set_state(Link
*link
, LinkState state
) {
443 if (link
->state
== state
)
448 link_send_changed(link
, "AdministrativeState", NULL
);
453 static void link_enter_unmanaged(Link
*link
) {
456 log_link_debug(link
, "Unmanaged");
458 link_set_state(link
, LINK_STATE_UNMANAGED
);
463 static int link_stop_clients(Link
*link
) {
467 assert(link
->manager
);
468 assert(link
->manager
->event
);
473 if (link
->dhcp_client
) {
474 k
= sd_dhcp_client_stop(link
->dhcp_client
);
476 r
= log_link_warning_errno(link
, r
, "Could not stop DHCPv4 client: %m");
480 k
= sd_ipv4ll_stop(link
->ipv4ll
);
482 r
= log_link_warning_errno(link
, r
, "Could not stop IPv4 link-local: %m");
485 if(link
->ndisc_router_discovery
) {
486 if (link
->dhcp6_client
) {
487 k
= sd_dhcp6_client_stop(link
->dhcp6_client
);
489 r
= log_link_warning_errno(link
, r
, "Could not stop DHCPv6 client: %m");
492 k
= sd_ndisc_stop(link
->ndisc_router_discovery
);
494 r
= log_link_warning_errno(link
, r
, "Could not stop IPv6 Router Discovery: %m");
498 k
= sd_lldp_stop(link
->lldp
);
500 r
= log_link_warning_errno(link
, r
, "Could not stop LLDP: %m");
506 void link_enter_failed(Link
*link
) {
509 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
512 log_link_warning(link
, "Failed");
514 link_set_state(link
, LINK_STATE_FAILED
);
516 link_stop_clients(link
);
521 static Address
* link_find_dhcp_server_address(Link
*link
) {
525 assert(link
->network
);
527 /* The first statically configured address if there is any */
528 LIST_FOREACH(addresses
, address
, link
->network
->static_addresses
) {
530 if (address
->family
!= AF_INET
)
533 if (in_addr_is_null(address
->family
, &address
->in_addr
))
539 /* If that didn't work, find a suitable address we got from the pool */
540 LIST_FOREACH(addresses
, address
, link
->pool_addresses
) {
541 if (address
->family
!= AF_INET
)
550 static int link_enter_configured(Link
*link
) {
552 assert(link
->network
);
553 assert(link
->state
== LINK_STATE_SETTING_ROUTES
);
555 log_link_info(link
, "Configured");
557 link_set_state(link
, LINK_STATE_CONFIGURED
);
564 void link_check_ready(Link
*link
) {
573 if (!link
->static_configured
)
576 if (link_ipv4ll_enabled(link
))
577 if (!link
->ipv4ll_address
||
581 if ((link_dhcp4_enabled(link
) && !link_dhcp6_enabled(link
) &&
582 !link
->dhcp4_configured
) ||
583 (link_dhcp6_enabled(link
) && !link_dhcp4_enabled(link
) &&
584 !link
->dhcp6_configured
) ||
585 (link_dhcp4_enabled(link
) && link_dhcp6_enabled(link
) &&
586 !link
->dhcp4_configured
&& !link
->dhcp6_configured
))
589 SET_FOREACH(a
, link
->addresses
, i
)
590 if (!address_is_ready(a
))
593 if (link
->state
!= LINK_STATE_CONFIGURED
)
594 link_enter_configured(link
);
599 static int route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
600 _cleanup_link_unref_ Link
*link
= userdata
;
603 assert(link
->link_messages
> 0);
604 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
605 LINK_STATE_SETTING_ROUTES
, LINK_STATE_FAILED
,
608 link
->link_messages
--;
610 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
613 r
= sd_netlink_message_get_errno(m
);
614 if (r
< 0 && r
!= -EEXIST
)
615 log_link_warning_errno(link
, r
, "Could not set route: %m");
617 if (link
->link_messages
== 0) {
618 log_link_debug(link
, "Routes set");
619 link
->static_configured
= true;
620 link_check_ready(link
);
626 static int link_enter_set_routes(Link
*link
) {
631 assert(link
->network
);
632 assert(link
->state
== LINK_STATE_SETTING_ADDRESSES
);
634 link_set_state(link
, LINK_STATE_SETTING_ROUTES
);
636 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
637 r
= route_configure(rt
, link
, &route_handler
);
639 log_link_warning_errno(link
, r
, "Could not set routes: %m");
640 link_enter_failed(link
);
644 link
->link_messages
++;
647 if (link
->link_messages
== 0) {
648 link
->static_configured
= true;
649 link_check_ready(link
);
651 log_link_debug(link
, "Setting routes");
656 int link_route_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
657 _cleanup_link_unref_ Link
*link
= userdata
;
662 assert(link
->ifname
);
664 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
667 r
= sd_netlink_message_get_errno(m
);
668 if (r
< 0 && r
!= -ESRCH
)
669 log_link_warning_errno(link
, r
, "Could not drop route: %m");
674 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
675 _cleanup_link_unref_ Link
*link
= userdata
;
681 assert(link
->ifname
);
682 assert(link
->link_messages
> 0);
683 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
684 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
686 link
->link_messages
--;
688 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
691 r
= sd_netlink_message_get_errno(m
);
692 if (r
< 0 && r
!= -EEXIST
)
693 log_link_warning_errno(link
, r
, "could not set address: %m");
695 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
697 if (link
->link_messages
== 0) {
698 log_link_debug(link
, "Addresses set");
699 link_enter_set_routes(link
);
705 static int link_push_dns_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
706 _cleanup_free_
struct in_addr
*addresses
= NULL
;
707 size_t n_addresses
= 0, n_allocated
= 0;
710 log_debug("Copying DNS server information from %s", link
->ifname
);
715 STRV_FOREACH(a
, link
->network
->dns
) {
718 /* Only look for IPv4 addresses */
719 if (inet_pton(AF_INET
, *a
, &ia
) <= 0)
722 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
725 addresses
[n_addresses
++] = ia
;
728 if (link
->network
->dhcp_dns
&&
730 const struct in_addr
*da
= NULL
;
733 n
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &da
);
736 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
739 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
744 if (n_addresses
<= 0)
747 return sd_dhcp_server_set_dns(s
, addresses
, n_addresses
);
750 static int link_push_ntp_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
751 _cleanup_free_
struct in_addr
*addresses
= NULL
;
752 size_t n_addresses
= 0, n_allocated
= 0;
758 log_debug("Copying NTP server information from %s", link
->ifname
);
760 STRV_FOREACH(a
, link
->network
->ntp
) {
763 /* Only look for IPv4 addresses */
764 if (inet_pton(AF_INET
, *a
, &ia
) <= 0)
767 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
770 addresses
[n_addresses
++] = ia
;
773 if (link
->network
->dhcp_ntp
&&
775 const struct in_addr
*da
= NULL
;
778 n
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &da
);
781 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
784 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
789 if (n_addresses
<= 0)
792 return sd_dhcp_server_set_ntp(s
, addresses
, n_addresses
);
795 static int link_enter_set_addresses(Link
*link
) {
800 assert(link
->network
);
801 assert(link
->state
!= _LINK_STATE_INVALID
);
803 link_set_state(link
, LINK_STATE_SETTING_ADDRESSES
);
805 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
806 r
= address_configure(ad
, link
, &address_handler
, false);
808 log_link_warning_errno(link
, r
, "Could not set addresses: %m");
809 link_enter_failed(link
);
813 link
->link_messages
++;
816 /* now that we can figure out a default address for the dhcp server,
818 if (link_dhcp4_server_enabled(link
)) {
821 bool acquired_uplink
= false;
823 address
= link_find_dhcp_server_address(link
);
825 log_link_warning(link
, "Failed to find suitable address for DHCPv4 server instance.");
826 link_enter_failed(link
);
830 /* use the server address' subnet as the pool */
831 r
= sd_dhcp_server_configure_pool(link
->dhcp_server
, &address
->in_addr
.in
, address
->prefixlen
,
832 link
->network
->dhcp_server_pool_offset
, link
->network
->dhcp_server_pool_size
);
837 r = sd_dhcp_server_set_router(link->dhcp_server,
838 &main_address->in_addr.in);
843 if (link
->network
->dhcp_server_max_lease_time_usec
> 0) {
844 r
= sd_dhcp_server_set_max_lease_time(
846 DIV_ROUND_UP(link
->network
->dhcp_server_max_lease_time_usec
, USEC_PER_SEC
));
851 if (link
->network
->dhcp_server_default_lease_time_usec
> 0) {
852 r
= sd_dhcp_server_set_default_lease_time(
854 DIV_ROUND_UP(link
->network
->dhcp_server_default_lease_time_usec
, USEC_PER_SEC
));
859 if (link
->network
->dhcp_server_emit_dns
) {
861 if (link
->network
->n_dhcp_server_dns
> 0)
862 r
= sd_dhcp_server_set_dns(link
->dhcp_server
, link
->network
->dhcp_server_dns
, link
->network
->n_dhcp_server_dns
);
864 uplink
= manager_find_uplink(link
->manager
, link
);
865 acquired_uplink
= true;
868 log_link_debug(link
, "Not emitting DNS server information on link, couldn't find suitable uplink.");
871 r
= link_push_dns_to_dhcp_server(uplink
, link
->dhcp_server
);
874 log_link_warning_errno(link
, r
, "Failed to set DNS server for DHCP server, ignoring: %m");
878 if (link
->network
->dhcp_server_emit_ntp
) {
880 if (link
->network
->n_dhcp_server_ntp
> 0)
881 r
= sd_dhcp_server_set_ntp(link
->dhcp_server
, link
->network
->dhcp_server_ntp
, link
->network
->n_dhcp_server_ntp
);
883 if (!acquired_uplink
)
884 uplink
= manager_find_uplink(link
->manager
, link
);
887 log_link_debug(link
, "Not emitting NTP server information on link, couldn't find suitable uplink.");
890 r
= link_push_ntp_to_dhcp_server(uplink
, link
->dhcp_server
);
894 log_link_warning_errno(link
, r
, "Failed to set NTP server for DHCP server, ignoring: %m");
897 if (link
->network
->dhcp_server_emit_timezone
) {
898 _cleanup_free_
char *buffer
= NULL
;
899 const char *tz
= NULL
;
901 if (link
->network
->dhcp_server_timezone
)
902 tz
= link
->network
->dhcp_server_timezone
;
904 r
= get_timezone(&buffer
);
906 log_warning_errno(r
, "Failed to determine timezone: %m");
912 r
= sd_dhcp_server_set_timezone(link
->dhcp_server
, tz
);
918 r
= sd_dhcp_server_start(link
->dhcp_server
);
920 log_link_warning_errno(link
, r
, "Could not start DHCPv4 server instance: %m");
922 link_enter_failed(link
);
927 log_link_debug(link
, "Offering DHCPv4 leases");
930 if (link
->link_messages
== 0)
931 link_enter_set_routes(link
);
933 log_link_debug(link
, "Setting addresses");
938 int link_address_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
939 _cleanup_link_unref_ Link
*link
= userdata
;
944 assert(link
->ifname
);
946 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
949 r
= sd_netlink_message_get_errno(m
);
950 if (r
< 0 && r
!= -EADDRNOTAVAIL
)
951 log_link_warning_errno(link
, r
, "Could not drop address: %m");
956 static int link_set_bridge_fdb(Link
*const link
) {
960 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
961 r
= fdb_entry_configure(link
, fdb_entry
);
963 log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
971 static int link_set_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
972 _cleanup_link_unref_ Link
*link
= userdata
;
975 log_link_debug(link
, "Set link");
977 r
= sd_netlink_message_get_errno(m
);
978 if (r
< 0 && r
!= -EEXIST
) {
979 log_link_error_errno(link
, r
, "Could not join netdev: %m");
980 link_enter_failed(link
);
987 static int set_hostname_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
988 _cleanup_link_unref_ Link
*link
= userdata
;
989 const sd_bus_error
*e
;
994 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
997 e
= sd_bus_message_get_error(m
);
999 log_link_warning_errno(link
, sd_bus_error_get_errno(e
), "Could not set hostname: %s", e
->message
);
1004 int link_set_hostname(Link
*link
, const char *hostname
) {
1008 assert(link
->manager
);
1010 log_link_debug(link
, "Setting transient hostname: '%s'", strna(hostname
));
1012 if (!link
->manager
->bus
) {
1013 /* TODO: replace by assert when we can rely on kdbus */
1014 log_link_info(link
, "Not connected to system bus, ignoring transient hostname.");
1018 r
= sd_bus_call_method_async(
1021 "org.freedesktop.hostname1",
1022 "/org/freedesktop/hostname1",
1023 "org.freedesktop.hostname1",
1025 set_hostname_handler
,
1032 return log_link_error_errno(link
, r
, "Could not set transient hostname: %m");
1039 static int set_timezone_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
1040 _cleanup_link_unref_ Link
*link
= userdata
;
1041 const sd_bus_error
*e
;
1046 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1049 e
= sd_bus_message_get_error(m
);
1051 log_link_warning_errno(link
, sd_bus_error_get_errno(e
), "Could not set timezone: %s", e
->message
);
1056 int link_set_timezone(Link
*link
, const char *tz
) {
1060 assert(link
->manager
);
1063 log_link_debug(link
, "Setting system timezone: '%s'", tz
);
1065 if (!link
->manager
->bus
) {
1066 log_link_info(link
, "Not connected to system bus, ignoring timezone.");
1070 r
= sd_bus_call_method_async(
1073 "org.freedesktop.timedate1",
1074 "/org/freedesktop/timedate1",
1075 "org.freedesktop.timedate1",
1077 set_timezone_handler
,
1083 return log_link_error_errno(link
, r
, "Could not set timezone: %m");
1090 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1091 _cleanup_link_unref_ Link
*link
= userdata
;
1096 assert(link
->ifname
);
1098 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1101 r
= sd_netlink_message_get_errno(m
);
1103 log_link_warning_errno(link
, r
, "Could not set MTU: %m");
1108 int link_set_mtu(Link
*link
, uint32_t mtu
) {
1109 _cleanup_netlink_message_unref_ sd_netlink_message
*req
= NULL
;
1113 assert(link
->manager
);
1114 assert(link
->manager
->rtnl
);
1116 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1118 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1120 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1122 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1124 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1126 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, set_mtu_handler
, link
, 0, NULL
);
1128 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1135 static int link_set_bridge(Link
*link
) {
1136 _cleanup_netlink_message_unref_ sd_netlink_message
*req
= NULL
;
1140 assert(link
->network
);
1142 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1144 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1146 r
= sd_rtnl_message_link_set_family(req
, PF_BRIDGE
);
1148 return log_link_error_errno(link
, r
, "Could not set message family: %m");
1150 r
= sd_netlink_message_open_container(req
, IFLA_PROTINFO
);
1152 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1154 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_GUARD
, !link
->network
->use_bpdu
);
1156 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_GUARD attribute: %m");
1158 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MODE
, link
->network
->hairpin
);
1160 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MODE attribute: %m");
1162 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_FAST_LEAVE
, link
->network
->fast_leave
);
1164 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_FAST_LEAVE attribute: %m");
1166 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_PROTECT
, !link
->network
->allow_port_to_be_root
);
1168 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PROTECT attribute: %m");
1170 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_UNICAST_FLOOD
, link
->network
->unicast_flood
);
1172 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_UNICAST_FLOOD attribute: %m");
1174 if(link
->network
->cost
!= 0) {
1175 r
= sd_netlink_message_append_u32(req
, IFLA_BRPORT_COST
, link
->network
->cost
);
1177 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_COST attribute: %m");
1180 r
= sd_netlink_message_close_container(req
);
1182 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1184 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_set_handler
, link
, 0, NULL
);
1186 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1193 static void lldp_handler(sd_lldp
*lldp
, int event
, void *userdata
) {
1194 Link
*link
= userdata
;
1198 assert(link
->network
);
1199 assert(link
->manager
);
1202 case SD_LLDP_EVENT_UPDATE_INFO
:
1203 r
= sd_lldp_save(link
->lldp
, link
->lldp_file
);
1205 log_link_warning_errno(link
, r
, "Could not save LLDP: %m");
1213 static int link_acquire_conf(Link
*link
) {
1217 assert(link
->network
);
1218 assert(link
->manager
);
1219 assert(link
->manager
->event
);
1221 if (link_ipv4ll_enabled(link
)) {
1222 assert(link
->ipv4ll
);
1224 log_link_debug(link
, "Acquiring IPv4 link-local address");
1226 r
= sd_ipv4ll_start(link
->ipv4ll
);
1228 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1231 if (link_dhcp4_enabled(link
)) {
1232 assert(link
->dhcp_client
);
1234 log_link_debug(link
, "Acquiring DHCPv4 lease");
1236 r
= sd_dhcp_client_start(link
->dhcp_client
);
1238 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1241 if (link_dhcp6_enabled(link
)) {
1242 assert(link
->ndisc_router_discovery
);
1244 log_link_debug(link
, "Discovering IPv6 routers");
1246 r
= sd_ndisc_router_discovery_start(link
->ndisc_router_discovery
);
1248 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1251 if (link_lldp_enabled(link
)) {
1254 log_link_debug(link
, "Starting LLDP");
1256 r
= sd_lldp_start(link
->lldp
);
1258 return log_link_warning_errno(link
, r
, "Could not start LLDP: %m");
1264 bool link_has_carrier(Link
*link
) {
1265 /* see Documentation/networking/operstates.txt in the kernel sources */
1267 if (link
->kernel_operstate
== IF_OPER_UP
)
1270 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1271 /* operstate may not be implemented, so fall back to flags */
1272 if ((link
->flags
& IFF_LOWER_UP
) && !(link
->flags
& IFF_DORMANT
))
1278 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1279 _cleanup_link_unref_ Link
*link
= userdata
;
1284 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1287 r
= sd_netlink_message_get_errno(m
);
1289 /* we warn but don't fail the link, as it may be
1291 log_link_warning_errno(link
, r
, "Could not bring up interface: %m");
1296 static int link_up(Link
*link
) {
1297 _cleanup_netlink_message_unref_ sd_netlink_message
*req
= NULL
;
1298 uint8_t ipv6ll_mode
;
1302 assert(link
->network
);
1303 assert(link
->manager
);
1304 assert(link
->manager
->rtnl
);
1306 log_link_debug(link
, "Bringing link up");
1308 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1310 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1312 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1314 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1316 if (link
->network
->mac
) {
1317 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
1319 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
1322 if (link
->network
->mtu
) {
1323 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, link
->network
->mtu
);
1325 return log_link_error_errno(link
, r
, "Could not set MTU: %m");
1328 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1330 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1332 if (socket_ipv6_is_supported()) {
1333 /* if the kernel lacks ipv6 support setting IFF_UP fails if any ipv6 options are passed */
1334 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1336 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1338 ipv6ll_mode
= link_ipv6ll_enabled(link
) ? IN6_ADDR_GEN_MODE_EUI64
: IN6_ADDR_GEN_MODE_NONE
;
1339 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1341 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1343 if (!in_addr_is_null(AF_INET6
, &link
->network
->ipv6_token
)) {
1344 r
= sd_netlink_message_append_in6_addr(req
, IFLA_INET6_TOKEN
, &link
->network
->ipv6_token
.in6
);
1346 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_TOKEN: %m");
1349 r
= sd_netlink_message_close_container(req
);
1351 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1354 r
= sd_netlink_message_close_container(req
);
1356 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1358 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_up_handler
, link
, 0, NULL
);
1360 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1367 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1368 _cleanup_link_unref_ Link
*link
= userdata
;
1373 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1376 r
= sd_netlink_message_get_errno(m
);
1378 log_link_warning_errno(link
, r
, "Could not bring down interface: %m");
1383 static int link_down(Link
*link
) {
1384 _cleanup_netlink_message_unref_ sd_netlink_message
*req
= NULL
;
1388 assert(link
->manager
);
1389 assert(link
->manager
->rtnl
);
1391 log_link_debug(link
, "Bringing link down");
1393 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
1394 RTM_SETLINK
, link
->ifindex
);
1396 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1398 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
1400 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1402 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_down_handler
, link
, 0, NULL
);
1404 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1411 static int link_handle_bound_to_list(Link
*link
) {
1415 bool required_up
= false;
1416 bool link_is_up
= false;
1420 if (hashmap_isempty(link
->bound_to_links
))
1423 if (link
->flags
& IFF_UP
)
1426 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
1427 if (link_has_carrier(l
)) {
1432 if (!required_up
&& link_is_up
) {
1433 r
= link_down(link
);
1436 } else if (required_up
&& !link_is_up
) {
1445 static int link_handle_bound_by_list(Link
*link
) {
1452 if (hashmap_isempty(link
->bound_by_links
))
1455 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
1456 r
= link_handle_bound_to_list(l
);
1464 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
1470 if (link
== carrier
)
1473 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
1476 r
= hashmap_ensure_allocated(h
, NULL
);
1480 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
1487 static int link_new_bound_by_list(Link
*link
) {
1492 bool list_updated
= false;
1495 assert(link
->manager
);
1499 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
1500 if (!carrier
->network
)
1503 if (strv_isempty(carrier
->network
->bind_carrier
))
1506 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
, 0)) {
1507 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
1511 list_updated
= true;
1518 HASHMAP_FOREACH (carrier
, link
->bound_by_links
, i
) {
1519 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
1523 link_dirty(carrier
);
1529 static int link_new_bound_to_list(Link
*link
) {
1534 bool list_updated
= false;
1537 assert(link
->manager
);
1542 if (strv_isempty(link
->network
->bind_carrier
))
1547 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
1548 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
, 0)) {
1549 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
1553 list_updated
= true;
1560 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
1561 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
1565 link_dirty(carrier
);
1571 static int link_new_carrier_maps(Link
*link
) {
1574 r
= link_new_bound_by_list(link
);
1578 r
= link_handle_bound_by_list(link
);
1582 r
= link_new_bound_to_list(link
);
1586 r
= link_handle_bound_to_list(link
);
1593 static void link_free_bound_to_list(Link
*link
) {
1597 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
1598 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
1600 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
1601 link_dirty(bound_to
);
1607 static void link_free_bound_by_list(Link
*link
) {
1611 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
1612 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
1614 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
1615 link_dirty(bound_by
);
1616 link_handle_bound_to_list(bound_by
);
1623 static void link_free_carrier_maps(Link
*link
) {
1624 bool list_updated
= false;
1628 if (!hashmap_isempty(link
->bound_to_links
)) {
1629 link_free_bound_to_list(link
);
1630 list_updated
= true;
1633 if (!hashmap_isempty(link
->bound_by_links
)) {
1634 link_free_bound_by_list(link
);
1635 list_updated
= true;
1644 void link_drop(Link
*link
) {
1645 if (!link
|| link
->state
== LINK_STATE_LINGER
)
1648 link_set_state(link
, LINK_STATE_LINGER
);
1650 link_free_carrier_maps(link
);
1652 log_link_debug(link
, "Link removed");
1654 (void)unlink(link
->state_file
);
1660 static int link_joined(Link
*link
) {
1664 assert(link
->network
);
1666 if (!hashmap_isempty(link
->bound_to_links
)) {
1667 r
= link_handle_bound_to_list(link
);
1670 } else if (!(link
->flags
& IFF_UP
)) {
1673 link_enter_failed(link
);
1678 if(link
->network
->bridge
) {
1679 r
= link_set_bridge(link
);
1681 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
1684 return link_enter_set_addresses(link
);
1687 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1688 _cleanup_link_unref_ Link
*link
= userdata
;
1692 assert(link
->network
);
1696 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1699 r
= sd_netlink_message_get_errno(m
);
1700 if (r
< 0 && r
!= -EEXIST
) {
1701 log_link_error_errno(link
, r
, "Could not join netdev: %m");
1702 link_enter_failed(link
);
1705 log_link_debug(link
, "Joined netdev");
1707 if (link
->enslaving
<= 0)
1713 static int link_enter_join_netdev(Link
*link
) {
1719 assert(link
->network
);
1720 assert(link
->state
== LINK_STATE_PENDING
);
1722 link_set_state(link
, LINK_STATE_ENSLAVING
);
1726 if (!link
->network
->bridge
&&
1727 !link
->network
->bond
&&
1728 hashmap_isempty(link
->network
->stacked_netdevs
))
1729 return link_joined(link
);
1731 if (link
->network
->bond
) {
1732 log_struct(LOG_DEBUG
,
1733 LOG_LINK_INTERFACE(link
),
1734 LOG_NETDEV_INTERFACE(link
->network
->bond
),
1735 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
),
1738 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
1740 log_struct_errno(LOG_WARNING
, r
,
1741 LOG_LINK_INTERFACE(link
),
1742 LOG_NETDEV_INTERFACE(link
->network
->bond
),
1743 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
),
1746 link_enter_failed(link
);
1753 if (link
->network
->bridge
) {
1754 log_struct(LOG_DEBUG
,
1755 LOG_LINK_INTERFACE(link
),
1756 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
1757 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
),
1760 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
1762 log_struct_errno(LOG_WARNING
, r
,
1763 LOG_LINK_INTERFACE(link
),
1764 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
1765 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
),
1767 link_enter_failed(link
);
1774 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
1776 log_struct(LOG_DEBUG
,
1777 LOG_LINK_INTERFACE(link
),
1778 LOG_NETDEV_INTERFACE(netdev
),
1779 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
),
1782 r
= netdev_join(netdev
, link
, netdev_join_handler
);
1784 log_struct_errno(LOG_WARNING
, r
,
1785 LOG_LINK_INTERFACE(link
),
1786 LOG_NETDEV_INTERFACE(netdev
),
1787 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
),
1789 link_enter_failed(link
);
1799 static int link_set_ipv4_forward(Link
*link
) {
1800 const char *p
= NULL
, *v
;
1803 if (link
->flags
& IFF_LOOPBACK
)
1806 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
1809 p
= strjoina("/proc/sys/net/ipv4/conf/", link
->ifname
, "/forwarding");
1810 v
= one_zero(link_ipv4_forward_enabled(link
));
1812 r
= write_string_file(p
, v
, 0);
1814 /* If the right value is set anyway, don't complain */
1815 if (verify_one_line_file(p
, v
) > 0)
1818 log_link_warning_errno(link
, r
, "Cannot configure IPv4 forwarding for interface %s: %m", link
->ifname
);
1824 static int link_set_ipv6_forward(Link
*link
) {
1825 const char *p
= NULL
, *v
= NULL
;
1828 /* Make this a NOP if IPv6 is not available */
1829 if (!socket_ipv6_is_supported())
1832 if (link
->flags
& IFF_LOOPBACK
)
1835 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
1838 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/forwarding");
1839 v
= one_zero(link_ipv6_forward_enabled(link
));
1841 r
= write_string_file(p
, v
, 0);
1843 /* If the right value is set anyway, don't complain */
1844 if (verify_one_line_file(p
, v
) > 0)
1847 log_link_warning_errno(link
, r
, "Cannot configure IPv6 forwarding for interface: %m");
1853 static int link_set_ipv6_privacy_extensions(Link
*link
) {
1854 char buf
[DECIMAL_STR_MAX(unsigned) + 1];
1855 IPv6PrivacyExtensions s
;
1856 const char *p
= NULL
;
1859 /* Make this a NOP if IPv6 is not available */
1860 if (!socket_ipv6_is_supported())
1863 s
= link_ipv6_privacy_extensions(link
);
1864 if (s
== _IPV6_PRIVACY_EXTENSIONS_INVALID
)
1867 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/use_tempaddr");
1868 xsprintf(buf
, "%u", link
->network
->ipv6_privacy_extensions
);
1870 r
= write_string_file(p
, buf
, 0);
1872 /* If the right value is set anyway, don't complain */
1873 if (verify_one_line_file(p
, buf
) > 0)
1876 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
1882 static int link_set_ipv6_accept_ra(Link
*link
) {
1883 const char *p
= NULL
, *v
= NULL
;
1886 /* Make this a NOP if IPv6 is not available */
1887 if (!socket_ipv6_is_supported())
1890 if (link
->flags
& IFF_LOOPBACK
)
1893 /* If unset use system default (enabled if local forwarding is disabled.
1894 * disabled if local forwarding is enabled).
1895 * If set, ignore or enforce RA independent of local forwarding state.
1897 if (link
->network
->ipv6_accept_ra
< 0)
1898 /* default to accept RA if ip_forward is disabled and ignore RA if ip_forward is enabled */
1900 else if (link
->network
->ipv6_accept_ra
> 0)
1901 /* "2" means accept RA even if ip_forward is enabled */
1904 /* "0" means ignore RA */
1907 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/accept_ra");
1909 r
= write_string_file(p
, v
, 0);
1911 /* If the right value is set anyway, don't complain */
1912 if (verify_one_line_file(p
, v
) > 0)
1915 log_link_warning_errno(link
, r
, "Cannot configure IPv6 accept_ra for interface: %m");
1921 static int link_set_ipv6_dad_transmits(Link
*link
) {
1922 char buf
[DECIMAL_STR_MAX(unsigned) + 1];
1923 const char *p
= NULL
;
1926 /* Make this a NOP if IPv6 is not available */
1927 if (!socket_ipv6_is_supported())
1930 if (link
->flags
& IFF_LOOPBACK
)
1933 if (link
->network
->ipv6_dad_transmits
< 0)
1936 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/dad_transmits");
1938 xsprintf(buf
, "%u", link
->network
->ipv6_dad_transmits
);
1940 r
= write_string_file(p
, buf
, 0);
1942 /* If the right value is set anyway, don't complain */
1943 if (verify_one_line_file(p
, buf
) > 0)
1946 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
1952 static int link_configure(Link
*link
) {
1956 assert(link
->network
);
1957 assert(link
->state
== LINK_STATE_PENDING
);
1959 r
= link_set_bridge_fdb(link
);
1963 r
= link_set_ipv4_forward(link
);
1967 r
= link_set_ipv6_forward(link
);
1971 r
= link_set_ipv6_privacy_extensions(link
);
1975 r
= link_set_ipv6_accept_ra(link
);
1979 r
= link_set_ipv6_dad_transmits(link
);
1983 if (link_ipv4ll_enabled(link
)) {
1984 r
= ipv4ll_configure(link
);
1989 if (link_dhcp4_enabled(link
)) {
1990 r
= dhcp4_configure(link
);
1995 if (link_dhcp4_server_enabled(link
)) {
1996 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
2000 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
2005 if (link_dhcp6_enabled(link
)) {
2006 r
= ndisc_configure(link
);
2011 if (link_lldp_enabled(link
)) {
2012 r
= sd_lldp_new(link
->ifindex
, link
->ifname
, &link
->mac
, &link
->lldp
);
2016 r
= sd_lldp_attach_event(link
->lldp
, NULL
, 0);
2020 r
= sd_lldp_set_callback(link
->lldp
,
2021 lldp_handler
, link
);
2026 if (link_has_carrier(link
)) {
2027 r
= link_acquire_conf(link
);
2032 return link_enter_join_netdev(link
);
2035 static int link_initialized_and_synced(sd_netlink
*rtnl
, sd_netlink_message
*m
,
2037 _cleanup_link_unref_ Link
*link
= userdata
;
2042 assert(link
->ifname
);
2043 assert(link
->manager
);
2045 if (link
->state
!= LINK_STATE_PENDING
)
2048 log_link_debug(link
, "Link state is up-to-date");
2050 r
= link_new_bound_by_list(link
);
2054 r
= link_handle_bound_by_list(link
);
2058 r
= network_get(link
->manager
, link
->udev_device
, link
->ifname
,
2059 &link
->mac
, &network
);
2061 link_enter_unmanaged(link
);
2066 if (link
->flags
& IFF_LOOPBACK
) {
2067 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
2068 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
2070 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
2071 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
2073 if (network
->dhcp_server
)
2074 log_link_debug(link
, "Ignoring DHCP server for loopback link");
2077 r
= network_apply(link
->manager
, network
, link
);
2081 r
= link_new_bound_to_list(link
);
2085 r
= link_configure(link
);
2092 int link_initialized(Link
*link
, struct udev_device
*device
) {
2093 _cleanup_netlink_message_unref_ sd_netlink_message
*req
= NULL
;
2097 assert(link
->manager
);
2098 assert(link
->manager
->rtnl
);
2101 if (link
->state
!= LINK_STATE_PENDING
)
2104 if (link
->udev_device
)
2107 log_link_debug(link
, "udev initialized link");
2109 link
->udev_device
= udev_device_ref(device
);
2111 /* udev has initialized the link, but we don't know if we have yet
2112 * processed the NEWLINK messages with the latest state. Do a GETLINK,
2113 * when it returns we know that the pending NEWLINKs have already been
2114 * processed and that we are up-to-date */
2116 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
2121 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
,
2122 link_initialized_and_synced
, link
, 0, NULL
);
2131 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
2133 _cleanup_udev_device_unref_
struct udev_device
*device
= NULL
;
2134 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
2142 r
= link_new(m
, message
, ret
);
2148 log_link_debug(link
, "Link %d added", link
->ifindex
);
2150 if (detect_container() <= 0) {
2151 /* not in a container, udev will be around */
2152 sprintf(ifindex_str
, "n%d", link
->ifindex
);
2153 device
= udev_device_new_from_device_id(m
->udev
, ifindex_str
);
2155 r
= log_link_warning_errno(link
, errno
, "Could not find udev device: %m");
2159 if (udev_device_get_is_initialized(device
) <= 0) {
2161 log_link_debug(link
, "link pending udev initialization...");
2165 r
= link_initialized(link
, device
);
2169 /* we are calling a callback directly, so must take a ref */
2172 r
= link_initialized_and_synced(m
->rtnl
, NULL
, link
);
2179 link_enter_failed(link
);
2183 static int link_carrier_gained(Link
*link
) {
2188 if (link
->network
) {
2189 r
= link_acquire_conf(link
);
2191 link_enter_failed(link
);
2196 r
= link_handle_bound_by_list(link
);
2203 static int link_carrier_lost(Link
*link
) {
2208 r
= link_stop_clients(link
);
2210 link_enter_failed(link
);
2214 r
= link_handle_bound_by_list(link
);
2221 int link_carrier_reset(Link
*link
) {
2226 if (link_has_carrier(link
)) {
2227 r
= link_carrier_lost(link
);
2231 r
= link_carrier_gained(link
);
2235 log_link_info(link
, "Reset carrier");
2242 int link_update(Link
*link
, sd_netlink_message
*m
) {
2243 struct ether_addr mac
;
2246 bool had_carrier
, carrier_gained
, carrier_lost
;
2250 assert(link
->ifname
);
2253 if (link
->state
== LINK_STATE_LINGER
) {
2255 log_link_info(link
, "Link readded");
2256 link_set_state(link
, LINK_STATE_ENSLAVING
);
2258 r
= link_new_carrier_maps(link
);
2263 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
2264 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
2265 log_link_info(link
, "Renamed to %s", ifname
);
2267 link_free_carrier_maps(link
);
2269 r
= free_and_strdup(&link
->ifname
, ifname
);
2273 r
= link_new_carrier_maps(link
);
2278 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
2279 if (r
>= 0 && mtu
> 0) {
2281 if (!link
->original_mtu
) {
2282 link
->original_mtu
= mtu
;
2283 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
2286 if (link
->dhcp_client
) {
2287 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
2290 log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
2296 /* The kernel may broadcast NEWLINK messages without the MAC address
2297 set, simply ignore them. */
2298 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
2300 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
2303 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
2306 log_link_debug(link
, "MAC address: "
2307 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
2308 mac
.ether_addr_octet
[0],
2309 mac
.ether_addr_octet
[1],
2310 mac
.ether_addr_octet
[2],
2311 mac
.ether_addr_octet
[3],
2312 mac
.ether_addr_octet
[4],
2313 mac
.ether_addr_octet
[5]);
2316 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
2318 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
2321 if (link
->dhcp_client
) {
2322 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
2323 (const uint8_t *) &link
->mac
,
2327 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
2330 if (link
->dhcp6_client
) {
2331 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
2332 (const uint8_t *) &link
->mac
,
2336 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
2341 had_carrier
= link_has_carrier(link
);
2343 r
= link_update_flags(link
, m
);
2347 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
2348 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
2350 if (carrier_gained
) {
2351 log_link_info(link
, "Gained carrier");
2353 r
= link_carrier_gained(link
);
2356 } else if (carrier_lost
) {
2357 log_link_info(link
, "Lost carrier");
2359 r
= link_carrier_lost(link
);
2368 int link_save(Link
*link
) {
2369 _cleanup_free_
char *temp_path
= NULL
;
2370 _cleanup_fclose_
FILE *f
= NULL
;
2371 const char *admin_state
, *oper_state
;
2377 assert(link
->state_file
);
2378 assert(link
->lease_file
);
2379 assert(link
->manager
);
2381 if (link
->state
== LINK_STATE_LINGER
) {
2382 unlink(link
->state_file
);
2386 admin_state
= link_state_to_string(link
->state
);
2387 assert(admin_state
);
2389 oper_state
= link_operstate_to_string(link
->operstate
);
2392 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
2396 fchmod(fileno(f
), 0644);
2399 "# This is private data. Do not parse.\n"
2402 admin_state
, oper_state
);
2404 if (link
->network
) {
2405 char **address
, **domain
;
2407 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
2409 if (link
->dhcp6_client
) {
2410 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
,
2413 log_link_debug(link
, "No DHCPv6 lease");
2416 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
2420 STRV_FOREACH(address
, link
->network
->dns
) {
2427 if (link
->network
->dhcp_dns
&&
2429 const struct in_addr
*addresses
;
2431 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
2435 serialize_in_addrs(f
, addresses
, r
);
2440 if (link
->network
->dhcp_dns
&& dhcp6_lease
) {
2441 struct in6_addr
*in6_addrs
;
2443 r
= sd_dhcp6_lease_get_dns(dhcp6_lease
, &in6_addrs
);
2447 serialize_in6_addrs(f
, in6_addrs
, r
);
2455 STRV_FOREACH(address
, link
->network
->ntp
) {
2462 if (link
->network
->dhcp_ntp
&&
2464 const struct in_addr
*addresses
;
2466 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
2470 serialize_in_addrs(f
, addresses
, r
);
2475 if (link
->network
->dhcp_ntp
&& dhcp6_lease
) {
2476 struct in6_addr
*in6_addrs
;
2480 r
= sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease
,
2485 serialize_in6_addrs(f
, in6_addrs
, r
);
2489 r
= sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease
, &hosts
);
2491 STRV_FOREACH(hostname
, hosts
) {
2494 fputs(*hostname
, f
);
2502 fprintf(f
, "DOMAINS=");
2504 STRV_FOREACH(domain
, link
->network
->domains
) {
2511 if (link
->network
->dhcp_domains
&&
2513 const char *domainname
;
2515 r
= sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &domainname
);
2519 fputs(domainname
, f
);
2524 if (link
->network
->dhcp_domains
&& dhcp6_lease
) {
2527 r
= sd_dhcp6_lease_get_domains(dhcp6_lease
, &domains
);
2529 STRV_FOREACH(domain
, domains
) {
2540 fprintf(f
, "WILDCARD_DOMAIN=%s\n",
2541 yes_no(link
->network
->wildcard_domain
));
2543 fprintf(f
, "LLMNR=%s\n",
2544 resolve_support_to_string(link
->network
->llmnr
));
2546 fprintf(f
, "ADDRESSES=");
2548 SET_FOREACH(a
, link
->addresses
, i
) {
2549 _cleanup_free_
char *address_str
= NULL
;
2551 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
2555 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
2562 if (!hashmap_isempty(link
->bound_to_links
)) {
2566 fputs("CARRIER_BOUND_TO=", f
);
2567 HASHMAP_FOREACH(carrier
, link
->bound_to_links
, i
) {
2570 fputs(carrier
->ifname
, f
);
2577 if (!hashmap_isempty(link
->bound_by_links
)) {
2581 fputs("CARRIER_BOUND_BY=", f
);
2582 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
2585 fputs(carrier
->ifname
, f
);
2592 if (link
->dhcp_lease
) {
2593 const char *tz
= NULL
;
2595 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
2597 fprintf(f
, "TIMEZONE=%s\n", tz
);
2600 if (link
->dhcp_lease
) {
2601 assert(link
->network
);
2603 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
2611 unlink(link
->lease_file
);
2614 assert(link
->network
);
2616 r
= sd_lldp_save(link
->lldp
, link
->lldp_file
);
2624 unlink(link
->lldp_file
);
2626 r
= fflush_and_check(f
);
2630 if (rename(temp_path
, link
->state_file
) < 0) {
2638 (void) unlink(link
->state_file
);
2640 (void) unlink(temp_path
);
2642 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
2645 /* The serialized state in /run is no longer up-to-date. */
2646 void link_dirty(Link
*link
) {
2651 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
2653 /* allocation errors are ignored */
2656 r
= set_put(link
->manager
->dirty_links
, link
);
2658 /* allocation errors are ignored */
2664 /* The serialized state in /run is up-to-date */
2665 void link_clean(Link
*link
) {
2667 assert(link
->manager
);
2669 set_remove(link
->manager
->dirty_links
, link
);
2673 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
2674 [LINK_STATE_PENDING
] = "pending",
2675 [LINK_STATE_ENSLAVING
] = "configuring",
2676 [LINK_STATE_SETTING_ADDRESSES
] = "configuring",
2677 [LINK_STATE_SETTING_ROUTES
] = "configuring",
2678 [LINK_STATE_CONFIGURED
] = "configured",
2679 [LINK_STATE_UNMANAGED
] = "unmanaged",
2680 [LINK_STATE_FAILED
] = "failed",
2681 [LINK_STATE_LINGER
] = "linger",
2684 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);
2686 static const char* const link_operstate_table
[_LINK_OPERSTATE_MAX
] = {
2687 [LINK_OPERSTATE_OFF
] = "off",
2688 [LINK_OPERSTATE_NO_CARRIER
] = "no-carrier",
2689 [LINK_OPERSTATE_DORMANT
] = "dormant",
2690 [LINK_OPERSTATE_CARRIER
] = "carrier",
2691 [LINK_OPERSTATE_DEGRADED
] = "degraded",
2692 [LINK_OPERSTATE_ROUTABLE
] = "routable",
2695 DEFINE_STRING_TABLE_LOOKUP(link_operstate
, LinkOperationalState
);