resources.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. goto out_fail;
  98. }
  99. if (atm_register_sysfs(dev) < 0) {
  100. printk(KERN_ERR "atm_dev_register: "
  101. "atm_register_sysfs failed for dev %s\n",
  102. type);
  103. atm_proc_dev_deregister(dev);
  104. goto out_fail;
  105. }
  106. list_add_tail(&dev->dev_list, &atm_devs);
  107. out:
  108. mutex_unlock(&atm_dev_mutex);
  109. return dev;
  110. out_fail:
  111. kfree(dev);
  112. dev = NULL;
  113. goto out;
  114. }
  115. void atm_dev_deregister(struct atm_dev *dev)
  116. {
  117. BUG_ON(test_bit(ATM_DF_REMOVED, &dev->flags));
  118. set_bit(ATM_DF_REMOVED, &dev->flags);
  119. /*
  120. * if we remove current device from atm_devs list, new device
  121. * with same number can appear, such we need deregister proc,
  122. * release async all vccs and remove them from vccs list too
  123. */
  124. mutex_lock(&atm_dev_mutex);
  125. list_del(&dev->dev_list);
  126. mutex_unlock(&atm_dev_mutex);
  127. atm_dev_release_vccs(dev);
  128. atm_unregister_sysfs(dev);
  129. atm_proc_dev_deregister(dev);
  130. atm_dev_put(dev);
  131. }
  132. static void copy_aal_stats(struct k_atm_aal_stats *from,
  133. struct atm_aal_stats *to)
  134. {
  135. #define __HANDLE_ITEM(i) to->i = atomic_read(&from->i)
  136. __AAL_STAT_ITEMS
  137. #undef __HANDLE_ITEM
  138. }
  139. static void subtract_aal_stats(struct k_atm_aal_stats *from,
  140. struct atm_aal_stats *to)
  141. {
  142. #define __HANDLE_ITEM(i) atomic_sub(to->i, &from->i)
  143. __AAL_STAT_ITEMS
  144. #undef __HANDLE_ITEM
  145. }
  146. static int fetch_stats(struct atm_dev *dev, struct atm_dev_stats __user *arg, int zero)
  147. {
  148. struct atm_dev_stats tmp;
  149. int error = 0;
  150. copy_aal_stats(&dev->stats.aal0, &tmp.aal0);
  151. copy_aal_stats(&dev->stats.aal34, &tmp.aal34);
  152. copy_aal_stats(&dev->stats.aal5, &tmp.aal5);
  153. if (arg)
  154. error = copy_to_user(arg, &tmp, sizeof(tmp));
  155. if (zero && !error) {
  156. subtract_aal_stats(&dev->stats.aal0, &tmp.aal0);
  157. subtract_aal_stats(&dev->stats.aal34, &tmp.aal34);
  158. subtract_aal_stats(&dev->stats.aal5, &tmp.aal5);
  159. }
  160. return error ? -EFAULT : 0;
  161. }
  162. int atm_dev_ioctl(unsigned int cmd, void __user *arg)
  163. {
  164. void __user *buf;
  165. int error, len, number, size = 0;
  166. struct atm_dev *dev;
  167. struct list_head *p;
  168. int *tmp_buf, *tmp_p;
  169. struct atm_iobuf __user *iobuf = arg;
  170. struct atmif_sioc __user *sioc = arg;
  171. switch (cmd) {
  172. case ATM_GETNAMES:
  173. if (get_user(buf, &iobuf->buffer))
  174. return -EFAULT;
  175. if (get_user(len, &iobuf->length))
  176. return -EFAULT;
  177. mutex_lock(&atm_dev_mutex);
  178. list_for_each(p, &atm_devs)
  179. size += sizeof(int);
  180. if (size > len) {
  181. mutex_unlock(&atm_dev_mutex);
  182. return -E2BIG;
  183. }
  184. tmp_buf = kmalloc(size, GFP_ATOMIC);
  185. if (!tmp_buf) {
  186. mutex_unlock(&atm_dev_mutex);
  187. return -ENOMEM;
  188. }
  189. tmp_p = tmp_buf;
  190. list_for_each(p, &atm_devs) {
  191. dev = list_entry(p, struct atm_dev, dev_list);
  192. *tmp_p++ = dev->number;
  193. }
  194. mutex_unlock(&atm_dev_mutex);
  195. error = ((copy_to_user(buf, tmp_buf, size)) ||
  196. put_user(size, &iobuf->length))
  197. ? -EFAULT : 0;
  198. kfree(tmp_buf);
  199. return error;
  200. default:
  201. break;
  202. }
  203. if (get_user(buf, &sioc->arg))
  204. return -EFAULT;
  205. if (get_user(len, &sioc->length))
  206. return -EFAULT;
  207. if (get_user(number, &sioc->number))
  208. return -EFAULT;
  209. if (!(dev = try_then_request_module(atm_dev_lookup(number),
  210. "atm-device-%d", 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, ATM_ADDR_LOCAL);
  284. break;
  285. case ATM_ADDADDR:
  286. case ATM_DELADDR:
  287. case ATM_ADDLECSADDR:
  288. case ATM_DELLECSADDR:
  289. if (!capable(CAP_NET_ADMIN)) {
  290. error = -EPERM;
  291. goto done;
  292. }
  293. {
  294. struct sockaddr_atmsvc addr;
  295. if (copy_from_user(&addr, buf, sizeof(addr))) {
  296. error = -EFAULT;
  297. goto done;
  298. }
  299. if (cmd == ATM_ADDADDR || cmd == ATM_ADDLECSADDR)
  300. error = atm_add_addr(dev, &addr,
  301. (cmd == ATM_ADDADDR ?
  302. ATM_ADDR_LOCAL : ATM_ADDR_LECS));
  303. else
  304. error = atm_del_addr(dev, &addr,
  305. (cmd == ATM_DELADDR ?
  306. ATM_ADDR_LOCAL : ATM_ADDR_LECS));
  307. goto done;
  308. }
  309. case ATM_GETADDR:
  310. case ATM_GETLECSADDR:
  311. error = atm_get_addr(dev, buf, len,
  312. (cmd == ATM_GETADDR ?
  313. ATM_ADDR_LOCAL : ATM_ADDR_LECS));
  314. if (error < 0)
  315. goto done;
  316. size = error;
  317. /* may return 0, but later on size == 0 means "don't
  318. write the length" */
  319. error = put_user(size, &sioc->length)
  320. ? -EFAULT : 0;
  321. goto done;
  322. case ATM_SETLOOP:
  323. if (__ATM_LM_XTRMT((int) (unsigned long) buf) &&
  324. __ATM_LM_XTLOC((int) (unsigned long) buf) >
  325. __ATM_LM_XTRMT((int) (unsigned long) buf)) {
  326. error = -EINVAL;
  327. goto done;
  328. }
  329. /* fall through */
  330. case ATM_SETCIRANGE:
  331. case SONET_GETSTATZ:
  332. case SONET_SETDIAG:
  333. case SONET_CLRDIAG:
  334. case SONET_SETFRAMING:
  335. if (!capable(CAP_NET_ADMIN)) {
  336. error = -EPERM;
  337. goto done;
  338. }
  339. /* fall through */
  340. default:
  341. if (!dev->ops->ioctl) {
  342. error = -EINVAL;
  343. goto done;
  344. }
  345. size = dev->ops->ioctl(dev, cmd, buf);
  346. if (size < 0) {
  347. error = (size == -ENOIOCTLCMD ? -EINVAL : size);
  348. goto done;
  349. }
  350. }
  351. if (size)
  352. error = put_user(size, &sioc->length)
  353. ? -EFAULT : 0;
  354. else
  355. error = 0;
  356. done:
  357. atm_dev_put(dev);
  358. return error;
  359. }
  360. static __inline__ void *dev_get_idx(loff_t left)
  361. {
  362. struct list_head *p;
  363. list_for_each(p, &atm_devs) {
  364. if (!--left)
  365. break;
  366. }
  367. return (p != &atm_devs) ? p : NULL;
  368. }
  369. void *atm_dev_seq_start(struct seq_file *seq, loff_t *pos)
  370. {
  371. mutex_lock(&atm_dev_mutex);
  372. return *pos ? dev_get_idx(*pos) : (void *) 1;
  373. }
  374. void atm_dev_seq_stop(struct seq_file *seq, void *v)
  375. {
  376. mutex_unlock(&atm_dev_mutex);
  377. }
  378. void *atm_dev_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  379. {
  380. ++*pos;
  381. v = (v == (void *)1) ? atm_devs.next : ((struct list_head *)v)->next;
  382. return (v == &atm_devs) ? NULL : v;
  383. }
  384. EXPORT_SYMBOL(atm_dev_register);
  385. EXPORT_SYMBOL(atm_dev_deregister);
  386. EXPORT_SYMBOL(atm_dev_lookup);