resources.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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. #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
  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 = kzalloc(sizeof(*dev), GFP_KERNEL);
  28. if (!dev)
  29. return NULL;
  30. dev->type = type;
  31. dev->signal = ATM_PHY_SIG_UNKNOWN;
  32. dev->link_rate = ATM_OC3_PCR;
  33. spin_lock_init(&dev->lock);
  34. INIT_LIST_HEAD(&dev->local);
  35. INIT_LIST_HEAD(&dev->lecs);
  36. return dev;
  37. }
  38. static struct atm_dev *__atm_dev_lookup(int number)
  39. {
  40. struct atm_dev *dev;
  41. struct list_head *p;
  42. list_for_each(p, &atm_devs) {
  43. dev = list_entry(p, struct atm_dev, dev_list);
  44. if (dev->number == number) {
  45. atm_dev_hold(dev);
  46. return dev;
  47. }
  48. }
  49. return NULL;
  50. }
  51. struct atm_dev *atm_dev_lookup(int number)
  52. {
  53. struct atm_dev *dev;
  54. mutex_lock(&atm_dev_mutex);
  55. dev = __atm_dev_lookup(number);
  56. mutex_unlock(&atm_dev_mutex);
  57. return dev;
  58. }
  59. struct atm_dev *atm_dev_register(const char *type, const struct atmdev_ops *ops,
  60. int number, unsigned long *flags)
  61. {
  62. struct atm_dev *dev, *inuse;
  63. dev = __alloc_atm_dev(type);
  64. if (!dev) {
  65. pr_err("no space for dev %s\n", 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. pr_err("atm_proc_dev_register failed for dev %s\n", type);
  93. goto out_fail;
  94. }
  95. if (atm_register_sysfs(dev) < 0) {
  96. pr_err("atm_register_sysfs failed for dev %s\n", type);
  97. atm_proc_dev_deregister(dev);
  98. goto out_fail;
  99. }
  100. list_add_tail(&dev->dev_list, &atm_devs);
  101. out:
  102. mutex_unlock(&atm_dev_mutex);
  103. return dev;
  104. out_fail:
  105. kfree(dev);
  106. dev = NULL;
  107. goto out;
  108. }
  109. void atm_dev_deregister(struct atm_dev *dev)
  110. {
  111. BUG_ON(test_bit(ATM_DF_REMOVED, &dev->flags));
  112. set_bit(ATM_DF_REMOVED, &dev->flags);
  113. /*
  114. * if we remove current device from atm_devs list, new device
  115. * with same number can appear, such we need deregister proc,
  116. * release async all vccs and remove them from vccs list too
  117. */
  118. mutex_lock(&atm_dev_mutex);
  119. list_del(&dev->dev_list);
  120. mutex_unlock(&atm_dev_mutex);
  121. atm_dev_release_vccs(dev);
  122. atm_unregister_sysfs(dev);
  123. atm_proc_dev_deregister(dev);
  124. atm_dev_put(dev);
  125. }
  126. static void copy_aal_stats(struct k_atm_aal_stats *from,
  127. struct atm_aal_stats *to)
  128. {
  129. #define __HANDLE_ITEM(i) to->i = atomic_read(&from->i)
  130. __AAL_STAT_ITEMS
  131. #undef __HANDLE_ITEM
  132. }
  133. static void subtract_aal_stats(struct k_atm_aal_stats *from,
  134. struct atm_aal_stats *to)
  135. {
  136. #define __HANDLE_ITEM(i) atomic_sub(to->i, &from->i)
  137. __AAL_STAT_ITEMS
  138. #undef __HANDLE_ITEM
  139. }
  140. static int fetch_stats(struct atm_dev *dev, struct atm_dev_stats __user *arg, int zero)
  141. {
  142. struct atm_dev_stats tmp;
  143. int error = 0;
  144. copy_aal_stats(&dev->stats.aal0, &tmp.aal0);
  145. copy_aal_stats(&dev->stats.aal34, &tmp.aal34);
  146. copy_aal_stats(&dev->stats.aal5, &tmp.aal5);
  147. if (arg)
  148. error = copy_to_user(arg, &tmp, sizeof(tmp));
  149. if (zero && !error) {
  150. subtract_aal_stats(&dev->stats.aal0, &tmp.aal0);
  151. subtract_aal_stats(&dev->stats.aal34, &tmp.aal34);
  152. subtract_aal_stats(&dev->stats.aal5, &tmp.aal5);
  153. }
  154. return error ? -EFAULT : 0;
  155. }
  156. int atm_dev_ioctl(unsigned int cmd, void __user *arg, int compat)
  157. {
  158. void __user *buf;
  159. int error, len, number, size = 0;
  160. struct atm_dev *dev;
  161. struct list_head *p;
  162. int *tmp_buf, *tmp_p;
  163. int __user *sioc_len;
  164. int __user *iobuf_len;
  165. #ifndef CONFIG_COMPAT
  166. compat = 0; /* Just so the compiler _knows_ */
  167. #endif
  168. switch (cmd) {
  169. case ATM_GETNAMES:
  170. if (compat) {
  171. #ifdef CONFIG_COMPAT
  172. struct compat_atm_iobuf __user *ciobuf = arg;
  173. compat_uptr_t cbuf;
  174. iobuf_len = &ciobuf->length;
  175. if (get_user(cbuf, &ciobuf->buffer))
  176. return -EFAULT;
  177. buf = compat_ptr(cbuf);
  178. #endif
  179. } else {
  180. struct atm_iobuf __user *iobuf = arg;
  181. iobuf_len = &iobuf->length;
  182. if (get_user(buf, &iobuf->buffer))
  183. return -EFAULT;
  184. }
  185. if (get_user(len, iobuf_len))
  186. return -EFAULT;
  187. mutex_lock(&atm_dev_mutex);
  188. list_for_each(p, &atm_devs)
  189. size += sizeof(int);
  190. if (size > len) {
  191. mutex_unlock(&atm_dev_mutex);
  192. return -E2BIG;
  193. }
  194. tmp_buf = kmalloc(size, GFP_ATOMIC);
  195. if (!tmp_buf) {
  196. mutex_unlock(&atm_dev_mutex);
  197. return -ENOMEM;
  198. }
  199. tmp_p = tmp_buf;
  200. list_for_each(p, &atm_devs) {
  201. dev = list_entry(p, struct atm_dev, dev_list);
  202. *tmp_p++ = dev->number;
  203. }
  204. mutex_unlock(&atm_dev_mutex);
  205. error = ((copy_to_user(buf, tmp_buf, size)) ||
  206. put_user(size, iobuf_len))
  207. ? -EFAULT : 0;
  208. kfree(tmp_buf);
  209. return error;
  210. default:
  211. break;
  212. }
  213. if (compat) {
  214. #ifdef CONFIG_COMPAT
  215. struct compat_atmif_sioc __user *csioc = arg;
  216. compat_uptr_t carg;
  217. sioc_len = &csioc->length;
  218. if (get_user(carg, &csioc->arg))
  219. return -EFAULT;
  220. buf = compat_ptr(carg);
  221. if (get_user(len, &csioc->length))
  222. return -EFAULT;
  223. if (get_user(number, &csioc->number))
  224. return -EFAULT;
  225. #endif
  226. } else {
  227. struct atmif_sioc __user *sioc = arg;
  228. sioc_len = &sioc->length;
  229. if (get_user(buf, &sioc->arg))
  230. return -EFAULT;
  231. if (get_user(len, &sioc->length))
  232. return -EFAULT;
  233. if (get_user(number, &sioc->number))
  234. return -EFAULT;
  235. }
  236. if (!(dev = try_then_request_module(atm_dev_lookup(number),
  237. "atm-device-%d", number)))
  238. return -ENODEV;
  239. switch (cmd) {
  240. case ATM_GETTYPE:
  241. size = strlen(dev->type) + 1;
  242. if (copy_to_user(buf, dev->type, size)) {
  243. error = -EFAULT;
  244. goto done;
  245. }
  246. break;
  247. case ATM_GETESI:
  248. size = ESI_LEN;
  249. if (copy_to_user(buf, dev->esi, size)) {
  250. error = -EFAULT;
  251. goto done;
  252. }
  253. break;
  254. case ATM_SETESI:
  255. {
  256. int i;
  257. for (i = 0; i < ESI_LEN; i++)
  258. if (dev->esi[i]) {
  259. error = -EEXIST;
  260. goto done;
  261. }
  262. }
  263. /* fall through */
  264. case ATM_SETESIF:
  265. {
  266. unsigned char esi[ESI_LEN];
  267. if (!capable(CAP_NET_ADMIN)) {
  268. error = -EPERM;
  269. goto done;
  270. }
  271. if (copy_from_user(esi, buf, ESI_LEN)) {
  272. error = -EFAULT;
  273. goto done;
  274. }
  275. memcpy(dev->esi, esi, ESI_LEN);
  276. error = ESI_LEN;
  277. goto done;
  278. }
  279. case ATM_GETSTATZ:
  280. if (!capable(CAP_NET_ADMIN)) {
  281. error = -EPERM;
  282. goto done;
  283. }
  284. /* fall through */
  285. case ATM_GETSTAT:
  286. size = sizeof(struct atm_dev_stats);
  287. error = fetch_stats(dev, buf, cmd == ATM_GETSTATZ);
  288. if (error)
  289. goto done;
  290. break;
  291. case ATM_GETCIRANGE:
  292. size = sizeof(struct atm_cirange);
  293. if (copy_to_user(buf, &dev->ci_range, size)) {
  294. error = -EFAULT;
  295. goto done;
  296. }
  297. break;
  298. case ATM_GETLINKRATE:
  299. size = sizeof(int);
  300. if (copy_to_user(buf, &dev->link_rate, size)) {
  301. error = -EFAULT;
  302. goto done;
  303. }
  304. break;
  305. case ATM_RSTADDR:
  306. if (!capable(CAP_NET_ADMIN)) {
  307. error = -EPERM;
  308. goto done;
  309. }
  310. atm_reset_addr(dev, ATM_ADDR_LOCAL);
  311. break;
  312. case ATM_ADDADDR:
  313. case ATM_DELADDR:
  314. case ATM_ADDLECSADDR:
  315. case ATM_DELLECSADDR:
  316. if (!capable(CAP_NET_ADMIN)) {
  317. error = -EPERM;
  318. goto done;
  319. }
  320. {
  321. struct sockaddr_atmsvc addr;
  322. if (copy_from_user(&addr, buf, sizeof(addr))) {
  323. error = -EFAULT;
  324. goto done;
  325. }
  326. if (cmd == ATM_ADDADDR || cmd == ATM_ADDLECSADDR)
  327. error = atm_add_addr(dev, &addr,
  328. (cmd == ATM_ADDADDR ?
  329. ATM_ADDR_LOCAL : ATM_ADDR_LECS));
  330. else
  331. error = atm_del_addr(dev, &addr,
  332. (cmd == ATM_DELADDR ?
  333. ATM_ADDR_LOCAL : ATM_ADDR_LECS));
  334. goto done;
  335. }
  336. case ATM_GETADDR:
  337. case ATM_GETLECSADDR:
  338. error = atm_get_addr(dev, buf, len,
  339. (cmd == ATM_GETADDR ?
  340. ATM_ADDR_LOCAL : ATM_ADDR_LECS));
  341. if (error < 0)
  342. goto done;
  343. size = error;
  344. /* may return 0, but later on size == 0 means "don't
  345. write the length" */
  346. error = put_user(size, sioc_len)
  347. ? -EFAULT : 0;
  348. goto done;
  349. case ATM_SETLOOP:
  350. if (__ATM_LM_XTRMT((int) (unsigned long) buf) &&
  351. __ATM_LM_XTLOC((int) (unsigned long) buf) >
  352. __ATM_LM_XTRMT((int) (unsigned long) buf)) {
  353. error = -EINVAL;
  354. goto done;
  355. }
  356. /* fall through */
  357. case ATM_SETCIRANGE:
  358. case SONET_GETSTATZ:
  359. case SONET_SETDIAG:
  360. case SONET_CLRDIAG:
  361. case SONET_SETFRAMING:
  362. if (!capable(CAP_NET_ADMIN)) {
  363. error = -EPERM;
  364. goto done;
  365. }
  366. /* fall through */
  367. default:
  368. if (compat) {
  369. #ifdef CONFIG_COMPAT
  370. if (!dev->ops->compat_ioctl) {
  371. error = -EINVAL;
  372. goto done;
  373. }
  374. size = dev->ops->compat_ioctl(dev, cmd, buf);
  375. #endif
  376. } else {
  377. if (!dev->ops->ioctl) {
  378. error = -EINVAL;
  379. goto done;
  380. }
  381. size = dev->ops->ioctl(dev, cmd, buf);
  382. }
  383. if (size < 0) {
  384. error = (size == -ENOIOCTLCMD ? -EINVAL : size);
  385. goto done;
  386. }
  387. }
  388. if (size)
  389. error = put_user(size, sioc_len)
  390. ? -EFAULT : 0;
  391. else
  392. error = 0;
  393. done:
  394. atm_dev_put(dev);
  395. return error;
  396. }
  397. static __inline__ void *dev_get_idx(loff_t left)
  398. {
  399. struct list_head *p;
  400. list_for_each(p, &atm_devs) {
  401. if (!--left)
  402. break;
  403. }
  404. return (p != &atm_devs) ? p : NULL;
  405. }
  406. void *atm_dev_seq_start(struct seq_file *seq, loff_t *pos)
  407. {
  408. mutex_lock(&atm_dev_mutex);
  409. return *pos ? dev_get_idx(*pos) : SEQ_START_TOKEN;
  410. }
  411. void atm_dev_seq_stop(struct seq_file *seq, void *v)
  412. {
  413. mutex_unlock(&atm_dev_mutex);
  414. }
  415. void *atm_dev_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  416. {
  417. ++*pos;
  418. v = (v == SEQ_START_TOKEN)
  419. ? atm_devs.next : ((struct list_head *)v)->next;
  420. return (v == &atm_devs) ? NULL : v;
  421. }
  422. EXPORT_SYMBOL(atm_dev_register);
  423. EXPORT_SYMBOL(atm_dev_deregister);
  424. EXPORT_SYMBOL(atm_dev_lookup);