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 <net/switchdev.h>
28 #include "br_private.h"
30 static struct kmem_cache
*br_fdb_cache __read_mostly
;
31 static int fdb_insert(struct net_bridge
*br
, struct net_bridge_port
*source
,
32 const unsigned char *addr
, u16 vid
);
33 static void fdb_notify(struct net_bridge
*br
,
34 const struct net_bridge_fdb_entry
*, int);
36 static u32 fdb_salt __read_mostly
;
38 int __init
br_fdb_init(void)
40 br_fdb_cache
= kmem_cache_create("bridge_fdb_cache",
41 sizeof(struct net_bridge_fdb_entry
),
43 SLAB_HWCACHE_ALIGN
, NULL
);
47 get_random_bytes(&fdb_salt
, sizeof(fdb_salt
));
51 void br_fdb_fini(void)
53 kmem_cache_destroy(br_fdb_cache
);
57 /* if topology_changing then use forward_delay (default 15 sec)
58 * otherwise keep longer (default 5 minutes)
60 static inline unsigned long hold_time(const struct net_bridge
*br
)
62 return br
->topology_change
? br
->forward_delay
: br
->ageing_time
;
65 static inline int has_expired(const struct net_bridge
*br
,
66 const struct net_bridge_fdb_entry
*fdb
)
68 return !fdb
->is_static
&& !fdb
->added_by_external_learn
&&
69 time_before_eq(fdb
->updated
+ hold_time(br
), jiffies
);
72 static inline int br_mac_hash(const unsigned char *mac
, __u16 vid
)
74 /* use 1 byte of OUI and 3 bytes of NIC */
75 u32 key
= get_unaligned((u32
*)(mac
+ 2));
76 return jhash_2words(key
, vid
, fdb_salt
) & (BR_HASH_SIZE
- 1);
79 static void fdb_rcu_free(struct rcu_head
*head
)
81 struct net_bridge_fdb_entry
*ent
82 = container_of(head
, struct net_bridge_fdb_entry
, rcu
);
83 kmem_cache_free(br_fdb_cache
, ent
);
86 static struct net_bridge_fdb_entry
*fdb_find_rcu(struct hlist_head
*head
,
87 const unsigned char *addr
,
90 struct net_bridge_fdb_entry
*f
;
92 WARN_ON_ONCE(!rcu_read_lock_held());
94 hlist_for_each_entry_rcu(f
, head
, hlist
)
95 if (ether_addr_equal(f
->addr
.addr
, addr
) && f
->vlan_id
== vid
)
101 /* requires bridge hash_lock */
102 static struct net_bridge_fdb_entry
*br_fdb_find(struct net_bridge
*br
,
103 const unsigned char *addr
,
106 struct hlist_head
*head
= &br
->hash
[br_mac_hash(addr
, vid
)];
107 struct net_bridge_fdb_entry
*fdb
;
109 lockdep_assert_held_once(&br
->hash_lock
);
112 fdb
= fdb_find_rcu(head
, addr
, vid
);
118 struct net_bridge_fdb_entry
*br_fdb_find_rcu(struct net_bridge
*br
,
119 const unsigned char *addr
,
122 struct hlist_head
*head
= &br
->hash
[br_mac_hash(addr
, vid
)];
124 return fdb_find_rcu(head
, addr
, vid
);
127 /* When a static FDB entry is added, the mac address from the entry is
128 * added to the bridge private HW address list and all required ports
129 * are then updated with the new information.
132 static void fdb_add_hw_addr(struct net_bridge
*br
, const unsigned char *addr
)
135 struct net_bridge_port
*p
;
139 list_for_each_entry(p
, &br
->port_list
, list
) {
140 if (!br_promisc_port(p
)) {
141 err
= dev_uc_add(p
->dev
, addr
);
149 list_for_each_entry_continue_reverse(p
, &br
->port_list
, list
) {
150 if (!br_promisc_port(p
))
151 dev_uc_del(p
->dev
, addr
);
155 /* When a static FDB entry is deleted, the HW address from that entry is
156 * also removed from the bridge private HW address list and updates all
157 * the ports with needed information.
160 static void fdb_del_hw_addr(struct net_bridge
*br
, const unsigned char *addr
)
162 struct net_bridge_port
*p
;
166 list_for_each_entry(p
, &br
->port_list
, list
) {
167 if (!br_promisc_port(p
))
168 dev_uc_del(p
->dev
, addr
);
172 static void fdb_del_external_learn(struct net_bridge_fdb_entry
*f
)
174 struct switchdev_obj_port_fdb fdb
= {
176 .orig_dev
= f
->dst
->dev
,
177 .id
= SWITCHDEV_OBJ_ID_PORT_FDB
,
178 .flags
= SWITCHDEV_F_DEFER
,
183 ether_addr_copy(fdb
.addr
, f
->addr
.addr
);
184 switchdev_port_obj_del(f
->dst
->dev
, &fdb
.obj
);
187 static void fdb_delete(struct net_bridge
*br
, struct net_bridge_fdb_entry
*f
)
190 fdb_del_hw_addr(br
, f
->addr
.addr
);
192 if (f
->added_by_external_learn
)
193 fdb_del_external_learn(f
);
195 hlist_del_init_rcu(&f
->hlist
);
196 fdb_notify(br
, f
, RTM_DELNEIGH
);
197 call_rcu(&f
->rcu
, fdb_rcu_free
);
200 /* Delete a local entry if no other port had the same address. */
201 static void fdb_delete_local(struct net_bridge
*br
,
202 const struct net_bridge_port
*p
,
203 struct net_bridge_fdb_entry
*f
)
205 const unsigned char *addr
= f
->addr
.addr
;
206 struct net_bridge_vlan_group
*vg
;
207 const struct net_bridge_vlan
*v
;
208 struct net_bridge_port
*op
;
209 u16 vid
= f
->vlan_id
;
211 /* Maybe another port has same hw addr? */
212 list_for_each_entry(op
, &br
->port_list
, list
) {
213 vg
= nbp_vlan_group(op
);
214 if (op
!= p
&& ether_addr_equal(op
->dev
->dev_addr
, addr
) &&
215 (!vid
|| br_vlan_find(vg
, vid
))) {
217 f
->added_by_user
= 0;
222 vg
= br_vlan_group(br
);
223 v
= br_vlan_find(vg
, vid
);
224 /* Maybe bridge device has same hw addr? */
225 if (p
&& ether_addr_equal(br
->dev
->dev_addr
, addr
) &&
226 (!vid
|| (v
&& br_vlan_should_use(v
)))) {
228 f
->added_by_user
= 0;
235 void br_fdb_find_delete_local(struct net_bridge
*br
,
236 const struct net_bridge_port
*p
,
237 const unsigned char *addr
, u16 vid
)
239 struct net_bridge_fdb_entry
*f
;
241 spin_lock_bh(&br
->hash_lock
);
242 f
= br_fdb_find(br
, addr
, vid
);
243 if (f
&& f
->is_local
&& !f
->added_by_user
&& f
->dst
== p
)
244 fdb_delete_local(br
, p
, f
);
245 spin_unlock_bh(&br
->hash_lock
);
248 void br_fdb_changeaddr(struct net_bridge_port
*p
, const unsigned char *newaddr
)
250 struct net_bridge_vlan_group
*vg
;
251 struct net_bridge
*br
= p
->br
;
252 struct net_bridge_vlan
*v
;
255 spin_lock_bh(&br
->hash_lock
);
257 vg
= nbp_vlan_group(p
);
258 /* Search all chains since old address/hash is unknown */
259 for (i
= 0; i
< BR_HASH_SIZE
; i
++) {
260 struct hlist_node
*h
;
261 hlist_for_each(h
, &br
->hash
[i
]) {
262 struct net_bridge_fdb_entry
*f
;
264 f
= hlist_entry(h
, struct net_bridge_fdb_entry
, hlist
);
265 if (f
->dst
== p
&& f
->is_local
&& !f
->added_by_user
) {
267 fdb_delete_local(br
, p
, f
);
269 /* if this port has no vlan information
270 * configured, we can safely be done at
273 if (!vg
|| !vg
->num_vlans
)
280 /* insert new address, may fail if invalid address or dup. */
281 fdb_insert(br
, p
, newaddr
, 0);
283 if (!vg
|| !vg
->num_vlans
)
286 /* Now add entries for every VLAN configured on the port.
287 * This function runs under RTNL so the bitmap will not change
290 list_for_each_entry(v
, &vg
->vlan_list
, vlist
)
291 fdb_insert(br
, p
, newaddr
, v
->vid
);
294 spin_unlock_bh(&br
->hash_lock
);
297 void br_fdb_change_mac_address(struct net_bridge
*br
, const u8
*newaddr
)
299 struct net_bridge_vlan_group
*vg
;
300 struct net_bridge_fdb_entry
*f
;
301 struct net_bridge_vlan
*v
;
303 spin_lock_bh(&br
->hash_lock
);
305 /* If old entry was unassociated with any port, then delete it. */
306 f
= br_fdb_find(br
, br
->dev
->dev_addr
, 0);
307 if (f
&& f
->is_local
&& !f
->dst
&& !f
->added_by_user
)
308 fdb_delete_local(br
, NULL
, f
);
310 fdb_insert(br
, NULL
, newaddr
, 0);
311 vg
= br_vlan_group(br
);
312 if (!vg
|| !vg
->num_vlans
)
314 /* Now remove and add entries for every VLAN configured on the
315 * bridge. This function runs under RTNL so the bitmap will not
316 * change from under us.
318 list_for_each_entry(v
, &vg
->vlan_list
, vlist
) {
319 if (!br_vlan_should_use(v
))
321 f
= br_fdb_find(br
, br
->dev
->dev_addr
, v
->vid
);
322 if (f
&& f
->is_local
&& !f
->dst
&& !f
->added_by_user
)
323 fdb_delete_local(br
, NULL
, f
);
324 fdb_insert(br
, NULL
, newaddr
, v
->vid
);
327 spin_unlock_bh(&br
->hash_lock
);
330 void br_fdb_cleanup(struct work_struct
*work
)
332 struct net_bridge
*br
= container_of(work
, struct net_bridge
,
334 unsigned long delay
= hold_time(br
);
335 unsigned long work_delay
= delay
;
336 unsigned long now
= jiffies
;
339 for (i
= 0; i
< BR_HASH_SIZE
; i
++) {
340 struct net_bridge_fdb_entry
*f
;
341 struct hlist_node
*n
;
343 if (!br
->hash
[i
].first
)
346 spin_lock_bh(&br
->hash_lock
);
347 hlist_for_each_entry_safe(f
, n
, &br
->hash
[i
], hlist
) {
348 unsigned long this_timer
;
352 if (f
->added_by_external_learn
)
354 this_timer
= f
->updated
+ delay
;
355 if (time_after(this_timer
, now
))
356 work_delay
= min(work_delay
, this_timer
- now
);
360 spin_unlock_bh(&br
->hash_lock
);
364 /* Cleanup minimum 10 milliseconds apart */
365 work_delay
= max_t(unsigned long, work_delay
, msecs_to_jiffies(10));
366 mod_delayed_work(system_long_wq
, &br
->gc_work
, work_delay
);
369 /* Completely flush all dynamic entries in forwarding database.*/
370 void br_fdb_flush(struct net_bridge
*br
)
374 spin_lock_bh(&br
->hash_lock
);
375 for (i
= 0; i
< BR_HASH_SIZE
; i
++) {
376 struct net_bridge_fdb_entry
*f
;
377 struct hlist_node
*n
;
378 hlist_for_each_entry_safe(f
, n
, &br
->hash
[i
], hlist
) {
383 spin_unlock_bh(&br
->hash_lock
);
386 /* Flush all entries referring to a specific port.
387 * if do_all is set also flush static entries
388 * if vid is set delete all entries that match the vlan_id
390 void br_fdb_delete_by_port(struct net_bridge
*br
,
391 const struct net_bridge_port
*p
,
397 spin_lock_bh(&br
->hash_lock
);
398 for (i
= 0; i
< BR_HASH_SIZE
; i
++) {
399 struct hlist_node
*h
, *g
;
401 hlist_for_each_safe(h
, g
, &br
->hash
[i
]) {
402 struct net_bridge_fdb_entry
*f
403 = hlist_entry(h
, struct net_bridge_fdb_entry
, hlist
);
408 if (f
->is_static
|| (vid
&& f
->vlan_id
!= vid
))
412 fdb_delete_local(br
, p
, f
);
417 spin_unlock_bh(&br
->hash_lock
);
420 #if IS_ENABLED(CONFIG_ATM_LANE)
421 /* Interface used by ATM LANE hook to test
422 * if an addr is on some other bridge port */
423 int br_fdb_test_addr(struct net_device
*dev
, unsigned char *addr
)
425 struct net_bridge_fdb_entry
*fdb
;
426 struct net_bridge_port
*port
;
430 port
= br_port_get_rcu(dev
);
434 fdb
= br_fdb_find_rcu(port
->br
, addr
, 0);
435 ret
= fdb
&& fdb
->dst
&& fdb
->dst
->dev
!= dev
&&
436 fdb
->dst
->state
== BR_STATE_FORWARDING
;
442 #endif /* CONFIG_ATM_LANE */
445 * Fill buffer with forwarding table records in
448 int br_fdb_fillbuf(struct net_bridge
*br
, void *buf
,
449 unsigned long maxnum
, unsigned long skip
)
451 struct __fdb_entry
*fe
= buf
;
453 struct net_bridge_fdb_entry
*f
;
455 memset(buf
, 0, maxnum
*sizeof(struct __fdb_entry
));
458 for (i
= 0; i
< BR_HASH_SIZE
; i
++) {
459 hlist_for_each_entry_rcu(f
, &br
->hash
[i
], hlist
) {
463 if (has_expired(br
, f
))
466 /* ignore pseudo entry for local MAC address */
475 /* convert from internal format to API */
476 memcpy(fe
->mac_addr
, f
->addr
.addr
, ETH_ALEN
);
478 /* due to ABI compat need to split into hi/lo */
479 fe
->port_no
= f
->dst
->port_no
;
480 fe
->port_hi
= f
->dst
->port_no
>> 8;
482 fe
->is_local
= f
->is_local
;
484 fe
->ageing_timer_value
= jiffies_delta_to_clock_t(jiffies
- f
->updated
);
496 static struct net_bridge_fdb_entry
*fdb_create(struct hlist_head
*head
,
497 struct net_bridge_port
*source
,
498 const unsigned char *addr
,
500 unsigned char is_local
,
501 unsigned char is_static
)
503 struct net_bridge_fdb_entry
*fdb
;
505 fdb
= kmem_cache_alloc(br_fdb_cache
, GFP_ATOMIC
);
507 memcpy(fdb
->addr
.addr
, addr
, ETH_ALEN
);
510 fdb
->is_local
= is_local
;
511 fdb
->is_static
= is_static
;
512 fdb
->added_by_user
= 0;
513 fdb
->added_by_external_learn
= 0;
514 fdb
->updated
= fdb
->used
= jiffies
;
515 hlist_add_head_rcu(&fdb
->hlist
, head
);
520 static int fdb_insert(struct net_bridge
*br
, struct net_bridge_port
*source
,
521 const unsigned char *addr
, u16 vid
)
523 struct hlist_head
*head
= &br
->hash
[br_mac_hash(addr
, vid
)];
524 struct net_bridge_fdb_entry
*fdb
;
526 if (!is_valid_ether_addr(addr
))
529 fdb
= br_fdb_find(br
, addr
, vid
);
531 /* it is okay to have multiple ports with same
532 * address, just use the first one.
536 br_warn(br
, "adding interface %s with same address as a received packet (addr:%pM, vlan:%u)\n",
537 source
? source
->dev
->name
: br
->dev
->name
, addr
, vid
);
541 fdb
= fdb_create(head
, source
, addr
, vid
, 1, 1);
545 fdb_add_hw_addr(br
, addr
);
546 fdb_notify(br
, fdb
, RTM_NEWNEIGH
);
550 /* Add entry for local address of interface */
551 int br_fdb_insert(struct net_bridge
*br
, struct net_bridge_port
*source
,
552 const unsigned char *addr
, u16 vid
)
556 spin_lock_bh(&br
->hash_lock
);
557 ret
= fdb_insert(br
, source
, addr
, vid
);
558 spin_unlock_bh(&br
->hash_lock
);
562 void br_fdb_update(struct net_bridge
*br
, struct net_bridge_port
*source
,
563 const unsigned char *addr
, u16 vid
, bool added_by_user
)
565 struct hlist_head
*head
= &br
->hash
[br_mac_hash(addr
, vid
)];
566 struct net_bridge_fdb_entry
*fdb
;
567 bool fdb_modified
= false;
569 /* some users want to always flood. */
570 if (hold_time(br
) == 0)
573 /* ignore packets unless we are using this port */
574 if (!(source
->state
== BR_STATE_LEARNING
||
575 source
->state
== BR_STATE_FORWARDING
))
578 fdb
= fdb_find_rcu(head
, addr
, vid
);
580 /* attempt to update an entry for a local interface */
581 if (unlikely(fdb
->is_local
)) {
583 br_warn(br
, "received packet on %s with own address as source address (addr:%pM, vlan:%u)\n",
584 source
->dev
->name
, addr
, vid
);
586 unsigned long now
= jiffies
;
588 /* fastpath: update of existing entry */
589 if (unlikely(source
!= fdb
->dst
)) {
593 if (now
!= fdb
->updated
)
595 if (unlikely(added_by_user
))
596 fdb
->added_by_user
= 1;
597 /* Take over HW learned entry */
598 if (unlikely(fdb
->added_by_external_learn
)) {
599 fdb
->added_by_external_learn
= 0;
602 if (unlikely(fdb_modified
))
603 fdb_notify(br
, fdb
, RTM_NEWNEIGH
);
606 spin_lock(&br
->hash_lock
);
607 if (likely(!fdb_find_rcu(head
, addr
, vid
))) {
608 fdb
= fdb_create(head
, source
, addr
, vid
, 0, 0);
610 if (unlikely(added_by_user
))
611 fdb
->added_by_user
= 1;
612 fdb_notify(br
, fdb
, RTM_NEWNEIGH
);
615 /* else we lose race and someone else inserts
616 * it first, don't bother updating
618 spin_unlock(&br
->hash_lock
);
622 static int fdb_to_nud(const struct net_bridge
*br
,
623 const struct net_bridge_fdb_entry
*fdb
)
626 return NUD_PERMANENT
;
627 else if (fdb
->is_static
)
629 else if (has_expired(br
, fdb
))
632 return NUD_REACHABLE
;
635 static int fdb_fill_info(struct sk_buff
*skb
, const struct net_bridge
*br
,
636 const struct net_bridge_fdb_entry
*fdb
,
637 u32 portid
, u32 seq
, int type
, unsigned int flags
)
639 unsigned long now
= jiffies
;
640 struct nda_cacheinfo ci
;
641 struct nlmsghdr
*nlh
;
644 nlh
= nlmsg_put(skb
, portid
, seq
, type
, sizeof(*ndm
), flags
);
648 ndm
= nlmsg_data(nlh
);
649 ndm
->ndm_family
= AF_BRIDGE
;
652 ndm
->ndm_flags
= fdb
->added_by_external_learn
? NTF_EXT_LEARNED
: 0;
654 ndm
->ndm_ifindex
= fdb
->dst
? fdb
->dst
->dev
->ifindex
: br
->dev
->ifindex
;
655 ndm
->ndm_state
= fdb_to_nud(br
, fdb
);
657 if (nla_put(skb
, NDA_LLADDR
, ETH_ALEN
, &fdb
->addr
))
658 goto nla_put_failure
;
659 if (nla_put_u32(skb
, NDA_MASTER
, br
->dev
->ifindex
))
660 goto nla_put_failure
;
661 ci
.ndm_used
= jiffies_to_clock_t(now
- fdb
->used
);
662 ci
.ndm_confirmed
= 0;
663 ci
.ndm_updated
= jiffies_to_clock_t(now
- fdb
->updated
);
665 if (nla_put(skb
, NDA_CACHEINFO
, sizeof(ci
), &ci
))
666 goto nla_put_failure
;
668 if (fdb
->vlan_id
&& nla_put(skb
, NDA_VLAN
, sizeof(u16
), &fdb
->vlan_id
))
669 goto nla_put_failure
;
675 nlmsg_cancel(skb
, nlh
);
679 static inline size_t fdb_nlmsg_size(void)
681 return NLMSG_ALIGN(sizeof(struct ndmsg
))
682 + nla_total_size(ETH_ALEN
) /* NDA_LLADDR */
683 + nla_total_size(sizeof(u32
)) /* NDA_MASTER */
684 + nla_total_size(sizeof(u16
)) /* NDA_VLAN */
685 + nla_total_size(sizeof(struct nda_cacheinfo
));
688 static void fdb_notify(struct net_bridge
*br
,
689 const struct net_bridge_fdb_entry
*fdb
, int type
)
691 struct net
*net
= dev_net(br
->dev
);
695 skb
= nlmsg_new(fdb_nlmsg_size(), GFP_ATOMIC
);
699 err
= fdb_fill_info(skb
, br
, fdb
, 0, 0, type
, 0);
701 /* -EMSGSIZE implies BUG in fdb_nlmsg_size() */
702 WARN_ON(err
== -EMSGSIZE
);
706 rtnl_notify(skb
, net
, 0, RTNLGRP_NEIGH
, NULL
, GFP_ATOMIC
);
709 rtnl_set_sk_err(net
, RTNLGRP_NEIGH
, err
);
712 /* Dump information about entries, in response to GETNEIGH */
713 int br_fdb_dump(struct sk_buff
*skb
,
714 struct netlink_callback
*cb
,
715 struct net_device
*dev
,
716 struct net_device
*filter_dev
,
719 struct net_bridge
*br
= netdev_priv(dev
);
723 if (!(dev
->priv_flags
& IFF_EBRIDGE
))
727 err
= ndo_dflt_fdb_dump(skb
, cb
, dev
, NULL
, idx
);
732 for (i
= 0; i
< BR_HASH_SIZE
; i
++) {
733 struct net_bridge_fdb_entry
*f
;
735 hlist_for_each_entry_rcu(f
, &br
->hash
[i
], hlist
) {
737 if (*idx
< cb
->args
[2])
741 (!f
->dst
|| f
->dst
->dev
!= filter_dev
)) {
742 if (filter_dev
!= dev
)
744 /* !f->dst is a special case for bridge
745 * It means the MAC belongs to the bridge
746 * Therefore need a little more filtering
747 * we only want to dump the !f->dst case
752 if (!filter_dev
&& f
->dst
)
755 err
= fdb_fill_info(skb
, br
, f
,
756 NETLINK_CB(cb
->skb
).portid
,
771 /* Update (create or replace) forwarding database entry */
772 static int fdb_add_entry(struct net_bridge
*br
, struct net_bridge_port
*source
,
773 const __u8
*addr
, __u16 state
, __u16 flags
, __u16 vid
)
775 struct hlist_head
*head
= &br
->hash
[br_mac_hash(addr
, vid
)];
776 struct net_bridge_fdb_entry
*fdb
;
777 bool modified
= false;
779 /* If the port cannot learn allow only local and static entries */
780 if (source
&& !(state
& NUD_PERMANENT
) && !(state
& NUD_NOARP
) &&
781 !(source
->state
== BR_STATE_LEARNING
||
782 source
->state
== BR_STATE_FORWARDING
))
785 if (!source
&& !(state
& NUD_PERMANENT
)) {
786 pr_info("bridge: RTM_NEWNEIGH %s without NUD_PERMANENT\n",
791 fdb
= br_fdb_find(br
, addr
, vid
);
793 if (!(flags
& NLM_F_CREATE
))
796 fdb
= fdb_create(head
, source
, addr
, vid
, 0, 0);
802 if (flags
& NLM_F_EXCL
)
805 if (fdb
->dst
!= source
) {
811 if (fdb_to_nud(br
, fdb
) != state
) {
812 if (state
& NUD_PERMANENT
) {
814 if (!fdb
->is_static
) {
816 fdb_add_hw_addr(br
, addr
);
818 } else if (state
& NUD_NOARP
) {
820 if (!fdb
->is_static
) {
822 fdb_add_hw_addr(br
, addr
);
826 if (fdb
->is_static
) {
828 fdb_del_hw_addr(br
, addr
);
834 fdb
->added_by_user
= 1;
838 fdb
->updated
= jiffies
;
839 fdb_notify(br
, fdb
, RTM_NEWNEIGH
);
845 static int __br_fdb_add(struct ndmsg
*ndm
, struct net_bridge
*br
,
846 struct net_bridge_port
*p
, const unsigned char *addr
,
847 u16 nlh_flags
, u16 vid
)
851 if (ndm
->ndm_flags
& NTF_USE
) {
853 pr_info("bridge: RTM_NEWNEIGH %s with NTF_USE is not supported\n",
859 br_fdb_update(br
, p
, addr
, vid
, true);
862 } else if (ndm
->ndm_flags
& NTF_EXT_LEARNED
) {
863 err
= br_fdb_external_learn_add(br
, p
, addr
, vid
);
865 spin_lock_bh(&br
->hash_lock
);
866 err
= fdb_add_entry(br
, p
, addr
, ndm
->ndm_state
,
868 spin_unlock_bh(&br
->hash_lock
);
874 /* Add new permanent fdb entry with RTM_NEWNEIGH */
875 int br_fdb_add(struct ndmsg
*ndm
, struct nlattr
*tb
[],
876 struct net_device
*dev
,
877 const unsigned char *addr
, u16 vid
, u16 nlh_flags
)
879 struct net_bridge_vlan_group
*vg
;
880 struct net_bridge_port
*p
= NULL
;
881 struct net_bridge_vlan
*v
;
882 struct net_bridge
*br
= NULL
;
885 if (!(ndm
->ndm_state
& (NUD_PERMANENT
|NUD_NOARP
|NUD_REACHABLE
))) {
886 pr_info("bridge: RTM_NEWNEIGH with invalid state %#x\n", ndm
->ndm_state
);
890 if (is_zero_ether_addr(addr
)) {
891 pr_info("bridge: RTM_NEWNEIGH with invalid ether address\n");
895 if (dev
->priv_flags
& IFF_EBRIDGE
) {
896 br
= netdev_priv(dev
);
897 vg
= br_vlan_group(br
);
899 p
= br_port_get_rtnl(dev
);
901 pr_info("bridge: RTM_NEWNEIGH %s not a bridge port\n",
906 vg
= nbp_vlan_group(p
);
910 v
= br_vlan_find(vg
, vid
);
911 if (!v
|| !br_vlan_should_use(v
)) {
912 pr_info("bridge: RTM_NEWNEIGH with unconfigured vlan %d on %s\n", vid
, dev
->name
);
916 /* VID was specified, so use it. */
917 err
= __br_fdb_add(ndm
, br
, p
, addr
, nlh_flags
, vid
);
919 err
= __br_fdb_add(ndm
, br
, p
, addr
, nlh_flags
, 0);
920 if (err
|| !vg
|| !vg
->num_vlans
)
923 /* We have vlans configured on this port and user didn't
924 * specify a VLAN. To be nice, add/update entry for every
927 list_for_each_entry(v
, &vg
->vlan_list
, vlist
) {
928 if (!br_vlan_should_use(v
))
930 err
= __br_fdb_add(ndm
, br
, p
, addr
, nlh_flags
, v
->vid
);
940 static int fdb_delete_by_addr_and_port(struct net_bridge
*br
,
941 const struct net_bridge_port
*p
,
942 const u8
*addr
, u16 vlan
)
944 struct net_bridge_fdb_entry
*fdb
;
946 fdb
= br_fdb_find(br
, addr
, vlan
);
947 if (!fdb
|| fdb
->dst
!= p
)
955 static int __br_fdb_delete(struct net_bridge
*br
,
956 const struct net_bridge_port
*p
,
957 const unsigned char *addr
, u16 vid
)
961 spin_lock_bh(&br
->hash_lock
);
962 err
= fdb_delete_by_addr_and_port(br
, p
, addr
, vid
);
963 spin_unlock_bh(&br
->hash_lock
);
968 /* Remove neighbor entry with RTM_DELNEIGH */
969 int br_fdb_delete(struct ndmsg
*ndm
, struct nlattr
*tb
[],
970 struct net_device
*dev
,
971 const unsigned char *addr
, u16 vid
)
973 struct net_bridge_vlan_group
*vg
;
974 struct net_bridge_port
*p
= NULL
;
975 struct net_bridge_vlan
*v
;
976 struct net_bridge
*br
;
979 if (dev
->priv_flags
& IFF_EBRIDGE
) {
980 br
= netdev_priv(dev
);
981 vg
= br_vlan_group(br
);
983 p
= br_port_get_rtnl(dev
);
985 pr_info("bridge: RTM_DELNEIGH %s not a bridge port\n",
989 vg
= nbp_vlan_group(p
);
994 v
= br_vlan_find(vg
, vid
);
996 pr_info("bridge: RTM_DELNEIGH with unconfigured vlan %d on %s\n", vid
, dev
->name
);
1000 err
= __br_fdb_delete(br
, p
, addr
, vid
);
1003 err
&= __br_fdb_delete(br
, p
, addr
, 0);
1004 if (!vg
|| !vg
->num_vlans
)
1007 list_for_each_entry(v
, &vg
->vlan_list
, vlist
) {
1008 if (!br_vlan_should_use(v
))
1010 err
&= __br_fdb_delete(br
, p
, addr
, v
->vid
);
1017 int br_fdb_sync_static(struct net_bridge
*br
, struct net_bridge_port
*p
)
1019 struct net_bridge_fdb_entry
*fdb
, *tmp
;
1025 for (i
= 0; i
< BR_HASH_SIZE
; i
++) {
1026 hlist_for_each_entry(fdb
, &br
->hash
[i
], hlist
) {
1027 /* We only care for static entries */
1028 if (!fdb
->is_static
)
1031 err
= dev_uc_add(p
->dev
, fdb
->addr
.addr
);
1039 for (i
= 0; i
< BR_HASH_SIZE
; i
++) {
1040 hlist_for_each_entry(tmp
, &br
->hash
[i
], hlist
) {
1041 /* If we reached the fdb that failed, we can stop */
1045 /* We only care for static entries */
1046 if (!tmp
->is_static
)
1049 dev_uc_del(p
->dev
, tmp
->addr
.addr
);
1055 void br_fdb_unsync_static(struct net_bridge
*br
, struct net_bridge_port
*p
)
1057 struct net_bridge_fdb_entry
*fdb
;
1062 for (i
= 0; i
< BR_HASH_SIZE
; i
++) {
1063 hlist_for_each_entry_rcu(fdb
, &br
->hash
[i
], hlist
) {
1064 /* We only care for static entries */
1065 if (!fdb
->is_static
)
1068 dev_uc_del(p
->dev
, fdb
->addr
.addr
);
1073 int br_fdb_external_learn_add(struct net_bridge
*br
, struct net_bridge_port
*p
,
1074 const unsigned char *addr
, u16 vid
)
1076 struct hlist_head
*head
;
1077 struct net_bridge_fdb_entry
*fdb
;
1081 spin_lock_bh(&br
->hash_lock
);
1083 head
= &br
->hash
[br_mac_hash(addr
, vid
)];
1084 fdb
= br_fdb_find(br
, addr
, vid
);
1086 fdb
= fdb_create(head
, p
, addr
, vid
, 0, 0);
1091 fdb
->added_by_external_learn
= 1;
1092 fdb_notify(br
, fdb
, RTM_NEWNEIGH
);
1093 } else if (fdb
->added_by_external_learn
) {
1095 fdb
->updated
= fdb
->used
= jiffies
;
1096 } else if (!fdb
->added_by_user
) {
1097 /* Take over SW learned entry */
1098 fdb
->added_by_external_learn
= 1;
1099 fdb
->updated
= jiffies
;
1100 fdb_notify(br
, fdb
, RTM_NEWNEIGH
);
1104 spin_unlock_bh(&br
->hash_lock
);
1109 int br_fdb_external_learn_del(struct net_bridge
*br
, struct net_bridge_port
*p
,
1110 const unsigned char *addr
, u16 vid
)
1112 struct net_bridge_fdb_entry
*fdb
;
1116 spin_lock_bh(&br
->hash_lock
);
1118 fdb
= br_fdb_find(br
, addr
, vid
);
1119 if (fdb
&& fdb
->added_by_external_learn
)
1120 fdb_delete(br
, fdb
);
1124 spin_unlock_bh(&br
->hash_lock
);