resources.c 9.2 KB

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