xref: /openbmc/linux/net/bridge/br_device.c (revision bd4af432)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *	Device handling code
4  *	Linux ethernet bridge
5  *
6  *	Authors:
7  *	Lennert Buytenhek		<buytenh@gnu.org>
8  */
9 
10 #include <linux/kernel.h>
11 #include <linux/netdevice.h>
12 #include <linux/netpoll.h>
13 #include <linux/etherdevice.h>
14 #include <linux/ethtool.h>
15 #include <linux/list.h>
16 #include <linux/netfilter_bridge.h>
17 
18 #include <linux/uaccess.h>
19 #include "br_private.h"
20 
21 #define COMMON_FEATURES (NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HIGHDMA | \
22 			 NETIF_F_GSO_MASK | NETIF_F_HW_CSUM)
23 
24 const struct nf_br_ops __rcu *nf_br_ops __read_mostly;
25 EXPORT_SYMBOL_GPL(nf_br_ops);
26 
27 /* net device transmit always called with BH disabled */
28 netdev_tx_t br_dev_xmit(struct sk_buff *skb, struct net_device *dev)
29 {
30 	struct net_bridge *br = netdev_priv(dev);
31 	struct net_bridge_fdb_entry *dst;
32 	struct net_bridge_mdb_entry *mdst;
33 	struct pcpu_sw_netstats *brstats = this_cpu_ptr(br->stats);
34 	const struct nf_br_ops *nf_ops;
35 	u8 state = BR_STATE_FORWARDING;
36 	const unsigned char *dest;
37 	u16 vid = 0;
38 
39 	rcu_read_lock();
40 	nf_ops = rcu_dereference(nf_br_ops);
41 	if (nf_ops && nf_ops->br_dev_xmit_hook(skb)) {
42 		rcu_read_unlock();
43 		return NETDEV_TX_OK;
44 	}
45 
46 	u64_stats_update_begin(&brstats->syncp);
47 	brstats->tx_packets++;
48 	brstats->tx_bytes += skb->len;
49 	u64_stats_update_end(&brstats->syncp);
50 
51 	br_switchdev_frame_unmark(skb);
52 	BR_INPUT_SKB_CB(skb)->brdev = dev;
53 	BR_INPUT_SKB_CB(skb)->frag_max_size = 0;
54 
55 	skb_reset_mac_header(skb);
56 	skb_pull(skb, ETH_HLEN);
57 
58 	if (!br_allowed_ingress(br, br_vlan_group_rcu(br), skb, &vid, &state))
59 		goto out;
60 
61 	if (IS_ENABLED(CONFIG_INET) &&
62 	    (eth_hdr(skb)->h_proto == htons(ETH_P_ARP) ||
63 	     eth_hdr(skb)->h_proto == htons(ETH_P_RARP)) &&
64 	    br_opt_get(br, BROPT_NEIGH_SUPPRESS_ENABLED)) {
65 		br_do_proxy_suppress_arp(skb, br, vid, NULL);
66 	} else if (IS_ENABLED(CONFIG_IPV6) &&
67 		   skb->protocol == htons(ETH_P_IPV6) &&
68 		   br_opt_get(br, BROPT_NEIGH_SUPPRESS_ENABLED) &&
69 		   pskb_may_pull(skb, sizeof(struct ipv6hdr) +
70 				 sizeof(struct nd_msg)) &&
71 		   ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
72 			struct nd_msg *msg, _msg;
73 
74 			msg = br_is_nd_neigh_msg(skb, &_msg);
75 			if (msg)
76 				br_do_suppress_nd(skb, br, vid, NULL, msg);
77 	}
78 
79 	dest = eth_hdr(skb)->h_dest;
80 	if (is_broadcast_ether_addr(dest)) {
81 		br_flood(br, skb, BR_PKT_BROADCAST, false, true);
82 	} else if (is_multicast_ether_addr(dest)) {
83 		if (unlikely(netpoll_tx_running(dev))) {
84 			br_flood(br, skb, BR_PKT_MULTICAST, false, true);
85 			goto out;
86 		}
87 		if (br_multicast_rcv(br, NULL, skb, vid)) {
88 			kfree_skb(skb);
89 			goto out;
90 		}
91 
92 		mdst = br_mdb_get(br, skb, vid);
93 		if ((mdst || BR_INPUT_SKB_CB_MROUTERS_ONLY(skb)) &&
94 		    br_multicast_querier_exists(br, eth_hdr(skb)))
95 			br_multicast_flood(mdst, skb, false, true);
96 		else
97 			br_flood(br, skb, BR_PKT_MULTICAST, false, true);
98 	} else if ((dst = br_fdb_find_rcu(br, dest, vid)) != NULL) {
99 		br_forward(dst->dst, skb, false, true);
100 	} else {
101 		br_flood(br, skb, BR_PKT_UNICAST, false, true);
102 	}
103 out:
104 	rcu_read_unlock();
105 	return NETDEV_TX_OK;
106 }
107 
108 static int br_dev_init(struct net_device *dev)
109 {
110 	struct net_bridge *br = netdev_priv(dev);
111 	int err;
112 
113 	br->stats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
114 	if (!br->stats)
115 		return -ENOMEM;
116 
117 	err = br_fdb_hash_init(br);
118 	if (err) {
119 		free_percpu(br->stats);
120 		return err;
121 	}
122 
123 	err = br_mdb_hash_init(br);
124 	if (err) {
125 		free_percpu(br->stats);
126 		br_fdb_hash_fini(br);
127 		return err;
128 	}
129 
130 	err = br_vlan_init(br);
131 	if (err) {
132 		free_percpu(br->stats);
133 		br_mdb_hash_fini(br);
134 		br_fdb_hash_fini(br);
135 		return err;
136 	}
137 
138 	err = br_multicast_init_stats(br);
139 	if (err) {
140 		free_percpu(br->stats);
141 		br_vlan_flush(br);
142 		br_mdb_hash_fini(br);
143 		br_fdb_hash_fini(br);
144 	}
145 
146 	return err;
147 }
148 
149 static void br_dev_uninit(struct net_device *dev)
150 {
151 	struct net_bridge *br = netdev_priv(dev);
152 
153 	br_multicast_dev_del(br);
154 	br_multicast_uninit_stats(br);
155 	br_vlan_flush(br);
156 	br_mdb_hash_fini(br);
157 	br_fdb_hash_fini(br);
158 	free_percpu(br->stats);
159 }
160 
161 static int br_dev_open(struct net_device *dev)
162 {
163 	struct net_bridge *br = netdev_priv(dev);
164 
165 	netdev_update_features(dev);
166 	netif_start_queue(dev);
167 	br_stp_enable_bridge(br);
168 	br_multicast_open(br);
169 
170 	return 0;
171 }
172 
173 static void br_dev_set_multicast_list(struct net_device *dev)
174 {
175 }
176 
177 static void br_dev_change_rx_flags(struct net_device *dev, int change)
178 {
179 	if (change & IFF_PROMISC)
180 		br_manage_promisc(netdev_priv(dev));
181 }
182 
183 static int br_dev_stop(struct net_device *dev)
184 {
185 	struct net_bridge *br = netdev_priv(dev);
186 
187 	br_stp_disable_bridge(br);
188 	br_multicast_stop(br);
189 
190 	netif_stop_queue(dev);
191 
192 	return 0;
193 }
194 
195 static void br_get_stats64(struct net_device *dev,
196 			   struct rtnl_link_stats64 *stats)
197 {
198 	struct net_bridge *br = netdev_priv(dev);
199 	struct pcpu_sw_netstats tmp, sum = { 0 };
200 	unsigned int cpu;
201 
202 	for_each_possible_cpu(cpu) {
203 		unsigned int start;
204 		const struct pcpu_sw_netstats *bstats
205 			= per_cpu_ptr(br->stats, cpu);
206 		do {
207 			start = u64_stats_fetch_begin_irq(&bstats->syncp);
208 			memcpy(&tmp, bstats, sizeof(tmp));
209 		} while (u64_stats_fetch_retry_irq(&bstats->syncp, start));
210 		sum.tx_bytes   += tmp.tx_bytes;
211 		sum.tx_packets += tmp.tx_packets;
212 		sum.rx_bytes   += tmp.rx_bytes;
213 		sum.rx_packets += tmp.rx_packets;
214 	}
215 
216 	stats->tx_bytes   = sum.tx_bytes;
217 	stats->tx_packets = sum.tx_packets;
218 	stats->rx_bytes   = sum.rx_bytes;
219 	stats->rx_packets = sum.rx_packets;
220 }
221 
222 static int br_change_mtu(struct net_device *dev, int new_mtu)
223 {
224 	struct net_bridge *br = netdev_priv(dev);
225 
226 	dev->mtu = new_mtu;
227 
228 	/* this flag will be cleared if the MTU was automatically adjusted */
229 	br_opt_toggle(br, BROPT_MTU_SET_BY_USER, true);
230 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
231 	/* remember the MTU in the rtable for PMTU */
232 	dst_metric_set(&br->fake_rtable.dst, RTAX_MTU, new_mtu);
233 #endif
234 
235 	return 0;
236 }
237 
238 /* Allow setting mac address to any valid ethernet address. */
239 static int br_set_mac_address(struct net_device *dev, void *p)
240 {
241 	struct net_bridge *br = netdev_priv(dev);
242 	struct sockaddr *addr = p;
243 
244 	if (!is_valid_ether_addr(addr->sa_data))
245 		return -EADDRNOTAVAIL;
246 
247 	/* dev_set_mac_addr() can be called by a master device on bridge's
248 	 * NETDEV_UNREGISTER, but since it's being destroyed do nothing
249 	 */
250 	if (dev->reg_state != NETREG_REGISTERED)
251 		return -EBUSY;
252 
253 	spin_lock_bh(&br->lock);
254 	if (!ether_addr_equal(dev->dev_addr, addr->sa_data)) {
255 		/* Mac address will be changed in br_stp_change_bridge_id(). */
256 		br_stp_change_bridge_id(br, addr->sa_data);
257 	}
258 	spin_unlock_bh(&br->lock);
259 
260 	return 0;
261 }
262 
263 static void br_getinfo(struct net_device *dev, struct ethtool_drvinfo *info)
264 {
265 	strlcpy(info->driver, "bridge", sizeof(info->driver));
266 	strlcpy(info->version, BR_VERSION, sizeof(info->version));
267 	strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
268 	strlcpy(info->bus_info, "N/A", sizeof(info->bus_info));
269 }
270 
271 static int br_get_link_ksettings(struct net_device *dev,
272 				 struct ethtool_link_ksettings *cmd)
273 {
274 	struct net_bridge *br = netdev_priv(dev);
275 	struct net_bridge_port *p;
276 
277 	cmd->base.duplex = DUPLEX_UNKNOWN;
278 	cmd->base.port = PORT_OTHER;
279 	cmd->base.speed = SPEED_UNKNOWN;
280 
281 	list_for_each_entry(p, &br->port_list, list) {
282 		struct ethtool_link_ksettings ecmd;
283 		struct net_device *pdev = p->dev;
284 
285 		if (!netif_running(pdev) || !netif_oper_up(pdev))
286 			continue;
287 
288 		if (__ethtool_get_link_ksettings(pdev, &ecmd))
289 			continue;
290 
291 		if (ecmd.base.speed == (__u32)SPEED_UNKNOWN)
292 			continue;
293 
294 		if (cmd->base.speed == (__u32)SPEED_UNKNOWN ||
295 		    cmd->base.speed < ecmd.base.speed)
296 			cmd->base.speed = ecmd.base.speed;
297 	}
298 
299 	return 0;
300 }
301 
302 static netdev_features_t br_fix_features(struct net_device *dev,
303 	netdev_features_t features)
304 {
305 	struct net_bridge *br = netdev_priv(dev);
306 
307 	return br_features_recompute(br, features);
308 }
309 
310 #ifdef CONFIG_NET_POLL_CONTROLLER
311 static void br_poll_controller(struct net_device *br_dev)
312 {
313 }
314 
315 static void br_netpoll_cleanup(struct net_device *dev)
316 {
317 	struct net_bridge *br = netdev_priv(dev);
318 	struct net_bridge_port *p;
319 
320 	list_for_each_entry(p, &br->port_list, list)
321 		br_netpoll_disable(p);
322 }
323 
324 static int __br_netpoll_enable(struct net_bridge_port *p)
325 {
326 	struct netpoll *np;
327 	int err;
328 
329 	np = kzalloc(sizeof(*p->np), GFP_KERNEL);
330 	if (!np)
331 		return -ENOMEM;
332 
333 	err = __netpoll_setup(np, p->dev);
334 	if (err) {
335 		kfree(np);
336 		return err;
337 	}
338 
339 	p->np = np;
340 	return err;
341 }
342 
343 int br_netpoll_enable(struct net_bridge_port *p)
344 {
345 	if (!p->br->dev->npinfo)
346 		return 0;
347 
348 	return __br_netpoll_enable(p);
349 }
350 
351 static int br_netpoll_setup(struct net_device *dev, struct netpoll_info *ni)
352 {
353 	struct net_bridge *br = netdev_priv(dev);
354 	struct net_bridge_port *p;
355 	int err = 0;
356 
357 	list_for_each_entry(p, &br->port_list, list) {
358 		if (!p->dev)
359 			continue;
360 		err = __br_netpoll_enable(p);
361 		if (err)
362 			goto fail;
363 	}
364 
365 out:
366 	return err;
367 
368 fail:
369 	br_netpoll_cleanup(dev);
370 	goto out;
371 }
372 
373 void br_netpoll_disable(struct net_bridge_port *p)
374 {
375 	struct netpoll *np = p->np;
376 
377 	if (!np)
378 		return;
379 
380 	p->np = NULL;
381 
382 	__netpoll_free(np);
383 }
384 
385 #endif
386 
387 static int br_add_slave(struct net_device *dev, struct net_device *slave_dev,
388 			struct netlink_ext_ack *extack)
389 
390 {
391 	struct net_bridge *br = netdev_priv(dev);
392 
393 	return br_add_if(br, slave_dev, extack);
394 }
395 
396 static int br_del_slave(struct net_device *dev, struct net_device *slave_dev)
397 {
398 	struct net_bridge *br = netdev_priv(dev);
399 
400 	return br_del_if(br, slave_dev);
401 }
402 
403 static const struct ethtool_ops br_ethtool_ops = {
404 	.get_drvinfo		 = br_getinfo,
405 	.get_link		 = ethtool_op_get_link,
406 	.get_link_ksettings	 = br_get_link_ksettings,
407 };
408 
409 static const struct net_device_ops br_netdev_ops = {
410 	.ndo_open		 = br_dev_open,
411 	.ndo_stop		 = br_dev_stop,
412 	.ndo_init		 = br_dev_init,
413 	.ndo_uninit		 = br_dev_uninit,
414 	.ndo_start_xmit		 = br_dev_xmit,
415 	.ndo_get_stats64	 = br_get_stats64,
416 	.ndo_set_mac_address	 = br_set_mac_address,
417 	.ndo_set_rx_mode	 = br_dev_set_multicast_list,
418 	.ndo_change_rx_flags	 = br_dev_change_rx_flags,
419 	.ndo_change_mtu		 = br_change_mtu,
420 	.ndo_do_ioctl		 = br_dev_ioctl,
421 #ifdef CONFIG_NET_POLL_CONTROLLER
422 	.ndo_netpoll_setup	 = br_netpoll_setup,
423 	.ndo_netpoll_cleanup	 = br_netpoll_cleanup,
424 	.ndo_poll_controller	 = br_poll_controller,
425 #endif
426 	.ndo_add_slave		 = br_add_slave,
427 	.ndo_del_slave		 = br_del_slave,
428 	.ndo_fix_features        = br_fix_features,
429 	.ndo_fdb_add		 = br_fdb_add,
430 	.ndo_fdb_del		 = br_fdb_delete,
431 	.ndo_fdb_dump		 = br_fdb_dump,
432 	.ndo_fdb_get		 = br_fdb_get,
433 	.ndo_bridge_getlink	 = br_getlink,
434 	.ndo_bridge_setlink	 = br_setlink,
435 	.ndo_bridge_dellink	 = br_dellink,
436 	.ndo_features_check	 = passthru_features_check,
437 };
438 
439 static struct device_type br_type = {
440 	.name	= "bridge",
441 };
442 
443 void br_dev_setup(struct net_device *dev)
444 {
445 	struct net_bridge *br = netdev_priv(dev);
446 
447 	eth_hw_addr_random(dev);
448 	ether_setup(dev);
449 
450 	dev->netdev_ops = &br_netdev_ops;
451 	dev->needs_free_netdev = true;
452 	dev->ethtool_ops = &br_ethtool_ops;
453 	SET_NETDEV_DEVTYPE(dev, &br_type);
454 	dev->priv_flags = IFF_EBRIDGE | IFF_NO_QUEUE;
455 
456 	dev->features = COMMON_FEATURES | NETIF_F_LLTX | NETIF_F_NETNS_LOCAL |
457 			NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
458 	dev->hw_features = COMMON_FEATURES | NETIF_F_HW_VLAN_CTAG_TX |
459 			   NETIF_F_HW_VLAN_STAG_TX;
460 	dev->vlan_features = COMMON_FEATURES;
461 
462 	br->dev = dev;
463 	spin_lock_init(&br->lock);
464 	INIT_LIST_HEAD(&br->port_list);
465 	INIT_HLIST_HEAD(&br->fdb_list);
466 	spin_lock_init(&br->hash_lock);
467 
468 	br->bridge_id.prio[0] = 0x80;
469 	br->bridge_id.prio[1] = 0x00;
470 
471 	ether_addr_copy(br->group_addr, eth_stp_addr);
472 
473 	br->stp_enabled = BR_NO_STP;
474 	br->group_fwd_mask = BR_GROUPFWD_DEFAULT;
475 	br->group_fwd_mask_required = BR_GROUPFWD_DEFAULT;
476 
477 	br->designated_root = br->bridge_id;
478 	br->bridge_max_age = br->max_age = 20 * HZ;
479 	br->bridge_hello_time = br->hello_time = 2 * HZ;
480 	br->bridge_forward_delay = br->forward_delay = 15 * HZ;
481 	br->bridge_ageing_time = br->ageing_time = BR_DEFAULT_AGEING_TIME;
482 	dev->max_mtu = ETH_MAX_MTU;
483 
484 	br_netfilter_rtable_init(br);
485 	br_stp_timer_init(br);
486 	br_multicast_init(br);
487 	INIT_DELAYED_WORK(&br->gc_work, br_fdb_cleanup);
488 }
489