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