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