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 spin_lock_init(&bt_sk(sk)->accept_q_lock); 158 159 sock_reset_flag(sk, SOCK_ZAPPED); 160 161 sk->sk_protocol = proto; 162 sk->sk_state = BT_OPEN; 163 164 /* Init peer information so it can be properly monitored */ 165 if (!kern) { 166 spin_lock(&sk->sk_peer_lock); 167 sk->sk_peer_pid = get_pid(task_tgid(current)); 168 sk->sk_peer_cred = get_current_cred(); 169 spin_unlock(&sk->sk_peer_lock); 170 } 171 172 return sk; 173 } 174 EXPORT_SYMBOL(bt_sock_alloc); 175 176 void bt_sock_link(struct bt_sock_list *l, struct sock *sk) 177 { 178 write_lock(&l->lock); 179 sk_add_node(sk, &l->head); 180 write_unlock(&l->lock); 181 } 182 EXPORT_SYMBOL(bt_sock_link); 183 184 void bt_sock_unlink(struct bt_sock_list *l, struct sock *sk) 185 { 186 write_lock(&l->lock); 187 sk_del_node_init(sk); 188 write_unlock(&l->lock); 189 } 190 EXPORT_SYMBOL(bt_sock_unlink); 191 192 bool bt_sock_linked(struct bt_sock_list *l, struct sock *s) 193 { 194 struct sock *sk; 195 196 if (!l || !s) 197 return false; 198 199 read_lock(&l->lock); 200 201 sk_for_each(sk, &l->head) { 202 if (s == sk) { 203 read_unlock(&l->lock); 204 return true; 205 } 206 } 207 208 read_unlock(&l->lock); 209 210 return false; 211 } 212 EXPORT_SYMBOL(bt_sock_linked); 213 214 void bt_accept_enqueue(struct sock *parent, struct sock *sk, bool bh) 215 { 216 const struct cred *old_cred; 217 struct pid *old_pid; 218 struct bt_sock *par = bt_sk(parent); 219 220 BT_DBG("parent %p, sk %p", parent, sk); 221 222 sock_hold(sk); 223 224 if (bh) 225 bh_lock_sock_nested(sk); 226 else 227 lock_sock_nested(sk, SINGLE_DEPTH_NESTING); 228 229 bt_sk(sk)->parent = parent; 230 231 spin_lock_bh(&par->accept_q_lock); 232 list_add_tail(&bt_sk(sk)->accept_q, &par->accept_q); 233 sk_acceptq_added(parent); 234 spin_unlock_bh(&par->accept_q_lock); 235 236 /* Copy credentials from parent since for incoming connections the 237 * socket is allocated by the kernel. 238 */ 239 spin_lock(&sk->sk_peer_lock); 240 old_pid = sk->sk_peer_pid; 241 old_cred = sk->sk_peer_cred; 242 sk->sk_peer_pid = get_pid(parent->sk_peer_pid); 243 sk->sk_peer_cred = get_cred(parent->sk_peer_cred); 244 spin_unlock(&sk->sk_peer_lock); 245 246 put_pid(old_pid); 247 put_cred(old_cred); 248 249 if (bh) 250 bh_unlock_sock(sk); 251 else 252 release_sock(sk); 253 } 254 EXPORT_SYMBOL(bt_accept_enqueue); 255 256 /* Calling function must hold the sk lock. 257 * bt_sk(sk)->parent must be non-NULL meaning sk is in the parent list. 258 */ 259 void bt_accept_unlink(struct sock *sk) 260 { 261 struct sock *parent = bt_sk(sk)->parent; 262 263 BT_DBG("sk %p state %d", sk, sk->sk_state); 264 265 spin_lock_bh(&bt_sk(parent)->accept_q_lock); 266 list_del_init(&bt_sk(sk)->accept_q); 267 sk_acceptq_removed(parent); 268 spin_unlock_bh(&bt_sk(parent)->accept_q_lock); 269 bt_sk(sk)->parent = NULL; 270 sock_put(sk); 271 } 272 EXPORT_SYMBOL(bt_accept_unlink); 273 274 static struct sock *bt_accept_get(struct sock *parent, struct sock *sk) 275 { 276 struct bt_sock *bt = bt_sk(parent); 277 struct sock *next = NULL; 278 279 /* accept_q is modified from child teardown paths too, so take a 280 * temporary reference before dropping the queue lock. 281 */ 282 spin_lock_bh(&bt->accept_q_lock); 283 284 if (sk) { 285 if (bt_sk(sk)->parent != parent) 286 goto out; 287 288 if (!list_is_last(&bt_sk(sk)->accept_q, &bt->accept_q)) { 289 next = &list_next_entry(bt_sk(sk), accept_q)->sk; 290 sock_hold(next); 291 } 292 } else if (!list_empty(&bt->accept_q)) { 293 next = &list_first_entry(&bt->accept_q, 294 struct bt_sock, accept_q)->sk; 295 sock_hold(next); 296 } 297 298 out: 299 spin_unlock_bh(&bt->accept_q_lock); 300 return next; 301 } 302 303 struct sock *bt_accept_dequeue(struct sock *parent, struct socket *newsock) 304 { 305 struct sock *sk, *next; 306 307 BT_DBG("parent %p", parent); 308 309 restart: 310 for (sk = bt_accept_get(parent, NULL); sk; sk = next) { 311 /* Prevent early freeing of sk due to unlink and sock_kill */ 312 lock_sock(sk); 313 314 /* Check sk has not already been unlinked via 315 * bt_accept_unlink() due to serialisation caused by sk locking 316 */ 317 if (bt_sk(sk)->parent != parent) { 318 BT_DBG("sk %p, already unlinked", sk); 319 release_sock(sk); 320 sock_put(sk); 321 322 goto restart; 323 } 324 325 next = bt_accept_get(parent, sk); 326 327 /* sk is safely in the parent list so reduce reference count */ 328 sock_put(sk); 329 330 /* FIXME: Is this check still needed */ 331 if (sk->sk_state == BT_CLOSED) { 332 bt_accept_unlink(sk); 333 release_sock(sk); 334 continue; 335 } 336 337 if (sk->sk_state == BT_CONNECTED || !newsock || 338 test_bit(BT_SK_DEFER_SETUP, &bt_sk(parent)->flags)) { 339 bt_accept_unlink(sk); 340 if (newsock) 341 sock_graft(sk, newsock); 342 343 release_sock(sk); 344 if (next) 345 sock_put(next); 346 return sk; 347 } 348 349 release_sock(sk); 350 } 351 352 return NULL; 353 } 354 EXPORT_SYMBOL(bt_accept_dequeue); 355 356 int bt_sock_recvmsg(struct socket *sock, struct msghdr *msg, size_t len, 357 int flags) 358 { 359 struct sock *sk = sock->sk; 360 struct sk_buff *skb; 361 size_t copied; 362 size_t skblen; 363 int err; 364 365 BT_DBG("sock %p sk %p len %zu", sock, sk, len); 366 367 if (flags & MSG_OOB) 368 return -EOPNOTSUPP; 369 370 skb = skb_recv_datagram(sk, flags, &err); 371 if (!skb) { 372 if (sk->sk_shutdown & RCV_SHUTDOWN) 373 err = 0; 374 375 return err; 376 } 377 378 skblen = skb->len; 379 copied = skb->len; 380 if (len < copied) { 381 msg->msg_flags |= MSG_TRUNC; 382 copied = len; 383 } 384 385 skb_reset_transport_header(skb); 386 err = skb_copy_datagram_msg(skb, 0, msg, copied); 387 if (err == 0) { 388 sock_recv_cmsgs(msg, sk, skb); 389 390 if (msg->msg_name && bt_sk(sk)->skb_msg_name) 391 bt_sk(sk)->skb_msg_name(skb, msg->msg_name, 392 &msg->msg_namelen); 393 394 if (test_bit(BT_SK_PKT_STATUS, &bt_sk(sk)->flags)) { 395 u8 pkt_status = hci_skb_pkt_status(skb); 396 397 put_cmsg(msg, SOL_BLUETOOTH, BT_SCM_PKT_STATUS, 398 sizeof(pkt_status), &pkt_status); 399 } 400 401 if (test_bit(BT_SK_PKT_SEQNUM, &bt_sk(sk)->flags)) { 402 u16 pkt_seqnum = hci_skb_pkt_seqnum(skb); 403 404 put_cmsg(msg, SOL_BLUETOOTH, BT_SCM_PKT_SEQNUM, 405 sizeof(pkt_seqnum), &pkt_seqnum); 406 } 407 } 408 409 skb_free_datagram(sk, skb); 410 411 if (flags & MSG_TRUNC) 412 copied = skblen; 413 414 return err ? : copied; 415 } 416 EXPORT_SYMBOL(bt_sock_recvmsg); 417 418 static long bt_sock_data_wait(struct sock *sk, long timeo) 419 { 420 DECLARE_WAITQUEUE(wait, current); 421 422 add_wait_queue(sk_sleep(sk), &wait); 423 for (;;) { 424 set_current_state(TASK_INTERRUPTIBLE); 425 426 if (!skb_queue_empty(&sk->sk_receive_queue)) 427 break; 428 429 if (sk->sk_err || (sk->sk_shutdown & RCV_SHUTDOWN)) 430 break; 431 432 if (signal_pending(current) || !timeo) 433 break; 434 435 sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk); 436 release_sock(sk); 437 timeo = schedule_timeout(timeo); 438 lock_sock(sk); 439 sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk); 440 } 441 442 __set_current_state(TASK_RUNNING); 443 remove_wait_queue(sk_sleep(sk), &wait); 444 return timeo; 445 } 446 447 int bt_sock_stream_recvmsg(struct socket *sock, struct msghdr *msg, 448 size_t size, int flags) 449 { 450 struct sock *sk = sock->sk; 451 int err = 0; 452 size_t target, copied = 0; 453 long timeo; 454 455 if (flags & MSG_OOB) 456 return -EOPNOTSUPP; 457 458 BT_DBG("sk %p size %zu", sk, size); 459 460 lock_sock(sk); 461 462 target = sock_rcvlowat(sk, flags & MSG_WAITALL, size); 463 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); 464 465 do { 466 struct sk_buff *skb; 467 int chunk; 468 469 skb = skb_dequeue(&sk->sk_receive_queue); 470 if (!skb) { 471 if (copied >= target) 472 break; 473 474 err = sock_error(sk); 475 if (err) 476 break; 477 if (sk->sk_shutdown & RCV_SHUTDOWN) 478 break; 479 480 err = -EAGAIN; 481 if (!timeo) 482 break; 483 484 timeo = bt_sock_data_wait(sk, timeo); 485 486 if (signal_pending(current)) { 487 err = sock_intr_errno(timeo); 488 goto out; 489 } 490 continue; 491 } 492 493 chunk = min_t(unsigned int, skb->len, size); 494 if (skb_copy_datagram_msg(skb, 0, msg, chunk)) { 495 skb_queue_head(&sk->sk_receive_queue, skb); 496 if (!copied) 497 copied = -EFAULT; 498 break; 499 } 500 copied += chunk; 501 size -= chunk; 502 503 sock_recv_cmsgs(msg, sk, skb); 504 505 if (!(flags & MSG_PEEK)) { 506 int skb_len = skb_headlen(skb); 507 508 if (chunk <= skb_len) { 509 __skb_pull(skb, chunk); 510 } else { 511 struct sk_buff *frag; 512 513 __skb_pull(skb, skb_len); 514 chunk -= skb_len; 515 516 skb_walk_frags(skb, frag) { 517 if (chunk <= frag->len) { 518 /* Pulling partial data */ 519 skb->len -= chunk; 520 skb->data_len -= chunk; 521 __skb_pull(frag, chunk); 522 break; 523 } else if (frag->len) { 524 /* Pulling all frag data */ 525 chunk -= frag->len; 526 skb->len -= frag->len; 527 skb->data_len -= frag->len; 528 __skb_pull(frag, frag->len); 529 } 530 } 531 } 532 533 if (skb->len) { 534 skb_queue_head(&sk->sk_receive_queue, skb); 535 break; 536 } 537 kfree_skb(skb); 538 539 } else { 540 /* put message back and return */ 541 skb_queue_head(&sk->sk_receive_queue, skb); 542 break; 543 } 544 } while (size); 545 546 out: 547 release_sock(sk); 548 return copied ? : err; 549 } 550 EXPORT_SYMBOL(bt_sock_stream_recvmsg); 551 552 static inline __poll_t bt_accept_poll(struct sock *parent) 553 { 554 struct bt_sock *bt = bt_sk(parent); 555 struct bt_sock *s; 556 struct sock *sk; 557 __poll_t mask = 0; 558 559 spin_lock_bh(&bt->accept_q_lock); 560 list_for_each_entry(s, &bt->accept_q, accept_q) { 561 int state; 562 563 sk = (struct sock *)s; 564 state = READ_ONCE(sk->sk_state); 565 566 if (state == BT_CONNECTED || 567 (test_bit(BT_SK_DEFER_SETUP, &bt->flags) && 568 state == BT_CONNECT2)) { 569 mask = EPOLLIN | EPOLLRDNORM; 570 break; 571 } 572 } 573 spin_unlock_bh(&bt->accept_q_lock); 574 575 return mask; 576 } 577 578 __poll_t bt_sock_poll(struct file *file, struct socket *sock, 579 poll_table *wait) 580 { 581 struct sock *sk = sock->sk; 582 __poll_t mask = 0; 583 584 poll_wait(file, sk_sleep(sk), wait); 585 586 if (sk->sk_state == BT_LISTEN) 587 return bt_accept_poll(sk); 588 589 if (sk->sk_err || !skb_queue_empty_lockless(&sk->sk_error_queue)) 590 mask |= EPOLLERR | 591 (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? EPOLLPRI : 0); 592 593 if (sk->sk_shutdown & RCV_SHUTDOWN) 594 mask |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM; 595 596 if (sk->sk_shutdown == SHUTDOWN_MASK) 597 mask |= EPOLLHUP; 598 599 if (!skb_queue_empty_lockless(&sk->sk_receive_queue)) 600 mask |= EPOLLIN | EPOLLRDNORM; 601 602 if (sk->sk_state == BT_CLOSED) 603 mask |= EPOLLHUP; 604 605 if (sk->sk_state == BT_CONNECT || 606 sk->sk_state == BT_CONNECT2 || 607 sk->sk_state == BT_CONFIG) 608 return mask; 609 610 if (!test_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags) && sock_writeable(sk)) 611 mask |= EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND; 612 else 613 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk); 614 615 return mask; 616 } 617 EXPORT_SYMBOL(bt_sock_poll); 618 619 static int bt_ethtool_get_ts_info(struct sock *sk, unsigned int index, 620 void __user *useraddr) 621 { 622 struct ethtool_ts_info info; 623 struct kernel_ethtool_ts_info ts_info = {}; 624 int ret; 625 626 ret = hci_ethtool_ts_info(index, sk->sk_protocol, &ts_info); 627 if (ret == -ENODEV) 628 return ret; 629 else if (ret < 0) 630 return -EIO; 631 632 memset(&info, 0, sizeof(info)); 633 634 info.cmd = ETHTOOL_GET_TS_INFO; 635 info.so_timestamping = ts_info.so_timestamping; 636 info.phc_index = ts_info.phc_index; 637 info.tx_types = ts_info.tx_types; 638 info.rx_filters = ts_info.rx_filters; 639 640 if (copy_to_user(useraddr, &info, sizeof(info))) 641 return -EFAULT; 642 643 return 0; 644 } 645 646 static int bt_ethtool(struct sock *sk, const struct ifreq *ifr, 647 void __user *useraddr) 648 { 649 unsigned int index; 650 u32 ethcmd; 651 int n; 652 653 if (copy_from_user(ðcmd, useraddr, sizeof(ethcmd))) 654 return -EFAULT; 655 656 if (sscanf(ifr->ifr_name, "hci%u%n", &index, &n) != 1 || 657 n != strlen(ifr->ifr_name)) 658 return -ENODEV; 659 660 switch (ethcmd) { 661 case ETHTOOL_GET_TS_INFO: 662 return bt_ethtool_get_ts_info(sk, index, useraddr); 663 } 664 665 return -EOPNOTSUPP; 666 } 667 668 static int bt_dev_ioctl(struct socket *sock, unsigned int cmd, void __user *arg) 669 { 670 struct sock *sk = sock->sk; 671 struct ifreq ifr = {}; 672 void __user *data; 673 char *colon; 674 int ret = -ENOIOCTLCMD; 675 676 if (get_user_ifreq(&ifr, &data, arg)) 677 return -EFAULT; 678 679 ifr.ifr_name[IFNAMSIZ - 1] = 0; 680 colon = strchr(ifr.ifr_name, ':'); 681 if (colon) 682 *colon = 0; 683 684 switch (cmd) { 685 case SIOCETHTOOL: 686 ret = bt_ethtool(sk, &ifr, data); 687 break; 688 } 689 690 if (colon) 691 *colon = ':'; 692 693 if (put_user_ifreq(&ifr, arg)) 694 return -EFAULT; 695 696 return ret; 697 } 698 699 int bt_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) 700 { 701 struct sock *sk = sock->sk; 702 struct sk_buff *skb; 703 long amount; 704 int err; 705 706 BT_DBG("sk %p cmd %x arg %lx", sk, cmd, arg); 707 708 switch (cmd) { 709 case TIOCOUTQ: 710 if (sk->sk_state == BT_LISTEN) 711 return -EINVAL; 712 713 amount = sk->sk_sndbuf - sk_wmem_alloc_get(sk); 714 if (amount < 0) 715 amount = 0; 716 err = put_user(amount, (int __user *)arg); 717 break; 718 719 case TIOCINQ: 720 if (sk->sk_state == BT_LISTEN) 721 return -EINVAL; 722 723 spin_lock(&sk->sk_receive_queue.lock); 724 skb = skb_peek(&sk->sk_receive_queue); 725 amount = skb ? skb->len : 0; 726 spin_unlock(&sk->sk_receive_queue.lock); 727 728 err = put_user(amount, (int __user *)arg); 729 break; 730 731 case SIOCETHTOOL: 732 err = bt_dev_ioctl(sock, cmd, (void __user *)arg); 733 break; 734 735 default: 736 err = -ENOIOCTLCMD; 737 break; 738 } 739 740 return err; 741 } 742 EXPORT_SYMBOL(bt_sock_ioctl); 743 744 /* This function expects the sk lock to be held when called */ 745 int bt_sock_wait_state(struct sock *sk, int state, unsigned long timeo) 746 { 747 DECLARE_WAITQUEUE(wait, current); 748 int err = 0; 749 750 BT_DBG("sk %p", sk); 751 752 add_wait_queue(sk_sleep(sk), &wait); 753 set_current_state(TASK_INTERRUPTIBLE); 754 while (sk->sk_state != state) { 755 if (!timeo) { 756 err = -EINPROGRESS; 757 break; 758 } 759 760 if (signal_pending(current)) { 761 err = sock_intr_errno(timeo); 762 break; 763 } 764 765 release_sock(sk); 766 timeo = schedule_timeout(timeo); 767 lock_sock(sk); 768 set_current_state(TASK_INTERRUPTIBLE); 769 770 err = sock_error(sk); 771 if (err) 772 break; 773 } 774 __set_current_state(TASK_RUNNING); 775 remove_wait_queue(sk_sleep(sk), &wait); 776 return err; 777 } 778 EXPORT_SYMBOL(bt_sock_wait_state); 779 780 /* This function expects the sk lock to be held when called */ 781 int bt_sock_wait_ready(struct sock *sk, unsigned int msg_flags) 782 { 783 DECLARE_WAITQUEUE(wait, current); 784 unsigned long timeo; 785 int err = 0; 786 787 BT_DBG("sk %p", sk); 788 789 timeo = sock_sndtimeo(sk, !!(msg_flags & MSG_DONTWAIT)); 790 791 add_wait_queue(sk_sleep(sk), &wait); 792 set_current_state(TASK_INTERRUPTIBLE); 793 while (test_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags)) { 794 if (!timeo) { 795 err = -EAGAIN; 796 break; 797 } 798 799 if (signal_pending(current)) { 800 err = sock_intr_errno(timeo); 801 break; 802 } 803 804 release_sock(sk); 805 timeo = schedule_timeout(timeo); 806 lock_sock(sk); 807 set_current_state(TASK_INTERRUPTIBLE); 808 809 err = sock_error(sk); 810 if (err) 811 break; 812 } 813 __set_current_state(TASK_RUNNING); 814 remove_wait_queue(sk_sleep(sk), &wait); 815 816 return err; 817 } 818 EXPORT_SYMBOL(bt_sock_wait_ready); 819 820 #ifdef CONFIG_PROC_FS 821 static void *bt_seq_start(struct seq_file *seq, loff_t *pos) 822 __acquires(seq->private->l->lock) 823 { 824 struct bt_sock_list *l = pde_data(file_inode(seq->file)); 825 826 read_lock(&l->lock); 827 return seq_hlist_start_head(&l->head, *pos); 828 } 829 830 static void *bt_seq_next(struct seq_file *seq, void *v, loff_t *pos) 831 { 832 struct bt_sock_list *l = pde_data(file_inode(seq->file)); 833 834 return seq_hlist_next(v, &l->head, pos); 835 } 836 837 static void bt_seq_stop(struct seq_file *seq, void *v) 838 __releases(seq->private->l->lock) 839 { 840 struct bt_sock_list *l = pde_data(file_inode(seq->file)); 841 842 read_unlock(&l->lock); 843 } 844 845 static int bt_seq_show(struct seq_file *seq, void *v) 846 { 847 struct bt_sock_list *l = pde_data(file_inode(seq->file)); 848 849 if (v == SEQ_START_TOKEN) { 850 seq_puts(seq, "sk RefCnt Rmem Wmem User Inode Parent"); 851 852 if (l->custom_seq_show) { 853 seq_putc(seq, ' '); 854 l->custom_seq_show(seq, v); 855 } 856 857 seq_putc(seq, '\n'); 858 } else { 859 struct sock *sk = sk_entry(v); 860 struct bt_sock *bt = bt_sk(sk); 861 862 seq_printf(seq, 863 "%pK %-6d %-6u %-6u %-6u %-6llu %-6llu", 864 sk, 865 refcount_read(&sk->sk_refcnt), 866 sk_rmem_alloc_get(sk), 867 sk_wmem_alloc_get(sk), 868 from_kuid(seq_user_ns(seq), sk_uid(sk)), 869 sock_i_ino(sk), 870 bt->parent ? sock_i_ino(bt->parent) : 0ULL); 871 872 if (l->custom_seq_show) { 873 seq_putc(seq, ' '); 874 l->custom_seq_show(seq, v); 875 } 876 877 seq_putc(seq, '\n'); 878 } 879 return 0; 880 } 881 882 static const struct seq_operations bt_seq_ops = { 883 .start = bt_seq_start, 884 .next = bt_seq_next, 885 .stop = bt_seq_stop, 886 .show = bt_seq_show, 887 }; 888 889 int bt_procfs_init(struct net *net, const char *name, 890 struct bt_sock_list *sk_list, 891 int (*seq_show)(struct seq_file *, void *)) 892 { 893 sk_list->custom_seq_show = seq_show; 894 895 if (!proc_create_seq_data(name, 0, net->proc_net, &bt_seq_ops, sk_list)) 896 return -ENOMEM; 897 return 0; 898 } 899 900 void bt_procfs_cleanup(struct net *net, const char *name) 901 { 902 remove_proc_entry(name, net->proc_net); 903 } 904 #else 905 int bt_procfs_init(struct net *net, const char *name, 906 struct bt_sock_list *sk_list, 907 int (*seq_show)(struct seq_file *, void *)) 908 { 909 return 0; 910 } 911 912 void bt_procfs_cleanup(struct net *net, const char *name) 913 { 914 } 915 #endif 916 EXPORT_SYMBOL(bt_procfs_init); 917 EXPORT_SYMBOL(bt_procfs_cleanup); 918 919 static const struct net_proto_family bt_sock_family_ops = { 920 .owner = THIS_MODULE, 921 .family = PF_BLUETOOTH, 922 .create = bt_sock_create, 923 }; 924 925 struct dentry *bt_debugfs; 926 EXPORT_SYMBOL_GPL(bt_debugfs); 927 928 #define VERSION __stringify(BT_SUBSYS_VERSION) "." \ 929 __stringify(BT_SUBSYS_REVISION) 930 931 static int __init bt_init(void) 932 { 933 int err; 934 935 sock_skb_cb_check_size(sizeof(struct bt_skb_cb)); 936 937 BT_INFO("Core ver %s", VERSION); 938 939 err = bt_selftest(); 940 if (err < 0) 941 return err; 942 943 bt_debugfs = debugfs_create_dir("bluetooth", NULL); 944 945 bt_leds_init(); 946 947 err = bt_sysfs_init(); 948 if (err < 0) 949 goto cleanup_led; 950 951 err = sock_register(&bt_sock_family_ops); 952 if (err) 953 goto cleanup_sysfs; 954 955 BT_INFO("HCI device and connection manager initialized"); 956 957 err = hci_sock_init(); 958 if (err) 959 goto unregister_socket; 960 961 err = l2cap_init(); 962 if (err) 963 goto cleanup_socket; 964 965 err = sco_init(); 966 if (err) 967 goto cleanup_cap; 968 969 err = mgmt_init(); 970 if (err) 971 goto cleanup_sco; 972 973 return 0; 974 975 cleanup_sco: 976 sco_exit(); 977 cleanup_cap: 978 l2cap_exit(); 979 cleanup_socket: 980 hci_sock_cleanup(); 981 unregister_socket: 982 sock_unregister(PF_BLUETOOTH); 983 cleanup_sysfs: 984 bt_sysfs_cleanup(); 985 cleanup_led: 986 bt_leds_cleanup(); 987 debugfs_remove_recursive(bt_debugfs); 988 return err; 989 } 990 991 static void __exit bt_exit(void) 992 { 993 iso_exit(); 994 995 mgmt_exit(); 996 997 sco_exit(); 998 999 l2cap_exit(); 1000 1001 hci_sock_cleanup(); 1002 1003 sock_unregister(PF_BLUETOOTH); 1004 1005 bt_sysfs_cleanup(); 1006 1007 bt_leds_cleanup(); 1008 1009 debugfs_remove_recursive(bt_debugfs); 1010 } 1011 1012 subsys_initcall(bt_init); 1013 module_exit(bt_exit); 1014 1015 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>"); 1016 MODULE_DESCRIPTION("Bluetooth Core ver " VERSION); 1017 MODULE_VERSION(VERSION); 1018 MODULE_LICENSE("GPL"); 1019 MODULE_ALIAS_NETPROTO(PF_BLUETOOTH); 1020