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