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