l2cap_signal.c revision 1.3 1 /* $NetBSD: l2cap_signal.c,v 1.3 2006/12/07 21:36:27 plunky Exp $ */
2
3 /*-
4 * Copyright (c) 2005 Iain Hibbert.
5 * Copyright (c) 2006 Itronix Inc.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of Itronix Inc. may not be used to endorse
17 * or promote products derived from this software without specific
18 * prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
24 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 * ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: l2cap_signal.c,v 1.3 2006/12/07 21:36:27 plunky Exp $");
35
36 #include <sys/param.h>
37 #include <sys/kernel.h>
38 #include <sys/mbuf.h>
39 #include <sys/proc.h>
40 #include <sys/queue.h>
41 #include <sys/systm.h>
42
43 #include <machine/stdarg.h>
44
45 #include <netbt/bluetooth.h>
46 #include <netbt/hci.h>
47 #include <netbt/l2cap.h>
48
49 /*******************************************************************************
50 *
51 * L2CAP Signal processing
52 */
53
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, ...);
64
65 /*
66 * process incoming signal packets (CID 0x0001). Can contain multiple
67 * requests/responses.
68 */
69 void
70 l2cap_recv_signal(struct mbuf *m, struct hci_link *link)
71 {
72 l2cap_cmd_hdr_t cmd;
73
74 for(;;) {
75 if (m->m_pkthdr.len == 0)
76 goto finish;
77
78 if (m->m_pkthdr.len < sizeof(cmd))
79 goto reject;
80
81 m_copydata(m, 0, sizeof(cmd), &cmd);
82 cmd.length = le16toh(cmd.length);
83
84 if (m->m_pkthdr.len < sizeof(cmd) + cmd.length)
85 goto reject;
86
87 DPRINTFN(2, "(%s) code %d, ident %d, len %d\n",
88 link->hl_unit->hci_devname,
89 cmd.code, cmd.ident, cmd.length);
90
91 switch (cmd.code) {
92 case L2CAP_COMMAND_REJ:
93 if (cmd.length > sizeof(l2cap_cmd_rej_cp))
94 goto finish;
95
96 l2cap_recv_command_rej(m, link);
97 break;
98
99 case L2CAP_CONNECT_REQ:
100 if (cmd.length != sizeof(l2cap_con_req_cp))
101 goto reject;
102
103 l2cap_recv_connect_req(m, link);
104 break;
105
106 case L2CAP_CONNECT_RSP:
107 if (cmd.length != sizeof(l2cap_con_rsp_cp))
108 goto finish;
109
110 l2cap_recv_connect_rsp(m, link);
111 break;
112
113 case L2CAP_CONFIG_REQ:
114 l2cap_recv_config_req(m, link);
115 break;
116
117 case L2CAP_CONFIG_RSP:
118 l2cap_recv_config_rsp(m, link);
119 break;
120
121 case L2CAP_DISCONNECT_REQ:
122 if (cmd.length != sizeof(l2cap_discon_req_cp))
123 goto reject;
124
125 l2cap_recv_disconnect_req(m, link);
126 break;
127
128 case L2CAP_DISCONNECT_RSP:
129 if (cmd.length != sizeof(l2cap_discon_rsp_cp))
130 goto finish;
131
132 l2cap_recv_disconnect_rsp(m, link);
133 break;
134
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;
140
141 case L2CAP_ECHO_RSP:
142 m_adj(m, sizeof(cmd) + cmd.length);
143 break;
144
145 case L2CAP_INFO_REQ:
146 if (cmd.length != sizeof(l2cap_info_req_cp))
147 goto reject;
148
149 l2cap_recv_info_req(m, link);
150 break;
151
152 case L2CAP_INFO_RSP:
153 m_adj(m, sizeof(cmd) + cmd.length);
154 break;
155
156 default:
157 goto reject;
158 }
159 }
160
161 #ifdef DIAGNOSTIC
162 panic("impossible!");
163 #endif
164
165 reject:
166 l2cap_send_command_rej(link, cmd.ident, L2CAP_REJ_NOT_UNDERSTOOD);
167 finish:
168 m_freem(m);
169 }
170
171 /*
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!)
175 */
176 static void
177 l2cap_recv_command_rej(struct mbuf *m, struct hci_link *link)
178 {
179 struct l2cap_req *req;
180 struct l2cap_channel *chan;
181 l2cap_cmd_hdr_t cmd;
182 l2cap_cmd_rej_cp cp;
183
184 m_copydata(m, 0, sizeof(cmd), &cmd);
185 m_adj(m, sizeof(cmd));
186
187 cmd.length = le16toh(cmd.length);
188
189 m_copydata(m, 0, cmd.length, &cp);
190 m_adj(m, cmd.length);
191
192 req = l2cap_request_lookup(link, cmd.ident);
193 if (req == NULL)
194 return;
195
196 switch (le16toh(cp.reason)) {
197 case L2CAP_REJ_NOT_UNDERSTOOD:
198 /*
199 * I dont know what to do, just move up the timeout
200 */
201 callout_schedule(&req->lr_rtx, 0);
202 break;
203
204 case L2CAP_REJ_MTU_EXCEEDED:
205 /*
206 * I didnt send any commands over L2CAP_MTU_MINIMUM size, but..
207 */
208 link->hl_mtu = le16toh(cp.data[0]);
209 callout_schedule(&req->lr_rtx, 0); // XX maybe resend instead?
210 break;
211
212 case L2CAP_REJ_INVALID_CID:
213 /*
214 * Well, if they dont have such a channel then our channel is
215 * most likely closed. Make it so.
216 */
217 chan = req->lr_chan;
218 l2cap_request_free(req);
219 if (chan != NULL && chan->lc_state != L2CAP_CLOSED)
220 l2cap_close(chan, ECONNABORTED);
221
222 break;
223
224 default:
225 UNKNOWN(le16toh(cp.reason));
226 break;
227 }
228 }
229
230 /*
231 * Process Received Connect Request. Find listening channel matching
232 * psm & addr and ask upper layer for a new channel.
233 */
234 static void
235 l2cap_recv_connect_req(struct mbuf *m, struct hci_link *link)
236 {
237 struct sockaddr_bt laddr, raddr;
238 struct l2cap_channel *chan, *new;
239 l2cap_cmd_hdr_t cmd;
240 l2cap_con_req_cp cp;
241 l2cap_con_rsp_cp rp;
242 int err;
243
244 /* extract cmd */
245 m_copydata(m, 0, sizeof(cmd), &cmd);
246 m_adj(m, sizeof(cmd));
247
248 /* extract request */
249 m_copydata(m, 0, sizeof(cp), &cp);
250 m_adj(m, sizeof(cp));
251
252 /* init response */
253 memset(&rp, 0, sizeof(rp));
254 rp.scid = cp.scid;
255
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 = le16toh(cp.psm);
260 bdaddr_copy(&laddr.bt_bdaddr, &link->hl_unit->hci_bdaddr);
261
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 = le16toh(cp.psm);
266 bdaddr_copy(&raddr.bt_bdaddr, &link->hl_bdaddr);
267
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;
272
273 if (!bdaddr_same(&laddr.bt_bdaddr, &chan->lc_laddr.bt_bdaddr)
274 && bdaddr_any(&chan->lc_laddr.bt_bdaddr) == 0)
275 continue;
276
277 new= (*chan->lc_proto->newconn)(chan->lc_upper, &laddr, &raddr);
278 if (new == NULL)
279 continue;
280
281 err = l2cap_cid_alloc(new);
282 if (err) {
283 rp.result = htole16(L2CAP_NO_RESOURCES);
284 l2cap_send_signal(link, L2CAP_CONNECT_RSP,
285 cmd.ident, sizeof(rp), &rp);
286 (*new->lc_proto->disconnected)(new->lc_upper, err);
287 return;
288 }
289
290 new->lc_link = hci_acl_open(link->hl_unit, &link->hl_bdaddr);
291 KASSERT(new->lc_link == link);
292
293 new->lc_rcid = le16toh(cp.scid);
294
295 memcpy(&new->lc_laddr, &laddr, sizeof(struct sockaddr_bt));
296 memcpy(&new->lc_raddr, &raddr, sizeof(struct sockaddr_bt));
297
298 rp.dcid = htole16(new->lc_lcid);
299 rp.result = htole16(L2CAP_SUCCESS);
300 l2cap_send_signal(link, L2CAP_CONNECT_RSP, cmd.ident,
301 sizeof(rp), &rp);
302
303 new->lc_state = L2CAP_WAIT_CONFIG_REQ | L2CAP_WAIT_CONFIG_RSP;
304 l2cap_send_config_req(new);
305 return;
306 }
307
308 rp.result = htole16(L2CAP_PSM_NOT_SUPPORTED);
309 l2cap_send_signal(link, L2CAP_CONNECT_RSP, cmd.ident, sizeof(rp), &rp);
310 }
311
312 /*
313 * Process Received Connect Response.
314 */
315 static void
316 l2cap_recv_connect_rsp(struct mbuf *m, struct hci_link *link)
317 {
318 l2cap_cmd_hdr_t cmd;
319 l2cap_con_rsp_cp cp;
320 struct l2cap_req *req;
321 struct l2cap_channel *chan;
322
323 m_copydata(m, 0, sizeof(cmd), &cmd);
324 m_adj(m, sizeof(cmd));
325
326 m_copydata(m, 0, sizeof(cp), &cp);
327 m_adj(m, sizeof(cp));
328
329 cp.scid = le16toh(cp.scid);
330 cp.dcid = le16toh(cp.dcid);
331 cp.result = le16toh(cp.result);
332
333 req = l2cap_request_lookup(link, cmd.ident);
334 if (req == NULL || req->lr_code != L2CAP_CONNECT_REQ)
335 return;
336
337 chan = req->lr_chan;
338 if (chan != NULL && chan->lc_lcid != cp.scid)
339 return;
340
341 if (chan == NULL || chan->lc_state != L2CAP_WAIT_CONNECT_RSP) {
342 l2cap_request_free(req);
343 return;
344 }
345
346 switch (cp.result) {
347 case L2CAP_SUCCESS:
348 /*
349 * Ok, at this point we have a connection to the other party. We
350 * could indicate upstream that we are ready for business and
351 * wait for a "Configure Channel Request" but I'm not so sure
352 * that is required in our case - we will proceed directly to
353 * sending our config request. We set two state bits because in
354 * the config state we are waiting for requests and responses.
355 */
356 l2cap_request_free(req);
357 chan->lc_rcid = cp.dcid;
358 chan->lc_state = L2CAP_WAIT_CONFIG_REQ | L2CAP_WAIT_CONFIG_RSP;
359 l2cap_send_config_req(chan);
360 break;
361
362 case L2CAP_PENDING:
363 // dont release request, should start eRTX timeout?
364 (*chan->lc_proto->connecting)(chan->lc_upper);
365 break;
366
367 case L2CAP_PSM_NOT_SUPPORTED:
368 case L2CAP_SECURITY_BLOCK:
369 case L2CAP_NO_RESOURCES:
370 default:
371 l2cap_request_free(req);
372 l2cap_close(chan, ECONNREFUSED);
373 break;
374 }
375 }
376
377 /*
378 * Process Received Config Reqest.
379 */
380 static void
381 l2cap_recv_config_req(struct mbuf *m, struct hci_link *link)
382 {
383 uint8_t buf[L2CAP_MTU_MINIMUM];
384 l2cap_cmd_hdr_t cmd;
385 l2cap_cfg_req_cp cp;
386 l2cap_cfg_opt_t opt;
387 l2cap_cfg_opt_val_t val;
388 l2cap_cfg_rsp_cp rp;
389 struct l2cap_channel *chan;
390 int left, len;
391
392 m_copydata(m, 0, sizeof(cmd), &cmd);
393 m_adj(m, sizeof(cmd));
394 left = le16toh(cmd.length);
395
396 if (left < sizeof(cp))
397 goto reject;
398
399 m_copydata(m, 0, sizeof(cp), &cp);
400 m_adj(m, sizeof(cp));
401 left -= sizeof(cp);
402
403 cp.dcid = le16toh(cp.dcid);
404 cp.flags = le16toh(cp.flags);
405
406 chan = l2cap_cid_lookup(cp.dcid);
407 if (chan == NULL || (chan->lc_state & L2CAP_WAIT_CONFIG_REQ) == 0) {
408 l2cap_send_command_rej(link, cmd.ident, L2CAP_REJ_INVALID_CID,
409 L2CAP_NULL_CID, cp.dcid);
410 goto out;
411 }
412
413 /* ready our response packet */
414 rp.scid = htole16(chan->lc_rcid);
415 rp.flags = 0; /* "No Continuation" */
416 rp.result = L2CAP_SUCCESS;
417 len = sizeof(rp);
418
419 /*
420 * Process the packet. We build the return packet on the fly adding any
421 * unacceptable parameters as we go. As we can only return one result,
422 * unknown option takes precedence so we start our return packet anew
423 * and ignore option values thereafter as they will be re-sent.
424 *
425 * Since we do not support enough options to make overflowing the min
426 * MTU size an issue in normal use, we just reject config requests that
427 * make that happen. This could be because options are repeated or the
428 * packet is corrupted in some way.
429 *
430 * If unknown option types threaten to overflow the packet, we just
431 * ignore them. We can deny them next time.
432 */
433 while (left > 0) {
434 if (left < sizeof(opt))
435 goto reject;
436
437 m_copydata(m, 0, sizeof(opt), &opt);
438 m_adj(m, sizeof(opt));
439 left -= sizeof(opt);
440
441 if (left < opt.length)
442 goto reject;
443
444 switch(opt.type & L2CAP_OPT_HINT_MASK) {
445 case L2CAP_OPT_MTU:
446 if (rp.result == L2CAP_UNKNOWN_OPTION)
447 break;
448
449 if (opt.length != L2CAP_OPT_MTU_SIZE)
450 goto reject;
451
452 m_copydata(m, 0, L2CAP_OPT_MTU_SIZE, &val);
453 val.mtu = le16toh(val.mtu);
454
455 /*
456 * XXX how do we know what the minimum acceptable MTU is
457 * for a channel? Spec says some profiles have a higher
458 * minimum but I have no way to find that out at this
459 * juncture..
460 */
461 if (val.mtu < L2CAP_MTU_MINIMUM) {
462 if (len + sizeof(opt) + L2CAP_OPT_MTU_SIZE > sizeof(buf))
463 goto reject;
464
465 rp.result = L2CAP_UNACCEPTABLE_PARAMS;
466 memcpy(buf + len, &opt, sizeof(opt));
467 len += sizeof(opt);
468 val.mtu = htole16(L2CAP_MTU_MINIMUM);
469 memcpy(buf + len, &val, L2CAP_OPT_MTU_SIZE);
470 len += L2CAP_OPT_MTU_SIZE;
471 } else
472 chan->lc_omtu = val.mtu;
473
474 break;
475
476 case L2CAP_OPT_FLUSH_TIMO:
477 if (rp.result == L2CAP_UNKNOWN_OPTION)
478 break;
479
480 if (opt.length != L2CAP_OPT_FLUSH_TIMO_SIZE)
481 goto reject;
482
483 /*
484 * I think that this is informational only - he is
485 * informing us of the flush timeout he will be using.
486 * I dont think this affects us in any significant way,
487 * so just ignore this value for now.
488 */
489 break;
490
491 case L2CAP_OPT_QOS:
492 default:
493 /* ignore hints */
494 if (opt.type & L2CAP_OPT_HINT_BIT)
495 break;
496
497 /* unknown options supercede all else */
498 if (rp.result != L2CAP_UNKNOWN_OPTION) {
499 rp.result = L2CAP_UNKNOWN_OPTION;
500 len = sizeof(rp);
501 }
502
503 /* ignore if it don't fit */
504 if (len + sizeof(opt) > sizeof(buf))
505 break;
506
507 /* return unknown option type, but no data */
508 buf[len++] = opt.type;
509 buf[len++] = 0;
510 break;
511 }
512
513 m_adj(m, opt.length);
514 left -= opt.length;
515 }
516
517 rp.result = htole16(rp.result);
518 memcpy(buf, &rp, sizeof(rp));
519 l2cap_send_signal(link, L2CAP_CONFIG_RSP, cmd.ident, len, buf);
520
521 if ((cp.flags & L2CAP_OPT_CFLAG_BIT) == 0
522 && rp.result == le16toh(L2CAP_SUCCESS)) {
523
524 chan->lc_state &= ~L2CAP_WAIT_CONFIG_REQ;
525
526 if ((chan->lc_state & L2CAP_WAIT_CONFIG_RSP) == 0) {
527 chan->lc_state = L2CAP_OPEN;
528 // XXX how to distinguish REconfiguration?
529 (*chan->lc_proto->connected)(chan->lc_upper);
530 }
531 }
532 return;
533
534 reject:
535 l2cap_send_command_rej(link, cmd.ident, L2CAP_REJ_NOT_UNDERSTOOD);
536 out:
537 m_adj(m, left);
538 }
539
540 /*
541 * Process Received Config Response.
542 */
543 static void
544 l2cap_recv_config_rsp(struct mbuf *m, struct hci_link *link)
545 {
546 l2cap_cmd_hdr_t cmd;
547 l2cap_cfg_rsp_cp cp;
548 l2cap_cfg_opt_t opt;
549 l2cap_cfg_opt_val_t val;
550 struct l2cap_req *req;
551 struct l2cap_channel *chan;
552 int left;
553
554 m_copydata(m, 0, sizeof(cmd), &cmd);
555 m_adj(m, sizeof(cmd));
556 left = le16toh(cmd.length);
557
558 if (left < sizeof(cp))
559 goto out;
560
561 m_copydata(m, 0, sizeof(cp), &cp);
562 m_adj(m, sizeof(cp));
563 left -= sizeof(cp);
564
565 cp.scid = le16toh(cp.scid);
566 cp.flags = le16toh(cp.flags);
567 cp.result = le16toh(cp.result);
568
569 req = l2cap_request_lookup(link, cmd.ident);
570 if (req == NULL || req->lr_code != L2CAP_CONFIG_REQ)
571 goto out;
572
573 chan = req->lr_chan;
574 if (chan != NULL && chan->lc_lcid != cp.scid)
575 goto out;
576
577 l2cap_request_free(req);
578
579 if (chan == NULL || (chan->lc_state & L2CAP_WAIT_CONFIG_RSP) == 0)
580 goto out;
581
582 if ((cp.flags & L2CAP_OPT_CFLAG_BIT)) {
583 l2cap_cfg_req_cp rp;
584
585 /*
586 * They have more to tell us and want another ID to
587 * use, so send an empty config request
588 */
589 if (l2cap_request_alloc(chan, L2CAP_CONFIG_REQ))
590 goto discon;
591
592 rp.dcid = htole16(cp.scid);
593 rp.flags = 0;
594
595 if (l2cap_send_signal(link, L2CAP_CONFIG_REQ, link->hl_lastid,
596 sizeof(rp), &rp))
597 goto discon;
598 }
599
600 switch(cp.result) {
601 case L2CAP_SUCCESS:
602 /*
603 * If continuation flag was not set, our config request was
604 * accepted. We may have to wait for their config request to
605 * complete, so check that but otherwise we are open
606 *
607 * There may be 'advisory' values in the packet but we just
608 * ignore those..
609 */
610 if ((cp.flags & L2CAP_OPT_CFLAG_BIT) == 0) {
611 chan->lc_state &= ~L2CAP_WAIT_CONFIG_RSP;
612
613 if ((chan->lc_state & L2CAP_WAIT_CONFIG_REQ) == 0) {
614 chan->lc_state = L2CAP_OPEN;
615 // XXX how to distinguish REconfiguration?
616 (*chan->lc_proto->connected)(chan->lc_upper);
617 }
618 }
619 goto out;
620
621 case L2CAP_UNACCEPTABLE_PARAMS:
622 /*
623 * Packet contains unacceptable parameters with preferred values
624 */
625 while (left > 0) {
626 if (left < sizeof(opt))
627 goto discon;
628
629 m_copydata(m, 0, sizeof(opt), &opt);
630 m_adj(m, sizeof(opt));
631 left -= sizeof(opt);
632
633 if (left < opt.length)
634 goto discon;
635
636 switch (opt.type) {
637 case L2CAP_OPT_MTU:
638 if (opt.length != L2CAP_OPT_MTU_SIZE)
639 goto discon;
640
641 m_copydata(m, 0, L2CAP_OPT_MTU_SIZE, &val);
642 chan->lc_imtu = le16toh(val.mtu);
643 if (chan->lc_imtu < L2CAP_MTU_MINIMUM)
644 chan->lc_imtu = L2CAP_MTU_DEFAULT;
645 break;
646
647 case L2CAP_OPT_FLUSH_TIMO:
648 if (opt.length != L2CAP_OPT_FLUSH_TIMO_SIZE)
649 goto discon;
650
651 /*
652 * Spec says: If we cannot honor proposed value,
653 * either disconnect or try again with original
654 * value. I can't really see why they want to
655 * interfere with OUR flush timeout in any case
656 * so we just punt for now.
657 */
658 goto discon;
659
660 case L2CAP_OPT_QOS:
661 break;
662
663 default:
664 UNKNOWN(opt.type);
665 goto discon;
666 }
667
668 m_adj(m, opt.length);
669 left -= opt.length;
670 }
671
672 if ((cp.flags & L2CAP_OPT_CFLAG_BIT) == 0)
673 l2cap_send_config_req(chan); // no state change
674
675 goto out;
676
677 case L2CAP_REJECT:
678 goto discon;
679
680 case L2CAP_UNKNOWN_OPTION:
681 /*
682 * Packet contains options not understood. Turn off unknown
683 * options by setting them to default values (means they will
684 * not be requested again).
685 *
686 * If our option was already off then fail (paranoia?)
687 *
688 * XXX Should we consider that options were set for a reason?
689 */
690 while (left > 0) {
691 if (left < sizeof(opt))
692 goto discon;
693
694 m_copydata(m, 0, sizeof(opt), &opt);
695 m_adj(m, sizeof(opt));
696 left -= sizeof(opt);
697
698 if (left < opt.length)
699 goto discon;
700
701 m_adj(m, opt.length);
702 left -= opt.length;
703
704 switch(opt.type) {
705 case L2CAP_OPT_MTU:
706 if (chan->lc_imtu == L2CAP_MTU_DEFAULT)
707 goto discon;
708
709 chan->lc_imtu = L2CAP_MTU_DEFAULT;
710 break;
711
712 case L2CAP_OPT_FLUSH_TIMO:
713 if (chan->lc_flush == L2CAP_FLUSH_TIMO_DEFAULT)
714 goto discon;
715
716 chan->lc_flush = L2CAP_FLUSH_TIMO_DEFAULT;
717 break;
718
719 case L2CAP_OPT_QOS:
720 break;
721
722 default:
723 UNKNOWN(opt.type);
724 goto discon;
725 }
726 }
727
728 if ((cp.flags & L2CAP_OPT_CFLAG_BIT) == 0)
729 l2cap_send_config_req(chan); /* no state change */
730
731 goto out;
732
733 default:
734 UNKNOWN(cp.result);
735 goto discon;
736 }
737
738 DPRINTF("how did I get here!?\n");
739
740 discon:
741 l2cap_send_disconnect_req(chan);
742 l2cap_close(chan, ECONNABORTED);
743
744 out:
745 m_adj(m, left);
746 }
747
748 /*
749 * Process Received Disconnect Request. We must validate scid and dcid
750 * just in case but otherwise this connection is finished.
751 */
752 static void
753 l2cap_recv_disconnect_req(struct mbuf *m, struct hci_link *link)
754 {
755 l2cap_cmd_hdr_t cmd;
756 l2cap_discon_req_cp cp;
757 l2cap_discon_rsp_cp rp;
758 struct l2cap_channel *chan;
759
760 m_copydata(m, 0, sizeof(cmd), &cmd);
761 m_adj(m, sizeof(cmd));
762
763 m_copydata(m, 0, sizeof(cp), &cp);
764 m_adj(m, sizeof(cp));
765
766 cp.scid = le16toh(cp.scid);
767 cp.dcid = le16toh(cp.dcid);
768
769 chan = l2cap_cid_lookup(cp.dcid);
770 if (chan == NULL || chan->lc_rcid != cp.scid) {
771 l2cap_send_command_rej(link, cmd.ident, L2CAP_REJ_INVALID_CID,
772 cp.dcid, cp.scid);
773 return;
774 }
775
776 rp.dcid = htole16(chan->lc_lcid);
777 rp.scid = htole16(chan->lc_rcid);
778 l2cap_send_signal(link, L2CAP_DISCONNECT_RSP, cmd.ident,
779 sizeof(rp), &rp);
780
781 if (chan->lc_state != L2CAP_CLOSED)
782 l2cap_close(chan, ECONNRESET);
783 }
784
785 /*
786 * Process Received Disconnect Response. We must validate scid and dcid but
787 * unless we were waiting for this signal, ignore it.
788 */
789 static void
790 l2cap_recv_disconnect_rsp(struct mbuf *m, struct hci_link *link)
791 {
792 l2cap_cmd_hdr_t cmd;
793 l2cap_discon_rsp_cp cp;
794 struct l2cap_req *req;
795 struct l2cap_channel *chan;
796
797 m_copydata(m, 0, sizeof(cmd), &cmd);
798 m_adj(m, sizeof(cmd));
799
800 m_copydata(m, 0, sizeof(cp), &cp);
801 m_adj(m, sizeof(cp));
802
803 cp.scid = le16toh(cp.scid);
804 cp.dcid = le16toh(cp.dcid);
805
806 req = l2cap_request_lookup(link, cmd.ident);
807 if (req == NULL || req->lr_code != L2CAP_DISCONNECT_REQ)
808 return;
809
810 chan = req->lr_chan;
811 if (chan == NULL
812 || chan->lc_lcid != cp.scid
813 || chan->lc_rcid != cp.dcid)
814 return;
815
816 l2cap_request_free(req);
817
818 if (chan->lc_state != L2CAP_WAIT_DISCONNECT)
819 return;
820
821 l2cap_close(chan, 0);
822 }
823
824 /*
825 * Process Received Info Request. We must respond but alas dont
826 * support anything as yet so thats easy.
827 */
828 static void
829 l2cap_recv_info_req(struct mbuf *m, struct hci_link *link)
830 {
831 l2cap_cmd_hdr_t cmd;
832 l2cap_info_req_cp cp;
833 l2cap_info_rsp_cp rp;
834
835 m_copydata(m, 0, sizeof(cmd), &cmd);
836 m_adj(m, sizeof(cmd));
837
838 m_copydata(m, 0, sizeof(cp), &cp);
839 m_adj(m, sizeof(cp));
840
841 switch(le16toh(cp.type)) {
842 case L2CAP_CONNLESS_MTU:
843 case L2CAP_EXTENDED_FEATURES:
844 default:
845 rp.type = cp.type;
846 rp.result = htole16(L2CAP_NOT_SUPPORTED);
847
848 l2cap_send_signal(link, L2CAP_INFO_RSP, cmd.ident,
849 sizeof(rp), &rp);
850 break;
851 }
852 }
853
854 /*
855 * Construct signal and wrap in C-Frame for link.
856 */
857 static int
858 l2cap_send_signal(struct hci_link *link, uint8_t code, uint8_t ident,
859 uint16_t length, void *data)
860 {
861 struct mbuf *m;
862 l2cap_hdr_t *hdr;
863 l2cap_cmd_hdr_t *cmd;
864
865 #ifdef DIAGNOSTIC
866 if (link == NULL)
867 return ENETDOWN;
868
869 if (sizeof(l2cap_cmd_hdr_t) + length > link->hl_mtu)
870 printf("(%s) exceeding L2CAP Signal MTU for link!\n",
871 link->hl_unit->hci_devname);
872 #endif
873
874 m = m_gethdr(M_DONTWAIT, MT_DATA);
875 if (m == NULL)
876 return ENOMEM;
877
878 hdr = mtod(m, l2cap_hdr_t *);
879 cmd = (l2cap_cmd_hdr_t *)(hdr + 1);
880
881 m->m_len = m->m_pkthdr.len = MHLEN;
882
883 /* Command Data */
884 if (length > 0)
885 m_copyback(m, sizeof(hdr) + sizeof(cmd), length, data);
886
887 /* Command Header */
888 cmd->code = code;
889 cmd->ident = ident;
890 cmd->length = htole16(length);
891 length += sizeof(cmd);
892
893 /* C-Frame Header */
894 hdr->length = htole16(length);
895 hdr->dcid = htole16(L2CAP_SIGNAL_CID);
896 length += sizeof(hdr);
897
898 if (m->m_pkthdr.len != MAX(MHLEN, length)) {
899 m_freem(m);
900 return ENOMEM;
901 }
902
903 m->m_pkthdr.len = length;
904 m->m_len = MIN(length, MHLEN);
905
906 DPRINTFN(2, "(%s) code %d, ident %d, len %d\n",
907 link->hl_unit->hci_devname, code, ident, length);
908
909 return hci_acl_send(m, link, NULL);
910 }
911
912 /*
913 * Send Command Reject packet.
914 */
915 static int
916 l2cap_send_command_rej(struct hci_link *link, uint8_t ident,
917 uint16_t reason, ...)
918 {
919 l2cap_cmd_rej_cp cp;
920 int len = 0;
921 va_list ap;
922
923 va_start(ap, reason);
924
925 cp.reason = htole16(reason);
926
927 switch (reason) {
928 case L2CAP_REJ_NOT_UNDERSTOOD:
929 len = 2;
930 break;
931
932 case L2CAP_REJ_MTU_EXCEEDED:
933 len = 4;
934 cp.data[0] = va_arg(ap, int); /* SigMTU */
935 cp.data[0] = htole16(cp.data[0]);
936 break;
937
938 case L2CAP_REJ_INVALID_CID:
939 len = 6;
940 cp.data[0] = va_arg(ap, int); /* dcid */
941 cp.data[0] = htole16(cp.data[0]);
942 cp.data[1] = va_arg(ap, int); /* scid */
943 cp.data[1] = htole16(cp.data[1]);
944 break;
945
946 default:
947 UNKNOWN(reason);
948 return EINVAL;
949 }
950
951 va_end(ap);
952
953 return l2cap_send_signal(link, L2CAP_COMMAND_REJ, ident, len, &cp);
954 }
955
956 /*
957 * Send Connect Request
958 */
959 int
960 l2cap_send_connect_req(struct l2cap_channel *chan)
961 {
962 l2cap_con_req_cp cp;
963 int err;
964
965 err = l2cap_request_alloc(chan, L2CAP_CONNECT_REQ);
966 if (err)
967 return err;
968
969 cp.psm = htole16(chan->lc_raddr.bt_psm);
970 cp.scid = htole16(chan->lc_lcid);
971
972 return l2cap_send_signal(chan->lc_link, L2CAP_CONNECT_REQ,
973 chan->lc_link->hl_lastid, sizeof(cp), &cp);
974 }
975
976 /*
977 * Send Config Request
978 *
979 * For outgoing config request, we only put options in the packet if they
980 * differ from the default and would have to be actioned. We dont support
981 * enough option types to make overflowing SigMTU an issue so it can all
982 * go in one packet.
983 */
984 int
985 l2cap_send_config_req(struct l2cap_channel *chan)
986 {
987 l2cap_cfg_req_cp *cp;
988 l2cap_cfg_opt_t *opt;
989 l2cap_cfg_opt_val_t *val;
990 uint8_t *next, buf[L2CAP_MTU_MINIMUM];
991 int err;
992
993 err = l2cap_request_alloc(chan, L2CAP_CONFIG_REQ);
994 if (err)
995 return err;
996
997 /* Config Header (4 octets) */
998 cp = (l2cap_cfg_req_cp *)buf;
999 cp->dcid = htole16(chan->lc_rcid);
1000 cp->flags = 0; /* "No Continuation" */
1001
1002 next = buf + sizeof(l2cap_cfg_req_cp);
1003
1004 /* Incoming MTU (4 octets) */
1005 if (chan->lc_imtu != L2CAP_MTU_DEFAULT) {
1006 opt = (l2cap_cfg_opt_t *)next;
1007 opt->type = L2CAP_OPT_MTU;
1008 opt->length = L2CAP_OPT_MTU_SIZE;
1009
1010 val = (l2cap_cfg_opt_val_t *)(opt + 1);
1011 val->mtu = htole16(chan->lc_imtu);
1012
1013 next += sizeof(l2cap_cfg_opt_t) + L2CAP_OPT_MTU_SIZE;
1014 }
1015
1016 /* Flush Timeout (4 octets) */
1017 if (chan->lc_flush != L2CAP_FLUSH_TIMO_DEFAULT) {
1018 opt = (l2cap_cfg_opt_t *)next;
1019 opt->type = L2CAP_OPT_FLUSH_TIMO;
1020 opt->length = L2CAP_OPT_FLUSH_TIMO_SIZE;
1021
1022 val = (l2cap_cfg_opt_val_t *)(opt + 1);
1023 val->flush_timo = htole16(chan->lc_flush);
1024
1025 next += sizeof(l2cap_cfg_opt_t) + L2CAP_OPT_FLUSH_TIMO_SIZE;
1026 }
1027
1028 /* Outgoing QoS Flow (24 octets) */
1029 /* Retransmission & Flow Control (11 octets) */
1030 /*
1031 * From here we need to start paying attention to SigMTU as we have
1032 * possibly overflowed the minimum supported..
1033 */
1034
1035 return l2cap_send_signal(chan->lc_link, L2CAP_CONFIG_REQ,
1036 chan->lc_link->hl_lastid, (int)(next - buf), buf);
1037 }
1038
1039 /*
1040 * Send Disconnect Request
1041 */
1042 int
1043 l2cap_send_disconnect_req(struct l2cap_channel *chan)
1044 {
1045 l2cap_discon_req_cp cp;
1046 int err;
1047
1048 err = l2cap_request_alloc(chan, L2CAP_DISCONNECT_REQ);
1049 if (err)
1050 return err;
1051
1052 cp.dcid = htole16(chan->lc_rcid);
1053 cp.scid = htole16(chan->lc_lcid);
1054
1055 return l2cap_send_signal(chan->lc_link, L2CAP_DISCONNECT_REQ,
1056 chan->lc_link->hl_lastid, sizeof(cp), &cp);
1057 }
1058