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