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