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