1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Linux ethernet bridge
7 * Lennert Buytenhek <buytenh@gnu.org>
10 #include <linux/kernel.h>
11 #include <linux/netdevice.h>
12 #include <linux/netpoll.h>
13 #include <linux/etherdevice.h>
14 #include <linux/ethtool.h>
15 #include <linux/list.h>
16 #include <linux/netfilter_bridge.h>
18 #include <linux/uaccess.h>
19 #include "br_private.h"
21 #define COMMON_FEATURES (NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HIGHDMA | \
22 NETIF_F_GSO_MASK | NETIF_F_HW_CSUM)
24 const struct nf_br_ops __rcu
*nf_br_ops __read_mostly
;
25 EXPORT_SYMBOL_GPL(nf_br_ops
);
27 /* net device transmit always called with BH disabled */
28 netdev_tx_t
br_dev_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
30 enum skb_drop_reason reason
= pskb_may_pull_reason(skb
, ETH_HLEN
);
31 struct net_bridge_mcast_port
*pmctx_null
= NULL
;
32 struct net_bridge
*br
= netdev_priv(dev
);
33 struct net_bridge_mcast
*brmctx
= &br
->multicast_ctx
;
34 struct net_bridge_fdb_entry
*dst
;
35 struct net_bridge_mdb_entry
*mdst
;
36 const struct nf_br_ops
*nf_ops
;
37 u8 state
= BR_STATE_FORWARDING
;
38 struct net_bridge_vlan
*vlan
;
39 const unsigned char *dest
;
42 if (unlikely(reason
!= SKB_NOT_DROPPED_YET
)) {
43 kfree_skb_reason(skb
, reason
);
47 memset(skb
->cb
, 0, sizeof(struct br_input_skb_cb
));
48 br_tc_skb_miss_set(skb
, false);
51 nf_ops
= rcu_dereference(nf_br_ops
);
52 if (nf_ops
&& nf_ops
->br_dev_xmit_hook(skb
)) {
57 dev_sw_netstats_tx_add(dev
, 1, skb
->len
);
59 br_switchdev_frame_unmark(skb
);
60 BR_INPUT_SKB_CB(skb
)->brdev
= dev
;
61 BR_INPUT_SKB_CB(skb
)->frag_max_size
= 0;
63 skb_reset_mac_header(skb
);
64 skb_pull(skb
, ETH_HLEN
);
66 if (!br_allowed_ingress(br
, br_vlan_group_rcu(br
), skb
, &vid
,
70 if (IS_ENABLED(CONFIG_INET
) &&
71 (eth_hdr(skb
)->h_proto
== htons(ETH_P_ARP
) ||
72 eth_hdr(skb
)->h_proto
== htons(ETH_P_RARP
)) &&
73 br_opt_get(br
, BROPT_NEIGH_SUPPRESS_ENABLED
)) {
74 br_do_proxy_suppress_arp(skb
, br
, vid
, NULL
);
75 } else if (IS_ENABLED(CONFIG_IPV6
) &&
76 skb
->protocol
== htons(ETH_P_IPV6
) &&
77 br_opt_get(br
, BROPT_NEIGH_SUPPRESS_ENABLED
) &&
78 pskb_may_pull(skb
, sizeof(struct ipv6hdr
) +
79 sizeof(struct nd_msg
)) &&
80 ipv6_hdr(skb
)->nexthdr
== IPPROTO_ICMPV6
) {
81 struct nd_msg
*msg
, _msg
;
83 msg
= br_is_nd_neigh_msg(skb
, &_msg
);
85 br_do_suppress_nd(skb
, br
, vid
, NULL
, msg
);
88 dest
= eth_hdr(skb
)->h_dest
;
89 if (is_broadcast_ether_addr(dest
)) {
90 br_flood(br
, skb
, BR_PKT_BROADCAST
, false, true, vid
);
91 } else if (is_multicast_ether_addr(dest
)) {
92 if (unlikely(netpoll_tx_running(dev
))) {
93 br_flood(br
, skb
, BR_PKT_MULTICAST
, false, true, vid
);
96 if (br_multicast_rcv(&brmctx
, &pmctx_null
, vlan
, skb
, vid
)) {
101 mdst
= br_mdb_entry_skb_get(brmctx
, skb
, vid
);
102 if ((mdst
|| BR_INPUT_SKB_CB_MROUTERS_ONLY(skb
)) &&
103 br_multicast_querier_exists(brmctx
, eth_hdr(skb
), mdst
))
104 br_multicast_flood(mdst
, skb
, brmctx
, false, true);
106 br_flood(br
, skb
, BR_PKT_MULTICAST
, false, true, vid
);
107 } else if ((dst
= br_fdb_find_rcu(br
, dest
, vid
)) != NULL
) {
108 br_forward(dst
->dst
, skb
, false, true);
110 br_flood(br
, skb
, BR_PKT_UNICAST
, false, true, vid
);
117 static int br_dev_init(struct net_device
*dev
)
119 struct net_bridge
*br
= netdev_priv(dev
);
122 err
= br_fdb_hash_init(br
);
126 err
= br_mdb_hash_init(br
);
128 br_fdb_hash_fini(br
);
132 err
= br_vlan_init(br
);
134 br_mdb_hash_fini(br
);
135 br_fdb_hash_fini(br
);
139 err
= br_multicast_init_stats(br
);
142 br_mdb_hash_fini(br
);
143 br_fdb_hash_fini(br
);
147 netdev_lockdep_set_classes(dev
);
151 static void br_dev_uninit(struct net_device
*dev
)
153 struct net_bridge
*br
= netdev_priv(dev
);
155 br_multicast_dev_del(br
);
156 br_multicast_uninit_stats(br
);
158 br_mdb_hash_fini(br
);
159 br_fdb_hash_fini(br
);
162 static int br_dev_open(struct net_device
*dev
)
164 struct net_bridge
*br
= netdev_priv(dev
);
166 netdev_update_features(dev
);
167 netif_start_queue(dev
);
168 br_stp_enable_bridge(br
);
169 br_multicast_open(br
);
171 if (br_opt_get(br
, BROPT_MULTICAST_ENABLED
))
172 br_multicast_join_snoopers(br
);
177 static void br_dev_set_multicast_list(struct net_device
*dev
)
181 static void br_dev_change_rx_flags(struct net_device
*dev
, int change
)
183 if (change
& IFF_PROMISC
)
184 br_manage_promisc(netdev_priv(dev
));
187 static int br_dev_stop(struct net_device
*dev
)
189 struct net_bridge
*br
= netdev_priv(dev
);
191 br_stp_disable_bridge(br
);
192 br_multicast_stop(br
);
194 if (br_opt_get(br
, BROPT_MULTICAST_ENABLED
))
195 br_multicast_leave_snoopers(br
);
197 netif_stop_queue(dev
);
202 static int br_change_mtu(struct net_device
*dev
, int new_mtu
)
204 struct net_bridge
*br
= netdev_priv(dev
);
206 WRITE_ONCE(dev
->mtu
, new_mtu
);
208 /* this flag will be cleared if the MTU was automatically adjusted */
209 br_opt_toggle(br
, BROPT_MTU_SET_BY_USER
, true);
210 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
211 /* remember the MTU in the rtable for PMTU */
212 dst_metric_set(&br
->fake_rtable
.dst
, RTAX_MTU
, new_mtu
);
218 /* Allow setting mac address to any valid ethernet address. */
219 static int br_set_mac_address(struct net_device
*dev
, void *p
)
221 struct net_bridge
*br
= netdev_priv(dev
);
222 struct sockaddr
*addr
= p
;
224 if (!is_valid_ether_addr(addr
->sa_data
))
225 return -EADDRNOTAVAIL
;
227 /* dev_set_mac_addr() can be called by a master device on bridge's
228 * NETDEV_UNREGISTER, but since it's being destroyed do nothing
230 if (dev
->reg_state
!= NETREG_REGISTERED
)
233 spin_lock_bh(&br
->lock
);
234 if (!ether_addr_equal(dev
->dev_addr
, addr
->sa_data
)) {
235 /* Mac address will be changed in br_stp_change_bridge_id(). */
236 br_stp_change_bridge_id(br
, addr
->sa_data
);
238 spin_unlock_bh(&br
->lock
);
243 static void br_getinfo(struct net_device
*dev
, struct ethtool_drvinfo
*info
)
245 strscpy(info
->driver
, "bridge", sizeof(info
->driver
));
246 strscpy(info
->version
, BR_VERSION
, sizeof(info
->version
));
247 strscpy(info
->fw_version
, "N/A", sizeof(info
->fw_version
));
248 strscpy(info
->bus_info
, "N/A", sizeof(info
->bus_info
));
251 static int br_get_link_ksettings(struct net_device
*dev
,
252 struct ethtool_link_ksettings
*cmd
)
254 struct net_bridge
*br
= netdev_priv(dev
);
255 struct net_bridge_port
*p
;
257 cmd
->base
.duplex
= DUPLEX_UNKNOWN
;
258 cmd
->base
.port
= PORT_OTHER
;
259 cmd
->base
.speed
= SPEED_UNKNOWN
;
261 list_for_each_entry(p
, &br
->port_list
, list
) {
262 struct ethtool_link_ksettings ecmd
;
263 struct net_device
*pdev
= p
->dev
;
265 if (!netif_running(pdev
) || !netif_oper_up(pdev
))
268 if (__ethtool_get_link_ksettings(pdev
, &ecmd
))
271 if (ecmd
.base
.speed
== (__u32
)SPEED_UNKNOWN
)
274 if (cmd
->base
.speed
== (__u32
)SPEED_UNKNOWN
||
275 cmd
->base
.speed
< ecmd
.base
.speed
)
276 cmd
->base
.speed
= ecmd
.base
.speed
;
282 static netdev_features_t
br_fix_features(struct net_device
*dev
,
283 netdev_features_t features
)
285 struct net_bridge
*br
= netdev_priv(dev
);
287 return br_features_recompute(br
, features
);
290 #ifdef CONFIG_NET_POLL_CONTROLLER
291 static void br_poll_controller(struct net_device
*br_dev
)
295 static void br_netpoll_cleanup(struct net_device
*dev
)
297 struct net_bridge
*br
= netdev_priv(dev
);
298 struct net_bridge_port
*p
;
300 list_for_each_entry(p
, &br
->port_list
, list
)
301 br_netpoll_disable(p
);
304 static int __br_netpoll_enable(struct net_bridge_port
*p
)
309 np
= kzalloc(sizeof(*p
->np
), GFP_KERNEL
);
313 err
= __netpoll_setup(np
, p
->dev
);
323 int br_netpoll_enable(struct net_bridge_port
*p
)
325 if (!p
->br
->dev
->npinfo
)
328 return __br_netpoll_enable(p
);
331 static int br_netpoll_setup(struct net_device
*dev
)
333 struct net_bridge
*br
= netdev_priv(dev
);
334 struct net_bridge_port
*p
;
337 list_for_each_entry(p
, &br
->port_list
, list
) {
340 err
= __br_netpoll_enable(p
);
349 br_netpoll_cleanup(dev
);
353 void br_netpoll_disable(struct net_bridge_port
*p
)
355 struct netpoll
*np
= p
->np
;
367 static int br_add_slave(struct net_device
*dev
, struct net_device
*slave_dev
,
368 struct netlink_ext_ack
*extack
)
371 struct net_bridge
*br
= netdev_priv(dev
);
373 return br_add_if(br
, slave_dev
, extack
);
376 static int br_del_slave(struct net_device
*dev
, struct net_device
*slave_dev
)
378 struct net_bridge
*br
= netdev_priv(dev
);
380 return br_del_if(br
, slave_dev
);
383 static int br_fill_forward_path(struct net_device_path_ctx
*ctx
,
384 struct net_device_path
*path
)
386 struct net_bridge_fdb_entry
*f
;
387 struct net_bridge_port
*dst
;
388 struct net_bridge
*br
;
390 if (netif_is_bridge_port(ctx
->dev
))
393 br
= netdev_priv(ctx
->dev
);
395 br_vlan_fill_forward_path_pvid(br
, ctx
, path
);
397 f
= br_fdb_find_rcu(br
, ctx
->daddr
, path
->bridge
.vlan_id
);
401 dst
= READ_ONCE(f
->dst
);
405 if (br_vlan_fill_forward_path_mode(br
, dst
, path
))
408 path
->type
= DEV_PATH_BRIDGE
;
409 path
->dev
= dst
->br
->dev
;
412 switch (path
->bridge
.vlan_mode
) {
413 case DEV_PATH_BR_VLAN_TAG
:
414 if (ctx
->num_vlans
>= ARRAY_SIZE(ctx
->vlan
))
416 ctx
->vlan
[ctx
->num_vlans
].id
= path
->bridge
.vlan_id
;
417 ctx
->vlan
[ctx
->num_vlans
].proto
= path
->bridge
.vlan_proto
;
420 case DEV_PATH_BR_VLAN_UNTAG_HW
:
421 case DEV_PATH_BR_VLAN_UNTAG
:
424 case DEV_PATH_BR_VLAN_KEEP
:
431 static const struct ethtool_ops br_ethtool_ops
= {
432 .get_drvinfo
= br_getinfo
,
433 .get_link
= ethtool_op_get_link
,
434 .get_link_ksettings
= br_get_link_ksettings
,
437 static const struct net_device_ops br_netdev_ops
= {
438 .ndo_open
= br_dev_open
,
439 .ndo_stop
= br_dev_stop
,
440 .ndo_init
= br_dev_init
,
441 .ndo_uninit
= br_dev_uninit
,
442 .ndo_start_xmit
= br_dev_xmit
,
443 .ndo_get_stats64
= dev_get_tstats64
,
444 .ndo_set_mac_address
= br_set_mac_address
,
445 .ndo_set_rx_mode
= br_dev_set_multicast_list
,
446 .ndo_change_rx_flags
= br_dev_change_rx_flags
,
447 .ndo_change_mtu
= br_change_mtu
,
448 .ndo_siocdevprivate
= br_dev_siocdevprivate
,
449 #ifdef CONFIG_NET_POLL_CONTROLLER
450 .ndo_netpoll_setup
= br_netpoll_setup
,
451 .ndo_netpoll_cleanup
= br_netpoll_cleanup
,
452 .ndo_poll_controller
= br_poll_controller
,
454 .ndo_add_slave
= br_add_slave
,
455 .ndo_del_slave
= br_del_slave
,
456 .ndo_fix_features
= br_fix_features
,
457 .ndo_fdb_add
= br_fdb_add
,
458 .ndo_fdb_del
= br_fdb_delete
,
459 .ndo_fdb_del_bulk
= br_fdb_delete_bulk
,
460 .ndo_fdb_dump
= br_fdb_dump
,
461 .ndo_fdb_get
= br_fdb_get
,
462 .ndo_mdb_add
= br_mdb_add
,
463 .ndo_mdb_del
= br_mdb_del
,
464 .ndo_mdb_del_bulk
= br_mdb_del_bulk
,
465 .ndo_mdb_dump
= br_mdb_dump
,
466 .ndo_mdb_get
= br_mdb_get
,
467 .ndo_bridge_getlink
= br_getlink
,
468 .ndo_bridge_setlink
= br_setlink
,
469 .ndo_bridge_dellink
= br_dellink
,
470 .ndo_features_check
= passthru_features_check
,
471 .ndo_fill_forward_path
= br_fill_forward_path
,
474 static const struct device_type br_type
= {
478 void br_dev_setup(struct net_device
*dev
)
480 struct net_bridge
*br
= netdev_priv(dev
);
482 eth_hw_addr_random(dev
);
485 dev
->netdev_ops
= &br_netdev_ops
;
486 dev
->needs_free_netdev
= true;
487 dev
->ethtool_ops
= &br_ethtool_ops
;
488 SET_NETDEV_DEVTYPE(dev
, &br_type
);
489 dev
->priv_flags
= IFF_EBRIDGE
| IFF_NO_QUEUE
;
491 dev
->netns_local
= true;
493 dev
->features
= COMMON_FEATURES
| NETIF_F_HW_VLAN_CTAG_TX
|
494 NETIF_F_HW_VLAN_STAG_TX
;
495 dev
->hw_features
= COMMON_FEATURES
| NETIF_F_HW_VLAN_CTAG_TX
|
496 NETIF_F_HW_VLAN_STAG_TX
;
497 dev
->vlan_features
= COMMON_FEATURES
;
498 dev
->pcpu_stat_type
= NETDEV_PCPU_STAT_TSTATS
;
501 spin_lock_init(&br
->lock
);
502 INIT_LIST_HEAD(&br
->port_list
);
503 INIT_HLIST_HEAD(&br
->fdb_list
);
504 INIT_HLIST_HEAD(&br
->frame_type_list
);
505 #if IS_ENABLED(CONFIG_BRIDGE_MRP)
506 INIT_HLIST_HEAD(&br
->mrp_list
);
508 #if IS_ENABLED(CONFIG_BRIDGE_CFM)
509 INIT_HLIST_HEAD(&br
->mep_list
);
511 spin_lock_init(&br
->hash_lock
);
513 br
->bridge_id
.prio
[0] = 0x80;
514 br
->bridge_id
.prio
[1] = 0x00;
516 ether_addr_copy(br
->group_addr
, eth_stp_addr
);
518 br
->stp_enabled
= BR_NO_STP
;
519 br
->group_fwd_mask
= BR_GROUPFWD_DEFAULT
;
520 br
->group_fwd_mask_required
= BR_GROUPFWD_DEFAULT
;
522 br
->designated_root
= br
->bridge_id
;
523 br
->bridge_max_age
= br
->max_age
= 20 * HZ
;
524 br
->bridge_hello_time
= br
->hello_time
= 2 * HZ
;
525 br
->bridge_forward_delay
= br
->forward_delay
= 15 * HZ
;
526 br
->bridge_ageing_time
= br
->ageing_time
= BR_DEFAULT_AGEING_TIME
;
527 dev
->max_mtu
= ETH_MAX_MTU
;
529 br_netfilter_rtable_init(br
);
530 br_stp_timer_init(br
);
531 br_multicast_init(br
);
532 INIT_DELAYED_WORK(&br
->gc_work
, br_fdb_cleanup
);