Linux Audio

Check our new training course

Loading...
v6.8
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 *	Userspace interface
  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/etherdevice.h>
 13#include <linux/netpoll.h>
 14#include <linux/ethtool.h>
 15#include <linux/if_arp.h>
 16#include <linux/module.h>
 17#include <linux/init.h>
 18#include <linux/rtnetlink.h>
 19#include <linux/if_ether.h>
 20#include <linux/slab.h>
 21#include <net/dsa.h>
 22#include <net/sock.h>
 23#include <linux/if_vlan.h>
 24#include <net/switchdev.h>
 25#include <net/net_namespace.h>
 26
 27#include "br_private.h"
 28
 29/*
 30 * Determine initial path cost based on speed.
 31 * using recommendations from 802.1d standard
 32 *
 33 * Since driver might sleep need to not be holding any locks.
 34 */
 35static int port_cost(struct net_device *dev)
 36{
 37	struct ethtool_link_ksettings ecmd;
 
 38
 39	if (!__ethtool_get_link_ksettings(dev, &ecmd)) {
 40		switch (ecmd.base.speed) {
 41		case SPEED_10000:
 42			return 2;
 43		case SPEED_5000:
 44			return 3;
 45		case SPEED_2500:
 46			return 4;
 47		case SPEED_1000:
 48			return 5;
 49		case SPEED_100:
 50			return 19;
 51		case SPEED_10:
 52			return 100;
 53		case SPEED_UNKNOWN:
 54			return 100;
 55		default:
 56			if (ecmd.base.speed > SPEED_10000)
 57				return 1;
 58		}
 59	}
 60
 61	/* Old silly heuristics based on name */
 62	if (!strncmp(dev->name, "lec", 3))
 63		return 7;
 64
 65	if (!strncmp(dev->name, "plip", 4))
 66		return 2500;
 67
 68	return 100;	/* assume old 10Mbps */
 69}
 70
 71
 72/* Check for port carrier transitions. */
 73void br_port_carrier_check(struct net_bridge_port *p, bool *notified)
 74{
 75	struct net_device *dev = p->dev;
 76	struct net_bridge *br = p->br;
 77
 78	if (!(p->flags & BR_ADMIN_COST) &&
 79	    netif_running(dev) && netif_oper_up(dev))
 80		p->path_cost = port_cost(dev);
 81
 82	*notified = false;
 83	if (!netif_running(br->dev))
 84		return;
 85
 86	spin_lock_bh(&br->lock);
 87	if (netif_running(dev) && netif_oper_up(dev)) {
 88		if (p->state == BR_STATE_DISABLED) {
 89			br_stp_enable_port(p);
 90			*notified = true;
 91		}
 92	} else {
 93		if (p->state != BR_STATE_DISABLED) {
 94			br_stp_disable_port(p);
 95			*notified = true;
 96		}
 97	}
 98	spin_unlock_bh(&br->lock);
 99}
