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