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