Home | History | Annotate | Line # | Download | only in netbt
sco_socket.c revision 1.28
      1 /*	$NetBSD: sco_socket.c,v 1.28 2014/07/30 10:04:26 rtr Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2006 Itronix Inc.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. The name of Itronix Inc. may not be used to endorse
     16  *    or promote products derived from this software without specific
     17  *    prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
     23  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     26  * ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: sco_socket.c,v 1.28 2014/07/30 10:04:26 rtr Exp $");
     34 
     35 /* load symbolic names */
     36 #ifdef BLUETOOTH_DEBUG
     37 #define PRUREQUESTS
     38 #define PRCOREQUESTS
     39 #endif
     40 
     41 #include <sys/param.h>
     42 #include <sys/domain.h>
     43 #include <sys/kernel.h>
     44 #include <sys/mbuf.h>
     45 #include <sys/proc.h>
     46 #include <sys/protosw.h>
     47 #include <sys/socket.h>
     48 #include <sys/socketvar.h>
     49 #include <sys/systm.h>
     50 
     51 #include <netbt/bluetooth.h>
     52 #include <netbt/hci.h>
     53 #include <netbt/sco.h>
     54 
     55 /*******************************************************************************
     56  *
     57  * SCO SOCK_SEQPACKET sockets - low latency audio data
     58  */
     59 
     60 static void sco_connecting(void *);
     61 static void sco_connected(void *);
     62 static void sco_disconnected(void *, int);
     63 static void *sco_newconn(void *, struct sockaddr_bt *, struct sockaddr_bt *);
     64 static void sco_complete(void *, int);
     65 static void sco_linkmode(void *, int);
     66 static void sco_input(void *, struct mbuf *);
     67 
     68 static const struct btproto sco_proto = {
     69 	sco_connecting,
     70 	sco_connected,
     71 	sco_disconnected,
     72 	sco_newconn,
     73 	sco_complete,
     74 	sco_linkmode,
     75 	sco_input,
     76 };
     77 
     78 int sco_sendspace = 4096;
     79 int sco_recvspace = 4096;
     80 
     81 static int
     82 sco_attach(struct socket *so, int proto)
     83 {
     84 	int error;
     85 
     86 	KASSERT(so->so_pcb == NULL);
     87 
     88 	if (so->so_lock == NULL) {
     89 		mutex_obj_hold(bt_lock);
     90 		so->so_lock = bt_lock;
     91 		solock(so);
     92 	}
     93 	KASSERT(solocked(so));
     94 
     95 	error = soreserve(so, sco_sendspace, sco_recvspace);
     96 	if (error) {
     97 		return error;
     98 	}
     99 	return sco_attach_pcb((struct sco_pcb **)&so->so_pcb, &sco_proto, so);
    100 }
    101 
    102 static void
    103 sco_detach(struct socket *so)
    104 {
    105 	KASSERT(so->so_pcb != NULL);
    106 	sco_detach_pcb((struct sco_pcb **)&so->so_pcb);
    107 	KASSERT(so->so_pcb == NULL);
    108 }
    109 
    110 static int
    111 sco_accept(struct socket *so, struct mbuf *nam)
    112 {
    113 	struct sco_pcb *pcb = so->so_pcb;
    114 	struct sockaddr_bt *sa;
    115 
    116 	KASSERT(solocked(so));
    117 	KASSERT(nam != NULL);
    118 
    119 	if (pcb == NULL)
    120 		return EINVAL;
    121 
    122 	sa = mtod(nam, struct sockaddr_bt *);
    123 	nam->m_len = sizeof(struct sockaddr_bt);
    124 	return sco_peeraddr_pcb(pcb, sa);
    125 }
    126 
    127 static int
    128 sco_bind(struct socket *so, struct mbuf *nam)
    129 {
    130 	struct sco_pcb *pcb = so->so_pcb;
    131 	struct sockaddr_bt *sa;
    132 
    133 	KASSERT(solocked(so));
    134 	KASSERT(nam != NULL);
    135 
    136 	if (pcb == NULL)
    137 		return EINVAL;
    138 
    139 	sa = mtod(nam, struct sockaddr_bt *);
    140 	if (sa->bt_len != sizeof(struct sockaddr_bt))
    141 		return EINVAL;
    142 
    143 	if (sa->bt_family != AF_BLUETOOTH)
    144 		return EAFNOSUPPORT;
    145 
    146 	return sco_bind_pcb(pcb, sa);
    147 }
    148 
    149 static int
    150 sco_listen(struct socket *so)
    151 {
    152 	struct sco_pcb *pcb = so->so_pcb;
    153 
    154 	KASSERT(solocked(so));
    155 
    156 	if (pcb == NULL)
    157 		return EINVAL;
    158 
    159 	return sco_listen_pcb(pcb);
    160 }
    161 
    162 static int
    163 sco_connect(struct socket *so, struct mbuf *nam)
    164 {
    165 	struct sco_pcb *pcb = so->so_pcb;
    166 	struct sockaddr_bt *sa;
    167 
    168 	KASSERT(solocked(so));
    169 	KASSERT(nam != NULL);
    170 
    171 	if (pcb == NULL)
    172 		return EINVAL;
    173 
    174 	sa = mtod(nam, struct sockaddr_bt *);
    175 	if (sa->bt_len != sizeof(struct sockaddr_bt))
    176 		return EINVAL;
    177 
    178 	if (sa->bt_family != AF_BLUETOOTH)
    179 		return EAFNOSUPPORT;
    180 
    181 	soisconnecting(so);
    182 	return sco_connect_pcb(pcb, sa);
    183 }
    184 
    185 static int
    186 sco_ioctl(struct socket *so, u_long cmd, void *nam, struct ifnet *ifp)
    187 {
    188 	return EOPNOTSUPP;
    189 }
    190 
    191 static int
    192 sco_stat(struct socket *so, struct stat *ub)
    193 {
    194 	KASSERT(solocked(so));
    195 
    196 	return 0;
    197 }
    198 
    199 static int
    200 sco_peeraddr(struct socket *so, struct mbuf *nam)
    201 {
    202 	struct sco_pcb *pcb = (struct sco_pcb *)so->so_pcb;
    203 	struct sockaddr_bt *sa;
    204 
    205 	KASSERT(solocked(so));
    206 	KASSERT(pcb != NULL);
    207 	KASSERT(nam != NULL);
    208 
    209 	sa = mtod(nam, struct sockaddr_bt *);
    210 	nam->m_len = sizeof(struct sockaddr_bt);
    211 	return sco_peeraddr_pcb(pcb, sa);
    212 }
    213 
    214 static int
    215 sco_sockaddr(struct socket *so, struct mbuf *nam)
    216 {
    217 	struct sco_pcb *pcb = (struct sco_pcb *)so->so_pcb;
    218 	struct sockaddr_bt *sa;
    219 
    220 	KASSERT(solocked(so));
    221 	KASSERT(pcb != NULL);
    222 	KASSERT(nam != NULL);
    223 
    224 	sa = mtod(nam, struct sockaddr_bt *);
    225 	nam->m_len = sizeof(struct sockaddr_bt);
    226 	return sco_sockaddr_pcb(pcb, sa);
    227 }
    228 
    229 static int
    230 sco_recvoob(struct socket *so, struct mbuf *m, int flags)
    231 {
    232 	KASSERT(solocked(so));
    233 
    234 	return EOPNOTSUPP;
    235 }
    236 
    237 static int
    238 sco_sendoob(struct socket *so, struct mbuf *m, struct mbuf *control)
    239 {
    240 	KASSERT(solocked(so));
    241 
    242 	if (m)
    243 		m_freem(m);
    244 	if (control)
    245 		m_freem(control);
    246 
    247 	return EOPNOTSUPP;
    248 }
    249 
    250 /*
    251  * User Request.
    252  * up is socket
    253  * m is optional mbuf chain containing message
    254  * nam is optional mbuf chain containing an address
    255  * ctl is optional mbuf chain containing socket options
    256  * l is pointer to process requesting action (if any)
    257  *
    258  * we are responsible for disposing of m and ctl if
    259  * they are mbuf chains
    260  */
    261 static int
    262 sco_usrreq(struct socket *up, int req, struct mbuf *m,
    263     struct mbuf *nam, struct mbuf *ctl, struct lwp *l)
    264 {
    265 	struct sco_pcb *pcb = (struct sco_pcb *)up->so_pcb;
    266 	struct mbuf *m0;
    267 	int err = 0;
    268 
    269 	DPRINTFN(2, "%s\n", prurequests[req]);
    270 	KASSERT(req != PRU_ATTACH);
    271 	KASSERT(req != PRU_DETACH);
    272 	KASSERT(req != PRU_ACCEPT);
    273 	KASSERT(req != PRU_BIND);
    274 	KASSERT(req != PRU_LISTEN);
    275 	KASSERT(req != PRU_CONNECT);
    276 	KASSERT(req != PRU_CONTROL);
    277 	KASSERT(req != PRU_SENSE);
    278 	KASSERT(req != PRU_PEERADDR);
    279 	KASSERT(req != PRU_SOCKADDR);
    280 	KASSERT(req != PRU_RCVOOB);
    281 	KASSERT(req != PRU_SENDOOB);
    282 
    283 	switch(req) {
    284 	case PRU_PURGEIF:
    285 		return EOPNOTSUPP;
    286 	}
    287 
    288 	/* anything after here *requires* a pcb */
    289 	if (pcb == NULL) {
    290 		err = EINVAL;
    291 		goto release;
    292 	}
    293 
    294 	switch(req) {
    295 	case PRU_DISCONNECT:
    296 		soisdisconnecting(up);
    297 		return sco_disconnect(pcb, up->so_linger);
    298 
    299 	case PRU_ABORT:
    300 		sco_disconnect(pcb, 0);
    301 		soisdisconnected(up);
    302 		sco_detach(up);
    303 		return 0;
    304 
    305 	case PRU_SHUTDOWN:
    306 		socantsendmore(up);
    307 		break;
    308 
    309 	case PRU_SEND:
    310 		KASSERT(m != NULL);
    311 		if (m->m_pkthdr.len == 0)
    312 			break;
    313 
    314 		if (m->m_pkthdr.len > pcb->sp_mtu) {
    315 			err = EMSGSIZE;
    316 			break;
    317 		}
    318 
    319 		m0 = m_copypacket(m, M_DONTWAIT);
    320 		if (m0 == NULL) {
    321 			err = ENOMEM;
    322 			break;
    323 		}
    324 
    325 		if (ctl) /* no use for that */
    326 			m_freem(ctl);
    327 
    328 		sbappendrecord(&up->so_snd, m);
    329 		return sco_send(pcb, m0);
    330 
    331 	case PRU_RCVD:
    332 		return EOPNOTSUPP;	/* (no release) */
    333 
    334 	case PRU_CONNECT2:
    335 	case PRU_FASTTIMO:
    336 	case PRU_SLOWTIMO:
    337 	case PRU_PROTORCV:
    338 	case PRU_PROTOSEND:
    339 		err = EOPNOTSUPP;
    340 		break;
    341 
    342 	default:
    343 		UNKNOWN(req);
    344 		err = EOPNOTSUPP;
    345 		break;
    346 	}
    347 
    348 release:
    349 	if (m) m_freem(m);
    350 	if (ctl) m_freem(ctl);
    351 	return err;
    352 }
    353 
    354 /*
    355  * get/set socket options
    356  */
    357 int
    358 sco_ctloutput(int req, struct socket *so, struct sockopt *sopt)
    359 {
    360 	struct sco_pcb *pcb = (struct sco_pcb *)so->so_pcb;
    361 	int err = 0;
    362 
    363 	DPRINTFN(2, "req %s\n", prcorequests[req]);
    364 
    365 	if (pcb == NULL)
    366 		return EINVAL;
    367 
    368 	if (sopt->sopt_level != BTPROTO_SCO)
    369 		return ENOPROTOOPT;
    370 
    371 	switch(req) {
    372 	case PRCO_GETOPT:
    373 		err = sco_getopt(pcb, sopt);
    374 		break;
    375 
    376 	case PRCO_SETOPT:
    377 		err = sco_setopt(pcb, sopt);
    378 		break;
    379 
    380 	default:
    381 		err = ENOPROTOOPT;
    382 		break;
    383 	}
    384 
    385 	return err;
    386 }
    387 
    388 /*****************************************************************************
    389  *
    390  *	SCO Protocol socket callbacks
    391  *
    392  */
    393 static void
    394 sco_connecting(void *arg)
    395 {
    396 	struct socket *so = arg;
    397 
    398 	DPRINTF("Connecting\n");
    399 	soisconnecting(so);
    400 }
    401 
    402 static void
    403 sco_connected(void *arg)
    404 {
    405 	struct socket *so = arg;
    406 
    407 	DPRINTF("Connected\n");
    408 	soisconnected(so);
    409 }
    410 
    411 static void
    412 sco_disconnected(void *arg, int err)
    413 {
    414 	struct socket *so = arg;
    415 
    416 	DPRINTF("Disconnected (%d)\n", err);
    417 
    418 	so->so_error = err;
    419 	soisdisconnected(so);
    420 }
    421 
    422 static void *
    423 sco_newconn(void *arg, struct sockaddr_bt *laddr,
    424     struct sockaddr_bt *raddr)
    425 {
    426 	struct socket *so = arg;
    427 
    428 	DPRINTF("New Connection\n");
    429 	so = sonewconn(so, false);
    430 	if (so == NULL)
    431 		return NULL;
    432 
    433 	soisconnecting(so);
    434 	return so->so_pcb;
    435 }
    436 
    437 static void
    438 sco_complete(void *arg, int num)
    439 {
    440 	struct socket *so = arg;
    441 
    442 	while (num-- > 0)
    443 		sbdroprecord(&so->so_snd);
    444 
    445 	sowwakeup(so);
    446 }
    447 
    448 static void
    449 sco_linkmode(void *arg, int mode)
    450 {
    451 }
    452 
    453 static void
    454 sco_input(void *arg, struct mbuf *m)
    455 {
    456 	struct socket *so = arg;
    457 
    458 	/*
    459 	 * since this data is time sensitive, if the buffer
    460 	 * is full we just dump data until the latest one
    461 	 * will fit.
    462 	 */
    463 
    464 	while (m->m_pkthdr.len > sbspace(&so->so_rcv))
    465 		sbdroprecord(&so->so_rcv);
    466 
    467 	DPRINTFN(10, "received %d bytes\n", m->m_pkthdr.len);
    468 
    469 	sbappendrecord(&so->so_rcv, m);
    470 	sorwakeup(so);
    471 }
    472 
    473 PR_WRAP_USRREQS(sco)
    474 
    475 #define	sco_attach		sco_attach_wrapper
    476 #define	sco_detach		sco_detach_wrapper
    477 #define	sco_accept		sco_accept_wrapper
    478 #define	sco_bind		sco_bind_wrapper
    479 #define	sco_listen		sco_listen_wrapper
    480 #define	sco_connect		sco_connect_wrapper
    481 #define	sco_ioctl		sco_ioctl_wrapper
    482 #define	sco_stat		sco_stat_wrapper
    483 #define	sco_peeraddr		sco_peeraddr_wrapper
    484 #define	sco_sockaddr		sco_sockaddr_wrapper
    485 #define	sco_recvoob		sco_recvoob_wrapper
    486 #define	sco_sendoob		sco_sendoob_wrapper
    487 #define	sco_usrreq		sco_usrreq_wrapper
    488 
    489 const struct pr_usrreqs sco_usrreqs = {
    490 	.pr_attach	= sco_attach,
    491 	.pr_detach	= sco_detach,
    492 	.pr_accept	= sco_accept,
    493 	.pr_bind	= sco_bind,
    494 	.pr_listen	= sco_listen,
    495 	.pr_connect	= sco_connect,
    496 	.pr_ioctl	= sco_ioctl,
    497 	.pr_stat	= sco_stat,
    498 	.pr_peeraddr	= sco_peeraddr,
    499 	.pr_sockaddr	= sco_sockaddr,
    500 	.pr_recvoob	= sco_recvoob,
    501 	.pr_sendoob	= sco_sendoob,
    502 	.pr_generic	= sco_usrreq,
    503 };
    504