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