1 /* 2 * Copyright (c) 1987, 1989, 1992, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 * 7 * @(#)if_sl.c 8.2 (Berkeley) 09/21/93 8 */ 9 10 /* 11 * Serial Line interface 12 * 13 * Rick Adams 14 * Center for Seismic Studies 15 * 1300 N 17th Street, Suite 1450 16 * Arlington, Virginia 22209 17 * (703)276-7900 18 * rick@seismo.ARPA 19 * seismo!rick 20 * 21 * Pounded on heavily by Chris Torek (chris@mimsy.umd.edu, umcp-cs!chris). 22 * N.B.: this belongs in netinet, not net, the way it stands now. 23 * Should have a link-layer type designation, but wouldn't be 24 * backwards-compatible. 25 * 26 * Converted to 4.3BSD Beta by Chris Torek. 27 * Other changes made at Berkeley, based in part on code by Kirk Smith. 28 * W. Jolitz added slip abort. 29 * 30 * Hacked almost beyond recognition by Van Jacobson (van@helios.ee.lbl.gov). 31 * Added priority queuing for "interactive" traffic; hooks for TCP 32 * header compression; ICMP filtering (at 2400 baud, some cretin 33 * pinging you can use up all your bandwidth). Made low clist behavior 34 * more robust and slightly less likely to hang serial line. 35 * Sped up a bunch of things. 36 * 37 * Note that splimp() is used throughout to block both (tty) input 38 * interrupts and network activity; thus, splimp must be >= spltty. 39 */ 40 41 #include "sl.h" 42 #if NSL > 0 43 44 #include <sys/param.h> 45 #include <sys/proc.h> 46 #include <sys/mbuf.h> 47 #include <sys/buf.h> 48 #include <sys/dkstat.h> 49 #include <sys/socket.h> 50 #include <sys/ioctl.h> 51 #include <sys/file.h> 52 #include <sys/tty.h> 53 #include <sys/kernel.h> 54 #include <sys/conf.h> 55 56 #include <machine/cpu.h> 57 58 #include <net/if.h> 59 #include <net/if_types.h> 60 #include <net/netisr.h> 61 #include <net/route.h> 62 63 #if INET 64 #include <netinet/in.h> 65 #include <netinet/in_systm.h> 66 #include <netinet/in_var.h> 67 #include <netinet/ip.h> 68 #else 69 Huh? Slip without inet? 70 #endif 71 72 #include <net/slcompress.h> 73 #include <net/if_slvar.h> 74 75 /* 76 * SLMAX is a hard limit on input packet size. To simplify the code 77 * and improve performance, we require that packets fit in an mbuf 78 * cluster, and if we get a compressed packet, there's enough extra 79 * room to expand the header into a max length tcp/ip header (128 80 * bytes). So, SLMAX can be at most 81 * MCLBYTES - 128 82 * 83 * SLMTU is a hard limit on output packet size. To insure good 84 * interactive response, SLMTU wants to be the smallest size that 85 * amortizes the header cost. (Remember that even with 86 * type-of-service queuing, we have to wait for any in-progress 87 * packet to finish. I.e., we wait, on the average, 1/2 * mtu / 88 * cps, where cps is the line speed in characters per second. 89 * E.g., 533ms wait for a 1024 byte MTU on a 9600 baud line. The 90 * average compressed header size is 6-8 bytes so any MTU > 90 91 * bytes will give us 90% of the line bandwidth. A 100ms wait is 92 * tolerable (500ms is not), so want an MTU around 296. (Since TCP 93 * will send 256 byte segments (to allow for 40 byte headers), the 94 * typical packet size on the wire will be around 260 bytes). In 95 * 4.3tahoe+ systems, we can set an MTU in a route so we do that & 96 * leave the interface MTU relatively high (so we don't IP fragment 97 * when acting as a gateway to someone using a stupid MTU). 98 * 99 * Similar considerations apply to SLIP_HIWAT: It's the amount of 100 * data that will be queued 'downstream' of us (i.e., in clists 101 * waiting to be picked up by the tty output interrupt). If we 102 * queue a lot of data downstream, it's immune to our t.o.s. queuing. 103 * E.g., if SLIP_HIWAT is 1024, the interactive traffic in mixed 104 * telnet/ftp will see a 1 sec wait, independent of the mtu (the 105 * wait is dependent on the ftp window size but that's typically 106 * 1k - 4k). So, we want SLIP_HIWAT just big enough to amortize 107 * the cost (in idle time on the wire) of the tty driver running 108 * off the end of its clists & having to call back slstart for a 109 * new packet. For a tty interface with any buffering at all, this 110 * cost will be zero. Even with a totally brain dead interface (like 111 * the one on a typical workstation), the cost will be <= 1 character 112 * time. So, setting SLIP_HIWAT to ~100 guarantees that we'll lose 113 * at most 1% while maintaining good interactive response. 114 */ 115 #define BUFOFFSET 128 116 #define SLMAX (MCLBYTES - BUFOFFSET) 117 #define SLBUFSIZE (SLMAX + BUFOFFSET) 118 #define SLMTU 296 119 #define SLIP_HIWAT roundup(50,CBSIZE) 120 #define CLISTRESERVE 1024 /* Can't let clists get too low */ 121 122 /* 123 * SLIP ABORT ESCAPE MECHANISM: 124 * (inspired by HAYES modem escape arrangement) 125 * 1sec escape 1sec escape 1sec escape { 1sec escape 1sec escape } 126 * within window time signals a "soft" exit from slip mode by remote end 127 * if the IFF_DEBUG flag is on. 128 */ 129 #define ABT_ESC '\033' /* can't be t_intr - distant host must know it*/ 130 #define ABT_IDLE 1 /* in seconds - idle before an escape */ 131 #define ABT_COUNT 3 /* count of escapes for abort */ 132 #define ABT_WINDOW (ABT_COUNT*2+2) /* in seconds - time to count */ 133 134 struct sl_softc sl_softc[NSL]; 135 136 #define FRAME_END 0xc0 /* Frame End */ 137 #define FRAME_ESCAPE 0xdb /* Frame Esc */ 138 #define TRANS_FRAME_END 0xdc /* transposed frame end */ 139 #define TRANS_FRAME_ESCAPE 0xdd /* transposed frame esc */ 140 141 extern struct timeval time; 142 143 static int slinit __P((struct sl_softc *)); 144 static struct mbuf *sl_btom __P((struct sl_softc *, int)); 145 146 /* 147 * Called from boot code to establish sl interfaces. 148 */ 149 void 150 slattach() 151 { 152 register struct sl_softc *sc; 153 register int i = 0; 154 155 for (sc = sl_softc; i < NSL; sc++) { 156 sc->sc_if.if_name = "sl"; 157 sc->sc_if.if_next = NULL; 158 sc->sc_if.if_unit = i++; 159 sc->sc_if.if_mtu = SLMTU; 160 sc->sc_if.if_flags = 161 IFF_POINTOPOINT | SC_AUTOCOMP | IFF_MULTICAST; 162 sc->sc_if.if_type = IFT_SLIP; 163 sc->sc_if.if_ioctl = slioctl; 164 sc->sc_if.if_output = sloutput; 165 sc->sc_if.if_snd.ifq_maxlen = 50; 166 sc->sc_fastq.ifq_maxlen = 32; 167 if_attach(&sc->sc_if); 168 } 169 } 170 171 static int 172 slinit(sc) 173 register struct sl_softc *sc; 174 { 175 register caddr_t p; 176 177 if (sc->sc_ep == (u_char *) 0) { 178 MCLALLOC(p, M_WAIT); 179 if (p) 180 sc->sc_ep = (u_char *)p + SLBUFSIZE; 181 else { 182 printf("sl%d: can't allocate buffer\n", sc - sl_softc); 183 sc->sc_if.if_flags &= ~IFF_UP; 184 return (0); 185 } 186 } 187 sc->sc_buf = sc->sc_ep - SLMAX; 188 sc->sc_mp = sc->sc_buf; 189 sl_compress_init(&sc->sc_comp); 190 return (1); 191 } 192 193 /* 194 * Line specific open routine. 195 * Attach the given tty to the first available sl unit. 196 */ 197 /* ARGSUSED */ 198 int 199 slopen(dev, tp) 200 dev_t dev; 201 register struct tty *tp; 202 { 203 struct proc *p = curproc; /* XXX */ 204 register struct sl_softc *sc; 205 register int nsl; 206 int error; 207 208 if (error = suser(p->p_ucred, &p->p_acflag)) 209 return (error); 210 211 if (tp->t_line == SLIPDISC) 212 return (0); 213 214 for (nsl = NSL, sc = sl_softc; --nsl >= 0; sc++) 215 if (sc->sc_ttyp == NULL) { 216 if (slinit(sc) == 0) 217 return (ENOBUFS); 218 tp->t_sc = (caddr_t)sc; 219 sc->sc_ttyp = tp; 220 sc->sc_if.if_baudrate = tp->t_ospeed; 221 ttyflush(tp, FREAD | FWRITE); 222 return (0); 223 } 224 return (ENXIO); 225 } 226 227 /* 228 * Line specific close routine. 229 * Detach the tty from the sl unit. 230 */ 231 void 232 slclose(tp) 233 struct tty *tp; 234 { 235 register struct sl_softc *sc; 236 int s; 237 238 ttywflush(tp); 239 s = splimp(); /* actually, max(spltty, splnet) */ 240 tp->t_line = 0; 241 sc = (struct sl_softc *)tp->t_sc; 242 if (sc != NULL) { 243 if_down(&sc->sc_if); 244 sc->sc_ttyp = NULL; 245 tp->t_sc = NULL; 246 MCLFREE((caddr_t)(sc->sc_ep - SLBUFSIZE)); 247 sc->sc_ep = 0; 248 sc->sc_mp = 0; 249 sc->sc_buf = 0; 250 } 251 splx(s); 252 } 253 254 /* 255 * Line specific (tty) ioctl routine. 256 * Provide a way to get the sl unit number. 257 */ 258 /* ARGSUSED */ 259 int 260 sltioctl(tp, cmd, data, flag) 261 struct tty *tp; 262 int cmd; 263 caddr_t data; 264 int flag; 265 { 266 struct sl_softc *sc = (struct sl_softc *)tp->t_sc; 267 int s; 268 269 switch (cmd) { 270 case SLIOCGUNIT: 271 *(int *)data = sc->sc_if.if_unit; 272 break; 273 274 default: 275 return (-1); 276 } 277 return (0); 278 } 279 280 /* 281 * Queue a packet. Start transmission if not active. 282 */ 283 int 284 sloutput(ifp, m, dst, rtp) 285 struct ifnet *ifp; 286 register struct mbuf *m; 287 struct sockaddr *dst; 288 struct rtentry *rtp; 289 { 290 register struct sl_softc *sc = &sl_softc[ifp->if_unit]; 291 register struct ip *ip; 292 register struct ifqueue *ifq; 293 register int p; 294 int s; 295 296 /* 297 * `Cannot happen' (see slioctl). Someday we will extend 298 * the line protocol to support other address families. 299 */ 300 if (dst->sa_family != AF_INET) { 301 printf("sl%d: af%d not supported\n", sc->sc_if.if_unit, 302 dst->sa_family); 303 m_freem(m); 304 sc->sc_if.if_noproto++; 305 return (EAFNOSUPPORT); 306 } 307 308 if (sc->sc_ttyp == NULL) { 309 m_freem(m); 310 return (ENETDOWN); /* sort of */ 311 } 312 if ((sc->sc_ttyp->t_state & TS_CARR_ON) == 0) { 313 m_freem(m); 314 return (EHOSTUNREACH); 315 } 316 ifq = &sc->sc_if.if_snd; 317 ip = mtod(m, struct ip *); 318 if (ip->ip_tos & IPTOS_LOWDELAY) { 319 ifq = &sc->sc_fastq; 320 p = 1; 321 } else 322 p = 0; 323 if (ip->ip_p == IPPROTO_TCP) { 324 if (sc->sc_if.if_flags & SC_COMPRESS) { 325 /* 326 * The last parameter turns off connection id 327 * compression for background traffic: Since 328 * fastq traffic can jump ahead of the background 329 * traffic, we don't know what order packets will 330 * go on the line. 331 */ 332 p = sl_compress_tcp(m, ip, &sc->sc_comp, p); 333 *mtod(m, u_char *) |= p; 334 } 335 } else if (sc->sc_if.if_flags & SC_NOICMP && ip->ip_p == IPPROTO_ICMP) { 336 m_freem(m); 337 return (ENETRESET); /* XXX ? */ 338 } 339 s = splimp(); 340 if (IF_QFULL(ifq)) { 341 IF_DROP(ifq); 342 m_freem(m); 343 splx(s); 344 sc->sc_if.if_oerrors++; 345 return (ENOBUFS); 346 } 347 IF_ENQUEUE(ifq, m); 348 sc->sc_if.if_lastchange = time; 349 if (sc->sc_ttyp->t_outq.c_cc == 0) 350 slstart(sc->sc_ttyp); 351 splx(s); 352 return (0); 353 } 354 355 /* 356 * Start output on interface. Get another datagram 357 * to send from the interface queue and map it to 358 * the interface before starting output. 359 */ 360 void 361 slstart(tp) 362 register struct tty *tp; 363 { 364 register struct sl_softc *sc = (struct sl_softc *)tp->t_sc; 365 register struct mbuf *m; 366 register u_char *cp; 367 int s; 368 struct mbuf *m2; 369 extern int cfreecount; 370 371 for (;;) { 372 /* 373 * If there is more in the output queue, just send it now. 374 * We are being called in lieu of ttstart and must do what 375 * it would. 376 */ 377 if (tp->t_outq.c_cc != 0) { 378 (*tp->t_oproc)(tp); 379 if (tp->t_outq.c_cc > SLIP_HIWAT) 380 return; 381 } 382 /* 383 * This happens briefly when the line shuts down. 384 */ 385 if (sc == NULL) 386 return; 387 388 /* 389 * Get a packet and send it to the interface. 390 */ 391 s = splimp(); 392 IF_DEQUEUE(&sc->sc_fastq, m); 393 if (m) 394 sc->sc_if.if_omcasts++; /* XXX */ 395 else 396 IF_DEQUEUE(&sc->sc_if.if_snd, m); 397 splx(s); 398 if (m == NULL) 399 return; 400 sc->sc_if.if_lastchange = time; 401 402 /* 403 * If system is getting low on clists, just flush our 404 * output queue (if the stuff was important, it'll get 405 * retransmitted). 406 */ 407 if (cfreecount < CLISTRESERVE + SLMTU) { 408 m_freem(m); 409 sc->sc_if.if_collisions++; 410 continue; 411 } 412 /* 413 * The extra FRAME_END will start up a new packet, and thus 414 * will flush any accumulated garbage. We do this whenever 415 * the line may have been idle for some time. 416 */ 417 if (tp->t_outq.c_cc == 0) { 418 ++sc->sc_if.if_obytes; 419 (void) putc(FRAME_END, &tp->t_outq); 420 } 421 422 while (m) { 423 register u_char *ep; 424 425 cp = mtod(m, u_char *); ep = cp + m->m_len; 426 while (cp < ep) { 427 /* 428 * Find out how many bytes in the string we can 429 * handle without doing something special. 430 */ 431 register u_char *bp = cp; 432 433 while (cp < ep) { 434 switch (*cp++) { 435 case FRAME_ESCAPE: 436 case FRAME_END: 437 --cp; 438 goto out; 439 } 440 } 441 out: 442 if (cp > bp) { 443 /* 444 * Put n characters at once 445 * into the tty output queue. 446 */ 447 if (b_to_q((char *)bp, cp - bp, &tp->t_outq)) 448 break; 449 sc->sc_if.if_obytes += cp - bp; 450 } 451 /* 452 * If there are characters left in the mbuf, 453 * the first one must be special.. 454 * Put it out in a different form. 455 */ 456 if (cp < ep) { 457 if (putc(FRAME_ESCAPE, &tp->t_outq)) 458 break; 459 if (putc(*cp++ == FRAME_ESCAPE ? 460 TRANS_FRAME_ESCAPE : TRANS_FRAME_END, 461 &tp->t_outq)) { 462 (void) unputc(&tp->t_outq); 463 break; 464 } 465 sc->sc_if.if_obytes += 2; 466 } 467 } 468 MFREE(m, m2); 469 m = m2; 470 } 471 472 if (putc(FRAME_END, &tp->t_outq)) { 473 /* 474 * Not enough room. Remove a char to make room 475 * and end the packet normally. 476 * If you get many collisions (more than one or two 477 * a day) you probably do not have enough clists 478 * and you should increase "nclist" in param.c. 479 */ 480 (void) unputc(&tp->t_outq); 481 (void) putc(FRAME_END, &tp->t_outq); 482 sc->sc_if.if_collisions++; 483 } else { 484 ++sc->sc_if.if_obytes; 485 sc->sc_if.if_opackets++; 486 } 487 } 488 } 489 490 /* 491 * Copy data buffer to mbuf chain; add ifnet pointer. 492 */ 493 static struct mbuf * 494 sl_btom(sc, len) 495 register struct sl_softc *sc; 496 register int len; 497 { 498 register struct mbuf *m; 499 500 MGETHDR(m, M_DONTWAIT, MT_DATA); 501 if (m == NULL) 502 return (NULL); 503 504 /* 505 * If we have more than MHLEN bytes, it's cheaper to 506 * queue the cluster we just filled & allocate a new one 507 * for the input buffer. Otherwise, fill the mbuf we 508 * allocated above. Note that code in the input routine 509 * guarantees that packet will fit in a cluster. 510 */ 511 if (len >= MHLEN) { 512 MCLGET(m, M_DONTWAIT); 513 if ((m->m_flags & M_EXT) == 0) { 514 /* 515 * we couldn't get a cluster - if memory's this 516 * low, it's time to start dropping packets. 517 */ 518 (void) m_free(m); 519 return (NULL); 520 } 521 sc->sc_ep = mtod(m, u_char *) + SLBUFSIZE; 522 m->m_data = (caddr_t)sc->sc_buf; 523 m->m_ext.ext_buf = (caddr_t)((int)sc->sc_buf &~ MCLOFSET); 524 } else 525 bcopy((caddr_t)sc->sc_buf, mtod(m, caddr_t), len); 526 527 m->m_len = len; 528 m->m_pkthdr.len = len; 529 m->m_pkthdr.rcvif = &sc->sc_if; 530 return (m); 531 } 532 533 /* 534 * tty interface receiver interrupt. 535 */ 536 void 537 slinput(c, tp) 538 register int c; 539 register struct tty *tp; 540 { 541 register struct sl_softc *sc; 542 register struct mbuf *m; 543 register int len; 544 int s; 545 546 tk_nin++; 547 sc = (struct sl_softc *)tp->t_sc; 548 if (sc == NULL) 549 return; 550 if (!(tp->t_state&TS_CARR_ON)) /* XXX */ 551 return; 552 553 ++sc->sc_if.if_ibytes; 554 c &= 0xff; /* XXX */ 555 556 #ifdef ABT_ESC 557 if (sc->sc_if.if_flags & IFF_DEBUG) { 558 if (c == ABT_ESC) { 559 /* 560 * If we have a previous abort, see whether 561 * this one is within the time limit. 562 */ 563 if (sc->sc_abortcount && 564 time.tv_sec >= sc->sc_starttime + ABT_WINDOW) 565 sc->sc_abortcount = 0; 566 /* 567 * If we see an abort after "idle" time, count it; 568 * record when the first abort escape arrived. 569 */ 570 if (time.tv_sec >= sc->sc_lasttime + ABT_IDLE) { 571 if (++sc->sc_abortcount == 1) 572 sc->sc_starttime = time.tv_sec; 573 if (sc->sc_abortcount >= ABT_COUNT) { 574 slclose(tp); 575 return; 576 } 577 } 578 } else 579 sc->sc_abortcount = 0; 580 sc->sc_lasttime = time.tv_sec; 581 } 582 #endif 583 584 switch (c) { 585 586 case TRANS_FRAME_ESCAPE: 587 if (sc->sc_escape) 588 c = FRAME_ESCAPE; 589 break; 590 591 case TRANS_FRAME_END: 592 if (sc->sc_escape) 593 c = FRAME_END; 594 break; 595 596 case FRAME_ESCAPE: 597 sc->sc_escape = 1; 598 return; 599 600 case FRAME_END: 601 len = sc->sc_mp - sc->sc_buf; 602 if (len < 3) 603 /* less than min length packet - ignore */ 604 goto newpack; 605 606 if ((c = (*sc->sc_buf & 0xf0)) != (IPVERSION << 4)) { 607 if (c & 0x80) 608 c = TYPE_COMPRESSED_TCP; 609 else if (c == TYPE_UNCOMPRESSED_TCP) 610 *sc->sc_buf &= 0x4f; /* XXX */ 611 /* 612 * We've got something that's not an IP packet. 613 * If compression is enabled, try to decompress it. 614 * Otherwise, if `auto-enable' compression is on and 615 * it's a reasonable packet, decompress it and then 616 * enable compression. Otherwise, drop it. 617 */ 618 if (sc->sc_if.if_flags & SC_COMPRESS) { 619 len = sl_uncompress_tcp(&sc->sc_buf, len, 620 (u_int)c, &sc->sc_comp); 621 if (len <= 0) 622 goto error; 623 } else if ((sc->sc_if.if_flags & SC_AUTOCOMP) && 624 c == TYPE_UNCOMPRESSED_TCP && len >= 40) { 625 len = sl_uncompress_tcp(&sc->sc_buf, len, 626 (u_int)c, &sc->sc_comp); 627 if (len <= 0) 628 goto error; 629 sc->sc_if.if_flags |= SC_COMPRESS; 630 } else 631 goto error; 632 } 633 m = sl_btom(sc, len); 634 if (m == NULL) 635 goto error; 636 637 sc->sc_if.if_ipackets++; 638 sc->sc_if.if_lastchange = time; 639 s = splimp(); 640 if (IF_QFULL(&ipintrq)) { 641 IF_DROP(&ipintrq); 642 sc->sc_if.if_ierrors++; 643 sc->sc_if.if_iqdrops++; 644 m_freem(m); 645 } else { 646 IF_ENQUEUE(&ipintrq, m); 647 schednetisr(NETISR_IP); 648 } 649 splx(s); 650 goto newpack; 651 } 652 if (sc->sc_mp < sc->sc_ep) { 653 *sc->sc_mp++ = c; 654 sc->sc_escape = 0; 655 return; 656 } 657 error: 658 sc->sc_if.if_ierrors++; 659 newpack: 660 sc->sc_mp = sc->sc_buf = sc->sc_ep - SLMAX; 661 sc->sc_escape = 0; 662 } 663 664 /* 665 * Process an ioctl request. 666 */ 667 int 668 slioctl(ifp, cmd, data) 669 register struct ifnet *ifp; 670 int cmd; 671 caddr_t data; 672 { 673 register struct ifaddr *ifa = (struct ifaddr *)data; 674 register struct ifreq *ifr; 675 register int s = splimp(), error = 0; 676 677 switch (cmd) { 678 679 case SIOCSIFADDR: 680 if (ifa->ifa_addr->sa_family == AF_INET) 681 ifp->if_flags |= IFF_UP; 682 else 683 error = EAFNOSUPPORT; 684 break; 685 686 case SIOCSIFDSTADDR: 687 if (ifa->ifa_addr->sa_family != AF_INET) 688 error = EAFNOSUPPORT; 689 break; 690 691 case SIOCADDMULTI: 692 case SIOCDELMULTI: 693 ifr = (struct ifreq *)data; 694 if (ifr == 0) { 695 error = EAFNOSUPPORT; /* XXX */ 696 break; 697 } 698 switch (ifr->ifr_addr.sa_family) { 699 700 #ifdef INET 701 case AF_INET: 702 break; 703 #endif 704 705 default: 706 error = EAFNOSUPPORT; 707 break; 708 } 709 break; 710 #endif 711 712 default: 713 error = EINVAL; 714 } 715 splx(s); 716 return (error); 717 } 718