[CONNECTOR]: Update documentation to match reality.
[linux-2.6/verdex.git] / net / bridge / br_if.c
blobdefcf6a8607c3714f54dbb225b471570837dc630
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 <net/sock.h>
25 #include "br_private.h"
28 * Determine initial path cost based on speed.
29 * using recommendations from 802.1d standard
31 * Need to simulate user ioctl because not all device's that support
32 * ethtool, use ethtool_ops. Also, since driver might sleep need to
33 * not be holding any locks.
35 static int br_initial_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;
61 default:
62 pr_info("bridge: can't decode speed from %s: %d\n",
63 dev->name, ecmd.speed);
64 return 100;
68 /* Old silly heuristics based on name */
69 if (!strncmp(dev->name, "lec", 3))
70 return 7;
72 if (!strncmp(dev->name, "plip", 4))
73 return 2500;
75 return 100; /* assume old 10Mbps */
78 static void destroy_nbp(struct net_bridge_port *p)
80 struct net_device *dev = p->dev;
82 p->br = NULL;
83 p->dev = NULL;
84 dev_put(dev);
86 br_sysfs_freeif(p);
89 static void destroy_nbp_rcu(struct rcu_head *head)
91 struct net_bridge_port *p =
92 container_of(head, struct net_bridge_port, rcu);
93 destroy_nbp(p);
96 /* called with RTNL */
97 static void del_nbp(struct net_bridge_port *p)
99 struct net_bridge *br = p->br;
100 struct net_device *dev = p->dev;
102 dev->br_port = NULL;
103 dev_set_promiscuity(dev, -1);
105 spin_lock_bh(&br->lock);
106 br_stp_disable_port(p);
107 spin_unlock_bh(&br->lock);
109 br_fdb_delete_by_port(br, p);
111 list_del_rcu(&p->list);
113 del_timer_sync(&p->message_age_timer);
114 del_timer_sync(&p->forward_delay_timer);
115 del_timer_sync(&p->hold_timer);
117 call_rcu(&p->rcu, destroy_nbp_rcu);
120 /* called with RTNL */
121 static void del_br(struct net_bridge *br)
123 struct net_bridge_port *p, *n;
125 list_for_each_entry_safe(p, n, &br->port_list, list) {
126 br_sysfs_removeif(p);
127 del_nbp(p);
130 del_timer_sync(&br->gc_timer);
132 br_sysfs_delbr(br->dev);
133 unregister_netdevice(br->dev);
136 static struct net_device *new_bridge_dev(const char *name)
138 struct net_bridge *br;
139 struct net_device *dev;
141 dev = alloc_netdev(sizeof(struct net_bridge), name,
142 br_dev_setup);
144 if (!dev)
145 return NULL;
147 br = netdev_priv(dev);
148 br->dev = dev;
150 spin_lock_init(&br->lock);
151 INIT_LIST_HEAD(&br->port_list);
152 spin_lock_init(&br->hash_lock);
154 br->bridge_id.prio[0] = 0x80;
155 br->bridge_id.prio[1] = 0x00;
156 memset(br->bridge_id.addr, 0, ETH_ALEN);
158 br->stp_enabled = 0;
159 br->designated_root = br->bridge_id;
160 br->root_path_cost = 0;
161 br->root_port = 0;
162 br->bridge_max_age = br->max_age = 20 * HZ;
163 br->bridge_hello_time = br->hello_time = 2 * HZ;
164 br->bridge_forward_delay = br->forward_delay = 15 * HZ;
165 br->topology_change = 0;
166 br->topology_change_detected = 0;
167 br->ageing_time = 300 * HZ;
168 INIT_LIST_HEAD(&br->age_list);
170 br_stp_timer_init(br);
172 return dev;
175 /* find an available port number */
176 static int find_portno(struct net_bridge *br)
178 int index;
179 struct net_bridge_port *p;
180 unsigned long *inuse;
182 inuse = kmalloc(BITS_TO_LONGS(BR_MAX_PORTS)*sizeof(unsigned long),
183 GFP_KERNEL);
184 if (!inuse)
185 return -ENOMEM;
187 memset(inuse, 0, BITS_TO_LONGS(BR_MAX_PORTS)*sizeof(unsigned long));
188 set_bit(0, inuse); /* zero is reserved */
189 list_for_each_entry(p, &br->port_list, list) {
190 set_bit(p->port_no, inuse);
192 index = find_first_zero_bit(inuse, BR_MAX_PORTS);
193 kfree(inuse);
195 return (index >= BR_MAX_PORTS) ? -EXFULL : index;
198 /* called with RTNL */
199 static struct net_bridge_port *new_nbp(struct net_bridge *br,
200 struct net_device *dev,
201 unsigned long cost)
203 int index;
204 struct net_bridge_port *p;
206 index = find_portno(br);
207 if (index < 0)
208 return ERR_PTR(index);
210 p = kmalloc(sizeof(*p), GFP_KERNEL);
211 if (p == NULL)
212 return ERR_PTR(-ENOMEM);
214 memset(p, 0, sizeof(*p));
215 p->br = br;
216 dev_hold(dev);
217 p->dev = dev;
218 p->path_cost = cost;
219 p->priority = 0x8000 >> BR_PORT_BITS;
220 dev->br_port = p;
221 p->port_no = index;
222 br_init_port(p);
223 p->state = BR_STATE_DISABLED;
224 kobject_init(&p->kobj);
226 return p;
229 int br_add_bridge(const char *name)
231 struct net_device *dev;
232 int ret;
234 dev = new_bridge_dev(name);
235 if (!dev)
236 return -ENOMEM;
238 rtnl_lock();
239 if (strchr(dev->name, '%')) {
240 ret = dev_alloc_name(dev, dev->name);
241 if (ret < 0)
242 goto err1;
245 ret = register_netdevice(dev);
246 if (ret)
247 goto err2;
249 /* network device kobject is not setup until
250 * after rtnl_unlock does it's hotplug magic.
251 * so hold reference to avoid race.
253 dev_hold(dev);
254 rtnl_unlock();
256 ret = br_sysfs_addbr(dev);
257 dev_put(dev);
259 if (ret)
260 unregister_netdev(dev);
261 out:
262 return ret;
264 err2:
265 free_netdev(dev);
266 err1:
267 rtnl_unlock();
268 goto out;
271 int br_del_bridge(const char *name)
273 struct net_device *dev;
274 int ret = 0;
276 rtnl_lock();
277 dev = __dev_get_by_name(name);
278 if (dev == NULL)
279 ret = -ENXIO; /* Could not find device */
281 else if (!(dev->priv_flags & IFF_EBRIDGE)) {
282 /* Attempt to delete non bridge device! */
283 ret = -EPERM;
286 else if (dev->flags & IFF_UP) {
287 /* Not shutdown yet. */
288 ret = -EBUSY;
291 else
292 del_br(netdev_priv(dev));
294 rtnl_unlock();
295 return ret;
298 /* Mtu of the bridge pseudo-device 1500 or the minimum of the ports */
299 int br_min_mtu(const struct net_bridge *br)
301 const struct net_bridge_port *p;
302 int mtu = 0;
304 ASSERT_RTNL();
306 if (list_empty(&br->port_list))
307 mtu = 1500;
308 else {
309 list_for_each_entry(p, &br->port_list, list) {
310 if (!mtu || p->dev->mtu < mtu)
311 mtu = p->dev->mtu;
314 return mtu;
318 * Recomputes features using slave's features
320 void br_features_recompute(struct net_bridge *br)
322 struct net_bridge_port *p;
323 unsigned long features, checksum;
325 features = NETIF_F_SG | NETIF_F_FRAGLIST
326 | NETIF_F_HIGHDMA | NETIF_F_TSO;
327 checksum = NETIF_F_IP_CSUM; /* least commmon subset */
329 list_for_each_entry(p, &br->port_list, list) {
330 if (!(p->dev->features
331 & (NETIF_F_IP_CSUM|NETIF_F_NO_CSUM|NETIF_F_HW_CSUM)))
332 checksum = 0;
333 features &= p->dev->features;
336 br->dev->features = features | checksum | NETIF_F_LLTX;
339 /* called with RTNL */
340 int br_add_if(struct net_bridge *br, struct net_device *dev)
342 struct net_bridge_port *p;
343 int err = 0;
345 if (dev->flags & IFF_LOOPBACK || dev->type != ARPHRD_ETHER)
346 return -EINVAL;
348 if (dev->hard_start_xmit == br_dev_xmit)
349 return -ELOOP;
351 if (dev->br_port != NULL)
352 return -EBUSY;
354 if (IS_ERR(p = new_nbp(br, dev, br_initial_port_cost(dev))))
355 return PTR_ERR(p);
357 if ((err = br_fdb_insert(br, p, dev->dev_addr)))
358 destroy_nbp(p);
360 else if ((err = br_sysfs_addif(p)))
361 del_nbp(p);
362 else {
363 dev_set_promiscuity(dev, 1);
365 list_add_rcu(&p->list, &br->port_list);
367 spin_lock_bh(&br->lock);
368 br_stp_recalculate_bridge_id(br);
369 if ((br->dev->flags & IFF_UP)
370 && (dev->flags & IFF_UP) && netif_carrier_ok(dev))
371 br_stp_enable_port(p);
372 spin_unlock_bh(&br->lock);
374 dev_set_mtu(br->dev, br_min_mtu(br));
377 return err;
380 /* called with RTNL */
381 int br_del_if(struct net_bridge *br, struct net_device *dev)
383 struct net_bridge_port *p = dev->br_port;
385 if (!p || p->br != br)
386 return -EINVAL;
388 br_sysfs_removeif(p);
389 del_nbp(p);
391 spin_lock_bh(&br->lock);
392 br_stp_recalculate_bridge_id(br);
393 br_features_recompute(br);
394 spin_unlock_bh(&br->lock);
396 return 0;
399 void __exit br_cleanup_bridges(void)
401 struct net_device *dev, *nxt;
403 rtnl_lock();
404 for (dev = dev_base; dev; dev = nxt) {
405 nxt = dev->next;
406 if (dev->priv_flags & IFF_EBRIDGE)
407 del_br(dev->priv);
409 rtnl_unlock();