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