xref: /openbmc/linux/net/bridge/br_private.h (revision 9632233e)
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 
32 #define BR_VERSION	"2.3"
33 
34 /* Control of forwarding link local multicast */
35 #define BR_GROUPFWD_DEFAULT	0
36 /* Don't allow forwarding of control protocols like STP, MAC PAUSE and LACP */
37 enum {
38 	BR_GROUPFWD_STP		= BIT(0),
39 	BR_GROUPFWD_MACPAUSE	= BIT(1),
40 	BR_GROUPFWD_LACP	= BIT(2),
41 };
42 
43 #define BR_GROUPFWD_RESTRICTED (BR_GROUPFWD_STP | BR_GROUPFWD_MACPAUSE | \
44 				BR_GROUPFWD_LACP)
45 /* The Nearest Customer Bridge Group Address, 01-80-C2-00-00-[00,0B,0C,0D,0F] */
46 #define BR_GROUPFWD_8021AD	0xB801u
47 
48 /* Path to usermode spanning tree program */
49 #define BR_STP_PROG	"/sbin/bridge-stp"
50 
51 #define BR_FDB_NOTIFY_SETTABLE_BITS (FDB_NOTIFY_BIT | FDB_NOTIFY_INACTIVE_BIT)
52 
53 typedef struct bridge_id bridge_id;
54 typedef struct mac_addr mac_addr;
55 typedef __u16 port_id;
56 
57 struct bridge_id {
58 	unsigned char	prio[2];
59 	unsigned char	addr[ETH_ALEN];
60 };
61 
62 struct mac_addr {
63 	unsigned char	addr[ETH_ALEN];
64 };
65 
66 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
67 /* our own querier */
68 struct bridge_mcast_own_query {
69 	struct timer_list	timer;
70 	u32			startup_sent;
71 };
72 
73 /* other querier */
74 struct bridge_mcast_other_query {
75 	struct timer_list		timer;
76 	unsigned long			delay_time;
77 };
78 
79 /* selected querier */
80 struct bridge_mcast_querier {
81 	struct br_ip addr;
82 	struct net_bridge_port __rcu	*port;
83 };
84 
85 /* IGMP/MLD statistics */
86 struct bridge_mcast_stats {
87 	struct br_mcast_stats mstats;
88 	struct u64_stats_sync syncp;
89 };
90 #endif
91 
92 /* net_bridge_mcast_port must be always defined due to forwarding stubs */
93 struct net_bridge_mcast_port {
94 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
95 	struct net_bridge_port		*port;
96 
97 	struct bridge_mcast_own_query	ip4_own_query;
98 	struct timer_list		ip4_mc_router_timer;
99 	struct hlist_node		ip4_rlist;
100 #if IS_ENABLED(CONFIG_IPV6)
101 	struct bridge_mcast_own_query	ip6_own_query;
102 	struct timer_list		ip6_mc_router_timer;
103 	struct hlist_node		ip6_rlist;
104 #endif /* IS_ENABLED(CONFIG_IPV6) */
105 	unsigned char			multicast_router;
106 #endif /* CONFIG_BRIDGE_IGMP_SNOOPING */
107 };
108 
109 struct br_tunnel_info {
110 	__be64				tunnel_id;
111 	struct metadata_dst __rcu	*tunnel_dst;
112 };
113 
114 /* private vlan flags */
115 enum {
116 	BR_VLFLAG_PER_PORT_STATS = BIT(0),
117 	BR_VLFLAG_ADDED_BY_SWITCHDEV = BIT(1),
118 };
119 
120 /**
121  * struct net_bridge_vlan - per-vlan entry
122  *
123  * @vnode: rhashtable member
124  * @vid: VLAN id
125  * @flags: bridge vlan flags
126  * @priv_flags: private (in-kernel) bridge vlan flags
127  * @state: STP state (e.g. blocking, learning, forwarding)
128  * @stats: per-cpu VLAN statistics
129  * @br: if MASTER flag set, this points to a bridge struct
130  * @port: if MASTER flag unset, this points to a port struct
131  * @refcnt: if MASTER flag set, this is bumped for each port referencing it
132  * @brvlan: if MASTER flag unset, this points to the global per-VLAN context
133  *          for this VLAN entry
134  * @vlist: sorted list of VLAN entries
135  * @rcu: used for entry destruction
136  *
137  * This structure is shared between the global per-VLAN entries contained in
138  * the bridge rhashtable and the local per-port per-VLAN entries contained in
139  * the port's rhashtable. The union entries should be interpreted depending on
140  * the entry flags that are set.
141  */
142 struct net_bridge_vlan {
143 	struct rhash_head		vnode;
144 	struct rhash_head		tnode;
145 	u16				vid;
146 	u16				flags;
147 	u16				priv_flags;
148 	u8				state;
149 	struct pcpu_sw_netstats __percpu *stats;
150 	union {
151 		struct net_bridge	*br;
152 		struct net_bridge_port	*port;
153 	};
154 	union {
155 		refcount_t		refcnt;
156 		struct net_bridge_vlan	*brvlan;
157 	};
158 
159 	struct br_tunnel_info		tinfo;
160 
161 	struct list_head		vlist;
162 
163 	struct rcu_head			rcu;
164 };
165 
166 /**
167  * struct net_bridge_vlan_group
168  *
169  * @vlan_hash: VLAN entry rhashtable
170  * @vlan_list: sorted VLAN entry list
171  * @num_vlans: number of total VLAN entries
172  * @pvid: PVID VLAN id
173  * @pvid_state: PVID's STP state (e.g. forwarding, learning, blocking)
174  *
175  * IMPORTANT: Be careful when checking if there're VLAN entries using list
176  *            primitives because the bridge can have entries in its list which
177  *            are just for global context but not for filtering, i.e. they have
178  *            the master flag set but not the brentry flag. If you have to check
179  *            if there're "real" entries in the bridge please test @num_vlans
180  */
181 struct net_bridge_vlan_group {
182 	struct rhashtable		vlan_hash;
183 	struct rhashtable		tunnel_hash;
184 	struct list_head		vlan_list;
185 	u16				num_vlans;
186 	u16				pvid;
187 	u8				pvid_state;
188 };
189 
190 /* bridge fdb flags */
191 enum {
192 	BR_FDB_LOCAL,
193 	BR_FDB_STATIC,
194 	BR_FDB_STICKY,
195 	BR_FDB_ADDED_BY_USER,
196 	BR_FDB_ADDED_BY_EXT_LEARN,
197 	BR_FDB_OFFLOADED,
198 	BR_FDB_NOTIFY,
199 	BR_FDB_NOTIFY_INACTIVE
200 };
201 
202 struct net_bridge_fdb_key {
203 	mac_addr addr;
204 	u16 vlan_id;
205 };
206 
207 struct net_bridge_fdb_entry {
208 	struct rhash_head		rhnode;
209 	struct net_bridge_port		*dst;
210 
211 	struct net_bridge_fdb_key	key;
212 	struct hlist_node		fdb_node;
213 	unsigned long			flags;
214 
215 	/* write-heavy members should not affect lookups */
216 	unsigned long			updated ____cacheline_aligned_in_smp;
217 	unsigned long			used;
218 
219 	struct rcu_head			rcu;
220 };
221 
222 #define MDB_PG_FLAGS_PERMANENT	BIT(0)
223 #define MDB_PG_FLAGS_OFFLOAD	BIT(1)
224 #define MDB_PG_FLAGS_FAST_LEAVE	BIT(2)
225 #define MDB_PG_FLAGS_STAR_EXCL	BIT(3)
226 #define MDB_PG_FLAGS_BLOCKED	BIT(4)
227 
228 #define PG_SRC_ENT_LIMIT	32
229 
230 #define BR_SGRP_F_DELETE	BIT(0)
231 #define BR_SGRP_F_SEND		BIT(1)
232 #define BR_SGRP_F_INSTALLED	BIT(2)
233 
234 struct net_bridge_mcast_gc {
235 	struct hlist_node		gc_node;
236 	void				(*destroy)(struct net_bridge_mcast_gc *gc);
237 };
238 
239 struct net_bridge_group_src {
240 	struct hlist_node		node;
241 
242 	struct br_ip			addr;
243 	struct net_bridge_port_group	*pg;
244 	u8				flags;
245 	u8				src_query_rexmit_cnt;
246 	struct timer_list		timer;
247 
248 	struct net_bridge		*br;
249 	struct net_bridge_mcast_gc	mcast_gc;
250 	struct rcu_head			rcu;
251 };
252 
253 struct net_bridge_port_group_sg_key {
254 	struct net_bridge_port		*port;
255 	struct br_ip			addr;
256 };
257 
258 struct net_bridge_port_group {
259 	struct net_bridge_port_group __rcu *next;
260 	struct net_bridge_port_group_sg_key key;
261 	unsigned char			eth_addr[ETH_ALEN] __aligned(2);
262 	unsigned char			flags;
263 	unsigned char			filter_mode;
264 	unsigned char			grp_query_rexmit_cnt;
265 	unsigned char			rt_protocol;
266 
267 	struct hlist_head		src_list;
268 	unsigned int			src_ents;
269 	struct timer_list		timer;
270 	struct timer_list		rexmit_timer;
271 	struct hlist_node		mglist;
272 	struct rb_root			eht_set_tree;
273 	struct rb_root			eht_host_tree;
274 
275 	struct rhash_head		rhnode;
276 	struct net_bridge_mcast_gc	mcast_gc;
277 	struct rcu_head			rcu;
278 };
279 
280 struct net_bridge_mdb_entry {
281 	struct rhash_head		rhnode;
282 	struct net_bridge		*br;
283 	struct net_bridge_port_group __rcu *ports;
284 	struct br_ip			addr;
285 	bool				host_joined;
286 
287 	struct timer_list		timer;
288 	struct hlist_node		mdb_node;
289 
290 	struct net_bridge_mcast_gc	mcast_gc;
291 	struct rcu_head			rcu;
292 };
293 
294 struct net_bridge_port {
295 	struct net_bridge		*br;
296 	struct net_device		*dev;
297 	struct list_head		list;
298 
299 	unsigned long			flags;
300 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
301 	struct net_bridge_vlan_group	__rcu *vlgrp;
302 #endif
303 	struct net_bridge_port		__rcu *backup_port;
304 
305 	/* STP */
306 	u8				priority;
307 	u8				state;
308 	u16				port_no;
309 	unsigned char			topology_change_ack;
310 	unsigned char			config_pending;
311 	port_id				port_id;
312 	port_id				designated_port;
313 	bridge_id			designated_root;
314 	bridge_id			designated_bridge;
315 	u32				path_cost;
316 	u32				designated_cost;
317 	unsigned long			designated_age;
318 
319 	struct timer_list		forward_delay_timer;
320 	struct timer_list		hold_timer;
321 	struct timer_list		message_age_timer;
322 	struct kobject			kobj;
323 	struct rcu_head			rcu;
324 
325 	struct net_bridge_mcast_port	multicast_ctx;
326 
327 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
328 	struct bridge_mcast_stats	__percpu *mcast_stats;
329 
330 	u32				multicast_eht_hosts_limit;
331 	u32				multicast_eht_hosts_cnt;
332 	struct hlist_head		mglist;
333 #endif
334 
335 #ifdef CONFIG_SYSFS
336 	char				sysfs_name[IFNAMSIZ];
337 #endif
338 
339 #ifdef CONFIG_NET_POLL_CONTROLLER
340 	struct netpoll			*np;
341 #endif
342 #ifdef CONFIG_NET_SWITCHDEV
343 	int				offload_fwd_mark;
344 #endif
345 	u16				group_fwd_mask;
346 	u16				backup_redirected_cnt;
347 
348 	struct bridge_stp_xstats	stp_xstats;
349 };
350 
351 #define kobj_to_brport(obj)	container_of(obj, struct net_bridge_port, kobj)
352 
353 #define br_auto_port(p) ((p)->flags & BR_AUTO_MASK)
354 #define br_promisc_port(p) ((p)->flags & BR_PROMISC)
355 
356 static inline struct net_bridge_port *br_port_get_rcu(const struct net_device *dev)
357 {
358 	return rcu_dereference(dev->rx_handler_data);
359 }
360 
361 static inline struct net_bridge_port *br_port_get_rtnl(const struct net_device *dev)
362 {
363 	return netif_is_bridge_port(dev) ?
364 		rtnl_dereference(dev->rx_handler_data) : NULL;
365 }
366 
367 static inline struct net_bridge_port *br_port_get_rtnl_rcu(const struct net_device *dev)
368 {
369 	return netif_is_bridge_port(dev) ?
370 		rcu_dereference_rtnl(dev->rx_handler_data) : NULL;
371 }
372 
373 enum net_bridge_opts {
374 	BROPT_VLAN_ENABLED,
375 	BROPT_VLAN_STATS_ENABLED,
376 	BROPT_NF_CALL_IPTABLES,
377 	BROPT_NF_CALL_IP6TABLES,
378 	BROPT_NF_CALL_ARPTABLES,
379 	BROPT_GROUP_ADDR_SET,
380 	BROPT_MULTICAST_ENABLED,
381 	BROPT_MULTICAST_QUERIER,
382 	BROPT_MULTICAST_QUERY_USE_IFADDR,
383 	BROPT_MULTICAST_STATS_ENABLED,
384 	BROPT_HAS_IPV6_ADDR,
385 	BROPT_NEIGH_SUPPRESS_ENABLED,
386 	BROPT_MTU_SET_BY_USER,
387 	BROPT_VLAN_STATS_PER_PORT,
388 	BROPT_NO_LL_LEARN,
389 	BROPT_VLAN_BRIDGE_BINDING,
390 };
391 
392 struct net_bridge {
393 	spinlock_t			lock;
394 	spinlock_t			hash_lock;
395 	struct hlist_head		frame_type_list;
396 	struct net_device		*dev;
397 	unsigned long			options;
398 	/* These fields are accessed on each packet */
399 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
400 	__be16				vlan_proto;
401 	u16				default_pvid;
402 	struct net_bridge_vlan_group	__rcu *vlgrp;
403 #endif
404 
405 	struct rhashtable		fdb_hash_tbl;
406 	struct list_head		port_list;
407 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
408 	union {
409 		struct rtable		fake_rtable;
410 		struct rt6_info		fake_rt6_info;
411 	};
412 #endif
413 	u16				group_fwd_mask;
414 	u16				group_fwd_mask_required;
415 
416 	/* STP */
417 	bridge_id			designated_root;
418 	bridge_id			bridge_id;
419 	unsigned char			topology_change;
420 	unsigned char			topology_change_detected;
421 	u16				root_port;
422 	unsigned long			max_age;
423 	unsigned long			hello_time;
424 	unsigned long			forward_delay;
425 	unsigned long			ageing_time;
426 	unsigned long			bridge_max_age;
427 	unsigned long			bridge_hello_time;
428 	unsigned long			bridge_forward_delay;
429 	unsigned long			bridge_ageing_time;
430 	u32				root_path_cost;
431 
432 	u8				group_addr[ETH_ALEN];
433 
434 	enum {
435 		BR_NO_STP, 		/* no spanning tree */
436 		BR_KERNEL_STP,		/* old STP in kernel */
437 		BR_USER_STP,		/* new RSTP in userspace */
438 	} stp_enabled;
439 
440 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
441 
442 	u32				hash_max;
443 
444 	u32				multicast_last_member_count;
445 	u32				multicast_startup_query_count;
446 
447 	u8				multicast_igmp_version;
448 	u8				multicast_router;
449 #if IS_ENABLED(CONFIG_IPV6)
450 	u8				multicast_mld_version;
451 #endif
452 	spinlock_t			multicast_lock;
453 	unsigned long			multicast_last_member_interval;
454 	unsigned long			multicast_membership_interval;
455 	unsigned long			multicast_querier_interval;
456 	unsigned long			multicast_query_interval;
457 	unsigned long			multicast_query_response_interval;
458 	unsigned long			multicast_startup_query_interval;
459 
460 	struct rhashtable		mdb_hash_tbl;
461 	struct rhashtable		sg_port_tbl;
462 
463 	struct hlist_head		mcast_gc_list;
464 	struct hlist_head		mdb_list;
465 
466 	struct hlist_head		ip4_mc_router_list;
467 	struct timer_list		ip4_mc_router_timer;
468 	struct bridge_mcast_other_query	ip4_other_query;
469 	struct bridge_mcast_own_query	ip4_own_query;
470 	struct bridge_mcast_querier	ip4_querier;
471 	struct bridge_mcast_stats	__percpu *mcast_stats;
472 #if IS_ENABLED(CONFIG_IPV6)
473 	struct hlist_head		ip6_mc_router_list;
474 	struct timer_list		ip6_mc_router_timer;
475 	struct bridge_mcast_other_query	ip6_other_query;
476 	struct bridge_mcast_own_query	ip6_own_query;
477 	struct bridge_mcast_querier	ip6_querier;
478 #endif /* IS_ENABLED(CONFIG_IPV6) */
479 	struct work_struct		mcast_gc_work;
480 #endif
481 
482 	struct timer_list		hello_timer;
483 	struct timer_list		tcn_timer;
484 	struct timer_list		topology_change_timer;
485 	struct delayed_work		gc_work;
486 	struct kobject			*ifobj;
487 	u32				auto_cnt;
488 
489 #ifdef CONFIG_NET_SWITCHDEV
490 	int offload_fwd_mark;
491 #endif
492 	struct hlist_head		fdb_list;
493 
494 #if IS_ENABLED(CONFIG_BRIDGE_MRP)
495 	struct hlist_head		mrp_list;
496 #endif
497 #if IS_ENABLED(CONFIG_BRIDGE_CFM)
498 	struct hlist_head		mep_list;
499 #endif
500 };
501 
502 struct br_input_skb_cb {
503 	struct net_device *brdev;
504 
505 	u16 frag_max_size;
506 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
507 	u8 igmp;
508 	u8 mrouters_only:1;
509 #endif
510 	u8 proxyarp_replied:1;
511 	u8 src_port_isolated:1;
512 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
513 	u8 vlan_filtered:1;
514 #endif
515 #ifdef CONFIG_NETFILTER_FAMILY_BRIDGE
516 	u8 br_netfilter_broute:1;
517 #endif
518 
519 #ifdef CONFIG_NET_SWITCHDEV
520 	int offload_fwd_mark;
521 #endif
522 };
523 
524 #define BR_INPUT_SKB_CB(__skb)	((struct br_input_skb_cb *)(__skb)->cb)
525 
526 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
527 # define BR_INPUT_SKB_CB_MROUTERS_ONLY(__skb)	(BR_INPUT_SKB_CB(__skb)->mrouters_only)
528 #else
529 # define BR_INPUT_SKB_CB_MROUTERS_ONLY(__skb)	(0)
530 #endif
531 
532 #define br_printk(level, br, format, args...)	\
533 	printk(level "%s: " format, (br)->dev->name, ##args)
534 
535 #define br_err(__br, format, args...)			\
536 	br_printk(KERN_ERR, __br, format, ##args)
537 #define br_warn(__br, format, args...)			\
538 	br_printk(KERN_WARNING, __br, format, ##args)
539 #define br_notice(__br, format, args...)		\
540 	br_printk(KERN_NOTICE, __br, format, ##args)
541 #define br_info(__br, format, args...)			\
542 	br_printk(KERN_INFO, __br, format, ##args)
543 
544 #define br_debug(br, format, args...)			\
545 	pr_debug("%s: " format,  (br)->dev->name, ##args)
546 
547 /* called under bridge lock */
548 static inline int br_is_root_bridge(const struct net_bridge *br)
549 {
550 	return !memcmp(&br->bridge_id, &br->designated_root, 8);
551 }
552 
553 /* check if a VLAN entry is global */
554 static inline bool br_vlan_is_master(const struct net_bridge_vlan *v)
555 {
556 	return v->flags & BRIDGE_VLAN_INFO_MASTER;
557 }
558 
559 /* check if a VLAN entry is used by the bridge */
560 static inline bool br_vlan_is_brentry(const struct net_bridge_vlan *v)
561 {
562 	return v->flags & BRIDGE_VLAN_INFO_BRENTRY;
563 }
564 
565 /* check if we should use the vlan entry, returns false if it's only context */
566 static inline bool br_vlan_should_use(const struct net_bridge_vlan *v)
567 {
568 	if (br_vlan_is_master(v)) {
569 		if (br_vlan_is_brentry(v))
570 			return true;
571 		else
572 			return false;
573 	}
574 
575 	return true;
576 }
577 
578 static inline bool nbp_state_should_learn(const struct net_bridge_port *p)
579 {
580 	return p->state == BR_STATE_LEARNING || p->state == BR_STATE_FORWARDING;
581 }
582 
583 static inline bool br_vlan_valid_id(u16 vid, struct netlink_ext_ack *extack)
584 {
585 	bool ret = vid > 0 && vid < VLAN_VID_MASK;
586 
587 	if (!ret)
588 		NL_SET_ERR_MSG_MOD(extack, "Vlan id is invalid");
589 
590 	return ret;
591 }
592 
593 static inline bool br_vlan_valid_range(const struct bridge_vlan_info *cur,
594 				       const struct bridge_vlan_info *last,
595 				       struct netlink_ext_ack *extack)
596 {
597 	/* pvid flag is not allowed in ranges */
598 	if (cur->flags & BRIDGE_VLAN_INFO_PVID) {
599 		NL_SET_ERR_MSG_MOD(extack, "Pvid isn't allowed in a range");
600 		return false;
601 	}
602 
603 	/* when cur is the range end, check if:
604 	 *  - it has range start flag
605 	 *  - range ids are invalid (end is equal to or before start)
606 	 */
607 	if (last) {
608 		if (cur->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
609 			NL_SET_ERR_MSG_MOD(extack, "Found a new vlan range start while processing one");
610 			return false;
611 		} else if (!(cur->flags & BRIDGE_VLAN_INFO_RANGE_END)) {
612 			NL_SET_ERR_MSG_MOD(extack, "Vlan range end flag is missing");
613 			return false;
614 		} else if (cur->vid <= last->vid) {
615 			NL_SET_ERR_MSG_MOD(extack, "End vlan id is less than or equal to start vlan id");
616 			return false;
617 		}
618 	}
619 
620 	/* check for required range flags */
621 	if (!(cur->flags & (BRIDGE_VLAN_INFO_RANGE_BEGIN |
622 			    BRIDGE_VLAN_INFO_RANGE_END))) {
623 		NL_SET_ERR_MSG_MOD(extack, "Both vlan range flags are missing");
624 		return false;
625 	}
626 
627 	return true;
628 }
629 
630 static inline int br_afspec_cmd_to_rtm(int cmd)
631 {
632 	switch (cmd) {
633 	case RTM_SETLINK:
634 		return RTM_NEWVLAN;
635 	case RTM_DELLINK:
636 		return RTM_DELVLAN;
637 	}
638 
639 	return 0;
640 }
641 
642 static inline int br_opt_get(const struct net_bridge *br,
643 			     enum net_bridge_opts opt)
644 {
645 	return test_bit(opt, &br->options);
646 }
647 
648 int br_boolopt_toggle(struct net_bridge *br, enum br_boolopt_id opt, bool on,
649 		      struct netlink_ext_ack *extack);
650 int br_boolopt_get(const struct net_bridge *br, enum br_boolopt_id opt);
651 int br_boolopt_multi_toggle(struct net_bridge *br,
652 			    struct br_boolopt_multi *bm,
653 			    struct netlink_ext_ack *extack);
654 void br_boolopt_multi_get(const struct net_bridge *br,
655 			  struct br_boolopt_multi *bm);
656 void br_opt_toggle(struct net_bridge *br, enum net_bridge_opts opt, bool on);
657 
658 /* br_device.c */
659 void br_dev_setup(struct net_device *dev);
660 void br_dev_delete(struct net_device *dev, struct list_head *list);
661 netdev_tx_t br_dev_xmit(struct sk_buff *skb, struct net_device *dev);
662 #ifdef CONFIG_NET_POLL_CONTROLLER
663 static inline void br_netpoll_send_skb(const struct net_bridge_port *p,
664 				       struct sk_buff *skb)
665 {
666 	netpoll_send_skb(p->np, skb);
667 }
668 
669 int br_netpoll_enable(struct net_bridge_port *p);
670 void br_netpoll_disable(struct net_bridge_port *p);
671 #else
672 static inline void br_netpoll_send_skb(const struct net_bridge_port *p,
673 				       struct sk_buff *skb)
674 {
675 }
676 
677 static inline int br_netpoll_enable(struct net_bridge_port *p)
678 {
679 	return 0;
680 }
681 
682 static inline void br_netpoll_disable(struct net_bridge_port *p)
683 {
684 }
685 #endif
686 
687 /* br_fdb.c */
688 int br_fdb_init(void);
689 void br_fdb_fini(void);
690 int br_fdb_hash_init(struct net_bridge *br);
691 void br_fdb_hash_fini(struct net_bridge *br);
692 void br_fdb_flush(struct net_bridge *br);
693 void br_fdb_find_delete_local(struct net_bridge *br,
694 			      const struct net_bridge_port *p,
695 			      const unsigned char *addr, u16 vid);
696 void br_fdb_changeaddr(struct net_bridge_port *p, const unsigned char *newaddr);
697 void br_fdb_change_mac_address(struct net_bridge *br, const u8 *newaddr);
698 void br_fdb_cleanup(struct work_struct *work);
699 void br_fdb_delete_by_port(struct net_bridge *br,
700 			   const struct net_bridge_port *p, u16 vid, int do_all);
701 struct net_bridge_fdb_entry *br_fdb_find_rcu(struct net_bridge *br,
702 					     const unsigned char *addr,
703 					     __u16 vid);
704 int br_fdb_test_addr(struct net_device *dev, unsigned char *addr);
705 int br_fdb_fillbuf(struct net_bridge *br, void *buf, unsigned long count,
706 		   unsigned long off);
707 int br_fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
708 		  const unsigned char *addr, u16 vid);
709 void br_fdb_update(struct net_bridge *br, struct net_bridge_port *source,
710 		   const unsigned char *addr, u16 vid, unsigned long flags);
711 
712 int br_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
713 		  struct net_device *dev, const unsigned char *addr, u16 vid);
714 int br_fdb_add(struct ndmsg *nlh, struct nlattr *tb[], struct net_device *dev,
715 	       const unsigned char *addr, u16 vid, u16 nlh_flags,
716 	       struct netlink_ext_ack *extack);
717 int br_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
718 		struct net_device *dev, struct net_device *fdev, int *idx);
719 int br_fdb_get(struct sk_buff *skb, struct nlattr *tb[], struct net_device *dev,
720 	       const unsigned char *addr, u16 vid, u32 portid, u32 seq,
721 	       struct netlink_ext_ack *extack);
722 int br_fdb_sync_static(struct net_bridge *br, struct net_bridge_port *p);
723 void br_fdb_unsync_static(struct net_bridge *br, struct net_bridge_port *p);
724 int br_fdb_external_learn_add(struct net_bridge *br, struct net_bridge_port *p,
725 			      const unsigned char *addr, u16 vid,
726 			      bool swdev_notify);
727 int br_fdb_external_learn_del(struct net_bridge *br, struct net_bridge_port *p,
728 			      const unsigned char *addr, u16 vid,
729 			      bool swdev_notify);
730 void br_fdb_offloaded_set(struct net_bridge *br, struct net_bridge_port *p,
731 			  const unsigned char *addr, u16 vid, bool offloaded);
732 
733 /* br_forward.c */
734 enum br_pkt_type {
735 	BR_PKT_UNICAST,
736 	BR_PKT_MULTICAST,
737 	BR_PKT_BROADCAST
738 };
739 int br_dev_queue_push_xmit(struct net *net, struct sock *sk, struct sk_buff *skb);
740 void br_forward(const struct net_bridge_port *to, struct sk_buff *skb,
741 		bool local_rcv, bool local_orig);
742 int br_forward_finish(struct net *net, struct sock *sk, struct sk_buff *skb);
743 void br_flood(struct net_bridge *br, struct sk_buff *skb,
744 	      enum br_pkt_type pkt_type, bool local_rcv, bool local_orig);
745 
746 /* return true if both source port and dest port are isolated */
747 static inline bool br_skb_isolated(const struct net_bridge_port *to,
748 				   const struct sk_buff *skb)
749 {
750 	return BR_INPUT_SKB_CB(skb)->src_port_isolated &&
751 	       (to->flags & BR_ISOLATED);
752 }
753 
754 /* br_if.c */
755 void br_port_carrier_check(struct net_bridge_port *p, bool *notified);
756 int br_add_bridge(struct net *net, const char *name);
757 int br_del_bridge(struct net *net, const char *name);
758 int br_add_if(struct net_bridge *br, struct net_device *dev,
759 	      struct netlink_ext_ack *extack);
760 int br_del_if(struct net_bridge *br, struct net_device *dev);
761 void br_mtu_auto_adjust(struct net_bridge *br);
762 netdev_features_t br_features_recompute(struct net_bridge *br,
763 					netdev_features_t features);
764 void br_port_flags_change(struct net_bridge_port *port, unsigned long mask);
765 void br_manage_promisc(struct net_bridge *br);
766 int nbp_backup_change(struct net_bridge_port *p, struct net_device *backup_dev);
767 
768 /* br_input.c */
769 int br_handle_frame_finish(struct net *net, struct sock *sk, struct sk_buff *skb);
770 rx_handler_func_t *br_get_rx_handler(const struct net_device *dev);
771 
772 struct br_frame_type {
773 	__be16			type;
774 	int			(*frame_handler)(struct net_bridge_port *port,
775 						 struct sk_buff *skb);
776 	struct hlist_node	list;
777 };
778 
779 void br_add_frame(struct net_bridge *br, struct br_frame_type *ft);
780 void br_del_frame(struct net_bridge *br, struct br_frame_type *ft);
781 
782 static inline bool br_rx_handler_check_rcu(const struct net_device *dev)
783 {
784 	return rcu_dereference(dev->rx_handler) == br_get_rx_handler(dev);
785 }
786 
787 static inline bool br_rx_handler_check_rtnl(const struct net_device *dev)
788 {
789 	return rcu_dereference_rtnl(dev->rx_handler) == br_get_rx_handler(dev);
790 }
791 
792 static inline struct net_bridge_port *br_port_get_check_rcu(const struct net_device *dev)
793 {
794 	return br_rx_handler_check_rcu(dev) ? br_port_get_rcu(dev) : NULL;
795 }
796 
797 static inline struct net_bridge_port *
798 br_port_get_check_rtnl(const struct net_device *dev)
799 {
800 	return br_rx_handler_check_rtnl(dev) ? br_port_get_rtnl_rcu(dev) : NULL;
801 }
802 
803 /* br_ioctl.c */
804 int br_dev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
805 int br_ioctl_deviceless_stub(struct net *net, unsigned int cmd,
806 			     void __user *arg);
807 
808 /* br_multicast.c */
809 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
810 int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port,
811 		     struct sk_buff *skb, u16 vid);
812 struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br,
813 					struct sk_buff *skb, u16 vid);
814 int br_multicast_add_port(struct net_bridge_port *port);
815 void br_multicast_del_port(struct net_bridge_port *port);
816 void br_multicast_enable_port(struct net_bridge_port *port);
817 void br_multicast_disable_port(struct net_bridge_port *port);
818 void br_multicast_init(struct net_bridge *br);
819 void br_multicast_join_snoopers(struct net_bridge *br);
820 void br_multicast_leave_snoopers(struct net_bridge *br);
821 void br_multicast_open(struct net_bridge *br);
822 void br_multicast_stop(struct net_bridge *br);
823 void br_multicast_dev_del(struct net_bridge *br);
824 void br_multicast_flood(struct net_bridge_mdb_entry *mdst,
825 			struct sk_buff *skb, bool local_rcv, bool local_orig);
826 int br_multicast_set_router(struct net_bridge *br, unsigned long val);
827 int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val);
828 int br_multicast_toggle(struct net_bridge *br, unsigned long val,
829 			struct netlink_ext_ack *extack);
830 int br_multicast_set_querier(struct net_bridge *br, unsigned long val);
831 int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val);
832 int br_multicast_set_igmp_version(struct net_bridge *br, unsigned long val);
833 #if IS_ENABLED(CONFIG_IPV6)
834 int br_multicast_set_mld_version(struct net_bridge *br, unsigned long val);
835 #endif
836 struct net_bridge_mdb_entry *
837 br_mdb_ip_get(struct net_bridge *br, struct br_ip *dst);
838 struct net_bridge_mdb_entry *
839 br_multicast_new_group(struct net_bridge *br, struct br_ip *group);
840 struct net_bridge_port_group *
841 br_multicast_new_port_group(struct net_bridge_port *port, struct br_ip *group,
842 			    struct net_bridge_port_group __rcu *next,
843 			    unsigned char flags, const unsigned char *src,
844 			    u8 filter_mode, u8 rt_protocol);
845 int br_mdb_hash_init(struct net_bridge *br);
846 void br_mdb_hash_fini(struct net_bridge *br);
847 void br_mdb_notify(struct net_device *dev, struct net_bridge_mdb_entry *mp,
848 		   struct net_bridge_port_group *pg, int type);
849 void br_rtr_notify(struct net_device *dev, struct net_bridge_port *port,
850 		   int type);
851 void br_multicast_del_pg(struct net_bridge_mdb_entry *mp,
852 			 struct net_bridge_port_group *pg,
853 			 struct net_bridge_port_group __rcu **pp);
854 void br_multicast_count(struct net_bridge *br, const struct net_bridge_port *p,
855 			const struct sk_buff *skb, u8 type, u8 dir);
856 int br_multicast_init_stats(struct net_bridge *br);
857 void br_multicast_uninit_stats(struct net_bridge *br);
858 void br_multicast_get_stats(const struct net_bridge *br,
859 			    const struct net_bridge_port *p,
860 			    struct br_mcast_stats *dest);
861 void br_mdb_init(void);
862 void br_mdb_uninit(void);
863 void br_multicast_host_join(struct net_bridge_mdb_entry *mp, bool notify);
864 void br_multicast_host_leave(struct net_bridge_mdb_entry *mp, bool notify);
865 void br_multicast_star_g_handle_mode(struct net_bridge_port_group *pg,
866 				     u8 filter_mode);
867 void br_multicast_sg_add_exclude_ports(struct net_bridge_mdb_entry *star_mp,
868 				       struct net_bridge_port_group *sg);
869 struct net_bridge_group_src *
870 br_multicast_find_group_src(struct net_bridge_port_group *pg, struct br_ip *ip);
871 void br_multicast_del_group_src(struct net_bridge_group_src *src,
872 				bool fastleave);
873 
874 static inline bool br_group_is_l2(const struct br_ip *group)
875 {
876 	return group->proto == 0;
877 }
878 
879 #define mlock_dereference(X, br) \
880 	rcu_dereference_protected(X, lockdep_is_held(&br->multicast_lock))
881 
882 static inline struct hlist_node *
883 br_multicast_get_first_rport_node(struct net_bridge *b, struct sk_buff *skb) {
884 #if IS_ENABLED(CONFIG_IPV6)
885 	if (skb->protocol == htons(ETH_P_IPV6))
886 		return rcu_dereference(hlist_first_rcu(&b->ip6_mc_router_list));
887 #endif
888 	return rcu_dereference(hlist_first_rcu(&b->ip4_mc_router_list));
889 }
890 
891 static inline struct net_bridge_port *
892 br_multicast_rport_from_node_skb(struct hlist_node *rp, struct sk_buff *skb) {
893 	struct net_bridge_mcast_port *mctx;
894 
895 #if IS_ENABLED(CONFIG_IPV6)
896 	if (skb->protocol == htons(ETH_P_IPV6))
897 		mctx = hlist_entry_safe(rp, struct net_bridge_mcast_port,
898 					ip6_rlist);
899 	else
900 #endif
901 		mctx = hlist_entry_safe(rp, struct net_bridge_mcast_port,
902 					ip4_rlist);
903 
904 	if (mctx)
905 		return mctx->port;
906 	else
907 		return NULL;
908 }
909 
910 static inline bool br_ip4_multicast_is_router(struct net_bridge *br)
911 {
912 	return timer_pending(&br->ip4_mc_router_timer);
913 }
914 
915 static inline bool br_ip6_multicast_is_router(struct net_bridge *br)
916 {
917 #if IS_ENABLED(CONFIG_IPV6)
918 	return timer_pending(&br->ip6_mc_router_timer);
919 #else
920 	return false;
921 #endif
922 }
923 
924 static inline bool
925 br_multicast_is_router(struct net_bridge *br, struct sk_buff *skb)
926 {
927 	switch (br->multicast_router) {
928 	case MDB_RTR_TYPE_PERM:
929 		return true;
930 	case MDB_RTR_TYPE_TEMP_QUERY:
931 		if (skb) {
932 			if (skb->protocol == htons(ETH_P_IP))
933 				return br_ip4_multicast_is_router(br);
934 			else if (skb->protocol == htons(ETH_P_IPV6))
935 				return br_ip6_multicast_is_router(br);
936 		} else {
937 			return br_ip4_multicast_is_router(br) ||
938 			       br_ip6_multicast_is_router(br);
939 		}
940 		fallthrough;
941 	default:
942 		return false;
943 	}
944 }
945 
946 static inline bool
947 __br_multicast_querier_exists(struct net_bridge *br,
948 				struct bridge_mcast_other_query *querier,
949 				const bool is_ipv6)
950 {
951 	bool own_querier_enabled;
952 
953 	if (br_opt_get(br, BROPT_MULTICAST_QUERIER)) {
954 		if (is_ipv6 && !br_opt_get(br, BROPT_HAS_IPV6_ADDR))
955 			own_querier_enabled = false;
956 		else
957 			own_querier_enabled = true;
958 	} else {
959 		own_querier_enabled = false;
960 	}
961 
962 	return time_is_before_jiffies(querier->delay_time) &&
963 	       (own_querier_enabled || timer_pending(&querier->timer));
964 }
965 
966 static inline bool br_multicast_querier_exists(struct net_bridge *br,
967 					       struct ethhdr *eth,
968 					       const struct net_bridge_mdb_entry *mdb)
969 {
970 	switch (eth->h_proto) {
971 	case (htons(ETH_P_IP)):
972 		return __br_multicast_querier_exists(br,
973 			&br->ip4_other_query, false);
974 #if IS_ENABLED(CONFIG_IPV6)
975 	case (htons(ETH_P_IPV6)):
976 		return __br_multicast_querier_exists(br,
977 			&br->ip6_other_query, true);
978 #endif
979 	default:
980 		return !!mdb && br_group_is_l2(&mdb->addr);
981 	}
982 }
983 
984 static inline bool br_multicast_is_star_g(const struct br_ip *ip)
985 {
986 	switch (ip->proto) {
987 	case htons(ETH_P_IP):
988 		return ipv4_is_zeronet(ip->src.ip4);
989 #if IS_ENABLED(CONFIG_IPV6)
990 	case htons(ETH_P_IPV6):
991 		return ipv6_addr_any(&ip->src.ip6);
992 #endif
993 	default:
994 		return false;
995 	}
996 }
997 
998 static inline bool br_multicast_should_handle_mode(const struct net_bridge *br,
999 						   __be16 proto)
1000 {
1001 	switch (proto) {
1002 	case htons(ETH_P_IP):
1003 		return !!(br->multicast_igmp_version == 3);
1004 #if IS_ENABLED(CONFIG_IPV6)
1005 	case htons(ETH_P_IPV6):
1006 		return !!(br->multicast_mld_version == 2);
1007 #endif
1008 	default:
1009 		return false;
1010 	}
1011 }
1012 
1013 static inline int br_multicast_igmp_type(const struct sk_buff *skb)
1014 {
1015 	return BR_INPUT_SKB_CB(skb)->igmp;
1016 }
1017 
1018 static inline unsigned long br_multicast_lmqt(const struct net_bridge *br)
1019 {
1020 	return br->multicast_last_member_interval *
1021 	       br->multicast_last_member_count;
1022 }
1023 
1024 static inline unsigned long br_multicast_gmi(const struct net_bridge *br)
1025 {
1026 	/* use the RFC default of 2 for QRV */
1027 	return 2 * br->multicast_query_interval +
1028 	       br->multicast_query_response_interval;
1029 }
1030 #else
1031 static inline int br_multicast_rcv(struct net_bridge *br,
1032 				   struct net_bridge_port *port,
1033 				   struct sk_buff *skb,
1034 				   u16 vid)
1035 {
1036 	return 0;
1037 }
1038 
1039 static inline struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br,
1040 						      struct sk_buff *skb, u16 vid)
1041 {
1042 	return NULL;
1043 }
1044 
1045 static inline int br_multicast_add_port(struct net_bridge_port *port)
1046 {
1047 	return 0;
1048 }
1049 
1050 static inline void br_multicast_del_port(struct net_bridge_port *port)
1051 {
1052 }
1053 
1054 static inline void br_multicast_enable_port(struct net_bridge_port *port)
1055 {
1056 }
1057 
1058 static inline void br_multicast_disable_port(struct net_bridge_port *port)
1059 {
1060 }
1061 
1062 static inline void br_multicast_init(struct net_bridge *br)
1063 {
1064 }
1065 
1066 static inline void br_multicast_join_snoopers(struct net_bridge *br)
1067 {
1068 }
1069 
1070 static inline void br_multicast_leave_snoopers(struct net_bridge *br)
1071 {
1072 }
1073 
1074 static inline void br_multicast_open(struct net_bridge *br)
1075 {
1076 }
1077 
1078 static inline void br_multicast_stop(struct net_bridge *br)
1079 {
1080 }
1081 
1082 static inline void br_multicast_dev_del(struct net_bridge *br)
1083 {
1084 }
1085 
1086 static inline void br_multicast_flood(struct net_bridge_mdb_entry *mdst,
1087 				      struct sk_buff *skb,
1088 				      bool local_rcv, bool local_orig)
1089 {
1090 }
1091 
1092 static inline bool br_multicast_is_router(struct net_bridge *br,
1093 					  struct sk_buff *skb)
1094 {
1095 	return false;
1096 }
1097 
1098 static inline bool br_multicast_querier_exists(struct net_bridge *br,
1099 					       struct ethhdr *eth,
1100 					       const struct net_bridge_mdb_entry *mdb)
1101 {
1102 	return false;
1103 }
1104 
1105 static inline void br_mdb_init(void)
1106 {
1107 }
1108 
1109 static inline void br_mdb_uninit(void)
1110 {
1111 }
1112 
1113 static inline int br_mdb_hash_init(struct net_bridge *br)
1114 {
1115 	return 0;
1116 }
1117 
1118 static inline void br_mdb_hash_fini(struct net_bridge *br)
1119 {
1120 }
1121 
1122 static inline void br_multicast_count(struct net_bridge *br,
1123 				      const struct net_bridge_port *p,
1124 				      const struct sk_buff *skb,
1125 				      u8 type, u8 dir)
1126 {
1127 }
1128 
1129 static inline int br_multicast_init_stats(struct net_bridge *br)
1130 {
1131 	return 0;
1132 }
1133 
1134 static inline void br_multicast_uninit_stats(struct net_bridge *br)
1135 {
1136 }
1137 
1138 static inline int br_multicast_igmp_type(const struct sk_buff *skb)
1139 {
1140 	return 0;
1141 }
1142 #endif
1143 
1144 /* br_vlan.c */
1145 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
1146 bool br_allowed_ingress(const struct net_bridge *br,
1147 			struct net_bridge_vlan_group *vg, struct sk_buff *skb,
1148 			u16 *vid, u8 *state);
1149 bool br_allowed_egress(struct net_bridge_vlan_group *vg,
1150 		       const struct sk_buff *skb);
1151 bool br_should_learn(struct net_bridge_port *p, struct sk_buff *skb, u16 *vid);
1152 struct sk_buff *br_handle_vlan(struct net_bridge *br,
1153 			       const struct net_bridge_port *port,
1154 			       struct net_bridge_vlan_group *vg,
1155 			       struct sk_buff *skb);
1156 int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags,
1157 		bool *changed, struct netlink_ext_ack *extack);
1158 int br_vlan_delete(struct net_bridge *br, u16 vid);
1159 void br_vlan_flush(struct net_bridge *br);
1160 struct net_bridge_vlan *br_vlan_find(struct net_bridge_vlan_group *vg, u16 vid);
1161 void br_recalculate_fwd_mask(struct net_bridge *br);
1162 int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val,
1163 			  struct netlink_ext_ack *extack);
1164 int __br_vlan_set_proto(struct net_bridge *br, __be16 proto,
1165 			struct netlink_ext_ack *extack);
1166 int br_vlan_set_proto(struct net_bridge *br, unsigned long val,
1167 		      struct netlink_ext_ack *extack);
1168 int br_vlan_set_stats(struct net_bridge *br, unsigned long val);
1169 int br_vlan_set_stats_per_port(struct net_bridge *br, unsigned long val);
1170 int br_vlan_init(struct net_bridge *br);
1171 int br_vlan_set_default_pvid(struct net_bridge *br, unsigned long val,
1172 			     struct netlink_ext_ack *extack);
1173 int __br_vlan_set_default_pvid(struct net_bridge *br, u16 pvid,
1174 			       struct netlink_ext_ack *extack);
1175 int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags,
1176 		 bool *changed, struct netlink_ext_ack *extack);
1177 int nbp_vlan_delete(struct net_bridge_port *port, u16 vid);
1178 void nbp_vlan_flush(struct net_bridge_port *port);
1179 int nbp_vlan_init(struct net_bridge_port *port, struct netlink_ext_ack *extack);
1180 int nbp_get_num_vlan_infos(struct net_bridge_port *p, u32 filter_mask);
1181 void br_vlan_get_stats(const struct net_bridge_vlan *v,
1182 		       struct pcpu_sw_netstats *stats);
1183 void br_vlan_port_event(struct net_bridge_port *p, unsigned long event);
1184 int br_vlan_bridge_event(struct net_device *dev, unsigned long event,
1185 			 void *ptr);
1186 void br_vlan_rtnl_init(void);
1187 void br_vlan_rtnl_uninit(void);
1188 void br_vlan_notify(const struct net_bridge *br,
1189 		    const struct net_bridge_port *p,
1190 		    u16 vid, u16 vid_range,
1191 		    int cmd);
1192 bool br_vlan_can_enter_range(const struct net_bridge_vlan *v_curr,
1193 			     const struct net_bridge_vlan *range_end);
1194 
1195 void br_vlan_fill_forward_path_pvid(struct net_bridge *br,
1196 				    struct net_device_path_ctx *ctx,
1197 				    struct net_device_path *path);
1198 int br_vlan_fill_forward_path_mode(struct net_bridge *br,
1199 				   struct net_bridge_port *dst,
1200 				   struct net_device_path *path);
1201 
1202 static inline struct net_bridge_vlan_group *br_vlan_group(
1203 					const struct net_bridge *br)
1204 {
1205 	return rtnl_dereference(br->vlgrp);
1206 }
1207 
1208 static inline struct net_bridge_vlan_group *nbp_vlan_group(
1209 					const struct net_bridge_port *p)
1210 {
1211 	return rtnl_dereference(p->vlgrp);
1212 }
1213 
1214 static inline struct net_bridge_vlan_group *br_vlan_group_rcu(
1215 					const struct net_bridge *br)
1216 {
1217 	return rcu_dereference(br->vlgrp);
1218 }
1219 
1220 static inline struct net_bridge_vlan_group *nbp_vlan_group_rcu(
1221 					const struct net_bridge_port *p)
1222 {
1223 	return rcu_dereference(p->vlgrp);
1224 }
1225 
1226 /* Since bridge now depends on 8021Q module, but the time bridge sees the
1227  * skb, the vlan tag will always be present if the frame was tagged.
1228  */
1229 static inline int br_vlan_get_tag(const struct sk_buff *skb, u16 *vid)
1230 {
1231 	int err = 0;
1232 
1233 	if (skb_vlan_tag_present(skb)) {
1234 		*vid = skb_vlan_tag_get_id(skb);
1235 	} else {
1236 		*vid = 0;
1237 		err = -EINVAL;
1238 	}
1239 
1240 	return err;
1241 }
1242 
1243 static inline u16 br_get_pvid(const struct net_bridge_vlan_group *vg)
1244 {
1245 	if (!vg)
1246 		return 0;
1247 
1248 	smp_rmb();
1249 	return vg->pvid;
1250 }
1251 
1252 static inline u16 br_vlan_flags(const struct net_bridge_vlan *v, u16 pvid)
1253 {
1254 	return v->vid == pvid ? v->flags | BRIDGE_VLAN_INFO_PVID : v->flags;
1255 }
1256 #else
1257 static inline bool br_allowed_ingress(const struct net_bridge *br,
1258 				      struct net_bridge_vlan_group *vg,
1259 				      struct sk_buff *skb,
1260 				      u16 *vid, u8 *state)
1261 {
1262 	return true;
1263 }
1264 
1265 static inline bool br_allowed_egress(struct net_bridge_vlan_group *vg,
1266 				     const struct sk_buff *skb)
1267 {
1268 	return true;
1269 }
1270 
1271 static inline bool br_should_learn(struct net_bridge_port *p,
1272 				   struct sk_buff *skb, u16 *vid)
1273 {
1274 	return true;
1275 }
1276 
1277 static inline struct sk_buff *br_handle_vlan(struct net_bridge *br,
1278 					     const struct net_bridge_port *port,
1279 					     struct net_bridge_vlan_group *vg,
1280 					     struct sk_buff *skb)
1281 {
1282 	return skb;
1283 }
1284 
1285 static inline int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags,
1286 			      bool *changed, struct netlink_ext_ack *extack)
1287 {
1288 	*changed = false;
1289 	return -EOPNOTSUPP;
1290 }
1291 
1292 static inline int br_vlan_delete(struct net_bridge *br, u16 vid)
1293 {
1294 	return -EOPNOTSUPP;
1295 }
1296 
1297 static inline void br_vlan_flush(struct net_bridge *br)
1298 {
1299 }
1300 
1301 static inline void br_recalculate_fwd_mask(struct net_bridge *br)
1302 {
1303 }
1304 
1305 static inline int br_vlan_init(struct net_bridge *br)
1306 {
1307 	return 0;
1308 }
1309 
1310 static inline int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags,
1311 			       bool *changed, struct netlink_ext_ack *extack)
1312 {
1313 	*changed = false;
1314 	return -EOPNOTSUPP;
1315 }
1316 
1317 static inline int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
1318 {
1319 	return -EOPNOTSUPP;
1320 }
1321 
1322 static inline void nbp_vlan_flush(struct net_bridge_port *port)
1323 {
1324 }
1325 
1326 static inline struct net_bridge_vlan *br_vlan_find(struct net_bridge_vlan_group *vg,
1327 						   u16 vid)
1328 {
1329 	return NULL;
1330 }
1331 
1332 static inline int nbp_vlan_init(struct net_bridge_port *port,
1333 				struct netlink_ext_ack *extack)
1334 {
1335 	return 0;
1336 }
1337 
1338 static inline u16 br_vlan_get_tag(const struct sk_buff *skb, u16 *tag)
1339 {
1340 	return 0;
1341 }
1342 
1343 static inline u16 br_get_pvid(const struct net_bridge_vlan_group *vg)
1344 {
1345 	return 0;
1346 }
1347 
1348 static inline int br_vlan_filter_toggle(struct net_bridge *br,
1349 					unsigned long val,
1350 					struct netlink_ext_ack *extack)
1351 {
1352 	return -EOPNOTSUPP;
1353 }
1354 
1355 static inline int nbp_get_num_vlan_infos(struct net_bridge_port *p,
1356 					 u32 filter_mask)
1357 {
1358 	return 0;
1359 }
1360 
1361 static inline void br_vlan_fill_forward_path_pvid(struct net_bridge *br,
1362 						  struct net_device_path_ctx *ctx,
1363 						  struct net_device_path *path)
1364 {
1365 }
1366 
1367 static inline int br_vlan_fill_forward_path_mode(struct net_bridge *br,
1368 						 struct net_bridge_port *dst,
1369 						 struct net_device_path *path)
1370 {
1371 	return 0;
1372 }
1373 
1374 static inline struct net_bridge_vlan_group *br_vlan_group(
1375 					const struct net_bridge *br)
1376 {
1377 	return NULL;
1378 }
1379 
1380 static inline struct net_bridge_vlan_group *nbp_vlan_group(
1381 					const struct net_bridge_port *p)
1382 {
1383 	return NULL;
1384 }
1385 
1386 static inline struct net_bridge_vlan_group *br_vlan_group_rcu(
1387 					const struct net_bridge *br)
1388 {
1389 	return NULL;
1390 }
1391 
1392 static inline struct net_bridge_vlan_group *nbp_vlan_group_rcu(
1393 					const struct net_bridge_port *p)
1394 {
1395 	return NULL;
1396 }
1397 
1398 static inline void br_vlan_get_stats(const struct net_bridge_vlan *v,
1399 				     struct pcpu_sw_netstats *stats)
1400 {
1401 }
1402 
1403 static inline void br_vlan_port_event(struct net_bridge_port *p,
1404 				      unsigned long event)
1405 {
1406 }
1407 
1408 static inline int br_vlan_bridge_event(struct net_device *dev,
1409 				       unsigned long event, void *ptr)
1410 {
1411 	return 0;
1412 }
1413 
1414 static inline void br_vlan_rtnl_init(void)
1415 {
1416 }
1417 
1418 static inline void br_vlan_rtnl_uninit(void)
1419 {
1420 }
1421 
1422 static inline void br_vlan_notify(const struct net_bridge *br,
1423 				  const struct net_bridge_port *p,
1424 				  u16 vid, u16 vid_range,
1425 				  int cmd)
1426 {
1427 }
1428 
1429 static inline bool br_vlan_can_enter_range(const struct net_bridge_vlan *v_curr,
1430 					   const struct net_bridge_vlan *range_end)
1431 {
1432 	return true;
1433 }
1434 #endif
1435 
1436 /* br_vlan_options.c */
1437 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
1438 bool br_vlan_opts_eq_range(const struct net_bridge_vlan *v_curr,
1439 			   const struct net_bridge_vlan *range_end);
1440 bool br_vlan_opts_fill(struct sk_buff *skb, const struct net_bridge_vlan *v);
1441 size_t br_vlan_opts_nl_size(void);
1442 int br_vlan_process_options(const struct net_bridge *br,
1443 			    const struct net_bridge_port *p,
1444 			    struct net_bridge_vlan *range_start,
1445 			    struct net_bridge_vlan *range_end,
1446 			    struct nlattr **tb,
1447 			    struct netlink_ext_ack *extack);
1448 
1449 /* vlan state manipulation helpers using *_ONCE to annotate lock-free access */
1450 static inline u8 br_vlan_get_state(const struct net_bridge_vlan *v)
1451 {
1452 	return READ_ONCE(v->state);
1453 }
1454 
1455 static inline void br_vlan_set_state(struct net_bridge_vlan *v, u8 state)
1456 {
1457 	WRITE_ONCE(v->state, state);
1458 }
1459 
1460 static inline u8 br_vlan_get_pvid_state(const struct net_bridge_vlan_group *vg)
1461 {
1462 	return READ_ONCE(vg->pvid_state);
1463 }
1464 
1465 static inline void br_vlan_set_pvid_state(struct net_bridge_vlan_group *vg,
1466 					  u8 state)
1467 {
1468 	WRITE_ONCE(vg->pvid_state, state);
1469 }
1470 
1471 /* learn_allow is true at ingress and false at egress */
1472 static inline bool br_vlan_state_allowed(u8 state, bool learn_allow)
1473 {
1474 	switch (state) {
1475 	case BR_STATE_LEARNING:
1476 		return learn_allow;
1477 	case BR_STATE_FORWARDING:
1478 		return true;
1479 	default:
1480 		return false;
1481 	}
1482 }
1483 #endif
1484 
1485 struct nf_br_ops {
1486 	int (*br_dev_xmit_hook)(struct sk_buff *skb);
1487 };
1488 extern const struct nf_br_ops __rcu *nf_br_ops;
1489 
1490 /* br_netfilter.c */
1491 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1492 int br_nf_core_init(void);
1493 void br_nf_core_fini(void);
1494 void br_netfilter_rtable_init(struct net_bridge *);
1495 #else
1496 static inline int br_nf_core_init(void) { return 0; }
1497 static inline void br_nf_core_fini(void) {}
1498 #define br_netfilter_rtable_init(x)
1499 #endif
1500 
1501 /* br_stp.c */
1502 void br_set_state(struct net_bridge_port *p, unsigned int state);
1503 struct net_bridge_port *br_get_port(struct net_bridge *br, u16 port_no);
1504 void br_init_port(struct net_bridge_port *p);
1505 void br_become_designated_port(struct net_bridge_port *p);
1506 
1507 void __br_set_forward_delay(struct net_bridge *br, unsigned long t);
1508 int br_set_forward_delay(struct net_bridge *br, unsigned long x);
1509 int br_set_hello_time(struct net_bridge *br, unsigned long x);
1510 int br_set_max_age(struct net_bridge *br, unsigned long x);
1511 int __set_ageing_time(struct net_device *dev, unsigned long t);
1512 int br_set_ageing_time(struct net_bridge *br, clock_t ageing_time);
1513 
1514 
1515 /* br_stp_if.c */
1516 void br_stp_enable_bridge(struct net_bridge *br);
1517 void br_stp_disable_bridge(struct net_bridge *br);
1518 int br_stp_set_enabled(struct net_bridge *br, unsigned long val,
1519 		       struct netlink_ext_ack *extack);
1520 void br_stp_enable_port(struct net_bridge_port *p);
1521 void br_stp_disable_port(struct net_bridge_port *p);
1522 bool br_stp_recalculate_bridge_id(struct net_bridge *br);
1523 void br_stp_change_bridge_id(struct net_bridge *br, const unsigned char *a);
1524 void br_stp_set_bridge_priority(struct net_bridge *br, u16 newprio);
1525 int br_stp_set_port_priority(struct net_bridge_port *p, unsigned long newprio);
1526 int br_stp_set_path_cost(struct net_bridge_port *p, unsigned long path_cost);
1527 ssize_t br_show_bridge_id(char *buf, const struct bridge_id *id);
1528 
1529 /* br_stp_bpdu.c */
1530 struct stp_proto;
1531 void br_stp_rcv(const struct stp_proto *proto, struct sk_buff *skb,
1532 		struct net_device *dev);
1533 
1534 /* br_stp_timer.c */
1535 void br_stp_timer_init(struct net_bridge *br);
1536 void br_stp_port_timer_init(struct net_bridge_port *p);
1537 unsigned long br_timer_value(const struct timer_list *timer);
1538 
1539 /* br.c */
1540 #if IS_ENABLED(CONFIG_ATM_LANE)
1541 extern int (*br_fdb_test_addr_hook)(struct net_device *dev, unsigned char *addr);
1542 #endif
1543 
1544 /* br_mrp.c */
1545 #if IS_ENABLED(CONFIG_BRIDGE_MRP)
1546 int br_mrp_parse(struct net_bridge *br, struct net_bridge_port *p,
1547 		 struct nlattr *attr, int cmd, struct netlink_ext_ack *extack);
1548 bool br_mrp_enabled(struct net_bridge *br);
1549 void br_mrp_port_del(struct net_bridge *br, struct net_bridge_port *p);
1550 int br_mrp_fill_info(struct sk_buff *skb, struct net_bridge *br);
1551 #else
1552 static inline int br_mrp_parse(struct net_bridge *br, struct net_bridge_port *p,
1553 			       struct nlattr *attr, int cmd,
1554 			       struct netlink_ext_ack *extack)
1555 {
1556 	return -EOPNOTSUPP;
1557 }
1558 
1559 static inline bool br_mrp_enabled(struct net_bridge *br)
1560 {
1561 	return false;
1562 }
1563 
1564 static inline void br_mrp_port_del(struct net_bridge *br,
1565 				   struct net_bridge_port *p)
1566 {
1567 }
1568 
1569 static inline int br_mrp_fill_info(struct sk_buff *skb, struct net_bridge *br)
1570 {
1571 	return 0;
1572 }
1573 
1574 #endif
1575 
1576 /* br_cfm.c */
1577 #if IS_ENABLED(CONFIG_BRIDGE_CFM)
1578 int br_cfm_parse(struct net_bridge *br, struct net_bridge_port *p,
1579 		 struct nlattr *attr, int cmd, struct netlink_ext_ack *extack);
1580 bool br_cfm_created(struct net_bridge *br);
1581 void br_cfm_port_del(struct net_bridge *br, struct net_bridge_port *p);
1582 int br_cfm_config_fill_info(struct sk_buff *skb, struct net_bridge *br);
1583 int br_cfm_status_fill_info(struct sk_buff *skb,
1584 			    struct net_bridge *br,
1585 			    bool getlink);
1586 int br_cfm_mep_count(struct net_bridge *br, u32 *count);
1587 int br_cfm_peer_mep_count(struct net_bridge *br, u32 *count);
1588 #else
1589 static inline int br_cfm_parse(struct net_bridge *br, struct net_bridge_port *p,
1590 			       struct nlattr *attr, int cmd,
1591 			       struct netlink_ext_ack *extack)
1592 {
1593 	return -EOPNOTSUPP;
1594 }
1595 
1596 static inline bool br_cfm_created(struct net_bridge *br)
1597 {
1598 	return false;
1599 }
1600 
1601 static inline void br_cfm_port_del(struct net_bridge *br,
1602 				   struct net_bridge_port *p)
1603 {
1604 }
1605 
1606 static inline int br_cfm_config_fill_info(struct sk_buff *skb, struct net_bridge *br)
1607 {
1608 	return -EOPNOTSUPP;
1609 }
1610 
1611 static inline int br_cfm_status_fill_info(struct sk_buff *skb,
1612 					  struct net_bridge *br,
1613 					  bool getlink)
1614 {
1615 	return -EOPNOTSUPP;
1616 }
1617 
1618 static inline int br_cfm_mep_count(struct net_bridge *br, u32 *count)
1619 {
1620 	return -EOPNOTSUPP;
1621 }
1622 
1623 static inline int br_cfm_peer_mep_count(struct net_bridge *br, u32 *count)
1624 {
1625 	return -EOPNOTSUPP;
1626 }
1627 #endif
1628 
1629 /* br_netlink.c */
1630 extern struct rtnl_link_ops br_link_ops;
1631 int br_netlink_init(void);
1632 void br_netlink_fini(void);
1633 void br_ifinfo_notify(int event, const struct net_bridge *br,
1634 		      const struct net_bridge_port *port);
1635 void br_info_notify(int event, const struct net_bridge *br,
1636 		    const struct net_bridge_port *port, u32 filter);
1637 int br_setlink(struct net_device *dev, struct nlmsghdr *nlmsg, u16 flags,
1638 	       struct netlink_ext_ack *extack);
1639 int br_dellink(struct net_device *dev, struct nlmsghdr *nlmsg, u16 flags);
1640 int br_getlink(struct sk_buff *skb, u32 pid, u32 seq, struct net_device *dev,
1641 	       u32 filter_mask, int nlflags);
1642 int br_process_vlan_info(struct net_bridge *br,
1643 			 struct net_bridge_port *p, int cmd,
1644 			 struct bridge_vlan_info *vinfo_curr,
1645 			 struct bridge_vlan_info **vinfo_last,
1646 			 bool *changed,
1647 			 struct netlink_ext_ack *extack);
1648 
1649 #ifdef CONFIG_SYSFS
1650 /* br_sysfs_if.c */
1651 extern const struct sysfs_ops brport_sysfs_ops;
1652 int br_sysfs_addif(struct net_bridge_port *p);
1653 int br_sysfs_renameif(struct net_bridge_port *p);
1654 
1655 /* br_sysfs_br.c */
1656 int br_sysfs_addbr(struct net_device *dev);
1657 void br_sysfs_delbr(struct net_device *dev);
1658 
1659 #else
1660 
1661 static inline int br_sysfs_addif(struct net_bridge_port *p) { return 0; }
1662 static inline int br_sysfs_renameif(struct net_bridge_port *p) { return 0; }
1663 static inline int br_sysfs_addbr(struct net_device *dev) { return 0; }
1664 static inline void br_sysfs_delbr(struct net_device *dev) { return; }
1665 #endif /* CONFIG_SYSFS */
1666 
1667 /* br_switchdev.c */
1668 #ifdef CONFIG_NET_SWITCHDEV
1669 int nbp_switchdev_mark_set(struct net_bridge_port *p);
1670 void nbp_switchdev_frame_mark(const struct net_bridge_port *p,
1671 			      struct sk_buff *skb);
1672 bool nbp_switchdev_allowed_egress(const struct net_bridge_port *p,
1673 				  const struct sk_buff *skb);
1674 int br_switchdev_set_port_flag(struct net_bridge_port *p,
1675 			       unsigned long flags,
1676 			       unsigned long mask,
1677 			       struct netlink_ext_ack *extack);
1678 void br_switchdev_fdb_notify(struct net_bridge *br,
1679 			     const struct net_bridge_fdb_entry *fdb, int type);
1680 int br_switchdev_port_vlan_add(struct net_device *dev, u16 vid, u16 flags,
1681 			       struct netlink_ext_ack *extack);
1682 int br_switchdev_port_vlan_del(struct net_device *dev, u16 vid);
1683 
1684 static inline void br_switchdev_frame_unmark(struct sk_buff *skb)
1685 {
1686 	skb->offload_fwd_mark = 0;
1687 }
1688 #else
1689 static inline int nbp_switchdev_mark_set(struct net_bridge_port *p)
1690 {
1691 	return 0;
1692 }
1693 
1694 static inline void nbp_switchdev_frame_mark(const struct net_bridge_port *p,
1695 					    struct sk_buff *skb)
1696 {
1697 }
1698 
1699 static inline bool nbp_switchdev_allowed_egress(const struct net_bridge_port *p,
1700 						const struct sk_buff *skb)
1701 {
1702 	return true;
1703 }
1704 
1705 static inline int br_switchdev_set_port_flag(struct net_bridge_port *p,
1706 					     unsigned long flags,
1707 					     unsigned long mask,
1708 					     struct netlink_ext_ack *extack)
1709 {
1710 	return 0;
1711 }
1712 
1713 static inline int br_switchdev_port_vlan_add(struct net_device *dev,
1714 					     u16 vid, u16 flags,
1715 					     struct netlink_ext_ack *extack)
1716 {
1717 	return -EOPNOTSUPP;
1718 }
1719 
1720 static inline int br_switchdev_port_vlan_del(struct net_device *dev, u16 vid)
1721 {
1722 	return -EOPNOTSUPP;
1723 }
1724 
1725 static inline void
1726 br_switchdev_fdb_notify(struct net_bridge *br,
1727 			const struct net_bridge_fdb_entry *fdb, int type)
1728 {
1729 }
1730 
1731 static inline void br_switchdev_frame_unmark(struct sk_buff *skb)
1732 {
1733 }
1734 #endif /* CONFIG_NET_SWITCHDEV */
1735 
1736 /* br_arp_nd_proxy.c */
1737 void br_recalculate_neigh_suppress_enabled(struct net_bridge *br);
1738 void br_do_proxy_suppress_arp(struct sk_buff *skb, struct net_bridge *br,
1739 			      u16 vid, struct net_bridge_port *p);
1740 void br_do_suppress_nd(struct sk_buff *skb, struct net_bridge *br,
1741 		       u16 vid, struct net_bridge_port *p, struct nd_msg *msg);
1742 struct nd_msg *br_is_nd_neigh_msg(struct sk_buff *skb, struct nd_msg *m);
1743 #endif
1744