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