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 "stdio-util.h"
37 #include "string-table.h"
38 #include "udev-util.h"
42 bool link_dhcp6_enabled(Link
*link
) {
43 if (link
->flags
& IFF_LOOPBACK
)
49 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV6
;
52 bool link_dhcp4_enabled(Link
*link
) {
53 if (link
->flags
& IFF_LOOPBACK
)
59 return link
->network
->dhcp
& ADDRESS_FAMILY_IPV4
;
62 bool link_dhcp4_server_enabled(Link
*link
) {
63 if (link
->flags
& IFF_LOOPBACK
)
69 return link
->network
->dhcp_server
;
72 bool link_ipv4ll_enabled(Link
*link
) {
73 if (link
->flags
& IFF_LOOPBACK
)
79 return link
->network
->link_local
& ADDRESS_FAMILY_IPV4
;
82 bool link_ipv6ll_enabled(Link
*link
) {
83 if (link
->flags
& IFF_LOOPBACK
)
89 return link
->network
->link_local
& ADDRESS_FAMILY_IPV6
;
92 bool link_lldp_enabled(Link
*link
) {
93 if (link
->flags
& IFF_LOOPBACK
)
99 if (link
->network
->bridge
)
102 return link
->network
->lldp
;
105 static bool link_ipv4_forward_enabled(Link
*link
) {
106 if (link
->flags
& IFF_LOOPBACK
)
112 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV4
;
115 static bool link_ipv6_forward_enabled(Link
*link
) {
116 if (link
->flags
& IFF_LOOPBACK
)
122 return link
->network
->ip_forward
& ADDRESS_FAMILY_IPV6
;
125 static IPv6PrivacyExtensions
link_ipv6_privacy_extensions(Link
*link
) {
126 if (link
->flags
& IFF_LOOPBACK
)
127 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
130 return _IPV6_PRIVACY_EXTENSIONS_INVALID
;
132 return link
->network
->ipv6_privacy_extensions
;
135 void link_update_operstate(Link
*link
) {
136 LinkOperationalState operstate
;
139 if (link
->kernel_operstate
== IF_OPER_DORMANT
)
140 operstate
= LINK_OPERSTATE_DORMANT
;
141 else if (link_has_carrier(link
)) {
143 uint8_t scope
= RT_SCOPE_NOWHERE
;
146 /* if we have carrier, check what addresses we have */
147 SET_FOREACH(address
, link
->addresses
, i
) {
148 if (!address_is_ready(address
))
151 if (address
->scope
< scope
)
152 scope
= address
->scope
;
155 /* for operstate we also take foreign addresses into account */
156 SET_FOREACH(address
, link
->addresses_foreign
, i
) {
157 if (!address_is_ready(address
))
160 if (address
->scope
< scope
)
161 scope
= address
->scope
;
164 if (scope
< RT_SCOPE_SITE
)
165 /* universally accessible addresses found */
166 operstate
= LINK_OPERSTATE_ROUTABLE
;
167 else if (scope
< RT_SCOPE_HOST
)
168 /* only link or site local addresses found */
169 operstate
= LINK_OPERSTATE_DEGRADED
;
171 /* no useful addresses found */
172 operstate
= LINK_OPERSTATE_CARRIER
;
173 } else if (link
->flags
& IFF_UP
)
174 operstate
= LINK_OPERSTATE_NO_CARRIER
;
176 operstate
= LINK_OPERSTATE_OFF
;
178 if (link
->operstate
!= operstate
) {
179 link
->operstate
= operstate
;
180 link_send_changed(link
, "OperationalState", NULL
);
182 manager_dirty(link
->manager
);
186 #define FLAG_STRING(string, flag, old, new) \
187 (((old ^ new) & flag) \
188 ? ((old & flag) ? (" -" string) : (" +" string)) \
191 static int link_update_flags(Link
*link
, sd_netlink_message
*m
) {
192 unsigned flags
, unknown_flags_added
, unknown_flags_removed
, unknown_flags
;
198 r
= sd_rtnl_message_link_get_flags(m
, &flags
);
200 return log_link_warning_errno(link
, r
, "Could not get link flags: %m");
202 r
= sd_netlink_message_read_u8(m
, IFLA_OPERSTATE
, &operstate
);
204 /* if we got a message without operstate, take it to mean
205 the state was unchanged */
206 operstate
= link
->kernel_operstate
;
208 if ((link
->flags
== flags
) && (link
->kernel_operstate
== operstate
))
211 if (link
->flags
!= flags
) {
212 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",
213 FLAG_STRING("LOOPBACK", IFF_LOOPBACK
, link
->flags
, flags
),
214 FLAG_STRING("MASTER", IFF_MASTER
, link
->flags
, flags
),
215 FLAG_STRING("SLAVE", IFF_SLAVE
, link
->flags
, flags
),
216 FLAG_STRING("UP", IFF_UP
, link
->flags
, flags
),
217 FLAG_STRING("DORMANT", IFF_DORMANT
, link
->flags
, flags
),
218 FLAG_STRING("LOWER_UP", IFF_LOWER_UP
, link
->flags
, flags
),
219 FLAG_STRING("RUNNING", IFF_RUNNING
, link
->flags
, flags
),
220 FLAG_STRING("MULTICAST", IFF_MULTICAST
, link
->flags
, flags
),
221 FLAG_STRING("BROADCAST", IFF_BROADCAST
, link
->flags
, flags
),
222 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT
, link
->flags
, flags
),
223 FLAG_STRING("PROMISC", IFF_PROMISC
, link
->flags
, flags
),
224 FLAG_STRING("ALLMULTI", IFF_ALLMULTI
, link
->flags
, flags
),
225 FLAG_STRING("PORTSEL", IFF_PORTSEL
, link
->flags
, flags
),
226 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA
, link
->flags
, flags
),
227 FLAG_STRING("DYNAMIC", IFF_DYNAMIC
, link
->flags
, flags
),
228 FLAG_STRING("NOARP", IFF_NOARP
, link
->flags
, flags
),
229 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS
, link
->flags
, flags
),
230 FLAG_STRING("DEBUG", IFF_DEBUG
, link
->flags
, flags
),
231 FLAG_STRING("ECHO", IFF_ECHO
, link
->flags
, flags
));
233 unknown_flags
= ~(IFF_LOOPBACK
| IFF_MASTER
| IFF_SLAVE
| IFF_UP
|
234 IFF_DORMANT
| IFF_LOWER_UP
| IFF_RUNNING
|
235 IFF_MULTICAST
| IFF_BROADCAST
| IFF_POINTOPOINT
|
236 IFF_PROMISC
| IFF_ALLMULTI
| IFF_PORTSEL
|
237 IFF_AUTOMEDIA
| IFF_DYNAMIC
| IFF_NOARP
|
238 IFF_NOTRAILERS
| IFF_DEBUG
| IFF_ECHO
);
239 unknown_flags_added
= ((link
->flags
^ flags
) & flags
& unknown_flags
);
240 unknown_flags_removed
= ((link
->flags
^ flags
) & link
->flags
& unknown_flags
);
242 /* link flags are currently at most 18 bits, let's align to
244 if (unknown_flags_added
)
246 "Unknown link flags gained: %#.5x (ignoring)",
247 unknown_flags_added
);
249 if (unknown_flags_removed
)
251 "Unknown link flags lost: %#.5x (ignoring)",
252 unknown_flags_removed
);
256 link
->kernel_operstate
= operstate
;
258 link_update_operstate(link
);
263 static int link_new(Manager
*manager
, sd_netlink_message
*message
, Link
**ret
) {
264 _cleanup_link_unref_ Link
*link
= NULL
;
273 r
= sd_netlink_message_get_type(message
, &type
);
276 else if (type
!= RTM_NEWLINK
)
279 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
282 else if (ifindex
<= 0)
285 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &ifname
);
289 link
= new0(Link
, 1);
294 link
->manager
= manager
;
295 link
->state
= LINK_STATE_PENDING
;
296 link
->rtnl_extended_attrs
= true;
297 link
->ifindex
= ifindex
;
298 link
->ifname
= strdup(ifname
);
302 r
= sd_netlink_message_read_ether_addr(message
, IFLA_ADDRESS
, &link
->mac
);
304 log_link_debug(link
, "MAC address not found for new device, continuing without");
306 r
= asprintf(&link
->state_file
, "/run/systemd/netif/links/%d",
311 r
= asprintf(&link
->lease_file
, "/run/systemd/netif/leases/%d",
316 r
= asprintf(&link
->lldp_file
, "/run/systemd/netif/lldp/%d",
322 r
= hashmap_ensure_allocated(&manager
->links
, NULL
);
326 r
= hashmap_put(manager
->links
, INT_TO_PTR(link
->ifindex
), link
);
330 r
= link_update_flags(link
, message
);
340 static void link_free(Link
*link
) {
348 while (!set_isempty(link
->addresses
))
349 address_free(set_first(link
->addresses
));
351 set_free(link
->addresses
);
353 while (!set_isempty(link
->addresses_foreign
))
354 address_free(set_first(link
->addresses_foreign
));
356 set_free(link
->addresses_foreign
);
358 while ((address
= link
->pool_addresses
)) {
359 LIST_REMOVE(addresses
, link
->pool_addresses
, address
);
360 address_free(address
);
363 sd_dhcp_server_unref(link
->dhcp_server
);
364 sd_dhcp_client_unref(link
->dhcp_client
);
365 sd_dhcp_lease_unref(link
->dhcp_lease
);
367 free(link
->lease_file
);
369 sd_lldp_free(link
->lldp
);
371 free(link
->lldp_file
);
373 sd_ipv4ll_unref(link
->ipv4ll
);
374 sd_dhcp6_client_unref(link
->dhcp6_client
);
375 sd_ndisc_unref(link
->ndisc_router_discovery
);
378 hashmap_remove(link
->manager
->links
, INT_TO_PTR(link
->ifindex
));
382 (void)unlink(link
->state_file
);
383 free(link
->state_file
);
385 udev_device_unref(link
->udev_device
);
387 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
)
388 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(carrier
->ifindex
));
389 hashmap_free(link
->bound_to_links
);
391 HASHMAP_FOREACH (carrier
, link
->bound_by_links
, i
)
392 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(carrier
->ifindex
));
393 hashmap_free(link
->bound_by_links
);
398 Link
*link_unref(Link
*link
) {
402 assert(link
->n_ref
> 0);
414 Link
*link_ref(Link
*link
) {
418 assert(link
->n_ref
> 0);
425 int link_get(Manager
*m
, int ifindex
, Link
**ret
) {
432 link
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
441 static void link_set_state(Link
*link
, LinkState state
) {
444 if (link
->state
== state
)
449 link_send_changed(link
, "AdministrativeState", NULL
);
454 static void link_enter_unmanaged(Link
*link
) {
457 log_link_debug(link
, "Unmanaged");
459 link_set_state(link
, LINK_STATE_UNMANAGED
);
464 static int link_stop_clients(Link
*link
) {
468 assert(link
->manager
);
469 assert(link
->manager
->event
);
474 if (link
->dhcp_client
) {
475 k
= sd_dhcp_client_stop(link
->dhcp_client
);
477 r
= log_link_warning_errno(link
, r
, "Could not stop DHCPv4 client: %m");
481 k
= sd_ipv4ll_stop(link
->ipv4ll
);
483 r
= log_link_warning_errno(link
, r
, "Could not stop IPv4 link-local: %m");
486 if(link
->ndisc_router_discovery
) {
487 if (link
->dhcp6_client
) {
488 k
= sd_dhcp6_client_stop(link
->dhcp6_client
);
490 r
= log_link_warning_errno(link
, r
, "Could not stop DHCPv6 client: %m");
493 k
= sd_ndisc_stop(link
->ndisc_router_discovery
);
495 r
= log_link_warning_errno(link
, r
, "Could not stop IPv6 Router Discovery: %m");
499 k
= sd_lldp_stop(link
->lldp
);
501 r
= log_link_warning_errno(link
, r
, "Could not stop LLDP: %m");
507 void link_enter_failed(Link
*link
) {
510 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
513 log_link_warning(link
, "Failed");
515 link_set_state(link
, LINK_STATE_FAILED
);
517 link_stop_clients(link
);
522 static Address
* link_find_dhcp_server_address(Link
*link
) {
526 assert(link
->network
);
528 /* The first statically configured address if there is any */
529 LIST_FOREACH(addresses
, address
, link
->network
->static_addresses
) {
531 if (address
->family
!= AF_INET
)
534 if (in_addr_is_null(address
->family
, &address
->in_addr
))
540 /* If that didn't work, find a suitable address we got from the pool */
541 LIST_FOREACH(addresses
, address
, link
->pool_addresses
) {
542 if (address
->family
!= AF_INET
)
551 static int link_enter_configured(Link
*link
) {
553 assert(link
->network
);
554 assert(link
->state
== LINK_STATE_SETTING_ROUTES
);
556 log_link_info(link
, "Configured");
558 link_set_state(link
, LINK_STATE_CONFIGURED
);
565 void link_check_ready(Link
*link
) {
574 if (!link
->static_configured
)
577 if (link_ipv4ll_enabled(link
))
578 if (!link
->ipv4ll_address
||
582 if ((link_dhcp4_enabled(link
) && !link_dhcp6_enabled(link
) &&
583 !link
->dhcp4_configured
) ||
584 (link_dhcp6_enabled(link
) && !link_dhcp4_enabled(link
) &&
585 !link
->dhcp6_configured
) ||
586 (link_dhcp4_enabled(link
) && link_dhcp6_enabled(link
) &&
587 !link
->dhcp4_configured
&& !link
->dhcp6_configured
))
590 SET_FOREACH(a
, link
->addresses
, i
)
591 if (!address_is_ready(a
))
594 if (link
->state
!= LINK_STATE_CONFIGURED
)
595 link_enter_configured(link
);
600 static int route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
601 _cleanup_link_unref_ Link
*link
= userdata
;
604 assert(link
->link_messages
> 0);
605 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
606 LINK_STATE_SETTING_ROUTES
, LINK_STATE_FAILED
,
609 link
->link_messages
--;
611 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
614 r
= sd_netlink_message_get_errno(m
);
615 if (r
< 0 && r
!= -EEXIST
)
616 log_link_warning_errno(link
, r
, "Could not set route: %m");
618 if (link
->link_messages
== 0) {
619 log_link_debug(link
, "Routes set");
620 link
->static_configured
= true;
621 link_check_ready(link
);
627 static int link_enter_set_routes(Link
*link
) {
632 assert(link
->network
);
633 assert(link
->state
== LINK_STATE_SETTING_ADDRESSES
);
635 link_set_state(link
, LINK_STATE_SETTING_ROUTES
);
637 LIST_FOREACH(routes
, rt
, link
->network
->static_routes
) {
638 r
= route_configure(rt
, link
, &route_handler
);
640 log_link_warning_errno(link
, r
, "Could not set routes: %m");
641 link_enter_failed(link
);
645 link
->link_messages
++;
648 if (link
->link_messages
== 0) {
649 link
->static_configured
= true;
650 link_check_ready(link
);
652 log_link_debug(link
, "Setting routes");
657 int link_route_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
658 _cleanup_link_unref_ Link
*link
= userdata
;
663 assert(link
->ifname
);
665 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
668 r
= sd_netlink_message_get_errno(m
);
669 if (r
< 0 && r
!= -ESRCH
)
670 log_link_warning_errno(link
, r
, "Could not drop route: %m");
675 static int address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
676 _cleanup_link_unref_ Link
*link
= userdata
;
682 assert(link
->ifname
);
683 assert(link
->link_messages
> 0);
684 assert(IN_SET(link
->state
, LINK_STATE_SETTING_ADDRESSES
,
685 LINK_STATE_FAILED
, LINK_STATE_LINGER
));
687 link
->link_messages
--;
689 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
692 r
= sd_netlink_message_get_errno(m
);
693 if (r
< 0 && r
!= -EEXIST
)
694 log_link_warning_errno(link
, r
, "could not set address: %m");
696 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
698 if (link
->link_messages
== 0) {
699 log_link_debug(link
, "Addresses set");
700 link_enter_set_routes(link
);
706 static int link_push_dns_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
707 _cleanup_free_
struct in_addr
*addresses
= NULL
;
708 size_t n_addresses
= 0, n_allocated
= 0;
711 log_debug("Copying DNS server information from %s", link
->ifname
);
716 STRV_FOREACH(a
, link
->network
->dns
) {
719 /* Only look for IPv4 addresses */
720 if (inet_pton(AF_INET
, *a
, &ia
) <= 0)
723 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
726 addresses
[n_addresses
++] = ia
;
729 if (link
->network
->dhcp_dns
&&
731 const struct in_addr
*da
= NULL
;
734 n
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &da
);
737 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
740 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
745 if (n_addresses
<= 0)
748 return sd_dhcp_server_set_dns(s
, addresses
, n_addresses
);
751 static int link_push_ntp_to_dhcp_server(Link
*link
, sd_dhcp_server
*s
) {
752 _cleanup_free_
struct in_addr
*addresses
= NULL
;
753 size_t n_addresses
= 0, n_allocated
= 0;
759 log_debug("Copying NTP server information from %s", link
->ifname
);
761 STRV_FOREACH(a
, link
->network
->ntp
) {
764 /* Only look for IPv4 addresses */
765 if (inet_pton(AF_INET
, *a
, &ia
) <= 0)
768 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
771 addresses
[n_addresses
++] = ia
;
774 if (link
->network
->dhcp_ntp
&&
776 const struct in_addr
*da
= NULL
;
779 n
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &da
);
782 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
785 memcpy(addresses
+ n_addresses
, da
, n
* sizeof(struct in_addr
));
790 if (n_addresses
<= 0)
793 return sd_dhcp_server_set_ntp(s
, addresses
, n_addresses
);
796 static int link_enter_set_addresses(Link
*link
) {
801 assert(link
->network
);
802 assert(link
->state
!= _LINK_STATE_INVALID
);
804 link_set_state(link
, LINK_STATE_SETTING_ADDRESSES
);
806 LIST_FOREACH(addresses
, ad
, link
->network
->static_addresses
) {
807 r
= address_configure(ad
, link
, &address_handler
, false);
809 log_link_warning_errno(link
, r
, "Could not set addresses: %m");
810 link_enter_failed(link
);
814 link
->link_messages
++;
817 /* now that we can figure out a default address for the dhcp server,
819 if (link_dhcp4_server_enabled(link
)) {
822 bool acquired_uplink
= false;
824 address
= link_find_dhcp_server_address(link
);
826 log_link_warning(link
, "Failed to find suitable address for DHCPv4 server instance.");
827 link_enter_failed(link
);
831 /* use the server address' subnet as the pool */
832 r
= sd_dhcp_server_configure_pool(link
->dhcp_server
, &address
->in_addr
.in
, address
->prefixlen
,
833 link
->network
->dhcp_server_pool_offset
, link
->network
->dhcp_server_pool_size
);
838 r = sd_dhcp_server_set_router(link->dhcp_server,
839 &main_address->in_addr.in);
844 if (link
->network
->dhcp_server_max_lease_time_usec
> 0) {
845 r
= sd_dhcp_server_set_max_lease_time(
847 DIV_ROUND_UP(link
->network
->dhcp_server_max_lease_time_usec
, USEC_PER_SEC
));
852 if (link
->network
->dhcp_server_default_lease_time_usec
> 0) {
853 r
= sd_dhcp_server_set_default_lease_time(
855 DIV_ROUND_UP(link
->network
->dhcp_server_default_lease_time_usec
, USEC_PER_SEC
));
860 if (link
->network
->dhcp_server_emit_dns
) {
862 if (link
->network
->n_dhcp_server_dns
> 0)
863 r
= sd_dhcp_server_set_dns(link
->dhcp_server
, link
->network
->dhcp_server_dns
, link
->network
->n_dhcp_server_dns
);
865 uplink
= manager_find_uplink(link
->manager
, link
);
866 acquired_uplink
= true;
869 log_link_debug(link
, "Not emitting DNS server information on link, couldn't find suitable uplink.");
872 r
= link_push_dns_to_dhcp_server(uplink
, link
->dhcp_server
);
875 log_link_warning_errno(link
, r
, "Failed to set DNS server for DHCP server, ignoring: %m");
879 if (link
->network
->dhcp_server_emit_ntp
) {
881 if (link
->network
->n_dhcp_server_ntp
> 0)
882 r
= sd_dhcp_server_set_ntp(link
->dhcp_server
, link
->network
->dhcp_server_ntp
, link
->network
->n_dhcp_server_ntp
);
884 if (!acquired_uplink
)
885 uplink
= manager_find_uplink(link
->manager
, link
);
888 log_link_debug(link
, "Not emitting NTP server information on link, couldn't find suitable uplink.");
891 r
= link_push_ntp_to_dhcp_server(uplink
, link
->dhcp_server
);
895 log_link_warning_errno(link
, r
, "Failed to set NTP server for DHCP server, ignoring: %m");
898 if (link
->network
->dhcp_server_emit_timezone
) {
899 _cleanup_free_
char *buffer
= NULL
;
900 const char *tz
= NULL
;
902 if (link
->network
->dhcp_server_timezone
)
903 tz
= link
->network
->dhcp_server_timezone
;
905 r
= get_timezone(&buffer
);
907 log_warning_errno(r
, "Failed to determine timezone: %m");
913 r
= sd_dhcp_server_set_timezone(link
->dhcp_server
, tz
);
919 r
= sd_dhcp_server_start(link
->dhcp_server
);
921 log_link_warning_errno(link
, r
, "Could not start DHCPv4 server instance: %m");
923 link_enter_failed(link
);
928 log_link_debug(link
, "Offering DHCPv4 leases");
931 if (link
->link_messages
== 0)
932 link_enter_set_routes(link
);
934 log_link_debug(link
, "Setting addresses");
939 int link_address_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
940 _cleanup_link_unref_ Link
*link
= userdata
;
945 assert(link
->ifname
);
947 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
950 r
= sd_netlink_message_get_errno(m
);
951 if (r
< 0 && r
!= -EADDRNOTAVAIL
)
952 log_link_warning_errno(link
, r
, "Could not drop address: %m");
957 static int link_set_bridge_fdb(Link
*const link
) {
961 LIST_FOREACH(static_fdb_entries
, fdb_entry
, link
->network
->static_fdb_entries
) {
962 r
= fdb_entry_configure(link
, fdb_entry
);
964 log_link_error_errno(link
, r
, "Failed to add MAC entry to static MAC table: %m");
972 static int link_set_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
973 _cleanup_link_unref_ Link
*link
= userdata
;
976 log_link_debug(link
, "Set link");
978 r
= sd_netlink_message_get_errno(m
);
979 if (r
< 0 && r
!= -EEXIST
) {
980 log_link_error_errno(link
, r
, "Could not join netdev: %m");
981 link_enter_failed(link
);
988 static int set_hostname_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
989 _cleanup_link_unref_ Link
*link
= userdata
;
990 const sd_bus_error
*e
;
995 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
998 e
= sd_bus_message_get_error(m
);
1000 log_link_warning_errno(link
, sd_bus_error_get_errno(e
), "Could not set hostname: %s", e
->message
);
1005 int link_set_hostname(Link
*link
, const char *hostname
) {
1009 assert(link
->manager
);
1011 log_link_debug(link
, "Setting transient hostname: '%s'", strna(hostname
));
1013 if (!link
->manager
->bus
) {
1014 /* TODO: replace by assert when we can rely on kdbus */
1015 log_link_info(link
, "Not connected to system bus, ignoring transient hostname.");
1019 r
= sd_bus_call_method_async(
1022 "org.freedesktop.hostname1",
1023 "/org/freedesktop/hostname1",
1024 "org.freedesktop.hostname1",
1026 set_hostname_handler
,
1033 return log_link_error_errno(link
, r
, "Could not set transient hostname: %m");
1040 static int set_timezone_handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
1041 _cleanup_link_unref_ Link
*link
= userdata
;
1042 const sd_bus_error
*e
;
1047 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1050 e
= sd_bus_message_get_error(m
);
1052 log_link_warning_errno(link
, sd_bus_error_get_errno(e
), "Could not set timezone: %s", e
->message
);
1057 int link_set_timezone(Link
*link
, const char *tz
) {
1061 assert(link
->manager
);
1064 log_link_debug(link
, "Setting system timezone: '%s'", tz
);
1066 if (!link
->manager
->bus
) {
1067 log_link_info(link
, "Not connected to system bus, ignoring timezone.");
1071 r
= sd_bus_call_method_async(
1074 "org.freedesktop.timedate1",
1075 "/org/freedesktop/timedate1",
1076 "org.freedesktop.timedate1",
1078 set_timezone_handler
,
1084 return log_link_error_errno(link
, r
, "Could not set timezone: %m");
1091 static int set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1092 _cleanup_link_unref_ Link
*link
= userdata
;
1097 assert(link
->ifname
);
1099 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1102 r
= sd_netlink_message_get_errno(m
);
1104 log_link_warning_errno(link
, r
, "Could not set MTU: %m");
1109 int link_set_mtu(Link
*link
, uint32_t mtu
) {
1110 _cleanup_netlink_message_unref_ sd_netlink_message
*req
= NULL
;
1114 assert(link
->manager
);
1115 assert(link
->manager
->rtnl
);
1117 log_link_debug(link
, "Setting MTU: %" PRIu32
, mtu
);
1119 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1121 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1123 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, mtu
);
1125 return log_link_error_errno(link
, r
, "Could not append MTU: %m");
1127 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, set_mtu_handler
, link
, 0, NULL
);
1129 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1136 static int link_set_bridge(Link
*link
) {
1137 _cleanup_netlink_message_unref_ sd_netlink_message
*req
= NULL
;
1141 assert(link
->network
);
1143 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1145 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1147 r
= sd_rtnl_message_link_set_family(req
, PF_BRIDGE
);
1149 return log_link_error_errno(link
, r
, "Could not set message family: %m");
1151 r
= sd_netlink_message_open_container(req
, IFLA_PROTINFO
);
1153 return log_link_error_errno(link
, r
, "Could not append IFLA_PROTINFO attribute: %m");
1155 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_GUARD
, !link
->network
->use_bpdu
);
1157 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_GUARD attribute: %m");
1159 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MODE
, link
->network
->hairpin
);
1161 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_MODE attribute: %m");
1163 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_FAST_LEAVE
, link
->network
->fast_leave
);
1165 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_FAST_LEAVE attribute: %m");
1167 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_PROTECT
, !link
->network
->allow_port_to_be_root
);
1169 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_PROTECT attribute: %m");
1171 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_UNICAST_FLOOD
, link
->network
->unicast_flood
);
1173 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_UNICAST_FLOOD attribute: %m");
1175 if(link
->network
->cost
!= 0) {
1176 r
= sd_netlink_message_append_u32(req
, IFLA_BRPORT_COST
, link
->network
->cost
);
1178 return log_link_error_errno(link
, r
, "Could not append IFLA_BRPORT_COST attribute: %m");
1181 r
= sd_netlink_message_close_container(req
);
1183 return log_link_error_errno(link
, r
, "Could not append IFLA_LINKINFO attribute: %m");
1185 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_set_handler
, link
, 0, NULL
);
1187 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1194 static void lldp_handler(sd_lldp
*lldp
, int event
, void *userdata
) {
1195 Link
*link
= userdata
;
1199 assert(link
->network
);
1200 assert(link
->manager
);
1203 case SD_LLDP_EVENT_UPDATE_INFO
:
1204 r
= sd_lldp_save(link
->lldp
, link
->lldp_file
);
1206 log_link_warning_errno(link
, r
, "Could not save LLDP: %m");
1214 static int link_acquire_conf(Link
*link
) {
1218 assert(link
->network
);
1219 assert(link
->manager
);
1220 assert(link
->manager
->event
);
1222 if (link_ipv4ll_enabled(link
)) {
1223 assert(link
->ipv4ll
);
1225 log_link_debug(link
, "Acquiring IPv4 link-local address");
1227 r
= sd_ipv4ll_start(link
->ipv4ll
);
1229 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
1232 if (link_dhcp4_enabled(link
)) {
1233 assert(link
->dhcp_client
);
1235 log_link_debug(link
, "Acquiring DHCPv4 lease");
1237 r
= sd_dhcp_client_start(link
->dhcp_client
);
1239 return log_link_warning_errno(link
, r
, "Could not acquire DHCPv4 lease: %m");
1242 if (link_dhcp6_enabled(link
)) {
1243 assert(link
->ndisc_router_discovery
);
1245 log_link_debug(link
, "Discovering IPv6 routers");
1247 r
= sd_ndisc_router_discovery_start(link
->ndisc_router_discovery
);
1249 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Discovery: %m");
1252 if (link_lldp_enabled(link
)) {
1255 log_link_debug(link
, "Starting LLDP");
1257 r
= sd_lldp_start(link
->lldp
);
1259 return log_link_warning_errno(link
, r
, "Could not start LLDP: %m");
1265 bool link_has_carrier(Link
*link
) {
1266 /* see Documentation/networking/operstates.txt in the kernel sources */
1268 if (link
->kernel_operstate
== IF_OPER_UP
)
1271 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1272 /* operstate may not be implemented, so fall back to flags */
1273 if ((link
->flags
& IFF_LOWER_UP
) && !(link
->flags
& IFF_DORMANT
))
1279 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1280 _cleanup_link_unref_ Link
*link
= userdata
;
1285 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1288 r
= sd_netlink_message_get_errno(m
);
1290 /* we warn but don't fail the link, as it may be
1292 log_link_warning_errno(link
, r
, "Could not bring up interface: %m");
1297 static int link_up(Link
*link
) {
1298 _cleanup_netlink_message_unref_ sd_netlink_message
*req
= NULL
;
1299 uint8_t ipv6ll_mode
;
1303 assert(link
->network
);
1304 assert(link
->manager
);
1305 assert(link
->manager
->rtnl
);
1307 log_link_debug(link
, "Bringing link up");
1309 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1311 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1313 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
1315 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1317 if (link
->network
->mac
) {
1318 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
1320 return log_link_error_errno(link
, r
, "Could not set MAC address: %m");
1323 if (link
->network
->mtu
) {
1324 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, link
->network
->mtu
);
1326 return log_link_error_errno(link
, r
, "Could not set MTU: %m");
1329 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
1331 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
1333 if (socket_ipv6_is_supported()) {
1334 /* if the kernel lacks ipv6 support setting IFF_UP fails if any ipv6 options are passed */
1335 r
= sd_netlink_message_open_container(req
, AF_INET6
);
1337 return log_link_error_errno(link
, r
, "Could not open AF_INET6 container: %m");
1339 ipv6ll_mode
= link_ipv6ll_enabled(link
) ? IN6_ADDR_GEN_MODE_EUI64
: IN6_ADDR_GEN_MODE_NONE
;
1340 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, ipv6ll_mode
);
1342 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m");
1344 if (!in_addr_is_null(AF_INET6
, &link
->network
->ipv6_token
)) {
1345 r
= sd_netlink_message_append_in6_addr(req
, IFLA_INET6_TOKEN
, &link
->network
->ipv6_token
.in6
);
1347 return log_link_error_errno(link
, r
, "Could not append IFLA_INET6_TOKEN: %m");
1350 r
= sd_netlink_message_close_container(req
);
1352 return log_link_error_errno(link
, r
, "Could not close AF_INET6 container: %m");
1355 r
= sd_netlink_message_close_container(req
);
1357 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
1359 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_up_handler
, link
, 0, NULL
);
1361 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1368 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1369 _cleanup_link_unref_ Link
*link
= userdata
;
1374 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1377 r
= sd_netlink_message_get_errno(m
);
1379 log_link_warning_errno(link
, r
, "Could not bring down interface: %m");
1384 static int link_down(Link
*link
) {
1385 _cleanup_netlink_message_unref_ sd_netlink_message
*req
= NULL
;
1389 assert(link
->manager
);
1390 assert(link
->manager
->rtnl
);
1392 log_link_debug(link
, "Bringing link down");
1394 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
,
1395 RTM_SETLINK
, link
->ifindex
);
1397 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1399 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
1401 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
1403 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, link_down_handler
, link
, 0, NULL
);
1405 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
1412 static int link_handle_bound_to_list(Link
*link
) {
1416 bool required_up
= false;
1417 bool link_is_up
= false;
1421 if (hashmap_isempty(link
->bound_to_links
))
1424 if (link
->flags
& IFF_UP
)
1427 HASHMAP_FOREACH (l
, link
->bound_to_links
, i
)
1428 if (link_has_carrier(l
)) {
1433 if (!required_up
&& link_is_up
) {
1434 r
= link_down(link
);
1437 } else if (required_up
&& !link_is_up
) {
1446 static int link_handle_bound_by_list(Link
*link
) {
1453 if (hashmap_isempty(link
->bound_by_links
))
1456 HASHMAP_FOREACH (l
, link
->bound_by_links
, i
) {
1457 r
= link_handle_bound_to_list(l
);
1465 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
1471 if (link
== carrier
)
1474 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
1477 r
= hashmap_ensure_allocated(h
, NULL
);
1481 r
= hashmap_put(*h
, INT_TO_PTR(carrier
->ifindex
), carrier
);
1488 static int link_new_bound_by_list(Link
*link
) {
1493 bool list_updated
= false;
1496 assert(link
->manager
);
1500 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
1501 if (!carrier
->network
)
1504 if (strv_isempty(carrier
->network
->bind_carrier
))
1507 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
, 0)) {
1508 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
1512 list_updated
= true;
1519 HASHMAP_FOREACH (carrier
, link
->bound_by_links
, i
) {
1520 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
1524 link_dirty(carrier
);
1530 static int link_new_bound_to_list(Link
*link
) {
1535 bool list_updated
= false;
1538 assert(link
->manager
);
1543 if (strv_isempty(link
->network
->bind_carrier
))
1548 HASHMAP_FOREACH (carrier
, m
->links
, i
) {
1549 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
, 0)) {
1550 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
1554 list_updated
= true;
1561 HASHMAP_FOREACH (carrier
, link
->bound_to_links
, i
) {
1562 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
1566 link_dirty(carrier
);
1572 static int link_new_carrier_maps(Link
*link
) {
1575 r
= link_new_bound_by_list(link
);
1579 r
= link_handle_bound_by_list(link
);
1583 r
= link_new_bound_to_list(link
);
1587 r
= link_handle_bound_to_list(link
);
1594 static void link_free_bound_to_list(Link
*link
) {
1598 HASHMAP_FOREACH (bound_to
, link
->bound_to_links
, i
) {
1599 hashmap_remove(link
->bound_to_links
, INT_TO_PTR(bound_to
->ifindex
));
1601 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
1602 link_dirty(bound_to
);
1608 static void link_free_bound_by_list(Link
*link
) {
1612 HASHMAP_FOREACH (bound_by
, link
->bound_by_links
, i
) {
1613 hashmap_remove(link
->bound_by_links
, INT_TO_PTR(bound_by
->ifindex
));
1615 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
1616 link_dirty(bound_by
);
1617 link_handle_bound_to_list(bound_by
);
1624 static void link_free_carrier_maps(Link
*link
) {
1625 bool list_updated
= false;
1629 if (!hashmap_isempty(link
->bound_to_links
)) {
1630 link_free_bound_to_list(link
);
1631 list_updated
= true;
1634 if (!hashmap_isempty(link
->bound_by_links
)) {
1635 link_free_bound_by_list(link
);
1636 list_updated
= true;
1645 void link_drop(Link
*link
) {
1646 if (!link
|| link
->state
== LINK_STATE_LINGER
)
1649 link_set_state(link
, LINK_STATE_LINGER
);
1651 link_free_carrier_maps(link
);
1653 log_link_debug(link
, "Link removed");
1655 (void)unlink(link
->state_file
);
1661 static int link_joined(Link
*link
) {
1665 assert(link
->network
);
1667 if (!hashmap_isempty(link
->bound_to_links
)) {
1668 r
= link_handle_bound_to_list(link
);
1671 } else if (!(link
->flags
& IFF_UP
)) {
1674 link_enter_failed(link
);
1679 if(link
->network
->bridge
) {
1680 r
= link_set_bridge(link
);
1682 log_link_error_errno(link
, r
, "Could not set bridge message: %m");
1685 return link_enter_set_addresses(link
);
1688 static int netdev_join_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
1689 _cleanup_link_unref_ Link
*link
= userdata
;
1693 assert(link
->network
);
1697 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1700 r
= sd_netlink_message_get_errno(m
);
1701 if (r
< 0 && r
!= -EEXIST
) {
1702 log_link_error_errno(link
, r
, "Could not join netdev: %m");
1703 link_enter_failed(link
);
1706 log_link_debug(link
, "Joined netdev");
1708 if (link
->enslaving
<= 0)
1714 static int link_enter_join_netdev(Link
*link
) {
1720 assert(link
->network
);
1721 assert(link
->state
== LINK_STATE_PENDING
);
1723 link_set_state(link
, LINK_STATE_ENSLAVING
);
1727 if (!link
->network
->bridge
&&
1728 !link
->network
->bond
&&
1729 hashmap_isempty(link
->network
->stacked_netdevs
))
1730 return link_joined(link
);
1732 if (link
->network
->bond
) {
1733 log_struct(LOG_DEBUG
,
1734 LOG_LINK_INTERFACE(link
),
1735 LOG_NETDEV_INTERFACE(link
->network
->bond
),
1736 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bond
->ifname
),
1739 r
= netdev_join(link
->network
->bond
, link
, netdev_join_handler
);
1741 log_struct_errno(LOG_WARNING
, r
,
1742 LOG_LINK_INTERFACE(link
),
1743 LOG_NETDEV_INTERFACE(link
->network
->bond
),
1744 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bond
->ifname
),
1747 link_enter_failed(link
);
1754 if (link
->network
->bridge
) {
1755 log_struct(LOG_DEBUG
,
1756 LOG_LINK_INTERFACE(link
),
1757 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
1758 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", link
->network
->bridge
->ifname
),
1761 r
= netdev_join(link
->network
->bridge
, link
, netdev_join_handler
);
1763 log_struct_errno(LOG_WARNING
, r
,
1764 LOG_LINK_INTERFACE(link
),
1765 LOG_NETDEV_INTERFACE(link
->network
->bridge
),
1766 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", link
->network
->bridge
->ifname
),
1768 link_enter_failed(link
);
1775 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
, i
) {
1777 log_struct(LOG_DEBUG
,
1778 LOG_LINK_INTERFACE(link
),
1779 LOG_NETDEV_INTERFACE(netdev
),
1780 LOG_LINK_MESSAGE(link
, "Enslaving by '%s'", netdev
->ifname
),
1783 r
= netdev_join(netdev
, link
, netdev_join_handler
);
1785 log_struct_errno(LOG_WARNING
, r
,
1786 LOG_LINK_INTERFACE(link
),
1787 LOG_NETDEV_INTERFACE(netdev
),
1788 LOG_LINK_MESSAGE(link
, "Could not join netdev '%s': %m", netdev
->ifname
),
1790 link_enter_failed(link
);
1800 static int link_set_ipv4_forward(Link
*link
) {
1801 const char *p
= NULL
, *v
;
1804 if (link
->flags
& IFF_LOOPBACK
)
1807 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
1810 p
= strjoina("/proc/sys/net/ipv4/conf/", link
->ifname
, "/forwarding");
1811 v
= one_zero(link_ipv4_forward_enabled(link
));
1813 r
= write_string_file(p
, v
, 0);
1815 /* If the right value is set anyway, don't complain */
1816 if (verify_one_line_file(p
, v
) > 0)
1819 log_link_warning_errno(link
, r
, "Cannot configure IPv4 forwarding for interface %s: %m", link
->ifname
);
1825 static int link_set_ipv6_forward(Link
*link
) {
1826 const char *p
= NULL
, *v
= NULL
;
1829 /* Make this a NOP if IPv6 is not available */
1830 if (!socket_ipv6_is_supported())
1833 if (link
->flags
& IFF_LOOPBACK
)
1836 if (link
->network
->ip_forward
== _ADDRESS_FAMILY_BOOLEAN_INVALID
)
1839 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/forwarding");
1840 v
= one_zero(link_ipv6_forward_enabled(link
));
1842 r
= write_string_file(p
, v
, 0);
1844 /* If the right value is set anyway, don't complain */
1845 if (verify_one_line_file(p
, v
) > 0)
1848 log_link_warning_errno(link
, r
, "Cannot configure IPv6 forwarding for interface: %m");
1854 static int link_set_ipv6_privacy_extensions(Link
*link
) {
1855 char buf
[DECIMAL_STR_MAX(unsigned) + 1];
1856 IPv6PrivacyExtensions s
;
1857 const char *p
= NULL
;
1860 /* Make this a NOP if IPv6 is not available */
1861 if (!socket_ipv6_is_supported())
1864 s
= link_ipv6_privacy_extensions(link
);
1865 if (s
== _IPV6_PRIVACY_EXTENSIONS_INVALID
)
1868 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/use_tempaddr");
1869 xsprintf(buf
, "%u", link
->network
->ipv6_privacy_extensions
);
1871 r
= write_string_file(p
, buf
, 0);
1873 /* If the right value is set anyway, don't complain */
1874 if (verify_one_line_file(p
, buf
) > 0)
1877 log_link_warning_errno(link
, r
, "Cannot configure IPv6 privacy extension for interface: %m");
1883 static int link_set_ipv6_accept_ra(Link
*link
) {
1884 const char *p
= NULL
, *v
= NULL
;
1887 /* Make this a NOP if IPv6 is not available */
1888 if (!socket_ipv6_is_supported())
1891 if (link
->flags
& IFF_LOOPBACK
)
1894 /* If unset use system default (enabled if local forwarding is disabled.
1895 * disabled if local forwarding is enabled).
1896 * If set, ignore or enforce RA independent of local forwarding state.
1898 if (link
->network
->ipv6_accept_ra
< 0)
1899 /* default to accept RA if ip_forward is disabled and ignore RA if ip_forward is enabled */
1901 else if (link
->network
->ipv6_accept_ra
> 0)
1902 /* "2" means accept RA even if ip_forward is enabled */
1905 /* "0" means ignore RA */
1908 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/accept_ra");
1910 r
= write_string_file(p
, v
, 0);
1912 /* If the right value is set anyway, don't complain */
1913 if (verify_one_line_file(p
, v
) > 0)
1916 log_link_warning_errno(link
, r
, "Cannot configure IPv6 accept_ra for interface: %m");
1922 static int link_set_ipv6_dad_transmits(Link
*link
) {
1923 char buf
[DECIMAL_STR_MAX(unsigned) + 1];
1924 const char *p
= NULL
;
1927 /* Make this a NOP if IPv6 is not available */
1928 if (!socket_ipv6_is_supported())
1931 if (link
->flags
& IFF_LOOPBACK
)
1934 if (link
->network
->ipv6_dad_transmits
< 0)
1937 p
= strjoina("/proc/sys/net/ipv6/conf/", link
->ifname
, "/dad_transmits");
1939 xsprintf(buf
, "%u", link
->network
->ipv6_dad_transmits
);
1941 r
= write_string_file(p
, buf
, 0);
1943 /* If the right value is set anyway, don't complain */
1944 if (verify_one_line_file(p
, buf
) > 0)
1947 log_link_warning_errno(link
, r
, "Cannot set IPv6 dad transmits for interface: %m");
1953 static int link_configure(Link
*link
) {
1957 assert(link
->network
);
1958 assert(link
->state
== LINK_STATE_PENDING
);
1960 r
= link_set_bridge_fdb(link
);
1964 r
= link_set_ipv4_forward(link
);
1968 r
= link_set_ipv6_forward(link
);
1972 r
= link_set_ipv6_privacy_extensions(link
);
1976 r
= link_set_ipv6_accept_ra(link
);
1980 r
= link_set_ipv6_dad_transmits(link
);
1984 if (link_ipv4ll_enabled(link
)) {
1985 r
= ipv4ll_configure(link
);
1990 if (link_dhcp4_enabled(link
)) {
1991 r
= dhcp4_configure(link
);
1996 if (link_dhcp4_server_enabled(link
)) {
1997 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
2001 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, NULL
, 0);
2006 if (link_dhcp6_enabled(link
)) {
2007 r
= ndisc_configure(link
);
2012 if (link_lldp_enabled(link
)) {
2013 r
= sd_lldp_new(link
->ifindex
, link
->ifname
, &link
->mac
, &link
->lldp
);
2017 r
= sd_lldp_attach_event(link
->lldp
, NULL
, 0);
2021 r
= sd_lldp_set_callback(link
->lldp
,
2022 lldp_handler
, link
);
2027 if (link_has_carrier(link
)) {
2028 r
= link_acquire_conf(link
);
2033 return link_enter_join_netdev(link
);
2036 static int link_initialized_and_synced(sd_netlink
*rtnl
, sd_netlink_message
*m
,
2038 _cleanup_link_unref_ Link
*link
= userdata
;
2043 assert(link
->ifname
);
2044 assert(link
->manager
);
2046 if (link
->state
!= LINK_STATE_PENDING
)
2049 log_link_debug(link
, "Link state is up-to-date");
2051 r
= link_new_bound_by_list(link
);
2055 r
= link_handle_bound_by_list(link
);
2059 r
= network_get(link
->manager
, link
->udev_device
, link
->ifname
,
2060 &link
->mac
, &network
);
2062 link_enter_unmanaged(link
);
2067 if (link
->flags
& IFF_LOOPBACK
) {
2068 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
2069 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
2071 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
2072 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
2074 if (network
->dhcp_server
)
2075 log_link_debug(link
, "Ignoring DHCP server for loopback link");
2078 r
= network_apply(link
->manager
, network
, link
);
2082 r
= link_new_bound_to_list(link
);
2086 r
= link_configure(link
);
2093 int link_initialized(Link
*link
, struct udev_device
*device
) {
2094 _cleanup_netlink_message_unref_ sd_netlink_message
*req
= NULL
;
2098 assert(link
->manager
);
2099 assert(link
->manager
->rtnl
);
2102 if (link
->state
!= LINK_STATE_PENDING
)
2105 if (link
->udev_device
)
2108 log_link_debug(link
, "udev initialized link");
2110 link
->udev_device
= udev_device_ref(device
);
2112 /* udev has initialized the link, but we don't know if we have yet
2113 * processed the NEWLINK messages with the latest state. Do a GETLINK,
2114 * when it returns we know that the pending NEWLINKs have already been
2115 * processed and that we are up-to-date */
2117 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
,
2122 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
,
2123 link_initialized_and_synced
, link
, 0, NULL
);
2132 int link_add(Manager
*m
, sd_netlink_message
*message
, Link
**ret
) {
2134 _cleanup_udev_device_unref_
struct udev_device
*device
= NULL
;
2135 char ifindex_str
[2 + DECIMAL_STR_MAX(int)];
2143 r
= link_new(m
, message
, ret
);
2149 log_link_debug(link
, "Link %d added", link
->ifindex
);
2151 if (detect_container() <= 0) {
2152 /* not in a container, udev will be around */
2153 sprintf(ifindex_str
, "n%d", link
->ifindex
);
2154 device
= udev_device_new_from_device_id(m
->udev
, ifindex_str
);
2156 r
= log_link_warning_errno(link
, errno
, "Could not find udev device: %m");
2160 if (udev_device_get_is_initialized(device
) <= 0) {
2162 log_link_debug(link
, "link pending udev initialization...");
2166 r
= link_initialized(link
, device
);
2170 /* we are calling a callback directly, so must take a ref */
2173 r
= link_initialized_and_synced(m
->rtnl
, NULL
, link
);
2180 link_enter_failed(link
);
2184 static int link_carrier_gained(Link
*link
) {
2189 if (link
->network
) {
2190 r
= link_acquire_conf(link
);
2192 link_enter_failed(link
);
2197 r
= link_handle_bound_by_list(link
);
2204 static int link_carrier_lost(Link
*link
) {
2209 r
= link_stop_clients(link
);
2211 link_enter_failed(link
);
2215 r
= link_handle_bound_by_list(link
);
2222 int link_carrier_reset(Link
*link
) {
2227 if (link_has_carrier(link
)) {
2228 r
= link_carrier_lost(link
);
2232 r
= link_carrier_gained(link
);
2236 log_link_info(link
, "Reset carrier");
2243 int link_update(Link
*link
, sd_netlink_message
*m
) {
2244 struct ether_addr mac
;
2247 bool had_carrier
, carrier_gained
, carrier_lost
;
2251 assert(link
->ifname
);
2254 if (link
->state
== LINK_STATE_LINGER
) {
2256 log_link_info(link
, "Link readded");
2257 link_set_state(link
, LINK_STATE_ENSLAVING
);
2259 r
= link_new_carrier_maps(link
);
2264 r
= sd_netlink_message_read_string(m
, IFLA_IFNAME
, &ifname
);
2265 if (r
>= 0 && !streq(ifname
, link
->ifname
)) {
2266 log_link_info(link
, "Renamed to %s", ifname
);
2268 link_free_carrier_maps(link
);
2270 r
= free_and_strdup(&link
->ifname
, ifname
);
2274 r
= link_new_carrier_maps(link
);
2279 r
= sd_netlink_message_read_u32(m
, IFLA_MTU
, &mtu
);
2280 if (r
>= 0 && mtu
> 0) {
2282 if (!link
->original_mtu
) {
2283 link
->original_mtu
= mtu
;
2284 log_link_debug(link
, "Saved original MTU: %" PRIu32
, link
->original_mtu
);
2287 if (link
->dhcp_client
) {
2288 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
,
2291 log_link_warning_errno(link
, r
, "Could not update MTU in DHCP client: %m");
2297 /* The kernel may broadcast NEWLINK messages without the MAC address
2298 set, simply ignore them. */
2299 r
= sd_netlink_message_read_ether_addr(m
, IFLA_ADDRESS
, &mac
);
2301 if (memcmp(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
2304 memcpy(link
->mac
.ether_addr_octet
, mac
.ether_addr_octet
,
2307 log_link_debug(link
, "MAC address: "
2308 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
2309 mac
.ether_addr_octet
[0],
2310 mac
.ether_addr_octet
[1],
2311 mac
.ether_addr_octet
[2],
2312 mac
.ether_addr_octet
[3],
2313 mac
.ether_addr_octet
[4],
2314 mac
.ether_addr_octet
[5]);
2317 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
2319 return log_link_warning_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
2322 if (link
->dhcp_client
) {
2323 r
= sd_dhcp_client_set_mac(link
->dhcp_client
,
2324 (const uint8_t *) &link
->mac
,
2328 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
2331 if (link
->dhcp6_client
) {
2332 r
= sd_dhcp6_client_set_mac(link
->dhcp6_client
,
2333 (const uint8_t *) &link
->mac
,
2337 return log_link_warning_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
2342 had_carrier
= link_has_carrier(link
);
2344 r
= link_update_flags(link
, m
);
2348 carrier_gained
= !had_carrier
&& link_has_carrier(link
);
2349 carrier_lost
= had_carrier
&& !link_has_carrier(link
);
2351 if (carrier_gained
) {
2352 log_link_info(link
, "Gained carrier");
2354 r
= link_carrier_gained(link
);
2357 } else if (carrier_lost
) {
2358 log_link_info(link
, "Lost carrier");
2360 r
= link_carrier_lost(link
);
2369 int link_save(Link
*link
) {
2370 _cleanup_free_
char *temp_path
= NULL
;
2371 _cleanup_fclose_
FILE *f
= NULL
;
2372 const char *admin_state
, *oper_state
;
2378 assert(link
->state_file
);
2379 assert(link
->lease_file
);
2380 assert(link
->manager
);
2382 if (link
->state
== LINK_STATE_LINGER
) {
2383 unlink(link
->state_file
);
2387 admin_state
= link_state_to_string(link
->state
);
2388 assert(admin_state
);
2390 oper_state
= link_operstate_to_string(link
->operstate
);
2393 r
= fopen_temporary(link
->state_file
, &f
, &temp_path
);
2397 fchmod(fileno(f
), 0644);
2400 "# This is private data. Do not parse.\n"
2403 admin_state
, oper_state
);
2405 if (link
->network
) {
2406 char **address
, **domain
;
2408 sd_dhcp6_lease
*dhcp6_lease
= NULL
;
2410 if (link
->dhcp6_client
) {
2411 r
= sd_dhcp6_client_get_lease(link
->dhcp6_client
,
2414 log_link_debug(link
, "No DHCPv6 lease");
2417 fprintf(f
, "NETWORK_FILE=%s\n", link
->network
->filename
);
2421 STRV_FOREACH(address
, link
->network
->dns
) {
2428 if (link
->network
->dhcp_dns
&&
2430 const struct in_addr
*addresses
;
2432 r
= sd_dhcp_lease_get_dns(link
->dhcp_lease
, &addresses
);
2436 serialize_in_addrs(f
, addresses
, r
);
2441 if (link
->network
->dhcp_dns
&& dhcp6_lease
) {
2442 struct in6_addr
*in6_addrs
;
2444 r
= sd_dhcp6_lease_get_dns(dhcp6_lease
, &in6_addrs
);
2448 serialize_in6_addrs(f
, in6_addrs
, r
);
2456 STRV_FOREACH(address
, link
->network
->ntp
) {
2463 if (link
->network
->dhcp_ntp
&&
2465 const struct in_addr
*addresses
;
2467 r
= sd_dhcp_lease_get_ntp(link
->dhcp_lease
, &addresses
);
2471 serialize_in_addrs(f
, addresses
, r
);
2476 if (link
->network
->dhcp_ntp
&& dhcp6_lease
) {
2477 struct in6_addr
*in6_addrs
;
2481 r
= sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease
,
2486 serialize_in6_addrs(f
, in6_addrs
, r
);
2490 r
= sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease
, &hosts
);
2492 STRV_FOREACH(hostname
, hosts
) {
2495 fputs(*hostname
, f
);
2503 fprintf(f
, "DOMAINS=");
2505 STRV_FOREACH(domain
, link
->network
->domains
) {
2512 if (link
->network
->dhcp_domains
&&
2514 const char *domainname
;
2516 r
= sd_dhcp_lease_get_domainname(link
->dhcp_lease
, &domainname
);
2520 fputs(domainname
, f
);
2525 if (link
->network
->dhcp_domains
&& dhcp6_lease
) {
2528 r
= sd_dhcp6_lease_get_domains(dhcp6_lease
, &domains
);
2530 STRV_FOREACH(domain
, domains
) {
2541 fprintf(f
, "WILDCARD_DOMAIN=%s\n",
2542 yes_no(link
->network
->wildcard_domain
));
2544 fprintf(f
, "LLMNR=%s\n",
2545 resolve_support_to_string(link
->network
->llmnr
));
2547 fprintf(f
, "ADDRESSES=");
2549 SET_FOREACH(a
, link
->addresses
, i
) {
2550 _cleanup_free_
char *address_str
= NULL
;
2552 r
= in_addr_to_string(a
->family
, &a
->in_addr
, &address_str
);
2556 fprintf(f
, "%s%s/%u", space
? " " : "", address_str
, a
->prefixlen
);
2563 if (!hashmap_isempty(link
->bound_to_links
)) {
2567 fputs("CARRIER_BOUND_TO=", f
);
2568 HASHMAP_FOREACH(carrier
, link
->bound_to_links
, i
) {
2571 fputs(carrier
->ifname
, f
);
2578 if (!hashmap_isempty(link
->bound_by_links
)) {
2582 fputs("CARRIER_BOUND_BY=", f
);
2583 HASHMAP_FOREACH(carrier
, link
->bound_by_links
, i
) {
2586 fputs(carrier
->ifname
, f
);
2593 if (link
->dhcp_lease
) {
2594 const char *tz
= NULL
;
2596 r
= sd_dhcp_lease_get_timezone(link
->dhcp_lease
, &tz
);
2598 fprintf(f
, "TIMEZONE=%s\n", tz
);
2601 if (link
->dhcp_lease
) {
2602 assert(link
->network
);
2604 r
= dhcp_lease_save(link
->dhcp_lease
, link
->lease_file
);
2612 unlink(link
->lease_file
);
2615 assert(link
->network
);
2617 r
= sd_lldp_save(link
->lldp
, link
->lldp_file
);
2625 unlink(link
->lldp_file
);
2627 r
= fflush_and_check(f
);
2631 if (rename(temp_path
, link
->state_file
) < 0) {
2639 (void) unlink(link
->state_file
);
2641 (void) unlink(temp_path
);
2643 return log_link_error_errno(link
, r
, "Failed to save link data to %s: %m", link
->state_file
);
2646 /* The serialized state in /run is no longer up-to-date. */
2647 void link_dirty(Link
*link
) {
2652 r
= set_ensure_allocated(&link
->manager
->dirty_links
, NULL
);
2654 /* allocation errors are ignored */
2657 r
= set_put(link
->manager
->dirty_links
, link
);
2659 /* allocation errors are ignored */
2665 /* The serialized state in /run is up-to-date */
2666 void link_clean(Link
*link
) {
2668 assert(link
->manager
);
2670 set_remove(link
->manager
->dirty_links
, link
);
2674 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
2675 [LINK_STATE_PENDING
] = "pending",
2676 [LINK_STATE_ENSLAVING
] = "configuring",
2677 [LINK_STATE_SETTING_ADDRESSES
] = "configuring",
2678 [LINK_STATE_SETTING_ROUTES
] = "configuring",
2679 [LINK_STATE_CONFIGURED
] = "configured",
2680 [LINK_STATE_UNMANAGED
] = "unmanaged",
2681 [LINK_STATE_FAILED
] = "failed",
2682 [LINK_STATE_LINGER
] = "linger",
2685 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);
2687 static const char* const link_operstate_table
[_LINK_OPERSTATE_MAX
] = {
2688 [LINK_OPERSTATE_OFF
] = "off",
2689 [LINK_OPERSTATE_NO_CARRIER
] = "no-carrier",
2690 [LINK_OPERSTATE_DORMANT
] = "dormant",
2691 [LINK_OPERSTATE_CARRIER
] = "carrier",
2692 [LINK_OPERSTATE_DEGRADED
] = "degraded",
2693 [LINK_OPERSTATE_ROUTABLE
] = "routable",
2696 DEFINE_STRING_TABLE_LOOKUP(link_operstate
, LinkOperationalState
);