Remove WARNS?=6 from Makefiles where it is already in ../Makefile.inc.
[dragonfly.git] / sys / netbt / l2cap_signal.c
blob4e02721115e96c6d66951d1207458e923fbff80b
1 /* $DragonFly: src/sys/netbt/l2cap_signal.c,v 1.2 2008/03/18 13:41:42 hasso Exp $ */
2 /* $OpenBSD: src/sys/netbt/l2cap_signal.c,v 1.3 2008/02/24 21:34:48 uwe Exp $ */
3 /* $NetBSD: l2cap_signal.c,v 1.9 2007/11/10 23:12:23 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 <stdarg.h>
37 #include <sys/param.h>
38 #include <sys/kernel.h>
39 #include <sys/mbuf.h>
40 #include <sys/proc.h>
41 #include <sys/queue.h>
42 #include <sys/systm.h>
43 #include <sys/endian.h>
45 #include <netbt/bluetooth.h>
46 #include <netbt/hci.h>
47 #include <netbt/l2cap.h>
49 /*******************************************************************************
51 * L2CAP Signal processing
54 static void l2cap_recv_command_rej(struct mbuf *, struct hci_link *);
55 static void l2cap_recv_connect_req(struct mbuf *, struct hci_link *);
56 static void l2cap_recv_connect_rsp(struct mbuf *, struct hci_link *);
57 static void l2cap_recv_config_req(struct mbuf *, struct hci_link *);
58 static void l2cap_recv_config_rsp(struct mbuf *, struct hci_link *);
59 static void l2cap_recv_disconnect_req(struct mbuf *, struct hci_link *);
60 static void l2cap_recv_disconnect_rsp(struct mbuf *, struct hci_link *);
61 static void l2cap_recv_info_req(struct mbuf *, struct hci_link *);
62 static int l2cap_send_signal(struct hci_link *, uint8_t, uint8_t, uint16_t, void *);
63 static int l2cap_send_command_rej(struct hci_link *, uint8_t, uint16_t, ...);
66 * process incoming signal packets (CID 0x0001). Can contain multiple
67 * requests/responses.
69 void
70 l2cap_recv_signal(struct mbuf *m, struct hci_link *link)
72 l2cap_cmd_hdr_t cmd;
74 for(;;) {
75 if (m->m_pkthdr.len == 0)
76 goto finish;
78 if (m->m_pkthdr.len < sizeof(cmd))
79 goto reject;
81 m_copydata(m, 0, sizeof(cmd), (caddr_t)&cmd);
82 cmd.length = letoh16(cmd.length);
84 if (m->m_pkthdr.len < sizeof(cmd) + cmd.length)
85 goto reject;
87 DPRINTFN(2, "(%s) code %d, ident %d, len %d\n",
88 device_get_nameunit(link->hl_unit->hci_dev),
89 cmd.code, cmd.ident, cmd.length);
91 switch (cmd.code) {
92 case L2CAP_COMMAND_REJ:
93 if (cmd.length > sizeof(l2cap_cmd_rej_cp))
94 goto finish;
96 l2cap_recv_command_rej(m, link);
97 break;
99 case L2CAP_CONNECT_REQ:
100 if (cmd.length != sizeof(l2cap_con_req_cp))
101 goto reject;
103 l2cap_recv_connect_req(m, link);
104 break;
106 case L2CAP_CONNECT_RSP:
107 if (cmd.length != sizeof(l2cap_con_rsp_cp))
108 goto finish;
110 l2cap_recv_connect_rsp(m, link);
111 break;
113 case L2CAP_CONFIG_REQ:
114 l2cap_recv_config_req(m, link);
115 break;
117 case L2CAP_CONFIG_RSP:
118 l2cap_recv_config_rsp(m, link);
119 break;
121 case L2CAP_DISCONNECT_REQ:
122 if (cmd.length != sizeof(l2cap_discon_req_cp))
123 goto reject;
125 l2cap_recv_disconnect_req(m, link);
126 break;
128 case L2CAP_DISCONNECT_RSP:
129 if (cmd.length != sizeof(l2cap_discon_rsp_cp))
130 goto finish;
132 l2cap_recv_disconnect_rsp(m, link);
133 break;
135 case L2CAP_ECHO_REQ:
136 m_adj(m, sizeof(cmd) + cmd.length);
137 l2cap_send_signal(link, L2CAP_ECHO_RSP, cmd.ident,
138 0, NULL);
139 break;
141 case L2CAP_ECHO_RSP:
142 m_adj(m, sizeof(cmd) + cmd.length);
143 break;
145 case L2CAP_INFO_REQ:
146 if (cmd.length != sizeof(l2cap_info_req_cp))
147 goto reject;
149 l2cap_recv_info_req(m, link);
150 break;
152 case L2CAP_INFO_RSP:
153 m_adj(m, sizeof(cmd) + cmd.length);
154 break;
156 default:
157 goto reject;
161 #ifdef DIAGNOSTIC
162 panic("impossible!");
163 #endif
165 reject:
166 l2cap_send_command_rej(link, cmd.ident, L2CAP_REJ_NOT_UNDERSTOOD);
167 finish:
168 m_freem(m);
172 * Process Received Command Reject. For now we dont try to recover gracefully
173 * from this, it probably means that the link is garbled or the other end is
174 * insufficiently capable of handling normal traffic. (not *my* fault, no way!)
176 static void
177 l2cap_recv_command_rej(struct mbuf *m, struct hci_link *link)
179 struct l2cap_req *req;
180 struct l2cap_channel *chan;
181 l2cap_cmd_hdr_t cmd;
182 l2cap_cmd_rej_cp cp;
184 m_copydata(m, 0, sizeof(cmd), (caddr_t)&cmd);
185 m_adj(m, sizeof(cmd));
187 cmd.length = letoh16(cmd.length);
189 m_copydata(m, 0, cmd.length, (caddr_t)&cp);
190 m_adj(m, cmd.length);
192 req = l2cap_request_lookup(link, cmd.ident);
193 if (req == NULL)
194 return;
196 switch (letoh16(cp.reason)) {
197 case L2CAP_REJ_NOT_UNDERSTOOD:
199 * I dont know what to do, just move up the timeout
201 callout_reset(&req->lr_rtx,0,l2cap_rtx,req);
202 break;
204 case L2CAP_REJ_MTU_EXCEEDED:
206 * I didnt send any commands over L2CAP_MTU_MINIMUM size, but..
208 * XXX maybe we should resend this, instead?
210 link->hl_mtu = letoh16(cp.data[0]);
211 callout_reset(&req->lr_rtx,0,l2cap_rtx,req);
212 break;
214 case L2CAP_REJ_INVALID_CID:
216 * Well, if they dont have such a channel then our channel is
217 * most likely closed. Make it so.
219 chan = req->lr_chan;
220 l2cap_request_free(req);
221 if (chan != NULL && chan->lc_state != L2CAP_CLOSED)
222 l2cap_close(chan, ECONNABORTED);
224 break;
226 default:
227 UNKNOWN(letoh16(cp.reason));
228 break;
233 * Process Received Connect Request. Find listening channel matching
234 * psm & addr and ask upper layer for a new channel.
236 static void
237 l2cap_recv_connect_req(struct mbuf *m, struct hci_link *link)
239 struct sockaddr_bt laddr, raddr;
240 struct l2cap_channel *chan, *new;
241 l2cap_cmd_hdr_t cmd;
242 l2cap_con_req_cp cp;
243 int err;
245 /* extract cmd */
246 m_copydata(m, 0, sizeof(cmd), (caddr_t)&cmd);
247 m_adj(m, sizeof(cmd));
249 /* extract request */
250 m_copydata(m, 0, sizeof(cp), (caddr_t)&cp);
251 m_adj(m, sizeof(cp));
253 cp.scid = letoh16(cp.scid);
254 cp.psm = letoh16(cp.psm);
256 memset(&laddr, 0, sizeof(struct sockaddr_bt));
257 laddr.bt_len = sizeof(struct sockaddr_bt);
258 laddr.bt_family = AF_BLUETOOTH;
259 laddr.bt_psm = cp.psm;
260 bdaddr_copy(&laddr.bt_bdaddr, &link->hl_unit->hci_bdaddr);
262 memset(&raddr, 0, sizeof(struct sockaddr_bt));
263 raddr.bt_len = sizeof(struct sockaddr_bt);
264 raddr.bt_family = AF_BLUETOOTH;
265 raddr.bt_psm = cp.psm;
266 bdaddr_copy(&raddr.bt_bdaddr, &link->hl_bdaddr);
268 LIST_FOREACH(chan, &l2cap_listen_list, lc_ncid) {
269 if (chan->lc_laddr.bt_psm != laddr.bt_psm
270 && chan->lc_laddr.bt_psm != L2CAP_PSM_ANY)
271 continue;
273 if (!bdaddr_same(&laddr.bt_bdaddr, &chan->lc_laddr.bt_bdaddr)
274 && bdaddr_any(&chan->lc_laddr.bt_bdaddr) == 0)
275 continue;
277 new= (*chan->lc_proto->newconn)(chan->lc_upper, &laddr, &raddr);
278 if (new == NULL)
279 continue;
281 err = l2cap_cid_alloc(new);
282 if (err) {
283 l2cap_send_connect_rsp(link, cmd.ident,
284 0, cp.scid,
285 L2CAP_NO_RESOURCES);
287 (*new->lc_proto->disconnected)(new->lc_upper, err);
288 return;
291 new->lc_link = hci_acl_open(link->hl_unit, &link->hl_bdaddr);
292 KKASSERT(new->lc_link == link);
294 new->lc_rcid = cp.scid;
295 new->lc_ident = cmd.ident;
297 memcpy(&new->lc_laddr, &laddr, sizeof(struct sockaddr_bt));
298 memcpy(&new->lc_raddr, &raddr, sizeof(struct sockaddr_bt));
300 new->lc_mode = chan->lc_mode;
302 err = l2cap_setmode(new);
303 if (err == EINPROGRESS) {
304 new->lc_state = L2CAP_WAIT_SEND_CONNECT_RSP;
305 (*new->lc_proto->connecting)(new->lc_upper);
306 return;
308 if (err) {
309 new->lc_state = L2CAP_CLOSED;
310 hci_acl_close(link, err);
311 new->lc_link = NULL;
313 l2cap_send_connect_rsp(link, cmd.ident,
314 0, cp.scid,
315 L2CAP_NO_RESOURCES);
317 (*new->lc_proto->disconnected)(new->lc_upper, err);
318 return;
321 err = l2cap_send_connect_rsp(link, cmd.ident,
322 new->lc_lcid, new->lc_rcid,
323 L2CAP_SUCCESS);
324 if (err) {
325 l2cap_close(new, err);
326 return;
329 new->lc_state = L2CAP_WAIT_CONFIG;
330 new->lc_flags |= (L2CAP_WAIT_CONFIG_REQ | L2CAP_WAIT_CONFIG_RSP);
331 err = l2cap_send_config_req(new);
332 if (err)
333 l2cap_close(new, err);
335 return;
338 l2cap_send_connect_rsp(link, cmd.ident,
339 0, cp.scid,
340 L2CAP_PSM_NOT_SUPPORTED);
344 * Process Received Connect Response.
346 static void
347 l2cap_recv_connect_rsp(struct mbuf *m, struct hci_link *link)
349 l2cap_cmd_hdr_t cmd;
350 l2cap_con_rsp_cp cp;
351 struct l2cap_req *req;
352 struct l2cap_channel *chan;
354 m_copydata(m, 0, sizeof(cmd), (caddr_t)&cmd);
355 m_adj(m, sizeof(cmd));
357 m_copydata(m, 0, sizeof(cp), (caddr_t)&cp);
358 m_adj(m, sizeof(cp));
360 cp.scid = letoh16(cp.scid);
361 cp.dcid = letoh16(cp.dcid);
362 cp.result = letoh16(cp.result);
364 req = l2cap_request_lookup(link, cmd.ident);
365 if (req == NULL || req->lr_code != L2CAP_CONNECT_REQ)
366 return;
368 chan = req->lr_chan;
369 if (chan != NULL && chan->lc_lcid != cp.scid)
370 return;
372 if (chan == NULL || chan->lc_state != L2CAP_WAIT_RECV_CONNECT_RSP) {
373 l2cap_request_free(req);
374 return;
377 switch (cp.result) {
378 case L2CAP_SUCCESS:
380 * Ok, at this point we have a connection to the other party. We
381 * could indicate upstream that we are ready for business and
382 * wait for a "Configure Channel Request" but I'm not so sure
383 * that is required in our case - we will proceed directly to
384 * sending our config request. We set two state bits because in
385 * the config state we are waiting for requests and responses.
387 l2cap_request_free(req);
388 chan->lc_rcid = cp.dcid;
389 chan->lc_state = L2CAP_WAIT_CONFIG;
390 chan->lc_flags |= (L2CAP_WAIT_CONFIG_REQ | L2CAP_WAIT_CONFIG_RSP);
391 l2cap_send_config_req(chan);
392 break;
394 case L2CAP_PENDING:
395 /* XXX dont release request, should start eRTX timeout? */
396 (*chan->lc_proto->connecting)(chan->lc_upper);
397 break;
399 case L2CAP_PSM_NOT_SUPPORTED:
400 case L2CAP_SECURITY_BLOCK:
401 case L2CAP_NO_RESOURCES:
402 default:
403 l2cap_request_free(req);
404 l2cap_close(chan, ECONNREFUSED);
405 break;
410 * Process Received Config Request.
412 static void
413 l2cap_recv_config_req(struct mbuf *m, struct hci_link *link)
415 uint8_t buf[L2CAP_MTU_MINIMUM];
416 l2cap_cmd_hdr_t cmd;
417 l2cap_cfg_req_cp cp;
418 l2cap_cfg_opt_t opt;
419 l2cap_cfg_opt_val_t val;
420 l2cap_cfg_rsp_cp rp;
421 struct l2cap_channel *chan;
422 int left, len;
424 m_copydata(m, 0, sizeof(cmd), (caddr_t)&cmd);
425 m_adj(m, sizeof(cmd));
426 left = letoh16(cmd.length);
428 if (left < sizeof(cp))
429 goto reject;
431 m_copydata(m, 0, sizeof(cp), (caddr_t)&cp);
432 m_adj(m, sizeof(cp));
433 left -= sizeof(cp);
435 cp.dcid = letoh16(cp.dcid);
436 cp.flags = letoh16(cp.flags);
438 chan = l2cap_cid_lookup(cp.dcid);
439 if (chan == NULL || chan->lc_link != link
440 || chan->lc_state != L2CAP_WAIT_CONFIG
441 || (chan->lc_flags & L2CAP_WAIT_CONFIG_REQ) == 0) {
442 /* XXX we should really accept reconfiguration requests */
443 l2cap_send_command_rej(link, cmd.ident, L2CAP_REJ_INVALID_CID,
444 L2CAP_NULL_CID, cp.dcid);
445 goto out;
448 /* ready our response packet */
449 rp.scid = htole16(chan->lc_rcid);
450 rp.flags = 0; /* "No Continuation" */
451 rp.result = L2CAP_SUCCESS;
452 len = sizeof(rp);
455 * Process the packet. We build the return packet on the fly adding any
456 * unacceptable parameters as we go. As we can only return one result,
457 * unknown option takes precedence so we start our return packet anew
458 * and ignore option values thereafter as they will be re-sent.
460 * Since we do not support enough options to make overflowing the min
461 * MTU size an issue in normal use, we just reject config requests that
462 * make that happen. This could be because options are repeated or the
463 * packet is corrupted in some way.
465 * If unknown option types threaten to overflow the packet, we just
466 * ignore them. We can deny them next time.
468 while (left > 0) {
469 if (left < sizeof(opt))
470 goto reject;
472 m_copydata(m, 0, sizeof(opt), (caddr_t)&opt);
473 m_adj(m, sizeof(opt));
474 left -= sizeof(opt);
476 if (left < opt.length)
477 goto reject;
479 switch(opt.type & L2CAP_OPT_HINT_MASK) {
480 case L2CAP_OPT_MTU:
481 if (rp.result == L2CAP_UNKNOWN_OPTION)
482 break;
484 if (opt.length != L2CAP_OPT_MTU_SIZE)
485 goto reject;
487 m_copydata(m, 0, L2CAP_OPT_MTU_SIZE, (caddr_t)&val);
488 val.mtu = letoh16(val.mtu);
491 * XXX how do we know what the minimum acceptable MTU is
492 * for a channel? Spec says some profiles have a higher
493 * minimum but I have no way to find that out at this
494 * juncture..
496 if (val.mtu < L2CAP_MTU_MINIMUM) {
497 if (len + sizeof(opt) + L2CAP_OPT_MTU_SIZE > sizeof(buf))
498 goto reject;
500 rp.result = L2CAP_UNACCEPTABLE_PARAMS;
501 memcpy(buf + len, &opt, sizeof(opt));
502 len += sizeof(opt);
503 val.mtu = htole16(L2CAP_MTU_MINIMUM);
504 memcpy(buf + len, &val, L2CAP_OPT_MTU_SIZE);
505 len += L2CAP_OPT_MTU_SIZE;
506 } else
507 chan->lc_omtu = val.mtu;
509 break;
511 case L2CAP_OPT_FLUSH_TIMO:
512 if (rp.result == L2CAP_UNKNOWN_OPTION)
513 break;
515 if (opt.length != L2CAP_OPT_FLUSH_TIMO_SIZE)
516 goto reject;
519 * I think that this is informational only - he is
520 * informing us of the flush timeout he will be using.
521 * I dont think this affects us in any significant way,
522 * so just ignore this value for now.
524 break;
526 case L2CAP_OPT_QOS:
527 if (rp.result == L2CAP_UNKNOWN_OPTION)
528 break;
530 if (opt.length != L2CAP_OPT_QOS_SIZE)
531 goto reject;
533 m_copydata(m, 0, L2CAP_OPT_QOS_SIZE, (caddr_t)&val);
534 if (val.qos.service_type == L2CAP_QOS_NO_TRAFFIC ||
535 val.qos.service_type == L2CAP_QOS_BEST_EFFORT)
537 * In accordance with the spec, we choose to
538 * ignore the fields an provide no response.
540 break;
542 if (len + sizeof(opt) + L2CAP_OPT_QOS_SIZE > sizeof(buf))
543 goto reject;
545 if (val.qos.service_type != L2CAP_QOS_GUARANTEED) {
547 * Instead of sending an "unacceptable
548 * parameters" response, treat this as an
549 * unknown option and include the option
550 * value in the response.
552 rp.result = L2CAP_UNKNOWN_OPTION;
553 } else {
555 * According to the spec, we must return
556 * specific values for wild card parameters.
557 * I don't know what to return without lying,
558 * so return "unacceptable parameters" and
559 * specify the preferred service type as
560 * "Best Effort".
562 rp.result = L2CAP_UNACCEPTABLE_PARAMS;
563 val.qos.service_type = L2CAP_QOS_BEST_EFFORT;
566 memcpy(buf + len, &opt, sizeof(opt));
567 len += sizeof(opt);
568 memcpy(buf + len, &val, L2CAP_OPT_QOS_SIZE);
569 len += L2CAP_OPT_QOS_SIZE;
570 break;
572 default:
573 /* ignore hints */
574 if (opt.type & L2CAP_OPT_HINT_BIT)
575 break;
577 /* unknown options supersede all else */
578 if (rp.result != L2CAP_UNKNOWN_OPTION) {
579 rp.result = L2CAP_UNKNOWN_OPTION;
580 len = sizeof(rp);
583 /* ignore if it doesn't fit */
584 if (len + sizeof(opt) > sizeof(buf))
585 break;
587 /* return unknown option type, but no data */
588 buf[len++] = opt.type;
589 buf[len++] = 0;
590 break;
593 m_adj(m, opt.length);
594 left -= opt.length;
597 rp.result = htole16(rp.result);
598 memcpy(buf, &rp, sizeof(rp));
599 l2cap_send_signal(link, L2CAP_CONFIG_RSP, cmd.ident, len, buf);
601 if ((cp.flags & L2CAP_OPT_CFLAG_BIT) == 0
602 && rp.result == letoh16(L2CAP_SUCCESS)) {
604 chan->lc_flags &= ~L2CAP_WAIT_CONFIG_REQ;
606 if ((chan->lc_flags & L2CAP_WAIT_CONFIG_RSP) == 0) {
607 chan->lc_state = L2CAP_OPEN;
608 /* XXX how to distinguish REconfiguration? */
609 (*chan->lc_proto->connected)(chan->lc_upper);
612 return;
614 reject:
615 l2cap_send_command_rej(link, cmd.ident, L2CAP_REJ_NOT_UNDERSTOOD);
616 out:
617 m_adj(m, left);
621 * Process Received Config Response.
623 static void
624 l2cap_recv_config_rsp(struct mbuf *m, struct hci_link *link)
626 l2cap_cmd_hdr_t cmd;
627 l2cap_cfg_rsp_cp cp;
628 l2cap_cfg_opt_t opt;
629 l2cap_cfg_opt_val_t val;
630 struct l2cap_req *req;
631 struct l2cap_channel *chan;
632 int left;
634 m_copydata(m, 0, sizeof(cmd), (caddr_t)&cmd);
635 m_adj(m, sizeof(cmd));
636 left = letoh16(cmd.length);
638 if (left < sizeof(cp))
639 goto out;
641 m_copydata(m, 0, sizeof(cp), (caddr_t)&cp);
642 m_adj(m, sizeof(cp));
643 left -= sizeof(cp);
645 cp.scid = letoh16(cp.scid);
646 cp.flags = letoh16(cp.flags);
647 cp.result = letoh16(cp.result);
649 req = l2cap_request_lookup(link, cmd.ident);
650 if (req == NULL || req->lr_code != L2CAP_CONFIG_REQ)
651 goto out;
653 chan = req->lr_chan;
654 if (chan != NULL && chan->lc_lcid != cp.scid)
655 goto out;
657 l2cap_request_free(req);
659 if (chan == NULL || chan->lc_state != L2CAP_WAIT_CONFIG
660 || (chan->lc_flags & L2CAP_WAIT_CONFIG_RSP) == 0)
661 goto out;
663 if ((cp.flags & L2CAP_OPT_CFLAG_BIT)) {
664 l2cap_cfg_req_cp rp;
667 * They have more to tell us and want another ID to
668 * use, so send an empty config request
670 if (l2cap_request_alloc(chan, L2CAP_CONFIG_REQ))
671 goto discon;
673 rp.dcid = htole16(cp.scid);
674 rp.flags = 0;
676 if (l2cap_send_signal(link, L2CAP_CONFIG_REQ, link->hl_lastid,
677 sizeof(rp), &rp))
678 goto discon;
681 switch(cp.result) {
682 case L2CAP_SUCCESS:
684 * If continuation flag was not set, our config request was
685 * accepted. We may have to wait for their config request to
686 * complete, so check that but otherwise we are open
688 * There may be 'advisory' values in the packet but we just
689 * ignore those..
691 if ((cp.flags & L2CAP_OPT_CFLAG_BIT) == 0) {
692 chan->lc_flags &= ~L2CAP_WAIT_CONFIG_RSP;
694 if ((chan->lc_flags & L2CAP_WAIT_CONFIG_REQ) == 0) {
695 chan->lc_state = L2CAP_OPEN;
696 /* XXX how to distinguish REconfiguration? */
697 (*chan->lc_proto->connected)(chan->lc_upper);
700 goto out;
702 case L2CAP_UNACCEPTABLE_PARAMS:
704 * Packet contains unacceptable parameters with preferred values
706 while (left > 0) {
707 if (left < sizeof(opt))
708 goto discon;
710 m_copydata(m, 0, sizeof(opt), (caddr_t)&opt);
711 m_adj(m, sizeof(opt));
712 left -= sizeof(opt);
714 if (left < opt.length)
715 goto discon;
717 switch (opt.type) {
718 case L2CAP_OPT_MTU:
719 if (opt.length != L2CAP_OPT_MTU_SIZE)
720 goto discon;
722 m_copydata(m, 0, L2CAP_OPT_MTU_SIZE, (caddr_t)&val);
723 chan->lc_imtu = letoh16(val.mtu);
724 if (chan->lc_imtu < L2CAP_MTU_MINIMUM)
725 chan->lc_imtu = L2CAP_MTU_DEFAULT;
726 break;
728 case L2CAP_OPT_FLUSH_TIMO:
729 if (opt.length != L2CAP_OPT_FLUSH_TIMO_SIZE)
730 goto discon;
733 * Spec says: If we cannot honor proposed value,
734 * either disconnect or try again with original
735 * value. I can't really see why they want to
736 * interfere with OUR flush timeout in any case
737 * so we just punt for now.
739 goto discon;
741 case L2CAP_OPT_QOS:
742 break;
744 default:
745 UNKNOWN(opt.type);
746 goto discon;
749 m_adj(m, opt.length);
750 left -= opt.length;
753 if ((cp.flags & L2CAP_OPT_CFLAG_BIT) == 0)
754 l2cap_send_config_req(chan); /* no state change */
756 goto out;
758 case L2CAP_REJECT:
759 goto discon;
761 case L2CAP_UNKNOWN_OPTION:
763 * Packet contains options not understood. Turn off unknown
764 * options by setting them to default values (means they will
765 * not be requested again).
767 * If our option was already off then fail (paranoia?)
769 * XXX Should we consider that options were set for a reason?
771 while (left > 0) {
772 if (left < sizeof(opt))
773 goto discon;
775 m_copydata(m, 0, sizeof(opt), (caddr_t)&opt);
776 m_adj(m, sizeof(opt));
777 left -= sizeof(opt);
779 if (left < opt.length)
780 goto discon;
782 m_adj(m, opt.length);
783 left -= opt.length;
785 switch(opt.type) {
786 case L2CAP_OPT_MTU:
787 if (chan->lc_imtu == L2CAP_MTU_DEFAULT)
788 goto discon;
790 chan->lc_imtu = L2CAP_MTU_DEFAULT;
791 break;
793 case L2CAP_OPT_FLUSH_TIMO:
794 if (chan->lc_flush == L2CAP_FLUSH_TIMO_DEFAULT)
795 goto discon;
797 chan->lc_flush = L2CAP_FLUSH_TIMO_DEFAULT;
798 break;
800 case L2CAP_OPT_QOS:
801 break;
803 default:
804 UNKNOWN(opt.type);
805 goto discon;
809 if ((cp.flags & L2CAP_OPT_CFLAG_BIT) == 0)
810 l2cap_send_config_req(chan); /* no state change */
812 goto out;
814 default:
815 UNKNOWN(cp.result);
816 goto discon;
819 DPRINTF("how did I get here!?\n");
821 discon:
822 l2cap_send_disconnect_req(chan);
823 l2cap_close(chan, ECONNABORTED);
825 out:
826 m_adj(m, left);
830 * Process Received Disconnect Request. We must validate scid and dcid
831 * just in case but otherwise this connection is finished.
833 static void
834 l2cap_recv_disconnect_req(struct mbuf *m, struct hci_link *link)
836 l2cap_cmd_hdr_t cmd;
837 l2cap_discon_req_cp cp;
838 l2cap_discon_rsp_cp rp;
839 struct l2cap_channel *chan;
841 m_copydata(m, 0, sizeof(cmd), (caddr_t)&cmd);
842 m_adj(m, sizeof(cmd));
844 m_copydata(m, 0, sizeof(cp), (caddr_t)&cp);
845 m_adj(m, sizeof(cp));
847 cp.scid = letoh16(cp.scid);
848 cp.dcid = letoh16(cp.dcid);
850 chan = l2cap_cid_lookup(cp.dcid);
851 if (chan == NULL || chan->lc_link != link || chan->lc_rcid != cp.scid) {
852 l2cap_send_command_rej(link, cmd.ident, L2CAP_REJ_INVALID_CID,
853 cp.dcid, cp.scid);
854 return;
857 rp.dcid = htole16(chan->lc_lcid);
858 rp.scid = htole16(chan->lc_rcid);
859 l2cap_send_signal(link, L2CAP_DISCONNECT_RSP, cmd.ident,
860 sizeof(rp), &rp);
862 if (chan->lc_state != L2CAP_CLOSED)
863 l2cap_close(chan, ECONNRESET);
867 * Process Received Disconnect Response. We must validate scid and dcid but
868 * unless we were waiting for this signal, ignore it.
870 static void
871 l2cap_recv_disconnect_rsp(struct mbuf *m, struct hci_link *link)
873 l2cap_cmd_hdr_t cmd;
874 l2cap_discon_rsp_cp cp;
875 struct l2cap_req *req;
876 struct l2cap_channel *chan;
878 m_copydata(m, 0, sizeof(cmd), (caddr_t)&cmd);
879 m_adj(m, sizeof(cmd));
881 m_copydata(m, 0, sizeof(cp), (caddr_t)&cp);
882 m_adj(m, sizeof(cp));
884 cp.scid = letoh16(cp.scid);
885 cp.dcid = letoh16(cp.dcid);
887 req = l2cap_request_lookup(link, cmd.ident);
888 if (req == NULL || req->lr_code != L2CAP_DISCONNECT_REQ)
889 return;
891 chan = req->lr_chan;
892 if (chan == NULL
893 || chan->lc_lcid != cp.scid
894 || chan->lc_rcid != cp.dcid)
895 return;
897 l2cap_request_free(req);
899 if (chan->lc_state != L2CAP_WAIT_DISCONNECT)
900 return;
902 l2cap_close(chan, 0);
906 * Process Received Info Request. We must respond but alas dont
907 * support anything as yet so thats easy.
909 static void
910 l2cap_recv_info_req(struct mbuf *m, struct hci_link *link)
912 l2cap_cmd_hdr_t cmd;
913 l2cap_info_req_cp cp;
914 l2cap_info_rsp_cp rp;
916 m_copydata(m, 0, sizeof(cmd), (caddr_t)&cmd);
917 m_adj(m, sizeof(cmd));
919 m_copydata(m, 0, sizeof(cp), (caddr_t)&cp);
920 m_adj(m, sizeof(cp));
922 switch(letoh16(cp.type)) {
923 case L2CAP_CONNLESS_MTU:
924 case L2CAP_EXTENDED_FEATURES:
925 default:
926 rp.type = cp.type;
927 rp.result = htole16(L2CAP_NOT_SUPPORTED);
929 l2cap_send_signal(link, L2CAP_INFO_RSP, cmd.ident,
930 sizeof(rp), &rp);
931 break;
936 * Construct signal and wrap in C-Frame for link.
938 static int
939 l2cap_send_signal(struct hci_link *link, uint8_t code, uint8_t ident,
940 uint16_t length, void *data)
942 struct mbuf *m;
943 l2cap_hdr_t *hdr;
944 l2cap_cmd_hdr_t *cmd;
946 #ifdef DIAGNOSTIC
947 if (link == NULL)
948 return ENETDOWN;
950 if (sizeof(l2cap_cmd_hdr_t) + length > link->hl_mtu)
951 kprintf("(%s) exceeding L2CAP Signal MTU for link!\n",
952 device_get_nameunit(link->hl_unit->hci_dev));
953 #endif
955 m = m_gethdr(MB_DONTWAIT, MT_DATA);
956 if (m == NULL)
957 return ENOMEM;
959 hdr = mtod(m, l2cap_hdr_t *);
960 cmd = (l2cap_cmd_hdr_t *)(hdr + 1);
962 m->m_len = m->m_pkthdr.len = MHLEN;
964 /* Command Data */
965 if (length > 0)
966 m_copyback(m, sizeof(*hdr) + sizeof(*cmd), length, data);
968 /* Command Header */
969 cmd->code = code;
970 cmd->ident = ident;
971 cmd->length = htole16(length);
972 length += sizeof(*cmd);
974 /* C-Frame Header */
975 hdr->length = htole16(length);
976 hdr->dcid = htole16(L2CAP_SIGNAL_CID);
977 length += sizeof(*hdr);
979 if (m->m_pkthdr.len != MAX(MHLEN, length)) {
980 m_freem(m);
981 return ENOMEM;
984 m->m_pkthdr.len = length;
985 m->m_len = MIN(length, MHLEN);
987 DPRINTFN(2, "(%s) code %d, ident %d, len %d\n",
988 device_get_nameunit(link->hl_unit->hci_dev), code, ident,
989 length);
991 return hci_acl_send(m, link, NULL);
995 * Send Command Reject packet.
997 static int
998 l2cap_send_command_rej(struct hci_link *link, uint8_t ident,
999 uint16_t reason, ...)
1001 l2cap_cmd_rej_cp cp;
1002 int len = 0;
1003 va_list ap;
1005 va_start(ap, reason);
1007 cp.reason = htole16(reason);
1009 switch (reason) {
1010 case L2CAP_REJ_NOT_UNDERSTOOD:
1011 len = 2;
1012 break;
1014 case L2CAP_REJ_MTU_EXCEEDED:
1015 len = 4;
1016 cp.data[0] = va_arg(ap, int); /* SigMTU */
1017 cp.data[0] = htole16(cp.data[0]);
1018 break;
1020 case L2CAP_REJ_INVALID_CID:
1021 len = 6;
1022 cp.data[0] = va_arg(ap, int); /* dcid */
1023 cp.data[0] = htole16(cp.data[0]);
1024 cp.data[1] = va_arg(ap, int); /* scid */
1025 cp.data[1] = htole16(cp.data[1]);
1026 break;
1028 default:
1029 UNKNOWN(reason);
1030 return EINVAL;
1033 va_end(ap);
1035 return l2cap_send_signal(link, L2CAP_COMMAND_REJ, ident, len, &cp);
1039 * Send Connect Request
1042 l2cap_send_connect_req(struct l2cap_channel *chan)
1044 l2cap_con_req_cp cp;
1045 int err;
1047 err = l2cap_request_alloc(chan, L2CAP_CONNECT_REQ);
1048 if (err)
1049 return err;
1051 cp.psm = htole16(chan->lc_raddr.bt_psm);
1052 cp.scid = htole16(chan->lc_lcid);
1054 return l2cap_send_signal(chan->lc_link, L2CAP_CONNECT_REQ,
1055 chan->lc_link->hl_lastid, sizeof(cp), &cp);
1059 * Send Config Request
1061 * For outgoing config request, we only put options in the packet if they
1062 * differ from the default and would have to be actioned. We dont support
1063 * enough option types to make overflowing SigMTU an issue so it can all
1064 * go in one packet.
1067 l2cap_send_config_req(struct l2cap_channel *chan)
1069 l2cap_cfg_req_cp *cp;
1070 l2cap_cfg_opt_t *opt;
1071 l2cap_cfg_opt_val_t *val;
1072 uint8_t *next, buf[L2CAP_MTU_MINIMUM];
1073 int err;
1075 err = l2cap_request_alloc(chan, L2CAP_CONFIG_REQ);
1076 if (err)
1077 return err;
1079 /* Config Header (4 octets) */
1080 cp = (l2cap_cfg_req_cp *)buf;
1081 cp->dcid = htole16(chan->lc_rcid);
1082 cp->flags = 0; /* "No Continuation" */
1084 next = buf + sizeof(l2cap_cfg_req_cp);
1086 /* Incoming MTU (4 octets) */
1087 if (chan->lc_imtu != L2CAP_MTU_DEFAULT) {
1088 opt = (l2cap_cfg_opt_t *)next;
1089 opt->type = L2CAP_OPT_MTU;
1090 opt->length = L2CAP_OPT_MTU_SIZE;
1092 val = (l2cap_cfg_opt_val_t *)(opt + 1);
1093 val->mtu = htole16(chan->lc_imtu);
1095 next += sizeof(l2cap_cfg_opt_t) + L2CAP_OPT_MTU_SIZE;
1098 /* Flush Timeout (4 octets) */
1099 if (chan->lc_flush != L2CAP_FLUSH_TIMO_DEFAULT) {
1100 opt = (l2cap_cfg_opt_t *)next;
1101 opt->type = L2CAP_OPT_FLUSH_TIMO;
1102 opt->length = L2CAP_OPT_FLUSH_TIMO_SIZE;
1104 val = (l2cap_cfg_opt_val_t *)(opt + 1);
1105 val->flush_timo = htole16(chan->lc_flush);
1107 next += sizeof(l2cap_cfg_opt_t) + L2CAP_OPT_FLUSH_TIMO_SIZE;
1110 /* Outgoing QoS Flow (24 octets) */
1111 /* Retransmission & Flow Control (11 octets) */
1113 * From here we need to start paying attention to SigMTU as we have
1114 * possibly overflowed the minimum supported..
1117 return l2cap_send_signal(chan->lc_link, L2CAP_CONFIG_REQ,
1118 chan->lc_link->hl_lastid, (int)(next - buf), buf);
1122 * Send Disconnect Request
1125 l2cap_send_disconnect_req(struct l2cap_channel *chan)
1127 l2cap_discon_req_cp cp;
1128 int err;
1130 err = l2cap_request_alloc(chan, L2CAP_DISCONNECT_REQ);
1131 if (err)
1132 return err;
1134 cp.dcid = htole16(chan->lc_rcid);
1135 cp.scid = htole16(chan->lc_lcid);
1137 return l2cap_send_signal(chan->lc_link, L2CAP_DISCONNECT_REQ,
1138 chan->lc_link->hl_lastid, sizeof(cp), &cp);
1142 * Send Connect Response
1145 l2cap_send_connect_rsp(struct hci_link *link, uint8_t ident, uint16_t dcid,
1146 uint16_t scid, uint16_t result)
1148 l2cap_con_rsp_cp cp;
1150 memset(&cp, 0, sizeof(cp));
1151 cp.dcid = htole16(dcid);
1152 cp.scid = htole16(scid);
1153 cp.result = htole16(result);
1155 return l2cap_send_signal(link, L2CAP_CONNECT_RSP, ident, sizeof(cp), &cp);