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 bool no_vlan = (nbp_get_vlan_info(p) == NULL) ? true : false;
98 spin_lock_bh(&br->hash_lock);
100 /* Search all chains since old address/hash is unknown */
101 for (i = 0; i < BR_HASH_SIZE; i++) {
102 struct hlist_node *h;
103 hlist_for_each(h, &br->hash[i]) {
104 struct net_bridge_fdb_entry *f;
106 f = hlist_entry(h, struct net_bridge_fdb_entry, hlist);
107 if (f->dst == p && f->is_local) {
108 /* maybe another port has same hw addr? */
109 struct net_bridge_port *op;
110 u16 vid = f->vlan_id;
111 list_for_each_entry(op, &br->port_list, list) {
113 ether_addr_equal(op->dev->dev_addr,
115 nbp_vlan_find(op, vid)) {
124 /* insert new address, may fail if invalid
127 fdb_insert(br, p, newaddr, vid);
129 /* if this port has no vlan information
130 * configured, we can safely be done at
140 spin_unlock_bh(&br->hash_lock);
143 void br_fdb_change_mac_address(struct net_bridge *br, const u8 *newaddr)
145 struct net_bridge_fdb_entry *f;
146 struct net_port_vlans *pv;
149 /* If old entry was unassociated with any port, then delete it. */
150 f = __br_fdb_get(br, br->dev->dev_addr, 0);
151 if (f && f->is_local && !f->dst)
154 fdb_insert(br, NULL, newaddr, 0);
156 /* Now remove and add entries for every VLAN configured on the
157 * bridge. This function runs under RTNL so the bitmap will not
158 * change from under us.
160 pv = br_get_vlan_info(br);
164 for (vid = find_next_bit(pv->vlan_bitmap, BR_VLAN_BITMAP_LEN, vid);
165 vid < BR_VLAN_BITMAP_LEN;
166 vid = find_next_bit(pv->vlan_bitmap, BR_VLAN_BITMAP_LEN, vid+1)) {
167 f = __br_fdb_get(br, br->dev->dev_addr, vid);
168 if (f && f->is_local && !f->dst)
170 fdb_insert(br, NULL, newaddr, vid);
174 void br_fdb_cleanup(unsigned long _data)
176 struct net_bridge *br = (struct net_bridge *)_data;
177 unsigned long delay = hold_time(br);
178 unsigned long next_timer = jiffies + br->ageing_time;
181 spin_lock(&br->hash_lock);
182 for (i = 0; i < BR_HASH_SIZE; i++) {
183 struct net_bridge_fdb_entry *f;
184 struct hlist_node *n;
186 hlist_for_each_entry_safe(f, n, &br->hash[i], hlist) {
187 unsigned long this_timer;
190 this_timer = f->updated + delay;
191 if (time_before_eq(this_timer, jiffies))
193 else if (time_before(this_timer, next_timer))
194 next_timer = this_timer;
197 spin_unlock(&br->hash_lock);
199 mod_timer(&br->gc_timer, round_jiffies_up(next_timer));
202 /* Completely flush all dynamic entries in forwarding database.*/
203 void br_fdb_flush(struct net_bridge *br)
207 spin_lock_bh(&br->hash_lock);
208 for (i = 0; i < BR_HASH_SIZE; i++) {
209 struct net_bridge_fdb_entry *f;
210 struct hlist_node *n;
211 hlist_for_each_entry_safe(f, n, &br->hash[i], hlist) {
216 spin_unlock_bh(&br->hash_lock);
219 /* Flush all entries referring to a specific port.
220 * if do_all is set also flush static entries
222 void br_fdb_delete_by_port(struct net_bridge *br,
223 const struct net_bridge_port *p,
228 spin_lock_bh(&br->hash_lock);
229 for (i = 0; i < BR_HASH_SIZE; i++) {
230 struct hlist_node *h, *g;
232 hlist_for_each_safe(h, g, &br->hash[i]) {
233 struct net_bridge_fdb_entry *f
234 = hlist_entry(h, struct net_bridge_fdb_entry, hlist);
238 if (f->is_static && !do_all)
241 * if multiple ports all have the same device address
242 * then when one port is deleted, assign
243 * the local entry to other port
246 struct net_bridge_port *op;
247 list_for_each_entry(op, &br->port_list, list) {
249 ether_addr_equal(op->dev->dev_addr,
261 spin_unlock_bh(&br->hash_lock);
264 /* No locking or refcounting, assumes caller has rcu_read_lock */
265 struct net_bridge_fdb_entry *__br_fdb_get(struct net_bridge *br,
266 const unsigned char *addr,
269 struct net_bridge_fdb_entry *fdb;
271 hlist_for_each_entry_rcu(fdb,
272 &br->hash[br_mac_hash(addr, vid)], hlist) {
273 if (ether_addr_equal(fdb->addr.addr, addr) &&
274 fdb->vlan_id == vid) {
275 if (unlikely(has_expired(br, fdb)))
284 #if IS_ENABLED(CONFIG_ATM_LANE)
285 /* Interface used by ATM LANE hook to test
286 * if an addr is on some other bridge port */
287 int br_fdb_test_addr(struct net_device *dev, unsigned char *addr)
289 struct net_bridge_fdb_entry *fdb;
290 struct net_bridge_port *port;
294 port = br_port_get_rcu(dev);
298 fdb = __br_fdb_get(port->br, addr, 0);
299 ret = fdb && fdb->dst && fdb->dst->dev != dev &&
300 fdb->dst->state == BR_STATE_FORWARDING;
306 #endif /* CONFIG_ATM_LANE */
309 * Fill buffer with forwarding table records in
312 int br_fdb_fillbuf(struct net_bridge *br, void *buf,
313 unsigned long maxnum, unsigned long skip)
315 struct __fdb_entry *fe = buf;
317 struct net_bridge_fdb_entry *f;
319 memset(buf, 0, maxnum*sizeof(struct __fdb_entry));
322 for (i = 0; i < BR_HASH_SIZE; i++) {
323 hlist_for_each_entry_rcu(f, &br->hash[i], hlist) {
327 if (has_expired(br, f))
330 /* ignore pseudo entry for local MAC address */
339 /* convert from internal format to API */
340 memcpy(fe->mac_addr, f->addr.addr, ETH_ALEN);
342 /* due to ABI compat need to split into hi/lo */
343 fe->port_no = f->dst->port_no;
344 fe->port_hi = f->dst->port_no >> 8;
346 fe->is_local = f->is_local;
348 fe->ageing_timer_value = jiffies_delta_to_clock_t(jiffies - f->updated);
360 static struct net_bridge_fdb_entry *fdb_find(struct hlist_head *head,
361 const unsigned char *addr,
364 struct net_bridge_fdb_entry *fdb;
366 hlist_for_each_entry(fdb, head, hlist) {
367 if (ether_addr_equal(fdb->addr.addr, addr) &&
374 static struct net_bridge_fdb_entry *fdb_find_rcu(struct hlist_head *head,
375 const unsigned char *addr,
378 struct net_bridge_fdb_entry *fdb;
380 hlist_for_each_entry_rcu(fdb, head, hlist) {
381 if (ether_addr_equal(fdb->addr.addr, addr) &&
388 static struct net_bridge_fdb_entry *fdb_create(struct hlist_head *head,
389 struct net_bridge_port *source,
390 const unsigned char *addr,
393 struct net_bridge_fdb_entry *fdb;
395 fdb = kmem_cache_alloc(br_fdb_cache, GFP_ATOMIC);
397 memcpy(fdb->addr.addr, addr, ETH_ALEN);
402 fdb->updated = fdb->used = jiffies;
403 hlist_add_head_rcu(&fdb->hlist, head);
408 static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
409 const unsigned char *addr, u16 vid)
411 struct hlist_head *head = &br->hash[br_mac_hash(addr, vid)];
412 struct net_bridge_fdb_entry *fdb;
414 if (!is_valid_ether_addr(addr))
417 fdb = fdb_find(head, addr, vid);
419 /* it is okay to have multiple ports with same
420 * address, just use the first one.
424 br_warn(br, "adding interface %s with same address "
425 "as a received packet\n",
430 fdb = fdb_create(head, source, addr, vid);
434 fdb->is_local = fdb->is_static = 1;
435 fdb_notify(br, fdb, RTM_NEWNEIGH);
439 /* Add entry for local address of interface */
440 int br_fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
441 const unsigned char *addr, u16 vid)
445 spin_lock_bh(&br->hash_lock);
446 ret = fdb_insert(br, source, addr, vid);
447 spin_unlock_bh(&br->hash_lock);
451 void br_fdb_update(struct net_bridge *br, struct net_bridge_port *source,
452 const unsigned char *addr, u16 vid)
454 struct hlist_head *head = &br->hash[br_mac_hash(addr, vid)];
455 struct net_bridge_fdb_entry *fdb;
457 /* some users want to always flood. */
458 if (hold_time(br) == 0)
461 /* ignore packets unless we are using this port */
462 if (!(source->state == BR_STATE_LEARNING ||
463 source->state == BR_STATE_FORWARDING))
466 fdb = fdb_find_rcu(head, addr, vid);
468 /* attempt to update an entry for a local interface */
469 if (unlikely(fdb->is_local)) {
471 br_warn(br, "received packet on %s with "
472 "own address as source address\n",
475 /* fastpath: update of existing entry */
477 fdb->updated = jiffies;
480 spin_lock(&br->hash_lock);
481 if (likely(!fdb_find(head, addr, vid))) {
482 fdb = fdb_create(head, source, addr, vid);
484 fdb_notify(br, fdb, RTM_NEWNEIGH);
486 /* else we lose race and someone else inserts
487 * it first, don't bother updating
489 spin_unlock(&br->hash_lock);
493 static int fdb_to_nud(const struct net_bridge_fdb_entry *fdb)
496 return NUD_PERMANENT;
497 else if (fdb->is_static)
499 else if (has_expired(fdb->dst->br, fdb))
502 return NUD_REACHABLE;
505 static int fdb_fill_info(struct sk_buff *skb, const struct net_bridge *br,
506 const struct net_bridge_fdb_entry *fdb,
507 u32 portid, u32 seq, int type, unsigned int flags)
509 unsigned long now = jiffies;
510 struct nda_cacheinfo ci;
511 struct nlmsghdr *nlh;
514 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
518 ndm = nlmsg_data(nlh);
519 ndm->ndm_family = AF_BRIDGE;
524 ndm->ndm_ifindex = fdb->dst ? fdb->dst->dev->ifindex : br->dev->ifindex;
525 ndm->ndm_state = fdb_to_nud(fdb);
527 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->addr))
528 goto nla_put_failure;
529 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
530 ci.ndm_confirmed = 0;
531 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
533 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
534 goto nla_put_failure;
536 if (nla_put(skb, NDA_VLAN, sizeof(u16), &fdb->vlan_id))
537 goto nla_put_failure;
539 return nlmsg_end(skb, nlh);
542 nlmsg_cancel(skb, nlh);
546 static inline size_t fdb_nlmsg_size(void)
548 return NLMSG_ALIGN(sizeof(struct ndmsg))
549 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
550 + nla_total_size(sizeof(u16)) /* NDA_VLAN */
551 + nla_total_size(sizeof(struct nda_cacheinfo));
554 static void fdb_notify(struct net_bridge *br,
555 const struct net_bridge_fdb_entry *fdb, int type)
557 struct net *net = dev_net(br->dev);
561 skb = nlmsg_new(fdb_nlmsg_size(), GFP_ATOMIC);
565 err = fdb_fill_info(skb, br, fdb, 0, 0, type, 0);
567 /* -EMSGSIZE implies BUG in fdb_nlmsg_size() */
568 WARN_ON(err == -EMSGSIZE);
572 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
576 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
579 /* Dump information about entries, in response to GETNEIGH */
580 int br_fdb_dump(struct sk_buff *skb,
581 struct netlink_callback *cb,
582 struct net_device *dev,
585 struct net_bridge *br = netdev_priv(dev);
588 if (!(dev->priv_flags & IFF_EBRIDGE))
591 for (i = 0; i < BR_HASH_SIZE; i++) {
592 struct net_bridge_fdb_entry *f;
594 hlist_for_each_entry_rcu(f, &br->hash[i], hlist) {
595 if (idx < cb->args[0])
598 if (fdb_fill_info(skb, br, f,
599 NETLINK_CB(cb->skb).portid,
613 /* Update (create or replace) forwarding database entry */
614 static int fdb_add_entry(struct net_bridge_port *source, const __u8 *addr,
615 __u16 state, __u16 flags, __u16 vid)
617 struct net_bridge *br = source->br;
618 struct hlist_head *head = &br->hash[br_mac_hash(addr, vid)];
619 struct net_bridge_fdb_entry *fdb;
621 fdb = fdb_find(head, addr, vid);
623 if (!(flags & NLM_F_CREATE))
626 fdb = fdb_create(head, source, addr, vid);
629 fdb_notify(br, fdb, RTM_NEWNEIGH);
631 if (flags & NLM_F_EXCL)
635 if (fdb_to_nud(fdb) != state) {
636 if (state & NUD_PERMANENT)
637 fdb->is_local = fdb->is_static = 1;
638 else if (state & NUD_NOARP) {
642 fdb->is_local = fdb->is_static = 0;
644 fdb->updated = fdb->used = jiffies;
645 fdb_notify(br, fdb, RTM_NEWNEIGH);
651 static int __br_fdb_add(struct ndmsg *ndm, struct net_bridge_port *p,
652 const unsigned char *addr, u16 nlh_flags, u16 vid)
656 if (ndm->ndm_flags & NTF_USE) {
658 br_fdb_update(p->br, p, addr, vid);
661 spin_lock_bh(&p->br->hash_lock);
662 err = fdb_add_entry(p, addr, ndm->ndm_state,
664 spin_unlock_bh(&p->br->hash_lock);
670 /* Add new permanent fdb entry with RTM_NEWNEIGH */
671 int br_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
672 struct net_device *dev,
673 const unsigned char *addr, u16 nlh_flags)
675 struct net_bridge_port *p;
677 struct net_port_vlans *pv;
678 unsigned short vid = VLAN_N_VID;
680 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE))) {
681 pr_info("bridge: RTM_NEWNEIGH with invalid state %#x\n", ndm->ndm_state);
686 if (nla_len(tb[NDA_VLAN]) != sizeof(unsigned short)) {
687 pr_info("bridge: RTM_NEWNEIGH with invalid vlan\n");
691 vid = nla_get_u16(tb[NDA_VLAN]);
693 if (vid >= VLAN_N_VID) {
694 pr_info("bridge: RTM_NEWNEIGH with invalid vlan id %d\n",
700 p = br_port_get_rtnl(dev);
702 pr_info("bridge: RTM_NEWNEIGH %s not a bridge port\n",
707 pv = nbp_get_vlan_info(p);
708 if (vid != VLAN_N_VID) {
709 if (!pv || !test_bit(vid, pv->vlan_bitmap)) {
710 pr_info("bridge: RTM_NEWNEIGH with unconfigured "
711 "vlan %d on port %s\n", vid, dev->name);
715 /* VID was specified, so use it. */
716 err = __br_fdb_add(ndm, p, addr, nlh_flags, vid);
718 if (!pv || bitmap_empty(pv->vlan_bitmap, BR_VLAN_BITMAP_LEN)) {
719 err = __br_fdb_add(ndm, p, addr, nlh_flags, 0);
723 /* We have vlans configured on this port and user didn't
724 * specify a VLAN. To be nice, add/update entry for every
727 vid = find_first_bit(pv->vlan_bitmap, BR_VLAN_BITMAP_LEN);
728 while (vid < BR_VLAN_BITMAP_LEN) {
729 err = __br_fdb_add(ndm, p, addr, nlh_flags, vid);
732 vid = find_next_bit(pv->vlan_bitmap,
733 BR_VLAN_BITMAP_LEN, vid+1);
741 int fdb_delete_by_addr(struct net_bridge *br, const u8 *addr,
744 struct hlist_head *head = &br->hash[br_mac_hash(addr, vlan)];
745 struct net_bridge_fdb_entry *fdb;
747 fdb = fdb_find(head, addr, vlan);
755 static int __br_fdb_delete(struct net_bridge_port *p,
756 const unsigned char *addr, u16 vid)
760 spin_lock_bh(&p->br->hash_lock);
761 err = fdb_delete_by_addr(p->br, addr, vid);
762 spin_unlock_bh(&p->br->hash_lock);
767 /* Remove neighbor entry with RTM_DELNEIGH */
768 int br_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
769 struct net_device *dev,
770 const unsigned char *addr)
772 struct net_bridge_port *p;
774 struct net_port_vlans *pv;
775 unsigned short vid = VLAN_N_VID;
778 if (nla_len(tb[NDA_VLAN]) != sizeof(unsigned short)) {
779 pr_info("bridge: RTM_NEWNEIGH with invalid vlan\n");
783 vid = nla_get_u16(tb[NDA_VLAN]);
785 if (vid >= VLAN_N_VID) {
786 pr_info("bridge: RTM_NEWNEIGH with invalid vlan id %d\n",
791 p = br_port_get_rtnl(dev);
793 pr_info("bridge: RTM_DELNEIGH %s not a bridge port\n",
798 pv = nbp_get_vlan_info(p);
799 if (vid != VLAN_N_VID) {
800 if (!pv || !test_bit(vid, pv->vlan_bitmap)) {
801 pr_info("bridge: RTM_DELNEIGH with unconfigured "
802 "vlan %d on port %s\n", vid, dev->name);
806 err = __br_fdb_delete(p, addr, vid);
808 if (!pv || bitmap_empty(pv->vlan_bitmap, BR_VLAN_BITMAP_LEN)) {
809 err = __br_fdb_delete(p, addr, 0);
813 /* We have vlans configured on this port and user didn't
814 * specify a VLAN. To be nice, add/update entry for every
818 vid = find_first_bit(pv->vlan_bitmap, BR_VLAN_BITMAP_LEN);
819 while (vid < BR_VLAN_BITMAP_LEN) {
820 err &= __br_fdb_delete(p, addr, vid);
821 vid = find_next_bit(pv->vlan_bitmap,
822 BR_VLAN_BITMAP_LEN, vid+1);