• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *	Device handling code
3  *	Linux ethernet bridge
4  *
5  *	Authors:
6  *	Lennert Buytenhek		<buytenh@gnu.org>
7  *
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.
12  */
13 
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>
21 
22 #include <asm/uaccess.h>
23 #include "br_private.h"
24 
25 /* net device transmit always called with BH disabled */
br_dev_xmit(struct sk_buff * skb,struct net_device * dev)26 netdev_tx_t br_dev_xmit(struct sk_buff *skb, struct net_device *dev)
27 {
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);
33 	u16 vid = 0;
34 
35 	rcu_read_lock();
36 #ifdef CONFIG_BRIDGE_NETFILTER
37 	if (skb->nf_bridge && (skb->nf_bridge->mask & BRNF_BRIDGED_DNAT)) {
38 		br_nf_pre_routing_finish_bridge_slow(skb);
39 		rcu_read_unlock();
40 		return NETDEV_TX_OK;
41 	}
42 #endif
43 
44 	if (!br_allowed_ingress(br, br_get_vlan_info(br), skb, &vid))
45 		goto out;
46 
47 	BR_INPUT_SKB_CB(skb)->brdev = dev;
48 
49 	skb_reset_mac_header(skb);
50 	skb_pull(skb, ETH_HLEN);
51 
52 	u64_stats_update_begin(&brstats->syncp);
53 	brstats->tx_packets++;
54 	/* Exclude ETH_HLEN from byte stats for consistency with Rx chain */
55 	brstats->tx_bytes += skb->len;
56 	u64_stats_update_end(&brstats->syncp);
57 
58 	if (is_broadcast_ether_addr(dest))
59 		br_flood_deliver(br, skb);
60 	else if (is_multicast_ether_addr(dest)) {
61 		if (unlikely(netpoll_tx_running(dev))) {
62 			br_flood_deliver(br, skb);
63 			goto out;
64 		}
65 		if (br_multicast_rcv(br, NULL, skb)) {
66 			kfree_skb(skb);
67 			goto out;
68 		}
69 
70 		mdst = br_mdb_get(br, skb, vid);
71 		if (mdst || BR_INPUT_SKB_CB_MROUTERS_ONLY(skb))
72 			br_multicast_deliver(mdst, skb);
73 		else
74 			br_flood_deliver(br, skb);
75 	} else if ((dst = __br_fdb_get(br, dest, vid)) != NULL)
76 		br_deliver(dst->dst, skb);
77 	else
78 		br_flood_deliver(br, skb);
79 
80 out:
81 	rcu_read_unlock();
82 	return NETDEV_TX_OK;
83 }
84 
br_dev_init(struct net_device * dev)85 static int br_dev_init(struct net_device *dev)
86 {
87 	struct net_bridge *br = netdev_priv(dev);
88 
89 	br->stats = alloc_percpu(struct br_cpu_netstats);
90 	if (!br->stats)
91 		return -ENOMEM;
92 
93 	return 0;
94 }
95 
br_dev_open(struct net_device * dev)96 static int br_dev_open(struct net_device *dev)
97 {
98 	struct net_bridge *br = netdev_priv(dev);
99 
100 	netdev_update_features(dev);
101 	netif_start_queue(dev);
102 	br_stp_enable_bridge(br);
103 	br_multicast_open(br);
104 
105 	return 0;
106 }
107 
br_dev_set_multicast_list(struct net_device * dev)108 static void br_dev_set_multicast_list(struct net_device *dev)
109 {
110 }
111 
br_dev_stop(struct net_device * dev)112 static int br_dev_stop(struct net_device *dev)
113 {
114 	struct net_bridge *br = netdev_priv(dev);
115 
116 	br_stp_disable_bridge(br);
117 	br_multicast_stop(br);
118 
119 	netif_stop_queue(dev);
120 
121 	return 0;
122 }
123 
br_get_stats64(struct net_device * dev,struct rtnl_link_stats64 * stats)124 static struct rtnl_link_stats64 *br_get_stats64(struct net_device *dev,
125 						struct rtnl_link_stats64 *stats)
126 {
127 	struct net_bridge *br = netdev_priv(dev);
128 	struct br_cpu_netstats tmp, sum = { 0 };
129 	unsigned int cpu;
130 
131 	for_each_possible_cpu(cpu) {
132 		unsigned int start;
133 		const struct br_cpu_netstats *bstats
134 			= per_cpu_ptr(br->stats, cpu);
135 		do {
136 			start = u64_stats_fetch_begin_bh(&bstats->syncp);
137 			memcpy(&tmp, bstats, sizeof(tmp));
138 		} while (u64_stats_fetch_retry_bh(&bstats->syncp, start));
139 		sum.tx_bytes   += tmp.tx_bytes;
140 		sum.tx_packets += tmp.tx_packets;
141 		sum.rx_bytes   += tmp.rx_bytes;
142 		sum.rx_packets += tmp.rx_packets;
143 	}
144 
145 	stats->tx_bytes   = sum.tx_bytes;
146 	stats->tx_packets = sum.tx_packets;
147 	stats->rx_bytes   = sum.rx_bytes;
148 	stats->rx_packets = sum.rx_packets;
149 
150 	return stats;
151 }
152 
br_change_mtu(struct net_device * dev,int new_mtu)153 static int br_change_mtu(struct net_device *dev, int new_mtu)
154 {
155 	struct net_bridge *br = netdev_priv(dev);
156 	if (new_mtu < 68 || new_mtu > br_min_mtu(br))
157 		return -EINVAL;
158 
159 	dev->mtu = new_mtu;
160 
161 #ifdef CONFIG_BRIDGE_NETFILTER
162 	/* remember the MTU in the rtable for PMTU */
163 	dst_metric_set(&br->fake_rtable.dst, RTAX_MTU, new_mtu);
164 #endif
165 
166 	return 0;
167 }
168 
169 /* Allow setting mac address to any valid ethernet address. */
br_set_mac_address(struct net_device * dev,void * p)170 static int br_set_mac_address(struct net_device *dev, void *p)
171 {
172 	struct net_bridge *br = netdev_priv(dev);
173 	struct sockaddr *addr = p;
174 
175 	if (!is_valid_ether_addr(addr->sa_data))
176 		return -EADDRNOTAVAIL;
177 
178 	spin_lock_bh(&br->lock);
179 	if (!ether_addr_equal(dev->dev_addr, addr->sa_data)) {
180 		memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
181 		br_fdb_change_mac_address(br, addr->sa_data);
182 		br_stp_change_bridge_id(br, addr->sa_data);
183 	}
184 	spin_unlock_bh(&br->lock);
185 
186 	return 0;
187 }
188 
br_getinfo(struct net_device * dev,struct ethtool_drvinfo * info)189 static void br_getinfo(struct net_device *dev, struct ethtool_drvinfo *info)
190 {
191 	strlcpy(info->driver, "bridge", sizeof(info->driver));
192 	strlcpy(info->version, BR_VERSION, sizeof(info->version));
193 	strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
194 	strlcpy(info->bus_info, "N/A", sizeof(info->bus_info));
195 }
196 
br_fix_features(struct net_device * dev,netdev_features_t features)197 static netdev_features_t br_fix_features(struct net_device *dev,
198 	netdev_features_t features)
199 {
200 	struct net_bridge *br = netdev_priv(dev);
201 
202 	return br_features_recompute(br, features);
203 }
204 
205 #ifdef CONFIG_NET_POLL_CONTROLLER
br_poll_controller(struct net_device * br_dev)206 static void br_poll_controller(struct net_device *br_dev)
207 {
208 }
209 
br_netpoll_cleanup(struct net_device * dev)210 static void br_netpoll_cleanup(struct net_device *dev)
211 {
212 	struct net_bridge *br = netdev_priv(dev);
213 	struct net_bridge_port *p;
214 
215 	list_for_each_entry(p, &br->port_list, list)
216 		br_netpoll_disable(p);
217 }
218 
br_netpoll_setup(struct net_device * dev,struct netpoll_info * ni,gfp_t gfp)219 static int br_netpoll_setup(struct net_device *dev, struct netpoll_info *ni,
220 			    gfp_t gfp)
221 {
222 	struct net_bridge *br = netdev_priv(dev);
223 	struct net_bridge_port *p;
224 	int err = 0;
225 
226 	list_for_each_entry(p, &br->port_list, list) {
227 		if (!p->dev)
228 			continue;
229 		err = br_netpoll_enable(p, gfp);
230 		if (err)
231 			goto fail;
232 	}
233 
234 out:
235 	return err;
236 
237 fail:
238 	br_netpoll_cleanup(dev);
239 	goto out;
240 }
241 
br_netpoll_enable(struct net_bridge_port * p,gfp_t gfp)242 int br_netpoll_enable(struct net_bridge_port *p, gfp_t gfp)
243 {
244 	struct netpoll *np;
245 	int err = 0;
246 
247 	np = kzalloc(sizeof(*p->np), gfp);
248 	err = -ENOMEM;
249 	if (!np)
250 		goto out;
251 
252 	err = __netpoll_setup(np, p->dev, gfp);
253 	if (err) {
254 		kfree(np);
255 		goto out;
256 	}
257 
258 	p->np = np;
259 
260 out:
261 	return err;
262 }
263 
br_netpoll_disable(struct net_bridge_port * p)264 void br_netpoll_disable(struct net_bridge_port *p)
265 {
266 	struct netpoll *np = p->np;
267 
268 	if (!np)
269 		return;
270 
271 	p->np = NULL;
272 
273 	__netpoll_free_async(np);
274 }
275 
276 #endif
277 
br_add_slave(struct net_device * dev,struct net_device * slave_dev)278 static int br_add_slave(struct net_device *dev, struct net_device *slave_dev)
279 
280 {
281 	struct net_bridge *br = netdev_priv(dev);
282 
283 	return br_add_if(br, slave_dev);
284 }
285 
br_del_slave(struct net_device * dev,struct net_device * slave_dev)286 static int br_del_slave(struct net_device *dev, struct net_device *slave_dev)
287 {
288 	struct net_bridge *br = netdev_priv(dev);
289 
290 	return br_del_if(br, slave_dev);
291 }
292 
293 static const struct ethtool_ops br_ethtool_ops = {
294 	.get_drvinfo    = br_getinfo,
295 	.get_link	= ethtool_op_get_link,
296 };
297 
298 static const struct net_device_ops br_netdev_ops = {
299 	.ndo_open		 = br_dev_open,
300 	.ndo_stop		 = br_dev_stop,
301 	.ndo_init		 = br_dev_init,
302 	.ndo_start_xmit		 = br_dev_xmit,
303 	.ndo_get_stats64	 = br_get_stats64,
304 	.ndo_set_mac_address	 = br_set_mac_address,
305 	.ndo_set_rx_mode	 = br_dev_set_multicast_list,
306 	.ndo_change_mtu		 = br_change_mtu,
307 	.ndo_do_ioctl		 = br_dev_ioctl,
308 #ifdef CONFIG_NET_POLL_CONTROLLER
309 	.ndo_netpoll_setup	 = br_netpoll_setup,
310 	.ndo_netpoll_cleanup	 = br_netpoll_cleanup,
311 	.ndo_poll_controller	 = br_poll_controller,
312 #endif
313 	.ndo_add_slave		 = br_add_slave,
314 	.ndo_del_slave		 = br_del_slave,
315 	.ndo_fix_features        = br_fix_features,
316 	.ndo_fdb_add		 = br_fdb_add,
317 	.ndo_fdb_del		 = br_fdb_delete,
318 	.ndo_fdb_dump		 = br_fdb_dump,
319 	.ndo_bridge_getlink	 = br_getlink,
320 	.ndo_bridge_setlink	 = br_setlink,
321 	.ndo_bridge_dellink	 = br_dellink,
322 };
323 
br_dev_free(struct net_device * dev)324 static void br_dev_free(struct net_device *dev)
325 {
326 	struct net_bridge *br = netdev_priv(dev);
327 
328 	free_percpu(br->stats);
329 	free_netdev(dev);
330 }
331 
332 static struct device_type br_type = {
333 	.name	= "bridge",
334 };
335 
br_dev_setup(struct net_device * dev)336 void br_dev_setup(struct net_device *dev)
337 {
338 	struct net_bridge *br = netdev_priv(dev);
339 
340 	eth_hw_addr_random(dev);
341 	ether_setup(dev);
342 
343 	dev->netdev_ops = &br_netdev_ops;
344 	dev->destructor = br_dev_free;
345 	SET_ETHTOOL_OPS(dev, &br_ethtool_ops);
346 	SET_NETDEV_DEVTYPE(dev, &br_type);
347 	dev->tx_queue_len = 0;
348 	dev->priv_flags = IFF_EBRIDGE;
349 
350 	dev->features = NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HIGHDMA |
351 			NETIF_F_GSO_MASK | NETIF_F_HW_CSUM | NETIF_F_LLTX |
352 			NETIF_F_NETNS_LOCAL | NETIF_F_HW_VLAN_CTAG_TX;
353 	dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HIGHDMA |
354 			   NETIF_F_GSO_MASK | NETIF_F_HW_CSUM |
355 			   NETIF_F_HW_VLAN_CTAG_TX;
356 
357 	br->dev = dev;
358 	spin_lock_init(&br->lock);
359 	INIT_LIST_HEAD(&br->port_list);
360 	spin_lock_init(&br->hash_lock);
361 
362 	br->bridge_id.prio[0] = 0x80;
363 	br->bridge_id.prio[1] = 0x00;
364 
365 	memcpy(br->group_addr, eth_reserved_addr_base, ETH_ALEN);
366 
367 	br->stp_enabled = BR_NO_STP;
368 	br->group_fwd_mask = BR_GROUPFWD_DEFAULT;
369 
370 	br->designated_root = br->bridge_id;
371 	br->bridge_max_age = br->max_age = 20 * HZ;
372 	br->bridge_hello_time = br->hello_time = 2 * HZ;
373 	br->bridge_forward_delay = br->forward_delay = 15 * HZ;
374 	br->ageing_time = 300 * HZ;
375 
376 	br_netfilter_rtable_init(br);
377 	br_stp_timer_init(br);
378 	br_multicast_init(br);
379 }
380