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