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;
129 #endif /* CONFIG_BRIDGE_IGMP_SNOOPING */
132 /* net_bridge_mcast must be always defined due to forwarding stubs */
133 struct net_bridge_mcast {
134 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
135 struct net_bridge *br;
136 struct net_bridge_vlan *vlan;
138 u32 multicast_last_member_count;
139 u32 multicast_startup_query_count;
141 u8 multicast_querier;
142 u8 multicast_igmp_version;
144 #if IS_ENABLED(CONFIG_IPV6)
145 u8 multicast_mld_version;
147 unsigned long multicast_last_member_interval;
148 unsigned long multicast_membership_interval;
149 unsigned long multicast_querier_interval;
150 unsigned long multicast_query_interval;
151 unsigned long multicast_query_response_interval;
152 unsigned long multicast_startup_query_interval;
153 struct hlist_head ip4_mc_router_list;
154 struct timer_list ip4_mc_router_timer;
155 struct bridge_mcast_other_query ip4_other_query;
156 struct bridge_mcast_own_query ip4_own_query;
157 struct bridge_mcast_querier ip4_querier;
158 #if IS_ENABLED(CONFIG_IPV6)
159 struct hlist_head ip6_mc_router_list;
160 struct timer_list ip6_mc_router_timer;
161 struct bridge_mcast_other_query ip6_other_query;
162 struct bridge_mcast_own_query ip6_own_query;
163 struct bridge_mcast_querier ip6_querier;
164 #endif /* IS_ENABLED(CONFIG_IPV6) */
165 #endif /* CONFIG_BRIDGE_IGMP_SNOOPING */
168 struct br_tunnel_info {
170 struct metadata_dst __rcu *tunnel_dst;
173 /* private vlan flags */
175 BR_VLFLAG_PER_PORT_STATS = BIT(0),
176 BR_VLFLAG_ADDED_BY_SWITCHDEV = BIT(1),
177 BR_VLFLAG_MCAST_ENABLED = BIT(2),
178 BR_VLFLAG_GLOBAL_MCAST_ENABLED = BIT(3),
182 * struct net_bridge_vlan - per-vlan entry
184 * @vnode: rhashtable member
186 * @flags: bridge vlan flags
187 * @priv_flags: private (in-kernel) bridge vlan flags
188 * @state: STP state (e.g. blocking, learning, forwarding)
189 * @stats: per-cpu VLAN statistics
190 * @br: if MASTER flag set, this points to a bridge struct
191 * @port: if MASTER flag unset, this points to a port struct
192 * @refcnt: if MASTER flag set, this is bumped for each port referencing it
193 * @brvlan: if MASTER flag unset, this points to the global per-VLAN context
194 * for this VLAN entry
195 * @br_mcast_ctx: if MASTER flag set, this is the global vlan multicast context
196 * @port_mcast_ctx: if MASTER flag unset, this is the per-port/vlan multicast
198 * @msti: if MASTER flag set, this holds the VLANs MST instance
199 * @vlist: sorted list of VLAN entries
200 * @rcu: used for entry destruction
202 * This structure is shared between the global per-VLAN entries contained in
203 * the bridge rhashtable and the local per-port per-VLAN entries contained in
204 * the port's rhashtable. The union entries should be interpreted depending on
205 * the entry flags that are set.
207 struct net_bridge_vlan {
208 struct rhash_head vnode;
209 struct rhash_head tnode;
214 struct pcpu_sw_netstats __percpu *stats;
216 struct net_bridge *br;
217 struct net_bridge_port *port;
221 struct net_bridge_vlan *brvlan;
224 struct br_tunnel_info tinfo;
227 struct net_bridge_mcast br_mcast_ctx;
228 struct net_bridge_mcast_port port_mcast_ctx;
233 struct list_head vlist;
239 * struct net_bridge_vlan_group
241 * @vlan_hash: VLAN entry rhashtable
242 * @vlan_list: sorted VLAN entry list
243 * @num_vlans: number of total VLAN entries
244 * @pvid: PVID VLAN id
245 * @pvid_state: PVID's STP state (e.g. forwarding, learning, blocking)
247 * IMPORTANT: Be careful when checking if there're VLAN entries using list
248 * primitives because the bridge can have entries in its list which
249 * are just for global context but not for filtering, i.e. they have
250 * the master flag set but not the brentry flag. If you have to check
251 * if there're "real" entries in the bridge please test @num_vlans
253 struct net_bridge_vlan_group {
254 struct rhashtable vlan_hash;
255 struct rhashtable tunnel_hash;
256 struct list_head vlan_list;
262 /* bridge fdb flags */
267 BR_FDB_ADDED_BY_USER,
268 BR_FDB_ADDED_BY_EXT_LEARN,
271 BR_FDB_NOTIFY_INACTIVE,
275 struct net_bridge_fdb_key {
280 struct net_bridge_fdb_entry {
281 struct rhash_head rhnode;
282 struct net_bridge_port *dst;
284 struct net_bridge_fdb_key key;
285 struct hlist_node fdb_node;
288 /* write-heavy members should not affect lookups */
289 unsigned long updated ____cacheline_aligned_in_smp;
295 struct net_bridge_fdb_flush_desc {
297 unsigned long flags_mask;
302 #define MDB_PG_FLAGS_PERMANENT BIT(0)
303 #define MDB_PG_FLAGS_OFFLOAD BIT(1)
304 #define MDB_PG_FLAGS_FAST_LEAVE BIT(2)
305 #define MDB_PG_FLAGS_STAR_EXCL BIT(3)
306 #define MDB_PG_FLAGS_BLOCKED BIT(4)
308 #define PG_SRC_ENT_LIMIT 32
310 #define BR_SGRP_F_DELETE BIT(0)
311 #define BR_SGRP_F_SEND BIT(1)
312 #define BR_SGRP_F_INSTALLED BIT(2)
313 #define BR_SGRP_F_USER_ADDED BIT(3)
315 struct net_bridge_mcast_gc {
316 struct hlist_node gc_node;
317 void (*destroy)(struct net_bridge_mcast_gc *gc);
320 struct net_bridge_group_src {
321 struct hlist_node node;
324 struct net_bridge_port_group *pg;
326 u8 src_query_rexmit_cnt;
327 struct timer_list timer;
329 struct net_bridge *br;
330 struct net_bridge_mcast_gc mcast_gc;
334 struct net_bridge_port_group_sg_key {
335 struct net_bridge_port *port;
339 struct net_bridge_port_group {
340 struct net_bridge_port_group __rcu *next;
341 struct net_bridge_port_group_sg_key key;
342 unsigned char eth_addr[ETH_ALEN] __aligned(2);
344 unsigned char filter_mode;
345 unsigned char grp_query_rexmit_cnt;
346 unsigned char rt_protocol;
348 struct hlist_head src_list;
349 unsigned int src_ents;
350 struct timer_list timer;
351 struct timer_list rexmit_timer;
352 struct hlist_node mglist;
353 struct rb_root eht_set_tree;
354 struct rb_root eht_host_tree;
356 struct rhash_head rhnode;
357 struct net_bridge_mcast_gc mcast_gc;
361 struct net_bridge_mdb_entry {
362 struct rhash_head rhnode;
363 struct net_bridge *br;
364 struct net_bridge_port_group __rcu *ports;
368 struct timer_list timer;
369 struct hlist_node mdb_node;
371 struct net_bridge_mcast_gc mcast_gc;
375 struct net_bridge_port {
376 struct net_bridge *br;
377 struct net_device *dev;
378 netdevice_tracker dev_tracker;
379 struct list_head list;
382 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
383 struct net_bridge_vlan_group __rcu *vlgrp;
385 struct net_bridge_port __rcu *backup_port;
391 unsigned char topology_change_ack;
392 unsigned char config_pending;
394 port_id designated_port;
395 bridge_id designated_root;
396 bridge_id designated_bridge;
399 unsigned long designated_age;
401 struct timer_list forward_delay_timer;
402 struct timer_list hold_timer;
403 struct timer_list message_age_timer;
407 struct net_bridge_mcast_port multicast_ctx;
409 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
410 struct bridge_mcast_stats __percpu *mcast_stats;
412 u32 multicast_eht_hosts_limit;
413 u32 multicast_eht_hosts_cnt;
414 struct hlist_head mglist;
418 char sysfs_name[IFNAMSIZ];
421 #ifdef CONFIG_NET_POLL_CONTROLLER
424 #ifdef CONFIG_NET_SWITCHDEV
425 /* Identifier used to group ports that share the same switchdev
430 struct netdev_phys_item_id ppid;
433 u16 backup_redirected_cnt;
435 struct bridge_stp_xstats stp_xstats;
438 #define kobj_to_brport(obj) container_of(obj, struct net_bridge_port, kobj)
440 #define br_auto_port(p) ((p)->flags & BR_AUTO_MASK)
441 #define br_promisc_port(p) ((p)->flags & BR_PROMISC)
443 static inline struct net_bridge_port *br_port_get_rcu(const struct net_device *dev)
445 return rcu_dereference(dev->rx_handler_data);
448 static inline struct net_bridge_port *br_port_get_rtnl(const struct net_device *dev)
450 return netif_is_bridge_port(dev) ?
451 rtnl_dereference(dev->rx_handler_data) : NULL;
454 static inline struct net_bridge_port *br_port_get_rtnl_rcu(const struct net_device *dev)
456 return netif_is_bridge_port(dev) ?
457 rcu_dereference_rtnl(dev->rx_handler_data) : NULL;
460 enum net_bridge_opts {
462 BROPT_VLAN_STATS_ENABLED,
463 BROPT_NF_CALL_IPTABLES,
464 BROPT_NF_CALL_IP6TABLES,
465 BROPT_NF_CALL_ARPTABLES,
466 BROPT_GROUP_ADDR_SET,
467 BROPT_MULTICAST_ENABLED,
468 BROPT_MULTICAST_QUERY_USE_IFADDR,
469 BROPT_MULTICAST_STATS_ENABLED,
471 BROPT_NEIGH_SUPPRESS_ENABLED,
472 BROPT_MTU_SET_BY_USER,
473 BROPT_VLAN_STATS_PER_PORT,
475 BROPT_VLAN_BRIDGE_BINDING,
476 BROPT_MCAST_VLAN_SNOOPING_ENABLED,
482 spinlock_t hash_lock;
483 struct hlist_head frame_type_list;
484 struct net_device *dev;
485 unsigned long options;
486 /* These fields are accessed on each packet */
487 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
490 struct net_bridge_vlan_group __rcu *vlgrp;
493 struct rhashtable fdb_hash_tbl;
494 struct list_head port_list;
495 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
497 struct rtable fake_rtable;
498 struct rt6_info fake_rt6_info;
502 u16 group_fwd_mask_required;
505 bridge_id designated_root;
507 unsigned char topology_change;
508 unsigned char topology_change_detected;
510 unsigned long max_age;
511 unsigned long hello_time;
512 unsigned long forward_delay;
513 unsigned long ageing_time;
514 unsigned long bridge_max_age;
515 unsigned long bridge_hello_time;
516 unsigned long bridge_forward_delay;
517 unsigned long bridge_ageing_time;
520 u8 group_addr[ETH_ALEN];
523 BR_NO_STP, /* no spanning tree */
524 BR_KERNEL_STP, /* old STP in kernel */
525 BR_USER_STP, /* new RSTP in userspace */
528 struct net_bridge_mcast multicast_ctx;
530 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
531 struct bridge_mcast_stats __percpu *mcast_stats;
535 spinlock_t multicast_lock;
537 struct rhashtable mdb_hash_tbl;
538 struct rhashtable sg_port_tbl;
540 struct hlist_head mcast_gc_list;
541 struct hlist_head mdb_list;
543 struct work_struct mcast_gc_work;
546 struct timer_list hello_timer;
547 struct timer_list tcn_timer;
548 struct timer_list topology_change_timer;
549 struct delayed_work gc_work;
550 struct kobject *ifobj;
553 #ifdef CONFIG_NET_SWITCHDEV
554 /* Counter used to make sure that hardware domains get unique
555 * identifiers in case a bridge spans multiple switchdev instances.
558 /* Bit mask of hardware domain numbers in use */
559 unsigned long busy_hwdoms;
561 struct hlist_head fdb_list;
563 #if IS_ENABLED(CONFIG_BRIDGE_MRP)
564 struct hlist_head mrp_list;
566 #if IS_ENABLED(CONFIG_BRIDGE_CFM)
567 struct hlist_head mep_list;
571 struct br_input_skb_cb {
572 struct net_device *brdev;
575 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
579 u8 proxyarp_replied:1;
580 u8 src_port_isolated:1;
581 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
584 #ifdef CONFIG_NETFILTER_FAMILY_BRIDGE
585 u8 br_netfilter_broute:1;
588 #ifdef CONFIG_NET_SWITCHDEV
589 /* Set if TX data plane offloading is used towards at least one
593 /* The switchdev hardware domain from which this packet was received.
594 * If skb->offload_fwd_mark was set, then this packet was already
595 * forwarded by hardware to the other ports in the source hardware
596 * domain, otherwise it wasn't.
599 /* Bit mask of hardware domains towards this packet has already been
600 * transmitted using the TX data plane offload.
602 unsigned long fwd_hwdoms;
606 #define BR_INPUT_SKB_CB(__skb) ((struct br_input_skb_cb *)(__skb)->cb)
608 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
609 # define BR_INPUT_SKB_CB_MROUTERS_ONLY(__skb) (BR_INPUT_SKB_CB(__skb)->mrouters_only)
611 # define BR_INPUT_SKB_CB_MROUTERS_ONLY(__skb) (0)
614 #define br_printk(level, br, format, args...) \
615 printk(level "%s: " format, (br)->dev->name, ##args)
617 #define br_err(__br, format, args...) \
618 br_printk(KERN_ERR, __br, format, ##args)
619 #define br_warn(__br, format, args...) \
620 br_printk(KERN_WARNING, __br, format, ##args)
621 #define br_notice(__br, format, args...) \
622 br_printk(KERN_NOTICE, __br, format, ##args)
623 #define br_info(__br, format, args...) \
624 br_printk(KERN_INFO, __br, format, ##args)
626 #define br_debug(br, format, args...) \
627 pr_debug("%s: " format, (br)->dev->name, ##args)
629 /* called under bridge lock */
630 static inline int br_is_root_bridge(const struct net_bridge *br)
632 return !memcmp(&br->bridge_id, &br->designated_root, 8);
635 /* check if a VLAN entry is global */
636 static inline bool br_vlan_is_master(const struct net_bridge_vlan *v)
638 return v->flags & BRIDGE_VLAN_INFO_MASTER;
641 /* check if a VLAN entry is used by the bridge */
642 static inline bool br_vlan_is_brentry(const struct net_bridge_vlan *v)
644 return v->flags & BRIDGE_VLAN_INFO_BRENTRY;
647 /* check if we should use the vlan entry, returns false if it's only context */
648 static inline bool br_vlan_should_use(const struct net_bridge_vlan *v)
650 if (br_vlan_is_master(v)) {
651 if (br_vlan_is_brentry(v))
660 static inline bool nbp_state_should_learn(const struct net_bridge_port *p)
662 return p->state == BR_STATE_LEARNING || p->state == BR_STATE_FORWARDING;
665 static inline bool br_vlan_valid_id(u16 vid, struct netlink_ext_ack *extack)
667 bool ret = vid > 0 && vid < VLAN_VID_MASK;
670 NL_SET_ERR_MSG_MOD(extack, "Vlan id is invalid");
675 static inline bool br_vlan_valid_range(const struct bridge_vlan_info *cur,
676 const struct bridge_vlan_info *last,
677 struct netlink_ext_ack *extack)
679 /* pvid flag is not allowed in ranges */
680 if (cur->flags & BRIDGE_VLAN_INFO_PVID) {
681 NL_SET_ERR_MSG_MOD(extack, "Pvid isn't allowed in a range");
685 /* when cur is the range end, check if:
686 * - it has range start flag
687 * - range ids are invalid (end is equal to or before start)
690 if (cur->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
691 NL_SET_ERR_MSG_MOD(extack, "Found a new vlan range start while processing one");
693 } else if (!(cur->flags & BRIDGE_VLAN_INFO_RANGE_END)) {
694 NL_SET_ERR_MSG_MOD(extack, "Vlan range end flag is missing");
696 } else if (cur->vid <= last->vid) {
697 NL_SET_ERR_MSG_MOD(extack, "End vlan id is less than or equal to start vlan id");
702 /* check for required range flags */
703 if (!(cur->flags & (BRIDGE_VLAN_INFO_RANGE_BEGIN |
704 BRIDGE_VLAN_INFO_RANGE_END))) {
705 NL_SET_ERR_MSG_MOD(extack, "Both vlan range flags are missing");
712 static inline u8 br_vlan_multicast_router(const struct net_bridge_vlan *v)
714 u8 mcast_router = MDB_RTR_TYPE_DISABLED;
716 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
717 if (!br_vlan_is_master(v))
718 mcast_router = v->port_mcast_ctx.multicast_router;
720 mcast_router = v->br_mcast_ctx.multicast_router;
726 static inline int br_afspec_cmd_to_rtm(int cmd)
738 static inline int br_opt_get(const struct net_bridge *br,
739 enum net_bridge_opts opt)
741 return test_bit(opt, &br->options);
744 int br_boolopt_toggle(struct net_bridge *br, enum br_boolopt_id opt, bool on,
745 struct netlink_ext_ack *extack);
746 int br_boolopt_get(const struct net_bridge *br, enum br_boolopt_id opt);
747 int br_boolopt_multi_toggle(struct net_bridge *br,
748 struct br_boolopt_multi *bm,
749 struct netlink_ext_ack *extack);
750 void br_boolopt_multi_get(const struct net_bridge *br,
751 struct br_boolopt_multi *bm);
752 void br_opt_toggle(struct net_bridge *br, enum net_bridge_opts opt, bool on);
755 void br_dev_setup(struct net_device *dev);
756 void br_dev_delete(struct net_device *dev, struct list_head *list);
757 netdev_tx_t br_dev_xmit(struct sk_buff *skb, struct net_device *dev);
758 #ifdef CONFIG_NET_POLL_CONTROLLER
759 static inline void br_netpoll_send_skb(const struct net_bridge_port *p,
762 netpoll_send_skb(p->np, skb);
765 int br_netpoll_enable(struct net_bridge_port *p);
766 void br_netpoll_disable(struct net_bridge_port *p);
768 static inline void br_netpoll_send_skb(const struct net_bridge_port *p,
773 static inline int br_netpoll_enable(struct net_bridge_port *p)
778 static inline void br_netpoll_disable(struct net_bridge_port *p)
784 #define FDB_FLUSH_IGNORED_NDM_FLAGS (NTF_MASTER | NTF_SELF)
785 #define FDB_FLUSH_ALLOWED_NDM_STATES (NUD_PERMANENT | NUD_NOARP)
786 #define FDB_FLUSH_ALLOWED_NDM_FLAGS (NTF_USE | NTF_EXT_LEARNED | \
787 NTF_STICKY | NTF_OFFLOADED)
789 int br_fdb_init(void);
790 void br_fdb_fini(void);
791 int br_fdb_hash_init(struct net_bridge *br);
792 void br_fdb_hash_fini(struct net_bridge *br);
793 void br_fdb_flush(struct net_bridge *br,
794 const struct net_bridge_fdb_flush_desc *desc);
795 void br_fdb_find_delete_local(struct net_bridge *br,
796 const struct net_bridge_port *p,
797 const unsigned char *addr, u16 vid);
798 void br_fdb_changeaddr(struct net_bridge_port *p, const unsigned char *newaddr);
799 void br_fdb_change_mac_address(struct net_bridge *br, const u8 *newaddr);
800 void br_fdb_cleanup(struct work_struct *work);
801 void br_fdb_delete_by_port(struct net_bridge *br,
802 const struct net_bridge_port *p, u16 vid, int do_all);
803 struct net_bridge_fdb_entry *br_fdb_find_rcu(struct net_bridge *br,
804 const unsigned char *addr,
806 int br_fdb_test_addr(struct net_device *dev, unsigned char *addr);
807 int br_fdb_fillbuf(struct net_bridge *br, void *buf, unsigned long count,
809 int br_fdb_add_local(struct net_bridge *br, struct net_bridge_port *source,
810 const unsigned char *addr, u16 vid);
811 void br_fdb_update(struct net_bridge *br, struct net_bridge_port *source,
812 const unsigned char *addr, u16 vid, unsigned long flags);
814 int br_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
815 struct net_device *dev, const unsigned char *addr, u16 vid,
816 struct netlink_ext_ack *extack);
817 int br_fdb_delete_bulk(struct ndmsg *ndm, struct nlattr *tb[],
818 struct net_device *dev, u16 vid,
819 struct netlink_ext_ack *extack);
820 int br_fdb_add(struct ndmsg *nlh, struct nlattr *tb[], struct net_device *dev,
821 const unsigned char *addr, u16 vid, u16 nlh_flags,
822 struct netlink_ext_ack *extack);
823 int br_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
824 struct net_device *dev, struct net_device *fdev, int *idx);
825 int br_fdb_get(struct sk_buff *skb, struct nlattr *tb[], struct net_device *dev,
826 const unsigned char *addr, u16 vid, u32 portid, u32 seq,
827 struct netlink_ext_ack *extack);
828 int br_fdb_sync_static(struct net_bridge *br, struct net_bridge_port *p);
829 void br_fdb_unsync_static(struct net_bridge *br, struct net_bridge_port *p);
830 int br_fdb_external_learn_add(struct net_bridge *br, struct net_bridge_port *p,
831 const unsigned char *addr, u16 vid,
832 bool locked, bool swdev_notify);
833 int br_fdb_external_learn_del(struct net_bridge *br, struct net_bridge_port *p,
834 const unsigned char *addr, u16 vid,
836 void br_fdb_offloaded_set(struct net_bridge *br, struct net_bridge_port *p,
837 const unsigned char *addr, u16 vid, bool offloaded);
845 int br_dev_queue_push_xmit(struct net *net, struct sock *sk, struct sk_buff *skb);
846 void br_forward(const struct net_bridge_port *to, struct sk_buff *skb,
847 bool local_rcv, bool local_orig);
848 int br_forward_finish(struct net *net, struct sock *sk, struct sk_buff *skb);
849 void br_flood(struct net_bridge *br, struct sk_buff *skb,
850 enum br_pkt_type pkt_type, bool local_rcv, bool local_orig);
852 /* return true if both source port and dest port are isolated */
853 static inline bool br_skb_isolated(const struct net_bridge_port *to,
854 const struct sk_buff *skb)
856 return BR_INPUT_SKB_CB(skb)->src_port_isolated &&
857 (to->flags & BR_ISOLATED);
861 void br_port_carrier_check(struct net_bridge_port *p, bool *notified);
862 int br_add_bridge(struct net *net, const char *name);
863 int br_del_bridge(struct net *net, const char *name);
864 int br_add_if(struct net_bridge *br, struct net_device *dev,
865 struct netlink_ext_ack *extack);
866 int br_del_if(struct net_bridge *br, struct net_device *dev);
867 void br_mtu_auto_adjust(struct net_bridge *br);
868 netdev_features_t br_features_recompute(struct net_bridge *br,
869 netdev_features_t features);
870 void br_port_flags_change(struct net_bridge_port *port, unsigned long mask);
871 void br_manage_promisc(struct net_bridge *br);
872 int nbp_backup_change(struct net_bridge_port *p, struct net_device *backup_dev);
875 int br_handle_frame_finish(struct net *net, struct sock *sk, struct sk_buff *skb);
876 rx_handler_func_t *br_get_rx_handler(const struct net_device *dev);
878 struct br_frame_type {
880 int (*frame_handler)(struct net_bridge_port *port,
881 struct sk_buff *skb);
882 struct hlist_node list;
885 void br_add_frame(struct net_bridge *br, struct br_frame_type *ft);
886 void br_del_frame(struct net_bridge *br, struct br_frame_type *ft);
888 static inline bool br_rx_handler_check_rcu(const struct net_device *dev)
890 return rcu_dereference(dev->rx_handler) == br_get_rx_handler(dev);
893 static inline bool br_rx_handler_check_rtnl(const struct net_device *dev)
895 return rcu_dereference_rtnl(dev->rx_handler) == br_get_rx_handler(dev);
898 static inline struct net_bridge_port *br_port_get_check_rcu(const struct net_device *dev)
900 return br_rx_handler_check_rcu(dev) ? br_port_get_rcu(dev) : NULL;
903 static inline struct net_bridge_port *
904 br_port_get_check_rtnl(const struct net_device *dev)
906 return br_rx_handler_check_rtnl(dev) ? br_port_get_rtnl_rcu(dev) : NULL;
910 int br_dev_siocdevprivate(struct net_device *dev, struct ifreq *rq,
911 void __user *data, int cmd);
912 int br_ioctl_stub(struct net *net, struct net_bridge *br, unsigned int cmd,
913 struct ifreq *ifr, void __user *uarg);
916 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
917 int br_multicast_rcv(struct net_bridge_mcast **brmctx,
918 struct net_bridge_mcast_port **pmctx,
919 struct net_bridge_vlan *vlan,
920 struct sk_buff *skb, u16 vid);
921 struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge_mcast *brmctx,
922 struct sk_buff *skb, u16 vid);
923 int br_multicast_add_port(struct net_bridge_port *port);
924 void br_multicast_del_port(struct net_bridge_port *port);
925 void br_multicast_enable_port(struct net_bridge_port *port);
926 void br_multicast_disable_port(struct net_bridge_port *port);
927 void br_multicast_init(struct net_bridge *br);
928 void br_multicast_join_snoopers(struct net_bridge *br);
929 void br_multicast_leave_snoopers(struct net_bridge *br);
930 void br_multicast_open(struct net_bridge *br);
931 void br_multicast_stop(struct net_bridge *br);
932 void br_multicast_dev_del(struct net_bridge *br);
933 void br_multicast_flood(struct net_bridge_mdb_entry *mdst, struct sk_buff *skb,
934 struct net_bridge_mcast *brmctx,
935 bool local_rcv, bool local_orig);
936 int br_multicast_set_router(struct net_bridge_mcast *brmctx, unsigned long val);
937 int br_multicast_set_port_router(struct net_bridge_mcast_port *pmctx,
939 int br_multicast_set_vlan_router(struct net_bridge_vlan *v, u8 mcast_router);
940 int br_multicast_toggle(struct net_bridge *br, unsigned long val,
941 struct netlink_ext_ack *extack);
942 int br_multicast_set_querier(struct net_bridge_mcast *brmctx, unsigned long val);
943 int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val);
944 int br_multicast_set_igmp_version(struct net_bridge_mcast *brmctx,
946 #if IS_ENABLED(CONFIG_IPV6)
947 int br_multicast_set_mld_version(struct net_bridge_mcast *brmctx,
950 struct net_bridge_mdb_entry *
951 br_mdb_ip_get(struct net_bridge *br, struct br_ip *dst);
952 struct net_bridge_mdb_entry *
953 br_multicast_new_group(struct net_bridge *br, struct br_ip *group);
954 struct net_bridge_port_group *
955 br_multicast_new_port_group(struct net_bridge_port *port,
956 const struct br_ip *group,
957 struct net_bridge_port_group __rcu *next,
958 unsigned char flags, const unsigned char *src,
959 u8 filter_mode, u8 rt_protocol);
960 int br_mdb_hash_init(struct net_bridge *br);
961 void br_mdb_hash_fini(struct net_bridge *br);
962 void br_mdb_notify(struct net_device *dev, struct net_bridge_mdb_entry *mp,
963 struct net_bridge_port_group *pg, int type);
964 void br_rtr_notify(struct net_device *dev, struct net_bridge_mcast_port *pmctx,
966 void br_multicast_del_pg(struct net_bridge_mdb_entry *mp,
967 struct net_bridge_port_group *pg,
968 struct net_bridge_port_group __rcu **pp);
969 void br_multicast_count(struct net_bridge *br,
970 const struct net_bridge_port *p,
971 const struct sk_buff *skb, u8 type, u8 dir);
972 int br_multicast_init_stats(struct net_bridge *br);
973 void br_multicast_uninit_stats(struct net_bridge *br);
974 void br_multicast_get_stats(const struct net_bridge *br,
975 const struct net_bridge_port *p,
976 struct br_mcast_stats *dest);
977 void br_mdb_init(void);
978 void br_mdb_uninit(void);
979 void br_multicast_host_join(const struct net_bridge_mcast *brmctx,
980 struct net_bridge_mdb_entry *mp, bool notify);
981 void br_multicast_host_leave(struct net_bridge_mdb_entry *mp, bool notify);
982 void br_multicast_star_g_handle_mode(struct net_bridge_port_group *pg,
984 void br_multicast_sg_add_exclude_ports(struct net_bridge_mdb_entry *star_mp,
985 struct net_bridge_port_group *sg);
986 struct net_bridge_group_src *
987 br_multicast_find_group_src(struct net_bridge_port_group *pg, struct br_ip *ip);
988 struct net_bridge_group_src *
989 br_multicast_new_group_src(struct net_bridge_port_group *pg,
990 struct br_ip *src_ip);
991 void __br_multicast_del_group_src(struct net_bridge_group_src *src);
992 void br_multicast_del_group_src(struct net_bridge_group_src *src,
994 void br_multicast_ctx_init(struct net_bridge *br,
995 struct net_bridge_vlan *vlan,
996 struct net_bridge_mcast *brmctx);
997 void br_multicast_ctx_deinit(struct net_bridge_mcast *brmctx);
998 void br_multicast_port_ctx_init(struct net_bridge_port *port,
999 struct net_bridge_vlan *vlan,
1000 struct net_bridge_mcast_port *pmctx);
1001 void br_multicast_port_ctx_deinit(struct net_bridge_mcast_port *pmctx);
1002 void br_multicast_toggle_one_vlan(struct net_bridge_vlan *vlan, bool on);
1003 int br_multicast_toggle_vlan_snooping(struct net_bridge *br, bool on,
1004 struct netlink_ext_ack *extack);
1005 bool br_multicast_toggle_global_vlan(struct net_bridge_vlan *vlan, bool on);
1007 int br_rports_fill_info(struct sk_buff *skb,
1008 const struct net_bridge_mcast *brmctx);
1009 int br_multicast_dump_querier_state(struct sk_buff *skb,
1010 const struct net_bridge_mcast *brmctx,
1012 size_t br_multicast_querier_state_size(void);
1013 size_t br_rports_size(const struct net_bridge_mcast *brmctx);
1014 void br_multicast_set_query_intvl(struct net_bridge_mcast *brmctx,
1016 void br_multicast_set_startup_query_intvl(struct net_bridge_mcast *brmctx,
1019 static inline bool br_group_is_l2(const struct br_ip *group)
1021 return group->proto == 0;
1024 #define mlock_dereference(X, br) \
1025 rcu_dereference_protected(X, lockdep_is_held(&br->multicast_lock))
1027 static inline struct hlist_node *
1028 br_multicast_get_first_rport_node(struct net_bridge_mcast *brmctx,
1029 struct sk_buff *skb)
1031 #if IS_ENABLED(CONFIG_IPV6)
1032 if (skb->protocol == htons(ETH_P_IPV6))
1033 return rcu_dereference(hlist_first_rcu(&brmctx->ip6_mc_router_list));
1035 return rcu_dereference(hlist_first_rcu(&brmctx->ip4_mc_router_list));
1038 static inline struct net_bridge_port *
1039 br_multicast_rport_from_node_skb(struct hlist_node *rp, struct sk_buff *skb)
1041 struct net_bridge_mcast_port *mctx;
1043 #if IS_ENABLED(CONFIG_IPV6)
1044 if (skb->protocol == htons(ETH_P_IPV6))
1045 mctx = hlist_entry_safe(rp, struct net_bridge_mcast_port,
1049 mctx = hlist_entry_safe(rp, struct net_bridge_mcast_port,
1058 static inline bool br_ip4_multicast_is_router(struct net_bridge_mcast *brmctx)
1060 return timer_pending(&brmctx->ip4_mc_router_timer);
1063 static inline bool br_ip6_multicast_is_router(struct net_bridge_mcast *brmctx)
1065 #if IS_ENABLED(CONFIG_IPV6)
1066 return timer_pending(&brmctx->ip6_mc_router_timer);
1073 br_multicast_is_router(struct net_bridge_mcast *brmctx, struct sk_buff *skb)
1075 switch (brmctx->multicast_router) {
1076 case MDB_RTR_TYPE_PERM:
1078 case MDB_RTR_TYPE_TEMP_QUERY:
1080 if (skb->protocol == htons(ETH_P_IP))
1081 return br_ip4_multicast_is_router(brmctx);
1082 else if (skb->protocol == htons(ETH_P_IPV6))
1083 return br_ip6_multicast_is_router(brmctx);
1085 return br_ip4_multicast_is_router(brmctx) ||
1086 br_ip6_multicast_is_router(brmctx);
1095 __br_multicast_querier_exists(struct net_bridge_mcast *brmctx,
1096 struct bridge_mcast_other_query *querier,
1099 bool own_querier_enabled;
1101 if (brmctx->multicast_querier) {
1102 if (is_ipv6 && !br_opt_get(brmctx->br, BROPT_HAS_IPV6_ADDR))
1103 own_querier_enabled = false;
1105 own_querier_enabled = true;
1107 own_querier_enabled = false;
1110 return time_is_before_jiffies(querier->delay_time) &&
1111 (own_querier_enabled || timer_pending(&querier->timer));
1114 static inline bool br_multicast_querier_exists(struct net_bridge_mcast *brmctx,
1116 const struct net_bridge_mdb_entry *mdb)
1118 switch (eth->h_proto) {
1119 case (htons(ETH_P_IP)):
1120 return __br_multicast_querier_exists(brmctx,
1121 &brmctx->ip4_other_query, false);
1122 #if IS_ENABLED(CONFIG_IPV6)
1123 case (htons(ETH_P_IPV6)):
1124 return __br_multicast_querier_exists(brmctx,
1125 &brmctx->ip6_other_query, true);
1128 return !!mdb && br_group_is_l2(&mdb->addr);
1132 static inline bool br_multicast_is_star_g(const struct br_ip *ip)
1134 switch (ip->proto) {
1135 case htons(ETH_P_IP):
1136 return ipv4_is_zeronet(ip->src.ip4);
1137 #if IS_ENABLED(CONFIG_IPV6)
1138 case htons(ETH_P_IPV6):
1139 return ipv6_addr_any(&ip->src.ip6);
1147 br_multicast_should_handle_mode(const struct net_bridge_mcast *brmctx,
1151 case htons(ETH_P_IP):
1152 return !!(brmctx->multicast_igmp_version == 3);
1153 #if IS_ENABLED(CONFIG_IPV6)
1154 case htons(ETH_P_IPV6):
1155 return !!(brmctx->multicast_mld_version == 2);
1162 static inline int br_multicast_igmp_type(const struct sk_buff *skb)
1164 return BR_INPUT_SKB_CB(skb)->igmp;
1167 static inline unsigned long br_multicast_lmqt(const struct net_bridge_mcast *brmctx)
1169 return brmctx->multicast_last_member_interval *
1170 brmctx->multicast_last_member_count;
1173 static inline unsigned long br_multicast_gmi(const struct net_bridge_mcast *brmctx)
1175 return brmctx->multicast_membership_interval;
1179 br_multicast_ctx_is_vlan(const struct net_bridge_mcast *brmctx)
1181 return !!brmctx->vlan;
1185 br_multicast_port_ctx_is_vlan(const struct net_bridge_mcast_port *pmctx)
1187 return !!pmctx->vlan;
1190 static inline struct net_bridge_mcast *
1191 br_multicast_port_ctx_get_global(const struct net_bridge_mcast_port *pmctx)
1193 if (!br_multicast_port_ctx_is_vlan(pmctx))
1194 return &pmctx->port->br->multicast_ctx;
1196 return &pmctx->vlan->brvlan->br_mcast_ctx;
1200 br_multicast_ctx_vlan_global_disabled(const struct net_bridge_mcast *brmctx)
1202 return br_multicast_ctx_is_vlan(brmctx) &&
1203 (!br_opt_get(brmctx->br, BROPT_MCAST_VLAN_SNOOPING_ENABLED) ||
1204 !(brmctx->vlan->priv_flags & BR_VLFLAG_GLOBAL_MCAST_ENABLED));
1208 br_multicast_ctx_vlan_disabled(const struct net_bridge_mcast *brmctx)
1210 return br_multicast_ctx_is_vlan(brmctx) &&
1211 !(brmctx->vlan->priv_flags & BR_VLFLAG_MCAST_ENABLED);
1215 br_multicast_port_ctx_vlan_disabled(const struct net_bridge_mcast_port *pmctx)
1217 return br_multicast_port_ctx_is_vlan(pmctx) &&
1218 !(pmctx->vlan->priv_flags & BR_VLFLAG_MCAST_ENABLED);
1222 br_multicast_port_ctx_state_disabled(const struct net_bridge_mcast_port *pmctx)
1224 return pmctx->port->state == BR_STATE_DISABLED ||
1225 (br_multicast_port_ctx_is_vlan(pmctx) &&
1226 (br_multicast_port_ctx_vlan_disabled(pmctx) ||
1227 pmctx->vlan->state == BR_STATE_DISABLED));
1231 br_multicast_port_ctx_state_stopped(const struct net_bridge_mcast_port *pmctx)
1233 return br_multicast_port_ctx_state_disabled(pmctx) ||
1234 pmctx->port->state == BR_STATE_BLOCKING ||
1235 (br_multicast_port_ctx_is_vlan(pmctx) &&
1236 pmctx->vlan->state == BR_STATE_BLOCKING);
1240 br_rports_have_mc_router(const struct net_bridge_mcast *brmctx)
1242 #if IS_ENABLED(CONFIG_IPV6)
1243 return !hlist_empty(&brmctx->ip4_mc_router_list) ||
1244 !hlist_empty(&brmctx->ip6_mc_router_list);
1246 return !hlist_empty(&brmctx->ip4_mc_router_list);
1251 br_multicast_ctx_options_equal(const struct net_bridge_mcast *brmctx1,
1252 const struct net_bridge_mcast *brmctx2)
1254 return brmctx1->multicast_igmp_version ==
1255 brmctx2->multicast_igmp_version &&
1256 brmctx1->multicast_last_member_count ==
1257 brmctx2->multicast_last_member_count &&
1258 brmctx1->multicast_startup_query_count ==
1259 brmctx2->multicast_startup_query_count &&
1260 brmctx1->multicast_last_member_interval ==
1261 brmctx2->multicast_last_member_interval &&
1262 brmctx1->multicast_membership_interval ==
1263 brmctx2->multicast_membership_interval &&
1264 brmctx1->multicast_querier_interval ==
1265 brmctx2->multicast_querier_interval &&
1266 brmctx1->multicast_query_interval ==
1267 brmctx2->multicast_query_interval &&
1268 brmctx1->multicast_query_response_interval ==
1269 brmctx2->multicast_query_response_interval &&
1270 brmctx1->multicast_startup_query_interval ==
1271 brmctx2->multicast_startup_query_interval &&
1272 brmctx1->multicast_querier == brmctx2->multicast_querier &&
1273 brmctx1->multicast_router == brmctx2->multicast_router &&
1274 !br_rports_have_mc_router(brmctx1) &&
1275 !br_rports_have_mc_router(brmctx2) &&
1276 #if IS_ENABLED(CONFIG_IPV6)
1277 brmctx1->multicast_mld_version ==
1278 brmctx2->multicast_mld_version &&
1284 br_multicast_ctx_matches_vlan_snooping(const struct net_bridge_mcast *brmctx)
1286 bool vlan_snooping_enabled;
1288 vlan_snooping_enabled = !!br_opt_get(brmctx->br,
1289 BROPT_MCAST_VLAN_SNOOPING_ENABLED);
1291 return !!(vlan_snooping_enabled == br_multicast_ctx_is_vlan(brmctx));
1294 static inline int br_multicast_rcv(struct net_bridge_mcast **brmctx,
1295 struct net_bridge_mcast_port **pmctx,
1296 struct net_bridge_vlan *vlan,
1297 struct sk_buff *skb,
1303 static inline struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge_mcast *brmctx,
1304 struct sk_buff *skb, u16 vid)
1309 static inline int br_multicast_add_port(struct net_bridge_port *port)
1314 static inline void br_multicast_del_port(struct net_bridge_port *port)
1318 static inline void br_multicast_enable_port(struct net_bridge_port *port)
1322 static inline void br_multicast_disable_port(struct net_bridge_port *port)
1326 static inline void br_multicast_init(struct net_bridge *br)
1330 static inline void br_multicast_join_snoopers(struct net_bridge *br)
1334 static inline void br_multicast_leave_snoopers(struct net_bridge *br)
1338 static inline void br_multicast_open(struct net_bridge *br)
1342 static inline void br_multicast_stop(struct net_bridge *br)
1346 static inline void br_multicast_dev_del(struct net_bridge *br)
1350 static inline void br_multicast_flood(struct net_bridge_mdb_entry *mdst,
1351 struct sk_buff *skb,
1352 struct net_bridge_mcast *brmctx,
1353 bool local_rcv, bool local_orig)
1357 static inline bool br_multicast_is_router(struct net_bridge_mcast *brmctx,
1358 struct sk_buff *skb)
1363 static inline bool br_multicast_querier_exists(struct net_bridge_mcast *brmctx,
1365 const struct net_bridge_mdb_entry *mdb)
1370 static inline void br_mdb_init(void)
1374 static inline void br_mdb_uninit(void)
1378 static inline int br_mdb_hash_init(struct net_bridge *br)
1383 static inline void br_mdb_hash_fini(struct net_bridge *br)
1387 static inline void br_multicast_count(struct net_bridge *br,
1388 const struct net_bridge_port *p,
1389 const struct sk_buff *skb,
1394 static inline int br_multicast_init_stats(struct net_bridge *br)
1399 static inline void br_multicast_uninit_stats(struct net_bridge *br)
1403 static inline int br_multicast_igmp_type(const struct sk_buff *skb)
1408 static inline void br_multicast_ctx_init(struct net_bridge *br,
1409 struct net_bridge_vlan *vlan,
1410 struct net_bridge_mcast *brmctx)
1414 static inline void br_multicast_ctx_deinit(struct net_bridge_mcast *brmctx)
1418 static inline void br_multicast_port_ctx_init(struct net_bridge_port *port,
1419 struct net_bridge_vlan *vlan,
1420 struct net_bridge_mcast_port *pmctx)
1424 static inline void br_multicast_port_ctx_deinit(struct net_bridge_mcast_port *pmctx)
1428 static inline void br_multicast_toggle_one_vlan(struct net_bridge_vlan *vlan,
1433 static inline int br_multicast_toggle_vlan_snooping(struct net_bridge *br,
1435 struct netlink_ext_ack *extack)
1440 static inline bool br_multicast_toggle_global_vlan(struct net_bridge_vlan *vlan,
1447 br_multicast_ctx_options_equal(const struct net_bridge_mcast *brmctx1,
1448 const struct net_bridge_mcast *brmctx2)
1455 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
1456 bool br_allowed_ingress(const struct net_bridge *br,
1457 struct net_bridge_vlan_group *vg, struct sk_buff *skb,
1458 u16 *vid, u8 *state,
1459 struct net_bridge_vlan **vlan);
1460 bool br_allowed_egress(struct net_bridge_vlan_group *vg,
1461 const struct sk_buff *skb);
1462 bool br_should_learn(struct net_bridge_port *p, struct sk_buff *skb, u16 *vid);
1463 struct sk_buff *br_handle_vlan(struct net_bridge *br,
1464 const struct net_bridge_port *port,
1465 struct net_bridge_vlan_group *vg,
1466 struct sk_buff *skb);
1467 int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags,
1468 bool *changed, struct netlink_ext_ack *extack);
1469 int br_vlan_delete(struct net_bridge *br, u16 vid);
1470 void br_vlan_flush(struct net_bridge *br);
1471 struct net_bridge_vlan *br_vlan_find(struct net_bridge_vlan_group *vg, u16 vid);
1472 void br_recalculate_fwd_mask(struct net_bridge *br);
1473 int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val,
1474 struct netlink_ext_ack *extack);
1475 int __br_vlan_set_proto(struct net_bridge *br, __be16 proto,
1476 struct netlink_ext_ack *extack);
1477 int br_vlan_set_proto(struct net_bridge *br, unsigned long val,
1478 struct netlink_ext_ack *extack);
1479 int br_vlan_set_stats(struct net_bridge *br, unsigned long val);
1480 int br_vlan_set_stats_per_port(struct net_bridge *br, unsigned long val);
1481 int br_vlan_init(struct net_bridge *br);
1482 int br_vlan_set_default_pvid(struct net_bridge *br, unsigned long val,
1483 struct netlink_ext_ack *extack);
1484 int __br_vlan_set_default_pvid(struct net_bridge *br, u16 pvid,
1485 struct netlink_ext_ack *extack);
1486 int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags,
1487 bool *changed, struct netlink_ext_ack *extack);
1488 int nbp_vlan_delete(struct net_bridge_port *port, u16 vid);
1489 void nbp_vlan_flush(struct net_bridge_port *port);
1490 int nbp_vlan_init(struct net_bridge_port *port, struct netlink_ext_ack *extack);
1491 int nbp_get_num_vlan_infos(struct net_bridge_port *p, u32 filter_mask);
1492 void br_vlan_get_stats(const struct net_bridge_vlan *v,
1493 struct pcpu_sw_netstats *stats);
1494 void br_vlan_port_event(struct net_bridge_port *p, unsigned long event);
1495 int br_vlan_bridge_event(struct net_device *dev, unsigned long event,
1497 void br_vlan_rtnl_init(void);
1498 void br_vlan_rtnl_uninit(void);
1499 void br_vlan_notify(const struct net_bridge *br,
1500 const struct net_bridge_port *p,
1501 u16 vid, u16 vid_range,
1503 bool br_vlan_can_enter_range(const struct net_bridge_vlan *v_curr,
1504 const struct net_bridge_vlan *range_end);
1506 void br_vlan_fill_forward_path_pvid(struct net_bridge *br,
1507 struct net_device_path_ctx *ctx,
1508 struct net_device_path *path);
1509 int br_vlan_fill_forward_path_mode(struct net_bridge *br,
1510 struct net_bridge_port *dst,
1511 struct net_device_path *path);
1513 static inline struct net_bridge_vlan_group *br_vlan_group(
1514 const struct net_bridge *br)
1516 return rtnl_dereference(br->vlgrp);
1519 static inline struct net_bridge_vlan_group *nbp_vlan_group(
1520 const struct net_bridge_port *p)
1522 return rtnl_dereference(p->vlgrp);
1525 static inline struct net_bridge_vlan_group *br_vlan_group_rcu(
1526 const struct net_bridge *br)
1528 return rcu_dereference(br->vlgrp);
1531 static inline struct net_bridge_vlan_group *nbp_vlan_group_rcu(
1532 const struct net_bridge_port *p)
1534 return rcu_dereference(p->vlgrp);
1537 /* Since bridge now depends on 8021Q module, but the time bridge sees the
1538 * skb, the vlan tag will always be present if the frame was tagged.
1540 static inline int br_vlan_get_tag(const struct sk_buff *skb, u16 *vid)
1544 if (skb_vlan_tag_present(skb)) {
1545 *vid = skb_vlan_tag_get_id(skb);
1554 static inline u16 br_get_pvid(const struct net_bridge_vlan_group *vg)
1563 static inline u16 br_vlan_flags(const struct net_bridge_vlan *v, u16 pvid)
1565 return v->vid == pvid ? v->flags | BRIDGE_VLAN_INFO_PVID : v->flags;
1568 static inline bool br_allowed_ingress(const struct net_bridge *br,
1569 struct net_bridge_vlan_group *vg,
1570 struct sk_buff *skb,
1571 u16 *vid, u8 *state,
1572 struct net_bridge_vlan **vlan)
1579 static inline bool br_allowed_egress(struct net_bridge_vlan_group *vg,
1580 const struct sk_buff *skb)
1585 static inline bool br_should_learn(struct net_bridge_port *p,
1586 struct sk_buff *skb, u16 *vid)
1591 static inline struct sk_buff *br_handle_vlan(struct net_bridge *br,
1592 const struct net_bridge_port *port,
1593 struct net_bridge_vlan_group *vg,
1594 struct sk_buff *skb)
1599 static inline int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags,
1600 bool *changed, struct netlink_ext_ack *extack)
1606 static inline int br_vlan_delete(struct net_bridge *br, u16 vid)
1611 static inline void br_vlan_flush(struct net_bridge *br)
1615 static inline void br_recalculate_fwd_mask(struct net_bridge *br)
1619 static inline int br_vlan_init(struct net_bridge *br)
1624 static inline int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags,
1625 bool *changed, struct netlink_ext_ack *extack)
1631 static inline int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
1636 static inline void nbp_vlan_flush(struct net_bridge_port *port)
1640 static inline struct net_bridge_vlan *br_vlan_find(struct net_bridge_vlan_group *vg,
1646 static inline int nbp_vlan_init(struct net_bridge_port *port,
1647 struct netlink_ext_ack *extack)
1652 static inline u16 br_vlan_get_tag(const struct sk_buff *skb, u16 *tag)
1657 static inline u16 br_get_pvid(const struct net_bridge_vlan_group *vg)
1662 static inline int br_vlan_filter_toggle(struct net_bridge *br,
1664 struct netlink_ext_ack *extack)
1669 static inline int nbp_get_num_vlan_infos(struct net_bridge_port *p,
1675 static inline void br_vlan_fill_forward_path_pvid(struct net_bridge *br,
1676 struct net_device_path_ctx *ctx,
1677 struct net_device_path *path)
1681 static inline int br_vlan_fill_forward_path_mode(struct net_bridge *br,
1682 struct net_bridge_port *dst,
1683 struct net_device_path *path)
1688 static inline struct net_bridge_vlan_group *br_vlan_group(
1689 const struct net_bridge *br)
1694 static inline struct net_bridge_vlan_group *nbp_vlan_group(
1695 const struct net_bridge_port *p)
1700 static inline struct net_bridge_vlan_group *br_vlan_group_rcu(
1701 const struct net_bridge *br)
1706 static inline struct net_bridge_vlan_group *nbp_vlan_group_rcu(
1707 const struct net_bridge_port *p)
1712 static inline void br_vlan_get_stats(const struct net_bridge_vlan *v,
1713 struct pcpu_sw_netstats *stats)
1717 static inline void br_vlan_port_event(struct net_bridge_port *p,
1718 unsigned long event)
1722 static inline int br_vlan_bridge_event(struct net_device *dev,
1723 unsigned long event, void *ptr)
1728 static inline void br_vlan_rtnl_init(void)
1732 static inline void br_vlan_rtnl_uninit(void)
1736 static inline void br_vlan_notify(const struct net_bridge *br,
1737 const struct net_bridge_port *p,
1738 u16 vid, u16 vid_range,
1743 static inline bool br_vlan_can_enter_range(const struct net_bridge_vlan *v_curr,
1744 const struct net_bridge_vlan *range_end)
1749 static inline u16 br_vlan_flags(const struct net_bridge_vlan *v, u16 pvid)
1756 /* br_vlan_options.c */
1757 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
1758 bool br_vlan_opts_eq_range(const struct net_bridge_vlan *v_curr,
1759 const struct net_bridge_vlan *range_end);
1760 bool br_vlan_opts_fill(struct sk_buff *skb, const struct net_bridge_vlan *v);
1761 size_t br_vlan_opts_nl_size(void);
1762 int br_vlan_process_options(const struct net_bridge *br,
1763 const struct net_bridge_port *p,
1764 struct net_bridge_vlan *range_start,
1765 struct net_bridge_vlan *range_end,
1767 struct netlink_ext_ack *extack);
1768 int br_vlan_rtm_process_global_options(struct net_device *dev,
1769 const struct nlattr *attr,
1771 struct netlink_ext_ack *extack);
1772 bool br_vlan_global_opts_can_enter_range(const struct net_bridge_vlan *v_curr,
1773 const struct net_bridge_vlan *r_end);
1774 bool br_vlan_global_opts_fill(struct sk_buff *skb, u16 vid, u16 vid_range,
1775 const struct net_bridge_vlan *v_opts);
1777 /* vlan state manipulation helpers using *_ONCE to annotate lock-free access */
1778 static inline u8 br_vlan_get_state(const struct net_bridge_vlan *v)
1780 return READ_ONCE(v->state);
1783 static inline void br_vlan_set_state(struct net_bridge_vlan *v, u8 state)
1785 WRITE_ONCE(v->state, state);
1788 static inline u8 br_vlan_get_pvid_state(const struct net_bridge_vlan_group *vg)
1790 return READ_ONCE(vg->pvid_state);
1793 static inline void br_vlan_set_pvid_state(struct net_bridge_vlan_group *vg,
1796 WRITE_ONCE(vg->pvid_state, state);
1799 /* learn_allow is true at ingress and false at egress */
1800 static inline bool br_vlan_state_allowed(u8 state, bool learn_allow)
1803 case BR_STATE_LEARNING:
1805 case BR_STATE_FORWARDING:
1814 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
1815 DECLARE_STATIC_KEY_FALSE(br_mst_used);
1816 static inline bool br_mst_is_enabled(struct net_bridge *br)
1818 return static_branch_unlikely(&br_mst_used) &&
1819 br_opt_get(br, BROPT_MST_ENABLED);
1822 int br_mst_set_state(struct net_bridge_port *p, u16 msti, u8 state,
1823 struct netlink_ext_ack *extack);
1824 int br_mst_vlan_set_msti(struct net_bridge_vlan *v, u16 msti);
1825 void br_mst_vlan_init_state(struct net_bridge_vlan *v);
1826 int br_mst_set_enabled(struct net_bridge *br, bool on,
1827 struct netlink_ext_ack *extack);
1828 size_t br_mst_info_size(const struct net_bridge_vlan_group *vg);
1829 int br_mst_fill_info(struct sk_buff *skb,
1830 const struct net_bridge_vlan_group *vg);
1831 int br_mst_process(struct net_bridge_port *p, const struct nlattr *mst_attr,
1832 struct netlink_ext_ack *extack);
1834 static inline bool br_mst_is_enabled(struct net_bridge *br)
1839 static inline int br_mst_set_state(struct net_bridge_port *p, u16 msti,
1840 u8 state, struct netlink_ext_ack *extack)
1845 static inline int br_mst_set_enabled(struct net_bridge *br, bool on,
1846 struct netlink_ext_ack *extack)
1851 static inline size_t br_mst_info_size(const struct net_bridge_vlan_group *vg)
1856 static inline int br_mst_fill_info(struct sk_buff *skb,
1857 const struct net_bridge_vlan_group *vg)
1862 static inline int br_mst_process(struct net_bridge_port *p,
1863 const struct nlattr *mst_attr,
1864 struct netlink_ext_ack *extack)
1871 int (*br_dev_xmit_hook)(struct sk_buff *skb);
1873 extern const struct nf_br_ops __rcu *nf_br_ops;
1875 /* br_netfilter.c */
1876 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1877 int br_nf_core_init(void);
1878 void br_nf_core_fini(void);
1879 void br_netfilter_rtable_init(struct net_bridge *);
1881 static inline int br_nf_core_init(void) { return 0; }
1882 static inline void br_nf_core_fini(void) {}
1883 #define br_netfilter_rtable_init(x)
1887 void br_set_state(struct net_bridge_port *p, unsigned int state);
1888 struct net_bridge_port *br_get_port(struct net_bridge *br, u16 port_no);
1889 void br_init_port(struct net_bridge_port *p);
1890 void br_become_designated_port(struct net_bridge_port *p);
1892 void __br_set_forward_delay(struct net_bridge *br, unsigned long t);
1893 int br_set_forward_delay(struct net_bridge *br, unsigned long x);
1894 int br_set_hello_time(struct net_bridge *br, unsigned long x);
1895 int br_set_max_age(struct net_bridge *br, unsigned long x);
1896 int __set_ageing_time(struct net_device *dev, unsigned long t);
1897 int br_set_ageing_time(struct net_bridge *br, clock_t ageing_time);
1901 void br_stp_enable_bridge(struct net_bridge *br);
1902 void br_stp_disable_bridge(struct net_bridge *br);
1903 int br_stp_set_enabled(struct net_bridge *br, unsigned long val,
1904 struct netlink_ext_ack *extack);
1905 void br_stp_enable_port(struct net_bridge_port *p);
1906 void br_stp_disable_port(struct net_bridge_port *p);
1907 bool br_stp_recalculate_bridge_id(struct net_bridge *br);
1908 void br_stp_change_bridge_id(struct net_bridge *br, const unsigned char *a);
1909 void br_stp_set_bridge_priority(struct net_bridge *br, u16 newprio);
1910 int br_stp_set_port_priority(struct net_bridge_port *p, unsigned long newprio);
1911 int br_stp_set_path_cost(struct net_bridge_port *p, unsigned long path_cost);
1912 ssize_t br_show_bridge_id(char *buf, const struct bridge_id *id);
1916 void br_stp_rcv(const struct stp_proto *proto, struct sk_buff *skb,
1917 struct net_device *dev);
1919 /* br_stp_timer.c */
1920 void br_stp_timer_init(struct net_bridge *br);
1921 void br_stp_port_timer_init(struct net_bridge_port *p);
1922 unsigned long br_timer_value(const struct timer_list *timer);
1925 #if IS_ENABLED(CONFIG_ATM_LANE)
1926 extern int (*br_fdb_test_addr_hook)(struct net_device *dev, unsigned char *addr);
1930 #if IS_ENABLED(CONFIG_BRIDGE_MRP)
1931 int br_mrp_parse(struct net_bridge *br, struct net_bridge_port *p,
1932 struct nlattr *attr, int cmd, struct netlink_ext_ack *extack);
1933 bool br_mrp_enabled(struct net_bridge *br);
1934 void br_mrp_port_del(struct net_bridge *br, struct net_bridge_port *p);
1935 int br_mrp_fill_info(struct sk_buff *skb, struct net_bridge *br);
1937 static inline int br_mrp_parse(struct net_bridge *br, struct net_bridge_port *p,
1938 struct nlattr *attr, int cmd,
1939 struct netlink_ext_ack *extack)
1944 static inline bool br_mrp_enabled(struct net_bridge *br)
1949 static inline void br_mrp_port_del(struct net_bridge *br,
1950 struct net_bridge_port *p)
1954 static inline int br_mrp_fill_info(struct sk_buff *skb, struct net_bridge *br)
1962 #if IS_ENABLED(CONFIG_BRIDGE_CFM)
1963 int br_cfm_parse(struct net_bridge *br, struct net_bridge_port *p,
1964 struct nlattr *attr, int cmd, struct netlink_ext_ack *extack);
1965 bool br_cfm_created(struct net_bridge *br);
1966 void br_cfm_port_del(struct net_bridge *br, struct net_bridge_port *p);
1967 int br_cfm_config_fill_info(struct sk_buff *skb, struct net_bridge *br);
1968 int br_cfm_status_fill_info(struct sk_buff *skb,
1969 struct net_bridge *br,
1971 int br_cfm_mep_count(struct net_bridge *br, u32 *count);
1972 int br_cfm_peer_mep_count(struct net_bridge *br, u32 *count);
1974 static inline int br_cfm_parse(struct net_bridge *br, struct net_bridge_port *p,
1975 struct nlattr *attr, int cmd,
1976 struct netlink_ext_ack *extack)
1981 static inline bool br_cfm_created(struct net_bridge *br)
1986 static inline void br_cfm_port_del(struct net_bridge *br,
1987 struct net_bridge_port *p)
1991 static inline int br_cfm_config_fill_info(struct sk_buff *skb, struct net_bridge *br)
1996 static inline int br_cfm_status_fill_info(struct sk_buff *skb,
1997 struct net_bridge *br,
2003 static inline int br_cfm_mep_count(struct net_bridge *br, u32 *count)
2009 static inline int br_cfm_peer_mep_count(struct net_bridge *br, u32 *count)
2017 extern struct rtnl_link_ops br_link_ops;
2018 int br_netlink_init(void);
2019 void br_netlink_fini(void);
2020 void br_ifinfo_notify(int event, const struct net_bridge *br,
2021 const struct net_bridge_port *port);
2022 void br_info_notify(int event, const struct net_bridge *br,
2023 const struct net_bridge_port *port, u32 filter);
2024 int br_setlink(struct net_device *dev, struct nlmsghdr *nlmsg, u16 flags,
2025 struct netlink_ext_ack *extack);
2026 int br_dellink(struct net_device *dev, struct nlmsghdr *nlmsg, u16 flags);
2027 int br_getlink(struct sk_buff *skb, u32 pid, u32 seq, struct net_device *dev,
2028 u32 filter_mask, int nlflags);
2029 int br_process_vlan_info(struct net_bridge *br,
2030 struct net_bridge_port *p, int cmd,
2031 struct bridge_vlan_info *vinfo_curr,
2032 struct bridge_vlan_info **vinfo_last,
2034 struct netlink_ext_ack *extack);
2038 extern const struct sysfs_ops brport_sysfs_ops;
2039 int br_sysfs_addif(struct net_bridge_port *p);
2040 int br_sysfs_renameif(struct net_bridge_port *p);
2043 int br_sysfs_addbr(struct net_device *dev);
2044 void br_sysfs_delbr(struct net_device *dev);
2048 static inline int br_sysfs_addif(struct net_bridge_port *p) { return 0; }
2049 static inline int br_sysfs_renameif(struct net_bridge_port *p) { return 0; }
2050 static inline int br_sysfs_addbr(struct net_device *dev) { return 0; }
2051 static inline void br_sysfs_delbr(struct net_device *dev) { return; }
2052 #endif /* CONFIG_SYSFS */
2054 /* br_switchdev.c */
2055 #ifdef CONFIG_NET_SWITCHDEV
2056 int br_switchdev_port_offload(struct net_bridge_port *p,
2057 struct net_device *dev, const void *ctx,
2058 struct notifier_block *atomic_nb,
2059 struct notifier_block *blocking_nb,
2060 bool tx_fwd_offload,
2061 struct netlink_ext_ack *extack);
2063 void br_switchdev_port_unoffload(struct net_bridge_port *p, const void *ctx,
2064 struct notifier_block *atomic_nb,
2065 struct notifier_block *blocking_nb);
2067 bool br_switchdev_frame_uses_tx_fwd_offload(struct sk_buff *skb);
2069 void br_switchdev_frame_set_offload_fwd_mark(struct sk_buff *skb);
2071 void nbp_switchdev_frame_mark_tx_fwd_offload(const struct net_bridge_port *p,
2072 struct sk_buff *skb);
2073 void nbp_switchdev_frame_mark_tx_fwd_to_hwdom(const struct net_bridge_port *p,
2074 struct sk_buff *skb);
2075 void nbp_switchdev_frame_mark(const struct net_bridge_port *p,
2076 struct sk_buff *skb);
2077 bool nbp_switchdev_allowed_egress(const struct net_bridge_port *p,
2078 const struct sk_buff *skb);
2079 int br_switchdev_set_port_flag(struct net_bridge_port *p,
2080 unsigned long flags,
2082 struct netlink_ext_ack *extack);
2083 void br_switchdev_fdb_notify(struct net_bridge *br,
2084 const struct net_bridge_fdb_entry *fdb, int type);
2085 void br_switchdev_mdb_notify(struct net_device *dev,
2086 struct net_bridge_mdb_entry *mp,
2087 struct net_bridge_port_group *pg,
2089 int br_switchdev_port_vlan_add(struct net_device *dev, u16 vid, u16 flags,
2090 bool changed, struct netlink_ext_ack *extack);
2091 int br_switchdev_port_vlan_del(struct net_device *dev, u16 vid);
2092 void br_switchdev_init(struct net_bridge *br);
2094 static inline void br_switchdev_frame_unmark(struct sk_buff *skb)
2096 skb->offload_fwd_mark = 0;
2100 br_switchdev_port_offload(struct net_bridge_port *p,
2101 struct net_device *dev, const void *ctx,
2102 struct notifier_block *atomic_nb,
2103 struct notifier_block *blocking_nb,
2104 bool tx_fwd_offload,
2105 struct netlink_ext_ack *extack)
2111 br_switchdev_port_unoffload(struct net_bridge_port *p, const void *ctx,
2112 struct notifier_block *atomic_nb,
2113 struct notifier_block *blocking_nb)
2117 static inline bool br_switchdev_frame_uses_tx_fwd_offload(struct sk_buff *skb)
2122 static inline void br_switchdev_frame_set_offload_fwd_mark(struct sk_buff *skb)
2127 nbp_switchdev_frame_mark_tx_fwd_offload(const struct net_bridge_port *p,
2128 struct sk_buff *skb)
2133 nbp_switchdev_frame_mark_tx_fwd_to_hwdom(const struct net_bridge_port *p,
2134 struct sk_buff *skb)
2138 static inline void nbp_switchdev_frame_mark(const struct net_bridge_port *p,
2139 struct sk_buff *skb)
2143 static inline bool nbp_switchdev_allowed_egress(const struct net_bridge_port *p,
2144 const struct sk_buff *skb)
2149 static inline int br_switchdev_set_port_flag(struct net_bridge_port *p,
2150 unsigned long flags,
2152 struct netlink_ext_ack *extack)
2157 static inline int br_switchdev_port_vlan_add(struct net_device *dev, u16 vid,
2158 u16 flags, bool changed,
2159 struct netlink_ext_ack *extack)
2164 static inline int br_switchdev_port_vlan_del(struct net_device *dev, u16 vid)
2170 br_switchdev_fdb_notify(struct net_bridge *br,
2171 const struct net_bridge_fdb_entry *fdb, int type)
2175 static inline void br_switchdev_mdb_notify(struct net_device *dev,
2176 struct net_bridge_mdb_entry *mp,
2177 struct net_bridge_port_group *pg,
2182 static inline void br_switchdev_frame_unmark(struct sk_buff *skb)
2186 static inline void br_switchdev_init(struct net_bridge *br)
2190 #endif /* CONFIG_NET_SWITCHDEV */
2192 /* br_arp_nd_proxy.c */
2193 void br_recalculate_neigh_suppress_enabled(struct net_bridge *br);
2194 void br_do_proxy_suppress_arp(struct sk_buff *skb, struct net_bridge *br,
2195 u16 vid, struct net_bridge_port *p);
2196 void br_do_suppress_nd(struct sk_buff *skb, struct net_bridge *br,
2197 u16 vid, struct net_bridge_port *p, struct nd_msg *msg);
2198 struct nd_msg *br_is_nd_neigh_msg(struct sk_buff *skb, struct nd_msg *m);