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