1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 #include <netinet/in.h>
5 #include <linux/if_arp.h>
6 #include <linux/if_bridge.h>
8 #include "missing_network.h"
9 #include "netif-util.h"
10 #include "netlink-util.h"
11 #include "networkd-address.h"
12 #include "networkd-can.h"
13 #include "networkd-link.h"
14 #include "networkd-manager.h"
15 #include "networkd-queue.h"
16 #include "networkd-setlink.h"
17 #include "networkd-sriov.h"
18 #include "networkd-wiphy.h"
20 static int get_link_default_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
21 return link_getlink_handler_internal(rtnl
, m
, link
, "Failed to sync link information");
24 static int get_link_master_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
25 if (get_link_default_handler(rtnl
, m
, link
) > 0)
26 link
->master_set
= true;
30 static int get_link_update_flag_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
32 assert(link
->set_flags_messages
> 0);
34 link
->set_flags_messages
--;
36 return get_link_default_handler(rtnl
, m
, link
);
39 static int set_link_handler_internal(
41 sd_netlink_message
*m
,
45 link_netlink_message_handler_t get_link_handler
) {
53 r
= sd_netlink_message_get_errno(m
);
55 const char *error_msg
;
57 error_msg
= strjoina("Failed to set ", request_type_to_string(req
->type
), ignore
? ", ignoring" : "");
58 log_link_message_warning_errno(link
, m
, r
, error_msg
);
61 link_enter_failed(link
);
65 log_link_debug(link
, "%s set.", request_type_to_string(req
->type
));
67 if (get_link_handler
) {
68 r
= link_call_getlink(link
, get_link_handler
);
70 link_enter_failed(link
);
75 if (link
->set_link_messages
== 0)
76 link_check_ready(link
);
81 static int link_set_addrgen_mode_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Request
*req
, Link
*link
, void *userdata
) {
84 r
= set_link_handler_internal(rtnl
, m
, req
, link
, /* ignore = */ true, NULL
);
88 r
= link_drop_ipv6ll_addresses(link
);
90 log_link_warning_errno(link
, r
, "Failed to drop IPv6LL addresses: %m");
91 link_enter_failed(link
);
97 static int link_set_bond_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Request
*req
, Link
*link
, void *userdata
) {
98 return set_link_handler_internal(rtnl
, m
, req
, link
, /* ignore = */ false, NULL
);
101 static int link_set_bridge_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Request
*req
, Link
*link
, void *userdata
) {
102 return set_link_handler_internal(rtnl
, m
, req
, link
, /* ignore = */ true, NULL
);
105 static int link_set_bridge_vlan_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Request
*req
, Link
*link
, void *userdata
) {
106 return set_link_handler_internal(rtnl
, m
, req
, link
, /* ignore = */ false, NULL
);
109 static int link_set_can_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Request
*req
, Link
*link
, void *userdata
) {
110 return set_link_handler_internal(rtnl
, m
, req
, link
, /* ignore = */ false, NULL
);
113 static int link_set_flags_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Request
*req
, Link
*link
, void *userdata
) {
114 return set_link_handler_internal(rtnl
, m
, req
, link
, /* ignore = */ false, get_link_default_handler
);
117 static int link_set_group_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Request
*req
, Link
*link
, void *userdata
) {
118 return set_link_handler_internal(rtnl
, m
, req
, link
, /* ignore = */ false, NULL
);
121 static int link_set_ipoib_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Request
*req
, Link
*link
, void *userdata
) {
122 return set_link_handler_internal(rtnl
, m
, req
, link
, /* ignore = */ true, NULL
);
125 static int link_set_mac_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Request
*req
, Link
*link
, void *userdata
) {
126 return set_link_handler_internal(rtnl
, m
, req
, link
, /* ignore = */ true, get_link_default_handler
);
129 static int link_set_mac_allow_retry_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Request
*req
, Link
*link
, void *userdata
) {
135 r
= sd_netlink_message_get_errno(m
);
137 /* Most real network devices refuse to set its hardware address with -EBUSY when its
138 * operstate is not down. See, eth_prepare_mac_addr_change() in net/ethernet/eth.c
141 log_link_message_debug_errno(link
, m
, r
, "Failed to set MAC address, retrying again: %m");
143 r
= link_request_to_set_mac(link
, /* allow_retry = */ false);
145 link_enter_failed(link
);
150 return link_set_mac_handler(rtnl
, m
, req
, link
, userdata
);
153 static int link_set_master_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Request
*req
, Link
*link
, void *userdata
) {
154 return set_link_handler_internal(rtnl
, m
, req
, link
, /* ignore = */ false, get_link_master_handler
);
157 static int link_unset_master_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Request
*req
, Link
*link
, void *userdata
) {
158 /* Some devices do not support setting master ifindex. Let's ignore error on unsetting master ifindex. */
159 return set_link_handler_internal(rtnl
, m
, req
, link
, /* ignore = */ true, get_link_master_handler
);
162 static int link_set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Request
*req
, Link
*link
, void *userdata
) {
165 r
= set_link_handler_internal(rtnl
, m
, req
, link
, /* ignore = */ true, get_link_default_handler
);
169 /* The kernel resets ipv6 mtu after changing device mtu;
170 * we must set this here, after we've set device mtu */
171 r
= link_set_ipv6_mtu(link
);
173 log_link_warning_errno(link
, r
, "Failed to set IPv6 MTU, ignoring: %m");
178 static int link_configure_fill_message(
180 sd_netlink_message
*req
,
186 case REQUEST_TYPE_SET_LINK_ADDRESS_GENERATION_MODE
:
187 r
= ipv6ll_addrgen_mode_fill_message(req
, PTR_TO_UINT8(userdata
));
191 case REQUEST_TYPE_SET_LINK_BOND
:
192 r
= sd_netlink_message_set_flags(req
, NLM_F_REQUEST
| NLM_F_ACK
);
196 r
= sd_netlink_message_open_container(req
, IFLA_LINKINFO
);
200 r
= sd_netlink_message_open_container_union(req
, IFLA_INFO_DATA
, "bond");
204 if (link
->network
->active_slave
) {
205 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_ACTIVE_SLAVE
, link
->ifindex
);
210 if (link
->network
->primary_slave
) {
211 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_PRIMARY
, link
->ifindex
);
216 r
= sd_netlink_message_close_container(req
);
220 r
= sd_netlink_message_close_container(req
);
225 case REQUEST_TYPE_SET_LINK_BRIDGE
:
226 r
= sd_rtnl_message_link_set_family(req
, AF_BRIDGE
);
230 r
= sd_netlink_message_open_container(req
, IFLA_PROTINFO
);
234 if (link
->network
->use_bpdu
>= 0) {
235 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_GUARD
, !link
->network
->use_bpdu
);
240 if (link
->network
->hairpin
>= 0) {
241 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MODE
, link
->network
->hairpin
);
246 if (link
->network
->isolated
>= 0) {
247 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_ISOLATED
, link
->network
->isolated
);
252 if (link
->network
->fast_leave
>= 0) {
253 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_FAST_LEAVE
, link
->network
->fast_leave
);
258 if (link
->network
->allow_port_to_be_root
>= 0) {
259 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_PROTECT
, !link
->network
->allow_port_to_be_root
);
264 if (link
->network
->unicast_flood
>= 0) {
265 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_UNICAST_FLOOD
, link
->network
->unicast_flood
);
270 if (link
->network
->multicast_flood
>= 0) {
271 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MCAST_FLOOD
, link
->network
->multicast_flood
);
276 if (link
->network
->multicast_to_unicast
>= 0) {
277 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MCAST_TO_UCAST
, link
->network
->multicast_to_unicast
);
282 if (link
->network
->neighbor_suppression
>= 0) {
283 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_NEIGH_SUPPRESS
, link
->network
->neighbor_suppression
);
288 if (link
->network
->learning
>= 0) {
289 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_LEARNING
, link
->network
->learning
);
294 if (link
->network
->bridge_proxy_arp
>= 0) {
295 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_PROXYARP
, link
->network
->bridge_proxy_arp
);
300 if (link
->network
->bridge_proxy_arp_wifi
>= 0) {
301 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_PROXYARP_WIFI
, link
->network
->bridge_proxy_arp_wifi
);
306 if (link
->network
->cost
!= 0) {
307 r
= sd_netlink_message_append_u32(req
, IFLA_BRPORT_COST
, link
->network
->cost
);
312 if (link
->network
->priority
!= LINK_BRIDGE_PORT_PRIORITY_INVALID
) {
313 r
= sd_netlink_message_append_u16(req
, IFLA_BRPORT_PRIORITY
, link
->network
->priority
);
318 if (link
->network
->multicast_router
!= _MULTICAST_ROUTER_INVALID
) {
319 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MULTICAST_ROUTER
, link
->network
->multicast_router
);
324 r
= sd_netlink_message_close_container(req
);
328 case REQUEST_TYPE_SET_LINK_BRIDGE_VLAN
:
329 r
= sd_rtnl_message_link_set_family(req
, AF_BRIDGE
);
333 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
337 if (link
->master_ifindex
<= 0) {
338 /* master needs BRIDGE_FLAGS_SELF flag */
339 r
= sd_netlink_message_append_u16(req
, IFLA_BRIDGE_FLAGS
, BRIDGE_FLAGS_SELF
);
344 r
= bridge_vlan_append_info(link
, req
, link
->network
->pvid
, link
->network
->br_vid_bitmap
, link
->network
->br_untagged_bitmap
);
348 r
= sd_netlink_message_close_container(req
);
353 case REQUEST_TYPE_SET_LINK_CAN
:
354 r
= can_set_netlink_message(link
, req
);
358 case REQUEST_TYPE_SET_LINK_FLAGS
: {
359 unsigned ifi_change
= 0, ifi_flags
= 0;
361 if (link
->network
->arp
>= 0) {
362 ifi_change
|= IFF_NOARP
;
363 SET_FLAG(ifi_flags
, IFF_NOARP
, link
->network
->arp
== 0);
366 if (link
->network
->multicast
>= 0) {
367 ifi_change
|= IFF_MULTICAST
;
368 SET_FLAG(ifi_flags
, IFF_MULTICAST
, link
->network
->multicast
);
371 if (link
->network
->allmulticast
>= 0) {
372 ifi_change
|= IFF_ALLMULTI
;
373 SET_FLAG(ifi_flags
, IFF_ALLMULTI
, link
->network
->allmulticast
);
376 if (link
->network
->promiscuous
>= 0) {
377 ifi_change
|= IFF_PROMISC
;
378 SET_FLAG(ifi_flags
, IFF_PROMISC
, link
->network
->promiscuous
);
381 r
= sd_rtnl_message_link_set_flags(req
, ifi_flags
, ifi_change
);
387 case REQUEST_TYPE_SET_LINK_GROUP
:
388 r
= sd_netlink_message_append_u32(req
, IFLA_GROUP
, (uint32_t) link
->network
->group
);
392 case REQUEST_TYPE_SET_LINK_MAC
:
393 r
= netlink_message_append_hw_addr(req
, IFLA_ADDRESS
, &link
->requested_hw_addr
);
397 case REQUEST_TYPE_SET_LINK_IPOIB
:
398 r
= ipoib_set_netlink_message(link
, req
);
402 case REQUEST_TYPE_SET_LINK_MASTER
:
403 r
= sd_netlink_message_append_u32(req
, IFLA_MASTER
, PTR_TO_UINT32(userdata
));
407 case REQUEST_TYPE_SET_LINK_MTU
:
408 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, PTR_TO_UINT32(userdata
));
413 assert_not_reached();
419 static int link_configure(Link
*link
, Request
*req
) {
420 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
424 assert(link
->manager
);
427 log_link_debug(link
, "Setting %s", request_type_to_string(req
->type
));
429 if (req
->type
== REQUEST_TYPE_SET_LINK_BOND
)
430 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &m
, RTM_NEWLINK
, link
->master_ifindex
);
431 else if (IN_SET(req
->type
, REQUEST_TYPE_SET_LINK_CAN
, REQUEST_TYPE_SET_LINK_IPOIB
))
432 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &m
, RTM_NEWLINK
, link
->ifindex
);
434 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &m
, RTM_SETLINK
, link
->ifindex
);
438 r
= link_configure_fill_message(link
, m
, req
->type
, req
->userdata
);
442 return request_call_netlink_async(link
->manager
->rtnl
, m
, req
);
445 static bool netdev_is_ready(NetDev
*netdev
) {
448 if (netdev
->state
!= NETDEV_STATE_READY
)
450 if (netdev
->ifindex
== 0)
456 static int link_is_ready_to_set_link(Link
*link
, Request
*req
) {
460 assert(link
->manager
);
461 assert(link
->network
);
464 if (!IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
))
468 case REQUEST_TYPE_SET_LINK_BOND
:
469 case REQUEST_TYPE_SET_LINK_BRIDGE
:
470 if (!link
->master_set
)
473 if (link
->network
->keep_master
&& link
->master_ifindex
<= 0)
477 case REQUEST_TYPE_SET_LINK_BRIDGE_VLAN
:
478 if (!link
->master_set
)
481 if (link
->network
->keep_master
&& link
->master_ifindex
<= 0 && !streq_ptr(link
->kind
, "bridge"))
486 case REQUEST_TYPE_SET_LINK_CAN
:
487 /* Do not check link->set_flgas_messages here, as it is ok even if link->flags
488 * is outdated, and checking the counter causes a deadlock. */
489 if (FLAGS_SET(link
->flags
, IFF_UP
)) {
490 /* The CAN interface must be down to configure bitrate, etc... */
491 r
= link_down_now(link
);
497 case REQUEST_TYPE_SET_LINK_MAC
:
498 if (req
->netlink_handler
== link_set_mac_handler
) {
499 /* This is the second attempt to set hardware address. On the first attempt
500 * req->netlink_handler points to link_set_mac_allow_retry_handler().
501 * The first attempt failed as the interface was up. */
502 r
= link_down_now(link
);
508 case REQUEST_TYPE_SET_LINK_MASTER
: {
512 .type
= REQUEST_TYPE_SET_LINK_MAC
,
515 if (link
->network
->batadv
) {
516 if (!netdev_is_ready(link
->network
->batadv
))
518 m
= link
->network
->batadv
->ifindex
;
519 } else if (link
->network
->bond
) {
520 if (ordered_set_contains(link
->manager
->request_queue
, &req_mac
))
522 if (!netdev_is_ready(link
->network
->bond
))
524 m
= link
->network
->bond
->ifindex
;
526 /* Do not check link->set_flgas_messages here, as it is ok even if link->flags
527 * is outdated, and checking the counter causes a deadlock. */
528 if (FLAGS_SET(link
->flags
, IFF_UP
)) {
529 /* link must be down when joining to bond master. */
530 r
= link_down_now(link
);
534 } else if (link
->network
->bridge
) {
535 if (ordered_set_contains(link
->manager
->request_queue
, &req_mac
))
537 if (!netdev_is_ready(link
->network
->bridge
))
539 m
= link
->network
->bridge
->ifindex
;
540 } else if (link
->network
->vrf
) {
541 if (!netdev_is_ready(link
->network
->vrf
))
543 m
= link
->network
->vrf
->ifindex
;
546 if (m
== (uint32_t) link
->master_ifindex
) {
547 /* The requested master is already set. */
548 link
->master_set
= true;
549 return -EALREADY
; /* indicate to cancel the request. */
552 req
->userdata
= UINT32_TO_PTR(m
);
555 case REQUEST_TYPE_SET_LINK_MTU
: {
556 Request req_ipoib
= {
558 .type
= REQUEST_TYPE_SET_LINK_IPOIB
,
561 return !ordered_set_contains(link
->manager
->request_queue
, &req_ipoib
);
570 static int link_process_set_link(Request
*req
, Link
*link
, void *userdata
) {
576 r
= link_is_ready_to_set_link(link
, req
);
578 return 1; /* Cancel the request. */
582 r
= link_configure(link
, req
);
584 return log_link_warning_errno(link
, r
, "Failed to set %s", request_type_to_string(req
->type
));
589 static int link_request_set_link(
592 request_netlink_handler_t netlink_handler
,
600 r
= link_queue_request_full(link
, type
, NULL
, NULL
, NULL
, NULL
,
601 link_process_set_link
,
602 &link
->set_link_messages
,
606 return log_link_warning_errno(link
, r
, "Failed to request to set %s: %m",
607 request_type_to_string(type
));
609 log_link_debug(link
, "Requested to set %s", request_type_to_string(type
));
616 int link_request_to_set_addrgen_mode(Link
*link
) {
617 IPv6LinkLocalAddressGenMode mode
;
622 assert(link
->network
);
624 if (!socket_ipv6_is_supported())
627 mode
= link_get_ipv6ll_addrgen_mode(link
);
629 if (mode
== link
->ipv6ll_address_gen_mode
)
632 /* If the link is already up, then changing the mode by netlink does not take effect until the
633 * link goes down. Hence, we need to reset the interface. However, setting the mode by sysctl
634 * does not need that. Let's use the sysctl interface when the link is already up.
635 * See also issue #22424. */
636 if (mode
!= IPV6_LINK_LOCAL_ADDRESSS_GEN_MODE_NONE
&&
637 FLAGS_SET(link
->flags
, IFF_UP
)) {
638 r
= link_set_ipv6ll_addrgen_mode(link
, mode
);
640 log_link_warning_errno(link
, r
, "Cannot set IPv6 address generation mode, ignoring: %m");
645 r
= link_request_set_link(link
, REQUEST_TYPE_SET_LINK_ADDRESS_GENERATION_MODE
,
646 link_set_addrgen_mode_handler
,
651 req
->userdata
= UINT8_TO_PTR(mode
);
655 int link_request_to_set_bond(Link
*link
) {
657 assert(link
->network
);
659 if (!link
->network
->bond
) {
662 if (!link
->network
->keep_master
)
665 if (link_get_master(link
, &master
) < 0)
668 if (!streq_ptr(master
->kind
, "bond"))
672 return link_request_set_link(link
, REQUEST_TYPE_SET_LINK_BOND
,
673 link_set_bond_handler
, NULL
);
676 int link_request_to_set_bridge(Link
*link
) {
678 assert(link
->network
);
680 if (!link
->network
->bridge
) {
683 if (!link
->network
->keep_master
)
686 if (link_get_master(link
, &master
) < 0)
689 if (!streq_ptr(master
->kind
, "bridge"))
693 return link_request_set_link(link
, REQUEST_TYPE_SET_LINK_BRIDGE
,
694 link_set_bridge_handler
,
698 int link_request_to_set_bridge_vlan(Link
*link
) {
700 assert(link
->network
);
702 if (!link
->network
->use_br_vlan
)
705 if (!link
->network
->bridge
&& !streq_ptr(link
->kind
, "bridge")) {
708 if (!link
->network
->keep_master
)
711 if (link_get_master(link
, &master
) < 0)
714 if (!streq_ptr(master
->kind
, "bridge"))
718 return link_request_set_link(link
, REQUEST_TYPE_SET_LINK_BRIDGE_VLAN
,
719 link_set_bridge_vlan_handler
,
723 int link_request_to_set_can(Link
*link
) {
725 assert(link
->network
);
727 if (link
->iftype
!= ARPHRD_CAN
)
730 if (!streq_ptr(link
->kind
, "can"))
733 return link_request_set_link(link
, REQUEST_TYPE_SET_LINK_CAN
,
734 link_set_can_handler
,
738 int link_request_to_set_flags(Link
*link
) {
740 assert(link
->network
);
742 if (link
->network
->arp
< 0 &&
743 link
->network
->multicast
< 0 &&
744 link
->network
->allmulticast
< 0 &&
745 link
->network
->promiscuous
< 0)
748 return link_request_set_link(link
, REQUEST_TYPE_SET_LINK_FLAGS
,
749 link_set_flags_handler
,
753 int link_request_to_set_group(Link
*link
) {
755 assert(link
->network
);
757 if (link
->network
->group
< 0)
760 return link_request_set_link(link
, REQUEST_TYPE_SET_LINK_GROUP
,
761 link_set_group_handler
,
765 int link_request_to_set_mac(Link
*link
, bool allow_retry
) {
769 assert(link
->network
);
771 if (link
->network
->hw_addr
.length
== 0)
774 link
->requested_hw_addr
= link
->network
->hw_addr
;
775 r
= net_verify_hardware_address(link
->ifname
, /* is_static = */ true,
776 link
->iftype
, &link
->hw_addr
, &link
->requested_hw_addr
);
780 if (hw_addr_equal(&link
->hw_addr
, &link
->requested_hw_addr
))
783 return link_request_set_link(link
, REQUEST_TYPE_SET_LINK_MAC
,
784 allow_retry
? link_set_mac_allow_retry_handler
: link_set_mac_handler
,
788 int link_request_to_set_ipoib(Link
*link
) {
790 assert(link
->network
);
792 if (link
->iftype
!= ARPHRD_INFINIBAND
)
795 if (link
->network
->ipoib_mode
< 0 &&
796 link
->network
->ipoib_umcast
< 0)
799 return link_request_set_link(link
, REQUEST_TYPE_SET_LINK_IPOIB
,
800 link_set_ipoib_handler
,
804 int link_request_to_set_master(Link
*link
) {
806 assert(link
->network
);
808 if (link
->network
->keep_master
) {
809 /* When KeepMaster=yes, BatmanAdvanced=, Bond=, Bridge=, and VRF= are ignored. */
810 link
->master_set
= true;
813 } else if (link
->network
->batadv
|| link
->network
->bond
|| link
->network
->bridge
|| link
->network
->vrf
) {
814 link
->master_set
= false;
815 return link_request_set_link(link
, REQUEST_TYPE_SET_LINK_MASTER
,
816 link_set_master_handler
,
819 } else if (link
->master_ifindex
!= 0) {
820 /* Unset master only when it is set. */
821 link
->master_set
= false;
822 return link_request_set_link(link
, REQUEST_TYPE_SET_LINK_MASTER
,
823 link_unset_master_handler
,
827 /* Nothing we need to do. */
828 link
->master_set
= true;
833 int link_request_to_set_mtu(Link
*link
, uint32_t mtu
) {
840 assert(link
->network
);
842 min_mtu
= link
->min_mtu
;
843 origin
= "the minimum MTU of the interface";
844 if (link_ipv6_enabled(link
)) {
845 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes on the interface. Bump up
846 * MTU bytes to IPV6_MTU_MIN. */
847 if (min_mtu
< IPV6_MIN_MTU
) {
848 min_mtu
= IPV6_MIN_MTU
;
849 origin
= "the minimum IPv6 MTU";
851 if (min_mtu
< link
->network
->ipv6_mtu
) {
852 min_mtu
= link
->network
->ipv6_mtu
;
853 origin
= "the requested IPv6 MTU in IPv6MTUBytes=";
858 log_link_warning(link
, "Bumping the requested MTU %"PRIu32
" to %s (%"PRIu32
")",
859 mtu
, origin
, min_mtu
);
863 if (mtu
> link
->max_mtu
) {
864 log_link_warning(link
, "Reducing the requested MTU %"PRIu32
" to the interface's maximum MTU %"PRIu32
".",
869 if (link
->mtu
== mtu
)
872 r
= link_request_set_link(link
, REQUEST_TYPE_SET_LINK_MTU
,
873 link_set_mtu_handler
,
878 req
->userdata
= UINT32_TO_PTR(mtu
);
882 static bool link_reduces_vlan_mtu(Link
*link
) {
883 /* See netif_reduces_vlan_mtu() in kernel. */
884 return streq_ptr(link
->kind
, "macsec");
887 static uint32_t link_get_requested_mtu_by_stacked_netdevs(Link
*link
) {
891 HASHMAP_FOREACH(dev
, link
->network
->stacked_netdevs
)
892 if (dev
->kind
== NETDEV_KIND_VLAN
&& dev
->mtu
> 0)
893 /* See vlan_dev_change_mtu() in kernel. */
894 mtu
= MAX(mtu
, link_reduces_vlan_mtu(link
) ? dev
->mtu
+ 4 : dev
->mtu
);
896 else if (dev
->kind
== NETDEV_KIND_MACVLAN
&& dev
->mtu
> mtu
)
897 /* See macvlan_change_mtu() in kernel. */
903 int link_configure_mtu(Link
*link
) {
907 assert(link
->network
);
909 if (link
->network
->mtu
> 0)
910 return link_request_to_set_mtu(link
, link
->network
->mtu
);
912 mtu
= link_get_requested_mtu_by_stacked_netdevs(link
);
913 if (link
->mtu
>= mtu
)
916 log_link_notice(link
, "Bumping MTU bytes from %"PRIu32
" to %"PRIu32
" because of stacked device. "
917 "If it is not desired, then please explicitly specify MTUBytes= setting.",
920 return link_request_to_set_mtu(link
, mtu
);
923 static int link_up_dsa_slave(Link
*link
) {
929 /* For older kernels (specifically, older than 9d5ef190e5615a7b63af89f88c4106a5bc127974, kernel-5.12),
930 * it is necessary to bring up a DSA slave that its master interface is already up. And bringing up
931 * the slave fails with -ENETDOWN. So, let's bring up the master even if it is not managed by us,
932 * and try to bring up the slave after the master becomes up. */
934 if (link
->dsa_master_ifindex
<= 0)
937 if (!streq_ptr(link
->driver
, "dsa"))
940 if (link_get_by_index(link
->manager
, link
->dsa_master_ifindex
, &master
) < 0)
943 if (master
->state
== LINK_STATE_UNMANAGED
) {
944 /* If the DSA master interface is unmanaged, then it will never become up.
945 * Let's request to bring up the master. */
946 r
= link_request_to_bring_up_or_down(master
, /* up = */ true);
951 r
= link_request_to_bring_up_or_down(link
, /* up = */ true);
958 static int link_up_or_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Request
*req
, Link
*link
, void *userdata
) {
959 bool on_activate
, up
;
966 on_activate
= req
->type
== REQUEST_TYPE_ACTIVATE_LINK
;
967 up
= PTR_TO_INT(req
->userdata
);
969 r
= sd_netlink_message_get_errno(m
);
970 if (r
== -ENETDOWN
&& up
&& link_up_dsa_slave(link
) > 0)
971 log_link_message_debug_errno(link
, m
, r
, "Could not bring up dsa slave, retrying again after dsa master becomes up");
973 log_link_message_warning_errno(link
, m
, r
, up
?
974 "Could not bring up interface, ignoring" :
975 "Could not bring down interface, ignoring");
977 r
= link_call_getlink(link
, get_link_update_flag_handler
);
979 link_enter_failed(link
);
983 link
->set_flags_messages
++;
986 link
->activated
= true;
987 link_check_ready(link
);
993 static const char *up_or_down(bool up
) {
994 return up
? "up" : "down";
997 static int link_up_or_down(Link
*link
, bool up
, Request
*req
) {
998 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
1002 assert(link
->manager
);
1003 assert(link
->manager
->rtnl
);
1006 log_link_debug(link
, "Bringing link %s", up_or_down(up
));
1008 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &m
, RTM_SETLINK
, link
->ifindex
);
1012 r
= sd_rtnl_message_link_set_flags(m
, up
? IFF_UP
: 0, IFF_UP
);
1016 return request_call_netlink_async(link
->manager
->rtnl
, m
, req
);
1019 static bool link_is_ready_to_activate_one(Link
*link
, bool allow_unmanaged
) {
1022 if (!IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
, LINK_STATE_UNMANAGED
))
1026 return allow_unmanaged
;
1028 if (link
->set_link_messages
> 0)
1034 static bool link_is_ready_to_activate(Link
*link
, bool up
) {
1037 if (!check_ready_for_all_sr_iov_ports(link
, /* allow_unmanaged = */ false,
1038 link_is_ready_to_activate_one
))
1041 if (up
&& link_rfkilled(link
) > 0)
1047 static int link_process_activation(Request
*req
, Link
*link
, void *userdata
) {
1048 bool up
= PTR_TO_INT(userdata
);
1054 if (!link_is_ready_to_activate(link
, up
))
1057 r
= link_up_or_down(link
, up
, req
);
1059 return log_link_warning_errno(link
, r
, "Failed to activate link: %m");
1064 int link_request_to_activate(Link
*link
) {
1069 assert(link
->network
);
1071 switch (link
->network
->activation_policy
) {
1072 case ACTIVATION_POLICY_BOUND
:
1073 r
= link_handle_bound_to_list(link
);
1077 case ACTIVATION_POLICY_MANUAL
:
1078 link
->activated
= true;
1079 link_check_ready(link
);
1081 case ACTIVATION_POLICY_UP
:
1082 case ACTIVATION_POLICY_ALWAYS_UP
:
1085 case ACTIVATION_POLICY_DOWN
:
1086 case ACTIVATION_POLICY_ALWAYS_DOWN
:
1090 assert_not_reached();
1093 link
->activated
= false;
1095 r
= link_queue_request_full(link
, REQUEST_TYPE_ACTIVATE_LINK
,
1096 INT_TO_PTR(up
), NULL
, NULL
, NULL
,
1097 link_process_activation
,
1098 &link
->set_flags_messages
,
1099 link_up_or_down_handler
, NULL
);
1101 return log_link_error_errno(link
, r
, "Failed to request to activate link: %m");
1103 log_link_debug(link
, "Requested to activate link");
1107 static bool link_is_ready_to_bring_up_or_down(Link
*link
, bool up
) {
1110 if (up
&& link
->dsa_master_ifindex
> 0) {
1113 /* The master interface must be up. See comments in link_up_dsa_slave(). */
1115 if (link_get_by_index(link
->manager
, link
->dsa_master_ifindex
, &master
) < 0)
1118 if (!FLAGS_SET(master
->flags
, IFF_UP
))
1122 if (link
->state
== LINK_STATE_UNMANAGED
)
1125 if (!IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
))
1128 if (link
->set_link_messages
> 0)
1131 if (!link
->activated
)
1134 if (up
&& link_rfkilled(link
) > 0)
1140 static int link_process_up_or_down(Request
*req
, Link
*link
, void *userdata
) {
1141 bool up
= PTR_TO_INT(userdata
);
1147 if (!link_is_ready_to_bring_up_or_down(link
, up
))
1150 r
= link_up_or_down(link
, up
, req
);
1152 return log_link_warning_errno(link
, r
, "Failed to bring link %s: %m", up_or_down(up
));
1157 int link_request_to_bring_up_or_down(Link
*link
, bool up
) {
1162 r
= link_queue_request_full(link
, REQUEST_TYPE_UP_DOWN
,
1163 INT_TO_PTR(up
), NULL
, NULL
, NULL
,
1164 link_process_up_or_down
,
1165 &link
->set_flags_messages
,
1166 link_up_or_down_handler
, NULL
);
1168 return log_link_warning_errno(link
, r
, "Failed to request to bring link %s: %m",
1171 log_link_debug(link
, "Requested to bring link %s", up_or_down(up
));
1175 static int link_down_now_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1180 assert(link
->set_flags_messages
> 0);
1182 link
->set_flags_messages
--;
1184 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1187 r
= sd_netlink_message_get_errno(m
);
1189 log_link_message_warning_errno(link
, m
, r
, "Could not bring down interface, ignoring");
1191 r
= link_call_getlink(link
, get_link_update_flag_handler
);
1193 link_enter_failed(link
);
1197 link
->set_flags_messages
++;
1201 int link_down_now(Link
*link
) {
1202 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1206 assert(link
->manager
);
1207 assert(link
->manager
->rtnl
);
1209 log_link_debug(link
, "Bringing link down");
1211 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1213 return log_link_warning_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1215 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
1217 return log_link_warning_errno(link
, r
, "Could not set link flags: %m");
1219 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_down_now_handler
,
1220 link_netlink_destroy_callback
, link
);
1222 return log_link_warning_errno(link
, r
, "Could not send rtnetlink message: %m");
1224 link
->set_flags_messages
++;
1229 static int link_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1235 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1238 r
= sd_netlink_message_get_errno(m
);
1240 log_link_message_warning_errno(link
, m
, r
, "Could not remove interface, ignoring");
1245 int link_remove(Link
*link
) {
1246 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1250 assert(link
->manager
);
1251 assert(link
->manager
->rtnl
);
1253 log_link_debug(link
, "Removing link.");
1255 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_DELLINK
, link
->ifindex
);
1257 return log_link_debug_errno(link
, r
, "Could not allocate RTM_DELLINK message: %m");
1259 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_remove_handler
,
1260 link_netlink_destroy_callback
, link
);
1262 return log_link_debug_errno(link
, r
, "Could not send rtnetlink message: %m");