100
101static void br_port_set_promisc(struct net_bridge_port *p)
102{
103	int err = 0;
104
105	if (br_promisc_port(p))
106		return;
107
108	err = dev_set_promiscuity(p->dev, 1);
109	if (err)
110		return;
111
112	br_fdb_unsync_static(p->br, p);
113	p->flags |= BR_PROMISC;
114}
115
116static void br_port_clear_promisc(struct net_bridge_port *p)
117{
118	int err;
119
120	/* Check if the port is already non-promisc or if it doesn't
121	 * support UNICAST filtering.  Without unicast filtering support
122	 * we'll end up re-enabling promisc mode anyway, so just check for
123	 * it here.
124	 */
125	if (!br_promisc_port(p) || !(p->dev->priv_flags & IFF_UNICAST_FLT))
126		return;
127
128	/* Since we'll be clearing the promisc mode, program the port
129	 * first so that we don't have interruption in traffic.
130	 */
131	err = br_fdb_sync_static(p->br, p);
132	if (err)
133		return;
134
135	dev_set_promiscuity(p->dev, -1);
136	p->flags &= ~BR_PROMISC;
137}
138
139/* When a port is added or removed or when certain port flags
140 * change, this function is called to automatically manage
141 * promiscuity setting of all the bridge ports.  We are always called
142 * under RTNL so can skip using rcu primitives.
143 */
144void br_manage_promisc(struct net_bridge *br)
145{
146	struct net_bridge_port *p;
147	bool set_all = false;
148
149	/* If vlan filtering is disabled or bridge interface is placed
150	 * into promiscuous mode, place all ports in promiscuous mode.
151	 */
152	if ((br->dev->flags & IFF_PROMISC) || !br_vlan_enabled(br->dev))
153		set_all = true;
154
155	list_for_each_entry(p, &br->port_list, list) {
156		if (set_all) {
157			br_port_set_promisc(p);
158		} else {
159			/* If the number of auto-ports is <= 1, then all other
160			 * ports will have their output configuration
161			 * statically specified through fdbs.  Since ingress
162			 * on the auto-port becomes forwarding/egress to other
163			 * ports and egress configuration is statically known,
164			 * we can say that ingress configuration of the
165			 * auto-port is also statically known.
166			 * This lets us disable promiscuous mode and write
167			 * this config to hw.
168			 */
169			if ((p->dev->priv_flags & IFF_UNICAST_FLT) &&
170			    (br->auto_cnt == 0 ||
171			     (br->auto_cnt == 1 && br_auto_port(p))))
172				br_port_clear_promisc(p);
173			else
174				br_port_set_promisc(p);
175		}
176	}
177}
178
179int nbp_backup_change(struct net_bridge_port *p,
180		      struct net_device *backup_dev)
181{
182	struct net_bridge_port *old_backup = rtnl_dereference(p->backup_port);
183	struct net_bridge_port *backup_p = NULL;
184
185	ASSERT_RTNL();
186
187	if (backup_dev) {
188		if (!netif_is_bridge_port(backup_dev))
189			return -ENOENT;
190
191		backup_p = br_port_get_rtnl(backup_dev);
192		if (backup_p->br != p->br)
193			return -EINVAL;
194	}
195
196	if (p == backup_p)
197		return -EINVAL;
198
199	if (old_backup == backup_p)
200		return 0;
201
202	/* if the backup link is already set, clear it */
203	if (old_backup)
204		old_backup->backup_redirected_cnt--;
205
206	if (backup_p)
207		backup_p->backup_redirected_cnt++;
208	rcu_assign_pointer(p->backup_port, backup_p);
209
210	return 0;
211}
212
213static void nbp_backup_clear(struct net_bridge_port *p)
214{
215	nbp_backup_change(p, NULL);
216	if (p->backup_redirected_cnt) {
217		struct net_bridge_port *cur_p;
218
219		list_for_each_entry(cur_p, &p->br->port_list, list) {
220			struct net_bridge_port *backup_p;
221
222			backup_p = rtnl_dereference(cur_p->backup_port);
223			if (backup_p == p)
224				nbp_backup_change(cur_p, NULL);
225		}
226	}
227
228	WARN_ON(rcu_access_pointer(p->backup_port) || p->backup_redirected_cnt);
229}
230
231static void nbp_update_port_count(struct net_bridge *br)
232{
233	struct net_bridge_port *p;
234	u32 cnt = 0;
235
236	list_for_each_entry(p, &br->port_list, list) {
237		if (br_auto_port(p))
238			cnt++;
239	}
240	if (br->auto_cnt != cnt) {
241		br->auto_cnt = cnt;
242		br_manage_promisc(br);
243	}
244}
245
246static void nbp_delete_promisc(struct net_bridge_port *p)
247{
248	/* If port is currently promiscuous, unset promiscuity.
249	 * Otherwise, it is a static port so remove all addresses
250	 * from it.
251	 */
252	dev_set_allmulti(p->dev, -1);
253	if (br_promisc_port(p))
254		dev_set_promiscuity(p->dev, -1);
255	else
256		br_fdb_unsync_static(p->br, p);
257}
258
259static void release_nbp(struct kobject *kobj)
260{
261	struct net_bridge_port *p
262		= container_of(kobj, struct net_bridge_port, kobj);
263	kfree(p);
264}
265
266static void brport_get_ownership(const struct kobject *kobj, kuid_t *uid, kgid_t *gid)
267{
268	struct net_bridge_port *p = kobj_to_brport(kobj);
269
270	net_ns_get_ownership(dev_net(p->dev), uid, gid);
271}
272
273static const struct kobj_type brport_ktype = {
274#ifdef CONFIG_SYSFS
275	.sysfs_ops = &brport_sysfs_ops,
276#endif
277	.release = release_nbp,
278	.get_ownership = brport_get_ownership,
279};
280
281static void destroy_nbp(struct net_bridge_port *p)
282{
283	struct net_device *dev = p->dev;
284
285	p->br = NULL;
286	p->dev = NULL;
287	netdev_put(dev, &p->dev_tracker);
288
289	kobject_put(&p->kobj);
290}
291
292static void destroy_nbp_rcu(struct rcu_head *head)
293{
294	struct net_bridge_port *p =
295			container_of(head, struct net_bridge_port, rcu);
296	destroy_nbp(p);
297}
298
299static unsigned get_max_headroom(struct net_bridge *br)
300{
301	unsigned max_headroom = 0;
302	struct net_bridge_port *p;
303
304	list_for_each_entry(p, &br->port_list, list) {
305		unsigned dev_headroom = netdev_get_fwd_headroom(p->dev);
306
307		if (dev_headroom > max_headroom)
308			max_headroom = dev_headroom;
309	}
310
311	return max_headroom;
312}
313
314static void update_headroom(struct net_bridge *br, int new_hr)
315{
316	struct net_bridge_port *p;
317
318	list_for_each_entry(p, &br->port_list, list)
319		netdev_set_rx_headroom(p->dev, new_hr);
320
321	br->dev->needed_headroom = new_hr;
322}
323
324/* Delete port(interface) from bridge is done in two steps.
325 * via RCU. First step, marks device as down. That deletes
326 * all the timers and stops new packets from flowing through.
327 *
328 * Final cleanup doesn't occur until after all CPU's finished
329 * processing packets.
330 *
331 * Protected from multiple admin operations by RTNL mutex
332 */
333static void del_nbp(struct net_bridge_port *p)
334{
335	struct net_bridge *br = p->br;
336	struct net_device *dev = p->dev;
337
338	sysfs_remove_link(br->ifobj, p->dev->name);
339
340	nbp_delete_promisc(p);
341
342	spin_lock_bh(&br->lock);
343	br_stp_disable_port(p);
344	spin_unlock_bh(&br->lock);
345
346	br_mrp_port_del(br, p);
347	br_cfm_port_del(br, p);
348
349	br_ifinfo_notify(RTM_DELLINK, NULL, p);
350
351	list_del_rcu(&p->list);
352	if (netdev_get_fwd_headroom(dev) == br->dev->needed_headroom)
353		update_headroom(br, get_max_headroom(br));
354	netdev_reset_rx_headroom(dev);
355
356	nbp_vlan_flush(p);
357	br_fdb_delete_by_port(br, p, 0, 1);
358	switchdev_deferred_process();
359	nbp_backup_clear(p);
360
361	nbp_update_port_count(br);
362
363	netdev_upper_dev_unlink(dev, br->dev);
364
365	dev->priv_flags &= ~IFF_BRIDGE_PORT;
366
367	netdev_rx_handler_unregister(dev);
 
 
 
368
369	br_multicast_del_port(p);
370
371	kobject_uevent(&p->kobj, KOBJ_REMOVE);
372	kobject_del(&p->kobj);
373
374	br_netpoll_disable(p);
375
376	call_rcu(&p->rcu, destroy_nbp_rcu);
377}
378
379/* Delete bridge device */
380void br_dev_delete(struct net_device *dev, struct list_head *head)
381{
382	struct net_bridge *br = netdev_priv(dev);
383	struct net_bridge_port *p, *n;
384
385	list_for_each_entry_safe(p, n, &br->port_list, list) {
386		del_nbp(p);
387	}
388
389	br_recalculate_neigh_suppress_enabled(br);
390
391	br_fdb_delete_by_port(br, NULL, 0, 1);
392
393	cancel_delayed_work_sync(&br->gc_work);
394
395	br_sysfs_delbr(br->dev);
396	unregister_netdevice_queue(br->dev, head);
397}
398
399/* find an available port number */
400static int find_portno(struct net_bridge *br)
401{
402	int index;
403	struct net_bridge_port *p;
404	unsigned long *inuse;
405
406	inuse = bitmap_zalloc(BR_MAX_PORTS, GFP_KERNEL);
 
407	if (!inuse)
408		return -ENOMEM;
409
410	__set_bit(0, inuse);	/* zero is reserved */
411	list_for_each_entry(p, &br->port_list, list)
412		__set_bit(p->port_no, inuse);
413
414	index = find_first_zero_bit(inuse, BR_MAX_PORTS);
415	bitmap_free(inuse);
416
417	return (index >= BR_MAX_PORTS) ? -EXFULL : index;
418}
419
420/* called with RTNL but without bridge lock */
421static struct net_bridge_port *new_nbp(struct net_bridge *br,
422				       struct net_device *dev)
423{
 
424	struct net_bridge_port *p;
425	int index, err;
426
427	index = find_portno(br);
428	if (index < 0)
429		return ERR_PTR(index);
430
431	p = kzalloc(sizeof(*p), GFP_KERNEL);
432	if (p == NULL)
433		return ERR_PTR(-ENOMEM);
434
435	p->br = br;
436	netdev_hold(dev, &p->dev_tracker, GFP_KERNEL);
437	p->dev = dev;
438	p->path_cost = port_cost(dev);
439	p->priority = 0x8000 >> BR_PORT_BITS;
440	p->port_no = index;
441	p->flags = BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD | BR_BCAST_FLOOD;
442	br_init_port(p);
443	br_set_state(p, BR_STATE_DISABLED);
444	br_stp_port_timer_init(p);
445	err = br_multicast_add_port(p);
446	if (err) {
447		netdev_put(dev, &p->dev_tracker);
448		kfree(p);
449		p = ERR_PTR(err);
450	}
451
452	return p;
453}
454
455int br_add_bridge(struct net *net, const char *name)
456{
457	struct net_device *dev;
458	int res;
459
460	dev = alloc_netdev(sizeof(struct net_bridge), name, NET_NAME_UNKNOWN,
461			   br_dev_setup);
462
463	if (!dev)
464		return -ENOMEM;
465
466	dev_net_set(dev, net);
467	dev->rtnl_link_ops = &br_link_ops;
468
469	res = register_netdevice(dev);
470	if (res)
471		free_netdev(dev);
472	return res;
473}
474
475int br_del_bridge(struct net *net, const char *name)
476{
477	struct net_device *dev;
478	int ret = 0;
479
 
480	dev = __dev_get_by_name(net, name);
481	if (dev == NULL)
482		ret =  -ENXIO; 	/* Could not find device */
483
484	else if (!netif_is_bridge_master(dev)) {
485		/* Attempt to delete non bridge device! */
486		ret = -EPERM;
487	}
488
489	else if (dev->flags & IFF_UP) {
490		/* Not shutdown yet. */
491		ret = -EBUSY;
492	}
493
494	else
495		br_dev_delete(dev, NULL);
496
 
497	return ret;
498}
499
500/* MTU of the bridge pseudo-device: ETH_DATA_LEN or the minimum of the ports */
501static int br_mtu_min(const struct net_bridge *br)
502{
503	const struct net_bridge_port *p;
504	int ret_mtu = 0;
505
506	list_for_each_entry(p, &br->port_list, list)
507		if (!ret_mtu || ret_mtu > p->dev->mtu)
508			ret_mtu = p->dev->mtu;
509
510	return ret_mtu ? ret_mtu : ETH_DATA_LEN;
511}
512
513void br_mtu_auto_adjust(struct net_bridge *br)
514{
515	ASSERT_RTNL();
516
517	/* if the bridge MTU was manually configured don't mess with it */
518	if (br_opt_get(br, BROPT_MTU_SET_BY_USER))
519		return;
520
521	/* change to the minimum MTU and clear the flag which was set by
522	 * the bridge ndo_change_mtu callback
523	 */
524	dev_set_mtu(br->dev, br_mtu_min(br));
525	br_opt_toggle(br, BROPT_MTU_SET_BY_USER, false);
526}
527
528static void br_set_gso_limits(struct net_bridge *br)
529{
530	unsigned int tso_max_size = TSO_MAX_SIZE;
531	const struct net_bridge_port *p;
532	u16 tso_max_segs = TSO_MAX_SEGS;
533
534	list_for_each_entry(p, &br->port_list, list) {
535		tso_max_size = min(tso_max_size, p->dev->tso_max_size);
536		tso_max_segs = min(tso_max_segs, p->dev->tso_max_segs);
537	}
538	netif_set_tso_max_size(br->dev, tso_max_size);
539	netif_set_tso_max_segs(br->dev, tso_max_segs);
540}
541
542/*
543 * Recomputes features using slave's features
544 */
545netdev_features_t br_features_recompute(struct net_bridge *br,
546	netdev_features_t features)
547{
548	struct net_bridge_port *p;
549	netdev_features_t mask;
550
551	if (list_empty(&br->port_list))
552		return features;
553
554	mask = features;
555	features &= ~NETIF_F_ONE_FOR_ALL;
556
557	list_for_each_entry(p, &br->port_list, list) {
558		features = netdev_increment_features(features,
559						     p->dev->features, mask);
560	}
561	features = netdev_add_tso_features(features, mask);
562
563	return features;
564}
565
566/* called with RTNL */
567int br_add_if(struct net_bridge *br, struct net_device *dev,
568	      struct netlink_ext_ack *extack)
569{
570	struct net_bridge_port *p;
571	int err = 0;
572	unsigned br_hr, dev_hr;
573	bool changed_addr, fdb_synced = false;
574
575	/* Don't allow bridging non-ethernet like devices. */
576	if ((dev->flags & IFF_LOOPBACK) ||
577	    dev->type != ARPHRD_ETHER || dev->addr_len != ETH_ALEN ||
578	    !is_valid_ether_addr(dev->dev_addr))
579		return -EINVAL;
580
581	/* No bridging of bridges */
582	if (dev->netdev_ops->ndo_start_xmit == br_dev_xmit) {
583		NL_SET_ERR_MSG(extack,
584			       "Can not enslave a bridge to a bridge");
585		return -ELOOP;
586	}
587
588	/* Device has master upper dev */
589	if (netdev_master_upper_dev_get(dev))
590		return -EBUSY;
591
592	/* No bridging devices that dislike that (e.g. wireless) */
593	if (dev->priv_flags & IFF_DONT_BRIDGE) {
594		NL_SET_ERR_MSG(extack,
595			       "Device does not allow enslaving to a bridge");
596		return -EOPNOTSUPP;
597	}
598
599	p = new_nbp(br, dev);
600	if (IS_ERR(p))
601		return PTR_ERR(p);
602
603	call_netdevice_notifiers(NETDEV_JOIN, dev);
604
605	err = dev_set_allmulti(dev, 1);
606	if (err) {
607		br_multicast_del_port(p);
608		netdev_put(dev, &p->dev_tracker);
609		kfree(p);	/* kobject not yet init'd, manually free */
610		goto err1;
611	}
612
613	err = kobject_init_and_add(&p->kobj, &brport_ktype, &(dev->dev.kobj),
614				   SYSFS_BRIDGE_PORT_ATTR);
615	if (err)
616		goto err2;
 
 
 
 
617
618	err = br_sysfs_addif(p);
619	if (err)
620		goto err2;
621
622	err = br_netpoll_enable(p);
 
 
 
623	if (err)
624		goto err3;
625
626	err = netdev_rx_handler_register(dev, br_get_rx_handler(dev), p);
627	if (err)
628		goto err4;
629
630	dev->priv_flags |= IFF_BRIDGE_PORT;
631
632	err = netdev_master_upper_dev_link(dev, br->dev, NULL, NULL, extack);
633	if (err)
634		goto err5;
635
636	dev_disable_lro(dev);
637
638	list_add_rcu(&p->list, &br->port_list);
639
640	nbp_update_port_count(br);
641	if (!br_promisc_port(p) && (p->dev->priv_flags & IFF_UNICAST_FLT)) {
642		/* When updating the port count we also update all ports'
643		 * promiscuous mode.
644		 * A port leaving promiscuous mode normally gets the bridge's
645		 * fdb synced to the unicast filter (if supported), however,
646		 * `br_port_clear_promisc` does not distinguish between
647		 * non-promiscuous ports and *new* ports, so we need to
648		 * sync explicitly here.
649		 */
650		fdb_synced = br_fdb_sync_static(br, p) == 0;
651		if (!fdb_synced)
652			netdev_err(dev, "failed to sync bridge static fdb addresses to this port\n");
653	}
654
655	netdev_update_features(br->dev);
656
657	br_hr = br->dev->needed_headroom;
658	dev_hr = netdev_get_fwd_headroom(dev);
659	if (br_hr < dev_hr)
660		update_headroom(br, dev_hr);
661	else
662		netdev_set_rx_headroom(dev, br_hr);
663
664	if (br_fdb_add_local(br, p, dev->dev_addr, 0))
665		netdev_err(dev, "failed insert local address bridge forwarding table\n");
666
667	if (br->dev->addr_assign_type != NET_ADDR_SET) {
668		/* Ask for permission to use this MAC address now, even if we
669		 * don't end up choosing it below.
670		 */
671		err = dev_pre_changeaddr_notify(br->dev, dev->dev_addr, extack);
672		if (err)
673			goto err6;
674	}
675
676	err = nbp_vlan_init(p, extack);
677	if (err) {
678		netdev_err(dev, "failed to initialize vlan filtering on this port\n");
679		goto err6;
680	}
681
682	spin_lock_bh(&br->lock);
683	changed_addr = br_stp_recalculate_bridge_id(br);
684
685	if (netif_running(dev) && netif_oper_up(dev) &&
686	    (br->dev->flags & IFF_UP))
687		br_stp_enable_port(p);
688	spin_unlock_bh(&br->lock);
689
690	br_ifinfo_notify(RTM_NEWLINK, NULL, p);
691
692	if (changed_addr)
693		call_netdevice_notifiers(NETDEV_CHANGEADDR, br->dev);
694
695	br_mtu_auto_adjust(br);
696	br_set_gso_limits(br);
697
698	kobject_uevent(&p->kobj, KOBJ_ADD);
699
700	return 0;
701
702err6:
703	if (fdb_synced)
704		br_fdb_unsync_static(br, p);
705	list_del_rcu(&p->list);
706	br_fdb_delete_by_port(br, p, 0, 1);
707	nbp_update_port_count(br);
708	netdev_upper_dev_unlink(dev, br->dev);
709err5:
710	dev->priv_flags &= ~IFF_BRIDGE_PORT;
711	netdev_rx_handler_unregister(dev);
712err4:
713	br_netpoll_disable(p);
714err3:
715	sysfs_remove_link(br->ifobj, p->dev->name);
716err2:
717	br_multicast_del_port(p);
718	netdev_put(dev, &p->dev_tracker);
719	kobject_put(&p->kobj);
720	dev_set_allmulti(dev, -1);
721err1:
 
 
 
 
 
 
 
722	return err;
723}
724
725/* called with RTNL */
726int br_del_if(struct net_bridge *br, struct net_device *dev)
727{
728	struct net_bridge_port *p;
729	bool changed_addr;
730
731	p = br_port_get_rtnl(dev);
732	if (!p || p->br != br)
733		return -EINVAL;
734
735	/* Since more than one interface can be attached to a bridge,
736	 * there still maybe an alternate path for netconsole to use;
737	 * therefore there is no reason for a NETDEV_RELEASE event.
738	 */
739	del_nbp(p);
740
741	br_mtu_auto_adjust(br);
742	br_set_gso_limits(br);
743
744	spin_lock_bh(&br->lock);
745	changed_addr = br_stp_recalculate_bridge_id(br);
746	spin_unlock_bh(&br->lock);
747
748	if (changed_addr)
749		call_netdevice_notifiers(NETDEV_CHANGEADDR, br->dev);
750
751	netdev_update_features(br->dev);
752
753	return 0;
754}
755
756void br_port_flags_change(struct net_bridge_port *p, unsigned long mask)
757{
758	struct net_bridge *br = p->br;
759
760	if (mask & BR_AUTO_MASK)
761		nbp_update_port_count(br);
762
763	if (mask & (BR_NEIGH_SUPPRESS | BR_NEIGH_VLAN_SUPPRESS))
764		br_recalculate_neigh_suppress_enabled(br);
765}
766
767bool br_port_flag_is_set(const struct net_device *dev, unsigned long flag)
768{
769	struct net_bridge_port *p;
 
770
771	p = br_port_get_rtnl_rcu(dev);
772	if (!p)
773		return false;
774
775	return p->flags & flag;
776}
777EXPORT_SYMBOL_GPL(br_port_flag_is_set);
v3.1
 
  1/*
  2 *	Userspace interface
  3 *	Linux ethernet bridge
  4 *
  5 *	Authors:
  6 *	Lennert Buytenhek		<buytenh@gnu.org>
  7 *
  8 *	This program is free software; you can redistribute it and/or
  9 *	modify it under the terms of the GNU General Public License
 10 *	as published by the Free Software Foundation; either version
 11 *	2 of the License, or (at your option) any later version.
 12 */
 13
 14#include <linux/kernel.h>
 15#include <linux/netdevice.h>
 
 16#include <linux/netpoll.h>
 17#include <linux/ethtool.h>
 18#include <linux/if_arp.h>
 19#include <linux/module.h>
 20#include <linux/init.h>
 21#include <linux/rtnetlink.h>
 22#include <linux/if_ether.h>
 23#include <linux/slab.h>
 
 24#include <net/sock.h>
 
 
 
 25
 26#include "br_private.h"
 27
 28/*
 29 * Determine initial path cost based on speed.
 30 * using recommendations from 802.1d standard
 31 *
 32 * Since driver might sleep need to not be holding any locks.
 33 */
 34static int port_cost(struct net_device *dev)
 35{
 36	if (dev->ethtool_ops && dev->ethtool_ops->get_settings) {
 37		struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET, };
 38
 39		if (!dev_ethtool_get_settings(dev, &ecmd)) {
 40			switch (ethtool_cmd_speed(&ecmd)) {
 41			case SPEED_10000:
 42				return 2;
 43			case SPEED_1000:
 44				return 4;
 45			case SPEED_100:
 46				return 19;
 47			case SPEED_10:
 48				return 100;
 49			}
 
 
 
 
 
 
 
 
 50		}
 51	}
 52
 53	/* Old silly heuristics based on name */
 54	if (!strncmp(dev->name, "lec", 3))
 55		return 7;
 56
 57	if (!strncmp(dev->name, "plip", 4))
 58		return 2500;
 59
 60	return 100;	/* assume old 10Mbps */
 61}
 62
 63
 64/* Check for port carrier transistions. */
 65void br_port_carrier_check(struct net_bridge_port *p)
 66{
 67	struct net_device *dev = p->dev;
 68	struct net_bridge *br = p->br;
 69
 70	if (netif_running(dev) && netif_carrier_ok(dev))
 
 71		p->path_cost = port_cost(dev);
 72
 
 73	if (!netif_running(br->dev))
 74		return;
 75
 76	spin_lock_bh(&br->lock);
 77	if (netif_running(dev) && netif_carrier_ok(dev)) {
 78		if (p->state == BR_STATE_DISABLED)
 79			br_stp_enable_port(p);
 
 
 80	} else {
 81		if (p->state != BR_STATE_DISABLED)
 82			br_stp_disable_port(p);
 
 
 83	}
 84	spin_unlock_bh(&br->lock);
 85}
 86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 87static void release_nbp(struct kobject *kobj)
 88{
 89	struct net_bridge_port *p
 90		= container_of(kobj, struct net_bridge_port, kobj);
 91	kfree(p);
 92}
 93
 94static struct kobj_type brport_ktype = {
 
 
 
 
 
 
 
 95#ifdef CONFIG_SYSFS
 96	.sysfs_ops = &brport_sysfs_ops,
 97#endif
 98	.release = release_nbp,
 
 99};
