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 <net/pkt_cls.h> 19 #include <linux/if_vlan.h> 20 #include <linux/rhashtable.h> 21 #include <linux/refcount.h> 22 23 #define BR_HASH_BITS 8 24 #define BR_HASH_SIZE (1 << BR_HASH_BITS) 25 26 #define BR_HOLD_TIME (1*HZ) 27 28 #define BR_PORT_BITS 10 29 #define BR_MAX_PORTS (1<<BR_PORT_BITS) 30 31 #define BR_MULTICAST_DEFAULT_HASH_MAX 4096 32 #define BR_MULTICAST_QUERY_INTVL_MIN msecs_to_jiffies(1000) 33 #define BR_MULTICAST_STARTUP_QUERY_INTVL_MIN BR_MULTICAST_QUERY_INTVL_MIN 34 35 #define BR_HWDOM_MAX BITS_PER_LONG 36 37 #define BR_VERSION "2.3" 38 39 /* Control of forwarding link local multicast */ 40 #define BR_GROUPFWD_DEFAULT 0 41 /* Don't allow forwarding of control protocols like STP, MAC PAUSE and LACP */ 42 enum { 43 BR_GROUPFWD_STP = BIT(0), 44 BR_GROUPFWD_MACPAUSE = BIT(1), 45 BR_GROUPFWD_LACP = BIT(2), 46 }; 47 48 #define BR_GROUPFWD_RESTRICTED (BR_GROUPFWD_STP | BR_GROUPFWD_MACPAUSE | \ 49 BR_GROUPFWD_LACP) 50 /* The Nearest Customer Bridge Group Address, 01-80-C2-00-00-[00,0B,0C,0D,0F] */ 51 #define BR_GROUPFWD_8021AD 0xB801u 52 53 /* Path to usermode spanning tree program */ 54 #define BR_STP_PROG "/sbin/bridge-stp" 55 56 #define BR_FDB_NOTIFY_SETTABLE_BITS (FDB_NOTIFY_BIT | FDB_NOTIFY_INACTIVE_BIT) 57 58 typedef struct bridge_id bridge_id; 59 typedef struct mac_addr mac_addr; 60 typedef __u16 port_id; 61 62 struct bridge_id { 63 unsigned char prio[2]; 64 unsigned char addr[ETH_ALEN]; 65 }; 66 67 struct mac_addr { 68 unsigned char addr[ETH_ALEN]; 69 }; 70 71 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING 72 /* our own querier */ 73 struct bridge_mcast_own_query { 74 struct timer_list timer; 75 u32 startup_sent; 76 }; 77 78 /* other querier */ 79 struct bridge_mcast_other_query { 80 struct timer_list timer; 81 struct timer_list delay_timer; 82 }; 83 84 /* selected querier */ 85 struct bridge_mcast_querier { 86 struct br_ip addr; 87 int port_ifidx; 88 seqcount_spinlock_t seq; 89 }; 90 91 /* IGMP/MLD statistics */ 92 struct bridge_mcast_stats { 93 struct br_mcast_stats mstats; 94 struct u64_stats_sync syncp; 95 }; 96 97 struct br_mdb_src_entry { 98 struct br_ip addr; 99 }; 100 101 struct br_mdb_config { 102 struct net_bridge *br; 103 struct net_bridge_port *p; 104 struct br_mdb_entry *entry; 105 struct br_ip group; 106 bool src_entry; 107 u8 filter_mode; 108 u16 nlflags; 109 struct br_mdb_src_entry *src_entries; 110 int num_src_entries; 111 u8 rt_protocol; 112 }; 113 #endif 114 115 /* net_bridge_mcast_port must be always defined due to forwarding stubs */ 116 struct net_bridge_mcast_port { 117 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING 118 struct net_bridge_port *port; 119 struct net_bridge_vlan *vlan; 120 121 struct bridge_mcast_own_query ip4_own_query; 122 struct timer_list ip4_mc_router_timer; 123 struct hlist_node ip4_rlist; 124 #if IS_ENABLED(CONFIG_IPV6) 125 struct bridge_mcast_own_query ip6_own_query; 126 struct timer_list ip6_mc_router_timer; 127 struct hlist_node ip6_rlist; 128 #endif /* IS_ENABLED(CONFIG_IPV6) */ 129 unsigned char multicast_router; 130 u32 mdb_n_entries; 131 u32 mdb_max_entries; 132 #endif /* CONFIG_BRIDGE_IGMP_SNOOPING */ 133 }; 134 135 /* net_bridge_mcast must be always defined due to forwarding stubs */ 136 struct net_bridge_mcast { 137 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING 138 struct net_bridge *br; 139 struct net_bridge_vlan *vlan; 140 141 u32 multicast_last_member_count; 142 u32 multicast_startup_query_count; 143 144 u8 multicast_querier; 145 u8 multicast_igmp_version; 146 u8 multicast_router; 147 #if IS_ENABLED(CONFIG_IPV6) 148 u8 multicast_mld_version; 149 #endif 150 unsigned long multicast_last_member_interval; 151 unsigned long multicast_membership_interval; 152 unsigned long multicast_querier_interval; 153 unsigned long multicast_query_interval; 154 unsigned long multicast_query_response_interval; 155 unsigned long multicast_startup_query_interval; 156 struct hlist_head ip4_mc_router_list; 157 struct timer_list ip4_mc_router_timer; 158 struct bridge_mcast_other_query ip4_other_query; 159 struct bridge_mcast_own_query ip4_own_query; 160 struct bridge_mcast_querier ip4_querier; 161 #if IS_ENABLED(CONFIG_IPV6) 162 struct hlist_head ip6_mc_router_list; 163 struct timer_list ip6_mc_router_timer; 164 struct bridge_mcast_other_query ip6_other_query; 165 struct bridge_mcast_own_query ip6_own_query; 166 struct bridge_mcast_querier ip6_querier; 167 #endif /* IS_ENABLED(CONFIG_IPV6) */ 168 #endif /* CONFIG_BRIDGE_IGMP_SNOOPING */ 169 }; 170 171 struct br_tunnel_info { 172 __be64 tunnel_id; 173 struct metadata_dst __rcu *tunnel_dst; 174 }; 175 176 /* private vlan flags */ 177 enum { 178 BR_VLFLAG_PER_PORT_STATS = BIT(0), 179 BR_VLFLAG_ADDED_BY_SWITCHDEV = BIT(1), 180 BR_VLFLAG_MCAST_ENABLED = BIT(2), 181 BR_VLFLAG_GLOBAL_MCAST_ENABLED = BIT(3), 182 BR_VLFLAG_NEIGH_SUPPRESS_ENABLED = BIT(4), 183 }; 184 185 /** 186 * struct net_bridge_vlan - per-vlan entry 187 * 188 * @vnode: rhashtable member 189 * @tnode: rhashtable member 190 * @vid: VLAN id 191 * @flags: bridge vlan flags 192 * @priv_flags: private (in-kernel) bridge vlan flags 193 * @state: STP state (e.g. blocking, learning, forwarding) 194 * @stats: per-cpu VLAN statistics 195 * @br: if MASTER flag set, this points to a bridge struct 196 * @port: if MASTER flag unset, this points to a port struct 197 * @refcnt: if MASTER flag set, this is bumped for each port referencing it 198 * @brvlan: if MASTER flag unset, this points to the global per-VLAN context 199 * for this VLAN entry 200 * @tinfo: bridge tunnel info 201 * @br_mcast_ctx: if MASTER flag set, this is the global vlan multicast context 202 * @port_mcast_ctx: if MASTER flag unset, this is the per-port/vlan multicast 203 * context 204 * @msti: if MASTER flag set, this holds the VLANs MST instance 205 * @vlist: sorted list of VLAN entries 206 * @rcu: used for entry destruction 207 * 208 * This structure is shared between the global per-VLAN entries contained in 209 * the bridge rhashtable and the local per-port per-VLAN entries contained in 210 * the port's rhashtable. The union entries should be interpreted depending on 211 * the entry flags that are set. 212 */ 213 struct net_bridge_vlan { 214 struct rhash_head vnode; 215 struct rhash_head tnode; 216 u16 vid; 217 u16 flags; 218 u16 priv_flags; 219 u8 state; 220 struct pcpu_sw_netstats __percpu *stats; 221 union { 222 struct net_bridge *br; 223 struct net_bridge_port *port; 224 }; 225 union { 226 refcount_t refcnt; 227 struct net_bridge_vlan *brvlan; 228 }; 229 230 struct br_tunnel_info tinfo; 231 232 union { 233 struct net_bridge_mcast br_mcast_ctx; 234 struct net_bridge_mcast_port port_mcast_ctx; 235 }; 236 237 u16 msti; 238 239 struct list_head vlist; 240 241 struct rcu_head rcu; 242 }; 243 244 /** 245 * struct net_bridge_vlan_group 246 * 247 * @vlan_hash: VLAN entry rhashtable 248 * @vlan_list: sorted VLAN entry list 249 * @num_vlans: number of total VLAN entries 250 * @pvid: PVID VLAN id 251 * @pvid_state: PVID's STP state (e.g. forwarding, learning, blocking) 252 * 253 * IMPORTANT: Be careful when checking if there're VLAN entries using list 254 * primitives because the bridge can have entries in its list which 255 * are just for global context but not for filtering, i.e. they have 256 * the master flag set but not the brentry flag. If you have to check 257 * if there're "real" entries in the bridge please test @num_vlans 258 */ 259 struct net_bridge_vlan_group { 260 struct rhashtable vlan_hash; 261 struct rhashtable tunnel_hash; 262 struct list_head vlan_list; 263 u16 num_vlans; 264 u16 pvid; 265 u8 pvid_state; 266 }; 267 268 /* bridge fdb flags */ 269 enum { 270 BR_FDB_LOCAL, 271 BR_FDB_STATIC, 272 BR_FDB_STICKY, 273 BR_FDB_ADDED_BY_USER, 274 BR_FDB_ADDED_BY_EXT_LEARN, 275 BR_FDB_OFFLOADED, 276 BR_FDB_NOTIFY, 277 BR_FDB_NOTIFY_INACTIVE, 278 BR_FDB_LOCKED, 279 BR_FDB_DYNAMIC_LEARNED, 280 }; 281 282 struct net_bridge_fdb_key { 283 mac_addr addr; 284 u16 vlan_id; 285 }; 286 287 struct net_bridge_fdb_entry { 288 struct rhash_head rhnode; 289 struct net_bridge_port *dst; 290 291 struct net_bridge_fdb_key key; 292 struct hlist_node fdb_node; 293 unsigned long flags; 294 295 /* write-heavy members should not affect lookups */ 296 unsigned long updated ____cacheline_aligned_in_smp; 297 unsigned long used; 298 299 struct rcu_head rcu; 300 }; 301 302 struct net_bridge_fdb_flush_desc { 303 unsigned long flags; 304 unsigned long flags_mask; 305 int port_ifindex; 306 u16 vlan_id; 307 }; 308 309 #define MDB_PG_FLAGS_PERMANENT BIT(0) 310 #define MDB_PG_FLAGS_OFFLOAD BIT(1) 311 #define MDB_PG_FLAGS_FAST_LEAVE BIT(2) 312 #define MDB_PG_FLAGS_STAR_EXCL BIT(3) 313 #define MDB_PG_FLAGS_BLOCKED BIT(4) 314 315 #define PG_SRC_ENT_LIMIT 32 316 317 #define BR_SGRP_F_DELETE BIT(0) 318 #define BR_SGRP_F_SEND BIT(1) 319 #define BR_SGRP_F_INSTALLED BIT(2) 320 #define BR_SGRP_F_USER_ADDED BIT(3) 321 322 struct net_bridge_mcast_gc { 323 struct hlist_node gc_node; 324 void (*destroy)(struct net_bridge_mcast_gc *gc); 325 }; 326 327 struct net_bridge_group_src { 328 struct hlist_node node; 329 330 struct br_ip addr; 331 struct net_bridge_port_group *pg; 332 u8 flags; 333 u8 src_query_rexmit_cnt; 334 struct timer_list timer; 335 336 struct net_bridge *br; 337 struct net_bridge_mcast_gc mcast_gc; 338 struct rcu_head rcu; 339 }; 340 341 struct net_bridge_port_group_sg_key { 342 struct net_bridge_port *port; 343 struct br_ip addr; 344 }; 345 346 struct net_bridge_port_group { 347 struct net_bridge_port_group __rcu *next; 348 struct net_bridge_port_group_sg_key key; 349 unsigned char eth_addr[ETH_ALEN] __aligned(2); 350 unsigned char flags; 351 unsigned char filter_mode; 352 unsigned char grp_query_rexmit_cnt; 353 unsigned char rt_protocol; 354 355 struct hlist_head src_list; 356 unsigned int src_ents; 357 struct timer_list timer; 358 struct timer_list rexmit_timer; 359 struct hlist_node mglist; 360 struct rb_root eht_set_tree; 361 struct rb_root eht_host_tree; 362 363 struct rhash_head rhnode; 364 struct net_bridge_mcast_gc mcast_gc; 365 struct rcu_head rcu; 366 }; 367 368 struct net_bridge_mdb_entry { 369 struct rhash_head rhnode; 370 struct net_bridge *br; 371 struct net_bridge_port_group __rcu *ports; 372 struct br_ip addr; 373 bool host_joined; 374 375 struct timer_list timer; 376 struct hlist_node mdb_node; 377 378 struct net_bridge_mcast_gc mcast_gc; 379 struct rcu_head rcu; 380 }; 381 382 struct net_bridge_port { 383 struct net_bridge *br; 384 struct net_device *dev; 385 netdevice_tracker dev_tracker; 386 struct list_head list; 387 388 unsigned long flags; 389 #ifdef CONFIG_BRIDGE_VLAN_FILTERING 390 struct net_bridge_vlan_group __rcu *vlgrp; 391 #endif 392 struct net_bridge_port __rcu *backup_port; 393 u32 backup_nhid; 394 395 /* STP */ 396 u8 priority; 397 u8 state; 398 u16 port_no; 399 unsigned char topology_change_ack; 400 unsigned char config_pending; 401 port_id port_id; 402 port_id designated_port; 403 bridge_id designated_root; 404 bridge_id designated_bridge; 405 u32 path_cost; 406 u32 designated_cost; 407 unsigned long designated_age; 408 409 struct timer_list forward_delay_timer; 410 struct timer_list hold_timer; 411 struct timer_list message_age_timer; 412 struct kobject kobj; 413 struct rcu_head rcu; 414 415 struct net_bridge_mcast_port multicast_ctx; 416 417 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING 418 struct bridge_mcast_stats __percpu *mcast_stats; 419 420 u32 multicast_eht_hosts_limit; 421 u32 multicast_eht_hosts_cnt; 422 struct hlist_head mglist; 423 #endif 424 425 #ifdef CONFIG_SYSFS 426 char sysfs_name[IFNAMSIZ]; 427 #endif 428 429 #ifdef CONFIG_NET_POLL_CONTROLLER 430 struct netpoll *np; 431 #endif 432 #ifdef CONFIG_NET_SWITCHDEV 433 /* Identifier used to group ports that share the same switchdev 434 * hardware domain. 435 */ 436 int hwdom; 437 int offload_count; 438 struct netdev_phys_item_id ppid; 439 #endif 440 u16 group_fwd_mask; 441 u16 backup_redirected_cnt; 442 443 struct bridge_stp_xstats stp_xstats; 444 }; 445 446 #define kobj_to_brport(obj) container_of(obj, struct net_bridge_port, kobj) 447 448 #define br_auto_port(p) ((p)->flags & BR_AUTO_MASK) 449 #define br_promisc_port(p) ((p)->flags & BR_PROMISC) 450 451 static inline struct net_bridge_port *br_port_get_rcu(const struct net_device *dev) 452 { 453 return rcu_dereference(dev->rx_handler_data); 454 } 455 456 static inline struct net_bridge_port *br_port_get_rtnl(const struct net_device *dev) 457 { 458 return netif_is_bridge_port(dev) ? 459 rtnl_dereference(dev->rx_handler_data) : NULL; 460 } 461 462 static inline struct net_bridge_port *br_port_get_rtnl_rcu(const struct net_device *dev) 463 { 464 return netif_is_bridge_port(dev) ? 465 rcu_dereference_rtnl(dev->rx_handler_data) : NULL; 466 } 467 468 enum net_bridge_opts { 469 BROPT_VLAN_ENABLED, 470 BROPT_VLAN_STATS_ENABLED, 471 BROPT_NF_CALL_IPTABLES, 472 BROPT_NF_CALL_IP6TABLES, 473 BROPT_NF_CALL_ARPTABLES, 474 BROPT_GROUP_ADDR_SET, 475 BROPT_MULTICAST_ENABLED, 476 BROPT_MULTICAST_QUERY_USE_IFADDR, 477 BROPT_MULTICAST_STATS_ENABLED, 478 BROPT_HAS_IPV6_ADDR, 479 BROPT_NEIGH_SUPPRESS_ENABLED, 480 BROPT_MTU_SET_BY_USER, 481 BROPT_VLAN_STATS_PER_PORT, 482 BROPT_NO_LL_LEARN, 483 BROPT_VLAN_BRIDGE_BINDING, 484 BROPT_MCAST_VLAN_SNOOPING_ENABLED, 485 BROPT_MST_ENABLED, 486 }; 487 488 struct net_bridge { 489 spinlock_t lock; 490 spinlock_t hash_lock; 491 struct hlist_head frame_type_list; 492 struct net_device *dev; 493 unsigned long options; 494 /* These fields are accessed on each packet */ 495 #ifdef CONFIG_BRIDGE_VLAN_FILTERING 496 __be16 vlan_proto; 497 u16 default_pvid; 498 struct net_bridge_vlan_group __rcu *vlgrp; 499 #endif 500 501 struct rhashtable fdb_hash_tbl; 502 struct list_head port_list; 503 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 504 union { 505 struct rtable fake_rtable; 506 struct rt6_info fake_rt6_info; 507 }; 508 #endif 509 u16 group_fwd_mask; 510 u16 group_fwd_mask_required; 511 512 /* STP */ 513 bridge_id designated_root; 514 bridge_id bridge_id; 515 unsigned char topology_change; 516 unsigned char topology_change_detected; 517 u16 root_port; 518 unsigned long max_age; 519 unsigned long hello_time; 520 unsigned long forward_delay; 521 unsigned long ageing_time; 522 unsigned long bridge_max_age; 523 unsigned long bridge_hello_time; 524 unsigned long bridge_forward_delay; 525 unsigned long bridge_ageing_time; 526 u32 root_path_cost; 527 528 u8 group_addr[ETH_ALEN]; 529 530 enum { 531 BR_NO_STP, /* no spanning tree */ 532 BR_KERNEL_STP, /* old STP in kernel */ 533 BR_USER_STP, /* new RSTP in userspace */ 534 } stp_enabled; 535 536 struct net_bridge_mcast multicast_ctx; 537 538 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING 539 struct bridge_mcast_stats __percpu *mcast_stats; 540 541 u32 hash_max; 542 543 spinlock_t multicast_lock; 544 545 struct rhashtable mdb_hash_tbl; 546 struct rhashtable sg_port_tbl; 547 548 struct hlist_head mcast_gc_list; 549 struct hlist_head mdb_list; 550 551 struct work_struct mcast_gc_work; 552 #endif 553 554 struct timer_list hello_timer; 555 struct timer_list tcn_timer; 556 struct timer_list topology_change_timer; 557 struct delayed_work gc_work; 558 struct kobject *ifobj; 559 u32 auto_cnt; 560 561 atomic_t fdb_n_learned; 562 u32 fdb_max_learned; 563 564 #ifdef CONFIG_NET_SWITCHDEV 565 /* Counter used to make sure that hardware domains get unique 566 * identifiers in case a bridge spans multiple switchdev instances. 567 */ 568 int last_hwdom; 569 /* Bit mask of hardware domain numbers in use */ 570 unsigned long busy_hwdoms; 571 #endif 572 struct hlist_head fdb_list; 573 574 #if IS_ENABLED(CONFIG_BRIDGE_MRP) 575 struct hlist_head mrp_list; 576 #endif 577 #if IS_ENABLED(CONFIG_BRIDGE_CFM) 578 struct hlist_head mep_list; 579 #endif 580 }; 581 582 struct br_input_skb_cb { 583 struct net_device *brdev; 584 585 u16 frag_max_size; 586 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING 587 u8 igmp; 588 u8 mrouters_only:1; 589 #endif 590 u8 proxyarp_replied:1; 591 u8 src_port_isolated:1; 592 #ifdef CONFIG_BRIDGE_VLAN_FILTERING 593 u8 vlan_filtered:1; 594 #endif 595 #ifdef CONFIG_NETFILTER_FAMILY_BRIDGE 596 u8 br_netfilter_broute:1; 597 #endif 598 599 #ifdef CONFIG_NET_SWITCHDEV 600 /* Set if TX data plane offloading is used towards at least one 601 * hardware domain. 602 */ 603 u8 tx_fwd_offload:1; 604 /* The switchdev hardware domain from which this packet was received. 605 * If skb->offload_fwd_mark was set, then this packet was already 606 * forwarded by hardware to the other ports in the source hardware 607 * domain, otherwise it wasn't. 608 */ 609 int src_hwdom; 610 /* Bit mask of hardware domains towards this packet has already been 611 * transmitted using the TX data plane offload. 612 */ 613 unsigned long fwd_hwdoms; 614 #endif 615 616 u32 backup_nhid; 617 }; 618 619 #define BR_INPUT_SKB_CB(__skb) ((struct br_input_skb_cb *)(__skb)->cb) 620 621 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING 622 # define BR_INPUT_SKB_CB_MROUTERS_ONLY(__skb) (BR_INPUT_SKB_CB(__skb)->mrouters_only) 623 #else 624 # define BR_INPUT_SKB_CB_MROUTERS_ONLY(__skb) (0) 625 #endif 626 627 #define br_printk(level, br, format, args...) \ 628 printk(level "%s: " format, (br)->dev->name, ##args) 629 630 #define br_err(__br, format, args...) \ 631 br_printk(KERN_ERR, __br, format, ##args) 632 #define br_warn(__br, format, args...) \ 633 br_printk(KERN_WARNING, __br, format, ##args) 634 #define br_notice(__br, format, args...) \ 635 br_printk(KERN_NOTICE, __br, format, ##args) 636 #define br_info(__br, format, args...) \ 637 br_printk(KERN_INFO, __br, format, ##args) 638 639 #define br_debug(br, format, args...) \ 640 pr_debug("%s: " format, (br)->dev->name, ##args) 641 642 /* called under bridge lock */ 643 static inline int br_is_root_bridge(const struct net_bridge *br) 644 { 645 return !memcmp(&br->bridge_id, &br->designated_root, 8); 646 } 647 648 /* check if a VLAN entry is global */ 649 static inline bool br_vlan_is_master(const struct net_bridge_vlan *v) 650 { 651 return v->flags & BRIDGE_VLAN_INFO_MASTER; 652 } 653 654 /* check if a VLAN entry is used by the bridge */ 655 static inline bool br_vlan_is_brentry(const struct net_bridge_vlan *v) 656 { 657 return v->flags & BRIDGE_VLAN_INFO_BRENTRY; 658 } 659 660 /* check if we should use the vlan entry, returns false if it's only context */ 661 static inline bool br_vlan_should_use(const struct net_bridge_vlan *v) 662 { 663 if (br_vlan_is_master(v)) { 664 if (br_vlan_is_brentry(v)) 665 return true; 666 else 667 return false; 668 } 669 670 return true; 671 } 672 673 static inline bool nbp_state_should_learn(const struct net_bridge_port *p) 674 { 675 return p->state == BR_STATE_LEARNING || p->state == BR_STATE_FORWARDING; 676 } 677 678 static inline bool br_vlan_valid_id(u16 vid, struct netlink_ext_ack *extack) 679 { 680 bool ret = vid > 0 && vid < VLAN_VID_MASK; 681 682 if (!ret) 683 NL_SET_ERR_MSG_MOD(extack, "Vlan id is invalid"); 684 685 return ret; 686 } 687 688 static inline bool br_vlan_valid_range(const struct bridge_vlan_info *cur, 689 const struct bridge_vlan_info *last, 690 struct netlink_ext_ack *extack) 691 { 692 /* pvid flag is not allowed in ranges */ 693 if (cur->flags & BRIDGE_VLAN_INFO_PVID) { 694 NL_SET_ERR_MSG_MOD(extack, "Pvid isn't allowed in a range"); 695 return false; 696 } 697 698 /* when cur is the range end, check if: 699 * - it has range start flag 700 * - range ids are invalid (end is equal to or before start) 701 */ 702 if (last) { 703 if (cur->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) { 704 NL_SET_ERR_MSG_MOD(extack, "Found a new vlan range start while processing one"); 705 return false; 706 } else if (!(cur->flags & BRIDGE_VLAN_INFO_RANGE_END)) { 707 NL_SET_ERR_MSG_MOD(extack, "Vlan range end flag is missing"); 708 return false; 709 } else if (cur->vid <= last->vid) { 710 NL_SET_ERR_MSG_MOD(extack, "End vlan id is less than or equal to start vlan id"); 711 return false; 712 } 713 } 714 715 /* check for required range flags */ 716 if (!(cur->flags & (BRIDGE_VLAN_INFO_RANGE_BEGIN | 717 BRIDGE_VLAN_INFO_RANGE_END))) { 718 NL_SET_ERR_MSG_MOD(extack, "Both vlan range flags are missing"); 719 return false; 720 } 721 722 return true; 723 } 724 725 static inline u8 br_vlan_multicast_router(const struct net_bridge_vlan *v) 726 { 727 u8 mcast_router = MDB_RTR_TYPE_DISABLED; 728 729 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING 730 if (!br_vlan_is_master(v)) 731 mcast_router = v->port_mcast_ctx.multicast_router; 732 else 733 mcast_router = v->br_mcast_ctx.multicast_router; 734 #endif 735 736 return mcast_router; 737 } 738 739 static inline int br_afspec_cmd_to_rtm(int cmd) 740 { 741 switch (cmd) { 742 case RTM_SETLINK: 743 return RTM_NEWVLAN; 744 case RTM_DELLINK: 745 return RTM_DELVLAN; 746 } 747 748 return 0; 749 } 750 751 static inline int br_opt_get(const struct net_bridge *br, 752 enum net_bridge_opts opt) 753 { 754 return test_bit(opt, &br->options); 755 } 756 757 int br_boolopt_toggle(struct net_bridge *br, enum br_boolopt_id opt, bool on, 758 struct netlink_ext_ack *extack); 759 int br_boolopt_get(const struct net_bridge *br, enum br_boolopt_id opt); 760 int br_boolopt_multi_toggle(struct net_bridge *br, 761 struct br_boolopt_multi *bm, 762 struct netlink_ext_ack *extack); 763 void br_boolopt_multi_get(const struct net_bridge *br, 764 struct br_boolopt_multi *bm); 765 void br_opt_toggle(struct net_bridge *br, enum net_bridge_opts opt, bool on); 766 767 #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT) 768 static inline void br_tc_skb_miss_set(struct sk_buff *skb, bool miss) 769 { 770 struct tc_skb_ext *ext; 771 772 if (!tc_skb_ext_tc_enabled()) 773 return; 774 775 ext = skb_ext_find(skb, TC_SKB_EXT); 776 if (ext) { 777 ext->l2_miss = miss; 778 return; 779 } 780 if (!miss) 781 return; 782 ext = tc_skb_ext_alloc(skb); 783 if (!ext) 784 return; 785 ext->l2_miss = true; 786 } 787 #else 788 static inline void br_tc_skb_miss_set(struct sk_buff *skb, bool miss) 789 { 790 } 791 #endif 792 793 /* br_device.c */ 794 void br_dev_setup(struct net_device *dev); 795 void br_dev_delete(struct net_device *dev, struct list_head *list); 796 netdev_tx_t br_dev_xmit(struct sk_buff *skb, struct net_device *dev); 797 #ifdef CONFIG_NET_POLL_CONTROLLER 798 static inline void br_netpoll_send_skb(const struct net_bridge_port *p, 799 struct sk_buff *skb) 800 { 801 netpoll_send_skb(p->np, skb); 802 } 803 804 int br_netpoll_enable(struct net_bridge_port *p); 805 void br_netpoll_disable(struct net_bridge_port *p); 806 #else 807 static inline void br_netpoll_send_skb(const struct net_bridge_port *p, 808 struct sk_buff *skb) 809 { 810 } 811 812 static inline int br_netpoll_enable(struct net_bridge_port *p) 813 { 814 return 0; 815 } 816 817 static inline void br_netpoll_disable(struct net_bridge_port *p) 818 { 819 } 820 #endif 821 822 /* br_fdb.c */ 823 #define FDB_FLUSH_IGNORED_NDM_FLAGS (NTF_MASTER | NTF_SELF) 824 #define FDB_FLUSH_ALLOWED_NDM_STATES (NUD_PERMANENT | NUD_NOARP) 825 #define FDB_FLUSH_ALLOWED_NDM_FLAGS (NTF_USE | NTF_EXT_LEARNED | \ 826 NTF_STICKY | NTF_OFFLOADED) 827 828 int br_fdb_init(void); 829 void br_fdb_fini(void); 830 int br_fdb_hash_init(struct net_bridge *br); 831 void br_fdb_hash_fini(struct net_bridge *br); 832 void br_fdb_flush(struct net_bridge *br, 833 const struct net_bridge_fdb_flush_desc *desc); 834 void br_fdb_find_delete_local(struct net_bridge *br, 835 const struct net_bridge_port *p, 836 const unsigned char *addr, u16 vid); 837 void br_fdb_changeaddr(struct net_bridge_port *p, const unsigned char *newaddr); 838 void br_fdb_change_mac_address(struct net_bridge *br, const u8 *newaddr); 839 void br_fdb_cleanup(struct work_struct *work); 840 void br_fdb_delete_by_port(struct net_bridge *br, 841 const struct net_bridge_port *p, u16 vid, int do_all); 842 struct net_bridge_fdb_entry *br_fdb_find_rcu(struct net_bridge *br, 843 const unsigned char *addr, 844 __u16 vid); 845 int br_fdb_test_addr(struct net_device *dev, unsigned char *addr); 846 int br_fdb_fillbuf(struct net_bridge *br, void *buf, unsigned long count, 847 unsigned long off); 848 int br_fdb_add_local(struct net_bridge *br, struct net_bridge_port *source, 849 const unsigned char *addr, u16 vid); 850 void br_fdb_update(struct net_bridge *br, struct net_bridge_port *source, 851 const unsigned char *addr, u16 vid, unsigned long flags); 852 853 int br_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[], 854 struct net_device *dev, const unsigned char *addr, u16 vid, 855 struct netlink_ext_ack *extack); 856 int br_fdb_delete_bulk(struct nlmsghdr *nlh, struct net_device *dev, 857 struct netlink_ext_ack *extack); 858 int br_fdb_add(struct ndmsg *nlh, struct nlattr *tb[], struct net_device *dev, 859 const unsigned char *addr, u16 vid, u16 nlh_flags, 860 struct netlink_ext_ack *extack); 861 int br_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb, 862 struct net_device *dev, struct net_device *fdev, int *idx); 863 int br_fdb_get(struct sk_buff *skb, struct nlattr *tb[], struct net_device *dev, 864 const unsigned char *addr, u16 vid, u32 portid, u32 seq, 865 struct netlink_ext_ack *extack); 866 int br_fdb_sync_static(struct net_bridge *br, struct net_bridge_port *p); 867 void br_fdb_unsync_static(struct net_bridge *br, struct net_bridge_port *p); 868 int br_fdb_external_learn_add(struct net_bridge *br, struct net_bridge_port *p, 869 const unsigned char *addr, u16 vid, 870 bool locked, bool swdev_notify); 871 int br_fdb_external_learn_del(struct net_bridge *br, struct net_bridge_port *p, 872 const unsigned char *addr, u16 vid, 873 bool swdev_notify); 874 void br_fdb_offloaded_set(struct net_bridge *br, struct net_bridge_port *p, 875 const unsigned char *addr, u16 vid, bool offloaded); 876 877 /* br_forward.c */ 878 enum br_pkt_type { 879 BR_PKT_UNICAST, 880 BR_PKT_MULTICAST, 881 BR_PKT_BROADCAST 882 }; 883 int br_dev_queue_push_xmit(struct net *net, struct sock *sk, struct sk_buff *skb); 884 void br_forward(const struct net_bridge_port *to, struct sk_buff *skb, 885 bool local_rcv, bool local_orig); 886 int br_forward_finish(struct net *net, struct sock *sk, struct sk_buff *skb); 887 void br_flood(struct net_bridge *br, struct sk_buff *skb, 888 enum br_pkt_type pkt_type, bool local_rcv, bool local_orig, 889 u16 vid); 890 891 /* return true if both source port and dest port are isolated */ 892 static inline bool br_skb_isolated(const struct net_bridge_port *to, 893 const struct sk_buff *skb) 894 { 895 return BR_INPUT_SKB_CB(skb)->src_port_isolated && 896 (to->flags & BR_ISOLATED); 897 } 898 899 /* br_if.c */ 900 void br_port_carrier_check(struct net_bridge_port *p, bool *notified); 901 int br_add_bridge(struct net *net, const char *name); 902 int br_del_bridge(struct net *net, const char *name); 903 int br_add_if(struct net_bridge *br, struct net_device *dev, 904 struct netlink_ext_ack *extack); 905 int br_del_if(struct net_bridge *br, struct net_device *dev); 906 void br_mtu_auto_adjust(struct net_bridge *br); 907 netdev_features_t br_features_recompute(struct net_bridge *br, 908 netdev_features_t features); 909 void br_port_flags_change(struct net_bridge_port *port, unsigned long mask); 910 void br_manage_promisc(struct net_bridge *br); 911 int nbp_backup_change(struct net_bridge_port *p, struct net_device *backup_dev); 912 913 /* br_input.c */ 914 int br_handle_frame_finish(struct net *net, struct sock *sk, struct sk_buff *skb); 915 rx_handler_func_t *br_get_rx_handler(const struct net_device *dev); 916 917 struct br_frame_type { 918 __be16 type; 919 int (*frame_handler)(struct net_bridge_port *port, 920 struct sk_buff *skb); 921 struct hlist_node list; 922 }; 923 924 void br_add_frame(struct net_bridge *br, struct br_frame_type *ft); 925 void br_del_frame(struct net_bridge *br, struct br_frame_type *ft); 926 927 static inline bool br_rx_handler_check_rcu(const struct net_device *dev) 928 { 929 return rcu_dereference(dev->rx_handler) == br_get_rx_handler(dev); 930 } 931 932 static inline bool br_rx_handler_check_rtnl(const struct net_device *dev) 933 { 934 return rcu_dereference_rtnl(dev->rx_handler) == br_get_rx_handler(dev); 935 } 936 937 static inline struct net_bridge_port *br_port_get_check_rcu(const struct net_device *dev) 938 { 939 return br_rx_handler_check_rcu(dev) ? br_port_get_rcu(dev) : NULL; 940 } 941 942 static inline struct net_bridge_port * 943 br_port_get_check_rtnl(const struct net_device *dev) 944 { 945 return br_rx_handler_check_rtnl(dev) ? br_port_get_rtnl_rcu(dev) : NULL; 946 } 947 948 /* br_ioctl.c */ 949 int br_dev_siocdevprivate(struct net_device *dev, struct ifreq *rq, 950 void __user *data, int cmd); 951 int br_ioctl_stub(struct net *net, struct net_bridge *br, unsigned int cmd, 952 struct ifreq *ifr, void __user *uarg); 953 954 /* br_multicast.c */ 955 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING 956 int br_multicast_rcv(struct net_bridge_mcast **brmctx, 957 struct net_bridge_mcast_port **pmctx, 958 struct net_bridge_vlan *vlan, 959 struct sk_buff *skb, u16 vid); 960 struct net_bridge_mdb_entry * 961 br_mdb_entry_skb_get(struct net_bridge_mcast *brmctx, struct sk_buff *skb, 962 u16 vid); 963 int br_multicast_add_port(struct net_bridge_port *port); 964 void br_multicast_del_port(struct net_bridge_port *port); 965 void br_multicast_enable_port(struct net_bridge_port *port); 966 void br_multicast_disable_port(struct net_bridge_port *port); 967 void br_multicast_init(struct net_bridge *br); 968 void br_multicast_join_snoopers(struct net_bridge *br); 969 void br_multicast_leave_snoopers(struct net_bridge *br); 970 void br_multicast_open(struct net_bridge *br); 971 void br_multicast_stop(struct net_bridge *br); 972 void br_multicast_dev_del(struct net_bridge *br); 973 void br_multicast_flood(struct net_bridge_mdb_entry *mdst, struct sk_buff *skb, 974 struct net_bridge_mcast *brmctx, 975 bool local_rcv, bool local_orig); 976 int br_multicast_set_router(struct net_bridge_mcast *brmctx, unsigned long val); 977 int br_multicast_set_port_router(struct net_bridge_mcast_port *pmctx, 978 unsigned long val); 979 int br_multicast_set_vlan_router(struct net_bridge_vlan *v, u8 mcast_router); 980 int br_multicast_toggle(struct net_bridge *br, unsigned long val, 981 struct netlink_ext_ack *extack); 982 int br_multicast_set_querier(struct net_bridge_mcast *brmctx, unsigned long val); 983 int br_multicast_set_igmp_version(struct net_bridge_mcast *brmctx, 984 unsigned long val); 985 #if IS_ENABLED(CONFIG_IPV6) 986 int br_multicast_set_mld_version(struct net_bridge_mcast *brmctx, 987 unsigned long val); 988 #endif 989 struct net_bridge_mdb_entry * 990 br_mdb_ip_get(struct net_bridge *br, struct br_ip *dst); 991 struct net_bridge_mdb_entry * 992 br_multicast_new_group(struct net_bridge *br, struct br_ip *group); 993 struct net_bridge_port_group * 994 br_multicast_new_port_group(struct net_bridge_port *port, 995 const struct br_ip *group, 996 struct net_bridge_port_group __rcu *next, 997 unsigned char flags, const unsigned char *src, 998 u8 filter_mode, u8 rt_protocol, 999 struct netlink_ext_ack *extack); 1000 void br_multicast_del_port_group(struct net_bridge_port_group *p); 1001 int br_mdb_hash_init(struct net_bridge *br); 1002 void br_mdb_hash_fini(struct net_bridge *br); 1003 void br_mdb_notify(struct net_device *dev, struct net_bridge_mdb_entry *mp, 1004 struct net_bridge_port_group *pg, int type); 1005 void br_rtr_notify(struct net_device *dev, struct net_bridge_mcast_port *pmctx, 1006 int type); 1007 void br_multicast_del_pg(struct net_bridge_mdb_entry *mp, 1008 struct net_bridge_port_group *pg, 1009 struct net_bridge_port_group __rcu **pp); 1010 void br_multicast_count(struct net_bridge *br, 1011 const struct net_bridge_port *p, 1012 const struct sk_buff *skb, u8 type, u8 dir); 1013 int br_multicast_init_stats(struct net_bridge *br); 1014 void br_multicast_uninit_stats(struct net_bridge *br); 1015 void br_multicast_get_stats(const struct net_bridge *br, 1016 const struct net_bridge_port *p, 1017 struct br_mcast_stats *dest); 1018 u32 br_multicast_ngroups_get(const struct net_bridge_mcast_port *pmctx); 1019 void br_multicast_ngroups_set_max(struct net_bridge_mcast_port *pmctx, u32 max); 1020 u32 br_multicast_ngroups_get_max(const struct net_bridge_mcast_port *pmctx); 1021 int br_mdb_add(struct net_device *dev, struct nlattr *tb[], u16 nlmsg_flags, 1022 struct netlink_ext_ack *extack); 1023 int br_mdb_del(struct net_device *dev, struct nlattr *tb[], 1024 struct netlink_ext_ack *extack); 1025 int br_mdb_del_bulk(struct net_device *dev, struct nlattr *tb[], 1026 struct netlink_ext_ack *extack); 1027 int br_mdb_dump(struct net_device *dev, struct sk_buff *skb, 1028 struct netlink_callback *cb); 1029 int br_mdb_get(struct net_device *dev, struct nlattr *tb[], u32 portid, u32 seq, 1030 struct netlink_ext_ack *extack); 1031 void br_multicast_host_join(const struct net_bridge_mcast *brmctx, 1032 struct net_bridge_mdb_entry *mp, bool notify); 1033 void br_multicast_host_leave(struct net_bridge_mdb_entry *mp, bool notify); 1034 void br_multicast_star_g_handle_mode(struct net_bridge_port_group *pg, 1035 u8 filter_mode); 1036 void br_multicast_sg_add_exclude_ports(struct net_bridge_mdb_entry *star_mp, 1037 struct net_bridge_port_group *sg); 1038 struct net_bridge_group_src * 1039 br_multicast_find_group_src(struct net_bridge_port_group *pg, struct br_ip *ip); 1040 struct net_bridge_group_src * 1041 br_multicast_new_group_src(struct net_bridge_port_group *pg, 1042 struct br_ip *src_ip); 1043 void __br_multicast_del_group_src(struct net_bridge_group_src *src); 1044 void br_multicast_del_group_src(struct net_bridge_group_src *src, 1045 bool fastleave); 1046 void br_multicast_ctx_init(struct net_bridge *br, 1047 struct net_bridge_vlan *vlan, 1048 struct net_bridge_mcast *brmctx); 1049 void br_multicast_ctx_deinit(struct net_bridge_mcast *brmctx); 1050 void br_multicast_port_ctx_init(struct net_bridge_port *port, 1051 struct net_bridge_vlan *vlan, 1052 struct net_bridge_mcast_port *pmctx); 1053 void br_multicast_port_ctx_deinit(struct net_bridge_mcast_port *pmctx); 1054 void br_multicast_toggle_one_vlan(struct net_bridge_vlan *vlan, bool on); 1055 int br_multicast_toggle_vlan_snooping(struct net_bridge *br, bool on, 1056 struct netlink_ext_ack *extack); 1057 bool br_multicast_toggle_global_vlan(struct net_bridge_vlan *vlan, bool on); 1058 1059 int br_rports_fill_info(struct sk_buff *skb, 1060 const struct net_bridge_mcast *brmctx); 1061 int br_multicast_dump_querier_state(struct sk_buff *skb, 1062 const struct net_bridge_mcast *brmctx, 1063 int nest_attr); 1064 size_t br_multicast_querier_state_size(void); 1065 size_t br_rports_size(const struct net_bridge_mcast *brmctx); 1066 void br_multicast_set_query_intvl(struct net_bridge_mcast *brmctx, 1067 unsigned long val); 1068 void br_multicast_set_startup_query_intvl(struct net_bridge_mcast *brmctx, 1069 unsigned long val); 1070 1071 static inline bool br_group_is_l2(const struct br_ip *group) 1072 { 1073 return group->proto == 0; 1074 } 1075 1076 #define mlock_dereference(X, br) \ 1077 rcu_dereference_protected(X, lockdep_is_held(&br->multicast_lock)) 1078 1079 static inline struct hlist_node * 1080 br_multicast_get_first_rport_node(struct net_bridge_mcast *brmctx, 1081 struct sk_buff *skb) 1082 { 1083 #if IS_ENABLED(CONFIG_IPV6) 1084 if (skb->protocol == htons(ETH_P_IPV6)) 1085 return rcu_dereference(hlist_first_rcu(&brmctx->ip6_mc_router_list)); 1086 #endif 1087 return rcu_dereference(hlist_first_rcu(&brmctx->ip4_mc_router_list)); 1088 } 1089 1090 static inline struct net_bridge_port * 1091 br_multicast_rport_from_node_skb(struct hlist_node *rp, struct sk_buff *skb) 1092 { 1093 struct net_bridge_mcast_port *mctx; 1094 1095 #if IS_ENABLED(CONFIG_IPV6) 1096 if (skb->protocol == htons(ETH_P_IPV6)) 1097 mctx = hlist_entry_safe(rp, struct net_bridge_mcast_port, 1098 ip6_rlist); 1099 else 1100 #endif 1101 mctx = hlist_entry_safe(rp, struct net_bridge_mcast_port, 1102 ip4_rlist); 1103 1104 if (mctx) 1105 return mctx->port; 1106 else 1107 return NULL; 1108 } 1109 1110 static inline bool br_ip4_multicast_is_router(struct net_bridge_mcast *brmctx) 1111 { 1112 return timer_pending(&brmctx->ip4_mc_router_timer); 1113 } 1114 1115 static inline bool br_ip6_multicast_is_router(struct net_bridge_mcast *brmctx) 1116 { 1117 #if IS_ENABLED(CONFIG_IPV6) 1118 return timer_pending(&brmctx->ip6_mc_router_timer); 1119 #else 1120 return false; 1121 #endif 1122 } 1123 1124 static inline bool 1125 br_multicast_is_router(struct net_bridge_mcast *brmctx, struct sk_buff *skb) 1126 { 1127 switch (brmctx->multicast_router) { 1128 case MDB_RTR_TYPE_PERM: 1129 return true; 1130 case MDB_RTR_TYPE_TEMP_QUERY: 1131 if (skb) { 1132 if (skb->protocol == htons(ETH_P_IP)) 1133 return br_ip4_multicast_is_router(brmctx); 1134 else if (skb->protocol == htons(ETH_P_IPV6)) 1135 return br_ip6_multicast_is_router(brmctx); 1136 } else { 1137 return br_ip4_multicast_is_router(brmctx) || 1138 br_ip6_multicast_is_router(brmctx); 1139 } 1140 fallthrough; 1141 default: 1142 return false; 1143 } 1144 } 1145 1146 static inline bool 1147 __br_multicast_querier_exists(struct net_bridge_mcast *brmctx, 1148 struct bridge_mcast_other_query *querier, 1149 const bool is_ipv6) 1150 { 1151 bool own_querier_enabled; 1152 1153 if (brmctx->multicast_querier) { 1154 if (is_ipv6 && !br_opt_get(brmctx->br, BROPT_HAS_IPV6_ADDR)) 1155 own_querier_enabled = false; 1156 else 1157 own_querier_enabled = true; 1158 } else { 1159 own_querier_enabled = false; 1160 } 1161 1162 return !timer_pending(&querier->delay_timer) && 1163 (own_querier_enabled || timer_pending(&querier->timer)); 1164 } 1165 1166 static inline bool br_multicast_querier_exists(struct net_bridge_mcast *brmctx, 1167 struct ethhdr *eth, 1168 const struct net_bridge_mdb_entry *mdb) 1169 { 1170 switch (eth->h_proto) { 1171 case (htons(ETH_P_IP)): 1172 return __br_multicast_querier_exists(brmctx, 1173 &brmctx->ip4_other_query, false); 1174 #if IS_ENABLED(CONFIG_IPV6) 1175 case (htons(ETH_P_IPV6)): 1176 return __br_multicast_querier_exists(brmctx, 1177 &brmctx->ip6_other_query, true); 1178 #endif 1179 default: 1180 return !!mdb && br_group_is_l2(&mdb->addr); 1181 } 1182 } 1183 1184 static inline bool br_multicast_is_star_g(const struct br_ip *ip) 1185 { 1186 switch (ip->proto) { 1187 case htons(ETH_P_IP): 1188 return ipv4_is_zeronet(ip->src.ip4); 1189 #if IS_ENABLED(CONFIG_IPV6) 1190 case htons(ETH_P_IPV6): 1191 return ipv6_addr_any(&ip->src.ip6); 1192 #endif 1193 default: 1194 return false; 1195 } 1196 } 1197 1198 static inline bool 1199 br_multicast_should_handle_mode(const struct net_bridge_mcast *brmctx, 1200 __be16 proto) 1201 { 1202 switch (proto) { 1203 case htons(ETH_P_IP): 1204 return !!(brmctx->multicast_igmp_version == 3); 1205 #if IS_ENABLED(CONFIG_IPV6) 1206 case htons(ETH_P_IPV6): 1207 return !!(brmctx->multicast_mld_version == 2); 1208 #endif 1209 default: 1210 return false; 1211 } 1212 } 1213 1214 static inline int br_multicast_igmp_type(const struct sk_buff *skb) 1215 { 1216 return BR_INPUT_SKB_CB(skb)->igmp; 1217 } 1218 1219 static inline unsigned long br_multicast_lmqt(const struct net_bridge_mcast *brmctx) 1220 { 1221 return brmctx->multicast_last_member_interval * 1222 brmctx->multicast_last_member_count; 1223 } 1224 1225 static inline unsigned long br_multicast_gmi(const struct net_bridge_mcast *brmctx) 1226 { 1227 return brmctx->multicast_membership_interval; 1228 } 1229 1230 static inline bool 1231 br_multicast_ctx_is_vlan(const struct net_bridge_mcast *brmctx) 1232 { 1233 return !!brmctx->vlan; 1234 } 1235 1236 static inline bool 1237 br_multicast_port_ctx_is_vlan(const struct net_bridge_mcast_port *pmctx) 1238 { 1239 return !!pmctx->vlan; 1240 } 1241 1242 static inline struct net_bridge_mcast * 1243 br_multicast_port_ctx_get_global(const struct net_bridge_mcast_port *pmctx) 1244 { 1245 if (!br_multicast_port_ctx_is_vlan(pmctx)) 1246 return &pmctx->port->br->multicast_ctx; 1247 else 1248 return &pmctx->vlan->brvlan->br_mcast_ctx; 1249 } 1250 1251 static inline bool 1252 br_multicast_ctx_vlan_global_disabled(const struct net_bridge_mcast *brmctx) 1253 { 1254 return br_multicast_ctx_is_vlan(brmctx) && 1255 (!br_opt_get(brmctx->br, BROPT_MCAST_VLAN_SNOOPING_ENABLED) || 1256 !(brmctx->vlan->priv_flags & BR_VLFLAG_GLOBAL_MCAST_ENABLED)); 1257 } 1258 1259 static inline bool 1260 br_multicast_ctx_vlan_disabled(const struct net_bridge_mcast *brmctx) 1261 { 1262 return br_multicast_ctx_is_vlan(brmctx) && 1263 !(brmctx->vlan->priv_flags & BR_VLFLAG_MCAST_ENABLED); 1264 } 1265 1266 static inline bool 1267 br_multicast_port_ctx_vlan_disabled(const struct net_bridge_mcast_port *pmctx) 1268 { 1269 return br_multicast_port_ctx_is_vlan(pmctx) && 1270 !(pmctx->vlan->priv_flags & BR_VLFLAG_MCAST_ENABLED); 1271 } 1272 1273 static inline bool 1274 br_multicast_port_ctx_state_disabled(const struct net_bridge_mcast_port *pmctx) 1275 { 1276 return pmctx->port->state == BR_STATE_DISABLED || 1277 (br_multicast_port_ctx_is_vlan(pmctx) && 1278 (br_multicast_port_ctx_vlan_disabled(pmctx) || 1279 pmctx->vlan->state == BR_STATE_DISABLED)); 1280 } 1281 1282 static inline bool 1283 br_multicast_port_ctx_state_stopped(const struct net_bridge_mcast_port *pmctx) 1284 { 1285 return br_multicast_port_ctx_state_disabled(pmctx) || 1286 pmctx->port->state == BR_STATE_BLOCKING || 1287 (br_multicast_port_ctx_is_vlan(pmctx) && 1288 pmctx->vlan->state == BR_STATE_BLOCKING); 1289 } 1290 1291 static inline bool 1292 br_rports_have_mc_router(const struct net_bridge_mcast *brmctx) 1293 { 1294 #if IS_ENABLED(CONFIG_IPV6) 1295 return !hlist_empty(&brmctx->ip4_mc_router_list) || 1296 !hlist_empty(&brmctx->ip6_mc_router_list); 1297 #else 1298 return !hlist_empty(&brmctx->ip4_mc_router_list); 1299 #endif 1300 } 1301 1302 static inline bool 1303 br_multicast_ctx_options_equal(const struct net_bridge_mcast *brmctx1, 1304 const struct net_bridge_mcast *brmctx2) 1305 { 1306 return brmctx1->multicast_igmp_version == 1307 brmctx2->multicast_igmp_version && 1308 brmctx1->multicast_last_member_count == 1309 brmctx2->multicast_last_member_count && 1310 brmctx1->multicast_startup_query_count == 1311 brmctx2->multicast_startup_query_count && 1312 brmctx1->multicast_last_member_interval == 1313 brmctx2->multicast_last_member_interval && 1314 brmctx1->multicast_membership_interval == 1315 brmctx2->multicast_membership_interval && 1316 brmctx1->multicast_querier_interval == 1317 brmctx2->multicast_querier_interval && 1318 brmctx1->multicast_query_interval == 1319 brmctx2->multicast_query_interval && 1320 brmctx1->multicast_query_response_interval == 1321 brmctx2->multicast_query_response_interval && 1322 brmctx1->multicast_startup_query_interval == 1323 brmctx2->multicast_startup_query_interval && 1324 brmctx1->multicast_querier == brmctx2->multicast_querier && 1325 brmctx1->multicast_router == brmctx2->multicast_router && 1326 !br_rports_have_mc_router(brmctx1) && 1327 !br_rports_have_mc_router(brmctx2) && 1328 #if IS_ENABLED(CONFIG_IPV6) 1329 brmctx1->multicast_mld_version == 1330 brmctx2->multicast_mld_version && 1331 #endif 1332 true; 1333 } 1334 1335 static inline bool 1336 br_multicast_ctx_matches_vlan_snooping(const struct net_bridge_mcast *brmctx) 1337 { 1338 bool vlan_snooping_enabled; 1339 1340 vlan_snooping_enabled = !!br_opt_get(brmctx->br, 1341 BROPT_MCAST_VLAN_SNOOPING_ENABLED); 1342 1343 return !!(vlan_snooping_enabled == br_multicast_ctx_is_vlan(brmctx)); 1344 } 1345 #else 1346 static inline int br_multicast_rcv(struct net_bridge_mcast **brmctx, 1347 struct net_bridge_mcast_port **pmctx, 1348 struct net_bridge_vlan *vlan, 1349 struct sk_buff *skb, 1350 u16 vid) 1351 { 1352 return 0; 1353 } 1354 1355 static inline struct net_bridge_mdb_entry * 1356 br_mdb_entry_skb_get(struct net_bridge_mcast *brmctx, struct sk_buff *skb, 1357 u16 vid) 1358 { 1359 return NULL; 1360 } 1361 1362 static inline int br_multicast_add_port(struct net_bridge_port *port) 1363 { 1364 return 0; 1365 } 1366 1367 static inline void br_multicast_del_port(struct net_bridge_port *port) 1368 { 1369 } 1370 1371 static inline void br_multicast_enable_port(struct net_bridge_port *port) 1372 { 1373 } 1374 1375 static inline void br_multicast_disable_port(struct net_bridge_port *port) 1376 { 1377 } 1378 1379 static inline void br_multicast_init(struct net_bridge *br) 1380 { 1381 } 1382 1383 static inline void br_multicast_join_snoopers(struct net_bridge *br) 1384 { 1385 } 1386 1387 static inline void br_multicast_leave_snoopers(struct net_bridge *br) 1388 { 1389 } 1390 1391 static inline void br_multicast_open(struct net_bridge *br) 1392 { 1393 } 1394 1395 static inline void br_multicast_stop(struct net_bridge *br) 1396 { 1397 } 1398 1399 static inline void br_multicast_dev_del(struct net_bridge *br) 1400 { 1401 } 1402 1403 static inline void br_multicast_flood(struct net_bridge_mdb_entry *mdst, 1404 struct sk_buff *skb, 1405 struct net_bridge_mcast *brmctx, 1406 bool local_rcv, bool local_orig) 1407 { 1408 } 1409 1410 static inline bool br_multicast_is_router(struct net_bridge_mcast *brmctx, 1411 struct sk_buff *skb) 1412 { 1413 return false; 1414 } 1415 1416 static inline bool br_multicast_querier_exists(struct net_bridge_mcast *brmctx, 1417 struct ethhdr *eth, 1418 const struct net_bridge_mdb_entry *mdb) 1419 { 1420 return false; 1421 } 1422 1423 static inline int br_mdb_add(struct net_device *dev, struct nlattr *tb[], 1424 u16 nlmsg_flags, struct netlink_ext_ack *extack) 1425 { 1426 return -EOPNOTSUPP; 1427 } 1428 1429 static inline int br_mdb_del(struct net_device *dev, struct nlattr *tb[], 1430 struct netlink_ext_ack *extack) 1431 { 1432 return -EOPNOTSUPP; 1433 } 1434 1435 static inline int br_mdb_del_bulk(struct net_device *dev, struct nlattr *tb[], 1436 struct netlink_ext_ack *extack) 1437 { 1438 return -EOPNOTSUPP; 1439 } 1440 1441 static inline int br_mdb_dump(struct net_device *dev, struct sk_buff *skb, 1442 struct netlink_callback *cb) 1443 { 1444 return 0; 1445 } 1446 1447 static inline int br_mdb_get(struct net_device *dev, struct nlattr *tb[], 1448 u32 portid, u32 seq, 1449 struct netlink_ext_ack *extack) 1450 { 1451 return -EOPNOTSUPP; 1452 } 1453 1454 static inline int br_mdb_hash_init(struct net_bridge *br) 1455 { 1456 return 0; 1457 } 1458 1459 static inline void br_mdb_hash_fini(struct net_bridge *br) 1460 { 1461 } 1462 1463 static inline void br_multicast_count(struct net_bridge *br, 1464 const struct net_bridge_port *p, 1465 const struct sk_buff *skb, 1466 u8 type, u8 dir) 1467 { 1468 } 1469 1470 static inline int br_multicast_init_stats(struct net_bridge *br) 1471 { 1472 return 0; 1473 } 1474 1475 static inline void br_multicast_uninit_stats(struct net_bridge *br) 1476 { 1477 } 1478 1479 static inline int br_multicast_igmp_type(const struct sk_buff *skb) 1480 { 1481 return 0; 1482 } 1483 1484 static inline void br_multicast_ctx_init(struct net_bridge *br, 1485 struct net_bridge_vlan *vlan, 1486 struct net_bridge_mcast *brmctx) 1487 { 1488 } 1489 1490 static inline void br_multicast_ctx_deinit(struct net_bridge_mcast *brmctx) 1491 { 1492 } 1493 1494 static inline void br_multicast_port_ctx_init(struct net_bridge_port *port, 1495 struct net_bridge_vlan *vlan, 1496 struct net_bridge_mcast_port *pmctx) 1497 { 1498 } 1499 1500 static inline void br_multicast_port_ctx_deinit(struct net_bridge_mcast_port *pmctx) 1501 { 1502 } 1503 1504 static inline void br_multicast_toggle_one_vlan(struct net_bridge_vlan *vlan, 1505 bool on) 1506 { 1507 } 1508 1509 static inline int br_multicast_toggle_vlan_snooping(struct net_bridge *br, 1510 bool on, 1511 struct netlink_ext_ack *extack) 1512 { 1513 return -EOPNOTSUPP; 1514 } 1515 1516 static inline bool br_multicast_toggle_global_vlan(struct net_bridge_vlan *vlan, 1517 bool on) 1518 { 1519 return false; 1520 } 1521 1522 static inline bool 1523 br_multicast_ctx_options_equal(const struct net_bridge_mcast *brmctx1, 1524 const struct net_bridge_mcast *brmctx2) 1525 { 1526 return true; 1527 } 1528 #endif 1529 1530 /* br_vlan.c */ 1531 #ifdef CONFIG_BRIDGE_VLAN_FILTERING 1532 bool br_allowed_ingress(const struct net_bridge *br, 1533 struct net_bridge_vlan_group *vg, struct sk_buff *skb, 1534 u16 *vid, u8 *state, 1535 struct net_bridge_vlan **vlan); 1536 bool br_allowed_egress(struct net_bridge_vlan_group *vg, 1537 const struct sk_buff *skb); 1538 bool br_should_learn(struct net_bridge_port *p, struct sk_buff *skb, u16 *vid); 1539 struct sk_buff *br_handle_vlan(struct net_bridge *br, 1540 const struct net_bridge_port *port, 1541 struct net_bridge_vlan_group *vg, 1542 struct sk_buff *skb); 1543 int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags, 1544 bool *changed, struct netlink_ext_ack *extack); 1545 int br_vlan_delete(struct net_bridge *br, u16 vid); 1546 void br_vlan_flush(struct net_bridge *br); 1547 struct net_bridge_vlan *br_vlan_find(struct net_bridge_vlan_group *vg, u16 vid); 1548 void br_recalculate_fwd_mask(struct net_bridge *br); 1549 int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val, 1550 struct netlink_ext_ack *extack); 1551 int __br_vlan_set_proto(struct net_bridge *br, __be16 proto, 1552 struct netlink_ext_ack *extack); 1553 int br_vlan_set_proto(struct net_bridge *br, unsigned long val, 1554 struct netlink_ext_ack *extack); 1555 int br_vlan_set_stats(struct net_bridge *br, unsigned long val); 1556 int br_vlan_set_stats_per_port(struct net_bridge *br, unsigned long val); 1557 int br_vlan_init(struct net_bridge *br); 1558 int br_vlan_set_default_pvid(struct net_bridge *br, unsigned long val, 1559 struct netlink_ext_ack *extack); 1560 int __br_vlan_set_default_pvid(struct net_bridge *br, u16 pvid, 1561 struct netlink_ext_ack *extack); 1562 int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags, 1563 bool *changed, struct netlink_ext_ack *extack); 1564 int nbp_vlan_delete(struct net_bridge_port *port, u16 vid); 1565 void nbp_vlan_flush(struct net_bridge_port *port); 1566 int nbp_vlan_init(struct net_bridge_port *port, struct netlink_ext_ack *extack); 1567 int nbp_get_num_vlan_infos(struct net_bridge_port *p, u32 filter_mask); 1568 void br_vlan_get_stats(const struct net_bridge_vlan *v, 1569 struct pcpu_sw_netstats *stats); 1570 void br_vlan_port_event(struct net_bridge_port *p, unsigned long event); 1571 int br_vlan_bridge_event(struct net_device *dev, unsigned long event, 1572 void *ptr); 1573 void br_vlan_rtnl_init(void); 1574 void br_vlan_rtnl_uninit(void); 1575 void br_vlan_notify(const struct net_bridge *br, 1576 const struct net_bridge_port *p, 1577 u16 vid, u16 vid_range, 1578 int cmd); 1579 bool br_vlan_can_enter_range(const struct net_bridge_vlan *v_curr, 1580 const struct net_bridge_vlan *range_end); 1581 1582 void br_vlan_fill_forward_path_pvid(struct net_bridge *br, 1583 struct net_device_path_ctx *ctx, 1584 struct net_device_path *path); 1585 int br_vlan_fill_forward_path_mode(struct net_bridge *br, 1586 struct net_bridge_port *dst, 1587 struct net_device_path *path); 1588 1589 static inline struct net_bridge_vlan_group *br_vlan_group( 1590 const struct net_bridge *br) 1591 { 1592 return rtnl_dereference(br->vlgrp); 1593 } 1594 1595 static inline struct net_bridge_vlan_group *nbp_vlan_group( 1596 const struct net_bridge_port *p) 1597 { 1598 return rtnl_dereference(p->vlgrp); 1599 } 1600 1601 static inline struct net_bridge_vlan_group *br_vlan_group_rcu( 1602 const struct net_bridge *br) 1603 { 1604 return rcu_dereference(br->vlgrp); 1605 } 1606 1607 static inline struct net_bridge_vlan_group *nbp_vlan_group_rcu( 1608 const struct net_bridge_port *p) 1609 { 1610 return rcu_dereference(p->vlgrp); 1611 } 1612 1613 /* Since bridge now depends on 8021Q module, but the time bridge sees the 1614 * skb, the vlan tag will always be present if the frame was tagged. 1615 */ 1616 static inline int br_vlan_get_tag(const struct sk_buff *skb, u16 *vid) 1617 { 1618 int err = 0; 1619 1620 if (skb_vlan_tag_present(skb)) { 1621 *vid = skb_vlan_tag_get_id(skb); 1622 } else { 1623 *vid = 0; 1624 err = -EINVAL; 1625 } 1626 1627 return err; 1628 } 1629 1630 static inline u16 br_get_pvid(const struct net_bridge_vlan_group *vg) 1631 { 1632 if (!vg) 1633 return 0; 1634 1635 smp_rmb(); 1636 return vg->pvid; 1637 } 1638 1639 static inline u16 br_vlan_flags(const struct net_bridge_vlan *v, u16 pvid) 1640 { 1641 return v->vid == pvid ? v->flags | BRIDGE_VLAN_INFO_PVID : v->flags; 1642 } 1643 #else 1644 static inline bool br_allowed_ingress(const struct net_bridge *br, 1645 struct net_bridge_vlan_group *vg, 1646 struct sk_buff *skb, 1647 u16 *vid, u8 *state, 1648 struct net_bridge_vlan **vlan) 1649 1650 { 1651 *vlan = NULL; 1652 return true; 1653 } 1654 1655 static inline bool br_allowed_egress(struct net_bridge_vlan_group *vg, 1656 const struct sk_buff *skb) 1657 { 1658 return true; 1659 } 1660 1661 static inline bool br_should_learn(struct net_bridge_port *p, 1662 struct sk_buff *skb, u16 *vid) 1663 { 1664 return true; 1665 } 1666 1667 static inline struct sk_buff *br_handle_vlan(struct net_bridge *br, 1668 const struct net_bridge_port *port, 1669 struct net_bridge_vlan_group *vg, 1670 struct sk_buff *skb) 1671 { 1672 return skb; 1673 } 1674 1675 static inline int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags, 1676 bool *changed, struct netlink_ext_ack *extack) 1677 { 1678 *changed = false; 1679 return -EOPNOTSUPP; 1680 } 1681 1682 static inline int br_vlan_delete(struct net_bridge *br, u16 vid) 1683 { 1684 return -EOPNOTSUPP; 1685 } 1686 1687 static inline void br_vlan_flush(struct net_bridge *br) 1688 { 1689 } 1690 1691 static inline void br_recalculate_fwd_mask(struct net_bridge *br) 1692 { 1693 } 1694 1695 static inline int br_vlan_init(struct net_bridge *br) 1696 { 1697 return 0; 1698 } 1699 1700 static inline int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags, 1701 bool *changed, struct netlink_ext_ack *extack) 1702 { 1703 *changed = false; 1704 return -EOPNOTSUPP; 1705 } 1706 1707 static inline int nbp_vlan_delete(struct net_bridge_port *port, u16 vid) 1708 { 1709 return -EOPNOTSUPP; 1710 } 1711 1712 static inline void nbp_vlan_flush(struct net_bridge_port *port) 1713 { 1714 } 1715 1716 static inline struct net_bridge_vlan *br_vlan_find(struct net_bridge_vlan_group *vg, 1717 u16 vid) 1718 { 1719 return NULL; 1720 } 1721 1722 static inline int nbp_vlan_init(struct net_bridge_port *port, 1723 struct netlink_ext_ack *extack) 1724 { 1725 return 0; 1726 } 1727 1728 static inline u16 br_vlan_get_tag(const struct sk_buff *skb, u16 *tag) 1729 { 1730 return 0; 1731 } 1732 1733 static inline u16 br_get_pvid(const struct net_bridge_vlan_group *vg) 1734 { 1735 return 0; 1736 } 1737 1738 static inline int br_vlan_filter_toggle(struct net_bridge *br, 1739 unsigned long val, 1740 struct netlink_ext_ack *extack) 1741 { 1742 return -EOPNOTSUPP; 1743 } 1744 1745 static inline int nbp_get_num_vlan_infos(struct net_bridge_port *p, 1746 u32 filter_mask) 1747 { 1748 return 0; 1749 } 1750 1751 static inline void br_vlan_fill_forward_path_pvid(struct net_bridge *br, 1752 struct net_device_path_ctx *ctx, 1753 struct net_device_path *path) 1754 { 1755 } 1756 1757 static inline int br_vlan_fill_forward_path_mode(struct net_bridge *br, 1758 struct net_bridge_port *dst, 1759 struct net_device_path *path) 1760 { 1761 return 0; 1762 } 1763 1764 static inline struct net_bridge_vlan_group *br_vlan_group( 1765 const struct net_bridge *br) 1766 { 1767 return NULL; 1768 } 1769 1770 static inline struct net_bridge_vlan_group *nbp_vlan_group( 1771 const struct net_bridge_port *p) 1772 { 1773 return NULL; 1774 } 1775 1776 static inline struct net_bridge_vlan_group *br_vlan_group_rcu( 1777 const struct net_bridge *br) 1778 { 1779 return NULL; 1780 } 1781 1782 static inline struct net_bridge_vlan_group *nbp_vlan_group_rcu( 1783 const struct net_bridge_port *p) 1784 { 1785 return NULL; 1786 } 1787 1788 static inline void br_vlan_get_stats(const struct net_bridge_vlan *v, 1789 struct pcpu_sw_netstats *stats) 1790 { 1791 } 1792 1793 static inline void br_vlan_port_event(struct net_bridge_port *p, 1794 unsigned long event) 1795 { 1796 } 1797 1798 static inline int br_vlan_bridge_event(struct net_device *dev, 1799 unsigned long event, void *ptr) 1800 { 1801 return 0; 1802 } 1803 1804 static inline void br_vlan_rtnl_init(void) 1805 { 1806 } 1807 1808 static inline void br_vlan_rtnl_uninit(void) 1809 { 1810 } 1811 1812 static inline void br_vlan_notify(const struct net_bridge *br, 1813 const struct net_bridge_port *p, 1814 u16 vid, u16 vid_range, 1815 int cmd) 1816 { 1817 } 1818 1819 static inline bool br_vlan_can_enter_range(const struct net_bridge_vlan *v_curr, 1820 const struct net_bridge_vlan *range_end) 1821 { 1822 return true; 1823 } 1824 1825 static inline u16 br_vlan_flags(const struct net_bridge_vlan *v, u16 pvid) 1826 { 1827 return 0; 1828 } 1829 1830 #endif 1831 1832 /* br_vlan_options.c */ 1833 #ifdef CONFIG_BRIDGE_VLAN_FILTERING 1834 bool br_vlan_opts_eq_range(const struct net_bridge_vlan *v_curr, 1835 const struct net_bridge_vlan *range_end); 1836 bool br_vlan_opts_fill(struct sk_buff *skb, const struct net_bridge_vlan *v, 1837 const struct net_bridge_port *p); 1838 size_t br_vlan_opts_nl_size(void); 1839 int br_vlan_process_options(const struct net_bridge *br, 1840 const struct net_bridge_port *p, 1841 struct net_bridge_vlan *range_start, 1842 struct net_bridge_vlan *range_end, 1843 struct nlattr **tb, 1844 struct netlink_ext_ack *extack); 1845 int br_vlan_rtm_process_global_options(struct net_device *dev, 1846 const struct nlattr *attr, 1847 int cmd, 1848 struct netlink_ext_ack *extack); 1849 bool br_vlan_global_opts_can_enter_range(const struct net_bridge_vlan *v_curr, 1850 const struct net_bridge_vlan *r_end); 1851 bool br_vlan_global_opts_fill(struct sk_buff *skb, u16 vid, u16 vid_range, 1852 const struct net_bridge_vlan *v_opts); 1853 1854 /* vlan state manipulation helpers using *_ONCE to annotate lock-free access */ 1855 static inline u8 br_vlan_get_state(const struct net_bridge_vlan *v) 1856 { 1857 return READ_ONCE(v->state); 1858 } 1859 1860 static inline void br_vlan_set_state(struct net_bridge_vlan *v, u8 state) 1861 { 1862 WRITE_ONCE(v->state, state); 1863 } 1864 1865 static inline u8 br_vlan_get_pvid_state(const struct net_bridge_vlan_group *vg) 1866 { 1867 return READ_ONCE(vg->pvid_state); 1868 } 1869 1870 static inline void br_vlan_set_pvid_state(struct net_bridge_vlan_group *vg, 1871 u8 state) 1872 { 1873 WRITE_ONCE(vg->pvid_state, state); 1874 } 1875 1876 /* learn_allow is true at ingress and false at egress */ 1877 static inline bool br_vlan_state_allowed(u8 state, bool learn_allow) 1878 { 1879 switch (state) { 1880 case BR_STATE_LEARNING: 1881 return learn_allow; 1882 case BR_STATE_FORWARDING: 1883 return true; 1884 default: 1885 return false; 1886 } 1887 } 1888 #endif 1889 1890 /* br_mst.c */ 1891 #ifdef CONFIG_BRIDGE_VLAN_FILTERING 1892 DECLARE_STATIC_KEY_FALSE(br_mst_used); 1893 static inline bool br_mst_is_enabled(struct net_bridge *br) 1894 { 1895 return static_branch_unlikely(&br_mst_used) && 1896 br_opt_get(br, BROPT_MST_ENABLED); 1897 } 1898 1899 int br_mst_set_state(struct net_bridge_port *p, u16 msti, u8 state, 1900 struct netlink_ext_ack *extack); 1901 int br_mst_vlan_set_msti(struct net_bridge_vlan *v, u16 msti); 1902 void br_mst_vlan_init_state(struct net_bridge_vlan *v); 1903 int br_mst_set_enabled(struct net_bridge *br, bool on, 1904 struct netlink_ext_ack *extack); 1905 size_t br_mst_info_size(const struct net_bridge_vlan_group *vg); 1906 int br_mst_fill_info(struct sk_buff *skb, 1907 const struct net_bridge_vlan_group *vg); 1908 int br_mst_process(struct net_bridge_port *p, const struct nlattr *mst_attr, 1909 struct netlink_ext_ack *extack); 1910 #else 1911 static inline bool br_mst_is_enabled(struct net_bridge *br) 1912 { 1913 return false; 1914 } 1915 1916 static inline int br_mst_set_state(struct net_bridge_port *p, u16 msti, 1917 u8 state, struct netlink_ext_ack *extack) 1918 { 1919 return -EOPNOTSUPP; 1920 } 1921 1922 static inline int br_mst_set_enabled(struct net_bridge *br, bool on, 1923 struct netlink_ext_ack *extack) 1924 { 1925 return -EOPNOTSUPP; 1926 } 1927 1928 static inline size_t br_mst_info_size(const struct net_bridge_vlan_group *vg) 1929 { 1930 return 0; 1931 } 1932 1933 static inline int br_mst_fill_info(struct sk_buff *skb, 1934 const struct net_bridge_vlan_group *vg) 1935 { 1936 return -EOPNOTSUPP; 1937 } 1938 1939 static inline int br_mst_process(struct net_bridge_port *p, 1940 const struct nlattr *mst_attr, 1941 struct netlink_ext_ack *extack) 1942 { 1943 return -EOPNOTSUPP; 1944 } 1945 #endif 1946 1947 struct nf_br_ops { 1948 int (*br_dev_xmit_hook)(struct sk_buff *skb); 1949 }; 1950 extern const struct nf_br_ops __rcu *nf_br_ops; 1951 1952 /* br_netfilter.c */ 1953 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 1954 int br_nf_core_init(void); 1955 void br_nf_core_fini(void); 1956 void br_netfilter_rtable_init(struct net_bridge *); 1957 #else 1958 static inline int br_nf_core_init(void) { return 0; } 1959 static inline void br_nf_core_fini(void) {} 1960 #define br_netfilter_rtable_init(x) 1961 #endif 1962 1963 /* br_stp.c */ 1964 void br_set_state(struct net_bridge_port *p, unsigned int state); 1965 struct net_bridge_port *br_get_port(struct net_bridge *br, u16 port_no); 1966 void br_init_port(struct net_bridge_port *p); 1967 void br_become_designated_port(struct net_bridge_port *p); 1968 1969 void __br_set_forward_delay(struct net_bridge *br, unsigned long t); 1970 int br_set_forward_delay(struct net_bridge *br, unsigned long x); 1971 int br_set_hello_time(struct net_bridge *br, unsigned long x); 1972 int br_set_max_age(struct net_bridge *br, unsigned long x); 1973 int __set_ageing_time(struct net_device *dev, unsigned long t); 1974 int br_set_ageing_time(struct net_bridge *br, clock_t ageing_time); 1975 1976 1977 /* br_stp_if.c */ 1978 void br_stp_enable_bridge(struct net_bridge *br); 1979 void br_stp_disable_bridge(struct net_bridge *br); 1980 int br_stp_set_enabled(struct net_bridge *br, unsigned long val, 1981 struct netlink_ext_ack *extack); 1982 void br_stp_enable_port(struct net_bridge_port *p); 1983 void br_stp_disable_port(struct net_bridge_port *p); 1984 bool br_stp_recalculate_bridge_id(struct net_bridge *br); 1985 void br_stp_change_bridge_id(struct net_bridge *br, const unsigned char *a); 1986 void br_stp_set_bridge_priority(struct net_bridge *br, u16 newprio); 1987 int br_stp_set_port_priority(struct net_bridge_port *p, unsigned long newprio); 1988 int br_stp_set_path_cost(struct net_bridge_port *p, unsigned long path_cost); 1989 ssize_t br_show_bridge_id(char *buf, const struct bridge_id *id); 1990 1991 /* br_stp_bpdu.c */ 1992 struct stp_proto; 1993 void br_stp_rcv(const struct stp_proto *proto, struct sk_buff *skb, 1994 struct net_device *dev); 1995 1996 /* br_stp_timer.c */ 1997 void br_stp_timer_init(struct net_bridge *br); 1998 void br_stp_port_timer_init(struct net_bridge_port *p); 1999 unsigned long br_timer_value(const struct timer_list *timer); 2000 2001 /* br.c */ 2002 #if IS_ENABLED(CONFIG_ATM_LANE) 2003 extern int (*br_fdb_test_addr_hook)(struct net_device *dev, unsigned char *addr); 2004 #endif 2005 2006 /* br_mrp.c */ 2007 #if IS_ENABLED(CONFIG_BRIDGE_MRP) 2008 int br_mrp_parse(struct net_bridge *br, struct net_bridge_port *p, 2009 struct nlattr *attr, int cmd, struct netlink_ext_ack *extack); 2010 bool br_mrp_enabled(struct net_bridge *br); 2011 void br_mrp_port_del(struct net_bridge *br, struct net_bridge_port *p); 2012 int br_mrp_fill_info(struct sk_buff *skb, struct net_bridge *br); 2013 #else 2014 static inline int br_mrp_parse(struct net_bridge *br, struct net_bridge_port *p, 2015 struct nlattr *attr, int cmd, 2016 struct netlink_ext_ack *extack) 2017 { 2018 return -EOPNOTSUPP; 2019 } 2020 2021 static inline bool br_mrp_enabled(struct net_bridge *br) 2022 { 2023 return false; 2024 } 2025 2026 static inline void br_mrp_port_del(struct net_bridge *br, 2027 struct net_bridge_port *p) 2028 { 2029 } 2030 2031 static inline int br_mrp_fill_info(struct sk_buff *skb, struct net_bridge *br) 2032 { 2033 return 0; 2034 } 2035 2036 #endif 2037 2038 /* br_cfm.c */ 2039 #if IS_ENABLED(CONFIG_BRIDGE_CFM) 2040 int br_cfm_parse(struct net_bridge *br, struct net_bridge_port *p, 2041 struct nlattr *attr, int cmd, struct netlink_ext_ack *extack); 2042 bool br_cfm_created(struct net_bridge *br); 2043 void br_cfm_port_del(struct net_bridge *br, struct net_bridge_port *p); 2044 int br_cfm_config_fill_info(struct sk_buff *skb, struct net_bridge *br); 2045 int br_cfm_status_fill_info(struct sk_buff *skb, 2046 struct net_bridge *br, 2047 bool getlink); 2048 int br_cfm_mep_count(struct net_bridge *br, u32 *count); 2049 int br_cfm_peer_mep_count(struct net_bridge *br, u32 *count); 2050 #else 2051 static inline int br_cfm_parse(struct net_bridge *br, struct net_bridge_port *p, 2052 struct nlattr *attr, int cmd, 2053 struct netlink_ext_ack *extack) 2054 { 2055 return -EOPNOTSUPP; 2056 } 2057 2058 static inline bool br_cfm_created(struct net_bridge *br) 2059 { 2060 return false; 2061 } 2062 2063 static inline void br_cfm_port_del(struct net_bridge *br, 2064 struct net_bridge_port *p) 2065 { 2066 } 2067 2068 static inline int br_cfm_config_fill_info(struct sk_buff *skb, struct net_bridge *br) 2069 { 2070 return -EOPNOTSUPP; 2071 } 2072 2073 static inline int br_cfm_status_fill_info(struct sk_buff *skb, 2074 struct net_bridge *br, 2075 bool getlink) 2076 { 2077 return -EOPNOTSUPP; 2078 } 2079 2080 static inline int br_cfm_mep_count(struct net_bridge *br, u32 *count) 2081 { 2082 *count = 0; 2083 return -EOPNOTSUPP; 2084 } 2085 2086 static inline int br_cfm_peer_mep_count(struct net_bridge *br, u32 *count) 2087 { 2088 *count = 0; 2089 return -EOPNOTSUPP; 2090 } 2091 #endif 2092 2093 /* br_netlink.c */ 2094 extern struct rtnl_link_ops br_link_ops; 2095 int br_netlink_init(void); 2096 void br_netlink_fini(void); 2097 void br_ifinfo_notify(int event, const struct net_bridge *br, 2098 const struct net_bridge_port *port); 2099 void br_info_notify(int event, const struct net_bridge *br, 2100 const struct net_bridge_port *port, u32 filter); 2101 int br_setlink(struct net_device *dev, struct nlmsghdr *nlmsg, u16 flags, 2102 struct netlink_ext_ack *extack); 2103 int br_dellink(struct net_device *dev, struct nlmsghdr *nlmsg, u16 flags); 2104 int br_getlink(struct sk_buff *skb, u32 pid, u32 seq, struct net_device *dev, 2105 u32 filter_mask, int nlflags); 2106 int br_process_vlan_info(struct net_bridge *br, 2107 struct net_bridge_port *p, int cmd, 2108 struct bridge_vlan_info *vinfo_curr, 2109 struct bridge_vlan_info **vinfo_last, 2110 bool *changed, 2111 struct netlink_ext_ack *extack); 2112 2113 #ifdef CONFIG_SYSFS 2114 /* br_sysfs_if.c */ 2115 extern const struct sysfs_ops brport_sysfs_ops; 2116 int br_sysfs_addif(struct net_bridge_port *p); 2117 int br_sysfs_renameif(struct net_bridge_port *p); 2118 2119 /* br_sysfs_br.c */ 2120 int br_sysfs_addbr(struct net_device *dev); 2121 void br_sysfs_delbr(struct net_device *dev); 2122 2123 #else 2124 2125 static inline int br_sysfs_addif(struct net_bridge_port *p) { return 0; } 2126 static inline int br_sysfs_renameif(struct net_bridge_port *p) { return 0; } 2127 static inline int br_sysfs_addbr(struct net_device *dev) { return 0; } 2128 static inline void br_sysfs_delbr(struct net_device *dev) { return; } 2129 #endif /* CONFIG_SYSFS */ 2130 2131 /* br_switchdev.c */ 2132 #ifdef CONFIG_NET_SWITCHDEV 2133 int br_switchdev_port_offload(struct net_bridge_port *p, 2134 struct net_device *dev, const void *ctx, 2135 struct notifier_block *atomic_nb, 2136 struct notifier_block *blocking_nb, 2137 bool tx_fwd_offload, 2138 struct netlink_ext_ack *extack); 2139 2140 void br_switchdev_port_unoffload(struct net_bridge_port *p, const void *ctx, 2141 struct notifier_block *atomic_nb, 2142 struct notifier_block *blocking_nb); 2143 2144 int br_switchdev_port_replay(struct net_bridge_port *p, 2145 struct net_device *dev, const void *ctx, 2146 struct notifier_block *atomic_nb, 2147 struct notifier_block *blocking_nb, 2148 struct netlink_ext_ack *extack); 2149 2150 bool br_switchdev_frame_uses_tx_fwd_offload(struct sk_buff *skb); 2151 2152 void br_switchdev_frame_set_offload_fwd_mark(struct sk_buff *skb); 2153 2154 void nbp_switchdev_frame_mark_tx_fwd_offload(const struct net_bridge_port *p, 2155 struct sk_buff *skb); 2156 void nbp_switchdev_frame_mark_tx_fwd_to_hwdom(const struct net_bridge_port *p, 2157 struct sk_buff *skb); 2158 void nbp_switchdev_frame_mark(const struct net_bridge_port *p, 2159 struct sk_buff *skb); 2160 bool nbp_switchdev_allowed_egress(const struct net_bridge_port *p, 2161 const struct sk_buff *skb); 2162 int br_switchdev_set_port_flag(struct net_bridge_port *p, 2163 unsigned long flags, 2164 unsigned long mask, 2165 struct netlink_ext_ack *extack); 2166 void br_switchdev_fdb_notify(struct net_bridge *br, 2167 const struct net_bridge_fdb_entry *fdb, int type); 2168 void br_switchdev_mdb_notify(struct net_device *dev, 2169 struct net_bridge_mdb_entry *mp, 2170 struct net_bridge_port_group *pg, 2171 int type); 2172 int br_switchdev_port_vlan_add(struct net_device *dev, u16 vid, u16 flags, 2173 bool changed, struct netlink_ext_ack *extack); 2174 int br_switchdev_port_vlan_del(struct net_device *dev, u16 vid); 2175 void br_switchdev_init(struct net_bridge *br); 2176 2177 static inline void br_switchdev_frame_unmark(struct sk_buff *skb) 2178 { 2179 skb->offload_fwd_mark = 0; 2180 } 2181 #else 2182 static inline int 2183 br_switchdev_port_offload(struct net_bridge_port *p, 2184 struct net_device *dev, const void *ctx, 2185 struct notifier_block *atomic_nb, 2186 struct notifier_block *blocking_nb, 2187 bool tx_fwd_offload, 2188 struct netlink_ext_ack *extack) 2189 { 2190 return -EOPNOTSUPP; 2191 } 2192 2193 static inline void 2194 br_switchdev_port_unoffload(struct net_bridge_port *p, const void *ctx, 2195 struct notifier_block *atomic_nb, 2196 struct notifier_block *blocking_nb) 2197 { 2198 } 2199 2200 static inline int 2201 br_switchdev_port_replay(struct net_bridge_port *p, 2202 struct net_device *dev, const void *ctx, 2203 struct notifier_block *atomic_nb, 2204 struct notifier_block *blocking_nb, 2205 struct netlink_ext_ack *extack) 2206 { 2207 return -EOPNOTSUPP; 2208 } 2209 2210 static inline bool br_switchdev_frame_uses_tx_fwd_offload(struct sk_buff *skb) 2211 { 2212 return false; 2213 } 2214 2215 static inline void br_switchdev_frame_set_offload_fwd_mark(struct sk_buff *skb) 2216 { 2217 } 2218 2219 static inline void 2220 nbp_switchdev_frame_mark_tx_fwd_offload(const struct net_bridge_port *p, 2221 struct sk_buff *skb) 2222 { 2223 } 2224 2225 static inline void 2226 nbp_switchdev_frame_mark_tx_fwd_to_hwdom(const struct net_bridge_port *p, 2227 struct sk_buff *skb) 2228 { 2229 } 2230 2231 static inline void nbp_switchdev_frame_mark(const struct net_bridge_port *p, 2232 struct sk_buff *skb) 2233 { 2234 } 2235 2236 static inline bool nbp_switchdev_allowed_egress(const struct net_bridge_port *p, 2237 const struct sk_buff *skb) 2238 { 2239 return true; 2240 } 2241 2242 static inline int br_switchdev_set_port_flag(struct net_bridge_port *p, 2243 unsigned long flags, 2244 unsigned long mask, 2245 struct netlink_ext_ack *extack) 2246 { 2247 return 0; 2248 } 2249 2250 static inline int br_switchdev_port_vlan_add(struct net_device *dev, u16 vid, 2251 u16 flags, bool changed, 2252 struct netlink_ext_ack *extack) 2253 { 2254 return -EOPNOTSUPP; 2255 } 2256 2257 static inline int br_switchdev_port_vlan_del(struct net_device *dev, u16 vid) 2258 { 2259 return -EOPNOTSUPP; 2260 } 2261 2262 static inline void 2263 br_switchdev_fdb_notify(struct net_bridge *br, 2264 const struct net_bridge_fdb_entry *fdb, int type) 2265 { 2266 } 2267 2268 static inline void br_switchdev_mdb_notify(struct net_device *dev, 2269 struct net_bridge_mdb_entry *mp, 2270 struct net_bridge_port_group *pg, 2271 int type) 2272 { 2273 } 2274 2275 static inline void br_switchdev_frame_unmark(struct sk_buff *skb) 2276 { 2277 } 2278 2279 static inline void br_switchdev_init(struct net_bridge *br) 2280 { 2281 } 2282 2283 #endif /* CONFIG_NET_SWITCHDEV */ 2284 2285 /* br_arp_nd_proxy.c */ 2286 void br_recalculate_neigh_suppress_enabled(struct net_bridge *br); 2287 void br_do_proxy_suppress_arp(struct sk_buff *skb, struct net_bridge *br, 2288 u16 vid, struct net_bridge_port *p); 2289 void br_do_suppress_nd(struct sk_buff *skb, struct net_bridge *br, 2290 u16 vid, struct net_bridge_port *p, struct nd_msg *msg); 2291 struct nd_msg *br_is_nd_neigh_msg(struct sk_buff *skb, struct nd_msg *m); 2292 bool br_is_neigh_suppress_enabled(const struct net_bridge_port *p, u16 vid); 2293 #endif 2294