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