bridge: Fix the way to check if a local fdb entry can be deleted
[deliverable/linux.git] / net / bridge / br_fdb.c
1 /*
2 * Forwarding database
3 * Linux ethernet bridge
4 *
5 * Authors:
6 * Lennert Buytenhek <buytenh@gnu.org>
7 *
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>
16 #include <linux/rculist.h>
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>
22 #include <linux/random.h>
23 #include <linux/slab.h>
24 #include <linux/atomic.h>
25 #include <asm/unaligned.h>
26 #include <linux/if_vlan.h>
27 #include "br_private.h"
28
29 static struct kmem_cache *br_fdb_cache __read_mostly;
30 static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
31 const unsigned char *addr, u16 vid);
32 static void fdb_notify(struct net_bridge *br,
33 const struct net_bridge_fdb_entry *, int);
34
35 static u32 fdb_salt __read_mostly;
36
37 int __init br_fdb_init(void)
38 {
39 br_fdb_cache = kmem_cache_create("bridge_fdb_cache",
40 sizeof(struct net_bridge_fdb_entry),
41 0,
42 SLAB_HWCACHE_ALIGN, NULL);
43 if (!br_fdb_cache)
44 return -ENOMEM;
45
46 get_random_bytes(&fdb_salt, sizeof(fdb_salt));
47 return 0;
48 }
49
50 void br_fdb_fini(void)
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 */
59 static inline unsigned long hold_time(const struct net_bridge *br)
60 {
61 return br->topology_change ? br->forward_delay : br->ageing_time;
62 }
63
64 static inline int has_expired(const struct net_bridge *br,
65 const struct net_bridge_fdb_entry *fdb)
66 {
67 return !fdb->is_static &&
68 time_before_eq(fdb->updated + hold_time(br), jiffies);
69 }
70
71 static inline int br_mac_hash(const unsigned char *mac, __u16 vid)
72 {
73 /* use 1 byte of OUI and 3 bytes of NIC */
74 u32 key = get_unaligned((u32 *)(mac + 2));
75 return jhash_2words(key, vid, fdb_salt) & (BR_HASH_SIZE - 1);
76 }
77
78 static 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
85 static void fdb_delete(struct net_bridge *br, struct net_bridge_fdb_entry *f)
86 {
87 hlist_del_rcu(&f->hlist);
88 fdb_notify(br, f, RTM_DELNEIGH);
89 call_rcu(&f->rcu, fdb_rcu_free);
90 }
91
92 void br_fdb_changeaddr(struct net_bridge_port *p, const unsigned char *newaddr)
93 {
94 struct net_bridge *br = p->br;
95 struct net_port_vlans *pv = nbp_get_vlan_info(p);
96 bool no_vlan = !pv;
97 int i;
98 u16 vid;
99
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);
109 if (f->dst == p && f->is_local && !f->added_by_user) {
110 /* maybe another port has same hw addr? */
111 struct net_bridge_port *op;
112 u16 vid = f->vlan_id;
113 list_for_each_entry(op, &br->port_list, list) {
114 if (op != p &&
115 ether_addr_equal(op->dev->dev_addr,
116 f->addr.addr) &&
117 (!vid || nbp_vlan_find(op, vid))) {
118 f->dst = op;
119 goto skip_delete;
120 }
121 }
122
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
131 /* delete old one */
132 fdb_delete(br, f);
133 skip_delete:
134 /* if this port has no vlan information
135 * configured, we can safely be done at
136 * this point.
137 */
138 if (no_vlan)
139 goto insert;
140 }
141 }
142 }
143
144 insert:
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
158 done:
159 spin_unlock_bh(&br->hash_lock);
160 }
161
162 void br_fdb_change_mac_address(struct net_bridge *br, const u8 *newaddr)
163 {
164 struct net_bridge_fdb_entry *f;
165 struct net_port_vlans *pv;
166 u16 vid = 0;
167
168 /* If old entry was unassociated with any port, then delete it. */
169 f = __br_fdb_get(br, br->dev->dev_addr, 0);
170 if (f && f->is_local && !f->dst)
171 fdb_delete(br, f);
172
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
183 for_each_set_bit_from(vid, pv->vlan_bitmap, VLAN_N_VID) {
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 }
189 }
190
191 void br_fdb_cleanup(unsigned long _data)
192 {
193 struct net_bridge *br = (struct net_bridge *)_data;
194 unsigned long delay = hold_time(br);
195 unsigned long next_timer = jiffies + br->ageing_time;
196 int i;
197
198 spin_lock(&br->hash_lock);
199 for (i = 0; i < BR_HASH_SIZE; i++) {
200 struct net_bridge_fdb_entry *f;
201 struct hlist_node *n;
202
203 hlist_for_each_entry_safe(f, n, &br->hash[i], hlist) {
204 unsigned long this_timer;
205 if (f->is_static)
206 continue;
207 this_timer = f->updated + delay;
208 if (time_before_eq(this_timer, jiffies))
209 fdb_delete(br, f);
210 else if (time_before(this_timer, next_timer))
211 next_timer = this_timer;
212 }
213 }
214 spin_unlock(&br->hash_lock);
215
216 mod_timer(&br->gc_timer, round_jiffies_up(next_timer));
217 }
218
219 /* Completely flush all dynamic entries in forwarding database.*/
220 void 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;
227 struct hlist_node *n;
228 hlist_for_each_entry_safe(f, n, &br->hash[i], hlist) {
229 if (!f->is_static)
230 fdb_delete(br, f);
231 }
232 }
233 spin_unlock_bh(&br->hash_lock);
234 }
235
236 /* Flush all entries referring to a specific port.
237 * if do_all is set also flush static entries
238 */
239 void br_fdb_delete_by_port(struct net_bridge *br,
240 const struct net_bridge_port *p,
241 int do_all)
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;
248
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);
252 if (f->dst != p)
253 continue;
254
255 if (f->is_static && !do_all)
256 continue;
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) {
265 if (op != p &&
266 ether_addr_equal(op->dev->dev_addr,
267 f->addr.addr)) {
268 f->dst = op;
269 f->added_by_user = 0;
270 goto skip_delete;
271 }
272 }
273 }
274
275 fdb_delete(br, f);
276 skip_delete: ;
277 }
278 }
279 spin_unlock_bh(&br->hash_lock);
280 }
281
282 /* No locking or refcounting, assumes caller has rcu_read_lock */
283 struct net_bridge_fdb_entry *__br_fdb_get(struct net_bridge *br,
284 const unsigned char *addr,
285 __u16 vid)
286 {
287 struct net_bridge_fdb_entry *fdb;
288
289 hlist_for_each_entry_rcu(fdb,
290 &br->hash[br_mac_hash(addr, vid)], hlist) {
291 if (ether_addr_equal(fdb->addr.addr, addr) &&
292 fdb->vlan_id == vid) {
293 if (unlikely(has_expired(br, fdb)))
294 break;
295 return fdb;
296 }
297 }
298
299 return NULL;
300 }
301
302 #if IS_ENABLED(CONFIG_ATM_LANE)
303 /* Interface used by ATM LANE hook to test
304 * if an addr is on some other bridge port */
305 int br_fdb_test_addr(struct net_device *dev, unsigned char *addr)
306 {
307 struct net_bridge_fdb_entry *fdb;
308 struct net_bridge_port *port;
309 int ret;
310
311 rcu_read_lock();
312 port = br_port_get_rcu(dev);
313 if (!port)
314 ret = 0;
315 else {
316 fdb = __br_fdb_get(port->br, addr, 0);
317 ret = fdb && fdb->dst && fdb->dst->dev != dev &&
318 fdb->dst->state == BR_STATE_FORWARDING;
319 }
320 rcu_read_unlock();
321
322 return ret;
323 }
324 #endif /* CONFIG_ATM_LANE */
325
326 /*
327 * Fill buffer with forwarding table records in
328 * the API format.
329 */
330 int 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;
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++) {
341 hlist_for_each_entry_rcu(f, &br->hash[i], hlist) {
342 if (num >= maxnum)
343 goto out;
344
345 if (has_expired(br, f))
346 continue;
347
348 /* ignore pseudo entry for local MAC address */
349 if (!f->dst)
350 continue;
351
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);
359
360 /* due to ABI compat need to split into hi/lo */
361 fe->port_no = f->dst->port_no;
362 fe->port_hi = f->dst->port_no >> 8;
363
364 fe->is_local = f->is_local;
365 if (!f->is_static)
366 fe->ageing_timer_value = jiffies_delta_to_clock_t(jiffies - f->updated);
367 ++fe;
368 ++num;
369 }
370 }
371
372 out:
373 rcu_read_unlock();
374
375 return num;
376 }
377
378 static struct net_bridge_fdb_entry *fdb_find(struct hlist_head *head,
379 const unsigned char *addr,
380 __u16 vid)
381 {
382 struct net_bridge_fdb_entry *fdb;
383
384 hlist_for_each_entry(fdb, head, hlist) {
385 if (ether_addr_equal(fdb->addr.addr, addr) &&
386 fdb->vlan_id == vid)
387 return fdb;
388 }
389 return NULL;
390 }
391
392 static struct net_bridge_fdb_entry *fdb_find_rcu(struct hlist_head *head,
393 const unsigned char *addr,
394 __u16 vid)
395 {
396 struct net_bridge_fdb_entry *fdb;
397
398 hlist_for_each_entry_rcu(fdb, head, hlist) {
399 if (ether_addr_equal(fdb->addr.addr, addr) &&
400 fdb->vlan_id == vid)
401 return fdb;
402 }
403 return NULL;
404 }
405
406 static struct net_bridge_fdb_entry *fdb_create(struct hlist_head *head,
407 struct net_bridge_port *source,
408 const unsigned char *addr,
409 __u16 vid)
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);
416 fdb->dst = source;
417 fdb->vlan_id = vid;
418 fdb->is_local = 0;
419 fdb->is_static = 0;
420 fdb->added_by_user = 0;
421 fdb->updated = fdb->used = jiffies;
422 hlist_add_head_rcu(&fdb->hlist, head);
423 }
424 return fdb;
425 }
426
427 static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
428 const unsigned char *addr, u16 vid)
429 {
430 struct hlist_head *head = &br->hash[br_mac_hash(addr, vid)];
431 struct net_bridge_fdb_entry *fdb;
432
433 if (!is_valid_ether_addr(addr))
434 return -EINVAL;
435
436 fdb = fdb_find(head, addr, vid);
437 if (fdb) {
438 /* it is okay to have multiple ports with same
439 * address, just use the first one.
440 */
441 if (fdb->is_local)
442 return 0;
443 br_warn(br, "adding interface %s with same address "
444 "as a received packet\n",
445 source ? source->dev->name : br->dev->name);
446 fdb_delete(br, fdb);
447 }
448
449 fdb = fdb_create(head, source, addr, vid);
450 if (!fdb)
451 return -ENOMEM;
452
453 fdb->is_local = fdb->is_static = 1;
454 fdb_notify(br, fdb, RTM_NEWNEIGH);
455 return 0;
456 }
457
458 /* Add entry for local address of interface */
459 int br_fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
460 const unsigned char *addr, u16 vid)
461 {
462 int ret;
463
464 spin_lock_bh(&br->hash_lock);
465 ret = fdb_insert(br, source, addr, vid);
466 spin_unlock_bh(&br->hash_lock);
467 return ret;
468 }
469
470 void br_fdb_update(struct net_bridge *br, struct net_bridge_port *source,
471 const unsigned char *addr, u16 vid, bool added_by_user)
472 {
473 struct hlist_head *head = &br->hash[br_mac_hash(addr, vid)];
474 struct net_bridge_fdb_entry *fdb;
475
476 /* some users want to always flood. */
477 if (hold_time(br) == 0)
478 return;
479
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
485 fdb = fdb_find_rcu(head, addr, vid);
486 if (likely(fdb)) {
487 /* attempt to update an entry for a local interface */
488 if (unlikely(fdb->is_local)) {
489 if (net_ratelimit())
490 br_warn(br, "received packet on %s with "
491 "own address as source address\n",
492 source->dev->name);
493 } else {
494 /* fastpath: update of existing entry */
495 fdb->dst = source;
496 fdb->updated = jiffies;
497 if (unlikely(added_by_user))
498 fdb->added_by_user = 1;
499 }
500 } else {
501 spin_lock(&br->hash_lock);
502 if (likely(!fdb_find(head, addr, vid))) {
503 fdb = fdb_create(head, source, addr, vid);
504 if (fdb) {
505 if (unlikely(added_by_user))
506 fdb->added_by_user = 1;
507 fdb_notify(br, fdb, RTM_NEWNEIGH);
508 }
509 }
510 /* else we lose race and someone else inserts
511 * it first, don't bother updating
512 */
513 spin_unlock(&br->hash_lock);
514 }
515 }
516
517 static 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
529 static int fdb_fill_info(struct sk_buff *skb, const struct net_bridge *br,
530 const struct net_bridge_fdb_entry *fdb,
531 u32 portid, u32 seq, int type, unsigned int flags)
532 {
533 unsigned long now = jiffies;
534 struct nda_cacheinfo ci;
535 struct nlmsghdr *nlh;
536 struct ndmsg *ndm;
537
538 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
539 if (nlh == NULL)
540 return -EMSGSIZE;
541
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;
548 ndm->ndm_ifindex = fdb->dst ? fdb->dst->dev->ifindex : br->dev->ifindex;
549 ndm->ndm_state = fdb_to_nud(fdb);
550
551 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->addr))
552 goto nla_put_failure;
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;
557 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
558 goto nla_put_failure;
559
560 if (nla_put(skb, NDA_VLAN, sizeof(u16), &fdb->vlan_id))
561 goto nla_put_failure;
562
563 return nlmsg_end(skb, nlh);
564
565 nla_put_failure:
566 nlmsg_cancel(skb, nlh);
567 return -EMSGSIZE;
568 }
569
570 static inline size_t fdb_nlmsg_size(void)
571 {
572 return NLMSG_ALIGN(sizeof(struct ndmsg))
573 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
574 + nla_total_size(sizeof(u16)) /* NDA_VLAN */
575 + nla_total_size(sizeof(struct nda_cacheinfo));
576 }
577
578 static void fdb_notify(struct net_bridge *br,
579 const struct net_bridge_fdb_entry *fdb, int type)
580 {
581 struct net *net = dev_net(br->dev);
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
589 err = fdb_fill_info(skb, br, fdb, 0, 0, type, 0);
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;
598 errout:
599 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
600 }
601
602 /* Dump information about entries, in response to GETNEIGH */
603 int br_fdb_dump(struct sk_buff *skb,
604 struct netlink_callback *cb,
605 struct net_device *dev,
606 int idx)
607 {
608 struct net_bridge *br = netdev_priv(dev);
609 int i;
610
611 if (!(dev->priv_flags & IFF_EBRIDGE))
612 goto out;
613
614 for (i = 0; i < BR_HASH_SIZE; i++) {
615 struct net_bridge_fdb_entry *f;
616
617 hlist_for_each_entry_rcu(f, &br->hash[i], hlist) {
618 if (idx < cb->args[0])
619 goto skip;
620
621 if (fdb_fill_info(skb, br, f,
622 NETLINK_CB(cb->skb).portid,
623 cb->nlh->nlmsg_seq,
624 RTM_NEWNEIGH,
625 NLM_F_MULTI) < 0)
626 break;
627 skip:
628 ++idx;
629 }
630 }
631
632 out:
633 return idx;
634 }
635
636 /* Update (create or replace) forwarding database entry */
637 static int fdb_add_entry(struct net_bridge_port *source, const __u8 *addr,
638 __u16 state, __u16 flags, __u16 vid)
639 {
640 struct net_bridge *br = source->br;
641 struct hlist_head *head = &br->hash[br_mac_hash(addr, vid)];
642 struct net_bridge_fdb_entry *fdb;
643 bool modified = false;
644
645 fdb = fdb_find(head, addr, vid);
646 if (fdb == NULL) {
647 if (!(flags & NLM_F_CREATE))
648 return -ENOENT;
649
650 fdb = fdb_create(head, source, addr, vid);
651 if (!fdb)
652 return -ENOMEM;
653
654 modified = true;
655 } else {
656 if (flags & NLM_F_EXCL)
657 return -EEXIST;
658
659 if (fdb->dst != source) {
660 fdb->dst = source;
661 modified = true;
662 }
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;
673
674 modified = true;
675 }
676 fdb->added_by_user = 1;
677
678 fdb->used = jiffies;
679 if (modified) {
680 fdb->updated = jiffies;
681 fdb_notify(br, fdb, RTM_NEWNEIGH);
682 }
683
684 return 0;
685 }
686
687 static 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();
694 br_fdb_update(p->br, p, addr, vid, true);
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
706 /* Add new permanent fdb entry with RTM_NEWNEIGH */
707 int br_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
708 struct net_device *dev,
709 const unsigned char *addr, u16 nlh_flags)
710 {
711 struct net_bridge_port *p;
712 int err = 0;
713 struct net_port_vlans *pv;
714 unsigned short vid = VLAN_N_VID;
715
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
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
729 if (!vid || vid >= VLAN_VID_MASK) {
730 pr_info("bridge: RTM_NEWNEIGH with invalid vlan id %d\n",
731 vid);
732 return -EINVAL;
733 }
734 }
735
736 if (is_zero_ether_addr(addr)) {
737 pr_info("bridge: RTM_NEWNEIGH with invalid ether address\n");
738 return -EINVAL;
739 }
740
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
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);
758 } else {
759 if (!pv || bitmap_empty(pv->vlan_bitmap, VLAN_N_VID)) {
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 */
768 for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID) {
769 err = __br_fdb_add(ndm, p, addr, nlh_flags, vid);
770 if (err)
771 goto out;
772 }
773 }
774
775 out:
776 return err;
777 }
778
779 int fdb_delete_by_addr(struct net_bridge *br, const u8 *addr,
780 u16 vlan)
781 {
782 struct hlist_head *head = &br->hash[br_mac_hash(addr, vlan)];
783 struct net_bridge_fdb_entry *fdb;
784
785 fdb = fdb_find(head, addr, vlan);
786 if (!fdb)
787 return -ENOENT;
788
789 fdb_delete(br, fdb);
790 return 0;
791 }
792
793 static 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
805 /* Remove neighbor entry with RTM_DELNEIGH */
806 int br_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
807 struct net_device *dev,
808 const unsigned char *addr)
809 {
810 struct net_bridge_port *p;
811 int err;
812 struct net_port_vlans *pv;
813 unsigned short vid = VLAN_N_VID;
814
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
823 if (!vid || vid >= VLAN_VID_MASK) {
824 pr_info("bridge: RTM_NEWNEIGH with invalid vlan id %d\n",
825 vid);
826 return -EINVAL;
827 }
828 }
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
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 }
843
844 err = __br_fdb_delete(p, addr, vid);
845 } else {
846 if (!pv || bitmap_empty(pv->vlan_bitmap, VLAN_N_VID)) {
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;
856 for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID) {
857 err &= __br_fdb_delete(p, addr, vid);
858 }
859 }
860 out:
861 return err;
862 }
This page took 0.050646 seconds and 5 git commands to generate.