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