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