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 * @msti: if MASTER flag set, this holds the VLANs MST instance
182 * @vlist: sorted list of VLAN entries
183 * @rcu: used for entry destruction
185 * This structure is shared between the global per-VLAN entries contained in
186 * the bridge rhashtable and the local per-port per-VLAN entries contained in
187 * the port's rhashtable. The union entries should be interpreted depending on
188 * the entry flags that are set.
190 struct net_bridge_vlan {
191 struct rhash_head vnode;
192 struct rhash_head tnode;
197 struct pcpu_sw_netstats __percpu *stats;
199 struct net_bridge *br;
200 struct net_bridge_port *port;
204 struct net_bridge_vlan *brvlan;
207 struct br_tunnel_info tinfo;
210 struct net_bridge_mcast br_mcast_ctx;
211 struct net_bridge_mcast_port port_mcast_ctx;
216 struct list_head vlist;
222 * struct net_bridge_vlan_group
224 * @vlan_hash: VLAN entry rhashtable
225 * @vlan_list: sorted VLAN entry list
226 * @num_vlans: number of total VLAN entries
227 * @pvid: PVID VLAN id
228 * @pvid_state: PVID's STP state (e.g. forwarding, learning, blocking)
230 * IMPORTANT: Be careful when checking if there're VLAN entries using list
231 * primitives because the bridge can have entries in its list which
232 * are just for global context but not for filtering, i.e. they have
233 * the master flag set but not the brentry flag. If you have to check
234 * if there're "real" entries in the bridge please test @num_vlans
236 struct net_bridge_vlan_group {
237 struct rhashtable vlan_hash;
238 struct rhashtable tunnel_hash;
239 struct list_head vlan_list;
245 /* bridge fdb flags */
250 BR_FDB_ADDED_BY_USER,
251 BR_FDB_ADDED_BY_EXT_LEARN,
254 BR_FDB_NOTIFY_INACTIVE
257 struct net_bridge_fdb_key {
262 struct net_bridge_fdb_entry {
263 struct rhash_head rhnode;
264 struct net_bridge_port *dst;
266 struct net_bridge_fdb_key key;
267 struct hlist_node fdb_node;
270 /* write-heavy members should not affect lookups */
271 unsigned long updated ____cacheline_aligned_in_smp;
277 #define MDB_PG_FLAGS_PERMANENT BIT(0)
278 #define MDB_PG_FLAGS_OFFLOAD BIT(1)
279 #define MDB_PG_FLAGS_FAST_LEAVE BIT(2)
280 #define MDB_PG_FLAGS_STAR_EXCL BIT(3)
281 #define MDB_PG_FLAGS_BLOCKED BIT(4)
283 #define PG_SRC_ENT_LIMIT 32
285 #define BR_SGRP_F_DELETE BIT(0)
286 #define BR_SGRP_F_SEND BIT(1)
287 #define BR_SGRP_F_INSTALLED BIT(2)
289 struct net_bridge_mcast_gc {
290 struct hlist_node gc_node;
291 void (*destroy)(struct net_bridge_mcast_gc *gc);
294 struct net_bridge_group_src {
295 struct hlist_node node;
298 struct net_bridge_port_group *pg;
300 u8 src_query_rexmit_cnt;
301 struct timer_list timer;
303 struct net_bridge *br;
304 struct net_bridge_mcast_gc mcast_gc;
308 struct net_bridge_port_group_sg_key {
309 struct net_bridge_port *port;
313 struct net_bridge_port_group {
314 struct net_bridge_port_group __rcu *next;
315 struct net_bridge_port_group_sg_key key;
316 unsigned char eth_addr[ETH_ALEN] __aligned(2);
318 unsigned char filter_mode;
319 unsigned char grp_query_rexmit_cnt;
320 unsigned char rt_protocol;
322 struct hlist_head src_list;
323 unsigned int src_ents;
324 struct timer_list timer;
325 struct timer_list rexmit_timer;
326 struct hlist_node mglist;
327 struct rb_root eht_set_tree;
328 struct rb_root eht_host_tree;
330 struct rhash_head rhnode;
331 struct net_bridge_mcast_gc mcast_gc;
335 struct net_bridge_mdb_entry {
336 struct rhash_head rhnode;
337 struct net_bridge *br;
338 struct net_bridge_port_group __rcu *ports;
342 struct timer_list timer;
343 struct hlist_node mdb_node;
345 struct net_bridge_mcast_gc mcast_gc;
349 struct net_bridge_port {
350 struct net_bridge *br;
351 struct net_device *dev;
352 netdevice_tracker dev_tracker;
353 struct list_head list;
356 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
357 struct net_bridge_vlan_group __rcu *vlgrp;
359 struct net_bridge_port __rcu *backup_port;
365 unsigned char topology_change_ack;
366 unsigned char config_pending;
368 port_id designated_port;
369 bridge_id designated_root;
370 bridge_id designated_bridge;
373 unsigned long designated_age;
375 struct timer_list forward_delay_timer;
376 struct timer_list hold_timer;
377 struct timer_list message_age_timer;
381 struct net_bridge_mcast_port multicast_ctx;
383 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
384 struct bridge_mcast_stats __percpu *mcast_stats;
386 u32 multicast_eht_hosts_limit;
387 u32 multicast_eht_hosts_cnt;
388 struct hlist_head mglist;
392 char sysfs_name[IFNAMSIZ];
395 #ifdef CONFIG_NET_POLL_CONTROLLER
398 #ifdef CONFIG_NET_SWITCHDEV
399 /* Identifier used to group ports that share the same switchdev
404 struct netdev_phys_item_id ppid;
407 u16 backup_redirected_cnt;
409 struct bridge_stp_xstats stp_xstats;
412 #define kobj_to_brport(obj) container_of(obj, struct net_bridge_port, kobj)
414 #define br_auto_port(p) ((p)->flags & BR_AUTO_MASK)
415 #define br_promisc_port(p) ((p)->flags & BR_PROMISC)
417 static inline struct net_bridge_port *br_port_get_rcu(const struct net_device *dev)
419 return rcu_dereference(dev->rx_handler_data);
422 static inline struct net_bridge_port *br_port_get_rtnl(const struct net_device *dev)
424 return netif_is_bridge_port(dev) ?
425 rtnl_dereference(dev->rx_handler_data) : NULL;
428 static inline struct net_bridge_port *br_port_get_rtnl_rcu(const struct net_device *dev)
430 return netif_is_bridge_port(dev) ?
431 rcu_dereference_rtnl(dev->rx_handler_data) : NULL;
434 enum net_bridge_opts {
436 BROPT_VLAN_STATS_ENABLED,
437 BROPT_NF_CALL_IPTABLES,
438 BROPT_NF_CALL_IP6TABLES,
439 BROPT_NF_CALL_ARPTABLES,
440 BROPT_GROUP_ADDR_SET,
441 BROPT_MULTICAST_ENABLED,
442 BROPT_MULTICAST_QUERY_USE_IFADDR,
443 BROPT_MULTICAST_STATS_ENABLED,
445 BROPT_NEIGH_SUPPRESS_ENABLED,
446 BROPT_MTU_SET_BY_USER,
447 BROPT_VLAN_STATS_PER_PORT,
449 BROPT_VLAN_BRIDGE_BINDING,
450 BROPT_MCAST_VLAN_SNOOPING_ENABLED,
456 spinlock_t hash_lock;
457 struct hlist_head frame_type_list;
458 struct net_device *dev;
459 unsigned long options;
460 /* These fields are accessed on each packet */
461 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
464 struct net_bridge_vlan_group __rcu *vlgrp;
467 struct rhashtable fdb_hash_tbl;
468 struct list_head port_list;
469 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
471 struct rtable fake_rtable;
472 struct rt6_info fake_rt6_info;
476 u16 group_fwd_mask_required;
479 bridge_id designated_root;
481 unsigned char topology_change;
482 unsigned char topology_change_detected;
484 unsigned long max_age;
485 unsigned long hello_time;
486 unsigned long forward_delay;
487 unsigned long ageing_time;
488 unsigned long bridge_max_age;
489 unsigned long bridge_hello_time;
490 unsigned long bridge_forward_delay;
491 unsigned long bridge_ageing_time;
494 u8 group_addr[ETH_ALEN];
497 BR_NO_STP, /* no spanning tree */
498 BR_KERNEL_STP, /* old STP in kernel */
499 BR_USER_STP, /* new RSTP in userspace */
502 struct net_bridge_mcast multicast_ctx;
504 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
505 struct bridge_mcast_stats __percpu *mcast_stats;
509 spinlock_t multicast_lock;
511 struct rhashtable mdb_hash_tbl;
512 struct rhashtable sg_port_tbl;
514 struct hlist_head mcast_gc_list;
515 struct hlist_head mdb_list;
517 struct work_struct mcast_gc_work;
520 struct timer_list hello_timer;
521 struct timer_list tcn_timer;
522 struct timer_list topology_change_timer;
523 struct delayed_work gc_work;
524 struct kobject *ifobj;
527 #ifdef CONFIG_NET_SWITCHDEV
528 /* Counter used to make sure that hardware domains get unique
529 * identifiers in case a bridge spans multiple switchdev instances.
532 /* Bit mask of hardware domain numbers in use */
533 unsigned long busy_hwdoms;
535 struct hlist_head fdb_list;
537 #if IS_ENABLED(CONFIG_BRIDGE_MRP)
538 struct hlist_head mrp_list;
540 #if IS_ENABLED(CONFIG_BRIDGE_CFM)
541 struct hlist_head mep_list;
545 struct br_input_skb_cb {
546 struct net_device *brdev;
549 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
553 u8 proxyarp_replied:1;
554 u8 src_port_isolated:1;
555 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
558 #ifdef CONFIG_NETFILTER_FAMILY_BRIDGE
559 u8 br_netfilter_broute:1;
562 #ifdef CONFIG_NET_SWITCHDEV
563 /* Set if TX data plane offloading is used towards at least one
567 /* The switchdev hardware domain from which this packet was received.
568 * If skb->offload_fwd_mark was set, then this packet was already
569 * forwarded by hardware to the other ports in the source hardware
570 * domain, otherwise it wasn't.
573 /* Bit mask of hardware domains towards this packet has already been
574 * transmitted using the TX data plane offload.
576 unsigned long fwd_hwdoms;
580 #define BR_INPUT_SKB_CB(__skb) ((struct br_input_skb_cb *)(__skb)->cb)
582 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
583 # define BR_INPUT_SKB_CB_MROUTERS_ONLY(__skb) (BR_INPUT_SKB_CB(__skb)->mrouters_only)
585 # define BR_INPUT_SKB_CB_MROUTERS_ONLY(__skb) (0)
588 #define br_printk(level, br, format, args...) \
589 printk(level "%s: " format, (br)->dev->name, ##args)
591 #define br_err(__br, format, args...) \
592 br_printk(KERN_ERR, __br, format, ##args)
593 #define br_warn(__br, format, args...) \
594 br_printk(KERN_WARNING, __br, format, ##args)
595 #define br_notice(__br, format, args...) \
596 br_printk(KERN_NOTICE, __br, format, ##args)
597 #define br_info(__br, format, args...) \
598 br_printk(KERN_INFO, __br, format, ##args)
600 #define br_debug(br, format, args...) \
601 pr_debug("%s: " format, (br)->dev->name, ##args)
603 /* called under bridge lock */
604 static inline int br_is_root_bridge(const struct net_bridge *br)
606 return !memcmp(&br->bridge_id, &br->designated_root, 8);
609 /* check if a VLAN entry is global */
610 static inline bool br_vlan_is_master(const struct net_bridge_vlan *v)
612 return v->flags & BRIDGE_VLAN_INFO_MASTER;
615 /* check if a VLAN entry is used by the bridge */
616 static inline bool br_vlan_is_brentry(const struct net_bridge_vlan *v)
618 return v->flags & BRIDGE_VLAN_INFO_BRENTRY;
621 /* check if we should use the vlan entry, returns false if it's only context */
622 static inline bool br_vlan_should_use(const struct net_bridge_vlan *v)
624 if (br_vlan_is_master(v)) {
625 if (br_vlan_is_brentry(v))
634 static inline bool nbp_state_should_learn(const struct net_bridge_port *p)
636 return p->state == BR_STATE_LEARNING || p->state == BR_STATE_FORWARDING;
639 static inline bool br_vlan_valid_id(u16 vid, struct netlink_ext_ack *extack)
641 bool ret = vid > 0 && vid < VLAN_VID_MASK;
644 NL_SET_ERR_MSG_MOD(extack, "Vlan id is invalid");
649 static inline bool br_vlan_valid_range(const struct bridge_vlan_info *cur,
650 const struct bridge_vlan_info *last,
651 struct netlink_ext_ack *extack)
653 /* pvid flag is not allowed in ranges */
654 if (cur->flags & BRIDGE_VLAN_INFO_PVID) {
655 NL_SET_ERR_MSG_MOD(extack, "Pvid isn't allowed in a range");
659 /* when cur is the range end, check if:
660 * - it has range start flag
661 * - range ids are invalid (end is equal to or before start)
664 if (cur->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
665 NL_SET_ERR_MSG_MOD(extack, "Found a new vlan range start while processing one");
667 } else if (!(cur->flags & BRIDGE_VLAN_INFO_RANGE_END)) {
668 NL_SET_ERR_MSG_MOD(extack, "Vlan range end flag is missing");
670 } else if (cur->vid <= last->vid) {
671 NL_SET_ERR_MSG_MOD(extack, "End vlan id is less than or equal to start vlan id");
676 /* check for required range flags */
677 if (!(cur->flags & (BRIDGE_VLAN_INFO_RANGE_BEGIN |
678 BRIDGE_VLAN_INFO_RANGE_END))) {
679 NL_SET_ERR_MSG_MOD(extack, "Both vlan range flags are missing");
686 static inline u8 br_vlan_multicast_router(const struct net_bridge_vlan *v)
688 u8 mcast_router = MDB_RTR_TYPE_DISABLED;
690 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
691 if (!br_vlan_is_master(v))
692 mcast_router = v->port_mcast_ctx.multicast_router;
694 mcast_router = v->br_mcast_ctx.multicast_router;
700 static inline int br_afspec_cmd_to_rtm(int cmd)
712 static inline int br_opt_get(const struct net_bridge *br,
713 enum net_bridge_opts opt)
715 return test_bit(opt, &br->options);
718 int br_boolopt_toggle(struct net_bridge *br, enum br_boolopt_id opt, bool on,
719 struct netlink_ext_ack *extack);
720 int br_boolopt_get(const struct net_bridge *br, enum br_boolopt_id opt);
721 int br_boolopt_multi_toggle(struct net_bridge *br,
722 struct br_boolopt_multi *bm,
723 struct netlink_ext_ack *extack);
724 void br_boolopt_multi_get(const struct net_bridge *br,
725 struct br_boolopt_multi *bm);
726 void br_opt_toggle(struct net_bridge *br, enum net_bridge_opts opt, bool on);
729 void br_dev_setup(struct net_device *dev);
730 void br_dev_delete(struct net_device *dev, struct list_head *list);
731 netdev_tx_t br_dev_xmit(struct sk_buff *skb, struct net_device *dev);
732 #ifdef CONFIG_NET_POLL_CONTROLLER
733 static inline void br_netpoll_send_skb(const struct net_bridge_port *p,
736 netpoll_send_skb(p->np, skb);
739 int br_netpoll_enable(struct net_bridge_port *p);
740 void br_netpoll_disable(struct net_bridge_port *p);
742 static inline void br_netpoll_send_skb(const struct net_bridge_port *p,
747 static inline int br_netpoll_enable(struct net_bridge_port *p)
752 static inline void br_netpoll_disable(struct net_bridge_port *p)
758 int br_fdb_init(void);
759 void br_fdb_fini(void);
760 int br_fdb_hash_init(struct net_bridge *br);
761 void br_fdb_hash_fini(struct net_bridge *br);
762 void br_fdb_flush(struct net_bridge *br);
763 void br_fdb_find_delete_local(struct net_bridge *br,
764 const struct net_bridge_port *p,
765 const unsigned char *addr, u16 vid);
766 void br_fdb_changeaddr(struct net_bridge_port *p, const unsigned char *newaddr);
767 void br_fdb_change_mac_address(struct net_bridge *br, const u8 *newaddr);
768 void br_fdb_cleanup(struct work_struct *work);
769 void br_fdb_delete_by_port(struct net_bridge *br,
770 const struct net_bridge_port *p, u16 vid, int do_all);
771 struct net_bridge_fdb_entry *br_fdb_find_rcu(struct net_bridge *br,
772 const unsigned char *addr,
774 int br_fdb_test_addr(struct net_device *dev, unsigned char *addr);
775 int br_fdb_fillbuf(struct net_bridge *br, void *buf, unsigned long count,
777 int br_fdb_add_local(struct net_bridge *br, struct net_bridge_port *source,
778 const unsigned char *addr, u16 vid);
779 void br_fdb_update(struct net_bridge *br, struct net_bridge_port *source,
780 const unsigned char *addr, u16 vid, unsigned long flags);
782 int br_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
783 struct net_device *dev, const unsigned char *addr, u16 vid);
784 int br_fdb_add(struct ndmsg *nlh, struct nlattr *tb[], struct net_device *dev,
785 const unsigned char *addr, u16 vid, u16 nlh_flags,
786 struct netlink_ext_ack *extack);
787 int br_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
788 struct net_device *dev, struct net_device *fdev, int *idx);
789 int br_fdb_get(struct sk_buff *skb, struct nlattr *tb[], struct net_device *dev,
790 const unsigned char *addr, u16 vid, u32 portid, u32 seq,
791 struct netlink_ext_ack *extack);
792 int br_fdb_sync_static(struct net_bridge *br, struct net_bridge_port *p);
793 void br_fdb_unsync_static(struct net_bridge *br, struct net_bridge_port *p);
794 int br_fdb_external_learn_add(struct net_bridge *br, struct net_bridge_port *p,
795 const unsigned char *addr, u16 vid,
797 int br_fdb_external_learn_del(struct net_bridge *br, struct net_bridge_port *p,
798 const unsigned char *addr, u16 vid,
800 void br_fdb_offloaded_set(struct net_bridge *br, struct net_bridge_port *p,
801 const unsigned char *addr, u16 vid, bool offloaded);
809 int br_dev_queue_push_xmit(struct net *net, struct sock *sk, struct sk_buff *skb);
810 void br_forward(const struct net_bridge_port *to, struct sk_buff *skb,
811 bool local_rcv, bool local_orig);
812 int br_forward_finish(struct net *net, struct sock *sk, struct sk_buff *skb);
813 void br_flood(struct net_bridge *br, struct sk_buff *skb,
814 enum br_pkt_type pkt_type, bool local_rcv, bool local_orig);
816 /* return true if both source port and dest port are isolated */
817 static inline bool br_skb_isolated(const struct net_bridge_port *to,
818 const struct sk_buff *skb)
820 return BR_INPUT_SKB_CB(skb)->src_port_isolated &&
821 (to->flags & BR_ISOLATED);
825 void br_port_carrier_check(struct net_bridge_port *p, bool *notified);
826 int br_add_bridge(struct net *net, const char *name);
827 int br_del_bridge(struct net *net, const char *name);
828 int br_add_if(struct net_bridge *br, struct net_device *dev,
829 struct netlink_ext_ack *extack);
830 int br_del_if(struct net_bridge *br, struct net_device *dev);
831 void br_mtu_auto_adjust(struct net_bridge *br);
832 netdev_features_t br_features_recompute(struct net_bridge *br,
833 netdev_features_t features);
834 void br_port_flags_change(struct net_bridge_port *port, unsigned long mask);
835 void br_manage_promisc(struct net_bridge *br);
836 int nbp_backup_change(struct net_bridge_port *p, struct net_device *backup_dev);
839 int br_handle_frame_finish(struct net *net, struct sock *sk, struct sk_buff *skb);
840 rx_handler_func_t *br_get_rx_handler(const struct net_device *dev);
842 struct br_frame_type {
844 int (*frame_handler)(struct net_bridge_port *port,
845 struct sk_buff *skb);
846 struct hlist_node list;
849 void br_add_frame(struct net_bridge *br, struct br_frame_type *ft);
850 void br_del_frame(struct net_bridge *br, struct br_frame_type *ft);
852 static inline bool br_rx_handler_check_rcu(const struct net_device *dev)
854 return rcu_dereference(dev->rx_handler) == br_get_rx_handler(dev);
857 static inline bool br_rx_handler_check_rtnl(const struct net_device *dev)
859 return rcu_dereference_rtnl(dev->rx_handler) == br_get_rx_handler(dev);
862 static inline struct net_bridge_port *br_port_get_check_rcu(const struct net_device *dev)
864 return br_rx_handler_check_rcu(dev) ? br_port_get_rcu(dev) : NULL;
867 static inline struct net_bridge_port *
868 br_port_get_check_rtnl(const struct net_device *dev)
870 return br_rx_handler_check_rtnl(dev) ? br_port_get_rtnl_rcu(dev) : NULL;
874 int br_dev_siocdevprivate(struct net_device *dev, struct ifreq *rq,
875 void __user *data, int cmd);
876 int br_ioctl_stub(struct net *net, struct net_bridge *br, unsigned int cmd,
877 struct ifreq *ifr, void __user *uarg);
880 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
881 int br_multicast_rcv(struct net_bridge_mcast **brmctx,
882 struct net_bridge_mcast_port **pmctx,
883 struct net_bridge_vlan *vlan,
884 struct sk_buff *skb, u16 vid);
885 struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge_mcast *brmctx,
886 struct sk_buff *skb, u16 vid);
887 int br_multicast_add_port(struct net_bridge_port *port);
888 void br_multicast_del_port(struct net_bridge_port *port);
889 void br_multicast_enable_port(struct net_bridge_port *port);
890 void br_multicast_disable_port(struct net_bridge_port *port);
891 void br_multicast_init(struct net_bridge *br);
892 void br_multicast_join_snoopers(struct net_bridge *br);
893 void br_multicast_leave_snoopers(struct net_bridge *br);
894 void br_multicast_open(struct net_bridge *br);
895 void br_multicast_stop(struct net_bridge *br);
896 void br_multicast_dev_del(struct net_bridge *br);
897 void br_multicast_flood(struct net_bridge_mdb_entry *mdst, struct sk_buff *skb,
898 struct net_bridge_mcast *brmctx,
899 bool local_rcv, bool local_orig);
900 int br_multicast_set_router(struct net_bridge_mcast *brmctx, unsigned long val);
901 int br_multicast_set_port_router(struct net_bridge_mcast_port *pmctx,
903 int br_multicast_set_vlan_router(struct net_bridge_vlan *v, u8 mcast_router);
904 int br_multicast_toggle(struct net_bridge *br, unsigned long val,
905 struct netlink_ext_ack *extack);
906 int br_multicast_set_querier(struct net_bridge_mcast *brmctx, unsigned long val);
907 int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val);
908 int br_multicast_set_igmp_version(struct net_bridge_mcast *brmctx,
910 #if IS_ENABLED(CONFIG_IPV6)
911 int br_multicast_set_mld_version(struct net_bridge_mcast *brmctx,
914 struct net_bridge_mdb_entry *
915 br_mdb_ip_get(struct net_bridge *br, struct br_ip *dst);
916 struct net_bridge_mdb_entry *
917 br_multicast_new_group(struct net_bridge *br, struct br_ip *group);
918 struct net_bridge_port_group *
919 br_multicast_new_port_group(struct net_bridge_port *port, struct br_ip *group,
920 struct net_bridge_port_group __rcu *next,
921 unsigned char flags, const unsigned char *src,
922 u8 filter_mode, u8 rt_protocol);
923 int br_mdb_hash_init(struct net_bridge *br);
924 void br_mdb_hash_fini(struct net_bridge *br);
925 void br_mdb_notify(struct net_device *dev, struct net_bridge_mdb_entry *mp,
926 struct net_bridge_port_group *pg, int type);
927 void br_rtr_notify(struct net_device *dev, struct net_bridge_mcast_port *pmctx,
929 void br_multicast_del_pg(struct net_bridge_mdb_entry *mp,
930 struct net_bridge_port_group *pg,
931 struct net_bridge_port_group __rcu **pp);
932 void br_multicast_count(struct net_bridge *br,
933 const struct net_bridge_port *p,
934 const struct sk_buff *skb, u8 type, u8 dir);
935 int br_multicast_init_stats(struct net_bridge *br);
936 void br_multicast_uninit_stats(struct net_bridge *br);
937 void br_multicast_get_stats(const struct net_bridge *br,
938 const struct net_bridge_port *p,
939 struct br_mcast_stats *dest);
940 void br_mdb_init(void);
941 void br_mdb_uninit(void);
942 void br_multicast_host_join(const struct net_bridge_mcast *brmctx,
943 struct net_bridge_mdb_entry *mp, bool notify);
944 void br_multicast_host_leave(struct net_bridge_mdb_entry *mp, bool notify);
945 void br_multicast_star_g_handle_mode(struct net_bridge_port_group *pg,
947 void br_multicast_sg_add_exclude_ports(struct net_bridge_mdb_entry *star_mp,
948 struct net_bridge_port_group *sg);
949 struct net_bridge_group_src *
950 br_multicast_find_group_src(struct net_bridge_port_group *pg, struct br_ip *ip);
951 void br_multicast_del_group_src(struct net_bridge_group_src *src,
953 void br_multicast_ctx_init(struct net_bridge *br,
954 struct net_bridge_vlan *vlan,
955 struct net_bridge_mcast *brmctx);
956 void br_multicast_ctx_deinit(struct net_bridge_mcast *brmctx);
957 void br_multicast_port_ctx_init(struct net_bridge_port *port,
958 struct net_bridge_vlan *vlan,
959 struct net_bridge_mcast_port *pmctx);
960 void br_multicast_port_ctx_deinit(struct net_bridge_mcast_port *pmctx);
961 void br_multicast_toggle_one_vlan(struct net_bridge_vlan *vlan, bool on);
962 int br_multicast_toggle_vlan_snooping(struct net_bridge *br, bool on,
963 struct netlink_ext_ack *extack);
964 bool br_multicast_toggle_global_vlan(struct net_bridge_vlan *vlan, bool on);
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,
1406 br_multicast_ctx_options_equal(const struct net_bridge_mcast *brmctx1,
1407 const struct net_bridge_mcast *brmctx2)
1414 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
1415 bool br_allowed_ingress(const struct net_bridge *br,
1416 struct net_bridge_vlan_group *vg, struct sk_buff *skb,
1417 u16 *vid, u8 *state,
1418 struct net_bridge_vlan **vlan);
1419 bool br_allowed_egress(struct net_bridge_vlan_group *vg,
1420 const struct sk_buff *skb);
1421 bool br_should_learn(struct net_bridge_port *p, struct sk_buff *skb, u16 *vid);
1422 struct sk_buff *br_handle_vlan(struct net_bridge *br,
1423 const struct net_bridge_port *port,
1424 struct net_bridge_vlan_group *vg,
1425 struct sk_buff *skb);
1426 int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags,
1427 bool *changed, struct netlink_ext_ack *extack);
1428 int br_vlan_delete(struct net_bridge *br, u16 vid);
1429 void br_vlan_flush(struct net_bridge *br);
1430 struct net_bridge_vlan *br_vlan_find(struct net_bridge_vlan_group *vg, u16 vid);
1431 void br_recalculate_fwd_mask(struct net_bridge *br);
1432 int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val,
1433 struct netlink_ext_ack *extack);
1434 int __br_vlan_set_proto(struct net_bridge *br, __be16 proto,
1435 struct netlink_ext_ack *extack);
1436 int br_vlan_set_proto(struct net_bridge *br, unsigned long val,
1437 struct netlink_ext_ack *extack);
1438 int br_vlan_set_stats(struct net_bridge *br, unsigned long val);
1439 int br_vlan_set_stats_per_port(struct net_bridge *br, unsigned long val);
1440 int br_vlan_init(struct net_bridge *br);
1441 int br_vlan_set_default_pvid(struct net_bridge *br, unsigned long val,
1442 struct netlink_ext_ack *extack);
1443 int __br_vlan_set_default_pvid(struct net_bridge *br, u16 pvid,
1444 struct netlink_ext_ack *extack);
1445 int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags,
1446 bool *changed, struct netlink_ext_ack *extack);
1447 int nbp_vlan_delete(struct net_bridge_port *port, u16 vid);
1448 void nbp_vlan_flush(struct net_bridge_port *port);
1449 int nbp_vlan_init(struct net_bridge_port *port, struct netlink_ext_ack *extack);
1450 int nbp_get_num_vlan_infos(struct net_bridge_port *p, u32 filter_mask);
1451 void br_vlan_get_stats(const struct net_bridge_vlan *v,
1452 struct pcpu_sw_netstats *stats);
1453 void br_vlan_port_event(struct net_bridge_port *p, unsigned long event);
1454 int br_vlan_bridge_event(struct net_device *dev, unsigned long event,
1456 void br_vlan_rtnl_init(void);
1457 void br_vlan_rtnl_uninit(void);
1458 void br_vlan_notify(const struct net_bridge *br,
1459 const struct net_bridge_port *p,
1460 u16 vid, u16 vid_range,
1462 bool br_vlan_can_enter_range(const struct net_bridge_vlan *v_curr,
1463 const struct net_bridge_vlan *range_end);
1465 void br_vlan_fill_forward_path_pvid(struct net_bridge *br,
1466 struct net_device_path_ctx *ctx,
1467 struct net_device_path *path);
1468 int br_vlan_fill_forward_path_mode(struct net_bridge *br,
1469 struct net_bridge_port *dst,
1470 struct net_device_path *path);
1472 static inline struct net_bridge_vlan_group *br_vlan_group(
1473 const struct net_bridge *br)
1475 return rtnl_dereference(br->vlgrp);
1478 static inline struct net_bridge_vlan_group *nbp_vlan_group(
1479 const struct net_bridge_port *p)
1481 return rtnl_dereference(p->vlgrp);
1484 static inline struct net_bridge_vlan_group *br_vlan_group_rcu(
1485 const struct net_bridge *br)
1487 return rcu_dereference(br->vlgrp);
1490 static inline struct net_bridge_vlan_group *nbp_vlan_group_rcu(
1491 const struct net_bridge_port *p)
1493 return rcu_dereference(p->vlgrp);
1496 /* Since bridge now depends on 8021Q module, but the time bridge sees the
1497 * skb, the vlan tag will always be present if the frame was tagged.
1499 static inline int br_vlan_get_tag(const struct sk_buff *skb, u16 *vid)
1503 if (skb_vlan_tag_present(skb)) {
1504 *vid = skb_vlan_tag_get_id(skb);
1513 static inline u16 br_get_pvid(const struct net_bridge_vlan_group *vg)
1522 static inline u16 br_vlan_flags(const struct net_bridge_vlan *v, u16 pvid)
1524 return v->vid == pvid ? v->flags | BRIDGE_VLAN_INFO_PVID : v->flags;
1527 static inline bool br_allowed_ingress(const struct net_bridge *br,
1528 struct net_bridge_vlan_group *vg,
1529 struct sk_buff *skb,
1530 u16 *vid, u8 *state,
1531 struct net_bridge_vlan **vlan)
1538 static inline bool br_allowed_egress(struct net_bridge_vlan_group *vg,
1539 const struct sk_buff *skb)
1544 static inline bool br_should_learn(struct net_bridge_port *p,
1545 struct sk_buff *skb, u16 *vid)
1550 static inline struct sk_buff *br_handle_vlan(struct net_bridge *br,
1551 const struct net_bridge_port *port,
1552 struct net_bridge_vlan_group *vg,
1553 struct sk_buff *skb)
1558 static inline int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags,
1559 bool *changed, struct netlink_ext_ack *extack)
1565 static inline int br_vlan_delete(struct net_bridge *br, u16 vid)
1570 static inline void br_vlan_flush(struct net_bridge *br)
1574 static inline void br_recalculate_fwd_mask(struct net_bridge *br)
1578 static inline int br_vlan_init(struct net_bridge *br)
1583 static inline int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags,
1584 bool *changed, struct netlink_ext_ack *extack)
1590 static inline int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
1595 static inline void nbp_vlan_flush(struct net_bridge_port *port)
1599 static inline struct net_bridge_vlan *br_vlan_find(struct net_bridge_vlan_group *vg,
1605 static inline int nbp_vlan_init(struct net_bridge_port *port,
1606 struct netlink_ext_ack *extack)
1611 static inline u16 br_vlan_get_tag(const struct sk_buff *skb, u16 *tag)
1616 static inline u16 br_get_pvid(const struct net_bridge_vlan_group *vg)
1621 static inline int br_vlan_filter_toggle(struct net_bridge *br,
1623 struct netlink_ext_ack *extack)
1628 static inline int nbp_get_num_vlan_infos(struct net_bridge_port *p,
1634 static inline void br_vlan_fill_forward_path_pvid(struct net_bridge *br,
1635 struct net_device_path_ctx *ctx,
1636 struct net_device_path *path)
1640 static inline int br_vlan_fill_forward_path_mode(struct net_bridge *br,
1641 struct net_bridge_port *dst,
1642 struct net_device_path *path)
1647 static inline struct net_bridge_vlan_group *br_vlan_group(
1648 const struct net_bridge *br)
1653 static inline struct net_bridge_vlan_group *nbp_vlan_group(
1654 const struct net_bridge_port *p)
1659 static inline struct net_bridge_vlan_group *br_vlan_group_rcu(
1660 const struct net_bridge *br)
1665 static inline struct net_bridge_vlan_group *nbp_vlan_group_rcu(
1666 const struct net_bridge_port *p)
1671 static inline void br_vlan_get_stats(const struct net_bridge_vlan *v,
1672 struct pcpu_sw_netstats *stats)
1676 static inline void br_vlan_port_event(struct net_bridge_port *p,
1677 unsigned long event)
1681 static inline int br_vlan_bridge_event(struct net_device *dev,
1682 unsigned long event, void *ptr)
1687 static inline void br_vlan_rtnl_init(void)
1691 static inline void br_vlan_rtnl_uninit(void)
1695 static inline void br_vlan_notify(const struct net_bridge *br,
1696 const struct net_bridge_port *p,
1697 u16 vid, u16 vid_range,
1702 static inline bool br_vlan_can_enter_range(const struct net_bridge_vlan *v_curr,
1703 const struct net_bridge_vlan *range_end)
1708 static inline u16 br_vlan_flags(const struct net_bridge_vlan *v, u16 pvid)
1715 /* br_vlan_options.c */
1716 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
1717 bool br_vlan_opts_eq_range(const struct net_bridge_vlan *v_curr,
1718 const struct net_bridge_vlan *range_end);
1719 bool br_vlan_opts_fill(struct sk_buff *skb, const struct net_bridge_vlan *v);
1720 size_t br_vlan_opts_nl_size(void);
1721 int br_vlan_process_options(const struct net_bridge *br,
1722 const struct net_bridge_port *p,
1723 struct net_bridge_vlan *range_start,
1724 struct net_bridge_vlan *range_end,
1726 struct netlink_ext_ack *extack);
1727 int br_vlan_rtm_process_global_options(struct net_device *dev,
1728 const struct nlattr *attr,
1730 struct netlink_ext_ack *extack);
1731 bool br_vlan_global_opts_can_enter_range(const struct net_bridge_vlan *v_curr,
1732 const struct net_bridge_vlan *r_end);
1733 bool br_vlan_global_opts_fill(struct sk_buff *skb, u16 vid, u16 vid_range,
1734 const struct net_bridge_vlan *v_opts);
1736 /* vlan state manipulation helpers using *_ONCE to annotate lock-free access */
1737 static inline u8 br_vlan_get_state(const struct net_bridge_vlan *v)
1739 return READ_ONCE(v->state);
1742 static inline void br_vlan_set_state(struct net_bridge_vlan *v, u8 state)
1744 WRITE_ONCE(v->state, state);
1747 static inline u8 br_vlan_get_pvid_state(const struct net_bridge_vlan_group *vg)
1749 return READ_ONCE(vg->pvid_state);
1752 static inline void br_vlan_set_pvid_state(struct net_bridge_vlan_group *vg,
1755 WRITE_ONCE(vg->pvid_state, state);
1758 /* learn_allow is true at ingress and false at egress */
1759 static inline bool br_vlan_state_allowed(u8 state, bool learn_allow)
1762 case BR_STATE_LEARNING:
1764 case BR_STATE_FORWARDING:
1773 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
1774 DECLARE_STATIC_KEY_FALSE(br_mst_used);
1775 static inline bool br_mst_is_enabled(struct net_bridge *br)
1777 return static_branch_unlikely(&br_mst_used) &&
1778 br_opt_get(br, BROPT_MST_ENABLED);
1781 int br_mst_set_state(struct net_bridge_port *p, u16 msti, u8 state,
1782 struct netlink_ext_ack *extack);
1783 int br_mst_vlan_set_msti(struct net_bridge_vlan *v, u16 msti);
1784 void br_mst_vlan_init_state(struct net_bridge_vlan *v);
1785 int br_mst_set_enabled(struct net_bridge *br, bool on,
1786 struct netlink_ext_ack *extack);
1787 size_t br_mst_info_size(const struct net_bridge_vlan_group *vg);
1788 int br_mst_fill_info(struct sk_buff *skb,
1789 const struct net_bridge_vlan_group *vg);
1790 int br_mst_process(struct net_bridge_port *p, const struct nlattr *mst_attr,
1791 struct netlink_ext_ack *extack);
1793 static inline bool br_mst_is_enabled(struct net_bridge *br)
1798 static inline int br_mst_set_state(struct net_bridge_port *p, u16 msti,
1799 u8 state, struct netlink_ext_ack *extack)
1804 static inline int br_mst_set_enabled(struct net_bridge *br, bool on,
1805 struct netlink_ext_ack *extack)
1810 static inline size_t br_mst_info_size(const struct net_bridge_vlan_group *vg)
1815 static inline int br_mst_fill_info(struct sk_buff *skb,
1816 const struct net_bridge_vlan_group *vg)
1821 static inline int br_mst_process(struct net_bridge_port *p,
1822 const struct nlattr *mst_attr,
1823 struct netlink_ext_ack *extack)
1830 int (*br_dev_xmit_hook)(struct sk_buff *skb);
1832 extern const struct nf_br_ops __rcu *nf_br_ops;
1834 /* br_netfilter.c */
1835 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1836 int br_nf_core_init(void);
1837 void br_nf_core_fini(void);
1838 void br_netfilter_rtable_init(struct net_bridge *);
1840 static inline int br_nf_core_init(void) { return 0; }
1841 static inline void br_nf_core_fini(void) {}
1842 #define br_netfilter_rtable_init(x)
1846 void br_set_state(struct net_bridge_port *p, unsigned int state);
1847 struct net_bridge_port *br_get_port(struct net_bridge *br, u16 port_no);
1848 void br_init_port(struct net_bridge_port *p);
1849 void br_become_designated_port(struct net_bridge_port *p);
1851 void __br_set_forward_delay(struct net_bridge *br, unsigned long t);
1852 int br_set_forward_delay(struct net_bridge *br, unsigned long x);
1853 int br_set_hello_time(struct net_bridge *br, unsigned long x);
1854 int br_set_max_age(struct net_bridge *br, unsigned long x);
1855 int __set_ageing_time(struct net_device *dev, unsigned long t);
1856 int br_set_ageing_time(struct net_bridge *br, clock_t ageing_time);
1860 void br_stp_enable_bridge(struct net_bridge *br);
1861 void br_stp_disable_bridge(struct net_bridge *br);
1862 int br_stp_set_enabled(struct net_bridge *br, unsigned long val,
1863 struct netlink_ext_ack *extack);
1864 void br_stp_enable_port(struct net_bridge_port *p);
1865 void br_stp_disable_port(struct net_bridge_port *p);
1866 bool br_stp_recalculate_bridge_id(struct net_bridge *br);
1867 void br_stp_change_bridge_id(struct net_bridge *br, const unsigned char *a);
1868 void br_stp_set_bridge_priority(struct net_bridge *br, u16 newprio);
1869 int br_stp_set_port_priority(struct net_bridge_port *p, unsigned long newprio);
1870 int br_stp_set_path_cost(struct net_bridge_port *p, unsigned long path_cost);
1871 ssize_t br_show_bridge_id(char *buf, const struct bridge_id *id);
1875 void br_stp_rcv(const struct stp_proto *proto, struct sk_buff *skb,
1876 struct net_device *dev);
1878 /* br_stp_timer.c */
1879 void br_stp_timer_init(struct net_bridge *br);
1880 void br_stp_port_timer_init(struct net_bridge_port *p);
1881 unsigned long br_timer_value(const struct timer_list *timer);
1884 #if IS_ENABLED(CONFIG_ATM_LANE)
1885 extern int (*br_fdb_test_addr_hook)(struct net_device *dev, unsigned char *addr);
1889 #if IS_ENABLED(CONFIG_BRIDGE_MRP)
1890 int br_mrp_parse(struct net_bridge *br, struct net_bridge_port *p,
1891 struct nlattr *attr, int cmd, struct netlink_ext_ack *extack);
1892 bool br_mrp_enabled(struct net_bridge *br);
1893 void br_mrp_port_del(struct net_bridge *br, struct net_bridge_port *p);
1894 int br_mrp_fill_info(struct sk_buff *skb, struct net_bridge *br);
1896 static inline int br_mrp_parse(struct net_bridge *br, struct net_bridge_port *p,
1897 struct nlattr *attr, int cmd,
1898 struct netlink_ext_ack *extack)
1903 static inline bool br_mrp_enabled(struct net_bridge *br)
1908 static inline void br_mrp_port_del(struct net_bridge *br,
1909 struct net_bridge_port *p)
1913 static inline int br_mrp_fill_info(struct sk_buff *skb, struct net_bridge *br)
1921 #if IS_ENABLED(CONFIG_BRIDGE_CFM)
1922 int br_cfm_parse(struct net_bridge *br, struct net_bridge_port *p,
1923 struct nlattr *attr, int cmd, struct netlink_ext_ack *extack);
1924 bool br_cfm_created(struct net_bridge *br);
1925 void br_cfm_port_del(struct net_bridge *br, struct net_bridge_port *p);
1926 int br_cfm_config_fill_info(struct sk_buff *skb, struct net_bridge *br);
1927 int br_cfm_status_fill_info(struct sk_buff *skb,
1928 struct net_bridge *br,
1930 int br_cfm_mep_count(struct net_bridge *br, u32 *count);
1931 int br_cfm_peer_mep_count(struct net_bridge *br, u32 *count);
1933 static inline int br_cfm_parse(struct net_bridge *br, struct net_bridge_port *p,
1934 struct nlattr *attr, int cmd,
1935 struct netlink_ext_ack *extack)
1940 static inline bool br_cfm_created(struct net_bridge *br)
1945 static inline void br_cfm_port_del(struct net_bridge *br,
1946 struct net_bridge_port *p)
1950 static inline int br_cfm_config_fill_info(struct sk_buff *skb, struct net_bridge *br)
1955 static inline int br_cfm_status_fill_info(struct sk_buff *skb,
1956 struct net_bridge *br,
1962 static inline int br_cfm_mep_count(struct net_bridge *br, u32 *count)
1968 static inline int br_cfm_peer_mep_count(struct net_bridge *br, u32 *count)
1976 extern struct rtnl_link_ops br_link_ops;
1977 int br_netlink_init(void);
1978 void br_netlink_fini(void);
1979 void br_ifinfo_notify(int event, const struct net_bridge *br,
1980 const struct net_bridge_port *port);
1981 void br_info_notify(int event, const struct net_bridge *br,
1982 const struct net_bridge_port *port, u32 filter);
1983 int br_setlink(struct net_device *dev, struct nlmsghdr *nlmsg, u16 flags,
1984 struct netlink_ext_ack *extack);
1985 int br_dellink(struct net_device *dev, struct nlmsghdr *nlmsg, u16 flags);
1986 int br_getlink(struct sk_buff *skb, u32 pid, u32 seq, struct net_device *dev,
1987 u32 filter_mask, int nlflags);
1988 int br_process_vlan_info(struct net_bridge *br,
1989 struct net_bridge_port *p, int cmd,
1990 struct bridge_vlan_info *vinfo_curr,
1991 struct bridge_vlan_info **vinfo_last,
1993 struct netlink_ext_ack *extack);
1997 extern const struct sysfs_ops brport_sysfs_ops;
1998 int br_sysfs_addif(struct net_bridge_port *p);
1999 int br_sysfs_renameif(struct net_bridge_port *p);
2002 int br_sysfs_addbr(struct net_device *dev);
2003 void br_sysfs_delbr(struct net_device *dev);
2007 static inline int br_sysfs_addif(struct net_bridge_port *p) { return 0; }
2008 static inline int br_sysfs_renameif(struct net_bridge_port *p) { return 0; }
2009 static inline int br_sysfs_addbr(struct net_device *dev) { return 0; }
2010 static inline void br_sysfs_delbr(struct net_device *dev) { return; }
2011 #endif /* CONFIG_SYSFS */
2013 /* br_switchdev.c */
2014 #ifdef CONFIG_NET_SWITCHDEV
2015 int br_switchdev_port_offload(struct net_bridge_port *p,
2016 struct net_device *dev, const void *ctx,
2017 struct notifier_block *atomic_nb,
2018 struct notifier_block *blocking_nb,
2019 bool tx_fwd_offload,
2020 struct netlink_ext_ack *extack);
2022 void br_switchdev_port_unoffload(struct net_bridge_port *p, const void *ctx,
2023 struct notifier_block *atomic_nb,
2024 struct notifier_block *blocking_nb);
2026 bool br_switchdev_frame_uses_tx_fwd_offload(struct sk_buff *skb);
2028 void br_switchdev_frame_set_offload_fwd_mark(struct sk_buff *skb);
2030 void nbp_switchdev_frame_mark_tx_fwd_offload(const struct net_bridge_port *p,
2031 struct sk_buff *skb);
2032 void nbp_switchdev_frame_mark_tx_fwd_to_hwdom(const struct net_bridge_port *p,
2033 struct sk_buff *skb);
2034 void nbp_switchdev_frame_mark(const struct net_bridge_port *p,
2035 struct sk_buff *skb);
2036 bool nbp_switchdev_allowed_egress(const struct net_bridge_port *p,
2037 const struct sk_buff *skb);
2038 int br_switchdev_set_port_flag(struct net_bridge_port *p,
2039 unsigned long flags,
2041 struct netlink_ext_ack *extack);
2042 void br_switchdev_fdb_notify(struct net_bridge *br,
2043 const struct net_bridge_fdb_entry *fdb, int type);
2044 void br_switchdev_mdb_notify(struct net_device *dev,
2045 struct net_bridge_mdb_entry *mp,
2046 struct net_bridge_port_group *pg,
2048 int br_switchdev_port_vlan_add(struct net_device *dev, u16 vid, u16 flags,
2049 bool changed, struct netlink_ext_ack *extack);
2050 int br_switchdev_port_vlan_del(struct net_device *dev, u16 vid);
2051 void br_switchdev_init(struct net_bridge *br);
2053 static inline void br_switchdev_frame_unmark(struct sk_buff *skb)
2055 skb->offload_fwd_mark = 0;
2059 br_switchdev_port_offload(struct net_bridge_port *p,
2060 struct net_device *dev, const void *ctx,
2061 struct notifier_block *atomic_nb,
2062 struct notifier_block *blocking_nb,
2063 bool tx_fwd_offload,
2064 struct netlink_ext_ack *extack)
2070 br_switchdev_port_unoffload(struct net_bridge_port *p, const void *ctx,
2071 struct notifier_block *atomic_nb,
2072 struct notifier_block *blocking_nb)
2076 static inline bool br_switchdev_frame_uses_tx_fwd_offload(struct sk_buff *skb)
2081 static inline void br_switchdev_frame_set_offload_fwd_mark(struct sk_buff *skb)
2086 nbp_switchdev_frame_mark_tx_fwd_offload(const struct net_bridge_port *p,
2087 struct sk_buff *skb)
2092 nbp_switchdev_frame_mark_tx_fwd_to_hwdom(const struct net_bridge_port *p,
2093 struct sk_buff *skb)
2097 static inline void nbp_switchdev_frame_mark(const struct net_bridge_port *p,
2098 struct sk_buff *skb)
2102 static inline bool nbp_switchdev_allowed_egress(const struct net_bridge_port *p,
2103 const struct sk_buff *skb)
2108 static inline int br_switchdev_set_port_flag(struct net_bridge_port *p,
2109 unsigned long flags,
2111 struct netlink_ext_ack *extack)
2116 static inline int br_switchdev_port_vlan_add(struct net_device *dev, u16 vid,
2117 u16 flags, bool changed,
2118 struct netlink_ext_ack *extack)
2123 static inline int br_switchdev_port_vlan_del(struct net_device *dev, u16 vid)
2129 br_switchdev_fdb_notify(struct net_bridge *br,
2130 const struct net_bridge_fdb_entry *fdb, int type)
2134 static inline void br_switchdev_mdb_notify(struct net_device *dev,
2135 struct net_bridge_mdb_entry *mp,
2136 struct net_bridge_port_group *pg,
2141 static inline void br_switchdev_frame_unmark(struct sk_buff *skb)
2145 static inline void br_switchdev_init(struct net_bridge *br)
2149 #endif /* CONFIG_NET_SWITCHDEV */
2151 /* br_arp_nd_proxy.c */
2152 void br_recalculate_neigh_suppress_enabled(struct net_bridge *br);
2153 void br_do_proxy_suppress_arp(struct sk_buff *skb, struct net_bridge *br,
2154 u16 vid, struct net_bridge_port *p);
2155 void br_do_suppress_nd(struct sk_buff *skb, struct net_bridge *br,
2156 u16 vid, struct net_bridge_port *p, struct nd_msg *msg);
2157 struct nd_msg *br_is_nd_neigh_msg(struct sk_buff *skb, struct nd_msg *m);