1 /* $NetBSD: if_sl.c,v 1.121 2015/08/24 22:21:26 pooka Exp $ */ 2 3 /* 4 * Copyright (c) 1987, 1989, 1992, 1993 5 * The Regents of the University of California. 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. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 * 31 * @(#)if_sl.c 8.9 (Berkeley) 1/9/95 32 */ 33 34 /* 35 * Serial Line interface 36 * 37 * Rick Adams 38 * Center for Seismic Studies 39 * 1300 N 17th Street, Suite 1450 40 * Arlington, Virginia 22209 41 * (703)276-7900 42 * rick@seismo.ARPA 43 * seismo!rick 44 * 45 * Pounded on heavily by Chris Torek (chris@mimsy.umd.edu, umcp-cs!chris). 46 * N.B.: this belongs in netinet, not net, the way it stands now. 47 * Should have a link-layer type designation, but wouldn't be 48 * backwards-compatible. 49 * 50 * Converted to 4.3BSD Beta by Chris Torek. 51 * Other changes made at Berkeley, based in part on code by Kirk Smith. 52 * W. Jolitz added slip abort. 53 * 54 * Hacked almost beyond recognition by Van Jacobson (van@helios.ee.lbl.gov). 55 * Added priority queuing for "interactive" traffic; hooks for TCP 56 * header compression; ICMP filtering (at 2400 baud, some cretin 57 * pinging you can use up all your bandwidth). Made low clist behavior 58 * more robust and slightly less likely to hang serial line. 59 * Sped up a bunch of things. 60 */ 61 62 #include <sys/cdefs.h> 63 __KERNEL_RCSID(0, "$NetBSD: if_sl.c,v 1.121 2015/08/24 22:21:26 pooka Exp $"); 64 65 #ifdef _KERNEL_OPT 66 #include "opt_inet.h" 67 #endif 68 69 #include <sys/param.h> 70 #include <sys/proc.h> 71 #include <sys/malloc.h> 72 #include <sys/mbuf.h> 73 #include <sys/buf.h> 74 #include <sys/dkstat.h> 75 #include <sys/socket.h> 76 #include <sys/ioctl.h> 77 #include <sys/file.h> 78 #include <sys/conf.h> 79 #include <sys/tty.h> 80 #include <sys/kernel.h> 81 #include <sys/socketvar.h> 82 #if __NetBSD__ 83 #include <sys/systm.h> 84 #include <sys/kauth.h> 85 #endif 86 #include <sys/cpu.h> 87 #include <sys/intr.h> 88 89 #include <net/if.h> 90 #include <net/if_types.h> 91 #include <net/netisr.h> 92 #include <net/route.h> 93 94 #ifdef INET 95 #include <netinet/in.h> 96 #include <netinet/in_systm.h> 97 #include <netinet/in_var.h> 98 #include <netinet/ip.h> 99 #endif 100 101 #include <net/slcompress.h> 102 #include <net/if_slvar.h> 103 #include <net/slip.h> 104 #include <net/ppp_defs.h> 105 #include <net/if_ppp.h> 106 107 #include <sys/time.h> 108 #include <net/bpf.h> 109 110 #include "ioconf.h" 111 112 /* 113 * SLMAX is a hard limit on input packet size. To simplify the code 114 * and improve performance, we require that packets fit in an mbuf 115 * cluster, and if we get a compressed packet, there's enough extra 116 * room to expand the header into a max length tcp/ip header (128 117 * bytes). So, SLMAX can be at most 118 * MCLBYTES - 128 119 * 120 * SLMTU is a hard limit on output packet size. To insure good 121 * interactive response, SLMTU wants to be the smallest size that 122 * amortizes the header cost. (Remember that even with 123 * type-of-service queuing, we have to wait for any in-progress 124 * packet to finish. I.e., we wait, on the average, 1/2 * mtu / 125 * cps, where cps is the line speed in characters per second. 126 * E.g., 533ms wait for a 1024 byte MTU on a 9600 baud line. The 127 * average compressed header size is 6-8 bytes so any MTU > 90 128 * bytes will give us 90% of the line bandwidth. A 100ms wait is 129 * tolerable (500ms is not), so want an MTU around 296. (Since TCP 130 * will send 256 byte segments (to allow for 40 byte headers), the 131 * typical packet size on the wire will be around 260 bytes). In 132 * 4.3tahoe+ systems, we can set an MTU in a route so we do that & 133 * leave the interface MTU relatively high (so we don't IP fragment 134 * when acting as a gateway to someone using a stupid MTU). 135 * 136 * Similar considerations apply to SLIP_HIWAT: It's the amount of 137 * data that will be queued 'downstream' of us (i.e., in clists 138 * waiting to be picked up by the tty output interrupt). If we 139 * queue a lot of data downstream, it's immune to our t.o.s. queuing. 140 * E.g., if SLIP_HIWAT is 1024, the interactive traffic in mixed 141 * telnet/ftp will see a 1 sec wait, independent of the mtu (the 142 * wait is dependent on the ftp window size but that's typically 143 * 1k - 4k). So, we want SLIP_HIWAT just big enough to amortize 144 * the cost (in idle time on the wire) of the tty driver running 145 * off the end of its clists & having to call back slstart for a 146 * new packet. For a tty interface with any buffering at all, this 147 * cost will be zero. Even with a totally brain dead interface (like 148 * the one on a typical workstation), the cost will be <= 1 character 149 * time. So, setting SLIP_HIWAT to ~100 guarantees that we'll lose 150 * at most 1% while maintaining good interactive response. 151 */ 152 #define BUFOFFSET (128+sizeof(struct ifnet **)+SLIP_HDRLEN) 153 #define SLMAX (MCLBYTES - BUFOFFSET) 154 #define SLBUFSIZE (SLMAX + BUFOFFSET) 155 #ifndef SLMTU 156 #define SLMTU 296 157 #endif 158 #if (SLMTU < 3) 159 #error SLMTU way too small. 160 #endif 161 #define SLIP_HIWAT roundup(50, TTROUND) 162 #ifndef __NetBSD__ /* XXX - cgd */ 163 #define CLISTRESERVE 1024 /* Can't let clists get too low */ 164 #endif /* !__NetBSD__ */ 165 166 /* 167 * SLIP ABORT ESCAPE MECHANISM: 168 * (inspired by HAYES modem escape arrangement) 169 * 1sec escape 1sec escape 1sec escape { 1sec escape 1sec escape } 170 * within window time signals a "soft" exit from slip mode by remote end 171 * if the IFF_DEBUG flag is on. 172 */ 173 #define ABT_ESC '\033' /* can't be t_intr - distant host must know it*/ 174 #define ABT_IDLE 1 /* in seconds - idle before an escape */ 175 #define ABT_COUNT 3 /* count of escapes for abort */ 176 #define ABT_WINDOW (ABT_COUNT*2+2) /* in seconds - time to count */ 177 178 static int sl_clone_create(struct if_clone *, int); 179 static int sl_clone_destroy(struct ifnet *); 180 181 static LIST_HEAD(, sl_softc) sl_softc_list; 182 183 struct if_clone sl_cloner = 184 IF_CLONE_INITIALIZER("sl", sl_clone_create, sl_clone_destroy); 185 186 #define FRAME_END 0xc0 /* Frame End */ 187 #define FRAME_ESCAPE 0xdb /* Frame Esc */ 188 #define TRANS_FRAME_END 0xdc /* transposed frame end */ 189 #define TRANS_FRAME_ESCAPE 0xdd /* transposed frame esc */ 190 191 static void slintr(void *); 192 193 static int slinit(struct sl_softc *); 194 static struct mbuf *sl_btom(struct sl_softc *, int); 195 196 static int slclose(struct tty *, int); 197 static int slinput(int, struct tty *); 198 static int slioctl(struct ifnet *, u_long, void *); 199 static int slopen(dev_t, struct tty *); 200 static int sloutput(struct ifnet *, struct mbuf *, const struct sockaddr *, 201 struct rtentry *); 202 static int slstart(struct tty *); 203 static int sltioctl(struct tty *, u_long, void *, int, struct lwp *); 204 205 static struct linesw slip_disc = { 206 .l_name = "slip", 207 .l_open = slopen, 208 .l_close = slclose, 209 .l_read = ttyerrio, 210 .l_write = ttyerrio, 211 .l_ioctl = sltioctl, 212 .l_rint = slinput, 213 .l_start = slstart, 214 .l_modem = nullmodem, 215 .l_poll = ttyerrpoll 216 }; 217 218 void 219 slattach(int n __unused) 220 { 221 222 if (ttyldisc_attach(&slip_disc) != 0) 223 panic("slattach"); 224 LIST_INIT(&sl_softc_list); 225 if_clone_attach(&sl_cloner); 226 } 227 228 static int 229 sl_clone_create(struct if_clone *ifc, int unit) 230 { 231 struct sl_softc *sc; 232 233 sc = malloc(sizeof(*sc), M_DEVBUF, M_WAIT|M_ZERO); 234 sc->sc_unit = unit; 235 if_initname(&sc->sc_if, ifc->ifc_name, unit); 236 sc->sc_if.if_softc = sc; 237 sc->sc_if.if_mtu = SLMTU; 238 sc->sc_if.if_flags = IFF_POINTOPOINT | SC_AUTOCOMP | IFF_MULTICAST; 239 sc->sc_if.if_type = IFT_SLIP; 240 sc->sc_if.if_ioctl = slioctl; 241 sc->sc_if.if_output = sloutput; 242 sc->sc_if.if_dlt = DLT_SLIP; 243 sc->sc_fastq.ifq_maxlen = 32; 244 IFQ_SET_READY(&sc->sc_if.if_snd); 245 if_attach(&sc->sc_if); 246 if_alloc_sadl(&sc->sc_if); 247 bpf_attach(&sc->sc_if, DLT_SLIP, SLIP_HDRLEN); 248 LIST_INSERT_HEAD(&sl_softc_list, sc, sc_iflist); 249 return 0; 250 } 251 252 static int 253 sl_clone_destroy(struct ifnet *ifp) 254 { 255 struct sl_softc *sc = (struct sl_softc *)ifp->if_softc; 256 257 if (sc->sc_ttyp != NULL) 258 return EBUSY; /* Not removing it */ 259 260 LIST_REMOVE(sc, sc_iflist); 261 262 bpf_detach(ifp); 263 if_detach(ifp); 264 265 free(sc, M_DEVBUF); 266 return 0; 267 } 268 269 static int 270 slinit(struct sl_softc *sc) 271 { 272 273 if (sc->sc_mbuf == NULL) { 274 sc->sc_mbuf = m_gethdr(M_WAIT, MT_DATA); 275 m_clget(sc->sc_mbuf, M_WAIT); 276 } 277 sc->sc_ep = (u_char *)sc->sc_mbuf->m_ext.ext_buf + 278 sc->sc_mbuf->m_ext.ext_size; 279 sc->sc_mp = sc->sc_pktstart = (u_char *)sc->sc_mbuf->m_ext.ext_buf + 280 BUFOFFSET; 281 282 #ifdef INET 283 sl_compress_init(&sc->sc_comp); 284 #endif 285 286 return 1; 287 } 288 289 /* 290 * Line specific open routine. 291 * Attach the given tty to the first available sl unit. 292 */ 293 /* ARGSUSED */ 294 static int 295 slopen(dev_t dev, struct tty *tp) 296 { 297 struct lwp *l = curlwp; /* XXX */ 298 struct sl_softc *sc; 299 int error; 300 301 error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_INTERFACE_SLIP, 302 KAUTH_REQ_NETWORK_INTERFACE_SLIP_ADD, NULL, NULL, NULL); 303 if (error) 304 return error; 305 306 if (tp->t_linesw == &slip_disc) 307 return 0; 308 309 LIST_FOREACH(sc, &sl_softc_list, sc_iflist) 310 if (sc->sc_ttyp == NULL) { 311 sc->sc_si = softint_establish(SOFTINT_NET, 312 slintr, sc); 313 if (sc->sc_si == NULL) 314 return ENOMEM; 315 if (slinit(sc) == 0) { 316 softint_disestablish(sc->sc_si); 317 return ENOBUFS; 318 } 319 tp->t_sc = (void *)sc; 320 sc->sc_ttyp = tp; 321 sc->sc_if.if_baudrate = tp->t_ospeed; 322 mutex_spin_enter(&tty_lock); 323 tp->t_state |= TS_ISOPEN | TS_XCLUDE; 324 ttyflush(tp, FREAD | FWRITE); 325 /* 326 * make sure tty output queue is large enough 327 * to hold a full-sized packet (including frame 328 * end, and a possible extra frame end). full-sized 329 * packet occupies a max of 2*SLMAX bytes (because 330 * of possible escapes), and add two on for frame 331 * ends. 332 */ 333 if (tp->t_outq.c_cn < 2 * SLMAX + 2) { 334 sc->sc_oldbufsize = tp->t_outq.c_cn; 335 sc->sc_oldbufquot = tp->t_outq.c_cq != 0; 336 337 clfree(&tp->t_outq); 338 mutex_spin_exit(&tty_lock); 339 error = clalloc(&tp->t_outq, 2 * SLMAX + 2, 0); 340 if (error) { 341 softint_disestablish(sc->sc_si); 342 /* 343 * clalloc() might return -1 which 344 * is no good, so we need to return 345 * something else. 346 */ 347 return ENOMEM; /* XXX ?! */ 348 } 349 } else { 350 sc->sc_oldbufsize = sc->sc_oldbufquot = 0; 351 mutex_spin_exit(&tty_lock); 352 } 353 return 0; 354 } 355 return ENXIO; 356 } 357 358 /* 359 * Line specific close routine. 360 * Detach the tty from the sl unit. 361 */ 362 static int 363 slclose(struct tty *tp, int flag) 364 { 365 struct sl_softc *sc; 366 int s; 367 368 ttywflush(tp); 369 sc = tp->t_sc; 370 371 if (sc != NULL) { 372 softint_disestablish(sc->sc_si); 373 s = splnet(); 374 if_down(&sc->sc_if); 375 IF_PURGE(&sc->sc_fastq); 376 splx(s); 377 378 s = spltty(); 379 ttyldisc_release(tp->t_linesw); 380 tp->t_linesw = ttyldisc_default(); 381 tp->t_state = 0; 382 383 sc->sc_ttyp = NULL; 384 tp->t_sc = NULL; 385 386 m_freem(sc->sc_mbuf); 387 sc->sc_mbuf = NULL; 388 sc->sc_ep = sc->sc_mp = sc->sc_pktstart = NULL; 389 IF_PURGE(&sc->sc_inq); 390 391 /* 392 * If necessary, install a new outq buffer of the 393 * appropriate size. 394 */ 395 if (sc->sc_oldbufsize != 0) { 396 clfree(&tp->t_outq); 397 clalloc(&tp->t_outq, sc->sc_oldbufsize, 398 sc->sc_oldbufquot); 399 } 400 splx(s); 401 } 402 403 return 0; 404 } 405 406 /* 407 * Line specific (tty) ioctl routine. 408 * Provide a way to get the sl unit number. 409 */ 410 /* ARGSUSED */ 411 static int 412 sltioctl(struct tty *tp, u_long cmd, void *data, int flag, 413 struct lwp *l) 414 { 415 struct sl_softc *sc = (struct sl_softc *)tp->t_sc; 416 417 switch (cmd) { 418 case SLIOCGUNIT: 419 *(int *)data = sc->sc_unit; /* XXX */ 420 break; 421 422 default: 423 return EPASSTHROUGH; 424 } 425 return 0; 426 } 427 428 /* 429 * Queue a packet. Start transmission if not active. 430 * Compression happens in slintr(); if we do it here, IP TOS 431 * will cause us to not compress "background" packets, because 432 * ordering gets trashed. It can be done for all packets in slintr(). 433 */ 434 static int 435 sloutput(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst, 436 struct rtentry *rtp) 437 { 438 struct sl_softc *sc = ifp->if_softc; 439 struct ip *ip; 440 struct ifqueue *ifq = NULL; 441 int s, error; 442 ALTQ_DECL(struct altq_pktattr pktattr;) 443 444 IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family, &pktattr); 445 446 /* 447 * `Cannot happen' (see slioctl). Someday we will extend 448 * the line protocol to support other address families. 449 */ 450 if (dst->sa_family != AF_INET) { 451 printf("%s: af%d not supported\n", sc->sc_if.if_xname, 452 dst->sa_family); 453 m_freem(m); 454 sc->sc_if.if_noproto++; 455 return EAFNOSUPPORT; 456 } 457 458 if (sc->sc_ttyp == NULL) { 459 m_freem(m); 460 return ENETDOWN; /* sort of */ 461 } 462 if ((sc->sc_ttyp->t_state & TS_CARR_ON) == 0 && 463 (sc->sc_ttyp->t_cflag & CLOCAL) == 0) { 464 m_freem(m); 465 printf("%s: no carrier and not local\n", sc->sc_if.if_xname); 466 return EHOSTUNREACH; 467 } 468 ip = mtod(m, struct ip *); 469 #ifdef INET 470 if (sc->sc_if.if_flags & SC_NOICMP && ip->ip_p == IPPROTO_ICMP) { 471 m_freem(m); 472 return ENETRESET; /* XXX ? */ 473 } 474 #endif 475 476 s = spltty(); 477 if (sc->sc_oqlen && sc->sc_ttyp->t_outq.c_cc == sc->sc_oqlen) { 478 struct bintime bt; 479 480 /* if output's been stalled for too long, and restart */ 481 getbinuptime(&bt); 482 bintime_sub(&bt, &sc->sc_lastpacket); 483 if (bt.sec > 0) { 484 sc->sc_otimeout++; 485 slstart(sc->sc_ttyp); 486 } 487 } 488 splx(s); 489 490 s = splnet(); 491 #ifdef INET 492 if ((ip->ip_tos & IPTOS_LOWDELAY) != 0) 493 ifq = &sc->sc_fastq; 494 #endif 495 if ((error = ifq_enqueue2(ifp, ifq, m ALTQ_COMMA 496 ALTQ_DECL(&pktattr))) != 0) { 497 splx(s); 498 return error; 499 } 500 getbinuptime(&sc->sc_lastpacket); 501 splx(s); 502 503 s = spltty(); 504 if ((sc->sc_oqlen = sc->sc_ttyp->t_outq.c_cc) == 0) 505 slstart(sc->sc_ttyp); 506 splx(s); 507 508 return 0; 509 } 510 511 /* 512 * Start output on interface. Get another datagram 513 * to send from the interface queue and map it to 514 * the interface before starting output. 515 */ 516 static int 517 slstart(struct tty *tp) 518 { 519 struct sl_softc *sc = tp->t_sc; 520 521 /* 522 * If there is more in the output queue, just send it now. 523 * We are being called in lieu of ttstart and must do what 524 * it would. 525 */ 526 if (tp->t_outq.c_cc != 0) { 527 (*tp->t_oproc)(tp); 528 if (tp->t_outq.c_cc > SLIP_HIWAT) 529 return 0; 530 } 531 532 /* 533 * This happens briefly when the line shuts down. 534 */ 535 if (sc == NULL) 536 return 0; 537 softint_schedule(sc->sc_si); 538 return 0; 539 } 540 541 /* 542 * Copy data buffer to mbuf chain; add ifnet pointer. 543 */ 544 static struct mbuf * 545 sl_btom(struct sl_softc *sc, int len) 546 { 547 struct mbuf *m; 548 549 /* 550 * Allocate a new input buffer and swap. 551 */ 552 m = sc->sc_mbuf; 553 MGETHDR(sc->sc_mbuf, M_DONTWAIT, MT_DATA); 554 if (sc->sc_mbuf == NULL) { 555 sc->sc_mbuf = m; 556 return NULL; 557 } 558 MCLGET(sc->sc_mbuf, M_DONTWAIT); 559 if ((sc->sc_mbuf->m_flags & M_EXT) == 0) { 560 m_freem(sc->sc_mbuf); 561 sc->sc_mbuf = m; 562 return NULL; 563 } 564 sc->sc_ep = (u_char *)sc->sc_mbuf->m_ext.ext_buf + 565 sc->sc_mbuf->m_ext.ext_size; 566 567 m->m_data = sc->sc_pktstart; 568 569 m->m_pkthdr.len = m->m_len = len; 570 m->m_pkthdr.rcvif = &sc->sc_if; 571 return m; 572 } 573 574 /* 575 * tty interface receiver interrupt. 576 */ 577 static int 578 slinput(int c, struct tty *tp) 579 { 580 struct sl_softc *sc; 581 struct mbuf *m; 582 int len; 583 584 tk_nin++; 585 sc = (struct sl_softc *)tp->t_sc; 586 if (sc == NULL) 587 return 0; 588 if ((c & TTY_ERRORMASK) || ((tp->t_state & TS_CARR_ON) == 0 && 589 (tp->t_cflag & CLOCAL) == 0)) { 590 sc->sc_flags |= SC_ERROR; 591 return 0; 592 } 593 c &= TTY_CHARMASK; 594 595 ++sc->sc_if.if_ibytes; 596 597 if (sc->sc_if.if_flags & IFF_DEBUG) { 598 if (c == ABT_ESC) { 599 /* 600 * If we have a previous abort, see whether 601 * this one is within the time limit. 602 */ 603 if (sc->sc_abortcount && 604 time_second >= sc->sc_starttime + ABT_WINDOW) 605 sc->sc_abortcount = 0; 606 /* 607 * If we see an abort after "idle" time, count it; 608 * record when the first abort escape arrived. 609 */ 610 if (time_second >= sc->sc_lasttime + ABT_IDLE) { 611 if (++sc->sc_abortcount == 1) 612 sc->sc_starttime = time_second; 613 if (sc->sc_abortcount >= ABT_COUNT) { 614 slclose(tp, 0); 615 return 0; 616 } 617 } 618 } else 619 sc->sc_abortcount = 0; 620 sc->sc_lasttime = time_second; 621 } 622 623 switch (c) { 624 625 case TRANS_FRAME_ESCAPE: 626 if (sc->sc_escape) 627 c = FRAME_ESCAPE; 628 break; 629 630 case TRANS_FRAME_END: 631 if (sc->sc_escape) 632 c = FRAME_END; 633 break; 634 635 case FRAME_ESCAPE: 636 sc->sc_escape = 1; 637 return 0; 638 639 case FRAME_END: 640 if (sc->sc_flags & SC_ERROR) { 641 sc->sc_flags &= ~SC_ERROR; 642 goto newpack; 643 } 644 len = sc->sc_mp - sc->sc_pktstart; 645 if (len < 3) 646 /* less than min length packet - ignore */ 647 goto newpack; 648 649 m = sl_btom(sc, len); 650 if (m == NULL) 651 goto error; 652 653 IF_ENQUEUE(&sc->sc_inq, m); 654 softint_schedule(sc->sc_si); 655 goto newpack; 656 } 657 if (sc->sc_mp < sc->sc_ep) { 658 *sc->sc_mp++ = c; 659 sc->sc_escape = 0; 660 return 0; 661 } 662 663 /* can't put lower; would miss an extra frame */ 664 sc->sc_flags |= SC_ERROR; 665 666 error: 667 sc->sc_if.if_ierrors++; 668 newpack: 669 sc->sc_mp = sc->sc_pktstart = (u_char *)sc->sc_mbuf->m_ext.ext_buf + 670 BUFOFFSET; 671 sc->sc_escape = 0; 672 673 return 0; 674 } 675 676 static void 677 slintr(void *arg) 678 { 679 struct sl_softc *sc = arg; 680 struct tty *tp = sc->sc_ttyp; 681 struct mbuf *m; 682 int s, len; 683 u_char *pktstart; 684 #ifdef INET 685 u_char c; 686 #endif 687 u_char chdr[CHDR_LEN]; 688 689 KASSERT(tp != NULL); 690 691 /* 692 * Output processing loop. 693 */ 694 mutex_enter(softnet_lock); 695 for (;;) { 696 #ifdef INET 697 struct ip *ip; 698 #endif 699 struct mbuf *m2; 700 struct mbuf *bpf_m; 701 702 /* 703 * Do not remove the packet from the queue if it 704 * doesn't look like it will fit into the current 705 * serial output queue. With a packet full of 706 * escapes, this could be as bad as MTU*2+2. 707 */ 708 s = spltty(); 709 if (tp->t_outq.c_cn - tp->t_outq.c_cc < 710 2 * sc->sc_if.if_mtu + 2) { 711 splx(s); 712 break; 713 } 714 splx(s); 715 716 /* 717 * Get a packet and send it to the interface. 718 */ 719 s = splnet(); 720 IF_DEQUEUE(&sc->sc_fastq, m); 721 if (m) 722 sc->sc_if.if_omcasts++; /* XXX */ 723 else 724 IFQ_DEQUEUE(&sc->sc_if.if_snd, m); 725 splx(s); 726 727 if (m == NULL) 728 break; 729 730 /* 731 * We do the header compression here rather than in 732 * sloutput() because the packets will be out of order 733 * if we are using TOS queueing, and the connection 734 * ID compression will get munged when this happens. 735 */ 736 if (sc->sc_if.if_bpf) { 737 /* 738 * We need to save the TCP/IP header before 739 * it's compressed. To avoid complicated 740 * code, we just make a deep copy of the 741 * entire packet (since this is a serial 742 * line, packets should be short and/or the 743 * copy should be negligible cost compared 744 * to the packet transmission time). 745 */ 746 bpf_m = m_dup(m, 0, M_COPYALL, M_DONTWAIT); 747 } else 748 bpf_m = NULL; 749 #ifdef INET 750 if ((ip = mtod(m, struct ip *))->ip_p == IPPROTO_TCP) { 751 if (sc->sc_if.if_flags & SC_COMPRESS) 752 *mtod(m, u_char *) |= 753 sl_compress_tcp(m, ip, &sc->sc_comp, 1); 754 } 755 #endif 756 if (bpf_m) 757 bpf_mtap_sl_out(&sc->sc_if, mtod(m, u_char *), bpf_m); 758 getbinuptime(&sc->sc_lastpacket); 759 760 s = spltty(); 761 762 /* 763 * The extra FRAME_END will start up a new packet, 764 * and thus will flush any accumulated garbage. We 765 * do this whenever the line may have been idle for 766 * some time. 767 */ 768 if (tp->t_outq.c_cc == 0) { 769 sc->sc_if.if_obytes++; 770 (void)putc(FRAME_END, &tp->t_outq); 771 } 772 773 while (m) { 774 u_char *bp, *cp, *ep; 775 776 bp = cp = mtod(m, u_char *); 777 ep = cp + m->m_len; 778 while (cp < ep) { 779 /* 780 * Find out how many bytes in the 781 * string we can handle without 782 * doing something special. 783 */ 784 while (cp < ep) { 785 switch (*cp++) { 786 case FRAME_ESCAPE: 787 case FRAME_END: 788 cp--; 789 goto out; 790 } 791 } 792 out: 793 if (cp > bp) { 794 /* 795 * Put N characters at once 796 * into the tty output queue. 797 */ 798 if (b_to_q(bp, cp - bp, &tp->t_outq)) 799 break; 800 sc->sc_if.if_obytes += cp - bp; 801 } 802 /* 803 * If there are characters left in 804 * the mbuf, the first one must be 805 * special.. Put it out in a different 806 * form. 807 */ 808 if (cp < ep) { 809 if (putc(FRAME_ESCAPE, &tp->t_outq)) 810 break; 811 if (putc(*cp++ == FRAME_ESCAPE ? 812 TRANS_FRAME_ESCAPE : 813 TRANS_FRAME_END, 814 &tp->t_outq)) { 815 (void)unputc(&tp->t_outq); 816 break; 817 } 818 sc->sc_if.if_obytes += 2; 819 } 820 bp = cp; 821 } 822 MFREE(m, m2); 823 m = m2; 824 } 825 826 if (putc(FRAME_END, &tp->t_outq)) { 827 /* 828 * Not enough room. Remove a char to make 829 * room and end the packet normally. If 830 * you get many collisions (more than one 831 * or two a day), you probably do not have 832 * enough clists and you should increase 833 * "nclist" in param.c 834 */ 835 (void)unputc(&tp->t_outq); 836 (void)putc(FRAME_END, &tp->t_outq); 837 sc->sc_if.if_collisions++; 838 } else { 839 sc->sc_if.if_obytes++; 840 sc->sc_if.if_opackets++; 841 } 842 843 /* 844 * We now have characters in the output queue, 845 * kick the serial port. 846 */ 847 (*tp->t_oproc)(tp); 848 splx(s); 849 } 850 851 /* 852 * Input processing loop. 853 */ 854 for (;;) { 855 s = spltty(); 856 IF_DEQUEUE(&sc->sc_inq, m); 857 splx(s); 858 if (m == NULL) 859 break; 860 pktstart = mtod(m, u_char *); 861 len = m->m_pkthdr.len; 862 if (sc->sc_if.if_bpf) { 863 /* 864 * Save the compressed header, so we 865 * can tack it on later. Note that we 866 * will end up copying garbage in some 867 * cases but this is okay. We remember 868 * where the buffer started so we can 869 * compute the new header length. 870 */ 871 memcpy(chdr, pktstart, CHDR_LEN); 872 } 873 #ifdef INET 874 if ((c = (*pktstart & 0xf0)) != (IPVERSION << 4)) { 875 if (c & 0x80) 876 c = TYPE_COMPRESSED_TCP; 877 else if (c == TYPE_UNCOMPRESSED_TCP) 878 *pktstart &= 0x4f; /* XXX */ 879 /* 880 * We've got something that's not an IP 881 * packet. If compression is enabled, 882 * try to decompress it. Otherwise, if 883 * `auto-enable' compression is on and 884 * it's a reasonable packet, decompress 885 * it and then enable compression. 886 * Otherwise, drop it. 887 */ 888 if (sc->sc_if.if_flags & SC_COMPRESS) { 889 len = sl_uncompress_tcp(&pktstart, len, 890 (u_int)c, &sc->sc_comp); 891 if (len <= 0) { 892 m_freem(m); 893 continue; 894 } 895 } else if ((sc->sc_if.if_flags & SC_AUTOCOMP) && 896 c == TYPE_UNCOMPRESSED_TCP && len >= 40) { 897 len = sl_uncompress_tcp(&pktstart, len, 898 (u_int)c, &sc->sc_comp); 899 if (len <= 0) { 900 m_freem(m); 901 continue; 902 } 903 sc->sc_if.if_flags |= SC_COMPRESS; 904 } else { 905 m_freem(m); 906 continue; 907 } 908 } 909 #endif 910 m->m_data = (void *) pktstart; 911 m->m_pkthdr.len = m->m_len = len; 912 if (sc->sc_if.if_bpf) { 913 bpf_mtap_sl_in(&sc->sc_if, chdr, &m); 914 if (m == NULL) 915 continue; 916 } 917 /* 918 * If the packet will fit into a single 919 * header mbuf, copy it into one, to save 920 * memory. 921 */ 922 if (m->m_pkthdr.len < MHLEN) { 923 struct mbuf *n; 924 int pktlen; 925 926 MGETHDR(n, M_DONTWAIT, MT_DATA); 927 pktlen = m->m_pkthdr.len; 928 M_MOVE_PKTHDR(n, m); 929 memcpy(mtod(n, void *), mtod(m, void *), pktlen); 930 n->m_len = m->m_len; 931 m_freem(m); 932 m = n; 933 } 934 935 sc->sc_if.if_ipackets++; 936 getbinuptime(&sc->sc_lastpacket); 937 938 #ifdef INET 939 s = splnet(); 940 if (__predict_false(!pktq_enqueue(ip_pktq, m, 0))) { 941 sc->sc_if.if_ierrors++; 942 sc->sc_if.if_iqdrops++; 943 m_freem(m); 944 } 945 splx(s); 946 #endif 947 } 948 mutex_exit(softnet_lock); 949 } 950 951 /* 952 * Process an ioctl request. 953 */ 954 static int 955 slioctl(struct ifnet *ifp, u_long cmd, void *data) 956 { 957 struct ifaddr *ifa = (struct ifaddr *)data; 958 struct ifreq *ifr = (struct ifreq *)data; 959 int s = splnet(), error = 0; 960 struct sl_softc *sc = ifp->if_softc; 961 struct ppp_stats *psp; 962 struct ppp_comp_stats *pcp; 963 964 switch (cmd) { 965 966 case SIOCINITIFADDR: 967 if (ifa->ifa_addr->sa_family == AF_INET) 968 ifp->if_flags |= IFF_UP; 969 else 970 error = EAFNOSUPPORT; 971 break; 972 973 case SIOCSIFDSTADDR: 974 if (ifa->ifa_addr->sa_family != AF_INET) 975 error = EAFNOSUPPORT; 976 break; 977 978 case SIOCSIFMTU: 979 if ((ifr->ifr_mtu < 3) || (ifr->ifr_mtu > SLMAX)) { 980 error = EINVAL; 981 break; 982 } 983 /*FALLTHROUGH*/ 984 case SIOCGIFMTU: 985 if ((error = ifioctl_common(&sc->sc_if, cmd, data)) == ENETRESET) 986 error = 0; 987 break; 988 989 case SIOCADDMULTI: 990 case SIOCDELMULTI: 991 if (ifr == 0) { 992 error = EAFNOSUPPORT; /* XXX */ 993 break; 994 } 995 switch (ifreq_getaddr(cmd, ifr)->sa_family) { 996 997 #ifdef INET 998 case AF_INET: 999 break; 1000 #endif 1001 1002 default: 1003 error = EAFNOSUPPORT; 1004 break; 1005 } 1006 break; 1007 1008 case SIOCGPPPSTATS: 1009 psp = &((struct ifpppstatsreq *) data)->stats; 1010 (void)memset(psp, 0, sizeof(*psp)); 1011 psp->p.ppp_ibytes = sc->sc_if.if_ibytes; 1012 psp->p.ppp_ipackets = sc->sc_if.if_ipackets; 1013 psp->p.ppp_ierrors = sc->sc_if.if_ierrors; 1014 psp->p.ppp_obytes = sc->sc_if.if_obytes; 1015 psp->p.ppp_opackets = sc->sc_if.if_opackets; 1016 psp->p.ppp_oerrors = sc->sc_if.if_oerrors; 1017 #ifdef INET 1018 psp->vj.vjs_packets = sc->sc_comp.sls_packets; 1019 psp->vj.vjs_compressed = sc->sc_comp.sls_compressed; 1020 psp->vj.vjs_searches = sc->sc_comp.sls_searches; 1021 psp->vj.vjs_misses = sc->sc_comp.sls_misses; 1022 psp->vj.vjs_uncompressedin = sc->sc_comp.sls_uncompressedin; 1023 psp->vj.vjs_compressedin = sc->sc_comp.sls_compressedin; 1024 psp->vj.vjs_errorin = sc->sc_comp.sls_errorin; 1025 psp->vj.vjs_tossed = sc->sc_comp.sls_tossed; 1026 #endif 1027 break; 1028 1029 case SIOCGPPPCSTATS: 1030 pcp = &((struct ifpppcstatsreq *) data)->stats; 1031 (void)memset(pcp, 0, sizeof(*pcp)); 1032 break; 1033 1034 default: 1035 error = ifioctl_common(ifp, cmd, data); 1036 break; 1037 } 1038 splx(s); 1039 return error; 1040 } 1041