resources.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* net/atm/resources.c - Statically allocated resources */
  3. /* Written 1995-2000 by Werner Almesberger, EPFL LRC/ICA */
  4. /* Fixes
  5. * Arnaldo Carvalho de Melo <acme@conectiva.com.br>
  6. * 2002/01 - don't free the whole struct sock on sk->destruct time,
  7. * use the default destruct function initialized by sock_init_data */
  8. #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
  9. #include <linux/ctype.h>
  10. #include <linux/string.h>
  11. #include <linux/atmdev.h>
  12. #include <linux/sonet.h>
  13. #include <linux/kernel.h> /* for barrier */
  14. #include <linux/module.h>
  15. #include <linux/bitops.h>
  16. #include <linux/capability.h>
  17. #include <linux/delay.h>
  18. #include <linux/mutex.h>
  19. #include <linux/slab.h>
  20. #include <net/sock.h> /* for struct sock */
  21. #include "common.h"
  22. #include "resources.h"
  23. #include "addr.h"
  24. LIST_HEAD(atm_devs);
  25. DEFINE_MUTEX(atm_dev_mutex);
  26. static struct atm_dev *__alloc_atm_dev(const char *type)
  27. {
  28. struct atm_dev *dev;
  29. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  30. if (!dev)
  31. return NULL;
  32. dev->type = type;
  33. dev->signal = ATM_PHY_SIG_UNKNOWN;
  34. dev->link_rate = ATM_OC3_PCR;
  35. spin_lock_init(&dev->lock);
  36. INIT_LIST_HEAD(&dev->local);
  37. INIT_LIST_HEAD(&dev->lecs);
  38. return 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->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. mutex_lock(&atm_dev_mutex);
  57. dev = __atm_dev_lookup(number);
  58. mutex_unlock(&atm_dev_mutex);
  59. return dev;
  60. }
  61. EXPORT_SYMBOL(atm_dev_lookup);
  62. struct atm_dev *atm_dev_register(const char *type, struct device *parent,
  63. const struct atmdev_ops *ops, int number,
  64. unsigned long *flags)
  65. {
  66. struct atm_dev *dev, *inuse;
  67. dev = __alloc_atm_dev(type);
  68. if (!dev) {
  69. pr_err("no space for dev %s\n", type);
  70. return NULL;
  71. }
  72. mutex_lock(&atm_dev_mutex);
  73. if (number != -1) {
  74. inuse = __atm_dev_lookup(number);
  75. if (inuse) {
  76. atm_dev_put(inuse);
  77. mutex_unlock(&atm_dev_mutex);
  78. kfree(dev);
  79. return NULL;
  80. }
  81. dev->number = number;
  82. } else {
  83. dev->number = 0;
  84. while ((inuse = __atm_dev_lookup(dev->number))) {
  85. atm_dev_put(inuse);
  86. dev->number++;
  87. }
  88. }
  89. dev->ops = ops;
  90. if (flags)
  91. dev->flags = *flags;
  92. else
  93. memset(&dev->flags, 0, sizeof(dev->flags));
  94. memset(&dev->stats, 0, sizeof(dev->stats));
  95. refcount_set(&dev->refcnt, 1);
  96. if (atm_proc_dev_register(dev) < 0) {
  97. pr_err("atm_proc_dev_register failed for dev %s\n", type);
  98. goto out_fail;
  99. }
  100. if (atm_register_sysfs(dev, parent) < 0) {
  101. pr_err("atm_register_sysfs failed for dev %s\n", type);
  102. atm_proc_dev_deregister(dev);
  103. goto out_fail;
  104. }
  105. list_add_tail(&dev->dev_list, &atm_devs);
  106. out:
  107. mutex_unlock(&atm_dev_mutex);
  108. return dev;
  109. out_fail:
  110. kfree(dev);
  111. dev = NULL;
  112. goto out;
  113. }
  114. EXPORT_SYMBOL(atm_dev_register);
  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. EXPORT_SYMBOL(atm_dev_deregister);
  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,
  148. int zero)
  149. {
  150. struct atm_dev_stats tmp;
  151. int error = 0;
  152. copy_aal_stats(&dev->stats.aal0, &tmp.aal0);
  153. copy_aal_stats(&dev->stats.aal34, &tmp.aal34);
  154. copy_aal_stats(&dev->stats.aal5, &tmp.aal5);
  155. if (arg)
  156. error = copy_to_user(arg, &tmp, sizeof(tmp));
  157. if (zero && !error) {
  158. subtract_aal_stats(&dev->stats.aal0, &tmp.aal0);
  159. subtract_aal_stats(&dev->stats.aal34, &tmp.aal34);
  160. subtract_aal_stats(&dev->stats.aal5, &tmp.aal5);
  161. }
  162. return error ? -EFAULT : 0;
  163. }
  164. int atm_dev_ioctl(unsigned int cmd, void __user *arg, int compat)
  165. {
  166. void __user *buf;
  167. int error, len, number, size = 0;
  168. struct atm_dev *dev;
  169. struct list_head *p;
  170. int *tmp_buf, *tmp_p;
  171. int __user *sioc_len;
  172. int __user *iobuf_len;
  173. #ifndef CONFIG_COMPAT
  174. compat = 0; /* Just so the compiler _knows_ */
  175. #endif
  176. switch (cmd) {
  177. case ATM_GETNAMES:
  178. if (compat) {
  179. #ifdef CONFIG_COMPAT
  180. struct compat_atm_iobuf __user *ciobuf = arg;
  181. compat_uptr_t cbuf;
  182. iobuf_len = &ciobuf->length;
  183. if (get_user(cbuf, &ciobuf->buffer))
  184. return -EFAULT;
  185. buf = compat_ptr(cbuf);
  186. #endif
  187. } else {
  188. struct atm_iobuf __user *iobuf = arg;
  189. iobuf_len = &iobuf->length;
  190. if (get_user(buf, &iobuf->buffer))
  191. return -EFAULT;
  192. }
  193. if (get_user(len, iobuf_len))
  194. return -EFAULT;
  195. mutex_lock(&atm_dev_mutex);
  196. list_for_each(p, &atm_devs)
  197. size += sizeof(int);
  198. if (size > len) {
  199. mutex_unlock(&atm_dev_mutex);
  200. return -E2BIG;
  201. }
  202. tmp_buf = kmalloc(size, GFP_ATOMIC);
  203. if (!tmp_buf) {
  204. mutex_unlock(&atm_dev_mutex);
  205. return -ENOMEM;
  206. }
  207. tmp_p = tmp_buf;
  208. list_for_each(p, &atm_devs) {
  209. dev = list_entry(p, struct atm_dev, dev_list);
  210. *tmp_p++ = dev->number;
  211. }
  212. mutex_unlock(&atm_dev_mutex);
  213. error = ((copy_to_user(buf, tmp_buf, size)) ||
  214. put_user(size, iobuf_len))
  215. ? -EFAULT : 0;
  216. kfree(tmp_buf);
  217. return error;
  218. default:
  219. break;
  220. }
  221. if (compat) {
  222. #ifdef CONFIG_COMPAT
  223. struct compat_atmif_sioc __user *csioc = arg;
  224. compat_uptr_t carg;
  225. sioc_len = &csioc->length;
  226. if (get_user(carg, &csioc->arg))
  227. return -EFAULT;
  228. buf = compat_ptr(carg);
  229. if (get_user(len, &csioc->length))
  230. return -EFAULT;
  231. if (get_user(number, &csioc->number))
  232. return -EFAULT;
  233. #endif
  234. } else {
  235. struct atmif_sioc __user *sioc = arg;
  236. sioc_len = &sioc->length;
  237. if (get_user(buf, &sioc->arg))
  238. return -EFAULT;
  239. if (get_user(len, &sioc->length))
  240. return -EFAULT;
  241. if (get_user(number, &sioc->number))
  242. return -EFAULT;
  243. }
  244. dev = try_then_request_module(atm_dev_lookup(number), "atm-device-%d",
  245. number);
  246. if (!dev)
  247. return -ENODEV;
  248. switch (cmd) {
  249. case ATM_GETTYPE:
  250. size = strlen(dev->type) + 1;
  251. if (copy_to_user(buf, dev->type, size)) {
  252. error = -EFAULT;
  253. goto done;
  254. }
  255. break;
  256. case ATM_GETESI:
  257. size = ESI_LEN;
  258. if (copy_to_user(buf, dev->esi, size)) {
  259. error = -EFAULT;
  260. goto done;
  261. }
  262. break;
  263. case ATM_SETESI:
  264. {
  265. int i;
  266. for (i = 0; i < ESI_LEN; i++)
  267. if (dev->esi[i]) {
  268. error = -EEXIST;
  269. goto done;
  270. }
  271. }
  272. /* fall through */
  273. case ATM_SETESIF:
  274. {
  275. unsigned char esi[ESI_LEN];
  276. if (!capable(CAP_NET_ADMIN)) {
  277. error = -EPERM;
  278. goto done;
  279. }
  280. if (copy_from_user(esi, buf, ESI_LEN)) {
  281. error = -EFAULT;
  282. goto done;
  283. }
  284. memcpy(dev->esi, esi, ESI_LEN);
  285. error = ESI_LEN;
  286. goto done;
  287. }
  288. case ATM_GETSTATZ:
  289. if (!capable(CAP_NET_ADMIN)) {
  290. error = -EPERM;
  291. goto done;
  292. }
  293. /* fall through */
  294. case ATM_GETSTAT:
  295. size = sizeof(struct atm_dev_stats);
  296. error = fetch_stats(dev, buf, cmd == ATM_GETSTATZ);
  297. if (error)
  298. goto done;
  299. break;
  300. case ATM_GETCIRANGE:
  301. size = sizeof(struct atm_cirange);
  302. if (copy_to_user(buf, &dev->ci_range, size)) {
  303. error = -EFAULT;
  304. goto done;
  305. }
  306. break;
  307. case ATM_GETLINKRATE:
  308. size = sizeof(int);
  309. if (copy_to_user(buf, &dev->link_rate, size)) {
  310. error = -EFAULT;
  311. goto done;
  312. }
  313. break;
  314. case ATM_RSTADDR:
  315. if (!capable(CAP_NET_ADMIN)) {
  316. error = -EPERM;
  317. goto done;
  318. }
  319. atm_reset_addr(dev, ATM_ADDR_LOCAL);
  320. break;
  321. case ATM_ADDADDR:
  322. case ATM_DELADDR:
  323. case ATM_ADDLECSADDR:
  324. case ATM_DELLECSADDR:
  325. {
  326. struct sockaddr_atmsvc addr;
  327. if (!capable(CAP_NET_ADMIN)) {
  328. error = -EPERM;
  329. goto done;
  330. }
  331. if (copy_from_user(&addr, buf, sizeof(addr))) {
  332. error = -EFAULT;
  333. goto done;
  334. }
  335. if (cmd == ATM_ADDADDR || cmd == ATM_ADDLECSADDR)
  336. error = atm_add_addr(dev, &addr,
  337. (cmd == ATM_ADDADDR ?
  338. ATM_ADDR_LOCAL : ATM_ADDR_LECS));
  339. else
  340. error = atm_del_addr(dev, &addr,
  341. (cmd == ATM_DELADDR ?
  342. ATM_ADDR_LOCAL : ATM_ADDR_LECS));
  343. goto done;
  344. }
  345. case ATM_GETADDR:
  346. case ATM_GETLECSADDR:
  347. error = atm_get_addr(dev, buf, len,
  348. (cmd == ATM_GETADDR ?
  349. ATM_ADDR_LOCAL : ATM_ADDR_LECS));
  350. if (error < 0)
  351. goto done;
  352. size = error;
  353. /* may return 0, but later on size == 0 means "don't
  354. write the length" */
  355. error = put_user(size, sioc_len) ? -EFAULT : 0;
  356. goto done;
  357. case ATM_SETLOOP:
  358. if (__ATM_LM_XTRMT((int) (unsigned long) buf) &&
  359. __ATM_LM_XTLOC((int) (unsigned long) buf) >
  360. __ATM_LM_XTRMT((int) (unsigned long) buf)) {
  361. error = -EINVAL;
  362. goto done;
  363. }
  364. /* fall through */
  365. case ATM_SETCIRANGE:
  366. case SONET_GETSTATZ:
  367. case SONET_SETDIAG:
  368. case SONET_CLRDIAG:
  369. case SONET_SETFRAMING:
  370. if (!capable(CAP_NET_ADMIN)) {
  371. error = -EPERM;
  372. goto done;
  373. }
  374. /* fall through */
  375. default:
  376. if (compat) {
  377. #ifdef CONFIG_COMPAT
  378. if (!dev->ops->compat_ioctl) {
  379. error = -EINVAL;
  380. goto done;
  381. }
  382. size = dev->ops->compat_ioctl(dev, cmd, buf);
  383. #endif
  384. } else {
  385. if (!dev->ops->ioctl) {
  386. error = -EINVAL;
  387. goto done;
  388. }
  389. size = dev->ops->ioctl(dev, cmd, buf);
  390. }
  391. if (size < 0) {
  392. error = (size == -ENOIOCTLCMD ? -ENOTTY : size);
  393. goto done;
  394. }
  395. }
  396. if (size)
  397. error = put_user(size, sioc_len) ? -EFAULT : 0;
  398. else
  399. error = 0;
  400. done:
  401. atm_dev_put(dev);
  402. return error;
  403. }
  404. void *atm_dev_seq_start(struct seq_file *seq, loff_t *pos)
  405. {
  406. mutex_lock(&atm_dev_mutex);
  407. return seq_list_start_head(&atm_devs, *pos);
  408. }
  409. void atm_dev_seq_stop(struct seq_file *seq, void *v)
  410. {
  411. mutex_unlock(&atm_dev_mutex);
  412. }
  413. void *atm_dev_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  414. {
  415. return seq_list_next(v, &atm_devs, pos);
  416. }