Home | History | Annotate | Line # | Download | only in netbt
l2cap_signal.c revision 1.15.8.2
      1  1.15.8.2  jdolecek /*	$NetBSD: l2cap_signal.c,v 1.15.8.2 2017/12/03 11:39:03 jdolecek 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.15.8.2  jdolecek __KERNEL_RCSID(0, "$NetBSD: l2cap_signal.c,v 1.15.8.2 2017/12/03 11:39:03 jdolecek 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 <netbt/bluetooth.h>
     44       1.1   gdamore #include <netbt/hci.h>
     45       1.1   gdamore #include <netbt/l2cap.h>
     46       1.1   gdamore 
     47       1.1   gdamore /*******************************************************************************
     48       1.1   gdamore  *
     49       1.1   gdamore  *	L2CAP Signal processing
     50       1.1   gdamore  */
     51       1.1   gdamore 
     52       1.1   gdamore static void l2cap_recv_command_rej(struct mbuf *, struct hci_link *);
     53       1.1   gdamore static void l2cap_recv_connect_req(struct mbuf *, struct hci_link *);
     54       1.1   gdamore static void l2cap_recv_connect_rsp(struct mbuf *, struct hci_link *);
     55       1.1   gdamore static void l2cap_recv_config_req(struct mbuf *, struct hci_link *);
     56       1.1   gdamore static void l2cap_recv_config_rsp(struct mbuf *, struct hci_link *);
     57       1.1   gdamore static void l2cap_recv_disconnect_req(struct mbuf *, struct hci_link *);
     58       1.1   gdamore static void l2cap_recv_disconnect_rsp(struct mbuf *, struct hci_link *);
     59       1.1   gdamore static void l2cap_recv_info_req(struct mbuf *, struct hci_link *);
     60       1.1   gdamore static int l2cap_send_signal(struct hci_link *, uint8_t, uint8_t, uint16_t, void *);
     61  1.15.8.2  jdolecek static int l2cap_send_command_rej(struct hci_link *, uint8_t, int, ...);
     62      1.15    plunky static void l2cap_qos_btoh(l2cap_qos_t *, void *);
     63      1.15    plunky static void l2cap_qos_htob(void *, l2cap_qos_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.9    plunky 			device_xname(link->hl_unit->hci_dev),
     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 	panic("impossible!");
    161       1.1   gdamore 
    162       1.1   gdamore reject:
    163       1.1   gdamore 	l2cap_send_command_rej(link, cmd.ident, L2CAP_REJ_NOT_UNDERSTOOD);
    164       1.1   gdamore finish:
    165       1.1   gdamore 	m_freem(m);
    166       1.1   gdamore }
    167       1.1   gdamore 
    168       1.1   gdamore /*
    169       1.1   gdamore  * Process Received Command Reject. For now we dont try to recover gracefully
    170       1.1   gdamore  * from this, it probably means that the link is garbled or the other end is
    171       1.1   gdamore  * insufficiently capable of handling normal traffic. (not *my* fault, no way!)
    172       1.1   gdamore  */
    173       1.1   gdamore static void
    174       1.1   gdamore l2cap_recv_command_rej(struct mbuf *m, struct hci_link *link)
    175       1.1   gdamore {
    176       1.1   gdamore 	struct l2cap_req *req;
    177       1.1   gdamore 	struct l2cap_channel *chan;
    178       1.1   gdamore 	l2cap_cmd_hdr_t cmd;
    179       1.1   gdamore 	l2cap_cmd_rej_cp cp;
    180       1.1   gdamore 
    181       1.1   gdamore 	m_copydata(m, 0, sizeof(cmd), &cmd);
    182       1.1   gdamore 	m_adj(m, sizeof(cmd));
    183       1.1   gdamore 
    184       1.1   gdamore 	cmd.length = le16toh(cmd.length);
    185       1.1   gdamore 
    186       1.1   gdamore 	m_copydata(m, 0, cmd.length, &cp);
    187       1.1   gdamore 	m_adj(m, cmd.length);
    188       1.1   gdamore 
    189       1.1   gdamore 	req = l2cap_request_lookup(link, cmd.ident);
    190       1.1   gdamore 	if (req == NULL)
    191       1.1   gdamore 		return;
    192       1.1   gdamore 
    193       1.1   gdamore 	switch (le16toh(cp.reason)) {
    194       1.1   gdamore 	case L2CAP_REJ_NOT_UNDERSTOOD:
    195       1.1   gdamore 		/*
    196       1.1   gdamore 		 * I dont know what to do, just move up the timeout
    197       1.1   gdamore 		 */
    198       1.1   gdamore 		callout_schedule(&req->lr_rtx, 0);
    199       1.1   gdamore 		break;
    200       1.1   gdamore 
    201       1.1   gdamore 	case L2CAP_REJ_MTU_EXCEEDED:
    202       1.1   gdamore 		/*
    203       1.1   gdamore 		 * I didnt send any commands over L2CAP_MTU_MINIMUM size, but..
    204       1.6    plunky 		 *
    205       1.6    plunky 		 * XXX maybe we should resend this, instead?
    206       1.1   gdamore 		 */
    207       1.1   gdamore 		link->hl_mtu = le16toh(cp.data[0]);
    208       1.6    plunky 		callout_schedule(&req->lr_rtx, 0);
    209       1.1   gdamore 		break;
    210       1.1   gdamore 
    211       1.1   gdamore 	case L2CAP_REJ_INVALID_CID:
    212       1.1   gdamore 		/*
    213       1.1   gdamore 		 * Well, if they dont have such a channel then our channel is
    214       1.1   gdamore 		 * most likely closed. Make it so.
    215       1.1   gdamore 		 */
    216       1.1   gdamore 		chan = req->lr_chan;
    217       1.1   gdamore 		l2cap_request_free(req);
    218       1.1   gdamore 		if (chan != NULL && chan->lc_state != L2CAP_CLOSED)
    219       1.1   gdamore 			l2cap_close(chan, ECONNABORTED);
    220       1.1   gdamore 
    221       1.1   gdamore 		break;
    222       1.1   gdamore 
    223       1.1   gdamore 	default:
    224       1.1   gdamore 		UNKNOWN(le16toh(cp.reason));
    225       1.1   gdamore 		break;
    226       1.1   gdamore 	}
    227       1.1   gdamore }
    228       1.1   gdamore 
    229       1.1   gdamore /*
    230       1.1   gdamore  * Process Received Connect Request. Find listening channel matching
    231       1.1   gdamore  * psm & addr and ask upper layer for a new channel.
    232       1.1   gdamore  */
    233       1.1   gdamore static void
    234       1.1   gdamore l2cap_recv_connect_req(struct mbuf *m, struct hci_link *link)
    235       1.1   gdamore {
    236       1.1   gdamore 	struct sockaddr_bt laddr, raddr;
    237       1.1   gdamore 	struct l2cap_channel *chan, *new;
    238       1.1   gdamore 	l2cap_cmd_hdr_t cmd;
    239       1.1   gdamore 	l2cap_con_req_cp cp;
    240       1.1   gdamore 	int err;
    241       1.1   gdamore 
    242       1.1   gdamore 	/* extract cmd */
    243       1.1   gdamore 	m_copydata(m, 0, sizeof(cmd), &cmd);
    244       1.1   gdamore 	m_adj(m, sizeof(cmd));
    245       1.1   gdamore 
    246       1.1   gdamore 	/* extract request */
    247       1.1   gdamore 	m_copydata(m, 0, sizeof(cp), &cp);
    248       1.1   gdamore 	m_adj(m, sizeof(cp));
    249       1.1   gdamore 
    250       1.7    plunky 	cp.scid = le16toh(cp.scid);
    251       1.7    plunky 	cp.psm = le16toh(cp.psm);
    252       1.1   gdamore 
    253       1.1   gdamore 	memset(&laddr, 0, sizeof(struct sockaddr_bt));
    254       1.1   gdamore 	laddr.bt_len = sizeof(struct sockaddr_bt);
    255       1.1   gdamore 	laddr.bt_family = AF_BLUETOOTH;
    256       1.7    plunky 	laddr.bt_psm = cp.psm;
    257       1.1   gdamore 	bdaddr_copy(&laddr.bt_bdaddr, &link->hl_unit->hci_bdaddr);
    258       1.1   gdamore 
    259       1.1   gdamore 	memset(&raddr, 0, sizeof(struct sockaddr_bt));
    260       1.1   gdamore 	raddr.bt_len = sizeof(struct sockaddr_bt);
    261       1.1   gdamore 	raddr.bt_family = AF_BLUETOOTH;
    262       1.7    plunky 	raddr.bt_psm = cp.psm;
    263       1.1   gdamore 	bdaddr_copy(&raddr.bt_bdaddr, &link->hl_bdaddr);
    264       1.1   gdamore 
    265       1.1   gdamore 	LIST_FOREACH(chan, &l2cap_listen_list, lc_ncid) {
    266      1.10    plunky 		if (chan->lc_laddr.bt_psm != laddr.bt_psm)
    267       1.1   gdamore 			continue;
    268       1.1   gdamore 
    269       1.1   gdamore 		if (!bdaddr_same(&laddr.bt_bdaddr, &chan->lc_laddr.bt_bdaddr)
    270       1.1   gdamore 		    && bdaddr_any(&chan->lc_laddr.bt_bdaddr) == 0)
    271       1.1   gdamore 			continue;
    272       1.1   gdamore 
    273       1.1   gdamore 		new= (*chan->lc_proto->newconn)(chan->lc_upper, &laddr, &raddr);
    274       1.1   gdamore 		if (new == NULL)
    275       1.1   gdamore 			continue;
    276       1.1   gdamore 
    277       1.1   gdamore 		err = l2cap_cid_alloc(new);
    278       1.1   gdamore 		if (err) {
    279       1.7    plunky 			l2cap_send_connect_rsp(link, cmd.ident,
    280       1.7    plunky 						0, cp.scid,
    281       1.7    plunky 						L2CAP_NO_RESOURCES);
    282       1.7    plunky 
    283       1.1   gdamore 			(*new->lc_proto->disconnected)(new->lc_upper, err);
    284       1.1   gdamore 			return;
    285       1.1   gdamore 		}
    286       1.1   gdamore 
    287       1.1   gdamore 		new->lc_link = hci_acl_open(link->hl_unit, &link->hl_bdaddr);
    288       1.1   gdamore 		KASSERT(new->lc_link == link);
    289       1.1   gdamore 
    290       1.7    plunky 		new->lc_rcid = cp.scid;
    291       1.7    plunky 		new->lc_ident = cmd.ident;
    292       1.1   gdamore 
    293       1.1   gdamore 		memcpy(&new->lc_laddr, &laddr, sizeof(struct sockaddr_bt));
    294       1.1   gdamore 		memcpy(&new->lc_raddr, &raddr, sizeof(struct sockaddr_bt));
    295       1.1   gdamore 
    296       1.7    plunky 		new->lc_mode = chan->lc_mode;
    297       1.7    plunky 
    298       1.7    plunky 		err = l2cap_setmode(new);
    299       1.7    plunky 		if (err == EINPROGRESS) {
    300       1.7    plunky 			new->lc_state = L2CAP_WAIT_SEND_CONNECT_RSP;
    301       1.7    plunky 			(*new->lc_proto->connecting)(new->lc_upper);
    302       1.7    plunky 			return;
    303       1.7    plunky 		}
    304       1.7    plunky 		if (err) {
    305       1.7    plunky 			new->lc_state = L2CAP_CLOSED;
    306       1.7    plunky 			hci_acl_close(link, err);
    307       1.7    plunky 			new->lc_link = NULL;
    308       1.7    plunky 
    309       1.7    plunky 			l2cap_send_connect_rsp(link, cmd.ident,
    310       1.7    plunky 						0, cp.scid,
    311       1.7    plunky 						L2CAP_NO_RESOURCES);
    312       1.7    plunky 
    313       1.7    plunky 			(*new->lc_proto->disconnected)(new->lc_upper, err);
    314       1.7    plunky 			return;
    315       1.7    plunky 		}
    316       1.7    plunky 
    317       1.7    plunky 		err = l2cap_send_connect_rsp(link, cmd.ident,
    318       1.7    plunky 					      new->lc_lcid, new->lc_rcid,
    319       1.7    plunky 					      L2CAP_SUCCESS);
    320       1.7    plunky 		if (err) {
    321       1.7    plunky 			l2cap_close(new, err);
    322       1.7    plunky 			return;
    323       1.7    plunky 		}
    324       1.1   gdamore 
    325       1.5    plunky 		new->lc_state = L2CAP_WAIT_CONFIG;
    326       1.5    plunky 		new->lc_flags |= (L2CAP_WAIT_CONFIG_REQ | L2CAP_WAIT_CONFIG_RSP);
    327       1.7    plunky 		err = l2cap_send_config_req(new);
    328       1.7    plunky 		if (err)
    329       1.7    plunky 			l2cap_close(new, err);
    330       1.7    plunky 
    331       1.1   gdamore 		return;
    332       1.1   gdamore 	}
    333       1.1   gdamore 
    334       1.7    plunky 	l2cap_send_connect_rsp(link, cmd.ident,
    335       1.7    plunky 				0, cp.scid,
    336       1.7    plunky 				L2CAP_PSM_NOT_SUPPORTED);
    337       1.1   gdamore }
    338       1.1   gdamore 
    339       1.1   gdamore /*
    340       1.1   gdamore  * Process Received Connect Response.
    341       1.1   gdamore  */
    342       1.1   gdamore static void
    343       1.1   gdamore l2cap_recv_connect_rsp(struct mbuf *m, struct hci_link *link)
    344       1.1   gdamore {
    345       1.1   gdamore 	l2cap_cmd_hdr_t cmd;
    346       1.1   gdamore 	l2cap_con_rsp_cp cp;
    347       1.1   gdamore 	struct l2cap_req *req;
    348       1.1   gdamore 	struct l2cap_channel *chan;
    349       1.1   gdamore 
    350       1.1   gdamore 	m_copydata(m, 0, sizeof(cmd), &cmd);
    351       1.1   gdamore 	m_adj(m, sizeof(cmd));
    352       1.1   gdamore 
    353       1.1   gdamore 	m_copydata(m, 0, sizeof(cp), &cp);
    354       1.1   gdamore 	m_adj(m, sizeof(cp));
    355       1.1   gdamore 
    356       1.1   gdamore 	cp.scid = le16toh(cp.scid);
    357       1.1   gdamore 	cp.dcid = le16toh(cp.dcid);
    358       1.1   gdamore 	cp.result = le16toh(cp.result);
    359       1.1   gdamore 
    360       1.1   gdamore 	req = l2cap_request_lookup(link, cmd.ident);
    361       1.1   gdamore 	if (req == NULL || req->lr_code != L2CAP_CONNECT_REQ)
    362       1.1   gdamore 		return;
    363       1.1   gdamore 
    364       1.1   gdamore 	chan = req->lr_chan;
    365       1.1   gdamore 	if (chan != NULL && chan->lc_lcid != cp.scid)
    366       1.1   gdamore 		return;
    367       1.1   gdamore 
    368       1.7    plunky 	if (chan == NULL || chan->lc_state != L2CAP_WAIT_RECV_CONNECT_RSP) {
    369       1.1   gdamore 		l2cap_request_free(req);
    370       1.1   gdamore 		return;
    371       1.1   gdamore 	}
    372       1.1   gdamore 
    373       1.1   gdamore 	switch (cp.result) {
    374       1.1   gdamore 	case L2CAP_SUCCESS:
    375       1.1   gdamore 		/*
    376       1.1   gdamore 		 * Ok, at this point we have a connection to the other party. We
    377       1.1   gdamore 		 * could indicate upstream that we are ready for business and
    378       1.1   gdamore 		 * wait for a "Configure Channel Request" but I'm not so sure
    379       1.1   gdamore 		 * that is required in our case - we will proceed directly to
    380       1.1   gdamore 		 * sending our config request. We set two state bits because in
    381       1.1   gdamore 		 * the config state we are waiting for requests and responses.
    382       1.1   gdamore 		 */
    383       1.1   gdamore 		l2cap_request_free(req);
    384       1.1   gdamore 		chan->lc_rcid = cp.dcid;
    385       1.5    plunky 		chan->lc_state = L2CAP_WAIT_CONFIG;
    386       1.5    plunky 		chan->lc_flags |= (L2CAP_WAIT_CONFIG_REQ | L2CAP_WAIT_CONFIG_RSP);
    387       1.1   gdamore 		l2cap_send_config_req(chan);
    388       1.1   gdamore 		break;
    389       1.1   gdamore 
    390       1.1   gdamore 	case L2CAP_PENDING:
    391       1.6    plunky 		/* XXX dont release request, should start eRTX timeout? */
    392       1.1   gdamore 		(*chan->lc_proto->connecting)(chan->lc_upper);
    393       1.1   gdamore 		break;
    394       1.1   gdamore 
    395       1.1   gdamore 	case L2CAP_PSM_NOT_SUPPORTED:
    396       1.1   gdamore 	case L2CAP_SECURITY_BLOCK:
    397       1.1   gdamore 	case L2CAP_NO_RESOURCES:
    398       1.1   gdamore 	default:
    399       1.1   gdamore 		l2cap_request_free(req);
    400       1.1   gdamore 		l2cap_close(chan, ECONNREFUSED);
    401       1.1   gdamore 		break;
    402       1.1   gdamore 	}
    403       1.1   gdamore }
    404       1.1   gdamore 
    405       1.1   gdamore /*
    406       1.1   gdamore  * Process Received Config Reqest.
    407       1.1   gdamore  */
    408       1.1   gdamore static void
    409       1.1   gdamore l2cap_recv_config_req(struct mbuf *m, struct hci_link *link)
    410       1.1   gdamore {
    411       1.1   gdamore 	uint8_t buf[L2CAP_MTU_MINIMUM];
    412       1.1   gdamore 	l2cap_cmd_hdr_t cmd;
    413       1.1   gdamore 	l2cap_cfg_req_cp cp;
    414       1.3    plunky 	l2cap_cfg_opt_t opt;
    415       1.3    plunky 	l2cap_cfg_opt_val_t val;
    416       1.3    plunky 	l2cap_cfg_rsp_cp rp;
    417       1.1   gdamore 	struct l2cap_channel *chan;
    418       1.1   gdamore 	int left, len;
    419       1.1   gdamore 
    420       1.1   gdamore 	m_copydata(m, 0, sizeof(cmd), &cmd);
    421       1.1   gdamore 	m_adj(m, sizeof(cmd));
    422       1.1   gdamore 	left = le16toh(cmd.length);
    423       1.1   gdamore 
    424       1.1   gdamore 	if (left < sizeof(cp))
    425       1.1   gdamore 		goto reject;
    426       1.1   gdamore 
    427       1.1   gdamore 	m_copydata(m, 0, sizeof(cp), &cp);
    428       1.1   gdamore 	m_adj(m, sizeof(cp));
    429       1.1   gdamore 	left -= sizeof(cp);
    430       1.1   gdamore 
    431       1.1   gdamore 	cp.dcid = le16toh(cp.dcid);
    432       1.1   gdamore 	cp.flags = le16toh(cp.flags);
    433       1.1   gdamore 
    434       1.1   gdamore 	chan = l2cap_cid_lookup(cp.dcid);
    435       1.4    plunky 	if (chan == NULL || chan->lc_link != link
    436       1.5    plunky 	    || chan->lc_state != L2CAP_WAIT_CONFIG
    437       1.5    plunky 	    || (chan->lc_flags & L2CAP_WAIT_CONFIG_REQ) == 0) {
    438       1.5    plunky 		/* XXX we should really accept reconfiguration requests */
    439       1.1   gdamore 		l2cap_send_command_rej(link, cmd.ident, L2CAP_REJ_INVALID_CID,
    440       1.1   gdamore 					L2CAP_NULL_CID, cp.dcid);
    441       1.1   gdamore 		goto out;
    442       1.1   gdamore 	}
    443       1.1   gdamore 
    444       1.1   gdamore 	/* ready our response packet */
    445       1.3    plunky 	rp.scid = htole16(chan->lc_rcid);
    446       1.3    plunky 	rp.flags = 0;	/* "No Continuation" */
    447       1.3    plunky 	rp.result = L2CAP_SUCCESS;
    448       1.3    plunky 	len = sizeof(rp);
    449       1.1   gdamore 
    450       1.1   gdamore 	/*
    451       1.1   gdamore 	 * Process the packet. We build the return packet on the fly adding any
    452       1.1   gdamore 	 * unacceptable parameters as we go. As we can only return one result,
    453       1.1   gdamore 	 * unknown option takes precedence so we start our return packet anew
    454       1.1   gdamore 	 * and ignore option values thereafter as they will be re-sent.
    455       1.1   gdamore 	 *
    456       1.1   gdamore 	 * Since we do not support enough options to make overflowing the min
    457       1.1   gdamore 	 * MTU size an issue in normal use, we just reject config requests that
    458       1.3    plunky 	 * make that happen. This could be because options are repeated or the
    459       1.3    plunky 	 * packet is corrupted in some way.
    460       1.1   gdamore 	 *
    461       1.3    plunky 	 * If unknown option types threaten to overflow the packet, we just
    462       1.3    plunky 	 * ignore them. We can deny them next time.
    463       1.1   gdamore 	 */
    464       1.1   gdamore 	while (left > 0) {
    465       1.3    plunky 		if (left < sizeof(opt))
    466       1.1   gdamore 			goto reject;
    467       1.1   gdamore 
    468       1.3    plunky 		m_copydata(m, 0, sizeof(opt), &opt);
    469       1.3    plunky 		m_adj(m, sizeof(opt));
    470       1.3    plunky 		left -= sizeof(opt);
    471       1.1   gdamore 
    472       1.3    plunky 		if (left < opt.length)
    473       1.1   gdamore 			goto reject;
    474       1.1   gdamore 
    475       1.3    plunky 		switch(opt.type & L2CAP_OPT_HINT_MASK) {
    476       1.1   gdamore 		case L2CAP_OPT_MTU:
    477       1.3    plunky 			if (rp.result == L2CAP_UNKNOWN_OPTION)
    478       1.1   gdamore 				break;
    479       1.1   gdamore 
    480       1.3    plunky 			if (opt.length != L2CAP_OPT_MTU_SIZE)
    481       1.1   gdamore 				goto reject;
    482       1.1   gdamore 
    483       1.3    plunky 			m_copydata(m, 0, L2CAP_OPT_MTU_SIZE, &val);
    484       1.3    plunky 			val.mtu = le16toh(val.mtu);
    485       1.1   gdamore 
    486       1.1   gdamore 			/*
    487       1.1   gdamore 			 * XXX how do we know what the minimum acceptable MTU is
    488       1.1   gdamore 			 * for a channel? Spec says some profiles have a higher
    489       1.1   gdamore 			 * minimum but I have no way to find that out at this
    490       1.1   gdamore 			 * juncture..
    491       1.1   gdamore 			 */
    492       1.3    plunky 			if (val.mtu < L2CAP_MTU_MINIMUM) {
    493       1.3    plunky 				if (len + sizeof(opt) + L2CAP_OPT_MTU_SIZE > sizeof(buf))
    494       1.3    plunky 					goto reject;
    495       1.3    plunky 
    496       1.3    plunky 				rp.result = L2CAP_UNACCEPTABLE_PARAMS;
    497       1.3    plunky 				memcpy(buf + len, &opt, sizeof(opt));
    498       1.3    plunky 				len += sizeof(opt);
    499       1.3    plunky 				val.mtu = htole16(L2CAP_MTU_MINIMUM);
    500       1.3    plunky 				memcpy(buf + len, &val, L2CAP_OPT_MTU_SIZE);
    501       1.3    plunky 				len += L2CAP_OPT_MTU_SIZE;
    502       1.1   gdamore 			} else
    503       1.3    plunky 				chan->lc_omtu = val.mtu;
    504       1.1   gdamore 
    505       1.1   gdamore 			break;
    506       1.1   gdamore 
    507       1.1   gdamore 		case L2CAP_OPT_FLUSH_TIMO:
    508       1.3    plunky 			if (rp.result == L2CAP_UNKNOWN_OPTION)
    509       1.1   gdamore 				break;
    510       1.1   gdamore 
    511       1.3    plunky 			if (opt.length != L2CAP_OPT_FLUSH_TIMO_SIZE)
    512       1.1   gdamore 				goto reject;
    513       1.1   gdamore 
    514       1.1   gdamore 			/*
    515       1.1   gdamore 			 * I think that this is informational only - he is
    516       1.1   gdamore 			 * informing us of the flush timeout he will be using.
    517       1.1   gdamore 			 * I dont think this affects us in any significant way,
    518       1.1   gdamore 			 * so just ignore this value for now.
    519       1.1   gdamore 			 */
    520       1.1   gdamore 			break;
    521       1.1   gdamore 
    522       1.1   gdamore 		case L2CAP_OPT_QOS:
    523      1.15    plunky 			if (rp.result == L2CAP_UNKNOWN_OPTION)
    524      1.15    plunky 				break;
    525      1.15    plunky 
    526      1.15    plunky 			if (opt.length != L2CAP_OPT_QOS_SIZE)
    527      1.15    plunky 				goto reject;
    528      1.15    plunky 
    529      1.15    plunky 			/*
    530      1.15    plunky 			 * We don't actually support QoS, but an incoming
    531      1.15    plunky 			 * config request is merely advising us of their
    532      1.15    plunky 			 * outgoing traffic flow, so be nice.
    533      1.15    plunky 			 */
    534      1.15    plunky 			m_copydata(m, 0, L2CAP_OPT_QOS_SIZE, &val);
    535      1.15    plunky 			switch (val.qos.service_type) {
    536      1.15    plunky 			case L2CAP_QOS_NO_TRAFFIC:
    537      1.15    plunky 				/*
    538      1.15    plunky 				 * "No traffic" means they don't plan to send
    539      1.15    plunky 				 * any data and the fields should be ignored.
    540      1.15    plunky 				 */
    541      1.15    plunky 				chan->lc_iqos = l2cap_default_qos;
    542      1.15    plunky 				chan->lc_iqos.service_type = L2CAP_QOS_NO_TRAFFIC;
    543      1.15    plunky 				break;
    544      1.15    plunky 
    545      1.15    plunky 			case L2CAP_QOS_BEST_EFFORT:
    546      1.15    plunky 				/*
    547      1.15    plunky 				 * "Best effort" is the default, and we may
    548      1.15    plunky 				 * choose to ignore the fields, try to satisfy
    549      1.15    plunky 				 * the parameters while giving no response, or
    550      1.15    plunky 				 * respond with the settings we will try to
    551      1.15    plunky 				 * meet.
    552      1.15    plunky 				 */
    553      1.15    plunky 				l2cap_qos_btoh(&chan->lc_iqos, &val.qos);
    554      1.15    plunky 				break;
    555      1.15    plunky 
    556      1.15    plunky 			case L2CAP_QOS_GUARANTEED:
    557      1.15    plunky 			default:
    558      1.15    plunky 				/*
    559      1.15    plunky 			 	 * Anything else we don't support, so make a
    560      1.15    plunky 				 * counter-offer with the current settings.
    561      1.15    plunky 				 */
    562      1.15    plunky 				if (len + sizeof(opt) + L2CAP_OPT_QOS_SIZE > sizeof(buf))
    563      1.15    plunky 					goto reject;
    564      1.15    plunky 
    565      1.15    plunky 				rp.result = L2CAP_UNACCEPTABLE_PARAMS;
    566      1.15    plunky 				memcpy(buf + len, &opt, sizeof(opt));
    567      1.15    plunky 				len += sizeof(opt);
    568      1.15    plunky 				l2cap_qos_htob(buf + len, &chan->lc_iqos);
    569      1.15    plunky 				len += L2CAP_OPT_QOS_SIZE;
    570      1.15    plunky 				break;
    571      1.15    plunky 			}
    572      1.15    plunky 			break;
    573      1.15    plunky 
    574       1.1   gdamore 		default:
    575       1.1   gdamore 			/* ignore hints */
    576       1.3    plunky 			if (opt.type & L2CAP_OPT_HINT_BIT)
    577       1.1   gdamore 				break;
    578       1.1   gdamore 
    579       1.1   gdamore 			/* unknown options supercede all else */
    580       1.3    plunky 			if (rp.result != L2CAP_UNKNOWN_OPTION) {
    581       1.3    plunky 				rp.result = L2CAP_UNKNOWN_OPTION;
    582       1.3    plunky 				len = sizeof(rp);
    583       1.1   gdamore 			}
    584       1.1   gdamore 
    585       1.3    plunky 			/* ignore if it don't fit */
    586       1.3    plunky 			if (len + sizeof(opt) > sizeof(buf))
    587       1.3    plunky 				break;
    588       1.3    plunky 
    589       1.3    plunky 			/* return unknown option type, but no data */
    590       1.3    plunky 			buf[len++] = opt.type;
    591       1.3    plunky 			buf[len++] = 0;
    592       1.1   gdamore 			break;
    593       1.1   gdamore 		}
    594       1.1   gdamore 
    595       1.3    plunky 		m_adj(m, opt.length);
    596       1.3    plunky 		left -= opt.length;
    597       1.1   gdamore 	}
    598       1.1   gdamore 
    599       1.3    plunky 	rp.result = htole16(rp.result);
    600       1.3    plunky 	memcpy(buf, &rp, sizeof(rp));
    601       1.1   gdamore 	l2cap_send_signal(link, L2CAP_CONFIG_RSP, cmd.ident, len, buf);
    602       1.1   gdamore 
    603       1.1   gdamore 	if ((cp.flags & L2CAP_OPT_CFLAG_BIT) == 0
    604       1.3    plunky 	    && rp.result == le16toh(L2CAP_SUCCESS)) {
    605       1.1   gdamore 
    606       1.5    plunky 		chan->lc_flags &= ~L2CAP_WAIT_CONFIG_REQ;
    607       1.1   gdamore 
    608       1.5    plunky 		if ((chan->lc_flags & L2CAP_WAIT_CONFIG_RSP) == 0) {
    609       1.1   gdamore 			chan->lc_state = L2CAP_OPEN;
    610       1.6    plunky 			/* XXX how to distinguish REconfiguration? */
    611       1.1   gdamore 			(*chan->lc_proto->connected)(chan->lc_upper);
    612       1.1   gdamore 		}
    613       1.1   gdamore 	}
    614       1.1   gdamore 	return;
    615       1.1   gdamore 
    616       1.1   gdamore reject:
    617       1.1   gdamore 	l2cap_send_command_rej(link, cmd.ident, L2CAP_REJ_NOT_UNDERSTOOD);
    618       1.1   gdamore out:
    619       1.1   gdamore 	m_adj(m, left);
    620       1.1   gdamore }
    621       1.1   gdamore 
    622       1.1   gdamore /*
    623       1.1   gdamore  * Process Received Config Response.
    624       1.1   gdamore  */
    625       1.1   gdamore static void
    626       1.1   gdamore l2cap_recv_config_rsp(struct mbuf *m, struct hci_link *link)
    627       1.1   gdamore {
    628       1.1   gdamore 	l2cap_cmd_hdr_t cmd;
    629       1.1   gdamore 	l2cap_cfg_rsp_cp cp;
    630       1.1   gdamore 	l2cap_cfg_opt_t opt;
    631       1.1   gdamore 	l2cap_cfg_opt_val_t val;
    632       1.1   gdamore 	struct l2cap_req *req;
    633       1.1   gdamore 	struct l2cap_channel *chan;
    634       1.1   gdamore 	int left;
    635       1.1   gdamore 
    636       1.1   gdamore 	m_copydata(m, 0, sizeof(cmd), &cmd);
    637       1.1   gdamore 	m_adj(m, sizeof(cmd));
    638       1.1   gdamore 	left = le16toh(cmd.length);
    639       1.1   gdamore 
    640       1.1   gdamore 	if (left < sizeof(cp))
    641       1.1   gdamore 		goto out;
    642       1.1   gdamore 
    643       1.1   gdamore 	m_copydata(m, 0, sizeof(cp), &cp);
    644       1.1   gdamore 	m_adj(m, sizeof(cp));
    645       1.1   gdamore 	left -= sizeof(cp);
    646       1.1   gdamore 
    647       1.1   gdamore 	cp.scid = le16toh(cp.scid);
    648       1.1   gdamore 	cp.flags = le16toh(cp.flags);
    649       1.1   gdamore 	cp.result = le16toh(cp.result);
    650       1.1   gdamore 
    651       1.1   gdamore 	req = l2cap_request_lookup(link, cmd.ident);
    652       1.1   gdamore 	if (req == NULL || req->lr_code != L2CAP_CONFIG_REQ)
    653       1.1   gdamore 		goto out;
    654       1.1   gdamore 
    655       1.1   gdamore 	chan = req->lr_chan;
    656       1.1   gdamore 	if (chan != NULL && chan->lc_lcid != cp.scid)
    657       1.1   gdamore 		goto out;
    658       1.1   gdamore 
    659       1.1   gdamore 	l2cap_request_free(req);
    660       1.1   gdamore 
    661       1.5    plunky 	if (chan == NULL || chan->lc_state != L2CAP_WAIT_CONFIG
    662       1.5    plunky 	    || (chan->lc_flags & L2CAP_WAIT_CONFIG_RSP) == 0)
    663       1.1   gdamore 		goto out;
    664       1.1   gdamore 
    665       1.1   gdamore 	if ((cp.flags & L2CAP_OPT_CFLAG_BIT)) {
    666       1.1   gdamore 		l2cap_cfg_req_cp rp;
    667       1.1   gdamore 
    668       1.1   gdamore 		/*
    669       1.1   gdamore 		 * They have more to tell us and want another ID to
    670       1.1   gdamore 		 * use, so send an empty config request
    671       1.1   gdamore 		 */
    672       1.1   gdamore 		if (l2cap_request_alloc(chan, L2CAP_CONFIG_REQ))
    673       1.1   gdamore 			goto discon;
    674       1.1   gdamore 
    675       1.1   gdamore 		rp.dcid = htole16(cp.scid);
    676       1.1   gdamore 		rp.flags = 0;
    677       1.1   gdamore 
    678       1.1   gdamore 		if (l2cap_send_signal(link, L2CAP_CONFIG_REQ, link->hl_lastid,
    679       1.1   gdamore 					sizeof(rp), &rp))
    680       1.1   gdamore 			goto discon;
    681       1.1   gdamore 	}
    682       1.1   gdamore 
    683       1.1   gdamore 	switch(cp.result) {
    684       1.1   gdamore 	case L2CAP_SUCCESS:
    685       1.1   gdamore 		/*
    686       1.1   gdamore 		 * If continuation flag was not set, our config request was
    687       1.1   gdamore 		 * accepted. We may have to wait for their config request to
    688       1.1   gdamore 		 * complete, so check that but otherwise we are open
    689       1.1   gdamore 		 *
    690       1.1   gdamore 		 * There may be 'advisory' values in the packet but we just
    691       1.1   gdamore 		 * ignore those..
    692       1.1   gdamore 		 */
    693       1.1   gdamore 		if ((cp.flags & L2CAP_OPT_CFLAG_BIT) == 0) {
    694       1.5    plunky 			chan->lc_flags &= ~L2CAP_WAIT_CONFIG_RSP;
    695       1.1   gdamore 
    696       1.5    plunky 			if ((chan->lc_flags & L2CAP_WAIT_CONFIG_REQ) == 0) {
    697       1.1   gdamore 				chan->lc_state = L2CAP_OPEN;
    698       1.6    plunky 				/* XXX how to distinguish REconfiguration? */
    699       1.1   gdamore 				(*chan->lc_proto->connected)(chan->lc_upper);
    700       1.1   gdamore 			}
    701       1.1   gdamore 		}
    702       1.1   gdamore 		goto out;
    703       1.1   gdamore 
    704       1.1   gdamore 	case L2CAP_UNACCEPTABLE_PARAMS:
    705       1.1   gdamore 		/*
    706       1.1   gdamore 		 * Packet contains unacceptable parameters with preferred values
    707       1.1   gdamore 		 */
    708       1.1   gdamore 		while (left > 0) {
    709       1.1   gdamore 			if (left < sizeof(opt))
    710       1.1   gdamore 				goto discon;
    711       1.1   gdamore 
    712       1.1   gdamore 			m_copydata(m, 0, sizeof(opt), &opt);
    713       1.1   gdamore 			m_adj(m, sizeof(opt));
    714       1.1   gdamore 			left -= sizeof(opt);
    715       1.1   gdamore 
    716       1.1   gdamore 			if (left < opt.length)
    717       1.1   gdamore 				goto discon;
    718       1.1   gdamore 
    719       1.1   gdamore 			switch (opt.type) {
    720       1.1   gdamore 			case L2CAP_OPT_MTU:
    721       1.1   gdamore 				if (opt.length != L2CAP_OPT_MTU_SIZE)
    722       1.1   gdamore 					goto discon;
    723       1.1   gdamore 
    724       1.1   gdamore 				m_copydata(m, 0, L2CAP_OPT_MTU_SIZE, &val);
    725       1.1   gdamore 				chan->lc_imtu = le16toh(val.mtu);
    726       1.1   gdamore 				if (chan->lc_imtu < L2CAP_MTU_MINIMUM)
    727       1.1   gdamore 					chan->lc_imtu = L2CAP_MTU_DEFAULT;
    728       1.1   gdamore 				break;
    729       1.1   gdamore 
    730       1.1   gdamore 			case L2CAP_OPT_FLUSH_TIMO:
    731       1.1   gdamore 				if (opt.length != L2CAP_OPT_FLUSH_TIMO_SIZE)
    732       1.1   gdamore 					goto discon;
    733       1.1   gdamore 
    734       1.1   gdamore 				/*
    735       1.1   gdamore 				 * Spec says: If we cannot honor proposed value,
    736       1.1   gdamore 				 * either disconnect or try again with original
    737       1.1   gdamore 				 * value. I can't really see why they want to
    738       1.1   gdamore 				 * interfere with OUR flush timeout in any case
    739       1.1   gdamore 				 * so we just punt for now.
    740       1.1   gdamore 				 */
    741       1.1   gdamore 				goto discon;
    742       1.1   gdamore 
    743       1.1   gdamore 			case L2CAP_OPT_QOS:
    744      1.15    plunky 				if (opt.length != L2CAP_OPT_QOS_SIZE)
    745      1.15    plunky 					goto discon;
    746      1.15    plunky 
    747      1.15    plunky 				/*
    748      1.15    plunky 				 * This may happen even if we haven't sent a
    749      1.15    plunky 				 * QoS request, where they need to state their
    750      1.15    plunky 				 * preferred incoming traffic flow.
    751      1.15    plunky 				 * We don't support anything, but copy in the
    752      1.15    plunky 				 * parameters if no action is good enough.
    753      1.15    plunky 				 */
    754      1.15    plunky 				m_copydata(m, 0, L2CAP_OPT_QOS_SIZE, &val);
    755      1.15    plunky 				switch (val.qos.service_type) {
    756      1.15    plunky 				case L2CAP_QOS_NO_TRAFFIC:
    757      1.15    plunky 				case L2CAP_QOS_BEST_EFFORT:
    758      1.15    plunky 					l2cap_qos_btoh(&chan->lc_oqos, &val.qos);
    759      1.15    plunky 					break;
    760      1.15    plunky 
    761      1.15    plunky 				case L2CAP_QOS_GUARANTEED:
    762      1.15    plunky 				default:
    763      1.15    plunky 					goto discon;
    764      1.15    plunky 				}
    765       1.1   gdamore 				break;
    766       1.1   gdamore 
    767       1.1   gdamore 			default:
    768       1.1   gdamore 				UNKNOWN(opt.type);
    769       1.1   gdamore 				goto discon;
    770       1.1   gdamore 			}
    771       1.1   gdamore 
    772       1.1   gdamore 			m_adj(m, opt.length);
    773       1.1   gdamore 			left -= opt.length;
    774       1.1   gdamore 		}
    775       1.1   gdamore 
    776       1.1   gdamore 		if ((cp.flags & L2CAP_OPT_CFLAG_BIT) == 0)
    777       1.6    plunky 			l2cap_send_config_req(chan);	/* no state change */
    778       1.1   gdamore 
    779       1.1   gdamore 		goto out;
    780       1.1   gdamore 
    781       1.1   gdamore 	case L2CAP_REJECT:
    782       1.1   gdamore 		goto discon;
    783       1.1   gdamore 
    784       1.1   gdamore 	case L2CAP_UNKNOWN_OPTION:
    785       1.1   gdamore 		/*
    786       1.1   gdamore 		 * Packet contains options not understood. Turn off unknown
    787       1.1   gdamore 		 * options by setting them to default values (means they will
    788       1.1   gdamore 		 * not be requested again).
    789       1.1   gdamore 		 *
    790       1.1   gdamore 		 * If our option was already off then fail (paranoia?)
    791       1.1   gdamore 		 *
    792       1.1   gdamore 		 * XXX Should we consider that options were set for a reason?
    793       1.1   gdamore 		 */
    794       1.1   gdamore 		while (left > 0) {
    795       1.1   gdamore 			if (left < sizeof(opt))
    796       1.1   gdamore 				goto discon;
    797       1.1   gdamore 
    798       1.1   gdamore 			m_copydata(m, 0, sizeof(opt), &opt);
    799       1.1   gdamore 			m_adj(m, sizeof(opt));
    800       1.1   gdamore 			left -= sizeof(opt);
    801       1.1   gdamore 
    802       1.1   gdamore 			if (left < opt.length)
    803       1.1   gdamore 				goto discon;
    804       1.1   gdamore 
    805       1.1   gdamore 			m_adj(m, opt.length);
    806       1.1   gdamore 			left -= opt.length;
    807       1.1   gdamore 
    808       1.1   gdamore 			switch(opt.type) {
    809       1.1   gdamore 			case L2CAP_OPT_MTU:
    810       1.1   gdamore 				if (chan->lc_imtu == L2CAP_MTU_DEFAULT)
    811       1.1   gdamore 					goto discon;
    812       1.1   gdamore 
    813       1.1   gdamore 				chan->lc_imtu = L2CAP_MTU_DEFAULT;
    814       1.1   gdamore 				break;
    815       1.1   gdamore 
    816       1.1   gdamore 			case L2CAP_OPT_FLUSH_TIMO:
    817       1.1   gdamore 				if (chan->lc_flush == L2CAP_FLUSH_TIMO_DEFAULT)
    818       1.1   gdamore 					goto discon;
    819       1.1   gdamore 
    820       1.1   gdamore 				chan->lc_flush = L2CAP_FLUSH_TIMO_DEFAULT;
    821       1.1   gdamore 				break;
    822       1.1   gdamore 
    823       1.1   gdamore 			case L2CAP_OPT_QOS:
    824       1.1   gdamore 				break;
    825       1.1   gdamore 
    826       1.1   gdamore 			default:
    827       1.1   gdamore 				UNKNOWN(opt.type);
    828       1.1   gdamore 				goto discon;
    829       1.1   gdamore 			}
    830       1.1   gdamore 		}
    831       1.1   gdamore 
    832       1.1   gdamore 		if ((cp.flags & L2CAP_OPT_CFLAG_BIT) == 0)
    833       1.1   gdamore 			l2cap_send_config_req(chan);	/* no state change */
    834       1.1   gdamore 
    835       1.1   gdamore 		goto out;
    836       1.1   gdamore 
    837       1.1   gdamore 	default:
    838       1.1   gdamore 		UNKNOWN(cp.result);
    839       1.1   gdamore 		goto discon;
    840       1.1   gdamore 	}
    841       1.1   gdamore 
    842       1.1   gdamore 	DPRINTF("how did I get here!?\n");
    843       1.1   gdamore 
    844       1.1   gdamore discon:
    845       1.1   gdamore 	l2cap_send_disconnect_req(chan);
    846       1.1   gdamore 	l2cap_close(chan, ECONNABORTED);
    847       1.1   gdamore 
    848       1.1   gdamore out:
    849       1.1   gdamore 	m_adj(m, left);
    850       1.1   gdamore }
    851       1.1   gdamore 
    852       1.1   gdamore /*
    853       1.1   gdamore  * Process Received Disconnect Request. We must validate scid and dcid
    854       1.1   gdamore  * just in case but otherwise this connection is finished.
    855       1.1   gdamore  */
    856       1.1   gdamore static void
    857       1.1   gdamore l2cap_recv_disconnect_req(struct mbuf *m, struct hci_link *link)
    858       1.1   gdamore {
    859       1.1   gdamore 	l2cap_cmd_hdr_t cmd;
    860       1.1   gdamore 	l2cap_discon_req_cp cp;
    861       1.1   gdamore 	l2cap_discon_rsp_cp rp;
    862       1.1   gdamore 	struct l2cap_channel *chan;
    863       1.1   gdamore 
    864       1.1   gdamore 	m_copydata(m, 0, sizeof(cmd), &cmd);
    865       1.1   gdamore 	m_adj(m, sizeof(cmd));
    866       1.1   gdamore 
    867       1.1   gdamore 	m_copydata(m, 0, sizeof(cp), &cp);
    868       1.1   gdamore 	m_adj(m, sizeof(cp));
    869       1.1   gdamore 
    870       1.1   gdamore 	cp.scid = le16toh(cp.scid);
    871       1.1   gdamore 	cp.dcid = le16toh(cp.dcid);
    872       1.1   gdamore 
    873       1.1   gdamore 	chan = l2cap_cid_lookup(cp.dcid);
    874       1.4    plunky 	if (chan == NULL || chan->lc_link != link || chan->lc_rcid != cp.scid) {
    875       1.1   gdamore 		l2cap_send_command_rej(link, cmd.ident, L2CAP_REJ_INVALID_CID,
    876       1.1   gdamore 					cp.dcid, cp.scid);
    877       1.1   gdamore 		return;
    878       1.1   gdamore 	}
    879       1.1   gdamore 
    880       1.1   gdamore 	rp.dcid = htole16(chan->lc_lcid);
    881       1.1   gdamore 	rp.scid = htole16(chan->lc_rcid);
    882       1.1   gdamore 	l2cap_send_signal(link, L2CAP_DISCONNECT_RSP, cmd.ident,
    883       1.1   gdamore 				sizeof(rp), &rp);
    884       1.1   gdamore 
    885       1.1   gdamore 	if (chan->lc_state != L2CAP_CLOSED)
    886      1.11    plunky 		l2cap_close(chan, 0);
    887       1.1   gdamore }
    888       1.1   gdamore 
    889       1.1   gdamore /*
    890       1.1   gdamore  * Process Received Disconnect Response. We must validate scid and dcid but
    891       1.1   gdamore  * unless we were waiting for this signal, ignore it.
    892       1.1   gdamore  */
    893       1.1   gdamore static void
    894       1.1   gdamore l2cap_recv_disconnect_rsp(struct mbuf *m, struct hci_link *link)
    895       1.1   gdamore {
    896       1.1   gdamore 	l2cap_cmd_hdr_t cmd;
    897       1.1   gdamore 	l2cap_discon_rsp_cp cp;
    898       1.1   gdamore 	struct l2cap_req *req;
    899       1.1   gdamore 	struct l2cap_channel *chan;
    900       1.1   gdamore 
    901       1.1   gdamore 	m_copydata(m, 0, sizeof(cmd), &cmd);
    902       1.1   gdamore 	m_adj(m, sizeof(cmd));
    903       1.1   gdamore 
    904       1.1   gdamore 	m_copydata(m, 0, sizeof(cp), &cp);
    905       1.1   gdamore 	m_adj(m, sizeof(cp));
    906       1.1   gdamore 
    907       1.1   gdamore 	cp.scid = le16toh(cp.scid);
    908       1.1   gdamore 	cp.dcid = le16toh(cp.dcid);
    909       1.1   gdamore 
    910       1.1   gdamore 	req = l2cap_request_lookup(link, cmd.ident);
    911       1.1   gdamore 	if (req == NULL || req->lr_code != L2CAP_DISCONNECT_REQ)
    912       1.1   gdamore 		return;
    913       1.1   gdamore 
    914       1.1   gdamore 	chan = req->lr_chan;
    915       1.1   gdamore 	if (chan == NULL
    916       1.1   gdamore 	    || chan->lc_lcid != cp.scid
    917       1.1   gdamore 	    || chan->lc_rcid != cp.dcid)
    918       1.1   gdamore 		return;
    919       1.1   gdamore 
    920       1.1   gdamore 	l2cap_request_free(req);
    921       1.1   gdamore 
    922       1.1   gdamore 	if (chan->lc_state != L2CAP_WAIT_DISCONNECT)
    923       1.1   gdamore 		return;
    924       1.1   gdamore 
    925       1.1   gdamore 	l2cap_close(chan, 0);
    926       1.1   gdamore }
    927       1.1   gdamore 
    928       1.1   gdamore /*
    929      1.12    plunky  * Process Received Info Request.
    930       1.1   gdamore  */
    931       1.1   gdamore static void
    932       1.1   gdamore l2cap_recv_info_req(struct mbuf *m, struct hci_link *link)
    933       1.1   gdamore {
    934       1.1   gdamore 	l2cap_cmd_hdr_t cmd;
    935       1.1   gdamore 	l2cap_info_req_cp cp;
    936      1.12    plunky 	uint8_t rsp[12];
    937       1.1   gdamore 
    938       1.1   gdamore 	m_copydata(m, 0, sizeof(cmd), &cmd);
    939       1.1   gdamore 	m_adj(m, sizeof(cmd));
    940       1.1   gdamore 
    941       1.1   gdamore 	m_copydata(m, 0, sizeof(cp), &cp);
    942       1.1   gdamore 	m_adj(m, sizeof(cp));
    943       1.1   gdamore 
    944      1.12    plunky 	cp.type = le16toh(cp.type);
    945      1.12    plunky 	switch(cp.type) {
    946      1.12    plunky 	case L2CAP_EXTENDED_FEATURES:
    947      1.12    plunky 		/*
    948      1.12    plunky 		 * 32-bit data field, unused bits set to zero
    949      1.12    plunky 		 *
    950      1.12    plunky 		 * octet bit feature
    951      1.12    plunky 		 *   0   0   Flow control mode
    952      1.12    plunky 		 *   0   1   Retransmission mode
    953      1.12    plunky 		 *   0   2   Bi-directional QoS
    954      1.12    plunky 		 *   0   3   Enhanced retransmission mode
    955      1.12    plunky 		 *   0   4   Streaming mode
    956      1.12    plunky 		 *   0   5   FCS option
    957      1.12    plunky 		 *   0   6   Extended flow specification for BR/EDR
    958      1.12    plunky 		 *   0   7   Fixed channels (SET)
    959      1.12    plunky 		 *   1   0   Extended window size
    960      1.12    plunky 		 *   1   1   Unicast connectionless data reception
    961      1.12    plunky 		 */
    962      1.12    plunky 		le16enc(rsp + 0, cp.type);
    963      1.12    plunky 		le16enc(rsp + 2, L2CAP_SUCCESS);
    964      1.12    plunky 		le32enc(rsp + 4, 0x00000080);
    965      1.12    plunky 		l2cap_send_signal(link, L2CAP_INFO_RSP, cmd.ident, 8, rsp);
    966      1.12    plunky 		break;
    967      1.12    plunky 
    968      1.12    plunky 	case L2CAP_FIXED_CHANNELS:
    969      1.12    plunky 		/*
    970      1.12    plunky 		 * 64-bit data field, unused bits set to zero
    971      1.12    plunky 		 *
    972      1.12    plunky 		 * octet bit channel
    973      1.12    plunky 		 *   0   0   0x0000 Null
    974      1.12    plunky 		 *   0   1   0x0001 L2CAP Signalling Channel (SET)
    975      1.12    plunky 		 *   0   2   0x0002 Connectionless Reception
    976      1.12    plunky 		 *   0   3   0x0003 AMP Manager Protocol Channel
    977  1.15.8.2  jdolecek 		 *   0   7   0x0007 BR/EDR Security Manager
    978  1.15.8.2  jdolecek 		 *   7   7   0x003f AMP Test Manager
    979      1.12    plunky 		 */
    980      1.12    plunky 		le16enc(rsp + 0, cp.type);
    981      1.12    plunky 		le16enc(rsp + 2, L2CAP_SUCCESS);
    982      1.12    plunky 		le64enc(rsp + 4, 0x0000000000000002);
    983      1.12    plunky 		l2cap_send_signal(link, L2CAP_INFO_RSP, cmd.ident, 12, rsp);
    984      1.12    plunky 		break;
    985      1.12    plunky 
    986       1.1   gdamore 	case L2CAP_CONNLESS_MTU:
    987       1.1   gdamore 	default:
    988      1.12    plunky 		le16enc(rsp + 0, cp.type);
    989      1.12    plunky 		le16enc(rsp + 2, L2CAP_NOT_SUPPORTED);
    990      1.12    plunky 		l2cap_send_signal(link, L2CAP_INFO_RSP, cmd.ident, 4, rsp);
    991       1.1   gdamore 		break;
    992       1.1   gdamore 	}
    993       1.1   gdamore }
    994       1.1   gdamore 
    995       1.1   gdamore /*
    996       1.1   gdamore  * Construct signal and wrap in C-Frame for link.
    997       1.1   gdamore  */
    998       1.1   gdamore static int
    999       1.1   gdamore l2cap_send_signal(struct hci_link *link, uint8_t code, uint8_t ident,
   1000       1.1   gdamore 			uint16_t length, void *data)
   1001       1.1   gdamore {
   1002       1.1   gdamore 	struct mbuf *m;
   1003       1.1   gdamore 	l2cap_hdr_t *hdr;
   1004       1.1   gdamore 	l2cap_cmd_hdr_t *cmd;
   1005       1.1   gdamore 
   1006      1.14    plunky 	KASSERT(link != NULL);
   1007      1.14    plunky 	KASSERT(sizeof(l2cap_cmd_hdr_t) + length <= link->hl_mtu);
   1008       1.1   gdamore 
   1009       1.1   gdamore 	m = m_gethdr(M_DONTWAIT, MT_DATA);
   1010       1.1   gdamore 	if (m == NULL)
   1011       1.1   gdamore 		return ENOMEM;
   1012       1.1   gdamore 
   1013       1.1   gdamore 	hdr = mtod(m, l2cap_hdr_t *);
   1014       1.1   gdamore 	cmd = (l2cap_cmd_hdr_t *)(hdr + 1);
   1015       1.1   gdamore 
   1016       1.1   gdamore 	m->m_len = m->m_pkthdr.len = MHLEN;
   1017       1.1   gdamore 
   1018       1.1   gdamore 	/* Command Data */
   1019       1.1   gdamore 	if (length > 0)
   1020       1.8    plunky 		m_copyback(m, sizeof(*hdr) + sizeof(*cmd), length, data);
   1021       1.1   gdamore 
   1022       1.1   gdamore 	/* Command Header */
   1023       1.1   gdamore 	cmd->code = code;
   1024       1.1   gdamore 	cmd->ident = ident;
   1025       1.1   gdamore 	cmd->length = htole16(length);
   1026       1.8    plunky 	length += sizeof(*cmd);
   1027       1.1   gdamore 
   1028       1.1   gdamore 	/* C-Frame Header */
   1029       1.1   gdamore 	hdr->length = htole16(length);
   1030       1.1   gdamore 	hdr->dcid = htole16(L2CAP_SIGNAL_CID);
   1031       1.8    plunky 	length += sizeof(*hdr);
   1032       1.1   gdamore 
   1033       1.1   gdamore 	if (m->m_pkthdr.len != MAX(MHLEN, length)) {
   1034       1.1   gdamore 		m_freem(m);
   1035       1.1   gdamore 		return ENOMEM;
   1036       1.1   gdamore 	}
   1037       1.1   gdamore 
   1038       1.1   gdamore 	m->m_pkthdr.len = length;
   1039       1.1   gdamore 	m->m_len = MIN(length, MHLEN);
   1040       1.1   gdamore 
   1041       1.1   gdamore 	DPRINTFN(2, "(%s) code %d, ident %d, len %d\n",
   1042       1.9    plunky 		device_xname(link->hl_unit->hci_dev), code, ident, length);
   1043       1.1   gdamore 
   1044       1.1   gdamore 	return hci_acl_send(m, link, NULL);
   1045       1.1   gdamore }
   1046       1.1   gdamore 
   1047       1.1   gdamore /*
   1048       1.1   gdamore  * Send Command Reject packet.
   1049       1.1   gdamore  */
   1050       1.1   gdamore static int
   1051       1.1   gdamore l2cap_send_command_rej(struct hci_link *link, uint8_t ident,
   1052  1.15.8.2  jdolecek 			int reason, ...)
   1053       1.1   gdamore {
   1054       1.1   gdamore 	l2cap_cmd_rej_cp cp;
   1055       1.1   gdamore 	int len = 0;
   1056       1.1   gdamore 	va_list ap;
   1057       1.1   gdamore 
   1058       1.1   gdamore 	va_start(ap, reason);
   1059       1.1   gdamore 
   1060       1.1   gdamore 	cp.reason = htole16(reason);
   1061       1.1   gdamore 
   1062       1.1   gdamore 	switch (reason) {
   1063       1.1   gdamore 	case L2CAP_REJ_NOT_UNDERSTOOD:
   1064       1.1   gdamore 		len = 2;
   1065       1.1   gdamore 		break;
   1066       1.1   gdamore 
   1067       1.1   gdamore 	case L2CAP_REJ_MTU_EXCEEDED:
   1068       1.1   gdamore 		len = 4;
   1069       1.1   gdamore 		cp.data[0] = va_arg(ap, int);		/* SigMTU */
   1070       1.1   gdamore 		cp.data[0] = htole16(cp.data[0]);
   1071       1.1   gdamore 		break;
   1072       1.1   gdamore 
   1073       1.1   gdamore 	case L2CAP_REJ_INVALID_CID:
   1074       1.1   gdamore 		len = 6;
   1075       1.1   gdamore 		cp.data[0] = va_arg(ap, int);		/* dcid */
   1076       1.1   gdamore 		cp.data[0] = htole16(cp.data[0]);
   1077       1.1   gdamore 		cp.data[1] = va_arg(ap, int);		/* scid */
   1078       1.1   gdamore 		cp.data[1] = htole16(cp.data[1]);
   1079       1.1   gdamore 		break;
   1080       1.1   gdamore 
   1081       1.1   gdamore 	default:
   1082       1.1   gdamore 		UNKNOWN(reason);
   1083  1.15.8.1       tls 		va_end(ap);
   1084       1.1   gdamore 		return EINVAL;
   1085       1.1   gdamore 	}
   1086       1.1   gdamore 
   1087       1.1   gdamore 	va_end(ap);
   1088       1.1   gdamore 
   1089       1.1   gdamore 	return l2cap_send_signal(link, L2CAP_COMMAND_REJ, ident, len, &cp);
   1090       1.1   gdamore }
   1091       1.1   gdamore 
   1092       1.1   gdamore /*
   1093       1.1   gdamore  * Send Connect Request
   1094       1.1   gdamore  */
   1095       1.1   gdamore int
   1096       1.1   gdamore l2cap_send_connect_req(struct l2cap_channel *chan)
   1097       1.1   gdamore {
   1098       1.1   gdamore 	l2cap_con_req_cp cp;
   1099       1.1   gdamore 	int err;
   1100       1.1   gdamore 
   1101       1.1   gdamore 	err = l2cap_request_alloc(chan, L2CAP_CONNECT_REQ);
   1102       1.1   gdamore 	if (err)
   1103       1.1   gdamore 		return err;
   1104       1.1   gdamore 
   1105       1.1   gdamore 	cp.psm = htole16(chan->lc_raddr.bt_psm);
   1106       1.1   gdamore 	cp.scid = htole16(chan->lc_lcid);
   1107       1.1   gdamore 
   1108       1.1   gdamore 	return l2cap_send_signal(chan->lc_link, L2CAP_CONNECT_REQ,
   1109       1.1   gdamore 				chan->lc_link->hl_lastid, sizeof(cp), &cp);
   1110       1.1   gdamore }
   1111       1.1   gdamore 
   1112       1.1   gdamore /*
   1113       1.1   gdamore  * Send Config Request
   1114       1.1   gdamore  *
   1115       1.1   gdamore  * For outgoing config request, we only put options in the packet if they
   1116       1.1   gdamore  * differ from the default and would have to be actioned. We dont support
   1117       1.1   gdamore  * enough option types to make overflowing SigMTU an issue so it can all
   1118       1.1   gdamore  * go in one packet.
   1119       1.1   gdamore  */
   1120       1.1   gdamore int
   1121       1.1   gdamore l2cap_send_config_req(struct l2cap_channel *chan)
   1122       1.1   gdamore {
   1123       1.1   gdamore 	l2cap_cfg_req_cp *cp;
   1124       1.1   gdamore 	l2cap_cfg_opt_t *opt;
   1125       1.1   gdamore 	l2cap_cfg_opt_val_t *val;
   1126       1.1   gdamore 	uint8_t *next, buf[L2CAP_MTU_MINIMUM];
   1127       1.1   gdamore 	int err;
   1128       1.1   gdamore 
   1129       1.1   gdamore 	err = l2cap_request_alloc(chan, L2CAP_CONFIG_REQ);
   1130       1.1   gdamore 	if (err)
   1131       1.1   gdamore 		return err;
   1132       1.1   gdamore 
   1133       1.1   gdamore 	/* Config Header (4 octets) */
   1134       1.1   gdamore 	cp = (l2cap_cfg_req_cp *)buf;
   1135       1.1   gdamore 	cp->dcid = htole16(chan->lc_rcid);
   1136       1.1   gdamore 	cp->flags = 0;	/* "No Continuation" */
   1137       1.1   gdamore 
   1138       1.1   gdamore 	next = buf + sizeof(l2cap_cfg_req_cp);
   1139       1.1   gdamore 
   1140       1.1   gdamore 	/* Incoming MTU (4 octets) */
   1141       1.1   gdamore 	if (chan->lc_imtu != L2CAP_MTU_DEFAULT) {
   1142       1.1   gdamore 		opt = (l2cap_cfg_opt_t *)next;
   1143       1.1   gdamore 		opt->type = L2CAP_OPT_MTU;
   1144       1.1   gdamore 		opt->length = L2CAP_OPT_MTU_SIZE;
   1145       1.1   gdamore 
   1146       1.1   gdamore 		val = (l2cap_cfg_opt_val_t *)(opt + 1);
   1147       1.1   gdamore 		val->mtu = htole16(chan->lc_imtu);
   1148       1.1   gdamore 
   1149       1.1   gdamore 		next += sizeof(l2cap_cfg_opt_t) + L2CAP_OPT_MTU_SIZE;
   1150       1.1   gdamore 	}
   1151       1.1   gdamore 
   1152       1.1   gdamore 	/* Flush Timeout (4 octets) */
   1153       1.1   gdamore 	if (chan->lc_flush != L2CAP_FLUSH_TIMO_DEFAULT) {
   1154       1.1   gdamore 		opt = (l2cap_cfg_opt_t *)next;
   1155       1.1   gdamore 		opt->type = L2CAP_OPT_FLUSH_TIMO;
   1156       1.1   gdamore 		opt->length = L2CAP_OPT_FLUSH_TIMO_SIZE;
   1157       1.1   gdamore 
   1158       1.1   gdamore 		val = (l2cap_cfg_opt_val_t *)(opt + 1);
   1159       1.1   gdamore 		val->flush_timo = htole16(chan->lc_flush);
   1160       1.1   gdamore 
   1161       1.1   gdamore 		next += sizeof(l2cap_cfg_opt_t) + L2CAP_OPT_FLUSH_TIMO_SIZE;
   1162       1.1   gdamore 	}
   1163       1.1   gdamore 
   1164       1.1   gdamore 	/* Outgoing QoS Flow (24 octets) */
   1165       1.1   gdamore 	/* Retransmission & Flow Control (11 octets) */
   1166       1.1   gdamore 	/*
   1167       1.1   gdamore 	 * From here we need to start paying attention to SigMTU as we have
   1168       1.1   gdamore 	 * possibly overflowed the minimum supported..
   1169       1.1   gdamore 	 */
   1170       1.1   gdamore 
   1171       1.1   gdamore 	return l2cap_send_signal(chan->lc_link, L2CAP_CONFIG_REQ,
   1172       1.1   gdamore 				    chan->lc_link->hl_lastid, (int)(next - buf), buf);
   1173       1.1   gdamore }
   1174       1.1   gdamore 
   1175       1.1   gdamore /*
   1176       1.1   gdamore  * Send Disconnect Request
   1177       1.1   gdamore  */
   1178       1.1   gdamore int
   1179       1.1   gdamore l2cap_send_disconnect_req(struct l2cap_channel *chan)
   1180       1.1   gdamore {
   1181       1.1   gdamore 	l2cap_discon_req_cp cp;
   1182       1.1   gdamore 	int err;
   1183       1.1   gdamore 
   1184       1.1   gdamore 	err = l2cap_request_alloc(chan, L2CAP_DISCONNECT_REQ);
   1185       1.1   gdamore 	if (err)
   1186       1.1   gdamore 		return err;
   1187       1.1   gdamore 
   1188       1.1   gdamore 	cp.dcid = htole16(chan->lc_rcid);
   1189       1.1   gdamore 	cp.scid = htole16(chan->lc_lcid);
   1190       1.1   gdamore 
   1191       1.1   gdamore 	return l2cap_send_signal(chan->lc_link, L2CAP_DISCONNECT_REQ,
   1192       1.1   gdamore 				    chan->lc_link->hl_lastid, sizeof(cp), &cp);
   1193       1.1   gdamore }
   1194       1.7    plunky 
   1195       1.7    plunky /*
   1196       1.7    plunky  * Send Connect Response
   1197       1.7    plunky  */
   1198       1.7    plunky int
   1199       1.7    plunky l2cap_send_connect_rsp(struct hci_link *link, uint8_t ident, uint16_t dcid, uint16_t scid, uint16_t result)
   1200       1.7    plunky {
   1201       1.7    plunky 	l2cap_con_rsp_cp cp;
   1202       1.7    plunky 
   1203       1.7    plunky 	memset(&cp, 0, sizeof(cp));
   1204       1.7    plunky 	cp.dcid = htole16(dcid);
   1205       1.7    plunky 	cp.scid = htole16(scid);
   1206       1.7    plunky 	cp.result = htole16(result);
   1207       1.7    plunky 
   1208       1.7    plunky 	return l2cap_send_signal(link, L2CAP_CONNECT_RSP, ident, sizeof(cp), &cp);
   1209       1.7    plunky }
   1210      1.15    plunky 
   1211      1.15    plunky /*
   1212      1.15    plunky  * copy in QoS buffer to host
   1213      1.15    plunky  */
   1214      1.15    plunky static void
   1215      1.15    plunky l2cap_qos_btoh(l2cap_qos_t *qos, void *buf)
   1216      1.15    plunky {
   1217      1.15    plunky 	l2cap_qos_t *src = buf;
   1218      1.15    plunky 
   1219      1.15    plunky 	qos->flags = src->flags;
   1220      1.15    plunky 	qos->service_type = src->service_type;
   1221      1.15    plunky 	qos->token_rate = le32toh(src->token_rate);
   1222      1.15    plunky 	qos->token_bucket_size = le32toh(src->token_bucket_size);
   1223      1.15    plunky 	qos->peak_bandwidth = le32toh(src->peak_bandwidth);
   1224      1.15    plunky 	qos->latency = le32toh(src->latency);
   1225      1.15    plunky 	qos->delay_variation = le32toh(src->delay_variation);
   1226      1.15    plunky }
   1227      1.15    plunky 
   1228      1.15    plunky /*
   1229      1.15    plunky  * copy out host QoS to buffer
   1230      1.15    plunky  */
   1231      1.15    plunky static void
   1232      1.15    plunky l2cap_qos_htob(void *buf, l2cap_qos_t *qos)
   1233      1.15    plunky {
   1234      1.15    plunky 	l2cap_qos_t *dst = buf;
   1235      1.15    plunky 
   1236      1.15    plunky 	dst->flags = qos->flags;
   1237      1.15    plunky 	dst->service_type = qos->service_type;
   1238      1.15    plunky 	dst->token_rate = htole32(qos->token_rate);
   1239      1.15    plunky 	dst->token_bucket_size = htole32(qos->token_bucket_size);
   1240      1.15    plunky 	dst->peak_bandwidth = htole32(qos->peak_bandwidth);
   1241      1.15    plunky 	dst->latency = htole32(qos->latency);
   1242      1.15    plunky 	dst->delay_variation = htole32(qos->delay_variation);
   1243      1.15    plunky }
   1244