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