1 /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 /* 3 * Linux ethernet bridge 4 * 5 * Authors: 6 * Lennert Buytenhek <buytenh@gnu.org> 7 */ 8 9 #ifndef _BR_PRIVATE_H 10 #define _BR_PRIVATE_H 11 12 #include <linux/netdevice.h> 13 #include <linux/if_bridge.h> 14 #include <linux/netpoll.h> 15 #include <linux/u64_stats_sync.h> 16 #include <net/route.h> 17 #include <net/ip6_fib.h> 18 #include <linux/if_vlan.h> 19 #include <linux/rhashtable.h> 20 #include <linux/refcount.h> 21 22 #define BR_HASH_BITS 8 23 #define BR_HASH_SIZE (1 << BR_HASH_BITS) 24 25 #define BR_HOLD_TIME (1*HZ) 26 27 #define BR_PORT_BITS 10 28 #define BR_MAX_PORTS (1<<BR_PORT_BITS) 29 30 #define BR_MULTICAST_DEFAULT_HASH_MAX 4096 31 32 #define BR_VERSION "2.3" 33 34 /* Control of forwarding link local multicast */ 35 #define BR_GROUPFWD_DEFAULT 0 36 /* Don't allow forwarding of control protocols like STP, MAC PAUSE and LACP */ 37 enum { 38 BR_GROUPFWD_STP = BIT(0), 39 BR_GROUPFWD_MACPAUSE = BIT(1), 40 BR_GROUPFWD_LACP = BIT(2), 41 }; 42 43 #define BR_GROUPFWD_RESTRICTED (BR_GROUPFWD_STP | BR_GROUPFWD_MACPAUSE | \ 44 BR_GROUPFWD_LACP) 45 /* The Nearest Customer Bridge Group Address, 01-80-C2-00-00-[00,0B,0C,0D,0F] */ 46 #define BR_GROUPFWD_8021AD 0xB801u 47 48 /* Path to usermode spanning tree program */ 49 #define BR_STP_PROG "/sbin/bridge-stp" 50 51 #define BR_FDB_NOTIFY_SETTABLE_BITS (FDB_NOTIFY_BIT | FDB_NOTIFY_INACTIVE_BIT) 52 53 typedef struct bridge_id bridge_id; 54 typedef struct mac_addr mac_addr; 55 typedef __u16 port_id; 56 57 struct bridge_id { 58 unsigned char prio[2]; 59 unsigned char addr[ETH_ALEN]; 60 }; 61 62 struct mac_addr { 63 unsigned char addr[ETH_ALEN]; 64 }; 65 66 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING 67 /* our own querier */ 68 struct bridge_mcast_own_query { 69 struct timer_list timer; 70 u32 startup_sent; 71 }; 72 73 /* other querier */ 74 struct bridge_mcast_other_query { 75 struct timer_list timer; 76 unsigned long delay_time; 77 }; 78 79 /* selected querier */ 80 struct bridge_mcast_querier { 81 struct br_ip addr; 82 struct net_bridge_port __rcu *port; 83 }; 84 85 /* IGMP/MLD statistics */ 86 struct bridge_mcast_stats { 87 struct br_mcast_stats mstats; 88 struct u64_stats_sync syncp; 89 }; 90 #endif 91 92 struct br_tunnel_info { 93 __be64 tunnel_id; 94 struct metadata_dst *tunnel_dst; 95 }; 96 97 /* private vlan flags */ 98 enum { 99 BR_VLFLAG_PER_PORT_STATS = BIT(0), 100 BR_VLFLAG_ADDED_BY_SWITCHDEV = BIT(1), 101 }; 102 103 /** 104 * struct net_bridge_vlan - per-vlan entry 105 * 106 * @vnode: rhashtable member 107 * @vid: VLAN id 108 * @flags: bridge vlan flags 109 * @priv_flags: private (in-kernel) bridge vlan flags 110 * @state: STP state (e.g. blocking, learning, forwarding) 111 * @stats: per-cpu VLAN statistics 112 * @br: if MASTER flag set, this points to a bridge struct 113 * @port: if MASTER flag unset, this points to a port struct 114 * @refcnt: if MASTER flag set, this is bumped for each port referencing it 115 * @brvlan: if MASTER flag unset, this points to the global per-VLAN context 116 * for this VLAN entry 117 * @vlist: sorted list of VLAN entries 118 * @rcu: used for entry destruction 119 * 120 * This structure is shared between the global per-VLAN entries contained in 121 * the bridge rhashtable and the local per-port per-VLAN entries contained in 122 * the port's rhashtable. The union entries should be interpreted depending on 123 * the entry flags that are set. 124 */ 125 struct net_bridge_vlan { 126 struct rhash_head vnode; 127 struct rhash_head tnode; 128 u16 vid; 129 u16 flags; 130 u16 priv_flags; 131 u8 state; 132 struct pcpu_sw_netstats __percpu *stats; 133 union { 134 struct net_bridge *br; 135 struct net_bridge_port *port; 136 }; 137 union { 138 refcount_t refcnt; 139 struct net_bridge_vlan *brvlan; 140 }; 141 142 struct br_tunnel_info tinfo; 143 144 struct list_head vlist; 145 146 struct rcu_head rcu; 147 }; 148 149 /** 150 * struct net_bridge_vlan_group 151 * 152 * @vlan_hash: VLAN entry rhashtable 153 * @vlan_list: sorted VLAN entry list 154 * @num_vlans: number of total VLAN entries 155 * @pvid: PVID VLAN id 156 * @pvid_state: PVID's STP state (e.g. forwarding, learning, blocking) 157 * 158 * IMPORTANT: Be careful when checking if there're VLAN entries using list 159 * primitives because the bridge can have entries in its list which 160 * are just for global context but not for filtering, i.e. they have 161 * the master flag set but not the brentry flag. If you have to check 162 * if there're "real" entries in the bridge please test @num_vlans 163 */ 164 struct net_bridge_vlan_group { 165 struct rhashtable vlan_hash; 166 struct rhashtable tunnel_hash; 167 struct list_head vlan_list; 168 u16 num_vlans; 169 u16 pvid; 170 u8 pvid_state; 171 }; 172 173 /* bridge fdb flags */ 174 enum { 175 BR_FDB_LOCAL, 176 BR_FDB_STATIC, 177 BR_FDB_STICKY, 178 BR_FDB_ADDED_BY_USER, 179 BR_FDB_ADDED_BY_EXT_LEARN, 180 BR_FDB_OFFLOADED, 181 BR_FDB_NOTIFY, 182 BR_FDB_NOTIFY_INACTIVE 183 }; 184 185 struct net_bridge_fdb_key { 186 mac_addr addr; 187 u16 vlan_id; 188 }; 189 190 struct net_bridge_fdb_entry { 191 struct rhash_head rhnode; 192 struct net_bridge_port *dst; 193 194 struct net_bridge_fdb_key key; 195 struct hlist_node fdb_node; 196 unsigned long flags; 197 198 /* write-heavy members should not affect lookups */ 199 unsigned long updated ____cacheline_aligned_in_smp; 200 unsigned long used; 201 202 struct rcu_head rcu; 203 }; 204 205 #define MDB_PG_FLAGS_PERMANENT BIT(0) 206 #define MDB_PG_FLAGS_OFFLOAD BIT(1) 207 #define MDB_PG_FLAGS_FAST_LEAVE BIT(2) 208 #define MDB_PG_FLAGS_STAR_EXCL BIT(3) 209 #define MDB_PG_FLAGS_BLOCKED BIT(4) 210 211 #define PG_SRC_ENT_LIMIT 32 212 213 #define BR_SGRP_F_DELETE BIT(0) 214 #define BR_SGRP_F_SEND BIT(1) 215 #define BR_SGRP_F_INSTALLED BIT(2) 216 217 struct net_bridge_mcast_gc { 218 struct hlist_node gc_node; 219 void (*destroy)(struct net_bridge_mcast_gc *gc); 220 }; 221 222 struct net_bridge_group_src { 223 struct hlist_node node; 224 225 struct br_ip addr; 226 struct net_bridge_port_group *pg; 227 u8 flags; 228 u8 src_query_rexmit_cnt; 229 struct timer_list timer; 230 231 struct net_bridge *br; 232 struct net_bridge_mcast_gc mcast_gc; 233 struct rcu_head rcu; 234 }; 235 236 struct net_bridge_port_group_sg_key { 237 struct net_bridge_port *port; 238 struct br_ip addr; 239 }; 240 241 struct net_bridge_port_group { 242 struct net_bridge_port_group __rcu *next; 243 struct net_bridge_port_group_sg_key key; 244 unsigned char eth_addr[ETH_ALEN] __aligned(2); 245 unsigned char flags; 246 unsigned char filter_mode; 247 unsigned char grp_query_rexmit_cnt; 248 unsigned char rt_protocol; 249 250 struct hlist_head src_list; 251 unsigned int src_ents; 252 struct timer_list timer; 253 struct timer_list rexmit_timer; 254 struct hlist_node mglist; 255 256 struct rhash_head rhnode; 257 struct net_bridge_mcast_gc mcast_gc; 258 struct rcu_head rcu; 259 }; 260 261 struct net_bridge_mdb_entry { 262 struct rhash_head rhnode; 263 struct net_bridge *br; 264 struct net_bridge_port_group __rcu *ports; 265 struct br_ip addr; 266 bool host_joined; 267 268 struct timer_list timer; 269 struct hlist_node mdb_node; 270 271 struct net_bridge_mcast_gc mcast_gc; 272 struct rcu_head rcu; 273 }; 274 275 struct net_bridge_port { 276 struct net_bridge *br; 277 struct net_device *dev; 278 struct list_head list; 279 280 unsigned long flags; 281 #ifdef CONFIG_BRIDGE_VLAN_FILTERING 282 struct net_bridge_vlan_group __rcu *vlgrp; 283 #endif 284 struct net_bridge_port __rcu *backup_port; 285 286 /* STP */ 287 u8 priority; 288 u8 state; 289 u16 port_no; 290 unsigned char topology_change_ack; 291 unsigned char config_pending; 292 port_id port_id; 293 port_id designated_port; 294 bridge_id designated_root; 295 bridge_id designated_bridge; 296 u32 path_cost; 297 u32 designated_cost; 298 unsigned long designated_age; 299 300 struct timer_list forward_delay_timer; 301 struct timer_list hold_timer; 302 struct timer_list message_age_timer; 303 struct kobject kobj; 304 struct rcu_head rcu; 305 306 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING 307 struct bridge_mcast_own_query ip4_own_query; 308 #if IS_ENABLED(CONFIG_IPV6) 309 struct bridge_mcast_own_query ip6_own_query; 310 #endif /* IS_ENABLED(CONFIG_IPV6) */ 311 unsigned char multicast_router; 312 struct bridge_mcast_stats __percpu *mcast_stats; 313 struct timer_list multicast_router_timer; 314 struct hlist_head mglist; 315 struct hlist_node rlist; 316 #endif 317 318 #ifdef CONFIG_SYSFS 319 char sysfs_name[IFNAMSIZ]; 320 #endif 321 322 #ifdef CONFIG_NET_POLL_CONTROLLER 323 struct netpoll *np; 324 #endif 325 #ifdef CONFIG_NET_SWITCHDEV 326 int offload_fwd_mark; 327 #endif 328 u16 group_fwd_mask; 329 u16 backup_redirected_cnt; 330 331 struct bridge_stp_xstats stp_xstats; 332 }; 333 334 #define kobj_to_brport(obj) container_of(obj, struct net_bridge_port, kobj) 335 336 #define br_auto_port(p) ((p)->flags & BR_AUTO_MASK) 337 #define br_promisc_port(p) ((p)->flags & BR_PROMISC) 338 339 static inline struct net_bridge_port *br_port_get_rcu(const struct net_device *dev) 340 { 341 return rcu_dereference(dev->rx_handler_data); 342 } 343 344 static inline struct net_bridge_port *br_port_get_rtnl(const struct net_device *dev) 345 { 346 return netif_is_bridge_port(dev) ? 347 rtnl_dereference(dev->rx_handler_data) : NULL; 348 } 349 350 static inline struct net_bridge_port *br_port_get_rtnl_rcu(const struct net_device *dev) 351 { 352 return netif_is_bridge_port(dev) ? 353 rcu_dereference_rtnl(dev->rx_handler_data) : NULL; 354 } 355 356 enum net_bridge_opts { 357 BROPT_VLAN_ENABLED, 358 BROPT_VLAN_STATS_ENABLED, 359 BROPT_NF_CALL_IPTABLES, 360 BROPT_NF_CALL_IP6TABLES, 361 BROPT_NF_CALL_ARPTABLES, 362 BROPT_GROUP_ADDR_SET, 363 BROPT_MULTICAST_ENABLED, 364 BROPT_MULTICAST_QUERIER, 365 BROPT_MULTICAST_QUERY_USE_IFADDR, 366 BROPT_MULTICAST_STATS_ENABLED, 367 BROPT_HAS_IPV6_ADDR, 368 BROPT_NEIGH_SUPPRESS_ENABLED, 369 BROPT_MTU_SET_BY_USER, 370 BROPT_VLAN_STATS_PER_PORT, 371 BROPT_NO_LL_LEARN, 372 BROPT_VLAN_BRIDGE_BINDING, 373 }; 374 375 struct net_bridge { 376 spinlock_t lock; 377 spinlock_t hash_lock; 378 struct hlist_head frame_type_list; 379 struct net_device *dev; 380 unsigned long options; 381 /* These fields are accessed on each packet */ 382 #ifdef CONFIG_BRIDGE_VLAN_FILTERING 383 __be16 vlan_proto; 384 u16 default_pvid; 385 struct net_bridge_vlan_group __rcu *vlgrp; 386 #endif 387 388 struct rhashtable fdb_hash_tbl; 389 struct list_head port_list; 390 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 391 union { 392 struct rtable fake_rtable; 393 struct rt6_info fake_rt6_info; 394 }; 395 #endif 396 u16 group_fwd_mask; 397 u16 group_fwd_mask_required; 398 399 /* STP */ 400 bridge_id designated_root; 401 bridge_id bridge_id; 402 unsigned char topology_change; 403 unsigned char topology_change_detected; 404 u16 root_port; 405 unsigned long max_age; 406 unsigned long hello_time; 407 unsigned long forward_delay; 408 unsigned long ageing_time; 409 unsigned long bridge_max_age; 410 unsigned long bridge_hello_time; 411 unsigned long bridge_forward_delay; 412 unsigned long bridge_ageing_time; 413 u32 root_path_cost; 414 415 u8 group_addr[ETH_ALEN]; 416 417 enum { 418 BR_NO_STP, /* no spanning tree */ 419 BR_KERNEL_STP, /* old STP in kernel */ 420 BR_USER_STP, /* new RSTP in userspace */ 421 } stp_enabled; 422 423 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING 424 425 u32 hash_max; 426 427 u32 multicast_last_member_count; 428 u32 multicast_startup_query_count; 429 430 u8 multicast_igmp_version; 431 u8 multicast_router; 432 #if IS_ENABLED(CONFIG_IPV6) 433 u8 multicast_mld_version; 434 #endif 435 spinlock_t multicast_lock; 436 unsigned long multicast_last_member_interval; 437 unsigned long multicast_membership_interval; 438 unsigned long multicast_querier_interval; 439 unsigned long multicast_query_interval; 440 unsigned long multicast_query_response_interval; 441 unsigned long multicast_startup_query_interval; 442 443 struct rhashtable mdb_hash_tbl; 444 struct rhashtable sg_port_tbl; 445 446 struct hlist_head mcast_gc_list; 447 struct hlist_head mdb_list; 448 struct hlist_head router_list; 449 450 struct timer_list multicast_router_timer; 451 struct bridge_mcast_other_query ip4_other_query; 452 struct bridge_mcast_own_query ip4_own_query; 453 struct bridge_mcast_querier ip4_querier; 454 struct bridge_mcast_stats __percpu *mcast_stats; 455 #if IS_ENABLED(CONFIG_IPV6) 456 struct bridge_mcast_other_query ip6_other_query; 457 struct bridge_mcast_own_query ip6_own_query; 458 struct bridge_mcast_querier ip6_querier; 459 #endif /* IS_ENABLED(CONFIG_IPV6) */ 460 struct work_struct mcast_gc_work; 461 #endif 462 463 struct timer_list hello_timer; 464 struct timer_list tcn_timer; 465 struct timer_list topology_change_timer; 466 struct delayed_work gc_work; 467 struct kobject *ifobj; 468 u32 auto_cnt; 469 470 #ifdef CONFIG_NET_SWITCHDEV 471 int offload_fwd_mark; 472 #endif 473 struct hlist_head fdb_list; 474 475 #if IS_ENABLED(CONFIG_BRIDGE_MRP) 476 struct hlist_head mrp_list; 477 #endif 478 #if IS_ENABLED(CONFIG_BRIDGE_CFM) 479 struct hlist_head mep_list; 480 #endif 481 }; 482 483 struct br_input_skb_cb { 484 struct net_device *brdev; 485 486 u16 frag_max_size; 487 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING 488 u8 igmp; 489 u8 mrouters_only:1; 490 #endif 491 u8 proxyarp_replied:1; 492 u8 src_port_isolated:1; 493 #ifdef CONFIG_BRIDGE_VLAN_FILTERING 494 u8 vlan_filtered:1; 495 #endif 496 #ifdef CONFIG_NETFILTER_FAMILY_BRIDGE 497 u8 br_netfilter_broute:1; 498 #endif 499 500 #ifdef CONFIG_NET_SWITCHDEV 501 int offload_fwd_mark; 502 #endif 503 }; 504 505 #define BR_INPUT_SKB_CB(__skb) ((struct br_input_skb_cb *)(__skb)->cb) 506 507 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING 508 # define BR_INPUT_SKB_CB_MROUTERS_ONLY(__skb) (BR_INPUT_SKB_CB(__skb)->mrouters_only) 509 #else 510 # define BR_INPUT_SKB_CB_MROUTERS_ONLY(__skb) (0) 511 #endif 512 513 #define br_printk(level, br, format, args...) \ 514 printk(level "%s: " format, (br)->dev->name, ##args) 515 516 #define br_err(__br, format, args...) \ 517 br_printk(KERN_ERR, __br, format, ##args) 518 #define br_warn(__br, format, args...) \ 519 br_printk(KERN_WARNING, __br, format, ##args) 520 #define br_notice(__br, format, args...) \ 521 br_printk(KERN_NOTICE, __br, format, ##args) 522 #define br_info(__br, format, args...) \ 523 br_printk(KERN_INFO, __br, format, ##args) 524 525 #define br_debug(br, format, args...) \ 526 pr_debug("%s: " format, (br)->dev->name, ##args) 527 528 /* called under bridge lock */ 529 static inline int br_is_root_bridge(const struct net_bridge *br) 530 { 531 return !memcmp(&br->bridge_id, &br->designated_root, 8); 532 } 533 534 /* check if a VLAN entry is global */ 535 static inline bool br_vlan_is_master(const struct net_bridge_vlan *v) 536 { 537 return v->flags & BRIDGE_VLAN_INFO_MASTER; 538 } 539 540 /* check if a VLAN entry is used by the bridge */ 541 static inline bool br_vlan_is_brentry(const struct net_bridge_vlan *v) 542 { 543 return v->flags & BRIDGE_VLAN_INFO_BRENTRY; 544 } 545 546 /* check if we should use the vlan entry, returns false if it's only context */ 547 static inline bool br_vlan_should_use(const struct net_bridge_vlan *v) 548 { 549 if (br_vlan_is_master(v)) { 550 if (br_vlan_is_brentry(v)) 551 return true; 552 else 553 return false; 554 } 555 556 return true; 557 } 558 559 static inline bool nbp_state_should_learn(const struct net_bridge_port *p) 560 { 561 return p->state == BR_STATE_LEARNING || p->state == BR_STATE_FORWARDING; 562 } 563 564 static inline bool br_vlan_valid_id(u16 vid, struct netlink_ext_ack *extack) 565 { 566 bool ret = vid > 0 && vid < VLAN_VID_MASK; 567 568 if (!ret) 569 NL_SET_ERR_MSG_MOD(extack, "Vlan id is invalid"); 570 571 return ret; 572 } 573 574 static inline bool br_vlan_valid_range(const struct bridge_vlan_info *cur, 575 const struct bridge_vlan_info *last, 576 struct netlink_ext_ack *extack) 577 { 578 /* pvid flag is not allowed in ranges */ 579 if (cur->flags & BRIDGE_VLAN_INFO_PVID) { 580 NL_SET_ERR_MSG_MOD(extack, "Pvid isn't allowed in a range"); 581 return false; 582 } 583 584 /* when cur is the range end, check if: 585 * - it has range start flag 586 * - range ids are invalid (end is equal to or before start) 587 */ 588 if (last) { 589 if (cur->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) { 590 NL_SET_ERR_MSG_MOD(extack, "Found a new vlan range start while processing one"); 591 return false; 592 } else if (!(cur->flags & BRIDGE_VLAN_INFO_RANGE_END)) { 593 NL_SET_ERR_MSG_MOD(extack, "Vlan range end flag is missing"); 594 return false; 595 } else if (cur->vid <= last->vid) { 596 NL_SET_ERR_MSG_MOD(extack, "End vlan id is less than or equal to start vlan id"); 597 return false; 598 } 599 } 600 601 /* check for required range flags */ 602 if (!(cur->flags & (BRIDGE_VLAN_INFO_RANGE_BEGIN | 603 BRIDGE_VLAN_INFO_RANGE_END))) { 604 NL_SET_ERR_MSG_MOD(extack, "Both vlan range flags are missing"); 605 return false; 606 } 607 608 return true; 609 } 610 611 static inline int br_afspec_cmd_to_rtm(int cmd) 612 { 613 switch (cmd) { 614 case RTM_SETLINK: 615 return RTM_NEWVLAN; 616 case RTM_DELLINK: 617 return RTM_DELVLAN; 618 } 619 620 return 0; 621 } 622 623 static inline int br_opt_get(const struct net_bridge *br, 624 enum net_bridge_opts opt) 625 { 626 return test_bit(opt, &br->options); 627 } 628 629 int br_boolopt_toggle(struct net_bridge *br, enum br_boolopt_id opt, bool on, 630 struct netlink_ext_ack *extack); 631 int br_boolopt_get(const struct net_bridge *br, enum br_boolopt_id opt); 632 int br_boolopt_multi_toggle(struct net_bridge *br, 633 struct br_boolopt_multi *bm, 634 struct netlink_ext_ack *extack); 635 void br_boolopt_multi_get(const struct net_bridge *br, 636 struct br_boolopt_multi *bm); 637 void br_opt_toggle(struct net_bridge *br, enum net_bridge_opts opt, bool on); 638 639 /* br_device.c */ 640 void br_dev_setup(struct net_device *dev); 641 void br_dev_delete(struct net_device *dev, struct list_head *list); 642 netdev_tx_t br_dev_xmit(struct sk_buff *skb, struct net_device *dev); 643 #ifdef CONFIG_NET_POLL_CONTROLLER 644 static inline void br_netpoll_send_skb(const struct net_bridge_port *p, 645 struct sk_buff *skb) 646 { 647 netpoll_send_skb(p->np, skb); 648 } 649 650 int br_netpoll_enable(struct net_bridge_port *p); 651 void br_netpoll_disable(struct net_bridge_port *p); 652 #else 653 static inline void br_netpoll_send_skb(const struct net_bridge_port *p, 654 struct sk_buff *skb) 655 { 656 } 657 658 static inline int br_netpoll_enable(struct net_bridge_port *p) 659 { 660 return 0; 661 } 662 663 static inline void br_netpoll_disable(struct net_bridge_port *p) 664 { 665 } 666 #endif 667 668 /* br_fdb.c */ 669 int br_fdb_init(void); 670 void br_fdb_fini(void); 671 int br_fdb_hash_init(struct net_bridge *br); 672 void br_fdb_hash_fini(struct net_bridge *br); 673 void br_fdb_flush(struct net_bridge *br); 674 void br_fdb_find_delete_local(struct net_bridge *br, 675 const struct net_bridge_port *p, 676 const unsigned char *addr, u16 vid); 677 void br_fdb_changeaddr(struct net_bridge_port *p, const unsigned char *newaddr); 678 void br_fdb_change_mac_address(struct net_bridge *br, const u8 *newaddr); 679 void br_fdb_cleanup(struct work_struct *work); 680 void br_fdb_delete_by_port(struct net_bridge *br, 681 const struct net_bridge_port *p, u16 vid, int do_all); 682 struct net_bridge_fdb_entry *br_fdb_find_rcu(struct net_bridge *br, 683 const unsigned char *addr, 684 __u16 vid); 685 int br_fdb_test_addr(struct net_device *dev, unsigned char *addr); 686 int br_fdb_fillbuf(struct net_bridge *br, void *buf, unsigned long count, 687 unsigned long off); 688 int br_fdb_insert(struct net_bridge *br, struct net_bridge_port *source, 689 const unsigned char *addr, u16 vid); 690 void br_fdb_update(struct net_bridge *br, struct net_bridge_port *source, 691 const unsigned char *addr, u16 vid, unsigned long flags); 692 693 int br_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[], 694 struct net_device *dev, const unsigned char *addr, u16 vid); 695 int br_fdb_add(struct ndmsg *nlh, struct nlattr *tb[], struct net_device *dev, 696 const unsigned char *addr, u16 vid, u16 nlh_flags, 697 struct netlink_ext_ack *extack); 698 int br_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb, 699 struct net_device *dev, struct net_device *fdev, int *idx); 700 int br_fdb_get(struct sk_buff *skb, struct nlattr *tb[], struct net_device *dev, 701 const unsigned char *addr, u16 vid, u32 portid, u32 seq, 702 struct netlink_ext_ack *extack); 703 int br_fdb_sync_static(struct net_bridge *br, struct net_bridge_port *p); 704 void br_fdb_unsync_static(struct net_bridge *br, struct net_bridge_port *p); 705 int br_fdb_external_learn_add(struct net_bridge *br, struct net_bridge_port *p, 706 const unsigned char *addr, u16 vid, 707 bool swdev_notify); 708 int br_fdb_external_learn_del(struct net_bridge *br, struct net_bridge_port *p, 709 const unsigned char *addr, u16 vid, 710 bool swdev_notify); 711 void br_fdb_offloaded_set(struct net_bridge *br, struct net_bridge_port *p, 712 const unsigned char *addr, u16 vid, bool offloaded); 713 714 /* br_forward.c */ 715 enum br_pkt_type { 716 BR_PKT_UNICAST, 717 BR_PKT_MULTICAST, 718 BR_PKT_BROADCAST 719 }; 720 int br_dev_queue_push_xmit(struct net *net, struct sock *sk, struct sk_buff *skb); 721 void br_forward(const struct net_bridge_port *to, struct sk_buff *skb, 722 bool local_rcv, bool local_orig); 723 int br_forward_finish(struct net *net, struct sock *sk, struct sk_buff *skb); 724 void br_flood(struct net_bridge *br, struct sk_buff *skb, 725 enum br_pkt_type pkt_type, bool local_rcv, bool local_orig); 726 727 /* return true if both source port and dest port are isolated */ 728 static inline bool br_skb_isolated(const struct net_bridge_port *to, 729 const struct sk_buff *skb) 730 { 731 return BR_INPUT_SKB_CB(skb)->src_port_isolated && 732 (to->flags & BR_ISOLATED); 733 } 734 735 /* br_if.c */ 736 void br_port_carrier_check(struct net_bridge_port *p, bool *notified); 737 int br_add_bridge(struct net *net, const char *name); 738 int br_del_bridge(struct net *net, const char *name); 739 int br_add_if(struct net_bridge *br, struct net_device *dev, 740 struct netlink_ext_ack *extack); 741 int br_del_if(struct net_bridge *br, struct net_device *dev); 742 void br_mtu_auto_adjust(struct net_bridge *br); 743 netdev_features_t br_features_recompute(struct net_bridge *br, 744 netdev_features_t features); 745 void br_port_flags_change(struct net_bridge_port *port, unsigned long mask); 746 void br_manage_promisc(struct net_bridge *br); 747 int nbp_backup_change(struct net_bridge_port *p, struct net_device *backup_dev); 748 749 /* br_input.c */ 750 int br_handle_frame_finish(struct net *net, struct sock *sk, struct sk_buff *skb); 751 rx_handler_func_t *br_get_rx_handler(const struct net_device *dev); 752 753 struct br_frame_type { 754 __be16 type; 755 int (*frame_handler)(struct net_bridge_port *port, 756 struct sk_buff *skb); 757 struct hlist_node list; 758 }; 759 760 void br_add_frame(struct net_bridge *br, struct br_frame_type *ft); 761 void br_del_frame(struct net_bridge *br, struct br_frame_type *ft); 762 763 static inline bool br_rx_handler_check_rcu(const struct net_device *dev) 764 { 765 return rcu_dereference(dev->rx_handler) == br_get_rx_handler(dev); 766 } 767 768 static inline bool br_rx_handler_check_rtnl(const struct net_device *dev) 769 { 770 return rcu_dereference_rtnl(dev->rx_handler) == br_get_rx_handler(dev); 771 } 772 773 static inline struct net_bridge_port *br_port_get_check_rcu(const struct net_device *dev) 774 { 775 return br_rx_handler_check_rcu(dev) ? br_port_get_rcu(dev) : NULL; 776 } 777 778 static inline struct net_bridge_port * 779 br_port_get_check_rtnl(const struct net_device *dev) 780 { 781 return br_rx_handler_check_rtnl(dev) ? br_port_get_rtnl_rcu(dev) : NULL; 782 } 783 784 /* br_ioctl.c */ 785 int br_dev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd); 786 int br_ioctl_deviceless_stub(struct net *net, unsigned int cmd, 787 void __user *arg); 788 789 /* br_multicast.c */ 790 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING 791 int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port, 792 struct sk_buff *skb, u16 vid); 793 struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br, 794 struct sk_buff *skb, u16 vid); 795 int br_multicast_add_port(struct net_bridge_port *port); 796 void br_multicast_del_port(struct net_bridge_port *port); 797 void br_multicast_enable_port(struct net_bridge_port *port); 798 void br_multicast_disable_port(struct net_bridge_port *port); 799 void br_multicast_init(struct net_bridge *br); 800 void br_multicast_join_snoopers(struct net_bridge *br); 801 void br_multicast_leave_snoopers(struct net_bridge *br); 802 void br_multicast_open(struct net_bridge *br); 803 void br_multicast_stop(struct net_bridge *br); 804 void br_multicast_dev_del(struct net_bridge *br); 805 void br_multicast_flood(struct net_bridge_mdb_entry *mdst, 806 struct sk_buff *skb, bool local_rcv, bool local_orig); 807 int br_multicast_set_router(struct net_bridge *br, unsigned long val); 808 int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val); 809 int br_multicast_toggle(struct net_bridge *br, unsigned long val); 810 int br_multicast_set_querier(struct net_bridge *br, unsigned long val); 811 int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val); 812 int br_multicast_set_igmp_version(struct net_bridge *br, unsigned long val); 813 #if IS_ENABLED(CONFIG_IPV6) 814 int br_multicast_set_mld_version(struct net_bridge *br, unsigned long val); 815 #endif 816 struct net_bridge_mdb_entry * 817 br_mdb_ip_get(struct net_bridge *br, struct br_ip *dst); 818 struct net_bridge_mdb_entry * 819 br_multicast_new_group(struct net_bridge *br, struct br_ip *group); 820 struct net_bridge_port_group * 821 br_multicast_new_port_group(struct net_bridge_port *port, struct br_ip *group, 822 struct net_bridge_port_group __rcu *next, 823 unsigned char flags, const unsigned char *src, 824 u8 filter_mode, u8 rt_protocol); 825 int br_mdb_hash_init(struct net_bridge *br); 826 void br_mdb_hash_fini(struct net_bridge *br); 827 void br_mdb_notify(struct net_device *dev, struct net_bridge_mdb_entry *mp, 828 struct net_bridge_port_group *pg, int type); 829 void br_rtr_notify(struct net_device *dev, struct net_bridge_port *port, 830 int type); 831 void br_multicast_del_pg(struct net_bridge_mdb_entry *mp, 832 struct net_bridge_port_group *pg, 833 struct net_bridge_port_group __rcu **pp); 834 void br_multicast_count(struct net_bridge *br, const struct net_bridge_port *p, 835 const struct sk_buff *skb, u8 type, u8 dir); 836 int br_multicast_init_stats(struct net_bridge *br); 837 void br_multicast_uninit_stats(struct net_bridge *br); 838 void br_multicast_get_stats(const struct net_bridge *br, 839 const struct net_bridge_port *p, 840 struct br_mcast_stats *dest); 841 void br_mdb_init(void); 842 void br_mdb_uninit(void); 843 void br_multicast_host_join(struct net_bridge_mdb_entry *mp, bool notify); 844 void br_multicast_host_leave(struct net_bridge_mdb_entry *mp, bool notify); 845 void br_multicast_star_g_handle_mode(struct net_bridge_port_group *pg, 846 u8 filter_mode); 847 void br_multicast_sg_add_exclude_ports(struct net_bridge_mdb_entry *star_mp, 848 struct net_bridge_port_group *sg); 849 850 static inline bool br_group_is_l2(const struct br_ip *group) 851 { 852 return group->proto == 0; 853 } 854 855 #define mlock_dereference(X, br) \ 856 rcu_dereference_protected(X, lockdep_is_held(&br->multicast_lock)) 857 858 static inline bool br_multicast_is_router(struct net_bridge *br) 859 { 860 return br->multicast_router == 2 || 861 (br->multicast_router == 1 && 862 timer_pending(&br->multicast_router_timer)); 863 } 864 865 static inline bool 866 __br_multicast_querier_exists(struct net_bridge *br, 867 struct bridge_mcast_other_query *querier, 868 const bool is_ipv6) 869 { 870 bool own_querier_enabled; 871 872 if (br_opt_get(br, BROPT_MULTICAST_QUERIER)) { 873 if (is_ipv6 && !br_opt_get(br, BROPT_HAS_IPV6_ADDR)) 874 own_querier_enabled = false; 875 else 876 own_querier_enabled = true; 877 } else { 878 own_querier_enabled = false; 879 } 880 881 return time_is_before_jiffies(querier->delay_time) && 882 (own_querier_enabled || timer_pending(&querier->timer)); 883 } 884 885 static inline bool br_multicast_querier_exists(struct net_bridge *br, 886 struct ethhdr *eth, 887 const struct net_bridge_mdb_entry *mdb) 888 { 889 switch (eth->h_proto) { 890 case (htons(ETH_P_IP)): 891 return __br_multicast_querier_exists(br, 892 &br->ip4_other_query, false); 893 #if IS_ENABLED(CONFIG_IPV6) 894 case (htons(ETH_P_IPV6)): 895 return __br_multicast_querier_exists(br, 896 &br->ip6_other_query, true); 897 #endif 898 default: 899 return !!mdb && br_group_is_l2(&mdb->addr); 900 } 901 } 902 903 static inline bool br_multicast_is_star_g(const struct br_ip *ip) 904 { 905 switch (ip->proto) { 906 case htons(ETH_P_IP): 907 return ipv4_is_zeronet(ip->src.ip4); 908 #if IS_ENABLED(CONFIG_IPV6) 909 case htons(ETH_P_IPV6): 910 return ipv6_addr_any(&ip->src.ip6); 911 #endif 912 default: 913 return false; 914 } 915 } 916 917 static inline bool br_multicast_should_handle_mode(const struct net_bridge *br, 918 __be16 proto) 919 { 920 switch (proto) { 921 case htons(ETH_P_IP): 922 return !!(br->multicast_igmp_version == 3); 923 #if IS_ENABLED(CONFIG_IPV6) 924 case htons(ETH_P_IPV6): 925 return !!(br->multicast_mld_version == 2); 926 #endif 927 default: 928 return false; 929 } 930 } 931 932 static inline int br_multicast_igmp_type(const struct sk_buff *skb) 933 { 934 return BR_INPUT_SKB_CB(skb)->igmp; 935 } 936 937 static inline unsigned long br_multicast_lmqt(const struct net_bridge *br) 938 { 939 return br->multicast_last_member_interval * 940 br->multicast_last_member_count; 941 } 942 943 static inline unsigned long br_multicast_gmi(const struct net_bridge *br) 944 { 945 /* use the RFC default of 2 for QRV */ 946 return 2 * br->multicast_query_interval + 947 br->multicast_query_response_interval; 948 } 949 #else 950 static inline int br_multicast_rcv(struct net_bridge *br, 951 struct net_bridge_port *port, 952 struct sk_buff *skb, 953 u16 vid) 954 { 955 return 0; 956 } 957 958 static inline struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br, 959 struct sk_buff *skb, u16 vid) 960 { 961 return NULL; 962 } 963 964 static inline int br_multicast_add_port(struct net_bridge_port *port) 965 { 966 return 0; 967 } 968 969 static inline void br_multicast_del_port(struct net_bridge_port *port) 970 { 971 } 972 973 static inline void br_multicast_enable_port(struct net_bridge_port *port) 974 { 975 } 976 977 static inline void br_multicast_disable_port(struct net_bridge_port *port) 978 { 979 } 980 981 static inline void br_multicast_init(struct net_bridge *br) 982 { 983 } 984 985 static inline void br_multicast_join_snoopers(struct net_bridge *br) 986 { 987 } 988 989 static inline void br_multicast_leave_snoopers(struct net_bridge *br) 990 { 991 } 992 993 static inline void br_multicast_open(struct net_bridge *br) 994 { 995 } 996 997 static inline void br_multicast_stop(struct net_bridge *br) 998 { 999 } 1000 1001 static inline void br_multicast_dev_del(struct net_bridge *br) 1002 { 1003 } 1004 1005 static inline void br_multicast_flood(struct net_bridge_mdb_entry *mdst, 1006 struct sk_buff *skb, 1007 bool local_rcv, bool local_orig) 1008 { 1009 } 1010 1011 static inline bool br_multicast_is_router(struct net_bridge *br) 1012 { 1013 return false; 1014 } 1015 1016 static inline bool br_multicast_querier_exists(struct net_bridge *br, 1017 struct ethhdr *eth, 1018 const struct net_bridge_mdb_entry *mdb) 1019 { 1020 return false; 1021 } 1022 1023 static inline void br_mdb_init(void) 1024 { 1025 } 1026 1027 static inline void br_mdb_uninit(void) 1028 { 1029 } 1030 1031 static inline int br_mdb_hash_init(struct net_bridge *br) 1032 { 1033 return 0; 1034 } 1035 1036 static inline void br_mdb_hash_fini(struct net_bridge *br) 1037 { 1038 } 1039 1040 static inline void br_multicast_count(struct net_bridge *br, 1041 const struct net_bridge_port *p, 1042 const struct sk_buff *skb, 1043 u8 type, u8 dir) 1044 { 1045 } 1046 1047 static inline int br_multicast_init_stats(struct net_bridge *br) 1048 { 1049 return 0; 1050 } 1051 1052 static inline void br_multicast_uninit_stats(struct net_bridge *br) 1053 { 1054 } 1055 1056 static inline int br_multicast_igmp_type(const struct sk_buff *skb) 1057 { 1058 return 0; 1059 } 1060 #endif 1061 1062 /* br_vlan.c */ 1063 #ifdef CONFIG_BRIDGE_VLAN_FILTERING 1064 bool br_allowed_ingress(const struct net_bridge *br, 1065 struct net_bridge_vlan_group *vg, struct sk_buff *skb, 1066 u16 *vid, u8 *state); 1067 bool br_allowed_egress(struct net_bridge_vlan_group *vg, 1068 const struct sk_buff *skb); 1069 bool br_should_learn(struct net_bridge_port *p, struct sk_buff *skb, u16 *vid); 1070 struct sk_buff *br_handle_vlan(struct net_bridge *br, 1071 const struct net_bridge_port *port, 1072 struct net_bridge_vlan_group *vg, 1073 struct sk_buff *skb); 1074 int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags, 1075 bool *changed, struct netlink_ext_ack *extack); 1076 int br_vlan_delete(struct net_bridge *br, u16 vid); 1077 void br_vlan_flush(struct net_bridge *br); 1078 struct net_bridge_vlan *br_vlan_find(struct net_bridge_vlan_group *vg, u16 vid); 1079 void br_recalculate_fwd_mask(struct net_bridge *br); 1080 int __br_vlan_filter_toggle(struct net_bridge *br, unsigned long val); 1081 int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val); 1082 int __br_vlan_set_proto(struct net_bridge *br, __be16 proto); 1083 int br_vlan_set_proto(struct net_bridge *br, unsigned long val); 1084 int br_vlan_set_stats(struct net_bridge *br, unsigned long val); 1085 int br_vlan_set_stats_per_port(struct net_bridge *br, unsigned long val); 1086 int br_vlan_init(struct net_bridge *br); 1087 int br_vlan_set_default_pvid(struct net_bridge *br, unsigned long val); 1088 int __br_vlan_set_default_pvid(struct net_bridge *br, u16 pvid, 1089 struct netlink_ext_ack *extack); 1090 int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags, 1091 bool *changed, struct netlink_ext_ack *extack); 1092 int nbp_vlan_delete(struct net_bridge_port *port, u16 vid); 1093 void nbp_vlan_flush(struct net_bridge_port *port); 1094 int nbp_vlan_init(struct net_bridge_port *port, struct netlink_ext_ack *extack); 1095 int nbp_get_num_vlan_infos(struct net_bridge_port *p, u32 filter_mask); 1096 void br_vlan_get_stats(const struct net_bridge_vlan *v, 1097 struct pcpu_sw_netstats *stats); 1098 void br_vlan_port_event(struct net_bridge_port *p, unsigned long event); 1099 int br_vlan_bridge_event(struct net_device *dev, unsigned long event, 1100 void *ptr); 1101 void br_vlan_rtnl_init(void); 1102 void br_vlan_rtnl_uninit(void); 1103 void br_vlan_notify(const struct net_bridge *br, 1104 const struct net_bridge_port *p, 1105 u16 vid, u16 vid_range, 1106 int cmd); 1107 bool br_vlan_can_enter_range(const struct net_bridge_vlan *v_curr, 1108 const struct net_bridge_vlan *range_end); 1109 1110 static inline struct net_bridge_vlan_group *br_vlan_group( 1111 const struct net_bridge *br) 1112 { 1113 return rtnl_dereference(br->vlgrp); 1114 } 1115 1116 static inline struct net_bridge_vlan_group *nbp_vlan_group( 1117 const struct net_bridge_port *p) 1118 { 1119 return rtnl_dereference(p->vlgrp); 1120 } 1121 1122 static inline struct net_bridge_vlan_group *br_vlan_group_rcu( 1123 const struct net_bridge *br) 1124 { 1125 return rcu_dereference(br->vlgrp); 1126 } 1127 1128 static inline struct net_bridge_vlan_group *nbp_vlan_group_rcu( 1129 const struct net_bridge_port *p) 1130 { 1131 return rcu_dereference(p->vlgrp); 1132 } 1133 1134 /* Since bridge now depends on 8021Q module, but the time bridge sees the 1135 * skb, the vlan tag will always be present if the frame was tagged. 1136 */ 1137 static inline int br_vlan_get_tag(const struct sk_buff *skb, u16 *vid) 1138 { 1139 int err = 0; 1140 1141 if (skb_vlan_tag_present(skb)) { 1142 *vid = skb_vlan_tag_get_id(skb); 1143 } else { 1144 *vid = 0; 1145 err = -EINVAL; 1146 } 1147 1148 return err; 1149 } 1150 1151 static inline u16 br_get_pvid(const struct net_bridge_vlan_group *vg) 1152 { 1153 if (!vg) 1154 return 0; 1155 1156 smp_rmb(); 1157 return vg->pvid; 1158 } 1159 1160 static inline u16 br_vlan_flags(const struct net_bridge_vlan *v, u16 pvid) 1161 { 1162 return v->vid == pvid ? v->flags | BRIDGE_VLAN_INFO_PVID : v->flags; 1163 } 1164 #else 1165 static inline bool br_allowed_ingress(const struct net_bridge *br, 1166 struct net_bridge_vlan_group *vg, 1167 struct sk_buff *skb, 1168 u16 *vid, u8 *state) 1169 { 1170 return true; 1171 } 1172 1173 static inline bool br_allowed_egress(struct net_bridge_vlan_group *vg, 1174 const struct sk_buff *skb) 1175 { 1176 return true; 1177 } 1178 1179 static inline bool br_should_learn(struct net_bridge_port *p, 1180 struct sk_buff *skb, u16 *vid) 1181 { 1182 return true; 1183 } 1184 1185 static inline struct sk_buff *br_handle_vlan(struct net_bridge *br, 1186 const struct net_bridge_port *port, 1187 struct net_bridge_vlan_group *vg, 1188 struct sk_buff *skb) 1189 { 1190 return skb; 1191 } 1192 1193 static inline int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags, 1194 bool *changed, struct netlink_ext_ack *extack) 1195 { 1196 *changed = false; 1197 return -EOPNOTSUPP; 1198 } 1199 1200 static inline int br_vlan_delete(struct net_bridge *br, u16 vid) 1201 { 1202 return -EOPNOTSUPP; 1203 } 1204 1205 static inline void br_vlan_flush(struct net_bridge *br) 1206 { 1207 } 1208 1209 static inline void br_recalculate_fwd_mask(struct net_bridge *br) 1210 { 1211 } 1212 1213 static inline int br_vlan_init(struct net_bridge *br) 1214 { 1215 return 0; 1216 } 1217 1218 static inline int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags, 1219 bool *changed, struct netlink_ext_ack *extack) 1220 { 1221 *changed = false; 1222 return -EOPNOTSUPP; 1223 } 1224 1225 static inline int nbp_vlan_delete(struct net_bridge_port *port, u16 vid) 1226 { 1227 return -EOPNOTSUPP; 1228 } 1229 1230 static inline void nbp_vlan_flush(struct net_bridge_port *port) 1231 { 1232 } 1233 1234 static inline struct net_bridge_vlan *br_vlan_find(struct net_bridge_vlan_group *vg, 1235 u16 vid) 1236 { 1237 return NULL; 1238 } 1239 1240 static inline int nbp_vlan_init(struct net_bridge_port *port, 1241 struct netlink_ext_ack *extack) 1242 { 1243 return 0; 1244 } 1245 1246 static inline u16 br_vlan_get_tag(const struct sk_buff *skb, u16 *tag) 1247 { 1248 return 0; 1249 } 1250 1251 static inline u16 br_get_pvid(const struct net_bridge_vlan_group *vg) 1252 { 1253 return 0; 1254 } 1255 1256 static inline int __br_vlan_filter_toggle(struct net_bridge *br, 1257 unsigned long val) 1258 { 1259 return -EOPNOTSUPP; 1260 } 1261 1262 static inline int nbp_get_num_vlan_infos(struct net_bridge_port *p, 1263 u32 filter_mask) 1264 { 1265 return 0; 1266 } 1267 1268 static inline struct net_bridge_vlan_group *br_vlan_group( 1269 const struct net_bridge *br) 1270 { 1271 return NULL; 1272 } 1273 1274 static inline struct net_bridge_vlan_group *nbp_vlan_group( 1275 const struct net_bridge_port *p) 1276 { 1277 return NULL; 1278 } 1279 1280 static inline struct net_bridge_vlan_group *br_vlan_group_rcu( 1281 const struct net_bridge *br) 1282 { 1283 return NULL; 1284 } 1285 1286 static inline struct net_bridge_vlan_group *nbp_vlan_group_rcu( 1287 const struct net_bridge_port *p) 1288 { 1289 return NULL; 1290 } 1291 1292 static inline void br_vlan_get_stats(const struct net_bridge_vlan *v, 1293 struct pcpu_sw_netstats *stats) 1294 { 1295 } 1296 1297 static inline void br_vlan_port_event(struct net_bridge_port *p, 1298 unsigned long event) 1299 { 1300 } 1301 1302 static inline int br_vlan_bridge_event(struct net_device *dev, 1303 unsigned long event, void *ptr) 1304 { 1305 return 0; 1306 } 1307 1308 static inline void br_vlan_rtnl_init(void) 1309 { 1310 } 1311 1312 static inline void br_vlan_rtnl_uninit(void) 1313 { 1314 } 1315 1316 static inline void br_vlan_notify(const struct net_bridge *br, 1317 const struct net_bridge_port *p, 1318 u16 vid, u16 vid_range, 1319 int cmd) 1320 { 1321 } 1322 1323 static inline bool br_vlan_can_enter_range(const struct net_bridge_vlan *v_curr, 1324 const struct net_bridge_vlan *range_end) 1325 { 1326 return true; 1327 } 1328 #endif 1329 1330 /* br_vlan_options.c */ 1331 #ifdef CONFIG_BRIDGE_VLAN_FILTERING 1332 bool br_vlan_opts_eq_range(const struct net_bridge_vlan *v_curr, 1333 const struct net_bridge_vlan *range_end); 1334 bool br_vlan_opts_fill(struct sk_buff *skb, const struct net_bridge_vlan *v); 1335 size_t br_vlan_opts_nl_size(void); 1336 int br_vlan_process_options(const struct net_bridge *br, 1337 const struct net_bridge_port *p, 1338 struct net_bridge_vlan *range_start, 1339 struct net_bridge_vlan *range_end, 1340 struct nlattr **tb, 1341 struct netlink_ext_ack *extack); 1342 1343 /* vlan state manipulation helpers using *_ONCE to annotate lock-free access */ 1344 static inline u8 br_vlan_get_state(const struct net_bridge_vlan *v) 1345 { 1346 return READ_ONCE(v->state); 1347 } 1348 1349 static inline void br_vlan_set_state(struct net_bridge_vlan *v, u8 state) 1350 { 1351 WRITE_ONCE(v->state, state); 1352 } 1353 1354 static inline u8 br_vlan_get_pvid_state(const struct net_bridge_vlan_group *vg) 1355 { 1356 return READ_ONCE(vg->pvid_state); 1357 } 1358 1359 static inline void br_vlan_set_pvid_state(struct net_bridge_vlan_group *vg, 1360 u8 state) 1361 { 1362 WRITE_ONCE(vg->pvid_state, state); 1363 } 1364 1365 /* learn_allow is true at ingress and false at egress */ 1366 static inline bool br_vlan_state_allowed(u8 state, bool learn_allow) 1367 { 1368 switch (state) { 1369 case BR_STATE_LEARNING: 1370 return learn_allow; 1371 case BR_STATE_FORWARDING: 1372 return true; 1373 default: 1374 return false; 1375 } 1376 } 1377 #endif 1378 1379 struct nf_br_ops { 1380 int (*br_dev_xmit_hook)(struct sk_buff *skb); 1381 }; 1382 extern const struct nf_br_ops __rcu *nf_br_ops; 1383 1384 /* br_netfilter.c */ 1385 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 1386 int br_nf_core_init(void); 1387 void br_nf_core_fini(void); 1388 void br_netfilter_rtable_init(struct net_bridge *); 1389 #else 1390 static inline int br_nf_core_init(void) { return 0; } 1391 static inline void br_nf_core_fini(void) {} 1392 #define br_netfilter_rtable_init(x) 1393 #endif 1394 1395 /* br_stp.c */ 1396 void br_set_state(struct net_bridge_port *p, unsigned int state); 1397 struct net_bridge_port *br_get_port(struct net_bridge *br, u16 port_no); 1398 void br_init_port(struct net_bridge_port *p); 1399 void br_become_designated_port(struct net_bridge_port *p); 1400 1401 void __br_set_forward_delay(struct net_bridge *br, unsigned long t); 1402 int br_set_forward_delay(struct net_bridge *br, unsigned long x); 1403 int br_set_hello_time(struct net_bridge *br, unsigned long x); 1404 int br_set_max_age(struct net_bridge *br, unsigned long x); 1405 int __set_ageing_time(struct net_device *dev, unsigned long t); 1406 int br_set_ageing_time(struct net_bridge *br, clock_t ageing_time); 1407 1408 1409 /* br_stp_if.c */ 1410 void br_stp_enable_bridge(struct net_bridge *br); 1411 void br_stp_disable_bridge(struct net_bridge *br); 1412 int br_stp_set_enabled(struct net_bridge *br, unsigned long val, 1413 struct netlink_ext_ack *extack); 1414 void br_stp_enable_port(struct net_bridge_port *p); 1415 void br_stp_disable_port(struct net_bridge_port *p); 1416 bool br_stp_recalculate_bridge_id(struct net_bridge *br); 1417 void br_stp_change_bridge_id(struct net_bridge *br, const unsigned char *a); 1418 void br_stp_set_bridge_priority(struct net_bridge *br, u16 newprio); 1419 int br_stp_set_port_priority(struct net_bridge_port *p, unsigned long newprio); 1420 int br_stp_set_path_cost(struct net_bridge_port *p, unsigned long path_cost); 1421 ssize_t br_show_bridge_id(char *buf, const struct bridge_id *id); 1422 1423 /* br_stp_bpdu.c */ 1424 struct stp_proto; 1425 void br_stp_rcv(const struct stp_proto *proto, struct sk_buff *skb, 1426 struct net_device *dev); 1427 1428 /* br_stp_timer.c */ 1429 void br_stp_timer_init(struct net_bridge *br); 1430 void br_stp_port_timer_init(struct net_bridge_port *p); 1431 unsigned long br_timer_value(const struct timer_list *timer); 1432 1433 /* br.c */ 1434 #if IS_ENABLED(CONFIG_ATM_LANE) 1435 extern int (*br_fdb_test_addr_hook)(struct net_device *dev, unsigned char *addr); 1436 #endif 1437 1438 /* br_mrp.c */ 1439 #if IS_ENABLED(CONFIG_BRIDGE_MRP) 1440 int br_mrp_parse(struct net_bridge *br, struct net_bridge_port *p, 1441 struct nlattr *attr, int cmd, struct netlink_ext_ack *extack); 1442 bool br_mrp_enabled(struct net_bridge *br); 1443 void br_mrp_port_del(struct net_bridge *br, struct net_bridge_port *p); 1444 int br_mrp_fill_info(struct sk_buff *skb, struct net_bridge *br); 1445 #else 1446 static inline int br_mrp_parse(struct net_bridge *br, struct net_bridge_port *p, 1447 struct nlattr *attr, int cmd, 1448 struct netlink_ext_ack *extack) 1449 { 1450 return -EOPNOTSUPP; 1451 } 1452 1453 static inline bool br_mrp_enabled(struct net_bridge *br) 1454 { 1455 return false; 1456 } 1457 1458 static inline void br_mrp_port_del(struct net_bridge *br, 1459 struct net_bridge_port *p) 1460 { 1461 } 1462 1463 static inline int br_mrp_fill_info(struct sk_buff *skb, struct net_bridge *br) 1464 { 1465 return 0; 1466 } 1467 1468 #endif 1469 1470 /* br_cfm.c */ 1471 #if IS_ENABLED(CONFIG_BRIDGE_CFM) 1472 int br_cfm_parse(struct net_bridge *br, struct net_bridge_port *p, 1473 struct nlattr *attr, int cmd, struct netlink_ext_ack *extack); 1474 bool br_cfm_created(struct net_bridge *br); 1475 void br_cfm_port_del(struct net_bridge *br, struct net_bridge_port *p); 1476 int br_cfm_config_fill_info(struct sk_buff *skb, struct net_bridge *br); 1477 int br_cfm_status_fill_info(struct sk_buff *skb, 1478 struct net_bridge *br, 1479 bool getlink); 1480 int br_cfm_mep_count(struct net_bridge *br, u32 *count); 1481 int br_cfm_peer_mep_count(struct net_bridge *br, u32 *count); 1482 #else 1483 static inline int br_cfm_parse(struct net_bridge *br, struct net_bridge_port *p, 1484 struct nlattr *attr, int cmd, 1485 struct netlink_ext_ack *extack) 1486 { 1487 return -EOPNOTSUPP; 1488 } 1489 1490 static inline bool br_cfm_created(struct net_bridge *br) 1491 { 1492 return false; 1493 } 1494 1495 static inline void br_cfm_port_del(struct net_bridge *br, 1496 struct net_bridge_port *p) 1497 { 1498 } 1499 1500 static inline int br_cfm_config_fill_info(struct sk_buff *skb, struct net_bridge *br) 1501 { 1502 return -EOPNOTSUPP; 1503 } 1504 1505 static inline int br_cfm_status_fill_info(struct sk_buff *skb, 1506 struct net_bridge *br, 1507 bool getlink) 1508 { 1509 return -EOPNOTSUPP; 1510 } 1511 1512 static inline int br_cfm_mep_count(struct net_bridge *br, u32 *count) 1513 { 1514 return -EOPNOTSUPP; 1515 } 1516 1517 static inline int br_cfm_peer_mep_count(struct net_bridge *br, u32 *count) 1518 { 1519 return -EOPNOTSUPP; 1520 } 1521 #endif 1522 1523 /* br_netlink.c */ 1524 extern struct rtnl_link_ops br_link_ops; 1525 int br_netlink_init(void); 1526 void br_netlink_fini(void); 1527 void br_ifinfo_notify(int event, const struct net_bridge *br, 1528 const struct net_bridge_port *port); 1529 void br_info_notify(int event, const struct net_bridge *br, 1530 const struct net_bridge_port *port, u32 filter); 1531 int br_setlink(struct net_device *dev, struct nlmsghdr *nlmsg, u16 flags, 1532 struct netlink_ext_ack *extack); 1533 int br_dellink(struct net_device *dev, struct nlmsghdr *nlmsg, u16 flags); 1534 int br_getlink(struct sk_buff *skb, u32 pid, u32 seq, struct net_device *dev, 1535 u32 filter_mask, int nlflags); 1536 int br_process_vlan_info(struct net_bridge *br, 1537 struct net_bridge_port *p, int cmd, 1538 struct bridge_vlan_info *vinfo_curr, 1539 struct bridge_vlan_info **vinfo_last, 1540 bool *changed, 1541 struct netlink_ext_ack *extack); 1542 1543 #ifdef CONFIG_SYSFS 1544 /* br_sysfs_if.c */ 1545 extern const struct sysfs_ops brport_sysfs_ops; 1546 int br_sysfs_addif(struct net_bridge_port *p); 1547 int br_sysfs_renameif(struct net_bridge_port *p); 1548 1549 /* br_sysfs_br.c */ 1550 int br_sysfs_addbr(struct net_device *dev); 1551 void br_sysfs_delbr(struct net_device *dev); 1552 1553 #else 1554 1555 static inline int br_sysfs_addif(struct net_bridge_port *p) { return 0; } 1556 static inline int br_sysfs_renameif(struct net_bridge_port *p) { return 0; } 1557 static inline int br_sysfs_addbr(struct net_device *dev) { return 0; } 1558 static inline void br_sysfs_delbr(struct net_device *dev) { return; } 1559 #endif /* CONFIG_SYSFS */ 1560 1561 /* br_switchdev.c */ 1562 #ifdef CONFIG_NET_SWITCHDEV 1563 int nbp_switchdev_mark_set(struct net_bridge_port *p); 1564 void nbp_switchdev_frame_mark(const struct net_bridge_port *p, 1565 struct sk_buff *skb); 1566 bool nbp_switchdev_allowed_egress(const struct net_bridge_port *p, 1567 const struct sk_buff *skb); 1568 int br_switchdev_set_port_flag(struct net_bridge_port *p, 1569 unsigned long flags, 1570 unsigned long mask); 1571 void br_switchdev_fdb_notify(const struct net_bridge_fdb_entry *fdb, 1572 int type); 1573 int br_switchdev_port_vlan_add(struct net_device *dev, u16 vid, u16 flags, 1574 struct netlink_ext_ack *extack); 1575 int br_switchdev_port_vlan_del(struct net_device *dev, u16 vid); 1576 1577 static inline void br_switchdev_frame_unmark(struct sk_buff *skb) 1578 { 1579 skb->offload_fwd_mark = 0; 1580 } 1581 #else 1582 static inline int nbp_switchdev_mark_set(struct net_bridge_port *p) 1583 { 1584 return 0; 1585 } 1586 1587 static inline void nbp_switchdev_frame_mark(const struct net_bridge_port *p, 1588 struct sk_buff *skb) 1589 { 1590 } 1591 1592 static inline bool nbp_switchdev_allowed_egress(const struct net_bridge_port *p, 1593 const struct sk_buff *skb) 1594 { 1595 return true; 1596 } 1597 1598 static inline int br_switchdev_set_port_flag(struct net_bridge_port *p, 1599 unsigned long flags, 1600 unsigned long mask) 1601 { 1602 return 0; 1603 } 1604 1605 static inline int br_switchdev_port_vlan_add(struct net_device *dev, 1606 u16 vid, u16 flags, 1607 struct netlink_ext_ack *extack) 1608 { 1609 return -EOPNOTSUPP; 1610 } 1611 1612 static inline int br_switchdev_port_vlan_del(struct net_device *dev, u16 vid) 1613 { 1614 return -EOPNOTSUPP; 1615 } 1616 1617 static inline void 1618 br_switchdev_fdb_notify(const struct net_bridge_fdb_entry *fdb, int type) 1619 { 1620 } 1621 1622 static inline void br_switchdev_frame_unmark(struct sk_buff *skb) 1623 { 1624 } 1625 #endif /* CONFIG_NET_SWITCHDEV */ 1626 1627 /* br_arp_nd_proxy.c */ 1628 void br_recalculate_neigh_suppress_enabled(struct net_bridge *br); 1629 void br_do_proxy_suppress_arp(struct sk_buff *skb, struct net_bridge *br, 1630 u16 vid, struct net_bridge_port *p); 1631 void br_do_suppress_nd(struct sk_buff *skb, struct net_bridge *br, 1632 u16 vid, struct net_bridge_port *p, struct nd_msg *msg); 1633 struct nd_msg *br_is_nd_neigh_msg(struct sk_buff *skb, struct nd_msg *m); 1634 #endif 1635