Home | History | Annotate | Line # | Download | only in netbt
l2cap_socket.c revision 1.25
      1 /*	$NetBSD: l2cap_socket.c,v 1.25 2014/07/24 15:12:03 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.25 2014/07/24 15:12:03 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_accept(struct socket *so, struct mbuf *nam)
    121 {
    122 	struct l2cap_channel *pcb = so->so_pcb;
    123 	struct sockaddr_bt *sa;
    124 
    125 	KASSERT(solocked(so));
    126 	KASSERT(nam != NULL);
    127 
    128 	if (pcb == NULL)
    129 		return EINVAL;
    130 
    131 	sa = mtod(nam, struct sockaddr_bt *);
    132 	nam->m_len = sizeof(struct sockaddr_bt);
    133 	return l2cap_peeraddr_pcb(pcb, sa);
    134 }
    135 
    136 static int
    137 l2cap_bind(struct socket *so, struct mbuf *nam)
    138 {
    139 	struct l2cap_channel *pcb = so->so_pcb;
    140 	struct sockaddr_bt *sa;
    141 
    142 	KASSERT(solocked(so));
    143 	KASSERT(nam != NULL);
    144 
    145 	if (pcb == NULL)
    146 		return EINVAL;
    147 
    148 	sa = mtod(nam, struct sockaddr_bt *);
    149 	if (sa->bt_len != sizeof(struct sockaddr_bt))
    150 		return EINVAL;
    151 
    152 	if (sa->bt_family != AF_BLUETOOTH)
    153 		return EAFNOSUPPORT;
    154 
    155 	return l2cap_bind_pcb(pcb, sa);
    156 }
    157 
    158 static int
    159 l2cap_listen(struct socket *so)
    160 {
    161 	struct l2cap_channel *pcb = so->so_pcb;
    162 
    163 	KASSERT(solocked(so));
    164 
    165 	if (pcb == NULL)
    166 		return EINVAL;
    167 
    168 	return l2cap_listen_pcb(pcb);
    169 }
    170 
    171 static int
    172 l2cap_ioctl(struct socket *so, u_long cmd, void *nam, struct ifnet *ifp)
    173 {
    174 	return EPASSTHROUGH;
    175 }
    176 
    177 static int
    178 l2cap_stat(struct socket *so, struct stat *ub)
    179 {
    180 	KASSERT(solocked(so));
    181 
    182 	return 0;
    183 }
    184 
    185 static int
    186 l2cap_peeraddr(struct socket *so, struct mbuf *nam)
    187 {
    188 	struct l2cap_channel *pcb = so->so_pcb;
    189 	struct sockaddr_bt *sa;
    190 
    191 	KASSERT(solocked(so));
    192 	KASSERT(pcb != NULL);
    193 	KASSERT(nam != NULL);
    194 
    195 	sa = mtod(nam, struct sockaddr_bt *);
    196 	nam->m_len = sizeof(struct sockaddr_bt);
    197 	return l2cap_peeraddr_pcb(pcb, sa);
    198 }
    199 
    200 static int
    201 l2cap_sockaddr(struct socket *so, struct mbuf *nam)
    202 {
    203 	struct l2cap_channel *pcb = so->so_pcb;
    204 	struct sockaddr_bt *sa;
    205 
    206 	KASSERT(solocked(so));
    207 	KASSERT(pcb != NULL);
    208 	KASSERT(nam != NULL);
    209 
    210 	sa = mtod(nam, struct sockaddr_bt *);
    211 	nam->m_len = sizeof(struct sockaddr_bt);
    212 	return l2cap_sockaddr_pcb(pcb, sa);
    213 }
    214 
    215 static int
    216 l2cap_recvoob(struct socket *so, struct mbuf *m, int flags)
    217 {
    218 	KASSERT(solocked(so));
    219 
    220 	return EOPNOTSUPP;
    221 }
    222 
    223 static int
    224 l2cap_sendoob(struct socket *so, struct mbuf *m, struct mbuf *control)
    225 {
    226 	KASSERT(solocked(so));
    227 
    228 	if (m)
    229 		m_freem(m);
    230 	if (control)
    231 		m_freem(control);
    232 
    233 	return EOPNOTSUPP;
    234 }
    235 
    236 /*
    237  * User Request.
    238  * up is socket
    239  * m is optional mbuf chain containing message
    240  * nam is either
    241  *	optional mbuf chain containing an address
    242  *	message flags (PRU_RCVD)
    243  * ctl is either
    244  *	optional mbuf chain containing socket options
    245  *	optional interface pointer PRU_PURGEIF
    246  * l is pointer to process requesting action (if any)
    247  *
    248  * we are responsible for disposing of m and ctl if
    249  * they are mbuf chains
    250  */
    251 static int
    252 l2cap_usrreq(struct socket *up, int req, struct mbuf *m,
    253     struct mbuf *nam, struct mbuf *ctl, struct lwp *l)
    254 {
    255 	struct l2cap_channel *pcb = up->so_pcb;
    256 	struct sockaddr_bt *sa;
    257 	struct mbuf *m0;
    258 	int err = 0;
    259 
    260 	DPRINTFN(2, "%s\n", prurequests[req]);
    261 	KASSERT(req != PRU_ATTACH);
    262 	KASSERT(req != PRU_DETACH);
    263 	KASSERT(req != PRU_ACCEPT);
    264 	KASSERT(req != PRU_BIND);
    265 	KASSERT(req != PRU_LISTEN);
    266 	KASSERT(req != PRU_CONTROL);
    267 	KASSERT(req != PRU_SENSE);
    268 	KASSERT(req != PRU_PEERADDR);
    269 	KASSERT(req != PRU_SOCKADDR);
    270 	KASSERT(req != PRU_RCVOOB);
    271 	KASSERT(req != PRU_SENDOOB);
    272 
    273 	switch (req) {
    274 	case PRU_PURGEIF:
    275 		return EOPNOTSUPP;
    276 	}
    277 
    278 	if (pcb == NULL) {
    279 		err = EINVAL;
    280 		goto release;
    281 	}
    282 
    283 	switch(req) {
    284 	case PRU_DISCONNECT:
    285 		soisdisconnecting(up);
    286 		return l2cap_disconnect(pcb, up->so_linger);
    287 
    288 	case PRU_ABORT:
    289 		l2cap_disconnect(pcb, 0);
    290 		soisdisconnected(up);
    291 		l2cap_detach(up);
    292 		return 0;
    293 
    294 	case PRU_CONNECT:
    295 		KASSERT(nam != NULL);
    296 		sa = mtod(nam, struct sockaddr_bt *);
    297 
    298 		if (sa->bt_len != sizeof(struct sockaddr_bt))
    299 			return EINVAL;
    300 
    301 		if (sa->bt_family != AF_BLUETOOTH)
    302 			return EAFNOSUPPORT;
    303 
    304 		soisconnecting(up);
    305 		return l2cap_connect(pcb, sa);
    306 
    307 	case PRU_SHUTDOWN:
    308 		socantsendmore(up);
    309 		break;
    310 
    311 	case PRU_SEND:
    312 		KASSERT(m != NULL);
    313 		if (m->m_pkthdr.len == 0)
    314 			break;
    315 
    316 		if (m->m_pkthdr.len > pcb->lc_omtu) {
    317 			err = EMSGSIZE;
    318 			break;
    319 		}
    320 
    321 		m0 = m_copypacket(m, M_DONTWAIT);
    322 		if (m0 == NULL) {
    323 			err = ENOMEM;
    324 			break;
    325 		}
    326 
    327 		if (ctl)	/* no use for that */
    328 			m_freem(ctl);
    329 
    330 		sbappendrecord(&up->so_snd, m);
    331 		return l2cap_send(pcb, m0);
    332 
    333 	case PRU_RCVD:
    334 		return EOPNOTSUPP;	/* (no release) */
    335 
    336 	case PRU_CONNECT2:
    337 	case PRU_FASTTIMO:
    338 	case PRU_SLOWTIMO:
    339 	case PRU_PROTORCV:
    340 	case PRU_PROTOSEND:
    341 		err = EOPNOTSUPP;
    342 		break;
    343 
    344 	default:
    345 		UNKNOWN(req);
    346 		err = EOPNOTSUPP;
    347 		break;
    348 	}
    349 
    350 release:
    351 	if (m) m_freem(m);
    352 	if (ctl) m_freem(ctl);
    353 	return err;
    354 }
    355 
    356 /*
    357  * l2cap_ctloutput(req, socket, sockopt)
    358  *
    359  *	Apply configuration commands to channel. This corresponds to
    360  *	"Reconfigure Channel Request" in the L2CAP specification.
    361  */
    362 int
    363 l2cap_ctloutput(int req, struct socket *so, struct sockopt *sopt)
    364 {
    365 	struct l2cap_channel *pcb = so->so_pcb;
    366 	int err = 0;
    367 
    368 	DPRINTFN(2, "%s\n", prcorequests[req]);
    369 
    370 	if (pcb == NULL)
    371 		return EINVAL;
    372 
    373 	if (sopt->sopt_level != BTPROTO_L2CAP)
    374 		return ENOPROTOOPT;
    375 
    376 	switch(req) {
    377 	case PRCO_GETOPT:
    378 		err = l2cap_getopt(pcb, sopt);
    379 		break;
    380 
    381 	case PRCO_SETOPT:
    382 		err = l2cap_setopt(pcb, sopt);
    383 		break;
    384 
    385 	default:
    386 		err = ENOPROTOOPT;
    387 		break;
    388 	}
    389 
    390 	return err;
    391 }
    392 
    393 /**********************************************************************
    394  *
    395  *	L2CAP Protocol socket callbacks
    396  *
    397  */
    398 
    399 static void
    400 l2cap_connecting(void *arg)
    401 {
    402 	struct socket *so = arg;
    403 
    404 	DPRINTF("Connecting\n");
    405 	soisconnecting(so);
    406 }
    407 
    408 static void
    409 l2cap_connected(void *arg)
    410 {
    411 	struct socket *so = arg;
    412 
    413 	DPRINTF("Connected\n");
    414 	soisconnected(so);
    415 }
    416 
    417 static void
    418 l2cap_disconnected(void *arg, int err)
    419 {
    420 	struct socket *so = arg;
    421 
    422 	DPRINTF("Disconnected (%d)\n", err);
    423 
    424 	so->so_error = err;
    425 	soisdisconnected(so);
    426 }
    427 
    428 static void *
    429 l2cap_newconn(void *arg, struct sockaddr_bt *laddr,
    430     struct sockaddr_bt *raddr)
    431 {
    432 	struct socket *so = arg;
    433 
    434 	DPRINTF("New Connection\n");
    435 	so = sonewconn(so, false);
    436 	if (so == NULL)
    437 		return NULL;
    438 
    439 	soisconnecting(so);
    440 
    441 	return so->so_pcb;
    442 }
    443 
    444 static void
    445 l2cap_complete(void *arg, int count)
    446 {
    447 	struct socket *so = arg;
    448 
    449 	while (count-- > 0)
    450 		sbdroprecord(&so->so_snd);
    451 
    452 	sowwakeup(so);
    453 }
    454 
    455 static void
    456 l2cap_linkmode(void *arg, int new)
    457 {
    458 	struct socket *so = arg;
    459 	struct sockopt sopt;
    460 	int mode;
    461 
    462 	DPRINTF("auth %s, encrypt %s, secure %s\n",
    463 		(new & L2CAP_LM_AUTH ? "on" : "off"),
    464 		(new & L2CAP_LM_ENCRYPT ? "on" : "off"),
    465 		(new & L2CAP_LM_SECURE ? "on" : "off"));
    466 
    467 	sockopt_init(&sopt, BTPROTO_L2CAP, SO_L2CAP_LM, 0);
    468 	(void)l2cap_getopt(so->so_pcb, &sopt);
    469 	(void)sockopt_getint(&sopt, &mode);
    470 	sockopt_destroy(&sopt);
    471 
    472 	if (((mode & L2CAP_LM_AUTH) && !(new & L2CAP_LM_AUTH))
    473 	    || ((mode & L2CAP_LM_ENCRYPT) && !(new & L2CAP_LM_ENCRYPT))
    474 	    || ((mode & L2CAP_LM_SECURE) && !(new & L2CAP_LM_SECURE)))
    475 		l2cap_disconnect(so->so_pcb, 0);
    476 }
    477 
    478 static void
    479 l2cap_input(void *arg, struct mbuf *m)
    480 {
    481 	struct socket *so = arg;
    482 
    483 	if (m->m_pkthdr.len > sbspace(&so->so_rcv)) {
    484 		printf("%s: packet (%d bytes) dropped (socket buffer full)\n",
    485 			__func__, m->m_pkthdr.len);
    486 		m_freem(m);
    487 		return;
    488 	}
    489 
    490 	DPRINTFN(10, "received %d bytes\n", m->m_pkthdr.len);
    491 
    492 	sbappendrecord(&so->so_rcv, m);
    493 	sorwakeup(so);
    494 }
    495 
    496 PR_WRAP_USRREQS(l2cap)
    497 
    498 #define	l2cap_attach		l2cap_attach_wrapper
    499 #define	l2cap_detach		l2cap_detach_wrapper
    500 #define	l2cap_accept		l2cap_accept_wrapper
    501 #define	l2cap_bind		l2cap_bind_wrapper
    502 #define	l2cap_listen		l2cap_listen_wrapper
    503 #define	l2cap_ioctl		l2cap_ioctl_wrapper
    504 #define	l2cap_stat		l2cap_stat_wrapper
    505 #define	l2cap_peeraddr		l2cap_peeraddr_wrapper
    506 #define	l2cap_sockaddr		l2cap_sockaddr_wrapper
    507 #define	l2cap_recvoob		l2cap_recvoob_wrapper
    508 #define	l2cap_sendoob		l2cap_sendoob_wrapper
    509 #define	l2cap_usrreq		l2cap_usrreq_wrapper
    510 
    511 const struct pr_usrreqs l2cap_usrreqs = {
    512 	.pr_attach	= l2cap_attach,
    513 	.pr_detach	= l2cap_detach,
    514 	.pr_accept	= l2cap_accept,
    515 	.pr_bind	= l2cap_bind,
    516 	.pr_listen	= l2cap_listen,
    517 	.pr_ioctl	= l2cap_ioctl,
    518 	.pr_stat	= l2cap_stat,
    519 	.pr_peeraddr	= l2cap_peeraddr,
    520 	.pr_sockaddr	= l2cap_sockaddr,
    521 	.pr_recvoob	= l2cap_recvoob,
    522 	.pr_sendoob	= l2cap_sendoob,
    523 	.pr_generic	= l2cap_usrreq,
    524 };
    525