Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Forwarding database |
| 3 | * Linux ethernet bridge |
| 4 | * |
| 5 | * Authors: |
| 6 | * Lennert Buytenhek <buytenh@gnu.org> |
| 7 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License |
| 10 | * as published by the Free Software Foundation; either version |
| 11 | * 2 of the License, or (at your option) any later version. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/init.h> |
Franck Bui-Huu | 8252474 | 2008-05-12 21:21:05 +0200 | [diff] [blame] | 16 | #include <linux/rculist.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/spinlock.h> |
| 18 | #include <linux/times.h> |
| 19 | #include <linux/netdevice.h> |
| 20 | #include <linux/etherdevice.h> |
| 21 | #include <linux/jhash.h> |
Stephen Hemminger | 3f89092 | 2007-03-21 13:42:33 -0700 | [diff] [blame] | 22 | #include <linux/random.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 23 | #include <linux/slab.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 24 | #include <linux/atomic.h> |
Stephen Hemminger | 3f89092 | 2007-03-21 13:42:33 -0700 | [diff] [blame] | 25 | #include <asm/unaligned.h> |
Vlad Yasevich | 2ba071e | 2013-02-13 12:00:16 +0000 | [diff] [blame] | 26 | #include <linux/if_vlan.h> |
Scott Feldman | b4ad7ba | 2015-06-14 11:33:11 -0700 | [diff] [blame] | 27 | #include <net/switchdev.h> |
Roopa Prabhu | b74fd30 | 2017-08-29 13:16:57 -0700 | [diff] [blame] | 28 | #include <trace/events/bridge.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include "br_private.h" |
| 30 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 31 | static const struct rhashtable_params br_fdb_rht_params = { |
| 32 | .head_offset = offsetof(struct net_bridge_fdb_entry, rhnode), |
| 33 | .key_offset = offsetof(struct net_bridge_fdb_entry, key), |
| 34 | .key_len = sizeof(struct net_bridge_fdb_key), |
| 35 | .automatic_shrinking = true, |
| 36 | .locks_mul = 1, |
| 37 | }; |
| 38 | |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 39 | static struct kmem_cache *br_fdb_cache __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source, |
Vlad Yasevich | bc9a25d | 2013-02-13 12:00:19 +0000 | [diff] [blame] | 41 | const unsigned char *addr, u16 vid); |
stephen hemminger | 31e8a49c | 2011-12-08 07:17:41 +0000 | [diff] [blame] | 42 | static void fdb_notify(struct net_bridge *br, |
| 43 | const struct net_bridge_fdb_entry *, int); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
Akinobu Mita | 87a596e | 2007-04-07 18:57:07 +0900 | [diff] [blame] | 45 | int __init br_fdb_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | { |
| 47 | br_fdb_cache = kmem_cache_create("bridge_fdb_cache", |
| 48 | sizeof(struct net_bridge_fdb_entry), |
| 49 | 0, |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 50 | SLAB_HWCACHE_ALIGN, NULL); |
Akinobu Mita | 87a596e | 2007-04-07 18:57:07 +0900 | [diff] [blame] | 51 | if (!br_fdb_cache) |
| 52 | return -ENOMEM; |
| 53 | |
Akinobu Mita | 87a596e | 2007-04-07 18:57:07 +0900 | [diff] [blame] | 54 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Andrew Morton | 73afc90 | 2007-12-05 21:35:23 -0800 | [diff] [blame] | 57 | void br_fdb_fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | { |
| 59 | kmem_cache_destroy(br_fdb_cache); |
| 60 | } |
| 61 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 62 | int br_fdb_hash_init(struct net_bridge *br) |
| 63 | { |
| 64 | return rhashtable_init(&br->fdb_hash_tbl, &br_fdb_rht_params); |
| 65 | } |
| 66 | |
| 67 | void br_fdb_hash_fini(struct net_bridge *br) |
| 68 | { |
| 69 | rhashtable_destroy(&br->fdb_hash_tbl); |
| 70 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
| 72 | /* if topology_changing then use forward_delay (default 15 sec) |
| 73 | * otherwise keep longer (default 5 minutes) |
| 74 | */ |
Stephen Hemminger | 3f89092 | 2007-03-21 13:42:33 -0700 | [diff] [blame] | 75 | static inline unsigned long hold_time(const struct net_bridge *br) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | { |
| 77 | return br->topology_change ? br->forward_delay : br->ageing_time; |
| 78 | } |
| 79 | |
Stephen Hemminger | 3f89092 | 2007-03-21 13:42:33 -0700 | [diff] [blame] | 80 | static inline int has_expired(const struct net_bridge *br, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | const struct net_bridge_fdb_entry *fdb) |
| 82 | { |
Roopa Prabhu | eda7a5e | 2017-02-16 13:38:04 -0800 | [diff] [blame] | 83 | return !fdb->is_static && !fdb->added_by_external_learn && |
stephen hemminger | 7cd8861 | 2011-04-04 14:03:28 +0000 | [diff] [blame] | 84 | time_before_eq(fdb->updated + hold_time(br), jiffies); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Michał Mirosław | da67829 | 2009-06-05 05:35:28 +0000 | [diff] [blame] | 87 | static void fdb_rcu_free(struct rcu_head *head) |
| 88 | { |
| 89 | struct net_bridge_fdb_entry *ent |
| 90 | = container_of(head, struct net_bridge_fdb_entry, rcu); |
| 91 | kmem_cache_free(br_fdb_cache, ent); |
| 92 | } |
| 93 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 94 | static struct net_bridge_fdb_entry *fdb_find_rcu(struct rhashtable *tbl, |
Nikolay Aleksandrov | bfd0aea | 2017-02-13 14:59:09 +0100 | [diff] [blame] | 95 | const unsigned char *addr, |
| 96 | __u16 vid) |
| 97 | { |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 98 | struct net_bridge_fdb_key key; |
Nikolay Aleksandrov | bfd0aea | 2017-02-13 14:59:09 +0100 | [diff] [blame] | 99 | |
Nikolay Aleksandrov | 410b3d4 | 2017-02-13 14:59:10 +0100 | [diff] [blame] | 100 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 101 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 102 | key.vlan_id = vid; |
| 103 | memcpy(key.addr.addr, addr, sizeof(key.addr.addr)); |
Nikolay Aleksandrov | bfd0aea | 2017-02-13 14:59:09 +0100 | [diff] [blame] | 104 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 105 | return rhashtable_lookup(tbl, &key, br_fdb_rht_params); |
Nikolay Aleksandrov | bfd0aea | 2017-02-13 14:59:09 +0100 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | /* requires bridge hash_lock */ |
| 109 | static struct net_bridge_fdb_entry *br_fdb_find(struct net_bridge *br, |
| 110 | const unsigned char *addr, |
| 111 | __u16 vid) |
| 112 | { |
Nikolay Aleksandrov | bfd0aea | 2017-02-13 14:59:09 +0100 | [diff] [blame] | 113 | struct net_bridge_fdb_entry *fdb; |
| 114 | |
WANG Cong | d12c917 | 2017-03-16 10:32:42 -0700 | [diff] [blame] | 115 | lockdep_assert_held_once(&br->hash_lock); |
Nikolay Aleksandrov | 410b3d4 | 2017-02-13 14:59:10 +0100 | [diff] [blame] | 116 | |
Nikolay Aleksandrov | bfd0aea | 2017-02-13 14:59:09 +0100 | [diff] [blame] | 117 | rcu_read_lock(); |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 118 | fdb = fdb_find_rcu(&br->fdb_hash_tbl, addr, vid); |
Nikolay Aleksandrov | bfd0aea | 2017-02-13 14:59:09 +0100 | [diff] [blame] | 119 | rcu_read_unlock(); |
| 120 | |
| 121 | return fdb; |
| 122 | } |
| 123 | |
| 124 | struct net_bridge_fdb_entry *br_fdb_find_rcu(struct net_bridge *br, |
| 125 | const unsigned char *addr, |
| 126 | __u16 vid) |
| 127 | { |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 128 | return fdb_find_rcu(&br->fdb_hash_tbl, addr, vid); |
Nikolay Aleksandrov | bfd0aea | 2017-02-13 14:59:09 +0100 | [diff] [blame] | 129 | } |
| 130 | |
Vlad Yasevich | 145beee | 2014-05-16 09:59:19 -0400 | [diff] [blame] | 131 | /* When a static FDB entry is added, the mac address from the entry is |
| 132 | * added to the bridge private HW address list and all required ports |
| 133 | * are then updated with the new information. |
| 134 | * Called under RTNL. |
| 135 | */ |
Jiri Pirko | 020ec6b | 2014-11-28 14:34:12 +0100 | [diff] [blame] | 136 | 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] | 137 | { |
| 138 | int err; |
Li RongQing | a3f5ee7 | 2014-06-18 16:07:16 +0800 | [diff] [blame] | 139 | struct net_bridge_port *p; |
Vlad Yasevich | 145beee | 2014-05-16 09:59:19 -0400 | [diff] [blame] | 140 | |
| 141 | ASSERT_RTNL(); |
| 142 | |
| 143 | list_for_each_entry(p, &br->port_list, list) { |
| 144 | if (!br_promisc_port(p)) { |
| 145 | err = dev_uc_add(p->dev, addr); |
| 146 | if (err) |
| 147 | goto undo; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | return; |
| 152 | undo: |
Li RongQing | a3f5ee7 | 2014-06-18 16:07:16 +0800 | [diff] [blame] | 153 | list_for_each_entry_continue_reverse(p, &br->port_list, list) { |
| 154 | if (!br_promisc_port(p)) |
| 155 | dev_uc_del(p->dev, addr); |
Vlad Yasevich | 145beee | 2014-05-16 09:59:19 -0400 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
| 159 | /* When a static FDB entry is deleted, the HW address from that entry is |
| 160 | * also removed from the bridge private HW address list and updates all |
| 161 | * the ports with needed information. |
| 162 | * Called under RTNL. |
| 163 | */ |
Jiri Pirko | 020ec6b | 2014-11-28 14:34:12 +0100 | [diff] [blame] | 164 | 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] | 165 | { |
| 166 | struct net_bridge_port *p; |
| 167 | |
| 168 | ASSERT_RTNL(); |
| 169 | |
| 170 | list_for_each_entry(p, &br->port_list, list) { |
| 171 | if (!br_promisc_port(p)) |
| 172 | dev_uc_del(p->dev, addr); |
| 173 | } |
| 174 | } |
| 175 | |
stephen hemminger | 31e8a49c | 2011-12-08 07:17:41 +0000 | [diff] [blame] | 176 | static void fdb_delete(struct net_bridge *br, struct net_bridge_fdb_entry *f) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | { |
Roopa Prabhu | b74fd30 | 2017-08-29 13:16:57 -0700 | [diff] [blame] | 178 | trace_fdb_delete(br, f); |
| 179 | |
Vlad Yasevich | 145beee | 2014-05-16 09:59:19 -0400 | [diff] [blame] | 180 | if (f->is_static) |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 181 | fdb_del_hw_addr(br, f->key.addr.addr); |
Vlad Yasevich | 145beee | 2014-05-16 09:59:19 -0400 | [diff] [blame] | 182 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 183 | hlist_del_init_rcu(&f->fdb_node); |
| 184 | rhashtable_remove_fast(&br->fdb_hash_tbl, &f->rhnode, |
| 185 | br_fdb_rht_params); |
stephen hemminger | 31e8a49c | 2011-12-08 07:17:41 +0000 | [diff] [blame] | 186 | fdb_notify(br, f, RTM_DELNEIGH); |
Michał Mirosław | da67829 | 2009-06-05 05:35:28 +0000 | [diff] [blame] | 187 | call_rcu(&f->rcu, fdb_rcu_free); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Toshiaki Makita | 960b589 | 2014-02-07 16:48:23 +0900 | [diff] [blame] | 190 | /* Delete a local entry if no other port had the same address. */ |
| 191 | static void fdb_delete_local(struct net_bridge *br, |
| 192 | const struct net_bridge_port *p, |
| 193 | struct net_bridge_fdb_entry *f) |
| 194 | { |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 195 | const unsigned char *addr = f->key.addr.addr; |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 196 | struct net_bridge_vlan_group *vg; |
| 197 | const struct net_bridge_vlan *v; |
Toshiaki Makita | 960b589 | 2014-02-07 16:48:23 +0900 | [diff] [blame] | 198 | struct net_bridge_port *op; |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 199 | u16 vid = f->key.vlan_id; |
Toshiaki Makita | 960b589 | 2014-02-07 16:48:23 +0900 | [diff] [blame] | 200 | |
| 201 | /* Maybe another port has same hw addr? */ |
| 202 | list_for_each_entry(op, &br->port_list, list) { |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 203 | vg = nbp_vlan_group(op); |
Toshiaki Makita | 960b589 | 2014-02-07 16:48:23 +0900 | [diff] [blame] | 204 | if (op != p && ether_addr_equal(op->dev->dev_addr, addr) && |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 205 | (!vid || br_vlan_find(vg, vid))) { |
Toshiaki Makita | 960b589 | 2014-02-07 16:48:23 +0900 | [diff] [blame] | 206 | f->dst = op; |
Toshiaki Makita | a778e6d | 2014-02-07 16:48:24 +0900 | [diff] [blame] | 207 | f->added_by_user = 0; |
Toshiaki Makita | 960b589 | 2014-02-07 16:48:23 +0900 | [diff] [blame] | 208 | return; |
| 209 | } |
| 210 | } |
| 211 | |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 212 | vg = br_vlan_group(br); |
| 213 | v = br_vlan_find(vg, vid); |
Toshiaki Makita | 960b589 | 2014-02-07 16:48:23 +0900 | [diff] [blame] | 214 | /* Maybe bridge device has same hw addr? */ |
| 215 | if (p && ether_addr_equal(br->dev->dev_addr, addr) && |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 216 | (!vid || (v && br_vlan_should_use(v)))) { |
Toshiaki Makita | 960b589 | 2014-02-07 16:48:23 +0900 | [diff] [blame] | 217 | f->dst = NULL; |
Toshiaki Makita | a778e6d | 2014-02-07 16:48:24 +0900 | [diff] [blame] | 218 | f->added_by_user = 0; |
Toshiaki Makita | 960b589 | 2014-02-07 16:48:23 +0900 | [diff] [blame] | 219 | return; |
| 220 | } |
| 221 | |
| 222 | fdb_delete(br, f); |
| 223 | } |
| 224 | |
Toshiaki Makita | 424bb9c | 2014-02-07 16:48:25 +0900 | [diff] [blame] | 225 | void br_fdb_find_delete_local(struct net_bridge *br, |
| 226 | const struct net_bridge_port *p, |
| 227 | const unsigned char *addr, u16 vid) |
| 228 | { |
Toshiaki Makita | 424bb9c | 2014-02-07 16:48:25 +0900 | [diff] [blame] | 229 | struct net_bridge_fdb_entry *f; |
| 230 | |
| 231 | spin_lock_bh(&br->hash_lock); |
Nikolay Aleksandrov | bfd0aea | 2017-02-13 14:59:09 +0100 | [diff] [blame] | 232 | f = br_fdb_find(br, addr, vid); |
Toshiaki Makita | 424bb9c | 2014-02-07 16:48:25 +0900 | [diff] [blame] | 233 | if (f && f->is_local && !f->added_by_user && f->dst == p) |
| 234 | fdb_delete_local(br, p, f); |
| 235 | spin_unlock_bh(&br->hash_lock); |
| 236 | } |
| 237 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | void br_fdb_changeaddr(struct net_bridge_port *p, const unsigned char *newaddr) |
| 239 | { |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 240 | struct net_bridge_vlan_group *vg; |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 241 | struct net_bridge_fdb_entry *f; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | struct net_bridge *br = p->br; |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 243 | struct net_bridge_vlan *v; |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 244 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | spin_lock_bh(&br->hash_lock); |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 246 | vg = nbp_vlan_group(p); |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 247 | hlist_for_each_entry(f, &br->fdb_list, fdb_node) { |
| 248 | if (f->dst == p && f->is_local && !f->added_by_user) { |
| 249 | /* delete old one */ |
| 250 | fdb_delete_local(br, p, f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 252 | /* if this port has no vlan information |
| 253 | * configured, we can safely be done at |
| 254 | * this point. |
| 255 | */ |
| 256 | if (!vg || !vg->num_vlans) |
| 257 | goto insert; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | } |
| 259 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | |
Toshiaki Makita | 2836882 | 2014-02-07 16:48:19 +0900 | [diff] [blame] | 261 | insert: |
| 262 | /* insert new address, may fail if invalid address or dup. */ |
| 263 | fdb_insert(br, p, newaddr, 0); |
| 264 | |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 265 | if (!vg || !vg->num_vlans) |
Toshiaki Makita | 2836882 | 2014-02-07 16:48:19 +0900 | [diff] [blame] | 266 | goto done; |
| 267 | |
| 268 | /* Now add entries for every VLAN configured on the port. |
| 269 | * This function runs under RTNL so the bitmap will not change |
| 270 | * from under us. |
| 271 | */ |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 272 | list_for_each_entry(v, &vg->vlan_list, vlist) |
| 273 | fdb_insert(br, p, newaddr, v->vid); |
Toshiaki Makita | 2836882 | 2014-02-07 16:48:19 +0900 | [diff] [blame] | 274 | |
Vlad Yasevich | bc9a25d | 2013-02-13 12:00:19 +0000 | [diff] [blame] | 275 | done: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | spin_unlock_bh(&br->hash_lock); |
| 277 | } |
| 278 | |
stephen hemminger | 4359881 | 2011-12-08 07:17:49 +0000 | [diff] [blame] | 279 | void br_fdb_change_mac_address(struct net_bridge *br, const u8 *newaddr) |
| 280 | { |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 281 | struct net_bridge_vlan_group *vg; |
stephen hemminger | 4359881 | 2011-12-08 07:17:49 +0000 | [diff] [blame] | 282 | struct net_bridge_fdb_entry *f; |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 283 | struct net_bridge_vlan *v; |
stephen hemminger | 4359881 | 2011-12-08 07:17:49 +0000 | [diff] [blame] | 284 | |
Toshiaki Makita | ac4c886 | 2014-02-07 16:48:26 +0900 | [diff] [blame] | 285 | spin_lock_bh(&br->hash_lock); |
| 286 | |
stephen hemminger | 4359881 | 2011-12-08 07:17:49 +0000 | [diff] [blame] | 287 | /* If old entry was unassociated with any port, then delete it. */ |
Nikolay Aleksandrov | bfd0aea | 2017-02-13 14:59:09 +0100 | [diff] [blame] | 288 | f = br_fdb_find(br, br->dev->dev_addr, 0); |
Toshiaki Makita | 7bb90c3 | 2016-08-04 11:11:19 +0900 | [diff] [blame] | 289 | if (f && f->is_local && !f->dst && !f->added_by_user) |
Toshiaki Makita | 960b589 | 2014-02-07 16:48:23 +0900 | [diff] [blame] | 290 | fdb_delete_local(br, NULL, f); |
stephen hemminger | 4359881 | 2011-12-08 07:17:49 +0000 | [diff] [blame] | 291 | |
Vlad Yasevich | bc9a25d | 2013-02-13 12:00:19 +0000 | [diff] [blame] | 292 | fdb_insert(br, NULL, newaddr, 0); |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 293 | vg = br_vlan_group(br); |
| 294 | if (!vg || !vg->num_vlans) |
| 295 | goto out; |
Vlad Yasevich | bc9a25d | 2013-02-13 12:00:19 +0000 | [diff] [blame] | 296 | /* Now remove and add entries for every VLAN configured on the |
| 297 | * bridge. This function runs under RTNL so the bitmap will not |
| 298 | * change from under us. |
| 299 | */ |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 300 | list_for_each_entry(v, &vg->vlan_list, vlist) { |
Toshiaki Makita | 0b148de | 2016-06-07 19:14:17 +0900 | [diff] [blame] | 301 | if (!br_vlan_should_use(v)) |
| 302 | continue; |
Nikolay Aleksandrov | bfd0aea | 2017-02-13 14:59:09 +0100 | [diff] [blame] | 303 | f = br_fdb_find(br, br->dev->dev_addr, v->vid); |
Toshiaki Makita | 7bb90c3 | 2016-08-04 11:11:19 +0900 | [diff] [blame] | 304 | if (f && f->is_local && !f->dst && !f->added_by_user) |
Toshiaki Makita | 960b589 | 2014-02-07 16:48:23 +0900 | [diff] [blame] | 305 | fdb_delete_local(br, NULL, f); |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 306 | fdb_insert(br, NULL, newaddr, v->vid); |
Vlad Yasevich | bc9a25d | 2013-02-13 12:00:19 +0000 | [diff] [blame] | 307 | } |
Toshiaki Makita | ac4c886 | 2014-02-07 16:48:26 +0900 | [diff] [blame] | 308 | out: |
| 309 | spin_unlock_bh(&br->hash_lock); |
stephen hemminger | 4359881 | 2011-12-08 07:17:49 +0000 | [diff] [blame] | 310 | } |
| 311 | |
Nikolay Aleksandrov | f7cdee8 | 2017-02-04 18:05:07 +0100 | [diff] [blame] | 312 | void br_fdb_cleanup(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | { |
Nikolay Aleksandrov | f7cdee8 | 2017-02-04 18:05:07 +0100 | [diff] [blame] | 314 | struct net_bridge *br = container_of(work, struct net_bridge, |
| 315 | gc_work.work); |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 316 | struct net_bridge_fdb_entry *f = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | unsigned long delay = hold_time(br); |
Nikolay Aleksandrov | f7cdee8 | 2017-02-04 18:05:07 +0100 | [diff] [blame] | 318 | unsigned long work_delay = delay; |
| 319 | unsigned long now = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 321 | /* this part is tricky, in order to avoid blocking learning and |
| 322 | * consequently forwarding, we rely on rcu to delete objects with |
| 323 | * delayed freeing allowing us to continue traversing |
| 324 | */ |
| 325 | rcu_read_lock(); |
| 326 | hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) { |
| 327 | unsigned long this_timer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 329 | if (f->is_static || f->added_by_external_learn) |
Nikolay Aleksandrov | f7cdee8 | 2017-02-04 18:05:07 +0100 | [diff] [blame] | 330 | continue; |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 331 | this_timer = f->updated + delay; |
| 332 | if (time_after(this_timer, now)) { |
| 333 | work_delay = min(work_delay, this_timer - now); |
| 334 | } else { |
| 335 | spin_lock_bh(&br->hash_lock); |
| 336 | if (!hlist_unhashed(&f->fdb_node)) |
stephen hemminger | 31e8a49c | 2011-12-08 07:17:41 +0000 | [diff] [blame] | 337 | fdb_delete(br, f); |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 338 | spin_unlock_bh(&br->hash_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | } |
| 340 | } |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 341 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | |
Nikolay Aleksandrov | f7cdee8 | 2017-02-04 18:05:07 +0100 | [diff] [blame] | 343 | /* Cleanup minimum 10 milliseconds apart */ |
| 344 | work_delay = max_t(unsigned long, work_delay, msecs_to_jiffies(10)); |
| 345 | mod_delayed_work(system_long_wq, &br->gc_work, work_delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | } |
| 347 | |
Stephen Hemminger | 9cf6374 | 2007-04-09 12:57:54 -0700 | [diff] [blame] | 348 | /* Completely flush all dynamic entries in forwarding database.*/ |
| 349 | void br_fdb_flush(struct net_bridge *br) |
| 350 | { |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 351 | struct net_bridge_fdb_entry *f; |
| 352 | struct hlist_node *tmp; |
Stephen Hemminger | 1a62069 | 2006-10-12 14:45:38 -0700 | [diff] [blame] | 353 | |
Stephen Hemminger | 9cf6374 | 2007-04-09 12:57:54 -0700 | [diff] [blame] | 354 | spin_lock_bh(&br->hash_lock); |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 355 | hlist_for_each_entry_safe(f, tmp, &br->fdb_list, fdb_node) { |
| 356 | if (!f->is_static) |
| 357 | fdb_delete(br, f); |
Stephen Hemminger | 9cf6374 | 2007-04-09 12:57:54 -0700 | [diff] [blame] | 358 | } |
| 359 | spin_unlock_bh(&br->hash_lock); |
| 360 | } |
| 361 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 362 | /* Flush all entries referring to a specific port. |
Stephen Hemminger | 9cf6374 | 2007-04-09 12:57:54 -0700 | [diff] [blame] | 363 | * if do_all is set also flush static entries |
Nikolay Aleksandrov | 1ea2d02 | 2015-06-23 05:28:16 -0700 | [diff] [blame] | 364 | * if vid is set delete all entries that match the vlan_id |
Stephen Hemminger | 9cf6374 | 2007-04-09 12:57:54 -0700 | [diff] [blame] | 365 | */ |
Stephen Hemminger | 1a62069 | 2006-10-12 14:45:38 -0700 | [diff] [blame] | 366 | void br_fdb_delete_by_port(struct net_bridge *br, |
| 367 | const struct net_bridge_port *p, |
Nikolay Aleksandrov | 1ea2d02 | 2015-06-23 05:28:16 -0700 | [diff] [blame] | 368 | u16 vid, |
Stephen Hemminger | 1a62069 | 2006-10-12 14:45:38 -0700 | [diff] [blame] | 369 | int do_all) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | { |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 371 | struct net_bridge_fdb_entry *f; |
| 372 | struct hlist_node *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | |
| 374 | spin_lock_bh(&br->hash_lock); |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 375 | hlist_for_each_entry_safe(f, tmp, &br->fdb_list, fdb_node) { |
| 376 | if (f->dst != p) |
| 377 | continue; |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 378 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 379 | if (!do_all) |
| 380 | if (f->is_static || (vid && f->key.vlan_id != vid)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | continue; |
| 382 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 383 | if (f->is_local) |
| 384 | fdb_delete_local(br, p, f); |
| 385 | else |
| 386 | fdb_delete(br, f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | } |
| 388 | spin_unlock_bh(&br->hash_lock); |
| 389 | } |
| 390 | |
Igor Maravić | e6373c4 | 2011-12-12 02:58:25 +0000 | [diff] [blame] | 391 | #if IS_ENABLED(CONFIG_ATM_LANE) |
Michał Mirosław | da67829 | 2009-06-05 05:35:28 +0000 | [diff] [blame] | 392 | /* Interface used by ATM LANE hook to test |
| 393 | * if an addr is on some other bridge port */ |
| 394 | int br_fdb_test_addr(struct net_device *dev, unsigned char *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | { |
| 396 | struct net_bridge_fdb_entry *fdb; |
stephen hemminger | b5ed54e | 2010-11-15 06:38:13 +0000 | [diff] [blame] | 397 | struct net_bridge_port *port; |
Michał Mirosław | da67829 | 2009-06-05 05:35:28 +0000 | [diff] [blame] | 398 | int ret; |
| 399 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | rcu_read_lock(); |
stephen hemminger | b5ed54e | 2010-11-15 06:38:13 +0000 | [diff] [blame] | 401 | port = br_port_get_rcu(dev); |
| 402 | if (!port) |
| 403 | ret = 0; |
| 404 | else { |
Nikolay Aleksandrov | bfd0aea | 2017-02-13 14:59:09 +0100 | [diff] [blame] | 405 | fdb = br_fdb_find_rcu(port->br, addr, 0); |
stephen hemminger | 4359881 | 2011-12-08 07:17:49 +0000 | [diff] [blame] | 406 | ret = fdb && fdb->dst && fdb->dst->dev != dev && |
stephen hemminger | b5ed54e | 2010-11-15 06:38:13 +0000 | [diff] [blame] | 407 | fdb->dst->state == BR_STATE_FORWARDING; |
| 408 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | |
Michał Mirosław | da67829 | 2009-06-05 05:35:28 +0000 | [diff] [blame] | 411 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | } |
Michał Mirosław | da67829 | 2009-06-05 05:35:28 +0000 | [diff] [blame] | 413 | #endif /* CONFIG_ATM_LANE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | |
| 415 | /* |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 416 | * Fill buffer with forwarding table records in |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | * the API format. |
| 418 | */ |
| 419 | int br_fdb_fillbuf(struct net_bridge *br, void *buf, |
| 420 | unsigned long maxnum, unsigned long skip) |
| 421 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | struct net_bridge_fdb_entry *f; |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 423 | struct __fdb_entry *fe = buf; |
| 424 | int num = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | |
| 426 | memset(buf, 0, maxnum*sizeof(struct __fdb_entry)); |
| 427 | |
| 428 | rcu_read_lock(); |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 429 | hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) { |
| 430 | if (num >= maxnum) |
| 431 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 433 | if (has_expired(br, f)) |
| 434 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 436 | /* ignore pseudo entry for local MAC address */ |
| 437 | if (!f->dst) |
| 438 | continue; |
stephen hemminger | 4359881 | 2011-12-08 07:17:49 +0000 | [diff] [blame] | 439 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 440 | if (skip) { |
| 441 | --skip; |
| 442 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 445 | /* convert from internal format to API */ |
| 446 | memcpy(fe->mac_addr, f->key.addr.addr, ETH_ALEN); |
| 447 | |
| 448 | /* due to ABI compat need to split into hi/lo */ |
| 449 | fe->port_no = f->dst->port_no; |
| 450 | fe->port_hi = f->dst->port_no >> 8; |
| 451 | |
| 452 | fe->is_local = f->is_local; |
| 453 | if (!f->is_static) |
| 454 | fe->ageing_timer_value = jiffies_delta_to_clock_t(jiffies - f->updated); |
| 455 | ++fe; |
| 456 | ++num; |
| 457 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | rcu_read_unlock(); |
| 459 | |
| 460 | return num; |
| 461 | } |
| 462 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 463 | static struct net_bridge_fdb_entry *fdb_create(struct net_bridge *br, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | struct net_bridge_port *source, |
Vlad Yasevich | 2ba071e | 2013-02-13 12:00:16 +0000 | [diff] [blame] | 465 | const unsigned char *addr, |
Roopa Prabhu | b7af147 | 2015-10-27 07:52:56 -0700 | [diff] [blame] | 466 | __u16 vid, |
| 467 | unsigned char is_local, |
| 468 | unsigned char is_static) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | { |
| 470 | struct net_bridge_fdb_entry *fdb; |
| 471 | |
| 472 | fdb = kmem_cache_alloc(br_fdb_cache, GFP_ATOMIC); |
| 473 | if (fdb) { |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 474 | memcpy(fdb->key.addr.addr, addr, ETH_ALEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | fdb->dst = source; |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 476 | fdb->key.vlan_id = vid; |
Roopa Prabhu | b7af147 | 2015-10-27 07:52:56 -0700 | [diff] [blame] | 477 | fdb->is_local = is_local; |
| 478 | fdb->is_static = is_static; |
Toshiaki Makita | a5642ab | 2014-02-07 16:48:18 +0900 | [diff] [blame] | 479 | fdb->added_by_user = 0; |
Scott Feldman | cf6b8e1 | 2014-11-28 14:34:21 +0100 | [diff] [blame] | 480 | fdb->added_by_external_learn = 0; |
Arkadi Sharshevsky | 9fe8bce | 2017-06-08 08:44:15 +0200 | [diff] [blame] | 481 | fdb->offloaded = 0; |
stephen hemminger | 7cd8861 | 2011-04-04 14:03:28 +0000 | [diff] [blame] | 482 | fdb->updated = fdb->used = jiffies; |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 483 | if (rhashtable_lookup_insert_fast(&br->fdb_hash_tbl, |
| 484 | &fdb->rhnode, |
| 485 | br_fdb_rht_params)) { |
| 486 | kmem_cache_free(br_fdb_cache, fdb); |
| 487 | fdb = NULL; |
| 488 | } else { |
| 489 | hlist_add_head_rcu(&fdb->fdb_node, &br->fdb_list); |
| 490 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | } |
| 492 | return fdb; |
| 493 | } |
| 494 | |
| 495 | static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source, |
Vlad Yasevich | bc9a25d | 2013-02-13 12:00:19 +0000 | [diff] [blame] | 496 | const unsigned char *addr, u16 vid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | struct net_bridge_fdb_entry *fdb; |
| 499 | |
| 500 | if (!is_valid_ether_addr(addr)) |
| 501 | return -EINVAL; |
| 502 | |
Nikolay Aleksandrov | bfd0aea | 2017-02-13 14:59:09 +0100 | [diff] [blame] | 503 | fdb = br_fdb_find(br, addr, vid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | if (fdb) { |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 505 | /* it is okay to have multiple ports with same |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | * address, just use the first one. |
| 507 | */ |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 508 | if (fdb->is_local) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | return 0; |
Roopa Prabhu | de1dfee | 2016-10-11 16:12:56 -0700 | [diff] [blame] | 510 | br_warn(br, "adding interface %s with same address as a received packet (addr:%pM, vlan:%u)\n", |
| 511 | source ? source->dev->name : br->dev->name, addr, vid); |
stephen hemminger | 31e8a49c | 2011-12-08 07:17:41 +0000 | [diff] [blame] | 512 | fdb_delete(br, fdb); |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 513 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 515 | fdb = fdb_create(br, source, addr, vid, 1, 1); |
stephen hemminger | 03e9b64 | 2011-04-04 14:03:27 +0000 | [diff] [blame] | 516 | if (!fdb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | return -ENOMEM; |
| 518 | |
Jiri Pirko | 020ec6b | 2014-11-28 14:34:12 +0100 | [diff] [blame] | 519 | fdb_add_hw_addr(br, addr); |
stephen hemminger | 31e8a49c | 2011-12-08 07:17:41 +0000 | [diff] [blame] | 520 | fdb_notify(br, fdb, RTM_NEWNEIGH); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | return 0; |
| 522 | } |
| 523 | |
stephen hemminger | 03e9b64 | 2011-04-04 14:03:27 +0000 | [diff] [blame] | 524 | /* Add entry for local address of interface */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | int br_fdb_insert(struct net_bridge *br, struct net_bridge_port *source, |
Vlad Yasevich | bc9a25d | 2013-02-13 12:00:19 +0000 | [diff] [blame] | 526 | const unsigned char *addr, u16 vid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | { |
| 528 | int ret; |
| 529 | |
| 530 | spin_lock_bh(&br->hash_lock); |
Vlad Yasevich | bc9a25d | 2013-02-13 12:00:19 +0000 | [diff] [blame] | 531 | ret = fdb_insert(br, source, addr, vid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | spin_unlock_bh(&br->hash_lock); |
| 533 | return ret; |
| 534 | } |
| 535 | |
| 536 | void br_fdb_update(struct net_bridge *br, struct net_bridge_port *source, |
Toshiaki Makita | a5642ab | 2014-02-07 16:48:18 +0900 | [diff] [blame] | 537 | const unsigned char *addr, u16 vid, bool added_by_user) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | struct net_bridge_fdb_entry *fdb; |
Jon Maxwell | c65c7a3 | 2014-05-29 17:27:16 +1000 | [diff] [blame] | 540 | bool fdb_modified = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | |
| 542 | /* some users want to always flood. */ |
| 543 | if (hold_time(br) == 0) |
| 544 | return; |
| 545 | |
Stephen Hemminger | df1c0b8 | 2007-08-30 22:15:35 -0700 | [diff] [blame] | 546 | /* ignore packets unless we are using this port */ |
| 547 | if (!(source->state == BR_STATE_LEARNING || |
| 548 | source->state == BR_STATE_FORWARDING)) |
| 549 | return; |
| 550 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 551 | fdb = fdb_find_rcu(&br->fdb_hash_tbl, addr, vid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | if (likely(fdb)) { |
| 553 | /* attempt to update an entry for a local interface */ |
| 554 | if (unlikely(fdb->is_local)) { |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 555 | if (net_ratelimit()) |
Roopa Prabhu | de1dfee | 2016-10-11 16:12:56 -0700 | [diff] [blame] | 556 | br_warn(br, "received packet on %s with own address as source address (addr:%pM, vlan:%u)\n", |
| 557 | source->dev->name, addr, vid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | } else { |
stephen hemminger | ca6d448 | 2017-02-07 08:46:46 -0800 | [diff] [blame] | 559 | unsigned long now = jiffies; |
| 560 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | /* fastpath: update of existing entry */ |
Jon Maxwell | c65c7a3 | 2014-05-29 17:27:16 +1000 | [diff] [blame] | 562 | if (unlikely(source != fdb->dst)) { |
| 563 | fdb->dst = source; |
| 564 | fdb_modified = true; |
Arkadi Sharshevsky | 58073b3 | 2017-04-28 22:39:07 +0300 | [diff] [blame] | 565 | /* Take over HW learned entry */ |
| 566 | if (unlikely(fdb->added_by_external_learn)) |
| 567 | fdb->added_by_external_learn = 0; |
Jon Maxwell | c65c7a3 | 2014-05-29 17:27:16 +1000 | [diff] [blame] | 568 | } |
stephen hemminger | ca6d448 | 2017-02-07 08:46:46 -0800 | [diff] [blame] | 569 | if (now != fdb->updated) |
| 570 | fdb->updated = now; |
Toshiaki Makita | a5642ab | 2014-02-07 16:48:18 +0900 | [diff] [blame] | 571 | if (unlikely(added_by_user)) |
| 572 | fdb->added_by_user = 1; |
Roopa Prabhu | e3cfddd | 2017-08-30 22:18:13 -0700 | [diff] [blame] | 573 | if (unlikely(fdb_modified)) { |
| 574 | trace_br_fdb_update(br, source, addr, vid, added_by_user); |
Jon Maxwell | c65c7a3 | 2014-05-29 17:27:16 +1000 | [diff] [blame] | 575 | fdb_notify(br, fdb, RTM_NEWNEIGH); |
Roopa Prabhu | e3cfddd | 2017-08-30 22:18:13 -0700 | [diff] [blame] | 576 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | } |
| 578 | } else { |
Stephen Hemminger | f8ae737 | 2006-03-20 22:58:36 -0800 | [diff] [blame] | 579 | spin_lock(&br->hash_lock); |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 580 | fdb = fdb_create(br, source, addr, vid, 0, 0); |
| 581 | if (fdb) { |
| 582 | if (unlikely(added_by_user)) |
| 583 | fdb->added_by_user = 1; |
| 584 | trace_br_fdb_update(br, source, addr, vid, |
| 585 | added_by_user); |
| 586 | fdb_notify(br, fdb, RTM_NEWNEIGH); |
stephen hemminger | f58ee4e | 2011-12-06 13:02:24 +0000 | [diff] [blame] | 587 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | /* else we lose race and someone else inserts |
| 589 | * it first, don't bother updating |
| 590 | */ |
Stephen Hemminger | f8ae737 | 2006-03-20 22:58:36 -0800 | [diff] [blame] | 591 | spin_unlock(&br->hash_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | } |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 594 | |
Roopa Prabhu | 3741873 | 2015-10-08 10:38:52 -0700 | [diff] [blame] | 595 | static int fdb_to_nud(const struct net_bridge *br, |
| 596 | const struct net_bridge_fdb_entry *fdb) |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 597 | { |
| 598 | if (fdb->is_local) |
| 599 | return NUD_PERMANENT; |
| 600 | else if (fdb->is_static) |
| 601 | return NUD_NOARP; |
Roopa Prabhu | 3741873 | 2015-10-08 10:38:52 -0700 | [diff] [blame] | 602 | else if (has_expired(br, fdb)) |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 603 | return NUD_STALE; |
| 604 | else |
| 605 | return NUD_REACHABLE; |
| 606 | } |
| 607 | |
stephen hemminger | 31e8a49c | 2011-12-08 07:17:41 +0000 | [diff] [blame] | 608 | static int fdb_fill_info(struct sk_buff *skb, const struct net_bridge *br, |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 609 | const struct net_bridge_fdb_entry *fdb, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 610 | u32 portid, u32 seq, int type, unsigned int flags) |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 611 | { |
| 612 | unsigned long now = jiffies; |
| 613 | struct nda_cacheinfo ci; |
| 614 | struct nlmsghdr *nlh; |
| 615 | struct ndmsg *ndm; |
| 616 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 617 | nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags); |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 618 | if (nlh == NULL) |
| 619 | return -EMSGSIZE; |
| 620 | |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 621 | ndm = nlmsg_data(nlh); |
| 622 | ndm->ndm_family = AF_BRIDGE; |
| 623 | ndm->ndm_pad1 = 0; |
| 624 | ndm->ndm_pad2 = 0; |
Arkadi Sharshevsky | 9fe8bce | 2017-06-08 08:44:15 +0200 | [diff] [blame] | 625 | ndm->ndm_flags = 0; |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 626 | ndm->ndm_type = 0; |
stephen hemminger | 4359881 | 2011-12-08 07:17:49 +0000 | [diff] [blame] | 627 | ndm->ndm_ifindex = fdb->dst ? fdb->dst->dev->ifindex : br->dev->ifindex; |
Roopa Prabhu | 3741873 | 2015-10-08 10:38:52 -0700 | [diff] [blame] | 628 | ndm->ndm_state = fdb_to_nud(br, fdb); |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 629 | |
Arkadi Sharshevsky | 9fe8bce | 2017-06-08 08:44:15 +0200 | [diff] [blame] | 630 | if (fdb->offloaded) |
| 631 | ndm->ndm_flags |= NTF_OFFLOADED; |
| 632 | if (fdb->added_by_external_learn) |
| 633 | ndm->ndm_flags |= NTF_EXT_LEARNED; |
| 634 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 635 | if (nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->key.addr)) |
David S. Miller | 2eb812e | 2012-04-01 20:49:54 -0400 | [diff] [blame] | 636 | goto nla_put_failure; |
Roopa Prabhu | 41c389d | 2014-05-27 22:39:37 -0700 | [diff] [blame] | 637 | if (nla_put_u32(skb, NDA_MASTER, br->dev->ifindex)) |
| 638 | goto nla_put_failure; |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 639 | ci.ndm_used = jiffies_to_clock_t(now - fdb->used); |
| 640 | ci.ndm_confirmed = 0; |
| 641 | ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated); |
| 642 | ci.ndm_refcnt = 0; |
David S. Miller | 2eb812e | 2012-04-01 20:49:54 -0400 | [diff] [blame] | 643 | if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci)) |
| 644 | goto nla_put_failure; |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 645 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 646 | if (fdb->key.vlan_id && nla_put(skb, NDA_VLAN, sizeof(u16), |
| 647 | &fdb->key.vlan_id)) |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 648 | goto nla_put_failure; |
| 649 | |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 650 | nlmsg_end(skb, nlh); |
| 651 | return 0; |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 652 | |
| 653 | nla_put_failure: |
| 654 | nlmsg_cancel(skb, nlh); |
| 655 | return -EMSGSIZE; |
| 656 | } |
| 657 | |
| 658 | static inline size_t fdb_nlmsg_size(void) |
| 659 | { |
| 660 | return NLMSG_ALIGN(sizeof(struct ndmsg)) |
| 661 | + nla_total_size(ETH_ALEN) /* NDA_LLADDR */ |
Roopa Prabhu | 41c389d | 2014-05-27 22:39:37 -0700 | [diff] [blame] | 662 | + nla_total_size(sizeof(u32)) /* NDA_MASTER */ |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 663 | + nla_total_size(sizeof(u16)) /* NDA_VLAN */ |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 664 | + nla_total_size(sizeof(struct nda_cacheinfo)); |
| 665 | } |
| 666 | |
stephen hemminger | 31e8a49c | 2011-12-08 07:17:41 +0000 | [diff] [blame] | 667 | static void fdb_notify(struct net_bridge *br, |
| 668 | const struct net_bridge_fdb_entry *fdb, int type) |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 669 | { |
stephen hemminger | 31e8a49c | 2011-12-08 07:17:41 +0000 | [diff] [blame] | 670 | struct net *net = dev_net(br->dev); |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 671 | struct sk_buff *skb; |
| 672 | int err = -ENOBUFS; |
| 673 | |
Arkadi Sharshevsky | 6b26b51 | 2017-06-08 08:44:14 +0200 | [diff] [blame] | 674 | br_switchdev_fdb_notify(fdb, type); |
| 675 | |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 676 | skb = nlmsg_new(fdb_nlmsg_size(), GFP_ATOMIC); |
| 677 | if (skb == NULL) |
| 678 | goto errout; |
| 679 | |
stephen hemminger | 31e8a49c | 2011-12-08 07:17:41 +0000 | [diff] [blame] | 680 | err = fdb_fill_info(skb, br, fdb, 0, 0, type, 0); |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 681 | if (err < 0) { |
| 682 | /* -EMSGSIZE implies BUG in fdb_nlmsg_size() */ |
| 683 | WARN_ON(err == -EMSGSIZE); |
| 684 | kfree_skb(skb); |
| 685 | goto errout; |
| 686 | } |
| 687 | rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC); |
| 688 | return; |
| 689 | errout: |
tanxiaojun | 87e823b | 2013-12-19 13:28:10 +0800 | [diff] [blame] | 690 | rtnl_set_sk_err(net, RTNLGRP_NEIGH, err); |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | /* Dump information about entries, in response to GETNEIGH */ |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 694 | int br_fdb_dump(struct sk_buff *skb, |
| 695 | struct netlink_callback *cb, |
| 696 | struct net_device *dev, |
Jamal Hadi Salim | 5d5eacb | 2014-07-10 07:01:58 -0400 | [diff] [blame] | 697 | struct net_device *filter_dev, |
Roopa Prabhu | d297653 | 2016-08-30 21:56:45 -0700 | [diff] [blame] | 698 | int *idx) |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 699 | { |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 700 | struct net_bridge *br = netdev_priv(dev); |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 701 | struct net_bridge_fdb_entry *f; |
Roopa Prabhu | d297653 | 2016-08-30 21:56:45 -0700 | [diff] [blame] | 702 | int err = 0; |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 703 | |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 704 | if (!(dev->priv_flags & IFF_EBRIDGE)) |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 705 | return err; |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 706 | |
Roopa Prabhu | d297653 | 2016-08-30 21:56:45 -0700 | [diff] [blame] | 707 | if (!filter_dev) { |
| 708 | err = ndo_dflt_fdb_dump(skb, cb, dev, NULL, idx); |
| 709 | if (err < 0) |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 710 | return err; |
Roopa Prabhu | d297653 | 2016-08-30 21:56:45 -0700 | [diff] [blame] | 711 | } |
Hubert Sokolowski | 6cb6974 | 2015-01-05 17:29:21 +0000 | [diff] [blame] | 712 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 713 | rcu_read_lock(); |
| 714 | hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) { |
| 715 | if (*idx < cb->args[2]) |
| 716 | goto skip; |
| 717 | if (filter_dev && (!f->dst || f->dst->dev != filter_dev)) { |
| 718 | if (filter_dev != dev) |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 719 | goto skip; |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 720 | /* !f->dst is a special case for bridge |
| 721 | * It means the MAC belongs to the bridge |
| 722 | * Therefore need a little more filtering |
| 723 | * we only want to dump the !f->dst case |
| 724 | */ |
| 725 | if (f->dst) |
Hubert Sokolowski | 6cb6974 | 2015-01-05 17:29:21 +0000 | [diff] [blame] | 726 | goto skip; |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 727 | } |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 728 | if (!filter_dev && f->dst) |
| 729 | goto skip; |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 730 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 731 | err = fdb_fill_info(skb, br, f, |
| 732 | NETLINK_CB(cb->skb).portid, |
| 733 | cb->nlh->nlmsg_seq, |
| 734 | RTM_NEWNEIGH, |
| 735 | NLM_F_MULTI); |
| 736 | if (err < 0) |
| 737 | break; |
| 738 | skip: |
| 739 | *idx += 1; |
| 740 | } |
| 741 | rcu_read_unlock(); |
| 742 | |
Roopa Prabhu | d297653 | 2016-08-30 21:56:45 -0700 | [diff] [blame] | 743 | return err; |
stephen hemminger | b078f0d | 2011-04-04 14:03:30 +0000 | [diff] [blame] | 744 | } |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 745 | |
stephen hemminger | 292d139 | 2011-11-09 18:30:08 +0000 | [diff] [blame] | 746 | /* Update (create or replace) forwarding database entry */ |
Toshiaki Makita | 7bb90c3 | 2016-08-04 11:11:19 +0900 | [diff] [blame] | 747 | static int fdb_add_entry(struct net_bridge *br, struct net_bridge_port *source, |
| 748 | const __u8 *addr, __u16 state, __u16 flags, __u16 vid) |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 749 | { |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 750 | struct net_bridge_fdb_entry *fdb; |
roopa | b0a397f | 2013-04-22 12:56:49 +0000 | [diff] [blame] | 751 | bool modified = false; |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 752 | |
Wilson Kok | eb8d7ba | 2015-05-25 06:39:31 -0700 | [diff] [blame] | 753 | /* If the port cannot learn allow only local and static entries */ |
Toshiaki Makita | 7bb90c3 | 2016-08-04 11:11:19 +0900 | [diff] [blame] | 754 | if (source && !(state & NUD_PERMANENT) && !(state & NUD_NOARP) && |
Wilson Kok | eb8d7ba | 2015-05-25 06:39:31 -0700 | [diff] [blame] | 755 | !(source->state == BR_STATE_LEARNING || |
| 756 | source->state == BR_STATE_FORWARDING)) |
| 757 | return -EPERM; |
| 758 | |
Toshiaki Makita | 7bb90c3 | 2016-08-04 11:11:19 +0900 | [diff] [blame] | 759 | if (!source && !(state & NUD_PERMANENT)) { |
| 760 | pr_info("bridge: RTM_NEWNEIGH %s without NUD_PERMANENT\n", |
| 761 | br->dev->name); |
| 762 | return -EINVAL; |
| 763 | } |
| 764 | |
Nikolay Aleksandrov | bfd0aea | 2017-02-13 14:59:09 +0100 | [diff] [blame] | 765 | fdb = br_fdb_find(br, addr, vid); |
stephen hemminger | 64af1ba | 2011-09-30 14:37:27 +0000 | [diff] [blame] | 766 | if (fdb == NULL) { |
| 767 | if (!(flags & NLM_F_CREATE)) |
| 768 | return -ENOENT; |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 769 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 770 | fdb = fdb_create(br, source, addr, vid, 0, 0); |
stephen hemminger | 64af1ba | 2011-09-30 14:37:27 +0000 | [diff] [blame] | 771 | if (!fdb) |
| 772 | return -ENOMEM; |
roopa | b0a397f | 2013-04-22 12:56:49 +0000 | [diff] [blame] | 773 | |
| 774 | modified = true; |
stephen hemminger | 64af1ba | 2011-09-30 14:37:27 +0000 | [diff] [blame] | 775 | } else { |
| 776 | if (flags & NLM_F_EXCL) |
| 777 | return -EEXIST; |
roopa | b0a397f | 2013-04-22 12:56:49 +0000 | [diff] [blame] | 778 | |
| 779 | if (fdb->dst != source) { |
| 780 | fdb->dst = source; |
| 781 | modified = true; |
| 782 | } |
stephen hemminger | 64af1ba | 2011-09-30 14:37:27 +0000 | [diff] [blame] | 783 | } |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 784 | |
Roopa Prabhu | 3741873 | 2015-10-08 10:38:52 -0700 | [diff] [blame] | 785 | if (fdb_to_nud(br, fdb) != state) { |
Vlad Yasevich | 145beee | 2014-05-16 09:59:19 -0400 | [diff] [blame] | 786 | if (state & NUD_PERMANENT) { |
| 787 | fdb->is_local = 1; |
| 788 | if (!fdb->is_static) { |
| 789 | fdb->is_static = 1; |
Jiri Pirko | 020ec6b | 2014-11-28 14:34:12 +0100 | [diff] [blame] | 790 | fdb_add_hw_addr(br, addr); |
Vlad Yasevich | 145beee | 2014-05-16 09:59:19 -0400 | [diff] [blame] | 791 | } |
| 792 | } else if (state & NUD_NOARP) { |
stephen hemminger | 292d139 | 2011-11-09 18:30:08 +0000 | [diff] [blame] | 793 | fdb->is_local = 0; |
Vlad Yasevich | 145beee | 2014-05-16 09:59:19 -0400 | [diff] [blame] | 794 | if (!fdb->is_static) { |
| 795 | fdb->is_static = 1; |
Jiri Pirko | 020ec6b | 2014-11-28 14:34:12 +0100 | [diff] [blame] | 796 | fdb_add_hw_addr(br, addr); |
Vlad Yasevich | 145beee | 2014-05-16 09:59:19 -0400 | [diff] [blame] | 797 | } |
| 798 | } else { |
| 799 | fdb->is_local = 0; |
| 800 | if (fdb->is_static) { |
| 801 | fdb->is_static = 0; |
Jiri Pirko | 020ec6b | 2014-11-28 14:34:12 +0100 | [diff] [blame] | 802 | fdb_del_hw_addr(br, addr); |
Vlad Yasevich | 145beee | 2014-05-16 09:59:19 -0400 | [diff] [blame] | 803 | } |
| 804 | } |
stephen hemminger | 292d139 | 2011-11-09 18:30:08 +0000 | [diff] [blame] | 805 | |
roopa | b0a397f | 2013-04-22 12:56:49 +0000 | [diff] [blame] | 806 | modified = true; |
| 807 | } |
Toshiaki Makita | a5642ab | 2014-02-07 16:48:18 +0900 | [diff] [blame] | 808 | fdb->added_by_user = 1; |
roopa | b0a397f | 2013-04-22 12:56:49 +0000 | [diff] [blame] | 809 | |
| 810 | fdb->used = jiffies; |
| 811 | if (modified) { |
| 812 | fdb->updated = jiffies; |
stephen hemminger | 31e8a49c | 2011-12-08 07:17:41 +0000 | [diff] [blame] | 813 | fdb_notify(br, fdb, RTM_NEWNEIGH); |
stephen hemminger | 292d139 | 2011-11-09 18:30:08 +0000 | [diff] [blame] | 814 | } |
| 815 | |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 816 | return 0; |
| 817 | } |
| 818 | |
Toshiaki Makita | 7bb90c3 | 2016-08-04 11:11:19 +0900 | [diff] [blame] | 819 | static int __br_fdb_add(struct ndmsg *ndm, struct net_bridge *br, |
| 820 | struct net_bridge_port *p, const unsigned char *addr, |
| 821 | u16 nlh_flags, u16 vid) |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 822 | { |
| 823 | int err = 0; |
| 824 | |
| 825 | if (ndm->ndm_flags & NTF_USE) { |
Toshiaki Makita | 7bb90c3 | 2016-08-04 11:11:19 +0900 | [diff] [blame] | 826 | if (!p) { |
| 827 | pr_info("bridge: RTM_NEWNEIGH %s with NTF_USE is not supported\n", |
| 828 | br->dev->name); |
| 829 | return -EINVAL; |
| 830 | } |
Nikolay Aleksandrov | c4c832f | 2015-06-06 06:49:00 -0700 | [diff] [blame] | 831 | local_bh_disable(); |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 832 | rcu_read_lock(); |
Toshiaki Makita | 7bb90c3 | 2016-08-04 11:11:19 +0900 | [diff] [blame] | 833 | br_fdb_update(br, p, addr, vid, true); |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 834 | rcu_read_unlock(); |
Nikolay Aleksandrov | c4c832f | 2015-06-06 06:49:00 -0700 | [diff] [blame] | 835 | local_bh_enable(); |
Nikolay Aleksandrov | eb100e0 | 2017-03-23 12:27:13 +0200 | [diff] [blame] | 836 | } else if (ndm->ndm_flags & NTF_EXT_LEARNED) { |
| 837 | err = br_fdb_external_learn_add(br, p, addr, vid); |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 838 | } else { |
Toshiaki Makita | 7bb90c3 | 2016-08-04 11:11:19 +0900 | [diff] [blame] | 839 | spin_lock_bh(&br->hash_lock); |
| 840 | err = fdb_add_entry(br, p, addr, ndm->ndm_state, |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 841 | nlh_flags, vid); |
Toshiaki Makita | 7bb90c3 | 2016-08-04 11:11:19 +0900 | [diff] [blame] | 842 | spin_unlock_bh(&br->hash_lock); |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | return err; |
| 846 | } |
| 847 | |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 848 | /* Add new permanent fdb entry with RTM_NEWNEIGH */ |
stephen hemminger | edc7d57 | 2012-10-01 12:32:33 +0000 | [diff] [blame] | 849 | int br_fdb_add(struct ndmsg *ndm, struct nlattr *tb[], |
| 850 | struct net_device *dev, |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 851 | const unsigned char *addr, u16 vid, u16 nlh_flags) |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 852 | { |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 853 | struct net_bridge_vlan_group *vg; |
Roopa Prabhu | 3741873 | 2015-10-08 10:38:52 -0700 | [diff] [blame] | 854 | struct net_bridge_port *p = NULL; |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 855 | struct net_bridge_vlan *v; |
Roopa Prabhu | 3741873 | 2015-10-08 10:38:52 -0700 | [diff] [blame] | 856 | struct net_bridge *br = NULL; |
John Fastabend | 7716202 | 2012-04-15 06:43:56 +0000 | [diff] [blame] | 857 | int err = 0; |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 858 | |
Roopa Prabhu | b74fd30 | 2017-08-29 13:16:57 -0700 | [diff] [blame] | 859 | trace_br_fdb_add(ndm, dev, addr, vid, nlh_flags); |
| 860 | |
stephen hemminger | 292d139 | 2011-11-09 18:30:08 +0000 | [diff] [blame] | 861 | if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE))) { |
| 862 | pr_info("bridge: RTM_NEWNEIGH with invalid state %#x\n", ndm->ndm_state); |
| 863 | return -EINVAL; |
| 864 | } |
| 865 | |
Stephen Hemminger | 537f7f8 | 2013-06-25 09:34:36 -0700 | [diff] [blame] | 866 | if (is_zero_ether_addr(addr)) { |
| 867 | pr_info("bridge: RTM_NEWNEIGH with invalid ether address\n"); |
| 868 | return -EINVAL; |
| 869 | } |
| 870 | |
Roopa Prabhu | 3741873 | 2015-10-08 10:38:52 -0700 | [diff] [blame] | 871 | if (dev->priv_flags & IFF_EBRIDGE) { |
| 872 | br = netdev_priv(dev); |
| 873 | vg = br_vlan_group(br); |
| 874 | } else { |
| 875 | p = br_port_get_rtnl(dev); |
| 876 | if (!p) { |
| 877 | pr_info("bridge: RTM_NEWNEIGH %s not a bridge port\n", |
| 878 | dev->name); |
| 879 | return -EINVAL; |
| 880 | } |
Toshiaki Makita | 7bb90c3 | 2016-08-04 11:11:19 +0900 | [diff] [blame] | 881 | br = p->br; |
Roopa Prabhu | 3741873 | 2015-10-08 10:38:52 -0700 | [diff] [blame] | 882 | vg = nbp_vlan_group(p); |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 883 | } |
| 884 | |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 885 | if (vid) { |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 886 | v = br_vlan_find(vg, vid); |
Roopa Prabhu | 3741873 | 2015-10-08 10:38:52 -0700 | [diff] [blame] | 887 | if (!v || !br_vlan_should_use(v)) { |
| 888 | 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] | 889 | return -EINVAL; |
| 890 | } |
| 891 | |
| 892 | /* VID was specified, so use it. */ |
Toshiaki Makita | 7bb90c3 | 2016-08-04 11:11:19 +0900 | [diff] [blame] | 893 | err = __br_fdb_add(ndm, br, p, addr, nlh_flags, vid); |
stephen hemminger | 292d139 | 2011-11-09 18:30:08 +0000 | [diff] [blame] | 894 | } else { |
Toshiaki Makita | 7bb90c3 | 2016-08-04 11:11:19 +0900 | [diff] [blame] | 895 | err = __br_fdb_add(ndm, br, p, addr, nlh_flags, 0); |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 896 | if (err || !vg || !vg->num_vlans) |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 897 | goto out; |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 898 | |
| 899 | /* We have vlans configured on this port and user didn't |
| 900 | * specify a VLAN. To be nice, add/update entry for every |
| 901 | * vlan on this port. |
| 902 | */ |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 903 | list_for_each_entry(v, &vg->vlan_list, vlist) { |
Roopa Prabhu | 3741873 | 2015-10-08 10:38:52 -0700 | [diff] [blame] | 904 | if (!br_vlan_should_use(v)) |
| 905 | continue; |
Toshiaki Makita | 7bb90c3 | 2016-08-04 11:11:19 +0900 | [diff] [blame] | 906 | err = __br_fdb_add(ndm, br, p, addr, nlh_flags, v->vid); |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 907 | if (err) |
| 908 | goto out; |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 909 | } |
stephen hemminger | 292d139 | 2011-11-09 18:30:08 +0000 | [diff] [blame] | 910 | } |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 911 | |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 912 | out: |
| 913 | return err; |
| 914 | } |
| 915 | |
Nikolay Aleksandrov | 5019ab5 | 2017-02-13 14:59:11 +0100 | [diff] [blame] | 916 | static int fdb_delete_by_addr_and_port(struct net_bridge *br, |
| 917 | const struct net_bridge_port *p, |
Nikolay Aleksandrov | 8c86f96 | 2015-06-09 03:34:13 -0700 | [diff] [blame] | 918 | const u8 *addr, u16 vlan) |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 919 | { |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 920 | struct net_bridge_fdb_entry *fdb; |
| 921 | |
Nikolay Aleksandrov | bfd0aea | 2017-02-13 14:59:09 +0100 | [diff] [blame] | 922 | fdb = br_fdb_find(br, addr, vlan); |
Nikolay Aleksandrov | 8c86f96 | 2015-06-09 03:34:13 -0700 | [diff] [blame] | 923 | if (!fdb || fdb->dst != p) |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 924 | return -ENOENT; |
| 925 | |
| 926 | fdb_delete(br, fdb); |
Nikolay Aleksandrov | 5019ab5 | 2017-02-13 14:59:11 +0100 | [diff] [blame] | 927 | |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 928 | return 0; |
| 929 | } |
| 930 | |
Nikolay Aleksandrov | 5019ab5 | 2017-02-13 14:59:11 +0100 | [diff] [blame] | 931 | static int __br_fdb_delete(struct net_bridge *br, |
| 932 | const struct net_bridge_port *p, |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 933 | const unsigned char *addr, u16 vid) |
| 934 | { |
| 935 | int err; |
| 936 | |
Nikolay Aleksandrov | 5019ab5 | 2017-02-13 14:59:11 +0100 | [diff] [blame] | 937 | spin_lock_bh(&br->hash_lock); |
| 938 | err = fdb_delete_by_addr_and_port(br, p, addr, vid); |
| 939 | spin_unlock_bh(&br->hash_lock); |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 940 | |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 941 | return err; |
| 942 | } |
| 943 | |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 944 | /* Remove neighbor entry with RTM_DELNEIGH */ |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 945 | int br_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[], |
| 946 | struct net_device *dev, |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 947 | const unsigned char *addr, u16 vid) |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 948 | { |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 949 | struct net_bridge_vlan_group *vg; |
Roopa Prabhu | 3741873 | 2015-10-08 10:38:52 -0700 | [diff] [blame] | 950 | struct net_bridge_port *p = NULL; |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 951 | struct net_bridge_vlan *v; |
Nikolay Aleksandrov | 5019ab5 | 2017-02-13 14:59:11 +0100 | [diff] [blame] | 952 | struct net_bridge *br; |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 953 | int err; |
| 954 | |
Roopa Prabhu | 3741873 | 2015-10-08 10:38:52 -0700 | [diff] [blame] | 955 | if (dev->priv_flags & IFF_EBRIDGE) { |
| 956 | br = netdev_priv(dev); |
| 957 | vg = br_vlan_group(br); |
| 958 | } else { |
| 959 | p = br_port_get_rtnl(dev); |
| 960 | if (!p) { |
| 961 | pr_info("bridge: RTM_DELNEIGH %s not a bridge port\n", |
| 962 | dev->name); |
| 963 | return -EINVAL; |
| 964 | } |
| 965 | vg = nbp_vlan_group(p); |
Nikolay Aleksandrov | 5019ab5 | 2017-02-13 14:59:11 +0100 | [diff] [blame] | 966 | br = p->br; |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 967 | } |
| 968 | |
Jiri Pirko | f6f6424 | 2014-11-28 14:34:15 +0100 | [diff] [blame] | 969 | if (vid) { |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 970 | v = br_vlan_find(vg, vid); |
| 971 | if (!v) { |
Roopa Prabhu | 3741873 | 2015-10-08 10:38:52 -0700 | [diff] [blame] | 972 | 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] | 973 | return -EINVAL; |
| 974 | } |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 975 | |
Nikolay Aleksandrov | 5019ab5 | 2017-02-13 14:59:11 +0100 | [diff] [blame] | 976 | err = __br_fdb_delete(br, p, addr, vid); |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 977 | } else { |
Toshiaki Makita | 25d3b49 | 2015-02-09 20:16:17 +0900 | [diff] [blame] | 978 | err = -ENOENT; |
Nikolay Aleksandrov | 5019ab5 | 2017-02-13 14:59:11 +0100 | [diff] [blame] | 979 | err &= __br_fdb_delete(br, p, addr, 0); |
Nikolay Aleksandrov | 2594e906 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 980 | if (!vg || !vg->num_vlans) |
Nikolay Aleksandrov | 5019ab5 | 2017-02-13 14:59:11 +0100 | [diff] [blame] | 981 | return err; |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 982 | |
Roopa Prabhu | 3741873 | 2015-10-08 10:38:52 -0700 | [diff] [blame] | 983 | list_for_each_entry(v, &vg->vlan_list, vlist) { |
| 984 | if (!br_vlan_should_use(v)) |
| 985 | continue; |
Nikolay Aleksandrov | 5019ab5 | 2017-02-13 14:59:11 +0100 | [diff] [blame] | 986 | err &= __br_fdb_delete(br, p, addr, v->vid); |
Roopa Prabhu | 3741873 | 2015-10-08 10:38:52 -0700 | [diff] [blame] | 987 | } |
Vlad Yasevich | 1690be6 | 2013-02-13 12:00:18 +0000 | [diff] [blame] | 988 | } |
Nikolay Aleksandrov | 5019ab5 | 2017-02-13 14:59:11 +0100 | [diff] [blame] | 989 | |
stephen hemminger | 36fd2b6 | 2011-04-04 14:03:31 +0000 | [diff] [blame] | 990 | return err; |
| 991 | } |
Vlad Yasevich | 8db24af | 2014-05-16 09:59:17 -0400 | [diff] [blame] | 992 | |
| 993 | int br_fdb_sync_static(struct net_bridge *br, struct net_bridge_port *p) |
| 994 | { |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 995 | struct net_bridge_fdb_entry *f, *tmp; |
Geert Uytterhoeven | 367dc65 | 2018-02-01 11:25:27 +0100 | [diff] [blame] | 996 | int err = 0; |
Vlad Yasevich | 8db24af | 2014-05-16 09:59:17 -0400 | [diff] [blame] | 997 | |
| 998 | ASSERT_RTNL(); |
| 999 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 1000 | /* the key here is that static entries change only under rtnl */ |
| 1001 | rcu_read_lock(); |
| 1002 | hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) { |
| 1003 | /* We only care for static entries */ |
| 1004 | if (!f->is_static) |
| 1005 | continue; |
| 1006 | err = dev_uc_add(p->dev, f->key.addr.addr); |
| 1007 | if (err) |
| 1008 | goto rollback; |
Vlad Yasevich | 8db24af | 2014-05-16 09:59:17 -0400 | [diff] [blame] | 1009 | } |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 1010 | done: |
| 1011 | rcu_read_unlock(); |
| 1012 | |
| 1013 | return err; |
Vlad Yasevich | 8db24af | 2014-05-16 09:59:17 -0400 | [diff] [blame] | 1014 | |
| 1015 | rollback: |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 1016 | hlist_for_each_entry_rcu(tmp, &br->fdb_list, fdb_node) { |
| 1017 | /* We only care for static entries */ |
| 1018 | if (!tmp->is_static) |
| 1019 | continue; |
| 1020 | if (tmp == f) |
| 1021 | break; |
| 1022 | dev_uc_del(p->dev, tmp->key.addr.addr); |
Vlad Yasevich | 8db24af | 2014-05-16 09:59:17 -0400 | [diff] [blame] | 1023 | } |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 1024 | |
| 1025 | goto done; |
Vlad Yasevich | 8db24af | 2014-05-16 09:59:17 -0400 | [diff] [blame] | 1026 | } |
| 1027 | |
| 1028 | void br_fdb_unsync_static(struct net_bridge *br, struct net_bridge_port *p) |
| 1029 | { |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 1030 | struct net_bridge_fdb_entry *f; |
Vlad Yasevich | 8db24af | 2014-05-16 09:59:17 -0400 | [diff] [blame] | 1031 | |
| 1032 | ASSERT_RTNL(); |
| 1033 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 1034 | rcu_read_lock(); |
| 1035 | hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) { |
| 1036 | /* We only care for static entries */ |
| 1037 | if (!f->is_static) |
| 1038 | continue; |
Vlad Yasevich | 8db24af | 2014-05-16 09:59:17 -0400 | [diff] [blame] | 1039 | |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 1040 | dev_uc_del(p->dev, f->key.addr.addr); |
Vlad Yasevich | 8db24af | 2014-05-16 09:59:17 -0400 | [diff] [blame] | 1041 | } |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 1042 | rcu_read_unlock(); |
Vlad Yasevich | 8db24af | 2014-05-16 09:59:17 -0400 | [diff] [blame] | 1043 | } |
Scott Feldman | cf6b8e1 | 2014-11-28 14:34:21 +0100 | [diff] [blame] | 1044 | |
Jiri Pirko | 3aeb661 | 2015-01-15 23:49:37 +0100 | [diff] [blame] | 1045 | int br_fdb_external_learn_add(struct net_bridge *br, struct net_bridge_port *p, |
Scott Feldman | cf6b8e1 | 2014-11-28 14:34:21 +0100 | [diff] [blame] | 1046 | const unsigned char *addr, u16 vid) |
| 1047 | { |
Scott Feldman | cf6b8e1 | 2014-11-28 14:34:21 +0100 | [diff] [blame] | 1048 | struct net_bridge_fdb_entry *fdb; |
Nikolay Aleksandrov | 7597b26 | 2017-07-03 15:14:59 -0700 | [diff] [blame] | 1049 | bool modified = false; |
Scott Feldman | cf6b8e1 | 2014-11-28 14:34:21 +0100 | [diff] [blame] | 1050 | int err = 0; |
| 1051 | |
Roopa Prabhu | b74fd30 | 2017-08-29 13:16:57 -0700 | [diff] [blame] | 1052 | trace_br_fdb_external_learn_add(br, p, addr, vid); |
| 1053 | |
Scott Feldman | cf6b8e1 | 2014-11-28 14:34:21 +0100 | [diff] [blame] | 1054 | spin_lock_bh(&br->hash_lock); |
| 1055 | |
Nikolay Aleksandrov | bfd0aea | 2017-02-13 14:59:09 +0100 | [diff] [blame] | 1056 | fdb = br_fdb_find(br, addr, vid); |
Scott Feldman | cf6b8e1 | 2014-11-28 14:34:21 +0100 | [diff] [blame] | 1057 | if (!fdb) { |
Nikolay Aleksandrov | eb79358 | 2017-12-12 16:02:50 +0200 | [diff] [blame] | 1058 | fdb = fdb_create(br, p, addr, vid, 0, 0); |
Scott Feldman | cf6b8e1 | 2014-11-28 14:34:21 +0100 | [diff] [blame] | 1059 | if (!fdb) { |
| 1060 | err = -ENOMEM; |
| 1061 | goto err_unlock; |
| 1062 | } |
| 1063 | fdb->added_by_external_learn = 1; |
| 1064 | fdb_notify(br, fdb, RTM_NEWNEIGH); |
Nikolay Aleksandrov | 7597b26 | 2017-07-03 15:14:59 -0700 | [diff] [blame] | 1065 | } else { |
Scott Feldman | cf6b8e1 | 2014-11-28 14:34:21 +0100 | [diff] [blame] | 1066 | fdb->updated = jiffies; |
Nikolay Aleksandrov | 7597b26 | 2017-07-03 15:14:59 -0700 | [diff] [blame] | 1067 | |
| 1068 | if (fdb->dst != p) { |
| 1069 | fdb->dst = p; |
| 1070 | modified = true; |
| 1071 | } |
| 1072 | |
| 1073 | if (fdb->added_by_external_learn) { |
| 1074 | /* Refresh entry */ |
| 1075 | fdb->used = jiffies; |
| 1076 | } else if (!fdb->added_by_user) { |
| 1077 | /* Take over SW learned entry */ |
| 1078 | fdb->added_by_external_learn = 1; |
| 1079 | modified = true; |
| 1080 | } |
| 1081 | |
| 1082 | if (modified) |
| 1083 | fdb_notify(br, fdb, RTM_NEWNEIGH); |
Scott Feldman | cf6b8e1 | 2014-11-28 14:34:21 +0100 | [diff] [blame] | 1084 | } |
| 1085 | |
| 1086 | err_unlock: |
| 1087 | spin_unlock_bh(&br->hash_lock); |
Scott Feldman | cf6b8e1 | 2014-11-28 14:34:21 +0100 | [diff] [blame] | 1088 | |
| 1089 | return err; |
| 1090 | } |
Scott Feldman | cf6b8e1 | 2014-11-28 14:34:21 +0100 | [diff] [blame] | 1091 | |
Jiri Pirko | 3aeb661 | 2015-01-15 23:49:37 +0100 | [diff] [blame] | 1092 | int br_fdb_external_learn_del(struct net_bridge *br, struct net_bridge_port *p, |
Scott Feldman | cf6b8e1 | 2014-11-28 14:34:21 +0100 | [diff] [blame] | 1093 | const unsigned char *addr, u16 vid) |
| 1094 | { |
Scott Feldman | cf6b8e1 | 2014-11-28 14:34:21 +0100 | [diff] [blame] | 1095 | struct net_bridge_fdb_entry *fdb; |
| 1096 | int err = 0; |
| 1097 | |
Scott Feldman | cf6b8e1 | 2014-11-28 14:34:21 +0100 | [diff] [blame] | 1098 | spin_lock_bh(&br->hash_lock); |
| 1099 | |
Nikolay Aleksandrov | bfd0aea | 2017-02-13 14:59:09 +0100 | [diff] [blame] | 1100 | fdb = br_fdb_find(br, addr, vid); |
Scott Feldman | cf6b8e1 | 2014-11-28 14:34:21 +0100 | [diff] [blame] | 1101 | if (fdb && fdb->added_by_external_learn) |
| 1102 | fdb_delete(br, fdb); |
| 1103 | else |
| 1104 | err = -ENOENT; |
| 1105 | |
| 1106 | spin_unlock_bh(&br->hash_lock); |
Scott Feldman | cf6b8e1 | 2014-11-28 14:34:21 +0100 | [diff] [blame] | 1107 | |
| 1108 | return err; |
| 1109 | } |
Arkadi Sharshevsky | 9fe8bce | 2017-06-08 08:44:15 +0200 | [diff] [blame] | 1110 | |
| 1111 | void br_fdb_offloaded_set(struct net_bridge *br, struct net_bridge_port *p, |
| 1112 | const unsigned char *addr, u16 vid) |
| 1113 | { |
| 1114 | struct net_bridge_fdb_entry *fdb; |
| 1115 | |
| 1116 | spin_lock_bh(&br->hash_lock); |
| 1117 | |
| 1118 | fdb = br_fdb_find(br, addr, vid); |
| 1119 | if (fdb) |
| 1120 | fdb->offloaded = 1; |
| 1121 | |
| 1122 | spin_unlock_bh(&br->hash_lock); |
| 1123 | } |