ioctl.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. if (net_ratelimit())
  89. pr_warning("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. if (net_ratelimit())
  115. pr_warning("32-bit task cannot be atmsigd\n");
  116. error = -EINVAL;
  117. goto done;
  118. }
  119. #endif
  120. error = sigd_attach(vcc);
  121. if (!error)
  122. sock->state = SS_CONNECTED;
  123. goto done;
  124. case ATM_SETBACKEND:
  125. case ATM_NEWBACKENDIF:
  126. {
  127. atm_backend_t backend;
  128. error = get_user(backend, (atm_backend_t __user *)argp);
  129. if (error)
  130. goto done;
  131. switch (backend) {
  132. case ATM_BACKEND_PPP:
  133. request_module("pppoatm");
  134. break;
  135. case ATM_BACKEND_BR2684:
  136. request_module("br2684");
  137. break;
  138. }
  139. break;
  140. }
  141. case ATMMPC_CTRL:
  142. case ATMMPC_DATA:
  143. request_module("mpoa");
  144. break;
  145. case ATMARPD_CTRL:
  146. request_module("clip");
  147. break;
  148. case ATMLEC_CTRL:
  149. request_module("lec");
  150. break;
  151. }
  152. error = -ENOIOCTLCMD;
  153. mutex_lock(&ioctl_mutex);
  154. list_for_each(pos, &ioctl_list) {
  155. struct atm_ioctl *ic = list_entry(pos, struct atm_ioctl, list);
  156. if (try_module_get(ic->owner)) {
  157. error = ic->ioctl(sock, cmd, arg);
  158. module_put(ic->owner);
  159. if (error != -ENOIOCTLCMD)
  160. break;
  161. }
  162. }
  163. mutex_unlock(&ioctl_mutex);
  164. if (error != -ENOIOCTLCMD)
  165. goto done;
  166. error = atm_dev_ioctl(cmd, argp, compat);
  167. done:
  168. return error;
  169. }
  170. int vcc_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  171. {
  172. return do_vcc_ioctl(sock, cmd, arg, 0);
  173. }
  174. #ifdef CONFIG_COMPAT
  175. /*
  176. * FIXME:
  177. * The compat_ioctl handling is duplicated, using both these conversion
  178. * routines and the compat argument to the actual handlers. Both
  179. * versions are somewhat incomplete and should be merged, e.g. by
  180. * moving the ioctl number translation into the actual handlers and
  181. * killing the conversion code.
  182. *
  183. * -arnd, November 2009
  184. */
  185. #define ATM_GETLINKRATE32 _IOW('a', ATMIOC_ITF+1, struct compat_atmif_sioc)
  186. #define ATM_GETNAMES32 _IOW('a', ATMIOC_ITF+3, struct compat_atm_iobuf)
  187. #define ATM_GETTYPE32 _IOW('a', ATMIOC_ITF+4, struct compat_atmif_sioc)
  188. #define ATM_GETESI32 _IOW('a', ATMIOC_ITF+5, struct compat_atmif_sioc)
  189. #define ATM_GETADDR32 _IOW('a', ATMIOC_ITF+6, struct compat_atmif_sioc)
  190. #define ATM_RSTADDR32 _IOW('a', ATMIOC_ITF+7, struct compat_atmif_sioc)
  191. #define ATM_ADDADDR32 _IOW('a', ATMIOC_ITF+8, struct compat_atmif_sioc)
  192. #define ATM_DELADDR32 _IOW('a', ATMIOC_ITF+9, struct compat_atmif_sioc)
  193. #define ATM_GETCIRANGE32 _IOW('a', ATMIOC_ITF+10, struct compat_atmif_sioc)
  194. #define ATM_SETCIRANGE32 _IOW('a', ATMIOC_ITF+11, struct compat_atmif_sioc)
  195. #define ATM_SETESI32 _IOW('a', ATMIOC_ITF+12, struct compat_atmif_sioc)
  196. #define ATM_SETESIF32 _IOW('a', ATMIOC_ITF+13, struct compat_atmif_sioc)
  197. #define ATM_GETSTAT32 _IOW('a', ATMIOC_SARCOM+0, struct compat_atmif_sioc)
  198. #define ATM_GETSTATZ32 _IOW('a', ATMIOC_SARCOM+1, struct compat_atmif_sioc)
  199. #define ATM_GETLOOP32 _IOW('a', ATMIOC_SARCOM+2, struct compat_atmif_sioc)
  200. #define ATM_SETLOOP32 _IOW('a', ATMIOC_SARCOM+3, struct compat_atmif_sioc)
  201. #define ATM_QUERYLOOP32 _IOW('a', ATMIOC_SARCOM+4, struct compat_atmif_sioc)
  202. static struct {
  203. unsigned int cmd32;
  204. unsigned int cmd;
  205. } atm_ioctl_map[] = {
  206. { ATM_GETLINKRATE32, ATM_GETLINKRATE },
  207. { ATM_GETNAMES32, ATM_GETNAMES },
  208. { ATM_GETTYPE32, ATM_GETTYPE },
  209. { ATM_GETESI32, ATM_GETESI },
  210. { ATM_GETADDR32, ATM_GETADDR },
  211. { ATM_RSTADDR32, ATM_RSTADDR },
  212. { ATM_ADDADDR32, ATM_ADDADDR },
  213. { ATM_DELADDR32, ATM_DELADDR },
  214. { ATM_GETCIRANGE32, ATM_GETCIRANGE },
  215. { ATM_SETCIRANGE32, ATM_SETCIRANGE },
  216. { ATM_SETESI32, ATM_SETESI },
  217. { ATM_SETESIF32, ATM_SETESIF },
  218. { ATM_GETSTAT32, ATM_GETSTAT },
  219. { ATM_GETSTATZ32, ATM_GETSTATZ },
  220. { ATM_GETLOOP32, ATM_GETLOOP },
  221. { ATM_SETLOOP32, ATM_SETLOOP },
  222. { ATM_QUERYLOOP32, ATM_QUERYLOOP },
  223. };
  224. #define NR_ATM_IOCTL ARRAY_SIZE(atm_ioctl_map)
  225. static int do_atm_iobuf(struct socket *sock, unsigned int cmd,
  226. unsigned long arg)
  227. {
  228. struct atm_iobuf __user *iobuf;
  229. struct compat_atm_iobuf __user *iobuf32;
  230. u32 data;
  231. void __user *datap;
  232. int len, err;
  233. iobuf = compat_alloc_user_space(sizeof(*iobuf));
  234. iobuf32 = compat_ptr(arg);
  235. if (get_user(len, &iobuf32->length) ||
  236. get_user(data, &iobuf32->buffer))
  237. return -EFAULT;
  238. datap = compat_ptr(data);
  239. if (put_user(len, &iobuf->length) ||
  240. put_user(datap, &iobuf->buffer))
  241. return -EFAULT;
  242. err = do_vcc_ioctl(sock, cmd, (unsigned long) iobuf, 0);
  243. if (!err) {
  244. if (copy_in_user(&iobuf32->length, &iobuf->length,
  245. sizeof(int)))
  246. err = -EFAULT;
  247. }
  248. return err;
  249. }
  250. static int do_atmif_sioc(struct socket *sock, unsigned int cmd,
  251. unsigned long arg)
  252. {
  253. struct atmif_sioc __user *sioc;
  254. struct compat_atmif_sioc __user *sioc32;
  255. u32 data;
  256. void __user *datap;
  257. int err;
  258. sioc = compat_alloc_user_space(sizeof(*sioc));
  259. sioc32 = compat_ptr(arg);
  260. if (copy_in_user(&sioc->number, &sioc32->number, 2 * sizeof(int)) ||
  261. get_user(data, &sioc32->arg))
  262. return -EFAULT;
  263. datap = compat_ptr(data);
  264. if (put_user(datap, &sioc->arg))
  265. return -EFAULT;
  266. err = do_vcc_ioctl(sock, cmd, (unsigned long) sioc, 0);
  267. if (!err) {
  268. if (copy_in_user(&sioc32->length, &sioc->length,
  269. sizeof(int)))
  270. err = -EFAULT;
  271. }
  272. return err;
  273. }
  274. static int do_atm_ioctl(struct socket *sock, unsigned int cmd32,
  275. unsigned long arg)
  276. {
  277. int i;
  278. unsigned int cmd = 0;
  279. switch (cmd32) {
  280. case SONET_GETSTAT:
  281. case SONET_GETSTATZ:
  282. case SONET_GETDIAG:
  283. case SONET_SETDIAG:
  284. case SONET_CLRDIAG:
  285. case SONET_SETFRAMING:
  286. case SONET_GETFRAMING:
  287. case SONET_GETFRSENSE:
  288. return do_atmif_sioc(sock, cmd32, arg);
  289. }
  290. for (i = 0; i < NR_ATM_IOCTL; i++) {
  291. if (cmd32 == atm_ioctl_map[i].cmd32) {
  292. cmd = atm_ioctl_map[i].cmd;
  293. break;
  294. }
  295. }
  296. if (i == NR_ATM_IOCTL)
  297. return -EINVAL;
  298. switch (cmd) {
  299. case ATM_GETNAMES:
  300. return do_atm_iobuf(sock, cmd, arg);
  301. case ATM_GETLINKRATE:
  302. case ATM_GETTYPE:
  303. case ATM_GETESI:
  304. case ATM_GETADDR:
  305. case ATM_RSTADDR:
  306. case ATM_ADDADDR:
  307. case ATM_DELADDR:
  308. case ATM_GETCIRANGE:
  309. case ATM_SETCIRANGE:
  310. case ATM_SETESI:
  311. case ATM_SETESIF:
  312. case ATM_GETSTAT:
  313. case ATM_GETSTATZ:
  314. case ATM_GETLOOP:
  315. case ATM_SETLOOP:
  316. case ATM_QUERYLOOP:
  317. return do_atmif_sioc(sock, cmd, arg);
  318. }
  319. return -EINVAL;
  320. }
  321. int vcc_compat_ioctl(struct socket *sock, unsigned int cmd,
  322. unsigned long arg)
  323. {
  324. int ret;
  325. ret = do_vcc_ioctl(sock, cmd, arg, 1);
  326. if (ret != -ENOIOCTLCMD)
  327. return ret;
  328. return do_atm_ioctl(sock, cmd, arg);
  329. }
  330. #endif