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 struct net_bridge_fdb_entry
*fdb_find(struct hlist_head
*head
,
31 const unsigned char *addr
,
33 static int fdb_insert(struct net_bridge
*br
, struct net_bridge_port
*source
,
34 const unsigned char *addr
, u16 vid
);
35 static void fdb_notify(struct net_bridge
*br
,
36 const struct net_bridge_fdb_entry
*, int);
38 static u32 fdb_salt __read_mostly
;
40 int __init
br_fdb_init(void)
42 br_fdb_cache
= kmem_cache_create("bridge_fdb_cache",
43 sizeof(struct net_bridge_fdb_entry
),
45 SLAB_HWCACHE_ALIGN
, NULL
);
49 get_random_bytes(&fdb_salt
, sizeof(fdb_salt
));
53 void br_fdb_fini(void)
55 kmem_cache_destroy(br_fdb_cache
);
59 /* if topology_changing then use forward_delay (default 15 sec)
60 * otherwise keep longer (default 5 minutes)
62 static inline unsigned long hold_time(const struct net_bridge
*br
)
64 return br
->topology_change
? br
->forward_delay
: br
->ageing_time
;
67 static inline int has_expired(const struct net_bridge
*br
,
68 const struct net_bridge_fdb_entry
*fdb
)
70 return !fdb
->is_static
&&
71 time_before_eq(fdb
->updated
+ hold_time(br
), jiffies
);
74 static inline int br_mac_hash(const unsigned char *mac
, __u16 vid
)
76 /* use 1 byte of OUI and 3 bytes of NIC */
77 u32 key
= get_unaligned((u32
*)(mac
+ 2));
78 return jhash_2words(key
, vid
, fdb_salt
) & (BR_HASH_SIZE
- 1);
81 static void fdb_rcu_free(struct rcu_head
*head
)
83 struct net_bridge_fdb_entry
*ent
84 = container_of(head
, struct net_bridge_fdb_entry
, rcu
);
85 kmem_cache_free(br_fdb_cache
, ent
);
88 /* When a static FDB entry is added, the mac address from the entry is
89 * added to the bridge private HW address list and all required ports
90 * are then updated with the new information.
93 static void fdb_add_hw(struct net_bridge
*br
, const unsigned char *addr
)
96 struct net_bridge_port
*p
, *tmp
;
100 list_for_each_entry(p
, &br
->port_list
, list
) {
101 if (!br_promisc_port(p
)) {
102 err
= dev_uc_add(p
->dev
, addr
);
110 list_for_each_entry(tmp
, &br
->port_list
, list
) {
113 if (!br_promisc_port(tmp
))
114 dev_uc_del(tmp
->dev
, addr
);
118 /* When a static FDB entry is deleted, the HW address from that entry is
119 * also removed from the bridge private HW address list and updates all
120 * the ports with needed information.
123 static void fdb_del_hw(struct net_bridge
*br
, const unsigned char *addr
)
125 struct net_bridge_port
*p
;
129 list_for_each_entry(p
, &br
->port_list
, list
) {
130 if (!br_promisc_port(p
))
131 dev_uc_del(p
->dev
, addr
);
135 static void fdb_delete(struct net_bridge
*br
, struct net_bridge_fdb_entry
*f
)
138 fdb_del_hw(br
, f
->addr
.addr
);
140 hlist_del_rcu(&f
->hlist
);
141 fdb_notify(br
, f
, RTM_DELNEIGH
);
142 call_rcu(&f
->rcu
, fdb_rcu_free
);
145 /* Delete a local entry if no other port had the same address. */
146 static void fdb_delete_local(struct net_bridge
*br
,
147 const struct net_bridge_port
*p
,
148 struct net_bridge_fdb_entry
*f
)
150 const unsigned char *addr
= f
->addr
.addr
;
151 u16 vid
= f
->vlan_id
;
152 struct net_bridge_port
*op
;
154 /* Maybe another port has same hw addr? */
155 list_for_each_entry(op
, &br
->port_list
, list
) {
156 if (op
!= p
&& ether_addr_equal(op
->dev
->dev_addr
, addr
) &&
157 (!vid
|| nbp_vlan_find(op
, vid
))) {
159 f
->added_by_user
= 0;
164 /* Maybe bridge device has same hw addr? */
165 if (p
&& ether_addr_equal(br
->dev
->dev_addr
, addr
) &&
166 (!vid
|| br_vlan_find(br
, vid
))) {
168 f
->added_by_user
= 0;
175 void br_fdb_find_delete_local(struct net_bridge
*br
,
176 const struct net_bridge_port
*p
,
177 const unsigned char *addr
, u16 vid
)
179 struct hlist_head
*head
= &br
->hash
[br_mac_hash(addr
, vid
)];
180 struct net_bridge_fdb_entry
*f
;
182 spin_lock_bh(&br
->hash_lock
);
183 f
= fdb_find(head
, addr
, vid
);
184 if (f
&& f
->is_local
&& !f
->added_by_user
&& f
->dst
== p
)
185 fdb_delete_local(br
, p
, f
);
186 spin_unlock_bh(&br
->hash_lock
);
189 void br_fdb_changeaddr(struct net_bridge_port
*p
, const unsigned char *newaddr
)
191 struct net_bridge
*br
= p
->br
;
192 struct net_port_vlans
*pv
= nbp_get_vlan_info(p
);
197 spin_lock_bh(&br
->hash_lock
);
199 /* Search all chains since old address/hash is unknown */
200 for (i
= 0; i
< BR_HASH_SIZE
; i
++) {
201 struct hlist_node
*h
;
202 hlist_for_each(h
, &br
->hash
[i
]) {
203 struct net_bridge_fdb_entry
*f
;
205 f
= hlist_entry(h
, struct net_bridge_fdb_entry
, hlist
);
206 if (f
->dst
== p
&& f
->is_local
&& !f
->added_by_user
) {
208 fdb_delete_local(br
, p
, f
);
210 /* if this port has no vlan information
211 * configured, we can safely be done at
221 /* insert new address, may fail if invalid address or dup. */
222 fdb_insert(br
, p
, newaddr
, 0);
227 /* Now add entries for every VLAN configured on the port.
228 * This function runs under RTNL so the bitmap will not change
231 for_each_set_bit(vid
, pv
->vlan_bitmap
, VLAN_N_VID
)
232 fdb_insert(br
, p
, newaddr
, vid
);
235 spin_unlock_bh(&br
->hash_lock
);
238 void br_fdb_change_mac_address(struct net_bridge
*br
, const u8
*newaddr
)
240 struct net_bridge_fdb_entry
*f
;
241 struct net_port_vlans
*pv
;
244 spin_lock_bh(&br
->hash_lock
);
246 /* If old entry was unassociated with any port, then delete it. */
247 f
= __br_fdb_get(br
, br
->dev
->dev_addr
, 0);
248 if (f
&& f
->is_local
&& !f
->dst
)
249 fdb_delete_local(br
, NULL
, f
);
251 fdb_insert(br
, NULL
, newaddr
, 0);
253 /* Now remove and add entries for every VLAN configured on the
254 * bridge. This function runs under RTNL so the bitmap will not
255 * change from under us.
257 pv
= br_get_vlan_info(br
);
261 for_each_set_bit_from(vid
, pv
->vlan_bitmap
, VLAN_N_VID
) {
262 f
= __br_fdb_get(br
, br
->dev
->dev_addr
, vid
);
263 if (f
&& f
->is_local
&& !f
->dst
)
264 fdb_delete_local(br
, NULL
, f
);
265 fdb_insert(br
, NULL
, newaddr
, vid
);
268 spin_unlock_bh(&br
->hash_lock
);
271 void br_fdb_cleanup(unsigned long _data
)
273 struct net_bridge
*br
= (struct net_bridge
*)_data
;
274 unsigned long delay
= hold_time(br
);
275 unsigned long next_timer
= jiffies
+ br
->ageing_time
;
278 spin_lock(&br
->hash_lock
);
279 for (i
= 0; i
< BR_HASH_SIZE
; i
++) {
280 struct net_bridge_fdb_entry
*f
;
281 struct hlist_node
*n
;
283 hlist_for_each_entry_safe(f
, n
, &br
->hash
[i
], hlist
) {
284 unsigned long this_timer
;
287 this_timer
= f
->updated
+ delay
;
288 if (time_before_eq(this_timer
, jiffies
))
290 else if (time_before(this_timer
, next_timer
))
291 next_timer
= this_timer
;
294 spin_unlock(&br
->hash_lock
);
296 mod_timer(&br
->gc_timer
, round_jiffies_up(next_timer
));
299 /* Completely flush all dynamic entries in forwarding database.*/
300 void br_fdb_flush(struct net_bridge
*br
)
304 spin_lock_bh(&br
->hash_lock
);
305 for (i
= 0; i
< BR_HASH_SIZE
; i
++) {
306 struct net_bridge_fdb_entry
*f
;
307 struct hlist_node
*n
;
308 hlist_for_each_entry_safe(f
, n
, &br
->hash
[i
], hlist
) {
313 spin_unlock_bh(&br
->hash_lock
);
316 /* Flush all entries referring to a specific port.
317 * if do_all is set also flush static entries
319 void br_fdb_delete_by_port(struct net_bridge
*br
,
320 const struct net_bridge_port
*p
,
325 spin_lock_bh(&br
->hash_lock
);
326 for (i
= 0; i
< BR_HASH_SIZE
; i
++) {
327 struct hlist_node
*h
, *g
;
329 hlist_for_each_safe(h
, g
, &br
->hash
[i
]) {
330 struct net_bridge_fdb_entry
*f
331 = hlist_entry(h
, struct net_bridge_fdb_entry
, hlist
);
335 if (f
->is_static
&& !do_all
)
339 fdb_delete_local(br
, p
, f
);
344 spin_unlock_bh(&br
->hash_lock
);
347 /* No locking or refcounting, assumes caller has rcu_read_lock */
348 struct net_bridge_fdb_entry
*__br_fdb_get(struct net_bridge
*br
,
349 const unsigned char *addr
,
352 struct net_bridge_fdb_entry
*fdb
;
354 hlist_for_each_entry_rcu(fdb
,
355 &br
->hash
[br_mac_hash(addr
, vid
)], hlist
) {
356 if (ether_addr_equal(fdb
->addr
.addr
, addr
) &&
357 fdb
->vlan_id
== vid
) {
358 if (unlikely(has_expired(br
, fdb
)))
367 #if IS_ENABLED(CONFIG_ATM_LANE)
368 /* Interface used by ATM LANE hook to test
369 * if an addr is on some other bridge port */
370 int br_fdb_test_addr(struct net_device
*dev
, unsigned char *addr
)
372 struct net_bridge_fdb_entry
*fdb
;
373 struct net_bridge_port
*port
;
377 port
= br_port_get_rcu(dev
);
381 fdb
= __br_fdb_get(port
->br
, addr
, 0);
382 ret
= fdb
&& fdb
->dst
&& fdb
->dst
->dev
!= dev
&&
383 fdb
->dst
->state
== BR_STATE_FORWARDING
;
389 #endif /* CONFIG_ATM_LANE */
392 * Fill buffer with forwarding table records in
395 int br_fdb_fillbuf(struct net_bridge
*br
, void *buf
,
396 unsigned long maxnum
, unsigned long skip
)
398 struct __fdb_entry
*fe
= buf
;
400 struct net_bridge_fdb_entry
*f
;
402 memset(buf
, 0, maxnum
*sizeof(struct __fdb_entry
));
405 for (i
= 0; i
< BR_HASH_SIZE
; i
++) {
406 hlist_for_each_entry_rcu(f
, &br
->hash
[i
], hlist
) {
410 if (has_expired(br
, f
))
413 /* ignore pseudo entry for local MAC address */
422 /* convert from internal format to API */
423 memcpy(fe
->mac_addr
, f
->addr
.addr
, ETH_ALEN
);
425 /* due to ABI compat need to split into hi/lo */
426 fe
->port_no
= f
->dst
->port_no
;
427 fe
->port_hi
= f
->dst
->port_no
>> 8;
429 fe
->is_local
= f
->is_local
;
431 fe
->ageing_timer_value
= jiffies_delta_to_clock_t(jiffies
- f
->updated
);
443 static struct net_bridge_fdb_entry
*fdb_find(struct hlist_head
*head
,
444 const unsigned char *addr
,
447 struct net_bridge_fdb_entry
*fdb
;
449 hlist_for_each_entry(fdb
, head
, hlist
) {
450 if (ether_addr_equal(fdb
->addr
.addr
, addr
) &&
457 static struct net_bridge_fdb_entry
*fdb_find_rcu(struct hlist_head
*head
,
458 const unsigned char *addr
,
461 struct net_bridge_fdb_entry
*fdb
;
463 hlist_for_each_entry_rcu(fdb
, head
, hlist
) {
464 if (ether_addr_equal(fdb
->addr
.addr
, addr
) &&
471 static struct net_bridge_fdb_entry
*fdb_create(struct hlist_head
*head
,
472 struct net_bridge_port
*source
,
473 const unsigned char *addr
,
476 struct net_bridge_fdb_entry
*fdb
;
478 fdb
= kmem_cache_alloc(br_fdb_cache
, GFP_ATOMIC
);
480 memcpy(fdb
->addr
.addr
, addr
, ETH_ALEN
);
485 fdb
->added_by_user
= 0;
486 fdb
->updated
= fdb
->used
= jiffies
;
487 hlist_add_head_rcu(&fdb
->hlist
, head
);
492 static int fdb_insert(struct net_bridge
*br
, struct net_bridge_port
*source
,
493 const unsigned char *addr
, u16 vid
)
495 struct hlist_head
*head
= &br
->hash
[br_mac_hash(addr
, vid
)];
496 struct net_bridge_fdb_entry
*fdb
;
498 if (!is_valid_ether_addr(addr
))
501 fdb
= fdb_find(head
, addr
, vid
);
503 /* it is okay to have multiple ports with same
504 * address, just use the first one.
508 br_warn(br
, "adding interface %s with same address "
509 "as a received packet\n",
510 source
? source
->dev
->name
: br
->dev
->name
);
514 fdb
= fdb_create(head
, source
, addr
, vid
);
518 fdb
->is_local
= fdb
->is_static
= 1;
519 fdb_add_hw(br
, addr
);
520 fdb_notify(br
, fdb
, RTM_NEWNEIGH
);
524 /* Add entry for local address of interface */
525 int br_fdb_insert(struct net_bridge
*br
, struct net_bridge_port
*source
,
526 const unsigned char *addr
, u16 vid
)
530 spin_lock_bh(&br
->hash_lock
);
531 ret
= fdb_insert(br
, source
, addr
, vid
);
532 spin_unlock_bh(&br
->hash_lock
);
536 void br_fdb_update(struct net_bridge
*br
, struct net_bridge_port
*source
,
537 const unsigned char *addr
, u16 vid
, bool added_by_user
)
539 struct hlist_head
*head
= &br
->hash
[br_mac_hash(addr
, vid
)];
540 struct net_bridge_fdb_entry
*fdb
;
541 bool fdb_modified
= false;
543 /* some users want to always flood. */
544 if (hold_time(br
) == 0)
547 /* ignore packets unless we are using this port */
548 if (!(source
->state
== BR_STATE_LEARNING
||
549 source
->state
== BR_STATE_FORWARDING
))
552 fdb
= fdb_find_rcu(head
, addr
, vid
);
554 /* attempt to update an entry for a local interface */
555 if (unlikely(fdb
->is_local
)) {
557 br_warn(br
, "received packet on %s with "
558 "own address as source address\n",
561 /* fastpath: update of existing entry */
562 if (unlikely(source
!= fdb
->dst
)) {
566 fdb
->updated
= jiffies
;
567 if (unlikely(added_by_user
))
568 fdb
->added_by_user
= 1;
569 if (unlikely(fdb_modified
))
570 fdb_notify(br
, fdb
, RTM_NEWNEIGH
);
573 spin_lock(&br
->hash_lock
);
574 if (likely(!fdb_find(head
, addr
, vid
))) {
575 fdb
= fdb_create(head
, source
, addr
, vid
);
577 if (unlikely(added_by_user
))
578 fdb
->added_by_user
= 1;
579 fdb_notify(br
, fdb
, RTM_NEWNEIGH
);
582 /* else we lose race and someone else inserts
583 * it first, don't bother updating
585 spin_unlock(&br
->hash_lock
);
589 static int fdb_to_nud(const struct net_bridge_fdb_entry
*fdb
)
592 return NUD_PERMANENT
;
593 else if (fdb
->is_static
)
595 else if (has_expired(fdb
->dst
->br
, fdb
))
598 return NUD_REACHABLE
;
601 static int fdb_fill_info(struct sk_buff
*skb
, const struct net_bridge
*br
,
602 const struct net_bridge_fdb_entry
*fdb
,
603 u32 portid
, u32 seq
, int type
, unsigned int flags
)
605 unsigned long now
= jiffies
;
606 struct nda_cacheinfo ci
;
607 struct nlmsghdr
*nlh
;
610 nlh
= nlmsg_put(skb
, portid
, seq
, type
, sizeof(*ndm
), flags
);
614 ndm
= nlmsg_data(nlh
);
615 ndm
->ndm_family
= AF_BRIDGE
;
620 ndm
->ndm_ifindex
= fdb
->dst
? fdb
->dst
->dev
->ifindex
: br
->dev
->ifindex
;
621 ndm
->ndm_state
= fdb_to_nud(fdb
);
623 if (nla_put(skb
, NDA_LLADDR
, ETH_ALEN
, &fdb
->addr
))
624 goto nla_put_failure
;
625 if (nla_put_u32(skb
, NDA_MASTER
, br
->dev
->ifindex
))
626 goto nla_put_failure
;
627 ci
.ndm_used
= jiffies_to_clock_t(now
- fdb
->used
);
628 ci
.ndm_confirmed
= 0;
629 ci
.ndm_updated
= jiffies_to_clock_t(now
- fdb
->updated
);
631 if (nla_put(skb
, NDA_CACHEINFO
, sizeof(ci
), &ci
))
632 goto nla_put_failure
;
634 if (nla_put(skb
, NDA_VLAN
, sizeof(u16
), &fdb
->vlan_id
))
635 goto nla_put_failure
;
637 return nlmsg_end(skb
, nlh
);
640 nlmsg_cancel(skb
, nlh
);
644 static inline size_t fdb_nlmsg_size(void)
646 return NLMSG_ALIGN(sizeof(struct ndmsg
))
647 + nla_total_size(ETH_ALEN
) /* NDA_LLADDR */
648 + nla_total_size(sizeof(u32
)) /* NDA_MASTER */
649 + nla_total_size(sizeof(u16
)) /* NDA_VLAN */
650 + nla_total_size(sizeof(struct nda_cacheinfo
));
653 static void fdb_notify(struct net_bridge
*br
,
654 const struct net_bridge_fdb_entry
*fdb
, int type
)
656 struct net
*net
= dev_net(br
->dev
);
660 skb
= nlmsg_new(fdb_nlmsg_size(), GFP_ATOMIC
);
664 err
= fdb_fill_info(skb
, br
, fdb
, 0, 0, type
, 0);
666 /* -EMSGSIZE implies BUG in fdb_nlmsg_size() */
667 WARN_ON(err
== -EMSGSIZE
);
671 rtnl_notify(skb
, net
, 0, RTNLGRP_NEIGH
, NULL
, GFP_ATOMIC
);
674 rtnl_set_sk_err(net
, RTNLGRP_NEIGH
, err
);
677 /* Dump information about entries, in response to GETNEIGH */
678 int br_fdb_dump(struct sk_buff
*skb
,
679 struct netlink_callback
*cb
,
680 struct net_device
*dev
,
683 struct net_bridge
*br
= netdev_priv(dev
);
686 if (!(dev
->priv_flags
& IFF_EBRIDGE
))
689 for (i
= 0; i
< BR_HASH_SIZE
; i
++) {
690 struct net_bridge_fdb_entry
*f
;
692 hlist_for_each_entry_rcu(f
, &br
->hash
[i
], hlist
) {
693 if (idx
< cb
->args
[0])
696 if (fdb_fill_info(skb
, br
, f
,
697 NETLINK_CB(cb
->skb
).portid
,
711 /* Update (create or replace) forwarding database entry */
712 static int fdb_add_entry(struct net_bridge_port
*source
, const __u8
*addr
,
713 __u16 state
, __u16 flags
, __u16 vid
)
715 struct net_bridge
*br
= source
->br
;
716 struct hlist_head
*head
= &br
->hash
[br_mac_hash(addr
, vid
)];
717 struct net_bridge_fdb_entry
*fdb
;
718 bool modified
= false;
720 fdb
= fdb_find(head
, addr
, vid
);
722 if (!(flags
& NLM_F_CREATE
))
725 fdb
= fdb_create(head
, source
, addr
, vid
);
731 if (flags
& NLM_F_EXCL
)
734 if (fdb
->dst
!= source
) {
740 if (fdb_to_nud(fdb
) != state
) {
741 if (state
& NUD_PERMANENT
) {
743 if (!fdb
->is_static
) {
745 fdb_add_hw(br
, addr
);
747 } else if (state
& NUD_NOARP
) {
749 if (!fdb
->is_static
) {
751 fdb_add_hw(br
, addr
);
755 if (fdb
->is_static
) {
757 fdb_del_hw(br
, addr
);
763 fdb
->added_by_user
= 1;
767 fdb
->updated
= jiffies
;
768 fdb_notify(br
, fdb
, RTM_NEWNEIGH
);
774 static int __br_fdb_add(struct ndmsg
*ndm
, struct net_bridge_port
*p
,
775 const unsigned char *addr
, u16 nlh_flags
, u16 vid
)
779 if (ndm
->ndm_flags
& NTF_USE
) {
781 br_fdb_update(p
->br
, p
, addr
, vid
, true);
784 spin_lock_bh(&p
->br
->hash_lock
);
785 err
= fdb_add_entry(p
, addr
, ndm
->ndm_state
,
787 spin_unlock_bh(&p
->br
->hash_lock
);
793 /* Add new permanent fdb entry with RTM_NEWNEIGH */
794 int br_fdb_add(struct ndmsg
*ndm
, struct nlattr
*tb
[],
795 struct net_device
*dev
,
796 const unsigned char *addr
, u16 nlh_flags
)
798 struct net_bridge_port
*p
;
800 struct net_port_vlans
*pv
;
801 unsigned short vid
= VLAN_N_VID
;
803 if (!(ndm
->ndm_state
& (NUD_PERMANENT
|NUD_NOARP
|NUD_REACHABLE
))) {
804 pr_info("bridge: RTM_NEWNEIGH with invalid state %#x\n", ndm
->ndm_state
);
809 if (nla_len(tb
[NDA_VLAN
]) != sizeof(unsigned short)) {
810 pr_info("bridge: RTM_NEWNEIGH with invalid vlan\n");
814 vid
= nla_get_u16(tb
[NDA_VLAN
]);
816 if (!vid
|| vid
>= VLAN_VID_MASK
) {
817 pr_info("bridge: RTM_NEWNEIGH with invalid vlan id %d\n",
823 if (is_zero_ether_addr(addr
)) {
824 pr_info("bridge: RTM_NEWNEIGH with invalid ether address\n");
828 p
= br_port_get_rtnl(dev
);
830 pr_info("bridge: RTM_NEWNEIGH %s not a bridge port\n",
835 pv
= nbp_get_vlan_info(p
);
836 if (vid
!= VLAN_N_VID
) {
837 if (!pv
|| !test_bit(vid
, pv
->vlan_bitmap
)) {
838 pr_info("bridge: RTM_NEWNEIGH with unconfigured "
839 "vlan %d on port %s\n", vid
, dev
->name
);
843 /* VID was specified, so use it. */
844 err
= __br_fdb_add(ndm
, p
, addr
, nlh_flags
, vid
);
846 if (!pv
|| bitmap_empty(pv
->vlan_bitmap
, VLAN_N_VID
)) {
847 err
= __br_fdb_add(ndm
, p
, addr
, nlh_flags
, 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
855 for_each_set_bit(vid
, pv
->vlan_bitmap
, VLAN_N_VID
) {
856 err
= __br_fdb_add(ndm
, p
, addr
, nlh_flags
, vid
);
866 static int fdb_delete_by_addr(struct net_bridge
*br
, const u8
*addr
, u16 vlan
)
868 struct hlist_head
*head
= &br
->hash
[br_mac_hash(addr
, vlan
)];
869 struct net_bridge_fdb_entry
*fdb
;
871 fdb
= fdb_find(head
, addr
, vlan
);
879 static int __br_fdb_delete(struct net_bridge_port
*p
,
880 const unsigned char *addr
, u16 vid
)
884 spin_lock_bh(&p
->br
->hash_lock
);
885 err
= fdb_delete_by_addr(p
->br
, addr
, vid
);
886 spin_unlock_bh(&p
->br
->hash_lock
);
891 /* Remove neighbor entry with RTM_DELNEIGH */
892 int br_fdb_delete(struct ndmsg
*ndm
, struct nlattr
*tb
[],
893 struct net_device
*dev
,
894 const unsigned char *addr
)
896 struct net_bridge_port
*p
;
898 struct net_port_vlans
*pv
;
899 unsigned short vid
= VLAN_N_VID
;
902 if (nla_len(tb
[NDA_VLAN
]) != sizeof(unsigned short)) {
903 pr_info("bridge: RTM_NEWNEIGH with invalid vlan\n");
907 vid
= nla_get_u16(tb
[NDA_VLAN
]);
909 if (!vid
|| vid
>= VLAN_VID_MASK
) {
910 pr_info("bridge: RTM_NEWNEIGH with invalid vlan id %d\n",
915 p
= br_port_get_rtnl(dev
);
917 pr_info("bridge: RTM_DELNEIGH %s not a bridge port\n",
922 pv
= nbp_get_vlan_info(p
);
923 if (vid
!= VLAN_N_VID
) {
924 if (!pv
|| !test_bit(vid
, pv
->vlan_bitmap
)) {
925 pr_info("bridge: RTM_DELNEIGH with unconfigured "
926 "vlan %d on port %s\n", vid
, dev
->name
);
930 err
= __br_fdb_delete(p
, addr
, vid
);
932 if (!pv
|| bitmap_empty(pv
->vlan_bitmap
, VLAN_N_VID
)) {
933 err
= __br_fdb_delete(p
, addr
, 0);
937 /* We have vlans configured on this port and user didn't
938 * specify a VLAN. To be nice, add/update entry for every
942 for_each_set_bit(vid
, pv
->vlan_bitmap
, VLAN_N_VID
) {
943 err
&= __br_fdb_delete(p
, addr
, vid
);
950 int br_fdb_sync_static(struct net_bridge
*br
, struct net_bridge_port
*p
)
952 struct net_bridge_fdb_entry
*fdb
, *tmp
;
958 for (i
= 0; i
< BR_HASH_SIZE
; i
++) {
959 hlist_for_each_entry(fdb
, &br
->hash
[i
], hlist
) {
960 /* We only care for static entries */
964 err
= dev_uc_add(p
->dev
, fdb
->addr
.addr
);
972 for (i
= 0; i
< BR_HASH_SIZE
; i
++) {
973 hlist_for_each_entry(tmp
, &br
->hash
[i
], hlist
) {
974 /* If we reached the fdb that failed, we can stop */
978 /* We only care for static entries */
982 dev_uc_del(p
->dev
, tmp
->addr
.addr
);
988 void br_fdb_unsync_static(struct net_bridge
*br
, struct net_bridge_port
*p
)
990 struct net_bridge_fdb_entry
*fdb
;
995 for (i
= 0; i
< BR_HASH_SIZE
; i
++) {
996 hlist_for_each_entry_rcu(fdb
, &br
->hash
[i
], hlist
) {
997 /* We only care for static entries */
1001 dev_uc_del(p
->dev
, fdb
->addr
.addr
);