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 (!vid
|| nbp_vlan_find(op
, vid
))) {
123 /* maybe bridge device has same hw addr? */
124 if (ether_addr_equal(br
->dev
->dev_addr
,
126 (!vid
|| br_vlan_find(br
, vid
))) {
134 /* if this port has no vlan information
135 * configured, we can safely be done at
145 /* insert new address, may fail if invalid address or dup. */
146 fdb_insert(br
, p
, newaddr
, 0);
151 /* Now add entries for every VLAN configured on the port.
152 * This function runs under RTNL so the bitmap will not change
155 for_each_set_bit(vid
, pv
->vlan_bitmap
, VLAN_N_VID
)
156 fdb_insert(br
, p
, newaddr
, vid
);
159 spin_unlock_bh(&br
->hash_lock
);
162 void br_fdb_change_mac_address(struct net_bridge
*br
, const u8
*newaddr
)
164 struct net_bridge_fdb_entry
*f
;
165 struct net_port_vlans
*pv
;
168 /* If old entry was unassociated with any port, then delete it. */
169 f
= __br_fdb_get(br
, br
->dev
->dev_addr
, 0);
170 if (f
&& f
->is_local
&& !f
->dst
)
173 fdb_insert(br
, NULL
, newaddr
, 0);
175 /* Now remove and add entries for every VLAN configured on the
176 * bridge. This function runs under RTNL so the bitmap will not
177 * change from under us.
179 pv
= br_get_vlan_info(br
);
183 for_each_set_bit_from(vid
, pv
->vlan_bitmap
, VLAN_N_VID
) {
184 f
= __br_fdb_get(br
, br
->dev
->dev_addr
, vid
);
185 if (f
&& f
->is_local
&& !f
->dst
)
187 fdb_insert(br
, NULL
, newaddr
, vid
);
191 void br_fdb_cleanup(unsigned long _data
)
193 struct net_bridge
*br
= (struct net_bridge
*)_data
;
194 unsigned long delay
= hold_time(br
);
195 unsigned long next_timer
= jiffies
+ br
->ageing_time
;
198 spin_lock(&br
->hash_lock
);
199 for (i
= 0; i
< BR_HASH_SIZE
; i
++) {
200 struct net_bridge_fdb_entry
*f
;
201 struct hlist_node
*n
;
203 hlist_for_each_entry_safe(f
, n
, &br
->hash
[i
], hlist
) {
204 unsigned long this_timer
;
207 this_timer
= f
->updated
+ delay
;
208 if (time_before_eq(this_timer
, jiffies
))
210 else if (time_before(this_timer
, next_timer
))
211 next_timer
= this_timer
;
214 spin_unlock(&br
->hash_lock
);
216 mod_timer(&br
->gc_timer
, round_jiffies_up(next_timer
));
219 /* Completely flush all dynamic entries in forwarding database.*/
220 void br_fdb_flush(struct net_bridge
*br
)
224 spin_lock_bh(&br
->hash_lock
);
225 for (i
= 0; i
< BR_HASH_SIZE
; i
++) {
226 struct net_bridge_fdb_entry
*f
;
227 struct hlist_node
*n
;
228 hlist_for_each_entry_safe(f
, n
, &br
->hash
[i
], hlist
) {
233 spin_unlock_bh(&br
->hash_lock
);
236 /* Flush all entries referring to a specific port.
237 * if do_all is set also flush static entries
239 void br_fdb_delete_by_port(struct net_bridge
*br
,
240 const struct net_bridge_port
*p
,
245 spin_lock_bh(&br
->hash_lock
);
246 for (i
= 0; i
< BR_HASH_SIZE
; i
++) {
247 struct hlist_node
*h
, *g
;
249 hlist_for_each_safe(h
, g
, &br
->hash
[i
]) {
250 struct net_bridge_fdb_entry
*f
251 = hlist_entry(h
, struct net_bridge_fdb_entry
, hlist
);
255 if (f
->is_static
&& !do_all
)
258 * if multiple ports all have the same device address
259 * then when one port is deleted, assign
260 * the local entry to other port
263 struct net_bridge_port
*op
;
264 list_for_each_entry(op
, &br
->port_list
, list
) {
266 ether_addr_equal(op
->dev
->dev_addr
,
269 f
->added_by_user
= 0;
279 spin_unlock_bh(&br
->hash_lock
);
282 /* No locking or refcounting, assumes caller has rcu_read_lock */
283 struct net_bridge_fdb_entry
*__br_fdb_get(struct net_bridge
*br
,
284 const unsigned char *addr
,
287 struct net_bridge_fdb_entry
*fdb
;
289 hlist_for_each_entry_rcu(fdb
,
290 &br
->hash
[br_mac_hash(addr
, vid
)], hlist
) {
291 if (ether_addr_equal(fdb
->addr
.addr
, addr
) &&
292 fdb
->vlan_id
== vid
) {
293 if (unlikely(has_expired(br
, fdb
)))
302 #if IS_ENABLED(CONFIG_ATM_LANE)
303 /* Interface used by ATM LANE hook to test
304 * if an addr is on some other bridge port */
305 int br_fdb_test_addr(struct net_device
*dev
, unsigned char *addr
)
307 struct net_bridge_fdb_entry
*fdb
;
308 struct net_bridge_port
*port
;
312 port
= br_port_get_rcu(dev
);
316 fdb
= __br_fdb_get(port
->br
, addr
, 0);
317 ret
= fdb
&& fdb
->dst
&& fdb
->dst
->dev
!= dev
&&
318 fdb
->dst
->state
== BR_STATE_FORWARDING
;
324 #endif /* CONFIG_ATM_LANE */
327 * Fill buffer with forwarding table records in
330 int br_fdb_fillbuf(struct net_bridge
*br
, void *buf
,
331 unsigned long maxnum
, unsigned long skip
)
333 struct __fdb_entry
*fe
= buf
;
335 struct net_bridge_fdb_entry
*f
;
337 memset(buf
, 0, maxnum
*sizeof(struct __fdb_entry
));
340 for (i
= 0; i
< BR_HASH_SIZE
; i
++) {
341 hlist_for_each_entry_rcu(f
, &br
->hash
[i
], hlist
) {
345 if (has_expired(br
, f
))
348 /* ignore pseudo entry for local MAC address */
357 /* convert from internal format to API */
358 memcpy(fe
->mac_addr
, f
->addr
.addr
, ETH_ALEN
);
360 /* due to ABI compat need to split into hi/lo */
361 fe
->port_no
= f
->dst
->port_no
;
362 fe
->port_hi
= f
->dst
->port_no
>> 8;
364 fe
->is_local
= f
->is_local
;
366 fe
->ageing_timer_value
= jiffies_delta_to_clock_t(jiffies
- f
->updated
);
378 static struct net_bridge_fdb_entry
*fdb_find(struct hlist_head
*head
,
379 const unsigned char *addr
,
382 struct net_bridge_fdb_entry
*fdb
;
384 hlist_for_each_entry(fdb
, head
, hlist
) {
385 if (ether_addr_equal(fdb
->addr
.addr
, addr
) &&
392 static struct net_bridge_fdb_entry
*fdb_find_rcu(struct hlist_head
*head
,
393 const unsigned char *addr
,
396 struct net_bridge_fdb_entry
*fdb
;
398 hlist_for_each_entry_rcu(fdb
, head
, hlist
) {
399 if (ether_addr_equal(fdb
->addr
.addr
, addr
) &&
406 static struct net_bridge_fdb_entry
*fdb_create(struct hlist_head
*head
,
407 struct net_bridge_port
*source
,
408 const unsigned char *addr
,
411 struct net_bridge_fdb_entry
*fdb
;
413 fdb
= kmem_cache_alloc(br_fdb_cache
, GFP_ATOMIC
);
415 memcpy(fdb
->addr
.addr
, addr
, ETH_ALEN
);
420 fdb
->added_by_user
= 0;
421 fdb
->updated
= fdb
->used
= jiffies
;
422 hlist_add_head_rcu(&fdb
->hlist
, head
);
427 static int fdb_insert(struct net_bridge
*br
, struct net_bridge_port
*source
,
428 const unsigned char *addr
, u16 vid
)
430 struct hlist_head
*head
= &br
->hash
[br_mac_hash(addr
, vid
)];
431 struct net_bridge_fdb_entry
*fdb
;
433 if (!is_valid_ether_addr(addr
))
436 fdb
= fdb_find(head
, addr
, vid
);
438 /* it is okay to have multiple ports with same
439 * address, just use the first one.
443 br_warn(br
, "adding interface %s with same address "
444 "as a received packet\n",
445 source
? source
->dev
->name
: br
->dev
->name
);
449 fdb
= fdb_create(head
, source
, addr
, vid
);
453 fdb
->is_local
= fdb
->is_static
= 1;
454 fdb_notify(br
, fdb
, RTM_NEWNEIGH
);
458 /* Add entry for local address of interface */
459 int br_fdb_insert(struct net_bridge
*br
, struct net_bridge_port
*source
,
460 const unsigned char *addr
, u16 vid
)
464 spin_lock_bh(&br
->hash_lock
);
465 ret
= fdb_insert(br
, source
, addr
, vid
);
466 spin_unlock_bh(&br
->hash_lock
);
470 void br_fdb_update(struct net_bridge
*br
, struct net_bridge_port
*source
,
471 const unsigned char *addr
, u16 vid
, bool added_by_user
)
473 struct hlist_head
*head
= &br
->hash
[br_mac_hash(addr
, vid
)];
474 struct net_bridge_fdb_entry
*fdb
;
476 /* some users want to always flood. */
477 if (hold_time(br
) == 0)
480 /* ignore packets unless we are using this port */
481 if (!(source
->state
== BR_STATE_LEARNING
||
482 source
->state
== BR_STATE_FORWARDING
))
485 fdb
= fdb_find_rcu(head
, addr
, vid
);
487 /* attempt to update an entry for a local interface */
488 if (unlikely(fdb
->is_local
)) {
490 br_warn(br
, "received packet on %s with "
491 "own address as source address\n",
494 /* fastpath: update of existing entry */
496 fdb
->updated
= jiffies
;
497 if (unlikely(added_by_user
))
498 fdb
->added_by_user
= 1;
501 spin_lock(&br
->hash_lock
);
502 if (likely(!fdb_find(head
, addr
, vid
))) {
503 fdb
= fdb_create(head
, source
, addr
, vid
);
505 if (unlikely(added_by_user
))
506 fdb
->added_by_user
= 1;
507 fdb_notify(br
, fdb
, RTM_NEWNEIGH
);
510 /* else we lose race and someone else inserts
511 * it first, don't bother updating
513 spin_unlock(&br
->hash_lock
);
517 static int fdb_to_nud(const struct net_bridge_fdb_entry
*fdb
)
520 return NUD_PERMANENT
;
521 else if (fdb
->is_static
)
523 else if (has_expired(fdb
->dst
->br
, fdb
))
526 return NUD_REACHABLE
;
529 static int fdb_fill_info(struct sk_buff
*skb
, const struct net_bridge
*br
,
530 const struct net_bridge_fdb_entry
*fdb
,
531 u32 portid
, u32 seq
, int type
, unsigned int flags
)
533 unsigned long now
= jiffies
;
534 struct nda_cacheinfo ci
;
535 struct nlmsghdr
*nlh
;
538 nlh
= nlmsg_put(skb
, portid
, seq
, type
, sizeof(*ndm
), flags
);
542 ndm
= nlmsg_data(nlh
);
543 ndm
->ndm_family
= AF_BRIDGE
;
548 ndm
->ndm_ifindex
= fdb
->dst
? fdb
->dst
->dev
->ifindex
: br
->dev
->ifindex
;
549 ndm
->ndm_state
= fdb_to_nud(fdb
);
551 if (nla_put(skb
, NDA_LLADDR
, ETH_ALEN
, &fdb
->addr
))
552 goto nla_put_failure
;
553 ci
.ndm_used
= jiffies_to_clock_t(now
- fdb
->used
);
554 ci
.ndm_confirmed
= 0;
555 ci
.ndm_updated
= jiffies_to_clock_t(now
- fdb
->updated
);
557 if (nla_put(skb
, NDA_CACHEINFO
, sizeof(ci
), &ci
))
558 goto nla_put_failure
;
560 if (nla_put(skb
, NDA_VLAN
, sizeof(u16
), &fdb
->vlan_id
))
561 goto nla_put_failure
;
563 return nlmsg_end(skb
, nlh
);
566 nlmsg_cancel(skb
, nlh
);
570 static inline size_t fdb_nlmsg_size(void)
572 return NLMSG_ALIGN(sizeof(struct ndmsg
))
573 + nla_total_size(ETH_ALEN
) /* NDA_LLADDR */
574 + nla_total_size(sizeof(u16
)) /* NDA_VLAN */
575 + nla_total_size(sizeof(struct nda_cacheinfo
));
578 static void fdb_notify(struct net_bridge
*br
,
579 const struct net_bridge_fdb_entry
*fdb
, int type
)
581 struct net
*net
= dev_net(br
->dev
);
585 skb
= nlmsg_new(fdb_nlmsg_size(), GFP_ATOMIC
);
589 err
= fdb_fill_info(skb
, br
, fdb
, 0, 0, type
, 0);
591 /* -EMSGSIZE implies BUG in fdb_nlmsg_size() */
592 WARN_ON(err
== -EMSGSIZE
);
596 rtnl_notify(skb
, net
, 0, RTNLGRP_NEIGH
, NULL
, GFP_ATOMIC
);
599 rtnl_set_sk_err(net
, RTNLGRP_NEIGH
, err
);
602 /* Dump information about entries, in response to GETNEIGH */
603 int br_fdb_dump(struct sk_buff
*skb
,
604 struct netlink_callback
*cb
,
605 struct net_device
*dev
,
608 struct net_bridge
*br
= netdev_priv(dev
);
611 if (!(dev
->priv_flags
& IFF_EBRIDGE
))
614 for (i
= 0; i
< BR_HASH_SIZE
; i
++) {
615 struct net_bridge_fdb_entry
*f
;
617 hlist_for_each_entry_rcu(f
, &br
->hash
[i
], hlist
) {
618 if (idx
< cb
->args
[0])
621 if (fdb_fill_info(skb
, br
, f
,
622 NETLINK_CB(cb
->skb
).portid
,
636 /* Update (create or replace) forwarding database entry */
637 static int fdb_add_entry(struct net_bridge_port
*source
, const __u8
*addr
,
638 __u16 state
, __u16 flags
, __u16 vid
)
640 struct net_bridge
*br
= source
->br
;
641 struct hlist_head
*head
= &br
->hash
[br_mac_hash(addr
, vid
)];
642 struct net_bridge_fdb_entry
*fdb
;
643 bool modified
= false;
645 fdb
= fdb_find(head
, addr
, vid
);
647 if (!(flags
& NLM_F_CREATE
))
650 fdb
= fdb_create(head
, source
, addr
, vid
);
656 if (flags
& NLM_F_EXCL
)
659 if (fdb
->dst
!= source
) {
665 if (fdb_to_nud(fdb
) != state
) {
666 if (state
& NUD_PERMANENT
)
667 fdb
->is_local
= fdb
->is_static
= 1;
668 else if (state
& NUD_NOARP
) {
672 fdb
->is_local
= fdb
->is_static
= 0;
676 fdb
->added_by_user
= 1;
680 fdb
->updated
= jiffies
;
681 fdb_notify(br
, fdb
, RTM_NEWNEIGH
);
687 static int __br_fdb_add(struct ndmsg
*ndm
, struct net_bridge_port
*p
,
688 const unsigned char *addr
, u16 nlh_flags
, u16 vid
)
692 if (ndm
->ndm_flags
& NTF_USE
) {
694 br_fdb_update(p
->br
, p
, addr
, vid
, true);
697 spin_lock_bh(&p
->br
->hash_lock
);
698 err
= fdb_add_entry(p
, addr
, ndm
->ndm_state
,
700 spin_unlock_bh(&p
->br
->hash_lock
);
706 /* Add new permanent fdb entry with RTM_NEWNEIGH */
707 int br_fdb_add(struct ndmsg
*ndm
, struct nlattr
*tb
[],
708 struct net_device
*dev
,
709 const unsigned char *addr
, u16 nlh_flags
)
711 struct net_bridge_port
*p
;
713 struct net_port_vlans
*pv
;
714 unsigned short vid
= VLAN_N_VID
;
716 if (!(ndm
->ndm_state
& (NUD_PERMANENT
|NUD_NOARP
|NUD_REACHABLE
))) {
717 pr_info("bridge: RTM_NEWNEIGH with invalid state %#x\n", ndm
->ndm_state
);
722 if (nla_len(tb
[NDA_VLAN
]) != sizeof(unsigned short)) {
723 pr_info("bridge: RTM_NEWNEIGH with invalid vlan\n");
727 vid
= nla_get_u16(tb
[NDA_VLAN
]);
729 if (!vid
|| vid
>= VLAN_VID_MASK
) {
730 pr_info("bridge: RTM_NEWNEIGH with invalid vlan id %d\n",
736 if (is_zero_ether_addr(addr
)) {
737 pr_info("bridge: RTM_NEWNEIGH with invalid ether address\n");
741 p
= br_port_get_rtnl(dev
);
743 pr_info("bridge: RTM_NEWNEIGH %s not a bridge port\n",
748 pv
= nbp_get_vlan_info(p
);
749 if (vid
!= VLAN_N_VID
) {
750 if (!pv
|| !test_bit(vid
, pv
->vlan_bitmap
)) {
751 pr_info("bridge: RTM_NEWNEIGH with unconfigured "
752 "vlan %d on port %s\n", vid
, dev
->name
);
756 /* VID was specified, so use it. */
757 err
= __br_fdb_add(ndm
, p
, addr
, nlh_flags
, vid
);
759 if (!pv
|| bitmap_empty(pv
->vlan_bitmap
, VLAN_N_VID
)) {
760 err
= __br_fdb_add(ndm
, p
, addr
, nlh_flags
, 0);
764 /* We have vlans configured on this port and user didn't
765 * specify a VLAN. To be nice, add/update entry for every
768 for_each_set_bit(vid
, pv
->vlan_bitmap
, VLAN_N_VID
) {
769 err
= __br_fdb_add(ndm
, p
, addr
, nlh_flags
, vid
);
779 int fdb_delete_by_addr(struct net_bridge
*br
, const u8
*addr
,
782 struct hlist_head
*head
= &br
->hash
[br_mac_hash(addr
, vlan
)];
783 struct net_bridge_fdb_entry
*fdb
;
785 fdb
= fdb_find(head
, addr
, vlan
);
793 static int __br_fdb_delete(struct net_bridge_port
*p
,
794 const unsigned char *addr
, u16 vid
)
798 spin_lock_bh(&p
->br
->hash_lock
);
799 err
= fdb_delete_by_addr(p
->br
, addr
, vid
);
800 spin_unlock_bh(&p
->br
->hash_lock
);
805 /* Remove neighbor entry with RTM_DELNEIGH */
806 int br_fdb_delete(struct ndmsg
*ndm
, struct nlattr
*tb
[],
807 struct net_device
*dev
,
808 const unsigned char *addr
)
810 struct net_bridge_port
*p
;
812 struct net_port_vlans
*pv
;
813 unsigned short vid
= VLAN_N_VID
;
816 if (nla_len(tb
[NDA_VLAN
]) != sizeof(unsigned short)) {
817 pr_info("bridge: RTM_NEWNEIGH with invalid vlan\n");
821 vid
= nla_get_u16(tb
[NDA_VLAN
]);
823 if (!vid
|| vid
>= VLAN_VID_MASK
) {
824 pr_info("bridge: RTM_NEWNEIGH with invalid vlan id %d\n",
829 p
= br_port_get_rtnl(dev
);
831 pr_info("bridge: RTM_DELNEIGH %s not a bridge port\n",
836 pv
= nbp_get_vlan_info(p
);
837 if (vid
!= VLAN_N_VID
) {
838 if (!pv
|| !test_bit(vid
, pv
->vlan_bitmap
)) {
839 pr_info("bridge: RTM_DELNEIGH with unconfigured "
840 "vlan %d on port %s\n", vid
, dev
->name
);
844 err
= __br_fdb_delete(p
, addr
, vid
);
846 if (!pv
|| bitmap_empty(pv
->vlan_bitmap
, VLAN_N_VID
)) {
847 err
= __br_fdb_delete(p
, addr
, 0);
851 /* We have vlans configured on this port and user didn't
852 * specify a VLAN. To be nice, add/update entry for every
856 for_each_set_bit(vid
, pv
->vlan_bitmap
, VLAN_N_VID
) {
857 err
&= __br_fdb_delete(p
, addr
, vid
);