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
) {
56 if (ecmd
.base
.speed
> SPEED_10000
)
61 /* Old silly heuristics based on name */
62 if (!strncmp(dev
->name
, "lec", 3))
65 if (!strncmp(dev
->name
, "plip", 4))
68 return 100; /* assume old 10Mbps */
72 /* Check for port carrier transitions. */
73 void br_port_carrier_check(struct net_bridge_port
*p
, bool *notified
)
75 struct net_device
*dev
= p
->dev
;
76 struct net_bridge
*br
= p
->br
;
78 if (!(p
->flags
& BR_ADMIN_COST
) &&
79 netif_running(dev
) && netif_oper_up(dev
))
80 p
->path_cost
= port_cost(dev
);
83 if (!netif_running(br
->dev
))
86 spin_lock_bh(&br
->lock
);
87 if (netif_running(dev
) && netif_oper_up(dev
)) {
88 if (p
->state
== BR_STATE_DISABLED
) {
89 br_stp_enable_port(p
);
93 if (p
->state
!= BR_STATE_DISABLED
) {
94 br_stp_disable_port(p
);
98 spin_unlock_bh(&br
->lock
);
101 static void br_port_set_promisc(struct net_bridge_port
*p
)
105 if (br_promisc_port(p
))
108 err
= dev_set_promiscuity(p
->dev
, 1);
112 br_fdb_unsync_static(p
->br
, p
);
113 p
->flags
|= BR_PROMISC
;
116 static void br_port_clear_promisc(struct net_bridge_port
*p
)
120 /* Check if the port is already non-promisc or if it doesn't
121 * support UNICAST filtering. Without unicast filtering support
122 * we'll end up re-enabling promisc mode anyway, so just check for
125 if (!br_promisc_port(p
) || !(p
->dev
->priv_flags
& IFF_UNICAST_FLT
))
128 /* Since we'll be clearing the promisc mode, program the port
129 * first so that we don't have interruption in traffic.
131 err
= br_fdb_sync_static(p
->br
, p
);
135 dev_set_promiscuity(p
->dev
, -1);
136 p
->flags
&= ~BR_PROMISC
;
139 /* When a port is added or removed or when certain port flags
140 * change, this function is called to automatically manage
141 * promiscuity setting of all the bridge ports. We are always called
142 * under RTNL so can skip using rcu primitives.
144 void br_manage_promisc(struct net_bridge
*br
)
146 struct net_bridge_port
*p
;
147 bool set_all
= false;
149 /* If vlan filtering is disabled or bridge interface is placed
150 * into promiscuous mode, place all ports in promiscuous mode.
152 if ((br
->dev
->flags
& IFF_PROMISC
) || !br_vlan_enabled(br
->dev
))
155 list_for_each_entry(p
, &br
->port_list
, list
) {
157 br_port_set_promisc(p
);
159 /* If the number of auto-ports is <= 1, then all other
160 * ports will have their output configuration
161 * statically specified through fdbs. Since ingress
162 * on the auto-port becomes forwarding/egress to other
163 * ports and egress configuration is statically known,
164 * we can say that ingress configuration of the
165 * auto-port is also statically known.
166 * This lets us disable promiscuous mode and write
169 if ((p
->dev
->priv_flags
& IFF_UNICAST_FLT
) &&
170 (br
->auto_cnt
== 0 ||
171 (br
->auto_cnt
== 1 && br_auto_port(p
))))
172 br_port_clear_promisc(p
);
174 br_port_set_promisc(p
);
179 int nbp_backup_change(struct net_bridge_port
*p
,
180 struct net_device
*backup_dev
)
182 struct net_bridge_port
*old_backup
= rtnl_dereference(p
->backup_port
);
183 struct net_bridge_port
*backup_p
= NULL
;
188 if (!netif_is_bridge_port(backup_dev
))
191 backup_p
= br_port_get_rtnl(backup_dev
);
192 if (backup_p
->br
!= p
->br
)
199 if (old_backup
== backup_p
)
202 /* if the backup link is already set, clear it */
204 old_backup
->backup_redirected_cnt
--;
207 backup_p
->backup_redirected_cnt
++;
208 rcu_assign_pointer(p
->backup_port
, backup_p
);
213 static void nbp_backup_clear(struct net_bridge_port
*p
)
215 nbp_backup_change(p
, NULL
);
216 if (p
->backup_redirected_cnt
) {
217 struct net_bridge_port
*cur_p
;
219 list_for_each_entry(cur_p
, &p
->br
->port_list
, list
) {
220 struct net_bridge_port
*backup_p
;
222 backup_p
= rtnl_dereference(cur_p
->backup_port
);
224 nbp_backup_change(cur_p
, NULL
);
228 WARN_ON(rcu_access_pointer(p
->backup_port
) || p
->backup_redirected_cnt
);
231 static void nbp_update_port_count(struct net_bridge
*br
)
233 struct net_bridge_port
*p
;
236 list_for_each_entry(p
, &br
->port_list
, list
) {
240 if (br
->auto_cnt
!= cnt
) {
242 br_manage_promisc(br
);
246 static void nbp_delete_promisc(struct net_bridge_port
*p
)
248 /* If port is currently promiscuous, unset promiscuity.
249 * Otherwise, it is a static port so remove all addresses
252 dev_set_allmulti(p
->dev
, -1);
253 if (br_promisc_port(p
))
254 dev_set_promiscuity(p
->dev
, -1);
256 br_fdb_unsync_static(p
->br
, p
);
259 static void release_nbp(struct kobject
*kobj
)
261 struct net_bridge_port
*p
262 = container_of(kobj
, struct net_bridge_port
, kobj
);
266 static void brport_get_ownership(const struct kobject
*kobj
, kuid_t
*uid
, kgid_t
*gid
)
268 struct net_bridge_port
*p
= kobj_to_brport(kobj
);
270 net_ns_get_ownership(dev_net(p
->dev
), uid
, gid
);
273 static const struct kobj_type brport_ktype
= {
275 .sysfs_ops
= &brport_sysfs_ops
,
277 .release
= release_nbp
,
278 .get_ownership
= brport_get_ownership
,
281 static void destroy_nbp(struct net_bridge_port
*p
)
283 struct net_device
*dev
= p
->dev
;
287 netdev_put(dev
, &p
->dev_tracker
);
289 kobject_put(&p
->kobj
);
292 static void destroy_nbp_rcu(struct rcu_head
*head
)
294 struct net_bridge_port
*p
=
295 container_of(head
, struct net_bridge_port
, rcu
);
299 static unsigned get_max_headroom(struct net_bridge
*br
)
301 unsigned max_headroom
= 0;
302 struct net_bridge_port
*p
;
304 list_for_each_entry(p
, &br
->port_list
, list
) {
305 unsigned dev_headroom
= netdev_get_fwd_headroom(p
->dev
);
307 if (dev_headroom
> max_headroom
)
308 max_headroom
= dev_headroom
;
314 static void update_headroom(struct net_bridge
*br
, int new_hr
)
316 struct net_bridge_port
*p
;
318 list_for_each_entry(p
, &br
->port_list
, list
)
319 netdev_set_rx_headroom(p
->dev
, new_hr
);
321 br
->dev
->needed_headroom
= new_hr
;
324 /* Delete port(interface) from bridge is done in two steps.
325 * via RCU. First step, marks device as down. That deletes
326 * all the timers and stops new packets from flowing through.
328 * Final cleanup doesn't occur until after all CPU's finished
329 * processing packets.
331 * Protected from multiple admin operations by RTNL mutex
333 static void del_nbp(struct net_bridge_port
*p
)
335 struct net_bridge
*br
= p
->br
;
336 struct net_device
*dev
= p
->dev
;
338 sysfs_remove_link(br
->ifobj
, p
->dev
->name
);
340 nbp_delete_promisc(p
);
342 spin_lock_bh(&br
->lock
);
343 br_stp_disable_port(p
);
344 spin_unlock_bh(&br
->lock
);
346 br_mrp_port_del(br
, p
);
347 br_cfm_port_del(br
, p
);
349 br_ifinfo_notify(RTM_DELLINK
, NULL
, p
);
351 list_del_rcu(&p
->list
);
352 if (netdev_get_fwd_headroom(dev
) == br
->dev
->needed_headroom
)
353 update_headroom(br
, get_max_headroom(br
));
354 netdev_reset_rx_headroom(dev
);
357 br_fdb_delete_by_port(br
, p
, 0, 1);
358 switchdev_deferred_process();
361 nbp_update_port_count(br
);
363 netdev_upper_dev_unlink(dev
, br
->dev
);
365 dev
->priv_flags
&= ~IFF_BRIDGE_PORT
;
367 netdev_rx_handler_unregister(dev
);
369 br_multicast_del_port(p
);
371 kobject_uevent(&p
->kobj
, KOBJ_REMOVE
);
372 kobject_del(&p
->kobj
);
374 br_netpoll_disable(p
);
376 call_rcu(&p
->rcu
, destroy_nbp_rcu
);
379 /* Delete bridge device */
380 void br_dev_delete(struct net_device
*dev
, struct list_head
*head
)
382 struct net_bridge
*br
= netdev_priv(dev
);
383 struct net_bridge_port
*p
, *n
;
385 list_for_each_entry_safe(p
, n
, &br
->port_list
, list
) {
389 br_recalculate_neigh_suppress_enabled(br
);
391 br_fdb_delete_by_port(br
, NULL
, 0, 1);
393 cancel_delayed_work_sync(&br
->gc_work
);
395 br_sysfs_delbr(br
->dev
);
396 unregister_netdevice_queue(br
->dev
, head
);
399 /* find an available port number */
400 static int find_portno(struct net_bridge
*br
)
403 struct net_bridge_port
*p
;
404 unsigned long *inuse
;
406 inuse
= bitmap_zalloc(BR_MAX_PORTS
, GFP_KERNEL
);
410 __set_bit(0, inuse
); /* zero is reserved */
411 list_for_each_entry(p
, &br
->port_list
, list
)
412 __set_bit(p
->port_no
, inuse
);
414 index
= find_first_zero_bit(inuse
, BR_MAX_PORTS
);
417 return (index
>= BR_MAX_PORTS
) ? -EXFULL
: index
;
420 /* called with RTNL but without bridge lock */
421 static struct net_bridge_port
*new_nbp(struct net_bridge
*br
,
422 struct net_device
*dev
)
424 struct net_bridge_port
*p
;
427 index
= find_portno(br
);
429 return ERR_PTR(index
);
431 p
= kzalloc(sizeof(*p
), GFP_KERNEL
);
433 return ERR_PTR(-ENOMEM
);
436 netdev_hold(dev
, &p
->dev_tracker
, GFP_KERNEL
);
438 p
->path_cost
= port_cost(dev
);
439 p
->priority
= 0x8000 >> BR_PORT_BITS
;
441 p
->flags
= BR_LEARNING
| BR_FLOOD
| BR_MCAST_FLOOD
| BR_BCAST_FLOOD
;
443 br_set_state(p
, BR_STATE_DISABLED
);
444 br_stp_port_timer_init(p
);
445 err
= br_multicast_add_port(p
);
447 netdev_put(dev
, &p
->dev_tracker
);
455 int br_add_bridge(struct net
*net
, const char *name
)
457 struct net_device
*dev
;
460 dev
= alloc_netdev(sizeof(struct net_bridge
), name
, NET_NAME_UNKNOWN
,
466 dev_net_set(dev
, net
);
467 dev
->rtnl_link_ops
= &br_link_ops
;
469 res
= register_netdevice(dev
);
475 int br_del_bridge(struct net
*net
, const char *name
)
477 struct net_device
*dev
;
480 dev
= __dev_get_by_name(net
, name
);
482 ret
= -ENXIO
; /* Could not find device */
484 else if (!netif_is_bridge_master(dev
)) {
485 /* Attempt to delete non bridge device! */
489 else if (dev
->flags
& IFF_UP
) {
490 /* Not shutdown yet. */
495 br_dev_delete(dev
, NULL
);
500 /* MTU of the bridge pseudo-device: ETH_DATA_LEN or the minimum of the ports */
501 static int br_mtu_min(const struct net_bridge
*br
)
503 const struct net_bridge_port
*p
;
506 list_for_each_entry(p
, &br
->port_list
, list
)
507 if (!ret_mtu
|| ret_mtu
> p
->dev
->mtu
)
508 ret_mtu
= p
->dev
->mtu
;
510 return ret_mtu
? ret_mtu
: ETH_DATA_LEN
;
513 void br_mtu_auto_adjust(struct net_bridge
*br
)
517 /* if the bridge MTU was manually configured don't mess with it */
518 if (br_opt_get(br
, BROPT_MTU_SET_BY_USER
))
521 /* change to the minimum MTU and clear the flag which was set by
522 * the bridge ndo_change_mtu callback
524 dev_set_mtu(br
->dev
, br_mtu_min(br
));
525 br_opt_toggle(br
, BROPT_MTU_SET_BY_USER
, false);
528 static void br_set_gso_limits(struct net_bridge
*br
)
530 unsigned int tso_max_size
= TSO_MAX_SIZE
;
531 const struct net_bridge_port
*p
;
532 u16 tso_max_segs
= TSO_MAX_SEGS
;
534 list_for_each_entry(p
, &br
->port_list
, list
) {
535 tso_max_size
= min(tso_max_size
, p
->dev
->tso_max_size
);
536 tso_max_segs
= min(tso_max_segs
, p
->dev
->tso_max_segs
);
538 netif_set_tso_max_size(br
->dev
, tso_max_size
);
539 netif_set_tso_max_segs(br
->dev
, tso_max_segs
);
543 * Recomputes features using slave's features
545 netdev_features_t
br_features_recompute(struct net_bridge
*br
,
546 netdev_features_t features
)
548 struct net_bridge_port
*p
;
549 netdev_features_t mask
;
551 if (list_empty(&br
->port_list
))
555 features
&= ~NETIF_F_ONE_FOR_ALL
;
557 list_for_each_entry(p
, &br
->port_list
, list
) {
558 features
= netdev_increment_features(features
,
559 p
->dev
->features
, mask
);
561 features
= netdev_add_tso_features(features
, mask
);
566 /* called with RTNL */
567 int br_add_if(struct net_bridge
*br
, struct net_device
*dev
,
568 struct netlink_ext_ack
*extack
)
570 struct net_bridge_port
*p
;
572 unsigned br_hr
, dev_hr
;
573 bool changed_addr
, fdb_synced
= false;
575 /* Don't allow bridging non-ethernet like devices. */
576 if ((dev
->flags
& IFF_LOOPBACK
) ||
577 dev
->type
!= ARPHRD_ETHER
|| dev
->addr_len
!= ETH_ALEN
||
578 !is_valid_ether_addr(dev
->dev_addr
))
581 /* No bridging of bridges */
582 if (dev
->netdev_ops
->ndo_start_xmit
== br_dev_xmit
) {
583 NL_SET_ERR_MSG(extack
,
584 "Can not enslave a bridge to a bridge");
588 /* Device has master upper dev */
589 if (netdev_master_upper_dev_get(dev
))
592 /* No bridging devices that dislike that (e.g. wireless) */
593 if (dev
->priv_flags
& IFF_DONT_BRIDGE
) {
594 NL_SET_ERR_MSG(extack
,
595 "Device does not allow enslaving to a bridge");
599 p
= new_nbp(br
, dev
);
603 call_netdevice_notifiers(NETDEV_JOIN
, dev
);
605 err
= dev_set_allmulti(dev
, 1);
607 br_multicast_del_port(p
);
608 netdev_put(dev
, &p
->dev_tracker
);
609 kfree(p
); /* kobject not yet init'd, manually free */
613 err
= kobject_init_and_add(&p
->kobj
, &brport_ktype
, &(dev
->dev
.kobj
),
614 SYSFS_BRIDGE_PORT_ATTR
);
618 err
= br_sysfs_addif(p
);
622 err
= br_netpoll_enable(p
);
626 err
= netdev_rx_handler_register(dev
, br_get_rx_handler(dev
), p
);
630 dev
->priv_flags
|= IFF_BRIDGE_PORT
;
632 err
= netdev_master_upper_dev_link(dev
, br
->dev
, NULL
, NULL
, extack
);
636 dev_disable_lro(dev
);
638 list_add_rcu(&p
->list
, &br
->port_list
);
640 nbp_update_port_count(br
);
641 if (!br_promisc_port(p
) && (p
->dev
->priv_flags
& IFF_UNICAST_FLT
)) {
642 /* When updating the port count we also update all ports'
644 * A port leaving promiscuous mode normally gets the bridge's
645 * fdb synced to the unicast filter (if supported), however,
646 * `br_port_clear_promisc` does not distinguish between
647 * non-promiscuous ports and *new* ports, so we need to
648 * sync explicitly here.
650 fdb_synced
= br_fdb_sync_static(br
, p
) == 0;
652 netdev_err(dev
, "failed to sync bridge static fdb addresses to this port\n");
655 netdev_update_features(br
->dev
);
657 br_hr
= br
->dev
->needed_headroom
;
658 dev_hr
= netdev_get_fwd_headroom(dev
);
660 update_headroom(br
, dev_hr
);
662 netdev_set_rx_headroom(dev
, br_hr
);
664 if (br_fdb_add_local(br
, p
, dev
->dev_addr
, 0))
665 netdev_err(dev
, "failed insert local address bridge forwarding table\n");
667 if (br
->dev
->addr_assign_type
!= NET_ADDR_SET
) {
668 /* Ask for permission to use this MAC address now, even if we
669 * don't end up choosing it below.
671 err
= dev_pre_changeaddr_notify(br
->dev
, dev
->dev_addr
, extack
);
676 err
= nbp_vlan_init(p
, extack
);
678 netdev_err(dev
, "failed to initialize vlan filtering on this port\n");
682 spin_lock_bh(&br
->lock
);
683 changed_addr
= br_stp_recalculate_bridge_id(br
);
685 if (netif_running(dev
) && netif_oper_up(dev
) &&
686 (br
->dev
->flags
& IFF_UP
))
687 br_stp_enable_port(p
);
688 spin_unlock_bh(&br
->lock
);
690 br_ifinfo_notify(RTM_NEWLINK
, NULL
, p
);
693 call_netdevice_notifiers(NETDEV_CHANGEADDR
, br
->dev
);
695 br_mtu_auto_adjust(br
);
696 br_set_gso_limits(br
);
698 kobject_uevent(&p
->kobj
, KOBJ_ADD
);
704 br_fdb_unsync_static(br
, p
);
705 list_del_rcu(&p
->list
);
706 br_fdb_delete_by_port(br
, p
, 0, 1);
707 nbp_update_port_count(br
);
708 netdev_upper_dev_unlink(dev
, br
->dev
);
710 dev
->priv_flags
&= ~IFF_BRIDGE_PORT
;
711 netdev_rx_handler_unregister(dev
);
713 br_netpoll_disable(p
);
715 sysfs_remove_link(br
->ifobj
, p
->dev
->name
);
717 br_multicast_del_port(p
);
718 netdev_put(dev
, &p
->dev_tracker
);
719 kobject_put(&p
->kobj
);
720 dev_set_allmulti(dev
, -1);
725 /* called with RTNL */
726 int br_del_if(struct net_bridge
*br
, struct net_device
*dev
)
728 struct net_bridge_port
*p
;
731 p
= br_port_get_rtnl(dev
);
732 if (!p
|| p
->br
!= br
)
735 /* Since more than one interface can be attached to a bridge,
736 * there still maybe an alternate path for netconsole to use;
737 * therefore there is no reason for a NETDEV_RELEASE event.
741 br_mtu_auto_adjust(br
);
742 br_set_gso_limits(br
);
744 spin_lock_bh(&br
->lock
);
745 changed_addr
= br_stp_recalculate_bridge_id(br
);
746 spin_unlock_bh(&br
->lock
);
749 call_netdevice_notifiers(NETDEV_CHANGEADDR
, br
->dev
);
751 netdev_update_features(br
->dev
);
756 void br_port_flags_change(struct net_bridge_port
*p
, unsigned long mask
)
758 struct net_bridge
*br
= p
->br
;
760 if (mask
& BR_AUTO_MASK
)
761 nbp_update_port_count(br
);
763 if (mask
& (BR_NEIGH_SUPPRESS
| BR_NEIGH_VLAN_SUPPRESS
))
764 br_recalculate_neigh_suppress_enabled(br
);
767 bool br_port_flag_is_set(const struct net_device
*dev
, unsigned long flag
)
769 struct net_bridge_port
*p
;
771 p
= br_port_get_rtnl_rcu(dev
);
775 return p
->flags
& flag
;
777 EXPORT_SYMBOL_GPL(br_port_flag_is_set
);