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