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