xref: /openbmc/linux/net/bridge/br_device.c (revision 62975d27)
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 struct lock_class_key bridge_netdev_addr_lock_key;
109 
110 static void br_set_lockdep_class(struct net_device *dev)
111 {
112 	lockdep_set_class(&dev->addr_list_lock, &bridge_netdev_addr_lock_key);
113 }
114 
115 static int br_dev_init(struct net_device *dev)
116 {
117 	struct net_bridge *br = netdev_priv(dev);
118 	int err;
119 
120 	br->stats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
121 	if (!br->stats)
122 		return -ENOMEM;
123 
124 	err = br_fdb_hash_init(br);
125 	if (err) {
126 		free_percpu(br->stats);
127 		return err;
128 	}
129 
130 	err = br_mdb_hash_init(br);
131 	if (err) {
132 		free_percpu(br->stats);
133 		br_fdb_hash_fini(br);
134 		return err;
135 	}
136 
137 	err = br_vlan_init(br);
138 	if (err) {
139 		free_percpu(br->stats);
140 		br_mdb_hash_fini(br);
141 		br_fdb_hash_fini(br);
142 		return err;
143 	}
144 
145 	err = br_multicast_init_stats(br);
146 	if (err) {
147 		free_percpu(br->stats);
148 		br_vlan_flush(br);
149 		br_mdb_hash_fini(br);
150 		br_fdb_hash_fini(br);
151 	}
152 
153 	br_set_lockdep_class(dev);
154 	return err;
155 }
156 
157 static void br_dev_uninit(struct net_device *dev)
158 {
159 	struct net_bridge *br = netdev_priv(dev);
160 
161 	br_multicast_dev_del(br);
162 	br_multicast_uninit_stats(br);
163 	br_vlan_flush(br);
164 	br_mdb_hash_fini(br);
165 	br_fdb_hash_fini(br);
166 	free_percpu(br->stats);
167 }
168 
169 static int br_dev_open(struct net_device *dev)
170 {
171 	struct net_bridge *br = netdev_priv(dev);
172 
173 	netdev_update_features(dev);
174 	netif_start_queue(dev);
175 	br_stp_enable_bridge(br);
176 	br_multicast_open(br);
177 
178 	return 0;
179 }
180 
181 static void br_dev_set_multicast_list(struct net_device *dev)
182 {
183 }
184 
185 static void br_dev_change_rx_flags(struct net_device *dev, int change)
186 {
187 	if (change & IFF_PROMISC)
188 		br_manage_promisc(netdev_priv(dev));
189 }
190 
191 static int br_dev_stop(struct net_device *dev)
192 {
193 	struct net_bridge *br = netdev_priv(dev);
194 
195 	br_stp_disable_bridge(br);
196 	br_multicast_stop(br);
197 
198 	netif_stop_queue(dev);
199 
200 	return 0;
201 }
202 
203 static void br_get_stats64(struct net_device *dev,
204 			   struct rtnl_link_stats64 *stats)
205 {
206 	struct net_bridge *br = netdev_priv(dev);
207 	struct pcpu_sw_netstats tmp, sum = { 0 };
208 	unsigned int cpu;
209 
210 	for_each_possible_cpu(cpu) {
211 		unsigned int start;
212 		const struct pcpu_sw_netstats *bstats
213 			= per_cpu_ptr(br->stats, cpu);
214 		do {
215 			start = u64_stats_fetch_begin_irq(&bstats->syncp);
216 			memcpy(&tmp, bstats, sizeof(tmp));
217 		} while (u64_stats_fetch_retry_irq(&bstats->syncp, start));
218 		sum.tx_bytes   += tmp.tx_bytes;
219 		sum.tx_packets += tmp.tx_packets;
220 		sum.rx_bytes   += tmp.rx_bytes;
221 		sum.rx_packets += tmp.rx_packets;
222 	}
223 
224 	stats->tx_bytes   = sum.tx_bytes;
225 	stats->tx_packets = sum.tx_packets;
226 	stats->rx_bytes   = sum.rx_bytes;
227 	stats->rx_packets = sum.rx_packets;
228 }
229 
230 static int br_change_mtu(struct net_device *dev, int new_mtu)
231 {
232 	struct net_bridge *br = netdev_priv(dev);
233 
234 	dev->mtu = new_mtu;
235 
236 	/* this flag will be cleared if the MTU was automatically adjusted */
237 	br_opt_toggle(br, BROPT_MTU_SET_BY_USER, true);
238 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
239 	/* remember the MTU in the rtable for PMTU */
240 	dst_metric_set(&br->fake_rtable.dst, RTAX_MTU, new_mtu);
241 #endif
242 
243 	return 0;
244 }
245 
246 /* Allow setting mac address to any valid ethernet address. */
247 static int br_set_mac_address(struct net_device *dev, void *p)
248 {
249 	struct net_bridge *br = netdev_priv(dev);
250 	struct sockaddr *addr = p;
251 
252 	if (!is_valid_ether_addr(addr->sa_data))
253 		return -EADDRNOTAVAIL;
254 
255 	/* dev_set_mac_addr() can be called by a master device on bridge's
256 	 * NETDEV_UNREGISTER, but since it's being destroyed do nothing
257 	 */
258 	if (dev->reg_state != NETREG_REGISTERED)
259 		return -EBUSY;
260 
261 	spin_lock_bh(&br->lock);
262 	if (!ether_addr_equal(dev->dev_addr, addr->sa_data)) {
263 		/* Mac address will be changed in br_stp_change_bridge_id(). */
264 		br_stp_change_bridge_id(br, addr->sa_data);
265 	}
266 	spin_unlock_bh(&br->lock);
267 
268 	return 0;
269 }
270 
271 static void br_getinfo(struct net_device *dev, struct ethtool_drvinfo *info)
272 {
273 	strlcpy(info->driver, "bridge", sizeof(info->driver));
274 	strlcpy(info->version, BR_VERSION, sizeof(info->version));
275 	strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
276 	strlcpy(info->bus_info, "N/A", sizeof(info->bus_info));
277 }
278 
279 static int br_get_link_ksettings(struct net_device *dev,
280 				 struct ethtool_link_ksettings *cmd)
281 {
282 	struct net_bridge *br = netdev_priv(dev);
283 	struct net_bridge_port *p;
284 
285 	cmd->base.duplex = DUPLEX_UNKNOWN;
286 	cmd->base.port = PORT_OTHER;
287 	cmd->base.speed = SPEED_UNKNOWN;
288 
289 	list_for_each_entry(p, &br->port_list, list) {
290 		struct ethtool_link_ksettings ecmd;
291 		struct net_device *pdev = p->dev;
292 
293 		if (!netif_running(pdev) || !netif_oper_up(pdev))
294 			continue;
295 
296 		if (__ethtool_get_link_ksettings(pdev, &ecmd))
297 			continue;
298 
299 		if (ecmd.base.speed == (__u32)SPEED_UNKNOWN)
300 			continue;
301 
302 		if (cmd->base.speed == (__u32)SPEED_UNKNOWN ||
303 		    cmd->base.speed < ecmd.base.speed)
304 			cmd->base.speed = ecmd.base.speed;
305 	}
306 
307 	return 0;
308 }
309 
310 static netdev_features_t br_fix_features(struct net_device *dev,
311 	netdev_features_t features)
312 {
313 	struct net_bridge *br = netdev_priv(dev);
314 
315 	return br_features_recompute(br, features);
316 }
317 
318 #ifdef CONFIG_NET_POLL_CONTROLLER
319 static void br_poll_controller(struct net_device *br_dev)
320 {
321 }
322 
323 static void br_netpoll_cleanup(struct net_device *dev)
324 {
325 	struct net_bridge *br = netdev_priv(dev);
326 	struct net_bridge_port *p;
327 
328 	list_for_each_entry(p, &br->port_list, list)
329 		br_netpoll_disable(p);
330 }
331 
332 static int __br_netpoll_enable(struct net_bridge_port *p)
333 {
334 	struct netpoll *np;
335 	int err;
336 
337 	np = kzalloc(sizeof(*p->np), GFP_KERNEL);
338 	if (!np)
339 		return -ENOMEM;
340 
341 	err = __netpoll_setup(np, p->dev);
342 	if (err) {
343 		kfree(np);
344 		return err;
345 	}
346 
347 	p->np = np;
348 	return err;
349 }
350 
351 int br_netpoll_enable(struct net_bridge_port *p)
352 {
353 	if (!p->br->dev->npinfo)
354 		return 0;
355 
356 	return __br_netpoll_enable(p);
357 }
358 
359 static int br_netpoll_setup(struct net_device *dev, struct netpoll_info *ni)
360 {
361 	struct net_bridge *br = netdev_priv(dev);
362 	struct net_bridge_port *p;
363 	int err = 0;
364 
365 	list_for_each_entry(p, &br->port_list, list) {
366 		if (!p->dev)
367 			continue;
368 		err = __br_netpoll_enable(p);
369 		if (err)
370 			goto fail;
371 	}
372 
373 out:
374 	return err;
375 
376 fail:
377 	br_netpoll_cleanup(dev);
378 	goto out;
379 }
380 
381 void br_netpoll_disable(struct net_bridge_port *p)
382 {
383 	struct netpoll *np = p->np;
384 
385 	if (!np)
386 		return;
387 
388 	p->np = NULL;
389 
390 	__netpoll_free(np);
391 }
392 
393 #endif
394 
395 static int br_add_slave(struct net_device *dev, struct net_device *slave_dev,
396 			struct netlink_ext_ack *extack)
397 
398 {
399 	struct net_bridge *br = netdev_priv(dev);
400 
401 	return br_add_if(br, slave_dev, extack);
402 }
403 
404 static int br_del_slave(struct net_device *dev, struct net_device *slave_dev)
405 {
406 	struct net_bridge *br = netdev_priv(dev);
407 
408 	return br_del_if(br, slave_dev);
409 }
410 
411 static const struct ethtool_ops br_ethtool_ops = {
412 	.get_drvinfo		 = br_getinfo,
413 	.get_link		 = ethtool_op_get_link,
414 	.get_link_ksettings	 = br_get_link_ksettings,
415 };
416 
417 static const struct net_device_ops br_netdev_ops = {
418 	.ndo_open		 = br_dev_open,
419 	.ndo_stop		 = br_dev_stop,
420 	.ndo_init		 = br_dev_init,
421 	.ndo_uninit		 = br_dev_uninit,
422 	.ndo_start_xmit		 = br_dev_xmit,
423 	.ndo_get_stats64	 = br_get_stats64,
424 	.ndo_set_mac_address	 = br_set_mac_address,
425 	.ndo_set_rx_mode	 = br_dev_set_multicast_list,
426 	.ndo_change_rx_flags	 = br_dev_change_rx_flags,
427 	.ndo_change_mtu		 = br_change_mtu,
428 	.ndo_do_ioctl		 = br_dev_ioctl,
429 #ifdef CONFIG_NET_POLL_CONTROLLER
430 	.ndo_netpoll_setup	 = br_netpoll_setup,
431 	.ndo_netpoll_cleanup	 = br_netpoll_cleanup,
432 	.ndo_poll_controller	 = br_poll_controller,
433 #endif
434 	.ndo_add_slave		 = br_add_slave,
435 	.ndo_del_slave		 = br_del_slave,
436 	.ndo_fix_features        = br_fix_features,
437 	.ndo_fdb_add		 = br_fdb_add,
438 	.ndo_fdb_del		 = br_fdb_delete,
439 	.ndo_fdb_dump		 = br_fdb_dump,
440 	.ndo_fdb_get		 = br_fdb_get,
441 	.ndo_bridge_getlink	 = br_getlink,
442 	.ndo_bridge_setlink	 = br_setlink,
443 	.ndo_bridge_dellink	 = br_dellink,
444 	.ndo_features_check	 = passthru_features_check,
445 };
446 
447 static struct device_type br_type = {
448 	.name	= "bridge",
449 };
450 
451 void br_dev_setup(struct net_device *dev)
452 {
453 	struct net_bridge *br = netdev_priv(dev);
454 
455 	eth_hw_addr_random(dev);
456 	ether_setup(dev);
457 
458 	dev->netdev_ops = &br_netdev_ops;
459 	dev->needs_free_netdev = true;
460 	dev->ethtool_ops = &br_ethtool_ops;
461 	SET_NETDEV_DEVTYPE(dev, &br_type);
462 	dev->priv_flags = IFF_EBRIDGE | IFF_NO_QUEUE;
463 
464 	dev->features = COMMON_FEATURES | NETIF_F_LLTX | NETIF_F_NETNS_LOCAL |
465 			NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
466 	dev->hw_features = COMMON_FEATURES | NETIF_F_HW_VLAN_CTAG_TX |
467 			   NETIF_F_HW_VLAN_STAG_TX;
468 	dev->vlan_features = COMMON_FEATURES;
469 
470 	br->dev = dev;
471 	spin_lock_init(&br->lock);
472 	INIT_LIST_HEAD(&br->port_list);
473 	INIT_HLIST_HEAD(&br->fdb_list);
474 #if IS_ENABLED(CONFIG_BRIDGE_MRP)
475 	INIT_LIST_HEAD(&br->mrp_list);
476 #endif
477 	spin_lock_init(&br->hash_lock);
478 
479 	br->bridge_id.prio[0] = 0x80;
480 	br->bridge_id.prio[1] = 0x00;
481 
482 	ether_addr_copy(br->group_addr, eth_stp_addr);
483 
484 	br->stp_enabled = BR_NO_STP;
485 	br->group_fwd_mask = BR_GROUPFWD_DEFAULT;
486 	br->group_fwd_mask_required = BR_GROUPFWD_DEFAULT;
487 
488 	br->designated_root = br->bridge_id;
489 	br->bridge_max_age = br->max_age = 20 * HZ;
490 	br->bridge_hello_time = br->hello_time = 2 * HZ;
491 	br->bridge_forward_delay = br->forward_delay = 15 * HZ;
492 	br->bridge_ageing_time = br->ageing_time = BR_DEFAULT_AGEING_TIME;
493 	dev->max_mtu = ETH_MAX_MTU;
494 
495 	br_netfilter_rtable_init(br);
496 	br_stp_timer_init(br);
497 	br_multicast_init(br);
498 	INIT_DELAYED_WORK(&br->gc_work, br_fdb_cleanup);
499 }
500