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 <trace/events/bridge.h>
29 #include "br_private.h"
31 static struct kmem_cache
*br_fdb_cache __read_mostly
;
32 static int fdb_insert(struct net_bridge
*br
, struct net_bridge_port
*source
,
33 const unsigned char *addr
, u16 vid
);
34 static void fdb_notify(struct net_bridge
*br
,
35 const struct net_bridge_fdb_entry
*, int);
37 static u32 fdb_salt __read_mostly
;
39 int __init
br_fdb_init(void)
41 br_fdb_cache
= kmem_cache_create("bridge_fdb_cache",
42 sizeof(struct net_bridge_fdb_entry
),
44 SLAB_HWCACHE_ALIGN
, NULL
);
48 get_random_bytes(&fdb_salt
, sizeof(fdb_salt
));
52 void br_fdb_fini(void)
54 kmem_cache_destroy(br_fdb_cache
);
58 /* if topology_changing then use forward_delay (default 15 sec)
59 * otherwise keep longer (default 5 minutes)
61 static inline unsigned long hold_time(const struct net_bridge
*br
)
63 return br
->topology_change
? br
->forward_delay
: br
->ageing_time
;
66 static inline int has_expired(const struct net_bridge
*br
,
67 const struct net_bridge_fdb_entry
*fdb
)
69 return !fdb
->is_static
&& !fdb
->added_by_external_learn
&&
70 time_before_eq(fdb
->updated
+ hold_time(br
), jiffies
);
73 static inline int br_mac_hash(const unsigned char *mac
, __u16 vid
)
75 /* use 1 byte of OUI and 3 bytes of NIC */
76 u32 key
= get_unaligned((u32
*)(mac
+ 2));
77 return jhash_2words(key
, vid
, fdb_salt
) & (BR_HASH_SIZE
- 1);
80 static void fdb_rcu_free(struct rcu_head
*head
)
82 struct net_bridge_fdb_entry
*ent
83 = container_of(head
, struct net_bridge_fdb_entry
, rcu
);
84 kmem_cache_free(br_fdb_cache
, ent
);
87 static struct net_bridge_fdb_entry
*fdb_find_rcu(struct hlist_head
*head
,
88 const unsigned char *addr
,
91 struct net_bridge_fdb_entry
*f
;
93 WARN_ON_ONCE(!rcu_read_lock_held());
95 hlist_for_each_entry_rcu(f
, head
, hlist
)
96 if (ether_addr_equal(f
->addr
.addr
, addr
) && f
->vlan_id
== vid
)
102 /* requires bridge hash_lock */
103 static struct net_bridge_fdb_entry
*br_fdb_find(struct net_bridge
*br
,
104 const unsigned char *addr
,
107 struct hlist_head
*head
= &br
->hash
[br_mac_hash(addr
, vid
)];
108 struct net_bridge_fdb_entry
*fdb
;
110 lockdep_assert_held_once(&br
->hash_lock
);
113 fdb
= fdb_find_rcu(head
, addr
, vid
);
119 struct net_bridge_fdb_entry
*br_fdb_find_rcu(struct net_bridge
*br
,
120 const unsigned char *addr
,
123 struct hlist_head
*head
= &br
->hash
[br_mac_hash(addr
, vid
)];
125 return fdb_find_rcu(head
, addr
, vid
);
128 /* When a static FDB entry is added, the mac address from the entry is
129 * added to the bridge private HW address list and all required ports
130 * are then updated with the new information.
133 static void fdb_add_hw_addr(struct net_bridge
*br
, const unsigned char *addr
)
136 struct net_bridge_port
*p
;
140 list_for_each_entry(p
, &br
->port_list
, list
) {
141 if (!br_promisc_port(p
)) {
142 err
= dev_uc_add(p
->dev
, addr
);
150 list_for_each_entry_continue_reverse(p
, &br
->port_list
, list
) {
151 if (!br_promisc_port(p
))
152 dev_uc_del(p
->dev
, addr
);
156 /* When a static FDB entry is deleted, the HW address from that entry is
157 * also removed from the bridge private HW address list and updates all
158 * the ports with needed information.
161 static void fdb_del_hw_addr(struct net_bridge
*br
, const unsigned char *addr
)
163 struct net_bridge_port
*p
;
167 list_for_each_entry(p
, &br
->port_list
, list
) {
168 if (!br_promisc_port(p
))
169 dev_uc_del(p
->dev
, addr
);
173 static void fdb_delete(struct net_bridge
*br
, struct net_bridge_fdb_entry
*f
)
175 trace_fdb_delete(br
, f
);
178 fdb_del_hw_addr(br
, f
->addr
.addr
);
180 hlist_del_init_rcu(&f
->hlist
);
181 fdb_notify(br
, f
, RTM_DELNEIGH
);
182 call_rcu(&f
->rcu
, fdb_rcu_free
);
185 /* Delete a local entry if no other port had the same address. */
186 static void fdb_delete_local(struct net_bridge
*br
,
187 const struct net_bridge_port
*p
,
188 struct net_bridge_fdb_entry
*f
)
190 const unsigned char *addr
= f
->addr
.addr
;
191 struct net_bridge_vlan_group
*vg
;
192 const struct net_bridge_vlan
*v
;
193 struct net_bridge_port
*op
;
194 u16 vid
= f
->vlan_id
;
196 /* Maybe another port has same hw addr? */
197 list_for_each_entry(op
, &br
->port_list
, list
) {
198 vg
= nbp_vlan_group(op
);
199 if (op
!= p
&& ether_addr_equal(op
->dev
->dev_addr
, addr
) &&
200 (!vid
|| br_vlan_find(vg
, vid
))) {
202 f
->added_by_user
= 0;
207 vg
= br_vlan_group(br
);
208 v
= br_vlan_find(vg
, vid
);
209 /* Maybe bridge device has same hw addr? */
210 if (p
&& ether_addr_equal(br
->dev
->dev_addr
, addr
) &&
211 (!vid
|| (v
&& br_vlan_should_use(v
)))) {
213 f
->added_by_user
= 0;
220 void br_fdb_find_delete_local(struct net_bridge
*br
,
221 const struct net_bridge_port
*p
,
222 const unsigned char *addr
, u16 vid
)
224 struct net_bridge_fdb_entry
*f
;
226 spin_lock_bh(&br
->hash_lock
);
227 f
= br_fdb_find(br
, addr
, vid
);
228 if (f
&& f
->is_local
&& !f
->added_by_user
&& f
->dst
== p
)
229 fdb_delete_local(br
, p
, f
);
230 spin_unlock_bh(&br
->hash_lock
);
233 void br_fdb_changeaddr(struct net_bridge_port
*p
, const unsigned char *newaddr
)
235 struct net_bridge_vlan_group
*vg
;
236 struct net_bridge
*br
= p
->br
;
237 struct net_bridge_vlan
*v
;
240 spin_lock_bh(&br
->hash_lock
);
242 vg
= nbp_vlan_group(p
);
243 /* Search all chains since old address/hash is unknown */
244 for (i
= 0; i
< BR_HASH_SIZE
; i
++) {
245 struct hlist_node
*h
;
246 hlist_for_each(h
, &br
->hash
[i
]) {
247 struct net_bridge_fdb_entry
*f
;
249 f
= hlist_entry(h
, struct net_bridge_fdb_entry
, hlist
);
250 if (f
->dst
== p
&& f
->is_local
&& !f
->added_by_user
) {
252 fdb_delete_local(br
, p
, f
);
254 /* if this port has no vlan information
255 * configured, we can safely be done at
258 if (!vg
|| !vg
->num_vlans
)
265 /* insert new address, may fail if invalid address or dup. */
266 fdb_insert(br
, p
, newaddr
, 0);
268 if (!vg
|| !vg
->num_vlans
)
271 /* Now add entries for every VLAN configured on the port.
272 * This function runs under RTNL so the bitmap will not change
275 list_for_each_entry(v
, &vg
->vlan_list
, vlist
)
276 fdb_insert(br
, p
, newaddr
, v
->vid
);
279 spin_unlock_bh(&br
->hash_lock
);
282 void br_fdb_change_mac_address(struct net_bridge
*br
, const u8
*newaddr
)
284 struct net_bridge_vlan_group
*vg
;
285 struct net_bridge_fdb_entry
*f
;
286 struct net_bridge_vlan
*v
;
288 spin_lock_bh(&br
->hash_lock
);
290 /* If old entry was unassociated with any port, then delete it. */
291 f
= br_fdb_find(br
, br
->dev
->dev_addr
, 0);
292 if (f
&& f
->is_local
&& !f
->dst
&& !f
->added_by_user
)
293 fdb_delete_local(br
, NULL
, f
);
295 fdb_insert(br
, NULL
, newaddr
, 0);
296 vg
= br_vlan_group(br
);
297 if (!vg
|| !vg
->num_vlans
)
299 /* Now remove and add entries for every VLAN configured on the
300 * bridge. This function runs under RTNL so the bitmap will not
301 * change from under us.
303 list_for_each_entry(v
, &vg
->vlan_list
, vlist
) {
304 if (!br_vlan_should_use(v
))
306 f
= br_fdb_find(br
, br
->dev
->dev_addr
, v
->vid
);
307 if (f
&& f
->is_local
&& !f
->dst
&& !f
->added_by_user
)
308 fdb_delete_local(br
, NULL
, f
);
309 fdb_insert(br
, NULL
, newaddr
, v
->vid
);
312 spin_unlock_bh(&br
->hash_lock
);
315 void br_fdb_cleanup(struct work_struct
*work
)
317 struct net_bridge
*br
= container_of(work
, struct net_bridge
,
319 unsigned long delay
= hold_time(br
);
320 unsigned long work_delay
= delay
;
321 unsigned long now
= jiffies
;
324 for (i
= 0; i
< BR_HASH_SIZE
; i
++) {
325 struct net_bridge_fdb_entry
*f
;
326 struct hlist_node
*n
;
328 if (!br
->hash
[i
].first
)
331 spin_lock_bh(&br
->hash_lock
);
332 hlist_for_each_entry_safe(f
, n
, &br
->hash
[i
], hlist
) {
333 unsigned long this_timer
;
337 if (f
->added_by_external_learn
)
339 this_timer
= f
->updated
+ delay
;
340 if (time_after(this_timer
, now
))
341 work_delay
= min(work_delay
, this_timer
- now
);
345 spin_unlock_bh(&br
->hash_lock
);
349 /* Cleanup minimum 10 milliseconds apart */
350 work_delay
= max_t(unsigned long, work_delay
, msecs_to_jiffies(10));
351 mod_delayed_work(system_long_wq
, &br
->gc_work
, work_delay
);
354 /* Completely flush all dynamic entries in forwarding database.*/
355 void br_fdb_flush(struct net_bridge
*br
)
359 spin_lock_bh(&br
->hash_lock
);
360 for (i
= 0; i
< BR_HASH_SIZE
; i
++) {
361 struct net_bridge_fdb_entry
*f
;
362 struct hlist_node
*n
;
363 hlist_for_each_entry_safe(f
, n
, &br
->hash
[i
], hlist
) {
368 spin_unlock_bh(&br
->hash_lock
);
371 /* Flush all entries referring to a specific port.
372 * if do_all is set also flush static entries
373 * if vid is set delete all entries that match the vlan_id
375 void br_fdb_delete_by_port(struct net_bridge
*br
,
376 const struct net_bridge_port
*p
,
382 spin_lock_bh(&br
->hash_lock
);
383 for (i
= 0; i
< BR_HASH_SIZE
; i
++) {
384 struct hlist_node
*h
, *g
;
386 hlist_for_each_safe(h
, g
, &br
->hash
[i
]) {
387 struct net_bridge_fdb_entry
*f
388 = hlist_entry(h
, struct net_bridge_fdb_entry
, hlist
);
393 if (f
->is_static
|| (vid
&& f
->vlan_id
!= vid
))
397 fdb_delete_local(br
, p
, f
);
402 spin_unlock_bh(&br
->hash_lock
);
405 #if IS_ENABLED(CONFIG_ATM_LANE)
406 /* Interface used by ATM LANE hook to test
407 * if an addr is on some other bridge port */
408 int br_fdb_test_addr(struct net_device
*dev
, unsigned char *addr
)
410 struct net_bridge_fdb_entry
*fdb
;
411 struct net_bridge_port
*port
;
415 port
= br_port_get_rcu(dev
);
419 fdb
= br_fdb_find_rcu(port
->br
, addr
, 0);
420 ret
= fdb
&& fdb
->dst
&& fdb
->dst
->dev
!= dev
&&
421 fdb
->dst
->state
== BR_STATE_FORWARDING
;
427 #endif /* CONFIG_ATM_LANE */
430 * Fill buffer with forwarding table records in
433 int br_fdb_fillbuf(struct net_bridge
*br
, void *buf
,
434 unsigned long maxnum
, unsigned long skip
)
436 struct __fdb_entry
*fe
= buf
;
438 struct net_bridge_fdb_entry
*f
;
440 memset(buf
, 0, maxnum
*sizeof(struct __fdb_entry
));
443 for (i
= 0; i
< BR_HASH_SIZE
; i
++) {
444 hlist_for_each_entry_rcu(f
, &br
->hash
[i
], hlist
) {
448 if (has_expired(br
, f
))
451 /* ignore pseudo entry for local MAC address */
460 /* convert from internal format to API */
461 memcpy(fe
->mac_addr
, f
->addr
.addr
, ETH_ALEN
);
463 /* due to ABI compat need to split into hi/lo */
464 fe
->port_no
= f
->dst
->port_no
;
465 fe
->port_hi
= f
->dst
->port_no
>> 8;
467 fe
->is_local
= f
->is_local
;
469 fe
->ageing_timer_value
= jiffies_delta_to_clock_t(jiffies
- f
->updated
);
481 static struct net_bridge_fdb_entry
*fdb_create(struct hlist_head
*head
,
482 struct net_bridge_port
*source
,
483 const unsigned char *addr
,
485 unsigned char is_local
,
486 unsigned char is_static
)
488 struct net_bridge_fdb_entry
*fdb
;
490 fdb
= kmem_cache_alloc(br_fdb_cache
, GFP_ATOMIC
);
492 memcpy(fdb
->addr
.addr
, addr
, ETH_ALEN
);
495 fdb
->is_local
= is_local
;
496 fdb
->is_static
= is_static
;
497 fdb
->added_by_user
= 0;
498 fdb
->added_by_external_learn
= 0;
500 fdb
->updated
= fdb
->used
= jiffies
;
501 hlist_add_head_rcu(&fdb
->hlist
, head
);
506 static int fdb_insert(struct net_bridge
*br
, struct net_bridge_port
*source
,
507 const unsigned char *addr
, u16 vid
)
509 struct hlist_head
*head
= &br
->hash
[br_mac_hash(addr
, vid
)];
510 struct net_bridge_fdb_entry
*fdb
;
512 if (!is_valid_ether_addr(addr
))
515 fdb
= br_fdb_find(br
, addr
, vid
);
517 /* it is okay to have multiple ports with same
518 * address, just use the first one.
522 br_warn(br
, "adding interface %s with same address as a received packet (addr:%pM, vlan:%u)\n",
523 source
? source
->dev
->name
: br
->dev
->name
, addr
, vid
);
527 fdb
= fdb_create(head
, source
, addr
, vid
, 1, 1);
531 fdb_add_hw_addr(br
, addr
);
532 fdb_notify(br
, fdb
, RTM_NEWNEIGH
);
536 /* Add entry for local address of interface */
537 int br_fdb_insert(struct net_bridge
*br
, struct net_bridge_port
*source
,
538 const unsigned char *addr
, u16 vid
)
542 spin_lock_bh(&br
->hash_lock
);
543 ret
= fdb_insert(br
, source
, addr
, vid
);
544 spin_unlock_bh(&br
->hash_lock
);
548 void br_fdb_update(struct net_bridge
*br
, struct net_bridge_port
*source
,
549 const unsigned char *addr
, u16 vid
, bool added_by_user
)
551 struct hlist_head
*head
= &br
->hash
[br_mac_hash(addr
, vid
)];
552 struct net_bridge_fdb_entry
*fdb
;
553 bool fdb_modified
= false;
555 /* some users want to always flood. */
556 if (hold_time(br
) == 0)
559 /* ignore packets unless we are using this port */
560 if (!(source
->state
== BR_STATE_LEARNING
||
561 source
->state
== BR_STATE_FORWARDING
))
564 fdb
= fdb_find_rcu(head
, addr
, vid
);
566 /* attempt to update an entry for a local interface */
567 if (unlikely(fdb
->is_local
)) {
569 br_warn(br
, "received packet on %s with own address as source address (addr:%pM, vlan:%u)\n",
570 source
->dev
->name
, addr
, vid
);
572 unsigned long now
= jiffies
;
574 /* fastpath: update of existing entry */
575 if (unlikely(source
!= fdb
->dst
)) {
578 /* Take over HW learned entry */
579 if (unlikely(fdb
->added_by_external_learn
))
580 fdb
->added_by_external_learn
= 0;
582 if (now
!= fdb
->updated
)
584 if (unlikely(added_by_user
))
585 fdb
->added_by_user
= 1;
586 if (unlikely(fdb_modified
)) {
587 trace_br_fdb_update(br
, source
, addr
, vid
, added_by_user
);
588 fdb_notify(br
, fdb
, RTM_NEWNEIGH
);
592 spin_lock(&br
->hash_lock
);
593 if (likely(!fdb_find_rcu(head
, addr
, vid
))) {
594 fdb
= fdb_create(head
, source
, addr
, vid
, 0, 0);
596 if (unlikely(added_by_user
))
597 fdb
->added_by_user
= 1;
598 trace_br_fdb_update(br
, source
, addr
, vid
, added_by_user
);
599 fdb_notify(br
, fdb
, RTM_NEWNEIGH
);
602 /* else we lose race and someone else inserts
603 * it first, don't bother updating
605 spin_unlock(&br
->hash_lock
);
609 static int fdb_to_nud(const struct net_bridge
*br
,
610 const struct net_bridge_fdb_entry
*fdb
)
613 return NUD_PERMANENT
;
614 else if (fdb
->is_static
)
616 else if (has_expired(br
, fdb
))
619 return NUD_REACHABLE
;
622 static int fdb_fill_info(struct sk_buff
*skb
, const struct net_bridge
*br
,
623 const struct net_bridge_fdb_entry
*fdb
,
624 u32 portid
, u32 seq
, int type
, unsigned int flags
)
626 unsigned long now
= jiffies
;
627 struct nda_cacheinfo ci
;
628 struct nlmsghdr
*nlh
;
631 nlh
= nlmsg_put(skb
, portid
, seq
, type
, sizeof(*ndm
), flags
);
635 ndm
= nlmsg_data(nlh
);
636 ndm
->ndm_family
= AF_BRIDGE
;
641 ndm
->ndm_ifindex
= fdb
->dst
? fdb
->dst
->dev
->ifindex
: br
->dev
->ifindex
;
642 ndm
->ndm_state
= fdb_to_nud(br
, fdb
);
645 ndm
->ndm_flags
|= NTF_OFFLOADED
;
646 if (fdb
->added_by_external_learn
)
647 ndm
->ndm_flags
|= NTF_EXT_LEARNED
;
649 if (nla_put(skb
, NDA_LLADDR
, ETH_ALEN
, &fdb
->addr
))
650 goto nla_put_failure
;
651 if (nla_put_u32(skb
, NDA_MASTER
, br
->dev
->ifindex
))
652 goto nla_put_failure
;
653 ci
.ndm_used
= jiffies_to_clock_t(now
- fdb
->used
);
654 ci
.ndm_confirmed
= 0;
655 ci
.ndm_updated
= jiffies_to_clock_t(now
- fdb
->updated
);
657 if (nla_put(skb
, NDA_CACHEINFO
, sizeof(ci
), &ci
))
658 goto nla_put_failure
;
660 if (fdb
->vlan_id
&& nla_put(skb
, NDA_VLAN
, sizeof(u16
), &fdb
->vlan_id
))
661 goto nla_put_failure
;
667 nlmsg_cancel(skb
, nlh
);
671 static inline size_t fdb_nlmsg_size(void)
673 return NLMSG_ALIGN(sizeof(struct ndmsg
))
674 + nla_total_size(ETH_ALEN
) /* NDA_LLADDR */
675 + nla_total_size(sizeof(u32
)) /* NDA_MASTER */
676 + nla_total_size(sizeof(u16
)) /* NDA_VLAN */
677 + nla_total_size(sizeof(struct nda_cacheinfo
));
680 static void fdb_notify(struct net_bridge
*br
,
681 const struct net_bridge_fdb_entry
*fdb
, int type
)
683 struct net
*net
= dev_net(br
->dev
);
687 br_switchdev_fdb_notify(fdb
, type
);
689 skb
= nlmsg_new(fdb_nlmsg_size(), GFP_ATOMIC
);
693 err
= fdb_fill_info(skb
, br
, fdb
, 0, 0, type
, 0);
695 /* -EMSGSIZE implies BUG in fdb_nlmsg_size() */
696 WARN_ON(err
== -EMSGSIZE
);
700 rtnl_notify(skb
, net
, 0, RTNLGRP_NEIGH
, NULL
, GFP_ATOMIC
);
703 rtnl_set_sk_err(net
, RTNLGRP_NEIGH
, err
);
706 /* Dump information about entries, in response to GETNEIGH */
707 int br_fdb_dump(struct sk_buff
*skb
,
708 struct netlink_callback
*cb
,
709 struct net_device
*dev
,
710 struct net_device
*filter_dev
,
713 struct net_bridge
*br
= netdev_priv(dev
);
717 if (!(dev
->priv_flags
& IFF_EBRIDGE
))
721 err
= ndo_dflt_fdb_dump(skb
, cb
, dev
, NULL
, idx
);
726 for (i
= 0; i
< BR_HASH_SIZE
; i
++) {
727 struct net_bridge_fdb_entry
*f
;
729 hlist_for_each_entry_rcu(f
, &br
->hash
[i
], hlist
) {
731 if (*idx
< cb
->args
[2])
735 (!f
->dst
|| f
->dst
->dev
!= filter_dev
)) {
736 if (filter_dev
!= dev
)
738 /* !f->dst is a special case for bridge
739 * It means the MAC belongs to the bridge
740 * Therefore need a little more filtering
741 * we only want to dump the !f->dst case
746 if (!filter_dev
&& f
->dst
)
749 err
= fdb_fill_info(skb
, br
, f
,
750 NETLINK_CB(cb
->skb
).portid
,
765 /* Update (create or replace) forwarding database entry */
766 static int fdb_add_entry(struct net_bridge
*br
, struct net_bridge_port
*source
,
767 const __u8
*addr
, __u16 state
, __u16 flags
, __u16 vid
)
769 struct hlist_head
*head
= &br
->hash
[br_mac_hash(addr
, vid
)];
770 struct net_bridge_fdb_entry
*fdb
;
771 bool modified
= false;
773 /* If the port cannot learn allow only local and static entries */
774 if (source
&& !(state
& NUD_PERMANENT
) && !(state
& NUD_NOARP
) &&
775 !(source
->state
== BR_STATE_LEARNING
||
776 source
->state
== BR_STATE_FORWARDING
))
779 if (!source
&& !(state
& NUD_PERMANENT
)) {
780 pr_info("bridge: RTM_NEWNEIGH %s without NUD_PERMANENT\n",
785 fdb
= br_fdb_find(br
, addr
, vid
);
787 if (!(flags
& NLM_F_CREATE
))
790 fdb
= fdb_create(head
, source
, addr
, vid
, 0, 0);
796 if (flags
& NLM_F_EXCL
)
799 if (fdb
->dst
!= source
) {
805 if (fdb_to_nud(br
, fdb
) != state
) {
806 if (state
& NUD_PERMANENT
) {
808 if (!fdb
->is_static
) {
810 fdb_add_hw_addr(br
, addr
);
812 } else if (state
& NUD_NOARP
) {
814 if (!fdb
->is_static
) {
816 fdb_add_hw_addr(br
, addr
);
820 if (fdb
->is_static
) {
822 fdb_del_hw_addr(br
, addr
);
828 fdb
->added_by_user
= 1;
832 fdb
->updated
= jiffies
;
833 fdb_notify(br
, fdb
, RTM_NEWNEIGH
);
839 static int __br_fdb_add(struct ndmsg
*ndm
, struct net_bridge
*br
,
840 struct net_bridge_port
*p
, const unsigned char *addr
,
841 u16 nlh_flags
, u16 vid
)
845 if (ndm
->ndm_flags
& NTF_USE
) {
847 pr_info("bridge: RTM_NEWNEIGH %s with NTF_USE is not supported\n",
853 br_fdb_update(br
, p
, addr
, vid
, true);
856 } else if (ndm
->ndm_flags
& NTF_EXT_LEARNED
) {
857 err
= br_fdb_external_learn_add(br
, p
, addr
, vid
);
859 spin_lock_bh(&br
->hash_lock
);
860 err
= fdb_add_entry(br
, p
, addr
, ndm
->ndm_state
,
862 spin_unlock_bh(&br
->hash_lock
);
868 /* Add new permanent fdb entry with RTM_NEWNEIGH */
869 int br_fdb_add(struct ndmsg
*ndm
, struct nlattr
*tb
[],
870 struct net_device
*dev
,
871 const unsigned char *addr
, u16 vid
, u16 nlh_flags
)
873 struct net_bridge_vlan_group
*vg
;
874 struct net_bridge_port
*p
= NULL
;
875 struct net_bridge_vlan
*v
;
876 struct net_bridge
*br
= NULL
;
879 trace_br_fdb_add(ndm
, dev
, addr
, vid
, nlh_flags
);
881 if (!(ndm
->ndm_state
& (NUD_PERMANENT
|NUD_NOARP
|NUD_REACHABLE
))) {
882 pr_info("bridge: RTM_NEWNEIGH with invalid state %#x\n", ndm
->ndm_state
);
886 if (is_zero_ether_addr(addr
)) {
887 pr_info("bridge: RTM_NEWNEIGH with invalid ether address\n");
891 if (dev
->priv_flags
& IFF_EBRIDGE
) {
892 br
= netdev_priv(dev
);
893 vg
= br_vlan_group(br
);
895 p
= br_port_get_rtnl(dev
);
897 pr_info("bridge: RTM_NEWNEIGH %s not a bridge port\n",
902 vg
= nbp_vlan_group(p
);
906 v
= br_vlan_find(vg
, vid
);
907 if (!v
|| !br_vlan_should_use(v
)) {
908 pr_info("bridge: RTM_NEWNEIGH with unconfigured vlan %d on %s\n", vid
, dev
->name
);
912 /* VID was specified, so use it. */
913 err
= __br_fdb_add(ndm
, br
, p
, addr
, nlh_flags
, vid
);
915 err
= __br_fdb_add(ndm
, br
, p
, addr
, nlh_flags
, 0);
916 if (err
|| !vg
|| !vg
->num_vlans
)
919 /* We have vlans configured on this port and user didn't
920 * specify a VLAN. To be nice, add/update entry for every
923 list_for_each_entry(v
, &vg
->vlan_list
, vlist
) {
924 if (!br_vlan_should_use(v
))
926 err
= __br_fdb_add(ndm
, br
, p
, addr
, nlh_flags
, v
->vid
);
936 static int fdb_delete_by_addr_and_port(struct net_bridge
*br
,
937 const struct net_bridge_port
*p
,
938 const u8
*addr
, u16 vlan
)
940 struct net_bridge_fdb_entry
*fdb
;
942 fdb
= br_fdb_find(br
, addr
, vlan
);
943 if (!fdb
|| fdb
->dst
!= p
)
951 static int __br_fdb_delete(struct net_bridge
*br
,
952 const struct net_bridge_port
*p
,
953 const unsigned char *addr
, u16 vid
)
957 spin_lock_bh(&br
->hash_lock
);
958 err
= fdb_delete_by_addr_and_port(br
, p
, addr
, vid
);
959 spin_unlock_bh(&br
->hash_lock
);
964 /* Remove neighbor entry with RTM_DELNEIGH */
965 int br_fdb_delete(struct ndmsg
*ndm
, struct nlattr
*tb
[],
966 struct net_device
*dev
,
967 const unsigned char *addr
, u16 vid
)
969 struct net_bridge_vlan_group
*vg
;
970 struct net_bridge_port
*p
= NULL
;
971 struct net_bridge_vlan
*v
;
972 struct net_bridge
*br
;
975 if (dev
->priv_flags
& IFF_EBRIDGE
) {
976 br
= netdev_priv(dev
);
977 vg
= br_vlan_group(br
);
979 p
= br_port_get_rtnl(dev
);
981 pr_info("bridge: RTM_DELNEIGH %s not a bridge port\n",
985 vg
= nbp_vlan_group(p
);
990 v
= br_vlan_find(vg
, vid
);
992 pr_info("bridge: RTM_DELNEIGH with unconfigured vlan %d on %s\n", vid
, dev
->name
);
996 err
= __br_fdb_delete(br
, p
, addr
, vid
);
999 err
&= __br_fdb_delete(br
, p
, addr
, 0);
1000 if (!vg
|| !vg
->num_vlans
)
1003 list_for_each_entry(v
, &vg
->vlan_list
, vlist
) {
1004 if (!br_vlan_should_use(v
))
1006 err
&= __br_fdb_delete(br
, p
, addr
, v
->vid
);
1013 int br_fdb_sync_static(struct net_bridge
*br
, struct net_bridge_port
*p
)
1015 struct net_bridge_fdb_entry
*fdb
, *tmp
;
1021 for (i
= 0; i
< BR_HASH_SIZE
; i
++) {
1022 hlist_for_each_entry(fdb
, &br
->hash
[i
], hlist
) {
1023 /* We only care for static entries */
1024 if (!fdb
->is_static
)
1027 err
= dev_uc_add(p
->dev
, fdb
->addr
.addr
);
1035 for (i
= 0; i
< BR_HASH_SIZE
; i
++) {
1036 hlist_for_each_entry(tmp
, &br
->hash
[i
], hlist
) {
1037 /* If we reached the fdb that failed, we can stop */
1041 /* We only care for static entries */
1042 if (!tmp
->is_static
)
1045 dev_uc_del(p
->dev
, tmp
->addr
.addr
);
1051 void br_fdb_unsync_static(struct net_bridge
*br
, struct net_bridge_port
*p
)
1053 struct net_bridge_fdb_entry
*fdb
;
1058 for (i
= 0; i
< BR_HASH_SIZE
; i
++) {
1059 hlist_for_each_entry_rcu(fdb
, &br
->hash
[i
], hlist
) {
1060 /* We only care for static entries */
1061 if (!fdb
->is_static
)
1064 dev_uc_del(p
->dev
, fdb
->addr
.addr
);
1069 int br_fdb_external_learn_add(struct net_bridge
*br
, struct net_bridge_port
*p
,
1070 const unsigned char *addr
, u16 vid
)
1072 struct net_bridge_fdb_entry
*fdb
;
1073 struct hlist_head
*head
;
1074 bool modified
= false;
1077 trace_br_fdb_external_learn_add(br
, p
, addr
, vid
);
1079 spin_lock_bh(&br
->hash_lock
);
1081 head
= &br
->hash
[br_mac_hash(addr
, vid
)];
1082 fdb
= br_fdb_find(br
, addr
, vid
);
1084 fdb
= fdb_create(head
, p
, addr
, vid
, 0, 0);
1089 fdb
->added_by_external_learn
= 1;
1090 fdb_notify(br
, fdb
, RTM_NEWNEIGH
);
1092 fdb
->updated
= jiffies
;
1094 if (fdb
->dst
!= p
) {
1099 if (fdb
->added_by_external_learn
) {
1101 fdb
->used
= jiffies
;
1102 } else if (!fdb
->added_by_user
) {
1103 /* Take over SW learned entry */
1104 fdb
->added_by_external_learn
= 1;
1109 fdb_notify(br
, fdb
, RTM_NEWNEIGH
);
1113 spin_unlock_bh(&br
->hash_lock
);
1118 int br_fdb_external_learn_del(struct net_bridge
*br
, struct net_bridge_port
*p
,
1119 const unsigned char *addr
, u16 vid
)
1121 struct net_bridge_fdb_entry
*fdb
;
1124 spin_lock_bh(&br
->hash_lock
);
1126 fdb
= br_fdb_find(br
, addr
, vid
);
1127 if (fdb
&& fdb
->added_by_external_learn
)
1128 fdb_delete(br
, fdb
);
1132 spin_unlock_bh(&br
->hash_lock
);
1137 void br_fdb_offloaded_set(struct net_bridge
*br
, struct net_bridge_port
*p
,
1138 const unsigned char *addr
, u16 vid
)
1140 struct net_bridge_fdb_entry
*fdb
;
1142 spin_lock_bh(&br
->hash_lock
);
1144 fdb
= br_fdb_find(br
, addr
, vid
);
1148 spin_unlock_bh(&br
->hash_lock
);