resources.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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. #include <linux/config.h>
  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/delay.h>
  16. #include <net/sock.h> /* for struct sock */
  17. #include "common.h"
  18. #include "resources.h"
  19. #include "addr.h"
  20. LIST_HEAD(atm_devs);
  21. DECLARE_MUTEX(atm_dev_mutex);
  22. static struct atm_dev *__alloc_atm_dev(const char *type)
  23. {
  24. struct atm_dev *dev;
  25. dev = kmalloc(sizeof(*dev), GFP_KERNEL);
  26. if (!dev)
  27. return NULL;
  28. memset(dev, 0, sizeof(*dev));
  29. dev->type = type;
  30. dev->signal = ATM_PHY_SIG_UNKNOWN;
  31. dev->link_rate = ATM_OC3_PCR;
  32. spin_lock_init(&dev->lock);
  33. INIT_LIST_HEAD(&dev->local);
  34. INIT_LIST_HEAD(&dev->lecs);
  35. return dev;
  36. }
  37. static struct atm_dev *__atm_dev_lookup(int number)
  38. {
  39. struct atm_dev *dev;
  40. struct list_head *p;
  41. list_for_each(p, &atm_devs) {
  42. dev = list_entry(p, struct atm_dev, dev_list);
  43. if (dev->number == number) {
  44. atm_dev_hold(dev);
  45. return dev;
  46. }
  47. }
  48. return NULL;
  49. }
  50. struct atm_dev *atm_dev_lookup(int number)
  51. {
  52. struct atm_dev *dev;
  53. down(&atm_dev_mutex);
  54. dev = __atm_dev_lookup(number);
  55. up(&atm_dev_mutex);
  56. return dev;
  57. }
  58. struct atm_dev *atm_dev_register(const char *type, const struct atmdev_ops *ops,
  59. int number, unsigned long *flags)
  60. {
  61. struct atm_dev *dev, *inuse;
  62. dev = __alloc_atm_dev(type);
  63. if (!dev) {
  64. printk(KERN_ERR "atm_dev_register: no space for dev %s\n",
  65. type);
  66. return NULL;
  67. }
  68. down(&atm_dev_mutex);
  69. if (number != -1) {
  70. if ((inuse = __atm_dev_lookup(number))) {
  71. atm_dev_put(inuse);
  72. up(&atm_dev_mutex);
  73. kfree(dev);
  74. return NULL;
  75. }
  76. dev->number = number;
  77. } else {
  78. dev->number = 0;
  79. while ((inuse = __atm_dev_lookup(dev->number))) {
  80. atm_dev_put(inuse);
  81. dev->number++;
  82. }
  83. }
  84. dev->ops = ops;
  85. if (flags)
  86. dev->flags = *flags;
  87. else
  88. memset(&dev->flags, 0, sizeof(dev->flags));
  89. memset(&dev->stats, 0, sizeof(dev->stats));
  90. atomic_set(&dev->refcnt, 1);
  91. if (atm_proc_dev_register(dev) < 0) {
  92. printk(KERN_ERR "atm_dev_register: "
  93. "atm_proc_dev_register failed for dev %s\n",
  94. type);
  95. up(&atm_dev_mutex);
  96. kfree(dev);
  97. return NULL;
  98. }
  99. list_add_tail(&dev->dev_list, &atm_devs);
  100. up(&atm_dev_mutex);
  101. return dev;
  102. }
  103. void atm_dev_deregister(struct atm_dev *dev)
  104. {
  105. BUG_ON(test_bit(ATM_DF_REMOVED, &dev->flags));
  106. set_bit(ATM_DF_REMOVED, &dev->flags);
  107. /*
  108. * if we remove current device from atm_devs list, new device
  109. * with same number can appear, such we need deregister proc,
  110. * release async all vccs and remove them from vccs list too
  111. */
  112. down(&atm_dev_mutex);
  113. list_del(&dev->dev_list);
  114. up(&atm_dev_mutex);
  115. atm_dev_release_vccs(dev);
  116. atm_proc_dev_deregister(dev);
  117. atm_dev_put(dev);
  118. }
  119. static void copy_aal_stats(struct k_atm_aal_stats *from,
  120. struct atm_aal_stats *to)
  121. {
  122. #define __HANDLE_ITEM(i) to->i = atomic_read(&from->i)
  123. __AAL_STAT_ITEMS
  124. #undef __HANDLE_ITEM
  125. }
  126. static void subtract_aal_stats(struct k_atm_aal_stats *from,
  127. struct atm_aal_stats *to)
  128. {
  129. #define __HANDLE_ITEM(i) atomic_sub(to->i, &from->i)
  130. __AAL_STAT_ITEMS
  131. #undef __HANDLE_ITEM
  132. }
  133. static int fetch_stats(struct atm_dev *dev, struct atm_dev_stats __user *arg, int zero)
  134. {
  135. struct atm_dev_stats tmp;
  136. int error = 0;
  137. copy_aal_stats(&dev->stats.aal0, &tmp.aal0);
  138. copy_aal_stats(&dev->stats.aal34, &tmp.aal34);
  139. copy_aal_stats(&dev->stats.aal5, &tmp.aal5);
  140. if (arg)
  141. error = copy_to_user(arg, &tmp, sizeof(tmp));
  142. if (zero && !error) {
  143. subtract_aal_stats(&dev->stats.aal0, &tmp.aal0);
  144. subtract_aal_stats(&dev->stats.aal34, &tmp.aal34);
  145. subtract_aal_stats(&dev->stats.aal5, &tmp.aal5);
  146. }
  147. return error ? -EFAULT : 0;
  148. }
  149. int atm_dev_ioctl(unsigned int cmd, void __user *arg)
  150. {
  151. void __user *buf;
  152. int error, len, number, size = 0;
  153. struct atm_dev *dev;
  154. struct list_head *p;
  155. int *tmp_buf, *tmp_p;
  156. struct atm_iobuf __user *iobuf = arg;
  157. struct atmif_sioc __user *sioc = arg;
  158. switch (cmd) {
  159. case ATM_GETNAMES:
  160. if (get_user(buf, &iobuf->buffer))
  161. return -EFAULT;
  162. if (get_user(len, &iobuf->length))
  163. return -EFAULT;
  164. down(&atm_dev_mutex);
  165. list_for_each(p, &atm_devs)
  166. size += sizeof(int);
  167. if (size > len) {
  168. up(&atm_dev_mutex);
  169. return -E2BIG;
  170. }
  171. tmp_buf = kmalloc(size, GFP_ATOMIC);
  172. if (!tmp_buf) {
  173. up(&atm_dev_mutex);
  174. return -ENOMEM;
  175. }
  176. tmp_p = tmp_buf;
  177. list_for_each(p, &atm_devs) {
  178. dev = list_entry(p, struct atm_dev, dev_list);
  179. *tmp_p++ = dev->number;
  180. }
  181. up(&atm_dev_mutex);
  182. error = ((copy_to_user(buf, tmp_buf, size)) ||
  183. put_user(size, &iobuf->length))
  184. ? -EFAULT : 0;
  185. kfree(tmp_buf);
  186. return error;
  187. default:
  188. break;
  189. }
  190. if (get_user(buf, &sioc->arg))
  191. return -EFAULT;
  192. if (get_user(len, &sioc->length))
  193. return -EFAULT;
  194. if (get_user(number, &sioc->number))
  195. return -EFAULT;
  196. if (!(dev = try_then_request_module(atm_dev_lookup(number),
  197. "atm-device-%d", number)))
  198. return -ENODEV;
  199. switch (cmd) {
  200. case ATM_GETTYPE:
  201. size = strlen(dev->type) + 1;
  202. if (copy_to_user(buf, dev->type, size)) {
  203. error = -EFAULT;
  204. goto done;
  205. }
  206. break;
  207. case ATM_GETESI:
  208. size = ESI_LEN;
  209. if (copy_to_user(buf, dev->esi, size)) {
  210. error = -EFAULT;
  211. goto done;
  212. }
  213. break;
  214. case ATM_SETESI:
  215. {
  216. int i;
  217. for (i = 0; i < ESI_LEN; i++)
  218. if (dev->esi[i]) {
  219. error = -EEXIST;
  220. goto done;
  221. }
  222. }
  223. /* fall through */
  224. case ATM_SETESIF:
  225. {
  226. unsigned char esi[ESI_LEN];
  227. if (!capable(CAP_NET_ADMIN)) {
  228. error = -EPERM;
  229. goto done;
  230. }
  231. if (copy_from_user(esi, buf, ESI_LEN)) {
  232. error = -EFAULT;
  233. goto done;
  234. }
  235. memcpy(dev->esi, esi, ESI_LEN);
  236. error = ESI_LEN;
  237. goto done;
  238. }
  239. case ATM_GETSTATZ:
  240. if (!capable(CAP_NET_ADMIN)) {
  241. error = -EPERM;
  242. goto done;
  243. }
  244. /* fall through */
  245. case ATM_GETSTAT:
  246. size = sizeof(struct atm_dev_stats);
  247. error = fetch_stats(dev, buf, cmd == ATM_GETSTATZ);
  248. if (error)
  249. goto done;
  250. break;
  251. case ATM_GETCIRANGE:
  252. size = sizeof(struct atm_cirange);
  253. if (copy_to_user(buf, &dev->ci_range, size)) {
  254. error = -EFAULT;
  255. goto done;
  256. }
  257. break;
  258. case ATM_GETLINKRATE:
  259. size = sizeof(int);
  260. if (copy_to_user(buf, &dev->link_rate, size)) {
  261. error = -EFAULT;
  262. goto done;
  263. }
  264. break;
  265. case ATM_RSTADDR:
  266. if (!capable(CAP_NET_ADMIN)) {
  267. error = -EPERM;
  268. goto done;
  269. }
  270. atm_reset_addr(dev, ATM_ADDR_LOCAL);
  271. break;
  272. case ATM_ADDADDR:
  273. case ATM_DELADDR:
  274. case ATM_ADDLECSADDR:
  275. case ATM_DELLECSADDR:
  276. if (!capable(CAP_NET_ADMIN)) {
  277. error = -EPERM;
  278. goto done;
  279. }
  280. {
  281. struct sockaddr_atmsvc addr;
  282. if (copy_from_user(&addr, buf, sizeof(addr))) {
  283. error = -EFAULT;
  284. goto done;
  285. }
  286. if (cmd == ATM_ADDADDR || cmd == ATM_ADDLECSADDR)
  287. error = atm_add_addr(dev, &addr,
  288. (cmd == ATM_ADDADDR ?
  289. ATM_ADDR_LOCAL : ATM_ADDR_LECS));
  290. else
  291. error = atm_del_addr(dev, &addr,
  292. (cmd == ATM_DELADDR ?
  293. ATM_ADDR_LOCAL : ATM_ADDR_LECS));
  294. goto done;
  295. }
  296. case ATM_GETADDR:
  297. case ATM_GETLECSADDR:
  298. error = atm_get_addr(dev, buf, len,
  299. (cmd == ATM_GETADDR ?
  300. ATM_ADDR_LOCAL : ATM_ADDR_LECS));
  301. if (error < 0)
  302. goto done;
  303. size = error;
  304. /* may return 0, but later on size == 0 means "don't
  305. write the length" */
  306. error = put_user(size, &sioc->length)
  307. ? -EFAULT : 0;
  308. goto done;
  309. case ATM_SETLOOP:
  310. if (__ATM_LM_XTRMT((int) (unsigned long) buf) &&
  311. __ATM_LM_XTLOC((int) (unsigned long) buf) >
  312. __ATM_LM_XTRMT((int) (unsigned long) buf)) {
  313. error = -EINVAL;
  314. goto done;
  315. }
  316. /* fall through */
  317. case ATM_SETCIRANGE:
  318. case SONET_GETSTATZ:
  319. case SONET_SETDIAG:
  320. case SONET_CLRDIAG:
  321. case SONET_SETFRAMING:
  322. if (!capable(CAP_NET_ADMIN)) {
  323. error = -EPERM;
  324. goto done;
  325. }
  326. /* fall through */
  327. default:
  328. if (!dev->ops->ioctl) {
  329. error = -EINVAL;
  330. goto done;
  331. }
  332. size = dev->ops->ioctl(dev, cmd, buf);
  333. if (size < 0) {
  334. error = (size == -ENOIOCTLCMD ? -EINVAL : size);
  335. goto done;
  336. }
  337. }
  338. if (size)
  339. error = put_user(size, &sioc->length)
  340. ? -EFAULT : 0;
  341. else
  342. error = 0;
  343. done:
  344. atm_dev_put(dev);
  345. return error;
  346. }
  347. static __inline__ void *dev_get_idx(loff_t left)
  348. {
  349. struct list_head *p;
  350. list_for_each(p, &atm_devs) {
  351. if (!--left)
  352. break;
  353. }
  354. return (p != &atm_devs) ? p : NULL;
  355. }
  356. void *atm_dev_seq_start(struct seq_file *seq, loff_t *pos)
  357. {
  358. down(&atm_dev_mutex);
  359. return *pos ? dev_get_idx(*pos) : (void *) 1;
  360. }
  361. void atm_dev_seq_stop(struct seq_file *seq, void *v)
  362. {
  363. up(&atm_dev_mutex);
  364. }
  365. void *atm_dev_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  366. {
  367. ++*pos;
  368. v = (v == (void *)1) ? atm_devs.next : ((struct list_head *)v)->next;
  369. return (v == &atm_devs) ? NULL : v;
  370. }
  371. EXPORT_SYMBOL(atm_dev_register);
  372. EXPORT_SYMBOL(atm_dev_deregister);
  373. EXPORT_SYMBOL(atm_dev_lookup);