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