ioctl.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 ATM_SETSC:
  73. printk(KERN_WARNING "ATM_SETSC is obsolete\n");
  74. error = 0;
  75. goto done;
  76. case ATMSIGD_CTRL:
  77. if (!capable(CAP_NET_ADMIN)) {
  78. error = -EPERM;
  79. goto done;
  80. }
  81. /*
  82. * The user/kernel protocol for exchanging signalling
  83. * info uses kernel pointers as opaque references,
  84. * so the holder of the file descriptor can scribble
  85. * on the kernel... so we should make sure that we
  86. * have the same privledges that /proc/kcore needs
  87. */
  88. if (!capable(CAP_SYS_RAWIO)) {
  89. error = -EPERM;
  90. goto done;
  91. }
  92. error = sigd_attach(vcc);
  93. if (!error)
  94. sock->state = SS_CONNECTED;
  95. goto done;
  96. case ATM_SETBACKEND:
  97. case ATM_NEWBACKENDIF:
  98. {
  99. atm_backend_t backend;
  100. error = get_user(backend, (atm_backend_t __user *) argp);
  101. if (error)
  102. goto done;
  103. switch (backend) {
  104. case ATM_BACKEND_PPP:
  105. request_module("pppoatm");
  106. break;
  107. case ATM_BACKEND_BR2684:
  108. request_module("br2684");
  109. break;
  110. }
  111. }
  112. break;
  113. case ATMMPC_CTRL:
  114. case ATMMPC_DATA:
  115. request_module("mpoa");
  116. break;
  117. case ATMARPD_CTRL:
  118. request_module("clip");
  119. break;
  120. case ATMLEC_CTRL:
  121. request_module("lec");
  122. break;
  123. }
  124. error = -ENOIOCTLCMD;
  125. mutex_lock(&ioctl_mutex);
  126. list_for_each(pos, &ioctl_list) {
  127. struct atm_ioctl * ic = list_entry(pos, struct atm_ioctl, list);
  128. if (try_module_get(ic->owner)) {
  129. error = ic->ioctl(sock, cmd, arg);
  130. module_put(ic->owner);
  131. if (error != -ENOIOCTLCMD)
  132. break;
  133. }
  134. }
  135. mutex_unlock(&ioctl_mutex);
  136. if (error != -ENOIOCTLCMD)
  137. goto done;
  138. error = atm_dev_ioctl(cmd, argp);
  139. done:
  140. return error;
  141. }