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);
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;
97 spin_lock_bh(&br->hash_lock);
99 /* Search all chains since old address/hash is unknown */
100 for (i = 0; i < BR_HASH_SIZE; i++) {
101 struct hlist_node *h;
102 hlist_for_each(h, &br->hash[i]) {
103 struct net_bridge_fdb_entry *f;
105 f = hlist_entry(h, struct net_bridge_fdb_entry, hlist);
106 if (f->dst == p && f->is_local) {
107 /* maybe another port has same hw addr? */
108 struct net_bridge_port *op;
109 list_for_each_entry(op, &br->port_list, list) {
111 ether_addr_equal(op->dev->dev_addr,
125 /* insert new address, may fail if invalid address or dup. */
126 fdb_insert(br, p, newaddr);
128 spin_unlock_bh(&br->hash_lock);
131 void br_fdb_change_mac_address(struct net_bridge *br, const u8 *newaddr)
133 struct net_bridge_fdb_entry *f;
135 /* If old entry was unassociated with any port, then delete it. */
136 f = __br_fdb_get(br, br->dev->dev_addr, 0);
137 if (f && f->is_local && !f->dst)
140 fdb_insert(br, NULL, newaddr);
143 void br_fdb_cleanup(unsigned long _data)
145 struct net_bridge *br = (struct net_bridge *)_data;
146 unsigned long delay = hold_time(br);
147 unsigned long next_timer = jiffies + br->ageing_time;
150 spin_lock(&br->hash_lock);
151 for (i = 0; i < BR_HASH_SIZE; i++) {
152 struct net_bridge_fdb_entry *f;
153 struct hlist_node *h, *n;
155 hlist_for_each_entry_safe(f, h, n, &br->hash[i], hlist) {
156 unsigned long this_timer;
159 this_timer = f->updated + delay;
160 if (time_before_eq(this_timer, jiffies))
162 else if (time_before(this_timer, next_timer))
163 next_timer = this_timer;
166 spin_unlock(&br->hash_lock);
168 mod_timer(&br->gc_timer, round_jiffies_up(next_timer));
171 /* Completely flush all dynamic entries in forwarding database.*/
172 void br_fdb_flush(struct net_bridge *br)
176 spin_lock_bh(&br->hash_lock);
177 for (i = 0; i < BR_HASH_SIZE; i++) {
178 struct net_bridge_fdb_entry *f;
179 struct hlist_node *h, *n;
180 hlist_for_each_entry_safe(f, h, n, &br->hash[i], hlist) {
185 spin_unlock_bh(&br->hash_lock);
188 /* Flush all entries referring to a specific port.
189 * if do_all is set also flush static entries
191 void br_fdb_delete_by_port(struct net_bridge *br,
192 const struct net_bridge_port *p,
197 spin_lock_bh(&br->hash_lock);
198 for (i = 0; i < BR_HASH_SIZE; i++) {
199 struct hlist_node *h, *g;
201 hlist_for_each_safe(h, g, &br->hash[i]) {
202 struct net_bridge_fdb_entry *f
203 = hlist_entry(h, struct net_bridge_fdb_entry, hlist);
207 if (f->is_static && !do_all)
210 * if multiple ports all have the same device address
211 * then when one port is deleted, assign
212 * the local entry to other port
215 struct net_bridge_port *op;
216 list_for_each_entry(op, &br->port_list, list) {
218 ether_addr_equal(op->dev->dev_addr,
230 spin_unlock_bh(&br->hash_lock);
233 /* No locking or refcounting, assumes caller has rcu_read_lock */
234 struct net_bridge_fdb_entry *__br_fdb_get(struct net_bridge *br,
235 const unsigned char *addr,
238 struct hlist_node *h;
239 struct net_bridge_fdb_entry *fdb;
241 hlist_for_each_entry_rcu(fdb, h,
242 &br->hash[br_mac_hash(addr, vid)], hlist) {
243 if (ether_addr_equal(fdb->addr.addr, addr) &&
244 fdb->vlan_id == vid) {
245 if (unlikely(has_expired(br, fdb)))
254 #if IS_ENABLED(CONFIG_ATM_LANE)
255 /* Interface used by ATM LANE hook to test
256 * if an addr is on some other bridge port */
257 int br_fdb_test_addr(struct net_device *dev, unsigned char *addr)
259 struct net_bridge_fdb_entry *fdb;
260 struct net_bridge_port *port;
264 port = br_port_get_rcu(dev);
268 fdb = __br_fdb_get(port->br, addr, 0);
269 ret = fdb && fdb->dst && fdb->dst->dev != dev &&
270 fdb->dst->state == BR_STATE_FORWARDING;
276 #endif /* CONFIG_ATM_LANE */
279 * Fill buffer with forwarding table records in
282 int br_fdb_fillbuf(struct net_bridge *br, void *buf,
283 unsigned long maxnum, unsigned long skip)
285 struct __fdb_entry *fe = buf;
287 struct hlist_node *h;
288 struct net_bridge_fdb_entry *f;
290 memset(buf, 0, maxnum*sizeof(struct __fdb_entry));
293 for (i = 0; i < BR_HASH_SIZE; i++) {
294 hlist_for_each_entry_rcu(f, h, &br->hash[i], hlist) {
298 if (has_expired(br, f))
301 /* ignore pseudo entry for local MAC address */
310 /* convert from internal format to API */
311 memcpy(fe->mac_addr, f->addr.addr, ETH_ALEN);
313 /* due to ABI compat need to split into hi/lo */
314 fe->port_no = f->dst->port_no;
315 fe->port_hi = f->dst->port_no >> 8;
317 fe->is_local = f->is_local;
319 fe->ageing_timer_value = jiffies_delta_to_clock_t(jiffies - f->updated);
331 static struct net_bridge_fdb_entry *fdb_find(struct hlist_head *head,
332 const unsigned char *addr,
335 struct hlist_node *h;
336 struct net_bridge_fdb_entry *fdb;
338 hlist_for_each_entry(fdb, h, head, hlist) {
339 if (ether_addr_equal(fdb->addr.addr, addr) &&
346 static struct net_bridge_fdb_entry *fdb_find_rcu(struct hlist_head *head,
347 const unsigned char *addr,
350 struct hlist_node *h;
351 struct net_bridge_fdb_entry *fdb;
353 hlist_for_each_entry_rcu(fdb, h, head, hlist) {
354 if (ether_addr_equal(fdb->addr.addr, addr) &&
361 static struct net_bridge_fdb_entry *fdb_create(struct hlist_head *head,
362 struct net_bridge_port *source,
363 const unsigned char *addr,
366 struct net_bridge_fdb_entry *fdb;
368 fdb = kmem_cache_alloc(br_fdb_cache, GFP_ATOMIC);
370 memcpy(fdb->addr.addr, addr, ETH_ALEN);
375 fdb->updated = fdb->used = jiffies;
376 hlist_add_head_rcu(&fdb->hlist, head);
381 static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
382 const unsigned char *addr)
384 struct hlist_head *head = &br->hash[br_mac_hash(addr, 0)];
385 struct net_bridge_fdb_entry *fdb;
387 if (!is_valid_ether_addr(addr))
390 fdb = fdb_find(head, addr, 0);
392 /* it is okay to have multiple ports with same
393 * address, just use the first one.
397 br_warn(br, "adding interface %s with same address "
398 "as a received packet\n",
403 fdb = fdb_create(head, source, addr, 0);
407 fdb->is_local = fdb->is_static = 1;
408 fdb_notify(br, fdb, RTM_NEWNEIGH);
412 /* Add entry for local address of interface */
413 int br_fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
414 const unsigned char *addr)
418 spin_lock_bh(&br->hash_lock);
419 ret = fdb_insert(br, source, addr);
420 spin_unlock_bh(&br->hash_lock);
424 void br_fdb_update(struct net_bridge *br, struct net_bridge_port *source,
425 const unsigned char *addr, u16 vid)
427 struct hlist_head *head = &br->hash[br_mac_hash(addr, vid)];
428 struct net_bridge_fdb_entry *fdb;
430 /* some users want to always flood. */
431 if (hold_time(br) == 0)
434 /* ignore packets unless we are using this port */
435 if (!(source->state == BR_STATE_LEARNING ||
436 source->state == BR_STATE_FORWARDING))
439 fdb = fdb_find_rcu(head, addr, vid);
441 /* attempt to update an entry for a local interface */
442 if (unlikely(fdb->is_local)) {
444 br_warn(br, "received packet on %s with "
445 "own address as source address\n",
448 /* fastpath: update of existing entry */
450 fdb->updated = jiffies;
453 spin_lock(&br->hash_lock);
454 if (likely(!fdb_find(head, addr, vid))) {
455 fdb = fdb_create(head, source, addr, vid);
457 fdb_notify(br, fdb, RTM_NEWNEIGH);
459 /* else we lose race and someone else inserts
460 * it first, don't bother updating
462 spin_unlock(&br->hash_lock);
466 static int fdb_to_nud(const struct net_bridge_fdb_entry *fdb)
469 return NUD_PERMANENT;
470 else if (fdb->is_static)
472 else if (has_expired(fdb->dst->br, fdb))
475 return NUD_REACHABLE;
478 static int fdb_fill_info(struct sk_buff *skb, const struct net_bridge *br,
479 const struct net_bridge_fdb_entry *fdb,
480 u32 portid, u32 seq, int type, unsigned int flags)
482 unsigned long now = jiffies;
483 struct nda_cacheinfo ci;
484 struct nlmsghdr *nlh;
487 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
491 ndm = nlmsg_data(nlh);
492 ndm->ndm_family = AF_BRIDGE;
497 ndm->ndm_ifindex = fdb->dst ? fdb->dst->dev->ifindex : br->dev->ifindex;
498 ndm->ndm_state = fdb_to_nud(fdb);
500 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->addr))
501 goto nla_put_failure;
502 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
503 ci.ndm_confirmed = 0;
504 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
506 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
507 goto nla_put_failure;
508 return nlmsg_end(skb, nlh);
511 nlmsg_cancel(skb, nlh);
515 static inline size_t fdb_nlmsg_size(void)
517 return NLMSG_ALIGN(sizeof(struct ndmsg))
518 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
519 + nla_total_size(sizeof(struct nda_cacheinfo));
522 static void fdb_notify(struct net_bridge *br,
523 const struct net_bridge_fdb_entry *fdb, int type)
525 struct net *net = dev_net(br->dev);
529 skb = nlmsg_new(fdb_nlmsg_size(), GFP_ATOMIC);
533 err = fdb_fill_info(skb, br, fdb, 0, 0, type, 0);
535 /* -EMSGSIZE implies BUG in fdb_nlmsg_size() */
536 WARN_ON(err == -EMSGSIZE);
540 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
544 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
547 /* Dump information about entries, in response to GETNEIGH */
548 int br_fdb_dump(struct sk_buff *skb,
549 struct netlink_callback *cb,
550 struct net_device *dev,
553 struct net_bridge *br = netdev_priv(dev);
556 if (!(dev->priv_flags & IFF_EBRIDGE))
559 for (i = 0; i < BR_HASH_SIZE; i++) {
560 struct hlist_node *h;
561 struct net_bridge_fdb_entry *f;
563 hlist_for_each_entry_rcu(f, h, &br->hash[i], hlist) {
564 if (idx < cb->args[0])
567 if (fdb_fill_info(skb, br, f,
568 NETLINK_CB(cb->skb).portid,
582 /* Update (create or replace) forwarding database entry */
583 static int fdb_add_entry(struct net_bridge_port *source, const __u8 *addr,
584 __u16 state, __u16 flags, __u16 vid)
586 struct net_bridge *br = source->br;
587 struct hlist_head *head = &br->hash[br_mac_hash(addr, vid)];
588 struct net_bridge_fdb_entry *fdb;
590 fdb = fdb_find(head, addr, vid);
592 if (!(flags & NLM_F_CREATE))
595 fdb = fdb_create(head, source, addr, vid);
598 fdb_notify(br, fdb, RTM_NEWNEIGH);
600 if (flags & NLM_F_EXCL)
604 if (fdb_to_nud(fdb) != state) {
605 if (state & NUD_PERMANENT)
606 fdb->is_local = fdb->is_static = 1;
607 else if (state & NUD_NOARP) {
611 fdb->is_local = fdb->is_static = 0;
613 fdb->updated = fdb->used = jiffies;
614 fdb_notify(br, fdb, RTM_NEWNEIGH);
620 /* Add new permanent fdb entry with RTM_NEWNEIGH */
621 int br_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
622 struct net_device *dev,
623 const unsigned char *addr, u16 nlh_flags)
625 struct net_bridge_port *p;
628 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE))) {
629 pr_info("bridge: RTM_NEWNEIGH with invalid state %#x\n", ndm->ndm_state);
633 p = br_port_get_rtnl(dev);
635 pr_info("bridge: RTM_NEWNEIGH %s not a bridge port\n",
640 if (ndm->ndm_flags & NTF_USE) {
642 br_fdb_update(p->br, p, addr, 0);
645 spin_lock_bh(&p->br->hash_lock);
646 err = fdb_add_entry(p, addr, ndm->ndm_state, nlh_flags,
648 spin_unlock_bh(&p->br->hash_lock);
654 static int fdb_delete_by_addr(struct net_bridge_port *p, const u8 *addr)
656 struct net_bridge *br = p->br;
657 struct hlist_head *head = &br->hash[br_mac_hash(addr, 0)];
658 struct net_bridge_fdb_entry *fdb;
660 fdb = fdb_find(head, addr, 0);
664 fdb_delete(p->br, fdb);
668 /* Remove neighbor entry with RTM_DELNEIGH */
669 int br_fdb_delete(struct ndmsg *ndm, struct net_device *dev,
670 const unsigned char *addr)
672 struct net_bridge_port *p;
675 p = br_port_get_rtnl(dev);
677 pr_info("bridge: RTM_DELNEIGH %s not a bridge port\n",
682 spin_lock_bh(&p->br->hash_lock);
683 err = fdb_delete_by_addr(p, addr);
684 spin_unlock_bh(&p->br->hash_lock);