blob: 529f7e64aa2c33ae45d162b035ccf241431c6c0d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/ctype.h>
12#include <linux/string.h>
13#include <linux/atmdev.h>
14#include <linux/sonet.h>
15#include <linux/kernel.h> /* for barrier */
16#include <linux/module.h>
17#include <linux/bitops.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080018#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/delay.h>
Ingo Molnar57b47a52006-03-20 22:35:41 -080020#include <linux/mutex.h>
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <net/sock.h> /* for struct sock */
23
24#include "common.h"
25#include "resources.h"
26#include "addr.h"
27
28
29LIST_HEAD(atm_devs);
Ingo Molnar57b47a52006-03-20 22:35:41 -080030DEFINE_MUTEX(atm_dev_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32static struct atm_dev *__alloc_atm_dev(const char *type)
33{
34 struct atm_dev *dev;
35
Panagiotis Issaris0da974f2006-07-21 14:51:30 -070036 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 if (!dev)
38 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 dev->type = type;
40 dev->signal = ATM_PHY_SIG_UNKNOWN;
41 dev->link_rate = ATM_OC3_PCR;
42 spin_lock_init(&dev->lock);
43 INIT_LIST_HEAD(&dev->local);
Eric Kinzie0f21ba72005-10-06 22:19:28 -070044 INIT_LIST_HEAD(&dev->lecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46 return dev;
47}
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049static struct atm_dev *__atm_dev_lookup(int number)
50{
51 struct atm_dev *dev;
52 struct list_head *p;
53
54 list_for_each(p, &atm_devs) {
55 dev = list_entry(p, struct atm_dev, dev_list);
Stanislaw Gruszkaaaaaaad2005-11-29 16:16:21 -080056 if (dev->number == number) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 atm_dev_hold(dev);
58 return dev;
59 }
60 }
61 return NULL;
62}
63
64struct atm_dev *atm_dev_lookup(int number)
65{
66 struct atm_dev *dev;
67
Ingo Molnar57b47a52006-03-20 22:35:41 -080068 mutex_lock(&atm_dev_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 dev = __atm_dev_lookup(number);
Ingo Molnar57b47a52006-03-20 22:35:41 -080070 mutex_unlock(&atm_dev_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 return dev;
72}
73
Stanislaw Gruszka64bf69d2005-11-29 16:16:41 -080074
Linus Torvalds1da177e2005-04-16 15:20:36 -070075struct atm_dev *atm_dev_register(const char *type, const struct atmdev_ops *ops,
76 int number, unsigned long *flags)
77{
78 struct atm_dev *dev, *inuse;
79
80 dev = __alloc_atm_dev(type);
81 if (!dev) {
82 printk(KERN_ERR "atm_dev_register: no space for dev %s\n",
83 type);
84 return NULL;
85 }
Ingo Molnar57b47a52006-03-20 22:35:41 -080086 mutex_lock(&atm_dev_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 if (number != -1) {
88 if ((inuse = __atm_dev_lookup(number))) {
89 atm_dev_put(inuse);
Ingo Molnar57b47a52006-03-20 22:35:41 -080090 mutex_unlock(&atm_dev_mutex);
Adrian Bunkebc37b62005-04-21 16:48:26 -070091 kfree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 return NULL;
93 }
94 dev->number = number;
95 } else {
96 dev->number = 0;
97 while ((inuse = __atm_dev_lookup(dev->number))) {
98 atm_dev_put(inuse);
99 dev->number++;
100 }
101 }
102
103 dev->ops = ops;
104 if (flags)
105 dev->flags = *flags;
106 else
107 memset(&dev->flags, 0, sizeof(dev->flags));
108 memset(&dev->stats, 0, sizeof(dev->stats));
109 atomic_set(&dev->refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111 if (atm_proc_dev_register(dev) < 0) {
112 printk(KERN_ERR "atm_dev_register: "
113 "atm_proc_dev_register failed for dev %s\n",
114 type);
Roman Kagan656d98b2006-06-29 12:36:34 -0700115 goto out_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 }
117
Roman Kagan656d98b2006-06-29 12:36:34 -0700118 if (atm_register_sysfs(dev) < 0) {
119 printk(KERN_ERR "atm_dev_register: "
120 "atm_register_sysfs failed for dev %s\n",
121 type);
122 atm_proc_dev_deregister(dev);
123 goto out_fail;
124 }
125
126 list_add_tail(&dev->dev_list, &atm_devs);
127
128out:
129 mutex_unlock(&atm_dev_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 return dev;
Roman Kagan656d98b2006-06-29 12:36:34 -0700131
132out_fail:
133 kfree(dev);
134 dev = NULL;
135 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136}
137
138
139void atm_dev_deregister(struct atm_dev *dev)
140{
Stanislaw Gruszka64bf69d2005-11-29 16:16:41 -0800141 BUG_ON(test_bit(ATM_DF_REMOVED, &dev->flags));
142 set_bit(ATM_DF_REMOVED, &dev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Stanislaw Gruszka64bf69d2005-11-29 16:16:41 -0800144 /*
145 * if we remove current device from atm_devs list, new device
146 * with same number can appear, such we need deregister proc,
147 * release async all vccs and remove them from vccs list too
148 */
Ingo Molnar57b47a52006-03-20 22:35:41 -0800149 mutex_lock(&atm_dev_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 list_del(&dev->dev_list);
Ingo Molnar57b47a52006-03-20 22:35:41 -0800151 mutex_unlock(&atm_dev_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Stanislaw Gruszka64bf69d2005-11-29 16:16:41 -0800153 atm_dev_release_vccs(dev);
Roman Kagan656d98b2006-06-29 12:36:34 -0700154 atm_unregister_sysfs(dev);
Stanislaw Gruszka64bf69d2005-11-29 16:16:41 -0800155 atm_proc_dev_deregister(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Stanislaw Gruszka64bf69d2005-11-29 16:16:41 -0800157 atm_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158}
159
160
161static void copy_aal_stats(struct k_atm_aal_stats *from,
162 struct atm_aal_stats *to)
163{
164#define __HANDLE_ITEM(i) to->i = atomic_read(&from->i)
165 __AAL_STAT_ITEMS
166#undef __HANDLE_ITEM
167}
168
169
170static void subtract_aal_stats(struct k_atm_aal_stats *from,
171 struct atm_aal_stats *to)
172{
173#define __HANDLE_ITEM(i) atomic_sub(to->i, &from->i)
174 __AAL_STAT_ITEMS
175#undef __HANDLE_ITEM
176}
177
178
179static int fetch_stats(struct atm_dev *dev, struct atm_dev_stats __user *arg, int zero)
180{
181 struct atm_dev_stats tmp;
182 int error = 0;
183
184 copy_aal_stats(&dev->stats.aal0, &tmp.aal0);
185 copy_aal_stats(&dev->stats.aal34, &tmp.aal34);
186 copy_aal_stats(&dev->stats.aal5, &tmp.aal5);
187 if (arg)
188 error = copy_to_user(arg, &tmp, sizeof(tmp));
189 if (zero && !error) {
190 subtract_aal_stats(&dev->stats.aal0, &tmp.aal0);
191 subtract_aal_stats(&dev->stats.aal34, &tmp.aal34);
192 subtract_aal_stats(&dev->stats.aal5, &tmp.aal5);
193 }
194 return error ? -EFAULT : 0;
195}
196
197
198int atm_dev_ioctl(unsigned int cmd, void __user *arg)
199{
200 void __user *buf;
201 int error, len, number, size = 0;
202 struct atm_dev *dev;
203 struct list_head *p;
204 int *tmp_buf, *tmp_p;
205 struct atm_iobuf __user *iobuf = arg;
206 struct atmif_sioc __user *sioc = arg;
207 switch (cmd) {
208 case ATM_GETNAMES:
209 if (get_user(buf, &iobuf->buffer))
210 return -EFAULT;
211 if (get_user(len, &iobuf->length))
212 return -EFAULT;
Ingo Molnar57b47a52006-03-20 22:35:41 -0800213 mutex_lock(&atm_dev_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 list_for_each(p, &atm_devs)
215 size += sizeof(int);
216 if (size > len) {
Ingo Molnar57b47a52006-03-20 22:35:41 -0800217 mutex_unlock(&atm_dev_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 return -E2BIG;
219 }
220 tmp_buf = kmalloc(size, GFP_ATOMIC);
221 if (!tmp_buf) {
Ingo Molnar57b47a52006-03-20 22:35:41 -0800222 mutex_unlock(&atm_dev_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 return -ENOMEM;
224 }
225 tmp_p = tmp_buf;
226 list_for_each(p, &atm_devs) {
227 dev = list_entry(p, struct atm_dev, dev_list);
228 *tmp_p++ = dev->number;
229 }
Ingo Molnar57b47a52006-03-20 22:35:41 -0800230 mutex_unlock(&atm_dev_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 error = ((copy_to_user(buf, tmp_buf, size)) ||
232 put_user(size, &iobuf->length))
233 ? -EFAULT : 0;
234 kfree(tmp_buf);
235 return error;
236 default:
237 break;
238 }
239
240 if (get_user(buf, &sioc->arg))
241 return -EFAULT;
242 if (get_user(len, &sioc->length))
243 return -EFAULT;
244 if (get_user(number, &sioc->number))
245 return -EFAULT;
246
Mitchell Blank Jr50accc92005-11-29 16:15:18 -0800247 if (!(dev = try_then_request_module(atm_dev_lookup(number),
248 "atm-device-%d", number)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 return -ENODEV;
250
251 switch (cmd) {
252 case ATM_GETTYPE:
253 size = strlen(dev->type) + 1;
254 if (copy_to_user(buf, dev->type, size)) {
255 error = -EFAULT;
256 goto done;
257 }
258 break;
259 case ATM_GETESI:
260 size = ESI_LEN;
261 if (copy_to_user(buf, dev->esi, size)) {
262 error = -EFAULT;
263 goto done;
264 }
265 break;
266 case ATM_SETESI:
267 {
268 int i;
269
270 for (i = 0; i < ESI_LEN; i++)
271 if (dev->esi[i]) {
272 error = -EEXIST;
273 goto done;
274 }
275 }
276 /* fall through */
277 case ATM_SETESIF:
278 {
279 unsigned char esi[ESI_LEN];
280
281 if (!capable(CAP_NET_ADMIN)) {
282 error = -EPERM;
283 goto done;
284 }
285 if (copy_from_user(esi, buf, ESI_LEN)) {
286 error = -EFAULT;
287 goto done;
288 }
289 memcpy(dev->esi, esi, ESI_LEN);
290 error = ESI_LEN;
291 goto done;
292 }
293 case ATM_GETSTATZ:
294 if (!capable(CAP_NET_ADMIN)) {
295 error = -EPERM;
296 goto done;
297 }
298 /* fall through */
299 case ATM_GETSTAT:
300 size = sizeof(struct atm_dev_stats);
301 error = fetch_stats(dev, buf, cmd == ATM_GETSTATZ);
302 if (error)
303 goto done;
304 break;
305 case ATM_GETCIRANGE:
306 size = sizeof(struct atm_cirange);
307 if (copy_to_user(buf, &dev->ci_range, size)) {
308 error = -EFAULT;
309 goto done;
310 }
311 break;
312 case ATM_GETLINKRATE:
313 size = sizeof(int);
314 if (copy_to_user(buf, &dev->link_rate, size)) {
315 error = -EFAULT;
316 goto done;
317 }
318 break;
319 case ATM_RSTADDR:
320 if (!capable(CAP_NET_ADMIN)) {
321 error = -EPERM;
322 goto done;
323 }
Eric Kinzie0f21ba72005-10-06 22:19:28 -0700324 atm_reset_addr(dev, ATM_ADDR_LOCAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 break;
326 case ATM_ADDADDR:
327 case ATM_DELADDR:
Eric Kinzie0f21ba72005-10-06 22:19:28 -0700328 case ATM_ADDLECSADDR:
329 case ATM_DELLECSADDR:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 if (!capable(CAP_NET_ADMIN)) {
331 error = -EPERM;
332 goto done;
333 }
334 {
335 struct sockaddr_atmsvc addr;
336
337 if (copy_from_user(&addr, buf, sizeof(addr))) {
338 error = -EFAULT;
339 goto done;
340 }
Eric Kinzie0f21ba72005-10-06 22:19:28 -0700341 if (cmd == ATM_ADDADDR || cmd == ATM_ADDLECSADDR)
342 error = atm_add_addr(dev, &addr,
343 (cmd == ATM_ADDADDR ?
344 ATM_ADDR_LOCAL : ATM_ADDR_LECS));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 else
Eric Kinzie0f21ba72005-10-06 22:19:28 -0700346 error = atm_del_addr(dev, &addr,
347 (cmd == ATM_DELADDR ?
348 ATM_ADDR_LOCAL : ATM_ADDR_LECS));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 goto done;
350 }
351 case ATM_GETADDR:
Eric Kinzie0f21ba72005-10-06 22:19:28 -0700352 case ATM_GETLECSADDR:
353 error = atm_get_addr(dev, buf, len,
354 (cmd == ATM_GETADDR ?
355 ATM_ADDR_LOCAL : ATM_ADDR_LECS));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 if (error < 0)
357 goto done;
358 size = error;
359 /* may return 0, but later on size == 0 means "don't
360 write the length" */
361 error = put_user(size, &sioc->length)
362 ? -EFAULT : 0;
363 goto done;
364 case ATM_SETLOOP:
365 if (__ATM_LM_XTRMT((int) (unsigned long) buf) &&
366 __ATM_LM_XTLOC((int) (unsigned long) buf) >
367 __ATM_LM_XTRMT((int) (unsigned long) buf)) {
368 error = -EINVAL;
369 goto done;
370 }
371 /* fall through */
372 case ATM_SETCIRANGE:
373 case SONET_GETSTATZ:
374 case SONET_SETDIAG:
375 case SONET_CLRDIAG:
376 case SONET_SETFRAMING:
377 if (!capable(CAP_NET_ADMIN)) {
378 error = -EPERM;
379 goto done;
380 }
381 /* fall through */
382 default:
383 if (!dev->ops->ioctl) {
384 error = -EINVAL;
385 goto done;
386 }
387 size = dev->ops->ioctl(dev, cmd, buf);
388 if (size < 0) {
389 error = (size == -ENOIOCTLCMD ? -EINVAL : size);
390 goto done;
391 }
392 }
393
394 if (size)
395 error = put_user(size, &sioc->length)
396 ? -EFAULT : 0;
397 else
398 error = 0;
399done:
400 atm_dev_put(dev);
401 return error;
402}
403
404static __inline__ void *dev_get_idx(loff_t left)
405{
406 struct list_head *p;
407
408 list_for_each(p, &atm_devs) {
409 if (!--left)
410 break;
411 }
412 return (p != &atm_devs) ? p : NULL;
413}
414
415void *atm_dev_seq_start(struct seq_file *seq, loff_t *pos)
416{
Ingo Molnar57b47a52006-03-20 22:35:41 -0800417 mutex_lock(&atm_dev_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 return *pos ? dev_get_idx(*pos) : (void *) 1;
419}
420
421void atm_dev_seq_stop(struct seq_file *seq, void *v)
422{
Ingo Molnar57b47a52006-03-20 22:35:41 -0800423 mutex_unlock(&atm_dev_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424}
425
426void *atm_dev_seq_next(struct seq_file *seq, void *v, loff_t *pos)
427{
428 ++*pos;
429 v = (v == (void *)1) ? atm_devs.next : ((struct list_head *)v)->next;
430 return (v == &atm_devs) ? NULL : v;
431}
432
433
434EXPORT_SYMBOL(atm_dev_register);
435EXPORT_SYMBOL(atm_dev_deregister);
436EXPORT_SYMBOL(atm_dev_lookup);