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