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