1 /* 2 BlueZ - Bluetooth protocol stack for Linux 3 Copyright (C) 2000-2001 Qualcomm Incorporated 4 Copyright (C) 2009-2010 Gustavo F. Padovan <gustavo@padovan.org> 5 Copyright (C) 2010 Google Inc. 6 Copyright (C) 2011 ProFUSION Embedded Systems 7 8 Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com> 9 10 This program is free software; you can redistribute it and/or modify 11 it under the terms of the GNU General Public License version 2 as 12 published by the Free Software Foundation; 13 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. 17 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY 18 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 21 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 22 23 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 24 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 25 SOFTWARE IS DISCLAIMED. 26 */ 27 28 /* Bluetooth L2CAP sockets. */ 29 30 #include <linux/module.h> 31 #include <linux/export.h> 32 #include <linux/filter.h> 33 #include <linux/sched/signal.h> 34 35 #include <net/bluetooth/bluetooth.h> 36 #include <net/bluetooth/hci_core.h> 37 #include <net/bluetooth/l2cap.h> 38 39 #include "smp.h" 40 41 static struct bt_sock_list l2cap_sk_list = { 42 .lock = __RW_LOCK_UNLOCKED(l2cap_sk_list.lock) 43 }; 44 45 static const struct proto_ops l2cap_sock_ops; 46 static void l2cap_sock_init(struct sock *sk, struct sock *parent); 47 static struct sock *l2cap_sock_alloc(struct net *net, struct socket *sock, 48 int proto, gfp_t prio, int kern); 49 static void l2cap_sock_cleanup_listen(struct sock *parent); 50 51 bool l2cap_is_socket(struct socket *sock) 52 { 53 return sock && sock->ops == &l2cap_sock_ops; 54 } 55 EXPORT_SYMBOL(l2cap_is_socket); 56 57 static int l2cap_validate_bredr_psm(u16 psm) 58 { 59 /* PSM must be odd and lsb of upper byte must be 0 */ 60 if ((psm & 0x0101) != 0x0001) 61 return -EINVAL; 62 63 /* Restrict usage of well-known PSMs */ 64 if (psm < L2CAP_PSM_DYN_START && !capable(CAP_NET_BIND_SERVICE)) 65 return -EACCES; 66 67 return 0; 68 } 69 70 static int l2cap_validate_le_psm(u16 psm) 71 { 72 /* Valid LE_PSM ranges are defined only until 0x00ff */ 73 if (psm > L2CAP_PSM_LE_DYN_END) 74 return -EINVAL; 75 76 /* Restrict fixed, SIG assigned PSM values to CAP_NET_BIND_SERVICE */ 77 if (psm < L2CAP_PSM_LE_DYN_START && !capable(CAP_NET_BIND_SERVICE)) 78 return -EACCES; 79 80 return 0; 81 } 82 83 static int l2cap_sock_bind(struct socket *sock, struct sockaddr *addr, int alen) 84 { 85 struct sock *sk = sock->sk; 86 struct l2cap_chan *chan = l2cap_pi(sk)->chan; 87 struct sockaddr_l2 la; 88 int len, err = 0; 89 90 BT_DBG("sk %p", sk); 91 92 if (!addr || alen < offsetofend(struct sockaddr, sa_family) || 93 addr->sa_family != AF_BLUETOOTH) 94 return -EINVAL; 95 96 memset(&la, 0, sizeof(la)); 97 len = min_t(unsigned int, sizeof(la), alen); 98 memcpy(&la, addr, len); 99 100 if (la.l2_cid && la.l2_psm) 101 return -EINVAL; 102 103 if (!bdaddr_type_is_valid(la.l2_bdaddr_type)) 104 return -EINVAL; 105 106 if (bdaddr_type_is_le(la.l2_bdaddr_type)) { 107 /* We only allow ATT user space socket */ 108 if (la.l2_cid && 109 la.l2_cid != cpu_to_le16(L2CAP_CID_ATT)) 110 return -EINVAL; 111 } 112 113 lock_sock(sk); 114 115 if (sk->sk_state != BT_OPEN) { 116 err = -EBADFD; 117 goto done; 118 } 119 120 if (la.l2_psm) { 121 __u16 psm = __le16_to_cpu(la.l2_psm); 122 123 if (la.l2_bdaddr_type == BDADDR_BREDR) 124 err = l2cap_validate_bredr_psm(psm); 125 else 126 err = l2cap_validate_le_psm(psm); 127 128 if (err) 129 goto done; 130 } 131 132 bacpy(&chan->src, &la.l2_bdaddr); 133 chan->src_type = la.l2_bdaddr_type; 134 135 if (la.l2_cid) 136 err = l2cap_add_scid(chan, __le16_to_cpu(la.l2_cid)); 137 else 138 err = l2cap_add_psm(chan, &la.l2_bdaddr, la.l2_psm); 139 140 if (err < 0) 141 goto done; 142 143 switch (chan->chan_type) { 144 case L2CAP_CHAN_CONN_LESS: 145 if (__le16_to_cpu(la.l2_psm) == L2CAP_PSM_3DSP) 146 chan->sec_level = BT_SECURITY_SDP; 147 break; 148 case L2CAP_CHAN_CONN_ORIENTED: 149 if (__le16_to_cpu(la.l2_psm) == L2CAP_PSM_SDP || 150 __le16_to_cpu(la.l2_psm) == L2CAP_PSM_RFCOMM) 151 chan->sec_level = BT_SECURITY_SDP; 152 break; 153 case L2CAP_CHAN_RAW: 154 chan->sec_level = BT_SECURITY_SDP; 155 break; 156 case L2CAP_CHAN_FIXED: 157 /* Fixed channels default to the L2CAP core not holding a 158 * hci_conn reference for them. For fixed channels mapping to 159 * L2CAP sockets we do want to hold a reference so set the 160 * appropriate flag to request it. 161 */ 162 set_bit(FLAG_HOLD_HCI_CONN, &chan->flags); 163 break; 164 } 165 166 /* Use L2CAP_MODE_LE_FLOWCTL (CoC) in case of LE address and 167 * L2CAP_MODE_EXT_FLOWCTL (ECRED) has not been set. 168 */ 169 if (chan->psm && bdaddr_type_is_le(chan->src_type) && 170 chan->mode != L2CAP_MODE_EXT_FLOWCTL) 171 chan->mode = L2CAP_MODE_LE_FLOWCTL; 172 173 chan->state = BT_BOUND; 174 sk->sk_state = BT_BOUND; 175 176 done: 177 release_sock(sk); 178 return err; 179 } 180 181 static int l2cap_sock_connect(struct socket *sock, struct sockaddr *addr, 182 int alen, int flags) 183 { 184 struct sock *sk = sock->sk; 185 struct l2cap_chan *chan = l2cap_pi(sk)->chan; 186 struct sockaddr_l2 la; 187 int len, err = 0; 188 bool zapped; 189 190 BT_DBG("sk %p", sk); 191 192 lock_sock(sk); 193 zapped = sock_flag(sk, SOCK_ZAPPED); 194 release_sock(sk); 195 196 if (zapped) 197 return -EINVAL; 198 199 if (!addr || alen < offsetofend(struct sockaddr, sa_family) || 200 addr->sa_family != AF_BLUETOOTH) 201 return -EINVAL; 202 203 memset(&la, 0, sizeof(la)); 204 len = min_t(unsigned int, sizeof(la), alen); 205 memcpy(&la, addr, len); 206 207 if (la.l2_cid && la.l2_psm) 208 return -EINVAL; 209 210 if (!bdaddr_type_is_valid(la.l2_bdaddr_type)) 211 return -EINVAL; 212 213 /* Check that the socket wasn't bound to something that 214 * conflicts with the address given to connect(). If chan->src 215 * is BDADDR_ANY it means bind() was never used, in which case 216 * chan->src_type and la.l2_bdaddr_type do not need to match. 217 */ 218 if (chan->src_type == BDADDR_BREDR && bacmp(&chan->src, BDADDR_ANY) && 219 bdaddr_type_is_le(la.l2_bdaddr_type)) { 220 /* Old user space versions will try to incorrectly bind 221 * the ATT socket using BDADDR_BREDR. We need to accept 222 * this and fix up the source address type only when 223 * both the source CID and destination CID indicate 224 * ATT. Anything else is an invalid combination. 225 */ 226 if (chan->scid != L2CAP_CID_ATT || 227 la.l2_cid != cpu_to_le16(L2CAP_CID_ATT)) 228 return -EINVAL; 229 230 /* We don't have the hdev available here to make a 231 * better decision on random vs public, but since all 232 * user space versions that exhibit this issue anyway do 233 * not support random local addresses assuming public 234 * here is good enough. 235 */ 236 chan->src_type = BDADDR_LE_PUBLIC; 237 } 238 239 if (chan->src_type != BDADDR_BREDR && la.l2_bdaddr_type == BDADDR_BREDR) 240 return -EINVAL; 241 242 if (bdaddr_type_is_le(la.l2_bdaddr_type)) { 243 /* We only allow ATT user space socket */ 244 if (la.l2_cid && 245 la.l2_cid != cpu_to_le16(L2CAP_CID_ATT)) 246 return -EINVAL; 247 } 248 249 /* Use L2CAP_MODE_LE_FLOWCTL (CoC) in case of LE address and 250 * L2CAP_MODE_EXT_FLOWCTL (ECRED) has not been set. 251 */ 252 if (chan->psm && bdaddr_type_is_le(chan->src_type) && 253 chan->mode != L2CAP_MODE_EXT_FLOWCTL) 254 chan->mode = L2CAP_MODE_LE_FLOWCTL; 255 256 err = l2cap_chan_connect(chan, la.l2_psm, __le16_to_cpu(la.l2_cid), 257 &la.l2_bdaddr, la.l2_bdaddr_type, 258 sk->sk_sndtimeo); 259 if (err) 260 return err; 261 262 lock_sock(sk); 263 264 err = bt_sock_wait_state(sk, BT_CONNECTED, 265 sock_sndtimeo(sk, flags & O_NONBLOCK)); 266 267 release_sock(sk); 268 269 return err; 270 } 271 272 static int l2cap_sock_listen(struct socket *sock, int backlog) 273 { 274 struct sock *sk = sock->sk; 275 struct l2cap_chan *chan = l2cap_pi(sk)->chan; 276 int err = 0; 277 278 BT_DBG("sk %p backlog %d", sk, backlog); 279 280 lock_sock(sk); 281 282 if (sk->sk_state != BT_BOUND) { 283 err = -EBADFD; 284 goto done; 285 } 286 287 if (sk->sk_type != SOCK_SEQPACKET && sk->sk_type != SOCK_STREAM) { 288 err = -EINVAL; 289 goto done; 290 } 291 292 switch (chan->mode) { 293 case L2CAP_MODE_BASIC: 294 case L2CAP_MODE_LE_FLOWCTL: 295 break; 296 case L2CAP_MODE_EXT_FLOWCTL: 297 if (!enable_ecred) { 298 err = -EOPNOTSUPP; 299 goto done; 300 } 301 break; 302 case L2CAP_MODE_ERTM: 303 case L2CAP_MODE_STREAMING: 304 if (!disable_ertm) 305 break; 306 fallthrough; 307 default: 308 err = -EOPNOTSUPP; 309 goto done; 310 } 311 312 sk->sk_max_ack_backlog = backlog; 313 sk->sk_ack_backlog = 0; 314 315 /* Listening channels need to use nested locking in order not to 316 * cause lockdep warnings when the created child channels end up 317 * being locked in the same thread as the parent channel. 318 */ 319 atomic_set(&chan->nesting, L2CAP_NESTING_PARENT); 320 321 chan->state = BT_LISTEN; 322 sk->sk_state = BT_LISTEN; 323 324 done: 325 release_sock(sk); 326 return err; 327 } 328 329 static int l2cap_sock_accept(struct socket *sock, struct socket *newsock, 330 int flags, bool kern) 331 { 332 DEFINE_WAIT_FUNC(wait, woken_wake_function); 333 struct sock *sk = sock->sk, *nsk; 334 long timeo; 335 int err = 0; 336 337 lock_sock_nested(sk, L2CAP_NESTING_PARENT); 338 339 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK); 340 341 BT_DBG("sk %p timeo %ld", sk, timeo); 342 343 /* Wait for an incoming connection. (wake-one). */ 344 add_wait_queue_exclusive(sk_sleep(sk), &wait); 345 while (1) { 346 if (sk->sk_state != BT_LISTEN) { 347 err = -EBADFD; 348 break; 349 } 350 351 nsk = bt_accept_dequeue(sk, newsock); 352 if (nsk) 353 break; 354 355 if (!timeo) { 356 err = -EAGAIN; 357 break; 358 } 359 360 if (signal_pending(current)) { 361 err = sock_intr_errno(timeo); 362 break; 363 } 364 365 release_sock(sk); 366 367 timeo = wait_woken(&wait, TASK_INTERRUPTIBLE, timeo); 368 369 lock_sock_nested(sk, L2CAP_NESTING_PARENT); 370 } 371 remove_wait_queue(sk_sleep(sk), &wait); 372 373 if (err) 374 goto done; 375 376 newsock->state = SS_CONNECTED; 377 378 BT_DBG("new socket %p", nsk); 379 380 done: 381 release_sock(sk); 382 return err; 383 } 384 385 static int l2cap_sock_getname(struct socket *sock, struct sockaddr *addr, 386 int peer) 387 { 388 struct sockaddr_l2 *la = (struct sockaddr_l2 *) addr; 389 struct sock *sk = sock->sk; 390 struct l2cap_chan *chan = l2cap_pi(sk)->chan; 391 392 BT_DBG("sock %p, sk %p", sock, sk); 393 394 if (peer && sk->sk_state != BT_CONNECTED && 395 sk->sk_state != BT_CONNECT && sk->sk_state != BT_CONNECT2 && 396 sk->sk_state != BT_CONFIG) 397 return -ENOTCONN; 398 399 memset(la, 0, sizeof(struct sockaddr_l2)); 400 addr->sa_family = AF_BLUETOOTH; 401 402 la->l2_psm = chan->psm; 403 404 if (peer) { 405 bacpy(&la->l2_bdaddr, &chan->dst); 406 la->l2_cid = cpu_to_le16(chan->dcid); 407 la->l2_bdaddr_type = chan->dst_type; 408 } else { 409 bacpy(&la->l2_bdaddr, &chan->src); 410 la->l2_cid = cpu_to_le16(chan->scid); 411 la->l2_bdaddr_type = chan->src_type; 412 } 413 414 return sizeof(struct sockaddr_l2); 415 } 416 417 static int l2cap_get_mode(struct l2cap_chan *chan) 418 { 419 switch (chan->mode) { 420 case L2CAP_MODE_BASIC: 421 return BT_MODE_BASIC; 422 case L2CAP_MODE_ERTM: 423 return BT_MODE_ERTM; 424 case L2CAP_MODE_STREAMING: 425 return BT_MODE_STREAMING; 426 case L2CAP_MODE_LE_FLOWCTL: 427 return BT_MODE_LE_FLOWCTL; 428 case L2CAP_MODE_EXT_FLOWCTL: 429 return BT_MODE_EXT_FLOWCTL; 430 } 431 432 return -EINVAL; 433 } 434 435 static int l2cap_sock_getsockopt_old(struct socket *sock, int optname, 436 char __user *optval, int __user *optlen) 437 { 438 struct sock *sk = sock->sk; 439 struct l2cap_chan *chan = l2cap_pi(sk)->chan; 440 struct l2cap_options opts; 441 struct l2cap_conninfo cinfo; 442 int len, err = 0; 443 u32 opt; 444 445 BT_DBG("sk %p", sk); 446 447 if (get_user(len, optlen)) 448 return -EFAULT; 449 450 lock_sock(sk); 451 452 switch (optname) { 453 case L2CAP_OPTIONS: 454 /* LE sockets should use BT_SNDMTU/BT_RCVMTU, but since 455 * legacy ATT code depends on getsockopt for 456 * L2CAP_OPTIONS we need to let this pass. 457 */ 458 if (bdaddr_type_is_le(chan->src_type) && 459 chan->scid != L2CAP_CID_ATT) { 460 err = -EINVAL; 461 break; 462 } 463 464 /* Only BR/EDR modes are supported here */ 465 switch (chan->mode) { 466 case L2CAP_MODE_BASIC: 467 case L2CAP_MODE_ERTM: 468 case L2CAP_MODE_STREAMING: 469 break; 470 default: 471 err = -EINVAL; 472 break; 473 } 474 475 if (err < 0) 476 break; 477 478 memset(&opts, 0, sizeof(opts)); 479 opts.imtu = chan->imtu; 480 opts.omtu = chan->omtu; 481 opts.flush_to = chan->flush_to; 482 opts.mode = chan->mode; 483 opts.fcs = chan->fcs; 484 opts.max_tx = chan->max_tx; 485 opts.txwin_size = chan->tx_win; 486 487 BT_DBG("mode 0x%2.2x", chan->mode); 488 489 len = min_t(unsigned int, len, sizeof(opts)); 490 if (copy_to_user(optval, (char *) &opts, len)) 491 err = -EFAULT; 492 493 break; 494 495 case L2CAP_LM: 496 switch (chan->sec_level) { 497 case BT_SECURITY_LOW: 498 opt = L2CAP_LM_AUTH; 499 break; 500 case BT_SECURITY_MEDIUM: 501 opt = L2CAP_LM_AUTH | L2CAP_LM_ENCRYPT; 502 break; 503 case BT_SECURITY_HIGH: 504 opt = L2CAP_LM_AUTH | L2CAP_LM_ENCRYPT | 505 L2CAP_LM_SECURE; 506 break; 507 case BT_SECURITY_FIPS: 508 opt = L2CAP_LM_AUTH | L2CAP_LM_ENCRYPT | 509 L2CAP_LM_SECURE | L2CAP_LM_FIPS; 510 break; 511 default: 512 opt = 0; 513 break; 514 } 515 516 if (test_bit(FLAG_ROLE_SWITCH, &chan->flags)) 517 opt |= L2CAP_LM_MASTER; 518 519 if (test_bit(FLAG_FORCE_RELIABLE, &chan->flags)) 520 opt |= L2CAP_LM_RELIABLE; 521 522 if (put_user(opt, (u32 __user *) optval)) 523 err = -EFAULT; 524 525 break; 526 527 case L2CAP_CONNINFO: 528 if (sk->sk_state != BT_CONNECTED && 529 !(sk->sk_state == BT_CONNECT2 && 530 test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))) { 531 err = -ENOTCONN; 532 break; 533 } 534 535 memset(&cinfo, 0, sizeof(cinfo)); 536 cinfo.hci_handle = chan->conn->hcon->handle; 537 memcpy(cinfo.dev_class, chan->conn->hcon->dev_class, 3); 538 539 len = min_t(unsigned int, len, sizeof(cinfo)); 540 if (copy_to_user(optval, (char *) &cinfo, len)) 541 err = -EFAULT; 542 543 break; 544 545 default: 546 err = -ENOPROTOOPT; 547 break; 548 } 549 550 release_sock(sk); 551 return err; 552 } 553 554 static int l2cap_sock_getsockopt(struct socket *sock, int level, int optname, 555 char __user *optval, int __user *optlen) 556 { 557 struct sock *sk = sock->sk; 558 struct l2cap_chan *chan = l2cap_pi(sk)->chan; 559 struct bt_security sec; 560 struct bt_power pwr; 561 u32 phys; 562 int len, mode, err = 0; 563 564 BT_DBG("sk %p", sk); 565 566 if (level == SOL_L2CAP) 567 return l2cap_sock_getsockopt_old(sock, optname, optval, optlen); 568 569 if (level != SOL_BLUETOOTH) 570 return -ENOPROTOOPT; 571 572 if (get_user(len, optlen)) 573 return -EFAULT; 574 575 lock_sock(sk); 576 577 switch (optname) { 578 case BT_SECURITY: 579 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED && 580 chan->chan_type != L2CAP_CHAN_FIXED && 581 chan->chan_type != L2CAP_CHAN_RAW) { 582 err = -EINVAL; 583 break; 584 } 585 586 memset(&sec, 0, sizeof(sec)); 587 if (chan->conn) { 588 sec.level = chan->conn->hcon->sec_level; 589 590 if (sk->sk_state == BT_CONNECTED) 591 sec.key_size = chan->conn->hcon->enc_key_size; 592 } else { 593 sec.level = chan->sec_level; 594 } 595 596 len = min_t(unsigned int, len, sizeof(sec)); 597 if (copy_to_user(optval, (char *) &sec, len)) 598 err = -EFAULT; 599 600 break; 601 602 case BT_DEFER_SETUP: 603 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) { 604 err = -EINVAL; 605 break; 606 } 607 608 if (put_user(test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags), 609 (u32 __user *) optval)) 610 err = -EFAULT; 611 612 break; 613 614 case BT_FLUSHABLE: 615 if (put_user(test_bit(FLAG_FLUSHABLE, &chan->flags), 616 (u32 __user *) optval)) 617 err = -EFAULT; 618 619 break; 620 621 case BT_POWER: 622 if (sk->sk_type != SOCK_SEQPACKET && sk->sk_type != SOCK_STREAM 623 && sk->sk_type != SOCK_RAW) { 624 err = -EINVAL; 625 break; 626 } 627 628 pwr.force_active = test_bit(FLAG_FORCE_ACTIVE, &chan->flags); 629 630 len = min_t(unsigned int, len, sizeof(pwr)); 631 if (copy_to_user(optval, (char *) &pwr, len)) 632 err = -EFAULT; 633 634 break; 635 636 case BT_CHANNEL_POLICY: 637 if (put_user(chan->chan_policy, (u32 __user *) optval)) 638 err = -EFAULT; 639 break; 640 641 case BT_SNDMTU: 642 if (!bdaddr_type_is_le(chan->src_type)) { 643 err = -EINVAL; 644 break; 645 } 646 647 if (sk->sk_state != BT_CONNECTED) { 648 err = -ENOTCONN; 649 break; 650 } 651 652 if (put_user(chan->omtu, (u16 __user *) optval)) 653 err = -EFAULT; 654 break; 655 656 case BT_RCVMTU: 657 if (!bdaddr_type_is_le(chan->src_type)) { 658 err = -EINVAL; 659 break; 660 } 661 662 if (put_user(chan->imtu, (u16 __user *) optval)) 663 err = -EFAULT; 664 break; 665 666 case BT_PHY: 667 if (sk->sk_state != BT_CONNECTED) { 668 err = -ENOTCONN; 669 break; 670 } 671 672 phys = hci_conn_get_phy(chan->conn->hcon); 673 674 if (put_user(phys, (u32 __user *) optval)) 675 err = -EFAULT; 676 break; 677 678 case BT_MODE: 679 if (!enable_ecred) { 680 err = -ENOPROTOOPT; 681 break; 682 } 683 684 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { 685 err = -EINVAL; 686 break; 687 } 688 689 mode = l2cap_get_mode(chan); 690 if (mode < 0) { 691 err = mode; 692 break; 693 } 694 695 if (put_user(mode, (u8 __user *) optval)) 696 err = -EFAULT; 697 break; 698 699 default: 700 err = -ENOPROTOOPT; 701 break; 702 } 703 704 release_sock(sk); 705 return err; 706 } 707 708 static bool l2cap_valid_mtu(struct l2cap_chan *chan, u16 mtu) 709 { 710 switch (chan->scid) { 711 case L2CAP_CID_ATT: 712 if (mtu < L2CAP_LE_MIN_MTU) 713 return false; 714 break; 715 716 default: 717 if (mtu < L2CAP_DEFAULT_MIN_MTU) 718 return false; 719 } 720 721 return true; 722 } 723 724 static int l2cap_sock_setsockopt_old(struct socket *sock, int optname, 725 sockptr_t optval, unsigned int optlen) 726 { 727 struct sock *sk = sock->sk; 728 struct l2cap_chan *chan = l2cap_pi(sk)->chan; 729 struct l2cap_options opts; 730 int err = 0; 731 u32 opt; 732 733 BT_DBG("sk %p", sk); 734 735 lock_sock(sk); 736 737 switch (optname) { 738 case L2CAP_OPTIONS: 739 if (bdaddr_type_is_le(chan->src_type)) { 740 err = -EINVAL; 741 break; 742 } 743 744 if (sk->sk_state == BT_CONNECTED) { 745 err = -EINVAL; 746 break; 747 } 748 749 opts.imtu = chan->imtu; 750 opts.omtu = chan->omtu; 751 opts.flush_to = chan->flush_to; 752 opts.mode = chan->mode; 753 opts.fcs = chan->fcs; 754 opts.max_tx = chan->max_tx; 755 opts.txwin_size = chan->tx_win; 756 757 err = bt_copy_from_sockptr(&opts, sizeof(opts), optval, optlen); 758 if (err) 759 break; 760 761 if (opts.txwin_size > L2CAP_DEFAULT_EXT_WINDOW) { 762 err = -EINVAL; 763 break; 764 } 765 766 if (!l2cap_valid_mtu(chan, opts.imtu)) { 767 err = -EINVAL; 768 break; 769 } 770 771 /* Only BR/EDR modes are supported here */ 772 switch (opts.mode) { 773 case L2CAP_MODE_BASIC: 774 clear_bit(CONF_STATE2_DEVICE, &chan->conf_state); 775 break; 776 case L2CAP_MODE_ERTM: 777 case L2CAP_MODE_STREAMING: 778 if (!disable_ertm) 779 break; 780 fallthrough; 781 default: 782 err = -EINVAL; 783 break; 784 } 785 786 if (err < 0) 787 break; 788 789 chan->mode = opts.mode; 790 791 BT_DBG("mode 0x%2.2x", chan->mode); 792 793 chan->imtu = opts.imtu; 794 chan->omtu = opts.omtu; 795 chan->fcs = opts.fcs; 796 chan->max_tx = opts.max_tx; 797 chan->tx_win = opts.txwin_size; 798 chan->flush_to = opts.flush_to; 799 break; 800 801 case L2CAP_LM: 802 err = bt_copy_from_sockptr(&opt, sizeof(opt), optval, optlen); 803 if (err) 804 break; 805 806 if (opt & L2CAP_LM_FIPS) { 807 err = -EINVAL; 808 break; 809 } 810 811 if (opt & L2CAP_LM_AUTH) 812 chan->sec_level = BT_SECURITY_LOW; 813 if (opt & L2CAP_LM_ENCRYPT) 814 chan->sec_level = BT_SECURITY_MEDIUM; 815 if (opt & L2CAP_LM_SECURE) 816 chan->sec_level = BT_SECURITY_HIGH; 817 818 if (opt & L2CAP_LM_MASTER) 819 set_bit(FLAG_ROLE_SWITCH, &chan->flags); 820 else 821 clear_bit(FLAG_ROLE_SWITCH, &chan->flags); 822 823 if (opt & L2CAP_LM_RELIABLE) 824 set_bit(FLAG_FORCE_RELIABLE, &chan->flags); 825 else 826 clear_bit(FLAG_FORCE_RELIABLE, &chan->flags); 827 break; 828 829 default: 830 err = -ENOPROTOOPT; 831 break; 832 } 833 834 release_sock(sk); 835 return err; 836 } 837 838 static int l2cap_set_mode(struct l2cap_chan *chan, u8 mode) 839 { 840 switch (mode) { 841 case BT_MODE_BASIC: 842 if (bdaddr_type_is_le(chan->src_type)) 843 return -EINVAL; 844 mode = L2CAP_MODE_BASIC; 845 clear_bit(CONF_STATE2_DEVICE, &chan->conf_state); 846 break; 847 case BT_MODE_ERTM: 848 if (!disable_ertm || bdaddr_type_is_le(chan->src_type)) 849 return -EINVAL; 850 mode = L2CAP_MODE_ERTM; 851 break; 852 case BT_MODE_STREAMING: 853 if (!disable_ertm || bdaddr_type_is_le(chan->src_type)) 854 return -EINVAL; 855 mode = L2CAP_MODE_STREAMING; 856 break; 857 case BT_MODE_LE_FLOWCTL: 858 if (!bdaddr_type_is_le(chan->src_type)) 859 return -EINVAL; 860 mode = L2CAP_MODE_LE_FLOWCTL; 861 break; 862 case BT_MODE_EXT_FLOWCTL: 863 /* TODO: Add support for ECRED PDUs to BR/EDR */ 864 if (!bdaddr_type_is_le(chan->src_type)) 865 return -EINVAL; 866 mode = L2CAP_MODE_EXT_FLOWCTL; 867 break; 868 default: 869 return -EINVAL; 870 } 871 872 chan->mode = mode; 873 874 return 0; 875 } 876 877 static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname, 878 sockptr_t optval, unsigned int optlen) 879 { 880 struct sock *sk = sock->sk; 881 struct l2cap_chan *chan = l2cap_pi(sk)->chan; 882 struct bt_security sec; 883 struct bt_power pwr; 884 struct l2cap_conn *conn; 885 int err = 0; 886 u32 opt; 887 u16 mtu; 888 u8 mode; 889 890 BT_DBG("sk %p", sk); 891 892 if (level == SOL_L2CAP) 893 return l2cap_sock_setsockopt_old(sock, optname, optval, optlen); 894 895 if (level != SOL_BLUETOOTH) 896 return -ENOPROTOOPT; 897 898 lock_sock(sk); 899 900 switch (optname) { 901 case BT_SECURITY: 902 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED && 903 chan->chan_type != L2CAP_CHAN_FIXED && 904 chan->chan_type != L2CAP_CHAN_RAW) { 905 err = -EINVAL; 906 break; 907 } 908 909 sec.level = BT_SECURITY_LOW; 910 911 err = bt_copy_from_sockptr(&sec, sizeof(sec), optval, optlen); 912 if (err) 913 break; 914 915 if (sec.level < BT_SECURITY_LOW || 916 sec.level > BT_SECURITY_FIPS) { 917 err = -EINVAL; 918 break; 919 } 920 921 chan->sec_level = sec.level; 922 923 if (!chan->conn) 924 break; 925 926 conn = chan->conn; 927 928 /* change security for LE channels */ 929 if (chan->scid == L2CAP_CID_ATT) { 930 if (smp_conn_security(conn->hcon, sec.level)) { 931 err = -EINVAL; 932 break; 933 } 934 935 set_bit(FLAG_PENDING_SECURITY, &chan->flags); 936 sk->sk_state = BT_CONFIG; 937 chan->state = BT_CONFIG; 938 939 /* or for ACL link */ 940 } else if ((sk->sk_state == BT_CONNECT2 && 941 test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) || 942 sk->sk_state == BT_CONNECTED) { 943 if (!l2cap_chan_check_security(chan, true)) 944 set_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags); 945 else 946 sk->sk_state_change(sk); 947 } else { 948 err = -EINVAL; 949 } 950 break; 951 952 case BT_DEFER_SETUP: 953 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) { 954 err = -EINVAL; 955 break; 956 } 957 958 err = bt_copy_from_sockptr(&opt, sizeof(opt), optval, optlen); 959 if (err) 960 break; 961 962 if (opt) { 963 set_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags); 964 set_bit(FLAG_DEFER_SETUP, &chan->flags); 965 } else { 966 clear_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags); 967 clear_bit(FLAG_DEFER_SETUP, &chan->flags); 968 } 969 break; 970 971 case BT_FLUSHABLE: 972 err = bt_copy_from_sockptr(&opt, sizeof(opt), optval, optlen); 973 if (err) 974 break; 975 976 if (opt > BT_FLUSHABLE_ON) { 977 err = -EINVAL; 978 break; 979 } 980 981 if (opt == BT_FLUSHABLE_OFF) { 982 conn = chan->conn; 983 /* proceed further only when we have l2cap_conn and 984 No Flush support in the LM */ 985 if (!conn || !lmp_no_flush_capable(conn->hcon->hdev)) { 986 err = -EINVAL; 987 break; 988 } 989 } 990 991 if (opt) 992 set_bit(FLAG_FLUSHABLE, &chan->flags); 993 else 994 clear_bit(FLAG_FLUSHABLE, &chan->flags); 995 break; 996 997 case BT_POWER: 998 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED && 999 chan->chan_type != L2CAP_CHAN_RAW) { 1000 err = -EINVAL; 1001 break; 1002 } 1003 1004 pwr.force_active = BT_POWER_FORCE_ACTIVE_ON; 1005 1006 err = bt_copy_from_sockptr(&pwr, sizeof(pwr), optval, optlen); 1007 if (err) 1008 break; 1009 1010 if (pwr.force_active) 1011 set_bit(FLAG_FORCE_ACTIVE, &chan->flags); 1012 else 1013 clear_bit(FLAG_FORCE_ACTIVE, &chan->flags); 1014 break; 1015 1016 case BT_CHANNEL_POLICY: 1017 err = bt_copy_from_sockptr(&opt, sizeof(opt), optval, optlen); 1018 if (err) 1019 break; 1020 1021 err = -EOPNOTSUPP; 1022 break; 1023 1024 case BT_SNDMTU: 1025 if (!bdaddr_type_is_le(chan->src_type)) { 1026 err = -EINVAL; 1027 break; 1028 } 1029 1030 /* Setting is not supported as it's the remote side that 1031 * decides this. 1032 */ 1033 err = -EPERM; 1034 break; 1035 1036 case BT_RCVMTU: 1037 if (!bdaddr_type_is_le(chan->src_type)) { 1038 err = -EINVAL; 1039 break; 1040 } 1041 1042 if (chan->mode == L2CAP_MODE_LE_FLOWCTL && 1043 sk->sk_state == BT_CONNECTED) { 1044 err = -EISCONN; 1045 break; 1046 } 1047 1048 err = bt_copy_from_sockptr(&mtu, sizeof(mtu), optval, optlen); 1049 if (err) 1050 break; 1051 1052 if (chan->mode == L2CAP_MODE_EXT_FLOWCTL && 1053 sk->sk_state == BT_CONNECTED) 1054 err = l2cap_chan_reconfigure(chan, mtu); 1055 else 1056 chan->imtu = mtu; 1057 1058 break; 1059 1060 case BT_MODE: 1061 if (!enable_ecred) { 1062 err = -ENOPROTOOPT; 1063 break; 1064 } 1065 1066 BT_DBG("sk->sk_state %u", sk->sk_state); 1067 1068 if (sk->sk_state != BT_BOUND) { 1069 err = -EINVAL; 1070 break; 1071 } 1072 1073 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { 1074 err = -EINVAL; 1075 break; 1076 } 1077 1078 err = bt_copy_from_sockptr(&mode, sizeof(mode), optval, optlen); 1079 if (err) 1080 break; 1081 1082 BT_DBG("mode %u", mode); 1083 1084 err = l2cap_set_mode(chan, mode); 1085 if (err) 1086 break; 1087 1088 BT_DBG("mode 0x%2.2x", chan->mode); 1089 1090 break; 1091 1092 default: 1093 err = -ENOPROTOOPT; 1094 break; 1095 } 1096 1097 release_sock(sk); 1098 return err; 1099 } 1100 1101 static int l2cap_sock_sendmsg(struct socket *sock, struct msghdr *msg, 1102 size_t len) 1103 { 1104 struct sock *sk = sock->sk; 1105 struct l2cap_chan *chan = l2cap_pi(sk)->chan; 1106 int err; 1107 1108 BT_DBG("sock %p, sk %p", sock, sk); 1109 1110 err = sock_error(sk); 1111 if (err) 1112 return err; 1113 1114 if (msg->msg_flags & MSG_OOB) 1115 return -EOPNOTSUPP; 1116 1117 if (sk->sk_state != BT_CONNECTED) 1118 return -ENOTCONN; 1119 1120 lock_sock(sk); 1121 err = bt_sock_wait_ready(sk, msg->msg_flags); 1122 release_sock(sk); 1123 if (err) 1124 return err; 1125 1126 l2cap_chan_lock(chan); 1127 err = l2cap_chan_send(chan, msg, len); 1128 l2cap_chan_unlock(chan); 1129 1130 return err; 1131 } 1132 1133 static int l2cap_sock_recvmsg(struct socket *sock, struct msghdr *msg, 1134 size_t len, int flags) 1135 { 1136 struct sock *sk = sock->sk; 1137 struct l2cap_pinfo *pi = l2cap_pi(sk); 1138 int err; 1139 1140 lock_sock(sk); 1141 1142 if (sk->sk_state == BT_CONNECT2 && test_bit(BT_SK_DEFER_SETUP, 1143 &bt_sk(sk)->flags)) { 1144 if (pi->chan->mode == L2CAP_MODE_EXT_FLOWCTL) { 1145 sk->sk_state = BT_CONNECTED; 1146 pi->chan->state = BT_CONNECTED; 1147 __l2cap_ecred_conn_rsp_defer(pi->chan); 1148 } else if (bdaddr_type_is_le(pi->chan->src_type)) { 1149 sk->sk_state = BT_CONNECTED; 1150 pi->chan->state = BT_CONNECTED; 1151 __l2cap_le_connect_rsp_defer(pi->chan); 1152 } else { 1153 sk->sk_state = BT_CONFIG; 1154 pi->chan->state = BT_CONFIG; 1155 __l2cap_connect_rsp_defer(pi->chan); 1156 } 1157 1158 err = 0; 1159 goto done; 1160 } 1161 1162 release_sock(sk); 1163 1164 if (sock->type == SOCK_STREAM) 1165 err = bt_sock_stream_recvmsg(sock, msg, len, flags); 1166 else 1167 err = bt_sock_recvmsg(sock, msg, len, flags); 1168 1169 if (pi->chan->mode != L2CAP_MODE_ERTM) 1170 return err; 1171 1172 /* Attempt to put pending rx data in the socket buffer */ 1173 1174 lock_sock(sk); 1175 1176 if (!test_bit(CONN_LOCAL_BUSY, &pi->chan->conn_state)) 1177 goto done; 1178 1179 if (pi->rx_busy_skb) { 1180 if (!__sock_queue_rcv_skb(sk, pi->rx_busy_skb)) 1181 pi->rx_busy_skb = NULL; 1182 else 1183 goto done; 1184 } 1185 1186 /* Restore data flow when half of the receive buffer is 1187 * available. This avoids resending large numbers of 1188 * frames. 1189 */ 1190 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf >> 1) 1191 l2cap_chan_busy(pi->chan, 0); 1192 1193 done: 1194 release_sock(sk); 1195 return err; 1196 } 1197 1198 /* Kill socket (only if zapped and orphan) 1199 * Must be called on unlocked socket, with l2cap channel lock. 1200 */ 1201 static void l2cap_sock_kill(struct sock *sk) 1202 { 1203 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket) 1204 return; 1205 1206 BT_DBG("sk %p state %s", sk, state_to_string(sk->sk_state)); 1207 1208 /* Kill poor orphan */ 1209 1210 l2cap_chan_put(l2cap_pi(sk)->chan); 1211 sock_set_flag(sk, SOCK_DEAD); 1212 sock_put(sk); 1213 } 1214 1215 static int __l2cap_wait_ack(struct sock *sk, struct l2cap_chan *chan) 1216 { 1217 DECLARE_WAITQUEUE(wait, current); 1218 int err = 0; 1219 int timeo = L2CAP_WAIT_ACK_POLL_PERIOD; 1220 /* Timeout to prevent infinite loop */ 1221 unsigned long timeout = jiffies + L2CAP_WAIT_ACK_TIMEOUT; 1222 1223 add_wait_queue(sk_sleep(sk), &wait); 1224 set_current_state(TASK_INTERRUPTIBLE); 1225 do { 1226 BT_DBG("Waiting for %d ACKs, timeout %04d ms", 1227 chan->unacked_frames, time_after(jiffies, timeout) ? 0 : 1228 jiffies_to_msecs(timeout - jiffies)); 1229 1230 if (!timeo) 1231 timeo = L2CAP_WAIT_ACK_POLL_PERIOD; 1232 1233 if (signal_pending(current)) { 1234 err = sock_intr_errno(timeo); 1235 break; 1236 } 1237 1238 release_sock(sk); 1239 timeo = schedule_timeout(timeo); 1240 lock_sock(sk); 1241 set_current_state(TASK_INTERRUPTIBLE); 1242 1243 err = sock_error(sk); 1244 if (err) 1245 break; 1246 1247 if (time_after(jiffies, timeout)) { 1248 err = -ENOLINK; 1249 break; 1250 } 1251 1252 } while (chan->unacked_frames > 0 && 1253 chan->state == BT_CONNECTED); 1254 1255 set_current_state(TASK_RUNNING); 1256 remove_wait_queue(sk_sleep(sk), &wait); 1257 return err; 1258 } 1259 1260 static int l2cap_sock_shutdown(struct socket *sock, int how) 1261 { 1262 struct sock *sk = sock->sk; 1263 struct l2cap_chan *chan; 1264 struct l2cap_conn *conn; 1265 int err = 0; 1266 1267 BT_DBG("sock %p, sk %p, how %d", sock, sk, how); 1268 1269 /* 'how' parameter is mapped to sk_shutdown as follows: 1270 * SHUT_RD (0) --> RCV_SHUTDOWN (1) 1271 * SHUT_WR (1) --> SEND_SHUTDOWN (2) 1272 * SHUT_RDWR (2) --> SHUTDOWN_MASK (3) 1273 */ 1274 how++; 1275 1276 if (!sk) 1277 return 0; 1278 1279 lock_sock(sk); 1280 1281 if ((sk->sk_shutdown & how) == how) 1282 goto shutdown_already; 1283 1284 BT_DBG("Handling sock shutdown"); 1285 1286 /* prevent sk structure from being freed whilst unlocked */ 1287 sock_hold(sk); 1288 1289 chan = l2cap_pi(sk)->chan; 1290 /* prevent chan structure from being freed whilst unlocked */ 1291 l2cap_chan_hold(chan); 1292 1293 BT_DBG("chan %p state %s", chan, state_to_string(chan->state)); 1294 1295 if (chan->mode == L2CAP_MODE_ERTM && 1296 chan->unacked_frames > 0 && 1297 chan->state == BT_CONNECTED) { 1298 err = __l2cap_wait_ack(sk, chan); 1299 1300 /* After waiting for ACKs, check whether shutdown 1301 * has already been actioned to close the L2CAP 1302 * link such as by l2cap_disconnection_req(). 1303 */ 1304 if ((sk->sk_shutdown & how) == how) 1305 goto shutdown_matched; 1306 } 1307 1308 /* Try setting the RCV_SHUTDOWN bit, return early if SEND_SHUTDOWN 1309 * is already set 1310 */ 1311 if ((how & RCV_SHUTDOWN) && !(sk->sk_shutdown & RCV_SHUTDOWN)) { 1312 sk->sk_shutdown |= RCV_SHUTDOWN; 1313 if ((sk->sk_shutdown & how) == how) 1314 goto shutdown_matched; 1315 } 1316 1317 sk->sk_shutdown |= SEND_SHUTDOWN; 1318 release_sock(sk); 1319 1320 l2cap_chan_lock(chan); 1321 conn = chan->conn; 1322 if (conn) 1323 /* prevent conn structure from being freed */ 1324 l2cap_conn_get(conn); 1325 l2cap_chan_unlock(chan); 1326 1327 if (conn) 1328 /* mutex lock must be taken before l2cap_chan_lock() */ 1329 mutex_lock(&conn->chan_lock); 1330 1331 l2cap_chan_lock(chan); 1332 l2cap_chan_close(chan, 0); 1333 l2cap_chan_unlock(chan); 1334 1335 if (conn) { 1336 mutex_unlock(&conn->chan_lock); 1337 l2cap_conn_put(conn); 1338 } 1339 1340 lock_sock(sk); 1341 1342 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime && 1343 !(current->flags & PF_EXITING)) 1344 err = bt_sock_wait_state(sk, BT_CLOSED, 1345 sk->sk_lingertime); 1346 1347 shutdown_matched: 1348 l2cap_chan_put(chan); 1349 sock_put(sk); 1350 1351 shutdown_already: 1352 if (!err && sk->sk_err) 1353 err = -sk->sk_err; 1354 1355 release_sock(sk); 1356 1357 BT_DBG("Sock shutdown complete err: %d", err); 1358 1359 return err; 1360 } 1361 1362 static int l2cap_sock_release(struct socket *sock) 1363 { 1364 struct sock *sk = sock->sk; 1365 int err; 1366 struct l2cap_chan *chan; 1367 1368 BT_DBG("sock %p, sk %p", sock, sk); 1369 1370 if (!sk) 1371 return 0; 1372 1373 l2cap_sock_cleanup_listen(sk); 1374 bt_sock_unlink(&l2cap_sk_list, sk); 1375 1376 err = l2cap_sock_shutdown(sock, SHUT_RDWR); 1377 chan = l2cap_pi(sk)->chan; 1378 1379 l2cap_chan_hold(chan); 1380 l2cap_chan_lock(chan); 1381 1382 sock_orphan(sk); 1383 l2cap_sock_kill(sk); 1384 1385 l2cap_chan_unlock(chan); 1386 l2cap_chan_put(chan); 1387 1388 return err; 1389 } 1390 1391 static void l2cap_sock_cleanup_listen(struct sock *parent) 1392 { 1393 struct sock *sk; 1394 1395 BT_DBG("parent %p state %s", parent, 1396 state_to_string(parent->sk_state)); 1397 1398 /* Close not yet accepted channels */ 1399 while ((sk = bt_accept_dequeue(parent, NULL))) { 1400 struct l2cap_chan *chan = l2cap_pi(sk)->chan; 1401 1402 BT_DBG("child chan %p state %s", chan, 1403 state_to_string(chan->state)); 1404 1405 l2cap_chan_hold(chan); 1406 l2cap_chan_lock(chan); 1407 1408 __clear_chan_timer(chan); 1409 l2cap_chan_close(chan, ECONNRESET); 1410 l2cap_sock_kill(sk); 1411 1412 l2cap_chan_unlock(chan); 1413 l2cap_chan_put(chan); 1414 } 1415 } 1416 1417 static struct l2cap_chan *l2cap_sock_new_connection_cb(struct l2cap_chan *chan) 1418 { 1419 struct sock *sk, *parent = chan->data; 1420 1421 lock_sock(parent); 1422 1423 /* Check for backlog size */ 1424 if (sk_acceptq_is_full(parent)) { 1425 BT_DBG("backlog full %d", parent->sk_ack_backlog); 1426 release_sock(parent); 1427 return NULL; 1428 } 1429 1430 sk = l2cap_sock_alloc(sock_net(parent), NULL, BTPROTO_L2CAP, 1431 GFP_ATOMIC, 0); 1432 if (!sk) { 1433 release_sock(parent); 1434 return NULL; 1435 } 1436 1437 bt_sock_reclassify_lock(sk, BTPROTO_L2CAP); 1438 1439 l2cap_sock_init(sk, parent); 1440 1441 bt_accept_enqueue(parent, sk, false); 1442 1443 release_sock(parent); 1444 1445 return l2cap_pi(sk)->chan; 1446 } 1447 1448 static int l2cap_sock_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb) 1449 { 1450 struct sock *sk = chan->data; 1451 int err; 1452 1453 lock_sock(sk); 1454 1455 if (l2cap_pi(sk)->rx_busy_skb) { 1456 err = -ENOMEM; 1457 goto done; 1458 } 1459 1460 if (chan->mode != L2CAP_MODE_ERTM && 1461 chan->mode != L2CAP_MODE_STREAMING) { 1462 /* Even if no filter is attached, we could potentially 1463 * get errors from security modules, etc. 1464 */ 1465 err = sk_filter(sk, skb); 1466 if (err) 1467 goto done; 1468 } 1469 1470 err = __sock_queue_rcv_skb(sk, skb); 1471 1472 /* For ERTM, handle one skb that doesn't fit into the recv 1473 * buffer. This is important to do because the data frames 1474 * have already been acked, so the skb cannot be discarded. 1475 * 1476 * Notify the l2cap core that the buffer is full, so the 1477 * LOCAL_BUSY state is entered and no more frames are 1478 * acked and reassembled until there is buffer space 1479 * available. 1480 */ 1481 if (err < 0 && chan->mode == L2CAP_MODE_ERTM) { 1482 l2cap_pi(sk)->rx_busy_skb = skb; 1483 l2cap_chan_busy(chan, 1); 1484 err = 0; 1485 } 1486 1487 done: 1488 release_sock(sk); 1489 1490 return err; 1491 } 1492 1493 static void l2cap_sock_close_cb(struct l2cap_chan *chan) 1494 { 1495 struct sock *sk = chan->data; 1496 1497 if (!sk) 1498 return; 1499 1500 l2cap_sock_kill(sk); 1501 } 1502 1503 static void l2cap_sock_teardown_cb(struct l2cap_chan *chan, int err) 1504 { 1505 struct sock *sk = chan->data; 1506 struct sock *parent; 1507 1508 if (!sk) 1509 return; 1510 1511 BT_DBG("chan %p state %s", chan, state_to_string(chan->state)); 1512 1513 /* This callback can be called both for server (BT_LISTEN) 1514 * sockets as well as "normal" ones. To avoid lockdep warnings 1515 * with child socket locking (through l2cap_sock_cleanup_listen) 1516 * we need separation into separate nesting levels. The simplest 1517 * way to accomplish this is to inherit the nesting level used 1518 * for the channel. 1519 */ 1520 lock_sock_nested(sk, atomic_read(&chan->nesting)); 1521 1522 parent = bt_sk(sk)->parent; 1523 1524 switch (chan->state) { 1525 case BT_OPEN: 1526 case BT_BOUND: 1527 case BT_CLOSED: 1528 break; 1529 case BT_LISTEN: 1530 l2cap_sock_cleanup_listen(sk); 1531 sk->sk_state = BT_CLOSED; 1532 chan->state = BT_CLOSED; 1533 1534 break; 1535 default: 1536 sk->sk_state = BT_CLOSED; 1537 chan->state = BT_CLOSED; 1538 1539 sk->sk_err = err; 1540 1541 if (parent) { 1542 bt_accept_unlink(sk); 1543 parent->sk_data_ready(parent); 1544 } else { 1545 sk->sk_state_change(sk); 1546 } 1547 1548 break; 1549 } 1550 release_sock(sk); 1551 1552 /* Only zap after cleanup to avoid use after free race */ 1553 sock_set_flag(sk, SOCK_ZAPPED); 1554 1555 } 1556 1557 static void l2cap_sock_state_change_cb(struct l2cap_chan *chan, int state, 1558 int err) 1559 { 1560 struct sock *sk = chan->data; 1561 1562 sk->sk_state = state; 1563 1564 if (err) 1565 sk->sk_err = err; 1566 } 1567 1568 static struct sk_buff *l2cap_sock_alloc_skb_cb(struct l2cap_chan *chan, 1569 unsigned long hdr_len, 1570 unsigned long len, int nb) 1571 { 1572 struct sock *sk = chan->data; 1573 struct sk_buff *skb; 1574 int err; 1575 1576 l2cap_chan_unlock(chan); 1577 skb = bt_skb_send_alloc(sk, hdr_len + len, nb, &err); 1578 l2cap_chan_lock(chan); 1579 1580 if (!skb) 1581 return ERR_PTR(err); 1582 1583 /* Channel lock is released before requesting new skb and then 1584 * reacquired thus we need to recheck channel state. 1585 */ 1586 if (chan->state != BT_CONNECTED) { 1587 kfree_skb(skb); 1588 return ERR_PTR(-ENOTCONN); 1589 } 1590 1591 skb->priority = READ_ONCE(sk->sk_priority); 1592 1593 bt_cb(skb)->l2cap.chan = chan; 1594 1595 return skb; 1596 } 1597 1598 static void l2cap_sock_ready_cb(struct l2cap_chan *chan) 1599 { 1600 struct sock *sk = chan->data; 1601 struct sock *parent; 1602 1603 lock_sock(sk); 1604 1605 parent = bt_sk(sk)->parent; 1606 1607 BT_DBG("sk %p, parent %p", sk, parent); 1608 1609 sk->sk_state = BT_CONNECTED; 1610 sk->sk_state_change(sk); 1611 1612 if (parent) 1613 parent->sk_data_ready(parent); 1614 1615 release_sock(sk); 1616 } 1617 1618 static void l2cap_sock_defer_cb(struct l2cap_chan *chan) 1619 { 1620 struct sock *parent, *sk = chan->data; 1621 1622 lock_sock(sk); 1623 1624 parent = bt_sk(sk)->parent; 1625 if (parent) 1626 parent->sk_data_ready(parent); 1627 1628 release_sock(sk); 1629 } 1630 1631 static void l2cap_sock_resume_cb(struct l2cap_chan *chan) 1632 { 1633 struct sock *sk = chan->data; 1634 1635 if (test_and_clear_bit(FLAG_PENDING_SECURITY, &chan->flags)) { 1636 sk->sk_state = BT_CONNECTED; 1637 chan->state = BT_CONNECTED; 1638 } 1639 1640 clear_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags); 1641 sk->sk_state_change(sk); 1642 } 1643 1644 static void l2cap_sock_set_shutdown_cb(struct l2cap_chan *chan) 1645 { 1646 struct sock *sk = chan->data; 1647 1648 lock_sock(sk); 1649 sk->sk_shutdown = SHUTDOWN_MASK; 1650 release_sock(sk); 1651 } 1652 1653 static long l2cap_sock_get_sndtimeo_cb(struct l2cap_chan *chan) 1654 { 1655 struct sock *sk = chan->data; 1656 1657 return sk->sk_sndtimeo; 1658 } 1659 1660 static struct pid *l2cap_sock_get_peer_pid_cb(struct l2cap_chan *chan) 1661 { 1662 struct sock *sk = chan->data; 1663 1664 return sk->sk_peer_pid; 1665 } 1666 1667 static void l2cap_sock_suspend_cb(struct l2cap_chan *chan) 1668 { 1669 struct sock *sk = chan->data; 1670 1671 set_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags); 1672 sk->sk_state_change(sk); 1673 } 1674 1675 static int l2cap_sock_filter(struct l2cap_chan *chan, struct sk_buff *skb) 1676 { 1677 struct sock *sk = chan->data; 1678 1679 switch (chan->mode) { 1680 case L2CAP_MODE_ERTM: 1681 case L2CAP_MODE_STREAMING: 1682 return sk_filter(sk, skb); 1683 } 1684 1685 return 0; 1686 } 1687 1688 static const struct l2cap_ops l2cap_chan_ops = { 1689 .name = "L2CAP Socket Interface", 1690 .new_connection = l2cap_sock_new_connection_cb, 1691 .recv = l2cap_sock_recv_cb, 1692 .close = l2cap_sock_close_cb, 1693 .teardown = l2cap_sock_teardown_cb, 1694 .state_change = l2cap_sock_state_change_cb, 1695 .ready = l2cap_sock_ready_cb, 1696 .defer = l2cap_sock_defer_cb, 1697 .resume = l2cap_sock_resume_cb, 1698 .suspend = l2cap_sock_suspend_cb, 1699 .set_shutdown = l2cap_sock_set_shutdown_cb, 1700 .get_sndtimeo = l2cap_sock_get_sndtimeo_cb, 1701 .get_peer_pid = l2cap_sock_get_peer_pid_cb, 1702 .alloc_skb = l2cap_sock_alloc_skb_cb, 1703 .filter = l2cap_sock_filter, 1704 }; 1705 1706 static void l2cap_sock_destruct(struct sock *sk) 1707 { 1708 BT_DBG("sk %p", sk); 1709 1710 if (l2cap_pi(sk)->chan) { 1711 l2cap_pi(sk)->chan->data = NULL; 1712 l2cap_chan_put(l2cap_pi(sk)->chan); 1713 } 1714 1715 if (l2cap_pi(sk)->rx_busy_skb) { 1716 kfree_skb(l2cap_pi(sk)->rx_busy_skb); 1717 l2cap_pi(sk)->rx_busy_skb = NULL; 1718 } 1719 1720 skb_queue_purge(&sk->sk_receive_queue); 1721 skb_queue_purge(&sk->sk_write_queue); 1722 } 1723 1724 static void l2cap_skb_msg_name(struct sk_buff *skb, void *msg_name, 1725 int *msg_namelen) 1726 { 1727 DECLARE_SOCKADDR(struct sockaddr_l2 *, la, msg_name); 1728 1729 memset(la, 0, sizeof(struct sockaddr_l2)); 1730 la->l2_family = AF_BLUETOOTH; 1731 la->l2_psm = bt_cb(skb)->l2cap.psm; 1732 bacpy(&la->l2_bdaddr, &bt_cb(skb)->l2cap.bdaddr); 1733 1734 *msg_namelen = sizeof(struct sockaddr_l2); 1735 } 1736 1737 static void l2cap_sock_init(struct sock *sk, struct sock *parent) 1738 { 1739 struct l2cap_chan *chan = l2cap_pi(sk)->chan; 1740 1741 BT_DBG("sk %p", sk); 1742 1743 if (parent) { 1744 struct l2cap_chan *pchan = l2cap_pi(parent)->chan; 1745 1746 sk->sk_type = parent->sk_type; 1747 bt_sk(sk)->flags = bt_sk(parent)->flags; 1748 1749 chan->chan_type = pchan->chan_type; 1750 chan->imtu = pchan->imtu; 1751 chan->omtu = pchan->omtu; 1752 chan->conf_state = pchan->conf_state; 1753 chan->mode = pchan->mode; 1754 chan->fcs = pchan->fcs; 1755 chan->max_tx = pchan->max_tx; 1756 chan->tx_win = pchan->tx_win; 1757 chan->tx_win_max = pchan->tx_win_max; 1758 chan->sec_level = pchan->sec_level; 1759 chan->flags = pchan->flags; 1760 chan->tx_credits = pchan->tx_credits; 1761 chan->rx_credits = pchan->rx_credits; 1762 1763 if (chan->chan_type == L2CAP_CHAN_FIXED) { 1764 chan->scid = pchan->scid; 1765 chan->dcid = pchan->scid; 1766 } 1767 1768 security_sk_clone(parent, sk); 1769 } else { 1770 switch (sk->sk_type) { 1771 case SOCK_RAW: 1772 chan->chan_type = L2CAP_CHAN_RAW; 1773 break; 1774 case SOCK_DGRAM: 1775 chan->chan_type = L2CAP_CHAN_CONN_LESS; 1776 bt_sk(sk)->skb_msg_name = l2cap_skb_msg_name; 1777 break; 1778 case SOCK_SEQPACKET: 1779 case SOCK_STREAM: 1780 chan->chan_type = L2CAP_CHAN_CONN_ORIENTED; 1781 break; 1782 } 1783 1784 chan->imtu = L2CAP_DEFAULT_MTU; 1785 chan->omtu = 0; 1786 if (!disable_ertm && sk->sk_type == SOCK_STREAM) { 1787 chan->mode = L2CAP_MODE_ERTM; 1788 set_bit(CONF_STATE2_DEVICE, &chan->conf_state); 1789 } else { 1790 chan->mode = L2CAP_MODE_BASIC; 1791 } 1792 1793 l2cap_chan_set_defaults(chan); 1794 } 1795 1796 /* Default config options */ 1797 chan->flush_to = L2CAP_DEFAULT_FLUSH_TO; 1798 1799 chan->data = sk; 1800 chan->ops = &l2cap_chan_ops; 1801 } 1802 1803 static struct proto l2cap_proto = { 1804 .name = "L2CAP", 1805 .owner = THIS_MODULE, 1806 .obj_size = sizeof(struct l2cap_pinfo) 1807 }; 1808 1809 static struct sock *l2cap_sock_alloc(struct net *net, struct socket *sock, 1810 int proto, gfp_t prio, int kern) 1811 { 1812 struct sock *sk; 1813 struct l2cap_chan *chan; 1814 1815 sk = bt_sock_alloc(net, sock, &l2cap_proto, proto, prio, kern); 1816 if (!sk) 1817 return NULL; 1818 1819 sk->sk_destruct = l2cap_sock_destruct; 1820 sk->sk_sndtimeo = L2CAP_CONN_TIMEOUT; 1821 1822 chan = l2cap_chan_create(); 1823 if (!chan) { 1824 sk_free(sk); 1825 return NULL; 1826 } 1827 1828 l2cap_chan_hold(chan); 1829 1830 l2cap_pi(sk)->chan = chan; 1831 1832 return sk; 1833 } 1834 1835 static int l2cap_sock_create(struct net *net, struct socket *sock, int protocol, 1836 int kern) 1837 { 1838 struct sock *sk; 1839 1840 BT_DBG("sock %p", sock); 1841 1842 sock->state = SS_UNCONNECTED; 1843 1844 if (sock->type != SOCK_SEQPACKET && sock->type != SOCK_STREAM && 1845 sock->type != SOCK_DGRAM && sock->type != SOCK_RAW) 1846 return -ESOCKTNOSUPPORT; 1847 1848 if (sock->type == SOCK_RAW && !kern && !capable(CAP_NET_RAW)) 1849 return -EPERM; 1850 1851 sock->ops = &l2cap_sock_ops; 1852 1853 sk = l2cap_sock_alloc(net, sock, protocol, GFP_ATOMIC, kern); 1854 if (!sk) 1855 return -ENOMEM; 1856 1857 l2cap_sock_init(sk, NULL); 1858 bt_sock_link(&l2cap_sk_list, sk); 1859 return 0; 1860 } 1861 1862 static const struct proto_ops l2cap_sock_ops = { 1863 .family = PF_BLUETOOTH, 1864 .owner = THIS_MODULE, 1865 .release = l2cap_sock_release, 1866 .bind = l2cap_sock_bind, 1867 .connect = l2cap_sock_connect, 1868 .listen = l2cap_sock_listen, 1869 .accept = l2cap_sock_accept, 1870 .getname = l2cap_sock_getname, 1871 .sendmsg = l2cap_sock_sendmsg, 1872 .recvmsg = l2cap_sock_recvmsg, 1873 .poll = bt_sock_poll, 1874 .ioctl = bt_sock_ioctl, 1875 .gettstamp = sock_gettstamp, 1876 .mmap = sock_no_mmap, 1877 .socketpair = sock_no_socketpair, 1878 .shutdown = l2cap_sock_shutdown, 1879 .setsockopt = l2cap_sock_setsockopt, 1880 .getsockopt = l2cap_sock_getsockopt 1881 }; 1882 1883 static const struct net_proto_family l2cap_sock_family_ops = { 1884 .family = PF_BLUETOOTH, 1885 .owner = THIS_MODULE, 1886 .create = l2cap_sock_create, 1887 }; 1888 1889 int __init l2cap_init_sockets(void) 1890 { 1891 int err; 1892 1893 BUILD_BUG_ON(sizeof(struct sockaddr_l2) > sizeof(struct sockaddr)); 1894 1895 err = proto_register(&l2cap_proto, 0); 1896 if (err < 0) 1897 return err; 1898 1899 err = bt_sock_register(BTPROTO_L2CAP, &l2cap_sock_family_ops); 1900 if (err < 0) { 1901 BT_ERR("L2CAP socket registration failed"); 1902 goto error; 1903 } 1904 1905 err = bt_procfs_init(&init_net, "l2cap", &l2cap_sk_list, 1906 NULL); 1907 if (err < 0) { 1908 BT_ERR("Failed to create L2CAP proc file"); 1909 bt_sock_unregister(BTPROTO_L2CAP); 1910 goto error; 1911 } 1912 1913 BT_INFO("L2CAP socket layer initialized"); 1914 1915 return 0; 1916 1917 error: 1918 proto_unregister(&l2cap_proto); 1919 return err; 1920 } 1921 1922 void l2cap_cleanup_sockets(void) 1923 { 1924 bt_procfs_cleanup(&init_net, "l2cap"); 1925 bt_sock_unregister(BTPROTO_L2CAP); 1926 proto_unregister(&l2cap_proto); 1927 } 1928