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