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