bridge: Fix the way to check if a local fdb entry can be deleted
[deliverable/linux.git] / net / bridge / br_fdb.c
CommitLineData
1da177e4
LT
1/*
2 * Forwarding database
3 * Linux ethernet bridge
4 *
5 * Authors:
6 * Lennert Buytenhek <buytenh@gnu.org>
7 *
1da177e4
LT
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>
82524746 16#include <linux/rculist.h>
1da177e4
LT
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>
3f890923 22#include <linux/random.h>
5a0e3ad6 23#include <linux/slab.h>
60063497 24#include <linux/atomic.h>
3f890923 25#include <asm/unaligned.h>
2ba071ec 26#include <linux/if_vlan.h>
1da177e4
LT
27#include "br_private.h"
28
e18b890b 29static struct kmem_cache *br_fdb_cache __read_mostly;
1da177e4 30static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
bc9a25d2 31 const unsigned char *addr, u16 vid);
31e8a49c 32static void fdb_notify(struct net_bridge *br,
33 const struct net_bridge_fdb_entry *, int);
1da177e4 34
3f890923
SH
35static u32 fdb_salt __read_mostly;
36
87a596e0 37int __init br_fdb_init(void)
1da177e4
LT
38{
39 br_fdb_cache = kmem_cache_create("bridge_fdb_cache",
40 sizeof(struct net_bridge_fdb_entry),
41 0,
20c2df83 42 SLAB_HWCACHE_ALIGN, NULL);
87a596e0
AM
43 if (!br_fdb_cache)
44 return -ENOMEM;
45
3f890923 46 get_random_bytes(&fdb_salt, sizeof(fdb_salt));
87a596e0 47 return 0;
1da177e4
LT
48}
49
73afc906 50void br_fdb_fini(void)
1da177e4
LT
51{
52 kmem_cache_destroy(br_fdb_cache);
53}
54
55
56/* if topology_changing then use forward_delay (default 15 sec)
57 * otherwise keep longer (default 5 minutes)
58 */
3f890923 59static inline unsigned long hold_time(const struct net_bridge *br)
1da177e4
LT
60{
61 return br->topology_change ? br->forward_delay : br->ageing_time;
62}
63
3f890923 64static inline int has_expired(const struct net_bridge *br,
1da177e4
LT
65 const struct net_bridge_fdb_entry *fdb)
66{
f64f9e71 67 return !fdb->is_static &&
7cd8861a 68 time_before_eq(fdb->updated + hold_time(br), jiffies);
1da177e4
LT
69}
70
2ba071ec 71static inline int br_mac_hash(const unsigned char *mac, __u16 vid)
1da177e4 72{
2ba071ec 73 /* use 1 byte of OUI and 3 bytes of NIC */
3f890923 74 u32 key = get_unaligned((u32 *)(mac + 2));
2ba071ec 75 return jhash_2words(key, vid, fdb_salt) & (BR_HASH_SIZE - 1);
1da177e4
LT
76}
77
da678292
MM
78static void fdb_rcu_free(struct rcu_head *head)
79{
80 struct net_bridge_fdb_entry *ent
81 = container_of(head, struct net_bridge_fdb_entry, rcu);
82 kmem_cache_free(br_fdb_cache, ent);
83}
84
31e8a49c 85static void fdb_delete(struct net_bridge *br, struct net_bridge_fdb_entry *f)
1da177e4
LT
86{
87 hlist_del_rcu(&f->hlist);
31e8a49c 88 fdb_notify(br, f, RTM_DELNEIGH);
da678292 89 call_rcu(&f->rcu, fdb_rcu_free);
1da177e4
LT
90}
91
92void br_fdb_changeaddr(struct net_bridge_port *p, const unsigned char *newaddr)
93{
94 struct net_bridge *br = p->br;
2836882f
TM
95 struct net_port_vlans *pv = nbp_get_vlan_info(p);
96 bool no_vlan = !pv;
1da177e4 97 int i;
2836882f 98 u16 vid;
9d6f229f 99
1da177e4
LT
100 spin_lock_bh(&br->hash_lock);
101
102 /* Search all chains since old address/hash is unknown */
103 for (i = 0; i < BR_HASH_SIZE; i++) {
104 struct hlist_node *h;
105 hlist_for_each(h, &br->hash[i]) {
106 struct net_bridge_fdb_entry *f;
107
108 f = hlist_entry(h, struct net_bridge_fdb_entry, hlist);
a5642ab4 109 if (f->dst == p && f->is_local && !f->added_by_user) {
1da177e4
LT
110 /* maybe another port has same hw addr? */
111 struct net_bridge_port *op;
bc9a25d2 112 u16 vid = f->vlan_id;
1da177e4 113 list_for_each_entry(op, &br->port_list, list) {
9d6f229f 114 if (op != p &&
9a7b6ef9 115 ether_addr_equal(op->dev->dev_addr,
bc9a25d2 116 f->addr.addr) &&
2b292fb4 117 (!vid || nbp_vlan_find(op, vid))) {
1da177e4 118 f->dst = op;
2836882f 119 goto skip_delete;
1da177e4
LT
120 }
121 }
122
2b292fb4
TM
123 /* maybe bridge device has same hw addr? */
124 if (ether_addr_equal(br->dev->dev_addr,
125 f->addr.addr) &&
126 (!vid || br_vlan_find(br, vid))) {
127 f->dst = NULL;
128 goto skip_delete;
129 }
130
1da177e4 131 /* delete old one */
31e8a49c 132 fdb_delete(br, f);
2836882f 133skip_delete:
bc9a25d2
VY
134 /* if this port has no vlan information
135 * configured, we can safely be done at
136 * this point.
137 */
138 if (no_vlan)
2836882f 139 goto insert;
1da177e4
LT
140 }
141 }
142 }
1da177e4 143
2836882f
TM
144insert:
145 /* insert new address, may fail if invalid address or dup. */
146 fdb_insert(br, p, newaddr, 0);
147
148 if (no_vlan)
149 goto done;
150
151 /* Now add entries for every VLAN configured on the port.
152 * This function runs under RTNL so the bitmap will not change
153 * from under us.
154 */
155 for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID)
156 fdb_insert(br, p, newaddr, vid);
157
bc9a25d2 158done:
1da177e4
LT
159 spin_unlock_bh(&br->hash_lock);
160}
161
43598813 162void br_fdb_change_mac_address(struct net_bridge *br, const u8 *newaddr)
163{
164 struct net_bridge_fdb_entry *f;
bc9a25d2
VY
165 struct net_port_vlans *pv;
166 u16 vid = 0;
43598813 167
168 /* If old entry was unassociated with any port, then delete it. */
2ba071ec 169 f = __br_fdb_get(br, br->dev->dev_addr, 0);
43598813 170 if (f && f->is_local && !f->dst)
171 fdb_delete(br, f);
172
bc9a25d2
VY
173 fdb_insert(br, NULL, newaddr, 0);
174
175 /* Now remove and add entries for every VLAN configured on the
176 * bridge. This function runs under RTNL so the bitmap will not
177 * change from under us.
178 */
179 pv = br_get_vlan_info(br);
180 if (!pv)
181 return;
182
ef40b7ef 183 for_each_set_bit_from(vid, pv->vlan_bitmap, VLAN_N_VID) {
bc9a25d2
VY
184 f = __br_fdb_get(br, br->dev->dev_addr, vid);
185 if (f && f->is_local && !f->dst)
186 fdb_delete(br, f);
187 fdb_insert(br, NULL, newaddr, vid);
188 }
43598813 189}
190
1da177e4
LT
191void br_fdb_cleanup(unsigned long _data)
192{
193 struct net_bridge *br = (struct net_bridge *)_data;
194 unsigned long delay = hold_time(br);
25442e06 195 unsigned long next_timer = jiffies + br->ageing_time;
1da177e4
LT
196 int i;
197
27a42938 198 spin_lock(&br->hash_lock);
1da177e4
LT
199 for (i = 0; i < BR_HASH_SIZE; i++) {
200 struct net_bridge_fdb_entry *f;
b67bfe0d 201 struct hlist_node *n;
1da177e4 202
b67bfe0d 203 hlist_for_each_entry_safe(f, n, &br->hash[i], hlist) {
071f7722
BE
204 unsigned long this_timer;
205 if (f->is_static)
206 continue;
7cd8861a 207 this_timer = f->updated + delay;
071f7722 208 if (time_before_eq(this_timer, jiffies))
31e8a49c 209 fdb_delete(br, f);
2bec008c 210 else if (time_before(this_timer, next_timer))
071f7722 211 next_timer = this_timer;
1da177e4
LT
212 }
213 }
27a42938 214 spin_unlock(&br->hash_lock);
1da177e4 215
25442e06 216 mod_timer(&br->gc_timer, round_jiffies_up(next_timer));
1da177e4
LT
217}
218
9cf63747
SH
219/* Completely flush all dynamic entries in forwarding database.*/
220void br_fdb_flush(struct net_bridge *br)
221{
222 int i;
223
224 spin_lock_bh(&br->hash_lock);
225 for (i = 0; i < BR_HASH_SIZE; i++) {
226 struct net_bridge_fdb_entry *f;
b67bfe0d
SL
227 struct hlist_node *n;
228 hlist_for_each_entry_safe(f, n, &br->hash[i], hlist) {
9cf63747 229 if (!f->is_static)
31e8a49c 230 fdb_delete(br, f);
9cf63747
SH
231 }
232 }
233 spin_unlock_bh(&br->hash_lock);
234}
1a620698 235
25985edc 236/* Flush all entries referring to a specific port.
9cf63747
SH
237 * if do_all is set also flush static entries
238 */
1a620698
SH
239void br_fdb_delete_by_port(struct net_bridge *br,
240 const struct net_bridge_port *p,
241 int do_all)
1da177e4
LT
242{
243 int i;
244
245 spin_lock_bh(&br->hash_lock);
246 for (i = 0; i < BR_HASH_SIZE; i++) {
247 struct hlist_node *h, *g;
9d6f229f 248
1da177e4
LT
249 hlist_for_each_safe(h, g, &br->hash[i]) {
250 struct net_bridge_fdb_entry *f
251 = hlist_entry(h, struct net_bridge_fdb_entry, hlist);
9d6f229f 252 if (f->dst != p)
1da177e4
LT
253 continue;
254
1a620698
SH
255 if (f->is_static && !do_all)
256 continue;
1da177e4
LT
257 /*
258 * if multiple ports all have the same device address
259 * then when one port is deleted, assign
260 * the local entry to other port
261 */
262 if (f->is_local) {
263 struct net_bridge_port *op;
264 list_for_each_entry(op, &br->port_list, list) {
9d6f229f 265 if (op != p &&
9a7b6ef9
JP
266 ether_addr_equal(op->dev->dev_addr,
267 f->addr.addr)) {
1da177e4 268 f->dst = op;
a5642ab4 269 f->added_by_user = 0;
1da177e4
LT
270 goto skip_delete;
271 }
272 }
273 }
274
31e8a49c 275 fdb_delete(br, f);
1da177e4
LT
276 skip_delete: ;
277 }
278 }
279 spin_unlock_bh(&br->hash_lock);
280}
281
eeaf61d8 282/* No locking or refcounting, assumes caller has rcu_read_lock */
1da177e4 283struct net_bridge_fdb_entry *__br_fdb_get(struct net_bridge *br,
2ba071ec
VY
284 const unsigned char *addr,
285 __u16 vid)
1da177e4 286{
1da177e4
LT
287 struct net_bridge_fdb_entry *fdb;
288
b67bfe0d 289 hlist_for_each_entry_rcu(fdb,
2ba071ec
VY
290 &br->hash[br_mac_hash(addr, vid)], hlist) {
291 if (ether_addr_equal(fdb->addr.addr, addr) &&
292 fdb->vlan_id == vid) {
1da177e4
LT
293 if (unlikely(has_expired(br, fdb)))
294 break;
295 return fdb;
296 }
297 }
298
299 return NULL;
300}
301
e6373c4c 302#if IS_ENABLED(CONFIG_ATM_LANE)
da678292
MM
303/* Interface used by ATM LANE hook to test
304 * if an addr is on some other bridge port */
305int br_fdb_test_addr(struct net_device *dev, unsigned char *addr)
1da177e4
LT
306{
307 struct net_bridge_fdb_entry *fdb;
b5ed54e9 308 struct net_bridge_port *port;
da678292
MM
309 int ret;
310
1da177e4 311 rcu_read_lock();
b5ed54e9 312 port = br_port_get_rcu(dev);
313 if (!port)
314 ret = 0;
315 else {
2ba071ec 316 fdb = __br_fdb_get(port->br, addr, 0);
43598813 317 ret = fdb && fdb->dst && fdb->dst->dev != dev &&
b5ed54e9 318 fdb->dst->state == BR_STATE_FORWARDING;
319 }
1da177e4 320 rcu_read_unlock();
1da177e4 321
da678292 322 return ret;
1da177e4 323}
da678292 324#endif /* CONFIG_ATM_LANE */
1da177e4
LT
325
326/*
9d6f229f 327 * Fill buffer with forwarding table records in
1da177e4
LT
328 * the API format.
329 */
330int br_fdb_fillbuf(struct net_bridge *br, void *buf,
331 unsigned long maxnum, unsigned long skip)
332{
333 struct __fdb_entry *fe = buf;
334 int i, num = 0;
1da177e4
LT
335 struct net_bridge_fdb_entry *f;
336
337 memset(buf, 0, maxnum*sizeof(struct __fdb_entry));
338
339 rcu_read_lock();
340 for (i = 0; i < BR_HASH_SIZE; i++) {
b67bfe0d 341 hlist_for_each_entry_rcu(f, &br->hash[i], hlist) {
1da177e4
LT
342 if (num >= maxnum)
343 goto out;
344
9d6f229f 345 if (has_expired(br, f))
1da177e4
LT
346 continue;
347
43598813 348 /* ignore pseudo entry for local MAC address */
349 if (!f->dst)
350 continue;
351
1da177e4
LT
352 if (skip) {
353 --skip;
354 continue;
355 }
356
357 /* convert from internal format to API */
358 memcpy(fe->mac_addr, f->addr.addr, ETH_ALEN);
ae4f8fca
SH
359
360 /* due to ABI compat need to split into hi/lo */
1da177e4 361 fe->port_no = f->dst->port_no;
ae4f8fca
SH
362 fe->port_hi = f->dst->port_no >> 8;
363
1da177e4
LT
364 fe->is_local = f->is_local;
365 if (!f->is_static)
a399a805 366 fe->ageing_timer_value = jiffies_delta_to_clock_t(jiffies - f->updated);
1da177e4
LT
367 ++fe;
368 ++num;
369 }
370 }
371
372 out:
373 rcu_read_unlock();
374
375 return num;
376}
377
664de48b 378static struct net_bridge_fdb_entry *fdb_find(struct hlist_head *head,
2ba071ec
VY
379 const unsigned char *addr,
380 __u16 vid)
664de48b 381{
664de48b 382 struct net_bridge_fdb_entry *fdb;
383
b67bfe0d 384 hlist_for_each_entry(fdb, head, hlist) {
2ba071ec
VY
385 if (ether_addr_equal(fdb->addr.addr, addr) &&
386 fdb->vlan_id == vid)
664de48b 387 return fdb;
388 }
389 return NULL;
390}
391
392static struct net_bridge_fdb_entry *fdb_find_rcu(struct hlist_head *head,
2ba071ec
VY
393 const unsigned char *addr,
394 __u16 vid)
1da177e4 395{
1da177e4
LT
396 struct net_bridge_fdb_entry *fdb;
397
b67bfe0d 398 hlist_for_each_entry_rcu(fdb, head, hlist) {
2ba071ec
VY
399 if (ether_addr_equal(fdb->addr.addr, addr) &&
400 fdb->vlan_id == vid)
1da177e4
LT
401 return fdb;
402 }
403 return NULL;
404}
405
406static struct net_bridge_fdb_entry *fdb_create(struct hlist_head *head,
407 struct net_bridge_port *source,
2ba071ec
VY
408 const unsigned char *addr,
409 __u16 vid)
1da177e4
LT
410{
411 struct net_bridge_fdb_entry *fdb;
412
413 fdb = kmem_cache_alloc(br_fdb_cache, GFP_ATOMIC);
414 if (fdb) {
415 memcpy(fdb->addr.addr, addr, ETH_ALEN);
1da177e4 416 fdb->dst = source;
2ba071ec 417 fdb->vlan_id = vid;
03e9b64b 418 fdb->is_local = 0;
419 fdb->is_static = 0;
a5642ab4 420 fdb->added_by_user = 0;
7cd8861a 421 fdb->updated = fdb->used = jiffies;
1158f762 422 hlist_add_head_rcu(&fdb->hlist, head);
1da177e4
LT
423 }
424 return fdb;
425}
426
427static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
bc9a25d2 428 const unsigned char *addr, u16 vid)
1da177e4 429{
bc9a25d2 430 struct hlist_head *head = &br->hash[br_mac_hash(addr, vid)];
1da177e4
LT
431 struct net_bridge_fdb_entry *fdb;
432
433 if (!is_valid_ether_addr(addr))
434 return -EINVAL;
435
bc9a25d2 436 fdb = fdb_find(head, addr, vid);
1da177e4 437 if (fdb) {
9d6f229f 438 /* it is okay to have multiple ports with same
1da177e4
LT
439 * address, just use the first one.
440 */
9d6f229f 441 if (fdb->is_local)
1da177e4 442 return 0;
28a16c97 443 br_warn(br, "adding interface %s with same address "
1da177e4 444 "as a received packet\n",
9b46922e 445 source ? source->dev->name : br->dev->name);
31e8a49c 446 fdb_delete(br, fdb);
9d6f229f 447 }
1da177e4 448
bc9a25d2 449 fdb = fdb_create(head, source, addr, vid);
03e9b64b 450 if (!fdb)
1da177e4
LT
451 return -ENOMEM;
452
03e9b64b 453 fdb->is_local = fdb->is_static = 1;
31e8a49c 454 fdb_notify(br, fdb, RTM_NEWNEIGH);
1da177e4
LT
455 return 0;
456}
457
03e9b64b 458/* Add entry for local address of interface */
1da177e4 459int br_fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
bc9a25d2 460 const unsigned char *addr, u16 vid)
1da177e4
LT
461{
462 int ret;
463
464 spin_lock_bh(&br->hash_lock);
bc9a25d2 465 ret = fdb_insert(br, source, addr, vid);
1da177e4
LT
466 spin_unlock_bh(&br->hash_lock);
467 return ret;
468}
469
470void br_fdb_update(struct net_bridge *br, struct net_bridge_port *source,
a5642ab4 471 const unsigned char *addr, u16 vid, bool added_by_user)
1da177e4 472{
2ba071ec 473 struct hlist_head *head = &br->hash[br_mac_hash(addr, vid)];
1da177e4
LT
474 struct net_bridge_fdb_entry *fdb;
475
476 /* some users want to always flood. */
477 if (hold_time(br) == 0)
478 return;
479
df1c0b84
SH
480 /* ignore packets unless we are using this port */
481 if (!(source->state == BR_STATE_LEARNING ||
482 source->state == BR_STATE_FORWARDING))
483 return;
484
2ba071ec 485 fdb = fdb_find_rcu(head, addr, vid);
1da177e4
LT
486 if (likely(fdb)) {
487 /* attempt to update an entry for a local interface */
488 if (unlikely(fdb->is_local)) {
9d6f229f 489 if (net_ratelimit())
28a16c97 490 br_warn(br, "received packet on %s with "
491 "own address as source address\n",
492 source->dev->name);
1da177e4
LT
493 } else {
494 /* fastpath: update of existing entry */
495 fdb->dst = source;
7cd8861a 496 fdb->updated = jiffies;
a5642ab4
TM
497 if (unlikely(added_by_user))
498 fdb->added_by_user = 1;
1da177e4
LT
499 }
500 } else {
f8ae737d 501 spin_lock(&br->hash_lock);
2ba071ec
VY
502 if (likely(!fdb_find(head, addr, vid))) {
503 fdb = fdb_create(head, source, addr, vid);
a5642ab4
TM
504 if (fdb) {
505 if (unlikely(added_by_user))
506 fdb->added_by_user = 1;
31e8a49c 507 fdb_notify(br, fdb, RTM_NEWNEIGH);
a5642ab4 508 }
f58ee4e1 509 }
1da177e4
LT
510 /* else we lose race and someone else inserts
511 * it first, don't bother updating
512 */
f8ae737d 513 spin_unlock(&br->hash_lock);
1da177e4 514 }
1da177e4 515}
b078f0df 516
517static int fdb_to_nud(const struct net_bridge_fdb_entry *fdb)
518{
519 if (fdb->is_local)
520 return NUD_PERMANENT;
521 else if (fdb->is_static)
522 return NUD_NOARP;
523 else if (has_expired(fdb->dst->br, fdb))
524 return NUD_STALE;
525 else
526 return NUD_REACHABLE;
527}
528
31e8a49c 529static int fdb_fill_info(struct sk_buff *skb, const struct net_bridge *br,
b078f0df 530 const struct net_bridge_fdb_entry *fdb,
15e47304 531 u32 portid, u32 seq, int type, unsigned int flags)
b078f0df 532{
533 unsigned long now = jiffies;
534 struct nda_cacheinfo ci;
535 struct nlmsghdr *nlh;
536 struct ndmsg *ndm;
537
15e47304 538 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
b078f0df 539 if (nlh == NULL)
540 return -EMSGSIZE;
541
b078f0df 542 ndm = nlmsg_data(nlh);
543 ndm->ndm_family = AF_BRIDGE;
544 ndm->ndm_pad1 = 0;
545 ndm->ndm_pad2 = 0;
546 ndm->ndm_flags = 0;
547 ndm->ndm_type = 0;
43598813 548 ndm->ndm_ifindex = fdb->dst ? fdb->dst->dev->ifindex : br->dev->ifindex;
b078f0df 549 ndm->ndm_state = fdb_to_nud(fdb);
550
2eb812e6
DM
551 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->addr))
552 goto nla_put_failure;
b078f0df 553 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
554 ci.ndm_confirmed = 0;
555 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
556 ci.ndm_refcnt = 0;
2eb812e6
DM
557 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
558 goto nla_put_failure;
1690be63
VY
559
560 if (nla_put(skb, NDA_VLAN, sizeof(u16), &fdb->vlan_id))
561 goto nla_put_failure;
562
b078f0df 563 return nlmsg_end(skb, nlh);
564
565nla_put_failure:
566 nlmsg_cancel(skb, nlh);
567 return -EMSGSIZE;
568}
569
570static inline size_t fdb_nlmsg_size(void)
571{
572 return NLMSG_ALIGN(sizeof(struct ndmsg))
573 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
1690be63 574 + nla_total_size(sizeof(u16)) /* NDA_VLAN */
b078f0df 575 + nla_total_size(sizeof(struct nda_cacheinfo));
576}
577
31e8a49c 578static void fdb_notify(struct net_bridge *br,
579 const struct net_bridge_fdb_entry *fdb, int type)
b078f0df 580{
31e8a49c 581 struct net *net = dev_net(br->dev);
b078f0df 582 struct sk_buff *skb;
583 int err = -ENOBUFS;
584
585 skb = nlmsg_new(fdb_nlmsg_size(), GFP_ATOMIC);
586 if (skb == NULL)
587 goto errout;
588
31e8a49c 589 err = fdb_fill_info(skb, br, fdb, 0, 0, type, 0);
b078f0df 590 if (err < 0) {
591 /* -EMSGSIZE implies BUG in fdb_nlmsg_size() */
592 WARN_ON(err == -EMSGSIZE);
593 kfree_skb(skb);
594 goto errout;
595 }
596 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
597 return;
598errout:
87e823b3 599 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
b078f0df 600}
601
602/* Dump information about entries, in response to GETNEIGH */
77162022
JF
603int br_fdb_dump(struct sk_buff *skb,
604 struct netlink_callback *cb,
605 struct net_device *dev,
606 int idx)
b078f0df 607{
77162022
JF
608 struct net_bridge *br = netdev_priv(dev);
609 int i;
b078f0df 610
77162022
JF
611 if (!(dev->priv_flags & IFF_EBRIDGE))
612 goto out;
b078f0df 613
77162022 614 for (i = 0; i < BR_HASH_SIZE; i++) {
77162022 615 struct net_bridge_fdb_entry *f;
b078f0df 616
b67bfe0d 617 hlist_for_each_entry_rcu(f, &br->hash[i], hlist) {
77162022
JF
618 if (idx < cb->args[0])
619 goto skip;
620
621 if (fdb_fill_info(skb, br, f,
15e47304 622 NETLINK_CB(cb->skb).portid,
77162022
JF
623 cb->nlh->nlmsg_seq,
624 RTM_NEWNEIGH,
625 NLM_F_MULTI) < 0)
626 break;
b078f0df 627skip:
77162022 628 ++idx;
b078f0df 629 }
630 }
b078f0df 631
77162022
JF
632out:
633 return idx;
b078f0df 634}
36fd2b63 635
292d1398 636/* Update (create or replace) forwarding database entry */
36fd2b63 637static int fdb_add_entry(struct net_bridge_port *source, const __u8 *addr,
2ba071ec 638 __u16 state, __u16 flags, __u16 vid)
36fd2b63 639{
640 struct net_bridge *br = source->br;
2ba071ec 641 struct hlist_head *head = &br->hash[br_mac_hash(addr, vid)];
36fd2b63 642 struct net_bridge_fdb_entry *fdb;
b0a397fb 643 bool modified = false;
36fd2b63 644
2ba071ec 645 fdb = fdb_find(head, addr, vid);
64af1bac 646 if (fdb == NULL) {
647 if (!(flags & NLM_F_CREATE))
648 return -ENOENT;
36fd2b63 649
2ba071ec 650 fdb = fdb_create(head, source, addr, vid);
64af1bac 651 if (!fdb)
652 return -ENOMEM;
b0a397fb 653
654 modified = true;
64af1bac 655 } else {
656 if (flags & NLM_F_EXCL)
657 return -EEXIST;
b0a397fb 658
659 if (fdb->dst != source) {
660 fdb->dst = source;
661 modified = true;
662 }
292d1398 663 }
664
665 if (fdb_to_nud(fdb) != state) {
666 if (state & NUD_PERMANENT)
667 fdb->is_local = fdb->is_static = 1;
668 else if (state & NUD_NOARP) {
669 fdb->is_local = 0;
670 fdb->is_static = 1;
671 } else
672 fdb->is_local = fdb->is_static = 0;
64af1bac 673
b0a397fb 674 modified = true;
675 }
a5642ab4 676 fdb->added_by_user = 1;
b0a397fb 677
678 fdb->used = jiffies;
679 if (modified) {
680 fdb->updated = jiffies;
31e8a49c 681 fdb_notify(br, fdb, RTM_NEWNEIGH);
64af1bac 682 }
36fd2b63 683
36fd2b63 684 return 0;
685}
686
1690be63
VY
687static int __br_fdb_add(struct ndmsg *ndm, struct net_bridge_port *p,
688 const unsigned char *addr, u16 nlh_flags, u16 vid)
689{
690 int err = 0;
691
692 if (ndm->ndm_flags & NTF_USE) {
693 rcu_read_lock();
a5642ab4 694 br_fdb_update(p->br, p, addr, vid, true);
1690be63
VY
695 rcu_read_unlock();
696 } else {
697 spin_lock_bh(&p->br->hash_lock);
698 err = fdb_add_entry(p, addr, ndm->ndm_state,
699 nlh_flags, vid);
700 spin_unlock_bh(&p->br->hash_lock);
701 }
702
703 return err;
704}
705
36fd2b63 706/* Add new permanent fdb entry with RTM_NEWNEIGH */
edc7d573 707int br_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
708 struct net_device *dev,
6b6e2725 709 const unsigned char *addr, u16 nlh_flags)
36fd2b63 710{
36fd2b63 711 struct net_bridge_port *p;
77162022 712 int err = 0;
1690be63
VY
713 struct net_port_vlans *pv;
714 unsigned short vid = VLAN_N_VID;
36fd2b63 715
292d1398 716 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE))) {
717 pr_info("bridge: RTM_NEWNEIGH with invalid state %#x\n", ndm->ndm_state);
718 return -EINVAL;
719 }
720
1690be63
VY
721 if (tb[NDA_VLAN]) {
722 if (nla_len(tb[NDA_VLAN]) != sizeof(unsigned short)) {
723 pr_info("bridge: RTM_NEWNEIGH with invalid vlan\n");
724 return -EINVAL;
725 }
726
727 vid = nla_get_u16(tb[NDA_VLAN]);
728
8adff41c 729 if (!vid || vid >= VLAN_VID_MASK) {
1690be63
VY
730 pr_info("bridge: RTM_NEWNEIGH with invalid vlan id %d\n",
731 vid);
732 return -EINVAL;
733 }
734 }
735
537f7f84
SH
736 if (is_zero_ether_addr(addr)) {
737 pr_info("bridge: RTM_NEWNEIGH with invalid ether address\n");
738 return -EINVAL;
739 }
740
36fd2b63 741 p = br_port_get_rtnl(dev);
742 if (p == NULL) {
743 pr_info("bridge: RTM_NEWNEIGH %s not a bridge port\n",
744 dev->name);
745 return -EINVAL;
746 }
747
1690be63
VY
748 pv = nbp_get_vlan_info(p);
749 if (vid != VLAN_N_VID) {
750 if (!pv || !test_bit(vid, pv->vlan_bitmap)) {
751 pr_info("bridge: RTM_NEWNEIGH with unconfigured "
752 "vlan %d on port %s\n", vid, dev->name);
753 return -EINVAL;
754 }
755
756 /* VID was specified, so use it. */
757 err = __br_fdb_add(ndm, p, addr, nlh_flags, vid);
292d1398 758 } else {
ef40b7ef 759 if (!pv || bitmap_empty(pv->vlan_bitmap, VLAN_N_VID)) {
1690be63
VY
760 err = __br_fdb_add(ndm, p, addr, nlh_flags, 0);
761 goto out;
762 }
763
764 /* We have vlans configured on this port and user didn't
765 * specify a VLAN. To be nice, add/update entry for every
766 * vlan on this port.
767 */
ef40b7ef 768 for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID) {
1690be63
VY
769 err = __br_fdb_add(ndm, p, addr, nlh_flags, vid);
770 if (err)
771 goto out;
1690be63 772 }
292d1398 773 }
36fd2b63 774
1690be63 775out:
36fd2b63 776 return err;
777}
778
bc9a25d2
VY
779int fdb_delete_by_addr(struct net_bridge *br, const u8 *addr,
780 u16 vlan)
36fd2b63 781{
1690be63 782 struct hlist_head *head = &br->hash[br_mac_hash(addr, vlan)];
36fd2b63 783 struct net_bridge_fdb_entry *fdb;
784
1690be63 785 fdb = fdb_find(head, addr, vlan);
36fd2b63 786 if (!fdb)
787 return -ENOENT;
788
1690be63 789 fdb_delete(br, fdb);
36fd2b63 790 return 0;
791}
792
1690be63
VY
793static int __br_fdb_delete(struct net_bridge_port *p,
794 const unsigned char *addr, u16 vid)
795{
796 int err;
797
798 spin_lock_bh(&p->br->hash_lock);
799 err = fdb_delete_by_addr(p->br, addr, vid);
800 spin_unlock_bh(&p->br->hash_lock);
801
802 return err;
803}
804
36fd2b63 805/* Remove neighbor entry with RTM_DELNEIGH */
1690be63
VY
806int br_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
807 struct net_device *dev,
6b6e2725 808 const unsigned char *addr)
36fd2b63 809{
36fd2b63 810 struct net_bridge_port *p;
36fd2b63 811 int err;
1690be63
VY
812 struct net_port_vlans *pv;
813 unsigned short vid = VLAN_N_VID;
36fd2b63 814
1690be63
VY
815 if (tb[NDA_VLAN]) {
816 if (nla_len(tb[NDA_VLAN]) != sizeof(unsigned short)) {
817 pr_info("bridge: RTM_NEWNEIGH with invalid vlan\n");
818 return -EINVAL;
819 }
820
821 vid = nla_get_u16(tb[NDA_VLAN]);
822
8adff41c 823 if (!vid || vid >= VLAN_VID_MASK) {
1690be63
VY
824 pr_info("bridge: RTM_NEWNEIGH with invalid vlan id %d\n",
825 vid);
826 return -EINVAL;
827 }
828 }
36fd2b63 829 p = br_port_get_rtnl(dev);
830 if (p == NULL) {
831 pr_info("bridge: RTM_DELNEIGH %s not a bridge port\n",
832 dev->name);
833 return -EINVAL;
834 }
835
1690be63
VY
836 pv = nbp_get_vlan_info(p);
837 if (vid != VLAN_N_VID) {
838 if (!pv || !test_bit(vid, pv->vlan_bitmap)) {
839 pr_info("bridge: RTM_DELNEIGH with unconfigured "
840 "vlan %d on port %s\n", vid, dev->name);
841 return -EINVAL;
842 }
36fd2b63 843
1690be63
VY
844 err = __br_fdb_delete(p, addr, vid);
845 } else {
ef40b7ef 846 if (!pv || bitmap_empty(pv->vlan_bitmap, VLAN_N_VID)) {
1690be63
VY
847 err = __br_fdb_delete(p, addr, 0);
848 goto out;
849 }
850
851 /* We have vlans configured on this port and user didn't
852 * specify a VLAN. To be nice, add/update entry for every
853 * vlan on this port.
854 */
855 err = -ENOENT;
ef40b7ef 856 for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID) {
1690be63 857 err &= __br_fdb_delete(p, addr, vid);
1690be63
VY
858 }
859 }
860out:
36fd2b63 861 return err;
862}
This page took 0.756818 seconds and 5 git commands to generate.