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