resources.c 9.2 KB

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