Home | History | Annotate | Line # | Download | only in net
if_sl.c revision 1.72.2.5
      1 /*	$NetBSD: if_sl.c,v 1.72.2.5 2001/11/14 19:17:24 nathanw 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.72.2.5 2001/11/14 19:17:24 nathanw 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/lwp.h>
     77 #include <sys/proc.h>
     78 #include <sys/malloc.h>
     79 #include <sys/mbuf.h>
     80 #include <sys/buf.h>
     81 #include <sys/dkstat.h>
     82 #include <sys/socket.h>
     83 #include <sys/ioctl.h>
     84 #include <sys/file.h>
     85 #include <sys/conf.h>
     86 #include <sys/tty.h>
     87 #include <sys/kernel.h>
     88 #if __NetBSD__
     89 #include <sys/systm.h>
     90 #endif
     91 
     92 #include <machine/cpu.h>
     93 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
     94 #include <machine/intr.h>
     95 #endif
     96 
     97 #include <net/if.h>
     98 #include <net/if_types.h>
     99 #include <net/netisr.h>
    100 #include <net/route.h>
    101 
    102 #ifdef INET
    103 #include <netinet/in.h>
    104 #include <netinet/in_systm.h>
    105 #include <netinet/in_var.h>
    106 #include <netinet/ip.h>
    107 #else
    108 #error Slip without inet?
    109 #endif
    110 
    111 #include <net/slcompress.h>
    112 #include <net/if_slvar.h>
    113 #include <net/slip.h>
    114 
    115 #if NBPFILTER > 0
    116 #include <sys/time.h>
    117 #include <net/bpf.h>
    118 #endif
    119 
    120 /*
    121  * SLMAX is a hard limit on input packet size.  To simplify the code
    122  * and improve performance, we require that packets fit in an mbuf
    123  * cluster, and if we get a compressed packet, there's enough extra
    124  * room to expand the header into a max length tcp/ip header (128
    125  * bytes).  So, SLMAX can be at most
    126  *	MCLBYTES - 128
    127  *
    128  * SLMTU is a hard limit on output packet size.  To insure good
    129  * interactive response, SLMTU wants to be the smallest size that
    130  * amortizes the header cost.  (Remember that even with
    131  * type-of-service queuing, we have to wait for any in-progress
    132  * packet to finish.  I.e., we wait, on the average, 1/2 * mtu /
    133  * cps, where cps is the line speed in characters per second.
    134  * E.g., 533ms wait for a 1024 byte MTU on a 9600 baud line.  The
    135  * average compressed header size is 6-8 bytes so any MTU > 90
    136  * bytes will give us 90% of the line bandwidth.  A 100ms wait is
    137  * tolerable (500ms is not), so want an MTU around 296.  (Since TCP
    138  * will send 256 byte segments (to allow for 40 byte headers), the
    139  * typical packet size on the wire will be around 260 bytes).  In
    140  * 4.3tahoe+ systems, we can set an MTU in a route so we do that &
    141  * leave the interface MTU relatively high (so we don't IP fragment
    142  * when acting as a gateway to someone using a stupid MTU).
    143  *
    144  * Similar considerations apply to SLIP_HIWAT:  It's the amount of
    145  * data that will be queued 'downstream' of us (i.e., in clists
    146  * waiting to be picked up by the tty output interrupt).  If we
    147  * queue a lot of data downstream, it's immune to our t.o.s. queuing.
    148  * E.g., if SLIP_HIWAT is 1024, the interactive traffic in mixed
    149  * telnet/ftp will see a 1 sec wait, independent of the mtu (the
    150  * wait is dependent on the ftp window size but that's typically
    151  * 1k - 4k).  So, we want SLIP_HIWAT just big enough to amortize
    152  * the cost (in idle time on the wire) of the tty driver running
    153  * off the end of its clists & having to call back slstart for a
    154  * new packet.  For a tty interface with any buffering at all, this
    155  * cost will be zero.  Even with a totally brain dead interface (like
    156  * the one on a typical workstation), the cost will be <= 1 character
    157  * time.  So, setting SLIP_HIWAT to ~100 guarantees that we'll lose
    158  * at most 1% while maintaining good interactive response.
    159  */
    160 #define	BUFOFFSET	(128+sizeof(struct ifnet **)+SLIP_HDRLEN)
    161 #define	SLMAX		(MCLBYTES - BUFOFFSET)
    162 #define	SLBUFSIZE	(SLMAX + BUFOFFSET)
    163 #ifndef SLMTU
    164 #define	SLMTU		296
    165 #endif
    166 #if (SLMTU < 3)
    167 #error SLMTU way too small.
    168 #endif
    169 #define	SLIP_HIWAT	roundup(50,CBSIZE)
    170 #ifndef __NetBSD__					/* XXX - cgd */
    171 #define	CLISTRESERVE	1024	/* Can't let clists get too low */
    172 #endif	/* !__NetBSD__ */
    173 
    174 /*
    175  * SLIP ABORT ESCAPE MECHANISM:
    176  *	(inspired by HAYES modem escape arrangement)
    177  *	1sec escape 1sec escape 1sec escape { 1sec escape 1sec escape }
    178  *	within window time signals a "soft" exit from slip mode by remote end
    179  *	if the IFF_DEBUG flag is on.
    180  */
    181 #define	ABT_ESC		'\033'	/* can't be t_intr - distant host must know it*/
    182 #define	ABT_IDLE	1	/* in seconds - idle before an escape */
    183 #define	ABT_COUNT	3	/* count of escapes for abort */
    184 #define	ABT_WINDOW	(ABT_COUNT*2+2)	/* in seconds - time to count */
    185 
    186 struct sl_softc sl_softc[NSL];
    187 
    188 #define FRAME_END	 	0xc0		/* Frame End */
    189 #define FRAME_ESCAPE		0xdb		/* Frame Esc */
    190 #define TRANS_FRAME_END	 	0xdc		/* transposed frame end */
    191 #define TRANS_FRAME_ESCAPE 	0xdd		/* transposed frame esc */
    192 
    193 #ifndef __HAVE_GENERIC_SOFT_INTERRUPTS
    194 void	slnetisr(void);
    195 #endif
    196 void	slintr(void *);
    197 
    198 static int slinit __P((struct sl_softc *));
    199 static struct mbuf *sl_btom __P((struct sl_softc *, int));
    200 
    201 /*
    202  * Called from boot code to establish sl interfaces.
    203  */
    204 void
    205 slattach()
    206 {
    207 	struct sl_softc *sc;
    208 	int i = 0;
    209 
    210 	for (sc = sl_softc; i < NSL; sc++) {
    211 		sc->sc_unit = i;		/* XXX */
    212 		sprintf(sc->sc_if.if_xname, "sl%d", i++);
    213 		sc->sc_if.if_softc = sc;
    214 		sc->sc_if.if_mtu = SLMTU;
    215 		sc->sc_if.if_flags =
    216 		    IFF_POINTOPOINT | SC_AUTOCOMP | IFF_MULTICAST;
    217 		sc->sc_if.if_type = IFT_SLIP;
    218 		sc->sc_if.if_ioctl = slioctl;
    219 		sc->sc_if.if_output = sloutput;
    220 		sc->sc_if.if_dlt = DLT_SLIP;
    221 		sc->sc_fastq.ifq_maxlen = 32;
    222 		IFQ_SET_READY(&sc->sc_if.if_snd);
    223 		if_attach(&sc->sc_if);
    224 		if_alloc_sadl(&sc->sc_if);
    225 #if NBPFILTER > 0
    226 		bpfattach(&sc->sc_if, DLT_SLIP, SLIP_HDRLEN);
    227 #endif
    228 	}
    229 }
    230 
    231 static int
    232 slinit(sc)
    233 	struct sl_softc *sc;
    234 {
    235 
    236 	if (sc->sc_mbuf == NULL) {
    237 		MGETHDR(sc->sc_mbuf, M_WAIT, MT_DATA);
    238 		MCLGET(sc->sc_mbuf, M_WAIT);
    239 	}
    240 	sc->sc_ep = (u_char *) sc->sc_mbuf->m_ext.ext_buf +
    241 	    sc->sc_mbuf->m_ext.ext_size;
    242 	sc->sc_mp = sc->sc_pktstart = (u_char *) sc->sc_mbuf->m_ext.ext_buf +
    243 	    BUFOFFSET;
    244 
    245 	sl_compress_init(&sc->sc_comp);
    246 
    247 	return (1);
    248 }
    249 
    250 /*
    251  * Line specific open routine.
    252  * Attach the given tty to the first available sl unit.
    253  */
    254 /* ARGSUSED */
    255 int
    256 slopen(dev, tp)
    257 	dev_t dev;
    258 	struct tty *tp;
    259 {
    260 	struct proc *p = curproc->l_proc;		/* XXX */
    261 	struct sl_softc *sc;
    262 	int nsl;
    263 	int error;
    264 	int s;
    265 
    266 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    267 		return (error);
    268 
    269 	if (tp->t_linesw->l_no == SLIPDISC)
    270 		return (0);
    271 
    272 	for (nsl = NSL, sc = sl_softc; --nsl >= 0; sc++)
    273 		if (sc->sc_ttyp == NULL) {
    274 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
    275 			sc->sc_si = softintr_establish(IPL_SOFTNET,
    276 			    slintr, sc);
    277 			if (sc->sc_si == NULL)
    278 				return (ENOMEM);
    279 #endif
    280 			if (slinit(sc) == 0) {
    281 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
    282 				softintr_disestablish(sc->sc_si);
    283 #endif
    284 				return (ENOBUFS);
    285 			}
    286 			tp->t_sc = (caddr_t)sc;
    287 			sc->sc_ttyp = tp;
    288 			sc->sc_if.if_baudrate = tp->t_ospeed;
    289 			s = spltty();
    290 			tp->t_state |= TS_ISOPEN | TS_XCLUDE;
    291 			splx(s);
    292 			ttyflush(tp, FREAD | FWRITE);
    293 #ifdef __NetBSD__
    294 			/*
    295 			 * make sure tty output queue is large enough
    296 			 * to hold a full-sized packet (including frame
    297 			 * end, and a possible extra frame end).  full-sized
    298 			 * packet occupies a max of 2*SLMAX bytes (because
    299 			 * of possible escapes), and add two on for frame
    300 			 * ends.
    301 			 */
    302 			s = spltty();
    303 			if (tp->t_outq.c_cn < 2*SLMAX+2) {
    304 				sc->sc_oldbufsize = tp->t_outq.c_cn;
    305 				sc->sc_oldbufquot = tp->t_outq.c_cq != 0;
    306 
    307 				clfree(&tp->t_outq);
    308 				error = clalloc(&tp->t_outq, 2*SLMAX+2, 0);
    309 				if (error) {
    310 					splx(s);
    311 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
    312 					softintr_disestablish(sc->sc_si);
    313 #endif
    314 					return(error);
    315 				}
    316 			} else
    317 				sc->sc_oldbufsize = sc->sc_oldbufquot = 0;
    318 			splx(s);
    319 #endif /* __NetBSD__ */
    320 			return (0);
    321 		}
    322 	return (ENXIO);
    323 }
    324 
    325 /*
    326  * Line specific close routine.
    327  * Detach the tty from the sl unit.
    328  */
    329 void
    330 slclose(tp)
    331 	struct tty *tp;
    332 {
    333 	struct sl_softc *sc;
    334 	int s;
    335 
    336 	ttywflush(tp);
    337 	sc = tp->t_sc;
    338 
    339 	if (sc != NULL) {
    340 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
    341 		softintr_disestablish(sc->sc_si);
    342 #endif
    343 		s = splnet();
    344 		if_down(&sc->sc_if);
    345 		IF_PURGE(&sc->sc_fastq);
    346 		splx(s);
    347 
    348 		s = spltty();
    349 		tp->t_linesw = linesw[0];	/* default line disc. */
    350 		tp->t_state = 0;
    351 
    352 		sc->sc_ttyp = NULL;
    353 		tp->t_sc = NULL;
    354 
    355 		m_freem(sc->sc_mbuf);
    356 		sc->sc_mbuf = NULL;
    357 		sc->sc_ep = sc->sc_mp = sc->sc_pktstart = NULL;
    358 		IF_PURGE(&sc->sc_inq);
    359 
    360 		/*
    361 		 * If necessary, install a new outq buffer of the
    362 		 * appropriate size.
    363 		 */
    364 		if (sc->sc_oldbufsize != 0) {
    365 			clfree(&tp->t_outq);
    366 			clalloc(&tp->t_outq, sc->sc_oldbufsize,
    367 			    sc->sc_oldbufquot);
    368 		}
    369 		splx(s);
    370 	}
    371 }
    372 
    373 /*
    374  * Line specific (tty) ioctl routine.
    375  * Provide a way to get the sl unit number.
    376  */
    377 /* ARGSUSED */
    378 int
    379 sltioctl(tp, cmd, data, flag)
    380 	struct tty *tp;
    381 	u_long cmd;
    382 	caddr_t data;
    383 	int flag;
    384 {
    385 	struct sl_softc *sc = (struct sl_softc *)tp->t_sc;
    386 
    387 	switch (cmd) {
    388 	case SLIOCGUNIT:
    389 		*(int *)data = sc->sc_unit;	/* XXX */
    390 		break;
    391 
    392 	default:
    393 		return (-1);
    394 	}
    395 	return (0);
    396 }
    397 
    398 /*
    399  * Queue a packet.  Start transmission if not active.
    400  * Compression happens in slintr(); if we do it here, IP TOS
    401  * will cause us to not compress "background" packets, because
    402  * ordering gets trashed.  It can be done for all packets in slintr().
    403  */
    404 int
    405 sloutput(ifp, m, dst, rtp)
    406 	struct ifnet *ifp;
    407 	struct mbuf *m;
    408 	struct sockaddr *dst;
    409 	struct rtentry *rtp;
    410 {
    411 	struct sl_softc *sc = ifp->if_softc;
    412 	struct ip *ip;
    413 	int s, error;
    414 	ALTQ_DECL(struct altq_pktattr pktattr;)
    415 
    416 	IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family, &pktattr);
    417 
    418 	/*
    419 	 * `Cannot happen' (see slioctl).  Someday we will extend
    420 	 * the line protocol to support other address families.
    421 	 */
    422 	if (dst->sa_family != AF_INET) {
    423 		printf("%s: af%d not supported\n", sc->sc_if.if_xname,
    424 		    dst->sa_family);
    425 		m_freem(m);
    426 		sc->sc_if.if_noproto++;
    427 		return (EAFNOSUPPORT);
    428 	}
    429 
    430 	if (sc->sc_ttyp == NULL) {
    431 		m_freem(m);
    432 		return (ENETDOWN);	/* sort of */
    433 	}
    434 	if ((sc->sc_ttyp->t_state & TS_CARR_ON) == 0 &&
    435 	    (sc->sc_ttyp->t_cflag & CLOCAL) == 0) {
    436 		m_freem(m);
    437 		printf("%s: no carrier and not local\n", sc->sc_if.if_xname);
    438 		return (EHOSTUNREACH);
    439 	}
    440 	ip = mtod(m, struct ip *);
    441 	if (sc->sc_if.if_flags & SC_NOICMP && ip->ip_p == IPPROTO_ICMP) {
    442 		m_freem(m);
    443 		return (ENETRESET);		/* XXX ? */
    444 	}
    445 
    446 	s = spltty();
    447 	if (sc->sc_oqlen && sc->sc_ttyp->t_outq.c_cc == sc->sc_oqlen) {
    448 		struct timeval tv;
    449 
    450 		/* if output's been stalled for too long, and restart */
    451 		timersub(&time, &sc->sc_lastpacket, &tv);
    452 		if (tv.tv_sec > 0) {
    453 			sc->sc_otimeout++;
    454 			slstart(sc->sc_ttyp);
    455 		}
    456 	}
    457 	splx(s);
    458 
    459 	s = splnet();
    460 	if ((ip->ip_tos & IPTOS_LOWDELAY) != 0
    461 #ifdef ALTQ
    462 	    && ALTQ_IS_ENABLED(&ifp->if_snd) == 0
    463 #endif
    464 	    ) {
    465 		if (IF_QFULL(&sc->sc_fastq)) {
    466 			IF_DROP(&sc->sc_fastq);
    467 			m_freem(m);
    468 			error = ENOBUFS;
    469 		} else {
    470 			IF_ENQUEUE(&sc->sc_fastq, m);
    471 			error = 0;
    472 		}
    473 	} else
    474 		IFQ_ENQUEUE(&ifp->if_snd, m, &pktattr, error);
    475 	if (error) {
    476 		splx(s);
    477 		ifp->if_oerrors++;
    478 		return (error);
    479 	}
    480 	sc->sc_lastpacket = time;
    481 	splx(s);
    482 
    483 	s = spltty();
    484 	if ((sc->sc_oqlen = sc->sc_ttyp->t_outq.c_cc) == 0)
    485 		slstart(sc->sc_ttyp);
    486 	splx(s);
    487 
    488 	return (0);
    489 }
    490 
    491 /*
    492  * Start output on interface.  Get another datagram
    493  * to send from the interface queue and map it to
    494  * the interface before starting output.
    495  */
    496 void
    497 slstart(tp)
    498 	struct tty *tp;
    499 {
    500 	struct sl_softc *sc = tp->t_sc;
    501 
    502 	/*
    503 	 * If there is more in the output queue, just send it now.
    504 	 * We are being called in lieu of ttstart and must do what
    505 	 * it would.
    506 	 */
    507 	if (tp->t_outq.c_cc != 0) {
    508 		(*tp->t_oproc)(tp);
    509 		if (tp->t_outq.c_cc > SLIP_HIWAT)
    510 			return;
    511 	}
    512 
    513 	/*
    514 	 * This happens briefly when the line shuts down.
    515 	 */
    516 	if (sc == NULL)
    517 		return;
    518 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
    519 	softintr_schedule(sc->sc_si);
    520 #else
    521     {
    522 	int s = splhigh();
    523 	schednetisr(NETISR_SLIP);
    524 	splx(s);
    525     }
    526 #endif
    527 }
    528 
    529 /*
    530  * Copy data buffer to mbuf chain; add ifnet pointer.
    531  */
    532 static struct mbuf *
    533 sl_btom(sc, len)
    534 	struct sl_softc *sc;
    535 	int len;
    536 {
    537 	struct mbuf *m;
    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 = splhigh();
    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_lastpacket = 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 				bp = cp;
    861 			}
    862 			MFREE(m, m2);
    863 			m = m2;
    864 		}
    865 
    866 		if (putc(FRAME_END, &tp->t_outq)) {
    867 			/*
    868 			 * Not enough room.  Remove a char to make
    869 			 * room and end the packet normally.  If
    870 			 * you get many collisions (more than one
    871 			 * or two a day), you probably do not have
    872 			 * enough clists and you should increase
    873 			 * "nclist" in param.c
    874 			 */
    875 			(void) unputc(&tp->t_outq);
    876 			(void) putc(FRAME_END, &tp->t_outq);
    877 			sc->sc_if.if_collisions++;
    878 		} else {
    879 			sc->sc_if.if_obytes++;
    880 			sc->sc_if.if_opackets++;
    881 		}
    882 
    883 		/*
    884 		 * We now have characters in the output queue,
    885 		 * kick the serial port.
    886 		 */
    887 		(*tp->t_oproc)(tp);
    888 		splx(s);
    889 	}
    890 
    891 	/*
    892 	 * Input processing loop.
    893 	 */
    894 	for (;;) {
    895 		s = spltty();
    896 		IF_DEQUEUE(&sc->sc_inq, m);
    897 		splx(s);
    898 		if (m == NULL)
    899 			break;
    900 		pktstart = mtod(m, u_char *);
    901 		len = m->m_pkthdr.len;
    902 #if NBPFILTER > 0
    903 		if (sc->sc_if.if_bpf) {
    904 			/*
    905 			 * Save the compressed header, so we
    906 			 * can tack it on later.  Note that we
    907 			 * will end up copying garbage in some
    908 			 * cases but this is okay.  We remember
    909 			 * where the buffer started so we can
    910 			 * compute the new header length.
    911 			 */
    912 			memcpy(chdr, pktstart, CHDR_LEN);
    913 		}
    914 #endif /* NBPFILTER > 0 */
    915 		if ((c = (*pktstart & 0xf0)) != (IPVERSION << 4)) {
    916 			if (c & 0x80)
    917 				c = TYPE_COMPRESSED_TCP;
    918 			else if (c == TYPE_UNCOMPRESSED_TCP)
    919 				*pktstart &= 0x4f; /* XXX */
    920 			/*
    921 			 * We've got something that's not an IP
    922 			 * packet.  If compression is enabled,
    923 			 * try to decompress it.  Otherwise, if
    924 			 * `auto-enable' compression is on and
    925 			 * it's a reasonable packet, decompress
    926 			 * it and then enable compression.
    927 			 * Otherwise, drop it.
    928 			 */
    929 			if (sc->sc_if.if_flags & SC_COMPRESS) {
    930 				len = sl_uncompress_tcp(&pktstart, len,
    931 				    (u_int)c, &sc->sc_comp);
    932 				if (len <= 0) {
    933 					m_freem(m);
    934 					continue;
    935 				}
    936 			} else if ((sc->sc_if.if_flags & SC_AUTOCOMP) &&
    937 			    c == TYPE_UNCOMPRESSED_TCP && len >= 40) {
    938 				len = sl_uncompress_tcp(&pktstart, len,
    939 				    (u_int)c, &sc->sc_comp);
    940 				if (len <= 0) {
    941 					m_freem(m);
    942 					continue;
    943 				}
    944 				sc->sc_if.if_flags |= SC_COMPRESS;
    945 			} else {
    946 				m_freem(m);
    947 				continue;
    948 			}
    949 		}
    950 		m->m_data = (caddr_t) pktstart;
    951 		m->m_pkthdr.len = m->m_len = len;
    952 #if NBPFILTER > 0
    953 		if (sc->sc_if.if_bpf) {
    954 			/*
    955 			 * Put the SLIP pseudo-"link header" in place.
    956 			 * Note this M_PREPEND() should bever fail,
    957 			 * since we know we always have enough space
    958 			 * in the input buffer.
    959 			 */
    960 			u_char *hp;
    961 
    962 			M_PREPEND(m, SLIP_HDRLEN, M_DONTWAIT);
    963 			if (m == NULL)
    964 				continue;
    965 
    966 			hp = mtod(m, u_char *);
    967 			hp[SLX_DIR] = SLIPDIR_IN;
    968 			memcpy(&hp[SLX_CHDR], chdr, CHDR_LEN);
    969 
    970 			s = splnet();
    971 			bpf_mtap(sc->sc_if.if_bpf, m);
    972 			splx(s);
    973 
    974 			m_adj(m, SLIP_HDRLEN);
    975 		}
    976 #endif /* NBPFILTER > 0 */
    977 		/*
    978 		 * If the packet will fit into a single
    979 		 * header mbuf, copy it into one, to save
    980 		 * memory.
    981 		 */
    982 		if (m->m_pkthdr.len < MHLEN) {
    983 			struct mbuf *n;
    984 
    985 			MGETHDR(n, M_DONTWAIT, MT_DATA);
    986 			M_COPY_PKTHDR(n, m);
    987 			memcpy(mtod(n, caddr_t), mtod(m, caddr_t),
    988 			    m->m_pkthdr.len);
    989 			n->m_len = m->m_len;
    990 			m_freem(m);
    991 			m = n;
    992 		}
    993 
    994 		sc->sc_if.if_ipackets++;
    995 		sc->sc_lastpacket = time;
    996 
    997 		s = splnet();
    998 		if (IF_QFULL(&ipintrq)) {
    999 			IF_DROP(&ipintrq);
   1000 			sc->sc_if.if_ierrors++;
   1001 			sc->sc_if.if_iqdrops++;
   1002 			m_freem(m);
   1003 		} else {
   1004 			IF_ENQUEUE(&ipintrq, m);
   1005 			schednetisr(NETISR_IP);
   1006 		}
   1007 		splx(s);
   1008 	}
   1009 }
   1010 
   1011 /*
   1012  * Process an ioctl request.
   1013  */
   1014 int
   1015 slioctl(ifp, cmd, data)
   1016 	struct ifnet *ifp;
   1017 	u_long cmd;
   1018 	caddr_t data;
   1019 {
   1020 	struct ifaddr *ifa = (struct ifaddr *)data;
   1021 	struct ifreq *ifr = (struct ifreq *)data;
   1022 	int s = splnet(), error = 0;
   1023 	struct sl_softc *sc = ifp->if_softc;
   1024 
   1025 	switch (cmd) {
   1026 
   1027 	case SIOCSIFADDR:
   1028 		if (ifa->ifa_addr->sa_family == AF_INET)
   1029 			ifp->if_flags |= IFF_UP;
   1030 		else
   1031 			error = EAFNOSUPPORT;
   1032 		break;
   1033 
   1034 	case SIOCSIFDSTADDR:
   1035 		if (ifa->ifa_addr->sa_family != AF_INET)
   1036 			error = EAFNOSUPPORT;
   1037 		break;
   1038 
   1039 	case SIOCSIFMTU:
   1040 		if ((ifr->ifr_mtu < 3) || (ifr->ifr_mtu > SLMAX)) {
   1041 		    error = EINVAL;
   1042 		    break;
   1043 		}
   1044 		sc->sc_if.if_mtu = ifr->ifr_mtu;
   1045 		break;
   1046 
   1047 	case SIOCGIFMTU:
   1048 		ifr->ifr_mtu = sc->sc_if.if_mtu;
   1049 		break;
   1050 
   1051 	case SIOCADDMULTI:
   1052 	case SIOCDELMULTI:
   1053 		if (ifr == 0) {
   1054 			error = EAFNOSUPPORT;		/* XXX */
   1055 			break;
   1056 		}
   1057 		switch (ifr->ifr_addr.sa_family) {
   1058 
   1059 #ifdef INET
   1060 		case AF_INET:
   1061 			break;
   1062 #endif
   1063 
   1064 		default:
   1065 			error = EAFNOSUPPORT;
   1066 			break;
   1067 		}
   1068 		break;
   1069 
   1070 	default:
   1071 		error = EINVAL;
   1072 	}
   1073 	splx(s);
   1074 	return (error);
   1075 }
   1076 #endif
   1077