kernel/netgraph7: Replace usage of MALLOC/FREE with kmalloc/kfree here too.
[dragonfly.git] / sys / netgraph7 / bluetooth / l2cap / ng_l2cap_misc.c
CommitLineData
b06ebda0
MD
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: src/sys/netgraph/bluetooth/l2cap/ng_l2cap_misc.c,v 1.12 2005/08/31 18:13:23 emax Exp $
5a975a3d 32 * $DragonFly: src/sys/netgraph7/bluetooth/l2cap/ng_l2cap_misc.c,v 1.2 2008/06/26 23:05:40 dillon Exp $
b06ebda0
MD
33 */
34
35#include <sys/param.h>
36#include <sys/systm.h>
37#include <sys/kernel.h>
38#include <sys/malloc.h>
39#include <sys/mbuf.h>
40#include <sys/queue.h>
5a975a3d
MD
41#include "ng_message.h"
42#include "netgraph.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"
b06ebda0
MD
52
53static u_int16_t ng_l2cap_get_cid (ng_l2cap_p);
54
55/******************************************************************************
56 ******************************************************************************
57 ** Utility routines
58 ******************************************************************************
59 ******************************************************************************/
60
61/*
62 * Send hook information to the upper layer
63 */
64
65void
66ng_l2cap_send_hook_info(node_p node, hook_p hook, void *arg1, int arg2)
67{
68 ng_l2cap_p l2cap = NULL;
69 struct ng_mesg *msg = NULL;
70 int error = 0;
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,
5a975a3d 82 sizeof(bdaddr_t), M_WAITOK | M_NULLOK);
b06ebda0
MD
83 if (msg != NULL) {
84 bcopy(&l2cap->bdaddr, msg->data, sizeof(bdaddr_t));
85 NG_SEND_MSG_HOOK(error, node, msg, hook, 0);
86 } else
87 error = ENOMEM;
88
89 if (error != 0)
90 NG_L2CAP_INFO(
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),
93 error);
94} /* ng_l2cap_send_hook_info */
95
96/*
97 * Create new connection descriptor for the "remote" unit.
98 * Will link connection descriptor to the l2cap node.
99 */
100
101ng_l2cap_con_p
102ng_l2cap_new_con(ng_l2cap_p l2cap, bdaddr_p bdaddr)
103{
104 static int fake_con_handle = 0x0f00;
105 ng_l2cap_con_p con = NULL;
106
107 /* Create new connection descriptor */
fc025606
SW
108 con = kmalloc(sizeof(*con), M_NETGRAPH_L2CAP,
109 M_WAITOK | M_NULLOK | M_ZERO);
b06ebda0
MD
110 if (con == NULL)
111 return (NULL);
112
113 con->l2cap = l2cap;
114 con->state = NG_L2CAP_CON_CLOSED;
115
116 /*
117 * XXX
118 *
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
128 */
129
130 con->con_handle = fake_con_handle ++;
131 if (fake_con_handle > 0x0fff)
132 fake_con_handle = 0x0f00;
133
134 bcopy(bdaddr, &con->remote, sizeof(con->remote));
135 ng_callout_init(&con->con_timo);
136
137 con->ident = NG_L2CAP_FIRST_IDENT - 1;
138 TAILQ_INIT(&con->cmd_list);
139
140 /* Link connection */
141 LIST_INSERT_HEAD(&l2cap->con_list, con, next);
142
143 return (con);
144} /* ng_l2cap_new_con */
145
146/*
147 * Add reference to the connection descriptor
148 */
149
150void
151ng_l2cap_con_ref(ng_l2cap_con_p con)
152{
153 con->refcnt ++;
154
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)
158 panic(
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);
162
163 ng_l2cap_discon_untimeout(con);
164 }
165} /* ng_l2cap_con_ref */
166
167/*
168 * Remove reference from the connection descriptor
169 */
170
171void
172ng_l2cap_con_unref(ng_l2cap_con_p con)
173{
174 con->refcnt --;
175
176 if (con->refcnt < 0)
177 panic(
178"%s: %s - con->refcnt < 0\n", __func__, NG_NODE_NAME(con->l2cap->node));
179
180 /*
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
187 */
188
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 */
196
197/*
198 * Set auto disconnect timeout
199 * XXX FIXME: check return code from ng_callout
200 */
201
202int
203ng_l2cap_discon_timeout(ng_l2cap_con_p con)
204{
205 if (con->flags & (NG_L2CAP_CON_LP_TIMO|NG_L2CAP_CON_AUTO_DISCON_TIMO))
206 panic(
207"%s: %s - invalid timeout, state=%d, flags=%#x\n",
208 __func__, NG_NODE_NAME(con->l2cap->node),
209 con->state, con->flags);
210
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,
215 con->con_handle);
216
217 return (0);
218} /* ng_l2cap_discon_timeout */
219
220/*
221 * Unset auto disconnect timeout
222 */
223
224int
225ng_l2cap_discon_untimeout(ng_l2cap_con_p con)
226{
227 if (!(con->flags & NG_L2CAP_CON_AUTO_DISCON_TIMO))
228 panic(
229"%s: %s - no disconnect timeout, state=%d, flags=%#x\n",
230 __func__, NG_NODE_NAME(con->l2cap->node),
231 con->state, con->flags);
232
233 if (ng_uncallout(&con->con_timo, con->l2cap->node) == 0)
234 return (ETIMEDOUT);
235
236 con->flags &= ~NG_L2CAP_CON_AUTO_DISCON_TIMO;
237
238 return (0);
239} /* ng_l2cap_discon_untimeout */
240
241/*
242 * Free connection descriptor. Will unlink connection and free everything.
243 */
244
245void
246ng_l2cap_free_con(ng_l2cap_con_p con)
247{
248 ng_l2cap_chan_p f = NULL, n = NULL;
249
250 con->state = NG_L2CAP_CON_CLOSED;
251
252 while (con->tx_pkt != NULL) {
253 struct mbuf *m = con->tx_pkt->m_nextpkt;
254
255 m_freem(con->tx_pkt);
256 con->tx_pkt = m;
257 }
258
259 NG_FREE_M(con->rx_pkt);
260
261 for (f = LIST_FIRST(&con->l2cap->chan_list); f != NULL; ) {
262 n = LIST_NEXT(f, next);
263
264 if (f->con == con)
265 ng_l2cap_free_chan(f);
266
267 f = n;
268 }
269
270 while (!TAILQ_EMPTY(&con->cmd_list)) {
271 ng_l2cap_cmd_p cmd = TAILQ_FIRST(&con->cmd_list);
272
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);
277 }
278
279 if (con->flags & (NG_L2CAP_CON_AUTO_DISCON_TIMO|NG_L2CAP_CON_LP_TIMO))
280 panic(
281"%s: %s - timeout pending! state=%d, flags=%#x\n",
282 __func__, NG_NODE_NAME(con->l2cap->node),
283 con->state, con->flags);
284
285 LIST_REMOVE(con, next);
286
287 bzero(con, sizeof(*con));
fc025606 288 kfree(con, M_NETGRAPH_L2CAP);
b06ebda0
MD
289} /* ng_l2cap_free_con */
290
291/*
292 * Get connection by "remote" address
293 */
294
295ng_l2cap_con_p
296ng_l2cap_con_by_addr(ng_l2cap_p l2cap, bdaddr_p bdaddr)
297{
298 ng_l2cap_con_p con = NULL;
299
300 LIST_FOREACH(con, &l2cap->con_list, next)
301 if (bcmp(bdaddr, &con->remote, sizeof(con->remote)) == 0)
302 break;
303
304 return (con);
305} /* ng_l2cap_con_by_addr */
306
307/*
308 * Get connection by "handle"
309 */
310
311ng_l2cap_con_p
312ng_l2cap_con_by_handle(ng_l2cap_p l2cap, u_int16_t con_handle)
313{
314 ng_l2cap_con_p con = NULL;
315
316 LIST_FOREACH(con, &l2cap->con_list, next)
317 if (con->con_handle == con_handle)
318 break;
319
320 return (con);
321} /* ng_l2cap_con_by_handle */
322
323/*
324 * Allocate new L2CAP channel descriptor on "con" conection with "psm".
325 * Will link the channel to the l2cap node
326 */
327
328ng_l2cap_chan_p
329ng_l2cap_new_chan(ng_l2cap_p l2cap, ng_l2cap_con_p con, u_int16_t psm)
330{
331 ng_l2cap_chan_p ch = NULL;
332
fc025606
SW
333 ch = kmalloc(sizeof(*ch), M_NETGRAPH_L2CAP,
334 M_WAITOK | M_NULLOK | M_ZERO);
b06ebda0
MD
335 if (ch == NULL)
336 return (NULL);
337
338 ch->scid = ng_l2cap_get_cid(l2cap);
339
340 if (ch->scid != NG_L2CAP_NULL_CID) {
341 /* Initialize channel */
342 ch->psm = psm;
343 ch->con = con;
344 ch->state = NG_L2CAP_CLOSED;
345
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));
349
350 ch->omtu = NG_L2CAP_MTU_DEFAULT;
351 bcopy(ng_l2cap_default_flow(), &ch->oflow, sizeof(ch->oflow));
352
353 ch->flush_timo = NG_L2CAP_FLUSH_TIMO_DEFAULT;
354 ch->link_timo = NG_L2CAP_LINK_TIMO_DEFAULT;
355
356 LIST_INSERT_HEAD(&l2cap->chan_list, ch, next);
357
358 ng_l2cap_con_ref(con);
359 } else {
360 bzero(ch, sizeof(*ch));
fc025606 361 kfree(ch, M_NETGRAPH_L2CAP);
b06ebda0
MD
362 ch = NULL;
363 }
364
365 return (ch);
366} /* ng_l2cap_new_chan */
367
368/*
369 * Get channel by source (local) channel ID
370 */
371
372ng_l2cap_chan_p
373ng_l2cap_chan_by_scid(ng_l2cap_p l2cap, u_int16_t scid)
374{
375 ng_l2cap_chan_p ch = NULL;
376
377 LIST_FOREACH(ch, &l2cap->chan_list, next)
378 if (ch->scid == scid)
379 break;
380
381 return (ch);
382} /* ng_l2cap_chan_by_scid */
383
384/*
385 * Free channel descriptor.
386 */
387
388void
389ng_l2cap_free_chan(ng_l2cap_chan_p ch)
390{
391 ng_l2cap_cmd_p f = NULL, n = NULL;
392
393 f = TAILQ_FIRST(&ch->con->cmd_list);
394 while (f != NULL) {
395 n = TAILQ_NEXT(f, next);
396
397 if (f->ch == ch) {
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);
402 }
403
404 f = n;
405 }
406
407 LIST_REMOVE(ch, next);
408
409 ng_l2cap_con_unref(ch->con);
410
411 bzero(ch, sizeof(*ch));
fc025606 412 kfree(ch, M_NETGRAPH_L2CAP);
b06ebda0
MD
413} /* ng_l2cap_free_chan */
414
415/*
416 * Create new L2CAP command descriptor. WILL NOT add command to the queue.
417 */
418
419ng_l2cap_cmd_p
420ng_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)
422{
423 ng_l2cap_cmd_p cmd = NULL;
424
425 KASSERT((ch == NULL || ch->con == con),
426("%s: %s - invalid channel pointer!\n",
427 __func__, NG_NODE_NAME(con->l2cap->node)));
428
fc025606
SW
429 cmd = kmalloc(sizeof(*cmd), M_NETGRAPH_L2CAP,
430 M_WAITOK | M_NULLOK | M_ZERO);
b06ebda0
MD
431 if (cmd == NULL)
432 return (NULL);
433
434 cmd->con = con;
435 cmd->ch = ch;
436 cmd->ident = ident;
437 cmd->code = code;
438 cmd->token = token;
439 ng_callout_init(&cmd->timo);
440
441 return (cmd);
442} /* ng_l2cap_new_cmd */
443
444/*
445 * Get pending (i.e. initiated by local side) L2CAP command descriptor by ident
446 */
447
448ng_l2cap_cmd_p
449ng_l2cap_cmd_by_ident(ng_l2cap_con_p con, u_int8_t ident)
450{
451 ng_l2cap_cmd_p cmd = NULL;
452
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)));
458
459 break;
460 }
461 }
462
463 return (cmd);
464} /* ng_l2cap_cmd_by_ident */
465
466/*
467 * Set LP timeout
468 * XXX FIXME: check return code from ng_callout
469 */
470
471int
472ng_l2cap_lp_timeout(ng_l2cap_con_p con)
473{
474 if (con->flags & (NG_L2CAP_CON_LP_TIMO|NG_L2CAP_CON_AUTO_DISCON_TIMO))
475 panic(
476"%s: %s - invalid timeout, state=%d, flags=%#x\n",
477 __func__, NG_NODE_NAME(con->l2cap->node),
478 con->state, con->flags);
479
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,
484 con->con_handle);
485
486 return (0);
487} /* ng_l2cap_lp_timeout */
488
489/*
490 * Unset LP timeout
491 */
492
493int
494ng_l2cap_lp_untimeout(ng_l2cap_con_p con)
495{
496 if (!(con->flags & NG_L2CAP_CON_LP_TIMO))
497 panic(
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);
501
502 if (ng_uncallout(&con->con_timo, con->l2cap->node) == 0)
503 return (ETIMEDOUT);
504
505 con->flags &= ~NG_L2CAP_CON_LP_TIMO;
506
507 return (0);
508} /* ng_l2cap_lp_untimeout */
509
510/*
511 * Set L2CAP command timeout
512 * XXX FIXME: check return code from ng_callout
513 */
514
515int
516ng_l2cap_command_timeout(ng_l2cap_cmd_p cmd, int timo)
517{
518 int arg;
519
520 if (cmd->flags & NG_L2CAP_CMD_PENDING)
521 panic(
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);
525
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);
530
531 return (0);
532} /* ng_l2cap_command_timeout */
533
534/*
535 * Unset L2CAP command timeout
536 */
537
538int
539ng_l2cap_command_untimeout(ng_l2cap_cmd_p cmd)
540{
541 if (!(cmd->flags & NG_L2CAP_CMD_PENDING))
542 panic(
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);
546
547 if (ng_uncallout(&cmd->timo, cmd->con->l2cap->node) == 0)
548 return (ETIMEDOUT);
549
550 cmd->flags &= ~NG_L2CAP_CMD_PENDING;
551
552 return (0);
553} /* ng_l2cap_command_untimeout */
554
555/*
556 * Prepend "m"buf with "size" bytes
557 */
558
559struct mbuf *
560ng_l2cap_prepend(struct mbuf *m, int size)
561{
5a975a3d 562 M_PREPEND(m, size, MB_DONTWAIT);
b06ebda0
MD
563 if (m == NULL || (m->m_len < size && (m = m_pullup(m, size)) == NULL))
564 return (NULL);
565
566 return (m);
567} /* ng_l2cap_prepend */
568
569/*
570 * Default flow settings
571 */
572
573ng_l2cap_flow_p
574ng_l2cap_default_flow(void)
575{
576 static ng_l2cap_flow_t default_flow = {
577 /* flags */ 0x0,
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 */
584 };
585
586 return (&default_flow);
587} /* ng_l2cap_default_flow */
588
589/*
590 * Get next available channel ID
591 * XXX FIXME this is *UGLY* but will do for now
592 */
593
594static u_int16_t
595ng_l2cap_get_cid(ng_l2cap_p l2cap)
596{
597 u_int16_t cid = l2cap->cid + 1;
598
599 if (cid < NG_L2CAP_FIRST_CID)
600 cid = NG_L2CAP_FIRST_CID;
601
602 while (cid != l2cap->cid) {
603 if (ng_l2cap_chan_by_scid(l2cap, cid) == NULL) {
604 l2cap->cid = cid;
605
606 return (cid);
607 }
608
609 cid ++;
610 if (cid < NG_L2CAP_FIRST_CID)
611 cid = NG_L2CAP_FIRST_CID;
612 }
613
614 return (NG_L2CAP_NULL_CID);
615} /* ng_l2cap_get_cid */
616
617/*
618 * Get next available command ident
619 * XXX FIXME this is *UGLY* but will do for now
620 */
621
622u_int8_t
623ng_l2cap_get_ident(ng_l2cap_con_p con)
624{
625 u_int8_t ident = con->ident + 1;
626
627 if (ident < NG_L2CAP_FIRST_IDENT)
628 ident = NG_L2CAP_FIRST_IDENT;
629
630 while (ident != con->ident) {
631 if (ng_l2cap_cmd_by_ident(con, ident) == NULL) {
632 con->ident = ident;
633
634 return (ident);
635 }
636
637 ident ++;
638 if (ident < NG_L2CAP_FIRST_IDENT)
639 ident = NG_L2CAP_FIRST_IDENT;
640 }
641
642 return (NG_L2CAP_NULL_IDENT);
643} /* ng_l2cap_get_ident */
644