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