Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Joe Perches | 9982446 | 2010-01-26 11:40:00 +0000 | [diff] [blame] | 10 | #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Randy Dunlap | 4fc268d | 2006-01-11 12:17:47 -0800 | [diff] [blame] | 19 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/delay.h> |
Ingo Molnar | 57b47a5 | 2006-03-20 22:35:41 -0800 | [diff] [blame] | 21 | #include <linux/mutex.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 22 | #include <linux/slab.h> |
Ingo Molnar | 57b47a5 | 2006-03-20 22:35:41 -0800 | [diff] [blame] | 23 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <net/sock.h> /* for struct sock */ |
| 25 | |
| 26 | #include "common.h" |
| 27 | #include "resources.h" |
| 28 | #include "addr.h" |
| 29 | |
| 30 | |
| 31 | LIST_HEAD(atm_devs); |
Ingo Molnar | 57b47a5 | 2006-03-20 22:35:41 -0800 | [diff] [blame] | 32 | DEFINE_MUTEX(atm_dev_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
| 34 | static struct atm_dev *__alloc_atm_dev(const char *type) |
| 35 | { |
| 36 | struct atm_dev *dev; |
| 37 | |
Panagiotis Issaris | 0da974f | 2006-07-21 14:51:30 -0700 | [diff] [blame] | 38 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | if (!dev) |
| 40 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | dev->type = type; |
| 42 | dev->signal = ATM_PHY_SIG_UNKNOWN; |
| 43 | dev->link_rate = ATM_OC3_PCR; |
| 44 | spin_lock_init(&dev->lock); |
| 45 | INIT_LIST_HEAD(&dev->local); |
Eric Kinzie | 0f21ba7 | 2005-10-06 22:19:28 -0700 | [diff] [blame] | 46 | INIT_LIST_HEAD(&dev->lecs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
| 48 | return dev; |
| 49 | } |
| 50 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | static struct atm_dev *__atm_dev_lookup(int number) |
| 52 | { |
| 53 | struct atm_dev *dev; |
| 54 | struct list_head *p; |
| 55 | |
| 56 | list_for_each(p, &atm_devs) { |
| 57 | dev = list_entry(p, struct atm_dev, dev_list); |
Stanislaw Gruszka | aaaaaad | 2005-11-29 16:16:21 -0800 | [diff] [blame] | 58 | if (dev->number == number) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | atm_dev_hold(dev); |
| 60 | return dev; |
| 61 | } |
| 62 | } |
| 63 | return NULL; |
| 64 | } |
| 65 | |
| 66 | struct atm_dev *atm_dev_lookup(int number) |
| 67 | { |
| 68 | struct atm_dev *dev; |
| 69 | |
Ingo Molnar | 57b47a5 | 2006-03-20 22:35:41 -0800 | [diff] [blame] | 70 | mutex_lock(&atm_dev_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | dev = __atm_dev_lookup(number); |
Ingo Molnar | 57b47a5 | 2006-03-20 22:35:41 -0800 | [diff] [blame] | 72 | mutex_unlock(&atm_dev_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | return dev; |
| 74 | } |
Joe Perches | 07b54c9 | 2010-01-26 11:40:16 +0000 | [diff] [blame] | 75 | EXPORT_SYMBOL(atm_dev_lookup); |
Stanislaw Gruszka | 64bf69d | 2005-11-29 16:16:41 -0800 | [diff] [blame] | 76 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | struct atm_dev *atm_dev_register(const char *type, const struct atmdev_ops *ops, |
| 78 | int number, unsigned long *flags) |
| 79 | { |
| 80 | struct atm_dev *dev, *inuse; |
| 81 | |
| 82 | dev = __alloc_atm_dev(type); |
| 83 | if (!dev) { |
Joe Perches | 9982446 | 2010-01-26 11:40:00 +0000 | [diff] [blame] | 84 | pr_err("no space for dev %s\n", type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | return NULL; |
| 86 | } |
Ingo Molnar | 57b47a5 | 2006-03-20 22:35:41 -0800 | [diff] [blame] | 87 | mutex_lock(&atm_dev_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | if (number != -1) { |
Joe Perches | 07b54c9 | 2010-01-26 11:40:16 +0000 | [diff] [blame] | 89 | inuse = __atm_dev_lookup(number); |
| 90 | if (inuse) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | atm_dev_put(inuse); |
Ingo Molnar | 57b47a5 | 2006-03-20 22:35:41 -0800 | [diff] [blame] | 92 | mutex_unlock(&atm_dev_mutex); |
Adrian Bunk | ebc37b6 | 2005-04-21 16:48:26 -0700 | [diff] [blame] | 93 | kfree(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | return NULL; |
| 95 | } |
| 96 | dev->number = number; |
| 97 | } else { |
| 98 | dev->number = 0; |
| 99 | while ((inuse = __atm_dev_lookup(dev->number))) { |
| 100 | atm_dev_put(inuse); |
| 101 | dev->number++; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | dev->ops = ops; |
| 106 | if (flags) |
| 107 | dev->flags = *flags; |
| 108 | else |
| 109 | memset(&dev->flags, 0, sizeof(dev->flags)); |
| 110 | memset(&dev->stats, 0, sizeof(dev->stats)); |
| 111 | atomic_set(&dev->refcnt, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | |
| 113 | if (atm_proc_dev_register(dev) < 0) { |
Joe Perches | 9982446 | 2010-01-26 11:40:00 +0000 | [diff] [blame] | 114 | pr_err("atm_proc_dev_register failed for dev %s\n", type); |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 115 | goto out_fail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 118 | if (atm_register_sysfs(dev) < 0) { |
Joe Perches | 9982446 | 2010-01-26 11:40:00 +0000 | [diff] [blame] | 119 | pr_err("atm_register_sysfs failed for dev %s\n", type); |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 120 | atm_proc_dev_deregister(dev); |
| 121 | goto out_fail; |
| 122 | } |
| 123 | |
| 124 | list_add_tail(&dev->dev_list, &atm_devs); |
| 125 | |
| 126 | out: |
| 127 | mutex_unlock(&atm_dev_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | return dev; |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 129 | |
| 130 | out_fail: |
| 131 | kfree(dev); |
| 132 | dev = NULL; |
| 133 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | } |
Joe Perches | 07b54c9 | 2010-01-26 11:40:16 +0000 | [diff] [blame] | 135 | EXPORT_SYMBOL(atm_dev_register); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | |
| 137 | void atm_dev_deregister(struct atm_dev *dev) |
| 138 | { |
Stanislaw Gruszka | 64bf69d | 2005-11-29 16:16:41 -0800 | [diff] [blame] | 139 | BUG_ON(test_bit(ATM_DF_REMOVED, &dev->flags)); |
| 140 | set_bit(ATM_DF_REMOVED, &dev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
Stanislaw Gruszka | 64bf69d | 2005-11-29 16:16:41 -0800 | [diff] [blame] | 142 | /* |
YOSHIFUJI Hideaki | f7d5745 | 2007-02-09 23:24:29 +0900 | [diff] [blame] | 143 | * if we remove current device from atm_devs list, new device |
| 144 | * with same number can appear, such we need deregister proc, |
Stanislaw Gruszka | 64bf69d | 2005-11-29 16:16:41 -0800 | [diff] [blame] | 145 | * release async all vccs and remove them from vccs list too |
| 146 | */ |
Ingo Molnar | 57b47a5 | 2006-03-20 22:35:41 -0800 | [diff] [blame] | 147 | mutex_lock(&atm_dev_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | list_del(&dev->dev_list); |
Ingo Molnar | 57b47a5 | 2006-03-20 22:35:41 -0800 | [diff] [blame] | 149 | mutex_unlock(&atm_dev_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | |
Stanislaw Gruszka | 64bf69d | 2005-11-29 16:16:41 -0800 | [diff] [blame] | 151 | atm_dev_release_vccs(dev); |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 152 | atm_unregister_sysfs(dev); |
Stanislaw Gruszka | 64bf69d | 2005-11-29 16:16:41 -0800 | [diff] [blame] | 153 | atm_proc_dev_deregister(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | |
Stanislaw Gruszka | 64bf69d | 2005-11-29 16:16:41 -0800 | [diff] [blame] | 155 | atm_dev_put(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | } |
Joe Perches | 07b54c9 | 2010-01-26 11:40:16 +0000 | [diff] [blame] | 157 | EXPORT_SYMBOL(atm_dev_deregister); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | |
| 159 | static void copy_aal_stats(struct k_atm_aal_stats *from, |
| 160 | struct atm_aal_stats *to) |
| 161 | { |
| 162 | #define __HANDLE_ITEM(i) to->i = atomic_read(&from->i) |
| 163 | __AAL_STAT_ITEMS |
| 164 | #undef __HANDLE_ITEM |
| 165 | } |
| 166 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | static void subtract_aal_stats(struct k_atm_aal_stats *from, |
| 168 | struct atm_aal_stats *to) |
| 169 | { |
| 170 | #define __HANDLE_ITEM(i) atomic_sub(to->i, &from->i) |
| 171 | __AAL_STAT_ITEMS |
| 172 | #undef __HANDLE_ITEM |
| 173 | } |
| 174 | |
Joe Perches | 07b54c9 | 2010-01-26 11:40:16 +0000 | [diff] [blame] | 175 | static int fetch_stats(struct atm_dev *dev, struct atm_dev_stats __user *arg, |
| 176 | int zero) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | { |
| 178 | struct atm_dev_stats tmp; |
| 179 | int error = 0; |
| 180 | |
| 181 | copy_aal_stats(&dev->stats.aal0, &tmp.aal0); |
| 182 | copy_aal_stats(&dev->stats.aal34, &tmp.aal34); |
| 183 | copy_aal_stats(&dev->stats.aal5, &tmp.aal5); |
| 184 | if (arg) |
| 185 | error = copy_to_user(arg, &tmp, sizeof(tmp)); |
| 186 | if (zero && !error) { |
| 187 | subtract_aal_stats(&dev->stats.aal0, &tmp.aal0); |
| 188 | subtract_aal_stats(&dev->stats.aal34, &tmp.aal34); |
| 189 | subtract_aal_stats(&dev->stats.aal5, &tmp.aal5); |
| 190 | } |
| 191 | return error ? -EFAULT : 0; |
| 192 | } |
| 193 | |
David Woodhouse | 8865c41 | 2008-12-03 22:12:38 -0800 | [diff] [blame] | 194 | int atm_dev_ioctl(unsigned int cmd, void __user *arg, int compat) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | { |
| 196 | void __user *buf; |
| 197 | int error, len, number, size = 0; |
| 198 | struct atm_dev *dev; |
| 199 | struct list_head *p; |
| 200 | int *tmp_buf, *tmp_p; |
David Woodhouse | 8865c41 | 2008-12-03 22:12:38 -0800 | [diff] [blame] | 201 | int __user *sioc_len; |
| 202 | int __user *iobuf_len; |
| 203 | |
| 204 | #ifndef CONFIG_COMPAT |
| 205 | compat = 0; /* Just so the compiler _knows_ */ |
| 206 | #endif |
| 207 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | switch (cmd) { |
Joe Perches | 07b54c9 | 2010-01-26 11:40:16 +0000 | [diff] [blame] | 209 | case ATM_GETNAMES: |
| 210 | if (compat) { |
David Woodhouse | 8865c41 | 2008-12-03 22:12:38 -0800 | [diff] [blame] | 211 | #ifdef CONFIG_COMPAT |
Joe Perches | 07b54c9 | 2010-01-26 11:40:16 +0000 | [diff] [blame] | 212 | struct compat_atm_iobuf __user *ciobuf = arg; |
| 213 | compat_uptr_t cbuf; |
| 214 | iobuf_len = &ciobuf->length; |
| 215 | if (get_user(cbuf, &ciobuf->buffer)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | return -EFAULT; |
Joe Perches | 07b54c9 | 2010-01-26 11:40:16 +0000 | [diff] [blame] | 217 | buf = compat_ptr(cbuf); |
| 218 | #endif |
| 219 | } else { |
| 220 | struct atm_iobuf __user *iobuf = arg; |
| 221 | iobuf_len = &iobuf->length; |
| 222 | if (get_user(buf, &iobuf->buffer)) |
| 223 | return -EFAULT; |
| 224 | } |
| 225 | if (get_user(len, iobuf_len)) |
| 226 | return -EFAULT; |
| 227 | mutex_lock(&atm_dev_mutex); |
| 228 | list_for_each(p, &atm_devs) |
| 229 | size += sizeof(int); |
| 230 | if (size > len) { |
Ingo Molnar | 57b47a5 | 2006-03-20 22:35:41 -0800 | [diff] [blame] | 231 | mutex_unlock(&atm_dev_mutex); |
Joe Perches | 07b54c9 | 2010-01-26 11:40:16 +0000 | [diff] [blame] | 232 | return -E2BIG; |
| 233 | } |
| 234 | tmp_buf = kmalloc(size, GFP_ATOMIC); |
| 235 | if (!tmp_buf) { |
| 236 | mutex_unlock(&atm_dev_mutex); |
| 237 | return -ENOMEM; |
| 238 | } |
| 239 | tmp_p = tmp_buf; |
| 240 | list_for_each(p, &atm_devs) { |
| 241 | dev = list_entry(p, struct atm_dev, dev_list); |
| 242 | *tmp_p++ = dev->number; |
| 243 | } |
| 244 | mutex_unlock(&atm_dev_mutex); |
| 245 | error = ((copy_to_user(buf, tmp_buf, size)) || |
| 246 | put_user(size, iobuf_len)) |
| 247 | ? -EFAULT : 0; |
| 248 | kfree(tmp_buf); |
| 249 | return error; |
| 250 | default: |
| 251 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | } |
| 253 | |
David Woodhouse | 8865c41 | 2008-12-03 22:12:38 -0800 | [diff] [blame] | 254 | if (compat) { |
| 255 | #ifdef CONFIG_COMPAT |
| 256 | struct compat_atmif_sioc __user *csioc = arg; |
| 257 | compat_uptr_t carg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | |
David Woodhouse | 8865c41 | 2008-12-03 22:12:38 -0800 | [diff] [blame] | 259 | sioc_len = &csioc->length; |
| 260 | if (get_user(carg, &csioc->arg)) |
| 261 | return -EFAULT; |
| 262 | buf = compat_ptr(carg); |
| 263 | |
| 264 | if (get_user(len, &csioc->length)) |
| 265 | return -EFAULT; |
| 266 | if (get_user(number, &csioc->number)) |
| 267 | return -EFAULT; |
| 268 | #endif |
| 269 | } else { |
| 270 | struct atmif_sioc __user *sioc = arg; |
| 271 | |
| 272 | sioc_len = &sioc->length; |
| 273 | if (get_user(buf, &sioc->arg)) |
| 274 | return -EFAULT; |
| 275 | if (get_user(len, &sioc->length)) |
| 276 | return -EFAULT; |
| 277 | if (get_user(number, &sioc->number)) |
| 278 | return -EFAULT; |
| 279 | } |
Joe Perches | 07b54c9 | 2010-01-26 11:40:16 +0000 | [diff] [blame] | 280 | |
| 281 | dev = try_then_request_module(atm_dev_lookup(number), "atm-device-%d", |
| 282 | number); |
| 283 | if (!dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | return -ENODEV; |
YOSHIFUJI Hideaki | f7d5745 | 2007-02-09 23:24:29 +0900 | [diff] [blame] | 285 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | switch (cmd) { |
Joe Perches | 07b54c9 | 2010-01-26 11:40:16 +0000 | [diff] [blame] | 287 | case ATM_GETTYPE: |
| 288 | size = strlen(dev->type) + 1; |
| 289 | if (copy_to_user(buf, dev->type, size)) { |
| 290 | error = -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | goto done; |
Joe Perches | 07b54c9 | 2010-01-26 11:40:16 +0000 | [diff] [blame] | 292 | } |
| 293 | break; |
| 294 | case ATM_GETESI: |
| 295 | size = ESI_LEN; |
| 296 | if (copy_to_user(buf, dev->esi, size)) { |
| 297 | error = -EFAULT; |
| 298 | goto done; |
| 299 | } |
| 300 | break; |
| 301 | case ATM_SETESI: |
| 302 | { |
| 303 | int i; |
| 304 | |
| 305 | for (i = 0; i < ESI_LEN; i++) |
| 306 | if (dev->esi[i]) { |
| 307 | error = -EEXIST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | goto done; |
| 309 | } |
| 310 | } |
Joe Perches | 07b54c9 | 2010-01-26 11:40:16 +0000 | [diff] [blame] | 311 | /* fall through */ |
| 312 | case ATM_SETESIF: |
| 313 | { |
| 314 | unsigned char esi[ESI_LEN]; |
| 315 | |
| 316 | if (!capable(CAP_NET_ADMIN)) { |
| 317 | error = -EPERM; |
| 318 | goto done; |
| 319 | } |
| 320 | if (copy_from_user(esi, buf, ESI_LEN)) { |
| 321 | error = -EFAULT; |
| 322 | goto done; |
| 323 | } |
| 324 | memcpy(dev->esi, esi, ESI_LEN); |
| 325 | error = ESI_LEN; |
| 326 | goto done; |
| 327 | } |
| 328 | case ATM_GETSTATZ: |
| 329 | if (!capable(CAP_NET_ADMIN)) { |
| 330 | error = -EPERM; |
| 331 | goto done; |
| 332 | } |
| 333 | /* fall through */ |
| 334 | case ATM_GETSTAT: |
| 335 | size = sizeof(struct atm_dev_stats); |
| 336 | error = fetch_stats(dev, buf, cmd == ATM_GETSTATZ); |
| 337 | if (error) |
| 338 | goto done; |
| 339 | break; |
| 340 | case ATM_GETCIRANGE: |
| 341 | size = sizeof(struct atm_cirange); |
| 342 | if (copy_to_user(buf, &dev->ci_range, size)) { |
| 343 | error = -EFAULT; |
| 344 | goto done; |
| 345 | } |
| 346 | break; |
| 347 | case ATM_GETLINKRATE: |
| 348 | size = sizeof(int); |
| 349 | if (copy_to_user(buf, &dev->link_rate, size)) { |
| 350 | error = -EFAULT; |
| 351 | goto done; |
| 352 | } |
| 353 | break; |
| 354 | case ATM_RSTADDR: |
| 355 | if (!capable(CAP_NET_ADMIN)) { |
| 356 | error = -EPERM; |
| 357 | goto done; |
| 358 | } |
| 359 | atm_reset_addr(dev, ATM_ADDR_LOCAL); |
| 360 | break; |
| 361 | case ATM_ADDADDR: |
| 362 | case ATM_DELADDR: |
| 363 | case ATM_ADDLECSADDR: |
| 364 | case ATM_DELLECSADDR: |
| 365 | { |
| 366 | struct sockaddr_atmsvc addr; |
| 367 | |
| 368 | if (!capable(CAP_NET_ADMIN)) { |
| 369 | error = -EPERM; |
| 370 | goto done; |
| 371 | } |
| 372 | |
| 373 | if (copy_from_user(&addr, buf, sizeof(addr))) { |
| 374 | error = -EFAULT; |
| 375 | goto done; |
| 376 | } |
| 377 | if (cmd == ATM_ADDADDR || cmd == ATM_ADDLECSADDR) |
| 378 | error = atm_add_addr(dev, &addr, |
| 379 | (cmd == ATM_ADDADDR ? |
| 380 | ATM_ADDR_LOCAL : ATM_ADDR_LECS)); |
| 381 | else |
| 382 | error = atm_del_addr(dev, &addr, |
| 383 | (cmd == ATM_DELADDR ? |
| 384 | ATM_ADDR_LOCAL : ATM_ADDR_LECS)); |
| 385 | goto done; |
| 386 | } |
| 387 | case ATM_GETADDR: |
| 388 | case ATM_GETLECSADDR: |
| 389 | error = atm_get_addr(dev, buf, len, |
| 390 | (cmd == ATM_GETADDR ? |
| 391 | ATM_ADDR_LOCAL : ATM_ADDR_LECS)); |
| 392 | if (error < 0) |
| 393 | goto done; |
| 394 | size = error; |
| 395 | /* may return 0, but later on size == 0 means "don't |
| 396 | write the length" */ |
| 397 | error = put_user(size, sioc_len) ? -EFAULT : 0; |
| 398 | goto done; |
| 399 | case ATM_SETLOOP: |
| 400 | if (__ATM_LM_XTRMT((int) (unsigned long) buf) && |
| 401 | __ATM_LM_XTLOC((int) (unsigned long) buf) > |
| 402 | __ATM_LM_XTRMT((int) (unsigned long) buf)) { |
| 403 | error = -EINVAL; |
| 404 | goto done; |
| 405 | } |
| 406 | /* fall through */ |
| 407 | case ATM_SETCIRANGE: |
| 408 | case SONET_GETSTATZ: |
| 409 | case SONET_SETDIAG: |
| 410 | case SONET_CLRDIAG: |
| 411 | case SONET_SETFRAMING: |
| 412 | if (!capable(CAP_NET_ADMIN)) { |
| 413 | error = -EPERM; |
| 414 | goto done; |
| 415 | } |
| 416 | /* fall through */ |
| 417 | default: |
| 418 | if (compat) { |
| 419 | #ifdef CONFIG_COMPAT |
| 420 | if (!dev->ops->compat_ioctl) { |
| 421 | error = -EINVAL; |
| 422 | goto done; |
| 423 | } |
| 424 | size = dev->ops->compat_ioctl(dev, cmd, buf); |
| 425 | #endif |
| 426 | } else { |
| 427 | if (!dev->ops->ioctl) { |
| 428 | error = -EINVAL; |
| 429 | goto done; |
| 430 | } |
| 431 | size = dev->ops->ioctl(dev, cmd, buf); |
| 432 | } |
| 433 | if (size < 0) { |
| 434 | error = (size == -ENOIOCTLCMD ? -EINVAL : size); |
| 435 | goto done; |
| 436 | } |
| 437 | } |
YOSHIFUJI Hideaki | f7d5745 | 2007-02-09 23:24:29 +0900 | [diff] [blame] | 438 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | if (size) |
Joe Perches | 07b54c9 | 2010-01-26 11:40:16 +0000 | [diff] [blame] | 440 | error = put_user(size, sioc_len) ? -EFAULT : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | else |
| 442 | error = 0; |
| 443 | done: |
| 444 | atm_dev_put(dev); |
| 445 | return error; |
| 446 | } |
| 447 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | void *atm_dev_seq_start(struct seq_file *seq, loff_t *pos) |
| 449 | { |
YOSHIFUJI Hideaki | f7d5745 | 2007-02-09 23:24:29 +0900 | [diff] [blame] | 450 | mutex_lock(&atm_dev_mutex); |
Li Zefan | 67de792 | 2010-02-08 23:21:51 +0000 | [diff] [blame] | 451 | return seq_list_start_head(&atm_devs, *pos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | void atm_dev_seq_stop(struct seq_file *seq, void *v) |
| 455 | { |
YOSHIFUJI Hideaki | f7d5745 | 2007-02-09 23:24:29 +0900 | [diff] [blame] | 456 | mutex_unlock(&atm_dev_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | } |
YOSHIFUJI Hideaki | f7d5745 | 2007-02-09 23:24:29 +0900 | [diff] [blame] | 458 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | void *atm_dev_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 460 | { |
Li Zefan | 67de792 | 2010-02-08 23:21:51 +0000 | [diff] [blame] | 461 | return seq_list_next(v, &atm_devs, pos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | } |