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