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