1 /* $DragonFly: src/sys/netbt/l2cap_misc.c,v 1.2 2008/03/18 13:41:42 hasso Exp $ */
2 /* $OpenBSD: src/sys/netbt/l2cap_misc.c,v 1.3 2008/02/24 21:34:48 uwe Exp $ */
3 /* $NetBSD: l2cap_misc.c,v 1.5 2007/11/03 17:20:17 plunky Exp $ */
6 * Copyright (c) 2005 Iain Hibbert.
7 * Copyright (c) 2006 Itronix Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. The name of Itronix Inc. may not be used to endorse
19 * or promote products derived from this software without specific
20 * prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
26 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29 * ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
35 #include <sys/param.h>
36 #include <sys/kernel.h>
39 #include <sys/queue.h>
40 #include <sys/systm.h>
42 #include <net/pf/pfvar.h>
44 #include <netbt/bluetooth.h>
45 #include <netbt/hci.h>
46 #include <netbt/l2cap.h>
48 struct l2cap_channel_list
49 l2cap_active_list
= LIST_HEAD_INITIALIZER(l2cap_active_list
);
50 struct l2cap_channel_list
51 l2cap_listen_list
= LIST_HEAD_INITIALIZER(l2cap_listen_list
);
53 vm_zone_t l2cap_req_pool
;
54 vm_zone_t l2cap_pdu_pool
;
56 const l2cap_qos_t l2cap_default_qos
= {
58 L2CAP_QOS_BEST_EFFORT
, /* service type */
59 0x00000000, /* token rate */
60 0x00000000, /* token bucket size */
61 0x00000000, /* peak bandwidth */
62 0xffffffff, /* latency */
63 0xffffffff /* delay variation */
67 * L2CAP request timeouts
69 int l2cap_response_timeout
= 30; /* seconds */
70 int l2cap_response_extended_timeout
= 180; /* seconds */
79 * Set Link Mode on channel
82 l2cap_setmode(struct l2cap_channel
*chan
)
84 KKASSERT(chan
!= NULL
);
85 KKASSERT(chan
->lc_link
!= NULL
);
87 DPRINTF("(%s) CID #%d, auth %s, encrypt %s, secure %s\n",
88 device_get_nameunit(chan
->lc_link
->hl_unit
->hci_dev
), chan
->lc_lcid
,
89 (chan
->lc_mode
& L2CAP_LM_AUTH
? "yes" : "no"),
90 (chan
->lc_mode
& L2CAP_LM_ENCRYPT
? "yes" : "no"),
91 (chan
->lc_mode
& L2CAP_LM_SECURE
? "yes" : "no"));
93 if (chan
->lc_mode
& L2CAP_LM_AUTH
)
94 chan
->lc_link
->hl_flags
|= HCI_LINK_AUTH_REQ
;
96 if (chan
->lc_mode
& L2CAP_LM_ENCRYPT
)
97 chan
->lc_link
->hl_flags
|= HCI_LINK_ENCRYPT_REQ
;
99 if (chan
->lc_mode
& L2CAP_LM_SECURE
)
100 chan
->lc_link
->hl_flags
|= HCI_LINK_SECURE_REQ
;
102 return hci_acl_setmode(chan
->lc_link
);
106 * Allocate a new Request structure & ID and set the timer going
109 l2cap_request_alloc(struct l2cap_channel
*chan
, uint8_t code
)
111 struct hci_link
*link
= chan
->lc_link
;
112 struct l2cap_req
*req
;
118 /* find next ID (0 is not allowed) */
119 next_id
= link
->hl_lastid
+ 1;
123 /* Ouroboros check */
124 req
= TAILQ_FIRST(&link
->hl_reqs
);
125 if (req
&& req
->lr_id
== next_id
)
128 req
= pool_get(&l2cap_req_pool
, M_NOWAIT
);
132 req
->lr_id
= link
->hl_lastid
= next_id
;
138 callout_init(&req
->lr_rtx
);
139 callout_reset(&req
->lr_rtx
, l2cap_response_timeout
* hz
,
142 TAILQ_INSERT_TAIL(&link
->hl_reqs
, req
, lr_next
);
148 * Find a running request for this link
151 l2cap_request_lookup(struct hci_link
*link
, uint8_t id
)
153 struct l2cap_req
*req
;
155 TAILQ_FOREACH(req
, &link
->hl_reqs
, lr_next
) {
156 if (req
->lr_id
== id
)
164 * Halt and free a request
167 l2cap_request_free(struct l2cap_req
*req
)
169 struct hci_link
*link
= req
->lr_link
;
171 callout_stop(&req
->lr_rtx
);
172 if (callout_active(&req
->lr_rtx
))
175 TAILQ_REMOVE(&link
->hl_reqs
, req
, lr_next
);
176 pool_put(&l2cap_req_pool
, req
);
180 * Response Timeout eXpired
182 * No response to our request, so deal with it as best we can.
184 * XXX should try again at least with ertx?
189 struct l2cap_req
*req
= arg
;
190 struct l2cap_channel
*chan
;
193 l2cap_request_free(req
);
195 DPRINTF("cid %d, ident %d\n", (chan
? chan
->lc_lcid
: 0), req
->lr_id
);
197 if (chan
&& chan
->lc_state
!= L2CAP_CLOSED
)
198 l2cap_close(chan
, ETIMEDOUT
);
203 * Allocate next available CID to channel. We keep a single
204 * ordered list of channels, so find the first gap.
206 * If this turns out to be not enough (!), could use a
207 * list per HCI unit..
210 l2cap_cid_alloc(struct l2cap_channel
*chan
)
212 struct l2cap_channel
*used
, *prev
= NULL
;
213 uint16_t cid
= L2CAP_FIRST_CID
;
215 if (chan
->lc_lcid
!= L2CAP_NULL_CID
|| chan
->lc_state
!= L2CAP_CLOSED
)
218 LIST_FOREACH(used
, &l2cap_active_list
, lc_ncid
) {
219 if (used
->lc_lcid
> cid
)
220 break; /* found our gap */
222 KKASSERT(used
->lc_lcid
== cid
);
225 if (cid
== L2CAP_LAST_CID
)
228 prev
= used
; /* for insert after */
234 LIST_INSERT_AFTER(prev
, chan
, lc_ncid
);
236 LIST_INSERT_HEAD(&l2cap_active_list
, chan
, lc_ncid
);
242 * Find channel with CID
244 struct l2cap_channel
*
245 l2cap_cid_lookup(uint16_t cid
)
247 struct l2cap_channel
*chan
;
249 LIST_FOREACH(chan
, &l2cap_active_list
, lc_ncid
) {
250 if (chan
->lc_lcid
== cid
)
253 if (chan
->lc_lcid
> cid
)