resources.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. list_for_each_entry(dev, &atm_devs, 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. EXPORT_SYMBOL(atm_dev_lookup);
  60. struct atm_dev *atm_dev_register(const char *type, struct device *parent,
  61. const struct atmdev_ops *ops, int number,
  62. unsigned long *flags)
  63. {
  64. struct atm_dev *dev, *inuse;
  65. dev = __alloc_atm_dev(type);
  66. if (!dev) {
  67. pr_err("no space for dev %s\n", type);
  68. return NULL;
  69. }
  70. mutex_lock(&atm_dev_mutex);
  71. if (number != -1) {
  72. inuse = __atm_dev_lookup(number);
  73. if (inuse) {
  74. atm_dev_put(inuse);
  75. mutex_unlock(&atm_dev_mutex);
  76. kfree(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. refcount_set(&dev->refcnt, 1);
  94. if (atm_proc_dev_register(dev) < 0) {
  95. pr_err("atm_proc_dev_register failed for dev %s\n", type);
  96. mutex_unlock(&atm_dev_mutex);
  97. kfree(dev);
  98. return NULL;
  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. put_device(&dev->class_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. atm_dev_release_vccs(dev);
  127. atm_unregister_sysfs(dev);
  128. atm_proc_dev_deregister(dev);
  129. mutex_unlock(&atm_dev_mutex);
  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_getnames(void __user *buf, int __user *iobuf_len)
  165. {
  166. int error, len, size = 0;
  167. struct atm_dev *dev;
  168. struct list_head *p;
  169. int *tmp_buf, *tmp_p;
  170. if (get_user(len, iobuf_len))
  171. return -EFAULT;
  172. mutex_lock(&atm_dev_mutex);
  173. list_for_each(p, &atm_devs)
  174. size += sizeof(int);
  175. if (size > len) {
  176. mutex_unlock(&atm_dev_mutex);
  177. return -E2BIG;
  178. }
  179. tmp_buf = kmalloc(size, GFP_ATOMIC);
  180. if (!tmp_buf) {
  181. mutex_unlock(&atm_dev_mutex);
  182. return -ENOMEM;
  183. }
  184. tmp_p = tmp_buf;
  185. list_for_each_entry(dev, &atm_devs, dev_list) {
  186. *tmp_p++ = dev->number;
  187. }
  188. mutex_unlock(&atm_dev_mutex);
  189. error = ((copy_to_user(buf, tmp_buf, size)) ||
  190. put_user(size, iobuf_len))
  191. ? -EFAULT : 0;
  192. kfree(tmp_buf);
  193. return error;
  194. }
  195. int atm_dev_ioctl(unsigned int cmd, void __user *buf, int __user *sioc_len,
  196. int number, int compat)
  197. {
  198. int error, len, size = 0;
  199. struct atm_dev *dev;
  200. if (get_user(len, sioc_len))
  201. return -EFAULT;
  202. dev = try_then_request_module(atm_dev_lookup(number), "atm-device-%d",
  203. number);
  204. if (!dev)
  205. return -ENODEV;
  206. switch (cmd) {
  207. case ATM_GETTYPE:
  208. size = strlen(dev->type) + 1;
  209. if (copy_to_user(buf, dev->type, size)) {
  210. error = -EFAULT;
  211. goto done;
  212. }
  213. break;
  214. case ATM_GETESI:
  215. size = ESI_LEN;
  216. if (copy_to_user(buf, dev->esi, size)) {
  217. error = -EFAULT;
  218. goto done;
  219. }
  220. break;
  221. case ATM_SETESI:
  222. {
  223. int i;
  224. for (i = 0; i < ESI_LEN; i++)
  225. if (dev->esi[i]) {
  226. error = -EEXIST;
  227. goto done;
  228. }
  229. }
  230. fallthrough;
  231. case ATM_SETESIF:
  232. {
  233. unsigned char esi[ESI_LEN];
  234. if (!capable(CAP_NET_ADMIN)) {
  235. error = -EPERM;
  236. goto done;
  237. }
  238. if (copy_from_user(esi, buf, ESI_LEN)) {
  239. error = -EFAULT;
  240. goto done;
  241. }
  242. memcpy(dev->esi, esi, ESI_LEN);
  243. error = ESI_LEN;
  244. goto done;
  245. }
  246. case ATM_GETSTATZ:
  247. if (!capable(CAP_NET_ADMIN)) {
  248. error = -EPERM;
  249. goto done;
  250. }
  251. fallthrough;
  252. case ATM_GETSTAT:
  253. size = sizeof(struct atm_dev_stats);
  254. error = fetch_stats(dev, buf, cmd == ATM_GETSTATZ);
  255. if (error)
  256. goto done;
  257. break;
  258. case ATM_GETCIRANGE:
  259. size = sizeof(struct atm_cirange);
  260. if (copy_to_user(buf, &dev->ci_range, size)) {
  261. error = -EFAULT;
  262. goto done;
  263. }
  264. break;
  265. case ATM_GETLINKRATE:
  266. size = sizeof(int);
  267. if (copy_to_user(buf, &dev->link_rate, size)) {
  268. error = -EFAULT;
  269. goto done;
  270. }
  271. break;
  272. case ATM_RSTADDR:
  273. if (!capable(CAP_NET_ADMIN)) {
  274. error = -EPERM;
  275. goto done;
  276. }
  277. atm_reset_addr(dev, ATM_ADDR_LOCAL);
  278. break;
  279. case ATM_ADDADDR:
  280. case ATM_DELADDR:
  281. case ATM_ADDLECSADDR:
  282. case ATM_DELLECSADDR:
  283. {
  284. struct sockaddr_atmsvc addr;
  285. if (!capable(CAP_NET_ADMIN)) {
  286. error = -EPERM;
  287. goto done;
  288. }
  289. if (copy_from_user(&addr, buf, sizeof(addr))) {
  290. error = -EFAULT;
  291. goto done;
  292. }
  293. if (cmd == ATM_ADDADDR || cmd == ATM_ADDLECSADDR)
  294. error = atm_add_addr(dev, &addr,
  295. (cmd == ATM_ADDADDR ?
  296. ATM_ADDR_LOCAL : ATM_ADDR_LECS));
  297. else
  298. error = atm_del_addr(dev, &addr,
  299. (cmd == ATM_DELADDR ?
  300. ATM_ADDR_LOCAL : ATM_ADDR_LECS));
  301. goto done;
  302. }
  303. case ATM_GETADDR:
  304. case ATM_GETLECSADDR:
  305. error = atm_get_addr(dev, buf, len,
  306. (cmd == ATM_GETADDR ?
  307. ATM_ADDR_LOCAL : ATM_ADDR_LECS));
  308. if (error < 0)
  309. goto done;
  310. size = error;
  311. /* may return 0, but later on size == 0 means "don't
  312. write the length" */
  313. error = put_user(size, sioc_len) ? -EFAULT : 0;
  314. goto done;
  315. case ATM_SETLOOP:
  316. if (__ATM_LM_XTRMT((int) (unsigned long) buf) &&
  317. __ATM_LM_XTLOC((int) (unsigned long) buf) >
  318. __ATM_LM_XTRMT((int) (unsigned long) buf)) {
  319. error = -EINVAL;
  320. goto done;
  321. }
  322. fallthrough;
  323. case ATM_SETCIRANGE:
  324. case SONET_GETSTATZ:
  325. case SONET_SETDIAG:
  326. case SONET_CLRDIAG:
  327. case SONET_SETFRAMING:
  328. if (!capable(CAP_NET_ADMIN)) {
  329. error = -EPERM;
  330. goto done;
  331. }
  332. fallthrough;
  333. default:
  334. if (IS_ENABLED(CONFIG_COMPAT) && compat) {
  335. #ifdef CONFIG_COMPAT
  336. if (!dev->ops->compat_ioctl) {
  337. error = -EINVAL;
  338. goto done;
  339. }
  340. size = dev->ops->compat_ioctl(dev, cmd, buf);
  341. #endif
  342. } else {
  343. if (!dev->ops->ioctl) {
  344. error = -EINVAL;
  345. goto done;
  346. }
  347. size = dev->ops->ioctl(dev, cmd, buf);
  348. }
  349. if (size < 0) {
  350. error = (size == -ENOIOCTLCMD ? -ENOTTY : size);
  351. goto done;
  352. }
  353. }
  354. if (size)
  355. error = put_user(size, sioc_len) ? -EFAULT : 0;
  356. else
  357. error = 0;
  358. done:
  359. atm_dev_put(dev);
  360. return error;
  361. }
  362. #ifdef CONFIG_PROC_FS
  363. void *atm_dev_seq_start(struct seq_file *seq, loff_t *pos)
  364. {
  365. mutex_lock(&atm_dev_mutex);
  366. return seq_list_start_head(&atm_devs, *pos);
  367. }
  368. void atm_dev_seq_stop(struct seq_file *seq, void *v)
  369. {
  370. mutex_unlock(&atm_dev_mutex);
  371. }
  372. void *atm_dev_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  373. {
  374. return seq_list_next(v, &atm_devs, pos);
  375. }
  376. #endif