ioctl.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* ATM ioctl handling */
  3. /* Written 1995-2000 by Werner Almesberger, EPFL LRC/ICA */
  4. /* 2003 John Levon <levon@movementarian.org> */
  5. #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
  6. #include <linux/module.h>
  7. #include <linux/kmod.h>
  8. #include <linux/net.h> /* struct socket, struct proto_ops */
  9. #include <linux/atm.h> /* ATM stuff */
  10. #include <linux/atmdev.h>
  11. #include <linux/atmclip.h> /* CLIP_*ENCAP */
  12. #include <linux/atmarp.h> /* manifest constants */
  13. #include <linux/capability.h>
  14. #include <linux/sonet.h> /* for ioctls */
  15. #include <linux/atmsvc.h>
  16. #include <linux/atmmpc.h>
  17. #include <net/atmclip.h>
  18. #include <linux/atmlec.h>
  19. #include <linux/mutex.h>
  20. #include <asm/ioctls.h>
  21. #include <net/compat.h>
  22. #include "resources.h"
  23. #include "signaling.h" /* for WAITING and sigd_attach */
  24. #include "common.h"
  25. static DEFINE_MUTEX(ioctl_mutex);
  26. static LIST_HEAD(ioctl_list);
  27. void register_atm_ioctl(struct atm_ioctl *ioctl)
  28. {
  29. mutex_lock(&ioctl_mutex);
  30. list_add_tail(&ioctl->list, &ioctl_list);
  31. mutex_unlock(&ioctl_mutex);
  32. }
  33. EXPORT_SYMBOL(register_atm_ioctl);
  34. void deregister_atm_ioctl(struct atm_ioctl *ioctl)
  35. {
  36. mutex_lock(&ioctl_mutex);
  37. list_del(&ioctl->list);
  38. mutex_unlock(&ioctl_mutex);
  39. }
  40. EXPORT_SYMBOL(deregister_atm_ioctl);
  41. static int do_vcc_ioctl(struct socket *sock, unsigned int cmd,
  42. unsigned long arg, int compat)
  43. {
  44. struct sock *sk = sock->sk;
  45. struct atm_vcc *vcc;
  46. int error;
  47. struct list_head *pos;
  48. void __user *argp = (void __user *)arg;
  49. void __user *buf;
  50. int __user *len;
  51. vcc = ATM_SD(sock);
  52. switch (cmd) {
  53. case SIOCOUTQ:
  54. if (sock->state != SS_CONNECTED ||
  55. !test_bit(ATM_VF_READY, &vcc->flags)) {
  56. error = -EINVAL;
  57. goto done;
  58. }
  59. error = put_user(sk->sk_sndbuf - sk_wmem_alloc_get(sk),
  60. (int __user *)argp);
  61. goto done;
  62. case SIOCINQ:
  63. {
  64. struct sk_buff *skb;
  65. int amount;
  66. if (sock->state != SS_CONNECTED) {
  67. error = -EINVAL;
  68. goto done;
  69. }
  70. spin_lock_irq(&sk->sk_receive_queue.lock);
  71. skb = skb_peek(&sk->sk_receive_queue);
  72. amount = skb ? skb->len : 0;
  73. spin_unlock_irq(&sk->sk_receive_queue.lock);
  74. error = put_user(amount, (int __user *)argp);
  75. goto done;
  76. }
  77. case ATM_SETSC:
  78. net_warn_ratelimited("ATM_SETSC is obsolete; used by %s:%d\n",
  79. current->comm, task_pid_nr(current));
  80. error = 0;
  81. goto done;
  82. case ATMSIGD_CTRL:
  83. if (!capable(CAP_NET_ADMIN)) {
  84. error = -EPERM;
  85. goto done;
  86. }
  87. /*
  88. * The user/kernel protocol for exchanging signalling
  89. * info uses kernel pointers as opaque references,
  90. * so the holder of the file descriptor can scribble
  91. * on the kernel... so we should make sure that we
  92. * have the same privileges that /proc/kcore needs
  93. */
  94. if (!capable(CAP_SYS_RAWIO)) {
  95. error = -EPERM;
  96. goto done;
  97. }
  98. #ifdef CONFIG_COMPAT
  99. /* WTF? I don't even want to _think_ about making this
  100. work for 32-bit userspace. TBH I don't really want
  101. to think about it at all. dwmw2. */
  102. if (compat) {
  103. net_warn_ratelimited("32-bit task cannot be atmsigd\n");
  104. error = -EINVAL;
  105. goto done;
  106. }
  107. #endif
  108. error = sigd_attach(vcc);
  109. if (!error)
  110. sock->state = SS_CONNECTED;
  111. goto done;
  112. case ATM_SETBACKEND:
  113. case ATM_NEWBACKENDIF:
  114. {
  115. atm_backend_t backend;
  116. error = get_user(backend, (atm_backend_t __user *)argp);
  117. if (error)
  118. goto done;
  119. switch (backend) {
  120. case ATM_BACKEND_PPP:
  121. request_module("pppoatm");
  122. break;
  123. case ATM_BACKEND_BR2684:
  124. request_module("br2684");
  125. break;
  126. }
  127. break;
  128. }
  129. case ATMMPC_CTRL:
  130. case ATMMPC_DATA:
  131. request_module("mpoa");
  132. break;
  133. case ATMARPD_CTRL:
  134. request_module("clip");
  135. break;
  136. case ATMLEC_CTRL:
  137. request_module("lec");
  138. break;
  139. }
  140. error = -ENOIOCTLCMD;
  141. mutex_lock(&ioctl_mutex);
  142. list_for_each(pos, &ioctl_list) {
  143. struct atm_ioctl *ic = list_entry(pos, struct atm_ioctl, list);
  144. if (try_module_get(ic->owner)) {
  145. error = ic->ioctl(sock, cmd, arg);
  146. module_put(ic->owner);
  147. if (error != -ENOIOCTLCMD)
  148. break;
  149. }
  150. }
  151. mutex_unlock(&ioctl_mutex);
  152. if (error != -ENOIOCTLCMD)
  153. goto done;
  154. if (cmd == ATM_GETNAMES) {
  155. if (IS_ENABLED(CONFIG_COMPAT) && compat) {
  156. #ifdef CONFIG_COMPAT
  157. struct compat_atm_iobuf __user *ciobuf = argp;
  158. compat_uptr_t cbuf;
  159. len = &ciobuf->length;
  160. if (get_user(cbuf, &ciobuf->buffer))
  161. return -EFAULT;
  162. buf = compat_ptr(cbuf);
  163. #endif
  164. } else {
  165. struct atm_iobuf __user *iobuf = argp;
  166. len = &iobuf->length;
  167. if (get_user(buf, &iobuf->buffer))
  168. return -EFAULT;
  169. }
  170. error = atm_getnames(buf, len);
  171. } else {
  172. int number;
  173. if (IS_ENABLED(CONFIG_COMPAT) && compat) {
  174. #ifdef CONFIG_COMPAT
  175. struct compat_atmif_sioc __user *csioc = argp;
  176. compat_uptr_t carg;
  177. len = &csioc->length;
  178. if (get_user(carg, &csioc->arg))
  179. return -EFAULT;
  180. buf = compat_ptr(carg);
  181. if (get_user(number, &csioc->number))
  182. return -EFAULT;
  183. #endif
  184. } else {
  185. struct atmif_sioc __user *sioc = argp;
  186. len = &sioc->length;
  187. if (get_user(buf, &sioc->arg))
  188. return -EFAULT;
  189. if (get_user(number, &sioc->number))
  190. return -EFAULT;
  191. }
  192. error = atm_dev_ioctl(cmd, buf, len, number, compat);
  193. }
  194. done:
  195. return error;
  196. }
  197. int vcc_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  198. {
  199. return do_vcc_ioctl(sock, cmd, arg, 0);
  200. }
  201. #ifdef CONFIG_COMPAT
  202. /*
  203. * FIXME:
  204. * The compat_ioctl handling is duplicated, using both these conversion
  205. * routines and the compat argument to the actual handlers. Both
  206. * versions are somewhat incomplete and should be merged, e.g. by
  207. * moving the ioctl number translation into the actual handlers and
  208. * killing the conversion code.
  209. *
  210. * -arnd, November 2009
  211. */
  212. #define ATM_GETLINKRATE32 _IOW('a', ATMIOC_ITF+1, struct compat_atmif_sioc)
  213. #define ATM_GETNAMES32 _IOW('a', ATMIOC_ITF+3, struct compat_atm_iobuf)
  214. #define ATM_GETTYPE32 _IOW('a', ATMIOC_ITF+4, struct compat_atmif_sioc)
  215. #define ATM_GETESI32 _IOW('a', ATMIOC_ITF+5, struct compat_atmif_sioc)
  216. #define ATM_GETADDR32 _IOW('a', ATMIOC_ITF+6, struct compat_atmif_sioc)
  217. #define ATM_RSTADDR32 _IOW('a', ATMIOC_ITF+7, struct compat_atmif_sioc)
  218. #define ATM_ADDADDR32 _IOW('a', ATMIOC_ITF+8, struct compat_atmif_sioc)
  219. #define ATM_DELADDR32 _IOW('a', ATMIOC_ITF+9, struct compat_atmif_sioc)
  220. #define ATM_GETCIRANGE32 _IOW('a', ATMIOC_ITF+10, struct compat_atmif_sioc)
  221. #define ATM_SETCIRANGE32 _IOW('a', ATMIOC_ITF+11, struct compat_atmif_sioc)
  222. #define ATM_SETESI32 _IOW('a', ATMIOC_ITF+12, struct compat_atmif_sioc)
  223. #define ATM_SETESIF32 _IOW('a', ATMIOC_ITF+13, struct compat_atmif_sioc)
  224. #define ATM_GETSTAT32 _IOW('a', ATMIOC_SARCOM+0, struct compat_atmif_sioc)
  225. #define ATM_GETSTATZ32 _IOW('a', ATMIOC_SARCOM+1, struct compat_atmif_sioc)
  226. #define ATM_GETLOOP32 _IOW('a', ATMIOC_SARCOM+2, struct compat_atmif_sioc)
  227. #define ATM_SETLOOP32 _IOW('a', ATMIOC_SARCOM+3, struct compat_atmif_sioc)
  228. #define ATM_QUERYLOOP32 _IOW('a', ATMIOC_SARCOM+4, struct compat_atmif_sioc)
  229. static struct {
  230. unsigned int cmd32;
  231. unsigned int cmd;
  232. } atm_ioctl_map[] = {
  233. { ATM_GETLINKRATE32, ATM_GETLINKRATE },
  234. { ATM_GETNAMES32, ATM_GETNAMES },
  235. { ATM_GETTYPE32, ATM_GETTYPE },
  236. { ATM_GETESI32, ATM_GETESI },
  237. { ATM_GETADDR32, ATM_GETADDR },
  238. { ATM_RSTADDR32, ATM_RSTADDR },
  239. { ATM_ADDADDR32, ATM_ADDADDR },
  240. { ATM_DELADDR32, ATM_DELADDR },
  241. { ATM_GETCIRANGE32, ATM_GETCIRANGE },
  242. { ATM_SETCIRANGE32, ATM_SETCIRANGE },
  243. { ATM_SETESI32, ATM_SETESI },
  244. { ATM_SETESIF32, ATM_SETESIF },
  245. { ATM_GETSTAT32, ATM_GETSTAT },
  246. { ATM_GETSTATZ32, ATM_GETSTATZ },
  247. { ATM_GETLOOP32, ATM_GETLOOP },
  248. { ATM_SETLOOP32, ATM_SETLOOP },
  249. { ATM_QUERYLOOP32, ATM_QUERYLOOP },
  250. };
  251. #define NR_ATM_IOCTL ARRAY_SIZE(atm_ioctl_map)
  252. static int do_atm_iobuf(struct socket *sock, unsigned int cmd,
  253. unsigned long arg)
  254. {
  255. struct compat_atm_iobuf __user *iobuf32 = compat_ptr(arg);
  256. u32 data;
  257. if (get_user(data, &iobuf32->buffer))
  258. return -EFAULT;
  259. return atm_getnames(&iobuf32->length, compat_ptr(data));
  260. }
  261. static int do_atmif_sioc(struct socket *sock, unsigned int cmd,
  262. unsigned long arg)
  263. {
  264. struct compat_atmif_sioc __user *sioc32 = compat_ptr(arg);
  265. int number;
  266. u32 data;
  267. if (get_user(data, &sioc32->arg) || get_user(number, &sioc32->number))
  268. return -EFAULT;
  269. return atm_dev_ioctl(cmd, compat_ptr(data), &sioc32->length, number, 0);
  270. }
  271. static int do_atm_ioctl(struct socket *sock, unsigned int cmd32,
  272. unsigned long arg)
  273. {
  274. int i;
  275. unsigned int cmd = 0;
  276. switch (cmd32) {
  277. case SONET_GETSTAT:
  278. case SONET_GETSTATZ:
  279. case SONET_GETDIAG:
  280. case SONET_SETDIAG:
  281. case SONET_CLRDIAG:
  282. case SONET_SETFRAMING:
  283. case SONET_GETFRAMING:
  284. case SONET_GETFRSENSE:
  285. return do_atmif_sioc(sock, cmd32, arg);
  286. }
  287. for (i = 0; i < NR_ATM_IOCTL; i++) {
  288. if (cmd32 == atm_ioctl_map[i].cmd32) {
  289. cmd = atm_ioctl_map[i].cmd;
  290. break;
  291. }
  292. }
  293. if (i == NR_ATM_IOCTL)
  294. return -EINVAL;
  295. switch (cmd) {
  296. case ATM_GETNAMES:
  297. return do_atm_iobuf(sock, cmd, arg);
  298. case ATM_GETLINKRATE:
  299. case ATM_GETTYPE:
  300. case ATM_GETESI:
  301. case ATM_GETADDR:
  302. case ATM_RSTADDR:
  303. case ATM_ADDADDR:
  304. case ATM_DELADDR:
  305. case ATM_GETCIRANGE:
  306. case ATM_SETCIRANGE:
  307. case ATM_SETESI:
  308. case ATM_SETESIF:
  309. case ATM_GETSTAT:
  310. case ATM_GETSTATZ:
  311. case ATM_GETLOOP:
  312. case ATM_SETLOOP:
  313. case ATM_QUERYLOOP:
  314. return do_atmif_sioc(sock, cmd, arg);
  315. }
  316. return -EINVAL;
  317. }
  318. int vcc_compat_ioctl(struct socket *sock, unsigned int cmd,
  319. unsigned long arg)
  320. {
  321. int ret;
  322. ret = do_vcc_ioctl(sock, cmd, arg, 1);
  323. if (ret != -ENOIOCTLCMD)
  324. return ret;
  325. return do_atm_ioctl(sock, cmd, arg);
  326. }
  327. #endif