1 /* 2 * Copyright (c) 1987, 1989, 1992, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)if_sl.c 8.6 (Berkeley) 2/1/94 34 * $FreeBSD: src/sys/net/if_sl.c,v 1.84.2.2 2002/02/13 00:43:10 dillon Exp $ 35 * $DragonFly: src/sys/net/sl/if_sl.c,v 1.14 2004/07/31 07:52:54 dillon Exp $ 36 */ 37 38 /* 39 * Serial Line interface 40 * 41 * Rick Adams 42 * Center for Seismic Studies 43 * 1300 N 17th Street, Suite 1450 44 * Arlington, Virginia 22209 45 * (703)276-7900 46 * rick@seismo.ARPA 47 * seismo!rick 48 * 49 * Pounded on heavily by Chris Torek (chris@mimsy.umd.edu, umcp-cs!chris). 50 * N.B.: this belongs in netinet, not net, the way it stands now. 51 * Should have a link-layer type designation, but wouldn't be 52 * backwards-compatible. 53 * 54 * Converted to 4.3BSD Beta by Chris Torek. 55 * Other changes made at Berkeley, based in part on code by Kirk Smith. 56 * W. Jolitz added slip abort. 57 * 58 * Hacked almost beyond recognition by Van Jacobson (van@helios.ee.lbl.gov). 59 * Added priority queuing for "interactive" traffic; hooks for TCP 60 * header compression; ICMP filtering (at 2400 baud, some cretin 61 * pinging you can use up all your bandwidth). Made low clist behavior 62 * more robust and slightly less likely to hang serial line. 63 * Sped up a bunch of things. 64 * 65 * Note that splimp() is used throughout to block both (tty) input 66 * interrupts and network activity; thus, splimp must be >= spltty. 67 */ 68 69 #include "use_sl.h" 70 71 #include "opt_inet.h" 72 #if !defined(KLD_MODULE) 73 #include "opt_slip.h" 74 #endif 75 #include <sys/param.h> 76 #include <sys/systm.h> 77 #include <sys/proc.h> 78 #include <sys/malloc.h> 79 #include <sys/mbuf.h> 80 #include <sys/dkstat.h> 81 #include <sys/socket.h> 82 #include <sys/sockio.h> 83 #include <sys/fcntl.h> 84 #include <sys/signalvar.h> 85 #include <sys/tty.h> 86 #include <sys/clist.h> 87 #include <sys/kernel.h> 88 #include <sys/conf.h> 89 90 #include <net/if.h> 91 #include <net/if_types.h> 92 #include <net/netisr.h> 93 94 #if INET 95 #include <netinet/in.h> 96 #include <netinet/in_systm.h> 97 #include <netinet/in_var.h> 98 #include <netinet/ip.h> 99 #else 100 #error "Huh? Slip without inet?" 101 #endif 102 103 #include <net/slcompress.h> 104 #include "if_slvar.h" 105 #include <net/slip.h> 106 107 #include <net/bpf.h> 108 109 #ifdef __i386__ 110 #include <i386/isa/intr_machdep.h> 111 #endif 112 113 static void slattach (void *); 114 PSEUDO_SET(slattach, if_sl); 115 116 /* 117 * SLRMAX is a hard limit on input packet size. To simplify the code 118 * and improve performance, we require that packets fit in an mbuf 119 * cluster, and if we get a compressed packet, there's enough extra 120 * room to expand the header into a max length tcp/ip header (128 121 * bytes). So, SLRMAX can be at most 122 * MCLBYTES - 128 123 * 124 * SLMTU is the default transmit MTU. The transmit MTU should be kept 125 * small enough so that interactive use doesn't suffer, but large 126 * enough to provide good performance. 552 is a good choice for SLMTU 127 * because it is high enough to not fragment TCP packets being routed 128 * through this host. Packet fragmentation is bad with SLIP because 129 * fragment headers aren't compressed. The previous assumptions about 130 * the best MTU value don't really hold when using modern modems with 131 * BTLZ data compression because the modem buffers play a much larger 132 * role in interactive performance than the MTU. The MTU can be changed 133 * at any time to suit the specific environment with ifconfig(8), and 134 * its maximum value is defined as SLTMAX. SLTMAX must not be so large 135 * that it would overflow the stack if BPF is configured (XXX; if_ppp.c 136 * handles this better). 137 * 138 * SLIP_HIWAT is the amount of data that will be queued 'downstream' 139 * of us (i.e., in clists waiting to be picked up by the tty output 140 * interrupt). If we queue a lot of data downstream, it's immune to 141 * our t.o.s. queuing. 142 * E.g., if SLIP_HIWAT is 1024, the interactive traffic in mixed 143 * telnet/ftp will see a 1 sec wait, independent of the mtu (the 144 * wait is dependent on the ftp window size but that's typically 145 * 1k - 4k). So, we want SLIP_HIWAT just big enough to amortize 146 * the cost (in idle time on the wire) of the tty driver running 147 * off the end of its clists & having to call back slstart for a 148 * new packet. For a tty interface with any buffering at all, this 149 * cost will be zero. Even with a totally brain dead interface (like 150 * the one on a typical workstation), the cost will be <= 1 character 151 * time. So, setting SLIP_HIWAT to ~100 guarantees that we'll lose 152 * at most 1% while maintaining good interactive response. 153 */ 154 #define BUFOFFSET (128+sizeof(struct ifnet **)+SLIP_HDRLEN) 155 #define SLRMAX (MCLBYTES - BUFOFFSET) 156 #define SLBUFSIZE (SLRMAX + BUFOFFSET) 157 #ifndef SLMTU 158 #define SLMTU 552 /* default MTU */ 159 #endif 160 #define SLTMAX 1500 /* maximum MTU */ 161 #define SLIP_HIWAT roundup(50,CBSIZE) 162 #define CLISTRESERVE 1024 /* Can't let clists get too low */ 163 164 /* 165 * SLIP ABORT ESCAPE MECHANISM: 166 * (inspired by HAYES modem escape arrangement) 167 * 1sec escape 1sec escape 1sec escape { 1sec escape 1sec escape } 168 * within window time signals a "soft" exit from slip mode by remote end 169 * if the IFF_DEBUG flag is on. 170 */ 171 #define ABT_ESC '\033' /* can't be t_intr - distant host must know it*/ 172 #define ABT_IDLE 1 /* in seconds - idle before an escape */ 173 #define ABT_COUNT 3 /* count of escapes for abort */ 174 #define ABT_WINDOW (ABT_COUNT*2+2) /* in seconds - time to count */ 175 176 static struct sl_softc sl_softc[NSL]; 177 178 #define FRAME_END 0xc0 /* Frame End */ 179 #define FRAME_ESCAPE 0xdb /* Frame Esc */ 180 #define TRANS_FRAME_END 0xdc /* transposed frame end */ 181 #define TRANS_FRAME_ESCAPE 0xdd /* transposed frame esc */ 182 183 static int slinit (struct sl_softc *); 184 static struct mbuf *sl_btom (struct sl_softc *, int); 185 static timeout_t sl_keepalive; 186 static timeout_t sl_outfill; 187 static int slclose (struct tty *,int); 188 static int slinput (int, struct tty *); 189 static int slioctl (struct ifnet *, u_long, caddr_t, struct ucred *); 190 static int sltioctl (struct tty *, u_long, caddr_t, int, struct thread *); 191 static int slopen (dev_t, struct tty *); 192 static int sloutput (struct ifnet *, 193 struct mbuf *, struct sockaddr *, struct rtentry *); 194 static int slstart (struct tty *); 195 196 static struct linesw slipdisc = { 197 slopen, slclose, l_noread, l_nowrite, 198 sltioctl, slinput, slstart, ttymodem, 199 FRAME_END 200 }; 201 202 /* 203 * Called from boot code to establish sl interfaces. 204 */ 205 static void 206 slattach(dummy) 207 void *dummy; 208 { 209 struct sl_softc *sc; 210 int i = 0; 211 212 linesw[SLIPDISC] = slipdisc; 213 214 for (sc = sl_softc; i < NSL; sc++) { 215 if_initname(&(sc->sc_if), "sl", i++); 216 sc->sc_if.if_mtu = SLMTU; 217 sc->sc_if.if_flags = 218 #ifdef SLIP_IFF_OPTS 219 SLIP_IFF_OPTS; 220 #else 221 IFF_POINTOPOINT | SC_AUTOCOMP | IFF_MULTICAST; 222 #endif 223 sc->sc_if.if_type = IFT_SLIP; 224 sc->sc_if.if_ioctl = slioctl; 225 sc->sc_if.if_output = sloutput; 226 sc->sc_if.if_snd.ifq_maxlen = 50; 227 sc->sc_fastq.ifq_maxlen = 32; 228 sc->sc_if.if_linkmib = sc; 229 sc->sc_if.if_linkmiblen = sizeof *sc; 230 if_attach(&sc->sc_if); 231 bpfattach(&sc->sc_if, DLT_SLIP, SLIP_HDRLEN); 232 } 233 } 234 235 static int 236 slinit(sc) 237 struct sl_softc *sc; 238 { 239 #ifdef __i386__ 240 int s; 241 242 s = splhigh(); 243 tty_imask |= net_imask; 244 net_imask = tty_imask; 245 update_intr_masks(); 246 splx(s); 247 if (bootverbose) 248 printf("new imasks: bio %x, tty %x, net %x\n", 249 bio_imask, tty_imask, net_imask); 250 #endif 251 if (sc->sc_ep == NULL) 252 sc->sc_ep = malloc(SLBUFSIZE, M_DEVBUF, M_WAITOK); 253 sc->sc_buf = sc->sc_ep + SLBUFSIZE - SLRMAX; 254 sc->sc_mp = sc->sc_buf; 255 sl_compress_init(&sc->sc_comp, -1); 256 return (1); 257 } 258 259 /* 260 * Line specific open routine. 261 * Attach the given tty to the first available sl unit. 262 */ 263 /* ARGSUSED */ 264 static int 265 slopen(dev_t dev, struct tty *tp) 266 { 267 struct sl_softc *sc; 268 int nsl; 269 int s, error; 270 struct thread *td = curthread; /* XXX */ 271 272 error = suser(td); 273 if (error) 274 return (error); 275 276 if (tp->t_line == SLIPDISC) 277 return (0); 278 279 for (nsl = NSL, sc = sl_softc; --nsl >= 0; sc++) 280 if (sc->sc_ttyp == NULL && !(sc->sc_flags & SC_STATIC)) { 281 if (slinit(sc) == 0) 282 return (ENOBUFS); 283 tp->t_sc = (caddr_t)sc; 284 sc->sc_ttyp = tp; 285 sc->sc_if.if_baudrate = tp->t_ospeed; 286 ttyflush(tp, FREAD | FWRITE); 287 288 tp->t_line = SLIPDISC; 289 /* 290 * We don't use t_canq or t_rawq, so reduce their 291 * cblock resources to 0. Reserve enough cblocks 292 * for t_outq to guarantee that we can fit a full 293 * packet if the SLIP_HIWAT check allows slstart() 294 * to loop. Use the same value for the cblock 295 * limit since the reserved blocks should always 296 * be enough. Reserving cblocks probably makes 297 * the CLISTRESERVE check unnecessary and wasteful. 298 */ 299 clist_alloc_cblocks(&tp->t_canq, 0, 0); 300 clist_alloc_cblocks(&tp->t_outq, 301 SLIP_HIWAT + 2 * sc->sc_if.if_mtu + 1, 302 SLIP_HIWAT + 2 * sc->sc_if.if_mtu + 1); 303 clist_alloc_cblocks(&tp->t_rawq, 0, 0); 304 305 s = splnet(); 306 if_up(&sc->sc_if); 307 splx(s); 308 return (0); 309 } 310 return (ENXIO); 311 } 312 313 /* 314 * Line specific close routine. 315 * Detach the tty from the sl unit. 316 */ 317 static int 318 slclose(tp,flag) 319 struct tty *tp; 320 int flag; 321 { 322 struct sl_softc *sc; 323 int s; 324 325 ttyflush(tp, FREAD | FWRITE); 326 /* 327 * XXX the placement of the following spl is misleading. tty 328 * interrupts must be blocked across line discipline switches 329 * and throughout closes to avoid races. 330 */ 331 s = splimp(); /* actually, max(spltty, splnet) */ 332 clist_free_cblocks(&tp->t_outq); 333 tp->t_line = 0; 334 sc = (struct sl_softc *)tp->t_sc; 335 if (sc != NULL) { 336 if (sc->sc_outfill) { 337 sc->sc_outfill = 0; 338 untimeout(sl_outfill, sc, sc->sc_ofhandle); 339 } 340 if (sc->sc_keepalive) { 341 sc->sc_keepalive = 0; 342 untimeout(sl_keepalive, sc, sc->sc_kahandle); 343 } 344 if_down(&sc->sc_if); 345 sc->sc_flags &= SC_STATIC; 346 sc->sc_ttyp = NULL; 347 tp->t_sc = NULL; 348 if (sc->sc_ep) { 349 free(sc->sc_ep, M_DEVBUF); 350 sc->sc_ep = NULL; 351 } 352 sc->sc_mp = 0; 353 sc->sc_buf = 0; 354 } 355 splx(s); 356 return 0; 357 } 358 359 /* 360 * Line specific (tty) ioctl routine. 361 * Provide a way to get the sl unit number. 362 */ 363 /* ARGSUSED */ 364 static int 365 sltioctl(struct tty *tp, u_long cmd, caddr_t data, int flag, struct thread *p) 366 { 367 struct sl_softc *sc = (struct sl_softc *)tp->t_sc, *nc, *tmpnc; 368 int s, nsl; 369 370 s = splimp(); 371 switch (cmd) { 372 case SLIOCGUNIT: 373 *(int *)data = sc->sc_if.if_dunit; 374 break; 375 376 case SLIOCSUNIT: 377 if (sc->sc_if.if_dunit != *(u_int *)data) { 378 for (nsl = NSL, nc = sl_softc; --nsl >= 0; nc++) { 379 if ( nc->sc_if.if_dunit == *(u_int *)data 380 && nc->sc_ttyp == NULL 381 ) { 382 tmpnc = malloc(sizeof *tmpnc, M_TEMP, 383 M_WAITOK); 384 *tmpnc = *nc; 385 *nc = *sc; 386 nc->sc_if = tmpnc->sc_if; 387 tmpnc->sc_if = sc->sc_if; 388 *sc = *tmpnc; 389 free(tmpnc, M_TEMP); 390 if (sc->sc_if.if_flags & IFF_UP) { 391 if_down(&sc->sc_if); 392 if (!(nc->sc_if.if_flags & IFF_UP)) 393 if_up(&nc->sc_if); 394 } else if (nc->sc_if.if_flags & IFF_UP) 395 if_down(&nc->sc_if); 396 sc->sc_flags &= ~SC_STATIC; 397 sc->sc_flags |= (nc->sc_flags & SC_STATIC); 398 tp->t_sc = sc = nc; 399 clist_alloc_cblocks(&tp->t_outq, 400 SLIP_HIWAT + 2 * sc->sc_if.if_mtu + 1, 401 SLIP_HIWAT + 2 * sc->sc_if.if_mtu + 1); 402 sl_compress_init(&sc->sc_comp, -1); 403 goto slfound; 404 } 405 } 406 splx(s); 407 return (ENXIO); 408 } 409 slfound: 410 sc->sc_flags |= SC_STATIC; 411 break; 412 413 case SLIOCSKEEPAL: 414 sc->sc_keepalive = *(u_int *)data * hz; 415 if (sc->sc_keepalive) { 416 sc->sc_flags |= SC_KEEPALIVE; 417 sc->sc_kahandle = timeout(sl_keepalive, sc, 418 sc->sc_keepalive); 419 } else { 420 if ((sc->sc_flags & SC_KEEPALIVE) != 0) { 421 untimeout(sl_keepalive, sc, sc->sc_kahandle); 422 sc->sc_flags &= ~SC_KEEPALIVE; 423 } 424 } 425 break; 426 427 case SLIOCGKEEPAL: 428 *(int *)data = sc->sc_keepalive / hz; 429 break; 430 431 case SLIOCSOUTFILL: 432 sc->sc_outfill = *(u_int *)data * hz; 433 if (sc->sc_outfill) { 434 sc->sc_flags |= SC_OUTWAIT; 435 sc->sc_ofhandle = timeout(sl_outfill, sc, 436 sc->sc_outfill); 437 } else { 438 if ((sc->sc_flags & SC_OUTWAIT) != 0) { 439 untimeout(sl_outfill, sc, sc->sc_ofhandle); 440 sc->sc_flags &= ~SC_OUTWAIT; 441 } 442 } 443 break; 444 445 case SLIOCGOUTFILL: 446 *(int *)data = sc->sc_outfill / hz; 447 break; 448 449 default: 450 splx(s); 451 return (ENOIOCTL); 452 } 453 splx(s); 454 return (0); 455 } 456 457 /* 458 * Queue a packet. Start transmission if not active. 459 * Compression happens in slstart; if we do it here, IP TOS 460 * will cause us to not compress "background" packets, because 461 * ordering gets trashed. It can be done for all packets in slstart. 462 */ 463 static int 464 sloutput(ifp, m, dst, rtp) 465 struct ifnet *ifp; 466 struct mbuf *m; 467 struct sockaddr *dst; 468 struct rtentry *rtp; 469 { 470 struct sl_softc *sc = &sl_softc[ifp->if_dunit]; 471 struct ip *ip; 472 struct ifqueue *ifq; 473 int s; 474 475 /* 476 * `Cannot happen' (see slioctl). Someday we will extend 477 * the line protocol to support other address families. 478 */ 479 if (dst->sa_family != AF_INET) { 480 printf("%s: af%d not supported\n", sc->sc_if.if_xname, 481 dst->sa_family); 482 m_freem(m); 483 sc->sc_if.if_noproto++; 484 return (EAFNOSUPPORT); 485 } 486 487 if (sc->sc_ttyp == NULL || !(ifp->if_flags & IFF_UP)) { 488 m_freem(m); 489 return (ENETDOWN); 490 } 491 if ((sc->sc_ttyp->t_state & TS_CONNECTED) == 0) { 492 m_freem(m); 493 return (EHOSTUNREACH); 494 } 495 ifq = &sc->sc_if.if_snd; 496 ip = mtod(m, struct ip *); 497 if (sc->sc_if.if_flags & SC_NOICMP && ip->ip_p == IPPROTO_ICMP) { 498 m_freem(m); 499 return (ENETRESET); /* XXX ? */ 500 } 501 if (ip->ip_tos & IPTOS_LOWDELAY) 502 ifq = &sc->sc_fastq; 503 s = splimp(); 504 if (IF_QFULL(ifq)) { 505 IF_DROP(ifq); 506 m_freem(m); 507 splx(s); 508 sc->sc_if.if_oerrors++; 509 return (ENOBUFS); 510 } 511 IF_ENQUEUE(ifq, m); 512 if (sc->sc_ttyp->t_outq.c_cc == 0) 513 slstart(sc->sc_ttyp); 514 splx(s); 515 return (0); 516 } 517 518 /* 519 * Start output on interface. Get another datagram 520 * to send from the interface queue and map it to 521 * the interface before starting output. 522 */ 523 static int 524 slstart(tp) 525 struct tty *tp; 526 { 527 struct sl_softc *sc = (struct sl_softc *)tp->t_sc; 528 struct mbuf *m; 529 u_char *cp; 530 struct ip *ip; 531 int s; 532 u_char bpfbuf[SLTMAX + SLIP_HDRLEN]; 533 int len = 0; 534 535 for (;;) { 536 /* 537 * Call output process whether or not there is more in the 538 * output queue. We are being called in lieu of ttstart 539 * and must do what it would. 540 */ 541 (*tp->t_oproc)(tp); 542 543 if (tp->t_outq.c_cc != 0) { 544 if (sc != NULL) 545 sc->sc_flags &= ~SC_OUTWAIT; 546 if (tp->t_outq.c_cc > SLIP_HIWAT) 547 return 0; 548 } 549 550 /* 551 * This happens briefly when the line shuts down. 552 */ 553 if (sc == NULL) 554 return 0; 555 556 /* 557 * Get a packet and send it to the interface. 558 */ 559 s = splimp(); 560 IF_DEQUEUE(&sc->sc_fastq, m); 561 if (m) 562 sc->sc_if.if_omcasts++; /* XXX */ 563 else 564 IF_DEQUEUE(&sc->sc_if.if_snd, m); 565 splx(s); 566 if (m == NULL) 567 return 0; 568 569 /* 570 * We do the header compression here rather than in sloutput 571 * because the packets will be out of order if we are using TOS 572 * queueing, and the connection id compression will get 573 * munged when this happens. 574 */ 575 if (sc->sc_if.if_bpf) { 576 /* 577 * We need to save the TCP/IP header before it's 578 * compressed. To avoid complicated code, we just 579 * copy the entire packet into a stack buffer (since 580 * this is a serial line, packets should be short 581 * and/or the copy should be negligible cost compared 582 * to the packet transmission time). 583 */ 584 struct mbuf *m1 = m; 585 u_char *cp = bpfbuf + SLIP_HDRLEN; 586 587 len = 0; 588 do { 589 int mlen = m1->m_len; 590 591 bcopy(mtod(m1, caddr_t), cp, mlen); 592 cp += mlen; 593 len += mlen; 594 } while ((m1 = m1->m_next) != NULL); 595 } 596 ip = mtod(m, struct ip *); 597 if (ip->ip_v == IPVERSION && ip->ip_p == IPPROTO_TCP) { 598 if (sc->sc_if.if_flags & SC_COMPRESS) 599 *mtod(m, u_char *) |= sl_compress_tcp(m, ip, 600 &sc->sc_comp, 1); 601 } 602 if (sc->sc_if.if_bpf) { 603 /* 604 * Put the SLIP pseudo-"link header" in place. The 605 * compressed header is now at the beginning of the 606 * mbuf. 607 */ 608 bpfbuf[SLX_DIR] = SLIPDIR_OUT; 609 bcopy(mtod(m, caddr_t), &bpfbuf[SLX_CHDR], CHDR_LEN); 610 bpf_tap(&sc->sc_if, bpfbuf, len + SLIP_HDRLEN); 611 } 612 613 /* 614 * If system is getting low on clists, just flush our 615 * output queue (if the stuff was important, it'll get 616 * retransmitted). Note that SLTMAX is used instead of 617 * the current if_mtu setting because connections that 618 * have already been established still use the original 619 * (possibly larger) mss. 620 */ 621 if (cfreecount < CLISTRESERVE + SLTMAX) { 622 m_freem(m); 623 sc->sc_if.if_collisions++; 624 continue; 625 } 626 627 sc->sc_flags &= ~SC_OUTWAIT; 628 /* 629 * The extra FRAME_END will start up a new packet, and thus 630 * will flush any accumulated garbage. We do this whenever 631 * the line may have been idle for some time. 632 */ 633 if (tp->t_outq.c_cc == 0) { 634 ++sc->sc_if.if_obytes; 635 (void) putc(FRAME_END, &tp->t_outq); 636 } 637 638 while (m) { 639 u_char *ep; 640 641 cp = mtod(m, u_char *); ep = cp + m->m_len; 642 while (cp < ep) { 643 /* 644 * Find out how many bytes in the string we can 645 * handle without doing something special. 646 */ 647 u_char *bp = cp; 648 649 while (cp < ep) { 650 switch (*cp++) { 651 case FRAME_ESCAPE: 652 case FRAME_END: 653 --cp; 654 goto out; 655 } 656 } 657 out: 658 if (cp > bp) { 659 /* 660 * Put n characters at once 661 * into the tty output queue. 662 */ 663 if (b_to_q((char *)bp, cp - bp, 664 &tp->t_outq)) 665 break; 666 sc->sc_if.if_obytes += cp - bp; 667 } 668 /* 669 * If there are characters left in the mbuf, 670 * the first one must be special.. 671 * Put it out in a different form. 672 */ 673 if (cp < ep) { 674 if (putc(FRAME_ESCAPE, &tp->t_outq)) 675 break; 676 if (putc(*cp++ == FRAME_ESCAPE ? 677 TRANS_FRAME_ESCAPE : TRANS_FRAME_END, 678 &tp->t_outq)) { 679 (void) unputc(&tp->t_outq); 680 break; 681 } 682 sc->sc_if.if_obytes += 2; 683 } 684 } 685 m = m_free(m); 686 } 687 688 if (putc(FRAME_END, &tp->t_outq)) { 689 /* 690 * Not enough room. Remove a char to make room 691 * and end the packet normally. 692 * If you get many collisions (more than one or two 693 * a day) you probably do not have enough clists 694 * and you should increase "nclist" in param.c. 695 */ 696 (void) unputc(&tp->t_outq); 697 (void) putc(FRAME_END, &tp->t_outq); 698 sc->sc_if.if_collisions++; 699 } else { 700 ++sc->sc_if.if_obytes; 701 sc->sc_if.if_opackets++; 702 } 703 } 704 return 0; 705 } 706 707 /* 708 * Copy data buffer to mbuf chain; add ifnet pointer. 709 */ 710 static struct mbuf * 711 sl_btom(sc, len) 712 struct sl_softc *sc; 713 int len; 714 { 715 struct mbuf *m; 716 717 if (len >= MCLBYTES) 718 return (NULL); 719 720 MGETHDR(m, MB_DONTWAIT, MT_DATA); 721 if (m == NULL) 722 return (NULL); 723 724 /* 725 * If we have more than MHLEN bytes, it's cheaper to 726 * queue the cluster we just filled & allocate a new one 727 * for the input buffer. Otherwise, fill the mbuf we 728 * allocated above. Note that code in the input routine 729 * guarantees that packet will fit in a cluster. 730 */ 731 if (len >= MHLEN) { 732 MCLGET(m, MB_DONTWAIT); 733 if ((m->m_flags & M_EXT) == 0) { 734 /* 735 * we couldn't get a cluster - if memory's this 736 * low, it's time to start dropping packets. 737 */ 738 m_free(m); 739 return (NULL); 740 } 741 } 742 bcopy((caddr_t)sc->sc_buf, mtod(m, caddr_t), len); 743 m->m_len = len; 744 m->m_pkthdr.len = len; 745 m->m_pkthdr.rcvif = &sc->sc_if; 746 return (m); 747 } 748 749 /* 750 * tty interface receiver interrupt. 751 */ 752 static int 753 slinput(c, tp) 754 int c; 755 struct tty *tp; 756 { 757 struct sl_softc *sc; 758 struct mbuf *m; 759 int len; 760 u_char chdr[CHDR_LEN]; 761 762 tk_nin++; 763 sc = (struct sl_softc *)tp->t_sc; 764 if (sc == NULL) 765 return 0; 766 if (c & TTY_ERRORMASK || (tp->t_state & TS_CONNECTED) == 0) { 767 sc->sc_flags |= SC_ERROR; 768 return 0; 769 } 770 c &= TTY_CHARMASK; 771 772 ++sc->sc_if.if_ibytes; 773 774 if (sc->sc_if.if_flags & IFF_DEBUG) { 775 if (c == ABT_ESC) { 776 /* 777 * If we have a previous abort, see whether 778 * this one is within the time limit. 779 */ 780 if (sc->sc_abortcount && 781 time_second >= sc->sc_starttime + ABT_WINDOW) 782 sc->sc_abortcount = 0; 783 /* 784 * If we see an abort after "idle" time, count it; 785 * record when the first abort escape arrived. 786 */ 787 if (time_second >= sc->sc_lasttime + ABT_IDLE) { 788 if (++sc->sc_abortcount == 1) 789 sc->sc_starttime = time_second; 790 if (sc->sc_abortcount >= ABT_COUNT) { 791 slclose(tp,0); 792 return 0; 793 } 794 } 795 } else 796 sc->sc_abortcount = 0; 797 sc->sc_lasttime = time_second; 798 } 799 800 switch (c) { 801 802 case TRANS_FRAME_ESCAPE: 803 if (sc->sc_escape) 804 c = FRAME_ESCAPE; 805 break; 806 807 case TRANS_FRAME_END: 808 if (sc->sc_escape) 809 c = FRAME_END; 810 break; 811 812 case FRAME_ESCAPE: 813 sc->sc_escape = 1; 814 return 0; 815 816 case FRAME_END: 817 sc->sc_flags &= ~SC_KEEPALIVE; 818 if(sc->sc_flags & SC_ERROR) { 819 sc->sc_flags &= ~SC_ERROR; 820 goto newpack; 821 } 822 len = sc->sc_mp - sc->sc_buf; 823 if (len < 3) 824 /* less than min length packet - ignore */ 825 goto newpack; 826 827 if (sc->sc_if.if_bpf) { 828 /* 829 * Save the compressed header, so we 830 * can tack it on later. Note that we 831 * will end up copying garbage in some 832 * cases but this is okay. We remember 833 * where the buffer started so we can 834 * compute the new header length. 835 */ 836 bcopy(sc->sc_buf, chdr, CHDR_LEN); 837 } 838 839 if ((c = (*sc->sc_buf & 0xf0)) != (IPVERSION << 4)) { 840 if (c & 0x80) 841 c = TYPE_COMPRESSED_TCP; 842 else if (c == TYPE_UNCOMPRESSED_TCP) 843 *sc->sc_buf &= 0x4f; /* XXX */ 844 /* 845 * We've got something that's not an IP packet. 846 * If compression is enabled, try to decompress it. 847 * Otherwise, if `auto-enable' compression is on and 848 * it's a reasonable packet, decompress it and then 849 * enable compression. Otherwise, drop it. 850 */ 851 if (sc->sc_if.if_flags & SC_COMPRESS) { 852 len = sl_uncompress_tcp(&sc->sc_buf, len, 853 (u_int)c, &sc->sc_comp); 854 if (len <= 0) 855 goto error; 856 } else if ((sc->sc_if.if_flags & SC_AUTOCOMP) && 857 c == TYPE_UNCOMPRESSED_TCP && len >= 40) { 858 len = sl_uncompress_tcp(&sc->sc_buf, len, 859 (u_int)c, &sc->sc_comp); 860 if (len <= 0) 861 goto error; 862 sc->sc_if.if_flags |= SC_COMPRESS; 863 } else 864 goto error; 865 } 866 if (sc->sc_if.if_bpf) { 867 /* 868 * Put the SLIP pseudo-"link header" in place. 869 * We couldn't do this any earlier since 870 * decompression probably moved the buffer 871 * pointer. Then, invoke BPF. 872 */ 873 u_char *hp = sc->sc_buf - SLIP_HDRLEN; 874 875 hp[SLX_DIR] = SLIPDIR_IN; 876 bcopy(chdr, &hp[SLX_CHDR], CHDR_LEN); 877 bpf_tap(&sc->sc_if, hp, len + SLIP_HDRLEN); 878 } 879 m = sl_btom(sc, len); 880 if (m == NULL) 881 goto error; 882 883 sc->sc_if.if_ipackets++; 884 885 if ((sc->sc_if.if_flags & IFF_UP) == 0) { 886 m_freem(m); 887 goto newpack; 888 } 889 890 if (netisr_queue(NETISR_IP, m)) { 891 sc->sc_if.if_ierrors++; 892 sc->sc_if.if_iqdrops++; 893 } 894 895 goto newpack; 896 } 897 if (sc->sc_mp < sc->sc_ep + SLBUFSIZE) { 898 *sc->sc_mp++ = c; 899 sc->sc_escape = 0; 900 return 0; 901 } 902 903 /* can't put lower; would miss an extra frame */ 904 sc->sc_flags |= SC_ERROR; 905 906 error: 907 sc->sc_if.if_ierrors++; 908 newpack: 909 sc->sc_mp = sc->sc_buf = sc->sc_ep + SLBUFSIZE - SLRMAX; 910 sc->sc_escape = 0; 911 return 0; 912 } 913 914 /* 915 * Process an ioctl request. 916 */ 917 static int 918 slioctl(ifp, cmd, data, cr) 919 struct ifnet *ifp; 920 u_long cmd; 921 caddr_t data; 922 struct ucred *cr; 923 { 924 struct ifaddr *ifa = (struct ifaddr *)data; 925 struct ifreq *ifr = (struct ifreq *)data; 926 int s, error = 0; 927 928 s = splimp(); 929 930 switch (cmd) { 931 932 case SIOCSIFFLAGS: 933 /* 934 * if.c will set the interface up even if we 935 * don't want it to. 936 */ 937 if (sl_softc[ifp->if_dunit].sc_ttyp == NULL) { 938 ifp->if_flags &= ~IFF_UP; 939 } 940 break; 941 case SIOCSIFADDR: 942 /* 943 * This is "historical" - set the interface up when 944 * setting the address. 945 */ 946 if (ifa->ifa_addr->sa_family == AF_INET) { 947 if (sl_softc[ifp->if_dunit].sc_ttyp != NULL) 948 ifp->if_flags |= IFF_UP; 949 } else { 950 error = EAFNOSUPPORT; 951 } 952 break; 953 954 case SIOCSIFDSTADDR: 955 if (ifa->ifa_addr->sa_family != AF_INET) 956 error = EAFNOSUPPORT; 957 break; 958 959 case SIOCADDMULTI: 960 case SIOCDELMULTI: 961 break; 962 963 case SIOCSIFMTU: 964 /* 965 * Set the interface MTU. 966 */ 967 if (ifr->ifr_mtu > SLTMAX) 968 error = EINVAL; 969 else { 970 struct tty *tp; 971 972 ifp->if_mtu = ifr->ifr_mtu; 973 tp = sl_softc[ifp->if_dunit].sc_ttyp; 974 if (tp != NULL) 975 clist_alloc_cblocks(&tp->t_outq, 976 SLIP_HIWAT + 2 * ifp->if_mtu + 1, 977 SLIP_HIWAT + 2 * ifp->if_mtu + 1); 978 } 979 break; 980 981 default: 982 error = EINVAL; 983 } 984 splx(s); 985 return (error); 986 } 987 988 static void 989 sl_keepalive(chan) 990 void *chan; 991 { 992 struct sl_softc *sc = chan; 993 994 if (sc->sc_keepalive) { 995 if (sc->sc_flags & SC_KEEPALIVE) 996 pgsignal (sc->sc_ttyp->t_pgrp, SIGURG, 1); 997 else 998 sc->sc_flags |= SC_KEEPALIVE; 999 sc->sc_kahandle = timeout(sl_keepalive, sc, sc->sc_keepalive); 1000 } else { 1001 sc->sc_flags &= ~SC_KEEPALIVE; 1002 } 1003 } 1004 1005 static void 1006 sl_outfill(chan) 1007 void *chan; 1008 { 1009 struct sl_softc *sc = chan; 1010 struct tty *tp = sc->sc_ttyp; 1011 int s; 1012 1013 if (sc->sc_outfill && tp != NULL) { 1014 if (sc->sc_flags & SC_OUTWAIT) { 1015 s = splimp (); 1016 ++sc->sc_if.if_obytes; 1017 (void) putc(FRAME_END, &tp->t_outq); 1018 (*tp->t_oproc)(tp); 1019 splx (s); 1020 } else 1021 sc->sc_flags |= SC_OUTWAIT; 1022 sc->sc_ofhandle = timeout(sl_outfill, sc, sc->sc_outfill); 1023 } else { 1024 sc->sc_flags &= ~SC_OUTWAIT; 1025 } 1026 } 1027