100
101static void destroy_nbp(struct net_bridge_port *p)
102{
103	struct net_device *dev = p->dev;
104
105	p->br = NULL;
106	p->dev = NULL;
107	dev_put(dev);
108
109	kobject_put(&p->kobj);
110}
111
112static void destroy_nbp_rcu(struct rcu_head *head)
113{
114	struct net_bridge_port *p =
115			container_of(head, struct net_bridge_port, rcu);
116	destroy_nbp(p);
117}
118
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119/* Delete port(interface) from bridge is done in two steps.
120 * via RCU. First step, marks device as down. That deletes
121 * all the timers and stops new packets from flowing through.
122 *
123 * Final cleanup doesn't occur until after all CPU's finished
124 * processing packets.
125 *
126 * Protected from multiple admin operations by RTNL mutex
127 */
128static void del_nbp(struct net_bridge_port *p)
129{
130	struct net_bridge *br = p->br;
131	struct net_device *dev = p->dev;
132
133	sysfs_remove_link(br->ifobj, p->dev->name);
134
135	dev_set_promiscuity(dev, -1);
136
137	spin_lock_bh(&br->lock);
138	br_stp_disable_port(p);
139	spin_unlock_bh(&br->lock);
140
141	br_ifinfo_notify(RTM_DELLINK, p);
 
142
143	br_fdb_delete_by_port(br, p, 1);
144
145	list_del_rcu(&p->list);
 
 
 
 
 
 
 
 
 
 
 
 
146
147	dev->priv_flags &= ~IFF_BRIDGE_PORT;
148
149	netdev_rx_handler_unregister(dev);
150	synchronize_net();
151
152	netdev_set_master(dev, NULL);
153
154	br_multicast_del_port(p);
155
156	kobject_uevent(&p->kobj, KOBJ_REMOVE);
157	kobject_del(&p->kobj);
158
159	br_netpoll_disable(p);
160
161	call_rcu(&p->rcu, destroy_nbp_rcu);
162}
163
164/* Delete bridge device */
165void br_dev_delete(struct net_device *dev, struct list_head *head)
166{
167	struct net_bridge *br = netdev_priv(dev);
168	struct net_bridge_port *p, *n;
169
170	list_for_each_entry_safe(p, n, &br->port_list, list) {
171		del_nbp(p);
172	}
173
174	del_timer_sync(&br->gc_timer);
 
 
 
 
175
176	br_sysfs_delbr(br->dev);
177	unregister_netdevice_queue(br->dev, head);
178}
179
180/* find an available port number */
181static int find_portno(struct net_bridge *br)
182{
183	int index;
184	struct net_bridge_port *p;
185	unsigned long *inuse;
186
187	inuse = kcalloc(BITS_TO_LONGS(BR_MAX_PORTS), sizeof(unsigned long),
188			GFP_KERNEL);
189	if (!inuse)
190		return -ENOMEM;
191
192	set_bit(0, inuse);	/* zero is reserved */
193	list_for_each_entry(p, &br->port_list, list) {
194		set_bit(p->port_no, inuse);
195	}
196	index = find_first_zero_bit(inuse, BR_MAX_PORTS);
197	kfree(inuse);
198
199	return (index >= BR_MAX_PORTS) ? -EXFULL : index;
200}
201
202/* called with RTNL but without bridge lock */
203static struct net_bridge_port *new_nbp(struct net_bridge *br,
204				       struct net_device *dev)
205{
206	int index;
207	struct net_bridge_port *p;
 
208
209	index = find_portno(br);
210	if (index < 0)
211		return ERR_PTR(index);
212
213	p = kzalloc(sizeof(*p), GFP_KERNEL);
214	if (p == NULL)
215		return ERR_PTR(-ENOMEM);
216
217	p->br = br;
218	dev_hold(dev);
219	p->dev = dev;
220	p->path_cost = port_cost(dev);
221	p->priority = 0x8000 >> BR_PORT_BITS;
222	p->port_no = index;
223	p->flags = 0;
224	br_init_port(p);
225	p->state = BR_STATE_DISABLED;
226	br_stp_port_timer_init(p);
227	br_multicast_add_port(p);
 
 
 
 
 
228
229	return p;
230}
231
232int br_add_bridge(struct net *net, const char *name)
233{
234	struct net_device *dev;
235	int res;
236
237	dev = alloc_netdev(sizeof(struct net_bridge), name,
238			   br_dev_setup);
239
240	if (!dev)
241		return -ENOMEM;
242
243	dev_net_set(dev, net);
 
244
245	res = register_netdev(dev);
246	if (res)
247		free_netdev(dev);
248	return res;
249}
250
251int br_del_bridge(struct net *net, const char *name)
252{
253	struct net_device *dev;
254	int ret = 0;
255
256	rtnl_lock();
257	dev = __dev_get_by_name(net, name);
258	if (dev == NULL)
259		ret =  -ENXIO; 	/* Could not find device */
260
261	else if (!(dev->priv_flags & IFF_EBRIDGE)) {
262		/* Attempt to delete non bridge device! */
263		ret = -EPERM;
264	}
265
266	else if (dev->flags & IFF_UP) {
267		/* Not shutdown yet. */
268		ret = -EBUSY;
269	}
270
271	else
272		br_dev_delete(dev, NULL);
273
274	rtnl_unlock();
275	return ret;
276}
277
278/* MTU of the bridge pseudo-device: ETH_DATA_LEN or the minimum of the ports */
279int br_min_mtu(const struct net_bridge *br)
280{
281	const struct net_bridge_port *p;
282	int mtu = 0;
 
 
 
 
283
 
 
 
 
 
284	ASSERT_RTNL();
285
286	if (list_empty(&br->port_list))
287		mtu = ETH_DATA_LEN;
288	else {
289		list_for_each_entry(p, &br->port_list, list) {
290			if (!mtu  || p->dev->mtu < mtu)
291				mtu = p->dev->mtu;
292		}
 
 
 
 
 
 
 
 
 
 
 
 
 
293	}
294	return mtu;
 
295}
296
297/*
298 * Recomputes features using slave's features
299 */
300u32 br_features_recompute(struct net_bridge *br, u32 features)
 
