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