Merge master.kernel.org:/pub/scm/linux/kernel/git/davej/cpufreq
[linux-2.6] / net / bridge / br_if.c
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_device *dev = arg;
83         struct net_bridge_port *p;
84
85         rtnl_lock();
86         p = dev->br_port;
87         if (!p)
88                 goto done;
89
90         if (netif_carrier_ok(p->dev)) {
91                 u32 cost = port_cost(p->dev);
92
93                 spin_lock_bh(&p->br->lock);
94                 if (p->state == BR_STATE_DISABLED) {
95                         p->path_cost = cost;
96                         br_stp_enable_port(p);
97                 }
98                 spin_unlock_bh(&p->br->lock);
99         } else {
100                 spin_lock_bh(&p->br->lock);
101                 if (p->state != BR_STATE_DISABLED)
102                         br_stp_disable_port(p);
103                 spin_unlock_bh(&p->br->lock);
104         }
105 done:
106         rtnl_unlock();
107 }
108
109 static void release_nbp(struct kobject *kobj)
110 {
111         struct net_bridge_port *p
112                 = container_of(kobj, struct net_bridge_port, kobj);
113         kfree(p);
114 }
115
116 static struct kobj_type brport_ktype = {
117 #ifdef CONFIG_SYSFS
118         .sysfs_ops = &brport_sysfs_ops,
119 #endif
120         .release = release_nbp,
121 };
122
123 static void destroy_nbp(struct net_bridge_port *p)
124 {
125         struct net_device *dev = p->dev;
126
127         p->br = NULL;
128         p->dev = NULL;
129         dev_put(dev);
130
131         kobject_put(&p->kobj);
132 }
133
134 static void destroy_nbp_rcu(struct rcu_head *head)
135 {
136         struct net_bridge_port *p =
137                         container_of(head, struct net_bridge_port, rcu);
138         destroy_nbp(p);
139 }
140
141 /* Delete port(interface) from bridge is done in two steps.
142  * via RCU. First step, marks device as down. That deletes
143  * all the timers and stops new packets from flowing through.
144  *
145  * Final cleanup doesn't occur until after all CPU's finished
146  * processing packets.
147  *
148  * Protected from multiple admin operations by RTNL mutex
149  */
150 static void del_nbp(struct net_bridge_port *p)
151 {
152         struct net_bridge *br = p->br;
153         struct net_device *dev = p->dev;
154
155         sysfs_remove_link(&br->ifobj, dev->name);
156
157         dev_set_promiscuity(dev, -1);
158
159         cancel_delayed_work(&p->carrier_check);
160
161         spin_lock_bh(&br->lock);
162         br_stp_disable_port(p);
163         spin_unlock_bh(&br->lock);
164
165         br_fdb_delete_by_port(br, p);
166
167         list_del_rcu(&p->list);
168
169         rcu_assign_pointer(dev->br_port, NULL);
170
171         kobject_del(&p->kobj);
172
173         call_rcu(&p->rcu, destroy_nbp_rcu);
174 }
175
176 /* called with RTNL */
177 static void del_br(struct net_bridge *br)
178 {
179         struct net_bridge_port *p, *n;
180
181         list_for_each_entry_safe(p, n, &br->port_list, list) {
182                 del_nbp(p);
183         }
184
185         del_timer_sync(&br->gc_timer);
186
187         br_sysfs_delbr(br->dev);
188         unregister_netdevice(br->dev);
189 }
190
191 static struct net_device *new_bridge_dev(const char *name)
192 {
193         struct net_bridge *br;
194         struct net_device *dev;
195
196         dev = alloc_netdev(sizeof(struct net_bridge), name,
197                            br_dev_setup);
198         
199         if (!dev)
200                 return NULL;
201
202         br = netdev_priv(dev);
203         br->dev = dev;
204
205         spin_lock_init(&br->lock);
206         INIT_LIST_HEAD(&br->port_list);
207         spin_lock_init(&br->hash_lock);
208
209         br->bridge_id.prio[0] = 0x80;
210         br->bridge_id.prio[1] = 0x00;
211         memset(br->bridge_id.addr, 0, ETH_ALEN);
212
213         br->feature_mask = dev->features;
214         br->stp_enabled = 0;
215         br->designated_root = br->bridge_id;
216         br->root_path_cost = 0;
217         br->root_port = 0;
218         br->bridge_max_age = br->max_age = 20 * HZ;
219         br->bridge_hello_time = br->hello_time = 2 * HZ;
220         br->bridge_forward_delay = br->forward_delay = 15 * HZ;
221         br->topology_change = 0;
222         br->topology_change_detected = 0;
223         br->ageing_time = 300 * HZ;
224         INIT_LIST_HEAD(&br->age_list);
225
226         br_stp_timer_init(br);
227
228         return dev;
229 }
230
231 /* find an available port number */
232 static int find_portno(struct net_bridge *br)
233 {
234         int index;
235         struct net_bridge_port *p;
236         unsigned long *inuse;
237
238         inuse = kmalloc(BITS_TO_LONGS(BR_MAX_PORTS)*sizeof(unsigned long),
239                         GFP_KERNEL);
240         if (!inuse)
241                 return -ENOMEM;
242
243         memset(inuse, 0, BITS_TO_LONGS(BR_MAX_PORTS)*sizeof(unsigned long));
244         set_bit(0, inuse);      /* zero is reserved */
245         list_for_each_entry(p, &br->port_list, list) {
246                 set_bit(p->port_no, inuse);
247         }
248         index = find_first_zero_bit(inuse, BR_MAX_PORTS);
249         kfree(inuse);
250
251         return (index >= BR_MAX_PORTS) ? -EXFULL : index;
252 }
253
254 /* called with RTNL but without bridge lock */
255 static struct net_bridge_port *new_nbp(struct net_bridge *br, 
256                                        struct net_device *dev)
257 {
258         int index;
259         struct net_bridge_port *p;
260         
261         index = find_portno(br);
262         if (index < 0)
263                 return ERR_PTR(index);
264
265         p = kmalloc(sizeof(*p), GFP_KERNEL);
266         if (p == NULL)
267                 return ERR_PTR(-ENOMEM);
268
269         memset(p, 0, sizeof(*p));
270         p->br = br;
271         dev_hold(dev);
272         p->dev = dev;
273         p->path_cost = port_cost(dev);
274         p->priority = 0x8000 >> BR_PORT_BITS;
275         p->port_no = index;
276         br_init_port(p);
277         p->state = BR_STATE_DISABLED;
278         INIT_WORK(&p->carrier_check, port_carrier_check, dev);
279         kobject_init(&p->kobj);
280
281         kobject_set_name(&p->kobj, SYSFS_BRIDGE_PORT_ATTR);
282         p->kobj.ktype = &brport_ktype;
283         p->kobj.parent = &(dev->class_dev.kobj);
284         p->kobj.kset = NULL;
285
286         return p;
287 }
288
289 int br_add_bridge(const char *name)
290 {
291         struct net_device *dev;
292         int ret;
293
294         dev = new_bridge_dev(name);
295         if (!dev) 
296                 return -ENOMEM;
297
298         rtnl_lock();
299         if (strchr(dev->name, '%')) {
300                 ret = dev_alloc_name(dev, dev->name);
301                 if (ret < 0)
302                         goto err1;
303         }
304
305         ret = register_netdevice(dev);
306         if (ret)
307                 goto err2;
308
309         /* network device kobject is not setup until
310          * after rtnl_unlock does it's hotplug magic.
311          * so hold reference to avoid race.
312          */
313         dev_hold(dev);
314         rtnl_unlock();
315
316         ret = br_sysfs_addbr(dev);
317         dev_put(dev);
318
319         if (ret) 
320                 unregister_netdev(dev);
321  out:
322         return ret;
323
324  err2:
325         free_netdev(dev);
326  err1:
327         rtnl_unlock();
328         goto out;
329 }
330
331 int br_del_bridge(const char *name)
332 {
333         struct net_device *dev;
334         int ret = 0;
335
336         rtnl_lock();
337         dev = __dev_get_by_name(name);
338         if (dev == NULL) 
339                 ret =  -ENXIO;  /* Could not find device */
340
341         else if (!(dev->priv_flags & IFF_EBRIDGE)) {
342                 /* Attempt to delete non bridge device! */
343                 ret = -EPERM;
344         }
345
346         else if (dev->flags & IFF_UP) {
347                 /* Not shutdown yet. */
348                 ret = -EBUSY;
349         } 
350
351         else 
352                 del_br(netdev_priv(dev));
353
354         rtnl_unlock();
355         return ret;
356 }
357
358 /* MTU of the bridge pseudo-device: ETH_DATA_LEN or the minimum of the ports */
359 int br_min_mtu(const struct net_bridge *br)
360 {
361         const struct net_bridge_port *p;
362         int mtu = 0;
363
364         ASSERT_RTNL();
365
366         if (list_empty(&br->port_list))
367                 mtu = ETH_DATA_LEN;
368         else {
369                 list_for_each_entry(p, &br->port_list, list) {
370                         if (!mtu  || p->dev->mtu < mtu)
371                                 mtu = p->dev->mtu;
372                 }
373         }
374         return mtu;
375 }
376
377 /*
378  * Recomputes features using slave's features
379  */
380 void br_features_recompute(struct net_bridge *br)
381 {
382         struct net_bridge_port *p;
383         unsigned long features, checksum;
384
385         features = br->feature_mask &~ NETIF_F_IP_CSUM;
386         checksum = br->feature_mask & NETIF_F_IP_CSUM;
387
388         list_for_each_entry(p, &br->port_list, list) {
389                 if (!(p->dev->features 
390                       & (NETIF_F_IP_CSUM|NETIF_F_NO_CSUM|NETIF_F_HW_CSUM)))
391                         checksum = 0;
392                 features &= p->dev->features;
393         }
394
395         br->dev->features = features | checksum | NETIF_F_LLTX;
396 }
397
398 /* called with RTNL */
399 int br_add_if(struct net_bridge *br, struct net_device *dev)
400 {
401         struct net_bridge_port *p;
402         int err = 0;
403
404         if (dev->flags & IFF_LOOPBACK || dev->type != ARPHRD_ETHER)
405                 return -EINVAL;
406
407         if (dev->hard_start_xmit == br_dev_xmit)
408                 return -ELOOP;
409
410         if (dev->br_port != NULL)
411                 return -EBUSY;
412
413         p = new_nbp(br, dev);
414         if (IS_ERR(p))
415                 return PTR_ERR(p);
416
417         err = kobject_add(&p->kobj);
418         if (err)
419                 goto err0;
420
421         err = br_fdb_insert(br, p, dev->dev_addr);
422         if (err)
423                 goto err1;
424
425         err = br_sysfs_addif(p);
426         if (err)
427                 goto err2;
428
429         rcu_assign_pointer(dev->br_port, p);
430         dev_set_promiscuity(dev, 1);
431
432         list_add_rcu(&p->list, &br->port_list);
433
434         spin_lock_bh(&br->lock);
435         br_stp_recalculate_bridge_id(br);
436         br_features_recompute(br);
437         schedule_delayed_work(&p->carrier_check, BR_PORT_DEBOUNCE);
438         spin_unlock_bh(&br->lock);
439
440         dev_set_mtu(br->dev, br_min_mtu(br));
441         kobject_uevent(&p->kobj, KOBJ_ADD);
442
443         return 0;
444 err2:
445         br_fdb_delete_by_port(br, p);
446 err1:
447         kobject_del(&p->kobj);
448 err0:
449         kobject_put(&p->kobj);
450         return err;
451 }
452
453 /* called with RTNL */
454 int br_del_if(struct net_bridge *br, struct net_device *dev)
455 {
456         struct net_bridge_port *p = dev->br_port;
457         
458         if (!p || p->br != br) 
459                 return -EINVAL;
460
461         del_nbp(p);
462
463         spin_lock_bh(&br->lock);
464         br_stp_recalculate_bridge_id(br);
465         br_features_recompute(br);
466         spin_unlock_bh(&br->lock);
467
468         return 0;
469 }
470
471 void __exit br_cleanup_bridges(void)
472 {
473         struct net_device *dev, *nxt;
474
475         rtnl_lock();
476         for (dev = dev_base; dev; dev = nxt) {
477                 nxt = dev->next;
478                 if (dev->priv_flags & IFF_EBRIDGE)
479                         del_br(dev->priv);
480         }
481         rtnl_unlock();
482
483 }