6 * Copyright (c) Maksim Yevmenkin <m_evmenkin@yahoo.com>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
30 * $Id: ng_l2cap_misc.c,v 1.5 2003/09/08 19:11:45 max Exp $
31 * $FreeBSD: src/sys/netgraph/bluetooth/l2cap/ng_l2cap_misc.c,v 1.12 2005/08/31 18:13:23 emax Exp $
32 * $DragonFly: src/sys/netgraph7/bluetooth/l2cap/ng_l2cap_misc.c,v 1.2 2008/06/26 23:05:40 dillon Exp $
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
40 #include <sys/queue.h>
41 #include "ng_message.h"
43 #include "bluetooth/include/ng_bluetooth.h"
44 #include "bluetooth/include/ng_hci.h"
45 #include "bluetooth/include/ng_l2cap.h"
46 #include "bluetooth/l2cap/ng_l2cap_var.h"
47 #include "bluetooth/l2cap/ng_l2cap_cmds.h"
48 #include "bluetooth/l2cap/ng_l2cap_evnt.h"
49 #include "bluetooth/l2cap/ng_l2cap_llpi.h"
50 #include "bluetooth/l2cap/ng_l2cap_ulpi.h"
51 #include "bluetooth/l2cap/ng_l2cap_misc.h"
53 static u_int16_t ng_l2cap_get_cid (ng_l2cap_p);
55 /******************************************************************************
56 ******************************************************************************
58 ******************************************************************************
59 ******************************************************************************/
62 * Send hook information to the upper layer
66 ng_l2cap_send_hook_info(node_p node, hook_p hook, void *arg1, int arg2)
68 ng_l2cap_p l2cap = NULL;
69 struct ng_mesg *msg = NULL;
72 if (node == NULL || NG_NODE_NOT_VALID(node) ||
73 hook == NULL || NG_HOOK_NOT_VALID(hook))
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)
81 NG_MKMESSAGE(msg, NGM_L2CAP_COOKIE, NGM_L2CAP_NODE_HOOK_INFO,
82 sizeof(bdaddr_t), M_WAITOK | M_NULLOK);
84 bcopy(&l2cap->bdaddr, msg->data, sizeof(bdaddr_t));
85 NG_SEND_MSG_HOOK(error, node, msg, hook, 0);
91 "%s: %s - failed to send HOOK_INFO message to hook \"%s\", error=%d\n",
92 __func__, NG_NODE_NAME(l2cap->node), NG_HOOK_NAME(hook),
94 } /* ng_l2cap_send_hook_info */
97 * Create new connection descriptor for the "remote" unit.
98 * Will link connection descriptor to the l2cap node.
102 ng_l2cap_new_con(ng_l2cap_p l2cap, bdaddr_p bdaddr)
104 static int fake_con_handle = 0x0f00;
105 ng_l2cap_con_p con = NULL;
107 /* Create new connection descriptor */
108 MALLOC(con, ng_l2cap_con_p, sizeof(*con), M_NETGRAPH_L2CAP,
109 M_WAITOK | M_NULLOK | M_ZERO);
114 con->state = NG_L2CAP_CON_CLOSED;
119 * Assign fake connection handle to the connection descriptor.
120 * Bluetooth specification marks 0x0f00 - 0x0fff connection
121 * handles as reserved. We need this fake connection handles
122 * for timeouts. Connection handle will be passed as argument
123 * to timeout so when timeout happens we can find the right
124 * connection descriptor. We can not pass pointers, because
125 * timeouts are external (to Netgraph) events and there might
126 * be a race when node/hook goes down and timeout event already
127 * went into node's queue
130 con->con_handle = fake_con_handle ++;
131 if (fake_con_handle > 0x0fff)
132 fake_con_handle = 0x0f00;
134 bcopy(bdaddr, &con->remote, sizeof(con->remote));
135 ng_callout_init(&con->con_timo);
137 con->ident = NG_L2CAP_FIRST_IDENT - 1;
138 TAILQ_INIT(&con->cmd_list);
140 /* Link connection */
141 LIST_INSERT_HEAD(&l2cap->con_list, con, next);
144 } /* ng_l2cap_new_con */
147 * Add reference to the connection descriptor
151 ng_l2cap_con_ref(ng_l2cap_con_p con)
155 if (con->flags & NG_L2CAP_CON_AUTO_DISCON_TIMO) {
156 if ((con->state != NG_L2CAP_CON_OPEN) ||
157 (con->flags & NG_L2CAP_CON_OUTGOING) == 0)
159 "%s: %s - bad auto disconnect timeout, state=%d, flags=%#x\n",
160 __func__, NG_NODE_NAME(con->l2cap->node),
161 con->state, con->flags);
163 ng_l2cap_discon_untimeout(con);
165 } /* ng_l2cap_con_ref */
168 * Remove reference from the connection descriptor
172 ng_l2cap_con_unref(ng_l2cap_con_p con)
178 "%s: %s - con->refcnt < 0\n", __func__, NG_NODE_NAME(con->l2cap->node));
181 * Set auto disconnect timer only if the following conditions are met:
182 * 1) we have no reference on the connection
183 * 2) connection is in OPEN state
184 * 3) it is an outgoing connection
185 * 4) disconnect timeout > 0
186 * 5) connection is not dying
189 if ((con->refcnt == 0) &&
190 (con->state == NG_L2CAP_CON_OPEN) &&
191 (con->flags & NG_L2CAP_CON_OUTGOING) &&
192 (con->l2cap->discon_timo > 0) &&
193 ((con->flags & NG_L2CAP_CON_DYING) == 0))
194 ng_l2cap_discon_timeout(con);
195 } /* ng_l2cap_con_unref */
198 * Set auto disconnect timeout
199 * XXX FIXME: check return code from ng_callout
203 ng_l2cap_discon_timeout(ng_l2cap_con_p con)
205 if (con->flags & (NG_L2CAP_CON_LP_TIMO|NG_L2CAP_CON_AUTO_DISCON_TIMO))
207 "%s: %s - invalid timeout, state=%d, flags=%#x\n",
208 __func__, NG_NODE_NAME(con->l2cap->node),
209 con->state, con->flags);
211 con->flags |= NG_L2CAP_CON_AUTO_DISCON_TIMO;
212 ng_callout(&con->con_timo, con->l2cap->node, NULL,
213 con->l2cap->discon_timo * hz,
214 ng_l2cap_process_discon_timeout, NULL,
218 } /* ng_l2cap_discon_timeout */
221 * Unset auto disconnect timeout
225 ng_l2cap_discon_untimeout(ng_l2cap_con_p con)
227 if (!(con->flags & NG_L2CAP_CON_AUTO_DISCON_TIMO))
229 "%s: %s - no disconnect timeout, state=%d, flags=%#x\n",
230 __func__, NG_NODE_NAME(con->l2cap->node),
231 con->state, con->flags);
233 if (ng_uncallout(&con->con_timo, con->l2cap->node) == 0)
236 con->flags &= ~NG_L2CAP_CON_AUTO_DISCON_TIMO;
239 } /* ng_l2cap_discon_untimeout */
242 * Free connection descriptor. Will unlink connection and free everything.
246 ng_l2cap_free_con(ng_l2cap_con_p con)
248 ng_l2cap_chan_p f = NULL, n = NULL;
250 con->state = NG_L2CAP_CON_CLOSED;
252 while (con->tx_pkt != NULL) {
253 struct mbuf *m = con->tx_pkt->m_nextpkt;
255 m_freem(con->tx_pkt);
259 NG_FREE_M(con->rx_pkt);
261 for (f = LIST_FIRST(&con->l2cap->chan_list); f != NULL; ) {
262 n = LIST_NEXT(f, next);
265 ng_l2cap_free_chan(f);
270 while (!TAILQ_EMPTY(&con->cmd_list)) {
271 ng_l2cap_cmd_p cmd = TAILQ_FIRST(&con->cmd_list);
273 ng_l2cap_unlink_cmd(cmd);
274 if (cmd->flags & NG_L2CAP_CMD_PENDING)
275 ng_l2cap_command_untimeout(cmd);
276 ng_l2cap_free_cmd(cmd);
279 if (con->flags & (NG_L2CAP_CON_AUTO_DISCON_TIMO|NG_L2CAP_CON_LP_TIMO))
281 "%s: %s - timeout pending! state=%d, flags=%#x\n",
282 __func__, NG_NODE_NAME(con->l2cap->node),
283 con->state, con->flags);
285 LIST_REMOVE(con, next);
287 bzero(con, sizeof(*con));
288 FREE(con, M_NETGRAPH_L2CAP);
289 } /* ng_l2cap_free_con */
292 * Get connection by "remote" address
296 ng_l2cap_con_by_addr(ng_l2cap_p l2cap, bdaddr_p bdaddr)
298 ng_l2cap_con_p con = NULL;
300 LIST_FOREACH(con, &l2cap->con_list, next)
301 if (bcmp(bdaddr, &con->remote, sizeof(con->remote)) == 0)
305 } /* ng_l2cap_con_by_addr */
308 * Get connection by "handle"
312 ng_l2cap_con_by_handle(ng_l2cap_p l2cap, u_int16_t con_handle)
314 ng_l2cap_con_p con = NULL;
316 LIST_FOREACH(con, &l2cap->con_list, next)
317 if (con->con_handle == con_handle)
321 } /* ng_l2cap_con_by_handle */
324 * Allocate new L2CAP channel descriptor on "con" conection with "psm".
325 * Will link the channel to the l2cap node
329 ng_l2cap_new_chan(ng_l2cap_p l2cap, ng_l2cap_con_p con, u_int16_t psm)
331 ng_l2cap_chan_p ch = NULL;
333 MALLOC(ch, ng_l2cap_chan_p, sizeof(*ch), M_NETGRAPH_L2CAP,
334 M_WAITOK | M_NULLOK | M_ZERO);
338 ch->scid = ng_l2cap_get_cid(l2cap);
340 if (ch->scid != NG_L2CAP_NULL_CID) {
341 /* Initialize channel */
344 ch->state = NG_L2CAP_CLOSED;
346 /* Set MTU and flow control settings to defaults */
347 ch->imtu = NG_L2CAP_MTU_DEFAULT;
348 bcopy(ng_l2cap_default_flow(), &ch->iflow, sizeof(ch->iflow));
350 ch->omtu = NG_L2CAP_MTU_DEFAULT;
351 bcopy(ng_l2cap_default_flow(), &ch->oflow, sizeof(ch->oflow));
353 ch->flush_timo = NG_L2CAP_FLUSH_TIMO_DEFAULT;
354 ch->link_timo = NG_L2CAP_LINK_TIMO_DEFAULT;
356 LIST_INSERT_HEAD(&l2cap->chan_list, ch, next);
358 ng_l2cap_con_ref(con);
360 bzero(ch, sizeof(*ch));
361 FREE(ch, M_NETGRAPH_L2CAP);
366 } /* ng_l2cap_new_chan */
369 * Get channel by source (local) channel ID
373 ng_l2cap_chan_by_scid(ng_l2cap_p l2cap, u_int16_t scid)
375 ng_l2cap_chan_p ch = NULL;
377 LIST_FOREACH(ch, &l2cap->chan_list, next)
378 if (ch->scid == scid)
382 } /* ng_l2cap_chan_by_scid */
385 * Free channel descriptor.
389 ng_l2cap_free_chan(ng_l2cap_chan_p ch)
391 ng_l2cap_cmd_p f = NULL, n = NULL;
393 f = TAILQ_FIRST(&ch->con->cmd_list);
395 n = TAILQ_NEXT(f, next);
398 ng_l2cap_unlink_cmd(f);
399 if (f->flags & NG_L2CAP_CMD_PENDING)
400 ng_l2cap_command_untimeout(f);
401 ng_l2cap_free_cmd(f);
407 LIST_REMOVE(ch, next);
409 ng_l2cap_con_unref(ch->con);
411 bzero(ch, sizeof(*ch));
412 FREE(ch, M_NETGRAPH_L2CAP);
413 } /* ng_l2cap_free_chan */
416 * Create new L2CAP command descriptor. WILL NOT add command to the queue.
420 ng_l2cap_new_cmd(ng_l2cap_con_p con, ng_l2cap_chan_p ch, u_int8_t ident,
421 u_int8_t code, u_int32_t token)
423 ng_l2cap_cmd_p cmd = NULL;
425 KASSERT((ch == NULL || ch->con == con),
426 ("%s: %s - invalid channel pointer!\n",
427 __func__, NG_NODE_NAME(con->l2cap->node)));
429 MALLOC(cmd, ng_l2cap_cmd_p, sizeof(*cmd), M_NETGRAPH_L2CAP,
430 M_WAITOK | M_NULLOK | M_ZERO);
439 ng_callout_init(&cmd->timo);
442 } /* ng_l2cap_new_cmd */
445 * Get pending (i.e. initiated by local side) L2CAP command descriptor by ident
449 ng_l2cap_cmd_by_ident(ng_l2cap_con_p con, u_int8_t ident)
451 ng_l2cap_cmd_p cmd = NULL;
453 TAILQ_FOREACH(cmd, &con->cmd_list, next) {
454 if ((cmd->flags & NG_L2CAP_CMD_PENDING) && cmd->ident == ident) {
455 KASSERT((cmd->con == con),
456 ("%s: %s - invalid connection pointer!\n",
457 __func__, NG_NODE_NAME(con->l2cap->node)));
464 } /* ng_l2cap_cmd_by_ident */
468 * XXX FIXME: check return code from ng_callout
472 ng_l2cap_lp_timeout(ng_l2cap_con_p con)
474 if (con->flags & (NG_L2CAP_CON_LP_TIMO|NG_L2CAP_CON_AUTO_DISCON_TIMO))
476 "%s: %s - invalid timeout, state=%d, flags=%#x\n",
477 __func__, NG_NODE_NAME(con->l2cap->node),
478 con->state, con->flags);
480 con->flags |= NG_L2CAP_CON_LP_TIMO;
481 ng_callout(&con->con_timo, con->l2cap->node, NULL,
482 bluetooth_hci_connect_timeout(),
483 ng_l2cap_process_lp_timeout, NULL,
487 } /* ng_l2cap_lp_timeout */
494 ng_l2cap_lp_untimeout(ng_l2cap_con_p con)
496 if (!(con->flags & NG_L2CAP_CON_LP_TIMO))
498 "%s: %s - no LP connection timeout, state=%d, flags=%#x\n",
499 __func__, NG_NODE_NAME(con->l2cap->node),
500 con->state, con->flags);
502 if (ng_uncallout(&con->con_timo, con->l2cap->node) == 0)
505 con->flags &= ~NG_L2CAP_CON_LP_TIMO;
508 } /* ng_l2cap_lp_untimeout */
511 * Set L2CAP command timeout
512 * XXX FIXME: check return code from ng_callout
516 ng_l2cap_command_timeout(ng_l2cap_cmd_p cmd, int timo)
520 if (cmd->flags & NG_L2CAP_CMD_PENDING)
522 "%s: %s - duplicated command timeout, code=%#x, flags=%#x\n",
523 __func__, NG_NODE_NAME(cmd->con->l2cap->node),
524 cmd->code, cmd->flags);
526 arg = ((cmd->ident << 16) | cmd->con->con_handle);
527 cmd->flags |= NG_L2CAP_CMD_PENDING;
528 ng_callout(&cmd->timo, cmd->con->l2cap->node, NULL, timo,
529 ng_l2cap_process_command_timeout, NULL, arg);
532 } /* ng_l2cap_command_timeout */
535 * Unset L2CAP command timeout
539 ng_l2cap_command_untimeout(ng_l2cap_cmd_p cmd)
541 if (!(cmd->flags & NG_L2CAP_CMD_PENDING))
543 "%s: %s - no command timeout, code=%#x, flags=%#x\n",
544 __func__, NG_NODE_NAME(cmd->con->l2cap->node),
545 cmd->code, cmd->flags);
547 if (ng_uncallout(&cmd->timo, cmd->con->l2cap->node) == 0)
550 cmd->flags &= ~NG_L2CAP_CMD_PENDING;
553 } /* ng_l2cap_command_untimeout */
556 * Prepend "m"buf with "size" bytes
560 ng_l2cap_prepend(struct mbuf *m, int size)
562 M_PREPEND(m, size, MB_DONTWAIT);
563 if (m == NULL || (m->m_len < size && (m = m_pullup(m, size)) == NULL))
567 } /* ng_l2cap_prepend */
570 * Default flow settings
574 ng_l2cap_default_flow(void)
576 static ng_l2cap_flow_t default_flow = {
578 /* service_type */ NG_HCI_SERVICE_TYPE_BEST_EFFORT,
579 /* token_rate */ 0xffffffff, /* maximum */
580 /* token_bucket_size */ 0xffffffff, /* maximum */
581 /* peak_bandwidth */ 0x00000000, /* maximum */
582 /* latency */ 0xffffffff, /* don't care */
583 /* delay_variation */ 0xffffffff /* don't care */
586 return (&default_flow);
587 } /* ng_l2cap_default_flow */
590 * Get next available channel ID
591 * XXX FIXME this is *UGLY* but will do for now
595 ng_l2cap_get_cid(ng_l2cap_p l2cap)
597 u_int16_t cid = l2cap->cid + 1;
599 if (cid < NG_L2CAP_FIRST_CID)
600 cid = NG_L2CAP_FIRST_CID;
602 while (cid != l2cap->cid) {
603 if (ng_l2cap_chan_by_scid(l2cap, cid) == NULL) {
610 if (cid < NG_L2CAP_FIRST_CID)
611 cid = NG_L2CAP_FIRST_CID;
614 return (NG_L2CAP_NULL_CID);
615 } /* ng_l2cap_get_cid */
618 * Get next available command ident
619 * XXX FIXME this is *UGLY* but will do for now
623 ng_l2cap_get_ident(ng_l2cap_con_p con)
625 u_int8_t ident = con->ident + 1;
627 if (ident < NG_L2CAP_FIRST_IDENT)
628 ident = NG_L2CAP_FIRST_IDENT;
630 while (ident != con->ident) {
631 if (ng_l2cap_cmd_by_ident(con, ident) == NULL) {
638 if (ident < NG_L2CAP_FIRST_IDENT)
639 ident = NG_L2CAP_FIRST_IDENT;
642 return (NG_L2CAP_NULL_IDENT);
643 } /* ng_l2cap_get_ident */