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 "br_private.h"
28 static struct kmem_cache
*br_fdb_cache __read_mostly
;
29 static int fdb_insert(struct net_bridge
*br
, struct net_bridge_port
*source
,
30 const unsigned char *addr
);
31 static void fdb_notify(struct net_bridge
*br
,
32 const struct net_bridge_fdb_entry
*, int);
34 static u32 fdb_salt __read_mostly
;
36 int __init
br_fdb_init(void)
38 br_fdb_cache
= kmem_cache_create("bridge_fdb_cache",
39 sizeof(struct net_bridge_fdb_entry
),
41 SLAB_HWCACHE_ALIGN
, NULL
);
45 get_random_bytes(&fdb_salt
, sizeof(fdb_salt
));
49 void br_fdb_fini(void)
51 kmem_cache_destroy(br_fdb_cache
);
55 /* if topology_changing then use forward_delay (default 15 sec)
56 * otherwise keep longer (default 5 minutes)
58 static inline unsigned long hold_time(const struct net_bridge
*br
)
60 return br
->topology_change
? br
->forward_delay
: br
->ageing_time
;
63 static inline int has_expired(const struct net_bridge
*br
,
64 const struct net_bridge_fdb_entry
*fdb
)
66 return !fdb
->is_static
&&
67 time_before_eq(fdb
->updated
+ hold_time(br
), jiffies
);
70 static inline int br_mac_hash(const unsigned char *mac
)
72 /* use 1 byte of OUI cnd 3 bytes of NIC */
73 u32 key
= get_unaligned((u32
*)(mac
+ 2));
74 return jhash_1word(key
, fdb_salt
) & (BR_HASH_SIZE
- 1);
77 static void fdb_rcu_free(struct rcu_head
*head
)
79 struct net_bridge_fdb_entry
*ent
80 = container_of(head
, struct net_bridge_fdb_entry
, rcu
);
81 kmem_cache_free(br_fdb_cache
, ent
);
84 static void fdb_delete(struct net_bridge
*br
, struct net_bridge_fdb_entry
*f
)
86 hlist_del_rcu(&f
->hlist
);
87 fdb_notify(br
, f
, RTM_DELNEIGH
);
88 call_rcu(&f
->rcu
, fdb_rcu_free
);
91 void br_fdb_changeaddr(struct net_bridge_port
*p
, const unsigned char *newaddr
)
93 struct net_bridge
*br
= p
->br
;
96 spin_lock_bh(&br
->hash_lock
);
98 /* Search all chains since old address/hash is unknown */
99 for (i
= 0; i
< BR_HASH_SIZE
; i
++) {
100 struct hlist_node
*h
;
101 hlist_for_each(h
, &br
->hash
[i
]) {
102 struct net_bridge_fdb_entry
*f
;
104 f
= hlist_entry(h
, struct net_bridge_fdb_entry
, hlist
);
105 if (f
->dst
== p
&& f
->is_local
) {
106 /* maybe another port has same hw addr? */
107 struct net_bridge_port
*op
;
108 list_for_each_entry(op
, &br
->port_list
, list
) {
110 ether_addr_equal(op
->dev
->dev_addr
,
124 /* insert new address, may fail if invalid address or dup. */
125 fdb_insert(br
, p
, newaddr
);
127 spin_unlock_bh(&br
->hash_lock
);
130 void br_fdb_change_mac_address(struct net_bridge
*br
, const u8
*newaddr
)
132 struct net_bridge_fdb_entry
*f
;
134 /* If old entry was unassociated with any port, then delete it. */
135 f
= __br_fdb_get(br
, br
->dev
->dev_addr
);
136 if (f
&& f
->is_local
&& !f
->dst
)
139 fdb_insert(br
, NULL
, newaddr
);
142 void br_fdb_cleanup(unsigned long _data
)
144 struct net_bridge
*br
= (struct net_bridge
*)_data
;
145 unsigned long delay
= hold_time(br
);
146 unsigned long next_timer
= jiffies
+ br
->ageing_time
;
149 spin_lock(&br
->hash_lock
);
150 for (i
= 0; i
< BR_HASH_SIZE
; i
++) {
151 struct net_bridge_fdb_entry
*f
;
152 struct hlist_node
*h
, *n
;
154 hlist_for_each_entry_safe(f
, h
, n
, &br
->hash
[i
], hlist
) {
155 unsigned long this_timer
;
158 this_timer
= f
->updated
+ delay
;
159 if (time_before_eq(this_timer
, jiffies
))
161 else if (time_before(this_timer
, next_timer
))
162 next_timer
= this_timer
;
165 spin_unlock(&br
->hash_lock
);
167 mod_timer(&br
->gc_timer
, round_jiffies_up(next_timer
));
170 /* Completely flush all dynamic entries in forwarding database.*/
171 void br_fdb_flush(struct net_bridge
*br
)
175 spin_lock_bh(&br
->hash_lock
);
176 for (i
= 0; i
< BR_HASH_SIZE
; i
++) {
177 struct net_bridge_fdb_entry
*f
;
178 struct hlist_node
*h
, *n
;
179 hlist_for_each_entry_safe(f
, h
, n
, &br
->hash
[i
], hlist
) {
184 spin_unlock_bh(&br
->hash_lock
);
187 /* Flush all entries referring to a specific port.
188 * if do_all is set also flush static entries
190 void br_fdb_delete_by_port(struct net_bridge
*br
,
191 const struct net_bridge_port
*p
,
196 spin_lock_bh(&br
->hash_lock
);
197 for (i
= 0; i
< BR_HASH_SIZE
; i
++) {
198 struct hlist_node
*h
, *g
;
200 hlist_for_each_safe(h
, g
, &br
->hash
[i
]) {
201 struct net_bridge_fdb_entry
*f
202 = hlist_entry(h
, struct net_bridge_fdb_entry
, hlist
);
206 if (f
->is_static
&& !do_all
)
209 * if multiple ports all have the same device address
210 * then when one port is deleted, assign
211 * the local entry to other port
214 struct net_bridge_port
*op
;
215 list_for_each_entry(op
, &br
->port_list
, list
) {
217 ether_addr_equal(op
->dev
->dev_addr
,
229 spin_unlock_bh(&br
->hash_lock
);
232 /* No locking or refcounting, assumes caller has rcu_read_lock */
233 struct net_bridge_fdb_entry
*__br_fdb_get(struct net_bridge
*br
,
234 const unsigned char *addr
)
236 struct hlist_node
*h
;
237 struct net_bridge_fdb_entry
*fdb
;
239 hlist_for_each_entry_rcu(fdb
, h
, &br
->hash
[br_mac_hash(addr
)], hlist
) {
240 if (ether_addr_equal(fdb
->addr
.addr
, addr
)) {
241 if (unlikely(has_expired(br
, fdb
)))
250 #if IS_ENABLED(CONFIG_ATM_LANE)
251 /* Interface used by ATM LANE hook to test
252 * if an addr is on some other bridge port */
253 int br_fdb_test_addr(struct net_device
*dev
, unsigned char *addr
)
255 struct net_bridge_fdb_entry
*fdb
;
256 struct net_bridge_port
*port
;
260 port
= br_port_get_rcu(dev
);
264 fdb
= __br_fdb_get(port
->br
, addr
);
265 ret
= fdb
&& fdb
->dst
&& fdb
->dst
->dev
!= dev
&&
266 fdb
->dst
->state
== BR_STATE_FORWARDING
;
272 #endif /* CONFIG_ATM_LANE */
275 * Fill buffer with forwarding table records in
278 int br_fdb_fillbuf(struct net_bridge
*br
, void *buf
,
279 unsigned long maxnum
, unsigned long skip
)
281 struct __fdb_entry
*fe
= buf
;
283 struct hlist_node
*h
;
284 struct net_bridge_fdb_entry
*f
;
286 memset(buf
, 0, maxnum
*sizeof(struct __fdb_entry
));
289 for (i
= 0; i
< BR_HASH_SIZE
; i
++) {
290 hlist_for_each_entry_rcu(f
, h
, &br
->hash
[i
], hlist
) {
294 if (has_expired(br
, f
))
297 /* ignore pseudo entry for local MAC address */
306 /* convert from internal format to API */
307 memcpy(fe
->mac_addr
, f
->addr
.addr
, ETH_ALEN
);
309 /* due to ABI compat need to split into hi/lo */
310 fe
->port_no
= f
->dst
->port_no
;
311 fe
->port_hi
= f
->dst
->port_no
>> 8;
313 fe
->is_local
= f
->is_local
;
315 fe
->ageing_timer_value
= jiffies_to_clock_t(jiffies
- f
->updated
);
327 static struct net_bridge_fdb_entry
*fdb_find(struct hlist_head
*head
,
328 const unsigned char *addr
)
330 struct hlist_node
*h
;
331 struct net_bridge_fdb_entry
*fdb
;
333 hlist_for_each_entry(fdb
, h
, head
, hlist
) {
334 if (ether_addr_equal(fdb
->addr
.addr
, addr
))
340 static struct net_bridge_fdb_entry
*fdb_find_rcu(struct hlist_head
*head
,
341 const unsigned char *addr
)
343 struct hlist_node
*h
;
344 struct net_bridge_fdb_entry
*fdb
;
346 hlist_for_each_entry_rcu(fdb
, h
, head
, hlist
) {
347 if (ether_addr_equal(fdb
->addr
.addr
, addr
))
353 static struct net_bridge_fdb_entry
*fdb_create(struct hlist_head
*head
,
354 struct net_bridge_port
*source
,
355 const unsigned char *addr
)
357 struct net_bridge_fdb_entry
*fdb
;
359 fdb
= kmem_cache_alloc(br_fdb_cache
, GFP_ATOMIC
);
361 memcpy(fdb
->addr
.addr
, addr
, ETH_ALEN
);
365 fdb
->updated
= fdb
->used
= jiffies
;
366 hlist_add_head_rcu(&fdb
->hlist
, head
);
371 static int fdb_insert(struct net_bridge
*br
, struct net_bridge_port
*source
,
372 const unsigned char *addr
)
374 struct hlist_head
*head
= &br
->hash
[br_mac_hash(addr
)];
375 struct net_bridge_fdb_entry
*fdb
;
377 if (!is_valid_ether_addr(addr
))
380 fdb
= fdb_find(head
, addr
);
382 /* it is okay to have multiple ports with same
383 * address, just use the first one.
387 br_warn(br
, "adding interface %s with same address "
388 "as a received packet\n",
393 fdb
= fdb_create(head
, source
, addr
);
397 fdb
->is_local
= fdb
->is_static
= 1;
398 fdb_notify(br
, fdb
, RTM_NEWNEIGH
);
402 /* Add entry for local address of interface */
403 int br_fdb_insert(struct net_bridge
*br
, struct net_bridge_port
*source
,
404 const unsigned char *addr
)
408 spin_lock_bh(&br
->hash_lock
);
409 ret
= fdb_insert(br
, source
, addr
);
410 spin_unlock_bh(&br
->hash_lock
);
414 void br_fdb_update(struct net_bridge
*br
, struct net_bridge_port
*source
,
415 const unsigned char *addr
)
417 struct hlist_head
*head
= &br
->hash
[br_mac_hash(addr
)];
418 struct net_bridge_fdb_entry
*fdb
;
420 /* some users want to always flood. */
421 if (hold_time(br
) == 0)
424 /* ignore packets unless we are using this port */
425 if (!(source
->state
== BR_STATE_LEARNING
||
426 source
->state
== BR_STATE_FORWARDING
))
429 fdb
= fdb_find_rcu(head
, addr
);
431 /* attempt to update an entry for a local interface */
432 if (unlikely(fdb
->is_local
)) {
434 br_warn(br
, "received packet on %s with "
435 "own address as source address\n",
438 /* fastpath: update of existing entry */
440 fdb
->updated
= jiffies
;
443 spin_lock(&br
->hash_lock
);
444 if (likely(!fdb_find(head
, addr
))) {
445 fdb
= fdb_create(head
, source
, addr
);
447 fdb_notify(br
, fdb
, RTM_NEWNEIGH
);
449 /* else we lose race and someone else inserts
450 * it first, don't bother updating
452 spin_unlock(&br
->hash_lock
);
456 static int fdb_to_nud(const struct net_bridge_fdb_entry
*fdb
)
459 return NUD_PERMANENT
;
460 else if (fdb
->is_static
)
462 else if (has_expired(fdb
->dst
->br
, fdb
))
465 return NUD_REACHABLE
;
468 static int fdb_fill_info(struct sk_buff
*skb
, const struct net_bridge
*br
,
469 const struct net_bridge_fdb_entry
*fdb
,
470 u32 pid
, u32 seq
, int type
, unsigned int flags
)
472 unsigned long now
= jiffies
;
473 struct nda_cacheinfo ci
;
474 struct nlmsghdr
*nlh
;
477 nlh
= nlmsg_put(skb
, pid
, seq
, type
, sizeof(*ndm
), flags
);
481 ndm
= nlmsg_data(nlh
);
482 ndm
->ndm_family
= AF_BRIDGE
;
487 ndm
->ndm_ifindex
= fdb
->dst
? fdb
->dst
->dev
->ifindex
: br
->dev
->ifindex
;
488 ndm
->ndm_state
= fdb_to_nud(fdb
);
490 if (nla_put(skb
, NDA_LLADDR
, ETH_ALEN
, &fdb
->addr
))
491 goto nla_put_failure
;
492 ci
.ndm_used
= jiffies_to_clock_t(now
- fdb
->used
);
493 ci
.ndm_confirmed
= 0;
494 ci
.ndm_updated
= jiffies_to_clock_t(now
- fdb
->updated
);
496 if (nla_put(skb
, NDA_CACHEINFO
, sizeof(ci
), &ci
))
497 goto nla_put_failure
;
498 return nlmsg_end(skb
, nlh
);
501 nlmsg_cancel(skb
, nlh
);
505 static inline size_t fdb_nlmsg_size(void)
507 return NLMSG_ALIGN(sizeof(struct ndmsg
))
508 + nla_total_size(ETH_ALEN
) /* NDA_LLADDR */
509 + nla_total_size(sizeof(struct nda_cacheinfo
));
512 static void fdb_notify(struct net_bridge
*br
,
513 const struct net_bridge_fdb_entry
*fdb
, int type
)
515 struct net
*net
= dev_net(br
->dev
);
519 skb
= nlmsg_new(fdb_nlmsg_size(), GFP_ATOMIC
);
523 err
= fdb_fill_info(skb
, br
, fdb
, 0, 0, type
, 0);
525 /* -EMSGSIZE implies BUG in fdb_nlmsg_size() */
526 WARN_ON(err
== -EMSGSIZE
);
530 rtnl_notify(skb
, net
, 0, RTNLGRP_NEIGH
, NULL
, GFP_ATOMIC
);
534 rtnl_set_sk_err(net
, RTNLGRP_NEIGH
, err
);
537 /* Dump information about entries, in response to GETNEIGH */
538 int br_fdb_dump(struct sk_buff
*skb
,
539 struct netlink_callback
*cb
,
540 struct net_device
*dev
,
543 struct net_bridge
*br
= netdev_priv(dev
);
546 if (!(dev
->priv_flags
& IFF_EBRIDGE
))
549 for (i
= 0; i
< BR_HASH_SIZE
; i
++) {
550 struct hlist_node
*h
;
551 struct net_bridge_fdb_entry
*f
;
553 hlist_for_each_entry_rcu(f
, h
, &br
->hash
[i
], hlist
) {
554 if (idx
< cb
->args
[0])
557 if (fdb_fill_info(skb
, br
, f
,
558 NETLINK_CB(cb
->skb
).pid
,
572 /* Update (create or replace) forwarding database entry */
573 static int fdb_add_entry(struct net_bridge_port
*source
, const __u8
*addr
,
574 __u16 state
, __u16 flags
)
576 struct net_bridge
*br
= source
->br
;
577 struct hlist_head
*head
= &br
->hash
[br_mac_hash(addr
)];
578 struct net_bridge_fdb_entry
*fdb
;
580 fdb
= fdb_find(head
, addr
);
582 if (!(flags
& NLM_F_CREATE
))
585 fdb
= fdb_create(head
, source
, addr
);
588 fdb_notify(br
, fdb
, RTM_NEWNEIGH
);
590 if (flags
& NLM_F_EXCL
)
594 if (fdb_to_nud(fdb
) != state
) {
595 if (state
& NUD_PERMANENT
)
596 fdb
->is_local
= fdb
->is_static
= 1;
597 else if (state
& NUD_NOARP
) {
601 fdb
->is_local
= fdb
->is_static
= 0;
603 fdb
->updated
= fdb
->used
= jiffies
;
604 fdb_notify(br
, fdb
, RTM_NEWNEIGH
);
610 /* Add new permanent fdb entry with RTM_NEWNEIGH */
611 int br_fdb_add(struct ndmsg
*ndm
, struct net_device
*dev
,
612 unsigned char *addr
, u16 nlh_flags
)
614 struct net_bridge_port
*p
;
617 if (!(ndm
->ndm_state
& (NUD_PERMANENT
|NUD_NOARP
|NUD_REACHABLE
))) {
618 pr_info("bridge: RTM_NEWNEIGH with invalid state %#x\n", ndm
->ndm_state
);
622 p
= br_port_get_rtnl(dev
);
624 pr_info("bridge: RTM_NEWNEIGH %s not a bridge port\n",
629 if (ndm
->ndm_flags
& NTF_USE
) {
631 br_fdb_update(p
->br
, p
, addr
);
634 spin_lock_bh(&p
->br
->hash_lock
);
635 err
= fdb_add_entry(p
, addr
, ndm
->ndm_state
, nlh_flags
);
636 spin_unlock_bh(&p
->br
->hash_lock
);
642 static int fdb_delete_by_addr(struct net_bridge_port
*p
, u8
*addr
)
644 struct net_bridge
*br
= p
->br
;
645 struct hlist_head
*head
= &br
->hash
[br_mac_hash(addr
)];
646 struct net_bridge_fdb_entry
*fdb
;
648 fdb
= fdb_find(head
, addr
);
652 fdb_delete(p
->br
, fdb
);
656 /* Remove neighbor entry with RTM_DELNEIGH */
657 int br_fdb_delete(struct ndmsg
*ndm
, struct net_device
*dev
,
660 struct net_bridge_port
*p
;
663 p
= br_port_get_rtnl(dev
);
665 pr_info("bridge: RTM_DELNEIGH %s not a bridge port\n",
670 spin_lock_bh(&p
->br
->hash_lock
);
671 err
= fdb_delete_by_addr(p
, addr
);
672 spin_unlock_bh(&p
->br
->hash_lock
);