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