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