Home | History | Annotate | Line # | Download | only in net
if_sl.c revision 1.13
      1 /*
      2  * Copyright (c) 1987, 1989 Regents of the University of California.
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. All advertising materials mentioning features or use of this software
     14  *    must display the following acknowledgement:
     15  *	This product includes software developed by the University of
     16  *	California, Berkeley and its contributors.
     17  * 4. Neither the name of the University nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  *
     33  *	from: if_sl.c,v 1.11 84/10/04 12:54:47 rick Exp
     34  *	from: @(#)if_sl.c	7.22 (Berkeley) 4/20/91
     35  *	if_sl.c,v 1.8 1993/05/22 11:42:11 cgd Exp
     36  */
     37 
     38 /*
     39  * Serial Line interface
     40  *
     41  * Rick Adams
     42  * Center for Seismic Studies
     43  * 1300 N 17th Street, Suite 1450
     44  * Arlington, Virginia 22209
     45  * (703)276-7900
     46  * rick (at) seismo.ARPA
     47  * seismo!rick
     48  *
     49  * Pounded on heavily by Chris Torek (chris (at) mimsy.umd.edu, umcp-cs!chris).
     50  * N.B.: this belongs in netinet, not net, the way it stands now.
     51  * Should have a link-layer type designation, but wouldn't be
     52  * backwards-compatible.
     53  *
     54  * Converted to 4.3BSD Beta by Chris Torek.
     55  * Other changes made at Berkeley, based in part on code by Kirk Smith.
     56  * W. Jolitz added slip abort.
     57  *
     58  * Hacked almost beyond recognition by Van Jacobson (van (at) helios.ee.lbl.gov).
     59  * Added priority queuing for "interactive" traffic; hooks for TCP
     60  * header compression; ICMP filtering (at 2400 baud, some cretin
     61  * pinging you can use up all your bandwidth).  Made low clist behavior
     62  * more robust and slightly less likely to hang serial line.
     63  * Sped up a bunch of things.
     64  *
     65  * Note that splimp() is used throughout to block both (tty) input
     66  * interrupts and network activity; thus, splimp must be >= spltty.
     67  */
     68 
     69 #include "sl.h"
     70 #if NSL > 0
     71 
     72 #include "param.h"
     73 #include "proc.h"
     74 #include "mbuf.h"
     75 #include "buf.h"
     76 #include "dkstat.h"
     77 #include "socket.h"
     78 #include "ioctl.h"
     79 #include "file.h"
     80 #include "tty.h"
     81 #include "kernel.h"
     82 #include "conf.h"
     83 
     84 #include "if.h"
     85 #include "if_types.h"
     86 #include "netisr.h"
     87 #include "route.h"
     88 #if INET
     89 #include "netinet/in.h"
     90 #include "netinet/in_systm.h"
     91 #include "netinet/in_var.h"
     92 #include "netinet/ip.h"
     93 #else
     94 Huh? Slip without inet?
     95 #endif
     96 
     97 #include "machine/mtpr.h"
     98 
     99 #include "slcompress.h"
    100 #include "if_slvar.h"
    101 #include "slip.h"
    102 
    103 #include "bpfilter.h"
    104 #if NBPFILTER > 0
    105 #include <sys/time.h>
    106 #include <net/bpf.h>
    107 #endif
    108 
    109 /*
    110  * SLMAX is a hard limit on input packet size.  To simplify the code
    111  * and improve performance, we require that packets fit in an mbuf
    112  * cluster, and if we get a compressed packet, there's enough extra
    113  * room to expand the header into a max length tcp/ip header (128
    114  * bytes).  So, SLMAX can be at most
    115  *	MCLBYTES - 128
    116  *
    117  * SLMTU is a hard limit on output packet size.  To insure good
    118  * interactive response, SLMTU wants to be the smallest size that
    119  * amortizes the header cost.  (Remember that even with
    120  * type-of-service queuing, we have to wait for any in-progress
    121  * packet to finish.  I.e., we wait, on the average, 1/2 * mtu /
    122  * cps, where cps is the line speed in characters per second.
    123  * E.g., 533ms wait for a 1024 byte MTU on a 9600 baud line.  The
    124  * average compressed header size is 6-8 bytes so any MTU > 90
    125  * bytes will give us 90% of the line bandwidth.  A 100ms wait is
    126  * tolerable (500ms is not), so want an MTU around 296.  (Since TCP
    127  * will send 256 byte segments (to allow for 40 byte headers), the
    128  * typical packet size on the wire will be around 260 bytes).  In
    129  * 4.3tahoe+ systems, we can set an MTU in a route so we do that &
    130  * leave the interface MTU relatively high (so we don't IP fragment
    131  * when acting as a gateway to someone using a stupid MTU).
    132  *
    133  * Similar considerations apply to SLIP_HIWAT:  It's the amount of
    134  * data that will be queued 'downstream' of us (i.e., in clists
    135  * waiting to be picked up by the tty output interrupt).  If we
    136  * queue a lot of data downstream, it's immune to our t.o.s. queuing.
    137  * E.g., if SLIP_HIWAT is 1024, the interactive traffic in mixed
    138  * telnet/ftp will see a 1 sec wait, independent of the mtu (the
    139  * wait is dependent on the ftp window size but that's typically
    140  * 1k - 4k).  So, we want SLIP_HIWAT just big enough to amortize
    141  * the cost (in idle time on the wire) of the tty driver running
    142  * off the end of its clists & having to call back slstart for a
    143  * new packet.  For a tty interface with any buffering at all, this
    144  * cost will be zero.  Even with a totally brain dead interface (like
    145  * the one on a typical workstation), the cost will be <= 1 character
    146  * time.  So, setting SLIP_HIWAT to ~100 guarantees that we'll lose
    147  * at most 1% while maintaining good interactive response.
    148  */
    149 #if NBPFILTER > 0
    150 #define BUFOFFSET (128+sizeof(struct ifnet **)+SLIP_HDRLEN)
    151 #else
    152 #define BUFOFFSET (128+sizeof(struct ifnet **))
    153 #endif
    154 #define	SLMAX		(MCLBYTES - BUFOFFSET)
    155 #define	SLBUFSIZE	(SLMAX + BUFOFFSET)
    156 #define	SLMTU		296	/* try 1006 later */
    157 #define	SLIP_HIWAT	roundup(50,CBSIZE)
    158 
    159 /*
    160  * SLIP ABORT ESCAPE MECHANISM:
    161  *	(inspired by HAYES modem escape arrangement)
    162  *	1sec escape 1sec escape 1sec escape { 1sec escape 1sec escape }
    163  *	signals a "soft" exit from slip mode by usermode process
    164  */
    165 
    166 #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 #define t_sc T_LINEP
    194 
    195 int sloutput(), slioctl(), ttrstrt();
    196 extern struct timeval time;
    197 
    198 /*
    199  * Called from boot code to establish sl interfaces.
    200  */
    201 slattach()
    202 {
    203 	register struct sl_softc *sc;
    204 	register int i = 0;
    205 
    206 	for (sc = sl_softc; i < NSL; sc++) {
    207 		sc->sc_if.if_name = "sl";
    208 		sc->sc_if.if_unit = i++;
    209 		sc->sc_if.if_mtu = SLMTU;
    210 		sc->sc_if.if_flags = IFF_POINTOPOINT;
    211 		sc->sc_if.if_type = IFT_SLIP;
    212 		sc->sc_if.if_ioctl = slioctl;
    213 		sc->sc_if.if_output = sloutput;
    214 		sc->sc_if.if_snd.ifq_maxlen = 50;
    215 		sc->sc_fastq.ifq_maxlen = 32;
    216 		if_attach(&sc->sc_if);
    217 #if NBPFILTER > 0
    218 		bpfattach(&sc->sc_bpf, &sc->sc_if, DLT_SLIP,
    219 			SLIP_HDRLEN);
    220 #endif
    221 	}
    222 }
    223 
    224 static int
    225 slinit(sc)
    226 	register struct sl_softc *sc;
    227 {
    228 	register caddr_t p;
    229 
    230 	if (sc->sc_ep == (u_char *) 0) {
    231 		MCLALLOC(p, M_WAIT);
    232 		if (p)
    233 			sc->sc_ep = (u_char *)p + SLBUFSIZE;
    234 		else {
    235 			printf("sl%d: can't allocate buffer\n", sc - sl_softc);
    236 			sc->sc_if.if_flags &= ~IFF_UP;
    237 			return (0);
    238 		}
    239 	}
    240 	sc->sc_buf = sc->sc_ep - SLMAX;
    241 	sc->sc_mp = sc->sc_buf;
    242 	sl_compress_init(&sc->sc_comp);
    243 	return (1);
    244 }
    245 
    246 /*
    247  * Line specific open routine.
    248  * Attach the given tty to the first available sl unit.
    249  */
    250 /* ARGSUSED */
    251 int
    252 slopen(dev_t dev, struct tty *tp)
    253 {
    254 	struct proc *p = curproc;		/* XXX */
    255 	register struct sl_softc *sc;
    256 	register int nsl;
    257 	int error;
    258 
    259 	if (error = suser(p->p_ucred, &p->p_acflag))
    260 		return (error);
    261 
    262 	if (tp->t_line == SLIPDISC)
    263 		return (0);
    264 
    265 	for (nsl = NSL, sc = sl_softc; --nsl >= 0; sc++)
    266 		if (sc->sc_ttyp == NULL) {
    267 			if (slinit(sc) == 0)
    268 				return (ENOBUFS);
    269 			tp->t_sc = (caddr_t)sc;
    270 			sc->sc_ttyp = tp;
    271 			sc->sc_if.if_baudrate = tp->t_ospeed;
    272 			ttyflush(tp, FREAD | FWRITE);
    273 			return (0);
    274 		}
    275 	return (ENXIO);
    276 }
    277 
    278 /*
    279  * Line specific close routine.
    280  * Detach the tty from the sl unit.
    281  * Mimics part of ttyclose().
    282  */
    283 void
    284 slclose(tp, flag)
    285 	struct tty *tp;
    286 	int flag;
    287 {
    288 	register struct sl_softc *sc;
    289 	int s;
    290 
    291 	ttywflush(tp);
    292 	s = splimp();		/* actually, max(spltty, splnet) */
    293 	tp->t_line = 0;
    294 	sc = (struct sl_softc *)tp->t_sc;
    295 	if (sc != NULL) {
    296 		if_down(&sc->sc_if);
    297 		sc->sc_ttyp = NULL;
    298 		tp->t_sc = NULL;
    299 		MCLFREE((caddr_t)(sc->sc_ep - SLBUFSIZE));
    300 		sc->sc_ep = 0;
    301 		sc->sc_mp = 0;
    302 		sc->sc_buf = 0;
    303 	}
    304 	splx(s);
    305 }
    306 
    307 /*
    308  * Line specific (tty) ioctl routine.
    309  * Provide a way to get the sl unit number.
    310  */
    311 /* ARGSUSED */
    312 sltioctl(tp, cmd, data, flag)
    313 	struct tty *tp;
    314 	caddr_t data;
    315 {
    316 	struct sl_softc *sc = (struct sl_softc *)tp->t_sc;
    317 	struct proc *p = curproc;		/* XXX */
    318 	int error;
    319 	int s;
    320 
    321 	switch (cmd) {
    322 	case SLIOCGUNIT:
    323 		*(int *)data = sc->sc_if.if_unit;
    324 		break;
    325 
    326 	case SLIOCGFLAGS:
    327 		*(int *)data = sc->sc_flags;
    328 		break;
    329 
    330 	case SLIOCSFLAGS:
    331 		if (error = suser(p->p_ucred, &p->p_acflag))
    332 			return (error);
    333 #define	SC_MASK	0xffff
    334 		s = splimp();
    335 		sc->sc_flags =
    336 		    (sc->sc_flags &~ SC_MASK) | ((*(int *)data) & SC_MASK);
    337 		splx(s);
    338 		break;
    339 
    340 	default:
    341 		return (-1);
    342 	}
    343 	return (0);
    344 }
    345 
    346 /*
    347  * Queue a packet.  Start transmission if not active.
    348  */
    349 sloutput(ifp, m, dst)
    350 	struct ifnet *ifp;
    351 	register struct mbuf *m;
    352 	struct sockaddr *dst;
    353 {
    354 	register struct sl_softc *sc = &sl_softc[ifp->if_unit];
    355 	register struct ip *ip;
    356 	register struct ifqueue *ifq;
    357 	int s;
    358 
    359 	/*
    360 	 * `Cannot happen' (see slioctl).  Someday we will extend
    361 	 * the line protocol to support other address families.
    362 	 */
    363 	if (dst->sa_family != AF_INET) {
    364 		printf("sl%d: af%d not supported\n", sc->sc_if.if_unit,
    365 			dst->sa_family);
    366 		m_freem(m);
    367 		return (EAFNOSUPPORT);
    368 	}
    369 
    370 	if (sc->sc_ttyp == NULL) {
    371 		m_freem(m);
    372 		return (ENETDOWN);	/* sort of */
    373 	}
    374 	if ((sc->sc_ttyp->t_state & TS_CARR_ON) == 0) {
    375 		m_freem(m);
    376 		return (EHOSTUNREACH);
    377 	}
    378 	ifq = &sc->sc_if.if_snd;
    379 	if ((ip = mtod(m, struct ip *))->ip_p == IPPROTO_TCP) {
    380 		u_short srcport = ntohs(((short *)ip)[ip->ip_hl << 1]);
    381 		u_short dstport = ntohs(((short *)ip)[(ip->ip_hl << 1) + 1]);
    382 
    383 		if (INTERACTIVE(srcport) || INTERACTIVE(dstport)) {
    384 			ifq = &sc->sc_fastq;
    385 		}
    386 
    387 	} else if (sc->sc_flags & SC_NOICMP && ip->ip_p == IPPROTO_ICMP) {
    388 		m_freem(m);
    389 		return (0);
    390 	}
    391 	s = splimp();
    392 	if (IF_QFULL(ifq)) {
    393 		IF_DROP(ifq);
    394 		m_freem(m);
    395 		splx(s);
    396 		sc->sc_if.if_oerrors++;
    397 		return (ENOBUFS);
    398 	}
    399 	IF_ENQUEUE(ifq, m);
    400 	sc->sc_if.if_lastchange = time;
    401 	if (sc->sc_ttyp->t_outq.c_cc == 0)
    402 		slstart(sc->sc_ttyp);
    403 	splx(s);
    404 	return (0);
    405 }
    406 
    407 /*
    408  * Start output on interface.  Get another datagram
    409  * to send from the interface queue and map it to
    410  * the interface before starting output.
    411  */
    412 void
    413 slstart(tp)
    414 	register struct tty *tp;
    415 {
    416 	register struct sl_softc *sc = (struct sl_softc *)tp->t_sc;
    417 	register struct mbuf *m;
    418 	register u_char *cp;
    419 	register struct ip *ip;
    420 	int s;
    421 	struct mbuf *m2;
    422 #if NBPFILTER > 0
    423 	u_char bpfbuf[SLMTU + SLIP_HDRLEN];
    424 	register int len;
    425 #endif
    426 
    427 	for (;;) {
    428 		/*
    429 		 * If there is more in the output queue, just send it now.
    430 		 * We are being called in lieu of ttstart and must do what
    431 		 * it would.
    432 		 */
    433 		if (tp->t_outq.c_cc != 0) {
    434 			(*tp->t_oproc)(tp);
    435 			if (tp->t_outq.c_cc > SLIP_HIWAT)
    436 				return;
    437 		}
    438 		/*
    439 		 * This happens briefly when the line shuts down.
    440 		 */
    441 		if (sc == NULL)
    442 			return;
    443 
    444 		/*
    445 		 * Do not remove the packet from the IP queue if it
    446 		 * doesn't look like the packet will fit into the
    447 		 * current COM output queue, with a packet full of
    448 		 * escapes this could be as bad as SLMTU*2.  The size
    449 		 * of the ring buffer must be at least SLMTU*2 to
    450 		 * avoid deadlock.
    451 		 */
    452 		if (tp->t_outq.c_cn - tp->t_outq.c_cc < 2 * SLMTU)
    453 			return;
    454 
    455 		/*
    456 		 * Get a packet and send it to the interface.
    457 		 */
    458 		s = splimp();
    459 		IF_DEQUEUE(&sc->sc_fastq, m);
    460 		if (m == NULL)
    461 			IF_DEQUEUE(&sc->sc_if.if_snd, m);
    462 		splx(s);
    463 		if (m == NULL)
    464 			return;
    465 		/*
    466 		 * We do the header compression here rather than in sl_output
    467 		 * because the packets will be out of order if we are using TOS
    468 		 * queueing, and the connection id compression will get messed
    469 		 * up when this happens.
    470 		 */
    471 #if NBPFILTER > 0
    472 		if (sc->sc_bpf) {
    473 		/*
    474 		 * We need to save the TCP/IP header before it's compressed.
    475 		 * To avoid complicated code, we just copy the entire packet
    476 		 * into a stack buffer (since this is a serial line, packets
    477 		 * should be short and/or the copy should be negligible cost
    478 		 * compared to the packet transmission time).
    479 		*/
    480 			register struct mbuf *m1 = m;
    481 			register u_char *cp = bpfbuf + SLIP_HDRLEN;
    482 			len = 0;
    483 			do {
    484 				register int mlen = m1->m_len;
    485 
    486 				bcopy(mtod(m1, caddr_t), cp, mlen);
    487 				cp += mlen;
    488 				len += mlen;
    489 			} while (m1 = m1->m_next);
    490 		}
    491 #endif
    492 	        if ((ip = mtod(m, struct ip *))->ip_p == IPPROTO_TCP) {
    493 			if (sc->sc_flags & SC_COMPRESS)
    494 				*mtod(m, u_char *) |= sl_compress_tcp(m, ip, &sc->sc_comp, 1);
    495 		}
    496 #if NBPFILTER > 0
    497 		if (sc->sc_bpf) {
    498                 /*
    499                  * Put the SLIP pseudo-"link header" in place.  The compressed
    500                  * header is now at the beginning of the mbuf.
    501                  */
    502 			bpfbuf[SLX_DIR] = SLIPDIR_OUT;
    503 			bcopy(mtod(m, caddr_t), &bpfbuf[SLX_CHDR], CHDR_LEN);
    504 			bpf_tap(sc->sc_bpf, bpfbuf, len + SLIP_HDRLEN);
    505 		}
    506 #endif
    507 
    508 		sc->sc_if.if_lastchange = time;
    509 
    510 		/*
    511 		 * The extra FRAME_END will start up a new packet, and thus
    512 		 * will flush any accumulated garbage.  We do this whenever
    513 		 * the line may have been idle for some time.
    514 		 */
    515 		if (tp->t_outq.c_cc == 0) {
    516 			++sc->sc_bytessent;
    517 			(void) putc(FRAME_END, &tp->t_outq);
    518 		}
    519 
    520 		while (m) {
    521 			register u_char *ep;
    522 
    523 			cp = mtod(m, u_char *); ep = cp + m->m_len;
    524 			while (cp < ep) {
    525 				/*
    526 				 * Find out how many bytes in the string we can
    527 				 * handle without doing something special.
    528 				 */
    529 				register u_char *bp = cp;
    530 
    531 				while (cp < ep) {
    532 					switch (*cp++) {
    533 					case FRAME_ESCAPE:
    534 					case FRAME_END:
    535 						--cp;
    536 						goto out;
    537 					}
    538 				}
    539 				out:
    540 				if (cp > bp) {
    541 					/*
    542 					 * Put n characters at once
    543 					 * into the tty output queue.
    544 					 */
    545 					if (b_to_q((u_char *)bp, cp - bp, &tp->t_outq))
    546 						break;
    547 					sc->sc_bytessent += cp - bp;
    548 				}
    549 				/*
    550 				 * If there are characters left in the mbuf,
    551 				 * the first one must be special..
    552 				 * Put it out in a different form.
    553 				 */
    554 				if (cp < ep) {
    555 					if (putc(FRAME_ESCAPE, &tp->t_outq))
    556 						break;
    557 					if (putc(*cp++ == FRAME_ESCAPE ?
    558 					   TRANS_FRAME_ESCAPE : TRANS_FRAME_END,
    559 					   &tp->t_outq)) {
    560 						(void) unputc(&tp->t_outq);
    561 						break;
    562 					}
    563 					sc->sc_bytessent += 2;
    564 				}
    565 			}
    566 			MFREE(m, m2);
    567 			m = m2;
    568 		}
    569 
    570 		if (putc(FRAME_END, &tp->t_outq)) {
    571 			/*
    572 			 * Not enough room.  Remove a char to make room
    573 			 * and end the packet normally.
    574 			 * If you get many collisions (more than one or two
    575 			 * a day) you probably do not have enough clists
    576 			 * and you should increase "nclist" in param.c.
    577 			 */
    578 			(void) unputc(&tp->t_outq);
    579 			(void) putc(FRAME_END, &tp->t_outq);
    580 			sc->sc_if.if_collisions++;
    581 		} else {
    582 			++sc->sc_bytessent;
    583 			sc->sc_if.if_opackets++;
    584 		}
    585 		sc->sc_if.if_obytes = sc->sc_bytessent;
    586 	}
    587 }
    588 
    589 /*
    590  * Copy data buffer to mbuf chain; add ifnet pointer.
    591  */
    592 static struct mbuf *
    593 sl_btom(sc, len)
    594 	register struct sl_softc *sc;
    595 	register int len;
    596 {
    597 	register struct mbuf *m;
    598 
    599 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    600 	if (m == NULL)
    601 		return (NULL);
    602 
    603 	/*
    604 	 * If we have more than MHLEN bytes, it's cheaper to
    605 	 * queue the cluster we just filled & allocate a new one
    606 	 * for the input buffer.  Otherwise, fill the mbuf we
    607 	 * allocated above.  Note that code in the input routine
    608 	 * guarantees that packet will fit in a cluster.
    609 	 */
    610 	if (len >= MHLEN) {
    611 		MCLGET(m, M_DONTWAIT);
    612 		if ((m->m_flags & M_EXT) == 0) {
    613 			/*
    614 			 * we couldn't get a cluster - if memory's this
    615 			 * low, it's time to start dropping packets.
    616 			 */
    617 			(void) m_free(m);
    618 			return (NULL);
    619 		}
    620 		sc->sc_ep = mtod(m, u_char *) + SLBUFSIZE;
    621 		m->m_data = (caddr_t)sc->sc_buf;
    622 		m->m_ext.ext_buf = (caddr_t)((int)sc->sc_buf &~ MCLOFSET);
    623 	} else
    624 		bcopy((caddr_t)sc->sc_buf, mtod(m, caddr_t), len);
    625 
    626 	m->m_len = len;
    627 	m->m_pkthdr.len = len;
    628 	m->m_pkthdr.rcvif = &sc->sc_if;
    629 	return (m);
    630 }
    631 
    632 /*
    633  * tty interface receiver interrupt.
    634  */
    635 void
    636 slinput(c, tp)
    637 	register int c;
    638 	register struct tty *tp;
    639 {
    640 	register struct sl_softc *sc;
    641 	register struct mbuf *m;
    642 	register int len;
    643 	int s;
    644 #if NBPFILTER > 0
    645 	u_char chdr[CHDR_LEN];
    646 #endif
    647 	tk_nin++;
    648 	sc = (struct sl_softc *)tp->t_sc;
    649 	if (sc == NULL)
    650 		return;
    651 	if (c & TTY_ERRORMASK || ((tp->t_state & TS_CARR_ON) == 0 &&
    652 				  (tp->t_cflag & CLOCAL) == 0)) {
    653 		sc->sc_flags |= SC_ERROR;
    654 		return;
    655 	}
    656 
    657 	++sc->sc_bytesrcvd;
    658 	++sc->sc_if.if_ibytes;
    659 
    660 #ifdef ABT_ESC
    661 	if (sc->sc_flags & SC_ABORT) {
    662 		/* if we see an abort after "idle" time, count it */
    663 		if (c == ABT_ESC && time.tv_sec >= sc->sc_lasttime + ABT_WAIT) {
    664 			sc->sc_abortcount++;
    665 			/* record when the first abort escape arrived */
    666 			if (sc->sc_abortcount == 1)
    667 				sc->sc_starttime = time.tv_sec;
    668 		}
    669 		/*
    670 		 * if we have an abort, see that we have not run out of time,
    671 		 * or that we have an "idle" time after the complete escape
    672 		 * sequence
    673 		 */
    674 		if (sc->sc_abortcount) {
    675 			if (time.tv_sec >= sc->sc_starttime + ABT_RECYCLE)
    676 				sc->sc_abortcount = 0;
    677 			if (sc->sc_abortcount >= ABT_SOFT &&
    678 			    time.tv_sec >= sc->sc_lasttime + ABT_WAIT) {
    679 				slclose(tp);
    680 				return;
    681 			}
    682 		}
    683 		sc->sc_lasttime = time.tv_sec;
    684 	}
    685 #endif
    686 
    687 	switch (c) {
    688 
    689 	case TRANS_FRAME_ESCAPE:
    690 		if (sc->sc_escape)
    691 			c = FRAME_ESCAPE;
    692 		break;
    693 
    694 	case TRANS_FRAME_END:
    695 		if (sc->sc_escape)
    696 			c = FRAME_END;
    697 		break;
    698 
    699 	case FRAME_ESCAPE:
    700 		sc->sc_escape = 1;
    701 		return;
    702 
    703 	case FRAME_END:
    704 		if(sc->sc_flags & SC_ERROR) {
    705 			sc->sc_flags &= ~SC_ERROR;
    706 			goto newpack;
    707 		}
    708 		len = sc->sc_mp - sc->sc_buf;
    709 
    710 		if (len < 3)
    711 			/* less than min length packet - ignore */
    712 			goto newpack;
    713 
    714 #if NBPFILTER > 0
    715 		if (sc->sc_bpf)
    716 			/*
    717 			 * Save the compressed header, so we can
    718 			 * tack it on later.  Note that we just
    719 			 * we will end up copying garbage in some
    720 			 * cases but this is okay.  We remember
    721 			 * where the buffer started so we can
    722 			 * compute the new header length.
    723 			 */
    724 			bcopy(sc->sc_buf, chdr, CHDR_LEN);
    725 #endif
    726 		if ((c = (*sc->sc_buf & 0xf0)) != (IPVERSION << 4)) {
    727 			if (c & 0x80)
    728 				c = TYPE_COMPRESSED_TCP;
    729 			else if (c == TYPE_UNCOMPRESSED_TCP)
    730 				*sc->sc_buf &= 0x4f; /* XXX */
    731 			/*
    732 			 * We've got something that's not an IP packet.
    733 			 * If compression is enabled, try to decompress it.
    734 			 * Otherwise, if `auto-enable' compression is on and
    735 			 * it's a reasonable packet, decompress it and then
    736 			 * enable compression.  Otherwise, drop it.
    737 			 */
    738 			if (sc->sc_flags & SC_COMPRESS) {
    739 				len = sl_uncompress_tcp(&sc->sc_buf, len,
    740 							(u_int)c, &sc->sc_comp);
    741 				if (len <= 0)
    742 					goto error;
    743 			} else if ((sc->sc_flags & SC_AUTOCOMP) &&
    744 			    c == TYPE_UNCOMPRESSED_TCP && len >= 40) {
    745 				len = sl_uncompress_tcp(&sc->sc_buf, len,
    746 							(u_int)c, &sc->sc_comp);
    747 				if (len <= 0)
    748 					goto error;
    749 				sc->sc_flags |= SC_COMPRESS;
    750 			} else
    751 				goto error;
    752 		}
    753 #if NBPFILTER > 0
    754 		if (sc->sc_bpf) {
    755 			/*
    756 			 * Put the SLIP pseudo-"link header" in place.
    757 			 * We couldn't do this any earlier since
    758 			 * decompression probably moved the buffer
    759 			 * pointer.  Then, invoke BPF.
    760 			 */
    761 			register u_char *hp = sc->sc_buf - SLIP_HDRLEN;
    762 
    763 			hp[SLX_DIR] = SLIPDIR_IN;
    764 			bcopy(chdr, &hp[SLX_CHDR], CHDR_LEN);
    765 			bpf_tap(sc->sc_bpf, hp, len + SLIP_HDRLEN);
    766 		}
    767 #endif
    768 		m = sl_btom(sc, len);
    769 		if (m == NULL)
    770 			goto error;
    771 
    772 		sc->sc_if.if_ipackets++;
    773 		sc->sc_if.if_lastchange = time;
    774 		s = splimp();
    775 		if (IF_QFULL(&ipintrq)) {
    776 			IF_DROP(&ipintrq);
    777 			sc->sc_if.if_ierrors++;
    778 			sc->sc_if.if_iqdrops++;
    779 			m_freem(m);
    780 		} else {
    781 			IF_ENQUEUE(&ipintrq, m);
    782 			schednetisr(NETISR_IP);
    783 		}
    784 		splx(s);
    785 		goto newpack;
    786 	}
    787 	if (sc->sc_mp < sc->sc_ep) {
    788 		*sc->sc_mp++ = c;
    789 		sc->sc_escape = 0;
    790 		return;
    791 	}
    792 	sc->sc_flags |= SC_ERROR;
    793 error:
    794 	sc->sc_if.if_ierrors++;
    795 newpack:
    796 	sc->sc_mp = sc->sc_buf = sc->sc_ep - SLMAX;
    797 	sc->sc_escape = 0;
    798 }
    799 
    800 /*
    801  * Process an ioctl request.
    802  */
    803 slioctl(ifp, cmd, data)
    804 	register struct ifnet *ifp;
    805 	int cmd;
    806 	caddr_t data;
    807 {
    808 	register struct ifaddr *ifa = (struct ifaddr *)data;
    809 	int s = splimp(), error = 0;
    810 
    811 	switch (cmd) {
    812 
    813 	case SIOCSIFADDR:
    814 		if (ifa->ifa_addr->sa_family == AF_INET)
    815 			ifp->if_flags |= IFF_UP;
    816 		else
    817 			error = EAFNOSUPPORT;
    818 		break;
    819 
    820 	case SIOCSIFDSTADDR:
    821 		if (ifa->ifa_addr->sa_family != AF_INET)
    822 			error = EAFNOSUPPORT;
    823 		break;
    824 
    825 	default:
    826 		error = EINVAL;
    827 	}
    828 	splx(s);
    829 	return (error);
    830 }
    831 #endif
    832