af_bluetooth.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  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/debugfs.h>
  23. #include <linux/stringify.h>
  24. #include <linux/sched/signal.h>
  25. #include <asm/ioctls.h>
  26. #include <net/bluetooth/bluetooth.h>
  27. #include <linux/proc_fs.h>
  28. #include "leds.h"
  29. #include "selftest.h"
  30. /* Bluetooth sockets */
  31. #define BT_MAX_PROTO 8
  32. static const struct net_proto_family *bt_proto[BT_MAX_PROTO];
  33. static DEFINE_RWLOCK(bt_proto_lock);
  34. static struct lock_class_key bt_lock_key[BT_MAX_PROTO];
  35. static const char *const bt_key_strings[BT_MAX_PROTO] = {
  36. "sk_lock-AF_BLUETOOTH-BTPROTO_L2CAP",
  37. "sk_lock-AF_BLUETOOTH-BTPROTO_HCI",
  38. "sk_lock-AF_BLUETOOTH-BTPROTO_SCO",
  39. "sk_lock-AF_BLUETOOTH-BTPROTO_RFCOMM",
  40. "sk_lock-AF_BLUETOOTH-BTPROTO_BNEP",
  41. "sk_lock-AF_BLUETOOTH-BTPROTO_CMTP",
  42. "sk_lock-AF_BLUETOOTH-BTPROTO_HIDP",
  43. "sk_lock-AF_BLUETOOTH-BTPROTO_AVDTP",
  44. };
  45. static struct lock_class_key bt_slock_key[BT_MAX_PROTO];
  46. static const char *const bt_slock_key_strings[BT_MAX_PROTO] = {
  47. "slock-AF_BLUETOOTH-BTPROTO_L2CAP",
  48. "slock-AF_BLUETOOTH-BTPROTO_HCI",
  49. "slock-AF_BLUETOOTH-BTPROTO_SCO",
  50. "slock-AF_BLUETOOTH-BTPROTO_RFCOMM",
  51. "slock-AF_BLUETOOTH-BTPROTO_BNEP",
  52. "slock-AF_BLUETOOTH-BTPROTO_CMTP",
  53. "slock-AF_BLUETOOTH-BTPROTO_HIDP",
  54. "slock-AF_BLUETOOTH-BTPROTO_AVDTP",
  55. };
  56. void bt_sock_reclassify_lock(struct sock *sk, int proto)
  57. {
  58. BUG_ON(!sk);
  59. BUG_ON(!sock_allow_reclassification(sk));
  60. sock_lock_init_class_and_name(sk,
  61. bt_slock_key_strings[proto], &bt_slock_key[proto],
  62. bt_key_strings[proto], &bt_lock_key[proto]);
  63. }
  64. EXPORT_SYMBOL(bt_sock_reclassify_lock);
  65. int bt_sock_register(int proto, const struct net_proto_family *ops)
  66. {
  67. int err = 0;
  68. if (proto < 0 || proto >= BT_MAX_PROTO)
  69. return -EINVAL;
  70. write_lock(&bt_proto_lock);
  71. if (bt_proto[proto])
  72. err = -EEXIST;
  73. else
  74. bt_proto[proto] = ops;
  75. write_unlock(&bt_proto_lock);
  76. return err;
  77. }
  78. EXPORT_SYMBOL(bt_sock_register);
  79. void bt_sock_unregister(int proto)
  80. {
  81. if (proto < 0 || proto >= BT_MAX_PROTO)
  82. return;
  83. write_lock(&bt_proto_lock);
  84. bt_proto[proto] = NULL;
  85. write_unlock(&bt_proto_lock);
  86. }
  87. EXPORT_SYMBOL(bt_sock_unregister);
  88. static int bt_sock_create(struct net *net, struct socket *sock, int proto,
  89. int kern)
  90. {
  91. int err;
  92. if (net != &init_net)
  93. return -EAFNOSUPPORT;
  94. if (proto < 0 || proto >= BT_MAX_PROTO)
  95. return -EINVAL;
  96. if (!bt_proto[proto])
  97. request_module("bt-proto-%d", proto);
  98. err = -EPROTONOSUPPORT;
  99. read_lock(&bt_proto_lock);
  100. if (bt_proto[proto] && try_module_get(bt_proto[proto]->owner)) {
  101. err = bt_proto[proto]->create(net, sock, proto, kern);
  102. if (!err)
  103. bt_sock_reclassify_lock(sock->sk, proto);
  104. module_put(bt_proto[proto]->owner);
  105. }
  106. read_unlock(&bt_proto_lock);
  107. return err;
  108. }
  109. void bt_sock_link(struct bt_sock_list *l, struct sock *sk)
  110. {
  111. write_lock(&l->lock);
  112. sk_add_node(sk, &l->head);
  113. write_unlock(&l->lock);
  114. }
  115. EXPORT_SYMBOL(bt_sock_link);
  116. void bt_sock_unlink(struct bt_sock_list *l, struct sock *sk)
  117. {
  118. write_lock(&l->lock);
  119. sk_del_node_init(sk);
  120. write_unlock(&l->lock);
  121. }
  122. EXPORT_SYMBOL(bt_sock_unlink);
  123. void bt_accept_enqueue(struct sock *parent, struct sock *sk, bool bh)
  124. {
  125. BT_DBG("parent %p, sk %p", parent, sk);
  126. sock_hold(sk);
  127. if (bh)
  128. bh_lock_sock_nested(sk);
  129. else
  130. lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
  131. list_add_tail(&bt_sk(sk)->accept_q, &bt_sk(parent)->accept_q);
  132. bt_sk(sk)->parent = parent;
  133. if (bh)
  134. bh_unlock_sock(sk);
  135. else
  136. release_sock(sk);
  137. sk_acceptq_added(parent);
  138. }
  139. EXPORT_SYMBOL(bt_accept_enqueue);
  140. /* Calling function must hold the sk lock.
  141. * bt_sk(sk)->parent must be non-NULL meaning sk is in the parent list.
  142. */
  143. void bt_accept_unlink(struct sock *sk)
  144. {
  145. BT_DBG("sk %p state %d", sk, sk->sk_state);
  146. list_del_init(&bt_sk(sk)->accept_q);
  147. sk_acceptq_removed(bt_sk(sk)->parent);
  148. bt_sk(sk)->parent = NULL;
  149. sock_put(sk);
  150. }
  151. EXPORT_SYMBOL(bt_accept_unlink);
  152. struct sock *bt_accept_dequeue(struct sock *parent, struct socket *newsock)
  153. {
  154. struct bt_sock *s, *n;
  155. struct sock *sk;
  156. BT_DBG("parent %p", parent);
  157. restart:
  158. list_for_each_entry_safe(s, n, &bt_sk(parent)->accept_q, accept_q) {
  159. sk = (struct sock *)s;
  160. /* Prevent early freeing of sk due to unlink and sock_kill */
  161. sock_hold(sk);
  162. lock_sock(sk);
  163. /* Check sk has not already been unlinked via
  164. * bt_accept_unlink() due to serialisation caused by sk locking
  165. */
  166. if (!bt_sk(sk)->parent) {
  167. BT_DBG("sk %p, already unlinked", sk);
  168. release_sock(sk);
  169. sock_put(sk);
  170. /* Restart the loop as sk is no longer in the list
  171. * and also avoid a potential infinite loop because
  172. * list_for_each_entry_safe() is not thread safe.
  173. */
  174. goto restart;
  175. }
  176. /* sk is safely in the parent list so reduce reference count */
  177. sock_put(sk);
  178. /* FIXME: Is this check still needed */
  179. if (sk->sk_state == BT_CLOSED) {
  180. bt_accept_unlink(sk);
  181. release_sock(sk);
  182. continue;
  183. }
  184. if (sk->sk_state == BT_CONNECTED || !newsock ||
  185. test_bit(BT_SK_DEFER_SETUP, &bt_sk(parent)->flags)) {
  186. bt_accept_unlink(sk);
  187. if (newsock)
  188. sock_graft(sk, newsock);
  189. release_sock(sk);
  190. return sk;
  191. }
  192. release_sock(sk);
  193. }
  194. return NULL;
  195. }
  196. EXPORT_SYMBOL(bt_accept_dequeue);
  197. int bt_sock_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
  198. int flags)
  199. {
  200. int noblock = flags & MSG_DONTWAIT;
  201. struct sock *sk = sock->sk;
  202. struct sk_buff *skb;
  203. size_t copied;
  204. size_t skblen;
  205. int err;
  206. BT_DBG("sock %p sk %p len %zu", sock, sk, len);
  207. if (flags & MSG_OOB)
  208. return -EOPNOTSUPP;
  209. skb = skb_recv_datagram(sk, flags, noblock, &err);
  210. if (!skb) {
  211. if (sk->sk_shutdown & RCV_SHUTDOWN)
  212. return 0;
  213. return err;
  214. }
  215. skblen = skb->len;
  216. copied = skb->len;
  217. if (len < copied) {
  218. msg->msg_flags |= MSG_TRUNC;
  219. copied = len;
  220. }
  221. skb_reset_transport_header(skb);
  222. err = skb_copy_datagram_msg(skb, 0, msg, copied);
  223. if (err == 0) {
  224. sock_recv_ts_and_drops(msg, sk, skb);
  225. if (msg->msg_name && bt_sk(sk)->skb_msg_name)
  226. bt_sk(sk)->skb_msg_name(skb, msg->msg_name,
  227. &msg->msg_namelen);
  228. if (bt_sk(sk)->skb_put_cmsg)
  229. bt_sk(sk)->skb_put_cmsg(skb, msg, sk);
  230. }
  231. skb_free_datagram(sk, skb);
  232. if (flags & MSG_TRUNC)
  233. copied = skblen;
  234. return err ? : copied;
  235. }
  236. EXPORT_SYMBOL(bt_sock_recvmsg);
  237. static long bt_sock_data_wait(struct sock *sk, long timeo)
  238. {
  239. DECLARE_WAITQUEUE(wait, current);
  240. add_wait_queue(sk_sleep(sk), &wait);
  241. for (;;) {
  242. set_current_state(TASK_INTERRUPTIBLE);
  243. if (!skb_queue_empty(&sk->sk_receive_queue))
  244. break;
  245. if (sk->sk_err || (sk->sk_shutdown & RCV_SHUTDOWN))
  246. break;
  247. if (signal_pending(current) || !timeo)
  248. break;
  249. sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
  250. release_sock(sk);
  251. timeo = schedule_timeout(timeo);
  252. lock_sock(sk);
  253. sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
  254. }
  255. __set_current_state(TASK_RUNNING);
  256. remove_wait_queue(sk_sleep(sk), &wait);
  257. return timeo;
  258. }
  259. int bt_sock_stream_recvmsg(struct socket *sock, struct msghdr *msg,
  260. size_t size, int flags)
  261. {
  262. struct sock *sk = sock->sk;
  263. int err = 0;
  264. size_t target, copied = 0;
  265. long timeo;
  266. if (flags & MSG_OOB)
  267. return -EOPNOTSUPP;
  268. BT_DBG("sk %p size %zu", sk, size);
  269. lock_sock(sk);
  270. target = sock_rcvlowat(sk, flags & MSG_WAITALL, size);
  271. timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
  272. do {
  273. struct sk_buff *skb;
  274. int chunk;
  275. skb = skb_dequeue(&sk->sk_receive_queue);
  276. if (!skb) {
  277. if (copied >= target)
  278. break;
  279. err = sock_error(sk);
  280. if (err)
  281. break;
  282. if (sk->sk_shutdown & RCV_SHUTDOWN)
  283. break;
  284. err = -EAGAIN;
  285. if (!timeo)
  286. break;
  287. timeo = bt_sock_data_wait(sk, timeo);
  288. if (signal_pending(current)) {
  289. err = sock_intr_errno(timeo);
  290. goto out;
  291. }
  292. continue;
  293. }
  294. chunk = min_t(unsigned int, skb->len, size);
  295. if (skb_copy_datagram_msg(skb, 0, msg, chunk)) {
  296. skb_queue_head(&sk->sk_receive_queue, skb);
  297. if (!copied)
  298. copied = -EFAULT;
  299. break;
  300. }
  301. copied += chunk;
  302. size -= chunk;
  303. sock_recv_ts_and_drops(msg, sk, skb);
  304. if (!(flags & MSG_PEEK)) {
  305. int skb_len = skb_headlen(skb);
  306. if (chunk <= skb_len) {
  307. __skb_pull(skb, chunk);
  308. } else {
  309. struct sk_buff *frag;
  310. __skb_pull(skb, skb_len);
  311. chunk -= skb_len;
  312. skb_walk_frags(skb, frag) {
  313. if (chunk <= frag->len) {
  314. /* Pulling partial data */
  315. skb->len -= chunk;
  316. skb->data_len -= chunk;
  317. __skb_pull(frag, chunk);
  318. break;
  319. } else if (frag->len) {
  320. /* Pulling all frag data */
  321. chunk -= frag->len;
  322. skb->len -= frag->len;
  323. skb->data_len -= frag->len;
  324. __skb_pull(frag, frag->len);
  325. }
  326. }
  327. }
  328. if (skb->len) {
  329. skb_queue_head(&sk->sk_receive_queue, skb);
  330. break;
  331. }
  332. kfree_skb(skb);
  333. } else {
  334. /* put message back and return */
  335. skb_queue_head(&sk->sk_receive_queue, skb);
  336. break;
  337. }
  338. } while (size);
  339. out:
  340. release_sock(sk);
  341. return copied ? : err;
  342. }
  343. EXPORT_SYMBOL(bt_sock_stream_recvmsg);
  344. static inline __poll_t bt_accept_poll(struct sock *parent)
  345. {
  346. struct bt_sock *s, *n;
  347. struct sock *sk;
  348. list_for_each_entry_safe(s, n, &bt_sk(parent)->accept_q, accept_q) {
  349. sk = (struct sock *)s;
  350. if (sk->sk_state == BT_CONNECTED ||
  351. (test_bit(BT_SK_DEFER_SETUP, &bt_sk(parent)->flags) &&
  352. sk->sk_state == BT_CONNECT2))
  353. return EPOLLIN | EPOLLRDNORM;
  354. }
  355. return 0;
  356. }
  357. __poll_t bt_sock_poll(struct file *file, struct socket *sock,
  358. poll_table *wait)
  359. {
  360. struct sock *sk = sock->sk;
  361. __poll_t mask = 0;
  362. poll_wait(file, sk_sleep(sk), wait);
  363. if (sk->sk_state == BT_LISTEN)
  364. return bt_accept_poll(sk);
  365. if (sk->sk_err || !skb_queue_empty_lockless(&sk->sk_error_queue))
  366. mask |= EPOLLERR |
  367. (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? EPOLLPRI : 0);
  368. if (sk->sk_shutdown & RCV_SHUTDOWN)
  369. mask |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM;
  370. if (sk->sk_shutdown == SHUTDOWN_MASK)
  371. mask |= EPOLLHUP;
  372. if (!skb_queue_empty_lockless(&sk->sk_receive_queue))
  373. mask |= EPOLLIN | EPOLLRDNORM;
  374. if (sk->sk_state == BT_CLOSED)
  375. mask |= EPOLLHUP;
  376. if (sk->sk_state == BT_CONNECT ||
  377. sk->sk_state == BT_CONNECT2 ||
  378. sk->sk_state == BT_CONFIG)
  379. return mask;
  380. if (!test_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags) && sock_writeable(sk))
  381. mask |= EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND;
  382. else
  383. sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
  384. return mask;
  385. }
  386. EXPORT_SYMBOL(bt_sock_poll);
  387. int bt_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  388. {
  389. struct sock *sk = sock->sk;
  390. struct sk_buff *skb;
  391. long amount;
  392. int err;
  393. BT_DBG("sk %p cmd %x arg %lx", sk, cmd, arg);
  394. switch (cmd) {
  395. case TIOCOUTQ:
  396. if (sk->sk_state == BT_LISTEN)
  397. return -EINVAL;
  398. amount = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
  399. if (amount < 0)
  400. amount = 0;
  401. err = put_user(amount, (int __user *) arg);
  402. break;
  403. case TIOCINQ:
  404. if (sk->sk_state == BT_LISTEN)
  405. return -EINVAL;
  406. lock_sock(sk);
  407. skb = skb_peek(&sk->sk_receive_queue);
  408. amount = skb ? skb->len : 0;
  409. release_sock(sk);
  410. err = put_user(amount, (int __user *) arg);
  411. break;
  412. default:
  413. err = -ENOIOCTLCMD;
  414. break;
  415. }
  416. return err;
  417. }
  418. EXPORT_SYMBOL(bt_sock_ioctl);
  419. /* This function expects the sk lock to be held when called */
  420. int bt_sock_wait_state(struct sock *sk, int state, unsigned long timeo)
  421. {
  422. DECLARE_WAITQUEUE(wait, current);
  423. int err = 0;
  424. BT_DBG("sk %p", sk);
  425. add_wait_queue(sk_sleep(sk), &wait);
  426. set_current_state(TASK_INTERRUPTIBLE);
  427. while (sk->sk_state != state) {
  428. if (!timeo) {
  429. err = -EINPROGRESS;
  430. break;
  431. }
  432. if (signal_pending(current)) {
  433. err = sock_intr_errno(timeo);
  434. break;
  435. }
  436. release_sock(sk);
  437. timeo = schedule_timeout(timeo);
  438. lock_sock(sk);
  439. set_current_state(TASK_INTERRUPTIBLE);
  440. err = sock_error(sk);
  441. if (err)
  442. break;
  443. }
  444. __set_current_state(TASK_RUNNING);
  445. remove_wait_queue(sk_sleep(sk), &wait);
  446. return err;
  447. }
  448. EXPORT_SYMBOL(bt_sock_wait_state);
  449. /* This function expects the sk lock to be held when called */
  450. int bt_sock_wait_ready(struct sock *sk, unsigned long flags)
  451. {
  452. DECLARE_WAITQUEUE(wait, current);
  453. unsigned long timeo;
  454. int err = 0;
  455. BT_DBG("sk %p", sk);
  456. timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
  457. add_wait_queue(sk_sleep(sk), &wait);
  458. set_current_state(TASK_INTERRUPTIBLE);
  459. while (test_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags)) {
  460. if (!timeo) {
  461. err = -EAGAIN;
  462. break;
  463. }
  464. if (signal_pending(current)) {
  465. err = sock_intr_errno(timeo);
  466. break;
  467. }
  468. release_sock(sk);
  469. timeo = schedule_timeout(timeo);
  470. lock_sock(sk);
  471. set_current_state(TASK_INTERRUPTIBLE);
  472. err = sock_error(sk);
  473. if (err)
  474. break;
  475. }
  476. __set_current_state(TASK_RUNNING);
  477. remove_wait_queue(sk_sleep(sk), &wait);
  478. return err;
  479. }
  480. EXPORT_SYMBOL(bt_sock_wait_ready);
  481. #ifdef CONFIG_PROC_FS
  482. static void *bt_seq_start(struct seq_file *seq, loff_t *pos)
  483. __acquires(seq->private->l->lock)
  484. {
  485. struct bt_sock_list *l = PDE_DATA(file_inode(seq->file));
  486. read_lock(&l->lock);
  487. return seq_hlist_start_head(&l->head, *pos);
  488. }
  489. static void *bt_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  490. {
  491. struct bt_sock_list *l = PDE_DATA(file_inode(seq->file));
  492. return seq_hlist_next(v, &l->head, pos);
  493. }
  494. static void bt_seq_stop(struct seq_file *seq, void *v)
  495. __releases(seq->private->l->lock)
  496. {
  497. struct bt_sock_list *l = PDE_DATA(file_inode(seq->file));
  498. read_unlock(&l->lock);
  499. }
  500. static int bt_seq_show(struct seq_file *seq, void *v)
  501. {
  502. struct bt_sock_list *l = PDE_DATA(file_inode(seq->file));
  503. if (v == SEQ_START_TOKEN) {
  504. seq_puts(seq ,"sk RefCnt Rmem Wmem User Inode Parent");
  505. if (l->custom_seq_show) {
  506. seq_putc(seq, ' ');
  507. l->custom_seq_show(seq, v);
  508. }
  509. seq_putc(seq, '\n');
  510. } else {
  511. struct sock *sk = sk_entry(v);
  512. struct bt_sock *bt = bt_sk(sk);
  513. seq_printf(seq,
  514. "%pK %-6d %-6u %-6u %-6u %-6lu %-6lu",
  515. sk,
  516. refcount_read(&sk->sk_refcnt),
  517. sk_rmem_alloc_get(sk),
  518. sk_wmem_alloc_get(sk),
  519. from_kuid(seq_user_ns(seq), sock_i_uid(sk)),
  520. sock_i_ino(sk),
  521. bt->parent? sock_i_ino(bt->parent): 0LU);
  522. if (l->custom_seq_show) {
  523. seq_putc(seq, ' ');
  524. l->custom_seq_show(seq, v);
  525. }
  526. seq_putc(seq, '\n');
  527. }
  528. return 0;
  529. }
  530. static const struct seq_operations bt_seq_ops = {
  531. .start = bt_seq_start,
  532. .next = bt_seq_next,
  533. .stop = bt_seq_stop,
  534. .show = bt_seq_show,
  535. };
  536. int bt_procfs_init(struct net *net, const char *name,
  537. struct bt_sock_list *sk_list,
  538. int (* seq_show)(struct seq_file *, void *))
  539. {
  540. sk_list->custom_seq_show = seq_show;
  541. if (!proc_create_seq_data(name, 0, net->proc_net, &bt_seq_ops, sk_list))
  542. return -ENOMEM;
  543. return 0;
  544. }
  545. void bt_procfs_cleanup(struct net *net, const char *name)
  546. {
  547. remove_proc_entry(name, net->proc_net);
  548. }
  549. #else
  550. int bt_procfs_init(struct net *net, const char *name,
  551. struct bt_sock_list *sk_list,
  552. int (* seq_show)(struct seq_file *, void *))
  553. {
  554. return 0;
  555. }
  556. void bt_procfs_cleanup(struct net *net, const char *name)
  557. {
  558. }
  559. #endif
  560. EXPORT_SYMBOL(bt_procfs_init);
  561. EXPORT_SYMBOL(bt_procfs_cleanup);
  562. static const struct net_proto_family bt_sock_family_ops = {
  563. .owner = THIS_MODULE,
  564. .family = PF_BLUETOOTH,
  565. .create = bt_sock_create,
  566. };
  567. struct dentry *bt_debugfs;
  568. EXPORT_SYMBOL_GPL(bt_debugfs);
  569. #define VERSION __stringify(BT_SUBSYS_VERSION) "." \
  570. __stringify(BT_SUBSYS_REVISION)
  571. static int __init bt_init(void)
  572. {
  573. int err;
  574. sock_skb_cb_check_size(sizeof(struct bt_skb_cb));
  575. BT_INFO("Core ver %s", VERSION);
  576. err = bt_selftest();
  577. if (err < 0)
  578. return err;
  579. bt_debugfs = debugfs_create_dir("bluetooth", NULL);
  580. bt_leds_init();
  581. err = bt_sysfs_init();
  582. if (err < 0)
  583. return err;
  584. err = sock_register(&bt_sock_family_ops);
  585. if (err)
  586. goto cleanup_sysfs;
  587. BT_INFO("HCI device and connection manager initialized");
  588. err = hci_sock_init();
  589. if (err)
  590. goto unregister_socket;
  591. err = l2cap_init();
  592. if (err)
  593. goto cleanup_socket;
  594. err = sco_init();
  595. if (err)
  596. goto cleanup_cap;
  597. err = mgmt_init();
  598. if (err)
  599. goto cleanup_sco;
  600. return 0;
  601. cleanup_sco:
  602. sco_exit();
  603. cleanup_cap:
  604. l2cap_exit();
  605. cleanup_socket:
  606. hci_sock_cleanup();
  607. unregister_socket:
  608. sock_unregister(PF_BLUETOOTH);
  609. cleanup_sysfs:
  610. bt_sysfs_cleanup();
  611. return err;
  612. }
  613. static void __exit bt_exit(void)
  614. {
  615. mgmt_exit();
  616. sco_exit();
  617. l2cap_exit();
  618. hci_sock_cleanup();
  619. sock_unregister(PF_BLUETOOTH);
  620. bt_sysfs_cleanup();
  621. bt_leds_cleanup();
  622. debugfs_remove_recursive(bt_debugfs);
  623. }
  624. subsys_initcall(bt_init);
  625. module_exit(bt_exit);
  626. MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
  627. MODULE_DESCRIPTION("Bluetooth Core ver " VERSION);
  628. MODULE_VERSION(VERSION);
  629. MODULE_LICENSE("GPL");
  630. MODULE_ALIAS_NETPROTO(PF_BLUETOOTH);