1 /* $NetBSD: hci_socket.c,v 1.41 2015/04/03 20:01:07 rtr Exp $ */ 2 3 /*- 4 * Copyright (c) 2005 Iain Hibbert. 5 * Copyright (c) 2006 Itronix Inc. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. The name of Itronix Inc. may not be used to endorse 17 * or promote products derived from this software without specific 18 * prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY 24 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 27 * ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 #include <sys/cdefs.h> 34 __KERNEL_RCSID(0, "$NetBSD: hci_socket.c,v 1.41 2015/04/03 20:01:07 rtr Exp $"); 35 36 /* load symbolic names */ 37 #ifdef BLUETOOTH_DEBUG 38 #define PRUREQUESTS 39 #define PRCOREQUESTS 40 #endif 41 42 #include <sys/param.h> 43 #include <sys/domain.h> 44 #include <sys/kauth.h> 45 #include <sys/kernel.h> 46 #include <sys/kmem.h> 47 #include <sys/mbuf.h> 48 #include <sys/proc.h> 49 #include <sys/protosw.h> 50 #include <sys/socket.h> 51 #include <sys/socketvar.h> 52 #include <sys/systm.h> 53 54 #include <netbt/bluetooth.h> 55 #include <netbt/hci.h> 56 57 /******************************************************************************* 58 * 59 * HCI SOCK_RAW Sockets - for control of Bluetooth Devices 60 * 61 */ 62 63 /* 64 * the raw HCI protocol control block 65 */ 66 struct hci_pcb { 67 struct socket *hp_socket; /* socket */ 68 kauth_cred_t hp_cred; /* owner credential */ 69 unsigned int hp_flags; /* flags */ 70 bdaddr_t hp_laddr; /* local address */ 71 bdaddr_t hp_raddr; /* remote address */ 72 struct hci_filter hp_efilter; /* user event filter */ 73 struct hci_filter hp_pfilter; /* user packet filter */ 74 LIST_ENTRY(hci_pcb) hp_next; /* next HCI pcb */ 75 }; 76 77 /* hp_flags */ 78 #define HCI_DIRECTION (1<<1) /* direction control messages */ 79 #define HCI_PROMISCUOUS (1<<2) /* listen to all units */ 80 81 LIST_HEAD(hci_pcb_list, hci_pcb) hci_pcb = LIST_HEAD_INITIALIZER(hci_pcb); 82 83 /* sysctl defaults */ 84 int hci_sendspace = HCI_CMD_PKT_SIZE; 85 int hci_recvspace = 4096; 86 87 /* unprivileged commands opcode table */ 88 static const struct { 89 uint16_t opcode; 90 uint8_t offs; /* 0 - 63 */ 91 uint8_t mask; /* bit 0 - 7 */ 92 uint8_t length; /* approved length */ 93 } hci_cmds[] = { 94 { HCI_CMD_INQUIRY, 95 0, 0x01, sizeof(hci_inquiry_cp) }, 96 { HCI_CMD_REMOTE_NAME_REQ, 97 2, 0x08, sizeof(hci_remote_name_req_cp) }, 98 { HCI_CMD_READ_REMOTE_FEATURES, 99 2, 0x20, sizeof(hci_read_remote_features_cp) }, 100 { HCI_CMD_READ_REMOTE_EXTENDED_FEATURES, 101 2, 0x40, sizeof(hci_read_remote_extended_features_cp) }, 102 { HCI_CMD_READ_REMOTE_VER_INFO, 103 2, 0x80, sizeof(hci_read_remote_ver_info_cp) }, 104 { HCI_CMD_READ_CLOCK_OFFSET, 105 3, 0x01, sizeof(hci_read_clock_offset_cp) }, 106 { HCI_CMD_READ_LMP_HANDLE, 107 3, 0x02, sizeof(hci_read_lmp_handle_cp) }, 108 { HCI_CMD_ROLE_DISCOVERY, 109 4, 0x80, sizeof(hci_role_discovery_cp) }, 110 { HCI_CMD_READ_LINK_POLICY_SETTINGS, 111 5, 0x02, sizeof(hci_read_link_policy_settings_cp) }, 112 { HCI_CMD_READ_DEFAULT_LINK_POLICY_SETTINGS, 113 5, 0x08, 0 }, 114 { HCI_CMD_READ_PIN_TYPE, 115 6, 0x04, 0 }, 116 { HCI_CMD_READ_LOCAL_NAME, 117 7, 0x02, 0 }, 118 { HCI_CMD_READ_CON_ACCEPT_TIMEOUT, 119 7, 0x04, 0 }, 120 { HCI_CMD_READ_PAGE_TIMEOUT, 121 7, 0x10, 0 }, 122 { HCI_CMD_READ_SCAN_ENABLE, 123 7, 0x40, 0 }, 124 { HCI_CMD_READ_PAGE_SCAN_ACTIVITY, 125 8, 0x01, 0 }, 126 { HCI_CMD_READ_INQUIRY_SCAN_ACTIVITY, 127 8, 0x04, 0 }, 128 { HCI_CMD_READ_AUTH_ENABLE, 129 8, 0x10, 0 }, 130 { HCI_CMD_READ_ENCRYPTION_MODE, 131 8, 0x40, 0 }, 132 { HCI_CMD_READ_UNIT_CLASS, 133 9, 0x01, 0 }, 134 { HCI_CMD_READ_VOICE_SETTING, 135 9, 0x04, 0 }, 136 { HCI_CMD_READ_AUTO_FLUSH_TIMEOUT, 137 9, 0x10, sizeof(hci_read_auto_flush_timeout_cp) }, 138 { HCI_CMD_READ_NUM_BROADCAST_RETRANS, 139 9, 0x40, 0 }, 140 { HCI_CMD_READ_HOLD_MODE_ACTIVITY, 141 10, 0x01, 0 }, 142 { HCI_CMD_READ_XMIT_LEVEL, 143 10, 0x04, sizeof(hci_read_xmit_level_cp) }, 144 { HCI_CMD_READ_SCO_FLOW_CONTROL, 145 10, 0x08, 0 }, 146 { HCI_CMD_READ_LINK_SUPERVISION_TIMEOUT, 147 11, 0x01, sizeof(hci_read_link_supervision_timeout_cp) }, 148 { HCI_CMD_READ_NUM_SUPPORTED_IAC, 149 11, 0x04, 0 }, 150 { HCI_CMD_READ_IAC_LAP, 151 11, 0x08, 0 }, 152 { HCI_CMD_READ_PAGE_SCAN_PERIOD, 153 11, 0x20, 0 }, 154 { HCI_CMD_READ_PAGE_SCAN, 155 11, 0x80, 0 }, 156 { HCI_CMD_READ_INQUIRY_SCAN_TYPE, 157 12, 0x10, 0 }, 158 { HCI_CMD_READ_INQUIRY_MODE, 159 12, 0x40, 0 }, 160 { HCI_CMD_READ_PAGE_SCAN_TYPE, 161 13, 0x01, 0 }, 162 { HCI_CMD_READ_AFH_ASSESSMENT, 163 13, 0x04, 0 }, 164 { HCI_CMD_READ_LOCAL_VER, 165 14, 0x08, 0 }, 166 { HCI_CMD_READ_LOCAL_COMMANDS, 167 14, 0x10, 0 }, 168 { HCI_CMD_READ_LOCAL_FEATURES, 169 14, 0x20, 0 }, 170 { HCI_CMD_READ_LOCAL_EXTENDED_FEATURES, 171 14, 0x40, sizeof(hci_read_local_extended_features_cp) }, 172 { HCI_CMD_READ_BUFFER_SIZE, 173 14, 0x80, 0 }, 174 { HCI_CMD_READ_COUNTRY_CODE, 175 15, 0x01, 0 }, 176 { HCI_CMD_READ_BDADDR, 177 15, 0x02, 0 }, 178 { HCI_CMD_READ_FAILED_CONTACT_CNTR, 179 15, 0x04, sizeof(hci_read_failed_contact_cntr_cp) }, 180 { HCI_CMD_READ_LINK_QUALITY, 181 15, 0x10, sizeof(hci_read_link_quality_cp) }, 182 { HCI_CMD_READ_RSSI, 183 15, 0x20, sizeof(hci_read_rssi_cp) }, 184 { HCI_CMD_READ_AFH_CHANNEL_MAP, 185 15, 0x40, sizeof(hci_read_afh_channel_map_cp) }, 186 { HCI_CMD_READ_CLOCK, 187 15, 0x80, sizeof(hci_read_clock_cp) }, 188 { HCI_CMD_READ_LOOPBACK_MODE, 189 16, 0x01, 0 }, 190 { HCI_CMD_READ_EXTENDED_INQUIRY_RSP, 191 17, 0x01, 0 }, 192 { HCI_CMD_READ_SIMPLE_PAIRING_MODE, 193 17, 0x20, 0 }, 194 { HCI_CMD_READ_INQUIRY_RSP_XMIT_POWER, 195 18, 0x01, 0 }, 196 { HCI_CMD_READ_DEFAULT_ERRDATA_REPORTING, 197 18, 0x04, 0 }, 198 }; 199 200 /* 201 * supply a basic device send/recv policy 202 */ 203 static int 204 hci_device_cb(kauth_cred_t cred, kauth_action_t action, void *cookie, 205 void *arg0, void *arg1, void *arg2, void *arg3) 206 { 207 int i, result; 208 209 result = KAUTH_RESULT_DEFER; 210 211 switch (action) { 212 case KAUTH_DEVICE_BLUETOOTH_SEND: { 213 struct hci_unit *unit = (struct hci_unit *)arg0; 214 hci_cmd_hdr_t *hdr = (hci_cmd_hdr_t *)arg1; 215 216 /* 217 * Allow sending unprivileged commands if the packet size 218 * is correct and the unit claims to support it 219 */ 220 221 if (hdr->type != HCI_CMD_PKT) 222 break; 223 224 for (i = 0; i < __arraycount(hci_cmds); i++) { 225 if (hdr->opcode == hci_cmds[i].opcode 226 && hdr->length == hci_cmds[i].length 227 && (unit->hci_cmds[hci_cmds[i].offs] & hci_cmds[i].mask)) { 228 result = KAUTH_RESULT_ALLOW; 229 break; 230 } 231 } 232 233 break; 234 } 235 236 case KAUTH_DEVICE_BLUETOOTH_RECV: 237 switch((uint8_t)(uintptr_t)arg0) { 238 case HCI_CMD_PKT: { 239 uint16_t opcode = (uint16_t)(uintptr_t)arg1; 240 241 /* 242 * Allow to see any unprivileged command packet 243 */ 244 245 for (i = 0; i < __arraycount(hci_cmds); i++) { 246 if (opcode == hci_cmds[i].opcode) { 247 result = KAUTH_RESULT_ALLOW; 248 break; 249 } 250 } 251 252 break; 253 } 254 255 case HCI_EVENT_PKT: { 256 uint8_t event = (uint8_t)(uintptr_t)arg1; 257 258 /* 259 * Allow to receive most events 260 */ 261 262 switch (event) { 263 case HCI_EVENT_RETURN_LINK_KEYS: 264 case HCI_EVENT_LINK_KEY_NOTIFICATION: 265 case HCI_EVENT_USER_CONFIRM_REQ: 266 case HCI_EVENT_USER_PASSKEY_NOTIFICATION: 267 case HCI_EVENT_VENDOR: 268 break; 269 270 default: 271 result = KAUTH_RESULT_ALLOW; 272 break; 273 } 274 275 break; 276 } 277 278 case HCI_ACL_DATA_PKT: 279 case HCI_SCO_DATA_PKT: { 280 /* uint16_t handle = (uint16_t)(uintptr_t)arg1; */ 281 /* 282 * don't normally allow receiving data packets 283 */ 284 break; 285 } 286 287 default: 288 break; 289 } 290 291 break; 292 293 default: 294 break; 295 } 296 297 return result; 298 } 299 300 /* 301 * HCI protocol init routine, 302 * - set up a kauth listener to provide basic packet access policy 303 */ 304 void 305 hci_init(void) 306 { 307 308 if (kauth_listen_scope(KAUTH_SCOPE_DEVICE, hci_device_cb, NULL) == NULL) 309 panic("Bluetooth HCI: cannot listen on device scope"); 310 } 311 312 /* 313 * When command packet reaches the device, we can drop 314 * it from the socket buffer (called from hci_output_acl) 315 */ 316 void 317 hci_drop(void *arg) 318 { 319 struct socket *so = arg; 320 321 sbdroprecord(&so->so_snd); 322 sowwakeup(so); 323 } 324 325 /* 326 * HCI socket is going away and has some pending packets. We let them 327 * go by design, but remove the context pointer as it will be invalid 328 * and we no longer need to be notified. 329 */ 330 static void 331 hci_cmdwait_flush(struct socket *so) 332 { 333 struct hci_unit *unit; 334 struct socket *ctx; 335 struct mbuf *m; 336 337 DPRINTF("flushing %p\n", so); 338 339 SIMPLEQ_FOREACH(unit, &hci_unit_list, hci_next) { 340 m = MBUFQ_FIRST(&unit->hci_cmdwait); 341 while (m != NULL) { 342 ctx = M_GETCTX(m, struct socket *); 343 if (ctx == so) 344 M_SETCTX(m, NULL); 345 346 m = MBUFQ_NEXT(m); 347 } 348 } 349 } 350 351 /* 352 * HCI send packet 353 * This came from userland, so check it out. 354 */ 355 static int 356 hci_send_pcb(struct hci_pcb *pcb, struct mbuf *m, bdaddr_t *addr) 357 { 358 struct hci_unit *unit; 359 struct mbuf *m0; 360 hci_cmd_hdr_t hdr; 361 int err; 362 363 KASSERT(m != NULL); 364 KASSERT(addr != NULL); 365 366 /* wants at least a header to start with */ 367 if (m->m_pkthdr.len < sizeof(hdr)) { 368 err = EMSGSIZE; 369 goto bad; 370 } 371 m_copydata(m, 0, sizeof(hdr), &hdr); 372 hdr.opcode = le16toh(hdr.opcode); 373 374 /* only allows CMD packets to be sent */ 375 if (hdr.type != HCI_CMD_PKT) { 376 err = EINVAL; 377 goto bad; 378 } 379 380 /* validates packet length */ 381 if (m->m_pkthdr.len != sizeof(hdr) + hdr.length) { 382 err = EMSGSIZE; 383 goto bad; 384 } 385 386 /* finds destination */ 387 unit = hci_unit_lookup(addr); 388 if (unit == NULL) { 389 err = ENETDOWN; 390 goto bad; 391 } 392 393 /* security checks for unprivileged users */ 394 if (pcb->hp_cred != NULL 395 && kauth_authorize_device(pcb->hp_cred, 396 KAUTH_DEVICE_BLUETOOTH_SEND, 397 unit, &hdr, NULL, NULL) != 0) { 398 err = EPERM; 399 goto bad; 400 } 401 402 /* makess a copy for precious to keep */ 403 m0 = m_copypacket(m, M_DONTWAIT); 404 if (m0 == NULL) { 405 err = ENOMEM; 406 goto bad; 407 } 408 sbappendrecord(&pcb->hp_socket->so_snd, m0); 409 M_SETCTX(m, pcb->hp_socket); /* enable drop callback */ 410 411 DPRINTFN(2, "(%s) opcode (%03x|%04x)\n", device_xname(unit->hci_dev), 412 HCI_OGF(hdr.opcode), HCI_OCF(hdr.opcode)); 413 414 /* Sendss it */ 415 if (unit->hci_num_cmd_pkts == 0) 416 MBUFQ_ENQUEUE(&unit->hci_cmdwait, m); 417 else 418 hci_output_cmd(unit, m); 419 420 return 0; 421 422 bad: 423 DPRINTF("packet (%d bytes) not sent (error %d)\n", 424 m->m_pkthdr.len, err); 425 if (m) m_freem(m); 426 return err; 427 } 428 429 static int 430 hci_attach(struct socket *so, int proto) 431 { 432 struct hci_pcb *pcb; 433 int error; 434 435 KASSERT(so->so_pcb == NULL); 436 437 if (so->so_lock == NULL) { 438 mutex_obj_hold(bt_lock); 439 so->so_lock = bt_lock; 440 solock(so); 441 } 442 KASSERT(solocked(so)); 443 444 error = soreserve(so, hci_sendspace, hci_recvspace); 445 if (error) { 446 return error; 447 } 448 449 pcb = kmem_zalloc(sizeof(struct hci_pcb), KM_SLEEP); 450 pcb->hp_cred = kauth_cred_dup(curlwp->l_cred); 451 pcb->hp_socket = so; 452 453 /* 454 * Set default user filter. By default, socket only passes 455 * Command_Complete and Command_Status Events. 456 */ 457 hci_filter_set(HCI_EVENT_COMMAND_COMPL, &pcb->hp_efilter); 458 hci_filter_set(HCI_EVENT_COMMAND_STATUS, &pcb->hp_efilter); 459 hci_filter_set(HCI_EVENT_PKT, &pcb->hp_pfilter); 460 461 LIST_INSERT_HEAD(&hci_pcb, pcb, hp_next); 462 so->so_pcb = pcb; 463 464 return 0; 465 } 466 467 static void 468 hci_detach(struct socket *so) 469 { 470 struct hci_pcb *pcb; 471 472 pcb = (struct hci_pcb *)so->so_pcb; 473 KASSERT(pcb != NULL); 474 475 if (so->so_snd.sb_mb != NULL) 476 hci_cmdwait_flush(so); 477 478 if (pcb->hp_cred != NULL) 479 kauth_cred_free(pcb->hp_cred); 480 481 so->so_pcb = NULL; 482 LIST_REMOVE(pcb, hp_next); 483 kmem_free(pcb, sizeof(*pcb)); 484 } 485 486 static int 487 hci_accept(struct socket *so, struct mbuf *nam) 488 { 489 KASSERT(solocked(so)); 490 491 return EOPNOTSUPP; 492 } 493 494 static int 495 hci_bind(struct socket *so, struct sockaddr *nam, struct lwp *l) 496 { 497 struct hci_pcb *pcb = so->so_pcb; 498 struct sockaddr_bt *sa = (struct sockaddr_bt *)nam; 499 500 KASSERT(solocked(so)); 501 KASSERT(pcb != NULL); 502 KASSERT(nam != NULL); 503 504 if (sa->bt_len != sizeof(struct sockaddr_bt)) 505 return EINVAL; 506 507 if (sa->bt_family != AF_BLUETOOTH) 508 return EAFNOSUPPORT; 509 510 bdaddr_copy(&pcb->hp_laddr, &sa->bt_bdaddr); 511 512 if (bdaddr_any(&sa->bt_bdaddr)) 513 pcb->hp_flags |= HCI_PROMISCUOUS; 514 else 515 pcb->hp_flags &= ~HCI_PROMISCUOUS; 516 517 return 0; 518 } 519 520 static int 521 hci_listen(struct socket *so, struct lwp *l) 522 { 523 KASSERT(solocked(so)); 524 525 return EOPNOTSUPP; 526 } 527 528 static int 529 hci_connect(struct socket *so, struct mbuf *nam, struct lwp *l) 530 { 531 struct hci_pcb *pcb = so->so_pcb; 532 struct sockaddr_bt *sa; 533 534 KASSERT(solocked(so)); 535 KASSERT(pcb != NULL); 536 KASSERT(nam != NULL); 537 538 sa = mtod(nam, struct sockaddr_bt *); 539 if (sa->bt_len != sizeof(struct sockaddr_bt)) 540 return EINVAL; 541 542 if (sa->bt_family != AF_BLUETOOTH) 543 return EAFNOSUPPORT; 544 545 if (hci_unit_lookup(&sa->bt_bdaddr) == NULL) 546 return EADDRNOTAVAIL; 547 548 bdaddr_copy(&pcb->hp_raddr, &sa->bt_bdaddr); 549 soisconnected(so); 550 return 0; 551 } 552 553 static int 554 hci_connect2(struct socket *so, struct socket *so2) 555 { 556 KASSERT(solocked(so)); 557 558 return EOPNOTSUPP; 559 } 560 561 static int 562 hci_disconnect(struct socket *so) 563 { 564 struct hci_pcb *pcb = so->so_pcb; 565 566 KASSERT(solocked(so)); 567 KASSERT(pcb != NULL); 568 569 bdaddr_copy(&pcb->hp_raddr, BDADDR_ANY); 570 571 /* XXX we cannot call soisdisconnected() here, as it sets 572 * SS_CANTRCVMORE and SS_CANTSENDMORE. The problem being, 573 * that soisconnected() does not clear these and if you 574 * try to reconnect this socket (which is permitted) you 575 * get a broken pipe when you try to write any data. 576 */ 577 so->so_state &= ~SS_ISCONNECTED; 578 return 0; 579 } 580 581 static int 582 hci_shutdown(struct socket *so) 583 { 584 KASSERT(solocked(so)); 585 586 socantsendmore(so); 587 return 0; 588 } 589 590 static int 591 hci_abort(struct socket *so) 592 { 593 KASSERT(solocked(so)); 594 595 soisdisconnected(so); 596 hci_detach(so); 597 return 0; 598 } 599 600 static int 601 hci_ioctl(struct socket *so, u_long cmd, void *nam, struct ifnet *ifp) 602 { 603 int err; 604 mutex_enter(bt_lock); 605 err = hci_ioctl_pcb(cmd, nam); 606 mutex_exit(bt_lock); 607 return err; 608 } 609 610 static int 611 hci_stat(struct socket *so, struct stat *ub) 612 { 613 KASSERT(solocked(so)); 614 615 return 0; 616 } 617 618 static int 619 hci_peeraddr(struct socket *so, struct mbuf *nam) 620 { 621 struct hci_pcb *pcb = (struct hci_pcb *)so->so_pcb; 622 struct sockaddr_bt *sa; 623 624 KASSERT(solocked(so)); 625 KASSERT(pcb != NULL); 626 KASSERT(nam != NULL); 627 628 sa = mtod(nam, struct sockaddr_bt *); 629 memset(sa, 0, sizeof(struct sockaddr_bt)); 630 nam->m_len = 631 sa->bt_len = sizeof(struct sockaddr_bt); 632 sa->bt_family = AF_BLUETOOTH; 633 bdaddr_copy(&sa->bt_bdaddr, &pcb->hp_raddr); 634 return 0; 635 } 636 637 static int 638 hci_sockaddr(struct socket *so, struct mbuf *nam) 639 { 640 struct hci_pcb *pcb = (struct hci_pcb *)so->so_pcb; 641 struct sockaddr_bt *sa; 642 643 KASSERT(solocked(so)); 644 KASSERT(pcb != NULL); 645 KASSERT(nam != NULL); 646 647 sa = mtod(nam, struct sockaddr_bt *); 648 memset(sa, 0, sizeof(struct sockaddr_bt)); 649 nam->m_len = 650 sa->bt_len = sizeof(struct sockaddr_bt); 651 sa->bt_family = AF_BLUETOOTH; 652 bdaddr_copy(&sa->bt_bdaddr, &pcb->hp_laddr); 653 return 0; 654 } 655 656 static int 657 hci_rcvd(struct socket *so, int flags, struct lwp *l) 658 { 659 KASSERT(solocked(so)); 660 661 return EOPNOTSUPP; 662 } 663 664 static int 665 hci_recvoob(struct socket *so, struct mbuf *m, int flags) 666 { 667 KASSERT(solocked(so)); 668 669 return EOPNOTSUPP; 670 } 671 672 static int 673 hci_send(struct socket *so, struct mbuf *m, struct mbuf *nam, 674 struct mbuf *control, struct lwp *l) 675 { 676 struct hci_pcb *pcb = so->so_pcb; 677 struct sockaddr_bt * sa = NULL; 678 int err = 0; 679 680 KASSERT(solocked(so)); 681 KASSERT(pcb != NULL); 682 683 if (control) /* have no use for this */ 684 m_freem(control); 685 686 if (nam) { 687 sa = mtod(nam, struct sockaddr_bt *); 688 689 if (sa->bt_len != sizeof(struct sockaddr_bt)) { 690 err = EINVAL; 691 goto release; 692 } 693 694 if (sa->bt_family != AF_BLUETOOTH) { 695 err = EAFNOSUPPORT; 696 goto release; 697 } 698 } 699 700 return hci_send_pcb(pcb, m, (sa ? &sa->bt_bdaddr : &pcb->hp_raddr)); 701 702 release: 703 if (m) 704 m_freem(m); 705 706 return err; 707 } 708 709 static int 710 hci_sendoob(struct socket *so, struct mbuf *m, struct mbuf *control) 711 { 712 KASSERT(solocked(so)); 713 714 if (m) 715 m_freem(m); 716 if (control) 717 m_freem(control); 718 719 return EOPNOTSUPP; 720 } 721 722 static int 723 hci_purgeif(struct socket *so, struct ifnet *ifp) 724 { 725 726 return EOPNOTSUPP; 727 } 728 729 /* 730 * User Request. 731 * up is socket 732 * m is optional mbuf chain containing message 733 * nam is optional mbuf chain containing an address 734 * ctl is optional mbuf chain containing socket options 735 * l is pointer to process requesting action (if any) 736 * 737 * we are responsible for disposing of m and ctl 738 */ 739 static int 740 hci_usrreq(struct socket *up, int req, struct mbuf *m, 741 struct mbuf *nam, struct mbuf *ctl, struct lwp *l) 742 { 743 struct hci_pcb *pcb = up->so_pcb; 744 int err = 0; 745 746 DPRINTFN(2, "%s\n", prurequests[req]); 747 KASSERT(req != PRU_ATTACH); 748 KASSERT(req != PRU_DETACH); 749 KASSERT(req != PRU_ACCEPT); 750 KASSERT(req != PRU_BIND); 751 KASSERT(req != PRU_LISTEN); 752 KASSERT(req != PRU_CONNECT); 753 KASSERT(req != PRU_CONNECT2); 754 KASSERT(req != PRU_DISCONNECT); 755 KASSERT(req != PRU_SHUTDOWN); 756 KASSERT(req != PRU_ABORT); 757 KASSERT(req != PRU_CONTROL); 758 KASSERT(req != PRU_SENSE); 759 KASSERT(req != PRU_PEERADDR); 760 KASSERT(req != PRU_SOCKADDR); 761 KASSERT(req != PRU_RCVD); 762 KASSERT(req != PRU_RCVOOB); 763 KASSERT(req != PRU_SEND); 764 KASSERT(req != PRU_SENDOOB); 765 KASSERT(req != PRU_PURGEIF); 766 767 /* anything after here *requires* a pcb */ 768 if (pcb == NULL) { 769 err = EINVAL; 770 goto release; 771 } 772 773 switch(req) { 774 case PRU_FASTTIMO: 775 case PRU_SLOWTIMO: 776 case PRU_PROTORCV: 777 case PRU_PROTOSEND: 778 err = EOPNOTSUPP; 779 break; 780 781 default: 782 UNKNOWN(req); 783 err = EOPNOTSUPP; 784 break; 785 } 786 787 release: 788 if (m) 789 m_freem(m); 790 if (ctl) 791 m_freem(ctl); 792 return err; 793 } 794 795 /* 796 * get/set socket options 797 */ 798 int 799 hci_ctloutput(int req, struct socket *so, struct sockopt *sopt) 800 { 801 struct hci_pcb *pcb = (struct hci_pcb *)so->so_pcb; 802 int optval, err = 0; 803 804 DPRINTFN(2, "req %s\n", prcorequests[req]); 805 806 if (pcb == NULL) 807 return EINVAL; 808 809 if (sopt->sopt_level != BTPROTO_HCI) 810 return ENOPROTOOPT; 811 812 switch(req) { 813 case PRCO_GETOPT: 814 switch (sopt->sopt_name) { 815 case SO_HCI_EVT_FILTER: 816 err = sockopt_set(sopt, &pcb->hp_efilter, 817 sizeof(struct hci_filter)); 818 819 break; 820 821 case SO_HCI_PKT_FILTER: 822 err = sockopt_set(sopt, &pcb->hp_pfilter, 823 sizeof(struct hci_filter)); 824 825 break; 826 827 case SO_HCI_DIRECTION: 828 err = sockopt_setint(sopt, 829 (pcb->hp_flags & HCI_DIRECTION ? 1 : 0)); 830 831 break; 832 833 default: 834 err = ENOPROTOOPT; 835 break; 836 } 837 break; 838 839 case PRCO_SETOPT: 840 switch (sopt->sopt_name) { 841 case SO_HCI_EVT_FILTER: /* set event filter */ 842 err = sockopt_get(sopt, &pcb->hp_efilter, 843 sizeof(pcb->hp_efilter)); 844 845 break; 846 847 case SO_HCI_PKT_FILTER: /* set packet filter */ 848 err = sockopt_get(sopt, &pcb->hp_pfilter, 849 sizeof(pcb->hp_pfilter)); 850 851 break; 852 853 case SO_HCI_DIRECTION: /* request direction ctl messages */ 854 err = sockopt_getint(sopt, &optval); 855 if (err) 856 break; 857 858 if (optval) 859 pcb->hp_flags |= HCI_DIRECTION; 860 else 861 pcb->hp_flags &= ~HCI_DIRECTION; 862 break; 863 864 default: 865 err = ENOPROTOOPT; 866 break; 867 } 868 break; 869 870 default: 871 err = ENOPROTOOPT; 872 break; 873 } 874 875 return err; 876 } 877 878 /* 879 * HCI mbuf tap routine 880 * 881 * copy packets to any raw HCI sockets that wish (and are 882 * permitted) to see them 883 */ 884 void 885 hci_mtap(struct mbuf *m, struct hci_unit *unit) 886 { 887 struct hci_pcb *pcb; 888 struct mbuf *m0, *ctlmsg, **ctl; 889 struct sockaddr_bt sa; 890 uint8_t type; 891 uint8_t event; 892 uint16_t arg1; 893 894 KASSERT(m->m_len >= sizeof(type)); 895 896 type = *mtod(m, uint8_t *); 897 898 memset(&sa, 0, sizeof(sa)); 899 sa.bt_len = sizeof(struct sockaddr_bt); 900 sa.bt_family = AF_BLUETOOTH; 901 bdaddr_copy(&sa.bt_bdaddr, &unit->hci_bdaddr); 902 903 LIST_FOREACH(pcb, &hci_pcb, hp_next) { 904 /* 905 * filter according to source address 906 */ 907 if ((pcb->hp_flags & HCI_PROMISCUOUS) == 0 908 && bdaddr_same(&pcb->hp_laddr, &sa.bt_bdaddr) == 0) 909 continue; 910 911 /* 912 * filter according to packet type filter 913 */ 914 if (hci_filter_test(type, &pcb->hp_pfilter) == 0) 915 continue; 916 917 /* 918 * filter according to event/security filters 919 */ 920 switch(type) { 921 case HCI_EVENT_PKT: 922 KASSERT(m->m_len >= sizeof(hci_event_hdr_t)); 923 924 event = mtod(m, hci_event_hdr_t *)->event; 925 926 if (hci_filter_test(event, &pcb->hp_efilter) == 0) 927 continue; 928 929 arg1 = event; 930 break; 931 932 case HCI_CMD_PKT: 933 KASSERT(m->m_len >= sizeof(hci_cmd_hdr_t)); 934 arg1 = le16toh(mtod(m, hci_cmd_hdr_t *)->opcode); 935 break; 936 937 case HCI_ACL_DATA_PKT: 938 KASSERT(m->m_len >= sizeof(hci_acldata_hdr_t)); 939 arg1 = le16toh(mtod(m, hci_acldata_hdr_t *)->con_handle); 940 arg1 = HCI_CON_HANDLE(arg1); 941 break; 942 943 case HCI_SCO_DATA_PKT: 944 KASSERT(m->m_len >= sizeof(hci_scodata_hdr_t)); 945 arg1 = le16toh(mtod(m, hci_scodata_hdr_t *)->con_handle); 946 arg1 = HCI_CON_HANDLE(arg1); 947 break; 948 949 default: 950 arg1 = 0; 951 break; 952 } 953 954 if (pcb->hp_cred != NULL 955 && kauth_authorize_device(pcb->hp_cred, 956 KAUTH_DEVICE_BLUETOOTH_RECV, 957 KAUTH_ARG(type), KAUTH_ARG(arg1), NULL, NULL) != 0) 958 continue; 959 960 /* 961 * create control messages 962 */ 963 ctlmsg = NULL; 964 ctl = &ctlmsg; 965 if (pcb->hp_flags & HCI_DIRECTION) { 966 int dir = m->m_flags & M_LINK0 ? 1 : 0; 967 968 *ctl = sbcreatecontrol(&dir, sizeof(dir), 969 SCM_HCI_DIRECTION, BTPROTO_HCI); 970 971 if (*ctl != NULL) 972 ctl = &((*ctl)->m_next); 973 } 974 if (pcb->hp_socket->so_options & SO_TIMESTAMP) { 975 struct timeval tv; 976 977 microtime(&tv); 978 *ctl = sbcreatecontrol(&tv, sizeof(tv), 979 SCM_TIMESTAMP, SOL_SOCKET); 980 981 if (*ctl != NULL) 982 ctl = &((*ctl)->m_next); 983 } 984 985 /* 986 * copy to socket 987 */ 988 m0 = m_copypacket(m, M_DONTWAIT); 989 if (m0 && sbappendaddr(&pcb->hp_socket->so_rcv, 990 (struct sockaddr *)&sa, m0, ctlmsg)) { 991 sorwakeup(pcb->hp_socket); 992 } else { 993 m_freem(ctlmsg); 994 m_freem(m0); 995 } 996 } 997 } 998 999 PR_WRAP_USRREQS(hci) 1000 1001 #define hci_attach hci_attach_wrapper 1002 #define hci_detach hci_detach_wrapper 1003 #define hci_accept hci_accept_wrapper 1004 #define hci_bind hci_bind_wrapper 1005 #define hci_listen hci_listen_wrapper 1006 #define hci_connect hci_connect_wrapper 1007 #define hci_connect2 hci_connect2_wrapper 1008 #define hci_disconnect hci_disconnect_wrapper 1009 #define hci_shutdown hci_shutdown_wrapper 1010 #define hci_abort hci_abort_wrapper 1011 #define hci_ioctl hci_ioctl_wrapper 1012 #define hci_stat hci_stat_wrapper 1013 #define hci_peeraddr hci_peeraddr_wrapper 1014 #define hci_sockaddr hci_sockaddr_wrapper 1015 #define hci_rcvd hci_rcvd_wrapper 1016 #define hci_recvoob hci_recvoob_wrapper 1017 #define hci_send hci_send_wrapper 1018 #define hci_sendoob hci_sendoob_wrapper 1019 #define hci_purgeif hci_purgeif_wrapper 1020 #define hci_usrreq hci_usrreq_wrapper 1021 1022 const struct pr_usrreqs hci_usrreqs = { 1023 .pr_attach = hci_attach, 1024 .pr_detach = hci_detach, 1025 .pr_accept = hci_accept, 1026 .pr_bind = hci_bind, 1027 .pr_listen = hci_listen, 1028 .pr_connect = hci_connect, 1029 .pr_connect2 = hci_connect2, 1030 .pr_disconnect = hci_disconnect, 1031 .pr_shutdown = hci_shutdown, 1032 .pr_abort = hci_abort, 1033 .pr_ioctl = hci_ioctl, 1034 .pr_stat = hci_stat, 1035 .pr_peeraddr = hci_peeraddr, 1036 .pr_sockaddr = hci_sockaddr, 1037 .pr_rcvd = hci_rcvd, 1038 .pr_recvoob = hci_recvoob, 1039 .pr_send = hci_send, 1040 .pr_sendoob = hci_sendoob, 1041 .pr_purgeif = hci_purgeif, 1042 .pr_generic = hci_usrreq, 1043 }; 1044