ioctl.c 9.2 KB

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