1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Linux ethernet bridge
7 * Lennert Buytenhek <buytenh@gnu.org>
10 #include <linux/kernel.h>
11 #include <linux/netdevice.h>
12 #include <linux/etherdevice.h>
13 #include <linux/netpoll.h>
14 #include <linux/ethtool.h>
15 #include <linux/if_arp.h>
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/rtnetlink.h>
19 #include <linux/if_ether.h>
20 #include <linux/slab.h>
23 #include <linux/if_vlan.h>
24 #include <net/switchdev.h>
25 #include <net/net_namespace.h>
27 #include "br_private.h"
30 * Determine initial path cost based on speed.
31 * using recommendations from 802.1d standard
33 * Since driver might sleep need to not be holding any locks.
35 static int port_cost(struct net_device
*dev
)
37 struct ethtool_link_ksettings ecmd
;
39 if (!__ethtool_get_link_ksettings(dev
, &ecmd
)) {
40 switch (ecmd
.base
.speed
) {
52 /* Old silly heuristics based on name */
53 if (!strncmp(dev
->name
, "lec", 3))
56 if (!strncmp(dev
->name
, "plip", 4))
59 return 100; /* assume old 10Mbps */
63 /* Check for port carrier transitions. */
64 void br_port_carrier_check(struct net_bridge_port
*p
, bool *notified
)
66 struct net_device
*dev
= p
->dev
;
67 struct net_bridge
*br
= p
->br
;
69 if (!(p
->flags
& BR_ADMIN_COST
) &&
70 netif_running(dev
) && netif_oper_up(dev
))
71 p
->path_cost
= port_cost(dev
);
74 if (!netif_running(br
->dev
))
77 spin_lock_bh(&br
->lock
);
78 if (netif_running(dev
) && netif_oper_up(dev
)) {
79 if (p
->state
== BR_STATE_DISABLED
) {
80 br_stp_enable_port(p
);
84 if (p
->state
!= BR_STATE_DISABLED
) {
85 br_stp_disable_port(p
);
89 spin_unlock_bh(&br
->lock
);
92 static void br_port_set_promisc(struct net_bridge_port
*p
)
96 if (br_promisc_port(p
))
99 err
= dev_set_promiscuity(p
->dev
, 1);
103 br_fdb_unsync_static(p
->br
, p
);
104 p
->flags
|= BR_PROMISC
;
107 static void br_port_clear_promisc(struct net_bridge_port
*p
)
111 /* Check if the port is already non-promisc or if it doesn't
112 * support UNICAST filtering. Without unicast filtering support
113 * we'll end up re-enabling promisc mode anyway, so just check for
116 if (!br_promisc_port(p
) || !(p
->dev
->priv_flags
& IFF_UNICAST_FLT
))
119 /* Since we'll be clearing the promisc mode, program the port
120 * first so that we don't have interruption in traffic.
122 err
= br_fdb_sync_static(p
->br
, p
);
126 dev_set_promiscuity(p
->dev
, -1);
127 p
->flags
&= ~BR_PROMISC
;
130 /* When a port is added or removed or when certain port flags
131 * change, this function is called to automatically manage
132 * promiscuity setting of all the bridge ports. We are always called
133 * under RTNL so can skip using rcu primitives.
135 void br_manage_promisc(struct net_bridge
*br
)
137 struct net_bridge_port
*p
;
138 bool set_all
= false;
140 /* If vlan filtering is disabled or bridge interface is placed
141 * into promiscuous mode, place all ports in promiscuous mode.
143 if ((br
->dev
->flags
& IFF_PROMISC
) || !br_vlan_enabled(br
->dev
))
146 list_for_each_entry(p
, &br
->port_list
, list
) {
148 br_port_set_promisc(p
);
150 /* If the number of auto-ports is <= 1, then all other
151 * ports will have their output configuration
152 * statically specified through fdbs. Since ingress
153 * on the auto-port becomes forwarding/egress to other
154 * ports and egress configuration is statically known,
155 * we can say that ingress configuration of the
156 * auto-port is also statically known.
157 * This lets us disable promiscuous mode and write
160 if (br
->auto_cnt
== 0 ||
161 (br
->auto_cnt
== 1 && br_auto_port(p
)))
162 br_port_clear_promisc(p
);
164 br_port_set_promisc(p
);
169 int nbp_backup_change(struct net_bridge_port
*p
,
170 struct net_device
*backup_dev
)
172 struct net_bridge_port
*old_backup
= rtnl_dereference(p
->backup_port
);
173 struct net_bridge_port
*backup_p
= NULL
;
178 if (!netif_is_bridge_port(backup_dev
))
181 backup_p
= br_port_get_rtnl(backup_dev
);
182 if (backup_p
->br
!= p
->br
)
189 if (old_backup
== backup_p
)
192 /* if the backup link is already set, clear it */
194 old_backup
->backup_redirected_cnt
--;
197 backup_p
->backup_redirected_cnt
++;
198 rcu_assign_pointer(p
->backup_port
, backup_p
);
203 static void nbp_backup_clear(struct net_bridge_port
*p
)
205 nbp_backup_change(p
, NULL
);
206 if (p
->backup_redirected_cnt
) {
207 struct net_bridge_port
*cur_p
;
209 list_for_each_entry(cur_p
, &p
->br
->port_list
, list
) {
210 struct net_bridge_port
*backup_p
;
212 backup_p
= rtnl_dereference(cur_p
->backup_port
);
214 nbp_backup_change(cur_p
, NULL
);
218 WARN_ON(rcu_access_pointer(p
->backup_port
) || p
->backup_redirected_cnt
);
221 static void nbp_update_port_count(struct net_bridge
*br
)
223 struct net_bridge_port
*p
;
226 list_for_each_entry(p
, &br
->port_list
, list
) {
230 if (br
->auto_cnt
!= cnt
) {
232 br_manage_promisc(br
);
236 static void nbp_delete_promisc(struct net_bridge_port
*p
)
238 /* If port is currently promiscuous, unset promiscuity.
239 * Otherwise, it is a static port so remove all addresses
242 dev_set_allmulti(p
->dev
, -1);
243 if (br_promisc_port(p
))
244 dev_set_promiscuity(p
->dev
, -1);
246 br_fdb_unsync_static(p
->br
, p
);
249 static void release_nbp(struct kobject
*kobj
)
251 struct net_bridge_port
*p
252 = container_of(kobj
, struct net_bridge_port
, kobj
);
256 static void brport_get_ownership(struct kobject
*kobj
, kuid_t
*uid
, kgid_t
*gid
)
258 struct net_bridge_port
*p
= kobj_to_brport(kobj
);
260 net_ns_get_ownership(dev_net(p
->dev
), uid
, gid
);
263 static struct kobj_type brport_ktype
= {
265 .sysfs_ops
= &brport_sysfs_ops
,
267 .release
= release_nbp
,
268 .get_ownership
= brport_get_ownership
,
271 static void destroy_nbp(struct net_bridge_port
*p
)
273 struct net_device
*dev
= p
->dev
;
279 kobject_put(&p
->kobj
);
282 static void destroy_nbp_rcu(struct rcu_head
*head
)
284 struct net_bridge_port
*p
=
285 container_of(head
, struct net_bridge_port
, rcu
);
289 static unsigned get_max_headroom(struct net_bridge
*br
)
291 unsigned max_headroom
= 0;
292 struct net_bridge_port
*p
;
294 list_for_each_entry(p
, &br
->port_list
, list
) {
295 unsigned dev_headroom
= netdev_get_fwd_headroom(p
->dev
);
297 if (dev_headroom
> max_headroom
)
298 max_headroom
= dev_headroom
;
304 static void update_headroom(struct net_bridge
*br
, int new_hr
)
306 struct net_bridge_port
*p
;
308 list_for_each_entry(p
, &br
->port_list
, list
)
309 netdev_set_rx_headroom(p
->dev
, new_hr
);
311 br
->dev
->needed_headroom
= new_hr
;
314 /* Delete port(interface) from bridge is done in two steps.
315 * via RCU. First step, marks device as down. That deletes
316 * all the timers and stops new packets from flowing through.
318 * Final cleanup doesn't occur until after all CPU's finished
319 * processing packets.
321 * Protected from multiple admin operations by RTNL mutex
323 static void del_nbp(struct net_bridge_port
*p
)
325 struct net_bridge
*br
= p
->br
;
326 struct net_device
*dev
= p
->dev
;
328 sysfs_remove_link(br
->ifobj
, p
->dev
->name
);
330 nbp_delete_promisc(p
);
332 spin_lock_bh(&br
->lock
);
333 br_stp_disable_port(p
);
334 spin_unlock_bh(&br
->lock
);
336 br_ifinfo_notify(RTM_DELLINK
, NULL
, p
);
338 list_del_rcu(&p
->list
);
339 if (netdev_get_fwd_headroom(dev
) == br
->dev
->needed_headroom
)
340 update_headroom(br
, get_max_headroom(br
));
341 netdev_reset_rx_headroom(dev
);
344 br_fdb_delete_by_port(br
, p
, 0, 1);
345 switchdev_deferred_process();
348 nbp_update_port_count(br
);
350 netdev_upper_dev_unlink(dev
, br
->dev
);
352 dev
->priv_flags
&= ~IFF_BRIDGE_PORT
;
354 netdev_rx_handler_unregister(dev
);
356 br_multicast_del_port(p
);
358 kobject_uevent(&p
->kobj
, KOBJ_REMOVE
);
359 kobject_del(&p
->kobj
);
361 br_netpoll_disable(p
);
363 call_rcu(&p
->rcu
, destroy_nbp_rcu
);
366 /* Delete bridge device */
367 void br_dev_delete(struct net_device
*dev
, struct list_head
*head
)
369 struct net_bridge
*br
= netdev_priv(dev
);
370 struct net_bridge_port
*p
, *n
;
372 list_for_each_entry_safe(p
, n
, &br
->port_list
, list
) {
376 br_recalculate_neigh_suppress_enabled(br
);
378 br_fdb_delete_by_port(br
, NULL
, 0, 1);
380 cancel_delayed_work_sync(&br
->gc_work
);
382 br_sysfs_delbr(br
->dev
);
383 unregister_netdevice_queue(br
->dev
, head
);
386 /* find an available port number */
387 static int find_portno(struct net_bridge
*br
)
390 struct net_bridge_port
*p
;
391 unsigned long *inuse
;
393 inuse
= bitmap_zalloc(BR_MAX_PORTS
, GFP_KERNEL
);
397 set_bit(0, inuse
); /* zero is reserved */
398 list_for_each_entry(p
, &br
->port_list
, list
) {
399 set_bit(p
->port_no
, inuse
);
401 index
= find_first_zero_bit(inuse
, BR_MAX_PORTS
);
404 return (index
>= BR_MAX_PORTS
) ? -EXFULL
: index
;
407 /* called with RTNL but without bridge lock */
408 static struct net_bridge_port
*new_nbp(struct net_bridge
*br
,
409 struct net_device
*dev
)
411 struct net_bridge_port
*p
;
414 index
= find_portno(br
);
416 return ERR_PTR(index
);
418 p
= kzalloc(sizeof(*p
), GFP_KERNEL
);
420 return ERR_PTR(-ENOMEM
);
425 p
->path_cost
= port_cost(dev
);
426 p
->priority
= 0x8000 >> BR_PORT_BITS
;
428 p
->flags
= BR_LEARNING
| BR_FLOOD
| BR_MCAST_FLOOD
| BR_BCAST_FLOOD
;
430 br_set_state(p
, BR_STATE_DISABLED
);
431 br_stp_port_timer_init(p
);
432 err
= br_multicast_add_port(p
);
442 int br_add_bridge(struct net
*net
, const char *name
)
444 struct net_device
*dev
;
447 dev
= alloc_netdev(sizeof(struct net_bridge
), name
, NET_NAME_UNKNOWN
,
453 dev_net_set(dev
, net
);
454 dev
->rtnl_link_ops
= &br_link_ops
;
456 res
= register_netdev(dev
);
462 int br_del_bridge(struct net
*net
, const char *name
)
464 struct net_device
*dev
;
468 dev
= __dev_get_by_name(net
, name
);
470 ret
= -ENXIO
; /* Could not find device */
472 else if (!(dev
->priv_flags
& IFF_EBRIDGE
)) {
473 /* Attempt to delete non bridge device! */
477 else if (dev
->flags
& IFF_UP
) {
478 /* Not shutdown yet. */
483 br_dev_delete(dev
, NULL
);
489 /* MTU of the bridge pseudo-device: ETH_DATA_LEN or the minimum of the ports */
490 static int br_mtu_min(const struct net_bridge
*br
)
492 const struct net_bridge_port
*p
;
495 list_for_each_entry(p
, &br
->port_list
, list
)
496 if (!ret_mtu
|| ret_mtu
> p
->dev
->mtu
)
497 ret_mtu
= p
->dev
->mtu
;
499 return ret_mtu
? ret_mtu
: ETH_DATA_LEN
;
502 void br_mtu_auto_adjust(struct net_bridge
*br
)
506 /* if the bridge MTU was manually configured don't mess with it */
507 if (br_opt_get(br
, BROPT_MTU_SET_BY_USER
))
510 /* change to the minimum MTU and clear the flag which was set by
511 * the bridge ndo_change_mtu callback
513 dev_set_mtu(br
->dev
, br_mtu_min(br
));
514 br_opt_toggle(br
, BROPT_MTU_SET_BY_USER
, false);
517 static void br_set_gso_limits(struct net_bridge
*br
)
519 unsigned int gso_max_size
= GSO_MAX_SIZE
;
520 u16 gso_max_segs
= GSO_MAX_SEGS
;
521 const struct net_bridge_port
*p
;
523 list_for_each_entry(p
, &br
->port_list
, list
) {
524 gso_max_size
= min(gso_max_size
, p
->dev
->gso_max_size
);
525 gso_max_segs
= min(gso_max_segs
, p
->dev
->gso_max_segs
);
527 br
->dev
->gso_max_size
= gso_max_size
;
528 br
->dev
->gso_max_segs
= gso_max_segs
;
532 * Recomputes features using slave's features
534 netdev_features_t
br_features_recompute(struct net_bridge
*br
,
535 netdev_features_t features
)
537 struct net_bridge_port
*p
;
538 netdev_features_t mask
;
540 if (list_empty(&br
->port_list
))
544 features
&= ~NETIF_F_ONE_FOR_ALL
;
546 list_for_each_entry(p
, &br
->port_list
, list
) {
547 features
= netdev_increment_features(features
,
548 p
->dev
->features
, mask
);
550 features
= netdev_add_tso_features(features
, mask
);
555 /* called with RTNL */
556 int br_add_if(struct net_bridge
*br
, struct net_device
*dev
,
557 struct netlink_ext_ack
*extack
)
559 struct net_bridge_port
*p
;
561 unsigned br_hr
, dev_hr
;
564 /* Don't allow bridging non-ethernet like devices, or DSA-enabled
565 * master network devices since the bridge layer rx_handler prevents
566 * the DSA fake ethertype handler to be invoked, so we do not strip off
567 * the DSA switch tag protocol header and the bridge layer just return
568 * RX_HANDLER_CONSUMED, stopping RX processing for these frames.
570 if ((dev
->flags
& IFF_LOOPBACK
) ||
571 dev
->type
!= ARPHRD_ETHER
|| dev
->addr_len
!= ETH_ALEN
||
572 !is_valid_ether_addr(dev
->dev_addr
) ||
573 netdev_uses_dsa(dev
))
576 /* No bridging of bridges */
577 if (dev
->netdev_ops
->ndo_start_xmit
== br_dev_xmit
) {
578 NL_SET_ERR_MSG(extack
,
579 "Can not enslave a bridge to a bridge");
583 /* Device has master upper dev */
584 if (netdev_master_upper_dev_get(dev
))
587 /* No bridging devices that dislike that (e.g. wireless) */
588 if (dev
->priv_flags
& IFF_DONT_BRIDGE
) {
589 NL_SET_ERR_MSG(extack
,
590 "Device does not allow enslaving to a bridge");
594 p
= new_nbp(br
, dev
);
598 call_netdevice_notifiers(NETDEV_JOIN
, dev
);
600 err
= dev_set_allmulti(dev
, 1);
602 kfree(p
); /* kobject not yet init'd, manually free */
606 err
= kobject_init_and_add(&p
->kobj
, &brport_ktype
, &(dev
->dev
.kobj
),
607 SYSFS_BRIDGE_PORT_ATTR
);
611 err
= br_sysfs_addif(p
);
615 err
= br_netpoll_enable(p
);
619 err
= netdev_rx_handler_register(dev
, br_handle_frame
, p
);
623 dev
->priv_flags
|= IFF_BRIDGE_PORT
;
625 err
= netdev_master_upper_dev_link(dev
, br
->dev
, NULL
, NULL
, extack
);
629 err
= nbp_switchdev_mark_set(p
);
633 dev_disable_lro(dev
);
635 list_add_rcu(&p
->list
, &br
->port_list
);
637 nbp_update_port_count(br
);
639 netdev_update_features(br
->dev
);
641 br_hr
= br
->dev
->needed_headroom
;
642 dev_hr
= netdev_get_fwd_headroom(dev
);
644 update_headroom(br
, dev_hr
);
646 netdev_set_rx_headroom(dev
, br_hr
);
648 if (br_fdb_insert(br
, p
, dev
->dev_addr
, 0))
649 netdev_err(dev
, "failed insert local address bridge forwarding table\n");
651 if (br
->dev
->addr_assign_type
!= NET_ADDR_SET
) {
652 /* Ask for permission to use this MAC address now, even if we
653 * don't end up choosing it below.
655 err
= dev_pre_changeaddr_notify(br
->dev
, dev
->dev_addr
, extack
);
660 err
= nbp_vlan_init(p
, extack
);
662 netdev_err(dev
, "failed to initialize vlan filtering on this port\n");
666 spin_lock_bh(&br
->lock
);
667 changed_addr
= br_stp_recalculate_bridge_id(br
);
669 if (netif_running(dev
) && netif_oper_up(dev
) &&
670 (br
->dev
->flags
& IFF_UP
))
671 br_stp_enable_port(p
);
672 spin_unlock_bh(&br
->lock
);
674 br_ifinfo_notify(RTM_NEWLINK
, NULL
, p
);
677 call_netdevice_notifiers(NETDEV_CHANGEADDR
, br
->dev
);
679 br_mtu_auto_adjust(br
);
680 br_set_gso_limits(br
);
682 kobject_uevent(&p
->kobj
, KOBJ_ADD
);
687 list_del_rcu(&p
->list
);
688 br_fdb_delete_by_port(br
, p
, 0, 1);
689 nbp_update_port_count(br
);
691 netdev_upper_dev_unlink(dev
, br
->dev
);
693 dev
->priv_flags
&= ~IFF_BRIDGE_PORT
;
694 netdev_rx_handler_unregister(dev
);
696 br_netpoll_disable(p
);
698 sysfs_remove_link(br
->ifobj
, p
->dev
->name
);
700 kobject_put(&p
->kobj
);
701 dev_set_allmulti(dev
, -1);
707 /* called with RTNL */
708 int br_del_if(struct net_bridge
*br
, struct net_device
*dev
)
710 struct net_bridge_port
*p
;
713 p
= br_port_get_rtnl(dev
);
714 if (!p
|| p
->br
!= br
)
717 /* Since more than one interface can be attached to a bridge,
718 * there still maybe an alternate path for netconsole to use;
719 * therefore there is no reason for a NETDEV_RELEASE event.
723 br_mtu_auto_adjust(br
);
724 br_set_gso_limits(br
);
726 spin_lock_bh(&br
->lock
);
727 changed_addr
= br_stp_recalculate_bridge_id(br
);
728 spin_unlock_bh(&br
->lock
);
731 call_netdevice_notifiers(NETDEV_CHANGEADDR
, br
->dev
);
733 netdev_update_features(br
->dev
);
738 void br_port_flags_change(struct net_bridge_port
*p
, unsigned long mask
)
740 struct net_bridge
*br
= p
->br
;
742 if (mask
& BR_AUTO_MASK
)
743 nbp_update_port_count(br
);
745 if (mask
& BR_NEIGH_SUPPRESS
)
746 br_recalculate_neigh_suppress_enabled(br
);
749 bool br_port_flag_is_set(const struct net_device
*dev
, unsigned long flag
)
751 struct net_bridge_port
*p
;
753 p
= br_port_get_rtnl_rcu(dev
);
757 return p
->flags
& flag
;
759 EXPORT_SYMBOL_GPL(br_port_flag_is_set
);