1 /* $NetBSD: l2cap_signal.c,v 1.9 2007/11/10 23:12:23 plunky Exp $ */
4 * Copyright (c) 2005 Iain Hibbert.
5 * Copyright (c) 2006 Itronix Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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.
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: l2cap_signal.c,v 1.9 2007/11/10 23:12:23 plunky Exp $");
36 #include <sys/param.h>
37 #include <sys/kernel.h>
40 #include <sys/queue.h>
41 #include <sys/systm.h>
43 #include <machine/stdarg.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
70 l2cap_recv_signal(struct mbuf
*m
, struct hci_link
*link
)
75 if (m
->m_pkthdr
.len
== 0)
78 if (m
->m_pkthdr
.len
< sizeof(cmd
))
81 m_copydata(m
, 0, sizeof(cmd
), &cmd
);
82 cmd
.length
= le16toh(cmd
.length
);
84 if (m
->m_pkthdr
.len
< sizeof(cmd
) + cmd
.length
)
87 DPRINTFN(2, "(%s) code %d, ident %d, len %d\n",
88 device_xname(link
->hl_unit
->hci_dev
),
89 cmd
.code
, cmd
.ident
, cmd
.length
);
92 case L2CAP_COMMAND_REJ
:
93 if (cmd
.length
> sizeof(l2cap_cmd_rej_cp
))
96 l2cap_recv_command_rej(m
, link
);
99 case L2CAP_CONNECT_REQ
:
100 if (cmd
.length
!= sizeof(l2cap_con_req_cp
))
103 l2cap_recv_connect_req(m
, link
);
106 case L2CAP_CONNECT_RSP
:
107 if (cmd
.length
!= sizeof(l2cap_con_rsp_cp
))
110 l2cap_recv_connect_rsp(m
, link
);
113 case L2CAP_CONFIG_REQ
:
114 l2cap_recv_config_req(m
, link
);
117 case L2CAP_CONFIG_RSP
:
118 l2cap_recv_config_rsp(m
, link
);
121 case L2CAP_DISCONNECT_REQ
:
122 if (cmd
.length
!= sizeof(l2cap_discon_req_cp
))
125 l2cap_recv_disconnect_req(m
, link
);
128 case L2CAP_DISCONNECT_RSP
:
129 if (cmd
.length
!= sizeof(l2cap_discon_rsp_cp
))
132 l2cap_recv_disconnect_rsp(m
, link
);
136 m_adj(m
, sizeof(cmd
) + cmd
.length
);
137 l2cap_send_signal(link
, L2CAP_ECHO_RSP
, cmd
.ident
,
142 m_adj(m
, sizeof(cmd
) + cmd
.length
);
146 if (cmd
.length
!= sizeof(l2cap_info_req_cp
))
149 l2cap_recv_info_req(m
, link
);
153 m_adj(m
, sizeof(cmd
) + cmd
.length
);
162 panic("impossible!");
166 l2cap_send_command_rej(link
, cmd
.ident
, L2CAP_REJ_NOT_UNDERSTOOD
);
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!)
177 l2cap_recv_command_rej(struct mbuf
*m
, struct hci_link
*link
)
179 struct l2cap_req
*req
;
180 struct l2cap_channel
*chan
;
184 m_copydata(m
, 0, sizeof(cmd
), &cmd
);
185 m_adj(m
, sizeof(cmd
));
187 cmd
.length
= le16toh(cmd
.length
);
189 m_copydata(m
, 0, cmd
.length
, &cp
);
190 m_adj(m
, cmd
.length
);
192 req
= l2cap_request_lookup(link
, cmd
.ident
);
196 switch (le16toh(cp
.reason
)) {
197 case L2CAP_REJ_NOT_UNDERSTOOD
:
199 * I dont know what to do, just move up the timeout
201 callout_schedule(&req
->lr_rtx
, 0);
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
= le16toh(cp
.data
[0]);
211 callout_schedule(&req
->lr_rtx
, 0);
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.
220 l2cap_request_free(req
);
221 if (chan
!= NULL
&& chan
->lc_state
!= L2CAP_CLOSED
)
222 l2cap_close(chan
, ECONNABORTED
);
227 UNKNOWN(le16toh(cp
.reason
));
233 * Process Received Connect Request. Find listening channel matching
234 * psm & addr and ask upper layer for a new channel.
237 l2cap_recv_connect_req(struct mbuf
*m
, struct hci_link
*link
)
239 struct sockaddr_bt laddr
, raddr
;
240 struct l2cap_channel
*chan
, *new;
246 m_copydata(m
, 0, sizeof(cmd
), &cmd
);
247 m_adj(m
, sizeof(cmd
));
249 /* extract request */
250 m_copydata(m
, 0, sizeof(cp
), &cp
);
251 m_adj(m
, sizeof(cp
));
253 cp
.scid
= le16toh(cp
.scid
);
254 cp
.psm
= le16toh(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
)
272 if (!bdaddr_same(&laddr
.bt_bdaddr
, &chan
->lc_laddr
.bt_bdaddr
)
273 && bdaddr_any(&chan
->lc_laddr
.bt_bdaddr
) == 0)
276 new= (*chan
->lc_proto
->newconn
)(chan
->lc_upper
, &laddr
, &raddr
);
280 err
= l2cap_cid_alloc(new);
282 l2cap_send_connect_rsp(link
, cmd
.ident
,
286 (*new->lc_proto
->disconnected
)(new->lc_upper
, err
);
290 new->lc_link
= hci_acl_open(link
->hl_unit
, &link
->hl_bdaddr
);
291 KASSERT(new->lc_link
== link
);
293 new->lc_rcid
= cp
.scid
;
294 new->lc_ident
= cmd
.ident
;
296 memcpy(&new->lc_laddr
, &laddr
, sizeof(struct sockaddr_bt
));
297 memcpy(&new->lc_raddr
, &raddr
, sizeof(struct sockaddr_bt
));
299 new->lc_mode
= chan
->lc_mode
;
301 err
= l2cap_setmode(new);
302 if (err
== EINPROGRESS
) {
303 new->lc_state
= L2CAP_WAIT_SEND_CONNECT_RSP
;
304 (*new->lc_proto
->connecting
)(new->lc_upper
);
308 new->lc_state
= L2CAP_CLOSED
;
309 hci_acl_close(link
, err
);
312 l2cap_send_connect_rsp(link
, cmd
.ident
,
316 (*new->lc_proto
->disconnected
)(new->lc_upper
, err
);
320 err
= l2cap_send_connect_rsp(link
, cmd
.ident
,
321 new->lc_lcid
, new->lc_rcid
,
324 l2cap_close(new, err
);
328 new->lc_state
= L2CAP_WAIT_CONFIG
;
329 new->lc_flags
|= (L2CAP_WAIT_CONFIG_REQ
| L2CAP_WAIT_CONFIG_RSP
);
330 err
= l2cap_send_config_req(new);
332 l2cap_close(new, err
);
337 l2cap_send_connect_rsp(link
, cmd
.ident
,
339 L2CAP_PSM_NOT_SUPPORTED
);
343 * Process Received Connect Response.
346 l2cap_recv_connect_rsp(struct mbuf
*m
, struct hci_link
*link
)
350 struct l2cap_req
*req
;
351 struct l2cap_channel
*chan
;
353 m_copydata(m
, 0, sizeof(cmd
), &cmd
);
354 m_adj(m
, sizeof(cmd
));
356 m_copydata(m
, 0, sizeof(cp
), &cp
);
357 m_adj(m
, sizeof(cp
));
359 cp
.scid
= le16toh(cp
.scid
);
360 cp
.dcid
= le16toh(cp
.dcid
);
361 cp
.result
= le16toh(cp
.result
);
363 req
= l2cap_request_lookup(link
, cmd
.ident
);
364 if (req
== NULL
|| req
->lr_code
!= L2CAP_CONNECT_REQ
)
368 if (chan
!= NULL
&& chan
->lc_lcid
!= cp
.scid
)
371 if (chan
== NULL
|| chan
->lc_state
!= L2CAP_WAIT_RECV_CONNECT_RSP
) {
372 l2cap_request_free(req
);
379 * Ok, at this point we have a connection to the other party. We
380 * could indicate upstream that we are ready for business and
381 * wait for a "Configure Channel Request" but I'm not so sure
382 * that is required in our case - we will proceed directly to
383 * sending our config request. We set two state bits because in
384 * the config state we are waiting for requests and responses.
386 l2cap_request_free(req
);
387 chan
->lc_rcid
= cp
.dcid
;
388 chan
->lc_state
= L2CAP_WAIT_CONFIG
;
389 chan
->lc_flags
|= (L2CAP_WAIT_CONFIG_REQ
| L2CAP_WAIT_CONFIG_RSP
);
390 l2cap_send_config_req(chan
);
394 /* XXX dont release request, should start eRTX timeout? */
395 (*chan
->lc_proto
->connecting
)(chan
->lc_upper
);
398 case L2CAP_PSM_NOT_SUPPORTED
:
399 case L2CAP_SECURITY_BLOCK
:
400 case L2CAP_NO_RESOURCES
:
402 l2cap_request_free(req
);
403 l2cap_close(chan
, ECONNREFUSED
);
409 * Process Received Config Reqest.
412 l2cap_recv_config_req(struct mbuf
*m
, struct hci_link
*link
)
414 uint8_t buf
[L2CAP_MTU_MINIMUM
];
418 l2cap_cfg_opt_val_t val
;
420 struct l2cap_channel
*chan
;
423 m_copydata(m
, 0, sizeof(cmd
), &cmd
);
424 m_adj(m
, sizeof(cmd
));
425 left
= le16toh(cmd
.length
);
427 if (left
< sizeof(cp
))
430 m_copydata(m
, 0, sizeof(cp
), &cp
);
431 m_adj(m
, sizeof(cp
));
434 cp
.dcid
= le16toh(cp
.dcid
);
435 cp
.flags
= le16toh(cp
.flags
);
437 chan
= l2cap_cid_lookup(cp
.dcid
);
438 if (chan
== NULL
|| chan
->lc_link
!= link
439 || chan
->lc_state
!= L2CAP_WAIT_CONFIG
440 || (chan
->lc_flags
& L2CAP_WAIT_CONFIG_REQ
) == 0) {
441 /* XXX we should really accept reconfiguration requests */
442 l2cap_send_command_rej(link
, cmd
.ident
, L2CAP_REJ_INVALID_CID
,
443 L2CAP_NULL_CID
, cp
.dcid
);
447 /* ready our response packet */
448 rp
.scid
= htole16(chan
->lc_rcid
);
449 rp
.flags
= 0; /* "No Continuation" */
450 rp
.result
= L2CAP_SUCCESS
;
454 * Process the packet. We build the return packet on the fly adding any
455 * unacceptable parameters as we go. As we can only return one result,
456 * unknown option takes precedence so we start our return packet anew
457 * and ignore option values thereafter as they will be re-sent.
459 * Since we do not support enough options to make overflowing the min
460 * MTU size an issue in normal use, we just reject config requests that
461 * make that happen. This could be because options are repeated or the
462 * packet is corrupted in some way.
464 * If unknown option types threaten to overflow the packet, we just
465 * ignore them. We can deny them next time.
468 if (left
< sizeof(opt
))
471 m_copydata(m
, 0, sizeof(opt
), &opt
);
472 m_adj(m
, sizeof(opt
));
475 if (left
< opt
.length
)
478 switch(opt
.type
& L2CAP_OPT_HINT_MASK
) {
480 if (rp
.result
== L2CAP_UNKNOWN_OPTION
)
483 if (opt
.length
!= L2CAP_OPT_MTU_SIZE
)
486 m_copydata(m
, 0, L2CAP_OPT_MTU_SIZE
, &val
);
487 val
.mtu
= le16toh(val
.mtu
);
490 * XXX how do we know what the minimum acceptable MTU is
491 * for a channel? Spec says some profiles have a higher
492 * minimum but I have no way to find that out at this
495 if (val
.mtu
< L2CAP_MTU_MINIMUM
) {
496 if (len
+ sizeof(opt
) + L2CAP_OPT_MTU_SIZE
> sizeof(buf
))
499 rp
.result
= L2CAP_UNACCEPTABLE_PARAMS
;
500 memcpy(buf
+ len
, &opt
, sizeof(opt
));
502 val
.mtu
= htole16(L2CAP_MTU_MINIMUM
);
503 memcpy(buf
+ len
, &val
, L2CAP_OPT_MTU_SIZE
);
504 len
+= L2CAP_OPT_MTU_SIZE
;
506 chan
->lc_omtu
= val
.mtu
;
510 case L2CAP_OPT_FLUSH_TIMO
:
511 if (rp
.result
== L2CAP_UNKNOWN_OPTION
)
514 if (opt
.length
!= L2CAP_OPT_FLUSH_TIMO_SIZE
)
518 * I think that this is informational only - he is
519 * informing us of the flush timeout he will be using.
520 * I dont think this affects us in any significant way,
521 * so just ignore this value for now.
528 if (opt
.type
& L2CAP_OPT_HINT_BIT
)
531 /* unknown options supercede all else */
532 if (rp
.result
!= L2CAP_UNKNOWN_OPTION
) {
533 rp
.result
= L2CAP_UNKNOWN_OPTION
;
537 /* ignore if it don't fit */
538 if (len
+ sizeof(opt
) > sizeof(buf
))
541 /* return unknown option type, but no data */
542 buf
[len
++] = opt
.type
;
547 m_adj(m
, opt
.length
);
551 rp
.result
= htole16(rp
.result
);
552 memcpy(buf
, &rp
, sizeof(rp
));
553 l2cap_send_signal(link
, L2CAP_CONFIG_RSP
, cmd
.ident
, len
, buf
);
555 if ((cp
.flags
& L2CAP_OPT_CFLAG_BIT
) == 0
556 && rp
.result
== le16toh(L2CAP_SUCCESS
)) {
558 chan
->lc_flags
&= ~L2CAP_WAIT_CONFIG_REQ
;
560 if ((chan
->lc_flags
& L2CAP_WAIT_CONFIG_RSP
) == 0) {
561 chan
->lc_state
= L2CAP_OPEN
;
562 /* XXX how to distinguish REconfiguration? */
563 (*chan
->lc_proto
->connected
)(chan
->lc_upper
);
569 l2cap_send_command_rej(link
, cmd
.ident
, L2CAP_REJ_NOT_UNDERSTOOD
);
575 * Process Received Config Response.
578 l2cap_recv_config_rsp(struct mbuf
*m
, struct hci_link
*link
)
583 l2cap_cfg_opt_val_t val
;
584 struct l2cap_req
*req
;
585 struct l2cap_channel
*chan
;
588 m_copydata(m
, 0, sizeof(cmd
), &cmd
);
589 m_adj(m
, sizeof(cmd
));
590 left
= le16toh(cmd
.length
);
592 if (left
< sizeof(cp
))
595 m_copydata(m
, 0, sizeof(cp
), &cp
);
596 m_adj(m
, sizeof(cp
));
599 cp
.scid
= le16toh(cp
.scid
);
600 cp
.flags
= le16toh(cp
.flags
);
601 cp
.result
= le16toh(cp
.result
);
603 req
= l2cap_request_lookup(link
, cmd
.ident
);
604 if (req
== NULL
|| req
->lr_code
!= L2CAP_CONFIG_REQ
)
608 if (chan
!= NULL
&& chan
->lc_lcid
!= cp
.scid
)
611 l2cap_request_free(req
);
613 if (chan
== NULL
|| chan
->lc_state
!= L2CAP_WAIT_CONFIG
614 || (chan
->lc_flags
& L2CAP_WAIT_CONFIG_RSP
) == 0)
617 if ((cp
.flags
& L2CAP_OPT_CFLAG_BIT
)) {
621 * They have more to tell us and want another ID to
622 * use, so send an empty config request
624 if (l2cap_request_alloc(chan
, L2CAP_CONFIG_REQ
))
627 rp
.dcid
= htole16(cp
.scid
);
630 if (l2cap_send_signal(link
, L2CAP_CONFIG_REQ
, link
->hl_lastid
,
638 * If continuation flag was not set, our config request was
639 * accepted. We may have to wait for their config request to
640 * complete, so check that but otherwise we are open
642 * There may be 'advisory' values in the packet but we just
645 if ((cp
.flags
& L2CAP_OPT_CFLAG_BIT
) == 0) {
646 chan
->lc_flags
&= ~L2CAP_WAIT_CONFIG_RSP
;
648 if ((chan
->lc_flags
& L2CAP_WAIT_CONFIG_REQ
) == 0) {
649 chan
->lc_state
= L2CAP_OPEN
;
650 /* XXX how to distinguish REconfiguration? */
651 (*chan
->lc_proto
->connected
)(chan
->lc_upper
);
656 case L2CAP_UNACCEPTABLE_PARAMS
:
658 * Packet contains unacceptable parameters with preferred values
661 if (left
< sizeof(opt
))
664 m_copydata(m
, 0, sizeof(opt
), &opt
);
665 m_adj(m
, sizeof(opt
));
668 if (left
< opt
.length
)
673 if (opt
.length
!= L2CAP_OPT_MTU_SIZE
)
676 m_copydata(m
, 0, L2CAP_OPT_MTU_SIZE
, &val
);
677 chan
->lc_imtu
= le16toh(val
.mtu
);
678 if (chan
->lc_imtu
< L2CAP_MTU_MINIMUM
)
679 chan
->lc_imtu
= L2CAP_MTU_DEFAULT
;
682 case L2CAP_OPT_FLUSH_TIMO
:
683 if (opt
.length
!= L2CAP_OPT_FLUSH_TIMO_SIZE
)
687 * Spec says: If we cannot honor proposed value,
688 * either disconnect or try again with original
689 * value. I can't really see why they want to
690 * interfere with OUR flush timeout in any case
691 * so we just punt for now.
703 m_adj(m
, opt
.length
);
707 if ((cp
.flags
& L2CAP_OPT_CFLAG_BIT
) == 0)
708 l2cap_send_config_req(chan
); /* no state change */
715 case L2CAP_UNKNOWN_OPTION
:
717 * Packet contains options not understood. Turn off unknown
718 * options by setting them to default values (means they will
719 * not be requested again).
721 * If our option was already off then fail (paranoia?)
723 * XXX Should we consider that options were set for a reason?
726 if (left
< sizeof(opt
))
729 m_copydata(m
, 0, sizeof(opt
), &opt
);
730 m_adj(m
, sizeof(opt
));
733 if (left
< opt
.length
)
736 m_adj(m
, opt
.length
);
741 if (chan
->lc_imtu
== L2CAP_MTU_DEFAULT
)
744 chan
->lc_imtu
= L2CAP_MTU_DEFAULT
;
747 case L2CAP_OPT_FLUSH_TIMO
:
748 if (chan
->lc_flush
== L2CAP_FLUSH_TIMO_DEFAULT
)
751 chan
->lc_flush
= L2CAP_FLUSH_TIMO_DEFAULT
;
763 if ((cp
.flags
& L2CAP_OPT_CFLAG_BIT
) == 0)
764 l2cap_send_config_req(chan
); /* no state change */
773 DPRINTF("how did I get here!?\n");
776 l2cap_send_disconnect_req(chan
);
777 l2cap_close(chan
, ECONNABORTED
);
784 * Process Received Disconnect Request. We must validate scid and dcid
785 * just in case but otherwise this connection is finished.
788 l2cap_recv_disconnect_req(struct mbuf
*m
, struct hci_link
*link
)
791 l2cap_discon_req_cp cp
;
792 l2cap_discon_rsp_cp rp
;
793 struct l2cap_channel
*chan
;
795 m_copydata(m
, 0, sizeof(cmd
), &cmd
);
796 m_adj(m
, sizeof(cmd
));
798 m_copydata(m
, 0, sizeof(cp
), &cp
);
799 m_adj(m
, sizeof(cp
));
801 cp
.scid
= le16toh(cp
.scid
);
802 cp
.dcid
= le16toh(cp
.dcid
);
804 chan
= l2cap_cid_lookup(cp
.dcid
);
805 if (chan
== NULL
|| chan
->lc_link
!= link
|| chan
->lc_rcid
!= cp
.scid
) {
806 l2cap_send_command_rej(link
, cmd
.ident
, L2CAP_REJ_INVALID_CID
,
811 rp
.dcid
= htole16(chan
->lc_lcid
);
812 rp
.scid
= htole16(chan
->lc_rcid
);
813 l2cap_send_signal(link
, L2CAP_DISCONNECT_RSP
, cmd
.ident
,
816 if (chan
->lc_state
!= L2CAP_CLOSED
)
817 l2cap_close(chan
, ECONNRESET
);
821 * Process Received Disconnect Response. We must validate scid and dcid but
822 * unless we were waiting for this signal, ignore it.
825 l2cap_recv_disconnect_rsp(struct mbuf
*m
, struct hci_link
*link
)
828 l2cap_discon_rsp_cp cp
;
829 struct l2cap_req
*req
;
830 struct l2cap_channel
*chan
;
832 m_copydata(m
, 0, sizeof(cmd
), &cmd
);
833 m_adj(m
, sizeof(cmd
));
835 m_copydata(m
, 0, sizeof(cp
), &cp
);
836 m_adj(m
, sizeof(cp
));
838 cp
.scid
= le16toh(cp
.scid
);
839 cp
.dcid
= le16toh(cp
.dcid
);
841 req
= l2cap_request_lookup(link
, cmd
.ident
);
842 if (req
== NULL
|| req
->lr_code
!= L2CAP_DISCONNECT_REQ
)
847 || chan
->lc_lcid
!= cp
.scid
848 || chan
->lc_rcid
!= cp
.dcid
)
851 l2cap_request_free(req
);
853 if (chan
->lc_state
!= L2CAP_WAIT_DISCONNECT
)
856 l2cap_close(chan
, 0);
860 * Process Received Info Request. We must respond but alas dont
861 * support anything as yet so thats easy.
864 l2cap_recv_info_req(struct mbuf
*m
, struct hci_link
*link
)
867 l2cap_info_req_cp cp
;
868 l2cap_info_rsp_cp rp
;
870 m_copydata(m
, 0, sizeof(cmd
), &cmd
);
871 m_adj(m
, sizeof(cmd
));
873 m_copydata(m
, 0, sizeof(cp
), &cp
);
874 m_adj(m
, sizeof(cp
));
876 switch(le16toh(cp
.type
)) {
877 case L2CAP_CONNLESS_MTU
:
878 case L2CAP_EXTENDED_FEATURES
:
881 rp
.result
= htole16(L2CAP_NOT_SUPPORTED
);
883 l2cap_send_signal(link
, L2CAP_INFO_RSP
, cmd
.ident
,
890 * Construct signal and wrap in C-Frame for link.
893 l2cap_send_signal(struct hci_link
*link
, uint8_t code
, uint8_t ident
,
894 uint16_t length
, void *data
)
898 l2cap_cmd_hdr_t
*cmd
;
904 if (sizeof(l2cap_cmd_hdr_t
) + length
> link
->hl_mtu
)
905 aprint_error_dev(link
->hl_unit
->hci_dev
,
906 "exceeding L2CAP Signal MTU for link!\n");
909 m
= m_gethdr(M_DONTWAIT
, MT_DATA
);
913 hdr
= mtod(m
, l2cap_hdr_t
*);
914 cmd
= (l2cap_cmd_hdr_t
*)(hdr
+ 1);
916 m
->m_len
= m
->m_pkthdr
.len
= MHLEN
;
920 m_copyback(m
, sizeof(*hdr
) + sizeof(*cmd
), length
, data
);
925 cmd
->length
= htole16(length
);
926 length
+= sizeof(*cmd
);
929 hdr
->length
= htole16(length
);
930 hdr
->dcid
= htole16(L2CAP_SIGNAL_CID
);
931 length
+= sizeof(*hdr
);
933 if (m
->m_pkthdr
.len
!= MAX(MHLEN
, length
)) {
938 m
->m_pkthdr
.len
= length
;
939 m
->m_len
= MIN(length
, MHLEN
);
941 DPRINTFN(2, "(%s) code %d, ident %d, len %d\n",
942 device_xname(link
->hl_unit
->hci_dev
), code
, ident
, length
);
944 return hci_acl_send(m
, link
, NULL
);
948 * Send Command Reject packet.
951 l2cap_send_command_rej(struct hci_link
*link
, uint8_t ident
,
952 uint16_t reason
, ...)
958 va_start(ap
, reason
);
960 cp
.reason
= htole16(reason
);
963 case L2CAP_REJ_NOT_UNDERSTOOD
:
967 case L2CAP_REJ_MTU_EXCEEDED
:
969 cp
.data
[0] = va_arg(ap
, int); /* SigMTU */
970 cp
.data
[0] = htole16(cp
.data
[0]);
973 case L2CAP_REJ_INVALID_CID
:
975 cp
.data
[0] = va_arg(ap
, int); /* dcid */
976 cp
.data
[0] = htole16(cp
.data
[0]);
977 cp
.data
[1] = va_arg(ap
, int); /* scid */
978 cp
.data
[1] = htole16(cp
.data
[1]);
988 return l2cap_send_signal(link
, L2CAP_COMMAND_REJ
, ident
, len
, &cp
);
992 * Send Connect Request
995 l2cap_send_connect_req(struct l2cap_channel
*chan
)
1000 err
= l2cap_request_alloc(chan
, L2CAP_CONNECT_REQ
);
1004 cp
.psm
= htole16(chan
->lc_raddr
.bt_psm
);
1005 cp
.scid
= htole16(chan
->lc_lcid
);
1007 return l2cap_send_signal(chan
->lc_link
, L2CAP_CONNECT_REQ
,
1008 chan
->lc_link
->hl_lastid
, sizeof(cp
), &cp
);
1012 * Send Config Request
1014 * For outgoing config request, we only put options in the packet if they
1015 * differ from the default and would have to be actioned. We dont support
1016 * enough option types to make overflowing SigMTU an issue so it can all
1020 l2cap_send_config_req(struct l2cap_channel
*chan
)
1022 l2cap_cfg_req_cp
*cp
;
1023 l2cap_cfg_opt_t
*opt
;
1024 l2cap_cfg_opt_val_t
*val
;
1025 uint8_t *next
, buf
[L2CAP_MTU_MINIMUM
];
1028 err
= l2cap_request_alloc(chan
, L2CAP_CONFIG_REQ
);
1032 /* Config Header (4 octets) */
1033 cp
= (l2cap_cfg_req_cp
*)buf
;
1034 cp
->dcid
= htole16(chan
->lc_rcid
);
1035 cp
->flags
= 0; /* "No Continuation" */
1037 next
= buf
+ sizeof(l2cap_cfg_req_cp
);
1039 /* Incoming MTU (4 octets) */
1040 if (chan
->lc_imtu
!= L2CAP_MTU_DEFAULT
) {
1041 opt
= (l2cap_cfg_opt_t
*)next
;
1042 opt
->type
= L2CAP_OPT_MTU
;
1043 opt
->length
= L2CAP_OPT_MTU_SIZE
;
1045 val
= (l2cap_cfg_opt_val_t
*)(opt
+ 1);
1046 val
->mtu
= htole16(chan
->lc_imtu
);
1048 next
+= sizeof(l2cap_cfg_opt_t
) + L2CAP_OPT_MTU_SIZE
;
1051 /* Flush Timeout (4 octets) */
1052 if (chan
->lc_flush
!= L2CAP_FLUSH_TIMO_DEFAULT
) {
1053 opt
= (l2cap_cfg_opt_t
*)next
;
1054 opt
->type
= L2CAP_OPT_FLUSH_TIMO
;
1055 opt
->length
= L2CAP_OPT_FLUSH_TIMO_SIZE
;
1057 val
= (l2cap_cfg_opt_val_t
*)(opt
+ 1);
1058 val
->flush_timo
= htole16(chan
->lc_flush
);
1060 next
+= sizeof(l2cap_cfg_opt_t
) + L2CAP_OPT_FLUSH_TIMO_SIZE
;
1063 /* Outgoing QoS Flow (24 octets) */
1064 /* Retransmission & Flow Control (11 octets) */
1066 * From here we need to start paying attention to SigMTU as we have
1067 * possibly overflowed the minimum supported..
1070 return l2cap_send_signal(chan
->lc_link
, L2CAP_CONFIG_REQ
,
1071 chan
->lc_link
->hl_lastid
, (int)(next
- buf
), buf
);
1075 * Send Disconnect Request
1078 l2cap_send_disconnect_req(struct l2cap_channel
*chan
)
1080 l2cap_discon_req_cp cp
;
1083 err
= l2cap_request_alloc(chan
, L2CAP_DISCONNECT_REQ
);
1087 cp
.dcid
= htole16(chan
->lc_rcid
);
1088 cp
.scid
= htole16(chan
->lc_lcid
);
1090 return l2cap_send_signal(chan
->lc_link
, L2CAP_DISCONNECT_REQ
,
1091 chan
->lc_link
->hl_lastid
, sizeof(cp
), &cp
);
1095 * Send Connect Response
1098 l2cap_send_connect_rsp(struct hci_link
*link
, uint8_t ident
, uint16_t dcid
, uint16_t scid
, uint16_t result
)
1100 l2cap_con_rsp_cp cp
;
1102 memset(&cp
, 0, sizeof(cp
));
1103 cp
.dcid
= htole16(dcid
);
1104 cp
.scid
= htole16(scid
);
1105 cp
.result
= htole16(result
);
1107 return l2cap_send_signal(link
, L2CAP_CONNECT_RSP
, ident
, sizeof(cp
), &cp
);