Home | History | Annotate | Line # | Download | only in net
if_sl.c revision 1.27
      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  *	$Id: if_sl.c,v 1.27 1994/03/08 07:27:23 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 "bpfilter.h"
     73 
     74 #include <sys/param.h>
     75 #include <sys/proc.h>
     76 #include <sys/mbuf.h>
     77 #include <sys/buf.h>
     78 #include <sys/dkstat.h>
     79 #include <sys/socket.h>
     80 #include <sys/ioctl.h>
     81 #include <sys/file.h>
     82 #include <sys/tty.h>
     83 #include <sys/kernel.h>
     84 #include <sys/conf.h>
     85 
     86 #include <machine/cpu.h>
     87 
     88 #include <net/if.h>
     89 #include <net/if_types.h>
     90 #include <net/netisr.h>
     91 #include <net/route.h>
     92 
     93 #if INET
     94 #include <netinet/in.h>
     95 #include <netinet/in_systm.h>
     96 #include <netinet/in_var.h>
     97 #include <netinet/ip.h>
     98 #else
     99 Huh? Slip without inet?
    100 #endif
    101 
    102 #include <net/slcompress.h>
    103 #include <net/if_slvar.h>
    104 #include <net/slip.h>
    105 
    106 #if NBPFILTER > 0
    107 #include <sys/time.h>
    108 #include <net/bpf.h>
    109 #endif
    110 
    111 /*
    112  * SLMAX is a hard limit on input packet size.  To simplify the code
    113  * and improve performance, we require that packets fit in an mbuf
    114  * cluster, and if we get a compressed packet, there's enough extra
    115  * room to expand the header into a max length tcp/ip header (128
    116  * bytes).  So, SLMAX can be at most
    117  *	MCLBYTES - 128
    118  *
    119  * SLMTU is a hard limit on output packet size.  To insure good
    120  * interactive response, SLMTU wants to be the smallest size that
    121  * amortizes the header cost.  (Remember that even with
    122  * type-of-service queuing, we have to wait for any in-progress
    123  * packet to finish.  I.e., we wait, on the average, 1/2 * mtu /
    124  * cps, where cps is the line speed in characters per second.
    125  * E.g., 533ms wait for a 1024 byte MTU on a 9600 baud line.  The
    126  * average compressed header size is 6-8 bytes so any MTU > 90
    127  * bytes will give us 90% of the line bandwidth.  A 100ms wait is
    128  * tolerable (500ms is not), so want an MTU around 296.  (Since TCP
    129  * will send 256 byte segments (to allow for 40 byte headers), the
    130  * typical packet size on the wire will be around 260 bytes).  In
    131  * 4.3tahoe+ systems, we can set an MTU in a route so we do that &
    132  * leave the interface MTU relatively high (so we don't IP fragment
    133  * when acting as a gateway to someone using a stupid MTU).
    134  *
    135  * Similar considerations apply to SLIP_HIWAT:  It's the amount of
    136  * data that will be queued 'downstream' of us (i.e., in clists
    137  * waiting to be picked up by the tty output interrupt).  If we
    138  * queue a lot of data downstream, it's immune to our t.o.s. queuing.
    139  * E.g., if SLIP_HIWAT is 1024, the interactive traffic in mixed
    140  * telnet/ftp will see a 1 sec wait, independent of the mtu (the
    141  * wait is dependent on the ftp window size but that's typically
    142  * 1k - 4k).  So, we want SLIP_HIWAT just big enough to amortize
    143  * the cost (in idle time on the wire) of the tty driver running
    144  * off the end of its clists & having to call back slstart for a
    145  * new packet.  For a tty interface with any buffering at all, this
    146  * cost will be zero.  Even with a totally brain dead interface (like
    147  * the one on a typical workstation), the cost will be <= 1 character
    148  * time.  So, setting SLIP_HIWAT to ~100 guarantees that we'll lose
    149  * at most 1% while maintaining good interactive response.
    150  */
    151 #if NBPFILTER > 0
    152 #define	BUFOFFSET	(128+sizeof(struct ifnet **)+SLIP_HDRLEN)
    153 #else
    154 #define	BUFOFFSET	(128+sizeof(struct ifnet **))
    155 #endif
    156 #define	SLMAX		(MCLBYTES - BUFOFFSET)
    157 #define	SLBUFSIZE	(SLMAX + BUFOFFSET)
    158 #ifndef SLMTU
    159 #define	SLMTU		296
    160 #endif
    161 #if (SLMTU < 3)
    162 Huh?  SLMTU way too small.
    163 #endif
    164 #define	SLIP_HIWAT	roundup(50,CBSIZE)
    165 #ifndef __NetBSD__						/* XXX - cgd */
    166 #define	CLISTRESERVE	1024	/* Can't let clists get too low */
    167 #endif	/* !__NetBSD__ */
    168 
    169 /*
    170  * SLIP ABORT ESCAPE MECHANISM:
    171  *	(inspired by HAYES modem escape arrangement)
    172  *	1sec escape 1sec escape 1sec escape { 1sec escape 1sec escape }
    173  *	within window time signals a "soft" exit from slip mode by remote end
    174  *	if the IFF_DEBUG flag is on.
    175  */
    176 #define	ABT_ESC		'\033'	/* can't be t_intr - distant host must know it*/
    177 #define	ABT_IDLE	1	/* in seconds - idle before an escape */
    178 #define	ABT_COUNT	3	/* count of escapes for abort */
    179 #define	ABT_WINDOW	(ABT_COUNT*2+2)	/* in seconds - time to count */
    180 
    181 struct sl_softc sl_softc[NSL];
    182 
    183 #define FRAME_END	 	0xc0		/* Frame End */
    184 #define FRAME_ESCAPE		0xdb		/* Frame Esc */
    185 #define TRANS_FRAME_END	 	0xdc		/* transposed frame end */
    186 #define TRANS_FRAME_ESCAPE 	0xdd		/* transposed frame esc */
    187 
    188 extern struct timeval time;
    189 
    190 static int slinit __P((struct sl_softc *));
    191 static struct mbuf *sl_btom __P((struct sl_softc *, int));
    192 
    193 /*
    194  * Called from boot code to establish sl interfaces.
    195  */
    196 void
    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_next = NULL;
    205 		sc->sc_if.if_unit = i++;
    206 		sc->sc_if.if_mtu = SLMTU;
    207 		sc->sc_if.if_flags =
    208 		    IFF_POINTOPOINT | SC_AUTOCOMP | IFF_MULTICAST;
    209 		sc->sc_if.if_type = IFT_SLIP;
    210 		sc->sc_if.if_ioctl = slioctl;
    211 		sc->sc_if.if_output = sloutput;
    212 		sc->sc_if.if_snd.ifq_maxlen = 50;
    213 		sc->sc_fastq.ifq_maxlen = 32;
    214 		if_attach(&sc->sc_if);
    215 #if NBPFILTER > 0
    216 		bpfattach(&sc->sc_bpf, &sc->sc_if, DLT_SLIP, SLIP_HDRLEN);
    217 #endif
    218 	}
    219 }
    220 
    221 static int
    222 slinit(sc)
    223 	register struct sl_softc *sc;
    224 {
    225 	register caddr_t p;
    226 
    227 	if (sc->sc_ep == (u_char *) 0) {
    228 		MCLALLOC(p, M_WAIT);
    229 		if (p)
    230 			sc->sc_ep = (u_char *)p + SLBUFSIZE;
    231 		else {
    232 			printf("sl%d: can't allocate buffer\n", sc - sl_softc);
    233 			sc->sc_if.if_flags &= ~IFF_UP;
    234 			return (0);
    235 		}
    236 	}
    237 	sc->sc_buf = sc->sc_ep - SLMAX;
    238 	sc->sc_mp = sc->sc_buf;
    239 	sl_compress_init(&sc->sc_comp);
    240 	return (1);
    241 }
    242 
    243 /*
    244  * Line specific open routine.
    245  * Attach the given tty to the first available sl unit.
    246  */
    247 /* ARGSUSED */
    248 int
    249 slopen(dev, tp)
    250 	dev_t dev;
    251 	register struct tty *tp;
    252 {
    253 	struct proc *p = curproc;		/* XXX */
    254 	register struct sl_softc *sc;
    255 	register int nsl;
    256 	int error;
    257 #ifdef __NetBSD__
    258 	int s;
    259 #endif
    260 
    261 	if (error = suser(p->p_ucred, &p->p_acflag))
    262 		return (error);
    263 
    264 	if (tp->t_line == SLIPDISC)
    265 		return (0);
    266 
    267 	for (nsl = NSL, sc = sl_softc; --nsl >= 0; sc++)
    268 		if (sc->sc_ttyp == NULL) {
    269 			if (slinit(sc) == 0)
    270 				return (ENOBUFS);
    271 			tp->t_sc = (caddr_t)sc;
    272 			sc->sc_ttyp = tp;
    273 			sc->sc_if.if_baudrate = tp->t_ospeed;
    274 			ttyflush(tp, FREAD | FWRITE);
    275 #ifdef __NetBSD__
    276 			/*
    277 			 * make sure tty output queue is large enough
    278 			 * to hold a full-sized packet (including frame
    279 			 * end, and a possible extra frame end).  full-sized
    280 			 * packet occupies a max of 2*SLMTU bytes (because
    281 			 * of possible escapes), and add two on for frame
    282 			 * ends.
    283 			 */
    284 			s = spltty();
    285 			if (tp->t_outq.c_cn < 2*SLMTU+2) {
    286 				sc->sc_oldbufsize = tp->t_outq.c_cn;
    287 				sc->sc_oldbufquot = tp->t_outq.c_cq != 0;
    288 
    289 				clfree(&tp->t_outq);
    290 				error = clalloc(&tp->t_outq, 3*SLMTU, 0);
    291 				if (error) {
    292 					splx(s);
    293 					return(error);
    294 				}
    295 			} else
    296 				sc->sc_oldbufsize = sc->sc_oldbufquot = 0;
    297 			splx(s);
    298 #endif /* __NetBSD__ */
    299 			return (0);
    300 		}
    301 	return (ENXIO);
    302 }
    303 
    304 /*
    305  * Line specific close routine.
    306  * Detach the tty from the sl unit.
    307  */
    308 void
    309 slclose(tp)
    310 	struct tty *tp;
    311 {
    312 	register struct sl_softc *sc;
    313 	int s;
    314 
    315 	ttywflush(tp);
    316 	s = splimp();		/* actually, max(spltty, splnet) */
    317 	tp->t_line = 0;
    318 	sc = (struct sl_softc *)tp->t_sc;
    319 	if (sc != NULL) {
    320 		if_down(&sc->sc_if);
    321 		sc->sc_ttyp = NULL;
    322 		tp->t_sc = NULL;
    323 		MCLFREE((caddr_t)(sc->sc_ep - SLBUFSIZE));
    324 		sc->sc_ep = 0;
    325 		sc->sc_mp = 0;
    326 		sc->sc_buf = 0;
    327 	}
    328 #ifdef __NetBSD__
    329 	/* if necessary, install a new outq buffer of the appropriate size */
    330 	if (sc->sc_oldbufsize != 0) {
    331 		clfree(&tp->t_outq);
    332 		clalloc(&tp->t_outq, sc->sc_oldbufsize, sc->sc_oldbufquot);
    333 	}
    334 #endif
    335 	splx(s);
    336 }
    337 
    338 /*
    339  * Line specific (tty) ioctl routine.
    340  * Provide a way to get the sl unit number.
    341  */
    342 /* ARGSUSED */
    343 int
    344 sltioctl(tp, cmd, data, flag)
    345 	struct tty *tp;
    346 	int cmd;
    347 	caddr_t data;
    348 	int flag;
    349 {
    350 	struct sl_softc *sc = (struct sl_softc *)tp->t_sc;
    351 	int s;
    352 
    353 	switch (cmd) {
    354 	case SLIOCGUNIT:
    355 		*(int *)data = sc->sc_if.if_unit;
    356 		break;
    357 
    358 	default:
    359 		return (-1);
    360 	}
    361 	return (0);
    362 }
    363 
    364 /*
    365  * Queue a packet.  Start transmission if not active.
    366  * Compression happens in slstart; if we do it here, IP TOS
    367  * will cause us to not compress "background" packets, because
    368  * ordering gets hosed.  It can be done for all packets in slstart.
    369  */
    370 int
    371 sloutput(ifp, m, dst, rtp)
    372 	struct ifnet *ifp;
    373 	register struct mbuf *m;
    374 	struct sockaddr *dst;
    375 	struct rtentry *rtp;
    376 {
    377 	register struct sl_softc *sc = &sl_softc[ifp->if_unit];
    378 	register struct ip *ip;
    379 	register struct ifqueue *ifq;
    380 	int s;
    381 
    382 	/*
    383 	 * `Cannot happen' (see slioctl).  Someday we will extend
    384 	 * the line protocol to support other address families.
    385 	 */
    386 	if (dst->sa_family != AF_INET) {
    387 		printf("sl%d: af%d not supported\n", sc->sc_if.if_unit,
    388 			dst->sa_family);
    389 		m_freem(m);
    390 		sc->sc_if.if_noproto++;
    391 		return (EAFNOSUPPORT);
    392 	}
    393 
    394 	if (sc->sc_ttyp == NULL) {
    395 		m_freem(m);
    396 		return (ENETDOWN);	/* sort of */
    397 	}
    398 	if ((sc->sc_ttyp->t_state & TS_CARR_ON) == 0 &&
    399 	    (sc->sc_ttyp->t_cflag & CLOCAL) == 0) {
    400 		m_freem(m);
    401 		return (EHOSTUNREACH);
    402 	}
    403 	ifq = &sc->sc_if.if_snd;
    404 	ip = mtod(m, struct ip *);
    405 	if (sc->sc_if.if_flags & SC_NOICMP && ip->ip_p == IPPROTO_ICMP) {
    406 		m_freem(m);
    407 		return (ENETRESET);		/* XXX ? */
    408 	}
    409 	if (ip->ip_tos & IPTOS_LOWDELAY)
    410 		ifq = &sc->sc_fastq;
    411 	s = splimp();
    412 	if (sc->sc_oqlen && sc->sc_ttyp->t_outq.c_cc == sc->sc_oqlen) {
    413 		struct timeval tv = time;
    414 
    415 		/* if output's been stalled for too long, and restart */
    416 		timevalsub(&tv, &sc->sc_if.if_lastchange);
    417 		if (tv.tv_sec > 0) {
    418 			sc->sc_otimeout++;
    419 			slstart(sc->sc_ttyp);
    420 		}
    421 	}
    422 	if (IF_QFULL(ifq)) {
    423 		IF_DROP(ifq);
    424 		m_freem(m);
    425 		splx(s);
    426 		sc->sc_if.if_oerrors++;
    427 		return (ENOBUFS);
    428 	}
    429 	IF_ENQUEUE(ifq, m);
    430 	sc->sc_if.if_lastchange = time;
    431 	if ((sc->sc_oqlen = sc->sc_ttyp->t_outq.c_cc) == 0)
    432 		slstart(sc->sc_ttyp);
    433 	splx(s);
    434 	return (0);
    435 }
    436 
    437 /*
    438  * Start output on interface.  Get another datagram
    439  * to send from the interface queue and map it to
    440  * the interface before starting output.
    441  */
    442 void
    443 slstart(tp)
    444 	register struct tty *tp;
    445 {
    446 	register struct sl_softc *sc = (struct sl_softc *)tp->t_sc;
    447 	register struct mbuf *m;
    448 	register u_char *cp;
    449 	register struct ip *ip;
    450 	int s;
    451 	struct mbuf *m2;
    452 #if NBPFILTER > 0
    453 	u_char bpfbuf[SLMTU + SLIP_HDRLEN];
    454 	register int len;
    455 #endif
    456 #ifndef __NetBSD__						/* XXX - cgd */
    457 	extern int cfreecount;
    458 #endif
    459 
    460 	for (;;) {
    461 		/*
    462 		 * If there is more in the output queue, just send it now.
    463 		 * We are being called in lieu of ttstart and must do what
    464 		 * it would.
    465 		 */
    466 		if (tp->t_outq.c_cc != 0) {
    467 			(*tp->t_oproc)(tp);
    468 			if (tp->t_outq.c_cc > SLIP_HIWAT)
    469 				return;
    470 		}
    471 		/*
    472 		 * This happens briefly when the line shuts down.
    473 		 */
    474 		if (sc == NULL)
    475 			return;
    476 
    477 #ifdef __NetBSD__						/* XXX - cgd */
    478 		/*
    479 		 * Do not remove the packet from the IP queue if it
    480 		 * doesn't look like the packet will fit into the
    481 		 * current serial output queue, with a packet full of
    482 		 * escapes this could be as bad as SLMTU*2+2.
    483 		 */
    484 		if (tp->t_outq.c_cn - tp->t_outq.c_cc < 2*SLMTU+2)
    485 			return;
    486 #endif /* __NetBSD__ */
    487 
    488 		/*
    489 		 * Get a packet and send it to the interface.
    490 		 */
    491 		s = splimp();
    492 		IF_DEQUEUE(&sc->sc_fastq, m);
    493 		if (m)
    494 			sc->sc_if.if_omcasts++;		/* XXX */
    495 		else
    496 			IF_DEQUEUE(&sc->sc_if.if_snd, m);
    497 		splx(s);
    498 		if (m == NULL)
    499 			return;
    500 
    501 		/*
    502 		 * We do the header compression here rather than in sloutput
    503 		 * because the packets will be out of order if we are using TOS
    504 		 * queueing, and the connection id compression will get
    505 		 * munged when this happens.
    506 		 */
    507 #if NBPFILTER > 0
    508 		if (sc->sc_bpf) {
    509 			/*
    510 			 * We need to save the TCP/IP header before it's
    511 			 * compressed.  To avoid complicated code, we just
    512 			 * copy the entire packet into a stack buffer (since
    513 			 * this is a serial line, packets should be short
    514 			 * and/or the copy should be negligible cost compared
    515 			 * to the packet transmission time).
    516 			 */
    517 			register struct mbuf *m1 = m;
    518 			register u_char *cp = bpfbuf + SLIP_HDRLEN;
    519 
    520 			len = 0;
    521 			do {
    522 				register int mlen = m1->m_len;
    523 
    524 				bcopy(mtod(m1, caddr_t), cp, mlen);
    525 				cp += mlen;
    526 				len += mlen;
    527 			} while (m1 = m1->m_next);
    528 		}
    529 #endif
    530 		if ((ip = mtod(m, struct ip *))->ip_p == IPPROTO_TCP) {
    531 			if (sc->sc_if.if_flags & SC_COMPRESS)
    532 				*mtod(m, u_char *) |= sl_compress_tcp(m, ip,
    533 				    &sc->sc_comp, 1);
    534 		}
    535 #if NBPFILTER > 0
    536 		if (sc->sc_bpf) {
    537 			/*
    538 			 * Put the SLIP pseudo-"link header" in place.  The
    539 			 * compressed header is now at the beginning of the
    540 			 * mbuf.
    541 			 */
    542 			bpfbuf[SLX_DIR] = SLIPDIR_OUT;
    543 			bcopy(mtod(m, caddr_t), &bpfbuf[SLX_CHDR], CHDR_LEN);
    544 			bpf_tap(sc->sc_bpf, bpfbuf, len + SLIP_HDRLEN);
    545 		}
    546 #endif
    547 		sc->sc_if.if_lastchange = time;
    548 
    549 #ifndef __NetBSD__						/* XXX - cgd */
    550 		/*
    551 		 * If system is getting low on clists, just flush our
    552 		 * output queue (if the stuff was important, it'll get
    553 		 * retransmitted).
    554 		 */
    555 		if (cfreecount < CLISTRESERVE + SLMTU) {
    556 			m_freem(m);
    557 			sc->sc_if.if_collisions++;
    558 			continue;
    559 		}
    560 #endif /* !__NetBSD__ */
    561 		/*
    562 		 * The extra FRAME_END will start up a new packet, and thus
    563 		 * will flush any accumulated garbage.  We do this whenever
    564 		 * the line may have been idle for some time.
    565 		 */
    566 		if (tp->t_outq.c_cc == 0) {
    567 			++sc->sc_if.if_obytes;
    568 			(void) putc(FRAME_END, &tp->t_outq);
    569 		}
    570 
    571 		while (m) {
    572 			register u_char *ep;
    573 
    574 			cp = mtod(m, u_char *); ep = cp + m->m_len;
    575 			while (cp < ep) {
    576 				/*
    577 				 * Find out how many bytes in the string we can
    578 				 * handle without doing something special.
    579 				 */
    580 				register u_char *bp = cp;
    581 
    582 				while (cp < ep) {
    583 					switch (*cp++) {
    584 					case FRAME_ESCAPE:
    585 					case FRAME_END:
    586 						--cp;
    587 						goto out;
    588 					}
    589 				}
    590 				out:
    591 				if (cp > bp) {
    592 					/*
    593 					 * Put n characters at once
    594 					 * into the tty output queue.
    595 					 */
    596 #ifdef __NetBSD__						/* XXX - cgd */
    597 					if (b_to_q((u_char *)bp, cp - bp,
    598 #else
    599 					if (b_to_q((char *)bp, cp - bp,
    600 #endif
    601 					    &tp->t_outq))
    602 						break;
    603 					sc->sc_if.if_obytes += cp - bp;
    604 				}
    605 				/*
    606 				 * If there are characters left in the mbuf,
    607 				 * the first one must be special..
    608 				 * Put it out in a different form.
    609 				 */
    610 				if (cp < ep) {
    611 					if (putc(FRAME_ESCAPE, &tp->t_outq))
    612 						break;
    613 					if (putc(*cp++ == FRAME_ESCAPE ?
    614 					   TRANS_FRAME_ESCAPE : TRANS_FRAME_END,
    615 					   &tp->t_outq)) {
    616 						(void) unputc(&tp->t_outq);
    617 						break;
    618 					}
    619 					sc->sc_if.if_obytes += 2;
    620 				}
    621 			}
    622 			MFREE(m, m2);
    623 			m = m2;
    624 		}
    625 
    626 		if (putc(FRAME_END, &tp->t_outq)) {
    627 			/*
    628 			 * Not enough room.  Remove a char to make room
    629 			 * and end the packet normally.
    630 			 * If you get many collisions (more than one or two
    631 			 * a day) you probably do not have enough clists
    632 			 * and you should increase "nclist" in param.c.
    633 			 */
    634 			(void) unputc(&tp->t_outq);
    635 			(void) putc(FRAME_END, &tp->t_outq);
    636 			sc->sc_if.if_collisions++;
    637 		} else {
    638 			++sc->sc_if.if_obytes;
    639 			sc->sc_if.if_opackets++;
    640 		}
    641 	}
    642 }
    643 
    644 /*
    645  * Copy data buffer to mbuf chain; add ifnet pointer.
    646  */
    647 static struct mbuf *
    648 sl_btom(sc, len)
    649 	register struct sl_softc *sc;
    650 	register int len;
    651 {
    652 	register struct mbuf *m;
    653 
    654 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    655 	if (m == NULL)
    656 		return (NULL);
    657 
    658 	/*
    659 	 * If we have more than MHLEN bytes, it's cheaper to
    660 	 * queue the cluster we just filled & allocate a new one
    661 	 * for the input buffer.  Otherwise, fill the mbuf we
    662 	 * allocated above.  Note that code in the input routine
    663 	 * guarantees that packet will fit in a cluster.
    664 	 */
    665 	if (len >= MHLEN) {
    666 		MCLGET(m, M_DONTWAIT);
    667 		if ((m->m_flags & M_EXT) == 0) {
    668 			/*
    669 			 * we couldn't get a cluster - if memory's this
    670 			 * low, it's time to start dropping packets.
    671 			 */
    672 			(void) m_free(m);
    673 			return (NULL);
    674 		}
    675 		sc->sc_ep = mtod(m, u_char *) + SLBUFSIZE;
    676 		m->m_data = (caddr_t)sc->sc_buf;
    677 		m->m_ext.ext_buf = (caddr_t)((int)sc->sc_buf &~ MCLOFSET);
    678 	} else
    679 		bcopy((caddr_t)sc->sc_buf, mtod(m, caddr_t), len);
    680 
    681 	m->m_len = len;
    682 	m->m_pkthdr.len = len;
    683 	m->m_pkthdr.rcvif = &sc->sc_if;
    684 	return (m);
    685 }
    686 
    687 /*
    688  * tty interface receiver interrupt.
    689  */
    690 void
    691 slinput(c, tp)
    692 	register int c;
    693 	register struct tty *tp;
    694 {
    695 	register struct sl_softc *sc;
    696 	register struct mbuf *m;
    697 	register int len;
    698 	int s;
    699 #if NBPFILTER > 0
    700 	u_char chdr[CHDR_LEN];
    701 #endif
    702 
    703 	tk_nin++;
    704 	sc = (struct sl_softc *)tp->t_sc;
    705 	if (sc == NULL)
    706 		return;
    707 	if (c & TTY_ERRORMASK || ((tp->t_state & TS_CARR_ON) == 0 &&
    708 	    (tp->t_cflag & CLOCAL) == 0)) {
    709 		sc->sc_flags |= SC_ERROR;
    710 		return;
    711 	}
    712 	c &= TTY_CHARMASK;
    713 
    714 	++sc->sc_if.if_ibytes;
    715 
    716 	if (sc->sc_if.if_flags & IFF_DEBUG) {
    717 		if (c == ABT_ESC) {
    718 			/*
    719 			 * If we have a previous abort, see whether
    720 			 * this one is within the time limit.
    721 			 */
    722 			if (sc->sc_abortcount &&
    723 			    time.tv_sec >= sc->sc_starttime + ABT_WINDOW)
    724 				sc->sc_abortcount = 0;
    725 			/*
    726 			 * If we see an abort after "idle" time, count it;
    727 			 * record when the first abort escape arrived.
    728 			 */
    729 			if (time.tv_sec >= sc->sc_lasttime + ABT_IDLE) {
    730 				if (++sc->sc_abortcount == 1)
    731 					sc->sc_starttime = time.tv_sec;
    732 				if (sc->sc_abortcount >= ABT_COUNT) {
    733 					slclose(tp);
    734 					return;
    735 				}
    736 			}
    737 		} else
    738 			sc->sc_abortcount = 0;
    739 		sc->sc_lasttime = time.tv_sec;
    740 	}
    741 
    742 	switch (c) {
    743 
    744 	case TRANS_FRAME_ESCAPE:
    745 		if (sc->sc_escape)
    746 			c = FRAME_ESCAPE;
    747 		break;
    748 
    749 	case TRANS_FRAME_END:
    750 		if (sc->sc_escape)
    751 			c = FRAME_END;
    752 		break;
    753 
    754 	case FRAME_ESCAPE:
    755 		sc->sc_escape = 1;
    756 		return;
    757 
    758 	case FRAME_END:
    759 		if(sc->sc_flags & SC_ERROR) {
    760 			sc->sc_flags &= ~SC_ERROR;
    761 			goto newpack;
    762 		}
    763 		len = sc->sc_mp - sc->sc_buf;
    764 		if (len < 3)
    765 			/* less than min length packet - ignore */
    766 			goto newpack;
    767 
    768 #if NBPFILTER > 0
    769 		if (sc->sc_bpf) {
    770 			/*
    771 			 * Save the compressed header, so we
    772 			 * can tack it on later.  Note that we
    773 			 * will end up copying garbage in some
    774 			 * cases but this is okay.  We remember
    775 			 * where the buffer started so we can
    776 			 * compute the new header length.
    777 			 */
    778 			bcopy(sc->sc_buf, chdr, CHDR_LEN);
    779 		}
    780 #endif
    781 
    782 		if ((c = (*sc->sc_buf & 0xf0)) != (IPVERSION << 4)) {
    783 			if (c & 0x80)
    784 				c = TYPE_COMPRESSED_TCP;
    785 			else if (c == TYPE_UNCOMPRESSED_TCP)
    786 				*sc->sc_buf &= 0x4f; /* XXX */
    787 			/*
    788 			 * We've got something that's not an IP packet.
    789 			 * If compression is enabled, try to decompress it.
    790 			 * Otherwise, if `auto-enable' compression is on and
    791 			 * it's a reasonable packet, decompress it and then
    792 			 * enable compression.  Otherwise, drop it.
    793 			 */
    794 			if (sc->sc_if.if_flags & SC_COMPRESS) {
    795 				len = sl_uncompress_tcp(&sc->sc_buf, len,
    796 							(u_int)c, &sc->sc_comp);
    797 				if (len <= 0)
    798 					goto error;
    799 			} else if ((sc->sc_if.if_flags & SC_AUTOCOMP) &&
    800 			    c == TYPE_UNCOMPRESSED_TCP && len >= 40) {
    801 				len = sl_uncompress_tcp(&sc->sc_buf, len,
    802 							(u_int)c, &sc->sc_comp);
    803 				if (len <= 0)
    804 					goto error;
    805 				sc->sc_if.if_flags |= SC_COMPRESS;
    806 			} else
    807 				goto error;
    808 		}
    809 #if NBPFILTER > 0
    810 		if (sc->sc_bpf) {
    811 			/*
    812 			 * Put the SLIP pseudo-"link header" in place.
    813 			 * We couldn't do this any earlier since
    814 			 * decompression probably moved the buffer
    815 			 * pointer.  Then, invoke BPF.
    816 			 */
    817 			register u_char *hp = sc->sc_buf - SLIP_HDRLEN;
    818 
    819 			hp[SLX_DIR] = SLIPDIR_IN;
    820 			bcopy(chdr, &hp[SLX_CHDR], CHDR_LEN);
    821 			bpf_tap(sc->sc_bpf, hp, len + SLIP_HDRLEN);
    822 		}
    823 #endif
    824 		m = sl_btom(sc, len);
    825 		if (m == NULL)
    826 			goto error;
    827 
    828 		sc->sc_if.if_ipackets++;
    829 		sc->sc_if.if_lastchange = time;
    830 		s = splimp();
    831 		if (IF_QFULL(&ipintrq)) {
    832 			IF_DROP(&ipintrq);
    833 			sc->sc_if.if_ierrors++;
    834 			sc->sc_if.if_iqdrops++;
    835 			m_freem(m);
    836 		} else {
    837 			IF_ENQUEUE(&ipintrq, m);
    838 			schednetisr(NETISR_IP);
    839 		}
    840 		splx(s);
    841 		goto newpack;
    842 	}
    843 	if (sc->sc_mp < sc->sc_ep) {
    844 		*sc->sc_mp++ = c;
    845 		sc->sc_escape = 0;
    846 		return;
    847 	}
    848 
    849 	/* can't put lower; would miss an extra frame */
    850 	sc->sc_flags |= SC_ERROR;
    851 
    852 error:
    853 	sc->sc_if.if_ierrors++;
    854 newpack:
    855 	sc->sc_mp = sc->sc_buf = sc->sc_ep - SLMAX;
    856 	sc->sc_escape = 0;
    857 }
    858 
    859 /*
    860  * Process an ioctl request.
    861  */
    862 int
    863 slioctl(ifp, cmd, data)
    864 	register struct ifnet *ifp;
    865 	int cmd;
    866 	caddr_t data;
    867 {
    868 	register struct ifaddr *ifa = (struct ifaddr *)data;
    869 	register struct ifreq *ifr;
    870 	register int s = splimp(), error = 0;
    871 
    872 	switch (cmd) {
    873 
    874 	case SIOCSIFADDR:
    875 		if (ifa->ifa_addr->sa_family == AF_INET)
    876 			ifp->if_flags |= IFF_UP;
    877 		else
    878 			error = EAFNOSUPPORT;
    879 		break;
    880 
    881 	case SIOCSIFDSTADDR:
    882 		if (ifa->ifa_addr->sa_family != AF_INET)
    883 			error = EAFNOSUPPORT;
    884 		break;
    885 
    886 	case SIOCADDMULTI:
    887 	case SIOCDELMULTI:
    888 		ifr = (struct ifreq *)data;
    889 		if (ifr == 0) {
    890 			error = EAFNOSUPPORT;		/* XXX */
    891 			break;
    892 		}
    893 		switch (ifr->ifr_addr.sa_family) {
    894 
    895 #ifdef INET
    896 		case AF_INET:
    897 			break;
    898 #endif
    899 
    900 		default:
    901 			error = EAFNOSUPPORT;
    902 			break;
    903 		}
    904 		break;
    905 
    906 	default:
    907 		error = EINVAL;
    908 	}
    909 	splx(s);
    910 	return (error);
    911 }
    912 #endif
    913