Home | History | Annotate | Line # | Download | only in netbt
rfcomm_socket.c revision 1.21
      1 /*	$NetBSD: rfcomm_socket.c,v 1.21 2014/07/07 15:13:21 rtr Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2006 Itronix Inc.
      5  * All rights reserved.
      6  *
      7  * Written by Iain Hibbert for Itronix Inc.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. The name of Itronix Inc. may not be used to endorse
     18  *    or promote products derived from this software without specific
     19  *    prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
     25  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     26  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     28  * ON ANY THEORY OF LIABILITY, WHETHER IN
     29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31  * POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 __KERNEL_RCSID(0, "$NetBSD: rfcomm_socket.c,v 1.21 2014/07/07 15:13:21 rtr Exp $");
     36 
     37 /* load symbolic names */
     38 #ifdef BLUETOOTH_DEBUG
     39 #define PRUREQUESTS
     40 #define PRCOREQUESTS
     41 #endif
     42 
     43 #include <sys/param.h>
     44 #include <sys/domain.h>
     45 #include <sys/kernel.h>
     46 #include <sys/mbuf.h>
     47 #include <sys/proc.h>
     48 #include <sys/protosw.h>
     49 #include <sys/socket.h>
     50 #include <sys/socketvar.h>
     51 #include <sys/systm.h>
     52 
     53 #include <netbt/bluetooth.h>
     54 #include <netbt/rfcomm.h>
     55 
     56 /****************************************************************************
     57  *
     58  *	RFCOMM SOCK_STREAM Sockets - serial line emulation
     59  *
     60  */
     61 
     62 static void rfcomm_connecting(void *);
     63 static void rfcomm_connected(void *);
     64 static void rfcomm_disconnected(void *, int);
     65 static void *rfcomm_newconn(void *, struct sockaddr_bt *, struct sockaddr_bt *);
     66 static void rfcomm_complete(void *, int);
     67 static void rfcomm_linkmode(void *, int);
     68 static void rfcomm_input(void *, struct mbuf *);
     69 
     70 static const struct btproto rfcomm_proto = {
     71 	rfcomm_connecting,
     72 	rfcomm_connected,
     73 	rfcomm_disconnected,
     74 	rfcomm_newconn,
     75 	rfcomm_complete,
     76 	rfcomm_linkmode,
     77 	rfcomm_input,
     78 };
     79 
     80 /* sysctl variables */
     81 int rfcomm_sendspace = 4096;
     82 int rfcomm_recvspace = 4096;
     83 
     84 static int
     85 rfcomm_attach(struct socket *so, int proto)
     86 {
     87 	int error;
     88 
     89 	KASSERT(so->so_pcb == NULL);
     90 
     91 	if (so->so_lock == NULL) {
     92 		mutex_obj_hold(bt_lock);
     93 		so->so_lock = bt_lock;
     94 		solock(so);
     95 	}
     96 	KASSERT(solocked(so));
     97 
     98 	/*
     99 	 * Since we have nothing to add, we attach the DLC
    100 	 * structure directly to our PCB pointer.
    101 	 */
    102 	error = soreserve(so, rfcomm_sendspace, rfcomm_recvspace);
    103 	if (error)
    104 		return error;
    105 
    106 	error = rfcomm_attach_pcb((struct rfcomm_dlc **)&so->so_pcb,
    107 				&rfcomm_proto, so);
    108 	if (error)
    109 		return error;
    110 
    111 	error = rfcomm_rcvd(so->so_pcb, sbspace(&so->so_rcv));
    112 	if (error) {
    113 		rfcomm_detach_pcb((struct rfcomm_dlc **)&so->so_pcb);
    114 		return error;
    115 	}
    116 	return 0;
    117 }
    118 
    119 static void
    120 rfcomm_detach(struct socket *so)
    121 {
    122 	KASSERT(so->so_pcb != NULL);
    123 	rfcomm_detach_pcb((struct rfcomm_dlc **)&so->so_pcb);
    124 	KASSERT(so->so_pcb == NULL);
    125 }
    126 
    127 static int
    128 rfcomm_ioctl(struct socket *up, u_long cmd, void *nam, struct ifnet *ifp)
    129 {
    130 	return EPASSTHROUGH;
    131 }
    132 
    133 static int
    134 rfcomm_stat(struct socket *so, struct stat *ub)
    135 {
    136 	return 0;
    137 }
    138 
    139 /*
    140  * User Request.
    141  * up is socket
    142  * m is optional mbuf chain containing message
    143  * nam is either
    144  *	optional mbuf chain containing an address
    145  *	message flags (PRU_RCVD)
    146  * ctl is either
    147  *	optional mbuf chain containing socket options
    148  *	optional interface pointer PRU_PURGEIF
    149  * l is pointer to process requesting action (if any)
    150  *
    151  * we are responsible for disposing of m and ctl if
    152  * they are mbuf chains
    153  */
    154 static int
    155 rfcomm_usrreq(struct socket *up, int req, struct mbuf *m,
    156 		struct mbuf *nam, struct mbuf *ctl, struct lwp *l)
    157 {
    158 	struct rfcomm_dlc *pcb = up->so_pcb;
    159 	struct sockaddr_bt *sa;
    160 	struct mbuf *m0;
    161 	int err = 0;
    162 
    163 	DPRINTFN(2, "%s\n", prurequests[req]);
    164 	KASSERT(req != PRU_ATTACH);
    165 	KASSERT(req != PRU_DETACH);
    166 	KASSERT(req != PRU_CONTROL);
    167 	KASSERT(req != PRU_SENSE);
    168 
    169 	switch (req) {
    170 	case PRU_PURGEIF:
    171 		return EOPNOTSUPP;
    172 	}
    173 	if (pcb == NULL) {
    174 		err = EINVAL;
    175 		goto release;
    176 	}
    177 
    178 	switch(req) {
    179 	case PRU_DISCONNECT:
    180 		soisdisconnecting(up);
    181 		return rfcomm_disconnect(pcb, up->so_linger);
    182 
    183 	case PRU_ABORT:
    184 		rfcomm_disconnect(pcb, 0);
    185 		soisdisconnected(up);
    186 		rfcomm_detach(up);
    187 		return 0;
    188 
    189 	case PRU_BIND:
    190 		KASSERT(nam != NULL);
    191 		sa = mtod(nam, struct sockaddr_bt *);
    192 
    193 		if (sa->bt_len != sizeof(struct sockaddr_bt))
    194 			return EINVAL;
    195 
    196 		if (sa->bt_family != AF_BLUETOOTH)
    197 			return EAFNOSUPPORT;
    198 
    199 		return rfcomm_bind(pcb, sa);
    200 
    201 	case PRU_CONNECT:
    202 		KASSERT(nam != NULL);
    203 		sa = mtod(nam, struct sockaddr_bt *);
    204 
    205 		if (sa->bt_len != sizeof(struct sockaddr_bt))
    206 			return EINVAL;
    207 
    208 		if (sa->bt_family != AF_BLUETOOTH)
    209 			return EAFNOSUPPORT;
    210 
    211 		soisconnecting(up);
    212 		return rfcomm_connect(pcb, sa);
    213 
    214 	case PRU_PEERADDR:
    215 		KASSERT(nam != NULL);
    216 		sa = mtod(nam, struct sockaddr_bt *);
    217 		nam->m_len = sizeof(struct sockaddr_bt);
    218 		return rfcomm_peeraddr(pcb, sa);
    219 
    220 	case PRU_SOCKADDR:
    221 		KASSERT(nam != NULL);
    222 		sa = mtod(nam, struct sockaddr_bt *);
    223 		nam->m_len = sizeof(struct sockaddr_bt);
    224 		return rfcomm_sockaddr(pcb, sa);
    225 
    226 	case PRU_SHUTDOWN:
    227 		socantsendmore(up);
    228 		break;
    229 
    230 	case PRU_SEND:
    231 		KASSERT(m != NULL);
    232 
    233 		if (ctl)	/* no use for that */
    234 			m_freem(ctl);
    235 
    236 		m0 = m_copypacket(m, M_DONTWAIT);
    237 		if (m0 == NULL)
    238 			return ENOMEM;
    239 
    240 		sbappendstream(&up->so_snd, m);
    241 
    242 		return rfcomm_send(pcb, m0);
    243 
    244 	case PRU_RCVD:
    245 		return rfcomm_rcvd(pcb, sbspace(&up->so_rcv));
    246 
    247 	case PRU_RCVOOB:
    248 		return EOPNOTSUPP;	/* (no release) */
    249 
    250 	case PRU_LISTEN:
    251 		return rfcomm_listen(pcb);
    252 
    253 	case PRU_ACCEPT:
    254 		KASSERT(nam != NULL);
    255 		sa = mtod(nam, struct sockaddr_bt *);
    256 		nam->m_len = sizeof(struct sockaddr_bt);
    257 		return rfcomm_peeraddr(pcb, sa);
    258 
    259 	case PRU_CONNECT2:
    260 	case PRU_SENDOOB:
    261 	case PRU_FASTTIMO:
    262 	case PRU_SLOWTIMO:
    263 	case PRU_PROTORCV:
    264 	case PRU_PROTOSEND:
    265 		err = EOPNOTSUPP;
    266 		break;
    267 
    268 	default:
    269 		UNKNOWN(req);
    270 		err = EOPNOTSUPP;
    271 		break;
    272 	}
    273 
    274 release:
    275 	if (m) m_freem(m);
    276 	if (ctl) m_freem(ctl);
    277 	return err;
    278 }
    279 
    280 /*
    281  * rfcomm_ctloutput(req, socket, sockopt)
    282  *
    283  */
    284 int
    285 rfcomm_ctloutput(int req, struct socket *so, struct sockopt *sopt)
    286 {
    287 	struct rfcomm_dlc *pcb = so->so_pcb;
    288 	int err = 0;
    289 
    290 	DPRINTFN(2, "%s\n", prcorequests[req]);
    291 
    292 	if (pcb == NULL)
    293 		return EINVAL;
    294 
    295 	if (sopt->sopt_level != BTPROTO_RFCOMM)
    296 		return ENOPROTOOPT;
    297 
    298 	switch(req) {
    299 	case PRCO_GETOPT:
    300 		err = rfcomm_getopt(pcb, sopt);
    301 		break;
    302 
    303 	case PRCO_SETOPT:
    304 		err = rfcomm_setopt(pcb, sopt);
    305 		break;
    306 
    307 	default:
    308 		err = ENOPROTOOPT;
    309 		break;
    310 	}
    311 
    312 	return err;
    313 }
    314 
    315 /**********************************************************************
    316  *
    317  * RFCOMM callbacks
    318  */
    319 
    320 static void
    321 rfcomm_connecting(void *arg)
    322 {
    323 	/* struct socket *so = arg; */
    324 
    325 	KASSERT(arg != NULL);
    326 	DPRINTF("Connecting\n");
    327 }
    328 
    329 static void
    330 rfcomm_connected(void *arg)
    331 {
    332 	struct socket *so = arg;
    333 
    334 	KASSERT(so != NULL);
    335 	DPRINTF("Connected\n");
    336 	soisconnected(so);
    337 }
    338 
    339 static void
    340 rfcomm_disconnected(void *arg, int err)
    341 {
    342 	struct socket *so = arg;
    343 
    344 	KASSERT(so != NULL);
    345 	DPRINTF("Disconnected\n");
    346 
    347 	so->so_error = err;
    348 	soisdisconnected(so);
    349 }
    350 
    351 static void *
    352 rfcomm_newconn(void *arg, struct sockaddr_bt *laddr,
    353     struct sockaddr_bt *raddr)
    354 {
    355 	struct socket *so = arg;
    356 
    357 	DPRINTF("New Connection\n");
    358 	so = sonewconn(so, false);
    359 	if (so == NULL)
    360 		return NULL;
    361 
    362 	soisconnecting(so);
    363 
    364 	return so->so_pcb;
    365 }
    366 
    367 /*
    368  * rfcomm_complete(rfcomm_dlc, length)
    369  *
    370  * length bytes are sent and may be removed from socket buffer
    371  */
    372 static void
    373 rfcomm_complete(void *arg, int length)
    374 {
    375 	struct socket *so = arg;
    376 
    377 	sbdrop(&so->so_snd, length);
    378 	sowwakeup(so);
    379 }
    380 
    381 /*
    382  * rfcomm_linkmode(rfcomm_dlc, new)
    383  *
    384  * link mode change notification.
    385  */
    386 static void
    387 rfcomm_linkmode(void *arg, int new)
    388 {
    389 	struct socket *so = arg;
    390 	struct sockopt sopt;
    391 	int mode;
    392 
    393 	DPRINTF("auth %s, encrypt %s, secure %s\n",
    394 		(new & RFCOMM_LM_AUTH ? "on" : "off"),
    395 		(new & RFCOMM_LM_ENCRYPT ? "on" : "off"),
    396 		(new & RFCOMM_LM_SECURE ? "on" : "off"));
    397 
    398 	sockopt_init(&sopt, BTPROTO_RFCOMM, SO_RFCOMM_LM, 0);
    399 	(void)rfcomm_getopt(so->so_pcb, &sopt);
    400 	(void)sockopt_getint(&sopt, &mode);
    401 	sockopt_destroy(&sopt);
    402 
    403 	if (((mode & RFCOMM_LM_AUTH) && !(new & RFCOMM_LM_AUTH))
    404 	    || ((mode & RFCOMM_LM_ENCRYPT) && !(new & RFCOMM_LM_ENCRYPT))
    405 	    || ((mode & RFCOMM_LM_SECURE) && !(new & RFCOMM_LM_SECURE)))
    406 		rfcomm_disconnect(so->so_pcb, 0);
    407 }
    408 
    409 /*
    410  * rfcomm_input(rfcomm_dlc, mbuf)
    411  */
    412 static void
    413 rfcomm_input(void *arg, struct mbuf *m)
    414 {
    415 	struct socket *so = arg;
    416 
    417 	KASSERT(so != NULL);
    418 
    419 	if (m->m_pkthdr.len > sbspace(&so->so_rcv)) {
    420 		printf("%s: %d bytes dropped (socket buffer full)\n",
    421 			__func__, m->m_pkthdr.len);
    422 		m_freem(m);
    423 		return;
    424 	}
    425 
    426 	DPRINTFN(10, "received %d bytes\n", m->m_pkthdr.len);
    427 
    428 	sbappendstream(&so->so_rcv, m);
    429 	sorwakeup(so);
    430 }
    431 
    432 PR_WRAP_USRREQS(rfcomm)
    433 
    434 #define	rfcomm_attach		rfcomm_attach_wrapper
    435 #define	rfcomm_detach		rfcomm_detach_wrapper
    436 #define	rfcomm_ioctl		rfcomm_ioctl_wrapper
    437 #define	rfcomm_stat		rfcomm_stat_wrapper
    438 #define	rfcomm_usrreq		rfcomm_usrreq_wrapper
    439 
    440 const struct pr_usrreqs rfcomm_usrreqs = {
    441 	.pr_attach	= rfcomm_attach,
    442 	.pr_detach	= rfcomm_detach,
    443 	.pr_ioctl	= rfcomm_ioctl,
    444 	.pr_stat	= rfcomm_stat,
    445 	.pr_generic	= rfcomm_usrreq,
    446 };
    447