Home | History | Annotate | Line # | Download | only in netbt
l2cap_upper.c revision 1.3
      1 /*	$NetBSD: l2cap_upper.c,v 1.3 2007/03/12 20:34:54 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_upper.c,v 1.3 2007/03/12 20:34:54 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/socket.h>
     42 #include <sys/socketvar.h>
     43 #include <sys/systm.h>
     44 
     45 #include <netbt/bluetooth.h>
     46 #include <netbt/hci.h>
     47 #include <netbt/l2cap.h>
     48 
     49 /*******************************************************************************
     50  *
     51  *	L2CAP Channel - Upper Protocol API
     52  */
     53 
     54 /*
     55  * l2cap_attach(handle, btproto, upper)
     56  *
     57  *	attach new l2cap_channel to handle, populate
     58  *	with with reasonable defaults
     59  */
     60 int
     61 l2cap_attach(struct l2cap_channel **handle,
     62 		const struct btproto *proto, void *upper)
     63 {
     64 	struct l2cap_channel *chan;
     65 
     66 	KASSERT(handle);
     67 	KASSERT(proto);
     68 	KASSERT(upper);
     69 
     70 	chan = malloc(sizeof(struct l2cap_channel), M_BLUETOOTH,
     71 			M_NOWAIT | M_ZERO);
     72 	if (chan == NULL)
     73 		return ENOMEM;
     74 
     75 	chan->lc_proto = proto;
     76 	chan->lc_upper = upper;
     77 
     78 	chan->lc_state = L2CAP_CLOSED;
     79 
     80 	chan->lc_lcid = L2CAP_NULL_CID;
     81 	chan->lc_rcid = L2CAP_NULL_CID;
     82 
     83 	chan->lc_laddr.bt_len = sizeof(struct sockaddr_bt);
     84 	chan->lc_laddr.bt_family = AF_BLUETOOTH;
     85 	chan->lc_laddr.bt_psm = L2CAP_PSM_ANY;
     86 
     87 	chan->lc_raddr.bt_len = sizeof(struct sockaddr_bt);
     88 	chan->lc_raddr.bt_family = AF_BLUETOOTH;
     89 	chan->lc_raddr.bt_psm = L2CAP_PSM_ANY;
     90 
     91 	chan->lc_imtu = L2CAP_MTU_DEFAULT;
     92 	chan->lc_omtu = L2CAP_MTU_DEFAULT;
     93 	chan->lc_flush = L2CAP_FLUSH_TIMO_DEFAULT;
     94 
     95 	memcpy(&chan->lc_iqos, &l2cap_default_qos, sizeof(l2cap_qos_t));
     96 	memcpy(&chan->lc_oqos, &l2cap_default_qos, sizeof(l2cap_qos_t));
     97 
     98 	MBUFQ_INIT(&chan->lc_txq);
     99 
    100 	*handle = chan;
    101 	return 0;
    102 }
    103 
    104 /*
    105  * l2cap_bind(l2cap_channel, sockaddr)
    106  *
    107  *	set local address of channel
    108  */
    109 int
    110 l2cap_bind(struct l2cap_channel *chan, struct sockaddr_bt *addr)
    111 {
    112 
    113 	memcpy(&chan->lc_laddr, addr, sizeof(struct sockaddr_bt));
    114 	return 0;
    115 }
    116 
    117 /*
    118  * l2cap_sockaddr(l2cap_channel, sockaddr)
    119  *
    120  *	get local address of channel
    121  */
    122 int
    123 l2cap_sockaddr(struct l2cap_channel *chan, struct sockaddr_bt *addr)
    124 {
    125 
    126 	memcpy(addr, &chan->lc_laddr, sizeof(struct sockaddr_bt));
    127 	return 0;
    128 }
    129 
    130 /*
    131  * l2cap_connect(l2cap_channel, sockaddr)
    132  *
    133  *	Initiate a connection to destination. This corresponds to
    134  *	"Open Channel Request" in the L2CAP specification and will
    135  *	result in one of the following:
    136  *
    137  *		proto->connected(upper)
    138  *		proto->disconnected(upper, error)
    139  *
    140  *	and, optionally
    141  *		proto->connecting(upper)
    142  */
    143 int
    144 l2cap_connect(struct l2cap_channel *chan, struct sockaddr_bt *dest)
    145 {
    146 	struct hci_unit *unit;
    147 	int err;
    148 
    149 	memcpy(&chan->lc_raddr, dest, sizeof(struct sockaddr_bt));
    150 
    151 	if (L2CAP_PSM_INVALID(chan->lc_raddr.bt_psm))
    152 		return EINVAL;
    153 
    154 	if (bdaddr_any(&chan->lc_raddr.bt_bdaddr))
    155 		return EDESTADDRREQ;
    156 
    157 	/* set local address if it needs setting */
    158 	if (bdaddr_any(&chan->lc_laddr.bt_bdaddr)) {
    159 		err = hci_route_lookup(&chan->lc_laddr.bt_bdaddr,
    160 					&chan->lc_raddr.bt_bdaddr);
    161 		if (err)
    162 			return err;
    163 	}
    164 
    165 	unit = hci_unit_lookup(&chan->lc_laddr.bt_bdaddr);
    166 	if (unit == NULL)
    167 		return EHOSTUNREACH;
    168 
    169 	/* attach to active list */
    170 	err = l2cap_cid_alloc(chan);
    171 	if (err)
    172 		return err;
    173 
    174 	/* open link to remote device */
    175 	chan->lc_link = hci_acl_open(unit, &chan->lc_raddr.bt_bdaddr);
    176 	if (chan->lc_link == NULL)
    177 		return EHOSTUNREACH;
    178 
    179 	/*
    180 	 * We queue a connect request right away even though the link
    181 	 * may not yet be open; the queue will be started automatically
    182 	 * at the right time.
    183 	 */
    184 	chan->lc_state = L2CAP_WAIT_CONNECT_RSP;
    185 	err = l2cap_send_connect_req(chan);
    186 	if (err) {
    187 		chan->lc_state = L2CAP_CLOSED;
    188 		hci_acl_close(chan->lc_link, err);
    189 		chan->lc_link = NULL;
    190 		return err;
    191 	}
    192 
    193 	return 0;
    194 }
    195 
    196 /*
    197  * l2cap_peeraddr(l2cap_channel, sockaddr)
    198  *
    199  *	get remote address of channel
    200  */
    201 int
    202 l2cap_peeraddr(struct l2cap_channel *chan, struct sockaddr_bt *addr)
    203 {
    204 
    205 	memcpy(addr, &chan->lc_raddr, sizeof(struct sockaddr_bt));
    206 	return 0;
    207 }
    208 
    209 /*
    210  * l2cap_disconnect(l2cap_channel, linger)
    211  *
    212  *	Initiate L2CAP disconnection. This corresponds to
    213  *	"Close Channel Request" in the L2CAP specification
    214  *	and will result in a call to
    215  *
    216  *		proto->disconnected(upper, error)
    217  *
    218  *	when the disconnection is complete. If linger is set,
    219  *	the call will not be made until data has flushed from
    220  *	the queue.
    221  */
    222 int
    223 l2cap_disconnect(struct l2cap_channel *chan, int linger)
    224 {
    225 	int err = 0;
    226 
    227 	if (chan->lc_state == L2CAP_CLOSED
    228 	    || chan->lc_state == L2CAP_WAIT_DISCONNECT)
    229 		return EINVAL;
    230 
    231 	chan->lc_flags |= L2CAP_SHUTDOWN;
    232 
    233 	/*
    234 	 * no need to do anything unless the queue is empty or
    235 	 * we are not lingering..
    236 	 */
    237 	if ((MBUFQ_FIRST(&chan->lc_txq) == NULL && chan->lc_pending == 0)
    238 	    || linger == 0) {
    239 		chan->lc_state = L2CAP_WAIT_DISCONNECT;
    240 		err = l2cap_send_disconnect_req(chan);
    241 		if (err)
    242 			l2cap_close(chan, err);
    243 	}
    244 	return err;
    245 }
    246 
    247 /*
    248  * l2cap_detach(handle)
    249  *
    250  *	Detach l2cap channel from handle & close it down
    251  */
    252 int
    253 l2cap_detach(struct l2cap_channel **handle)
    254 {
    255 	struct l2cap_channel *chan;
    256 
    257 	chan = *handle;
    258 	*handle = NULL;
    259 
    260 	if (chan->lc_state != L2CAP_CLOSED)
    261 		l2cap_close(chan, 0);
    262 
    263 	if (chan->lc_lcid != L2CAP_NULL_CID) {
    264 		LIST_REMOVE(chan, lc_ncid);
    265 		chan->lc_lcid = L2CAP_NULL_CID;
    266 	}
    267 
    268 	MBUFQ_DRAIN(&chan->lc_txq);
    269 
    270 	/*
    271 	 * Could implement some kind of delayed expunge to make sure that the
    272 	 * CID is really dead before it becomes available for reuse?
    273 	 */
    274 
    275 	free(chan, M_BLUETOOTH);
    276 	return 0;
    277 }
    278 
    279 /*
    280  * l2cap_listen(l2cap_channel)
    281  *
    282  *	Use this channel as a listening post (until detached). This will
    283  *	result in calls to:
    284  *
    285  *		proto->newconn(upper, laddr, raddr)
    286  *
    287  *	for incoming connections matching the psm and local address of the
    288  *	channel (NULL psm/address are permitted and match any protocol/device).
    289  *
    290  *	The upper layer should create and return a new channel.
    291  *
    292  *	You cannot use this channel for anything else subsequent to this call
    293  */
    294 int
    295 l2cap_listen(struct l2cap_channel *chan)
    296 {
    297 	struct l2cap_channel *used, *prev = NULL;
    298 
    299 	if (chan->lc_lcid != L2CAP_NULL_CID)
    300 		return EINVAL;
    301 
    302 	if (chan->lc_laddr.bt_psm != L2CAP_PSM_ANY
    303 	    && L2CAP_PSM_INVALID(chan->lc_laddr.bt_psm))
    304 		return EADDRNOTAVAIL;
    305 
    306 	/*
    307 	 * This CID is irrelevant, as the channel is not stored on the active
    308 	 * list and the socket code does not allow operations on listening
    309 	 * sockets, but we set it so the detach code knows to LIST_REMOVE the
    310 	 * channel.
    311 	 */
    312 	chan->lc_lcid = L2CAP_SIGNAL_CID;
    313 
    314 	/*
    315 	 * The list of listening channels is stored in an order such that new
    316 	 * listeners dont usurp current listeners, but that specific listening
    317 	 * takes precedence over promiscuous, and the connect request code can
    318 	 * easily use the first matching entry.
    319 	 */
    320 	LIST_FOREACH(used, &l2cap_listen_list, lc_ncid) {
    321 		if (used->lc_laddr.bt_psm < chan->lc_laddr.bt_psm)
    322 			break;
    323 
    324 		if (used->lc_laddr.bt_psm == chan->lc_laddr.bt_psm
    325 			&& bdaddr_any(&used->lc_laddr.bt_bdaddr)
    326 			&& !bdaddr_any(&chan->lc_laddr.bt_bdaddr))
    327 			break;
    328 
    329 		prev = used;
    330 	}
    331 
    332 	if (prev == NULL)
    333 		LIST_INSERT_HEAD(&l2cap_listen_list, chan, lc_ncid);
    334 	else
    335 		LIST_INSERT_AFTER(prev, chan, lc_ncid);
    336 
    337 	return 0;
    338 }
    339 
    340 /*
    341  * l2cap_send(l2cap_channel, mbuf)
    342  *
    343  *	Output SDU on channel described by channel. This corresponds
    344  *	to "Send Data Request" in the L2CAP specification. The upper
    345  *	layer will be notified when SDU's have completed sending by a
    346  *	call to:
    347  *
    348  *		proto->complete(upper, n)
    349  *
    350  *	(currently n == 1)
    351  *
    352  *	Note: I'm not sure how this will work out, but I think that
    353  *	if outgoing Retransmission Mode or Flow Control Mode is
    354  *	negotiated then this call will not be made until the SDU has
    355  *	been acknowleged by the peer L2CAP entity. For 'Best Effort'
    356  *	it will be made when the packet has cleared the controller
    357  *	buffers.
    358  *
    359  *	We only support Basic mode so far, so encapsulate with a
    360  *	B-Frame header and start sending if we are not already
    361  */
    362 int
    363 l2cap_send(struct l2cap_channel *chan, struct mbuf *m)
    364 {
    365 	l2cap_hdr_t *hdr;
    366 	int plen;
    367 
    368 	if (chan->lc_state == L2CAP_CLOSED) {
    369 		m_freem(m);
    370 		return ENOTCONN;
    371 	}
    372 
    373 	plen = m->m_pkthdr.len;
    374 
    375 	DPRINTFN(5, "send %d bytes on CID #%d (pending = %d)\n",
    376 		plen, chan->lc_lcid, chan->lc_pending);
    377 
    378 	/* Encapsulate with B-Frame */
    379 	M_PREPEND(m, sizeof(l2cap_hdr_t), M_DONTWAIT);
    380 	if (m == NULL)
    381 		return ENOMEM;
    382 
    383 	hdr = mtod(m, l2cap_hdr_t *);
    384 	hdr->length = htole16(plen);
    385 	hdr->dcid = htole16(chan->lc_rcid);
    386 
    387 	/* Queue it on our list */
    388 	MBUFQ_ENQUEUE(&chan->lc_txq, m);
    389 
    390 	/* If we are not sending, then start doing so */
    391 	if (chan->lc_pending == 0)
    392 		return l2cap_start(chan);
    393 
    394 	return 0;
    395 }
    396 
    397 /*
    398  * l2cap_setopt(channel, opt, addr)
    399  *
    400  *	Apply configuration options to channel. This corresponds to
    401  *	"Configure Channel Request" in the L2CAP specification.
    402  */
    403 int
    404 l2cap_setopt(struct l2cap_channel *chan, int opt, void *addr)
    405 {
    406 	int err = 0;
    407 	uint16_t tmp;
    408 
    409 	/*
    410 	 * currently we dont allow changing any options when channel
    411 	 * is other than closed. We could allow this (not sure why?)
    412 	 * but would have to instigate a configure request.
    413 	 */
    414 	if (chan->lc_state != L2CAP_CLOSED)
    415 		return EBUSY;
    416 
    417 	switch (opt) {
    418 	case SO_L2CAP_IMTU:	/* set Incoming MTU */
    419 		tmp = *(uint16_t *)addr;
    420 		if (tmp < L2CAP_MTU_MINIMUM) {
    421 			err = EINVAL;
    422 			break;
    423 		}
    424 
    425 		chan->lc_imtu = tmp;
    426 		break;
    427 
    428 	case SO_L2CAP_OQOS:	/* set Outgoing QoS flow spec */
    429 		// XXX
    430 		// memcpy(&chan->lc_oqos, addr, sizeof(l2cap_qos_t));
    431 		//break;
    432 
    433 	case SO_L2CAP_FLUSH:	/* set Outgoing Flush Timeout */
    434 		// XXX
    435 		// chan->lc_flush = *(uint16_t *)addr;
    436 		//break;
    437 
    438 	default:
    439 		err = ENOPROTOOPT;
    440 		break;
    441 	}
    442 
    443 	return err;
    444 }
    445 
    446 int
    447 l2cap_getopt(struct l2cap_channel *chan, int opt, void *addr)
    448 {
    449 
    450 	switch (opt) {
    451 	case SO_L2CAP_IMTU:	/* get Incoming MTU */
    452 		*(uint16_t *)addr = chan->lc_imtu;
    453 		return sizeof(uint16_t);
    454 
    455 	case SO_L2CAP_OMTU:	/* get Outgoing MTU */
    456 		*(uint16_t *)addr = chan->lc_omtu;
    457 		return sizeof(uint16_t);
    458 
    459 	case SO_L2CAP_IQOS:	/* get Incoming QoS flow spec */
    460 		memcpy(addr, &chan->lc_iqos, sizeof(l2cap_qos_t));
    461 		return sizeof(l2cap_qos_t);
    462 
    463 	case SO_L2CAP_OQOS:	/* get Outgoing QoS flow spec */
    464 		memcpy(addr, &chan->lc_oqos, sizeof(l2cap_qos_t));
    465 		return sizeof(l2cap_qos_t);
    466 
    467 	case SO_L2CAP_FLUSH:	/* get Flush Timeout */
    468 		*(uint16_t *)addr = chan->lc_flush;
    469 		return sizeof(uint16_t);
    470 
    471 	default:
    472 		break;
    473 	}
    474 
    475 	return 0;
    476 }
    477