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