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