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