1 /* 2 * ng_h4.c 3 */ 4 5 /*- 6 * Copyright (c) 2001-2002 Maksim Yevmenkin <m_evmenkin@yahoo.com> 7 * All rights reserved. 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 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 * 30 * $Id: ng_h4.c,v 1.10 2005/10/31 17:57:43 max Exp $ 31 * $FreeBSD: src/sys/netgraph/bluetooth/drivers/h4/ng_h4.c,v 1.17 2007/08/13 17:19:28 emax Exp $ 32 * $DragonFly: src/sys/netgraph7/bluetooth/drivers/h4/ng_h4.c,v 1.2 2008/06/26 23:05:40 dillon Exp $ 33 * $DragonFly: src/sys/netgraph7/bluetooth/drivers/h4/ng_h4.c,v 1.2 2008/06/26 23:05:40 dillon Exp $ 34 * 35 * Based on: 36 * --------- 37 * 38 * FreeBSD: src/sys/netgraph/ng_tty.c 39 * Author: Archie Cobbs <archie@freebsd.org> 40 * 41 */ 42 43 #include <sys/param.h> 44 #include <sys/systm.h> 45 #include <sys/kernel.h> 46 #include <sys/conf.h> 47 #include <sys/endian.h> 48 #include <sys/errno.h> 49 #include <sys/fcntl.h> 50 #include <sys/malloc.h> 51 #include <sys/mbuf.h> 52 #include <sys/priv.h> 53 #include <sys/socket.h> 54 #include <sys/tty.h> 55 #include <sys/ttycom.h> 56 #include <net/if.h> 57 #include <net/if_var.h> 58 #include "ng_message.h" 59 #include "netgraph.h" 60 #include "ng_parse.h" 61 #include "bluetooth/include/ng_bluetooth.h" 62 #include "bluetooth/include/ng_hci.h" 63 #include "bluetooth/include/ng_h4.h" 64 #include "bluetooth/drivers/h4/ng_h4_var.h" 65 #include "bluetooth/drivers/h4/ng_h4_prse.h" 66 67 /***************************************************************************** 68 ***************************************************************************** 69 ** This node implements a Bluetooth HCI UART transport layer as per chapter 70 ** H4 of the Bluetooth Specification Book v1.1. It is a terminal line 71 ** discipline that is also a netgraph node. Installing this line discipline 72 ** on a terminal device instantiates a new netgraph node of this type, which 73 ** allows access to the device via the "hook" hook of the node. 74 ** 75 ** Once the line discipline is installed, you can find out the name of the 76 ** corresponding netgraph node via a NGIOCGINFO ioctl(). 77 ***************************************************************************** 78 *****************************************************************************/ 79 80 /* MALLOC define */ 81 #ifndef NG_SEPARATE_MALLOC 82 MALLOC_DEFINE(M_NETGRAPH_H4, "netgraph_h4", "Netgraph Bluetooth H4 node"); 83 #else 84 #define M_NETGRAPH_H4 M_NETGRAPH 85 #endif /* NG_SEPARATE_MALLOC */ 86 87 /* Line discipline methods */ 88 static int ng_h4_open (struct cdev *, struct tty *); 89 static int ng_h4_close (struct tty *, int); 90 static int ng_h4_read (struct tty *, struct uio *, int); 91 static int ng_h4_write (struct tty *, struct uio *, int); 92 static int ng_h4_input (int, struct tty *); 93 static int ng_h4_start (struct tty *); 94 static int ng_h4_ioctl (struct tty *, u_long, caddr_t, 95 int, struct thread *); 96 97 /* Line discipline descriptor */ 98 static struct linesw ng_h4_disc = { 99 ng_h4_open, /* open */ 100 ng_h4_close, /* close */ 101 ng_h4_read, /* read */ 102 ng_h4_write, /* write */ 103 ng_h4_ioctl, /* ioctl */ 104 ng_h4_input, /* input */ 105 ng_h4_start, /* start */ 106 ttymodem /* modem */ 107 }; 108 109 /* Netgraph methods */ 110 static ng_constructor_t ng_h4_constructor; 111 static ng_rcvmsg_t ng_h4_rcvmsg; 112 static ng_shutdown_t ng_h4_shutdown; 113 static ng_newhook_t ng_h4_newhook; 114 static ng_connect_t ng_h4_connect; 115 static ng_rcvdata_t ng_h4_rcvdata; 116 static ng_disconnect_t ng_h4_disconnect; 117 118 /* Other stuff */ 119 static void ng_h4_process_timeout (node_p, hook_p, void *, int); 120 static int ng_h4_mod_event (module_t, int, void *); 121 122 /* Netgraph node type descriptor */ 123 static struct ng_type typestruct = { 124 .version = NG_ABI_VERSION, 125 .name = NG_H4_NODE_TYPE, 126 .mod_event = ng_h4_mod_event, 127 .constructor = ng_h4_constructor, 128 .rcvmsg = ng_h4_rcvmsg, 129 .shutdown = ng_h4_shutdown, 130 .newhook = ng_h4_newhook, 131 .connect = ng_h4_connect, 132 .rcvdata = ng_h4_rcvdata, 133 .disconnect = ng_h4_disconnect, 134 .cmdlist = ng_h4_cmdlist 135 }; 136 NETGRAPH_INIT(h4, &typestruct); 137 MODULE_VERSION(ng_h4, NG_BLUETOOTH_VERSION); 138 139 static int ng_h4_node = 0; 140 141 /***************************************************************************** 142 ***************************************************************************** 143 ** Line discipline methods 144 ***************************************************************************** 145 *****************************************************************************/ 146 147 /* 148 * Set our line discipline on the tty. 149 */ 150 151 static int 152 ng_h4_open(struct cdev *dev, struct tty *tp) 153 { 154 struct thread *td = curthread; 155 char name[NG_NODESIZ]; 156 ng_h4_info_p sc = NULL; 157 int error; 158 159 /* Super-user only */ 160 error = priv_check(td, PRIV_NETGRAPH_TTY); /* XXX */ 161 if (error != 0) 162 return (error); 163 164 /* Initialize private struct */ 165 MALLOC(sc, ng_h4_info_p, sizeof(*sc), M_NETGRAPH_H4, M_WAITOK | M_NULLOK | M_ZERO); 166 if (sc == NULL) 167 return (ENOMEM); 168 169 sc->tp = tp; 170 sc->debug = NG_H4_WARN_LEVEL; 171 172 sc->state = NG_H4_W4_PKT_IND; 173 sc->want = 1; 174 sc->got = 0; 175 176 mtx_init(&sc->outq.ifq_mtx, "ng_h4 node+queue", NULL, MTX_DEF); 177 IFQ_SET_MAXLEN(&sc->outq, NG_H4_DEFAULTQLEN); 178 ng_callout_init(&sc->timo); 179 180 NG_H4_LOCK(sc); 181 182 /* Setup netgraph node */ 183 error = ng_make_node_common(&typestruct, &sc->node); 184 if (error != 0) { 185 NG_H4_UNLOCK(sc); 186 187 printf("%s: Unable to create new node!\n", __func__); 188 189 mtx_destroy(&sc->outq.ifq_mtx); 190 bzero(sc, sizeof(*sc)); 191 FREE(sc, M_NETGRAPH_H4); 192 193 return (error); 194 } 195 196 /* Assign node its name */ 197 snprintf(name, sizeof(name), "%s%d", typestruct.name, ng_h4_node ++); 198 199 error = ng_name_node(sc->node, name); 200 if (error != 0) { 201 NG_H4_UNLOCK(sc); 202 203 printf("%s: %s - node name exists?\n", __func__, name); 204 205 NG_NODE_UNREF(sc->node); 206 mtx_destroy(&sc->outq.ifq_mtx); 207 bzero(sc, sizeof(*sc)); 208 FREE(sc, M_NETGRAPH_H4); 209 210 return (error); 211 } 212 213 /* Set back pointers */ 214 NG_NODE_SET_PRIVATE(sc->node, sc); 215 tp->t_lsc = (caddr_t) sc; 216 217 /* The node has to be a WRITER because data can change node status */ 218 NG_NODE_FORCE_WRITER(sc->node); 219 220 /* 221 * Pre-allocate cblocks to the an appropriate amount. 222 * I'm not sure what is appropriate. 223 */ 224 225 ttyflush(tp, FREAD | FWRITE); 226 clist_alloc_cblocks(&tp->t_canq, 0, 0); 227 clist_alloc_cblocks(&tp->t_rawq, 0, 0); 228 clist_alloc_cblocks(&tp->t_outq, 229 MLEN + NG_H4_HIWATER, MLEN + NG_H4_HIWATER); 230 231 NG_H4_UNLOCK(sc); 232 233 return (error); 234 } /* ng_h4_open */ 235 236 /* 237 * Line specific close routine, called from device close routine 238 * and from ttioctl. This causes the node to be destroyed as well. 239 */ 240 241 static int 242 ng_h4_close(struct tty *tp, int flag) 243 { 244 ng_h4_info_p sc = (ng_h4_info_p) tp->t_lsc; 245 246 ttyflush(tp, FREAD | FWRITE); 247 clist_free_cblocks(&tp->t_outq); 248 249 if (sc != NULL) { 250 NG_H4_LOCK(sc); 251 252 if (callout_pending(&sc->timo)) 253 ng_uncallout(&sc->timo, sc->node); 254 255 tp->t_lsc = NULL; 256 sc->dying = 1; 257 258 NG_H4_UNLOCK(sc); 259 260 ng_rmnode_self(sc->node); 261 } 262 263 return (0); 264 } /* ng_h4_close */ 265 266 /* 267 * Once the device has been turned into a node, we don't allow reading. 268 */ 269 270 static int 271 ng_h4_read(struct tty *tp, struct uio *uio, int flag) 272 { 273 return (EIO); 274 } /* ng_h4_read */ 275 276 /* 277 * Once the device has been turned into a node, we don't allow writing. 278 */ 279 280 static int 281 ng_h4_write(struct tty *tp, struct uio *uio, int flag) 282 { 283 return (EIO); 284 } /* ng_h4_write */ 285 286 /* 287 * We implement the NGIOCGINFO ioctl() defined in ng_message.h. 288 */ 289 290 static int 291 ng_h4_ioctl(struct tty *tp, u_long cmd, caddr_t data, int flag, 292 struct thread *td) 293 { 294 ng_h4_info_p sc = (ng_h4_info_p) tp->t_lsc; 295 int error = 0; 296 297 if (sc == NULL) 298 return (ENXIO); 299 300 NG_H4_LOCK(sc); 301 302 switch (cmd) { 303 case NGIOCGINFO: 304 #undef NI 305 #define NI(x) ((struct nodeinfo *)(x)) 306 307 bzero(data, sizeof(*NI(data))); 308 309 if (NG_NODE_HAS_NAME(sc->node)) 310 strncpy(NI(data)->name, NG_NODE_NAME(sc->node), 311 sizeof(NI(data)->name) - 1); 312 313 strncpy(NI(data)->type, sc->node->nd_type->name, 314 sizeof(NI(data)->type) - 1); 315 316 NI(data)->id = (u_int32_t) ng_node2ID(sc->node); 317 NI(data)->hooks = NG_NODE_NUMHOOKS(sc->node); 318 break; 319 320 default: 321 error = ENOIOCTL; 322 break; 323 } 324 325 NG_H4_UNLOCK(sc); 326 327 return (error); 328 } /* ng_h4_ioctl */ 329 330 /* 331 * Receive data coming from the device. We get one character at a time, which 332 * is kindof silly. 333 */ 334 335 static int 336 ng_h4_input(int c, struct tty *tp) 337 { 338 ng_h4_info_p sc = (ng_h4_info_p) tp->t_lsc; 339 340 if (sc == NULL || tp != sc->tp || 341 sc->node == NULL || NG_NODE_NOT_VALID(sc->node)) 342 return (0); 343 344 NG_H4_LOCK(sc); 345 346 /* Check for error conditions */ 347 if ((tp->t_state & TS_CONNECTED) == 0) { 348 NG_H4_INFO("%s: %s - no carrier\n", __func__, 349 NG_NODE_NAME(sc->node)); 350 351 sc->state = NG_H4_W4_PKT_IND; 352 sc->want = 1; 353 sc->got = 0; 354 355 NG_H4_UNLOCK(sc); 356 357 return (0); /* XXX Loss of synchronization here! */ 358 } 359 360 /* Check for framing error or overrun on this char */ 361 if (c & TTY_ERRORMASK) { 362 NG_H4_ERR("%s: %s - line error %#x, c=%#x\n", __func__, 363 NG_NODE_NAME(sc->node), c & TTY_ERRORMASK, 364 c & TTY_CHARMASK); 365 366 NG_H4_STAT_IERROR(sc->stat); 367 368 sc->state = NG_H4_W4_PKT_IND; 369 sc->want = 1; 370 sc->got = 0; 371 372 NG_H4_UNLOCK(sc); 373 374 return (0); /* XXX Loss of synchronization here! */ 375 } 376 377 NG_H4_STAT_BYTES_RECV(sc->stat, 1); 378 379 /* Append char to mbuf */ 380 if (sc->got >= sizeof(sc->ibuf)) { 381 NG_H4_ALERT("%s: %s - input buffer overflow, c=%#x, got=%d\n", 382 __func__, NG_NODE_NAME(sc->node), c & TTY_CHARMASK, 383 sc->got); 384 385 NG_H4_STAT_IERROR(sc->stat); 386 387 sc->state = NG_H4_W4_PKT_IND; 388 sc->want = 1; 389 sc->got = 0; 390 391 NG_H4_UNLOCK(sc); 392 393 return (0); /* XXX Loss of synchronization here! */ 394 } 395 396 sc->ibuf[sc->got ++] = (c & TTY_CHARMASK); 397 398 NG_H4_INFO("%s: %s - got char %#x, want=%d, got=%d\n", __func__, 399 NG_NODE_NAME(sc->node), c, sc->want, sc->got); 400 401 if (sc->got < sc->want) { 402 NG_H4_UNLOCK(sc); 403 404 return (0); /* Wait for more */ 405 } 406 407 switch (sc->state) { 408 /* Got packet indicator */ 409 case NG_H4_W4_PKT_IND: 410 NG_H4_INFO("%s: %s - got packet indicator %#x\n", __func__, 411 NG_NODE_NAME(sc->node), sc->ibuf[0]); 412 413 sc->state = NG_H4_W4_PKT_HDR; 414 415 /* 416 * Since packet indicator included in the packet header 417 * just set sc->want to sizeof(packet header). 418 */ 419 420 switch (sc->ibuf[0]) { 421 case NG_HCI_ACL_DATA_PKT: 422 sc->want = sizeof(ng_hci_acldata_pkt_t); 423 break; 424 425 case NG_HCI_SCO_DATA_PKT: 426 sc->want = sizeof(ng_hci_scodata_pkt_t); 427 break; 428 429 case NG_HCI_EVENT_PKT: 430 sc->want = sizeof(ng_hci_event_pkt_t); 431 break; 432 433 default: 434 NG_H4_WARN("%s: %s - ignoring unknown packet " \ 435 "type=%#x\n", __func__, NG_NODE_NAME(sc->node), 436 sc->ibuf[0]); 437 438 NG_H4_STAT_IERROR(sc->stat); 439 440 sc->state = NG_H4_W4_PKT_IND; 441 sc->want = 1; 442 sc->got = 0; 443 break; 444 } 445 break; 446 447 /* Got packet header */ 448 case NG_H4_W4_PKT_HDR: 449 sc->state = NG_H4_W4_PKT_DATA; 450 451 switch (sc->ibuf[0]) { 452 case NG_HCI_ACL_DATA_PKT: 453 c = le16toh(((ng_hci_acldata_pkt_t *) 454 (sc->ibuf))->length); 455 break; 456 457 case NG_HCI_SCO_DATA_PKT: 458 c = ((ng_hci_scodata_pkt_t *)(sc->ibuf))->length; 459 break; 460 461 case NG_HCI_EVENT_PKT: 462 c = ((ng_hci_event_pkt_t *)(sc->ibuf))->length; 463 break; 464 465 default: 466 KASSERT((0), ("Invalid packet type=%#x\n", 467 sc->ibuf[0])); 468 break; 469 } 470 471 NG_H4_INFO("%s: %s - got packet header, packet type=%#x, " \ 472 "packet size=%d, payload size=%d\n", __func__, 473 NG_NODE_NAME(sc->node), sc->ibuf[0], sc->got, c); 474 475 if (c > 0) { 476 sc->want += c; 477 478 /* 479 * Try to prevent possible buffer overrun 480 * 481 * XXX I'm *really* confused here. It turns out 482 * that Xircom card sends us packets with length 483 * greater then 512 bytes! This is greater then 484 * our old receive buffer (ibuf) size. In the same 485 * time the card demands from us *not* to send 486 * packets greater then 192 bytes. Weird! How the 487 * hell i should know how big *receive* buffer 488 * should be? For now increase receiving buffer 489 * size to 1K and add the following check. 490 */ 491 492 if (sc->want >= sizeof(sc->ibuf)) { 493 int b; 494 495 NG_H4_ALERT("%s: %s - packet too big for " \ 496 "buffer, type=%#x, got=%d, want=%d, " \ 497 "length=%d\n", __func__, 498 NG_NODE_NAME(sc->node), sc->ibuf[0], 499 sc->got, sc->want, c); 500 501 NG_H4_ALERT("Packet header:\n"); 502 for (b = 0; b < sc->got; b++) 503 NG_H4_ALERT("%#x ", sc->ibuf[b]); 504 NG_H4_ALERT("\n"); 505 506 /* Reset state */ 507 NG_H4_STAT_IERROR(sc->stat); 508 509 sc->state = NG_H4_W4_PKT_IND; 510 sc->want = 1; 511 sc->got = 0; 512 } 513 514 break; 515 } 516 517 /* else FALLTHROUGH and deliver frame */ 518 /* XXX Is this true? Should we deliver empty frame? */ 519 520 /* Got packet data */ 521 case NG_H4_W4_PKT_DATA: 522 NG_H4_INFO("%s: %s - got full packet, packet type=%#x, " \ 523 "packet size=%d\n", __func__, 524 NG_NODE_NAME(sc->node), sc->ibuf[0], sc->got); 525 526 if (sc->hook != NULL && NG_HOOK_IS_VALID(sc->hook)) { 527 struct mbuf *m = NULL; 528 529 MGETHDR(m, MB_DONTWAIT, MT_DATA); 530 if (m != NULL) { 531 m->m_pkthdr.len = 0; 532 533 /* XXX m_copyback() is stupid */ 534 m->m_len = min(MHLEN, sc->got); 535 536 m_copyback(m, 0, sc->got, sc->ibuf); 537 NG_SEND_DATA_ONLY(c, sc->hook, m); 538 } else { 539 NG_H4_ERR("%s: %s - could not get mbuf\n", 540 __func__, NG_NODE_NAME(sc->node)); 541 542 NG_H4_STAT_IERROR(sc->stat); 543 } 544 } 545 546 sc->state = NG_H4_W4_PKT_IND; 547 sc->want = 1; 548 sc->got = 0; 549 550 NG_H4_STAT_PCKTS_RECV(sc->stat); 551 break; 552 553 default: 554 KASSERT((0), ("Invalid H4 node state=%d", sc->state)); 555 break; 556 } 557 558 NG_H4_UNLOCK(sc); 559 560 return (0); 561 } /* ng_h4_input */ 562 563 /* 564 * This is called when the device driver is ready for more output. Called from 565 * tty system. 566 */ 567 568 static int 569 ng_h4_start(struct tty *tp) 570 { 571 ng_h4_info_p sc = (ng_h4_info_p) tp->t_lsc; 572 struct mbuf *m = NULL; 573 int size; 574 575 if (sc == NULL || tp != sc->tp || 576 sc->node == NULL || NG_NODE_NOT_VALID(sc->node)) 577 return (0); 578 579 #if 0 580 while (tp->t_outq.c_cc < NG_H4_HIWATER) { /* XXX 2.2 specific ? */ 581 #else 582 while (1) { 583 #endif 584 /* Remove first mbuf from queue */ 585 IF_DEQUEUE(&sc->outq, m); 586 if (m == NULL) 587 break; 588 589 /* Send as much of it as possible */ 590 while (m != NULL) { 591 size = m->m_len - b_to_q(mtod(m, u_char *), 592 m->m_len, &tp->t_outq); 593 594 NG_H4_LOCK(sc); 595 NG_H4_STAT_BYTES_SENT(sc->stat, size); 596 NG_H4_UNLOCK(sc); 597 598 m->m_data += size; 599 m->m_len -= size; 600 if (m->m_len > 0) 601 break; /* device can't take no more */ 602 603 m = m_free(m); 604 } 605 606 /* Put remainder of mbuf chain (if any) back on queue */ 607 if (m != NULL) { 608 IF_PREPEND(&sc->outq, m); 609 break; 610 } 611 612 /* Full packet has been sent */ 613 NG_H4_LOCK(sc); 614 NG_H4_STAT_PCKTS_SENT(sc->stat); 615 NG_H4_UNLOCK(sc); 616 } 617 618 /* 619 * Call output process whether or not there is any output. We are 620 * being called in lieu of ttstart and must do what it would. 621 */ 622 623 tt_oproc(sc->tp); 624 625 /* 626 * This timeout is needed for operation on a pseudo-tty, because the 627 * pty code doesn't call pppstart after it has drained the t_outq. 628 */ 629 630 NG_H4_LOCK(sc); 631 632 if (!IFQ_IS_EMPTY(&sc->outq) && !callout_pending(&sc->timo)) 633 ng_callout(&sc->timo, sc->node, NULL, 1, 634 ng_h4_process_timeout, NULL, 0); 635 636 NG_H4_UNLOCK(sc); 637 638 return (0); 639 } /* ng_h4_start */ 640 641 /***************************************************************************** 642 ***************************************************************************** 643 ** Netgraph node methods 644 ***************************************************************************** 645 *****************************************************************************/ 646 647 /* 648 * Initialize a new node of this type. We only allow nodes to be created as 649 * a result of setting the line discipline on a tty, so always return an error 650 * if not. 651 */ 652 653 static int 654 ng_h4_constructor(node_p node) 655 { 656 return (EOPNOTSUPP); 657 } /* ng_h4_constructor */ 658 659 /* 660 * Add a new hook. There can only be one. 661 */ 662 663 static int 664 ng_h4_newhook(node_p node, hook_p hook, const char *name) 665 { 666 ng_h4_info_p sc = (ng_h4_info_p) NG_NODE_PRIVATE(node); 667 668 if (strcmp(name, NG_H4_HOOK) != 0) 669 return (EINVAL); 670 671 NG_H4_LOCK(sc); 672 673 if (sc->hook != NULL) { 674 NG_H4_UNLOCK(sc); 675 return (EISCONN); 676 } 677 sc->hook = hook; 678 679 NG_H4_UNLOCK(sc); 680 681 return (0); 682 } /* ng_h4_newhook */ 683 684 /* 685 * Connect hook. Just say yes. 686 */ 687 688 static int 689 ng_h4_connect(hook_p hook) 690 { 691 ng_h4_info_p sc = (ng_h4_info_p) NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 692 693 if (hook != sc->hook) 694 panic("%s: hook != sc->hook\n", __func__); 695 696 NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook)); 697 NG_HOOK_FORCE_QUEUE(hook); 698 699 return (0); 700 } /* ng_h4_connect */ 701 702 /* 703 * Disconnect the hook 704 */ 705 706 static int 707 ng_h4_disconnect(hook_p hook) 708 { 709 ng_h4_info_p sc = (ng_h4_info_p) NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 710 711 /* 712 * We need to check for sc != NULL because we can be called from 713 * ng_h4_close() via ng_rmnode_self() 714 */ 715 716 if (sc != NULL) { 717 if (hook != sc->hook) 718 panic("%s: hook != sc->hook\n", __func__); 719 720 NG_H4_LOCK(sc); 721 722 /* XXX do we have to untimeout and drain out queue? */ 723 if (callout_pending(&sc->timo)) 724 ng_uncallout(&sc->timo, sc->node); 725 726 _IF_DRAIN(&sc->outq); 727 728 sc->state = NG_H4_W4_PKT_IND; 729 sc->want = 1; 730 sc->got = 0; 731 732 sc->hook = NULL; 733 734 NG_H4_UNLOCK(sc); 735 } 736 737 return (0); 738 } /* ng_h4_disconnect */ 739 740 /* 741 * Remove this node. The does the netgraph portion of the shutdown. 742 * This should only be called indirectly from ng_h4_close(). 743 */ 744 745 static int 746 ng_h4_shutdown(node_p node) 747 { 748 ng_h4_info_p sc = (ng_h4_info_p) NG_NODE_PRIVATE(node); 749 750 NG_H4_LOCK(sc); 751 752 if (!sc->dying) { 753 NG_H4_UNLOCK(sc); 754 755 NG_NODE_REVIVE(node); /* we will persist */ 756 757 return (EOPNOTSUPP); 758 } 759 760 NG_H4_UNLOCK(sc); 761 762 NG_NODE_SET_PRIVATE(node, NULL); 763 764 _IF_DRAIN(&sc->outq); 765 766 NG_NODE_UNREF(node); 767 mtx_destroy(&sc->outq.ifq_mtx); 768 bzero(sc, sizeof(*sc)); 769 FREE(sc, M_NETGRAPH_H4); 770 771 return (0); 772 } /* ng_h4_shutdown */ 773 774 /* 775 * Receive incoming data from Netgraph system. Put it on our 776 * output queue and start output if necessary. 777 */ 778 779 static int 780 ng_h4_rcvdata(hook_p hook, item_p item) 781 { 782 ng_h4_info_p sc = (ng_h4_info_p)NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 783 struct mbuf *m = NULL; 784 int qlen; 785 786 if (sc == NULL) 787 return (EHOSTDOWN); 788 789 if (hook != sc->hook) 790 panic("%s: hook != sc->hook\n", __func__); 791 792 NGI_GET_M(item, m); 793 NG_FREE_ITEM(item); 794 795 NG_H4_LOCK(sc); 796 797 if (_IF_QFULL(&sc->outq)) { 798 NG_H4_ERR("%s: %s - dropping mbuf, len=%d\n", __func__, 799 NG_NODE_NAME(sc->node), m->m_pkthdr.len); 800 801 NG_H4_STAT_OERROR(sc->stat); 802 _IF_DROP(&sc->outq); 803 804 NG_H4_UNLOCK(sc); 805 806 NG_FREE_M(m); 807 808 return (ENOBUFS); 809 } 810 811 NG_H4_INFO("%s: %s - queue mbuf, len=%d\n", __func__, 812 NG_NODE_NAME(sc->node), m->m_pkthdr.len); 813 814 _IF_ENQUEUE(&sc->outq, m); 815 qlen = _IF_QLEN(&sc->outq); 816 817 NG_H4_UNLOCK(sc); 818 819 /* 820 * If qlen > 1, then we should already have a scheduled callout 821 */ 822 823 if (qlen == 1) { 824 mtx_lock(&Giant); 825 ng_h4_start(sc->tp); 826 mtx_unlock(&Giant); 827 } 828 829 return (0); 830 } /* ng_h4_rcvdata */ 831 832 /* 833 * Receive control message 834 */ 835 836 static int 837 ng_h4_rcvmsg(node_p node, item_p item, hook_p lasthook) 838 { 839 ng_h4_info_p sc = (ng_h4_info_p) NG_NODE_PRIVATE(node); 840 struct ng_mesg *msg = NULL, *resp = NULL; 841 int error = 0; 842 843 if (sc == NULL) 844 return (EHOSTDOWN); 845 846 NGI_GET_MSG(item, msg); 847 NG_H4_LOCK(sc); 848 849 switch (msg->header.typecookie) { 850 case NGM_GENERIC_COOKIE: 851 switch (msg->header.cmd) { 852 case NGM_TEXT_STATUS: 853 NG_MKRESPONSE(resp, msg, NG_TEXTRESPONSE, M_WAITOK | M_NULLOK); 854 if (resp == NULL) 855 error = ENOMEM; 856 else 857 snprintf(resp->data, NG_TEXTRESPONSE, 858 "Hook: %s\n" \ 859 "Debug: %d\n" \ 860 "State: %d\n" \ 861 "Queue: [have:%d,max:%d]\n" \ 862 "Input: [got:%d,want:%d]", 863 (sc->hook != NULL)? NG_H4_HOOK : "", 864 sc->debug, 865 sc->state, 866 _IF_QLEN(&sc->outq), 867 sc->outq.ifq_maxlen, 868 sc->got, 869 sc->want); 870 break; 871 872 default: 873 error = EINVAL; 874 break; 875 } 876 break; 877 878 case NGM_H4_COOKIE: 879 switch (msg->header.cmd) { 880 case NGM_H4_NODE_RESET: 881 _IF_DRAIN(&sc->outq); 882 sc->state = NG_H4_W4_PKT_IND; 883 sc->want = 1; 884 sc->got = 0; 885 break; 886 887 case NGM_H4_NODE_GET_STATE: 888 NG_MKRESPONSE(resp, msg, sizeof(ng_h4_node_state_ep), 889 M_WAITOK | M_NULLOK); 890 if (resp == NULL) 891 error = ENOMEM; 892 else 893 *((ng_h4_node_state_ep *)(resp->data)) = 894 sc->state; 895 break; 896 897 case NGM_H4_NODE_GET_DEBUG: 898 NG_MKRESPONSE(resp, msg, sizeof(ng_h4_node_debug_ep), 899 M_WAITOK | M_NULLOK); 900 if (resp == NULL) 901 error = ENOMEM; 902 else 903 *((ng_h4_node_debug_ep *)(resp->data)) = 904 sc->debug; 905 break; 906 907 case NGM_H4_NODE_SET_DEBUG: 908 if (msg->header.arglen != sizeof(ng_h4_node_debug_ep)) 909 error = EMSGSIZE; 910 else 911 sc->debug = 912 *((ng_h4_node_debug_ep *)(msg->data)); 913 break; 914 915 case NGM_H4_NODE_GET_QLEN: 916 NG_MKRESPONSE(resp, msg, sizeof(ng_h4_node_qlen_ep), 917 M_WAITOK | M_NULLOK); 918 if (resp == NULL) 919 error = ENOMEM; 920 else 921 *((ng_h4_node_qlen_ep *)(resp->data)) = 922 sc->outq.ifq_maxlen; 923 break; 924 925 case NGM_H4_NODE_SET_QLEN: 926 if (msg->header.arglen != sizeof(ng_h4_node_qlen_ep)) 927 error = EMSGSIZE; 928 else if (*((ng_h4_node_qlen_ep *)(msg->data)) <= 0) 929 error = EINVAL; 930 else 931 IFQ_SET_MAXLEN(&sc->outq, 932 *((ng_h4_node_qlen_ep *)(msg->data))); 933 break; 934 935 case NGM_H4_NODE_GET_STAT: 936 NG_MKRESPONSE(resp, msg, sizeof(ng_h4_node_stat_ep), 937 M_WAITOK | M_NULLOK); 938 if (resp == NULL) 939 error = ENOMEM; 940 else 941 bcopy(&sc->stat, resp->data, 942 sizeof(ng_h4_node_stat_ep)); 943 break; 944 945 case NGM_H4_NODE_RESET_STAT: 946 NG_H4_STAT_RESET(sc->stat); 947 break; 948 949 default: 950 error = EINVAL; 951 break; 952 } 953 break; 954 955 default: 956 error = EINVAL; 957 break; 958 } 959 960 NG_H4_UNLOCK(sc); 961 962 NG_RESPOND_MSG(error, node, item, resp); 963 NG_FREE_MSG(msg); 964 965 return (error); 966 } /* ng_h4_rcvmsg */ 967 968 /* 969 * Timeout processing function. 970 * We still have data to output to the device, so try sending more. 971 */ 972 973 static void 974 ng_h4_process_timeout(node_p node, hook_p hook, void *arg1, int arg2) 975 { 976 ng_h4_info_p sc = (ng_h4_info_p) NG_NODE_PRIVATE(node); 977 978 mtx_lock(&Giant); 979 ng_h4_start(sc->tp); 980 mtx_unlock(&Giant); 981 } /* ng_h4_process_timeout */ 982 983 /* 984 * Handle loading and unloading for this node type 985 */ 986 987 static int 988 ng_h4_mod_event(module_t mod, int event, void *data) 989 { 990 static int ng_h4_ldisc; 991 int error = 0; 992 993 switch (event) { 994 case MOD_LOAD: 995 /* Register line discipline */ 996 mtx_lock(&Giant); 997 ng_h4_ldisc = ldisc_register(H4DISC, &ng_h4_disc); 998 mtx_unlock(&Giant); 999 1000 if (ng_h4_ldisc < 0) { 1001 printf("%s: can't register H4 line discipline\n", 1002 __func__); 1003 error = EIO; 1004 } 1005 break; 1006 1007 case MOD_UNLOAD: 1008 /* Unregister line discipline */ 1009 mtx_lock(&Giant); 1010 ldisc_deregister(ng_h4_ldisc); 1011 mtx_unlock(&Giant); 1012 break; 1013 1014 default: 1015 error = EOPNOTSUPP; 1016 break; 1017 } 1018 1019 return (error); 1020 } /* ng_h4_mod_event */ 1021 1022