ioctl.c 9.2 KB

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