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;
96 struct br_mdb_src_entry {
100 struct br_mdb_config {
101 struct net_bridge *br;
102 struct net_bridge_port *p;
103 struct br_mdb_entry *entry;
108 struct br_mdb_src_entry *src_entries;
114 /* net_bridge_mcast_port must be always defined due to forwarding stubs */
115 struct net_bridge_mcast_port {
116 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
117 struct net_bridge_port *port;
118 struct net_bridge_vlan *vlan;
120 struct bridge_mcast_own_query ip4_own_query;
121 struct timer_list ip4_mc_router_timer;
122 struct hlist_node ip4_rlist;
123 #if IS_ENABLED(CONFIG_IPV6)
124 struct bridge_mcast_own_query ip6_own_query;
125 struct timer_list ip6_mc_router_timer;
126 struct hlist_node ip6_rlist;
127 #endif /* IS_ENABLED(CONFIG_IPV6) */
128 unsigned char multicast_router;
131 #endif /* CONFIG_BRIDGE_IGMP_SNOOPING */
134 /* net_bridge_mcast must be always defined due to forwarding stubs */
135 struct net_bridge_mcast {
136 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
137 struct net_bridge *br;
138 struct net_bridge_vlan *vlan;
140 u32 multicast_last_member_count;
141 u32 multicast_startup_query_count;
143 u8 multicast_querier;
144 u8 multicast_igmp_version;
146 #if IS_ENABLED(CONFIG_IPV6)
147 u8 multicast_mld_version;
149 unsigned long multicast_last_member_interval;
150 unsigned long multicast_membership_interval;
151 unsigned long multicast_querier_interval;
152 unsigned long multicast_query_interval;
153 unsigned long multicast_query_response_interval;
154 unsigned long multicast_startup_query_interval;
155 struct hlist_head ip4_mc_router_list;
156 struct timer_list ip4_mc_router_timer;
157 struct bridge_mcast_other_query ip4_other_query;
158 struct bridge_mcast_own_query ip4_own_query;
159 struct bridge_mcast_querier ip4_querier;
160 #if IS_ENABLED(CONFIG_IPV6)
161 struct hlist_head ip6_mc_router_list;
162 struct timer_list ip6_mc_router_timer;
163 struct bridge_mcast_other_query ip6_other_query;
164 struct bridge_mcast_own_query ip6_own_query;
165 struct bridge_mcast_querier ip6_querier;
166 #endif /* IS_ENABLED(CONFIG_IPV6) */
167 #endif /* CONFIG_BRIDGE_IGMP_SNOOPING */
170 struct br_tunnel_info {
172 struct metadata_dst __rcu *tunnel_dst;
175 /* private vlan flags */
177 BR_VLFLAG_PER_PORT_STATS = BIT(0),
178 BR_VLFLAG_ADDED_BY_SWITCHDEV = BIT(1),
179 BR_VLFLAG_MCAST_ENABLED = BIT(2),
180 BR_VLFLAG_GLOBAL_MCAST_ENABLED = BIT(3),
184 * struct net_bridge_vlan - per-vlan entry
186 * @vnode: rhashtable member
188 * @flags: bridge vlan flags
189 * @priv_flags: private (in-kernel) bridge vlan flags
190 * @state: STP state (e.g. blocking, learning, forwarding)
191 * @stats: per-cpu VLAN statistics
192 * @br: if MASTER flag set, this points to a bridge struct
193 * @port: if MASTER flag unset, this points to a port struct
194 * @refcnt: if MASTER flag set, this is bumped for each port referencing it
195 * @brvlan: if MASTER flag unset, this points to the global per-VLAN context
196 * for this VLAN entry
197 * @br_mcast_ctx: if MASTER flag set, this is the global vlan multicast context
198 * @port_mcast_ctx: if MASTER flag unset, this is the per-port/vlan multicast
200 * @msti: if MASTER flag set, this holds the VLANs MST instance
201 * @vlist: sorted list of VLAN entries
202 * @rcu: used for entry destruction
204 * This structure is shared between the global per-VLAN entries contained in
205 * the bridge rhashtable and the local per-port per-VLAN entries contained in
206 * the port's rhashtable. The union entries should be interpreted depending on
207 * the entry flags that are set.
209 struct net_bridge_vlan {
210 struct rhash_head vnode;
211 struct rhash_head tnode;
216 struct pcpu_sw_netstats __percpu *stats;
218 struct net_bridge *br;
219 struct net_bridge_port *port;
223 struct net_bridge_vlan *brvlan;
226 struct br_tunnel_info tinfo;
229 struct net_bridge_mcast br_mcast_ctx;
230 struct net_bridge_mcast_port port_mcast_ctx;
235 struct list_head vlist;
241 * struct net_bridge_vlan_group
243 * @vlan_hash: VLAN entry rhashtable
244 * @vlan_list: sorted VLAN entry list
245 * @num_vlans: number of total VLAN entries
246 * @pvid: PVID VLAN id
247 * @pvid_state: PVID's STP state (e.g. forwarding, learning, blocking)
249 * IMPORTANT: Be careful when checking if there're VLAN entries using list
250 * primitives because the bridge can have entries in its list which
251 * are just for global context but not for filtering, i.e. they have
252 * the master flag set but not the brentry flag. If you have to check
253 * if there're "real" entries in the bridge please test @num_vlans
255 struct net_bridge_vlan_group {
256 struct rhashtable vlan_hash;
257 struct rhashtable tunnel_hash;
258 struct list_head vlan_list;
264 /* bridge fdb flags */
269 BR_FDB_ADDED_BY_USER,
270 BR_FDB_ADDED_BY_EXT_LEARN,
273 BR_FDB_NOTIFY_INACTIVE,
277 struct net_bridge_fdb_key {
282 struct net_bridge_fdb_entry {
283 struct rhash_head rhnode;
284 struct net_bridge_port *dst;
286 struct net_bridge_fdb_key key;
287 struct hlist_node fdb_node;
290 /* write-heavy members should not affect lookups */
291 unsigned long updated ____cacheline_aligned_in_smp;
297 struct net_bridge_fdb_flush_desc {
299 unsigned long flags_mask;
304 #define MDB_PG_FLAGS_PERMANENT BIT(0)
305 #define MDB_PG_FLAGS_OFFLOAD BIT(1)
306 #define MDB_PG_FLAGS_FAST_LEAVE BIT(2)
307 #define MDB_PG_FLAGS_STAR_EXCL BIT(3)
308 #define MDB_PG_FLAGS_BLOCKED BIT(4)
310 #define PG_SRC_ENT_LIMIT 32
312 #define BR_SGRP_F_DELETE BIT(0)
313 #define BR_SGRP_F_SEND BIT(1)
314 #define BR_SGRP_F_INSTALLED BIT(2)
315 #define BR_SGRP_F_USER_ADDED BIT(3)
317 struct net_bridge_mcast_gc {
318 struct hlist_node gc_node;
319 void (*destroy)(struct net_bridge_mcast_gc *gc);
322 struct net_bridge_group_src {
323 struct hlist_node node;
326 struct net_bridge_port_group *pg;
328 u8 src_query_rexmit_cnt;
329 struct timer_list timer;
331 struct net_bridge *br;
332 struct net_bridge_mcast_gc mcast_gc;
336 struct net_bridge_port_group_sg_key {
337 struct net_bridge_port *port;
341 struct net_bridge_port_group {
342 struct net_bridge_port_group __rcu *next;
343 struct net_bridge_port_group_sg_key key;
344 unsigned char eth_addr[ETH_ALEN] __aligned(2);
346 unsigned char filter_mode;
347 unsigned char grp_query_rexmit_cnt;
348 unsigned char rt_protocol;
350 struct hlist_head src_list;
351 unsigned int src_ents;
352 struct timer_list timer;
353 struct timer_list rexmit_timer;
354 struct hlist_node mglist;
355 struct rb_root eht_set_tree;
356 struct rb_root eht_host_tree;
358 struct rhash_head rhnode;
359 struct net_bridge_mcast_gc mcast_gc;
363 struct net_bridge_mdb_entry {
364 struct rhash_head rhnode;
365 struct net_bridge *br;
366 struct net_bridge_port_group __rcu *ports;
370 struct timer_list timer;
371 struct hlist_node mdb_node;
373 struct net_bridge_mcast_gc mcast_gc;
377 struct net_bridge_port {
378 struct net_bridge *br;
379 struct net_device *dev;
380 netdevice_tracker dev_tracker;
381 struct list_head list;
384 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
385 struct net_bridge_vlan_group __rcu *vlgrp;
387 struct net_bridge_port __rcu *backup_port;
393 unsigned char topology_change_ack;
394 unsigned char config_pending;
396 port_id designated_port;
397 bridge_id designated_root;
398 bridge_id designated_bridge;
401 unsigned long designated_age;
403 struct timer_list forward_delay_timer;
404 struct timer_list hold_timer;
405 struct timer_list message_age_timer;
409 struct net_bridge_mcast_port multicast_ctx;
411 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
412 struct bridge_mcast_stats __percpu *mcast_stats;
414 u32 multicast_eht_hosts_limit;
415 u32 multicast_eht_hosts_cnt;
416 struct hlist_head mglist;
420 char sysfs_name[IFNAMSIZ];
423 #ifdef CONFIG_NET_POLL_CONTROLLER
426 #ifdef CONFIG_NET_SWITCHDEV
427 /* Identifier used to group ports that share the same switchdev
432 struct netdev_phys_item_id ppid;
435 u16 backup_redirected_cnt;
437 struct bridge_stp_xstats stp_xstats;
440 #define kobj_to_brport(obj) container_of(obj, struct net_bridge_port, kobj)
442 #define br_auto_port(p) ((p)->flags & BR_AUTO_MASK)
443 #define br_promisc_port(p) ((p)->flags & BR_PROMISC)
445 static inline struct net_bridge_port *br_port_get_rcu(const struct net_device *dev)
447 return rcu_dereference(dev->rx_handler_data);
450 static inline struct net_bridge_port *br_port_get_rtnl(const struct net_device *dev)
452 return netif_is_bridge_port(dev) ?
453 rtnl_dereference(dev->rx_handler_data) : NULL;
456 static inline struct net_bridge_port *br_port_get_rtnl_rcu(const struct net_device *dev)
458 return netif_is_bridge_port(dev) ?
459 rcu_dereference_rtnl(dev->rx_handler_data) : NULL;
462 enum net_bridge_opts {
464 BROPT_VLAN_STATS_ENABLED,
465 BROPT_NF_CALL_IPTABLES,
466 BROPT_NF_CALL_IP6TABLES,
467 BROPT_NF_CALL_ARPTABLES,
468 BROPT_GROUP_ADDR_SET,
469 BROPT_MULTICAST_ENABLED,
470 BROPT_MULTICAST_QUERY_USE_IFADDR,
471 BROPT_MULTICAST_STATS_ENABLED,
473 BROPT_NEIGH_SUPPRESS_ENABLED,
474 BROPT_MTU_SET_BY_USER,
475 BROPT_VLAN_STATS_PER_PORT,
477 BROPT_VLAN_BRIDGE_BINDING,
478 BROPT_MCAST_VLAN_SNOOPING_ENABLED,
484 spinlock_t hash_lock;
485 struct hlist_head frame_type_list;
486 struct net_device *dev;
487 unsigned long options;
488 /* These fields are accessed on each packet */
489 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
492 struct net_bridge_vlan_group __rcu *vlgrp;
495 struct rhashtable fdb_hash_tbl;
496 struct list_head port_list;
497 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
499 struct rtable fake_rtable;
500 struct rt6_info fake_rt6_info;
504 u16 group_fwd_mask_required;
507 bridge_id designated_root;
509 unsigned char topology_change;
510 unsigned char topology_change_detected;
512 unsigned long max_age;
513 unsigned long hello_time;
514 unsigned long forward_delay;
515 unsigned long ageing_time;
516 unsigned long bridge_max_age;
517 unsigned long bridge_hello_time;
518 unsigned long bridge_forward_delay;
519 unsigned long bridge_ageing_time;
522 u8 group_addr[ETH_ALEN];
525 BR_NO_STP, /* no spanning tree */
526 BR_KERNEL_STP, /* old STP in kernel */
527 BR_USER_STP, /* new RSTP in userspace */
530 struct net_bridge_mcast multicast_ctx;
532 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
533 struct bridge_mcast_stats __percpu *mcast_stats;
537 spinlock_t multicast_lock;
539 struct rhashtable mdb_hash_tbl;
540 struct rhashtable sg_port_tbl;
542 struct hlist_head mcast_gc_list;
543 struct hlist_head mdb_list;
545 struct work_struct mcast_gc_work;
548 struct timer_list hello_timer;
549 struct timer_list tcn_timer;
550 struct timer_list topology_change_timer;
551 struct delayed_work gc_work;
552 struct kobject *ifobj;
555 #ifdef CONFIG_NET_SWITCHDEV
556 /* Counter used to make sure that hardware domains get unique
557 * identifiers in case a bridge spans multiple switchdev instances.
560 /* Bit mask of hardware domain numbers in use */
561 unsigned long busy_hwdoms;
563 struct hlist_head fdb_list;
565 #if IS_ENABLED(CONFIG_BRIDGE_MRP)
566 struct hlist_head mrp_list;
568 #if IS_ENABLED(CONFIG_BRIDGE_CFM)
569 struct hlist_head mep_list;
573 struct br_input_skb_cb {
574 struct net_device *brdev;
577 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
581 u8 proxyarp_replied:1;
582 u8 src_port_isolated:1;
583 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
586 #ifdef CONFIG_NETFILTER_FAMILY_BRIDGE
587 u8 br_netfilter_broute:1;
590 #ifdef CONFIG_NET_SWITCHDEV
591 /* Set if TX data plane offloading is used towards at least one
595 /* The switchdev hardware domain from which this packet was received.
596 * If skb->offload_fwd_mark was set, then this packet was already
597 * forwarded by hardware to the other ports in the source hardware
598 * domain, otherwise it wasn't.
601 /* Bit mask of hardware domains towards this packet has already been
602 * transmitted using the TX data plane offload.
604 unsigned long fwd_hwdoms;
608 #define BR_INPUT_SKB_CB(__skb) ((struct br_input_skb_cb *)(__skb)->cb)
610 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
611 # define BR_INPUT_SKB_CB_MROUTERS_ONLY(__skb) (BR_INPUT_SKB_CB(__skb)->mrouters_only)
613 # define BR_INPUT_SKB_CB_MROUTERS_ONLY(__skb) (0)
616 #define br_printk(level, br, format, args...) \
617 printk(level "%s: " format, (br)->dev->name, ##args)
619 #define br_err(__br, format, args...) \
620 br_printk(KERN_ERR, __br, format, ##args)
621 #define br_warn(__br, format, args...) \
622 br_printk(KERN_WARNING, __br, format, ##args)
623 #define br_notice(__br, format, args...) \
624 br_printk(KERN_NOTICE, __br, format, ##args)
625 #define br_info(__br, format, args...) \
626 br_printk(KERN_INFO, __br, format, ##args)
628 #define br_debug(br, format, args...) \
629 pr_debug("%s: " format, (br)->dev->name, ##args)
631 /* called under bridge lock */
632 static inline int br_is_root_bridge(const struct net_bridge *br)
634 return !memcmp(&br->bridge_id, &br->designated_root, 8);
637 /* check if a VLAN entry is global */
638 static inline bool br_vlan_is_master(const struct net_bridge_vlan *v)
640 return v->flags & BRIDGE_VLAN_INFO_MASTER;
643 /* check if a VLAN entry is used by the bridge */
644 static inline bool br_vlan_is_brentry(const struct net_bridge_vlan *v)
646 return v->flags & BRIDGE_VLAN_INFO_BRENTRY;
649 /* check if we should use the vlan entry, returns false if it's only context */
650 static inline bool br_vlan_should_use(const struct net_bridge_vlan *v)
652 if (br_vlan_is_master(v)) {
653 if (br_vlan_is_brentry(v))
662 static inline bool nbp_state_should_learn(const struct net_bridge_port *p)
664 return p->state == BR_STATE_LEARNING || p->state == BR_STATE_FORWARDING;
667 static inline bool br_vlan_valid_id(u16 vid, struct netlink_ext_ack *extack)
669 bool ret = vid > 0 && vid < VLAN_VID_MASK;
672 NL_SET_ERR_MSG_MOD(extack, "Vlan id is invalid");
677 static inline bool br_vlan_valid_range(const struct bridge_vlan_info *cur,
678 const struct bridge_vlan_info *last,
679 struct netlink_ext_ack *extack)
681 /* pvid flag is not allowed in ranges */
682 if (cur->flags & BRIDGE_VLAN_INFO_PVID) {
683 NL_SET_ERR_MSG_MOD(extack, "Pvid isn't allowed in a range");
687 /* when cur is the range end, check if:
688 * - it has range start flag
689 * - range ids are invalid (end is equal to or before start)
692 if (cur->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
693 NL_SET_ERR_MSG_MOD(extack, "Found a new vlan range start while processing one");
695 } else if (!(cur->flags & BRIDGE_VLAN_INFO_RANGE_END)) {
696 NL_SET_ERR_MSG_MOD(extack, "Vlan range end flag is missing");
698 } else if (cur->vid <= last->vid) {
699 NL_SET_ERR_MSG_MOD(extack, "End vlan id is less than or equal to start vlan id");
704 /* check for required range flags */
705 if (!(cur->flags & (BRIDGE_VLAN_INFO_RANGE_BEGIN |
706 BRIDGE_VLAN_INFO_RANGE_END))) {
707 NL_SET_ERR_MSG_MOD(extack, "Both vlan range flags are missing");
714 static inline u8 br_vlan_multicast_router(const struct net_bridge_vlan *v)
716 u8 mcast_router = MDB_RTR_TYPE_DISABLED;
718 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
719 if (!br_vlan_is_master(v))
720 mcast_router = v->port_mcast_ctx.multicast_router;
722 mcast_router = v->br_mcast_ctx.multicast_router;
728 static inline int br_afspec_cmd_to_rtm(int cmd)
740 static inline int br_opt_get(const struct net_bridge *br,
741 enum net_bridge_opts opt)
743 return test_bit(opt, &br->options);
746 int br_boolopt_toggle(struct net_bridge *br, enum br_boolopt_id opt, bool on,
747 struct netlink_ext_ack *extack);
748 int br_boolopt_get(const struct net_bridge *br, enum br_boolopt_id opt);
749 int br_boolopt_multi_toggle(struct net_bridge *br,
750 struct br_boolopt_multi *bm,
751 struct netlink_ext_ack *extack);
752 void br_boolopt_multi_get(const struct net_bridge *br,
753 struct br_boolopt_multi *bm);
754 void br_opt_toggle(struct net_bridge *br, enum net_bridge_opts opt, bool on);
757 void br_dev_setup(struct net_device *dev);
758 void br_dev_delete(struct net_device *dev, struct list_head *list);
759 netdev_tx_t br_dev_xmit(struct sk_buff *skb, struct net_device *dev);
760 #ifdef CONFIG_NET_POLL_CONTROLLER
761 static inline void br_netpoll_send_skb(const struct net_bridge_port *p,
764 netpoll_send_skb(p->np, skb);
767 int br_netpoll_enable(struct net_bridge_port *p);
768 void br_netpoll_disable(struct net_bridge_port *p);
770 static inline void br_netpoll_send_skb(const struct net_bridge_port *p,
775 static inline int br_netpoll_enable(struct net_bridge_port *p)
780 static inline void br_netpoll_disable(struct net_bridge_port *p)
786 #define FDB_FLUSH_IGNORED_NDM_FLAGS (NTF_MASTER | NTF_SELF)
787 #define FDB_FLUSH_ALLOWED_NDM_STATES (NUD_PERMANENT | NUD_NOARP)
788 #define FDB_FLUSH_ALLOWED_NDM_FLAGS (NTF_USE | NTF_EXT_LEARNED | \
789 NTF_STICKY | NTF_OFFLOADED)
791 int br_fdb_init(void);
792 void br_fdb_fini(void);
793 int br_fdb_hash_init(struct net_bridge *br);
794 void br_fdb_hash_fini(struct net_bridge *br);
795 void br_fdb_flush(struct net_bridge *br,
796 const struct net_bridge_fdb_flush_desc *desc);
797 void br_fdb_find_delete_local(struct net_bridge *br,
798 const struct net_bridge_port *p,
799 const unsigned char *addr, u16 vid);
800 void br_fdb_changeaddr(struct net_bridge_port *p, const unsigned char *newaddr);
801 void br_fdb_change_mac_address(struct net_bridge *br, const u8 *newaddr);
802 void br_fdb_cleanup(struct work_struct *work);
803 void br_fdb_delete_by_port(struct net_bridge *br,
804 const struct net_bridge_port *p, u16 vid, int do_all);
805 struct net_bridge_fdb_entry *br_fdb_find_rcu(struct net_bridge *br,
806 const unsigned char *addr,
808 int br_fdb_test_addr(struct net_device *dev, unsigned char *addr);
809 int br_fdb_fillbuf(struct net_bridge *br, void *buf, unsigned long count,
811 int br_fdb_add_local(struct net_bridge *br, struct net_bridge_port *source,
812 const unsigned char *addr, u16 vid);
813 void br_fdb_update(struct net_bridge *br, struct net_bridge_port *source,
814 const unsigned char *addr, u16 vid, unsigned long flags);
816 int br_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
817 struct net_device *dev, const unsigned char *addr, u16 vid,
818 struct netlink_ext_ack *extack);
819 int br_fdb_delete_bulk(struct ndmsg *ndm, struct nlattr *tb[],
820 struct net_device *dev, u16 vid,
821 struct netlink_ext_ack *extack);
822 int br_fdb_add(struct ndmsg *nlh, struct nlattr *tb[], struct net_device *dev,
823 const unsigned char *addr, u16 vid, u16 nlh_flags,
824 struct netlink_ext_ack *extack);
825 int br_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
826 struct net_device *dev, struct net_device *fdev, int *idx);
827 int br_fdb_get(struct sk_buff *skb, struct nlattr *tb[], struct net_device *dev,
828 const unsigned char *addr, u16 vid, u32 portid, u32 seq,
829 struct netlink_ext_ack *extack);
830 int br_fdb_sync_static(struct net_bridge *br, struct net_bridge_port *p);
831 void br_fdb_unsync_static(struct net_bridge *br, struct net_bridge_port *p);
832 int br_fdb_external_learn_add(struct net_bridge *br, struct net_bridge_port *p,
833 const unsigned char *addr, u16 vid,
834 bool locked, bool swdev_notify);
835 int br_fdb_external_learn_del(struct net_bridge *br, struct net_bridge_port *p,
836 const unsigned char *addr, u16 vid,
838 void br_fdb_offloaded_set(struct net_bridge *br, struct net_bridge_port *p,
839 const unsigned char *addr, u16 vid, bool offloaded);
847 int br_dev_queue_push_xmit(struct net *net, struct sock *sk, struct sk_buff *skb);
848 void br_forward(const struct net_bridge_port *to, struct sk_buff *skb,
849 bool local_rcv, bool local_orig);
850 int br_forward_finish(struct net *net, struct sock *sk, struct sk_buff *skb);
851 void br_flood(struct net_bridge *br, struct sk_buff *skb,
852 enum br_pkt_type pkt_type, bool local_rcv, bool local_orig);
854 /* return true if both source port and dest port are isolated */
855 static inline bool br_skb_isolated(const struct net_bridge_port *to,
856 const struct sk_buff *skb)
858 return BR_INPUT_SKB_CB(skb)->src_port_isolated &&
859 (to->flags & BR_ISOLATED);
863 void br_port_carrier_check(struct net_bridge_port *p, bool *notified);
864 int br_add_bridge(struct net *net, const char *name);
865 int br_del_bridge(struct net *net, const char *name);
866 int br_add_if(struct net_bridge *br, struct net_device *dev,
867 struct netlink_ext_ack *extack);
868 int br_del_if(struct net_bridge *br, struct net_device *dev);
869 void br_mtu_auto_adjust(struct net_bridge *br);
870 netdev_features_t br_features_recompute(struct net_bridge *br,
871 netdev_features_t features);
872 void br_port_flags_change(struct net_bridge_port *port, unsigned long mask);
873 void br_manage_promisc(struct net_bridge *br);
874 int nbp_backup_change(struct net_bridge_port *p, struct net_device *backup_dev);
877 int br_handle_frame_finish(struct net *net, struct sock *sk, struct sk_buff *skb);
878 rx_handler_func_t *br_get_rx_handler(const struct net_device *dev);
880 struct br_frame_type {
882 int (*frame_handler)(struct net_bridge_port *port,
883 struct sk_buff *skb);
884 struct hlist_node list;
887 void br_add_frame(struct net_bridge *br, struct br_frame_type *ft);
888 void br_del_frame(struct net_bridge *br, struct br_frame_type *ft);
890 static inline bool br_rx_handler_check_rcu(const struct net_device *dev)
892 return rcu_dereference(dev->rx_handler) == br_get_rx_handler(dev);
895 static inline bool br_rx_handler_check_rtnl(const struct net_device *dev)
897 return rcu_dereference_rtnl(dev->rx_handler) == br_get_rx_handler(dev);
900 static inline struct net_bridge_port *br_port_get_check_rcu(const struct net_device *dev)
902 return br_rx_handler_check_rcu(dev) ? br_port_get_rcu(dev) : NULL;
905 static inline struct net_bridge_port *
906 br_port_get_check_rtnl(const struct net_device *dev)
908 return br_rx_handler_check_rtnl(dev) ? br_port_get_rtnl_rcu(dev) : NULL;
912 int br_dev_siocdevprivate(struct net_device *dev, struct ifreq *rq,
913 void __user *data, int cmd);
914 int br_ioctl_stub(struct net *net, struct net_bridge *br, unsigned int cmd,
915 struct ifreq *ifr, void __user *uarg);
918 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
919 int br_multicast_rcv(struct net_bridge_mcast **brmctx,
920 struct net_bridge_mcast_port **pmctx,
921 struct net_bridge_vlan *vlan,
922 struct sk_buff *skb, u16 vid);
923 struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge_mcast *brmctx,
924 struct sk_buff *skb, u16 vid);
925 int br_multicast_add_port(struct net_bridge_port *port);
926 void br_multicast_del_port(struct net_bridge_port *port);
927 void br_multicast_enable_port(struct net_bridge_port *port);
928 void br_multicast_disable_port(struct net_bridge_port *port);
929 void br_multicast_init(struct net_bridge *br);
930 void br_multicast_join_snoopers(struct net_bridge *br);
931 void br_multicast_leave_snoopers(struct net_bridge *br);
932 void br_multicast_open(struct net_bridge *br);
933 void br_multicast_stop(struct net_bridge *br);
934 void br_multicast_dev_del(struct net_bridge *br);
935 void br_multicast_flood(struct net_bridge_mdb_entry *mdst, struct sk_buff *skb,
936 struct net_bridge_mcast *brmctx,
937 bool local_rcv, bool local_orig);
938 int br_multicast_set_router(struct net_bridge_mcast *brmctx, unsigned long val);
939 int br_multicast_set_port_router(struct net_bridge_mcast_port *pmctx,
941 int br_multicast_set_vlan_router(struct net_bridge_vlan *v, u8 mcast_router);
942 int br_multicast_toggle(struct net_bridge *br, unsigned long val,
943 struct netlink_ext_ack *extack);
944 int br_multicast_set_querier(struct net_bridge_mcast *brmctx, unsigned long val);
945 int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val);
946 int br_multicast_set_igmp_version(struct net_bridge_mcast *brmctx,
948 #if IS_ENABLED(CONFIG_IPV6)
949 int br_multicast_set_mld_version(struct net_bridge_mcast *brmctx,
952 struct net_bridge_mdb_entry *
953 br_mdb_ip_get(struct net_bridge *br, struct br_ip *dst);
954 struct net_bridge_mdb_entry *
955 br_multicast_new_group(struct net_bridge *br, struct br_ip *group);
956 struct net_bridge_port_group *
957 br_multicast_new_port_group(struct net_bridge_port *port,
958 const struct br_ip *group,
959 struct net_bridge_port_group __rcu *next,
960 unsigned char flags, const unsigned char *src,
961 u8 filter_mode, u8 rt_protocol,
962 struct netlink_ext_ack *extack);
963 void br_multicast_del_port_group(struct net_bridge_port_group *p);
964 int br_mdb_hash_init(struct net_bridge *br);
965 void br_mdb_hash_fini(struct net_bridge *br);
966 void br_mdb_notify(struct net_device *dev, struct net_bridge_mdb_entry *mp,
967 struct net_bridge_port_group *pg, int type);
968 void br_rtr_notify(struct net_device *dev, struct net_bridge_mcast_port *pmctx,
970 void br_multicast_del_pg(struct net_bridge_mdb_entry *mp,
971 struct net_bridge_port_group *pg,
972 struct net_bridge_port_group __rcu **pp);
973 void br_multicast_count(struct net_bridge *br,
974 const struct net_bridge_port *p,
975 const struct sk_buff *skb, u8 type, u8 dir);
976 int br_multicast_init_stats(struct net_bridge *br);
977 void br_multicast_uninit_stats(struct net_bridge *br);
978 void br_multicast_get_stats(const struct net_bridge *br,
979 const struct net_bridge_port *p,
980 struct br_mcast_stats *dest);
981 u32 br_multicast_ngroups_get(const struct net_bridge_mcast_port *pmctx);
982 void br_multicast_ngroups_set_max(struct net_bridge_mcast_port *pmctx, u32 max);
983 u32 br_multicast_ngroups_get_max(const struct net_bridge_mcast_port *pmctx);
984 void br_mdb_init(void);
985 void br_mdb_uninit(void);
986 void br_multicast_host_join(const struct net_bridge_mcast *brmctx,
987 struct net_bridge_mdb_entry *mp, bool notify);
988 void br_multicast_host_leave(struct net_bridge_mdb_entry *mp, bool notify);
989 void br_multicast_star_g_handle_mode(struct net_bridge_port_group *pg,
991 void br_multicast_sg_add_exclude_ports(struct net_bridge_mdb_entry *star_mp,
992 struct net_bridge_port_group *sg);
993 struct net_bridge_group_src *
994 br_multicast_find_group_src(struct net_bridge_port_group *pg, struct br_ip *ip);
995 struct net_bridge_group_src *
996 br_multicast_new_group_src(struct net_bridge_port_group *pg,
997 struct br_ip *src_ip);
998 void __br_multicast_del_group_src(struct net_bridge_group_src *src);
999 void br_multicast_del_group_src(struct net_bridge_group_src *src,
1001 void br_multicast_ctx_init(struct net_bridge *br,
1002 struct net_bridge_vlan *vlan,
1003 struct net_bridge_mcast *brmctx);
1004 void br_multicast_ctx_deinit(struct net_bridge_mcast *brmctx);
1005 void br_multicast_port_ctx_init(struct net_bridge_port *port,
1006 struct net_bridge_vlan *vlan,
1007 struct net_bridge_mcast_port *pmctx);
1008 void br_multicast_port_ctx_deinit(struct net_bridge_mcast_port *pmctx);
1009 void br_multicast_toggle_one_vlan(struct net_bridge_vlan *vlan, bool on);
1010 int br_multicast_toggle_vlan_snooping(struct net_bridge *br, bool on,
1011 struct netlink_ext_ack *extack);
1012 bool br_multicast_toggle_global_vlan(struct net_bridge_vlan *vlan, bool on);
1014 int br_rports_fill_info(struct sk_buff *skb,
1015 const struct net_bridge_mcast *brmctx);
1016 int br_multicast_dump_querier_state(struct sk_buff *skb,
1017 const struct net_bridge_mcast *brmctx,
1019 size_t br_multicast_querier_state_size(void);
1020 size_t br_rports_size(const struct net_bridge_mcast *brmctx);
1021 void br_multicast_set_query_intvl(struct net_bridge_mcast *brmctx,
1023 void br_multicast_set_startup_query_intvl(struct net_bridge_mcast *brmctx,
1026 static inline bool br_group_is_l2(const struct br_ip *group)
1028 return group->proto == 0;
1031 #define mlock_dereference(X, br) \
1032 rcu_dereference_protected(X, lockdep_is_held(&br->multicast_lock))
1034 static inline struct hlist_node *
1035 br_multicast_get_first_rport_node(struct net_bridge_mcast *brmctx,
1036 struct sk_buff *skb)
1038 #if IS_ENABLED(CONFIG_IPV6)
1039 if (skb->protocol == htons(ETH_P_IPV6))
1040 return rcu_dereference(hlist_first_rcu(&brmctx->ip6_mc_router_list));
1042 return rcu_dereference(hlist_first_rcu(&brmctx->ip4_mc_router_list));
1045 static inline struct net_bridge_port *
1046 br_multicast_rport_from_node_skb(struct hlist_node *rp, struct sk_buff *skb)
1048 struct net_bridge_mcast_port *mctx;
1050 #if IS_ENABLED(CONFIG_IPV6)
1051 if (skb->protocol == htons(ETH_P_IPV6))
1052 mctx = hlist_entry_safe(rp, struct net_bridge_mcast_port,
1056 mctx = hlist_entry_safe(rp, struct net_bridge_mcast_port,
1065 static inline bool br_ip4_multicast_is_router(struct net_bridge_mcast *brmctx)
1067 return timer_pending(&brmctx->ip4_mc_router_timer);
1070 static inline bool br_ip6_multicast_is_router(struct net_bridge_mcast *brmctx)
1072 #if IS_ENABLED(CONFIG_IPV6)
1073 return timer_pending(&brmctx->ip6_mc_router_timer);
1080 br_multicast_is_router(struct net_bridge_mcast *brmctx, struct sk_buff *skb)
1082 switch (brmctx->multicast_router) {
1083 case MDB_RTR_TYPE_PERM:
1085 case MDB_RTR_TYPE_TEMP_QUERY:
1087 if (skb->protocol == htons(ETH_P_IP))
1088 return br_ip4_multicast_is_router(brmctx);
1089 else if (skb->protocol == htons(ETH_P_IPV6))
1090 return br_ip6_multicast_is_router(brmctx);
1092 return br_ip4_multicast_is_router(brmctx) ||
1093 br_ip6_multicast_is_router(brmctx);
1102 __br_multicast_querier_exists(struct net_bridge_mcast *brmctx,
1103 struct bridge_mcast_other_query *querier,
1106 bool own_querier_enabled;
1108 if (brmctx->multicast_querier) {
1109 if (is_ipv6 && !br_opt_get(brmctx->br, BROPT_HAS_IPV6_ADDR))
1110 own_querier_enabled = false;
1112 own_querier_enabled = true;
1114 own_querier_enabled = false;
1117 return time_is_before_jiffies(querier->delay_time) &&
1118 (own_querier_enabled || timer_pending(&querier->timer));
1121 static inline bool br_multicast_querier_exists(struct net_bridge_mcast *brmctx,
1123 const struct net_bridge_mdb_entry *mdb)
1125 switch (eth->h_proto) {
1126 case (htons(ETH_P_IP)):
1127 return __br_multicast_querier_exists(brmctx,
1128 &brmctx->ip4_other_query, false);
1129 #if IS_ENABLED(CONFIG_IPV6)
1130 case (htons(ETH_P_IPV6)):
1131 return __br_multicast_querier_exists(brmctx,
1132 &brmctx->ip6_other_query, true);
1135 return !!mdb && br_group_is_l2(&mdb->addr);
1139 static inline bool br_multicast_is_star_g(const struct br_ip *ip)
1141 switch (ip->proto) {
1142 case htons(ETH_P_IP):
1143 return ipv4_is_zeronet(ip->src.ip4);
1144 #if IS_ENABLED(CONFIG_IPV6)
1145 case htons(ETH_P_IPV6):
1146 return ipv6_addr_any(&ip->src.ip6);
1154 br_multicast_should_handle_mode(const struct net_bridge_mcast *brmctx,
1158 case htons(ETH_P_IP):
1159 return !!(brmctx->multicast_igmp_version == 3);
1160 #if IS_ENABLED(CONFIG_IPV6)
1161 case htons(ETH_P_IPV6):
1162 return !!(brmctx->multicast_mld_version == 2);
1169 static inline int br_multicast_igmp_type(const struct sk_buff *skb)
1171 return BR_INPUT_SKB_CB(skb)->igmp;
1174 static inline unsigned long br_multicast_lmqt(const struct net_bridge_mcast *brmctx)
1176 return brmctx->multicast_last_member_interval *
1177 brmctx->multicast_last_member_count;
1180 static inline unsigned long br_multicast_gmi(const struct net_bridge_mcast *brmctx)
1182 return brmctx->multicast_membership_interval;
1186 br_multicast_ctx_is_vlan(const struct net_bridge_mcast *brmctx)
1188 return !!brmctx->vlan;
1192 br_multicast_port_ctx_is_vlan(const struct net_bridge_mcast_port *pmctx)
1194 return !!pmctx->vlan;
1197 static inline struct net_bridge_mcast *
1198 br_multicast_port_ctx_get_global(const struct net_bridge_mcast_port *pmctx)
1200 if (!br_multicast_port_ctx_is_vlan(pmctx))
1201 return &pmctx->port->br->multicast_ctx;
1203 return &pmctx->vlan->brvlan->br_mcast_ctx;
1207 br_multicast_ctx_vlan_global_disabled(const struct net_bridge_mcast *brmctx)
1209 return br_multicast_ctx_is_vlan(brmctx) &&
1210 (!br_opt_get(brmctx->br, BROPT_MCAST_VLAN_SNOOPING_ENABLED) ||
1211 !(brmctx->vlan->priv_flags & BR_VLFLAG_GLOBAL_MCAST_ENABLED));
1215 br_multicast_ctx_vlan_disabled(const struct net_bridge_mcast *brmctx)
1217 return br_multicast_ctx_is_vlan(brmctx) &&
1218 !(brmctx->vlan->priv_flags & BR_VLFLAG_MCAST_ENABLED);
1222 br_multicast_port_ctx_vlan_disabled(const struct net_bridge_mcast_port *pmctx)
1224 return br_multicast_port_ctx_is_vlan(pmctx) &&
1225 !(pmctx->vlan->priv_flags & BR_VLFLAG_MCAST_ENABLED);
1229 br_multicast_port_ctx_state_disabled(const struct net_bridge_mcast_port *pmctx)
1231 return pmctx->port->state == BR_STATE_DISABLED ||
1232 (br_multicast_port_ctx_is_vlan(pmctx) &&
1233 (br_multicast_port_ctx_vlan_disabled(pmctx) ||
1234 pmctx->vlan->state == BR_STATE_DISABLED));
1238 br_multicast_port_ctx_state_stopped(const struct net_bridge_mcast_port *pmctx)
1240 return br_multicast_port_ctx_state_disabled(pmctx) ||
1241 pmctx->port->state == BR_STATE_BLOCKING ||
1242 (br_multicast_port_ctx_is_vlan(pmctx) &&
1243 pmctx->vlan->state == BR_STATE_BLOCKING);
1247 br_rports_have_mc_router(const struct net_bridge_mcast *brmctx)
1249 #if IS_ENABLED(CONFIG_IPV6)
1250 return !hlist_empty(&brmctx->ip4_mc_router_list) ||
1251 !hlist_empty(&brmctx->ip6_mc_router_list);
1253 return !hlist_empty(&brmctx->ip4_mc_router_list);
1258 br_multicast_ctx_options_equal(const struct net_bridge_mcast *brmctx1,
1259 const struct net_bridge_mcast *brmctx2)
1261 return brmctx1->multicast_igmp_version ==
1262 brmctx2->multicast_igmp_version &&
1263 brmctx1->multicast_last_member_count ==
1264 brmctx2->multicast_last_member_count &&
1265 brmctx1->multicast_startup_query_count ==
1266 brmctx2->multicast_startup_query_count &&
1267 brmctx1->multicast_last_member_interval ==
1268 brmctx2->multicast_last_member_interval &&
1269 brmctx1->multicast_membership_interval ==
1270 brmctx2->multicast_membership_interval &&
1271 brmctx1->multicast_querier_interval ==
1272 brmctx2->multicast_querier_interval &&
1273 brmctx1->multicast_query_interval ==
1274 brmctx2->multicast_query_interval &&
1275 brmctx1->multicast_query_response_interval ==
1276 brmctx2->multicast_query_response_interval &&
1277 brmctx1->multicast_startup_query_interval ==
1278 brmctx2->multicast_startup_query_interval &&
1279 brmctx1->multicast_querier == brmctx2->multicast_querier &&
1280 brmctx1->multicast_router == brmctx2->multicast_router &&
1281 !br_rports_have_mc_router(brmctx1) &&
1282 !br_rports_have_mc_router(brmctx2) &&
1283 #if IS_ENABLED(CONFIG_IPV6)
1284 brmctx1->multicast_mld_version ==
1285 brmctx2->multicast_mld_version &&
1291 br_multicast_ctx_matches_vlan_snooping(const struct net_bridge_mcast *brmctx)
1293 bool vlan_snooping_enabled;
1295 vlan_snooping_enabled = !!br_opt_get(brmctx->br,
1296 BROPT_MCAST_VLAN_SNOOPING_ENABLED);
1298 return !!(vlan_snooping_enabled == br_multicast_ctx_is_vlan(brmctx));
1301 static inline int br_multicast_rcv(struct net_bridge_mcast **brmctx,
1302 struct net_bridge_mcast_port **pmctx,
1303 struct net_bridge_vlan *vlan,
1304 struct sk_buff *skb,
1310 static inline struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge_mcast *brmctx,
1311 struct sk_buff *skb, u16 vid)
1316 static inline int br_multicast_add_port(struct net_bridge_port *port)
1321 static inline void br_multicast_del_port(struct net_bridge_port *port)
1325 static inline void br_multicast_enable_port(struct net_bridge_port *port)
1329 static inline void br_multicast_disable_port(struct net_bridge_port *port)
1333 static inline void br_multicast_init(struct net_bridge *br)
1337 static inline void br_multicast_join_snoopers(struct net_bridge *br)
1341 static inline void br_multicast_leave_snoopers(struct net_bridge *br)
1345 static inline void br_multicast_open(struct net_bridge *br)
1349 static inline void br_multicast_stop(struct net_bridge *br)
1353 static inline void br_multicast_dev_del(struct net_bridge *br)
1357 static inline void br_multicast_flood(struct net_bridge_mdb_entry *mdst,
1358 struct sk_buff *skb,
1359 struct net_bridge_mcast *brmctx,
1360 bool local_rcv, bool local_orig)
1364 static inline bool br_multicast_is_router(struct net_bridge_mcast *brmctx,
1365 struct sk_buff *skb)
1370 static inline bool br_multicast_querier_exists(struct net_bridge_mcast *brmctx,
1372 const struct net_bridge_mdb_entry *mdb)
1377 static inline void br_mdb_init(void)
1381 static inline void br_mdb_uninit(void)
1385 static inline int br_mdb_hash_init(struct net_bridge *br)
1390 static inline void br_mdb_hash_fini(struct net_bridge *br)
1394 static inline void br_multicast_count(struct net_bridge *br,
1395 const struct net_bridge_port *p,
1396 const struct sk_buff *skb,
1401 static inline int br_multicast_init_stats(struct net_bridge *br)
1406 static inline void br_multicast_uninit_stats(struct net_bridge *br)
1410 static inline int br_multicast_igmp_type(const struct sk_buff *skb)
1415 static inline void br_multicast_ctx_init(struct net_bridge *br,
1416 struct net_bridge_vlan *vlan,
1417 struct net_bridge_mcast *brmctx)
1421 static inline void br_multicast_ctx_deinit(struct net_bridge_mcast *brmctx)
1425 static inline void br_multicast_port_ctx_init(struct net_bridge_port *port,
1426 struct net_bridge_vlan *vlan,
1427 struct net_bridge_mcast_port *pmctx)
1431 static inline void br_multicast_port_ctx_deinit(struct net_bridge_mcast_port *pmctx)
1435 static inline void br_multicast_toggle_one_vlan(struct net_bridge_vlan *vlan,
1440 static inline int br_multicast_toggle_vlan_snooping(struct net_bridge *br,
1442 struct netlink_ext_ack *extack)
1447 static inline bool br_multicast_toggle_global_vlan(struct net_bridge_vlan *vlan,
1454 br_multicast_ctx_options_equal(const struct net_bridge_mcast *brmctx1,
1455 const struct net_bridge_mcast *brmctx2)
1462 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
1463 bool br_allowed_ingress(const struct net_bridge *br,
1464 struct net_bridge_vlan_group *vg, struct sk_buff *skb,
1465 u16 *vid, u8 *state,
1466 struct net_bridge_vlan **vlan);
1467 bool br_allowed_egress(struct net_bridge_vlan_group *vg,
1468 const struct sk_buff *skb);
1469 bool br_should_learn(struct net_bridge_port *p, struct sk_buff *skb, u16 *vid);
1470 struct sk_buff *br_handle_vlan(struct net_bridge *br,
1471 const struct net_bridge_port *port,
1472 struct net_bridge_vlan_group *vg,
1473 struct sk_buff *skb);
1474 int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags,
1475 bool *changed, struct netlink_ext_ack *extack);
1476 int br_vlan_delete(struct net_bridge *br, u16 vid);
1477 void br_vlan_flush(struct net_bridge *br);
1478 struct net_bridge_vlan *br_vlan_find(struct net_bridge_vlan_group *vg, u16 vid);
1479 void br_recalculate_fwd_mask(struct net_bridge *br);
1480 int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val,
1481 struct netlink_ext_ack *extack);
1482 int __br_vlan_set_proto(struct net_bridge *br, __be16 proto,
1483 struct netlink_ext_ack *extack);
1484 int br_vlan_set_proto(struct net_bridge *br, unsigned long val,
1485 struct netlink_ext_ack *extack);
1486 int br_vlan_set_stats(struct net_bridge *br, unsigned long val);
1487 int br_vlan_set_stats_per_port(struct net_bridge *br, unsigned long val);
1488 int br_vlan_init(struct net_bridge *br);
1489 int br_vlan_set_default_pvid(struct net_bridge *br, unsigned long val,
1490 struct netlink_ext_ack *extack);
1491 int __br_vlan_set_default_pvid(struct net_bridge *br, u16 pvid,
1492 struct netlink_ext_ack *extack);
1493 int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags,
1494 bool *changed, struct netlink_ext_ack *extack);
1495 int nbp_vlan_delete(struct net_bridge_port *port, u16 vid);
1496 void nbp_vlan_flush(struct net_bridge_port *port);
1497 int nbp_vlan_init(struct net_bridge_port *port, struct netlink_ext_ack *extack);
1498 int nbp_get_num_vlan_infos(struct net_bridge_port *p, u32 filter_mask);
1499 void br_vlan_get_stats(const struct net_bridge_vlan *v,
1500 struct pcpu_sw_netstats *stats);
1501 void br_vlan_port_event(struct net_bridge_port *p, unsigned long event);
1502 int br_vlan_bridge_event(struct net_device *dev, unsigned long event,
1504 void br_vlan_rtnl_init(void);
1505 void br_vlan_rtnl_uninit(void);
1506 void br_vlan_notify(const struct net_bridge *br,
1507 const struct net_bridge_port *p,
1508 u16 vid, u16 vid_range,
1510 bool br_vlan_can_enter_range(const struct net_bridge_vlan *v_curr,
1511 const struct net_bridge_vlan *range_end);
1513 void br_vlan_fill_forward_path_pvid(struct net_bridge *br,
1514 struct net_device_path_ctx *ctx,
1515 struct net_device_path *path);
1516 int br_vlan_fill_forward_path_mode(struct net_bridge *br,
1517 struct net_bridge_port *dst,
1518 struct net_device_path *path);
1520 static inline struct net_bridge_vlan_group *br_vlan_group(
1521 const struct net_bridge *br)
1523 return rtnl_dereference(br->vlgrp);
1526 static inline struct net_bridge_vlan_group *nbp_vlan_group(
1527 const struct net_bridge_port *p)
1529 return rtnl_dereference(p->vlgrp);
1532 static inline struct net_bridge_vlan_group *br_vlan_group_rcu(
1533 const struct net_bridge *br)
1535 return rcu_dereference(br->vlgrp);
1538 static inline struct net_bridge_vlan_group *nbp_vlan_group_rcu(
1539 const struct net_bridge_port *p)
1541 return rcu_dereference(p->vlgrp);
1544 /* Since bridge now depends on 8021Q module, but the time bridge sees the
1545 * skb, the vlan tag will always be present if the frame was tagged.
1547 static inline int br_vlan_get_tag(const struct sk_buff *skb, u16 *vid)
1551 if (skb_vlan_tag_present(skb)) {
1552 *vid = skb_vlan_tag_get_id(skb);
1561 static inline u16 br_get_pvid(const struct net_bridge_vlan_group *vg)
1570 static inline u16 br_vlan_flags(const struct net_bridge_vlan *v, u16 pvid)
1572 return v->vid == pvid ? v->flags | BRIDGE_VLAN_INFO_PVID : v->flags;
1575 static inline bool br_allowed_ingress(const struct net_bridge *br,
1576 struct net_bridge_vlan_group *vg,
1577 struct sk_buff *skb,
1578 u16 *vid, u8 *state,
1579 struct net_bridge_vlan **vlan)
1586 static inline bool br_allowed_egress(struct net_bridge_vlan_group *vg,
1587 const struct sk_buff *skb)
1592 static inline bool br_should_learn(struct net_bridge_port *p,
1593 struct sk_buff *skb, u16 *vid)
1598 static inline struct sk_buff *br_handle_vlan(struct net_bridge *br,
1599 const struct net_bridge_port *port,
1600 struct net_bridge_vlan_group *vg,
1601 struct sk_buff *skb)
1606 static inline int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags,
1607 bool *changed, struct netlink_ext_ack *extack)
1613 static inline int br_vlan_delete(struct net_bridge *br, u16 vid)
1618 static inline void br_vlan_flush(struct net_bridge *br)
1622 static inline void br_recalculate_fwd_mask(struct net_bridge *br)
1626 static inline int br_vlan_init(struct net_bridge *br)
1631 static inline int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags,
1632 bool *changed, struct netlink_ext_ack *extack)
1638 static inline int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
1643 static inline void nbp_vlan_flush(struct net_bridge_port *port)
1647 static inline struct net_bridge_vlan *br_vlan_find(struct net_bridge_vlan_group *vg,
1653 static inline int nbp_vlan_init(struct net_bridge_port *port,
1654 struct netlink_ext_ack *extack)
1659 static inline u16 br_vlan_get_tag(const struct sk_buff *skb, u16 *tag)
1664 static inline u16 br_get_pvid(const struct net_bridge_vlan_group *vg)
1669 static inline int br_vlan_filter_toggle(struct net_bridge *br,
1671 struct netlink_ext_ack *extack)
1676 static inline int nbp_get_num_vlan_infos(struct net_bridge_port *p,
1682 static inline void br_vlan_fill_forward_path_pvid(struct net_bridge *br,
1683 struct net_device_path_ctx *ctx,
1684 struct net_device_path *path)
1688 static inline int br_vlan_fill_forward_path_mode(struct net_bridge *br,
1689 struct net_bridge_port *dst,
1690 struct net_device_path *path)
1695 static inline struct net_bridge_vlan_group *br_vlan_group(
1696 const struct net_bridge *br)
1701 static inline struct net_bridge_vlan_group *nbp_vlan_group(
1702 const struct net_bridge_port *p)
1707 static inline struct net_bridge_vlan_group *br_vlan_group_rcu(
1708 const struct net_bridge *br)
1713 static inline struct net_bridge_vlan_group *nbp_vlan_group_rcu(
1714 const struct net_bridge_port *p)
1719 static inline void br_vlan_get_stats(const struct net_bridge_vlan *v,
1720 struct pcpu_sw_netstats *stats)
1724 static inline void br_vlan_port_event(struct net_bridge_port *p,
1725 unsigned long event)
1729 static inline int br_vlan_bridge_event(struct net_device *dev,
1730 unsigned long event, void *ptr)
1735 static inline void br_vlan_rtnl_init(void)
1739 static inline void br_vlan_rtnl_uninit(void)
1743 static inline void br_vlan_notify(const struct net_bridge *br,
1744 const struct net_bridge_port *p,
1745 u16 vid, u16 vid_range,
1750 static inline bool br_vlan_can_enter_range(const struct net_bridge_vlan *v_curr,
1751 const struct net_bridge_vlan *range_end)
1756 static inline u16 br_vlan_flags(const struct net_bridge_vlan *v, u16 pvid)
1763 /* br_vlan_options.c */
1764 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
1765 bool br_vlan_opts_eq_range(const struct net_bridge_vlan *v_curr,
1766 const struct net_bridge_vlan *range_end);
1767 bool br_vlan_opts_fill(struct sk_buff *skb, const struct net_bridge_vlan *v,
1768 const struct net_bridge_port *p);
1769 size_t br_vlan_opts_nl_size(void);
1770 int br_vlan_process_options(const struct net_bridge *br,
1771 const struct net_bridge_port *p,
1772 struct net_bridge_vlan *range_start,
1773 struct net_bridge_vlan *range_end,
1775 struct netlink_ext_ack *extack);
1776 int br_vlan_rtm_process_global_options(struct net_device *dev,
1777 const struct nlattr *attr,
1779 struct netlink_ext_ack *extack);
1780 bool br_vlan_global_opts_can_enter_range(const struct net_bridge_vlan *v_curr,
1781 const struct net_bridge_vlan *r_end);
1782 bool br_vlan_global_opts_fill(struct sk_buff *skb, u16 vid, u16 vid_range,
1783 const struct net_bridge_vlan *v_opts);
1785 /* vlan state manipulation helpers using *_ONCE to annotate lock-free access */
1786 static inline u8 br_vlan_get_state(const struct net_bridge_vlan *v)
1788 return READ_ONCE(v->state);
1791 static inline void br_vlan_set_state(struct net_bridge_vlan *v, u8 state)
1793 WRITE_ONCE(v->state, state);
1796 static inline u8 br_vlan_get_pvid_state(const struct net_bridge_vlan_group *vg)
1798 return READ_ONCE(vg->pvid_state);
1801 static inline void br_vlan_set_pvid_state(struct net_bridge_vlan_group *vg,
1804 WRITE_ONCE(vg->pvid_state, state);
1807 /* learn_allow is true at ingress and false at egress */
1808 static inline bool br_vlan_state_allowed(u8 state, bool learn_allow)
1811 case BR_STATE_LEARNING:
1813 case BR_STATE_FORWARDING:
1822 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
1823 DECLARE_STATIC_KEY_FALSE(br_mst_used);
1824 static inline bool br_mst_is_enabled(struct net_bridge *br)
1826 return static_branch_unlikely(&br_mst_used) &&
1827 br_opt_get(br, BROPT_MST_ENABLED);
1830 int br_mst_set_state(struct net_bridge_port *p, u16 msti, u8 state,
1831 struct netlink_ext_ack *extack);
1832 int br_mst_vlan_set_msti(struct net_bridge_vlan *v, u16 msti);
1833 void br_mst_vlan_init_state(struct net_bridge_vlan *v);
1834 int br_mst_set_enabled(struct net_bridge *br, bool on,
1835 struct netlink_ext_ack *extack);
1836 size_t br_mst_info_size(const struct net_bridge_vlan_group *vg);
1837 int br_mst_fill_info(struct sk_buff *skb,
1838 const struct net_bridge_vlan_group *vg);
1839 int br_mst_process(struct net_bridge_port *p, const struct nlattr *mst_attr,
1840 struct netlink_ext_ack *extack);
1842 static inline bool br_mst_is_enabled(struct net_bridge *br)
1847 static inline int br_mst_set_state(struct net_bridge_port *p, u16 msti,
1848 u8 state, struct netlink_ext_ack *extack)
1853 static inline int br_mst_set_enabled(struct net_bridge *br, bool on,
1854 struct netlink_ext_ack *extack)
1859 static inline size_t br_mst_info_size(const struct net_bridge_vlan_group *vg)
1864 static inline int br_mst_fill_info(struct sk_buff *skb,
1865 const struct net_bridge_vlan_group *vg)
1870 static inline int br_mst_process(struct net_bridge_port *p,
1871 const struct nlattr *mst_attr,
1872 struct netlink_ext_ack *extack)
1879 int (*br_dev_xmit_hook)(struct sk_buff *skb);
1881 extern const struct nf_br_ops __rcu *nf_br_ops;
1883 /* br_netfilter.c */
1884 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1885 int br_nf_core_init(void);
1886 void br_nf_core_fini(void);
1887 void br_netfilter_rtable_init(struct net_bridge *);
1889 static inline int br_nf_core_init(void) { return 0; }
1890 static inline void br_nf_core_fini(void) {}
1891 #define br_netfilter_rtable_init(x)
1895 void br_set_state(struct net_bridge_port *p, unsigned int state);
1896 struct net_bridge_port *br_get_port(struct net_bridge *br, u16 port_no);
1897 void br_init_port(struct net_bridge_port *p);
1898 void br_become_designated_port(struct net_bridge_port *p);
1900 void __br_set_forward_delay(struct net_bridge *br, unsigned long t);
1901 int br_set_forward_delay(struct net_bridge *br, unsigned long x);
1902 int br_set_hello_time(struct net_bridge *br, unsigned long x);
1903 int br_set_max_age(struct net_bridge *br, unsigned long x);
1904 int __set_ageing_time(struct net_device *dev, unsigned long t);
1905 int br_set_ageing_time(struct net_bridge *br, clock_t ageing_time);
1909 void br_stp_enable_bridge(struct net_bridge *br);
1910 void br_stp_disable_bridge(struct net_bridge *br);
1911 int br_stp_set_enabled(struct net_bridge *br, unsigned long val,
1912 struct netlink_ext_ack *extack);
1913 void br_stp_enable_port(struct net_bridge_port *p);
1914 void br_stp_disable_port(struct net_bridge_port *p);
1915 bool br_stp_recalculate_bridge_id(struct net_bridge *br);
1916 void br_stp_change_bridge_id(struct net_bridge *br, const unsigned char *a);
1917 void br_stp_set_bridge_priority(struct net_bridge *br, u16 newprio);
1918 int br_stp_set_port_priority(struct net_bridge_port *p, unsigned long newprio);
1919 int br_stp_set_path_cost(struct net_bridge_port *p, unsigned long path_cost);
1920 ssize_t br_show_bridge_id(char *buf, const struct bridge_id *id);
1924 void br_stp_rcv(const struct stp_proto *proto, struct sk_buff *skb,
1925 struct net_device *dev);
1927 /* br_stp_timer.c */
1928 void br_stp_timer_init(struct net_bridge *br);
1929 void br_stp_port_timer_init(struct net_bridge_port *p);
1930 unsigned long br_timer_value(const struct timer_list *timer);
1933 #if IS_ENABLED(CONFIG_ATM_LANE)
1934 extern int (*br_fdb_test_addr_hook)(struct net_device *dev, unsigned char *addr);
1938 #if IS_ENABLED(CONFIG_BRIDGE_MRP)
1939 int br_mrp_parse(struct net_bridge *br, struct net_bridge_port *p,
1940 struct nlattr *attr, int cmd, struct netlink_ext_ack *extack);
1941 bool br_mrp_enabled(struct net_bridge *br);
1942 void br_mrp_port_del(struct net_bridge *br, struct net_bridge_port *p);
1943 int br_mrp_fill_info(struct sk_buff *skb, struct net_bridge *br);
1945 static inline int br_mrp_parse(struct net_bridge *br, struct net_bridge_port *p,
1946 struct nlattr *attr, int cmd,
1947 struct netlink_ext_ack *extack)
1952 static inline bool br_mrp_enabled(struct net_bridge *br)
1957 static inline void br_mrp_port_del(struct net_bridge *br,
1958 struct net_bridge_port *p)
1962 static inline int br_mrp_fill_info(struct sk_buff *skb, struct net_bridge *br)
1970 #if IS_ENABLED(CONFIG_BRIDGE_CFM)
1971 int br_cfm_parse(struct net_bridge *br, struct net_bridge_port *p,
1972 struct nlattr *attr, int cmd, struct netlink_ext_ack *extack);
1973 bool br_cfm_created(struct net_bridge *br);
1974 void br_cfm_port_del(struct net_bridge *br, struct net_bridge_port *p);
1975 int br_cfm_config_fill_info(struct sk_buff *skb, struct net_bridge *br);
1976 int br_cfm_status_fill_info(struct sk_buff *skb,
1977 struct net_bridge *br,
1979 int br_cfm_mep_count(struct net_bridge *br, u32 *count);
1980 int br_cfm_peer_mep_count(struct net_bridge *br, u32 *count);
1982 static inline int br_cfm_parse(struct net_bridge *br, struct net_bridge_port *p,
1983 struct nlattr *attr, int cmd,
1984 struct netlink_ext_ack *extack)
1989 static inline bool br_cfm_created(struct net_bridge *br)
1994 static inline void br_cfm_port_del(struct net_bridge *br,
1995 struct net_bridge_port *p)
1999 static inline int br_cfm_config_fill_info(struct sk_buff *skb, struct net_bridge *br)
2004 static inline int br_cfm_status_fill_info(struct sk_buff *skb,
2005 struct net_bridge *br,
2011 static inline int br_cfm_mep_count(struct net_bridge *br, u32 *count)
2017 static inline int br_cfm_peer_mep_count(struct net_bridge *br, u32 *count)
2025 extern struct rtnl_link_ops br_link_ops;
2026 int br_netlink_init(void);
2027 void br_netlink_fini(void);
2028 void br_ifinfo_notify(int event, const struct net_bridge *br,
2029 const struct net_bridge_port *port);
2030 void br_info_notify(int event, const struct net_bridge *br,
2031 const struct net_bridge_port *port, u32 filter);
2032 int br_setlink(struct net_device *dev, struct nlmsghdr *nlmsg, u16 flags,
2033 struct netlink_ext_ack *extack);
2034 int br_dellink(struct net_device *dev, struct nlmsghdr *nlmsg, u16 flags);
2035 int br_getlink(struct sk_buff *skb, u32 pid, u32 seq, struct net_device *dev,
2036 u32 filter_mask, int nlflags);
2037 int br_process_vlan_info(struct net_bridge *br,
2038 struct net_bridge_port *p, int cmd,
2039 struct bridge_vlan_info *vinfo_curr,
2040 struct bridge_vlan_info **vinfo_last,
2042 struct netlink_ext_ack *extack);
2046 extern const struct sysfs_ops brport_sysfs_ops;
2047 int br_sysfs_addif(struct net_bridge_port *p);
2048 int br_sysfs_renameif(struct net_bridge_port *p);
2051 int br_sysfs_addbr(struct net_device *dev);
2052 void br_sysfs_delbr(struct net_device *dev);
2056 static inline int br_sysfs_addif(struct net_bridge_port *p) { return 0; }
2057 static inline int br_sysfs_renameif(struct net_bridge_port *p) { return 0; }
2058 static inline int br_sysfs_addbr(struct net_device *dev) { return 0; }
2059 static inline void br_sysfs_delbr(struct net_device *dev) { return; }
2060 #endif /* CONFIG_SYSFS */
2062 /* br_switchdev.c */
2063 #ifdef CONFIG_NET_SWITCHDEV
2064 int br_switchdev_port_offload(struct net_bridge_port *p,
2065 struct net_device *dev, const void *ctx,
2066 struct notifier_block *atomic_nb,
2067 struct notifier_block *blocking_nb,
2068 bool tx_fwd_offload,
2069 struct netlink_ext_ack *extack);
2071 void br_switchdev_port_unoffload(struct net_bridge_port *p, const void *ctx,
2072 struct notifier_block *atomic_nb,
2073 struct notifier_block *blocking_nb);
2075 bool br_switchdev_frame_uses_tx_fwd_offload(struct sk_buff *skb);
2077 void br_switchdev_frame_set_offload_fwd_mark(struct sk_buff *skb);
2079 void nbp_switchdev_frame_mark_tx_fwd_offload(const struct net_bridge_port *p,
2080 struct sk_buff *skb);
2081 void nbp_switchdev_frame_mark_tx_fwd_to_hwdom(const struct net_bridge_port *p,
2082 struct sk_buff *skb);
2083 void nbp_switchdev_frame_mark(const struct net_bridge_port *p,
2084 struct sk_buff *skb);
2085 bool nbp_switchdev_allowed_egress(const struct net_bridge_port *p,
2086 const struct sk_buff *skb);
2087 int br_switchdev_set_port_flag(struct net_bridge_port *p,
2088 unsigned long flags,
2090 struct netlink_ext_ack *extack);
2091 void br_switchdev_fdb_notify(struct net_bridge *br,
2092 const struct net_bridge_fdb_entry *fdb, int type);
2093 void br_switchdev_mdb_notify(struct net_device *dev,
2094 struct net_bridge_mdb_entry *mp,
2095 struct net_bridge_port_group *pg,
2097 int br_switchdev_port_vlan_add(struct net_device *dev, u16 vid, u16 flags,
2098 bool changed, struct netlink_ext_ack *extack);
2099 int br_switchdev_port_vlan_del(struct net_device *dev, u16 vid);
2100 void br_switchdev_init(struct net_bridge *br);
2102 static inline void br_switchdev_frame_unmark(struct sk_buff *skb)
2104 skb->offload_fwd_mark = 0;
2108 br_switchdev_port_offload(struct net_bridge_port *p,
2109 struct net_device *dev, const void *ctx,
2110 struct notifier_block *atomic_nb,
2111 struct notifier_block *blocking_nb,
2112 bool tx_fwd_offload,
2113 struct netlink_ext_ack *extack)
2119 br_switchdev_port_unoffload(struct net_bridge_port *p, const void *ctx,
2120 struct notifier_block *atomic_nb,
2121 struct notifier_block *blocking_nb)
2125 static inline bool br_switchdev_frame_uses_tx_fwd_offload(struct sk_buff *skb)
2130 static inline void br_switchdev_frame_set_offload_fwd_mark(struct sk_buff *skb)
2135 nbp_switchdev_frame_mark_tx_fwd_offload(const struct net_bridge_port *p,
2136 struct sk_buff *skb)
2141 nbp_switchdev_frame_mark_tx_fwd_to_hwdom(const struct net_bridge_port *p,
2142 struct sk_buff *skb)
2146 static inline void nbp_switchdev_frame_mark(const struct net_bridge_port *p,
2147 struct sk_buff *skb)
2151 static inline bool nbp_switchdev_allowed_egress(const struct net_bridge_port *p,
2152 const struct sk_buff *skb)
2157 static inline int br_switchdev_set_port_flag(struct net_bridge_port *p,
2158 unsigned long flags,
2160 struct netlink_ext_ack *extack)
2165 static inline int br_switchdev_port_vlan_add(struct net_device *dev, u16 vid,
2166 u16 flags, bool changed,
2167 struct netlink_ext_ack *extack)
2172 static inline int br_switchdev_port_vlan_del(struct net_device *dev, u16 vid)
2178 br_switchdev_fdb_notify(struct net_bridge *br,
2179 const struct net_bridge_fdb_entry *fdb, int type)
2183 static inline void br_switchdev_mdb_notify(struct net_device *dev,
2184 struct net_bridge_mdb_entry *mp,
2185 struct net_bridge_port_group *pg,
2190 static inline void br_switchdev_frame_unmark(struct sk_buff *skb)
2194 static inline void br_switchdev_init(struct net_bridge *br)
2198 #endif /* CONFIG_NET_SWITCHDEV */
2200 /* br_arp_nd_proxy.c */
2201 void br_recalculate_neigh_suppress_enabled(struct net_bridge *br);
2202 void br_do_proxy_suppress_arp(struct sk_buff *skb, struct net_bridge *br,
2203 u16 vid, struct net_bridge_port *p);
2204 void br_do_suppress_nd(struct sk_buff *skb, struct net_bridge *br,
2205 u16 vid, struct net_bridge_port *p, struct nd_msg *msg);
2206 struct nd_msg *br_is_nd_neigh_msg(struct sk_buff *skb, struct nd_msg *m);