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