1 /* 2 * ng_btsocket_hci_raw.c 3 * 4 * Copyright (c) 2001-2002 Maksim Yevmenkin <m_evmenkin@yahoo.com> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * 28 * $Id: ng_btsocket_hci_raw.c,v 1.14 2003/09/14 23:29:06 max Exp $ 29 * $FreeBSD$ 30 */ 31 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/bitstring.h> 35 #include <sys/domain.h> 36 #include <sys/endian.h> 37 #include <sys/errno.h> 38 #include <sys/filedesc.h> 39 #include <sys/ioccom.h> 40 #include <sys/kernel.h> 41 #include <sys/lock.h> 42 #include <sys/malloc.h> 43 #include <sys/mbuf.h> 44 #include <sys/mutex.h> 45 #include <sys/protosw.h> 46 #include <sys/queue.h> 47 #include <sys/socket.h> 48 #include <sys/socketvar.h> 49 #include <sys/sysctl.h> 50 #include <sys/taskqueue.h> 51 #include <netgraph/ng_message.h> 52 #include <netgraph/netgraph.h> 53 #include <netgraph/bluetooth/include/ng_bluetooth.h> 54 #include <netgraph/bluetooth/include/ng_hci.h> 55 #include <netgraph/bluetooth/include/ng_l2cap.h> 56 #include <netgraph/bluetooth/include/ng_btsocket.h> 57 #include <netgraph/bluetooth/include/ng_btsocket_hci_raw.h> 58 59 /* MALLOC define */ 60 #ifdef NG_SEPARATE_MALLOC 61 MALLOC_DEFINE(M_NETGRAPH_BTSOCKET_HCI_RAW, "netgraph_btsocks_hci_raw", 62 "Netgraph Bluetooth raw HCI sockets"); 63 #else 64 #define M_NETGRAPH_BTSOCKET_HCI_RAW M_NETGRAPH 65 #endif /* NG_SEPARATE_MALLOC */ 66 67 /* Netgraph node methods */ 68 static ng_constructor_t ng_btsocket_hci_raw_node_constructor; 69 static ng_rcvmsg_t ng_btsocket_hci_raw_node_rcvmsg; 70 static ng_shutdown_t ng_btsocket_hci_raw_node_shutdown; 71 static ng_newhook_t ng_btsocket_hci_raw_node_newhook; 72 static ng_connect_t ng_btsocket_hci_raw_node_connect; 73 static ng_rcvdata_t ng_btsocket_hci_raw_node_rcvdata; 74 static ng_disconnect_t ng_btsocket_hci_raw_node_disconnect; 75 76 static void ng_btsocket_hci_raw_input (void *, int); 77 static void ng_btsocket_hci_raw_output(node_p, hook_p, void *, int); 78 static void ng_btsocket_hci_raw_savctl(ng_btsocket_hci_raw_pcb_p, 79 struct mbuf **, 80 struct mbuf *); 81 static int ng_btsocket_hci_raw_filter(ng_btsocket_hci_raw_pcb_p, 82 struct mbuf *, int); 83 84 #define ng_btsocket_hci_raw_wakeup_input_task() \ 85 taskqueue_enqueue(taskqueue_swi_giant, &ng_btsocket_hci_raw_task) 86 87 /* Security filter */ 88 struct ng_btsocket_hci_raw_sec_filter { 89 bitstr_t bit_decl(events, 0xff); 90 bitstr_t bit_decl(commands[0x3f], 0x3ff); 91 }; 92 93 /* Netgraph type descriptor */ 94 static struct ng_type typestruct = { 95 .version = NG_ABI_VERSION, 96 .name = NG_BTSOCKET_HCI_RAW_NODE_TYPE, 97 .constructor = ng_btsocket_hci_raw_node_constructor, 98 .rcvmsg = ng_btsocket_hci_raw_node_rcvmsg, 99 .shutdown = ng_btsocket_hci_raw_node_shutdown, 100 .newhook = ng_btsocket_hci_raw_node_newhook, 101 .connect = ng_btsocket_hci_raw_node_connect, 102 .rcvdata = ng_btsocket_hci_raw_node_rcvdata, 103 .disconnect = ng_btsocket_hci_raw_node_disconnect, 104 }; 105 106 /* Globals */ 107 extern int ifqmaxlen; 108 static u_int32_t ng_btsocket_hci_raw_debug_level; 109 static u_int32_t ng_btsocket_hci_raw_ioctl_timeout; 110 static node_p ng_btsocket_hci_raw_node; 111 static struct ng_bt_itemq ng_btsocket_hci_raw_queue; 112 static struct mtx ng_btsocket_hci_raw_queue_mtx; 113 static struct task ng_btsocket_hci_raw_task; 114 static LIST_HEAD(, ng_btsocket_hci_raw_pcb) ng_btsocket_hci_raw_sockets; 115 static struct mtx ng_btsocket_hci_raw_sockets_mtx; 116 static u_int32_t ng_btsocket_hci_raw_token; 117 static struct mtx ng_btsocket_hci_raw_token_mtx; 118 static struct ng_btsocket_hci_raw_sec_filter *ng_btsocket_hci_raw_sec_filter; 119 120 /* Sysctl tree */ 121 SYSCTL_DECL(_net_bluetooth_hci_sockets); 122 SYSCTL_NODE(_net_bluetooth_hci_sockets, OID_AUTO, raw, CTLFLAG_RW, 123 0, "Bluetooth raw HCI sockets family"); 124 SYSCTL_INT(_net_bluetooth_hci_sockets_raw, OID_AUTO, debug_level, CTLFLAG_RW, 125 &ng_btsocket_hci_raw_debug_level, NG_BTSOCKET_WARN_LEVEL, 126 "Bluetooth raw HCI sockets debug level"); 127 SYSCTL_INT(_net_bluetooth_hci_sockets_raw, OID_AUTO, ioctl_timeout, CTLFLAG_RW, 128 &ng_btsocket_hci_raw_ioctl_timeout, 5, 129 "Bluetooth raw HCI sockets ioctl timeout"); 130 SYSCTL_INT(_net_bluetooth_hci_sockets_raw, OID_AUTO, queue_len, CTLFLAG_RD, 131 &ng_btsocket_hci_raw_queue.len, 0, 132 "Bluetooth raw HCI sockets input queue length"); 133 SYSCTL_INT(_net_bluetooth_hci_sockets_raw, OID_AUTO, queue_maxlen, CTLFLAG_RD, 134 &ng_btsocket_hci_raw_queue.maxlen, 0, 135 "Bluetooth raw HCI sockets input queue max. length"); 136 SYSCTL_INT(_net_bluetooth_hci_sockets_raw, OID_AUTO, queue_drops, CTLFLAG_RD, 137 &ng_btsocket_hci_raw_queue.drops, 0, 138 "Bluetooth raw HCI sockets input queue drops"); 139 140 /* Debug */ 141 #define NG_BTSOCKET_HCI_RAW_INFO \ 142 if (ng_btsocket_hci_raw_debug_level >= NG_BTSOCKET_INFO_LEVEL) \ 143 printf 144 145 #define NG_BTSOCKET_HCI_RAW_WARN \ 146 if (ng_btsocket_hci_raw_debug_level >= NG_BTSOCKET_WARN_LEVEL) \ 147 printf 148 149 #define NG_BTSOCKET_HCI_RAW_ERR \ 150 if (ng_btsocket_hci_raw_debug_level >= NG_BTSOCKET_ERR_LEVEL) \ 151 printf 152 153 #define NG_BTSOCKET_HCI_RAW_ALERT \ 154 if (ng_btsocket_hci_raw_debug_level >= NG_BTSOCKET_ALERT_LEVEL) \ 155 printf 156 157 /**************************************************************************** 158 **************************************************************************** 159 ** Netgraph specific 160 **************************************************************************** 161 ****************************************************************************/ 162 163 /* 164 * Netgraph node constructor. Do not allow to create node of this type. 165 */ 166 167 static int 168 ng_btsocket_hci_raw_node_constructor(node_p node) 169 { 170 return (EINVAL); 171 } /* ng_btsocket_hci_raw_node_constructor */ 172 173 /* 174 * Netgraph node destructor. Just let old node go and create new fresh one. 175 */ 176 177 static int 178 ng_btsocket_hci_raw_node_shutdown(node_p node) 179 { 180 int error = 0; 181 182 NG_NODE_UNREF(node); 183 184 error = ng_make_node_common(&typestruct, &ng_btsocket_hci_raw_node); 185 if (error != 0) { 186 NG_BTSOCKET_HCI_RAW_ALERT( 187 "%s: Could not create Netgraph node, error=%d\n", __func__, error); 188 189 ng_btsocket_hci_raw_node = NULL; 190 191 return (ENOMEM); 192 } 193 194 error = ng_name_node(ng_btsocket_hci_raw_node, 195 NG_BTSOCKET_HCI_RAW_NODE_TYPE); 196 if (error != 0) { 197 NG_BTSOCKET_HCI_RAW_ALERT( 198 "%s: Could not name Netgraph node, error=%d\n", __func__, error); 199 200 NG_NODE_UNREF(ng_btsocket_hci_raw_node); 201 ng_btsocket_hci_raw_node = NULL; 202 203 return (EINVAL); 204 } 205 206 return (0); 207 } /* ng_btsocket_hci_raw_node_shutdown */ 208 209 /* 210 * Create new hook. Just say "yes" 211 */ 212 213 static int 214 ng_btsocket_hci_raw_node_newhook(node_p node, hook_p hook, char const *name) 215 { 216 return (0); 217 } /* ng_btsocket_hci_raw_node_newhook */ 218 219 /* 220 * Connect hook. Just say "yes" 221 */ 222 223 static int 224 ng_btsocket_hci_raw_node_connect(hook_p hook) 225 { 226 return (0); 227 } /* ng_btsocket_hci_raw_node_connect */ 228 229 /* 230 * Disconnect hook 231 */ 232 233 static int 234 ng_btsocket_hci_raw_node_disconnect(hook_p hook) 235 { 236 return (0); 237 } /* ng_btsocket_hci_raw_node_disconnect */ 238 239 /* 240 * Receive control message. 241 * Make sure it is a message from HCI node and it is a response. 242 * Enqueue item and schedule input task. 243 */ 244 245 static int 246 ng_btsocket_hci_raw_node_rcvmsg(node_p node, item_p item, hook_p lasthook) 247 { 248 struct ng_mesg *msg = NGI_MSG(item); /* item still has message */ 249 int empty, error = 0; 250 251 mtx_lock(&ng_btsocket_hci_raw_sockets_mtx); 252 empty = LIST_EMPTY(&ng_btsocket_hci_raw_sockets); 253 mtx_unlock(&ng_btsocket_hci_raw_sockets_mtx); 254 255 if (empty) { 256 NG_FREE_ITEM(item); 257 return (0); 258 } 259 260 if (msg != NULL && 261 msg->header.typecookie == NGM_HCI_COOKIE && 262 msg->header.flags & NGF_RESP) { 263 if (msg->header.token == 0) { 264 NG_FREE_ITEM(item); 265 return (0); 266 } 267 268 mtx_lock(&ng_btsocket_hci_raw_queue_mtx); 269 if (NG_BT_ITEMQ_FULL(&ng_btsocket_hci_raw_queue)) { 270 NG_BTSOCKET_HCI_RAW_ERR( 271 "%s: Input queue is full\n", __func__); 272 273 NG_BT_ITEMQ_DROP(&ng_btsocket_hci_raw_queue); 274 NG_FREE_ITEM(item); 275 error = ENOBUFS; 276 } else { 277 NG_BT_ITEMQ_ENQUEUE(&ng_btsocket_hci_raw_queue, item); 278 error = ng_btsocket_hci_raw_wakeup_input_task(); 279 } 280 mtx_unlock(&ng_btsocket_hci_raw_queue_mtx); 281 } else { 282 NG_FREE_ITEM(item); 283 error = EINVAL; 284 } 285 286 return (error); 287 } /* ng_btsocket_hci_raw_node_rcvmsg */ 288 289 /* 290 * Receive packet from the one of our hook. 291 * Prepend every packet with sockaddr_hci and record sender's node name. 292 * Enqueue item and schedule input task. 293 */ 294 295 static int 296 ng_btsocket_hci_raw_node_rcvdata(hook_p hook, item_p item) 297 { 298 struct mbuf *nam = NULL; 299 int empty, error; 300 301 mtx_lock(&ng_btsocket_hci_raw_sockets_mtx); 302 empty = LIST_EMPTY(&ng_btsocket_hci_raw_sockets); 303 mtx_unlock(&ng_btsocket_hci_raw_sockets_mtx); 304 305 if (empty) { 306 NG_FREE_ITEM(item); 307 return (0); 308 } 309 310 MGET(nam, M_DONTWAIT, MT_SONAME); 311 if (nam != NULL) { 312 struct sockaddr_hci *sa = mtod(nam, struct sockaddr_hci *); 313 314 nam->m_len = sizeof(struct sockaddr_hci); 315 316 sa->hci_len = sizeof(*sa); 317 sa->hci_family = AF_BLUETOOTH; 318 strlcpy(sa->hci_node, NG_PEER_NODE_NAME(hook), 319 sizeof(sa->hci_node)); 320 321 NGI_GET_M(item, nam->m_next); 322 NGI_M(item) = nam; 323 324 mtx_lock(&ng_btsocket_hci_raw_queue_mtx); 325 if (NG_BT_ITEMQ_FULL(&ng_btsocket_hci_raw_queue)) { 326 NG_BTSOCKET_HCI_RAW_ERR( 327 "%s: Input queue is full\n", __func__); 328 329 NG_BT_ITEMQ_DROP(&ng_btsocket_hci_raw_queue); 330 NG_FREE_ITEM(item); 331 error = ENOBUFS; 332 } else { 333 NG_BT_ITEMQ_ENQUEUE(&ng_btsocket_hci_raw_queue, item); 334 error = ng_btsocket_hci_raw_wakeup_input_task(); 335 } 336 mtx_unlock(&ng_btsocket_hci_raw_queue_mtx); 337 } else { 338 NG_BTSOCKET_HCI_RAW_ERR( 339 "%s: Failed to allocate address mbuf\n", __func__); 340 341 NG_FREE_ITEM(item); 342 error = ENOBUFS; 343 } 344 345 return (error); 346 } /* ng_btsocket_hci_raw_node_rcvdata */ 347 348 /**************************************************************************** 349 **************************************************************************** 350 ** Sockets specific 351 **************************************************************************** 352 ****************************************************************************/ 353 354 /* 355 * Get next token. We need token to avoid theoretical race where process 356 * submits ioctl() message then interrupts ioctl() and re-submits another 357 * ioctl() on the same socket *before* first ioctl() complete. 358 */ 359 360 static void 361 ng_btsocket_hci_raw_get_token(u_int32_t *token) 362 { 363 mtx_lock(&ng_btsocket_hci_raw_token_mtx); 364 365 if (++ ng_btsocket_hci_raw_token == 0) 366 ng_btsocket_hci_raw_token = 1; 367 368 *token = ng_btsocket_hci_raw_token; 369 370 mtx_unlock(&ng_btsocket_hci_raw_token_mtx); 371 } /* ng_btsocket_hci_raw_get_token */ 372 373 /* 374 * Send Netgraph message to the node - do not expect reply 375 */ 376 377 static int 378 ng_btsocket_hci_raw_send_ngmsg(char *path, int cmd, void *arg, int arglen) 379 { 380 struct ng_mesg *msg = NULL; 381 int error = 0; 382 383 NG_MKMESSAGE(msg, NGM_HCI_COOKIE, cmd, arglen, M_NOWAIT); 384 if (msg == NULL) 385 return (ENOMEM); 386 387 if (arg != NULL && arglen > 0) 388 bcopy(arg, msg->data, arglen); 389 390 NG_SEND_MSG_PATH(error, ng_btsocket_hci_raw_node, msg, path, 0); 391 392 return (error); 393 } /* ng_btsocket_hci_raw_send_ngmsg */ 394 395 /* 396 * Send Netgraph message to the node (no data) and wait for reply 397 */ 398 399 static int 400 ng_btsocket_hci_raw_send_sync_ngmsg(ng_btsocket_hci_raw_pcb_p pcb, char *path, 401 int cmd, void *rsp, int rsplen) 402 { 403 struct ng_mesg *msg = NULL; 404 int error = 0; 405 406 mtx_assert(&pcb->pcb_mtx, MA_OWNED); 407 408 NG_MKMESSAGE(msg, NGM_HCI_COOKIE, cmd, 0, M_NOWAIT); 409 if (msg == NULL) 410 return (ENOMEM); 411 412 ng_btsocket_hci_raw_get_token(&msg->header.token); 413 pcb->token = msg->header.token; 414 pcb->msg = NULL; 415 416 NG_SEND_MSG_PATH(error, ng_btsocket_hci_raw_node, msg, path, 0); 417 if (error != 0) { 418 pcb->token = 0; 419 return (error); 420 } 421 422 error = msleep(&pcb->msg, &pcb->pcb_mtx, PZERO|PCATCH, "hcictl", 423 ng_btsocket_hci_raw_ioctl_timeout * hz); 424 pcb->token = 0; 425 426 if (error != 0) 427 return (error); 428 429 if (pcb->msg != NULL && pcb->msg->header.cmd == cmd) 430 bcopy(pcb->msg->data, rsp, rsplen); 431 else 432 error = EINVAL; 433 434 NG_FREE_MSG(pcb->msg); /* checks for != NULL */ 435 436 return (0); 437 } /* ng_btsocket_hci_raw_send_sync_ngmsg */ 438 439 /* 440 * Create control information for the packet 441 */ 442 443 static void 444 ng_btsocket_hci_raw_savctl(ng_btsocket_hci_raw_pcb_p pcb, struct mbuf **ctl, 445 struct mbuf *m) 446 { 447 int dir; 448 struct timeval tv; 449 450 mtx_assert(&pcb->pcb_mtx, MA_OWNED); 451 452 if (pcb->flags & NG_BTSOCKET_HCI_RAW_DIRECTION) { 453 dir = (m->m_flags & M_PROTO1)? 1 : 0; 454 *ctl = sbcreatecontrol((caddr_t) &dir, sizeof(dir), 455 SCM_HCI_RAW_DIRECTION, SOL_HCI_RAW); 456 if (*ctl != NULL) 457 ctl = &((*ctl)->m_next); 458 } 459 460 if (pcb->so->so_options & SO_TIMESTAMP) { 461 microtime(&tv); 462 *ctl = sbcreatecontrol((caddr_t) &tv, sizeof(tv), 463 SCM_TIMESTAMP, SOL_SOCKET); 464 if (*ctl != NULL) 465 ctl = &((*ctl)->m_next); 466 } 467 } /* ng_btsocket_hci_raw_savctl */ 468 469 /* 470 * Raw HCI sockets data input routine 471 */ 472 473 static void 474 ng_btsocket_hci_raw_data_input(struct mbuf *nam) 475 { 476 ng_btsocket_hci_raw_pcb_p pcb = NULL; 477 struct mbuf *m0 = NULL, *m = NULL; 478 struct sockaddr_hci *sa = NULL; 479 480 m0 = nam->m_next; 481 nam->m_next = NULL; 482 483 KASSERT((nam->m_type == MT_SONAME), 484 ("%s: m_type=%d\n", __func__, nam->m_type)); 485 KASSERT((m0->m_flags & M_PKTHDR), 486 ("%s: m_flags=%#x\n", __func__, m0->m_flags)); 487 488 sa = mtod(nam, struct sockaddr_hci *); 489 490 mtx_lock(&ng_btsocket_hci_raw_sockets_mtx); 491 492 LIST_FOREACH(pcb, &ng_btsocket_hci_raw_sockets, next) { 493 494 mtx_lock(&pcb->pcb_mtx); 495 496 /* 497 * If socket was bound then check address and 498 * make sure it matches. 499 */ 500 501 if (pcb->addr.hci_node[0] != 0 && 502 strcmp(sa->hci_node, pcb->addr.hci_node) != 0) 503 goto next; 504 505 /* 506 * Check packet against filters 507 * XXX do we have to call m_pullup() here? 508 */ 509 510 if (ng_btsocket_hci_raw_filter(pcb, m0, 1) != 0) 511 goto next; 512 513 /* 514 * Make a copy of the packet, append to the socket's 515 * receive queue and wakeup socket. sbappendaddr() 516 * will check if socket has enough buffer space. 517 */ 518 519 m = m_dup(m0, M_DONTWAIT); 520 if (m != NULL) { 521 struct mbuf *ctl = NULL; 522 523 ng_btsocket_hci_raw_savctl(pcb, &ctl, m); 524 525 if (sbappendaddr(&pcb->so->so_rcv, 526 (struct sockaddr *) sa, m, ctl)) 527 sorwakeup(pcb->so); 528 else { 529 NG_BTSOCKET_HCI_RAW_INFO( 530 "%s: sbappendaddr() failed\n", __func__); 531 532 NG_FREE_M(m); 533 NG_FREE_M(ctl); 534 } 535 } 536 next: 537 mtx_unlock(&pcb->pcb_mtx); 538 } 539 540 mtx_unlock(&ng_btsocket_hci_raw_sockets_mtx); 541 542 NG_FREE_M(nam); 543 NG_FREE_M(m0); 544 } /* ng_btsocket_hci_raw_data_input */ 545 546 /* 547 * Raw HCI sockets message input routine 548 */ 549 550 static void 551 ng_btsocket_hci_raw_msg_input(struct ng_mesg *msg) 552 { 553 ng_btsocket_hci_raw_pcb_p pcb = NULL; 554 555 mtx_lock(&ng_btsocket_hci_raw_sockets_mtx); 556 557 LIST_FOREACH(pcb, &ng_btsocket_hci_raw_sockets, next) { 558 mtx_lock(&pcb->pcb_mtx); 559 560 if (msg->header.token == pcb->token) { 561 pcb->msg = msg; 562 wakeup(&pcb->msg); 563 564 mtx_unlock(&pcb->pcb_mtx); 565 mtx_unlock(&ng_btsocket_hci_raw_sockets_mtx); 566 567 return; 568 } 569 570 mtx_unlock(&pcb->pcb_mtx); 571 } 572 573 mtx_unlock(&ng_btsocket_hci_raw_sockets_mtx); 574 575 NG_FREE_MSG(msg); /* checks for != NULL */ 576 } /* ng_btsocket_hci_raw_msg_input */ 577 578 /* 579 * Raw HCI sockets input routines 580 */ 581 582 static void 583 ng_btsocket_hci_raw_input(void *context, int pending) 584 { 585 item_p item = NULL; 586 587 for (;;) { 588 mtx_lock(&ng_btsocket_hci_raw_queue_mtx); 589 NG_BT_ITEMQ_DEQUEUE(&ng_btsocket_hci_raw_queue, item); 590 mtx_unlock(&ng_btsocket_hci_raw_queue_mtx); 591 592 if (item == NULL) 593 break; 594 595 switch(item->el_flags & NGQF_TYPE) { 596 case NGQF_DATA: { 597 struct mbuf *m = NULL; 598 599 NGI_GET_M(item, m); 600 ng_btsocket_hci_raw_data_input(m); 601 } break; 602 603 case NGQF_MESG: { 604 struct ng_mesg *msg = NULL; 605 606 NGI_GET_MSG(item, msg); 607 ng_btsocket_hci_raw_msg_input(msg); 608 } break; 609 610 default: 611 KASSERT(0, 612 ("%s: invalid item type=%ld\n", __func__, (item->el_flags & NGQF_TYPE))); 613 break; 614 } 615 616 NG_FREE_ITEM(item); 617 } 618 } /* ng_btsocket_hci_raw_input */ 619 620 /* 621 * Raw HCI sockets output routine 622 */ 623 624 static void 625 ng_btsocket_hci_raw_output(node_p node, hook_p hook, void *arg1, int arg2) 626 { 627 struct mbuf *nam = (struct mbuf *) arg1, *m = NULL; 628 struct sockaddr_hci *sa = NULL; 629 int error; 630 631 m = nam->m_next; 632 nam->m_next = NULL; 633 634 KASSERT((nam->m_type == MT_SONAME), 635 ("%s: m_type=%d\n", __func__, nam->m_type)); 636 KASSERT((m->m_flags & M_PKTHDR), 637 ("%s: m_flags=%#x\n", __func__, m->m_flags)); 638 639 sa = mtod(nam, struct sockaddr_hci *); 640 641 /* 642 * Find downstream hook 643 * XXX For now access node hook list directly. Should be safe because 644 * we used ng_send_fn() and we should have exclusive lock on the node. 645 */ 646 647 LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) { 648 if (hook == NULL || NG_HOOK_NOT_VALID(hook) || 649 NG_NODE_NOT_VALID(NG_PEER_NODE(hook))) 650 continue; 651 652 if (strcmp(sa->hci_node, NG_PEER_NODE_NAME(hook)) == 0) { 653 NG_SEND_DATA_ONLY(error, hook, m); /* sets m to NULL */ 654 break; 655 } 656 } 657 658 NG_FREE_M(nam); /* check for != NULL */ 659 NG_FREE_M(m); 660 } /* ng_btsocket_hci_raw_output */ 661 662 /* 663 * Check frame against security and socket filters. 664 * d (direction bit) == 1 means incoming frame. 665 */ 666 667 static int 668 ng_btsocket_hci_raw_filter(ng_btsocket_hci_raw_pcb_p pcb, struct mbuf *m, int d) 669 { 670 int type, event, opcode; 671 672 mtx_assert(&pcb->pcb_mtx, MA_OWNED); 673 674 switch ((type = *mtod(m, u_int8_t *))) { 675 case NG_HCI_CMD_PKT: 676 if (!(pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)) { 677 opcode = le16toh(mtod(m, ng_hci_cmd_pkt_t *)->opcode); 678 679 if (!bit_test( 680 ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF(opcode) - 1], 681 NG_HCI_OCF(opcode) - 1)) 682 return (EPERM); 683 } 684 685 if (d && !bit_test(pcb->filter.packet_mask, NG_HCI_CMD_PKT - 1)) 686 return (EPERM); 687 break; 688 689 case NG_HCI_ACL_DATA_PKT: 690 case NG_HCI_SCO_DATA_PKT: 691 if (!(pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED) || 692 !bit_test(pcb->filter.packet_mask, type - 1) || 693 !d) 694 return (EPERM); 695 break; 696 697 case NG_HCI_EVENT_PKT: 698 if (!d) 699 return (EINVAL); 700 701 event = mtod(m, ng_hci_event_pkt_t *)->event - 1; 702 703 if (!(pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED)) 704 if (!bit_test(ng_btsocket_hci_raw_sec_filter->events, event)) 705 return (EPERM); 706 707 if (!bit_test(pcb->filter.event_mask, event)) 708 return (EPERM); 709 break; 710 711 default: 712 return (EINVAL); 713 } 714 715 return (0); 716 } /* ng_btsocket_hci_raw_filter */ 717 718 /* 719 * Initialize everything 720 */ 721 722 void 723 ng_btsocket_hci_raw_init(void) 724 { 725 bitstr_t *f = NULL; 726 int error = 0; 727 728 ng_btsocket_hci_raw_node = NULL; 729 ng_btsocket_hci_raw_debug_level = NG_BTSOCKET_WARN_LEVEL; 730 ng_btsocket_hci_raw_ioctl_timeout = 5; 731 732 /* Register Netgraph node type */ 733 error = ng_newtype(&typestruct); 734 if (error != 0) { 735 NG_BTSOCKET_HCI_RAW_ALERT( 736 "%s: Could not register Netgraph node type, error=%d\n", __func__, error); 737 738 return; 739 } 740 741 /* Create Netgrapg node */ 742 error = ng_make_node_common(&typestruct, &ng_btsocket_hci_raw_node); 743 if (error != 0) { 744 NG_BTSOCKET_HCI_RAW_ALERT( 745 "%s: Could not create Netgraph node, error=%d\n", __func__, error); 746 747 ng_btsocket_hci_raw_node = NULL; 748 749 return; 750 } 751 752 error = ng_name_node(ng_btsocket_hci_raw_node, 753 NG_BTSOCKET_HCI_RAW_NODE_TYPE); 754 if (error != 0) { 755 NG_BTSOCKET_HCI_RAW_ALERT( 756 "%s: Could not name Netgraph node, error=%d\n", __func__, error); 757 758 NG_NODE_UNREF(ng_btsocket_hci_raw_node); 759 ng_btsocket_hci_raw_node = NULL; 760 761 return; 762 } 763 764 /* Create input queue */ 765 NG_BT_ITEMQ_INIT(&ng_btsocket_hci_raw_queue, ifqmaxlen); 766 mtx_init(&ng_btsocket_hci_raw_queue_mtx, 767 "btsocks_hci_raw_queue_mtx", NULL, MTX_DEF); 768 TASK_INIT(&ng_btsocket_hci_raw_task, 0, 769 ng_btsocket_hci_raw_input, NULL); 770 771 /* Create list of sockets */ 772 LIST_INIT(&ng_btsocket_hci_raw_sockets); 773 mtx_init(&ng_btsocket_hci_raw_sockets_mtx, 774 "btsocks_hci_raw_sockets_mtx", NULL, MTX_DEF); 775 776 /* Tokens */ 777 ng_btsocket_hci_raw_token = 0; 778 mtx_init(&ng_btsocket_hci_raw_token_mtx, 779 "btsocks_hci_raw_token_mtx", NULL, MTX_DEF); 780 781 /* 782 * Security filter 783 * XXX never FREE()ed 784 */ 785 786 ng_btsocket_hci_raw_sec_filter = NULL; 787 788 MALLOC(ng_btsocket_hci_raw_sec_filter, 789 struct ng_btsocket_hci_raw_sec_filter *, 790 sizeof(struct ng_btsocket_hci_raw_sec_filter), 791 M_NETGRAPH_BTSOCKET_HCI_RAW, M_NOWAIT|M_ZERO); 792 if (ng_btsocket_hci_raw_sec_filter == NULL) { 793 printf("%s: Could not allocate security filter!\n", __func__); 794 return; 795 } 796 797 /* 798 * XXX How paranoid can we get? 799 * 800 * Initialize security filter. If bit is set in the mask then 801 * unprivileged socket is allowed to send (receive) this command 802 * (event). 803 */ 804 805 /* Enable all events */ 806 memset(&ng_btsocket_hci_raw_sec_filter->events, 0xff, 807 sizeof(ng_btsocket_hci_raw_sec_filter->events)/ 808 sizeof(ng_btsocket_hci_raw_sec_filter->events[0])); 809 810 /* Disable some critical events */ 811 f = ng_btsocket_hci_raw_sec_filter->events; 812 bit_clear(f, NG_HCI_EVENT_RETURN_LINK_KEYS - 1); 813 bit_clear(f, NG_HCI_EVENT_LINK_KEY_NOTIFICATION - 1); 814 bit_clear(f, NG_HCI_EVENT_VENDOR - 1); 815 816 /* Commands - Link control */ 817 f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_LINK_CONTROL-1]; 818 bit_set(f, NG_HCI_OCF_INQUIRY - 1); 819 bit_set(f, NG_HCI_OCF_INQUIRY_CANCEL - 1); 820 bit_set(f, NG_HCI_OCF_PERIODIC_INQUIRY - 1); 821 bit_set(f, NG_HCI_OCF_EXIT_PERIODIC_INQUIRY - 1); 822 bit_set(f, NG_HCI_OCF_REMOTE_NAME_REQ - 1); 823 bit_set(f, NG_HCI_OCF_READ_REMOTE_FEATURES - 1); 824 bit_set(f, NG_HCI_OCF_READ_REMOTE_VER_INFO - 1); 825 bit_set(f, NG_HCI_OCF_READ_CLOCK_OFFSET - 1); 826 827 /* Commands - Link policy */ 828 f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_LINK_POLICY-1]; 829 bit_set(f, NG_HCI_OCF_ROLE_DISCOVERY - 1); 830 bit_set(f, NG_HCI_OCF_READ_LINK_POLICY_SETTINGS - 1); 831 832 /* Commands - Host controller and baseband */ 833 f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_HC_BASEBAND-1]; 834 bit_set(f, NG_HCI_OCF_READ_PIN_TYPE - 1); 835 bit_set(f, NG_HCI_OCF_READ_LOCAL_NAME - 1); 836 bit_set(f, NG_HCI_OCF_READ_CON_ACCEPT_TIMO - 1); 837 bit_set(f, NG_HCI_OCF_READ_PAGE_TIMO - 1); 838 bit_set(f, NG_HCI_OCF_READ_SCAN_ENABLE - 1); 839 bit_set(f, NG_HCI_OCF_READ_PAGE_SCAN_ACTIVITY - 1); 840 bit_set(f, NG_HCI_OCF_READ_INQUIRY_SCAN_ACTIVITY - 1); 841 bit_set(f, NG_HCI_OCF_READ_AUTH_ENABLE - 1); 842 bit_set(f, NG_HCI_OCF_READ_ENCRYPTION_MODE - 1); 843 bit_set(f, NG_HCI_OCF_READ_UNIT_CLASS - 1); 844 bit_set(f, NG_HCI_OCF_READ_VOICE_SETTINGS - 1); 845 bit_set(f, NG_HCI_OCF_READ_AUTO_FLUSH_TIMO - 1); 846 bit_set(f, NG_HCI_OCF_READ_NUM_BROADCAST_RETRANS - 1); 847 bit_set(f, NG_HCI_OCF_READ_HOLD_MODE_ACTIVITY - 1); 848 bit_set(f, NG_HCI_OCF_READ_XMIT_LEVEL - 1); 849 bit_set(f, NG_HCI_OCF_READ_SCO_FLOW_CONTROL - 1); 850 bit_set(f, NG_HCI_OCF_READ_LINK_SUPERVISION_TIMO - 1); 851 bit_set(f, NG_HCI_OCF_READ_SUPPORTED_IAC_NUM - 1); 852 bit_set(f, NG_HCI_OCF_READ_IAC_LAP - 1); 853 bit_set(f, NG_HCI_OCF_READ_PAGE_SCAN_PERIOD - 1); 854 bit_set(f, NG_HCI_OCF_READ_PAGE_SCAN - 1); 855 856 /* Commands - Informational */ 857 f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_INFO - 1]; 858 bit_set(f, NG_HCI_OCF_READ_LOCAL_VER - 1); 859 bit_set(f, NG_HCI_OCF_READ_LOCAL_FEATURES - 1); 860 bit_set(f, NG_HCI_OCF_READ_BUFFER_SIZE - 1); 861 bit_set(f, NG_HCI_OCF_READ_COUNTRY_CODE - 1); 862 bit_set(f, NG_HCI_OCF_READ_BDADDR - 1); 863 864 /* Commands - Status */ 865 f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_STATUS - 1]; 866 bit_set(f, NG_HCI_OCF_READ_FAILED_CONTACT_CNTR - 1); 867 bit_set(f, NG_HCI_OCF_GET_LINK_QUALITY - 1); 868 bit_set(f, NG_HCI_OCF_READ_RSSI - 1); 869 870 /* Commands - Testing */ 871 f = ng_btsocket_hci_raw_sec_filter->commands[NG_HCI_OGF_TESTING - 1]; 872 bit_set(f, NG_HCI_OCF_READ_LOOPBACK_MODE - 1); 873 } /* ng_btsocket_hci_raw_init */ 874 875 /* 876 * Abort connection on socket 877 */ 878 879 int 880 ng_btsocket_hci_raw_abort(struct socket *so) 881 { 882 return (ng_btsocket_hci_raw_detach(so)); 883 } /* ng_btsocket_hci_raw_abort */ 884 885 /* 886 * Create new raw HCI socket 887 */ 888 889 int 890 ng_btsocket_hci_raw_attach(struct socket *so, int proto, struct thread *td) 891 { 892 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so); 893 int error = 0; 894 895 if (pcb != NULL) 896 return (EISCONN); 897 898 if (ng_btsocket_hci_raw_node == NULL) 899 return (EPROTONOSUPPORT); 900 if (proto != BLUETOOTH_PROTO_HCI) 901 return (EPROTONOSUPPORT); 902 if (so->so_type != SOCK_RAW) 903 return (ESOCKTNOSUPPORT); 904 905 error = soreserve(so, NG_BTSOCKET_HCI_RAW_SENDSPACE, 906 NG_BTSOCKET_HCI_RAW_RECVSPACE); 907 if (error != 0) 908 return (error); 909 910 MALLOC(pcb, ng_btsocket_hci_raw_pcb_p, sizeof(*pcb), 911 M_NETGRAPH_BTSOCKET_HCI_RAW, M_NOWAIT|M_ZERO); 912 if (pcb == NULL) 913 return (ENOMEM); 914 915 so->so_pcb = (caddr_t) pcb; 916 pcb->so = so; 917 918 if (suser(td) == 0) 919 pcb->flags |= NG_BTSOCKET_HCI_RAW_PRIVILEGED; 920 921 /* 922 * Set default socket filter. By default socket only accepts HCI 923 * Command_Complete and Command_Status event packets. 924 */ 925 926 bit_set(pcb->filter.event_mask, NG_HCI_EVENT_COMMAND_COMPL - 1); 927 bit_set(pcb->filter.event_mask, NG_HCI_EVENT_COMMAND_STATUS - 1); 928 929 mtx_init(&pcb->pcb_mtx, "btsocks_hci_raw_pcb_mtx", NULL, MTX_DEF); 930 931 mtx_lock(&ng_btsocket_hci_raw_sockets_mtx); 932 LIST_INSERT_HEAD(&ng_btsocket_hci_raw_sockets, pcb, next); 933 mtx_unlock(&ng_btsocket_hci_raw_sockets_mtx); 934 935 return (0); 936 } /* ng_btsocket_hci_raw_attach */ 937 938 /* 939 * Bind raw HCI socket 940 */ 941 942 int 943 ng_btsocket_hci_raw_bind(struct socket *so, struct sockaddr *nam, 944 struct thread *td) 945 { 946 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so); 947 struct sockaddr_hci *sa = (struct sockaddr_hci *) nam; 948 949 if (pcb == NULL) 950 return (EINVAL); 951 if (ng_btsocket_hci_raw_node == NULL) 952 return (EINVAL); 953 954 if (sa == NULL) 955 return (EINVAL); 956 if (sa->hci_family != AF_BLUETOOTH) 957 return (EAFNOSUPPORT); 958 if (sa->hci_len != sizeof(*sa)) 959 return (EINVAL); 960 if (sa->hci_node[0] == 0) 961 return (EINVAL); 962 963 bcopy(sa, &pcb->addr, sizeof(pcb->addr)); 964 965 return (0); 966 } /* ng_btsocket_hci_raw_bind */ 967 968 /* 969 * Connect raw HCI socket 970 */ 971 972 int 973 ng_btsocket_hci_raw_connect(struct socket *so, struct sockaddr *nam, 974 struct thread *td) 975 { 976 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so); 977 struct sockaddr_hci *sa = (struct sockaddr_hci *) nam; 978 979 if (pcb == NULL) 980 return (EINVAL); 981 if (ng_btsocket_hci_raw_node == NULL) 982 return (EINVAL); 983 984 if (sa == NULL) 985 return (EINVAL); 986 if (sa->hci_family != AF_BLUETOOTH) 987 return (EAFNOSUPPORT); 988 if (sa->hci_len != sizeof(*sa)) 989 return (EINVAL); 990 if (sa->hci_node[0] == 0) 991 return (EDESTADDRREQ); 992 if (bcmp(sa, &pcb->addr, sizeof(pcb->addr)) != 0) 993 return (EADDRNOTAVAIL); 994 995 soisconnected(so); 996 997 return (0); 998 } /* ng_btsocket_hci_raw_connect */ 999 1000 /* 1001 * Process ioctl on socket 1002 */ 1003 1004 int 1005 ng_btsocket_hci_raw_control(struct socket *so, u_long cmd, caddr_t data, 1006 struct ifnet *ifp, struct thread *td) 1007 { 1008 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so); 1009 char path[NG_NODESIZ + 1]; 1010 struct ng_mesg *msg = NULL; 1011 int error = 0; 1012 1013 if (pcb == NULL) 1014 return (EINVAL); 1015 if (ng_btsocket_hci_raw_node == NULL) 1016 return (EINVAL); 1017 1018 mtx_lock(&pcb->pcb_mtx); 1019 1020 /* Check if we have device name */ 1021 if (pcb->addr.hci_node[0] == 0) { 1022 mtx_unlock(&pcb->pcb_mtx); 1023 return (EHOSTUNREACH); 1024 } 1025 1026 /* Check if we have pending ioctl() */ 1027 if (pcb->token != 0) { 1028 mtx_unlock(&pcb->pcb_mtx); 1029 return (EBUSY); 1030 } 1031 1032 snprintf(path, sizeof(path), "%s:", pcb->addr.hci_node); 1033 1034 switch (cmd) { 1035 case SIOC_HCI_RAW_NODE_GET_STATE: { 1036 struct ng_btsocket_hci_raw_node_state *p = 1037 (struct ng_btsocket_hci_raw_node_state *) data; 1038 1039 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path, 1040 NGM_HCI_NODE_GET_STATE, 1041 &p->state, sizeof(p->state)); 1042 } break; 1043 1044 case SIOC_HCI_RAW_NODE_INIT: 1045 if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED) 1046 error = ng_btsocket_hci_raw_send_ngmsg(path, 1047 NGM_HCI_NODE_INIT, NULL, 0); 1048 else 1049 error = EPERM; 1050 break; 1051 1052 case SIOC_HCI_RAW_NODE_GET_DEBUG: { 1053 struct ng_btsocket_hci_raw_node_debug *p = 1054 (struct ng_btsocket_hci_raw_node_debug *) data; 1055 1056 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path, 1057 NGM_HCI_NODE_GET_DEBUG, 1058 &p->debug, sizeof(p->debug)); 1059 } break; 1060 1061 case SIOC_HCI_RAW_NODE_SET_DEBUG: { 1062 struct ng_btsocket_hci_raw_node_debug *p = 1063 (struct ng_btsocket_hci_raw_node_debug *) data; 1064 1065 if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED) 1066 error = ng_btsocket_hci_raw_send_ngmsg(path, 1067 NGM_HCI_NODE_SET_DEBUG, &p->debug, 1068 sizeof(p->debug)); 1069 else 1070 error = EPERM; 1071 } break; 1072 1073 case SIOC_HCI_RAW_NODE_GET_BUFFER: { 1074 struct ng_btsocket_hci_raw_node_buffer *p = 1075 (struct ng_btsocket_hci_raw_node_buffer *) data; 1076 1077 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path, 1078 NGM_HCI_NODE_GET_BUFFER, 1079 &p->buffer, sizeof(p->buffer)); 1080 } break; 1081 1082 case SIOC_HCI_RAW_NODE_GET_BDADDR: { 1083 struct ng_btsocket_hci_raw_node_bdaddr *p = 1084 (struct ng_btsocket_hci_raw_node_bdaddr *) data; 1085 1086 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path, 1087 NGM_HCI_NODE_GET_BDADDR, 1088 &p->bdaddr, sizeof(p->bdaddr)); 1089 } break; 1090 1091 case SIOC_HCI_RAW_NODE_GET_FEATURES: { 1092 struct ng_btsocket_hci_raw_node_features *p = 1093 (struct ng_btsocket_hci_raw_node_features *) data; 1094 1095 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path, 1096 NGM_HCI_NODE_GET_FEATURES, 1097 &p->features, sizeof(p->features)); 1098 } break; 1099 1100 case SIOC_HCI_RAW_NODE_GET_STAT: { 1101 struct ng_btsocket_hci_raw_node_stat *p = 1102 (struct ng_btsocket_hci_raw_node_stat *) data; 1103 1104 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path, 1105 NGM_HCI_NODE_GET_STAT, 1106 &p->stat, sizeof(p->stat)); 1107 } break; 1108 1109 case SIOC_HCI_RAW_NODE_RESET_STAT: 1110 if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED) 1111 error = ng_btsocket_hci_raw_send_ngmsg(path, 1112 NGM_HCI_NODE_RESET_STAT, NULL, 0); 1113 else 1114 error = EPERM; 1115 break; 1116 1117 case SIOC_HCI_RAW_NODE_FLUSH_NEIGHBOR_CACHE: 1118 if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED) 1119 error = ng_btsocket_hci_raw_send_ngmsg(path, 1120 NGM_HCI_NODE_FLUSH_NEIGHBOR_CACHE, 1121 NULL, 0); 1122 else 1123 error = EPERM; 1124 break; 1125 1126 case SIOC_HCI_RAW_NODE_GET_NEIGHBOR_CACHE: { 1127 struct ng_btsocket_hci_raw_node_neighbor_cache *p = 1128 (struct ng_btsocket_hci_raw_node_neighbor_cache *) data; 1129 ng_hci_node_get_neighbor_cache_ep *p1 = NULL; 1130 ng_hci_node_neighbor_cache_entry_ep *p2 = NULL; 1131 1132 if (p->num_entries <= 0 || 1133 p->num_entries > NG_HCI_MAX_NEIGHBOR_NUM || 1134 p->entries == NULL) { 1135 error = EINVAL; 1136 break; 1137 } 1138 1139 NG_MKMESSAGE(msg, NGM_HCI_COOKIE, 1140 NGM_HCI_NODE_GET_NEIGHBOR_CACHE, 0, M_NOWAIT); 1141 if (msg == NULL) { 1142 error = ENOMEM; 1143 break; 1144 } 1145 ng_btsocket_hci_raw_get_token(&msg->header.token); 1146 pcb->token = msg->header.token; 1147 pcb->msg = NULL; 1148 1149 NG_SEND_MSG_PATH(error, ng_btsocket_hci_raw_node, msg, path, 0); 1150 if (error != 0) { 1151 pcb->token = 0; 1152 break; 1153 } 1154 1155 error = msleep(&pcb->msg, &pcb->pcb_mtx, 1156 PZERO|PCATCH, "hcictl", 1157 ng_btsocket_hci_raw_ioctl_timeout * hz); 1158 pcb->token = 0; 1159 1160 if (error != 0) 1161 break; 1162 1163 if (pcb->msg != NULL && 1164 pcb->msg->header.cmd == NGM_HCI_NODE_GET_NEIGHBOR_CACHE) { 1165 /* Return data back to user space */ 1166 p1 = (ng_hci_node_get_neighbor_cache_ep *) 1167 (pcb->msg->data); 1168 p2 = (ng_hci_node_neighbor_cache_entry_ep *) 1169 (p1 + 1); 1170 1171 p->num_entries = min(p->num_entries, p1->num_entries); 1172 if (p->num_entries > 0) 1173 error = copyout((caddr_t) p2, 1174 (caddr_t) p->entries, 1175 p->num_entries * sizeof(*p2)); 1176 } else 1177 error = EINVAL; 1178 1179 NG_FREE_MSG(pcb->msg); /* checks for != NULL */ 1180 }break; 1181 1182 case SIOC_HCI_RAW_NODE_GET_CON_LIST: { 1183 struct ng_btsocket_hci_raw_con_list *p = 1184 (struct ng_btsocket_hci_raw_con_list *) data; 1185 ng_hci_node_con_list_ep *p1 = NULL; 1186 ng_hci_node_con_ep *p2 = NULL; 1187 1188 if (p->num_connections == 0 || 1189 p->num_connections > NG_HCI_MAX_CON_NUM || 1190 p->connections == NULL) { 1191 error = EINVAL; 1192 break; 1193 } 1194 1195 NG_MKMESSAGE(msg, NGM_HCI_COOKIE, NGM_HCI_NODE_GET_CON_LIST, 1196 0, M_NOWAIT); 1197 if (msg == NULL) { 1198 error = ENOMEM; 1199 break; 1200 } 1201 ng_btsocket_hci_raw_get_token(&msg->header.token); 1202 pcb->token = msg->header.token; 1203 pcb->msg = NULL; 1204 1205 NG_SEND_MSG_PATH(error, ng_btsocket_hci_raw_node, msg, path, 0); 1206 if (error != 0) { 1207 pcb->token = 0; 1208 break; 1209 } 1210 1211 error = msleep(&pcb->msg, &pcb->pcb_mtx, 1212 PZERO|PCATCH, "hcictl", 1213 ng_btsocket_hci_raw_ioctl_timeout * hz); 1214 pcb->token = 0; 1215 1216 if (error != 0) 1217 break; 1218 1219 if (pcb->msg != NULL && 1220 pcb->msg->header.cmd == NGM_HCI_NODE_GET_CON_LIST) { 1221 /* Return data back to user space */ 1222 p1 = (ng_hci_node_con_list_ep *)(pcb->msg->data); 1223 p2 = (ng_hci_node_con_ep *)(p1 + 1); 1224 1225 p->num_connections = min(p->num_connections, 1226 p1->num_connections); 1227 if (p->num_connections > 0) 1228 error = copyout((caddr_t) p2, 1229 (caddr_t) p->connections, 1230 p->num_connections * sizeof(*p2)); 1231 } else 1232 error = EINVAL; 1233 1234 NG_FREE_MSG(pcb->msg); /* checks for != NULL */ 1235 } break; 1236 1237 case SIOC_HCI_RAW_NODE_GET_LINK_POLICY_MASK: { 1238 struct ng_btsocket_hci_raw_node_link_policy_mask *p = 1239 (struct ng_btsocket_hci_raw_node_link_policy_mask *) 1240 data; 1241 1242 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path, 1243 NGM_HCI_NODE_GET_LINK_POLICY_SETTINGS_MASK, 1244 &p->policy_mask, sizeof(p->policy_mask)); 1245 } break; 1246 1247 case SIOC_HCI_RAW_NODE_SET_LINK_POLICY_MASK: { 1248 struct ng_btsocket_hci_raw_node_link_policy_mask *p = 1249 (struct ng_btsocket_hci_raw_node_link_policy_mask *) 1250 data; 1251 1252 if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED) 1253 error = ng_btsocket_hci_raw_send_ngmsg(path, 1254 NGM_HCI_NODE_SET_LINK_POLICY_SETTINGS_MASK, 1255 &p->policy_mask, 1256 sizeof(p->policy_mask)); 1257 else 1258 error = EPERM; 1259 } break; 1260 1261 case SIOC_HCI_RAW_NODE_GET_PACKET_MASK: { 1262 struct ng_btsocket_hci_raw_node_packet_mask *p = 1263 (struct ng_btsocket_hci_raw_node_packet_mask *) data; 1264 1265 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path, 1266 NGM_HCI_NODE_GET_PACKET_MASK, 1267 &p->packet_mask, sizeof(p->packet_mask)); 1268 } break; 1269 1270 case SIOC_HCI_RAW_NODE_SET_PACKET_MASK: { 1271 struct ng_btsocket_hci_raw_node_packet_mask *p = 1272 (struct ng_btsocket_hci_raw_node_packet_mask *) data; 1273 1274 if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED) 1275 error = ng_btsocket_hci_raw_send_ngmsg(path, 1276 NGM_HCI_NODE_SET_PACKET_MASK, 1277 &p->packet_mask, 1278 sizeof(p->packet_mask)); 1279 else 1280 error = EPERM; 1281 } break; 1282 1283 case SIOC_HCI_RAW_NODE_GET_ROLE_SWITCH: { 1284 struct ng_btsocket_hci_raw_node_role_switch *p = 1285 (struct ng_btsocket_hci_raw_node_role_switch *) data; 1286 1287 error = ng_btsocket_hci_raw_send_sync_ngmsg(pcb, path, 1288 NGM_HCI_NODE_GET_ROLE_SWITCH, 1289 &p->role_switch, sizeof(p->role_switch)); 1290 } break; 1291 1292 case SIOC_HCI_RAW_NODE_SET_ROLE_SWITCH: { 1293 struct ng_btsocket_hci_raw_node_role_switch *p = 1294 (struct ng_btsocket_hci_raw_node_role_switch *) data; 1295 1296 if (pcb->flags & NG_BTSOCKET_HCI_RAW_PRIVILEGED) 1297 error = ng_btsocket_hci_raw_send_ngmsg(path, 1298 NGM_HCI_NODE_SET_ROLE_SWITCH, 1299 &p->role_switch, 1300 sizeof(p->role_switch)); 1301 else 1302 error = EPERM; 1303 } break; 1304 1305 default: 1306 error = EINVAL; 1307 break; 1308 } 1309 1310 mtx_unlock(&pcb->pcb_mtx); 1311 1312 return (error); 1313 } /* ng_btsocket_hci_raw_control */ 1314 1315 /* 1316 * Process getsockopt/setsockopt system calls 1317 */ 1318 1319 int 1320 ng_btsocket_hci_raw_ctloutput(struct socket *so, struct sockopt *sopt) 1321 { 1322 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so); 1323 struct ng_btsocket_hci_raw_filter filter; 1324 int error = 0, dir; 1325 1326 if (pcb == NULL) 1327 return (EINVAL); 1328 if (ng_btsocket_hci_raw_node == NULL) 1329 return (EINVAL); 1330 1331 if (sopt->sopt_level != SOL_HCI_RAW) 1332 return (0); 1333 1334 mtx_lock(&pcb->pcb_mtx); 1335 1336 switch (sopt->sopt_dir) { 1337 case SOPT_GET: 1338 switch (sopt->sopt_name) { 1339 case SO_HCI_RAW_FILTER: 1340 error = sooptcopyout(sopt, &pcb->filter, 1341 sizeof(pcb->filter)); 1342 break; 1343 1344 case SO_HCI_RAW_DIRECTION: 1345 dir = (pcb->flags & NG_BTSOCKET_HCI_RAW_DIRECTION)?1:0; 1346 error = sooptcopyout(sopt, &dir, sizeof(dir)); 1347 break; 1348 1349 default: 1350 error = EINVAL; 1351 break; 1352 } 1353 break; 1354 1355 case SOPT_SET: 1356 switch (sopt->sopt_name) { 1357 case SO_HCI_RAW_FILTER: 1358 error = sooptcopyin(sopt, &filter, sizeof(filter), 1359 sizeof(filter)); 1360 if (error == 0) 1361 bcopy(&filter, &pcb->filter, 1362 sizeof(pcb->filter)); 1363 break; 1364 1365 case SO_HCI_RAW_DIRECTION: 1366 error = sooptcopyin(sopt, &dir, sizeof(dir), 1367 sizeof(dir)); 1368 if (error != 0) 1369 break; 1370 1371 if (dir) 1372 pcb->flags |= NG_BTSOCKET_HCI_RAW_DIRECTION; 1373 else 1374 pcb->flags &= ~NG_BTSOCKET_HCI_RAW_DIRECTION; 1375 break; 1376 1377 default: 1378 error = EINVAL; 1379 break; 1380 } 1381 break; 1382 1383 default: 1384 error = EINVAL; 1385 break; 1386 } 1387 1388 mtx_unlock(&pcb->pcb_mtx); 1389 1390 return (error); 1391 } /* ng_btsocket_hci_raw_ctloutput */ 1392 1393 /* 1394 * Detach raw HCI socket 1395 */ 1396 1397 int 1398 ng_btsocket_hci_raw_detach(struct socket *so) 1399 { 1400 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so); 1401 1402 if (pcb == NULL) 1403 return (EINVAL); 1404 if (ng_btsocket_hci_raw_node == NULL) 1405 return (EINVAL); 1406 1407 mtx_lock(&ng_btsocket_hci_raw_sockets_mtx); 1408 mtx_lock(&pcb->pcb_mtx); 1409 1410 LIST_REMOVE(pcb, next); 1411 1412 mtx_unlock(&pcb->pcb_mtx); 1413 mtx_unlock(&ng_btsocket_hci_raw_sockets_mtx); 1414 1415 mtx_destroy(&pcb->pcb_mtx); 1416 1417 bzero(pcb, sizeof(*pcb)); 1418 FREE(pcb, M_NETGRAPH_BTSOCKET_HCI_RAW); 1419 1420 SOCK_LOCK(so); 1421 so->so_pcb = NULL; 1422 sotryfree(so); 1423 1424 return (0); 1425 } /* ng_btsocket_hci_raw_detach */ 1426 1427 /* 1428 * Disconnect raw HCI socket 1429 */ 1430 1431 int 1432 ng_btsocket_hci_raw_disconnect(struct socket *so) 1433 { 1434 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so); 1435 1436 if (pcb == NULL) 1437 return (EINVAL); 1438 if (ng_btsocket_hci_raw_node == NULL) 1439 return (EINVAL); 1440 1441 soisdisconnected(so); 1442 1443 return (0); 1444 } /* ng_btsocket_hci_raw_disconnect */ 1445 1446 /* 1447 * Get socket peer's address 1448 */ 1449 1450 int 1451 ng_btsocket_hci_raw_peeraddr(struct socket *so, struct sockaddr **nam) 1452 { 1453 return (ng_btsocket_hci_raw_sockaddr(so, nam)); 1454 } /* ng_btsocket_hci_raw_peeraddr */ 1455 1456 /* 1457 * Send data 1458 */ 1459 1460 int 1461 ng_btsocket_hci_raw_send(struct socket *so, int flags, struct mbuf *m, 1462 struct sockaddr *sa, struct mbuf *control, struct thread *td) 1463 { 1464 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so); 1465 struct mbuf *nam = NULL; 1466 int error = 0; 1467 1468 if (ng_btsocket_hci_raw_node == NULL) { 1469 error = ENETDOWN; 1470 goto drop; 1471 } 1472 if (pcb == NULL) { 1473 error = EINVAL; 1474 goto drop; 1475 } 1476 if (control != NULL) { 1477 error = EINVAL; 1478 goto drop; 1479 } 1480 1481 if (m->m_pkthdr.len < sizeof(ng_hci_cmd_pkt_t) || 1482 m->m_pkthdr.len > sizeof(ng_hci_cmd_pkt_t) + NG_HCI_CMD_PKT_SIZE) { 1483 error = EMSGSIZE; 1484 goto drop; 1485 } 1486 1487 if (m->m_len < sizeof(ng_hci_cmd_pkt_t)) { 1488 if ((m = m_pullup(m, sizeof(ng_hci_cmd_pkt_t))) == NULL) { 1489 error = ENOBUFS; 1490 goto drop; 1491 } 1492 } 1493 if (*mtod(m, u_int8_t *) != NG_HCI_CMD_PKT) { 1494 error = ENOTSUP; 1495 goto drop; 1496 } 1497 1498 mtx_lock(&pcb->pcb_mtx); 1499 1500 error = ng_btsocket_hci_raw_filter(pcb, m, 0); 1501 if (error != 0) { 1502 mtx_unlock(&pcb->pcb_mtx); 1503 goto drop; 1504 } 1505 1506 if (sa == NULL) { 1507 if (pcb->addr.hci_node[0] == 0) { 1508 mtx_unlock(&pcb->pcb_mtx); 1509 error = EDESTADDRREQ; 1510 goto drop; 1511 } 1512 1513 sa = (struct sockaddr *) &pcb->addr; 1514 } 1515 1516 MGET(nam, M_DONTWAIT, MT_SONAME); 1517 if (nam == NULL) { 1518 mtx_unlock(&pcb->pcb_mtx); 1519 error = ENOBUFS; 1520 goto drop; 1521 } 1522 1523 nam->m_len = sizeof(struct sockaddr_hci); 1524 bcopy(sa,mtod(nam, struct sockaddr_hci *),sizeof(struct sockaddr_hci)); 1525 1526 nam->m_next = m; 1527 m = NULL; 1528 1529 mtx_unlock(&pcb->pcb_mtx); 1530 1531 return (ng_send_fn(ng_btsocket_hci_raw_node, NULL, 1532 ng_btsocket_hci_raw_output, nam, 0)); 1533 drop: 1534 NG_FREE_M(control); /* NG_FREE_M checks for != NULL */ 1535 NG_FREE_M(nam); 1536 NG_FREE_M(m); 1537 1538 return (error); 1539 } /* ng_btsocket_hci_raw_send */ 1540 1541 /* 1542 * Get socket address 1543 */ 1544 1545 int 1546 ng_btsocket_hci_raw_sockaddr(struct socket *so, struct sockaddr **nam) 1547 { 1548 ng_btsocket_hci_raw_pcb_p pcb = so2hci_raw_pcb(so); 1549 struct sockaddr_hci sa; 1550 1551 if (pcb == NULL) 1552 return (EINVAL); 1553 if (ng_btsocket_hci_raw_node == NULL) 1554 return (EINVAL); 1555 1556 bzero(&sa, sizeof(sa)); 1557 sa.hci_len = sizeof(sa); 1558 sa.hci_family = AF_BLUETOOTH; 1559 strlcpy(sa.hci_node, pcb->addr.hci_node, sizeof(sa.hci_node)); 1560 1561 *nam = sodupsockaddr((struct sockaddr *) &sa, M_NOWAIT); 1562 1563 return ((*nam == NULL)? ENOMEM : 0); 1564 } /* ng_btsocket_hci_raw_sockaddr */ 1565 1566