Home | History | Annotate | Line # | Download | only in netbt
l2cap_socket.c revision 1.16
      1 /*	$NetBSD: l2cap_socket.c,v 1.16 2014/06/22 08:10:18 rtr 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_socket.c,v 1.16 2014/06/22 08:10:18 rtr Exp $");
     35 
     36 /* load symbolic names */
     37 #ifdef BLUETOOTH_DEBUG
     38 #define PRUREQUESTS
     39 #define PRCOREQUESTS
     40 #endif
     41 
     42 #include <sys/param.h>
     43 #include <sys/domain.h>
     44 #include <sys/kernel.h>
     45 #include <sys/mbuf.h>
     46 #include <sys/proc.h>
     47 #include <sys/protosw.h>
     48 #include <sys/socket.h>
     49 #include <sys/socketvar.h>
     50 #include <sys/systm.h>
     51 
     52 #include <netbt/bluetooth.h>
     53 #include <netbt/l2cap.h>
     54 
     55 /*
     56  * L2CAP Sockets
     57  *
     58  *	SOCK_SEQPACKET - normal L2CAP connection
     59  *
     60  *	SOCK_DGRAM - connectionless L2CAP - XXX not yet
     61  */
     62 
     63 static void l2cap_connecting(void *);
     64 static void l2cap_connected(void *);
     65 static void l2cap_disconnected(void *, int);
     66 static void *l2cap_newconn(void *, struct sockaddr_bt *, struct sockaddr_bt *);
     67 static void l2cap_complete(void *, int);
     68 static void l2cap_linkmode(void *, int);
     69 static void l2cap_input(void *, struct mbuf *);
     70 
     71 static const struct btproto l2cap_proto = {
     72 	l2cap_connecting,
     73 	l2cap_connected,
     74 	l2cap_disconnected,
     75 	l2cap_newconn,
     76 	l2cap_complete,
     77 	l2cap_linkmode,
     78 	l2cap_input,
     79 };
     80 
     81 /* sysctl variables */
     82 int l2cap_sendspace = 4096;
     83 int l2cap_recvspace = 4096;
     84 
     85 static int
     86 l2cap_attach(struct socket *so, int proto)
     87 {
     88 	int error;
     89 
     90 	KASSERT(so->so_pcb == NULL);
     91 
     92 	if (so->so_lock == NULL) {
     93 		mutex_obj_hold(bt_lock);
     94 		so->so_lock = bt_lock;
     95 		solock(so);
     96 	}
     97 	KASSERT(solocked(so));
     98 
     99 	/*
    100 	 * For L2CAP socket PCB we just use an l2cap_channel structure
    101 	 * since we have nothing to add..
    102 	 */
    103 	error = soreserve(so, l2cap_sendspace, l2cap_recvspace);
    104 	if (error)
    105 		return error;
    106 
    107 	return l2cap_attach_pcb((struct l2cap_channel **)&so->so_pcb,
    108 				&l2cap_proto, so);
    109 }
    110 
    111 static void
    112 l2cap_detach(struct socket *so)
    113 {
    114 	KASSERT(so->so_pcb != NULL);
    115 	l2cap_detach_pcb((struct l2cap_channel **)&so->so_pcb);
    116 	KASSERT(so->so_pcb == NULL);
    117 }
    118 
    119 static int
    120 l2cap_ioctl(struct socket *up, struct mbuf *m,
    121     struct mbuf *nam, struct mbuf *ctl, struct lwp *l)
    122 {
    123 	return EPASSTHROUGH;
    124 }
    125 
    126 /*
    127  * User Request.
    128  * up is socket
    129  * m is optional mbuf chain containing message
    130  * nam is either
    131  *	optional mbuf chain containing an address
    132  *	message flags (PRU_RCVD)
    133  * ctl is either
    134  *	optional mbuf chain containing socket options
    135  *	optional interface pointer PRU_PURGEIF
    136  * l is pointer to process requesting action (if any)
    137  *
    138  * we are responsible for disposing of m and ctl if
    139  * they are mbuf chains
    140  */
    141 static int
    142 l2cap_usrreq(struct socket *up, int req, struct mbuf *m,
    143     struct mbuf *nam, struct mbuf *ctl, struct lwp *l)
    144 {
    145 	struct l2cap_channel *pcb = up->so_pcb;
    146 	struct sockaddr_bt *sa;
    147 	struct mbuf *m0;
    148 	int err = 0;
    149 
    150 	DPRINTFN(2, "%s\n", prurequests[req]);
    151 	KASSERT(req != PRU_ATTACH);
    152 	KASSERT(req != PRU_DETACH);
    153 	KASSERT(req != PRU_CONTROL);
    154 
    155 	switch (req) {
    156 	case PRU_PURGEIF:
    157 		return EOPNOTSUPP;
    158 	}
    159 
    160 	if (pcb == NULL) {
    161 		err = EINVAL;
    162 		goto release;
    163 	}
    164 
    165 	switch(req) {
    166 	case PRU_DISCONNECT:
    167 		soisdisconnecting(up);
    168 		return l2cap_disconnect(pcb, up->so_linger);
    169 
    170 	case PRU_ABORT:
    171 		l2cap_disconnect(pcb, 0);
    172 		soisdisconnected(up);
    173 		l2cap_detach(up);
    174 		return 0;
    175 
    176 	case PRU_BIND:
    177 		KASSERT(nam != NULL);
    178 		sa = mtod(nam, struct sockaddr_bt *);
    179 
    180 		if (sa->bt_len != sizeof(struct sockaddr_bt))
    181 			return EINVAL;
    182 
    183 		if (sa->bt_family != AF_BLUETOOTH)
    184 			return EAFNOSUPPORT;
    185 
    186 		return l2cap_bind(pcb, sa);
    187 
    188 	case PRU_CONNECT:
    189 		KASSERT(nam != NULL);
    190 		sa = mtod(nam, struct sockaddr_bt *);
    191 
    192 		if (sa->bt_len != sizeof(struct sockaddr_bt))
    193 			return EINVAL;
    194 
    195 		if (sa->bt_family != AF_BLUETOOTH)
    196 			return EAFNOSUPPORT;
    197 
    198 		soisconnecting(up);
    199 		return l2cap_connect(pcb, sa);
    200 
    201 	case PRU_PEERADDR:
    202 		KASSERT(nam != NULL);
    203 		sa = mtod(nam, struct sockaddr_bt *);
    204 		nam->m_len = sizeof(struct sockaddr_bt);
    205 		return l2cap_peeraddr(pcb, sa);
    206 
    207 	case PRU_SOCKADDR:
    208 		KASSERT(nam != NULL);
    209 		sa = mtod(nam, struct sockaddr_bt *);
    210 		nam->m_len = sizeof(struct sockaddr_bt);
    211 		return l2cap_sockaddr(pcb, sa);
    212 
    213 	case PRU_SHUTDOWN:
    214 		socantsendmore(up);
    215 		break;
    216 
    217 	case PRU_SEND:
    218 		KASSERT(m != NULL);
    219 		if (m->m_pkthdr.len == 0)
    220 			break;
    221 
    222 		if (m->m_pkthdr.len > pcb->lc_omtu) {
    223 			err = EMSGSIZE;
    224 			break;
    225 		}
    226 
    227 		m0 = m_copypacket(m, M_DONTWAIT);
    228 		if (m0 == NULL) {
    229 			err = ENOMEM;
    230 			break;
    231 		}
    232 
    233 		if (ctl)	/* no use for that */
    234 			m_freem(ctl);
    235 
    236 		sbappendrecord(&up->so_snd, m);
    237 		return l2cap_send(pcb, m0);
    238 
    239 	case PRU_SENSE:
    240 		return 0;		/* (no release) */
    241 
    242 	case PRU_RCVD:
    243 	case PRU_RCVOOB:
    244 		return EOPNOTSUPP;	/* (no release) */
    245 
    246 	case PRU_LISTEN:
    247 		return l2cap_listen(pcb);
    248 
    249 	case PRU_ACCEPT:
    250 		KASSERT(nam != NULL);
    251 		sa = mtod(nam, struct sockaddr_bt *);
    252 		nam->m_len = sizeof(struct sockaddr_bt);
    253 		return l2cap_peeraddr(pcb, sa);
    254 
    255 	case PRU_CONNECT2:
    256 	case PRU_SENDOOB:
    257 	case PRU_FASTTIMO:
    258 	case PRU_SLOWTIMO:
    259 	case PRU_PROTORCV:
    260 	case PRU_PROTOSEND:
    261 		err = EOPNOTSUPP;
    262 		break;
    263 
    264 	default:
    265 		UNKNOWN(req);
    266 		err = EOPNOTSUPP;
    267 		break;
    268 	}
    269 
    270 release:
    271 	if (m) m_freem(m);
    272 	if (ctl) m_freem(ctl);
    273 	return err;
    274 }
    275 
    276 /*
    277  * l2cap_ctloutput(req, socket, sockopt)
    278  *
    279  *	Apply configuration commands to channel. This corresponds to
    280  *	"Reconfigure Channel Request" in the L2CAP specification.
    281  */
    282 int
    283 l2cap_ctloutput(int req, struct socket *so, struct sockopt *sopt)
    284 {
    285 	struct l2cap_channel *pcb = so->so_pcb;
    286 	int err = 0;
    287 
    288 	DPRINTFN(2, "%s\n", prcorequests[req]);
    289 
    290 	if (pcb == NULL)
    291 		return EINVAL;
    292 
    293 	if (sopt->sopt_level != BTPROTO_L2CAP)
    294 		return ENOPROTOOPT;
    295 
    296 	switch(req) {
    297 	case PRCO_GETOPT:
    298 		err = l2cap_getopt(pcb, sopt);
    299 		break;
    300 
    301 	case PRCO_SETOPT:
    302 		err = l2cap_setopt(pcb, sopt);
    303 		break;
    304 
    305 	default:
    306 		err = ENOPROTOOPT;
    307 		break;
    308 	}
    309 
    310 	return err;
    311 }
    312 
    313 /**********************************************************************
    314  *
    315  *	L2CAP Protocol socket callbacks
    316  *
    317  */
    318 
    319 static void
    320 l2cap_connecting(void *arg)
    321 {
    322 	struct socket *so = arg;
    323 
    324 	DPRINTF("Connecting\n");
    325 	soisconnecting(so);
    326 }
    327 
    328 static void
    329 l2cap_connected(void *arg)
    330 {
    331 	struct socket *so = arg;
    332 
    333 	DPRINTF("Connected\n");
    334 	soisconnected(so);
    335 }
    336 
    337 static void
    338 l2cap_disconnected(void *arg, int err)
    339 {
    340 	struct socket *so = arg;
    341 
    342 	DPRINTF("Disconnected (%d)\n", err);
    343 
    344 	so->so_error = err;
    345 	soisdisconnected(so);
    346 }
    347 
    348 static void *
    349 l2cap_newconn(void *arg, struct sockaddr_bt *laddr,
    350     struct sockaddr_bt *raddr)
    351 {
    352 	struct socket *so = arg;
    353 
    354 	DPRINTF("New Connection\n");
    355 	so = sonewconn(so, false);
    356 	if (so == NULL)
    357 		return NULL;
    358 
    359 	soisconnecting(so);
    360 
    361 	return so->so_pcb;
    362 }
    363 
    364 static void
    365 l2cap_complete(void *arg, int count)
    366 {
    367 	struct socket *so = arg;
    368 
    369 	while (count-- > 0)
    370 		sbdroprecord(&so->so_snd);
    371 
    372 	sowwakeup(so);
    373 }
    374 
    375 static void
    376 l2cap_linkmode(void *arg, int new)
    377 {
    378 	struct socket *so = arg;
    379 	struct sockopt sopt;
    380 	int mode;
    381 
    382 	DPRINTF("auth %s, encrypt %s, secure %s\n",
    383 		(new & L2CAP_LM_AUTH ? "on" : "off"),
    384 		(new & L2CAP_LM_ENCRYPT ? "on" : "off"),
    385 		(new & L2CAP_LM_SECURE ? "on" : "off"));
    386 
    387 	sockopt_init(&sopt, BTPROTO_L2CAP, SO_L2CAP_LM, 0);
    388 	(void)l2cap_getopt(so->so_pcb, &sopt);
    389 	(void)sockopt_getint(&sopt, &mode);
    390 	sockopt_destroy(&sopt);
    391 
    392 	if (((mode & L2CAP_LM_AUTH) && !(new & L2CAP_LM_AUTH))
    393 	    || ((mode & L2CAP_LM_ENCRYPT) && !(new & L2CAP_LM_ENCRYPT))
    394 	    || ((mode & L2CAP_LM_SECURE) && !(new & L2CAP_LM_SECURE)))
    395 		l2cap_disconnect(so->so_pcb, 0);
    396 }
    397 
    398 static void
    399 l2cap_input(void *arg, struct mbuf *m)
    400 {
    401 	struct socket *so = arg;
    402 
    403 	if (m->m_pkthdr.len > sbspace(&so->so_rcv)) {
    404 		printf("%s: packet (%d bytes) dropped (socket buffer full)\n",
    405 			__func__, m->m_pkthdr.len);
    406 		m_freem(m);
    407 		return;
    408 	}
    409 
    410 	DPRINTFN(10, "received %d bytes\n", m->m_pkthdr.len);
    411 
    412 	sbappendrecord(&so->so_rcv, m);
    413 	sorwakeup(so);
    414 }
    415 
    416 PR_WRAP_USRREQS(l2cap)
    417 
    418 #define	l2cap_attach		l2cap_attach_wrapper
    419 #define	l2cap_detach		l2cap_detach_wrapper
    420 #define	l2cap_ioctl		l2cap_ioctl_wrapper
    421 #define	l2cap_usrreq		l2cap_usrreq_wrapper
    422 
    423 const struct pr_usrreqs l2cap_usrreqs = {
    424 	.pr_attach	= l2cap_attach,
    425 	.pr_detach	= l2cap_detach,
    426 	.pr_ioctl	= l2cap_ioctl,
    427 	.pr_generic	= l2cap_usrreq,
    428 };
    429