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