resources.c 9.8 KB

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