1 /* $OpenBSD: l2cap_signal.c,v 1.2 2007/07/22 21:05:00 gwk Exp $ */
2 /* $NetBSD: l2cap_signal.c,v 1.8 2007/05/16 18:34:49 plunky Exp $ */
3 /* $DragonFly: src/sys/netbt/l2cap_signal.c,v 1.1 2007/12/30 20:02:56 hasso Exp $ */
6 * Copyright (c) 2005 Iain Hibbert.
7 * Copyright (c) 2006 Itronix Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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/cdefs.h>
39 #include <sys/param.h>
40 #include <sys/kernel.h>
43 #include <sys/queue.h>
44 #include <sys/systm.h>
45 #include <sys/endian.h>
47 #include <netbt/bluetooth.h>
48 #include <netbt/hci.h>
49 #include <netbt/l2cap.h>
51 /*******************************************************************************
53 * L2CAP Signal processing
56 static void l2cap_recv_command_rej(struct mbuf
*, struct hci_link
*);
57 static void l2cap_recv_connect_req(struct mbuf
*, struct hci_link
*);
58 static void l2cap_recv_connect_rsp(struct mbuf
*, struct hci_link
*);
59 static void l2cap_recv_config_req(struct mbuf
*, struct hci_link
*);
60 static void l2cap_recv_config_rsp(struct mbuf
*, struct hci_link
*);
61 static void l2cap_recv_disconnect_req(struct mbuf
*, struct hci_link
*);
62 static void l2cap_recv_disconnect_rsp(struct mbuf
*, struct hci_link
*);
63 static void l2cap_recv_info_req(struct mbuf
*, struct hci_link
*);
64 static int l2cap_send_signal(struct hci_link
*, uint8_t, uint8_t, uint16_t, void *);
65 static int l2cap_send_command_rej(struct hci_link
*, uint8_t, uint16_t, ...);
68 * process incoming signal packets (CID 0x0001). Can contain multiple
72 l2cap_recv_signal(struct mbuf
*m
, struct hci_link
*link
)
77 if (m
->m_pkthdr
.len
== 0)
80 if (m
->m_pkthdr
.len
< sizeof(cmd
))
83 m_copydata(m
, 0, sizeof(cmd
), (caddr_t
)&cmd
);
84 cmd
.length
= letoh16(cmd
.length
);
86 if (m
->m_pkthdr
.len
< sizeof(cmd
) + cmd
.length
)
89 DPRINTFN(2, "(%s) code %d, ident %d, len %d\n",
90 link
->hl_unit
->hci_devname
,
91 cmd
.code
, cmd
.ident
, cmd
.length
);
94 case L2CAP_COMMAND_REJ
:
95 if (cmd
.length
> sizeof(l2cap_cmd_rej_cp
))
98 l2cap_recv_command_rej(m
, link
);
101 case L2CAP_CONNECT_REQ
:
102 if (cmd
.length
!= sizeof(l2cap_con_req_cp
))
105 l2cap_recv_connect_req(m
, link
);
108 case L2CAP_CONNECT_RSP
:
109 if (cmd
.length
!= sizeof(l2cap_con_rsp_cp
))
112 l2cap_recv_connect_rsp(m
, link
);
115 case L2CAP_CONFIG_REQ
:
116 l2cap_recv_config_req(m
, link
);
119 case L2CAP_CONFIG_RSP
:
120 l2cap_recv_config_rsp(m
, link
);
123 case L2CAP_DISCONNECT_REQ
:
124 if (cmd
.length
!= sizeof(l2cap_discon_req_cp
))
127 l2cap_recv_disconnect_req(m
, link
);
130 case L2CAP_DISCONNECT_RSP
:
131 if (cmd
.length
!= sizeof(l2cap_discon_rsp_cp
))
134 l2cap_recv_disconnect_rsp(m
, link
);
138 m_adj(m
, sizeof(cmd
) + cmd
.length
);
139 l2cap_send_signal(link
, L2CAP_ECHO_RSP
, cmd
.ident
,
144 m_adj(m
, sizeof(cmd
) + cmd
.length
);
148 if (cmd
.length
!= sizeof(l2cap_info_req_cp
))
151 l2cap_recv_info_req(m
, link
);
155 m_adj(m
, sizeof(cmd
) + cmd
.length
);
164 panic("impossible!");
168 l2cap_send_command_rej(link
, cmd
.ident
, L2CAP_REJ_NOT_UNDERSTOOD
);
174 * Process Received Command Reject. For now we dont try to recover gracefully
175 * from this, it probably means that the link is garbled or the other end is
176 * insufficiently capable of handling normal traffic. (not *my* fault, no way!)
179 l2cap_recv_command_rej(struct mbuf
*m
, struct hci_link
*link
)
181 struct l2cap_req
*req
;
182 struct l2cap_channel
*chan
;
186 m_copydata(m
, 0, sizeof(cmd
), (caddr_t
)&cmd
);
187 m_adj(m
, sizeof(cmd
));
189 cmd
.length
= letoh16(cmd
.length
);
191 m_copydata(m
, 0, cmd
.length
, (caddr_t
)&cp
);
192 m_adj(m
, cmd
.length
);
194 req
= l2cap_request_lookup(link
, cmd
.ident
);
198 switch (letoh16(cp
.reason
)) {
199 case L2CAP_REJ_NOT_UNDERSTOOD
:
201 * I dont know what to do, just move up the timeout
203 callout_reset(&req
->lr_rtx
,0,l2cap_rtx
,req
);
206 case L2CAP_REJ_MTU_EXCEEDED
:
208 * I didnt send any commands over L2CAP_MTU_MINIMUM size, but..
210 * XXX maybe we should resend this, instead?
212 link
->hl_mtu
= letoh16(cp
.data
[0]);
213 callout_reset(&req
->lr_rtx
,0,l2cap_rtx
,req
);
216 case L2CAP_REJ_INVALID_CID
:
218 * Well, if they dont have such a channel then our channel is
219 * most likely closed. Make it so.
222 l2cap_request_free(req
);
223 if (chan
!= NULL
&& chan
->lc_state
!= L2CAP_CLOSED
)
224 l2cap_close(chan
, ECONNABORTED
);
229 UNKNOWN(letoh16(cp
.reason
));
235 * Process Received Connect Request. Find listening channel matching
236 * psm & addr and ask upper layer for a new channel.
239 l2cap_recv_connect_req(struct mbuf
*m
, struct hci_link
*link
)
241 struct sockaddr_bt laddr
, raddr
;
242 struct l2cap_channel
*chan
, *new;
248 m_copydata(m
, 0, sizeof(cmd
), (caddr_t
)&cmd
);
249 m_adj(m
, sizeof(cmd
));
251 /* extract request */
252 m_copydata(m
, 0, sizeof(cp
), (caddr_t
)&cp
);
253 m_adj(m
, sizeof(cp
));
255 cp
.scid
= letoh16(cp
.scid
);
256 cp
.psm
= letoh16(cp
.psm
);
258 memset(&laddr
, 0, sizeof(struct sockaddr_bt
));
259 laddr
.bt_len
= sizeof(struct sockaddr_bt
);
260 laddr
.bt_family
= AF_BLUETOOTH
;
261 laddr
.bt_psm
= cp
.psm
;
262 bdaddr_copy(&laddr
.bt_bdaddr
, &link
->hl_unit
->hci_bdaddr
);
264 memset(&raddr
, 0, sizeof(struct sockaddr_bt
));
265 raddr
.bt_len
= sizeof(struct sockaddr_bt
);
266 raddr
.bt_family
= AF_BLUETOOTH
;
267 raddr
.bt_psm
= cp
.psm
;
268 bdaddr_copy(&raddr
.bt_bdaddr
, &link
->hl_bdaddr
);
270 LIST_FOREACH(chan
, &l2cap_listen_list
, lc_ncid
) {
271 if (chan
->lc_laddr
.bt_psm
!= laddr
.bt_psm
272 && chan
->lc_laddr
.bt_psm
!= L2CAP_PSM_ANY
)
275 if (!bdaddr_same(&laddr
.bt_bdaddr
, &chan
->lc_laddr
.bt_bdaddr
)
276 && bdaddr_any(&chan
->lc_laddr
.bt_bdaddr
) == 0)
279 new= (*chan
->lc_proto
->newconn
)(chan
->lc_upper
, &laddr
, &raddr
);
283 err
= l2cap_cid_alloc(new);
285 l2cap_send_connect_rsp(link
, cmd
.ident
,
289 (*new->lc_proto
->disconnected
)(new->lc_upper
, err
);
293 new->lc_link
= hci_acl_open(link
->hl_unit
, &link
->hl_bdaddr
);
294 KKASSERT(new->lc_link
== link
);
296 new->lc_rcid
= cp
.scid
;
297 new->lc_ident
= cmd
.ident
;
299 memcpy(&new->lc_laddr
, &laddr
, sizeof(struct sockaddr_bt
));
300 memcpy(&new->lc_raddr
, &raddr
, sizeof(struct sockaddr_bt
));
302 new->lc_mode
= chan
->lc_mode
;
304 err
= l2cap_setmode(new);
305 if (err
== EINPROGRESS
) {
306 new->lc_state
= L2CAP_WAIT_SEND_CONNECT_RSP
;
307 (*new->lc_proto
->connecting
)(new->lc_upper
);
311 new->lc_state
= L2CAP_CLOSED
;
312 hci_acl_close(link
, err
);
315 l2cap_send_connect_rsp(link
, cmd
.ident
,
319 (*new->lc_proto
->disconnected
)(new->lc_upper
, err
);
323 err
= l2cap_send_connect_rsp(link
, cmd
.ident
,
324 new->lc_lcid
, new->lc_rcid
,
327 l2cap_close(new, err
);
331 new->lc_state
= L2CAP_WAIT_CONFIG
;
332 new->lc_flags
|= (L2CAP_WAIT_CONFIG_REQ
| L2CAP_WAIT_CONFIG_RSP
);
333 err
= l2cap_send_config_req(new);
335 l2cap_close(new, err
);
340 l2cap_send_connect_rsp(link
, cmd
.ident
,
342 L2CAP_PSM_NOT_SUPPORTED
);
346 * Process Received Connect Response.
349 l2cap_recv_connect_rsp(struct mbuf
*m
, struct hci_link
*link
)
353 struct l2cap_req
*req
;
354 struct l2cap_channel
*chan
;
356 m_copydata(m
, 0, sizeof(cmd
), (caddr_t
)&cmd
);
357 m_adj(m
, sizeof(cmd
));
359 m_copydata(m
, 0, sizeof(cp
), (caddr_t
)&cp
);
360 m_adj(m
, sizeof(cp
));
362 cp
.scid
= letoh16(cp
.scid
);
363 cp
.dcid
= letoh16(cp
.dcid
);
364 cp
.result
= letoh16(cp
.result
);
366 req
= l2cap_request_lookup(link
, cmd
.ident
);
367 if (req
== NULL
|| req
->lr_code
!= L2CAP_CONNECT_REQ
)
371 if (chan
!= NULL
&& chan
->lc_lcid
!= cp
.scid
)
374 if (chan
== NULL
|| chan
->lc_state
!= L2CAP_WAIT_RECV_CONNECT_RSP
) {
375 l2cap_request_free(req
);
382 * Ok, at this point we have a connection to the other party. We
383 * could indicate upstream that we are ready for business and
384 * wait for a "Configure Channel Request" but I'm not so sure
385 * that is required in our case - we will proceed directly to
386 * sending our config request. We set two state bits because in
387 * the config state we are waiting for requests and responses.
389 l2cap_request_free(req
);
390 chan
->lc_rcid
= cp
.dcid
;
391 chan
->lc_state
= L2CAP_WAIT_CONFIG
;
392 chan
->lc_flags
|= (L2CAP_WAIT_CONFIG_REQ
| L2CAP_WAIT_CONFIG_RSP
);
393 l2cap_send_config_req(chan
);
397 /* XXX dont release request, should start eRTX timeout? */
398 (*chan
->lc_proto
->connecting
)(chan
->lc_upper
);
401 case L2CAP_PSM_NOT_SUPPORTED
:
402 case L2CAP_SECURITY_BLOCK
:
403 case L2CAP_NO_RESOURCES
:
405 l2cap_request_free(req
);
406 l2cap_close(chan
, ECONNREFUSED
);
412 * Process Received Config Reqest.
415 l2cap_recv_config_req(struct mbuf
*m
, struct hci_link
*link
)
417 uint8_t buf
[L2CAP_MTU_MINIMUM
];
421 l2cap_cfg_opt_val_t val
;
423 struct l2cap_channel
*chan
;
426 m_copydata(m
, 0, sizeof(cmd
), (caddr_t
)&cmd
);
427 m_adj(m
, sizeof(cmd
));
428 left
= letoh16(cmd
.length
);
430 if (left
< sizeof(cp
))
433 m_copydata(m
, 0, sizeof(cp
), (caddr_t
)&cp
);
434 m_adj(m
, sizeof(cp
));
437 cp
.dcid
= letoh16(cp
.dcid
);
438 cp
.flags
= letoh16(cp
.flags
);
440 chan
= l2cap_cid_lookup(cp
.dcid
);
441 if (chan
== NULL
|| chan
->lc_link
!= link
442 || chan
->lc_state
!= L2CAP_WAIT_CONFIG
443 || (chan
->lc_flags
& L2CAP_WAIT_CONFIG_REQ
) == 0) {
444 /* XXX we should really accept reconfiguration requests */
445 l2cap_send_command_rej(link
, cmd
.ident
, L2CAP_REJ_INVALID_CID
,
446 L2CAP_NULL_CID
, cp
.dcid
);
450 /* ready our response packet */
451 rp
.scid
= htole16(chan
->lc_rcid
);
452 rp
.flags
= 0; /* "No Continuation" */
453 rp
.result
= L2CAP_SUCCESS
;
457 * Process the packet. We build the return packet on the fly adding any
458 * unacceptable parameters as we go. As we can only return one result,
459 * unknown option takes precedence so we start our return packet anew
460 * and ignore option values thereafter as they will be re-sent.
462 * Since we do not support enough options to make overflowing the min
463 * MTU size an issue in normal use, we just reject config requests that
464 * make that happen. This could be because options are repeated or the
465 * packet is corrupted in some way.
467 * If unknown option types threaten to overflow the packet, we just
468 * ignore them. We can deny them next time.
471 if (left
< sizeof(opt
))
474 m_copydata(m
, 0, sizeof(opt
), (caddr_t
)&opt
);
475 m_adj(m
, sizeof(opt
));
478 if (left
< opt
.length
)
481 switch(opt
.type
& L2CAP_OPT_HINT_MASK
) {
483 if (rp
.result
== L2CAP_UNKNOWN_OPTION
)
486 if (opt
.length
!= L2CAP_OPT_MTU_SIZE
)
489 m_copydata(m
, 0, L2CAP_OPT_MTU_SIZE
, (caddr_t
)&val
);
490 val
.mtu
= letoh16(val
.mtu
);
493 * XXX how do we know what the minimum acceptable MTU is
494 * for a channel? Spec says some profiles have a higher
495 * minimum but I have no way to find that out at this
498 if (val
.mtu
< L2CAP_MTU_MINIMUM
) {
499 if (len
+ sizeof(opt
) + L2CAP_OPT_MTU_SIZE
> sizeof(buf
))
502 rp
.result
= L2CAP_UNACCEPTABLE_PARAMS
;
503 memcpy(buf
+ len
, &opt
, sizeof(opt
));
505 val
.mtu
= htole16(L2CAP_MTU_MINIMUM
);
506 memcpy(buf
+ len
, &val
, L2CAP_OPT_MTU_SIZE
);
507 len
+= L2CAP_OPT_MTU_SIZE
;
509 chan
->lc_omtu
= val
.mtu
;
513 case L2CAP_OPT_FLUSH_TIMO
:
514 if (rp
.result
== L2CAP_UNKNOWN_OPTION
)
517 if (opt
.length
!= L2CAP_OPT_FLUSH_TIMO_SIZE
)
521 * I think that this is informational only - he is
522 * informing us of the flush timeout he will be using.
523 * I dont think this affects us in any significant way,
524 * so just ignore this value for now.
531 if (opt
.type
& L2CAP_OPT_HINT_BIT
)
534 /* unknown options supercede all else */
535 if (rp
.result
!= L2CAP_UNKNOWN_OPTION
) {
536 rp
.result
= L2CAP_UNKNOWN_OPTION
;
540 /* ignore if it don't fit */
541 if (len
+ sizeof(opt
) > sizeof(buf
))
544 /* return unknown option type, but no data */
545 buf
[len
++] = opt
.type
;
550 m_adj(m
, opt
.length
);
554 rp
.result
= htole16(rp
.result
);
555 memcpy(buf
, &rp
, sizeof(rp
));
556 l2cap_send_signal(link
, L2CAP_CONFIG_RSP
, cmd
.ident
, len
, buf
);
558 if ((cp
.flags
& L2CAP_OPT_CFLAG_BIT
) == 0
559 && rp
.result
== letoh16(L2CAP_SUCCESS
)) {
561 chan
->lc_flags
&= ~L2CAP_WAIT_CONFIG_REQ
;
563 if ((chan
->lc_flags
& L2CAP_WAIT_CONFIG_RSP
) == 0) {
564 chan
->lc_state
= L2CAP_OPEN
;
565 /* XXX how to distinguish REconfiguration? */
566 (*chan
->lc_proto
->connected
)(chan
->lc_upper
);
572 l2cap_send_command_rej(link
, cmd
.ident
, L2CAP_REJ_NOT_UNDERSTOOD
);
578 * Process Received Config Response.
581 l2cap_recv_config_rsp(struct mbuf
*m
, struct hci_link
*link
)
586 l2cap_cfg_opt_val_t val
;
587 struct l2cap_req
*req
;
588 struct l2cap_channel
*chan
;
591 m_copydata(m
, 0, sizeof(cmd
), (caddr_t
)&cmd
);
592 m_adj(m
, sizeof(cmd
));
593 left
= letoh16(cmd
.length
);
595 if (left
< sizeof(cp
))
598 m_copydata(m
, 0, sizeof(cp
), (caddr_t
)&cp
);
599 m_adj(m
, sizeof(cp
));
602 cp
.scid
= letoh16(cp
.scid
);
603 cp
.flags
= letoh16(cp
.flags
);
604 cp
.result
= letoh16(cp
.result
);
606 req
= l2cap_request_lookup(link
, cmd
.ident
);
607 if (req
== NULL
|| req
->lr_code
!= L2CAP_CONFIG_REQ
)
611 if (chan
!= NULL
&& chan
->lc_lcid
!= cp
.scid
)
614 l2cap_request_free(req
);
616 if (chan
== NULL
|| chan
->lc_state
!= L2CAP_WAIT_CONFIG
617 || (chan
->lc_flags
& L2CAP_WAIT_CONFIG_RSP
) == 0)
620 if ((cp
.flags
& L2CAP_OPT_CFLAG_BIT
)) {
624 * They have more to tell us and want another ID to
625 * use, so send an empty config request
627 if (l2cap_request_alloc(chan
, L2CAP_CONFIG_REQ
))
630 rp
.dcid
= htole16(cp
.scid
);
633 if (l2cap_send_signal(link
, L2CAP_CONFIG_REQ
, link
->hl_lastid
,
641 * If continuation flag was not set, our config request was
642 * accepted. We may have to wait for their config request to
643 * complete, so check that but otherwise we are open
645 * There may be 'advisory' values in the packet but we just
648 if ((cp
.flags
& L2CAP_OPT_CFLAG_BIT
) == 0) {
649 chan
->lc_flags
&= ~L2CAP_WAIT_CONFIG_RSP
;
651 if ((chan
->lc_flags
& L2CAP_WAIT_CONFIG_REQ
) == 0) {
652 chan
->lc_state
= L2CAP_OPEN
;
653 /* XXX how to distinguish REconfiguration? */
654 (*chan
->lc_proto
->connected
)(chan
->lc_upper
);
659 case L2CAP_UNACCEPTABLE_PARAMS
:
661 * Packet contains unacceptable parameters with preferred values
664 if (left
< sizeof(opt
))
667 m_copydata(m
, 0, sizeof(opt
), (caddr_t
)&opt
);
668 m_adj(m
, sizeof(opt
));
671 if (left
< opt
.length
)
676 if (opt
.length
!= L2CAP_OPT_MTU_SIZE
)
679 m_copydata(m
, 0, L2CAP_OPT_MTU_SIZE
, (caddr_t
)&val
);
680 chan
->lc_imtu
= letoh16(val
.mtu
);
681 if (chan
->lc_imtu
< L2CAP_MTU_MINIMUM
)
682 chan
->lc_imtu
= L2CAP_MTU_DEFAULT
;
685 case L2CAP_OPT_FLUSH_TIMO
:
686 if (opt
.length
!= L2CAP_OPT_FLUSH_TIMO_SIZE
)
690 * Spec says: If we cannot honor proposed value,
691 * either disconnect or try again with original
692 * value. I can't really see why they want to
693 * interfere with OUR flush timeout in any case
694 * so we just punt for now.
706 m_adj(m
, opt
.length
);
710 if ((cp
.flags
& L2CAP_OPT_CFLAG_BIT
) == 0)
711 l2cap_send_config_req(chan
); /* no state change */
718 case L2CAP_UNKNOWN_OPTION
:
720 * Packet contains options not understood. Turn off unknown
721 * options by setting them to default values (means they will
722 * not be requested again).
724 * If our option was already off then fail (paranoia?)
726 * XXX Should we consider that options were set for a reason?
729 if (left
< sizeof(opt
))
732 m_copydata(m
, 0, sizeof(opt
), (caddr_t
)&opt
);
733 m_adj(m
, sizeof(opt
));
736 if (left
< opt
.length
)
739 m_adj(m
, opt
.length
);
744 if (chan
->lc_imtu
== L2CAP_MTU_DEFAULT
)
747 chan
->lc_imtu
= L2CAP_MTU_DEFAULT
;
750 case L2CAP_OPT_FLUSH_TIMO
:
751 if (chan
->lc_flush
== L2CAP_FLUSH_TIMO_DEFAULT
)
754 chan
->lc_flush
= L2CAP_FLUSH_TIMO_DEFAULT
;
766 if ((cp
.flags
& L2CAP_OPT_CFLAG_BIT
) == 0)
767 l2cap_send_config_req(chan
); /* no state change */
776 DPRINTF("how did I get here!?\n");
779 l2cap_send_disconnect_req(chan
);
780 l2cap_close(chan
, ECONNABORTED
);
787 * Process Received Disconnect Request. We must validate scid and dcid
788 * just in case but otherwise this connection is finished.
791 l2cap_recv_disconnect_req(struct mbuf
*m
, struct hci_link
*link
)
794 l2cap_discon_req_cp cp
;
795 l2cap_discon_rsp_cp rp
;
796 struct l2cap_channel
*chan
;
798 m_copydata(m
, 0, sizeof(cmd
), (caddr_t
)&cmd
);
799 m_adj(m
, sizeof(cmd
));
801 m_copydata(m
, 0, sizeof(cp
), (caddr_t
)&cp
);
802 m_adj(m
, sizeof(cp
));
804 cp
.scid
= letoh16(cp
.scid
);
805 cp
.dcid
= letoh16(cp
.dcid
);
807 chan
= l2cap_cid_lookup(cp
.dcid
);
808 if (chan
== NULL
|| chan
->lc_link
!= link
|| chan
->lc_rcid
!= cp
.scid
) {
809 l2cap_send_command_rej(link
, cmd
.ident
, L2CAP_REJ_INVALID_CID
,
814 rp
.dcid
= htole16(chan
->lc_lcid
);
815 rp
.scid
= htole16(chan
->lc_rcid
);
816 l2cap_send_signal(link
, L2CAP_DISCONNECT_RSP
, cmd
.ident
,
819 if (chan
->lc_state
!= L2CAP_CLOSED
)
820 l2cap_close(chan
, ECONNRESET
);
824 * Process Received Disconnect Response. We must validate scid and dcid but
825 * unless we were waiting for this signal, ignore it.
828 l2cap_recv_disconnect_rsp(struct mbuf
*m
, struct hci_link
*link
)
831 l2cap_discon_rsp_cp cp
;
832 struct l2cap_req
*req
;
833 struct l2cap_channel
*chan
;
835 m_copydata(m
, 0, sizeof(cmd
), (caddr_t
)&cmd
);
836 m_adj(m
, sizeof(cmd
));
838 m_copydata(m
, 0, sizeof(cp
), (caddr_t
)&cp
);
839 m_adj(m
, sizeof(cp
));
841 cp
.scid
= letoh16(cp
.scid
);
842 cp
.dcid
= letoh16(cp
.dcid
);
844 req
= l2cap_request_lookup(link
, cmd
.ident
);
845 if (req
== NULL
|| req
->lr_code
!= L2CAP_DISCONNECT_REQ
)
850 || chan
->lc_lcid
!= cp
.scid
851 || chan
->lc_rcid
!= cp
.dcid
)
854 l2cap_request_free(req
);
856 if (chan
->lc_state
!= L2CAP_WAIT_DISCONNECT
)
859 l2cap_close(chan
, 0);
863 * Process Received Info Request. We must respond but alas dont
864 * support anything as yet so thats easy.
867 l2cap_recv_info_req(struct mbuf
*m
, struct hci_link
*link
)
870 l2cap_info_req_cp cp
;
871 l2cap_info_rsp_cp rp
;
873 m_copydata(m
, 0, sizeof(cmd
), (caddr_t
)&cmd
);
874 m_adj(m
, sizeof(cmd
));
876 m_copydata(m
, 0, sizeof(cp
), (caddr_t
)&cp
);
877 m_adj(m
, sizeof(cp
));
879 switch(letoh16(cp
.type
)) {
880 case L2CAP_CONNLESS_MTU
:
881 case L2CAP_EXTENDED_FEATURES
:
884 rp
.result
= htole16(L2CAP_NOT_SUPPORTED
);
886 l2cap_send_signal(link
, L2CAP_INFO_RSP
, cmd
.ident
,
893 * Construct signal and wrap in C-Frame for link.
896 l2cap_send_signal(struct hci_link
*link
, uint8_t code
, uint8_t ident
,
897 uint16_t length
, void *data
)
901 l2cap_cmd_hdr_t
*cmd
;
907 if (sizeof(l2cap_cmd_hdr_t
) + length
> link
->hl_mtu
)
908 kprintf("(%s) exceeding L2CAP Signal MTU for link!\n",
909 link
->hl_unit
->hci_devname
);
912 m
= m_gethdr(MB_DONTWAIT
, MT_DATA
);
916 hdr
= mtod(m
, l2cap_hdr_t
*);
917 cmd
= (l2cap_cmd_hdr_t
*)(hdr
+ 1);
919 m
->m_len
= m
->m_pkthdr
.len
= MHLEN
;
923 m_copyback(m
, sizeof(*hdr
) + sizeof(*cmd
), length
, data
);
928 cmd
->length
= htole16(length
);
929 length
+= sizeof(*cmd
);
932 hdr
->length
= htole16(length
);
933 hdr
->dcid
= htole16(L2CAP_SIGNAL_CID
);
934 length
+= sizeof(*hdr
);
936 if (m
->m_pkthdr
.len
!= MAX(MHLEN
, length
)) {
941 m
->m_pkthdr
.len
= length
;
942 m
->m_len
= MIN(length
, MHLEN
);
944 DPRINTFN(2, "(%s) code %d, ident %d, len %d\n",
945 link
->hl_unit
->hci_devname
, code
, ident
, length
);
947 return hci_acl_send(m
, link
, NULL
);
951 * Send Command Reject packet.
954 l2cap_send_command_rej(struct hci_link
*link
, uint8_t ident
,
955 uint16_t reason
, ...)
961 va_start(ap
, reason
);
963 cp
.reason
= htole16(reason
);
966 case L2CAP_REJ_NOT_UNDERSTOOD
:
970 case L2CAP_REJ_MTU_EXCEEDED
:
972 cp
.data
[0] = va_arg(ap
, int); /* SigMTU */
973 cp
.data
[0] = htole16(cp
.data
[0]);
976 case L2CAP_REJ_INVALID_CID
:
978 cp
.data
[0] = va_arg(ap
, int); /* dcid */
979 cp
.data
[0] = htole16(cp
.data
[0]);
980 cp
.data
[1] = va_arg(ap
, int); /* scid */
981 cp
.data
[1] = htole16(cp
.data
[1]);
991 return l2cap_send_signal(link
, L2CAP_COMMAND_REJ
, ident
, len
, &cp
);
995 * Send Connect Request
998 l2cap_send_connect_req(struct l2cap_channel
*chan
)
1000 l2cap_con_req_cp cp
;
1003 err
= l2cap_request_alloc(chan
, L2CAP_CONNECT_REQ
);
1007 cp
.psm
= htole16(chan
->lc_raddr
.bt_psm
);
1008 cp
.scid
= htole16(chan
->lc_lcid
);
1010 return l2cap_send_signal(chan
->lc_link
, L2CAP_CONNECT_REQ
,
1011 chan
->lc_link
->hl_lastid
, sizeof(cp
), &cp
);
1015 * Send Config Request
1017 * For outgoing config request, we only put options in the packet if they
1018 * differ from the default and would have to be actioned. We dont support
1019 * enough option types to make overflowing SigMTU an issue so it can all
1023 l2cap_send_config_req(struct l2cap_channel
*chan
)
1025 l2cap_cfg_req_cp
*cp
;
1026 l2cap_cfg_opt_t
*opt
;
1027 l2cap_cfg_opt_val_t
*val
;
1028 uint8_t *next
, buf
[L2CAP_MTU_MINIMUM
];
1031 err
= l2cap_request_alloc(chan
, L2CAP_CONFIG_REQ
);
1035 /* Config Header (4 octets) */
1036 cp
= (l2cap_cfg_req_cp
*)buf
;
1037 cp
->dcid
= htole16(chan
->lc_rcid
);
1038 cp
->flags
= 0; /* "No Continuation" */
1040 next
= buf
+ sizeof(l2cap_cfg_req_cp
);
1042 /* Incoming MTU (4 octets) */
1043 if (chan
->lc_imtu
!= L2CAP_MTU_DEFAULT
) {
1044 opt
= (l2cap_cfg_opt_t
*)next
;
1045 opt
->type
= L2CAP_OPT_MTU
;
1046 opt
->length
= L2CAP_OPT_MTU_SIZE
;
1048 val
= (l2cap_cfg_opt_val_t
*)(opt
+ 1);
1049 val
->mtu
= htole16(chan
->lc_imtu
);
1051 next
+= sizeof(l2cap_cfg_opt_t
) + L2CAP_OPT_MTU_SIZE
;
1054 /* Flush Timeout (4 octets) */
1055 if (chan
->lc_flush
!= L2CAP_FLUSH_TIMO_DEFAULT
) {
1056 opt
= (l2cap_cfg_opt_t
*)next
;
1057 opt
->type
= L2CAP_OPT_FLUSH_TIMO
;
1058 opt
->length
= L2CAP_OPT_FLUSH_TIMO_SIZE
;
1060 val
= (l2cap_cfg_opt_val_t
*)(opt
+ 1);
1061 val
->flush_timo
= htole16(chan
->lc_flush
);
1063 next
+= sizeof(l2cap_cfg_opt_t
) + L2CAP_OPT_FLUSH_TIMO_SIZE
;
1066 /* Outgoing QoS Flow (24 octets) */
1067 /* Retransmission & Flow Control (11 octets) */
1069 * From here we need to start paying attention to SigMTU as we have
1070 * possibly overflowed the minimum supported..
1073 return l2cap_send_signal(chan
->lc_link
, L2CAP_CONFIG_REQ
,
1074 chan
->lc_link
->hl_lastid
, (int)(next
- buf
), buf
);
1078 * Send Disconnect Request
1081 l2cap_send_disconnect_req(struct l2cap_channel
*chan
)
1083 l2cap_discon_req_cp cp
;
1086 err
= l2cap_request_alloc(chan
, L2CAP_DISCONNECT_REQ
);
1090 cp
.dcid
= htole16(chan
->lc_rcid
);
1091 cp
.scid
= htole16(chan
->lc_lcid
);
1093 return l2cap_send_signal(chan
->lc_link
, L2CAP_DISCONNECT_REQ
,
1094 chan
->lc_link
->hl_lastid
, sizeof(cp
), &cp
);
1098 * Send Connect Response
1101 l2cap_send_connect_rsp(struct hci_link
*link
, uint8_t ident
, uint16_t dcid
,
1102 uint16_t scid
, uint16_t result
)
1104 l2cap_con_rsp_cp cp
;
1106 memset(&cp
, 0, sizeof(cp
));
1107 cp
.dcid
= htole16(dcid
);
1108 cp
.scid
= htole16(scid
);
1109 cp
.result
= htole16(result
);
1111 return l2cap_send_signal(link
, L2CAP_CONNECT_RSP
, ident
, sizeof(cp
), &cp
);