sock.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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/export.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 "cmtp.h"
  36. static struct bt_sock_list cmtp_sk_list = {
  37. .lock = __RW_LOCK_UNLOCKED(cmtp_sk_list.lock)
  38. };
  39. static int cmtp_sock_release(struct socket *sock)
  40. {
  41. struct sock *sk = sock->sk;
  42. BT_DBG("sock %p sk %p", sock, sk);
  43. if (!sk)
  44. return 0;
  45. bt_sock_unlink(&cmtp_sk_list, sk);
  46. sock_orphan(sk);
  47. sock_put(sk);
  48. return 0;
  49. }
  50. static int cmtp_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  51. {
  52. struct cmtp_connadd_req ca;
  53. struct cmtp_conndel_req cd;
  54. struct cmtp_connlist_req cl;
  55. struct cmtp_conninfo ci;
  56. struct socket *nsock;
  57. void __user *argp = (void __user *)arg;
  58. int err;
  59. BT_DBG("cmd %x arg %lx", cmd, arg);
  60. switch (cmd) {
  61. case CMTPCONNADD:
  62. if (!capable(CAP_NET_ADMIN))
  63. return -EPERM;
  64. if (copy_from_user(&ca, argp, sizeof(ca)))
  65. return -EFAULT;
  66. nsock = sockfd_lookup(ca.sock, &err);
  67. if (!nsock)
  68. return err;
  69. if (nsock->sk->sk_state != BT_CONNECTED) {
  70. sockfd_put(nsock);
  71. return -EBADFD;
  72. }
  73. err = cmtp_add_connection(&ca, nsock);
  74. if (!err) {
  75. if (copy_to_user(argp, &ca, sizeof(ca)))
  76. err = -EFAULT;
  77. } else
  78. sockfd_put(nsock);
  79. return err;
  80. case CMTPCONNDEL:
  81. if (!capable(CAP_NET_ADMIN))
  82. return -EPERM;
  83. if (copy_from_user(&cd, argp, sizeof(cd)))
  84. return -EFAULT;
  85. return cmtp_del_connection(&cd);
  86. case CMTPGETCONNLIST:
  87. if (copy_from_user(&cl, argp, sizeof(cl)))
  88. return -EFAULT;
  89. if (cl.cnum <= 0)
  90. return -EINVAL;
  91. err = cmtp_get_connlist(&cl);
  92. if (!err && copy_to_user(argp, &cl, sizeof(cl)))
  93. return -EFAULT;
  94. return err;
  95. case CMTPGETCONNINFO:
  96. if (copy_from_user(&ci, argp, sizeof(ci)))
  97. return -EFAULT;
  98. err = cmtp_get_conninfo(&ci);
  99. if (!err && copy_to_user(argp, &ci, sizeof(ci)))
  100. return -EFAULT;
  101. return err;
  102. }
  103. return -EINVAL;
  104. }
  105. #ifdef CONFIG_COMPAT
  106. static int cmtp_sock_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  107. {
  108. if (cmd == CMTPGETCONNLIST) {
  109. struct cmtp_connlist_req cl;
  110. u32 uci;
  111. int err;
  112. if (get_user(cl.cnum, (u32 __user *) arg) ||
  113. get_user(uci, (u32 __user *) (arg + 4)))
  114. return -EFAULT;
  115. cl.ci = compat_ptr(uci);
  116. if (cl.cnum <= 0)
  117. return -EINVAL;
  118. err = cmtp_get_connlist(&cl);
  119. if (!err && put_user(cl.cnum, (u32 __user *) arg))
  120. err = -EFAULT;
  121. return err;
  122. }
  123. return cmtp_sock_ioctl(sock, cmd, arg);
  124. }
  125. #endif
  126. static const struct proto_ops cmtp_sock_ops = {
  127. .family = PF_BLUETOOTH,
  128. .owner = THIS_MODULE,
  129. .release = cmtp_sock_release,
  130. .ioctl = cmtp_sock_ioctl,
  131. #ifdef CONFIG_COMPAT
  132. .compat_ioctl = cmtp_sock_compat_ioctl,
  133. #endif
  134. .bind = sock_no_bind,
  135. .getname = sock_no_getname,
  136. .sendmsg = sock_no_sendmsg,
  137. .recvmsg = sock_no_recvmsg,
  138. .poll = sock_no_poll,
  139. .listen = sock_no_listen,
  140. .shutdown = sock_no_shutdown,
  141. .setsockopt = sock_no_setsockopt,
  142. .getsockopt = sock_no_getsockopt,
  143. .connect = sock_no_connect,
  144. .socketpair = sock_no_socketpair,
  145. .accept = sock_no_accept,
  146. .mmap = sock_no_mmap
  147. };
  148. static struct proto cmtp_proto = {
  149. .name = "CMTP",
  150. .owner = THIS_MODULE,
  151. .obj_size = sizeof(struct bt_sock)
  152. };
  153. static int cmtp_sock_create(struct net *net, struct socket *sock, int protocol,
  154. int kern)
  155. {
  156. struct sock *sk;
  157. BT_DBG("sock %p", sock);
  158. if (sock->type != SOCK_RAW)
  159. return -ESOCKTNOSUPPORT;
  160. sk = sk_alloc(net, PF_BLUETOOTH, GFP_ATOMIC, &cmtp_proto, kern);
  161. if (!sk)
  162. return -ENOMEM;
  163. sock_init_data(sock, sk);
  164. sock->ops = &cmtp_sock_ops;
  165. sock->state = SS_UNCONNECTED;
  166. sock_reset_flag(sk, SOCK_ZAPPED);
  167. sk->sk_protocol = protocol;
  168. sk->sk_state = BT_OPEN;
  169. bt_sock_link(&cmtp_sk_list, sk);
  170. return 0;
  171. }
  172. static const struct net_proto_family cmtp_sock_family_ops = {
  173. .family = PF_BLUETOOTH,
  174. .owner = THIS_MODULE,
  175. .create = cmtp_sock_create
  176. };
  177. int cmtp_init_sockets(void)
  178. {
  179. int err;
  180. err = proto_register(&cmtp_proto, 0);
  181. if (err < 0)
  182. return err;
  183. err = bt_sock_register(BTPROTO_CMTP, &cmtp_sock_family_ops);
  184. if (err < 0) {
  185. BT_ERR("Can't register CMTP socket");
  186. goto error;
  187. }
  188. err = bt_procfs_init(&init_net, "cmtp", &cmtp_sk_list, NULL);
  189. if (err < 0) {
  190. BT_ERR("Failed to create CMTP proc file");
  191. bt_sock_unregister(BTPROTO_HIDP);
  192. goto error;
  193. }
  194. BT_INFO("CMTP socket layer initialized");
  195. return 0;
  196. error:
  197. proto_unregister(&cmtp_proto);
  198. return err;
  199. }
  200. void cmtp_cleanup_sockets(void)
  201. {
  202. bt_procfs_cleanup(&init_net, "cmtp");
  203. bt_sock_unregister(BTPROTO_CMTP);
  204. proto_unregister(&cmtp_proto);
  205. }