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