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