blob: 474d36f93342ea5c3a7cfcb210d3520b2fe78c6c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Forwarding database
3 * Linux ethernet bridge
4 *
5 * Authors:
6 * Lennert Buytenhek <buytenh@gnu.org>
7 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * 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-Huu82524742008-05-12 21:21:05 +020016#include <linux/rculist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#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 Hemminger3f890922007-03-21 13:42:33 -070022#include <linux/random.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Arun Sharma600634972011-07-26 16:09:06 -070024#include <linux/atomic.h>
Stephen Hemminger3f890922007-03-21 13:42:33 -070025#include <asm/unaligned.h>
Vlad Yasevich2ba071e2013-02-13 12:00:16 +000026#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "br_private.h"
28
Christoph Lametere18b8902006-12-06 20:33:20 -080029static struct kmem_cache *br_fdb_cache __read_mostly;
Toshiaki Makita424bb9c2014-02-07 16:48:25 +090030static struct net_bridge_fdb_entry *fdb_find(struct hlist_head *head,
31 const unsigned char *addr,
32 __u16 vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +000034 const unsigned char *addr, u16 vid);
stephen hemminger31e8a492011-12-08 07:17:41 +000035static void fdb_notify(struct net_bridge *br,
36 const struct net_bridge_fdb_entry *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Stephen Hemminger3f890922007-03-21 13:42:33 -070038static u32 fdb_salt __read_mostly;
39
Akinobu Mita87a596e2007-04-07 18:57:07 +090040int __init br_fdb_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
42 br_fdb_cache = kmem_cache_create("bridge_fdb_cache",
43 sizeof(struct net_bridge_fdb_entry),
44 0,
Paul Mundt20c2df82007-07-20 10:11:58 +090045 SLAB_HWCACHE_ALIGN, NULL);
Akinobu Mita87a596e2007-04-07 18:57:07 +090046 if (!br_fdb_cache)
47 return -ENOMEM;
48
Stephen Hemminger3f890922007-03-21 13:42:33 -070049 get_random_bytes(&fdb_salt, sizeof(fdb_salt));
Akinobu Mita87a596e2007-04-07 18:57:07 +090050 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051}
52
Andrew Morton73afc902007-12-05 21:35:23 -080053void br_fdb_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
55 kmem_cache_destroy(br_fdb_cache);
56}
57
58
59/* if topology_changing then use forward_delay (default 15 sec)
60 * otherwise keep longer (default 5 minutes)
61 */
Stephen Hemminger3f890922007-03-21 13:42:33 -070062static inline unsigned long hold_time(const struct net_bridge *br)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
64 return br->topology_change ? br->forward_delay : br->ageing_time;
65}
66
Stephen Hemminger3f890922007-03-21 13:42:33 -070067static inline int has_expired(const struct net_bridge *br,
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 const struct net_bridge_fdb_entry *fdb)
69{
Joe Perchesf64f9e72009-11-29 16:55:45 -080070 return !fdb->is_static &&
stephen hemminger7cd88612011-04-04 14:03:28 +000071 time_before_eq(fdb->updated + hold_time(br), jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072}
73
Vlad Yasevich2ba071e2013-02-13 12:00:16 +000074static inline int br_mac_hash(const unsigned char *mac, __u16 vid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
Vlad Yasevich2ba071e2013-02-13 12:00:16 +000076 /* use 1 byte of OUI and 3 bytes of NIC */
Stephen Hemminger3f890922007-03-21 13:42:33 -070077 u32 key = get_unaligned((u32 *)(mac + 2));
Vlad Yasevich2ba071e2013-02-13 12:00:16 +000078 return jhash_2words(key, vid, fdb_salt) & (BR_HASH_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079}
80
Michał Mirosławda678292009-06-05 05:35:28 +000081static void fdb_rcu_free(struct rcu_head *head)
82{
83 struct net_bridge_fdb_entry *ent
84 = container_of(head, struct net_bridge_fdb_entry, rcu);
85 kmem_cache_free(br_fdb_cache, ent);
86}
87
stephen hemminger31e8a492011-12-08 07:17:41 +000088static void fdb_delete(struct net_bridge *br, struct net_bridge_fdb_entry *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
90 hlist_del_rcu(&f->hlist);
stephen hemminger31e8a492011-12-08 07:17:41 +000091 fdb_notify(br, f, RTM_DELNEIGH);
Michał Mirosławda678292009-06-05 05:35:28 +000092 call_rcu(&f->rcu, fdb_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093}
94
Toshiaki Makita960b5892014-02-07 16:48:23 +090095/* Delete a local entry if no other port had the same address. */
96static void fdb_delete_local(struct net_bridge *br,
97 const struct net_bridge_port *p,
98 struct net_bridge_fdb_entry *f)
99{
100 const unsigned char *addr = f->addr.addr;
101 u16 vid = f->vlan_id;
102 struct net_bridge_port *op;
103
104 /* Maybe another port has same hw addr? */
105 list_for_each_entry(op, &br->port_list, list) {
106 if (op != p && ether_addr_equal(op->dev->dev_addr, addr) &&
107 (!vid || nbp_vlan_find(op, vid))) {
108 f->dst = op;
Toshiaki Makitaa778e6d2014-02-07 16:48:24 +0900109 f->added_by_user = 0;
Toshiaki Makita960b5892014-02-07 16:48:23 +0900110 return;
111 }
112 }
113
114 /* Maybe bridge device has same hw addr? */
115 if (p && ether_addr_equal(br->dev->dev_addr, addr) &&
116 (!vid || br_vlan_find(br, vid))) {
117 f->dst = NULL;
Toshiaki Makitaa778e6d2014-02-07 16:48:24 +0900118 f->added_by_user = 0;
Toshiaki Makita960b5892014-02-07 16:48:23 +0900119 return;
120 }
121
122 fdb_delete(br, f);
123}
124
Toshiaki Makita424bb9c2014-02-07 16:48:25 +0900125void br_fdb_find_delete_local(struct net_bridge *br,
126 const struct net_bridge_port *p,
127 const unsigned char *addr, u16 vid)
128{
129 struct hlist_head *head = &br->hash[br_mac_hash(addr, vid)];
130 struct net_bridge_fdb_entry *f;
131
132 spin_lock_bh(&br->hash_lock);
133 f = fdb_find(head, addr, vid);
134 if (f && f->is_local && !f->added_by_user && f->dst == p)
135 fdb_delete_local(br, p, f);
136 spin_unlock_bh(&br->hash_lock);
137}
138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139void br_fdb_changeaddr(struct net_bridge_port *p, const unsigned char *newaddr)
140{
141 struct net_bridge *br = p->br;
Toshiaki Makita28368822014-02-07 16:48:19 +0900142 struct net_port_vlans *pv = nbp_get_vlan_info(p);
143 bool no_vlan = !pv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 int i;
Toshiaki Makita28368822014-02-07 16:48:19 +0900145 u16 vid;
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 spin_lock_bh(&br->hash_lock);
148
149 /* Search all chains since old address/hash is unknown */
150 for (i = 0; i < BR_HASH_SIZE; i++) {
151 struct hlist_node *h;
152 hlist_for_each(h, &br->hash[i]) {
153 struct net_bridge_fdb_entry *f;
154
155 f = hlist_entry(h, struct net_bridge_fdb_entry, hlist);
Toshiaki Makitaa5642ab2014-02-07 16:48:18 +0900156 if (f->dst == p && f->is_local && !f->added_by_user) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 /* delete old one */
Toshiaki Makita960b5892014-02-07 16:48:23 +0900158 fdb_delete_local(br, p, f);
159
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000160 /* if this port has no vlan information
161 * configured, we can safely be done at
162 * this point.
163 */
164 if (no_vlan)
Toshiaki Makita28368822014-02-07 16:48:19 +0900165 goto insert;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 }
167 }
168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Toshiaki Makita28368822014-02-07 16:48:19 +0900170insert:
171 /* insert new address, may fail if invalid address or dup. */
172 fdb_insert(br, p, newaddr, 0);
173
174 if (no_vlan)
175 goto done;
176
177 /* Now add entries for every VLAN configured on the port.
178 * This function runs under RTNL so the bitmap will not change
179 * from under us.
180 */
181 for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID)
182 fdb_insert(br, p, newaddr, vid);
183
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000184done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 spin_unlock_bh(&br->hash_lock);
186}
187
stephen hemminger43598812011-12-08 07:17:49 +0000188void br_fdb_change_mac_address(struct net_bridge *br, const u8 *newaddr)
189{
190 struct net_bridge_fdb_entry *f;
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000191 struct net_port_vlans *pv;
192 u16 vid = 0;
stephen hemminger43598812011-12-08 07:17:49 +0000193
Toshiaki Makitaac4c8862014-02-07 16:48:26 +0900194 spin_lock_bh(&br->hash_lock);
195
stephen hemminger43598812011-12-08 07:17:49 +0000196 /* If old entry was unassociated with any port, then delete it. */
Vlad Yasevich2ba071e2013-02-13 12:00:16 +0000197 f = __br_fdb_get(br, br->dev->dev_addr, 0);
stephen hemminger43598812011-12-08 07:17:49 +0000198 if (f && f->is_local && !f->dst)
Toshiaki Makita960b5892014-02-07 16:48:23 +0900199 fdb_delete_local(br, NULL, f);
stephen hemminger43598812011-12-08 07:17:49 +0000200
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000201 fdb_insert(br, NULL, newaddr, 0);
202
203 /* Now remove and add entries for every VLAN configured on the
204 * bridge. This function runs under RTNL so the bitmap will not
205 * change from under us.
206 */
207 pv = br_get_vlan_info(br);
208 if (!pv)
Toshiaki Makitaac4c8862014-02-07 16:48:26 +0900209 goto out;
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000210
Toshiaki Makitaef40b7e2013-08-20 17:10:18 +0900211 for_each_set_bit_from(vid, pv->vlan_bitmap, VLAN_N_VID) {
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000212 f = __br_fdb_get(br, br->dev->dev_addr, vid);
213 if (f && f->is_local && !f->dst)
Toshiaki Makita960b5892014-02-07 16:48:23 +0900214 fdb_delete_local(br, NULL, f);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000215 fdb_insert(br, NULL, newaddr, vid);
216 }
Toshiaki Makitaac4c8862014-02-07 16:48:26 +0900217out:
218 spin_unlock_bh(&br->hash_lock);
stephen hemminger43598812011-12-08 07:17:49 +0000219}
220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221void br_fdb_cleanup(unsigned long _data)
222{
223 struct net_bridge *br = (struct net_bridge *)_data;
224 unsigned long delay = hold_time(br);
stephen hemminger25442e02010-06-15 06:14:12 +0000225 unsigned long next_timer = jiffies + br->ageing_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 int i;
227
Eric Dumazet27a42932012-01-16 04:35:50 +0000228 spin_lock(&br->hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 for (i = 0; i < BR_HASH_SIZE; i++) {
230 struct net_bridge_fdb_entry *f;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800231 struct hlist_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Sasha Levinb67bfe02013-02-27 17:06:00 -0800233 hlist_for_each_entry_safe(f, n, &br->hash[i], hlist) {
Baruch Even071f7722007-05-31 01:20:45 -0700234 unsigned long this_timer;
235 if (f->is_static)
236 continue;
stephen hemminger7cd88612011-04-04 14:03:28 +0000237 this_timer = f->updated + delay;
Baruch Even071f7722007-05-31 01:20:45 -0700238 if (time_before_eq(this_timer, jiffies))
stephen hemminger31e8a492011-12-08 07:17:41 +0000239 fdb_delete(br, f);
Fabio Checconi2bec0082008-03-20 15:54:58 -0700240 else if (time_before(this_timer, next_timer))
Baruch Even071f7722007-05-31 01:20:45 -0700241 next_timer = this_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 }
243 }
Eric Dumazet27a42932012-01-16 04:35:50 +0000244 spin_unlock(&br->hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
stephen hemminger25442e02010-06-15 06:14:12 +0000246 mod_timer(&br->gc_timer, round_jiffies_up(next_timer));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247}
248
Stephen Hemminger9cf63742007-04-09 12:57:54 -0700249/* Completely flush all dynamic entries in forwarding database.*/
250void br_fdb_flush(struct net_bridge *br)
251{
252 int i;
Stephen Hemminger1a620692006-10-12 14:45:38 -0700253
Stephen Hemminger9cf63742007-04-09 12:57:54 -0700254 spin_lock_bh(&br->hash_lock);
255 for (i = 0; i < BR_HASH_SIZE; i++) {
256 struct net_bridge_fdb_entry *f;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800257 struct hlist_node *n;
258 hlist_for_each_entry_safe(f, n, &br->hash[i], hlist) {
Stephen Hemminger9cf63742007-04-09 12:57:54 -0700259 if (!f->is_static)
stephen hemminger31e8a492011-12-08 07:17:41 +0000260 fdb_delete(br, f);
Stephen Hemminger9cf63742007-04-09 12:57:54 -0700261 }
262 }
263 spin_unlock_bh(&br->hash_lock);
264}
265
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300266/* Flush all entries referring to a specific port.
Stephen Hemminger9cf63742007-04-09 12:57:54 -0700267 * if do_all is set also flush static entries
268 */
Stephen Hemminger1a620692006-10-12 14:45:38 -0700269void br_fdb_delete_by_port(struct net_bridge *br,
270 const struct net_bridge_port *p,
271 int do_all)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
273 int i;
274
275 spin_lock_bh(&br->hash_lock);
276 for (i = 0; i < BR_HASH_SIZE; i++) {
277 struct hlist_node *h, *g;
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 hlist_for_each_safe(h, g, &br->hash[i]) {
280 struct net_bridge_fdb_entry *f
281 = hlist_entry(h, struct net_bridge_fdb_entry, hlist);
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900282 if (f->dst != p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 continue;
284
Stephen Hemminger1a620692006-10-12 14:45:38 -0700285 if (f->is_static && !do_all)
286 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Toshiaki Makitaa778e6d2014-02-07 16:48:24 +0900288 if (f->is_local)
289 fdb_delete_local(br, p, f);
290 else
291 fdb_delete(br, f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 }
293 }
294 spin_unlock_bh(&br->hash_lock);
295}
296
stephen hemmingereeaf61d2010-07-27 08:26:30 +0000297/* No locking or refcounting, assumes caller has rcu_read_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298struct net_bridge_fdb_entry *__br_fdb_get(struct net_bridge *br,
Vlad Yasevich2ba071e2013-02-13 12:00:16 +0000299 const unsigned char *addr,
300 __u16 vid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 struct net_bridge_fdb_entry *fdb;
303
Sasha Levinb67bfe02013-02-27 17:06:00 -0800304 hlist_for_each_entry_rcu(fdb,
Vlad Yasevich2ba071e2013-02-13 12:00:16 +0000305 &br->hash[br_mac_hash(addr, vid)], hlist) {
306 if (ether_addr_equal(fdb->addr.addr, addr) &&
307 fdb->vlan_id == vid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 if (unlikely(has_expired(br, fdb)))
309 break;
310 return fdb;
311 }
312 }
313
314 return NULL;
315}
316
Igor Maraviće6373c42011-12-12 02:58:25 +0000317#if IS_ENABLED(CONFIG_ATM_LANE)
Michał Mirosławda678292009-06-05 05:35:28 +0000318/* Interface used by ATM LANE hook to test
319 * if an addr is on some other bridge port */
320int br_fdb_test_addr(struct net_device *dev, unsigned char *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
322 struct net_bridge_fdb_entry *fdb;
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000323 struct net_bridge_port *port;
Michał Mirosławda678292009-06-05 05:35:28 +0000324 int ret;
325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 rcu_read_lock();
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000327 port = br_port_get_rcu(dev);
328 if (!port)
329 ret = 0;
330 else {
Vlad Yasevich2ba071e2013-02-13 12:00:16 +0000331 fdb = __br_fdb_get(port->br, addr, 0);
stephen hemminger43598812011-12-08 07:17:49 +0000332 ret = fdb && fdb->dst && fdb->dst->dev != dev &&
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000333 fdb->dst->state == BR_STATE_FORWARDING;
334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Michał Mirosławda678292009-06-05 05:35:28 +0000337 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
Michał Mirosławda678292009-06-05 05:35:28 +0000339#endif /* CONFIG_ATM_LANE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341/*
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900342 * Fill buffer with forwarding table records in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 * the API format.
344 */
345int br_fdb_fillbuf(struct net_bridge *br, void *buf,
346 unsigned long maxnum, unsigned long skip)
347{
348 struct __fdb_entry *fe = buf;
349 int i, num = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 struct net_bridge_fdb_entry *f;
351
352 memset(buf, 0, maxnum*sizeof(struct __fdb_entry));
353
354 rcu_read_lock();
355 for (i = 0; i < BR_HASH_SIZE; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800356 hlist_for_each_entry_rcu(f, &br->hash[i], hlist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 if (num >= maxnum)
358 goto out;
359
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900360 if (has_expired(br, f))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 continue;
362
stephen hemminger43598812011-12-08 07:17:49 +0000363 /* ignore pseudo entry for local MAC address */
364 if (!f->dst)
365 continue;
366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 if (skip) {
368 --skip;
369 continue;
370 }
371
372 /* convert from internal format to API */
373 memcpy(fe->mac_addr, f->addr.addr, ETH_ALEN);
Stephen Hemmingerae4f8fc2008-05-02 16:53:33 -0700374
375 /* due to ABI compat need to split into hi/lo */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 fe->port_no = f->dst->port_no;
Stephen Hemmingerae4f8fc2008-05-02 16:53:33 -0700377 fe->port_hi = f->dst->port_no >> 8;
378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 fe->is_local = f->is_local;
380 if (!f->is_static)
Eric Dumazeta399a802012-08-08 21:13:53 +0000381 fe->ageing_timer_value = jiffies_delta_to_clock_t(jiffies - f->updated);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 ++fe;
383 ++num;
384 }
385 }
386
387 out:
388 rcu_read_unlock();
389
390 return num;
391}
392
stephen hemminger664de482011-04-04 14:03:29 +0000393static struct net_bridge_fdb_entry *fdb_find(struct hlist_head *head,
Vlad Yasevich2ba071e2013-02-13 12:00:16 +0000394 const unsigned char *addr,
395 __u16 vid)
stephen hemminger664de482011-04-04 14:03:29 +0000396{
stephen hemminger664de482011-04-04 14:03:29 +0000397 struct net_bridge_fdb_entry *fdb;
398
Sasha Levinb67bfe02013-02-27 17:06:00 -0800399 hlist_for_each_entry(fdb, head, hlist) {
Vlad Yasevich2ba071e2013-02-13 12:00:16 +0000400 if (ether_addr_equal(fdb->addr.addr, addr) &&
401 fdb->vlan_id == vid)
stephen hemminger664de482011-04-04 14:03:29 +0000402 return fdb;
403 }
404 return NULL;
405}
406
407static struct net_bridge_fdb_entry *fdb_find_rcu(struct hlist_head *head,
Vlad Yasevich2ba071e2013-02-13 12:00:16 +0000408 const unsigned char *addr,
409 __u16 vid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 struct net_bridge_fdb_entry *fdb;
412
Sasha Levinb67bfe02013-02-27 17:06:00 -0800413 hlist_for_each_entry_rcu(fdb, head, hlist) {
Vlad Yasevich2ba071e2013-02-13 12:00:16 +0000414 if (ether_addr_equal(fdb->addr.addr, addr) &&
415 fdb->vlan_id == vid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 return fdb;
417 }
418 return NULL;
419}
420
421static struct net_bridge_fdb_entry *fdb_create(struct hlist_head *head,
422 struct net_bridge_port *source,
Vlad Yasevich2ba071e2013-02-13 12:00:16 +0000423 const unsigned char *addr,
424 __u16 vid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
426 struct net_bridge_fdb_entry *fdb;
427
428 fdb = kmem_cache_alloc(br_fdb_cache, GFP_ATOMIC);
429 if (fdb) {
430 memcpy(fdb->addr.addr, addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 fdb->dst = source;
Vlad Yasevich2ba071e2013-02-13 12:00:16 +0000432 fdb->vlan_id = vid;
stephen hemminger03e9b642011-04-04 14:03:27 +0000433 fdb->is_local = 0;
434 fdb->is_static = 0;
Toshiaki Makitaa5642ab2014-02-07 16:48:18 +0900435 fdb->added_by_user = 0;
stephen hemminger7cd88612011-04-04 14:03:28 +0000436 fdb->updated = fdb->used = jiffies;
Pavel Emelyanov1158f762011-02-04 13:02:36 -0800437 hlist_add_head_rcu(&fdb->hlist, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 }
439 return fdb;
440}
441
442static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000443 const unsigned char *addr, u16 vid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000445 struct hlist_head *head = &br->hash[br_mac_hash(addr, vid)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 struct net_bridge_fdb_entry *fdb;
447
448 if (!is_valid_ether_addr(addr))
449 return -EINVAL;
450
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000451 fdb = fdb_find(head, addr, vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 if (fdb) {
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900453 /* it is okay to have multiple ports with same
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 * address, just use the first one.
455 */
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900456 if (fdb->is_local)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 return 0;
stephen hemminger28a16c92010-05-10 09:31:09 +0000458 br_warn(br, "adding interface %s with same address "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 "as a received packet\n",
Hong zhi guo9b469222013-03-23 02:27:50 +0000460 source ? source->dev->name : br->dev->name);
stephen hemminger31e8a492011-12-08 07:17:41 +0000461 fdb_delete(br, fdb);
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000464 fdb = fdb_create(head, source, addr, vid);
stephen hemminger03e9b642011-04-04 14:03:27 +0000465 if (!fdb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 return -ENOMEM;
467
stephen hemminger03e9b642011-04-04 14:03:27 +0000468 fdb->is_local = fdb->is_static = 1;
stephen hemminger31e8a492011-12-08 07:17:41 +0000469 fdb_notify(br, fdb, RTM_NEWNEIGH);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 return 0;
471}
472
stephen hemminger03e9b642011-04-04 14:03:27 +0000473/* Add entry for local address of interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474int br_fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000475 const unsigned char *addr, u16 vid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
477 int ret;
478
479 spin_lock_bh(&br->hash_lock);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000480 ret = fdb_insert(br, source, addr, vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 spin_unlock_bh(&br->hash_lock);
482 return ret;
483}
484
485void br_fdb_update(struct net_bridge *br, struct net_bridge_port *source,
Toshiaki Makitaa5642ab2014-02-07 16:48:18 +0900486 const unsigned char *addr, u16 vid, bool added_by_user)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
Vlad Yasevich2ba071e2013-02-13 12:00:16 +0000488 struct hlist_head *head = &br->hash[br_mac_hash(addr, vid)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 struct net_bridge_fdb_entry *fdb;
Jon Maxwellc65c7a32014-05-29 17:27:16 +1000490 bool fdb_modified = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492 /* some users want to always flood. */
493 if (hold_time(br) == 0)
494 return;
495
Stephen Hemmingerdf1c0b82007-08-30 22:15:35 -0700496 /* ignore packets unless we are using this port */
497 if (!(source->state == BR_STATE_LEARNING ||
498 source->state == BR_STATE_FORWARDING))
499 return;
500
Vlad Yasevich2ba071e2013-02-13 12:00:16 +0000501 fdb = fdb_find_rcu(head, addr, vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 if (likely(fdb)) {
503 /* attempt to update an entry for a local interface */
504 if (unlikely(fdb->is_local)) {
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900505 if (net_ratelimit())
stephen hemminger28a16c92010-05-10 09:31:09 +0000506 br_warn(br, "received packet on %s with "
507 "own address as source address\n",
508 source->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 } else {
510 /* fastpath: update of existing entry */
Jon Maxwellc65c7a32014-05-29 17:27:16 +1000511 if (unlikely(source != fdb->dst)) {
512 fdb->dst = source;
513 fdb_modified = true;
514 }
stephen hemminger7cd88612011-04-04 14:03:28 +0000515 fdb->updated = jiffies;
Toshiaki Makitaa5642ab2014-02-07 16:48:18 +0900516 if (unlikely(added_by_user))
517 fdb->added_by_user = 1;
Jon Maxwellc65c7a32014-05-29 17:27:16 +1000518 if (unlikely(fdb_modified))
519 fdb_notify(br, fdb, RTM_NEWNEIGH);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 }
521 } else {
Stephen Hemmingerf8ae7372006-03-20 22:58:36 -0800522 spin_lock(&br->hash_lock);
Vlad Yasevich2ba071e2013-02-13 12:00:16 +0000523 if (likely(!fdb_find(head, addr, vid))) {
524 fdb = fdb_create(head, source, addr, vid);
Toshiaki Makitaa5642ab2014-02-07 16:48:18 +0900525 if (fdb) {
526 if (unlikely(added_by_user))
527 fdb->added_by_user = 1;
stephen hemminger31e8a492011-12-08 07:17:41 +0000528 fdb_notify(br, fdb, RTM_NEWNEIGH);
Toshiaki Makitaa5642ab2014-02-07 16:48:18 +0900529 }
stephen hemmingerf58ee4e2011-12-06 13:02:24 +0000530 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 /* else we lose race and someone else inserts
532 * it first, don't bother updating
533 */
Stephen Hemmingerf8ae7372006-03-20 22:58:36 -0800534 spin_unlock(&br->hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536}
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000537
538static int fdb_to_nud(const struct net_bridge_fdb_entry *fdb)
539{
540 if (fdb->is_local)
541 return NUD_PERMANENT;
542 else if (fdb->is_static)
543 return NUD_NOARP;
544 else if (has_expired(fdb->dst->br, fdb))
545 return NUD_STALE;
546 else
547 return NUD_REACHABLE;
548}
549
stephen hemminger31e8a492011-12-08 07:17:41 +0000550static int fdb_fill_info(struct sk_buff *skb, const struct net_bridge *br,
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000551 const struct net_bridge_fdb_entry *fdb,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000552 u32 portid, u32 seq, int type, unsigned int flags)
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000553{
554 unsigned long now = jiffies;
555 struct nda_cacheinfo ci;
556 struct nlmsghdr *nlh;
557 struct ndmsg *ndm;
558
Eric W. Biederman15e47302012-09-07 20:12:54 +0000559 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000560 if (nlh == NULL)
561 return -EMSGSIZE;
562
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000563 ndm = nlmsg_data(nlh);
564 ndm->ndm_family = AF_BRIDGE;
565 ndm->ndm_pad1 = 0;
566 ndm->ndm_pad2 = 0;
567 ndm->ndm_flags = 0;
568 ndm->ndm_type = 0;
stephen hemminger43598812011-12-08 07:17:49 +0000569 ndm->ndm_ifindex = fdb->dst ? fdb->dst->dev->ifindex : br->dev->ifindex;
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000570 ndm->ndm_state = fdb_to_nud(fdb);
571
David S. Miller2eb812e2012-04-01 20:49:54 -0400572 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->addr))
573 goto nla_put_failure;
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000574 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
575 ci.ndm_confirmed = 0;
576 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
577 ci.ndm_refcnt = 0;
David S. Miller2eb812e2012-04-01 20:49:54 -0400578 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
579 goto nla_put_failure;
Vlad Yasevich1690be62013-02-13 12:00:18 +0000580
581 if (nla_put(skb, NDA_VLAN, sizeof(u16), &fdb->vlan_id))
582 goto nla_put_failure;
583
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000584 return nlmsg_end(skb, nlh);
585
586nla_put_failure:
587 nlmsg_cancel(skb, nlh);
588 return -EMSGSIZE;
589}
590
591static inline size_t fdb_nlmsg_size(void)
592{
593 return NLMSG_ALIGN(sizeof(struct ndmsg))
594 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
Vlad Yasevich1690be62013-02-13 12:00:18 +0000595 + nla_total_size(sizeof(u16)) /* NDA_VLAN */
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000596 + nla_total_size(sizeof(struct nda_cacheinfo));
597}
598
stephen hemminger31e8a492011-12-08 07:17:41 +0000599static void fdb_notify(struct net_bridge *br,
600 const struct net_bridge_fdb_entry *fdb, int type)
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000601{
stephen hemminger31e8a492011-12-08 07:17:41 +0000602 struct net *net = dev_net(br->dev);
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000603 struct sk_buff *skb;
604 int err = -ENOBUFS;
605
606 skb = nlmsg_new(fdb_nlmsg_size(), GFP_ATOMIC);
607 if (skb == NULL)
608 goto errout;
609
stephen hemminger31e8a492011-12-08 07:17:41 +0000610 err = fdb_fill_info(skb, br, fdb, 0, 0, type, 0);
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000611 if (err < 0) {
612 /* -EMSGSIZE implies BUG in fdb_nlmsg_size() */
613 WARN_ON(err == -EMSGSIZE);
614 kfree_skb(skb);
615 goto errout;
616 }
617 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
618 return;
619errout:
tanxiaojun87e823b2013-12-19 13:28:10 +0800620 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000621}
622
623/* Dump information about entries, in response to GETNEIGH */
John Fastabend77162022012-04-15 06:43:56 +0000624int br_fdb_dump(struct sk_buff *skb,
625 struct netlink_callback *cb,
626 struct net_device *dev,
627 int idx)
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000628{
John Fastabend77162022012-04-15 06:43:56 +0000629 struct net_bridge *br = netdev_priv(dev);
630 int i;
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000631
John Fastabend77162022012-04-15 06:43:56 +0000632 if (!(dev->priv_flags & IFF_EBRIDGE))
633 goto out;
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000634
John Fastabend77162022012-04-15 06:43:56 +0000635 for (i = 0; i < BR_HASH_SIZE; i++) {
John Fastabend77162022012-04-15 06:43:56 +0000636 struct net_bridge_fdb_entry *f;
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000637
Sasha Levinb67bfe02013-02-27 17:06:00 -0800638 hlist_for_each_entry_rcu(f, &br->hash[i], hlist) {
John Fastabend77162022012-04-15 06:43:56 +0000639 if (idx < cb->args[0])
640 goto skip;
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000641
John Fastabend77162022012-04-15 06:43:56 +0000642 if (fdb_fill_info(skb, br, f,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000643 NETLINK_CB(cb->skb).portid,
John Fastabend77162022012-04-15 06:43:56 +0000644 cb->nlh->nlmsg_seq,
645 RTM_NEWNEIGH,
646 NLM_F_MULTI) < 0)
647 break;
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000648skip:
John Fastabend77162022012-04-15 06:43:56 +0000649 ++idx;
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000650 }
651 }
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000652
John Fastabend77162022012-04-15 06:43:56 +0000653out:
654 return idx;
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000655}
stephen hemminger36fd2b62011-04-04 14:03:31 +0000656
stephen hemminger292d1392011-11-09 18:30:08 +0000657/* Update (create or replace) forwarding database entry */
stephen hemminger36fd2b62011-04-04 14:03:31 +0000658static int fdb_add_entry(struct net_bridge_port *source, const __u8 *addr,
Vlad Yasevich2ba071e2013-02-13 12:00:16 +0000659 __u16 state, __u16 flags, __u16 vid)
stephen hemminger36fd2b62011-04-04 14:03:31 +0000660{
661 struct net_bridge *br = source->br;
Vlad Yasevich2ba071e2013-02-13 12:00:16 +0000662 struct hlist_head *head = &br->hash[br_mac_hash(addr, vid)];
stephen hemminger36fd2b62011-04-04 14:03:31 +0000663 struct net_bridge_fdb_entry *fdb;
roopab0a397f2013-04-22 12:56:49 +0000664 bool modified = false;
stephen hemminger36fd2b62011-04-04 14:03:31 +0000665
Vlad Yasevich2ba071e2013-02-13 12:00:16 +0000666 fdb = fdb_find(head, addr, vid);
stephen hemminger64af1ba2011-09-30 14:37:27 +0000667 if (fdb == NULL) {
668 if (!(flags & NLM_F_CREATE))
669 return -ENOENT;
stephen hemminger36fd2b62011-04-04 14:03:31 +0000670
Vlad Yasevich2ba071e2013-02-13 12:00:16 +0000671 fdb = fdb_create(head, source, addr, vid);
stephen hemminger64af1ba2011-09-30 14:37:27 +0000672 if (!fdb)
673 return -ENOMEM;
roopab0a397f2013-04-22 12:56:49 +0000674
675 modified = true;
stephen hemminger64af1ba2011-09-30 14:37:27 +0000676 } else {
677 if (flags & NLM_F_EXCL)
678 return -EEXIST;
roopab0a397f2013-04-22 12:56:49 +0000679
680 if (fdb->dst != source) {
681 fdb->dst = source;
682 modified = true;
683 }
stephen hemminger64af1ba2011-09-30 14:37:27 +0000684 }
stephen hemminger36fd2b62011-04-04 14:03:31 +0000685
stephen hemminger292d1392011-11-09 18:30:08 +0000686 if (fdb_to_nud(fdb) != state) {
687 if (state & NUD_PERMANENT)
688 fdb->is_local = fdb->is_static = 1;
689 else if (state & NUD_NOARP) {
690 fdb->is_local = 0;
691 fdb->is_static = 1;
692 } else
693 fdb->is_local = fdb->is_static = 0;
694
roopab0a397f2013-04-22 12:56:49 +0000695 modified = true;
696 }
Toshiaki Makitaa5642ab2014-02-07 16:48:18 +0900697 fdb->added_by_user = 1;
roopab0a397f2013-04-22 12:56:49 +0000698
699 fdb->used = jiffies;
700 if (modified) {
701 fdb->updated = jiffies;
stephen hemminger31e8a492011-12-08 07:17:41 +0000702 fdb_notify(br, fdb, RTM_NEWNEIGH);
stephen hemminger292d1392011-11-09 18:30:08 +0000703 }
704
stephen hemminger36fd2b62011-04-04 14:03:31 +0000705 return 0;
706}
707
Vlad Yasevich1690be62013-02-13 12:00:18 +0000708static int __br_fdb_add(struct ndmsg *ndm, struct net_bridge_port *p,
709 const unsigned char *addr, u16 nlh_flags, u16 vid)
710{
711 int err = 0;
712
713 if (ndm->ndm_flags & NTF_USE) {
714 rcu_read_lock();
Toshiaki Makitaa5642ab2014-02-07 16:48:18 +0900715 br_fdb_update(p->br, p, addr, vid, true);
Vlad Yasevich1690be62013-02-13 12:00:18 +0000716 rcu_read_unlock();
717 } else {
718 spin_lock_bh(&p->br->hash_lock);
719 err = fdb_add_entry(p, addr, ndm->ndm_state,
720 nlh_flags, vid);
721 spin_unlock_bh(&p->br->hash_lock);
722 }
723
724 return err;
725}
726
stephen hemminger36fd2b62011-04-04 14:03:31 +0000727/* Add new permanent fdb entry with RTM_NEWNEIGH */
stephen hemmingeredc7d572012-10-01 12:32:33 +0000728int br_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
729 struct net_device *dev,
stephen hemminger6b6e2722012-09-17 10:03:26 +0000730 const unsigned char *addr, u16 nlh_flags)
stephen hemminger36fd2b62011-04-04 14:03:31 +0000731{
stephen hemminger36fd2b62011-04-04 14:03:31 +0000732 struct net_bridge_port *p;
John Fastabend77162022012-04-15 06:43:56 +0000733 int err = 0;
Vlad Yasevich1690be62013-02-13 12:00:18 +0000734 struct net_port_vlans *pv;
735 unsigned short vid = VLAN_N_VID;
stephen hemminger36fd2b62011-04-04 14:03:31 +0000736
stephen hemminger292d1392011-11-09 18:30:08 +0000737 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE))) {
738 pr_info("bridge: RTM_NEWNEIGH with invalid state %#x\n", ndm->ndm_state);
739 return -EINVAL;
740 }
741
Vlad Yasevich1690be62013-02-13 12:00:18 +0000742 if (tb[NDA_VLAN]) {
743 if (nla_len(tb[NDA_VLAN]) != sizeof(unsigned short)) {
744 pr_info("bridge: RTM_NEWNEIGH with invalid vlan\n");
745 return -EINVAL;
746 }
747
748 vid = nla_get_u16(tb[NDA_VLAN]);
749
Toshiaki Makita8adff412013-10-16 17:07:13 +0900750 if (!vid || vid >= VLAN_VID_MASK) {
Vlad Yasevich1690be62013-02-13 12:00:18 +0000751 pr_info("bridge: RTM_NEWNEIGH with invalid vlan id %d\n",
752 vid);
753 return -EINVAL;
754 }
755 }
756
Stephen Hemminger537f7f82013-06-25 09:34:36 -0700757 if (is_zero_ether_addr(addr)) {
758 pr_info("bridge: RTM_NEWNEIGH with invalid ether address\n");
759 return -EINVAL;
760 }
761
stephen hemminger36fd2b62011-04-04 14:03:31 +0000762 p = br_port_get_rtnl(dev);
763 if (p == NULL) {
764 pr_info("bridge: RTM_NEWNEIGH %s not a bridge port\n",
765 dev->name);
766 return -EINVAL;
767 }
768
Vlad Yasevich1690be62013-02-13 12:00:18 +0000769 pv = nbp_get_vlan_info(p);
770 if (vid != VLAN_N_VID) {
771 if (!pv || !test_bit(vid, pv->vlan_bitmap)) {
772 pr_info("bridge: RTM_NEWNEIGH with unconfigured "
773 "vlan %d on port %s\n", vid, dev->name);
774 return -EINVAL;
775 }
776
777 /* VID was specified, so use it. */
778 err = __br_fdb_add(ndm, p, addr, nlh_flags, vid);
stephen hemminger292d1392011-11-09 18:30:08 +0000779 } else {
Toshiaki Makitaef40b7e2013-08-20 17:10:18 +0900780 if (!pv || bitmap_empty(pv->vlan_bitmap, VLAN_N_VID)) {
Vlad Yasevich1690be62013-02-13 12:00:18 +0000781 err = __br_fdb_add(ndm, p, addr, nlh_flags, 0);
782 goto out;
783 }
784
785 /* We have vlans configured on this port and user didn't
786 * specify a VLAN. To be nice, add/update entry for every
787 * vlan on this port.
788 */
Toshiaki Makitaef40b7e2013-08-20 17:10:18 +0900789 for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID) {
Vlad Yasevich1690be62013-02-13 12:00:18 +0000790 err = __br_fdb_add(ndm, p, addr, nlh_flags, vid);
791 if (err)
792 goto out;
Vlad Yasevich1690be62013-02-13 12:00:18 +0000793 }
stephen hemminger292d1392011-11-09 18:30:08 +0000794 }
stephen hemminger36fd2b62011-04-04 14:03:31 +0000795
Vlad Yasevich1690be62013-02-13 12:00:18 +0000796out:
797 return err;
798}
799
Toshiaki Makita424bb9c2014-02-07 16:48:25 +0900800static int fdb_delete_by_addr(struct net_bridge *br, const u8 *addr, u16 vlan)
Vlad Yasevich1690be62013-02-13 12:00:18 +0000801{
802 struct hlist_head *head = &br->hash[br_mac_hash(addr, vlan)];
803 struct net_bridge_fdb_entry *fdb;
804
805 fdb = fdb_find(head, addr, vlan);
806 if (!fdb)
807 return -ENOENT;
808
809 fdb_delete(br, fdb);
810 return 0;
811}
812
813static int __br_fdb_delete(struct net_bridge_port *p,
814 const unsigned char *addr, u16 vid)
815{
816 int err;
817
818 spin_lock_bh(&p->br->hash_lock);
819 err = fdb_delete_by_addr(p->br, addr, vid);
820 spin_unlock_bh(&p->br->hash_lock);
821
stephen hemminger36fd2b62011-04-04 14:03:31 +0000822 return err;
823}
824
stephen hemminger36fd2b62011-04-04 14:03:31 +0000825/* Remove neighbor entry with RTM_DELNEIGH */
Vlad Yasevich1690be62013-02-13 12:00:18 +0000826int br_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
827 struct net_device *dev,
stephen hemminger6b6e2722012-09-17 10:03:26 +0000828 const unsigned char *addr)
stephen hemminger36fd2b62011-04-04 14:03:31 +0000829{
stephen hemminger36fd2b62011-04-04 14:03:31 +0000830 struct net_bridge_port *p;
stephen hemminger36fd2b62011-04-04 14:03:31 +0000831 int err;
Vlad Yasevich1690be62013-02-13 12:00:18 +0000832 struct net_port_vlans *pv;
833 unsigned short vid = VLAN_N_VID;
stephen hemminger36fd2b62011-04-04 14:03:31 +0000834
Vlad Yasevich1690be62013-02-13 12:00:18 +0000835 if (tb[NDA_VLAN]) {
836 if (nla_len(tb[NDA_VLAN]) != sizeof(unsigned short)) {
837 pr_info("bridge: RTM_NEWNEIGH with invalid vlan\n");
838 return -EINVAL;
839 }
840
841 vid = nla_get_u16(tb[NDA_VLAN]);
842
Toshiaki Makita8adff412013-10-16 17:07:13 +0900843 if (!vid || vid >= VLAN_VID_MASK) {
Vlad Yasevich1690be62013-02-13 12:00:18 +0000844 pr_info("bridge: RTM_NEWNEIGH with invalid vlan id %d\n",
845 vid);
846 return -EINVAL;
847 }
848 }
stephen hemminger36fd2b62011-04-04 14:03:31 +0000849 p = br_port_get_rtnl(dev);
850 if (p == NULL) {
851 pr_info("bridge: RTM_DELNEIGH %s not a bridge port\n",
852 dev->name);
853 return -EINVAL;
854 }
855
Vlad Yasevich1690be62013-02-13 12:00:18 +0000856 pv = nbp_get_vlan_info(p);
857 if (vid != VLAN_N_VID) {
858 if (!pv || !test_bit(vid, pv->vlan_bitmap)) {
859 pr_info("bridge: RTM_DELNEIGH with unconfigured "
860 "vlan %d on port %s\n", vid, dev->name);
861 return -EINVAL;
862 }
stephen hemminger36fd2b62011-04-04 14:03:31 +0000863
Vlad Yasevich1690be62013-02-13 12:00:18 +0000864 err = __br_fdb_delete(p, addr, vid);
865 } else {
Toshiaki Makitaef40b7e2013-08-20 17:10:18 +0900866 if (!pv || bitmap_empty(pv->vlan_bitmap, VLAN_N_VID)) {
Vlad Yasevich1690be62013-02-13 12:00:18 +0000867 err = __br_fdb_delete(p, addr, 0);
868 goto out;
869 }
870
871 /* We have vlans configured on this port and user didn't
872 * specify a VLAN. To be nice, add/update entry for every
873 * vlan on this port.
874 */
875 err = -ENOENT;
Toshiaki Makitaef40b7e2013-08-20 17:10:18 +0900876 for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID) {
Vlad Yasevich1690be62013-02-13 12:00:18 +0000877 err &= __br_fdb_delete(p, addr, vid);
Vlad Yasevich1690be62013-02-13 12:00:18 +0000878 }
879 }
880out:
stephen hemminger36fd2b62011-04-04 14:03:31 +0000881 return err;
882}