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 /* Delete a local entry if no other port had the same address. */
93 static void fdb_delete_local(struct net_bridge *br,
94 const struct net_bridge_port *p,
95 struct net_bridge_fdb_entry *f)
97 const unsigned char *addr = f->addr.addr;
99 struct net_bridge_port *op;
101 /* Maybe another port has same hw addr? */
102 list_for_each_entry(op, &br->port_list, list) {
103 if (op != p && ether_addr_equal(op->dev->dev_addr, addr) &&
104 (!vid || nbp_vlan_find(op, vid))) {
110 /* Maybe bridge device has same hw addr? */
111 if (p && ether_addr_equal(br->dev->dev_addr, addr) &&
112 (!vid || br_vlan_find(br, vid))) {
120 void br_fdb_changeaddr(struct net_bridge_port *p, const unsigned char *newaddr)
122 struct net_bridge *br = p->br;
123 struct net_port_vlans *pv = nbp_get_vlan_info(p);
128 spin_lock_bh(&br->hash_lock);
130 /* Search all chains since old address/hash is unknown */
131 for (i = 0; i < BR_HASH_SIZE; i++) {
132 struct hlist_node *h;
133 hlist_for_each(h, &br->hash[i]) {
134 struct net_bridge_fdb_entry *f;
136 f = hlist_entry(h, struct net_bridge_fdb_entry, hlist);
137 if (f->dst == p && f->is_local && !f->added_by_user) {
139 fdb_delete_local(br, p, f);
141 /* if this port has no vlan information
142 * configured, we can safely be done at
152 /* insert new address, may fail if invalid address or dup. */
153 fdb_insert(br, p, newaddr, 0);
158 /* Now add entries for every VLAN configured on the port.
159 * This function runs under RTNL so the bitmap will not change
162 for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID)
163 fdb_insert(br, p, newaddr, vid);
166 spin_unlock_bh(&br->hash_lock);
169 void br_fdb_change_mac_address(struct net_bridge *br, const u8 *newaddr)
171 struct net_bridge_fdb_entry *f;
172 struct net_port_vlans *pv;
175 /* If old entry was unassociated with any port, then delete it. */
176 f = __br_fdb_get(br, br->dev->dev_addr, 0);
177 if (f && f->is_local && !f->dst)
178 fdb_delete_local(br, NULL, f);
180 fdb_insert(br, NULL, newaddr, 0);
182 /* Now remove and add entries for every VLAN configured on the
183 * bridge. This function runs under RTNL so the bitmap will not
184 * change from under us.
186 pv = br_get_vlan_info(br);
190 for_each_set_bit_from(vid, pv->vlan_bitmap, VLAN_N_VID) {
191 f = __br_fdb_get(br, br->dev->dev_addr, vid);
192 if (f && f->is_local && !f->dst)
193 fdb_delete_local(br, NULL, f);
194 fdb_insert(br, NULL, newaddr, vid);
198 void br_fdb_cleanup(unsigned long _data)
200 struct net_bridge *br = (struct net_bridge *)_data;
201 unsigned long delay = hold_time(br);
202 unsigned long next_timer = jiffies + br->ageing_time;
205 spin_lock(&br->hash_lock);
206 for (i = 0; i < BR_HASH_SIZE; i++) {
207 struct net_bridge_fdb_entry *f;
208 struct hlist_node *n;
210 hlist_for_each_entry_safe(f, n, &br->hash[i], hlist) {
211 unsigned long this_timer;
214 this_timer = f->updated + delay;
215 if (time_before_eq(this_timer, jiffies))
217 else if (time_before(this_timer, next_timer))
218 next_timer = this_timer;
221 spin_unlock(&br->hash_lock);
223 mod_timer(&br->gc_timer, round_jiffies_up(next_timer));
226 /* Completely flush all dynamic entries in forwarding database.*/
227 void br_fdb_flush(struct net_bridge *br)
231 spin_lock_bh(&br->hash_lock);
232 for (i = 0; i < BR_HASH_SIZE; i++) {
233 struct net_bridge_fdb_entry *f;
234 struct hlist_node *n;
235 hlist_for_each_entry_safe(f, n, &br->hash[i], hlist) {
240 spin_unlock_bh(&br->hash_lock);
243 /* Flush all entries referring to a specific port.
244 * if do_all is set also flush static entries
246 void br_fdb_delete_by_port(struct net_bridge *br,
247 const struct net_bridge_port *p,
252 spin_lock_bh(&br->hash_lock);
253 for (i = 0; i < BR_HASH_SIZE; i++) {
254 struct hlist_node *h, *g;
256 hlist_for_each_safe(h, g, &br->hash[i]) {
257 struct net_bridge_fdb_entry *f
258 = hlist_entry(h, struct net_bridge_fdb_entry, hlist);
262 if (f->is_static && !do_all)
265 * if multiple ports all have the same device address
266 * then when one port is deleted, assign
267 * the local entry to other port
270 struct net_bridge_port *op;
271 list_for_each_entry(op, &br->port_list, list) {
273 ether_addr_equal(op->dev->dev_addr,
276 f->added_by_user = 0;
286 spin_unlock_bh(&br->hash_lock);
289 /* No locking or refcounting, assumes caller has rcu_read_lock */
290 struct net_bridge_fdb_entry *__br_fdb_get(struct net_bridge *br,
291 const unsigned char *addr,
294 struct net_bridge_fdb_entry *fdb;
296 hlist_for_each_entry_rcu(fdb,
297 &br->hash[br_mac_hash(addr, vid)], hlist) {
298 if (ether_addr_equal(fdb->addr.addr, addr) &&
299 fdb->vlan_id == vid) {
300 if (unlikely(has_expired(br, fdb)))
309 #if IS_ENABLED(CONFIG_ATM_LANE)
310 /* Interface used by ATM LANE hook to test
311 * if an addr is on some other bridge port */
312 int br_fdb_test_addr(struct net_device *dev, unsigned char *addr)
314 struct net_bridge_fdb_entry *fdb;
315 struct net_bridge_port *port;
319 port = br_port_get_rcu(dev);
323 fdb = __br_fdb_get(port->br, addr, 0);
324 ret = fdb && fdb->dst && fdb->dst->dev != dev &&
325 fdb->dst->state == BR_STATE_FORWARDING;
331 #endif /* CONFIG_ATM_LANE */
334 * Fill buffer with forwarding table records in
337 int br_fdb_fillbuf(struct net_bridge *br, void *buf,
338 unsigned long maxnum, unsigned long skip)
340 struct __fdb_entry *fe = buf;
342 struct net_bridge_fdb_entry *f;
344 memset(buf, 0, maxnum*sizeof(struct __fdb_entry));
347 for (i = 0; i < BR_HASH_SIZE; i++) {
348 hlist_for_each_entry_rcu(f, &br->hash[i], hlist) {
352 if (has_expired(br, f))
355 /* ignore pseudo entry for local MAC address */
364 /* convert from internal format to API */
365 memcpy(fe->mac_addr, f->addr.addr, ETH_ALEN);
367 /* due to ABI compat need to split into hi/lo */
368 fe->port_no = f->dst->port_no;
369 fe->port_hi = f->dst->port_no >> 8;
371 fe->is_local = f->is_local;
373 fe->ageing_timer_value = jiffies_delta_to_clock_t(jiffies - f->updated);
385 static struct net_bridge_fdb_entry *fdb_find(struct hlist_head *head,
386 const unsigned char *addr,
389 struct net_bridge_fdb_entry *fdb;
391 hlist_for_each_entry(fdb, head, hlist) {
392 if (ether_addr_equal(fdb->addr.addr, addr) &&
399 static struct net_bridge_fdb_entry *fdb_find_rcu(struct hlist_head *head,
400 const unsigned char *addr,
403 struct net_bridge_fdb_entry *fdb;
405 hlist_for_each_entry_rcu(fdb, head, hlist) {
406 if (ether_addr_equal(fdb->addr.addr, addr) &&
413 static struct net_bridge_fdb_entry *fdb_create(struct hlist_head *head,
414 struct net_bridge_port *source,
415 const unsigned char *addr,
418 struct net_bridge_fdb_entry *fdb;
420 fdb = kmem_cache_alloc(br_fdb_cache, GFP_ATOMIC);
422 memcpy(fdb->addr.addr, addr, ETH_ALEN);
427 fdb->added_by_user = 0;
428 fdb->updated = fdb->used = jiffies;
429 hlist_add_head_rcu(&fdb->hlist, head);
434 static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
435 const unsigned char *addr, u16 vid)
437 struct hlist_head *head = &br->hash[br_mac_hash(addr, vid)];
438 struct net_bridge_fdb_entry *fdb;
440 if (!is_valid_ether_addr(addr))
443 fdb = fdb_find(head, addr, vid);
445 /* it is okay to have multiple ports with same
446 * address, just use the first one.
450 br_warn(br, "adding interface %s with same address "
451 "as a received packet\n",
452 source ? source->dev->name : br->dev->name);
456 fdb = fdb_create(head, source, addr, vid);
460 fdb->is_local = fdb->is_static = 1;
461 fdb_notify(br, fdb, RTM_NEWNEIGH);
465 /* Add entry for local address of interface */
466 int br_fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
467 const unsigned char *addr, u16 vid)
471 spin_lock_bh(&br->hash_lock);
472 ret = fdb_insert(br, source, addr, vid);
473 spin_unlock_bh(&br->hash_lock);
477 void br_fdb_update(struct net_bridge *br, struct net_bridge_port *source,
478 const unsigned char *addr, u16 vid, bool added_by_user)
480 struct hlist_head *head = &br->hash[br_mac_hash(addr, vid)];
481 struct net_bridge_fdb_entry *fdb;
483 /* some users want to always flood. */
484 if (hold_time(br) == 0)
487 /* ignore packets unless we are using this port */
488 if (!(source->state == BR_STATE_LEARNING ||
489 source->state == BR_STATE_FORWARDING))
492 fdb = fdb_find_rcu(head, addr, vid);
494 /* attempt to update an entry for a local interface */
495 if (unlikely(fdb->is_local)) {
497 br_warn(br, "received packet on %s with "
498 "own address as source address\n",
501 /* fastpath: update of existing entry */
503 fdb->updated = jiffies;
504 if (unlikely(added_by_user))
505 fdb->added_by_user = 1;
508 spin_lock(&br->hash_lock);
509 if (likely(!fdb_find(head, addr, vid))) {
510 fdb = fdb_create(head, source, addr, vid);
512 if (unlikely(added_by_user))
513 fdb->added_by_user = 1;
514 fdb_notify(br, fdb, RTM_NEWNEIGH);
517 /* else we lose race and someone else inserts
518 * it first, don't bother updating
520 spin_unlock(&br->hash_lock);
524 static int fdb_to_nud(const struct net_bridge_fdb_entry *fdb)
527 return NUD_PERMANENT;
528 else if (fdb->is_static)
530 else if (has_expired(fdb->dst->br, fdb))
533 return NUD_REACHABLE;
536 static int fdb_fill_info(struct sk_buff *skb, const struct net_bridge *br,
537 const struct net_bridge_fdb_entry *fdb,
538 u32 portid, u32 seq, int type, unsigned int flags)
540 unsigned long now = jiffies;
541 struct nda_cacheinfo ci;
542 struct nlmsghdr *nlh;
545 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
549 ndm = nlmsg_data(nlh);
550 ndm->ndm_family = AF_BRIDGE;
555 ndm->ndm_ifindex = fdb->dst ? fdb->dst->dev->ifindex : br->dev->ifindex;
556 ndm->ndm_state = fdb_to_nud(fdb);
558 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->addr))
559 goto nla_put_failure;
560 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
561 ci.ndm_confirmed = 0;
562 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
564 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
565 goto nla_put_failure;
567 if (nla_put(skb, NDA_VLAN, sizeof(u16), &fdb->vlan_id))
568 goto nla_put_failure;
570 return nlmsg_end(skb, nlh);
573 nlmsg_cancel(skb, nlh);
577 static inline size_t fdb_nlmsg_size(void)
579 return NLMSG_ALIGN(sizeof(struct ndmsg))
580 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
581 + nla_total_size(sizeof(u16)) /* NDA_VLAN */
582 + nla_total_size(sizeof(struct nda_cacheinfo));
585 static void fdb_notify(struct net_bridge *br,
586 const struct net_bridge_fdb_entry *fdb, int type)
588 struct net *net = dev_net(br->dev);
592 skb = nlmsg_new(fdb_nlmsg_size(), GFP_ATOMIC);
596 err = fdb_fill_info(skb, br, fdb, 0, 0, type, 0);
598 /* -EMSGSIZE implies BUG in fdb_nlmsg_size() */
599 WARN_ON(err == -EMSGSIZE);
603 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
606 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
609 /* Dump information about entries, in response to GETNEIGH */
610 int br_fdb_dump(struct sk_buff *skb,
611 struct netlink_callback *cb,
612 struct net_device *dev,
615 struct net_bridge *br = netdev_priv(dev);
618 if (!(dev->priv_flags & IFF_EBRIDGE))
621 for (i = 0; i < BR_HASH_SIZE; i++) {
622 struct net_bridge_fdb_entry *f;
624 hlist_for_each_entry_rcu(f, &br->hash[i], hlist) {
625 if (idx < cb->args[0])
628 if (fdb_fill_info(skb, br, f,
629 NETLINK_CB(cb->skb).portid,
643 /* Update (create or replace) forwarding database entry */
644 static int fdb_add_entry(struct net_bridge_port *source, const __u8 *addr,
645 __u16 state, __u16 flags, __u16 vid)
647 struct net_bridge *br = source->br;
648 struct hlist_head *head = &br->hash[br_mac_hash(addr, vid)];
649 struct net_bridge_fdb_entry *fdb;
650 bool modified = false;
652 fdb = fdb_find(head, addr, vid);
654 if (!(flags & NLM_F_CREATE))
657 fdb = fdb_create(head, source, addr, vid);
663 if (flags & NLM_F_EXCL)
666 if (fdb->dst != source) {
672 if (fdb_to_nud(fdb) != state) {
673 if (state & NUD_PERMANENT)
674 fdb->is_local = fdb->is_static = 1;
675 else if (state & NUD_NOARP) {
679 fdb->is_local = fdb->is_static = 0;
683 fdb->added_by_user = 1;
687 fdb->updated = jiffies;
688 fdb_notify(br, fdb, RTM_NEWNEIGH);
694 static int __br_fdb_add(struct ndmsg *ndm, struct net_bridge_port *p,
695 const unsigned char *addr, u16 nlh_flags, u16 vid)
699 if (ndm->ndm_flags & NTF_USE) {
701 br_fdb_update(p->br, p, addr, vid, true);
704 spin_lock_bh(&p->br->hash_lock);
705 err = fdb_add_entry(p, addr, ndm->ndm_state,
707 spin_unlock_bh(&p->br->hash_lock);
713 /* Add new permanent fdb entry with RTM_NEWNEIGH */
714 int br_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
715 struct net_device *dev,
716 const unsigned char *addr, u16 nlh_flags)
718 struct net_bridge_port *p;
720 struct net_port_vlans *pv;
721 unsigned short vid = VLAN_N_VID;
723 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE))) {
724 pr_info("bridge: RTM_NEWNEIGH with invalid state %#x\n", ndm->ndm_state);
729 if (nla_len(tb[NDA_VLAN]) != sizeof(unsigned short)) {
730 pr_info("bridge: RTM_NEWNEIGH with invalid vlan\n");
734 vid = nla_get_u16(tb[NDA_VLAN]);
736 if (!vid || vid >= VLAN_VID_MASK) {
737 pr_info("bridge: RTM_NEWNEIGH with invalid vlan id %d\n",
743 if (is_zero_ether_addr(addr)) {
744 pr_info("bridge: RTM_NEWNEIGH with invalid ether address\n");
748 p = br_port_get_rtnl(dev);
750 pr_info("bridge: RTM_NEWNEIGH %s not a bridge port\n",
755 pv = nbp_get_vlan_info(p);
756 if (vid != VLAN_N_VID) {
757 if (!pv || !test_bit(vid, pv->vlan_bitmap)) {
758 pr_info("bridge: RTM_NEWNEIGH with unconfigured "
759 "vlan %d on port %s\n", vid, dev->name);
763 /* VID was specified, so use it. */
764 err = __br_fdb_add(ndm, p, addr, nlh_flags, vid);
766 if (!pv || bitmap_empty(pv->vlan_bitmap, VLAN_N_VID)) {
767 err = __br_fdb_add(ndm, p, addr, nlh_flags, 0);
771 /* We have vlans configured on this port and user didn't
772 * specify a VLAN. To be nice, add/update entry for every
775 for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID) {
776 err = __br_fdb_add(ndm, p, addr, nlh_flags, vid);
786 int fdb_delete_by_addr(struct net_bridge *br, const u8 *addr,
789 struct hlist_head *head = &br->hash[br_mac_hash(addr, vlan)];
790 struct net_bridge_fdb_entry *fdb;
792 fdb = fdb_find(head, addr, vlan);
800 static int __br_fdb_delete(struct net_bridge_port *p,
801 const unsigned char *addr, u16 vid)
805 spin_lock_bh(&p->br->hash_lock);
806 err = fdb_delete_by_addr(p->br, addr, vid);
807 spin_unlock_bh(&p->br->hash_lock);
812 /* Remove neighbor entry with RTM_DELNEIGH */
813 int br_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
814 struct net_device *dev,
815 const unsigned char *addr)
817 struct net_bridge_port *p;
819 struct net_port_vlans *pv;
820 unsigned short vid = VLAN_N_VID;
823 if (nla_len(tb[NDA_VLAN]) != sizeof(unsigned short)) {
824 pr_info("bridge: RTM_NEWNEIGH with invalid vlan\n");
828 vid = nla_get_u16(tb[NDA_VLAN]);
830 if (!vid || vid >= VLAN_VID_MASK) {
831 pr_info("bridge: RTM_NEWNEIGH with invalid vlan id %d\n",
836 p = br_port_get_rtnl(dev);
838 pr_info("bridge: RTM_DELNEIGH %s not a bridge port\n",
843 pv = nbp_get_vlan_info(p);
844 if (vid != VLAN_N_VID) {
845 if (!pv || !test_bit(vid, pv->vlan_bitmap)) {
846 pr_info("bridge: RTM_DELNEIGH with unconfigured "
847 "vlan %d on port %s\n", vid, dev->name);
851 err = __br_fdb_delete(p, addr, vid);
853 if (!pv || bitmap_empty(pv->vlan_bitmap, VLAN_N_VID)) {
854 err = __br_fdb_delete(p, addr, 0);
858 /* We have vlans configured on this port and user didn't
859 * specify a VLAN. To be nice, add/update entry for every
863 for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID) {
864 err &= __br_fdb_delete(p, addr, vid);