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