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