Merge branch 'for-linus' of git://brick.kernel.dk/data/git/linux-2.6-block
[linux-2.6/kvm.git] / net / bridge / br_if.c
blobba442883e877fbda4808db858f215ac9c30334df
1 /*
2 * Userspace interface
3 * Linux ethernet bridge
5 * Authors:
6 * Lennert Buytenhek <buytenh@gnu.org>
8 * $Id: br_if.c,v 1.7 2001/12/24 00:59:55 davem Exp $
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.
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>
26 #include "br_private.h"
29 * Determine initial path cost based on speed.
30 * using recommendations from 802.1d standard
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.
36 static int port_cost(struct net_device *dev)
38 struct ethtool_cmd ecmd = { ETHTOOL_GSET };
39 struct ifreq ifr;
40 mm_segment_t old_fs;
41 int err;
43 strncpy(ifr.ifr_name, dev->name, IFNAMSIZ);
44 ifr.ifr_data = (void __user *) &ecmd;
46 old_fs = get_fs();
47 set_fs(KERNEL_DS);
48 err = dev_ethtool(&ifr);
49 set_fs(old_fs);
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;
64 /* Old silly heuristics based on name */
65 if (!strncmp(dev->name, "lec", 3))
66 return 7;
68 if (!strncmp(dev->name, "plip", 4))
69 return 2500;
71 return 100; /* assume old 10Mbps */
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.
80 static void port_carrier_check(void *arg)
82 struct net_bridge_port *p = arg;
84 rtnl_lock();
85 if (netif_carrier_ok(p->dev)) {
86 u32 cost = port_cost(p->dev);
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);
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);
100 rtnl_unlock();
103 static void destroy_nbp(struct net_bridge_port *p)
105 struct net_device *dev = p->dev;
107 p->br = NULL;
108 p->dev = NULL;
109 dev_put(dev);
111 br_sysfs_freeif(p);
114 static void destroy_nbp_rcu(struct rcu_head *head)
116 struct net_bridge_port *p =
117 container_of(head, struct net_bridge_port, rcu);
118 destroy_nbp(p);
121 /* called with RTNL */
122 static void del_nbp(struct net_bridge_port *p)
124 struct net_bridge *br = p->br;
125 struct net_device *dev = p->dev;
127 dev->br_port = NULL;
128 dev_set_promiscuity(dev, -1);
130 cancel_delayed_work(&p->carrier_check);
131 flush_scheduled_work();
133 spin_lock_bh(&br->lock);
134 br_stp_disable_port(p);
135 spin_unlock_bh(&br->lock);
137 br_fdb_delete_by_port(br, p);
139 list_del_rcu(&p->list);
141 del_timer_sync(&p->message_age_timer);
142 del_timer_sync(&p->forward_delay_timer);
143 del_timer_sync(&p->hold_timer);
145 call_rcu(&p->rcu, destroy_nbp_rcu);
148 /* called with RTNL */
149 static void del_br(struct net_bridge *br)
151 struct net_bridge_port *p, *n;
153 list_for_each_entry_safe(p, n, &br->port_list, list) {
154 br_sysfs_removeif(p);
155 del_nbp(p);
158 del_timer_sync(&br->gc_timer);
160 br_sysfs_delbr(br->dev);
161 unregister_netdevice(br->dev);
164 static struct net_device *new_bridge_dev(const char *name)
166 struct net_bridge *br;
167 struct net_device *dev;
169 dev = alloc_netdev(sizeof(struct net_bridge), name,
170 br_dev_setup);
172 if (!dev)
173 return NULL;
175 br = netdev_priv(dev);
176 br->dev = dev;
178 spin_lock_init(&br->lock);
179 INIT_LIST_HEAD(&br->port_list);
180 spin_lock_init(&br->hash_lock);
182 br->bridge_id.prio[0] = 0x80;
183 br->bridge_id.prio[1] = 0x00;
184 memset(br->bridge_id.addr, 0, ETH_ALEN);
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);
199 br_stp_timer_init(br);
201 return dev;
204 /* find an available port number */
205 static int find_portno(struct net_bridge *br)
207 int index;
208 struct net_bridge_port *p;
209 unsigned long *inuse;
211 inuse = kmalloc(BITS_TO_LONGS(BR_MAX_PORTS)*sizeof(unsigned long),
212 GFP_KERNEL);
213 if (!inuse)
214 return -ENOMEM;
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);
221 index = find_first_zero_bit(inuse, BR_MAX_PORTS);
222 kfree(inuse);
224 return (index >= BR_MAX_PORTS) ? -EXFULL : index;
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)
231 int index;
232 struct net_bridge_port *p;
234 index = find_portno(br);
235 if (index < 0)
236 return ERR_PTR(index);
238 p = kmalloc(sizeof(*p), GFP_KERNEL);
239 if (p == NULL)
240 return ERR_PTR(-ENOMEM);
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);
255 return p;
258 int br_add_bridge(const char *name)
260 struct net_device *dev;
261 int ret;
263 dev = new_bridge_dev(name);
264 if (!dev)
265 return -ENOMEM;
267 rtnl_lock();
268 if (strchr(dev->name, '%')) {
269 ret = dev_alloc_name(dev, dev->name);
270 if (ret < 0)
271 goto err1;
274 ret = register_netdevice(dev);
275 if (ret)
276 goto err2;
278 /* network device kobject is not setup until
279 * after rtnl_unlock does it's hotplug magic.
280 * so hold reference to avoid race.
282 dev_hold(dev);
283 rtnl_unlock();
285 ret = br_sysfs_addbr(dev);
286 dev_put(dev);
288 if (ret)
289 unregister_netdev(dev);
290 out:
291 return ret;
293 err2:
294 free_netdev(dev);
295 err1:
296 rtnl_unlock();
297 goto out;
300 int br_del_bridge(const char *name)
302 struct net_device *dev;
303 int ret = 0;
305 rtnl_lock();
306 dev = __dev_get_by_name(name);
307 if (dev == NULL)
308 ret = -ENXIO; /* Could not find device */
310 else if (!(dev->priv_flags & IFF_EBRIDGE)) {
311 /* Attempt to delete non bridge device! */
312 ret = -EPERM;
315 else if (dev->flags & IFF_UP) {
316 /* Not shutdown yet. */
317 ret = -EBUSY;
320 else
321 del_br(netdev_priv(dev));
323 rtnl_unlock();
324 return ret;
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)
330 const struct net_bridge_port *p;
331 int mtu = 0;
333 ASSERT_RTNL();
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;
343 return mtu;
347 * Recomputes features using slave's features
349 void br_features_recompute(struct net_bridge *br)
351 struct net_bridge_port *p;
352 unsigned long features, checksum;
354 features = br->feature_mask &~ NETIF_F_IP_CSUM;
355 checksum = br->feature_mask & NETIF_F_IP_CSUM;
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;
364 br->dev->features = features | checksum | NETIF_F_LLTX;
367 /* called with RTNL */
368 int br_add_if(struct net_bridge *br, struct net_device *dev)
370 struct net_bridge_port *p;
371 int err = 0;
373 if (dev->flags & IFF_LOOPBACK || dev->type != ARPHRD_ETHER)
374 return -EINVAL;
376 if (dev->hard_start_xmit == br_dev_xmit)
377 return -ELOOP;
379 if (dev->br_port != NULL)
380 return -EBUSY;
382 if (IS_ERR(p = new_nbp(br, dev)))
383 return PTR_ERR(p);
385 if ((err = br_fdb_insert(br, p, dev->dev_addr)))
386 destroy_nbp(p);
388 else if ((err = br_sysfs_addif(p)))
389 del_nbp(p);
390 else {
391 dev_set_promiscuity(dev, 1);
393 list_add_rcu(&p->list, &br->port_list);
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);
403 dev_set_mtu(br->dev, br_min_mtu(br));
406 return err;
409 /* called with RTNL */
410 int br_del_if(struct net_bridge *br, struct net_device *dev)
412 struct net_bridge_port *p = dev->br_port;
414 if (!p || p->br != br)
415 return -EINVAL;
417 br_sysfs_removeif(p);
418 del_nbp(p);
420 spin_lock_bh(&br->lock);
421 br_stp_recalculate_bridge_id(br);
422 br_features_recompute(br);
423 spin_unlock_bh(&br->lock);
425 return 0;
428 void __exit br_cleanup_bridges(void)
430 struct net_device *dev, *nxt;
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);
438 rtnl_unlock();