xref: /openbmc/linux/net/bridge/br_private.h (revision 083e3534)
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,
945 			    const struct br_ip *group,
946 			    struct net_bridge_port_group __rcu *next,
947 			    unsigned char flags, const unsigned char *src,
948 			    u8 filter_mode, u8 rt_protocol);
949 int br_mdb_hash_init(struct net_bridge *br);
950 void br_mdb_hash_fini(struct net_bridge *br);
951 void br_mdb_notify(struct net_device *dev, struct net_bridge_mdb_entry *mp,
952 		   struct net_bridge_port_group *pg, int type);
953 void br_rtr_notify(struct net_device *dev, struct net_bridge_mcast_port *pmctx,
954 		   int type);
955 void br_multicast_del_pg(struct net_bridge_mdb_entry *mp,
956 			 struct net_bridge_port_group *pg,
957 			 struct net_bridge_port_group __rcu **pp);
958 void br_multicast_count(struct net_bridge *br,
959 			const struct net_bridge_port *p,
960 			const struct sk_buff *skb, u8 type, u8 dir);
961 int br_multicast_init_stats(struct net_bridge *br);
962 void br_multicast_uninit_stats(struct net_bridge *br);
963 void br_multicast_get_stats(const struct net_bridge *br,
964 			    const struct net_bridge_port *p,
965 			    struct br_mcast_stats *dest);
966 void br_mdb_init(void);
967 void br_mdb_uninit(void);
968 void br_multicast_host_join(const struct net_bridge_mcast *brmctx,
969 			    struct net_bridge_mdb_entry *mp, bool notify);
970 void br_multicast_host_leave(struct net_bridge_mdb_entry *mp, bool notify);
971 void br_multicast_star_g_handle_mode(struct net_bridge_port_group *pg,
972 				     u8 filter_mode);
973 void br_multicast_sg_add_exclude_ports(struct net_bridge_mdb_entry *star_mp,
974 				       struct net_bridge_port_group *sg);
975 struct net_bridge_group_src *
976 br_multicast_find_group_src(struct net_bridge_port_group *pg, struct br_ip *ip);
977 struct net_bridge_group_src *
978 br_multicast_new_group_src(struct net_bridge_port_group *pg,
979 			   struct br_ip *src_ip);
980 void __br_multicast_del_group_src(struct net_bridge_group_src *src);
981 void br_multicast_del_group_src(struct net_bridge_group_src *src,
982 				bool fastleave);
983 void br_multicast_ctx_init(struct net_bridge *br,
984 			   struct net_bridge_vlan *vlan,
985 			   struct net_bridge_mcast *brmctx);
986 void br_multicast_ctx_deinit(struct net_bridge_mcast *brmctx);
987 void br_multicast_port_ctx_init(struct net_bridge_port *port,
988 				struct net_bridge_vlan *vlan,
989 				struct net_bridge_mcast_port *pmctx);
990 void br_multicast_port_ctx_deinit(struct net_bridge_mcast_port *pmctx);
991 void br_multicast_toggle_one_vlan(struct net_bridge_vlan *vlan, bool on);
992 int br_multicast_toggle_vlan_snooping(struct net_bridge *br, bool on,
993 				      struct netlink_ext_ack *extack);
994 bool br_multicast_toggle_global_vlan(struct net_bridge_vlan *vlan, bool on);
995 
996 int br_rports_fill_info(struct sk_buff *skb,
997 			const struct net_bridge_mcast *brmctx);
998 int br_multicast_dump_querier_state(struct sk_buff *skb,
999 				    const struct net_bridge_mcast *brmctx,
1000 				    int nest_attr);
1001 size_t br_multicast_querier_state_size(void);
1002 size_t br_rports_size(const struct net_bridge_mcast *brmctx);
1003 void br_multicast_set_query_intvl(struct net_bridge_mcast *brmctx,
1004 				  unsigned long val);
1005 void br_multicast_set_startup_query_intvl(struct net_bridge_mcast *brmctx,
1006 					  unsigned long val);
1007 
1008 static inline bool br_group_is_l2(const struct br_ip *group)
1009 {
1010 	return group->proto == 0;
1011 }
1012 
1013 #define mlock_dereference(X, br) \
1014 	rcu_dereference_protected(X, lockdep_is_held(&br->multicast_lock))
1015 
1016 static inline struct hlist_node *
1017 br_multicast_get_first_rport_node(struct net_bridge_mcast *brmctx,
1018 				  struct sk_buff *skb)
1019 {
1020 #if IS_ENABLED(CONFIG_IPV6)
1021 	if (skb->protocol == htons(ETH_P_IPV6))
1022 		return rcu_dereference(hlist_first_rcu(&brmctx->ip6_mc_router_list));
1023 #endif
1024 	return rcu_dereference(hlist_first_rcu(&brmctx->ip4_mc_router_list));
1025 }
1026 
1027 static inline struct net_bridge_port *
1028 br_multicast_rport_from_node_skb(struct hlist_node *rp, struct sk_buff *skb)
1029 {
1030 	struct net_bridge_mcast_port *mctx;
1031 
1032 #if IS_ENABLED(CONFIG_IPV6)
1033 	if (skb->protocol == htons(ETH_P_IPV6))
1034 		mctx = hlist_entry_safe(rp, struct net_bridge_mcast_port,
1035 					ip6_rlist);
1036 	else
1037 #endif
1038 		mctx = hlist_entry_safe(rp, struct net_bridge_mcast_port,
1039 					ip4_rlist);
1040 
1041 	if (mctx)
1042 		return mctx->port;
1043 	else
1044 		return NULL;
1045 }
1046 
1047 static inline bool br_ip4_multicast_is_router(struct net_bridge_mcast *brmctx)
1048 {
1049 	return timer_pending(&brmctx->ip4_mc_router_timer);
1050 }
1051 
1052 static inline bool br_ip6_multicast_is_router(struct net_bridge_mcast *brmctx)
1053 {
1054 #if IS_ENABLED(CONFIG_IPV6)
1055 	return timer_pending(&brmctx->ip6_mc_router_timer);
1056 #else
1057 	return false;
1058 #endif
1059 }
1060 
1061 static inline bool
1062 br_multicast_is_router(struct net_bridge_mcast *brmctx, struct sk_buff *skb)
1063 {
1064 	switch (brmctx->multicast_router) {
1065 	case MDB_RTR_TYPE_PERM:
1066 		return true;
1067 	case MDB_RTR_TYPE_TEMP_QUERY:
1068 		if (skb) {
1069 			if (skb->protocol == htons(ETH_P_IP))
1070 				return br_ip4_multicast_is_router(brmctx);
1071 			else if (skb->protocol == htons(ETH_P_IPV6))
1072 				return br_ip6_multicast_is_router(brmctx);
1073 		} else {
1074 			return br_ip4_multicast_is_router(brmctx) ||
1075 			       br_ip6_multicast_is_router(brmctx);
1076 		}
1077 		fallthrough;
1078 	default:
1079 		return false;
1080 	}
1081 }
1082 
1083 static inline bool
1084 __br_multicast_querier_exists(struct net_bridge_mcast *brmctx,
1085 			      struct bridge_mcast_other_query *querier,
1086 			      const bool is_ipv6)
1087 {
1088 	bool own_querier_enabled;
1089 
1090 	if (brmctx->multicast_querier) {
1091 		if (is_ipv6 && !br_opt_get(brmctx->br, BROPT_HAS_IPV6_ADDR))
1092 			own_querier_enabled = false;
1093 		else
1094 			own_querier_enabled = true;
1095 	} else {
1096 		own_querier_enabled = false;
1097 	}
1098 
1099 	return time_is_before_jiffies(querier->delay_time) &&
1100 	       (own_querier_enabled || timer_pending(&querier->timer));
1101 }
1102 
1103 static inline bool br_multicast_querier_exists(struct net_bridge_mcast *brmctx,
1104 					       struct ethhdr *eth,
1105 					       const struct net_bridge_mdb_entry *mdb)
1106 {
1107 	switch (eth->h_proto) {
1108 	case (htons(ETH_P_IP)):
1109 		return __br_multicast_querier_exists(brmctx,
1110 			&brmctx->ip4_other_query, false);
1111 #if IS_ENABLED(CONFIG_IPV6)
1112 	case (htons(ETH_P_IPV6)):
1113 		return __br_multicast_querier_exists(brmctx,
1114 			&brmctx->ip6_other_query, true);
1115 #endif
1116 	default:
1117 		return !!mdb && br_group_is_l2(&mdb->addr);
1118 	}
1119 }
1120 
1121 static inline bool br_multicast_is_star_g(const struct br_ip *ip)
1122 {
1123 	switch (ip->proto) {
1124 	case htons(ETH_P_IP):
1125 		return ipv4_is_zeronet(ip->src.ip4);
1126 #if IS_ENABLED(CONFIG_IPV6)
1127 	case htons(ETH_P_IPV6):
1128 		return ipv6_addr_any(&ip->src.ip6);
1129 #endif
1130 	default:
1131 		return false;
1132 	}
1133 }
1134 
1135 static inline bool
1136 br_multicast_should_handle_mode(const struct net_bridge_mcast *brmctx,
1137 				__be16 proto)
1138 {
1139 	switch (proto) {
1140 	case htons(ETH_P_IP):
1141 		return !!(brmctx->multicast_igmp_version == 3);
1142 #if IS_ENABLED(CONFIG_IPV6)
1143 	case htons(ETH_P_IPV6):
1144 		return !!(brmctx->multicast_mld_version == 2);
1145 #endif
1146 	default:
1147 		return false;
1148 	}
1149 }
1150 
1151 static inline int br_multicast_igmp_type(const struct sk_buff *skb)
1152 {
1153 	return BR_INPUT_SKB_CB(skb)->igmp;
1154 }
1155 
1156 static inline unsigned long br_multicast_lmqt(const struct net_bridge_mcast *brmctx)
1157 {
1158 	return brmctx->multicast_last_member_interval *
1159 	       brmctx->multicast_last_member_count;
1160 }
1161 
1162 static inline unsigned long br_multicast_gmi(const struct net_bridge_mcast *brmctx)
1163 {
1164 	return brmctx->multicast_membership_interval;
1165 }
1166 
1167 static inline bool
1168 br_multicast_ctx_is_vlan(const struct net_bridge_mcast *brmctx)
1169 {
1170 	return !!brmctx->vlan;
1171 }
1172 
1173 static inline bool
1174 br_multicast_port_ctx_is_vlan(const struct net_bridge_mcast_port *pmctx)
1175 {
1176 	return !!pmctx->vlan;
1177 }
1178 
1179 static inline struct net_bridge_mcast *
1180 br_multicast_port_ctx_get_global(const struct net_bridge_mcast_port *pmctx)
1181 {
1182 	if (!br_multicast_port_ctx_is_vlan(pmctx))
1183 		return &pmctx->port->br->multicast_ctx;
1184 	else
1185 		return &pmctx->vlan->brvlan->br_mcast_ctx;
1186 }
1187 
1188 static inline bool
1189 br_multicast_ctx_vlan_global_disabled(const struct net_bridge_mcast *brmctx)
1190 {
1191 	return br_multicast_ctx_is_vlan(brmctx) &&
1192 	       (!br_opt_get(brmctx->br, BROPT_MCAST_VLAN_SNOOPING_ENABLED) ||
1193 		!(brmctx->vlan->priv_flags & BR_VLFLAG_GLOBAL_MCAST_ENABLED));
1194 }
1195 
1196 static inline bool
1197 br_multicast_ctx_vlan_disabled(const struct net_bridge_mcast *brmctx)
1198 {
1199 	return br_multicast_ctx_is_vlan(brmctx) &&
1200 	       !(brmctx->vlan->priv_flags & BR_VLFLAG_MCAST_ENABLED);
1201 }
1202 
1203 static inline bool
1204 br_multicast_port_ctx_vlan_disabled(const struct net_bridge_mcast_port *pmctx)
1205 {
1206 	return br_multicast_port_ctx_is_vlan(pmctx) &&
1207 	       !(pmctx->vlan->priv_flags & BR_VLFLAG_MCAST_ENABLED);
1208 }
1209 
1210 static inline bool
1211 br_multicast_port_ctx_state_disabled(const struct net_bridge_mcast_port *pmctx)
1212 {
1213 	return pmctx->port->state == BR_STATE_DISABLED ||
1214 	       (br_multicast_port_ctx_is_vlan(pmctx) &&
1215 		(br_multicast_port_ctx_vlan_disabled(pmctx) ||
1216 		 pmctx->vlan->state == BR_STATE_DISABLED));
1217 }
1218 
1219 static inline bool
1220 br_multicast_port_ctx_state_stopped(const struct net_bridge_mcast_port *pmctx)
1221 {
1222 	return br_multicast_port_ctx_state_disabled(pmctx) ||
1223 	       pmctx->port->state == BR_STATE_BLOCKING ||
1224 	       (br_multicast_port_ctx_is_vlan(pmctx) &&
1225 		pmctx->vlan->state == BR_STATE_BLOCKING);
1226 }
1227 
1228 static inline bool
1229 br_rports_have_mc_router(const struct net_bridge_mcast *brmctx)
1230 {
1231 #if IS_ENABLED(CONFIG_IPV6)
1232 	return !hlist_empty(&brmctx->ip4_mc_router_list) ||
1233 	       !hlist_empty(&brmctx->ip6_mc_router_list);
1234 #else
1235 	return !hlist_empty(&brmctx->ip4_mc_router_list);
1236 #endif
1237 }
1238 
1239 static inline bool
1240 br_multicast_ctx_options_equal(const struct net_bridge_mcast *brmctx1,
1241 			       const struct net_bridge_mcast *brmctx2)
1242 {
1243 	return brmctx1->multicast_igmp_version ==
1244 	       brmctx2->multicast_igmp_version &&
1245 	       brmctx1->multicast_last_member_count ==
1246 	       brmctx2->multicast_last_member_count &&
1247 	       brmctx1->multicast_startup_query_count ==
1248 	       brmctx2->multicast_startup_query_count &&
1249 	       brmctx1->multicast_last_member_interval ==
1250 	       brmctx2->multicast_last_member_interval &&
1251 	       brmctx1->multicast_membership_interval ==
1252 	       brmctx2->multicast_membership_interval &&
1253 	       brmctx1->multicast_querier_interval ==
1254 	       brmctx2->multicast_querier_interval &&
1255 	       brmctx1->multicast_query_interval ==
1256 	       brmctx2->multicast_query_interval &&
1257 	       brmctx1->multicast_query_response_interval ==
1258 	       brmctx2->multicast_query_response_interval &&
1259 	       brmctx1->multicast_startup_query_interval ==
1260 	       brmctx2->multicast_startup_query_interval &&
1261 	       brmctx1->multicast_querier == brmctx2->multicast_querier &&
1262 	       brmctx1->multicast_router == brmctx2->multicast_router &&
1263 	       !br_rports_have_mc_router(brmctx1) &&
1264 	       !br_rports_have_mc_router(brmctx2) &&
1265 #if IS_ENABLED(CONFIG_IPV6)
1266 	       brmctx1->multicast_mld_version ==
1267 	       brmctx2->multicast_mld_version &&
1268 #endif
1269 	       true;
1270 }
1271 
1272 static inline bool
1273 br_multicast_ctx_matches_vlan_snooping(const struct net_bridge_mcast *brmctx)
1274 {
1275 	bool vlan_snooping_enabled;
1276 
1277 	vlan_snooping_enabled = !!br_opt_get(brmctx->br,
1278 					     BROPT_MCAST_VLAN_SNOOPING_ENABLED);
1279 
1280 	return !!(vlan_snooping_enabled == br_multicast_ctx_is_vlan(brmctx));
1281 }
1282 #else
1283 static inline int br_multicast_rcv(struct net_bridge_mcast **brmctx,
1284 				   struct net_bridge_mcast_port **pmctx,
1285 				   struct net_bridge_vlan *vlan,
1286 				   struct sk_buff *skb,
1287 				   u16 vid)
1288 {
1289 	return 0;
1290 }
1291 
1292 static inline struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge_mcast *brmctx,
1293 						      struct sk_buff *skb, u16 vid)
1294 {
1295 	return NULL;
1296 }
1297 
1298 static inline int br_multicast_add_port(struct net_bridge_port *port)
1299 {
1300 	return 0;
1301 }
1302 
1303 static inline void br_multicast_del_port(struct net_bridge_port *port)
1304 {
1305 }
1306 
1307 static inline void br_multicast_enable_port(struct net_bridge_port *port)
1308 {
1309 }
1310 
1311 static inline void br_multicast_disable_port(struct net_bridge_port *port)
1312 {
1313 }
1314 
1315 static inline void br_multicast_init(struct net_bridge *br)
1316 {
1317 }
1318 
1319 static inline void br_multicast_join_snoopers(struct net_bridge *br)
1320 {
1321 }
1322 
1323 static inline void br_multicast_leave_snoopers(struct net_bridge *br)
1324 {
1325 }
1326 
1327 static inline void br_multicast_open(struct net_bridge *br)
1328 {
1329 }
1330 
1331 static inline void br_multicast_stop(struct net_bridge *br)
1332 {
1333 }
1334 
1335 static inline void br_multicast_dev_del(struct net_bridge *br)
1336 {
1337 }
1338 
1339 static inline void br_multicast_flood(struct net_bridge_mdb_entry *mdst,
1340 				      struct sk_buff *skb,
1341 				      struct net_bridge_mcast *brmctx,
1342 				      bool local_rcv, bool local_orig)
1343 {
1344 }
1345 
1346 static inline bool br_multicast_is_router(struct net_bridge_mcast *brmctx,
1347 					  struct sk_buff *skb)
1348 {
1349 	return false;
1350 }
1351 
1352 static inline bool br_multicast_querier_exists(struct net_bridge_mcast *brmctx,
1353 					       struct ethhdr *eth,
1354 					       const struct net_bridge_mdb_entry *mdb)
1355 {
1356 	return false;
1357 }
1358 
1359 static inline void br_mdb_init(void)
1360 {
1361 }
1362 
1363 static inline void br_mdb_uninit(void)
1364 {
1365 }
1366 
1367 static inline int br_mdb_hash_init(struct net_bridge *br)
1368 {
1369 	return 0;
1370 }
1371 
1372 static inline void br_mdb_hash_fini(struct net_bridge *br)
1373 {
1374 }
1375 
1376 static inline void br_multicast_count(struct net_bridge *br,
1377 				      const struct net_bridge_port *p,
1378 				      const struct sk_buff *skb,
1379 				      u8 type, u8 dir)
1380 {
1381 }
1382 
1383 static inline int br_multicast_init_stats(struct net_bridge *br)
1384 {
1385 	return 0;
1386 }
1387 
1388 static inline void br_multicast_uninit_stats(struct net_bridge *br)
1389 {
1390 }
1391 
1392 static inline int br_multicast_igmp_type(const struct sk_buff *skb)
1393 {
1394 	return 0;
1395 }
1396 
1397 static inline void br_multicast_ctx_init(struct net_bridge *br,
1398 					 struct net_bridge_vlan *vlan,
1399 					 struct net_bridge_mcast *brmctx)
1400 {
1401 }
1402 
1403 static inline void br_multicast_ctx_deinit(struct net_bridge_mcast *brmctx)
1404 {
1405 }
1406 
1407 static inline void br_multicast_port_ctx_init(struct net_bridge_port *port,
1408 					      struct net_bridge_vlan *vlan,
1409 					      struct net_bridge_mcast_port *pmctx)
1410 {
1411 }
1412 
1413 static inline void br_multicast_port_ctx_deinit(struct net_bridge_mcast_port *pmctx)
1414 {
1415 }
1416 
1417 static inline void br_multicast_toggle_one_vlan(struct net_bridge_vlan *vlan,
1418 						bool on)
1419 {
1420 }
1421 
1422 static inline int br_multicast_toggle_vlan_snooping(struct net_bridge *br,
1423 						    bool on,
1424 						    struct netlink_ext_ack *extack)
1425 {
1426 	return -EOPNOTSUPP;
1427 }
1428 
1429 static inline bool br_multicast_toggle_global_vlan(struct net_bridge_vlan *vlan,
1430 						   bool on)
1431 {
1432 	return false;
1433 }
1434 
1435 static inline bool
1436 br_multicast_ctx_options_equal(const struct net_bridge_mcast *brmctx1,
1437 			       const struct net_bridge_mcast *brmctx2)
1438 {
1439 	return true;
1440 }
1441 #endif
1442 
1443 /* br_vlan.c */
1444 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
1445 bool br_allowed_ingress(const struct net_bridge *br,
1446 			struct net_bridge_vlan_group *vg, struct sk_buff *skb,
1447 			u16 *vid, u8 *state,
1448 			struct net_bridge_vlan **vlan);
1449 bool br_allowed_egress(struct net_bridge_vlan_group *vg,
1450 		       const struct sk_buff *skb);
1451 bool br_should_learn(struct net_bridge_port *p, struct sk_buff *skb, u16 *vid);
1452 struct sk_buff *br_handle_vlan(struct net_bridge *br,
1453 			       const struct net_bridge_port *port,
1454 			       struct net_bridge_vlan_group *vg,
1455 			       struct sk_buff *skb);
1456 int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags,
1457 		bool *changed, struct netlink_ext_ack *extack);
1458 int br_vlan_delete(struct net_bridge *br, u16 vid);
1459 void br_vlan_flush(struct net_bridge *br);
1460 struct net_bridge_vlan *br_vlan_find(struct net_bridge_vlan_group *vg, u16 vid);
1461 void br_recalculate_fwd_mask(struct net_bridge *br);
1462 int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val,
1463 			  struct netlink_ext_ack *extack);
1464 int __br_vlan_set_proto(struct net_bridge *br, __be16 proto,
1465 			struct netlink_ext_ack *extack);
1466 int br_vlan_set_proto(struct net_bridge *br, unsigned long val,
1467 		      struct netlink_ext_ack *extack);
1468 int br_vlan_set_stats(struct net_bridge *br, unsigned long val);
1469 int br_vlan_set_stats_per_port(struct net_bridge *br, unsigned long val);
1470 int br_vlan_init(struct net_bridge *br);
1471 int br_vlan_set_default_pvid(struct net_bridge *br, unsigned long val,
1472 			     struct netlink_ext_ack *extack);
1473 int __br_vlan_set_default_pvid(struct net_bridge *br, u16 pvid,
1474 			       struct netlink_ext_ack *extack);
1475 int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags,
1476 		 bool *changed, struct netlink_ext_ack *extack);
1477 int nbp_vlan_delete(struct net_bridge_port *port, u16 vid);
1478 void nbp_vlan_flush(struct net_bridge_port *port);
1479 int nbp_vlan_init(struct net_bridge_port *port, struct netlink_ext_ack *extack);
1480 int nbp_get_num_vlan_infos(struct net_bridge_port *p, u32 filter_mask);
1481 void br_vlan_get_stats(const struct net_bridge_vlan *v,
1482 		       struct pcpu_sw_netstats *stats);
1483 void br_vlan_port_event(struct net_bridge_port *p, unsigned long event);
1484 int br_vlan_bridge_event(struct net_device *dev, unsigned long event,
1485 			 void *ptr);
1486 void br_vlan_rtnl_init(void);
1487 void br_vlan_rtnl_uninit(void);
1488 void br_vlan_notify(const struct net_bridge *br,
1489 		    const struct net_bridge_port *p,
1490 		    u16 vid, u16 vid_range,
1491 		    int cmd);
1492 bool br_vlan_can_enter_range(const struct net_bridge_vlan *v_curr,
1493 			     const struct net_bridge_vlan *range_end);
1494 
1495 void br_vlan_fill_forward_path_pvid(struct net_bridge *br,
1496 				    struct net_device_path_ctx *ctx,
1497 				    struct net_device_path *path);
1498 int br_vlan_fill_forward_path_mode(struct net_bridge *br,
1499 				   struct net_bridge_port *dst,
1500 				   struct net_device_path *path);
1501 
1502 static inline struct net_bridge_vlan_group *br_vlan_group(
1503 					const struct net_bridge *br)
1504 {
1505 	return rtnl_dereference(br->vlgrp);
1506 }
1507 
1508 static inline struct net_bridge_vlan_group *nbp_vlan_group(
1509 					const struct net_bridge_port *p)
1510 {
1511 	return rtnl_dereference(p->vlgrp);
1512 }
1513 
1514 static inline struct net_bridge_vlan_group *br_vlan_group_rcu(
1515 					const struct net_bridge *br)
1516 {
1517 	return rcu_dereference(br->vlgrp);
1518 }
1519 
1520 static inline struct net_bridge_vlan_group *nbp_vlan_group_rcu(
1521 					const struct net_bridge_port *p)
1522 {
1523 	return rcu_dereference(p->vlgrp);
1524 }
1525 
1526 /* Since bridge now depends on 8021Q module, but the time bridge sees the
1527  * skb, the vlan tag will always be present if the frame was tagged.
1528  */
1529 static inline int br_vlan_get_tag(const struct sk_buff *skb, u16 *vid)
1530 {
1531 	int err = 0;
1532 
1533 	if (skb_vlan_tag_present(skb)) {
1534 		*vid = skb_vlan_tag_get_id(skb);
1535 	} else {
1536 		*vid = 0;
1537 		err = -EINVAL;
1538 	}
1539 
1540 	return err;
1541 }
1542 
1543 static inline u16 br_get_pvid(const struct net_bridge_vlan_group *vg)
1544 {
1545 	if (!vg)
1546 		return 0;
1547 
1548 	smp_rmb();
1549 	return vg->pvid;
1550 }
1551 
1552 static inline u16 br_vlan_flags(const struct net_bridge_vlan *v, u16 pvid)
1553 {
1554 	return v->vid == pvid ? v->flags | BRIDGE_VLAN_INFO_PVID : v->flags;
1555 }
1556 #else
1557 static inline bool br_allowed_ingress(const struct net_bridge *br,
1558 				      struct net_bridge_vlan_group *vg,
1559 				      struct sk_buff *skb,
1560 				      u16 *vid, u8 *state,
1561 				      struct net_bridge_vlan **vlan)
1562 
1563 {
1564 	*vlan = NULL;
1565 	return true;
1566 }
1567 
1568 static inline bool br_allowed_egress(struct net_bridge_vlan_group *vg,
1569 				     const struct sk_buff *skb)
1570 {
1571 	return true;
1572 }
1573 
1574 static inline bool br_should_learn(struct net_bridge_port *p,
1575 				   struct sk_buff *skb, u16 *vid)
1576 {
1577 	return true;
1578 }
1579 
1580 static inline struct sk_buff *br_handle_vlan(struct net_bridge *br,
1581 					     const struct net_bridge_port *port,
1582 					     struct net_bridge_vlan_group *vg,
1583 					     struct sk_buff *skb)
1584 {
1585 	return skb;
1586 }
1587 
1588 static inline int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags,
1589 			      bool *changed, struct netlink_ext_ack *extack)
1590 {
1591 	*changed = false;
1592 	return -EOPNOTSUPP;
1593 }
1594 
1595 static inline int br_vlan_delete(struct net_bridge *br, u16 vid)
1596 {
1597 	return -EOPNOTSUPP;
1598 }
1599 
1600 static inline void br_vlan_flush(struct net_bridge *br)
1601 {
1602 }
1603 
1604 static inline void br_recalculate_fwd_mask(struct net_bridge *br)
1605 {
1606 }
1607 
1608 static inline int br_vlan_init(struct net_bridge *br)
1609 {
1610 	return 0;
1611 }
1612 
1613 static inline int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags,
1614 			       bool *changed, struct netlink_ext_ack *extack)
1615 {
1616 	*changed = false;
1617 	return -EOPNOTSUPP;
1618 }
1619 
1620 static inline int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
1621 {
1622 	return -EOPNOTSUPP;
1623 }
1624 
1625 static inline void nbp_vlan_flush(struct net_bridge_port *port)
1626 {
1627 }
1628 
1629 static inline struct net_bridge_vlan *br_vlan_find(struct net_bridge_vlan_group *vg,
1630 						   u16 vid)
1631 {
1632 	return NULL;
1633 }
1634 
1635 static inline int nbp_vlan_init(struct net_bridge_port *port,
1636 				struct netlink_ext_ack *extack)
1637 {
1638 	return 0;
1639 }
1640 
1641 static inline u16 br_vlan_get_tag(const struct sk_buff *skb, u16 *tag)
1642 {
1643 	return 0;
1644 }
1645 
1646 static inline u16 br_get_pvid(const struct net_bridge_vlan_group *vg)
1647 {
1648 	return 0;
1649 }
1650 
1651 static inline int br_vlan_filter_toggle(struct net_bridge *br,
1652 					unsigned long val,
1653 					struct netlink_ext_ack *extack)
1654 {
1655 	return -EOPNOTSUPP;
1656 }
1657 
1658 static inline int nbp_get_num_vlan_infos(struct net_bridge_port *p,
1659 					 u32 filter_mask)
1660 {
1661 	return 0;
1662 }
1663 
1664 static inline void br_vlan_fill_forward_path_pvid(struct net_bridge *br,
1665 						  struct net_device_path_ctx *ctx,
1666 						  struct net_device_path *path)
1667 {
1668 }
1669 
1670 static inline int br_vlan_fill_forward_path_mode(struct net_bridge *br,
1671 						 struct net_bridge_port *dst,
1672 						 struct net_device_path *path)
1673 {
1674 	return 0;
1675 }
1676 
1677 static inline struct net_bridge_vlan_group *br_vlan_group(
1678 					const struct net_bridge *br)
1679 {
1680 	return NULL;
1681 }
1682 
1683 static inline struct net_bridge_vlan_group *nbp_vlan_group(
1684 					const struct net_bridge_port *p)
1685 {
1686 	return NULL;
1687 }
1688 
1689 static inline struct net_bridge_vlan_group *br_vlan_group_rcu(
1690 					const struct net_bridge *br)
1691 {
1692 	return NULL;
1693 }
1694 
1695 static inline struct net_bridge_vlan_group *nbp_vlan_group_rcu(
1696 					const struct net_bridge_port *p)
1697 {
1698 	return NULL;
1699 }
1700 
1701 static inline void br_vlan_get_stats(const struct net_bridge_vlan *v,
1702 				     struct pcpu_sw_netstats *stats)
1703 {
1704 }
1705 
1706 static inline void br_vlan_port_event(struct net_bridge_port *p,
1707 				      unsigned long event)
1708 {
1709 }
1710 
1711 static inline int br_vlan_bridge_event(struct net_device *dev,
1712 				       unsigned long event, void *ptr)
1713 {
1714 	return 0;
1715 }
1716 
1717 static inline void br_vlan_rtnl_init(void)
1718 {
1719 }
1720 
1721 static inline void br_vlan_rtnl_uninit(void)
1722 {
1723 }
1724 
1725 static inline void br_vlan_notify(const struct net_bridge *br,
1726 				  const struct net_bridge_port *p,
1727 				  u16 vid, u16 vid_range,
1728 				  int cmd)
1729 {
1730 }
1731 
1732 static inline bool br_vlan_can_enter_range(const struct net_bridge_vlan *v_curr,
1733 					   const struct net_bridge_vlan *range_end)
1734 {
1735 	return true;
1736 }
1737 
1738 static inline u16 br_vlan_flags(const struct net_bridge_vlan *v, u16 pvid)
1739 {
1740 	return 0;
1741 }
1742 
1743 #endif
1744 
1745 /* br_vlan_options.c */
1746 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
1747 bool br_vlan_opts_eq_range(const struct net_bridge_vlan *v_curr,
1748 			   const struct net_bridge_vlan *range_end);
1749 bool br_vlan_opts_fill(struct sk_buff *skb, const struct net_bridge_vlan *v);
1750 size_t br_vlan_opts_nl_size(void);
1751 int br_vlan_process_options(const struct net_bridge *br,
1752 			    const struct net_bridge_port *p,
1753 			    struct net_bridge_vlan *range_start,
1754 			    struct net_bridge_vlan *range_end,
1755 			    struct nlattr **tb,
1756 			    struct netlink_ext_ack *extack);
1757 int br_vlan_rtm_process_global_options(struct net_device *dev,
1758 				       const struct nlattr *attr,
1759 				       int cmd,
1760 				       struct netlink_ext_ack *extack);
1761 bool br_vlan_global_opts_can_enter_range(const struct net_bridge_vlan *v_curr,
1762 					 const struct net_bridge_vlan *r_end);
1763 bool br_vlan_global_opts_fill(struct sk_buff *skb, u16 vid, u16 vid_range,
1764 			      const struct net_bridge_vlan *v_opts);
1765 
1766 /* vlan state manipulation helpers using *_ONCE to annotate lock-free access */
1767 static inline u8 br_vlan_get_state(const struct net_bridge_vlan *v)
1768 {
1769 	return READ_ONCE(v->state);
1770 }
1771 
1772 static inline void br_vlan_set_state(struct net_bridge_vlan *v, u8 state)
1773 {
1774 	WRITE_ONCE(v->state, state);
1775 }
1776 
1777 static inline u8 br_vlan_get_pvid_state(const struct net_bridge_vlan_group *vg)
1778 {
1779 	return READ_ONCE(vg->pvid_state);
1780 }
1781 
1782 static inline void br_vlan_set_pvid_state(struct net_bridge_vlan_group *vg,
1783 					  u8 state)
1784 {
1785 	WRITE_ONCE(vg->pvid_state, state);
1786 }
1787 
1788 /* learn_allow is true at ingress and false at egress */
1789 static inline bool br_vlan_state_allowed(u8 state, bool learn_allow)
1790 {
1791 	switch (state) {
1792 	case BR_STATE_LEARNING:
1793 		return learn_allow;
1794 	case BR_STATE_FORWARDING:
1795 		return true;
1796 	default:
1797 		return false;
1798 	}
1799 }
1800 #endif
1801 
1802 /* br_mst.c */
1803 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
1804 DECLARE_STATIC_KEY_FALSE(br_mst_used);
1805 static inline bool br_mst_is_enabled(struct net_bridge *br)
1806 {
1807 	return static_branch_unlikely(&br_mst_used) &&
1808 		br_opt_get(br, BROPT_MST_ENABLED);
1809 }
1810 
1811 int br_mst_set_state(struct net_bridge_port *p, u16 msti, u8 state,
1812 		     struct netlink_ext_ack *extack);
1813 int br_mst_vlan_set_msti(struct net_bridge_vlan *v, u16 msti);
1814 void br_mst_vlan_init_state(struct net_bridge_vlan *v);
1815 int br_mst_set_enabled(struct net_bridge *br, bool on,
1816 		       struct netlink_ext_ack *extack);
1817 size_t br_mst_info_size(const struct net_bridge_vlan_group *vg);
1818 int br_mst_fill_info(struct sk_buff *skb,
1819 		     const struct net_bridge_vlan_group *vg);
1820 int br_mst_process(struct net_bridge_port *p, const struct nlattr *mst_attr,
1821 		   struct netlink_ext_ack *extack);
1822 #else
1823 static inline bool br_mst_is_enabled(struct net_bridge *br)
1824 {
1825 	return false;
1826 }
1827 
1828 static inline int br_mst_set_state(struct net_bridge_port *p, u16 msti,
1829 				   u8 state, struct netlink_ext_ack *extack)
1830 {
1831 	return -EOPNOTSUPP;
1832 }
1833 
1834 static inline int br_mst_set_enabled(struct net_bridge *br, bool on,
1835 				     struct netlink_ext_ack *extack)
1836 {
1837 	return -EOPNOTSUPP;
1838 }
1839 
1840 static inline size_t br_mst_info_size(const struct net_bridge_vlan_group *vg)
1841 {
1842 	return 0;
1843 }
1844 
1845 static inline int br_mst_fill_info(struct sk_buff *skb,
1846 				   const struct net_bridge_vlan_group *vg)
1847 {
1848 	return -EOPNOTSUPP;
1849 }
1850 
1851 static inline int br_mst_process(struct net_bridge_port *p,
1852 				 const struct nlattr *mst_attr,
1853 				 struct netlink_ext_ack *extack)
1854 {
1855 	return -EOPNOTSUPP;
1856 }
1857 #endif
1858 
1859 struct nf_br_ops {
1860 	int (*br_dev_xmit_hook)(struct sk_buff *skb);
1861 };
1862 extern const struct nf_br_ops __rcu *nf_br_ops;
1863 
1864 /* br_netfilter.c */
1865 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1866 int br_nf_core_init(void);
1867 void br_nf_core_fini(void);
1868 void br_netfilter_rtable_init(struct net_bridge *);
1869 #else
1870 static inline int br_nf_core_init(void) { return 0; }
1871 static inline void br_nf_core_fini(void) {}
1872 #define br_netfilter_rtable_init(x)
1873 #endif
1874 
1875 /* br_stp.c */
1876 void br_set_state(struct net_bridge_port *p, unsigned int state);
1877 struct net_bridge_port *br_get_port(struct net_bridge *br, u16 port_no);
1878 void br_init_port(struct net_bridge_port *p);
1879 void br_become_designated_port(struct net_bridge_port *p);
1880 
1881 void __br_set_forward_delay(struct net_bridge *br, unsigned long t);
1882 int br_set_forward_delay(struct net_bridge *br, unsigned long x);
1883 int br_set_hello_time(struct net_bridge *br, unsigned long x);
1884 int br_set_max_age(struct net_bridge *br, unsigned long x);
1885 int __set_ageing_time(struct net_device *dev, unsigned long t);
1886 int br_set_ageing_time(struct net_bridge *br, clock_t ageing_time);
1887 
1888 
1889 /* br_stp_if.c */
1890 void br_stp_enable_bridge(struct net_bridge *br);
1891 void br_stp_disable_bridge(struct net_bridge *br);
1892 int br_stp_set_enabled(struct net_bridge *br, unsigned long val,
1893 		       struct netlink_ext_ack *extack);
1894 void br_stp_enable_port(struct net_bridge_port *p);
1895 void br_stp_disable_port(struct net_bridge_port *p);
1896 bool br_stp_recalculate_bridge_id(struct net_bridge *br);
1897 void br_stp_change_bridge_id(struct net_bridge *br, const unsigned char *a);
1898 void br_stp_set_bridge_priority(struct net_bridge *br, u16 newprio);
1899 int br_stp_set_port_priority(struct net_bridge_port *p, unsigned long newprio);
1900 int br_stp_set_path_cost(struct net_bridge_port *p, unsigned long path_cost);
1901 ssize_t br_show_bridge_id(char *buf, const struct bridge_id *id);
1902 
1903 /* br_stp_bpdu.c */
1904 struct stp_proto;
1905 void br_stp_rcv(const struct stp_proto *proto, struct sk_buff *skb,
1906 		struct net_device *dev);
1907 
1908 /* br_stp_timer.c */
1909 void br_stp_timer_init(struct net_bridge *br);
1910 void br_stp_port_timer_init(struct net_bridge_port *p);
1911 unsigned long br_timer_value(const struct timer_list *timer);
1912 
1913 /* br.c */
1914 #if IS_ENABLED(CONFIG_ATM_LANE)
1915 extern int (*br_fdb_test_addr_hook)(struct net_device *dev, unsigned char *addr);
1916 #endif
1917 
1918 /* br_mrp.c */
1919 #if IS_ENABLED(CONFIG_BRIDGE_MRP)
1920 int br_mrp_parse(struct net_bridge *br, struct net_bridge_port *p,
1921 		 struct nlattr *attr, int cmd, struct netlink_ext_ack *extack);
1922 bool br_mrp_enabled(struct net_bridge *br);
1923 void br_mrp_port_del(struct net_bridge *br, struct net_bridge_port *p);
1924 int br_mrp_fill_info(struct sk_buff *skb, struct net_bridge *br);
1925 #else
1926 static inline int br_mrp_parse(struct net_bridge *br, struct net_bridge_port *p,
1927 			       struct nlattr *attr, int cmd,
1928 			       struct netlink_ext_ack *extack)
1929 {
1930 	return -EOPNOTSUPP;
1931 }
1932 
1933 static inline bool br_mrp_enabled(struct net_bridge *br)
1934 {
1935 	return false;
1936 }
1937 
1938 static inline void br_mrp_port_del(struct net_bridge *br,
1939 				   struct net_bridge_port *p)
1940 {
1941 }
1942 
1943 static inline int br_mrp_fill_info(struct sk_buff *skb, struct net_bridge *br)
1944 {
1945 	return 0;
1946 }
1947 
1948 #endif
1949 
1950 /* br_cfm.c */
1951 #if IS_ENABLED(CONFIG_BRIDGE_CFM)
1952 int br_cfm_parse(struct net_bridge *br, struct net_bridge_port *p,
1953 		 struct nlattr *attr, int cmd, struct netlink_ext_ack *extack);
1954 bool br_cfm_created(struct net_bridge *br);
1955 void br_cfm_port_del(struct net_bridge *br, struct net_bridge_port *p);
1956 int br_cfm_config_fill_info(struct sk_buff *skb, struct net_bridge *br);
1957 int br_cfm_status_fill_info(struct sk_buff *skb,
1958 			    struct net_bridge *br,
1959 			    bool getlink);
1960 int br_cfm_mep_count(struct net_bridge *br, u32 *count);
1961 int br_cfm_peer_mep_count(struct net_bridge *br, u32 *count);
1962 #else
1963 static inline int br_cfm_parse(struct net_bridge *br, struct net_bridge_port *p,
1964 			       struct nlattr *attr, int cmd,
1965 			       struct netlink_ext_ack *extack)
1966 {
1967 	return -EOPNOTSUPP;
1968 }
1969 
1970 static inline bool br_cfm_created(struct net_bridge *br)
1971 {
1972 	return false;
1973 }
1974 
1975 static inline void br_cfm_port_del(struct net_bridge *br,
1976 				   struct net_bridge_port *p)
1977 {
1978 }
1979 
1980 static inline int br_cfm_config_fill_info(struct sk_buff *skb, struct net_bridge *br)
1981 {
1982 	return -EOPNOTSUPP;
1983 }
1984 
1985 static inline int br_cfm_status_fill_info(struct sk_buff *skb,
1986 					  struct net_bridge *br,
1987 					  bool getlink)
1988 {
1989 	return -EOPNOTSUPP;
1990 }
1991 
1992 static inline int br_cfm_mep_count(struct net_bridge *br, u32 *count)
1993 {
1994 	*count = 0;
1995 	return -EOPNOTSUPP;
1996 }
1997 
1998 static inline int br_cfm_peer_mep_count(struct net_bridge *br, u32 *count)
1999 {
2000 	*count = 0;
2001 	return -EOPNOTSUPP;
2002 }
2003 #endif
2004 
2005 /* br_netlink.c */
2006 extern struct rtnl_link_ops br_link_ops;
2007 int br_netlink_init(void);
2008 void br_netlink_fini(void);
2009 void br_ifinfo_notify(int event, const struct net_bridge *br,
2010 		      const struct net_bridge_port *port);
2011 void br_info_notify(int event, const struct net_bridge *br,
2012 		    const struct net_bridge_port *port, u32 filter);
2013 int br_setlink(struct net_device *dev, struct nlmsghdr *nlmsg, u16 flags,
2014 	       struct netlink_ext_ack *extack);
2015 int br_dellink(struct net_device *dev, struct nlmsghdr *nlmsg, u16 flags);
2016 int br_getlink(struct sk_buff *skb, u32 pid, u32 seq, struct net_device *dev,
2017 	       u32 filter_mask, int nlflags);
2018 int br_process_vlan_info(struct net_bridge *br,
2019 			 struct net_bridge_port *p, int cmd,
2020 			 struct bridge_vlan_info *vinfo_curr,
2021 			 struct bridge_vlan_info **vinfo_last,
2022 			 bool *changed,
2023 			 struct netlink_ext_ack *extack);
2024 
2025 #ifdef CONFIG_SYSFS
2026 /* br_sysfs_if.c */
2027 extern const struct sysfs_ops brport_sysfs_ops;
2028 int br_sysfs_addif(struct net_bridge_port *p);
2029 int br_sysfs_renameif(struct net_bridge_port *p);
2030 
2031 /* br_sysfs_br.c */
2032 int br_sysfs_addbr(struct net_device *dev);
2033 void br_sysfs_delbr(struct net_device *dev);
2034 
2035 #else
2036 
2037 static inline int br_sysfs_addif(struct net_bridge_port *p) { return 0; }
2038 static inline int br_sysfs_renameif(struct net_bridge_port *p) { return 0; }
2039 static inline int br_sysfs_addbr(struct net_device *dev) { return 0; }
2040 static inline void br_sysfs_delbr(struct net_device *dev) { return; }
2041 #endif /* CONFIG_SYSFS */
2042 
2043 /* br_switchdev.c */
2044 #ifdef CONFIG_NET_SWITCHDEV
2045 int br_switchdev_port_offload(struct net_bridge_port *p,
2046 			      struct net_device *dev, const void *ctx,
2047 			      struct notifier_block *atomic_nb,
2048 			      struct notifier_block *blocking_nb,
2049 			      bool tx_fwd_offload,
2050 			      struct netlink_ext_ack *extack);
2051 
2052 void br_switchdev_port_unoffload(struct net_bridge_port *p, const void *ctx,
2053 				 struct notifier_block *atomic_nb,
2054 				 struct notifier_block *blocking_nb);
2055 
2056 bool br_switchdev_frame_uses_tx_fwd_offload(struct sk_buff *skb);
2057 
2058 void br_switchdev_frame_set_offload_fwd_mark(struct sk_buff *skb);
2059 
2060 void nbp_switchdev_frame_mark_tx_fwd_offload(const struct net_bridge_port *p,
2061 					     struct sk_buff *skb);
2062 void nbp_switchdev_frame_mark_tx_fwd_to_hwdom(const struct net_bridge_port *p,
2063 					      struct sk_buff *skb);
2064 void nbp_switchdev_frame_mark(const struct net_bridge_port *p,
2065 			      struct sk_buff *skb);
2066 bool nbp_switchdev_allowed_egress(const struct net_bridge_port *p,
2067 				  const struct sk_buff *skb);
2068 int br_switchdev_set_port_flag(struct net_bridge_port *p,
2069 			       unsigned long flags,
2070 			       unsigned long mask,
2071 			       struct netlink_ext_ack *extack);
2072 void br_switchdev_fdb_notify(struct net_bridge *br,
2073 			     const struct net_bridge_fdb_entry *fdb, int type);
2074 void br_switchdev_mdb_notify(struct net_device *dev,
2075 			     struct net_bridge_mdb_entry *mp,
2076 			     struct net_bridge_port_group *pg,
2077 			     int type);
2078 int br_switchdev_port_vlan_add(struct net_device *dev, u16 vid, u16 flags,
2079 			       bool changed, struct netlink_ext_ack *extack);
2080 int br_switchdev_port_vlan_del(struct net_device *dev, u16 vid);
2081 void br_switchdev_init(struct net_bridge *br);
2082 
2083 static inline void br_switchdev_frame_unmark(struct sk_buff *skb)
2084 {
2085 	skb->offload_fwd_mark = 0;
2086 }
2087 #else
2088 static inline int
2089 br_switchdev_port_offload(struct net_bridge_port *p,
2090 			  struct net_device *dev, const void *ctx,
2091 			  struct notifier_block *atomic_nb,
2092 			  struct notifier_block *blocking_nb,
2093 			  bool tx_fwd_offload,
2094 			  struct netlink_ext_ack *extack)
2095 {
2096 	return -EOPNOTSUPP;
2097 }
2098 
2099 static inline void
2100 br_switchdev_port_unoffload(struct net_bridge_port *p, const void *ctx,
2101 			    struct notifier_block *atomic_nb,
2102 			    struct notifier_block *blocking_nb)
2103 {
2104 }
2105 
2106 static inline bool br_switchdev_frame_uses_tx_fwd_offload(struct sk_buff *skb)
2107 {
2108 	return false;
2109 }
2110 
2111 static inline void br_switchdev_frame_set_offload_fwd_mark(struct sk_buff *skb)
2112 {
2113 }
2114 
2115 static inline void
2116 nbp_switchdev_frame_mark_tx_fwd_offload(const struct net_bridge_port *p,
2117 					struct sk_buff *skb)
2118 {
2119 }
2120 
2121 static inline void
2122 nbp_switchdev_frame_mark_tx_fwd_to_hwdom(const struct net_bridge_port *p,
2123 					 struct sk_buff *skb)
2124 {
2125 }
2126 
2127 static inline void nbp_switchdev_frame_mark(const struct net_bridge_port *p,
2128 					    struct sk_buff *skb)
2129 {
2130 }
2131 
2132 static inline bool nbp_switchdev_allowed_egress(const struct net_bridge_port *p,
2133 						const struct sk_buff *skb)
2134 {
2135 	return true;
2136 }
2137 
2138 static inline int br_switchdev_set_port_flag(struct net_bridge_port *p,
2139 					     unsigned long flags,
2140 					     unsigned long mask,
2141 					     struct netlink_ext_ack *extack)
2142 {
2143 	return 0;
2144 }
2145 
2146 static inline int br_switchdev_port_vlan_add(struct net_device *dev, u16 vid,
2147 					     u16 flags, bool changed,
2148 					     struct netlink_ext_ack *extack)
2149 {
2150 	return -EOPNOTSUPP;
2151 }
2152 
2153 static inline int br_switchdev_port_vlan_del(struct net_device *dev, u16 vid)
2154 {
2155 	return -EOPNOTSUPP;
2156 }
2157 
2158 static inline void
2159 br_switchdev_fdb_notify(struct net_bridge *br,
2160 			const struct net_bridge_fdb_entry *fdb, int type)
2161 {
2162 }
2163 
2164 static inline void br_switchdev_mdb_notify(struct net_device *dev,
2165 					   struct net_bridge_mdb_entry *mp,
2166 					   struct net_bridge_port_group *pg,
2167 					   int type)
2168 {
2169 }
2170 
2171 static inline void br_switchdev_frame_unmark(struct sk_buff *skb)
2172 {
2173 }
2174 
2175 static inline void br_switchdev_init(struct net_bridge *br)
2176 {
2177 }
2178 
2179 #endif /* CONFIG_NET_SWITCHDEV */
2180 
2181 /* br_arp_nd_proxy.c */
2182 void br_recalculate_neigh_suppress_enabled(struct net_bridge *br);
2183 void br_do_proxy_suppress_arp(struct sk_buff *skb, struct net_bridge *br,
2184 			      u16 vid, struct net_bridge_port *p);
2185 void br_do_suppress_nd(struct sk_buff *skb, struct net_bridge *br,
2186 		       u16 vid, struct net_bridge_port *p, struct nd_msg *msg);
2187 struct nd_msg *br_is_nd_neigh_msg(struct sk_buff *skb, struct nd_msg *m);
2188 #endif
2189