resources.c 9.9 KB

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