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