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