Home | History | Annotate | Line # | Download | only in netbt
rfcomm_socket.c revision 1.36
      1 /*	$NetBSD: rfcomm_socket.c,v 1.36 2015/04/26 21:40:49 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.36 2015/04/26 21:40:49 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_pcb(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_accept(struct socket *so, struct sockaddr *nam)
    129 {
    130 	struct rfcomm_dlc *pcb = so->so_pcb;
    131 
    132 	KASSERT(solocked(so));
    133 	KASSERT(nam != NULL);
    134 
    135 	if (pcb == NULL)
    136 		return EINVAL;
    137 
    138 	return rfcomm_peeraddr_pcb(pcb, (struct sockaddr_bt *)nam);
    139 }
    140 
    141 static int
    142 rfcomm_bind(struct socket *so, struct sockaddr *nam, struct lwp *l)
    143 {
    144 	struct rfcomm_dlc *pcb = so->so_pcb;
    145 	struct sockaddr_bt *sa = (struct sockaddr_bt *)nam;
    146 
    147 	KASSERT(solocked(so));
    148 	KASSERT(nam != NULL);
    149 
    150 	if (pcb == NULL)
    151 		return EINVAL;
    152 
    153 	if (sa->bt_len != sizeof(struct sockaddr_bt))
    154 		return EINVAL;
    155 
    156 	if (sa->bt_family != AF_BLUETOOTH)
    157 		return EAFNOSUPPORT;
    158 
    159 	return rfcomm_bind_pcb(pcb, sa);
    160 }
    161 
    162 static int
    163 rfcomm_listen(struct socket *so, struct lwp *l)
    164 {
    165 	struct rfcomm_dlc *pcb = so->so_pcb;
    166 
    167 	KASSERT(solocked(so));
    168 
    169 	if (pcb == NULL)
    170 		return EINVAL;
    171 
    172 	return rfcomm_listen_pcb(pcb);
    173 }
    174 
    175 static int
    176 rfcomm_connect(struct socket *so, struct mbuf *nam, struct lwp *l)
    177 {
    178 	struct rfcomm_dlc *pcb = so->so_pcb;
    179 	struct sockaddr_bt *sa;
    180 
    181 	KASSERT(solocked(so));
    182 	KASSERT(nam != NULL);
    183 
    184 	if (pcb == NULL)
    185 		return EINVAL;
    186 
    187 	sa = mtod(nam, struct sockaddr_bt *);
    188 	if (sa->bt_len != sizeof(struct sockaddr_bt))
    189 		return EINVAL;
    190 
    191 	if (sa->bt_family != AF_BLUETOOTH)
    192 		return EAFNOSUPPORT;
    193 
    194 	soisconnecting(so);
    195 	return rfcomm_connect_pcb(pcb, sa);
    196 }
    197 
    198 static int
    199 rfcomm_connect2(struct socket *so, struct socket *so2)
    200 {
    201 	struct rfcomm_dlc *pcb = so->so_pcb;
    202 
    203 	KASSERT(solocked(so));
    204 
    205 	if (pcb == NULL)
    206 		return EINVAL;
    207 
    208 	return EOPNOTSUPP;
    209 }
    210 
    211 static int
    212 rfcomm_disconnect(struct socket *so)
    213 {
    214 	struct rfcomm_dlc *pcb = so->so_pcb;
    215 
    216 	KASSERT(solocked(so));
    217 
    218 	if (pcb == NULL)
    219 		return EINVAL;
    220 
    221 	soisdisconnecting(so);
    222 	return rfcomm_disconnect_pcb(pcb, so->so_linger);
    223 }
    224 
    225 static int
    226 rfcomm_shutdown(struct socket *so)
    227 {
    228 	KASSERT(solocked(so));
    229 
    230 	socantsendmore(so);
    231 	return 0;
    232 }
    233 
    234 static int
    235 rfcomm_abort(struct socket *so)
    236 {
    237 	struct rfcomm_dlc *pcb = so->so_pcb;
    238 
    239 	KASSERT(solocked(so));
    240 
    241 	if (pcb == NULL)
    242 		return EINVAL;
    243 
    244 	rfcomm_disconnect_pcb(pcb, 0);
    245 	soisdisconnected(so);
    246 	rfcomm_detach(so);
    247 	return 0;
    248 }
    249 
    250 static int
    251 rfcomm_ioctl(struct socket *so, u_long cmd, void *nam, struct ifnet *ifp)
    252 {
    253 	return EPASSTHROUGH;
    254 }
    255 
    256 static int
    257 rfcomm_stat(struct socket *so, struct stat *ub)
    258 {
    259 	KASSERT(solocked(so));
    260 
    261 	return 0;
    262 }
    263 
    264 static int
    265 rfcomm_peeraddr(struct socket *so, struct sockaddr *nam)
    266 {
    267 	struct rfcomm_dlc *pcb = so->so_pcb;
    268 
    269 	KASSERT(solocked(so));
    270 	KASSERT(pcb != NULL);
    271 	KASSERT(nam != NULL);
    272 
    273 	return rfcomm_peeraddr_pcb(pcb, (struct sockaddr_bt *)nam);
    274 }
    275 
    276 static int
    277 rfcomm_sockaddr(struct socket *so, struct sockaddr *nam)
    278 {
    279 	struct rfcomm_dlc *pcb = so->so_pcb;
    280 
    281 	KASSERT(solocked(so));
    282 	KASSERT(pcb != NULL);
    283 	KASSERT(nam != NULL);
    284 
    285 	return rfcomm_sockaddr_pcb(pcb, (struct sockaddr_bt *)nam);
    286 }
    287 
    288 static int
    289 rfcomm_rcvd(struct socket *so, int flags, struct lwp *l)
    290 {
    291 	struct rfcomm_dlc *pcb = so->so_pcb;
    292 
    293 	KASSERT(solocked(so));
    294 
    295 	if (pcb == NULL)
    296 		return EINVAL;
    297 
    298 	return rfcomm_rcvd_pcb(pcb, sbspace(&so->so_rcv));
    299 }
    300 
    301 static int
    302 rfcomm_recvoob(struct socket *so, struct mbuf *m, int flags)
    303 {
    304 	KASSERT(solocked(so));
    305 
    306 	return EOPNOTSUPP;
    307 }
    308 
    309 static int
    310 rfcomm_send(struct socket *so, struct mbuf *m, struct mbuf *nam,
    311     struct mbuf *control, struct lwp *l)
    312 {
    313 	struct rfcomm_dlc *pcb = so->so_pcb;
    314 	int err = 0;
    315 	struct mbuf *m0;
    316 
    317 	KASSERT(solocked(so));
    318 	KASSERT(m != NULL);
    319 
    320 	if (control)	/* no use for that */
    321 		m_freem(control);
    322 
    323 	if (pcb == NULL) {
    324 		err = EINVAL;
    325 		goto release;
    326 	}
    327 
    328 	m0 = m_copypacket(m, M_DONTWAIT);
    329 	if (m0 == NULL) {
    330 		err = ENOMEM;
    331 		goto release;
    332 	}
    333 
    334 	sbappendstream(&so->so_snd, m);
    335 	return rfcomm_send_pcb(pcb, m0);
    336 
    337 release:
    338 	m_freem(m);
    339 	return err;
    340 }
    341 
    342 static int
    343 rfcomm_sendoob(struct socket *so, struct mbuf *m, struct mbuf *control)
    344 {
    345 	KASSERT(solocked(so));
    346 
    347 	if (m)
    348 		m_freem(m);
    349 	if (control)
    350 		m_freem(control);
    351 
    352 	return EOPNOTSUPP;
    353 }
    354 
    355 static int
    356 rfcomm_purgeif(struct socket *so, struct ifnet *ifp)
    357 {
    358 
    359 	return EOPNOTSUPP;
    360 }
    361 
    362 /*
    363  * rfcomm_ctloutput(req, socket, sockopt)
    364  *
    365  */
    366 int
    367 rfcomm_ctloutput(int req, struct socket *so, struct sockopt *sopt)
    368 {
    369 	struct rfcomm_dlc *pcb = so->so_pcb;
    370 	int err = 0;
    371 
    372 	DPRINTFN(2, "%s\n", prcorequests[req]);
    373 
    374 	if (pcb == NULL)
    375 		return EINVAL;
    376 
    377 	if (sopt->sopt_level != BTPROTO_RFCOMM)
    378 		return ENOPROTOOPT;
    379 
    380 	switch(req) {
    381 	case PRCO_GETOPT:
    382 		err = rfcomm_getopt(pcb, sopt);
    383 		break;
    384 
    385 	case PRCO_SETOPT:
    386 		err = rfcomm_setopt(pcb, sopt);
    387 		break;
    388 
    389 	default:
    390 		err = ENOPROTOOPT;
    391 		break;
    392 	}
    393 
    394 	return err;
    395 }
    396 
    397 /**********************************************************************
    398  *
    399  * RFCOMM callbacks
    400  */
    401 
    402 static void
    403 rfcomm_connecting(void *arg)
    404 {
    405 	/* struct socket *so = arg; */
    406 
    407 	KASSERT(arg != NULL);
    408 	DPRINTF("Connecting\n");
    409 }
    410 
    411 static void
    412 rfcomm_connected(void *arg)
    413 {
    414 	struct socket *so = arg;
    415 
    416 	KASSERT(so != NULL);
    417 	DPRINTF("Connected\n");
    418 	soisconnected(so);
    419 }
    420 
    421 static void
    422 rfcomm_disconnected(void *arg, int err)
    423 {
    424 	struct socket *so = arg;
    425 
    426 	KASSERT(so != NULL);
    427 	DPRINTF("Disconnected\n");
    428 
    429 	so->so_error = err;
    430 	soisdisconnected(so);
    431 }
    432 
    433 static void *
    434 rfcomm_newconn(void *arg, struct sockaddr_bt *laddr,
    435     struct sockaddr_bt *raddr)
    436 {
    437 	struct socket *so = arg;
    438 
    439 	DPRINTF("New Connection\n");
    440 	so = sonewconn(so, false);
    441 	if (so == NULL)
    442 		return NULL;
    443 
    444 	soisconnecting(so);
    445 
    446 	return so->so_pcb;
    447 }
    448 
    449 /*
    450  * rfcomm_complete(rfcomm_dlc, length)
    451  *
    452  * length bytes are sent and may be removed from socket buffer
    453  */
    454 static void
    455 rfcomm_complete(void *arg, int length)
    456 {
    457 	struct socket *so = arg;
    458 
    459 	sbdrop(&so->so_snd, length);
    460 	sowwakeup(so);
    461 }
    462 
    463 /*
    464  * rfcomm_linkmode(rfcomm_dlc, new)
    465  *
    466  * link mode change notification.
    467  */
    468 static void
    469 rfcomm_linkmode(void *arg, int new)
    470 {
    471 	struct socket *so = arg;
    472 	struct sockopt sopt;
    473 	int mode;
    474 
    475 	DPRINTF("auth %s, encrypt %s, secure %s\n",
    476 		(new & RFCOMM_LM_AUTH ? "on" : "off"),
    477 		(new & RFCOMM_LM_ENCRYPT ? "on" : "off"),
    478 		(new & RFCOMM_LM_SECURE ? "on" : "off"));
    479 
    480 	sockopt_init(&sopt, BTPROTO_RFCOMM, SO_RFCOMM_LM, 0);
    481 	(void)rfcomm_getopt(so->so_pcb, &sopt);
    482 	(void)sockopt_getint(&sopt, &mode);
    483 	sockopt_destroy(&sopt);
    484 
    485 	if (((mode & RFCOMM_LM_AUTH) && !(new & RFCOMM_LM_AUTH))
    486 	    || ((mode & RFCOMM_LM_ENCRYPT) && !(new & RFCOMM_LM_ENCRYPT))
    487 	    || ((mode & RFCOMM_LM_SECURE) && !(new & RFCOMM_LM_SECURE)))
    488 		rfcomm_disconnect_pcb(so->so_pcb, 0);
    489 }
    490 
    491 /*
    492  * rfcomm_input(rfcomm_dlc, mbuf)
    493  */
    494 static void
    495 rfcomm_input(void *arg, struct mbuf *m)
    496 {
    497 	struct socket *so = arg;
    498 
    499 	KASSERT(so != NULL);
    500 
    501 	if (m->m_pkthdr.len > sbspace(&so->so_rcv)) {
    502 		printf("%s: %d bytes dropped (socket buffer full)\n",
    503 			__func__, m->m_pkthdr.len);
    504 		m_freem(m);
    505 		return;
    506 	}
    507 
    508 	DPRINTFN(10, "received %d bytes\n", m->m_pkthdr.len);
    509 
    510 	sbappendstream(&so->so_rcv, m);
    511 	sorwakeup(so);
    512 }
    513 
    514 PR_WRAP_USRREQS(rfcomm)
    515 
    516 #define	rfcomm_attach		rfcomm_attach_wrapper
    517 #define	rfcomm_detach		rfcomm_detach_wrapper
    518 #define	rfcomm_accept		rfcomm_accept_wrapper
    519 #define	rfcomm_bind		rfcomm_bind_wrapper
    520 #define	rfcomm_listen		rfcomm_listen_wrapper
    521 #define	rfcomm_connect		rfcomm_connect_wrapper
    522 #define	rfcomm_connect2		rfcomm_connect2_wrapper
    523 #define	rfcomm_disconnect	rfcomm_disconnect_wrapper
    524 #define	rfcomm_shutdown		rfcomm_shutdown_wrapper
    525 #define	rfcomm_abort		rfcomm_abort_wrapper
    526 #define	rfcomm_ioctl		rfcomm_ioctl_wrapper
    527 #define	rfcomm_stat		rfcomm_stat_wrapper
    528 #define	rfcomm_peeraddr		rfcomm_peeraddr_wrapper
    529 #define	rfcomm_sockaddr		rfcomm_sockaddr_wrapper
    530 #define	rfcomm_rcvd		rfcomm_rcvd_wrapper
    531 #define	rfcomm_recvoob		rfcomm_recvoob_wrapper
    532 #define	rfcomm_send		rfcomm_send_wrapper
    533 #define	rfcomm_sendoob		rfcomm_sendoob_wrapper
    534 #define	rfcomm_purgeif		rfcomm_purgeif_wrapper
    535 
    536 const struct pr_usrreqs rfcomm_usrreqs = {
    537 	.pr_attach	= rfcomm_attach,
    538 	.pr_detach	= rfcomm_detach,
    539 	.pr_accept	= rfcomm_accept,
    540 	.pr_bind	= rfcomm_bind,
    541 	.pr_listen	= rfcomm_listen,
    542 	.pr_connect	= rfcomm_connect,
    543 	.pr_connect2	= rfcomm_connect2,
    544 	.pr_disconnect	= rfcomm_disconnect,
    545 	.pr_shutdown	= rfcomm_shutdown,
    546 	.pr_abort	= rfcomm_abort,
    547 	.pr_ioctl	= rfcomm_ioctl,
    548 	.pr_stat	= rfcomm_stat,
    549 	.pr_peeraddr	= rfcomm_peeraddr,
    550 	.pr_sockaddr	= rfcomm_sockaddr,
    551 	.pr_rcvd	= rfcomm_rcvd,
    552 	.pr_recvoob	= rfcomm_recvoob,
    553 	.pr_send	= rfcomm_send,
    554 	.pr_sendoob	= rfcomm_sendoob,
    555 	.pr_purgeif	= rfcomm_purgeif,
    556 };
    557