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