blob: 648d0e84959567c09f0692675a4742a4c939c3d8 [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 hemminger31e8a49c2011-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
Vlad Yasevich145beee2014-05-16 09:59:19 -040088/* When a static FDB entry is added, the mac address from the entry is
89 * added to the bridge private HW address list and all required ports
90 * are then updated with the new information.
91 * Called under RTNL.
92 */
93static void fdb_add_hw(struct net_bridge *br, const unsigned char *addr)
94{
95 int err;
96 struct net_bridge_port *p, *tmp;
97
98 ASSERT_RTNL();
99
100 list_for_each_entry(p, &br->port_list, list) {
101 if (!br_promisc_port(p)) {
102 err = dev_uc_add(p->dev, addr);
103 if (err)
104 goto undo;
105 }
106 }
107
108 return;
109undo:
110 list_for_each_entry(tmp, &br->port_list, list) {
111 if (tmp == p)
112 break;
113 if (!br_promisc_port(tmp))
114 dev_uc_del(tmp->dev, addr);
115 }
116}
117
118/* When a static FDB entry is deleted, the HW address from that entry is
119 * also removed from the bridge private HW address list and updates all
120 * the ports with needed information.
121 * Called under RTNL.
122 */
123static void fdb_del_hw(struct net_bridge *br, const unsigned char *addr)
124{
125 struct net_bridge_port *p;
126
127 ASSERT_RTNL();
128
129 list_for_each_entry(p, &br->port_list, list) {
130 if (!br_promisc_port(p))
131 dev_uc_del(p->dev, addr);
132 }
133}
134
stephen hemminger31e8a49c2011-12-08 07:17:41 +0000135static void fdb_delete(struct net_bridge *br, struct net_bridge_fdb_entry *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
Vlad Yasevich145beee2014-05-16 09:59:19 -0400137 if (f->is_static)
138 fdb_del_hw(br, f->addr.addr);
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 hlist_del_rcu(&f->hlist);
stephen hemminger31e8a49c2011-12-08 07:17:41 +0000141 fdb_notify(br, f, RTM_DELNEIGH);
Michał Mirosławda678292009-06-05 05:35:28 +0000142 call_rcu(&f->rcu, fdb_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143}
144
Toshiaki Makita960b5892014-02-07 16:48:23 +0900145/* Delete a local entry if no other port had the same address. */
146static void fdb_delete_local(struct net_bridge *br,
147 const struct net_bridge_port *p,
148 struct net_bridge_fdb_entry *f)
149{
150 const unsigned char *addr = f->addr.addr;
151 u16 vid = f->vlan_id;
152 struct net_bridge_port *op;
153
154 /* Maybe another port has same hw addr? */
155 list_for_each_entry(op, &br->port_list, list) {
156 if (op != p && ether_addr_equal(op->dev->dev_addr, addr) &&
157 (!vid || nbp_vlan_find(op, vid))) {
158 f->dst = op;
Toshiaki Makitaa778e6d2014-02-07 16:48:24 +0900159 f->added_by_user = 0;
Toshiaki Makita960b5892014-02-07 16:48:23 +0900160 return;
161 }
162 }
163
164 /* Maybe bridge device has same hw addr? */
165 if (p && ether_addr_equal(br->dev->dev_addr, addr) &&
166 (!vid || br_vlan_find(br, vid))) {
167 f->dst = NULL;
Toshiaki Makitaa778e6d2014-02-07 16:48:24 +0900168 f->added_by_user = 0;
Toshiaki Makita960b5892014-02-07 16:48:23 +0900169 return;
170 }
171
172 fdb_delete(br, f);
173}
174
Toshiaki Makita424bb9c2014-02-07 16:48:25 +0900175void br_fdb_find_delete_local(struct net_bridge *br,
176 const struct net_bridge_port *p,
177 const unsigned char *addr, u16 vid)
178{
179 struct hlist_head *head = &br->hash[br_mac_hash(addr, vid)];
180 struct net_bridge_fdb_entry *f;
181
182 spin_lock_bh(&br->hash_lock);
183 f = fdb_find(head, addr, vid);
184 if (f && f->is_local && !f->added_by_user && f->dst == p)
185 fdb_delete_local(br, p, f);
186 spin_unlock_bh(&br->hash_lock);
187}
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189void br_fdb_changeaddr(struct net_bridge_port *p, const unsigned char *newaddr)
190{
191 struct net_bridge *br = p->br;
Toshiaki Makita28368822014-02-07 16:48:19 +0900192 struct net_port_vlans *pv = nbp_get_vlan_info(p);
193 bool no_vlan = !pv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 int i;
Toshiaki Makita28368822014-02-07 16:48:19 +0900195 u16 vid;
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 spin_lock_bh(&br->hash_lock);
198
199 /* Search all chains since old address/hash is unknown */
200 for (i = 0; i < BR_HASH_SIZE; i++) {
201 struct hlist_node *h;
202 hlist_for_each(h, &br->hash[i]) {
203 struct net_bridge_fdb_entry *f;
204
205 f = hlist_entry(h, struct net_bridge_fdb_entry, hlist);
Toshiaki Makitaa5642ab2014-02-07 16:48:18 +0900206 if (f->dst == p && f->is_local && !f->added_by_user) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 /* delete old one */
Toshiaki Makita960b5892014-02-07 16:48:23 +0900208 fdb_delete_local(br, p, f);
209
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000210 /* if this port has no vlan information
211 * configured, we can safely be done at
212 * this point.
213 */
214 if (no_vlan)
Toshiaki Makita28368822014-02-07 16:48:19 +0900215 goto insert;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 }
217 }
218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Toshiaki Makita28368822014-02-07 16:48:19 +0900220insert:
221 /* insert new address, may fail if invalid address or dup. */
222 fdb_insert(br, p, newaddr, 0);
223
224 if (no_vlan)
225 goto done;
226
227 /* Now add entries for every VLAN configured on the port.
228 * This function runs under RTNL so the bitmap will not change
229 * from under us.
230 */
231 for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID)
232 fdb_insert(br, p, newaddr, vid);
233
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000234done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 spin_unlock_bh(&br->hash_lock);
236}
237
stephen hemminger43598812011-12-08 07:17:49 +0000238void br_fdb_change_mac_address(struct net_bridge *br, const u8 *newaddr)
239{
240 struct net_bridge_fdb_entry *f;
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000241 struct net_port_vlans *pv;
242 u16 vid = 0;
stephen hemminger43598812011-12-08 07:17:49 +0000243
Toshiaki Makitaac4c8862014-02-07 16:48:26 +0900244 spin_lock_bh(&br->hash_lock);
245
stephen hemminger43598812011-12-08 07:17:49 +0000246 /* If old entry was unassociated with any port, then delete it. */
Vlad Yasevich2ba071e2013-02-13 12:00:16 +0000247 f = __br_fdb_get(br, br->dev->dev_addr, 0);
stephen hemminger43598812011-12-08 07:17:49 +0000248 if (f && f->is_local && !f->dst)
Toshiaki Makita960b5892014-02-07 16:48:23 +0900249 fdb_delete_local(br, NULL, f);
stephen hemminger43598812011-12-08 07:17:49 +0000250
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000251 fdb_insert(br, NULL, newaddr, 0);
252
253 /* Now remove and add entries for every VLAN configured on the
254 * bridge. This function runs under RTNL so the bitmap will not
255 * change from under us.
256 */
257 pv = br_get_vlan_info(br);
258 if (!pv)
Toshiaki Makitaac4c8862014-02-07 16:48:26 +0900259 goto out;
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000260
Toshiaki Makitaef40b7e2013-08-20 17:10:18 +0900261 for_each_set_bit_from(vid, pv->vlan_bitmap, VLAN_N_VID) {
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000262 f = __br_fdb_get(br, br->dev->dev_addr, vid);
263 if (f && f->is_local && !f->dst)
Toshiaki Makita960b5892014-02-07 16:48:23 +0900264 fdb_delete_local(br, NULL, f);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000265 fdb_insert(br, NULL, newaddr, vid);
266 }
Toshiaki Makitaac4c8862014-02-07 16:48:26 +0900267out:
268 spin_unlock_bh(&br->hash_lock);
stephen hemminger43598812011-12-08 07:17:49 +0000269}
270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271void br_fdb_cleanup(unsigned long _data)
272{
273 struct net_bridge *br = (struct net_bridge *)_data;
274 unsigned long delay = hold_time(br);
stephen hemminger25442e02010-06-15 06:14:12 +0000275 unsigned long next_timer = jiffies + br->ageing_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 int i;
277
Eric Dumazet27a42932012-01-16 04:35:50 +0000278 spin_lock(&br->hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 for (i = 0; i < BR_HASH_SIZE; i++) {
280 struct net_bridge_fdb_entry *f;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800281 struct hlist_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Sasha Levinb67bfe02013-02-27 17:06:00 -0800283 hlist_for_each_entry_safe(f, n, &br->hash[i], hlist) {
Baruch Even071f7722007-05-31 01:20:45 -0700284 unsigned long this_timer;
285 if (f->is_static)
286 continue;
stephen hemminger7cd88612011-04-04 14:03:28 +0000287 this_timer = f->updated + delay;
Baruch Even071f7722007-05-31 01:20:45 -0700288 if (time_before_eq(this_timer, jiffies))
stephen hemminger31e8a49c2011-12-08 07:17:41 +0000289 fdb_delete(br, f);
Fabio Checconi2bec0082008-03-20 15:54:58 -0700290 else if (time_before(this_timer, next_timer))
Baruch Even071f7722007-05-31 01:20:45 -0700291 next_timer = this_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 }
293 }
Eric Dumazet27a42932012-01-16 04:35:50 +0000294 spin_unlock(&br->hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
stephen hemminger25442e02010-06-15 06:14:12 +0000296 mod_timer(&br->gc_timer, round_jiffies_up(next_timer));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298
Stephen Hemminger9cf63742007-04-09 12:57:54 -0700299/* Completely flush all dynamic entries in forwarding database.*/
300void br_fdb_flush(struct net_bridge *br)
301{
302 int i;
Stephen Hemminger1a620692006-10-12 14:45:38 -0700303
Stephen Hemminger9cf63742007-04-09 12:57:54 -0700304 spin_lock_bh(&br->hash_lock);
305 for (i = 0; i < BR_HASH_SIZE; i++) {
306 struct net_bridge_fdb_entry *f;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800307 struct hlist_node *n;
308 hlist_for_each_entry_safe(f, n, &br->hash[i], hlist) {
Stephen Hemminger9cf63742007-04-09 12:57:54 -0700309 if (!f->is_static)
stephen hemminger31e8a49c2011-12-08 07:17:41 +0000310 fdb_delete(br, f);
Stephen Hemminger9cf63742007-04-09 12:57:54 -0700311 }
312 }
313 spin_unlock_bh(&br->hash_lock);
314}
315
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300316/* Flush all entries referring to a specific port.
Stephen Hemminger9cf63742007-04-09 12:57:54 -0700317 * if do_all is set also flush static entries
318 */
Stephen Hemminger1a620692006-10-12 14:45:38 -0700319void br_fdb_delete_by_port(struct net_bridge *br,
320 const struct net_bridge_port *p,
321 int do_all)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
323 int i;
324
325 spin_lock_bh(&br->hash_lock);
326 for (i = 0; i < BR_HASH_SIZE; i++) {
327 struct hlist_node *h, *g;
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 hlist_for_each_safe(h, g, &br->hash[i]) {
330 struct net_bridge_fdb_entry *f
331 = hlist_entry(h, struct net_bridge_fdb_entry, hlist);
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900332 if (f->dst != p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 continue;
334
Stephen Hemminger1a620692006-10-12 14:45:38 -0700335 if (f->is_static && !do_all)
336 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Toshiaki Makitaa778e6d2014-02-07 16:48:24 +0900338 if (f->is_local)
339 fdb_delete_local(br, p, f);
340 else
341 fdb_delete(br, f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
343 }
344 spin_unlock_bh(&br->hash_lock);
345}
346
stephen hemmingereeaf61d2010-07-27 08:26:30 +0000347/* No locking or refcounting, assumes caller has rcu_read_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348struct net_bridge_fdb_entry *__br_fdb_get(struct net_bridge *br,
Vlad Yasevich2ba071e2013-02-13 12:00:16 +0000349 const unsigned char *addr,
350 __u16 vid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 struct net_bridge_fdb_entry *fdb;
353
Sasha Levinb67bfe02013-02-27 17:06:00 -0800354 hlist_for_each_entry_rcu(fdb,
Vlad Yasevich2ba071e2013-02-13 12:00:16 +0000355 &br->hash[br_mac_hash(addr, vid)], hlist) {
356 if (ether_addr_equal(fdb->addr.addr, addr) &&
357 fdb->vlan_id == vid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 if (unlikely(has_expired(br, fdb)))
359 break;
360 return fdb;
361 }
362 }
363
364 return NULL;
365}
366
Igor Maraviće6373c42011-12-12 02:58:25 +0000367#if IS_ENABLED(CONFIG_ATM_LANE)
Michał Mirosławda678292009-06-05 05:35:28 +0000368/* Interface used by ATM LANE hook to test
369 * if an addr is on some other bridge port */
370int br_fdb_test_addr(struct net_device *dev, unsigned char *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
372 struct net_bridge_fdb_entry *fdb;
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000373 struct net_bridge_port *port;
Michał Mirosławda678292009-06-05 05:35:28 +0000374 int ret;
375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 rcu_read_lock();
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000377 port = br_port_get_rcu(dev);
378 if (!port)
379 ret = 0;
380 else {
Vlad Yasevich2ba071e2013-02-13 12:00:16 +0000381 fdb = __br_fdb_get(port->br, addr, 0);
stephen hemminger43598812011-12-08 07:17:49 +0000382 ret = fdb && fdb->dst && fdb->dst->dev != dev &&
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000383 fdb->dst->state == BR_STATE_FORWARDING;
384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Michał Mirosławda678292009-06-05 05:35:28 +0000387 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388}
Michał Mirosławda678292009-06-05 05:35:28 +0000389#endif /* CONFIG_ATM_LANE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391/*
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900392 * Fill buffer with forwarding table records in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 * the API format.
394 */
395int br_fdb_fillbuf(struct net_bridge *br, void *buf,
396 unsigned long maxnum, unsigned long skip)
397{
398 struct __fdb_entry *fe = buf;
399 int i, num = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 struct net_bridge_fdb_entry *f;
401
402 memset(buf, 0, maxnum*sizeof(struct __fdb_entry));
403
404 rcu_read_lock();
405 for (i = 0; i < BR_HASH_SIZE; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800406 hlist_for_each_entry_rcu(f, &br->hash[i], hlist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if (num >= maxnum)
408 goto out;
409
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900410 if (has_expired(br, f))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 continue;
412
stephen hemminger43598812011-12-08 07:17:49 +0000413 /* ignore pseudo entry for local MAC address */
414 if (!f->dst)
415 continue;
416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 if (skip) {
418 --skip;
419 continue;
420 }
421
422 /* convert from internal format to API */
423 memcpy(fe->mac_addr, f->addr.addr, ETH_ALEN);
Stephen Hemmingerae4f8fc2008-05-02 16:53:33 -0700424
425 /* due to ABI compat need to split into hi/lo */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 fe->port_no = f->dst->port_no;
Stephen Hemmingerae4f8fc2008-05-02 16:53:33 -0700427 fe->port_hi = f->dst->port_no >> 8;
428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 fe->is_local = f->is_local;
430 if (!f->is_static)
Eric Dumazeta399a802012-08-08 21:13:53 +0000431 fe->ageing_timer_value = jiffies_delta_to_clock_t(jiffies - f->updated);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 ++fe;
433 ++num;
434 }
435 }
436
437 out:
438 rcu_read_unlock();
439
440 return num;
441}
442
stephen hemminger664de482011-04-04 14:03:29 +0000443static struct net_bridge_fdb_entry *fdb_find(struct hlist_head *head,
Vlad Yasevich2ba071e2013-02-13 12:00:16 +0000444 const unsigned char *addr,
445 __u16 vid)
stephen hemminger664de482011-04-04 14:03:29 +0000446{
stephen hemminger664de482011-04-04 14:03:29 +0000447 struct net_bridge_fdb_entry *fdb;
448
Sasha Levinb67bfe02013-02-27 17:06:00 -0800449 hlist_for_each_entry(fdb, head, hlist) {
Vlad Yasevich2ba071e2013-02-13 12:00:16 +0000450 if (ether_addr_equal(fdb->addr.addr, addr) &&
451 fdb->vlan_id == vid)
stephen hemminger664de482011-04-04 14:03:29 +0000452 return fdb;
453 }
454 return NULL;
455}
456
457static struct net_bridge_fdb_entry *fdb_find_rcu(struct hlist_head *head,
Vlad Yasevich2ba071e2013-02-13 12:00:16 +0000458 const unsigned char *addr,
459 __u16 vid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 struct net_bridge_fdb_entry *fdb;
462
Sasha Levinb67bfe02013-02-27 17:06:00 -0800463 hlist_for_each_entry_rcu(fdb, head, hlist) {
Vlad Yasevich2ba071e2013-02-13 12:00:16 +0000464 if (ether_addr_equal(fdb->addr.addr, addr) &&
465 fdb->vlan_id == vid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 return fdb;
467 }
468 return NULL;
469}
470
471static struct net_bridge_fdb_entry *fdb_create(struct hlist_head *head,
472 struct net_bridge_port *source,
Vlad Yasevich2ba071e2013-02-13 12:00:16 +0000473 const unsigned char *addr,
474 __u16 vid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
476 struct net_bridge_fdb_entry *fdb;
477
478 fdb = kmem_cache_alloc(br_fdb_cache, GFP_ATOMIC);
479 if (fdb) {
480 memcpy(fdb->addr.addr, addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 fdb->dst = source;
Vlad Yasevich2ba071e2013-02-13 12:00:16 +0000482 fdb->vlan_id = vid;
stephen hemminger03e9b642011-04-04 14:03:27 +0000483 fdb->is_local = 0;
484 fdb->is_static = 0;
Toshiaki Makitaa5642ab2014-02-07 16:48:18 +0900485 fdb->added_by_user = 0;
stephen hemminger7cd88612011-04-04 14:03:28 +0000486 fdb->updated = fdb->used = jiffies;
Pavel Emelyanov1158f762011-02-04 13:02:36 -0800487 hlist_add_head_rcu(&fdb->hlist, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 }
489 return fdb;
490}
491
492static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000493 const unsigned char *addr, u16 vid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000495 struct hlist_head *head = &br->hash[br_mac_hash(addr, vid)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 struct net_bridge_fdb_entry *fdb;
497
498 if (!is_valid_ether_addr(addr))
499 return -EINVAL;
500
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000501 fdb = fdb_find(head, addr, vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 if (fdb) {
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900503 /* it is okay to have multiple ports with same
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 * address, just use the first one.
505 */
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900506 if (fdb->is_local)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 return 0;
stephen hemminger28a16c92010-05-10 09:31:09 +0000508 br_warn(br, "adding interface %s with same address "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 "as a received packet\n",
Hong zhi guo9b469222013-03-23 02:27:50 +0000510 source ? source->dev->name : br->dev->name);
stephen hemminger31e8a49c2011-12-08 07:17:41 +0000511 fdb_delete(br, fdb);
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000514 fdb = fdb_create(head, source, addr, vid);
stephen hemminger03e9b642011-04-04 14:03:27 +0000515 if (!fdb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 return -ENOMEM;
517
stephen hemminger03e9b642011-04-04 14:03:27 +0000518 fdb->is_local = fdb->is_static = 1;
Vlad Yasevich145beee2014-05-16 09:59:19 -0400519 fdb_add_hw(br, addr);
stephen hemminger31e8a49c2011-12-08 07:17:41 +0000520 fdb_notify(br, fdb, RTM_NEWNEIGH);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 return 0;
522}
523
stephen hemminger03e9b642011-04-04 14:03:27 +0000524/* Add entry for local address of interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525int br_fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000526 const unsigned char *addr, u16 vid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
528 int ret;
529
530 spin_lock_bh(&br->hash_lock);
Vlad Yasevichbc9a25d2013-02-13 12:00:19 +0000531 ret = fdb_insert(br, source, addr, vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 spin_unlock_bh(&br->hash_lock);
533 return ret;
534}
535
536void br_fdb_update(struct net_bridge *br, struct net_bridge_port *source,
Toshiaki Makitaa5642ab2014-02-07 16:48:18 +0900537 const unsigned char *addr, u16 vid, bool added_by_user)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
Vlad Yasevich2ba071e2013-02-13 12:00:16 +0000539 struct hlist_head *head = &br->hash[br_mac_hash(addr, vid)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 struct net_bridge_fdb_entry *fdb;
541
542 /* some users want to always flood. */
543 if (hold_time(br) == 0)
544 return;
545
Stephen Hemmingerdf1c0b82007-08-30 22:15:35 -0700546 /* ignore packets unless we are using this port */
547 if (!(source->state == BR_STATE_LEARNING ||
548 source->state == BR_STATE_FORWARDING))
549 return;
550
Vlad Yasevich2ba071e2013-02-13 12:00:16 +0000551 fdb = fdb_find_rcu(head, addr, vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 if (likely(fdb)) {
553 /* attempt to update an entry for a local interface */
554 if (unlikely(fdb->is_local)) {
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900555 if (net_ratelimit())
stephen hemminger28a16c92010-05-10 09:31:09 +0000556 br_warn(br, "received packet on %s with "
557 "own address as source address\n",
558 source->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 } else {
560 /* fastpath: update of existing entry */
561 fdb->dst = source;
stephen hemminger7cd88612011-04-04 14:03:28 +0000562 fdb->updated = jiffies;
Toshiaki Makitaa5642ab2014-02-07 16:48:18 +0900563 if (unlikely(added_by_user))
564 fdb->added_by_user = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 }
566 } else {
Stephen Hemmingerf8ae7372006-03-20 22:58:36 -0800567 spin_lock(&br->hash_lock);
Vlad Yasevich2ba071e2013-02-13 12:00:16 +0000568 if (likely(!fdb_find(head, addr, vid))) {
569 fdb = fdb_create(head, source, addr, vid);
Toshiaki Makitaa5642ab2014-02-07 16:48:18 +0900570 if (fdb) {
571 if (unlikely(added_by_user))
572 fdb->added_by_user = 1;
stephen hemminger31e8a49c2011-12-08 07:17:41 +0000573 fdb_notify(br, fdb, RTM_NEWNEIGH);
Toshiaki Makitaa5642ab2014-02-07 16:48:18 +0900574 }
stephen hemmingerf58ee4e2011-12-06 13:02:24 +0000575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 /* else we lose race and someone else inserts
577 * it first, don't bother updating
578 */
Stephen Hemmingerf8ae7372006-03-20 22:58:36 -0800579 spin_unlock(&br->hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581}
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000582
583static int fdb_to_nud(const struct net_bridge_fdb_entry *fdb)
584{
585 if (fdb->is_local)
586 return NUD_PERMANENT;
587 else if (fdb->is_static)
588 return NUD_NOARP;
589 else if (has_expired(fdb->dst->br, fdb))
590 return NUD_STALE;
591 else
592 return NUD_REACHABLE;
593}
594
stephen hemminger31e8a49c2011-12-08 07:17:41 +0000595static int fdb_fill_info(struct sk_buff *skb, const struct net_bridge *br,
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000596 const struct net_bridge_fdb_entry *fdb,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000597 u32 portid, u32 seq, int type, unsigned int flags)
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000598{
599 unsigned long now = jiffies;
600 struct nda_cacheinfo ci;
601 struct nlmsghdr *nlh;
602 struct ndmsg *ndm;
603
Eric W. Biederman15e47302012-09-07 20:12:54 +0000604 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000605 if (nlh == NULL)
606 return -EMSGSIZE;
607
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000608 ndm = nlmsg_data(nlh);
609 ndm->ndm_family = AF_BRIDGE;
610 ndm->ndm_pad1 = 0;
611 ndm->ndm_pad2 = 0;
612 ndm->ndm_flags = 0;
613 ndm->ndm_type = 0;
stephen hemminger43598812011-12-08 07:17:49 +0000614 ndm->ndm_ifindex = fdb->dst ? fdb->dst->dev->ifindex : br->dev->ifindex;
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000615 ndm->ndm_state = fdb_to_nud(fdb);
616
David S. Miller2eb812e2012-04-01 20:49:54 -0400617 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->addr))
618 goto nla_put_failure;
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000619 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
620 ci.ndm_confirmed = 0;
621 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
622 ci.ndm_refcnt = 0;
David S. Miller2eb812e2012-04-01 20:49:54 -0400623 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
624 goto nla_put_failure;
Vlad Yasevich1690be62013-02-13 12:00:18 +0000625
626 if (nla_put(skb, NDA_VLAN, sizeof(u16), &fdb->vlan_id))
627 goto nla_put_failure;
628
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000629 return nlmsg_end(skb, nlh);
630
631nla_put_failure:
632 nlmsg_cancel(skb, nlh);
633 return -EMSGSIZE;
634}
635
636static inline size_t fdb_nlmsg_size(void)
637{
638 return NLMSG_ALIGN(sizeof(struct ndmsg))
639 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
Vlad Yasevich1690be62013-02-13 12:00:18 +0000640 + nla_total_size(sizeof(u16)) /* NDA_VLAN */
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000641 + nla_total_size(sizeof(struct nda_cacheinfo));
642}
643
stephen hemminger31e8a49c2011-12-08 07:17:41 +0000644static void fdb_notify(struct net_bridge *br,
645 const struct net_bridge_fdb_entry *fdb, int type)
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000646{
stephen hemminger31e8a49c2011-12-08 07:17:41 +0000647 struct net *net = dev_net(br->dev);
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000648 struct sk_buff *skb;
649 int err = -ENOBUFS;
650
651 skb = nlmsg_new(fdb_nlmsg_size(), GFP_ATOMIC);
652 if (skb == NULL)
653 goto errout;
654
stephen hemminger31e8a49c2011-12-08 07:17:41 +0000655 err = fdb_fill_info(skb, br, fdb, 0, 0, type, 0);
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000656 if (err < 0) {
657 /* -EMSGSIZE implies BUG in fdb_nlmsg_size() */
658 WARN_ON(err == -EMSGSIZE);
659 kfree_skb(skb);
660 goto errout;
661 }
662 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
663 return;
664errout:
tanxiaojun87e823b2013-12-19 13:28:10 +0800665 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000666}
667
668/* Dump information about entries, in response to GETNEIGH */
John Fastabend77162022012-04-15 06:43:56 +0000669int br_fdb_dump(struct sk_buff *skb,
670 struct netlink_callback *cb,
671 struct net_device *dev,
672 int idx)
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000673{
John Fastabend77162022012-04-15 06:43:56 +0000674 struct net_bridge *br = netdev_priv(dev);
675 int i;
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000676
John Fastabend77162022012-04-15 06:43:56 +0000677 if (!(dev->priv_flags & IFF_EBRIDGE))
678 goto out;
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000679
John Fastabend77162022012-04-15 06:43:56 +0000680 for (i = 0; i < BR_HASH_SIZE; i++) {
John Fastabend77162022012-04-15 06:43:56 +0000681 struct net_bridge_fdb_entry *f;
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000682
Sasha Levinb67bfe02013-02-27 17:06:00 -0800683 hlist_for_each_entry_rcu(f, &br->hash[i], hlist) {
John Fastabend77162022012-04-15 06:43:56 +0000684 if (idx < cb->args[0])
685 goto skip;
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000686
John Fastabend77162022012-04-15 06:43:56 +0000687 if (fdb_fill_info(skb, br, f,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000688 NETLINK_CB(cb->skb).portid,
John Fastabend77162022012-04-15 06:43:56 +0000689 cb->nlh->nlmsg_seq,
690 RTM_NEWNEIGH,
691 NLM_F_MULTI) < 0)
692 break;
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000693skip:
John Fastabend77162022012-04-15 06:43:56 +0000694 ++idx;
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000695 }
696 }
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000697
John Fastabend77162022012-04-15 06:43:56 +0000698out:
699 return idx;
stephen hemmingerb078f0d2011-04-04 14:03:30 +0000700}
stephen hemminger36fd2b62011-04-04 14:03:31 +0000701
stephen hemminger292d1392011-11-09 18:30:08 +0000702/* Update (create or replace) forwarding database entry */
stephen hemminger36fd2b62011-04-04 14:03:31 +0000703static int fdb_add_entry(struct net_bridge_port *source, const __u8 *addr,
Vlad Yasevich2ba071e2013-02-13 12:00:16 +0000704 __u16 state, __u16 flags, __u16 vid)
stephen hemminger36fd2b62011-04-04 14:03:31 +0000705{
706 struct net_bridge *br = source->br;
Vlad Yasevich2ba071e2013-02-13 12:00:16 +0000707 struct hlist_head *head = &br->hash[br_mac_hash(addr, vid)];
stephen hemminger36fd2b62011-04-04 14:03:31 +0000708 struct net_bridge_fdb_entry *fdb;
roopab0a397f2013-04-22 12:56:49 +0000709 bool modified = false;
stephen hemminger36fd2b62011-04-04 14:03:31 +0000710
Vlad Yasevich2ba071e2013-02-13 12:00:16 +0000711 fdb = fdb_find(head, addr, vid);
stephen hemminger64af1ba2011-09-30 14:37:27 +0000712 if (fdb == NULL) {
713 if (!(flags & NLM_F_CREATE))
714 return -ENOENT;
stephen hemminger36fd2b62011-04-04 14:03:31 +0000715
Vlad Yasevich2ba071e2013-02-13 12:00:16 +0000716 fdb = fdb_create(head, source, addr, vid);
stephen hemminger64af1ba2011-09-30 14:37:27 +0000717 if (!fdb)
718 return -ENOMEM;
roopab0a397f2013-04-22 12:56:49 +0000719
720 modified = true;
stephen hemminger64af1ba2011-09-30 14:37:27 +0000721 } else {
722 if (flags & NLM_F_EXCL)
723 return -EEXIST;
roopab0a397f2013-04-22 12:56:49 +0000724
725 if (fdb->dst != source) {
726 fdb->dst = source;
727 modified = true;
728 }
stephen hemminger64af1ba2011-09-30 14:37:27 +0000729 }
stephen hemminger36fd2b62011-04-04 14:03:31 +0000730
stephen hemminger292d1392011-11-09 18:30:08 +0000731 if (fdb_to_nud(fdb) != state) {
Vlad Yasevich145beee2014-05-16 09:59:19 -0400732 if (state & NUD_PERMANENT) {
733 fdb->is_local = 1;
734 if (!fdb->is_static) {
735 fdb->is_static = 1;
736 fdb_add_hw(br, addr);
737 }
738 } else if (state & NUD_NOARP) {
stephen hemminger292d1392011-11-09 18:30:08 +0000739 fdb->is_local = 0;
Vlad Yasevich145beee2014-05-16 09:59:19 -0400740 if (!fdb->is_static) {
741 fdb->is_static = 1;
742 fdb_add_hw(br, addr);
743 }
744 } else {
745 fdb->is_local = 0;
746 if (fdb->is_static) {
747 fdb->is_static = 0;
748 fdb_del_hw(br, addr);
749 }
750 }
stephen hemminger292d1392011-11-09 18:30:08 +0000751
roopab0a397f2013-04-22 12:56:49 +0000752 modified = true;
753 }
Toshiaki Makitaa5642ab2014-02-07 16:48:18 +0900754 fdb->added_by_user = 1;
roopab0a397f2013-04-22 12:56:49 +0000755
756 fdb->used = jiffies;
757 if (modified) {
758 fdb->updated = jiffies;
stephen hemminger31e8a49c2011-12-08 07:17:41 +0000759 fdb_notify(br, fdb, RTM_NEWNEIGH);
stephen hemminger292d1392011-11-09 18:30:08 +0000760 }
761
stephen hemminger36fd2b62011-04-04 14:03:31 +0000762 return 0;
763}
764
Vlad Yasevich1690be62013-02-13 12:00:18 +0000765static int __br_fdb_add(struct ndmsg *ndm, struct net_bridge_port *p,
766 const unsigned char *addr, u16 nlh_flags, u16 vid)
767{
768 int err = 0;
769
770 if (ndm->ndm_flags & NTF_USE) {
771 rcu_read_lock();
Toshiaki Makitaa5642ab2014-02-07 16:48:18 +0900772 br_fdb_update(p->br, p, addr, vid, true);
Vlad Yasevich1690be62013-02-13 12:00:18 +0000773 rcu_read_unlock();
774 } else {
775 spin_lock_bh(&p->br->hash_lock);
776 err = fdb_add_entry(p, addr, ndm->ndm_state,
777 nlh_flags, vid);
778 spin_unlock_bh(&p->br->hash_lock);
779 }
780
781 return err;
782}
783
stephen hemminger36fd2b62011-04-04 14:03:31 +0000784/* Add new permanent fdb entry with RTM_NEWNEIGH */
stephen hemmingeredc7d572012-10-01 12:32:33 +0000785int br_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
786 struct net_device *dev,
stephen hemminger6b6e2722012-09-17 10:03:26 +0000787 const unsigned char *addr, u16 nlh_flags)
stephen hemminger36fd2b62011-04-04 14:03:31 +0000788{
stephen hemminger36fd2b62011-04-04 14:03:31 +0000789 struct net_bridge_port *p;
John Fastabend77162022012-04-15 06:43:56 +0000790 int err = 0;
Vlad Yasevich1690be62013-02-13 12:00:18 +0000791 struct net_port_vlans *pv;
792 unsigned short vid = VLAN_N_VID;
stephen hemminger36fd2b62011-04-04 14:03:31 +0000793
stephen hemminger292d1392011-11-09 18:30:08 +0000794 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE))) {
795 pr_info("bridge: RTM_NEWNEIGH with invalid state %#x\n", ndm->ndm_state);
796 return -EINVAL;
797 }
798
Vlad Yasevich1690be62013-02-13 12:00:18 +0000799 if (tb[NDA_VLAN]) {
800 if (nla_len(tb[NDA_VLAN]) != sizeof(unsigned short)) {
801 pr_info("bridge: RTM_NEWNEIGH with invalid vlan\n");
802 return -EINVAL;
803 }
804
805 vid = nla_get_u16(tb[NDA_VLAN]);
806
Toshiaki Makita8adff412013-10-16 17:07:13 +0900807 if (!vid || vid >= VLAN_VID_MASK) {
Vlad Yasevich1690be62013-02-13 12:00:18 +0000808 pr_info("bridge: RTM_NEWNEIGH with invalid vlan id %d\n",
809 vid);
810 return -EINVAL;
811 }
812 }
813
Stephen Hemminger537f7f82013-06-25 09:34:36 -0700814 if (is_zero_ether_addr(addr)) {
815 pr_info("bridge: RTM_NEWNEIGH with invalid ether address\n");
816 return -EINVAL;
817 }
818
stephen hemminger36fd2b62011-04-04 14:03:31 +0000819 p = br_port_get_rtnl(dev);
820 if (p == NULL) {
821 pr_info("bridge: RTM_NEWNEIGH %s not a bridge port\n",
822 dev->name);
823 return -EINVAL;
824 }
825
Vlad Yasevich1690be62013-02-13 12:00:18 +0000826 pv = nbp_get_vlan_info(p);
827 if (vid != VLAN_N_VID) {
828 if (!pv || !test_bit(vid, pv->vlan_bitmap)) {
829 pr_info("bridge: RTM_NEWNEIGH with unconfigured "
830 "vlan %d on port %s\n", vid, dev->name);
831 return -EINVAL;
832 }
833
834 /* VID was specified, so use it. */
835 err = __br_fdb_add(ndm, p, addr, nlh_flags, vid);
stephen hemminger292d1392011-11-09 18:30:08 +0000836 } else {
Toshiaki Makitaef40b7e2013-08-20 17:10:18 +0900837 if (!pv || bitmap_empty(pv->vlan_bitmap, VLAN_N_VID)) {
Vlad Yasevich1690be62013-02-13 12:00:18 +0000838 err = __br_fdb_add(ndm, p, addr, nlh_flags, 0);
839 goto out;
840 }
841
842 /* We have vlans configured on this port and user didn't
843 * specify a VLAN. To be nice, add/update entry for every
844 * vlan on this port.
845 */
Toshiaki Makitaef40b7e2013-08-20 17:10:18 +0900846 for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID) {
Vlad Yasevich1690be62013-02-13 12:00:18 +0000847 err = __br_fdb_add(ndm, p, addr, nlh_flags, vid);
848 if (err)
849 goto out;
Vlad Yasevich1690be62013-02-13 12:00:18 +0000850 }
stephen hemminger292d1392011-11-09 18:30:08 +0000851 }
stephen hemminger36fd2b62011-04-04 14:03:31 +0000852
Vlad Yasevich1690be62013-02-13 12:00:18 +0000853out:
854 return err;
855}
856
Toshiaki Makita424bb9c2014-02-07 16:48:25 +0900857static int fdb_delete_by_addr(struct net_bridge *br, const u8 *addr, u16 vlan)
Vlad Yasevich1690be62013-02-13 12:00:18 +0000858{
859 struct hlist_head *head = &br->hash[br_mac_hash(addr, vlan)];
860 struct net_bridge_fdb_entry *fdb;
861
862 fdb = fdb_find(head, addr, vlan);
863 if (!fdb)
864 return -ENOENT;
865
866 fdb_delete(br, fdb);
867 return 0;
868}
869
870static int __br_fdb_delete(struct net_bridge_port *p,
871 const unsigned char *addr, u16 vid)
872{
873 int err;
874
875 spin_lock_bh(&p->br->hash_lock);
876 err = fdb_delete_by_addr(p->br, addr, vid);
877 spin_unlock_bh(&p->br->hash_lock);
878
stephen hemminger36fd2b62011-04-04 14:03:31 +0000879 return err;
880}
881
stephen hemminger36fd2b62011-04-04 14:03:31 +0000882/* Remove neighbor entry with RTM_DELNEIGH */
Vlad Yasevich1690be62013-02-13 12:00:18 +0000883int br_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
884 struct net_device *dev,
stephen hemminger6b6e2722012-09-17 10:03:26 +0000885 const unsigned char *addr)
stephen hemminger36fd2b62011-04-04 14:03:31 +0000886{
stephen hemminger36fd2b62011-04-04 14:03:31 +0000887 struct net_bridge_port *p;
stephen hemminger36fd2b62011-04-04 14:03:31 +0000888 int err;
Vlad Yasevich1690be62013-02-13 12:00:18 +0000889 struct net_port_vlans *pv;
890 unsigned short vid = VLAN_N_VID;
stephen hemminger36fd2b62011-04-04 14:03:31 +0000891
Vlad Yasevich1690be62013-02-13 12:00:18 +0000892 if (tb[NDA_VLAN]) {
893 if (nla_len(tb[NDA_VLAN]) != sizeof(unsigned short)) {
894 pr_info("bridge: RTM_NEWNEIGH with invalid vlan\n");
895 return -EINVAL;
896 }
897
898 vid = nla_get_u16(tb[NDA_VLAN]);
899
Toshiaki Makita8adff412013-10-16 17:07:13 +0900900 if (!vid || vid >= VLAN_VID_MASK) {
Vlad Yasevich1690be62013-02-13 12:00:18 +0000901 pr_info("bridge: RTM_NEWNEIGH with invalid vlan id %d\n",
902 vid);
903 return -EINVAL;
904 }
905 }
stephen hemminger36fd2b62011-04-04 14:03:31 +0000906 p = br_port_get_rtnl(dev);
907 if (p == NULL) {
908 pr_info("bridge: RTM_DELNEIGH %s not a bridge port\n",
909 dev->name);
910 return -EINVAL;
911 }
912
Vlad Yasevich1690be62013-02-13 12:00:18 +0000913 pv = nbp_get_vlan_info(p);
914 if (vid != VLAN_N_VID) {
915 if (!pv || !test_bit(vid, pv->vlan_bitmap)) {
916 pr_info("bridge: RTM_DELNEIGH with unconfigured "
917 "vlan %d on port %s\n", vid, dev->name);
918 return -EINVAL;
919 }
stephen hemminger36fd2b62011-04-04 14:03:31 +0000920
Vlad Yasevich1690be62013-02-13 12:00:18 +0000921 err = __br_fdb_delete(p, addr, vid);
922 } else {
Toshiaki Makitaef40b7e2013-08-20 17:10:18 +0900923 if (!pv || bitmap_empty(pv->vlan_bitmap, VLAN_N_VID)) {
Vlad Yasevich1690be62013-02-13 12:00:18 +0000924 err = __br_fdb_delete(p, addr, 0);
925 goto out;
926 }
927
928 /* We have vlans configured on this port and user didn't
929 * specify a VLAN. To be nice, add/update entry for every
930 * vlan on this port.
931 */
932 err = -ENOENT;
Toshiaki Makitaef40b7e2013-08-20 17:10:18 +0900933 for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID) {
Vlad Yasevich1690be62013-02-13 12:00:18 +0000934 err &= __br_fdb_delete(p, addr, vid);
Vlad Yasevich1690be62013-02-13 12:00:18 +0000935 }
936 }
937out:
stephen hemminger36fd2b62011-04-04 14:03:31 +0000938 return err;
939}
Vlad Yasevich8db24af2014-05-16 09:59:17 -0400940
941int br_fdb_sync_static(struct net_bridge *br, struct net_bridge_port *p)
942{
943 struct net_bridge_fdb_entry *fdb, *tmp;
944 int i;
945 int err;
946
947 ASSERT_RTNL();
948
949 for (i = 0; i < BR_HASH_SIZE; i++) {
950 hlist_for_each_entry(fdb, &br->hash[i], hlist) {
951 /* We only care for static entries */
952 if (!fdb->is_static)
953 continue;
954
955 err = dev_uc_add(p->dev, fdb->addr.addr);
956 if (err)
957 goto rollback;
958 }
959 }
960 return 0;
961
962rollback:
963 for (i = 0; i < BR_HASH_SIZE; i++) {
964 hlist_for_each_entry(tmp, &br->hash[i], hlist) {
965 /* If we reached the fdb that failed, we can stop */
966 if (tmp == fdb)
967 break;
968
969 /* We only care for static entries */
970 if (!tmp->is_static)
971 continue;
972
973 dev_uc_del(p->dev, tmp->addr.addr);
974 }
975 }
976 return err;
977}
978
979void br_fdb_unsync_static(struct net_bridge *br, struct net_bridge_port *p)
980{
981 struct net_bridge_fdb_entry *fdb;
982 int i;
983
984 ASSERT_RTNL();
985
986 for (i = 0; i < BR_HASH_SIZE; i++) {
987 hlist_for_each_entry_rcu(fdb, &br->hash[i], hlist) {
988 /* We only care for static entries */
989 if (!fdb->is_static)
990 continue;
991
992 dev_uc_del(p->dev, fdb->addr.addr);
993 }
994 }
995}