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