b19e3104afd6bf512d9b98f8350cc0f37d9880ea
[muen/linux.git] / net / bridge / br_fdb.c
1 /*
2  *      Forwarding database
3  *      Linux ethernet bridge
4  *
5  *      Authors:
6  *      Lennert Buytenhek               <buytenh@gnu.org>
7  *
8  *      This program is free software; you can redistribute it and/or
9  *      modify it under the terms of the GNU General Public License
10  *      as published by the Free Software Foundation; either version
11  *      2 of the License, or (at your option) any later version.
12  */
13
14 #include <linux/kernel.h>
15 #include <linux/init.h>
16 #include <linux/rculist.h>
17 #include <linux/spinlock.h>
18 #include <linux/times.h>
19 #include <linux/netdevice.h>
20 #include <linux/etherdevice.h>
21 #include <linux/jhash.h>
22 #include <linux/random.h>
23 #include <linux/slab.h>
24 #include <linux/atomic.h>
25 #include <asm/unaligned.h>
26 #include <linux/if_vlan.h>
27 #include <net/switchdev.h>
28 #include <trace/events/bridge.h>
29 #include "br_private.h"
30
31 static const struct rhashtable_params br_fdb_rht_params = {
32         .head_offset = offsetof(struct net_bridge_fdb_entry, rhnode),
33         .key_offset = offsetof(struct net_bridge_fdb_entry, key),
34         .key_len = sizeof(struct net_bridge_fdb_key),
35         .automatic_shrinking = true,
36         .locks_mul = 1,
37 };
38
39 static struct kmem_cache *br_fdb_cache __read_mostly;
40 static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
41                       const unsigned char *addr, u16 vid);
42 static void fdb_notify(struct net_bridge *br,
43                        const struct net_bridge_fdb_entry *, int, bool);
44
45 int __init br_fdb_init(void)
46 {
47         br_fdb_cache = kmem_cache_create("bridge_fdb_cache",
48                                          sizeof(struct net_bridge_fdb_entry),
49                                          0,
50                                          SLAB_HWCACHE_ALIGN, NULL);
51         if (!br_fdb_cache)
52                 return -ENOMEM;
53
54         return 0;
55 }
56
57 void br_fdb_fini(void)
58 {
59         kmem_cache_destroy(br_fdb_cache);
60 }
61
62 int br_fdb_hash_init(struct net_bridge *br)
63 {
64         return rhashtable_init(&br->fdb_hash_tbl, &br_fdb_rht_params);
65 }
66
67 void br_fdb_hash_fini(struct net_bridge *br)
68 {
69         rhashtable_destroy(&br->fdb_hash_tbl);
70 }
71
72 /* if topology_changing then use forward_delay (default 15 sec)
73  * otherwise keep longer (default 5 minutes)
74  */
75 static inline unsigned long hold_time(const struct net_bridge *br)
76 {
77         return br->topology_change ? br->forward_delay : br->ageing_time;
78 }
79
80 static inline int has_expired(const struct net_bridge *br,
81                                   const struct net_bridge_fdb_entry *fdb)
82 {
83         return !fdb->is_static && !fdb->added_by_external_learn &&
84                 time_before_eq(fdb->updated + hold_time(br), jiffies);
85 }
86
87 static void fdb_rcu_free(struct rcu_head *head)
88 {
89         struct net_bridge_fdb_entry *ent
90                 = container_of(head, struct net_bridge_fdb_entry, rcu);
91         kmem_cache_free(br_fdb_cache, ent);
92 }
93
94 static struct net_bridge_fdb_entry *fdb_find_rcu(struct rhashtable *tbl,
95                                                  const unsigned char *addr,
96                                                  __u16 vid)
97 {
98         struct net_bridge_fdb_key key;
99
100         WARN_ON_ONCE(!rcu_read_lock_held());
101
102         key.vlan_id = vid;
103         memcpy(key.addr.addr, addr, sizeof(key.addr.addr));
104
105         return rhashtable_lookup(tbl, &key, br_fdb_rht_params);
106 }
107
108 /* requires bridge hash_lock */
109 static struct net_bridge_fdb_entry *br_fdb_find(struct net_bridge *br,
110                                                 const unsigned char *addr,
111                                                 __u16 vid)
112 {
113         struct net_bridge_fdb_entry *fdb;
114
115         lockdep_assert_held_once(&br->hash_lock);
116
117         rcu_read_lock();
118         fdb = fdb_find_rcu(&br->fdb_hash_tbl, addr, vid);
119         rcu_read_unlock();
120
121         return fdb;
122 }
123
124 struct net_device *br_fdb_find_port(const struct net_device *br_dev,
125                                     const unsigned char *addr,
126                                     __u16 vid)
127 {
128         struct net_bridge_fdb_entry *f;
129         struct net_device *dev = NULL;
130         struct net_bridge *br;
131
132         ASSERT_RTNL();
133
134         if (!netif_is_bridge_master(br_dev))
135                 return NULL;
136
137         br = netdev_priv(br_dev);
138         f = br_fdb_find(br, addr, vid);
139         if (f && f->dst)
140                 dev = f->dst->dev;
141
142         return dev;
143 }
144 EXPORT_SYMBOL_GPL(br_fdb_find_port);
145
146 struct net_bridge_fdb_entry *br_fdb_find_rcu(struct net_bridge *br,
147                                              const unsigned char *addr,
148                                              __u16 vid)
149 {
150         return fdb_find_rcu(&br->fdb_hash_tbl, addr, vid);
151 }
152
153 /* When a static FDB entry is added, the mac address from the entry is
154  * added to the bridge private HW address list and all required ports
155  * are then updated with the new information.
156  * Called under RTNL.
157  */
158 static void fdb_add_hw_addr(struct net_bridge *br, const unsigned char *addr)
159 {
160         int err;
161         struct net_bridge_port *p;
162
163         ASSERT_RTNL();
164
165         list_for_each_entry(p, &br->port_list, list) {
166                 if (!br_promisc_port(p)) {
167                         err = dev_uc_add(p->dev, addr);
168                         if (err)
169                                 goto undo;
170                 }
171         }
172
173         return;
174 undo:
175         list_for_each_entry_continue_reverse(p, &br->port_list, list) {
176                 if (!br_promisc_port(p))
177                         dev_uc_del(p->dev, addr);
178         }
179 }
180
181 /* When a static FDB entry is deleted, the HW address from that entry is
182  * also removed from the bridge private HW address list and updates all
183  * the ports with needed information.
184  * Called under RTNL.
185  */
186 static void fdb_del_hw_addr(struct net_bridge *br, const unsigned char *addr)
187 {
188         struct net_bridge_port *p;
189
190         ASSERT_RTNL();
191
192         list_for_each_entry(p, &br->port_list, list) {
193                 if (!br_promisc_port(p))
194                         dev_uc_del(p->dev, addr);
195         }
196 }
197
198 static void fdb_delete(struct net_bridge *br, struct net_bridge_fdb_entry *f,
199                        bool swdev_notify)
200 {
201         trace_fdb_delete(br, f);
202
203         if (f->is_static)
204                 fdb_del_hw_addr(br, f->key.addr.addr);
205
206         hlist_del_init_rcu(&f->fdb_node);
207         rhashtable_remove_fast(&br->fdb_hash_tbl, &f->rhnode,
208                                br_fdb_rht_params);
209         fdb_notify(br, f, RTM_DELNEIGH, swdev_notify);
210         call_rcu(&f->rcu, fdb_rcu_free);
211 }
212
213 /* Delete a local entry if no other port had the same address. */
214 static void fdb_delete_local(struct net_bridge *br,
215                              const struct net_bridge_port *p,
216                              struct net_bridge_fdb_entry *f)
217 {
218         const unsigned char *addr = f->key.addr.addr;
219         struct net_bridge_vlan_group *vg;
220         const struct net_bridge_vlan *v;
221         struct net_bridge_port *op;
222         u16 vid = f->key.vlan_id;
223
224         /* Maybe another port has same hw addr? */
225         list_for_each_entry(op, &br->port_list, list) {
226                 vg = nbp_vlan_group(op);
227                 if (op != p && ether_addr_equal(op->dev->dev_addr, addr) &&
228                     (!vid || br_vlan_find(vg, vid))) {
229                         f->dst = op;
230                         f->added_by_user = 0;
231                         return;
232                 }
233         }
234
235         vg = br_vlan_group(br);
236         v = br_vlan_find(vg, vid);
237         /* Maybe bridge device has same hw addr? */
238         if (p && ether_addr_equal(br->dev->dev_addr, addr) &&
239             (!vid || (v && br_vlan_should_use(v)))) {
240                 f->dst = NULL;
241                 f->added_by_user = 0;
242                 return;
243         }
244
245         fdb_delete(br, f, true);
246 }
247
248 void br_fdb_find_delete_local(struct net_bridge *br,
249                               const struct net_bridge_port *p,
250                               const unsigned char *addr, u16 vid)
251 {
252         struct net_bridge_fdb_entry *f;
253
254         spin_lock_bh(&br->hash_lock);
255         f = br_fdb_find(br, addr, vid);
256         if (f && f->is_local && !f->added_by_user && f->dst == p)
257                 fdb_delete_local(br, p, f);
258         spin_unlock_bh(&br->hash_lock);
259 }
260
261 void br_fdb_changeaddr(struct net_bridge_port *p, const unsigned char *newaddr)
262 {
263         struct net_bridge_vlan_group *vg;
264         struct net_bridge_fdb_entry *f;
265         struct net_bridge *br = p->br;
266         struct net_bridge_vlan *v;
267
268         spin_lock_bh(&br->hash_lock);
269         vg = nbp_vlan_group(p);
270         hlist_for_each_entry(f, &br->fdb_list, fdb_node) {
271                 if (f->dst == p && f->is_local && !f->added_by_user) {
272                         /* delete old one */
273                         fdb_delete_local(br, p, f);
274
275                         /* if this port has no vlan information
276                          * configured, we can safely be done at
277                          * this point.
278                          */
279                         if (!vg || !vg->num_vlans)
280                                 goto insert;
281                 }
282         }
283
284 insert:
285         /* insert new address,  may fail if invalid address or dup. */
286         fdb_insert(br, p, newaddr, 0);
287
288         if (!vg || !vg->num_vlans)
289                 goto done;
290
291         /* Now add entries for every VLAN configured on the port.
292          * This function runs under RTNL so the bitmap will not change
293          * from under us.
294          */
295         list_for_each_entry(v, &vg->vlan_list, vlist)
296                 fdb_insert(br, p, newaddr, v->vid);
297
298 done:
299         spin_unlock_bh(&br->hash_lock);
300 }
301
302 void br_fdb_change_mac_address(struct net_bridge *br, const u8 *newaddr)
303 {
304         struct net_bridge_vlan_group *vg;
305         struct net_bridge_fdb_entry *f;
306         struct net_bridge_vlan *v;
307
308         spin_lock_bh(&br->hash_lock);
309
310         /* If old entry was unassociated with any port, then delete it. */
311         f = br_fdb_find(br, br->dev->dev_addr, 0);
312         if (f && f->is_local && !f->dst && !f->added_by_user)
313                 fdb_delete_local(br, NULL, f);
314
315         fdb_insert(br, NULL, newaddr, 0);
316         vg = br_vlan_group(br);
317         if (!vg || !vg->num_vlans)
318                 goto out;
319         /* Now remove and add entries for every VLAN configured on the
320          * bridge.  This function runs under RTNL so the bitmap will not
321          * change from under us.
322          */
323         list_for_each_entry(v, &vg->vlan_list, vlist) {
324                 if (!br_vlan_should_use(v))
325                         continue;
326                 f = br_fdb_find(br, br->dev->dev_addr, v->vid);
327                 if (f && f->is_local && !f->dst && !f->added_by_user)
328                         fdb_delete_local(br, NULL, f);
329                 fdb_insert(br, NULL, newaddr, v->vid);
330         }
331 out:
332         spin_unlock_bh(&br->hash_lock);
333 }
334
335 void br_fdb_cleanup(struct work_struct *work)
336 {
337         struct net_bridge *br = container_of(work, struct net_bridge,
338                                              gc_work.work);
339         struct net_bridge_fdb_entry *f = NULL;
340         unsigned long delay = hold_time(br);
341         unsigned long work_delay = delay;
342         unsigned long now = jiffies;
343
344         /* this part is tricky, in order to avoid blocking learning and
345          * consequently forwarding, we rely on rcu to delete objects with
346          * delayed freeing allowing us to continue traversing
347          */
348         rcu_read_lock();
349         hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
350                 unsigned long this_timer;
351
352                 if (f->is_static || f->added_by_external_learn)
353                         continue;
354                 this_timer = f->updated + delay;
355                 if (time_after(this_timer, now)) {
356                         work_delay = min(work_delay, this_timer - now);
357                 } else {
358                         spin_lock_bh(&br->hash_lock);
359                         if (!hlist_unhashed(&f->fdb_node))
360                                 fdb_delete(br, f, true);
361                         spin_unlock_bh(&br->hash_lock);
362                 }
363         }
364         rcu_read_unlock();
365
366         /* Cleanup minimum 10 milliseconds apart */
367         work_delay = max_t(unsigned long, work_delay, msecs_to_jiffies(10));
368         mod_delayed_work(system_long_wq, &br->gc_work, work_delay);
369 }
370
371 /* Completely flush all dynamic entries in forwarding database.*/
372 void br_fdb_flush(struct net_bridge *br)
373 {
374         struct net_bridge_fdb_entry *f;
375         struct hlist_node *tmp;
376
377         spin_lock_bh(&br->hash_lock);
378         hlist_for_each_entry_safe(f, tmp, &br->fdb_list, fdb_node) {
379                 if (!f->is_static)
380                         fdb_delete(br, f, true);
381         }
382         spin_unlock_bh(&br->hash_lock);
383 }
384
385 /* Flush all entries referring to a specific port.
386  * if do_all is set also flush static entries
387  * if vid is set delete all entries that match the vlan_id
388  */
389 void br_fdb_delete_by_port(struct net_bridge *br,
390                            const struct net_bridge_port *p,
391                            u16 vid,
392                            int do_all)
393 {
394         struct net_bridge_fdb_entry *f;
395         struct hlist_node *tmp;
396
397         spin_lock_bh(&br->hash_lock);
398         hlist_for_each_entry_safe(f, tmp, &br->fdb_list, fdb_node) {
399                 if (f->dst != p)
400                         continue;
401
402                 if (!do_all)
403                         if (f->is_static || (vid && f->key.vlan_id != vid))
404                                 continue;
405
406                 if (f->is_local)
407                         fdb_delete_local(br, p, f);
408                 else
409                         fdb_delete(br, f, true);
410         }
411         spin_unlock_bh(&br->hash_lock);
412 }
413
414 #if IS_ENABLED(CONFIG_ATM_LANE)
415 /* Interface used by ATM LANE hook to test
416  * if an addr is on some other bridge port */
417 int br_fdb_test_addr(struct net_device *dev, unsigned char *addr)
418 {
419         struct net_bridge_fdb_entry *fdb;
420         struct net_bridge_port *port;
421         int ret;
422
423         rcu_read_lock();
424         port = br_port_get_rcu(dev);
425         if (!port)
426                 ret = 0;
427         else {
428                 fdb = br_fdb_find_rcu(port->br, addr, 0);
429                 ret = fdb && fdb->dst && fdb->dst->dev != dev &&
430                         fdb->dst->state == BR_STATE_FORWARDING;
431         }
432         rcu_read_unlock();
433
434         return ret;
435 }
436 #endif /* CONFIG_ATM_LANE */
437
438 /*
439  * Fill buffer with forwarding table records in
440  * the API format.
441  */
442 int br_fdb_fillbuf(struct net_bridge *br, void *buf,
443                    unsigned long maxnum, unsigned long skip)
444 {
445         struct net_bridge_fdb_entry *f;
446         struct __fdb_entry *fe = buf;
447         int num = 0;
448
449         memset(buf, 0, maxnum*sizeof(struct __fdb_entry));
450
451         rcu_read_lock();
452         hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
453                 if (num >= maxnum)
454                         break;
455
456                 if (has_expired(br, f))
457                         continue;
458
459                 /* ignore pseudo entry for local MAC address */
460                 if (!f->dst)
461                         continue;
462
463                 if (skip) {
464                         --skip;
465                         continue;
466                 }
467
468                 /* convert from internal format to API */
469                 memcpy(fe->mac_addr, f->key.addr.addr, ETH_ALEN);
470
471                 /* due to ABI compat need to split into hi/lo */
472                 fe->port_no = f->dst->port_no;
473                 fe->port_hi = f->dst->port_no >> 8;
474
475                 fe->is_local = f->is_local;
476                 if (!f->is_static)
477                         fe->ageing_timer_value = jiffies_delta_to_clock_t(jiffies - f->updated);
478                 ++fe;
479                 ++num;
480         }
481         rcu_read_unlock();
482
483         return num;
484 }
485
486 static struct net_bridge_fdb_entry *fdb_create(struct net_bridge *br,
487                                                struct net_bridge_port *source,
488                                                const unsigned char *addr,
489                                                __u16 vid,
490                                                unsigned char is_local,
491                                                unsigned char is_static)
492 {
493         struct net_bridge_fdb_entry *fdb;
494
495         fdb = kmem_cache_alloc(br_fdb_cache, GFP_ATOMIC);
496         if (fdb) {
497                 memcpy(fdb->key.addr.addr, addr, ETH_ALEN);
498                 fdb->dst = source;
499                 fdb->key.vlan_id = vid;
500                 fdb->is_local = is_local;
501                 fdb->is_static = is_static;
502                 fdb->added_by_user = 0;
503                 fdb->added_by_external_learn = 0;
504                 fdb->offloaded = 0;
505                 fdb->updated = fdb->used = jiffies;
506                 if (rhashtable_lookup_insert_fast(&br->fdb_hash_tbl,
507                                                   &fdb->rhnode,
508                                                   br_fdb_rht_params)) {
509                         kmem_cache_free(br_fdb_cache, fdb);
510                         fdb = NULL;
511                 } else {
512                         hlist_add_head_rcu(&fdb->fdb_node, &br->fdb_list);
513                 }
514         }
515         return fdb;
516 }
517
518 static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
519                   const unsigned char *addr, u16 vid)
520 {
521         struct net_bridge_fdb_entry *fdb;
522
523         if (!is_valid_ether_addr(addr))
524                 return -EINVAL;
525
526         fdb = br_fdb_find(br, addr, vid);
527         if (fdb) {
528                 /* it is okay to have multiple ports with same
529                  * address, just use the first one.
530                  */
531                 if (fdb->is_local)
532                         return 0;
533                 br_warn(br, "adding interface %s with same address as a received packet (addr:%pM, vlan:%u)\n",
534                        source ? source->dev->name : br->dev->name, addr, vid);
535                 fdb_delete(br, fdb, true);
536         }
537
538         fdb = fdb_create(br, source, addr, vid, 1, 1);
539         if (!fdb)
540                 return -ENOMEM;
541
542         fdb_add_hw_addr(br, addr);
543         fdb_notify(br, fdb, RTM_NEWNEIGH, true);
544         return 0;
545 }
546
547 /* Add entry for local address of interface */
548 int br_fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
549                   const unsigned char *addr, u16 vid)
550 {
551         int ret;
552
553         spin_lock_bh(&br->hash_lock);
554         ret = fdb_insert(br, source, addr, vid);
555         spin_unlock_bh(&br->hash_lock);
556         return ret;
557 }
558
559 void br_fdb_update(struct net_bridge *br, struct net_bridge_port *source,
560                    const unsigned char *addr, u16 vid, bool added_by_user)
561 {
562         struct net_bridge_fdb_entry *fdb;
563         bool fdb_modified = false;
564
565         /* some users want to always flood. */
566         if (hold_time(br) == 0)
567                 return;
568
569         /* ignore packets unless we are using this port */
570         if (!(source->state == BR_STATE_LEARNING ||
571               source->state == BR_STATE_FORWARDING))
572                 return;
573
574         fdb = fdb_find_rcu(&br->fdb_hash_tbl, addr, vid);
575         if (likely(fdb)) {
576                 /* attempt to update an entry for a local interface */
577                 if (unlikely(fdb->is_local)) {
578                         if (net_ratelimit())
579                                 br_warn(br, "received packet on %s with own address as source address (addr:%pM, vlan:%u)\n",
580                                         source->dev->name, addr, vid);
581                 } else {
582                         unsigned long now = jiffies;
583
584                         /* fastpath: update of existing entry */
585                         if (unlikely(source != fdb->dst)) {
586                                 fdb->dst = source;
587                                 fdb_modified = true;
588                                 /* Take over HW learned entry */
589                                 if (unlikely(fdb->added_by_external_learn))
590                                         fdb->added_by_external_learn = 0;
591                         }
592                         if (now != fdb->updated)
593                                 fdb->updated = now;
594                         if (unlikely(added_by_user))
595                                 fdb->added_by_user = 1;
596                         if (unlikely(fdb_modified)) {
597                                 trace_br_fdb_update(br, source, addr, vid, added_by_user);
598                                 fdb_notify(br, fdb, RTM_NEWNEIGH, true);
599                         }
600                 }
601         } else {
602                 spin_lock(&br->hash_lock);
603                 fdb = fdb_create(br, source, addr, vid, 0, 0);
604                 if (fdb) {
605                         if (unlikely(added_by_user))
606                                 fdb->added_by_user = 1;
607                         trace_br_fdb_update(br, source, addr, vid,
608                                             added_by_user);
609                         fdb_notify(br, fdb, RTM_NEWNEIGH, true);
610                 }
611                 /* else  we lose race and someone else inserts
612                  * it first, don't bother updating
613                  */
614                 spin_unlock(&br->hash_lock);
615         }
616 }
617
618 static int fdb_to_nud(const struct net_bridge *br,
619                       const struct net_bridge_fdb_entry *fdb)
620 {
621         if (fdb->is_local)
622                 return NUD_PERMANENT;
623         else if (fdb->is_static)
624                 return NUD_NOARP;
625         else if (has_expired(br, fdb))
626                 return NUD_STALE;
627         else
628                 return NUD_REACHABLE;
629 }
630
631 static int fdb_fill_info(struct sk_buff *skb, const struct net_bridge *br,
632                          const struct net_bridge_fdb_entry *fdb,
633                          u32 portid, u32 seq, int type, unsigned int flags)
634 {
635         unsigned long now = jiffies;
636         struct nda_cacheinfo ci;
637         struct nlmsghdr *nlh;
638         struct ndmsg *ndm;
639
640         nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
641         if (nlh == NULL)
642                 return -EMSGSIZE;
643
644         ndm = nlmsg_data(nlh);
645         ndm->ndm_family  = AF_BRIDGE;
646         ndm->ndm_pad1    = 0;
647         ndm->ndm_pad2    = 0;
648         ndm->ndm_flags   = 0;
649         ndm->ndm_type    = 0;
650         ndm->ndm_ifindex = fdb->dst ? fdb->dst->dev->ifindex : br->dev->ifindex;
651         ndm->ndm_state   = fdb_to_nud(br, fdb);
652
653         if (fdb->offloaded)
654                 ndm->ndm_flags |= NTF_OFFLOADED;
655         if (fdb->added_by_external_learn)
656                 ndm->ndm_flags |= NTF_EXT_LEARNED;
657
658         if (nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->key.addr))
659                 goto nla_put_failure;
660         if (nla_put_u32(skb, NDA_MASTER, br->dev->ifindex))
661                 goto nla_put_failure;
662         ci.ndm_used      = jiffies_to_clock_t(now - fdb->used);
663         ci.ndm_confirmed = 0;
664         ci.ndm_updated   = jiffies_to_clock_t(now - fdb->updated);
665         ci.ndm_refcnt    = 0;
666         if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
667                 goto nla_put_failure;
668
669         if (fdb->key.vlan_id && nla_put(skb, NDA_VLAN, sizeof(u16),
670                                         &fdb->key.vlan_id))
671                 goto nla_put_failure;
672
673         nlmsg_end(skb, nlh);
674         return 0;
675
676 nla_put_failure:
677         nlmsg_cancel(skb, nlh);
678         return -EMSGSIZE;
679 }
680
681 static inline size_t fdb_nlmsg_size(void)
682 {
683         return NLMSG_ALIGN(sizeof(struct ndmsg))
684                 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
685                 + nla_total_size(sizeof(u32)) /* NDA_MASTER */
686                 + nla_total_size(sizeof(u16)) /* NDA_VLAN */
687                 + nla_total_size(sizeof(struct nda_cacheinfo));
688 }
689
690 static void fdb_notify(struct net_bridge *br,
691                        const struct net_bridge_fdb_entry *fdb, int type,
692                        bool swdev_notify)
693 {
694         struct net *net = dev_net(br->dev);
695         struct sk_buff *skb;
696         int err = -ENOBUFS;
697
698         if (swdev_notify)
699                 br_switchdev_fdb_notify(fdb, type);
700
701         skb = nlmsg_new(fdb_nlmsg_size(), GFP_ATOMIC);
702         if (skb == NULL)
703                 goto errout;
704
705         err = fdb_fill_info(skb, br, fdb, 0, 0, type, 0);
706         if (err < 0) {
707                 /* -EMSGSIZE implies BUG in fdb_nlmsg_size() */
708                 WARN_ON(err == -EMSGSIZE);
709                 kfree_skb(skb);
710                 goto errout;
711         }
712         rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
713         return;
714 errout:
715         rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
716 }
717
718 /* Dump information about entries, in response to GETNEIGH */
719 int br_fdb_dump(struct sk_buff *skb,
720                 struct netlink_callback *cb,
721                 struct net_device *dev,
722                 struct net_device *filter_dev,
723                 int *idx)
724 {
725         struct net_bridge *br = netdev_priv(dev);
726         struct net_bridge_fdb_entry *f;
727         int err = 0;
728
729         if (!(dev->priv_flags & IFF_EBRIDGE))
730                 return err;
731
732         if (!filter_dev) {
733                 err = ndo_dflt_fdb_dump(skb, cb, dev, NULL, idx);
734                 if (err < 0)
735                         return err;
736         }
737
738         rcu_read_lock();
739         hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
740                 if (*idx < cb->args[2])
741                         goto skip;
742                 if (filter_dev && (!f->dst || f->dst->dev != filter_dev)) {
743                         if (filter_dev != dev)
744                                 goto skip;
745                         /* !f->dst is a special case for bridge
746                          * It means the MAC belongs to the bridge
747                          * Therefore need a little more filtering
748                          * we only want to dump the !f->dst case
749                          */
750                         if (f->dst)
751                                 goto skip;
752                 }
753                 if (!filter_dev && f->dst)
754                         goto skip;
755
756                 err = fdb_fill_info(skb, br, f,
757                                     NETLINK_CB(cb->skb).portid,
758                                     cb->nlh->nlmsg_seq,
759                                     RTM_NEWNEIGH,
760                                     NLM_F_MULTI);
761                 if (err < 0)
762                         break;
763 skip:
764                 *idx += 1;
765         }
766         rcu_read_unlock();
767
768         return err;
769 }
770
771 /* Update (create or replace) forwarding database entry */
772 static int fdb_add_entry(struct net_bridge *br, struct net_bridge_port *source,
773                          const __u8 *addr, __u16 state, __u16 flags, __u16 vid)
774 {
775         struct net_bridge_fdb_entry *fdb;
776         bool modified = false;
777
778         /* If the port cannot learn allow only local and static entries */
779         if (source && !(state & NUD_PERMANENT) && !(state & NUD_NOARP) &&
780             !(source->state == BR_STATE_LEARNING ||
781               source->state == BR_STATE_FORWARDING))
782                 return -EPERM;
783
784         if (!source && !(state & NUD_PERMANENT)) {
785                 pr_info("bridge: RTM_NEWNEIGH %s without NUD_PERMANENT\n",
786                         br->dev->name);
787                 return -EINVAL;
788         }
789
790         fdb = br_fdb_find(br, addr, vid);
791         if (fdb == NULL) {
792                 if (!(flags & NLM_F_CREATE))
793                         return -ENOENT;
794
795                 fdb = fdb_create(br, source, addr, vid, 0, 0);
796                 if (!fdb)
797                         return -ENOMEM;
798
799                 modified = true;
800         } else {
801                 if (flags & NLM_F_EXCL)
802                         return -EEXIST;
803
804                 if (fdb->dst != source) {
805                         fdb->dst = source;
806                         modified = true;
807                 }
808         }
809
810         if (fdb_to_nud(br, fdb) != state) {
811                 if (state & NUD_PERMANENT) {
812                         fdb->is_local = 1;
813                         if (!fdb->is_static) {
814                                 fdb->is_static = 1;
815                                 fdb_add_hw_addr(br, addr);
816                         }
817                 } else if (state & NUD_NOARP) {
818                         fdb->is_local = 0;
819                         if (!fdb->is_static) {
820                                 fdb->is_static = 1;
821                                 fdb_add_hw_addr(br, addr);
822                         }
823                 } else {
824                         fdb->is_local = 0;
825                         if (fdb->is_static) {
826                                 fdb->is_static = 0;
827                                 fdb_del_hw_addr(br, addr);
828                         }
829                 }
830
831                 modified = true;
832         }
833         fdb->added_by_user = 1;
834
835         fdb->used = jiffies;
836         if (modified) {
837                 fdb->updated = jiffies;
838                 fdb_notify(br, fdb, RTM_NEWNEIGH, true);
839         }
840
841         return 0;
842 }
843
844 static int __br_fdb_add(struct ndmsg *ndm, struct net_bridge *br,
845                         struct net_bridge_port *p, const unsigned char *addr,
846                         u16 nlh_flags, u16 vid)
847 {
848         int err = 0;
849
850         if (ndm->ndm_flags & NTF_USE) {
851                 if (!p) {
852                         pr_info("bridge: RTM_NEWNEIGH %s with NTF_USE is not supported\n",
853                                 br->dev->name);
854                         return -EINVAL;
855                 }
856                 local_bh_disable();
857                 rcu_read_lock();
858                 br_fdb_update(br, p, addr, vid, true);
859                 rcu_read_unlock();
860                 local_bh_enable();
861         } else if (ndm->ndm_flags & NTF_EXT_LEARNED) {
862                 err = br_fdb_external_learn_add(br, p, addr, vid, true);
863         } else {
864                 spin_lock_bh(&br->hash_lock);
865                 err = fdb_add_entry(br, p, addr, ndm->ndm_state,
866                                     nlh_flags, vid);
867                 spin_unlock_bh(&br->hash_lock);
868         }
869
870         return err;
871 }
872
873 /* Add new permanent fdb entry with RTM_NEWNEIGH */
874 int br_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
875                struct net_device *dev,
876                const unsigned char *addr, u16 vid, u16 nlh_flags)
877 {
878         struct net_bridge_vlan_group *vg;
879         struct net_bridge_port *p = NULL;
880         struct net_bridge_vlan *v;
881         struct net_bridge *br = NULL;
882         int err = 0;
883
884         trace_br_fdb_add(ndm, dev, addr, vid, nlh_flags);
885
886         if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE))) {
887                 pr_info("bridge: RTM_NEWNEIGH with invalid state %#x\n", ndm->ndm_state);
888                 return -EINVAL;
889         }
890
891         if (is_zero_ether_addr(addr)) {
892                 pr_info("bridge: RTM_NEWNEIGH with invalid ether address\n");
893                 return -EINVAL;
894         }
895
896         if (dev->priv_flags & IFF_EBRIDGE) {
897                 br = netdev_priv(dev);
898                 vg = br_vlan_group(br);
899         } else {
900                 p = br_port_get_rtnl(dev);
901                 if (!p) {
902                         pr_info("bridge: RTM_NEWNEIGH %s not a bridge port\n",
903                                 dev->name);
904                         return -EINVAL;
905                 }
906                 br = p->br;
907                 vg = nbp_vlan_group(p);
908         }
909
910         if (vid) {
911                 v = br_vlan_find(vg, vid);
912                 if (!v || !br_vlan_should_use(v)) {
913                         pr_info("bridge: RTM_NEWNEIGH with unconfigured vlan %d on %s\n", vid, dev->name);
914                         return -EINVAL;
915                 }
916
917                 /* VID was specified, so use it. */
918                 err = __br_fdb_add(ndm, br, p, addr, nlh_flags, vid);
919         } else {
920                 err = __br_fdb_add(ndm, br, p, addr, nlh_flags, 0);
921                 if (err || !vg || !vg->num_vlans)
922                         goto out;
923
924                 /* We have vlans configured on this port and user didn't
925                  * specify a VLAN.  To be nice, add/update entry for every
926                  * vlan on this port.
927                  */
928                 list_for_each_entry(v, &vg->vlan_list, vlist) {
929                         if (!br_vlan_should_use(v))
930                                 continue;
931                         err = __br_fdb_add(ndm, br, p, addr, nlh_flags, v->vid);
932                         if (err)
933                                 goto out;
934                 }
935         }
936
937 out:
938         return err;
939 }
940
941 static int fdb_delete_by_addr_and_port(struct net_bridge *br,
942                                        const struct net_bridge_port *p,
943                                        const u8 *addr, u16 vlan)
944 {
945         struct net_bridge_fdb_entry *fdb;
946
947         fdb = br_fdb_find(br, addr, vlan);
948         if (!fdb || fdb->dst != p)
949                 return -ENOENT;
950
951         fdb_delete(br, fdb, true);
952
953         return 0;
954 }
955
956 static int __br_fdb_delete(struct net_bridge *br,
957                            const struct net_bridge_port *p,
958                            const unsigned char *addr, u16 vid)
959 {
960         int err;
961
962         spin_lock_bh(&br->hash_lock);
963         err = fdb_delete_by_addr_and_port(br, p, addr, vid);
964         spin_unlock_bh(&br->hash_lock);
965
966         return err;
967 }
968
969 /* Remove neighbor entry with RTM_DELNEIGH */
970 int br_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
971                   struct net_device *dev,
972                   const unsigned char *addr, u16 vid)
973 {
974         struct net_bridge_vlan_group *vg;
975         struct net_bridge_port *p = NULL;
976         struct net_bridge_vlan *v;
977         struct net_bridge *br;
978         int err;
979
980         if (dev->priv_flags & IFF_EBRIDGE) {
981                 br = netdev_priv(dev);
982                 vg = br_vlan_group(br);
983         } else {
984                 p = br_port_get_rtnl(dev);
985                 if (!p) {
986                         pr_info("bridge: RTM_DELNEIGH %s not a bridge port\n",
987                                 dev->name);
988                         return -EINVAL;
989                 }
990                 vg = nbp_vlan_group(p);
991                 br = p->br;
992         }
993
994         if (vid) {
995                 v = br_vlan_find(vg, vid);
996                 if (!v) {
997                         pr_info("bridge: RTM_DELNEIGH with unconfigured vlan %d on %s\n", vid, dev->name);
998                         return -EINVAL;
999                 }
1000
1001                 err = __br_fdb_delete(br, p, addr, vid);
1002         } else {
1003                 err = -ENOENT;
1004                 err &= __br_fdb_delete(br, p, addr, 0);
1005                 if (!vg || !vg->num_vlans)
1006                         return err;
1007
1008                 list_for_each_entry(v, &vg->vlan_list, vlist) {
1009                         if (!br_vlan_should_use(v))
1010                                 continue;
1011                         err &= __br_fdb_delete(br, p, addr, v->vid);
1012                 }
1013         }
1014
1015         return err;
1016 }
1017
1018 int br_fdb_sync_static(struct net_bridge *br, struct net_bridge_port *p)
1019 {
1020         struct net_bridge_fdb_entry *f, *tmp;
1021         int err = 0;
1022
1023         ASSERT_RTNL();
1024
1025         /* the key here is that static entries change only under rtnl */
1026         rcu_read_lock();
1027         hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
1028                 /* We only care for static entries */
1029                 if (!f->is_static)
1030                         continue;
1031                 err = dev_uc_add(p->dev, f->key.addr.addr);
1032                 if (err)
1033                         goto rollback;
1034         }
1035 done:
1036         rcu_read_unlock();
1037
1038         return err;
1039
1040 rollback:
1041         hlist_for_each_entry_rcu(tmp, &br->fdb_list, fdb_node) {
1042                 /* We only care for static entries */
1043                 if (!tmp->is_static)
1044                         continue;
1045                 if (tmp == f)
1046                         break;
1047                 dev_uc_del(p->dev, tmp->key.addr.addr);
1048         }
1049
1050         goto done;
1051 }
1052
1053 void br_fdb_unsync_static(struct net_bridge *br, struct net_bridge_port *p)
1054 {
1055         struct net_bridge_fdb_entry *f;
1056
1057         ASSERT_RTNL();
1058
1059         rcu_read_lock();
1060         hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
1061                 /* We only care for static entries */
1062                 if (!f->is_static)
1063                         continue;
1064
1065                 dev_uc_del(p->dev, f->key.addr.addr);
1066         }
1067         rcu_read_unlock();
1068 }
1069
1070 int br_fdb_external_learn_add(struct net_bridge *br, struct net_bridge_port *p,
1071                               const unsigned char *addr, u16 vid,
1072                               bool swdev_notify)
1073 {
1074         struct net_bridge_fdb_entry *fdb;
1075         bool modified = false;
1076         int err = 0;
1077
1078         trace_br_fdb_external_learn_add(br, p, addr, vid);
1079
1080         spin_lock_bh(&br->hash_lock);
1081
1082         fdb = br_fdb_find(br, addr, vid);
1083         if (!fdb) {
1084                 fdb = fdb_create(br, p, addr, vid, 0, 0);
1085                 if (!fdb) {
1086                         err = -ENOMEM;
1087                         goto err_unlock;
1088                 }
1089                 fdb->added_by_external_learn = 1;
1090                 fdb_notify(br, fdb, RTM_NEWNEIGH, swdev_notify);
1091         } else {
1092                 fdb->updated = jiffies;
1093
1094                 if (fdb->dst != p) {
1095                         fdb->dst = p;
1096                         modified = true;
1097                 }
1098
1099                 if (fdb->added_by_external_learn) {
1100                         /* Refresh entry */
1101                         fdb->used = jiffies;
1102                 } else if (!fdb->added_by_user) {
1103                         /* Take over SW learned entry */
1104                         fdb->added_by_external_learn = 1;
1105                         modified = true;
1106                 }
1107
1108                 if (modified)
1109                         fdb_notify(br, fdb, RTM_NEWNEIGH, swdev_notify);
1110         }
1111
1112 err_unlock:
1113         spin_unlock_bh(&br->hash_lock);
1114
1115         return err;
1116 }
1117
1118 int br_fdb_external_learn_del(struct net_bridge *br, struct net_bridge_port *p,
1119                               const unsigned char *addr, u16 vid,
1120                               bool swdev_notify)
1121 {
1122         struct net_bridge_fdb_entry *fdb;
1123         int err = 0;
1124
1125         spin_lock_bh(&br->hash_lock);
1126
1127         fdb = br_fdb_find(br, addr, vid);
1128         if (fdb && fdb->added_by_external_learn)
1129                 fdb_delete(br, fdb, swdev_notify);
1130         else
1131                 err = -ENOENT;
1132
1133         spin_unlock_bh(&br->hash_lock);
1134
1135         return err;
1136 }
1137
1138 void br_fdb_offloaded_set(struct net_bridge *br, struct net_bridge_port *p,
1139                           const unsigned char *addr, u16 vid)
1140 {
1141         struct net_bridge_fdb_entry *fdb;
1142
1143         spin_lock_bh(&br->hash_lock);
1144
1145         fdb = br_fdb_find(br, addr, vid);
1146         if (fdb)
1147                 fdb->offloaded = 1;
1148
1149         spin_unlock_bh(&br->hash_lock);
1150 }