1 /* 2 * Copyright (c) 1987, 1989, 1992 Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 * 7 * @(#)if_sl.c 7.31 (Berkeley) 02/25/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 #define t_sc T_LINEP 142 143 int sloutput(), slioctl(), ttrstrt(); 144 extern struct timeval time; 145 146 /* 147 * Called from boot code to establish sl interfaces. 148 */ 149 slattach() 150 { 151 register struct sl_softc *sc; 152 register int i = 0; 153 154 for (sc = sl_softc; i < NSL; sc++) { 155 sc->sc_if.if_name = "sl"; 156 sc->sc_if.if_next = NULL; 157 sc->sc_if.if_unit = i++; 158 sc->sc_if.if_mtu = SLMTU; 159 sc->sc_if.if_flags = 160 IFF_POINTOPOINT | SC_AUTOCOMP | IFF_MULTICAST; 161 sc->sc_if.if_type = IFT_SLIP; 162 sc->sc_if.if_ioctl = slioctl; 163 sc->sc_if.if_output = sloutput; 164 sc->sc_if.if_snd.ifq_maxlen = 50; 165 sc->sc_fastq.ifq_maxlen = 32; 166 if_attach(&sc->sc_if); 167 } 168 } 169 170 static int 171 slinit(sc) 172 register struct sl_softc *sc; 173 { 174 register caddr_t p; 175 176 if (sc->sc_ep == (u_char *) 0) { 177 MCLALLOC(p, M_WAIT); 178 if (p) 179 sc->sc_ep = (u_char *)p + SLBUFSIZE; 180 else { 181 printf("sl%d: can't allocate buffer\n", sc - sl_softc); 182 sc->sc_if.if_flags &= ~IFF_UP; 183 return (0); 184 } 185 } 186 sc->sc_buf = sc->sc_ep - SLMAX; 187 sc->sc_mp = sc->sc_buf; 188 sl_compress_init(&sc->sc_comp); 189 return (1); 190 } 191 192 /* 193 * Line specific open routine. 194 * Attach the given tty to the first available sl unit. 195 */ 196 /* ARGSUSED */ 197 slopen(dev, tp) 198 dev_t dev; 199 register struct tty *tp; 200 { 201 struct proc *p = curproc; /* XXX */ 202 register struct sl_softc *sc; 203 register int nsl; 204 int error; 205 206 if (error = suser(p->p_ucred, &p->p_acflag)) 207 return (error); 208 209 if (tp->t_line == SLIPDISC) 210 return (0); 211 212 for (nsl = NSL, sc = sl_softc; --nsl >= 0; sc++) 213 if (sc->sc_ttyp == NULL) { 214 if (slinit(sc) == 0) 215 return (ENOBUFS); 216 tp->t_sc = (caddr_t)sc; 217 sc->sc_ttyp = tp; 218 sc->sc_if.if_baudrate = tp->t_ospeed; 219 ttyflush(tp, FREAD | FWRITE); 220 return (0); 221 } 222 return (ENXIO); 223 } 224 225 /* 226 * Line specific close routine. 227 * Detach the tty from the sl unit. 228 * Mimics part of ttyclose(). 229 */ 230 slclose(tp) 231 struct tty *tp; 232 { 233 register struct sl_softc *sc; 234 int s; 235 236 ttywflush(tp); 237 s = splimp(); /* actually, max(spltty, splnet) */ 238 tp->t_line = 0; 239 sc = (struct sl_softc *)tp->t_sc; 240 if (sc != NULL) { 241 if_down(&sc->sc_if); 242 sc->sc_ttyp = NULL; 243 tp->t_sc = NULL; 244 MCLFREE((caddr_t)(sc->sc_ep - SLBUFSIZE)); 245 sc->sc_ep = 0; 246 sc->sc_mp = 0; 247 sc->sc_buf = 0; 248 } 249 splx(s); 250 } 251 252 /* 253 * Line specific (tty) ioctl routine. 254 * Provide a way to get the sl unit number. 255 */ 256 /* ARGSUSED */ 257 sltioctl(tp, cmd, data, flag) 258 struct tty *tp; 259 int cmd; 260 caddr_t data; 261 int flag; 262 { 263 struct sl_softc *sc = (struct sl_softc *)tp->t_sc; 264 int s; 265 266 switch (cmd) { 267 case SLIOCGUNIT: 268 *(int *)data = sc->sc_if.if_unit; 269 break; 270 271 default: 272 return (-1); 273 } 274 return (0); 275 } 276 277 /* 278 * Queue a packet. Start transmission if not active. 279 */ 280 sloutput(ifp, m, dst) 281 struct ifnet *ifp; 282 register struct mbuf *m; 283 struct sockaddr *dst; 284 { 285 register struct sl_softc *sc = &sl_softc[ifp->if_unit]; 286 register struct ip *ip; 287 register struct ifqueue *ifq; 288 register int p; 289 int s; 290 291 /* 292 * `Cannot happen' (see slioctl). Someday we will extend 293 * the line protocol to support other address families. 294 */ 295 if (dst->sa_family != AF_INET) { 296 printf("sl%d: af%d not supported\n", sc->sc_if.if_unit, 297 dst->sa_family); 298 m_freem(m); 299 sc->sc_if.if_noproto++; 300 return (EAFNOSUPPORT); 301 } 302 303 if (sc->sc_ttyp == NULL) { 304 m_freem(m); 305 return (ENETDOWN); /* sort of */ 306 } 307 if ((sc->sc_ttyp->t_state & TS_CARR_ON) == 0) { 308 m_freem(m); 309 return (EHOSTUNREACH); 310 } 311 ifq = &sc->sc_if.if_snd; 312 ip = mtod(m, struct ip *); 313 if (ip->ip_tos & IPTOS_LOWDELAY) { 314 ifq = &sc->sc_fastq; 315 p = 1; 316 } else 317 p = 0; 318 if (ip->ip_p == IPPROTO_TCP) { 319 if (sc->sc_if.if_flags & SC_COMPRESS) { 320 /* 321 * The last parameter turns off connection id 322 * compression for background traffic: Since 323 * fastq traffic can jump ahead of the background 324 * traffic, we don't know what order packets will 325 * go on the line. 326 */ 327 p = sl_compress_tcp(m, ip, &sc->sc_comp, p); 328 *mtod(m, u_char *) |= p; 329 } 330 } else if (sc->sc_if.if_flags & SC_NOICMP && ip->ip_p == IPPROTO_ICMP) { 331 m_freem(m); 332 return (ENETRESET); /* XXX ? */ 333 } 334 s = splimp(); 335 if (IF_QFULL(ifq)) { 336 IF_DROP(ifq); 337 m_freem(m); 338 splx(s); 339 sc->sc_if.if_oerrors++; 340 return (ENOBUFS); 341 } 342 IF_ENQUEUE(ifq, m); 343 sc->sc_if.if_lastchange = time; 344 if (sc->sc_ttyp->t_outq.c_cc == 0) 345 slstart(sc->sc_ttyp); 346 splx(s); 347 return (0); 348 } 349 350 /* 351 * Start output on interface. Get another datagram 352 * to send from the interface queue and map it to 353 * the interface before starting output. 354 */ 355 slstart(tp) 356 register struct tty *tp; 357 { 358 register struct sl_softc *sc = (struct sl_softc *)tp->t_sc; 359 register struct mbuf *m; 360 register u_char *cp; 361 int s; 362 struct mbuf *m2; 363 extern int cfreecount; 364 365 for (;;) { 366 /* 367 * If there is more in the output queue, just send it now. 368 * We are being called in lieu of ttstart and must do what 369 * it would. 370 */ 371 if (tp->t_outq.c_cc != 0) { 372 (*tp->t_oproc)(tp); 373 if (tp->t_outq.c_cc > SLIP_HIWAT) 374 return; 375 } 376 /* 377 * This happens briefly when the line shuts down. 378 */ 379 if (sc == NULL) 380 return; 381 382 /* 383 * Get a packet and send it to the interface. 384 */ 385 s = splimp(); 386 IF_DEQUEUE(&sc->sc_fastq, m); 387 if (m) 388 sc->sc_if.if_omcasts++; /* XXX */ 389 else 390 IF_DEQUEUE(&sc->sc_if.if_snd, m); 391 splx(s); 392 if (m == NULL) 393 return; 394 sc->sc_if.if_lastchange = time; 395 396 /* 397 * If system is getting low on clists, just flush our 398 * output queue (if the stuff was important, it'll get 399 * retransmitted). 400 */ 401 if (cfreecount < CLISTRESERVE + SLMTU) { 402 m_freem(m); 403 sc->sc_if.if_collisions++; 404 continue; 405 } 406 /* 407 * The extra FRAME_END will start up a new packet, and thus 408 * will flush any accumulated garbage. We do this whenever 409 * the line may have been idle for some time. 410 */ 411 if (tp->t_outq.c_cc == 0) { 412 ++sc->sc_if.if_obytes; 413 (void) putc(FRAME_END, &tp->t_outq); 414 } 415 416 while (m) { 417 register u_char *ep; 418 419 cp = mtod(m, u_char *); ep = cp + m->m_len; 420 while (cp < ep) { 421 /* 422 * Find out how many bytes in the string we can 423 * handle without doing something special. 424 */ 425 register u_char *bp = cp; 426 427 while (cp < ep) { 428 switch (*cp++) { 429 case FRAME_ESCAPE: 430 case FRAME_END: 431 --cp; 432 goto out; 433 } 434 } 435 out: 436 if (cp > bp) { 437 /* 438 * Put n characters at once 439 * into the tty output queue. 440 */ 441 if (b_to_q((char *)bp, cp - bp, &tp->t_outq)) 442 break; 443 sc->sc_if.if_obytes += cp - bp; 444 } 445 /* 446 * If there are characters left in the mbuf, 447 * the first one must be special.. 448 * Put it out in a different form. 449 */ 450 if (cp < ep) { 451 if (putc(FRAME_ESCAPE, &tp->t_outq)) 452 break; 453 if (putc(*cp++ == FRAME_ESCAPE ? 454 TRANS_FRAME_ESCAPE : TRANS_FRAME_END, 455 &tp->t_outq)) { 456 (void) unputc(&tp->t_outq); 457 break; 458 } 459 sc->sc_if.if_obytes += 2; 460 } 461 } 462 MFREE(m, m2); 463 m = m2; 464 } 465 466 if (putc(FRAME_END, &tp->t_outq)) { 467 /* 468 * Not enough room. Remove a char to make room 469 * and end the packet normally. 470 * If you get many collisions (more than one or two 471 * a day) you probably do not have enough clists 472 * and you should increase "nclist" in param.c. 473 */ 474 (void) unputc(&tp->t_outq); 475 (void) putc(FRAME_END, &tp->t_outq); 476 sc->sc_if.if_collisions++; 477 } else { 478 ++sc->sc_if.if_obytes; 479 sc->sc_if.if_opackets++; 480 } 481 } 482 } 483 484 /* 485 * Copy data buffer to mbuf chain; add ifnet pointer. 486 */ 487 static struct mbuf * 488 sl_btom(sc, len) 489 register struct sl_softc *sc; 490 register int len; 491 { 492 register struct mbuf *m; 493 494 MGETHDR(m, M_DONTWAIT, MT_DATA); 495 if (m == NULL) 496 return (NULL); 497 498 /* 499 * If we have more than MHLEN bytes, it's cheaper to 500 * queue the cluster we just filled & allocate a new one 501 * for the input buffer. Otherwise, fill the mbuf we 502 * allocated above. Note that code in the input routine 503 * guarantees that packet will fit in a cluster. 504 */ 505 if (len >= MHLEN) { 506 MCLGET(m, M_DONTWAIT); 507 if ((m->m_flags & M_EXT) == 0) { 508 /* 509 * we couldn't get a cluster - if memory's this 510 * low, it's time to start dropping packets. 511 */ 512 (void) m_free(m); 513 return (NULL); 514 } 515 sc->sc_ep = mtod(m, u_char *) + SLBUFSIZE; 516 m->m_data = (caddr_t)sc->sc_buf; 517 m->m_ext.ext_buf = (caddr_t)((int)sc->sc_buf &~ MCLOFSET); 518 } else 519 bcopy((caddr_t)sc->sc_buf, mtod(m, caddr_t), len); 520 521 m->m_len = len; 522 m->m_pkthdr.len = len; 523 m->m_pkthdr.rcvif = &sc->sc_if; 524 return (m); 525 } 526 527 /* 528 * tty interface receiver interrupt. 529 */ 530 slinput(c, tp) 531 register int c; 532 register struct tty *tp; 533 { 534 register struct sl_softc *sc; 535 register struct mbuf *m; 536 register int len; 537 int s; 538 539 tk_nin++; 540 sc = (struct sl_softc *)tp->t_sc; 541 if (sc == NULL) 542 return; 543 if (!(tp->t_state&TS_CARR_ON)) /* XXX */ 544 return; 545 546 ++sc->sc_if.if_ibytes; 547 c &= 0xff; /* XXX */ 548 549 #ifdef ABT_ESC 550 if (sc->sc_if.if_flags & IFF_DEBUG) { 551 if (c == ABT_ESC) { 552 /* 553 * If we have a previous abort, see whether 554 * this one is within the time limit. 555 */ 556 if (sc->sc_abortcount && 557 time.tv_sec >= sc->sc_starttime + ABT_WINDOW) 558 sc->sc_abortcount = 0; 559 /* 560 * If we see an abort after "idle" time, count it; 561 * record when the first abort escape arrived. 562 */ 563 if (time.tv_sec >= sc->sc_lasttime + ABT_IDLE) { 564 if (++sc->sc_abortcount == 1) 565 sc->sc_starttime = time.tv_sec; 566 if (sc->sc_abortcount >= ABT_COUNT) { 567 slclose(tp); 568 return; 569 } 570 } 571 } else 572 sc->sc_abortcount = 0; 573 sc->sc_lasttime = time.tv_sec; 574 } 575 #endif 576 577 switch (c) { 578 579 case TRANS_FRAME_ESCAPE: 580 if (sc->sc_escape) 581 c = FRAME_ESCAPE; 582 break; 583 584 case TRANS_FRAME_END: 585 if (sc->sc_escape) 586 c = FRAME_END; 587 break; 588 589 case FRAME_ESCAPE: 590 sc->sc_escape = 1; 591 return; 592 593 case FRAME_END: 594 len = sc->sc_mp - sc->sc_buf; 595 if (len < 3) 596 /* less than min length packet - ignore */ 597 goto newpack; 598 599 if ((c = (*sc->sc_buf & 0xf0)) != (IPVERSION << 4)) { 600 if (c & 0x80) 601 c = TYPE_COMPRESSED_TCP; 602 else if (c == TYPE_UNCOMPRESSED_TCP) 603 *sc->sc_buf &= 0x4f; /* XXX */ 604 /* 605 * We've got something that's not an IP packet. 606 * If compression is enabled, try to decompress it. 607 * Otherwise, if `auto-enable' compression is on and 608 * it's a reasonable packet, decompress it and then 609 * enable compression. Otherwise, drop it. 610 */ 611 if (sc->sc_if.if_flags & SC_COMPRESS) { 612 len = sl_uncompress_tcp(&sc->sc_buf, len, 613 (u_int)c, &sc->sc_comp); 614 if (len <= 0) 615 goto error; 616 } else if ((sc->sc_if.if_flags & SC_AUTOCOMP) && 617 c == TYPE_UNCOMPRESSED_TCP && len >= 40) { 618 len = sl_uncompress_tcp(&sc->sc_buf, len, 619 (u_int)c, &sc->sc_comp); 620 if (len <= 0) 621 goto error; 622 sc->sc_if.if_flags |= SC_COMPRESS; 623 } else 624 goto error; 625 } 626 m = sl_btom(sc, len); 627 if (m == NULL) 628 goto error; 629 630 sc->sc_if.if_ipackets++; 631 sc->sc_if.if_lastchange = time; 632 s = splimp(); 633 if (IF_QFULL(&ipintrq)) { 634 IF_DROP(&ipintrq); 635 sc->sc_if.if_ierrors++; 636 sc->sc_if.if_iqdrops++; 637 m_freem(m); 638 } else { 639 IF_ENQUEUE(&ipintrq, m); 640 schednetisr(NETISR_IP); 641 } 642 splx(s); 643 goto newpack; 644 } 645 if (sc->sc_mp < sc->sc_ep) { 646 *sc->sc_mp++ = c; 647 sc->sc_escape = 0; 648 return; 649 } 650 error: 651 sc->sc_if.if_ierrors++; 652 newpack: 653 sc->sc_mp = sc->sc_buf = sc->sc_ep - SLMAX; 654 sc->sc_escape = 0; 655 } 656 657 /* 658 * Process an ioctl request. 659 */ 660 slioctl(ifp, cmd, data) 661 register struct ifnet *ifp; 662 int cmd; 663 caddr_t data; 664 { 665 register struct ifaddr *ifa = (struct ifaddr *)data; 666 register struct ifreq *ifr; 667 register int s = splimp(), error = 0; 668 669 switch (cmd) { 670 671 case SIOCSIFADDR: 672 if (ifa->ifa_addr->sa_family == AF_INET) 673 ifp->if_flags |= IFF_UP; 674 else 675 error = EAFNOSUPPORT; 676 break; 677 678 case SIOCSIFDSTADDR: 679 if (ifa->ifa_addr->sa_family != AF_INET) 680 error = EAFNOSUPPORT; 681 break; 682 683 case SIOCADDMULTI: 684 case SIOCDELMULTI: 685 ifr = (struct ifreq *)data; 686 if (ifr == 0) { 687 error = EAFNOSUPPORT; /* XXX */ 688 break; 689 } 690 switch (ifr->ifr_addr.sa_family) { 691 692 #ifdef INET 693 case AF_INET: 694 break; 695 #endif 696 697 default: 698 error = EAFNOSUPPORT; 699 break; 700 } 701 break; 702 #endif 703 704 default: 705 error = EINVAL; 706 } 707 splx(s); 708 return (error); 709 } 710