resources.c 8.9 KB

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