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