xref: /openbmc/linux/net/bridge/br_private.h (revision 5d331b7f)
1 /*
2  *	Linux ethernet bridge
3  *
4  *	Authors:
5  *	Lennert Buytenhek		<buytenh@gnu.org>
6  *
7  *	This program is free software; you can redistribute it and/or
8  *	modify it under the terms of the GNU General Public License
9  *	as published by the Free Software Foundation; either version
10  *	2 of the License, or (at your option) any later version.
11  */
12 
13 #ifndef _BR_PRIVATE_H
14 #define _BR_PRIVATE_H
15 
16 #include <linux/netdevice.h>
17 #include <linux/if_bridge.h>
18 #include <linux/netpoll.h>
19 #include <linux/u64_stats_sync.h>
20 #include <net/route.h>
21 #include <net/ip6_fib.h>
22 #include <linux/if_vlan.h>
23 #include <linux/rhashtable.h>
24 #include <linux/refcount.h>
25 
26 #define BR_HASH_BITS 8
27 #define BR_HASH_SIZE (1 << BR_HASH_BITS)
28 
29 #define BR_HOLD_TIME (1*HZ)
30 
31 #define BR_PORT_BITS	10
32 #define BR_MAX_PORTS	(1<<BR_PORT_BITS)
33 
34 #define BR_VERSION	"2.3"
35 
36 /* Control of forwarding link local multicast */
37 #define BR_GROUPFWD_DEFAULT	0
38 /* Don't allow forwarding of control protocols like STP, MAC PAUSE and LACP */
39 enum {
40 	BR_GROUPFWD_STP		= BIT(0),
41 	BR_GROUPFWD_MACPAUSE	= BIT(1),
42 	BR_GROUPFWD_LACP	= BIT(2),
43 };
44 
45 #define BR_GROUPFWD_RESTRICTED (BR_GROUPFWD_STP | BR_GROUPFWD_MACPAUSE | \
46 				BR_GROUPFWD_LACP)
47 /* The Nearest Customer Bridge Group Address, 01-80-C2-00-00-[00,0B,0C,0D,0F] */
48 #define BR_GROUPFWD_8021AD	0xB801u
49 
50 /* Path to usermode spanning tree program */
51 #define BR_STP_PROG	"/sbin/bridge-stp"
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_vlan_stats {
93 	u64 rx_bytes;
94 	u64 rx_packets;
95 	u64 tx_bytes;
96 	u64 tx_packets;
97 	struct u64_stats_sync syncp;
98 };
99 
100 struct br_tunnel_info {
101 	__be64			tunnel_id;
102 	struct metadata_dst	*tunnel_dst;
103 };
104 
105 /**
106  * struct net_bridge_vlan - per-vlan entry
107  *
108  * @vnode: rhashtable member
109  * @vid: VLAN id
110  * @flags: bridge vlan flags
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 	struct br_vlan_stats __percpu	*stats;
131 	union {
132 		struct net_bridge	*br;
133 		struct net_bridge_port	*port;
134 	};
135 	union {
136 		refcount_t		refcnt;
137 		struct net_bridge_vlan	*brvlan;
138 	};
139 
140 	struct br_tunnel_info		tinfo;
141 
142 	struct list_head		vlist;
143 
144 	struct rcu_head			rcu;
145 };
146 
147 /**
148  * struct net_bridge_vlan_group
149  *
150  * @vlan_hash: VLAN entry rhashtable
151  * @vlan_list: sorted VLAN entry list
152  * @num_vlans: number of total VLAN entries
153  * @pvid: PVID VLAN id
154  *
155  * IMPORTANT: Be careful when checking if there're VLAN entries using list
156  *            primitives because the bridge can have entries in its list which
157  *            are just for global context but not for filtering, i.e. they have
158  *            the master flag set but not the brentry flag. If you have to check
159  *            if there're "real" entries in the bridge please test @num_vlans
160  */
161 struct net_bridge_vlan_group {
162 	struct rhashtable		vlan_hash;
163 	struct rhashtable		tunnel_hash;
164 	struct list_head		vlan_list;
165 	u16				num_vlans;
166 	u16				pvid;
167 };
168 
169 struct net_bridge_fdb_key {
170 	mac_addr addr;
171 	u16 vlan_id;
172 };
173 
174 struct net_bridge_fdb_entry {
175 	struct rhash_head		rhnode;
176 	struct net_bridge_port		*dst;
177 
178 	struct net_bridge_fdb_key	key;
179 	struct hlist_node		fdb_node;
180 	unsigned char			is_local:1,
181 					is_static:1,
182 					is_sticky:1,
183 					added_by_user:1,
184 					added_by_external_learn:1,
185 					offloaded:1;
186 
187 	/* write-heavy members should not affect lookups */
188 	unsigned long			updated ____cacheline_aligned_in_smp;
189 	unsigned long			used;
190 
191 	struct rcu_head			rcu;
192 };
193 
194 #define MDB_PG_FLAGS_PERMANENT	BIT(0)
195 #define MDB_PG_FLAGS_OFFLOAD	BIT(1)
196 
197 struct net_bridge_port_group {
198 	struct net_bridge_port		*port;
199 	struct net_bridge_port_group __rcu *next;
200 	struct hlist_node		mglist;
201 	struct rcu_head			rcu;
202 	struct timer_list		timer;
203 	struct br_ip			addr;
204 	unsigned char			flags;
205 	unsigned char			eth_addr[ETH_ALEN];
206 };
207 
208 struct net_bridge_mdb_entry {
209 	struct hlist_node		hlist[2];
210 	struct net_bridge		*br;
211 	struct net_bridge_port_group __rcu *ports;
212 	struct rcu_head			rcu;
213 	struct timer_list		timer;
214 	struct br_ip			addr;
215 	bool				host_joined;
216 };
217 
218 struct net_bridge_mdb_htable {
219 	struct hlist_head		*mhash;
220 	struct rcu_head			rcu;
221 	struct net_bridge_mdb_htable	*old;
222 	u32				size;
223 	u32				max;
224 	u32				secret;
225 	u32				ver;
226 };
227 
228 struct net_bridge_port {
229 	struct net_bridge		*br;
230 	struct net_device		*dev;
231 	struct list_head		list;
232 
233 	unsigned long			flags;
234 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
235 	struct net_bridge_vlan_group	__rcu *vlgrp;
236 #endif
237 	struct net_bridge_port		__rcu *backup_port;
238 
239 	/* STP */
240 	u8				priority;
241 	u8				state;
242 	u16				port_no;
243 	unsigned char			topology_change_ack;
244 	unsigned char			config_pending;
245 	port_id				port_id;
246 	port_id				designated_port;
247 	bridge_id			designated_root;
248 	bridge_id			designated_bridge;
249 	u32				path_cost;
250 	u32				designated_cost;
251 	unsigned long			designated_age;
252 
253 	struct timer_list		forward_delay_timer;
254 	struct timer_list		hold_timer;
255 	struct timer_list		message_age_timer;
256 	struct kobject			kobj;
257 	struct rcu_head			rcu;
258 
259 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
260 	struct bridge_mcast_own_query	ip4_own_query;
261 #if IS_ENABLED(CONFIG_IPV6)
262 	struct bridge_mcast_own_query	ip6_own_query;
263 #endif /* IS_ENABLED(CONFIG_IPV6) */
264 	unsigned char			multicast_router;
265 	struct bridge_mcast_stats	__percpu *mcast_stats;
266 	struct timer_list		multicast_router_timer;
267 	struct hlist_head		mglist;
268 	struct hlist_node		rlist;
269 #endif
270 
271 #ifdef CONFIG_SYSFS
272 	char				sysfs_name[IFNAMSIZ];
273 #endif
274 
275 #ifdef CONFIG_NET_POLL_CONTROLLER
276 	struct netpoll			*np;
277 #endif
278 #ifdef CONFIG_NET_SWITCHDEV
279 	int				offload_fwd_mark;
280 #endif
281 	u16				group_fwd_mask;
282 	u16				backup_redirected_cnt;
283 };
284 
285 #define kobj_to_brport(obj)	container_of(obj, struct net_bridge_port, kobj)
286 
287 #define br_auto_port(p) ((p)->flags & BR_AUTO_MASK)
288 #define br_promisc_port(p) ((p)->flags & BR_PROMISC)
289 
290 #define br_port_exists(dev) (dev->priv_flags & IFF_BRIDGE_PORT)
291 
292 static inline struct net_bridge_port *br_port_get_rcu(const struct net_device *dev)
293 {
294 	return rcu_dereference(dev->rx_handler_data);
295 }
296 
297 static inline struct net_bridge_port *br_port_get_rtnl(const struct net_device *dev)
298 {
299 	return br_port_exists(dev) ?
300 		rtnl_dereference(dev->rx_handler_data) : NULL;
301 }
302 
303 static inline struct net_bridge_port *br_port_get_rtnl_rcu(const struct net_device *dev)
304 {
305 	return br_port_exists(dev) ?
306 		rcu_dereference_rtnl(dev->rx_handler_data) : NULL;
307 }
308 
309 enum net_bridge_opts {
310 	BROPT_VLAN_ENABLED,
311 	BROPT_VLAN_STATS_ENABLED,
312 	BROPT_NF_CALL_IPTABLES,
313 	BROPT_NF_CALL_IP6TABLES,
314 	BROPT_NF_CALL_ARPTABLES,
315 	BROPT_GROUP_ADDR_SET,
316 	BROPT_MULTICAST_ENABLED,
317 	BROPT_MULTICAST_QUERIER,
318 	BROPT_MULTICAST_QUERY_USE_IFADDR,
319 	BROPT_MULTICAST_STATS_ENABLED,
320 	BROPT_HAS_IPV6_ADDR,
321 	BROPT_NEIGH_SUPPRESS_ENABLED,
322 	BROPT_MTU_SET_BY_USER,
323 	BROPT_VLAN_STATS_PER_PORT,
324 };
325 
326 struct net_bridge {
327 	spinlock_t			lock;
328 	spinlock_t			hash_lock;
329 	struct list_head		port_list;
330 	struct net_device		*dev;
331 	struct pcpu_sw_netstats		__percpu *stats;
332 	unsigned long			options;
333 	/* These fields are accessed on each packet */
334 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
335 	__be16				vlan_proto;
336 	u16				default_pvid;
337 	struct net_bridge_vlan_group	__rcu *vlgrp;
338 #endif
339 
340 	struct rhashtable		fdb_hash_tbl;
341 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
342 	union {
343 		struct rtable		fake_rtable;
344 		struct rt6_info		fake_rt6_info;
345 	};
346 #endif
347 	u16				group_fwd_mask;
348 	u16				group_fwd_mask_required;
349 
350 	/* STP */
351 	bridge_id			designated_root;
352 	bridge_id			bridge_id;
353 	unsigned char			topology_change;
354 	unsigned char			topology_change_detected;
355 	u16				root_port;
356 	unsigned long			max_age;
357 	unsigned long			hello_time;
358 	unsigned long			forward_delay;
359 	unsigned long			ageing_time;
360 	unsigned long			bridge_max_age;
361 	unsigned long			bridge_hello_time;
362 	unsigned long			bridge_forward_delay;
363 	unsigned long			bridge_ageing_time;
364 	u32				root_path_cost;
365 
366 	u8				group_addr[ETH_ALEN];
367 
368 	enum {
369 		BR_NO_STP, 		/* no spanning tree */
370 		BR_KERNEL_STP,		/* old STP in kernel */
371 		BR_USER_STP,		/* new RSTP in userspace */
372 	} stp_enabled;
373 
374 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
375 
376 	u32				hash_elasticity;
377 	u32				hash_max;
378 
379 	u32				multicast_last_member_count;
380 	u32				multicast_startup_query_count;
381 
382 	u8				multicast_igmp_version;
383 	u8				multicast_router;
384 #if IS_ENABLED(CONFIG_IPV6)
385 	u8				multicast_mld_version;
386 #endif
387 	spinlock_t			multicast_lock;
388 	unsigned long			multicast_last_member_interval;
389 	unsigned long			multicast_membership_interval;
390 	unsigned long			multicast_querier_interval;
391 	unsigned long			multicast_query_interval;
392 	unsigned long			multicast_query_response_interval;
393 	unsigned long			multicast_startup_query_interval;
394 
395 	struct net_bridge_mdb_htable __rcu *mdb;
396 	struct hlist_head		router_list;
397 
398 	struct timer_list		multicast_router_timer;
399 	struct bridge_mcast_other_query	ip4_other_query;
400 	struct bridge_mcast_own_query	ip4_own_query;
401 	struct bridge_mcast_querier	ip4_querier;
402 	struct bridge_mcast_stats	__percpu *mcast_stats;
403 #if IS_ENABLED(CONFIG_IPV6)
404 	struct bridge_mcast_other_query	ip6_other_query;
405 	struct bridge_mcast_own_query	ip6_own_query;
406 	struct bridge_mcast_querier	ip6_querier;
407 #endif /* IS_ENABLED(CONFIG_IPV6) */
408 #endif
409 
410 	struct timer_list		hello_timer;
411 	struct timer_list		tcn_timer;
412 	struct timer_list		topology_change_timer;
413 	struct delayed_work		gc_work;
414 	struct kobject			*ifobj;
415 	u32				auto_cnt;
416 
417 #ifdef CONFIG_NET_SWITCHDEV
418 	int offload_fwd_mark;
419 #endif
420 	struct hlist_head		fdb_list;
421 };
422 
423 struct br_input_skb_cb {
424 	struct net_device *brdev;
425 
426 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
427 	int igmp;
428 	int mrouters_only;
429 #endif
430 
431 	bool proxyarp_replied;
432 	bool src_port_isolated;
433 
434 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
435 	bool vlan_filtered;
436 #endif
437 
438 #ifdef CONFIG_NET_SWITCHDEV
439 	int offload_fwd_mark;
440 #endif
441 };
442 
443 #define BR_INPUT_SKB_CB(__skb)	((struct br_input_skb_cb *)(__skb)->cb)
444 
445 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
446 # define BR_INPUT_SKB_CB_MROUTERS_ONLY(__skb)	(BR_INPUT_SKB_CB(__skb)->mrouters_only)
447 #else
448 # define BR_INPUT_SKB_CB_MROUTERS_ONLY(__skb)	(0)
449 #endif
450 
451 #define br_printk(level, br, format, args...)	\
452 	printk(level "%s: " format, (br)->dev->name, ##args)
453 
454 #define br_err(__br, format, args...)			\
455 	br_printk(KERN_ERR, __br, format, ##args)
456 #define br_warn(__br, format, args...)			\
457 	br_printk(KERN_WARNING, __br, format, ##args)
458 #define br_notice(__br, format, args...)		\
459 	br_printk(KERN_NOTICE, __br, format, ##args)
460 #define br_info(__br, format, args...)			\
461 	br_printk(KERN_INFO, __br, format, ##args)
462 
463 #define br_debug(br, format, args...)			\
464 	pr_debug("%s: " format,  (br)->dev->name, ##args)
465 
466 /* called under bridge lock */
467 static inline int br_is_root_bridge(const struct net_bridge *br)
468 {
469 	return !memcmp(&br->bridge_id, &br->designated_root, 8);
470 }
471 
472 /* check if a VLAN entry is global */
473 static inline bool br_vlan_is_master(const struct net_bridge_vlan *v)
474 {
475 	return v->flags & BRIDGE_VLAN_INFO_MASTER;
476 }
477 
478 /* check if a VLAN entry is used by the bridge */
479 static inline bool br_vlan_is_brentry(const struct net_bridge_vlan *v)
480 {
481 	return v->flags & BRIDGE_VLAN_INFO_BRENTRY;
482 }
483 
484 /* check if we should use the vlan entry, returns false if it's only context */
485 static inline bool br_vlan_should_use(const struct net_bridge_vlan *v)
486 {
487 	if (br_vlan_is_master(v)) {
488 		if (br_vlan_is_brentry(v))
489 			return true;
490 		else
491 			return false;
492 	}
493 
494 	return true;
495 }
496 
497 static inline int br_opt_get(const struct net_bridge *br,
498 			     enum net_bridge_opts opt)
499 {
500 	return test_bit(opt, &br->options);
501 }
502 
503 void br_opt_toggle(struct net_bridge *br, enum net_bridge_opts opt, bool on);
504 
505 /* br_device.c */
506 void br_dev_setup(struct net_device *dev);
507 void br_dev_delete(struct net_device *dev, struct list_head *list);
508 netdev_tx_t br_dev_xmit(struct sk_buff *skb, struct net_device *dev);
509 #ifdef CONFIG_NET_POLL_CONTROLLER
510 static inline void br_netpoll_send_skb(const struct net_bridge_port *p,
511 				       struct sk_buff *skb)
512 {
513 	struct netpoll *np = p->np;
514 
515 	if (np)
516 		netpoll_send_skb(np, skb);
517 }
518 
519 int br_netpoll_enable(struct net_bridge_port *p);
520 void br_netpoll_disable(struct net_bridge_port *p);
521 #else
522 static inline void br_netpoll_send_skb(const struct net_bridge_port *p,
523 				       struct sk_buff *skb)
524 {
525 }
526 
527 static inline int br_netpoll_enable(struct net_bridge_port *p)
528 {
529 	return 0;
530 }
531 
532 static inline void br_netpoll_disable(struct net_bridge_port *p)
533 {
534 }
535 #endif
536 
537 /* br_fdb.c */
538 int br_fdb_init(void);
539 void br_fdb_fini(void);
540 int br_fdb_hash_init(struct net_bridge *br);
541 void br_fdb_hash_fini(struct net_bridge *br);
542 void br_fdb_flush(struct net_bridge *br);
543 void br_fdb_find_delete_local(struct net_bridge *br,
544 			      const struct net_bridge_port *p,
545 			      const unsigned char *addr, u16 vid);
546 void br_fdb_changeaddr(struct net_bridge_port *p, const unsigned char *newaddr);
547 void br_fdb_change_mac_address(struct net_bridge *br, const u8 *newaddr);
548 void br_fdb_cleanup(struct work_struct *work);
549 void br_fdb_delete_by_port(struct net_bridge *br,
550 			   const struct net_bridge_port *p, u16 vid, int do_all);
551 struct net_bridge_fdb_entry *br_fdb_find_rcu(struct net_bridge *br,
552 					     const unsigned char *addr,
553 					     __u16 vid);
554 int br_fdb_test_addr(struct net_device *dev, unsigned char *addr);
555 int br_fdb_fillbuf(struct net_bridge *br, void *buf, unsigned long count,
556 		   unsigned long off);
557 int br_fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
558 		  const unsigned char *addr, u16 vid);
559 void br_fdb_update(struct net_bridge *br, struct net_bridge_port *source,
560 		   const unsigned char *addr, u16 vid, bool added_by_user);
561 
562 int br_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
563 		  struct net_device *dev, const unsigned char *addr, u16 vid);
564 int br_fdb_add(struct ndmsg *nlh, struct nlattr *tb[], struct net_device *dev,
565 	       const unsigned char *addr, u16 vid, u16 nlh_flags);
566 int br_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
567 		struct net_device *dev, struct net_device *fdev, int *idx);
568 int br_fdb_sync_static(struct net_bridge *br, struct net_bridge_port *p);
569 void br_fdb_unsync_static(struct net_bridge *br, struct net_bridge_port *p);
570 int br_fdb_external_learn_add(struct net_bridge *br, struct net_bridge_port *p,
571 			      const unsigned char *addr, u16 vid,
572 			      bool swdev_notify);
573 int br_fdb_external_learn_del(struct net_bridge *br, struct net_bridge_port *p,
574 			      const unsigned char *addr, u16 vid,
575 			      bool swdev_notify);
576 void br_fdb_offloaded_set(struct net_bridge *br, struct net_bridge_port *p,
577 			  const unsigned char *addr, u16 vid, bool offloaded);
578 
579 /* br_forward.c */
580 enum br_pkt_type {
581 	BR_PKT_UNICAST,
582 	BR_PKT_MULTICAST,
583 	BR_PKT_BROADCAST
584 };
585 int br_dev_queue_push_xmit(struct net *net, struct sock *sk, struct sk_buff *skb);
586 void br_forward(const struct net_bridge_port *to, struct sk_buff *skb,
587 		bool local_rcv, bool local_orig);
588 int br_forward_finish(struct net *net, struct sock *sk, struct sk_buff *skb);
589 void br_flood(struct net_bridge *br, struct sk_buff *skb,
590 	      enum br_pkt_type pkt_type, bool local_rcv, bool local_orig);
591 
592 /* return true if both source port and dest port are isolated */
593 static inline bool br_skb_isolated(const struct net_bridge_port *to,
594 				   const struct sk_buff *skb)
595 {
596 	return BR_INPUT_SKB_CB(skb)->src_port_isolated &&
597 	       (to->flags & BR_ISOLATED);
598 }
599 
600 /* br_if.c */
601 void br_port_carrier_check(struct net_bridge_port *p, bool *notified);
602 int br_add_bridge(struct net *net, const char *name);
603 int br_del_bridge(struct net *net, const char *name);
604 int br_add_if(struct net_bridge *br, struct net_device *dev,
605 	      struct netlink_ext_ack *extack);
606 int br_del_if(struct net_bridge *br, struct net_device *dev);
607 void br_mtu_auto_adjust(struct net_bridge *br);
608 netdev_features_t br_features_recompute(struct net_bridge *br,
609 					netdev_features_t features);
610 void br_port_flags_change(struct net_bridge_port *port, unsigned long mask);
611 void br_manage_promisc(struct net_bridge *br);
612 int nbp_backup_change(struct net_bridge_port *p, struct net_device *backup_dev);
613 
614 /* br_input.c */
615 int br_handle_frame_finish(struct net *net, struct sock *sk, struct sk_buff *skb);
616 rx_handler_result_t br_handle_frame(struct sk_buff **pskb);
617 
618 static inline bool br_rx_handler_check_rcu(const struct net_device *dev)
619 {
620 	return rcu_dereference(dev->rx_handler) == br_handle_frame;
621 }
622 
623 static inline bool br_rx_handler_check_rtnl(const struct net_device *dev)
624 {
625 	return rcu_dereference_rtnl(dev->rx_handler) == br_handle_frame;
626 }
627 
628 static inline struct net_bridge_port *br_port_get_check_rcu(const struct net_device *dev)
629 {
630 	return br_rx_handler_check_rcu(dev) ? br_port_get_rcu(dev) : NULL;
631 }
632 
633 static inline struct net_bridge_port *
634 br_port_get_check_rtnl(const struct net_device *dev)
635 {
636 	return br_rx_handler_check_rtnl(dev) ? br_port_get_rtnl_rcu(dev) : NULL;
637 }
638 
639 /* br_ioctl.c */
640 int br_dev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
641 int br_ioctl_deviceless_stub(struct net *net, unsigned int cmd,
642 			     void __user *arg);
643 
644 /* br_multicast.c */
645 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
646 extern unsigned int br_mdb_rehash_seq;
647 int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port,
648 		     struct sk_buff *skb, u16 vid);
649 struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br,
650 					struct sk_buff *skb, u16 vid);
651 int br_multicast_add_port(struct net_bridge_port *port);
652 void br_multicast_del_port(struct net_bridge_port *port);
653 void br_multicast_enable_port(struct net_bridge_port *port);
654 void br_multicast_disable_port(struct net_bridge_port *port);
655 void br_multicast_init(struct net_bridge *br);
656 void br_multicast_open(struct net_bridge *br);
657 void br_multicast_stop(struct net_bridge *br);
658 void br_multicast_dev_del(struct net_bridge *br);
659 void br_multicast_flood(struct net_bridge_mdb_entry *mdst,
660 			struct sk_buff *skb, bool local_rcv, bool local_orig);
661 int br_multicast_set_router(struct net_bridge *br, unsigned long val);
662 int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val);
663 int br_multicast_toggle(struct net_bridge *br, unsigned long val);
664 int br_multicast_set_querier(struct net_bridge *br, unsigned long val);
665 int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val);
666 int br_multicast_set_igmp_version(struct net_bridge *br, unsigned long val);
667 #if IS_ENABLED(CONFIG_IPV6)
668 int br_multicast_set_mld_version(struct net_bridge *br, unsigned long val);
669 #endif
670 struct net_bridge_mdb_entry *
671 br_mdb_ip_get(struct net_bridge_mdb_htable *mdb, struct br_ip *dst);
672 struct net_bridge_mdb_entry *
673 br_multicast_new_group(struct net_bridge *br, struct net_bridge_port *port,
674 		       struct br_ip *group);
675 void br_multicast_free_pg(struct rcu_head *head);
676 struct net_bridge_port_group *
677 br_multicast_new_port_group(struct net_bridge_port *port, struct br_ip *group,
678 			    struct net_bridge_port_group __rcu *next,
679 			    unsigned char flags, const unsigned char *src);
680 void br_mdb_init(void);
681 void br_mdb_uninit(void);
682 void br_mdb_notify(struct net_device *dev, struct net_bridge_port *port,
683 		   struct br_ip *group, int type, u8 flags);
684 void br_rtr_notify(struct net_device *dev, struct net_bridge_port *port,
685 		   int type);
686 void br_multicast_count(struct net_bridge *br, const struct net_bridge_port *p,
687 			const struct sk_buff *skb, u8 type, u8 dir);
688 int br_multicast_init_stats(struct net_bridge *br);
689 void br_multicast_uninit_stats(struct net_bridge *br);
690 void br_multicast_get_stats(const struct net_bridge *br,
691 			    const struct net_bridge_port *p,
692 			    struct br_mcast_stats *dest);
693 
694 #define mlock_dereference(X, br) \
695 	rcu_dereference_protected(X, lockdep_is_held(&br->multicast_lock))
696 
697 static inline bool br_multicast_is_router(struct net_bridge *br)
698 {
699 	return br->multicast_router == 2 ||
700 	       (br->multicast_router == 1 &&
701 		timer_pending(&br->multicast_router_timer));
702 }
703 
704 static inline bool
705 __br_multicast_querier_exists(struct net_bridge *br,
706 				struct bridge_mcast_other_query *querier,
707 				const bool is_ipv6)
708 {
709 	bool own_querier_enabled;
710 
711 	if (br_opt_get(br, BROPT_MULTICAST_QUERIER)) {
712 		if (is_ipv6 && !br_opt_get(br, BROPT_HAS_IPV6_ADDR))
713 			own_querier_enabled = false;
714 		else
715 			own_querier_enabled = true;
716 	} else {
717 		own_querier_enabled = false;
718 	}
719 
720 	return time_is_before_jiffies(querier->delay_time) &&
721 	       (own_querier_enabled || timer_pending(&querier->timer));
722 }
723 
724 static inline bool br_multicast_querier_exists(struct net_bridge *br,
725 					       struct ethhdr *eth)
726 {
727 	switch (eth->h_proto) {
728 	case (htons(ETH_P_IP)):
729 		return __br_multicast_querier_exists(br,
730 			&br->ip4_other_query, false);
731 #if IS_ENABLED(CONFIG_IPV6)
732 	case (htons(ETH_P_IPV6)):
733 		return __br_multicast_querier_exists(br,
734 			&br->ip6_other_query, true);
735 #endif
736 	default:
737 		return false;
738 	}
739 }
740 
741 static inline int br_multicast_igmp_type(const struct sk_buff *skb)
742 {
743 	return BR_INPUT_SKB_CB(skb)->igmp;
744 }
745 #else
746 static inline int br_multicast_rcv(struct net_bridge *br,
747 				   struct net_bridge_port *port,
748 				   struct sk_buff *skb,
749 				   u16 vid)
750 {
751 	return 0;
752 }
753 
754 static inline struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br,
755 						      struct sk_buff *skb, u16 vid)
756 {
757 	return NULL;
758 }
759 
760 static inline int br_multicast_add_port(struct net_bridge_port *port)
761 {
762 	return 0;
763 }
764 
765 static inline void br_multicast_del_port(struct net_bridge_port *port)
766 {
767 }
768 
769 static inline void br_multicast_enable_port(struct net_bridge_port *port)
770 {
771 }
772 
773 static inline void br_multicast_disable_port(struct net_bridge_port *port)
774 {
775 }
776 
777 static inline void br_multicast_init(struct net_bridge *br)
778 {
779 }
780 
781 static inline void br_multicast_open(struct net_bridge *br)
782 {
783 }
784 
785 static inline void br_multicast_stop(struct net_bridge *br)
786 {
787 }
788 
789 static inline void br_multicast_dev_del(struct net_bridge *br)
790 {
791 }
792 
793 static inline void br_multicast_flood(struct net_bridge_mdb_entry *mdst,
794 				      struct sk_buff *skb,
795 				      bool local_rcv, bool local_orig)
796 {
797 }
798 
799 static inline bool br_multicast_is_router(struct net_bridge *br)
800 {
801 	return false;
802 }
803 
804 static inline bool br_multicast_querier_exists(struct net_bridge *br,
805 					       struct ethhdr *eth)
806 {
807 	return false;
808 }
809 
810 static inline void br_mdb_init(void)
811 {
812 }
813 
814 static inline void br_mdb_uninit(void)
815 {
816 }
817 
818 static inline void br_multicast_count(struct net_bridge *br,
819 				      const struct net_bridge_port *p,
820 				      const struct sk_buff *skb,
821 				      u8 type, u8 dir)
822 {
823 }
824 
825 static inline int br_multicast_init_stats(struct net_bridge *br)
826 {
827 	return 0;
828 }
829 
830 static inline void br_multicast_uninit_stats(struct net_bridge *br)
831 {
832 }
833 
834 static inline int br_multicast_igmp_type(const struct sk_buff *skb)
835 {
836 	return 0;
837 }
838 #endif
839 
840 /* br_vlan.c */
841 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
842 bool br_allowed_ingress(const struct net_bridge *br,
843 			struct net_bridge_vlan_group *vg, struct sk_buff *skb,
844 			u16 *vid);
845 bool br_allowed_egress(struct net_bridge_vlan_group *vg,
846 		       const struct sk_buff *skb);
847 bool br_should_learn(struct net_bridge_port *p, struct sk_buff *skb, u16 *vid);
848 struct sk_buff *br_handle_vlan(struct net_bridge *br,
849 			       const struct net_bridge_port *port,
850 			       struct net_bridge_vlan_group *vg,
851 			       struct sk_buff *skb);
852 int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags,
853 		bool *changed);
854 int br_vlan_delete(struct net_bridge *br, u16 vid);
855 void br_vlan_flush(struct net_bridge *br);
856 struct net_bridge_vlan *br_vlan_find(struct net_bridge_vlan_group *vg, u16 vid);
857 void br_recalculate_fwd_mask(struct net_bridge *br);
858 int __br_vlan_filter_toggle(struct net_bridge *br, unsigned long val);
859 int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val);
860 int __br_vlan_set_proto(struct net_bridge *br, __be16 proto);
861 int br_vlan_set_proto(struct net_bridge *br, unsigned long val);
862 int br_vlan_set_stats(struct net_bridge *br, unsigned long val);
863 int br_vlan_set_stats_per_port(struct net_bridge *br, unsigned long val);
864 int br_vlan_init(struct net_bridge *br);
865 int br_vlan_set_default_pvid(struct net_bridge *br, unsigned long val);
866 int __br_vlan_set_default_pvid(struct net_bridge *br, u16 pvid);
867 int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags,
868 		 bool *changed);
869 int nbp_vlan_delete(struct net_bridge_port *port, u16 vid);
870 void nbp_vlan_flush(struct net_bridge_port *port);
871 int nbp_vlan_init(struct net_bridge_port *port);
872 int nbp_get_num_vlan_infos(struct net_bridge_port *p, u32 filter_mask);
873 void br_vlan_get_stats(const struct net_bridge_vlan *v,
874 		       struct br_vlan_stats *stats);
875 
876 static inline struct net_bridge_vlan_group *br_vlan_group(
877 					const struct net_bridge *br)
878 {
879 	return rtnl_dereference(br->vlgrp);
880 }
881 
882 static inline struct net_bridge_vlan_group *nbp_vlan_group(
883 					const struct net_bridge_port *p)
884 {
885 	return rtnl_dereference(p->vlgrp);
886 }
887 
888 static inline struct net_bridge_vlan_group *br_vlan_group_rcu(
889 					const struct net_bridge *br)
890 {
891 	return rcu_dereference(br->vlgrp);
892 }
893 
894 static inline struct net_bridge_vlan_group *nbp_vlan_group_rcu(
895 					const struct net_bridge_port *p)
896 {
897 	return rcu_dereference(p->vlgrp);
898 }
899 
900 /* Since bridge now depends on 8021Q module, but the time bridge sees the
901  * skb, the vlan tag will always be present if the frame was tagged.
902  */
903 static inline int br_vlan_get_tag(const struct sk_buff *skb, u16 *vid)
904 {
905 	int err = 0;
906 
907 	if (skb_vlan_tag_present(skb)) {
908 		*vid = skb_vlan_tag_get(skb) & VLAN_VID_MASK;
909 	} else {
910 		*vid = 0;
911 		err = -EINVAL;
912 	}
913 
914 	return err;
915 }
916 
917 static inline u16 br_get_pvid(const struct net_bridge_vlan_group *vg)
918 {
919 	if (!vg)
920 		return 0;
921 
922 	smp_rmb();
923 	return vg->pvid;
924 }
925 
926 #else
927 static inline bool br_allowed_ingress(const struct net_bridge *br,
928 				      struct net_bridge_vlan_group *vg,
929 				      struct sk_buff *skb,
930 				      u16 *vid)
931 {
932 	return true;
933 }
934 
935 static inline bool br_allowed_egress(struct net_bridge_vlan_group *vg,
936 				     const struct sk_buff *skb)
937 {
938 	return true;
939 }
940 
941 static inline bool br_should_learn(struct net_bridge_port *p,
942 				   struct sk_buff *skb, u16 *vid)
943 {
944 	return true;
945 }
946 
947 static inline struct sk_buff *br_handle_vlan(struct net_bridge *br,
948 					     const struct net_bridge_port *port,
949 					     struct net_bridge_vlan_group *vg,
950 					     struct sk_buff *skb)
951 {
952 	return skb;
953 }
954 
955 static inline int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags,
956 			      bool *changed)
957 {
958 	*changed = false;
959 	return -EOPNOTSUPP;
960 }
961 
962 static inline int br_vlan_delete(struct net_bridge *br, u16 vid)
963 {
964 	return -EOPNOTSUPP;
965 }
966 
967 static inline void br_vlan_flush(struct net_bridge *br)
968 {
969 }
970 
971 static inline void br_recalculate_fwd_mask(struct net_bridge *br)
972 {
973 }
974 
975 static inline int br_vlan_init(struct net_bridge *br)
976 {
977 	return 0;
978 }
979 
980 static inline int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags,
981 			       bool *changed)
982 {
983 	*changed = false;
984 	return -EOPNOTSUPP;
985 }
986 
987 static inline int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
988 {
989 	return -EOPNOTSUPP;
990 }
991 
992 static inline void nbp_vlan_flush(struct net_bridge_port *port)
993 {
994 }
995 
996 static inline struct net_bridge_vlan *br_vlan_find(struct net_bridge_vlan_group *vg,
997 						   u16 vid)
998 {
999 	return NULL;
1000 }
1001 
1002 static inline int nbp_vlan_init(struct net_bridge_port *port)
1003 {
1004 	return 0;
1005 }
1006 
1007 static inline u16 br_vlan_get_tag(const struct sk_buff *skb, u16 *tag)
1008 {
1009 	return 0;
1010 }
1011 
1012 static inline u16 br_get_pvid(const struct net_bridge_vlan_group *vg)
1013 {
1014 	return 0;
1015 }
1016 
1017 static inline int __br_vlan_filter_toggle(struct net_bridge *br,
1018 					  unsigned long val)
1019 {
1020 	return -EOPNOTSUPP;
1021 }
1022 
1023 static inline int nbp_get_num_vlan_infos(struct net_bridge_port *p,
1024 					 u32 filter_mask)
1025 {
1026 	return 0;
1027 }
1028 
1029 static inline struct net_bridge_vlan_group *br_vlan_group(
1030 					const struct net_bridge *br)
1031 {
1032 	return NULL;
1033 }
1034 
1035 static inline struct net_bridge_vlan_group *nbp_vlan_group(
1036 					const struct net_bridge_port *p)
1037 {
1038 	return NULL;
1039 }
1040 
1041 static inline struct net_bridge_vlan_group *br_vlan_group_rcu(
1042 					const struct net_bridge *br)
1043 {
1044 	return NULL;
1045 }
1046 
1047 static inline struct net_bridge_vlan_group *nbp_vlan_group_rcu(
1048 					const struct net_bridge_port *p)
1049 {
1050 	return NULL;
1051 }
1052 
1053 static inline void br_vlan_get_stats(const struct net_bridge_vlan *v,
1054 				     struct br_vlan_stats *stats)
1055 {
1056 }
1057 #endif
1058 
1059 struct nf_br_ops {
1060 	int (*br_dev_xmit_hook)(struct sk_buff *skb);
1061 };
1062 extern const struct nf_br_ops __rcu *nf_br_ops;
1063 
1064 /* br_netfilter.c */
1065 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1066 int br_nf_core_init(void);
1067 void br_nf_core_fini(void);
1068 void br_netfilter_rtable_init(struct net_bridge *);
1069 #else
1070 static inline int br_nf_core_init(void) { return 0; }
1071 static inline void br_nf_core_fini(void) {}
1072 #define br_netfilter_rtable_init(x)
1073 #endif
1074 
1075 /* br_stp.c */
1076 void br_set_state(struct net_bridge_port *p, unsigned int state);
1077 struct net_bridge_port *br_get_port(struct net_bridge *br, u16 port_no);
1078 void br_init_port(struct net_bridge_port *p);
1079 void br_become_designated_port(struct net_bridge_port *p);
1080 
1081 void __br_set_forward_delay(struct net_bridge *br, unsigned long t);
1082 int br_set_forward_delay(struct net_bridge *br, unsigned long x);
1083 int br_set_hello_time(struct net_bridge *br, unsigned long x);
1084 int br_set_max_age(struct net_bridge *br, unsigned long x);
1085 int __set_ageing_time(struct net_device *dev, unsigned long t);
1086 int br_set_ageing_time(struct net_bridge *br, clock_t ageing_time);
1087 
1088 
1089 /* br_stp_if.c */
1090 void br_stp_enable_bridge(struct net_bridge *br);
1091 void br_stp_disable_bridge(struct net_bridge *br);
1092 void br_stp_set_enabled(struct net_bridge *br, unsigned long val);
1093 void br_stp_enable_port(struct net_bridge_port *p);
1094 void br_stp_disable_port(struct net_bridge_port *p);
1095 bool br_stp_recalculate_bridge_id(struct net_bridge *br);
1096 void br_stp_change_bridge_id(struct net_bridge *br, const unsigned char *a);
1097 void br_stp_set_bridge_priority(struct net_bridge *br, u16 newprio);
1098 int br_stp_set_port_priority(struct net_bridge_port *p, unsigned long newprio);
1099 int br_stp_set_path_cost(struct net_bridge_port *p, unsigned long path_cost);
1100 ssize_t br_show_bridge_id(char *buf, const struct bridge_id *id);
1101 
1102 /* br_stp_bpdu.c */
1103 struct stp_proto;
1104 void br_stp_rcv(const struct stp_proto *proto, struct sk_buff *skb,
1105 		struct net_device *dev);
1106 
1107 /* br_stp_timer.c */
1108 void br_stp_timer_init(struct net_bridge *br);
1109 void br_stp_port_timer_init(struct net_bridge_port *p);
1110 unsigned long br_timer_value(const struct timer_list *timer);
1111 
1112 /* br.c */
1113 #if IS_ENABLED(CONFIG_ATM_LANE)
1114 extern int (*br_fdb_test_addr_hook)(struct net_device *dev, unsigned char *addr);
1115 #endif
1116 
1117 /* br_netlink.c */
1118 extern struct rtnl_link_ops br_link_ops;
1119 int br_netlink_init(void);
1120 void br_netlink_fini(void);
1121 void br_ifinfo_notify(int event, const struct net_bridge *br,
1122 		      const struct net_bridge_port *port);
1123 int br_setlink(struct net_device *dev, struct nlmsghdr *nlmsg, u16 flags);
1124 int br_dellink(struct net_device *dev, struct nlmsghdr *nlmsg, u16 flags);
1125 int br_getlink(struct sk_buff *skb, u32 pid, u32 seq, struct net_device *dev,
1126 	       u32 filter_mask, int nlflags);
1127 
1128 #ifdef CONFIG_SYSFS
1129 /* br_sysfs_if.c */
1130 extern const struct sysfs_ops brport_sysfs_ops;
1131 int br_sysfs_addif(struct net_bridge_port *p);
1132 int br_sysfs_renameif(struct net_bridge_port *p);
1133 
1134 /* br_sysfs_br.c */
1135 int br_sysfs_addbr(struct net_device *dev);
1136 void br_sysfs_delbr(struct net_device *dev);
1137 
1138 #else
1139 
1140 static inline int br_sysfs_addif(struct net_bridge_port *p) { return 0; }
1141 static inline int br_sysfs_renameif(struct net_bridge_port *p) { return 0; }
1142 static inline int br_sysfs_addbr(struct net_device *dev) { return 0; }
1143 static inline void br_sysfs_delbr(struct net_device *dev) { return; }
1144 #endif /* CONFIG_SYSFS */
1145 
1146 /* br_switchdev.c */
1147 #ifdef CONFIG_NET_SWITCHDEV
1148 int nbp_switchdev_mark_set(struct net_bridge_port *p);
1149 void nbp_switchdev_frame_mark(const struct net_bridge_port *p,
1150 			      struct sk_buff *skb);
1151 bool nbp_switchdev_allowed_egress(const struct net_bridge_port *p,
1152 				  const struct sk_buff *skb);
1153 int br_switchdev_set_port_flag(struct net_bridge_port *p,
1154 			       unsigned long flags,
1155 			       unsigned long mask);
1156 void br_switchdev_fdb_notify(const struct net_bridge_fdb_entry *fdb,
1157 			     int type);
1158 int br_switchdev_port_vlan_add(struct net_device *dev, u16 vid, u16 flags);
1159 int br_switchdev_port_vlan_del(struct net_device *dev, u16 vid);
1160 
1161 static inline void br_switchdev_frame_unmark(struct sk_buff *skb)
1162 {
1163 	skb->offload_fwd_mark = 0;
1164 }
1165 #else
1166 static inline int nbp_switchdev_mark_set(struct net_bridge_port *p)
1167 {
1168 	return 0;
1169 }
1170 
1171 static inline void nbp_switchdev_frame_mark(const struct net_bridge_port *p,
1172 					    struct sk_buff *skb)
1173 {
1174 }
1175 
1176 static inline bool nbp_switchdev_allowed_egress(const struct net_bridge_port *p,
1177 						const struct sk_buff *skb)
1178 {
1179 	return true;
1180 }
1181 
1182 static inline int br_switchdev_set_port_flag(struct net_bridge_port *p,
1183 					     unsigned long flags,
1184 					     unsigned long mask)
1185 {
1186 	return 0;
1187 }
1188 
1189 static inline int br_switchdev_port_vlan_add(struct net_device *dev,
1190 					     u16 vid, u16 flags)
1191 {
1192 	return -EOPNOTSUPP;
1193 }
1194 
1195 static inline int br_switchdev_port_vlan_del(struct net_device *dev, u16 vid)
1196 {
1197 	return -EOPNOTSUPP;
1198 }
1199 
1200 static inline void
1201 br_switchdev_fdb_notify(const struct net_bridge_fdb_entry *fdb, int type)
1202 {
1203 }
1204 
1205 static inline void br_switchdev_frame_unmark(struct sk_buff *skb)
1206 {
1207 }
1208 #endif /* CONFIG_NET_SWITCHDEV */
1209 
1210 /* br_arp_nd_proxy.c */
1211 void br_recalculate_neigh_suppress_enabled(struct net_bridge *br);
1212 void br_do_proxy_suppress_arp(struct sk_buff *skb, struct net_bridge *br,
1213 			      u16 vid, struct net_bridge_port *p);
1214 void br_do_suppress_nd(struct sk_buff *skb, struct net_bridge *br,
1215 		       u16 vid, struct net_bridge_port *p, struct nd_msg *msg);
1216 struct nd_msg *br_is_nd_neigh_msg(struct sk_buff *skb, struct nd_msg *m);
1217 #endif
1218