xref: /openbmc/linux/net/bridge/br_if.c (revision 87c2ce3b)
1 /*
2  *	Userspace interface
3  *	Linux ethernet bridge
4  *
5  *	Authors:
6  *	Lennert Buytenhek		<buytenh@gnu.org>
7  *
8  *	$Id: br_if.c,v 1.7 2001/12/24 00:59:55 davem Exp $
9  *
10  *	This program is free software; you can redistribute it and/or
11  *	modify it under the terms of the GNU General Public License
12  *	as published by the Free Software Foundation; either version
13  *	2 of the License, or (at your option) any later version.
14  */
15 
16 #include <linux/kernel.h>
17 #include <linux/netdevice.h>
18 #include <linux/ethtool.h>
19 #include <linux/if_arp.h>
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/rtnetlink.h>
23 #include <linux/if_ether.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  * Need to simulate user ioctl because not all device's that support
33  * ethtool, use ethtool_ops.  Also, since driver might sleep need to
34  * not be holding any locks.
35  */
36 static int port_cost(struct net_device *dev)
37 {
38 	struct ethtool_cmd ecmd = { ETHTOOL_GSET };
39 	struct ifreq ifr;
40 	mm_segment_t old_fs;
41 	int err;
42 
43 	strncpy(ifr.ifr_name, dev->name, IFNAMSIZ);
44 	ifr.ifr_data = (void __user *) &ecmd;
45 
46 	old_fs = get_fs();
47 	set_fs(KERNEL_DS);
48 	err = dev_ethtool(&ifr);
49 	set_fs(old_fs);
50 
51 	if (!err) {
52 		switch(ecmd.speed) {
53 		case SPEED_100:
54 			return 19;
55 		case SPEED_1000:
56 			return 4;
57 		case SPEED_10000:
58 			return 2;
59 		case SPEED_10:
60 			return 100;
61 		}
62 	}
63 
64 	/* Old silly heuristics based on name */
65 	if (!strncmp(dev->name, "lec", 3))
66 		return 7;
67 
68 	if (!strncmp(dev->name, "plip", 4))
69 		return 2500;
70 
71 	return 100;	/* assume old 10Mbps */
72 }
73 
74 
75 /*
76  * Check for port carrier transistions.
77  * Called from work queue to allow for calling functions that
78  * might sleep (such as speed check), and to debounce.
79  */
80 static void port_carrier_check(void *arg)
81 {
82 	struct net_bridge_port *p = arg;
83 
84 	rtnl_lock();
85 	if (netif_carrier_ok(p->dev)) {
86 		u32 cost = port_cost(p->dev);
87 
88 		spin_lock_bh(&p->br->lock);
89 		if (p->state == BR_STATE_DISABLED) {
90 			p->path_cost = cost;
91 			br_stp_enable_port(p);
92 		}
93 		spin_unlock_bh(&p->br->lock);
94 	} else {
95 		spin_lock_bh(&p->br->lock);
96 		if (p->state != BR_STATE_DISABLED)
97 			br_stp_disable_port(p);
98 		spin_unlock_bh(&p->br->lock);
99 	}
100 	rtnl_unlock();
101 }
102 
103 static void destroy_nbp(struct net_bridge_port *p)
104 {
105 	struct net_device *dev = p->dev;
106 
107 	p->br = NULL;
108 	p->dev = NULL;
109 	dev_put(dev);
110 
111 	br_sysfs_freeif(p);
112 }
113 
114 static void destroy_nbp_rcu(struct rcu_head *head)
115 {
116 	struct net_bridge_port *p =
117 			container_of(head, struct net_bridge_port, rcu);
118 	destroy_nbp(p);
119 }
120 
121 /* called with RTNL */
122 static void del_nbp(struct net_bridge_port *p)
123 {
124 	struct net_bridge *br = p->br;
125 	struct net_device *dev = p->dev;
126 
127 	dev->br_port = NULL;
128 	dev_set_promiscuity(dev, -1);
129 
130 	cancel_delayed_work(&p->carrier_check);
131 	flush_scheduled_work();
132 
133 	spin_lock_bh(&br->lock);
134 	br_stp_disable_port(p);
135 	spin_unlock_bh(&br->lock);
136 
137 	br_fdb_delete_by_port(br, p);
138 
139 	list_del_rcu(&p->list);
140 
141 	del_timer_sync(&p->message_age_timer);
142 	del_timer_sync(&p->forward_delay_timer);
143 	del_timer_sync(&p->hold_timer);
144 
145 	call_rcu(&p->rcu, destroy_nbp_rcu);
146 }
147 
148 /* called with RTNL */
149 static void del_br(struct net_bridge *br)
150 {
151 	struct net_bridge_port *p, *n;
152 
153 	list_for_each_entry_safe(p, n, &br->port_list, list) {
154 		br_sysfs_removeif(p);
155 		del_nbp(p);
156 	}
157 
158 	del_timer_sync(&br->gc_timer);
159 
160 	br_sysfs_delbr(br->dev);
161  	unregister_netdevice(br->dev);
162 }
163 
164 static struct net_device *new_bridge_dev(const char *name)
165 {
166 	struct net_bridge *br;
167 	struct net_device *dev;
168 
169 	dev = alloc_netdev(sizeof(struct net_bridge), name,
170 			   br_dev_setup);
171 
172 	if (!dev)
173 		return NULL;
174 
175 	br = netdev_priv(dev);
176 	br->dev = dev;
177 
178 	spin_lock_init(&br->lock);
179 	INIT_LIST_HEAD(&br->port_list);
180 	spin_lock_init(&br->hash_lock);
181 
182 	br->bridge_id.prio[0] = 0x80;
183 	br->bridge_id.prio[1] = 0x00;
184 	memset(br->bridge_id.addr, 0, ETH_ALEN);
185 
186 	br->feature_mask = dev->features;
187 	br->stp_enabled = 0;
188 	br->designated_root = br->bridge_id;
189 	br->root_path_cost = 0;
190 	br->root_port = 0;
191 	br->bridge_max_age = br->max_age = 20 * HZ;
192 	br->bridge_hello_time = br->hello_time = 2 * HZ;
193 	br->bridge_forward_delay = br->forward_delay = 15 * HZ;
194 	br->topology_change = 0;
195 	br->topology_change_detected = 0;
196 	br->ageing_time = 300 * HZ;
197 	INIT_LIST_HEAD(&br->age_list);
198 
199 	br_stp_timer_init(br);
200 
201 	return dev;
202 }
203 
204 /* find an available port number */
205 static int find_portno(struct net_bridge *br)
206 {
207 	int index;
208 	struct net_bridge_port *p;
209 	unsigned long *inuse;
210 
211 	inuse = kmalloc(BITS_TO_LONGS(BR_MAX_PORTS)*sizeof(unsigned long),
212 			GFP_KERNEL);
213 	if (!inuse)
214 		return -ENOMEM;
215 
216 	memset(inuse, 0, BITS_TO_LONGS(BR_MAX_PORTS)*sizeof(unsigned long));
217 	set_bit(0, inuse);	/* zero is reserved */
218 	list_for_each_entry(p, &br->port_list, list) {
219 		set_bit(p->port_no, inuse);
220 	}
221 	index = find_first_zero_bit(inuse, BR_MAX_PORTS);
222 	kfree(inuse);
223 
224 	return (index >= BR_MAX_PORTS) ? -EXFULL : index;
225 }
226 
227 /* called with RTNL but without bridge lock */
228 static struct net_bridge_port *new_nbp(struct net_bridge *br,
229 				       struct net_device *dev)
230 {
231 	int index;
232 	struct net_bridge_port *p;
233 
234 	index = find_portno(br);
235 	if (index < 0)
236 		return ERR_PTR(index);
237 
238 	p = kmalloc(sizeof(*p), GFP_KERNEL);
239 	if (p == NULL)
240 		return ERR_PTR(-ENOMEM);
241 
242 	memset(p, 0, sizeof(*p));
243 	p->br = br;
244 	dev_hold(dev);
245 	p->dev = dev;
246 	p->path_cost = port_cost(dev);
247  	p->priority = 0x8000 >> BR_PORT_BITS;
248 	dev->br_port = p;
249 	p->port_no = index;
250 	br_init_port(p);
251 	p->state = BR_STATE_DISABLED;
252 	INIT_WORK(&p->carrier_check, port_carrier_check, p);
253 	kobject_init(&p->kobj);
254 
255 	return p;
256 }
257 
258 int br_add_bridge(const char *name)
259 {
260 	struct net_device *dev;
261 	int ret;
262 
263 	dev = new_bridge_dev(name);
264 	if (!dev)
265 		return -ENOMEM;
266 
267 	rtnl_lock();
268 	if (strchr(dev->name, '%')) {
269 		ret = dev_alloc_name(dev, dev->name);
270 		if (ret < 0)
271 			goto err1;
272 	}
273 
274 	ret = register_netdevice(dev);
275 	if (ret)
276 		goto err2;
277 
278 	/* network device kobject is not setup until
279 	 * after rtnl_unlock does it's hotplug magic.
280 	 * so hold reference to avoid race.
281 	 */
282 	dev_hold(dev);
283 	rtnl_unlock();
284 
285 	ret = br_sysfs_addbr(dev);
286 	dev_put(dev);
287 
288 	if (ret)
289 		unregister_netdev(dev);
290  out:
291 	return ret;
292 
293  err2:
294 	free_netdev(dev);
295  err1:
296 	rtnl_unlock();
297 	goto out;
298 }
299 
300 int br_del_bridge(const char *name)
301 {
302 	struct net_device *dev;
303 	int ret = 0;
304 
305 	rtnl_lock();
306 	dev = __dev_get_by_name(name);
307 	if (dev == NULL)
308 		ret =  -ENXIO; 	/* Could not find device */
309 
310 	else if (!(dev->priv_flags & IFF_EBRIDGE)) {
311 		/* Attempt to delete non bridge device! */
312 		ret = -EPERM;
313 	}
314 
315 	else if (dev->flags & IFF_UP) {
316 		/* Not shutdown yet. */
317 		ret = -EBUSY;
318 	}
319 
320 	else
321 		del_br(netdev_priv(dev));
322 
323 	rtnl_unlock();
324 	return ret;
325 }
326 
327 /* MTU of the bridge pseudo-device: ETH_DATA_LEN or the minimum of the ports */
328 int br_min_mtu(const struct net_bridge *br)
329 {
330 	const struct net_bridge_port *p;
331 	int mtu = 0;
332 
333 	ASSERT_RTNL();
334 
335 	if (list_empty(&br->port_list))
336 		mtu = ETH_DATA_LEN;
337 	else {
338 		list_for_each_entry(p, &br->port_list, list) {
339 			if (!mtu  || p->dev->mtu < mtu)
340 				mtu = p->dev->mtu;
341 		}
342 	}
343 	return mtu;
344 }
345 
346 /*
347  * Recomputes features using slave's features
348  */
349 void br_features_recompute(struct net_bridge *br)
350 {
351 	struct net_bridge_port *p;
352 	unsigned long features, checksum;
353 
354 	features = br->feature_mask &~ NETIF_F_IP_CSUM;
355 	checksum = br->feature_mask & NETIF_F_IP_CSUM;
356 
357 	list_for_each_entry(p, &br->port_list, list) {
358 		if (!(p->dev->features
359 		      & (NETIF_F_IP_CSUM|NETIF_F_NO_CSUM|NETIF_F_HW_CSUM)))
360 			checksum = 0;
361 		features &= p->dev->features;
362 	}
363 
364 	br->dev->features = features | checksum | NETIF_F_LLTX;
365 }
366 
367 /* called with RTNL */
368 int br_add_if(struct net_bridge *br, struct net_device *dev)
369 {
370 	struct net_bridge_port *p;
371 	int err = 0;
372 
373 	if (dev->flags & IFF_LOOPBACK || dev->type != ARPHRD_ETHER)
374 		return -EINVAL;
375 
376 	if (dev->hard_start_xmit == br_dev_xmit)
377 		return -ELOOP;
378 
379 	if (dev->br_port != NULL)
380 		return -EBUSY;
381 
382 	if (IS_ERR(p = new_nbp(br, dev)))
383 		return PTR_ERR(p);
384 
385  	if ((err = br_fdb_insert(br, p, dev->dev_addr)))
386 		destroy_nbp(p);
387 
388 	else if ((err = br_sysfs_addif(p)))
389 		del_nbp(p);
390 	else {
391 		dev_set_promiscuity(dev, 1);
392 
393 		list_add_rcu(&p->list, &br->port_list);
394 
395 		spin_lock_bh(&br->lock);
396 		br_stp_recalculate_bridge_id(br);
397 		br_features_recompute(br);
398 		if ((br->dev->flags & IFF_UP)
399 		    && (dev->flags & IFF_UP) && netif_carrier_ok(dev))
400 			br_stp_enable_port(p);
401 		spin_unlock_bh(&br->lock);
402 
403 		dev_set_mtu(br->dev, br_min_mtu(br));
404 	}
405 
406 	return err;
407 }
408 
409 /* called with RTNL */
410 int br_del_if(struct net_bridge *br, struct net_device *dev)
411 {
412 	struct net_bridge_port *p = dev->br_port;
413 
414 	if (!p || p->br != br)
415 		return -EINVAL;
416 
417 	br_sysfs_removeif(p);
418 	del_nbp(p);
419 
420 	spin_lock_bh(&br->lock);
421 	br_stp_recalculate_bridge_id(br);
422 	br_features_recompute(br);
423 	spin_unlock_bh(&br->lock);
424 
425 	return 0;
426 }
427 
428 void __exit br_cleanup_bridges(void)
429 {
430 	struct net_device *dev, *nxt;
431 
432 	rtnl_lock();
433 	for (dev = dev_base; dev; dev = nxt) {
434 		nxt = dev->next;
435 		if (dev->priv_flags & IFF_EBRIDGE)
436 			del_br(dev->priv);
437 	}
438 	rtnl_unlock();
439 
440 }
441