Home | History | Annotate | Line # | Download | only in net
if_sl.c revision 1.2
      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  *	@(#)if_sl.c	7.22 (Berkeley) 4/20/91
     34  *
     35  * PATCHES MAGIC                LEVEL   PATCH THAT GOT US HERE
     36  * --------------------         -----   ----------------------
     37  * CURRENT PATCH LEVEL:         1       00019
     38  * --------------------         -----   ----------------------
     39  *
     40  * 30 Aug 92    Poul-Henning Kamp       Stabilize SLIP on lossy lines/UARTS
     41  */
     42 
     43 /*
     44  * Serial Line interface
     45  *
     46  * Rick Adams
     47  * Center for Seismic Studies
     48  * 1300 N 17th Street, Suite 1450
     49  * Arlington, Virginia 22209
     50  * (703)276-7900
     51  * rick (at) seismo.ARPA
     52  * seismo!rick
     53  *
     54  * Pounded on heavily by Chris Torek (chris (at) mimsy.umd.edu, umcp-cs!chris).
     55  * N.B.: this belongs in netinet, not net, the way it stands now.
     56  * Should have a link-layer type designation, but wouldn't be
     57  * backwards-compatible.
     58  *
     59  * Converted to 4.3BSD Beta by Chris Torek.
     60  * Other changes made at Berkeley, based in part on code by Kirk Smith.
     61  * W. Jolitz added slip abort.
     62  *
     63  * Hacked almost beyond recognition by Van Jacobson (van (at) helios.ee.lbl.gov).
     64  * Added priority queuing for "interactive" traffic; hooks for TCP
     65  * header compression; ICMP filtering (at 2400 baud, some cretin
     66  * pinging you can use up all your bandwidth).  Made low clist behavior
     67  * more robust and slightly less likely to hang serial line.
     68  * Sped up a bunch of things.
     69  *
     70  * Note that splimp() is used throughout to block both (tty) input
     71  * interrupts and network activity; thus, splimp must be >= spltty.
     72  */
     73 
     74 /* $Header: /tank/opengrok/rsync2/NetBSD/src/sys/net/if_sl.c,v 1.2 1993/03/21 18:04:42 cgd Exp $ */
     75 /* from if_sl.c,v 1.11 84/10/04 12:54:47 rick Exp */
     76 
     77 #include "sl.h"
     78 #if NSL > 0
     79 
     80 #include "param.h"
     81 #include "proc.h"
     82 #include "mbuf.h"
     83 #include "buf.h"
     84 #include "dkstat.h"
     85 #include "socket.h"
     86 #include "ioctl.h"
     87 #include "file.h"
     88 #include "tty.h"
     89 #include "kernel.h"
     90 #include "conf.h"
     91 
     92 #include "if.h"
     93 #include "if_types.h"
     94 #include "netisr.h"
     95 #include "route.h"
     96 #if INET
     97 #include "netinet/in.h"
     98 #include "netinet/in_systm.h"
     99 #include "netinet/in_var.h"
    100 #include "netinet/ip.h"
    101 #else
    102 Huh? Slip without inet?
    103 #endif
    104 
    105 #include "machine/mtpr.h"
    106 
    107 #include "slcompress.h"
    108 #include "if_slvar.h"
    109 
    110 /*
    111  * SLMAX is a hard limit on input packet size.  To simplify the code
    112  * and improve performance, we require that packets fit in an mbuf
    113  * cluster, and if we get a compressed packet, there's enough extra
    114  * room to expand the header into a max length tcp/ip header (128
    115  * bytes).  So, SLMAX can be at most
    116  *	MCLBYTES - 128
    117  *
    118  * SLMTU is a hard limit on output packet size.  To insure good
    119  * interactive response, SLMTU wants to be the smallest size that
    120  * amortizes the header cost.  (Remember that even with
    121  * type-of-service queuing, we have to wait for any in-progress
    122  * packet to finish.  I.e., we wait, on the average, 1/2 * mtu /
    123  * cps, where cps is the line speed in characters per second.
    124  * E.g., 533ms wait for a 1024 byte MTU on a 9600 baud line.  The
    125  * average compressed header size is 6-8 bytes so any MTU > 90
    126  * bytes will give us 90% of the line bandwidth.  A 100ms wait is
    127  * tolerable (500ms is not), so want an MTU around 296.  (Since TCP
    128  * will send 256 byte segments (to allow for 40 byte headers), the
    129  * typical packet size on the wire will be around 260 bytes).  In
    130  * 4.3tahoe+ systems, we can set an MTU in a route so we do that &
    131  * leave the interface MTU relatively high (so we don't IP fragment
    132  * when acting as a gateway to someone using a stupid MTU).
    133  *
    134  * Similar considerations apply to SLIP_HIWAT:  It's the amount of
    135  * data that will be queued 'downstream' of us (i.e., in clists
    136  * waiting to be picked up by the tty output interrupt).  If we
    137  * queue a lot of data downstream, it's immune to our t.o.s. queuing.
    138  * E.g., if SLIP_HIWAT is 1024, the interactive traffic in mixed
    139  * telnet/ftp will see a 1 sec wait, independent of the mtu (the
    140  * wait is dependent on the ftp window size but that's typically
    141  * 1k - 4k).  So, we want SLIP_HIWAT just big enough to amortize
    142  * the cost (in idle time on the wire) of the tty driver running
    143  * off the end of its clists & having to call back slstart for a
    144  * new packet.  For a tty interface with any buffering at all, this
    145  * cost will be zero.  Even with a totally brain dead interface (like
    146  * the one on a typical workstation), the cost will be <= 1 character
    147  * time.  So, setting SLIP_HIWAT to ~100 guarantees that we'll lose
    148  * at most 1% while maintaining good interactive response.
    149  */
    150 #define BUFOFFSET	128
    151 #define	SLMAX		(MCLBYTES - BUFOFFSET)
    152 #define	SLBUFSIZE	(SLMAX + BUFOFFSET)
    153 #define	SLMTU		296
    154 #define	SLIP_HIWAT	roundup(50,CBSIZE)
    155 #define	CLISTRESERVE	1024	/* Can't let clists get too low */
    156 
    157 /*
    158  * SLIP ABORT ESCAPE MECHANISM:
    159  *	(inspired by HAYES modem escape arrangement)
    160  *	1sec escape 1sec escape 1sec escape { 1sec escape 1sec escape }
    161  *	signals a "soft" exit from slip mode by usermode process
    162  */
    163 
    164 #define	ABT_ESC		'\033'	/* can't be t_intr - distant host must know it*/
    165 #define ABT_WAIT	1	/* in seconds - idle before an escape & after */
    166 #define ABT_RECYCLE	(5*2+2)	/* in seconds - time window processing abort */
    167 
    168 #define ABT_SOFT	3	/* count of escapes */
    169 
    170 /*
    171  * The following disgusting hack gets around the problem that IP TOS
    172  * can't be set yet.  We want to put "interactive" traffic on a high
    173  * priority queue.  To decide if traffic is interactive, we check that
    174  * a) it is TCP and b) one of its ports is telnet, rlogin or ftp control.
    175  */
    176 static u_short interactive_ports[8] = {
    177 	0,	513,	0,	0,
    178 	0,	21,	0,	23,
    179 };
    180 #define INTERACTIVE(p) (interactive_ports[(p) & 7] == (p))
    181 
    182 struct sl_softc sl_softc[NSL];
    183 
    184 #define FRAME_END	 	0xc0		/* Frame End */
    185 #define FRAME_ESCAPE		0xdb		/* Frame Esc */
    186 #define TRANS_FRAME_END	 	0xdc		/* transposed frame end */
    187 #define TRANS_FRAME_ESCAPE 	0xdd		/* transposed frame esc */
    188 
    189 #define t_sc T_LINEP
    190 
    191 int sloutput(), slioctl(), ttrstrt();
    192 extern struct timeval time;
    193 
    194 /*
    195  * Called from boot code to establish sl interfaces.
    196  */
    197 slattach()
    198 {
    199 	register struct sl_softc *sc;
    200 	register int i = 0;
    201 
    202 	for (sc = sl_softc; i < NSL; sc++) {
    203 		sc->sc_if.if_name = "sl";
    204 		sc->sc_if.if_unit = i++;
    205 		sc->sc_if.if_mtu = SLMTU;
    206 		sc->sc_if.if_flags = IFF_POINTOPOINT;
    207 		sc->sc_if.if_type = IFT_SLIP;
    208 		sc->sc_if.if_ioctl = slioctl;
    209 		sc->sc_if.if_output = sloutput;
    210 		sc->sc_if.if_snd.ifq_maxlen = 50;
    211 		sc->sc_fastq.ifq_maxlen = 32;
    212 		if_attach(&sc->sc_if);
    213 	}
    214 }
    215 
    216 static int
    217 slinit(sc)
    218 	register struct sl_softc *sc;
    219 {
    220 	register caddr_t p;
    221 
    222 	if (sc->sc_ep == (u_char *) 0) {
    223 		MCLALLOC(p, M_WAIT);
    224 		if (p)
    225 			sc->sc_ep = (u_char *)p + SLBUFSIZE;
    226 		else {
    227 			printf("sl%d: can't allocate buffer\n", sc - sl_softc);
    228 			sc->sc_if.if_flags &= ~IFF_UP;
    229 			return (0);
    230 		}
    231 	}
    232 	sc->sc_buf = sc->sc_ep - SLMAX;
    233 	sc->sc_mp = sc->sc_buf;
    234 	sl_compress_init(&sc->sc_comp);
    235 	return (1);
    236 }
    237 
    238 /*
    239  * Line specific open routine.
    240  * Attach the given tty to the first available sl unit.
    241  */
    242 /* ARGSUSED */
    243 slopen(dev, tp)
    244 	dev_t dev;
    245 	register struct tty *tp;
    246 {
    247 	struct proc *p = curproc;		/* XXX */
    248 	register struct sl_softc *sc;
    249 	register int nsl;
    250 	int error;
    251 
    252 	if (error = suser(p->p_ucred, &p->p_acflag))
    253 		return (error);
    254 
    255 	if (tp->t_line == SLIPDISC)
    256 		return (0);
    257 
    258 	for (nsl = NSL, sc = sl_softc; --nsl >= 0; sc++)
    259 		if (sc->sc_ttyp == NULL) {
    260 			if (slinit(sc) == 0)
    261 				return (ENOBUFS);
    262 			tp->t_sc = (caddr_t)sc;
    263 			sc->sc_ttyp = tp;
    264 			sc->sc_if.if_baudrate = tp->t_ospeed;
    265 			ttyflush(tp, FREAD | FWRITE);
    266 			return (0);
    267 		}
    268 	return (ENXIO);
    269 }
    270 
    271 /*
    272  * Line specific close routine.
    273  * Detach the tty from the sl unit.
    274  * Mimics part of ttyclose().
    275  */
    276 slclose(tp)
    277 	struct tty *tp;
    278 {
    279 	register struct sl_softc *sc;
    280 	int s;
    281 
    282 	ttywflush(tp);
    283 	s = splimp();		/* actually, max(spltty, splnet) */
    284 	tp->t_line = 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 sltioctl(tp, cmd, data, flag)
    304 	struct tty *tp;
    305 	caddr_t data;
    306 {
    307 	struct sl_softc *sc = (struct sl_softc *)tp->t_sc;
    308 	int s;
    309 
    310 	switch (cmd) {
    311 	case SLIOCGUNIT:
    312 		*(int *)data = sc->sc_if.if_unit;
    313 		break;
    314 
    315 	case SLIOCGFLAGS:
    316 		*(int *)data = sc->sc_flags;
    317 		break;
    318 
    319 	case SLIOCSFLAGS:
    320 #define	SC_MASK	0xffff
    321 		s = splimp();
    322 		sc->sc_flags =
    323 		    (sc->sc_flags &~ SC_MASK) | ((*(int *)data) & SC_MASK);
    324 		splx(s);
    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 		m_freem(m);
    363 		return (EHOSTUNREACH);
    364 	}
    365 	ifq = &sc->sc_if.if_snd;
    366 	if ((ip = mtod(m, struct ip *))->ip_p == IPPROTO_TCP) {
    367 		register int p = ((int *)ip)[ip->ip_hl];
    368 
    369 		if (INTERACTIVE(p & 0xffff) || INTERACTIVE(p >> 16)) {
    370 			ifq = &sc->sc_fastq;
    371 			p = 1;
    372 		} else
    373 			p = 0;
    374 
    375 		if (sc->sc_flags & SC_COMPRESS) {
    376 			/*
    377 			 * The last parameter turns off connection id
    378 			 * compression for background traffic:  Since
    379 			 * fastq traffic can jump ahead of the background
    380 			 * traffic, we don't know what order packets will
    381 			 * go on the line.
    382 			 */
    383 			p = sl_compress_tcp(m, ip, &sc->sc_comp, p);
    384 			*mtod(m, u_char *) |= p;
    385 		}
    386 	} else if (sc->sc_flags & SC_NOICMP && ip->ip_p == IPPROTO_ICMP) {
    387 		m_freem(m);
    388 		return (0);
    389 	}
    390 	s = splimp();
    391 	if (IF_QFULL(ifq)) {
    392 		IF_DROP(ifq);
    393 		m_freem(m);
    394 		splx(s);
    395 		sc->sc_if.if_oerrors++;
    396 		return (ENOBUFS);
    397 	}
    398 	IF_ENQUEUE(ifq, m);
    399 	sc->sc_if.if_lastchange = time;
    400 	if (RB_LEN(&sc->sc_ttyp->t_out) == 0)
    401 		slstart(sc->sc_ttyp);
    402 	splx(s);
    403 	return (0);
    404 }
    405 
    406 /*
    407  * Start output on interface.  Get another datagram
    408  * to send from the interface queue and map it to
    409  * the interface before starting output.
    410  */
    411 slstart(tp)
    412 	register struct tty *tp;
    413 {
    414 	register struct sl_softc *sc = (struct sl_softc *)tp->t_sc;
    415 	register struct mbuf *m;
    416 	register u_char *cp;
    417 	int s;
    418 	struct mbuf *m2;
    419 
    420 	for (;;) {
    421 		/*
    422 		 * If there is more in the output queue, just send it now.
    423 		 * We are being called in lieu of ttstart and must do what
    424 		 * it would.
    425 		 */
    426 		if (RB_LEN(&tp->t_out) != 0) {
    427 			(*tp->t_oproc)(tp);
    428 			if (RB_LEN(&tp->t_out) > SLIP_HIWAT)
    429 				return;
    430 		}
    431 		/*
    432 		 * This happens briefly when the line shuts down.
    433 		 */
    434 		if (sc == NULL)
    435 			return;
    436 
    437 		/*
    438 		 * Get a packet and send it to the interface.
    439 		 */
    440 		s = splimp();
    441 		IF_DEQUEUE(&sc->sc_fastq, m);
    442 		if (m == NULL)
    443 			IF_DEQUEUE(&sc->sc_if.if_snd, m);
    444 		splx(s);
    445 		if (m == NULL)
    446 			return;
    447 		sc->sc_if.if_lastchange = time;
    448 		/*
    449 		 * If system is getting low on clists, just flush our
    450 		 * output queue (if the stuff was important, it'll get
    451 		 * retransmitted).
    452 		 */
    453 		if (RBSZ - RB_LEN(&tp->t_out) < SLMTU) {
    454 			m_freem(m);
    455 			sc->sc_if.if_collisions++;
    456 			continue;
    457 		}
    458 
    459 		/*
    460 		 * The extra FRAME_END will start up a new packet, and thus
    461 		 * will flush any accumulated garbage.  We do this whenever
    462 		 * the line may have been idle for some time.
    463 		 */
    464 		if (RB_LEN(&tp->t_out) == 0) {
    465 			++sc->sc_bytessent;
    466 			(void) putc(FRAME_END, &tp->t_out);
    467 		}
    468 
    469 		while (m) {
    470 			register u_char *ep;
    471 
    472 			cp = mtod(m, u_char *); ep = cp + m->m_len;
    473 			while (cp < ep) {
    474 				/*
    475 				 * Find out how many bytes in the string we can
    476 				 * handle without doing something special.
    477 				 */
    478 				register u_char *bp = cp;
    479 
    480 				while (cp < ep) {
    481 					switch (*cp++) {
    482 					case FRAME_ESCAPE:
    483 					case FRAME_END:
    484 						--cp;
    485 						goto out;
    486 					}
    487 				}
    488 				out:
    489 				if (cp > bp) {
    490 					int cc;
    491 					/*
    492 					 * Put n characters at once
    493 					 * into the tty output queue.
    494 					 */
    495 #ifdef was
    496 					if (b_to_q((char *)bp, cp - bp, &tp->t_outq))
    497 						break;
    498 					sc->sc_bytessent += cp - bp;
    499 #else
    500 #ifdef works
    501 					if (cc = RB_CONTIGPUT(&tp->t_out)) {
    502 						cc = min (cc, cp - bp);
    503 						bcopy((char *)bp,
    504 							tp->t_out.rb_tl, cc);
    505 						tp->t_out.rb_tl =
    506 				  RB_ROLLOVER(&tp->t_out, tp->t_out.rb_tl + cc);
    507 						sc->sc_bytessent += cc;
    508 						bp += cc;
    509 					} else
    510 						break;
    511 					if (cp > bp && cc = RB_CONTIGPUT(&tp->t_out)) {
    512 						cc = min (cc, cp - bp);
    513 						bcopy((char *)bp,
    514 							tp->t_out.rb_tl, cc);
    515 						tp->t_out.rb_tl =
    516 				  RB_ROLLOVER(&tp->t_out, tp->t_out.rb_tl + cc);
    517 						sc->sc_bytessent += cc;
    518 						bp += cc;
    519 					} else
    520 						break;
    521 #else
    522 					while (cp > bp && (cc = RB_CONTIGPUT(&tp->t_out))) {
    523 						cc = min (cc, cp - bp);
    524 						bcopy((char *)bp,
    525 							tp->t_out.rb_tl, cc);
    526 						tp->t_out.rb_tl =
    527 				  RB_ROLLOVER(&tp->t_out, tp->t_out.rb_tl + cc);
    528 						sc->sc_bytessent += cc;
    529 						bp += cc;
    530 					}
    531 #endif
    532 #endif
    533 				}
    534 				/*
    535 				 * If there are characters left in the mbuf,
    536 				 * the first one must be special..
    537 				 * Put it out in a different form.
    538 				 */
    539 				if (cp < ep) {
    540 					if (putc(FRAME_ESCAPE, &tp->t_out))
    541 						break;
    542 					if (putc(*cp++ == FRAME_ESCAPE ?
    543 					   TRANS_FRAME_ESCAPE : TRANS_FRAME_END,
    544 					   &tp->t_out)) {
    545 						(void) unputc(&tp->t_out);
    546 						break;
    547 					}
    548 					sc->sc_bytessent += 2;
    549 				}
    550 			}
    551 			MFREE(m, m2);
    552 			m = m2;
    553 		}
    554 
    555 		if (putc(FRAME_END, &tp->t_out)) {
    556 			/*
    557 			 * Not enough room.  Remove a char to make room
    558 			 * and end the packet normally.
    559 			 * If you get many collisions (more than one or two
    560 			 * a day) you probably do not have enough clists
    561 			 * and you should increase "nclist" in param.c.
    562 			 */
    563 			(void) unputc(&tp->t_out);
    564 			(void) putc(FRAME_END, &tp->t_out);
    565 			sc->sc_if.if_collisions++;
    566 		} else {
    567 			++sc->sc_bytessent;
    568 			sc->sc_if.if_opackets++;
    569 		}
    570 		sc->sc_if.if_obytes = sc->sc_bytessent;
    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 slinput(c, tp)
    621 	register int c;
    622 	register struct tty *tp;
    623 {
    624 	register struct sl_softc *sc;
    625 	register struct mbuf *m;
    626 	register int len;
    627 	int s;
    628 
    629 	tk_nin++;
    630 	sc = (struct sl_softc *)tp->t_sc;
    631 	if (sc == NULL)
    632 		return;
    633 	if (c > 255 || !(tp->t_state&TS_CARR_ON)) {       /* XXX 30 Aug 92*/
    634 		sc->sc_flags |= SC_ERROR;
    635 		return;
    636 	}
    637 
    638 	++sc->sc_bytesrcvd;
    639 	++sc->sc_if.if_ibytes;
    640 	c &= 0xff;			/* XXX */
    641 
    642 #ifdef ABT_ESC
    643 	if (sc->sc_flags & SC_ABORT) {
    644 		/* if we see an abort after "idle" time, count it */
    645 		if (c == ABT_ESC && time.tv_sec >= sc->sc_lasttime + ABT_WAIT) {
    646 			sc->sc_abortcount++;
    647 			/* record when the first abort escape arrived */
    648 			if (sc->sc_abortcount == 1)
    649 				sc->sc_starttime = time.tv_sec;
    650 		}
    651 		/*
    652 		 * if we have an abort, see that we have not run out of time,
    653 		 * or that we have an "idle" time after the complete escape
    654 		 * sequence
    655 		 */
    656 		if (sc->sc_abortcount) {
    657 			if (time.tv_sec >= sc->sc_starttime + ABT_RECYCLE)
    658 				sc->sc_abortcount = 0;
    659 			if (sc->sc_abortcount >= ABT_SOFT &&
    660 			    time.tv_sec >= sc->sc_lasttime + ABT_WAIT) {
    661 				slclose(tp);
    662 				return;
    663 			}
    664 		}
    665 		sc->sc_lasttime = time.tv_sec;
    666 	}
    667 #endif
    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) {			/* 30 Aug 92*/
    687 			sc->sc_flags &= ~SC_ERROR;
    688 			goto newpack;
    689 		}
    690 		len = sc->sc_mp - sc->sc_buf;
    691 
    692 		if (len < 3)
    693 			/* less than min length packet - ignore */
    694 			goto newpack;
    695 
    696 		if ((c = (*sc->sc_buf & 0xf0)) != (IPVERSION << 4)) {
    697 			if (c & 0x80)
    698 				c = TYPE_COMPRESSED_TCP;
    699 			else if (c == TYPE_UNCOMPRESSED_TCP)
    700 				*sc->sc_buf &= 0x4f; /* XXX */
    701 			/*
    702 			 * We've got something that's not an IP packet.
    703 			 * If compression is enabled, try to decompress it.
    704 			 * Otherwise, if `auto-enable' compression is on and
    705 			 * it's a reasonable packet, decompress it and then
    706 			 * enable compression.  Otherwise, drop it.
    707 			 */
    708 			if (sc->sc_flags & SC_COMPRESS) {
    709 				len = sl_uncompress_tcp(&sc->sc_buf, len,
    710 							(u_int)c, &sc->sc_comp);
    711 				if (len <= 0)
    712 					goto error;
    713 			} else if ((sc->sc_flags & SC_AUTOCOMP) &&
    714 			    c == TYPE_UNCOMPRESSED_TCP && len >= 40) {
    715 				len = sl_uncompress_tcp(&sc->sc_buf, len,
    716 							(u_int)c, &sc->sc_comp);
    717 				if (len <= 0)
    718 					goto error;
    719 				sc->sc_flags |= SC_COMPRESS;
    720 			} else
    721 				goto error;
    722 		}
    723 		m = sl_btom(sc, len);
    724 		if (m == NULL)
    725 			goto error;
    726 
    727 		sc->sc_if.if_ipackets++;
    728 		sc->sc_if.if_lastchange = time;
    729 		s = splimp();
    730 		if (IF_QFULL(&ipintrq)) {
    731 			IF_DROP(&ipintrq);
    732 			sc->sc_if.if_ierrors++;
    733 			sc->sc_if.if_iqdrops++;
    734 			m_freem(m);
    735 		} else {
    736 			IF_ENQUEUE(&ipintrq, m);
    737 			schednetisr(NETISR_IP);
    738 		}
    739 		splx(s);
    740 		goto newpack;
    741 	}
    742 	if (sc->sc_mp < sc->sc_ep) {
    743 		*sc->sc_mp++ = c;
    744 		sc->sc_escape = 0;
    745 		return;
    746 	}
    747 	sc->sc_flags |= SC_ERROR;			/* 30 Aug 92*/
    748 error:
    749 	sc->sc_if.if_ierrors++;
    750 newpack:
    751 	sc->sc_mp = sc->sc_buf = sc->sc_ep - SLMAX;
    752 	sc->sc_escape = 0;
    753 }
    754 
    755 /*
    756  * Process an ioctl request.
    757  */
    758 slioctl(ifp, cmd, data)
    759 	register struct ifnet *ifp;
    760 	int cmd;
    761 	caddr_t data;
    762 {
    763 	register struct ifaddr *ifa = (struct ifaddr *)data;
    764 	int s = splimp(), error = 0;
    765 
    766 	switch (cmd) {
    767 
    768 	case SIOCSIFADDR:
    769 		if (ifa->ifa_addr->sa_family == AF_INET)
    770 			ifp->if_flags |= IFF_UP;
    771 		else
    772 			error = EAFNOSUPPORT;
    773 		break;
    774 
    775 	case SIOCSIFDSTADDR:
    776 		if (ifa->ifa_addr->sa_family != AF_INET)
    777 			error = EAFNOSUPPORT;
    778 		break;
    779 
    780 	default:
    781 		error = EINVAL;
    782 	}
    783 	splx(s);
    784 	return (error);
    785 }
    786 #endif
    787