if_sl.c revision 1.78
1/* $NetBSD: if_sl.c,v 1.78 2001/11/12 23:49:42 lukem Exp $ */ 2 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. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * @(#)if_sl.c 8.9 (Berkeley) 1/9/95 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 66#include <sys/cdefs.h> 67__KERNEL_RCSID(0, "$NetBSD: if_sl.c,v 1.78 2001/11/12 23:49:42 lukem Exp $"); 68 69#include "sl.h" 70#if NSL > 0 71 72#include "opt_inet.h" 73#include "bpfilter.h" 74 75#include <sys/param.h> 76#include <sys/proc.h> 77#include <sys/malloc.h> 78#include <sys/mbuf.h> 79#include <sys/buf.h> 80#include <sys/dkstat.h> 81#include <sys/socket.h> 82#include <sys/ioctl.h> 83#include <sys/file.h> 84#include <sys/conf.h> 85#include <sys/tty.h> 86#include <sys/kernel.h> 87#if __NetBSD__ 88#include <sys/systm.h> 89#endif 90 91#include <machine/cpu.h> 92#ifdef __HAVE_GENERIC_SOFT_INTERRUPTS 93#include <machine/intr.h> 94#endif 95 96#include <net/if.h> 97#include <net/if_types.h> 98#include <net/netisr.h> 99#include <net/route.h> 100 101#ifdef INET 102#include <netinet/in.h> 103#include <netinet/in_systm.h> 104#include <netinet/in_var.h> 105#include <netinet/ip.h> 106#else 107#error Slip without inet? 108#endif 109 110#include <net/slcompress.h> 111#include <net/if_slvar.h> 112#include <net/slip.h> 113 114#if NBPFILTER > 0 115#include <sys/time.h> 116#include <net/bpf.h> 117#endif 118 119/* 120 * SLMAX is a hard limit on input packet size. To simplify the code 121 * and improve performance, we require that packets fit in an mbuf 122 * cluster, and if we get a compressed packet, there's enough extra 123 * room to expand the header into a max length tcp/ip header (128 124 * bytes). So, SLMAX can be at most 125 * MCLBYTES - 128 126 * 127 * SLMTU is a hard limit on output packet size. To insure good 128 * interactive response, SLMTU wants to be the smallest size that 129 * amortizes the header cost. (Remember that even with 130 * type-of-service queuing, we have to wait for any in-progress 131 * packet to finish. I.e., we wait, on the average, 1/2 * mtu / 132 * cps, where cps is the line speed in characters per second. 133 * E.g., 533ms wait for a 1024 byte MTU on a 9600 baud line. The 134 * average compressed header size is 6-8 bytes so any MTU > 90 135 * bytes will give us 90% of the line bandwidth. A 100ms wait is 136 * tolerable (500ms is not), so want an MTU around 296. (Since TCP 137 * will send 256 byte segments (to allow for 40 byte headers), the 138 * typical packet size on the wire will be around 260 bytes). In 139 * 4.3tahoe+ systems, we can set an MTU in a route so we do that & 140 * leave the interface MTU relatively high (so we don't IP fragment 141 * when acting as a gateway to someone using a stupid MTU). 142 * 143 * Similar considerations apply to SLIP_HIWAT: It's the amount of 144 * data that will be queued 'downstream' of us (i.e., in clists 145 * waiting to be picked up by the tty output interrupt). If we 146 * queue a lot of data downstream, it's immune to our t.o.s. queuing. 147 * E.g., if SLIP_HIWAT is 1024, the interactive traffic in mixed 148 * telnet/ftp will see a 1 sec wait, independent of the mtu (the 149 * wait is dependent on the ftp window size but that's typically 150 * 1k - 4k). So, we want SLIP_HIWAT just big enough to amortize 151 * the cost (in idle time on the wire) of the tty driver running 152 * off the end of its clists & having to call back slstart for a 153 * new packet. For a tty interface with any buffering at all, this 154 * cost will be zero. Even with a totally brain dead interface (like 155 * the one on a typical workstation), the cost will be <= 1 character 156 * time. So, setting SLIP_HIWAT to ~100 guarantees that we'll lose 157 * at most 1% while maintaining good interactive response. 158 */ 159#define BUFOFFSET (128+sizeof(struct ifnet **)+SLIP_HDRLEN) 160#define SLMAX (MCLBYTES - BUFOFFSET) 161#define SLBUFSIZE (SLMAX + BUFOFFSET) 162#ifndef SLMTU 163#define SLMTU 296 164#endif 165#if (SLMTU < 3) 166#error SLMTU way too small. 167#endif 168#define SLIP_HIWAT roundup(50,CBSIZE) 169#ifndef __NetBSD__ /* XXX - cgd */ 170#define CLISTRESERVE 1024 /* Can't let clists get too low */ 171#endif /* !__NetBSD__ */ 172 173/* 174 * SLIP ABORT ESCAPE MECHANISM: 175 * (inspired by HAYES modem escape arrangement) 176 * 1sec escape 1sec escape 1sec escape { 1sec escape 1sec escape } 177 * within window time signals a "soft" exit from slip mode by remote end 178 * if the IFF_DEBUG flag is on. 179 */ 180#define ABT_ESC '\033' /* can't be t_intr - distant host must know it*/ 181#define ABT_IDLE 1 /* in seconds - idle before an escape */ 182#define ABT_COUNT 3 /* count of escapes for abort */ 183#define ABT_WINDOW (ABT_COUNT*2+2) /* in seconds - time to count */ 184 185struct sl_softc sl_softc[NSL]; 186 187#define FRAME_END 0xc0 /* Frame End */ 188#define FRAME_ESCAPE 0xdb /* Frame Esc */ 189#define TRANS_FRAME_END 0xdc /* transposed frame end */ 190#define TRANS_FRAME_ESCAPE 0xdd /* transposed frame esc */ 191 192#ifndef __HAVE_GENERIC_SOFT_INTERRUPTS 193void slnetisr(void); 194#endif 195void slintr(void *); 196 197static int slinit __P((struct sl_softc *)); 198static struct mbuf *sl_btom __P((struct sl_softc *, int)); 199 200/* 201 * Called from boot code to establish sl interfaces. 202 */ 203void 204slattach() 205{ 206 struct sl_softc *sc; 207 int i = 0; 208 209 for (sc = sl_softc; i < NSL; sc++) { 210 sc->sc_unit = i; /* XXX */ 211 sprintf(sc->sc_if.if_xname, "sl%d", i++); 212 sc->sc_if.if_softc = sc; 213 sc->sc_if.if_mtu = SLMTU; 214 sc->sc_if.if_flags = 215 IFF_POINTOPOINT | SC_AUTOCOMP | IFF_MULTICAST; 216 sc->sc_if.if_type = IFT_SLIP; 217 sc->sc_if.if_ioctl = slioctl; 218 sc->sc_if.if_output = sloutput; 219 sc->sc_if.if_dlt = DLT_SLIP; 220 sc->sc_fastq.ifq_maxlen = 32; 221 IFQ_SET_READY(&sc->sc_if.if_snd); 222 if_attach(&sc->sc_if); 223 if_alloc_sadl(&sc->sc_if); 224#if NBPFILTER > 0 225 bpfattach(&sc->sc_if, DLT_SLIP, SLIP_HDRLEN); 226#endif 227 } 228} 229 230static int 231slinit(sc) 232 struct sl_softc *sc; 233{ 234 235 if (sc->sc_mbuf == NULL) { 236 MGETHDR(sc->sc_mbuf, M_WAIT, MT_DATA); 237 MCLGET(sc->sc_mbuf, M_WAIT); 238 } 239 sc->sc_ep = (u_char *) sc->sc_mbuf->m_ext.ext_buf + 240 sc->sc_mbuf->m_ext.ext_size; 241 sc->sc_mp = sc->sc_pktstart = (u_char *) sc->sc_mbuf->m_ext.ext_buf + 242 BUFOFFSET; 243 244 sl_compress_init(&sc->sc_comp); 245 246 return (1); 247} 248 249/* 250 * Line specific open routine. 251 * Attach the given tty to the first available sl unit. 252 */ 253/* ARGSUSED */ 254int 255slopen(dev, tp) 256 dev_t dev; 257 struct tty *tp; 258{ 259 struct proc *p = curproc; /* XXX */ 260 struct sl_softc *sc; 261 int nsl; 262 int error; 263 int s; 264 265 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) 266 return (error); 267 268 if (tp->t_linesw->l_no == SLIPDISC) 269 return (0); 270 271 for (nsl = NSL, sc = sl_softc; --nsl >= 0; sc++) 272 if (sc->sc_ttyp == NULL) { 273#ifdef __HAVE_GENERIC_SOFT_INTERRUPTS 274 sc->sc_si = softintr_establish(IPL_SOFTNET, 275 slintr, sc); 276 if (sc->sc_si == NULL) 277 return (ENOMEM); 278#endif 279 if (slinit(sc) == 0) { 280#ifdef __HAVE_GENERIC_SOFT_INTERRUPTS 281 softintr_disestablish(sc->sc_si); 282#endif 283 return (ENOBUFS); 284 } 285 tp->t_sc = (caddr_t)sc; 286 sc->sc_ttyp = tp; 287 sc->sc_if.if_baudrate = tp->t_ospeed; 288 s = spltty(); 289 tp->t_state |= TS_ISOPEN | TS_XCLUDE; 290 splx(s); 291 ttyflush(tp, FREAD | FWRITE); 292#ifdef __NetBSD__ 293 /* 294 * make sure tty output queue is large enough 295 * to hold a full-sized packet (including frame 296 * end, and a possible extra frame end). full-sized 297 * packet occupies a max of 2*SLMAX bytes (because 298 * of possible escapes), and add two on for frame 299 * ends. 300 */ 301 s = spltty(); 302 if (tp->t_outq.c_cn < 2*SLMAX+2) { 303 sc->sc_oldbufsize = tp->t_outq.c_cn; 304 sc->sc_oldbufquot = tp->t_outq.c_cq != 0; 305 306 clfree(&tp->t_outq); 307 error = clalloc(&tp->t_outq, 2*SLMAX+2, 0); 308 if (error) { 309 splx(s); 310#ifdef __HAVE_GENERIC_SOFT_INTERRUPTS 311 softintr_disestablish(sc->sc_si); 312#endif 313 return(error); 314 } 315 } else 316 sc->sc_oldbufsize = sc->sc_oldbufquot = 0; 317 splx(s); 318#endif /* __NetBSD__ */ 319 return (0); 320 } 321 return (ENXIO); 322} 323 324/* 325 * Line specific close routine. 326 * Detach the tty from the sl unit. 327 */ 328void 329slclose(tp) 330 struct tty *tp; 331{ 332 struct sl_softc *sc; 333 int s; 334 335 ttywflush(tp); 336 sc = tp->t_sc; 337 338 if (sc != NULL) { 339#ifdef __HAVE_GENERIC_SOFT_INTERRUPTS 340 softintr_disestablish(sc->sc_si); 341#endif 342 s = splnet(); 343 if_down(&sc->sc_if); 344 IF_PURGE(&sc->sc_fastq); 345 splx(s); 346 347 s = spltty(); 348 tp->t_linesw = linesw[0]; /* default line disc. */ 349 tp->t_state = 0; 350 351 sc->sc_ttyp = NULL; 352 tp->t_sc = NULL; 353 354 m_freem(sc->sc_mbuf); 355 sc->sc_mbuf = NULL; 356 sc->sc_ep = sc->sc_mp = sc->sc_pktstart = NULL; 357 IF_PURGE(&sc->sc_inq); 358 359 /* 360 * If necessary, install a new outq buffer of the 361 * appropriate size. 362 */ 363 if (sc->sc_oldbufsize != 0) { 364 clfree(&tp->t_outq); 365 clalloc(&tp->t_outq, sc->sc_oldbufsize, 366 sc->sc_oldbufquot); 367 } 368 splx(s); 369 } 370} 371 372/* 373 * Line specific (tty) ioctl routine. 374 * Provide a way to get the sl unit number. 375 */ 376/* ARGSUSED */ 377int 378sltioctl(tp, cmd, data, flag) 379 struct tty *tp; 380 u_long cmd; 381 caddr_t data; 382 int flag; 383{ 384 struct sl_softc *sc = (struct sl_softc *)tp->t_sc; 385 386 switch (cmd) { 387 case SLIOCGUNIT: 388 *(int *)data = sc->sc_unit; /* XXX */ 389 break; 390 391 default: 392 return (-1); 393 } 394 return (0); 395} 396 397/* 398 * Queue a packet. Start transmission if not active. 399 * Compression happens in slintr(); if we do it here, IP TOS 400 * will cause us to not compress "background" packets, because 401 * ordering gets trashed. It can be done for all packets in slintr(). 402 */ 403int 404sloutput(ifp, m, dst, rtp) 405 struct ifnet *ifp; 406 struct mbuf *m; 407 struct sockaddr *dst; 408 struct rtentry *rtp; 409{ 410 struct sl_softc *sc = ifp->if_softc; 411 struct ip *ip; 412 int s, error; 413 ALTQ_DECL(struct altq_pktattr pktattr;) 414 415 IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family, &pktattr); 416 417 /* 418 * `Cannot happen' (see slioctl). Someday we will extend 419 * the line protocol to support other address families. 420 */ 421 if (dst->sa_family != AF_INET) { 422 printf("%s: af%d not supported\n", sc->sc_if.if_xname, 423 dst->sa_family); 424 m_freem(m); 425 sc->sc_if.if_noproto++; 426 return (EAFNOSUPPORT); 427 } 428 429 if (sc->sc_ttyp == NULL) { 430 m_freem(m); 431 return (ENETDOWN); /* sort of */ 432 } 433 if ((sc->sc_ttyp->t_state & TS_CARR_ON) == 0 && 434 (sc->sc_ttyp->t_cflag & CLOCAL) == 0) { 435 m_freem(m); 436 printf("%s: no carrier and not local\n", sc->sc_if.if_xname); 437 return (EHOSTUNREACH); 438 } 439 ip = mtod(m, struct ip *); 440 if (sc->sc_if.if_flags & SC_NOICMP && ip->ip_p == IPPROTO_ICMP) { 441 m_freem(m); 442 return (ENETRESET); /* XXX ? */ 443 } 444 445 s = spltty(); 446 if (sc->sc_oqlen && sc->sc_ttyp->t_outq.c_cc == sc->sc_oqlen) { 447 struct timeval tv; 448 449 /* if output's been stalled for too long, and restart */ 450 timersub(&time, &sc->sc_lastpacket, &tv); 451 if (tv.tv_sec > 0) { 452 sc->sc_otimeout++; 453 slstart(sc->sc_ttyp); 454 } 455 } 456 splx(s); 457 458 s = splnet(); 459 if ((ip->ip_tos & IPTOS_LOWDELAY) != 0 460#ifdef ALTQ 461 && ALTQ_IS_ENABLED(&ifp->if_snd) == 0 462#endif 463 ) { 464 if (IF_QFULL(&sc->sc_fastq)) { 465 IF_DROP(&sc->sc_fastq); 466 m_freem(m); 467 error = ENOBUFS; 468 } else { 469 IF_ENQUEUE(&sc->sc_fastq, m); 470 error = 0; 471 } 472 } else 473 IFQ_ENQUEUE(&ifp->if_snd, m, &pktattr, error); 474 if (error) { 475 splx(s); 476 ifp->if_oerrors++; 477 return (error); 478 } 479 sc->sc_lastpacket = time; 480 splx(s); 481 482 s = spltty(); 483 if ((sc->sc_oqlen = sc->sc_ttyp->t_outq.c_cc) == 0) 484 slstart(sc->sc_ttyp); 485 splx(s); 486 487 return (0); 488} 489 490/* 491 * Start output on interface. Get another datagram 492 * to send from the interface queue and map it to 493 * the interface before starting output. 494 */ 495void 496slstart(tp) 497 struct tty *tp; 498{ 499 struct sl_softc *sc = tp->t_sc; 500 501 /* 502 * If there is more in the output queue, just send it now. 503 * We are being called in lieu of ttstart and must do what 504 * it would. 505 */ 506 if (tp->t_outq.c_cc != 0) { 507 (*tp->t_oproc)(tp); 508 if (tp->t_outq.c_cc > SLIP_HIWAT) 509 return; 510 } 511 512 /* 513 * This happens briefly when the line shuts down. 514 */ 515 if (sc == NULL) 516 return; 517#ifdef __HAVE_GENERIC_SOFT_INTERRUPTS 518 softintr_schedule(sc->sc_si); 519#else 520 { 521 int s = splhigh(); 522 schednetisr(NETISR_SLIP); 523 splx(s); 524 } 525#endif 526} 527 528/* 529 * Copy data buffer to mbuf chain; add ifnet pointer. 530 */ 531static struct mbuf * 532sl_btom(sc, len) 533 struct sl_softc *sc; 534 int len; 535{ 536 struct mbuf *m; 537 538 /* 539 * Allocate a new input buffer and swap. 540 */ 541 m = sc->sc_mbuf; 542 MGETHDR(sc->sc_mbuf, M_DONTWAIT, MT_DATA); 543 if (sc->sc_mbuf == NULL) { 544 sc->sc_mbuf = m; 545 return (NULL); 546 } 547 MCLGET(sc->sc_mbuf, M_DONTWAIT); 548 if ((sc->sc_mbuf->m_flags & M_EXT) == 0) { 549 m_freem(sc->sc_mbuf); 550 sc->sc_mbuf = m; 551 return (NULL); 552 } 553 sc->sc_ep = (u_char *) sc->sc_mbuf->m_ext.ext_buf + 554 sc->sc_mbuf->m_ext.ext_size; 555 556 m->m_data = sc->sc_pktstart; 557 558 m->m_pkthdr.len = m->m_len = len; 559 m->m_pkthdr.rcvif = &sc->sc_if; 560 return (m); 561} 562 563/* 564 * tty interface receiver interrupt. 565 */ 566void 567slinput(c, tp) 568 int c; 569 struct tty *tp; 570{ 571 struct sl_softc *sc; 572 struct mbuf *m; 573 int len; 574 575 tk_nin++; 576 sc = (struct sl_softc *)tp->t_sc; 577 if (sc == NULL) 578 return; 579 if ((c & TTY_ERRORMASK) || ((tp->t_state & TS_CARR_ON) == 0 && 580 (tp->t_cflag & CLOCAL) == 0)) { 581 sc->sc_flags |= SC_ERROR; 582 return; 583 } 584 c &= TTY_CHARMASK; 585 586 ++sc->sc_if.if_ibytes; 587 588 if (sc->sc_if.if_flags & IFF_DEBUG) { 589 if (c == ABT_ESC) { 590 /* 591 * If we have a previous abort, see whether 592 * this one is within the time limit. 593 */ 594 if (sc->sc_abortcount && 595 time.tv_sec >= sc->sc_starttime + ABT_WINDOW) 596 sc->sc_abortcount = 0; 597 /* 598 * If we see an abort after "idle" time, count it; 599 * record when the first abort escape arrived. 600 */ 601 if (time.tv_sec >= sc->sc_lasttime + ABT_IDLE) { 602 if (++sc->sc_abortcount == 1) 603 sc->sc_starttime = time.tv_sec; 604 if (sc->sc_abortcount >= ABT_COUNT) { 605 slclose(tp); 606 return; 607 } 608 } 609 } else 610 sc->sc_abortcount = 0; 611 sc->sc_lasttime = time.tv_sec; 612 } 613 614 switch (c) { 615 616 case TRANS_FRAME_ESCAPE: 617 if (sc->sc_escape) 618 c = FRAME_ESCAPE; 619 break; 620 621 case TRANS_FRAME_END: 622 if (sc->sc_escape) 623 c = FRAME_END; 624 break; 625 626 case FRAME_ESCAPE: 627 sc->sc_escape = 1; 628 return; 629 630 case FRAME_END: 631 if(sc->sc_flags & SC_ERROR) { 632 sc->sc_flags &= ~SC_ERROR; 633 goto newpack; 634 } 635 len = sc->sc_mp - sc->sc_pktstart; 636 if (len < 3) 637 /* less than min length packet - ignore */ 638 goto newpack; 639 640 m = sl_btom(sc, len); 641 if (m == NULL) 642 goto error; 643 644 IF_ENQUEUE(&sc->sc_inq, m); 645#ifdef __HAVE_GENERIC_SOFT_INTERRUPTS 646 softintr_schedule(sc->sc_si); 647#else 648 { 649 int s = splhigh(); 650 schednetisr(NETISR_SLIP); 651 splx(s); 652 } 653#endif 654 goto newpack; 655 } 656 if (sc->sc_mp < sc->sc_ep) { 657 *sc->sc_mp++ = c; 658 sc->sc_escape = 0; 659 return; 660 } 661 662 /* can't put lower; would miss an extra frame */ 663 sc->sc_flags |= SC_ERROR; 664 665error: 666 sc->sc_if.if_ierrors++; 667newpack: 668 sc->sc_mp = sc->sc_pktstart = (u_char *) sc->sc_mbuf->m_ext.ext_buf + 669 BUFOFFSET; 670 sc->sc_escape = 0; 671} 672 673#ifndef __HAVE_GENERIC_SOFT_INTERRUPTS 674void 675slnetisr(void) 676{ 677 struct sl_softc *sc; 678 int i; 679 680 for (i = 0; i < NSL; i++) { 681 sc = &sl_softc[i]; 682 if (sc->sc_ttyp == NULL) 683 continue; 684 slintr(sc); 685 } 686} 687#endif 688 689void 690slintr(void *arg) 691{ 692 struct sl_softc *sc = arg; 693 struct tty *tp = sc->sc_ttyp; 694 struct mbuf *m; 695 int s, len; 696 u_char *pktstart, c; 697#if NBPFILTER > 0 698 u_char chdr[CHDR_LEN]; 699#endif 700 701 KASSERT(tp != NULL); 702 703 /* 704 * Output processing loop. 705 */ 706 for (;;) { 707 struct ip *ip; 708 struct mbuf *m2; 709#if NBPFILTER > 0 710 struct mbuf *bpf_m; 711#endif 712 713 /* 714 * Do not remove the packet from the queue if it 715 * doesn't look like it will fit into the current 716 * serial output queue. With a packet full of 717 * escapes, this could be as bad as MTU*2+2. 718 */ 719 s = spltty(); 720 if (tp->t_outq.c_cn - tp->t_outq.c_cc < 721 2*sc->sc_if.if_mtu+2) { 722 splx(s); 723 break; 724 } 725 splx(s); 726 727 /* 728 * Get a packet and send it to the interface. 729 */ 730 s = splnet(); 731 IF_DEQUEUE(&sc->sc_fastq, m); 732 if (m) 733 sc->sc_if.if_omcasts++; /* XXX */ 734 else 735 IFQ_DEQUEUE(&sc->sc_if.if_snd, m); 736 splx(s); 737 738 if (m == NULL) 739 break; 740 741 /* 742 * We do the header compression here rather than in 743 * sloutput() because the packets will be out of order 744 * if we are using TOS queueing, and the connection 745 * ID compression will get munged when this happens. 746 */ 747#if NBPFILTER > 0 748 if (sc->sc_if.if_bpf) { 749 /* 750 * We need to save the TCP/IP header before 751 * it's compressed. To avoid complicated 752 * code, we just make a deep copy of the 753 * entire packet (since this is a serial 754 * line, packets should be short and/or the 755 * copy should be negligible cost compared 756 * to the packet transmission time). 757 */ 758 bpf_m = m_dup(m, 0, M_COPYALL, M_DONTWAIT); 759 } else 760 bpf_m = NULL; 761#endif 762 if ((ip = mtod(m, struct ip *))->ip_p == IPPROTO_TCP) { 763 if (sc->sc_if.if_flags & SC_COMPRESS) 764 *mtod(m, u_char *) |= 765 sl_compress_tcp(m, ip, 766 &sc->sc_comp, 1); 767 } 768#if NBPFILTER > 0 769 if (sc->sc_if.if_bpf && bpf_m != NULL) { 770 /* 771 * Put the SLIP pseudo-"link header" in 772 * place. The compressed header is now 773 * at the beginning of the mbuf. 774 */ 775 struct mbuf n; 776 u_char *hp; 777 778 n.m_next = bpf_m; 779 n.m_data = n.m_dat; 780 n.m_len = SLIP_HDRLEN; 781 782 hp = mtod(&n, u_char *); 783 784 hp[SLX_DIR] = SLIPDIR_OUT; 785 memcpy(&hp[SLX_CHDR], mtod(m, caddr_t), 786 CHDR_LEN); 787 788 s = splnet(); 789 bpf_mtap(sc->sc_if.if_bpf, &n); 790 splx(s); 791 m_freem(bpf_m); 792 } 793#endif 794 sc->sc_lastpacket = time; 795 796 s = spltty(); 797 798 /* 799 * The extra FRAME_END will start up a new packet, 800 * and thus will flush any accumulated garbage. We 801 * do this whenever the line may have been idle for 802 * some time. 803 */ 804 if (tp->t_outq.c_cc == 0) { 805 sc->sc_if.if_obytes++; 806 (void) putc(FRAME_END, &tp->t_outq); 807 } 808 809 while (m) { 810 u_char *bp, *cp, *ep; 811 812 bp = cp = mtod(m, u_char *); 813 ep = cp + m->m_len; 814 while (cp < ep) { 815 /* 816 * Find out how many bytes in the 817 * string we can handle without 818 * doing something special. 819 */ 820 while (cp < ep) { 821 switch (*cp++) { 822 case FRAME_ESCAPE: 823 case FRAME_END: 824 cp--; 825 goto out; 826 } 827 } 828 out: 829 if (cp > bp) { 830 /* 831 * Put N characters at once 832 * into the tty output queue. 833 */ 834 if (b_to_q(bp, cp - bp, 835 &tp->t_outq)) 836 break; 837 sc->sc_if.if_obytes += cp - bp; 838 } 839 /* 840 * If there are characters left in 841 * the mbuf, the first one must be 842 * special.. Put it out in a different 843 * form. 844 */ 845 if (cp < ep) { 846 if (putc(FRAME_ESCAPE, 847 &tp->t_outq)) 848 break; 849 if (putc(*cp++ == FRAME_ESCAPE ? 850 TRANS_FRAME_ESCAPE : 851 TRANS_FRAME_END, 852 &tp->t_outq)) { 853 (void) 854 unputc(&tp->t_outq); 855 break; 856 } 857 sc->sc_if.if_obytes += 2; 858 } 859 bp = cp; 860 } 861 MFREE(m, m2); 862 m = m2; 863 } 864 865 if (putc(FRAME_END, &tp->t_outq)) { 866 /* 867 * Not enough room. Remove a char to make 868 * room and end the packet normally. If 869 * you get many collisions (more than one 870 * or two a day), you probably do not have 871 * enough clists and you should increase 872 * "nclist" in param.c 873 */ 874 (void) unputc(&tp->t_outq); 875 (void) putc(FRAME_END, &tp->t_outq); 876 sc->sc_if.if_collisions++; 877 } else { 878 sc->sc_if.if_obytes++; 879 sc->sc_if.if_opackets++; 880 } 881 882 /* 883 * We now have characters in the output queue, 884 * kick the serial port. 885 */ 886 (*tp->t_oproc)(tp); 887 splx(s); 888 } 889 890 /* 891 * Input processing loop. 892 */ 893 for (;;) { 894 s = spltty(); 895 IF_DEQUEUE(&sc->sc_inq, m); 896 splx(s); 897 if (m == NULL) 898 break; 899 pktstart = mtod(m, u_char *); 900 len = m->m_pkthdr.len; 901#if NBPFILTER > 0 902 if (sc->sc_if.if_bpf) { 903 /* 904 * Save the compressed header, so we 905 * can tack it on later. Note that we 906 * will end up copying garbage in some 907 * cases but this is okay. We remember 908 * where the buffer started so we can 909 * compute the new header length. 910 */ 911 memcpy(chdr, pktstart, CHDR_LEN); 912 } 913#endif /* NBPFILTER > 0 */ 914 if ((c = (*pktstart & 0xf0)) != (IPVERSION << 4)) { 915 if (c & 0x80) 916 c = TYPE_COMPRESSED_TCP; 917 else if (c == TYPE_UNCOMPRESSED_TCP) 918 *pktstart &= 0x4f; /* XXX */ 919 /* 920 * We've got something that's not an IP 921 * packet. If compression is enabled, 922 * try to decompress it. Otherwise, if 923 * `auto-enable' compression is on and 924 * it's a reasonable packet, decompress 925 * it and then enable compression. 926 * Otherwise, drop it. 927 */ 928 if (sc->sc_if.if_flags & SC_COMPRESS) { 929 len = sl_uncompress_tcp(&pktstart, len, 930 (u_int)c, &sc->sc_comp); 931 if (len <= 0) { 932 m_freem(m); 933 continue; 934 } 935 } else if ((sc->sc_if.if_flags & SC_AUTOCOMP) && 936 c == TYPE_UNCOMPRESSED_TCP && len >= 40) { 937 len = sl_uncompress_tcp(&pktstart, len, 938 (u_int)c, &sc->sc_comp); 939 if (len <= 0) { 940 m_freem(m); 941 continue; 942 } 943 sc->sc_if.if_flags |= SC_COMPRESS; 944 } else { 945 m_freem(m); 946 continue; 947 } 948 } 949 m->m_data = (caddr_t) pktstart; 950 m->m_pkthdr.len = m->m_len = len; 951#if NBPFILTER > 0 952 if (sc->sc_if.if_bpf) { 953 /* 954 * Put the SLIP pseudo-"link header" in place. 955 * Note this M_PREPEND() should bever fail, 956 * since we know we always have enough space 957 * in the input buffer. 958 */ 959 u_char *hp; 960 961 M_PREPEND(m, SLIP_HDRLEN, M_DONTWAIT); 962 if (m == NULL) 963 continue; 964 965 hp = mtod(m, u_char *); 966 hp[SLX_DIR] = SLIPDIR_IN; 967 memcpy(&hp[SLX_CHDR], chdr, CHDR_LEN); 968 969 s = splnet(); 970 bpf_mtap(sc->sc_if.if_bpf, m); 971 splx(s); 972 973 m_adj(m, SLIP_HDRLEN); 974 } 975#endif /* NBPFILTER > 0 */ 976 /* 977 * If the packet will fit into a single 978 * header mbuf, copy it into one, to save 979 * memory. 980 */ 981 if (m->m_pkthdr.len < MHLEN) { 982 struct mbuf *n; 983 984 MGETHDR(n, M_DONTWAIT, MT_DATA); 985 M_COPY_PKTHDR(n, m); 986 memcpy(mtod(n, caddr_t), mtod(m, caddr_t), 987 m->m_pkthdr.len); 988 n->m_len = m->m_len; 989 m_freem(m); 990 m = n; 991 } 992 993 sc->sc_if.if_ipackets++; 994 sc->sc_lastpacket = time; 995 996 s = splnet(); 997 if (IF_QFULL(&ipintrq)) { 998 IF_DROP(&ipintrq); 999 sc->sc_if.if_ierrors++; 1000 sc->sc_if.if_iqdrops++; 1001 m_freem(m); 1002 } else { 1003 IF_ENQUEUE(&ipintrq, m); 1004 schednetisr(NETISR_IP); 1005 } 1006 splx(s); 1007 } 1008} 1009 1010/* 1011 * Process an ioctl request. 1012 */ 1013int 1014slioctl(ifp, cmd, data) 1015 struct ifnet *ifp; 1016 u_long cmd; 1017 caddr_t data; 1018{ 1019 struct ifaddr *ifa = (struct ifaddr *)data; 1020 struct ifreq *ifr = (struct ifreq *)data; 1021 int s = splnet(), error = 0; 1022 struct sl_softc *sc = ifp->if_softc; 1023 1024 switch (cmd) { 1025 1026 case SIOCSIFADDR: 1027 if (ifa->ifa_addr->sa_family == AF_INET) 1028 ifp->if_flags |= IFF_UP; 1029 else 1030 error = EAFNOSUPPORT; 1031 break; 1032 1033 case SIOCSIFDSTADDR: 1034 if (ifa->ifa_addr->sa_family != AF_INET) 1035 error = EAFNOSUPPORT; 1036 break; 1037 1038 case SIOCSIFMTU: 1039 if ((ifr->ifr_mtu < 3) || (ifr->ifr_mtu > SLMAX)) { 1040 error = EINVAL; 1041 break; 1042 } 1043 sc->sc_if.if_mtu = ifr->ifr_mtu; 1044 break; 1045 1046 case SIOCGIFMTU: 1047 ifr->ifr_mtu = sc->sc_if.if_mtu; 1048 break; 1049 1050 case SIOCADDMULTI: 1051 case SIOCDELMULTI: 1052 if (ifr == 0) { 1053 error = EAFNOSUPPORT; /* XXX */ 1054 break; 1055 } 1056 switch (ifr->ifr_addr.sa_family) { 1057 1058#ifdef INET 1059 case AF_INET: 1060 break; 1061#endif 1062 1063 default: 1064 error = EAFNOSUPPORT; 1065 break; 1066 } 1067 break; 1068 1069 default: 1070 error = EINVAL; 1071 } 1072 splx(s); 1073 return (error); 1074} 1075#endif 1076