ioctl.c 9.3 KB

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