resources.c 9.1 KB

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