1 /* 2 BlueZ - Bluetooth protocol stack for Linux 3 Copyright (C) 2000-2001 Qualcomm Incorporated 4 5 Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com> 6 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 11 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 12 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 13 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. 14 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY 15 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 16 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 17 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 18 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 19 20 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 21 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 22 SOFTWARE IS DISCLAIMED. 23 */ 24 25 /* Bluetooth address family and sockets. */ 26 27 #include <linux/module.h> 28 #include <linux/debugfs.h> 29 #include <linux/stringify.h> 30 #include <linux/sched/signal.h> 31 32 #include <asm/ioctls.h> 33 34 #include <net/bluetooth/bluetooth.h> 35 #include <linux/proc_fs.h> 36 37 #include <linux/ethtool.h> 38 #include <linux/sockios.h> 39 40 #include "leds.h" 41 #include "selftest.h" 42 43 /* Bluetooth sockets */ 44 #define BT_MAX_PROTO (BTPROTO_LAST + 1) 45 static const struct net_proto_family *bt_proto[BT_MAX_PROTO]; 46 static DEFINE_RWLOCK(bt_proto_lock); 47 48 static struct lock_class_key bt_lock_key[BT_MAX_PROTO]; 49 static const char *const bt_key_strings[BT_MAX_PROTO] = { 50 "sk_lock-AF_BLUETOOTH-BTPROTO_L2CAP", 51 "sk_lock-AF_BLUETOOTH-BTPROTO_HCI", 52 "sk_lock-AF_BLUETOOTH-BTPROTO_SCO", 53 "sk_lock-AF_BLUETOOTH-BTPROTO_RFCOMM", 54 "sk_lock-AF_BLUETOOTH-BTPROTO_BNEP", 55 "sk_lock-AF_BLUETOOTH-BTPROTO_CMTP", 56 "sk_lock-AF_BLUETOOTH-BTPROTO_HIDP", 57 "sk_lock-AF_BLUETOOTH-BTPROTO_AVDTP", 58 "sk_lock-AF_BLUETOOTH-BTPROTO_ISO", 59 }; 60 61 static struct lock_class_key bt_slock_key[BT_MAX_PROTO]; 62 static const char *const bt_slock_key_strings[BT_MAX_PROTO] = { 63 "slock-AF_BLUETOOTH-BTPROTO_L2CAP", 64 "slock-AF_BLUETOOTH-BTPROTO_HCI", 65 "slock-AF_BLUETOOTH-BTPROTO_SCO", 66 "slock-AF_BLUETOOTH-BTPROTO_RFCOMM", 67 "slock-AF_BLUETOOTH-BTPROTO_BNEP", 68 "slock-AF_BLUETOOTH-BTPROTO_CMTP", 69 "slock-AF_BLUETOOTH-BTPROTO_HIDP", 70 "slock-AF_BLUETOOTH-BTPROTO_AVDTP", 71 "slock-AF_BLUETOOTH-BTPROTO_ISO", 72 }; 73 74 void bt_sock_reclassify_lock(struct sock *sk, int proto) 75 { 76 BUG_ON(!sk); 77 BUG_ON(!sock_allow_reclassification(sk)); 78 79 sock_lock_init_class_and_name(sk, 80 bt_slock_key_strings[proto], &bt_slock_key[proto], 81 bt_key_strings[proto], &bt_lock_key[proto]); 82 } 83 EXPORT_SYMBOL(bt_sock_reclassify_lock); 84 85 int bt_sock_register(int proto, const struct net_proto_family *ops) 86 { 87 int err = 0; 88 89 if (proto < 0 || proto >= BT_MAX_PROTO) 90 return -EINVAL; 91 92 write_lock(&bt_proto_lock); 93 94 if (bt_proto[proto]) 95 err = -EEXIST; 96 else 97 bt_proto[proto] = ops; 98 99 write_unlock(&bt_proto_lock); 100 101 return err; 102 } 103 EXPORT_SYMBOL(bt_sock_register); 104 105 void bt_sock_unregister(int proto) 106 { 107 if (proto < 0 || proto >= BT_MAX_PROTO) 108 return; 109 110 write_lock(&bt_proto_lock); 111 bt_proto[proto] = NULL; 112 write_unlock(&bt_proto_lock); 113 } 114 EXPORT_SYMBOL(bt_sock_unregister); 115 116 static int bt_sock_create(struct net *net, struct socket *sock, int proto, 117 int kern) 118 { 119 int err; 120 121 if (net != &init_net) 122 return -EAFNOSUPPORT; 123 124 if (proto < 0 || proto >= BT_MAX_PROTO) 125 return -EINVAL; 126 127 if (!bt_proto[proto]) 128 request_module("bt-proto-%d", proto); 129 130 err = -EPROTONOSUPPORT; 131 132 read_lock(&bt_proto_lock); 133 134 if (bt_proto[proto] && try_module_get(bt_proto[proto]->owner)) { 135 err = bt_proto[proto]->create(net, sock, proto, kern); 136 if (!err) 137 bt_sock_reclassify_lock(sock->sk, proto); 138 module_put(bt_proto[proto]->owner); 139 } 140 141 read_unlock(&bt_proto_lock); 142 143 return err; 144 } 145 146 struct sock *bt_sock_alloc(struct net *net, struct socket *sock, 147 struct proto *prot, int proto, gfp_t prio, int kern) 148 { 149 struct sock *sk; 150 151 sk = sk_alloc(net, PF_BLUETOOTH, prio, prot, kern); 152 if (!sk) 153 return NULL; 154 155 sock_init_data(sock, sk); 156 INIT_LIST_HEAD(&bt_sk(sk)->accept_q); 157 158 sock_reset_flag(sk, SOCK_ZAPPED); 159 160 sk->sk_protocol = proto; 161 sk->sk_state = BT_OPEN; 162 163 /* Init peer information so it can be properly monitored */ 164 if (!kern) { 165 spin_lock(&sk->sk_peer_lock); 166 sk->sk_peer_pid = get_pid(task_tgid(current)); 167 sk->sk_peer_cred = get_current_cred(); 168 spin_unlock(&sk->sk_peer_lock); 169 } 170 171 return sk; 172 } 173 EXPORT_SYMBOL(bt_sock_alloc); 174 175 void bt_sock_link(struct bt_sock_list *l, struct sock *sk) 176 { 177 write_lock(&l->lock); 178 sk_add_node(sk, &l->head); 179 write_unlock(&l->lock); 180 } 181 EXPORT_SYMBOL(bt_sock_link); 182 183 void bt_sock_unlink(struct bt_sock_list *l, struct sock *sk) 184 { 185 write_lock(&l->lock); 186 sk_del_node_init(sk); 187 write_unlock(&l->lock); 188 } 189 EXPORT_SYMBOL(bt_sock_unlink); 190 191 bool bt_sock_linked(struct bt_sock_list *l, struct sock *s) 192 { 193 struct sock *sk; 194 195 if (!l || !s) 196 return false; 197 198 read_lock(&l->lock); 199 200 sk_for_each(sk, &l->head) { 201 if (s == sk) { 202 read_unlock(&l->lock); 203 return true; 204 } 205 } 206 207 read_unlock(&l->lock); 208 209 return false; 210 } 211 EXPORT_SYMBOL(bt_sock_linked); 212 213 void bt_accept_enqueue(struct sock *parent, struct sock *sk, bool bh) 214 { 215 const struct cred *old_cred; 216 struct pid *old_pid; 217 218 BT_DBG("parent %p, sk %p", parent, sk); 219 220 sock_hold(sk); 221 222 if (bh) 223 bh_lock_sock_nested(sk); 224 else 225 lock_sock_nested(sk, SINGLE_DEPTH_NESTING); 226 227 list_add_tail(&bt_sk(sk)->accept_q, &bt_sk(parent)->accept_q); 228 bt_sk(sk)->parent = parent; 229 230 /* Copy credentials from parent since for incoming connections the 231 * socket is allocated by the kernel. 232 */ 233 spin_lock(&sk->sk_peer_lock); 234 old_pid = sk->sk_peer_pid; 235 old_cred = sk->sk_peer_cred; 236 sk->sk_peer_pid = get_pid(parent->sk_peer_pid); 237 sk->sk_peer_cred = get_cred(parent->sk_peer_cred); 238 spin_unlock(&sk->sk_peer_lock); 239 240 put_pid(old_pid); 241 put_cred(old_cred); 242 243 if (bh) 244 bh_unlock_sock(sk); 245 else 246 release_sock(sk); 247 248 sk_acceptq_added(parent); 249 } 250 EXPORT_SYMBOL(bt_accept_enqueue); 251 252 /* Calling function must hold the sk lock. 253 * bt_sk(sk)->parent must be non-NULL meaning sk is in the parent list. 254 */ 255 void bt_accept_unlink(struct sock *sk) 256 { 257 BT_DBG("sk %p state %d", sk, sk->sk_state); 258 259 list_del_init(&bt_sk(sk)->accept_q); 260 sk_acceptq_removed(bt_sk(sk)->parent); 261 bt_sk(sk)->parent = NULL; 262 sock_put(sk); 263 } 264 EXPORT_SYMBOL(bt_accept_unlink); 265 266 struct sock *bt_accept_dequeue(struct sock *parent, struct socket *newsock) 267 { 268 struct bt_sock *s, *n; 269 struct sock *sk; 270 271 BT_DBG("parent %p", parent); 272 273 restart: 274 list_for_each_entry_safe(s, n, &bt_sk(parent)->accept_q, accept_q) { 275 sk = (struct sock *)s; 276 277 /* Prevent early freeing of sk due to unlink and sock_kill */ 278 sock_hold(sk); 279 lock_sock(sk); 280 281 /* Check sk has not already been unlinked via 282 * bt_accept_unlink() due to serialisation caused by sk locking 283 */ 284 if (!bt_sk(sk)->parent) { 285 BT_DBG("sk %p, already unlinked", sk); 286 release_sock(sk); 287 sock_put(sk); 288 289 /* Restart the loop as sk is no longer in the list 290 * and also avoid a potential infinite loop because 291 * list_for_each_entry_safe() is not thread safe. 292 */ 293 goto restart; 294 } 295 296 /* sk is safely in the parent list so reduce reference count */ 297 sock_put(sk); 298 299 /* FIXME: Is this check still needed */ 300 if (sk->sk_state == BT_CLOSED) { 301 bt_accept_unlink(sk); 302 release_sock(sk); 303 continue; 304 } 305 306 if (sk->sk_state == BT_CONNECTED || !newsock || 307 test_bit(BT_SK_DEFER_SETUP, &bt_sk(parent)->flags)) { 308 bt_accept_unlink(sk); 309 if (newsock) 310 sock_graft(sk, newsock); 311 312 release_sock(sk); 313 return sk; 314 } 315 316 release_sock(sk); 317 } 318 319 return NULL; 320 } 321 EXPORT_SYMBOL(bt_accept_dequeue); 322 323 int bt_sock_recvmsg(struct socket *sock, struct msghdr *msg, size_t len, 324 int flags) 325 { 326 struct sock *sk = sock->sk; 327 struct sk_buff *skb; 328 size_t copied; 329 size_t skblen; 330 int err; 331 332 BT_DBG("sock %p sk %p len %zu", sock, sk, len); 333 334 if (flags & MSG_OOB) 335 return -EOPNOTSUPP; 336 337 skb = skb_recv_datagram(sk, flags, &err); 338 if (!skb) { 339 if (sk->sk_shutdown & RCV_SHUTDOWN) 340 err = 0; 341 342 return err; 343 } 344 345 skblen = skb->len; 346 copied = skb->len; 347 if (len < copied) { 348 msg->msg_flags |= MSG_TRUNC; 349 copied = len; 350 } 351 352 skb_reset_transport_header(skb); 353 err = skb_copy_datagram_msg(skb, 0, msg, copied); 354 if (err == 0) { 355 sock_recv_cmsgs(msg, sk, skb); 356 357 if (msg->msg_name && bt_sk(sk)->skb_msg_name) 358 bt_sk(sk)->skb_msg_name(skb, msg->msg_name, 359 &msg->msg_namelen); 360 361 if (test_bit(BT_SK_PKT_STATUS, &bt_sk(sk)->flags)) { 362 u8 pkt_status = hci_skb_pkt_status(skb); 363 364 put_cmsg(msg, SOL_BLUETOOTH, BT_SCM_PKT_STATUS, 365 sizeof(pkt_status), &pkt_status); 366 } 367 368 if (test_bit(BT_SK_PKT_SEQNUM, &bt_sk(sk)->flags)) { 369 u16 pkt_seqnum = hci_skb_pkt_seqnum(skb); 370 371 put_cmsg(msg, SOL_BLUETOOTH, BT_SCM_PKT_SEQNUM, 372 sizeof(pkt_seqnum), &pkt_seqnum); 373 } 374 } 375 376 skb_free_datagram(sk, skb); 377 378 if (flags & MSG_TRUNC) 379 copied = skblen; 380 381 return err ? : copied; 382 } 383 EXPORT_SYMBOL(bt_sock_recvmsg); 384 385 static long bt_sock_data_wait(struct sock *sk, long timeo) 386 { 387 DECLARE_WAITQUEUE(wait, current); 388 389 add_wait_queue(sk_sleep(sk), &wait); 390 for (;;) { 391 set_current_state(TASK_INTERRUPTIBLE); 392 393 if (!skb_queue_empty(&sk->sk_receive_queue)) 394 break; 395 396 if (sk->sk_err || (sk->sk_shutdown & RCV_SHUTDOWN)) 397 break; 398 399 if (signal_pending(current) || !timeo) 400 break; 401 402 sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk); 403 release_sock(sk); 404 timeo = schedule_timeout(timeo); 405 lock_sock(sk); 406 sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk); 407 } 408 409 __set_current_state(TASK_RUNNING); 410 remove_wait_queue(sk_sleep(sk), &wait); 411 return timeo; 412 } 413 414 int bt_sock_stream_recvmsg(struct socket *sock, struct msghdr *msg, 415 size_t size, int flags) 416 { 417 struct sock *sk = sock->sk; 418 int err = 0; 419 size_t target, copied = 0; 420 long timeo; 421 422 if (flags & MSG_OOB) 423 return -EOPNOTSUPP; 424 425 BT_DBG("sk %p size %zu", sk, size); 426 427 lock_sock(sk); 428 429 target = sock_rcvlowat(sk, flags & MSG_WAITALL, size); 430 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); 431 432 do { 433 struct sk_buff *skb; 434 int chunk; 435 436 skb = skb_dequeue(&sk->sk_receive_queue); 437 if (!skb) { 438 if (copied >= target) 439 break; 440 441 err = sock_error(sk); 442 if (err) 443 break; 444 if (sk->sk_shutdown & RCV_SHUTDOWN) 445 break; 446 447 err = -EAGAIN; 448 if (!timeo) 449 break; 450 451 timeo = bt_sock_data_wait(sk, timeo); 452 453 if (signal_pending(current)) { 454 err = sock_intr_errno(timeo); 455 goto out; 456 } 457 continue; 458 } 459 460 chunk = min_t(unsigned int, skb->len, size); 461 if (skb_copy_datagram_msg(skb, 0, msg, chunk)) { 462 skb_queue_head(&sk->sk_receive_queue, skb); 463 if (!copied) 464 copied = -EFAULT; 465 break; 466 } 467 copied += chunk; 468 size -= chunk; 469 470 sock_recv_cmsgs(msg, sk, skb); 471 472 if (!(flags & MSG_PEEK)) { 473 int skb_len = skb_headlen(skb); 474 475 if (chunk <= skb_len) { 476 __skb_pull(skb, chunk); 477 } else { 478 struct sk_buff *frag; 479 480 __skb_pull(skb, skb_len); 481 chunk -= skb_len; 482 483 skb_walk_frags(skb, frag) { 484 if (chunk <= frag->len) { 485 /* Pulling partial data */ 486 skb->len -= chunk; 487 skb->data_len -= chunk; 488 __skb_pull(frag, chunk); 489 break; 490 } else if (frag->len) { 491 /* Pulling all frag data */ 492 chunk -= frag->len; 493 skb->len -= frag->len; 494 skb->data_len -= frag->len; 495 __skb_pull(frag, frag->len); 496 } 497 } 498 } 499 500 if (skb->len) { 501 skb_queue_head(&sk->sk_receive_queue, skb); 502 break; 503 } 504 kfree_skb(skb); 505 506 } else { 507 /* put message back and return */ 508 skb_queue_head(&sk->sk_receive_queue, skb); 509 break; 510 } 511 } while (size); 512 513 out: 514 release_sock(sk); 515 return copied ? : err; 516 } 517 EXPORT_SYMBOL(bt_sock_stream_recvmsg); 518 519 static inline __poll_t bt_accept_poll(struct sock *parent) 520 { 521 struct bt_sock *s, *n; 522 struct sock *sk; 523 524 list_for_each_entry_safe(s, n, &bt_sk(parent)->accept_q, accept_q) { 525 sk = (struct sock *)s; 526 if (sk->sk_state == BT_CONNECTED || 527 (test_bit(BT_SK_DEFER_SETUP, &bt_sk(parent)->flags) && 528 sk->sk_state == BT_CONNECT2)) 529 return EPOLLIN | EPOLLRDNORM; 530 } 531 532 return 0; 533 } 534 535 __poll_t bt_sock_poll(struct file *file, struct socket *sock, 536 poll_table *wait) 537 { 538 struct sock *sk = sock->sk; 539 __poll_t mask = 0; 540 541 poll_wait(file, sk_sleep(sk), wait); 542 543 if (sk->sk_state == BT_LISTEN) 544 return bt_accept_poll(sk); 545 546 if (sk->sk_err || !skb_queue_empty_lockless(&sk->sk_error_queue)) 547 mask |= EPOLLERR | 548 (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? EPOLLPRI : 0); 549 550 if (sk->sk_shutdown & RCV_SHUTDOWN) 551 mask |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM; 552 553 if (sk->sk_shutdown == SHUTDOWN_MASK) 554 mask |= EPOLLHUP; 555 556 if (!skb_queue_empty_lockless(&sk->sk_receive_queue)) 557 mask |= EPOLLIN | EPOLLRDNORM; 558 559 if (sk->sk_state == BT_CLOSED) 560 mask |= EPOLLHUP; 561 562 if (sk->sk_state == BT_CONNECT || 563 sk->sk_state == BT_CONNECT2 || 564 sk->sk_state == BT_CONFIG) 565 return mask; 566 567 if (!test_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags) && sock_writeable(sk)) 568 mask |= EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND; 569 else 570 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk); 571 572 return mask; 573 } 574 EXPORT_SYMBOL(bt_sock_poll); 575 576 static int bt_ethtool_get_ts_info(struct sock *sk, unsigned int index, 577 void __user *useraddr) 578 { 579 struct ethtool_ts_info info; 580 struct kernel_ethtool_ts_info ts_info = {}; 581 int ret; 582 583 ret = hci_ethtool_ts_info(index, sk->sk_protocol, &ts_info); 584 if (ret == -ENODEV) 585 return ret; 586 else if (ret < 0) 587 return -EIO; 588 589 memset(&info, 0, sizeof(info)); 590 591 info.cmd = ETHTOOL_GET_TS_INFO; 592 info.so_timestamping = ts_info.so_timestamping; 593 info.phc_index = ts_info.phc_index; 594 info.tx_types = ts_info.tx_types; 595 info.rx_filters = ts_info.rx_filters; 596 597 if (copy_to_user(useraddr, &info, sizeof(info))) 598 return -EFAULT; 599 600 return 0; 601 } 602 603 static int bt_ethtool(struct sock *sk, const struct ifreq *ifr, 604 void __user *useraddr) 605 { 606 unsigned int index; 607 u32 ethcmd; 608 int n; 609 610 if (copy_from_user(ðcmd, useraddr, sizeof(ethcmd))) 611 return -EFAULT; 612 613 if (sscanf(ifr->ifr_name, "hci%u%n", &index, &n) != 1 || 614 n != strlen(ifr->ifr_name)) 615 return -ENODEV; 616 617 switch (ethcmd) { 618 case ETHTOOL_GET_TS_INFO: 619 return bt_ethtool_get_ts_info(sk, index, useraddr); 620 } 621 622 return -EOPNOTSUPP; 623 } 624 625 static int bt_dev_ioctl(struct socket *sock, unsigned int cmd, void __user *arg) 626 { 627 struct sock *sk = sock->sk; 628 struct ifreq ifr = {}; 629 void __user *data; 630 char *colon; 631 int ret = -ENOIOCTLCMD; 632 633 if (get_user_ifreq(&ifr, &data, arg)) 634 return -EFAULT; 635 636 ifr.ifr_name[IFNAMSIZ - 1] = 0; 637 colon = strchr(ifr.ifr_name, ':'); 638 if (colon) 639 *colon = 0; 640 641 switch (cmd) { 642 case SIOCETHTOOL: 643 ret = bt_ethtool(sk, &ifr, data); 644 break; 645 } 646 647 if (colon) 648 *colon = ':'; 649 650 if (put_user_ifreq(&ifr, arg)) 651 return -EFAULT; 652 653 return ret; 654 } 655 656 int bt_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) 657 { 658 struct sock *sk = sock->sk; 659 struct sk_buff *skb; 660 long amount; 661 int err; 662 663 BT_DBG("sk %p cmd %x arg %lx", sk, cmd, arg); 664 665 switch (cmd) { 666 case TIOCOUTQ: 667 if (sk->sk_state == BT_LISTEN) 668 return -EINVAL; 669 670 amount = sk->sk_sndbuf - sk_wmem_alloc_get(sk); 671 if (amount < 0) 672 amount = 0; 673 err = put_user(amount, (int __user *)arg); 674 break; 675 676 case TIOCINQ: 677 if (sk->sk_state == BT_LISTEN) 678 return -EINVAL; 679 680 spin_lock(&sk->sk_receive_queue.lock); 681 skb = skb_peek(&sk->sk_receive_queue); 682 amount = skb ? skb->len : 0; 683 spin_unlock(&sk->sk_receive_queue.lock); 684 685 err = put_user(amount, (int __user *)arg); 686 break; 687 688 case SIOCETHTOOL: 689 err = bt_dev_ioctl(sock, cmd, (void __user *)arg); 690 break; 691 692 default: 693 err = -ENOIOCTLCMD; 694 break; 695 } 696 697 return err; 698 } 699 EXPORT_SYMBOL(bt_sock_ioctl); 700 701 /* This function expects the sk lock to be held when called */ 702 int bt_sock_wait_state(struct sock *sk, int state, unsigned long timeo) 703 { 704 DECLARE_WAITQUEUE(wait, current); 705 int err = 0; 706 707 BT_DBG("sk %p", sk); 708 709 add_wait_queue(sk_sleep(sk), &wait); 710 set_current_state(TASK_INTERRUPTIBLE); 711 while (sk->sk_state != state) { 712 if (!timeo) { 713 err = -EINPROGRESS; 714 break; 715 } 716 717 if (signal_pending(current)) { 718 err = sock_intr_errno(timeo); 719 break; 720 } 721 722 release_sock(sk); 723 timeo = schedule_timeout(timeo); 724 lock_sock(sk); 725 set_current_state(TASK_INTERRUPTIBLE); 726 727 err = sock_error(sk); 728 if (err) 729 break; 730 } 731 __set_current_state(TASK_RUNNING); 732 remove_wait_queue(sk_sleep(sk), &wait); 733 return err; 734 } 735 EXPORT_SYMBOL(bt_sock_wait_state); 736 737 /* This function expects the sk lock to be held when called */ 738 int bt_sock_wait_ready(struct sock *sk, unsigned int msg_flags) 739 { 740 DECLARE_WAITQUEUE(wait, current); 741 unsigned long timeo; 742 int err = 0; 743 744 BT_DBG("sk %p", sk); 745 746 timeo = sock_sndtimeo(sk, !!(msg_flags & MSG_DONTWAIT)); 747 748 add_wait_queue(sk_sleep(sk), &wait); 749 set_current_state(TASK_INTERRUPTIBLE); 750 while (test_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags)) { 751 if (!timeo) { 752 err = -EAGAIN; 753 break; 754 } 755 756 if (signal_pending(current)) { 757 err = sock_intr_errno(timeo); 758 break; 759 } 760 761 release_sock(sk); 762 timeo = schedule_timeout(timeo); 763 lock_sock(sk); 764 set_current_state(TASK_INTERRUPTIBLE); 765 766 err = sock_error(sk); 767 if (err) 768 break; 769 } 770 __set_current_state(TASK_RUNNING); 771 remove_wait_queue(sk_sleep(sk), &wait); 772 773 return err; 774 } 775 EXPORT_SYMBOL(bt_sock_wait_ready); 776 777 #ifdef CONFIG_PROC_FS 778 static void *bt_seq_start(struct seq_file *seq, loff_t *pos) 779 __acquires(seq->private->l->lock) 780 { 781 struct bt_sock_list *l = pde_data(file_inode(seq->file)); 782 783 read_lock(&l->lock); 784 return seq_hlist_start_head(&l->head, *pos); 785 } 786 787 static void *bt_seq_next(struct seq_file *seq, void *v, loff_t *pos) 788 { 789 struct bt_sock_list *l = pde_data(file_inode(seq->file)); 790 791 return seq_hlist_next(v, &l->head, pos); 792 } 793 794 static void bt_seq_stop(struct seq_file *seq, void *v) 795 __releases(seq->private->l->lock) 796 { 797 struct bt_sock_list *l = pde_data(file_inode(seq->file)); 798 799 read_unlock(&l->lock); 800 } 801 802 static int bt_seq_show(struct seq_file *seq, void *v) 803 { 804 struct bt_sock_list *l = pde_data(file_inode(seq->file)); 805 806 if (v == SEQ_START_TOKEN) { 807 seq_puts(seq, "sk RefCnt Rmem Wmem User Inode Parent"); 808 809 if (l->custom_seq_show) { 810 seq_putc(seq, ' '); 811 l->custom_seq_show(seq, v); 812 } 813 814 seq_putc(seq, '\n'); 815 } else { 816 struct sock *sk = sk_entry(v); 817 struct bt_sock *bt = bt_sk(sk); 818 819 seq_printf(seq, 820 "%pK %-6d %-6u %-6u %-6u %-6lu %-6lu", 821 sk, 822 refcount_read(&sk->sk_refcnt), 823 sk_rmem_alloc_get(sk), 824 sk_wmem_alloc_get(sk), 825 from_kuid(seq_user_ns(seq), sk_uid(sk)), 826 sock_i_ino(sk), 827 bt->parent ? sock_i_ino(bt->parent) : 0LU); 828 829 if (l->custom_seq_show) { 830 seq_putc(seq, ' '); 831 l->custom_seq_show(seq, v); 832 } 833 834 seq_putc(seq, '\n'); 835 } 836 return 0; 837 } 838 839 static const struct seq_operations bt_seq_ops = { 840 .start = bt_seq_start, 841 .next = bt_seq_next, 842 .stop = bt_seq_stop, 843 .show = bt_seq_show, 844 }; 845 846 int bt_procfs_init(struct net *net, const char *name, 847 struct bt_sock_list *sk_list, 848 int (*seq_show)(struct seq_file *, void *)) 849 { 850 sk_list->custom_seq_show = seq_show; 851 852 if (!proc_create_seq_data(name, 0, net->proc_net, &bt_seq_ops, sk_list)) 853 return -ENOMEM; 854 return 0; 855 } 856 857 void bt_procfs_cleanup(struct net *net, const char *name) 858 { 859 remove_proc_entry(name, net->proc_net); 860 } 861 #else 862 int bt_procfs_init(struct net *net, const char *name, 863 struct bt_sock_list *sk_list, 864 int (*seq_show)(struct seq_file *, void *)) 865 { 866 return 0; 867 } 868 869 void bt_procfs_cleanup(struct net *net, const char *name) 870 { 871 } 872 #endif 873 EXPORT_SYMBOL(bt_procfs_init); 874 EXPORT_SYMBOL(bt_procfs_cleanup); 875 876 static const struct net_proto_family bt_sock_family_ops = { 877 .owner = THIS_MODULE, 878 .family = PF_BLUETOOTH, 879 .create = bt_sock_create, 880 }; 881 882 struct dentry *bt_debugfs; 883 EXPORT_SYMBOL_GPL(bt_debugfs); 884 885 #define VERSION __stringify(BT_SUBSYS_VERSION) "." \ 886 __stringify(BT_SUBSYS_REVISION) 887 888 static int __init bt_init(void) 889 { 890 int err; 891 892 sock_skb_cb_check_size(sizeof(struct bt_skb_cb)); 893 894 BT_INFO("Core ver %s", VERSION); 895 896 err = bt_selftest(); 897 if (err < 0) 898 return err; 899 900 bt_debugfs = debugfs_create_dir("bluetooth", NULL); 901 902 bt_leds_init(); 903 904 err = bt_sysfs_init(); 905 if (err < 0) 906 goto cleanup_led; 907 908 err = sock_register(&bt_sock_family_ops); 909 if (err) 910 goto cleanup_sysfs; 911 912 BT_INFO("HCI device and connection manager initialized"); 913 914 err = hci_sock_init(); 915 if (err) 916 goto unregister_socket; 917 918 err = l2cap_init(); 919 if (err) 920 goto cleanup_socket; 921 922 err = sco_init(); 923 if (err) 924 goto cleanup_cap; 925 926 err = mgmt_init(); 927 if (err) 928 goto cleanup_sco; 929 930 return 0; 931 932 cleanup_sco: 933 sco_exit(); 934 cleanup_cap: 935 l2cap_exit(); 936 cleanup_socket: 937 hci_sock_cleanup(); 938 unregister_socket: 939 sock_unregister(PF_BLUETOOTH); 940 cleanup_sysfs: 941 bt_sysfs_cleanup(); 942 cleanup_led: 943 bt_leds_cleanup(); 944 debugfs_remove_recursive(bt_debugfs); 945 return err; 946 } 947 948 static void __exit bt_exit(void) 949 { 950 iso_exit(); 951 952 mgmt_exit(); 953 954 sco_exit(); 955 956 l2cap_exit(); 957 958 hci_sock_cleanup(); 959 960 sock_unregister(PF_BLUETOOTH); 961 962 bt_sysfs_cleanup(); 963 964 bt_leds_cleanup(); 965 966 debugfs_remove_recursive(bt_debugfs); 967 } 968 969 subsys_initcall(bt_init); 970 module_exit(bt_exit); 971 972 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>"); 973 MODULE_DESCRIPTION("Bluetooth Core ver " VERSION); 974 MODULE_VERSION(VERSION); 975 MODULE_LICENSE("GPL"); 976 MODULE_ALIAS_NETPROTO(PF_BLUETOOTH); 977