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