sock.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. BNEP implementation for Linux Bluetooth stack (BlueZ).
  3. Copyright (C) 2001-2002 Inventel Systemes
  4. Written 2001-2002 by
  5. David Libault <david.libault@inventel.fr>
  6. Copyright (C) 2002 Maxim Krasnyansky <maxk@qualcomm.com>
  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/export.h>
  23. #include <linux/file.h>
  24. #include "bnep.h"
  25. static struct bt_sock_list bnep_sk_list = {
  26. .lock = __RW_LOCK_UNLOCKED(bnep_sk_list.lock)
  27. };
  28. static int bnep_sock_release(struct socket *sock)
  29. {
  30. struct sock *sk = sock->sk;
  31. BT_DBG("sock %p sk %p", sock, sk);
  32. if (!sk)
  33. return 0;
  34. bt_sock_unlink(&bnep_sk_list, sk);
  35. sock_orphan(sk);
  36. sock_put(sk);
  37. return 0;
  38. }
  39. static int bnep_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  40. {
  41. struct bnep_connlist_req cl;
  42. struct bnep_connadd_req ca;
  43. struct bnep_conndel_req cd;
  44. struct bnep_conninfo ci;
  45. struct socket *nsock;
  46. void __user *argp = (void __user *)arg;
  47. __u32 supp_feat = BIT(BNEP_SETUP_RESPONSE);
  48. int err;
  49. BT_DBG("cmd %x arg %lx", cmd, arg);
  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. #ifdef CONFIG_COMPAT
  103. static int bnep_sock_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  104. {
  105. if (cmd == BNEPGETCONNLIST) {
  106. struct bnep_connlist_req cl;
  107. u32 uci;
  108. int err;
  109. if (get_user(cl.cnum, (u32 __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 = bnep_get_connlist(&cl);
  116. if (!err && put_user(cl.cnum, (u32 __user *) arg))
  117. err = -EFAULT;
  118. return err;
  119. }
  120. return bnep_sock_ioctl(sock, cmd, arg);
  121. }
  122. #endif
  123. static const struct proto_ops bnep_sock_ops = {
  124. .family = PF_BLUETOOTH,
  125. .owner = THIS_MODULE,
  126. .release = bnep_sock_release,
  127. .ioctl = bnep_sock_ioctl,
  128. #ifdef CONFIG_COMPAT
  129. .compat_ioctl = bnep_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. .listen = sock_no_listen,
  136. .shutdown = sock_no_shutdown,
  137. .setsockopt = sock_no_setsockopt,
  138. .getsockopt = sock_no_getsockopt,
  139. .connect = sock_no_connect,
  140. .socketpair = sock_no_socketpair,
  141. .accept = sock_no_accept,
  142. .mmap = sock_no_mmap
  143. };
  144. static struct proto bnep_proto = {
  145. .name = "BNEP",
  146. .owner = THIS_MODULE,
  147. .obj_size = sizeof(struct bt_sock)
  148. };
  149. static int bnep_sock_create(struct net *net, struct socket *sock, int protocol,
  150. int kern)
  151. {
  152. struct sock *sk;
  153. BT_DBG("sock %p", sock);
  154. if (sock->type != SOCK_RAW)
  155. return -ESOCKTNOSUPPORT;
  156. sk = sk_alloc(net, PF_BLUETOOTH, GFP_ATOMIC, &bnep_proto, kern);
  157. if (!sk)
  158. return -ENOMEM;
  159. sock_init_data(sock, sk);
  160. sock->ops = &bnep_sock_ops;
  161. sock->state = SS_UNCONNECTED;
  162. sock_reset_flag(sk, SOCK_ZAPPED);
  163. sk->sk_protocol = protocol;
  164. sk->sk_state = BT_OPEN;
  165. bt_sock_link(&bnep_sk_list, sk);
  166. return 0;
  167. }
  168. static const struct net_proto_family bnep_sock_family_ops = {
  169. .family = PF_BLUETOOTH,
  170. .owner = THIS_MODULE,
  171. .create = bnep_sock_create
  172. };
  173. int __init bnep_sock_init(void)
  174. {
  175. int err;
  176. err = proto_register(&bnep_proto, 0);
  177. if (err < 0)
  178. return err;
  179. err = bt_sock_register(BTPROTO_BNEP, &bnep_sock_family_ops);
  180. if (err < 0) {
  181. BT_ERR("Can't register BNEP socket");
  182. goto error;
  183. }
  184. err = bt_procfs_init(&init_net, "bnep", &bnep_sk_list, NULL);
  185. if (err < 0) {
  186. BT_ERR("Failed to create BNEP proc file");
  187. bt_sock_unregister(BTPROTO_BNEP);
  188. goto error;
  189. }
  190. BT_INFO("BNEP socket layer initialized");
  191. return 0;
  192. error:
  193. proto_unregister(&bnep_proto);
  194. return err;
  195. }
  196. void __exit bnep_sock_cleanup(void)
  197. {
  198. bt_procfs_cleanup(&init_net, "bnep");
  199. bt_sock_unregister(BTPROTO_BNEP);
  200. proto_unregister(&bnep_proto);
  201. }