Linux 4.19-rc7
[linux-2.6/btrfs-unstable.git] / net / bridge / br_fdb.c
blob502f663495308cde5b7d2a8bf841f6e0e7045130
1 /*
2 * Forwarding database
3 * Linux ethernet bridge
5 * Authors:
6 * Lennert Buytenhek <buytenh@gnu.org>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
14 #include <linux/kernel.h>
15 #include <linux/init.h>
16 #include <linux/rculist.h>
17 #include <linux/spinlock.h>
18 #include <linux/times.h>
19 #include <linux/netdevice.h>
20 #include <linux/etherdevice.h>
21 #include <linux/jhash.h>
22 #include <linux/random.h>
23 #include <linux/slab.h>
24 #include <linux/atomic.h>
25 #include <asm/unaligned.h>
26 #include <linux/if_vlan.h>
27 #include <net/switchdev.h>
28 #include <trace/events/bridge.h>
29 #include "br_private.h"
31 static const struct rhashtable_params br_fdb_rht_params = {
32 .head_offset = offsetof(struct net_bridge_fdb_entry, rhnode),
33 .key_offset = offsetof(struct net_bridge_fdb_entry, key),
34 .key_len = sizeof(struct net_bridge_fdb_key),
35 .automatic_shrinking = true,
36 .locks_mul = 1,
39 static struct kmem_cache *br_fdb_cache __read_mostly;
40 static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
41 const unsigned char *addr, u16 vid);
42 static void fdb_notify(struct net_bridge *br,
43 const struct net_bridge_fdb_entry *, int, bool);
45 int __init br_fdb_init(void)
47 br_fdb_cache = kmem_cache_create("bridge_fdb_cache",
48 sizeof(struct net_bridge_fdb_entry),
50 SLAB_HWCACHE_ALIGN, NULL);
51 if (!br_fdb_cache)
52 return -ENOMEM;
54 return 0;
57 void br_fdb_fini(void)
59 kmem_cache_destroy(br_fdb_cache);
62 int br_fdb_hash_init(struct net_bridge *br)
64 return rhashtable_init(&br->fdb_hash_tbl, &br_fdb_rht_params);
67 void br_fdb_hash_fini(struct net_bridge *br)
69 rhashtable_destroy(&br->fdb_hash_tbl);
72 /* if topology_changing then use forward_delay (default 15 sec)
73 * otherwise keep longer (default 5 minutes)
75 static inline unsigned long hold_time(const struct net_bridge *br)
77 return br->topology_change ? br->forward_delay : br->ageing_time;
80 static inline int has_expired(const struct net_bridge *br,
81 const struct net_bridge_fdb_entry *fdb)
83 return !fdb->is_static && !fdb->added_by_external_learn &&
84 time_before_eq(fdb->updated + hold_time(br), jiffies);
87 static void fdb_rcu_free(struct rcu_head *head)
89 struct net_bridge_fdb_entry *ent
90 = container_of(head, struct net_bridge_fdb_entry, rcu);
91 kmem_cache_free(br_fdb_cache, ent);
94 static struct net_bridge_fdb_entry *fdb_find_rcu(struct rhashtable *tbl,
95 const unsigned char *addr,
96 __u16 vid)
98 struct net_bridge_fdb_key key;
100 WARN_ON_ONCE(!rcu_read_lock_held());
102 key.vlan_id = vid;
103 memcpy(key.addr.addr, addr, sizeof(key.addr.addr));
105 return rhashtable_lookup(tbl, &key, br_fdb_rht_params);
108 /* requires bridge hash_lock */
109 static struct net_bridge_fdb_entry *br_fdb_find(struct net_bridge *br,
110 const unsigned char *addr,
111 __u16 vid)
113 struct net_bridge_fdb_entry *fdb;
115 lockdep_assert_held_once(&br->hash_lock);
117 rcu_read_lock();
118 fdb = fdb_find_rcu(&br->fdb_hash_tbl, addr, vid);
119 rcu_read_unlock();
121 return fdb;
124 struct net_device *br_fdb_find_port(const struct net_device *br_dev,
125 const unsigned char *addr,
126 __u16 vid)
128 struct net_bridge_fdb_entry *f;
129 struct net_device *dev = NULL;
130 struct net_bridge *br;
132 ASSERT_RTNL();
134 if (!netif_is_bridge_master(br_dev))
135 return NULL;
137 br = netdev_priv(br_dev);
138 rcu_read_lock();
139 f = br_fdb_find_rcu(br, addr, vid);
140 if (f && f->dst)
141 dev = f->dst->dev;
142 rcu_read_unlock();
144 return dev;
146 EXPORT_SYMBOL_GPL(br_fdb_find_port);
148 struct net_bridge_fdb_entry *br_fdb_find_rcu(struct net_bridge *br,
149 const unsigned char *addr,
150 __u16 vid)
152 return fdb_find_rcu(&br->fdb_hash_tbl, addr, vid);
155 /* When a static FDB entry is added, the mac address from the entry is
156 * added to the bridge private HW address list and all required ports
157 * are then updated with the new information.
158 * Called under RTNL.
160 static void fdb_add_hw_addr(struct net_bridge *br, const unsigned char *addr)
162 int err;
163 struct net_bridge_port *p;
165 ASSERT_RTNL();
167 list_for_each_entry(p, &br->port_list, list) {
168 if (!br_promisc_port(p)) {
169 err = dev_uc_add(p->dev, addr);
170 if (err)
171 goto undo;
175 return;
176 undo:
177 list_for_each_entry_continue_reverse(p, &br->port_list, list) {
178 if (!br_promisc_port(p))
179 dev_uc_del(p->dev, addr);
183 /* When a static FDB entry is deleted, the HW address from that entry is
184 * also removed from the bridge private HW address list and updates all
185 * the ports with needed information.
186 * Called under RTNL.
188 static void fdb_del_hw_addr(struct net_bridge *br, const unsigned char *addr)
190 struct net_bridge_port *p;
192 ASSERT_RTNL();
194 list_for_each_entry(p, &br->port_list, list) {
195 if (!br_promisc_port(p))
196 dev_uc_del(p->dev, addr);
200 static void fdb_delete(struct net_bridge *br, struct net_bridge_fdb_entry *f,
201 bool swdev_notify)
203 trace_fdb_delete(br, f);
205 if (f->is_static)
206 fdb_del_hw_addr(br, f->key.addr.addr);
208 hlist_del_init_rcu(&f->fdb_node);
209 rhashtable_remove_fast(&br->fdb_hash_tbl, &f->rhnode,
210 br_fdb_rht_params);
211 fdb_notify(br, f, RTM_DELNEIGH, swdev_notify);
212 call_rcu(&f->rcu, fdb_rcu_free);
215 /* Delete a local entry if no other port had the same address. */
216 static void fdb_delete_local(struct net_bridge *br,
217 const struct net_bridge_port *p,
218 struct net_bridge_fdb_entry *f)
220 const unsigned char *addr = f->key.addr.addr;
221 struct net_bridge_vlan_group *vg;
222 const struct net_bridge_vlan *v;
223 struct net_bridge_port *op;
224 u16 vid = f->key.vlan_id;
226 /* Maybe another port has same hw addr? */
227 list_for_each_entry(op, &br->port_list, list) {
228 vg = nbp_vlan_group(op);
229 if (op != p && ether_addr_equal(op->dev->dev_addr, addr) &&
230 (!vid || br_vlan_find(vg, vid))) {
231 f->dst = op;
232 f->added_by_user = 0;
233 return;
237 vg = br_vlan_group(br);
238 v = br_vlan_find(vg, vid);
239 /* Maybe bridge device has same hw addr? */
240 if (p && ether_addr_equal(br->dev->dev_addr, addr) &&
241 (!vid || (v && br_vlan_should_use(v)))) {
242 f->dst = NULL;
243 f->added_by_user = 0;
244 return;
247 fdb_delete(br, f, true);
250 void br_fdb_find_delete_local(struct net_bridge *br,
251 const struct net_bridge_port *p,
252 const unsigned char *addr, u16 vid)
254 struct net_bridge_fdb_entry *f;
256 spin_lock_bh(&br->hash_lock);
257 f = br_fdb_find(br, addr, vid);
258 if (f && f->is_local && !f->added_by_user && f->dst == p)
259 fdb_delete_local(br, p, f);
260 spin_unlock_bh(&br->hash_lock);
263 void br_fdb_changeaddr(struct net_bridge_port *p, const unsigned char *newaddr)
265 struct net_bridge_vlan_group *vg;
266 struct net_bridge_fdb_entry *f;
267 struct net_bridge *br = p->br;
268 struct net_bridge_vlan *v;
270 spin_lock_bh(&br->hash_lock);
271 vg = nbp_vlan_group(p);
272 hlist_for_each_entry(f, &br->fdb_list, fdb_node) {
273 if (f->dst == p && f->is_local && !f->added_by_user) {
274 /* delete old one */
275 fdb_delete_local(br, p, f);
277 /* if this port has no vlan information
278 * configured, we can safely be done at
279 * this point.
281 if (!vg || !vg->num_vlans)
282 goto insert;
286 insert:
287 /* insert new address, may fail if invalid address or dup. */
288 fdb_insert(br, p, newaddr, 0);
290 if (!vg || !vg->num_vlans)
291 goto done;
293 /* Now add entries for every VLAN configured on the port.
294 * This function runs under RTNL so the bitmap will not change
295 * from under us.
297 list_for_each_entry(v, &vg->vlan_list, vlist)
298 fdb_insert(br, p, newaddr, v->vid);
300 done:
301 spin_unlock_bh(&br->hash_lock);
304 void br_fdb_change_mac_address(struct net_bridge *br, const u8 *newaddr)
306 struct net_bridge_vlan_group *vg;
307 struct net_bridge_fdb_entry *f;
308 struct net_bridge_vlan *v;
310 spin_lock_bh(&br->hash_lock);
312 /* If old entry was unassociated with any port, then delete it. */
313 f = br_fdb_find(br, br->dev->dev_addr, 0);
314 if (f && f->is_local && !f->dst && !f->added_by_user)
315 fdb_delete_local(br, NULL, f);
317 fdb_insert(br, NULL, newaddr, 0);
318 vg = br_vlan_group(br);
319 if (!vg || !vg->num_vlans)
320 goto out;
321 /* Now remove and add entries for every VLAN configured on the
322 * bridge. This function runs under RTNL so the bitmap will not
323 * change from under us.
325 list_for_each_entry(v, &vg->vlan_list, vlist) {
326 if (!br_vlan_should_use(v))
327 continue;
328 f = br_fdb_find(br, br->dev->dev_addr, v->vid);
329 if (f && f->is_local && !f->dst && !f->added_by_user)
330 fdb_delete_local(br, NULL, f);
331 fdb_insert(br, NULL, newaddr, v->vid);
333 out:
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,
340 gc_work.work);
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
350 rcu_read_lock();
351 hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
352 unsigned long this_timer;
354 if (f->is_static || f->added_by_external_learn)
355 continue;
356 this_timer = f->updated + delay;
357 if (time_after(this_timer, now)) {
358 work_delay = min(work_delay, this_timer - now);
359 } else {
360 spin_lock_bh(&br->hash_lock);
361 if (!hlist_unhashed(&f->fdb_node))
362 fdb_delete(br, f, true);
363 spin_unlock_bh(&br->hash_lock);
366 rcu_read_unlock();
368 /* Cleanup minimum 10 milliseconds apart */
369 work_delay = max_t(unsigned long, work_delay, msecs_to_jiffies(10));
370 mod_delayed_work(system_long_wq, &br->gc_work, work_delay);
373 /* Completely flush all dynamic entries in forwarding database.*/
374 void br_fdb_flush(struct net_bridge *br)
376 struct net_bridge_fdb_entry *f;
377 struct hlist_node *tmp;
379 spin_lock_bh(&br->hash_lock);
380 hlist_for_each_entry_safe(f, tmp, &br->fdb_list, fdb_node) {
381 if (!f->is_static)
382 fdb_delete(br, f, true);
384 spin_unlock_bh(&br->hash_lock);
387 /* Flush all entries referring to a specific port.
388 * if do_all is set also flush static entries
389 * if vid is set delete all entries that match the vlan_id
391 void br_fdb_delete_by_port(struct net_bridge *br,
392 const struct net_bridge_port *p,
393 u16 vid,
394 int do_all)
396 struct net_bridge_fdb_entry *f;
397 struct hlist_node *tmp;
399 spin_lock_bh(&br->hash_lock);
400 hlist_for_each_entry_safe(f, tmp, &br->fdb_list, fdb_node) {
401 if (f->dst != p)
402 continue;
404 if (!do_all)
405 if (f->is_static || (vid && f->key.vlan_id != vid))
406 continue;
408 if (f->is_local)
409 fdb_delete_local(br, p, f);
410 else
411 fdb_delete(br, f, true);
413 spin_unlock_bh(&br->hash_lock);
416 #if IS_ENABLED(CONFIG_ATM_LANE)
417 /* Interface used by ATM LANE hook to test
418 * if an addr is on some other bridge port */
419 int br_fdb_test_addr(struct net_device *dev, unsigned char *addr)
421 struct net_bridge_fdb_entry *fdb;
422 struct net_bridge_port *port;
423 int ret;
425 rcu_read_lock();
426 port = br_port_get_rcu(dev);
427 if (!port)
428 ret = 0;
429 else {
430 fdb = br_fdb_find_rcu(port->br, addr, 0);
431 ret = fdb && fdb->dst && fdb->dst->dev != dev &&
432 fdb->dst->state == BR_STATE_FORWARDING;
434 rcu_read_unlock();
436 return ret;
438 #endif /* CONFIG_ATM_LANE */
441 * Fill buffer with forwarding table records in
442 * the API format.
444 int br_fdb_fillbuf(struct net_bridge *br, void *buf,
445 unsigned long maxnum, unsigned long skip)
447 struct net_bridge_fdb_entry *f;
448 struct __fdb_entry *fe = buf;
449 int num = 0;
451 memset(buf, 0, maxnum*sizeof(struct __fdb_entry));
453 rcu_read_lock();
454 hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
455 if (num >= maxnum)
456 break;
458 if (has_expired(br, f))
459 continue;
461 /* ignore pseudo entry for local MAC address */
462 if (!f->dst)
463 continue;
465 if (skip) {
466 --skip;
467 continue;
470 /* convert from internal format to API */
471 memcpy(fe->mac_addr, f->key.addr.addr, ETH_ALEN);
473 /* due to ABI compat need to split into hi/lo */
474 fe->port_no = f->dst->port_no;
475 fe->port_hi = f->dst->port_no >> 8;
477 fe->is_local = f->is_local;
478 if (!f->is_static)
479 fe->ageing_timer_value = jiffies_delta_to_clock_t(jiffies - f->updated);
480 ++fe;
481 ++num;
483 rcu_read_unlock();
485 return num;
488 static struct net_bridge_fdb_entry *fdb_create(struct net_bridge *br,
489 struct net_bridge_port *source,
490 const unsigned char *addr,
491 __u16 vid,
492 unsigned char is_local,
493 unsigned char is_static)
495 struct net_bridge_fdb_entry *fdb;
497 fdb = kmem_cache_alloc(br_fdb_cache, GFP_ATOMIC);
498 if (fdb) {
499 memcpy(fdb->key.addr.addr, addr, ETH_ALEN);
500 fdb->dst = source;
501 fdb->key.vlan_id = vid;
502 fdb->is_local = is_local;
503 fdb->is_static = is_static;
504 fdb->added_by_user = 0;
505 fdb->added_by_external_learn = 0;
506 fdb->offloaded = 0;
507 fdb->updated = fdb->used = jiffies;
508 if (rhashtable_lookup_insert_fast(&br->fdb_hash_tbl,
509 &fdb->rhnode,
510 br_fdb_rht_params)) {
511 kmem_cache_free(br_fdb_cache, fdb);
512 fdb = NULL;
513 } else {
514 hlist_add_head_rcu(&fdb->fdb_node, &br->fdb_list);
517 return fdb;
520 static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
521 const unsigned char *addr, u16 vid)
523 struct net_bridge_fdb_entry *fdb;
525 if (!is_valid_ether_addr(addr))
526 return -EINVAL;
528 fdb = br_fdb_find(br, addr, vid);
529 if (fdb) {
530 /* it is okay to have multiple ports with same
531 * address, just use the first one.
533 if (fdb->is_local)
534 return 0;
535 br_warn(br, "adding interface %s with same address as a received packet (addr:%pM, vlan:%u)\n",
536 source ? source->dev->name : br->dev->name, addr, vid);
537 fdb_delete(br, fdb, true);
540 fdb = fdb_create(br, source, addr, vid, 1, 1);
541 if (!fdb)
542 return -ENOMEM;
544 fdb_add_hw_addr(br, addr);
545 fdb_notify(br, fdb, RTM_NEWNEIGH, true);
546 return 0;
549 /* Add entry for local address of interface */
550 int br_fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
551 const unsigned char *addr, u16 vid)
553 int ret;
555 spin_lock_bh(&br->hash_lock);
556 ret = fdb_insert(br, source, addr, vid);
557 spin_unlock_bh(&br->hash_lock);
558 return ret;
561 void br_fdb_update(struct net_bridge *br, struct net_bridge_port *source,
562 const unsigned char *addr, u16 vid, bool added_by_user)
564 struct net_bridge_fdb_entry *fdb;
565 bool fdb_modified = false;
567 /* some users want to always flood. */
568 if (hold_time(br) == 0)
569 return;
571 /* ignore packets unless we are using this port */
572 if (!(source->state == BR_STATE_LEARNING ||
573 source->state == BR_STATE_FORWARDING))
574 return;
576 fdb = fdb_find_rcu(&br->fdb_hash_tbl, addr, vid);
577 if (likely(fdb)) {
578 /* attempt to update an entry for a local interface */
579 if (unlikely(fdb->is_local)) {
580 if (net_ratelimit())
581 br_warn(br, "received packet on %s with own address as source address (addr:%pM, vlan:%u)\n",
582 source->dev->name, addr, vid);
583 } else {
584 unsigned long now = jiffies;
586 /* fastpath: update of existing entry */
587 if (unlikely(source != fdb->dst)) {
588 fdb->dst = source;
589 fdb_modified = true;
590 /* Take over HW learned entry */
591 if (unlikely(fdb->added_by_external_learn))
592 fdb->added_by_external_learn = 0;
594 if (now != fdb->updated)
595 fdb->updated = now;
596 if (unlikely(added_by_user))
597 fdb->added_by_user = 1;
598 if (unlikely(fdb_modified)) {
599 trace_br_fdb_update(br, source, addr, vid, added_by_user);
600 fdb_notify(br, fdb, RTM_NEWNEIGH, true);
603 } else {
604 spin_lock(&br->hash_lock);
605 fdb = fdb_create(br, source, addr, vid, 0, 0);
606 if (fdb) {
607 if (unlikely(added_by_user))
608 fdb->added_by_user = 1;
609 trace_br_fdb_update(br, source, addr, vid,
610 added_by_user);
611 fdb_notify(br, fdb, RTM_NEWNEIGH, true);
613 /* else we lose race and someone else inserts
614 * it first, don't bother updating
616 spin_unlock(&br->hash_lock);
620 static int fdb_to_nud(const struct net_bridge *br,
621 const struct net_bridge_fdb_entry *fdb)
623 if (fdb->is_local)
624 return NUD_PERMANENT;
625 else if (fdb->is_static)
626 return NUD_NOARP;
627 else if (has_expired(br, fdb))
628 return NUD_STALE;
629 else
630 return NUD_REACHABLE;
633 static int fdb_fill_info(struct sk_buff *skb, const struct net_bridge *br,
634 const struct net_bridge_fdb_entry *fdb,
635 u32 portid, u32 seq, int type, unsigned int flags)
637 unsigned long now = jiffies;
638 struct nda_cacheinfo ci;
639 struct nlmsghdr *nlh;
640 struct ndmsg *ndm;
642 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
643 if (nlh == NULL)
644 return -EMSGSIZE;
646 ndm = nlmsg_data(nlh);
647 ndm->ndm_family = AF_BRIDGE;
648 ndm->ndm_pad1 = 0;
649 ndm->ndm_pad2 = 0;
650 ndm->ndm_flags = 0;
651 ndm->ndm_type = 0;
652 ndm->ndm_ifindex = fdb->dst ? fdb->dst->dev->ifindex : br->dev->ifindex;
653 ndm->ndm_state = fdb_to_nud(br, fdb);
655 if (fdb->offloaded)
656 ndm->ndm_flags |= NTF_OFFLOADED;
657 if (fdb->added_by_external_learn)
658 ndm->ndm_flags |= NTF_EXT_LEARNED;
660 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->key.addr))
661 goto nla_put_failure;
662 if (nla_put_u32(skb, NDA_MASTER, br->dev->ifindex))
663 goto nla_put_failure;
664 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
665 ci.ndm_confirmed = 0;
666 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
667 ci.ndm_refcnt = 0;
668 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
669 goto nla_put_failure;
671 if (fdb->key.vlan_id && nla_put(skb, NDA_VLAN, sizeof(u16),
672 &fdb->key.vlan_id))
673 goto nla_put_failure;
675 nlmsg_end(skb, nlh);
676 return 0;
678 nla_put_failure:
679 nlmsg_cancel(skb, nlh);
680 return -EMSGSIZE;
683 static inline size_t fdb_nlmsg_size(void)
685 return NLMSG_ALIGN(sizeof(struct ndmsg))
686 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
687 + nla_total_size(sizeof(u32)) /* NDA_MASTER */
688 + nla_total_size(sizeof(u16)) /* NDA_VLAN */
689 + nla_total_size(sizeof(struct nda_cacheinfo));
692 static void fdb_notify(struct net_bridge *br,
693 const struct net_bridge_fdb_entry *fdb, int type,
694 bool swdev_notify)
696 struct net *net = dev_net(br->dev);
697 struct sk_buff *skb;
698 int err = -ENOBUFS;
700 if (swdev_notify)
701 br_switchdev_fdb_notify(fdb, type);
703 skb = nlmsg_new(fdb_nlmsg_size(), GFP_ATOMIC);
704 if (skb == NULL)
705 goto errout;
707 err = fdb_fill_info(skb, br, fdb, 0, 0, type, 0);
708 if (err < 0) {
709 /* -EMSGSIZE implies BUG in fdb_nlmsg_size() */
710 WARN_ON(err == -EMSGSIZE);
711 kfree_skb(skb);
712 goto errout;
714 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
715 return;
716 errout:
717 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
720 /* Dump information about entries, in response to GETNEIGH */
721 int br_fdb_dump(struct sk_buff *skb,
722 struct netlink_callback *cb,
723 struct net_device *dev,
724 struct net_device *filter_dev,
725 int *idx)
727 struct net_bridge *br = netdev_priv(dev);
728 struct net_bridge_fdb_entry *f;
729 int err = 0;
731 if (!(dev->priv_flags & IFF_EBRIDGE))
732 return err;
734 if (!filter_dev) {
735 err = ndo_dflt_fdb_dump(skb, cb, dev, NULL, idx);
736 if (err < 0)
737 return err;
740 rcu_read_lock();
741 hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
742 if (*idx < cb->args[2])
743 goto skip;
744 if (filter_dev && (!f->dst || f->dst->dev != filter_dev)) {
745 if (filter_dev != dev)
746 goto skip;
747 /* !f->dst is a special case for bridge
748 * It means the MAC belongs to the bridge
749 * Therefore need a little more filtering
750 * we only want to dump the !f->dst case
752 if (f->dst)
753 goto skip;
755 if (!filter_dev && f->dst)
756 goto skip;
758 err = fdb_fill_info(skb, br, f,
759 NETLINK_CB(cb->skb).portid,
760 cb->nlh->nlmsg_seq,
761 RTM_NEWNEIGH,
762 NLM_F_MULTI);
763 if (err < 0)
764 break;
765 skip:
766 *idx += 1;
768 rcu_read_unlock();
770 return err;
773 /* Update (create or replace) forwarding database entry */
774 static int fdb_add_entry(struct net_bridge *br, struct net_bridge_port *source,
775 const __u8 *addr, __u16 state, __u16 flags, __u16 vid)
777 struct net_bridge_fdb_entry *fdb;
778 bool modified = false;
780 /* If the port cannot learn allow only local and static entries */
781 if (source && !(state & NUD_PERMANENT) && !(state & NUD_NOARP) &&
782 !(source->state == BR_STATE_LEARNING ||
783 source->state == BR_STATE_FORWARDING))
784 return -EPERM;
786 if (!source && !(state & NUD_PERMANENT)) {
787 pr_info("bridge: RTM_NEWNEIGH %s without NUD_PERMANENT\n",
788 br->dev->name);
789 return -EINVAL;
792 fdb = br_fdb_find(br, addr, vid);
793 if (fdb == NULL) {
794 if (!(flags & NLM_F_CREATE))
795 return -ENOENT;
797 fdb = fdb_create(br, source, addr, vid, 0, 0);
798 if (!fdb)
799 return -ENOMEM;
801 modified = true;
802 } else {
803 if (flags & NLM_F_EXCL)
804 return -EEXIST;
806 if (fdb->dst != source) {
807 fdb->dst = source;
808 modified = true;
812 if (fdb_to_nud(br, fdb) != state) {
813 if (state & NUD_PERMANENT) {
814 fdb->is_local = 1;
815 if (!fdb->is_static) {
816 fdb->is_static = 1;
817 fdb_add_hw_addr(br, addr);
819 } else if (state & NUD_NOARP) {
820 fdb->is_local = 0;
821 if (!fdb->is_static) {
822 fdb->is_static = 1;
823 fdb_add_hw_addr(br, addr);
825 } else {
826 fdb->is_local = 0;
827 if (fdb->is_static) {
828 fdb->is_static = 0;
829 fdb_del_hw_addr(br, addr);
833 modified = true;
835 fdb->added_by_user = 1;
837 fdb->used = jiffies;
838 if (modified) {
839 fdb->updated = jiffies;
840 fdb_notify(br, fdb, RTM_NEWNEIGH, true);
843 return 0;
846 static int __br_fdb_add(struct ndmsg *ndm, struct net_bridge *br,
847 struct net_bridge_port *p, const unsigned char *addr,
848 u16 nlh_flags, u16 vid)
850 int err = 0;
852 if (ndm->ndm_flags & NTF_USE) {
853 if (!p) {
854 pr_info("bridge: RTM_NEWNEIGH %s with NTF_USE is not supported\n",
855 br->dev->name);
856 return -EINVAL;
858 local_bh_disable();
859 rcu_read_lock();
860 br_fdb_update(br, p, addr, vid, true);
861 rcu_read_unlock();
862 local_bh_enable();
863 } else if (ndm->ndm_flags & NTF_EXT_LEARNED) {
864 err = br_fdb_external_learn_add(br, p, addr, vid, true);
865 } else {
866 spin_lock_bh(&br->hash_lock);
867 err = fdb_add_entry(br, p, addr, ndm->ndm_state,
868 nlh_flags, vid);
869 spin_unlock_bh(&br->hash_lock);
872 return err;
875 /* Add new permanent fdb entry with RTM_NEWNEIGH */
876 int br_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
877 struct net_device *dev,
878 const unsigned char *addr, u16 vid, u16 nlh_flags)
880 struct net_bridge_vlan_group *vg;
881 struct net_bridge_port *p = NULL;
882 struct net_bridge_vlan *v;
883 struct net_bridge *br = NULL;
884 int err = 0;
886 trace_br_fdb_add(ndm, dev, addr, vid, nlh_flags);
888 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE))) {
889 pr_info("bridge: RTM_NEWNEIGH with invalid state %#x\n", ndm->ndm_state);
890 return -EINVAL;
893 if (is_zero_ether_addr(addr)) {
894 pr_info("bridge: RTM_NEWNEIGH with invalid ether address\n");
895 return -EINVAL;
898 if (dev->priv_flags & IFF_EBRIDGE) {
899 br = netdev_priv(dev);
900 vg = br_vlan_group(br);
901 } else {
902 p = br_port_get_rtnl(dev);
903 if (!p) {
904 pr_info("bridge: RTM_NEWNEIGH %s not a bridge port\n",
905 dev->name);
906 return -EINVAL;
908 br = p->br;
909 vg = nbp_vlan_group(p);
912 if (vid) {
913 v = br_vlan_find(vg, vid);
914 if (!v || !br_vlan_should_use(v)) {
915 pr_info("bridge: RTM_NEWNEIGH with unconfigured vlan %d on %s\n", vid, dev->name);
916 return -EINVAL;
919 /* VID was specified, so use it. */
920 err = __br_fdb_add(ndm, br, p, addr, nlh_flags, vid);
921 } else {
922 err = __br_fdb_add(ndm, br, p, addr, nlh_flags, 0);
923 if (err || !vg || !vg->num_vlans)
924 goto out;
926 /* We have vlans configured on this port and user didn't
927 * specify a VLAN. To be nice, add/update entry for every
928 * vlan on this port.
930 list_for_each_entry(v, &vg->vlan_list, vlist) {
931 if (!br_vlan_should_use(v))
932 continue;
933 err = __br_fdb_add(ndm, br, p, addr, nlh_flags, v->vid);
934 if (err)
935 goto out;
939 out:
940 return err;
943 static int fdb_delete_by_addr_and_port(struct net_bridge *br,
944 const struct net_bridge_port *p,
945 const u8 *addr, u16 vlan)
947 struct net_bridge_fdb_entry *fdb;
949 fdb = br_fdb_find(br, addr, vlan);
950 if (!fdb || fdb->dst != p)
951 return -ENOENT;
953 fdb_delete(br, fdb, true);
955 return 0;
958 static int __br_fdb_delete(struct net_bridge *br,
959 const struct net_bridge_port *p,
960 const unsigned char *addr, u16 vid)
962 int err;
964 spin_lock_bh(&br->hash_lock);
965 err = fdb_delete_by_addr_and_port(br, p, addr, vid);
966 spin_unlock_bh(&br->hash_lock);
968 return err;
971 /* Remove neighbor entry with RTM_DELNEIGH */
972 int br_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
973 struct net_device *dev,
974 const unsigned char *addr, u16 vid)
976 struct net_bridge_vlan_group *vg;
977 struct net_bridge_port *p = NULL;
978 struct net_bridge_vlan *v;
979 struct net_bridge *br;
980 int err;
982 if (dev->priv_flags & IFF_EBRIDGE) {
983 br = netdev_priv(dev);
984 vg = br_vlan_group(br);
985 } else {
986 p = br_port_get_rtnl(dev);
987 if (!p) {
988 pr_info("bridge: RTM_DELNEIGH %s not a bridge port\n",
989 dev->name);
990 return -EINVAL;
992 vg = nbp_vlan_group(p);
993 br = p->br;
996 if (vid) {
997 v = br_vlan_find(vg, vid);
998 if (!v) {
999 pr_info("bridge: RTM_DELNEIGH with unconfigured vlan %d on %s\n", vid, dev->name);
1000 return -EINVAL;
1003 err = __br_fdb_delete(br, p, addr, vid);
1004 } else {
1005 err = -ENOENT;
1006 err &= __br_fdb_delete(br, p, addr, 0);
1007 if (!vg || !vg->num_vlans)
1008 return err;
1010 list_for_each_entry(v, &vg->vlan_list, vlist) {
1011 if (!br_vlan_should_use(v))
1012 continue;
1013 err &= __br_fdb_delete(br, p, addr, v->vid);
1017 return err;
1020 int br_fdb_sync_static(struct net_bridge *br, struct net_bridge_port *p)
1022 struct net_bridge_fdb_entry *f, *tmp;
1023 int err = 0;
1025 ASSERT_RTNL();
1027 /* the key here is that static entries change only under rtnl */
1028 rcu_read_lock();
1029 hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
1030 /* We only care for static entries */
1031 if (!f->is_static)
1032 continue;
1033 err = dev_uc_add(p->dev, f->key.addr.addr);
1034 if (err)
1035 goto rollback;
1037 done:
1038 rcu_read_unlock();
1040 return err;
1042 rollback:
1043 hlist_for_each_entry_rcu(tmp, &br->fdb_list, fdb_node) {
1044 /* We only care for static entries */
1045 if (!tmp->is_static)
1046 continue;
1047 if (tmp == f)
1048 break;
1049 dev_uc_del(p->dev, tmp->key.addr.addr);
1052 goto done;
1055 void br_fdb_unsync_static(struct net_bridge *br, struct net_bridge_port *p)
1057 struct net_bridge_fdb_entry *f;
1059 ASSERT_RTNL();
1061 rcu_read_lock();
1062 hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
1063 /* We only care for static entries */
1064 if (!f->is_static)
1065 continue;
1067 dev_uc_del(p->dev, f->key.addr.addr);
1069 rcu_read_unlock();
1072 int br_fdb_external_learn_add(struct net_bridge *br, struct net_bridge_port *p,
1073 const unsigned char *addr, u16 vid,
1074 bool swdev_notify)
1076 struct net_bridge_fdb_entry *fdb;
1077 bool modified = false;
1078 int err = 0;
1080 trace_br_fdb_external_learn_add(br, p, addr, vid);
1082 spin_lock_bh(&br->hash_lock);
1084 fdb = br_fdb_find(br, addr, vid);
1085 if (!fdb) {
1086 fdb = fdb_create(br, p, addr, vid, 0, 0);
1087 if (!fdb) {
1088 err = -ENOMEM;
1089 goto err_unlock;
1091 fdb->added_by_external_learn = 1;
1092 fdb_notify(br, fdb, RTM_NEWNEIGH, swdev_notify);
1093 } else {
1094 fdb->updated = jiffies;
1096 if (fdb->dst != p) {
1097 fdb->dst = p;
1098 modified = true;
1101 if (fdb->added_by_external_learn) {
1102 /* Refresh entry */
1103 fdb->used = jiffies;
1104 } else if (!fdb->added_by_user) {
1105 /* Take over SW learned entry */
1106 fdb->added_by_external_learn = 1;
1107 modified = true;
1110 if (modified)
1111 fdb_notify(br, fdb, RTM_NEWNEIGH, swdev_notify);
1114 err_unlock:
1115 spin_unlock_bh(&br->hash_lock);
1117 return err;
1120 int br_fdb_external_learn_del(struct net_bridge *br, struct net_bridge_port *p,
1121 const unsigned char *addr, u16 vid,
1122 bool swdev_notify)
1124 struct net_bridge_fdb_entry *fdb;
1125 int err = 0;
1127 spin_lock_bh(&br->hash_lock);
1129 fdb = br_fdb_find(br, addr, vid);
1130 if (fdb && fdb->added_by_external_learn)
1131 fdb_delete(br, fdb, swdev_notify);
1132 else
1133 err = -ENOENT;
1135 spin_unlock_bh(&br->hash_lock);
1137 return err;
1140 void br_fdb_offloaded_set(struct net_bridge *br, struct net_bridge_port *p,
1141 const unsigned char *addr, u16 vid)
1143 struct net_bridge_fdb_entry *fdb;
1145 spin_lock_bh(&br->hash_lock);
1147 fdb = br_fdb_find(br, addr, vid);
1148 if (fdb)
1149 fdb->offloaded = 1;
1151 spin_unlock_bh(&br->hash_lock);