1 /* 2 * ng_l2cap_misc.c 3 */ 4 5 /*- 6 * Copyright (c) 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_l2cap_misc.c,v 1.5 2003/09/08 19:11:45 max Exp $ 31 * $FreeBSD$ 32 */ 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/kernel.h> 37 #include <sys/malloc.h> 38 #include <sys/mbuf.h> 39 #include <sys/queue.h> 40 #include <netgraph/ng_message.h> 41 #include <netgraph/netgraph.h> 42 #include <netgraph/bluetooth/include/ng_bluetooth.h> 43 #include <netgraph/bluetooth/include/ng_hci.h> 44 #include <netgraph/bluetooth/include/ng_l2cap.h> 45 #include <netgraph/bluetooth/l2cap/ng_l2cap_var.h> 46 #include <netgraph/bluetooth/l2cap/ng_l2cap_cmds.h> 47 #include <netgraph/bluetooth/l2cap/ng_l2cap_evnt.h> 48 #include <netgraph/bluetooth/l2cap/ng_l2cap_llpi.h> 49 #include <netgraph/bluetooth/l2cap/ng_l2cap_ulpi.h> 50 #include <netgraph/bluetooth/l2cap/ng_l2cap_misc.h> 51 52 static u_int16_t ng_l2cap_get_cid (ng_l2cap_p, int); 53 54 /****************************************************************************** 55 ****************************************************************************** 56 ** Utility routines 57 ****************************************************************************** 58 ******************************************************************************/ 59 60 /* 61 * Send hook information to the upper layer 62 */ 63 64 void 65 ng_l2cap_send_hook_info(node_p node, hook_p hook, void *arg1, int arg2) 66 { 67 ng_l2cap_p l2cap = NULL; 68 struct ng_mesg *msg = NULL; 69 int error = 0; 70 ng_l2cap_node_hook_info_ep *ep ; 71 72 if (node == NULL || NG_NODE_NOT_VALID(node) || 73 hook == NULL || NG_HOOK_NOT_VALID(hook)) 74 return; 75 76 l2cap = (ng_l2cap_p) NG_NODE_PRIVATE(node); 77 if (l2cap->hci == NULL || NG_HOOK_NOT_VALID(l2cap->hci) || 78 bcmp(&l2cap->bdaddr, NG_HCI_BDADDR_ANY, sizeof(l2cap->bdaddr)) == 0) 79 return; 80 81 NG_MKMESSAGE(msg, NGM_L2CAP_COOKIE, NGM_L2CAP_NODE_HOOK_INFO, 82 sizeof(*ep), M_NOWAIT); 83 84 if (msg != NULL) { 85 ep = (ng_l2cap_node_hook_info_ep *) &msg->data; 86 bcopy(&l2cap->bdaddr, &ep->addr, sizeof(bdaddr_t)); 87 NG_SEND_MSG_HOOK(error, node, msg, hook, 0); 88 } else 89 error = ENOMEM; 90 91 if (error != 0) 92 NG_L2CAP_INFO( 93 "%s: %s - failed to send HOOK_INFO message to hook \"%s\", error=%d\n", 94 __func__, NG_NODE_NAME(l2cap->node), NG_HOOK_NAME(hook), 95 error); 96 } /* ng_l2cap_send_hook_info */ 97 98 /* 99 * Create new connection descriptor for the "remote" unit. 100 * Will link connection descriptor to the l2cap node. 101 */ 102 103 ng_l2cap_con_p 104 ng_l2cap_new_con(ng_l2cap_p l2cap, bdaddr_p bdaddr, int type) 105 { 106 static int fake_con_handle = 0x0f00; 107 ng_l2cap_con_p con = NULL; 108 109 /* Create new connection descriptor */ 110 con = malloc(sizeof(*con), M_NETGRAPH_L2CAP, 111 M_NOWAIT|M_ZERO); 112 if (con == NULL) 113 return (NULL); 114 115 con->l2cap = l2cap; 116 con->state = NG_L2CAP_CON_CLOSED; 117 con->encryption = 0; 118 /* 119 * XXX 120 * 121 * Assign fake connection handle to the connection descriptor. 122 * Bluetooth specification marks 0x0f00 - 0x0fff connection 123 * handles as reserved. We need this fake connection handles 124 * for timeouts. Connection handle will be passed as argument 125 * to timeout so when timeout happens we can find the right 126 * connection descriptor. We can not pass pointers, because 127 * timeouts are external (to Netgraph) events and there might 128 * be a race when node/hook goes down and timeout event already 129 * went into node's queue 130 */ 131 132 con->con_handle = fake_con_handle ++; 133 if (fake_con_handle > 0x0fff) 134 fake_con_handle = 0x0f00; 135 136 bcopy(bdaddr, &con->remote, sizeof(con->remote)); 137 con->linktype = type; 138 ng_callout_init(&con->con_timo); 139 140 con->ident = NG_L2CAP_FIRST_IDENT - 1; 141 TAILQ_INIT(&con->cmd_list); 142 143 /* Link connection */ 144 LIST_INSERT_HEAD(&l2cap->con_list, con, next); 145 146 return (con); 147 } /* ng_l2cap_new_con */ 148 149 /* 150 * Add reference to the connection descriptor 151 */ 152 153 void 154 ng_l2cap_con_ref(ng_l2cap_con_p con) 155 { 156 con->refcnt ++; 157 158 if (con->flags & NG_L2CAP_CON_AUTO_DISCON_TIMO) { 159 if ((con->state != NG_L2CAP_CON_OPEN) || 160 (con->flags & NG_L2CAP_CON_OUTGOING) == 0) 161 panic( 162 "%s: %s - bad auto disconnect timeout, state=%d, flags=%#x\n", 163 __func__, NG_NODE_NAME(con->l2cap->node), 164 con->state, con->flags); 165 166 ng_l2cap_discon_untimeout(con); 167 } 168 } /* ng_l2cap_con_ref */ 169 170 /* 171 * Remove reference from the connection descriptor 172 */ 173 174 void 175 ng_l2cap_con_unref(ng_l2cap_con_p con) 176 { 177 con->refcnt --; 178 179 if (con->refcnt < 0) 180 panic( 181 "%s: %s - con->refcnt < 0\n", __func__, NG_NODE_NAME(con->l2cap->node)); 182 183 /* 184 * Set auto disconnect timer only if the following conditions are met: 185 * 1) we have no reference on the connection 186 * 2) connection is in OPEN state 187 * 3) it is an outgoing connection 188 * 4) disconnect timeout > 0 189 * 5) connection is not dying 190 */ 191 192 if ((con->refcnt == 0) && 193 (con->state == NG_L2CAP_CON_OPEN) && 194 (con->flags & NG_L2CAP_CON_OUTGOING) && 195 (con->l2cap->discon_timo > 0) && 196 ((con->flags & NG_L2CAP_CON_DYING) == 0)) 197 ng_l2cap_discon_timeout(con); 198 } /* ng_l2cap_con_unref */ 199 200 /* 201 * Set auto disconnect timeout 202 * XXX FIXME: check return code from ng_callout 203 */ 204 205 int 206 ng_l2cap_discon_timeout(ng_l2cap_con_p con) 207 { 208 if (con->flags & (NG_L2CAP_CON_LP_TIMO|NG_L2CAP_CON_AUTO_DISCON_TIMO)) 209 panic( 210 "%s: %s - invalid timeout, state=%d, flags=%#x\n", 211 __func__, NG_NODE_NAME(con->l2cap->node), 212 con->state, con->flags); 213 214 con->flags |= NG_L2CAP_CON_AUTO_DISCON_TIMO; 215 ng_callout(&con->con_timo, con->l2cap->node, NULL, 216 con->l2cap->discon_timo * hz, 217 ng_l2cap_process_discon_timeout, NULL, 218 con->con_handle); 219 220 return (0); 221 } /* ng_l2cap_discon_timeout */ 222 223 /* 224 * Unset auto disconnect timeout 225 */ 226 227 int 228 ng_l2cap_discon_untimeout(ng_l2cap_con_p con) 229 { 230 if (!(con->flags & NG_L2CAP_CON_AUTO_DISCON_TIMO)) 231 panic( 232 "%s: %s - no disconnect timeout, state=%d, flags=%#x\n", 233 __func__, NG_NODE_NAME(con->l2cap->node), 234 con->state, con->flags); 235 236 if (ng_uncallout(&con->con_timo, con->l2cap->node) == 0) 237 return (ETIMEDOUT); 238 239 con->flags &= ~NG_L2CAP_CON_AUTO_DISCON_TIMO; 240 241 return (0); 242 } /* ng_l2cap_discon_untimeout */ 243 244 /* 245 * Free connection descriptor. Will unlink connection and free everything. 246 */ 247 248 void 249 ng_l2cap_free_con(ng_l2cap_con_p con) 250 { 251 ng_l2cap_chan_p f = NULL, n = NULL; 252 253 con->state = NG_L2CAP_CON_CLOSED; 254 255 while (con->tx_pkt != NULL) { 256 struct mbuf *m = con->tx_pkt->m_nextpkt; 257 258 m_freem(con->tx_pkt); 259 con->tx_pkt = m; 260 } 261 262 NG_FREE_M(con->rx_pkt); 263 264 for (f = LIST_FIRST(&con->l2cap->chan_list); f != NULL; ) { 265 n = LIST_NEXT(f, next); 266 267 if (f->con == con) 268 ng_l2cap_free_chan(f); 269 270 f = n; 271 } 272 273 while (!TAILQ_EMPTY(&con->cmd_list)) { 274 ng_l2cap_cmd_p cmd = TAILQ_FIRST(&con->cmd_list); 275 276 ng_l2cap_unlink_cmd(cmd); 277 if (cmd->flags & NG_L2CAP_CMD_PENDING) 278 ng_l2cap_command_untimeout(cmd); 279 ng_l2cap_free_cmd(cmd); 280 } 281 282 if (con->flags & (NG_L2CAP_CON_AUTO_DISCON_TIMO|NG_L2CAP_CON_LP_TIMO)) 283 panic( 284 "%s: %s - timeout pending! state=%d, flags=%#x\n", 285 __func__, NG_NODE_NAME(con->l2cap->node), 286 con->state, con->flags); 287 288 LIST_REMOVE(con, next); 289 290 bzero(con, sizeof(*con)); 291 free(con, M_NETGRAPH_L2CAP); 292 } /* ng_l2cap_free_con */ 293 294 /* 295 * Get connection by "remote" address 296 */ 297 298 ng_l2cap_con_p 299 ng_l2cap_con_by_addr(ng_l2cap_p l2cap, bdaddr_p bdaddr, unsigned int type) 300 { 301 ng_l2cap_con_p con = NULL; 302 303 LIST_FOREACH(con, &l2cap->con_list, next) 304 if ((bcmp(bdaddr, &con->remote, sizeof(con->remote)) == 0)&& 305 (con->linktype == type)) 306 break; 307 308 return (con); 309 } /* ng_l2cap_con_by_addr */ 310 311 /* 312 * Get connection by "handle" 313 */ 314 315 ng_l2cap_con_p 316 ng_l2cap_con_by_handle(ng_l2cap_p l2cap, u_int16_t con_handle) 317 { 318 ng_l2cap_con_p con = NULL; 319 320 LIST_FOREACH(con, &l2cap->con_list, next) 321 if (con->con_handle == con_handle) 322 break; 323 324 return (con); 325 } /* ng_l2cap_con_by_handle */ 326 327 /* 328 * Allocate new L2CAP channel descriptor on "con" conection with "psm". 329 * Will link the channel to the l2cap node 330 */ 331 332 ng_l2cap_chan_p 333 ng_l2cap_new_chan(ng_l2cap_p l2cap, ng_l2cap_con_p con, u_int16_t psm, int idtype) 334 { 335 ng_l2cap_chan_p ch = NULL; 336 337 ch = malloc(sizeof(*ch), M_NETGRAPH_L2CAP, 338 M_NOWAIT|M_ZERO); 339 if (ch == NULL) 340 return (NULL); 341 if(idtype == NG_L2CAP_L2CA_IDTYPE_ATT){ 342 ch->scid = ch->dcid = NG_L2CAP_ATT_CID; 343 }else if(idtype == NG_L2CAP_L2CA_IDTYPE_SMP){ 344 ch->scid = ch->dcid = NG_L2CAP_SMP_CID; 345 }else{ 346 ch->scid = ng_l2cap_get_cid(l2cap, 347 (con->linktype!= NG_HCI_LINK_ACL)); 348 } 349 350 if (ch->scid != NG_L2CAP_NULL_CID) { 351 /* Initialize channel */ 352 ch->psm = psm; 353 ch->con = con; 354 ch->state = NG_L2CAP_CLOSED; 355 356 /* Set MTU and flow control settings to defaults */ 357 ch->imtu = NG_L2CAP_MTU_DEFAULT; 358 bcopy(ng_l2cap_default_flow(), &ch->iflow, sizeof(ch->iflow)); 359 360 ch->omtu = NG_L2CAP_MTU_DEFAULT; 361 bcopy(ng_l2cap_default_flow(), &ch->oflow, sizeof(ch->oflow)); 362 363 ch->flush_timo = NG_L2CAP_FLUSH_TIMO_DEFAULT; 364 ch->link_timo = NG_L2CAP_LINK_TIMO_DEFAULT; 365 366 LIST_INSERT_HEAD(&l2cap->chan_list, ch, next); 367 368 ng_l2cap_con_ref(con); 369 } else { 370 bzero(ch, sizeof(*ch)); 371 free(ch, M_NETGRAPH_L2CAP); 372 ch = NULL; 373 } 374 375 return (ch); 376 } /* ng_l2cap_new_chan */ 377 378 379 ng_l2cap_chan_p 380 ng_l2cap_chan_by_scid(ng_l2cap_p l2cap, u_int16_t scid, int idtype) 381 { 382 ng_l2cap_chan_p ch = NULL; 383 384 if((idtype == NG_L2CAP_L2CA_IDTYPE_ATT)|| 385 (idtype == NG_L2CAP_L2CA_IDTYPE_SMP)){ 386 return NULL; 387 } 388 389 LIST_FOREACH(ch, &l2cap->chan_list, next){ 390 if((idtype != NG_L2CAP_L2CA_IDTYPE_BREDR)&& 391 (ch->con->linktype == NG_HCI_LINK_ACL )) 392 continue; 393 if((idtype != NG_L2CAP_L2CA_IDTYPE_LE)&& 394 (ch->con->linktype != NG_HCI_LINK_ACL )) 395 continue; 396 if (ch->scid == scid) 397 break; 398 } 399 return (ch); 400 } /* ng_l2cap_chan_by_scid */ 401 402 ng_l2cap_chan_p 403 ng_l2cap_chan_by_conhandle(ng_l2cap_p l2cap, uint16_t scid, 404 u_int16_t con_handle) 405 { 406 ng_l2cap_chan_p ch = NULL; 407 408 409 LIST_FOREACH(ch, &l2cap->chan_list, next){ 410 if ((ch->scid == scid) && 411 (ch->con->con_handle == con_handle)) 412 break; 413 } 414 return (ch); 415 } /* ng_l2cap_chan_by_scid */ 416 417 /* 418 * Free channel descriptor. 419 */ 420 421 void 422 ng_l2cap_free_chan(ng_l2cap_chan_p ch) 423 { 424 ng_l2cap_cmd_p f = NULL, n = NULL; 425 426 f = TAILQ_FIRST(&ch->con->cmd_list); 427 428 while (f != NULL) { 429 n = TAILQ_NEXT(f, next); 430 431 if (f->ch == ch) { 432 ng_l2cap_unlink_cmd(f); 433 if (f->flags & NG_L2CAP_CMD_PENDING) 434 ng_l2cap_command_untimeout(f); 435 ng_l2cap_free_cmd(f); 436 } 437 438 f = n; 439 } 440 441 LIST_REMOVE(ch, next); 442 443 ng_l2cap_con_unref(ch->con); 444 445 bzero(ch, sizeof(*ch)); 446 free(ch, M_NETGRAPH_L2CAP); 447 } /* ng_l2cap_free_chan */ 448 449 /* 450 * Create new L2CAP command descriptor. WILL NOT add command to the queue. 451 */ 452 453 ng_l2cap_cmd_p 454 ng_l2cap_new_cmd(ng_l2cap_con_p con, ng_l2cap_chan_p ch, u_int8_t ident, 455 u_int8_t code, u_int32_t token) 456 { 457 ng_l2cap_cmd_p cmd = NULL; 458 459 KASSERT((ch == NULL || ch->con == con), 460 ("%s: %s - invalid channel pointer!\n", 461 __func__, NG_NODE_NAME(con->l2cap->node))); 462 463 cmd = malloc(sizeof(*cmd), M_NETGRAPH_L2CAP, 464 M_NOWAIT|M_ZERO); 465 if (cmd == NULL) 466 return (NULL); 467 468 cmd->con = con; 469 cmd->ch = ch; 470 cmd->ident = ident; 471 cmd->code = code; 472 cmd->token = token; 473 ng_callout_init(&cmd->timo); 474 475 return (cmd); 476 } /* ng_l2cap_new_cmd */ 477 478 /* 479 * Get pending (i.e. initiated by local side) L2CAP command descriptor by ident 480 */ 481 482 ng_l2cap_cmd_p 483 ng_l2cap_cmd_by_ident(ng_l2cap_con_p con, u_int8_t ident) 484 { 485 ng_l2cap_cmd_p cmd = NULL; 486 487 TAILQ_FOREACH(cmd, &con->cmd_list, next) { 488 if ((cmd->flags & NG_L2CAP_CMD_PENDING) && cmd->ident == ident) { 489 KASSERT((cmd->con == con), 490 ("%s: %s - invalid connection pointer!\n", 491 __func__, NG_NODE_NAME(con->l2cap->node))); 492 493 break; 494 } 495 } 496 497 return (cmd); 498 } /* ng_l2cap_cmd_by_ident */ 499 500 /* 501 * Set LP timeout 502 * XXX FIXME: check return code from ng_callout 503 */ 504 505 int 506 ng_l2cap_lp_timeout(ng_l2cap_con_p con) 507 { 508 if (con->flags & (NG_L2CAP_CON_LP_TIMO|NG_L2CAP_CON_AUTO_DISCON_TIMO)) 509 panic( 510 "%s: %s - invalid timeout, state=%d, flags=%#x\n", 511 __func__, NG_NODE_NAME(con->l2cap->node), 512 con->state, con->flags); 513 514 con->flags |= NG_L2CAP_CON_LP_TIMO; 515 ng_callout(&con->con_timo, con->l2cap->node, NULL, 516 bluetooth_hci_connect_timeout(), 517 ng_l2cap_process_lp_timeout, NULL, 518 con->con_handle); 519 520 return (0); 521 } /* ng_l2cap_lp_timeout */ 522 523 /* 524 * Unset LP timeout 525 */ 526 527 int 528 ng_l2cap_lp_untimeout(ng_l2cap_con_p con) 529 { 530 if (!(con->flags & NG_L2CAP_CON_LP_TIMO)) 531 panic( 532 "%s: %s - no LP connection timeout, state=%d, flags=%#x\n", 533 __func__, NG_NODE_NAME(con->l2cap->node), 534 con->state, con->flags); 535 536 if (ng_uncallout(&con->con_timo, con->l2cap->node) == 0) 537 return (ETIMEDOUT); 538 539 con->flags &= ~NG_L2CAP_CON_LP_TIMO; 540 541 return (0); 542 } /* ng_l2cap_lp_untimeout */ 543 544 /* 545 * Set L2CAP command timeout 546 * XXX FIXME: check return code from ng_callout 547 */ 548 549 int 550 ng_l2cap_command_timeout(ng_l2cap_cmd_p cmd, int timo) 551 { 552 int arg; 553 554 if (cmd->flags & NG_L2CAP_CMD_PENDING) 555 panic( 556 "%s: %s - duplicated command timeout, code=%#x, flags=%#x\n", 557 __func__, NG_NODE_NAME(cmd->con->l2cap->node), 558 cmd->code, cmd->flags); 559 560 arg = ((cmd->ident << 16) | cmd->con->con_handle); 561 cmd->flags |= NG_L2CAP_CMD_PENDING; 562 ng_callout(&cmd->timo, cmd->con->l2cap->node, NULL, timo, 563 ng_l2cap_process_command_timeout, NULL, arg); 564 565 return (0); 566 } /* ng_l2cap_command_timeout */ 567 568 /* 569 * Unset L2CAP command timeout 570 */ 571 572 int 573 ng_l2cap_command_untimeout(ng_l2cap_cmd_p cmd) 574 { 575 if (!(cmd->flags & NG_L2CAP_CMD_PENDING)) 576 panic( 577 "%s: %s - no command timeout, code=%#x, flags=%#x\n", 578 __func__, NG_NODE_NAME(cmd->con->l2cap->node), 579 cmd->code, cmd->flags); 580 581 if (ng_uncallout(&cmd->timo, cmd->con->l2cap->node) == 0) 582 return (ETIMEDOUT); 583 584 cmd->flags &= ~NG_L2CAP_CMD_PENDING; 585 586 return (0); 587 } /* ng_l2cap_command_untimeout */ 588 589 /* 590 * Prepend "m"buf with "size" bytes 591 */ 592 593 struct mbuf * 594 ng_l2cap_prepend(struct mbuf *m, int size) 595 { 596 M_PREPEND(m, size, M_NOWAIT); 597 if (m == NULL || (m->m_len < size && (m = m_pullup(m, size)) == NULL)) 598 return (NULL); 599 600 return (m); 601 } /* ng_l2cap_prepend */ 602 603 /* 604 * Default flow settings 605 */ 606 607 ng_l2cap_flow_p 608 ng_l2cap_default_flow(void) 609 { 610 static ng_l2cap_flow_t default_flow = { 611 /* flags */ 0x0, 612 /* service_type */ NG_HCI_SERVICE_TYPE_BEST_EFFORT, 613 /* token_rate */ 0xffffffff, /* maximum */ 614 /* token_bucket_size */ 0xffffffff, /* maximum */ 615 /* peak_bandwidth */ 0x00000000, /* maximum */ 616 /* latency */ 0xffffffff, /* don't care */ 617 /* delay_variation */ 0xffffffff /* don't care */ 618 }; 619 620 return (&default_flow); 621 } /* ng_l2cap_default_flow */ 622 623 /* 624 * Get next available channel ID 625 * XXX FIXME this is *UGLY* but will do for now 626 */ 627 628 static u_int16_t 629 ng_l2cap_get_cid(ng_l2cap_p l2cap,int isle) 630 { 631 u_int16_t cid ; 632 u_int16_t endcid; 633 uint16_t mask; 634 int idtype; 635 if(isle){ 636 endcid = l2cap->lecid; 637 /*Assume Last CID is 2^n-1 */ 638 mask = NG_L2CAP_LELAST_CID; 639 idtype = NG_L2CAP_L2CA_IDTYPE_LE; 640 }else{ 641 endcid = l2cap->cid; 642 /*Assume Last CID is 2^n-1 */ 643 mask = NG_L2CAP_LAST_CID; 644 idtype = NG_L2CAP_L2CA_IDTYPE_BREDR; 645 } 646 cid = (endcid+1) & mask; 647 648 if (cid < NG_L2CAP_FIRST_CID) 649 cid = NG_L2CAP_FIRST_CID; 650 651 while (cid != endcid) { 652 if (ng_l2cap_chan_by_scid(l2cap, cid, idtype) == NULL) { 653 if(!isle){ 654 l2cap->cid = cid; 655 }else{ 656 l2cap->lecid = cid; 657 } 658 return (cid); 659 } 660 661 cid ++; 662 cid &= mask; 663 if (cid < NG_L2CAP_FIRST_CID) 664 cid = NG_L2CAP_FIRST_CID; 665 } 666 667 return (NG_L2CAP_NULL_CID); 668 } /* ng_l2cap_get_cid */ 669 670 /* 671 * Get next available command ident 672 * XXX FIXME this is *UGLY* but will do for now 673 */ 674 675 u_int8_t 676 ng_l2cap_get_ident(ng_l2cap_con_p con) 677 { 678 u_int8_t ident = con->ident + 1; 679 680 if (ident < NG_L2CAP_FIRST_IDENT) 681 ident = NG_L2CAP_FIRST_IDENT; 682 683 while (ident != con->ident) { 684 if (ng_l2cap_cmd_by_ident(con, ident) == NULL) { 685 con->ident = ident; 686 687 return (ident); 688 } 689 690 ident ++; 691 if (ident < NG_L2CAP_FIRST_IDENT) 692 ident = NG_L2CAP_FIRST_IDENT; 693 } 694 695 return (NG_L2CAP_NULL_IDENT); 696 } /* ng_l2cap_get_ident */ 697 698