resources.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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/ctype.h>
  8. #include <linux/string.h>
  9. #include <linux/atmdev.h>
  10. #include <linux/sonet.h>
  11. #include <linux/kernel.h> /* for barrier */
  12. #include <linux/module.h>
  13. #include <linux/bitops.h>
  14. #include <linux/capability.h>
  15. #include <linux/delay.h>
  16. #include <linux/mutex.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. DEFINE_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. mutex_lock(&atm_dev_mutex);
  55. dev = __atm_dev_lookup(number);
  56. mutex_unlock(&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. mutex_lock(&atm_dev_mutex);
  70. if (number != -1) {
  71. if ((inuse = __atm_dev_lookup(number))) {
  72. atm_dev_put(inuse);
  73. mutex_unlock(&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. goto out_fail;
  97. }
  98. if (atm_register_sysfs(dev) < 0) {
  99. printk(KERN_ERR "atm_dev_register: "
  100. "atm_register_sysfs failed for dev %s\n",
  101. type);
  102. atm_proc_dev_deregister(dev);
  103. goto out_fail;
  104. }
  105. list_add_tail(&dev->dev_list, &atm_devs);
  106. out:
  107. mutex_unlock(&atm_dev_mutex);
  108. return dev;
  109. out_fail:
  110. kfree(dev);
  111. dev = NULL;
  112. goto out;
  113. }
  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. 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, int zero)
  146. {
  147. struct atm_dev_stats tmp;
  148. int error = 0;
  149. copy_aal_stats(&dev->stats.aal0, &tmp.aal0);
  150. copy_aal_stats(&dev->stats.aal34, &tmp.aal34);
  151. copy_aal_stats(&dev->stats.aal5, &tmp.aal5);
  152. if (arg)
  153. error = copy_to_user(arg, &tmp, sizeof(tmp));
  154. if (zero && !error) {
  155. subtract_aal_stats(&dev->stats.aal0, &tmp.aal0);
  156. subtract_aal_stats(&dev->stats.aal34, &tmp.aal34);
  157. subtract_aal_stats(&dev->stats.aal5, &tmp.aal5);
  158. }
  159. return error ? -EFAULT : 0;
  160. }
  161. int atm_dev_ioctl(unsigned int cmd, void __user *arg)
  162. {
  163. void __user *buf;
  164. int error, len, number, size = 0;
  165. struct atm_dev *dev;
  166. struct list_head *p;
  167. int *tmp_buf, *tmp_p;
  168. struct atm_iobuf __user *iobuf = arg;
  169. struct atmif_sioc __user *sioc = arg;
  170. switch (cmd) {
  171. case ATM_GETNAMES:
  172. if (get_user(buf, &iobuf->buffer))
  173. return -EFAULT;
  174. if (get_user(len, &iobuf->length))
  175. return -EFAULT;
  176. mutex_lock(&atm_dev_mutex);
  177. list_for_each(p, &atm_devs)
  178. size += sizeof(int);
  179. if (size > len) {
  180. mutex_unlock(&atm_dev_mutex);
  181. return -E2BIG;
  182. }
  183. tmp_buf = kmalloc(size, GFP_ATOMIC);
  184. if (!tmp_buf) {
  185. mutex_unlock(&atm_dev_mutex);
  186. return -ENOMEM;
  187. }
  188. tmp_p = tmp_buf;
  189. list_for_each(p, &atm_devs) {
  190. dev = list_entry(p, struct atm_dev, dev_list);
  191. *tmp_p++ = dev->number;
  192. }
  193. mutex_unlock(&atm_dev_mutex);
  194. error = ((copy_to_user(buf, tmp_buf, size)) ||
  195. put_user(size, &iobuf->length))
  196. ? -EFAULT : 0;
  197. kfree(tmp_buf);
  198. return error;
  199. default:
  200. break;
  201. }
  202. if (get_user(buf, &sioc->arg))
  203. return -EFAULT;
  204. if (get_user(len, &sioc->length))
  205. return -EFAULT;
  206. if (get_user(number, &sioc->number))
  207. return -EFAULT;
  208. if (!(dev = try_then_request_module(atm_dev_lookup(number),
  209. "atm-device-%d", number)))
  210. return -ENODEV;
  211. switch (cmd) {
  212. case ATM_GETTYPE:
  213. size = strlen(dev->type) + 1;
  214. if (copy_to_user(buf, dev->type, size)) {
  215. error = -EFAULT;
  216. goto done;
  217. }
  218. break;
  219. case ATM_GETESI:
  220. size = ESI_LEN;
  221. if (copy_to_user(buf, dev->esi, size)) {
  222. error = -EFAULT;
  223. goto done;
  224. }
  225. break;
  226. case ATM_SETESI:
  227. {
  228. int i;
  229. for (i = 0; i < ESI_LEN; i++)
  230. if (dev->esi[i]) {
  231. error = -EEXIST;
  232. goto done;
  233. }
  234. }
  235. /* fall through */
  236. case ATM_SETESIF:
  237. {
  238. unsigned char esi[ESI_LEN];
  239. if (!capable(CAP_NET_ADMIN)) {
  240. error = -EPERM;
  241. goto done;
  242. }
  243. if (copy_from_user(esi, buf, ESI_LEN)) {
  244. error = -EFAULT;
  245. goto done;
  246. }
  247. memcpy(dev->esi, esi, ESI_LEN);
  248. error = ESI_LEN;
  249. goto done;
  250. }
  251. case ATM_GETSTATZ:
  252. if (!capable(CAP_NET_ADMIN)) {
  253. error = -EPERM;
  254. goto done;
  255. }
  256. /* fall through */
  257. case ATM_GETSTAT:
  258. size = sizeof(struct atm_dev_stats);
  259. error = fetch_stats(dev, buf, cmd == ATM_GETSTATZ);
  260. if (error)
  261. goto done;
  262. break;
  263. case ATM_GETCIRANGE:
  264. size = sizeof(struct atm_cirange);
  265. if (copy_to_user(buf, &dev->ci_range, size)) {
  266. error = -EFAULT;
  267. goto done;
  268. }
  269. break;
  270. case ATM_GETLINKRATE:
  271. size = sizeof(int);
  272. if (copy_to_user(buf, &dev->link_rate, size)) {
  273. error = -EFAULT;
  274. goto done;
  275. }
  276. break;
  277. case ATM_RSTADDR:
  278. if (!capable(CAP_NET_ADMIN)) {
  279. error = -EPERM;
  280. goto done;
  281. }
  282. atm_reset_addr(dev, ATM_ADDR_LOCAL);
  283. break;
  284. case ATM_ADDADDR:
  285. case ATM_DELADDR:
  286. case ATM_ADDLECSADDR:
  287. case ATM_DELLECSADDR:
  288. if (!capable(CAP_NET_ADMIN)) {
  289. error = -EPERM;
  290. goto done;
  291. }
  292. {
  293. struct sockaddr_atmsvc addr;
  294. if (copy_from_user(&addr, buf, sizeof(addr))) {
  295. error = -EFAULT;
  296. goto done;
  297. }
  298. if (cmd == ATM_ADDADDR || cmd == ATM_ADDLECSADDR)
  299. error = atm_add_addr(dev, &addr,
  300. (cmd == ATM_ADDADDR ?
  301. ATM_ADDR_LOCAL : ATM_ADDR_LECS));
  302. else
  303. error = atm_del_addr(dev, &addr,
  304. (cmd == ATM_DELADDR ?
  305. ATM_ADDR_LOCAL : ATM_ADDR_LECS));
  306. goto done;
  307. }
  308. case ATM_GETADDR:
  309. case ATM_GETLECSADDR:
  310. error = atm_get_addr(dev, buf, len,
  311. (cmd == ATM_GETADDR ?
  312. ATM_ADDR_LOCAL : ATM_ADDR_LECS));
  313. if (error < 0)
  314. goto done;
  315. size = error;
  316. /* may return 0, but later on size == 0 means "don't
  317. write the length" */
  318. error = put_user(size, &sioc->length)
  319. ? -EFAULT : 0;
  320. goto done;
  321. case ATM_SETLOOP:
  322. if (__ATM_LM_XTRMT((int) (unsigned long) buf) &&
  323. __ATM_LM_XTLOC((int) (unsigned long) buf) >
  324. __ATM_LM_XTRMT((int) (unsigned long) buf)) {
  325. error = -EINVAL;
  326. goto done;
  327. }
  328. /* fall through */
  329. case ATM_SETCIRANGE:
  330. case SONET_GETSTATZ:
  331. case SONET_SETDIAG:
  332. case SONET_CLRDIAG:
  333. case SONET_SETFRAMING:
  334. if (!capable(CAP_NET_ADMIN)) {
  335. error = -EPERM;
  336. goto done;
  337. }
  338. /* fall through */
  339. default:
  340. if (!dev->ops->ioctl) {
  341. error = -EINVAL;
  342. goto done;
  343. }
  344. size = dev->ops->ioctl(dev, cmd, buf);
  345. if (size < 0) {
  346. error = (size == -ENOIOCTLCMD ? -EINVAL : size);
  347. goto done;
  348. }
  349. }
  350. if (size)
  351. error = put_user(size, &sioc->length)
  352. ? -EFAULT : 0;
  353. else
  354. error = 0;
  355. done:
  356. atm_dev_put(dev);
  357. return error;
  358. }
  359. static __inline__ void *dev_get_idx(loff_t left)
  360. {
  361. struct list_head *p;
  362. list_for_each(p, &atm_devs) {
  363. if (!--left)
  364. break;
  365. }
  366. return (p != &atm_devs) ? p : NULL;
  367. }
  368. void *atm_dev_seq_start(struct seq_file *seq, loff_t *pos)
  369. {
  370. mutex_lock(&atm_dev_mutex);
  371. return *pos ? dev_get_idx(*pos) : (void *) 1;
  372. }
  373. void atm_dev_seq_stop(struct seq_file *seq, void *v)
  374. {
  375. mutex_unlock(&atm_dev_mutex);
  376. }
  377. void *atm_dev_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  378. {
  379. ++*pos;
  380. v = (v == (void *)1) ? atm_devs.next : ((struct list_head *)v)->next;
  381. return (v == &atm_devs) ? NULL : v;
  382. }
  383. EXPORT_SYMBOL(atm_dev_register);
  384. EXPORT_SYMBOL(atm_dev_deregister);
  385. EXPORT_SYMBOL(atm_dev_lookup);