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