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