1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Linux ethernet bridge
7 * Lennert Buytenhek <buytenh@gnu.org>
10 #include <linux/kernel.h>
11 #include <linux/init.h>
12 #include <linux/rculist.h>
13 #include <linux/spinlock.h>
14 #include <linux/times.h>
15 #include <linux/netdevice.h>
16 #include <linux/etherdevice.h>
17 #include <linux/jhash.h>
18 #include <linux/random.h>
19 #include <linux/slab.h>
20 #include <linux/atomic.h>
21 #include <linux/unaligned.h>
22 #include <linux/if_vlan.h>
23 #include <net/switchdev.h>
24 #include <trace/events/bridge.h>
25 #include "br_private.h"
27 static const struct rhashtable_params br_fdb_rht_params
= {
28 .head_offset
= offsetof(struct net_bridge_fdb_entry
, rhnode
),
29 .key_offset
= offsetof(struct net_bridge_fdb_entry
, key
),
30 .key_len
= sizeof(struct net_bridge_fdb_key
),
31 .automatic_shrinking
= true,
34 static struct kmem_cache
*br_fdb_cache __read_mostly
;
36 int __init
br_fdb_init(void)
38 br_fdb_cache
= KMEM_CACHE(net_bridge_fdb_entry
, SLAB_HWCACHE_ALIGN
);
45 void br_fdb_fini(void)
47 kmem_cache_destroy(br_fdb_cache
);
50 int br_fdb_hash_init(struct net_bridge
*br
)
52 return rhashtable_init(&br
->fdb_hash_tbl
, &br_fdb_rht_params
);
55 void br_fdb_hash_fini(struct net_bridge
*br
)
57 rhashtable_destroy(&br
->fdb_hash_tbl
);
60 /* if topology_changing then use forward_delay (default 15 sec)
61 * otherwise keep longer (default 5 minutes)
63 static inline unsigned long hold_time(const struct net_bridge
*br
)
65 return br
->topology_change
? br
->forward_delay
: br
->ageing_time
;
68 static inline int has_expired(const struct net_bridge
*br
,
69 const struct net_bridge_fdb_entry
*fdb
)
71 return !test_bit(BR_FDB_STATIC
, &fdb
->flags
) &&
72 !test_bit(BR_FDB_ADDED_BY_EXT_LEARN
, &fdb
->flags
) &&
73 time_before_eq(fdb
->updated
+ hold_time(br
), jiffies
);
76 static int fdb_to_nud(const struct net_bridge
*br
,
77 const struct net_bridge_fdb_entry
*fdb
)
79 if (test_bit(BR_FDB_LOCAL
, &fdb
->flags
))
81 else if (test_bit(BR_FDB_STATIC
, &fdb
->flags
))
83 else if (has_expired(br
, fdb
))
89 static int fdb_fill_info(struct sk_buff
*skb
, const struct net_bridge
*br
,
90 const struct net_bridge_fdb_entry
*fdb
,
91 u32 portid
, u32 seq
, int type
, unsigned int flags
)
93 const struct net_bridge_port
*dst
= READ_ONCE(fdb
->dst
);
94 unsigned long now
= jiffies
;
95 struct nda_cacheinfo ci
;
100 nlh
= nlmsg_put(skb
, portid
, seq
, type
, sizeof(*ndm
), flags
);
104 ndm
= nlmsg_data(nlh
);
105 ndm
->ndm_family
= AF_BRIDGE
;
110 ndm
->ndm_ifindex
= dst
? dst
->dev
->ifindex
: br
->dev
->ifindex
;
111 ndm
->ndm_state
= fdb_to_nud(br
, fdb
);
113 if (test_bit(BR_FDB_OFFLOADED
, &fdb
->flags
))
114 ndm
->ndm_flags
|= NTF_OFFLOADED
;
115 if (test_bit(BR_FDB_ADDED_BY_EXT_LEARN
, &fdb
->flags
))
116 ndm
->ndm_flags
|= NTF_EXT_LEARNED
;
117 if (test_bit(BR_FDB_STICKY
, &fdb
->flags
))
118 ndm
->ndm_flags
|= NTF_STICKY
;
119 if (test_bit(BR_FDB_LOCKED
, &fdb
->flags
))
120 ext_flags
|= NTF_EXT_LOCKED
;
122 if (nla_put(skb
, NDA_LLADDR
, ETH_ALEN
, &fdb
->key
.addr
))
123 goto nla_put_failure
;
124 if (nla_put_u32(skb
, NDA_MASTER
, br
->dev
->ifindex
))
125 goto nla_put_failure
;
126 if (nla_put_u32(skb
, NDA_FLAGS_EXT
, ext_flags
))
127 goto nla_put_failure
;
129 ci
.ndm_used
= jiffies_to_clock_t(now
- fdb
->used
);
130 ci
.ndm_confirmed
= 0;
131 ci
.ndm_updated
= jiffies_to_clock_t(now
- fdb
->updated
);
133 if (nla_put(skb
, NDA_CACHEINFO
, sizeof(ci
), &ci
))
134 goto nla_put_failure
;
136 if (fdb
->key
.vlan_id
&& nla_put(skb
, NDA_VLAN
, sizeof(u16
),
138 goto nla_put_failure
;
140 if (test_bit(BR_FDB_NOTIFY
, &fdb
->flags
)) {
141 struct nlattr
*nest
= nla_nest_start(skb
, NDA_FDB_EXT_ATTRS
);
142 u8 notify_bits
= FDB_NOTIFY_BIT
;
145 goto nla_put_failure
;
146 if (test_bit(BR_FDB_NOTIFY_INACTIVE
, &fdb
->flags
))
147 notify_bits
|= FDB_NOTIFY_INACTIVE_BIT
;
149 if (nla_put_u8(skb
, NFEA_ACTIVITY_NOTIFY
, notify_bits
)) {
150 nla_nest_cancel(skb
, nest
);
151 goto nla_put_failure
;
154 nla_nest_end(skb
, nest
);
161 nlmsg_cancel(skb
, nlh
);
165 static inline size_t fdb_nlmsg_size(void)
167 return NLMSG_ALIGN(sizeof(struct ndmsg
))
168 + nla_total_size(ETH_ALEN
) /* NDA_LLADDR */
169 + nla_total_size(sizeof(u32
)) /* NDA_MASTER */
170 + nla_total_size(sizeof(u32
)) /* NDA_FLAGS_EXT */
171 + nla_total_size(sizeof(u16
)) /* NDA_VLAN */
172 + nla_total_size(sizeof(struct nda_cacheinfo
))
173 + nla_total_size(0) /* NDA_FDB_EXT_ATTRS */
174 + nla_total_size(sizeof(u8
)); /* NFEA_ACTIVITY_NOTIFY */
177 static void fdb_notify(struct net_bridge
*br
,
178 const struct net_bridge_fdb_entry
*fdb
, int type
,
181 struct net
*net
= dev_net(br
->dev
);
186 br_switchdev_fdb_notify(br
, fdb
, type
);
188 skb
= nlmsg_new(fdb_nlmsg_size(), GFP_ATOMIC
);
192 err
= fdb_fill_info(skb
, br
, fdb
, 0, 0, type
, 0);
194 /* -EMSGSIZE implies BUG in fdb_nlmsg_size() */
195 WARN_ON(err
== -EMSGSIZE
);
199 rtnl_notify(skb
, net
, 0, RTNLGRP_NEIGH
, NULL
, GFP_ATOMIC
);
202 rtnl_set_sk_err(net
, RTNLGRP_NEIGH
, err
);
205 static struct net_bridge_fdb_entry
*fdb_find_rcu(struct rhashtable
*tbl
,
206 const unsigned char *addr
,
209 struct net_bridge_fdb_key key
;
211 WARN_ON_ONCE(!rcu_read_lock_held());
214 memcpy(key
.addr
.addr
, addr
, sizeof(key
.addr
.addr
));
216 return rhashtable_lookup(tbl
, &key
, br_fdb_rht_params
);
219 /* requires bridge hash_lock */
220 static struct net_bridge_fdb_entry
*br_fdb_find(struct net_bridge
*br
,
221 const unsigned char *addr
,
224 struct net_bridge_fdb_entry
*fdb
;
226 lockdep_assert_held_once(&br
->hash_lock
);
229 fdb
= fdb_find_rcu(&br
->fdb_hash_tbl
, addr
, vid
);
235 struct net_device
*br_fdb_find_port(const struct net_device
*br_dev
,
236 const unsigned char *addr
,
239 struct net_bridge_fdb_entry
*f
;
240 struct net_device
*dev
= NULL
;
241 struct net_bridge
*br
;
245 if (!netif_is_bridge_master(br_dev
))
248 br
= netdev_priv(br_dev
);
250 f
= br_fdb_find_rcu(br
, addr
, vid
);
257 EXPORT_SYMBOL_GPL(br_fdb_find_port
);
259 struct net_bridge_fdb_entry
*br_fdb_find_rcu(struct net_bridge
*br
,
260 const unsigned char *addr
,
263 return fdb_find_rcu(&br
->fdb_hash_tbl
, addr
, vid
);
266 /* When a static FDB entry is added, the mac address from the entry is
267 * added to the bridge private HW address list and all required ports
268 * are then updated with the new information.
271 static void fdb_add_hw_addr(struct net_bridge
*br
, const unsigned char *addr
)
274 struct net_bridge_port
*p
;
278 list_for_each_entry(p
, &br
->port_list
, list
) {
279 if (!br_promisc_port(p
)) {
280 err
= dev_uc_add(p
->dev
, addr
);
288 list_for_each_entry_continue_reverse(p
, &br
->port_list
, list
) {
289 if (!br_promisc_port(p
))
290 dev_uc_del(p
->dev
, addr
);
294 /* When a static FDB entry is deleted, the HW address from that entry is
295 * also removed from the bridge private HW address list and updates all
296 * the ports with needed information.
299 static void fdb_del_hw_addr(struct net_bridge
*br
, const unsigned char *addr
)
301 struct net_bridge_port
*p
;
305 list_for_each_entry(p
, &br
->port_list
, list
) {
306 if (!br_promisc_port(p
))
307 dev_uc_del(p
->dev
, addr
);
311 static void fdb_delete(struct net_bridge
*br
, struct net_bridge_fdb_entry
*f
,
314 trace_fdb_delete(br
, f
);
316 if (test_bit(BR_FDB_STATIC
, &f
->flags
))
317 fdb_del_hw_addr(br
, f
->key
.addr
.addr
);
319 hlist_del_init_rcu(&f
->fdb_node
);
320 rhashtable_remove_fast(&br
->fdb_hash_tbl
, &f
->rhnode
,
322 if (test_and_clear_bit(BR_FDB_DYNAMIC_LEARNED
, &f
->flags
))
323 atomic_dec(&br
->fdb_n_learned
);
324 fdb_notify(br
, f
, RTM_DELNEIGH
, swdev_notify
);
328 /* Delete a local entry if no other port had the same address.
330 * This function should only be called on entries with BR_FDB_LOCAL set,
331 * so even with BR_FDB_ADDED_BY_USER cleared we never need to increase
332 * the accounting for dynamically learned entries again.
334 static void fdb_delete_local(struct net_bridge
*br
,
335 const struct net_bridge_port
*p
,
336 struct net_bridge_fdb_entry
*f
)
338 const unsigned char *addr
= f
->key
.addr
.addr
;
339 struct net_bridge_vlan_group
*vg
;
340 const struct net_bridge_vlan
*v
;
341 struct net_bridge_port
*op
;
342 u16 vid
= f
->key
.vlan_id
;
344 /* Maybe another port has same hw addr? */
345 list_for_each_entry(op
, &br
->port_list
, list
) {
346 vg
= nbp_vlan_group(op
);
347 if (op
!= p
&& ether_addr_equal(op
->dev
->dev_addr
, addr
) &&
348 (!vid
|| br_vlan_find(vg
, vid
))) {
350 clear_bit(BR_FDB_ADDED_BY_USER
, &f
->flags
);
355 vg
= br_vlan_group(br
);
356 v
= br_vlan_find(vg
, vid
);
357 /* Maybe bridge device has same hw addr? */
358 if (p
&& ether_addr_equal(br
->dev
->dev_addr
, addr
) &&
359 (!vid
|| (v
&& br_vlan_should_use(v
)))) {
361 clear_bit(BR_FDB_ADDED_BY_USER
, &f
->flags
);
365 fdb_delete(br
, f
, true);
368 void br_fdb_find_delete_local(struct net_bridge
*br
,
369 const struct net_bridge_port
*p
,
370 const unsigned char *addr
, u16 vid
)
372 struct net_bridge_fdb_entry
*f
;
374 spin_lock_bh(&br
->hash_lock
);
375 f
= br_fdb_find(br
, addr
, vid
);
376 if (f
&& test_bit(BR_FDB_LOCAL
, &f
->flags
) &&
377 !test_bit(BR_FDB_ADDED_BY_USER
, &f
->flags
) && f
->dst
== p
)
378 fdb_delete_local(br
, p
, f
);
379 spin_unlock_bh(&br
->hash_lock
);
382 static struct net_bridge_fdb_entry
*fdb_create(struct net_bridge
*br
,
383 struct net_bridge_port
*source
,
384 const unsigned char *addr
,
388 bool learned
= !test_bit(BR_FDB_ADDED_BY_USER
, &flags
) &&
389 !test_bit(BR_FDB_LOCAL
, &flags
);
390 u32 max_learned
= READ_ONCE(br
->fdb_max_learned
);
391 struct net_bridge_fdb_entry
*fdb
;
394 if (likely(learned
)) {
395 int n_learned
= atomic_read(&br
->fdb_n_learned
);
397 if (unlikely(max_learned
&& n_learned
>= max_learned
))
399 __set_bit(BR_FDB_DYNAMIC_LEARNED
, &flags
);
402 fdb
= kmem_cache_alloc(br_fdb_cache
, GFP_ATOMIC
);
406 memcpy(fdb
->key
.addr
.addr
, addr
, ETH_ALEN
);
407 WRITE_ONCE(fdb
->dst
, source
);
408 fdb
->key
.vlan_id
= vid
;
410 fdb
->updated
= fdb
->used
= jiffies
;
411 err
= rhashtable_lookup_insert_fast(&br
->fdb_hash_tbl
, &fdb
->rhnode
,
414 kmem_cache_free(br_fdb_cache
, fdb
);
419 atomic_inc(&br
->fdb_n_learned
);
421 hlist_add_head_rcu(&fdb
->fdb_node
, &br
->fdb_list
);
426 static int fdb_add_local(struct net_bridge
*br
, struct net_bridge_port
*source
,
427 const unsigned char *addr
, u16 vid
)
429 struct net_bridge_fdb_entry
*fdb
;
431 if (!is_valid_ether_addr(addr
))
434 fdb
= br_fdb_find(br
, addr
, vid
);
436 /* it is okay to have multiple ports with same
437 * address, just use the first one.
439 if (test_bit(BR_FDB_LOCAL
, &fdb
->flags
))
441 br_warn(br
, "adding interface %s with same address as a received packet (addr:%pM, vlan:%u)\n",
442 source
? source
->dev
->name
: br
->dev
->name
, addr
, vid
);
443 fdb_delete(br
, fdb
, true);
446 fdb
= fdb_create(br
, source
, addr
, vid
,
447 BIT(BR_FDB_LOCAL
) | BIT(BR_FDB_STATIC
));
451 fdb_add_hw_addr(br
, addr
);
452 fdb_notify(br
, fdb
, RTM_NEWNEIGH
, true);
456 void br_fdb_changeaddr(struct net_bridge_port
*p
, const unsigned char *newaddr
)
458 struct net_bridge_vlan_group
*vg
;
459 struct net_bridge_fdb_entry
*f
;
460 struct net_bridge
*br
= p
->br
;
461 struct net_bridge_vlan
*v
;
463 spin_lock_bh(&br
->hash_lock
);
464 vg
= nbp_vlan_group(p
);
465 hlist_for_each_entry(f
, &br
->fdb_list
, fdb_node
) {
466 if (f
->dst
== p
&& test_bit(BR_FDB_LOCAL
, &f
->flags
) &&
467 !test_bit(BR_FDB_ADDED_BY_USER
, &f
->flags
)) {
469 fdb_delete_local(br
, p
, f
);
471 /* if this port has no vlan information
472 * configured, we can safely be done at
475 if (!vg
|| !vg
->num_vlans
)
481 /* insert new address, may fail if invalid address or dup. */
482 fdb_add_local(br
, p
, newaddr
, 0);
484 if (!vg
|| !vg
->num_vlans
)
487 /* Now add entries for every VLAN configured on the port.
488 * This function runs under RTNL so the bitmap will not change
491 list_for_each_entry(v
, &vg
->vlan_list
, vlist
)
492 fdb_add_local(br
, p
, newaddr
, v
->vid
);
495 spin_unlock_bh(&br
->hash_lock
);
498 void br_fdb_change_mac_address(struct net_bridge
*br
, const u8
*newaddr
)
500 struct net_bridge_vlan_group
*vg
;
501 struct net_bridge_fdb_entry
*f
;
502 struct net_bridge_vlan
*v
;
504 spin_lock_bh(&br
->hash_lock
);
506 /* If old entry was unassociated with any port, then delete it. */
507 f
= br_fdb_find(br
, br
->dev
->dev_addr
, 0);
508 if (f
&& test_bit(BR_FDB_LOCAL
, &f
->flags
) &&
509 !f
->dst
&& !test_bit(BR_FDB_ADDED_BY_USER
, &f
->flags
))
510 fdb_delete_local(br
, NULL
, f
);
512 fdb_add_local(br
, NULL
, newaddr
, 0);
513 vg
= br_vlan_group(br
);
514 if (!vg
|| !vg
->num_vlans
)
516 /* Now remove and add entries for every VLAN configured on the
517 * bridge. This function runs under RTNL so the bitmap will not
518 * change from under us.
520 list_for_each_entry(v
, &vg
->vlan_list
, vlist
) {
521 if (!br_vlan_should_use(v
))
523 f
= br_fdb_find(br
, br
->dev
->dev_addr
, v
->vid
);
524 if (f
&& test_bit(BR_FDB_LOCAL
, &f
->flags
) &&
525 !f
->dst
&& !test_bit(BR_FDB_ADDED_BY_USER
, &f
->flags
))
526 fdb_delete_local(br
, NULL
, f
);
527 fdb_add_local(br
, NULL
, newaddr
, v
->vid
);
530 spin_unlock_bh(&br
->hash_lock
);
533 void br_fdb_cleanup(struct work_struct
*work
)
535 struct net_bridge
*br
= container_of(work
, struct net_bridge
,
537 struct net_bridge_fdb_entry
*f
= NULL
;
538 unsigned long delay
= hold_time(br
);
539 unsigned long work_delay
= delay
;
540 unsigned long now
= jiffies
;
542 /* this part is tricky, in order to avoid blocking learning and
543 * consequently forwarding, we rely on rcu to delete objects with
544 * delayed freeing allowing us to continue traversing
547 hlist_for_each_entry_rcu(f
, &br
->fdb_list
, fdb_node
) {
548 unsigned long this_timer
= f
->updated
+ delay
;
550 if (test_bit(BR_FDB_STATIC
, &f
->flags
) ||
551 test_bit(BR_FDB_ADDED_BY_EXT_LEARN
, &f
->flags
)) {
552 if (test_bit(BR_FDB_NOTIFY
, &f
->flags
)) {
553 if (time_after(this_timer
, now
))
554 work_delay
= min(work_delay
,
556 else if (!test_and_set_bit(BR_FDB_NOTIFY_INACTIVE
,
558 fdb_notify(br
, f
, RTM_NEWNEIGH
, false);
563 if (time_after(this_timer
, now
)) {
564 work_delay
= min(work_delay
, this_timer
- now
);
566 spin_lock_bh(&br
->hash_lock
);
567 if (!hlist_unhashed(&f
->fdb_node
))
568 fdb_delete(br
, f
, true);
569 spin_unlock_bh(&br
->hash_lock
);
574 /* Cleanup minimum 10 milliseconds apart */
575 work_delay
= max_t(unsigned long, work_delay
, msecs_to_jiffies(10));
576 mod_delayed_work(system_long_wq
, &br
->gc_work
, work_delay
);
579 static bool __fdb_flush_matches(const struct net_bridge
*br
,
580 const struct net_bridge_fdb_entry
*f
,
581 const struct net_bridge_fdb_flush_desc
*desc
)
583 const struct net_bridge_port
*dst
= READ_ONCE(f
->dst
);
584 int port_ifidx
= dst
? dst
->dev
->ifindex
: br
->dev
->ifindex
;
586 if (desc
->vlan_id
&& desc
->vlan_id
!= f
->key
.vlan_id
)
588 if (desc
->port_ifindex
&& desc
->port_ifindex
!= port_ifidx
)
590 if (desc
->flags_mask
&& (f
->flags
& desc
->flags_mask
) != desc
->flags
)
596 /* Flush forwarding database entries matching the description */
597 void br_fdb_flush(struct net_bridge
*br
,
598 const struct net_bridge_fdb_flush_desc
*desc
)
600 struct net_bridge_fdb_entry
*f
;
603 hlist_for_each_entry_rcu(f
, &br
->fdb_list
, fdb_node
) {
604 if (!__fdb_flush_matches(br
, f
, desc
))
607 spin_lock_bh(&br
->hash_lock
);
608 if (!hlist_unhashed(&f
->fdb_node
))
609 fdb_delete(br
, f
, true);
610 spin_unlock_bh(&br
->hash_lock
);
615 static unsigned long __ndm_state_to_fdb_flags(u16 ndm_state
)
617 unsigned long flags
= 0;
619 if (ndm_state
& NUD_PERMANENT
)
620 __set_bit(BR_FDB_LOCAL
, &flags
);
621 if (ndm_state
& NUD_NOARP
)
622 __set_bit(BR_FDB_STATIC
, &flags
);
627 static unsigned long __ndm_flags_to_fdb_flags(u8 ndm_flags
)
629 unsigned long flags
= 0;
631 if (ndm_flags
& NTF_USE
)
632 __set_bit(BR_FDB_ADDED_BY_USER
, &flags
);
633 if (ndm_flags
& NTF_EXT_LEARNED
)
634 __set_bit(BR_FDB_ADDED_BY_EXT_LEARN
, &flags
);
635 if (ndm_flags
& NTF_OFFLOADED
)
636 __set_bit(BR_FDB_OFFLOADED
, &flags
);
637 if (ndm_flags
& NTF_STICKY
)
638 __set_bit(BR_FDB_STICKY
, &flags
);
643 static int __fdb_flush_validate_ifindex(const struct net_bridge
*br
,
645 struct netlink_ext_ack
*extack
)
647 const struct net_device
*dev
;
649 dev
= __dev_get_by_index(dev_net(br
->dev
), ifindex
);
651 NL_SET_ERR_MSG_MOD(extack
, "Unknown flush device ifindex");
654 if (!netif_is_bridge_master(dev
) && !netif_is_bridge_port(dev
)) {
655 NL_SET_ERR_MSG_MOD(extack
, "Flush device is not a bridge or bridge port");
658 if (netif_is_bridge_master(dev
) && dev
!= br
->dev
) {
659 NL_SET_ERR_MSG_MOD(extack
,
660 "Flush bridge device does not match target bridge device");
663 if (netif_is_bridge_port(dev
)) {
664 struct net_bridge_port
*p
= br_port_get_rtnl(dev
);
667 NL_SET_ERR_MSG_MOD(extack
, "Port belongs to a different bridge device");
675 static const struct nla_policy br_fdb_del_bulk_policy
[NDA_MAX
+ 1] = {
676 [NDA_VLAN
] = NLA_POLICY_RANGE(NLA_U16
, 1, VLAN_N_VID
- 2),
677 [NDA_IFINDEX
] = NLA_POLICY_MIN(NLA_S32
, 1),
678 [NDA_NDM_STATE_MASK
] = { .type
= NLA_U16
},
679 [NDA_NDM_FLAGS_MASK
] = { .type
= NLA_U8
},
682 int br_fdb_delete_bulk(struct nlmsghdr
*nlh
, struct net_device
*dev
,
683 struct netlink_ext_ack
*extack
)
685 struct net_bridge_fdb_flush_desc desc
= {};
686 struct ndmsg
*ndm
= nlmsg_data(nlh
);
687 struct net_bridge_port
*p
= NULL
;
688 struct nlattr
*tb
[NDA_MAX
+ 1];
689 struct net_bridge
*br
;
693 ndm_flags
= ndm
->ndm_flags
& ~FDB_FLUSH_IGNORED_NDM_FLAGS
;
695 err
= nlmsg_parse(nlh
, sizeof(*ndm
), tb
, NDA_MAX
,
696 br_fdb_del_bulk_policy
, extack
);
700 if (netif_is_bridge_master(dev
)) {
701 br
= netdev_priv(dev
);
703 p
= br_port_get_rtnl(dev
);
705 NL_SET_ERR_MSG_MOD(extack
, "Device is not a bridge port");
712 desc
.vlan_id
= nla_get_u16(tb
[NDA_VLAN
]);
714 if (ndm_flags
& ~FDB_FLUSH_ALLOWED_NDM_FLAGS
) {
715 NL_SET_ERR_MSG(extack
, "Unsupported fdb flush ndm flag bits set");
718 if (ndm
->ndm_state
& ~FDB_FLUSH_ALLOWED_NDM_STATES
) {
719 NL_SET_ERR_MSG(extack
, "Unsupported fdb flush ndm state bits set");
723 desc
.flags
|= __ndm_state_to_fdb_flags(ndm
->ndm_state
);
724 desc
.flags
|= __ndm_flags_to_fdb_flags(ndm_flags
);
725 if (tb
[NDA_NDM_STATE_MASK
]) {
726 u16 ndm_state_mask
= nla_get_u16(tb
[NDA_NDM_STATE_MASK
]);
728 desc
.flags_mask
|= __ndm_state_to_fdb_flags(ndm_state_mask
);
730 if (tb
[NDA_NDM_FLAGS_MASK
]) {
731 u8 ndm_flags_mask
= nla_get_u8(tb
[NDA_NDM_FLAGS_MASK
]);
733 desc
.flags_mask
|= __ndm_flags_to_fdb_flags(ndm_flags_mask
);
735 if (tb
[NDA_IFINDEX
]) {
736 int ifidx
= nla_get_s32(tb
[NDA_IFINDEX
]);
738 err
= __fdb_flush_validate_ifindex(br
, ifidx
, extack
);
741 desc
.port_ifindex
= ifidx
;
743 /* flush was invoked with port device and NTF_MASTER */
744 desc
.port_ifindex
= p
->dev
->ifindex
;
747 br_debug(br
, "flushing port ifindex: %d vlan id: %u flags: 0x%lx flags mask: 0x%lx\n",
748 desc
.port_ifindex
, desc
.vlan_id
, desc
.flags
, desc
.flags_mask
);
750 br_fdb_flush(br
, &desc
);
755 /* Flush all entries referring to a specific port.
756 * if do_all is set also flush static entries
757 * if vid is set delete all entries that match the vlan_id
759 void br_fdb_delete_by_port(struct net_bridge
*br
,
760 const struct net_bridge_port
*p
,
764 struct net_bridge_fdb_entry
*f
;
765 struct hlist_node
*tmp
;
767 spin_lock_bh(&br
->hash_lock
);
768 hlist_for_each_entry_safe(f
, tmp
, &br
->fdb_list
, fdb_node
) {
773 if (test_bit(BR_FDB_STATIC
, &f
->flags
) ||
774 (test_bit(BR_FDB_ADDED_BY_EXT_LEARN
, &f
->flags
) &&
775 !test_bit(BR_FDB_OFFLOADED
, &f
->flags
)) ||
776 (vid
&& f
->key
.vlan_id
!= vid
))
779 if (test_bit(BR_FDB_LOCAL
, &f
->flags
))
780 fdb_delete_local(br
, p
, f
);
782 fdb_delete(br
, f
, true);
784 spin_unlock_bh(&br
->hash_lock
);
787 #if IS_ENABLED(CONFIG_ATM_LANE)
788 /* Interface used by ATM LANE hook to test
789 * if an addr is on some other bridge port */
790 int br_fdb_test_addr(struct net_device
*dev
, unsigned char *addr
)
792 struct net_bridge_fdb_entry
*fdb
;
793 struct net_bridge_port
*port
;
797 port
= br_port_get_rcu(dev
);
801 const struct net_bridge_port
*dst
= NULL
;
803 fdb
= br_fdb_find_rcu(port
->br
, addr
, 0);
805 dst
= READ_ONCE(fdb
->dst
);
807 ret
= dst
&& dst
->dev
!= dev
&&
808 dst
->state
== BR_STATE_FORWARDING
;
814 #endif /* CONFIG_ATM_LANE */
817 * Fill buffer with forwarding table records in
820 int br_fdb_fillbuf(struct net_bridge
*br
, void *buf
,
821 unsigned long maxnum
, unsigned long skip
)
823 struct net_bridge_fdb_entry
*f
;
824 struct __fdb_entry
*fe
= buf
;
827 memset(buf
, 0, maxnum
*sizeof(struct __fdb_entry
));
830 hlist_for_each_entry_rcu(f
, &br
->fdb_list
, fdb_node
) {
834 if (has_expired(br
, f
))
837 /* ignore pseudo entry for local MAC address */
846 /* convert from internal format to API */
847 memcpy(fe
->mac_addr
, f
->key
.addr
.addr
, ETH_ALEN
);
849 /* due to ABI compat need to split into hi/lo */
850 fe
->port_no
= f
->dst
->port_no
;
851 fe
->port_hi
= f
->dst
->port_no
>> 8;
853 fe
->is_local
= test_bit(BR_FDB_LOCAL
, &f
->flags
);
854 if (!test_bit(BR_FDB_STATIC
, &f
->flags
))
855 fe
->ageing_timer_value
= jiffies_delta_to_clock_t(jiffies
- f
->updated
);
864 /* Add entry for local address of interface */
865 int br_fdb_add_local(struct net_bridge
*br
, struct net_bridge_port
*source
,
866 const unsigned char *addr
, u16 vid
)
870 spin_lock_bh(&br
->hash_lock
);
871 ret
= fdb_add_local(br
, source
, addr
, vid
);
872 spin_unlock_bh(&br
->hash_lock
);
876 /* returns true if the fdb was modified */
877 static bool __fdb_mark_active(struct net_bridge_fdb_entry
*fdb
)
879 return !!(test_bit(BR_FDB_NOTIFY_INACTIVE
, &fdb
->flags
) &&
880 test_and_clear_bit(BR_FDB_NOTIFY_INACTIVE
, &fdb
->flags
));
883 void br_fdb_update(struct net_bridge
*br
, struct net_bridge_port
*source
,
884 const unsigned char *addr
, u16 vid
, unsigned long flags
)
886 struct net_bridge_fdb_entry
*fdb
;
888 /* some users want to always flood. */
889 if (hold_time(br
) == 0)
892 fdb
= fdb_find_rcu(&br
->fdb_hash_tbl
, addr
, vid
);
894 /* attempt to update an entry for a local interface */
895 if (unlikely(test_bit(BR_FDB_LOCAL
, &fdb
->flags
))) {
897 br_warn(br
, "received packet on %s with own address as source address (addr:%pM, vlan:%u)\n",
898 source
->dev
->name
, addr
, vid
);
900 unsigned long now
= jiffies
;
901 bool fdb_modified
= false;
903 if (now
!= fdb
->updated
) {
905 fdb_modified
= __fdb_mark_active(fdb
);
908 /* fastpath: update of existing entry */
909 if (unlikely(source
!= READ_ONCE(fdb
->dst
) &&
910 !test_bit(BR_FDB_STICKY
, &fdb
->flags
))) {
911 br_switchdev_fdb_notify(br
, fdb
, RTM_DELNEIGH
);
912 WRITE_ONCE(fdb
->dst
, source
);
914 /* Take over HW learned entry */
915 if (unlikely(test_bit(BR_FDB_ADDED_BY_EXT_LEARN
,
917 clear_bit(BR_FDB_ADDED_BY_EXT_LEARN
,
919 /* Clear locked flag when roaming to an
922 if (unlikely(test_bit(BR_FDB_LOCKED
, &fdb
->flags
)))
923 clear_bit(BR_FDB_LOCKED
, &fdb
->flags
);
926 if (unlikely(test_bit(BR_FDB_ADDED_BY_USER
, &flags
))) {
927 set_bit(BR_FDB_ADDED_BY_USER
, &fdb
->flags
);
928 if (test_and_clear_bit(BR_FDB_DYNAMIC_LEARNED
,
930 atomic_dec(&br
->fdb_n_learned
);
932 if (unlikely(fdb_modified
)) {
933 trace_br_fdb_update(br
, source
, addr
, vid
, flags
);
934 fdb_notify(br
, fdb
, RTM_NEWNEIGH
, true);
938 spin_lock(&br
->hash_lock
);
939 fdb
= fdb_create(br
, source
, addr
, vid
, flags
);
941 trace_br_fdb_update(br
, source
, addr
, vid
, flags
);
942 fdb_notify(br
, fdb
, RTM_NEWNEIGH
, true);
944 /* else we lose race and someone else inserts
945 * it first, don't bother updating
947 spin_unlock(&br
->hash_lock
);
951 /* Dump information about entries, in response to GETNEIGH */
952 int br_fdb_dump(struct sk_buff
*skb
,
953 struct netlink_callback
*cb
,
954 struct net_device
*dev
,
955 struct net_device
*filter_dev
,
958 struct net_bridge
*br
= netdev_priv(dev
);
959 struct net_bridge_fdb_entry
*f
;
962 if (!netif_is_bridge_master(dev
))
966 err
= ndo_dflt_fdb_dump(skb
, cb
, dev
, NULL
, idx
);
972 hlist_for_each_entry_rcu(f
, &br
->fdb_list
, fdb_node
) {
973 if (*idx
< cb
->args
[2])
975 if (filter_dev
&& (!f
->dst
|| f
->dst
->dev
!= filter_dev
)) {
976 if (filter_dev
!= dev
)
978 /* !f->dst is a special case for bridge
979 * It means the MAC belongs to the bridge
980 * Therefore need a little more filtering
981 * we only want to dump the !f->dst case
986 if (!filter_dev
&& f
->dst
)
989 err
= fdb_fill_info(skb
, br
, f
,
990 NETLINK_CB(cb
->skb
).portid
,
1004 int br_fdb_get(struct sk_buff
*skb
,
1005 struct nlattr
*tb
[],
1006 struct net_device
*dev
,
1007 const unsigned char *addr
,
1008 u16 vid
, u32 portid
, u32 seq
,
1009 struct netlink_ext_ack
*extack
)
1011 struct net_bridge
*br
= netdev_priv(dev
);
1012 struct net_bridge_fdb_entry
*f
;
1016 f
= br_fdb_find_rcu(br
, addr
, vid
);
1018 NL_SET_ERR_MSG(extack
, "Fdb entry not found");
1023 err
= fdb_fill_info(skb
, br
, f
, portid
, seq
,
1030 /* returns true if the fdb is modified */
1031 static bool fdb_handle_notify(struct net_bridge_fdb_entry
*fdb
, u8 notify
)
1033 bool modified
= false;
1035 /* allow to mark an entry as inactive, usually done on creation */
1036 if ((notify
& FDB_NOTIFY_INACTIVE_BIT
) &&
1037 !test_and_set_bit(BR_FDB_NOTIFY_INACTIVE
, &fdb
->flags
))
1040 if ((notify
& FDB_NOTIFY_BIT
) &&
1041 !test_and_set_bit(BR_FDB_NOTIFY
, &fdb
->flags
)) {
1042 /* enabled activity tracking */
1044 } else if (!(notify
& FDB_NOTIFY_BIT
) &&
1045 test_and_clear_bit(BR_FDB_NOTIFY
, &fdb
->flags
)) {
1046 /* disabled activity tracking, clear notify state */
1047 clear_bit(BR_FDB_NOTIFY_INACTIVE
, &fdb
->flags
);
1054 /* Update (create or replace) forwarding database entry */
1055 static int fdb_add_entry(struct net_bridge
*br
, struct net_bridge_port
*source
,
1056 const u8
*addr
, struct ndmsg
*ndm
, u16 flags
, u16 vid
,
1057 struct nlattr
*nfea_tb
[])
1059 bool is_sticky
= !!(ndm
->ndm_flags
& NTF_STICKY
);
1060 bool refresh
= !nfea_tb
[NFEA_DONT_REFRESH
];
1061 struct net_bridge_fdb_entry
*fdb
;
1062 u16 state
= ndm
->ndm_state
;
1063 bool modified
= false;
1066 /* If the port cannot learn allow only local and static entries */
1067 if (source
&& !(state
& NUD_PERMANENT
) && !(state
& NUD_NOARP
) &&
1068 !(source
->state
== BR_STATE_LEARNING
||
1069 source
->state
== BR_STATE_FORWARDING
))
1072 if (!source
&& !(state
& NUD_PERMANENT
)) {
1073 pr_info("bridge: RTM_NEWNEIGH %s without NUD_PERMANENT\n",
1078 if (is_sticky
&& (state
& NUD_PERMANENT
))
1081 if (nfea_tb
[NFEA_ACTIVITY_NOTIFY
]) {
1082 notify
= nla_get_u8(nfea_tb
[NFEA_ACTIVITY_NOTIFY
]);
1083 if ((notify
& ~BR_FDB_NOTIFY_SETTABLE_BITS
) ||
1084 (notify
& BR_FDB_NOTIFY_SETTABLE_BITS
) == FDB_NOTIFY_INACTIVE_BIT
)
1088 fdb
= br_fdb_find(br
, addr
, vid
);
1090 if (!(flags
& NLM_F_CREATE
))
1093 fdb
= fdb_create(br
, source
, addr
, vid
,
1094 BIT(BR_FDB_ADDED_BY_USER
));
1100 if (flags
& NLM_F_EXCL
)
1103 if (READ_ONCE(fdb
->dst
) != source
) {
1104 WRITE_ONCE(fdb
->dst
, source
);
1108 set_bit(BR_FDB_ADDED_BY_USER
, &fdb
->flags
);
1109 if (test_and_clear_bit(BR_FDB_DYNAMIC_LEARNED
, &fdb
->flags
))
1110 atomic_dec(&br
->fdb_n_learned
);
1113 if (fdb_to_nud(br
, fdb
) != state
) {
1114 if (state
& NUD_PERMANENT
) {
1115 set_bit(BR_FDB_LOCAL
, &fdb
->flags
);
1116 if (!test_and_set_bit(BR_FDB_STATIC
, &fdb
->flags
))
1117 fdb_add_hw_addr(br
, addr
);
1118 } else if (state
& NUD_NOARP
) {
1119 clear_bit(BR_FDB_LOCAL
, &fdb
->flags
);
1120 if (!test_and_set_bit(BR_FDB_STATIC
, &fdb
->flags
))
1121 fdb_add_hw_addr(br
, addr
);
1123 clear_bit(BR_FDB_LOCAL
, &fdb
->flags
);
1124 if (test_and_clear_bit(BR_FDB_STATIC
, &fdb
->flags
))
1125 fdb_del_hw_addr(br
, addr
);
1131 if (is_sticky
!= test_bit(BR_FDB_STICKY
, &fdb
->flags
)) {
1132 change_bit(BR_FDB_STICKY
, &fdb
->flags
);
1136 if (test_and_clear_bit(BR_FDB_LOCKED
, &fdb
->flags
))
1139 if (fdb_handle_notify(fdb
, notify
))
1142 fdb
->used
= jiffies
;
1145 fdb
->updated
= jiffies
;
1146 fdb_notify(br
, fdb
, RTM_NEWNEIGH
, true);
1152 static int __br_fdb_add(struct ndmsg
*ndm
, struct net_bridge
*br
,
1153 struct net_bridge_port
*p
, const unsigned char *addr
,
1154 u16 nlh_flags
, u16 vid
, struct nlattr
*nfea_tb
[],
1155 bool *notified
, struct netlink_ext_ack
*extack
)
1159 if (ndm
->ndm_flags
& NTF_USE
) {
1161 pr_info("bridge: RTM_NEWNEIGH %s with NTF_USE is not supported\n",
1165 if (!nbp_state_should_learn(p
))
1170 br_fdb_update(br
, p
, addr
, vid
, BIT(BR_FDB_ADDED_BY_USER
));
1173 } else if (ndm
->ndm_flags
& NTF_EXT_LEARNED
) {
1174 if (!p
&& !(ndm
->ndm_state
& NUD_PERMANENT
)) {
1175 NL_SET_ERR_MSG_MOD(extack
,
1176 "FDB entry towards bridge must be permanent");
1179 err
= br_fdb_external_learn_add(br
, p
, addr
, vid
, false, true);
1181 spin_lock_bh(&br
->hash_lock
);
1182 err
= fdb_add_entry(br
, p
, addr
, ndm
, nlh_flags
, vid
, nfea_tb
);
1183 spin_unlock_bh(&br
->hash_lock
);
1191 static const struct nla_policy br_nda_fdb_pol
[NFEA_MAX
+ 1] = {
1192 [NFEA_ACTIVITY_NOTIFY
] = { .type
= NLA_U8
},
1193 [NFEA_DONT_REFRESH
] = { .type
= NLA_FLAG
},
1196 /* Add new permanent fdb entry with RTM_NEWNEIGH */
1197 int br_fdb_add(struct ndmsg
*ndm
, struct nlattr
*tb
[],
1198 struct net_device
*dev
,
1199 const unsigned char *addr
, u16 vid
, u16 nlh_flags
,
1200 bool *notified
, struct netlink_ext_ack
*extack
)
1202 struct nlattr
*nfea_tb
[NFEA_MAX
+ 1], *attr
;
1203 struct net_bridge_vlan_group
*vg
;
1204 struct net_bridge_port
*p
= NULL
;
1205 struct net_bridge_vlan
*v
;
1206 struct net_bridge
*br
= NULL
;
1210 trace_br_fdb_add(ndm
, dev
, addr
, vid
, nlh_flags
);
1212 if (!(ndm
->ndm_state
& (NUD_PERMANENT
|NUD_NOARP
|NUD_REACHABLE
))) {
1213 pr_info("bridge: RTM_NEWNEIGH with invalid state %#x\n", ndm
->ndm_state
);
1217 if (is_zero_ether_addr(addr
)) {
1218 pr_info("bridge: RTM_NEWNEIGH with invalid ether address\n");
1222 if (netif_is_bridge_master(dev
)) {
1223 br
= netdev_priv(dev
);
1224 vg
= br_vlan_group(br
);
1226 p
= br_port_get_rtnl(dev
);
1228 pr_info("bridge: RTM_NEWNEIGH %s not a bridge port\n",
1233 vg
= nbp_vlan_group(p
);
1236 if (tb
[NDA_FLAGS_EXT
])
1237 ext_flags
= nla_get_u32(tb
[NDA_FLAGS_EXT
]);
1239 if (ext_flags
& NTF_EXT_LOCKED
) {
1240 NL_SET_ERR_MSG_MOD(extack
, "Cannot add FDB entry with \"locked\" flag set");
1244 if (tb
[NDA_FDB_EXT_ATTRS
]) {
1245 attr
= tb
[NDA_FDB_EXT_ATTRS
];
1246 err
= nla_parse_nested(nfea_tb
, NFEA_MAX
, attr
,
1247 br_nda_fdb_pol
, extack
);
1251 memset(nfea_tb
, 0, sizeof(struct nlattr
*) * (NFEA_MAX
+ 1));
1255 v
= br_vlan_find(vg
, vid
);
1256 if (!v
|| !br_vlan_should_use(v
)) {
1257 pr_info("bridge: RTM_NEWNEIGH with unconfigured vlan %d on %s\n", vid
, dev
->name
);
1261 /* VID was specified, so use it. */
1262 err
= __br_fdb_add(ndm
, br
, p
, addr
, nlh_flags
, vid
, nfea_tb
,
1265 err
= __br_fdb_add(ndm
, br
, p
, addr
, nlh_flags
, 0, nfea_tb
,
1267 if (err
|| !vg
|| !vg
->num_vlans
)
1270 /* We have vlans configured on this port and user didn't
1271 * specify a VLAN. To be nice, add/update entry for every
1272 * vlan on this port.
1274 list_for_each_entry(v
, &vg
->vlan_list
, vlist
) {
1275 if (!br_vlan_should_use(v
))
1277 err
= __br_fdb_add(ndm
, br
, p
, addr
, nlh_flags
, v
->vid
,
1278 nfea_tb
, notified
, extack
);
1288 static int fdb_delete_by_addr_and_port(struct net_bridge
*br
,
1289 const struct net_bridge_port
*p
,
1290 const u8
*addr
, u16 vlan
, bool *notified
)
1292 struct net_bridge_fdb_entry
*fdb
;
1294 fdb
= br_fdb_find(br
, addr
, vlan
);
1295 if (!fdb
|| READ_ONCE(fdb
->dst
) != p
)
1298 fdb_delete(br
, fdb
, true);
1304 static int __br_fdb_delete(struct net_bridge
*br
,
1305 const struct net_bridge_port
*p
,
1306 const unsigned char *addr
, u16 vid
, bool *notified
)
1310 spin_lock_bh(&br
->hash_lock
);
1311 err
= fdb_delete_by_addr_and_port(br
, p
, addr
, vid
, notified
);
1312 spin_unlock_bh(&br
->hash_lock
);
1317 /* Remove neighbor entry with RTM_DELNEIGH */
1318 int br_fdb_delete(struct ndmsg
*ndm
, struct nlattr
*tb
[],
1319 struct net_device
*dev
,
1320 const unsigned char *addr
, u16 vid
, bool *notified
,
1321 struct netlink_ext_ack
*extack
)
1323 struct net_bridge_vlan_group
*vg
;
1324 struct net_bridge_port
*p
= NULL
;
1325 struct net_bridge
*br
;
1328 if (netif_is_bridge_master(dev
)) {
1329 br
= netdev_priv(dev
);
1330 vg
= br_vlan_group(br
);
1332 p
= br_port_get_rtnl(dev
);
1334 pr_info("bridge: RTM_DELNEIGH %s not a bridge port\n",
1338 vg
= nbp_vlan_group(p
);
1343 err
= __br_fdb_delete(br
, p
, addr
, vid
, notified
);
1345 struct net_bridge_vlan
*v
;
1348 err
&= __br_fdb_delete(br
, p
, addr
, 0, notified
);
1349 if (!vg
|| !vg
->num_vlans
)
1352 list_for_each_entry(v
, &vg
->vlan_list
, vlist
) {
1353 if (!br_vlan_should_use(v
))
1355 err
&= __br_fdb_delete(br
, p
, addr
, v
->vid
, notified
);
1362 int br_fdb_sync_static(struct net_bridge
*br
, struct net_bridge_port
*p
)
1364 struct net_bridge_fdb_entry
*f
, *tmp
;
1369 /* the key here is that static entries change only under rtnl */
1371 hlist_for_each_entry_rcu(f
, &br
->fdb_list
, fdb_node
) {
1372 /* We only care for static entries */
1373 if (!test_bit(BR_FDB_STATIC
, &f
->flags
))
1375 err
= dev_uc_add(p
->dev
, f
->key
.addr
.addr
);
1385 hlist_for_each_entry_rcu(tmp
, &br
->fdb_list
, fdb_node
) {
1386 /* We only care for static entries */
1387 if (!test_bit(BR_FDB_STATIC
, &tmp
->flags
))
1391 dev_uc_del(p
->dev
, tmp
->key
.addr
.addr
);
1397 void br_fdb_unsync_static(struct net_bridge
*br
, struct net_bridge_port
*p
)
1399 struct net_bridge_fdb_entry
*f
;
1404 hlist_for_each_entry_rcu(f
, &br
->fdb_list
, fdb_node
) {
1405 /* We only care for static entries */
1406 if (!test_bit(BR_FDB_STATIC
, &f
->flags
))
1409 dev_uc_del(p
->dev
, f
->key
.addr
.addr
);
1414 int br_fdb_external_learn_add(struct net_bridge
*br
, struct net_bridge_port
*p
,
1415 const unsigned char *addr
, u16 vid
, bool locked
,
1418 struct net_bridge_fdb_entry
*fdb
;
1419 bool modified
= false;
1422 trace_br_fdb_external_learn_add(br
, p
, addr
, vid
);
1424 if (locked
&& (!p
|| !(p
->flags
& BR_PORT_MAB
)))
1427 spin_lock_bh(&br
->hash_lock
);
1429 fdb
= br_fdb_find(br
, addr
, vid
);
1431 unsigned long flags
= BIT(BR_FDB_ADDED_BY_EXT_LEARN
);
1434 flags
|= BIT(BR_FDB_ADDED_BY_USER
);
1437 flags
|= BIT(BR_FDB_LOCAL
);
1440 flags
|= BIT(BR_FDB_LOCKED
);
1442 fdb
= fdb_create(br
, p
, addr
, vid
, flags
);
1447 fdb_notify(br
, fdb
, RTM_NEWNEIGH
, swdev_notify
);
1450 (!test_bit(BR_FDB_LOCKED
, &fdb
->flags
) ||
1451 READ_ONCE(fdb
->dst
) != p
)) {
1456 fdb
->updated
= jiffies
;
1458 if (READ_ONCE(fdb
->dst
) != p
) {
1459 WRITE_ONCE(fdb
->dst
, p
);
1463 if (test_and_set_bit(BR_FDB_ADDED_BY_EXT_LEARN
, &fdb
->flags
)) {
1465 fdb
->used
= jiffies
;
1470 if (locked
!= test_bit(BR_FDB_LOCKED
, &fdb
->flags
)) {
1471 change_bit(BR_FDB_LOCKED
, &fdb
->flags
);
1476 set_bit(BR_FDB_ADDED_BY_USER
, &fdb
->flags
);
1479 set_bit(BR_FDB_LOCAL
, &fdb
->flags
);
1481 if ((swdev_notify
|| !p
) &&
1482 test_and_clear_bit(BR_FDB_DYNAMIC_LEARNED
, &fdb
->flags
))
1483 atomic_dec(&br
->fdb_n_learned
);
1486 fdb_notify(br
, fdb
, RTM_NEWNEIGH
, swdev_notify
);
1490 spin_unlock_bh(&br
->hash_lock
);
1495 int br_fdb_external_learn_del(struct net_bridge
*br
, struct net_bridge_port
*p
,
1496 const unsigned char *addr
, u16 vid
,
1499 struct net_bridge_fdb_entry
*fdb
;
1502 spin_lock_bh(&br
->hash_lock
);
1504 fdb
= br_fdb_find(br
, addr
, vid
);
1505 if (fdb
&& test_bit(BR_FDB_ADDED_BY_EXT_LEARN
, &fdb
->flags
))
1506 fdb_delete(br
, fdb
, swdev_notify
);
1510 spin_unlock_bh(&br
->hash_lock
);
1515 void br_fdb_offloaded_set(struct net_bridge
*br
, struct net_bridge_port
*p
,
1516 const unsigned char *addr
, u16 vid
, bool offloaded
)
1518 struct net_bridge_fdb_entry
*fdb
;
1520 spin_lock_bh(&br
->hash_lock
);
1522 fdb
= br_fdb_find(br
, addr
, vid
);
1523 if (fdb
&& offloaded
!= test_bit(BR_FDB_OFFLOADED
, &fdb
->flags
))
1524 change_bit(BR_FDB_OFFLOADED
, &fdb
->flags
);
1526 spin_unlock_bh(&br
->hash_lock
);
1529 void br_fdb_clear_offload(const struct net_device
*dev
, u16 vid
)
1531 struct net_bridge_fdb_entry
*f
;
1532 struct net_bridge_port
*p
;
1536 p
= br_port_get_rtnl(dev
);
1540 spin_lock_bh(&p
->br
->hash_lock
);
1541 hlist_for_each_entry(f
, &p
->br
->fdb_list
, fdb_node
) {
1542 if (f
->dst
== p
&& f
->key
.vlan_id
== vid
)
1543 clear_bit(BR_FDB_OFFLOADED
, &f
->flags
);
1545 spin_unlock_bh(&p
->br
->hash_lock
);
1547 EXPORT_SYMBOL_GPL(br_fdb_clear_offload
);