3 * Linux ethernet bridge
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 "br_private.h"
29 static struct kmem_cache *br_fdb_cache __read_mostly;
30 static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
31 const unsigned char *addr, u16 vid);
32 static void fdb_notify(struct net_bridge *br,
33 const struct net_bridge_fdb_entry *, int);
35 static u32 fdb_salt __read_mostly;
37 int __init br_fdb_init(void)
39 br_fdb_cache = kmem_cache_create("bridge_fdb_cache",
40 sizeof(struct net_bridge_fdb_entry),
42 SLAB_HWCACHE_ALIGN, NULL);
46 get_random_bytes(&fdb_salt, sizeof(fdb_salt));
50 void br_fdb_fini(void)
52 kmem_cache_destroy(br_fdb_cache);
56 /* if topology_changing then use forward_delay (default 15 sec)
57 * otherwise keep longer (default 5 minutes)
59 static inline unsigned long hold_time(const struct net_bridge *br)
61 return br->topology_change ? br->forward_delay : br->ageing_time;
64 static inline int has_expired(const struct net_bridge *br,
65 const struct net_bridge_fdb_entry *fdb)
67 return !fdb->is_static &&
68 time_before_eq(fdb->updated + hold_time(br), jiffies);
71 static inline int br_mac_hash(const unsigned char *mac, __u16 vid)
73 /* use 1 byte of OUI and 3 bytes of NIC */
74 u32 key = get_unaligned((u32 *)(mac + 2));
75 return jhash_2words(key, vid, fdb_salt) & (BR_HASH_SIZE - 1);
78 static void fdb_rcu_free(struct rcu_head *head)
80 struct net_bridge_fdb_entry *ent
81 = container_of(head, struct net_bridge_fdb_entry, rcu);
82 kmem_cache_free(br_fdb_cache, ent);
85 static void fdb_delete(struct net_bridge *br, struct net_bridge_fdb_entry *f)
87 hlist_del_rcu(&f->hlist);
88 fdb_notify(br, f, RTM_DELNEIGH);
89 call_rcu(&f->rcu, fdb_rcu_free);
92 void br_fdb_changeaddr(struct net_bridge_port *p, const unsigned char *newaddr)
94 struct net_bridge *br = p->br;
95 struct net_port_vlans *pv = nbp_get_vlan_info(p);
100 spin_lock_bh(&br->hash_lock);
102 /* Search all chains since old address/hash is unknown */
103 for (i = 0; i < BR_HASH_SIZE; i++) {
104 struct hlist_node *h;
105 hlist_for_each(h, &br->hash[i]) {
106 struct net_bridge_fdb_entry *f;
108 f = hlist_entry(h, struct net_bridge_fdb_entry, hlist);
109 if (f->dst == p && f->is_local && !f->added_by_user) {
110 /* maybe another port has same hw addr? */
111 struct net_bridge_port *op;
112 u16 vid = f->vlan_id;
113 list_for_each_entry(op, &br->port_list, list) {
115 ether_addr_equal(op->dev->dev_addr,
117 nbp_vlan_find(op, vid)) {
126 /* if this port has no vlan information
127 * configured, we can safely be done at
137 /* insert new address, may fail if invalid address or dup. */
138 fdb_insert(br, p, newaddr, 0);
143 /* Now add entries for every VLAN configured on the port.
144 * This function runs under RTNL so the bitmap will not change
147 for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID)
148 fdb_insert(br, p, newaddr, vid);
151 spin_unlock_bh(&br->hash_lock);
154 void br_fdb_change_mac_address(struct net_bridge *br, const u8 *newaddr)
156 struct net_bridge_fdb_entry *f;
157 struct net_port_vlans *pv;
160 /* If old entry was unassociated with any port, then delete it. */
161 f = __br_fdb_get(br, br->dev->dev_addr, 0);
162 if (f && f->is_local && !f->dst)
165 fdb_insert(br, NULL, newaddr, 0);
167 /* Now remove and add entries for every VLAN configured on the
168 * bridge. This function runs under RTNL so the bitmap will not
169 * change from under us.
171 pv = br_get_vlan_info(br);
175 for_each_set_bit_from(vid, pv->vlan_bitmap, VLAN_N_VID) {
176 f = __br_fdb_get(br, br->dev->dev_addr, vid);
177 if (f && f->is_local && !f->dst)
179 fdb_insert(br, NULL, newaddr, vid);
183 void br_fdb_cleanup(unsigned long _data)
185 struct net_bridge *br = (struct net_bridge *)_data;
186 unsigned long delay = hold_time(br);
187 unsigned long next_timer = jiffies + br->ageing_time;
190 spin_lock(&br->hash_lock);
191 for (i = 0; i < BR_HASH_SIZE; i++) {
192 struct net_bridge_fdb_entry *f;
193 struct hlist_node *n;
195 hlist_for_each_entry_safe(f, n, &br->hash[i], hlist) {
196 unsigned long this_timer;
199 this_timer = f->updated + delay;
200 if (time_before_eq(this_timer, jiffies))
202 else if (time_before(this_timer, next_timer))
203 next_timer = this_timer;
206 spin_unlock(&br->hash_lock);
208 mod_timer(&br->gc_timer, round_jiffies_up(next_timer));
211 /* Completely flush all dynamic entries in forwarding database.*/
212 void br_fdb_flush(struct net_bridge *br)
216 spin_lock_bh(&br->hash_lock);
217 for (i = 0; i < BR_HASH_SIZE; i++) {
218 struct net_bridge_fdb_entry *f;
219 struct hlist_node *n;
220 hlist_for_each_entry_safe(f, n, &br->hash[i], hlist) {
225 spin_unlock_bh(&br->hash_lock);
228 /* Flush all entries referring to a specific port.
229 * if do_all is set also flush static entries
231 void br_fdb_delete_by_port(struct net_bridge *br,
232 const struct net_bridge_port *p,
237 spin_lock_bh(&br->hash_lock);
238 for (i = 0; i < BR_HASH_SIZE; i++) {
239 struct hlist_node *h, *g;
241 hlist_for_each_safe(h, g, &br->hash[i]) {
242 struct net_bridge_fdb_entry *f
243 = hlist_entry(h, struct net_bridge_fdb_entry, hlist);
247 if (f->is_static && !do_all)
250 * if multiple ports all have the same device address
251 * then when one port is deleted, assign
252 * the local entry to other port
255 struct net_bridge_port *op;
256 list_for_each_entry(op, &br->port_list, list) {
258 ether_addr_equal(op->dev->dev_addr,
261 f->added_by_user = 0;
271 spin_unlock_bh(&br->hash_lock);
274 /* No locking or refcounting, assumes caller has rcu_read_lock */
275 struct net_bridge_fdb_entry *__br_fdb_get(struct net_bridge *br,
276 const unsigned char *addr,
279 struct net_bridge_fdb_entry *fdb;
281 hlist_for_each_entry_rcu(fdb,
282 &br->hash[br_mac_hash(addr, vid)], hlist) {
283 if (ether_addr_equal(fdb->addr.addr, addr) &&
284 fdb->vlan_id == vid) {
285 if (unlikely(has_expired(br, fdb)))
294 #if IS_ENABLED(CONFIG_ATM_LANE)
295 /* Interface used by ATM LANE hook to test
296 * if an addr is on some other bridge port */
297 int br_fdb_test_addr(struct net_device *dev, unsigned char *addr)
299 struct net_bridge_fdb_entry *fdb;
300 struct net_bridge_port *port;
304 port = br_port_get_rcu(dev);
308 fdb = __br_fdb_get(port->br, addr, 0);
309 ret = fdb && fdb->dst && fdb->dst->dev != dev &&
310 fdb->dst->state == BR_STATE_FORWARDING;
316 #endif /* CONFIG_ATM_LANE */
319 * Fill buffer with forwarding table records in
322 int br_fdb_fillbuf(struct net_bridge *br, void *buf,
323 unsigned long maxnum, unsigned long skip)
325 struct __fdb_entry *fe = buf;
327 struct net_bridge_fdb_entry *f;
329 memset(buf, 0, maxnum*sizeof(struct __fdb_entry));
332 for (i = 0; i < BR_HASH_SIZE; i++) {
333 hlist_for_each_entry_rcu(f, &br->hash[i], hlist) {
337 if (has_expired(br, f))
340 /* ignore pseudo entry for local MAC address */
349 /* convert from internal format to API */
350 memcpy(fe->mac_addr, f->addr.addr, ETH_ALEN);
352 /* due to ABI compat need to split into hi/lo */
353 fe->port_no = f->dst->port_no;
354 fe->port_hi = f->dst->port_no >> 8;
356 fe->is_local = f->is_local;
358 fe->ageing_timer_value = jiffies_delta_to_clock_t(jiffies - f->updated);
370 static struct net_bridge_fdb_entry *fdb_find(struct hlist_head *head,
371 const unsigned char *addr,
374 struct net_bridge_fdb_entry *fdb;
376 hlist_for_each_entry(fdb, head, hlist) {
377 if (ether_addr_equal(fdb->addr.addr, addr) &&
384 static struct net_bridge_fdb_entry *fdb_find_rcu(struct hlist_head *head,
385 const unsigned char *addr,
388 struct net_bridge_fdb_entry *fdb;
390 hlist_for_each_entry_rcu(fdb, head, hlist) {
391 if (ether_addr_equal(fdb->addr.addr, addr) &&
398 static struct net_bridge_fdb_entry *fdb_create(struct hlist_head *head,
399 struct net_bridge_port *source,
400 const unsigned char *addr,
403 struct net_bridge_fdb_entry *fdb;
405 fdb = kmem_cache_alloc(br_fdb_cache, GFP_ATOMIC);
407 memcpy(fdb->addr.addr, addr, ETH_ALEN);
412 fdb->added_by_user = 0;
413 fdb->updated = fdb->used = jiffies;
414 hlist_add_head_rcu(&fdb->hlist, head);
419 static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
420 const unsigned char *addr, u16 vid)
422 struct hlist_head *head = &br->hash[br_mac_hash(addr, vid)];
423 struct net_bridge_fdb_entry *fdb;
425 if (!is_valid_ether_addr(addr))
428 fdb = fdb_find(head, addr, vid);
430 /* it is okay to have multiple ports with same
431 * address, just use the first one.
435 br_warn(br, "adding interface %s with same address "
436 "as a received packet\n",
437 source ? source->dev->name : br->dev->name);
441 fdb = fdb_create(head, source, addr, vid);
445 fdb->is_local = fdb->is_static = 1;
446 fdb_notify(br, fdb, RTM_NEWNEIGH);
450 /* Add entry for local address of interface */
451 int br_fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
452 const unsigned char *addr, u16 vid)
456 spin_lock_bh(&br->hash_lock);
457 ret = fdb_insert(br, source, addr, vid);
458 spin_unlock_bh(&br->hash_lock);
462 void br_fdb_update(struct net_bridge *br, struct net_bridge_port *source,
463 const unsigned char *addr, u16 vid, bool added_by_user)
465 struct hlist_head *head = &br->hash[br_mac_hash(addr, vid)];
466 struct net_bridge_fdb_entry *fdb;
468 /* some users want to always flood. */
469 if (hold_time(br) == 0)
472 /* ignore packets unless we are using this port */
473 if (!(source->state == BR_STATE_LEARNING ||
474 source->state == BR_STATE_FORWARDING))
477 fdb = fdb_find_rcu(head, addr, vid);
479 /* attempt to update an entry for a local interface */
480 if (unlikely(fdb->is_local)) {
482 br_warn(br, "received packet on %s with "
483 "own address as source address\n",
486 /* fastpath: update of existing entry */
488 fdb->updated = jiffies;
489 if (unlikely(added_by_user))
490 fdb->added_by_user = 1;
493 spin_lock(&br->hash_lock);
494 if (likely(!fdb_find(head, addr, vid))) {
495 fdb = fdb_create(head, source, addr, vid);
497 if (unlikely(added_by_user))
498 fdb->added_by_user = 1;
499 fdb_notify(br, fdb, RTM_NEWNEIGH);
502 /* else we lose race and someone else inserts
503 * it first, don't bother updating
505 spin_unlock(&br->hash_lock);
509 static int fdb_to_nud(const struct net_bridge_fdb_entry *fdb)
512 return NUD_PERMANENT;
513 else if (fdb->is_static)
515 else if (has_expired(fdb->dst->br, fdb))
518 return NUD_REACHABLE;
521 static int fdb_fill_info(struct sk_buff *skb, const struct net_bridge *br,
522 const struct net_bridge_fdb_entry *fdb,
523 u32 portid, u32 seq, int type, unsigned int flags)
525 unsigned long now = jiffies;
526 struct nda_cacheinfo ci;
527 struct nlmsghdr *nlh;
530 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
534 ndm = nlmsg_data(nlh);
535 ndm->ndm_family = AF_BRIDGE;
540 ndm->ndm_ifindex = fdb->dst ? fdb->dst->dev->ifindex : br->dev->ifindex;
541 ndm->ndm_state = fdb_to_nud(fdb);
543 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->addr))
544 goto nla_put_failure;
545 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
546 ci.ndm_confirmed = 0;
547 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
549 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
550 goto nla_put_failure;
552 if (nla_put(skb, NDA_VLAN, sizeof(u16), &fdb->vlan_id))
553 goto nla_put_failure;
555 return nlmsg_end(skb, nlh);
558 nlmsg_cancel(skb, nlh);
562 static inline size_t fdb_nlmsg_size(void)
564 return NLMSG_ALIGN(sizeof(struct ndmsg))
565 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
566 + nla_total_size(sizeof(u16)) /* NDA_VLAN */
567 + nla_total_size(sizeof(struct nda_cacheinfo));
570 static void fdb_notify(struct net_bridge *br,
571 const struct net_bridge_fdb_entry *fdb, int type)
573 struct net *net = dev_net(br->dev);
577 skb = nlmsg_new(fdb_nlmsg_size(), GFP_ATOMIC);
581 err = fdb_fill_info(skb, br, fdb, 0, 0, type, 0);
583 /* -EMSGSIZE implies BUG in fdb_nlmsg_size() */
584 WARN_ON(err == -EMSGSIZE);
588 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
591 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
594 /* Dump information about entries, in response to GETNEIGH */
595 int br_fdb_dump(struct sk_buff *skb,
596 struct netlink_callback *cb,
597 struct net_device *dev,
600 struct net_bridge *br = netdev_priv(dev);
603 if (!(dev->priv_flags & IFF_EBRIDGE))
606 for (i = 0; i < BR_HASH_SIZE; i++) {
607 struct net_bridge_fdb_entry *f;
609 hlist_for_each_entry_rcu(f, &br->hash[i], hlist) {
610 if (idx < cb->args[0])
613 if (fdb_fill_info(skb, br, f,
614 NETLINK_CB(cb->skb).portid,
628 /* Update (create or replace) forwarding database entry */
629 static int fdb_add_entry(struct net_bridge_port *source, const __u8 *addr,
630 __u16 state, __u16 flags, __u16 vid)
632 struct net_bridge *br = source->br;
633 struct hlist_head *head = &br->hash[br_mac_hash(addr, vid)];
634 struct net_bridge_fdb_entry *fdb;
635 bool modified = false;
637 fdb = fdb_find(head, addr, vid);
639 if (!(flags & NLM_F_CREATE))
642 fdb = fdb_create(head, source, addr, vid);
648 if (flags & NLM_F_EXCL)
651 if (fdb->dst != source) {
657 if (fdb_to_nud(fdb) != state) {
658 if (state & NUD_PERMANENT)
659 fdb->is_local = fdb->is_static = 1;
660 else if (state & NUD_NOARP) {
664 fdb->is_local = fdb->is_static = 0;
668 fdb->added_by_user = 1;
672 fdb->updated = jiffies;
673 fdb_notify(br, fdb, RTM_NEWNEIGH);
679 static int __br_fdb_add(struct ndmsg *ndm, struct net_bridge_port *p,
680 const unsigned char *addr, u16 nlh_flags, u16 vid)
684 if (ndm->ndm_flags & NTF_USE) {
686 br_fdb_update(p->br, p, addr, vid, true);
689 spin_lock_bh(&p->br->hash_lock);
690 err = fdb_add_entry(p, addr, ndm->ndm_state,
692 spin_unlock_bh(&p->br->hash_lock);
698 /* Add new permanent fdb entry with RTM_NEWNEIGH */
699 int br_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
700 struct net_device *dev,
701 const unsigned char *addr, u16 nlh_flags)
703 struct net_bridge_port *p;
705 struct net_port_vlans *pv;
706 unsigned short vid = VLAN_N_VID;
708 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE))) {
709 pr_info("bridge: RTM_NEWNEIGH with invalid state %#x\n", ndm->ndm_state);
714 if (nla_len(tb[NDA_VLAN]) != sizeof(unsigned short)) {
715 pr_info("bridge: RTM_NEWNEIGH with invalid vlan\n");
719 vid = nla_get_u16(tb[NDA_VLAN]);
721 if (!vid || vid >= VLAN_VID_MASK) {
722 pr_info("bridge: RTM_NEWNEIGH with invalid vlan id %d\n",
728 if (is_zero_ether_addr(addr)) {
729 pr_info("bridge: RTM_NEWNEIGH with invalid ether address\n");
733 p = br_port_get_rtnl(dev);
735 pr_info("bridge: RTM_NEWNEIGH %s not a bridge port\n",
740 pv = nbp_get_vlan_info(p);
741 if (vid != VLAN_N_VID) {
742 if (!pv || !test_bit(vid, pv->vlan_bitmap)) {
743 pr_info("bridge: RTM_NEWNEIGH with unconfigured "
744 "vlan %d on port %s\n", vid, dev->name);
748 /* VID was specified, so use it. */
749 err = __br_fdb_add(ndm, p, addr, nlh_flags, vid);
751 if (!pv || bitmap_empty(pv->vlan_bitmap, VLAN_N_VID)) {
752 err = __br_fdb_add(ndm, p, addr, nlh_flags, 0);
756 /* We have vlans configured on this port and user didn't
757 * specify a VLAN. To be nice, add/update entry for every
760 for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID) {
761 err = __br_fdb_add(ndm, p, addr, nlh_flags, vid);
771 int fdb_delete_by_addr(struct net_bridge *br, const u8 *addr,
774 struct hlist_head *head = &br->hash[br_mac_hash(addr, vlan)];
775 struct net_bridge_fdb_entry *fdb;
777 fdb = fdb_find(head, addr, vlan);
785 static int __br_fdb_delete(struct net_bridge_port *p,
786 const unsigned char *addr, u16 vid)
790 spin_lock_bh(&p->br->hash_lock);
791 err = fdb_delete_by_addr(p->br, addr, vid);
792 spin_unlock_bh(&p->br->hash_lock);
797 /* Remove neighbor entry with RTM_DELNEIGH */
798 int br_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
799 struct net_device *dev,
800 const unsigned char *addr)
802 struct net_bridge_port *p;
804 struct net_port_vlans *pv;
805 unsigned short vid = VLAN_N_VID;
808 if (nla_len(tb[NDA_VLAN]) != sizeof(unsigned short)) {
809 pr_info("bridge: RTM_NEWNEIGH with invalid vlan\n");
813 vid = nla_get_u16(tb[NDA_VLAN]);
815 if (!vid || vid >= VLAN_VID_MASK) {
816 pr_info("bridge: RTM_NEWNEIGH with invalid vlan id %d\n",
821 p = br_port_get_rtnl(dev);
823 pr_info("bridge: RTM_DELNEIGH %s not a bridge port\n",
828 pv = nbp_get_vlan_info(p);
829 if (vid != VLAN_N_VID) {
830 if (!pv || !test_bit(vid, pv->vlan_bitmap)) {
831 pr_info("bridge: RTM_DELNEIGH with unconfigured "
832 "vlan %d on port %s\n", vid, dev->name);
836 err = __br_fdb_delete(p, addr, vid);
838 if (!pv || bitmap_empty(pv->vlan_bitmap, VLAN_N_VID)) {
839 err = __br_fdb_delete(p, addr, 0);
843 /* We have vlans configured on this port and user didn't
844 * specify a VLAN. To be nice, add/update entry for every
848 for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID) {
849 err &= __br_fdb_delete(p, addr, vid);