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