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