resources.c 9.3 KB

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