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