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 static struct lock_class_key bridge_netdev_addr_lock_key
;
29 /* net device transmit always called with BH disabled */
30 netdev_tx_t
br_dev_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
32 struct net_bridge
*br
= netdev_priv(dev
);
33 struct net_bridge_fdb_entry
*dst
;
34 struct net_bridge_mdb_entry
*mdst
;
35 struct pcpu_sw_netstats
*brstats
= this_cpu_ptr(br
->stats
);
36 const struct nf_br_ops
*nf_ops
;
37 const unsigned char *dest
;
42 nf_ops
= rcu_dereference(nf_br_ops
);
43 if (nf_ops
&& nf_ops
->br_dev_xmit_hook(skb
)) {
48 u64_stats_update_begin(&brstats
->syncp
);
49 brstats
->tx_packets
++;
50 brstats
->tx_bytes
+= skb
->len
;
51 u64_stats_update_end(&brstats
->syncp
);
53 br_switchdev_frame_unmark(skb
);
54 BR_INPUT_SKB_CB(skb
)->brdev
= dev
;
56 skb_reset_mac_header(skb
);
58 skb_pull(skb
, ETH_HLEN
);
60 if (!br_allowed_ingress(br
, br_vlan_group_rcu(br
), skb
, &vid
))
63 if (IS_ENABLED(CONFIG_INET
) &&
64 (eth
->h_proto
== htons(ETH_P_ARP
) ||
65 eth
->h_proto
== htons(ETH_P_RARP
)) &&
66 br_opt_get(br
, BROPT_NEIGH_SUPPRESS_ENABLED
)) {
67 br_do_proxy_suppress_arp(skb
, br
, vid
, NULL
);
68 } else if (IS_ENABLED(CONFIG_IPV6
) &&
69 skb
->protocol
== htons(ETH_P_IPV6
) &&
70 br_opt_get(br
, BROPT_NEIGH_SUPPRESS_ENABLED
) &&
71 pskb_may_pull(skb
, sizeof(struct ipv6hdr
) +
72 sizeof(struct nd_msg
)) &&
73 ipv6_hdr(skb
)->nexthdr
== IPPROTO_ICMPV6
) {
74 struct nd_msg
*msg
, _msg
;
76 msg
= br_is_nd_neigh_msg(skb
, &_msg
);
78 br_do_suppress_nd(skb
, br
, vid
, NULL
, msg
);
81 dest
= eth_hdr(skb
)->h_dest
;
82 if (is_broadcast_ether_addr(dest
)) {
83 br_flood(br
, skb
, BR_PKT_BROADCAST
, false, true);
84 } else if (is_multicast_ether_addr(dest
)) {
85 if (unlikely(netpoll_tx_running(dev
))) {
86 br_flood(br
, skb
, BR_PKT_MULTICAST
, false, true);
89 if (br_multicast_rcv(br
, NULL
, skb
, vid
)) {
94 mdst
= br_mdb_get(br
, skb
, vid
);
95 if ((mdst
|| BR_INPUT_SKB_CB_MROUTERS_ONLY(skb
)) &&
96 br_multicast_querier_exists(br
, eth_hdr(skb
)))
97 br_multicast_flood(mdst
, skb
, false, true);
99 br_flood(br
, skb
, BR_PKT_MULTICAST
, false, true);
100 } else if ((dst
= br_fdb_find_rcu(br
, dest
, vid
)) != NULL
) {
101 br_forward(dst
->dst
, skb
, false, true);
103 br_flood(br
, skb
, BR_PKT_UNICAST
, false, true);
110 static void br_set_lockdep_class(struct net_device
*dev
)
112 lockdep_set_class(&dev
->addr_list_lock
, &bridge_netdev_addr_lock_key
);
115 static int br_dev_init(struct net_device
*dev
)
117 struct net_bridge
*br
= netdev_priv(dev
);
120 br
->stats
= netdev_alloc_pcpu_stats(struct pcpu_sw_netstats
);
124 err
= br_fdb_hash_init(br
);
126 free_percpu(br
->stats
);
130 err
= br_mdb_hash_init(br
);
132 free_percpu(br
->stats
);
133 br_fdb_hash_fini(br
);
137 err
= br_vlan_init(br
);
139 free_percpu(br
->stats
);
140 br_mdb_hash_fini(br
);
141 br_fdb_hash_fini(br
);
145 err
= br_multicast_init_stats(br
);
147 free_percpu(br
->stats
);
149 br_mdb_hash_fini(br
);
150 br_fdb_hash_fini(br
);
152 br_set_lockdep_class(dev
);
157 static void br_dev_uninit(struct net_device
*dev
)
159 struct net_bridge
*br
= netdev_priv(dev
);
161 br_multicast_dev_del(br
);
162 br_multicast_uninit_stats(br
);
164 br_mdb_hash_fini(br
);
165 br_fdb_hash_fini(br
);
166 free_percpu(br
->stats
);
169 static int br_dev_open(struct net_device
*dev
)
171 struct net_bridge
*br
= netdev_priv(dev
);
173 netdev_update_features(dev
);
174 netif_start_queue(dev
);
175 br_stp_enable_bridge(br
);
176 br_multicast_open(br
);
181 static void br_dev_set_multicast_list(struct net_device
*dev
)
185 static void br_dev_change_rx_flags(struct net_device
*dev
, int change
)
187 if (change
& IFF_PROMISC
)
188 br_manage_promisc(netdev_priv(dev
));
191 static int br_dev_stop(struct net_device
*dev
)
193 struct net_bridge
*br
= netdev_priv(dev
);
195 br_stp_disable_bridge(br
);
196 br_multicast_stop(br
);
198 netif_stop_queue(dev
);
203 static void br_get_stats64(struct net_device
*dev
,
204 struct rtnl_link_stats64
*stats
)
206 struct net_bridge
*br
= netdev_priv(dev
);
207 struct pcpu_sw_netstats tmp
, sum
= { 0 };
210 for_each_possible_cpu(cpu
) {
212 const struct pcpu_sw_netstats
*bstats
213 = per_cpu_ptr(br
->stats
, cpu
);
215 start
= u64_stats_fetch_begin_irq(&bstats
->syncp
);
216 memcpy(&tmp
, bstats
, sizeof(tmp
));
217 } while (u64_stats_fetch_retry_irq(&bstats
->syncp
, start
));
218 sum
.tx_bytes
+= tmp
.tx_bytes
;
219 sum
.tx_packets
+= tmp
.tx_packets
;
220 sum
.rx_bytes
+= tmp
.rx_bytes
;
221 sum
.rx_packets
+= tmp
.rx_packets
;
224 stats
->tx_bytes
= sum
.tx_bytes
;
225 stats
->tx_packets
= sum
.tx_packets
;
226 stats
->rx_bytes
= sum
.rx_bytes
;
227 stats
->rx_packets
= sum
.rx_packets
;
230 static int br_change_mtu(struct net_device
*dev
, int new_mtu
)
232 struct net_bridge
*br
= netdev_priv(dev
);
236 /* this flag will be cleared if the MTU was automatically adjusted */
237 br_opt_toggle(br
, BROPT_MTU_SET_BY_USER
, true);
238 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
239 /* remember the MTU in the rtable for PMTU */
240 dst_metric_set(&br
->fake_rtable
.dst
, RTAX_MTU
, new_mtu
);
246 /* Allow setting mac address to any valid ethernet address. */
247 static int br_set_mac_address(struct net_device
*dev
, void *p
)
249 struct net_bridge
*br
= netdev_priv(dev
);
250 struct sockaddr
*addr
= p
;
252 if (!is_valid_ether_addr(addr
->sa_data
))
253 return -EADDRNOTAVAIL
;
255 spin_lock_bh(&br
->lock
);
256 if (!ether_addr_equal(dev
->dev_addr
, addr
->sa_data
)) {
257 /* Mac address will be changed in br_stp_change_bridge_id(). */
258 br_stp_change_bridge_id(br
, addr
->sa_data
);
260 spin_unlock_bh(&br
->lock
);
265 static void br_getinfo(struct net_device
*dev
, struct ethtool_drvinfo
*info
)
267 strlcpy(info
->driver
, "bridge", sizeof(info
->driver
));
268 strlcpy(info
->version
, BR_VERSION
, sizeof(info
->version
));
269 strlcpy(info
->fw_version
, "N/A", sizeof(info
->fw_version
));
270 strlcpy(info
->bus_info
, "N/A", sizeof(info
->bus_info
));
273 static netdev_features_t
br_fix_features(struct net_device
*dev
,
274 netdev_features_t features
)
276 struct net_bridge
*br
= netdev_priv(dev
);
278 return br_features_recompute(br
, features
);
281 #ifdef CONFIG_NET_POLL_CONTROLLER
282 static void br_poll_controller(struct net_device
*br_dev
)
286 static void br_netpoll_cleanup(struct net_device
*dev
)
288 struct net_bridge
*br
= netdev_priv(dev
);
289 struct net_bridge_port
*p
;
291 list_for_each_entry(p
, &br
->port_list
, list
)
292 br_netpoll_disable(p
);
295 static int __br_netpoll_enable(struct net_bridge_port
*p
)
300 np
= kzalloc(sizeof(*p
->np
), GFP_KERNEL
);
304 err
= __netpoll_setup(np
, p
->dev
);
314 int br_netpoll_enable(struct net_bridge_port
*p
)
316 if (!p
->br
->dev
->npinfo
)
319 return __br_netpoll_enable(p
);
322 static int br_netpoll_setup(struct net_device
*dev
, struct netpoll_info
*ni
)
324 struct net_bridge
*br
= netdev_priv(dev
);
325 struct net_bridge_port
*p
;
328 list_for_each_entry(p
, &br
->port_list
, list
) {
331 err
= __br_netpoll_enable(p
);
340 br_netpoll_cleanup(dev
);
344 void br_netpoll_disable(struct net_bridge_port
*p
)
346 struct netpoll
*np
= p
->np
;
358 static int br_add_slave(struct net_device
*dev
, struct net_device
*slave_dev
,
359 struct netlink_ext_ack
*extack
)
362 struct net_bridge
*br
= netdev_priv(dev
);
364 return br_add_if(br
, slave_dev
, extack
);
367 static int br_del_slave(struct net_device
*dev
, struct net_device
*slave_dev
)
369 struct net_bridge
*br
= netdev_priv(dev
);
371 return br_del_if(br
, slave_dev
);
374 static const struct ethtool_ops br_ethtool_ops
= {
375 .get_drvinfo
= br_getinfo
,
376 .get_link
= ethtool_op_get_link
,
379 static const struct net_device_ops br_netdev_ops
= {
380 .ndo_open
= br_dev_open
,
381 .ndo_stop
= br_dev_stop
,
382 .ndo_init
= br_dev_init
,
383 .ndo_uninit
= br_dev_uninit
,
384 .ndo_start_xmit
= br_dev_xmit
,
385 .ndo_get_stats64
= br_get_stats64
,
386 .ndo_set_mac_address
= br_set_mac_address
,
387 .ndo_set_rx_mode
= br_dev_set_multicast_list
,
388 .ndo_change_rx_flags
= br_dev_change_rx_flags
,
389 .ndo_change_mtu
= br_change_mtu
,
390 .ndo_do_ioctl
= br_dev_ioctl
,
391 #ifdef CONFIG_NET_POLL_CONTROLLER
392 .ndo_netpoll_setup
= br_netpoll_setup
,
393 .ndo_netpoll_cleanup
= br_netpoll_cleanup
,
394 .ndo_poll_controller
= br_poll_controller
,
396 .ndo_add_slave
= br_add_slave
,
397 .ndo_del_slave
= br_del_slave
,
398 .ndo_fix_features
= br_fix_features
,
399 .ndo_fdb_add
= br_fdb_add
,
400 .ndo_fdb_del
= br_fdb_delete
,
401 .ndo_fdb_dump
= br_fdb_dump
,
402 .ndo_fdb_get
= br_fdb_get
,
403 .ndo_bridge_getlink
= br_getlink
,
404 .ndo_bridge_setlink
= br_setlink
,
405 .ndo_bridge_dellink
= br_dellink
,
406 .ndo_features_check
= passthru_features_check
,
409 static struct device_type br_type
= {
413 void br_dev_setup(struct net_device
*dev
)
415 struct net_bridge
*br
= netdev_priv(dev
);
417 eth_hw_addr_random(dev
);
420 dev
->netdev_ops
= &br_netdev_ops
;
421 dev
->needs_free_netdev
= true;
422 dev
->ethtool_ops
= &br_ethtool_ops
;
423 SET_NETDEV_DEVTYPE(dev
, &br_type
);
424 dev
->priv_flags
= IFF_EBRIDGE
| IFF_NO_QUEUE
;
426 dev
->features
= COMMON_FEATURES
| NETIF_F_LLTX
| NETIF_F_NETNS_LOCAL
|
427 NETIF_F_HW_VLAN_CTAG_TX
| NETIF_F_HW_VLAN_STAG_TX
;
428 dev
->hw_features
= COMMON_FEATURES
| NETIF_F_HW_VLAN_CTAG_TX
|
429 NETIF_F_HW_VLAN_STAG_TX
;
430 dev
->vlan_features
= COMMON_FEATURES
;
433 spin_lock_init(&br
->lock
);
434 INIT_LIST_HEAD(&br
->port_list
);
435 INIT_HLIST_HEAD(&br
->fdb_list
);
436 spin_lock_init(&br
->hash_lock
);
438 br
->bridge_id
.prio
[0] = 0x80;
439 br
->bridge_id
.prio
[1] = 0x00;
441 ether_addr_copy(br
->group_addr
, eth_stp_addr
);
443 br
->stp_enabled
= BR_NO_STP
;
444 br
->group_fwd_mask
= BR_GROUPFWD_DEFAULT
;
445 br
->group_fwd_mask_required
= BR_GROUPFWD_DEFAULT
;
447 br
->designated_root
= br
->bridge_id
;
448 br
->bridge_max_age
= br
->max_age
= 20 * HZ
;
449 br
->bridge_hello_time
= br
->hello_time
= 2 * HZ
;
450 br
->bridge_forward_delay
= br
->forward_delay
= 15 * HZ
;
451 br
->bridge_ageing_time
= br
->ageing_time
= BR_DEFAULT_AGEING_TIME
;
452 dev
->max_mtu
= ETH_MAX_MTU
;
454 br_netfilter_rtable_init(br
);
455 br_stp_timer_init(br
);
456 br_multicast_init(br
);
457 INIT_DELAYED_WORK(&br
->gc_work
, br_fdb_cleanup
);