Home | History | Annotate | Line # | Download | only in net
if_sl.c revision 1.1.1.3
      1 /*
      2  * Copyright (c) 1987, 1989, 1992, 1993
      3  *	The Regents of the University of California.  All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. All advertising materials mentioning features or use of this software
     14  *    must display the following acknowledgement:
     15  *	This product includes software developed by the University of
     16  *	California, Berkeley and its contributors.
     17  * 4. Neither the name of the University nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  *
     33  *	@(#)if_sl.c	8.9 (Berkeley) 1/9/95
     34  */
     35 
     36 /*
     37  * Serial Line interface
     38  *
     39  * Rick Adams
     40  * Center for Seismic Studies
     41  * 1300 N 17th Street, Suite 1450
     42  * Arlington, Virginia 22209
     43  * (703)276-7900
     44  * rick (at) seismo.ARPA
     45  * seismo!rick
     46  *
     47  * Pounded on heavily by Chris Torek (chris (at) mimsy.umd.edu, umcp-cs!chris).
     48  * N.B.: this belongs in netinet, not net, the way it stands now.
     49  * Should have a link-layer type designation, but wouldn't be
     50  * backwards-compatible.
     51  *
     52  * Converted to 4.3BSD Beta by Chris Torek.
     53  * Other changes made at Berkeley, based in part on code by Kirk Smith.
     54  * W. Jolitz added slip abort.
     55  *
     56  * Hacked almost beyond recognition by Van Jacobson (van (at) helios.ee.lbl.gov).
     57  * Added priority queuing for "interactive" traffic; hooks for TCP
     58  * header compression; ICMP filtering (at 2400 baud, some cretin
     59  * pinging you can use up all your bandwidth).  Made low clist behavior
     60  * more robust and slightly less likely to hang serial line.
     61  * Sped up a bunch of things.
     62  *
     63  * Note that splimp() is used throughout to block both (tty) input
     64  * interrupts and network activity; thus, splimp must be >= spltty.
     65  */
     66 
     67 #include "sl.h"
     68 #if NSL > 0
     69 
     70 #include "bpfilter.h"
     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 <machine/cpu.h>
     85 
     86 #include <net/if.h>
     87 #include <net/if_types.h>
     88 #include <net/netisr.h>
     89 #include <net/route.h>
     90 
     91 #if INET
     92 #include <netinet/in.h>
     93 #include <netinet/in_systm.h>
     94 #include <netinet/in_var.h>
     95 #include <netinet/ip.h>
     96 #else
     97 Huh? Slip without inet?
     98 #endif
     99 
    100 #include <net/slcompress.h>
    101 #include <net/if_slvar.h>
    102 #include <net/slip.h>
    103 
    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
    157 #define	SLIP_HIWAT	roundup(50,CBSIZE)
    158 #define	CLISTRESERVE	1024	/* Can't let clists get too low */
    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  *	within window time signals a "soft" exit from slip mode by remote end
    165  *	if the IFF_DEBUG flag is on.
    166  */
    167 #define	ABT_ESC		'\033'	/* can't be t_intr - distant host must know it*/
    168 #define	ABT_IDLE	1	/* in seconds - idle before an escape */
    169 #define	ABT_COUNT	3	/* count of escapes for abort */
    170 #define	ABT_WINDOW	(ABT_COUNT*2+2)	/* in seconds - time to count */
    171 
    172 struct sl_softc sl_softc[NSL];
    173 
    174 #define FRAME_END	 	0xc0		/* Frame End */
    175 #define FRAME_ESCAPE		0xdb		/* Frame Esc */
    176 #define TRANS_FRAME_END	 	0xdc		/* transposed frame end */
    177 #define TRANS_FRAME_ESCAPE 	0xdd		/* transposed frame esc */
    178 
    179 static int slinit __P((struct sl_softc *));
    180 static struct mbuf *sl_btom __P((struct sl_softc *, int));
    181 
    182 /*
    183  * Called from boot code to establish sl interfaces.
    184  */
    185 void
    186 slattach()
    187 {
    188 	register struct sl_softc *sc;
    189 	register int i = 0;
    190 
    191 	for (sc = sl_softc; i < NSL; sc++) {
    192 		sc->sc_if.if_name = "sl";
    193 		sc->sc_if.if_next = NULL;
    194 		sc->sc_if.if_unit = i++;
    195 		sc->sc_if.if_mtu = SLMTU;
    196 		sc->sc_if.if_flags =
    197 		    IFF_POINTOPOINT | SC_AUTOCOMP | IFF_MULTICAST;
    198 		sc->sc_if.if_type = IFT_SLIP;
    199 		sc->sc_if.if_ioctl = slioctl;
    200 		sc->sc_if.if_output = sloutput;
    201 		sc->sc_if.if_snd.ifq_maxlen = 50;
    202 		sc->sc_fastq.ifq_maxlen = 32;
    203 		if_attach(&sc->sc_if);
    204 #if NBPFILTER > 0
    205 		bpfattach(&sc->sc_bpf, &sc->sc_if, DLT_SLIP, SLIP_HDRLEN);
    206 #endif
    207 	}
    208 }
    209 
    210 static int
    211 slinit(sc)
    212 	register struct sl_softc *sc;
    213 {
    214 	register caddr_t p;
    215 
    216 	if (sc->sc_ep == (u_char *) 0) {
    217 		MCLALLOC(p, M_WAIT);
    218 		if (p)
    219 			sc->sc_ep = (u_char *)p + SLBUFSIZE;
    220 		else {
    221 			printf("sl%d: can't allocate buffer\n", sc - sl_softc);
    222 			sc->sc_if.if_flags &= ~IFF_UP;
    223 			return (0);
    224 		}
    225 	}
    226 	sc->sc_buf = sc->sc_ep - SLMAX;
    227 	sc->sc_mp = sc->sc_buf;
    228 	sl_compress_init(&sc->sc_comp);
    229 	return (1);
    230 }
    231 
    232 /*
    233  * Line specific open routine.
    234  * Attach the given tty to the first available sl unit.
    235  */
    236 /* ARGSUSED */
    237 int
    238 slopen(dev, tp)
    239 	dev_t dev;
    240 	register struct tty *tp;
    241 {
    242 	struct proc *p = curproc;		/* XXX */
    243 	register struct sl_softc *sc;
    244 	register int nsl;
    245 	int error;
    246 	int s;
    247 
    248 	if (error = suser(p->p_ucred, &p->p_acflag))
    249 		return (error);
    250 
    251 	if (tp->t_line == SLIPDISC)
    252 		return (0);
    253 
    254 	for (nsl = NSL, sc = sl_softc; --nsl >= 0; sc++)
    255 		if (sc->sc_ttyp == NULL) {
    256 			if (slinit(sc) == 0)
    257 				return (ENOBUFS);
    258 			tp->t_sc = (caddr_t)sc;
    259 			sc->sc_ttyp = tp;
    260 			sc->sc_if.if_baudrate = tp->t_ospeed;
    261 			s = spltty();
    262 			tp->t_state |= TS_ISOPEN | TS_XCLUDE;
    263 			splx(s);
    264 			ttyflush(tp, FREAD | FWRITE);
    265 			return (0);
    266 		}
    267 	return (ENXIO);
    268 }
    269 
    270 /*
    271  * Line specific close routine.
    272  * Detach the tty from the sl unit.
    273  */
    274 void
    275 slclose(tp)
    276 	struct tty *tp;
    277 {
    278 	register struct sl_softc *sc;
    279 	int s;
    280 
    281 	ttywflush(tp);
    282 	s = splimp();		/* actually, max(spltty, splnet) */
    283 	tp->t_line = 0;
    284 	tp->t_state = 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 	u_long cmd;
    307 	caddr_t data;
    308 	int flag;
    309 {
    310 	struct sl_softc *sc = (struct sl_softc *)tp->t_sc;
    311 
    312 	switch (cmd) {
    313 	case SLIOCGUNIT:
    314 		*(int *)data = sc->sc_if.if_unit;
    315 		break;
    316 
    317 	default:
    318 		return (-1);
    319 	}
    320 	return (0);
    321 }
    322 
    323 /*
    324  * Queue a packet.  Start transmission if not active.
    325  * Compression happens in slstart; if we do it here, IP TOS
    326  * will cause us to not compress "background" packets, because
    327  * ordering gets trashed.  It can be done for all packets in slstart.
    328  */
    329 int
    330 sloutput(ifp, m, dst, rtp)
    331 	struct ifnet *ifp;
    332 	register struct mbuf *m;
    333 	struct sockaddr *dst;
    334 	struct rtentry *rtp;
    335 {
    336 	register struct sl_softc *sc = &sl_softc[ifp->if_unit];
    337 	register struct ip *ip;
    338 	register struct ifqueue *ifq;
    339 	int s;
    340 
    341 	/*
    342 	 * `Cannot happen' (see slioctl).  Someday we will extend
    343 	 * the line protocol to support other address families.
    344 	 */
    345 	if (dst->sa_family != AF_INET) {
    346 		printf("sl%d: af%d not supported\n", sc->sc_if.if_unit,
    347 			dst->sa_family);
    348 		m_freem(m);
    349 		sc->sc_if.if_noproto++;
    350 		return (EAFNOSUPPORT);
    351 	}
    352 
    353 	if (sc->sc_ttyp == NULL) {
    354 		m_freem(m);
    355 		return (ENETDOWN);	/* sort of */
    356 	}
    357 	if ((sc->sc_ttyp->t_state & TS_CARR_ON) == 0 &&
    358 	    (sc->sc_ttyp->t_cflag & CLOCAL) == 0) {
    359 		m_freem(m);
    360 		return (EHOSTUNREACH);
    361 	}
    362 	ifq = &sc->sc_if.if_snd;
    363 	ip = mtod(m, struct ip *);
    364 	if (sc->sc_if.if_flags & SC_NOICMP && ip->ip_p == IPPROTO_ICMP) {
    365 		m_freem(m);
    366 		return (ENETRESET);		/* XXX ? */
    367 	}
    368 	if (ip->ip_tos & IPTOS_LOWDELAY)
    369 		ifq = &sc->sc_fastq;
    370 	s = splimp();
    371 	if (IF_QFULL(ifq)) {
    372 		IF_DROP(ifq);
    373 		m_freem(m);
    374 		splx(s);
    375 		sc->sc_if.if_oerrors++;
    376 		return (ENOBUFS);
    377 	}
    378 	IF_ENQUEUE(ifq, m);
    379 	sc->sc_if.if_lastchange = time;
    380 	if (sc->sc_ttyp->t_outq.c_cc == 0)
    381 		slstart(sc->sc_ttyp);
    382 	splx(s);
    383 	return (0);
    384 }
    385 
    386 /*
    387  * Start output on interface.  Get another datagram
    388  * to send from the interface queue and map it to
    389  * the interface before starting output.
    390  */
    391 void
    392 slstart(tp)
    393 	register struct tty *tp;
    394 {
    395 	register struct sl_softc *sc = (struct sl_softc *)tp->t_sc;
    396 	register struct mbuf *m;
    397 	register u_char *cp;
    398 	register struct ip *ip;
    399 	int s;
    400 	struct mbuf *m2;
    401 #if NBPFILTER > 0
    402 	u_char bpfbuf[SLMTU + SLIP_HDRLEN];
    403 	register int len;
    404 #endif
    405 	extern int cfreecount;
    406 
    407 	for (;;) {
    408 		/*
    409 		 * If there is more in the output queue, just send it now.
    410 		 * We are being called in lieu of ttstart and must do what
    411 		 * it would.
    412 		 */
    413 		if (tp->t_outq.c_cc != 0) {
    414 			(*tp->t_oproc)(tp);
    415 			if (tp->t_outq.c_cc > SLIP_HIWAT)
    416 				return;
    417 		}
    418 		/*
    419 		 * This happens briefly when the line shuts down.
    420 		 */
    421 		if (sc == NULL)
    422 			return;
    423 
    424 		/*
    425 		 * Get a packet and send it to the interface.
    426 		 */
    427 		s = splimp();
    428 		IF_DEQUEUE(&sc->sc_fastq, m);
    429 		if (m)
    430 			sc->sc_if.if_omcasts++;		/* XXX */
    431 		else
    432 			IF_DEQUEUE(&sc->sc_if.if_snd, m);
    433 		splx(s);
    434 		if (m == NULL)
    435 			return;
    436 
    437 		/*
    438 		 * We do the header compression here rather than in sloutput
    439 		 * because the packets will be out of order if we are using TOS
    440 		 * queueing, and the connection id compression will get
    441 		 * munged when this happens.
    442 		 */
    443 #if NBPFILTER > 0
    444 		if (sc->sc_bpf) {
    445 			/*
    446 			 * We need to save the TCP/IP header before it's
    447 			 * compressed.  To avoid complicated code, we just
    448 			 * copy the entire packet into a stack buffer (since
    449 			 * this is a serial line, packets should be short
    450 			 * and/or the copy should be negligible cost compared
    451 			 * to the packet transmission time).
    452 			 */
    453 			register struct mbuf *m1 = m;
    454 			register u_char *cp = bpfbuf + SLIP_HDRLEN;
    455 
    456 			len = 0;
    457 			do {
    458 				register int mlen = m1->m_len;
    459 
    460 				bcopy(mtod(m1, caddr_t), cp, mlen);
    461 				cp += mlen;
    462 				len += mlen;
    463 			} while (m1 = m1->m_next);
    464 		}
    465 #endif
    466 		if ((ip = mtod(m, struct ip *))->ip_p == IPPROTO_TCP) {
    467 			if (sc->sc_if.if_flags & SC_COMPRESS)
    468 				*mtod(m, u_char *) |= sl_compress_tcp(m, ip,
    469 				    &sc->sc_comp, 1);
    470 		}
    471 #if NBPFILTER > 0
    472 		if (sc->sc_bpf) {
    473 			/*
    474 			 * Put the SLIP pseudo-"link header" in place.  The
    475 			 * compressed header is now at the beginning of the
    476 			 * mbuf.
    477 			 */
    478 			bpfbuf[SLX_DIR] = SLIPDIR_OUT;
    479 			bcopy(mtod(m, caddr_t), &bpfbuf[SLX_CHDR], CHDR_LEN);
    480 			bpf_tap(sc->sc_bpf, bpfbuf, len + SLIP_HDRLEN);
    481 		}
    482 #endif
    483 		sc->sc_if.if_lastchange = time;
    484 
    485 		/*
    486 		 * If system is getting low on clists, just flush our
    487 		 * output queue (if the stuff was important, it'll get
    488 		 * retransmitted).
    489 		 */
    490 		if (cfreecount < CLISTRESERVE + SLMTU) {
    491 			m_freem(m);
    492 			sc->sc_if.if_collisions++;
    493 			continue;
    494 		}
    495 		/*
    496 		 * The extra FRAME_END will start up a new packet, and thus
    497 		 * will flush any accumulated garbage.  We do this whenever
    498 		 * the line may have been idle for some time.
    499 		 */
    500 		if (tp->t_outq.c_cc == 0) {
    501 			++sc->sc_if.if_obytes;
    502 			(void) putc(FRAME_END, &tp->t_outq);
    503 		}
    504 
    505 		while (m) {
    506 			register u_char *ep;
    507 
    508 			cp = mtod(m, u_char *); ep = cp + m->m_len;
    509 			while (cp < ep) {
    510 				/*
    511 				 * Find out how many bytes in the string we can
    512 				 * handle without doing something special.
    513 				 */
    514 				register u_char *bp = cp;
    515 
    516 				while (cp < ep) {
    517 					switch (*cp++) {
    518 					case FRAME_ESCAPE:
    519 					case FRAME_END:
    520 						--cp;
    521 						goto out;
    522 					}
    523 				}
    524 				out:
    525 				if (cp > bp) {
    526 					/*
    527 					 * Put n characters at once
    528 					 * into the tty output queue.
    529 					 */
    530 					if (b_to_q((char *)bp, cp - bp,
    531 					    &tp->t_outq))
    532 						break;
    533 					sc->sc_if.if_obytes += cp - bp;
    534 				}
    535 				/*
    536 				 * If there are characters left in the mbuf,
    537 				 * the first one must be special..
    538 				 * Put it out in a different form.
    539 				 */
    540 				if (cp < ep) {
    541 					if (putc(FRAME_ESCAPE, &tp->t_outq))
    542 						break;
    543 					if (putc(*cp++ == FRAME_ESCAPE ?
    544 					   TRANS_FRAME_ESCAPE : TRANS_FRAME_END,
    545 					   &tp->t_outq)) {
    546 						(void) unputc(&tp->t_outq);
    547 						break;
    548 					}
    549 					sc->sc_if.if_obytes += 2;
    550 				}
    551 			}
    552 			MFREE(m, m2);
    553 			m = m2;
    554 		}
    555 
    556 		if (putc(FRAME_END, &tp->t_outq)) {
    557 			/*
    558 			 * Not enough room.  Remove a char to make room
    559 			 * and end the packet normally.
    560 			 * If you get many collisions (more than one or two
    561 			 * a day) you probably do not have enough clists
    562 			 * and you should increase "nclist" in param.c.
    563 			 */
    564 			(void) unputc(&tp->t_outq);
    565 			(void) putc(FRAME_END, &tp->t_outq);
    566 			sc->sc_if.if_collisions++;
    567 		} else {
    568 			++sc->sc_if.if_obytes;
    569 			sc->sc_if.if_opackets++;
    570 		}
    571 	}
    572 }
    573 
    574 /*
    575  * Copy data buffer to mbuf chain; add ifnet pointer.
    576  */
    577 static struct mbuf *
    578 sl_btom(sc, len)
    579 	register struct sl_softc *sc;
    580 	register int len;
    581 {
    582 	register struct mbuf *m;
    583 
    584 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    585 	if (m == NULL)
    586 		return (NULL);
    587 
    588 	/*
    589 	 * If we have more than MHLEN bytes, it's cheaper to
    590 	 * queue the cluster we just filled & allocate a new one
    591 	 * for the input buffer.  Otherwise, fill the mbuf we
    592 	 * allocated above.  Note that code in the input routine
    593 	 * guarantees that packet will fit in a cluster.
    594 	 */
    595 	if (len >= MHLEN) {
    596 		MCLGET(m, M_DONTWAIT);
    597 		if ((m->m_flags & M_EXT) == 0) {
    598 			/*
    599 			 * we couldn't get a cluster - if memory's this
    600 			 * low, it's time to start dropping packets.
    601 			 */
    602 			(void) m_free(m);
    603 			return (NULL);
    604 		}
    605 		sc->sc_ep = mtod(m, u_char *) + SLBUFSIZE;
    606 		m->m_data = (caddr_t)sc->sc_buf;
    607 		m->m_ext.ext_buf = (caddr_t)((int)sc->sc_buf &~ MCLOFSET);
    608 	} else
    609 		bcopy((caddr_t)sc->sc_buf, mtod(m, caddr_t), len);
    610 
    611 	m->m_len = len;
    612 	m->m_pkthdr.len = len;
    613 	m->m_pkthdr.rcvif = &sc->sc_if;
    614 	return (m);
    615 }
    616 
    617 /*
    618  * tty interface receiver interrupt.
    619  */
    620 void
    621 slinput(c, tp)
    622 	register int c;
    623 	register struct tty *tp;
    624 {
    625 	register struct sl_softc *sc;
    626 	register struct mbuf *m;
    627 	register int len;
    628 	int s;
    629 #if NBPFILTER > 0
    630 	u_char chdr[CHDR_LEN];
    631 #endif
    632 
    633 	tk_nin++;
    634 	sc = (struct sl_softc *)tp->t_sc;
    635 	if (sc == NULL)
    636 		return;
    637 	if ((c & TTY_ERRORMASK) || ((tp->t_state & TS_CARR_ON) == 0 &&
    638 	    (tp->t_cflag & CLOCAL) == 0)) {
    639 		sc->sc_flags |= SC_ERROR;
    640 		return;
    641 	}
    642 	c &= TTY_CHARMASK;
    643 
    644 	++sc->sc_if.if_ibytes;
    645 
    646 	if (sc->sc_if.if_flags & IFF_DEBUG) {
    647 		if (c == ABT_ESC) {
    648 			/*
    649 			 * If we have a previous abort, see whether
    650 			 * this one is within the time limit.
    651 			 */
    652 			if (sc->sc_abortcount &&
    653 			    time.tv_sec >= sc->sc_starttime + ABT_WINDOW)
    654 				sc->sc_abortcount = 0;
    655 			/*
    656 			 * If we see an abort after "idle" time, count it;
    657 			 * record when the first abort escape arrived.
    658 			 */
    659 			if (time.tv_sec >= sc->sc_lasttime + ABT_IDLE) {
    660 				if (++sc->sc_abortcount == 1)
    661 					sc->sc_starttime = time.tv_sec;
    662 				if (sc->sc_abortcount >= ABT_COUNT) {
    663 					slclose(tp);
    664 					return;
    665 				}
    666 			}
    667 		} else
    668 			sc->sc_abortcount = 0;
    669 		sc->sc_lasttime = time.tv_sec;
    670 	}
    671 
    672 	switch (c) {
    673 
    674 	case TRANS_FRAME_ESCAPE:
    675 		if (sc->sc_escape)
    676 			c = FRAME_ESCAPE;
    677 		break;
    678 
    679 	case TRANS_FRAME_END:
    680 		if (sc->sc_escape)
    681 			c = FRAME_END;
    682 		break;
    683 
    684 	case FRAME_ESCAPE:
    685 		sc->sc_escape = 1;
    686 		return;
    687 
    688 	case FRAME_END:
    689 		if(sc->sc_flags & SC_ERROR) {
    690 			sc->sc_flags &= ~SC_ERROR;
    691 			goto newpack;
    692 		}
    693 		len = sc->sc_mp - sc->sc_buf;
    694 		if (len < 3)
    695 			/* less than min length packet - ignore */
    696 			goto newpack;
    697 
    698 #if NBPFILTER > 0
    699 		if (sc->sc_bpf) {
    700 			/*
    701 			 * Save the compressed header, so we
    702 			 * can tack it on later.  Note that we
    703 			 * will end up copying garbage in some
    704 			 * cases but this is okay.  We remember
    705 			 * where the buffer started so we can
    706 			 * compute the new header length.
    707 			 */
    708 			bcopy(sc->sc_buf, chdr, CHDR_LEN);
    709 		}
    710 #endif
    711 
    712 		if ((c = (*sc->sc_buf & 0xf0)) != (IPVERSION << 4)) {
    713 			if (c & 0x80)
    714 				c = TYPE_COMPRESSED_TCP;
    715 			else if (c == TYPE_UNCOMPRESSED_TCP)
    716 				*sc->sc_buf &= 0x4f; /* XXX */
    717 			/*
    718 			 * We've got something that's not an IP packet.
    719 			 * If compression is enabled, try to decompress it.
    720 			 * Otherwise, if `auto-enable' compression is on and
    721 			 * it's a reasonable packet, decompress it and then
    722 			 * enable compression.  Otherwise, drop it.
    723 			 */
    724 			if (sc->sc_if.if_flags & SC_COMPRESS) {
    725 				len = sl_uncompress_tcp(&sc->sc_buf, len,
    726 							(u_int)c, &sc->sc_comp);
    727 				if (len <= 0)
    728 					goto error;
    729 			} else if ((sc->sc_if.if_flags & SC_AUTOCOMP) &&
    730 			    c == TYPE_UNCOMPRESSED_TCP && len >= 40) {
    731 				len = sl_uncompress_tcp(&sc->sc_buf, len,
    732 							(u_int)c, &sc->sc_comp);
    733 				if (len <= 0)
    734 					goto error;
    735 				sc->sc_if.if_flags |= SC_COMPRESS;
    736 			} else
    737 				goto error;
    738 		}
    739 #if NBPFILTER > 0
    740 		if (sc->sc_bpf) {
    741 			/*
    742 			 * Put the SLIP pseudo-"link header" in place.
    743 			 * We couldn't do this any earlier since
    744 			 * decompression probably moved the buffer
    745 			 * pointer.  Then, invoke BPF.
    746 			 */
    747 			register u_char *hp = sc->sc_buf - SLIP_HDRLEN;
    748 
    749 			hp[SLX_DIR] = SLIPDIR_IN;
    750 			bcopy(chdr, &hp[SLX_CHDR], CHDR_LEN);
    751 			bpf_tap(sc->sc_bpf, hp, len + SLIP_HDRLEN);
    752 		}
    753 #endif
    754 		m = sl_btom(sc, len);
    755 		if (m == NULL)
    756 			goto error;
    757 
    758 		sc->sc_if.if_ipackets++;
    759 		sc->sc_if.if_lastchange = time;
    760 		s = splimp();
    761 		if (IF_QFULL(&ipintrq)) {
    762 			IF_DROP(&ipintrq);
    763 			sc->sc_if.if_ierrors++;
    764 			sc->sc_if.if_iqdrops++;
    765 			m_freem(m);
    766 		} else {
    767 			IF_ENQUEUE(&ipintrq, m);
    768 			schednetisr(NETISR_IP);
    769 		}
    770 		splx(s);
    771 		goto newpack;
    772 	}
    773 	if (sc->sc_mp < sc->sc_ep) {
    774 		*sc->sc_mp++ = c;
    775 		sc->sc_escape = 0;
    776 		return;
    777 	}
    778 
    779 	/* can't put lower; would miss an extra frame */
    780 	sc->sc_flags |= SC_ERROR;
    781 
    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 int
    793 slioctl(ifp, cmd, data)
    794 	register struct ifnet *ifp;
    795 	u_long cmd;
    796 	caddr_t data;
    797 {
    798 	register struct ifaddr *ifa = (struct ifaddr *)data;
    799 	register struct ifreq *ifr;
    800 	register 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 	case SIOCADDMULTI:
    817 	case SIOCDELMULTI:
    818 		ifr = (struct ifreq *)data;
    819 		if (ifr == 0) {
    820 			error = EAFNOSUPPORT;		/* XXX */
    821 			break;
    822 		}
    823 		switch (ifr->ifr_addr.sa_family) {
    824 
    825 #ifdef INET
    826 		case AF_INET:
    827 			break;
    828 #endif
    829 
    830 		default:
    831 			error = EAFNOSUPPORT;
    832 			break;
    833 		}
    834 		break;
    835 
    836 	default:
    837 		error = EINVAL;
    838 	}
    839 	splx(s);
    840 	return (error);
    841 }
    842 #endif
    843