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 const struct rhashtable_params br_fdb_rht_params
= {
32 .head_offset
= offsetof(struct net_bridge_fdb_entry
, rhnode
),
33 .key_offset
= offsetof(struct net_bridge_fdb_entry
, key
),
34 .key_len
= sizeof(struct net_bridge_fdb_key
),
35 .automatic_shrinking
= true,
39 static struct kmem_cache
*br_fdb_cache __read_mostly
;
40 static int fdb_insert(struct net_bridge
*br
, struct net_bridge_port
*source
,
41 const unsigned char *addr
, u16 vid
);
42 static void fdb_notify(struct net_bridge
*br
,
43 const struct net_bridge_fdb_entry
*, int);
45 int __init
br_fdb_init(void)
47 br_fdb_cache
= kmem_cache_create("bridge_fdb_cache",
48 sizeof(struct net_bridge_fdb_entry
),
50 SLAB_HWCACHE_ALIGN
, NULL
);
57 void br_fdb_fini(void)
59 kmem_cache_destroy(br_fdb_cache
);
62 int br_fdb_hash_init(struct net_bridge
*br
)
64 return rhashtable_init(&br
->fdb_hash_tbl
, &br_fdb_rht_params
);
67 void br_fdb_hash_fini(struct net_bridge
*br
)
69 rhashtable_destroy(&br
->fdb_hash_tbl
);
72 /* if topology_changing then use forward_delay (default 15 sec)
73 * otherwise keep longer (default 5 minutes)
75 static inline unsigned long hold_time(const struct net_bridge
*br
)
77 return br
->topology_change
? br
->forward_delay
: br
->ageing_time
;
80 static inline int has_expired(const struct net_bridge
*br
,
81 const struct net_bridge_fdb_entry
*fdb
)
83 return !fdb
->is_static
&& !fdb
->added_by_external_learn
&&
84 time_before_eq(fdb
->updated
+ hold_time(br
), jiffies
);
87 static void fdb_rcu_free(struct rcu_head
*head
)
89 struct net_bridge_fdb_entry
*ent
90 = container_of(head
, struct net_bridge_fdb_entry
, rcu
);
91 kmem_cache_free(br_fdb_cache
, ent
);
94 static struct net_bridge_fdb_entry
*fdb_find_rcu(struct rhashtable
*tbl
,
95 const unsigned char *addr
,
98 struct net_bridge_fdb_key key
;
100 WARN_ON_ONCE(!rcu_read_lock_held());
103 memcpy(key
.addr
.addr
, addr
, sizeof(key
.addr
.addr
));
105 return rhashtable_lookup(tbl
, &key
, br_fdb_rht_params
);
108 /* requires bridge hash_lock */
109 static struct net_bridge_fdb_entry
*br_fdb_find(struct net_bridge
*br
,
110 const unsigned char *addr
,
113 struct net_bridge_fdb_entry
*fdb
;
115 lockdep_assert_held_once(&br
->hash_lock
);
118 fdb
= fdb_find_rcu(&br
->fdb_hash_tbl
, addr
, vid
);
124 struct net_bridge_fdb_entry
*br_fdb_find_rcu(struct net_bridge
*br
,
125 const unsigned char *addr
,
128 return fdb_find_rcu(&br
->fdb_hash_tbl
, addr
, vid
);
131 /* When a static FDB entry is added, the mac address from the entry is
132 * added to the bridge private HW address list and all required ports
133 * are then updated with the new information.
136 static void fdb_add_hw_addr(struct net_bridge
*br
, const unsigned char *addr
)
139 struct net_bridge_port
*p
;
143 list_for_each_entry(p
, &br
->port_list
, list
) {
144 if (!br_promisc_port(p
)) {
145 err
= dev_uc_add(p
->dev
, addr
);
153 list_for_each_entry_continue_reverse(p
, &br
->port_list
, list
) {
154 if (!br_promisc_port(p
))
155 dev_uc_del(p
->dev
, addr
);
159 /* When a static FDB entry is deleted, the HW address from that entry is
160 * also removed from the bridge private HW address list and updates all
161 * the ports with needed information.
164 static void fdb_del_hw_addr(struct net_bridge
*br
, const unsigned char *addr
)
166 struct net_bridge_port
*p
;
170 list_for_each_entry(p
, &br
->port_list
, list
) {
171 if (!br_promisc_port(p
))
172 dev_uc_del(p
->dev
, addr
);
176 static void fdb_delete(struct net_bridge
*br
, struct net_bridge_fdb_entry
*f
)
178 trace_fdb_delete(br
, f
);
181 fdb_del_hw_addr(br
, f
->key
.addr
.addr
);
183 hlist_del_init_rcu(&f
->fdb_node
);
184 rhashtable_remove_fast(&br
->fdb_hash_tbl
, &f
->rhnode
,
186 fdb_notify(br
, f
, RTM_DELNEIGH
);
187 call_rcu(&f
->rcu
, fdb_rcu_free
);
190 /* Delete a local entry if no other port had the same address. */
191 static void fdb_delete_local(struct net_bridge
*br
,
192 const struct net_bridge_port
*p
,
193 struct net_bridge_fdb_entry
*f
)
195 const unsigned char *addr
= f
->key
.addr
.addr
;
196 struct net_bridge_vlan_group
*vg
;
197 const struct net_bridge_vlan
*v
;
198 struct net_bridge_port
*op
;
199 u16 vid
= f
->key
.vlan_id
;
201 /* Maybe another port has same hw addr? */
202 list_for_each_entry(op
, &br
->port_list
, list
) {
203 vg
= nbp_vlan_group(op
);
204 if (op
!= p
&& ether_addr_equal(op
->dev
->dev_addr
, addr
) &&
205 (!vid
|| br_vlan_find(vg
, vid
))) {
207 f
->added_by_user
= 0;
212 vg
= br_vlan_group(br
);
213 v
= br_vlan_find(vg
, vid
);
214 /* Maybe bridge device has same hw addr? */
215 if (p
&& ether_addr_equal(br
->dev
->dev_addr
, addr
) &&
216 (!vid
|| (v
&& br_vlan_should_use(v
)))) {
218 f
->added_by_user
= 0;
225 void br_fdb_find_delete_local(struct net_bridge
*br
,
226 const struct net_bridge_port
*p
,
227 const unsigned char *addr
, u16 vid
)
229 struct net_bridge_fdb_entry
*f
;
231 spin_lock_bh(&br
->hash_lock
);
232 f
= br_fdb_find(br
, addr
, vid
);
233 if (f
&& f
->is_local
&& !f
->added_by_user
&& f
->dst
== p
)
234 fdb_delete_local(br
, p
, f
);
235 spin_unlock_bh(&br
->hash_lock
);
238 void br_fdb_changeaddr(struct net_bridge_port
*p
, const unsigned char *newaddr
)
240 struct net_bridge_vlan_group
*vg
;
241 struct net_bridge_fdb_entry
*f
;
242 struct net_bridge
*br
= p
->br
;
243 struct net_bridge_vlan
*v
;
245 spin_lock_bh(&br
->hash_lock
);
246 vg
= nbp_vlan_group(p
);
247 hlist_for_each_entry(f
, &br
->fdb_list
, fdb_node
) {
248 if (f
->dst
== p
&& f
->is_local
&& !f
->added_by_user
) {
250 fdb_delete_local(br
, p
, f
);
252 /* if this port has no vlan information
253 * configured, we can safely be done at
256 if (!vg
|| !vg
->num_vlans
)
262 /* insert new address, may fail if invalid address or dup. */
263 fdb_insert(br
, p
, newaddr
, 0);
265 if (!vg
|| !vg
->num_vlans
)
268 /* Now add entries for every VLAN configured on the port.
269 * This function runs under RTNL so the bitmap will not change
272 list_for_each_entry(v
, &vg
->vlan_list
, vlist
)
273 fdb_insert(br
, p
, newaddr
, v
->vid
);
276 spin_unlock_bh(&br
->hash_lock
);
279 void br_fdb_change_mac_address(struct net_bridge
*br
, const u8
*newaddr
)
281 struct net_bridge_vlan_group
*vg
;
282 struct net_bridge_fdb_entry
*f
;
283 struct net_bridge_vlan
*v
;
285 spin_lock_bh(&br
->hash_lock
);
287 /* If old entry was unassociated with any port, then delete it. */
288 f
= br_fdb_find(br
, br
->dev
->dev_addr
, 0);
289 if (f
&& f
->is_local
&& !f
->dst
&& !f
->added_by_user
)
290 fdb_delete_local(br
, NULL
, f
);
292 fdb_insert(br
, NULL
, newaddr
, 0);
293 vg
= br_vlan_group(br
);
294 if (!vg
|| !vg
->num_vlans
)
296 /* Now remove and add entries for every VLAN configured on the
297 * bridge. This function runs under RTNL so the bitmap will not
298 * change from under us.
300 list_for_each_entry(v
, &vg
->vlan_list
, vlist
) {
301 if (!br_vlan_should_use(v
))
303 f
= br_fdb_find(br
, br
->dev
->dev_addr
, v
->vid
);
304 if (f
&& f
->is_local
&& !f
->dst
&& !f
->added_by_user
)
305 fdb_delete_local(br
, NULL
, f
);
306 fdb_insert(br
, NULL
, newaddr
, v
->vid
);
309 spin_unlock_bh(&br
->hash_lock
);
312 void br_fdb_cleanup(struct work_struct
*work
)
314 struct net_bridge
*br
= container_of(work
, struct net_bridge
,
316 struct net_bridge_fdb_entry
*f
= NULL
;
317 unsigned long delay
= hold_time(br
);
318 unsigned long work_delay
= delay
;
319 unsigned long now
= jiffies
;
321 /* this part is tricky, in order to avoid blocking learning and
322 * consequently forwarding, we rely on rcu to delete objects with
323 * delayed freeing allowing us to continue traversing
326 hlist_for_each_entry_rcu(f
, &br
->fdb_list
, fdb_node
) {
327 unsigned long this_timer
;
329 if (f
->is_static
|| f
->added_by_external_learn
)
331 this_timer
= f
->updated
+ delay
;
332 if (time_after(this_timer
, now
)) {
333 work_delay
= min(work_delay
, this_timer
- now
);
335 spin_lock_bh(&br
->hash_lock
);
336 if (!hlist_unhashed(&f
->fdb_node
))
338 spin_unlock_bh(&br
->hash_lock
);
343 /* Cleanup minimum 10 milliseconds apart */
344 work_delay
= max_t(unsigned long, work_delay
, msecs_to_jiffies(10));
345 mod_delayed_work(system_long_wq
, &br
->gc_work
, work_delay
);
348 /* Completely flush all dynamic entries in forwarding database.*/
349 void br_fdb_flush(struct net_bridge
*br
)
351 struct net_bridge_fdb_entry
*f
;
352 struct hlist_node
*tmp
;
354 spin_lock_bh(&br
->hash_lock
);
355 hlist_for_each_entry_safe(f
, tmp
, &br
->fdb_list
, fdb_node
) {
359 spin_unlock_bh(&br
->hash_lock
);
362 /* Flush all entries referring to a specific port.
363 * if do_all is set also flush static entries
364 * if vid is set delete all entries that match the vlan_id
366 void br_fdb_delete_by_port(struct net_bridge
*br
,
367 const struct net_bridge_port
*p
,
371 struct net_bridge_fdb_entry
*f
;
372 struct hlist_node
*tmp
;
374 spin_lock_bh(&br
->hash_lock
);
375 hlist_for_each_entry_safe(f
, tmp
, &br
->fdb_list
, fdb_node
) {
380 if (f
->is_static
|| (vid
&& f
->key
.vlan_id
!= vid
))
384 fdb_delete_local(br
, p
, f
);
388 spin_unlock_bh(&br
->hash_lock
);
391 #if IS_ENABLED(CONFIG_ATM_LANE)
392 /* Interface used by ATM LANE hook to test
393 * if an addr is on some other bridge port */
394 int br_fdb_test_addr(struct net_device
*dev
, unsigned char *addr
)
396 struct net_bridge_fdb_entry
*fdb
;
397 struct net_bridge_port
*port
;
401 port
= br_port_get_rcu(dev
);
405 fdb
= br_fdb_find_rcu(port
->br
, addr
, 0);
406 ret
= fdb
&& fdb
->dst
&& fdb
->dst
->dev
!= dev
&&
407 fdb
->dst
->state
== BR_STATE_FORWARDING
;
413 #endif /* CONFIG_ATM_LANE */
416 * Fill buffer with forwarding table records in
419 int br_fdb_fillbuf(struct net_bridge
*br
, void *buf
,
420 unsigned long maxnum
, unsigned long skip
)
422 struct net_bridge_fdb_entry
*f
;
423 struct __fdb_entry
*fe
= buf
;
426 memset(buf
, 0, maxnum
*sizeof(struct __fdb_entry
));
429 hlist_for_each_entry_rcu(f
, &br
->fdb_list
, fdb_node
) {
433 if (has_expired(br
, f
))
436 /* ignore pseudo entry for local MAC address */
445 /* convert from internal format to API */
446 memcpy(fe
->mac_addr
, f
->key
.addr
.addr
, ETH_ALEN
);
448 /* due to ABI compat need to split into hi/lo */
449 fe
->port_no
= f
->dst
->port_no
;
450 fe
->port_hi
= f
->dst
->port_no
>> 8;
452 fe
->is_local
= f
->is_local
;
454 fe
->ageing_timer_value
= jiffies_delta_to_clock_t(jiffies
- f
->updated
);
463 static struct net_bridge_fdb_entry
*fdb_create(struct net_bridge
*br
,
464 struct net_bridge_port
*source
,
465 const unsigned char *addr
,
467 unsigned char is_local
,
468 unsigned char is_static
)
470 struct net_bridge_fdb_entry
*fdb
;
472 fdb
= kmem_cache_alloc(br_fdb_cache
, GFP_ATOMIC
);
474 memcpy(fdb
->key
.addr
.addr
, addr
, ETH_ALEN
);
476 fdb
->key
.vlan_id
= vid
;
477 fdb
->is_local
= is_local
;
478 fdb
->is_static
= is_static
;
479 fdb
->added_by_user
= 0;
480 fdb
->added_by_external_learn
= 0;
482 fdb
->updated
= fdb
->used
= jiffies
;
483 if (rhashtable_lookup_insert_fast(&br
->fdb_hash_tbl
,
485 br_fdb_rht_params
)) {
486 kmem_cache_free(br_fdb_cache
, fdb
);
489 hlist_add_head_rcu(&fdb
->fdb_node
, &br
->fdb_list
);
495 static int fdb_insert(struct net_bridge
*br
, struct net_bridge_port
*source
,
496 const unsigned char *addr
, u16 vid
)
498 struct net_bridge_fdb_entry
*fdb
;
500 if (!is_valid_ether_addr(addr
))
503 fdb
= br_fdb_find(br
, addr
, vid
);
505 /* it is okay to have multiple ports with same
506 * address, just use the first one.
510 br_warn(br
, "adding interface %s with same address as a received packet (addr:%pM, vlan:%u)\n",
511 source
? source
->dev
->name
: br
->dev
->name
, addr
, vid
);
515 fdb
= fdb_create(br
, source
, addr
, vid
, 1, 1);
519 fdb_add_hw_addr(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 net_bridge_fdb_entry
*fdb
;
540 bool fdb_modified
= false;
542 /* some users want to always flood. */
543 if (hold_time(br
) == 0)
546 /* ignore packets unless we are using this port */
547 if (!(source
->state
== BR_STATE_LEARNING
||
548 source
->state
== BR_STATE_FORWARDING
))
551 fdb
= fdb_find_rcu(&br
->fdb_hash_tbl
, addr
, vid
);
553 /* attempt to update an entry for a local interface */
554 if (unlikely(fdb
->is_local
)) {
556 br_warn(br
, "received packet on %s with own address as source address (addr:%pM, vlan:%u)\n",
557 source
->dev
->name
, addr
, vid
);
559 unsigned long now
= jiffies
;
561 /* fastpath: update of existing entry */
562 if (unlikely(source
!= fdb
->dst
)) {
565 /* Take over HW learned entry */
566 if (unlikely(fdb
->added_by_external_learn
))
567 fdb
->added_by_external_learn
= 0;
569 if (now
!= fdb
->updated
)
571 if (unlikely(added_by_user
))
572 fdb
->added_by_user
= 1;
573 if (unlikely(fdb_modified
)) {
574 trace_br_fdb_update(br
, source
, addr
, vid
, added_by_user
);
575 fdb_notify(br
, fdb
, RTM_NEWNEIGH
);
579 spin_lock(&br
->hash_lock
);
580 fdb
= fdb_create(br
, source
, addr
, vid
, 0, 0);
582 if (unlikely(added_by_user
))
583 fdb
->added_by_user
= 1;
584 trace_br_fdb_update(br
, source
, addr
, vid
,
586 fdb_notify(br
, fdb
, RTM_NEWNEIGH
);
588 /* else we lose race and someone else inserts
589 * it first, don't bother updating
591 spin_unlock(&br
->hash_lock
);
595 static int fdb_to_nud(const struct net_bridge
*br
,
596 const struct net_bridge_fdb_entry
*fdb
)
599 return NUD_PERMANENT
;
600 else if (fdb
->is_static
)
602 else if (has_expired(br
, fdb
))
605 return NUD_REACHABLE
;
608 static int fdb_fill_info(struct sk_buff
*skb
, const struct net_bridge
*br
,
609 const struct net_bridge_fdb_entry
*fdb
,
610 u32 portid
, u32 seq
, int type
, unsigned int flags
)
612 unsigned long now
= jiffies
;
613 struct nda_cacheinfo ci
;
614 struct nlmsghdr
*nlh
;
617 nlh
= nlmsg_put(skb
, portid
, seq
, type
, sizeof(*ndm
), flags
);
621 ndm
= nlmsg_data(nlh
);
622 ndm
->ndm_family
= AF_BRIDGE
;
627 ndm
->ndm_ifindex
= fdb
->dst
? fdb
->dst
->dev
->ifindex
: br
->dev
->ifindex
;
628 ndm
->ndm_state
= fdb_to_nud(br
, fdb
);
631 ndm
->ndm_flags
|= NTF_OFFLOADED
;
632 if (fdb
->added_by_external_learn
)
633 ndm
->ndm_flags
|= NTF_EXT_LEARNED
;
635 if (nla_put(skb
, NDA_LLADDR
, ETH_ALEN
, &fdb
->key
.addr
))
636 goto nla_put_failure
;
637 if (nla_put_u32(skb
, NDA_MASTER
, br
->dev
->ifindex
))
638 goto nla_put_failure
;
639 ci
.ndm_used
= jiffies_to_clock_t(now
- fdb
->used
);
640 ci
.ndm_confirmed
= 0;
641 ci
.ndm_updated
= jiffies_to_clock_t(now
- fdb
->updated
);
643 if (nla_put(skb
, NDA_CACHEINFO
, sizeof(ci
), &ci
))
644 goto nla_put_failure
;
646 if (fdb
->key
.vlan_id
&& nla_put(skb
, NDA_VLAN
, sizeof(u16
),
648 goto nla_put_failure
;
654 nlmsg_cancel(skb
, nlh
);
658 static inline size_t fdb_nlmsg_size(void)
660 return NLMSG_ALIGN(sizeof(struct ndmsg
))
661 + nla_total_size(ETH_ALEN
) /* NDA_LLADDR */
662 + nla_total_size(sizeof(u32
)) /* NDA_MASTER */
663 + nla_total_size(sizeof(u16
)) /* NDA_VLAN */
664 + nla_total_size(sizeof(struct nda_cacheinfo
));
667 static void fdb_notify(struct net_bridge
*br
,
668 const struct net_bridge_fdb_entry
*fdb
, int type
)
670 struct net
*net
= dev_net(br
->dev
);
674 br_switchdev_fdb_notify(fdb
, type
);
676 skb
= nlmsg_new(fdb_nlmsg_size(), GFP_ATOMIC
);
680 err
= fdb_fill_info(skb
, br
, fdb
, 0, 0, type
, 0);
682 /* -EMSGSIZE implies BUG in fdb_nlmsg_size() */
683 WARN_ON(err
== -EMSGSIZE
);
687 rtnl_notify(skb
, net
, 0, RTNLGRP_NEIGH
, NULL
, GFP_ATOMIC
);
690 rtnl_set_sk_err(net
, RTNLGRP_NEIGH
, err
);
693 /* Dump information about entries, in response to GETNEIGH */
694 int br_fdb_dump(struct sk_buff
*skb
,
695 struct netlink_callback
*cb
,
696 struct net_device
*dev
,
697 struct net_device
*filter_dev
,
700 struct net_bridge
*br
= netdev_priv(dev
);
701 struct net_bridge_fdb_entry
*f
;
704 if (!(dev
->priv_flags
& IFF_EBRIDGE
))
708 err
= ndo_dflt_fdb_dump(skb
, cb
, dev
, NULL
, idx
);
714 hlist_for_each_entry_rcu(f
, &br
->fdb_list
, fdb_node
) {
715 if (*idx
< cb
->args
[2])
717 if (filter_dev
&& (!f
->dst
|| f
->dst
->dev
!= filter_dev
)) {
718 if (filter_dev
!= dev
)
720 /* !f->dst is a special case for bridge
721 * It means the MAC belongs to the bridge
722 * Therefore need a little more filtering
723 * we only want to dump the !f->dst case
728 if (!filter_dev
&& f
->dst
)
731 err
= fdb_fill_info(skb
, br
, f
,
732 NETLINK_CB(cb
->skb
).portid
,
746 /* Update (create or replace) forwarding database entry */
747 static int fdb_add_entry(struct net_bridge
*br
, struct net_bridge_port
*source
,
748 const __u8
*addr
, __u16 state
, __u16 flags
, __u16 vid
)
750 struct net_bridge_fdb_entry
*fdb
;
751 bool modified
= false;
753 /* If the port cannot learn allow only local and static entries */
754 if (source
&& !(state
& NUD_PERMANENT
) && !(state
& NUD_NOARP
) &&
755 !(source
->state
== BR_STATE_LEARNING
||
756 source
->state
== BR_STATE_FORWARDING
))
759 if (!source
&& !(state
& NUD_PERMANENT
)) {
760 pr_info("bridge: RTM_NEWNEIGH %s without NUD_PERMANENT\n",
765 fdb
= br_fdb_find(br
, addr
, vid
);
767 if (!(flags
& NLM_F_CREATE
))
770 fdb
= fdb_create(br
, source
, addr
, vid
, 0, 0);
776 if (flags
& NLM_F_EXCL
)
779 if (fdb
->dst
!= source
) {
785 if (fdb_to_nud(br
, fdb
) != state
) {
786 if (state
& NUD_PERMANENT
) {
788 if (!fdb
->is_static
) {
790 fdb_add_hw_addr(br
, addr
);
792 } else if (state
& NUD_NOARP
) {
794 if (!fdb
->is_static
) {
796 fdb_add_hw_addr(br
, addr
);
800 if (fdb
->is_static
) {
802 fdb_del_hw_addr(br
, addr
);
808 fdb
->added_by_user
= 1;
812 fdb
->updated
= jiffies
;
813 fdb_notify(br
, fdb
, RTM_NEWNEIGH
);
819 static int __br_fdb_add(struct ndmsg
*ndm
, struct net_bridge
*br
,
820 struct net_bridge_port
*p
, const unsigned char *addr
,
821 u16 nlh_flags
, u16 vid
)
825 if (ndm
->ndm_flags
& NTF_USE
) {
827 pr_info("bridge: RTM_NEWNEIGH %s with NTF_USE is not supported\n",
833 br_fdb_update(br
, p
, addr
, vid
, true);
836 } else if (ndm
->ndm_flags
& NTF_EXT_LEARNED
) {
837 err
= br_fdb_external_learn_add(br
, p
, addr
, vid
);
839 spin_lock_bh(&br
->hash_lock
);
840 err
= fdb_add_entry(br
, p
, addr
, ndm
->ndm_state
,
842 spin_unlock_bh(&br
->hash_lock
);
848 /* Add new permanent fdb entry with RTM_NEWNEIGH */
849 int br_fdb_add(struct ndmsg
*ndm
, struct nlattr
*tb
[],
850 struct net_device
*dev
,
851 const unsigned char *addr
, u16 vid
, u16 nlh_flags
)
853 struct net_bridge_vlan_group
*vg
;
854 struct net_bridge_port
*p
= NULL
;
855 struct net_bridge_vlan
*v
;
856 struct net_bridge
*br
= NULL
;
859 trace_br_fdb_add(ndm
, dev
, addr
, vid
, nlh_flags
);
861 if (!(ndm
->ndm_state
& (NUD_PERMANENT
|NUD_NOARP
|NUD_REACHABLE
))) {
862 pr_info("bridge: RTM_NEWNEIGH with invalid state %#x\n", ndm
->ndm_state
);
866 if (is_zero_ether_addr(addr
)) {
867 pr_info("bridge: RTM_NEWNEIGH with invalid ether address\n");
871 if (dev
->priv_flags
& IFF_EBRIDGE
) {
872 br
= netdev_priv(dev
);
873 vg
= br_vlan_group(br
);
875 p
= br_port_get_rtnl(dev
);
877 pr_info("bridge: RTM_NEWNEIGH %s not a bridge port\n",
882 vg
= nbp_vlan_group(p
);
886 v
= br_vlan_find(vg
, vid
);
887 if (!v
|| !br_vlan_should_use(v
)) {
888 pr_info("bridge: RTM_NEWNEIGH with unconfigured vlan %d on %s\n", vid
, dev
->name
);
892 /* VID was specified, so use it. */
893 err
= __br_fdb_add(ndm
, br
, p
, addr
, nlh_flags
, vid
);
895 err
= __br_fdb_add(ndm
, br
, p
, addr
, nlh_flags
, 0);
896 if (err
|| !vg
|| !vg
->num_vlans
)
899 /* We have vlans configured on this port and user didn't
900 * specify a VLAN. To be nice, add/update entry for every
903 list_for_each_entry(v
, &vg
->vlan_list
, vlist
) {
904 if (!br_vlan_should_use(v
))
906 err
= __br_fdb_add(ndm
, br
, p
, addr
, nlh_flags
, v
->vid
);
916 static int fdb_delete_by_addr_and_port(struct net_bridge
*br
,
917 const struct net_bridge_port
*p
,
918 const u8
*addr
, u16 vlan
)
920 struct net_bridge_fdb_entry
*fdb
;
922 fdb
= br_fdb_find(br
, addr
, vlan
);
923 if (!fdb
|| fdb
->dst
!= p
)
931 static int __br_fdb_delete(struct net_bridge
*br
,
932 const struct net_bridge_port
*p
,
933 const unsigned char *addr
, u16 vid
)
937 spin_lock_bh(&br
->hash_lock
);
938 err
= fdb_delete_by_addr_and_port(br
, p
, addr
, vid
);
939 spin_unlock_bh(&br
->hash_lock
);
944 /* Remove neighbor entry with RTM_DELNEIGH */
945 int br_fdb_delete(struct ndmsg
*ndm
, struct nlattr
*tb
[],
946 struct net_device
*dev
,
947 const unsigned char *addr
, u16 vid
)
949 struct net_bridge_vlan_group
*vg
;
950 struct net_bridge_port
*p
= NULL
;
951 struct net_bridge_vlan
*v
;
952 struct net_bridge
*br
;
955 if (dev
->priv_flags
& IFF_EBRIDGE
) {
956 br
= netdev_priv(dev
);
957 vg
= br_vlan_group(br
);
959 p
= br_port_get_rtnl(dev
);
961 pr_info("bridge: RTM_DELNEIGH %s not a bridge port\n",
965 vg
= nbp_vlan_group(p
);
970 v
= br_vlan_find(vg
, vid
);
972 pr_info("bridge: RTM_DELNEIGH with unconfigured vlan %d on %s\n", vid
, dev
->name
);
976 err
= __br_fdb_delete(br
, p
, addr
, vid
);
979 err
&= __br_fdb_delete(br
, p
, addr
, 0);
980 if (!vg
|| !vg
->num_vlans
)
983 list_for_each_entry(v
, &vg
->vlan_list
, vlist
) {
984 if (!br_vlan_should_use(v
))
986 err
&= __br_fdb_delete(br
, p
, addr
, v
->vid
);
993 int br_fdb_sync_static(struct net_bridge
*br
, struct net_bridge_port
*p
)
995 struct net_bridge_fdb_entry
*f
, *tmp
;
1000 /* the key here is that static entries change only under rtnl */
1002 hlist_for_each_entry_rcu(f
, &br
->fdb_list
, fdb_node
) {
1003 /* We only care for static entries */
1006 err
= dev_uc_add(p
->dev
, f
->key
.addr
.addr
);
1016 hlist_for_each_entry_rcu(tmp
, &br
->fdb_list
, fdb_node
) {
1017 /* We only care for static entries */
1018 if (!tmp
->is_static
)
1022 dev_uc_del(p
->dev
, tmp
->key
.addr
.addr
);
1028 void br_fdb_unsync_static(struct net_bridge
*br
, struct net_bridge_port
*p
)
1030 struct net_bridge_fdb_entry
*f
;
1035 hlist_for_each_entry_rcu(f
, &br
->fdb_list
, fdb_node
) {
1036 /* We only care for static entries */
1040 dev_uc_del(p
->dev
, f
->key
.addr
.addr
);
1045 int br_fdb_external_learn_add(struct net_bridge
*br
, struct net_bridge_port
*p
,
1046 const unsigned char *addr
, u16 vid
)
1048 struct net_bridge_fdb_entry
*fdb
;
1049 bool modified
= false;
1052 trace_br_fdb_external_learn_add(br
, p
, addr
, vid
);
1054 spin_lock_bh(&br
->hash_lock
);
1056 fdb
= br_fdb_find(br
, addr
, vid
);
1058 fdb
= fdb_create(br
, p
, addr
, vid
, 0, 0);
1063 fdb
->added_by_external_learn
= 1;
1064 fdb_notify(br
, fdb
, RTM_NEWNEIGH
);
1066 fdb
->updated
= jiffies
;
1068 if (fdb
->dst
!= p
) {
1073 if (fdb
->added_by_external_learn
) {
1075 fdb
->used
= jiffies
;
1076 } else if (!fdb
->added_by_user
) {
1077 /* Take over SW learned entry */
1078 fdb
->added_by_external_learn
= 1;
1083 fdb_notify(br
, fdb
, RTM_NEWNEIGH
);
1087 spin_unlock_bh(&br
->hash_lock
);
1092 int br_fdb_external_learn_del(struct net_bridge
*br
, struct net_bridge_port
*p
,
1093 const unsigned char *addr
, u16 vid
)
1095 struct net_bridge_fdb_entry
*fdb
;
1098 spin_lock_bh(&br
->hash_lock
);
1100 fdb
= br_fdb_find(br
, addr
, vid
);
1101 if (fdb
&& fdb
->added_by_external_learn
)
1102 fdb_delete(br
, fdb
);
1106 spin_unlock_bh(&br
->hash_lock
);
1111 void br_fdb_offloaded_set(struct net_bridge
*br
, struct net_bridge_port
*p
,
1112 const unsigned char *addr
, u16 vid
)
1114 struct net_bridge_fdb_entry
*fdb
;
1116 spin_lock_bh(&br
->hash_lock
);
1118 fdb
= br_fdb_find(br
, addr
, vid
);
1122 spin_unlock_bh(&br
->hash_lock
);