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