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;
35 static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
36 const unsigned char *addr, u16 vid);
37 static void fdb_notify(struct net_bridge *br,
38 const struct net_bridge_fdb_entry *, int, bool);
40 int __init br_fdb_init(void)
42 br_fdb_cache = kmem_cache_create("bridge_fdb_cache",
43 sizeof(struct net_bridge_fdb_entry),
45 SLAB_HWCACHE_ALIGN, NULL);
52 void br_fdb_fini(void)
54 kmem_cache_destroy(br_fdb_cache);
57 int br_fdb_hash_init(struct net_bridge *br)
59 return rhashtable_init(&br->fdb_hash_tbl, &br_fdb_rht_params);
62 void br_fdb_hash_fini(struct net_bridge *br)
64 rhashtable_destroy(&br->fdb_hash_tbl);
67 /* if topology_changing then use forward_delay (default 15 sec)
68 * otherwise keep longer (default 5 minutes)
70 static inline unsigned long hold_time(const struct net_bridge *br)
72 return br->topology_change ? br->forward_delay : br->ageing_time;
75 static inline int has_expired(const struct net_bridge *br,
76 const struct net_bridge_fdb_entry *fdb)
78 return !test_bit(BR_FDB_STATIC, &fdb->flags) &&
79 !test_bit(BR_FDB_ADDED_BY_EXT_LEARN, &fdb->flags) &&
80 time_before_eq(fdb->updated + hold_time(br), jiffies);
83 static void fdb_rcu_free(struct rcu_head *head)
85 struct net_bridge_fdb_entry *ent
86 = container_of(head, struct net_bridge_fdb_entry, rcu);
87 kmem_cache_free(br_fdb_cache, ent);
90 static struct net_bridge_fdb_entry *fdb_find_rcu(struct rhashtable *tbl,
91 const unsigned char *addr,
94 struct net_bridge_fdb_key key;
96 WARN_ON_ONCE(!rcu_read_lock_held());
99 memcpy(key.addr.addr, addr, sizeof(key.addr.addr));
101 return rhashtable_lookup(tbl, &key, br_fdb_rht_params);
104 /* requires bridge hash_lock */
105 static struct net_bridge_fdb_entry *br_fdb_find(struct net_bridge *br,
106 const unsigned char *addr,
109 struct net_bridge_fdb_entry *fdb;
111 lockdep_assert_held_once(&br->hash_lock);
114 fdb = fdb_find_rcu(&br->fdb_hash_tbl, addr, vid);
120 struct net_device *br_fdb_find_port(const struct net_device *br_dev,
121 const unsigned char *addr,
124 struct net_bridge_fdb_entry *f;
125 struct net_device *dev = NULL;
126 struct net_bridge *br;
130 if (!netif_is_bridge_master(br_dev))
133 br = netdev_priv(br_dev);
135 f = br_fdb_find_rcu(br, addr, vid);
142 EXPORT_SYMBOL_GPL(br_fdb_find_port);
144 struct net_bridge_fdb_entry *br_fdb_find_rcu(struct net_bridge *br,
145 const unsigned char *addr,
148 return fdb_find_rcu(&br->fdb_hash_tbl, addr, vid);
151 /* When a static FDB entry is added, the mac address from the entry is
152 * added to the bridge private HW address list and all required ports
153 * are then updated with the new information.
156 static void fdb_add_hw_addr(struct net_bridge *br, const unsigned char *addr)
159 struct net_bridge_port *p;
163 list_for_each_entry(p, &br->port_list, list) {
164 if (!br_promisc_port(p)) {
165 err = dev_uc_add(p->dev, addr);
173 list_for_each_entry_continue_reverse(p, &br->port_list, list) {
174 if (!br_promisc_port(p))
175 dev_uc_del(p->dev, addr);
179 /* When a static FDB entry is deleted, the HW address from that entry is
180 * also removed from the bridge private HW address list and updates all
181 * the ports with needed information.
184 static void fdb_del_hw_addr(struct net_bridge *br, const unsigned char *addr)
186 struct net_bridge_port *p;
190 list_for_each_entry(p, &br->port_list, list) {
191 if (!br_promisc_port(p))
192 dev_uc_del(p->dev, addr);
196 static void fdb_delete(struct net_bridge *br, struct net_bridge_fdb_entry *f,
199 trace_fdb_delete(br, f);
201 if (test_bit(BR_FDB_STATIC, &f->flags))
202 fdb_del_hw_addr(br, f->key.addr.addr);
204 hlist_del_init_rcu(&f->fdb_node);
205 rhashtable_remove_fast(&br->fdb_hash_tbl, &f->rhnode,
207 fdb_notify(br, f, RTM_DELNEIGH, swdev_notify);
208 call_rcu(&f->rcu, fdb_rcu_free);
211 /* Delete a local entry if no other port had the same address. */
212 static void fdb_delete_local(struct net_bridge *br,
213 const struct net_bridge_port *p,
214 struct net_bridge_fdb_entry *f)
216 const unsigned char *addr = f->key.addr.addr;
217 struct net_bridge_vlan_group *vg;
218 const struct net_bridge_vlan *v;
219 struct net_bridge_port *op;
220 u16 vid = f->key.vlan_id;
222 /* Maybe another port has same hw addr? */
223 list_for_each_entry(op, &br->port_list, list) {
224 vg = nbp_vlan_group(op);
225 if (op != p && ether_addr_equal(op->dev->dev_addr, addr) &&
226 (!vid || br_vlan_find(vg, vid))) {
228 clear_bit(BR_FDB_ADDED_BY_USER, &f->flags);
233 vg = br_vlan_group(br);
234 v = br_vlan_find(vg, vid);
235 /* Maybe bridge device has same hw addr? */
236 if (p && ether_addr_equal(br->dev->dev_addr, addr) &&
237 (!vid || (v && br_vlan_should_use(v)))) {
239 clear_bit(BR_FDB_ADDED_BY_USER, &f->flags);
243 fdb_delete(br, f, true);
246 void br_fdb_find_delete_local(struct net_bridge *br,
247 const struct net_bridge_port *p,
248 const unsigned char *addr, u16 vid)
250 struct net_bridge_fdb_entry *f;
252 spin_lock_bh(&br->hash_lock);
253 f = br_fdb_find(br, addr, vid);
254 if (f && test_bit(BR_FDB_LOCAL, &f->flags) &&
255 !test_bit(BR_FDB_ADDED_BY_USER, &f->flags) && f->dst == p)
256 fdb_delete_local(br, p, f);
257 spin_unlock_bh(&br->hash_lock);
260 void br_fdb_changeaddr(struct net_bridge_port *p, const unsigned char *newaddr)
262 struct net_bridge_vlan_group *vg;
263 struct net_bridge_fdb_entry *f;
264 struct net_bridge *br = p->br;
265 struct net_bridge_vlan *v;
267 spin_lock_bh(&br->hash_lock);
268 vg = nbp_vlan_group(p);
269 hlist_for_each_entry(f, &br->fdb_list, fdb_node) {
270 if (f->dst == p && test_bit(BR_FDB_LOCAL, &f->flags) &&
271 !test_bit(BR_FDB_ADDED_BY_USER, &f->flags)) {
273 fdb_delete_local(br, p, f);
275 /* if this port has no vlan information
276 * configured, we can safely be done at
279 if (!vg || !vg->num_vlans)
285 /* insert new address, may fail if invalid address or dup. */
286 fdb_insert(br, p, newaddr, 0);
288 if (!vg || !vg->num_vlans)
291 /* Now add entries for every VLAN configured on the port.
292 * This function runs under RTNL so the bitmap will not change
295 list_for_each_entry(v, &vg->vlan_list, vlist)
296 fdb_insert(br, p, newaddr, v->vid);
299 spin_unlock_bh(&br->hash_lock);
302 void br_fdb_change_mac_address(struct net_bridge *br, const u8 *newaddr)
304 struct net_bridge_vlan_group *vg;
305 struct net_bridge_fdb_entry *f;
306 struct net_bridge_vlan *v;
308 spin_lock_bh(&br->hash_lock);
310 /* If old entry was unassociated with any port, then delete it. */
311 f = br_fdb_find(br, br->dev->dev_addr, 0);
312 if (f && test_bit(BR_FDB_LOCAL, &f->flags) &&
313 !f->dst && !test_bit(BR_FDB_ADDED_BY_USER, &f->flags))
314 fdb_delete_local(br, NULL, f);
316 fdb_insert(br, NULL, newaddr, 0);
317 vg = br_vlan_group(br);
318 if (!vg || !vg->num_vlans)
320 /* Now remove and add entries for every VLAN configured on the
321 * bridge. This function runs under RTNL so the bitmap will not
322 * change from under us.
324 list_for_each_entry(v, &vg->vlan_list, vlist) {
325 if (!br_vlan_should_use(v))
327 f = br_fdb_find(br, br->dev->dev_addr, v->vid);
328 if (f && test_bit(BR_FDB_LOCAL, &f->flags) &&
329 !f->dst && !test_bit(BR_FDB_ADDED_BY_USER, &f->flags))
330 fdb_delete_local(br, NULL, f);
331 fdb_insert(br, NULL, newaddr, v->vid);
334 spin_unlock_bh(&br->hash_lock);
337 void br_fdb_cleanup(struct work_struct *work)
339 struct net_bridge *br = container_of(work, struct net_bridge,
341 struct net_bridge_fdb_entry *f = NULL;
342 unsigned long delay = hold_time(br);
343 unsigned long work_delay = delay;
344 unsigned long now = jiffies;
346 /* this part is tricky, in order to avoid blocking learning and
347 * consequently forwarding, we rely on rcu to delete objects with
348 * delayed freeing allowing us to continue traversing
351 hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
352 unsigned long this_timer = f->updated + delay;
354 if (test_bit(BR_FDB_STATIC, &f->flags) ||
355 test_bit(BR_FDB_ADDED_BY_EXT_LEARN, &f->flags)) {
356 if (test_bit(BR_FDB_NOTIFY, &f->flags)) {
357 if (time_after(this_timer, now))
358 work_delay = min(work_delay,
360 else if (!test_and_set_bit(BR_FDB_NOTIFY_INACTIVE,
362 fdb_notify(br, f, RTM_NEWNEIGH, false);
367 if (time_after(this_timer, now)) {
368 work_delay = min(work_delay, this_timer - now);
370 spin_lock_bh(&br->hash_lock);
371 if (!hlist_unhashed(&f->fdb_node))
372 fdb_delete(br, f, true);
373 spin_unlock_bh(&br->hash_lock);
378 /* Cleanup minimum 10 milliseconds apart */
379 work_delay = max_t(unsigned long, work_delay, msecs_to_jiffies(10));
380 mod_delayed_work(system_long_wq, &br->gc_work, work_delay);
383 /* Completely flush all dynamic entries in forwarding database.*/
384 void br_fdb_flush(struct net_bridge *br)
386 struct net_bridge_fdb_entry *f;
387 struct hlist_node *tmp;
389 spin_lock_bh(&br->hash_lock);
390 hlist_for_each_entry_safe(f, tmp, &br->fdb_list, fdb_node) {
391 if (!test_bit(BR_FDB_STATIC, &f->flags))
392 fdb_delete(br, f, true);
394 spin_unlock_bh(&br->hash_lock);
397 /* Flush all entries referring to a specific port.
398 * if do_all is set also flush static entries
399 * if vid is set delete all entries that match the vlan_id
401 void br_fdb_delete_by_port(struct net_bridge *br,
402 const struct net_bridge_port *p,
406 struct net_bridge_fdb_entry *f;
407 struct hlist_node *tmp;
409 spin_lock_bh(&br->hash_lock);
410 hlist_for_each_entry_safe(f, tmp, &br->fdb_list, fdb_node) {
415 if (test_bit(BR_FDB_STATIC, &f->flags) ||
416 (test_bit(BR_FDB_ADDED_BY_EXT_LEARN, &f->flags) &&
417 !test_bit(BR_FDB_OFFLOADED, &f->flags)) ||
418 (vid && f->key.vlan_id != vid))
421 if (test_bit(BR_FDB_LOCAL, &f->flags))
422 fdb_delete_local(br, p, f);
424 fdb_delete(br, f, true);
426 spin_unlock_bh(&br->hash_lock);
429 #if IS_ENABLED(CONFIG_ATM_LANE)
430 /* Interface used by ATM LANE hook to test
431 * if an addr is on some other bridge port */
432 int br_fdb_test_addr(struct net_device *dev, unsigned char *addr)
434 struct net_bridge_fdb_entry *fdb;
435 struct net_bridge_port *port;
439 port = br_port_get_rcu(dev);
443 const struct net_bridge_port *dst = NULL;
445 fdb = br_fdb_find_rcu(port->br, addr, 0);
447 dst = READ_ONCE(fdb->dst);
449 ret = dst && dst->dev != dev &&
450 dst->state == BR_STATE_FORWARDING;
456 #endif /* CONFIG_ATM_LANE */
459 * Fill buffer with forwarding table records in
462 int br_fdb_fillbuf(struct net_bridge *br, void *buf,
463 unsigned long maxnum, unsigned long skip)
465 struct net_bridge_fdb_entry *f;
466 struct __fdb_entry *fe = buf;
469 memset(buf, 0, maxnum*sizeof(struct __fdb_entry));
472 hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
476 if (has_expired(br, f))
479 /* ignore pseudo entry for local MAC address */
488 /* convert from internal format to API */
489 memcpy(fe->mac_addr, f->key.addr.addr, ETH_ALEN);
491 /* due to ABI compat need to split into hi/lo */
492 fe->port_no = f->dst->port_no;
493 fe->port_hi = f->dst->port_no >> 8;
495 fe->is_local = test_bit(BR_FDB_LOCAL, &f->flags);
496 if (!test_bit(BR_FDB_STATIC, &f->flags))
497 fe->ageing_timer_value = jiffies_delta_to_clock_t(jiffies - f->updated);
506 static struct net_bridge_fdb_entry *fdb_create(struct net_bridge *br,
507 struct net_bridge_port *source,
508 const unsigned char *addr,
512 struct net_bridge_fdb_entry *fdb;
514 fdb = kmem_cache_alloc(br_fdb_cache, GFP_ATOMIC);
516 memcpy(fdb->key.addr.addr, addr, ETH_ALEN);
517 WRITE_ONCE(fdb->dst, source);
518 fdb->key.vlan_id = vid;
520 fdb->updated = fdb->used = jiffies;
521 if (rhashtable_lookup_insert_fast(&br->fdb_hash_tbl,
523 br_fdb_rht_params)) {
524 kmem_cache_free(br_fdb_cache, fdb);
527 hlist_add_head_rcu(&fdb->fdb_node, &br->fdb_list);
533 static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
534 const unsigned char *addr, u16 vid)
536 struct net_bridge_fdb_entry *fdb;
538 if (!is_valid_ether_addr(addr))
541 fdb = br_fdb_find(br, addr, vid);
543 /* it is okay to have multiple ports with same
544 * address, just use the first one.
546 if (test_bit(BR_FDB_LOCAL, &fdb->flags))
548 br_warn(br, "adding interface %s with same address as a received packet (addr:%pM, vlan:%u)\n",
549 source ? source->dev->name : br->dev->name, addr, vid);
550 fdb_delete(br, fdb, true);
553 fdb = fdb_create(br, source, addr, vid,
554 BIT(BR_FDB_LOCAL) | BIT(BR_FDB_STATIC));
558 fdb_add_hw_addr(br, addr);
559 fdb_notify(br, fdb, RTM_NEWNEIGH, true);
563 /* Add entry for local address of interface */
564 int br_fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
565 const unsigned char *addr, u16 vid)
569 spin_lock_bh(&br->hash_lock);
570 ret = fdb_insert(br, source, addr, vid);
571 spin_unlock_bh(&br->hash_lock);
575 /* returns true if the fdb was modified */
576 static bool __fdb_mark_active(struct net_bridge_fdb_entry *fdb)
578 return !!(test_bit(BR_FDB_NOTIFY_INACTIVE, &fdb->flags) &&
579 test_and_clear_bit(BR_FDB_NOTIFY_INACTIVE, &fdb->flags));
582 void br_fdb_update(struct net_bridge *br, struct net_bridge_port *source,
583 const unsigned char *addr, u16 vid, unsigned long flags)
585 struct net_bridge_fdb_entry *fdb;
587 /* some users want to always flood. */
588 if (hold_time(br) == 0)
591 fdb = fdb_find_rcu(&br->fdb_hash_tbl, addr, vid);
593 /* attempt to update an entry for a local interface */
594 if (unlikely(test_bit(BR_FDB_LOCAL, &fdb->flags))) {
596 br_warn(br, "received packet on %s with own address as source address (addr:%pM, vlan:%u)\n",
597 source->dev->name, addr, vid);
599 unsigned long now = jiffies;
600 bool fdb_modified = false;
602 if (now != fdb->updated) {
604 fdb_modified = __fdb_mark_active(fdb);
607 /* fastpath: update of existing entry */
608 if (unlikely(source != READ_ONCE(fdb->dst) &&
609 !test_bit(BR_FDB_STICKY, &fdb->flags))) {
610 br_switchdev_fdb_notify(br, fdb, RTM_DELNEIGH);
611 WRITE_ONCE(fdb->dst, source);
613 /* Take over HW learned entry */
614 if (unlikely(test_bit(BR_FDB_ADDED_BY_EXT_LEARN,
616 clear_bit(BR_FDB_ADDED_BY_EXT_LEARN,
620 if (unlikely(test_bit(BR_FDB_ADDED_BY_USER, &flags)))
621 set_bit(BR_FDB_ADDED_BY_USER, &fdb->flags);
622 if (unlikely(fdb_modified)) {
623 trace_br_fdb_update(br, source, addr, vid, flags);
624 fdb_notify(br, fdb, RTM_NEWNEIGH, true);
628 spin_lock(&br->hash_lock);
629 fdb = fdb_create(br, source, addr, vid, flags);
631 trace_br_fdb_update(br, source, addr, vid, flags);
632 fdb_notify(br, fdb, RTM_NEWNEIGH, true);
634 /* else we lose race and someone else inserts
635 * it first, don't bother updating
637 spin_unlock(&br->hash_lock);
641 static int fdb_to_nud(const struct net_bridge *br,
642 const struct net_bridge_fdb_entry *fdb)
644 if (test_bit(BR_FDB_LOCAL, &fdb->flags))
645 return NUD_PERMANENT;
646 else if (test_bit(BR_FDB_STATIC, &fdb->flags))
648 else if (has_expired(br, fdb))
651 return NUD_REACHABLE;
654 static int fdb_fill_info(struct sk_buff *skb, const struct net_bridge *br,
655 const struct net_bridge_fdb_entry *fdb,
656 u32 portid, u32 seq, int type, unsigned int flags)
658 const struct net_bridge_port *dst = READ_ONCE(fdb->dst);
659 unsigned long now = jiffies;
660 struct nda_cacheinfo ci;
661 struct nlmsghdr *nlh;
664 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
668 ndm = nlmsg_data(nlh);
669 ndm->ndm_family = AF_BRIDGE;
674 ndm->ndm_ifindex = dst ? dst->dev->ifindex : br->dev->ifindex;
675 ndm->ndm_state = fdb_to_nud(br, fdb);
677 if (test_bit(BR_FDB_OFFLOADED, &fdb->flags))
678 ndm->ndm_flags |= NTF_OFFLOADED;
679 if (test_bit(BR_FDB_ADDED_BY_EXT_LEARN, &fdb->flags))
680 ndm->ndm_flags |= NTF_EXT_LEARNED;
681 if (test_bit(BR_FDB_STICKY, &fdb->flags))
682 ndm->ndm_flags |= NTF_STICKY;
684 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->key.addr))
685 goto nla_put_failure;
686 if (nla_put_u32(skb, NDA_MASTER, br->dev->ifindex))
687 goto nla_put_failure;
688 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
689 ci.ndm_confirmed = 0;
690 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
692 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
693 goto nla_put_failure;
695 if (fdb->key.vlan_id && nla_put(skb, NDA_VLAN, sizeof(u16),
697 goto nla_put_failure;
699 if (test_bit(BR_FDB_NOTIFY, &fdb->flags)) {
700 struct nlattr *nest = nla_nest_start(skb, NDA_FDB_EXT_ATTRS);
701 u8 notify_bits = FDB_NOTIFY_BIT;
704 goto nla_put_failure;
705 if (test_bit(BR_FDB_NOTIFY_INACTIVE, &fdb->flags))
706 notify_bits |= FDB_NOTIFY_INACTIVE_BIT;
708 if (nla_put_u8(skb, NFEA_ACTIVITY_NOTIFY, notify_bits)) {
709 nla_nest_cancel(skb, nest);
710 goto nla_put_failure;
713 nla_nest_end(skb, nest);
720 nlmsg_cancel(skb, nlh);
724 static inline size_t fdb_nlmsg_size(void)
726 return NLMSG_ALIGN(sizeof(struct ndmsg))
727 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
728 + nla_total_size(sizeof(u32)) /* NDA_MASTER */
729 + nla_total_size(sizeof(u16)) /* NDA_VLAN */
730 + nla_total_size(sizeof(struct nda_cacheinfo))
731 + nla_total_size(0) /* NDA_FDB_EXT_ATTRS */
732 + nla_total_size(sizeof(u8)); /* NFEA_ACTIVITY_NOTIFY */
735 static int br_fdb_replay_one(struct net_bridge *br, struct notifier_block *nb,
736 const struct net_bridge_fdb_entry *fdb,
737 unsigned long action, const void *ctx)
739 const struct net_bridge_port *p = READ_ONCE(fdb->dst);
740 struct switchdev_notifier_fdb_info item;
743 item.addr = fdb->key.addr.addr;
744 item.vid = fdb->key.vlan_id;
745 item.added_by_user = test_bit(BR_FDB_ADDED_BY_USER, &fdb->flags);
746 item.offloaded = test_bit(BR_FDB_OFFLOADED, &fdb->flags);
747 item.is_local = test_bit(BR_FDB_LOCAL, &fdb->flags);
748 item.info.dev = (!p || item.is_local) ? br->dev : p->dev;
751 err = nb->notifier_call(nb, action, &item);
752 return notifier_to_errno(err);
755 int br_fdb_replay(const struct net_device *br_dev, const void *ctx, bool adding,
756 struct notifier_block *nb)
758 struct net_bridge_fdb_entry *fdb;
759 struct net_bridge *br;
760 unsigned long action;
766 if (!netif_is_bridge_master(br_dev))
769 br = netdev_priv(br_dev);
772 action = SWITCHDEV_FDB_ADD_TO_DEVICE;
774 action = SWITCHDEV_FDB_DEL_TO_DEVICE;
778 hlist_for_each_entry_rcu(fdb, &br->fdb_list, fdb_node) {
779 err = br_fdb_replay_one(br, nb, fdb, action, ctx);
789 static void fdb_notify(struct net_bridge *br,
790 const struct net_bridge_fdb_entry *fdb, int type,
793 struct net *net = dev_net(br->dev);
798 br_switchdev_fdb_notify(br, fdb, type);
800 skb = nlmsg_new(fdb_nlmsg_size(), GFP_ATOMIC);
804 err = fdb_fill_info(skb, br, fdb, 0, 0, type, 0);
806 /* -EMSGSIZE implies BUG in fdb_nlmsg_size() */
807 WARN_ON(err == -EMSGSIZE);
811 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
814 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
817 /* Dump information about entries, in response to GETNEIGH */
818 int br_fdb_dump(struct sk_buff *skb,
819 struct netlink_callback *cb,
820 struct net_device *dev,
821 struct net_device *filter_dev,
824 struct net_bridge *br = netdev_priv(dev);
825 struct net_bridge_fdb_entry *f;
828 if (!(dev->priv_flags & IFF_EBRIDGE))
832 err = ndo_dflt_fdb_dump(skb, cb, dev, NULL, idx);
838 hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
839 if (*idx < cb->args[2])
841 if (filter_dev && (!f->dst || f->dst->dev != filter_dev)) {
842 if (filter_dev != dev)
844 /* !f->dst is a special case for bridge
845 * It means the MAC belongs to the bridge
846 * Therefore need a little more filtering
847 * we only want to dump the !f->dst case
852 if (!filter_dev && f->dst)
855 err = fdb_fill_info(skb, br, f,
856 NETLINK_CB(cb->skb).portid,
870 int br_fdb_get(struct sk_buff *skb,
872 struct net_device *dev,
873 const unsigned char *addr,
874 u16 vid, u32 portid, u32 seq,
875 struct netlink_ext_ack *extack)
877 struct net_bridge *br = netdev_priv(dev);
878 struct net_bridge_fdb_entry *f;
882 f = br_fdb_find_rcu(br, addr, vid);
884 NL_SET_ERR_MSG(extack, "Fdb entry not found");
889 err = fdb_fill_info(skb, br, f, portid, seq,
896 /* returns true if the fdb is modified */
897 static bool fdb_handle_notify(struct net_bridge_fdb_entry *fdb, u8 notify)
899 bool modified = false;
901 /* allow to mark an entry as inactive, usually done on creation */
902 if ((notify & FDB_NOTIFY_INACTIVE_BIT) &&
903 !test_and_set_bit(BR_FDB_NOTIFY_INACTIVE, &fdb->flags))
906 if ((notify & FDB_NOTIFY_BIT) &&
907 !test_and_set_bit(BR_FDB_NOTIFY, &fdb->flags)) {
908 /* enabled activity tracking */
910 } else if (!(notify & FDB_NOTIFY_BIT) &&
911 test_and_clear_bit(BR_FDB_NOTIFY, &fdb->flags)) {
912 /* disabled activity tracking, clear notify state */
913 clear_bit(BR_FDB_NOTIFY_INACTIVE, &fdb->flags);
920 /* Update (create or replace) forwarding database entry */
921 static int fdb_add_entry(struct net_bridge *br, struct net_bridge_port *source,
922 const u8 *addr, struct ndmsg *ndm, u16 flags, u16 vid,
923 struct nlattr *nfea_tb[])
925 bool is_sticky = !!(ndm->ndm_flags & NTF_STICKY);
926 bool refresh = !nfea_tb[NFEA_DONT_REFRESH];
927 struct net_bridge_fdb_entry *fdb;
928 u16 state = ndm->ndm_state;
929 bool modified = false;
932 /* If the port cannot learn allow only local and static entries */
933 if (source && !(state & NUD_PERMANENT) && !(state & NUD_NOARP) &&
934 !(source->state == BR_STATE_LEARNING ||
935 source->state == BR_STATE_FORWARDING))
938 if (!source && !(state & NUD_PERMANENT)) {
939 pr_info("bridge: RTM_NEWNEIGH %s without NUD_PERMANENT\n",
944 if (is_sticky && (state & NUD_PERMANENT))
947 if (nfea_tb[NFEA_ACTIVITY_NOTIFY]) {
948 notify = nla_get_u8(nfea_tb[NFEA_ACTIVITY_NOTIFY]);
949 if ((notify & ~BR_FDB_NOTIFY_SETTABLE_BITS) ||
950 (notify & BR_FDB_NOTIFY_SETTABLE_BITS) == FDB_NOTIFY_INACTIVE_BIT)
954 fdb = br_fdb_find(br, addr, vid);
956 if (!(flags & NLM_F_CREATE))
959 fdb = fdb_create(br, source, addr, vid, 0);
965 if (flags & NLM_F_EXCL)
968 if (READ_ONCE(fdb->dst) != source) {
969 WRITE_ONCE(fdb->dst, source);
974 if (fdb_to_nud(br, fdb) != state) {
975 if (state & NUD_PERMANENT) {
976 set_bit(BR_FDB_LOCAL, &fdb->flags);
977 if (!test_and_set_bit(BR_FDB_STATIC, &fdb->flags))
978 fdb_add_hw_addr(br, addr);
979 } else if (state & NUD_NOARP) {
980 clear_bit(BR_FDB_LOCAL, &fdb->flags);
981 if (!test_and_set_bit(BR_FDB_STATIC, &fdb->flags))
982 fdb_add_hw_addr(br, addr);
984 clear_bit(BR_FDB_LOCAL, &fdb->flags);
985 if (test_and_clear_bit(BR_FDB_STATIC, &fdb->flags))
986 fdb_del_hw_addr(br, addr);
992 if (is_sticky != test_bit(BR_FDB_STICKY, &fdb->flags)) {
993 change_bit(BR_FDB_STICKY, &fdb->flags);
997 if (fdb_handle_notify(fdb, notify))
1000 set_bit(BR_FDB_ADDED_BY_USER, &fdb->flags);
1002 fdb->used = jiffies;
1005 fdb->updated = jiffies;
1006 fdb_notify(br, fdb, RTM_NEWNEIGH, true);
1012 static int __br_fdb_add(struct ndmsg *ndm, struct net_bridge *br,
1013 struct net_bridge_port *p, const unsigned char *addr,
1014 u16 nlh_flags, u16 vid, struct nlattr *nfea_tb[],
1015 struct netlink_ext_ack *extack)
1019 if (ndm->ndm_flags & NTF_USE) {
1021 pr_info("bridge: RTM_NEWNEIGH %s with NTF_USE is not supported\n",
1025 if (!nbp_state_should_learn(p))
1030 br_fdb_update(br, p, addr, vid, BIT(BR_FDB_ADDED_BY_USER));
1033 } else if (ndm->ndm_flags & NTF_EXT_LEARNED) {
1034 if (!p && !(ndm->ndm_state & NUD_PERMANENT)) {
1035 NL_SET_ERR_MSG_MOD(extack,
1036 "FDB entry towards bridge must be permanent");
1039 err = br_fdb_external_learn_add(br, p, addr, vid, true);
1041 spin_lock_bh(&br->hash_lock);
1042 err = fdb_add_entry(br, p, addr, ndm, nlh_flags, vid, nfea_tb);
1043 spin_unlock_bh(&br->hash_lock);
1049 static const struct nla_policy br_nda_fdb_pol[NFEA_MAX + 1] = {
1050 [NFEA_ACTIVITY_NOTIFY] = { .type = NLA_U8 },
1051 [NFEA_DONT_REFRESH] = { .type = NLA_FLAG },
1054 /* Add new permanent fdb entry with RTM_NEWNEIGH */
1055 int br_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
1056 struct net_device *dev,
1057 const unsigned char *addr, u16 vid, u16 nlh_flags,
1058 struct netlink_ext_ack *extack)
1060 struct nlattr *nfea_tb[NFEA_MAX + 1], *attr;
1061 struct net_bridge_vlan_group *vg;
1062 struct net_bridge_port *p = NULL;
1063 struct net_bridge_vlan *v;
1064 struct net_bridge *br = NULL;
1067 trace_br_fdb_add(ndm, dev, addr, vid, nlh_flags);
1069 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE))) {
1070 pr_info("bridge: RTM_NEWNEIGH with invalid state %#x\n", ndm->ndm_state);
1074 if (is_zero_ether_addr(addr)) {
1075 pr_info("bridge: RTM_NEWNEIGH with invalid ether address\n");
1079 if (dev->priv_flags & IFF_EBRIDGE) {
1080 br = netdev_priv(dev);
1081 vg = br_vlan_group(br);
1083 p = br_port_get_rtnl(dev);
1085 pr_info("bridge: RTM_NEWNEIGH %s not a bridge port\n",
1090 vg = nbp_vlan_group(p);
1093 if (tb[NDA_FDB_EXT_ATTRS]) {
1094 attr = tb[NDA_FDB_EXT_ATTRS];
1095 err = nla_parse_nested(nfea_tb, NFEA_MAX, attr,
1096 br_nda_fdb_pol, extack);
1100 memset(nfea_tb, 0, sizeof(struct nlattr *) * (NFEA_MAX + 1));
1104 v = br_vlan_find(vg, vid);
1105 if (!v || !br_vlan_should_use(v)) {
1106 pr_info("bridge: RTM_NEWNEIGH with unconfigured vlan %d on %s\n", vid, dev->name);
1110 /* VID was specified, so use it. */
1111 err = __br_fdb_add(ndm, br, p, addr, nlh_flags, vid, nfea_tb,
1114 err = __br_fdb_add(ndm, br, p, addr, nlh_flags, 0, nfea_tb,
1116 if (err || !vg || !vg->num_vlans)
1119 /* We have vlans configured on this port and user didn't
1120 * specify a VLAN. To be nice, add/update entry for every
1121 * vlan on this port.
1123 list_for_each_entry(v, &vg->vlan_list, vlist) {
1124 if (!br_vlan_should_use(v))
1126 err = __br_fdb_add(ndm, br, p, addr, nlh_flags, v->vid,
1137 static int fdb_delete_by_addr_and_port(struct net_bridge *br,
1138 const struct net_bridge_port *p,
1139 const u8 *addr, u16 vlan)
1141 struct net_bridge_fdb_entry *fdb;
1143 fdb = br_fdb_find(br, addr, vlan);
1144 if (!fdb || READ_ONCE(fdb->dst) != p)
1147 fdb_delete(br, fdb, true);
1152 static int __br_fdb_delete(struct net_bridge *br,
1153 const struct net_bridge_port *p,
1154 const unsigned char *addr, u16 vid)
1158 spin_lock_bh(&br->hash_lock);
1159 err = fdb_delete_by_addr_and_port(br, p, addr, vid);
1160 spin_unlock_bh(&br->hash_lock);
1165 /* Remove neighbor entry with RTM_DELNEIGH */
1166 int br_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
1167 struct net_device *dev,
1168 const unsigned char *addr, u16 vid)
1170 struct net_bridge_vlan_group *vg;
1171 struct net_bridge_port *p = NULL;
1172 struct net_bridge_vlan *v;
1173 struct net_bridge *br;
1176 if (dev->priv_flags & IFF_EBRIDGE) {
1177 br = netdev_priv(dev);
1178 vg = br_vlan_group(br);
1180 p = br_port_get_rtnl(dev);
1182 pr_info("bridge: RTM_DELNEIGH %s not a bridge port\n",
1186 vg = nbp_vlan_group(p);
1191 v = br_vlan_find(vg, vid);
1193 pr_info("bridge: RTM_DELNEIGH with unconfigured vlan %d on %s\n", vid, dev->name);
1197 err = __br_fdb_delete(br, p, addr, vid);
1200 err &= __br_fdb_delete(br, p, addr, 0);
1201 if (!vg || !vg->num_vlans)
1204 list_for_each_entry(v, &vg->vlan_list, vlist) {
1205 if (!br_vlan_should_use(v))
1207 err &= __br_fdb_delete(br, p, addr, v->vid);
1214 int br_fdb_sync_static(struct net_bridge *br, struct net_bridge_port *p)
1216 struct net_bridge_fdb_entry *f, *tmp;
1221 /* the key here is that static entries change only under rtnl */
1223 hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
1224 /* We only care for static entries */
1225 if (!test_bit(BR_FDB_STATIC, &f->flags))
1227 err = dev_uc_add(p->dev, f->key.addr.addr);
1237 hlist_for_each_entry_rcu(tmp, &br->fdb_list, fdb_node) {
1238 /* We only care for static entries */
1239 if (!test_bit(BR_FDB_STATIC, &tmp->flags))
1243 dev_uc_del(p->dev, tmp->key.addr.addr);
1249 void br_fdb_unsync_static(struct net_bridge *br, struct net_bridge_port *p)
1251 struct net_bridge_fdb_entry *f;
1256 hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
1257 /* We only care for static entries */
1258 if (!test_bit(BR_FDB_STATIC, &f->flags))
1261 dev_uc_del(p->dev, f->key.addr.addr);
1266 int br_fdb_external_learn_add(struct net_bridge *br, struct net_bridge_port *p,
1267 const unsigned char *addr, u16 vid,
1270 struct net_bridge_fdb_entry *fdb;
1271 bool modified = false;
1274 trace_br_fdb_external_learn_add(br, p, addr, vid);
1276 spin_lock_bh(&br->hash_lock);
1278 fdb = br_fdb_find(br, addr, vid);
1280 unsigned long flags = BIT(BR_FDB_ADDED_BY_EXT_LEARN);
1283 flags |= BIT(BR_FDB_ADDED_BY_USER);
1286 flags |= BIT(BR_FDB_LOCAL);
1288 fdb = fdb_create(br, p, addr, vid, flags);
1293 fdb_notify(br, fdb, RTM_NEWNEIGH, swdev_notify);
1295 fdb->updated = jiffies;
1297 if (READ_ONCE(fdb->dst) != p) {
1298 WRITE_ONCE(fdb->dst, p);
1302 if (test_bit(BR_FDB_ADDED_BY_EXT_LEARN, &fdb->flags)) {
1304 fdb->used = jiffies;
1305 } else if (!test_bit(BR_FDB_ADDED_BY_USER, &fdb->flags)) {
1306 /* Take over SW learned entry */
1307 set_bit(BR_FDB_ADDED_BY_EXT_LEARN, &fdb->flags);
1312 set_bit(BR_FDB_ADDED_BY_USER, &fdb->flags);
1315 set_bit(BR_FDB_LOCAL, &fdb->flags);
1318 fdb_notify(br, fdb, RTM_NEWNEIGH, swdev_notify);
1322 spin_unlock_bh(&br->hash_lock);
1327 int br_fdb_external_learn_del(struct net_bridge *br, struct net_bridge_port *p,
1328 const unsigned char *addr, u16 vid,
1331 struct net_bridge_fdb_entry *fdb;
1334 spin_lock_bh(&br->hash_lock);
1336 fdb = br_fdb_find(br, addr, vid);
1337 if (fdb && test_bit(BR_FDB_ADDED_BY_EXT_LEARN, &fdb->flags))
1338 fdb_delete(br, fdb, swdev_notify);
1342 spin_unlock_bh(&br->hash_lock);
1347 void br_fdb_offloaded_set(struct net_bridge *br, struct net_bridge_port *p,
1348 const unsigned char *addr, u16 vid, bool offloaded)
1350 struct net_bridge_fdb_entry *fdb;
1352 spin_lock_bh(&br->hash_lock);
1354 fdb = br_fdb_find(br, addr, vid);
1355 if (fdb && offloaded != test_bit(BR_FDB_OFFLOADED, &fdb->flags))
1356 change_bit(BR_FDB_OFFLOADED, &fdb->flags);
1358 spin_unlock_bh(&br->hash_lock);
1361 void br_fdb_clear_offload(const struct net_device *dev, u16 vid)
1363 struct net_bridge_fdb_entry *f;
1364 struct net_bridge_port *p;
1368 p = br_port_get_rtnl(dev);
1372 spin_lock_bh(&p->br->hash_lock);
1373 hlist_for_each_entry(f, &p->br->fdb_list, fdb_node) {
1374 if (f->dst == p && f->key.vlan_id == vid)
1375 clear_bit(BR_FDB_OFFLOADED, &f->flags);
1377 spin_unlock_bh(&p->br->hash_lock);
1379 EXPORT_SYMBOL_GPL(br_fdb_clear_offload);