if_sl.c revision 1.86
1/*	$NetBSD: if_sl.c,v 1.86 2004/08/19 20:58:24 christos 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. Neither the name of the University nor the names of its contributors
16 *    may be used to endorse or promote products derived from this software
17 *    without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 *	@(#)if_sl.c	8.9 (Berkeley) 1/9/95
32 */
33
34/*
35 * Serial Line interface
36 *
37 * Rick Adams
38 * Center for Seismic Studies
39 * 1300 N 17th Street, Suite 1450
40 * Arlington, Virginia 22209
41 * (703)276-7900
42 * rick@seismo.ARPA
43 * seismo!rick
44 *
45 * Pounded on heavily by Chris Torek (chris@mimsy.umd.edu, umcp-cs!chris).
46 * N.B.: this belongs in netinet, not net, the way it stands now.
47 * Should have a link-layer type designation, but wouldn't be
48 * backwards-compatible.
49 *
50 * Converted to 4.3BSD Beta by Chris Torek.
51 * Other changes made at Berkeley, based in part on code by Kirk Smith.
52 * W. Jolitz added slip abort.
53 *
54 * Hacked almost beyond recognition by Van Jacobson (van@helios.ee.lbl.gov).
55 * Added priority queuing for "interactive" traffic; hooks for TCP
56 * header compression; ICMP filtering (at 2400 baud, some cretin
57 * pinging you can use up all your bandwidth).  Made low clist behavior
58 * more robust and slightly less likely to hang serial line.
59 * Sped up a bunch of things.
60 */
61
62#include <sys/cdefs.h>
63__KERNEL_RCSID(0, "$NetBSD: if_sl.c,v 1.86 2004/08/19 20:58:24 christos Exp $");
64
65#include "sl.h"
66#if NSL > 0
67
68#include "opt_inet.h"
69#include "bpfilter.h"
70
71#include <sys/param.h>
72#include <sys/proc.h>
73#include <sys/malloc.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/conf.h>
81#include <sys/tty.h>
82#include <sys/kernel.h>
83#if __NetBSD__
84#include <sys/systm.h>
85#endif
86
87#include <machine/cpu.h>
88#include <machine/intr.h>
89
90#include <net/if.h>
91#include <net/if_types.h>
92#include <net/netisr.h>
93#include <net/route.h>
94
95#ifdef INET
96#include <netinet/in.h>
97#include <netinet/in_systm.h>
98#include <netinet/in_var.h>
99#include <netinet/ip.h>
100#else
101#error Slip without inet?
102#endif
103
104#include <net/slcompress.h>
105#include <net/if_slvar.h>
106#include <net/slip.h>
107
108#if NBPFILTER > 0
109#include <sys/time.h>
110#include <net/bpf.h>
111#endif
112
113/*
114 * SLMAX is a hard limit on input packet size.  To simplify the code
115 * and improve performance, we require that packets fit in an mbuf
116 * cluster, and if we get a compressed packet, there's enough extra
117 * room to expand the header into a max length tcp/ip header (128
118 * bytes).  So, SLMAX can be at most
119 *	MCLBYTES - 128
120 *
121 * SLMTU is a hard limit on output packet size.  To insure good
122 * interactive response, SLMTU wants to be the smallest size that
123 * amortizes the header cost.  (Remember that even with
124 * type-of-service queuing, we have to wait for any in-progress
125 * packet to finish.  I.e., we wait, on the average, 1/2 * mtu /
126 * cps, where cps is the line speed in characters per second.
127 * E.g., 533ms wait for a 1024 byte MTU on a 9600 baud line.  The
128 * average compressed header size is 6-8 bytes so any MTU > 90
129 * bytes will give us 90% of the line bandwidth.  A 100ms wait is
130 * tolerable (500ms is not), so want an MTU around 296.  (Since TCP
131 * will send 256 byte segments (to allow for 40 byte headers), the
132 * typical packet size on the wire will be around 260 bytes).  In
133 * 4.3tahoe+ systems, we can set an MTU in a route so we do that &
134 * leave the interface MTU relatively high (so we don't IP fragment
135 * when acting as a gateway to someone using a stupid MTU).
136 *
137 * Similar considerations apply to SLIP_HIWAT:  It's the amount of
138 * data that will be queued 'downstream' of us (i.e., in clists
139 * waiting to be picked up by the tty output interrupt).  If we
140 * queue a lot of data downstream, it's immune to our t.o.s. queuing.
141 * E.g., if SLIP_HIWAT is 1024, the interactive traffic in mixed
142 * telnet/ftp will see a 1 sec wait, independent of the mtu (the
143 * wait is dependent on the ftp window size but that's typically
144 * 1k - 4k).  So, we want SLIP_HIWAT just big enough to amortize
145 * the cost (in idle time on the wire) of the tty driver running
146 * off the end of its clists & having to call back slstart for a
147 * new packet.  For a tty interface with any buffering at all, this
148 * cost will be zero.  Even with a totally brain dead interface (like
149 * the one on a typical workstation), the cost will be <= 1 character
150 * time.  So, setting SLIP_HIWAT to ~100 guarantees that we'll lose
151 * at most 1% while maintaining good interactive response.
152 */
153#define	BUFOFFSET	(128+sizeof(struct ifnet **)+SLIP_HDRLEN)
154#define	SLMAX		(MCLBYTES - BUFOFFSET)
155#define	SLBUFSIZE	(SLMAX + BUFOFFSET)
156#ifndef SLMTU
157#define	SLMTU		296
158#endif
159#if (SLMTU < 3)
160#error SLMTU way too small.
161#endif
162#define	SLIP_HIWAT	roundup(50,CBSIZE)
163#ifndef __NetBSD__					/* XXX - cgd */
164#define	CLISTRESERVE	1024	/* Can't let clists get too low */
165#endif	/* !__NetBSD__ */
166
167/*
168 * SLIP ABORT ESCAPE MECHANISM:
169 *	(inspired by HAYES modem escape arrangement)
170 *	1sec escape 1sec escape 1sec escape { 1sec escape 1sec escape }
171 *	within window time signals a "soft" exit from slip mode by remote end
172 *	if the IFF_DEBUG flag is on.
173 */
174#define	ABT_ESC		'\033'	/* can't be t_intr - distant host must know it*/
175#define	ABT_IDLE	1	/* in seconds - idle before an escape */
176#define	ABT_COUNT	3	/* count of escapes for abort */
177#define	ABT_WINDOW	(ABT_COUNT*2+2)	/* in seconds - time to count */
178
179struct sl_softc sl_softc[NSL];
180
181#define FRAME_END	 	0xc0		/* Frame End */
182#define FRAME_ESCAPE		0xdb		/* Frame Esc */
183#define TRANS_FRAME_END	 	0xdc		/* transposed frame end */
184#define TRANS_FRAME_ESCAPE 	0xdd		/* transposed frame esc */
185
186#ifndef __HAVE_GENERIC_SOFT_INTERRUPTS
187void	slnetisr(void);
188#endif
189void	slintr(void *);
190
191static int slinit __P((struct sl_softc *));
192static struct mbuf *sl_btom __P((struct sl_softc *, int));
193
194/*
195 * Called from boot code to establish sl interfaces.
196 */
197void
198slattach()
199{
200	struct sl_softc *sc;
201	int i = 0;
202
203	for (sc = sl_softc; i < NSL; sc++) {
204		sc->sc_unit = i;		/* XXX */
205		snprintf(sc->sc_if.if_xname, sizeof(sc->sc_if.if_xname),
206		    "sl%d", i++);
207		sc->sc_if.if_softc = sc;
208		sc->sc_if.if_mtu = SLMTU;
209		sc->sc_if.if_flags =
210		    IFF_POINTOPOINT | SC_AUTOCOMP | IFF_MULTICAST;
211		sc->sc_if.if_type = IFT_SLIP;
212		sc->sc_if.if_ioctl = slioctl;
213		sc->sc_if.if_output = sloutput;
214		sc->sc_if.if_dlt = DLT_SLIP;
215		sc->sc_fastq.ifq_maxlen = 32;
216		IFQ_SET_READY(&sc->sc_if.if_snd);
217		if_attach(&sc->sc_if);
218		if_alloc_sadl(&sc->sc_if);
219#if NBPFILTER > 0
220		bpfattach(&sc->sc_if, DLT_SLIP, SLIP_HDRLEN);
221#endif
222	}
223}
224
225static int
226slinit(sc)
227	struct sl_softc *sc;
228{
229
230	if (sc->sc_mbuf == NULL) {
231		sc->sc_mbuf = m_gethdr(M_WAIT, MT_DATA);
232		m_clget(sc->sc_mbuf, M_WAIT);
233	}
234	sc->sc_ep = (u_char *) sc->sc_mbuf->m_ext.ext_buf +
235	    sc->sc_mbuf->m_ext.ext_size;
236	sc->sc_mp = sc->sc_pktstart = (u_char *) sc->sc_mbuf->m_ext.ext_buf +
237	    BUFOFFSET;
238
239	sl_compress_init(&sc->sc_comp);
240
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	struct tty *tp;
253{
254	struct proc *p = curproc;		/* XXX */
255	struct sl_softc *sc;
256	int nsl;
257	int error;
258	int s;
259
260	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
261		return (error);
262
263	if (tp->t_linesw->l_no == SLIPDISC)
264		return (0);
265
266	for (nsl = NSL, sc = sl_softc; --nsl >= 0; sc++)
267		if (sc->sc_ttyp == NULL) {
268#ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
269			sc->sc_si = softintr_establish(IPL_SOFTNET,
270			    slintr, sc);
271			if (sc->sc_si == NULL)
272				return (ENOMEM);
273#endif
274			if (slinit(sc) == 0) {
275#ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
276				softintr_disestablish(sc->sc_si);
277#endif
278				return (ENOBUFS);
279			}
280			tp->t_sc = (caddr_t)sc;
281			sc->sc_ttyp = tp;
282			sc->sc_if.if_baudrate = tp->t_ospeed;
283			s = spltty();
284			tp->t_state |= TS_ISOPEN | TS_XCLUDE;
285			splx(s);
286			ttyflush(tp, FREAD | FWRITE);
287#ifdef __NetBSD__
288			/*
289			 * make sure tty output queue is large enough
290			 * to hold a full-sized packet (including frame
291			 * end, and a possible extra frame end).  full-sized
292			 * packet occupies a max of 2*SLMAX bytes (because
293			 * of possible escapes), and add two on for frame
294			 * ends.
295			 */
296			s = spltty();
297			if (tp->t_outq.c_cn < 2*SLMAX+2) {
298				sc->sc_oldbufsize = tp->t_outq.c_cn;
299				sc->sc_oldbufquot = tp->t_outq.c_cq != 0;
300
301				clfree(&tp->t_outq);
302				error = clalloc(&tp->t_outq, 2*SLMAX+2, 0);
303				if (error) {
304					splx(s);
305#ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
306					softintr_disestablish(sc->sc_si);
307#endif
308					/*
309					 * clalloc() might return -1 which
310					 * is no good, so we need to return
311					 * something else.
312					 */
313					return (ENOMEM); /* XXX ?! */
314				}
315			} else
316				sc->sc_oldbufsize = sc->sc_oldbufquot = 0;
317			splx(s);
318#endif /* __NetBSD__ */
319			return (0);
320		}
321	return (ENXIO);
322}
323
324/*
325 * Line specific close routine.
326 * Detach the tty from the sl unit.
327 */
328void
329slclose(tp)
330	struct tty *tp;
331{
332	struct sl_softc *sc;
333	int s;
334
335	ttywflush(tp);
336	sc = tp->t_sc;
337
338	if (sc != NULL) {
339#ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
340		softintr_disestablish(sc->sc_si);
341#endif
342		s = splnet();
343		if_down(&sc->sc_if);
344		IF_PURGE(&sc->sc_fastq);
345		splx(s);
346
347		s = spltty();
348		tp->t_linesw = linesw[0];	/* default line disc. */
349		tp->t_state = 0;
350
351		sc->sc_ttyp = NULL;
352		tp->t_sc = NULL;
353
354		m_freem(sc->sc_mbuf);
355		sc->sc_mbuf = NULL;
356		sc->sc_ep = sc->sc_mp = sc->sc_pktstart = NULL;
357		IF_PURGE(&sc->sc_inq);
358
359		/*
360		 * If necessary, install a new outq buffer of the
361		 * appropriate size.
362		 */
363		if (sc->sc_oldbufsize != 0) {
364			clfree(&tp->t_outq);
365			clalloc(&tp->t_outq, sc->sc_oldbufsize,
366			    sc->sc_oldbufquot);
367		}
368		splx(s);
369	}
370}
371
372/*
373 * Line specific (tty) ioctl routine.
374 * Provide a way to get the sl unit number.
375 */
376/* ARGSUSED */
377int
378sltioctl(tp, cmd, data, flag)
379	struct tty *tp;
380	u_long cmd;
381	caddr_t data;
382	int flag;
383{
384	struct sl_softc *sc = (struct sl_softc *)tp->t_sc;
385
386	switch (cmd) {
387	case SLIOCGUNIT:
388		*(int *)data = sc->sc_unit;	/* XXX */
389		break;
390
391	default:
392		return (EPASSTHROUGH);
393	}
394	return (0);
395}
396
397/*
398 * Queue a packet.  Start transmission if not active.
399 * Compression happens in slintr(); if we do it here, IP TOS
400 * will cause us to not compress "background" packets, because
401 * ordering gets trashed.  It can be done for all packets in slintr().
402 */
403int
404sloutput(ifp, m, dst, rtp)
405	struct ifnet *ifp;
406	struct mbuf *m;
407	struct sockaddr *dst;
408	struct rtentry *rtp;
409{
410	struct sl_softc *sc = ifp->if_softc;
411	struct ip *ip;
412	int s, error;
413	ALTQ_DECL(struct altq_pktattr pktattr;)
414
415	IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family, &pktattr);
416
417	/*
418	 * `Cannot happen' (see slioctl).  Someday we will extend
419	 * the line protocol to support other address families.
420	 */
421	if (dst->sa_family != AF_INET) {
422		printf("%s: af%d not supported\n", sc->sc_if.if_xname,
423		    dst->sa_family);
424		m_freem(m);
425		sc->sc_if.if_noproto++;
426		return (EAFNOSUPPORT);
427	}
428
429	if (sc->sc_ttyp == NULL) {
430		m_freem(m);
431		return (ENETDOWN);	/* sort of */
432	}
433	if ((sc->sc_ttyp->t_state & TS_CARR_ON) == 0 &&
434	    (sc->sc_ttyp->t_cflag & CLOCAL) == 0) {
435		m_freem(m);
436		printf("%s: no carrier and not local\n", sc->sc_if.if_xname);
437		return (EHOSTUNREACH);
438	}
439	ip = mtod(m, struct ip *);
440	if (sc->sc_if.if_flags & SC_NOICMP && ip->ip_p == IPPROTO_ICMP) {
441		m_freem(m);
442		return (ENETRESET);		/* XXX ? */
443	}
444
445	s = spltty();
446	if (sc->sc_oqlen && sc->sc_ttyp->t_outq.c_cc == sc->sc_oqlen) {
447		struct timeval tv;
448
449		/* if output's been stalled for too long, and restart */
450		timersub(&time, &sc->sc_lastpacket, &tv);
451		if (tv.tv_sec > 0) {
452			sc->sc_otimeout++;
453			slstart(sc->sc_ttyp);
454		}
455	}
456	splx(s);
457
458	s = splnet();
459	if ((ip->ip_tos & IPTOS_LOWDELAY) != 0
460#ifdef ALTQ
461	    && ALTQ_IS_ENABLED(&ifp->if_snd) == 0
462#endif
463	    ) {
464		if (IF_QFULL(&sc->sc_fastq)) {
465			IF_DROP(&sc->sc_fastq);
466			m_freem(m);
467			error = ENOBUFS;
468		} else {
469			IF_ENQUEUE(&sc->sc_fastq, m);
470			error = 0;
471		}
472	} else
473		IFQ_ENQUEUE(&ifp->if_snd, m, &pktattr, error);
474	if (error) {
475		splx(s);
476		ifp->if_oerrors++;
477		return (error);
478	}
479	sc->sc_lastpacket = time;
480	splx(s);
481
482	s = spltty();
483	if ((sc->sc_oqlen = sc->sc_ttyp->t_outq.c_cc) == 0)
484		slstart(sc->sc_ttyp);
485	splx(s);
486
487	return (0);
488}
489
490/*
491 * Start output on interface.  Get another datagram
492 * to send from the interface queue and map it to
493 * the interface before starting output.
494 */
495void
496slstart(tp)
497	struct tty *tp;
498{
499	struct sl_softc *sc = tp->t_sc;
500
501	/*
502	 * If there is more in the output queue, just send it now.
503	 * We are being called in lieu of ttstart and must do what
504	 * it would.
505	 */
506	if (tp->t_outq.c_cc != 0) {
507		(*tp->t_oproc)(tp);
508		if (tp->t_outq.c_cc > SLIP_HIWAT)
509			return;
510	}
511
512	/*
513	 * This happens briefly when the line shuts down.
514	 */
515	if (sc == NULL)
516		return;
517#ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
518	softintr_schedule(sc->sc_si);
519#else
520    {
521	int s = splhigh();
522	schednetisr(NETISR_SLIP);
523	splx(s);
524    }
525#endif
526}
527
528/*
529 * Copy data buffer to mbuf chain; add ifnet pointer.
530 */
531static struct mbuf *
532sl_btom(sc, len)
533	struct sl_softc *sc;
534	int len;
535{
536	struct mbuf *m;
537
538	/*
539	 * Allocate a new input buffer and swap.
540	 */
541	m = sc->sc_mbuf;
542	MGETHDR(sc->sc_mbuf, M_DONTWAIT, MT_DATA);
543	if (sc->sc_mbuf == NULL) {
544		sc->sc_mbuf = m;
545		return (NULL);
546	}
547	MCLGET(sc->sc_mbuf, M_DONTWAIT);
548	if ((sc->sc_mbuf->m_flags & M_EXT) == 0) {
549		m_freem(sc->sc_mbuf);
550		sc->sc_mbuf = m;
551		return (NULL);
552	}
553	sc->sc_ep = (u_char *) sc->sc_mbuf->m_ext.ext_buf +
554	    sc->sc_mbuf->m_ext.ext_size;
555
556	m->m_data = sc->sc_pktstart;
557
558	m->m_pkthdr.len = m->m_len = len;
559	m->m_pkthdr.rcvif = &sc->sc_if;
560	return (m);
561}
562
563/*
564 * tty interface receiver interrupt.
565 */
566void
567slinput(c, tp)
568	int c;
569	struct tty *tp;
570{
571	struct sl_softc *sc;
572	struct mbuf *m;
573	int len;
574
575	tk_nin++;
576	sc = (struct sl_softc *)tp->t_sc;
577	if (sc == NULL)
578		return;
579	if ((c & TTY_ERRORMASK) || ((tp->t_state & TS_CARR_ON) == 0 &&
580	    (tp->t_cflag & CLOCAL) == 0)) {
581		sc->sc_flags |= SC_ERROR;
582		return;
583	}
584	c &= TTY_CHARMASK;
585
586	++sc->sc_if.if_ibytes;
587
588	if (sc->sc_if.if_flags & IFF_DEBUG) {
589		if (c == ABT_ESC) {
590			/*
591			 * If we have a previous abort, see whether
592			 * this one is within the time limit.
593			 */
594			if (sc->sc_abortcount &&
595			    time.tv_sec >= sc->sc_starttime + ABT_WINDOW)
596				sc->sc_abortcount = 0;
597			/*
598			 * If we see an abort after "idle" time, count it;
599			 * record when the first abort escape arrived.
600			 */
601			if (time.tv_sec >= sc->sc_lasttime + ABT_IDLE) {
602				if (++sc->sc_abortcount == 1)
603					sc->sc_starttime = time.tv_sec;
604				if (sc->sc_abortcount >= ABT_COUNT) {
605					slclose(tp);
606					return;
607				}
608			}
609		} else
610			sc->sc_abortcount = 0;
611		sc->sc_lasttime = time.tv_sec;
612	}
613
614	switch (c) {
615
616	case TRANS_FRAME_ESCAPE:
617		if (sc->sc_escape)
618			c = FRAME_ESCAPE;
619		break;
620
621	case TRANS_FRAME_END:
622		if (sc->sc_escape)
623			c = FRAME_END;
624		break;
625
626	case FRAME_ESCAPE:
627		sc->sc_escape = 1;
628		return;
629
630	case FRAME_END:
631		if(sc->sc_flags & SC_ERROR) {
632			sc->sc_flags &= ~SC_ERROR;
633			goto newpack;
634		}
635		len = sc->sc_mp - sc->sc_pktstart;
636		if (len < 3)
637			/* less than min length packet - ignore */
638			goto newpack;
639
640		m = sl_btom(sc, len);
641		if (m == NULL)
642			goto error;
643
644		IF_ENQUEUE(&sc->sc_inq, m);
645#ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
646		softintr_schedule(sc->sc_si);
647#else
648	    {
649		int s = splhigh();
650		schednetisr(NETISR_SLIP);
651		splx(s);
652	    }
653#endif
654		goto newpack;
655	}
656	if (sc->sc_mp < sc->sc_ep) {
657		*sc->sc_mp++ = c;
658		sc->sc_escape = 0;
659		return;
660	}
661
662	/* can't put lower; would miss an extra frame */
663	sc->sc_flags |= SC_ERROR;
664
665error:
666	sc->sc_if.if_ierrors++;
667newpack:
668	sc->sc_mp = sc->sc_pktstart = (u_char *) sc->sc_mbuf->m_ext.ext_buf +
669	    BUFOFFSET;
670	sc->sc_escape = 0;
671}
672
673#ifndef __HAVE_GENERIC_SOFT_INTERRUPTS
674void
675slnetisr(void)
676{
677	struct sl_softc *sc;
678	int i;
679
680	for (i = 0; i < NSL; i++) {
681		sc = &sl_softc[i];
682		if (sc->sc_ttyp == NULL)
683			continue;
684		slintr(sc);
685	}
686}
687#endif
688
689void
690slintr(void *arg)
691{
692	struct sl_softc *sc = arg;
693	struct tty *tp = sc->sc_ttyp;
694	struct mbuf *m;
695	int s, len;
696	u_char *pktstart, c;
697#if NBPFILTER > 0
698	u_char chdr[CHDR_LEN];
699#endif
700
701	KASSERT(tp != NULL);
702
703	/*
704	 * Output processing loop.
705	 */
706	for (;;) {
707		struct ip *ip;
708		struct mbuf *m2;
709#if NBPFILTER > 0
710		struct mbuf *bpf_m;
711#endif
712
713		/*
714		 * Do not remove the packet from the queue if it
715		 * doesn't look like it will fit into the current
716		 * serial output queue.  With a packet full of
717		 * escapes, this could be as bad as MTU*2+2.
718		 */
719		s = spltty();
720		if (tp->t_outq.c_cn - tp->t_outq.c_cc <
721		    2*sc->sc_if.if_mtu+2) {
722			splx(s);
723			break;
724		}
725		splx(s);
726
727		/*
728		 * Get a packet and send it to the interface.
729		 */
730		s = splnet();
731		IF_DEQUEUE(&sc->sc_fastq, m);
732		if (m)
733			sc->sc_if.if_omcasts++;	/* XXX */
734		else
735			IFQ_DEQUEUE(&sc->sc_if.if_snd, m);
736		splx(s);
737
738		if (m == NULL)
739			break;
740
741		/*
742		 * We do the header compression here rather than in
743		 * sloutput() because the packets will be out of order
744		 * if we are using TOS queueing, and the connection
745		 * ID compression will get munged when this happens.
746		 */
747#if NBPFILTER > 0
748		if (sc->sc_if.if_bpf) {
749			/*
750			 * We need to save the TCP/IP header before
751			 * it's compressed.  To avoid complicated
752			 * code, we just make a deep copy of the
753			 * entire packet (since this is a serial
754			 * line, packets should be short and/or the
755			 * copy should be negligible cost compared
756			 * to the packet transmission time).
757			 */
758			bpf_m = m_dup(m, 0, M_COPYALL, M_DONTWAIT);
759		} else
760			bpf_m = NULL;
761#endif
762		if ((ip = mtod(m, struct ip *))->ip_p == IPPROTO_TCP) {
763			if (sc->sc_if.if_flags & SC_COMPRESS)
764				*mtod(m, u_char *) |=
765				    sl_compress_tcp(m, ip,
766				    &sc->sc_comp, 1);
767		}
768#if NBPFILTER > 0
769		if (sc->sc_if.if_bpf && bpf_m != NULL)
770			bpf_mtap_sl_out(sc->sc_if.if_bpf, mtod(m, u_char *),
771			    bpf_m);
772#endif
773		sc->sc_lastpacket = time;
774
775		s = spltty();
776
777		/*
778		 * The extra FRAME_END will start up a new packet,
779		 * and thus will flush any accumulated garbage.  We
780		 * do this whenever the line may have been idle for
781		 * some time.
782		 */
783		if (tp->t_outq.c_cc == 0) {
784			sc->sc_if.if_obytes++;
785			(void) putc(FRAME_END, &tp->t_outq);
786		}
787
788		while (m) {
789			u_char *bp, *cp, *ep;
790
791			bp = cp = mtod(m, u_char *);
792			ep = cp + m->m_len;
793			while (cp < ep) {
794				/*
795				 * Find out how many bytes in the
796				 * string we can handle without
797				 * doing something special.
798				 */
799				while (cp < ep) {
800					switch (*cp++) {
801					case FRAME_ESCAPE:
802					case FRAME_END:
803						cp--;
804						goto out;
805					}
806				}
807				out:
808				if (cp > bp) {
809					/*
810					 * Put N characters at once
811					 * into the tty output queue.
812					 */
813					if (b_to_q(bp, cp - bp,
814					    &tp->t_outq))
815						break;
816					sc->sc_if.if_obytes += cp - bp;
817				}
818				/*
819				 * If there are characters left in
820				 * the mbuf, the first one must be
821				 * special..  Put it out in a different
822				 * form.
823				 */
824				if (cp < ep) {
825					if (putc(FRAME_ESCAPE,
826					    &tp->t_outq))
827						break;
828					if (putc(*cp++ == FRAME_ESCAPE ?
829					    TRANS_FRAME_ESCAPE :
830					    TRANS_FRAME_END,
831					    &tp->t_outq)) {
832						(void)
833						   unputc(&tp->t_outq);
834						break;
835					}
836					sc->sc_if.if_obytes += 2;
837				}
838				bp = cp;
839			}
840			MFREE(m, m2);
841			m = m2;
842		}
843
844		if (putc(FRAME_END, &tp->t_outq)) {
845			/*
846			 * Not enough room.  Remove a char to make
847			 * room and end the packet normally.  If
848			 * you get many collisions (more than one
849			 * or two a day), you probably do not have
850			 * enough clists and you should increase
851			 * "nclist" in param.c
852			 */
853			(void) unputc(&tp->t_outq);
854			(void) putc(FRAME_END, &tp->t_outq);
855			sc->sc_if.if_collisions++;
856		} else {
857			sc->sc_if.if_obytes++;
858			sc->sc_if.if_opackets++;
859		}
860
861		/*
862		 * We now have characters in the output queue,
863		 * kick the serial port.
864		 */
865		(*tp->t_oproc)(tp);
866		splx(s);
867	}
868
869	/*
870	 * Input processing loop.
871	 */
872	for (;;) {
873		s = spltty();
874		IF_DEQUEUE(&sc->sc_inq, m);
875		splx(s);
876		if (m == NULL)
877			break;
878		pktstart = mtod(m, u_char *);
879		len = m->m_pkthdr.len;
880#if NBPFILTER > 0
881		if (sc->sc_if.if_bpf) {
882			/*
883			 * Save the compressed header, so we
884			 * can tack it on later.  Note that we
885			 * will end up copying garbage in some
886			 * cases but this is okay.  We remember
887			 * where the buffer started so we can
888			 * compute the new header length.
889			 */
890			memcpy(chdr, pktstart, CHDR_LEN);
891		}
892#endif /* NBPFILTER > 0 */
893		if ((c = (*pktstart & 0xf0)) != (IPVERSION << 4)) {
894			if (c & 0x80)
895				c = TYPE_COMPRESSED_TCP;
896			else if (c == TYPE_UNCOMPRESSED_TCP)
897				*pktstart &= 0x4f; /* XXX */
898			/*
899			 * We've got something that's not an IP
900			 * packet.  If compression is enabled,
901			 * try to decompress it.  Otherwise, if
902			 * `auto-enable' compression is on and
903			 * it's a reasonable packet, decompress
904			 * it and then enable compression.
905			 * Otherwise, drop it.
906			 */
907			if (sc->sc_if.if_flags & SC_COMPRESS) {
908				len = sl_uncompress_tcp(&pktstart, len,
909				    (u_int)c, &sc->sc_comp);
910				if (len <= 0) {
911					m_freem(m);
912					continue;
913				}
914			} else if ((sc->sc_if.if_flags & SC_AUTOCOMP) &&
915			    c == TYPE_UNCOMPRESSED_TCP && len >= 40) {
916				len = sl_uncompress_tcp(&pktstart, len,
917				    (u_int)c, &sc->sc_comp);
918				if (len <= 0) {
919					m_freem(m);
920					continue;
921				}
922				sc->sc_if.if_flags |= SC_COMPRESS;
923			} else {
924				m_freem(m);
925				continue;
926			}
927		}
928		m->m_data = (caddr_t) pktstart;
929		m->m_pkthdr.len = m->m_len = len;
930#if NBPFILTER > 0
931		if (sc->sc_if.if_bpf) {
932			bpf_mtap_sl_in(sc->sc_if.if_bpf, chdr, &m);
933			if (m == NULL)
934				continue;
935		}
936#endif /* NBPFILTER > 0 */
937		/*
938		 * If the packet will fit into a single
939		 * header mbuf, copy it into one, to save
940		 * memory.
941		 */
942		if (m->m_pkthdr.len < MHLEN) {
943			struct mbuf *n;
944
945			MGETHDR(n, M_DONTWAIT, MT_DATA);
946			M_COPY_PKTHDR(n, m);
947			memcpy(mtod(n, caddr_t), mtod(m, caddr_t),
948			    m->m_pkthdr.len);
949			n->m_len = m->m_len;
950			m_freem(m);
951			m = n;
952		}
953
954		sc->sc_if.if_ipackets++;
955		sc->sc_lastpacket = time;
956
957		s = splnet();
958		if (IF_QFULL(&ipintrq)) {
959			IF_DROP(&ipintrq);
960			sc->sc_if.if_ierrors++;
961			sc->sc_if.if_iqdrops++;
962			m_freem(m);
963		} else {
964			IF_ENQUEUE(&ipintrq, m);
965			schednetisr(NETISR_IP);
966		}
967		splx(s);
968	}
969}
970
971/*
972 * Process an ioctl request.
973 */
974int
975slioctl(ifp, cmd, data)
976	struct ifnet *ifp;
977	u_long cmd;
978	caddr_t data;
979{
980	struct ifaddr *ifa = (struct ifaddr *)data;
981	struct ifreq *ifr = (struct ifreq *)data;
982	int s = splnet(), error = 0;
983	struct sl_softc *sc = ifp->if_softc;
984
985	switch (cmd) {
986
987	case SIOCSIFADDR:
988		if (ifa->ifa_addr->sa_family == AF_INET)
989			ifp->if_flags |= IFF_UP;
990		else
991			error = EAFNOSUPPORT;
992		break;
993
994	case SIOCSIFDSTADDR:
995		if (ifa->ifa_addr->sa_family != AF_INET)
996			error = EAFNOSUPPORT;
997		break;
998
999	case SIOCSIFMTU:
1000		if ((ifr->ifr_mtu < 3) || (ifr->ifr_mtu > SLMAX)) {
1001		    error = EINVAL;
1002		    break;
1003		}
1004		sc->sc_if.if_mtu = ifr->ifr_mtu;
1005		break;
1006
1007	case SIOCGIFMTU:
1008		ifr->ifr_mtu = sc->sc_if.if_mtu;
1009		break;
1010
1011	case SIOCADDMULTI:
1012	case SIOCDELMULTI:
1013		if (ifr == 0) {
1014			error = EAFNOSUPPORT;		/* XXX */
1015			break;
1016		}
1017		switch (ifr->ifr_addr.sa_family) {
1018
1019#ifdef INET
1020		case AF_INET:
1021			break;
1022#endif
1023
1024		default:
1025			error = EAFNOSUPPORT;
1026			break;
1027		}
1028		break;
1029
1030	default:
1031		error = EINVAL;
1032	}
1033	splx(s);
1034	return (error);
1035}
1036#endif
1037