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