[ATM]: avoid race conditions related to atm_devs list
[linux-2.6] / net / atm / resources.c
1 /* net/atm/resources.c - Statically allocated resources */
2
3 /* Written 1995-2000 by Werner Almesberger, EPFL LRC/ICA */
4
5 /* Fixes
6  * Arnaldo Carvalho de Melo <acme@conectiva.com.br>
7  * 2002/01 - don't free the whole struct sock on sk->destruct time,
8  *           use the default destruct function initialized by sock_init_data */
9
10
11 #include <linux/config.h>
12 #include <linux/ctype.h>
13 #include <linux/string.h>
14 #include <linux/atmdev.h>
15 #include <linux/sonet.h>
16 #include <linux/kernel.h> /* for barrier */
17 #include <linux/module.h>
18 #include <linux/bitops.h>
19 #include <linux/delay.h>
20 #include <net/sock.h>    /* for struct sock */
21
22 #include "common.h"
23 #include "resources.h"
24 #include "addr.h"
25
26
27 LIST_HEAD(atm_devs);
28 DECLARE_MUTEX(atm_dev_mutex);
29
30 static struct atm_dev *__alloc_atm_dev(const char *type)
31 {
32         struct atm_dev *dev;
33
34         dev = kmalloc(sizeof(*dev), GFP_KERNEL);
35         if (!dev)
36                 return NULL;
37         memset(dev, 0, sizeof(*dev));
38         dev->type = type;
39         dev->signal = ATM_PHY_SIG_UNKNOWN;
40         dev->link_rate = ATM_OC3_PCR;
41         spin_lock_init(&dev->lock);
42         INIT_LIST_HEAD(&dev->local);
43         INIT_LIST_HEAD(&dev->lecs);
44
45         return dev;
46 }
47
48 static struct atm_dev *__atm_dev_lookup(int number)
49 {
50         struct atm_dev *dev;
51         struct list_head *p;
52
53         list_for_each(p, &atm_devs) {
54                 dev = list_entry(p, struct atm_dev, dev_list);
55                 if (dev->number == number) {
56                         atm_dev_hold(dev);
57                         return dev;
58                 }
59         }
60         return NULL;
61 }
62
63 struct atm_dev *atm_dev_lookup(int number)
64 {
65         struct atm_dev *dev;
66
67         down(&atm_dev_mutex);
68         dev = __atm_dev_lookup(number);
69         up(&atm_dev_mutex);
70         return dev;
71 }
72
73 struct atm_dev *atm_dev_register(const char *type, const struct atmdev_ops *ops,
74                                  int number, unsigned long *flags)
75 {
76         struct atm_dev *dev, *inuse;
77
78         dev = __alloc_atm_dev(type);
79         if (!dev) {
80                 printk(KERN_ERR "atm_dev_register: no space for dev %s\n",
81                     type);
82                 return NULL;
83         }
84         down(&atm_dev_mutex);
85         if (number != -1) {
86                 if ((inuse = __atm_dev_lookup(number))) {
87                         atm_dev_put(inuse);
88                         up(&atm_dev_mutex);
89                         kfree(dev);
90                         return NULL;
91                 }
92                 dev->number = number;
93         } else {
94                 dev->number = 0;
95                 while ((inuse = __atm_dev_lookup(dev->number))) {
96                         atm_dev_put(inuse);
97                         dev->number++;
98                 }
99         }
100
101         dev->ops = ops;
102         if (flags)
103                 dev->flags = *flags;
104         else
105                 memset(&dev->flags, 0, sizeof(dev->flags));
106         memset(&dev->stats, 0, sizeof(dev->stats));
107         atomic_set(&dev->refcnt, 1);
108
109         if (atm_proc_dev_register(dev) < 0) {
110                 printk(KERN_ERR "atm_dev_register: "
111                        "atm_proc_dev_register failed for dev %s\n",
112                        type);
113                 up(&atm_dev_mutex);
114                 kfree(dev);
115                 return NULL;
116         }
117         list_add_tail(&dev->dev_list, &atm_devs);
118         up(&atm_dev_mutex);
119
120         return dev;
121 }
122
123
124 void atm_dev_deregister(struct atm_dev *dev)
125 {
126         unsigned long warning_time;
127
128         atm_proc_dev_deregister(dev);
129
130         down(&atm_dev_mutex);
131         list_del(&dev->dev_list);
132         up(&atm_dev_mutex);
133
134         warning_time = jiffies;
135         while (atomic_read(&dev->refcnt) != 1) {
136                 msleep(250);
137                 if ((jiffies - warning_time) > 10 * HZ) {
138                         printk(KERN_EMERG "atm_dev_deregister: waiting for "
139                                "dev %d to become free. Usage count = %d\n",
140                                dev->number, atomic_read(&dev->refcnt));
141                         warning_time = jiffies;
142                 }
143         }
144
145         kfree(dev);
146 }
147
148 void shutdown_atm_dev(struct atm_dev *dev)
149 {
150         if (atomic_read(&dev->refcnt) > 1) {
151                 set_bit(ATM_DF_CLOSE, &dev->flags);
152                 return;
153         }
154         if (dev->ops->dev_close)
155                 dev->ops->dev_close(dev);
156         atm_dev_deregister(dev);
157 }
158
159
160 static void copy_aal_stats(struct k_atm_aal_stats *from,
161     struct atm_aal_stats *to)
162 {
163 #define __HANDLE_ITEM(i) to->i = atomic_read(&from->i)
164         __AAL_STAT_ITEMS
165 #undef __HANDLE_ITEM
166 }
167
168
169 static void subtract_aal_stats(struct k_atm_aal_stats *from,
170     struct atm_aal_stats *to)
171 {
172 #define __HANDLE_ITEM(i) atomic_sub(to->i, &from->i)
173         __AAL_STAT_ITEMS
174 #undef __HANDLE_ITEM
175 }
176
177
178 static int fetch_stats(struct atm_dev *dev, struct atm_dev_stats __user *arg, int zero)
179 {
180         struct atm_dev_stats tmp;
181         int error = 0;
182
183         copy_aal_stats(&dev->stats.aal0, &tmp.aal0);
184         copy_aal_stats(&dev->stats.aal34, &tmp.aal34);
185         copy_aal_stats(&dev->stats.aal5, &tmp.aal5);
186         if (arg)
187                 error = copy_to_user(arg, &tmp, sizeof(tmp));
188         if (zero && !error) {
189                 subtract_aal_stats(&dev->stats.aal0, &tmp.aal0);
190                 subtract_aal_stats(&dev->stats.aal34, &tmp.aal34);
191                 subtract_aal_stats(&dev->stats.aal5, &tmp.aal5);
192         }
193         return error ? -EFAULT : 0;
194 }
195
196
197 int atm_dev_ioctl(unsigned int cmd, void __user *arg)
198 {
199         void __user *buf;
200         int error, len, number, size = 0;
201         struct atm_dev *dev;
202         struct list_head *p;
203         int *tmp_buf, *tmp_p;
204         struct atm_iobuf __user *iobuf = arg;
205         struct atmif_sioc __user *sioc = arg;
206         switch (cmd) {
207                 case ATM_GETNAMES:
208                         if (get_user(buf, &iobuf->buffer))
209                                 return -EFAULT;
210                         if (get_user(len, &iobuf->length))
211                                 return -EFAULT;
212                         down(&atm_dev_mutex);
213                         list_for_each(p, &atm_devs)
214                                 size += sizeof(int);
215                         if (size > len) {
216                                 up(&atm_dev_mutex);
217                                 return -E2BIG;
218                         }
219                         tmp_buf = kmalloc(size, GFP_ATOMIC);
220                         if (!tmp_buf) {
221                                 up(&atm_dev_mutex);
222                                 return -ENOMEM;
223                         }
224                         tmp_p = tmp_buf;
225                         list_for_each(p, &atm_devs) {
226                                 dev = list_entry(p, struct atm_dev, dev_list);
227                                 *tmp_p++ = dev->number;
228                         }
229                         up(&atm_dev_mutex);
230                         error = ((copy_to_user(buf, tmp_buf, size)) ||
231                                         put_user(size, &iobuf->length))
232                                                 ? -EFAULT : 0;
233                         kfree(tmp_buf);
234                         return error;
235                 default:
236                         break;
237         }
238
239         if (get_user(buf, &sioc->arg))
240                 return -EFAULT;
241         if (get_user(len, &sioc->length))
242                 return -EFAULT;
243         if (get_user(number, &sioc->number))
244                 return -EFAULT;
245
246         if (!(dev = try_then_request_module(atm_dev_lookup(number),
247                                             "atm-device-%d", number)))
248                 return -ENODEV;
249         
250         switch (cmd) {
251                 case ATM_GETTYPE:
252                         size = strlen(dev->type) + 1;
253                         if (copy_to_user(buf, dev->type, size)) {
254                                 error = -EFAULT;
255                                 goto done;
256                         }
257                         break;
258                 case ATM_GETESI:
259                         size = ESI_LEN;
260                         if (copy_to_user(buf, dev->esi, size)) {
261                                 error = -EFAULT;
262                                 goto done;
263                         }
264                         break;
265                 case ATM_SETESI:
266                         {
267                                 int i;
268
269                                 for (i = 0; i < ESI_LEN; i++)
270                                         if (dev->esi[i]) {
271                                                 error = -EEXIST;
272                                                 goto done;
273                                         }
274                         }
275                         /* fall through */
276                 case ATM_SETESIF:
277                         {
278                                 unsigned char esi[ESI_LEN];
279
280                                 if (!capable(CAP_NET_ADMIN)) {
281                                         error = -EPERM;
282                                         goto done;
283                                 }
284                                 if (copy_from_user(esi, buf, ESI_LEN)) {
285                                         error = -EFAULT;
286                                         goto done;
287                                 }
288                                 memcpy(dev->esi, esi, ESI_LEN);
289                                 error =  ESI_LEN;
290                                 goto done;
291                         }
292                 case ATM_GETSTATZ:
293                         if (!capable(CAP_NET_ADMIN)) {
294                                 error = -EPERM;
295                                 goto done;
296                         }
297                         /* fall through */
298                 case ATM_GETSTAT:
299                         size = sizeof(struct atm_dev_stats);
300                         error = fetch_stats(dev, buf, cmd == ATM_GETSTATZ);
301                         if (error)
302                                 goto done;
303                         break;
304                 case ATM_GETCIRANGE:
305                         size = sizeof(struct atm_cirange);
306                         if (copy_to_user(buf, &dev->ci_range, size)) {
307                                 error = -EFAULT;
308                                 goto done;
309                         }
310                         break;
311                 case ATM_GETLINKRATE:
312                         size = sizeof(int);
313                         if (copy_to_user(buf, &dev->link_rate, size)) {
314                                 error = -EFAULT;
315                                 goto done;
316                         }
317                         break;
318                 case ATM_RSTADDR:
319                         if (!capable(CAP_NET_ADMIN)) {
320                                 error = -EPERM;
321                                 goto done;
322                         }
323                         atm_reset_addr(dev, ATM_ADDR_LOCAL);
324                         break;
325                 case ATM_ADDADDR:
326                 case ATM_DELADDR:
327                 case ATM_ADDLECSADDR:
328                 case ATM_DELLECSADDR:
329                         if (!capable(CAP_NET_ADMIN)) {
330                                 error = -EPERM;
331                                 goto done;
332                         }
333                         {
334                                 struct sockaddr_atmsvc addr;
335
336                                 if (copy_from_user(&addr, buf, sizeof(addr))) {
337                                         error = -EFAULT;
338                                         goto done;
339                                 }
340                                 if (cmd == ATM_ADDADDR || cmd == ATM_ADDLECSADDR)
341                                         error = atm_add_addr(dev, &addr,
342                                                              (cmd == ATM_ADDADDR ?
343                                                               ATM_ADDR_LOCAL : ATM_ADDR_LECS));
344                                 else
345                                         error = atm_del_addr(dev, &addr,
346                                                              (cmd == ATM_DELADDR ?
347                                                               ATM_ADDR_LOCAL : ATM_ADDR_LECS));
348                                 goto done;
349                         }
350                 case ATM_GETADDR:
351                 case ATM_GETLECSADDR:
352                         error = atm_get_addr(dev, buf, len,
353                                              (cmd == ATM_GETADDR ?
354                                               ATM_ADDR_LOCAL : ATM_ADDR_LECS));
355                         if (error < 0)
356                                 goto done;
357                         size = error;
358                         /* may return 0, but later on size == 0 means "don't
359                            write the length" */
360                         error = put_user(size, &sioc->length)
361                                 ? -EFAULT : 0;
362                         goto done;
363                 case ATM_SETLOOP:
364                         if (__ATM_LM_XTRMT((int) (unsigned long) buf) &&
365                             __ATM_LM_XTLOC((int) (unsigned long) buf) >
366                             __ATM_LM_XTRMT((int) (unsigned long) buf)) {
367                                 error = -EINVAL;
368                                 goto done;
369                         }
370                         /* fall through */
371                 case ATM_SETCIRANGE:
372                 case SONET_GETSTATZ:
373                 case SONET_SETDIAG:
374                 case SONET_CLRDIAG:
375                 case SONET_SETFRAMING:
376                         if (!capable(CAP_NET_ADMIN)) {
377                                 error = -EPERM;
378                                 goto done;
379                         }
380                         /* fall through */
381                 default:
382                         if (!dev->ops->ioctl) {
383                                 error = -EINVAL;
384                                 goto done;
385                         }
386                         size = dev->ops->ioctl(dev, cmd, buf);
387                         if (size < 0) {
388                                 error = (size == -ENOIOCTLCMD ? -EINVAL : size);
389                                 goto done;
390                         }
391         }
392         
393         if (size)
394                 error = put_user(size, &sioc->length)
395                         ? -EFAULT : 0;
396         else
397                 error = 0;
398 done:
399         atm_dev_put(dev);
400         return error;
401 }
402
403 static __inline__ void *dev_get_idx(loff_t left)
404 {
405         struct list_head *p;
406
407         list_for_each(p, &atm_devs) {
408                 if (!--left)
409                         break;
410         }
411         return (p != &atm_devs) ? p : NULL;
412 }
413
414 void *atm_dev_seq_start(struct seq_file *seq, loff_t *pos)
415 {
416         down(&atm_dev_mutex);
417         return *pos ? dev_get_idx(*pos) : (void *) 1;
418 }
419
420 void atm_dev_seq_stop(struct seq_file *seq, void *v)
421 {
422         up(&atm_dev_mutex);
423 }
424  
425 void *atm_dev_seq_next(struct seq_file *seq, void *v, loff_t *pos)
426 {
427         ++*pos;
428         v = (v == (void *)1) ? atm_devs.next : ((struct list_head *)v)->next;
429         return (v == &atm_devs) ? NULL : v;
430 }
431
432
433 EXPORT_SYMBOL(atm_dev_register);
434 EXPORT_SYMBOL(atm_dev_deregister);
435 EXPORT_SYMBOL(atm_dev_lookup);
436 EXPORT_SYMBOL(shutdown_atm_dev);