1 /* 2 * Copyright (c) 1987, 1989, 1992, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)if_sl.c 8.6 (Berkeley) 2/1/94 34 * $FreeBSD: src/sys/net/if_sl.c,v 1.84.2.2 2002/02/13 00:43:10 dillon Exp $ 35 * $DragonFly: src/sys/net/sl/if_sl.c,v 1.15 2004/09/16 04:49:32 dillon Exp $ 36 */ 37 38 /* 39 * Serial Line interface 40 * 41 * Rick Adams 42 * Center for Seismic Studies 43 * 1300 N 17th Street, Suite 1450 44 * Arlington, Virginia 22209 45 * (703)276-7900 46 * rick@seismo.ARPA 47 * seismo!rick 48 * 49 * Pounded on heavily by Chris Torek (chris@mimsy.umd.edu, umcp-cs!chris). 50 * N.B.: this belongs in netinet, not net, the way it stands now. 51 * Should have a link-layer type designation, but wouldn't be 52 * backwards-compatible. 53 * 54 * Converted to 4.3BSD Beta by Chris Torek. 55 * Other changes made at Berkeley, based in part on code by Kirk Smith. 56 * W. Jolitz added slip abort. 57 * 58 * Hacked almost beyond recognition by Van Jacobson (van@helios.ee.lbl.gov). 59 * Added priority queuing for "interactive" traffic; hooks for TCP 60 * header compression; ICMP filtering (at 2400 baud, some cretin 61 * pinging you can use up all your bandwidth). Made low clist behavior 62 * more robust and slightly less likely to hang serial line. 63 * Sped up a bunch of things. 64 * 65 * Note that splimp() is used throughout to block both (tty) input 66 * interrupts and network activity; thus, splimp must be >= spltty. 67 */ 68 69 #include "use_sl.h" 70 71 #include "opt_inet.h" 72 #if !defined(KLD_MODULE) 73 #include "opt_slip.h" 74 #endif 75 #include <sys/param.h> 76 #include <sys/systm.h> 77 #include <sys/proc.h> 78 #include <sys/malloc.h> 79 #include <sys/mbuf.h> 80 #include <sys/dkstat.h> 81 #include <sys/socket.h> 82 #include <sys/sockio.h> 83 #include <sys/fcntl.h> 84 #include <sys/signalvar.h> 85 #include <sys/tty.h> 86 #include <sys/clist.h> 87 #include <sys/kernel.h> 88 #include <sys/conf.h> 89 90 #include <net/if.h> 91 #include <net/if_types.h> 92 #include <net/netisr.h> 93 94 #if INET 95 #include <netinet/in.h> 96 #include <netinet/in_systm.h> 97 #include <netinet/in_var.h> 98 #include <netinet/ip.h> 99 #else 100 #error "Huh? Slip without inet?" 101 #endif 102 103 #include <net/slcompress.h> 104 #include "if_slvar.h" 105 #include <net/slip.h> 106 107 #include <net/bpf.h> 108 109 #ifdef __i386__ 110 #include <i386/isa/intr_machdep.h> 111 #endif 112 113 static void slattach (void *); 114 PSEUDO_SET(slattach, if_sl); 115 116 /* 117 * SLRMAX is a hard limit on input packet size. To simplify the code 118 * and improve performance, we require that packets fit in an mbuf 119 * cluster, and if we get a compressed packet, there's enough extra 120 * room to expand the header into a max length tcp/ip header (128 121 * bytes). So, SLRMAX can be at most 122 * MCLBYTES - 128 123 * 124 * SLMTU is the default transmit MTU. The transmit MTU should be kept 125 * small enough so that interactive use doesn't suffer, but large 126 * enough to provide good performance. 552 is a good choice for SLMTU 127 * because it is high enough to not fragment TCP packets being routed 128 * through this host. Packet fragmentation is bad with SLIP because 129 * fragment headers aren't compressed. The previous assumptions about 130 * the best MTU value don't really hold when using modern modems with 131 * BTLZ data compression because the modem buffers play a much larger 132 * role in interactive performance than the MTU. The MTU can be changed 133 * at any time to suit the specific environment with ifconfig(8), and 134 * its maximum value is defined as SLTMAX. SLTMAX must not be so large 135 * that it would overflow the stack if BPF is configured (XXX; if_ppp.c 136 * handles this better). 137 * 138 * SLIP_HIWAT is the amount of data that will be queued 'downstream' 139 * of us (i.e., in clists waiting to be picked up by the tty output 140 * interrupt). If we queue a lot of data downstream, it's immune to 141 * our t.o.s. queuing. 142 * E.g., if SLIP_HIWAT is 1024, the interactive traffic in mixed 143 * telnet/ftp will see a 1 sec wait, independent of the mtu (the 144 * wait is dependent on the ftp window size but that's typically 145 * 1k - 4k). So, we want SLIP_HIWAT just big enough to amortize 146 * the cost (in idle time on the wire) of the tty driver running 147 * off the end of its clists & having to call back slstart for a 148 * new packet. For a tty interface with any buffering at all, this 149 * cost will be zero. Even with a totally brain dead interface (like 150 * the one on a typical workstation), the cost will be <= 1 character 151 * time. So, setting SLIP_HIWAT to ~100 guarantees that we'll lose 152 * at most 1% while maintaining good interactive response. 153 */ 154 #define BUFOFFSET (128+sizeof(struct ifnet **)+SLIP_HDRLEN) 155 #define SLRMAX (MCLBYTES - BUFOFFSET) 156 #define SLBUFSIZE (SLRMAX + BUFOFFSET) 157 #ifndef SLMTU 158 #define SLMTU 552 /* default MTU */ 159 #endif 160 #define SLTMAX 1500 /* maximum MTU */ 161 #define SLIP_HIWAT roundup(50,CBSIZE) 162 #define CLISTRESERVE 1024 /* Can't let clists get too low */ 163 164 /* 165 * SLIP ABORT ESCAPE MECHANISM: 166 * (inspired by HAYES modem escape arrangement) 167 * 1sec escape 1sec escape 1sec escape { 1sec escape 1sec escape } 168 * within window time signals a "soft" exit from slip mode by remote end 169 * if the IFF_DEBUG flag is on. 170 */ 171 #define ABT_ESC '\033' /* can't be t_intr - distant host must know it*/ 172 #define ABT_IDLE 1 /* in seconds - idle before an escape */ 173 #define ABT_COUNT 3 /* count of escapes for abort */ 174 #define ABT_WINDOW (ABT_COUNT*2+2) /* in seconds - time to count */ 175 176 static struct sl_softc sl_softc[NSL]; 177 178 #define FRAME_END 0xc0 /* Frame End */ 179 #define FRAME_ESCAPE 0xdb /* Frame Esc */ 180 #define TRANS_FRAME_END 0xdc /* transposed frame end */ 181 #define TRANS_FRAME_ESCAPE 0xdd /* transposed frame esc */ 182 183 static int slinit (struct sl_softc *); 184 static struct mbuf *sl_btom (struct sl_softc *, int); 185 static timeout_t sl_keepalive; 186 static timeout_t sl_outfill; 187 static int slclose (struct tty *,int); 188 static int slinput (int, struct tty *); 189 static int slioctl (struct ifnet *, u_long, caddr_t, struct ucred *); 190 static int sltioctl (struct tty *, u_long, caddr_t, int, struct thread *); 191 static int slopen (dev_t, struct tty *); 192 static int sloutput (struct ifnet *, 193 struct mbuf *, struct sockaddr *, struct rtentry *); 194 static int slstart (struct tty *); 195 196 static struct linesw slipdisc = { 197 slopen, slclose, l_noread, l_nowrite, 198 sltioctl, slinput, slstart, ttymodem, 199 FRAME_END 200 }; 201 202 /* 203 * Called from boot code to establish sl interfaces. 204 */ 205 static void 206 slattach(dummy) 207 void *dummy; 208 { 209 struct sl_softc *sc; 210 int i = 0; 211 212 linesw[SLIPDISC] = slipdisc; 213 214 for (sc = sl_softc; i < NSL; sc++) { 215 if_initname(&(sc->sc_if), "sl", i++); 216 sc->sc_if.if_mtu = SLMTU; 217 sc->sc_if.if_flags = 218 #ifdef SLIP_IFF_OPTS 219 SLIP_IFF_OPTS; 220 #else 221 IFF_POINTOPOINT | SC_AUTOCOMP | IFF_MULTICAST; 222 #endif 223 sc->sc_if.if_type = IFT_SLIP; 224 sc->sc_if.if_ioctl = slioctl; 225 sc->sc_if.if_output = sloutput; 226 sc->sc_if.if_snd.ifq_maxlen = 50; 227 sc->sc_fastq.ifq_maxlen = 32; 228 sc->sc_if.if_linkmib = sc; 229 sc->sc_if.if_linkmiblen = sizeof *sc; 230 callout_init(&sc->sc_oftimeout); 231 callout_init(&sc->sc_katimeout); 232 if_attach(&sc->sc_if); 233 bpfattach(&sc->sc_if, DLT_SLIP, SLIP_HDRLEN); 234 } 235 } 236 237 static int 238 slinit(sc) 239 struct sl_softc *sc; 240 { 241 #ifdef __i386__ 242 int s; 243 244 s = splhigh(); 245 tty_imask |= net_imask; 246 net_imask = tty_imask; 247 update_intr_masks(); 248 splx(s); 249 if (bootverbose) 250 printf("new imasks: bio %x, tty %x, net %x\n", 251 bio_imask, tty_imask, net_imask); 252 #endif 253 if (sc->sc_ep == NULL) 254 sc->sc_ep = malloc(SLBUFSIZE, M_DEVBUF, M_WAITOK); 255 sc->sc_buf = sc->sc_ep + SLBUFSIZE - SLRMAX; 256 sc->sc_mp = sc->sc_buf; 257 sl_compress_init(&sc->sc_comp, -1); 258 return (1); 259 } 260 261 /* 262 * Line specific open routine. 263 * Attach the given tty to the first available sl unit. 264 */ 265 /* ARGSUSED */ 266 static int 267 slopen(dev_t dev, struct tty *tp) 268 { 269 struct sl_softc *sc; 270 int nsl; 271 int s, error; 272 struct thread *td = curthread; /* XXX */ 273 274 error = suser(td); 275 if (error) 276 return (error); 277 278 if (tp->t_line == SLIPDISC) 279 return (0); 280 281 for (nsl = NSL, sc = sl_softc; --nsl >= 0; sc++) 282 if (sc->sc_ttyp == NULL && !(sc->sc_flags & SC_STATIC)) { 283 if (slinit(sc) == 0) 284 return (ENOBUFS); 285 tp->t_sc = (caddr_t)sc; 286 sc->sc_ttyp = tp; 287 sc->sc_if.if_baudrate = tp->t_ospeed; 288 ttyflush(tp, FREAD | FWRITE); 289 290 tp->t_line = SLIPDISC; 291 /* 292 * We don't use t_canq or t_rawq, so reduce their 293 * cblock resources to 0. Reserve enough cblocks 294 * for t_outq to guarantee that we can fit a full 295 * packet if the SLIP_HIWAT check allows slstart() 296 * to loop. Use the same value for the cblock 297 * limit since the reserved blocks should always 298 * be enough. Reserving cblocks probably makes 299 * the CLISTRESERVE check unnecessary and wasteful. 300 */ 301 clist_alloc_cblocks(&tp->t_canq, 0, 0); 302 clist_alloc_cblocks(&tp->t_outq, 303 SLIP_HIWAT + 2 * sc->sc_if.if_mtu + 1, 304 SLIP_HIWAT + 2 * sc->sc_if.if_mtu + 1); 305 clist_alloc_cblocks(&tp->t_rawq, 0, 0); 306 307 s = splnet(); 308 if_up(&sc->sc_if); 309 splx(s); 310 return (0); 311 } 312 return (ENXIO); 313 } 314 315 /* 316 * Line specific close routine. 317 * Detach the tty from the sl unit. 318 */ 319 static int 320 slclose(tp,flag) 321 struct tty *tp; 322 int flag; 323 { 324 struct sl_softc *sc; 325 int s; 326 327 ttyflush(tp, FREAD | FWRITE); 328 /* 329 * XXX the placement of the following spl is misleading. tty 330 * interrupts must be blocked across line discipline switches 331 * and throughout closes to avoid races. 332 */ 333 s = splimp(); /* actually, max(spltty, splnet) */ 334 clist_free_cblocks(&tp->t_outq); 335 tp->t_line = 0; 336 sc = (struct sl_softc *)tp->t_sc; 337 if (sc != NULL) { 338 if (sc->sc_outfill) { 339 sc->sc_outfill = 0; 340 callout_stop(&sc->sc_oftimeout); 341 } 342 if (sc->sc_keepalive) { 343 sc->sc_keepalive = 0; 344 callout_stop(&sc->sc_katimeout); 345 } 346 if_down(&sc->sc_if); 347 sc->sc_flags &= SC_STATIC; 348 sc->sc_ttyp = NULL; 349 tp->t_sc = NULL; 350 if (sc->sc_ep) { 351 free(sc->sc_ep, M_DEVBUF); 352 sc->sc_ep = NULL; 353 } 354 sc->sc_mp = 0; 355 sc->sc_buf = 0; 356 } 357 splx(s); 358 return 0; 359 } 360 361 /* 362 * Line specific (tty) ioctl routine. 363 * Provide a way to get the sl unit number. 364 */ 365 /* ARGSUSED */ 366 static int 367 sltioctl(struct tty *tp, u_long cmd, caddr_t data, int flag, struct thread *p) 368 { 369 struct sl_softc *sc = (struct sl_softc *)tp->t_sc, *nc, *tmpnc; 370 int s, nsl; 371 372 s = splimp(); 373 switch (cmd) { 374 case SLIOCGUNIT: 375 *(int *)data = sc->sc_if.if_dunit; 376 break; 377 378 case SLIOCSUNIT: 379 if (sc->sc_if.if_dunit != *(u_int *)data) { 380 for (nsl = NSL, nc = sl_softc; --nsl >= 0; nc++) { 381 if ( nc->sc_if.if_dunit == *(u_int *)data 382 && nc->sc_ttyp == NULL 383 ) { 384 tmpnc = malloc(sizeof *tmpnc, M_TEMP, 385 M_WAITOK); 386 *tmpnc = *nc; 387 *nc = *sc; 388 nc->sc_if = tmpnc->sc_if; 389 tmpnc->sc_if = sc->sc_if; 390 *sc = *tmpnc; 391 free(tmpnc, M_TEMP); 392 if (sc->sc_if.if_flags & IFF_UP) { 393 if_down(&sc->sc_if); 394 if (!(nc->sc_if.if_flags & IFF_UP)) 395 if_up(&nc->sc_if); 396 } else if (nc->sc_if.if_flags & IFF_UP) 397 if_down(&nc->sc_if); 398 sc->sc_flags &= ~SC_STATIC; 399 sc->sc_flags |= (nc->sc_flags & SC_STATIC); 400 tp->t_sc = sc = nc; 401 clist_alloc_cblocks(&tp->t_outq, 402 SLIP_HIWAT + 2 * sc->sc_if.if_mtu + 1, 403 SLIP_HIWAT + 2 * sc->sc_if.if_mtu + 1); 404 sl_compress_init(&sc->sc_comp, -1); 405 goto slfound; 406 } 407 } 408 splx(s); 409 return (ENXIO); 410 } 411 slfound: 412 sc->sc_flags |= SC_STATIC; 413 break; 414 415 case SLIOCSKEEPAL: 416 sc->sc_keepalive = *(u_int *)data * hz; 417 if (sc->sc_keepalive) { 418 sc->sc_flags |= SC_KEEPALIVE; 419 callout_reset(&sc->sc_katimeout, sc->sc_keepalive, 420 sl_keepalive, sc); 421 } else { 422 if ((sc->sc_flags & SC_KEEPALIVE) != 0) { 423 callout_stop(&sc->sc_katimeout); 424 sc->sc_flags &= ~SC_KEEPALIVE; 425 } 426 } 427 break; 428 429 case SLIOCGKEEPAL: 430 *(int *)data = sc->sc_keepalive / hz; 431 break; 432 433 case SLIOCSOUTFILL: 434 sc->sc_outfill = *(u_int *)data * hz; 435 if (sc->sc_outfill) { 436 sc->sc_flags |= SC_OUTWAIT; 437 callout_reset(&sc->sc_oftimeout, sc->sc_outfill, 438 sl_outfill, sc); 439 } else { 440 if ((sc->sc_flags & SC_OUTWAIT) != 0) { 441 callout_stop(&sc->sc_oftimeout); 442 sc->sc_flags &= ~SC_OUTWAIT; 443 } 444 } 445 break; 446 447 case SLIOCGOUTFILL: 448 *(int *)data = sc->sc_outfill / hz; 449 break; 450 451 default: 452 splx(s); 453 return (ENOIOCTL); 454 } 455 splx(s); 456 return (0); 457 } 458 459 /* 460 * Queue a packet. Start transmission if not active. 461 * Compression happens in slstart; if we do it here, IP TOS 462 * will cause us to not compress "background" packets, because 463 * ordering gets trashed. It can be done for all packets in slstart. 464 */ 465 static int 466 sloutput(ifp, m, dst, rtp) 467 struct ifnet *ifp; 468 struct mbuf *m; 469 struct sockaddr *dst; 470 struct rtentry *rtp; 471 { 472 struct sl_softc *sc = &sl_softc[ifp->if_dunit]; 473 struct ip *ip; 474 struct ifqueue *ifq; 475 int s; 476 477 /* 478 * `Cannot happen' (see slioctl). Someday we will extend 479 * the line protocol to support other address families. 480 */ 481 if (dst->sa_family != AF_INET) { 482 printf("%s: af%d not supported\n", sc->sc_if.if_xname, 483 dst->sa_family); 484 m_freem(m); 485 sc->sc_if.if_noproto++; 486 return (EAFNOSUPPORT); 487 } 488 489 if (sc->sc_ttyp == NULL || !(ifp->if_flags & IFF_UP)) { 490 m_freem(m); 491 return (ENETDOWN); 492 } 493 if ((sc->sc_ttyp->t_state & TS_CONNECTED) == 0) { 494 m_freem(m); 495 return (EHOSTUNREACH); 496 } 497 ifq = &sc->sc_if.if_snd; 498 ip = mtod(m, struct ip *); 499 if (sc->sc_if.if_flags & SC_NOICMP && ip->ip_p == IPPROTO_ICMP) { 500 m_freem(m); 501 return (ENETRESET); /* XXX ? */ 502 } 503 if (ip->ip_tos & IPTOS_LOWDELAY) 504 ifq = &sc->sc_fastq; 505 s = splimp(); 506 if (IF_QFULL(ifq)) { 507 IF_DROP(ifq); 508 m_freem(m); 509 splx(s); 510 sc->sc_if.if_oerrors++; 511 return (ENOBUFS); 512 } 513 IF_ENQUEUE(ifq, m); 514 if (sc->sc_ttyp->t_outq.c_cc == 0) 515 slstart(sc->sc_ttyp); 516 splx(s); 517 return (0); 518 } 519 520 /* 521 * Start output on interface. Get another datagram 522 * to send from the interface queue and map it to 523 * the interface before starting output. 524 */ 525 static int 526 slstart(tp) 527 struct tty *tp; 528 { 529 struct sl_softc *sc = (struct sl_softc *)tp->t_sc; 530 struct mbuf *m; 531 u_char *cp; 532 struct ip *ip; 533 int s; 534 u_char bpfbuf[SLTMAX + SLIP_HDRLEN]; 535 int len = 0; 536 537 for (;;) { 538 /* 539 * Call output process whether or not there is more in the 540 * output queue. We are being called in lieu of ttstart 541 * and must do what it would. 542 */ 543 (*tp->t_oproc)(tp); 544 545 if (tp->t_outq.c_cc != 0) { 546 if (sc != NULL) 547 sc->sc_flags &= ~SC_OUTWAIT; 548 if (tp->t_outq.c_cc > SLIP_HIWAT) 549 return 0; 550 } 551 552 /* 553 * This happens briefly when the line shuts down. 554 */ 555 if (sc == NULL) 556 return 0; 557 558 /* 559 * Get a packet and send it to the interface. 560 */ 561 s = splimp(); 562 IF_DEQUEUE(&sc->sc_fastq, m); 563 if (m) 564 sc->sc_if.if_omcasts++; /* XXX */ 565 else 566 IF_DEQUEUE(&sc->sc_if.if_snd, m); 567 splx(s); 568 if (m == NULL) 569 return 0; 570 571 /* 572 * We do the header compression here rather than in sloutput 573 * because the packets will be out of order if we are using TOS 574 * queueing, and the connection id compression will get 575 * munged when this happens. 576 */ 577 if (sc->sc_if.if_bpf) { 578 /* 579 * We need to save the TCP/IP header before it's 580 * compressed. To avoid complicated code, we just 581 * copy the entire packet into a stack buffer (since 582 * this is a serial line, packets should be short 583 * and/or the copy should be negligible cost compared 584 * to the packet transmission time). 585 */ 586 struct mbuf *m1 = m; 587 u_char *cp = bpfbuf + SLIP_HDRLEN; 588 589 len = 0; 590 do { 591 int mlen = m1->m_len; 592 593 bcopy(mtod(m1, caddr_t), cp, mlen); 594 cp += mlen; 595 len += mlen; 596 } while ((m1 = m1->m_next) != NULL); 597 } 598 ip = mtod(m, struct ip *); 599 if (ip->ip_v == IPVERSION && ip->ip_p == IPPROTO_TCP) { 600 if (sc->sc_if.if_flags & SC_COMPRESS) 601 *mtod(m, u_char *) |= sl_compress_tcp(m, ip, 602 &sc->sc_comp, 1); 603 } 604 if (sc->sc_if.if_bpf) { 605 /* 606 * Put the SLIP pseudo-"link header" in place. The 607 * compressed header is now at the beginning of the 608 * mbuf. 609 */ 610 bpfbuf[SLX_DIR] = SLIPDIR_OUT; 611 bcopy(mtod(m, caddr_t), &bpfbuf[SLX_CHDR], CHDR_LEN); 612 bpf_tap(&sc->sc_if, bpfbuf, len + SLIP_HDRLEN); 613 } 614 615 /* 616 * If system is getting low on clists, just flush our 617 * output queue (if the stuff was important, it'll get 618 * retransmitted). Note that SLTMAX is used instead of 619 * the current if_mtu setting because connections that 620 * have already been established still use the original 621 * (possibly larger) mss. 622 */ 623 if (cfreecount < CLISTRESERVE + SLTMAX) { 624 m_freem(m); 625 sc->sc_if.if_collisions++; 626 continue; 627 } 628 629 sc->sc_flags &= ~SC_OUTWAIT; 630 /* 631 * The extra FRAME_END will start up a new packet, and thus 632 * will flush any accumulated garbage. We do this whenever 633 * the line may have been idle for some time. 634 */ 635 if (tp->t_outq.c_cc == 0) { 636 ++sc->sc_if.if_obytes; 637 (void) putc(FRAME_END, &tp->t_outq); 638 } 639 640 while (m) { 641 u_char *ep; 642 643 cp = mtod(m, u_char *); ep = cp + m->m_len; 644 while (cp < ep) { 645 /* 646 * Find out how many bytes in the string we can 647 * handle without doing something special. 648 */ 649 u_char *bp = cp; 650 651 while (cp < ep) { 652 switch (*cp++) { 653 case FRAME_ESCAPE: 654 case FRAME_END: 655 --cp; 656 goto out; 657 } 658 } 659 out: 660 if (cp > bp) { 661 /* 662 * Put n characters at once 663 * into the tty output queue. 664 */ 665 if (b_to_q((char *)bp, cp - bp, 666 &tp->t_outq)) 667 break; 668 sc->sc_if.if_obytes += cp - bp; 669 } 670 /* 671 * If there are characters left in the mbuf, 672 * the first one must be special.. 673 * Put it out in a different form. 674 */ 675 if (cp < ep) { 676 if (putc(FRAME_ESCAPE, &tp->t_outq)) 677 break; 678 if (putc(*cp++ == FRAME_ESCAPE ? 679 TRANS_FRAME_ESCAPE : TRANS_FRAME_END, 680 &tp->t_outq)) { 681 (void) unputc(&tp->t_outq); 682 break; 683 } 684 sc->sc_if.if_obytes += 2; 685 } 686 } 687 m = m_free(m); 688 } 689 690 if (putc(FRAME_END, &tp->t_outq)) { 691 /* 692 * Not enough room. Remove a char to make room 693 * and end the packet normally. 694 * If you get many collisions (more than one or two 695 * a day) you probably do not have enough clists 696 * and you should increase "nclist" in param.c. 697 */ 698 (void) unputc(&tp->t_outq); 699 (void) putc(FRAME_END, &tp->t_outq); 700 sc->sc_if.if_collisions++; 701 } else { 702 ++sc->sc_if.if_obytes; 703 sc->sc_if.if_opackets++; 704 } 705 } 706 return 0; 707 } 708 709 /* 710 * Copy data buffer to mbuf chain; add ifnet pointer. 711 */ 712 static struct mbuf * 713 sl_btom(sc, len) 714 struct sl_softc *sc; 715 int len; 716 { 717 struct mbuf *m; 718 719 if (len >= MCLBYTES) 720 return (NULL); 721 722 MGETHDR(m, MB_DONTWAIT, MT_DATA); 723 if (m == NULL) 724 return (NULL); 725 726 /* 727 * If we have more than MHLEN bytes, it's cheaper to 728 * queue the cluster we just filled & allocate a new one 729 * for the input buffer. Otherwise, fill the mbuf we 730 * allocated above. Note that code in the input routine 731 * guarantees that packet will fit in a cluster. 732 */ 733 if (len >= MHLEN) { 734 MCLGET(m, MB_DONTWAIT); 735 if ((m->m_flags & M_EXT) == 0) { 736 /* 737 * we couldn't get a cluster - if memory's this 738 * low, it's time to start dropping packets. 739 */ 740 m_free(m); 741 return (NULL); 742 } 743 } 744 bcopy((caddr_t)sc->sc_buf, mtod(m, caddr_t), len); 745 m->m_len = len; 746 m->m_pkthdr.len = len; 747 m->m_pkthdr.rcvif = &sc->sc_if; 748 return (m); 749 } 750 751 /* 752 * tty interface receiver interrupt. 753 */ 754 static int 755 slinput(c, tp) 756 int c; 757 struct tty *tp; 758 { 759 struct sl_softc *sc; 760 struct mbuf *m; 761 int len; 762 u_char chdr[CHDR_LEN]; 763 764 tk_nin++; 765 sc = (struct sl_softc *)tp->t_sc; 766 if (sc == NULL) 767 return 0; 768 if (c & TTY_ERRORMASK || (tp->t_state & TS_CONNECTED) == 0) { 769 sc->sc_flags |= SC_ERROR; 770 return 0; 771 } 772 c &= TTY_CHARMASK; 773 774 ++sc->sc_if.if_ibytes; 775 776 if (sc->sc_if.if_flags & IFF_DEBUG) { 777 if (c == ABT_ESC) { 778 /* 779 * If we have a previous abort, see whether 780 * this one is within the time limit. 781 */ 782 if (sc->sc_abortcount && 783 time_second >= sc->sc_starttime + ABT_WINDOW) 784 sc->sc_abortcount = 0; 785 /* 786 * If we see an abort after "idle" time, count it; 787 * record when the first abort escape arrived. 788 */ 789 if (time_second >= sc->sc_lasttime + ABT_IDLE) { 790 if (++sc->sc_abortcount == 1) 791 sc->sc_starttime = time_second; 792 if (sc->sc_abortcount >= ABT_COUNT) { 793 slclose(tp,0); 794 return 0; 795 } 796 } 797 } else 798 sc->sc_abortcount = 0; 799 sc->sc_lasttime = time_second; 800 } 801 802 switch (c) { 803 804 case TRANS_FRAME_ESCAPE: 805 if (sc->sc_escape) 806 c = FRAME_ESCAPE; 807 break; 808 809 case TRANS_FRAME_END: 810 if (sc->sc_escape) 811 c = FRAME_END; 812 break; 813 814 case FRAME_ESCAPE: 815 sc->sc_escape = 1; 816 return 0; 817 818 case FRAME_END: 819 sc->sc_flags &= ~SC_KEEPALIVE; 820 if(sc->sc_flags & SC_ERROR) { 821 sc->sc_flags &= ~SC_ERROR; 822 goto newpack; 823 } 824 len = sc->sc_mp - sc->sc_buf; 825 if (len < 3) 826 /* less than min length packet - ignore */ 827 goto newpack; 828 829 if (sc->sc_if.if_bpf) { 830 /* 831 * Save the compressed header, so we 832 * can tack it on later. Note that we 833 * will end up copying garbage in some 834 * cases but this is okay. We remember 835 * where the buffer started so we can 836 * compute the new header length. 837 */ 838 bcopy(sc->sc_buf, chdr, CHDR_LEN); 839 } 840 841 if ((c = (*sc->sc_buf & 0xf0)) != (IPVERSION << 4)) { 842 if (c & 0x80) 843 c = TYPE_COMPRESSED_TCP; 844 else if (c == TYPE_UNCOMPRESSED_TCP) 845 *sc->sc_buf &= 0x4f; /* XXX */ 846 /* 847 * We've got something that's not an IP packet. 848 * If compression is enabled, try to decompress it. 849 * Otherwise, if `auto-enable' compression is on and 850 * it's a reasonable packet, decompress it and then 851 * enable compression. Otherwise, drop it. 852 */ 853 if (sc->sc_if.if_flags & SC_COMPRESS) { 854 len = sl_uncompress_tcp(&sc->sc_buf, len, 855 (u_int)c, &sc->sc_comp); 856 if (len <= 0) 857 goto error; 858 } else if ((sc->sc_if.if_flags & SC_AUTOCOMP) && 859 c == TYPE_UNCOMPRESSED_TCP && len >= 40) { 860 len = sl_uncompress_tcp(&sc->sc_buf, len, 861 (u_int)c, &sc->sc_comp); 862 if (len <= 0) 863 goto error; 864 sc->sc_if.if_flags |= SC_COMPRESS; 865 } else 866 goto error; 867 } 868 if (sc->sc_if.if_bpf) { 869 /* 870 * Put the SLIP pseudo-"link header" in place. 871 * We couldn't do this any earlier since 872 * decompression probably moved the buffer 873 * pointer. Then, invoke BPF. 874 */ 875 u_char *hp = sc->sc_buf - SLIP_HDRLEN; 876 877 hp[SLX_DIR] = SLIPDIR_IN; 878 bcopy(chdr, &hp[SLX_CHDR], CHDR_LEN); 879 bpf_tap(&sc->sc_if, hp, len + SLIP_HDRLEN); 880 } 881 m = sl_btom(sc, len); 882 if (m == NULL) 883 goto error; 884 885 sc->sc_if.if_ipackets++; 886 887 if ((sc->sc_if.if_flags & IFF_UP) == 0) { 888 m_freem(m); 889 goto newpack; 890 } 891 892 if (netisr_queue(NETISR_IP, m)) { 893 sc->sc_if.if_ierrors++; 894 sc->sc_if.if_iqdrops++; 895 } 896 897 goto newpack; 898 } 899 if (sc->sc_mp < sc->sc_ep + SLBUFSIZE) { 900 *sc->sc_mp++ = c; 901 sc->sc_escape = 0; 902 return 0; 903 } 904 905 /* can't put lower; would miss an extra frame */ 906 sc->sc_flags |= SC_ERROR; 907 908 error: 909 sc->sc_if.if_ierrors++; 910 newpack: 911 sc->sc_mp = sc->sc_buf = sc->sc_ep + SLBUFSIZE - SLRMAX; 912 sc->sc_escape = 0; 913 return 0; 914 } 915 916 /* 917 * Process an ioctl request. 918 */ 919 static int 920 slioctl(ifp, cmd, data, cr) 921 struct ifnet *ifp; 922 u_long cmd; 923 caddr_t data; 924 struct ucred *cr; 925 { 926 struct ifaddr *ifa = (struct ifaddr *)data; 927 struct ifreq *ifr = (struct ifreq *)data; 928 int s, error = 0; 929 930 s = splimp(); 931 932 switch (cmd) { 933 934 case SIOCSIFFLAGS: 935 /* 936 * if.c will set the interface up even if we 937 * don't want it to. 938 */ 939 if (sl_softc[ifp->if_dunit].sc_ttyp == NULL) { 940 ifp->if_flags &= ~IFF_UP; 941 } 942 break; 943 case SIOCSIFADDR: 944 /* 945 * This is "historical" - set the interface up when 946 * setting the address. 947 */ 948 if (ifa->ifa_addr->sa_family == AF_INET) { 949 if (sl_softc[ifp->if_dunit].sc_ttyp != NULL) 950 ifp->if_flags |= IFF_UP; 951 } else { 952 error = EAFNOSUPPORT; 953 } 954 break; 955 956 case SIOCSIFDSTADDR: 957 if (ifa->ifa_addr->sa_family != AF_INET) 958 error = EAFNOSUPPORT; 959 break; 960 961 case SIOCADDMULTI: 962 case SIOCDELMULTI: 963 break; 964 965 case SIOCSIFMTU: 966 /* 967 * Set the interface MTU. 968 */ 969 if (ifr->ifr_mtu > SLTMAX) 970 error = EINVAL; 971 else { 972 struct tty *tp; 973 974 ifp->if_mtu = ifr->ifr_mtu; 975 tp = sl_softc[ifp->if_dunit].sc_ttyp; 976 if (tp != NULL) 977 clist_alloc_cblocks(&tp->t_outq, 978 SLIP_HIWAT + 2 * ifp->if_mtu + 1, 979 SLIP_HIWAT + 2 * ifp->if_mtu + 1); 980 } 981 break; 982 983 default: 984 error = EINVAL; 985 } 986 splx(s); 987 return (error); 988 } 989 990 static void 991 sl_keepalive(chan) 992 void *chan; 993 { 994 struct sl_softc *sc = chan; 995 996 if (sc->sc_keepalive) { 997 if (sc->sc_flags & SC_KEEPALIVE) 998 pgsignal (sc->sc_ttyp->t_pgrp, SIGURG, 1); 999 else 1000 sc->sc_flags |= SC_KEEPALIVE; 1001 callout_reset(&sc->sc_katimeout, sc->sc_keepalive, 1002 sl_keepalive, sc); 1003 } else { 1004 sc->sc_flags &= ~SC_KEEPALIVE; 1005 } 1006 } 1007 1008 static void 1009 sl_outfill(chan) 1010 void *chan; 1011 { 1012 struct sl_softc *sc = chan; 1013 struct tty *tp = sc->sc_ttyp; 1014 int s; 1015 1016 if (sc->sc_outfill && tp != NULL) { 1017 if (sc->sc_flags & SC_OUTWAIT) { 1018 s = splimp (); 1019 ++sc->sc_if.if_obytes; 1020 (void) putc(FRAME_END, &tp->t_outq); 1021 (*tp->t_oproc)(tp); 1022 splx (s); 1023 } else 1024 sc->sc_flags |= SC_OUTWAIT; 1025 callout_reset(&sc->sc_oftimeout, sc->sc_outfill, 1026 sl_outfill, sc); 1027 } else { 1028 sc->sc_flags &= ~SC_OUTWAIT; 1029 } 1030 } 1031