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