1*fbd53556Spooka /* $NetBSD: l2cap_misc.c,v 1.7 2009/09/13 18:45:11 pooka Exp $ */
2a5c89047Sgdamore
3a5c89047Sgdamore /*-
4a5c89047Sgdamore * Copyright (c) 2005 Iain Hibbert.
5a5c89047Sgdamore * Copyright (c) 2006 Itronix Inc.
6a5c89047Sgdamore * All rights reserved.
7a5c89047Sgdamore *
8a5c89047Sgdamore * Redistribution and use in source and binary forms, with or without
9a5c89047Sgdamore * modification, are permitted provided that the following conditions
10a5c89047Sgdamore * are met:
11a5c89047Sgdamore * 1. Redistributions of source code must retain the above copyright
12a5c89047Sgdamore * notice, this list of conditions and the following disclaimer.
13a5c89047Sgdamore * 2. Redistributions in binary form must reproduce the above copyright
14a5c89047Sgdamore * notice, this list of conditions and the following disclaimer in the
15a5c89047Sgdamore * documentation and/or other materials provided with the distribution.
16a5c89047Sgdamore * 3. The name of Itronix Inc. may not be used to endorse
17a5c89047Sgdamore * or promote products derived from this software without specific
18a5c89047Sgdamore * prior written permission.
19a5c89047Sgdamore *
20a5c89047Sgdamore * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
21a5c89047Sgdamore * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22a5c89047Sgdamore * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23a5c89047Sgdamore * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
24a5c89047Sgdamore * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25a5c89047Sgdamore * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26a5c89047Sgdamore * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27a5c89047Sgdamore * ON ANY THEORY OF LIABILITY, WHETHER IN
28a5c89047Sgdamore * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29a5c89047Sgdamore * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30a5c89047Sgdamore * POSSIBILITY OF SUCH DAMAGE.
31a5c89047Sgdamore */
32a5c89047Sgdamore
33a5c89047Sgdamore #include <sys/cdefs.h>
34*fbd53556Spooka __KERNEL_RCSID(0, "$NetBSD: l2cap_misc.c,v 1.7 2009/09/13 18:45:11 pooka Exp $");
35a5c89047Sgdamore
36a5c89047Sgdamore #include <sys/param.h>
37a5c89047Sgdamore #include <sys/kernel.h>
38a5c89047Sgdamore #include <sys/mbuf.h>
39a5c89047Sgdamore #include <sys/proc.h>
40a5c89047Sgdamore #include <sys/queue.h>
41a5c89047Sgdamore #include <sys/systm.h>
42a5c89047Sgdamore
43a5c89047Sgdamore #include <netbt/bluetooth.h>
44a5c89047Sgdamore #include <netbt/hci.h>
45a5c89047Sgdamore #include <netbt/l2cap.h>
46a5c89047Sgdamore
47a5c89047Sgdamore struct l2cap_channel_list
48a5c89047Sgdamore l2cap_active_list = LIST_HEAD_INITIALIZER(l2cap_active_list);
49a5c89047Sgdamore struct l2cap_channel_list
50a5c89047Sgdamore l2cap_listen_list = LIST_HEAD_INITIALIZER(l2cap_listen_list);
51a5c89047Sgdamore
52*fbd53556Spooka struct pool l2cap_req_pool, l2cap_pdu_pool;
53a5c89047Sgdamore
54a5c89047Sgdamore const l2cap_qos_t l2cap_default_qos = {
55a5c89047Sgdamore 0, /* flags */
56a5c89047Sgdamore L2CAP_QOS_BEST_EFFORT, /* service type */
57a5c89047Sgdamore 0x00000000, /* token rate */
58a5c89047Sgdamore 0x00000000, /* token bucket size */
59a5c89047Sgdamore 0x00000000, /* peak bandwidth */
60a5c89047Sgdamore 0xffffffff, /* latency */
61a5c89047Sgdamore 0xffffffff /* delay variation */
62a5c89047Sgdamore };
63a5c89047Sgdamore
64a5c89047Sgdamore /*
65a5c89047Sgdamore * L2CAP request timeouts
66a5c89047Sgdamore */
67a5c89047Sgdamore int l2cap_response_timeout = 30; /* seconds */
68a5c89047Sgdamore int l2cap_response_extended_timeout = 180; /* seconds */
69a5c89047Sgdamore
70*fbd53556Spooka void
l2cap_init(void)71*fbd53556Spooka l2cap_init(void)
72*fbd53556Spooka {
73*fbd53556Spooka
74*fbd53556Spooka pool_init(&l2cap_req_pool, sizeof(struct l2cap_req), 0, 0, 0,
75*fbd53556Spooka "l2cap_req", NULL, IPL_SOFTNET);
76*fbd53556Spooka pool_init(&l2cap_pdu_pool, sizeof(struct l2cap_pdu), 0, 0, 0,
77*fbd53556Spooka "l2cap_pdu", NULL, IPL_SOFTNET);
78*fbd53556Spooka }
79*fbd53556Spooka
80a5c89047Sgdamore /*
81f5db72e7Splunky * Set Link Mode on channel
82f5db72e7Splunky */
83f5db72e7Splunky int
l2cap_setmode(struct l2cap_channel * chan)84f5db72e7Splunky l2cap_setmode(struct l2cap_channel *chan)
85f5db72e7Splunky {
86f5db72e7Splunky
87f5db72e7Splunky KASSERT(chan != NULL);
88f5db72e7Splunky KASSERT(chan->lc_link != NULL);
89f5db72e7Splunky
90f5db72e7Splunky DPRINTF("CID #%d, auth %s, encrypt %s, secure %s\n", chan->lc_lcid,
91f5db72e7Splunky (chan->lc_mode & L2CAP_LM_AUTH ? "yes" : "no"),
92f5db72e7Splunky (chan->lc_mode & L2CAP_LM_ENCRYPT ? "yes" : "no"),
93f5db72e7Splunky (chan->lc_mode & L2CAP_LM_SECURE ? "yes" : "no"));
94f5db72e7Splunky
95f5db72e7Splunky if (chan->lc_mode & L2CAP_LM_AUTH)
96f5db72e7Splunky chan->lc_link->hl_flags |= HCI_LINK_AUTH_REQ;
97f5db72e7Splunky
98f5db72e7Splunky if (chan->lc_mode & L2CAP_LM_ENCRYPT)
99f5db72e7Splunky chan->lc_link->hl_flags |= HCI_LINK_ENCRYPT_REQ;
100f5db72e7Splunky
101f5db72e7Splunky if (chan->lc_mode & L2CAP_LM_SECURE)
102f5db72e7Splunky chan->lc_link->hl_flags |= HCI_LINK_SECURE_REQ;
103f5db72e7Splunky
104f5db72e7Splunky return hci_acl_setmode(chan->lc_link);
105f5db72e7Splunky }
106f5db72e7Splunky
107f5db72e7Splunky /*
108a5c89047Sgdamore * Allocate a new Request structure & ID and set the timer going
109a5c89047Sgdamore */
110a5c89047Sgdamore int
l2cap_request_alloc(struct l2cap_channel * chan,uint8_t code)111a5c89047Sgdamore l2cap_request_alloc(struct l2cap_channel *chan, uint8_t code)
112a5c89047Sgdamore {
113a5c89047Sgdamore struct hci_link *link = chan->lc_link;
114a5c89047Sgdamore struct l2cap_req *req;
115a5c89047Sgdamore int next_id;
116a5c89047Sgdamore
117a5c89047Sgdamore if (link == NULL)
118a5c89047Sgdamore return ENETDOWN;
119a5c89047Sgdamore
120a5c89047Sgdamore /* find next ID (0 is not allowed) */
121a5c89047Sgdamore next_id = link->hl_lastid + 1;
122a5c89047Sgdamore if (next_id > 0xff)
123a5c89047Sgdamore next_id = 1;
124a5c89047Sgdamore
125a5c89047Sgdamore /* Ouroboros check */
126a5c89047Sgdamore req = TAILQ_FIRST(&link->hl_reqs);
127a5c89047Sgdamore if (req && req->lr_id == next_id)
128a5c89047Sgdamore return ENFILE;
129a5c89047Sgdamore
130a5c89047Sgdamore req = pool_get(&l2cap_req_pool, PR_NOWAIT);
131a5c89047Sgdamore if (req == NULL)
132a5c89047Sgdamore return ENOMEM;
133a5c89047Sgdamore
134a5c89047Sgdamore req->lr_id = link->hl_lastid = next_id;
135a5c89047Sgdamore
136a5c89047Sgdamore req->lr_code = code;
137a5c89047Sgdamore req->lr_chan = chan;
138a5c89047Sgdamore req->lr_link = link;
139a5c89047Sgdamore
14088ab7da9Sad callout_init(&req->lr_rtx, 0);
141644e69cdSplunky callout_setfunc(&req->lr_rtx, l2cap_rtx, req);
142644e69cdSplunky callout_schedule(&req->lr_rtx, l2cap_response_timeout * hz);
143a5c89047Sgdamore
144a5c89047Sgdamore TAILQ_INSERT_TAIL(&link->hl_reqs, req, lr_next);
145a5c89047Sgdamore
146a5c89047Sgdamore return 0;
147a5c89047Sgdamore }
148a5c89047Sgdamore
149a5c89047Sgdamore /*
150a5c89047Sgdamore * Find a running request for this link
151a5c89047Sgdamore */
152a5c89047Sgdamore struct l2cap_req *
l2cap_request_lookup(struct hci_link * link,uint8_t id)153a5c89047Sgdamore l2cap_request_lookup(struct hci_link *link, uint8_t id)
154a5c89047Sgdamore {
155a5c89047Sgdamore struct l2cap_req *req;
156a5c89047Sgdamore
157a5c89047Sgdamore TAILQ_FOREACH(req, &link->hl_reqs, lr_next) {
158a5c89047Sgdamore if (req->lr_id == id)
159a5c89047Sgdamore return req;
160a5c89047Sgdamore }
161a5c89047Sgdamore
162a5c89047Sgdamore return NULL;
163a5c89047Sgdamore }
164a5c89047Sgdamore
165a5c89047Sgdamore /*
166a5c89047Sgdamore * Halt and free a request
167a5c89047Sgdamore */
168a5c89047Sgdamore void
l2cap_request_free(struct l2cap_req * req)169a5c89047Sgdamore l2cap_request_free(struct l2cap_req *req)
170a5c89047Sgdamore {
171a5c89047Sgdamore struct hci_link *link = req->lr_link;
172a5c89047Sgdamore
173a5c89047Sgdamore callout_stop(&req->lr_rtx);
174a5c89047Sgdamore if (callout_invoking(&req->lr_rtx))
175a5c89047Sgdamore return;
176a5c89047Sgdamore
177644e69cdSplunky callout_destroy(&req->lr_rtx);
178644e69cdSplunky
179a5c89047Sgdamore TAILQ_REMOVE(&link->hl_reqs, req, lr_next);
180a5c89047Sgdamore pool_put(&l2cap_req_pool, req);
181a5c89047Sgdamore }
182a5c89047Sgdamore
183a5c89047Sgdamore /*
184a5c89047Sgdamore * Response Timeout eXpired
185a5c89047Sgdamore *
186a5c89047Sgdamore * No response to our request, so deal with it as best we can.
187a5c89047Sgdamore *
188a5c89047Sgdamore * XXX should try again at least with ertx?
189a5c89047Sgdamore */
190a5c89047Sgdamore void
l2cap_rtx(void * arg)191a5c89047Sgdamore l2cap_rtx(void *arg)
192a5c89047Sgdamore {
193a5c89047Sgdamore struct l2cap_req *req = arg;
194a5c89047Sgdamore struct l2cap_channel *chan;
195a5c89047Sgdamore
19615e29e98Sad mutex_enter(bt_lock);
197a5c89047Sgdamore callout_ack(&req->lr_rtx);
198a5c89047Sgdamore
199a5c89047Sgdamore chan = req->lr_chan;
200a5c89047Sgdamore l2cap_request_free(req);
201a5c89047Sgdamore
202a5c89047Sgdamore DPRINTF("cid %d, ident %d\n", (chan ? chan->lc_lcid : 0), req->lr_id);
203a5c89047Sgdamore
204a5c89047Sgdamore if (chan && chan->lc_state != L2CAP_CLOSED)
205a5c89047Sgdamore l2cap_close(chan, ETIMEDOUT);
206a5c89047Sgdamore
20715e29e98Sad mutex_exit(bt_lock);
208a5c89047Sgdamore }
209a5c89047Sgdamore
210a5c89047Sgdamore /*
211a5c89047Sgdamore * Allocate next available CID to channel. We keep a single
212a5c89047Sgdamore * ordered list of channels, so find the first gap.
213a5c89047Sgdamore *
214a5c89047Sgdamore * If this turns out to be not enough (!), could use a
215a5c89047Sgdamore * list per HCI unit..
216a5c89047Sgdamore */
217a5c89047Sgdamore int
l2cap_cid_alloc(struct l2cap_channel * chan)218a5c89047Sgdamore l2cap_cid_alloc(struct l2cap_channel *chan)
219a5c89047Sgdamore {
220a5c89047Sgdamore struct l2cap_channel *used, *prev = NULL;
221a5c89047Sgdamore uint16_t cid = L2CAP_FIRST_CID;
222a5c89047Sgdamore
223a5c89047Sgdamore if (chan->lc_lcid != L2CAP_NULL_CID || chan->lc_state != L2CAP_CLOSED)
224a5c89047Sgdamore return EISCONN;
225a5c89047Sgdamore
226a5c89047Sgdamore LIST_FOREACH(used, &l2cap_active_list, lc_ncid) {
227a5c89047Sgdamore if (used->lc_lcid > cid)
228a5c89047Sgdamore break; /* found our gap */
229a5c89047Sgdamore
230a5c89047Sgdamore KASSERT(used->lc_lcid == cid);
231a5c89047Sgdamore cid++;
232a5c89047Sgdamore
233a5c89047Sgdamore if (cid == L2CAP_LAST_CID)
234a5c89047Sgdamore return ENFILE;
235a5c89047Sgdamore
236a5c89047Sgdamore prev = used; /* for insert after */
237a5c89047Sgdamore }
238a5c89047Sgdamore
239a5c89047Sgdamore chan->lc_lcid = cid;
240a5c89047Sgdamore
241a5c89047Sgdamore if (prev)
242a5c89047Sgdamore LIST_INSERT_AFTER(prev, chan, lc_ncid);
243a5c89047Sgdamore else
244a5c89047Sgdamore LIST_INSERT_HEAD(&l2cap_active_list, chan, lc_ncid);
245a5c89047Sgdamore
246a5c89047Sgdamore return 0;
247a5c89047Sgdamore }
248a5c89047Sgdamore
249a5c89047Sgdamore /*
250a5c89047Sgdamore * Find channel with CID
251a5c89047Sgdamore */
252a5c89047Sgdamore struct l2cap_channel *
l2cap_cid_lookup(uint16_t cid)253a5c89047Sgdamore l2cap_cid_lookup(uint16_t cid)
254a5c89047Sgdamore {
255a5c89047Sgdamore struct l2cap_channel *chan;
256a5c89047Sgdamore
257a5c89047Sgdamore LIST_FOREACH(chan, &l2cap_active_list, lc_ncid) {
258a5c89047Sgdamore if (chan->lc_lcid == cid)
259a5c89047Sgdamore return chan;
260a5c89047Sgdamore
261a5c89047Sgdamore if (chan->lc_lcid > cid)
262a5c89047Sgdamore return NULL;
263a5c89047Sgdamore }
264a5c89047Sgdamore
265a5c89047Sgdamore return NULL;
266a5c89047Sgdamore }
267