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