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