1 /* 2 * ng_btsocket_l2cap_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_l2cap_raw.c,v 1.12 2003/09/14 23:29:06 max Exp $ 31 * $FreeBSD$ 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/errno.h> 39 #include <sys/filedesc.h> 40 #include <sys/ioccom.h> 41 #include <sys/kernel.h> 42 #include <sys/lock.h> 43 #include <sys/malloc.h> 44 #include <sys/mbuf.h> 45 #include <sys/mutex.h> 46 #include <sys/priv.h> 47 #include <sys/protosw.h> 48 #include <sys/queue.h> 49 #include <sys/socket.h> 50 #include <sys/socketvar.h> 51 #include <sys/sysctl.h> 52 #include <sys/taskqueue.h> 53 #include <netgraph/ng_message.h> 54 #include <netgraph/netgraph.h> 55 #include <netgraph/bluetooth/include/ng_bluetooth.h> 56 #include <netgraph/bluetooth/include/ng_hci.h> 57 #include <netgraph/bluetooth/include/ng_l2cap.h> 58 #include <netgraph/bluetooth/include/ng_btsocket.h> 59 #include <netgraph/bluetooth/include/ng_btsocket_l2cap.h> 60 61 /* MALLOC define */ 62 #ifdef NG_SEPARATE_MALLOC 63 static MALLOC_DEFINE(M_NETGRAPH_BTSOCKET_L2CAP_RAW, 64 "netgraph_btsocks_l2cap_raw", "Netgraph Bluetooth raw L2CAP sockets"); 65 #else 66 #define M_NETGRAPH_BTSOCKET_L2CAP_RAW M_NETGRAPH 67 #endif /* NG_SEPARATE_MALLOC */ 68 69 /* Netgraph node methods */ 70 static ng_constructor_t ng_btsocket_l2cap_raw_node_constructor; 71 static ng_rcvmsg_t ng_btsocket_l2cap_raw_node_rcvmsg; 72 static ng_shutdown_t ng_btsocket_l2cap_raw_node_shutdown; 73 static ng_newhook_t ng_btsocket_l2cap_raw_node_newhook; 74 static ng_connect_t ng_btsocket_l2cap_raw_node_connect; 75 static ng_rcvdata_t ng_btsocket_l2cap_raw_node_rcvdata; 76 static ng_disconnect_t ng_btsocket_l2cap_raw_node_disconnect; 77 78 static void ng_btsocket_l2cap_raw_input (void *, int); 79 static void ng_btsocket_l2cap_raw_rtclean (void *, int); 80 static void ng_btsocket_l2cap_raw_get_token (u_int32_t *); 81 82 static int ng_btsocket_l2cap_raw_send_ngmsg 83 (hook_p, int, void *, int); 84 static int ng_btsocket_l2cap_raw_send_sync_ngmsg 85 (ng_btsocket_l2cap_raw_pcb_p, int, void *, int); 86 87 #define ng_btsocket_l2cap_raw_wakeup_input_task() \ 88 taskqueue_enqueue(taskqueue_swi, &ng_btsocket_l2cap_raw_queue_task) 89 90 #define ng_btsocket_l2cap_raw_wakeup_route_task() \ 91 taskqueue_enqueue(taskqueue_swi, &ng_btsocket_l2cap_raw_rt_task) 92 93 /* Netgraph type descriptor */ 94 static struct ng_type typestruct = { 95 .version = NG_ABI_VERSION, 96 .name = NG_BTSOCKET_L2CAP_RAW_NODE_TYPE, 97 .constructor = ng_btsocket_l2cap_raw_node_constructor, 98 .rcvmsg = ng_btsocket_l2cap_raw_node_rcvmsg, 99 .shutdown = ng_btsocket_l2cap_raw_node_shutdown, 100 .newhook = ng_btsocket_l2cap_raw_node_newhook, 101 .connect = ng_btsocket_l2cap_raw_node_connect, 102 .rcvdata = ng_btsocket_l2cap_raw_node_rcvdata, 103 .disconnect = ng_btsocket_l2cap_raw_node_disconnect, 104 }; 105 106 /* Globals */ 107 extern int ifqmaxlen; 108 static u_int32_t ng_btsocket_l2cap_raw_debug_level; 109 static u_int32_t ng_btsocket_l2cap_raw_ioctl_timeout; 110 static node_p ng_btsocket_l2cap_raw_node; 111 static struct ng_bt_itemq ng_btsocket_l2cap_raw_queue; 112 static struct mtx ng_btsocket_l2cap_raw_queue_mtx; 113 static struct task ng_btsocket_l2cap_raw_queue_task; 114 static LIST_HEAD(, ng_btsocket_l2cap_raw_pcb) ng_btsocket_l2cap_raw_sockets; 115 static struct mtx ng_btsocket_l2cap_raw_sockets_mtx; 116 static u_int32_t ng_btsocket_l2cap_raw_token; 117 static struct mtx ng_btsocket_l2cap_raw_token_mtx; 118 static LIST_HEAD(, ng_btsocket_l2cap_rtentry) ng_btsocket_l2cap_raw_rt; 119 static struct mtx ng_btsocket_l2cap_raw_rt_mtx; 120 static struct task ng_btsocket_l2cap_raw_rt_task; 121 static struct timeval ng_btsocket_l2cap_raw_lasttime; 122 static int ng_btsocket_l2cap_raw_curpps; 123 124 /* Sysctl tree */ 125 SYSCTL_DECL(_net_bluetooth_l2cap_sockets); 126 static SYSCTL_NODE(_net_bluetooth_l2cap_sockets, OID_AUTO, raw, CTLFLAG_RW, 127 0, "Bluetooth raw L2CAP sockets family"); 128 SYSCTL_UINT(_net_bluetooth_l2cap_sockets_raw, OID_AUTO, debug_level, 129 CTLFLAG_RW, 130 &ng_btsocket_l2cap_raw_debug_level, NG_BTSOCKET_WARN_LEVEL, 131 "Bluetooth raw L2CAP sockets debug level"); 132 SYSCTL_UINT(_net_bluetooth_l2cap_sockets_raw, OID_AUTO, ioctl_timeout, 133 CTLFLAG_RW, 134 &ng_btsocket_l2cap_raw_ioctl_timeout, 5, 135 "Bluetooth raw L2CAP sockets ioctl timeout"); 136 SYSCTL_UINT(_net_bluetooth_l2cap_sockets_raw, OID_AUTO, queue_len, 137 CTLFLAG_RD, 138 &ng_btsocket_l2cap_raw_queue.len, 0, 139 "Bluetooth raw L2CAP sockets input queue length"); 140 SYSCTL_UINT(_net_bluetooth_l2cap_sockets_raw, OID_AUTO, queue_maxlen, 141 CTLFLAG_RD, 142 &ng_btsocket_l2cap_raw_queue.maxlen, 0, 143 "Bluetooth raw L2CAP sockets input queue max. length"); 144 SYSCTL_UINT(_net_bluetooth_l2cap_sockets_raw, OID_AUTO, queue_drops, 145 CTLFLAG_RD, 146 &ng_btsocket_l2cap_raw_queue.drops, 0, 147 "Bluetooth raw L2CAP sockets input queue drops"); 148 149 /* Debug */ 150 #define NG_BTSOCKET_L2CAP_RAW_INFO \ 151 if (ng_btsocket_l2cap_raw_debug_level >= NG_BTSOCKET_INFO_LEVEL && \ 152 ppsratecheck(&ng_btsocket_l2cap_raw_lasttime, &ng_btsocket_l2cap_raw_curpps, 1)) \ 153 printf 154 155 #define NG_BTSOCKET_L2CAP_RAW_WARN \ 156 if (ng_btsocket_l2cap_raw_debug_level >= NG_BTSOCKET_WARN_LEVEL && \ 157 ppsratecheck(&ng_btsocket_l2cap_raw_lasttime, &ng_btsocket_l2cap_raw_curpps, 1)) \ 158 printf 159 160 #define NG_BTSOCKET_L2CAP_RAW_ERR \ 161 if (ng_btsocket_l2cap_raw_debug_level >= NG_BTSOCKET_ERR_LEVEL && \ 162 ppsratecheck(&ng_btsocket_l2cap_raw_lasttime, &ng_btsocket_l2cap_raw_curpps, 1)) \ 163 printf 164 165 #define NG_BTSOCKET_L2CAP_RAW_ALERT \ 166 if (ng_btsocket_l2cap_raw_debug_level >= NG_BTSOCKET_ALERT_LEVEL && \ 167 ppsratecheck(&ng_btsocket_l2cap_raw_lasttime, &ng_btsocket_l2cap_raw_curpps, 1)) \ 168 printf 169 170 /***************************************************************************** 171 ***************************************************************************** 172 ** Netgraph node interface 173 ***************************************************************************** 174 *****************************************************************************/ 175 176 /* 177 * Netgraph node constructor. Do not allow to create node of this type. 178 */ 179 180 static int 181 ng_btsocket_l2cap_raw_node_constructor(node_p node) 182 { 183 return (EINVAL); 184 } /* ng_btsocket_l2cap_raw_node_constructor */ 185 186 /* 187 * Do local shutdown processing. Let old node go and create new fresh one. 188 */ 189 190 static int 191 ng_btsocket_l2cap_raw_node_shutdown(node_p node) 192 { 193 int error = 0; 194 195 NG_NODE_UNREF(node); 196 197 /* Create new node */ 198 error = ng_make_node_common(&typestruct, &ng_btsocket_l2cap_raw_node); 199 if (error != 0) { 200 NG_BTSOCKET_L2CAP_RAW_ALERT( 201 "%s: Could not create Netgraph node, error=%d\n", __func__, error); 202 203 ng_btsocket_l2cap_raw_node = NULL; 204 205 return (error); 206 } 207 208 error = ng_name_node(ng_btsocket_l2cap_raw_node, 209 NG_BTSOCKET_L2CAP_RAW_NODE_TYPE); 210 if (error != 0) { 211 NG_BTSOCKET_L2CAP_RAW_ALERT( 212 "%s: Could not name Netgraph node, error=%d\n", __func__, error); 213 214 NG_NODE_UNREF(ng_btsocket_l2cap_raw_node); 215 ng_btsocket_l2cap_raw_node = NULL; 216 217 return (error); 218 } 219 220 return (0); 221 } /* ng_btsocket_l2cap_raw_node_shutdown */ 222 223 /* 224 * We allow any hook to be connected to the node. 225 */ 226 227 static int 228 ng_btsocket_l2cap_raw_node_newhook(node_p node, hook_p hook, char const *name) 229 { 230 return (0); 231 } /* ng_btsocket_l2cap_raw_node_newhook */ 232 233 /* 234 * Just say "YEP, that's OK by me!" 235 */ 236 237 static int 238 ng_btsocket_l2cap_raw_node_connect(hook_p hook) 239 { 240 NG_HOOK_SET_PRIVATE(hook, NULL); 241 NG_HOOK_REF(hook); /* Keep extra reference to the hook */ 242 243 return (0); 244 } /* ng_btsocket_l2cap_raw_node_connect */ 245 246 /* 247 * Hook disconnection. Schedule route cleanup task 248 */ 249 250 static int 251 ng_btsocket_l2cap_raw_node_disconnect(hook_p hook) 252 { 253 /* 254 * If hook has private information than we must have this hook in 255 * the routing table and must schedule cleaning for the routing table. 256 * Otherwise hook was connected but we never got "hook_info" message, 257 * so we have never added this hook to the routing table and it save 258 * to just delete it. 259 */ 260 261 if (NG_HOOK_PRIVATE(hook) != NULL) 262 return (ng_btsocket_l2cap_raw_wakeup_route_task()); 263 264 NG_HOOK_UNREF(hook); /* Remove extra reference */ 265 266 return (0); 267 } /* ng_btsocket_l2cap_raw_node_disconnect */ 268 269 /* 270 * Process incoming messages 271 */ 272 273 static int 274 ng_btsocket_l2cap_raw_node_rcvmsg(node_p node, item_p item, hook_p hook) 275 { 276 struct ng_mesg *msg = NGI_MSG(item); /* item still has message */ 277 int error = 0; 278 279 if (msg != NULL && msg->header.typecookie == NGM_L2CAP_COOKIE) { 280 281 /* 282 * NGM_L2CAP_NODE_HOOK_INFO is special message initiated by 283 * L2CAP layer. Ignore all other messages if they are not 284 * replies or token is zero 285 */ 286 287 if (msg->header.cmd != NGM_L2CAP_NODE_HOOK_INFO) { 288 if (msg->header.token == 0 || 289 !(msg->header.flags & NGF_RESP)) { 290 NG_FREE_ITEM(item); 291 return (0); 292 } 293 } 294 295 mtx_lock(&ng_btsocket_l2cap_raw_queue_mtx); 296 if (NG_BT_ITEMQ_FULL(&ng_btsocket_l2cap_raw_queue)) { 297 NG_BTSOCKET_L2CAP_RAW_ERR( 298 "%s: Input queue is full\n", __func__); 299 300 NG_BT_ITEMQ_DROP(&ng_btsocket_l2cap_raw_queue); 301 NG_FREE_ITEM(item); 302 error = ENOBUFS; 303 } else { 304 if (hook != NULL) { 305 NG_HOOK_REF(hook); 306 NGI_SET_HOOK(item, hook); 307 } 308 309 NG_BT_ITEMQ_ENQUEUE(&ng_btsocket_l2cap_raw_queue, item); 310 error = ng_btsocket_l2cap_raw_wakeup_input_task(); 311 } 312 mtx_unlock(&ng_btsocket_l2cap_raw_queue_mtx); 313 } else { 314 NG_FREE_ITEM(item); 315 error = EINVAL; 316 } 317 318 return (error); 319 } /* ng_btsocket_l2cap_raw_node_rcvmsg */ 320 321 /* 322 * Receive data on a hook 323 */ 324 325 static int 326 ng_btsocket_l2cap_raw_node_rcvdata(hook_p hook, item_p item) 327 { 328 NG_FREE_ITEM(item); 329 330 return (EINVAL); 331 } /* ng_btsocket_l2cap_raw_node_rcvdata */ 332 333 /***************************************************************************** 334 ***************************************************************************** 335 ** Socket interface 336 ***************************************************************************** 337 *****************************************************************************/ 338 339 /* 340 * L2CAP sockets input routine 341 */ 342 343 static void 344 ng_btsocket_l2cap_raw_input(void *context, int pending) 345 { 346 item_p item = NULL; 347 hook_p hook = NULL; 348 struct ng_mesg *msg = NULL; 349 350 for (;;) { 351 mtx_lock(&ng_btsocket_l2cap_raw_queue_mtx); 352 NG_BT_ITEMQ_DEQUEUE(&ng_btsocket_l2cap_raw_queue, item); 353 mtx_unlock(&ng_btsocket_l2cap_raw_queue_mtx); 354 355 if (item == NULL) 356 break; 357 358 KASSERT((item->el_flags & NGQF_TYPE) == NGQF_MESG, 359 ("%s: invalid item type=%ld\n", __func__, (item->el_flags & NGQF_TYPE))); 360 361 NGI_GET_MSG(item, msg); 362 NGI_GET_HOOK(item, hook); 363 NG_FREE_ITEM(item); 364 365 switch (msg->header.cmd) { 366 case NGM_L2CAP_NODE_HOOK_INFO: { 367 ng_btsocket_l2cap_rtentry_t *rt = NULL; 368 369 if (hook == NULL || NG_HOOK_NOT_VALID(hook) || 370 msg->header.arglen != sizeof(bdaddr_t)) 371 break; 372 373 if (bcmp(msg->data, NG_HCI_BDADDR_ANY, 374 sizeof(bdaddr_t)) == 0) 375 break; 376 377 rt = (ng_btsocket_l2cap_rtentry_t *) 378 NG_HOOK_PRIVATE(hook); 379 if (rt == NULL) { 380 rt = malloc(sizeof(*rt), 381 M_NETGRAPH_BTSOCKET_L2CAP_RAW, 382 M_NOWAIT|M_ZERO); 383 if (rt == NULL) 384 break; 385 386 NG_HOOK_SET_PRIVATE(hook, rt); 387 388 mtx_lock(&ng_btsocket_l2cap_raw_rt_mtx); 389 390 LIST_INSERT_HEAD(&ng_btsocket_l2cap_raw_rt, 391 rt, next); 392 } else 393 mtx_lock(&ng_btsocket_l2cap_raw_rt_mtx); 394 395 bcopy(msg->data, &rt->src, sizeof(rt->src)); 396 rt->hook = hook; 397 398 NG_BTSOCKET_L2CAP_RAW_INFO( 399 "%s: Updating hook \"%s\", src bdaddr=%x:%x:%x:%x:%x:%x\n", 400 __func__, NG_HOOK_NAME(hook), 401 rt->src.b[5], rt->src.b[4], rt->src.b[3], 402 rt->src.b[2], rt->src.b[1], rt->src.b[0]); 403 404 mtx_unlock(&ng_btsocket_l2cap_raw_rt_mtx); 405 } break; 406 407 case NGM_L2CAP_NODE_GET_FLAGS: 408 case NGM_L2CAP_NODE_GET_DEBUG: 409 case NGM_L2CAP_NODE_GET_CON_LIST: 410 case NGM_L2CAP_NODE_GET_CHAN_LIST: 411 case NGM_L2CAP_NODE_GET_AUTO_DISCON_TIMO: 412 case NGM_L2CAP_L2CA_PING: 413 case NGM_L2CAP_L2CA_GET_INFO: { 414 ng_btsocket_l2cap_raw_pcb_p pcb = NULL; 415 416 mtx_lock(&ng_btsocket_l2cap_raw_sockets_mtx); 417 418 LIST_FOREACH(pcb,&ng_btsocket_l2cap_raw_sockets,next) { 419 mtx_lock(&pcb->pcb_mtx); 420 421 if (pcb->token == msg->header.token) { 422 pcb->msg = msg; 423 msg = NULL; 424 wakeup(&pcb->msg); 425 mtx_unlock(&pcb->pcb_mtx); 426 break; 427 } 428 429 mtx_unlock(&pcb->pcb_mtx); 430 } 431 432 mtx_unlock(&ng_btsocket_l2cap_raw_sockets_mtx); 433 } break; 434 435 default: 436 NG_BTSOCKET_L2CAP_RAW_WARN( 437 "%s: Unknown message, cmd=%d\n", __func__, msg->header.cmd); 438 break; 439 } 440 441 if (hook != NULL) 442 NG_HOOK_UNREF(hook); /* remove extra reference */ 443 444 NG_FREE_MSG(msg); /* Checks for msg != NULL */ 445 } 446 } /* ng_btsocket_l2cap_raw_input */ 447 448 /* 449 * Route cleanup task. Gets scheduled when hook is disconnected. Here we 450 * will find all sockets that use "invalid" hook and disconnect them. 451 */ 452 453 static void 454 ng_btsocket_l2cap_raw_rtclean(void *context, int pending) 455 { 456 ng_btsocket_l2cap_raw_pcb_p pcb = NULL; 457 ng_btsocket_l2cap_rtentry_p rt = NULL; 458 459 /* 460 * First disconnect all sockets that use "invalid" hook 461 */ 462 463 mtx_lock(&ng_btsocket_l2cap_raw_sockets_mtx); 464 465 LIST_FOREACH(pcb, &ng_btsocket_l2cap_raw_sockets, next) { 466 mtx_lock(&pcb->pcb_mtx); 467 468 if (pcb->rt != NULL && 469 pcb->rt->hook != NULL && NG_HOOK_NOT_VALID(pcb->rt->hook)) { 470 if (pcb->so != NULL && 471 pcb->so->so_state & SS_ISCONNECTED) 472 soisdisconnected(pcb->so); 473 474 pcb->rt = NULL; 475 } 476 477 mtx_unlock(&pcb->pcb_mtx); 478 } 479 480 mtx_unlock(&ng_btsocket_l2cap_raw_sockets_mtx); 481 482 /* 483 * Now cleanup routing table 484 */ 485 486 mtx_lock(&ng_btsocket_l2cap_raw_rt_mtx); 487 488 for (rt = LIST_FIRST(&ng_btsocket_l2cap_raw_rt); rt != NULL; ) { 489 ng_btsocket_l2cap_rtentry_p rt_next = LIST_NEXT(rt, next); 490 491 if (rt->hook != NULL && NG_HOOK_NOT_VALID(rt->hook)) { 492 LIST_REMOVE(rt, next); 493 494 NG_HOOK_SET_PRIVATE(rt->hook, NULL); 495 NG_HOOK_UNREF(rt->hook); /* Remove extra reference */ 496 497 bzero(rt, sizeof(*rt)); 498 free(rt, M_NETGRAPH_BTSOCKET_L2CAP_RAW); 499 } 500 501 rt = rt_next; 502 } 503 504 mtx_unlock(&ng_btsocket_l2cap_raw_rt_mtx); 505 } /* ng_btsocket_l2cap_raw_rtclean */ 506 507 /* 508 * Initialize everything 509 */ 510 511 void 512 ng_btsocket_l2cap_raw_init(void) 513 { 514 int error = 0; 515 516 ng_btsocket_l2cap_raw_node = NULL; 517 ng_btsocket_l2cap_raw_debug_level = NG_BTSOCKET_WARN_LEVEL; 518 ng_btsocket_l2cap_raw_ioctl_timeout = 5; 519 520 /* Register Netgraph node type */ 521 error = ng_newtype(&typestruct); 522 if (error != 0) { 523 NG_BTSOCKET_L2CAP_RAW_ALERT( 524 "%s: Could not register Netgraph node type, error=%d\n", __func__, error); 525 526 return; 527 } 528 529 /* Create Netgrapg node */ 530 error = ng_make_node_common(&typestruct, &ng_btsocket_l2cap_raw_node); 531 if (error != 0) { 532 NG_BTSOCKET_L2CAP_RAW_ALERT( 533 "%s: Could not create Netgraph node, error=%d\n", __func__, error); 534 535 ng_btsocket_l2cap_raw_node = NULL; 536 537 return; 538 } 539 540 error = ng_name_node(ng_btsocket_l2cap_raw_node, 541 NG_BTSOCKET_L2CAP_RAW_NODE_TYPE); 542 if (error != 0) { 543 NG_BTSOCKET_L2CAP_RAW_ALERT( 544 "%s: Could not name Netgraph node, error=%d\n", __func__, error); 545 546 NG_NODE_UNREF(ng_btsocket_l2cap_raw_node); 547 ng_btsocket_l2cap_raw_node = NULL; 548 549 return; 550 } 551 552 /* Create input queue */ 553 NG_BT_ITEMQ_INIT(&ng_btsocket_l2cap_raw_queue, ifqmaxlen); 554 mtx_init(&ng_btsocket_l2cap_raw_queue_mtx, 555 "btsocks_l2cap_raw_queue_mtx", NULL, MTX_DEF); 556 TASK_INIT(&ng_btsocket_l2cap_raw_queue_task, 0, 557 ng_btsocket_l2cap_raw_input, NULL); 558 559 /* Create list of sockets */ 560 LIST_INIT(&ng_btsocket_l2cap_raw_sockets); 561 mtx_init(&ng_btsocket_l2cap_raw_sockets_mtx, 562 "btsocks_l2cap_raw_sockets_mtx", NULL, MTX_DEF); 563 564 /* Tokens */ 565 ng_btsocket_l2cap_raw_token = 0; 566 mtx_init(&ng_btsocket_l2cap_raw_token_mtx, 567 "btsocks_l2cap_raw_token_mtx", NULL, MTX_DEF); 568 569 /* Routing table */ 570 LIST_INIT(&ng_btsocket_l2cap_raw_rt); 571 mtx_init(&ng_btsocket_l2cap_raw_rt_mtx, 572 "btsocks_l2cap_raw_rt_mtx", NULL, MTX_DEF); 573 TASK_INIT(&ng_btsocket_l2cap_raw_rt_task, 0, 574 ng_btsocket_l2cap_raw_rtclean, NULL); 575 } /* ng_btsocket_l2cap_raw_init */ 576 577 /* 578 * Abort connection on socket 579 */ 580 581 void 582 ng_btsocket_l2cap_raw_abort(struct socket *so) 583 { 584 585 (void)ng_btsocket_l2cap_raw_disconnect(so); 586 } /* ng_btsocket_l2cap_raw_abort */ 587 588 void 589 ng_btsocket_l2cap_raw_close(struct socket *so) 590 { 591 592 (void)ng_btsocket_l2cap_raw_disconnect(so); 593 } /* ng_btsocket_l2cap_raw_close */ 594 595 /* 596 * Create and attach new socket 597 */ 598 599 int 600 ng_btsocket_l2cap_raw_attach(struct socket *so, int proto, struct thread *td) 601 { 602 ng_btsocket_l2cap_raw_pcb_p pcb = so2l2cap_raw_pcb(so); 603 int error; 604 605 if (pcb != NULL) 606 return (EISCONN); 607 608 if (ng_btsocket_l2cap_raw_node == NULL) 609 return (EPROTONOSUPPORT); 610 if (so->so_type != SOCK_RAW) 611 return (ESOCKTNOSUPPORT); 612 613 /* Reserve send and receive space if it is not reserved yet */ 614 error = soreserve(so, NG_BTSOCKET_L2CAP_RAW_SENDSPACE, 615 NG_BTSOCKET_L2CAP_RAW_RECVSPACE); 616 if (error != 0) 617 return (error); 618 619 /* Allocate the PCB */ 620 pcb = malloc(sizeof(*pcb), 621 M_NETGRAPH_BTSOCKET_L2CAP_RAW, M_NOWAIT|M_ZERO); 622 if (pcb == NULL) 623 return (ENOMEM); 624 625 /* Link the PCB and the socket */ 626 so->so_pcb = (caddr_t) pcb; 627 pcb->so = so; 628 629 if (priv_check(td, PRIV_NETBLUETOOTH_RAW) == 0) 630 pcb->flags |= NG_BTSOCKET_L2CAP_RAW_PRIVILEGED; 631 632 mtx_init(&pcb->pcb_mtx, "btsocks_l2cap_raw_pcb_mtx", NULL, MTX_DEF); 633 634 /* Add the PCB to the list */ 635 mtx_lock(&ng_btsocket_l2cap_raw_sockets_mtx); 636 LIST_INSERT_HEAD(&ng_btsocket_l2cap_raw_sockets, pcb, next); 637 mtx_unlock(&ng_btsocket_l2cap_raw_sockets_mtx); 638 639 return (0); 640 } /* ng_btsocket_l2cap_raw_attach */ 641 642 /* 643 * Bind socket 644 */ 645 646 int 647 ng_btsocket_l2cap_raw_bind(struct socket *so, struct sockaddr *nam, 648 struct thread *td) 649 { 650 ng_btsocket_l2cap_raw_pcb_t *pcb = so2l2cap_raw_pcb(so); 651 struct sockaddr_l2cap *sa = (struct sockaddr_l2cap *) nam; 652 ng_btsocket_l2cap_rtentry_t *rt = NULL; 653 654 if (pcb == NULL) 655 return (EINVAL); 656 if (ng_btsocket_l2cap_raw_node == NULL) 657 return (EINVAL); 658 659 if (sa == NULL) 660 return (EINVAL); 661 if (sa->l2cap_family != AF_BLUETOOTH) 662 return (EAFNOSUPPORT); 663 if (sa->l2cap_len != sizeof(*sa)) 664 return (EINVAL); 665 666 if (bcmp(&sa->l2cap_bdaddr, NG_HCI_BDADDR_ANY, 667 sizeof(sa->l2cap_bdaddr)) != 0) { 668 mtx_lock(&ng_btsocket_l2cap_raw_rt_mtx); 669 670 LIST_FOREACH(rt, &ng_btsocket_l2cap_raw_rt, next) { 671 if (rt->hook == NULL || NG_HOOK_NOT_VALID(rt->hook)) 672 continue; 673 674 if (bcmp(&sa->l2cap_bdaddr, &rt->src, 675 sizeof(rt->src)) == 0) 676 break; 677 } 678 679 mtx_unlock(&ng_btsocket_l2cap_raw_rt_mtx); 680 681 if (rt == NULL) 682 return (ENETDOWN); 683 } else 684 rt = NULL; 685 686 mtx_lock(&pcb->pcb_mtx); 687 bcopy(&sa->l2cap_bdaddr, &pcb->src, sizeof(pcb->src)); 688 pcb->rt = rt; 689 mtx_unlock(&pcb->pcb_mtx); 690 691 return (0); 692 } /* ng_btsocket_l2cap_raw_bind */ 693 694 /* 695 * Connect socket 696 */ 697 698 int 699 ng_btsocket_l2cap_raw_connect(struct socket *so, struct sockaddr *nam, 700 struct thread *td) 701 { 702 ng_btsocket_l2cap_raw_pcb_t *pcb = so2l2cap_raw_pcb(so); 703 struct sockaddr_l2cap *sa = (struct sockaddr_l2cap *) nam; 704 ng_btsocket_l2cap_rtentry_t *rt = NULL; 705 int error; 706 707 if (pcb == NULL) 708 return (EINVAL); 709 if (ng_btsocket_l2cap_raw_node == NULL) 710 return (EINVAL); 711 712 if (sa == NULL) 713 return (EINVAL); 714 if (sa->l2cap_family != AF_BLUETOOTH) 715 return (EAFNOSUPPORT); 716 if (sa->l2cap_len != sizeof(*sa)) 717 return (EINVAL); 718 if (bcmp(&sa->l2cap_bdaddr, NG_HCI_BDADDR_ANY, sizeof(bdaddr_t)) == 0) 719 return (EINVAL); 720 721 mtx_lock(&pcb->pcb_mtx); 722 723 bcopy(&sa->l2cap_bdaddr, &pcb->dst, sizeof(pcb->dst)); 724 725 if (bcmp(&pcb->src, &pcb->dst, sizeof(pcb->src)) == 0) { 726 mtx_unlock(&pcb->pcb_mtx); 727 728 return (EADDRNOTAVAIL); 729 } 730 731 /* 732 * If there is route already - use it 733 */ 734 735 if (pcb->rt != NULL) { 736 soisconnected(so); 737 mtx_unlock(&pcb->pcb_mtx); 738 739 return (0); 740 } 741 742 /* 743 * Find the first hook that does not match specified destination address 744 */ 745 746 mtx_lock(&ng_btsocket_l2cap_raw_rt_mtx); 747 748 LIST_FOREACH(rt, &ng_btsocket_l2cap_raw_rt, next) { 749 if (rt->hook == NULL || NG_HOOK_NOT_VALID(rt->hook)) 750 continue; 751 752 if (bcmp(&pcb->dst, &rt->src, sizeof(rt->src)) != 0) 753 break; 754 } 755 756 if (rt != NULL) { 757 soisconnected(so); 758 759 pcb->rt = rt; 760 bcopy(&rt->src, &pcb->src, sizeof(pcb->src)); 761 762 error = 0; 763 } else 764 error = ENETDOWN; 765 766 mtx_unlock(&ng_btsocket_l2cap_raw_rt_mtx); 767 mtx_unlock(&pcb->pcb_mtx); 768 769 return (error); 770 } /* ng_btsocket_l2cap_raw_connect */ 771 772 /* 773 * Process ioctl's calls on socket 774 */ 775 776 int 777 ng_btsocket_l2cap_raw_control(struct socket *so, u_long cmd, caddr_t data, 778 struct ifnet *ifp, struct thread *td) 779 { 780 ng_btsocket_l2cap_raw_pcb_p pcb = so2l2cap_raw_pcb(so); 781 struct ng_mesg *msg = NULL; 782 int error = 0; 783 784 if (pcb == NULL) 785 return (EINVAL); 786 if (ng_btsocket_l2cap_raw_node == NULL) 787 return (EINVAL); 788 789 mtx_lock(&pcb->pcb_mtx); 790 791 /* Check if we route info */ 792 if (pcb->rt == NULL) { 793 mtx_unlock(&pcb->pcb_mtx); 794 return (EHOSTUNREACH); 795 } 796 797 /* Check if we have pending ioctl() */ 798 if (pcb->token != 0) { 799 mtx_unlock(&pcb->pcb_mtx); 800 return (EBUSY); 801 } 802 803 switch (cmd) { 804 case SIOC_L2CAP_NODE_GET_FLAGS: { 805 struct ng_btsocket_l2cap_raw_node_flags *p = 806 (struct ng_btsocket_l2cap_raw_node_flags *) data; 807 808 error = ng_btsocket_l2cap_raw_send_sync_ngmsg(pcb, 809 NGM_L2CAP_NODE_GET_FLAGS, 810 &p->flags, sizeof(p->flags)); 811 } break; 812 813 case SIOC_L2CAP_NODE_GET_DEBUG: { 814 struct ng_btsocket_l2cap_raw_node_debug *p = 815 (struct ng_btsocket_l2cap_raw_node_debug *) data; 816 817 error = ng_btsocket_l2cap_raw_send_sync_ngmsg(pcb, 818 NGM_L2CAP_NODE_GET_DEBUG, 819 &p->debug, sizeof(p->debug)); 820 } break; 821 822 case SIOC_L2CAP_NODE_SET_DEBUG: { 823 struct ng_btsocket_l2cap_raw_node_debug *p = 824 (struct ng_btsocket_l2cap_raw_node_debug *) data; 825 826 if (pcb->flags & NG_BTSOCKET_L2CAP_RAW_PRIVILEGED) 827 error = ng_btsocket_l2cap_raw_send_ngmsg(pcb->rt->hook, 828 NGM_L2CAP_NODE_SET_DEBUG, 829 &p->debug, sizeof(p->debug)); 830 else 831 error = EPERM; 832 } break; 833 834 case SIOC_L2CAP_NODE_GET_CON_LIST: { 835 struct ng_btsocket_l2cap_raw_con_list *p = 836 (struct ng_btsocket_l2cap_raw_con_list *) data; 837 ng_l2cap_node_con_list_ep *p1 = NULL; 838 ng_l2cap_node_con_ep *p2 = NULL; 839 840 if (p->num_connections == 0 || 841 p->num_connections > NG_L2CAP_MAX_CON_NUM || 842 p->connections == NULL) { 843 error = EINVAL; 844 break; 845 } 846 847 NG_MKMESSAGE(msg, NGM_L2CAP_COOKIE, NGM_L2CAP_NODE_GET_CON_LIST, 848 0, M_NOWAIT); 849 if (msg == NULL) { 850 error = ENOMEM; 851 break; 852 } 853 ng_btsocket_l2cap_raw_get_token(&msg->header.token); 854 pcb->token = msg->header.token; 855 pcb->msg = NULL; 856 857 NG_SEND_MSG_HOOK(error, ng_btsocket_l2cap_raw_node, msg, 858 pcb->rt->hook, 0); 859 if (error != 0) { 860 pcb->token = 0; 861 break; 862 } 863 864 error = msleep(&pcb->msg, &pcb->pcb_mtx, PZERO|PCATCH, "l2ctl", 865 ng_btsocket_l2cap_raw_ioctl_timeout * hz); 866 pcb->token = 0; 867 868 if (error != 0) 869 break; 870 871 if (pcb->msg != NULL && 872 pcb->msg->header.cmd == NGM_L2CAP_NODE_GET_CON_LIST) { 873 /* Return data back to user space */ 874 p1 = (ng_l2cap_node_con_list_ep *)(pcb->msg->data); 875 p2 = (ng_l2cap_node_con_ep *)(p1 + 1); 876 877 p->num_connections = min(p->num_connections, 878 p1->num_connections); 879 if (p->num_connections > 0) 880 error = copyout((caddr_t) p2, 881 (caddr_t) p->connections, 882 p->num_connections * sizeof(*p2)); 883 } else 884 error = EINVAL; 885 886 NG_FREE_MSG(pcb->msg); /* checks for != NULL */ 887 } break; 888 889 case SIOC_L2CAP_NODE_GET_CHAN_LIST: { 890 struct ng_btsocket_l2cap_raw_chan_list *p = 891 (struct ng_btsocket_l2cap_raw_chan_list *) data; 892 ng_l2cap_node_chan_list_ep *p1 = NULL; 893 ng_l2cap_node_chan_ep *p2 = NULL; 894 895 if (p->num_channels == 0 || 896 p->num_channels > NG_L2CAP_MAX_CHAN_NUM || 897 p->channels == NULL) { 898 error = EINVAL; 899 break; 900 } 901 902 NG_MKMESSAGE(msg, NGM_L2CAP_COOKIE, 903 NGM_L2CAP_NODE_GET_CHAN_LIST, 0, M_NOWAIT); 904 if (msg == NULL) { 905 error = ENOMEM; 906 break; 907 } 908 ng_btsocket_l2cap_raw_get_token(&msg->header.token); 909 pcb->token = msg->header.token; 910 pcb->msg = NULL; 911 912 NG_SEND_MSG_HOOK(error, ng_btsocket_l2cap_raw_node, msg, 913 pcb->rt->hook, 0); 914 if (error != 0) { 915 pcb->token = 0; 916 break; 917 } 918 919 error = msleep(&pcb->msg, &pcb->pcb_mtx, PZERO|PCATCH, "l2ctl", 920 ng_btsocket_l2cap_raw_ioctl_timeout * hz); 921 pcb->token = 0; 922 923 if (error != 0) 924 break; 925 926 if (pcb->msg != NULL && 927 pcb->msg->header.cmd == NGM_L2CAP_NODE_GET_CHAN_LIST) { 928 /* Return data back to user space */ 929 p1 = (ng_l2cap_node_chan_list_ep *)(pcb->msg->data); 930 p2 = (ng_l2cap_node_chan_ep *)(p1 + 1); 931 932 p->num_channels = min(p->num_channels, 933 p1->num_channels); 934 if (p->num_channels > 0) 935 error = copyout((caddr_t) p2, 936 (caddr_t) p->channels, 937 p->num_channels * sizeof(*p2)); 938 } else 939 error = EINVAL; 940 941 NG_FREE_MSG(pcb->msg); /* checks for != NULL */ 942 } break; 943 944 case SIOC_L2CAP_L2CA_PING: { 945 struct ng_btsocket_l2cap_raw_ping *p = 946 (struct ng_btsocket_l2cap_raw_ping *) data; 947 ng_l2cap_l2ca_ping_ip *ip = NULL; 948 ng_l2cap_l2ca_ping_op *op = NULL; 949 950 if ((p->echo_size != 0 && p->echo_data == NULL) || 951 p->echo_size > NG_L2CAP_MAX_ECHO_SIZE) { 952 error = EINVAL; 953 break; 954 } 955 956 NG_MKMESSAGE(msg, NGM_L2CAP_COOKIE, 957 NGM_L2CAP_L2CA_PING, sizeof(*ip) + p->echo_size, 958 M_NOWAIT); 959 if (msg == NULL) { 960 error = ENOMEM; 961 break; 962 } 963 ng_btsocket_l2cap_raw_get_token(&msg->header.token); 964 pcb->token = msg->header.token; 965 pcb->msg = NULL; 966 967 ip = (ng_l2cap_l2ca_ping_ip *)(msg->data); 968 bcopy(&pcb->dst, &ip->bdaddr, sizeof(ip->bdaddr)); 969 ip->echo_size = p->echo_size; 970 971 if (ip->echo_size > 0) { 972 error = copyin(p->echo_data, ip + 1, p->echo_size); 973 if (error != 0) { 974 NG_FREE_MSG(msg); 975 pcb->token = 0; 976 break; 977 } 978 } 979 980 NG_SEND_MSG_HOOK(error, ng_btsocket_l2cap_raw_node, msg, 981 pcb->rt->hook, 0); 982 if (error != 0) { 983 pcb->token = 0; 984 break; 985 } 986 987 error = msleep(&pcb->msg, &pcb->pcb_mtx, PZERO|PCATCH, "l2ctl", 988 bluetooth_l2cap_rtx_timeout()); 989 pcb->token = 0; 990 991 if (error != 0) 992 break; 993 994 if (pcb->msg != NULL && 995 pcb->msg->header.cmd == NGM_L2CAP_L2CA_PING) { 996 /* Return data back to the user space */ 997 op = (ng_l2cap_l2ca_ping_op *)(pcb->msg->data); 998 p->result = op->result; 999 p->echo_size = min(p->echo_size, op->echo_size); 1000 1001 if (p->echo_size > 0) 1002 error = copyout(op + 1, p->echo_data, 1003 p->echo_size); 1004 } else 1005 error = EINVAL; 1006 1007 NG_FREE_MSG(pcb->msg); /* checks for != NULL */ 1008 } break; 1009 1010 case SIOC_L2CAP_L2CA_GET_INFO: { 1011 struct ng_btsocket_l2cap_raw_get_info *p = 1012 (struct ng_btsocket_l2cap_raw_get_info *) data; 1013 ng_l2cap_l2ca_get_info_ip *ip = NULL; 1014 ng_l2cap_l2ca_get_info_op *op = NULL; 1015 1016 if (!(pcb->flags & NG_BTSOCKET_L2CAP_RAW_PRIVILEGED)) { 1017 error = EPERM; 1018 break; 1019 } 1020 1021 if (p->info_size != 0 && p->info_data == NULL) { 1022 error = EINVAL; 1023 break; 1024 } 1025 1026 NG_MKMESSAGE(msg, NGM_L2CAP_COOKIE, 1027 NGM_L2CAP_L2CA_GET_INFO, sizeof(*ip) + p->info_size, 1028 M_NOWAIT); 1029 if (msg == NULL) { 1030 error = ENOMEM; 1031 break; 1032 } 1033 ng_btsocket_l2cap_raw_get_token(&msg->header.token); 1034 pcb->token = msg->header.token; 1035 pcb->msg = NULL; 1036 1037 ip = (ng_l2cap_l2ca_get_info_ip *)(msg->data); 1038 bcopy(&pcb->dst, &ip->bdaddr, sizeof(ip->bdaddr)); 1039 ip->info_type = p->info_type; 1040 1041 NG_SEND_MSG_HOOK(error, ng_btsocket_l2cap_raw_node, msg, 1042 pcb->rt->hook, 0); 1043 if (error != 0) { 1044 pcb->token = 0; 1045 break; 1046 } 1047 1048 error = msleep(&pcb->msg, &pcb->pcb_mtx, PZERO|PCATCH, "l2ctl", 1049 bluetooth_l2cap_rtx_timeout()); 1050 pcb->token = 0; 1051 1052 if (error != 0) 1053 break; 1054 1055 if (pcb->msg != NULL && 1056 pcb->msg->header.cmd == NGM_L2CAP_L2CA_GET_INFO) { 1057 /* Return data back to the user space */ 1058 op = (ng_l2cap_l2ca_get_info_op *)(pcb->msg->data); 1059 p->result = op->result; 1060 p->info_size = min(p->info_size, op->info_size); 1061 1062 if (p->info_size > 0) 1063 error = copyout(op + 1, p->info_data, 1064 p->info_size); 1065 } else 1066 error = EINVAL; 1067 1068 NG_FREE_MSG(pcb->msg); /* checks for != NULL */ 1069 } break; 1070 1071 case SIOC_L2CAP_NODE_GET_AUTO_DISCON_TIMO: { 1072 struct ng_btsocket_l2cap_raw_auto_discon_timo *p = 1073 (struct ng_btsocket_l2cap_raw_auto_discon_timo *) data; 1074 1075 error = ng_btsocket_l2cap_raw_send_sync_ngmsg(pcb, 1076 NGM_L2CAP_NODE_GET_AUTO_DISCON_TIMO, 1077 &p->timeout, sizeof(p->timeout)); 1078 } break; 1079 1080 case SIOC_L2CAP_NODE_SET_AUTO_DISCON_TIMO: { 1081 struct ng_btsocket_l2cap_raw_auto_discon_timo *p = 1082 (struct ng_btsocket_l2cap_raw_auto_discon_timo *) data; 1083 1084 if (pcb->flags & NG_BTSOCKET_L2CAP_RAW_PRIVILEGED) 1085 error = ng_btsocket_l2cap_raw_send_ngmsg(pcb->rt->hook, 1086 NGM_L2CAP_NODE_SET_AUTO_DISCON_TIMO, 1087 &p->timeout, sizeof(p->timeout)); 1088 else 1089 error = EPERM; 1090 } break; 1091 1092 default: 1093 error = EINVAL; 1094 break; 1095 } 1096 1097 mtx_unlock(&pcb->pcb_mtx); 1098 1099 return (error); 1100 } /* ng_btsocket_l2cap_raw_control */ 1101 1102 /* 1103 * Detach and destroy socket 1104 */ 1105 1106 void 1107 ng_btsocket_l2cap_raw_detach(struct socket *so) 1108 { 1109 ng_btsocket_l2cap_raw_pcb_p pcb = so2l2cap_raw_pcb(so); 1110 1111 KASSERT(pcb != NULL, ("nt_btsocket_l2cap_raw_detach: pcb == NULL")); 1112 if (ng_btsocket_l2cap_raw_node == NULL) 1113 return; 1114 1115 mtx_lock(&ng_btsocket_l2cap_raw_sockets_mtx); 1116 mtx_lock(&pcb->pcb_mtx); 1117 1118 LIST_REMOVE(pcb, next); 1119 1120 mtx_unlock(&pcb->pcb_mtx); 1121 mtx_unlock(&ng_btsocket_l2cap_raw_sockets_mtx); 1122 1123 mtx_destroy(&pcb->pcb_mtx); 1124 1125 bzero(pcb, sizeof(*pcb)); 1126 free(pcb, M_NETGRAPH_BTSOCKET_L2CAP_RAW); 1127 1128 so->so_pcb = NULL; 1129 } /* ng_btsocket_l2cap_raw_detach */ 1130 1131 /* 1132 * Disconnect socket 1133 */ 1134 1135 int 1136 ng_btsocket_l2cap_raw_disconnect(struct socket *so) 1137 { 1138 ng_btsocket_l2cap_raw_pcb_p pcb = so2l2cap_raw_pcb(so); 1139 1140 if (pcb == NULL) 1141 return (EINVAL); 1142 if (ng_btsocket_l2cap_raw_node == NULL) 1143 return (EINVAL); 1144 1145 mtx_lock(&pcb->pcb_mtx); 1146 pcb->rt = NULL; 1147 soisdisconnected(so); 1148 mtx_unlock(&pcb->pcb_mtx); 1149 1150 return (0); 1151 } /* ng_btsocket_l2cap_raw_disconnect */ 1152 1153 /* 1154 * Get peer address 1155 */ 1156 1157 int 1158 ng_btsocket_l2cap_raw_peeraddr(struct socket *so, struct sockaddr **nam) 1159 { 1160 ng_btsocket_l2cap_raw_pcb_p pcb = so2l2cap_raw_pcb(so); 1161 struct sockaddr_l2cap sa; 1162 1163 if (pcb == NULL) 1164 return (EINVAL); 1165 if (ng_btsocket_l2cap_raw_node == NULL) 1166 return (EINVAL); 1167 1168 mtx_lock(&pcb->pcb_mtx); 1169 bcopy(&pcb->dst, &sa.l2cap_bdaddr, sizeof(sa.l2cap_bdaddr)); 1170 mtx_unlock(&pcb->pcb_mtx); 1171 1172 sa.l2cap_psm = 0; 1173 sa.l2cap_len = sizeof(sa); 1174 sa.l2cap_family = AF_BLUETOOTH; 1175 1176 *nam = sodupsockaddr((struct sockaddr *) &sa, M_NOWAIT); 1177 1178 return ((*nam == NULL)? ENOMEM : 0); 1179 } /* ng_btsocket_l2cap_raw_peeraddr */ 1180 1181 /* 1182 * Send data to socket 1183 */ 1184 1185 int 1186 ng_btsocket_l2cap_raw_send(struct socket *so, int flags, struct mbuf *m, 1187 struct sockaddr *nam, struct mbuf *control, struct thread *td) 1188 { 1189 NG_FREE_M(m); /* Checks for m != NULL */ 1190 NG_FREE_M(control); 1191 1192 return (EOPNOTSUPP); 1193 } /* ng_btsocket_l2cap_raw_send */ 1194 1195 /* 1196 * Get socket address 1197 */ 1198 1199 int 1200 ng_btsocket_l2cap_raw_sockaddr(struct socket *so, struct sockaddr **nam) 1201 { 1202 ng_btsocket_l2cap_raw_pcb_p pcb = so2l2cap_raw_pcb(so); 1203 struct sockaddr_l2cap sa; 1204 1205 if (pcb == NULL) 1206 return (EINVAL); 1207 if (ng_btsocket_l2cap_raw_node == NULL) 1208 return (EINVAL); 1209 1210 mtx_lock(&pcb->pcb_mtx); 1211 bcopy(&pcb->src, &sa.l2cap_bdaddr, sizeof(sa.l2cap_bdaddr)); 1212 mtx_unlock(&pcb->pcb_mtx); 1213 1214 sa.l2cap_psm = 0; 1215 sa.l2cap_len = sizeof(sa); 1216 sa.l2cap_family = AF_BLUETOOTH; 1217 1218 *nam = sodupsockaddr((struct sockaddr *) &sa, M_NOWAIT); 1219 1220 return ((*nam == NULL)? ENOMEM : 0); 1221 } /* ng_btsocket_l2cap_raw_sockaddr */ 1222 1223 /* 1224 * Get next token 1225 */ 1226 1227 static void 1228 ng_btsocket_l2cap_raw_get_token(u_int32_t *token) 1229 { 1230 mtx_lock(&ng_btsocket_l2cap_raw_token_mtx); 1231 1232 if (++ ng_btsocket_l2cap_raw_token == 0) 1233 ng_btsocket_l2cap_raw_token = 1; 1234 1235 *token = ng_btsocket_l2cap_raw_token; 1236 1237 mtx_unlock(&ng_btsocket_l2cap_raw_token_mtx); 1238 } /* ng_btsocket_l2cap_raw_get_token */ 1239 1240 /* 1241 * Send Netgraph message to the node - do not expect reply 1242 */ 1243 1244 static int 1245 ng_btsocket_l2cap_raw_send_ngmsg(hook_p hook, int cmd, void *arg, int arglen) 1246 { 1247 struct ng_mesg *msg = NULL; 1248 int error = 0; 1249 1250 NG_MKMESSAGE(msg, NGM_L2CAP_COOKIE, cmd, arglen, M_NOWAIT); 1251 if (msg == NULL) 1252 return (ENOMEM); 1253 1254 if (arg != NULL && arglen > 0) 1255 bcopy(arg, msg->data, arglen); 1256 1257 NG_SEND_MSG_HOOK(error, ng_btsocket_l2cap_raw_node, msg, hook, 0); 1258 1259 return (error); 1260 } /* ng_btsocket_l2cap_raw_send_ngmsg */ 1261 1262 /* 1263 * Send Netgraph message to the node (no data) and wait for reply 1264 */ 1265 1266 static int 1267 ng_btsocket_l2cap_raw_send_sync_ngmsg(ng_btsocket_l2cap_raw_pcb_p pcb, 1268 int cmd, void *rsp, int rsplen) 1269 { 1270 struct ng_mesg *msg = NULL; 1271 int error = 0; 1272 1273 mtx_assert(&pcb->pcb_mtx, MA_OWNED); 1274 1275 NG_MKMESSAGE(msg, NGM_L2CAP_COOKIE, cmd, 0, M_NOWAIT); 1276 if (msg == NULL) 1277 return (ENOMEM); 1278 1279 ng_btsocket_l2cap_raw_get_token(&msg->header.token); 1280 pcb->token = msg->header.token; 1281 pcb->msg = NULL; 1282 1283 NG_SEND_MSG_HOOK(error, ng_btsocket_l2cap_raw_node, msg, 1284 pcb->rt->hook, 0); 1285 if (error != 0) { 1286 pcb->token = 0; 1287 return (error); 1288 } 1289 1290 error = msleep(&pcb->msg, &pcb->pcb_mtx, PZERO|PCATCH, "l2ctl", 1291 ng_btsocket_l2cap_raw_ioctl_timeout * hz); 1292 pcb->token = 0; 1293 1294 if (error != 0) 1295 return (error); 1296 1297 if (pcb->msg != NULL && pcb->msg->header.cmd == cmd) 1298 bcopy(pcb->msg->data, rsp, rsplen); 1299 else 1300 error = EINVAL; 1301 1302 NG_FREE_MSG(pcb->msg); /* checks for != NULL */ 1303 1304 return (0); 1305 } /* ng_btsocket_l2cap_raw_send_sync_ngmsg */ 1306 1307