af_bluetooth.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. /*
  2. BlueZ - Bluetooth protocol stack for Linux
  3. Copyright (C) 2000-2001 Qualcomm Incorporated
  4. Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License version 2 as
  7. published by the Free Software Foundation;
  8. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  9. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  10. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
  11. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
  12. CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
  13. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
  17. COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
  18. SOFTWARE IS DISCLAIMED.
  19. */
  20. /* Bluetooth address family and sockets. */
  21. #include <linux/module.h>
  22. #include <linux/types.h>
  23. #include <linux/list.h>
  24. #include <linux/errno.h>
  25. #include <linux/kernel.h>
  26. #include <linux/sched.h>
  27. #include <linux/skbuff.h>
  28. #include <linux/init.h>
  29. #include <linux/poll.h>
  30. #include <net/sock.h>
  31. #include <asm/ioctls.h>
  32. #include <linux/kmod.h>
  33. #include <net/bluetooth/bluetooth.h>
  34. #ifdef CONFIG_ANDROID_PARANOID_NETWORK
  35. #include <linux/android_aid.h>
  36. #endif
  37. #ifndef CONFIG_BT_SOCK_DEBUG
  38. #undef BT_DBG
  39. #define BT_DBG(D...)
  40. #endif
  41. #define VERSION "2.16"
  42. /* Bluetooth sockets */
  43. #define BT_MAX_PROTO 8
  44. static const struct net_proto_family *bt_proto[BT_MAX_PROTO];
  45. static DEFINE_RWLOCK(bt_proto_lock);
  46. static struct lock_class_key bt_lock_key[BT_MAX_PROTO];
  47. static const char *const bt_key_strings[BT_MAX_PROTO] = {
  48. "sk_lock-AF_BLUETOOTH-BTPROTO_L2CAP",
  49. "sk_lock-AF_BLUETOOTH-BTPROTO_HCI",
  50. "sk_lock-AF_BLUETOOTH-BTPROTO_SCO",
  51. "sk_lock-AF_BLUETOOTH-BTPROTO_RFCOMM",
  52. "sk_lock-AF_BLUETOOTH-BTPROTO_BNEP",
  53. "sk_lock-AF_BLUETOOTH-BTPROTO_CMTP",
  54. "sk_lock-AF_BLUETOOTH-BTPROTO_HIDP",
  55. "sk_lock-AF_BLUETOOTH-BTPROTO_AVDTP",
  56. };
  57. static struct lock_class_key bt_slock_key[BT_MAX_PROTO];
  58. static const char *const bt_slock_key_strings[BT_MAX_PROTO] = {
  59. "slock-AF_BLUETOOTH-BTPROTO_L2CAP",
  60. "slock-AF_BLUETOOTH-BTPROTO_HCI",
  61. "slock-AF_BLUETOOTH-BTPROTO_SCO",
  62. "slock-AF_BLUETOOTH-BTPROTO_RFCOMM",
  63. "slock-AF_BLUETOOTH-BTPROTO_BNEP",
  64. "slock-AF_BLUETOOTH-BTPROTO_CMTP",
  65. "slock-AF_BLUETOOTH-BTPROTO_HIDP",
  66. "slock-AF_BLUETOOTH-BTPROTO_AVDTP",
  67. };
  68. static inline void bt_sock_reclassify_lock(struct socket *sock, int proto)
  69. {
  70. struct sock *sk = sock->sk;
  71. if (!sk)
  72. return;
  73. BUG_ON(sock_owned_by_user(sk));
  74. sock_lock_init_class_and_name(sk,
  75. bt_slock_key_strings[proto], &bt_slock_key[proto],
  76. bt_key_strings[proto], &bt_lock_key[proto]);
  77. }
  78. int bt_sock_register(int proto, const struct net_proto_family *ops)
  79. {
  80. int err = 0;
  81. if (proto < 0 || proto >= BT_MAX_PROTO)
  82. return -EINVAL;
  83. write_lock(&bt_proto_lock);
  84. if (bt_proto[proto])
  85. err = -EEXIST;
  86. else
  87. bt_proto[proto] = ops;
  88. write_unlock(&bt_proto_lock);
  89. return err;
  90. }
  91. EXPORT_SYMBOL(bt_sock_register);
  92. int bt_sock_unregister(int proto)
  93. {
  94. int err = 0;
  95. if (proto < 0 || proto >= BT_MAX_PROTO)
  96. return -EINVAL;
  97. write_lock(&bt_proto_lock);
  98. if (!bt_proto[proto])
  99. err = -ENOENT;
  100. else
  101. bt_proto[proto] = NULL;
  102. write_unlock(&bt_proto_lock);
  103. return err;
  104. }
  105. EXPORT_SYMBOL(bt_sock_unregister);
  106. #ifdef CONFIG_ANDROID_PARANOID_NETWORK
  107. static inline int current_has_bt_admin(void)
  108. {
  109. return (!current_euid() || in_egroup_p(AID_NET_BT_ADMIN));
  110. }
  111. static inline int current_has_bt(void)
  112. {
  113. return (current_has_bt_admin() || in_egroup_p(AID_NET_BT));
  114. }
  115. # else
  116. static inline int current_has_bt_admin(void)
  117. {
  118. return 1;
  119. }
  120. static inline int current_has_bt(void)
  121. {
  122. return 1;
  123. }
  124. #endif
  125. static int bt_sock_create(struct net *net, struct socket *sock, int proto,
  126. int kern)
  127. {
  128. int err;
  129. if (proto == BTPROTO_RFCOMM || proto == BTPROTO_SCO ||
  130. proto == BTPROTO_L2CAP) {
  131. if (!current_has_bt())
  132. return -EPERM;
  133. } else if (!current_has_bt_admin())
  134. return -EPERM;
  135. if (net != &init_net)
  136. return -EAFNOSUPPORT;
  137. if (proto < 0 || proto >= BT_MAX_PROTO)
  138. return -EINVAL;
  139. if (!bt_proto[proto])
  140. request_module("bt-proto-%d", proto);
  141. err = -EPROTONOSUPPORT;
  142. read_lock(&bt_proto_lock);
  143. if (bt_proto[proto] && try_module_get(bt_proto[proto]->owner)) {
  144. err = bt_proto[proto]->create(net, sock, proto, kern);
  145. bt_sock_reclassify_lock(sock, proto);
  146. module_put(bt_proto[proto]->owner);
  147. }
  148. read_unlock(&bt_proto_lock);
  149. return err;
  150. }
  151. void bt_sock_link(struct bt_sock_list *l, struct sock *sk)
  152. {
  153. write_lock_bh(&l->lock);
  154. sk_add_node(sk, &l->head);
  155. write_unlock_bh(&l->lock);
  156. }
  157. EXPORT_SYMBOL(bt_sock_link);
  158. void bt_sock_unlink(struct bt_sock_list *l, struct sock *sk)
  159. {
  160. write_lock_bh(&l->lock);
  161. sk_del_node_init(sk);
  162. write_unlock_bh(&l->lock);
  163. }
  164. EXPORT_SYMBOL(bt_sock_unlink);
  165. void bt_accept_enqueue(struct sock *parent, struct sock *sk)
  166. {
  167. BT_DBG("parent %p, sk %p", parent, sk);
  168. sock_hold(sk);
  169. list_add_tail(&bt_sk(sk)->accept_q, &bt_sk(parent)->accept_q);
  170. bt_sk(sk)->parent = parent;
  171. parent->sk_ack_backlog++;
  172. }
  173. EXPORT_SYMBOL(bt_accept_enqueue);
  174. void bt_accept_unlink(struct sock *sk)
  175. {
  176. BT_DBG("sk %p state %d", sk, sk->sk_state);
  177. list_del_init(&bt_sk(sk)->accept_q);
  178. bt_sk(sk)->parent->sk_ack_backlog--;
  179. bt_sk(sk)->parent = NULL;
  180. sock_put(sk);
  181. }
  182. EXPORT_SYMBOL(bt_accept_unlink);
  183. struct sock *bt_accept_dequeue(struct sock *parent, struct socket *newsock)
  184. {
  185. struct list_head *p, *n;
  186. struct sock *sk;
  187. BT_DBG("parent %p", parent);
  188. local_bh_disable();
  189. list_for_each_safe(p, n, &bt_sk(parent)->accept_q) {
  190. sk = (struct sock *) list_entry(p, struct bt_sock, accept_q);
  191. bh_lock_sock(sk);
  192. /* FIXME: Is this check still needed */
  193. if (sk->sk_state == BT_CLOSED) {
  194. bh_unlock_sock(sk);
  195. bt_accept_unlink(sk);
  196. continue;
  197. }
  198. if (sk->sk_state == BT_CONNECTED || !newsock ||
  199. bt_sk(parent)->defer_setup) {
  200. bt_accept_unlink(sk);
  201. if (newsock)
  202. sock_graft(sk, newsock);
  203. bh_unlock_sock(sk);
  204. local_bh_enable();
  205. return sk;
  206. }
  207. bh_unlock_sock(sk);
  208. }
  209. local_bh_enable();
  210. return NULL;
  211. }
  212. EXPORT_SYMBOL(bt_accept_dequeue);
  213. int bt_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
  214. struct msghdr *msg, size_t len, int flags)
  215. {
  216. int noblock = flags & MSG_DONTWAIT;
  217. struct sock *sk = sock->sk;
  218. struct sk_buff *skb;
  219. size_t copied;
  220. int err;
  221. BT_DBG("sock %p sk %p len %zu", sock, sk, len);
  222. if (flags & (MSG_OOB))
  223. return -EOPNOTSUPP;
  224. skb = skb_recv_datagram(sk, flags, noblock, &err);
  225. if (!skb) {
  226. if (sk->sk_shutdown & RCV_SHUTDOWN)
  227. return 0;
  228. return err;
  229. }
  230. msg->msg_namelen = 0;
  231. copied = skb->len;
  232. if (len < copied) {
  233. msg->msg_flags |= MSG_TRUNC;
  234. copied = len;
  235. }
  236. skb_reset_transport_header(skb);
  237. err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
  238. if (err == 0)
  239. sock_recv_ts_and_drops(msg, sk, skb);
  240. skb_free_datagram(sk, skb);
  241. return err ? : copied;
  242. }
  243. EXPORT_SYMBOL(bt_sock_recvmsg);
  244. static long bt_sock_data_wait(struct sock *sk, long timeo)
  245. {
  246. DECLARE_WAITQUEUE(wait, current);
  247. add_wait_queue(sk_sleep(sk), &wait);
  248. for (;;) {
  249. set_current_state(TASK_INTERRUPTIBLE);
  250. if (!skb_queue_empty(&sk->sk_receive_queue))
  251. break;
  252. if (sk->sk_err || (sk->sk_shutdown & RCV_SHUTDOWN))
  253. break;
  254. if (signal_pending(current) || !timeo)
  255. break;
  256. set_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
  257. release_sock(sk);
  258. timeo = schedule_timeout(timeo);
  259. lock_sock(sk);
  260. clear_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
  261. }
  262. __set_current_state(TASK_RUNNING);
  263. remove_wait_queue(sk_sleep(sk), &wait);
  264. return timeo;
  265. }
  266. int bt_sock_stream_recvmsg(struct kiocb *iocb, struct socket *sock,
  267. struct msghdr *msg, size_t size, int flags)
  268. {
  269. struct sock *sk = sock->sk;
  270. int err = 0;
  271. size_t target, copied = 0;
  272. long timeo;
  273. if (flags & MSG_OOB)
  274. return -EOPNOTSUPP;
  275. msg->msg_namelen = 0;
  276. BT_DBG("sk %p size %zu", sk, size);
  277. lock_sock(sk);
  278. target = sock_rcvlowat(sk, flags & MSG_WAITALL, size);
  279. timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
  280. do {
  281. struct sk_buff *skb;
  282. int chunk;
  283. skb = skb_dequeue(&sk->sk_receive_queue);
  284. if (!skb) {
  285. if (copied >= target)
  286. break;
  287. err = sock_error(sk);
  288. if (err)
  289. break;
  290. if (sk->sk_shutdown & RCV_SHUTDOWN)
  291. break;
  292. err = -EAGAIN;
  293. if (!timeo)
  294. break;
  295. timeo = bt_sock_data_wait(sk, timeo);
  296. if (signal_pending(current)) {
  297. err = sock_intr_errno(timeo);
  298. goto out;
  299. }
  300. continue;
  301. }
  302. chunk = min_t(unsigned int, skb->len, size);
  303. if (memcpy_toiovec(msg->msg_iov, skb->data, chunk)) {
  304. skb_queue_head(&sk->sk_receive_queue, skb);
  305. if (!copied)
  306. copied = -EFAULT;
  307. break;
  308. }
  309. copied += chunk;
  310. size -= chunk;
  311. sock_recv_ts_and_drops(msg, sk, skb);
  312. if (!(flags & MSG_PEEK)) {
  313. skb_pull(skb, chunk);
  314. if (skb->len) {
  315. skb_queue_head(&sk->sk_receive_queue, skb);
  316. break;
  317. }
  318. kfree_skb(skb);
  319. } else {
  320. /* put message back and return */
  321. skb_queue_head(&sk->sk_receive_queue, skb);
  322. break;
  323. }
  324. } while (size);
  325. out:
  326. release_sock(sk);
  327. return copied ? : err;
  328. }
  329. EXPORT_SYMBOL(bt_sock_stream_recvmsg);
  330. static inline unsigned int bt_accept_poll(struct sock *parent)
  331. {
  332. struct list_head *p, *n;
  333. struct sock *sk;
  334. list_for_each_safe(p, n, &bt_sk(parent)->accept_q) {
  335. sk = (struct sock *) list_entry(p, struct bt_sock, accept_q);
  336. if (sk->sk_state == BT_CONNECTED ||
  337. (bt_sk(parent)->defer_setup &&
  338. sk->sk_state == BT_CONNECT2))
  339. return POLLIN | POLLRDNORM;
  340. }
  341. return 0;
  342. }
  343. unsigned int bt_sock_poll(struct file *file, struct socket *sock, poll_table *wait)
  344. {
  345. struct sock *sk = sock->sk;
  346. unsigned int mask = 0;
  347. BT_DBG("sock %p, sk %p", sock, sk);
  348. poll_wait(file, sk_sleep(sk), wait);
  349. if (sk->sk_state == BT_LISTEN)
  350. return bt_accept_poll(sk);
  351. if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
  352. mask |= POLLERR;
  353. if (sk->sk_shutdown & RCV_SHUTDOWN)
  354. mask |= POLLRDHUP | POLLIN | POLLRDNORM;
  355. if (sk->sk_shutdown == SHUTDOWN_MASK)
  356. mask |= POLLHUP;
  357. if (!skb_queue_empty(&sk->sk_receive_queue))
  358. mask |= POLLIN | POLLRDNORM;
  359. if (sk->sk_state == BT_CLOSED)
  360. mask |= POLLHUP;
  361. if (sk->sk_state == BT_CONNECT ||
  362. sk->sk_state == BT_CONNECT2 ||
  363. sk->sk_state == BT_CONFIG)
  364. return mask;
  365. if (sock_writeable(sk))
  366. mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
  367. else
  368. set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
  369. return mask;
  370. }
  371. EXPORT_SYMBOL(bt_sock_poll);
  372. int bt_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  373. {
  374. struct sock *sk = sock->sk;
  375. struct sk_buff *skb;
  376. long amount;
  377. int err;
  378. BT_DBG("sk %p cmd %x arg %lx", sk, cmd, arg);
  379. switch (cmd) {
  380. case TIOCOUTQ:
  381. if (sk->sk_state == BT_LISTEN)
  382. return -EINVAL;
  383. amount = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
  384. if (amount < 0)
  385. amount = 0;
  386. err = put_user(amount, (int __user *) arg);
  387. break;
  388. case TIOCINQ:
  389. if (sk->sk_state == BT_LISTEN)
  390. return -EINVAL;
  391. lock_sock(sk);
  392. skb = skb_peek(&sk->sk_receive_queue);
  393. amount = skb ? skb->len : 0;
  394. release_sock(sk);
  395. err = put_user(amount, (int __user *) arg);
  396. break;
  397. case SIOCGSTAMP:
  398. err = sock_get_timestamp(sk, (struct timeval __user *) arg);
  399. break;
  400. case SIOCGSTAMPNS:
  401. err = sock_get_timestampns(sk, (struct timespec __user *) arg);
  402. break;
  403. default:
  404. err = -ENOIOCTLCMD;
  405. break;
  406. }
  407. return err;
  408. }
  409. EXPORT_SYMBOL(bt_sock_ioctl);
  410. int bt_sock_wait_state(struct sock *sk, int state, unsigned long timeo)
  411. {
  412. DECLARE_WAITQUEUE(wait, current);
  413. int err = 0;
  414. BT_DBG("sk %p", sk);
  415. add_wait_queue(sk_sleep(sk), &wait);
  416. set_current_state(TASK_INTERRUPTIBLE);
  417. while (sk->sk_state != state) {
  418. if (!timeo) {
  419. err = -EINPROGRESS;
  420. break;
  421. }
  422. if (signal_pending(current)) {
  423. err = sock_intr_errno(timeo);
  424. break;
  425. }
  426. release_sock(sk);
  427. timeo = schedule_timeout(timeo);
  428. lock_sock(sk);
  429. set_current_state(TASK_INTERRUPTIBLE);
  430. err = sock_error(sk);
  431. if (err)
  432. break;
  433. }
  434. __set_current_state(TASK_RUNNING);
  435. remove_wait_queue(sk_sleep(sk), &wait);
  436. return err;
  437. }
  438. EXPORT_SYMBOL(bt_sock_wait_state);
  439. static struct net_proto_family bt_sock_family_ops = {
  440. .owner = THIS_MODULE,
  441. .family = PF_BLUETOOTH,
  442. .create = bt_sock_create,
  443. };
  444. static int __init bt_init(void)
  445. {
  446. int err;
  447. BT_INFO("Core ver %s", VERSION);
  448. err = bt_sysfs_init();
  449. if (err < 0)
  450. return err;
  451. err = sock_register(&bt_sock_family_ops);
  452. if (err < 0) {
  453. bt_sysfs_cleanup();
  454. return err;
  455. }
  456. BT_INFO("HCI device and connection manager initialized");
  457. err = hci_sock_init();
  458. if (err < 0)
  459. goto error;
  460. err = l2cap_init();
  461. if (err < 0)
  462. goto sock_err;
  463. err = sco_init();
  464. if (err < 0) {
  465. l2cap_exit();
  466. goto sock_err;
  467. }
  468. return 0;
  469. sock_err:
  470. hci_sock_cleanup();
  471. error:
  472. sock_unregister(PF_BLUETOOTH);
  473. bt_sysfs_cleanup();
  474. return err;
  475. }
  476. static void __exit bt_exit(void)
  477. {
  478. sco_exit();
  479. l2cap_exit();
  480. hci_sock_cleanup();
  481. sock_unregister(PF_BLUETOOTH);
  482. bt_sysfs_cleanup();
  483. }
  484. subsys_initcall(bt_init);
  485. module_exit(bt_exit);
  486. MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
  487. MODULE_DESCRIPTION("Bluetooth Core ver " VERSION);
  488. MODULE_VERSION(VERSION);
  489. MODULE_LICENSE("GPL");
  490. MODULE_ALIAS_NETPROTO(PF_BLUETOOTH);