resources.c 9.6 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/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. DEFINE_SPINLOCK(atm_dev_lock);
  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->ops) && (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. spin_lock(&atm_dev_lock);
  54. dev = __atm_dev_lookup(number);
  55. spin_unlock(&atm_dev_lock);
  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. spin_lock(&atm_dev_lock);
  69. if (number != -1) {
  70. if ((inuse = __atm_dev_lookup(number))) {
  71. atm_dev_put(inuse);
  72. spin_unlock(&atm_dev_lock);
  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. list_add_tail(&dev->dev_list, &atm_devs);
  92. spin_unlock(&atm_dev_lock);
  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. spin_lock(&atm_dev_lock);
  98. list_del(&dev->dev_list);
  99. spin_unlock(&atm_dev_lock);
  100. kfree(dev);
  101. return NULL;
  102. }
  103. return dev;
  104. }
  105. void atm_dev_deregister(struct atm_dev *dev)
  106. {
  107. unsigned long warning_time;
  108. atm_proc_dev_deregister(dev);
  109. spin_lock(&atm_dev_lock);
  110. list_del(&dev->dev_list);
  111. spin_unlock(&atm_dev_lock);
  112. warning_time = jiffies;
  113. while (atomic_read(&dev->refcnt) != 1) {
  114. msleep(250);
  115. if ((jiffies - warning_time) > 10 * HZ) {
  116. printk(KERN_EMERG "atm_dev_deregister: waiting for "
  117. "dev %d to become free. Usage count = %d\n",
  118. dev->number, atomic_read(&dev->refcnt));
  119. warning_time = jiffies;
  120. }
  121. }
  122. kfree(dev);
  123. }
  124. void shutdown_atm_dev(struct atm_dev *dev)
  125. {
  126. if (atomic_read(&dev->refcnt) > 1) {
  127. set_bit(ATM_DF_CLOSE, &dev->flags);
  128. return;
  129. }
  130. if (dev->ops->dev_close)
  131. dev->ops->dev_close(dev);
  132. atm_dev_deregister(dev);
  133. }
  134. static void copy_aal_stats(struct k_atm_aal_stats *from,
  135. struct atm_aal_stats *to)
  136. {
  137. #define __HANDLE_ITEM(i) to->i = atomic_read(&from->i)
  138. __AAL_STAT_ITEMS
  139. #undef __HANDLE_ITEM
  140. }
  141. static void subtract_aal_stats(struct k_atm_aal_stats *from,
  142. struct atm_aal_stats *to)
  143. {
  144. #define __HANDLE_ITEM(i) atomic_sub(to->i, &from->i)
  145. __AAL_STAT_ITEMS
  146. #undef __HANDLE_ITEM
  147. }
  148. static int fetch_stats(struct atm_dev *dev, struct atm_dev_stats __user *arg, int zero)
  149. {
  150. struct atm_dev_stats tmp;
  151. int error = 0;
  152. copy_aal_stats(&dev->stats.aal0, &tmp.aal0);
  153. copy_aal_stats(&dev->stats.aal34, &tmp.aal34);
  154. copy_aal_stats(&dev->stats.aal5, &tmp.aal5);
  155. if (arg)
  156. error = copy_to_user(arg, &tmp, sizeof(tmp));
  157. if (zero && !error) {
  158. subtract_aal_stats(&dev->stats.aal0, &tmp.aal0);
  159. subtract_aal_stats(&dev->stats.aal34, &tmp.aal34);
  160. subtract_aal_stats(&dev->stats.aal5, &tmp.aal5);
  161. }
  162. return error ? -EFAULT : 0;
  163. }
  164. int atm_dev_ioctl(unsigned int cmd, void __user *arg)
  165. {
  166. void __user *buf;
  167. int error, len, number, size = 0;
  168. struct atm_dev *dev;
  169. struct list_head *p;
  170. int *tmp_buf, *tmp_p;
  171. struct atm_iobuf __user *iobuf = arg;
  172. struct atmif_sioc __user *sioc = arg;
  173. switch (cmd) {
  174. case ATM_GETNAMES:
  175. if (get_user(buf, &iobuf->buffer))
  176. return -EFAULT;
  177. if (get_user(len, &iobuf->length))
  178. return -EFAULT;
  179. spin_lock(&atm_dev_lock);
  180. list_for_each(p, &atm_devs)
  181. size += sizeof(int);
  182. if (size > len) {
  183. spin_unlock(&atm_dev_lock);
  184. return -E2BIG;
  185. }
  186. tmp_buf = kmalloc(size, GFP_ATOMIC);
  187. if (!tmp_buf) {
  188. spin_unlock(&atm_dev_lock);
  189. return -ENOMEM;
  190. }
  191. tmp_p = tmp_buf;
  192. list_for_each(p, &atm_devs) {
  193. dev = list_entry(p, struct atm_dev, dev_list);
  194. *tmp_p++ = dev->number;
  195. }
  196. spin_unlock(&atm_dev_lock);
  197. error = ((copy_to_user(buf, tmp_buf, size)) ||
  198. put_user(size, &iobuf->length))
  199. ? -EFAULT : 0;
  200. kfree(tmp_buf);
  201. return error;
  202. default:
  203. break;
  204. }
  205. if (get_user(buf, &sioc->arg))
  206. return -EFAULT;
  207. if (get_user(len, &sioc->length))
  208. return -EFAULT;
  209. if (get_user(number, &sioc->number))
  210. return -EFAULT;
  211. if (!(dev = atm_dev_lookup(number)))
  212. return -ENODEV;
  213. switch (cmd) {
  214. case ATM_GETTYPE:
  215. size = strlen(dev->type) + 1;
  216. if (copy_to_user(buf, dev->type, size)) {
  217. error = -EFAULT;
  218. goto done;
  219. }
  220. break;
  221. case ATM_GETESI:
  222. size = ESI_LEN;
  223. if (copy_to_user(buf, dev->esi, size)) {
  224. error = -EFAULT;
  225. goto done;
  226. }
  227. break;
  228. case ATM_SETESI:
  229. {
  230. int i;
  231. for (i = 0; i < ESI_LEN; i++)
  232. if (dev->esi[i]) {
  233. error = -EEXIST;
  234. goto done;
  235. }
  236. }
  237. /* fall through */
  238. case ATM_SETESIF:
  239. {
  240. unsigned char esi[ESI_LEN];
  241. if (!capable(CAP_NET_ADMIN)) {
  242. error = -EPERM;
  243. goto done;
  244. }
  245. if (copy_from_user(esi, buf, ESI_LEN)) {
  246. error = -EFAULT;
  247. goto done;
  248. }
  249. memcpy(dev->esi, esi, ESI_LEN);
  250. error = ESI_LEN;
  251. goto done;
  252. }
  253. case ATM_GETSTATZ:
  254. if (!capable(CAP_NET_ADMIN)) {
  255. error = -EPERM;
  256. goto done;
  257. }
  258. /* fall through */
  259. case ATM_GETSTAT:
  260. size = sizeof(struct atm_dev_stats);
  261. error = fetch_stats(dev, buf, cmd == ATM_GETSTATZ);
  262. if (error)
  263. goto done;
  264. break;
  265. case ATM_GETCIRANGE:
  266. size = sizeof(struct atm_cirange);
  267. if (copy_to_user(buf, &dev->ci_range, size)) {
  268. error = -EFAULT;
  269. goto done;
  270. }
  271. break;
  272. case ATM_GETLINKRATE:
  273. size = sizeof(int);
  274. if (copy_to_user(buf, &dev->link_rate, size)) {
  275. error = -EFAULT;
  276. goto done;
  277. }
  278. break;
  279. case ATM_RSTADDR:
  280. if (!capable(CAP_NET_ADMIN)) {
  281. error = -EPERM;
  282. goto done;
  283. }
  284. atm_reset_addr(dev, ATM_ADDR_LOCAL);
  285. break;
  286. case ATM_ADDADDR:
  287. case ATM_DELADDR:
  288. case ATM_ADDLECSADDR:
  289. case ATM_DELLECSADDR:
  290. if (!capable(CAP_NET_ADMIN)) {
  291. error = -EPERM;
  292. goto done;
  293. }
  294. {
  295. struct sockaddr_atmsvc addr;
  296. if (copy_from_user(&addr, buf, sizeof(addr))) {
  297. error = -EFAULT;
  298. goto done;
  299. }
  300. if (cmd == ATM_ADDADDR || cmd == ATM_ADDLECSADDR)
  301. error = atm_add_addr(dev, &addr,
  302. (cmd == ATM_ADDADDR ?
  303. ATM_ADDR_LOCAL : ATM_ADDR_LECS));
  304. else
  305. error = atm_del_addr(dev, &addr,
  306. (cmd == ATM_DELADDR ?
  307. ATM_ADDR_LOCAL : ATM_ADDR_LECS));
  308. goto done;
  309. }
  310. case ATM_GETADDR:
  311. case ATM_GETLECSADDR:
  312. error = atm_get_addr(dev, buf, len,
  313. (cmd == ATM_GETADDR ?
  314. ATM_ADDR_LOCAL : ATM_ADDR_LECS));
  315. if (error < 0)
  316. goto done;
  317. size = error;
  318. /* may return 0, but later on size == 0 means "don't
  319. write the length" */
  320. error = put_user(size, &sioc->length)
  321. ? -EFAULT : 0;
  322. goto done;
  323. case ATM_SETLOOP:
  324. if (__ATM_LM_XTRMT((int) (unsigned long) buf) &&
  325. __ATM_LM_XTLOC((int) (unsigned long) buf) >
  326. __ATM_LM_XTRMT((int) (unsigned long) buf)) {
  327. error = -EINVAL;
  328. goto done;
  329. }
  330. /* fall through */
  331. case ATM_SETCIRANGE:
  332. case SONET_GETSTATZ:
  333. case SONET_SETDIAG:
  334. case SONET_CLRDIAG:
  335. case SONET_SETFRAMING:
  336. if (!capable(CAP_NET_ADMIN)) {
  337. error = -EPERM;
  338. goto done;
  339. }
  340. /* fall through */
  341. default:
  342. if (!dev->ops->ioctl) {
  343. error = -EINVAL;
  344. goto done;
  345. }
  346. size = dev->ops->ioctl(dev, cmd, buf);
  347. if (size < 0) {
  348. error = (size == -ENOIOCTLCMD ? -EINVAL : size);
  349. goto done;
  350. }
  351. }
  352. if (size)
  353. error = put_user(size, &sioc->length)
  354. ? -EFAULT : 0;
  355. else
  356. error = 0;
  357. done:
  358. atm_dev_put(dev);
  359. return error;
  360. }
  361. static __inline__ void *dev_get_idx(loff_t left)
  362. {
  363. struct list_head *p;
  364. list_for_each(p, &atm_devs) {
  365. if (!--left)
  366. break;
  367. }
  368. return (p != &atm_devs) ? p : NULL;
  369. }
  370. void *atm_dev_seq_start(struct seq_file *seq, loff_t *pos)
  371. {
  372. spin_lock(&atm_dev_lock);
  373. return *pos ? dev_get_idx(*pos) : (void *) 1;
  374. }
  375. void atm_dev_seq_stop(struct seq_file *seq, void *v)
  376. {
  377. spin_unlock(&atm_dev_lock);
  378. }
  379. void *atm_dev_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  380. {
  381. ++*pos;
  382. v = (v == (void *)1) ? atm_devs.next : ((struct list_head *)v)->next;
  383. return (v == &atm_devs) ? NULL : v;
  384. }
  385. EXPORT_SYMBOL(atm_dev_register);
  386. EXPORT_SYMBOL(atm_dev_deregister);
  387. EXPORT_SYMBOL(atm_dev_lookup);
  388. EXPORT_SYMBOL(shutdown_atm_dev);