ioctl.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /* ATM ioctl handling */
  2. /* Written 1995-2000 by Werner Almesberger, EPFL LRC/ICA */
  3. /* 2003 John Levon <levon@movementarian.org> */
  4. #include <linux/module.h>
  5. #include <linux/kmod.h>
  6. #include <linux/net.h> /* struct socket, struct proto_ops */
  7. #include <linux/atm.h> /* ATM stuff */
  8. #include <linux/atmdev.h>
  9. #include <linux/atmclip.h> /* CLIP_*ENCAP */
  10. #include <linux/atmarp.h> /* manifest constants */
  11. #include <linux/capability.h>
  12. #include <linux/sonet.h> /* for ioctls */
  13. #include <linux/atmsvc.h>
  14. #include <linux/atmmpc.h>
  15. #include <net/atmclip.h>
  16. #include <linux/atmlec.h>
  17. #include <linux/mutex.h>
  18. #include <asm/ioctls.h>
  19. #include "resources.h"
  20. #include "signaling.h" /* for WAITING and sigd_attach */
  21. #include "common.h"
  22. static DEFINE_MUTEX(ioctl_mutex);
  23. static LIST_HEAD(ioctl_list);
  24. void register_atm_ioctl(struct atm_ioctl *ioctl)
  25. {
  26. mutex_lock(&ioctl_mutex);
  27. list_add_tail(&ioctl->list, &ioctl_list);
  28. mutex_unlock(&ioctl_mutex);
  29. }
  30. void deregister_atm_ioctl(struct atm_ioctl *ioctl)
  31. {
  32. mutex_lock(&ioctl_mutex);
  33. list_del(&ioctl->list);
  34. mutex_unlock(&ioctl_mutex);
  35. }
  36. EXPORT_SYMBOL(register_atm_ioctl);
  37. EXPORT_SYMBOL(deregister_atm_ioctl);
  38. int vcc_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  39. {
  40. struct sock *sk = sock->sk;
  41. struct atm_vcc *vcc;
  42. int error;
  43. struct list_head * pos;
  44. void __user *argp = (void __user *)arg;
  45. vcc = ATM_SD(sock);
  46. switch (cmd) {
  47. case SIOCOUTQ:
  48. if (sock->state != SS_CONNECTED ||
  49. !test_bit(ATM_VF_READY, &vcc->flags)) {
  50. error = -EINVAL;
  51. goto done;
  52. }
  53. error = put_user(sk->sk_sndbuf -
  54. atomic_read(&sk->sk_wmem_alloc),
  55. (int __user *) argp) ? -EFAULT : 0;
  56. goto done;
  57. case SIOCINQ:
  58. {
  59. struct sk_buff *skb;
  60. if (sock->state != SS_CONNECTED) {
  61. error = -EINVAL;
  62. goto done;
  63. }
  64. skb = skb_peek(&sk->sk_receive_queue);
  65. error = put_user(skb ? skb->len : 0,
  66. (int __user *)argp) ? -EFAULT : 0;
  67. goto done;
  68. }
  69. case SIOCGSTAMP: /* borrowed from IP */
  70. error = sock_get_timestamp(sk, argp);
  71. goto done;
  72. case SIOCGSTAMPNS: /* borrowed from IP */
  73. error = sock_get_timestampns(sk, argp);
  74. goto done;
  75. case ATM_SETSC:
  76. printk(KERN_WARNING "ATM_SETSC is obsolete\n");
  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 privledges that /proc/kcore needs
  90. */
  91. if (!capable(CAP_SYS_RAWIO)) {
  92. error = -EPERM;
  93. goto done;
  94. }
  95. error = sigd_attach(vcc);
  96. if (!error)
  97. sock->state = SS_CONNECTED;
  98. goto done;
  99. case ATM_SETBACKEND:
  100. case ATM_NEWBACKENDIF:
  101. {
  102. atm_backend_t backend;
  103. error = get_user(backend, (atm_backend_t __user *) argp);
  104. if (error)
  105. goto done;
  106. switch (backend) {
  107. case ATM_BACKEND_PPP:
  108. request_module("pppoatm");
  109. break;
  110. case ATM_BACKEND_BR2684:
  111. request_module("br2684");
  112. break;
  113. }
  114. }
  115. break;
  116. case ATMMPC_CTRL:
  117. case ATMMPC_DATA:
  118. request_module("mpoa");
  119. break;
  120. case ATMARPD_CTRL:
  121. request_module("clip");
  122. break;
  123. case ATMLEC_CTRL:
  124. request_module("lec");
  125. break;
  126. }
  127. error = -ENOIOCTLCMD;
  128. mutex_lock(&ioctl_mutex);
  129. list_for_each(pos, &ioctl_list) {
  130. struct atm_ioctl * ic = list_entry(pos, struct atm_ioctl, list);
  131. if (try_module_get(ic->owner)) {
  132. error = ic->ioctl(sock, cmd, arg);
  133. module_put(ic->owner);
  134. if (error != -ENOIOCTLCMD)
  135. break;
  136. }
  137. }
  138. mutex_unlock(&ioctl_mutex);
  139. if (error != -ENOIOCTLCMD)
  140. goto done;
  141. error = atm_dev_ioctl(cmd, argp);
  142. done:
  143. return error;
  144. }