sock.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. CMTP implementation for Linux Bluetooth stack (BlueZ).
  3. Copyright (C) 2002-2003 Marcel Holtmann <marcel@holtmann.org>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License version 2 as
  6. published by the Free Software Foundation;
  7. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  8. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  9. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
  10. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
  11. CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
  12. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
  16. COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
  17. SOFTWARE IS DISCLAIMED.
  18. */
  19. #include <linux/module.h>
  20. #include <linux/types.h>
  21. #include <linux/capability.h>
  22. #include <linux/errno.h>
  23. #include <linux/kernel.h>
  24. #include <linux/poll.h>
  25. #include <linux/fcntl.h>
  26. #include <linux/skbuff.h>
  27. #include <linux/socket.h>
  28. #include <linux/ioctl.h>
  29. #include <linux/file.h>
  30. #include <linux/compat.h>
  31. #include <linux/gfp.h>
  32. #include <linux/uaccess.h>
  33. #include <net/sock.h>
  34. #include <linux/isdn/capilli.h>
  35. #include <asm/system.h>
  36. #include "cmtp.h"
  37. static int cmtp_sock_release(struct socket *sock)
  38. {
  39. struct sock *sk = sock->sk;
  40. BT_DBG("sock %p sk %p", sock, sk);
  41. if (!sk)
  42. return 0;
  43. sock_orphan(sk);
  44. sock_put(sk);
  45. return 0;
  46. }
  47. static int cmtp_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  48. {
  49. struct cmtp_connadd_req ca;
  50. struct cmtp_conndel_req cd;
  51. struct cmtp_connlist_req cl;
  52. struct cmtp_conninfo ci;
  53. struct socket *nsock;
  54. void __user *argp = (void __user *)arg;
  55. int err;
  56. BT_DBG("cmd %x arg %lx", cmd, arg);
  57. switch (cmd) {
  58. case CMTPCONNADD:
  59. if (!capable(CAP_NET_ADMIN))
  60. return -EACCES;
  61. if (copy_from_user(&ca, argp, sizeof(ca)))
  62. return -EFAULT;
  63. nsock = sockfd_lookup(ca.sock, &err);
  64. if (!nsock)
  65. return err;
  66. if (nsock->sk->sk_state != BT_CONNECTED) {
  67. sockfd_put(nsock);
  68. return -EBADFD;
  69. }
  70. err = cmtp_add_connection(&ca, nsock);
  71. if (!err) {
  72. if (copy_to_user(argp, &ca, sizeof(ca)))
  73. err = -EFAULT;
  74. } else
  75. sockfd_put(nsock);
  76. return err;
  77. case CMTPCONNDEL:
  78. if (!capable(CAP_NET_ADMIN))
  79. return -EACCES;
  80. if (copy_from_user(&cd, argp, sizeof(cd)))
  81. return -EFAULT;
  82. return cmtp_del_connection(&cd);
  83. case CMTPGETCONNLIST:
  84. if (copy_from_user(&cl, argp, sizeof(cl)))
  85. return -EFAULT;
  86. if (cl.cnum <= 0)
  87. return -EINVAL;
  88. err = cmtp_get_connlist(&cl);
  89. if (!err && copy_to_user(argp, &cl, sizeof(cl)))
  90. return -EFAULT;
  91. return err;
  92. case CMTPGETCONNINFO:
  93. if (copy_from_user(&ci, argp, sizeof(ci)))
  94. return -EFAULT;
  95. err = cmtp_get_conninfo(&ci);
  96. if (!err && copy_to_user(argp, &ci, sizeof(ci)))
  97. return -EFAULT;
  98. return err;
  99. }
  100. return -EINVAL;
  101. }
  102. #ifdef CONFIG_COMPAT
  103. static int cmtp_sock_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  104. {
  105. if (cmd == CMTPGETCONNLIST) {
  106. struct cmtp_connlist_req cl;
  107. uint32_t uci;
  108. int err;
  109. if (get_user(cl.cnum, (uint32_t __user *) arg) ||
  110. get_user(uci, (u32 __user *) (arg + 4)))
  111. return -EFAULT;
  112. cl.ci = compat_ptr(uci);
  113. if (cl.cnum <= 0)
  114. return -EINVAL;
  115. err = cmtp_get_connlist(&cl);
  116. if (!err && put_user(cl.cnum, (uint32_t __user *) arg))
  117. err = -EFAULT;
  118. return err;
  119. }
  120. return cmtp_sock_ioctl(sock, cmd, arg);
  121. }
  122. #endif
  123. static const struct proto_ops cmtp_sock_ops = {
  124. .family = PF_BLUETOOTH,
  125. .owner = THIS_MODULE,
  126. .release = cmtp_sock_release,
  127. .ioctl = cmtp_sock_ioctl,
  128. #ifdef CONFIG_COMPAT
  129. .compat_ioctl = cmtp_sock_compat_ioctl,
  130. #endif
  131. .bind = sock_no_bind,
  132. .getname = sock_no_getname,
  133. .sendmsg = sock_no_sendmsg,
  134. .recvmsg = sock_no_recvmsg,
  135. .poll = sock_no_poll,
  136. .listen = sock_no_listen,
  137. .shutdown = sock_no_shutdown,
  138. .setsockopt = sock_no_setsockopt,
  139. .getsockopt = sock_no_getsockopt,
  140. .connect = sock_no_connect,
  141. .socketpair = sock_no_socketpair,
  142. .accept = sock_no_accept,
  143. .mmap = sock_no_mmap
  144. };
  145. static struct proto cmtp_proto = {
  146. .name = "CMTP",
  147. .owner = THIS_MODULE,
  148. .obj_size = sizeof(struct bt_sock)
  149. };
  150. static int cmtp_sock_create(struct net *net, struct socket *sock, int protocol,
  151. int kern)
  152. {
  153. struct sock *sk;
  154. BT_DBG("sock %p", sock);
  155. if (sock->type != SOCK_RAW)
  156. return -ESOCKTNOSUPPORT;
  157. sk = sk_alloc(net, PF_BLUETOOTH, GFP_ATOMIC, &cmtp_proto);
  158. if (!sk)
  159. return -ENOMEM;
  160. sock_init_data(sock, sk);
  161. sock->ops = &cmtp_sock_ops;
  162. sock->state = SS_UNCONNECTED;
  163. sock_reset_flag(sk, SOCK_ZAPPED);
  164. sk->sk_protocol = protocol;
  165. sk->sk_state = BT_OPEN;
  166. return 0;
  167. }
  168. static const struct net_proto_family cmtp_sock_family_ops = {
  169. .family = PF_BLUETOOTH,
  170. .owner = THIS_MODULE,
  171. .create = cmtp_sock_create
  172. };
  173. int cmtp_init_sockets(void)
  174. {
  175. int err;
  176. err = proto_register(&cmtp_proto, 0);
  177. if (err < 0)
  178. return err;
  179. err = bt_sock_register(BTPROTO_CMTP, &cmtp_sock_family_ops);
  180. if (err < 0)
  181. goto error;
  182. return 0;
  183. error:
  184. BT_ERR("Can't register CMTP socket");
  185. proto_unregister(&cmtp_proto);
  186. return err;
  187. }
  188. void cmtp_cleanup_sockets(void)
  189. {
  190. if (bt_sock_unregister(BTPROTO_CMTP) < 0)
  191. BT_ERR("Can't unregister CMTP socket");
  192. proto_unregister(&cmtp_proto);
  193. }