resources.c 9.4 KB

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