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