sock.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. BNEP implementation for Linux Bluetooth stack (BlueZ).
  3. Copyright (C) 2001-2002 Inventel Systemes
  4. Written 2001-2002 by
  5. David Libault <[email protected]>
  6. Copyright (C) 2002 Maxim Krasnyansky <[email protected]>
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License version 2 as
  9. published by the Free Software Foundation;
  10. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  11. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  12. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
  13. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
  14. CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
  15. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  16. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  17. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  18. ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
  19. COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
  20. SOFTWARE IS DISCLAIMED.
  21. */
  22. #include <linux/compat.h>
  23. #include <linux/export.h>
  24. #include <linux/file.h>
  25. #include "bnep.h"
  26. static struct bt_sock_list bnep_sk_list = {
  27. .lock = __RW_LOCK_UNLOCKED(bnep_sk_list.lock)
  28. };
  29. static int bnep_sock_release(struct socket *sock)
  30. {
  31. struct sock *sk = sock->sk;
  32. BT_DBG("sock %p sk %p", sock, sk);
  33. if (!sk)
  34. return 0;
  35. bt_sock_unlink(&bnep_sk_list, sk);
  36. sock_orphan(sk);
  37. sock_put(sk);
  38. return 0;
  39. }
  40. static int do_bnep_sock_ioctl(struct socket *sock, unsigned int cmd, void __user *argp)
  41. {
  42. struct bnep_connlist_req cl;
  43. struct bnep_connadd_req ca;
  44. struct bnep_conndel_req cd;
  45. struct bnep_conninfo ci;
  46. struct socket *nsock;
  47. __u32 supp_feat = BIT(BNEP_SETUP_RESPONSE);
  48. int err;
  49. BT_DBG("cmd %x arg %p", cmd, argp);
  50. switch (cmd) {
  51. case BNEPCONNADD:
  52. if (!capable(CAP_NET_ADMIN))
  53. return -EPERM;
  54. if (copy_from_user(&ca, argp, sizeof(ca)))
  55. return -EFAULT;
  56. nsock = sockfd_lookup(ca.sock, &err);
  57. if (!nsock)
  58. return err;
  59. if (nsock->sk->sk_state != BT_CONNECTED) {
  60. sockfd_put(nsock);
  61. return -EBADFD;
  62. }
  63. ca.device[sizeof(ca.device)-1] = 0;
  64. err = bnep_add_connection(&ca, nsock);
  65. if (!err) {
  66. if (copy_to_user(argp, &ca, sizeof(ca)))
  67. err = -EFAULT;
  68. } else
  69. sockfd_put(nsock);
  70. return err;
  71. case BNEPCONNDEL:
  72. if (!capable(CAP_NET_ADMIN))
  73. return -EPERM;
  74. if (copy_from_user(&cd, argp, sizeof(cd)))
  75. return -EFAULT;
  76. return bnep_del_connection(&cd);
  77. case BNEPGETCONNLIST:
  78. if (copy_from_user(&cl, argp, sizeof(cl)))
  79. return -EFAULT;
  80. if (cl.cnum <= 0)
  81. return -EINVAL;
  82. err = bnep_get_connlist(&cl);
  83. if (!err && copy_to_user(argp, &cl, sizeof(cl)))
  84. return -EFAULT;
  85. return err;
  86. case BNEPGETCONNINFO:
  87. if (copy_from_user(&ci, argp, sizeof(ci)))
  88. return -EFAULT;
  89. err = bnep_get_conninfo(&ci);
  90. if (!err && copy_to_user(argp, &ci, sizeof(ci)))
  91. return -EFAULT;
  92. return err;
  93. case BNEPGETSUPPFEAT:
  94. if (copy_to_user(argp, &supp_feat, sizeof(supp_feat)))
  95. return -EFAULT;
  96. return 0;
  97. default:
  98. return -EINVAL;
  99. }
  100. return 0;
  101. }
  102. static int bnep_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  103. {
  104. return do_bnep_sock_ioctl(sock, cmd, (void __user *)arg);
  105. }
  106. #ifdef CONFIG_COMPAT
  107. static int bnep_sock_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  108. {
  109. void __user *argp = compat_ptr(arg);
  110. if (cmd == BNEPGETCONNLIST) {
  111. struct bnep_connlist_req cl;
  112. unsigned __user *p = argp;
  113. u32 uci;
  114. int err;
  115. if (get_user(cl.cnum, p) || get_user(uci, p + 1))
  116. return -EFAULT;
  117. cl.ci = compat_ptr(uci);
  118. if (cl.cnum <= 0)
  119. return -EINVAL;
  120. err = bnep_get_connlist(&cl);
  121. if (!err && put_user(cl.cnum, p))
  122. err = -EFAULT;
  123. return err;
  124. }
  125. return do_bnep_sock_ioctl(sock, cmd, argp);
  126. }
  127. #endif
  128. static const struct proto_ops bnep_sock_ops = {
  129. .family = PF_BLUETOOTH,
  130. .owner = THIS_MODULE,
  131. .release = bnep_sock_release,
  132. .ioctl = bnep_sock_ioctl,
  133. #ifdef CONFIG_COMPAT
  134. .compat_ioctl = bnep_sock_compat_ioctl,
  135. #endif
  136. .bind = sock_no_bind,
  137. .getname = sock_no_getname,
  138. .sendmsg = sock_no_sendmsg,
  139. .recvmsg = sock_no_recvmsg,
  140. .listen = sock_no_listen,
  141. .shutdown = sock_no_shutdown,
  142. .connect = sock_no_connect,
  143. .socketpair = sock_no_socketpair,
  144. .accept = sock_no_accept,
  145. .mmap = sock_no_mmap
  146. };
  147. static struct proto bnep_proto = {
  148. .name = "BNEP",
  149. .owner = THIS_MODULE,
  150. .obj_size = sizeof(struct bt_sock)
  151. };
  152. static int bnep_sock_create(struct net *net, struct socket *sock, int protocol,
  153. int kern)
  154. {
  155. struct sock *sk;
  156. BT_DBG("sock %p", sock);
  157. if (sock->type != SOCK_RAW)
  158. return -ESOCKTNOSUPPORT;
  159. sk = sk_alloc(net, PF_BLUETOOTH, GFP_ATOMIC, &bnep_proto, kern);
  160. if (!sk)
  161. return -ENOMEM;
  162. sock_init_data(sock, sk);
  163. sock->ops = &bnep_sock_ops;
  164. sock->state = SS_UNCONNECTED;
  165. sock_reset_flag(sk, SOCK_ZAPPED);
  166. sk->sk_protocol = protocol;
  167. sk->sk_state = BT_OPEN;
  168. bt_sock_link(&bnep_sk_list, sk);
  169. return 0;
  170. }
  171. static const struct net_proto_family bnep_sock_family_ops = {
  172. .family = PF_BLUETOOTH,
  173. .owner = THIS_MODULE,
  174. .create = bnep_sock_create
  175. };
  176. int __init bnep_sock_init(void)
  177. {
  178. int err;
  179. err = proto_register(&bnep_proto, 0);
  180. if (err < 0)
  181. return err;
  182. err = bt_sock_register(BTPROTO_BNEP, &bnep_sock_family_ops);
  183. if (err < 0) {
  184. BT_ERR("Can't register BNEP socket");
  185. goto error;
  186. }
  187. err = bt_procfs_init(&init_net, "bnep", &bnep_sk_list, NULL);
  188. if (err < 0) {
  189. BT_ERR("Failed to create BNEP proc file");
  190. bt_sock_unregister(BTPROTO_BNEP);
  191. goto error;
  192. }
  193. BT_INFO("BNEP socket layer initialized");
  194. return 0;
  195. error:
  196. proto_unregister(&bnep_proto);
  197. return err;
  198. }
  199. void __exit bnep_sock_cleanup(void)
  200. {
  201. bt_procfs_cleanup(&init_net, "bnep");
  202. bt_sock_unregister(BTPROTO_BNEP);
  203. proto_unregister(&bnep_proto);
  204. }