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