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 $ */ 4 5 /*- 6 * Copyright (c) 2005 Iain Hibbert. 7 * Copyright (c) 2006 Itronix Inc. 8 * All rights reserved. 9 * 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. 21 * 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. 33 */ 34 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> 43 44 #include <netbt/bluetooth.h> 45 #include <netbt/hci.h> 46 #include <netbt/l2cap.h> 47 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); 52 53 vm_zone_t l2cap_req_pool; 54 vm_zone_t l2cap_pdu_pool; 55 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 */ 64 }; 65 66 /* 67 * L2CAP request timeouts 68 */ 69 int l2cap_response_timeout = 30; /* seconds */ 70 int l2cap_response_extended_timeout = 180; /* seconds */ 71 72 void 73 l2cap_init(void) 74 { 75 ;; 76 } 77 78 /* 79 * Set Link Mode on channel 80 */ 81 int 82 l2cap_setmode(struct l2cap_channel *chan) 83 { 84 KKASSERT(chan != NULL); 85 KKASSERT(chan->lc_link != NULL); 86 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")); 92 93 if (chan->lc_mode & L2CAP_LM_AUTH) 94 chan->lc_link->hl_flags |= HCI_LINK_AUTH_REQ; 95 96 if (chan->lc_mode & L2CAP_LM_ENCRYPT) 97 chan->lc_link->hl_flags |= HCI_LINK_ENCRYPT_REQ; 98 99 if (chan->lc_mode & L2CAP_LM_SECURE) 100 chan->lc_link->hl_flags |= HCI_LINK_SECURE_REQ; 101 102 return hci_acl_setmode(chan->lc_link); 103 } 104 105 /* 106 * Allocate a new Request structure & ID and set the timer going 107 */ 108 int 109 l2cap_request_alloc(struct l2cap_channel *chan, uint8_t code) 110 { 111 struct hci_link *link = chan->lc_link; 112 struct l2cap_req *req; 113 int next_id; 114 115 if (link == NULL) 116 return ENETDOWN; 117 118 /* find next ID (0 is not allowed) */ 119 next_id = link->hl_lastid + 1; 120 if (next_id > 0xff) 121 next_id = 1; 122 123 /* Ouroboros check */ 124 req = TAILQ_FIRST(&link->hl_reqs); 125 if (req && req->lr_id == next_id) 126 return ENFILE; 127 128 req = pool_get(&l2cap_req_pool, M_NOWAIT); 129 if (req == NULL) 130 return ENOMEM; 131 132 req->lr_id = link->hl_lastid = next_id; 133 134 req->lr_code = code; 135 req->lr_chan = chan; 136 req->lr_link = link; 137 138 callout_init(&req->lr_rtx); 139 callout_reset(&req->lr_rtx, l2cap_response_timeout * hz, 140 l2cap_rtx, req); 141 142 TAILQ_INSERT_TAIL(&link->hl_reqs, req, lr_next); 143 144 return 0; 145 } 146 147 /* 148 * Find a running request for this link 149 */ 150 struct l2cap_req * 151 l2cap_request_lookup(struct hci_link *link, uint8_t id) 152 { 153 struct l2cap_req *req; 154 155 TAILQ_FOREACH(req, &link->hl_reqs, lr_next) { 156 if (req->lr_id == id) 157 return req; 158 } 159 160 return NULL; 161 } 162 163 /* 164 * Halt and free a request 165 */ 166 void 167 l2cap_request_free(struct l2cap_req *req) 168 { 169 struct hci_link *link = req->lr_link; 170 171 callout_stop(&req->lr_rtx); 172 if (callout_active(&req->lr_rtx)) 173 return; 174 175 TAILQ_REMOVE(&link->hl_reqs, req, lr_next); 176 pool_put(&l2cap_req_pool, req); 177 } 178 179 /* 180 * Response Timeout eXpired 181 * 182 * No response to our request, so deal with it as best we can. 183 * 184 * XXX should try again at least with ertx? 185 */ 186 void 187 l2cap_rtx(void *arg) 188 { 189 struct l2cap_req *req = arg; 190 struct l2cap_channel *chan; 191 192 chan = req->lr_chan; 193 l2cap_request_free(req); 194 195 DPRINTF("cid %d, ident %d\n", (chan ? chan->lc_lcid : 0), req->lr_id); 196 197 if (chan && chan->lc_state != L2CAP_CLOSED) 198 l2cap_close(chan, ETIMEDOUT); 199 200 } 201 202 /* 203 * Allocate next available CID to channel. We keep a single 204 * ordered list of channels, so find the first gap. 205 * 206 * If this turns out to be not enough (!), could use a 207 * list per HCI unit.. 208 */ 209 int 210 l2cap_cid_alloc(struct l2cap_channel *chan) 211 { 212 struct l2cap_channel *used, *prev = NULL; 213 uint16_t cid = L2CAP_FIRST_CID; 214 215 if (chan->lc_lcid != L2CAP_NULL_CID || chan->lc_state != L2CAP_CLOSED) 216 return EISCONN; 217 218 LIST_FOREACH(used, &l2cap_active_list, lc_ncid) { 219 if (used->lc_lcid > cid) 220 break; /* found our gap */ 221 222 KKASSERT(used->lc_lcid == cid); 223 cid++; 224 225 if (cid == L2CAP_LAST_CID) 226 return ENFILE; 227 228 prev = used; /* for insert after */ 229 } 230 231 chan->lc_lcid = cid; 232 233 if (prev) 234 LIST_INSERT_AFTER(prev, chan, lc_ncid); 235 else 236 LIST_INSERT_HEAD(&l2cap_active_list, chan, lc_ncid); 237 238 return 0; 239 } 240 241 /* 242 * Find channel with CID 243 */ 244 struct l2cap_channel * 245 l2cap_cid_lookup(uint16_t cid) 246 { 247 struct l2cap_channel *chan; 248 249 LIST_FOREACH(chan, &l2cap_active_list, lc_ncid) { 250 if (chan->lc_lcid == cid) 251 return chan; 252 253 if (chan->lc_lcid > cid) 254 return NULL; 255 } 256 257 return NULL; 258 } 259