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