1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Linux ethernet bridge
6 * Lennert Buytenhek <buytenh@gnu.org>
12 #include <linux/netdevice.h>
13 #include <linux/if_bridge.h>
14 #include <linux/netpoll.h>
15 #include <linux/u64_stats_sync.h>
16 #include <net/route.h>
17 #include <net/ip6_fib.h>
18 #include <linux/if_vlan.h>
19 #include <linux/rhashtable.h>
20 #include <linux/refcount.h>
22 #define BR_HASH_BITS 8
23 #define BR_HASH_SIZE (1 << BR_HASH_BITS)
25 #define BR_HOLD_TIME (1*HZ)
27 #define BR_PORT_BITS 10
28 #define BR_MAX_PORTS (1<<BR_PORT_BITS)
30 #define BR_MULTICAST_DEFAULT_HASH_MAX 4096
31 #define BR_MULTICAST_QUERY_INTVL_MIN msecs_to_jiffies(1000)
32 #define BR_MULTICAST_STARTUP_QUERY_INTVL_MIN BR_MULTICAST_QUERY_INTVL_MIN
34 #define BR_HWDOM_MAX BITS_PER_LONG
36 #define BR_VERSION "2.3"
38 /* Control of forwarding link local multicast */
39 #define BR_GROUPFWD_DEFAULT 0
40 /* Don't allow forwarding of control protocols like STP, MAC PAUSE and LACP */
42 BR_GROUPFWD_STP = BIT(0),
43 BR_GROUPFWD_MACPAUSE = BIT(1),
44 BR_GROUPFWD_LACP = BIT(2),
47 #define BR_GROUPFWD_RESTRICTED (BR_GROUPFWD_STP | BR_GROUPFWD_MACPAUSE | \
49 /* The Nearest Customer Bridge Group Address, 01-80-C2-00-00-[00,0B,0C,0D,0F] */
50 #define BR_GROUPFWD_8021AD 0xB801u
52 /* Path to usermode spanning tree program */
53 #define BR_STP_PROG "/sbin/bridge-stp"
55 #define BR_FDB_NOTIFY_SETTABLE_BITS (FDB_NOTIFY_BIT | FDB_NOTIFY_INACTIVE_BIT)
57 typedef struct bridge_id bridge_id;
58 typedef struct mac_addr mac_addr;
59 typedef __u16 port_id;
62 unsigned char prio[2];
63 unsigned char addr[ETH_ALEN];
67 unsigned char addr[ETH_ALEN];
70 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
72 struct bridge_mcast_own_query {
73 struct timer_list timer;
78 struct bridge_mcast_other_query {
79 struct timer_list timer;
80 unsigned long delay_time;
83 /* selected querier */
84 struct bridge_mcast_querier {
87 seqcount_spinlock_t seq;
90 /* IGMP/MLD statistics */
91 struct bridge_mcast_stats {
92 struct br_mcast_stats mstats;
93 struct u64_stats_sync syncp;
97 /* net_bridge_mcast_port must be always defined due to forwarding stubs */
98 struct net_bridge_mcast_port {
99 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
100 struct net_bridge_port *port;
101 struct net_bridge_vlan *vlan;
103 struct bridge_mcast_own_query ip4_own_query;
104 struct timer_list ip4_mc_router_timer;
105 struct hlist_node ip4_rlist;
106 #if IS_ENABLED(CONFIG_IPV6)
107 struct bridge_mcast_own_query ip6_own_query;
108 struct timer_list ip6_mc_router_timer;
109 struct hlist_node ip6_rlist;
110 #endif /* IS_ENABLED(CONFIG_IPV6) */
111 unsigned char multicast_router;
112 #endif /* CONFIG_BRIDGE_IGMP_SNOOPING */
115 /* net_bridge_mcast must be always defined due to forwarding stubs */
116 struct net_bridge_mcast {
117 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
118 struct net_bridge *br;
119 struct net_bridge_vlan *vlan;
121 u32 multicast_last_member_count;
122 u32 multicast_startup_query_count;
124 u8 multicast_querier;
125 u8 multicast_igmp_version;
127 #if IS_ENABLED(CONFIG_IPV6)
128 u8 multicast_mld_version;
130 unsigned long multicast_last_member_interval;
131 unsigned long multicast_membership_interval;
132 unsigned long multicast_querier_interval;
133 unsigned long multicast_query_interval;
134 unsigned long multicast_query_response_interval;
135 unsigned long multicast_startup_query_interval;
136 struct hlist_head ip4_mc_router_list;
137 struct timer_list ip4_mc_router_timer;
138 struct bridge_mcast_other_query ip4_other_query;
139 struct bridge_mcast_own_query ip4_own_query;
140 struct bridge_mcast_querier ip4_querier;
141 #if IS_ENABLED(CONFIG_IPV6)
142 struct hlist_head ip6_mc_router_list;
143 struct timer_list ip6_mc_router_timer;
144 struct bridge_mcast_other_query ip6_other_query;
145 struct bridge_mcast_own_query ip6_own_query;
146 struct bridge_mcast_querier ip6_querier;
147 #endif /* IS_ENABLED(CONFIG_IPV6) */
148 #endif /* CONFIG_BRIDGE_IGMP_SNOOPING */
151 struct br_tunnel_info {
153 struct metadata_dst __rcu *tunnel_dst;
156 /* private vlan flags */
158 BR_VLFLAG_PER_PORT_STATS = BIT(0),
159 BR_VLFLAG_ADDED_BY_SWITCHDEV = BIT(1),
160 BR_VLFLAG_MCAST_ENABLED = BIT(2),
161 BR_VLFLAG_GLOBAL_MCAST_ENABLED = BIT(3),
165 * struct net_bridge_vlan - per-vlan entry
167 * @vnode: rhashtable member
169 * @flags: bridge vlan flags
170 * @priv_flags: private (in-kernel) bridge vlan flags
171 * @state: STP state (e.g. blocking, learning, forwarding)
172 * @stats: per-cpu VLAN statistics
173 * @br: if MASTER flag set, this points to a bridge struct
174 * @port: if MASTER flag unset, this points to a port struct
175 * @refcnt: if MASTER flag set, this is bumped for each port referencing it
176 * @brvlan: if MASTER flag unset, this points to the global per-VLAN context
177 * for this VLAN entry
178 * @br_mcast_ctx: if MASTER flag set, this is the global vlan multicast context
179 * @port_mcast_ctx: if MASTER flag unset, this is the per-port/vlan multicast
181 * @vlist: sorted list of VLAN entries
182 * @rcu: used for entry destruction
184 * This structure is shared between the global per-VLAN entries contained in
185 * the bridge rhashtable and the local per-port per-VLAN entries contained in
186 * the port's rhashtable. The union entries should be interpreted depending on
187 * the entry flags that are set.
189 struct net_bridge_vlan {
190 struct rhash_head vnode;
191 struct rhash_head tnode;
196 struct pcpu_sw_netstats __percpu *stats;
198 struct net_bridge *br;
199 struct net_bridge_port *port;
203 struct net_bridge_vlan *brvlan;
206 struct br_tunnel_info tinfo;
209 struct net_bridge_mcast br_mcast_ctx;
210 struct net_bridge_mcast_port port_mcast_ctx;
213 struct list_head vlist;
219 * struct net_bridge_vlan_group
221 * @vlan_hash: VLAN entry rhashtable
222 * @vlan_list: sorted VLAN entry list
223 * @num_vlans: number of total VLAN entries
224 * @pvid: PVID VLAN id
225 * @pvid_state: PVID's STP state (e.g. forwarding, learning, blocking)
227 * IMPORTANT: Be careful when checking if there're VLAN entries using list
228 * primitives because the bridge can have entries in its list which
229 * are just for global context but not for filtering, i.e. they have
230 * the master flag set but not the brentry flag. If you have to check
231 * if there're "real" entries in the bridge please test @num_vlans
233 struct net_bridge_vlan_group {
234 struct rhashtable vlan_hash;
235 struct rhashtable tunnel_hash;
236 struct list_head vlan_list;
242 /* bridge fdb flags */
247 BR_FDB_ADDED_BY_USER,
248 BR_FDB_ADDED_BY_EXT_LEARN,
251 BR_FDB_NOTIFY_INACTIVE
254 struct net_bridge_fdb_key {
259 struct net_bridge_fdb_entry {
260 struct rhash_head rhnode;
261 struct net_bridge_port *dst;
263 struct net_bridge_fdb_key key;
264 struct hlist_node fdb_node;
267 /* write-heavy members should not affect lookups */
268 unsigned long updated ____cacheline_aligned_in_smp;
274 #define MDB_PG_FLAGS_PERMANENT BIT(0)
275 #define MDB_PG_FLAGS_OFFLOAD BIT(1)
276 #define MDB_PG_FLAGS_FAST_LEAVE BIT(2)
277 #define MDB_PG_FLAGS_STAR_EXCL BIT(3)
278 #define MDB_PG_FLAGS_BLOCKED BIT(4)
280 #define PG_SRC_ENT_LIMIT 32
282 #define BR_SGRP_F_DELETE BIT(0)
283 #define BR_SGRP_F_SEND BIT(1)
284 #define BR_SGRP_F_INSTALLED BIT(2)
286 struct net_bridge_mcast_gc {
287 struct hlist_node gc_node;
288 void (*destroy)(struct net_bridge_mcast_gc *gc);
291 struct net_bridge_group_src {
292 struct hlist_node node;
295 struct net_bridge_port_group *pg;
297 u8 src_query_rexmit_cnt;
298 struct timer_list timer;
300 struct net_bridge *br;
301 struct net_bridge_mcast_gc mcast_gc;
305 struct net_bridge_port_group_sg_key {
306 struct net_bridge_port *port;
310 struct net_bridge_port_group {
311 struct net_bridge_port_group __rcu *next;
312 struct net_bridge_port_group_sg_key key;
313 unsigned char eth_addr[ETH_ALEN] __aligned(2);
315 unsigned char filter_mode;
316 unsigned char grp_query_rexmit_cnt;
317 unsigned char rt_protocol;
319 struct hlist_head src_list;
320 unsigned int src_ents;
321 struct timer_list timer;
322 struct timer_list rexmit_timer;
323 struct hlist_node mglist;
324 struct rb_root eht_set_tree;
325 struct rb_root eht_host_tree;
327 struct rhash_head rhnode;
328 struct net_bridge_mcast_gc mcast_gc;
332 struct net_bridge_mdb_entry {
333 struct rhash_head rhnode;
334 struct net_bridge *br;
335 struct net_bridge_port_group __rcu *ports;
339 struct timer_list timer;
340 struct hlist_node mdb_node;
342 struct net_bridge_mcast_gc mcast_gc;
346 struct net_bridge_port {
347 struct net_bridge *br;
348 struct net_device *dev;
349 struct list_head list;
352 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
353 struct net_bridge_vlan_group __rcu *vlgrp;
355 struct net_bridge_port __rcu *backup_port;
361 unsigned char topology_change_ack;
362 unsigned char config_pending;
364 port_id designated_port;
365 bridge_id designated_root;
366 bridge_id designated_bridge;
369 unsigned long designated_age;
371 struct timer_list forward_delay_timer;
372 struct timer_list hold_timer;
373 struct timer_list message_age_timer;
377 struct net_bridge_mcast_port multicast_ctx;
379 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
380 struct bridge_mcast_stats __percpu *mcast_stats;
382 u32 multicast_eht_hosts_limit;
383 u32 multicast_eht_hosts_cnt;
384 struct hlist_head mglist;
388 char sysfs_name[IFNAMSIZ];
391 #ifdef CONFIG_NET_POLL_CONTROLLER
394 #ifdef CONFIG_NET_SWITCHDEV
395 /* Identifier used to group ports that share the same switchdev
400 struct netdev_phys_item_id ppid;
403 u16 backup_redirected_cnt;
405 struct bridge_stp_xstats stp_xstats;
408 #define kobj_to_brport(obj) container_of(obj, struct net_bridge_port, kobj)
410 #define br_auto_port(p) ((p)->flags & BR_AUTO_MASK)
411 #define br_promisc_port(p) ((p)->flags & BR_PROMISC)
413 static inline struct net_bridge_port *br_port_get_rcu(const struct net_device *dev)
415 return rcu_dereference(dev->rx_handler_data);
418 static inline struct net_bridge_port *br_port_get_rtnl(const struct net_device *dev)
420 return netif_is_bridge_port(dev) ?
421 rtnl_dereference(dev->rx_handler_data) : NULL;
424 static inline struct net_bridge_port *br_port_get_rtnl_rcu(const struct net_device *dev)
426 return netif_is_bridge_port(dev) ?
427 rcu_dereference_rtnl(dev->rx_handler_data) : NULL;
430 enum net_bridge_opts {
432 BROPT_VLAN_STATS_ENABLED,
433 BROPT_NF_CALL_IPTABLES,
434 BROPT_NF_CALL_IP6TABLES,
435 BROPT_NF_CALL_ARPTABLES,
436 BROPT_GROUP_ADDR_SET,
437 BROPT_MULTICAST_ENABLED,
438 BROPT_MULTICAST_QUERY_USE_IFADDR,
439 BROPT_MULTICAST_STATS_ENABLED,
441 BROPT_NEIGH_SUPPRESS_ENABLED,
442 BROPT_MTU_SET_BY_USER,
443 BROPT_VLAN_STATS_PER_PORT,
445 BROPT_VLAN_BRIDGE_BINDING,
446 BROPT_MCAST_VLAN_SNOOPING_ENABLED,
451 spinlock_t hash_lock;
452 struct hlist_head frame_type_list;
453 struct net_device *dev;
454 unsigned long options;
455 /* These fields are accessed on each packet */
456 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
459 struct net_bridge_vlan_group __rcu *vlgrp;
462 struct rhashtable fdb_hash_tbl;
463 struct list_head port_list;
464 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
466 struct rtable fake_rtable;
467 struct rt6_info fake_rt6_info;
471 u16 group_fwd_mask_required;
474 bridge_id designated_root;
476 unsigned char topology_change;
477 unsigned char topology_change_detected;
479 unsigned long max_age;
480 unsigned long hello_time;
481 unsigned long forward_delay;
482 unsigned long ageing_time;
483 unsigned long bridge_max_age;
484 unsigned long bridge_hello_time;
485 unsigned long bridge_forward_delay;
486 unsigned long bridge_ageing_time;
489 u8 group_addr[ETH_ALEN];
492 BR_NO_STP, /* no spanning tree */
493 BR_KERNEL_STP, /* old STP in kernel */
494 BR_USER_STP, /* new RSTP in userspace */
497 struct net_bridge_mcast multicast_ctx;
499 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
500 struct bridge_mcast_stats __percpu *mcast_stats;
504 spinlock_t multicast_lock;
506 struct rhashtable mdb_hash_tbl;
507 struct rhashtable sg_port_tbl;
509 struct hlist_head mcast_gc_list;
510 struct hlist_head mdb_list;
512 struct work_struct mcast_gc_work;
515 struct timer_list hello_timer;
516 struct timer_list tcn_timer;
517 struct timer_list topology_change_timer;
518 struct delayed_work gc_work;
519 struct kobject *ifobj;
522 #ifdef CONFIG_NET_SWITCHDEV
523 /* Counter used to make sure that hardware domains get unique
524 * identifiers in case a bridge spans multiple switchdev instances.
527 /* Bit mask of hardware domain numbers in use */
528 unsigned long busy_hwdoms;
530 struct hlist_head fdb_list;
532 #if IS_ENABLED(CONFIG_BRIDGE_MRP)
533 struct hlist_head mrp_list;
535 #if IS_ENABLED(CONFIG_BRIDGE_CFM)
536 struct hlist_head mep_list;
540 struct br_input_skb_cb {
541 struct net_device *brdev;
544 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
548 u8 proxyarp_replied:1;
549 u8 src_port_isolated:1;
550 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
553 #ifdef CONFIG_NETFILTER_FAMILY_BRIDGE
554 u8 br_netfilter_broute:1;
557 #ifdef CONFIG_NET_SWITCHDEV
558 /* Set if TX data plane offloading is used towards at least one
562 /* The switchdev hardware domain from which this packet was received.
563 * If skb->offload_fwd_mark was set, then this packet was already
564 * forwarded by hardware to the other ports in the source hardware
565 * domain, otherwise it wasn't.
568 /* Bit mask of hardware domains towards this packet has already been
569 * transmitted using the TX data plane offload.
571 unsigned long fwd_hwdoms;
575 #define BR_INPUT_SKB_CB(__skb) ((struct br_input_skb_cb *)(__skb)->cb)
577 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
578 # define BR_INPUT_SKB_CB_MROUTERS_ONLY(__skb) (BR_INPUT_SKB_CB(__skb)->mrouters_only)
580 # define BR_INPUT_SKB_CB_MROUTERS_ONLY(__skb) (0)
583 #define br_printk(level, br, format, args...) \
584 printk(level "%s: " format, (br)->dev->name, ##args)
586 #define br_err(__br, format, args...) \
587 br_printk(KERN_ERR, __br, format, ##args)
588 #define br_warn(__br, format, args...) \
589 br_printk(KERN_WARNING, __br, format, ##args)
590 #define br_notice(__br, format, args...) \
591 br_printk(KERN_NOTICE, __br, format, ##args)
592 #define br_info(__br, format, args...) \
593 br_printk(KERN_INFO, __br, format, ##args)
595 #define br_debug(br, format, args...) \
596 pr_debug("%s: " format, (br)->dev->name, ##args)
598 /* called under bridge lock */
599 static inline int br_is_root_bridge(const struct net_bridge *br)
601 return !memcmp(&br->bridge_id, &br->designated_root, 8);
604 /* check if a VLAN entry is global */
605 static inline bool br_vlan_is_master(const struct net_bridge_vlan *v)
607 return v->flags & BRIDGE_VLAN_INFO_MASTER;
610 /* check if a VLAN entry is used by the bridge */
611 static inline bool br_vlan_is_brentry(const struct net_bridge_vlan *v)
613 return v->flags & BRIDGE_VLAN_INFO_BRENTRY;
616 /* check if we should use the vlan entry, returns false if it's only context */
617 static inline bool br_vlan_should_use(const struct net_bridge_vlan *v)
619 if (br_vlan_is_master(v)) {
620 if (br_vlan_is_brentry(v))
629 static inline bool nbp_state_should_learn(const struct net_bridge_port *p)
631 return p->state == BR_STATE_LEARNING || p->state == BR_STATE_FORWARDING;
634 static inline bool br_vlan_valid_id(u16 vid, struct netlink_ext_ack *extack)
636 bool ret = vid > 0 && vid < VLAN_VID_MASK;
639 NL_SET_ERR_MSG_MOD(extack, "Vlan id is invalid");
644 static inline bool br_vlan_valid_range(const struct bridge_vlan_info *cur,
645 const struct bridge_vlan_info *last,
646 struct netlink_ext_ack *extack)
648 /* pvid flag is not allowed in ranges */
649 if (cur->flags & BRIDGE_VLAN_INFO_PVID) {
650 NL_SET_ERR_MSG_MOD(extack, "Pvid isn't allowed in a range");
654 /* when cur is the range end, check if:
655 * - it has range start flag
656 * - range ids are invalid (end is equal to or before start)
659 if (cur->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
660 NL_SET_ERR_MSG_MOD(extack, "Found a new vlan range start while processing one");
662 } else if (!(cur->flags & BRIDGE_VLAN_INFO_RANGE_END)) {
663 NL_SET_ERR_MSG_MOD(extack, "Vlan range end flag is missing");
665 } else if (cur->vid <= last->vid) {
666 NL_SET_ERR_MSG_MOD(extack, "End vlan id is less than or equal to start vlan id");
671 /* check for required range flags */
672 if (!(cur->flags & (BRIDGE_VLAN_INFO_RANGE_BEGIN |
673 BRIDGE_VLAN_INFO_RANGE_END))) {
674 NL_SET_ERR_MSG_MOD(extack, "Both vlan range flags are missing");
681 static inline u8 br_vlan_multicast_router(const struct net_bridge_vlan *v)
683 u8 mcast_router = MDB_RTR_TYPE_DISABLED;
685 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
686 if (!br_vlan_is_master(v))
687 mcast_router = v->port_mcast_ctx.multicast_router;
689 mcast_router = v->br_mcast_ctx.multicast_router;
695 static inline int br_afspec_cmd_to_rtm(int cmd)
707 static inline int br_opt_get(const struct net_bridge *br,
708 enum net_bridge_opts opt)
710 return test_bit(opt, &br->options);
713 int br_boolopt_toggle(struct net_bridge *br, enum br_boolopt_id opt, bool on,
714 struct netlink_ext_ack *extack);
715 int br_boolopt_get(const struct net_bridge *br, enum br_boolopt_id opt);
716 int br_boolopt_multi_toggle(struct net_bridge *br,
717 struct br_boolopt_multi *bm,
718 struct netlink_ext_ack *extack);
719 void br_boolopt_multi_get(const struct net_bridge *br,
720 struct br_boolopt_multi *bm);
721 void br_opt_toggle(struct net_bridge *br, enum net_bridge_opts opt, bool on);
724 void br_dev_setup(struct net_device *dev);
725 void br_dev_delete(struct net_device *dev, struct list_head *list);
726 netdev_tx_t br_dev_xmit(struct sk_buff *skb, struct net_device *dev);
727 #ifdef CONFIG_NET_POLL_CONTROLLER
728 static inline void br_netpoll_send_skb(const struct net_bridge_port *p,
731 netpoll_send_skb(p->np, skb);
734 int br_netpoll_enable(struct net_bridge_port *p);
735 void br_netpoll_disable(struct net_bridge_port *p);
737 static inline void br_netpoll_send_skb(const struct net_bridge_port *p,
742 static inline int br_netpoll_enable(struct net_bridge_port *p)
747 static inline void br_netpoll_disable(struct net_bridge_port *p)
753 int br_fdb_init(void);
754 void br_fdb_fini(void);
755 int br_fdb_hash_init(struct net_bridge *br);
756 void br_fdb_hash_fini(struct net_bridge *br);
757 void br_fdb_flush(struct net_bridge *br);
758 void br_fdb_find_delete_local(struct net_bridge *br,
759 const struct net_bridge_port *p,
760 const unsigned char *addr, u16 vid);
761 void br_fdb_changeaddr(struct net_bridge_port *p, const unsigned char *newaddr);
762 void br_fdb_change_mac_address(struct net_bridge *br, const u8 *newaddr);
763 void br_fdb_cleanup(struct work_struct *work);
764 void br_fdb_delete_by_port(struct net_bridge *br,
765 const struct net_bridge_port *p, u16 vid, int do_all);
766 struct net_bridge_fdb_entry *br_fdb_find_rcu(struct net_bridge *br,
767 const unsigned char *addr,
769 int br_fdb_test_addr(struct net_device *dev, unsigned char *addr);
770 int br_fdb_fillbuf(struct net_bridge *br, void *buf, unsigned long count,
772 int br_fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
773 const unsigned char *addr, u16 vid);
774 void br_fdb_update(struct net_bridge *br, struct net_bridge_port *source,
775 const unsigned char *addr, u16 vid, unsigned long flags);
777 int br_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
778 struct net_device *dev, const unsigned char *addr, u16 vid);
779 int br_fdb_add(struct ndmsg *nlh, struct nlattr *tb[], struct net_device *dev,
780 const unsigned char *addr, u16 vid, u16 nlh_flags,
781 struct netlink_ext_ack *extack);
782 int br_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
783 struct net_device *dev, struct net_device *fdev, int *idx);
784 int br_fdb_get(struct sk_buff *skb, struct nlattr *tb[], struct net_device *dev,
785 const unsigned char *addr, u16 vid, u32 portid, u32 seq,
786 struct netlink_ext_ack *extack);
787 int br_fdb_sync_static(struct net_bridge *br, struct net_bridge_port *p);
788 void br_fdb_unsync_static(struct net_bridge *br, struct net_bridge_port *p);
789 int br_fdb_external_learn_add(struct net_bridge *br, struct net_bridge_port *p,
790 const unsigned char *addr, u16 vid,
792 int br_fdb_external_learn_del(struct net_bridge *br, struct net_bridge_port *p,
793 const unsigned char *addr, u16 vid,
795 void br_fdb_offloaded_set(struct net_bridge *br, struct net_bridge_port *p,
796 const unsigned char *addr, u16 vid, bool offloaded);
797 int br_fdb_replay(const struct net_device *br_dev, const void *ctx, bool adding,
798 struct notifier_block *nb);
806 int br_dev_queue_push_xmit(struct net *net, struct sock *sk, struct sk_buff *skb);
807 void br_forward(const struct net_bridge_port *to, struct sk_buff *skb,
808 bool local_rcv, bool local_orig);
809 int br_forward_finish(struct net *net, struct sock *sk, struct sk_buff *skb);
810 void br_flood(struct net_bridge *br, struct sk_buff *skb,
811 enum br_pkt_type pkt_type, bool local_rcv, bool local_orig);
813 /* return true if both source port and dest port are isolated */
814 static inline bool br_skb_isolated(const struct net_bridge_port *to,
815 const struct sk_buff *skb)
817 return BR_INPUT_SKB_CB(skb)->src_port_isolated &&
818 (to->flags & BR_ISOLATED);
822 void br_port_carrier_check(struct net_bridge_port *p, bool *notified);
823 int br_add_bridge(struct net *net, const char *name);
824 int br_del_bridge(struct net *net, const char *name);
825 int br_add_if(struct net_bridge *br, struct net_device *dev,
826 struct netlink_ext_ack *extack);
827 int br_del_if(struct net_bridge *br, struct net_device *dev);
828 void br_mtu_auto_adjust(struct net_bridge *br);
829 netdev_features_t br_features_recompute(struct net_bridge *br,
830 netdev_features_t features);
831 void br_port_flags_change(struct net_bridge_port *port, unsigned long mask);
832 void br_manage_promisc(struct net_bridge *br);
833 int nbp_backup_change(struct net_bridge_port *p, struct net_device *backup_dev);
836 int br_handle_frame_finish(struct net *net, struct sock *sk, struct sk_buff *skb);
837 rx_handler_func_t *br_get_rx_handler(const struct net_device *dev);
839 struct br_frame_type {
841 int (*frame_handler)(struct net_bridge_port *port,
842 struct sk_buff *skb);
843 struct hlist_node list;
846 void br_add_frame(struct net_bridge *br, struct br_frame_type *ft);
847 void br_del_frame(struct net_bridge *br, struct br_frame_type *ft);
849 static inline bool br_rx_handler_check_rcu(const struct net_device *dev)
851 return rcu_dereference(dev->rx_handler) == br_get_rx_handler(dev);
854 static inline bool br_rx_handler_check_rtnl(const struct net_device *dev)
856 return rcu_dereference_rtnl(dev->rx_handler) == br_get_rx_handler(dev);
859 static inline struct net_bridge_port *br_port_get_check_rcu(const struct net_device *dev)
861 return br_rx_handler_check_rcu(dev) ? br_port_get_rcu(dev) : NULL;
864 static inline struct net_bridge_port *
865 br_port_get_check_rtnl(const struct net_device *dev)
867 return br_rx_handler_check_rtnl(dev) ? br_port_get_rtnl_rcu(dev) : NULL;
871 int br_dev_siocdevprivate(struct net_device *dev, struct ifreq *rq,
872 void __user *data, int cmd);
873 int br_ioctl_stub(struct net *net, struct net_bridge *br, unsigned int cmd,
874 struct ifreq *ifr, void __user *uarg);
877 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
878 int br_multicast_rcv(struct net_bridge_mcast **brmctx,
879 struct net_bridge_mcast_port **pmctx,
880 struct net_bridge_vlan *vlan,
881 struct sk_buff *skb, u16 vid);
882 struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge_mcast *brmctx,
883 struct sk_buff *skb, u16 vid);
884 int br_multicast_add_port(struct net_bridge_port *port);
885 void br_multicast_del_port(struct net_bridge_port *port);
886 void br_multicast_enable_port(struct net_bridge_port *port);
887 void br_multicast_disable_port(struct net_bridge_port *port);
888 void br_multicast_init(struct net_bridge *br);
889 void br_multicast_join_snoopers(struct net_bridge *br);
890 void br_multicast_leave_snoopers(struct net_bridge *br);
891 void br_multicast_open(struct net_bridge *br);
892 void br_multicast_stop(struct net_bridge *br);
893 void br_multicast_dev_del(struct net_bridge *br);
894 void br_multicast_flood(struct net_bridge_mdb_entry *mdst, struct sk_buff *skb,
895 struct net_bridge_mcast *brmctx,
896 bool local_rcv, bool local_orig);
897 int br_multicast_set_router(struct net_bridge_mcast *brmctx, unsigned long val);
898 int br_multicast_set_port_router(struct net_bridge_mcast_port *pmctx,
900 int br_multicast_set_vlan_router(struct net_bridge_vlan *v, u8 mcast_router);
901 int br_multicast_toggle(struct net_bridge *br, unsigned long val,
902 struct netlink_ext_ack *extack);
903 int br_multicast_set_querier(struct net_bridge_mcast *brmctx, unsigned long val);
904 int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val);
905 int br_multicast_set_igmp_version(struct net_bridge_mcast *brmctx,
907 #if IS_ENABLED(CONFIG_IPV6)
908 int br_multicast_set_mld_version(struct net_bridge_mcast *brmctx,
911 struct net_bridge_mdb_entry *
912 br_mdb_ip_get(struct net_bridge *br, struct br_ip *dst);
913 struct net_bridge_mdb_entry *
914 br_multicast_new_group(struct net_bridge *br, struct br_ip *group);
915 struct net_bridge_port_group *
916 br_multicast_new_port_group(struct net_bridge_port *port, struct br_ip *group,
917 struct net_bridge_port_group __rcu *next,
918 unsigned char flags, const unsigned char *src,
919 u8 filter_mode, u8 rt_protocol);
920 int br_mdb_hash_init(struct net_bridge *br);
921 void br_mdb_hash_fini(struct net_bridge *br);
922 void br_mdb_notify(struct net_device *dev, struct net_bridge_mdb_entry *mp,
923 struct net_bridge_port_group *pg, int type);
924 void br_rtr_notify(struct net_device *dev, struct net_bridge_mcast_port *pmctx,
926 void br_multicast_del_pg(struct net_bridge_mdb_entry *mp,
927 struct net_bridge_port_group *pg,
928 struct net_bridge_port_group __rcu **pp);
929 void br_multicast_count(struct net_bridge *br,
930 const struct net_bridge_port *p,
931 const struct sk_buff *skb, u8 type, u8 dir);
932 int br_multicast_init_stats(struct net_bridge *br);
933 void br_multicast_uninit_stats(struct net_bridge *br);
934 void br_multicast_get_stats(const struct net_bridge *br,
935 const struct net_bridge_port *p,
936 struct br_mcast_stats *dest);
937 void br_mdb_init(void);
938 void br_mdb_uninit(void);
939 void br_multicast_host_join(const struct net_bridge_mcast *brmctx,
940 struct net_bridge_mdb_entry *mp, bool notify);
941 void br_multicast_host_leave(struct net_bridge_mdb_entry *mp, bool notify);
942 void br_multicast_star_g_handle_mode(struct net_bridge_port_group *pg,
944 void br_multicast_sg_add_exclude_ports(struct net_bridge_mdb_entry *star_mp,
945 struct net_bridge_port_group *sg);
946 struct net_bridge_group_src *
947 br_multicast_find_group_src(struct net_bridge_port_group *pg, struct br_ip *ip);
948 void br_multicast_del_group_src(struct net_bridge_group_src *src,
950 void br_multicast_ctx_init(struct net_bridge *br,
951 struct net_bridge_vlan *vlan,
952 struct net_bridge_mcast *brmctx);
953 void br_multicast_ctx_deinit(struct net_bridge_mcast *brmctx);
954 void br_multicast_port_ctx_init(struct net_bridge_port *port,
955 struct net_bridge_vlan *vlan,
956 struct net_bridge_mcast_port *pmctx);
957 void br_multicast_port_ctx_deinit(struct net_bridge_mcast_port *pmctx);
958 void br_multicast_toggle_one_vlan(struct net_bridge_vlan *vlan, bool on);
959 int br_multicast_toggle_vlan_snooping(struct net_bridge *br, bool on,
960 struct netlink_ext_ack *extack);
961 bool br_multicast_toggle_global_vlan(struct net_bridge_vlan *vlan, bool on);
963 int br_mdb_replay(struct net_device *br_dev, struct net_device *dev,
964 const void *ctx, bool adding, struct notifier_block *nb,
965 struct netlink_ext_ack *extack);
966 int br_rports_fill_info(struct sk_buff *skb,
967 const struct net_bridge_mcast *brmctx);
968 int br_multicast_dump_querier_state(struct sk_buff *skb,
969 const struct net_bridge_mcast *brmctx,
971 size_t br_multicast_querier_state_size(void);
972 size_t br_rports_size(const struct net_bridge_mcast *brmctx);
973 void br_multicast_set_query_intvl(struct net_bridge_mcast *brmctx,
975 void br_multicast_set_startup_query_intvl(struct net_bridge_mcast *brmctx,
978 static inline bool br_group_is_l2(const struct br_ip *group)
980 return group->proto == 0;
983 #define mlock_dereference(X, br) \
984 rcu_dereference_protected(X, lockdep_is_held(&br->multicast_lock))
986 static inline struct hlist_node *
987 br_multicast_get_first_rport_node(struct net_bridge_mcast *brmctx,
990 #if IS_ENABLED(CONFIG_IPV6)
991 if (skb->protocol == htons(ETH_P_IPV6))
992 return rcu_dereference(hlist_first_rcu(&brmctx->ip6_mc_router_list));
994 return rcu_dereference(hlist_first_rcu(&brmctx->ip4_mc_router_list));
997 static inline struct net_bridge_port *
998 br_multicast_rport_from_node_skb(struct hlist_node *rp, struct sk_buff *skb)
1000 struct net_bridge_mcast_port *mctx;
1002 #if IS_ENABLED(CONFIG_IPV6)
1003 if (skb->protocol == htons(ETH_P_IPV6))
1004 mctx = hlist_entry_safe(rp, struct net_bridge_mcast_port,
1008 mctx = hlist_entry_safe(rp, struct net_bridge_mcast_port,
1017 static inline bool br_ip4_multicast_is_router(struct net_bridge_mcast *brmctx)
1019 return timer_pending(&brmctx->ip4_mc_router_timer);
1022 static inline bool br_ip6_multicast_is_router(struct net_bridge_mcast *brmctx)
1024 #if IS_ENABLED(CONFIG_IPV6)
1025 return timer_pending(&brmctx->ip6_mc_router_timer);
1032 br_multicast_is_router(struct net_bridge_mcast *brmctx, struct sk_buff *skb)
1034 switch (brmctx->multicast_router) {
1035 case MDB_RTR_TYPE_PERM:
1037 case MDB_RTR_TYPE_TEMP_QUERY:
1039 if (skb->protocol == htons(ETH_P_IP))
1040 return br_ip4_multicast_is_router(brmctx);
1041 else if (skb->protocol == htons(ETH_P_IPV6))
1042 return br_ip6_multicast_is_router(brmctx);
1044 return br_ip4_multicast_is_router(brmctx) ||
1045 br_ip6_multicast_is_router(brmctx);
1054 __br_multicast_querier_exists(struct net_bridge_mcast *brmctx,
1055 struct bridge_mcast_other_query *querier,
1058 bool own_querier_enabled;
1060 if (brmctx->multicast_querier) {
1061 if (is_ipv6 && !br_opt_get(brmctx->br, BROPT_HAS_IPV6_ADDR))
1062 own_querier_enabled = false;
1064 own_querier_enabled = true;
1066 own_querier_enabled = false;
1069 return time_is_before_jiffies(querier->delay_time) &&
1070 (own_querier_enabled || timer_pending(&querier->timer));
1073 static inline bool br_multicast_querier_exists(struct net_bridge_mcast *brmctx,
1075 const struct net_bridge_mdb_entry *mdb)
1077 switch (eth->h_proto) {
1078 case (htons(ETH_P_IP)):
1079 return __br_multicast_querier_exists(brmctx,
1080 &brmctx->ip4_other_query, false);
1081 #if IS_ENABLED(CONFIG_IPV6)
1082 case (htons(ETH_P_IPV6)):
1083 return __br_multicast_querier_exists(brmctx,
1084 &brmctx->ip6_other_query, true);
1087 return !!mdb && br_group_is_l2(&mdb->addr);
1091 static inline bool br_multicast_is_star_g(const struct br_ip *ip)
1093 switch (ip->proto) {
1094 case htons(ETH_P_IP):
1095 return ipv4_is_zeronet(ip->src.ip4);
1096 #if IS_ENABLED(CONFIG_IPV6)
1097 case htons(ETH_P_IPV6):
1098 return ipv6_addr_any(&ip->src.ip6);
1106 br_multicast_should_handle_mode(const struct net_bridge_mcast *brmctx,
1110 case htons(ETH_P_IP):
1111 return !!(brmctx->multicast_igmp_version == 3);
1112 #if IS_ENABLED(CONFIG_IPV6)
1113 case htons(ETH_P_IPV6):
1114 return !!(brmctx->multicast_mld_version == 2);
1121 static inline int br_multicast_igmp_type(const struct sk_buff *skb)
1123 return BR_INPUT_SKB_CB(skb)->igmp;
1126 static inline unsigned long br_multicast_lmqt(const struct net_bridge_mcast *brmctx)
1128 return brmctx->multicast_last_member_interval *
1129 brmctx->multicast_last_member_count;
1132 static inline unsigned long br_multicast_gmi(const struct net_bridge_mcast *brmctx)
1134 return brmctx->multicast_membership_interval;
1138 br_multicast_ctx_is_vlan(const struct net_bridge_mcast *brmctx)
1140 return !!brmctx->vlan;
1144 br_multicast_port_ctx_is_vlan(const struct net_bridge_mcast_port *pmctx)
1146 return !!pmctx->vlan;
1149 static inline struct net_bridge_mcast *
1150 br_multicast_port_ctx_get_global(const struct net_bridge_mcast_port *pmctx)
1152 if (!br_multicast_port_ctx_is_vlan(pmctx))
1153 return &pmctx->port->br->multicast_ctx;
1155 return &pmctx->vlan->brvlan->br_mcast_ctx;
1159 br_multicast_ctx_vlan_global_disabled(const struct net_bridge_mcast *brmctx)
1161 return br_multicast_ctx_is_vlan(brmctx) &&
1162 (!br_opt_get(brmctx->br, BROPT_MCAST_VLAN_SNOOPING_ENABLED) ||
1163 !(brmctx->vlan->priv_flags & BR_VLFLAG_GLOBAL_MCAST_ENABLED));
1167 br_multicast_ctx_vlan_disabled(const struct net_bridge_mcast *brmctx)
1169 return br_multicast_ctx_is_vlan(brmctx) &&
1170 !(brmctx->vlan->priv_flags & BR_VLFLAG_MCAST_ENABLED);
1174 br_multicast_port_ctx_vlan_disabled(const struct net_bridge_mcast_port *pmctx)
1176 return br_multicast_port_ctx_is_vlan(pmctx) &&
1177 !(pmctx->vlan->priv_flags & BR_VLFLAG_MCAST_ENABLED);
1181 br_multicast_port_ctx_state_disabled(const struct net_bridge_mcast_port *pmctx)
1183 return pmctx->port->state == BR_STATE_DISABLED ||
1184 (br_multicast_port_ctx_is_vlan(pmctx) &&
1185 (br_multicast_port_ctx_vlan_disabled(pmctx) ||
1186 pmctx->vlan->state == BR_STATE_DISABLED));
1190 br_multicast_port_ctx_state_stopped(const struct net_bridge_mcast_port *pmctx)
1192 return br_multicast_port_ctx_state_disabled(pmctx) ||
1193 pmctx->port->state == BR_STATE_BLOCKING ||
1194 (br_multicast_port_ctx_is_vlan(pmctx) &&
1195 pmctx->vlan->state == BR_STATE_BLOCKING);
1199 br_rports_have_mc_router(const struct net_bridge_mcast *brmctx)
1201 #if IS_ENABLED(CONFIG_IPV6)
1202 return !hlist_empty(&brmctx->ip4_mc_router_list) ||
1203 !hlist_empty(&brmctx->ip6_mc_router_list);
1205 return !hlist_empty(&brmctx->ip4_mc_router_list);
1210 br_multicast_ctx_options_equal(const struct net_bridge_mcast *brmctx1,
1211 const struct net_bridge_mcast *brmctx2)
1213 return brmctx1->multicast_igmp_version ==
1214 brmctx2->multicast_igmp_version &&
1215 brmctx1->multicast_last_member_count ==
1216 brmctx2->multicast_last_member_count &&
1217 brmctx1->multicast_startup_query_count ==
1218 brmctx2->multicast_startup_query_count &&
1219 brmctx1->multicast_last_member_interval ==
1220 brmctx2->multicast_last_member_interval &&
1221 brmctx1->multicast_membership_interval ==
1222 brmctx2->multicast_membership_interval &&
1223 brmctx1->multicast_querier_interval ==
1224 brmctx2->multicast_querier_interval &&
1225 brmctx1->multicast_query_interval ==
1226 brmctx2->multicast_query_interval &&
1227 brmctx1->multicast_query_response_interval ==
1228 brmctx2->multicast_query_response_interval &&
1229 brmctx1->multicast_startup_query_interval ==
1230 brmctx2->multicast_startup_query_interval &&
1231 brmctx1->multicast_querier == brmctx2->multicast_querier &&
1232 brmctx1->multicast_router == brmctx2->multicast_router &&
1233 !br_rports_have_mc_router(brmctx1) &&
1234 !br_rports_have_mc_router(brmctx2) &&
1235 #if IS_ENABLED(CONFIG_IPV6)
1236 brmctx1->multicast_mld_version ==
1237 brmctx2->multicast_mld_version &&
1243 br_multicast_ctx_matches_vlan_snooping(const struct net_bridge_mcast *brmctx)
1245 bool vlan_snooping_enabled;
1247 vlan_snooping_enabled = !!br_opt_get(brmctx->br,
1248 BROPT_MCAST_VLAN_SNOOPING_ENABLED);
1250 return !!(vlan_snooping_enabled == br_multicast_ctx_is_vlan(brmctx));
1253 static inline int br_multicast_rcv(struct net_bridge_mcast **brmctx,
1254 struct net_bridge_mcast_port **pmctx,
1255 struct net_bridge_vlan *vlan,
1256 struct sk_buff *skb,
1262 static inline struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge_mcast *brmctx,
1263 struct sk_buff *skb, u16 vid)
1268 static inline int br_multicast_add_port(struct net_bridge_port *port)
1273 static inline void br_multicast_del_port(struct net_bridge_port *port)
1277 static inline void br_multicast_enable_port(struct net_bridge_port *port)
1281 static inline void br_multicast_disable_port(struct net_bridge_port *port)
1285 static inline void br_multicast_init(struct net_bridge *br)
1289 static inline void br_multicast_join_snoopers(struct net_bridge *br)
1293 static inline void br_multicast_leave_snoopers(struct net_bridge *br)
1297 static inline void br_multicast_open(struct net_bridge *br)
1301 static inline void br_multicast_stop(struct net_bridge *br)
1305 static inline void br_multicast_dev_del(struct net_bridge *br)
1309 static inline void br_multicast_flood(struct net_bridge_mdb_entry *mdst,
1310 struct sk_buff *skb,
1311 struct net_bridge_mcast *brmctx,
1312 bool local_rcv, bool local_orig)
1316 static inline bool br_multicast_is_router(struct net_bridge_mcast *brmctx,
1317 struct sk_buff *skb)
1322 static inline bool br_multicast_querier_exists(struct net_bridge_mcast *brmctx,
1324 const struct net_bridge_mdb_entry *mdb)
1329 static inline void br_mdb_init(void)
1333 static inline void br_mdb_uninit(void)
1337 static inline int br_mdb_hash_init(struct net_bridge *br)
1342 static inline void br_mdb_hash_fini(struct net_bridge *br)
1346 static inline void br_multicast_count(struct net_bridge *br,
1347 const struct net_bridge_port *p,
1348 const struct sk_buff *skb,
1353 static inline int br_multicast_init_stats(struct net_bridge *br)
1358 static inline void br_multicast_uninit_stats(struct net_bridge *br)
1362 static inline int br_multicast_igmp_type(const struct sk_buff *skb)
1367 static inline void br_multicast_ctx_init(struct net_bridge *br,
1368 struct net_bridge_vlan *vlan,
1369 struct net_bridge_mcast *brmctx)
1373 static inline void br_multicast_ctx_deinit(struct net_bridge_mcast *brmctx)
1377 static inline void br_multicast_port_ctx_init(struct net_bridge_port *port,
1378 struct net_bridge_vlan *vlan,
1379 struct net_bridge_mcast_port *pmctx)
1383 static inline void br_multicast_port_ctx_deinit(struct net_bridge_mcast_port *pmctx)
1387 static inline void br_multicast_toggle_one_vlan(struct net_bridge_vlan *vlan,
1392 static inline int br_multicast_toggle_vlan_snooping(struct net_bridge *br,
1394 struct netlink_ext_ack *extack)
1399 static inline bool br_multicast_toggle_global_vlan(struct net_bridge_vlan *vlan,
1405 static inline int br_mdb_replay(struct net_device *br_dev,
1406 struct net_device *dev, const void *ctx,
1407 bool adding, struct notifier_block *nb,
1408 struct netlink_ext_ack *extack)
1414 br_multicast_ctx_options_equal(const struct net_bridge_mcast *brmctx1,
1415 const struct net_bridge_mcast *brmctx2)
1422 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
1423 bool br_allowed_ingress(const struct net_bridge *br,
1424 struct net_bridge_vlan_group *vg, struct sk_buff *skb,
1425 u16 *vid, u8 *state,
1426 struct net_bridge_vlan **vlan);
1427 bool br_allowed_egress(struct net_bridge_vlan_group *vg,
1428 const struct sk_buff *skb);
1429 bool br_should_learn(struct net_bridge_port *p, struct sk_buff *skb, u16 *vid);
1430 struct sk_buff *br_handle_vlan(struct net_bridge *br,
1431 const struct net_bridge_port *port,
1432 struct net_bridge_vlan_group *vg,
1433 struct sk_buff *skb);
1434 int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags,
1435 bool *changed, struct netlink_ext_ack *extack);
1436 int br_vlan_delete(struct net_bridge *br, u16 vid);
1437 void br_vlan_flush(struct net_bridge *br);
1438 struct net_bridge_vlan *br_vlan_find(struct net_bridge_vlan_group *vg, u16 vid);
1439 void br_recalculate_fwd_mask(struct net_bridge *br);
1440 int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val,
1441 struct netlink_ext_ack *extack);
1442 int __br_vlan_set_proto(struct net_bridge *br, __be16 proto,
1443 struct netlink_ext_ack *extack);
1444 int br_vlan_set_proto(struct net_bridge *br, unsigned long val,
1445 struct netlink_ext_ack *extack);
1446 int br_vlan_set_stats(struct net_bridge *br, unsigned long val);
1447 int br_vlan_set_stats_per_port(struct net_bridge *br, unsigned long val);
1448 int br_vlan_init(struct net_bridge *br);
1449 int br_vlan_set_default_pvid(struct net_bridge *br, unsigned long val,
1450 struct netlink_ext_ack *extack);
1451 int __br_vlan_set_default_pvid(struct net_bridge *br, u16 pvid,
1452 struct netlink_ext_ack *extack);
1453 int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags,
1454 bool *changed, struct netlink_ext_ack *extack);
1455 int nbp_vlan_delete(struct net_bridge_port *port, u16 vid);
1456 void nbp_vlan_flush(struct net_bridge_port *port);
1457 int nbp_vlan_init(struct net_bridge_port *port, struct netlink_ext_ack *extack);
1458 int nbp_get_num_vlan_infos(struct net_bridge_port *p, u32 filter_mask);
1459 void br_vlan_get_stats(const struct net_bridge_vlan *v,
1460 struct pcpu_sw_netstats *stats);
1461 void br_vlan_port_event(struct net_bridge_port *p, unsigned long event);
1462 int br_vlan_bridge_event(struct net_device *dev, unsigned long event,
1464 void br_vlan_rtnl_init(void);
1465 void br_vlan_rtnl_uninit(void);
1466 void br_vlan_notify(const struct net_bridge *br,
1467 const struct net_bridge_port *p,
1468 u16 vid, u16 vid_range,
1470 int br_vlan_replay(struct net_device *br_dev, struct net_device *dev,
1471 const void *ctx, bool adding, struct notifier_block *nb,
1472 struct netlink_ext_ack *extack);
1473 bool br_vlan_can_enter_range(const struct net_bridge_vlan *v_curr,
1474 const struct net_bridge_vlan *range_end);
1476 void br_vlan_fill_forward_path_pvid(struct net_bridge *br,
1477 struct net_device_path_ctx *ctx,
1478 struct net_device_path *path);
1479 int br_vlan_fill_forward_path_mode(struct net_bridge *br,
1480 struct net_bridge_port *dst,
1481 struct net_device_path *path);
1483 static inline struct net_bridge_vlan_group *br_vlan_group(
1484 const struct net_bridge *br)
1486 return rtnl_dereference(br->vlgrp);
1489 static inline struct net_bridge_vlan_group *nbp_vlan_group(
1490 const struct net_bridge_port *p)
1492 return rtnl_dereference(p->vlgrp);
1495 static inline struct net_bridge_vlan_group *br_vlan_group_rcu(
1496 const struct net_bridge *br)
1498 return rcu_dereference(br->vlgrp);
1501 static inline struct net_bridge_vlan_group *nbp_vlan_group_rcu(
1502 const struct net_bridge_port *p)
1504 return rcu_dereference(p->vlgrp);
1507 /* Since bridge now depends on 8021Q module, but the time bridge sees the
1508 * skb, the vlan tag will always be present if the frame was tagged.
1510 static inline int br_vlan_get_tag(const struct sk_buff *skb, u16 *vid)
1514 if (skb_vlan_tag_present(skb)) {
1515 *vid = skb_vlan_tag_get_id(skb);
1524 static inline u16 br_get_pvid(const struct net_bridge_vlan_group *vg)
1533 static inline u16 br_vlan_flags(const struct net_bridge_vlan *v, u16 pvid)
1535 return v->vid == pvid ? v->flags | BRIDGE_VLAN_INFO_PVID : v->flags;
1538 static inline bool br_allowed_ingress(const struct net_bridge *br,
1539 struct net_bridge_vlan_group *vg,
1540 struct sk_buff *skb,
1541 u16 *vid, u8 *state,
1542 struct net_bridge_vlan **vlan)
1549 static inline bool br_allowed_egress(struct net_bridge_vlan_group *vg,
1550 const struct sk_buff *skb)
1555 static inline bool br_should_learn(struct net_bridge_port *p,
1556 struct sk_buff *skb, u16 *vid)
1561 static inline struct sk_buff *br_handle_vlan(struct net_bridge *br,
1562 const struct net_bridge_port *port,
1563 struct net_bridge_vlan_group *vg,
1564 struct sk_buff *skb)
1569 static inline int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags,
1570 bool *changed, struct netlink_ext_ack *extack)
1576 static inline int br_vlan_delete(struct net_bridge *br, u16 vid)
1581 static inline void br_vlan_flush(struct net_bridge *br)
1585 static inline void br_recalculate_fwd_mask(struct net_bridge *br)
1589 static inline int br_vlan_init(struct net_bridge *br)
1594 static inline int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags,
1595 bool *changed, struct netlink_ext_ack *extack)
1601 static inline int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
1606 static inline void nbp_vlan_flush(struct net_bridge_port *port)
1610 static inline struct net_bridge_vlan *br_vlan_find(struct net_bridge_vlan_group *vg,
1616 static inline int nbp_vlan_init(struct net_bridge_port *port,
1617 struct netlink_ext_ack *extack)
1622 static inline u16 br_vlan_get_tag(const struct sk_buff *skb, u16 *tag)
1627 static inline u16 br_get_pvid(const struct net_bridge_vlan_group *vg)
1632 static inline int br_vlan_filter_toggle(struct net_bridge *br,
1634 struct netlink_ext_ack *extack)
1639 static inline int nbp_get_num_vlan_infos(struct net_bridge_port *p,
1645 static inline void br_vlan_fill_forward_path_pvid(struct net_bridge *br,
1646 struct net_device_path_ctx *ctx,
1647 struct net_device_path *path)
1651 static inline int br_vlan_fill_forward_path_mode(struct net_bridge *br,
1652 struct net_bridge_port *dst,
1653 struct net_device_path *path)
1658 static inline struct net_bridge_vlan_group *br_vlan_group(
1659 const struct net_bridge *br)
1664 static inline struct net_bridge_vlan_group *nbp_vlan_group(
1665 const struct net_bridge_port *p)
1670 static inline struct net_bridge_vlan_group *br_vlan_group_rcu(
1671 const struct net_bridge *br)
1676 static inline struct net_bridge_vlan_group *nbp_vlan_group_rcu(
1677 const struct net_bridge_port *p)
1682 static inline void br_vlan_get_stats(const struct net_bridge_vlan *v,
1683 struct pcpu_sw_netstats *stats)
1687 static inline void br_vlan_port_event(struct net_bridge_port *p,
1688 unsigned long event)
1692 static inline int br_vlan_bridge_event(struct net_device *dev,
1693 unsigned long event, void *ptr)
1698 static inline void br_vlan_rtnl_init(void)
1702 static inline void br_vlan_rtnl_uninit(void)
1706 static inline void br_vlan_notify(const struct net_bridge *br,
1707 const struct net_bridge_port *p,
1708 u16 vid, u16 vid_range,
1713 static inline bool br_vlan_can_enter_range(const struct net_bridge_vlan *v_curr,
1714 const struct net_bridge_vlan *range_end)
1719 static inline int br_vlan_replay(struct net_device *br_dev,
1720 struct net_device *dev, const void *ctx,
1721 bool adding, struct notifier_block *nb,
1722 struct netlink_ext_ack *extack)
1728 /* br_vlan_options.c */
1729 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
1730 bool br_vlan_opts_eq_range(const struct net_bridge_vlan *v_curr,
1731 const struct net_bridge_vlan *range_end);
1732 bool br_vlan_opts_fill(struct sk_buff *skb, const struct net_bridge_vlan *v);
1733 size_t br_vlan_opts_nl_size(void);
1734 int br_vlan_process_options(const struct net_bridge *br,
1735 const struct net_bridge_port *p,
1736 struct net_bridge_vlan *range_start,
1737 struct net_bridge_vlan *range_end,
1739 struct netlink_ext_ack *extack);
1740 int br_vlan_rtm_process_global_options(struct net_device *dev,
1741 const struct nlattr *attr,
1743 struct netlink_ext_ack *extack);
1744 bool br_vlan_global_opts_can_enter_range(const struct net_bridge_vlan *v_curr,
1745 const struct net_bridge_vlan *r_end);
1746 bool br_vlan_global_opts_fill(struct sk_buff *skb, u16 vid, u16 vid_range,
1747 const struct net_bridge_vlan *v_opts);
1749 /* vlan state manipulation helpers using *_ONCE to annotate lock-free access */
1750 static inline u8 br_vlan_get_state(const struct net_bridge_vlan *v)
1752 return READ_ONCE(v->state);
1755 static inline void br_vlan_set_state(struct net_bridge_vlan *v, u8 state)
1757 WRITE_ONCE(v->state, state);
1760 static inline u8 br_vlan_get_pvid_state(const struct net_bridge_vlan_group *vg)
1762 return READ_ONCE(vg->pvid_state);
1765 static inline void br_vlan_set_pvid_state(struct net_bridge_vlan_group *vg,
1768 WRITE_ONCE(vg->pvid_state, state);
1771 /* learn_allow is true at ingress and false at egress */
1772 static inline bool br_vlan_state_allowed(u8 state, bool learn_allow)
1775 case BR_STATE_LEARNING:
1777 case BR_STATE_FORWARDING:
1786 int (*br_dev_xmit_hook)(struct sk_buff *skb);
1788 extern const struct nf_br_ops __rcu *nf_br_ops;
1790 /* br_netfilter.c */
1791 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1792 int br_nf_core_init(void);
1793 void br_nf_core_fini(void);
1794 void br_netfilter_rtable_init(struct net_bridge *);
1796 static inline int br_nf_core_init(void) { return 0; }
1797 static inline void br_nf_core_fini(void) {}
1798 #define br_netfilter_rtable_init(x)
1802 void br_set_state(struct net_bridge_port *p, unsigned int state);
1803 struct net_bridge_port *br_get_port(struct net_bridge *br, u16 port_no);
1804 void br_init_port(struct net_bridge_port *p);
1805 void br_become_designated_port(struct net_bridge_port *p);
1807 void __br_set_forward_delay(struct net_bridge *br, unsigned long t);
1808 int br_set_forward_delay(struct net_bridge *br, unsigned long x);
1809 int br_set_hello_time(struct net_bridge *br, unsigned long x);
1810 int br_set_max_age(struct net_bridge *br, unsigned long x);
1811 int __set_ageing_time(struct net_device *dev, unsigned long t);
1812 int br_set_ageing_time(struct net_bridge *br, clock_t ageing_time);
1816 void br_stp_enable_bridge(struct net_bridge *br);
1817 void br_stp_disable_bridge(struct net_bridge *br);
1818 int br_stp_set_enabled(struct net_bridge *br, unsigned long val,
1819 struct netlink_ext_ack *extack);
1820 void br_stp_enable_port(struct net_bridge_port *p);
1821 void br_stp_disable_port(struct net_bridge_port *p);
1822 bool br_stp_recalculate_bridge_id(struct net_bridge *br);
1823 void br_stp_change_bridge_id(struct net_bridge *br, const unsigned char *a);
1824 void br_stp_set_bridge_priority(struct net_bridge *br, u16 newprio);
1825 int br_stp_set_port_priority(struct net_bridge_port *p, unsigned long newprio);
1826 int br_stp_set_path_cost(struct net_bridge_port *p, unsigned long path_cost);
1827 ssize_t br_show_bridge_id(char *buf, const struct bridge_id *id);
1831 void br_stp_rcv(const struct stp_proto *proto, struct sk_buff *skb,
1832 struct net_device *dev);
1834 /* br_stp_timer.c */
1835 void br_stp_timer_init(struct net_bridge *br);
1836 void br_stp_port_timer_init(struct net_bridge_port *p);
1837 unsigned long br_timer_value(const struct timer_list *timer);
1840 #if IS_ENABLED(CONFIG_ATM_LANE)
1841 extern int (*br_fdb_test_addr_hook)(struct net_device *dev, unsigned char *addr);
1845 #if IS_ENABLED(CONFIG_BRIDGE_MRP)
1846 int br_mrp_parse(struct net_bridge *br, struct net_bridge_port *p,
1847 struct nlattr *attr, int cmd, struct netlink_ext_ack *extack);
1848 bool br_mrp_enabled(struct net_bridge *br);
1849 void br_mrp_port_del(struct net_bridge *br, struct net_bridge_port *p);
1850 int br_mrp_fill_info(struct sk_buff *skb, struct net_bridge *br);
1852 static inline int br_mrp_parse(struct net_bridge *br, struct net_bridge_port *p,
1853 struct nlattr *attr, int cmd,
1854 struct netlink_ext_ack *extack)
1859 static inline bool br_mrp_enabled(struct net_bridge *br)
1864 static inline void br_mrp_port_del(struct net_bridge *br,
1865 struct net_bridge_port *p)
1869 static inline int br_mrp_fill_info(struct sk_buff *skb, struct net_bridge *br)
1877 #if IS_ENABLED(CONFIG_BRIDGE_CFM)
1878 int br_cfm_parse(struct net_bridge *br, struct net_bridge_port *p,
1879 struct nlattr *attr, int cmd, struct netlink_ext_ack *extack);
1880 bool br_cfm_created(struct net_bridge *br);
1881 void br_cfm_port_del(struct net_bridge *br, struct net_bridge_port *p);
1882 int br_cfm_config_fill_info(struct sk_buff *skb, struct net_bridge *br);
1883 int br_cfm_status_fill_info(struct sk_buff *skb,
1884 struct net_bridge *br,
1886 int br_cfm_mep_count(struct net_bridge *br, u32 *count);
1887 int br_cfm_peer_mep_count(struct net_bridge *br, u32 *count);
1889 static inline int br_cfm_parse(struct net_bridge *br, struct net_bridge_port *p,
1890 struct nlattr *attr, int cmd,
1891 struct netlink_ext_ack *extack)
1896 static inline bool br_cfm_created(struct net_bridge *br)
1901 static inline void br_cfm_port_del(struct net_bridge *br,
1902 struct net_bridge_port *p)
1906 static inline int br_cfm_config_fill_info(struct sk_buff *skb, struct net_bridge *br)
1911 static inline int br_cfm_status_fill_info(struct sk_buff *skb,
1912 struct net_bridge *br,
1918 static inline int br_cfm_mep_count(struct net_bridge *br, u32 *count)
1924 static inline int br_cfm_peer_mep_count(struct net_bridge *br, u32 *count)
1932 extern struct rtnl_link_ops br_link_ops;
1933 int br_netlink_init(void);
1934 void br_netlink_fini(void);
1935 void br_ifinfo_notify(int event, const struct net_bridge *br,
1936 const struct net_bridge_port *port);
1937 void br_info_notify(int event, const struct net_bridge *br,
1938 const struct net_bridge_port *port, u32 filter);
1939 int br_setlink(struct net_device *dev, struct nlmsghdr *nlmsg, u16 flags,
1940 struct netlink_ext_ack *extack);
1941 int br_dellink(struct net_device *dev, struct nlmsghdr *nlmsg, u16 flags);
1942 int br_getlink(struct sk_buff *skb, u32 pid, u32 seq, struct net_device *dev,
1943 u32 filter_mask, int nlflags);
1944 int br_process_vlan_info(struct net_bridge *br,
1945 struct net_bridge_port *p, int cmd,
1946 struct bridge_vlan_info *vinfo_curr,
1947 struct bridge_vlan_info **vinfo_last,
1949 struct netlink_ext_ack *extack);
1953 extern const struct sysfs_ops brport_sysfs_ops;
1954 int br_sysfs_addif(struct net_bridge_port *p);
1955 int br_sysfs_renameif(struct net_bridge_port *p);
1958 int br_sysfs_addbr(struct net_device *dev);
1959 void br_sysfs_delbr(struct net_device *dev);
1963 static inline int br_sysfs_addif(struct net_bridge_port *p) { return 0; }
1964 static inline int br_sysfs_renameif(struct net_bridge_port *p) { return 0; }
1965 static inline int br_sysfs_addbr(struct net_device *dev) { return 0; }
1966 static inline void br_sysfs_delbr(struct net_device *dev) { return; }
1967 #endif /* CONFIG_SYSFS */
1969 /* br_switchdev.c */
1970 #ifdef CONFIG_NET_SWITCHDEV
1971 int br_switchdev_port_offload(struct net_bridge_port *p,
1972 struct net_device *dev, const void *ctx,
1973 struct notifier_block *atomic_nb,
1974 struct notifier_block *blocking_nb,
1975 bool tx_fwd_offload,
1976 struct netlink_ext_ack *extack);
1978 void br_switchdev_port_unoffload(struct net_bridge_port *p, const void *ctx,
1979 struct notifier_block *atomic_nb,
1980 struct notifier_block *blocking_nb);
1982 bool br_switchdev_frame_uses_tx_fwd_offload(struct sk_buff *skb);
1984 void br_switchdev_frame_set_offload_fwd_mark(struct sk_buff *skb);
1986 void nbp_switchdev_frame_mark_tx_fwd_offload(const struct net_bridge_port *p,
1987 struct sk_buff *skb);
1988 void nbp_switchdev_frame_mark_tx_fwd_to_hwdom(const struct net_bridge_port *p,
1989 struct sk_buff *skb);
1990 void nbp_switchdev_frame_mark(const struct net_bridge_port *p,
1991 struct sk_buff *skb);
1992 bool nbp_switchdev_allowed_egress(const struct net_bridge_port *p,
1993 const struct sk_buff *skb);
1994 int br_switchdev_set_port_flag(struct net_bridge_port *p,
1995 unsigned long flags,
1997 struct netlink_ext_ack *extack);
1998 void br_switchdev_fdb_notify(struct net_bridge *br,
1999 const struct net_bridge_fdb_entry *fdb, int type);
2000 int br_switchdev_port_vlan_add(struct net_device *dev, u16 vid, u16 flags,
2001 struct netlink_ext_ack *extack);
2002 int br_switchdev_port_vlan_del(struct net_device *dev, u16 vid);
2003 void br_switchdev_init(struct net_bridge *br);
2005 static inline void br_switchdev_frame_unmark(struct sk_buff *skb)
2007 skb->offload_fwd_mark = 0;
2011 br_switchdev_port_offload(struct net_bridge_port *p,
2012 struct net_device *dev, const void *ctx,
2013 struct notifier_block *atomic_nb,
2014 struct notifier_block *blocking_nb,
2015 bool tx_fwd_offload,
2016 struct netlink_ext_ack *extack)
2022 br_switchdev_port_unoffload(struct net_bridge_port *p, const void *ctx,
2023 struct notifier_block *atomic_nb,
2024 struct notifier_block *blocking_nb)
2028 static inline bool br_switchdev_frame_uses_tx_fwd_offload(struct sk_buff *skb)
2033 static inline void br_switchdev_frame_set_offload_fwd_mark(struct sk_buff *skb)
2038 nbp_switchdev_frame_mark_tx_fwd_offload(const struct net_bridge_port *p,
2039 struct sk_buff *skb)
2044 nbp_switchdev_frame_mark_tx_fwd_to_hwdom(const struct net_bridge_port *p,
2045 struct sk_buff *skb)
2049 static inline void nbp_switchdev_frame_mark(const struct net_bridge_port *p,
2050 struct sk_buff *skb)
2054 static inline bool nbp_switchdev_allowed_egress(const struct net_bridge_port *p,
2055 const struct sk_buff *skb)
2060 static inline int br_switchdev_set_port_flag(struct net_bridge_port *p,
2061 unsigned long flags,
2063 struct netlink_ext_ack *extack)
2068 static inline int br_switchdev_port_vlan_add(struct net_device *dev,
2070 struct netlink_ext_ack *extack)
2075 static inline int br_switchdev_port_vlan_del(struct net_device *dev, u16 vid)
2081 br_switchdev_fdb_notify(struct net_bridge *br,
2082 const struct net_bridge_fdb_entry *fdb, int type)
2086 static inline void br_switchdev_frame_unmark(struct sk_buff *skb)
2090 static inline void br_switchdev_init(struct net_bridge *br)
2094 #endif /* CONFIG_NET_SWITCHDEV */
2096 /* br_arp_nd_proxy.c */
2097 void br_recalculate_neigh_suppress_enabled(struct net_bridge *br);
2098 void br_do_proxy_suppress_arp(struct sk_buff *skb, struct net_bridge *br,
2099 u16 vid, struct net_bridge_port *p);
2100 void br_do_suppress_nd(struct sk_buff *skb, struct net_bridge *br,
2101 u16 vid, struct net_bridge_port *p, struct nd_msg *msg);
2102 struct nd_msg *br_is_nd_neigh_msg(struct sk_buff *skb, struct nd_msg *m);