This source file includes following definitions.
- br_fdb_init
- br_fdb_fini
- br_fdb_hash_init
- br_fdb_hash_fini
- hold_time
- has_expired
- fdb_rcu_free
- fdb_find_rcu
- br_fdb_find
- br_fdb_find_port
- br_fdb_find_rcu
- fdb_add_hw_addr
- fdb_del_hw_addr
- fdb_delete
- fdb_delete_local
- br_fdb_find_delete_local
- br_fdb_changeaddr
- br_fdb_change_mac_address
- br_fdb_cleanup
- br_fdb_flush
- br_fdb_delete_by_port
- br_fdb_test_addr
- br_fdb_fillbuf
- fdb_create
- fdb_insert
- br_fdb_insert
- br_fdb_update
- fdb_to_nud
- fdb_fill_info
- fdb_nlmsg_size
- fdb_notify
- br_fdb_dump
- br_fdb_get
- fdb_add_entry
- __br_fdb_add
- br_fdb_add
- fdb_delete_by_addr_and_port
- __br_fdb_delete
- br_fdb_delete
- br_fdb_sync_static
- br_fdb_unsync_static
- br_fdb_external_learn_add
- br_fdb_external_learn_del
- br_fdb_offloaded_set
- br_fdb_clear_offload
1
2
3
4
5
6
7
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
27 static 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
34 static struct kmem_cache *br_fdb_cache __read_mostly;
35 static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
36 const unsigned char *addr, u16 vid);
37 static void fdb_notify(struct net_bridge *br,
38 const struct net_bridge_fdb_entry *, int, bool);
39
40 int __init br_fdb_init(void)
41 {
42 br_fdb_cache = kmem_cache_create("bridge_fdb_cache",
43 sizeof(struct net_bridge_fdb_entry),
44 0,
45 SLAB_HWCACHE_ALIGN, NULL);
46 if (!br_fdb_cache)
47 return -ENOMEM;
48
49 return 0;
50 }
51
52 void br_fdb_fini(void)
53 {
54 kmem_cache_destroy(br_fdb_cache);
55 }
56
57 int br_fdb_hash_init(struct net_bridge *br)
58 {
59 return rhashtable_init(&br->fdb_hash_tbl, &br_fdb_rht_params);
60 }
61
62 void br_fdb_hash_fini(struct net_bridge *br)
63 {
64 rhashtable_destroy(&br->fdb_hash_tbl);
65 }
66
67
68
69
70 static inline unsigned long hold_time(const struct net_bridge *br)
71 {
72 return br->topology_change ? br->forward_delay : br->ageing_time;
73 }
74
75 static 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
82 static 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
89 static 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
104 static 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
119 struct 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 }
141 EXPORT_SYMBOL_GPL(br_fdb_find_port);
142
143 struct 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
151
152
153
154
155 static 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;
171 undo:
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
179
180
181
182
183 static 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
195 static 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
211 static 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
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
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
245 void 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
258 void 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
270 fdb_delete_local(br, p, f);
271
272
273
274
275
276 if (!vg || !vg->num_vlans)
277 goto insert;
278 }
279 }
280
281 insert:
282
283 fdb_insert(br, p, newaddr, 0);
284
285 if (!vg || !vg->num_vlans)
286 goto done;
287
288
289
290
291
292 list_for_each_entry(v, &vg->vlan_list, vlist)
293 fdb_insert(br, p, newaddr, v->vid);
294
295 done:
296 spin_unlock_bh(&br->hash_lock);
297 }
298
299 void 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
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
317
318
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 }
328 out:
329 spin_unlock_bh(&br->hash_lock);
330 }
331
332 void 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
342
343
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
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
369 void 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
383
384
385
386 void 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
413
414 int 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
434
435
436
437
438
439 int 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
457 if (!f->dst)
458 continue;
459
460 if (skip) {
461 --skip;
462 continue;
463 }
464
465
466 memcpy(fe->mac_addr, f->key.addr.addr, ETH_ALEN);
467
468
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
483 static 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
516 static 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
527
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
546 int 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
557 void 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
564 if (hold_time(br) == 0)
565 return;
566
567
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
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
583 if (unlikely(source != fdb->dst && !fdb->is_sticky)) {
584 fdb->dst = source;
585 fdb_modified = true;
586
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
610
611
612 spin_unlock(&br->hash_lock);
613 }
614 }
615
616 static 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
629 static 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
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)
685 + nla_total_size(sizeof(u32))
686 + nla_total_size(sizeof(u16))
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
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
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
746
747
748
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 int 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);
792 errout:
793 rcu_read_unlock();
794 return err;
795 }
796
797
798 static 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
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
881 static 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
911 int 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
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
963
964
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
975 out:
976 return err;
977 }
978
979 static 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
994 static 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
1008 int 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
1056 int 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
1064 rcu_read_lock();
1065 hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
1066
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 }
1073 done:
1074 rcu_read_unlock();
1075
1076 return err;
1077
1078 rollback:
1079 hlist_for_each_entry_rcu(tmp, &br->fdb_list, fdb_node) {
1080
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
1091 void 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
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
1108 int 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
1141 fdb->used = jiffies;
1142 } else if (!fdb->added_by_user) {
1143
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
1155 err_unlock:
1156 spin_unlock_bh(&br->hash_lock);
1157
1158 return err;
1159 }
1160
1161 int 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
1181 void 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
1195 void 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 }
1213 EXPORT_SYMBOL_GPL(br_fdb_clear_offload);