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 <asm/uaccess.h>
23 #include "br_private.h"
25 /* net device transmit always called with no BH (preempt_disabled) */
26 netdev_tx_t
br_dev_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
28 struct net_bridge
*br
= netdev_priv(dev
);
29 const unsigned char *dest
= skb
->data
;
30 struct net_bridge_fdb_entry
*dst
;
31 struct net_bridge_mdb_entry
*mdst
;
32 struct br_cpu_netstats
*brstats
= this_cpu_ptr(br
->stats
);
34 #ifdef CONFIG_BRIDGE_NETFILTER
35 if (skb
->nf_bridge
&& (skb
->nf_bridge
->mask
& BRNF_BRIDGED_DNAT
)) {
36 br_nf_pre_routing_finish_bridge_slow(skb
);
41 brstats
->tx_packets
++;
42 brstats
->tx_bytes
+= skb
->len
;
44 BR_INPUT_SKB_CB(skb
)->brdev
= dev
;
46 skb_reset_mac_header(skb
);
47 skb_pull(skb
, ETH_HLEN
);
49 if (is_multicast_ether_addr(dest
)) {
50 if (br_multicast_rcv(br
, NULL
, skb
))
53 mdst
= br_mdb_get(br
, skb
);
54 if (mdst
|| BR_INPUT_SKB_CB_MROUTERS_ONLY(skb
))
55 br_multicast_deliver(mdst
, skb
);
57 br_flood_deliver(br
, skb
);
58 } else if ((dst
= __br_fdb_get(br
, dest
)) != NULL
)
59 br_deliver(dst
->dst
, skb
);
61 br_flood_deliver(br
, skb
);
67 static int br_dev_open(struct net_device
*dev
)
69 struct net_bridge
*br
= netdev_priv(dev
);
71 br_features_recompute(br
);
72 netif_start_queue(dev
);
73 br_stp_enable_bridge(br
);
74 br_multicast_open(br
);
79 static void br_dev_set_multicast_list(struct net_device
*dev
)
83 static int br_dev_stop(struct net_device
*dev
)
85 struct net_bridge
*br
= netdev_priv(dev
);
87 br_stp_disable_bridge(br
);
88 br_multicast_stop(br
);
90 netif_stop_queue(dev
);
95 static struct net_device_stats
*br_get_stats(struct net_device
*dev
)
97 struct net_bridge
*br
= netdev_priv(dev
);
98 struct net_device_stats
*stats
= &dev
->stats
;
99 struct br_cpu_netstats sum
= { 0 };
102 for_each_possible_cpu(cpu
) {
103 const struct br_cpu_netstats
*bstats
104 = per_cpu_ptr(br
->stats
, cpu
);
106 sum
.tx_bytes
+= bstats
->tx_bytes
;
107 sum
.tx_packets
+= bstats
->tx_packets
;
108 sum
.rx_bytes
+= bstats
->rx_bytes
;
109 sum
.rx_packets
+= bstats
->rx_packets
;
112 stats
->tx_bytes
= sum
.tx_bytes
;
113 stats
->tx_packets
= sum
.tx_packets
;
114 stats
->rx_bytes
= sum
.rx_bytes
;
115 stats
->rx_packets
= sum
.rx_packets
;
120 static int br_change_mtu(struct net_device
*dev
, int new_mtu
)
122 struct net_bridge
*br
= netdev_priv(dev
);
123 if (new_mtu
< 68 || new_mtu
> br_min_mtu(br
))
128 #ifdef CONFIG_BRIDGE_NETFILTER
129 /* remember the MTU in the rtable for PMTU */
130 br
->fake_rtable
.u
.dst
.metrics
[RTAX_MTU
- 1] = new_mtu
;
136 /* Allow setting mac address to any valid ethernet address. */
137 static int br_set_mac_address(struct net_device
*dev
, void *p
)
139 struct net_bridge
*br
= netdev_priv(dev
);
140 struct sockaddr
*addr
= p
;
142 if (!is_valid_ether_addr(addr
->sa_data
))
145 spin_lock_bh(&br
->lock
);
146 memcpy(dev
->dev_addr
, addr
->sa_data
, ETH_ALEN
);
147 br_stp_change_bridge_id(br
, addr
->sa_data
);
148 br
->flags
|= BR_SET_MAC_ADDR
;
149 spin_unlock_bh(&br
->lock
);
154 static void br_getinfo(struct net_device
*dev
, struct ethtool_drvinfo
*info
)
156 strcpy(info
->driver
, "bridge");
157 strcpy(info
->version
, BR_VERSION
);
158 strcpy(info
->fw_version
, "N/A");
159 strcpy(info
->bus_info
, "N/A");
162 static int br_set_sg(struct net_device
*dev
, u32 data
)
164 struct net_bridge
*br
= netdev_priv(dev
);
167 br
->feature_mask
|= NETIF_F_SG
;
169 br
->feature_mask
&= ~NETIF_F_SG
;
171 br_features_recompute(br
);
175 static int br_set_tso(struct net_device
*dev
, u32 data
)
177 struct net_bridge
*br
= netdev_priv(dev
);
180 br
->feature_mask
|= NETIF_F_TSO
;
182 br
->feature_mask
&= ~NETIF_F_TSO
;
184 br_features_recompute(br
);
188 static int br_set_tx_csum(struct net_device
*dev
, u32 data
)
190 struct net_bridge
*br
= netdev_priv(dev
);
193 br
->feature_mask
|= NETIF_F_NO_CSUM
;
195 br
->feature_mask
&= ~NETIF_F_ALL_CSUM
;
197 br_features_recompute(br
);
201 #ifdef CONFIG_NET_POLL_CONTROLLER
202 static bool br_devices_support_netpoll(struct net_bridge
*br
)
204 struct net_bridge_port
*p
;
209 spin_lock_irqsave(&br
->lock
, flags
);
210 list_for_each_entry(p
, &br
->port_list
, list
) {
212 if ((p
->dev
->priv_flags
& IFF_DISABLE_NETPOLL
) ||
213 !p
->dev
->netdev_ops
->ndo_poll_controller
)
216 spin_unlock_irqrestore(&br
->lock
, flags
);
217 return count
!= 0 && ret
;
220 static void br_poll_controller(struct net_device
*br_dev
)
222 struct netpoll
*np
= br_dev
->npinfo
->netpoll
;
224 if (np
->real_dev
!= br_dev
)
225 netpoll_poll_dev(np
->real_dev
);
228 void br_netpoll_cleanup(struct net_device
*dev
)
230 struct net_bridge
*br
= netdev_priv(dev
);
231 struct net_bridge_port
*p
, *n
;
232 const struct net_device_ops
*ops
;
234 br
->dev
->npinfo
= NULL
;
235 list_for_each_entry_safe(p
, n
, &br
->port_list
, list
) {
237 ops
= p
->dev
->netdev_ops
;
238 if (ops
->ndo_netpoll_cleanup
)
239 ops
->ndo_netpoll_cleanup(p
->dev
);
241 p
->dev
->npinfo
= NULL
;
246 void br_netpoll_disable(struct net_bridge
*br
,
247 struct net_device
*dev
)
249 if (br_devices_support_netpoll(br
))
250 br
->dev
->priv_flags
&= ~IFF_DISABLE_NETPOLL
;
251 if (dev
->netdev_ops
->ndo_netpoll_cleanup
)
252 dev
->netdev_ops
->ndo_netpoll_cleanup(dev
);
257 void br_netpoll_enable(struct net_bridge
*br
,
258 struct net_device
*dev
)
260 if (br_devices_support_netpoll(br
)) {
261 br
->dev
->priv_flags
&= ~IFF_DISABLE_NETPOLL
;
263 dev
->npinfo
= br
->dev
->npinfo
;
264 } else if (!(br
->dev
->priv_flags
& IFF_DISABLE_NETPOLL
)) {
265 br
->dev
->priv_flags
|= IFF_DISABLE_NETPOLL
;
266 br_info(br
,"new device %s does not support netpoll (disabling)",
273 static const struct ethtool_ops br_ethtool_ops
= {
274 .get_drvinfo
= br_getinfo
,
275 .get_link
= ethtool_op_get_link
,
276 .get_tx_csum
= ethtool_op_get_tx_csum
,
277 .set_tx_csum
= br_set_tx_csum
,
278 .get_sg
= ethtool_op_get_sg
,
280 .get_tso
= ethtool_op_get_tso
,
281 .set_tso
= br_set_tso
,
282 .get_ufo
= ethtool_op_get_ufo
,
283 .set_ufo
= ethtool_op_set_ufo
,
284 .get_flags
= ethtool_op_get_flags
,
287 static const struct net_device_ops br_netdev_ops
= {
288 .ndo_open
= br_dev_open
,
289 .ndo_stop
= br_dev_stop
,
290 .ndo_start_xmit
= br_dev_xmit
,
291 .ndo_get_stats
= br_get_stats
,
292 .ndo_set_mac_address
= br_set_mac_address
,
293 .ndo_set_multicast_list
= br_dev_set_multicast_list
,
294 .ndo_change_mtu
= br_change_mtu
,
295 .ndo_do_ioctl
= br_dev_ioctl
,
296 #ifdef CONFIG_NET_POLL_CONTROLLER
297 .ndo_netpoll_cleanup
= br_netpoll_cleanup
,
298 .ndo_poll_controller
= br_poll_controller
,
302 static void br_dev_free(struct net_device
*dev
)
304 struct net_bridge
*br
= netdev_priv(dev
);
306 free_percpu(br
->stats
);
310 void br_dev_setup(struct net_device
*dev
)
312 random_ether_addr(dev
->dev_addr
);
315 dev
->netdev_ops
= &br_netdev_ops
;
316 dev
->destructor
= br_dev_free
;
317 SET_ETHTOOL_OPS(dev
, &br_ethtool_ops
);
318 dev
->tx_queue_len
= 0;
319 dev
->priv_flags
= IFF_EBRIDGE
;
321 dev
->features
= NETIF_F_SG
| NETIF_F_FRAGLIST
| NETIF_F_HIGHDMA
|
322 NETIF_F_GSO_MASK
| NETIF_F_NO_CSUM
| NETIF_F_LLTX
|
323 NETIF_F_NETNS_LOCAL
| NETIF_F_GSO
;