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_VERSION "2.3"
34 /* Control of forwarding link local multicast */
35 #define BR_GROUPFWD_DEFAULT 0
36 /* Don't allow forwarding of control protocols like STP, MAC PAUSE and LACP */
38 BR_GROUPFWD_STP = BIT(0),
39 BR_GROUPFWD_MACPAUSE = BIT(1),
40 BR_GROUPFWD_LACP = BIT(2),
43 #define BR_GROUPFWD_RESTRICTED (BR_GROUPFWD_STP | BR_GROUPFWD_MACPAUSE | \
45 /* The Nearest Customer Bridge Group Address, 01-80-C2-00-00-[00,0B,0C,0D,0F] */
46 #define BR_GROUPFWD_8021AD 0xB801u
48 /* Path to usermode spanning tree program */
49 #define BR_STP_PROG "/sbin/bridge-stp"
51 #define BR_FDB_NOTIFY_SETTABLE_BITS (FDB_NOTIFY_BIT | FDB_NOTIFY_INACTIVE_BIT)
53 typedef struct bridge_id bridge_id;
54 typedef struct mac_addr mac_addr;
55 typedef __u16 port_id;
58 unsigned char prio[2];
59 unsigned char addr[ETH_ALEN];
63 unsigned char addr[ETH_ALEN];
66 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
68 struct bridge_mcast_own_query {
69 struct timer_list timer;
74 struct bridge_mcast_other_query {
75 struct timer_list timer;
76 unsigned long delay_time;
79 /* selected querier */
80 struct bridge_mcast_querier {
82 struct net_bridge_port __rcu *port;
85 /* IGMP/MLD statistics */
86 struct bridge_mcast_stats {
87 struct br_mcast_stats mstats;
88 struct u64_stats_sync syncp;
92 struct br_vlan_stats {
97 struct u64_stats_sync syncp;
100 struct br_tunnel_info {
102 struct metadata_dst *tunnel_dst;
105 /* private vlan flags */
107 BR_VLFLAG_PER_PORT_STATS = BIT(0),
108 BR_VLFLAG_ADDED_BY_SWITCHDEV = BIT(1),
112 * struct net_bridge_vlan - per-vlan entry
114 * @vnode: rhashtable member
116 * @flags: bridge vlan flags
117 * @priv_flags: private (in-kernel) bridge vlan flags
118 * @state: STP state (e.g. blocking, learning, forwarding)
119 * @stats: per-cpu VLAN statistics
120 * @br: if MASTER flag set, this points to a bridge struct
121 * @port: if MASTER flag unset, this points to a port struct
122 * @refcnt: if MASTER flag set, this is bumped for each port referencing it
123 * @brvlan: if MASTER flag unset, this points to the global per-VLAN context
124 * for this VLAN entry
125 * @vlist: sorted list of VLAN entries
126 * @rcu: used for entry destruction
128 * This structure is shared between the global per-VLAN entries contained in
129 * the bridge rhashtable and the local per-port per-VLAN entries contained in
130 * the port's rhashtable. The union entries should be interpreted depending on
131 * the entry flags that are set.
133 struct net_bridge_vlan {
134 struct rhash_head vnode;
135 struct rhash_head tnode;
140 struct br_vlan_stats __percpu *stats;
142 struct net_bridge *br;
143 struct net_bridge_port *port;
147 struct net_bridge_vlan *brvlan;
150 struct br_tunnel_info tinfo;
152 struct list_head vlist;
158 * struct net_bridge_vlan_group
160 * @vlan_hash: VLAN entry rhashtable
161 * @vlan_list: sorted VLAN entry list
162 * @num_vlans: number of total VLAN entries
163 * @pvid: PVID VLAN id
164 * @pvid_state: PVID's STP state (e.g. forwarding, learning, blocking)
166 * IMPORTANT: Be careful when checking if there're VLAN entries using list
167 * primitives because the bridge can have entries in its list which
168 * are just for global context but not for filtering, i.e. they have
169 * the master flag set but not the brentry flag. If you have to check
170 * if there're "real" entries in the bridge please test @num_vlans
172 struct net_bridge_vlan_group {
173 struct rhashtable vlan_hash;
174 struct rhashtable tunnel_hash;
175 struct list_head vlan_list;
181 /* bridge fdb flags */
186 BR_FDB_ADDED_BY_USER,
187 BR_FDB_ADDED_BY_EXT_LEARN,
190 BR_FDB_NOTIFY_INACTIVE
193 struct net_bridge_fdb_key {
198 struct net_bridge_fdb_entry {
199 struct rhash_head rhnode;
200 struct net_bridge_port *dst;
202 struct net_bridge_fdb_key key;
203 struct hlist_node fdb_node;
206 /* write-heavy members should not affect lookups */
207 unsigned long updated ____cacheline_aligned_in_smp;
213 #define MDB_PG_FLAGS_PERMANENT BIT(0)
214 #define MDB_PG_FLAGS_OFFLOAD BIT(1)
215 #define MDB_PG_FLAGS_FAST_LEAVE BIT(2)
217 struct net_bridge_port_group {
218 struct net_bridge_port *port;
219 struct net_bridge_port_group __rcu *next;
220 struct hlist_node mglist;
222 struct timer_list timer;
224 unsigned char eth_addr[ETH_ALEN] __aligned(2);
228 struct net_bridge_mdb_entry {
229 struct rhash_head rhnode;
230 struct net_bridge *br;
231 struct net_bridge_port_group __rcu *ports;
233 struct timer_list timer;
236 struct hlist_node mdb_node;
239 struct net_bridge_port {
240 struct net_bridge *br;
241 struct net_device *dev;
242 struct list_head list;
245 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
246 struct net_bridge_vlan_group __rcu *vlgrp;
248 struct net_bridge_port __rcu *backup_port;
254 unsigned char topology_change_ack;
255 unsigned char config_pending;
257 port_id designated_port;
258 bridge_id designated_root;
259 bridge_id designated_bridge;
262 unsigned long designated_age;
264 struct timer_list forward_delay_timer;
265 struct timer_list hold_timer;
266 struct timer_list message_age_timer;
270 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
271 struct bridge_mcast_own_query ip4_own_query;
272 #if IS_ENABLED(CONFIG_IPV6)
273 struct bridge_mcast_own_query ip6_own_query;
274 #endif /* IS_ENABLED(CONFIG_IPV6) */
275 unsigned char multicast_router;
276 struct bridge_mcast_stats __percpu *mcast_stats;
277 struct timer_list multicast_router_timer;
278 struct hlist_head mglist;
279 struct hlist_node rlist;
283 char sysfs_name[IFNAMSIZ];
286 #ifdef CONFIG_NET_POLL_CONTROLLER
289 #ifdef CONFIG_NET_SWITCHDEV
290 int offload_fwd_mark;
293 u16 backup_redirected_cnt;
295 struct bridge_stp_xstats stp_xstats;
298 #define kobj_to_brport(obj) container_of(obj, struct net_bridge_port, kobj)
300 #define br_auto_port(p) ((p)->flags & BR_AUTO_MASK)
301 #define br_promisc_port(p) ((p)->flags & BR_PROMISC)
303 static inline struct net_bridge_port *br_port_get_rcu(const struct net_device *dev)
305 return rcu_dereference(dev->rx_handler_data);
308 static inline struct net_bridge_port *br_port_get_rtnl(const struct net_device *dev)
310 return netif_is_bridge_port(dev) ?
311 rtnl_dereference(dev->rx_handler_data) : NULL;
314 static inline struct net_bridge_port *br_port_get_rtnl_rcu(const struct net_device *dev)
316 return netif_is_bridge_port(dev) ?
317 rcu_dereference_rtnl(dev->rx_handler_data) : NULL;
320 enum net_bridge_opts {
322 BROPT_VLAN_STATS_ENABLED,
323 BROPT_NF_CALL_IPTABLES,
324 BROPT_NF_CALL_IP6TABLES,
325 BROPT_NF_CALL_ARPTABLES,
326 BROPT_GROUP_ADDR_SET,
327 BROPT_MULTICAST_ENABLED,
328 BROPT_MULTICAST_QUERIER,
329 BROPT_MULTICAST_QUERY_USE_IFADDR,
330 BROPT_MULTICAST_STATS_ENABLED,
332 BROPT_NEIGH_SUPPRESS_ENABLED,
333 BROPT_MTU_SET_BY_USER,
334 BROPT_VLAN_STATS_PER_PORT,
336 BROPT_VLAN_BRIDGE_BINDING,
341 spinlock_t hash_lock;
342 struct list_head port_list;
343 struct net_device *dev;
344 struct pcpu_sw_netstats __percpu *stats;
345 unsigned long options;
346 /* These fields are accessed on each packet */
347 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
350 struct net_bridge_vlan_group __rcu *vlgrp;
353 struct rhashtable fdb_hash_tbl;
354 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
356 struct rtable fake_rtable;
357 struct rt6_info fake_rt6_info;
361 u16 group_fwd_mask_required;
364 bridge_id designated_root;
366 unsigned char topology_change;
367 unsigned char topology_change_detected;
369 unsigned long max_age;
370 unsigned long hello_time;
371 unsigned long forward_delay;
372 unsigned long ageing_time;
373 unsigned long bridge_max_age;
374 unsigned long bridge_hello_time;
375 unsigned long bridge_forward_delay;
376 unsigned long bridge_ageing_time;
379 u8 group_addr[ETH_ALEN];
382 BR_NO_STP, /* no spanning tree */
383 BR_KERNEL_STP, /* old STP in kernel */
384 BR_USER_STP, /* new RSTP in userspace */
387 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
391 u32 multicast_last_member_count;
392 u32 multicast_startup_query_count;
394 u8 multicast_igmp_version;
396 #if IS_ENABLED(CONFIG_IPV6)
397 u8 multicast_mld_version;
399 spinlock_t multicast_lock;
400 unsigned long multicast_last_member_interval;
401 unsigned long multicast_membership_interval;
402 unsigned long multicast_querier_interval;
403 unsigned long multicast_query_interval;
404 unsigned long multicast_query_response_interval;
405 unsigned long multicast_startup_query_interval;
407 struct rhashtable mdb_hash_tbl;
409 struct hlist_head mdb_list;
410 struct hlist_head router_list;
412 struct timer_list multicast_router_timer;
413 struct bridge_mcast_other_query ip4_other_query;
414 struct bridge_mcast_own_query ip4_own_query;
415 struct bridge_mcast_querier ip4_querier;
416 struct bridge_mcast_stats __percpu *mcast_stats;
417 #if IS_ENABLED(CONFIG_IPV6)
418 struct bridge_mcast_other_query ip6_other_query;
419 struct bridge_mcast_own_query ip6_own_query;
420 struct bridge_mcast_querier ip6_querier;
421 #endif /* IS_ENABLED(CONFIG_IPV6) */
424 struct timer_list hello_timer;
425 struct timer_list tcn_timer;
426 struct timer_list topology_change_timer;
427 struct delayed_work gc_work;
428 struct kobject *ifobj;
431 #ifdef CONFIG_NET_SWITCHDEV
432 int offload_fwd_mark;
434 struct hlist_head fdb_list;
436 #if IS_ENABLED(CONFIG_BRIDGE_MRP)
437 struct list_head mrp_list;
441 struct br_input_skb_cb {
442 struct net_device *brdev;
445 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
449 u8 proxyarp_replied:1;
450 u8 src_port_isolated:1;
451 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
454 #ifdef CONFIG_NETFILTER_FAMILY_BRIDGE
455 u8 br_netfilter_broute:1;
458 #ifdef CONFIG_NET_SWITCHDEV
459 int offload_fwd_mark;
463 #define BR_INPUT_SKB_CB(__skb) ((struct br_input_skb_cb *)(__skb)->cb)
465 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
466 # define BR_INPUT_SKB_CB_MROUTERS_ONLY(__skb) (BR_INPUT_SKB_CB(__skb)->mrouters_only)
468 # define BR_INPUT_SKB_CB_MROUTERS_ONLY(__skb) (0)
471 #define br_printk(level, br, format, args...) \
472 printk(level "%s: " format, (br)->dev->name, ##args)
474 #define br_err(__br, format, args...) \
475 br_printk(KERN_ERR, __br, format, ##args)
476 #define br_warn(__br, format, args...) \
477 br_printk(KERN_WARNING, __br, format, ##args)
478 #define br_notice(__br, format, args...) \
479 br_printk(KERN_NOTICE, __br, format, ##args)
480 #define br_info(__br, format, args...) \
481 br_printk(KERN_INFO, __br, format, ##args)
483 #define br_debug(br, format, args...) \
484 pr_debug("%s: " format, (br)->dev->name, ##args)
486 /* called under bridge lock */
487 static inline int br_is_root_bridge(const struct net_bridge *br)
489 return !memcmp(&br->bridge_id, &br->designated_root, 8);
492 /* check if a VLAN entry is global */
493 static inline bool br_vlan_is_master(const struct net_bridge_vlan *v)
495 return v->flags & BRIDGE_VLAN_INFO_MASTER;
498 /* check if a VLAN entry is used by the bridge */
499 static inline bool br_vlan_is_brentry(const struct net_bridge_vlan *v)
501 return v->flags & BRIDGE_VLAN_INFO_BRENTRY;
504 /* check if we should use the vlan entry, returns false if it's only context */
505 static inline bool br_vlan_should_use(const struct net_bridge_vlan *v)
507 if (br_vlan_is_master(v)) {
508 if (br_vlan_is_brentry(v))
517 static inline bool nbp_state_should_learn(const struct net_bridge_port *p)
519 return p->state == BR_STATE_LEARNING || p->state == BR_STATE_FORWARDING;
522 static inline bool br_vlan_valid_id(u16 vid, struct netlink_ext_ack *extack)
524 bool ret = vid > 0 && vid < VLAN_VID_MASK;
527 NL_SET_ERR_MSG_MOD(extack, "Vlan id is invalid");
532 static inline bool br_vlan_valid_range(const struct bridge_vlan_info *cur,
533 const struct bridge_vlan_info *last,
534 struct netlink_ext_ack *extack)
536 /* pvid flag is not allowed in ranges */
537 if (cur->flags & BRIDGE_VLAN_INFO_PVID) {
538 NL_SET_ERR_MSG_MOD(extack, "Pvid isn't allowed in a range");
542 /* when cur is the range end, check if:
543 * - it has range start flag
544 * - range ids are invalid (end is equal to or before start)
547 if (cur->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
548 NL_SET_ERR_MSG_MOD(extack, "Found a new vlan range start while processing one");
550 } else if (!(cur->flags & BRIDGE_VLAN_INFO_RANGE_END)) {
551 NL_SET_ERR_MSG_MOD(extack, "Vlan range end flag is missing");
553 } else if (cur->vid <= last->vid) {
554 NL_SET_ERR_MSG_MOD(extack, "End vlan id is less than or equal to start vlan id");
559 /* check for required range flags */
560 if (!(cur->flags & (BRIDGE_VLAN_INFO_RANGE_BEGIN |
561 BRIDGE_VLAN_INFO_RANGE_END))) {
562 NL_SET_ERR_MSG_MOD(extack, "Both vlan range flags are missing");
569 static inline int br_afspec_cmd_to_rtm(int cmd)
581 static inline int br_opt_get(const struct net_bridge *br,
582 enum net_bridge_opts opt)
584 return test_bit(opt, &br->options);
587 int br_boolopt_toggle(struct net_bridge *br, enum br_boolopt_id opt, bool on,
588 struct netlink_ext_ack *extack);
589 int br_boolopt_get(const struct net_bridge *br, enum br_boolopt_id opt);
590 int br_boolopt_multi_toggle(struct net_bridge *br,
591 struct br_boolopt_multi *bm,
592 struct netlink_ext_ack *extack);
593 void br_boolopt_multi_get(const struct net_bridge *br,
594 struct br_boolopt_multi *bm);
595 void br_opt_toggle(struct net_bridge *br, enum net_bridge_opts opt, bool on);
598 void br_dev_setup(struct net_device *dev);
599 void br_dev_delete(struct net_device *dev, struct list_head *list);
600 netdev_tx_t br_dev_xmit(struct sk_buff *skb, struct net_device *dev);
601 #ifdef CONFIG_NET_POLL_CONTROLLER
602 static inline void br_netpoll_send_skb(const struct net_bridge_port *p,
605 netpoll_send_skb(p->np, skb);
608 int br_netpoll_enable(struct net_bridge_port *p);
609 void br_netpoll_disable(struct net_bridge_port *p);
611 static inline void br_netpoll_send_skb(const struct net_bridge_port *p,
616 static inline int br_netpoll_enable(struct net_bridge_port *p)
621 static inline void br_netpoll_disable(struct net_bridge_port *p)
627 int br_fdb_init(void);
628 void br_fdb_fini(void);
629 int br_fdb_hash_init(struct net_bridge *br);
630 void br_fdb_hash_fini(struct net_bridge *br);
631 void br_fdb_flush(struct net_bridge *br);
632 void br_fdb_find_delete_local(struct net_bridge *br,
633 const struct net_bridge_port *p,
634 const unsigned char *addr, u16 vid);
635 void br_fdb_changeaddr(struct net_bridge_port *p, const unsigned char *newaddr);
636 void br_fdb_change_mac_address(struct net_bridge *br, const u8 *newaddr);
637 void br_fdb_cleanup(struct work_struct *work);
638 void br_fdb_delete_by_port(struct net_bridge *br,
639 const struct net_bridge_port *p, u16 vid, int do_all);
640 struct net_bridge_fdb_entry *br_fdb_find_rcu(struct net_bridge *br,
641 const unsigned char *addr,
643 int br_fdb_test_addr(struct net_device *dev, unsigned char *addr);
644 int br_fdb_fillbuf(struct net_bridge *br, void *buf, unsigned long count,
646 int br_fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
647 const unsigned char *addr, u16 vid);
648 void br_fdb_update(struct net_bridge *br, struct net_bridge_port *source,
649 const unsigned char *addr, u16 vid, unsigned long flags);
651 int br_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
652 struct net_device *dev, const unsigned char *addr, u16 vid);
653 int br_fdb_add(struct ndmsg *nlh, struct nlattr *tb[], struct net_device *dev,
654 const unsigned char *addr, u16 vid, u16 nlh_flags,
655 struct netlink_ext_ack *extack);
656 int br_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
657 struct net_device *dev, struct net_device *fdev, int *idx);
658 int br_fdb_get(struct sk_buff *skb, struct nlattr *tb[], struct net_device *dev,
659 const unsigned char *addr, u16 vid, u32 portid, u32 seq,
660 struct netlink_ext_ack *extack);
661 int br_fdb_sync_static(struct net_bridge *br, struct net_bridge_port *p);
662 void br_fdb_unsync_static(struct net_bridge *br, struct net_bridge_port *p);
663 int br_fdb_external_learn_add(struct net_bridge *br, struct net_bridge_port *p,
664 const unsigned char *addr, u16 vid,
666 int br_fdb_external_learn_del(struct net_bridge *br, struct net_bridge_port *p,
667 const unsigned char *addr, u16 vid,
669 void br_fdb_offloaded_set(struct net_bridge *br, struct net_bridge_port *p,
670 const unsigned char *addr, u16 vid, bool offloaded);
678 int br_dev_queue_push_xmit(struct net *net, struct sock *sk, struct sk_buff *skb);
679 void br_forward(const struct net_bridge_port *to, struct sk_buff *skb,
680 bool local_rcv, bool local_orig);
681 int br_forward_finish(struct net *net, struct sock *sk, struct sk_buff *skb);
682 void br_flood(struct net_bridge *br, struct sk_buff *skb,
683 enum br_pkt_type pkt_type, bool local_rcv, bool local_orig);
685 /* return true if both source port and dest port are isolated */
686 static inline bool br_skb_isolated(const struct net_bridge_port *to,
687 const struct sk_buff *skb)
689 return BR_INPUT_SKB_CB(skb)->src_port_isolated &&
690 (to->flags & BR_ISOLATED);
694 void br_port_carrier_check(struct net_bridge_port *p, bool *notified);
695 int br_add_bridge(struct net *net, const char *name);
696 int br_del_bridge(struct net *net, const char *name);
697 int br_add_if(struct net_bridge *br, struct net_device *dev,
698 struct netlink_ext_ack *extack);
699 int br_del_if(struct net_bridge *br, struct net_device *dev);
700 void br_mtu_auto_adjust(struct net_bridge *br);
701 netdev_features_t br_features_recompute(struct net_bridge *br,
702 netdev_features_t features);
703 void br_port_flags_change(struct net_bridge_port *port, unsigned long mask);
704 void br_manage_promisc(struct net_bridge *br);
705 int nbp_backup_change(struct net_bridge_port *p, struct net_device *backup_dev);
708 int br_handle_frame_finish(struct net *net, struct sock *sk, struct sk_buff *skb);
709 rx_handler_func_t *br_get_rx_handler(const struct net_device *dev);
711 static inline bool br_rx_handler_check_rcu(const struct net_device *dev)
713 return rcu_dereference(dev->rx_handler) == br_get_rx_handler(dev);
716 static inline bool br_rx_handler_check_rtnl(const struct net_device *dev)
718 return rcu_dereference_rtnl(dev->rx_handler) == br_get_rx_handler(dev);
721 static inline struct net_bridge_port *br_port_get_check_rcu(const struct net_device *dev)
723 return br_rx_handler_check_rcu(dev) ? br_port_get_rcu(dev) : NULL;
726 static inline struct net_bridge_port *
727 br_port_get_check_rtnl(const struct net_device *dev)
729 return br_rx_handler_check_rtnl(dev) ? br_port_get_rtnl_rcu(dev) : NULL;
733 int br_dev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
734 int br_ioctl_deviceless_stub(struct net *net, unsigned int cmd,
738 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
739 int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port,
740 struct sk_buff *skb, u16 vid);
741 struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br,
742 struct sk_buff *skb, u16 vid);
743 int br_multicast_add_port(struct net_bridge_port *port);
744 void br_multicast_del_port(struct net_bridge_port *port);
745 void br_multicast_enable_port(struct net_bridge_port *port);
746 void br_multicast_disable_port(struct net_bridge_port *port);
747 void br_multicast_init(struct net_bridge *br);
748 void br_multicast_open(struct net_bridge *br);
749 void br_multicast_stop(struct net_bridge *br);
750 void br_multicast_dev_del(struct net_bridge *br);
751 void br_multicast_flood(struct net_bridge_mdb_entry *mdst,
752 struct sk_buff *skb, bool local_rcv, bool local_orig);
753 int br_multicast_set_router(struct net_bridge *br, unsigned long val);
754 int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val);
755 int br_multicast_toggle(struct net_bridge *br, unsigned long val);
756 int br_multicast_set_querier(struct net_bridge *br, unsigned long val);
757 int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val);
758 int br_multicast_set_igmp_version(struct net_bridge *br, unsigned long val);
759 #if IS_ENABLED(CONFIG_IPV6)
760 int br_multicast_set_mld_version(struct net_bridge *br, unsigned long val);
762 struct net_bridge_mdb_entry *
763 br_mdb_ip_get(struct net_bridge *br, struct br_ip *dst);
764 struct net_bridge_mdb_entry *
765 br_multicast_new_group(struct net_bridge *br, struct br_ip *group);
766 struct net_bridge_port_group *
767 br_multicast_new_port_group(struct net_bridge_port *port, struct br_ip *group,
768 struct net_bridge_port_group __rcu *next,
769 unsigned char flags, const unsigned char *src);
770 int br_mdb_hash_init(struct net_bridge *br);
771 void br_mdb_hash_fini(struct net_bridge *br);
772 void br_mdb_notify(struct net_device *dev, struct net_bridge_port *port,
773 struct br_ip *group, int type, u8 flags);
774 void br_rtr_notify(struct net_device *dev, struct net_bridge_port *port,
776 void br_multicast_count(struct net_bridge *br, const struct net_bridge_port *p,
777 const struct sk_buff *skb, u8 type, u8 dir);
778 int br_multicast_init_stats(struct net_bridge *br);
779 void br_multicast_uninit_stats(struct net_bridge *br);
780 void br_multicast_get_stats(const struct net_bridge *br,
781 const struct net_bridge_port *p,
782 struct br_mcast_stats *dest);
783 void br_mdb_init(void);
784 void br_mdb_uninit(void);
785 void br_multicast_host_join(struct net_bridge_mdb_entry *mp, bool notify);
786 void br_multicast_host_leave(struct net_bridge_mdb_entry *mp, bool notify);
788 #define mlock_dereference(X, br) \
789 rcu_dereference_protected(X, lockdep_is_held(&br->multicast_lock))
791 static inline bool br_multicast_is_router(struct net_bridge *br)
793 return br->multicast_router == 2 ||
794 (br->multicast_router == 1 &&
795 timer_pending(&br->multicast_router_timer));
799 __br_multicast_querier_exists(struct net_bridge *br,
800 struct bridge_mcast_other_query *querier,
803 bool own_querier_enabled;
805 if (br_opt_get(br, BROPT_MULTICAST_QUERIER)) {
806 if (is_ipv6 && !br_opt_get(br, BROPT_HAS_IPV6_ADDR))
807 own_querier_enabled = false;
809 own_querier_enabled = true;
811 own_querier_enabled = false;
814 return time_is_before_jiffies(querier->delay_time) &&
815 (own_querier_enabled || timer_pending(&querier->timer));
818 static inline bool br_multicast_querier_exists(struct net_bridge *br,
821 switch (eth->h_proto) {
822 case (htons(ETH_P_IP)):
823 return __br_multicast_querier_exists(br,
824 &br->ip4_other_query, false);
825 #if IS_ENABLED(CONFIG_IPV6)
826 case (htons(ETH_P_IPV6)):
827 return __br_multicast_querier_exists(br,
828 &br->ip6_other_query, true);
835 static inline int br_multicast_igmp_type(const struct sk_buff *skb)
837 return BR_INPUT_SKB_CB(skb)->igmp;
840 static inline int br_multicast_rcv(struct net_bridge *br,
841 struct net_bridge_port *port,
848 static inline struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br,
849 struct sk_buff *skb, u16 vid)
854 static inline int br_multicast_add_port(struct net_bridge_port *port)
859 static inline void br_multicast_del_port(struct net_bridge_port *port)
863 static inline void br_multicast_enable_port(struct net_bridge_port *port)
867 static inline void br_multicast_disable_port(struct net_bridge_port *port)
871 static inline void br_multicast_init(struct net_bridge *br)
875 static inline void br_multicast_open(struct net_bridge *br)
879 static inline void br_multicast_stop(struct net_bridge *br)
883 static inline void br_multicast_dev_del(struct net_bridge *br)
887 static inline void br_multicast_flood(struct net_bridge_mdb_entry *mdst,
889 bool local_rcv, bool local_orig)
893 static inline bool br_multicast_is_router(struct net_bridge *br)
898 static inline bool br_multicast_querier_exists(struct net_bridge *br,
904 static inline void br_mdb_init(void)
908 static inline void br_mdb_uninit(void)
912 static inline int br_mdb_hash_init(struct net_bridge *br)
917 static inline void br_mdb_hash_fini(struct net_bridge *br)
921 static inline void br_multicast_count(struct net_bridge *br,
922 const struct net_bridge_port *p,
923 const struct sk_buff *skb,
928 static inline int br_multicast_init_stats(struct net_bridge *br)
933 static inline void br_multicast_uninit_stats(struct net_bridge *br)
937 static inline int br_multicast_igmp_type(const struct sk_buff *skb)
944 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
945 bool br_allowed_ingress(const struct net_bridge *br,
946 struct net_bridge_vlan_group *vg, struct sk_buff *skb,
947 u16 *vid, u8 *state);
948 bool br_allowed_egress(struct net_bridge_vlan_group *vg,
949 const struct sk_buff *skb);
950 bool br_should_learn(struct net_bridge_port *p, struct sk_buff *skb, u16 *vid);
951 struct sk_buff *br_handle_vlan(struct net_bridge *br,
952 const struct net_bridge_port *port,
953 struct net_bridge_vlan_group *vg,
954 struct sk_buff *skb);
955 int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags,
956 bool *changed, struct netlink_ext_ack *extack);
957 int br_vlan_delete(struct net_bridge *br, u16 vid);
958 void br_vlan_flush(struct net_bridge *br);
959 struct net_bridge_vlan *br_vlan_find(struct net_bridge_vlan_group *vg, u16 vid);
960 void br_recalculate_fwd_mask(struct net_bridge *br);
961 int __br_vlan_filter_toggle(struct net_bridge *br, unsigned long val);
962 int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val);
963 int __br_vlan_set_proto(struct net_bridge *br, __be16 proto);
964 int br_vlan_set_proto(struct net_bridge *br, unsigned long val);
965 int br_vlan_set_stats(struct net_bridge *br, unsigned long val);
966 int br_vlan_set_stats_per_port(struct net_bridge *br, unsigned long val);
967 int br_vlan_init(struct net_bridge *br);
968 int br_vlan_set_default_pvid(struct net_bridge *br, unsigned long val);
969 int __br_vlan_set_default_pvid(struct net_bridge *br, u16 pvid,
970 struct netlink_ext_ack *extack);
971 int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags,
972 bool *changed, struct netlink_ext_ack *extack);
973 int nbp_vlan_delete(struct net_bridge_port *port, u16 vid);
974 void nbp_vlan_flush(struct net_bridge_port *port);
975 int nbp_vlan_init(struct net_bridge_port *port, struct netlink_ext_ack *extack);
976 int nbp_get_num_vlan_infos(struct net_bridge_port *p, u32 filter_mask);
977 void br_vlan_get_stats(const struct net_bridge_vlan *v,
978 struct br_vlan_stats *stats);
979 void br_vlan_port_event(struct net_bridge_port *p, unsigned long event);
980 int br_vlan_bridge_event(struct net_device *dev, unsigned long event,
982 void br_vlan_rtnl_init(void);
983 void br_vlan_rtnl_uninit(void);
984 void br_vlan_notify(const struct net_bridge *br,
985 const struct net_bridge_port *p,
986 u16 vid, u16 vid_range,
988 bool br_vlan_can_enter_range(const struct net_bridge_vlan *v_curr,
989 const struct net_bridge_vlan *range_end);
991 static inline struct net_bridge_vlan_group *br_vlan_group(
992 const struct net_bridge *br)
994 return rtnl_dereference(br->vlgrp);
997 static inline struct net_bridge_vlan_group *nbp_vlan_group(
998 const struct net_bridge_port *p)
1000 return rtnl_dereference(p->vlgrp);
1003 static inline struct net_bridge_vlan_group *br_vlan_group_rcu(
1004 const struct net_bridge *br)
1006 return rcu_dereference(br->vlgrp);
1009 static inline struct net_bridge_vlan_group *nbp_vlan_group_rcu(
1010 const struct net_bridge_port *p)
1012 return rcu_dereference(p->vlgrp);
1015 /* Since bridge now depends on 8021Q module, but the time bridge sees the
1016 * skb, the vlan tag will always be present if the frame was tagged.
1018 static inline int br_vlan_get_tag(const struct sk_buff *skb, u16 *vid)
1022 if (skb_vlan_tag_present(skb)) {
1023 *vid = skb_vlan_tag_get_id(skb);
1032 static inline u16 br_get_pvid(const struct net_bridge_vlan_group *vg)
1041 static inline u16 br_vlan_flags(const struct net_bridge_vlan *v, u16 pvid)
1043 return v->vid == pvid ? v->flags | BRIDGE_VLAN_INFO_PVID : v->flags;
1046 static inline bool br_allowed_ingress(const struct net_bridge *br,
1047 struct net_bridge_vlan_group *vg,
1048 struct sk_buff *skb,
1049 u16 *vid, u8 *state)
1054 static inline bool br_allowed_egress(struct net_bridge_vlan_group *vg,
1055 const struct sk_buff *skb)
1060 static inline bool br_should_learn(struct net_bridge_port *p,
1061 struct sk_buff *skb, u16 *vid)
1066 static inline struct sk_buff *br_handle_vlan(struct net_bridge *br,
1067 const struct net_bridge_port *port,
1068 struct net_bridge_vlan_group *vg,
1069 struct sk_buff *skb)
1074 static inline int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags,
1075 bool *changed, struct netlink_ext_ack *extack)
1081 static inline int br_vlan_delete(struct net_bridge *br, u16 vid)
1086 static inline void br_vlan_flush(struct net_bridge *br)
1090 static inline void br_recalculate_fwd_mask(struct net_bridge *br)
1094 static inline int br_vlan_init(struct net_bridge *br)
1099 static inline int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags,
1100 bool *changed, struct netlink_ext_ack *extack)
1106 static inline int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
1111 static inline void nbp_vlan_flush(struct net_bridge_port *port)
1115 static inline struct net_bridge_vlan *br_vlan_find(struct net_bridge_vlan_group *vg,
1121 static inline int nbp_vlan_init(struct net_bridge_port *port,
1122 struct netlink_ext_ack *extack)
1127 static inline u16 br_vlan_get_tag(const struct sk_buff *skb, u16 *tag)
1132 static inline u16 br_get_pvid(const struct net_bridge_vlan_group *vg)
1137 static inline int __br_vlan_filter_toggle(struct net_bridge *br,
1143 static inline int nbp_get_num_vlan_infos(struct net_bridge_port *p,
1149 static inline struct net_bridge_vlan_group *br_vlan_group(
1150 const struct net_bridge *br)
1155 static inline struct net_bridge_vlan_group *nbp_vlan_group(
1156 const struct net_bridge_port *p)
1161 static inline struct net_bridge_vlan_group *br_vlan_group_rcu(
1162 const struct net_bridge *br)
1167 static inline struct net_bridge_vlan_group *nbp_vlan_group_rcu(
1168 const struct net_bridge_port *p)
1173 static inline void br_vlan_get_stats(const struct net_bridge_vlan *v,
1174 struct br_vlan_stats *stats)
1178 static inline void br_vlan_port_event(struct net_bridge_port *p,
1179 unsigned long event)
1183 static inline int br_vlan_bridge_event(struct net_device *dev,
1184 unsigned long event, void *ptr)
1189 static inline void br_vlan_rtnl_init(void)
1193 static inline void br_vlan_rtnl_uninit(void)
1197 static inline void br_vlan_notify(const struct net_bridge *br,
1198 const struct net_bridge_port *p,
1199 u16 vid, u16 vid_range,
1204 static inline bool br_vlan_can_enter_range(const struct net_bridge_vlan *v_curr,
1205 const struct net_bridge_vlan *range_end)
1211 /* br_vlan_options.c */
1212 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
1213 bool br_vlan_opts_eq_range(const struct net_bridge_vlan *v_curr,
1214 const struct net_bridge_vlan *range_end);
1215 bool br_vlan_opts_fill(struct sk_buff *skb, const struct net_bridge_vlan *v);
1216 size_t br_vlan_opts_nl_size(void);
1217 int br_vlan_process_options(const struct net_bridge *br,
1218 const struct net_bridge_port *p,
1219 struct net_bridge_vlan *range_start,
1220 struct net_bridge_vlan *range_end,
1222 struct netlink_ext_ack *extack);
1224 /* vlan state manipulation helpers using *_ONCE to annotate lock-free access */
1225 static inline u8 br_vlan_get_state(const struct net_bridge_vlan *v)
1227 return READ_ONCE(v->state);
1230 static inline void br_vlan_set_state(struct net_bridge_vlan *v, u8 state)
1232 WRITE_ONCE(v->state, state);
1235 static inline u8 br_vlan_get_pvid_state(const struct net_bridge_vlan_group *vg)
1237 return READ_ONCE(vg->pvid_state);
1240 static inline void br_vlan_set_pvid_state(struct net_bridge_vlan_group *vg,
1243 WRITE_ONCE(vg->pvid_state, state);
1246 /* learn_allow is true at ingress and false at egress */
1247 static inline bool br_vlan_state_allowed(u8 state, bool learn_allow)
1250 case BR_STATE_LEARNING:
1252 case BR_STATE_FORWARDING:
1261 int (*br_dev_xmit_hook)(struct sk_buff *skb);
1263 extern const struct nf_br_ops __rcu *nf_br_ops;
1265 /* br_netfilter.c */
1266 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1267 int br_nf_core_init(void);
1268 void br_nf_core_fini(void);
1269 void br_netfilter_rtable_init(struct net_bridge *);
1271 static inline int br_nf_core_init(void) { return 0; }
1272 static inline void br_nf_core_fini(void) {}
1273 #define br_netfilter_rtable_init(x)
1277 void br_set_state(struct net_bridge_port *p, unsigned int state);
1278 struct net_bridge_port *br_get_port(struct net_bridge *br, u16 port_no);
1279 void br_init_port(struct net_bridge_port *p);
1280 void br_become_designated_port(struct net_bridge_port *p);
1282 void __br_set_forward_delay(struct net_bridge *br, unsigned long t);
1283 int br_set_forward_delay(struct net_bridge *br, unsigned long x);
1284 int br_set_hello_time(struct net_bridge *br, unsigned long x);
1285 int br_set_max_age(struct net_bridge *br, unsigned long x);
1286 int __set_ageing_time(struct net_device *dev, unsigned long t);
1287 int br_set_ageing_time(struct net_bridge *br, clock_t ageing_time);
1291 void br_stp_enable_bridge(struct net_bridge *br);
1292 void br_stp_disable_bridge(struct net_bridge *br);
1293 int br_stp_set_enabled(struct net_bridge *br, unsigned long val,
1294 struct netlink_ext_ack *extack);
1295 void br_stp_enable_port(struct net_bridge_port *p);
1296 void br_stp_disable_port(struct net_bridge_port *p);
1297 bool br_stp_recalculate_bridge_id(struct net_bridge *br);
1298 void br_stp_change_bridge_id(struct net_bridge *br, const unsigned char *a);
1299 void br_stp_set_bridge_priority(struct net_bridge *br, u16 newprio);
1300 int br_stp_set_port_priority(struct net_bridge_port *p, unsigned long newprio);
1301 int br_stp_set_path_cost(struct net_bridge_port *p, unsigned long path_cost);
1302 ssize_t br_show_bridge_id(char *buf, const struct bridge_id *id);
1306 void br_stp_rcv(const struct stp_proto *proto, struct sk_buff *skb,
1307 struct net_device *dev);
1309 /* br_stp_timer.c */
1310 void br_stp_timer_init(struct net_bridge *br);
1311 void br_stp_port_timer_init(struct net_bridge_port *p);
1312 unsigned long br_timer_value(const struct timer_list *timer);
1315 #if IS_ENABLED(CONFIG_ATM_LANE)
1316 extern int (*br_fdb_test_addr_hook)(struct net_device *dev, unsigned char *addr);
1320 #if IS_ENABLED(CONFIG_BRIDGE_MRP)
1321 int br_mrp_parse(struct net_bridge *br, struct net_bridge_port *p,
1322 struct nlattr *attr, int cmd, struct netlink_ext_ack *extack);
1323 int br_mrp_process(struct net_bridge_port *p, struct sk_buff *skb);
1324 bool br_mrp_enabled(struct net_bridge *br);
1325 void br_mrp_port_del(struct net_bridge *br, struct net_bridge_port *p);
1326 int br_mrp_fill_info(struct sk_buff *skb, struct net_bridge *br);
1328 static inline int br_mrp_parse(struct net_bridge *br, struct net_bridge_port *p,
1329 struct nlattr *attr, int cmd,
1330 struct netlink_ext_ack *extack)
1335 static inline int br_mrp_process(struct net_bridge_port *p, struct sk_buff *skb)
1340 static inline bool br_mrp_enabled(struct net_bridge *br)
1345 static inline void br_mrp_port_del(struct net_bridge *br,
1346 struct net_bridge_port *p)
1350 static inline int br_mrp_fill_info(struct sk_buff *skb, struct net_bridge *br)
1358 extern struct rtnl_link_ops br_link_ops;
1359 int br_netlink_init(void);
1360 void br_netlink_fini(void);
1361 void br_ifinfo_notify(int event, const struct net_bridge *br,
1362 const struct net_bridge_port *port);
1363 int br_setlink(struct net_device *dev, struct nlmsghdr *nlmsg, u16 flags,
1364 struct netlink_ext_ack *extack);
1365 int br_dellink(struct net_device *dev, struct nlmsghdr *nlmsg, u16 flags);
1366 int br_getlink(struct sk_buff *skb, u32 pid, u32 seq, struct net_device *dev,
1367 u32 filter_mask, int nlflags);
1368 int br_process_vlan_info(struct net_bridge *br,
1369 struct net_bridge_port *p, int cmd,
1370 struct bridge_vlan_info *vinfo_curr,
1371 struct bridge_vlan_info **vinfo_last,
1373 struct netlink_ext_ack *extack);
1377 extern const struct sysfs_ops brport_sysfs_ops;
1378 int br_sysfs_addif(struct net_bridge_port *p);
1379 int br_sysfs_renameif(struct net_bridge_port *p);
1382 int br_sysfs_addbr(struct net_device *dev);
1383 void br_sysfs_delbr(struct net_device *dev);
1387 static inline int br_sysfs_addif(struct net_bridge_port *p) { return 0; }
1388 static inline int br_sysfs_renameif(struct net_bridge_port *p) { return 0; }
1389 static inline int br_sysfs_addbr(struct net_device *dev) { return 0; }
1390 static inline void br_sysfs_delbr(struct net_device *dev) { return; }
1391 #endif /* CONFIG_SYSFS */
1393 /* br_switchdev.c */
1394 #ifdef CONFIG_NET_SWITCHDEV
1395 int nbp_switchdev_mark_set(struct net_bridge_port *p);
1396 void nbp_switchdev_frame_mark(const struct net_bridge_port *p,
1397 struct sk_buff *skb);
1398 bool nbp_switchdev_allowed_egress(const struct net_bridge_port *p,
1399 const struct sk_buff *skb);
1400 int br_switchdev_set_port_flag(struct net_bridge_port *p,
1401 unsigned long flags,
1402 unsigned long mask);
1403 void br_switchdev_fdb_notify(const struct net_bridge_fdb_entry *fdb,
1405 int br_switchdev_port_vlan_add(struct net_device *dev, u16 vid, u16 flags,
1406 struct netlink_ext_ack *extack);
1407 int br_switchdev_port_vlan_del(struct net_device *dev, u16 vid);
1409 static inline void br_switchdev_frame_unmark(struct sk_buff *skb)
1411 skb->offload_fwd_mark = 0;
1414 static inline int nbp_switchdev_mark_set(struct net_bridge_port *p)
1419 static inline void nbp_switchdev_frame_mark(const struct net_bridge_port *p,
1420 struct sk_buff *skb)
1424 static inline bool nbp_switchdev_allowed_egress(const struct net_bridge_port *p,
1425 const struct sk_buff *skb)
1430 static inline int br_switchdev_set_port_flag(struct net_bridge_port *p,
1431 unsigned long flags,
1437 static inline int br_switchdev_port_vlan_add(struct net_device *dev,
1439 struct netlink_ext_ack *extack)
1444 static inline int br_switchdev_port_vlan_del(struct net_device *dev, u16 vid)
1450 br_switchdev_fdb_notify(const struct net_bridge_fdb_entry *fdb, int type)
1454 static inline void br_switchdev_frame_unmark(struct sk_buff *skb)
1457 #endif /* CONFIG_NET_SWITCHDEV */
1459 /* br_arp_nd_proxy.c */
1460 void br_recalculate_neigh_suppress_enabled(struct net_bridge *br);
1461 void br_do_proxy_suppress_arp(struct sk_buff *skb, struct net_bridge *br,
1462 u16 vid, struct net_bridge_port *p);
1463 void br_do_suppress_nd(struct sk_buff *skb, struct net_bridge *br,
1464 u16 vid, struct net_bridge_port *p, struct nd_msg *msg);
1465 struct nd_msg *br_is_nd_neigh_msg(struct sk_buff *skb, struct nd_msg *m);