Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Forwarding database |
| 4 | * Linux ethernet bridge |
| 5 | * |
| 6 | * Authors: |
| 7 | * Lennert Buytenhek <buytenh@gnu.org> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <linux/kernel.h> |
| 11 | #include <linux/init.h> |
Franck Bui-Huu | 8252474 | 2008-05-12 21:21:05 +0200 | [diff] [blame] | 12 | #include <linux/rculist.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Stephen Hemminger | 3f89092 | 2007-03-21 13:42:33 -0700 | [diff] [blame] | 18 | #include <linux/random.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 19 | #include <linux/slab.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 20 | #include <linux/atomic.h> |
Stephen Hemminger | 3f89092 | 2007-03-21 13:42:33 -0700 | [diff] [blame] | 21 | #include <asm/unaligned.h> |
Vlad Yasevich | 2ba071e | 2013-02-13 12:00:16 +0000 | [diff] [blame] | 22 | #include <linux/if_vlan.h> |
Scott Feldman | b4ad7ba | 2015-06-14 11:33:11 -0700 | [diff] [blame] | 23 | #include <net/switchdev.h> |
Roopa Prabhu | b74fd30 | 2017-08-29 13:16:57 -0700 | [diff] [blame] | 24 | #include <trace/events/bridge.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include "br_private.h" |
| 26 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 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, |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 32 | }; |
| 33 | |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 34 | static struct kmem_cache *br_fdb_cache __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
Akinobu Mita | 87a596e | 2007-04-07 18:57:07 +0900 | [diff] [blame] | 36 | int __init br_fdb_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | { |
| 38 | br_fdb_cache = kmem_cache_create("bridge_fdb_cache", |
| 39 | sizeof(struct net_bridge_fdb_entry), |
| 40 | 0, |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 41 | SLAB_HWCACHE_ALIGN, NULL); |
Akinobu Mita | 87a596e | 2007-04-07 18:57:07 +0900 | [diff] [blame] | 42 | if (!br_fdb_cache) |
| 43 | return -ENOMEM; |
| 44 | |
Akinobu Mita | 87a596e | 2007-04-07 18:57:07 +0900 | [diff] [blame] | 45 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Andrew Morton | 73afc90 | 2007-12-05 21:35:23 -0800 | [diff] [blame] | 48 | void br_fdb_fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | { |
| 50 | kmem_cache_destroy(br_fdb_cache); |
| 51 | } |
| 52 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 53 | int br_fdb_hash_init(struct net_bridge *br) |
| 54 | { |
| 55 | return rhashtable_init(&br->fdb_hash_tbl, &br_fdb_rht_params); |
| 56 | } |
| 57 | |
| 58 | void br_fdb_hash_fini(struct net_bridge *br) |
| 59 | { |
| 60 | rhashtable_destroy(&br->fdb_hash_tbl); |
| 61 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
| 63 | /* if topology_changing then use forward_delay (default 15 sec) |
| 64 | * otherwise keep longer (default 5 minutes) |
| 65 | */ |
Stephen Hemminger | 3f89092 | 2007-03-21 13:42:33 -0700 | [diff] [blame] | 66 | static inline unsigned long hold_time(const struct net_bridge *br) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | { |
| 68 | return br->topology_change ? br->forward_delay : br->ageing_time; |
| 69 | } |
| 70 | |
Stephen Hemminger | 3f89092 | 2007-03-21 13:42:33 -0700 | [diff] [blame] | 71 | static inline int has_expired(const struct net_bridge *br, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | const struct net_bridge_fdb_entry *fdb) |
| 73 | { |
Nikolay Aleksandrov | 29e63ff | 2019-10-29 13:45:54 +0200 | [diff] [blame] | 74 | return !test_bit(BR_FDB_STATIC, &fdb->flags) && |
Nikolay Aleksandrov | b5cd9f7 | 2019-10-29 13:45:57 +0200 | [diff] [blame] | 75 | !test_bit(BR_FDB_ADDED_BY_EXT_LEARN, &fdb->flags) && |
Nikolay Aleksandrov | 29e63ff | 2019-10-29 13:45:54 +0200 | [diff] [blame] | 76 | time_before_eq(fdb->updated + hold_time(br), jiffies); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Michał Mirosław | da67829 | 2009-06-05 05:35:28 +0000 | [diff] [blame] | 79 | static void fdb_rcu_free(struct rcu_head *head) |
| 80 | { |
| 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); |
| 84 | } |
| 85 | |
Vladimir Oltean | 4682048 | 2021-10-26 17:27:36 +0300 | [diff] [blame] | 86 | static int fdb_to_nud(const struct net_bridge *br, |
| 87 | const struct net_bridge_fdb_entry *fdb) |
| 88 | { |
| 89 | if (test_bit(BR_FDB_LOCAL, &fdb->flags)) |
| 90 | return NUD_PERMANENT; |
| 91 | else if (test_bit(BR_FDB_STATIC, &fdb->flags)) |
| 92 | return NUD_NOARP; |
| 93 | else if (has_expired(br, fdb)) |
| 94 | return NUD_STALE; |
| 95 | else |
| 96 | return NUD_REACHABLE; |
| 97 | } |
| 98 | |
| 99 | static int fdb_fill_info(struct sk_buff *skb, const struct net_bridge *br, |
| 100 | const struct net_bridge_fdb_entry *fdb, |
| 101 | u32 portid, u32 seq, int type, unsigned int flags) |
| 102 | { |
| 103 | const struct net_bridge_port *dst = READ_ONCE(fdb->dst); |
| 104 | unsigned long now = jiffies; |
| 105 | struct nda_cacheinfo ci; |
| 106 | struct nlmsghdr *nlh; |
| 107 | struct ndmsg *ndm; |
| 108 | |
| 109 | nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags); |
| 110 | if (nlh == NULL) |
| 111 | return -EMSGSIZE; |
| 112 | |
| 113 | ndm = nlmsg_data(nlh); |
| 114 | ndm->ndm_family = AF_BRIDGE; |
| 115 | ndm->ndm_pad1 = 0; |
| 116 | ndm->ndm_pad2 = 0; |
| 117 | ndm->ndm_flags = 0; |
| 118 | ndm->ndm_type = 0; |
| 119 | ndm->ndm_ifindex = dst ? dst->dev->ifindex : br->dev->ifindex; |
| 120 | ndm->ndm_state = fdb_to_nud(br, fdb); |
| 121 | |
| 122 | if (test_bit(BR_FDB_OFFLOADED, &fdb->flags)) |
| 123 | ndm->ndm_flags |= NTF_OFFLOADED; |
| 124 | if (test_bit(BR_FDB_ADDED_BY_EXT_LEARN, &fdb->flags)) |
| 125 | ndm->ndm_flags |= NTF_EXT_LEARNED; |
| 126 | if (test_bit(BR_FDB_STICKY, &fdb->flags)) |
| 127 | ndm->ndm_flags |= NTF_STICKY; |
| 128 | |
| 129 | if (nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->key.addr)) |
| 130 | goto nla_put_failure; |
| 131 | if (nla_put_u32(skb, NDA_MASTER, br->dev->ifindex)) |
| 132 | goto nla_put_failure; |
| 133 | ci.ndm_used = jiffies_to_clock_t(now - fdb->used); |
| 134 | ci.ndm_confirmed = 0; |
| 135 | ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated); |
| 136 | ci.ndm_refcnt = 0; |
| 137 | if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci)) |
| 138 | goto nla_put_failure; |
| 139 | |
| 140 | if (fdb->key.vlan_id && nla_put(skb, NDA_VLAN, sizeof(u16), |
| 141 | &fdb->key.vlan_id)) |
| 142 | goto nla_put_failure; |
| 143 | |
| 144 | if (test_bit(BR_FDB_NOTIFY, &fdb->flags)) { |
| 145 | struct nlattr *nest = nla_nest_start(skb, NDA_FDB_EXT_ATTRS); |
| 146 | u8 notify_bits = FDB_NOTIFY_BIT; |
| 147 | |
| 148 | if (!nest) |
| 149 | goto nla_put_failure; |
| 150 | if (test_bit(BR_FDB_NOTIFY_INACTIVE, &fdb->flags)) |
| 151 | notify_bits |= FDB_NOTIFY_INACTIVE_BIT; |
| 152 | |
| 153 | if (nla_put_u8(skb, NFEA_ACTIVITY_NOTIFY, notify_bits)) { |
| 154 | nla_nest_cancel(skb, nest); |
| 155 | goto nla_put_failure; |
| 156 | } |
| 157 | |
| 158 | nla_nest_end(skb, nest); |
| 159 | } |
| 160 | |
| 161 | nlmsg_end(skb, nlh); |
| 162 | return 0; |
| 163 | |
| 164 | nla_put_failure: |
| 165 | nlmsg_cancel(skb, nlh); |
| 166 | return -EMSGSIZE; |
| 167 | } |
| 168 | |
| 169 | static inline size_t fdb_nlmsg_size(void) |
| 170 | { |
| 171 | return NLMSG_ALIGN(sizeof(struct ndmsg)) |
| 172 | + nla_total_size(ETH_ALEN) /* NDA_LLADDR */ |
| 173 | + nla_total_size(sizeof(u32)) /* NDA_MASTER */ |
| 174 | + nla_total_size(sizeof(u16)) /* NDA_VLAN */ |
| 175 | + nla_total_size(sizeof(struct nda_cacheinfo)) |
| 176 | + nla_total_size(0) /* NDA_FDB_EXT_ATTRS */ |
| 177 | + nla_total_size(sizeof(u8)); /* NFEA_ACTIVITY_NOTIFY */ |
| 178 | } |
| 179 | |
| 180 | static void fdb_notify(struct net_bridge *br, |
| 181 | const struct net_bridge_fdb_entry *fdb, int type, |
| 182 | bool swdev_notify) |
| 183 | { |
| 184 | struct net *net = dev_net(br->dev); |
| 185 | struct sk_buff *skb; |
| 186 | int err = -ENOBUFS; |
| 187 | |
| 188 | if (swdev_notify) |
| 189 | br_switchdev_fdb_notify(br, fdb, type); |
| 190 | |
| 191 | skb = nlmsg_new(fdb_nlmsg_size(), GFP_ATOMIC); |
| 192 | if (skb == NULL) |
| 193 | goto errout; |
| 194 | |
| 195 | err = fdb_fill_info(skb, br, fdb, 0, 0, type, 0); |
| 196 | if (err < 0) { |
| 197 | /* -EMSGSIZE implies BUG in fdb_nlmsg_size() */ |
| 198 | WARN_ON(err == -EMSGSIZE); |
| 199 | kfree_skb(skb); |
| 200 | goto errout; |
| 201 | } |
| 202 | rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC); |
| 203 | return; |
| 204 | errout: |
| 205 | rtnl_set_sk_err(net, RTNLGRP_NEIGH, err); |
| 206 | } |
| 207 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 208 | static struct net_bridge_fdb_entry *fdb_find_rcu(struct rhashtable *tbl, |
Nikolay Aleksandrov | bfd0aea | 2017-02-13 14:59:09 +0100 | [diff] [blame] | 209 | const unsigned char *addr, |
| 210 | __u16 vid) |
| 211 | { |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 212 | struct net_bridge_fdb_key key; |
Nikolay Aleksandrov | bfd0aea | 2017-02-13 14:59:09 +0100 | [diff] [blame] | 213 | |
Nikolay Aleksandrov | 410b3d4 | 2017-02-13 14:59:10 +0100 | [diff] [blame] | 214 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 215 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 216 | key.vlan_id = vid; |
| 217 | memcpy(key.addr.addr, addr, sizeof(key.addr.addr)); |
Nikolay Aleksandrov | bfd0aea | 2017-02-13 14:59:09 +0100 | [diff] [blame] | 218 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 219 | return rhashtable_lookup(tbl, &key, br_fdb_rht_params); |
Nikolay Aleksandrov | bfd0aea | 2017-02-13 14:59:09 +0100 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | /* requires bridge hash_lock */ |
| 223 | static struct net_bridge_fdb_entry *br_fdb_find(struct net_bridge *br, |
| 224 | const unsigned char *addr, |
| 225 | __u16 vid) |
| 226 | { |
Nikolay Aleksandrov | bfd0aea | 2017-02-13 14:59:09 +0100 | [diff] [blame] | 227 | struct net_bridge_fdb_entry *fdb; |
| 228 | |
WANG Cong | d12c917 | 2017-03-16 10:32:42 -0700 | [diff] [blame] | 229 | lockdep_assert_held_once(&br->hash_lock); |
Nikolay Aleksandrov | 410b3d4 | 2017-02-13 14:59:10 +0100 | [diff] [blame] | 230 | |
Nikolay Aleksandrov | bfd0aea | 2017-02-13 14:59:09 +0100 | [diff] [blame] | 231 | rcu_read_lock(); |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 232 | fdb = fdb_find_rcu(&br->fdb_hash_tbl, addr, vid); |
Nikolay Aleksandrov | bfd0aea | 2017-02-13 14:59:09 +0100 | [diff] [blame] | 233 | rcu_read_unlock(); |
| 234 | |
| 235 | return fdb; |
| 236 | } |
| 237 | |
Petr Machata | 4d4fd36 | 2018-04-29 10:56:08 +0300 | [diff] [blame] | 238 | struct net_device *br_fdb_find_port(const struct net_device *br_dev, |
| 239 | const unsigned char *addr, |
| 240 | __u16 vid) |
| 241 | { |
| 242 | struct net_bridge_fdb_entry *f; |
| 243 | struct net_device *dev = NULL; |
| 244 | struct net_bridge *br; |
| 245 | |
| 246 | ASSERT_RTNL(); |
| 247 | |
| 248 | if (!netif_is_bridge_master(br_dev)) |
| 249 | return NULL; |
| 250 | |
| 251 | br = netdev_priv(br_dev); |
Petr Machata | 873aca2 | 2018-06-08 15:11:47 +0200 | [diff] [blame] | 252 | rcu_read_lock(); |
| 253 | f = br_fdb_find_rcu(br, addr, vid); |
Petr Machata | 4d4fd36 | 2018-04-29 10:56:08 +0300 | [diff] [blame] | 254 | if (f && f->dst) |
| 255 | dev = f->dst->dev; |
Petr Machata | 873aca2 | 2018-06-08 15:11:47 +0200 | [diff] [blame] | 256 | rcu_read_unlock(); |
Petr Machata | 4d4fd36 | 2018-04-29 10:56:08 +0300 | [diff] [blame] | 257 | |
| 258 | return dev; |
| 259 | } |
| 260 | EXPORT_SYMBOL_GPL(br_fdb_find_port); |
| 261 | |
Nikolay Aleksandrov | bfd0aea | 2017-02-13 14:59:09 +0100 | [diff] [blame] | 262 | struct net_bridge_fdb_entry *br_fdb_find_rcu(struct net_bridge *br, |
| 263 | const unsigned char *addr, |
| 264 | __u16 vid) |
| 265 | { |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 266 | return fdb_find_rcu(&br->fdb_hash_tbl, addr, vid); |
Nikolay Aleksandrov | bfd0aea | 2017-02-13 14:59:09 +0100 | [diff] [blame] | 267 | } |
| 268 | |
Vlad Yasevich | 145beee | 2014-05-16 09:59:19 -0400 | [diff] [blame] | 269 | /* When a static FDB entry is added, the mac address from the entry is |
| 270 | * added to the bridge private HW address list and all required ports |
| 271 | * are then updated with the new information. |
| 272 | * Called under RTNL. |
| 273 | */ |
Jiri Pirko | 020ec6b | 2014-11-28 14:34:12 +0100 | [diff] [blame] | 274 | static void fdb_add_hw_addr(struct net_bridge *br, const unsigned char *addr) |
Vlad Yasevich | 145beee | 2014-05-16 09:59:19 -0400 | [diff] [blame] | 275 | { |
| 276 | int err; |
Li RongQing | a3f5ee7 | 2014-06-18 16:07:16 +0800 | [diff] [blame] | 277 | struct net_bridge_port *p; |
Vlad Yasevich | 145beee | 2014-05-16 09:59:19 -0400 | [diff] [blame] | 278 | |
| 279 | ASSERT_RTNL(); |
| 280 | |
| 281 | list_for_each_entry(p, &br->port_list, list) { |
| 282 | if (!br_promisc_port(p)) { |
| 283 | err = dev_uc_add(p->dev, addr); |
| 284 | if (err) |
| 285 | goto undo; |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | return; |
| 290 | undo: |
Li RongQing | a3f5ee7 | 2014-06-18 16:07:16 +0800 | [diff] [blame] | 291 | list_for_each_entry_continue_reverse(p, &br->port_list, list) { |
| 292 | if (!br_promisc_port(p)) |
| 293 | dev_uc_del(p->dev, addr); |
Vlad Yasevich | 145beee | 2014-05-16 09:59:19 -0400 | [diff] [blame] | 294 | } |
| 295 | } |
| 296 | |
| 297 | /* When a static FDB entry is deleted, the HW address from that entry is |
| 298 | * also removed from the bridge private HW address list and updates all |
| 299 | * the ports with needed information. |
| 300 | * Called under RTNL. |
| 301 | */ |
Jiri Pirko | 020ec6b | 2014-11-28 14:34:12 +0100 | [diff] [blame] | 302 | static void fdb_del_hw_addr(struct net_bridge *br, const unsigned char *addr) |
Vlad Yasevich | 145beee | 2014-05-16 09:59:19 -0400 | [diff] [blame] | 303 | { |
| 304 | struct net_bridge_port *p; |
| 305 | |
| 306 | ASSERT_RTNL(); |
| 307 | |
| 308 | list_for_each_entry(p, &br->port_list, list) { |
| 309 | if (!br_promisc_port(p)) |
| 310 | dev_uc_del(p->dev, addr); |
| 311 | } |
| 312 | } |
| 313 | |
Petr Machata | 161d82d | 2018-05-03 14:43:53 +0200 | [diff] [blame] | 314 | static void fdb_delete(struct net_bridge *br, struct net_bridge_fdb_entry *f, |
| 315 | bool swdev_notify) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | { |
Roopa Prabhu | b74fd30 | 2017-08-29 13:16:57 -0700 | [diff] [blame] | 317 | trace_fdb_delete(br, f); |
| 318 | |
Nikolay Aleksandrov | 29e63ff | 2019-10-29 13:45:54 +0200 | [diff] [blame] | 319 | if (test_bit(BR_FDB_STATIC, &f->flags)) |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 320 | fdb_del_hw_addr(br, f->key.addr.addr); |
Vlad Yasevich | 145beee | 2014-05-16 09:59:19 -0400 | [diff] [blame] | 321 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 322 | hlist_del_init_rcu(&f->fdb_node); |
| 323 | rhashtable_remove_fast(&br->fdb_hash_tbl, &f->rhnode, |
| 324 | br_fdb_rht_params); |
Petr Machata | 161d82d | 2018-05-03 14:43:53 +0200 | [diff] [blame] | 325 | fdb_notify(br, f, RTM_DELNEIGH, swdev_notify); |
Michał Mirosław | da67829 | 2009-06-05 05:35:28 +0000 | [diff] [blame] | 326 | call_rcu(&f->rcu, fdb_rcu_free); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | } |
| 328 | |
Toshiaki Makita | 960b589 | 2014-02-07 16:48:23 +0900 | [diff] [blame] | 329 | /* Delete a local entry if no other port had the same address. */ |
| 330 | static void fdb_delete_local(struct net_bridge *br, |
| 331 | const struct net_bridge_port *p, |
| 332 | struct net_bridge_fdb_entry *f) |
| 333 | { |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 334 | const unsigned char *addr = f->key.addr.addr; |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 335 | struct net_bridge_vlan_group *vg; |
| 336 | const struct net_bridge_vlan *v; |
Toshiaki Makita | 960b589 | 2014-02-07 16:48:23 +0900 | [diff] [blame] | 337 | struct net_bridge_port *op; |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 338 | u16 vid = f->key.vlan_id; |
Toshiaki Makita | 960b589 | 2014-02-07 16:48:23 +0900 | [diff] [blame] | 339 | |
| 340 | /* Maybe another port has same hw addr? */ |
| 341 | list_for_each_entry(op, &br->port_list, list) { |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 342 | vg = nbp_vlan_group(op); |
Toshiaki Makita | 960b589 | 2014-02-07 16:48:23 +0900 | [diff] [blame] | 343 | if (op != p && ether_addr_equal(op->dev->dev_addr, addr) && |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 344 | (!vid || br_vlan_find(vg, vid))) { |
Toshiaki Makita | 960b589 | 2014-02-07 16:48:23 +0900 | [diff] [blame] | 345 | f->dst = op; |
Nikolay Aleksandrov | ac3ca6a | 2019-10-29 13:45:56 +0200 | [diff] [blame] | 346 | clear_bit(BR_FDB_ADDED_BY_USER, &f->flags); |
Toshiaki Makita | 960b589 | 2014-02-07 16:48:23 +0900 | [diff] [blame] | 347 | return; |
| 348 | } |
| 349 | } |
| 350 | |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 351 | vg = br_vlan_group(br); |
| 352 | v = br_vlan_find(vg, vid); |
Toshiaki Makita | 960b589 | 2014-02-07 16:48:23 +0900 | [diff] [blame] | 353 | /* Maybe bridge device has same hw addr? */ |
| 354 | if (p && ether_addr_equal(br->dev->dev_addr, addr) && |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 355 | (!vid || (v && br_vlan_should_use(v)))) { |
Toshiaki Makita | 960b589 | 2014-02-07 16:48:23 +0900 | [diff] [blame] | 356 | f->dst = NULL; |
Nikolay Aleksandrov | ac3ca6a | 2019-10-29 13:45:56 +0200 | [diff] [blame] | 357 | clear_bit(BR_FDB_ADDED_BY_USER, &f->flags); |
Toshiaki Makita | 960b589 | 2014-02-07 16:48:23 +0900 | [diff] [blame] | 358 | return; |
| 359 | } |
| 360 | |
Petr Machata | 161d82d | 2018-05-03 14:43:53 +0200 | [diff] [blame] | 361 | fdb_delete(br, f, true); |
Toshiaki Makita | 960b589 | 2014-02-07 16:48:23 +0900 | [diff] [blame] | 362 | } |
| 363 | |
Toshiaki Makita | 424bb9c | 2014-02-07 16:48:25 +0900 | [diff] [blame] | 364 | void br_fdb_find_delete_local(struct net_bridge *br, |
| 365 | const struct net_bridge_port *p, |
| 366 | const unsigned char *addr, u16 vid) |
| 367 | { |
Toshiaki Makita | 424bb9c | 2014-02-07 16:48:25 +0900 | [diff] [blame] | 368 | struct net_bridge_fdb_entry *f; |
| 369 | |
| 370 | spin_lock_bh(&br->hash_lock); |
Nikolay Aleksandrov | bfd0aea | 2017-02-13 14:59:09 +0100 | [diff] [blame] | 371 | f = br_fdb_find(br, addr, vid); |
Nikolay Aleksandrov | 6869c3b | 2019-10-29 13:45:53 +0200 | [diff] [blame] | 372 | if (f && test_bit(BR_FDB_LOCAL, &f->flags) && |
Nikolay Aleksandrov | ac3ca6a | 2019-10-29 13:45:56 +0200 | [diff] [blame] | 373 | !test_bit(BR_FDB_ADDED_BY_USER, &f->flags) && f->dst == p) |
Toshiaki Makita | 424bb9c | 2014-02-07 16:48:25 +0900 | [diff] [blame] | 374 | fdb_delete_local(br, p, f); |
| 375 | spin_unlock_bh(&br->hash_lock); |
| 376 | } |
| 377 | |
Vladimir Oltean | 5f94a5e | 2021-10-26 17:27:37 +0300 | [diff] [blame] | 378 | static struct net_bridge_fdb_entry *fdb_create(struct net_bridge *br, |
| 379 | struct net_bridge_port *source, |
| 380 | const unsigned char *addr, |
| 381 | __u16 vid, |
| 382 | unsigned long flags) |
| 383 | { |
| 384 | struct net_bridge_fdb_entry *fdb; |
Vladimir Oltean | 9574fb5 | 2021-10-26 17:27:40 +0300 | [diff] [blame^] | 385 | int err; |
Vladimir Oltean | 5f94a5e | 2021-10-26 17:27:37 +0300 | [diff] [blame] | 386 | |
| 387 | fdb = kmem_cache_alloc(br_fdb_cache, GFP_ATOMIC); |
Vladimir Oltean | 9574fb5 | 2021-10-26 17:27:40 +0300 | [diff] [blame^] | 388 | if (!fdb) |
| 389 | return NULL; |
| 390 | |
| 391 | memcpy(fdb->key.addr.addr, addr, ETH_ALEN); |
| 392 | WRITE_ONCE(fdb->dst, source); |
| 393 | fdb->key.vlan_id = vid; |
| 394 | fdb->flags = flags; |
| 395 | fdb->updated = fdb->used = jiffies; |
| 396 | err = rhashtable_lookup_insert_fast(&br->fdb_hash_tbl, &fdb->rhnode, |
| 397 | br_fdb_rht_params); |
| 398 | if (err) { |
| 399 | kmem_cache_free(br_fdb_cache, fdb); |
| 400 | return NULL; |
Vladimir Oltean | 5f94a5e | 2021-10-26 17:27:37 +0300 | [diff] [blame] | 401 | } |
Vladimir Oltean | 9574fb5 | 2021-10-26 17:27:40 +0300 | [diff] [blame^] | 402 | |
| 403 | hlist_add_head_rcu(&fdb->fdb_node, &br->fdb_list); |
| 404 | |
Vladimir Oltean | 5f94a5e | 2021-10-26 17:27:37 +0300 | [diff] [blame] | 405 | return fdb; |
| 406 | } |
| 407 | |
Vladimir Oltean | 4731b6d | 2021-10-26 17:27:38 +0300 | [diff] [blame] | 408 | static int fdb_add_local(struct net_bridge *br, struct net_bridge_port *source, |
| 409 | const unsigned char *addr, u16 vid) |
Vladimir Oltean | 5f94a5e | 2021-10-26 17:27:37 +0300 | [diff] [blame] | 410 | { |
| 411 | struct net_bridge_fdb_entry *fdb; |
| 412 | |
| 413 | if (!is_valid_ether_addr(addr)) |
| 414 | return -EINVAL; |
| 415 | |
| 416 | fdb = br_fdb_find(br, addr, vid); |
| 417 | if (fdb) { |
| 418 | /* it is okay to have multiple ports with same |
| 419 | * address, just use the first one. |
| 420 | */ |
| 421 | if (test_bit(BR_FDB_LOCAL, &fdb->flags)) |
| 422 | return 0; |
| 423 | br_warn(br, "adding interface %s with same address as a received packet (addr:%pM, vlan:%u)\n", |
| 424 | source ? source->dev->name : br->dev->name, addr, vid); |
| 425 | fdb_delete(br, fdb, true); |
| 426 | } |
| 427 | |
| 428 | fdb = fdb_create(br, source, addr, vid, |
| 429 | BIT(BR_FDB_LOCAL) | BIT(BR_FDB_STATIC)); |
| 430 | if (!fdb) |
| 431 | return -ENOMEM; |
| 432 | |
| 433 | fdb_add_hw_addr(br, addr); |
| 434 | fdb_notify(br, fdb, RTM_NEWNEIGH, true); |
| 435 | return 0; |
| 436 | } |
| 437 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | void br_fdb_changeaddr(struct net_bridge_port *p, const unsigned char *newaddr) |
| 439 | { |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 440 | struct net_bridge_vlan_group *vg; |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 441 | struct net_bridge_fdb_entry *f; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | struct net_bridge *br = p->br; |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 443 | struct net_bridge_vlan *v; |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 444 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | spin_lock_bh(&br->hash_lock); |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 446 | vg = nbp_vlan_group(p); |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 447 | hlist_for_each_entry(f, &br->fdb_list, fdb_node) { |
Nikolay Aleksandrov | 6869c3b | 2019-10-29 13:45:53 +0200 | [diff] [blame] | 448 | if (f->dst == p && test_bit(BR_FDB_LOCAL, &f->flags) && |
Nikolay Aleksandrov | ac3ca6a | 2019-10-29 13:45:56 +0200 | [diff] [blame] | 449 | !test_bit(BR_FDB_ADDED_BY_USER, &f->flags)) { |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 450 | /* delete old one */ |
| 451 | fdb_delete_local(br, p, f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 453 | /* if this port has no vlan information |
| 454 | * configured, we can safely be done at |
| 455 | * this point. |
| 456 | */ |
| 457 | if (!vg || !vg->num_vlans) |
| 458 | goto insert; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | } |
| 460 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | |
Toshiaki Makita | 2836882 | 2014-02-07 16:48:19 +0900 | [diff] [blame] | 462 | insert: |
| 463 | /* insert new address, may fail if invalid address or dup. */ |
Vladimir Oltean | 4731b6d | 2021-10-26 17:27:38 +0300 | [diff] [blame] | 464 | fdb_add_local(br, p, newaddr, 0); |
Toshiaki Makita | 2836882 | 2014-02-07 16:48:19 +0900 | [diff] [blame] | 465 | |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 466 | if (!vg || !vg->num_vlans) |
Toshiaki Makita | 2836882 | 2014-02-07 16:48:19 +0900 | [diff] [blame] | 467 | goto done; |
| 468 | |
| 469 | /* Now add entries for every VLAN configured on the port. |
| 470 | * This function runs under RTNL so the bitmap will not change |
| 471 | * from under us. |
| 472 | */ |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 473 | list_for_each_entry(v, &vg->vlan_list, vlist) |
Vladimir Oltean | 4731b6d | 2021-10-26 17:27:38 +0300 | [diff] [blame] | 474 | fdb_add_local(br, p, newaddr, v->vid); |
Toshiaki Makita | 2836882 | 2014-02-07 16:48:19 +0900 | [diff] [blame] | 475 | |
Vlad Yasevich | bc9a25d | 2013-02-13 12:00:19 +0000 | [diff] [blame] | 476 | done: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | spin_unlock_bh(&br->hash_lock); |
| 478 | } |
| 479 | |
stephen hemminger | 4359881 | 2011-12-08 07:17:49 +0000 | [diff] [blame] | 480 | void br_fdb_change_mac_address(struct net_bridge *br, const u8 *newaddr) |
| 481 | { |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 482 | struct net_bridge_vlan_group *vg; |
stephen hemminger | 4359881 | 2011-12-08 07:17:49 +0000 | [diff] [blame] | 483 | struct net_bridge_fdb_entry *f; |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 484 | struct net_bridge_vlan *v; |
stephen hemminger | 4359881 | 2011-12-08 07:17:49 +0000 | [diff] [blame] | 485 | |
Toshiaki Makita | ac4c886 | 2014-02-07 16:48:26 +0900 | [diff] [blame] | 486 | spin_lock_bh(&br->hash_lock); |
| 487 | |
stephen hemminger | 4359881 | 2011-12-08 07:17:49 +0000 | [diff] [blame] | 488 | /* If old entry was unassociated with any port, then delete it. */ |
Nikolay Aleksandrov | bfd0aea | 2017-02-13 14:59:09 +0100 | [diff] [blame] | 489 | f = br_fdb_find(br, br->dev->dev_addr, 0); |
Nikolay Aleksandrov | 6869c3b | 2019-10-29 13:45:53 +0200 | [diff] [blame] | 490 | if (f && test_bit(BR_FDB_LOCAL, &f->flags) && |
Nikolay Aleksandrov | ac3ca6a | 2019-10-29 13:45:56 +0200 | [diff] [blame] | 491 | !f->dst && !test_bit(BR_FDB_ADDED_BY_USER, &f->flags)) |
Toshiaki Makita | 960b589 | 2014-02-07 16:48:23 +0900 | [diff] [blame] | 492 | fdb_delete_local(br, NULL, f); |
stephen hemminger | 4359881 | 2011-12-08 07:17:49 +0000 | [diff] [blame] | 493 | |
Vladimir Oltean | 4731b6d | 2021-10-26 17:27:38 +0300 | [diff] [blame] | 494 | fdb_add_local(br, NULL, newaddr, 0); |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 495 | vg = br_vlan_group(br); |
| 496 | if (!vg || !vg->num_vlans) |
| 497 | goto out; |
Vlad Yasevich | bc9a25d | 2013-02-13 12:00:19 +0000 | [diff] [blame] | 498 | /* Now remove and add entries for every VLAN configured on the |
| 499 | * bridge. This function runs under RTNL so the bitmap will not |
| 500 | * change from under us. |
| 501 | */ |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 502 | list_for_each_entry(v, &vg->vlan_list, vlist) { |
Toshiaki Makita | 0b148de | 2016-06-07 19:14:17 +0900 | [diff] [blame] | 503 | if (!br_vlan_should_use(v)) |
| 504 | continue; |
Nikolay Aleksandrov | bfd0aea | 2017-02-13 14:59:09 +0100 | [diff] [blame] | 505 | f = br_fdb_find(br, br->dev->dev_addr, v->vid); |
Nikolay Aleksandrov | 6869c3b | 2019-10-29 13:45:53 +0200 | [diff] [blame] | 506 | if (f && test_bit(BR_FDB_LOCAL, &f->flags) && |
Nikolay Aleksandrov | ac3ca6a | 2019-10-29 13:45:56 +0200 | [diff] [blame] | 507 | !f->dst && !test_bit(BR_FDB_ADDED_BY_USER, &f->flags)) |
Toshiaki Makita | 960b589 | 2014-02-07 16:48:23 +0900 | [diff] [blame] | 508 | fdb_delete_local(br, NULL, f); |
Vladimir Oltean | 4731b6d | 2021-10-26 17:27:38 +0300 | [diff] [blame] | 509 | fdb_add_local(br, NULL, newaddr, v->vid); |
Vlad Yasevich | bc9a25d | 2013-02-13 12:00:19 +0000 | [diff] [blame] | 510 | } |
Toshiaki Makita | ac4c886 | 2014-02-07 16:48:26 +0900 | [diff] [blame] | 511 | out: |
| 512 | spin_unlock_bh(&br->hash_lock); |
stephen hemminger | 4359881 | 2011-12-08 07:17:49 +0000 | [diff] [blame] | 513 | } |
| 514 | |
Nikolay Aleksandrov | f7cdee8 | 2017-02-04 18:05:07 +0100 | [diff] [blame] | 515 | void br_fdb_cleanup(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | { |
Nikolay Aleksandrov | f7cdee8 | 2017-02-04 18:05:07 +0100 | [diff] [blame] | 517 | struct net_bridge *br = container_of(work, struct net_bridge, |
| 518 | gc_work.work); |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 519 | struct net_bridge_fdb_entry *f = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | unsigned long delay = hold_time(br); |
Nikolay Aleksandrov | f7cdee8 | 2017-02-04 18:05:07 +0100 | [diff] [blame] | 521 | unsigned long work_delay = delay; |
| 522 | unsigned long now = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 524 | /* this part is tricky, in order to avoid blocking learning and |
| 525 | * consequently forwarding, we rely on rcu to delete objects with |
| 526 | * delayed freeing allowing us to continue traversing |
| 527 | */ |
| 528 | rcu_read_lock(); |
| 529 | hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) { |
Nikolay Aleksandrov | 31cbc39b | 2020-06-23 23:47:17 +0300 | [diff] [blame] | 530 | unsigned long this_timer = f->updated + delay; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | |
Nikolay Aleksandrov | 29e63ff | 2019-10-29 13:45:54 +0200 | [diff] [blame] | 532 | if (test_bit(BR_FDB_STATIC, &f->flags) || |
Nikolay Aleksandrov | 31cbc39b | 2020-06-23 23:47:17 +0300 | [diff] [blame] | 533 | test_bit(BR_FDB_ADDED_BY_EXT_LEARN, &f->flags)) { |
| 534 | if (test_bit(BR_FDB_NOTIFY, &f->flags)) { |
| 535 | if (time_after(this_timer, now)) |
| 536 | work_delay = min(work_delay, |
| 537 | this_timer - now); |
| 538 | else if (!test_and_set_bit(BR_FDB_NOTIFY_INACTIVE, |
| 539 | &f->flags)) |
| 540 | fdb_notify(br, f, RTM_NEWNEIGH, false); |
| 541 | } |
Nikolay Aleksandrov | f7cdee8 | 2017-02-04 18:05:07 +0100 | [diff] [blame] | 542 | continue; |
Nikolay Aleksandrov | 31cbc39b | 2020-06-23 23:47:17 +0300 | [diff] [blame] | 543 | } |
| 544 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 545 | if (time_after(this_timer, now)) { |
| 546 | work_delay = min(work_delay, this_timer - now); |
| 547 | } else { |
| 548 | spin_lock_bh(&br->hash_lock); |
| 549 | if (!hlist_unhashed(&f->fdb_node)) |
Petr Machata | 161d82d | 2018-05-03 14:43:53 +0200 | [diff] [blame] | 550 | fdb_delete(br, f, true); |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 551 | spin_unlock_bh(&br->hash_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | } |
| 553 | } |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 554 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | |
Nikolay Aleksandrov | f7cdee8 | 2017-02-04 18:05:07 +0100 | [diff] [blame] | 556 | /* Cleanup minimum 10 milliseconds apart */ |
| 557 | work_delay = max_t(unsigned long, work_delay, msecs_to_jiffies(10)); |
| 558 | mod_delayed_work(system_long_wq, &br->gc_work, work_delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | } |
| 560 | |
Stephen Hemminger | 9cf6374 | 2007-04-09 12:57:54 -0700 | [diff] [blame] | 561 | /* Completely flush all dynamic entries in forwarding database.*/ |
| 562 | void br_fdb_flush(struct net_bridge *br) |
| 563 | { |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 564 | struct net_bridge_fdb_entry *f; |
| 565 | struct hlist_node *tmp; |
Stephen Hemminger | 1a62069 | 2006-10-12 14:45:38 -0700 | [diff] [blame] | 566 | |
Stephen Hemminger | 9cf6374 | 2007-04-09 12:57:54 -0700 | [diff] [blame] | 567 | spin_lock_bh(&br->hash_lock); |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 568 | hlist_for_each_entry_safe(f, tmp, &br->fdb_list, fdb_node) { |
Nikolay Aleksandrov | 29e63ff | 2019-10-29 13:45:54 +0200 | [diff] [blame] | 569 | if (!test_bit(BR_FDB_STATIC, &f->flags)) |
Petr Machata | 161d82d | 2018-05-03 14:43:53 +0200 | [diff] [blame] | 570 | fdb_delete(br, f, true); |
Stephen Hemminger | 9cf6374 | 2007-04-09 12:57:54 -0700 | [diff] [blame] | 571 | } |
| 572 | spin_unlock_bh(&br->hash_lock); |
| 573 | } |
| 574 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 575 | /* Flush all entries referring to a specific port. |
Stephen Hemminger | 9cf6374 | 2007-04-09 12:57:54 -0700 | [diff] [blame] | 576 | * if do_all is set also flush static entries |
Nikolay Aleksandrov | 1ea2d02 | 2015-06-23 05:28:16 -0700 | [diff] [blame] | 577 | * if vid is set delete all entries that match the vlan_id |
Stephen Hemminger | 9cf6374 | 2007-04-09 12:57:54 -0700 | [diff] [blame] | 578 | */ |
Stephen Hemminger | 1a62069 | 2006-10-12 14:45:38 -0700 | [diff] [blame] | 579 | void br_fdb_delete_by_port(struct net_bridge *br, |
| 580 | const struct net_bridge_port *p, |
Nikolay Aleksandrov | 1ea2d02 | 2015-06-23 05:28:16 -0700 | [diff] [blame] | 581 | u16 vid, |
Stephen Hemminger | 1a62069 | 2006-10-12 14:45:38 -0700 | [diff] [blame] | 582 | int do_all) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | { |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 584 | struct net_bridge_fdb_entry *f; |
| 585 | struct hlist_node *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | |
| 587 | spin_lock_bh(&br->hash_lock); |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 588 | hlist_for_each_entry_safe(f, tmp, &br->fdb_list, fdb_node) { |
| 589 | if (f->dst != p) |
| 590 | continue; |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 591 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 592 | if (!do_all) |
Nikolay Aleksandrov | 29e63ff | 2019-10-29 13:45:54 +0200 | [diff] [blame] | 593 | if (test_bit(BR_FDB_STATIC, &f->flags) || |
Nikolay Aleksandrov | f2f3729 | 2020-09-28 18:30:02 +0300 | [diff] [blame] | 594 | (test_bit(BR_FDB_ADDED_BY_EXT_LEARN, &f->flags) && |
| 595 | !test_bit(BR_FDB_OFFLOADED, &f->flags)) || |
Nikolay Aleksandrov | 29e63ff | 2019-10-29 13:45:54 +0200 | [diff] [blame] | 596 | (vid && f->key.vlan_id != vid)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | continue; |
| 598 | |
Nikolay Aleksandrov | 6869c3b | 2019-10-29 13:45:53 +0200 | [diff] [blame] | 599 | if (test_bit(BR_FDB_LOCAL, &f->flags)) |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 600 | fdb_delete_local(br, p, f); |
| 601 | else |
Petr Machata | 161d82d | 2018-05-03 14:43:53 +0200 | [diff] [blame] | 602 | fdb_delete(br, f, true); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | } |
| 604 | spin_unlock_bh(&br->hash_lock); |
| 605 | } |
| 606 | |
Igor Maravić | e6373c4 | 2011-12-12 02:58:25 +0000 | [diff] [blame] | 607 | #if IS_ENABLED(CONFIG_ATM_LANE) |
Michał Mirosław | da67829 | 2009-06-05 05:35:28 +0000 | [diff] [blame] | 608 | /* Interface used by ATM LANE hook to test |
| 609 | * if an addr is on some other bridge port */ |
| 610 | int br_fdb_test_addr(struct net_device *dev, unsigned char *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | { |
| 612 | struct net_bridge_fdb_entry *fdb; |
stephen hemminger | b5ed54e | 2010-11-15 06:38:13 +0000 | [diff] [blame] | 613 | struct net_bridge_port *port; |
Michał Mirosław | da67829 | 2009-06-05 05:35:28 +0000 | [diff] [blame] | 614 | int ret; |
| 615 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | rcu_read_lock(); |
stephen hemminger | b5ed54e | 2010-11-15 06:38:13 +0000 | [diff] [blame] | 617 | port = br_port_get_rcu(dev); |
| 618 | if (!port) |
| 619 | ret = 0; |
| 620 | else { |
Vladimir Oltean | 3e19ae7 | 2021-06-29 17:06:44 +0300 | [diff] [blame] | 621 | const struct net_bridge_port *dst = NULL; |
| 622 | |
Nikolay Aleksandrov | bfd0aea | 2017-02-13 14:59:09 +0100 | [diff] [blame] | 623 | fdb = br_fdb_find_rcu(port->br, addr, 0); |
Vladimir Oltean | 3e19ae7 | 2021-06-29 17:06:44 +0300 | [diff] [blame] | 624 | if (fdb) |
| 625 | dst = READ_ONCE(fdb->dst); |
| 626 | |
| 627 | ret = dst && dst->dev != dev && |
| 628 | dst->state == BR_STATE_FORWARDING; |
stephen hemminger | b5ed54e | 2010-11-15 06:38:13 +0000 | [diff] [blame] | 629 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | |
Michał Mirosław | da67829 | 2009-06-05 05:35:28 +0000 | [diff] [blame] | 632 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | } |
Michał Mirosław | da67829 | 2009-06-05 05:35:28 +0000 | [diff] [blame] | 634 | #endif /* CONFIG_ATM_LANE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | |
| 636 | /* |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 637 | * Fill buffer with forwarding table records in |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | * the API format. |
| 639 | */ |
| 640 | int br_fdb_fillbuf(struct net_bridge *br, void *buf, |
| 641 | unsigned long maxnum, unsigned long skip) |
| 642 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | struct net_bridge_fdb_entry *f; |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 644 | struct __fdb_entry *fe = buf; |
| 645 | int num = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | |
| 647 | memset(buf, 0, maxnum*sizeof(struct __fdb_entry)); |
| 648 | |
| 649 | rcu_read_lock(); |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 650 | hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) { |
| 651 | if (num >= maxnum) |
| 652 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 654 | if (has_expired(br, f)) |
| 655 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 657 | /* ignore pseudo entry for local MAC address */ |
| 658 | if (!f->dst) |
| 659 | continue; |
stephen hemminger | 4359881 | 2011-12-08 07:17:49 +0000 | [diff] [blame] | 660 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 661 | if (skip) { |
| 662 | --skip; |
| 663 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 666 | /* convert from internal format to API */ |
| 667 | memcpy(fe->mac_addr, f->key.addr.addr, ETH_ALEN); |
| 668 | |
| 669 | /* due to ABI compat need to split into hi/lo */ |
| 670 | fe->port_no = f->dst->port_no; |
| 671 | fe->port_hi = f->dst->port_no >> 8; |
| 672 | |
Nikolay Aleksandrov | 6869c3b | 2019-10-29 13:45:53 +0200 | [diff] [blame] | 673 | fe->is_local = test_bit(BR_FDB_LOCAL, &f->flags); |
Nikolay Aleksandrov | 29e63ff | 2019-10-29 13:45:54 +0200 | [diff] [blame] | 674 | if (!test_bit(BR_FDB_STATIC, &f->flags)) |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 675 | fe->ageing_timer_value = jiffies_delta_to_clock_t(jiffies - f->updated); |
| 676 | ++fe; |
| 677 | ++num; |
| 678 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | rcu_read_unlock(); |
| 680 | |
| 681 | return num; |
| 682 | } |
| 683 | |
stephen hemminger | 03e9b64 | 2011-04-04 14:03:27 +0000 | [diff] [blame] | 684 | /* Add entry for local address of interface */ |
Vladimir Oltean | f6814fd | 2021-10-26 17:27:39 +0300 | [diff] [blame] | 685 | int br_fdb_add_local(struct net_bridge *br, struct net_bridge_port *source, |
| 686 | const unsigned char *addr, u16 vid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | { |
| 688 | int ret; |
| 689 | |
| 690 | spin_lock_bh(&br->hash_lock); |
Vladimir Oltean | 4731b6d | 2021-10-26 17:27:38 +0300 | [diff] [blame] | 691 | ret = fdb_add_local(br, source, addr, vid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | spin_unlock_bh(&br->hash_lock); |
| 693 | return ret; |
| 694 | } |
| 695 | |
Nikolay Aleksandrov | 31cbc39b | 2020-06-23 23:47:17 +0300 | [diff] [blame] | 696 | /* returns true if the fdb was modified */ |
| 697 | static bool __fdb_mark_active(struct net_bridge_fdb_entry *fdb) |
| 698 | { |
| 699 | return !!(test_bit(BR_FDB_NOTIFY_INACTIVE, &fdb->flags) && |
| 700 | test_and_clear_bit(BR_FDB_NOTIFY_INACTIVE, &fdb->flags)); |
| 701 | } |
| 702 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | void br_fdb_update(struct net_bridge *br, struct net_bridge_port *source, |
Nikolay Aleksandrov | be0c567 | 2019-11-01 14:46:37 +0200 | [diff] [blame] | 704 | const unsigned char *addr, u16 vid, unsigned long flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | struct net_bridge_fdb_entry *fdb; |
| 707 | |
| 708 | /* some users want to always flood. */ |
| 709 | if (hold_time(br) == 0) |
| 710 | return; |
| 711 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 712 | fdb = fdb_find_rcu(&br->fdb_hash_tbl, addr, vid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | if (likely(fdb)) { |
| 714 | /* attempt to update an entry for a local interface */ |
Nikolay Aleksandrov | 6869c3b | 2019-10-29 13:45:53 +0200 | [diff] [blame] | 715 | if (unlikely(test_bit(BR_FDB_LOCAL, &fdb->flags))) { |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 716 | if (net_ratelimit()) |
Roopa Prabhu | de1dfee | 2016-10-11 16:12:56 -0700 | [diff] [blame] | 717 | br_warn(br, "received packet on %s with own address as source address (addr:%pM, vlan:%u)\n", |
| 718 | source->dev->name, addr, vid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | } else { |
stephen hemminger | ca6d448 | 2017-02-07 08:46:46 -0800 | [diff] [blame] | 720 | unsigned long now = jiffies; |
Nikolay Aleksandrov | 31cbc39b | 2020-06-23 23:47:17 +0300 | [diff] [blame] | 721 | bool fdb_modified = false; |
| 722 | |
| 723 | if (now != fdb->updated) { |
| 724 | fdb->updated = now; |
| 725 | fdb_modified = __fdb_mark_active(fdb); |
| 726 | } |
stephen hemminger | ca6d448 | 2017-02-07 08:46:46 -0800 | [diff] [blame] | 727 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | /* fastpath: update of existing entry */ |
Vladimir Oltean | 3e19ae7 | 2021-06-29 17:06:44 +0300 | [diff] [blame] | 729 | if (unlikely(source != READ_ONCE(fdb->dst) && |
Nikolay Aleksandrov | e0458d9 | 2019-10-29 13:45:55 +0200 | [diff] [blame] | 730 | !test_bit(BR_FDB_STICKY, &fdb->flags))) { |
Tobias Waldekranz | 6eb38bf | 2021-06-29 17:06:45 +0300 | [diff] [blame] | 731 | br_switchdev_fdb_notify(br, fdb, RTM_DELNEIGH); |
Vladimir Oltean | 3e19ae7 | 2021-06-29 17:06:44 +0300 | [diff] [blame] | 732 | WRITE_ONCE(fdb->dst, source); |
Jon Maxwell | c65c7a3 | 2014-05-29 17:27:16 +1000 | [diff] [blame] | 733 | fdb_modified = true; |
Arkadi Sharshevsky | 58073b3 | 2017-04-28 22:39:07 +0300 | [diff] [blame] | 734 | /* Take over HW learned entry */ |
Nikolay Aleksandrov | 58ec1ea | 2019-11-01 14:46:39 +0200 | [diff] [blame] | 735 | if (unlikely(test_bit(BR_FDB_ADDED_BY_EXT_LEARN, |
| 736 | &fdb->flags))) |
| 737 | clear_bit(BR_FDB_ADDED_BY_EXT_LEARN, |
| 738 | &fdb->flags); |
Jon Maxwell | c65c7a3 | 2014-05-29 17:27:16 +1000 | [diff] [blame] | 739 | } |
Nikolay Aleksandrov | 31cbc39b | 2020-06-23 23:47:17 +0300 | [diff] [blame] | 740 | |
Nikolay Aleksandrov | be0c567 | 2019-11-01 14:46:37 +0200 | [diff] [blame] | 741 | if (unlikely(test_bit(BR_FDB_ADDED_BY_USER, &flags))) |
Nikolay Aleksandrov | ac3ca6a | 2019-10-29 13:45:56 +0200 | [diff] [blame] | 742 | set_bit(BR_FDB_ADDED_BY_USER, &fdb->flags); |
Roopa Prabhu | e3cfddd | 2017-08-30 22:18:13 -0700 | [diff] [blame] | 743 | if (unlikely(fdb_modified)) { |
Nikolay Aleksandrov | be0c567 | 2019-11-01 14:46:37 +0200 | [diff] [blame] | 744 | trace_br_fdb_update(br, source, addr, vid, flags); |
Petr Machata | 161d82d | 2018-05-03 14:43:53 +0200 | [diff] [blame] | 745 | fdb_notify(br, fdb, RTM_NEWNEIGH, true); |
Roopa Prabhu | e3cfddd | 2017-08-30 22:18:13 -0700 | [diff] [blame] | 746 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | } |
| 748 | } else { |
Stephen Hemminger | f8ae737 | 2006-03-20 22:58:36 -0800 | [diff] [blame] | 749 | spin_lock(&br->hash_lock); |
Nikolay Aleksandrov | be0c567 | 2019-11-01 14:46:37 +0200 | [diff] [blame] | 750 | fdb = fdb_create(br, source, addr, vid, flags); |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 751 | if (fdb) { |
Nikolay Aleksandrov | be0c567 | 2019-11-01 14:46:37 +0200 | [diff] [blame] | 752 | trace_br_fdb_update(br, source, addr, vid, flags); |
Petr Machata | 161d82d | 2018-05-03 14:43:53 +0200 | [diff] [blame] | 753 | fdb_notify(br, fdb, RTM_NEWNEIGH, true); |
stephen hemminger | f58ee4e | 2011-12-06 13:02:24 +0000 | [diff] [blame] | 754 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | /* else we lose race and someone else inserts |
| 756 | * it first, don't bother updating |
| 757 | */ |
Stephen Hemminger | f8ae737 | 2006-03-20 22:58:36 -0800 | [diff] [blame] | 758 | spin_unlock(&br->hash_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | } |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 761 | |
Vladimir Oltean | b4454bc | 2021-07-28 21:27:47 +0300 | [diff] [blame] | 762 | static int br_fdb_replay_one(struct net_bridge *br, struct notifier_block *nb, |
Vladimir Oltean | bdf123b | 2021-06-27 14:54:26 +0300 | [diff] [blame] | 763 | const struct net_bridge_fdb_entry *fdb, |
Vladimir Oltean | b4454bc | 2021-07-28 21:27:47 +0300 | [diff] [blame] | 764 | unsigned long action, const void *ctx) |
Vladimir Oltean | 04846f9 | 2021-03-23 01:51:45 +0200 | [diff] [blame] | 765 | { |
Vladimir Oltean | b4454bc | 2021-07-28 21:27:47 +0300 | [diff] [blame] | 766 | const struct net_bridge_port *p = READ_ONCE(fdb->dst); |
Vladimir Oltean | 04846f9 | 2021-03-23 01:51:45 +0200 | [diff] [blame] | 767 | struct switchdev_notifier_fdb_info item; |
| 768 | int err; |
| 769 | |
| 770 | item.addr = fdb->key.addr.addr; |
| 771 | item.vid = fdb->key.vlan_id; |
| 772 | item.added_by_user = test_bit(BR_FDB_ADDED_BY_USER, &fdb->flags); |
| 773 | item.offloaded = test_bit(BR_FDB_OFFLOADED, &fdb->flags); |
Vladimir Oltean | e887b2d | 2021-06-27 14:54:22 +0300 | [diff] [blame] | 774 | item.is_local = test_bit(BR_FDB_LOCAL, &fdb->flags); |
Vladimir Oltean | 2e19bb3 | 2021-08-02 14:36:33 +0300 | [diff] [blame] | 775 | item.info.dev = (!p || item.is_local) ? br->dev : p->dev; |
Vladimir Oltean | 0d2cfbd | 2021-06-27 14:54:25 +0300 | [diff] [blame] | 776 | item.info.ctx = ctx; |
Vladimir Oltean | 04846f9 | 2021-03-23 01:51:45 +0200 | [diff] [blame] | 777 | |
Vladimir Oltean | 7e8c185 | 2021-06-27 14:54:27 +0300 | [diff] [blame] | 778 | err = nb->notifier_call(nb, action, &item); |
Vladimir Oltean | 04846f9 | 2021-03-23 01:51:45 +0200 | [diff] [blame] | 779 | return notifier_to_errno(err); |
| 780 | } |
| 781 | |
Vladimir Oltean | b4454bc | 2021-07-28 21:27:47 +0300 | [diff] [blame] | 782 | int br_fdb_replay(const struct net_device *br_dev, const void *ctx, bool adding, |
| 783 | struct notifier_block *nb) |
Vladimir Oltean | 04846f9 | 2021-03-23 01:51:45 +0200 | [diff] [blame] | 784 | { |
| 785 | struct net_bridge_fdb_entry *fdb; |
| 786 | struct net_bridge *br; |
Vladimir Oltean | 7e8c185 | 2021-06-27 14:54:27 +0300 | [diff] [blame] | 787 | unsigned long action; |
Vladimir Oltean | 04846f9 | 2021-03-23 01:51:45 +0200 | [diff] [blame] | 788 | int err = 0; |
| 789 | |
Vladimir Oltean | 7105b50 | 2021-07-21 19:24:02 +0300 | [diff] [blame] | 790 | if (!nb) |
| 791 | return 0; |
| 792 | |
Vladimir Oltean | f851a72 | 2021-06-29 17:06:46 +0300 | [diff] [blame] | 793 | if (!netif_is_bridge_master(br_dev)) |
| 794 | return -EINVAL; |
| 795 | |
Vladimir Oltean | 04846f9 | 2021-03-23 01:51:45 +0200 | [diff] [blame] | 796 | br = netdev_priv(br_dev); |
| 797 | |
Vladimir Oltean | 7e8c185 | 2021-06-27 14:54:27 +0300 | [diff] [blame] | 798 | if (adding) |
| 799 | action = SWITCHDEV_FDB_ADD_TO_DEVICE; |
| 800 | else |
| 801 | action = SWITCHDEV_FDB_DEL_TO_DEVICE; |
| 802 | |
Vladimir Oltean | 04846f9 | 2021-03-23 01:51:45 +0200 | [diff] [blame] | 803 | rcu_read_lock(); |
| 804 | |
| 805 | hlist_for_each_entry_rcu(fdb, &br->fdb_list, fdb_node) { |
Vladimir Oltean | b4454bc | 2021-07-28 21:27:47 +0300 | [diff] [blame] | 806 | err = br_fdb_replay_one(br, nb, fdb, action, ctx); |
Vladimir Oltean | 04846f9 | 2021-03-23 01:51:45 +0200 | [diff] [blame] | 807 | if (err) |
| 808 | break; |
| 809 | } |
| 810 | |
| 811 | rcu_read_unlock(); |
| 812 | |
| 813 | return err; |
| 814 | } |
Vladimir Oltean | 04846f9 | 2021-03-23 01:51:45 +0200 | [diff] [blame] | 815 | |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 816 | /* Dump information about entries, in response to GETNEIGH */ |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 817 | int br_fdb_dump(struct sk_buff *skb, |
| 818 | struct netlink_callback *cb, |
| 819 | struct net_device *dev, |
Jamal Hadi Salim | 5d5eacb | 2014-07-10 07:01:58 -0400 | [diff] [blame] | 820 | struct net_device *filter_dev, |
Roopa Prabhu | d297653 | 2016-08-30 21:56:45 -0700 | [diff] [blame] | 821 | int *idx) |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 822 | { |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 823 | struct net_bridge *br = netdev_priv(dev); |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 824 | struct net_bridge_fdb_entry *f; |
Roopa Prabhu | d297653 | 2016-08-30 21:56:45 -0700 | [diff] [blame] | 825 | int err = 0; |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 826 | |
Kyungrok Chung | 254ec03 | 2021-10-16 20:21:36 +0900 | [diff] [blame] | 827 | if (!netif_is_bridge_master(dev)) |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 828 | return err; |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 829 | |
Roopa Prabhu | d297653 | 2016-08-30 21:56:45 -0700 | [diff] [blame] | 830 | if (!filter_dev) { |
| 831 | err = ndo_dflt_fdb_dump(skb, cb, dev, NULL, idx); |
| 832 | if (err < 0) |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 833 | return err; |
Roopa Prabhu | d297653 | 2016-08-30 21:56:45 -0700 | [diff] [blame] | 834 | } |
Hubert Sokolowski | 6cb6974 | 2015-01-05 17:29:21 +0000 | [diff] [blame] | 835 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 836 | rcu_read_lock(); |
| 837 | hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) { |
| 838 | if (*idx < cb->args[2]) |
| 839 | goto skip; |
| 840 | if (filter_dev && (!f->dst || f->dst->dev != filter_dev)) { |
| 841 | if (filter_dev != dev) |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 842 | goto skip; |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 843 | /* !f->dst is a special case for bridge |
| 844 | * It means the MAC belongs to the bridge |
| 845 | * Therefore need a little more filtering |
| 846 | * we only want to dump the !f->dst case |
| 847 | */ |
| 848 | if (f->dst) |
Hubert Sokolowski | 6cb6974 | 2015-01-05 17:29:21 +0000 | [diff] [blame] | 849 | goto skip; |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 850 | } |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 851 | if (!filter_dev && f->dst) |
| 852 | goto skip; |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 853 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 854 | err = fdb_fill_info(skb, br, f, |
| 855 | NETLINK_CB(cb->skb).portid, |
| 856 | cb->nlh->nlmsg_seq, |
| 857 | RTM_NEWNEIGH, |
| 858 | NLM_F_MULTI); |
| 859 | if (err < 0) |
| 860 | break; |
| 861 | skip: |
| 862 | *idx += 1; |
| 863 | } |
| 864 | rcu_read_unlock(); |
| 865 | |
Roopa Prabhu | d297653 | 2016-08-30 21:56:45 -0700 | [diff] [blame] | 866 | return err; |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 867 | } |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 868 | |
Roopa Prabhu | 4767456 | 2018-12-15 22:35:09 -0800 | [diff] [blame] | 869 | int br_fdb_get(struct sk_buff *skb, |
| 870 | struct nlattr *tb[], |
| 871 | struct net_device *dev, |
| 872 | const unsigned char *addr, |
| 873 | u16 vid, u32 portid, u32 seq, |
| 874 | struct netlink_ext_ack *extack) |
| 875 | { |
| 876 | struct net_bridge *br = netdev_priv(dev); |
| 877 | struct net_bridge_fdb_entry *f; |
| 878 | int err = 0; |
| 879 | |
| 880 | rcu_read_lock(); |
| 881 | f = br_fdb_find_rcu(br, addr, vid); |
| 882 | if (!f) { |
| 883 | NL_SET_ERR_MSG(extack, "Fdb entry not found"); |
| 884 | err = -ENOENT; |
| 885 | goto errout; |
| 886 | } |
| 887 | |
| 888 | err = fdb_fill_info(skb, br, f, portid, seq, |
| 889 | RTM_NEWNEIGH, 0); |
| 890 | errout: |
| 891 | rcu_read_unlock(); |
| 892 | return err; |
| 893 | } |
| 894 | |
Nikolay Aleksandrov | 31cbc39b | 2020-06-23 23:47:17 +0300 | [diff] [blame] | 895 | /* returns true if the fdb is modified */ |
| 896 | static bool fdb_handle_notify(struct net_bridge_fdb_entry *fdb, u8 notify) |
| 897 | { |
| 898 | bool modified = false; |
| 899 | |
| 900 | /* allow to mark an entry as inactive, usually done on creation */ |
| 901 | if ((notify & FDB_NOTIFY_INACTIVE_BIT) && |
| 902 | !test_and_set_bit(BR_FDB_NOTIFY_INACTIVE, &fdb->flags)) |
| 903 | modified = true; |
| 904 | |
| 905 | if ((notify & FDB_NOTIFY_BIT) && |
| 906 | !test_and_set_bit(BR_FDB_NOTIFY, &fdb->flags)) { |
| 907 | /* enabled activity tracking */ |
| 908 | modified = true; |
| 909 | } else if (!(notify & FDB_NOTIFY_BIT) && |
| 910 | test_and_clear_bit(BR_FDB_NOTIFY, &fdb->flags)) { |
| 911 | /* disabled activity tracking, clear notify state */ |
| 912 | clear_bit(BR_FDB_NOTIFY_INACTIVE, &fdb->flags); |
| 913 | modified = true; |
| 914 | } |
| 915 | |
| 916 | return modified; |
| 917 | } |
| 918 | |
stephen hemminger | 292d139 | 2011-11-09 18:30:08 +0000 | [diff] [blame] | 919 | /* Update (create or replace) forwarding database entry */ |
Toshiaki Makita | 7bb90c3 | 2016-08-04 11:11:19 +0900 | [diff] [blame] | 920 | static int fdb_add_entry(struct net_bridge *br, struct net_bridge_port *source, |
Nikolay Aleksandrov | 31cbc39b | 2020-06-23 23:47:17 +0300 | [diff] [blame] | 921 | const u8 *addr, struct ndmsg *ndm, u16 flags, u16 vid, |
| 922 | struct nlattr *nfea_tb[]) |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 923 | { |
Nikolay Aleksandrov | 0592ff8 | 2020-06-23 23:47:15 +0300 | [diff] [blame] | 924 | bool is_sticky = !!(ndm->ndm_flags & NTF_STICKY); |
Nikolay Aleksandrov | b5f1d9e | 2020-06-23 23:47:18 +0300 | [diff] [blame] | 925 | bool refresh = !nfea_tb[NFEA_DONT_REFRESH]; |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 926 | struct net_bridge_fdb_entry *fdb; |
Nikolay Aleksandrov | 0592ff8 | 2020-06-23 23:47:15 +0300 | [diff] [blame] | 927 | u16 state = ndm->ndm_state; |
roopa | b0a397f | 2013-04-22 12:56:49 +0000 | [diff] [blame] | 928 | bool modified = false; |
Nikolay Aleksandrov | 31cbc39b | 2020-06-23 23:47:17 +0300 | [diff] [blame] | 929 | u8 notify = 0; |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 930 | |
Wilson Kok | eb8d7ba | 2015-05-25 06:39:31 -0700 | [diff] [blame] | 931 | /* If the port cannot learn allow only local and static entries */ |
Toshiaki Makita | 7bb90c3 | 2016-08-04 11:11:19 +0900 | [diff] [blame] | 932 | if (source && !(state & NUD_PERMANENT) && !(state & NUD_NOARP) && |
Wilson Kok | eb8d7ba | 2015-05-25 06:39:31 -0700 | [diff] [blame] | 933 | !(source->state == BR_STATE_LEARNING || |
| 934 | source->state == BR_STATE_FORWARDING)) |
| 935 | return -EPERM; |
| 936 | |
Toshiaki Makita | 7bb90c3 | 2016-08-04 11:11:19 +0900 | [diff] [blame] | 937 | if (!source && !(state & NUD_PERMANENT)) { |
| 938 | pr_info("bridge: RTM_NEWNEIGH %s without NUD_PERMANENT\n", |
| 939 | br->dev->name); |
| 940 | return -EINVAL; |
| 941 | } |
| 942 | |
Nikolay Aleksandrov | 435f2e7 | 2018-09-11 09:39:53 +0300 | [diff] [blame] | 943 | if (is_sticky && (state & NUD_PERMANENT)) |
| 944 | return -EINVAL; |
| 945 | |
Nikolay Aleksandrov | 31cbc39b | 2020-06-23 23:47:17 +0300 | [diff] [blame] | 946 | if (nfea_tb[NFEA_ACTIVITY_NOTIFY]) { |
| 947 | notify = nla_get_u8(nfea_tb[NFEA_ACTIVITY_NOTIFY]); |
| 948 | if ((notify & ~BR_FDB_NOTIFY_SETTABLE_BITS) || |
| 949 | (notify & BR_FDB_NOTIFY_SETTABLE_BITS) == FDB_NOTIFY_INACTIVE_BIT) |
| 950 | return -EINVAL; |
| 951 | } |
| 952 | |
Nikolay Aleksandrov | bfd0aea | 2017-02-13 14:59:09 +0100 | [diff] [blame] | 953 | fdb = br_fdb_find(br, addr, vid); |
stephen hemminger | 64af1ba | 2011-09-30 14:37:27 +0000 | [diff] [blame] | 954 | if (fdb == NULL) { |
| 955 | if (!(flags & NLM_F_CREATE)) |
| 956 | return -ENOENT; |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 957 | |
Nikolay Aleksandrov | 3fb01a3 | 2019-10-29 13:45:59 +0200 | [diff] [blame] | 958 | fdb = fdb_create(br, source, addr, vid, 0); |
stephen hemminger | 64af1ba | 2011-09-30 14:37:27 +0000 | [diff] [blame] | 959 | if (!fdb) |
| 960 | return -ENOMEM; |
roopa | b0a397f | 2013-04-22 12:56:49 +0000 | [diff] [blame] | 961 | |
| 962 | modified = true; |
stephen hemminger | 64af1ba | 2011-09-30 14:37:27 +0000 | [diff] [blame] | 963 | } else { |
| 964 | if (flags & NLM_F_EXCL) |
| 965 | return -EEXIST; |
roopa | b0a397f | 2013-04-22 12:56:49 +0000 | [diff] [blame] | 966 | |
Vladimir Oltean | 3e19ae7 | 2021-06-29 17:06:44 +0300 | [diff] [blame] | 967 | if (READ_ONCE(fdb->dst) != source) { |
| 968 | WRITE_ONCE(fdb->dst, source); |
roopa | b0a397f | 2013-04-22 12:56:49 +0000 | [diff] [blame] | 969 | modified = true; |
| 970 | } |
stephen hemminger | 64af1ba | 2011-09-30 14:37:27 +0000 | [diff] [blame] | 971 | } |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 972 | |
Roopa Prabhu | 3741873 | 2015-10-08 10:38:52 -0700 | [diff] [blame] | 973 | if (fdb_to_nud(br, fdb) != state) { |
Vlad Yasevich | 145beee | 2014-05-16 09:59:19 -0400 | [diff] [blame] | 974 | if (state & NUD_PERMANENT) { |
Nikolay Aleksandrov | 6869c3b | 2019-10-29 13:45:53 +0200 | [diff] [blame] | 975 | set_bit(BR_FDB_LOCAL, &fdb->flags); |
Nikolay Aleksandrov | 29e63ff | 2019-10-29 13:45:54 +0200 | [diff] [blame] | 976 | if (!test_and_set_bit(BR_FDB_STATIC, &fdb->flags)) |
Jiri Pirko | 020ec6b | 2014-11-28 14:34:12 +0100 | [diff] [blame] | 977 | fdb_add_hw_addr(br, addr); |
Vlad Yasevich | 145beee | 2014-05-16 09:59:19 -0400 | [diff] [blame] | 978 | } else if (state & NUD_NOARP) { |
Nikolay Aleksandrov | 6869c3b | 2019-10-29 13:45:53 +0200 | [diff] [blame] | 979 | clear_bit(BR_FDB_LOCAL, &fdb->flags); |
Nikolay Aleksandrov | 29e63ff | 2019-10-29 13:45:54 +0200 | [diff] [blame] | 980 | if (!test_and_set_bit(BR_FDB_STATIC, &fdb->flags)) |
Jiri Pirko | 020ec6b | 2014-11-28 14:34:12 +0100 | [diff] [blame] | 981 | fdb_add_hw_addr(br, addr); |
Vlad Yasevich | 145beee | 2014-05-16 09:59:19 -0400 | [diff] [blame] | 982 | } else { |
Nikolay Aleksandrov | 6869c3b | 2019-10-29 13:45:53 +0200 | [diff] [blame] | 983 | clear_bit(BR_FDB_LOCAL, &fdb->flags); |
Nikolay Aleksandrov | 29e63ff | 2019-10-29 13:45:54 +0200 | [diff] [blame] | 984 | if (test_and_clear_bit(BR_FDB_STATIC, &fdb->flags)) |
Jiri Pirko | 020ec6b | 2014-11-28 14:34:12 +0100 | [diff] [blame] | 985 | fdb_del_hw_addr(br, addr); |
Vlad Yasevich | 145beee | 2014-05-16 09:59:19 -0400 | [diff] [blame] | 986 | } |
stephen hemminger | 292d139 | 2011-11-09 18:30:08 +0000 | [diff] [blame] | 987 | |
roopa | b0a397f | 2013-04-22 12:56:49 +0000 | [diff] [blame] | 988 | modified = true; |
| 989 | } |
Nikolay Aleksandrov | 435f2e7 | 2018-09-11 09:39:53 +0300 | [diff] [blame] | 990 | |
Nikolay Aleksandrov | e0458d9 | 2019-10-29 13:45:55 +0200 | [diff] [blame] | 991 | if (is_sticky != test_bit(BR_FDB_STICKY, &fdb->flags)) { |
| 992 | change_bit(BR_FDB_STICKY, &fdb->flags); |
Nikolay Aleksandrov | 435f2e7 | 2018-09-11 09:39:53 +0300 | [diff] [blame] | 993 | modified = true; |
| 994 | } |
| 995 | |
Nikolay Aleksandrov | 31cbc39b | 2020-06-23 23:47:17 +0300 | [diff] [blame] | 996 | if (fdb_handle_notify(fdb, notify)) |
| 997 | modified = true; |
| 998 | |
Nikolay Aleksandrov | ac3ca6a | 2019-10-29 13:45:56 +0200 | [diff] [blame] | 999 | set_bit(BR_FDB_ADDED_BY_USER, &fdb->flags); |
roopa | b0a397f | 2013-04-22 12:56:49 +0000 | [diff] [blame] | 1000 | |
| 1001 | fdb->used = jiffies; |
| 1002 | if (modified) { |
Nikolay Aleksandrov | b5f1d9e | 2020-06-23 23:47:18 +0300 | [diff] [blame] | 1003 | if (refresh) |
| 1004 | fdb->updated = jiffies; |
Petr Machata | 161d82d | 2018-05-03 14:43:53 +0200 | [diff] [blame] | 1005 | fdb_notify(br, fdb, RTM_NEWNEIGH, true); |
stephen hemminger | 292d139 | 2011-11-09 18:30:08 +0000 | [diff] [blame] | 1006 | } |
| 1007 | |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 1008 | return 0; |
| 1009 | } |
| 1010 | |
Toshiaki Makita | 7bb90c3 | 2016-08-04 11:11:19 +0900 | [diff] [blame] | 1011 | static int __br_fdb_add(struct ndmsg *ndm, struct net_bridge *br, |
| 1012 | struct net_bridge_port *p, const unsigned char *addr, |
Vladimir Oltean | 0541a62 | 2021-08-02 02:17:30 +0300 | [diff] [blame] | 1013 | u16 nlh_flags, u16 vid, struct nlattr *nfea_tb[], |
| 1014 | struct netlink_ext_ack *extack) |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 1015 | { |
| 1016 | int err = 0; |
| 1017 | |
| 1018 | if (ndm->ndm_flags & NTF_USE) { |
Toshiaki Makita | 7bb90c3 | 2016-08-04 11:11:19 +0900 | [diff] [blame] | 1019 | if (!p) { |
| 1020 | pr_info("bridge: RTM_NEWNEIGH %s with NTF_USE is not supported\n", |
| 1021 | br->dev->name); |
| 1022 | return -EINVAL; |
| 1023 | } |
Nikolay Aleksandrov | 5d1fcaf | 2019-11-04 11:36:51 +0200 | [diff] [blame] | 1024 | if (!nbp_state_should_learn(p)) |
| 1025 | return 0; |
| 1026 | |
Nikolay Aleksandrov | c4c832f | 2015-06-06 06:49:00 -0700 | [diff] [blame] | 1027 | local_bh_disable(); |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 1028 | rcu_read_lock(); |
Nikolay Aleksandrov | be0c567 | 2019-11-01 14:46:37 +0200 | [diff] [blame] | 1029 | br_fdb_update(br, p, addr, vid, BIT(BR_FDB_ADDED_BY_USER)); |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 1030 | rcu_read_unlock(); |
Nikolay Aleksandrov | c4c832f | 2015-06-06 06:49:00 -0700 | [diff] [blame] | 1031 | local_bh_enable(); |
Nikolay Aleksandrov | eb100e0 | 2017-03-23 12:27:13 +0200 | [diff] [blame] | 1032 | } else if (ndm->ndm_flags & NTF_EXT_LEARNED) { |
Vladimir Oltean | 0541a62 | 2021-08-02 02:17:30 +0300 | [diff] [blame] | 1033 | if (!p && !(ndm->ndm_state & NUD_PERMANENT)) { |
| 1034 | NL_SET_ERR_MSG_MOD(extack, |
| 1035 | "FDB entry towards bridge must be permanent"); |
| 1036 | return -EINVAL; |
| 1037 | } |
Nikolay Aleksandrov | 45a6878 | 2021-08-10 14:00:10 +0300 | [diff] [blame] | 1038 | err = br_fdb_external_learn_add(br, p, addr, vid, true); |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 1039 | } else { |
Toshiaki Makita | 7bb90c3 | 2016-08-04 11:11:19 +0900 | [diff] [blame] | 1040 | spin_lock_bh(&br->hash_lock); |
Nikolay Aleksandrov | 31cbc39b | 2020-06-23 23:47:17 +0300 | [diff] [blame] | 1041 | err = fdb_add_entry(br, p, addr, ndm, nlh_flags, vid, nfea_tb); |
Toshiaki Makita | 7bb90c3 | 2016-08-04 11:11:19 +0900 | [diff] [blame] | 1042 | spin_unlock_bh(&br->hash_lock); |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 1043 | } |
| 1044 | |
| 1045 | return err; |
| 1046 | } |
| 1047 | |
Nikolay Aleksandrov | 31cbc39b | 2020-06-23 23:47:17 +0300 | [diff] [blame] | 1048 | static const struct nla_policy br_nda_fdb_pol[NFEA_MAX + 1] = { |
| 1049 | [NFEA_ACTIVITY_NOTIFY] = { .type = NLA_U8 }, |
Nikolay Aleksandrov | b5f1d9e | 2020-06-23 23:47:18 +0300 | [diff] [blame] | 1050 | [NFEA_DONT_REFRESH] = { .type = NLA_FLAG }, |
Nikolay Aleksandrov | 31cbc39b | 2020-06-23 23:47:17 +0300 | [diff] [blame] | 1051 | }; |
| 1052 | |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 1053 | /* Add new permanent fdb entry with RTM_NEWNEIGH */ |
stephen hemminger | edc7d57 | 2012-10-01 12:32:33 +0000 | [diff] [blame] | 1054 | int br_fdb_add(struct ndmsg *ndm, struct nlattr *tb[], |
| 1055 | struct net_device *dev, |
Petr Machata | 87b0984 | 2019-01-16 23:06:50 +0000 | [diff] [blame] | 1056 | const unsigned char *addr, u16 vid, u16 nlh_flags, |
| 1057 | struct netlink_ext_ack *extack) |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 1058 | { |
Nikolay Aleksandrov | 31cbc39b | 2020-06-23 23:47:17 +0300 | [diff] [blame] | 1059 | struct nlattr *nfea_tb[NFEA_MAX + 1], *attr; |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 1060 | struct net_bridge_vlan_group *vg; |
Roopa Prabhu | 3741873 | 2015-10-08 10:38:52 -0700 | [diff] [blame] | 1061 | struct net_bridge_port *p = NULL; |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 1062 | struct net_bridge_vlan *v; |
Roopa Prabhu | 3741873 | 2015-10-08 10:38:52 -0700 | [diff] [blame] | 1063 | struct net_bridge *br = NULL; |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 1064 | int err = 0; |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 1065 | |
Roopa Prabhu | b74fd30 | 2017-08-29 13:16:57 -0700 | [diff] [blame] | 1066 | trace_br_fdb_add(ndm, dev, addr, vid, nlh_flags); |
| 1067 | |
stephen hemminger | 292d139 | 2011-11-09 18:30:08 +0000 | [diff] [blame] | 1068 | if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE))) { |
| 1069 | pr_info("bridge: RTM_NEWNEIGH with invalid state %#x\n", ndm->ndm_state); |
| 1070 | return -EINVAL; |
| 1071 | } |
| 1072 | |
Stephen Hemminger | 537f7f8 | 2013-06-25 09:34:36 -0700 | [diff] [blame] | 1073 | if (is_zero_ether_addr(addr)) { |
| 1074 | pr_info("bridge: RTM_NEWNEIGH with invalid ether address\n"); |
| 1075 | return -EINVAL; |
| 1076 | } |
| 1077 | |
Kyungrok Chung | 254ec03 | 2021-10-16 20:21:36 +0900 | [diff] [blame] | 1078 | if (netif_is_bridge_master(dev)) { |
Roopa Prabhu | 3741873 | 2015-10-08 10:38:52 -0700 | [diff] [blame] | 1079 | br = netdev_priv(dev); |
| 1080 | vg = br_vlan_group(br); |
| 1081 | } else { |
| 1082 | p = br_port_get_rtnl(dev); |
| 1083 | if (!p) { |
| 1084 | pr_info("bridge: RTM_NEWNEIGH %s not a bridge port\n", |
| 1085 | dev->name); |
| 1086 | return -EINVAL; |
| 1087 | } |
Toshiaki Makita | 7bb90c3 | 2016-08-04 11:11:19 +0900 | [diff] [blame] | 1088 | br = p->br; |
Roopa Prabhu | 3741873 | 2015-10-08 10:38:52 -0700 | [diff] [blame] | 1089 | vg = nbp_vlan_group(p); |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 1090 | } |
| 1091 | |
Nikolay Aleksandrov | 31cbc39b | 2020-06-23 23:47:17 +0300 | [diff] [blame] | 1092 | if (tb[NDA_FDB_EXT_ATTRS]) { |
| 1093 | attr = tb[NDA_FDB_EXT_ATTRS]; |
| 1094 | err = nla_parse_nested(nfea_tb, NFEA_MAX, attr, |
| 1095 | br_nda_fdb_pol, extack); |
| 1096 | if (err) |
| 1097 | return err; |
| 1098 | } else { |
| 1099 | memset(nfea_tb, 0, sizeof(struct nlattr *) * (NFEA_MAX + 1)); |
| 1100 | } |
| 1101 | |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 1102 | if (vid) { |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 1103 | v = br_vlan_find(vg, vid); |
Roopa Prabhu | 3741873 | 2015-10-08 10:38:52 -0700 | [diff] [blame] | 1104 | if (!v || !br_vlan_should_use(v)) { |
| 1105 | pr_info("bridge: RTM_NEWNEIGH with unconfigured vlan %d on %s\n", vid, dev->name); |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 1106 | return -EINVAL; |
| 1107 | } |
| 1108 | |
| 1109 | /* VID was specified, so use it. */ |
Vladimir Oltean | 0541a62 | 2021-08-02 02:17:30 +0300 | [diff] [blame] | 1110 | err = __br_fdb_add(ndm, br, p, addr, nlh_flags, vid, nfea_tb, |
| 1111 | extack); |
stephen hemminger | 292d139 | 2011-11-09 18:30:08 +0000 | [diff] [blame] | 1112 | } else { |
Vladimir Oltean | 0541a62 | 2021-08-02 02:17:30 +0300 | [diff] [blame] | 1113 | err = __br_fdb_add(ndm, br, p, addr, nlh_flags, 0, nfea_tb, |
| 1114 | extack); |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 1115 | if (err || !vg || !vg->num_vlans) |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 1116 | goto out; |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 1117 | |
| 1118 | /* We have vlans configured on this port and user didn't |
| 1119 | * specify a VLAN. To be nice, add/update entry for every |
| 1120 | * vlan on this port. |
| 1121 | */ |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 1122 | list_for_each_entry(v, &vg->vlan_list, vlist) { |
Roopa Prabhu | 3741873 | 2015-10-08 10:38:52 -0700 | [diff] [blame] | 1123 | if (!br_vlan_should_use(v)) |
| 1124 | continue; |
Nikolay Aleksandrov | 31cbc39b | 2020-06-23 23:47:17 +0300 | [diff] [blame] | 1125 | err = __br_fdb_add(ndm, br, p, addr, nlh_flags, v->vid, |
Vladimir Oltean | 0541a62 | 2021-08-02 02:17:30 +0300 | [diff] [blame] | 1126 | nfea_tb, extack); |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 1127 | if (err) |
| 1128 | goto out; |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 1129 | } |
stephen hemminger | 292d139 | 2011-11-09 18:30:08 +0000 | [diff] [blame] | 1130 | } |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 1131 | |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 1132 | out: |
| 1133 | return err; |
| 1134 | } |
| 1135 | |
Nikolay Aleksandrov | 5019ab5 | 2017-02-13 14:59:11 +0100 | [diff] [blame] | 1136 | static int fdb_delete_by_addr_and_port(struct net_bridge *br, |
| 1137 | const struct net_bridge_port *p, |
Nikolay Aleksandrov | 8c86f96 | 2015-06-09 03:34:13 -0700 | [diff] [blame] | 1138 | const u8 *addr, u16 vlan) |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 1139 | { |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 1140 | struct net_bridge_fdb_entry *fdb; |
| 1141 | |
Nikolay Aleksandrov | bfd0aea | 2017-02-13 14:59:09 +0100 | [diff] [blame] | 1142 | fdb = br_fdb_find(br, addr, vlan); |
Vladimir Oltean | 3e19ae7 | 2021-06-29 17:06:44 +0300 | [diff] [blame] | 1143 | if (!fdb || READ_ONCE(fdb->dst) != p) |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 1144 | return -ENOENT; |
| 1145 | |
Petr Machata | 161d82d | 2018-05-03 14:43:53 +0200 | [diff] [blame] | 1146 | fdb_delete(br, fdb, true); |
Nikolay Aleksandrov | 5019ab5 | 2017-02-13 14:59:11 +0100 | [diff] [blame] | 1147 | |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 1148 | return 0; |
| 1149 | } |
| 1150 | |
Nikolay Aleksandrov | 5019ab5 | 2017-02-13 14:59:11 +0100 | [diff] [blame] | 1151 | static int __br_fdb_delete(struct net_bridge *br, |
| 1152 | const struct net_bridge_port *p, |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 1153 | const unsigned char *addr, u16 vid) |
| 1154 | { |
| 1155 | int err; |
| 1156 | |
Nikolay Aleksandrov | 5019ab5 | 2017-02-13 14:59:11 +0100 | [diff] [blame] | 1157 | spin_lock_bh(&br->hash_lock); |
| 1158 | err = fdb_delete_by_addr_and_port(br, p, addr, vid); |
| 1159 | spin_unlock_bh(&br->hash_lock); |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 1160 | |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 1161 | return err; |
| 1162 | } |
| 1163 | |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 1164 | /* Remove neighbor entry with RTM_DELNEIGH */ |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 1165 | int br_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[], |
| 1166 | struct net_device *dev, |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 1167 | const unsigned char *addr, u16 vid) |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 1168 | { |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 1169 | struct net_bridge_vlan_group *vg; |
Roopa Prabhu | 3741873 | 2015-10-08 10:38:52 -0700 | [diff] [blame] | 1170 | struct net_bridge_port *p = NULL; |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 1171 | struct net_bridge_vlan *v; |
Nikolay Aleksandrov | 5019ab5 | 2017-02-13 14:59:11 +0100 | [diff] [blame] | 1172 | struct net_bridge *br; |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 1173 | int err; |
| 1174 | |
Kyungrok Chung | 254ec03 | 2021-10-16 20:21:36 +0900 | [diff] [blame] | 1175 | if (netif_is_bridge_master(dev)) { |
Roopa Prabhu | 3741873 | 2015-10-08 10:38:52 -0700 | [diff] [blame] | 1176 | br = netdev_priv(dev); |
| 1177 | vg = br_vlan_group(br); |
| 1178 | } else { |
| 1179 | p = br_port_get_rtnl(dev); |
| 1180 | if (!p) { |
| 1181 | pr_info("bridge: RTM_DELNEIGH %s not a bridge port\n", |
| 1182 | dev->name); |
| 1183 | return -EINVAL; |
| 1184 | } |
| 1185 | vg = nbp_vlan_group(p); |
Nikolay Aleksandrov | 5019ab5 | 2017-02-13 14:59:11 +0100 | [diff] [blame] | 1186 | br = p->br; |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 1187 | } |
| 1188 | |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 1189 | if (vid) { |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 1190 | v = br_vlan_find(vg, vid); |
| 1191 | if (!v) { |
Roopa Prabhu | 3741873 | 2015-10-08 10:38:52 -0700 | [diff] [blame] | 1192 | pr_info("bridge: RTM_DELNEIGH with unconfigured vlan %d on %s\n", vid, dev->name); |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 1193 | return -EINVAL; |
| 1194 | } |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 1195 | |
Nikolay Aleksandrov | 5019ab5 | 2017-02-13 14:59:11 +0100 | [diff] [blame] | 1196 | err = __br_fdb_delete(br, p, addr, vid); |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 1197 | } else { |
Toshiaki Makita | 25d3b49 | 2015-02-09 20:16:17 +0900 | [diff] [blame] | 1198 | err = -ENOENT; |
Nikolay Aleksandrov | 5019ab5 | 2017-02-13 14:59:11 +0100 | [diff] [blame] | 1199 | err &= __br_fdb_delete(br, p, addr, 0); |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 1200 | if (!vg || !vg->num_vlans) |
Nikolay Aleksandrov | 5019ab5 | 2017-02-13 14:59:11 +0100 | [diff] [blame] | 1201 | return err; |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 1202 | |
Roopa Prabhu | 3741873 | 2015-10-08 10:38:52 -0700 | [diff] [blame] | 1203 | list_for_each_entry(v, &vg->vlan_list, vlist) { |
| 1204 | if (!br_vlan_should_use(v)) |
| 1205 | continue; |
Nikolay Aleksandrov | 5019ab5 | 2017-02-13 14:59:11 +0100 | [diff] [blame] | 1206 | err &= __br_fdb_delete(br, p, addr, v->vid); |
Roopa Prabhu | 3741873 | 2015-10-08 10:38:52 -0700 | [diff] [blame] | 1207 | } |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 1208 | } |
Nikolay Aleksandrov | 5019ab5 | 2017-02-13 14:59:11 +0100 | [diff] [blame] | 1209 | |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 1210 | return err; |
| 1211 | } |
Vlad Yasevich | 8db24af | 2014-05-16 09:59:17 -0400 | [diff] [blame] | 1212 | |
| 1213 | int br_fdb_sync_static(struct net_bridge *br, struct net_bridge_port *p) |
| 1214 | { |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 1215 | struct net_bridge_fdb_entry *f, *tmp; |
Geert Uytterhoeven | 367dc65 | 2018-02-01 11:25:27 +0100 | [diff] [blame] | 1216 | int err = 0; |
Vlad Yasevich | 8db24af | 2014-05-16 09:59:17 -0400 | [diff] [blame] | 1217 | |
| 1218 | ASSERT_RTNL(); |
| 1219 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 1220 | /* the key here is that static entries change only under rtnl */ |
| 1221 | rcu_read_lock(); |
| 1222 | hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) { |
| 1223 | /* We only care for static entries */ |
Nikolay Aleksandrov | 29e63ff | 2019-10-29 13:45:54 +0200 | [diff] [blame] | 1224 | if (!test_bit(BR_FDB_STATIC, &f->flags)) |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 1225 | continue; |
| 1226 | err = dev_uc_add(p->dev, f->key.addr.addr); |
| 1227 | if (err) |
| 1228 | goto rollback; |
Vlad Yasevich | 8db24af | 2014-05-16 09:59:17 -0400 | [diff] [blame] | 1229 | } |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 1230 | done: |
| 1231 | rcu_read_unlock(); |
| 1232 | |
| 1233 | return err; |
Vlad Yasevich | 8db24af | 2014-05-16 09:59:17 -0400 | [diff] [blame] | 1234 | |
| 1235 | rollback: |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 1236 | hlist_for_each_entry_rcu(tmp, &br->fdb_list, fdb_node) { |
| 1237 | /* We only care for static entries */ |
Nikolay Aleksandrov | 29e63ff | 2019-10-29 13:45:54 +0200 | [diff] [blame] | 1238 | if (!test_bit(BR_FDB_STATIC, &tmp->flags)) |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 1239 | continue; |
| 1240 | if (tmp == f) |
| 1241 | break; |
| 1242 | dev_uc_del(p->dev, tmp->key.addr.addr); |
Vlad Yasevich | 8db24af | 2014-05-16 09:59:17 -0400 | [diff] [blame] | 1243 | } |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 1244 | |
| 1245 | goto done; |
Vlad Yasevich | 8db24af | 2014-05-16 09:59:17 -0400 | [diff] [blame] | 1246 | } |
| 1247 | |
| 1248 | void br_fdb_unsync_static(struct net_bridge *br, struct net_bridge_port *p) |
| 1249 | { |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 1250 | struct net_bridge_fdb_entry *f; |
Vlad Yasevich | 8db24af | 2014-05-16 09:59:17 -0400 | [diff] [blame] | 1251 | |
| 1252 | ASSERT_RTNL(); |
| 1253 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 1254 | rcu_read_lock(); |
| 1255 | hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) { |
| 1256 | /* We only care for static entries */ |
Nikolay Aleksandrov | 29e63ff | 2019-10-29 13:45:54 +0200 | [diff] [blame] | 1257 | if (!test_bit(BR_FDB_STATIC, &f->flags)) |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 1258 | continue; |
Vlad Yasevich | 8db24af | 2014-05-16 09:59:17 -0400 | [diff] [blame] | 1259 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 1260 | dev_uc_del(p->dev, f->key.addr.addr); |
Vlad Yasevich | 8db24af | 2014-05-16 09:59:17 -0400 | [diff] [blame] | 1261 | } |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 1262 | rcu_read_unlock(); |
Vlad Yasevich | 8db24af | 2014-05-16 09:59:17 -0400 | [diff] [blame] | 1263 | } |
Scott Feldman | cf6b8e1 | 2014-11-28 14:34:21 +0100 | [diff] [blame] | 1264 | |
Jiri Pirko | 3aeb661 | 2015-01-15 23:49:37 +0100 | [diff] [blame] | 1265 | int br_fdb_external_learn_add(struct net_bridge *br, struct net_bridge_port *p, |
Nikolay Aleksandrov | 45a6878 | 2021-08-10 14:00:10 +0300 | [diff] [blame] | 1266 | const unsigned char *addr, u16 vid, |
Petr Machata | 161d82d | 2018-05-03 14:43:53 +0200 | [diff] [blame] | 1267 | bool swdev_notify) |
Scott Feldman | cf6b8e1 | 2014-11-28 14:34:21 +0100 | [diff] [blame] | 1268 | { |
Scott Feldman | cf6b8e1 | 2014-11-28 14:34:21 +0100 | [diff] [blame] | 1269 | struct net_bridge_fdb_entry *fdb; |
Nikolay Aleksandrov | 7597b26 | 2017-07-03 15:14:59 -0700 | [diff] [blame] | 1270 | bool modified = false; |
Scott Feldman | cf6b8e1 | 2014-11-28 14:34:21 +0100 | [diff] [blame] | 1271 | int err = 0; |
| 1272 | |
Roopa Prabhu | b74fd30 | 2017-08-29 13:16:57 -0700 | [diff] [blame] | 1273 | trace_br_fdb_external_learn_add(br, p, addr, vid); |
| 1274 | |
Scott Feldman | cf6b8e1 | 2014-11-28 14:34:21 +0100 | [diff] [blame] | 1275 | spin_lock_bh(&br->hash_lock); |
| 1276 | |
Nikolay Aleksandrov | bfd0aea | 2017-02-13 14:59:09 +0100 | [diff] [blame] | 1277 | fdb = br_fdb_find(br, addr, vid); |
Scott Feldman | cf6b8e1 | 2014-11-28 14:34:21 +0100 | [diff] [blame] | 1278 | if (!fdb) { |
Nikolay Aleksandrov | 31f1155 | 2019-11-01 14:46:38 +0200 | [diff] [blame] | 1279 | unsigned long flags = BIT(BR_FDB_ADDED_BY_EXT_LEARN); |
| 1280 | |
| 1281 | if (swdev_notify) |
| 1282 | flags |= BIT(BR_FDB_ADDED_BY_USER); |
Vladimir Oltean | 0541a62 | 2021-08-02 02:17:30 +0300 | [diff] [blame] | 1283 | |
Nikolay Aleksandrov | 45a6878 | 2021-08-10 14:00:10 +0300 | [diff] [blame] | 1284 | if (!p) |
Vladimir Oltean | 0541a62 | 2021-08-02 02:17:30 +0300 | [diff] [blame] | 1285 | flags |= BIT(BR_FDB_LOCAL); |
| 1286 | |
Nikolay Aleksandrov | 31f1155 | 2019-11-01 14:46:38 +0200 | [diff] [blame] | 1287 | fdb = fdb_create(br, p, addr, vid, flags); |
Scott Feldman | cf6b8e1 | 2014-11-28 14:34:21 +0100 | [diff] [blame] | 1288 | if (!fdb) { |
| 1289 | err = -ENOMEM; |
| 1290 | goto err_unlock; |
| 1291 | } |
Petr Machata | 161d82d | 2018-05-03 14:43:53 +0200 | [diff] [blame] | 1292 | fdb_notify(br, fdb, RTM_NEWNEIGH, swdev_notify); |
Nikolay Aleksandrov | 7597b26 | 2017-07-03 15:14:59 -0700 | [diff] [blame] | 1293 | } else { |
Scott Feldman | cf6b8e1 | 2014-11-28 14:34:21 +0100 | [diff] [blame] | 1294 | fdb->updated = jiffies; |
Nikolay Aleksandrov | 7597b26 | 2017-07-03 15:14:59 -0700 | [diff] [blame] | 1295 | |
Vladimir Oltean | 3e19ae7 | 2021-06-29 17:06:44 +0300 | [diff] [blame] | 1296 | if (READ_ONCE(fdb->dst) != p) { |
| 1297 | WRITE_ONCE(fdb->dst, p); |
Nikolay Aleksandrov | 7597b26 | 2017-07-03 15:14:59 -0700 | [diff] [blame] | 1298 | modified = true; |
| 1299 | } |
| 1300 | |
Nikolay Aleksandrov | b5cd9f7 | 2019-10-29 13:45:57 +0200 | [diff] [blame] | 1301 | if (test_bit(BR_FDB_ADDED_BY_EXT_LEARN, &fdb->flags)) { |
Nikolay Aleksandrov | 7597b26 | 2017-07-03 15:14:59 -0700 | [diff] [blame] | 1302 | /* Refresh entry */ |
| 1303 | fdb->used = jiffies; |
Nikolay Aleksandrov | ac3ca6a | 2019-10-29 13:45:56 +0200 | [diff] [blame] | 1304 | } else if (!test_bit(BR_FDB_ADDED_BY_USER, &fdb->flags)) { |
Nikolay Aleksandrov | 7597b26 | 2017-07-03 15:14:59 -0700 | [diff] [blame] | 1305 | /* Take over SW learned entry */ |
Nikolay Aleksandrov | b5cd9f7 | 2019-10-29 13:45:57 +0200 | [diff] [blame] | 1306 | set_bit(BR_FDB_ADDED_BY_EXT_LEARN, &fdb->flags); |
Nikolay Aleksandrov | 7597b26 | 2017-07-03 15:14:59 -0700 | [diff] [blame] | 1307 | modified = true; |
| 1308 | } |
| 1309 | |
Ido Schimmel | 710ae72 | 2019-01-18 15:58:00 +0000 | [diff] [blame] | 1310 | if (swdev_notify) |
Nikolay Aleksandrov | ac3ca6a | 2019-10-29 13:45:56 +0200 | [diff] [blame] | 1311 | set_bit(BR_FDB_ADDED_BY_USER, &fdb->flags); |
Ido Schimmel | 710ae72 | 2019-01-18 15:58:00 +0000 | [diff] [blame] | 1312 | |
Nikolay Aleksandrov | 45a6878 | 2021-08-10 14:00:10 +0300 | [diff] [blame] | 1313 | if (!p) |
Vladimir Oltean | 0541a62 | 2021-08-02 02:17:30 +0300 | [diff] [blame] | 1314 | set_bit(BR_FDB_LOCAL, &fdb->flags); |
| 1315 | |
Nikolay Aleksandrov | 7597b26 | 2017-07-03 15:14:59 -0700 | [diff] [blame] | 1316 | if (modified) |
Petr Machata | 161d82d | 2018-05-03 14:43:53 +0200 | [diff] [blame] | 1317 | fdb_notify(br, fdb, RTM_NEWNEIGH, swdev_notify); |
Scott Feldman | cf6b8e1 | 2014-11-28 14:34:21 +0100 | [diff] [blame] | 1318 | } |
| 1319 | |
| 1320 | err_unlock: |
| 1321 | spin_unlock_bh(&br->hash_lock); |
Scott Feldman | cf6b8e1 | 2014-11-28 14:34:21 +0100 | [diff] [blame] | 1322 | |
| 1323 | return err; |
| 1324 | } |
Scott Feldman | cf6b8e1 | 2014-11-28 14:34:21 +0100 | [diff] [blame] | 1325 | |
Jiri Pirko | 3aeb661 | 2015-01-15 23:49:37 +0100 | [diff] [blame] | 1326 | int br_fdb_external_learn_del(struct net_bridge *br, struct net_bridge_port *p, |
Petr Machata | 161d82d | 2018-05-03 14:43:53 +0200 | [diff] [blame] | 1327 | const unsigned char *addr, u16 vid, |
| 1328 | bool swdev_notify) |
Scott Feldman | cf6b8e1 | 2014-11-28 14:34:21 +0100 | [diff] [blame] | 1329 | { |
Scott Feldman | cf6b8e1 | 2014-11-28 14:34:21 +0100 | [diff] [blame] | 1330 | struct net_bridge_fdb_entry *fdb; |
| 1331 | int err = 0; |
| 1332 | |
Scott Feldman | cf6b8e1 | 2014-11-28 14:34:21 +0100 | [diff] [blame] | 1333 | spin_lock_bh(&br->hash_lock); |
| 1334 | |
Nikolay Aleksandrov | bfd0aea | 2017-02-13 14:59:09 +0100 | [diff] [blame] | 1335 | fdb = br_fdb_find(br, addr, vid); |
Nikolay Aleksandrov | b5cd9f7 | 2019-10-29 13:45:57 +0200 | [diff] [blame] | 1336 | if (fdb && test_bit(BR_FDB_ADDED_BY_EXT_LEARN, &fdb->flags)) |
Petr Machata | 161d82d | 2018-05-03 14:43:53 +0200 | [diff] [blame] | 1337 | fdb_delete(br, fdb, swdev_notify); |
Scott Feldman | cf6b8e1 | 2014-11-28 14:34:21 +0100 | [diff] [blame] | 1338 | else |
| 1339 | err = -ENOENT; |
| 1340 | |
| 1341 | spin_unlock_bh(&br->hash_lock); |
Scott Feldman | cf6b8e1 | 2014-11-28 14:34:21 +0100 | [diff] [blame] | 1342 | |
| 1343 | return err; |
| 1344 | } |
Arkadi Sharshevsky | 9fe8bce | 2017-06-08 08:44:15 +0200 | [diff] [blame] | 1345 | |
| 1346 | void br_fdb_offloaded_set(struct net_bridge *br, struct net_bridge_port *p, |
Ido Schimmel | e9ba0fb | 2018-10-17 08:53:29 +0000 | [diff] [blame] | 1347 | const unsigned char *addr, u16 vid, bool offloaded) |
Arkadi Sharshevsky | 9fe8bce | 2017-06-08 08:44:15 +0200 | [diff] [blame] | 1348 | { |
| 1349 | struct net_bridge_fdb_entry *fdb; |
| 1350 | |
| 1351 | spin_lock_bh(&br->hash_lock); |
| 1352 | |
| 1353 | fdb = br_fdb_find(br, addr, vid); |
Nikolay Aleksandrov | d38c6e3 | 2019-10-29 13:45:58 +0200 | [diff] [blame] | 1354 | if (fdb && offloaded != test_bit(BR_FDB_OFFLOADED, &fdb->flags)) |
| 1355 | change_bit(BR_FDB_OFFLOADED, &fdb->flags); |
Arkadi Sharshevsky | 9fe8bce | 2017-06-08 08:44:15 +0200 | [diff] [blame] | 1356 | |
| 1357 | spin_unlock_bh(&br->hash_lock); |
| 1358 | } |
Petr Machata | 43920ed | 2018-12-07 19:55:07 +0000 | [diff] [blame] | 1359 | |
| 1360 | void br_fdb_clear_offload(const struct net_device *dev, u16 vid) |
| 1361 | { |
| 1362 | struct net_bridge_fdb_entry *f; |
| 1363 | struct net_bridge_port *p; |
| 1364 | |
| 1365 | ASSERT_RTNL(); |
| 1366 | |
| 1367 | p = br_port_get_rtnl(dev); |
| 1368 | if (!p) |
| 1369 | return; |
| 1370 | |
| 1371 | spin_lock_bh(&p->br->hash_lock); |
| 1372 | hlist_for_each_entry(f, &p->br->fdb_list, fdb_node) { |
| 1373 | if (f->dst == p && f->key.vlan_id == vid) |
Nikolay Aleksandrov | d38c6e3 | 2019-10-29 13:45:58 +0200 | [diff] [blame] | 1374 | clear_bit(BR_FDB_OFFLOADED, &f->flags); |
Petr Machata | 43920ed | 2018-12-07 19:55:07 +0000 | [diff] [blame] | 1375 | } |
| 1376 | spin_unlock_bh(&p->br->hash_lock); |
| 1377 | } |
| 1378 | EXPORT_SYMBOL_GPL(br_fdb_clear_offload); |