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