1 /* 2 * ng_btsocket_l2cap.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.c,v 1.16 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/endian.h> 39 #include <sys/errno.h> 40 #include <sys/filedesc.h> 41 #include <sys/ioccom.h> 42 #include <sys/kernel.h> 43 #include <sys/lock.h> 44 #include <sys/malloc.h> 45 #include <sys/mbuf.h> 46 #include <sys/mutex.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 54 #include <net/vnet.h> 55 56 #include <netgraph/ng_message.h> 57 #include <netgraph/netgraph.h> 58 #include <netgraph/bluetooth/include/ng_bluetooth.h> 59 #include <netgraph/bluetooth/include/ng_hci.h> 60 #include <netgraph/bluetooth/include/ng_l2cap.h> 61 #include <netgraph/bluetooth/include/ng_btsocket.h> 62 #include <netgraph/bluetooth/include/ng_btsocket_l2cap.h> 63 64 /* MALLOC define */ 65 #ifdef NG_SEPARATE_MALLOC 66 static MALLOC_DEFINE(M_NETGRAPH_BTSOCKET_L2CAP, "netgraph_btsocks_l2cap", 67 "Netgraph Bluetooth L2CAP sockets"); 68 #else 69 #define M_NETGRAPH_BTSOCKET_L2CAP M_NETGRAPH 70 #endif /* NG_SEPARATE_MALLOC */ 71 72 /* Netgraph node methods */ 73 static ng_constructor_t ng_btsocket_l2cap_node_constructor; 74 static ng_rcvmsg_t ng_btsocket_l2cap_node_rcvmsg; 75 static ng_shutdown_t ng_btsocket_l2cap_node_shutdown; 76 static ng_newhook_t ng_btsocket_l2cap_node_newhook; 77 static ng_connect_t ng_btsocket_l2cap_node_connect; 78 static ng_rcvdata_t ng_btsocket_l2cap_node_rcvdata; 79 static ng_disconnect_t ng_btsocket_l2cap_node_disconnect; 80 81 static void ng_btsocket_l2cap_input (void *, int); 82 static void ng_btsocket_l2cap_rtclean (void *, int); 83 84 /* Netgraph type descriptor */ 85 static struct ng_type typestruct = { 86 .version = NG_ABI_VERSION, 87 .name = NG_BTSOCKET_L2CAP_NODE_TYPE, 88 .constructor = ng_btsocket_l2cap_node_constructor, 89 .rcvmsg = ng_btsocket_l2cap_node_rcvmsg, 90 .shutdown = ng_btsocket_l2cap_node_shutdown, 91 .newhook = ng_btsocket_l2cap_node_newhook, 92 .connect = ng_btsocket_l2cap_node_connect, 93 .rcvdata = ng_btsocket_l2cap_node_rcvdata, 94 .disconnect = ng_btsocket_l2cap_node_disconnect, 95 }; 96 97 /* Globals */ 98 extern int ifqmaxlen; 99 static u_int32_t ng_btsocket_l2cap_debug_level; 100 static node_p ng_btsocket_l2cap_node; 101 static struct ng_bt_itemq ng_btsocket_l2cap_queue; 102 static struct mtx ng_btsocket_l2cap_queue_mtx; 103 static struct task ng_btsocket_l2cap_queue_task; 104 static LIST_HEAD(, ng_btsocket_l2cap_pcb) ng_btsocket_l2cap_sockets; 105 static struct mtx ng_btsocket_l2cap_sockets_mtx; 106 static LIST_HEAD(, ng_btsocket_l2cap_rtentry) ng_btsocket_l2cap_rt; 107 static struct mtx ng_btsocket_l2cap_rt_mtx; 108 static struct task ng_btsocket_l2cap_rt_task; 109 static struct timeval ng_btsocket_l2cap_lasttime; 110 static int ng_btsocket_l2cap_curpps; 111 112 /* Sysctl tree */ 113 SYSCTL_DECL(_net_bluetooth_l2cap_sockets); 114 static SYSCTL_NODE(_net_bluetooth_l2cap_sockets, OID_AUTO, seq, CTLFLAG_RW, 115 0, "Bluetooth SEQPACKET L2CAP sockets family"); 116 SYSCTL_UINT(_net_bluetooth_l2cap_sockets_seq, OID_AUTO, debug_level, 117 CTLFLAG_RW, 118 &ng_btsocket_l2cap_debug_level, NG_BTSOCKET_WARN_LEVEL, 119 "Bluetooth SEQPACKET L2CAP sockets debug level"); 120 SYSCTL_UINT(_net_bluetooth_l2cap_sockets_seq, OID_AUTO, queue_len, 121 CTLFLAG_RD, 122 &ng_btsocket_l2cap_queue.len, 0, 123 "Bluetooth SEQPACKET L2CAP sockets input queue length"); 124 SYSCTL_UINT(_net_bluetooth_l2cap_sockets_seq, OID_AUTO, queue_maxlen, 125 CTLFLAG_RD, 126 &ng_btsocket_l2cap_queue.maxlen, 0, 127 "Bluetooth SEQPACKET L2CAP sockets input queue max. length"); 128 SYSCTL_UINT(_net_bluetooth_l2cap_sockets_seq, OID_AUTO, queue_drops, 129 CTLFLAG_RD, 130 &ng_btsocket_l2cap_queue.drops, 0, 131 "Bluetooth SEQPACKET L2CAP sockets input queue drops"); 132 133 /* Debug */ 134 #define NG_BTSOCKET_L2CAP_INFO \ 135 if (ng_btsocket_l2cap_debug_level >= NG_BTSOCKET_INFO_LEVEL && \ 136 ppsratecheck(&ng_btsocket_l2cap_lasttime, &ng_btsocket_l2cap_curpps, 1)) \ 137 printf 138 139 #define NG_BTSOCKET_L2CAP_WARN \ 140 if (ng_btsocket_l2cap_debug_level >= NG_BTSOCKET_WARN_LEVEL && \ 141 ppsratecheck(&ng_btsocket_l2cap_lasttime, &ng_btsocket_l2cap_curpps, 1)) \ 142 printf 143 144 #define NG_BTSOCKET_L2CAP_ERR \ 145 if (ng_btsocket_l2cap_debug_level >= NG_BTSOCKET_ERR_LEVEL && \ 146 ppsratecheck(&ng_btsocket_l2cap_lasttime, &ng_btsocket_l2cap_curpps, 1)) \ 147 printf 148 149 #define NG_BTSOCKET_L2CAP_ALERT \ 150 if (ng_btsocket_l2cap_debug_level >= NG_BTSOCKET_ALERT_LEVEL && \ 151 ppsratecheck(&ng_btsocket_l2cap_lasttime, &ng_btsocket_l2cap_curpps, 1)) \ 152 printf 153 154 /* 155 * Netgraph message processing routines 156 */ 157 158 static int ng_btsocket_l2cap_process_l2ca_con_req_rsp 159 (struct ng_mesg *, ng_btsocket_l2cap_rtentry_p); 160 static int ng_btsocket_l2cap_process_l2ca_con_rsp_rsp 161 (struct ng_mesg *, ng_btsocket_l2cap_rtentry_p); 162 static int ng_btsocket_l2cap_process_l2ca_con_ind 163 (struct ng_mesg *, ng_btsocket_l2cap_rtentry_p); 164 165 static int ng_btsocket_l2cap_process_l2ca_cfg_req_rsp 166 (struct ng_mesg *, ng_btsocket_l2cap_rtentry_p); 167 static int ng_btsocket_l2cap_process_l2ca_cfg_rsp_rsp 168 (struct ng_mesg *, ng_btsocket_l2cap_rtentry_p); 169 static int ng_btsocket_l2cap_process_l2ca_cfg_ind 170 (struct ng_mesg *, ng_btsocket_l2cap_rtentry_p); 171 172 static int ng_btsocket_l2cap_process_l2ca_discon_rsp 173 (struct ng_mesg *, ng_btsocket_l2cap_rtentry_p); 174 static int ng_btsocket_l2cap_process_l2ca_discon_ind 175 (struct ng_mesg *, ng_btsocket_l2cap_rtentry_p); 176 177 static int ng_btsocket_l2cap_process_l2ca_write_rsp 178 (struct ng_mesg *, ng_btsocket_l2cap_rtentry_p); 179 180 /* 181 * Send L2CA_xxx messages to the lower layer 182 */ 183 184 static int ng_btsocket_l2cap_send_l2ca_con_req 185 (ng_btsocket_l2cap_pcb_p); 186 static int ng_btsocket_l2cap_send_l2ca_con_rsp_req 187 (u_int32_t, ng_btsocket_l2cap_rtentry_p, bdaddr_p, int, int, int); 188 static int ng_btsocket_l2cap_send_l2ca_cfg_req 189 (ng_btsocket_l2cap_pcb_p); 190 static int ng_btsocket_l2cap_send_l2ca_cfg_rsp 191 (ng_btsocket_l2cap_pcb_p); 192 static int ng_btsocket_l2cap_send_l2ca_discon_req 193 (u_int32_t, ng_btsocket_l2cap_pcb_p); 194 195 static int ng_btsocket_l2cap_send2 196 (ng_btsocket_l2cap_pcb_p); 197 198 /* 199 * Timeout processing routines 200 */ 201 202 static void ng_btsocket_l2cap_timeout (ng_btsocket_l2cap_pcb_p); 203 static void ng_btsocket_l2cap_untimeout (ng_btsocket_l2cap_pcb_p); 204 static void ng_btsocket_l2cap_process_timeout (void *); 205 206 /* 207 * Other stuff 208 */ 209 210 static ng_btsocket_l2cap_pcb_p ng_btsocket_l2cap_pcb_by_addr(bdaddr_p, int); 211 static ng_btsocket_l2cap_pcb_p ng_btsocket_l2cap_pcb_by_token(u_int32_t); 212 static ng_btsocket_l2cap_pcb_p ng_btsocket_l2cap_pcb_by_cid (bdaddr_p, int); 213 static int ng_btsocket_l2cap_result2errno(int); 214 215 #define ng_btsocket_l2cap_wakeup_input_task() \ 216 taskqueue_enqueue(taskqueue_swi_giant, &ng_btsocket_l2cap_queue_task) 217 218 #define ng_btsocket_l2cap_wakeup_route_task() \ 219 taskqueue_enqueue(taskqueue_swi_giant, &ng_btsocket_l2cap_rt_task) 220 221 /***************************************************************************** 222 ***************************************************************************** 223 ** Netgraph node interface 224 ***************************************************************************** 225 *****************************************************************************/ 226 227 /* 228 * Netgraph node constructor. Do not allow to create node of this type. 229 */ 230 231 static int 232 ng_btsocket_l2cap_node_constructor(node_p node) 233 { 234 return (EINVAL); 235 } /* ng_btsocket_l2cap_node_constructor */ 236 237 /* 238 * Do local shutdown processing. Let old node go and create new fresh one. 239 */ 240 241 static int 242 ng_btsocket_l2cap_node_shutdown(node_p node) 243 { 244 int error = 0; 245 246 NG_NODE_UNREF(node); 247 248 /* Create new node */ 249 error = ng_make_node_common(&typestruct, &ng_btsocket_l2cap_node); 250 if (error != 0) { 251 NG_BTSOCKET_L2CAP_ALERT( 252 "%s: Could not create Netgraph node, error=%d\n", __func__, error); 253 254 ng_btsocket_l2cap_node = NULL; 255 256 return (error); 257 } 258 259 error = ng_name_node(ng_btsocket_l2cap_node, 260 NG_BTSOCKET_L2CAP_NODE_TYPE); 261 if (error != 0) { 262 NG_BTSOCKET_L2CAP_ALERT( 263 "%s: Could not name Netgraph node, error=%d\n", __func__, error); 264 265 NG_NODE_UNREF(ng_btsocket_l2cap_node); 266 ng_btsocket_l2cap_node = NULL; 267 268 return (error); 269 } 270 271 return (0); 272 } /* ng_btsocket_l2cap_node_shutdown */ 273 274 /* 275 * We allow any hook to be connected to the node. 276 */ 277 278 static int 279 ng_btsocket_l2cap_node_newhook(node_p node, hook_p hook, char const *name) 280 { 281 return (0); 282 } /* ng_btsocket_l2cap_node_newhook */ 283 284 /* 285 * Just say "YEP, that's OK by me!" 286 */ 287 288 static int 289 ng_btsocket_l2cap_node_connect(hook_p hook) 290 { 291 NG_HOOK_SET_PRIVATE(hook, NULL); 292 NG_HOOK_REF(hook); /* Keep extra reference to the hook */ 293 294 #if 0 295 NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook)); 296 NG_HOOK_FORCE_QUEUE(hook); 297 #endif 298 299 return (0); 300 } /* ng_btsocket_l2cap_node_connect */ 301 302 /* 303 * Hook disconnection. Schedule route cleanup task 304 */ 305 306 static int 307 ng_btsocket_l2cap_node_disconnect(hook_p hook) 308 { 309 /* 310 * If hook has private information than we must have this hook in 311 * the routing table and must schedule cleaning for the routing table. 312 * Otherwise hook was connected but we never got "hook_info" message, 313 * so we have never added this hook to the routing table and it save 314 * to just delete it. 315 */ 316 317 if (NG_HOOK_PRIVATE(hook) != NULL) 318 return (ng_btsocket_l2cap_wakeup_route_task()); 319 320 NG_HOOK_UNREF(hook); /* Remove extra reference */ 321 322 return (0); 323 } /* ng_btsocket_l2cap_node_disconnect */ 324 325 /* 326 * Process incoming messages 327 */ 328 329 static int 330 ng_btsocket_l2cap_node_rcvmsg(node_p node, item_p item, hook_p hook) 331 { 332 struct ng_mesg *msg = NGI_MSG(item); /* item still has message */ 333 int error = 0; 334 335 if (msg != NULL && msg->header.typecookie == NGM_L2CAP_COOKIE) { 336 mtx_lock(&ng_btsocket_l2cap_queue_mtx); 337 if (NG_BT_ITEMQ_FULL(&ng_btsocket_l2cap_queue)) { 338 NG_BTSOCKET_L2CAP_ERR( 339 "%s: Input queue is full (msg)\n", __func__); 340 341 NG_BT_ITEMQ_DROP(&ng_btsocket_l2cap_queue); 342 NG_FREE_ITEM(item); 343 error = ENOBUFS; 344 } else { 345 if (hook != NULL) { 346 NG_HOOK_REF(hook); 347 NGI_SET_HOOK(item, hook); 348 } 349 350 NG_BT_ITEMQ_ENQUEUE(&ng_btsocket_l2cap_queue, item); 351 error = ng_btsocket_l2cap_wakeup_input_task(); 352 } 353 mtx_unlock(&ng_btsocket_l2cap_queue_mtx); 354 } else { 355 NG_FREE_ITEM(item); 356 error = EINVAL; 357 } 358 359 return (error); 360 } /* ng_btsocket_l2cap_node_rcvmsg */ 361 362 /* 363 * Receive data on a hook 364 */ 365 366 static int 367 ng_btsocket_l2cap_node_rcvdata(hook_p hook, item_p item) 368 { 369 int error = 0; 370 371 mtx_lock(&ng_btsocket_l2cap_queue_mtx); 372 if (NG_BT_ITEMQ_FULL(&ng_btsocket_l2cap_queue)) { 373 NG_BTSOCKET_L2CAP_ERR( 374 "%s: Input queue is full (data)\n", __func__); 375 376 NG_BT_ITEMQ_DROP(&ng_btsocket_l2cap_queue); 377 NG_FREE_ITEM(item); 378 error = ENOBUFS; 379 } else { 380 NG_HOOK_REF(hook); 381 NGI_SET_HOOK(item, hook); 382 383 NG_BT_ITEMQ_ENQUEUE(&ng_btsocket_l2cap_queue, item); 384 error = ng_btsocket_l2cap_wakeup_input_task(); 385 } 386 mtx_unlock(&ng_btsocket_l2cap_queue_mtx); 387 388 return (error); 389 } /* ng_btsocket_l2cap_node_rcvdata */ 390 391 /* 392 * Process L2CA_Connect respose. Socket layer must have initiated connection, 393 * so we have to have a socket associated with message token. 394 */ 395 396 static int 397 ng_btsocket_l2cap_process_l2ca_con_req_rsp(struct ng_mesg *msg, 398 ng_btsocket_l2cap_rtentry_p rt) 399 { 400 ng_l2cap_l2ca_con_op *op = NULL; 401 ng_btsocket_l2cap_pcb_t *pcb = NULL; 402 int error = 0; 403 404 if (msg->header.arglen != sizeof(*op)) 405 return (EMSGSIZE); 406 407 op = (ng_l2cap_l2ca_con_op *)(msg->data); 408 409 mtx_lock(&ng_btsocket_l2cap_sockets_mtx); 410 411 /* Look for the socket with the token */ 412 pcb = ng_btsocket_l2cap_pcb_by_token(msg->header.token); 413 if (pcb == NULL) { 414 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx); 415 return (ENOENT); 416 } 417 418 mtx_lock(&pcb->pcb_mtx); 419 420 NG_BTSOCKET_L2CAP_INFO( 421 "%s: Got L2CA_Connect response, token=%d, src bdaddr=%x:%x:%x:%x:%x:%x, " \ 422 "dst bdaddr=%x:%x:%x:%x:%x:%x, psm=%d, lcid=%d, result=%d, status=%d, " \ 423 "state=%d\n", __func__, msg->header.token, 424 pcb->src.b[5], pcb->src.b[4], pcb->src.b[3], 425 pcb->src.b[2], pcb->src.b[1], pcb->src.b[0], 426 pcb->dst.b[5], pcb->dst.b[4], pcb->dst.b[3], 427 pcb->dst.b[2], pcb->dst.b[1], pcb->dst.b[0], 428 pcb->psm, op->lcid, op->result, op->status, 429 pcb->state); 430 431 if (pcb->state != NG_BTSOCKET_L2CAP_CONNECTING) { 432 mtx_unlock(&pcb->pcb_mtx); 433 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx); 434 435 return (ENOENT); 436 } 437 438 ng_btsocket_l2cap_untimeout(pcb); 439 440 if (op->result == NG_L2CAP_PENDING) { 441 ng_btsocket_l2cap_timeout(pcb); 442 mtx_unlock(&pcb->pcb_mtx); 443 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx); 444 445 return (0); 446 } 447 448 if (op->result == NG_L2CAP_SUCCESS) { 449 /* 450 * Channel is now open, so update local channel ID and 451 * start configuration process. Source and destination 452 * addresses as well as route must be already set. 453 */ 454 455 pcb->cid = op->lcid; 456 457 error = ng_btsocket_l2cap_send_l2ca_cfg_req(pcb); 458 if (error != 0) { 459 /* Send disconnect request with "zero" token */ 460 ng_btsocket_l2cap_send_l2ca_discon_req(0, pcb); 461 462 /* ... and close the socket */ 463 pcb->state = NG_BTSOCKET_L2CAP_CLOSED; 464 soisdisconnected(pcb->so); 465 } else { 466 pcb->cfg_state = NG_BTSOCKET_L2CAP_CFG_IN_SENT; 467 pcb->state = NG_BTSOCKET_L2CAP_CONFIGURING; 468 469 ng_btsocket_l2cap_timeout(pcb); 470 } 471 } else { 472 /* 473 * We have failed to open connection, so convert result 474 * code to "errno" code and disconnect the socket. Channel 475 * already has been closed. 476 */ 477 478 pcb->so->so_error = ng_btsocket_l2cap_result2errno(op->result); 479 pcb->state = NG_BTSOCKET_L2CAP_CLOSED; 480 soisdisconnected(pcb->so); 481 } 482 483 mtx_unlock(&pcb->pcb_mtx); 484 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx); 485 486 return (error); 487 } /* ng_btsocket_l2cap_process_l2ca_con_req_rsp */ 488 489 /* 490 * Process L2CA_ConnectRsp response 491 */ 492 493 static int 494 ng_btsocket_l2cap_process_l2ca_con_rsp_rsp(struct ng_mesg *msg, 495 ng_btsocket_l2cap_rtentry_p rt) 496 { 497 ng_l2cap_l2ca_con_rsp_op *op = NULL; 498 ng_btsocket_l2cap_pcb_t *pcb = NULL; 499 500 if (msg->header.arglen != sizeof(*op)) 501 return (EMSGSIZE); 502 503 op = (ng_l2cap_l2ca_con_rsp_op *)(msg->data); 504 505 mtx_lock(&ng_btsocket_l2cap_sockets_mtx); 506 507 /* Look for the socket with the token */ 508 pcb = ng_btsocket_l2cap_pcb_by_token(msg->header.token); 509 if (pcb == NULL) { 510 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx); 511 return (ENOENT); 512 } 513 514 mtx_lock(&pcb->pcb_mtx); 515 516 NG_BTSOCKET_L2CAP_INFO( 517 "%s: Got L2CA_ConnectRsp response, token=%d, src bdaddr=%x:%x:%x:%x:%x:%x, " \ 518 "dst bdaddr=%x:%x:%x:%x:%x:%x, psm=%d, lcid=%d, result=%d, state=%d\n", 519 __func__, msg->header.token, 520 pcb->src.b[5], pcb->src.b[4], pcb->src.b[3], 521 pcb->src.b[2], pcb->src.b[1], pcb->src.b[0], 522 pcb->dst.b[5], pcb->dst.b[4], pcb->dst.b[3], 523 pcb->dst.b[2], pcb->dst.b[1], pcb->dst.b[0], 524 pcb->psm, pcb->cid, op->result, pcb->state); 525 526 if (pcb->state != NG_BTSOCKET_L2CAP_CONNECTING) { 527 mtx_unlock(&pcb->pcb_mtx); 528 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx); 529 530 return (ENOENT); 531 } 532 533 ng_btsocket_l2cap_untimeout(pcb); 534 535 /* Check the result and disconnect the socket on failure */ 536 if (op->result != NG_L2CAP_SUCCESS) { 537 /* Close the socket - channel already closed */ 538 pcb->so->so_error = ng_btsocket_l2cap_result2errno(op->result); 539 pcb->state = NG_BTSOCKET_L2CAP_CLOSED; 540 soisdisconnected(pcb->so); 541 } else { 542 /* Move to CONFIGURING state and wait for CONFIG_IND */ 543 pcb->cfg_state = 0; 544 pcb->state = NG_BTSOCKET_L2CAP_CONFIGURING; 545 ng_btsocket_l2cap_timeout(pcb); 546 } 547 548 mtx_unlock(&pcb->pcb_mtx); 549 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx); 550 551 return (0); 552 } /* ng_btsocket_process_l2ca_con_rsp_rsp */ 553 554 /* 555 * Process L2CA_Connect indicator. Find socket that listens on address 556 * and PSM. Find exact or closest match. Create new socket and initiate 557 * connection. 558 */ 559 560 static int 561 ng_btsocket_l2cap_process_l2ca_con_ind(struct ng_mesg *msg, 562 ng_btsocket_l2cap_rtentry_p rt) 563 { 564 ng_l2cap_l2ca_con_ind_ip *ip = NULL; 565 ng_btsocket_l2cap_pcb_t *pcb = NULL, *pcb1 = NULL; 566 int error = 0; 567 u_int32_t token = 0; 568 u_int16_t result = 0; 569 570 if (msg->header.arglen != sizeof(*ip)) 571 return (EMSGSIZE); 572 573 ip = (ng_l2cap_l2ca_con_ind_ip *)(msg->data); 574 575 NG_BTSOCKET_L2CAP_INFO( 576 "%s: Got L2CA_Connect indicator, src bdaddr=%x:%x:%x:%x:%x:%x, " \ 577 "dst bdaddr=%x:%x:%x:%x:%x:%x, psm=%d, lcid=%d, ident=%d\n", 578 __func__, 579 rt->src.b[5], rt->src.b[4], rt->src.b[3], 580 rt->src.b[2], rt->src.b[1], rt->src.b[0], 581 ip->bdaddr.b[5], ip->bdaddr.b[4], ip->bdaddr.b[3], 582 ip->bdaddr.b[2], ip->bdaddr.b[1], ip->bdaddr.b[0], 583 ip->psm, ip->lcid, ip->ident); 584 585 mtx_lock(&ng_btsocket_l2cap_sockets_mtx); 586 587 pcb = ng_btsocket_l2cap_pcb_by_addr(&rt->src, ip->psm); 588 if (pcb != NULL) { 589 struct socket *so1 = NULL; 590 591 mtx_lock(&pcb->pcb_mtx); 592 593 /* 594 * First check the pending connections queue and if we have 595 * space then create new socket and set proper source address. 596 */ 597 598 if (pcb->so->so_qlen <= pcb->so->so_qlimit) { 599 CURVNET_SET(pcb->so->so_vnet); 600 so1 = sonewconn(pcb->so, 0); 601 CURVNET_RESTORE(); 602 } 603 604 if (so1 == NULL) { 605 result = NG_L2CAP_NO_RESOURCES; 606 goto respond; 607 } 608 609 /* 610 * If we got here than we have created new socket. So complete 611 * connection. If we we listening on specific address then copy 612 * source address from listening socket, otherwise copy source 613 * address from hook's routing information. 614 */ 615 616 pcb1 = so2l2cap_pcb(so1); 617 KASSERT((pcb1 != NULL), 618 ("%s: pcb1 == NULL\n", __func__)); 619 620 mtx_lock(&pcb1->pcb_mtx); 621 622 if (bcmp(&pcb->src, NG_HCI_BDADDR_ANY, sizeof(pcb->src)) != 0) 623 bcopy(&pcb->src, &pcb1->src, sizeof(pcb1->src)); 624 else 625 bcopy(&rt->src, &pcb1->src, sizeof(pcb1->src)); 626 627 pcb1->flags &= ~NG_BTSOCKET_L2CAP_CLIENT; 628 629 bcopy(&ip->bdaddr, &pcb1->dst, sizeof(pcb1->dst)); 630 pcb1->psm = ip->psm; 631 pcb1->cid = ip->lcid; 632 pcb1->rt = rt; 633 634 /* Copy socket settings */ 635 pcb1->imtu = pcb->imtu; 636 bcopy(&pcb->oflow, &pcb1->oflow, sizeof(pcb1->oflow)); 637 pcb1->flush_timo = pcb->flush_timo; 638 639 token = pcb1->token; 640 } else 641 /* Nobody listens on requested BDADDR/PSM */ 642 result = NG_L2CAP_PSM_NOT_SUPPORTED; 643 644 respond: 645 error = ng_btsocket_l2cap_send_l2ca_con_rsp_req(token, rt, 646 &ip->bdaddr, ip->ident, ip->lcid, result); 647 if (pcb1 != NULL) { 648 if (error != 0) { 649 pcb1->so->so_error = error; 650 pcb1->state = NG_BTSOCKET_L2CAP_CLOSED; 651 soisdisconnected(pcb1->so); 652 } else { 653 pcb1->state = NG_BTSOCKET_L2CAP_CONNECTING; 654 soisconnecting(pcb1->so); 655 656 ng_btsocket_l2cap_timeout(pcb1); 657 } 658 659 mtx_unlock(&pcb1->pcb_mtx); 660 } 661 662 if (pcb != NULL) 663 mtx_unlock(&pcb->pcb_mtx); 664 665 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx); 666 667 return (error); 668 } /* ng_btsocket_l2cap_process_l2ca_con_ind */ 669 670 /* 671 * Process L2CA_Config response 672 */ 673 674 static int 675 ng_btsocket_l2cap_process_l2ca_cfg_req_rsp(struct ng_mesg *msg, 676 ng_btsocket_l2cap_rtentry_p rt) 677 { 678 ng_l2cap_l2ca_cfg_op *op = NULL; 679 ng_btsocket_l2cap_pcb_p pcb = NULL; 680 681 if (msg->header.arglen != sizeof(*op)) 682 return (EMSGSIZE); 683 684 op = (ng_l2cap_l2ca_cfg_op *)(msg->data); 685 686 mtx_lock(&ng_btsocket_l2cap_sockets_mtx); 687 688 /* 689 * Socket must have issued a Configure request, so we must have a 690 * socket that wants to be configured. Use Netgraph message token 691 * to find it 692 */ 693 694 pcb = ng_btsocket_l2cap_pcb_by_token(msg->header.token); 695 if (pcb == NULL) { 696 /* 697 * XXX FIXME what to do here? We could not find a 698 * socket with requested token. We even can not send 699 * Disconnect, because we do not know channel ID 700 */ 701 702 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx); 703 return (ENOENT); 704 } 705 706 mtx_lock(&pcb->pcb_mtx); 707 708 NG_BTSOCKET_L2CAP_INFO( 709 "%s: Got L2CA_Config response, token=%d, src bdaddr=%x:%x:%x:%x:%x:%x, " \ 710 "dst bdaddr=%x:%x:%x:%x:%x:%x, psm=%d, lcid=%d, result=%d, state=%d, " \ 711 "cfg_state=%x\n", 712 __func__, msg->header.token, 713 pcb->src.b[5], pcb->src.b[4], pcb->src.b[3], 714 pcb->src.b[2], pcb->src.b[1], pcb->src.b[0], 715 pcb->dst.b[5], pcb->dst.b[4], pcb->dst.b[3], 716 pcb->dst.b[2], pcb->dst.b[1], pcb->dst.b[0], 717 pcb->psm, pcb->cid, op->result, pcb->state, pcb->cfg_state); 718 719 if (pcb->state != NG_BTSOCKET_L2CAP_CONFIGURING) { 720 mtx_unlock(&pcb->pcb_mtx); 721 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx); 722 723 return (ENOENT); 724 } 725 726 if (op->result == NG_L2CAP_SUCCESS) { 727 /* 728 * XXX FIXME Actually set flush and link timeout. 729 * Set QoS here if required. Resolve conficts (flush_timo). 730 * Save incoming MTU (peer's outgoing MTU) and outgoing flow 731 * spec. 732 */ 733 734 pcb->imtu = op->imtu; 735 bcopy(&op->oflow, &pcb->oflow, sizeof(pcb->oflow)); 736 pcb->flush_timo = op->flush_timo; 737 738 /* 739 * We have configured incoming side, so record it and check 740 * if configuration is complete. If complete then mark socket 741 * as connected, otherwise wait for the peer. 742 */ 743 744 pcb->cfg_state &= ~NG_BTSOCKET_L2CAP_CFG_IN_SENT; 745 pcb->cfg_state |= NG_BTSOCKET_L2CAP_CFG_IN; 746 747 if (pcb->cfg_state == NG_BTSOCKET_L2CAP_CFG_BOTH) { 748 /* Configuration complete - mark socket as open */ 749 ng_btsocket_l2cap_untimeout(pcb); 750 pcb->state = NG_BTSOCKET_L2CAP_OPEN; 751 soisconnected(pcb->so); 752 } 753 } else { 754 /* 755 * Something went wrong. Could be unacceptable parameters, 756 * reject or unknown option. That's too bad, but we will 757 * not negotiate. Send Disconnect and close the channel. 758 */ 759 760 ng_btsocket_l2cap_untimeout(pcb); 761 762 switch (op->result) { 763 case NG_L2CAP_UNACCEPTABLE_PARAMS: 764 case NG_L2CAP_UNKNOWN_OPTION: 765 pcb->so->so_error = EINVAL; 766 break; 767 768 default: 769 pcb->so->so_error = ECONNRESET; 770 break; 771 } 772 773 /* Send disconnect with "zero" token */ 774 ng_btsocket_l2cap_send_l2ca_discon_req(0, pcb); 775 776 /* ... and close the socket */ 777 pcb->state = NG_BTSOCKET_L2CAP_CLOSED; 778 soisdisconnected(pcb->so); 779 } 780 781 mtx_unlock(&pcb->pcb_mtx); 782 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx); 783 784 return (0); 785 } /* ng_btsocket_l2cap_process_l2ca_cfg_req_rsp */ 786 787 /* 788 * Process L2CA_ConfigRsp response 789 */ 790 791 static int 792 ng_btsocket_l2cap_process_l2ca_cfg_rsp_rsp(struct ng_mesg *msg, 793 ng_btsocket_l2cap_rtentry_p rt) 794 { 795 ng_l2cap_l2ca_cfg_rsp_op *op = NULL; 796 ng_btsocket_l2cap_pcb_t *pcb = NULL; 797 int error = 0; 798 799 if (msg->header.arglen != sizeof(*op)) 800 return (EMSGSIZE); 801 802 op = (ng_l2cap_l2ca_cfg_rsp_op *)(msg->data); 803 804 mtx_lock(&ng_btsocket_l2cap_sockets_mtx); 805 806 /* Look for the socket with the token */ 807 pcb = ng_btsocket_l2cap_pcb_by_token(msg->header.token); 808 if (pcb == NULL) { 809 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx); 810 return (ENOENT); 811 } 812 813 mtx_lock(&pcb->pcb_mtx); 814 815 NG_BTSOCKET_L2CAP_INFO( 816 "%s: Got L2CA_ConfigRsp response, token=%d, src bdaddr=%x:%x:%x:%x:%x:%x, " \ 817 "dst bdaddr=%x:%x:%x:%x:%x:%x, psm=%d, lcid=%d, result=%d, state=%d, " \ 818 "cfg_state=%x\n", 819 __func__, msg->header.token, 820 pcb->src.b[5], pcb->src.b[4], pcb->src.b[3], 821 pcb->src.b[2], pcb->src.b[1], pcb->src.b[0], 822 pcb->dst.b[5], pcb->dst.b[4], pcb->dst.b[3], 823 pcb->dst.b[2], pcb->dst.b[1], pcb->dst.b[0], 824 pcb->psm, pcb->cid, op->result, pcb->state, pcb->cfg_state); 825 826 if (pcb->state != NG_BTSOCKET_L2CAP_CONFIGURING) { 827 mtx_unlock(&pcb->pcb_mtx); 828 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx); 829 830 return (ENOENT); 831 } 832 833 /* Check the result and disconnect socket of failure */ 834 if (op->result != NG_L2CAP_SUCCESS) 835 goto disconnect; 836 837 /* 838 * Now we done with remote side configuration. Configure local 839 * side if we have not done it yet. 840 */ 841 842 pcb->cfg_state &= ~NG_BTSOCKET_L2CAP_CFG_OUT_SENT; 843 pcb->cfg_state |= NG_BTSOCKET_L2CAP_CFG_OUT; 844 845 if (pcb->cfg_state == NG_BTSOCKET_L2CAP_CFG_BOTH) { 846 /* Configuration complete - mask socket as open */ 847 ng_btsocket_l2cap_untimeout(pcb); 848 pcb->state = NG_BTSOCKET_L2CAP_OPEN; 849 soisconnected(pcb->so); 850 } else { 851 if (!(pcb->cfg_state & NG_BTSOCKET_L2CAP_CFG_IN_SENT)) { 852 /* Send L2CA_Config request - incoming path */ 853 error = ng_btsocket_l2cap_send_l2ca_cfg_req(pcb); 854 if (error != 0) 855 goto disconnect; 856 857 pcb->cfg_state |= NG_BTSOCKET_L2CAP_CFG_IN_SENT; 858 } 859 } 860 861 mtx_unlock(&pcb->pcb_mtx); 862 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx); 863 864 return (error); 865 866 disconnect: 867 ng_btsocket_l2cap_untimeout(pcb); 868 869 /* Send disconnect with "zero" token */ 870 ng_btsocket_l2cap_send_l2ca_discon_req(0, pcb); 871 872 /* ... and close the socket */ 873 pcb->state = NG_BTSOCKET_L2CAP_CLOSED; 874 soisdisconnected(pcb->so); 875 876 mtx_unlock(&pcb->pcb_mtx); 877 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx); 878 879 return (error); 880 } /* ng_btsocket_l2cap_process_l2ca_cfg_rsp_rsp */ 881 882 /* 883 * Process L2CA_Config indicator 884 */ 885 886 static int 887 ng_btsocket_l2cap_process_l2ca_cfg_ind(struct ng_mesg *msg, 888 ng_btsocket_l2cap_rtentry_p rt) 889 { 890 ng_l2cap_l2ca_cfg_ind_ip *ip = NULL; 891 ng_btsocket_l2cap_pcb_t *pcb = NULL; 892 int error = 0; 893 894 if (msg->header.arglen != sizeof(*ip)) 895 return (EMSGSIZE); 896 897 ip = (ng_l2cap_l2ca_cfg_ind_ip *)(msg->data); 898 899 mtx_lock(&ng_btsocket_l2cap_sockets_mtx); 900 901 /* Check for the open socket that has given channel ID */ 902 pcb = ng_btsocket_l2cap_pcb_by_cid(&rt->src, ip->lcid); 903 if (pcb == NULL) { 904 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx); 905 return (ENOENT); 906 } 907 908 mtx_lock(&pcb->pcb_mtx); 909 910 NG_BTSOCKET_L2CAP_INFO( 911 "%s: Got L2CA_Config indicator, src bdaddr=%x:%x:%x:%x:%x:%x, " \ 912 "dst bdaddr=%x:%x:%x:%x:%x:%x, psm=%d, lcid=%d, state=%d, cfg_state=%x\n", 913 __func__, 914 pcb->src.b[5], pcb->src.b[4], pcb->src.b[3], 915 pcb->src.b[2], pcb->src.b[1], pcb->src.b[0], 916 pcb->dst.b[5], pcb->dst.b[4], pcb->dst.b[3], 917 pcb->dst.b[2], pcb->dst.b[1], pcb->dst.b[0], 918 pcb->psm, pcb->cid, pcb->state, pcb->cfg_state); 919 920 /* XXX FIXME re-configuration on open socket */ 921 if (pcb->state != NG_BTSOCKET_L2CAP_CONFIGURING) { 922 mtx_unlock(&pcb->pcb_mtx); 923 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx); 924 925 return (ENOENT); 926 } 927 928 /* 929 * XXX FIXME Actually set flush and link timeout. Set QoS here if 930 * required. Resolve conficts (flush_timo). Note outgoing MTU (peer's 931 * incoming MTU) and incoming flow spec. 932 */ 933 934 pcb->omtu = ip->omtu; 935 bcopy(&ip->iflow, &pcb->iflow, sizeof(pcb->iflow)); 936 pcb->flush_timo = ip->flush_timo; 937 938 /* 939 * Send L2CA_Config response to our peer and check for the errors, 940 * if any send disconnect to close the channel. 941 */ 942 943 if (!(pcb->cfg_state & NG_BTSOCKET_L2CAP_CFG_OUT_SENT)) { 944 error = ng_btsocket_l2cap_send_l2ca_cfg_rsp(pcb); 945 if (error != 0) { 946 ng_btsocket_l2cap_untimeout(pcb); 947 948 pcb->so->so_error = error; 949 950 /* Send disconnect with "zero" token */ 951 ng_btsocket_l2cap_send_l2ca_discon_req(0, pcb); 952 953 /* ... and close the socket */ 954 pcb->state = NG_BTSOCKET_L2CAP_CLOSED; 955 soisdisconnected(pcb->so); 956 } else 957 pcb->cfg_state |= NG_BTSOCKET_L2CAP_CFG_OUT_SENT; 958 } 959 960 mtx_unlock(&pcb->pcb_mtx); 961 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx); 962 963 return (error); 964 } /* ng_btsocket_l2cap_process_l2cap_cfg_ind */ 965 966 /* 967 * Process L2CA_Disconnect response 968 */ 969 970 static int 971 ng_btsocket_l2cap_process_l2ca_discon_rsp(struct ng_mesg *msg, 972 ng_btsocket_l2cap_rtentry_p rt) 973 { 974 ng_l2cap_l2ca_discon_op *op = NULL; 975 ng_btsocket_l2cap_pcb_t *pcb = NULL; 976 977 /* Check message */ 978 if (msg->header.arglen != sizeof(*op)) 979 return (EMSGSIZE); 980 981 op = (ng_l2cap_l2ca_discon_op *)(msg->data); 982 983 mtx_lock(&ng_btsocket_l2cap_sockets_mtx); 984 985 /* 986 * Socket layer must have issued L2CA_Disconnect request, so there 987 * must be a socket that wants to be disconnected. Use Netgraph 988 * message token to find it. 989 */ 990 991 pcb = ng_btsocket_l2cap_pcb_by_token(msg->header.token); 992 if (pcb == NULL) { 993 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx); 994 return (0); 995 } 996 997 mtx_lock(&pcb->pcb_mtx); 998 999 /* XXX Close socket no matter what op->result says */ 1000 if (pcb->state != NG_BTSOCKET_L2CAP_CLOSED) { 1001 NG_BTSOCKET_L2CAP_INFO( 1002 "%s: Got L2CA_Disconnect response, token=%d, src bdaddr=%x:%x:%x:%x:%x:%x, " \ 1003 "dst bdaddr=%x:%x:%x:%x:%x:%x, psm=%d, lcid=%d, result=%d, state=%d\n", 1004 __func__, msg->header.token, 1005 pcb->src.b[5], pcb->src.b[4], pcb->src.b[3], 1006 pcb->src.b[2], pcb->src.b[1], pcb->src.b[0], 1007 pcb->dst.b[5], pcb->dst.b[4], pcb->dst.b[3], 1008 pcb->dst.b[2], pcb->dst.b[1], pcb->dst.b[0], 1009 pcb->psm, pcb->cid, op->result, pcb->state); 1010 1011 ng_btsocket_l2cap_untimeout(pcb); 1012 1013 pcb->state = NG_BTSOCKET_L2CAP_CLOSED; 1014 soisdisconnected(pcb->so); 1015 } 1016 1017 mtx_unlock(&pcb->pcb_mtx); 1018 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx); 1019 1020 return (0); 1021 } /* ng_btsocket_l2cap_process_l2ca_discon_rsp */ 1022 1023 /* 1024 * Process L2CA_Disconnect indicator 1025 */ 1026 1027 static int 1028 ng_btsocket_l2cap_process_l2ca_discon_ind(struct ng_mesg *msg, 1029 ng_btsocket_l2cap_rtentry_p rt) 1030 { 1031 ng_l2cap_l2ca_discon_ind_ip *ip = NULL; 1032 ng_btsocket_l2cap_pcb_t *pcb = NULL; 1033 1034 /* Check message */ 1035 if (msg->header.arglen != sizeof(*ip)) 1036 return (EMSGSIZE); 1037 1038 ip = (ng_l2cap_l2ca_discon_ind_ip *)(msg->data); 1039 1040 mtx_lock(&ng_btsocket_l2cap_sockets_mtx); 1041 1042 /* Look for the socket with given channel ID */ 1043 pcb = ng_btsocket_l2cap_pcb_by_cid(&rt->src, ip->lcid); 1044 if (pcb == NULL) { 1045 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx); 1046 return (0); 1047 } 1048 1049 /* 1050 * Channel has already been destroyed, so disconnect the socket 1051 * and be done with it. If there was any pending request we can 1052 * not do anything here anyway. 1053 */ 1054 1055 mtx_lock(&pcb->pcb_mtx); 1056 1057 NG_BTSOCKET_L2CAP_INFO( 1058 "%s: Got L2CA_Disconnect indicator, src bdaddr=%x:%x:%x:%x:%x:%x, " \ 1059 "dst bdaddr=%x:%x:%x:%x:%x:%x, psm=%d, lcid=%d, state=%d\n", 1060 __func__, 1061 pcb->src.b[5], pcb->src.b[4], pcb->src.b[3], 1062 pcb->src.b[2], pcb->src.b[1], pcb->src.b[0], 1063 pcb->dst.b[5], pcb->dst.b[4], pcb->dst.b[3], 1064 pcb->dst.b[2], pcb->dst.b[1], pcb->dst.b[0], 1065 pcb->psm, pcb->cid, pcb->state); 1066 1067 if (pcb->flags & NG_BTSOCKET_L2CAP_TIMO) 1068 ng_btsocket_l2cap_untimeout(pcb); 1069 1070 pcb->state = NG_BTSOCKET_L2CAP_CLOSED; 1071 soisdisconnected(pcb->so); 1072 1073 mtx_unlock(&pcb->pcb_mtx); 1074 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx); 1075 1076 return (0); 1077 } /* ng_btsocket_l2cap_process_l2ca_discon_ind */ 1078 1079 /* 1080 * Process L2CA_Write response 1081 */ 1082 1083 static int 1084 ng_btsocket_l2cap_process_l2ca_write_rsp(struct ng_mesg *msg, 1085 ng_btsocket_l2cap_rtentry_p rt) 1086 { 1087 ng_l2cap_l2ca_write_op *op = NULL; 1088 ng_btsocket_l2cap_pcb_t *pcb = NULL; 1089 1090 /* Check message */ 1091 if (msg->header.arglen != sizeof(*op)) 1092 return (EMSGSIZE); 1093 1094 op = (ng_l2cap_l2ca_write_op *)(msg->data); 1095 1096 mtx_lock(&ng_btsocket_l2cap_sockets_mtx); 1097 1098 /* Look for the socket with given token */ 1099 pcb = ng_btsocket_l2cap_pcb_by_token(msg->header.token); 1100 if (pcb == NULL) { 1101 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx); 1102 return (ENOENT); 1103 } 1104 1105 mtx_lock(&pcb->pcb_mtx); 1106 1107 NG_BTSOCKET_L2CAP_INFO( 1108 "%s: Got L2CA_Write response, src bdaddr=%x:%x:%x:%x:%x:%x, " \ 1109 "dst bdaddr=%x:%x:%x:%x:%x:%x, psm=%d, lcid=%d, result=%d, length=%d, " \ 1110 "state=%d\n", __func__, 1111 pcb->src.b[5], pcb->src.b[4], pcb->src.b[3], 1112 pcb->src.b[2], pcb->src.b[1], pcb->src.b[0], 1113 pcb->dst.b[5], pcb->dst.b[4], pcb->dst.b[3], 1114 pcb->dst.b[2], pcb->dst.b[1], pcb->dst.b[0], 1115 pcb->psm, pcb->cid, op->result, op->length, 1116 pcb->state); 1117 1118 if (pcb->state != NG_BTSOCKET_L2CAP_OPEN) { 1119 mtx_unlock(&pcb->pcb_mtx); 1120 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx); 1121 1122 return (ENOENT); 1123 } 1124 1125 ng_btsocket_l2cap_untimeout(pcb); 1126 1127 /* 1128 * Check if we have more data to send 1129 */ 1130 1131 sbdroprecord(&pcb->so->so_snd); 1132 if (pcb->so->so_snd.sb_cc > 0) { 1133 if (ng_btsocket_l2cap_send2(pcb) == 0) 1134 ng_btsocket_l2cap_timeout(pcb); 1135 else 1136 sbdroprecord(&pcb->so->so_snd); /* XXX */ 1137 } 1138 1139 /* 1140 * Now set the result, drop packet from the socket send queue and 1141 * ask for more (wakeup sender) 1142 */ 1143 1144 pcb->so->so_error = ng_btsocket_l2cap_result2errno(op->result); 1145 sowwakeup(pcb->so); 1146 1147 mtx_unlock(&pcb->pcb_mtx); 1148 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx); 1149 1150 return (0); 1151 } /* ng_btsocket_l2cap_process_l2ca_write_rsp */ 1152 1153 /* 1154 * Send L2CA_Connect request 1155 */ 1156 1157 static int 1158 ng_btsocket_l2cap_send_l2ca_con_req(ng_btsocket_l2cap_pcb_p pcb) 1159 { 1160 struct ng_mesg *msg = NULL; 1161 ng_l2cap_l2ca_con_ip *ip = NULL; 1162 int error = 0; 1163 1164 mtx_assert(&pcb->pcb_mtx, MA_OWNED); 1165 1166 if (pcb->rt == NULL || 1167 pcb->rt->hook == NULL || NG_HOOK_NOT_VALID(pcb->rt->hook)) 1168 return (ENETDOWN); 1169 1170 NG_MKMESSAGE(msg, NGM_L2CAP_COOKIE, NGM_L2CAP_L2CA_CON, 1171 sizeof(*ip), M_NOWAIT); 1172 if (msg == NULL) 1173 return (ENOMEM); 1174 1175 msg->header.token = pcb->token; 1176 1177 ip = (ng_l2cap_l2ca_con_ip *)(msg->data); 1178 bcopy(&pcb->dst, &ip->bdaddr, sizeof(ip->bdaddr)); 1179 ip->psm = pcb->psm; 1180 1181 NG_SEND_MSG_HOOK(error, ng_btsocket_l2cap_node, msg,pcb->rt->hook, 0); 1182 1183 return (error); 1184 } /* ng_btsocket_l2cap_send_l2ca_con_req */ 1185 1186 /* 1187 * Send L2CA_Connect response 1188 */ 1189 1190 static int 1191 ng_btsocket_l2cap_send_l2ca_con_rsp_req(u_int32_t token, 1192 ng_btsocket_l2cap_rtentry_p rt, bdaddr_p dst, int ident, 1193 int lcid, int result) 1194 { 1195 struct ng_mesg *msg = NULL; 1196 ng_l2cap_l2ca_con_rsp_ip *ip = NULL; 1197 int error = 0; 1198 1199 if (rt == NULL || rt->hook == NULL || NG_HOOK_NOT_VALID(rt->hook)) 1200 return (ENETDOWN); 1201 1202 NG_MKMESSAGE(msg, NGM_L2CAP_COOKIE, NGM_L2CAP_L2CA_CON_RSP, 1203 sizeof(*ip), M_NOWAIT); 1204 if (msg == NULL) 1205 return (ENOMEM); 1206 1207 msg->header.token = token; 1208 1209 ip = (ng_l2cap_l2ca_con_rsp_ip *)(msg->data); 1210 bcopy(dst, &ip->bdaddr, sizeof(ip->bdaddr)); 1211 ip->ident = ident; 1212 ip->lcid = lcid; 1213 ip->result = result; 1214 ip->status = 0; 1215 1216 NG_SEND_MSG_HOOK(error, ng_btsocket_l2cap_node, msg, rt->hook, 0); 1217 1218 return (error); 1219 } /* ng_btsocket_l2cap_send_l2ca_con_rsp_req */ 1220 1221 /* 1222 * Send L2CA_Config request 1223 */ 1224 1225 static int 1226 ng_btsocket_l2cap_send_l2ca_cfg_req(ng_btsocket_l2cap_pcb_p pcb) 1227 { 1228 struct ng_mesg *msg = NULL; 1229 ng_l2cap_l2ca_cfg_ip *ip = NULL; 1230 int error = 0; 1231 1232 mtx_assert(&pcb->pcb_mtx, MA_OWNED); 1233 1234 if (pcb->rt == NULL || 1235 pcb->rt->hook == NULL || NG_HOOK_NOT_VALID(pcb->rt->hook)) 1236 return (ENETDOWN); 1237 1238 NG_MKMESSAGE(msg, NGM_L2CAP_COOKIE, NGM_L2CAP_L2CA_CFG, 1239 sizeof(*ip), M_NOWAIT); 1240 if (msg == NULL) 1241 return (ENOMEM); 1242 1243 msg->header.token = pcb->token; 1244 1245 ip = (ng_l2cap_l2ca_cfg_ip *)(msg->data); 1246 ip->lcid = pcb->cid; 1247 ip->imtu = pcb->imtu; 1248 bcopy(&pcb->oflow, &ip->oflow, sizeof(ip->oflow)); 1249 ip->flush_timo = pcb->flush_timo; 1250 ip->link_timo = pcb->link_timo; 1251 1252 NG_SEND_MSG_HOOK(error, ng_btsocket_l2cap_node, msg,pcb->rt->hook, 0); 1253 1254 return (error); 1255 } /* ng_btsocket_l2cap_send_l2ca_cfg_req */ 1256 1257 /* 1258 * Send L2CA_Config response 1259 */ 1260 1261 static int 1262 ng_btsocket_l2cap_send_l2ca_cfg_rsp(ng_btsocket_l2cap_pcb_p pcb) 1263 { 1264 struct ng_mesg *msg = NULL; 1265 ng_l2cap_l2ca_cfg_rsp_ip *ip = NULL; 1266 int error = 0; 1267 1268 mtx_assert(&pcb->pcb_mtx, MA_OWNED); 1269 1270 if (pcb->rt == NULL || 1271 pcb->rt->hook == NULL || NG_HOOK_NOT_VALID(pcb->rt->hook)) 1272 return (ENETDOWN); 1273 1274 NG_MKMESSAGE(msg, NGM_L2CAP_COOKIE, NGM_L2CAP_L2CA_CFG_RSP, 1275 sizeof(*ip), M_NOWAIT); 1276 if (msg == NULL) 1277 return (ENOMEM); 1278 1279 msg->header.token = pcb->token; 1280 1281 ip = (ng_l2cap_l2ca_cfg_rsp_ip *)(msg->data); 1282 ip->lcid = pcb->cid; 1283 ip->omtu = pcb->omtu; 1284 bcopy(&pcb->iflow, &ip->iflow, sizeof(ip->iflow)); 1285 1286 NG_SEND_MSG_HOOK(error, ng_btsocket_l2cap_node, msg, pcb->rt->hook, 0); 1287 1288 return (error); 1289 } /* ng_btsocket_l2cap_send_l2ca_cfg_rsp */ 1290 1291 /* 1292 * Send L2CA_Disconnect request 1293 */ 1294 1295 static int 1296 ng_btsocket_l2cap_send_l2ca_discon_req(u_int32_t token, 1297 ng_btsocket_l2cap_pcb_p pcb) 1298 { 1299 struct ng_mesg *msg = NULL; 1300 ng_l2cap_l2ca_discon_ip *ip = NULL; 1301 int error = 0; 1302 1303 mtx_assert(&pcb->pcb_mtx, MA_OWNED); 1304 1305 if (pcb->rt == NULL || 1306 pcb->rt->hook == NULL || NG_HOOK_NOT_VALID(pcb->rt->hook)) 1307 return (ENETDOWN); 1308 1309 NG_MKMESSAGE(msg, NGM_L2CAP_COOKIE, NGM_L2CAP_L2CA_DISCON, 1310 sizeof(*ip), M_NOWAIT); 1311 if (msg == NULL) 1312 return (ENOMEM); 1313 1314 msg->header.token = token; 1315 1316 ip = (ng_l2cap_l2ca_discon_ip *)(msg->data); 1317 ip->lcid = pcb->cid; 1318 1319 NG_SEND_MSG_HOOK(error, ng_btsocket_l2cap_node, msg,pcb->rt->hook, 0); 1320 1321 return (error); 1322 } /* ng_btsocket_l2cap_send_l2ca_discon_req */ 1323 1324 /***************************************************************************** 1325 ***************************************************************************** 1326 ** Socket interface 1327 ***************************************************************************** 1328 *****************************************************************************/ 1329 1330 /* 1331 * L2CAP sockets data input routine 1332 */ 1333 1334 static void 1335 ng_btsocket_l2cap_data_input(struct mbuf *m, hook_p hook) 1336 { 1337 ng_l2cap_hdr_t *hdr = NULL; 1338 ng_l2cap_clt_hdr_t *clt_hdr = NULL; 1339 ng_btsocket_l2cap_pcb_t *pcb = NULL; 1340 ng_btsocket_l2cap_rtentry_t *rt = NULL; 1341 1342 if (hook == NULL) { 1343 NG_BTSOCKET_L2CAP_ALERT( 1344 "%s: Invalid source hook for L2CAP data packet\n", __func__); 1345 goto drop; 1346 } 1347 1348 rt = (ng_btsocket_l2cap_rtentry_t *) NG_HOOK_PRIVATE(hook); 1349 if (rt == NULL) { 1350 NG_BTSOCKET_L2CAP_ALERT( 1351 "%s: Could not find out source bdaddr for L2CAP data packet\n", __func__); 1352 goto drop; 1353 } 1354 1355 /* Make sure we can access header */ 1356 if (m->m_pkthdr.len < sizeof(*hdr)) { 1357 NG_BTSOCKET_L2CAP_ERR( 1358 "%s: L2CAP data packet too small, len=%d\n", __func__, m->m_pkthdr.len); 1359 goto drop; 1360 } 1361 1362 if (m->m_len < sizeof(*hdr)) { 1363 m = m_pullup(m, sizeof(*hdr)); 1364 if (m == NULL) 1365 goto drop; 1366 } 1367 1368 /* Strip L2CAP packet header and verify packet length */ 1369 hdr = mtod(m, ng_l2cap_hdr_t *); 1370 m_adj(m, sizeof(*hdr)); 1371 1372 if (hdr->length != m->m_pkthdr.len) { 1373 NG_BTSOCKET_L2CAP_ERR( 1374 "%s: Bad L2CAP data packet length, len=%d, length=%d\n", 1375 __func__, m->m_pkthdr.len, hdr->length); 1376 goto drop; 1377 } 1378 1379 /* 1380 * Now process packet. Two cases: 1381 * 1382 * 1) Normal packet (cid != 2) then find connected socket and append 1383 * mbuf to the socket queue. Wakeup socket. 1384 * 1385 * 2) Broadcast packet (cid == 2) then find all sockets that connected 1386 * to the given PSM and have SO_BROADCAST bit set and append mbuf 1387 * to the socket queue. Wakeup socket. 1388 */ 1389 1390 NG_BTSOCKET_L2CAP_INFO( 1391 "%s: Received L2CAP data packet: src bdaddr=%x:%x:%x:%x:%x:%x, " \ 1392 "dcid=%d, length=%d\n", 1393 __func__, 1394 rt->src.b[5], rt->src.b[4], rt->src.b[3], 1395 rt->src.b[2], rt->src.b[1], rt->src.b[0], 1396 hdr->dcid, hdr->length); 1397 1398 if (hdr->dcid >= NG_L2CAP_FIRST_CID) { 1399 1400 mtx_lock(&ng_btsocket_l2cap_sockets_mtx); 1401 1402 /* Normal packet: find connected socket */ 1403 pcb = ng_btsocket_l2cap_pcb_by_cid(&rt->src, hdr->dcid); 1404 if (pcb == NULL) { 1405 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx); 1406 goto drop; 1407 } 1408 1409 mtx_lock(&pcb->pcb_mtx); 1410 1411 if (pcb->state != NG_BTSOCKET_L2CAP_OPEN) { 1412 NG_BTSOCKET_L2CAP_ERR( 1413 "%s: No connected socket found, src bdaddr=%x:%x:%x:%x:%x:%x, dcid=%d, " \ 1414 "state=%d\n", __func__, 1415 rt->src.b[5], rt->src.b[4], rt->src.b[3], 1416 rt->src.b[2], rt->src.b[1], rt->src.b[0], 1417 hdr->dcid, pcb->state); 1418 1419 mtx_unlock(&pcb->pcb_mtx); 1420 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx); 1421 goto drop; 1422 } 1423 1424 /* Check packet size against socket's incoming MTU */ 1425 if (hdr->length > pcb->imtu) { 1426 NG_BTSOCKET_L2CAP_ERR( 1427 "%s: L2CAP data packet too big, src bdaddr=%x:%x:%x:%x:%x:%x, " \ 1428 "dcid=%d, length=%d, imtu=%d\n", 1429 __func__, 1430 rt->src.b[5], rt->src.b[4], rt->src.b[3], 1431 rt->src.b[2], rt->src.b[1], rt->src.b[0], 1432 hdr->dcid, hdr->length, pcb->imtu); 1433 1434 mtx_unlock(&pcb->pcb_mtx); 1435 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx); 1436 goto drop; 1437 } 1438 1439 /* Check if we have enough space in socket receive queue */ 1440 if (m->m_pkthdr.len > sbspace(&pcb->so->so_rcv)) { 1441 1442 /* 1443 * This is really bad. Receive queue on socket does 1444 * not have enough space for the packet. We do not 1445 * have any other choice but drop the packet. L2CAP 1446 * does not provide any flow control. 1447 */ 1448 1449 NG_BTSOCKET_L2CAP_ERR( 1450 "%s: Not enough space in socket receive queue. Dropping L2CAP data packet, " \ 1451 "src bdaddr=%x:%x:%x:%x:%x:%x, dcid=%d, len=%d, space=%ld\n", 1452 __func__, 1453 rt->src.b[5], rt->src.b[4], rt->src.b[3], 1454 rt->src.b[2], rt->src.b[1], rt->src.b[0], 1455 hdr->dcid, m->m_pkthdr.len, 1456 sbspace(&pcb->so->so_rcv)); 1457 1458 mtx_unlock(&pcb->pcb_mtx); 1459 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx); 1460 goto drop; 1461 } 1462 1463 /* Append packet to the socket receive queue and wakeup */ 1464 sbappendrecord(&pcb->so->so_rcv, m); 1465 m = NULL; 1466 1467 sorwakeup(pcb->so); 1468 1469 mtx_unlock(&pcb->pcb_mtx); 1470 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx); 1471 } else if (hdr->dcid == NG_L2CAP_CLT_CID) { 1472 /* Broadcast packet: give packet to all sockets */ 1473 1474 /* Check packet size against connectionless MTU */ 1475 if (hdr->length > NG_L2CAP_MTU_DEFAULT) { 1476 NG_BTSOCKET_L2CAP_ERR( 1477 "%s: Connectionless L2CAP data packet too big, " \ 1478 "src bdaddr=%x:%x:%x:%x:%x:%x, length=%d\n", 1479 __func__, 1480 rt->src.b[5], rt->src.b[4], rt->src.b[3], 1481 rt->src.b[2], rt->src.b[1], rt->src.b[0], 1482 hdr->length); 1483 goto drop; 1484 } 1485 1486 /* Make sure we can access connectionless header */ 1487 if (m->m_pkthdr.len < sizeof(*clt_hdr)) { 1488 NG_BTSOCKET_L2CAP_ERR( 1489 "%s: Can not get L2CAP connectionless packet header, " \ 1490 "src bdaddr=%x:%x:%x:%x:%x:%x, length=%d\n", 1491 __func__, 1492 rt->src.b[5], rt->src.b[4], rt->src.b[3], 1493 rt->src.b[2], rt->src.b[1], rt->src.b[0], 1494 hdr->length); 1495 goto drop; 1496 } 1497 1498 if (m->m_len < sizeof(*clt_hdr)) { 1499 m = m_pullup(m, sizeof(*clt_hdr)); 1500 if (m == NULL) 1501 goto drop; 1502 } 1503 1504 /* Strip connectionless header and deliver packet */ 1505 clt_hdr = mtod(m, ng_l2cap_clt_hdr_t *); 1506 m_adj(m, sizeof(*clt_hdr)); 1507 1508 NG_BTSOCKET_L2CAP_INFO( 1509 "%s: Got L2CAP connectionless data packet, " \ 1510 "src bdaddr=%x:%x:%x:%x:%x:%x, psm=%d, length=%d\n", 1511 __func__, 1512 rt->src.b[5], rt->src.b[4], rt->src.b[3], 1513 rt->src.b[2], rt->src.b[1], rt->src.b[0], 1514 clt_hdr->psm, hdr->length); 1515 1516 mtx_lock(&ng_btsocket_l2cap_sockets_mtx); 1517 1518 LIST_FOREACH(pcb, &ng_btsocket_l2cap_sockets, next) { 1519 struct mbuf *copy = NULL; 1520 1521 mtx_lock(&pcb->pcb_mtx); 1522 1523 if (bcmp(&rt->src, &pcb->src, sizeof(pcb->src)) != 0 || 1524 pcb->psm != clt_hdr->psm || 1525 pcb->state != NG_BTSOCKET_L2CAP_OPEN || 1526 (pcb->so->so_options & SO_BROADCAST) == 0 || 1527 m->m_pkthdr.len > sbspace(&pcb->so->so_rcv)) 1528 goto next; 1529 1530 /* 1531 * Create a copy of the packet and append it to the 1532 * socket's queue. If m_dup() failed - no big deal 1533 * it is a broadcast traffic after all 1534 */ 1535 1536 copy = m_dup(m, M_NOWAIT); 1537 if (copy != NULL) { 1538 sbappendrecord(&pcb->so->so_rcv, copy); 1539 sorwakeup(pcb->so); 1540 } 1541 next: 1542 mtx_unlock(&pcb->pcb_mtx); 1543 } 1544 1545 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx); 1546 } 1547 drop: 1548 NG_FREE_M(m); /* checks for m != NULL */ 1549 } /* ng_btsocket_l2cap_data_input */ 1550 1551 /* 1552 * L2CAP sockets default message input routine 1553 */ 1554 1555 static void 1556 ng_btsocket_l2cap_default_msg_input(struct ng_mesg *msg, hook_p hook) 1557 { 1558 switch (msg->header.cmd) { 1559 case NGM_L2CAP_NODE_HOOK_INFO: { 1560 ng_btsocket_l2cap_rtentry_t *rt = NULL; 1561 1562 if (hook == NULL || msg->header.arglen != sizeof(bdaddr_t)) 1563 break; 1564 1565 if (bcmp(msg->data, NG_HCI_BDADDR_ANY, sizeof(bdaddr_t)) == 0) 1566 break; 1567 1568 mtx_lock(&ng_btsocket_l2cap_rt_mtx); 1569 1570 rt = (ng_btsocket_l2cap_rtentry_t *) NG_HOOK_PRIVATE(hook); 1571 if (rt == NULL) { 1572 rt = malloc(sizeof(*rt), 1573 M_NETGRAPH_BTSOCKET_L2CAP, M_NOWAIT|M_ZERO); 1574 if (rt == NULL) { 1575 mtx_unlock(&ng_btsocket_l2cap_rt_mtx); 1576 break; 1577 } 1578 1579 LIST_INSERT_HEAD(&ng_btsocket_l2cap_rt, rt, next); 1580 1581 NG_HOOK_SET_PRIVATE(hook, rt); 1582 } 1583 1584 bcopy(msg->data, &rt->src, sizeof(rt->src)); 1585 rt->hook = hook; 1586 1587 mtx_unlock(&ng_btsocket_l2cap_rt_mtx); 1588 1589 NG_BTSOCKET_L2CAP_INFO( 1590 "%s: Updating hook \"%s\", src bdaddr=%x:%x:%x:%x:%x:%x\n", 1591 __func__, NG_HOOK_NAME(hook), 1592 rt->src.b[5], rt->src.b[4], rt->src.b[3], 1593 rt->src.b[2], rt->src.b[1], rt->src.b[0]); 1594 } break; 1595 1596 default: 1597 NG_BTSOCKET_L2CAP_WARN( 1598 "%s: Unknown message, cmd=%d\n", __func__, msg->header.cmd); 1599 break; 1600 } 1601 1602 NG_FREE_MSG(msg); /* Checks for msg != NULL */ 1603 } /* ng_btsocket_l2cap_default_msg_input */ 1604 1605 /* 1606 * L2CAP sockets L2CA message input routine 1607 */ 1608 1609 static void 1610 ng_btsocket_l2cap_l2ca_msg_input(struct ng_mesg *msg, hook_p hook) 1611 { 1612 ng_btsocket_l2cap_rtentry_p rt = NULL; 1613 1614 if (hook == NULL) { 1615 NG_BTSOCKET_L2CAP_ALERT( 1616 "%s: Invalid source hook for L2CA message\n", __func__); 1617 goto drop; 1618 } 1619 1620 rt = (ng_btsocket_l2cap_rtentry_p) NG_HOOK_PRIVATE(hook); 1621 if (rt == NULL) { 1622 NG_BTSOCKET_L2CAP_ALERT( 1623 "%s: Could not find out source bdaddr for L2CA message\n", __func__); 1624 goto drop; 1625 } 1626 1627 switch (msg->header.cmd) { 1628 case NGM_L2CAP_L2CA_CON: /* L2CA_Connect response */ 1629 ng_btsocket_l2cap_process_l2ca_con_req_rsp(msg, rt); 1630 break; 1631 1632 case NGM_L2CAP_L2CA_CON_RSP: /* L2CA_ConnectRsp response */ 1633 ng_btsocket_l2cap_process_l2ca_con_rsp_rsp(msg, rt); 1634 break; 1635 1636 case NGM_L2CAP_L2CA_CON_IND: /* L2CA_Connect indicator */ 1637 ng_btsocket_l2cap_process_l2ca_con_ind(msg, rt); 1638 break; 1639 1640 case NGM_L2CAP_L2CA_CFG: /* L2CA_Config response */ 1641 ng_btsocket_l2cap_process_l2ca_cfg_req_rsp(msg, rt); 1642 break; 1643 1644 case NGM_L2CAP_L2CA_CFG_RSP: /* L2CA_ConfigRsp response */ 1645 ng_btsocket_l2cap_process_l2ca_cfg_rsp_rsp(msg, rt); 1646 break; 1647 1648 case NGM_L2CAP_L2CA_CFG_IND: /* L2CA_Config indicator */ 1649 ng_btsocket_l2cap_process_l2ca_cfg_ind(msg, rt); 1650 break; 1651 1652 case NGM_L2CAP_L2CA_DISCON: /* L2CA_Disconnect response */ 1653 ng_btsocket_l2cap_process_l2ca_discon_rsp(msg, rt); 1654 break; 1655 1656 case NGM_L2CAP_L2CA_DISCON_IND: /* L2CA_Disconnect indicator */ 1657 ng_btsocket_l2cap_process_l2ca_discon_ind(msg, rt); 1658 break; 1659 1660 case NGM_L2CAP_L2CA_WRITE: /* L2CA_Write response */ 1661 ng_btsocket_l2cap_process_l2ca_write_rsp(msg, rt); 1662 break; 1663 1664 /* XXX FIXME add other L2CA messages */ 1665 1666 default: 1667 NG_BTSOCKET_L2CAP_WARN( 1668 "%s: Unknown L2CA message, cmd=%d\n", __func__, msg->header.cmd); 1669 break; 1670 } 1671 drop: 1672 NG_FREE_MSG(msg); 1673 } /* ng_btsocket_l2cap_l2ca_msg_input */ 1674 1675 /* 1676 * L2CAP sockets input routine 1677 */ 1678 1679 static void 1680 ng_btsocket_l2cap_input(void *context, int pending) 1681 { 1682 item_p item = NULL; 1683 hook_p hook = NULL; 1684 1685 for (;;) { 1686 mtx_lock(&ng_btsocket_l2cap_queue_mtx); 1687 NG_BT_ITEMQ_DEQUEUE(&ng_btsocket_l2cap_queue, item); 1688 mtx_unlock(&ng_btsocket_l2cap_queue_mtx); 1689 1690 if (item == NULL) 1691 break; 1692 1693 NGI_GET_HOOK(item, hook); 1694 if (hook != NULL && NG_HOOK_NOT_VALID(hook)) 1695 goto drop; 1696 1697 switch(item->el_flags & NGQF_TYPE) { 1698 case NGQF_DATA: { 1699 struct mbuf *m = NULL; 1700 1701 NGI_GET_M(item, m); 1702 ng_btsocket_l2cap_data_input(m, hook); 1703 } break; 1704 1705 case NGQF_MESG: { 1706 struct ng_mesg *msg = NULL; 1707 1708 NGI_GET_MSG(item, msg); 1709 1710 switch (msg->header.cmd) { 1711 case NGM_L2CAP_L2CA_CON: 1712 case NGM_L2CAP_L2CA_CON_RSP: 1713 case NGM_L2CAP_L2CA_CON_IND: 1714 case NGM_L2CAP_L2CA_CFG: 1715 case NGM_L2CAP_L2CA_CFG_RSP: 1716 case NGM_L2CAP_L2CA_CFG_IND: 1717 case NGM_L2CAP_L2CA_DISCON: 1718 case NGM_L2CAP_L2CA_DISCON_IND: 1719 case NGM_L2CAP_L2CA_WRITE: 1720 /* XXX FIXME add other L2CA messages */ 1721 ng_btsocket_l2cap_l2ca_msg_input(msg, hook); 1722 break; 1723 1724 default: 1725 ng_btsocket_l2cap_default_msg_input(msg, hook); 1726 break; 1727 } 1728 } break; 1729 1730 default: 1731 KASSERT(0, 1732 ("%s: invalid item type=%ld\n", __func__, (item->el_flags & NGQF_TYPE))); 1733 break; 1734 } 1735 drop: 1736 if (hook != NULL) 1737 NG_HOOK_UNREF(hook); 1738 1739 NG_FREE_ITEM(item); 1740 } 1741 } /* ng_btsocket_l2cap_input */ 1742 1743 /* 1744 * Route cleanup task. Gets scheduled when hook is disconnected. Here we 1745 * will find all sockets that use "invalid" hook and disconnect them. 1746 */ 1747 1748 static void 1749 ng_btsocket_l2cap_rtclean(void *context, int pending) 1750 { 1751 ng_btsocket_l2cap_pcb_p pcb = NULL, pcb_next = NULL; 1752 ng_btsocket_l2cap_rtentry_p rt = NULL; 1753 1754 mtx_lock(&ng_btsocket_l2cap_rt_mtx); 1755 mtx_lock(&ng_btsocket_l2cap_sockets_mtx); 1756 1757 /* 1758 * First disconnect all sockets that use "invalid" hook 1759 */ 1760 1761 for (pcb = LIST_FIRST(&ng_btsocket_l2cap_sockets); pcb != NULL; ) { 1762 mtx_lock(&pcb->pcb_mtx); 1763 pcb_next = LIST_NEXT(pcb, next); 1764 1765 if (pcb->rt != NULL && 1766 pcb->rt->hook != NULL && NG_HOOK_NOT_VALID(pcb->rt->hook)) { 1767 if (pcb->flags & NG_BTSOCKET_L2CAP_TIMO) 1768 ng_btsocket_l2cap_untimeout(pcb); 1769 1770 pcb->so->so_error = ENETDOWN; 1771 pcb->state = NG_BTSOCKET_L2CAP_CLOSED; 1772 soisdisconnected(pcb->so); 1773 1774 pcb->token = 0; 1775 pcb->cid = 0; 1776 pcb->rt = NULL; 1777 } 1778 1779 mtx_unlock(&pcb->pcb_mtx); 1780 pcb = pcb_next; 1781 } 1782 1783 /* 1784 * Now cleanup routing table 1785 */ 1786 1787 for (rt = LIST_FIRST(&ng_btsocket_l2cap_rt); rt != NULL; ) { 1788 ng_btsocket_l2cap_rtentry_p rt_next = LIST_NEXT(rt, next); 1789 1790 if (rt->hook != NULL && NG_HOOK_NOT_VALID(rt->hook)) { 1791 LIST_REMOVE(rt, next); 1792 1793 NG_HOOK_SET_PRIVATE(rt->hook, NULL); 1794 NG_HOOK_UNREF(rt->hook); /* Remove extra reference */ 1795 1796 bzero(rt, sizeof(*rt)); 1797 free(rt, M_NETGRAPH_BTSOCKET_L2CAP); 1798 } 1799 1800 rt = rt_next; 1801 } 1802 1803 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx); 1804 mtx_unlock(&ng_btsocket_l2cap_rt_mtx); 1805 } /* ng_btsocket_l2cap_rtclean */ 1806 1807 /* 1808 * Initialize everything 1809 */ 1810 1811 void 1812 ng_btsocket_l2cap_init(void) 1813 { 1814 int error = 0; 1815 1816 /* Skip initialization of globals for non-default instances. */ 1817 if (!IS_DEFAULT_VNET(curvnet)) 1818 return; 1819 1820 ng_btsocket_l2cap_node = NULL; 1821 ng_btsocket_l2cap_debug_level = NG_BTSOCKET_WARN_LEVEL; 1822 1823 /* Register Netgraph node type */ 1824 error = ng_newtype(&typestruct); 1825 if (error != 0) { 1826 NG_BTSOCKET_L2CAP_ALERT( 1827 "%s: Could not register Netgraph node type, error=%d\n", __func__, error); 1828 1829 return; 1830 } 1831 1832 /* Create Netgrapg node */ 1833 error = ng_make_node_common(&typestruct, &ng_btsocket_l2cap_node); 1834 if (error != 0) { 1835 NG_BTSOCKET_L2CAP_ALERT( 1836 "%s: Could not create Netgraph node, error=%d\n", __func__, error); 1837 1838 ng_btsocket_l2cap_node = NULL; 1839 1840 return; 1841 } 1842 1843 error = ng_name_node(ng_btsocket_l2cap_node, 1844 NG_BTSOCKET_L2CAP_NODE_TYPE); 1845 if (error != 0) { 1846 NG_BTSOCKET_L2CAP_ALERT( 1847 "%s: Could not name Netgraph node, error=%d\n", __func__, error); 1848 1849 NG_NODE_UNREF(ng_btsocket_l2cap_node); 1850 ng_btsocket_l2cap_node = NULL; 1851 1852 return; 1853 } 1854 1855 /* Create input queue */ 1856 NG_BT_ITEMQ_INIT(&ng_btsocket_l2cap_queue, ifqmaxlen); 1857 mtx_init(&ng_btsocket_l2cap_queue_mtx, 1858 "btsocks_l2cap_queue_mtx", NULL, MTX_DEF); 1859 TASK_INIT(&ng_btsocket_l2cap_queue_task, 0, 1860 ng_btsocket_l2cap_input, NULL); 1861 1862 /* Create list of sockets */ 1863 LIST_INIT(&ng_btsocket_l2cap_sockets); 1864 mtx_init(&ng_btsocket_l2cap_sockets_mtx, 1865 "btsocks_l2cap_sockets_mtx", NULL, MTX_DEF); 1866 1867 /* Routing table */ 1868 LIST_INIT(&ng_btsocket_l2cap_rt); 1869 mtx_init(&ng_btsocket_l2cap_rt_mtx, 1870 "btsocks_l2cap_rt_mtx", NULL, MTX_DEF); 1871 TASK_INIT(&ng_btsocket_l2cap_rt_task, 0, 1872 ng_btsocket_l2cap_rtclean, NULL); 1873 } /* ng_btsocket_l2cap_init */ 1874 1875 /* 1876 * Abort connection on socket 1877 */ 1878 1879 void 1880 ng_btsocket_l2cap_abort(struct socket *so) 1881 { 1882 so->so_error = ECONNABORTED; 1883 1884 (void)ng_btsocket_l2cap_disconnect(so); 1885 } /* ng_btsocket_l2cap_abort */ 1886 1887 void 1888 ng_btsocket_l2cap_close(struct socket *so) 1889 { 1890 1891 (void)ng_btsocket_l2cap_disconnect(so); 1892 } /* ng_btsocket_l2cap_close */ 1893 1894 /* 1895 * Accept connection on socket. Nothing to do here, socket must be connected 1896 * and ready, so just return peer address and be done with it. 1897 */ 1898 1899 int 1900 ng_btsocket_l2cap_accept(struct socket *so, struct sockaddr **nam) 1901 { 1902 if (ng_btsocket_l2cap_node == NULL) 1903 return (EINVAL); 1904 1905 return (ng_btsocket_l2cap_peeraddr(so, nam)); 1906 } /* ng_btsocket_l2cap_accept */ 1907 1908 /* 1909 * Create and attach new socket 1910 */ 1911 1912 int 1913 ng_btsocket_l2cap_attach(struct socket *so, int proto, struct thread *td) 1914 { 1915 static u_int32_t token = 0; 1916 ng_btsocket_l2cap_pcb_p pcb = so2l2cap_pcb(so); 1917 int error; 1918 1919 /* Check socket and protocol */ 1920 if (ng_btsocket_l2cap_node == NULL) 1921 return (EPROTONOSUPPORT); 1922 if (so->so_type != SOCK_SEQPACKET) 1923 return (ESOCKTNOSUPPORT); 1924 1925 #if 0 /* XXX sonewconn() calls "pru_attach" with proto == 0 */ 1926 if (proto != 0) 1927 if (proto != BLUETOOTH_PROTO_L2CAP) 1928 return (EPROTONOSUPPORT); 1929 #endif /* XXX */ 1930 1931 if (pcb != NULL) 1932 return (EISCONN); 1933 1934 /* Reserve send and receive space if it is not reserved yet */ 1935 if ((so->so_snd.sb_hiwat == 0) || (so->so_rcv.sb_hiwat == 0)) { 1936 error = soreserve(so, NG_BTSOCKET_L2CAP_SENDSPACE, 1937 NG_BTSOCKET_L2CAP_RECVSPACE); 1938 if (error != 0) 1939 return (error); 1940 } 1941 1942 /* Allocate the PCB */ 1943 pcb = malloc(sizeof(*pcb), 1944 M_NETGRAPH_BTSOCKET_L2CAP, M_NOWAIT | M_ZERO); 1945 if (pcb == NULL) 1946 return (ENOMEM); 1947 1948 /* Link the PCB and the socket */ 1949 so->so_pcb = (caddr_t) pcb; 1950 pcb->so = so; 1951 pcb->state = NG_BTSOCKET_L2CAP_CLOSED; 1952 1953 /* Initialize PCB */ 1954 pcb->imtu = pcb->omtu = NG_L2CAP_MTU_DEFAULT; 1955 1956 /* Default flow */ 1957 pcb->iflow.flags = 0x0; 1958 pcb->iflow.service_type = NG_HCI_SERVICE_TYPE_BEST_EFFORT; 1959 pcb->iflow.token_rate = 0xffffffff; /* maximum */ 1960 pcb->iflow.token_bucket_size = 0xffffffff; /* maximum */ 1961 pcb->iflow.peak_bandwidth = 0x00000000; /* maximum */ 1962 pcb->iflow.latency = 0xffffffff; /* don't care */ 1963 pcb->iflow.delay_variation = 0xffffffff; /* don't care */ 1964 1965 bcopy(&pcb->iflow, &pcb->oflow, sizeof(pcb->oflow)); 1966 1967 pcb->flush_timo = NG_L2CAP_FLUSH_TIMO_DEFAULT; 1968 pcb->link_timo = NG_L2CAP_LINK_TIMO_DEFAULT; 1969 1970 /* 1971 * XXX Mark PCB mutex as DUPOK to prevent "duplicated lock of 1972 * the same type" message. When accepting new L2CAP connection 1973 * ng_btsocket_l2cap_process_l2ca_con_ind() holds both PCB mutexes 1974 * for "old" (accepting) PCB and "new" (created) PCB. 1975 */ 1976 1977 mtx_init(&pcb->pcb_mtx, "btsocks_l2cap_pcb_mtx", NULL, 1978 MTX_DEF|MTX_DUPOK); 1979 callout_init_mtx(&pcb->timo, &pcb->pcb_mtx, 0); 1980 1981 /* 1982 * Add the PCB to the list 1983 * 1984 * XXX FIXME VERY IMPORTANT! 1985 * 1986 * This is totally FUBAR. We could get here in two cases: 1987 * 1988 * 1) When user calls socket() 1989 * 2) When we need to accept new incomming connection and call 1990 * sonewconn() 1991 * 1992 * In the first case we must acquire ng_btsocket_l2cap_sockets_mtx. 1993 * In the second case we hold ng_btsocket_l2cap_sockets_mtx already. 1994 * So we now need to distinguish between these cases. From reading 1995 * /sys/kern/uipc_socket.c we can find out that sonewconn() calls 1996 * pru_attach with proto == 0 and td == NULL. For now use this fact 1997 * to figure out if we were called from socket() or from sonewconn(). 1998 */ 1999 2000 if (td != NULL) 2001 mtx_lock(&ng_btsocket_l2cap_sockets_mtx); 2002 else 2003 mtx_assert(&ng_btsocket_l2cap_sockets_mtx, MA_OWNED); 2004 2005 /* Set PCB token. Use ng_btsocket_l2cap_sockets_mtx for protection */ 2006 if (++ token == 0) 2007 token ++; 2008 2009 pcb->token = token; 2010 2011 LIST_INSERT_HEAD(&ng_btsocket_l2cap_sockets, pcb, next); 2012 2013 if (td != NULL) 2014 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx); 2015 2016 return (0); 2017 } /* ng_btsocket_l2cap_attach */ 2018 2019 /* 2020 * Bind socket 2021 */ 2022 2023 int 2024 ng_btsocket_l2cap_bind(struct socket *so, struct sockaddr *nam, 2025 struct thread *td) 2026 { 2027 ng_btsocket_l2cap_pcb_t *pcb = NULL; 2028 struct sockaddr_l2cap *sa = (struct sockaddr_l2cap *) nam; 2029 int psm, error = 0; 2030 2031 if (ng_btsocket_l2cap_node == NULL) 2032 return (EINVAL); 2033 2034 /* Verify address */ 2035 if (sa == NULL) 2036 return (EINVAL); 2037 if (sa->l2cap_family != AF_BLUETOOTH) 2038 return (EAFNOSUPPORT); 2039 if (sa->l2cap_len != sizeof(*sa)) 2040 return (EINVAL); 2041 2042 psm = le16toh(sa->l2cap_psm); 2043 2044 /* 2045 * Check if other socket has this address already (look for exact 2046 * match PSM and bdaddr) and assign socket address if it's available. 2047 * 2048 * Note: socket can be bound to ANY PSM (zero) thus allowing several 2049 * channels with the same PSM between the same pair of BD_ADDR'es. 2050 */ 2051 2052 mtx_lock(&ng_btsocket_l2cap_sockets_mtx); 2053 2054 LIST_FOREACH(pcb, &ng_btsocket_l2cap_sockets, next) 2055 if (psm != 0 && psm == pcb->psm && 2056 bcmp(&pcb->src, &sa->l2cap_bdaddr, sizeof(bdaddr_t)) == 0) 2057 break; 2058 2059 if (pcb == NULL) { 2060 /* Set socket address */ 2061 pcb = so2l2cap_pcb(so); 2062 if (pcb != NULL) { 2063 bcopy(&sa->l2cap_bdaddr, &pcb->src, sizeof(pcb->src)); 2064 pcb->psm = psm; 2065 } else 2066 error = EINVAL; 2067 } else 2068 error = EADDRINUSE; 2069 2070 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx); 2071 2072 return (error); 2073 } /* ng_btsocket_l2cap_bind */ 2074 2075 /* 2076 * Connect socket 2077 */ 2078 2079 int 2080 ng_btsocket_l2cap_connect(struct socket *so, struct sockaddr *nam, 2081 struct thread *td) 2082 { 2083 ng_btsocket_l2cap_pcb_t *pcb = so2l2cap_pcb(so); 2084 struct sockaddr_l2cap *sa = (struct sockaddr_l2cap *) nam; 2085 ng_btsocket_l2cap_rtentry_t *rt = NULL; 2086 int have_src, error = 0; 2087 2088 /* Check socket */ 2089 if (pcb == NULL) 2090 return (EINVAL); 2091 if (ng_btsocket_l2cap_node == NULL) 2092 return (EINVAL); 2093 if (pcb->state == NG_BTSOCKET_L2CAP_CONNECTING) 2094 return (EINPROGRESS); 2095 2096 /* Verify address */ 2097 if (sa == NULL) 2098 return (EINVAL); 2099 if (sa->l2cap_family != AF_BLUETOOTH) 2100 return (EAFNOSUPPORT); 2101 if (sa->l2cap_len != sizeof(*sa)) 2102 return (EINVAL); 2103 if (sa->l2cap_psm == 0 || 2104 bcmp(&sa->l2cap_bdaddr, NG_HCI_BDADDR_ANY, sizeof(bdaddr_t)) == 0) 2105 return (EDESTADDRREQ); 2106 if (pcb->psm != 0 && pcb->psm != le16toh(sa->l2cap_psm)) 2107 return (EINVAL); 2108 2109 /* 2110 * Routing. Socket should be bound to some source address. The source 2111 * address can be ANY. Destination address must be set and it must not 2112 * be ANY. If source address is ANY then find first rtentry that has 2113 * src != dst. 2114 */ 2115 2116 mtx_lock(&ng_btsocket_l2cap_rt_mtx); 2117 mtx_lock(&ng_btsocket_l2cap_sockets_mtx); 2118 mtx_lock(&pcb->pcb_mtx); 2119 2120 /* Send destination address and PSM */ 2121 bcopy(&sa->l2cap_bdaddr, &pcb->dst, sizeof(pcb->dst)); 2122 pcb->psm = le16toh(sa->l2cap_psm); 2123 2124 pcb->rt = NULL; 2125 have_src = bcmp(&pcb->src, NG_HCI_BDADDR_ANY, sizeof(pcb->src)); 2126 2127 LIST_FOREACH(rt, &ng_btsocket_l2cap_rt, next) { 2128 if (rt->hook == NULL || NG_HOOK_NOT_VALID(rt->hook)) 2129 continue; 2130 2131 /* Match src and dst */ 2132 if (have_src) { 2133 if (bcmp(&pcb->src, &rt->src, sizeof(rt->src)) == 0) 2134 break; 2135 } else { 2136 if (bcmp(&pcb->dst, &rt->src, sizeof(rt->src)) != 0) 2137 break; 2138 } 2139 } 2140 2141 if (rt != NULL) { 2142 pcb->rt = rt; 2143 2144 if (!have_src) 2145 bcopy(&rt->src, &pcb->src, sizeof(pcb->src)); 2146 } else 2147 error = EHOSTUNREACH; 2148 2149 /* 2150 * Send L2CA_Connect request 2151 */ 2152 2153 if (error == 0) { 2154 error = ng_btsocket_l2cap_send_l2ca_con_req(pcb); 2155 if (error == 0) { 2156 pcb->flags |= NG_BTSOCKET_L2CAP_CLIENT; 2157 pcb->state = NG_BTSOCKET_L2CAP_CONNECTING; 2158 soisconnecting(pcb->so); 2159 2160 ng_btsocket_l2cap_timeout(pcb); 2161 } 2162 } 2163 2164 mtx_unlock(&pcb->pcb_mtx); 2165 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx); 2166 mtx_unlock(&ng_btsocket_l2cap_rt_mtx); 2167 2168 return (error); 2169 } /* ng_btsocket_l2cap_connect */ 2170 2171 /* 2172 * Process ioctl's calls on socket 2173 */ 2174 2175 int 2176 ng_btsocket_l2cap_control(struct socket *so, u_long cmd, caddr_t data, 2177 struct ifnet *ifp, struct thread *td) 2178 { 2179 return (EINVAL); 2180 } /* ng_btsocket_l2cap_control */ 2181 2182 /* 2183 * Process getsockopt/setsockopt system calls 2184 */ 2185 2186 int 2187 ng_btsocket_l2cap_ctloutput(struct socket *so, struct sockopt *sopt) 2188 { 2189 ng_btsocket_l2cap_pcb_p pcb = so2l2cap_pcb(so); 2190 int error = 0; 2191 ng_l2cap_cfg_opt_val_t v; 2192 2193 if (pcb == NULL) 2194 return (EINVAL); 2195 if (ng_btsocket_l2cap_node == NULL) 2196 return (EINVAL); 2197 2198 if (sopt->sopt_level != SOL_L2CAP) 2199 return (0); 2200 2201 mtx_lock(&pcb->pcb_mtx); 2202 2203 switch (sopt->sopt_dir) { 2204 case SOPT_GET: 2205 switch (sopt->sopt_name) { 2206 case SO_L2CAP_IMTU: /* get incoming MTU */ 2207 error = sooptcopyout(sopt, &pcb->imtu, 2208 sizeof(pcb->imtu)); 2209 break; 2210 2211 case SO_L2CAP_OMTU: /* get outgoing (peer incoming) MTU */ 2212 error = sooptcopyout(sopt, &pcb->omtu, 2213 sizeof(pcb->omtu)); 2214 break; 2215 2216 case SO_L2CAP_IFLOW: /* get incoming flow spec. */ 2217 error = sooptcopyout(sopt, &pcb->iflow, 2218 sizeof(pcb->iflow)); 2219 break; 2220 2221 case SO_L2CAP_OFLOW: /* get outgoing flow spec. */ 2222 error = sooptcopyout(sopt, &pcb->oflow, 2223 sizeof(pcb->oflow)); 2224 break; 2225 2226 case SO_L2CAP_FLUSH: /* get flush timeout */ 2227 error = sooptcopyout(sopt, &pcb->flush_timo, 2228 sizeof(pcb->flush_timo)); 2229 break; 2230 2231 default: 2232 error = ENOPROTOOPT; 2233 break; 2234 } 2235 break; 2236 2237 case SOPT_SET: 2238 /* 2239 * XXX 2240 * We do not allow to change these parameters while socket is 2241 * connected or we are in the process of creating a connection. 2242 * May be this should indicate re-configuration of the open 2243 * channel? 2244 */ 2245 2246 if (pcb->state != NG_BTSOCKET_L2CAP_CLOSED) { 2247 error = EACCES; 2248 break; 2249 } 2250 2251 switch (sopt->sopt_name) { 2252 case SO_L2CAP_IMTU: /* set incoming MTU */ 2253 error = sooptcopyin(sopt, &v, sizeof(v), sizeof(v.mtu)); 2254 if (error == 0) 2255 pcb->imtu = v.mtu; 2256 break; 2257 2258 case SO_L2CAP_OFLOW: /* set outgoing flow spec. */ 2259 error = sooptcopyin(sopt, &v, sizeof(v),sizeof(v.flow)); 2260 if (error == 0) 2261 bcopy(&v.flow, &pcb->oflow, sizeof(pcb->oflow)); 2262 break; 2263 2264 case SO_L2CAP_FLUSH: /* set flush timeout */ 2265 error = sooptcopyin(sopt, &v, sizeof(v), 2266 sizeof(v.flush_timo)); 2267 if (error == 0) 2268 pcb->flush_timo = v.flush_timo; 2269 break; 2270 2271 default: 2272 error = ENOPROTOOPT; 2273 break; 2274 } 2275 break; 2276 2277 default: 2278 error = EINVAL; 2279 break; 2280 } 2281 2282 mtx_unlock(&pcb->pcb_mtx); 2283 2284 return (error); 2285 } /* ng_btsocket_l2cap_ctloutput */ 2286 2287 /* 2288 * Detach and destroy socket 2289 */ 2290 2291 void 2292 ng_btsocket_l2cap_detach(struct socket *so) 2293 { 2294 ng_btsocket_l2cap_pcb_p pcb = so2l2cap_pcb(so); 2295 2296 KASSERT(pcb != NULL, ("ng_btsocket_l2cap_detach: pcb == NULL")); 2297 2298 if (ng_btsocket_l2cap_node == NULL) 2299 return; 2300 2301 mtx_lock(&ng_btsocket_l2cap_sockets_mtx); 2302 mtx_lock(&pcb->pcb_mtx); 2303 2304 /* XXX what to do with pending request? */ 2305 if (pcb->flags & NG_BTSOCKET_L2CAP_TIMO) 2306 ng_btsocket_l2cap_untimeout(pcb); 2307 2308 if (pcb->state != NG_BTSOCKET_L2CAP_CLOSED && 2309 pcb->state != NG_BTSOCKET_L2CAP_DISCONNECTING) 2310 /* Send disconnect request with "zero" token */ 2311 ng_btsocket_l2cap_send_l2ca_discon_req(0, pcb); 2312 2313 pcb->state = NG_BTSOCKET_L2CAP_CLOSED; 2314 2315 LIST_REMOVE(pcb, next); 2316 2317 mtx_unlock(&pcb->pcb_mtx); 2318 mtx_unlock(&ng_btsocket_l2cap_sockets_mtx); 2319 2320 mtx_destroy(&pcb->pcb_mtx); 2321 bzero(pcb, sizeof(*pcb)); 2322 free(pcb, M_NETGRAPH_BTSOCKET_L2CAP); 2323 2324 soisdisconnected(so); 2325 so->so_pcb = NULL; 2326 } /* ng_btsocket_l2cap_detach */ 2327 2328 /* 2329 * Disconnect socket 2330 */ 2331 2332 int 2333 ng_btsocket_l2cap_disconnect(struct socket *so) 2334 { 2335 ng_btsocket_l2cap_pcb_p pcb = so2l2cap_pcb(so); 2336 int error = 0; 2337 2338 if (pcb == NULL) 2339 return (EINVAL); 2340 if (ng_btsocket_l2cap_node == NULL) 2341 return (EINVAL); 2342 2343 mtx_lock(&pcb->pcb_mtx); 2344 2345 if (pcb->state == NG_BTSOCKET_L2CAP_DISCONNECTING) { 2346 mtx_unlock(&pcb->pcb_mtx); 2347 return (EINPROGRESS); 2348 } 2349 2350 if (pcb->state != NG_BTSOCKET_L2CAP_CLOSED) { 2351 /* XXX FIXME what to do with pending request? */ 2352 if (pcb->flags & NG_BTSOCKET_L2CAP_TIMO) 2353 ng_btsocket_l2cap_untimeout(pcb); 2354 2355 error = ng_btsocket_l2cap_send_l2ca_discon_req(pcb->token, pcb); 2356 if (error == 0) { 2357 pcb->state = NG_BTSOCKET_L2CAP_DISCONNECTING; 2358 soisdisconnecting(so); 2359 2360 ng_btsocket_l2cap_timeout(pcb); 2361 } 2362 2363 /* XXX FIXME what to do if error != 0 */ 2364 } 2365 2366 mtx_unlock(&pcb->pcb_mtx); 2367 2368 return (error); 2369 } /* ng_btsocket_l2cap_disconnect */ 2370 2371 /* 2372 * Listen on socket 2373 */ 2374 2375 int 2376 ng_btsocket_l2cap_listen(struct socket *so, int backlog, struct thread *td) 2377 { 2378 ng_btsocket_l2cap_pcb_p pcb = so2l2cap_pcb(so); 2379 int error; 2380 2381 SOCK_LOCK(so); 2382 error = solisten_proto_check(so); 2383 if (error != 0) 2384 goto out; 2385 if (pcb == NULL) { 2386 error = EINVAL; 2387 goto out; 2388 } 2389 if (ng_btsocket_l2cap_node == NULL) { 2390 error = EINVAL; 2391 goto out; 2392 } 2393 if (pcb->psm == 0) { 2394 error = EADDRNOTAVAIL; 2395 goto out; 2396 } 2397 solisten_proto(so, backlog); 2398 out: 2399 SOCK_UNLOCK(so); 2400 return (error); 2401 } /* ng_btsocket_listen */ 2402 2403 /* 2404 * Get peer address 2405 */ 2406 2407 int 2408 ng_btsocket_l2cap_peeraddr(struct socket *so, struct sockaddr **nam) 2409 { 2410 ng_btsocket_l2cap_pcb_p pcb = so2l2cap_pcb(so); 2411 struct sockaddr_l2cap sa; 2412 2413 if (pcb == NULL) 2414 return (EINVAL); 2415 if (ng_btsocket_l2cap_node == NULL) 2416 return (EINVAL); 2417 2418 bcopy(&pcb->dst, &sa.l2cap_bdaddr, sizeof(sa.l2cap_bdaddr)); 2419 sa.l2cap_psm = htole16(pcb->psm); 2420 sa.l2cap_len = sizeof(sa); 2421 sa.l2cap_family = AF_BLUETOOTH; 2422 2423 *nam = sodupsockaddr((struct sockaddr *) &sa, M_NOWAIT); 2424 2425 return ((*nam == NULL)? ENOMEM : 0); 2426 } /* ng_btsocket_l2cap_peeraddr */ 2427 2428 /* 2429 * Send data to socket 2430 */ 2431 2432 int 2433 ng_btsocket_l2cap_send(struct socket *so, int flags, struct mbuf *m, 2434 struct sockaddr *nam, struct mbuf *control, struct thread *td) 2435 { 2436 ng_btsocket_l2cap_pcb_t *pcb = so2l2cap_pcb(so); 2437 int error = 0; 2438 2439 if (ng_btsocket_l2cap_node == NULL) { 2440 error = ENETDOWN; 2441 goto drop; 2442 } 2443 2444 /* Check socket and input */ 2445 if (pcb == NULL || m == NULL || control != NULL) { 2446 error = EINVAL; 2447 goto drop; 2448 } 2449 2450 mtx_lock(&pcb->pcb_mtx); 2451 2452 /* Make sure socket is connected */ 2453 if (pcb->state != NG_BTSOCKET_L2CAP_OPEN) { 2454 mtx_unlock(&pcb->pcb_mtx); 2455 error = ENOTCONN; 2456 goto drop; 2457 } 2458 2459 /* Check route */ 2460 if (pcb->rt == NULL || 2461 pcb->rt->hook == NULL || NG_HOOK_NOT_VALID(pcb->rt->hook)) { 2462 mtx_unlock(&pcb->pcb_mtx); 2463 error = ENETDOWN; 2464 goto drop; 2465 } 2466 2467 /* Check packet size agains outgoing (peer's incoming) MTU) */ 2468 if (m->m_pkthdr.len > pcb->omtu) { 2469 NG_BTSOCKET_L2CAP_ERR( 2470 "%s: Packet too big, len=%d, omtu=%d\n", __func__, m->m_pkthdr.len, pcb->omtu); 2471 2472 mtx_unlock(&pcb->pcb_mtx); 2473 error = EMSGSIZE; 2474 goto drop; 2475 } 2476 2477 /* 2478 * First put packet on socket send queue. Then check if we have 2479 * pending timeout. If we do not have timeout then we must send 2480 * packet and schedule timeout. Otherwise do nothing and wait for 2481 * L2CA_WRITE_RSP. 2482 */ 2483 2484 sbappendrecord(&pcb->so->so_snd, m); 2485 m = NULL; 2486 2487 if (!(pcb->flags & NG_BTSOCKET_L2CAP_TIMO)) { 2488 error = ng_btsocket_l2cap_send2(pcb); 2489 if (error == 0) 2490 ng_btsocket_l2cap_timeout(pcb); 2491 else 2492 sbdroprecord(&pcb->so->so_snd); /* XXX */ 2493 } 2494 2495 mtx_unlock(&pcb->pcb_mtx); 2496 drop: 2497 NG_FREE_M(m); /* checks for != NULL */ 2498 NG_FREE_M(control); 2499 2500 return (error); 2501 } /* ng_btsocket_l2cap_send */ 2502 2503 /* 2504 * Send first packet in the socket queue to the L2CAP layer 2505 */ 2506 2507 static int 2508 ng_btsocket_l2cap_send2(ng_btsocket_l2cap_pcb_p pcb) 2509 { 2510 struct mbuf *m = NULL; 2511 ng_l2cap_l2ca_hdr_t *hdr = NULL; 2512 int error = 0; 2513 2514 mtx_assert(&pcb->pcb_mtx, MA_OWNED); 2515 2516 if (pcb->so->so_snd.sb_cc == 0) 2517 return (EINVAL); /* XXX */ 2518 2519 m = m_dup(pcb->so->so_snd.sb_mb, M_NOWAIT); 2520 if (m == NULL) 2521 return (ENOBUFS); 2522 2523 /* Create L2CA packet header */ 2524 M_PREPEND(m, sizeof(*hdr), M_NOWAIT); 2525 if (m != NULL) 2526 if (m->m_len < sizeof(*hdr)) 2527 m = m_pullup(m, sizeof(*hdr)); 2528 2529 if (m == NULL) { 2530 NG_BTSOCKET_L2CAP_ERR( 2531 "%s: Failed to create L2CA packet header\n", __func__); 2532 2533 return (ENOBUFS); 2534 } 2535 2536 hdr = mtod(m, ng_l2cap_l2ca_hdr_t *); 2537 hdr->token = pcb->token; 2538 hdr->length = m->m_pkthdr.len - sizeof(*hdr); 2539 hdr->lcid = pcb->cid; 2540 2541 NG_BTSOCKET_L2CAP_INFO( 2542 "%s: Sending packet: len=%d, length=%d, lcid=%d, token=%d, state=%d\n", 2543 __func__, m->m_pkthdr.len, hdr->length, hdr->lcid, 2544 hdr->token, pcb->state); 2545 2546 /* 2547 * If we got here than we have successfuly creates new L2CAP 2548 * data packet and now we can send it to the L2CAP layer 2549 */ 2550 2551 NG_SEND_DATA_ONLY(error, pcb->rt->hook, m); 2552 2553 return (error); 2554 } /* ng_btsocket_l2cap_send2 */ 2555 2556 /* 2557 * Get socket address 2558 */ 2559 2560 int 2561 ng_btsocket_l2cap_sockaddr(struct socket *so, struct sockaddr **nam) 2562 { 2563 ng_btsocket_l2cap_pcb_p pcb = so2l2cap_pcb(so); 2564 struct sockaddr_l2cap sa; 2565 2566 if (pcb == NULL) 2567 return (EINVAL); 2568 if (ng_btsocket_l2cap_node == NULL) 2569 return (EINVAL); 2570 2571 bcopy(&pcb->src, &sa.l2cap_bdaddr, sizeof(sa.l2cap_bdaddr)); 2572 sa.l2cap_psm = htole16(pcb->psm); 2573 sa.l2cap_len = sizeof(sa); 2574 sa.l2cap_family = AF_BLUETOOTH; 2575 2576 *nam = sodupsockaddr((struct sockaddr *) &sa, M_NOWAIT); 2577 2578 return ((*nam == NULL)? ENOMEM : 0); 2579 } /* ng_btsocket_l2cap_sockaddr */ 2580 2581 /***************************************************************************** 2582 ***************************************************************************** 2583 ** Misc. functions 2584 ***************************************************************************** 2585 *****************************************************************************/ 2586 2587 /* 2588 * Look for the socket that listens on given PSM and bdaddr. Returns exact or 2589 * close match (if any). Caller must hold ng_btsocket_l2cap_sockets_mtx. 2590 */ 2591 2592 static ng_btsocket_l2cap_pcb_p 2593 ng_btsocket_l2cap_pcb_by_addr(bdaddr_p bdaddr, int psm) 2594 { 2595 ng_btsocket_l2cap_pcb_p p = NULL, p1 = NULL; 2596 2597 mtx_assert(&ng_btsocket_l2cap_sockets_mtx, MA_OWNED); 2598 2599 LIST_FOREACH(p, &ng_btsocket_l2cap_sockets, next) { 2600 if (p->so == NULL || !(p->so->so_options & SO_ACCEPTCONN) || 2601 p->psm != psm) 2602 continue; 2603 2604 if (bcmp(&p->src, bdaddr, sizeof(p->src)) == 0) 2605 break; 2606 2607 if (bcmp(&p->src, NG_HCI_BDADDR_ANY, sizeof(p->src)) == 0) 2608 p1 = p; 2609 } 2610 2611 return ((p != NULL)? p : p1); 2612 } /* ng_btsocket_l2cap_pcb_by_addr */ 2613 2614 /* 2615 * Look for the socket that has given token. 2616 * Caller must hold ng_btsocket_l2cap_sockets_mtx. 2617 */ 2618 2619 static ng_btsocket_l2cap_pcb_p 2620 ng_btsocket_l2cap_pcb_by_token(u_int32_t token) 2621 { 2622 ng_btsocket_l2cap_pcb_p p = NULL; 2623 2624 if (token == 0) 2625 return (NULL); 2626 2627 mtx_assert(&ng_btsocket_l2cap_sockets_mtx, MA_OWNED); 2628 2629 LIST_FOREACH(p, &ng_btsocket_l2cap_sockets, next) 2630 if (p->token == token) 2631 break; 2632 2633 return (p); 2634 } /* ng_btsocket_l2cap_pcb_by_token */ 2635 2636 /* 2637 * Look for the socket that assigned to given source address and channel ID. 2638 * Caller must hold ng_btsocket_l2cap_sockets_mtx 2639 */ 2640 2641 static ng_btsocket_l2cap_pcb_p 2642 ng_btsocket_l2cap_pcb_by_cid(bdaddr_p src, int cid) 2643 { 2644 ng_btsocket_l2cap_pcb_p p = NULL; 2645 2646 mtx_assert(&ng_btsocket_l2cap_sockets_mtx, MA_OWNED); 2647 2648 LIST_FOREACH(p, &ng_btsocket_l2cap_sockets, next) 2649 if (p->cid == cid && bcmp(src, &p->src, sizeof(p->src)) == 0) 2650 break; 2651 2652 return (p); 2653 } /* ng_btsocket_l2cap_pcb_by_cid */ 2654 2655 /* 2656 * Set timeout on socket 2657 */ 2658 2659 static void 2660 ng_btsocket_l2cap_timeout(ng_btsocket_l2cap_pcb_p pcb) 2661 { 2662 mtx_assert(&pcb->pcb_mtx, MA_OWNED); 2663 2664 if (!(pcb->flags & NG_BTSOCKET_L2CAP_TIMO)) { 2665 pcb->flags |= NG_BTSOCKET_L2CAP_TIMO; 2666 callout_reset(&pcb->timo, bluetooth_l2cap_ertx_timeout(), 2667 ng_btsocket_l2cap_process_timeout, pcb); 2668 } else 2669 KASSERT(0, 2670 ("%s: Duplicated socket timeout?!\n", __func__)); 2671 } /* ng_btsocket_l2cap_timeout */ 2672 2673 /* 2674 * Unset timeout on socket 2675 */ 2676 2677 static void 2678 ng_btsocket_l2cap_untimeout(ng_btsocket_l2cap_pcb_p pcb) 2679 { 2680 mtx_assert(&pcb->pcb_mtx, MA_OWNED); 2681 2682 if (pcb->flags & NG_BTSOCKET_L2CAP_TIMO) { 2683 callout_stop(&pcb->timo); 2684 pcb->flags &= ~NG_BTSOCKET_L2CAP_TIMO; 2685 } else 2686 KASSERT(0, 2687 ("%s: No socket timeout?!\n", __func__)); 2688 } /* ng_btsocket_l2cap_untimeout */ 2689 2690 /* 2691 * Process timeout on socket 2692 */ 2693 2694 static void 2695 ng_btsocket_l2cap_process_timeout(void *xpcb) 2696 { 2697 ng_btsocket_l2cap_pcb_p pcb = (ng_btsocket_l2cap_pcb_p) xpcb; 2698 2699 mtx_assert(&pcb->pcb_mtx, MA_OWNED); 2700 2701 pcb->flags &= ~NG_BTSOCKET_L2CAP_TIMO; 2702 pcb->so->so_error = ETIMEDOUT; 2703 2704 switch (pcb->state) { 2705 case NG_BTSOCKET_L2CAP_CONNECTING: 2706 case NG_BTSOCKET_L2CAP_CONFIGURING: 2707 /* Send disconnect request with "zero" token */ 2708 if (pcb->cid != 0) 2709 ng_btsocket_l2cap_send_l2ca_discon_req(0, pcb); 2710 2711 /* ... and close the socket */ 2712 pcb->state = NG_BTSOCKET_L2CAP_CLOSED; 2713 soisdisconnected(pcb->so); 2714 break; 2715 2716 case NG_BTSOCKET_L2CAP_OPEN: 2717 /* Send timeout - drop packet and wakeup sender */ 2718 sbdroprecord(&pcb->so->so_snd); 2719 sowwakeup(pcb->so); 2720 break; 2721 2722 case NG_BTSOCKET_L2CAP_DISCONNECTING: 2723 /* Disconnect timeout - disconnect the socket anyway */ 2724 pcb->state = NG_BTSOCKET_L2CAP_CLOSED; 2725 soisdisconnected(pcb->so); 2726 break; 2727 2728 default: 2729 NG_BTSOCKET_L2CAP_ERR( 2730 "%s: Invalid socket state=%d\n", __func__, pcb->state); 2731 break; 2732 } 2733 } /* ng_btsocket_l2cap_process_timeout */ 2734 2735 /* 2736 * Translate HCI/L2CAP error code into "errno" code 2737 * XXX Note: Some L2CAP and HCI error codes have the same value, but 2738 * different meaning 2739 */ 2740 2741 static int 2742 ng_btsocket_l2cap_result2errno(int result) 2743 { 2744 switch (result) { 2745 case 0x00: /* No error */ 2746 return (0); 2747 2748 case 0x01: /* Unknown HCI command */ 2749 return (ENODEV); 2750 2751 case 0x02: /* No connection */ 2752 return (ENOTCONN); 2753 2754 case 0x03: /* Hardware failure */ 2755 return (EIO); 2756 2757 case 0x04: /* Page timeout */ 2758 return (EHOSTDOWN); 2759 2760 case 0x05: /* Authentication failure */ 2761 case 0x06: /* Key missing */ 2762 case 0x18: /* Pairing not allowed */ 2763 case 0x21: /* Role change not allowed */ 2764 case 0x24: /* LMP PSU not allowed */ 2765 case 0x25: /* Encryption mode not acceptable */ 2766 case 0x26: /* Unit key used */ 2767 return (EACCES); 2768 2769 case 0x07: /* Memory full */ 2770 return (ENOMEM); 2771 2772 case 0x08: /* Connection timeout */ 2773 case 0x10: /* Host timeout */ 2774 case 0x22: /* LMP response timeout */ 2775 case 0xee: /* HCI timeout */ 2776 case 0xeeee: /* L2CAP timeout */ 2777 return (ETIMEDOUT); 2778 2779 case 0x09: /* Max number of connections */ 2780 case 0x0a: /* Max number of SCO connections to a unit */ 2781 return (EMLINK); 2782 2783 case 0x0b: /* ACL connection already exists */ 2784 return (EEXIST); 2785 2786 case 0x0c: /* Command disallowed */ 2787 return (EBUSY); 2788 2789 case 0x0d: /* Host rejected due to limited resources */ 2790 case 0x0e: /* Host rejected due to securiity reasons */ 2791 case 0x0f: /* Host rejected due to remote unit is a personal unit */ 2792 case 0x1b: /* SCO offset rejected */ 2793 case 0x1c: /* SCO interval rejected */ 2794 case 0x1d: /* SCO air mode rejected */ 2795 return (ECONNREFUSED); 2796 2797 case 0x11: /* Unsupported feature or parameter value */ 2798 case 0x19: /* Unknown LMP PDU */ 2799 case 0x1a: /* Unsupported remote feature */ 2800 case 0x20: /* Unsupported LMP parameter value */ 2801 case 0x27: /* QoS is not supported */ 2802 case 0x29: /* Paring with unit key not supported */ 2803 return (EOPNOTSUPP); 2804 2805 case 0x12: /* Invalid HCI command parameter */ 2806 case 0x1e: /* Invalid LMP parameters */ 2807 return (EINVAL); 2808 2809 case 0x13: /* Other end terminated connection: User ended connection */ 2810 case 0x14: /* Other end terminated connection: Low resources */ 2811 case 0x15: /* Other end terminated connection: About to power off */ 2812 return (ECONNRESET); 2813 2814 case 0x16: /* Connection terminated by local host */ 2815 return (ECONNABORTED); 2816 2817 #if 0 /* XXX not yet */ 2818 case 0x17: /* Repeated attempts */ 2819 case 0x1f: /* Unspecified error */ 2820 case 0x23: /* LMP error transaction collision */ 2821 case 0x28: /* Instant passed */ 2822 #endif 2823 } 2824 2825 return (ENOSYS); 2826 } /* ng_btsocket_l2cap_result2errno */ 2827 2828