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/init.h>
12 #include <linux/rculist.h>
13 #include <linux/spinlock.h>
14 #include <linux/times.h>
15 #include <linux/netdevice.h>
16 #include <linux/etherdevice.h>
17 #include <linux/jhash.h>
18 #include <linux/random.h>
19 #include <linux/slab.h>
20 #include <linux/atomic.h>
21 #include <asm/unaligned.h>
22 #include <linux/if_vlan.h>
23 #include <net/switchdev.h>
24 #include <trace/events/bridge.h>
25 #include "br_private.h"
27 static const struct rhashtable_params br_fdb_rht_params = {
28 .head_offset = offsetof(struct net_bridge_fdb_entry, rhnode),
29 .key_offset = offsetof(struct net_bridge_fdb_entry, key),
30 .key_len = sizeof(struct net_bridge_fdb_key),
31 .automatic_shrinking = true,
34 static struct kmem_cache *br_fdb_cache __read_mostly;
36 int __init br_fdb_init(void)
38 br_fdb_cache = kmem_cache_create("bridge_fdb_cache",
39 sizeof(struct net_bridge_fdb_entry),
41 SLAB_HWCACHE_ALIGN, NULL);
48 void br_fdb_fini(void)
50 kmem_cache_destroy(br_fdb_cache);
53 int br_fdb_hash_init(struct net_bridge *br)
55 return rhashtable_init(&br->fdb_hash_tbl, &br_fdb_rht_params);
58 void br_fdb_hash_fini(struct net_bridge *br)
60 rhashtable_destroy(&br->fdb_hash_tbl);
63 /* if topology_changing then use forward_delay (default 15 sec)
64 * otherwise keep longer (default 5 minutes)
66 static inline unsigned long hold_time(const struct net_bridge *br)
68 return br->topology_change ? br->forward_delay : br->ageing_time;
71 static inline int has_expired(const struct net_bridge *br,
72 const struct net_bridge_fdb_entry *fdb)
74 return !test_bit(BR_FDB_STATIC, &fdb->flags) &&
75 !test_bit(BR_FDB_ADDED_BY_EXT_LEARN, &fdb->flags) &&
76 time_before_eq(fdb->updated + hold_time(br), jiffies);
79 static void fdb_rcu_free(struct rcu_head *head)
81 struct net_bridge_fdb_entry *ent
82 = container_of(head, struct net_bridge_fdb_entry, rcu);
83 kmem_cache_free(br_fdb_cache, ent);
86 static int fdb_to_nud(const struct net_bridge *br,
87 const struct net_bridge_fdb_entry *fdb)
89 if (test_bit(BR_FDB_LOCAL, &fdb->flags))
91 else if (test_bit(BR_FDB_STATIC, &fdb->flags))
93 else if (has_expired(br, fdb))
99 static int fdb_fill_info(struct sk_buff *skb, const struct net_bridge *br,
100 const struct net_bridge_fdb_entry *fdb,
101 u32 portid, u32 seq, int type, unsigned int flags)
103 const struct net_bridge_port *dst = READ_ONCE(fdb->dst);
104 unsigned long now = jiffies;
105 struct nda_cacheinfo ci;
106 struct nlmsghdr *nlh;
109 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
113 ndm = nlmsg_data(nlh);
114 ndm->ndm_family = AF_BRIDGE;
119 ndm->ndm_ifindex = dst ? dst->dev->ifindex : br->dev->ifindex;
120 ndm->ndm_state = fdb_to_nud(br, fdb);
122 if (test_bit(BR_FDB_OFFLOADED, &fdb->flags))
123 ndm->ndm_flags |= NTF_OFFLOADED;
124 if (test_bit(BR_FDB_ADDED_BY_EXT_LEARN, &fdb->flags))
125 ndm->ndm_flags |= NTF_EXT_LEARNED;
126 if (test_bit(BR_FDB_STICKY, &fdb->flags))
127 ndm->ndm_flags |= NTF_STICKY;
129 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->key.addr))
130 goto nla_put_failure;
131 if (nla_put_u32(skb, NDA_MASTER, br->dev->ifindex))
132 goto nla_put_failure;
133 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
134 ci.ndm_confirmed = 0;
135 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
137 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
138 goto nla_put_failure;
140 if (fdb->key.vlan_id && nla_put(skb, NDA_VLAN, sizeof(u16),
142 goto nla_put_failure;
144 if (test_bit(BR_FDB_NOTIFY, &fdb->flags)) {
145 struct nlattr *nest = nla_nest_start(skb, NDA_FDB_EXT_ATTRS);
146 u8 notify_bits = FDB_NOTIFY_BIT;
149 goto nla_put_failure;
150 if (test_bit(BR_FDB_NOTIFY_INACTIVE, &fdb->flags))
151 notify_bits |= FDB_NOTIFY_INACTIVE_BIT;
153 if (nla_put_u8(skb, NFEA_ACTIVITY_NOTIFY, notify_bits)) {
154 nla_nest_cancel(skb, nest);
155 goto nla_put_failure;
158 nla_nest_end(skb, nest);
165 nlmsg_cancel(skb, nlh);
169 static inline size_t fdb_nlmsg_size(void)
171 return NLMSG_ALIGN(sizeof(struct ndmsg))
172 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
173 + nla_total_size(sizeof(u32)) /* NDA_MASTER */
174 + nla_total_size(sizeof(u16)) /* NDA_VLAN */
175 + nla_total_size(sizeof(struct nda_cacheinfo))
176 + nla_total_size(0) /* NDA_FDB_EXT_ATTRS */
177 + nla_total_size(sizeof(u8)); /* NFEA_ACTIVITY_NOTIFY */
180 static void fdb_notify(struct net_bridge *br,
181 const struct net_bridge_fdb_entry *fdb, int type,
184 struct net *net = dev_net(br->dev);
189 br_switchdev_fdb_notify(br, fdb, type);
191 skb = nlmsg_new(fdb_nlmsg_size(), GFP_ATOMIC);
195 err = fdb_fill_info(skb, br, fdb, 0, 0, type, 0);
197 /* -EMSGSIZE implies BUG in fdb_nlmsg_size() */
198 WARN_ON(err == -EMSGSIZE);
202 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
205 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
208 static struct net_bridge_fdb_entry *fdb_find_rcu(struct rhashtable *tbl,
209 const unsigned char *addr,
212 struct net_bridge_fdb_key key;
214 WARN_ON_ONCE(!rcu_read_lock_held());
217 memcpy(key.addr.addr, addr, sizeof(key.addr.addr));
219 return rhashtable_lookup(tbl, &key, br_fdb_rht_params);
222 /* requires bridge hash_lock */
223 static struct net_bridge_fdb_entry *br_fdb_find(struct net_bridge *br,
224 const unsigned char *addr,
227 struct net_bridge_fdb_entry *fdb;
229 lockdep_assert_held_once(&br->hash_lock);
232 fdb = fdb_find_rcu(&br->fdb_hash_tbl, addr, vid);
238 struct net_device *br_fdb_find_port(const struct net_device *br_dev,
239 const unsigned char *addr,
242 struct net_bridge_fdb_entry *f;
243 struct net_device *dev = NULL;
244 struct net_bridge *br;
248 if (!netif_is_bridge_master(br_dev))
251 br = netdev_priv(br_dev);
253 f = br_fdb_find_rcu(br, addr, vid);
260 EXPORT_SYMBOL_GPL(br_fdb_find_port);
262 struct net_bridge_fdb_entry *br_fdb_find_rcu(struct net_bridge *br,
263 const unsigned char *addr,
266 return fdb_find_rcu(&br->fdb_hash_tbl, addr, vid);
269 /* When a static FDB entry is added, the mac address from the entry is
270 * added to the bridge private HW address list and all required ports
271 * are then updated with the new information.
274 static void fdb_add_hw_addr(struct net_bridge *br, const unsigned char *addr)
277 struct net_bridge_port *p;
281 list_for_each_entry(p, &br->port_list, list) {
282 if (!br_promisc_port(p)) {
283 err = dev_uc_add(p->dev, addr);
291 list_for_each_entry_continue_reverse(p, &br->port_list, list) {
292 if (!br_promisc_port(p))
293 dev_uc_del(p->dev, addr);
297 /* When a static FDB entry is deleted, the HW address from that entry is
298 * also removed from the bridge private HW address list and updates all
299 * the ports with needed information.
302 static void fdb_del_hw_addr(struct net_bridge *br, const unsigned char *addr)
304 struct net_bridge_port *p;
308 list_for_each_entry(p, &br->port_list, list) {
309 if (!br_promisc_port(p))
310 dev_uc_del(p->dev, addr);
314 static void fdb_delete(struct net_bridge *br, struct net_bridge_fdb_entry *f,
317 trace_fdb_delete(br, f);
319 if (test_bit(BR_FDB_STATIC, &f->flags))
320 fdb_del_hw_addr(br, f->key.addr.addr);
322 hlist_del_init_rcu(&f->fdb_node);
323 rhashtable_remove_fast(&br->fdb_hash_tbl, &f->rhnode,
325 fdb_notify(br, f, RTM_DELNEIGH, swdev_notify);
326 call_rcu(&f->rcu, fdb_rcu_free);
329 /* Delete a local entry if no other port had the same address. */
330 static void fdb_delete_local(struct net_bridge *br,
331 const struct net_bridge_port *p,
332 struct net_bridge_fdb_entry *f)
334 const unsigned char *addr = f->key.addr.addr;
335 struct net_bridge_vlan_group *vg;
336 const struct net_bridge_vlan *v;
337 struct net_bridge_port *op;
338 u16 vid = f->key.vlan_id;
340 /* Maybe another port has same hw addr? */
341 list_for_each_entry(op, &br->port_list, list) {
342 vg = nbp_vlan_group(op);
343 if (op != p && ether_addr_equal(op->dev->dev_addr, addr) &&
344 (!vid || br_vlan_find(vg, vid))) {
346 clear_bit(BR_FDB_ADDED_BY_USER, &f->flags);
351 vg = br_vlan_group(br);
352 v = br_vlan_find(vg, vid);
353 /* Maybe bridge device has same hw addr? */
354 if (p && ether_addr_equal(br->dev->dev_addr, addr) &&
355 (!vid || (v && br_vlan_should_use(v)))) {
357 clear_bit(BR_FDB_ADDED_BY_USER, &f->flags);
361 fdb_delete(br, f, true);
364 void br_fdb_find_delete_local(struct net_bridge *br,
365 const struct net_bridge_port *p,
366 const unsigned char *addr, u16 vid)
368 struct net_bridge_fdb_entry *f;
370 spin_lock_bh(&br->hash_lock);
371 f = br_fdb_find(br, addr, vid);
372 if (f && test_bit(BR_FDB_LOCAL, &f->flags) &&
373 !test_bit(BR_FDB_ADDED_BY_USER, &f->flags) && f->dst == p)
374 fdb_delete_local(br, p, f);
375 spin_unlock_bh(&br->hash_lock);
378 static struct net_bridge_fdb_entry *fdb_create(struct net_bridge *br,
379 struct net_bridge_port *source,
380 const unsigned char *addr,
384 struct net_bridge_fdb_entry *fdb;
387 fdb = kmem_cache_alloc(br_fdb_cache, GFP_ATOMIC);
391 memcpy(fdb->key.addr.addr, addr, ETH_ALEN);
392 WRITE_ONCE(fdb->dst, source);
393 fdb->key.vlan_id = vid;
395 fdb->updated = fdb->used = jiffies;
396 err = rhashtable_lookup_insert_fast(&br->fdb_hash_tbl, &fdb->rhnode,
399 kmem_cache_free(br_fdb_cache, fdb);
403 hlist_add_head_rcu(&fdb->fdb_node, &br->fdb_list);
408 static int fdb_add_local(struct net_bridge *br, struct net_bridge_port *source,
409 const unsigned char *addr, u16 vid)
411 struct net_bridge_fdb_entry *fdb;
413 if (!is_valid_ether_addr(addr))
416 fdb = br_fdb_find(br, addr, vid);
418 /* it is okay to have multiple ports with same
419 * address, just use the first one.
421 if (test_bit(BR_FDB_LOCAL, &fdb->flags))
423 br_warn(br, "adding interface %s with same address as a received packet (addr:%pM, vlan:%u)\n",
424 source ? source->dev->name : br->dev->name, addr, vid);
425 fdb_delete(br, fdb, true);
428 fdb = fdb_create(br, source, addr, vid,
429 BIT(BR_FDB_LOCAL) | BIT(BR_FDB_STATIC));
433 fdb_add_hw_addr(br, addr);
434 fdb_notify(br, fdb, RTM_NEWNEIGH, true);
438 void br_fdb_changeaddr(struct net_bridge_port *p, const unsigned char *newaddr)
440 struct net_bridge_vlan_group *vg;
441 struct net_bridge_fdb_entry *f;
442 struct net_bridge *br = p->br;
443 struct net_bridge_vlan *v;
445 spin_lock_bh(&br->hash_lock);
446 vg = nbp_vlan_group(p);
447 hlist_for_each_entry(f, &br->fdb_list, fdb_node) {
448 if (f->dst == p && test_bit(BR_FDB_LOCAL, &f->flags) &&
449 !test_bit(BR_FDB_ADDED_BY_USER, &f->flags)) {
451 fdb_delete_local(br, p, f);
453 /* if this port has no vlan information
454 * configured, we can safely be done at
457 if (!vg || !vg->num_vlans)
463 /* insert new address, may fail if invalid address or dup. */
464 fdb_add_local(br, p, newaddr, 0);
466 if (!vg || !vg->num_vlans)
469 /* Now add entries for every VLAN configured on the port.
470 * This function runs under RTNL so the bitmap will not change
473 list_for_each_entry(v, &vg->vlan_list, vlist)
474 fdb_add_local(br, p, newaddr, v->vid);
477 spin_unlock_bh(&br->hash_lock);
480 void br_fdb_change_mac_address(struct net_bridge *br, const u8 *newaddr)
482 struct net_bridge_vlan_group *vg;
483 struct net_bridge_fdb_entry *f;
484 struct net_bridge_vlan *v;
486 spin_lock_bh(&br->hash_lock);
488 /* If old entry was unassociated with any port, then delete it. */
489 f = br_fdb_find(br, br->dev->dev_addr, 0);
490 if (f && test_bit(BR_FDB_LOCAL, &f->flags) &&
491 !f->dst && !test_bit(BR_FDB_ADDED_BY_USER, &f->flags))
492 fdb_delete_local(br, NULL, f);
494 fdb_add_local(br, NULL, newaddr, 0);
495 vg = br_vlan_group(br);
496 if (!vg || !vg->num_vlans)
498 /* Now remove and add entries for every VLAN configured on the
499 * bridge. This function runs under RTNL so the bitmap will not
500 * change from under us.
502 list_for_each_entry(v, &vg->vlan_list, vlist) {
503 if (!br_vlan_should_use(v))
505 f = br_fdb_find(br, br->dev->dev_addr, v->vid);
506 if (f && test_bit(BR_FDB_LOCAL, &f->flags) &&
507 !f->dst && !test_bit(BR_FDB_ADDED_BY_USER, &f->flags))
508 fdb_delete_local(br, NULL, f);
509 fdb_add_local(br, NULL, newaddr, v->vid);
512 spin_unlock_bh(&br->hash_lock);
515 void br_fdb_cleanup(struct work_struct *work)
517 struct net_bridge *br = container_of(work, struct net_bridge,
519 struct net_bridge_fdb_entry *f = NULL;
520 unsigned long delay = hold_time(br);
521 unsigned long work_delay = delay;
522 unsigned long now = jiffies;
524 /* this part is tricky, in order to avoid blocking learning and
525 * consequently forwarding, we rely on rcu to delete objects with
526 * delayed freeing allowing us to continue traversing
529 hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
530 unsigned long this_timer = f->updated + delay;
532 if (test_bit(BR_FDB_STATIC, &f->flags) ||
533 test_bit(BR_FDB_ADDED_BY_EXT_LEARN, &f->flags)) {
534 if (test_bit(BR_FDB_NOTIFY, &f->flags)) {
535 if (time_after(this_timer, now))
536 work_delay = min(work_delay,
538 else if (!test_and_set_bit(BR_FDB_NOTIFY_INACTIVE,
540 fdb_notify(br, f, RTM_NEWNEIGH, false);
545 if (time_after(this_timer, now)) {
546 work_delay = min(work_delay, this_timer - now);
548 spin_lock_bh(&br->hash_lock);
549 if (!hlist_unhashed(&f->fdb_node))
550 fdb_delete(br, f, true);
551 spin_unlock_bh(&br->hash_lock);
556 /* Cleanup minimum 10 milliseconds apart */
557 work_delay = max_t(unsigned long, work_delay, msecs_to_jiffies(10));
558 mod_delayed_work(system_long_wq, &br->gc_work, work_delay);
561 static bool __fdb_flush_matches(const struct net_bridge *br,
562 const struct net_bridge_fdb_entry *f,
563 const struct net_bridge_fdb_flush_desc *desc)
565 const struct net_bridge_port *dst = READ_ONCE(f->dst);
566 int port_ifidx = dst ? dst->dev->ifindex : br->dev->ifindex;
568 if (desc->vlan_id && desc->vlan_id != f->key.vlan_id)
570 if (desc->port_ifindex && desc->port_ifindex != port_ifidx)
572 if (desc->flags_mask && (f->flags & desc->flags_mask) != desc->flags)
578 /* Flush forwarding database entries matching the description */
579 void br_fdb_flush(struct net_bridge *br,
580 const struct net_bridge_fdb_flush_desc *desc)
582 struct net_bridge_fdb_entry *f;
585 hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
586 if (!__fdb_flush_matches(br, f, desc))
589 spin_lock_bh(&br->hash_lock);
590 if (!hlist_unhashed(&f->fdb_node))
591 fdb_delete(br, f, true);
592 spin_unlock_bh(&br->hash_lock);
597 static unsigned long __ndm_state_to_fdb_flags(u16 ndm_state)
599 unsigned long flags = 0;
601 if (ndm_state & NUD_PERMANENT)
602 __set_bit(BR_FDB_LOCAL, &flags);
603 if (ndm_state & NUD_NOARP)
604 __set_bit(BR_FDB_STATIC, &flags);
609 static unsigned long __ndm_flags_to_fdb_flags(u8 ndm_flags)
611 unsigned long flags = 0;
613 if (ndm_flags & NTF_USE)
614 __set_bit(BR_FDB_ADDED_BY_USER, &flags);
615 if (ndm_flags & NTF_EXT_LEARNED)
616 __set_bit(BR_FDB_ADDED_BY_EXT_LEARN, &flags);
617 if (ndm_flags & NTF_OFFLOADED)
618 __set_bit(BR_FDB_OFFLOADED, &flags);
619 if (ndm_flags & NTF_STICKY)
620 __set_bit(BR_FDB_STICKY, &flags);
625 static int __fdb_flush_validate_ifindex(const struct net_bridge *br,
627 struct netlink_ext_ack *extack)
629 const struct net_device *dev;
631 dev = __dev_get_by_index(dev_net(br->dev), ifindex);
633 NL_SET_ERR_MSG_MOD(extack, "Unknown flush device ifindex");
636 if (!netif_is_bridge_master(dev) && !netif_is_bridge_port(dev)) {
637 NL_SET_ERR_MSG_MOD(extack, "Flush device is not a bridge or bridge port");
640 if (netif_is_bridge_master(dev) && dev != br->dev) {
641 NL_SET_ERR_MSG_MOD(extack,
642 "Flush bridge device does not match target bridge device");
645 if (netif_is_bridge_port(dev)) {
646 struct net_bridge_port *p = br_port_get_rtnl(dev);
649 NL_SET_ERR_MSG_MOD(extack, "Port belongs to a different bridge device");
657 int br_fdb_delete_bulk(struct ndmsg *ndm, struct nlattr *tb[],
658 struct net_device *dev, u16 vid,
659 struct netlink_ext_ack *extack)
661 u8 ndm_flags = ndm->ndm_flags & ~FDB_FLUSH_IGNORED_NDM_FLAGS;
662 struct net_bridge_fdb_flush_desc desc = { .vlan_id = vid };
663 struct net_bridge_port *p = NULL;
664 struct net_bridge *br;
666 if (netif_is_bridge_master(dev)) {
667 br = netdev_priv(dev);
669 p = br_port_get_rtnl(dev);
671 NL_SET_ERR_MSG_MOD(extack, "Device is not a bridge port");
677 if (ndm_flags & ~FDB_FLUSH_ALLOWED_NDM_FLAGS) {
678 NL_SET_ERR_MSG(extack, "Unsupported fdb flush ndm flag bits set");
681 if (ndm->ndm_state & ~FDB_FLUSH_ALLOWED_NDM_STATES) {
682 NL_SET_ERR_MSG(extack, "Unsupported fdb flush ndm state bits set");
686 desc.flags |= __ndm_state_to_fdb_flags(ndm->ndm_state);
687 desc.flags |= __ndm_flags_to_fdb_flags(ndm_flags);
688 if (tb[NDA_NDM_STATE_MASK]) {
689 u16 ndm_state_mask = nla_get_u16(tb[NDA_NDM_STATE_MASK]);
691 desc.flags_mask |= __ndm_state_to_fdb_flags(ndm_state_mask);
693 if (tb[NDA_NDM_FLAGS_MASK]) {
694 u8 ndm_flags_mask = nla_get_u8(tb[NDA_NDM_FLAGS_MASK]);
696 desc.flags_mask |= __ndm_flags_to_fdb_flags(ndm_flags_mask);
698 if (tb[NDA_IFINDEX]) {
699 int err, ifidx = nla_get_s32(tb[NDA_IFINDEX]);
701 err = __fdb_flush_validate_ifindex(br, ifidx, extack);
704 desc.port_ifindex = ifidx;
706 /* flush was invoked with port device and NTF_MASTER */
707 desc.port_ifindex = p->dev->ifindex;
710 br_debug(br, "flushing port ifindex: %d vlan id: %u flags: 0x%lx flags mask: 0x%lx\n",
711 desc.port_ifindex, desc.vlan_id, desc.flags, desc.flags_mask);
713 br_fdb_flush(br, &desc);
718 /* Flush all entries referring to a specific port.
719 * if do_all is set also flush static entries
720 * if vid is set delete all entries that match the vlan_id
722 void br_fdb_delete_by_port(struct net_bridge *br,
723 const struct net_bridge_port *p,
727 struct net_bridge_fdb_entry *f;
728 struct hlist_node *tmp;
730 spin_lock_bh(&br->hash_lock);
731 hlist_for_each_entry_safe(f, tmp, &br->fdb_list, fdb_node) {
736 if (test_bit(BR_FDB_STATIC, &f->flags) ||
737 (test_bit(BR_FDB_ADDED_BY_EXT_LEARN, &f->flags) &&
738 !test_bit(BR_FDB_OFFLOADED, &f->flags)) ||
739 (vid && f->key.vlan_id != vid))
742 if (test_bit(BR_FDB_LOCAL, &f->flags))
743 fdb_delete_local(br, p, f);
745 fdb_delete(br, f, true);
747 spin_unlock_bh(&br->hash_lock);
750 #if IS_ENABLED(CONFIG_ATM_LANE)
751 /* Interface used by ATM LANE hook to test
752 * if an addr is on some other bridge port */
753 int br_fdb_test_addr(struct net_device *dev, unsigned char *addr)
755 struct net_bridge_fdb_entry *fdb;
756 struct net_bridge_port *port;
760 port = br_port_get_rcu(dev);
764 const struct net_bridge_port *dst = NULL;
766 fdb = br_fdb_find_rcu(port->br, addr, 0);
768 dst = READ_ONCE(fdb->dst);
770 ret = dst && dst->dev != dev &&
771 dst->state == BR_STATE_FORWARDING;
777 #endif /* CONFIG_ATM_LANE */
780 * Fill buffer with forwarding table records in
783 int br_fdb_fillbuf(struct net_bridge *br, void *buf,
784 unsigned long maxnum, unsigned long skip)
786 struct net_bridge_fdb_entry *f;
787 struct __fdb_entry *fe = buf;
790 memset(buf, 0, maxnum*sizeof(struct __fdb_entry));
793 hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
797 if (has_expired(br, f))
800 /* ignore pseudo entry for local MAC address */
809 /* convert from internal format to API */
810 memcpy(fe->mac_addr, f->key.addr.addr, ETH_ALEN);
812 /* due to ABI compat need to split into hi/lo */
813 fe->port_no = f->dst->port_no;
814 fe->port_hi = f->dst->port_no >> 8;
816 fe->is_local = test_bit(BR_FDB_LOCAL, &f->flags);
817 if (!test_bit(BR_FDB_STATIC, &f->flags))
818 fe->ageing_timer_value = jiffies_delta_to_clock_t(jiffies - f->updated);
827 /* Add entry for local address of interface */
828 int br_fdb_add_local(struct net_bridge *br, struct net_bridge_port *source,
829 const unsigned char *addr, u16 vid)
833 spin_lock_bh(&br->hash_lock);
834 ret = fdb_add_local(br, source, addr, vid);
835 spin_unlock_bh(&br->hash_lock);
839 /* returns true if the fdb was modified */
840 static bool __fdb_mark_active(struct net_bridge_fdb_entry *fdb)
842 return !!(test_bit(BR_FDB_NOTIFY_INACTIVE, &fdb->flags) &&
843 test_and_clear_bit(BR_FDB_NOTIFY_INACTIVE, &fdb->flags));
846 void br_fdb_update(struct net_bridge *br, struct net_bridge_port *source,
847 const unsigned char *addr, u16 vid, unsigned long flags)
849 struct net_bridge_fdb_entry *fdb;
851 /* some users want to always flood. */
852 if (hold_time(br) == 0)
855 fdb = fdb_find_rcu(&br->fdb_hash_tbl, addr, vid);
857 /* attempt to update an entry for a local interface */
858 if (unlikely(test_bit(BR_FDB_LOCAL, &fdb->flags))) {
860 br_warn(br, "received packet on %s with own address as source address (addr:%pM, vlan:%u)\n",
861 source->dev->name, addr, vid);
863 unsigned long now = jiffies;
864 bool fdb_modified = false;
866 if (now != fdb->updated) {
868 fdb_modified = __fdb_mark_active(fdb);
871 /* fastpath: update of existing entry */
872 if (unlikely(source != READ_ONCE(fdb->dst) &&
873 !test_bit(BR_FDB_STICKY, &fdb->flags))) {
874 br_switchdev_fdb_notify(br, fdb, RTM_DELNEIGH);
875 WRITE_ONCE(fdb->dst, source);
877 /* Take over HW learned entry */
878 if (unlikely(test_bit(BR_FDB_ADDED_BY_EXT_LEARN,
880 clear_bit(BR_FDB_ADDED_BY_EXT_LEARN,
884 if (unlikely(test_bit(BR_FDB_ADDED_BY_USER, &flags)))
885 set_bit(BR_FDB_ADDED_BY_USER, &fdb->flags);
886 if (unlikely(fdb_modified)) {
887 trace_br_fdb_update(br, source, addr, vid, flags);
888 fdb_notify(br, fdb, RTM_NEWNEIGH, true);
892 spin_lock(&br->hash_lock);
893 fdb = fdb_create(br, source, addr, vid, flags);
895 trace_br_fdb_update(br, source, addr, vid, flags);
896 fdb_notify(br, fdb, RTM_NEWNEIGH, true);
898 /* else we lose race and someone else inserts
899 * it first, don't bother updating
901 spin_unlock(&br->hash_lock);
905 /* Dump information about entries, in response to GETNEIGH */
906 int br_fdb_dump(struct sk_buff *skb,
907 struct netlink_callback *cb,
908 struct net_device *dev,
909 struct net_device *filter_dev,
912 struct net_bridge *br = netdev_priv(dev);
913 struct net_bridge_fdb_entry *f;
916 if (!netif_is_bridge_master(dev))
920 err = ndo_dflt_fdb_dump(skb, cb, dev, NULL, idx);
926 hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
927 if (*idx < cb->args[2])
929 if (filter_dev && (!f->dst || f->dst->dev != filter_dev)) {
930 if (filter_dev != dev)
932 /* !f->dst is a special case for bridge
933 * It means the MAC belongs to the bridge
934 * Therefore need a little more filtering
935 * we only want to dump the !f->dst case
940 if (!filter_dev && f->dst)
943 err = fdb_fill_info(skb, br, f,
944 NETLINK_CB(cb->skb).portid,
958 int br_fdb_get(struct sk_buff *skb,
960 struct net_device *dev,
961 const unsigned char *addr,
962 u16 vid, u32 portid, u32 seq,
963 struct netlink_ext_ack *extack)
965 struct net_bridge *br = netdev_priv(dev);
966 struct net_bridge_fdb_entry *f;
970 f = br_fdb_find_rcu(br, addr, vid);
972 NL_SET_ERR_MSG(extack, "Fdb entry not found");
977 err = fdb_fill_info(skb, br, f, portid, seq,
984 /* returns true if the fdb is modified */
985 static bool fdb_handle_notify(struct net_bridge_fdb_entry *fdb, u8 notify)
987 bool modified = false;
989 /* allow to mark an entry as inactive, usually done on creation */
990 if ((notify & FDB_NOTIFY_INACTIVE_BIT) &&
991 !test_and_set_bit(BR_FDB_NOTIFY_INACTIVE, &fdb->flags))
994 if ((notify & FDB_NOTIFY_BIT) &&
995 !test_and_set_bit(BR_FDB_NOTIFY, &fdb->flags)) {
996 /* enabled activity tracking */
998 } else if (!(notify & FDB_NOTIFY_BIT) &&
999 test_and_clear_bit(BR_FDB_NOTIFY, &fdb->flags)) {
1000 /* disabled activity tracking, clear notify state */
1001 clear_bit(BR_FDB_NOTIFY_INACTIVE, &fdb->flags);
1008 /* Update (create or replace) forwarding database entry */
1009 static int fdb_add_entry(struct net_bridge *br, struct net_bridge_port *source,
1010 const u8 *addr, struct ndmsg *ndm, u16 flags, u16 vid,
1011 struct nlattr *nfea_tb[])
1013 bool is_sticky = !!(ndm->ndm_flags & NTF_STICKY);
1014 bool refresh = !nfea_tb[NFEA_DONT_REFRESH];
1015 struct net_bridge_fdb_entry *fdb;
1016 u16 state = ndm->ndm_state;
1017 bool modified = false;
1020 /* If the port cannot learn allow only local and static entries */
1021 if (source && !(state & NUD_PERMANENT) && !(state & NUD_NOARP) &&
1022 !(source->state == BR_STATE_LEARNING ||
1023 source->state == BR_STATE_FORWARDING))
1026 if (!source && !(state & NUD_PERMANENT)) {
1027 pr_info("bridge: RTM_NEWNEIGH %s without NUD_PERMANENT\n",
1032 if (is_sticky && (state & NUD_PERMANENT))
1035 if (nfea_tb[NFEA_ACTIVITY_NOTIFY]) {
1036 notify = nla_get_u8(nfea_tb[NFEA_ACTIVITY_NOTIFY]);
1037 if ((notify & ~BR_FDB_NOTIFY_SETTABLE_BITS) ||
1038 (notify & BR_FDB_NOTIFY_SETTABLE_BITS) == FDB_NOTIFY_INACTIVE_BIT)
1042 fdb = br_fdb_find(br, addr, vid);
1044 if (!(flags & NLM_F_CREATE))
1047 fdb = fdb_create(br, source, addr, vid, 0);
1053 if (flags & NLM_F_EXCL)
1056 if (READ_ONCE(fdb->dst) != source) {
1057 WRITE_ONCE(fdb->dst, source);
1062 if (fdb_to_nud(br, fdb) != state) {
1063 if (state & NUD_PERMANENT) {
1064 set_bit(BR_FDB_LOCAL, &fdb->flags);
1065 if (!test_and_set_bit(BR_FDB_STATIC, &fdb->flags))
1066 fdb_add_hw_addr(br, addr);
1067 } else if (state & NUD_NOARP) {
1068 clear_bit(BR_FDB_LOCAL, &fdb->flags);
1069 if (!test_and_set_bit(BR_FDB_STATIC, &fdb->flags))
1070 fdb_add_hw_addr(br, addr);
1072 clear_bit(BR_FDB_LOCAL, &fdb->flags);
1073 if (test_and_clear_bit(BR_FDB_STATIC, &fdb->flags))
1074 fdb_del_hw_addr(br, addr);
1080 if (is_sticky != test_bit(BR_FDB_STICKY, &fdb->flags)) {
1081 change_bit(BR_FDB_STICKY, &fdb->flags);
1085 if (fdb_handle_notify(fdb, notify))
1088 set_bit(BR_FDB_ADDED_BY_USER, &fdb->flags);
1090 fdb->used = jiffies;
1093 fdb->updated = jiffies;
1094 fdb_notify(br, fdb, RTM_NEWNEIGH, true);
1100 static int __br_fdb_add(struct ndmsg *ndm, struct net_bridge *br,
1101 struct net_bridge_port *p, const unsigned char *addr,
1102 u16 nlh_flags, u16 vid, struct nlattr *nfea_tb[],
1103 struct netlink_ext_ack *extack)
1107 if (ndm->ndm_flags & NTF_USE) {
1109 pr_info("bridge: RTM_NEWNEIGH %s with NTF_USE is not supported\n",
1113 if (!nbp_state_should_learn(p))
1118 br_fdb_update(br, p, addr, vid, BIT(BR_FDB_ADDED_BY_USER));
1121 } else if (ndm->ndm_flags & NTF_EXT_LEARNED) {
1122 if (!p && !(ndm->ndm_state & NUD_PERMANENT)) {
1123 NL_SET_ERR_MSG_MOD(extack,
1124 "FDB entry towards bridge must be permanent");
1127 err = br_fdb_external_learn_add(br, p, addr, vid, true);
1129 spin_lock_bh(&br->hash_lock);
1130 err = fdb_add_entry(br, p, addr, ndm, nlh_flags, vid, nfea_tb);
1131 spin_unlock_bh(&br->hash_lock);
1137 static const struct nla_policy br_nda_fdb_pol[NFEA_MAX + 1] = {
1138 [NFEA_ACTIVITY_NOTIFY] = { .type = NLA_U8 },
1139 [NFEA_DONT_REFRESH] = { .type = NLA_FLAG },
1142 /* Add new permanent fdb entry with RTM_NEWNEIGH */
1143 int br_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
1144 struct net_device *dev,
1145 const unsigned char *addr, u16 vid, u16 nlh_flags,
1146 struct netlink_ext_ack *extack)
1148 struct nlattr *nfea_tb[NFEA_MAX + 1], *attr;
1149 struct net_bridge_vlan_group *vg;
1150 struct net_bridge_port *p = NULL;
1151 struct net_bridge_vlan *v;
1152 struct net_bridge *br = NULL;
1155 trace_br_fdb_add(ndm, dev, addr, vid, nlh_flags);
1157 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE))) {
1158 pr_info("bridge: RTM_NEWNEIGH with invalid state %#x\n", ndm->ndm_state);
1162 if (is_zero_ether_addr(addr)) {
1163 pr_info("bridge: RTM_NEWNEIGH with invalid ether address\n");
1167 if (netif_is_bridge_master(dev)) {
1168 br = netdev_priv(dev);
1169 vg = br_vlan_group(br);
1171 p = br_port_get_rtnl(dev);
1173 pr_info("bridge: RTM_NEWNEIGH %s not a bridge port\n",
1178 vg = nbp_vlan_group(p);
1181 if (tb[NDA_FDB_EXT_ATTRS]) {
1182 attr = tb[NDA_FDB_EXT_ATTRS];
1183 err = nla_parse_nested(nfea_tb, NFEA_MAX, attr,
1184 br_nda_fdb_pol, extack);
1188 memset(nfea_tb, 0, sizeof(struct nlattr *) * (NFEA_MAX + 1));
1192 v = br_vlan_find(vg, vid);
1193 if (!v || !br_vlan_should_use(v)) {
1194 pr_info("bridge: RTM_NEWNEIGH with unconfigured vlan %d on %s\n", vid, dev->name);
1198 /* VID was specified, so use it. */
1199 err = __br_fdb_add(ndm, br, p, addr, nlh_flags, vid, nfea_tb,
1202 err = __br_fdb_add(ndm, br, p, addr, nlh_flags, 0, nfea_tb,
1204 if (err || !vg || !vg->num_vlans)
1207 /* We have vlans configured on this port and user didn't
1208 * specify a VLAN. To be nice, add/update entry for every
1209 * vlan on this port.
1211 list_for_each_entry(v, &vg->vlan_list, vlist) {
1212 if (!br_vlan_should_use(v))
1214 err = __br_fdb_add(ndm, br, p, addr, nlh_flags, v->vid,
1225 static int fdb_delete_by_addr_and_port(struct net_bridge *br,
1226 const struct net_bridge_port *p,
1227 const u8 *addr, u16 vlan)
1229 struct net_bridge_fdb_entry *fdb;
1231 fdb = br_fdb_find(br, addr, vlan);
1232 if (!fdb || READ_ONCE(fdb->dst) != p)
1235 fdb_delete(br, fdb, true);
1240 static int __br_fdb_delete(struct net_bridge *br,
1241 const struct net_bridge_port *p,
1242 const unsigned char *addr, u16 vid)
1246 spin_lock_bh(&br->hash_lock);
1247 err = fdb_delete_by_addr_and_port(br, p, addr, vid);
1248 spin_unlock_bh(&br->hash_lock);
1253 /* Remove neighbor entry with RTM_DELNEIGH */
1254 int br_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
1255 struct net_device *dev,
1256 const unsigned char *addr, u16 vid,
1257 struct netlink_ext_ack *extack)
1259 struct net_bridge_vlan_group *vg;
1260 struct net_bridge_port *p = NULL;
1261 struct net_bridge_vlan *v;
1262 struct net_bridge *br;
1265 if (netif_is_bridge_master(dev)) {
1266 br = netdev_priv(dev);
1267 vg = br_vlan_group(br);
1269 p = br_port_get_rtnl(dev);
1271 pr_info("bridge: RTM_DELNEIGH %s not a bridge port\n",
1275 vg = nbp_vlan_group(p);
1280 v = br_vlan_find(vg, vid);
1282 pr_info("bridge: RTM_DELNEIGH with unconfigured vlan %d on %s\n", vid, dev->name);
1286 err = __br_fdb_delete(br, p, addr, vid);
1289 err &= __br_fdb_delete(br, p, addr, 0);
1290 if (!vg || !vg->num_vlans)
1293 list_for_each_entry(v, &vg->vlan_list, vlist) {
1294 if (!br_vlan_should_use(v))
1296 err &= __br_fdb_delete(br, p, addr, v->vid);
1303 int br_fdb_sync_static(struct net_bridge *br, struct net_bridge_port *p)
1305 struct net_bridge_fdb_entry *f, *tmp;
1310 /* the key here is that static entries change only under rtnl */
1312 hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
1313 /* We only care for static entries */
1314 if (!test_bit(BR_FDB_STATIC, &f->flags))
1316 err = dev_uc_add(p->dev, f->key.addr.addr);
1326 hlist_for_each_entry_rcu(tmp, &br->fdb_list, fdb_node) {
1327 /* We only care for static entries */
1328 if (!test_bit(BR_FDB_STATIC, &tmp->flags))
1332 dev_uc_del(p->dev, tmp->key.addr.addr);
1338 void br_fdb_unsync_static(struct net_bridge *br, struct net_bridge_port *p)
1340 struct net_bridge_fdb_entry *f;
1345 hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
1346 /* We only care for static entries */
1347 if (!test_bit(BR_FDB_STATIC, &f->flags))
1350 dev_uc_del(p->dev, f->key.addr.addr);
1355 int br_fdb_external_learn_add(struct net_bridge *br, struct net_bridge_port *p,
1356 const unsigned char *addr, u16 vid,
1359 struct net_bridge_fdb_entry *fdb;
1360 bool modified = false;
1363 trace_br_fdb_external_learn_add(br, p, addr, vid);
1365 spin_lock_bh(&br->hash_lock);
1367 fdb = br_fdb_find(br, addr, vid);
1369 unsigned long flags = BIT(BR_FDB_ADDED_BY_EXT_LEARN);
1372 flags |= BIT(BR_FDB_ADDED_BY_USER);
1375 flags |= BIT(BR_FDB_LOCAL);
1377 fdb = fdb_create(br, p, addr, vid, flags);
1382 fdb_notify(br, fdb, RTM_NEWNEIGH, swdev_notify);
1384 fdb->updated = jiffies;
1386 if (READ_ONCE(fdb->dst) != p) {
1387 WRITE_ONCE(fdb->dst, p);
1391 if (test_bit(BR_FDB_ADDED_BY_EXT_LEARN, &fdb->flags)) {
1393 fdb->used = jiffies;
1394 } else if (!test_bit(BR_FDB_ADDED_BY_USER, &fdb->flags)) {
1395 /* Take over SW learned entry */
1396 set_bit(BR_FDB_ADDED_BY_EXT_LEARN, &fdb->flags);
1401 set_bit(BR_FDB_ADDED_BY_USER, &fdb->flags);
1404 set_bit(BR_FDB_LOCAL, &fdb->flags);
1407 fdb_notify(br, fdb, RTM_NEWNEIGH, swdev_notify);
1411 spin_unlock_bh(&br->hash_lock);
1416 int br_fdb_external_learn_del(struct net_bridge *br, struct net_bridge_port *p,
1417 const unsigned char *addr, u16 vid,
1420 struct net_bridge_fdb_entry *fdb;
1423 spin_lock_bh(&br->hash_lock);
1425 fdb = br_fdb_find(br, addr, vid);
1426 if (fdb && test_bit(BR_FDB_ADDED_BY_EXT_LEARN, &fdb->flags))
1427 fdb_delete(br, fdb, swdev_notify);
1431 spin_unlock_bh(&br->hash_lock);
1436 void br_fdb_offloaded_set(struct net_bridge *br, struct net_bridge_port *p,
1437 const unsigned char *addr, u16 vid, bool offloaded)
1439 struct net_bridge_fdb_entry *fdb;
1441 spin_lock_bh(&br->hash_lock);
1443 fdb = br_fdb_find(br, addr, vid);
1444 if (fdb && offloaded != test_bit(BR_FDB_OFFLOADED, &fdb->flags))
1445 change_bit(BR_FDB_OFFLOADED, &fdb->flags);
1447 spin_unlock_bh(&br->hash_lock);
1450 void br_fdb_clear_offload(const struct net_device *dev, u16 vid)
1452 struct net_bridge_fdb_entry *f;
1453 struct net_bridge_port *p;
1457 p = br_port_get_rtnl(dev);
1461 spin_lock_bh(&p->br->hash_lock);
1462 hlist_for_each_entry(f, &p->br->fdb_list, fdb_node) {
1463 if (f->dst == p && f->key.vlan_id == vid)
1464 clear_bit(BR_FDB_OFFLOADED, &f->flags);
1466 spin_unlock_bh(&p->br->hash_lock);
1468 EXPORT_SYMBOL_GPL(br_fdb_clear_offload);