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