3 * Linux ethernet bridge
6 * Lennert Buytenhek <buytenh@gnu.org>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
14 #include <linux/kernel.h>
15 #include <linux/netdevice.h>
16 #include <linux/netpoll.h>
17 #include <linux/etherdevice.h>
18 #include <linux/ethtool.h>
19 #include <linux/list.h>
20 #include <linux/netfilter_bridge.h>
22 #include <linux/uaccess.h>
23 #include "br_private.h"
25 #define COMMON_FEATURES (NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HIGHDMA | \
26 NETIF_F_GSO_MASK | NETIF_F_HW_CSUM)
28 const struct nf_br_ops __rcu
*nf_br_ops __read_mostly
;
29 EXPORT_SYMBOL_GPL(nf_br_ops
);
31 static struct lock_class_key bridge_netdev_addr_lock_key
;
33 /* net device transmit always called with BH disabled */
34 netdev_tx_t
br_dev_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
36 struct net_bridge
*br
= netdev_priv(dev
);
37 struct net_bridge_fdb_entry
*dst
;
38 struct net_bridge_mdb_entry
*mdst
;
39 struct pcpu_sw_netstats
*brstats
= this_cpu_ptr(br
->stats
);
40 const struct nf_br_ops
*nf_ops
;
41 const unsigned char *dest
;
45 nf_ops
= rcu_dereference(nf_br_ops
);
46 if (nf_ops
&& nf_ops
->br_dev_xmit_hook(skb
)) {
51 u64_stats_update_begin(&brstats
->syncp
);
52 brstats
->tx_packets
++;
53 brstats
->tx_bytes
+= skb
->len
;
54 u64_stats_update_end(&brstats
->syncp
);
56 br_switchdev_frame_unmark(skb
);
57 BR_INPUT_SKB_CB(skb
)->brdev
= dev
;
59 skb_reset_mac_header(skb
);
60 skb_pull(skb
, ETH_HLEN
);
62 if (!br_allowed_ingress(br
, br_vlan_group_rcu(br
), skb
, &vid
))
65 dest
= eth_hdr(skb
)->h_dest
;
66 if (is_broadcast_ether_addr(dest
)) {
67 br_flood(br
, skb
, BR_PKT_BROADCAST
, false, true);
68 } else if (is_multicast_ether_addr(dest
)) {
69 if (unlikely(netpoll_tx_running(dev
))) {
70 br_flood(br
, skb
, BR_PKT_MULTICAST
, false, true);
73 if (br_multicast_rcv(br
, NULL
, skb
, vid
)) {
78 mdst
= br_mdb_get(br
, skb
, vid
);
79 if ((mdst
|| BR_INPUT_SKB_CB_MROUTERS_ONLY(skb
)) &&
80 br_multicast_querier_exists(br
, eth_hdr(skb
)))
81 br_multicast_flood(mdst
, skb
, false, true);
83 br_flood(br
, skb
, BR_PKT_MULTICAST
, false, true);
84 } else if ((dst
= br_fdb_find_rcu(br
, dest
, vid
)) != NULL
) {
85 br_forward(dst
->dst
, skb
, false, true);
87 br_flood(br
, skb
, BR_PKT_UNICAST
, false, true);
94 static void br_set_lockdep_class(struct net_device
*dev
)
96 lockdep_set_class(&dev
->addr_list_lock
, &bridge_netdev_addr_lock_key
);
99 static int br_dev_init(struct net_device
*dev
)
101 struct net_bridge
*br
= netdev_priv(dev
);
104 br
->stats
= netdev_alloc_pcpu_stats(struct pcpu_sw_netstats
);
108 err
= br_vlan_init(br
);
110 free_percpu(br
->stats
);
114 err
= br_multicast_init_stats(br
);
116 free_percpu(br
->stats
);
119 br_set_lockdep_class(dev
);
124 static void br_dev_uninit(struct net_device
*dev
)
126 struct net_bridge
*br
= netdev_priv(dev
);
128 br_multicast_dev_del(br
);
129 br_multicast_uninit_stats(br
);
131 free_percpu(br
->stats
);
134 static int br_dev_open(struct net_device
*dev
)
136 struct net_bridge
*br
= netdev_priv(dev
);
138 netdev_update_features(dev
);
139 netif_start_queue(dev
);
140 br_stp_enable_bridge(br
);
141 br_multicast_open(br
);
146 static void br_dev_set_multicast_list(struct net_device
*dev
)
150 static void br_dev_change_rx_flags(struct net_device
*dev
, int change
)
152 if (change
& IFF_PROMISC
)
153 br_manage_promisc(netdev_priv(dev
));
156 static int br_dev_stop(struct net_device
*dev
)
158 struct net_bridge
*br
= netdev_priv(dev
);
160 br_stp_disable_bridge(br
);
161 br_multicast_stop(br
);
163 netif_stop_queue(dev
);
168 static void br_get_stats64(struct net_device
*dev
,
169 struct rtnl_link_stats64
*stats
)
171 struct net_bridge
*br
= netdev_priv(dev
);
172 struct pcpu_sw_netstats tmp
, sum
= { 0 };
175 for_each_possible_cpu(cpu
) {
177 const struct pcpu_sw_netstats
*bstats
178 = per_cpu_ptr(br
->stats
, cpu
);
180 start
= u64_stats_fetch_begin_irq(&bstats
->syncp
);
181 memcpy(&tmp
, bstats
, sizeof(tmp
));
182 } while (u64_stats_fetch_retry_irq(&bstats
->syncp
, start
));
183 sum
.tx_bytes
+= tmp
.tx_bytes
;
184 sum
.tx_packets
+= tmp
.tx_packets
;
185 sum
.rx_bytes
+= tmp
.rx_bytes
;
186 sum
.rx_packets
+= tmp
.rx_packets
;
189 stats
->tx_bytes
= sum
.tx_bytes
;
190 stats
->tx_packets
= sum
.tx_packets
;
191 stats
->rx_bytes
= sum
.rx_bytes
;
192 stats
->rx_packets
= sum
.rx_packets
;
195 static int br_change_mtu(struct net_device
*dev
, int new_mtu
)
197 struct net_bridge
*br
= netdev_priv(dev
);
198 if (new_mtu
> br_min_mtu(br
))
203 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
204 /* remember the MTU in the rtable for PMTU */
205 dst_metric_set(&br
->fake_rtable
.dst
, RTAX_MTU
, new_mtu
);
211 /* Allow setting mac address to any valid ethernet address. */
212 static int br_set_mac_address(struct net_device
*dev
, void *p
)
214 struct net_bridge
*br
= netdev_priv(dev
);
215 struct sockaddr
*addr
= p
;
217 if (!is_valid_ether_addr(addr
->sa_data
))
218 return -EADDRNOTAVAIL
;
220 spin_lock_bh(&br
->lock
);
221 if (!ether_addr_equal(dev
->dev_addr
, addr
->sa_data
)) {
222 /* Mac address will be changed in br_stp_change_bridge_id(). */
223 br_stp_change_bridge_id(br
, addr
->sa_data
);
225 spin_unlock_bh(&br
->lock
);
230 static void br_getinfo(struct net_device
*dev
, struct ethtool_drvinfo
*info
)
232 strlcpy(info
->driver
, "bridge", sizeof(info
->driver
));
233 strlcpy(info
->version
, BR_VERSION
, sizeof(info
->version
));
234 strlcpy(info
->fw_version
, "N/A", sizeof(info
->fw_version
));
235 strlcpy(info
->bus_info
, "N/A", sizeof(info
->bus_info
));
238 static netdev_features_t
br_fix_features(struct net_device
*dev
,
239 netdev_features_t features
)
241 struct net_bridge
*br
= netdev_priv(dev
);
243 return br_features_recompute(br
, features
);
246 #ifdef CONFIG_NET_POLL_CONTROLLER
247 static void br_poll_controller(struct net_device
*br_dev
)
251 static void br_netpoll_cleanup(struct net_device
*dev
)
253 struct net_bridge
*br
= netdev_priv(dev
);
254 struct net_bridge_port
*p
;
256 list_for_each_entry(p
, &br
->port_list
, list
)
257 br_netpoll_disable(p
);
260 static int __br_netpoll_enable(struct net_bridge_port
*p
)
265 np
= kzalloc(sizeof(*p
->np
), GFP_KERNEL
);
269 err
= __netpoll_setup(np
, p
->dev
);
279 int br_netpoll_enable(struct net_bridge_port
*p
)
281 if (!p
->br
->dev
->npinfo
)
284 return __br_netpoll_enable(p
);
287 static int br_netpoll_setup(struct net_device
*dev
, struct netpoll_info
*ni
)
289 struct net_bridge
*br
= netdev_priv(dev
);
290 struct net_bridge_port
*p
;
293 list_for_each_entry(p
, &br
->port_list
, list
) {
296 err
= __br_netpoll_enable(p
);
305 br_netpoll_cleanup(dev
);
309 void br_netpoll_disable(struct net_bridge_port
*p
)
311 struct netpoll
*np
= p
->np
;
318 __netpoll_free_async(np
);
323 static int br_add_slave(struct net_device
*dev
, struct net_device
*slave_dev
)
326 struct net_bridge
*br
= netdev_priv(dev
);
328 return br_add_if(br
, slave_dev
);
331 static int br_del_slave(struct net_device
*dev
, struct net_device
*slave_dev
)
333 struct net_bridge
*br
= netdev_priv(dev
);
335 return br_del_if(br
, slave_dev
);
338 static const struct ethtool_ops br_ethtool_ops
= {
339 .get_drvinfo
= br_getinfo
,
340 .get_link
= ethtool_op_get_link
,
343 static const struct net_device_ops br_netdev_ops
= {
344 .ndo_open
= br_dev_open
,
345 .ndo_stop
= br_dev_stop
,
346 .ndo_init
= br_dev_init
,
347 .ndo_uninit
= br_dev_uninit
,
348 .ndo_start_xmit
= br_dev_xmit
,
349 .ndo_get_stats64
= br_get_stats64
,
350 .ndo_set_mac_address
= br_set_mac_address
,
351 .ndo_set_rx_mode
= br_dev_set_multicast_list
,
352 .ndo_change_rx_flags
= br_dev_change_rx_flags
,
353 .ndo_change_mtu
= br_change_mtu
,
354 .ndo_do_ioctl
= br_dev_ioctl
,
355 #ifdef CONFIG_NET_POLL_CONTROLLER
356 .ndo_netpoll_setup
= br_netpoll_setup
,
357 .ndo_netpoll_cleanup
= br_netpoll_cleanup
,
358 .ndo_poll_controller
= br_poll_controller
,
360 .ndo_add_slave
= br_add_slave
,
361 .ndo_del_slave
= br_del_slave
,
362 .ndo_fix_features
= br_fix_features
,
363 .ndo_fdb_add
= br_fdb_add
,
364 .ndo_fdb_del
= br_fdb_delete
,
365 .ndo_fdb_dump
= br_fdb_dump
,
366 .ndo_bridge_getlink
= br_getlink
,
367 .ndo_bridge_setlink
= br_setlink
,
368 .ndo_bridge_dellink
= br_dellink
,
369 .ndo_features_check
= passthru_features_check
,
372 static struct device_type br_type
= {
376 void br_dev_setup(struct net_device
*dev
)
378 struct net_bridge
*br
= netdev_priv(dev
);
380 eth_hw_addr_random(dev
);
383 dev
->netdev_ops
= &br_netdev_ops
;
384 dev
->needs_free_netdev
= true;
385 dev
->ethtool_ops
= &br_ethtool_ops
;
386 SET_NETDEV_DEVTYPE(dev
, &br_type
);
387 dev
->priv_flags
= IFF_EBRIDGE
| IFF_NO_QUEUE
;
389 dev
->features
= COMMON_FEATURES
| NETIF_F_LLTX
| NETIF_F_NETNS_LOCAL
|
390 NETIF_F_HW_VLAN_CTAG_TX
| NETIF_F_HW_VLAN_STAG_TX
;
391 dev
->hw_features
= COMMON_FEATURES
| NETIF_F_HW_VLAN_CTAG_TX
|
392 NETIF_F_HW_VLAN_STAG_TX
;
393 dev
->vlan_features
= COMMON_FEATURES
;
396 spin_lock_init(&br
->lock
);
397 INIT_LIST_HEAD(&br
->port_list
);
398 spin_lock_init(&br
->hash_lock
);
400 br
->bridge_id
.prio
[0] = 0x80;
401 br
->bridge_id
.prio
[1] = 0x00;
403 ether_addr_copy(br
->group_addr
, eth_reserved_addr_base
);
405 br
->stp_enabled
= BR_NO_STP
;
406 br
->group_fwd_mask
= BR_GROUPFWD_DEFAULT
;
407 br
->group_fwd_mask_required
= BR_GROUPFWD_DEFAULT
;
409 br
->designated_root
= br
->bridge_id
;
410 br
->bridge_max_age
= br
->max_age
= 20 * HZ
;
411 br
->bridge_hello_time
= br
->hello_time
= 2 * HZ
;
412 br
->bridge_forward_delay
= br
->forward_delay
= 15 * HZ
;
413 br
->bridge_ageing_time
= br
->ageing_time
= BR_DEFAULT_AGEING_TIME
;
414 dev
->max_mtu
= ETH_MAX_MTU
;
416 br_netfilter_rtable_init(br
);
417 br_stp_timer_init(br
);
418 br_multicast_init(br
);
419 INIT_DELAYED_WORK(&br
->gc_work
, br_fdb_cleanup
);