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 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 /* if topology_changing then use forward_delay (default 15 sec) 68 * otherwise keep longer (default 5 minutes) 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 !test_bit(BR_FDB_STATIC, &fdb->flags) && 79 !test_bit(BR_FDB_ADDED_BY_EXT_LEARN, &fdb->flags) && 80 time_before_eq(fdb->updated + hold_time(br), jiffies); 81 } 82 83 static void fdb_rcu_free(struct rcu_head *head) 84 { 85 struct net_bridge_fdb_entry *ent 86 = container_of(head, struct net_bridge_fdb_entry, rcu); 87 kmem_cache_free(br_fdb_cache, ent); 88 } 89 90 static struct net_bridge_fdb_entry *fdb_find_rcu(struct rhashtable *tbl, 91 const unsigned char *addr, 92 __u16 vid) 93 { 94 struct net_bridge_fdb_key key; 95 96 WARN_ON_ONCE(!rcu_read_lock_held()); 97 98 key.vlan_id = vid; 99 memcpy(key.addr.addr, addr, sizeof(key.addr.addr)); 100 101 return rhashtable_lookup(tbl, &key, br_fdb_rht_params); 102 } 103 104 /* requires bridge hash_lock */ 105 static struct net_bridge_fdb_entry *br_fdb_find(struct net_bridge *br, 106 const unsigned char *addr, 107 __u16 vid) 108 { 109 struct net_bridge_fdb_entry *fdb; 110 111 lockdep_assert_held_once(&br->hash_lock); 112 113 rcu_read_lock(); 114 fdb = fdb_find_rcu(&br->fdb_hash_tbl, addr, vid); 115 rcu_read_unlock(); 116 117 return fdb; 118 } 119 120 struct net_device *br_fdb_find_port(const struct net_device *br_dev, 121 const unsigned char *addr, 122 __u16 vid) 123 { 124 struct net_bridge_fdb_entry *f; 125 struct net_device *dev = NULL; 126 struct net_bridge *br; 127 128 ASSERT_RTNL(); 129 130 if (!netif_is_bridge_master(br_dev)) 131 return NULL; 132 133 br = netdev_priv(br_dev); 134 rcu_read_lock(); 135 f = br_fdb_find_rcu(br, addr, vid); 136 if (f && f->dst) 137 dev = f->dst->dev; 138 rcu_read_unlock(); 139 140 return dev; 141 } 142 EXPORT_SYMBOL_GPL(br_fdb_find_port); 143 144 struct net_bridge_fdb_entry *br_fdb_find_rcu(struct net_bridge *br, 145 const unsigned char *addr, 146 __u16 vid) 147 { 148 return fdb_find_rcu(&br->fdb_hash_tbl, addr, vid); 149 } 150 151 /* When a static FDB entry is added, the mac address from the entry is 152 * added to the bridge private HW address list and all required ports 153 * are then updated with the new information. 154 * Called under RTNL. 155 */ 156 static void fdb_add_hw_addr(struct net_bridge *br, const unsigned char *addr) 157 { 158 int err; 159 struct net_bridge_port *p; 160 161 ASSERT_RTNL(); 162 163 list_for_each_entry(p, &br->port_list, list) { 164 if (!br_promisc_port(p)) { 165 err = dev_uc_add(p->dev, addr); 166 if (err) 167 goto undo; 168 } 169 } 170 171 return; 172 undo: 173 list_for_each_entry_continue_reverse(p, &br->port_list, list) { 174 if (!br_promisc_port(p)) 175 dev_uc_del(p->dev, addr); 176 } 177 } 178 179 /* When a static FDB entry is deleted, the HW address from that entry is 180 * also removed from the bridge private HW address list and updates all 181 * the ports with needed information. 182 * Called under RTNL. 183 */ 184 static void fdb_del_hw_addr(struct net_bridge *br, const unsigned char *addr) 185 { 186 struct net_bridge_port *p; 187 188 ASSERT_RTNL(); 189 190 list_for_each_entry(p, &br->port_list, list) { 191 if (!br_promisc_port(p)) 192 dev_uc_del(p->dev, addr); 193 } 194 } 195 196 static void fdb_delete(struct net_bridge *br, struct net_bridge_fdb_entry *f, 197 bool swdev_notify) 198 { 199 trace_fdb_delete(br, f); 200 201 if (test_bit(BR_FDB_STATIC, &f->flags)) 202 fdb_del_hw_addr(br, f->key.addr.addr); 203 204 hlist_del_init_rcu(&f->fdb_node); 205 rhashtable_remove_fast(&br->fdb_hash_tbl, &f->rhnode, 206 br_fdb_rht_params); 207 fdb_notify(br, f, RTM_DELNEIGH, swdev_notify); 208 call_rcu(&f->rcu, fdb_rcu_free); 209 } 210 211 /* Delete a local entry if no other port had the same address. */ 212 static void fdb_delete_local(struct net_bridge *br, 213 const struct net_bridge_port *p, 214 struct net_bridge_fdb_entry *f) 215 { 216 const unsigned char *addr = f->key.addr.addr; 217 struct net_bridge_vlan_group *vg; 218 const struct net_bridge_vlan *v; 219 struct net_bridge_port *op; 220 u16 vid = f->key.vlan_id; 221 222 /* Maybe another port has same hw addr? */ 223 list_for_each_entry(op, &br->port_list, list) { 224 vg = nbp_vlan_group(op); 225 if (op != p && ether_addr_equal(op->dev->dev_addr, addr) && 226 (!vid || br_vlan_find(vg, vid))) { 227 f->dst = op; 228 clear_bit(BR_FDB_ADDED_BY_USER, &f->flags); 229 return; 230 } 231 } 232 233 vg = br_vlan_group(br); 234 v = br_vlan_find(vg, vid); 235 /* Maybe bridge device has same hw addr? */ 236 if (p && ether_addr_equal(br->dev->dev_addr, addr) && 237 (!vid || (v && br_vlan_should_use(v)))) { 238 f->dst = NULL; 239 clear_bit(BR_FDB_ADDED_BY_USER, &f->flags); 240 return; 241 } 242 243 fdb_delete(br, f, true); 244 } 245 246 void br_fdb_find_delete_local(struct net_bridge *br, 247 const struct net_bridge_port *p, 248 const unsigned char *addr, u16 vid) 249 { 250 struct net_bridge_fdb_entry *f; 251 252 spin_lock_bh(&br->hash_lock); 253 f = br_fdb_find(br, addr, vid); 254 if (f && test_bit(BR_FDB_LOCAL, &f->flags) && 255 !test_bit(BR_FDB_ADDED_BY_USER, &f->flags) && f->dst == p) 256 fdb_delete_local(br, p, f); 257 spin_unlock_bh(&br->hash_lock); 258 } 259 260 void br_fdb_changeaddr(struct net_bridge_port *p, const unsigned char *newaddr) 261 { 262 struct net_bridge_vlan_group *vg; 263 struct net_bridge_fdb_entry *f; 264 struct net_bridge *br = p->br; 265 struct net_bridge_vlan *v; 266 267 spin_lock_bh(&br->hash_lock); 268 vg = nbp_vlan_group(p); 269 hlist_for_each_entry(f, &br->fdb_list, fdb_node) { 270 if (f->dst == p && test_bit(BR_FDB_LOCAL, &f->flags) && 271 !test_bit(BR_FDB_ADDED_BY_USER, &f->flags)) { 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 && test_bit(BR_FDB_LOCAL, &f->flags) && 313 !f->dst && !test_bit(BR_FDB_ADDED_BY_USER, &f->flags)) 314 fdb_delete_local(br, NULL, f); 315 316 fdb_insert(br, NULL, newaddr, 0); 317 vg = br_vlan_group(br); 318 if (!vg || !vg->num_vlans) 319 goto out; 320 /* Now remove and add entries for every VLAN configured on the 321 * bridge. This function runs under RTNL so the bitmap will not 322 * change from under us. 323 */ 324 list_for_each_entry(v, &vg->vlan_list, vlist) { 325 if (!br_vlan_should_use(v)) 326 continue; 327 f = br_fdb_find(br, br->dev->dev_addr, v->vid); 328 if (f && test_bit(BR_FDB_LOCAL, &f->flags) && 329 !f->dst && !test_bit(BR_FDB_ADDED_BY_USER, &f->flags)) 330 fdb_delete_local(br, NULL, f); 331 fdb_insert(br, NULL, newaddr, v->vid); 332 } 333 out: 334 spin_unlock_bh(&br->hash_lock); 335 } 336 337 void br_fdb_cleanup(struct work_struct *work) 338 { 339 struct net_bridge *br = container_of(work, struct net_bridge, 340 gc_work.work); 341 struct net_bridge_fdb_entry *f = NULL; 342 unsigned long delay = hold_time(br); 343 unsigned long work_delay = delay; 344 unsigned long now = jiffies; 345 346 /* this part is tricky, in order to avoid blocking learning and 347 * consequently forwarding, we rely on rcu to delete objects with 348 * delayed freeing allowing us to continue traversing 349 */ 350 rcu_read_lock(); 351 hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) { 352 unsigned long this_timer = f->updated + delay; 353 354 if (test_bit(BR_FDB_STATIC, &f->flags) || 355 test_bit(BR_FDB_ADDED_BY_EXT_LEARN, &f->flags)) { 356 if (test_bit(BR_FDB_NOTIFY, &f->flags)) { 357 if (time_after(this_timer, now)) 358 work_delay = min(work_delay, 359 this_timer - now); 360 else if (!test_and_set_bit(BR_FDB_NOTIFY_INACTIVE, 361 &f->flags)) 362 fdb_notify(br, f, RTM_NEWNEIGH, false); 363 } 364 continue; 365 } 366 367 if (time_after(this_timer, now)) { 368 work_delay = min(work_delay, this_timer - now); 369 } else { 370 spin_lock_bh(&br->hash_lock); 371 if (!hlist_unhashed(&f->fdb_node)) 372 fdb_delete(br, f, true); 373 spin_unlock_bh(&br->hash_lock); 374 } 375 } 376 rcu_read_unlock(); 377 378 /* Cleanup minimum 10 milliseconds apart */ 379 work_delay = max_t(unsigned long, work_delay, msecs_to_jiffies(10)); 380 mod_delayed_work(system_long_wq, &br->gc_work, work_delay); 381 } 382 383 /* Completely flush all dynamic entries in forwarding database.*/ 384 void br_fdb_flush(struct net_bridge *br) 385 { 386 struct net_bridge_fdb_entry *f; 387 struct hlist_node *tmp; 388 389 spin_lock_bh(&br->hash_lock); 390 hlist_for_each_entry_safe(f, tmp, &br->fdb_list, fdb_node) { 391 if (!test_bit(BR_FDB_STATIC, &f->flags)) 392 fdb_delete(br, f, true); 393 } 394 spin_unlock_bh(&br->hash_lock); 395 } 396 397 /* Flush all entries referring to a specific port. 398 * if do_all is set also flush static entries 399 * if vid is set delete all entries that match the vlan_id 400 */ 401 void br_fdb_delete_by_port(struct net_bridge *br, 402 const struct net_bridge_port *p, 403 u16 vid, 404 int do_all) 405 { 406 struct net_bridge_fdb_entry *f; 407 struct hlist_node *tmp; 408 409 spin_lock_bh(&br->hash_lock); 410 hlist_for_each_entry_safe(f, tmp, &br->fdb_list, fdb_node) { 411 if (f->dst != p) 412 continue; 413 414 if (!do_all) 415 if (test_bit(BR_FDB_STATIC, &f->flags) || 416 (test_bit(BR_FDB_ADDED_BY_EXT_LEARN, &f->flags) && 417 !test_bit(BR_FDB_OFFLOADED, &f->flags)) || 418 (vid && f->key.vlan_id != vid)) 419 continue; 420 421 if (test_bit(BR_FDB_LOCAL, &f->flags)) 422 fdb_delete_local(br, p, f); 423 else 424 fdb_delete(br, f, true); 425 } 426 spin_unlock_bh(&br->hash_lock); 427 } 428 429 #if IS_ENABLED(CONFIG_ATM_LANE) 430 /* Interface used by ATM LANE hook to test 431 * if an addr is on some other bridge port */ 432 int br_fdb_test_addr(struct net_device *dev, unsigned char *addr) 433 { 434 struct net_bridge_fdb_entry *fdb; 435 struct net_bridge_port *port; 436 int ret; 437 438 rcu_read_lock(); 439 port = br_port_get_rcu(dev); 440 if (!port) 441 ret = 0; 442 else { 443 const struct net_bridge_port *dst = NULL; 444 445 fdb = br_fdb_find_rcu(port->br, addr, 0); 446 if (fdb) 447 dst = READ_ONCE(fdb->dst); 448 449 ret = dst && dst->dev != dev && 450 dst->state == BR_STATE_FORWARDING; 451 } 452 rcu_read_unlock(); 453 454 return ret; 455 } 456 #endif /* CONFIG_ATM_LANE */ 457 458 /* 459 * Fill buffer with forwarding table records in 460 * the API format. 461 */ 462 int br_fdb_fillbuf(struct net_bridge *br, void *buf, 463 unsigned long maxnum, unsigned long skip) 464 { 465 struct net_bridge_fdb_entry *f; 466 struct __fdb_entry *fe = buf; 467 int num = 0; 468 469 memset(buf, 0, maxnum*sizeof(struct __fdb_entry)); 470 471 rcu_read_lock(); 472 hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) { 473 if (num >= maxnum) 474 break; 475 476 if (has_expired(br, f)) 477 continue; 478 479 /* ignore pseudo entry for local MAC address */ 480 if (!f->dst) 481 continue; 482 483 if (skip) { 484 --skip; 485 continue; 486 } 487 488 /* convert from internal format to API */ 489 memcpy(fe->mac_addr, f->key.addr.addr, ETH_ALEN); 490 491 /* due to ABI compat need to split into hi/lo */ 492 fe->port_no = f->dst->port_no; 493 fe->port_hi = f->dst->port_no >> 8; 494 495 fe->is_local = test_bit(BR_FDB_LOCAL, &f->flags); 496 if (!test_bit(BR_FDB_STATIC, &f->flags)) 497 fe->ageing_timer_value = jiffies_delta_to_clock_t(jiffies - f->updated); 498 ++fe; 499 ++num; 500 } 501 rcu_read_unlock(); 502 503 return num; 504 } 505 506 static struct net_bridge_fdb_entry *fdb_create(struct net_bridge *br, 507 struct net_bridge_port *source, 508 const unsigned char *addr, 509 __u16 vid, 510 unsigned long flags) 511 { 512 struct net_bridge_fdb_entry *fdb; 513 514 fdb = kmem_cache_alloc(br_fdb_cache, GFP_ATOMIC); 515 if (fdb) { 516 memcpy(fdb->key.addr.addr, addr, ETH_ALEN); 517 WRITE_ONCE(fdb->dst, source); 518 fdb->key.vlan_id = vid; 519 fdb->flags = flags; 520 fdb->updated = fdb->used = jiffies; 521 if (rhashtable_lookup_insert_fast(&br->fdb_hash_tbl, 522 &fdb->rhnode, 523 br_fdb_rht_params)) { 524 kmem_cache_free(br_fdb_cache, fdb); 525 fdb = NULL; 526 } else { 527 hlist_add_head_rcu(&fdb->fdb_node, &br->fdb_list); 528 } 529 } 530 return fdb; 531 } 532 533 static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source, 534 const unsigned char *addr, u16 vid) 535 { 536 struct net_bridge_fdb_entry *fdb; 537 538 if (!is_valid_ether_addr(addr)) 539 return -EINVAL; 540 541 fdb = br_fdb_find(br, addr, vid); 542 if (fdb) { 543 /* it is okay to have multiple ports with same 544 * address, just use the first one. 545 */ 546 if (test_bit(BR_FDB_LOCAL, &fdb->flags)) 547 return 0; 548 br_warn(br, "adding interface %s with same address as a received packet (addr:%pM, vlan:%u)\n", 549 source ? source->dev->name : br->dev->name, addr, vid); 550 fdb_delete(br, fdb, true); 551 } 552 553 fdb = fdb_create(br, source, addr, vid, 554 BIT(BR_FDB_LOCAL) | BIT(BR_FDB_STATIC)); 555 if (!fdb) 556 return -ENOMEM; 557 558 fdb_add_hw_addr(br, addr); 559 fdb_notify(br, fdb, RTM_NEWNEIGH, true); 560 return 0; 561 } 562 563 /* Add entry for local address of interface */ 564 int br_fdb_insert(struct net_bridge *br, struct net_bridge_port *source, 565 const unsigned char *addr, u16 vid) 566 { 567 int ret; 568 569 spin_lock_bh(&br->hash_lock); 570 ret = fdb_insert(br, source, addr, vid); 571 spin_unlock_bh(&br->hash_lock); 572 return ret; 573 } 574 575 /* returns true if the fdb was modified */ 576 static bool __fdb_mark_active(struct net_bridge_fdb_entry *fdb) 577 { 578 return !!(test_bit(BR_FDB_NOTIFY_INACTIVE, &fdb->flags) && 579 test_and_clear_bit(BR_FDB_NOTIFY_INACTIVE, &fdb->flags)); 580 } 581 582 void br_fdb_update(struct net_bridge *br, struct net_bridge_port *source, 583 const unsigned char *addr, u16 vid, unsigned long flags) 584 { 585 struct net_bridge_fdb_entry *fdb; 586 587 /* some users want to always flood. */ 588 if (hold_time(br) == 0) 589 return; 590 591 fdb = fdb_find_rcu(&br->fdb_hash_tbl, addr, vid); 592 if (likely(fdb)) { 593 /* attempt to update an entry for a local interface */ 594 if (unlikely(test_bit(BR_FDB_LOCAL, &fdb->flags))) { 595 if (net_ratelimit()) 596 br_warn(br, "received packet on %s with own address as source address (addr:%pM, vlan:%u)\n", 597 source->dev->name, addr, vid); 598 } else { 599 unsigned long now = jiffies; 600 bool fdb_modified = false; 601 602 if (now != fdb->updated) { 603 fdb->updated = now; 604 fdb_modified = __fdb_mark_active(fdb); 605 } 606 607 /* fastpath: update of existing entry */ 608 if (unlikely(source != READ_ONCE(fdb->dst) && 609 !test_bit(BR_FDB_STICKY, &fdb->flags))) { 610 br_switchdev_fdb_notify(br, fdb, RTM_DELNEIGH); 611 WRITE_ONCE(fdb->dst, source); 612 fdb_modified = true; 613 /* Take over HW learned entry */ 614 if (unlikely(test_bit(BR_FDB_ADDED_BY_EXT_LEARN, 615 &fdb->flags))) 616 clear_bit(BR_FDB_ADDED_BY_EXT_LEARN, 617 &fdb->flags); 618 } 619 620 if (unlikely(test_bit(BR_FDB_ADDED_BY_USER, &flags))) 621 set_bit(BR_FDB_ADDED_BY_USER, &fdb->flags); 622 if (unlikely(fdb_modified)) { 623 trace_br_fdb_update(br, source, addr, vid, flags); 624 fdb_notify(br, fdb, RTM_NEWNEIGH, true); 625 } 626 } 627 } else { 628 spin_lock(&br->hash_lock); 629 fdb = fdb_create(br, source, addr, vid, flags); 630 if (fdb) { 631 trace_br_fdb_update(br, source, addr, vid, flags); 632 fdb_notify(br, fdb, RTM_NEWNEIGH, true); 633 } 634 /* else we lose race and someone else inserts 635 * it first, don't bother updating 636 */ 637 spin_unlock(&br->hash_lock); 638 } 639 } 640 641 static int fdb_to_nud(const struct net_bridge *br, 642 const struct net_bridge_fdb_entry *fdb) 643 { 644 if (test_bit(BR_FDB_LOCAL, &fdb->flags)) 645 return NUD_PERMANENT; 646 else if (test_bit(BR_FDB_STATIC, &fdb->flags)) 647 return NUD_NOARP; 648 else if (has_expired(br, fdb)) 649 return NUD_STALE; 650 else 651 return NUD_REACHABLE; 652 } 653 654 static int fdb_fill_info(struct sk_buff *skb, const struct net_bridge *br, 655 const struct net_bridge_fdb_entry *fdb, 656 u32 portid, u32 seq, int type, unsigned int flags) 657 { 658 const struct net_bridge_port *dst = READ_ONCE(fdb->dst); 659 unsigned long now = jiffies; 660 struct nda_cacheinfo ci; 661 struct nlmsghdr *nlh; 662 struct ndmsg *ndm; 663 664 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags); 665 if (nlh == NULL) 666 return -EMSGSIZE; 667 668 ndm = nlmsg_data(nlh); 669 ndm->ndm_family = AF_BRIDGE; 670 ndm->ndm_pad1 = 0; 671 ndm->ndm_pad2 = 0; 672 ndm->ndm_flags = 0; 673 ndm->ndm_type = 0; 674 ndm->ndm_ifindex = dst ? dst->dev->ifindex : br->dev->ifindex; 675 ndm->ndm_state = fdb_to_nud(br, fdb); 676 677 if (test_bit(BR_FDB_OFFLOADED, &fdb->flags)) 678 ndm->ndm_flags |= NTF_OFFLOADED; 679 if (test_bit(BR_FDB_ADDED_BY_EXT_LEARN, &fdb->flags)) 680 ndm->ndm_flags |= NTF_EXT_LEARNED; 681 if (test_bit(BR_FDB_STICKY, &fdb->flags)) 682 ndm->ndm_flags |= NTF_STICKY; 683 684 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->key.addr)) 685 goto nla_put_failure; 686 if (nla_put_u32(skb, NDA_MASTER, br->dev->ifindex)) 687 goto nla_put_failure; 688 ci.ndm_used = jiffies_to_clock_t(now - fdb->used); 689 ci.ndm_confirmed = 0; 690 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated); 691 ci.ndm_refcnt = 0; 692 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci)) 693 goto nla_put_failure; 694 695 if (fdb->key.vlan_id && nla_put(skb, NDA_VLAN, sizeof(u16), 696 &fdb->key.vlan_id)) 697 goto nla_put_failure; 698 699 if (test_bit(BR_FDB_NOTIFY, &fdb->flags)) { 700 struct nlattr *nest = nla_nest_start(skb, NDA_FDB_EXT_ATTRS); 701 u8 notify_bits = FDB_NOTIFY_BIT; 702 703 if (!nest) 704 goto nla_put_failure; 705 if (test_bit(BR_FDB_NOTIFY_INACTIVE, &fdb->flags)) 706 notify_bits |= FDB_NOTIFY_INACTIVE_BIT; 707 708 if (nla_put_u8(skb, NFEA_ACTIVITY_NOTIFY, notify_bits)) { 709 nla_nest_cancel(skb, nest); 710 goto nla_put_failure; 711 } 712 713 nla_nest_end(skb, nest); 714 } 715 716 nlmsg_end(skb, nlh); 717 return 0; 718 719 nla_put_failure: 720 nlmsg_cancel(skb, nlh); 721 return -EMSGSIZE; 722 } 723 724 static inline size_t fdb_nlmsg_size(void) 725 { 726 return NLMSG_ALIGN(sizeof(struct ndmsg)) 727 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */ 728 + nla_total_size(sizeof(u32)) /* NDA_MASTER */ 729 + nla_total_size(sizeof(u16)) /* NDA_VLAN */ 730 + nla_total_size(sizeof(struct nda_cacheinfo)) 731 + nla_total_size(0) /* NDA_FDB_EXT_ATTRS */ 732 + nla_total_size(sizeof(u8)); /* NFEA_ACTIVITY_NOTIFY */ 733 } 734 735 static int br_fdb_replay_one(struct notifier_block *nb, 736 const struct net_bridge_fdb_entry *fdb, 737 struct net_device *dev, unsigned long action, 738 const void *ctx) 739 { 740 struct switchdev_notifier_fdb_info item; 741 int err; 742 743 item.addr = fdb->key.addr.addr; 744 item.vid = fdb->key.vlan_id; 745 item.added_by_user = test_bit(BR_FDB_ADDED_BY_USER, &fdb->flags); 746 item.offloaded = test_bit(BR_FDB_OFFLOADED, &fdb->flags); 747 item.is_local = test_bit(BR_FDB_LOCAL, &fdb->flags); 748 item.info.dev = dev; 749 item.info.ctx = ctx; 750 751 err = nb->notifier_call(nb, action, &item); 752 return notifier_to_errno(err); 753 } 754 755 int br_fdb_replay(const struct net_device *br_dev, const struct net_device *dev, 756 const void *ctx, bool adding, struct notifier_block *nb) 757 { 758 struct net_bridge_fdb_entry *fdb; 759 struct net_bridge *br; 760 unsigned long action; 761 int err = 0; 762 763 if (!netif_is_bridge_master(br_dev)) 764 return -EINVAL; 765 766 if (!netif_is_bridge_port(dev) && !netif_is_bridge_master(dev)) 767 return -EINVAL; 768 769 br = netdev_priv(br_dev); 770 771 if (adding) 772 action = SWITCHDEV_FDB_ADD_TO_DEVICE; 773 else 774 action = SWITCHDEV_FDB_DEL_TO_DEVICE; 775 776 rcu_read_lock(); 777 778 hlist_for_each_entry_rcu(fdb, &br->fdb_list, fdb_node) { 779 const struct net_bridge_port *dst = READ_ONCE(fdb->dst); 780 struct net_device *dst_dev; 781 782 dst_dev = dst ? dst->dev : br->dev; 783 if (dst_dev != br_dev && dst_dev != dev) 784 continue; 785 786 err = br_fdb_replay_one(nb, fdb, dst_dev, action, ctx); 787 if (err) 788 break; 789 } 790 791 rcu_read_unlock(); 792 793 return err; 794 } 795 EXPORT_SYMBOL_GPL(br_fdb_replay); 796 797 static void fdb_notify(struct net_bridge *br, 798 const struct net_bridge_fdb_entry *fdb, int type, 799 bool swdev_notify) 800 { 801 struct net *net = dev_net(br->dev); 802 struct sk_buff *skb; 803 int err = -ENOBUFS; 804 805 if (swdev_notify) 806 br_switchdev_fdb_notify(br, fdb, type); 807 808 skb = nlmsg_new(fdb_nlmsg_size(), GFP_ATOMIC); 809 if (skb == NULL) 810 goto errout; 811 812 err = fdb_fill_info(skb, br, fdb, 0, 0, type, 0); 813 if (err < 0) { 814 /* -EMSGSIZE implies BUG in fdb_nlmsg_size() */ 815 WARN_ON(err == -EMSGSIZE); 816 kfree_skb(skb); 817 goto errout; 818 } 819 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC); 820 return; 821 errout: 822 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err); 823 } 824 825 /* Dump information about entries, in response to GETNEIGH */ 826 int br_fdb_dump(struct sk_buff *skb, 827 struct netlink_callback *cb, 828 struct net_device *dev, 829 struct net_device *filter_dev, 830 int *idx) 831 { 832 struct net_bridge *br = netdev_priv(dev); 833 struct net_bridge_fdb_entry *f; 834 int err = 0; 835 836 if (!(dev->priv_flags & IFF_EBRIDGE)) 837 return err; 838 839 if (!filter_dev) { 840 err = ndo_dflt_fdb_dump(skb, cb, dev, NULL, idx); 841 if (err < 0) 842 return err; 843 } 844 845 rcu_read_lock(); 846 hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) { 847 if (*idx < cb->args[2]) 848 goto skip; 849 if (filter_dev && (!f->dst || f->dst->dev != filter_dev)) { 850 if (filter_dev != dev) 851 goto skip; 852 /* !f->dst is a special case for bridge 853 * It means the MAC belongs to the bridge 854 * Therefore need a little more filtering 855 * we only want to dump the !f->dst case 856 */ 857 if (f->dst) 858 goto skip; 859 } 860 if (!filter_dev && f->dst) 861 goto skip; 862 863 err = fdb_fill_info(skb, br, f, 864 NETLINK_CB(cb->skb).portid, 865 cb->nlh->nlmsg_seq, 866 RTM_NEWNEIGH, 867 NLM_F_MULTI); 868 if (err < 0) 869 break; 870 skip: 871 *idx += 1; 872 } 873 rcu_read_unlock(); 874 875 return err; 876 } 877 878 int br_fdb_get(struct sk_buff *skb, 879 struct nlattr *tb[], 880 struct net_device *dev, 881 const unsigned char *addr, 882 u16 vid, u32 portid, u32 seq, 883 struct netlink_ext_ack *extack) 884 { 885 struct net_bridge *br = netdev_priv(dev); 886 struct net_bridge_fdb_entry *f; 887 int err = 0; 888 889 rcu_read_lock(); 890 f = br_fdb_find_rcu(br, addr, vid); 891 if (!f) { 892 NL_SET_ERR_MSG(extack, "Fdb entry not found"); 893 err = -ENOENT; 894 goto errout; 895 } 896 897 err = fdb_fill_info(skb, br, f, portid, seq, 898 RTM_NEWNEIGH, 0); 899 errout: 900 rcu_read_unlock(); 901 return err; 902 } 903 904 /* returns true if the fdb is modified */ 905 static bool fdb_handle_notify(struct net_bridge_fdb_entry *fdb, u8 notify) 906 { 907 bool modified = false; 908 909 /* allow to mark an entry as inactive, usually done on creation */ 910 if ((notify & FDB_NOTIFY_INACTIVE_BIT) && 911 !test_and_set_bit(BR_FDB_NOTIFY_INACTIVE, &fdb->flags)) 912 modified = true; 913 914 if ((notify & FDB_NOTIFY_BIT) && 915 !test_and_set_bit(BR_FDB_NOTIFY, &fdb->flags)) { 916 /* enabled activity tracking */ 917 modified = true; 918 } else if (!(notify & FDB_NOTIFY_BIT) && 919 test_and_clear_bit(BR_FDB_NOTIFY, &fdb->flags)) { 920 /* disabled activity tracking, clear notify state */ 921 clear_bit(BR_FDB_NOTIFY_INACTIVE, &fdb->flags); 922 modified = true; 923 } 924 925 return modified; 926 } 927 928 /* Update (create or replace) forwarding database entry */ 929 static int fdb_add_entry(struct net_bridge *br, struct net_bridge_port *source, 930 const u8 *addr, struct ndmsg *ndm, u16 flags, u16 vid, 931 struct nlattr *nfea_tb[]) 932 { 933 bool is_sticky = !!(ndm->ndm_flags & NTF_STICKY); 934 bool refresh = !nfea_tb[NFEA_DONT_REFRESH]; 935 struct net_bridge_fdb_entry *fdb; 936 u16 state = ndm->ndm_state; 937 bool modified = false; 938 u8 notify = 0; 939 940 /* If the port cannot learn allow only local and static entries */ 941 if (source && !(state & NUD_PERMANENT) && !(state & NUD_NOARP) && 942 !(source->state == BR_STATE_LEARNING || 943 source->state == BR_STATE_FORWARDING)) 944 return -EPERM; 945 946 if (!source && !(state & NUD_PERMANENT)) { 947 pr_info("bridge: RTM_NEWNEIGH %s without NUD_PERMANENT\n", 948 br->dev->name); 949 return -EINVAL; 950 } 951 952 if (is_sticky && (state & NUD_PERMANENT)) 953 return -EINVAL; 954 955 if (nfea_tb[NFEA_ACTIVITY_NOTIFY]) { 956 notify = nla_get_u8(nfea_tb[NFEA_ACTIVITY_NOTIFY]); 957 if ((notify & ~BR_FDB_NOTIFY_SETTABLE_BITS) || 958 (notify & BR_FDB_NOTIFY_SETTABLE_BITS) == FDB_NOTIFY_INACTIVE_BIT) 959 return -EINVAL; 960 } 961 962 fdb = br_fdb_find(br, addr, vid); 963 if (fdb == NULL) { 964 if (!(flags & NLM_F_CREATE)) 965 return -ENOENT; 966 967 fdb = fdb_create(br, source, addr, vid, 0); 968 if (!fdb) 969 return -ENOMEM; 970 971 modified = true; 972 } else { 973 if (flags & NLM_F_EXCL) 974 return -EEXIST; 975 976 if (READ_ONCE(fdb->dst) != source) { 977 WRITE_ONCE(fdb->dst, source); 978 modified = true; 979 } 980 } 981 982 if (fdb_to_nud(br, fdb) != state) { 983 if (state & NUD_PERMANENT) { 984 set_bit(BR_FDB_LOCAL, &fdb->flags); 985 if (!test_and_set_bit(BR_FDB_STATIC, &fdb->flags)) 986 fdb_add_hw_addr(br, addr); 987 } else if (state & NUD_NOARP) { 988 clear_bit(BR_FDB_LOCAL, &fdb->flags); 989 if (!test_and_set_bit(BR_FDB_STATIC, &fdb->flags)) 990 fdb_add_hw_addr(br, addr); 991 } else { 992 clear_bit(BR_FDB_LOCAL, &fdb->flags); 993 if (test_and_clear_bit(BR_FDB_STATIC, &fdb->flags)) 994 fdb_del_hw_addr(br, addr); 995 } 996 997 modified = true; 998 } 999 1000 if (is_sticky != test_bit(BR_FDB_STICKY, &fdb->flags)) { 1001 change_bit(BR_FDB_STICKY, &fdb->flags); 1002 modified = true; 1003 } 1004 1005 if (fdb_handle_notify(fdb, notify)) 1006 modified = true; 1007 1008 set_bit(BR_FDB_ADDED_BY_USER, &fdb->flags); 1009 1010 fdb->used = jiffies; 1011 if (modified) { 1012 if (refresh) 1013 fdb->updated = jiffies; 1014 fdb_notify(br, fdb, RTM_NEWNEIGH, true); 1015 } 1016 1017 return 0; 1018 } 1019 1020 static int __br_fdb_add(struct ndmsg *ndm, struct net_bridge *br, 1021 struct net_bridge_port *p, const unsigned char *addr, 1022 u16 nlh_flags, u16 vid, struct nlattr *nfea_tb[]) 1023 { 1024 int err = 0; 1025 1026 if (ndm->ndm_flags & NTF_USE) { 1027 if (!p) { 1028 pr_info("bridge: RTM_NEWNEIGH %s with NTF_USE is not supported\n", 1029 br->dev->name); 1030 return -EINVAL; 1031 } 1032 if (!nbp_state_should_learn(p)) 1033 return 0; 1034 1035 local_bh_disable(); 1036 rcu_read_lock(); 1037 br_fdb_update(br, p, addr, vid, BIT(BR_FDB_ADDED_BY_USER)); 1038 rcu_read_unlock(); 1039 local_bh_enable(); 1040 } else if (ndm->ndm_flags & NTF_EXT_LEARNED) { 1041 err = br_fdb_external_learn_add(br, p, addr, vid, true); 1042 } else { 1043 spin_lock_bh(&br->hash_lock); 1044 err = fdb_add_entry(br, p, addr, ndm, nlh_flags, vid, nfea_tb); 1045 spin_unlock_bh(&br->hash_lock); 1046 } 1047 1048 return err; 1049 } 1050 1051 static const struct nla_policy br_nda_fdb_pol[NFEA_MAX + 1] = { 1052 [NFEA_ACTIVITY_NOTIFY] = { .type = NLA_U8 }, 1053 [NFEA_DONT_REFRESH] = { .type = NLA_FLAG }, 1054 }; 1055 1056 /* Add new permanent fdb entry with RTM_NEWNEIGH */ 1057 int br_fdb_add(struct ndmsg *ndm, struct nlattr *tb[], 1058 struct net_device *dev, 1059 const unsigned char *addr, u16 vid, u16 nlh_flags, 1060 struct netlink_ext_ack *extack) 1061 { 1062 struct nlattr *nfea_tb[NFEA_MAX + 1], *attr; 1063 struct net_bridge_vlan_group *vg; 1064 struct net_bridge_port *p = NULL; 1065 struct net_bridge_vlan *v; 1066 struct net_bridge *br = NULL; 1067 int err = 0; 1068 1069 trace_br_fdb_add(ndm, dev, addr, vid, nlh_flags); 1070 1071 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE))) { 1072 pr_info("bridge: RTM_NEWNEIGH with invalid state %#x\n", ndm->ndm_state); 1073 return -EINVAL; 1074 } 1075 1076 if (is_zero_ether_addr(addr)) { 1077 pr_info("bridge: RTM_NEWNEIGH with invalid ether address\n"); 1078 return -EINVAL; 1079 } 1080 1081 if (dev->priv_flags & IFF_EBRIDGE) { 1082 br = netdev_priv(dev); 1083 vg = br_vlan_group(br); 1084 } else { 1085 p = br_port_get_rtnl(dev); 1086 if (!p) { 1087 pr_info("bridge: RTM_NEWNEIGH %s not a bridge port\n", 1088 dev->name); 1089 return -EINVAL; 1090 } 1091 br = p->br; 1092 vg = nbp_vlan_group(p); 1093 } 1094 1095 if (tb[NDA_FDB_EXT_ATTRS]) { 1096 attr = tb[NDA_FDB_EXT_ATTRS]; 1097 err = nla_parse_nested(nfea_tb, NFEA_MAX, attr, 1098 br_nda_fdb_pol, extack); 1099 if (err) 1100 return err; 1101 } else { 1102 memset(nfea_tb, 0, sizeof(struct nlattr *) * (NFEA_MAX + 1)); 1103 } 1104 1105 if (vid) { 1106 v = br_vlan_find(vg, vid); 1107 if (!v || !br_vlan_should_use(v)) { 1108 pr_info("bridge: RTM_NEWNEIGH with unconfigured vlan %d on %s\n", vid, dev->name); 1109 return -EINVAL; 1110 } 1111 1112 /* VID was specified, so use it. */ 1113 err = __br_fdb_add(ndm, br, p, addr, nlh_flags, vid, nfea_tb); 1114 } else { 1115 err = __br_fdb_add(ndm, br, p, addr, nlh_flags, 0, nfea_tb); 1116 if (err || !vg || !vg->num_vlans) 1117 goto out; 1118 1119 /* We have vlans configured on this port and user didn't 1120 * specify a VLAN. To be nice, add/update entry for every 1121 * vlan on this port. 1122 */ 1123 list_for_each_entry(v, &vg->vlan_list, vlist) { 1124 if (!br_vlan_should_use(v)) 1125 continue; 1126 err = __br_fdb_add(ndm, br, p, addr, nlh_flags, v->vid, 1127 nfea_tb); 1128 if (err) 1129 goto out; 1130 } 1131 } 1132 1133 out: 1134 return err; 1135 } 1136 1137 static int fdb_delete_by_addr_and_port(struct net_bridge *br, 1138 const struct net_bridge_port *p, 1139 const u8 *addr, u16 vlan) 1140 { 1141 struct net_bridge_fdb_entry *fdb; 1142 1143 fdb = br_fdb_find(br, addr, vlan); 1144 if (!fdb || READ_ONCE(fdb->dst) != p) 1145 return -ENOENT; 1146 1147 fdb_delete(br, fdb, true); 1148 1149 return 0; 1150 } 1151 1152 static int __br_fdb_delete(struct net_bridge *br, 1153 const struct net_bridge_port *p, 1154 const unsigned char *addr, u16 vid) 1155 { 1156 int err; 1157 1158 spin_lock_bh(&br->hash_lock); 1159 err = fdb_delete_by_addr_and_port(br, p, addr, vid); 1160 spin_unlock_bh(&br->hash_lock); 1161 1162 return err; 1163 } 1164 1165 /* Remove neighbor entry with RTM_DELNEIGH */ 1166 int br_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[], 1167 struct net_device *dev, 1168 const unsigned char *addr, u16 vid) 1169 { 1170 struct net_bridge_vlan_group *vg; 1171 struct net_bridge_port *p = NULL; 1172 struct net_bridge_vlan *v; 1173 struct net_bridge *br; 1174 int err; 1175 1176 if (dev->priv_flags & IFF_EBRIDGE) { 1177 br = netdev_priv(dev); 1178 vg = br_vlan_group(br); 1179 } else { 1180 p = br_port_get_rtnl(dev); 1181 if (!p) { 1182 pr_info("bridge: RTM_DELNEIGH %s not a bridge port\n", 1183 dev->name); 1184 return -EINVAL; 1185 } 1186 vg = nbp_vlan_group(p); 1187 br = p->br; 1188 } 1189 1190 if (vid) { 1191 v = br_vlan_find(vg, vid); 1192 if (!v) { 1193 pr_info("bridge: RTM_DELNEIGH with unconfigured vlan %d on %s\n", vid, dev->name); 1194 return -EINVAL; 1195 } 1196 1197 err = __br_fdb_delete(br, p, addr, vid); 1198 } else { 1199 err = -ENOENT; 1200 err &= __br_fdb_delete(br, p, addr, 0); 1201 if (!vg || !vg->num_vlans) 1202 return err; 1203 1204 list_for_each_entry(v, &vg->vlan_list, vlist) { 1205 if (!br_vlan_should_use(v)) 1206 continue; 1207 err &= __br_fdb_delete(br, p, addr, v->vid); 1208 } 1209 } 1210 1211 return err; 1212 } 1213 1214 int br_fdb_sync_static(struct net_bridge *br, struct net_bridge_port *p) 1215 { 1216 struct net_bridge_fdb_entry *f, *tmp; 1217 int err = 0; 1218 1219 ASSERT_RTNL(); 1220 1221 /* the key here is that static entries change only under rtnl */ 1222 rcu_read_lock(); 1223 hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) { 1224 /* We only care for static entries */ 1225 if (!test_bit(BR_FDB_STATIC, &f->flags)) 1226 continue; 1227 err = dev_uc_add(p->dev, f->key.addr.addr); 1228 if (err) 1229 goto rollback; 1230 } 1231 done: 1232 rcu_read_unlock(); 1233 1234 return err; 1235 1236 rollback: 1237 hlist_for_each_entry_rcu(tmp, &br->fdb_list, fdb_node) { 1238 /* We only care for static entries */ 1239 if (!test_bit(BR_FDB_STATIC, &tmp->flags)) 1240 continue; 1241 if (tmp == f) 1242 break; 1243 dev_uc_del(p->dev, tmp->key.addr.addr); 1244 } 1245 1246 goto done; 1247 } 1248 1249 void br_fdb_unsync_static(struct net_bridge *br, struct net_bridge_port *p) 1250 { 1251 struct net_bridge_fdb_entry *f; 1252 1253 ASSERT_RTNL(); 1254 1255 rcu_read_lock(); 1256 hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) { 1257 /* We only care for static entries */ 1258 if (!test_bit(BR_FDB_STATIC, &f->flags)) 1259 continue; 1260 1261 dev_uc_del(p->dev, f->key.addr.addr); 1262 } 1263 rcu_read_unlock(); 1264 } 1265 1266 int br_fdb_external_learn_add(struct net_bridge *br, struct net_bridge_port *p, 1267 const unsigned char *addr, u16 vid, 1268 bool swdev_notify) 1269 { 1270 struct net_bridge_fdb_entry *fdb; 1271 bool modified = false; 1272 int err = 0; 1273 1274 trace_br_fdb_external_learn_add(br, p, addr, vid); 1275 1276 spin_lock_bh(&br->hash_lock); 1277 1278 fdb = br_fdb_find(br, addr, vid); 1279 if (!fdb) { 1280 unsigned long flags = BIT(BR_FDB_ADDED_BY_EXT_LEARN); 1281 1282 if (swdev_notify) 1283 flags |= BIT(BR_FDB_ADDED_BY_USER); 1284 fdb = fdb_create(br, p, addr, vid, flags); 1285 if (!fdb) { 1286 err = -ENOMEM; 1287 goto err_unlock; 1288 } 1289 fdb_notify(br, fdb, RTM_NEWNEIGH, swdev_notify); 1290 } else { 1291 fdb->updated = jiffies; 1292 1293 if (READ_ONCE(fdb->dst) != p) { 1294 WRITE_ONCE(fdb->dst, p); 1295 modified = true; 1296 } 1297 1298 if (test_bit(BR_FDB_ADDED_BY_EXT_LEARN, &fdb->flags)) { 1299 /* Refresh entry */ 1300 fdb->used = jiffies; 1301 } else if (!test_bit(BR_FDB_ADDED_BY_USER, &fdb->flags)) { 1302 /* Take over SW learned entry */ 1303 set_bit(BR_FDB_ADDED_BY_EXT_LEARN, &fdb->flags); 1304 modified = true; 1305 } 1306 1307 if (swdev_notify) 1308 set_bit(BR_FDB_ADDED_BY_USER, &fdb->flags); 1309 1310 if (modified) 1311 fdb_notify(br, fdb, RTM_NEWNEIGH, swdev_notify); 1312 } 1313 1314 err_unlock: 1315 spin_unlock_bh(&br->hash_lock); 1316 1317 return err; 1318 } 1319 1320 int br_fdb_external_learn_del(struct net_bridge *br, struct net_bridge_port *p, 1321 const unsigned char *addr, u16 vid, 1322 bool swdev_notify) 1323 { 1324 struct net_bridge_fdb_entry *fdb; 1325 int err = 0; 1326 1327 spin_lock_bh(&br->hash_lock); 1328 1329 fdb = br_fdb_find(br, addr, vid); 1330 if (fdb && test_bit(BR_FDB_ADDED_BY_EXT_LEARN, &fdb->flags)) 1331 fdb_delete(br, fdb, swdev_notify); 1332 else 1333 err = -ENOENT; 1334 1335 spin_unlock_bh(&br->hash_lock); 1336 1337 return err; 1338 } 1339 1340 void br_fdb_offloaded_set(struct net_bridge *br, struct net_bridge_port *p, 1341 const unsigned char *addr, u16 vid, bool offloaded) 1342 { 1343 struct net_bridge_fdb_entry *fdb; 1344 1345 spin_lock_bh(&br->hash_lock); 1346 1347 fdb = br_fdb_find(br, addr, vid); 1348 if (fdb && offloaded != test_bit(BR_FDB_OFFLOADED, &fdb->flags)) 1349 change_bit(BR_FDB_OFFLOADED, &fdb->flags); 1350 1351 spin_unlock_bh(&br->hash_lock); 1352 } 1353 1354 void br_fdb_clear_offload(const struct net_device *dev, u16 vid) 1355 { 1356 struct net_bridge_fdb_entry *f; 1357 struct net_bridge_port *p; 1358 1359 ASSERT_RTNL(); 1360 1361 p = br_port_get_rtnl(dev); 1362 if (!p) 1363 return; 1364 1365 spin_lock_bh(&p->br->hash_lock); 1366 hlist_for_each_entry(f, &p->br->fdb_list, fdb_node) { 1367 if (f->dst == p && f->key.vlan_id == vid) 1368 clear_bit(BR_FDB_OFFLOADED, &f->flags); 1369 } 1370 spin_unlock_bh(&p->br->hash_lock); 1371 } 1372 EXPORT_SYMBOL_GPL(br_fdb_clear_offload); 1373