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