1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 #include <netinet/in.h>
5 #include <linux/if_arp.h>
7 #include "missing_network.h"
8 #include "netlink-util.h"
9 #include "networkd-can.h"
10 #include "networkd-link.h"
11 #include "networkd-manager.h"
12 #include "networkd-queue.h"
13 #include "networkd-setlink.h"
14 #include "string-table.h"
16 static const char *const set_link_operation_table
[_SET_LINK_OPERATION_MAX
] = {
17 [SET_LINK_ADDRESS_GENERATION_MODE
] = "IPv6LL address generation mode",
18 [SET_LINK_BOND
] = "bond configurations",
19 [SET_LINK_BRIDGE
] = "bridge configurations",
20 [SET_LINK_BRIDGE_VLAN
] = "bridge VLAN configurations",
21 [SET_LINK_CAN
] = "CAN interface configurations",
22 [SET_LINK_FLAGS
] = "link flags",
23 [SET_LINK_GROUP
] = "interface group",
24 [SET_LINK_MAC
] = "MAC address",
25 [SET_LINK_MASTER
] = "master interface",
26 [SET_LINK_MTU
] = "MTU",
29 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(set_link_operation
, SetLinkOperation
);
31 static int get_link_default_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
32 return link_getlink_handler_internal(rtnl
, m
, link
, "Failed to sync link information");
35 static int get_link_master_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
36 if (get_link_default_handler(rtnl
, m
, link
) > 0)
37 link
->master_set
= true;
41 static int get_link_update_flag_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
43 assert(link
->set_flags_messages
> 0);
45 link
->set_flags_messages
--;
47 return get_link_default_handler(rtnl
, m
, link
);
50 static int set_link_handler_internal(
52 sd_netlink_message
*m
,
56 link_netlink_message_handler_t get_link_handler
) {
62 assert(link
->set_link_messages
> 0);
63 assert(op
>= 0 && op
< _SET_LINK_OPERATION_MAX
);
65 link
->set_link_messages
--;
67 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
70 r
= sd_netlink_message_get_errno(m
);
72 const char *error_msg
;
74 error_msg
= strjoina("Failed to set ", set_link_operation_to_string(op
), ignore
? ", ignoring" : "");
75 log_link_message_warning_errno(link
, m
, r
, error_msg
);
78 link_enter_failed(link
);
82 log_link_debug(link
, "%s set.", set_link_operation_to_string(op
));
84 if (get_link_handler
) {
85 r
= link_call_getlink(link
, get_link_handler
);
87 link_enter_failed(link
);
92 if (link
->set_link_messages
== 0)
93 link_check_ready(link
);
100 assert(link
->set_flags_messages
> 0);
101 link
->set_flags_messages
--;
103 case SET_LINK_MASTER
:
104 link
->master_set
= true;
113 static int link_set_addrgen_mode_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
116 r
= set_link_handler_internal(rtnl
, m
, link
, SET_LINK_ADDRESS_GENERATION_MODE
, /* ignore = */ true, NULL
);
120 r
= link_drop_ipv6ll_addresses(link
);
122 log_link_warning_errno(link
, r
, "Failed to drop IPv6LL addresses: %m");
123 link_enter_failed(link
);
129 static int link_set_bond_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
130 return set_link_handler_internal(rtnl
, m
, link
, SET_LINK_BOND
, /* ignore = */ false, NULL
);
133 static int link_set_bridge_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
134 return set_link_handler_internal(rtnl
, m
, link
, SET_LINK_BRIDGE
, /* ignore = */ true, NULL
);
137 static int link_set_bridge_vlan_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
138 return set_link_handler_internal(rtnl
, m
, link
, SET_LINK_BRIDGE_VLAN
, /* ignore = */ false, NULL
);
141 static int link_set_can_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
142 return set_link_handler_internal(rtnl
, m
, link
, SET_LINK_CAN
, /* ignore = */ false, NULL
);
145 static int link_set_flags_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
146 return set_link_handler_internal(rtnl
, m
, link
, SET_LINK_FLAGS
, /* ignore = */ false, get_link_update_flag_handler
);
149 static int link_set_group_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
150 return set_link_handler_internal(rtnl
, m
, link
, SET_LINK_GROUP
, /* ignore = */ false, NULL
);
153 static int link_set_mac_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
154 return set_link_handler_internal(rtnl
, m
, link
, SET_LINK_MAC
, /* ignore = */ true, get_link_default_handler
);
157 static int link_set_mac_allow_retry_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
162 assert(link
->set_link_messages
> 0);
164 link
->set_link_messages
--;
166 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
169 r
= sd_netlink_message_get_errno(m
);
171 /* Most real network devices refuse to set its hardware address with -EBUSY when its
172 * operstate is not down. See, eth_prepare_mac_addr_change() in net/ethernet/eth.c
175 log_link_message_debug_errno(link
, m
, r
, "Failed to set MAC address, retrying again: %m");
177 r
= link_request_to_set_mac(link
, /* allow_retry = */ false);
179 link_enter_failed(link
);
184 /* set_link_mac_handler() also decrement set_link_messages, so once increment the value. */
185 link
->set_link_messages
++;
186 return link_set_mac_handler(rtnl
, m
, link
);
189 static int link_set_master_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
190 return set_link_handler_internal(rtnl
, m
, link
, SET_LINK_MASTER
, /* ignore = */ false, get_link_master_handler
);
193 static int link_unset_master_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
194 /* Some devices do not support setting master ifindex. Let's ignore error on unsetting master ifindex. */
195 return set_link_handler_internal(rtnl
, m
, link
, SET_LINK_MASTER
, /* ignore = */ true, get_link_master_handler
);
198 static int link_set_mtu_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
201 r
= set_link_handler_internal(rtnl
, m
, link
, SET_LINK_MTU
, /* ignore = */ true, get_link_default_handler
);
205 /* The kernel resets ipv6 mtu after changing device mtu;
206 * we must set this here, after we've set device mtu */
207 r
= link_set_ipv6_mtu(link
);
209 log_link_warning_errno(link
, r
, "Failed to set IPv6 MTU, ignoring: %m");
214 static int link_configure(
218 link_netlink_message_handler_t callback
) {
220 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
224 assert(link
->manager
);
225 assert(link
->manager
->rtnl
);
226 assert(link
->network
);
227 assert(op
>= 0 && op
< _SET_LINK_OPERATION_MAX
);
230 log_link_debug(link
, "Setting %s", set_link_operation_to_string(op
));
232 if (op
== SET_LINK_BOND
) {
233 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_NEWLINK
, link
->master_ifindex
);
235 return log_link_debug_errno(link
, r
, "Could not allocate RTM_NEWLINK message: %m");
236 } else if (op
== SET_LINK_CAN
) {
237 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_NEWLINK
, link
->ifindex
);
239 return log_link_debug_errno(link
, r
, "Could not allocate RTM_NEWLINK message: %m");
241 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
243 return log_link_debug_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
247 case SET_LINK_ADDRESS_GENERATION_MODE
:
248 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
250 return log_link_debug_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
252 r
= sd_netlink_message_open_container(req
, AF_INET6
);
254 return log_link_debug_errno(link
, r
, "Could not open AF_INET6 container: %m");
256 r
= sd_netlink_message_append_u8(req
, IFLA_INET6_ADDR_GEN_MODE
, PTR_TO_UINT8(userdata
));
258 return log_link_debug_errno(link
, r
, "Could not append IFLA_INET6_ADDR_GEN_MODE attribute: %m");
260 r
= sd_netlink_message_close_container(req
);
262 return log_link_debug_errno(link
, r
, "Could not close AF_INET6 container: %m");
264 r
= sd_netlink_message_close_container(req
);
266 return log_link_debug_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
269 r
= sd_netlink_message_set_flags(req
, NLM_F_REQUEST
| NLM_F_ACK
);
271 return log_link_debug_errno(link
, r
, "Could not set netlink message flags: %m");
273 r
= sd_netlink_message_open_container(req
, IFLA_LINKINFO
);
275 return log_link_debug_errno(link
, r
, "Could not open IFLA_LINKINFO container: %m");
277 r
= sd_netlink_message_open_container_union(req
, IFLA_INFO_DATA
, "bond");
279 return log_link_debug_errno(link
, r
, "Could not open IFLA_INFO_DATA container: %m");
281 if (link
->network
->active_slave
) {
282 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_ACTIVE_SLAVE
, link
->ifindex
);
284 return log_link_debug_errno(link
, r
, "Could not append IFLA_BOND_ACTIVE_SLAVE attribute: %m");
287 if (link
->network
->primary_slave
) {
288 r
= sd_netlink_message_append_u32(req
, IFLA_BOND_PRIMARY
, link
->ifindex
);
290 return log_link_debug_errno(link
, r
, "Could not append IFLA_BOND_PRIMARY attribute: %m");
293 r
= sd_netlink_message_close_container(req
);
295 return log_link_debug_errno(link
, r
, "Could not close IFLA_INFO_DATA container: %m");
297 r
= sd_netlink_message_close_container(req
);
299 return log_link_debug_errno(link
, r
, "Could not close IFLA_LINKINFO container: %m");
302 case SET_LINK_BRIDGE
:
303 r
= sd_rtnl_message_link_set_family(req
, AF_BRIDGE
);
305 return log_link_debug_errno(link
, r
, "Could not set message family: %m");
307 r
= sd_netlink_message_open_container(req
, IFLA_PROTINFO
);
309 return log_link_debug_errno(link
, r
, "Could not open IFLA_PROTINFO container: %m");
311 if (link
->network
->use_bpdu
>= 0) {
312 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_GUARD
, link
->network
->use_bpdu
);
314 return log_link_debug_errno(link
, r
, "Could not append IFLA_BRPORT_GUARD attribute: %m");
317 if (link
->network
->hairpin
>= 0) {
318 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MODE
, link
->network
->hairpin
);
320 return log_link_debug_errno(link
, r
, "Could not append IFLA_BRPORT_MODE attribute: %m");
323 if (link
->network
->fast_leave
>= 0) {
324 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_FAST_LEAVE
, link
->network
->fast_leave
);
326 return log_link_debug_errno(link
, r
, "Could not append IFLA_BRPORT_FAST_LEAVE attribute: %m");
329 if (link
->network
->allow_port_to_be_root
>= 0) {
330 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_PROTECT
, link
->network
->allow_port_to_be_root
);
332 return log_link_debug_errno(link
, r
, "Could not append IFLA_BRPORT_PROTECT attribute: %m");
335 if (link
->network
->unicast_flood
>= 0) {
336 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_UNICAST_FLOOD
, link
->network
->unicast_flood
);
338 return log_link_debug_errno(link
, r
, "Could not append IFLA_BRPORT_UNICAST_FLOOD attribute: %m");
341 if (link
->network
->multicast_flood
>= 0) {
342 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MCAST_FLOOD
, link
->network
->multicast_flood
);
344 return log_link_debug_errno(link
, r
, "Could not append IFLA_BRPORT_MCAST_FLOOD attribute: %m");
347 if (link
->network
->multicast_to_unicast
>= 0) {
348 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MCAST_TO_UCAST
, link
->network
->multicast_to_unicast
);
350 return log_link_debug_errno(link
, r
, "Could not append IFLA_BRPORT_MCAST_TO_UCAST attribute: %m");
353 if (link
->network
->neighbor_suppression
>= 0) {
354 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_NEIGH_SUPPRESS
, link
->network
->neighbor_suppression
);
356 return log_link_debug_errno(link
, r
, "Could not append IFLA_BRPORT_NEIGH_SUPPRESS attribute: %m");
359 if (link
->network
->learning
>= 0) {
360 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_LEARNING
, link
->network
->learning
);
362 return log_link_debug_errno(link
, r
, "Could not append IFLA_BRPORT_LEARNING attribute: %m");
365 if (link
->network
->bridge_proxy_arp
>= 0) {
366 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_PROXYARP
, link
->network
->bridge_proxy_arp
);
368 return log_link_debug_errno(link
, r
, "Could not append IFLA_BRPORT_PROXYARP attribute: %m");
371 if (link
->network
->bridge_proxy_arp_wifi
>= 0) {
372 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_PROXYARP_WIFI
, link
->network
->bridge_proxy_arp_wifi
);
374 return log_link_debug_errno(link
, r
, "Could not append IFLA_BRPORT_PROXYARP_WIFI attribute: %m");
377 if (link
->network
->cost
!= 0) {
378 r
= sd_netlink_message_append_u32(req
, IFLA_BRPORT_COST
, link
->network
->cost
);
380 return log_link_debug_errno(link
, r
, "Could not append IFLA_BRPORT_COST attribute: %m");
383 if (link
->network
->priority
!= LINK_BRIDGE_PORT_PRIORITY_INVALID
) {
384 r
= sd_netlink_message_append_u16(req
, IFLA_BRPORT_PRIORITY
, link
->network
->priority
);
386 return log_link_debug_errno(link
, r
, "Could not append IFLA_BRPORT_PRIORITY attribute: %m");
389 if (link
->network
->multicast_router
!= _MULTICAST_ROUTER_INVALID
) {
390 r
= sd_netlink_message_append_u8(req
, IFLA_BRPORT_MULTICAST_ROUTER
, link
->network
->multicast_router
);
392 return log_link_debug_errno(link
, r
, "Could not append IFLA_BRPORT_MULTICAST_ROUTER attribute: %m");
395 r
= sd_netlink_message_close_container(req
);
397 return log_link_debug_errno(link
, r
, "Could not close IFLA_PROTINFO container: %m");
399 case SET_LINK_BRIDGE_VLAN
:
400 r
= sd_rtnl_message_link_set_family(req
, AF_BRIDGE
);
402 return log_link_debug_errno(link
, r
, "Could not set message family: %m");
404 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
406 return log_link_debug_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
408 if (!link
->network
->bridge
) {
409 /* master needs BRIDGE_FLAGS_SELF flag */
410 r
= sd_netlink_message_append_u16(req
, IFLA_BRIDGE_FLAGS
, BRIDGE_FLAGS_SELF
);
412 return log_link_debug_errno(link
, r
, "Could not append IFLA_BRIDGE_FLAGS attribute: %m");
415 r
= bridge_vlan_append_info(link
, req
, link
->network
->pvid
, link
->network
->br_vid_bitmap
, link
->network
->br_untagged_bitmap
);
417 return log_link_debug_errno(link
, r
, "Could not append VLANs: %m");
419 r
= sd_netlink_message_close_container(req
);
421 return log_link_debug_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
425 r
= can_set_netlink_message(link
, req
);
429 case SET_LINK_FLAGS
: {
430 unsigned ifi_change
= 0, ifi_flags
= 0;
432 if (link
->network
->arp
>= 0) {
433 ifi_change
|= IFF_NOARP
;
434 SET_FLAG(ifi_flags
, IFF_NOARP
, link
->network
->arp
== 0);
437 if (link
->network
->multicast
>= 0) {
438 ifi_change
|= IFF_MULTICAST
;
439 SET_FLAG(ifi_flags
, IFF_MULTICAST
, link
->network
->multicast
);
442 if (link
->network
->allmulticast
>= 0) {
443 ifi_change
|= IFF_ALLMULTI
;
444 SET_FLAG(ifi_flags
, IFF_ALLMULTI
, link
->network
->allmulticast
);
447 if (link
->network
->promiscuous
>= 0) {
448 ifi_change
|= IFF_PROMISC
;
449 SET_FLAG(ifi_flags
, IFF_PROMISC
, link
->network
->promiscuous
);
452 r
= sd_rtnl_message_link_set_flags(req
, ifi_flags
, ifi_change
);
454 return log_link_debug_errno(link
, r
, "Could not set link flags: %m");
459 r
= sd_netlink_message_append_u32(req
, IFLA_GROUP
, link
->network
->group
);
461 return log_link_debug_errno(link
, r
, "Could not append IFLA_GROUP attribute: %m");
464 r
= sd_netlink_message_append_ether_addr(req
, IFLA_ADDRESS
, link
->network
->mac
);
466 return log_link_debug_errno(link
, r
, "Could not append IFLA_ADDRESS attribute: %m");
468 case SET_LINK_MASTER
:
469 r
= sd_netlink_message_append_u32(req
, IFLA_MASTER
, PTR_TO_UINT32(userdata
));
471 return log_link_debug_errno(link
, r
, "Could not append IFLA_MASTER attribute: %m");
474 r
= sd_netlink_message_append_u32(req
, IFLA_MTU
, PTR_TO_UINT32(userdata
));
476 return log_link_debug_errno(link
, r
, "Could not append IFLA_MTU attribute: %m");
479 assert_not_reached();
482 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, callback
,
483 link_netlink_destroy_callback
, link
);
485 return log_link_debug_errno(link
, r
, "Could not send RTM_SETLINK message: %m");
491 static bool netdev_is_ready(NetDev
*netdev
) {
494 if (netdev
->state
!= NETDEV_STATE_READY
)
496 if (netdev
->ifindex
== 0)
502 static bool link_is_ready_to_call_set_link(Request
*req
) {
510 op
= PTR_TO_INT(req
->set_link_operation_ptr
);
512 if (!IN_SET(link
->state
, LINK_STATE_INITIALIZED
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
))
517 case SET_LINK_BRIDGE
:
518 case SET_LINK_BRIDGE_VLAN
:
519 if (!link
->master_set
)
523 /* Do not check link->set_flgas_messages here, as it is ok even if link->flags
524 * is outdated, and checking the counter causes a deadlock. */
525 if (FLAGS_SET(link
->flags
, IFF_UP
)) {
526 /* The CAN interface must be down to configure bitrate, etc... */
529 link_enter_failed(link
);
535 if (req
->netlink_handler
== link_set_mac_handler
) {
536 /* This is the second trial to set MTU. On the first attempt
537 * req->netlink_handler points to link_set_mac_allow_retry_handler().
538 * The first trial failed as the interface was up. */
541 link_enter_failed(link
);
546 case SET_LINK_MASTER
: {
549 assert(link
->network
);
551 if (link
->network
->batadv
) {
552 if (!netdev_is_ready(link
->network
->batadv
))
554 m
= link
->network
->batadv
->ifindex
;
555 } else if (link
->network
->bond
) {
556 if (!netdev_is_ready(link
->network
->bond
))
558 m
= link
->network
->bond
->ifindex
;
560 /* Do not check link->set_flgas_messages here, as it is ok even if link->flags
561 * is outdated, and checking the counter causes a deadlock. */
562 if (FLAGS_SET(link
->flags
, IFF_UP
)) {
563 /* link must be down when joining to bond master. */
566 link_enter_failed(link
);
570 } else if (link
->network
->bridge
) {
571 if (!netdev_is_ready(link
->network
->bridge
))
573 m
= link
->network
->bridge
->ifindex
;
574 } else if (link
->network
->vrf
) {
575 if (!netdev_is_ready(link
->network
->vrf
))
577 m
= link
->network
->vrf
->ifindex
;
580 req
->userdata
= UINT32_TO_PTR(m
);
590 int request_process_set_link(Request
*req
) {
596 assert(req
->type
== REQUEST_TYPE_SET_LINK
);
597 assert(req
->netlink_handler
);
599 op
= PTR_TO_INT(req
->set_link_operation_ptr
);
601 assert(op
>= 0 && op
< _SET_LINK_OPERATION_MAX
);
603 if (!link_is_ready_to_call_set_link(req
))
606 r
= link_configure(req
->link
, op
, req
->userdata
, req
->netlink_handler
);
608 return log_link_error_errno(req
->link
, r
, "Failed to set %s: %m",
609 set_link_operation_to_string(op
));
611 if (op
== SET_LINK_FLAGS
)
612 req
->link
->set_flags_messages
++;
617 static int link_request_set_link(
620 link_netlink_message_handler_t netlink_handler
,
627 assert(op
>= 0 && op
< _SET_LINK_OPERATION_MAX
);
628 assert(netlink_handler
);
630 r
= link_queue_request(link
, REQUEST_TYPE_SET_LINK
, INT_TO_PTR(op
), false,
631 &link
->set_link_messages
, netlink_handler
, &req
);
633 return log_link_error_errno(link
, r
, "Failed to request to set %s: %m",
634 set_link_operation_to_string(op
));
636 log_link_debug(link
, "Requested to set %s", set_link_operation_to_string(op
));
643 int link_request_to_set_addrgen_mode(Link
*link
) {
649 assert(link
->network
);
651 if (!socket_ipv6_is_supported())
654 if (!link_ipv6ll_enabled(link
))
655 mode
= IN6_ADDR_GEN_MODE_NONE
;
656 else if (link
->network
->ipv6ll_address_gen_mode
>= 0)
657 mode
= link
->network
->ipv6ll_address_gen_mode
;
658 else if (in6_addr_is_set(&link
->network
->ipv6ll_stable_secret
))
659 mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
661 mode
= IN6_ADDR_GEN_MODE_EUI64
;
663 r
= link_request_set_link(link
, SET_LINK_ADDRESS_GENERATION_MODE
, link_set_addrgen_mode_handler
, &req
);
667 req
->userdata
= UINT8_TO_PTR(mode
);
671 int link_request_to_set_bond(Link
*link
) {
673 assert(link
->network
);
675 if (!link
->network
->bond
)
678 return link_request_set_link(link
, SET_LINK_BOND
, link_set_bond_handler
, NULL
);
681 int link_request_to_set_bridge(Link
*link
) {
683 assert(link
->network
);
685 if (!link
->network
->bridge
)
688 return link_request_set_link(link
, SET_LINK_BRIDGE
, link_set_bridge_handler
, NULL
);
691 int link_request_to_set_bridge_vlan(Link
*link
) {
693 assert(link
->network
);
695 if (!link
->network
->use_br_vlan
)
698 if (!link
->network
->bridge
&& !streq_ptr(link
->kind
, "bridge"))
701 return link_request_set_link(link
, SET_LINK_BRIDGE_VLAN
, link_set_bridge_vlan_handler
, NULL
);
704 int link_request_to_set_can(Link
*link
) {
706 assert(link
->network
);
708 if (link
->iftype
!= ARPHRD_CAN
)
711 if (!streq_ptr(link
->kind
, "can"))
714 return link_request_set_link(link
, SET_LINK_CAN
, link_set_can_handler
, NULL
);
717 int link_request_to_set_flags(Link
*link
) {
719 assert(link
->network
);
721 if (link
->network
->arp
< 0 &&
722 link
->network
->multicast
< 0 &&
723 link
->network
->allmulticast
< 0 &&
724 link
->network
->promiscuous
< 0)
727 return link_request_set_link(link
, SET_LINK_FLAGS
, link_set_flags_handler
, NULL
);
730 int link_request_to_set_group(Link
*link
) {
732 assert(link
->network
);
734 if (!link
->network
->group_set
)
737 return link_request_set_link(link
, SET_LINK_GROUP
, link_set_group_handler
, NULL
);
740 int link_request_to_set_mac(Link
*link
, bool allow_retry
) {
742 assert(link
->network
);
744 if (!link
->network
->mac
)
747 if (link
->hw_addr
.length
!= sizeof(struct ether_addr
)) {
748 /* Note that for now we only support changing hardware addresses on Ethernet. */
749 log_link_debug(link
, "Size of the hardware address (%zu) does not match the size of MAC address (%zu), ignoring.",
750 link
->hw_addr
.length
, sizeof(struct ether_addr
));
754 if (ether_addr_equal(&link
->hw_addr
.ether
, link
->network
->mac
))
757 return link_request_set_link(link
, SET_LINK_MAC
,
758 allow_retry
? link_set_mac_allow_retry_handler
: link_set_mac_handler
,
762 int link_request_to_set_master(Link
*link
) {
764 assert(link
->network
);
766 link
->master_set
= false;
768 if (link
->network
->batadv
|| link
->network
->bond
|| link
->network
->bridge
|| link
->network
->vrf
)
769 return link_request_set_link(link
, SET_LINK_MASTER
, link_set_master_handler
, NULL
);
771 return link_request_set_link(link
, SET_LINK_MASTER
, link_unset_master_handler
, NULL
);
774 int link_request_to_set_mtu(Link
*link
, uint32_t mtu
) {
781 assert(link
->network
);
783 min_mtu
= link
->min_mtu
;
784 origin
= "the minimum MTU of the interface";
785 if (link_ipv6_enabled(link
)) {
786 /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes on the interface. Bump up
787 * MTU bytes to IPV6_MTU_MIN. */
788 if (min_mtu
< IPV6_MIN_MTU
) {
789 min_mtu
= IPV6_MIN_MTU
;
790 origin
= "the minimum IPv6 MTU";
792 if (min_mtu
< link
->network
->ipv6_mtu
) {
793 min_mtu
= link
->network
->ipv6_mtu
;
794 origin
= "the requested IPv6 MTU in IPv6MTUBytes=";
799 log_link_warning(link
, "Bumping the requested MTU %"PRIu32
" to %s (%"PRIu32
")",
800 mtu
, origin
, min_mtu
);
804 if (mtu
> link
->max_mtu
) {
805 log_link_warning(link
, "Reducing the requested MTU %"PRIu32
" to the interface's maximum MTU %"PRIu32
".",
810 if (link
->mtu
== mtu
)
813 r
= link_request_set_link(link
, SET_LINK_MTU
, link_set_mtu_handler
, &req
);
817 req
->userdata
= UINT32_TO_PTR(mtu
);
821 static bool link_reduces_vlan_mtu(Link
*link
) {
822 /* See netif_reduces_vlan_mtu() in kernel. */
823 return streq_ptr(link
->kind
, "macsec");
826 static uint32_t link_get_requested_mtu_by_stacked_netdevs(Link
*link
) {
830 HASHMAP_FOREACH(dev
, link
->network
->stacked_netdevs
)
831 if (dev
->kind
== NETDEV_KIND_VLAN
&& dev
->mtu
> 0)
832 /* See vlan_dev_change_mtu() in kernel. */
833 mtu
= MAX(mtu
, link_reduces_vlan_mtu(link
) ? dev
->mtu
+ 4 : dev
->mtu
);
835 else if (dev
->kind
== NETDEV_KIND_MACVLAN
&& dev
->mtu
> mtu
)
836 /* See macvlan_change_mtu() in kernel. */
842 int link_configure_mtu(Link
*link
) {
846 assert(link
->network
);
848 if (link
->network
->mtu
> 0)
849 return link_request_to_set_mtu(link
, link
->network
->mtu
);
851 mtu
= link_get_requested_mtu_by_stacked_netdevs(link
);
852 if (link
->mtu
>= mtu
)
855 log_link_notice(link
, "Bumping MTU bytes from %"PRIu32
" to %"PRIu32
" because of stacked device. "
856 "If it is not desired, then please explicitly specify MTUBytes= setting.",
859 return link_request_to_set_mtu(link
, mtu
);
862 static int link_up_or_down_handler_internal(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
, bool up
, bool check_ready
) {
868 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
871 r
= sd_netlink_message_get_errno(m
);
873 log_link_message_warning_errno(link
, m
, r
, up
?
874 "Could not bring up interface, ignoring" :
875 "Could not bring down interface, ignoring");
877 r
= link_call_getlink(link
, get_link_update_flag_handler
);
879 link_enter_failed(link
);
884 link
->activated
= true;
885 link_check_ready(link
);
891 assert(link
->set_flags_messages
> 0);
892 link
->set_flags_messages
--;
897 static int link_activate_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
898 return link_up_or_down_handler_internal(rtnl
, m
, link
, true, true);
901 static int link_activate_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
902 return link_up_or_down_handler_internal(rtnl
, m
, link
, false, true);
905 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
906 return link_up_or_down_handler_internal(rtnl
, m
, link
, true, false);
909 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
910 return link_up_or_down_handler_internal(rtnl
, m
, link
, false, false);
913 static const char *up_or_down(bool up
) {
914 return up
? "up" : "down";
917 static int link_up_or_down(Link
*link
, bool up
, link_netlink_message_handler_t callback
) {
918 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
922 assert(link
->manager
);
923 assert(link
->manager
->rtnl
);
926 log_link_debug(link
, "Bringing link %s", up_or_down(up
));
928 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
930 return log_link_debug_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
932 r
= sd_rtnl_message_link_set_flags(req
, up
? IFF_UP
: 0, IFF_UP
);
934 return log_link_debug_errno(link
, r
, "Could not set link flags: %m");
936 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, callback
,
937 link_netlink_destroy_callback
, link
);
939 return log_link_debug_errno(link
, r
, "Could not send rtnetlink message: %m");
946 int link_down(Link
*link
) {
951 r
= link_up_or_down(link
, false, link_down_handler
);
953 return log_link_error_errno(link
, r
, "Failed to bring down interface: %m");
955 link
->set_flags_messages
++;
959 static bool link_is_ready_to_activate(Link
*link
) {
962 if (!IN_SET(link
->state
, LINK_STATE_INITIALIZED
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
))
965 if (link
->set_link_messages
> 0)
971 int request_process_activation(Request
*req
) {
978 assert(req
->type
== REQUEST_TYPE_ACTIVATE_LINK
);
979 assert(req
->netlink_handler
);
982 up
= PTR_TO_INT(req
->userdata
);
984 if (!link_is_ready_to_activate(link
))
987 r
= link_up_or_down(link
, up
, req
->netlink_handler
);
989 return log_link_error_errno(link
, r
, "Failed to bring %s: %m", up_or_down(up
));
994 int link_request_to_activate(Link
*link
) {
1000 assert(link
->network
);
1002 switch (link
->network
->activation_policy
) {
1003 case ACTIVATION_POLICY_BOUND
:
1004 r
= link_handle_bound_to_list(link
);
1008 case ACTIVATION_POLICY_MANUAL
:
1009 link
->activated
= true;
1010 link_check_ready(link
);
1012 case ACTIVATION_POLICY_UP
:
1013 case ACTIVATION_POLICY_ALWAYS_UP
:
1016 case ACTIVATION_POLICY_DOWN
:
1017 case ACTIVATION_POLICY_ALWAYS_DOWN
:
1021 assert_not_reached();
1024 link
->activated
= false;
1026 r
= link_queue_request(link
, REQUEST_TYPE_ACTIVATE_LINK
, NULL
, false, &link
->set_flags_messages
,
1027 up
? link_activate_up_handler
: link_activate_down_handler
, &req
);
1029 return log_link_error_errno(link
, r
, "Failed to request to activate link: %m");
1031 req
->userdata
= INT_TO_PTR(up
);
1033 log_link_debug(link
, "Requested to activate link");
1037 static bool link_is_ready_to_bring_up_or_down(Link
*link
) {
1040 if (link
->state
== LINK_STATE_UNMANAGED
)
1043 if (!IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
))
1046 if (link
->set_link_messages
> 0)
1049 if (!link
->activated
)
1055 int request_process_link_up_or_down(Request
*req
) {
1062 assert(req
->type
== REQUEST_TYPE_UP_DOWN
);
1065 up
= PTR_TO_INT(req
->userdata
);
1067 if (!link_is_ready_to_bring_up_or_down(link
))
1070 r
= link_up_or_down(link
, up
, req
->netlink_handler
);
1072 return log_link_error_errno(link
, r
, "Failed to bring %s: %m", up_or_down(up
));
1077 int link_request_to_bring_up_or_down(Link
*link
, bool up
) {
1083 r
= link_queue_request(link
, REQUEST_TYPE_UP_DOWN
, NULL
, false, &link
->set_flags_messages
,
1084 up
? link_up_handler
: link_down_handler
, &req
);
1086 return log_link_error_errno(link
, r
, "Failed to request to bring %s link: %m",
1089 req
->userdata
= INT_TO_PTR(up
);
1091 log_link_debug(link
, "Requested to bring link %s", up_or_down(up
));