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
) {
110 r
= set_link_handler_internal(rtnl
, m
, req
, link
, /* ignore = */ false, NULL
);
114 link
->bridge_vlan_set
= true;
118 static int link_del_bridge_vlan_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Request
*req
, Link
*link
, void *userdata
) {
119 return set_link_handler_internal(rtnl
, m
, req
, link
, /* ignore = */ false, NULL
);
122 static int link_set_can_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Request
*req
, Link
*link
, void *userdata
) {
123 return set_link_handler_internal(rtnl
, m
, req
, link
, /* ignore = */ false, NULL
);
126 static int link_set_flags_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Request
*req
, Link
*link
, void *userdata
) {
127 return set_link_handler_internal(rtnl
, m
, req
, link
, /* ignore = */ false, get_link_default_handler
);
130 static int link_set_group_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Request
*req
, Link
*link
, void *userdata
) {
131 return set_link_handler_internal(rtnl
, m
, req
, link
, /* ignore = */ false, NULL
);
134 static int link_set_ipoib_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Request
*req
, Link
*link
, void *userdata
) {
135 return set_link_handler_internal(rtnl
, m
, req
, link
, /* ignore = */ true, NULL
);
138 static int link_set_mac_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Request
*req
, Link
*link
, void *userdata
) {
139 return set_link_handler_internal(rtnl
, m
, req
, link
, /* ignore = */ true, get_link_default_handler
);
142 static int link_set_mac_allow_retry_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Request
*req
, Link
*link
, void *userdata
) {
148 r
= sd_netlink_message_get_errno(m
);
150 /* Most real network devices refuse to set its hardware address with -EBUSY when its
151 * operstate is not down. See, eth_prepare_mac_addr_change() in net/ethernet/eth.c
154 log_link_message_debug_errno(link
, m
, r
, "Failed to set MAC address, retrying again: %m");
156 r
= link_request_to_set_mac(link
, /* allow_retry = */ false);
158 link_enter_failed(link
);
163 return link_set_mac_handler(rtnl
, m
, req
, link
, userdata
);
166 static int link_set_master_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Request
*req
, Link
*link
, void *userdata
) {
167 return set_link_handler_internal(rtnl
, m
, req
, link
, /* ignore = */ false, get_link_master_handler
);
170 static int link_unset_master_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Request
*req
, Link
*link
, void *userdata
) {
171 /* Some devices do not support setting master ifindex. Let's ignore error on unsetting master ifindex. */
172 return set_link_handler_internal(rtnl
, m
, req
, link
, /* ignore = */ true, get_link_master_handler
);
175 static int link_set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Request
*req
, Link
*link
, void *userdata
) {
178 r
= set_link_handler_internal(rtnl
, m
, req
, link
, /* ignore = */ true, get_link_default_handler
);
182 /* The kernel resets ipv6 mtu after changing device mtu;
183 * we must set this here, after we've set device mtu */
184 r
= link_set_ipv6_mtu(link
);
186 log_link_warning_errno(link
, r
, "Failed to set IPv6 MTU, ignoring: %m");
191 static int link_configure_fill_message(
193 sd_netlink_message
*req
,
199 case REQUEST_TYPE_SET_LINK_ADDRESS_GENERATION_MODE
:
200 r
= ipv6ll_addrgen_mode_fill_message(req
, PTR_TO_UINT8(userdata
));
204 case REQUEST_TYPE_SET_LINK_BOND
:
205 r
= sd_netlink_message_set_flags(req
, NLM_F_REQUEST
| NLM_F_ACK
);
209 r
= sd_netlink_message_open_container(req
, IFLA_LINKINFO
);
213 r
= sd_netlink_message_open_container_union(req
, IFLA_INFO_DATA
, "bond");
217 if (link
->network
->active_slave
) {
218 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_ACTIVE_SLAVE
, link
->ifindex
);
223 if (link
->network
->primary_slave
) {
224 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_PRIMARY
, link
->ifindex
);
229 r
= sd_netlink_message_close_container(req
);
233 r
= sd_netlink_message_close_container(req
);
238 case REQUEST_TYPE_SET_LINK_BRIDGE
:
239 r
= sd_rtnl_message_link_set_family(req
, AF_BRIDGE
);
243 r
= sd_netlink_message_open_container(req
, IFLA_PROTINFO
);
247 if (link
->network
->use_bpdu
>= 0) {
248 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_GUARD
, !link
->network
->use_bpdu
);
253 if (link
->network
->hairpin
>= 0) {
254 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MODE
, link
->network
->hairpin
);
259 if (link
->network
->isolated
>= 0) {
260 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_ISOLATED
, link
->network
->isolated
);
265 if (link
->network
->fast_leave
>= 0) {
266 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_FAST_LEAVE
, link
->network
->fast_leave
);
271 if (link
->network
->allow_port_to_be_root
>= 0) {
272 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_PROTECT
, !link
->network
->allow_port_to_be_root
);
277 if (link
->network
->unicast_flood
>= 0) {
278 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_UNICAST_FLOOD
, link
->network
->unicast_flood
);
283 if (link
->network
->multicast_flood
>= 0) {
284 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MCAST_FLOOD
, link
->network
->multicast_flood
);
289 if (link
->network
->multicast_to_unicast
>= 0) {
290 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MCAST_TO_UCAST
, link
->network
->multicast_to_unicast
);
295 if (link
->network
->neighbor_suppression
>= 0) {
296 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_NEIGH_SUPPRESS
, link
->network
->neighbor_suppression
);
301 if (link
->network
->learning
>= 0) {
302 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_LEARNING
, link
->network
->learning
);
307 if (link
->network
->bridge_proxy_arp
>= 0) {
308 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_PROXYARP
, link
->network
->bridge_proxy_arp
);
313 if (link
->network
->bridge_proxy_arp_wifi
>= 0) {
314 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_PROXYARP_WIFI
, link
->network
->bridge_proxy_arp_wifi
);
319 if (link
->network
->cost
!= 0) {
320 r
= sd_netlink_message_append_u32(req
, IFLA_BRPORT_COST
, link
->network
->cost
);
325 if (link
->network
->priority
!= LINK_BRIDGE_PORT_PRIORITY_INVALID
) {
326 r
= sd_netlink_message_append_u16(req
, IFLA_BRPORT_PRIORITY
, link
->network
->priority
);
331 if (link
->network
->multicast_router
!= _MULTICAST_ROUTER_INVALID
) {
332 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MULTICAST_ROUTER
, link
->network
->multicast_router
);
337 r
= sd_netlink_message_close_container(req
);
341 case REQUEST_TYPE_SET_LINK_BRIDGE_VLAN
:
342 r
= bridge_vlan_set_message(link
, req
, /* is_set = */ true);
346 case REQUEST_TYPE_DEL_LINK_BRIDGE_VLAN
:
347 r
= bridge_vlan_set_message(link
, req
, /* is_set = */ false);
351 case REQUEST_TYPE_SET_LINK_CAN
:
352 r
= can_set_netlink_message(link
, req
);
356 case REQUEST_TYPE_SET_LINK_FLAGS
: {
357 unsigned ifi_change
= 0, ifi_flags
= 0;
359 if (link
->network
->arp
>= 0) {
360 ifi_change
|= IFF_NOARP
;
361 SET_FLAG(ifi_flags
, IFF_NOARP
, link
->network
->arp
== 0);
364 if (link
->network
->multicast
>= 0) {
365 ifi_change
|= IFF_MULTICAST
;
366 SET_FLAG(ifi_flags
, IFF_MULTICAST
, link
->network
->multicast
);
369 if (link
->network
->allmulticast
>= 0) {
370 ifi_change
|= IFF_ALLMULTI
;
371 SET_FLAG(ifi_flags
, IFF_ALLMULTI
, link
->network
->allmulticast
);
374 if (link
->network
->promiscuous
>= 0) {
375 ifi_change
|= IFF_PROMISC
;
376 SET_FLAG(ifi_flags
, IFF_PROMISC
, link
->network
->promiscuous
);
379 r
= sd_rtnl_message_link_set_flags(req
, ifi_flags
, ifi_change
);
385 case REQUEST_TYPE_SET_LINK_GROUP
:
386 r
= sd_netlink_message_append_u32(req
, IFLA_GROUP
, (uint32_t) link
->network
->group
);
390 case REQUEST_TYPE_SET_LINK_MAC
:
391 r
= netlink_message_append_hw_addr(req
, IFLA_ADDRESS
, &link
->requested_hw_addr
);
395 case REQUEST_TYPE_SET_LINK_IPOIB
:
396 r
= ipoib_set_netlink_message(link
, req
);
400 case REQUEST_TYPE_SET_LINK_MASTER
:
401 r
= sd_netlink_message_append_u32(req
, IFLA_MASTER
, PTR_TO_UINT32(userdata
));
405 case REQUEST_TYPE_SET_LINK_MTU
:
406 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, PTR_TO_UINT32(userdata
));
411 assert_not_reached();
417 static int link_configure(Link
*link
, Request
*req
) {
418 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
422 assert(link
->manager
);
425 log_link_debug(link
, "Setting %s", request_type_to_string(req
->type
));
427 if (req
->type
== REQUEST_TYPE_SET_LINK_BOND
)
428 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &m
, RTM_NEWLINK
, link
->master_ifindex
);
429 else if (IN_SET(req
->type
, REQUEST_TYPE_SET_LINK_CAN
, REQUEST_TYPE_SET_LINK_IPOIB
))
430 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &m
, RTM_NEWLINK
, link
->ifindex
);
431 else if (req
->type
== REQUEST_TYPE_DEL_LINK_BRIDGE_VLAN
)
432 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &m
, RTM_DELLINK
, 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"))
485 case REQUEST_TYPE_DEL_LINK_BRIDGE_VLAN
:
486 return link
->bridge_vlan_set
;
488 case REQUEST_TYPE_SET_LINK_CAN
:
489 /* Do not check link->set_flgas_messages here, as it is ok even if link->flags
490 * is outdated, and checking the counter causes a deadlock. */
491 if (FLAGS_SET(link
->flags
, IFF_UP
)) {
492 /* The CAN interface must be down to configure bitrate, etc... */
493 r
= link_down_now(link
);
499 case REQUEST_TYPE_SET_LINK_MAC
:
500 if (req
->netlink_handler
== link_set_mac_handler
) {
501 /* This is the second attempt to set hardware address. On the first attempt
502 * req->netlink_handler points to link_set_mac_allow_retry_handler().
503 * The first attempt failed as the interface was up. */
504 r
= link_down_now(link
);
508 /* If the kind of the link is "bond", we need
509 * set the slave link down as well. */
510 if (streq_ptr(link
->kind
, "bond")) {
511 r
= link_down_slave_links(link
);
518 case REQUEST_TYPE_SET_LINK_MASTER
: {
522 .type
= REQUEST_TYPE_SET_LINK_MAC
,
525 if (link
->network
->batadv
) {
526 if (!netdev_is_ready(link
->network
->batadv
))
528 m
= link
->network
->batadv
->ifindex
;
529 } else if (link
->network
->bond
) {
530 if (ordered_set_contains(link
->manager
->request_queue
, &req_mac
))
532 if (!netdev_is_ready(link
->network
->bond
))
534 m
= link
->network
->bond
->ifindex
;
536 /* Do not check link->set_flgas_messages here, as it is ok even if link->flags
537 * is outdated, and checking the counter causes a deadlock. */
538 if (FLAGS_SET(link
->flags
, IFF_UP
)) {
539 /* link must be down when joining to bond master. */
540 r
= link_down_now(link
);
544 } else if (link
->network
->bridge
) {
545 if (ordered_set_contains(link
->manager
->request_queue
, &req_mac
))
547 if (!netdev_is_ready(link
->network
->bridge
))
549 m
= link
->network
->bridge
->ifindex
;
550 } else if (link
->network
->vrf
) {
551 if (!netdev_is_ready(link
->network
->vrf
))
553 m
= link
->network
->vrf
->ifindex
;
556 if (m
== (uint32_t) link
->master_ifindex
) {
557 /* The requested master is already set. */
558 link
->master_set
= true;
559 return -EALREADY
; /* indicate to cancel the request. */
562 req
->userdata
= UINT32_TO_PTR(m
);
565 case REQUEST_TYPE_SET_LINK_MTU
: {
566 if (ordered_set_contains(link
->manager
->request_queue
,
569 .type
= REQUEST_TYPE_SET_LINK_IPOIB
,
573 /* Changing FD mode may affect MTU. */
574 if (ordered_set_contains(link
->manager
->request_queue
,
577 .type
= REQUEST_TYPE_SET_LINK_CAN
,
588 static int link_process_set_link(Request
*req
, Link
*link
, void *userdata
) {
594 r
= link_is_ready_to_set_link(link
, req
);
596 return 1; /* Cancel the request. */
600 r
= link_configure(link
, req
);
602 return log_link_warning_errno(link
, r
, "Failed to set %s", request_type_to_string(req
->type
));
607 static int link_request_set_link(
610 request_netlink_handler_t netlink_handler
,
618 r
= link_queue_request_full(link
, type
, NULL
, NULL
, NULL
, NULL
,
619 link_process_set_link
,
620 &link
->set_link_messages
,
624 return log_link_warning_errno(link
, r
, "Failed to request to set %s: %m",
625 request_type_to_string(type
));
627 log_link_debug(link
, "Requested to set %s", request_type_to_string(type
));
634 int link_request_to_set_addrgen_mode(Link
*link
) {
635 IPv6LinkLocalAddressGenMode mode
;
640 assert(link
->network
);
642 if (!socket_ipv6_is_supported())
645 mode
= link_get_ipv6ll_addrgen_mode(link
);
647 if (mode
== link
->ipv6ll_address_gen_mode
)
650 /* If the link is already up, then changing the mode by netlink does not take effect until the
651 * link goes down. Hence, we need to reset the interface. However, setting the mode by sysctl
652 * does not need that. Let's use the sysctl interface when the link is already up.
653 * See also issue #22424. */
654 if (mode
!= IPV6_LINK_LOCAL_ADDRESSS_GEN_MODE_NONE
&&
655 FLAGS_SET(link
->flags
, IFF_UP
)) {
656 r
= link_set_ipv6ll_addrgen_mode(link
, mode
);
658 log_link_warning_errno(link
, r
, "Cannot set IPv6 address generation mode, ignoring: %m");
663 r
= link_request_set_link(link
, REQUEST_TYPE_SET_LINK_ADDRESS_GENERATION_MODE
,
664 link_set_addrgen_mode_handler
,
669 req
->userdata
= UINT8_TO_PTR(mode
);
673 int link_request_to_set_bond(Link
*link
) {
675 assert(link
->network
);
677 if (!link
->network
->bond
) {
680 if (!link
->network
->keep_master
)
683 if (link_get_master(link
, &master
) < 0)
686 if (!streq_ptr(master
->kind
, "bond"))
690 return link_request_set_link(link
, REQUEST_TYPE_SET_LINK_BOND
,
691 link_set_bond_handler
, NULL
);
694 int link_request_to_set_bridge(Link
*link
) {
696 assert(link
->network
);
698 if (!link
->network
->bridge
) {
701 if (!link
->network
->keep_master
)
704 if (link_get_master(link
, &master
) < 0)
707 if (!streq_ptr(master
->kind
, "bridge"))
711 return link_request_set_link(link
, REQUEST_TYPE_SET_LINK_BRIDGE
,
712 link_set_bridge_handler
,
716 int link_request_to_set_bridge_vlan(Link
*link
) {
720 assert(link
->network
);
722 /* If nothing configured, use the default vlan ID. */
723 if (memeqzero(link
->network
->bridge_vlan_bitmap
, BRIDGE_VLAN_BITMAP_LEN
* sizeof(uint32_t)) &&
724 link
->network
->bridge_vlan_pvid
== BRIDGE_VLAN_KEEP_PVID
)
727 if (!link
->network
->bridge
&& !streq_ptr(link
->kind
, "bridge")) {
730 if (!link
->network
->keep_master
)
733 if (link_get_master(link
, &master
) < 0)
736 if (!streq_ptr(master
->kind
, "bridge"))
740 link
->bridge_vlan_set
= false;
742 r
= link_request_set_link(link
, REQUEST_TYPE_SET_LINK_BRIDGE_VLAN
,
743 link_set_bridge_vlan_handler
,
748 r
= link_request_set_link(link
, REQUEST_TYPE_DEL_LINK_BRIDGE_VLAN
,
749 link_del_bridge_vlan_handler
,
757 int link_request_to_set_can(Link
*link
) {
759 assert(link
->network
);
761 if (link
->iftype
!= ARPHRD_CAN
)
764 if (!streq_ptr(link
->kind
, "can"))
767 return link_request_set_link(link
, REQUEST_TYPE_SET_LINK_CAN
,
768 link_set_can_handler
,
772 int link_request_to_set_flags(Link
*link
) {
774 assert(link
->network
);
776 if (link
->network
->arp
< 0 &&
777 link
->network
->multicast
< 0 &&
778 link
->network
->allmulticast
< 0 &&
779 link
->network
->promiscuous
< 0)
782 return link_request_set_link(link
, REQUEST_TYPE_SET_LINK_FLAGS
,
783 link_set_flags_handler
,
787 int link_request_to_set_group(Link
*link
) {
789 assert(link
->network
);
791 if (link
->network
->group
< 0)
794 return link_request_set_link(link
, REQUEST_TYPE_SET_LINK_GROUP
,
795 link_set_group_handler
,
799 int link_request_to_set_mac(Link
*link
, bool allow_retry
) {
803 assert(link
->network
);
805 if (link
->network
->hw_addr
.length
== 0)
808 link
->requested_hw_addr
= link
->network
->hw_addr
;
809 r
= net_verify_hardware_address(link
->ifname
, /* is_static = */ true,
810 link
->iftype
, &link
->hw_addr
, &link
->requested_hw_addr
);
814 if (hw_addr_equal(&link
->hw_addr
, &link
->requested_hw_addr
))
817 return link_request_set_link(link
, REQUEST_TYPE_SET_LINK_MAC
,
818 allow_retry
? link_set_mac_allow_retry_handler
: link_set_mac_handler
,
822 int link_request_to_set_ipoib(Link
*link
) {
824 assert(link
->network
);
826 if (link
->iftype
!= ARPHRD_INFINIBAND
)
829 if (link
->network
->ipoib_mode
< 0 &&
830 link
->network
->ipoib_umcast
< 0)
833 return link_request_set_link(link
, REQUEST_TYPE_SET_LINK_IPOIB
,
834 link_set_ipoib_handler
,
838 int link_request_to_set_master(Link
*link
) {
840 assert(link
->network
);
842 if (link
->network
->keep_master
) {
843 /* When KeepMaster=yes, BatmanAdvanced=, Bond=, Bridge=, and VRF= are ignored. */
844 link
->master_set
= true;
847 } else if (link
->network
->batadv
|| link
->network
->bond
|| link
->network
->bridge
|| link
->network
->vrf
) {
848 link
->master_set
= false;
849 return link_request_set_link(link
, REQUEST_TYPE_SET_LINK_MASTER
,
850 link_set_master_handler
,
853 } else if (link
->master_ifindex
!= 0) {
854 /* Unset master only when it is set. */
855 link
->master_set
= false;
856 return link_request_set_link(link
, REQUEST_TYPE_SET_LINK_MASTER
,
857 link_unset_master_handler
,
861 /* Nothing we need to do. */
862 link
->master_set
= true;
867 int link_request_to_set_mtu(Link
*link
, uint32_t mtu
) {
869 uint32_t min_mtu
, max_mtu
;
874 assert(link
->network
);
876 min_mtu
= link
->min_mtu
;
877 origin
= "the minimum MTU of the interface";
878 if (link_ipv6_enabled(link
)) {
879 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes on the interface. Bump up
880 * MTU bytes to IPV6_MTU_MIN. */
881 if (min_mtu
< IPV6_MIN_MTU
) {
882 min_mtu
= IPV6_MIN_MTU
;
883 origin
= "the minimum IPv6 MTU";
885 if (min_mtu
< link
->network
->ipv6_mtu
) {
886 min_mtu
= link
->network
->ipv6_mtu
;
887 origin
= "the requested IPv6 MTU in IPv6MTUBytes=";
892 log_link_warning(link
, "Bumping the requested MTU %"PRIu32
" to %s (%"PRIu32
")",
893 mtu
, origin
, min_mtu
);
897 max_mtu
= link
->max_mtu
;
898 if (link
->iftype
== ARPHRD_CAN
)
899 /* The maximum MTU may be changed when FD mode is changed.
900 * See https://docs.kernel.org/networking/can.html#can-fd-flexible-data-rate-driver-support
901 * MTU = 16 (CAN_MTU) => Classical CAN device
902 * MTU = 72 (CANFD_MTU) => CAN FD capable device
903 * So, even if the current maximum is 16, we should not reduce the requested value now. */
904 max_mtu
= MAX(max_mtu
, 72u);
907 log_link_warning(link
, "Reducing the requested MTU %"PRIu32
" to the interface's maximum MTU %"PRIu32
".",
912 if (link
->mtu
== mtu
)
915 r
= link_request_set_link(link
, REQUEST_TYPE_SET_LINK_MTU
,
916 link_set_mtu_handler
,
921 req
->userdata
= UINT32_TO_PTR(mtu
);
925 static bool link_reduces_vlan_mtu(Link
*link
) {
926 /* See netif_reduces_vlan_mtu() in kernel. */
927 return streq_ptr(link
->kind
, "macsec");
930 static uint32_t link_get_requested_mtu_by_stacked_netdevs(Link
*link
) {
934 HASHMAP_FOREACH(dev
, link
->network
->stacked_netdevs
)
935 if (dev
->kind
== NETDEV_KIND_VLAN
&& dev
->mtu
> 0)
936 /* See vlan_dev_change_mtu() in kernel. */
937 mtu
= MAX(mtu
, link_reduces_vlan_mtu(link
) ? dev
->mtu
+ 4 : dev
->mtu
);
939 else if (dev
->kind
== NETDEV_KIND_MACVLAN
&& dev
->mtu
> mtu
)
940 /* See macvlan_change_mtu() in kernel. */
946 int link_configure_mtu(Link
*link
) {
950 assert(link
->network
);
952 if (link
->network
->mtu
> 0)
953 return link_request_to_set_mtu(link
, link
->network
->mtu
);
955 mtu
= link_get_requested_mtu_by_stacked_netdevs(link
);
956 if (link
->mtu
>= mtu
)
959 log_link_notice(link
, "Bumping MTU bytes from %"PRIu32
" to %"PRIu32
" because of stacked device. "
960 "If it is not desired, then please explicitly specify MTUBytes= setting.",
963 return link_request_to_set_mtu(link
, mtu
);
966 static int link_up_dsa_slave(Link
*link
) {
972 /* For older kernels (specifically, older than 9d5ef190e5615a7b63af89f88c4106a5bc127974, kernel-5.12),
973 * it is necessary to bring up a DSA slave that its master interface is already up. And bringing up
974 * the slave fails with -ENETDOWN. So, let's bring up the master even if it is not managed by us,
975 * and try to bring up the slave after the master becomes up. */
977 if (link
->dsa_master_ifindex
<= 0)
980 if (!streq_ptr(link
->driver
, "dsa"))
983 if (link_get_by_index(link
->manager
, link
->dsa_master_ifindex
, &master
) < 0)
986 if (master
->state
== LINK_STATE_UNMANAGED
) {
987 /* If the DSA master interface is unmanaged, then it will never become up.
988 * Let's request to bring up the master. */
989 r
= link_request_to_bring_up_or_down(master
, /* up = */ true);
994 r
= link_request_to_bring_up_or_down(link
, /* up = */ true);
1001 static int link_up_or_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Request
*req
, Link
*link
, void *userdata
) {
1002 bool on_activate
, up
;
1009 on_activate
= req
->type
== REQUEST_TYPE_ACTIVATE_LINK
;
1010 up
= PTR_TO_INT(req
->userdata
);
1012 r
= sd_netlink_message_get_errno(m
);
1013 if (r
== -ENETDOWN
&& up
&& link_up_dsa_slave(link
) > 0)
1014 log_link_message_debug_errno(link
, m
, r
, "Could not bring up dsa slave, retrying again after dsa master becomes up");
1016 log_link_message_warning_errno(link
, m
, r
, up
?
1017 "Could not bring up interface, ignoring" :
1018 "Could not bring down interface, ignoring");
1020 r
= link_call_getlink(link
, get_link_update_flag_handler
);
1022 link_enter_failed(link
);
1026 link
->set_flags_messages
++;
1029 link
->activated
= true;
1030 link_check_ready(link
);
1036 static const char *up_or_down(bool up
) {
1037 return up
? "up" : "down";
1040 static int link_up_or_down(Link
*link
, bool up
, Request
*req
) {
1041 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
1045 assert(link
->manager
);
1046 assert(link
->manager
->rtnl
);
1049 log_link_debug(link
, "Bringing link %s", up_or_down(up
));
1051 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &m
, RTM_SETLINK
, link
->ifindex
);
1055 r
= sd_rtnl_message_link_set_flags(m
, up
? IFF_UP
: 0, IFF_UP
);
1059 return request_call_netlink_async(link
->manager
->rtnl
, m
, req
);
1062 static bool link_is_ready_to_activate_one(Link
*link
, bool allow_unmanaged
) {
1065 if (!IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
, LINK_STATE_UNMANAGED
))
1069 return allow_unmanaged
;
1071 if (link
->set_link_messages
> 0)
1077 static bool link_is_ready_to_activate(Link
*link
, bool up
) {
1080 if (!check_ready_for_all_sr_iov_ports(link
, /* allow_unmanaged = */ false,
1081 link_is_ready_to_activate_one
))
1084 if (up
&& link_rfkilled(link
) > 0)
1090 static int link_process_activation(Request
*req
, Link
*link
, void *userdata
) {
1091 bool up
= PTR_TO_INT(userdata
);
1097 if (!link_is_ready_to_activate(link
, up
))
1100 r
= link_up_or_down(link
, up
, req
);
1102 return log_link_warning_errno(link
, r
, "Failed to activate link: %m");
1107 int link_request_to_activate(Link
*link
) {
1112 assert(link
->network
);
1114 switch (link
->network
->activation_policy
) {
1115 case ACTIVATION_POLICY_BOUND
:
1116 r
= link_handle_bound_to_list(link
);
1120 case ACTIVATION_POLICY_MANUAL
:
1121 link
->activated
= true;
1122 link_check_ready(link
);
1124 case ACTIVATION_POLICY_UP
:
1125 case ACTIVATION_POLICY_ALWAYS_UP
:
1128 case ACTIVATION_POLICY_DOWN
:
1129 case ACTIVATION_POLICY_ALWAYS_DOWN
:
1133 assert_not_reached();
1136 link
->activated
= false;
1138 r
= link_queue_request_full(link
, REQUEST_TYPE_ACTIVATE_LINK
,
1139 INT_TO_PTR(up
), NULL
, NULL
, NULL
,
1140 link_process_activation
,
1141 &link
->set_flags_messages
,
1142 link_up_or_down_handler
, NULL
);
1144 return log_link_error_errno(link
, r
, "Failed to request to activate link: %m");
1146 log_link_debug(link
, "Requested to activate link");
1150 static bool link_is_ready_to_bring_up_or_down(Link
*link
, bool up
) {
1153 if (up
&& link
->dsa_master_ifindex
> 0) {
1156 /* The master interface must be up. See comments in link_up_dsa_slave(). */
1158 if (link_get_by_index(link
->manager
, link
->dsa_master_ifindex
, &master
) < 0)
1161 if (!FLAGS_SET(master
->flags
, IFF_UP
))
1165 if (link
->state
== LINK_STATE_UNMANAGED
)
1168 if (!IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
))
1171 if (link
->set_link_messages
> 0)
1174 if (!link
->activated
)
1177 if (up
&& link_rfkilled(link
) > 0)
1183 static int link_process_up_or_down(Request
*req
, Link
*link
, void *userdata
) {
1184 bool up
= PTR_TO_INT(userdata
);
1190 if (!link_is_ready_to_bring_up_or_down(link
, up
))
1193 r
= link_up_or_down(link
, up
, req
);
1195 return log_link_warning_errno(link
, r
, "Failed to bring link %s: %m", up_or_down(up
));
1200 int link_request_to_bring_up_or_down(Link
*link
, bool up
) {
1205 r
= link_queue_request_full(link
, REQUEST_TYPE_UP_DOWN
,
1206 INT_TO_PTR(up
), NULL
, NULL
, NULL
,
1207 link_process_up_or_down
,
1208 &link
->set_flags_messages
,
1209 link_up_or_down_handler
, NULL
);
1211 return log_link_warning_errno(link
, r
, "Failed to request to bring link %s: %m",
1214 log_link_debug(link
, "Requested to bring link %s", up_or_down(up
));
1218 static int link_down_now_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1223 assert(link
->set_flags_messages
> 0);
1225 link
->set_flags_messages
--;
1227 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1230 r
= sd_netlink_message_get_errno(m
);
1232 log_link_message_warning_errno(link
, m
, r
, "Could not bring down interface, ignoring");
1234 r
= link_call_getlink(link
, get_link_update_flag_handler
);
1236 link_enter_failed(link
);
1240 link
->set_flags_messages
++;
1244 int link_down_now(Link
*link
) {
1245 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1249 assert(link
->manager
);
1250 assert(link
->manager
->rtnl
);
1252 log_link_debug(link
, "Bringing link down");
1254 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
1256 return log_link_warning_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
1258 r
= sd_rtnl_message_link_set_flags(req
, 0, IFF_UP
);
1260 return log_link_warning_errno(link
, r
, "Could not set link flags: %m");
1262 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_down_now_handler
,
1263 link_netlink_destroy_callback
, link
);
1265 return log_link_warning_errno(link
, r
, "Could not send rtnetlink message: %m");
1267 link
->set_flags_messages
++;
1272 int link_down_slave_links(Link
*link
) {
1278 SET_FOREACH(slave
, link
->slaves
) {
1279 r
= link_down_now(slave
);
1287 static int link_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1293 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1296 r
= sd_netlink_message_get_errno(m
);
1298 log_link_message_warning_errno(link
, m
, r
, "Could not remove interface, ignoring");
1303 int link_remove(Link
*link
) {
1304 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
1308 assert(link
->manager
);
1309 assert(link
->manager
->rtnl
);
1311 log_link_debug(link
, "Removing link.");
1313 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_DELLINK
, link
->ifindex
);
1315 return log_link_debug_errno(link
, r
, "Could not allocate RTM_DELLINK message: %m");
1317 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_remove_handler
,
1318 link_netlink_destroy_callback
, link
);
1320 return log_link_debug_errno(link
, r
, "Could not send rtnetlink message: %m");