af_bluetooth.c 18 KB

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