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