dma: better logic for guessing username
[dragonfly.git] / sys / netbt / l2cap_misc.c
blob63b768cdbbf5617d7555384c9cf0c57a4fc70bc6
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 $ */
5 /*-
6 * Copyright (c) 2005 Iain Hibbert.
7 * Copyright (c) 2006 Itronix Inc.
8 * All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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>
37 #include <sys/mbuf.h>
38 #include <sys/proc.h>
39 #include <sys/queue.h>
40 #include <sys/systm.h>
41 #include <net/if.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 = {
57 0, /* flags */
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 */
72 void
73 l2cap_init(void)
79 * Set Link Mode on channel
81 int
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;
113 int next_id;
115 if (link == NULL)
116 return ENETDOWN;
118 /* find next ID (0 is not allowed) */
119 next_id = link->hl_lastid + 1;
120 if (next_id > 0xff)
121 next_id = 1;
123 /* Ouroboros check */
124 req = TAILQ_FIRST(&link->hl_reqs);
125 if (req && req->lr_id == next_id)
126 return ENFILE;
128 req = pool_get(&l2cap_req_pool, M_NOWAIT);
129 if (req == NULL)
130 return ENOMEM;
132 req->lr_id = link->hl_lastid = next_id;
134 req->lr_code = code;
135 req->lr_chan = chan;
136 req->lr_link = link;
138 callout_init(&req->lr_rtx);
139 callout_reset(&req->lr_rtx, l2cap_response_timeout * hz,
140 l2cap_rtx, req);
142 TAILQ_INSERT_TAIL(&link->hl_reqs, req, lr_next);
144 return 0;
148 * Find a running request for this link
150 struct l2cap_req *
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)
157 return req;
160 return NULL;
164 * Halt and free a request
166 void
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))
173 return;
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?
186 void
187 l2cap_rtx(void *arg)
189 struct l2cap_req *req = arg;
190 struct l2cap_channel *chan;
192 chan = req->lr_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)
216 return EISCONN;
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);
223 cid++;
225 if (cid == L2CAP_LAST_CID)
226 return ENFILE;
228 prev = used; /* for insert after */
231 chan->lc_lcid = cid;
233 if (prev)
234 LIST_INSERT_AFTER(prev, chan, lc_ncid);
235 else
236 LIST_INSERT_HEAD(&l2cap_active_list, chan, lc_ncid);
238 return 0;
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)
251 return chan;
253 if (chan->lc_lcid > cid)
254 return NULL;
257 return NULL;