PageRenderTime 37ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/net/bluetooth/hidp/sock.c

https://gitlab.com/Sean.W/pru-linux-drivers
C | 304 lines | 218 code | 62 blank | 24 comment | 39 complexity | 713139051cd21626e83a3138054a5fa9 MD5 | raw file
  1. /*
  2. HIDP implementation for Linux Bluetooth stack (BlueZ).
  3. Copyright (C) 2003-2004 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/slab.h>
  25. #include <linux/poll.h>
  26. #include <linux/fcntl.h>
  27. #include <linux/skbuff.h>
  28. #include <linux/socket.h>
  29. #include <linux/ioctl.h>
  30. #include <linux/file.h>
  31. #include <linux/init.h>
  32. #include <linux/compat.h>
  33. #include <net/sock.h>
  34. #include "hidp.h"
  35. static int hidp_sock_release(struct socket *sock)
  36. {
  37. struct sock *sk = sock->sk;
  38. BT_DBG("sock %p sk %p", sock, sk);
  39. if (!sk)
  40. return 0;
  41. sock_orphan(sk);
  42. sock_put(sk);
  43. return 0;
  44. }
  45. static int hidp_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  46. {
  47. void __user *argp = (void __user *) arg;
  48. struct hidp_connadd_req ca;
  49. struct hidp_conndel_req cd;
  50. struct hidp_connlist_req cl;
  51. struct hidp_conninfo ci;
  52. struct socket *csock;
  53. struct socket *isock;
  54. int err;
  55. BT_DBG("cmd %x arg %lx", cmd, arg);
  56. switch (cmd) {
  57. case HIDPCONNADD:
  58. if (!capable(CAP_NET_ADMIN))
  59. return -EACCES;
  60. if (copy_from_user(&ca, argp, sizeof(ca)))
  61. return -EFAULT;
  62. csock = sockfd_lookup(ca.ctrl_sock, &err);
  63. if (!csock)
  64. return err;
  65. isock = sockfd_lookup(ca.intr_sock, &err);
  66. if (!isock) {
  67. sockfd_put(csock);
  68. return err;
  69. }
  70. if (csock->sk->sk_state != BT_CONNECTED || isock->sk->sk_state != BT_CONNECTED) {
  71. sockfd_put(csock);
  72. sockfd_put(isock);
  73. return -EBADFD;
  74. }
  75. err = hidp_add_connection(&ca, csock, isock);
  76. if (!err) {
  77. if (copy_to_user(argp, &ca, sizeof(ca)))
  78. err = -EFAULT;
  79. } else {
  80. sockfd_put(csock);
  81. sockfd_put(isock);
  82. }
  83. return err;
  84. case HIDPCONNDEL:
  85. if (!capable(CAP_NET_ADMIN))
  86. return -EACCES;
  87. if (copy_from_user(&cd, argp, sizeof(cd)))
  88. return -EFAULT;
  89. return hidp_del_connection(&cd);
  90. case HIDPGETCONNLIST:
  91. if (copy_from_user(&cl, argp, sizeof(cl)))
  92. return -EFAULT;
  93. if (cl.cnum <= 0)
  94. return -EINVAL;
  95. err = hidp_get_connlist(&cl);
  96. if (!err && copy_to_user(argp, &cl, sizeof(cl)))
  97. return -EFAULT;
  98. return err;
  99. case HIDPGETCONNINFO:
  100. if (copy_from_user(&ci, argp, sizeof(ci)))
  101. return -EFAULT;
  102. err = hidp_get_conninfo(&ci);
  103. if (!err && copy_to_user(argp, &ci, sizeof(ci)))
  104. return -EFAULT;
  105. return err;
  106. }
  107. return -EINVAL;
  108. }
  109. #ifdef CONFIG_COMPAT
  110. struct compat_hidp_connadd_req {
  111. int ctrl_sock; // Connected control socket
  112. int intr_sock; // Connteted interrupt socket
  113. __u16 parser;
  114. __u16 rd_size;
  115. compat_uptr_t rd_data;
  116. __u8 country;
  117. __u8 subclass;
  118. __u16 vendor;
  119. __u16 product;
  120. __u16 version;
  121. __u32 flags;
  122. __u32 idle_to;
  123. char name[128];
  124. };
  125. static int hidp_sock_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  126. {
  127. if (cmd == HIDPGETCONNLIST) {
  128. struct hidp_connlist_req cl;
  129. uint32_t uci;
  130. int err;
  131. if (get_user(cl.cnum, (uint32_t __user *) arg) ||
  132. get_user(uci, (u32 __user *) (arg + 4)))
  133. return -EFAULT;
  134. cl.ci = compat_ptr(uci);
  135. if (cl.cnum <= 0)
  136. return -EINVAL;
  137. err = hidp_get_connlist(&cl);
  138. if (!err && put_user(cl.cnum, (uint32_t __user *) arg))
  139. err = -EFAULT;
  140. return err;
  141. } else if (cmd == HIDPCONNADD) {
  142. struct compat_hidp_connadd_req ca;
  143. struct hidp_connadd_req __user *uca;
  144. uca = compat_alloc_user_space(sizeof(*uca));
  145. if (copy_from_user(&ca, (void __user *) arg, sizeof(ca)))
  146. return -EFAULT;
  147. if (put_user(ca.ctrl_sock, &uca->ctrl_sock) ||
  148. put_user(ca.intr_sock, &uca->intr_sock) ||
  149. put_user(ca.parser, &uca->parser) ||
  150. put_user(ca.rd_size, &uca->rd_size) ||
  151. put_user(compat_ptr(ca.rd_data), &uca->rd_data) ||
  152. put_user(ca.country, &uca->country) ||
  153. put_user(ca.subclass, &uca->subclass) ||
  154. put_user(ca.vendor, &uca->vendor) ||
  155. put_user(ca.product, &uca->product) ||
  156. put_user(ca.version, &uca->version) ||
  157. put_user(ca.flags, &uca->flags) ||
  158. put_user(ca.idle_to, &uca->idle_to) ||
  159. copy_to_user(&uca->name[0], &ca.name[0], 128))
  160. return -EFAULT;
  161. arg = (unsigned long) uca;
  162. /* Fall through. We don't actually write back any _changes_
  163. to the structure anyway, so there's no need to copy back
  164. into the original compat version */
  165. }
  166. return hidp_sock_ioctl(sock, cmd, arg);
  167. }
  168. #endif
  169. static const struct proto_ops hidp_sock_ops = {
  170. .family = PF_BLUETOOTH,
  171. .owner = THIS_MODULE,
  172. .release = hidp_sock_release,
  173. .ioctl = hidp_sock_ioctl,
  174. #ifdef CONFIG_COMPAT
  175. .compat_ioctl = hidp_sock_compat_ioctl,
  176. #endif
  177. .bind = sock_no_bind,
  178. .getname = sock_no_getname,
  179. .sendmsg = sock_no_sendmsg,
  180. .recvmsg = sock_no_recvmsg,
  181. .poll = sock_no_poll,
  182. .listen = sock_no_listen,
  183. .shutdown = sock_no_shutdown,
  184. .setsockopt = sock_no_setsockopt,
  185. .getsockopt = sock_no_getsockopt,
  186. .connect = sock_no_connect,
  187. .socketpair = sock_no_socketpair,
  188. .accept = sock_no_accept,
  189. .mmap = sock_no_mmap
  190. };
  191. static struct proto hidp_proto = {
  192. .name = "HIDP",
  193. .owner = THIS_MODULE,
  194. .obj_size = sizeof(struct bt_sock)
  195. };
  196. static int hidp_sock_create(struct net *net, struct socket *sock, int protocol,
  197. int kern)
  198. {
  199. struct sock *sk;
  200. BT_DBG("sock %p", sock);
  201. if (sock->type != SOCK_RAW)
  202. return -ESOCKTNOSUPPORT;
  203. sk = sk_alloc(net, PF_BLUETOOTH, GFP_ATOMIC, &hidp_proto);
  204. if (!sk)
  205. return -ENOMEM;
  206. sock_init_data(sock, sk);
  207. sock->ops = &hidp_sock_ops;
  208. sock->state = SS_UNCONNECTED;
  209. sock_reset_flag(sk, SOCK_ZAPPED);
  210. sk->sk_protocol = protocol;
  211. sk->sk_state = BT_OPEN;
  212. return 0;
  213. }
  214. static const struct net_proto_family hidp_sock_family_ops = {
  215. .family = PF_BLUETOOTH,
  216. .owner = THIS_MODULE,
  217. .create = hidp_sock_create
  218. };
  219. int __init hidp_init_sockets(void)
  220. {
  221. int err;
  222. err = proto_register(&hidp_proto, 0);
  223. if (err < 0)
  224. return err;
  225. err = bt_sock_register(BTPROTO_HIDP, &hidp_sock_family_ops);
  226. if (err < 0)
  227. goto error;
  228. return 0;
  229. error:
  230. BT_ERR("Can't register HIDP socket");
  231. proto_unregister(&hidp_proto);
  232. return err;
  233. }
  234. void __exit hidp_cleanup_sockets(void)
  235. {
  236. if (bt_sock_unregister(BTPROTO_HIDP) < 0)
  237. BT_ERR("Can't unregister HIDP socket");
  238. proto_unregister(&hidp_proto);
  239. }