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