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