Home | History | Annotate | Line # | Download | only in net
if_sl.c revision 1.25
      1 /*
      2  * Copyright (c) 1987, 1989 Regents of the University of California.
      3  * 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,v 1.11 84/10/04 12:54:47 rick Exp
     34  *	from: @(#)if_sl.c	7.22 (Berkeley) 4/20/91
     35  *	$Id: if_sl.c,v 1.25 1994/02/05 08:01:22 mycroft Exp $
     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 "bpfilter.h"
     73 
     74 #include <sys/param.h>
     75 #include <sys/proc.h>
     76 #include <sys/mbuf.h>
     77 #include <sys/buf.h>
     78 #include <sys/dkstat.h>
     79 #include <sys/socket.h>
     80 #include <sys/ioctl.h>
     81 #include <sys/file.h>
     82 #include <sys/tty.h>
     83 #include <sys/kernel.h>
     84 #include <sys/conf.h>
     85 
     86 #include <machine/cpu.h>
     87 
     88 #include <net/if.h>
     89 #include <net/if_types.h>
     90 #include <net/netisr.h>
     91 #include <net/route.h>
     92 
     93 #if INET
     94 #include <netinet/in.h>
     95 #include <netinet/in_systm.h>
     96 #include <netinet/in_var.h>
     97 #include <netinet/ip.h>
     98 #else
     99 Huh? Slip without inet?
    100 #endif
    101 
    102 #include <net/slcompress.h>
    103 #include <net/if_slvar.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 #define	SLMTU		296
    158 #define	SLIP_HIWAT	roundup(50,CBSIZE)
    159 #ifndef __NetBSD__						/* XXX - cgd */
    160 #define	CLISTRESERVE	1024	/* Can't let clists get too low */
    161 #endif	/* !__NetBSD__ */
    162 
    163 /*
    164  * SLIP ABORT ESCAPE MECHANISM:
    165  *	(inspired by HAYES modem escape arrangement)
    166  *	1sec escape 1sec escape 1sec escape { 1sec escape 1sec escape }
    167  *	within window time signals a "soft" exit from slip mode by remote end
    168  *	if the IFF_DEBUG flag is on.
    169  */
    170 #define	ABT_ESC		'\033'	/* can't be t_intr - distant host must know it*/
    171 #define	ABT_IDLE	1	/* in seconds - idle before an escape */
    172 #define	ABT_COUNT	3	/* count of escapes for abort */
    173 #define	ABT_WINDOW	(ABT_COUNT*2+2)	/* in seconds - time to count */
    174 
    175 struct sl_softc sl_softc[NSL];
    176 
    177 #define FRAME_END	 	0xc0		/* Frame End */
    178 #define FRAME_ESCAPE		0xdb		/* Frame Esc */
    179 #define TRANS_FRAME_END	 	0xdc		/* transposed frame end */
    180 #define TRANS_FRAME_ESCAPE 	0xdd		/* transposed frame esc */
    181 
    182 extern struct timeval time;
    183 
    184 static int slinit __P((struct sl_softc *));
    185 static struct mbuf *sl_btom __P((struct sl_softc *, int));
    186 
    187 /*
    188  * Called from boot code to establish sl interfaces.
    189  */
    190 void
    191 slattach()
    192 {
    193 	register struct sl_softc *sc;
    194 	register int i = 0;
    195 
    196 	for (sc = sl_softc; i < NSL; sc++) {
    197 		sc->sc_if.if_name = "sl";
    198 		sc->sc_if.if_next = NULL;
    199 		sc->sc_if.if_unit = i++;
    200 		sc->sc_if.if_mtu = SLMTU;
    201 		sc->sc_if.if_flags =
    202 		    IFF_POINTOPOINT | SC_AUTOCOMP | IFF_MULTICAST;
    203 		sc->sc_if.if_type = IFT_SLIP;
    204 		sc->sc_if.if_ioctl = slioctl;
    205 		sc->sc_if.if_output = sloutput;
    206 		sc->sc_if.if_snd.ifq_maxlen = 50;
    207 		sc->sc_fastq.ifq_maxlen = 32;
    208 		if_attach(&sc->sc_if);
    209 #if NBPFILTER > 0
    210 		bpfattach(&sc->sc_bpf, &sc->sc_if, DLT_SLIP, SLIP_HDRLEN);
    211 #endif
    212 	}
    213 }
    214 
    215 static int
    216 slinit(sc)
    217 	register struct sl_softc *sc;
    218 {
    219 	register caddr_t p;
    220 
    221 	if (sc->sc_ep == (u_char *) 0) {
    222 		MCLALLOC(p, M_WAIT);
    223 		if (p)
    224 			sc->sc_ep = (u_char *)p + SLBUFSIZE;
    225 		else {
    226 			printf("sl%d: can't allocate buffer\n", sc - sl_softc);
    227 			sc->sc_if.if_flags &= ~IFF_UP;
    228 			return (0);
    229 		}
    230 	}
    231 	sc->sc_buf = sc->sc_ep - SLMAX;
    232 	sc->sc_mp = sc->sc_buf;
    233 	sl_compress_init(&sc->sc_comp);
    234 	return (1);
    235 }
    236 
    237 /*
    238  * Line specific open routine.
    239  * Attach the given tty to the first available sl unit.
    240  */
    241 /* ARGSUSED */
    242 int
    243 slopen(dev, tp)
    244 	dev_t dev;
    245 	register struct tty *tp;
    246 {
    247 	struct proc *p = curproc;		/* XXX */
    248 	register struct sl_softc *sc;
    249 	register int nsl;
    250 	int error;
    251 
    252 	if (error = suser(p->p_ucred, &p->p_acflag))
    253 		return (error);
    254 
    255 	if (tp->t_line == SLIPDISC)
    256 		return (0);
    257 
    258 	for (nsl = NSL, sc = sl_softc; --nsl >= 0; sc++)
    259 		if (sc->sc_ttyp == NULL) {
    260 			if (slinit(sc) == 0)
    261 				return (ENOBUFS);
    262 			tp->t_sc = (caddr_t)sc;
    263 			sc->sc_ttyp = tp;
    264 			sc->sc_if.if_baudrate = tp->t_ospeed;
    265 			ttyflush(tp, FREAD | FWRITE);
    266 			return (0);
    267 		}
    268 	return (ENXIO);
    269 }
    270 
    271 /*
    272  * Line specific close routine.
    273  * Detach the tty from the sl unit.
    274  */
    275 void
    276 slclose(tp)
    277 	struct tty *tp;
    278 {
    279 	register struct sl_softc *sc;
    280 	int s;
    281 
    282 	ttywflush(tp);
    283 	s = splimp();		/* actually, max(spltty, splnet) */
    284 	tp->t_line = 0;
    285 	sc = (struct sl_softc *)tp->t_sc;
    286 	if (sc != NULL) {
    287 		if_down(&sc->sc_if);
    288 		sc->sc_ttyp = NULL;
    289 		tp->t_sc = NULL;
    290 		MCLFREE((caddr_t)(sc->sc_ep - SLBUFSIZE));
    291 		sc->sc_ep = 0;
    292 		sc->sc_mp = 0;
    293 		sc->sc_buf = 0;
    294 	}
    295 	splx(s);
    296 }
    297 
    298 /*
    299  * Line specific (tty) ioctl routine.
    300  * Provide a way to get the sl unit number.
    301  */
    302 /* ARGSUSED */
    303 int
    304 sltioctl(tp, cmd, data, flag)
    305 	struct tty *tp;
    306 	int cmd;
    307 	caddr_t data;
    308 	int flag;
    309 {
    310 	struct sl_softc *sc = (struct sl_softc *)tp->t_sc;
    311 	int s;
    312 
    313 	switch (cmd) {
    314 	case SLIOCGUNIT:
    315 		*(int *)data = sc->sc_if.if_unit;
    316 		break;
    317 
    318 	default:
    319 		return (-1);
    320 	}
    321 	return (0);
    322 }
    323 
    324 /*
    325  * Queue a packet.  Start transmission if not active.
    326  * Compression happens in slstart; if we do it here, IP TOS
    327  * will cause us to not compress "background" packets, because
    328  * ordering gets hosed.  It can be done for all packets in slstart.
    329  */
    330 int
    331 sloutput(ifp, m, dst, rtp)
    332 	struct ifnet *ifp;
    333 	register struct mbuf *m;
    334 	struct sockaddr *dst;
    335 	struct rtentry *rtp;
    336 {
    337 	register struct sl_softc *sc = &sl_softc[ifp->if_unit];
    338 	register struct ip *ip;
    339 	register struct ifqueue *ifq;
    340 	int s;
    341 
    342 	/*
    343 	 * `Cannot happen' (see slioctl).  Someday we will extend
    344 	 * the line protocol to support other address families.
    345 	 */
    346 	if (dst->sa_family != AF_INET) {
    347 		printf("sl%d: af%d not supported\n", sc->sc_if.if_unit,
    348 			dst->sa_family);
    349 		m_freem(m);
    350 		sc->sc_if.if_noproto++;
    351 		return (EAFNOSUPPORT);
    352 	}
    353 
    354 	if (sc->sc_ttyp == NULL) {
    355 		m_freem(m);
    356 		return (ENETDOWN);	/* sort of */
    357 	}
    358 	if ((sc->sc_ttyp->t_state & TS_CARR_ON) == 0 &&
    359 	    (sc->sc_ttyp->t_cflag & CLOCAL) == 0) {
    360 		m_freem(m);
    361 		return (EHOSTUNREACH);
    362 	}
    363 	ifq = &sc->sc_if.if_snd;
    364 	ip = mtod(m, struct ip *);
    365 	if (sc->sc_if.if_flags & SC_NOICMP && ip->ip_p == IPPROTO_ICMP) {
    366 		m_freem(m);
    367 		return (ENETRESET);		/* XXX ? */
    368 	}
    369 	if (ip->ip_tos & IPTOS_LOWDELAY)
    370 		ifq = &sc->sc_fastq;
    371 	s = splimp();
    372 	if (IF_QFULL(ifq)) {
    373 		IF_DROP(ifq);
    374 		m_freem(m);
    375 		splx(s);
    376 		sc->sc_if.if_oerrors++;
    377 		return (ENOBUFS);
    378 	}
    379 	IF_ENQUEUE(ifq, m);
    380 	sc->sc_if.if_lastchange = time;
    381 	if (sc->sc_ttyp->t_outq.c_cc == 0)
    382 		slstart(sc->sc_ttyp);
    383 #ifdef __NetBSD__
    384 	else							/* XXX - cgd */
    385 		(*sc->sc_ttyp->t_oproc)(sc->sc_ttyp);
    386 #endif
    387 	splx(s);
    388 	return (0);
    389 }
    390 
    391 /*
    392  * Start output on interface.  Get another datagram
    393  * to send from the interface queue and map it to
    394  * the interface before starting output.
    395  */
    396 void
    397 slstart(tp)
    398 	register struct tty *tp;
    399 {
    400 	register struct sl_softc *sc = (struct sl_softc *)tp->t_sc;
    401 	register struct mbuf *m;
    402 	register u_char *cp;
    403 	register struct ip *ip;
    404 	int s;
    405 	struct mbuf *m2;
    406 #if NBPFILTER > 0
    407 	u_char bpfbuf[SLMTU + SLIP_HDRLEN];
    408 	register int len;
    409 #endif
    410 #ifndef __NetBSD__						/* XXX - cgd */
    411 	extern int cfreecount;
    412 #endif
    413 
    414 	for (;;) {
    415 		/*
    416 		 * If there is more in the output queue, just send it now.
    417 		 * We are being called in lieu of ttstart and must do what
    418 		 * it would.
    419 		 */
    420 		if (tp->t_outq.c_cc != 0) {
    421 			(*tp->t_oproc)(tp);
    422 			if (tp->t_outq.c_cc > SLIP_HIWAT)
    423 				return;
    424 		}
    425 		/*
    426 		 * This happens briefly when the line shuts down.
    427 		 */
    428 		if (sc == NULL)
    429 			return;
    430 
    431 #ifdef __NetBSD__						/* XXX - cgd */
    432 		/*
    433 		 * Do not remove the packet from the IP queue if it
    434 		 * doesn't look like the packet will fit into the
    435 		 * current serial output queue, with a packet full of
    436 		 * escapes this could be as bad as SLMTU*2.  The size
    437 		 * of the ring buffer must be at least SLMTU*2 to
    438 		 * avoid deadlock.
    439 		 */
    440 		if (tp->t_outq.c_cn - tp->t_outq.c_cc < 2 * SLMTU)
    441 			return;
    442 #endif /* __NetBSD__ */
    443 
    444 		/*
    445 		 * Get a packet and send it to the interface.
    446 		 */
    447 		s = splimp();
    448 		IF_DEQUEUE(&sc->sc_fastq, m);
    449 		if (m)
    450 			sc->sc_if.if_omcasts++;		/* XXX */
    451 		else
    452 			IF_DEQUEUE(&sc->sc_if.if_snd, m);
    453 		splx(s);
    454 		if (m == NULL)
    455 			return;
    456 
    457 		/*
    458 		 * We do the header compression here rather than in sloutput
    459 		 * because the packets will be out of order if we are using TOS
    460 		 * queueing, and the connection id compression will get
    461 		 * munged when this happens.
    462 		 */
    463 #if NBPFILTER > 0
    464 		if (sc->sc_bpf) {
    465 			/*
    466 			 * We need to save the TCP/IP header before it's
    467 			 * compressed.  To avoid complicated code, we just
    468 			 * copy the entire packet into a stack buffer (since
    469 			 * this is a serial line, packets should be short
    470 			 * and/or the copy should be negligible cost compared
    471 			 * to the packet transmission time).
    472 			 */
    473 			register struct mbuf *m1 = m;
    474 			register u_char *cp = bpfbuf + SLIP_HDRLEN;
    475 
    476 			len = 0;
    477 			do {
    478 				register int mlen = m1->m_len;
    479 
    480 				bcopy(mtod(m1, caddr_t), cp, mlen);
    481 				cp += mlen;
    482 				len += mlen;
    483 			} while (m1 = m1->m_next);
    484 		}
    485 #endif
    486 		if ((ip = mtod(m, struct ip *))->ip_p == IPPROTO_TCP) {
    487 			if (sc->sc_if.if_flags & SC_COMPRESS)
    488 				*mtod(m, u_char *) |= sl_compress_tcp(m, ip,
    489 				    &sc->sc_comp, 1);
    490 		}
    491 #if NBPFILTER > 0
    492 		if (sc->sc_bpf) {
    493 			/*
    494 			 * Put the SLIP pseudo-"link header" in place.  The
    495 			 * compressed header is now at the beginning of the
    496 			 * mbuf.
    497 			 */
    498 			bpfbuf[SLX_DIR] = SLIPDIR_OUT;
    499 			bcopy(mtod(m, caddr_t), &bpfbuf[SLX_CHDR], CHDR_LEN);
    500 			bpf_tap(sc->sc_bpf, bpfbuf, len + SLIP_HDRLEN);
    501 		}
    502 #endif
    503 		sc->sc_if.if_lastchange = time;
    504 
    505 #ifndef __NetBSD__						/* XXX - cgd */
    506 		/*
    507 		 * If system is getting low on clists, just flush our
    508 		 * output queue (if the stuff was important, it'll get
    509 		 * retransmitted).
    510 		 */
    511 		if (cfreecount < CLISTRESERVE + SLMTU) {
    512 			m_freem(m);
    513 			sc->sc_if.if_collisions++;
    514 			continue;
    515 		}
    516 #endif /* !__NetBSD__ */
    517 		/*
    518 		 * The extra FRAME_END will start up a new packet, and thus
    519 		 * will flush any accumulated garbage.  We do this whenever
    520 		 * the line may have been idle for some time.
    521 		 */
    522 		if (tp->t_outq.c_cc == 0) {
    523 			++sc->sc_if.if_obytes;
    524 			(void) putc(FRAME_END, &tp->t_outq);
    525 		}
    526 
    527 		while (m) {
    528 			register u_char *ep;
    529 
    530 			cp = mtod(m, u_char *); ep = cp + m->m_len;
    531 			while (cp < ep) {
    532 				/*
    533 				 * Find out how many bytes in the string we can
    534 				 * handle without doing something special.
    535 				 */
    536 				register u_char *bp = cp;
    537 
    538 				while (cp < ep) {
    539 					switch (*cp++) {
    540 					case FRAME_ESCAPE:
    541 					case FRAME_END:
    542 						--cp;
    543 						goto out;
    544 					}
    545 				}
    546 				out:
    547 				if (cp > bp) {
    548 					/*
    549 					 * Put n characters at once
    550 					 * into the tty output queue.
    551 					 */
    552 #ifdef __NetBSD__						/* XXX - cgd */
    553 					if (b_to_q((u_char *)bp, cp - bp,
    554 #else
    555 					if (b_to_q((char *)bp, cp - bp,
    556 #endif
    557 					    &tp->t_outq))
    558 						break;
    559 					sc->sc_if.if_obytes += cp - bp;
    560 				}
    561 				/*
    562 				 * If there are characters left in the mbuf,
    563 				 * the first one must be special..
    564 				 * Put it out in a different form.
    565 				 */
    566 				if (cp < ep) {
    567 					if (putc(FRAME_ESCAPE, &tp->t_outq))
    568 						break;
    569 					if (putc(*cp++ == FRAME_ESCAPE ?
    570 					   TRANS_FRAME_ESCAPE : TRANS_FRAME_END,
    571 					   &tp->t_outq)) {
    572 						(void) unputc(&tp->t_outq);
    573 						break;
    574 					}
    575 					sc->sc_if.if_obytes += 2;
    576 				}
    577 			}
    578 			MFREE(m, m2);
    579 			m = m2;
    580 		}
    581 
    582 		if (putc(FRAME_END, &tp->t_outq)) {
    583 			/*
    584 			 * Not enough room.  Remove a char to make room
    585 			 * and end the packet normally.
    586 			 * If you get many collisions (more than one or two
    587 			 * a day) you probably do not have enough clists
    588 			 * and you should increase "nclist" in param.c.
    589 			 */
    590 			(void) unputc(&tp->t_outq);
    591 			(void) putc(FRAME_END, &tp->t_outq);
    592 			sc->sc_if.if_collisions++;
    593 		} else {
    594 			++sc->sc_if.if_obytes;
    595 			sc->sc_if.if_opackets++;
    596 		}
    597 	}
    598 }
    599 
    600 /*
    601  * Copy data buffer to mbuf chain; add ifnet pointer.
    602  */
    603 static struct mbuf *
    604 sl_btom(sc, len)
    605 	register struct sl_softc *sc;
    606 	register int len;
    607 {
    608 	register struct mbuf *m;
    609 
    610 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    611 	if (m == NULL)
    612 		return (NULL);
    613 
    614 	/*
    615 	 * If we have more than MHLEN bytes, it's cheaper to
    616 	 * queue the cluster we just filled & allocate a new one
    617 	 * for the input buffer.  Otherwise, fill the mbuf we
    618 	 * allocated above.  Note that code in the input routine
    619 	 * guarantees that packet will fit in a cluster.
    620 	 */
    621 	if (len >= MHLEN) {
    622 		MCLGET(m, M_DONTWAIT);
    623 		if ((m->m_flags & M_EXT) == 0) {
    624 			/*
    625 			 * we couldn't get a cluster - if memory's this
    626 			 * low, it's time to start dropping packets.
    627 			 */
    628 			(void) m_free(m);
    629 			return (NULL);
    630 		}
    631 		sc->sc_ep = mtod(m, u_char *) + SLBUFSIZE;
    632 		m->m_data = (caddr_t)sc->sc_buf;
    633 		m->m_ext.ext_buf = (caddr_t)((int)sc->sc_buf &~ MCLOFSET);
    634 	} else
    635 		bcopy((caddr_t)sc->sc_buf, mtod(m, caddr_t), len);
    636 
    637 	m->m_len = len;
    638 	m->m_pkthdr.len = len;
    639 	m->m_pkthdr.rcvif = &sc->sc_if;
    640 	return (m);
    641 }
    642 
    643 /*
    644  * tty interface receiver interrupt.
    645  */
    646 void
    647 slinput(c, tp)
    648 	register int c;
    649 	register struct tty *tp;
    650 {
    651 	register struct sl_softc *sc;
    652 	register struct mbuf *m;
    653 	register int len;
    654 	int s;
    655 #if NBPFILTER > 0
    656 	u_char chdr[CHDR_LEN];
    657 #endif
    658 
    659 	tk_nin++;
    660 	sc = (struct sl_softc *)tp->t_sc;
    661 	if (sc == NULL)
    662 		return;
    663 	if (c & TTY_ERRORMASK || ((tp->t_state & TS_CARR_ON) == 0 &&
    664 	    (tp->t_cflag & CLOCAL) == 0)) {
    665 		sc->sc_flags |= SC_ERROR;
    666 		return;
    667 	}
    668 	c &= TTY_CHARMASK;
    669 
    670 	++sc->sc_if.if_ibytes;
    671 
    672 	if (sc->sc_if.if_flags & IFF_DEBUG) {
    673 		if (c == ABT_ESC) {
    674 			/*
    675 			 * If we have a previous abort, see whether
    676 			 * this one is within the time limit.
    677 			 */
    678 			if (sc->sc_abortcount &&
    679 			    time.tv_sec >= sc->sc_starttime + ABT_WINDOW)
    680 				sc->sc_abortcount = 0;
    681 			/*
    682 			 * If we see an abort after "idle" time, count it;
    683 			 * record when the first abort escape arrived.
    684 			 */
    685 			if (time.tv_sec >= sc->sc_lasttime + ABT_IDLE) {
    686 				if (++sc->sc_abortcount == 1)
    687 					sc->sc_starttime = time.tv_sec;
    688 				if (sc->sc_abortcount >= ABT_COUNT) {
    689 					slclose(tp);
    690 					return;
    691 				}
    692 			}
    693 		} else
    694 			sc->sc_abortcount = 0;
    695 		sc->sc_lasttime = time.tv_sec;
    696 	}
    697 
    698 	switch (c) {
    699 
    700 	case TRANS_FRAME_ESCAPE:
    701 		if (sc->sc_escape)
    702 			c = FRAME_ESCAPE;
    703 		break;
    704 
    705 	case TRANS_FRAME_END:
    706 		if (sc->sc_escape)
    707 			c = FRAME_END;
    708 		break;
    709 
    710 	case FRAME_ESCAPE:
    711 		sc->sc_escape = 1;
    712 		return;
    713 
    714 	case FRAME_END:
    715 		if(sc->sc_flags & SC_ERROR) {
    716 			sc->sc_flags &= ~SC_ERROR;
    717 			goto newpack;
    718 		}
    719 		len = sc->sc_mp - sc->sc_buf;
    720 		if (len < 3)
    721 			/* less than min length packet - ignore */
    722 			goto newpack;
    723 
    724 #if NBPFILTER > 0
    725 		if (sc->sc_bpf) {
    726 			/*
    727 			 * Save the compressed header, so we
    728 			 * can tack it on later.  Note that we
    729 			 * will end up copying garbage in some
    730 			 * cases but this is okay.  We remember
    731 			 * where the buffer started so we can
    732 			 * compute the new header length.
    733 			 */
    734 			bcopy(sc->sc_buf, chdr, CHDR_LEN);
    735 		}
    736 #endif
    737 
    738 		if ((c = (*sc->sc_buf & 0xf0)) != (IPVERSION << 4)) {
    739 			if (c & 0x80)
    740 				c = TYPE_COMPRESSED_TCP;
    741 			else if (c == TYPE_UNCOMPRESSED_TCP)
    742 				*sc->sc_buf &= 0x4f; /* XXX */
    743 			/*
    744 			 * We've got something that's not an IP packet.
    745 			 * If compression is enabled, try to decompress it.
    746 			 * Otherwise, if `auto-enable' compression is on and
    747 			 * it's a reasonable packet, decompress it and then
    748 			 * enable compression.  Otherwise, drop it.
    749 			 */
    750 			if (sc->sc_if.if_flags & SC_COMPRESS) {
    751 				len = sl_uncompress_tcp(&sc->sc_buf, len,
    752 							(u_int)c, &sc->sc_comp);
    753 				if (len <= 0)
    754 					goto error;
    755 			} else if ((sc->sc_if.if_flags & SC_AUTOCOMP) &&
    756 			    c == TYPE_UNCOMPRESSED_TCP && len >= 40) {
    757 				len = sl_uncompress_tcp(&sc->sc_buf, len,
    758 							(u_int)c, &sc->sc_comp);
    759 				if (len <= 0)
    760 					goto error;
    761 				sc->sc_if.if_flags |= SC_COMPRESS;
    762 			} else
    763 				goto error;
    764 		}
    765 #if NBPFILTER > 0
    766 		if (sc->sc_bpf) {
    767 			/*
    768 			 * Put the SLIP pseudo-"link header" in place.
    769 			 * We couldn't do this any earlier since
    770 			 * decompression probably moved the buffer
    771 			 * pointer.  Then, invoke BPF.
    772 			 */
    773 			register u_char *hp = sc->sc_buf - SLIP_HDRLEN;
    774 
    775 			hp[SLX_DIR] = SLIPDIR_IN;
    776 			bcopy(chdr, &hp[SLX_CHDR], CHDR_LEN);
    777 			bpf_tap(sc->sc_bpf, hp, len + SLIP_HDRLEN);
    778 		}
    779 #endif
    780 		m = sl_btom(sc, len);
    781 		if (m == NULL)
    782 			goto error;
    783 
    784 		sc->sc_if.if_ipackets++;
    785 		sc->sc_if.if_lastchange = time;
    786 		s = splimp();
    787 		if (IF_QFULL(&ipintrq)) {
    788 			IF_DROP(&ipintrq);
    789 			sc->sc_if.if_ierrors++;
    790 			sc->sc_if.if_iqdrops++;
    791 			m_freem(m);
    792 		} else {
    793 			IF_ENQUEUE(&ipintrq, m);
    794 			schednetisr(NETISR_IP);
    795 		}
    796 		splx(s);
    797 		goto newpack;
    798 	}
    799 	if (sc->sc_mp < sc->sc_ep) {
    800 		*sc->sc_mp++ = c;
    801 		sc->sc_escape = 0;
    802 		return;
    803 	}
    804 
    805 	/* can't put lower; would miss an extra frame */
    806 	sc->sc_flags |= SC_ERROR;
    807 
    808 error:
    809 	sc->sc_if.if_ierrors++;
    810 newpack:
    811 	sc->sc_mp = sc->sc_buf = sc->sc_ep - SLMAX;
    812 	sc->sc_escape = 0;
    813 }
    814 
    815 /*
    816  * Process an ioctl request.
    817  */
    818 int
    819 slioctl(ifp, cmd, data)
    820 	register struct ifnet *ifp;
    821 	int cmd;
    822 	caddr_t data;
    823 {
    824 	register struct ifaddr *ifa = (struct ifaddr *)data;
    825 	register struct ifreq *ifr;
    826 	register int s = splimp(), error = 0;
    827 
    828 	switch (cmd) {
    829 
    830 	case SIOCSIFADDR:
    831 		if (ifa->ifa_addr->sa_family == AF_INET)
    832 			ifp->if_flags |= IFF_UP;
    833 		else
    834 			error = EAFNOSUPPORT;
    835 		break;
    836 
    837 	case SIOCSIFDSTADDR:
    838 		if (ifa->ifa_addr->sa_family != AF_INET)
    839 			error = EAFNOSUPPORT;
    840 		break;
    841 
    842 	case SIOCADDMULTI:
    843 	case SIOCDELMULTI:
    844 		ifr = (struct ifreq *)data;
    845 		if (ifr == 0) {
    846 			error = EAFNOSUPPORT;		/* XXX */
    847 			break;
    848 		}
    849 		switch (ifr->ifr_addr.sa_family) {
    850 
    851 #ifdef INET
    852 		case AF_INET:
    853 			break;
    854 #endif
    855 
    856 		default:
    857 			error = EAFNOSUPPORT;
    858 			break;
    859 		}
    860 		break;
    861 
    862 	default:
    863 		error = EINVAL;
    864 	}
    865 	splx(s);
    866 	return (error);
    867 }
    868 #endif
    869