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
32 #define BR_HWDOM_MAX BITS_PER_LONG
34 #define BR_VERSION "2.3"
36 /* Control of forwarding link local multicast */
37 #define BR_GROUPFWD_DEFAULT 0
38 /* Don't allow forwarding of control protocols like STP, MAC PAUSE and LACP */
40 BR_GROUPFWD_STP = BIT(0),
41 BR_GROUPFWD_MACPAUSE = BIT(1),
42 BR_GROUPFWD_LACP = BIT(2),
45 #define BR_GROUPFWD_RESTRICTED (BR_GROUPFWD_STP | BR_GROUPFWD_MACPAUSE | \
47 /* The Nearest Customer Bridge Group Address, 01-80-C2-00-00-[00,0B,0C,0D,0F] */
48 #define BR_GROUPFWD_8021AD 0xB801u
50 /* Path to usermode spanning tree program */
51 #define BR_STP_PROG "/sbin/bridge-stp"
53 #define BR_FDB_NOTIFY_SETTABLE_BITS (FDB_NOTIFY_BIT | FDB_NOTIFY_INACTIVE_BIT)
55 typedef struct bridge_id bridge_id;
56 typedef struct mac_addr mac_addr;
57 typedef __u16 port_id;
60 unsigned char prio[2];
61 unsigned char addr[ETH_ALEN];
65 unsigned char addr[ETH_ALEN];
68 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
70 struct bridge_mcast_own_query {
71 struct timer_list timer;
76 struct bridge_mcast_other_query {
77 struct timer_list timer;
78 unsigned long delay_time;
81 /* selected querier */
82 struct bridge_mcast_querier {
85 seqcount_spinlock_t seq;
88 /* IGMP/MLD statistics */
89 struct bridge_mcast_stats {
90 struct br_mcast_stats mstats;
91 struct u64_stats_sync syncp;
95 /* net_bridge_mcast_port must be always defined due to forwarding stubs */
96 struct net_bridge_mcast_port {
97 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
98 struct net_bridge_port *port;
99 struct net_bridge_vlan *vlan;
101 struct bridge_mcast_own_query ip4_own_query;
102 struct timer_list ip4_mc_router_timer;
103 struct hlist_node ip4_rlist;
104 #if IS_ENABLED(CONFIG_IPV6)
105 struct bridge_mcast_own_query ip6_own_query;
106 struct timer_list ip6_mc_router_timer;
107 struct hlist_node ip6_rlist;
108 #endif /* IS_ENABLED(CONFIG_IPV6) */
109 unsigned char multicast_router;
110 #endif /* CONFIG_BRIDGE_IGMP_SNOOPING */
113 /* net_bridge_mcast must be always defined due to forwarding stubs */
114 struct net_bridge_mcast {
115 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
116 struct net_bridge *br;
117 struct net_bridge_vlan *vlan;
119 u32 multicast_last_member_count;
120 u32 multicast_startup_query_count;
122 u8 multicast_querier;
123 u8 multicast_igmp_version;
125 #if IS_ENABLED(CONFIG_IPV6)
126 u8 multicast_mld_version;
128 unsigned long multicast_last_member_interval;
129 unsigned long multicast_membership_interval;
130 unsigned long multicast_querier_interval;
131 unsigned long multicast_query_interval;
132 unsigned long multicast_query_response_interval;
133 unsigned long multicast_startup_query_interval;
134 struct hlist_head ip4_mc_router_list;
135 struct timer_list ip4_mc_router_timer;
136 struct bridge_mcast_other_query ip4_other_query;
137 struct bridge_mcast_own_query ip4_own_query;
138 struct bridge_mcast_querier ip4_querier;
139 #if IS_ENABLED(CONFIG_IPV6)
140 struct hlist_head ip6_mc_router_list;
141 struct timer_list ip6_mc_router_timer;
142 struct bridge_mcast_other_query ip6_other_query;
143 struct bridge_mcast_own_query ip6_own_query;
144 struct bridge_mcast_querier ip6_querier;
145 #endif /* IS_ENABLED(CONFIG_IPV6) */
146 #endif /* CONFIG_BRIDGE_IGMP_SNOOPING */
149 struct br_tunnel_info {
151 struct metadata_dst __rcu *tunnel_dst;
154 /* private vlan flags */
156 BR_VLFLAG_PER_PORT_STATS = BIT(0),
157 BR_VLFLAG_ADDED_BY_SWITCHDEV = BIT(1),
158 BR_VLFLAG_MCAST_ENABLED = BIT(2),
159 BR_VLFLAG_GLOBAL_MCAST_ENABLED = BIT(3),
163 * struct net_bridge_vlan - per-vlan entry
165 * @vnode: rhashtable member
167 * @flags: bridge vlan flags
168 * @priv_flags: private (in-kernel) bridge vlan flags
169 * @state: STP state (e.g. blocking, learning, forwarding)
170 * @stats: per-cpu VLAN statistics
171 * @br: if MASTER flag set, this points to a bridge struct
172 * @port: if MASTER flag unset, this points to a port struct
173 * @refcnt: if MASTER flag set, this is bumped for each port referencing it
174 * @brvlan: if MASTER flag unset, this points to the global per-VLAN context
175 * for this VLAN entry
176 * @br_mcast_ctx: if MASTER flag set, this is the global vlan multicast context
177 * @port_mcast_ctx: if MASTER flag unset, this is the per-port/vlan multicast
179 * @vlist: sorted list of VLAN entries
180 * @rcu: used for entry destruction
182 * This structure is shared between the global per-VLAN entries contained in
183 * the bridge rhashtable and the local per-port per-VLAN entries contained in
184 * the port's rhashtable. The union entries should be interpreted depending on
185 * the entry flags that are set.
187 struct net_bridge_vlan {
188 struct rhash_head vnode;
189 struct rhash_head tnode;
194 struct pcpu_sw_netstats __percpu *stats;
196 struct net_bridge *br;
197 struct net_bridge_port *port;
201 struct net_bridge_vlan *brvlan;
204 struct br_tunnel_info tinfo;
207 struct net_bridge_mcast br_mcast_ctx;
208 struct net_bridge_mcast_port port_mcast_ctx;
211 struct list_head vlist;
217 * struct net_bridge_vlan_group
219 * @vlan_hash: VLAN entry rhashtable
220 * @vlan_list: sorted VLAN entry list
221 * @num_vlans: number of total VLAN entries
222 * @pvid: PVID VLAN id
223 * @pvid_state: PVID's STP state (e.g. forwarding, learning, blocking)
225 * IMPORTANT: Be careful when checking if there're VLAN entries using list
226 * primitives because the bridge can have entries in its list which
227 * are just for global context but not for filtering, i.e. they have
228 * the master flag set but not the brentry flag. If you have to check
229 * if there're "real" entries in the bridge please test @num_vlans
231 struct net_bridge_vlan_group {
232 struct rhashtable vlan_hash;
233 struct rhashtable tunnel_hash;
234 struct list_head vlan_list;
240 /* bridge fdb flags */
245 BR_FDB_ADDED_BY_USER,
246 BR_FDB_ADDED_BY_EXT_LEARN,
249 BR_FDB_NOTIFY_INACTIVE
252 struct net_bridge_fdb_key {
257 struct net_bridge_fdb_entry {
258 struct rhash_head rhnode;
259 struct net_bridge_port *dst;
261 struct net_bridge_fdb_key key;
262 struct hlist_node fdb_node;
265 /* write-heavy members should not affect lookups */
266 unsigned long updated ____cacheline_aligned_in_smp;
272 #define MDB_PG_FLAGS_PERMANENT BIT(0)
273 #define MDB_PG_FLAGS_OFFLOAD BIT(1)
274 #define MDB_PG_FLAGS_FAST_LEAVE BIT(2)
275 #define MDB_PG_FLAGS_STAR_EXCL BIT(3)
276 #define MDB_PG_FLAGS_BLOCKED BIT(4)
278 #define PG_SRC_ENT_LIMIT 32
280 #define BR_SGRP_F_DELETE BIT(0)
281 #define BR_SGRP_F_SEND BIT(1)
282 #define BR_SGRP_F_INSTALLED BIT(2)
284 struct net_bridge_mcast_gc {
285 struct hlist_node gc_node;
286 void (*destroy)(struct net_bridge_mcast_gc *gc);
289 struct net_bridge_group_src {
290 struct hlist_node node;
293 struct net_bridge_port_group *pg;
295 u8 src_query_rexmit_cnt;
296 struct timer_list timer;
298 struct net_bridge *br;
299 struct net_bridge_mcast_gc mcast_gc;
303 struct net_bridge_port_group_sg_key {
304 struct net_bridge_port *port;
308 struct net_bridge_port_group {
309 struct net_bridge_port_group __rcu *next;
310 struct net_bridge_port_group_sg_key key;
311 unsigned char eth_addr[ETH_ALEN] __aligned(2);
313 unsigned char filter_mode;
314 unsigned char grp_query_rexmit_cnt;
315 unsigned char rt_protocol;
317 struct hlist_head src_list;
318 unsigned int src_ents;
319 struct timer_list timer;
320 struct timer_list rexmit_timer;
321 struct hlist_node mglist;
322 struct rb_root eht_set_tree;
323 struct rb_root eht_host_tree;
325 struct rhash_head rhnode;
326 struct net_bridge_mcast_gc mcast_gc;
330 struct net_bridge_mdb_entry {
331 struct rhash_head rhnode;
332 struct net_bridge *br;
333 struct net_bridge_port_group __rcu *ports;
337 struct timer_list timer;
338 struct hlist_node mdb_node;
340 struct net_bridge_mcast_gc mcast_gc;
344 struct net_bridge_port {
345 struct net_bridge *br;
346 struct net_device *dev;
347 struct list_head list;
350 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
351 struct net_bridge_vlan_group __rcu *vlgrp;
353 struct net_bridge_port __rcu *backup_port;
359 unsigned char topology_change_ack;
360 unsigned char config_pending;
362 port_id designated_port;
363 bridge_id designated_root;
364 bridge_id designated_bridge;
367 unsigned long designated_age;
369 struct timer_list forward_delay_timer;
370 struct timer_list hold_timer;
371 struct timer_list message_age_timer;
375 struct net_bridge_mcast_port multicast_ctx;
377 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
378 struct bridge_mcast_stats __percpu *mcast_stats;
380 u32 multicast_eht_hosts_limit;
381 u32 multicast_eht_hosts_cnt;
382 struct hlist_head mglist;
386 char sysfs_name[IFNAMSIZ];
389 #ifdef CONFIG_NET_POLL_CONTROLLER
392 #ifdef CONFIG_NET_SWITCHDEV
393 /* Identifier used to group ports that share the same switchdev
398 struct netdev_phys_item_id ppid;
401 u16 backup_redirected_cnt;
403 struct bridge_stp_xstats stp_xstats;
406 #define kobj_to_brport(obj) container_of(obj, struct net_bridge_port, kobj)
408 #define br_auto_port(p) ((p)->flags & BR_AUTO_MASK)
409 #define br_promisc_port(p) ((p)->flags & BR_PROMISC)
411 static inline struct net_bridge_port *br_port_get_rcu(const struct net_device *dev)
413 return rcu_dereference(dev->rx_handler_data);
416 static inline struct net_bridge_port *br_port_get_rtnl(const struct net_device *dev)
418 return netif_is_bridge_port(dev) ?
419 rtnl_dereference(dev->rx_handler_data) : NULL;
422 static inline struct net_bridge_port *br_port_get_rtnl_rcu(const struct net_device *dev)
424 return netif_is_bridge_port(dev) ?
425 rcu_dereference_rtnl(dev->rx_handler_data) : NULL;
428 enum net_bridge_opts {
430 BROPT_VLAN_STATS_ENABLED,
431 BROPT_NF_CALL_IPTABLES,
432 BROPT_NF_CALL_IP6TABLES,
433 BROPT_NF_CALL_ARPTABLES,
434 BROPT_GROUP_ADDR_SET,
435 BROPT_MULTICAST_ENABLED,
436 BROPT_MULTICAST_QUERY_USE_IFADDR,
437 BROPT_MULTICAST_STATS_ENABLED,
439 BROPT_NEIGH_SUPPRESS_ENABLED,
440 BROPT_MTU_SET_BY_USER,
441 BROPT_VLAN_STATS_PER_PORT,
443 BROPT_VLAN_BRIDGE_BINDING,
444 BROPT_MCAST_VLAN_SNOOPING_ENABLED,
449 spinlock_t hash_lock;
450 struct hlist_head frame_type_list;
451 struct net_device *dev;
452 unsigned long options;
453 /* These fields are accessed on each packet */
454 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
457 struct net_bridge_vlan_group __rcu *vlgrp;
460 struct rhashtable fdb_hash_tbl;
461 struct list_head port_list;
462 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
464 struct rtable fake_rtable;
465 struct rt6_info fake_rt6_info;
469 u16 group_fwd_mask_required;
472 bridge_id designated_root;
474 unsigned char topology_change;
475 unsigned char topology_change_detected;
477 unsigned long max_age;
478 unsigned long hello_time;
479 unsigned long forward_delay;
480 unsigned long ageing_time;
481 unsigned long bridge_max_age;
482 unsigned long bridge_hello_time;
483 unsigned long bridge_forward_delay;
484 unsigned long bridge_ageing_time;
487 u8 group_addr[ETH_ALEN];
490 BR_NO_STP, /* no spanning tree */
491 BR_KERNEL_STP, /* old STP in kernel */
492 BR_USER_STP, /* new RSTP in userspace */
495 struct net_bridge_mcast multicast_ctx;
497 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
498 struct bridge_mcast_stats __percpu *mcast_stats;
502 spinlock_t multicast_lock;
504 struct rhashtable mdb_hash_tbl;
505 struct rhashtable sg_port_tbl;
507 struct hlist_head mcast_gc_list;
508 struct hlist_head mdb_list;
510 struct work_struct mcast_gc_work;
513 struct timer_list hello_timer;
514 struct timer_list tcn_timer;
515 struct timer_list topology_change_timer;
516 struct delayed_work gc_work;
517 struct kobject *ifobj;
520 #ifdef CONFIG_NET_SWITCHDEV
521 /* Counter used to make sure that hardware domains get unique
522 * identifiers in case a bridge spans multiple switchdev instances.
525 /* Bit mask of hardware domain numbers in use */
526 unsigned long busy_hwdoms;
528 struct hlist_head fdb_list;
530 #if IS_ENABLED(CONFIG_BRIDGE_MRP)
531 struct hlist_head mrp_list;
533 #if IS_ENABLED(CONFIG_BRIDGE_CFM)
534 struct hlist_head mep_list;
538 struct br_input_skb_cb {
539 struct net_device *brdev;
542 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
546 u8 proxyarp_replied:1;
547 u8 src_port_isolated:1;
548 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
551 #ifdef CONFIG_NETFILTER_FAMILY_BRIDGE
552 u8 br_netfilter_broute:1;
555 #ifdef CONFIG_NET_SWITCHDEV
556 /* Set if TX data plane offloading is used towards at least one
560 /* The switchdev hardware domain from which this packet was received.
561 * If skb->offload_fwd_mark was set, then this packet was already
562 * forwarded by hardware to the other ports in the source hardware
563 * domain, otherwise it wasn't.
566 /* Bit mask of hardware domains towards this packet has already been
567 * transmitted using the TX data plane offload.
569 unsigned long fwd_hwdoms;
573 #define BR_INPUT_SKB_CB(__skb) ((struct br_input_skb_cb *)(__skb)->cb)
575 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
576 # define BR_INPUT_SKB_CB_MROUTERS_ONLY(__skb) (BR_INPUT_SKB_CB(__skb)->mrouters_only)
578 # define BR_INPUT_SKB_CB_MROUTERS_ONLY(__skb) (0)
581 #define br_printk(level, br, format, args...) \
582 printk(level "%s: " format, (br)->dev->name, ##args)
584 #define br_err(__br, format, args...) \
585 br_printk(KERN_ERR, __br, format, ##args)
586 #define br_warn(__br, format, args...) \
587 br_printk(KERN_WARNING, __br, format, ##args)
588 #define br_notice(__br, format, args...) \
589 br_printk(KERN_NOTICE, __br, format, ##args)
590 #define br_info(__br, format, args...) \
591 br_printk(KERN_INFO, __br, format, ##args)
593 #define br_debug(br, format, args...) \
594 pr_debug("%s: " format, (br)->dev->name, ##args)
596 /* called under bridge lock */
597 static inline int br_is_root_bridge(const struct net_bridge *br)
599 return !memcmp(&br->bridge_id, &br->designated_root, 8);
602 /* check if a VLAN entry is global */
603 static inline bool br_vlan_is_master(const struct net_bridge_vlan *v)
605 return v->flags & BRIDGE_VLAN_INFO_MASTER;
608 /* check if a VLAN entry is used by the bridge */
609 static inline bool br_vlan_is_brentry(const struct net_bridge_vlan *v)
611 return v->flags & BRIDGE_VLAN_INFO_BRENTRY;
614 /* check if we should use the vlan entry, returns false if it's only context */
615 static inline bool br_vlan_should_use(const struct net_bridge_vlan *v)
617 if (br_vlan_is_master(v)) {
618 if (br_vlan_is_brentry(v))
627 static inline bool nbp_state_should_learn(const struct net_bridge_port *p)
629 return p->state == BR_STATE_LEARNING || p->state == BR_STATE_FORWARDING;
632 static inline bool br_vlan_valid_id(u16 vid, struct netlink_ext_ack *extack)
634 bool ret = vid > 0 && vid < VLAN_VID_MASK;
637 NL_SET_ERR_MSG_MOD(extack, "Vlan id is invalid");
642 static inline bool br_vlan_valid_range(const struct bridge_vlan_info *cur,
643 const struct bridge_vlan_info *last,
644 struct netlink_ext_ack *extack)
646 /* pvid flag is not allowed in ranges */
647 if (cur->flags & BRIDGE_VLAN_INFO_PVID) {
648 NL_SET_ERR_MSG_MOD(extack, "Pvid isn't allowed in a range");
652 /* when cur is the range end, check if:
653 * - it has range start flag
654 * - range ids are invalid (end is equal to or before start)
657 if (cur->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
658 NL_SET_ERR_MSG_MOD(extack, "Found a new vlan range start while processing one");
660 } else if (!(cur->flags & BRIDGE_VLAN_INFO_RANGE_END)) {
661 NL_SET_ERR_MSG_MOD(extack, "Vlan range end flag is missing");
663 } else if (cur->vid <= last->vid) {
664 NL_SET_ERR_MSG_MOD(extack, "End vlan id is less than or equal to start vlan id");
669 /* check for required range flags */
670 if (!(cur->flags & (BRIDGE_VLAN_INFO_RANGE_BEGIN |
671 BRIDGE_VLAN_INFO_RANGE_END))) {
672 NL_SET_ERR_MSG_MOD(extack, "Both vlan range flags are missing");
679 static inline u8 br_vlan_multicast_router(const struct net_bridge_vlan *v)
681 u8 mcast_router = MDB_RTR_TYPE_DISABLED;
683 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
684 if (!br_vlan_is_master(v))
685 mcast_router = v->port_mcast_ctx.multicast_router;
687 mcast_router = v->br_mcast_ctx.multicast_router;
693 static inline int br_afspec_cmd_to_rtm(int cmd)
705 static inline int br_opt_get(const struct net_bridge *br,
706 enum net_bridge_opts opt)
708 return test_bit(opt, &br->options);
711 int br_boolopt_toggle(struct net_bridge *br, enum br_boolopt_id opt, bool on,
712 struct netlink_ext_ack *extack);
713 int br_boolopt_get(const struct net_bridge *br, enum br_boolopt_id opt);
714 int br_boolopt_multi_toggle(struct net_bridge *br,
715 struct br_boolopt_multi *bm,
716 struct netlink_ext_ack *extack);
717 void br_boolopt_multi_get(const struct net_bridge *br,
718 struct br_boolopt_multi *bm);
719 void br_opt_toggle(struct net_bridge *br, enum net_bridge_opts opt, bool on);
722 void br_dev_setup(struct net_device *dev);
723 void br_dev_delete(struct net_device *dev, struct list_head *list);
724 netdev_tx_t br_dev_xmit(struct sk_buff *skb, struct net_device *dev);
725 #ifdef CONFIG_NET_POLL_CONTROLLER
726 static inline void br_netpoll_send_skb(const struct net_bridge_port *p,
729 netpoll_send_skb(p->np, skb);
732 int br_netpoll_enable(struct net_bridge_port *p);
733 void br_netpoll_disable(struct net_bridge_port *p);
735 static inline void br_netpoll_send_skb(const struct net_bridge_port *p,
740 static inline int br_netpoll_enable(struct net_bridge_port *p)
745 static inline void br_netpoll_disable(struct net_bridge_port *p)
751 int br_fdb_init(void);
752 void br_fdb_fini(void);
753 int br_fdb_hash_init(struct net_bridge *br);
754 void br_fdb_hash_fini(struct net_bridge *br);
755 void br_fdb_flush(struct net_bridge *br);
756 void br_fdb_find_delete_local(struct net_bridge *br,
757 const struct net_bridge_port *p,
758 const unsigned char *addr, u16 vid);
759 void br_fdb_changeaddr(struct net_bridge_port *p, const unsigned char *newaddr);
760 void br_fdb_change_mac_address(struct net_bridge *br, const u8 *newaddr);
761 void br_fdb_cleanup(struct work_struct *work);
762 void br_fdb_delete_by_port(struct net_bridge *br,
763 const struct net_bridge_port *p, u16 vid, int do_all);
764 struct net_bridge_fdb_entry *br_fdb_find_rcu(struct net_bridge *br,
765 const unsigned char *addr,
767 int br_fdb_test_addr(struct net_device *dev, unsigned char *addr);
768 int br_fdb_fillbuf(struct net_bridge *br, void *buf, unsigned long count,
770 int br_fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
771 const unsigned char *addr, u16 vid);
772 void br_fdb_update(struct net_bridge *br, struct net_bridge_port *source,
773 const unsigned char *addr, u16 vid, unsigned long flags);
775 int br_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
776 struct net_device *dev, const unsigned char *addr, u16 vid);
777 int br_fdb_add(struct ndmsg *nlh, struct nlattr *tb[], struct net_device *dev,
778 const unsigned char *addr, u16 vid, u16 nlh_flags,
779 struct netlink_ext_ack *extack);
780 int br_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
781 struct net_device *dev, struct net_device *fdev, int *idx);
782 int br_fdb_get(struct sk_buff *skb, struct nlattr *tb[], struct net_device *dev,
783 const unsigned char *addr, u16 vid, u32 portid, u32 seq,
784 struct netlink_ext_ack *extack);
785 int br_fdb_sync_static(struct net_bridge *br, struct net_bridge_port *p);
786 void br_fdb_unsync_static(struct net_bridge *br, struct net_bridge_port *p);
787 int br_fdb_external_learn_add(struct net_bridge *br, struct net_bridge_port *p,
788 const unsigned char *addr, u16 vid,
790 int br_fdb_external_learn_del(struct net_bridge *br, struct net_bridge_port *p,
791 const unsigned char *addr, u16 vid,
793 void br_fdb_offloaded_set(struct net_bridge *br, struct net_bridge_port *p,
794 const unsigned char *addr, u16 vid, bool offloaded);
795 int br_fdb_replay(const struct net_device *br_dev, const void *ctx, bool adding,
796 struct notifier_block *nb);
804 int br_dev_queue_push_xmit(struct net *net, struct sock *sk, struct sk_buff *skb);
805 void br_forward(const struct net_bridge_port *to, struct sk_buff *skb,
806 bool local_rcv, bool local_orig);
807 int br_forward_finish(struct net *net, struct sock *sk, struct sk_buff *skb);
808 void br_flood(struct net_bridge *br, struct sk_buff *skb,
809 enum br_pkt_type pkt_type, bool local_rcv, bool local_orig);
811 /* return true if both source port and dest port are isolated */
812 static inline bool br_skb_isolated(const struct net_bridge_port *to,
813 const struct sk_buff *skb)
815 return BR_INPUT_SKB_CB(skb)->src_port_isolated &&
816 (to->flags & BR_ISOLATED);
820 void br_port_carrier_check(struct net_bridge_port *p, bool *notified);
821 int br_add_bridge(struct net *net, const char *name);
822 int br_del_bridge(struct net *net, const char *name);
823 int br_add_if(struct net_bridge *br, struct net_device *dev,
824 struct netlink_ext_ack *extack);
825 int br_del_if(struct net_bridge *br, struct net_device *dev);
826 void br_mtu_auto_adjust(struct net_bridge *br);
827 netdev_features_t br_features_recompute(struct net_bridge *br,
828 netdev_features_t features);
829 void br_port_flags_change(struct net_bridge_port *port, unsigned long mask);
830 void br_manage_promisc(struct net_bridge *br);
831 int nbp_backup_change(struct net_bridge_port *p, struct net_device *backup_dev);
834 int br_handle_frame_finish(struct net *net, struct sock *sk, struct sk_buff *skb);
835 rx_handler_func_t *br_get_rx_handler(const struct net_device *dev);
837 struct br_frame_type {
839 int (*frame_handler)(struct net_bridge_port *port,
840 struct sk_buff *skb);
841 struct hlist_node list;
844 void br_add_frame(struct net_bridge *br, struct br_frame_type *ft);
845 void br_del_frame(struct net_bridge *br, struct br_frame_type *ft);
847 static inline bool br_rx_handler_check_rcu(const struct net_device *dev)
849 return rcu_dereference(dev->rx_handler) == br_get_rx_handler(dev);
852 static inline bool br_rx_handler_check_rtnl(const struct net_device *dev)
854 return rcu_dereference_rtnl(dev->rx_handler) == br_get_rx_handler(dev);
857 static inline struct net_bridge_port *br_port_get_check_rcu(const struct net_device *dev)
859 return br_rx_handler_check_rcu(dev) ? br_port_get_rcu(dev) : NULL;
862 static inline struct net_bridge_port *
863 br_port_get_check_rtnl(const struct net_device *dev)
865 return br_rx_handler_check_rtnl(dev) ? br_port_get_rtnl_rcu(dev) : NULL;
869 int br_dev_siocdevprivate(struct net_device *dev, struct ifreq *rq,
870 void __user *data, int cmd);
871 int br_ioctl_stub(struct net *net, struct net_bridge *br, unsigned int cmd,
872 struct ifreq *ifr, void __user *uarg);
875 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
876 int br_multicast_rcv(struct net_bridge_mcast **brmctx,
877 struct net_bridge_mcast_port **pmctx,
878 struct net_bridge_vlan *vlan,
879 struct sk_buff *skb, u16 vid);
880 struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge_mcast *brmctx,
881 struct sk_buff *skb, u16 vid);
882 int br_multicast_add_port(struct net_bridge_port *port);
883 void br_multicast_del_port(struct net_bridge_port *port);
884 void br_multicast_enable_port(struct net_bridge_port *port);
885 void br_multicast_disable_port(struct net_bridge_port *port);
886 void br_multicast_init(struct net_bridge *br);
887 void br_multicast_join_snoopers(struct net_bridge *br);
888 void br_multicast_leave_snoopers(struct net_bridge *br);
889 void br_multicast_open(struct net_bridge *br);
890 void br_multicast_stop(struct net_bridge *br);
891 void br_multicast_dev_del(struct net_bridge *br);
892 void br_multicast_flood(struct net_bridge_mdb_entry *mdst, struct sk_buff *skb,
893 struct net_bridge_mcast *brmctx,
894 bool local_rcv, bool local_orig);
895 int br_multicast_set_router(struct net_bridge_mcast *brmctx, unsigned long val);
896 int br_multicast_set_port_router(struct net_bridge_mcast_port *pmctx,
898 int br_multicast_set_vlan_router(struct net_bridge_vlan *v, u8 mcast_router);
899 int br_multicast_toggle(struct net_bridge *br, unsigned long val,
900 struct netlink_ext_ack *extack);
901 int br_multicast_set_querier(struct net_bridge_mcast *brmctx, unsigned long val);
902 int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val);
903 int br_multicast_set_igmp_version(struct net_bridge_mcast *brmctx,
905 #if IS_ENABLED(CONFIG_IPV6)
906 int br_multicast_set_mld_version(struct net_bridge_mcast *brmctx,
909 struct net_bridge_mdb_entry *
910 br_mdb_ip_get(struct net_bridge *br, struct br_ip *dst);
911 struct net_bridge_mdb_entry *
912 br_multicast_new_group(struct net_bridge *br, struct br_ip *group);
913 struct net_bridge_port_group *
914 br_multicast_new_port_group(struct net_bridge_port *port, struct br_ip *group,
915 struct net_bridge_port_group __rcu *next,
916 unsigned char flags, const unsigned char *src,
917 u8 filter_mode, u8 rt_protocol);
918 int br_mdb_hash_init(struct net_bridge *br);
919 void br_mdb_hash_fini(struct net_bridge *br);
920 void br_mdb_notify(struct net_device *dev, struct net_bridge_mdb_entry *mp,
921 struct net_bridge_port_group *pg, int type);
922 void br_rtr_notify(struct net_device *dev, struct net_bridge_mcast_port *pmctx,
924 void br_multicast_del_pg(struct net_bridge_mdb_entry *mp,
925 struct net_bridge_port_group *pg,
926 struct net_bridge_port_group __rcu **pp);
927 void br_multicast_count(struct net_bridge *br,
928 const struct net_bridge_port *p,
929 const struct sk_buff *skb, u8 type, u8 dir);
930 int br_multicast_init_stats(struct net_bridge *br);
931 void br_multicast_uninit_stats(struct net_bridge *br);
932 void br_multicast_get_stats(const struct net_bridge *br,
933 const struct net_bridge_port *p,
934 struct br_mcast_stats *dest);
935 void br_mdb_init(void);
936 void br_mdb_uninit(void);
937 void br_multicast_host_join(const struct net_bridge_mcast *brmctx,
938 struct net_bridge_mdb_entry *mp, bool notify);
939 void br_multicast_host_leave(struct net_bridge_mdb_entry *mp, bool notify);
940 void br_multicast_star_g_handle_mode(struct net_bridge_port_group *pg,
942 void br_multicast_sg_add_exclude_ports(struct net_bridge_mdb_entry *star_mp,
943 struct net_bridge_port_group *sg);
944 struct net_bridge_group_src *
945 br_multicast_find_group_src(struct net_bridge_port_group *pg, struct br_ip *ip);
946 void br_multicast_del_group_src(struct net_bridge_group_src *src,
948 void br_multicast_ctx_init(struct net_bridge *br,
949 struct net_bridge_vlan *vlan,
950 struct net_bridge_mcast *brmctx);
951 void br_multicast_ctx_deinit(struct net_bridge_mcast *brmctx);
952 void br_multicast_port_ctx_init(struct net_bridge_port *port,
953 struct net_bridge_vlan *vlan,
954 struct net_bridge_mcast_port *pmctx);
955 void br_multicast_port_ctx_deinit(struct net_bridge_mcast_port *pmctx);
956 void br_multicast_toggle_one_vlan(struct net_bridge_vlan *vlan, bool on);
957 int br_multicast_toggle_vlan_snooping(struct net_bridge *br, bool on,
958 struct netlink_ext_ack *extack);
959 bool br_multicast_toggle_global_vlan(struct net_bridge_vlan *vlan, bool on);
961 int br_mdb_replay(struct net_device *br_dev, struct net_device *dev,
962 const void *ctx, bool adding, struct notifier_block *nb,
963 struct netlink_ext_ack *extack);
964 int br_rports_fill_info(struct sk_buff *skb,
965 const struct net_bridge_mcast *brmctx);
966 int br_multicast_dump_querier_state(struct sk_buff *skb,
967 const struct net_bridge_mcast *brmctx,
969 size_t br_multicast_querier_state_size(void);
970 size_t br_rports_size(const struct net_bridge_mcast *brmctx);
972 static inline bool br_group_is_l2(const struct br_ip *group)
974 return group->proto == 0;
977 #define mlock_dereference(X, br) \
978 rcu_dereference_protected(X, lockdep_is_held(&br->multicast_lock))
980 static inline struct hlist_node *
981 br_multicast_get_first_rport_node(struct net_bridge_mcast *brmctx,
984 #if IS_ENABLED(CONFIG_IPV6)
985 if (skb->protocol == htons(ETH_P_IPV6))
986 return rcu_dereference(hlist_first_rcu(&brmctx->ip6_mc_router_list));
988 return rcu_dereference(hlist_first_rcu(&brmctx->ip4_mc_router_list));
991 static inline struct net_bridge_port *
992 br_multicast_rport_from_node_skb(struct hlist_node *rp, struct sk_buff *skb)
994 struct net_bridge_mcast_port *mctx;
996 #if IS_ENABLED(CONFIG_IPV6)
997 if (skb->protocol == htons(ETH_P_IPV6))
998 mctx = hlist_entry_safe(rp, struct net_bridge_mcast_port,
1002 mctx = hlist_entry_safe(rp, struct net_bridge_mcast_port,
1011 static inline bool br_ip4_multicast_is_router(struct net_bridge_mcast *brmctx)
1013 return timer_pending(&brmctx->ip4_mc_router_timer);
1016 static inline bool br_ip6_multicast_is_router(struct net_bridge_mcast *brmctx)
1018 #if IS_ENABLED(CONFIG_IPV6)
1019 return timer_pending(&brmctx->ip6_mc_router_timer);
1026 br_multicast_is_router(struct net_bridge_mcast *brmctx, struct sk_buff *skb)
1028 switch (brmctx->multicast_router) {
1029 case MDB_RTR_TYPE_PERM:
1031 case MDB_RTR_TYPE_TEMP_QUERY:
1033 if (skb->protocol == htons(ETH_P_IP))
1034 return br_ip4_multicast_is_router(brmctx);
1035 else if (skb->protocol == htons(ETH_P_IPV6))
1036 return br_ip6_multicast_is_router(brmctx);
1038 return br_ip4_multicast_is_router(brmctx) ||
1039 br_ip6_multicast_is_router(brmctx);
1048 __br_multicast_querier_exists(struct net_bridge_mcast *brmctx,
1049 struct bridge_mcast_other_query *querier,
1052 bool own_querier_enabled;
1054 if (brmctx->multicast_querier) {
1055 if (is_ipv6 && !br_opt_get(brmctx->br, BROPT_HAS_IPV6_ADDR))
1056 own_querier_enabled = false;
1058 own_querier_enabled = true;
1060 own_querier_enabled = false;
1063 return time_is_before_jiffies(querier->delay_time) &&
1064 (own_querier_enabled || timer_pending(&querier->timer));
1067 static inline bool br_multicast_querier_exists(struct net_bridge_mcast *brmctx,
1069 const struct net_bridge_mdb_entry *mdb)
1071 switch (eth->h_proto) {
1072 case (htons(ETH_P_IP)):
1073 return __br_multicast_querier_exists(brmctx,
1074 &brmctx->ip4_other_query, false);
1075 #if IS_ENABLED(CONFIG_IPV6)
1076 case (htons(ETH_P_IPV6)):
1077 return __br_multicast_querier_exists(brmctx,
1078 &brmctx->ip6_other_query, true);
1081 return !!mdb && br_group_is_l2(&mdb->addr);
1085 static inline bool br_multicast_is_star_g(const struct br_ip *ip)
1087 switch (ip->proto) {
1088 case htons(ETH_P_IP):
1089 return ipv4_is_zeronet(ip->src.ip4);
1090 #if IS_ENABLED(CONFIG_IPV6)
1091 case htons(ETH_P_IPV6):
1092 return ipv6_addr_any(&ip->src.ip6);
1100 br_multicast_should_handle_mode(const struct net_bridge_mcast *brmctx,
1104 case htons(ETH_P_IP):
1105 return !!(brmctx->multicast_igmp_version == 3);
1106 #if IS_ENABLED(CONFIG_IPV6)
1107 case htons(ETH_P_IPV6):
1108 return !!(brmctx->multicast_mld_version == 2);
1115 static inline int br_multicast_igmp_type(const struct sk_buff *skb)
1117 return BR_INPUT_SKB_CB(skb)->igmp;
1120 static inline unsigned long br_multicast_lmqt(const struct net_bridge_mcast *brmctx)
1122 return brmctx->multicast_last_member_interval *
1123 brmctx->multicast_last_member_count;
1126 static inline unsigned long br_multicast_gmi(const struct net_bridge_mcast *brmctx)
1128 /* use the RFC default of 2 for QRV */
1129 return 2 * brmctx->multicast_query_interval +
1130 brmctx->multicast_query_response_interval;
1134 br_multicast_ctx_is_vlan(const struct net_bridge_mcast *brmctx)
1136 return !!brmctx->vlan;
1140 br_multicast_port_ctx_is_vlan(const struct net_bridge_mcast_port *pmctx)
1142 return !!pmctx->vlan;
1145 static inline struct net_bridge_mcast *
1146 br_multicast_port_ctx_get_global(const struct net_bridge_mcast_port *pmctx)
1148 if (!br_multicast_port_ctx_is_vlan(pmctx))
1149 return &pmctx->port->br->multicast_ctx;
1151 return &pmctx->vlan->brvlan->br_mcast_ctx;
1155 br_multicast_ctx_vlan_global_disabled(const struct net_bridge_mcast *brmctx)
1157 return br_opt_get(brmctx->br, BROPT_MCAST_VLAN_SNOOPING_ENABLED) &&
1158 br_multicast_ctx_is_vlan(brmctx) &&
1159 !(brmctx->vlan->priv_flags & BR_VLFLAG_GLOBAL_MCAST_ENABLED);
1163 br_multicast_ctx_vlan_disabled(const struct net_bridge_mcast *brmctx)
1165 return br_multicast_ctx_is_vlan(brmctx) &&
1166 !(brmctx->vlan->priv_flags & BR_VLFLAG_MCAST_ENABLED);
1170 br_multicast_port_ctx_vlan_disabled(const struct net_bridge_mcast_port *pmctx)
1172 return br_multicast_port_ctx_is_vlan(pmctx) &&
1173 !(pmctx->vlan->priv_flags & BR_VLFLAG_MCAST_ENABLED);
1177 br_multicast_port_ctx_state_disabled(const struct net_bridge_mcast_port *pmctx)
1179 return pmctx->port->state == BR_STATE_DISABLED ||
1180 (br_multicast_port_ctx_is_vlan(pmctx) &&
1181 (br_multicast_port_ctx_vlan_disabled(pmctx) ||
1182 pmctx->vlan->state == BR_STATE_DISABLED));
1186 br_multicast_port_ctx_state_stopped(const struct net_bridge_mcast_port *pmctx)
1188 return br_multicast_port_ctx_state_disabled(pmctx) ||
1189 pmctx->port->state == BR_STATE_BLOCKING ||
1190 (br_multicast_port_ctx_is_vlan(pmctx) &&
1191 pmctx->vlan->state == BR_STATE_BLOCKING);
1195 br_rports_have_mc_router(const struct net_bridge_mcast *brmctx)
1197 #if IS_ENABLED(CONFIG_IPV6)
1198 return !hlist_empty(&brmctx->ip4_mc_router_list) ||
1199 !hlist_empty(&brmctx->ip6_mc_router_list);
1201 return !hlist_empty(&brmctx->ip4_mc_router_list);
1206 br_multicast_ctx_options_equal(const struct net_bridge_mcast *brmctx1,
1207 const struct net_bridge_mcast *brmctx2)
1209 return brmctx1->multicast_igmp_version ==
1210 brmctx2->multicast_igmp_version &&
1211 brmctx1->multicast_last_member_count ==
1212 brmctx2->multicast_last_member_count &&
1213 brmctx1->multicast_startup_query_count ==
1214 brmctx2->multicast_startup_query_count &&
1215 brmctx1->multicast_last_member_interval ==
1216 brmctx2->multicast_last_member_interval &&
1217 brmctx1->multicast_membership_interval ==
1218 brmctx2->multicast_membership_interval &&
1219 brmctx1->multicast_querier_interval ==
1220 brmctx2->multicast_querier_interval &&
1221 brmctx1->multicast_query_interval ==
1222 brmctx2->multicast_query_interval &&
1223 brmctx1->multicast_query_response_interval ==
1224 brmctx2->multicast_query_response_interval &&
1225 brmctx1->multicast_startup_query_interval ==
1226 brmctx2->multicast_startup_query_interval &&
1227 brmctx1->multicast_querier == brmctx2->multicast_querier &&
1228 brmctx1->multicast_router == brmctx2->multicast_router &&
1229 !br_rports_have_mc_router(brmctx1) &&
1230 !br_rports_have_mc_router(brmctx2) &&
1231 #if IS_ENABLED(CONFIG_IPV6)
1232 brmctx1->multicast_mld_version ==
1233 brmctx2->multicast_mld_version &&
1239 br_multicast_ctx_matches_vlan_snooping(const struct net_bridge_mcast *brmctx)
1241 bool vlan_snooping_enabled;
1243 vlan_snooping_enabled = !!br_opt_get(brmctx->br,
1244 BROPT_MCAST_VLAN_SNOOPING_ENABLED);
1246 return !!(vlan_snooping_enabled == br_multicast_ctx_is_vlan(brmctx));
1249 static inline int br_multicast_rcv(struct net_bridge_mcast **brmctx,
1250 struct net_bridge_mcast_port **pmctx,
1251 struct net_bridge_vlan *vlan,
1252 struct sk_buff *skb,
1258 static inline struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge_mcast *brmctx,
1259 struct sk_buff *skb, u16 vid)
1264 static inline int br_multicast_add_port(struct net_bridge_port *port)
1269 static inline void br_multicast_del_port(struct net_bridge_port *port)
1273 static inline void br_multicast_enable_port(struct net_bridge_port *port)
1277 static inline void br_multicast_disable_port(struct net_bridge_port *port)
1281 static inline void br_multicast_init(struct net_bridge *br)
1285 static inline void br_multicast_join_snoopers(struct net_bridge *br)
1289 static inline void br_multicast_leave_snoopers(struct net_bridge *br)
1293 static inline void br_multicast_open(struct net_bridge *br)
1297 static inline void br_multicast_stop(struct net_bridge *br)
1301 static inline void br_multicast_dev_del(struct net_bridge *br)
1305 static inline void br_multicast_flood(struct net_bridge_mdb_entry *mdst,
1306 struct sk_buff *skb,
1307 struct net_bridge_mcast *brmctx,
1308 bool local_rcv, bool local_orig)
1312 static inline bool br_multicast_is_router(struct net_bridge_mcast *brmctx,
1313 struct sk_buff *skb)
1318 static inline bool br_multicast_querier_exists(struct net_bridge_mcast *brmctx,
1320 const struct net_bridge_mdb_entry *mdb)
1325 static inline void br_mdb_init(void)
1329 static inline void br_mdb_uninit(void)
1333 static inline int br_mdb_hash_init(struct net_bridge *br)
1338 static inline void br_mdb_hash_fini(struct net_bridge *br)
1342 static inline void br_multicast_count(struct net_bridge *br,
1343 const struct net_bridge_port *p,
1344 const struct sk_buff *skb,
1349 static inline int br_multicast_init_stats(struct net_bridge *br)
1354 static inline void br_multicast_uninit_stats(struct net_bridge *br)
1358 static inline int br_multicast_igmp_type(const struct sk_buff *skb)
1363 static inline void br_multicast_ctx_init(struct net_bridge *br,
1364 struct net_bridge_vlan *vlan,
1365 struct net_bridge_mcast *brmctx)
1369 static inline void br_multicast_ctx_deinit(struct net_bridge_mcast *brmctx)
1373 static inline void br_multicast_port_ctx_init(struct net_bridge_port *port,
1374 struct net_bridge_vlan *vlan,
1375 struct net_bridge_mcast_port *pmctx)
1379 static inline void br_multicast_port_ctx_deinit(struct net_bridge_mcast_port *pmctx)
1383 static inline void br_multicast_toggle_one_vlan(struct net_bridge_vlan *vlan,
1388 static inline int br_multicast_toggle_vlan_snooping(struct net_bridge *br,
1390 struct netlink_ext_ack *extack)
1395 static inline bool br_multicast_toggle_global_vlan(struct net_bridge_vlan *vlan,
1401 static inline int br_mdb_replay(struct net_device *br_dev,
1402 struct net_device *dev, const void *ctx,
1403 bool adding, struct notifier_block *nb,
1404 struct netlink_ext_ack *extack)
1410 br_multicast_ctx_options_equal(const struct net_bridge_mcast *brmctx1,
1411 const struct net_bridge_mcast *brmctx2)
1418 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
1419 bool br_allowed_ingress(const struct net_bridge *br,
1420 struct net_bridge_vlan_group *vg, struct sk_buff *skb,
1421 u16 *vid, u8 *state,
1422 struct net_bridge_vlan **vlan);
1423 bool br_allowed_egress(struct net_bridge_vlan_group *vg,
1424 const struct sk_buff *skb);
1425 bool br_should_learn(struct net_bridge_port *p, struct sk_buff *skb, u16 *vid);
1426 struct sk_buff *br_handle_vlan(struct net_bridge *br,
1427 const struct net_bridge_port *port,
1428 struct net_bridge_vlan_group *vg,
1429 struct sk_buff *skb);
1430 int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags,
1431 bool *changed, struct netlink_ext_ack *extack);
1432 int br_vlan_delete(struct net_bridge *br, u16 vid);
1433 void br_vlan_flush(struct net_bridge *br);
1434 struct net_bridge_vlan *br_vlan_find(struct net_bridge_vlan_group *vg, u16 vid);
1435 void br_recalculate_fwd_mask(struct net_bridge *br);
1436 int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val,
1437 struct netlink_ext_ack *extack);
1438 int __br_vlan_set_proto(struct net_bridge *br, __be16 proto,
1439 struct netlink_ext_ack *extack);
1440 int br_vlan_set_proto(struct net_bridge *br, unsigned long val,
1441 struct netlink_ext_ack *extack);
1442 int br_vlan_set_stats(struct net_bridge *br, unsigned long val);
1443 int br_vlan_set_stats_per_port(struct net_bridge *br, unsigned long val);
1444 int br_vlan_init(struct net_bridge *br);
1445 int br_vlan_set_default_pvid(struct net_bridge *br, unsigned long val,
1446 struct netlink_ext_ack *extack);
1447 int __br_vlan_set_default_pvid(struct net_bridge *br, u16 pvid,
1448 struct netlink_ext_ack *extack);
1449 int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags,
1450 bool *changed, struct netlink_ext_ack *extack);
1451 int nbp_vlan_delete(struct net_bridge_port *port, u16 vid);
1452 void nbp_vlan_flush(struct net_bridge_port *port);
1453 int nbp_vlan_init(struct net_bridge_port *port, struct netlink_ext_ack *extack);
1454 int nbp_get_num_vlan_infos(struct net_bridge_port *p, u32 filter_mask);
1455 void br_vlan_get_stats(const struct net_bridge_vlan *v,
1456 struct pcpu_sw_netstats *stats);
1457 void br_vlan_port_event(struct net_bridge_port *p, unsigned long event);
1458 int br_vlan_bridge_event(struct net_device *dev, unsigned long event,
1460 void br_vlan_rtnl_init(void);
1461 void br_vlan_rtnl_uninit(void);
1462 void br_vlan_notify(const struct net_bridge *br,
1463 const struct net_bridge_port *p,
1464 u16 vid, u16 vid_range,
1466 int br_vlan_replay(struct net_device *br_dev, struct net_device *dev,
1467 const void *ctx, bool adding, struct notifier_block *nb,
1468 struct netlink_ext_ack *extack);
1469 bool br_vlan_can_enter_range(const struct net_bridge_vlan *v_curr,
1470 const struct net_bridge_vlan *range_end);
1472 void br_vlan_fill_forward_path_pvid(struct net_bridge *br,
1473 struct net_device_path_ctx *ctx,
1474 struct net_device_path *path);
1475 int br_vlan_fill_forward_path_mode(struct net_bridge *br,
1476 struct net_bridge_port *dst,
1477 struct net_device_path *path);
1479 static inline struct net_bridge_vlan_group *br_vlan_group(
1480 const struct net_bridge *br)
1482 return rtnl_dereference(br->vlgrp);
1485 static inline struct net_bridge_vlan_group *nbp_vlan_group(
1486 const struct net_bridge_port *p)
1488 return rtnl_dereference(p->vlgrp);
1491 static inline struct net_bridge_vlan_group *br_vlan_group_rcu(
1492 const struct net_bridge *br)
1494 return rcu_dereference(br->vlgrp);
1497 static inline struct net_bridge_vlan_group *nbp_vlan_group_rcu(
1498 const struct net_bridge_port *p)
1500 return rcu_dereference(p->vlgrp);
1503 /* Since bridge now depends on 8021Q module, but the time bridge sees the
1504 * skb, the vlan tag will always be present if the frame was tagged.
1506 static inline int br_vlan_get_tag(const struct sk_buff *skb, u16 *vid)
1510 if (skb_vlan_tag_present(skb)) {
1511 *vid = skb_vlan_tag_get_id(skb);
1520 static inline u16 br_get_pvid(const struct net_bridge_vlan_group *vg)
1529 static inline u16 br_vlan_flags(const struct net_bridge_vlan *v, u16 pvid)
1531 return v->vid == pvid ? v->flags | BRIDGE_VLAN_INFO_PVID : v->flags;
1534 static inline bool br_allowed_ingress(const struct net_bridge *br,
1535 struct net_bridge_vlan_group *vg,
1536 struct sk_buff *skb,
1537 u16 *vid, u8 *state,
1538 struct net_bridge_vlan **vlan)
1545 static inline bool br_allowed_egress(struct net_bridge_vlan_group *vg,
1546 const struct sk_buff *skb)
1551 static inline bool br_should_learn(struct net_bridge_port *p,
1552 struct sk_buff *skb, u16 *vid)
1557 static inline struct sk_buff *br_handle_vlan(struct net_bridge *br,
1558 const struct net_bridge_port *port,
1559 struct net_bridge_vlan_group *vg,
1560 struct sk_buff *skb)
1565 static inline int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags,
1566 bool *changed, struct netlink_ext_ack *extack)
1572 static inline int br_vlan_delete(struct net_bridge *br, u16 vid)
1577 static inline void br_vlan_flush(struct net_bridge *br)
1581 static inline void br_recalculate_fwd_mask(struct net_bridge *br)
1585 static inline int br_vlan_init(struct net_bridge *br)
1590 static inline int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags,
1591 bool *changed, struct netlink_ext_ack *extack)
1597 static inline int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
1602 static inline void nbp_vlan_flush(struct net_bridge_port *port)
1606 static inline struct net_bridge_vlan *br_vlan_find(struct net_bridge_vlan_group *vg,
1612 static inline int nbp_vlan_init(struct net_bridge_port *port,
1613 struct netlink_ext_ack *extack)
1618 static inline u16 br_vlan_get_tag(const struct sk_buff *skb, u16 *tag)
1623 static inline u16 br_get_pvid(const struct net_bridge_vlan_group *vg)
1628 static inline int br_vlan_filter_toggle(struct net_bridge *br,
1630 struct netlink_ext_ack *extack)
1635 static inline int nbp_get_num_vlan_infos(struct net_bridge_port *p,
1641 static inline void br_vlan_fill_forward_path_pvid(struct net_bridge *br,
1642 struct net_device_path_ctx *ctx,
1643 struct net_device_path *path)
1647 static inline int br_vlan_fill_forward_path_mode(struct net_bridge *br,
1648 struct net_bridge_port *dst,
1649 struct net_device_path *path)
1654 static inline struct net_bridge_vlan_group *br_vlan_group(
1655 const struct net_bridge *br)
1660 static inline struct net_bridge_vlan_group *nbp_vlan_group(
1661 const struct net_bridge_port *p)
1666 static inline struct net_bridge_vlan_group *br_vlan_group_rcu(
1667 const struct net_bridge *br)
1672 static inline struct net_bridge_vlan_group *nbp_vlan_group_rcu(
1673 const struct net_bridge_port *p)
1678 static inline void br_vlan_get_stats(const struct net_bridge_vlan *v,
1679 struct pcpu_sw_netstats *stats)
1683 static inline void br_vlan_port_event(struct net_bridge_port *p,
1684 unsigned long event)
1688 static inline int br_vlan_bridge_event(struct net_device *dev,
1689 unsigned long event, void *ptr)
1694 static inline void br_vlan_rtnl_init(void)
1698 static inline void br_vlan_rtnl_uninit(void)
1702 static inline void br_vlan_notify(const struct net_bridge *br,
1703 const struct net_bridge_port *p,
1704 u16 vid, u16 vid_range,
1709 static inline bool br_vlan_can_enter_range(const struct net_bridge_vlan *v_curr,
1710 const struct net_bridge_vlan *range_end)
1715 static inline int br_vlan_replay(struct net_device *br_dev,
1716 struct net_device *dev, const void *ctx,
1717 bool adding, struct notifier_block *nb,
1718 struct netlink_ext_ack *extack)
1724 /* br_vlan_options.c */
1725 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
1726 bool br_vlan_opts_eq_range(const struct net_bridge_vlan *v_curr,
1727 const struct net_bridge_vlan *range_end);
1728 bool br_vlan_opts_fill(struct sk_buff *skb, const struct net_bridge_vlan *v);
1729 size_t br_vlan_opts_nl_size(void);
1730 int br_vlan_process_options(const struct net_bridge *br,
1731 const struct net_bridge_port *p,
1732 struct net_bridge_vlan *range_start,
1733 struct net_bridge_vlan *range_end,
1735 struct netlink_ext_ack *extack);
1736 int br_vlan_rtm_process_global_options(struct net_device *dev,
1737 const struct nlattr *attr,
1739 struct netlink_ext_ack *extack);
1740 bool br_vlan_global_opts_can_enter_range(const struct net_bridge_vlan *v_curr,
1741 const struct net_bridge_vlan *r_end);
1742 bool br_vlan_global_opts_fill(struct sk_buff *skb, u16 vid, u16 vid_range,
1743 const struct net_bridge_vlan *v_opts);
1745 /* vlan state manipulation helpers using *_ONCE to annotate lock-free access */
1746 static inline u8 br_vlan_get_state(const struct net_bridge_vlan *v)
1748 return READ_ONCE(v->state);
1751 static inline void br_vlan_set_state(struct net_bridge_vlan *v, u8 state)
1753 WRITE_ONCE(v->state, state);
1756 static inline u8 br_vlan_get_pvid_state(const struct net_bridge_vlan_group *vg)
1758 return READ_ONCE(vg->pvid_state);
1761 static inline void br_vlan_set_pvid_state(struct net_bridge_vlan_group *vg,
1764 WRITE_ONCE(vg->pvid_state, state);
1767 /* learn_allow is true at ingress and false at egress */
1768 static inline bool br_vlan_state_allowed(u8 state, bool learn_allow)
1771 case BR_STATE_LEARNING:
1773 case BR_STATE_FORWARDING:
1782 int (*br_dev_xmit_hook)(struct sk_buff *skb);
1784 extern const struct nf_br_ops __rcu *nf_br_ops;
1786 /* br_netfilter.c */
1787 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1788 int br_nf_core_init(void);
1789 void br_nf_core_fini(void);
1790 void br_netfilter_rtable_init(struct net_bridge *);
1792 static inline int br_nf_core_init(void) { return 0; }
1793 static inline void br_nf_core_fini(void) {}
1794 #define br_netfilter_rtable_init(x)
1798 void br_set_state(struct net_bridge_port *p, unsigned int state);
1799 struct net_bridge_port *br_get_port(struct net_bridge *br, u16 port_no);
1800 void br_init_port(struct net_bridge_port *p);
1801 void br_become_designated_port(struct net_bridge_port *p);
1803 void __br_set_forward_delay(struct net_bridge *br, unsigned long t);
1804 int br_set_forward_delay(struct net_bridge *br, unsigned long x);
1805 int br_set_hello_time(struct net_bridge *br, unsigned long x);
1806 int br_set_max_age(struct net_bridge *br, unsigned long x);
1807 int __set_ageing_time(struct net_device *dev, unsigned long t);
1808 int br_set_ageing_time(struct net_bridge *br, clock_t ageing_time);
1812 void br_stp_enable_bridge(struct net_bridge *br);
1813 void br_stp_disable_bridge(struct net_bridge *br);
1814 int br_stp_set_enabled(struct net_bridge *br, unsigned long val,
1815 struct netlink_ext_ack *extack);
1816 void br_stp_enable_port(struct net_bridge_port *p);
1817 void br_stp_disable_port(struct net_bridge_port *p);
1818 bool br_stp_recalculate_bridge_id(struct net_bridge *br);
1819 void br_stp_change_bridge_id(struct net_bridge *br, const unsigned char *a);
1820 void br_stp_set_bridge_priority(struct net_bridge *br, u16 newprio);
1821 int br_stp_set_port_priority(struct net_bridge_port *p, unsigned long newprio);
1822 int br_stp_set_path_cost(struct net_bridge_port *p, unsigned long path_cost);
1823 ssize_t br_show_bridge_id(char *buf, const struct bridge_id *id);
1827 void br_stp_rcv(const struct stp_proto *proto, struct sk_buff *skb,
1828 struct net_device *dev);
1830 /* br_stp_timer.c */
1831 void br_stp_timer_init(struct net_bridge *br);
1832 void br_stp_port_timer_init(struct net_bridge_port *p);
1833 unsigned long br_timer_value(const struct timer_list *timer);
1836 #if IS_ENABLED(CONFIG_ATM_LANE)
1837 extern int (*br_fdb_test_addr_hook)(struct net_device *dev, unsigned char *addr);
1841 #if IS_ENABLED(CONFIG_BRIDGE_MRP)
1842 int br_mrp_parse(struct net_bridge *br, struct net_bridge_port *p,
1843 struct nlattr *attr, int cmd, struct netlink_ext_ack *extack);
1844 bool br_mrp_enabled(struct net_bridge *br);
1845 void br_mrp_port_del(struct net_bridge *br, struct net_bridge_port *p);
1846 int br_mrp_fill_info(struct sk_buff *skb, struct net_bridge *br);
1848 static inline int br_mrp_parse(struct net_bridge *br, struct net_bridge_port *p,
1849 struct nlattr *attr, int cmd,
1850 struct netlink_ext_ack *extack)
1855 static inline bool br_mrp_enabled(struct net_bridge *br)
1860 static inline void br_mrp_port_del(struct net_bridge *br,
1861 struct net_bridge_port *p)
1865 static inline int br_mrp_fill_info(struct sk_buff *skb, struct net_bridge *br)
1873 #if IS_ENABLED(CONFIG_BRIDGE_CFM)
1874 int br_cfm_parse(struct net_bridge *br, struct net_bridge_port *p,
1875 struct nlattr *attr, int cmd, struct netlink_ext_ack *extack);
1876 bool br_cfm_created(struct net_bridge *br);
1877 void br_cfm_port_del(struct net_bridge *br, struct net_bridge_port *p);
1878 int br_cfm_config_fill_info(struct sk_buff *skb, struct net_bridge *br);
1879 int br_cfm_status_fill_info(struct sk_buff *skb,
1880 struct net_bridge *br,
1882 int br_cfm_mep_count(struct net_bridge *br, u32 *count);
1883 int br_cfm_peer_mep_count(struct net_bridge *br, u32 *count);
1885 static inline int br_cfm_parse(struct net_bridge *br, struct net_bridge_port *p,
1886 struct nlattr *attr, int cmd,
1887 struct netlink_ext_ack *extack)
1892 static inline bool br_cfm_created(struct net_bridge *br)
1897 static inline void br_cfm_port_del(struct net_bridge *br,
1898 struct net_bridge_port *p)
1902 static inline int br_cfm_config_fill_info(struct sk_buff *skb, struct net_bridge *br)
1907 static inline int br_cfm_status_fill_info(struct sk_buff *skb,
1908 struct net_bridge *br,
1914 static inline int br_cfm_mep_count(struct net_bridge *br, u32 *count)
1919 static inline int br_cfm_peer_mep_count(struct net_bridge *br, u32 *count)
1926 extern struct rtnl_link_ops br_link_ops;
1927 int br_netlink_init(void);
1928 void br_netlink_fini(void);
1929 void br_ifinfo_notify(int event, const struct net_bridge *br,
1930 const struct net_bridge_port *port);
1931 void br_info_notify(int event, const struct net_bridge *br,
1932 const struct net_bridge_port *port, u32 filter);
1933 int br_setlink(struct net_device *dev, struct nlmsghdr *nlmsg, u16 flags,
1934 struct netlink_ext_ack *extack);
1935 int br_dellink(struct net_device *dev, struct nlmsghdr *nlmsg, u16 flags);
1936 int br_getlink(struct sk_buff *skb, u32 pid, u32 seq, struct net_device *dev,
1937 u32 filter_mask, int nlflags);
1938 int br_process_vlan_info(struct net_bridge *br,
1939 struct net_bridge_port *p, int cmd,
1940 struct bridge_vlan_info *vinfo_curr,
1941 struct bridge_vlan_info **vinfo_last,
1943 struct netlink_ext_ack *extack);
1947 extern const struct sysfs_ops brport_sysfs_ops;
1948 int br_sysfs_addif(struct net_bridge_port *p);
1949 int br_sysfs_renameif(struct net_bridge_port *p);
1952 int br_sysfs_addbr(struct net_device *dev);
1953 void br_sysfs_delbr(struct net_device *dev);
1957 static inline int br_sysfs_addif(struct net_bridge_port *p) { return 0; }
1958 static inline int br_sysfs_renameif(struct net_bridge_port *p) { return 0; }
1959 static inline int br_sysfs_addbr(struct net_device *dev) { return 0; }
1960 static inline void br_sysfs_delbr(struct net_device *dev) { return; }
1961 #endif /* CONFIG_SYSFS */
1963 /* br_switchdev.c */
1964 #ifdef CONFIG_NET_SWITCHDEV
1965 int br_switchdev_port_offload(struct net_bridge_port *p,
1966 struct net_device *dev, const void *ctx,
1967 struct notifier_block *atomic_nb,
1968 struct notifier_block *blocking_nb,
1969 bool tx_fwd_offload,
1970 struct netlink_ext_ack *extack);
1972 void br_switchdev_port_unoffload(struct net_bridge_port *p, const void *ctx,
1973 struct notifier_block *atomic_nb,
1974 struct notifier_block *blocking_nb);
1976 bool br_switchdev_frame_uses_tx_fwd_offload(struct sk_buff *skb);
1978 void br_switchdev_frame_set_offload_fwd_mark(struct sk_buff *skb);
1980 void nbp_switchdev_frame_mark_tx_fwd_offload(const struct net_bridge_port *p,
1981 struct sk_buff *skb);
1982 void nbp_switchdev_frame_mark_tx_fwd_to_hwdom(const struct net_bridge_port *p,
1983 struct sk_buff *skb);
1984 void nbp_switchdev_frame_mark(const struct net_bridge_port *p,
1985 struct sk_buff *skb);
1986 bool nbp_switchdev_allowed_egress(const struct net_bridge_port *p,
1987 const struct sk_buff *skb);
1988 int br_switchdev_set_port_flag(struct net_bridge_port *p,
1989 unsigned long flags,
1991 struct netlink_ext_ack *extack);
1992 void br_switchdev_fdb_notify(struct net_bridge *br,
1993 const struct net_bridge_fdb_entry *fdb, int type);
1994 int br_switchdev_port_vlan_add(struct net_device *dev, u16 vid, u16 flags,
1995 struct netlink_ext_ack *extack);
1996 int br_switchdev_port_vlan_del(struct net_device *dev, u16 vid);
1997 void br_switchdev_init(struct net_bridge *br);
1999 static inline void br_switchdev_frame_unmark(struct sk_buff *skb)
2001 skb->offload_fwd_mark = 0;
2005 br_switchdev_port_offload(struct net_bridge_port *p,
2006 struct net_device *dev, const void *ctx,
2007 struct notifier_block *atomic_nb,
2008 struct notifier_block *blocking_nb,
2009 bool tx_fwd_offload,
2010 struct netlink_ext_ack *extack)
2016 br_switchdev_port_unoffload(struct net_bridge_port *p, const void *ctx,
2017 struct notifier_block *atomic_nb,
2018 struct notifier_block *blocking_nb)
2022 static inline bool br_switchdev_frame_uses_tx_fwd_offload(struct sk_buff *skb)
2027 static inline void br_switchdev_frame_set_offload_fwd_mark(struct sk_buff *skb)
2032 nbp_switchdev_frame_mark_tx_fwd_offload(const struct net_bridge_port *p,
2033 struct sk_buff *skb)
2038 nbp_switchdev_frame_mark_tx_fwd_to_hwdom(const struct net_bridge_port *p,
2039 struct sk_buff *skb)
2043 static inline void nbp_switchdev_frame_mark(const struct net_bridge_port *p,
2044 struct sk_buff *skb)
2048 static inline bool nbp_switchdev_allowed_egress(const struct net_bridge_port *p,
2049 const struct sk_buff *skb)
2054 static inline int br_switchdev_set_port_flag(struct net_bridge_port *p,
2055 unsigned long flags,
2057 struct netlink_ext_ack *extack)
2062 static inline int br_switchdev_port_vlan_add(struct net_device *dev,
2064 struct netlink_ext_ack *extack)
2069 static inline int br_switchdev_port_vlan_del(struct net_device *dev, u16 vid)
2075 br_switchdev_fdb_notify(struct net_bridge *br,
2076 const struct net_bridge_fdb_entry *fdb, int type)
2080 static inline void br_switchdev_frame_unmark(struct sk_buff *skb)
2084 static inline void br_switchdev_init(struct net_bridge *br)
2088 #endif /* CONFIG_NET_SWITCHDEV */
2090 /* br_arp_nd_proxy.c */
2091 void br_recalculate_neigh_suppress_enabled(struct net_bridge *br);
2092 void br_do_proxy_suppress_arp(struct sk_buff *skb, struct net_bridge *br,
2093 u16 vid, struct net_bridge_port *p);
2094 void br_do_suppress_nd(struct sk_buff *skb, struct net_bridge *br,
2095 u16 vid, struct net_bridge_port *p, struct nd_msg *msg);
2096 struct nd_msg *br_is_nd_neigh_msg(struct sk_buff *skb, struct nd_msg *m);