301{
302	struct net_bridge_port *p;
303	u32 mask;
304
305	if (list_empty(&br->port_list))
306		return features;
307
308	mask = features;
309	features &= ~NETIF_F_ONE_FOR_ALL;
310
311	list_for_each_entry(p, &br->port_list, list) {
312		features = netdev_increment_features(features,
313						     p->dev->features, mask);
314	}
 
315
316	return features;
317}
318
319/* called with RTNL */
320int br_add_if(struct net_bridge *br, struct net_device *dev)
 
321{
322	struct net_bridge_port *p;
323	int err = 0;
324	bool changed_addr;
 
325
326	/* Don't allow bridging non-ethernet like devices */
327	if ((dev->flags & IFF_LOOPBACK) ||
328	    dev->type != ARPHRD_ETHER || dev->addr_len != ETH_ALEN)
 
329		return -EINVAL;
330
331	/* No bridging of bridges */
332	if (dev->netdev_ops->ndo_start_xmit == br_dev_xmit)
 
 
333		return -ELOOP;
 
334
335	/* Device is already being bridged */
336	if (br_port_exists(dev))
337		return -EBUSY;
338
339	/* No bridging devices that dislike that (e.g. wireless) */
340	if (dev->priv_flags & IFF_DONT_BRIDGE)
 
 
341		return -EOPNOTSUPP;
 
342
343	p = new_nbp(br, dev);
344	if (IS_ERR(p))
345		return PTR_ERR(p);
346
347	call_netdevice_notifiers(NETDEV_JOIN, dev);
348
349	err = dev_set_promiscuity(dev, 1);
350	if (err)
351		goto put_back;
 
 
 
 
352
353	err = kobject_init_and_add(&p->kobj, &brport_ktype, &(dev->dev.kobj),
354				   SYSFS_BRIDGE_PORT_ATTR);
355	if (err)
356		goto err0;
357
358	err = br_fdb_insert(br, p, dev->dev_addr);
359	if (err)
360		goto err1;
361
362	err = br_sysfs_addif(p);
363	if (err)
364		goto err2;
365
366	if (br_netpoll_info(br) && ((err = br_netpoll_enable(p))))
367		goto err3;
368
369	err = netdev_set_master(dev, br->dev);
370	if (err)
371		goto err3;
372
373	err = netdev_rx_handler_register(dev, br_handle_frame, p);
374	if (err)
375		goto err4;
376
377	dev->priv_flags |= IFF_BRIDGE_PORT;
378
 
 
 
 
379	dev_disable_lro(dev);
380
381	list_add_rcu(&p->list, &br->port_list);
382
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
383	netdev_update_features(br->dev);
384
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
385	spin_lock_bh(&br->lock);
386	changed_addr = br_stp_recalculate_bridge_id(br);
387
388	if ((dev->flags & IFF_UP) && netif_carrier_ok(dev) &&
389	    (br->dev->flags & IFF_UP))
390		br_stp_enable_port(p);
391	spin_unlock_bh(&br->lock);
392
393	br_ifinfo_notify(RTM_NEWLINK, p);
394
395	if (changed_addr)
396		call_netdevice_notifiers(NETDEV_CHANGEADDR, br->dev);
397
398	dev_set_mtu(br->dev, br_min_mtu(br));
 
399
400	kobject_uevent(&p->kobj, KOBJ_ADD);
401
402	return 0;
403
 
 
 
 
 
 
 
 
 
 
404err4:
405	netdev_set_master(dev, NULL);
406err3:
407	sysfs_remove_link(br->ifobj, p->dev->name);
408err2:
409	br_fdb_delete_by_port(br, p, 1);
 
 
 
410err1:
411	kobject_put(&p->kobj);
412	p = NULL; /* kobject_put frees */
413err0:
414	dev_set_promiscuity(dev, -1);
415put_back:
416	dev_put(dev);
417	kfree(p);
418	return err;
419}
420
421/* called with RTNL */
422int br_del_if(struct net_bridge *br, struct net_device *dev)
423{
424	struct net_bridge_port *p;
425	bool changed_addr;
426
427	p = br_port_get_rtnl(dev);
428	if (!p || p->br != br)
429		return -EINVAL;
430
 
 
 
 
431	del_nbp(p);
432
 
 
 
433	spin_lock_bh(&br->lock);
434	changed_addr = br_stp_recalculate_bridge_id(br);
435	spin_unlock_bh(&br->lock);
436
437	if (changed_addr)
438		call_netdevice_notifiers(NETDEV_CHANGEADDR, br->dev);
439
440	netdev_update_features(br->dev);
441
442	return 0;
443}
444
445void __net_exit br_net_exit(struct net *net)
446{
447	struct net_device *dev;
448	LIST_HEAD(list);
 
 
 
 
 
 
449
450	rtnl_lock();
451	for_each_netdev(net, dev)
452		if (dev->priv_flags & IFF_EBRIDGE)
453			br_dev_delete(dev, &list);
454
455	unregister_netdevice_many(&list);
456	rtnl_unlock();
 
457
 
458}