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