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/etherdevice.h>
17 #include <linux/ethtool.h>
19 #include <asm/uaccess.h>
20 #include "br_private.h"
22 /* net device transmit always called with no BH (preempt_disabled) */
23 netdev_tx_t
br_dev_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
25 struct net_bridge
*br
= netdev_priv(dev
);
26 const unsigned char *dest
= skb
->data
;
27 struct net_bridge_fdb_entry
*dst
;
28 struct net_bridge_mdb_entry
*mdst
;
29 struct br_cpu_netstats
*brstats
= this_cpu_ptr(br
->stats
);
31 brstats
->tx_packets
++;
32 brstats
->tx_bytes
+= skb
->len
;
34 BR_INPUT_SKB_CB(skb
)->brdev
= dev
;
36 skb_reset_mac_header(skb
);
37 skb_pull(skb
, ETH_HLEN
);
40 if (br_multicast_rcv(br
, NULL
, skb
))
43 mdst
= br_mdb_get(br
, skb
);
44 if (mdst
|| BR_INPUT_SKB_CB_MROUTERS_ONLY(skb
))
45 br_multicast_deliver(mdst
, skb
);
47 br_flood_deliver(br
, skb
);
48 } else if ((dst
= __br_fdb_get(br
, dest
)) != NULL
)
49 br_deliver(dst
->dst
, skb
);
51 br_flood_deliver(br
, skb
);
57 static int br_dev_open(struct net_device
*dev
)
59 struct net_bridge
*br
= netdev_priv(dev
);
61 br_features_recompute(br
);
62 netif_start_queue(dev
);
63 br_stp_enable_bridge(br
);
64 br_multicast_open(br
);
69 static void br_dev_set_multicast_list(struct net_device
*dev
)
73 static int br_dev_stop(struct net_device
*dev
)
75 struct net_bridge
*br
= netdev_priv(dev
);
77 br_stp_disable_bridge(br
);
78 br_multicast_stop(br
);
80 netif_stop_queue(dev
);
85 static struct net_device_stats
*br_get_stats(struct net_device
*dev
)
87 struct net_bridge
*br
= netdev_priv(dev
);
88 struct net_device_stats
*stats
= &dev
->stats
;
89 struct br_cpu_netstats sum
= { 0 };
92 for_each_possible_cpu(cpu
) {
93 const struct br_cpu_netstats
*bstats
94 = per_cpu_ptr(br
->stats
, cpu
);
96 sum
.tx_bytes
+= bstats
->tx_bytes
;
97 sum
.tx_packets
+= bstats
->tx_packets
;
98 sum
.rx_bytes
+= bstats
->rx_bytes
;
99 sum
.rx_packets
+= bstats
->rx_packets
;
102 stats
->tx_bytes
= sum
.tx_bytes
;
103 stats
->tx_packets
= sum
.tx_packets
;
104 stats
->rx_bytes
= sum
.rx_bytes
;
105 stats
->rx_packets
= sum
.rx_packets
;
110 static int br_change_mtu(struct net_device
*dev
, int new_mtu
)
112 struct net_bridge
*br
= netdev_priv(dev
);
113 if (new_mtu
< 68 || new_mtu
> br_min_mtu(br
))
118 #ifdef CONFIG_BRIDGE_NETFILTER
119 /* remember the MTU in the rtable for PMTU */
120 br
->fake_rtable
.u
.dst
.metrics
[RTAX_MTU
- 1] = new_mtu
;
126 /* Allow setting mac address to any valid ethernet address. */
127 static int br_set_mac_address(struct net_device
*dev
, void *p
)
129 struct net_bridge
*br
= netdev_priv(dev
);
130 struct sockaddr
*addr
= p
;
132 if (!is_valid_ether_addr(addr
->sa_data
))
135 spin_lock_bh(&br
->lock
);
136 memcpy(dev
->dev_addr
, addr
->sa_data
, ETH_ALEN
);
137 br_stp_change_bridge_id(br
, addr
->sa_data
);
138 br
->flags
|= BR_SET_MAC_ADDR
;
139 spin_unlock_bh(&br
->lock
);
144 static void br_getinfo(struct net_device
*dev
, struct ethtool_drvinfo
*info
)
146 strcpy(info
->driver
, "bridge");
147 strcpy(info
->version
, BR_VERSION
);
148 strcpy(info
->fw_version
, "N/A");
149 strcpy(info
->bus_info
, "N/A");
152 static int br_set_sg(struct net_device
*dev
, u32 data
)
154 struct net_bridge
*br
= netdev_priv(dev
);
157 br
->feature_mask
|= NETIF_F_SG
;
159 br
->feature_mask
&= ~NETIF_F_SG
;
161 br_features_recompute(br
);
165 static int br_set_tso(struct net_device
*dev
, u32 data
)
167 struct net_bridge
*br
= netdev_priv(dev
);
170 br
->feature_mask
|= NETIF_F_TSO
;
172 br
->feature_mask
&= ~NETIF_F_TSO
;
174 br_features_recompute(br
);
178 static int br_set_tx_csum(struct net_device
*dev
, u32 data
)
180 struct net_bridge
*br
= netdev_priv(dev
);
183 br
->feature_mask
|= NETIF_F_NO_CSUM
;
185 br
->feature_mask
&= ~NETIF_F_ALL_CSUM
;
187 br_features_recompute(br
);
191 static const struct ethtool_ops br_ethtool_ops
= {
192 .get_drvinfo
= br_getinfo
,
193 .get_link
= ethtool_op_get_link
,
194 .get_tx_csum
= ethtool_op_get_tx_csum
,
195 .set_tx_csum
= br_set_tx_csum
,
196 .get_sg
= ethtool_op_get_sg
,
198 .get_tso
= ethtool_op_get_tso
,
199 .set_tso
= br_set_tso
,
200 .get_ufo
= ethtool_op_get_ufo
,
201 .set_ufo
= ethtool_op_set_ufo
,
202 .get_flags
= ethtool_op_get_flags
,
205 static const struct net_device_ops br_netdev_ops
= {
206 .ndo_open
= br_dev_open
,
207 .ndo_stop
= br_dev_stop
,
208 .ndo_start_xmit
= br_dev_xmit
,
209 .ndo_get_stats
= br_get_stats
,
210 .ndo_set_mac_address
= br_set_mac_address
,
211 .ndo_set_multicast_list
= br_dev_set_multicast_list
,
212 .ndo_change_mtu
= br_change_mtu
,
213 .ndo_do_ioctl
= br_dev_ioctl
,
216 static void br_dev_free(struct net_device
*dev
)
218 struct net_bridge
*br
= netdev_priv(dev
);
220 free_percpu(br
->stats
);
224 void br_dev_setup(struct net_device
*dev
)
226 random_ether_addr(dev
->dev_addr
);
229 dev
->netdev_ops
= &br_netdev_ops
;
230 dev
->destructor
= br_dev_free
;
231 SET_ETHTOOL_OPS(dev
, &br_ethtool_ops
);
232 dev
->tx_queue_len
= 0;
233 dev
->priv_flags
= IFF_EBRIDGE
;
235 dev
->features
= NETIF_F_SG
| NETIF_F_FRAGLIST
| NETIF_F_HIGHDMA
|
236 NETIF_F_GSO_MASK
| NETIF_F_NO_CSUM
| NETIF_F_LLTX
|
237 NETIF_F_NETNS_LOCAL
| NETIF_F_GSO
;