if_sl.c revision 1.96
1/*	$NetBSD: if_sl.c,v 1.96 2006/03/02 17:20:07 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.96 2006/03/02 17:20:07 christos Exp $");
64
65#include "opt_inet.h"
66#include "bpfilter.h"
67
68#include <sys/param.h>
69#include <sys/proc.h>
70#include <sys/malloc.h>
71#include <sys/mbuf.h>
72#include <sys/buf.h>
73#include <sys/dkstat.h>
74#include <sys/socket.h>
75#include <sys/ioctl.h>
76#include <sys/file.h>
77#include <sys/conf.h>
78#include <sys/tty.h>
79#include <sys/kernel.h>
80#if __NetBSD__
81#include <sys/systm.h>
82#endif
83
84#include <machine/cpu.h>
85#include <machine/intr.h>
86
87#include <net/if.h>
88#include <net/if_types.h>
89#include <net/netisr.h>
90#include <net/route.h>
91
92#ifdef INET
93#include <netinet/in.h>
94#include <netinet/in_systm.h>
95#include <netinet/in_var.h>
96#include <netinet/ip.h>
97#endif
98
99#include <net/slcompress.h>
100#include <net/if_slvar.h>
101#include <net/slip.h>
102#include <net/ppp_defs.h>
103#include <net/if_ppp.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#define	BUFOFFSET	(128+sizeof(struct ifnet **)+SLIP_HDRLEN)
151#define	SLMAX		(MCLBYTES - BUFOFFSET)
152#define	SLBUFSIZE	(SLMAX + BUFOFFSET)
153#ifndef SLMTU
154#define	SLMTU		296
155#endif
156#if (SLMTU < 3)
157#error SLMTU way too small.
158#endif
159#define	SLIP_HIWAT	roundup(50,CBSIZE)
160#ifndef __NetBSD__					/* XXX - cgd */
161#define	CLISTRESERVE	1024	/* Can't let clists get too low */
162#endif	/* !__NetBSD__ */
163
164/*
165 * SLIP ABORT ESCAPE MECHANISM:
166 *	(inspired by HAYES modem escape arrangement)
167 *	1sec escape 1sec escape 1sec escape { 1sec escape 1sec escape }
168 *	within window time signals a "soft" exit from slip mode by remote end
169 *	if the IFF_DEBUG flag is on.
170 */
171#define	ABT_ESC		'\033'	/* can't be t_intr - distant host must know it*/
172#define	ABT_IDLE	1	/* in seconds - idle before an escape */
173#define	ABT_COUNT	3	/* count of escapes for abort */
174#define	ABT_WINDOW	(ABT_COUNT*2+2)	/* in seconds - time to count */
175
176static int		sl_clone_create(struct if_clone *, int);
177static int		sl_clone_destroy(struct ifnet *);
178
179static LIST_HEAD(, sl_softc) sl_softc_list;
180
181struct if_clone sl_cloner =
182    IF_CLONE_INITIALIZER("sl", sl_clone_create, sl_clone_destroy);
183
184#define FRAME_END	 	0xc0		/* Frame End */
185#define FRAME_ESCAPE		0xdb		/* Frame Esc */
186#define TRANS_FRAME_END	 	0xdc		/* transposed frame end */
187#define TRANS_FRAME_ESCAPE 	0xdd		/* transposed frame esc */
188
189#ifndef __HAVE_GENERIC_SOFT_INTERRUPTS
190void	slnetisr(void);
191#endif
192static void	slintr(void *);
193
194static int	slinit(struct sl_softc *);
195static struct mbuf *sl_btom(struct sl_softc *, int);
196
197static int	slclose(struct tty *, int);
198static int	slinput(int, struct tty *);
199static int	slioctl(struct ifnet *, u_long, caddr_t);
200static int	slopen(dev_t, struct tty *);
201static int	sloutput(struct ifnet *, struct mbuf *, struct sockaddr *,
202			 struct rtentry *);
203static int	slstart(struct tty *);
204static int	sltioctl(struct tty *, u_long, caddr_t, int, struct lwp *);
205
206static struct linesw slip_disc = {
207	.l_name = "slip",
208	.l_open = slopen,
209	.l_close = slclose,
210	.l_read = ttyerrio,
211	.l_write = ttyerrio,
212	.l_ioctl = sltioctl,
213	.l_rint = slinput,
214	.l_start = slstart,
215	.l_modem = nullmodem,
216	.l_poll = ttyerrpoll
217};
218
219void	slattach(void);
220
221void
222slattach(void)
223{
224	if (ttyldisc_attach(&slip_disc) != 0)
225		panic("slattach");
226	LIST_INIT(&sl_softc_list);
227	if_clone_attach(&sl_cloner);
228}
229
230static int
231sl_clone_create(struct if_clone *ifc, int unit)
232{
233	struct sl_softc *sc;
234
235	MALLOC(sc, struct sl_softc *, sizeof(*sc), M_DEVBUF, M_WAIT|M_ZERO);
236	sc->sc_unit = unit;
237	(void)snprintf(sc->sc_if.if_xname, sizeof(sc->sc_if.if_xname),
238	    "%s%d", ifc->ifc_name, unit);
239	sc->sc_if.if_softc = sc;
240	sc->sc_if.if_mtu = SLMTU;
241	sc->sc_if.if_flags = IFF_POINTOPOINT | SC_AUTOCOMP | IFF_MULTICAST;
242	sc->sc_if.if_type = IFT_SLIP;
243	sc->sc_if.if_ioctl = slioctl;
244	sc->sc_if.if_output = sloutput;
245	sc->sc_if.if_dlt = DLT_SLIP;
246	sc->sc_fastq.ifq_maxlen = 32;
247	IFQ_SET_READY(&sc->sc_if.if_snd);
248	if_attach(&sc->sc_if);
249	if_alloc_sadl(&sc->sc_if);
250#if NBPFILTER > 0
251	bpfattach(&sc->sc_if, DLT_SLIP, SLIP_HDRLEN);
252#endif
253	LIST_INSERT_HEAD(&sl_softc_list, sc, sc_iflist);
254	return 0;
255}
256
257static int
258sl_clone_destroy(struct ifnet *ifp)
259{
260	struct sl_softc *sc = (struct sl_softc *)ifp->if_softc;
261
262	if (sc->sc_ttyp != NULL)
263		return EBUSY;	/* Not removing it */
264
265	LIST_REMOVE(sc, sc_iflist);
266
267#if NBPFILTER > 0
268	bpfdetach(ifp);
269#endif
270	if_detach(ifp);
271
272	FREE(sc, M_DEVBUF);
273	return 0;
274}
275
276static int
277slinit(struct sl_softc *sc)
278{
279
280	if (sc->sc_mbuf == NULL) {
281		sc->sc_mbuf = m_gethdr(M_WAIT, MT_DATA);
282		m_clget(sc->sc_mbuf, M_WAIT);
283	}
284	sc->sc_ep = (u_char *) sc->sc_mbuf->m_ext.ext_buf +
285	    sc->sc_mbuf->m_ext.ext_size;
286	sc->sc_mp = sc->sc_pktstart = (u_char *) sc->sc_mbuf->m_ext.ext_buf +
287	    BUFOFFSET;
288
289#ifdef INET
290	sl_compress_init(&sc->sc_comp);
291#endif
292
293	return (1);
294}
295
296/*
297 * Line specific open routine.
298 * Attach the given tty to the first available sl unit.
299 */
300/* ARGSUSED */
301static int
302slopen(dev_t dev, struct tty *tp)
303{
304	struct proc *p = curproc;		/* XXX */
305	struct sl_softc *sc;
306	int error;
307	int s;
308
309	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
310		return (error);
311
312	if (tp->t_linesw == &slip_disc)
313		return (0);
314
315	LIST_FOREACH(sc, &sl_softc_list, sc_iflist)
316		if (sc->sc_ttyp == NULL) {
317#ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
318			sc->sc_si = softintr_establish(IPL_SOFTNET,
319			    slintr, sc);
320			if (sc->sc_si == NULL)
321				return (ENOMEM);
322#endif
323			if (slinit(sc) == 0) {
324#ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
325				softintr_disestablish(sc->sc_si);
326#endif
327				return (ENOBUFS);
328			}
329			tp->t_sc = (caddr_t)sc;
330			sc->sc_ttyp = tp;
331			sc->sc_if.if_baudrate = tp->t_ospeed;
332			s = spltty();
333			tp->t_state |= TS_ISOPEN | TS_XCLUDE;
334			splx(s);
335			ttyflush(tp, FREAD | FWRITE);
336#ifdef __NetBSD__
337			/*
338			 * make sure tty output queue is large enough
339			 * to hold a full-sized packet (including frame
340			 * end, and a possible extra frame end).  full-sized
341			 * packet occupies a max of 2*SLMAX bytes (because
342			 * of possible escapes), and add two on for frame
343			 * ends.
344			 */
345			s = spltty();
346			if (tp->t_outq.c_cn < 2*SLMAX+2) {
347				sc->sc_oldbufsize = tp->t_outq.c_cn;
348				sc->sc_oldbufquot = tp->t_outq.c_cq != 0;
349
350				clfree(&tp->t_outq);
351				error = clalloc(&tp->t_outq, 2*SLMAX+2, 0);
352				if (error) {
353					splx(s);
354#ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
355					softintr_disestablish(sc->sc_si);
356#endif
357					/*
358					 * clalloc() might return -1 which
359					 * is no good, so we need to return
360					 * something else.
361					 */
362					return (ENOMEM); /* XXX ?! */
363				}
364			} else
365				sc->sc_oldbufsize = sc->sc_oldbufquot = 0;
366			splx(s);
367#endif /* __NetBSD__ */
368			return (0);
369		}
370	return (ENXIO);
371}
372
373/*
374 * Line specific close routine.
375 * Detach the tty from the sl unit.
376 */
377static int
378slclose(struct tty *tp, int flag)
379{
380	struct sl_softc *sc;
381	int s;
382
383	ttywflush(tp);
384	sc = tp->t_sc;
385
386	if (sc != NULL) {
387#ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
388		softintr_disestablish(sc->sc_si);
389#endif
390		s = splnet();
391		if_down(&sc->sc_if);
392		IF_PURGE(&sc->sc_fastq);
393		splx(s);
394
395		s = spltty();
396		ttyldisc_release(tp->t_linesw);
397		tp->t_linesw = ttyldisc_default();
398		tp->t_state = 0;
399
400		sc->sc_ttyp = NULL;
401		tp->t_sc = NULL;
402
403		m_freem(sc->sc_mbuf);
404		sc->sc_mbuf = NULL;
405		sc->sc_ep = sc->sc_mp = sc->sc_pktstart = NULL;
406		IF_PURGE(&sc->sc_inq);
407
408		/*
409		 * If necessary, install a new outq buffer of the
410		 * appropriate size.
411		 */
412		if (sc->sc_oldbufsize != 0) {
413			clfree(&tp->t_outq);
414			clalloc(&tp->t_outq, sc->sc_oldbufsize,
415			    sc->sc_oldbufquot);
416		}
417		splx(s);
418	}
419
420	return (0);
421}
422
423/*
424 * Line specific (tty) ioctl routine.
425 * Provide a way to get the sl unit number.
426 */
427/* ARGSUSED */
428static int
429sltioctl(struct tty *tp, u_long cmd, caddr_t data, int flag, struct lwp *l)
430{
431	struct sl_softc *sc = (struct sl_softc *)tp->t_sc;
432
433	switch (cmd) {
434	case SLIOCGUNIT:
435		*(int *)data = sc->sc_unit;	/* XXX */
436		break;
437
438	default:
439		return (EPASSTHROUGH);
440	}
441	return (0);
442}
443
444/*
445 * Queue a packet.  Start transmission if not active.
446 * Compression happens in slintr(); if we do it here, IP TOS
447 * will cause us to not compress "background" packets, because
448 * ordering gets trashed.  It can be done for all packets in slintr().
449 */
450static int
451sloutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
452    struct rtentry *rtp)
453{
454	struct sl_softc *sc = ifp->if_softc;
455	struct ip *ip;
456	struct ifqueue *ifq = NULL;
457	int s, error;
458	ALTQ_DECL(struct altq_pktattr pktattr;)
459
460	IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family, &pktattr);
461
462	/*
463	 * `Cannot happen' (see slioctl).  Someday we will extend
464	 * the line protocol to support other address families.
465	 */
466	if (dst->sa_family != AF_INET) {
467		printf("%s: af%d not supported\n", sc->sc_if.if_xname,
468		    dst->sa_family);
469		m_freem(m);
470		sc->sc_if.if_noproto++;
471		return (EAFNOSUPPORT);
472	}
473
474	if (sc->sc_ttyp == NULL) {
475		m_freem(m);
476		return (ENETDOWN);	/* sort of */
477	}
478	if ((sc->sc_ttyp->t_state & TS_CARR_ON) == 0 &&
479	    (sc->sc_ttyp->t_cflag & CLOCAL) == 0) {
480		m_freem(m);
481		printf("%s: no carrier and not local\n", sc->sc_if.if_xname);
482		return (EHOSTUNREACH);
483	}
484	ip = mtod(m, struct ip *);
485#ifdef INET
486	if (sc->sc_if.if_flags & SC_NOICMP && ip->ip_p == IPPROTO_ICMP) {
487		m_freem(m);
488		return (ENETRESET);		/* XXX ? */
489	}
490#endif
491
492	s = spltty();
493	if (sc->sc_oqlen && sc->sc_ttyp->t_outq.c_cc == sc->sc_oqlen) {
494		struct timeval tv;
495
496		/* if output's been stalled for too long, and restart */
497		timersub(&time, &sc->sc_lastpacket, &tv);
498		if (tv.tv_sec > 0) {
499			sc->sc_otimeout++;
500			slstart(sc->sc_ttyp);
501		}
502	}
503	splx(s);
504
505	s = splnet();
506#ifdef INET
507	if ((ip->ip_tos & IPTOS_LOWDELAY) != 0)
508		ifq = &sc->sc_fastq;
509#endif
510	if ((error = ifq_enqueue2(ifp, ifq, m ALTQ_COMMA
511	    ALTQ_DECL(&pktattr))) != 0) {
512		splx(s);
513		return error;
514	}
515	sc->sc_lastpacket = time;
516	splx(s);
517
518	s = spltty();
519	if ((sc->sc_oqlen = sc->sc_ttyp->t_outq.c_cc) == 0)
520		slstart(sc->sc_ttyp);
521	splx(s);
522
523	return (0);
524}
525
526/*
527 * Start output on interface.  Get another datagram
528 * to send from the interface queue and map it to
529 * the interface before starting output.
530 */
531static int
532slstart(struct tty *tp)
533{
534	struct sl_softc *sc = tp->t_sc;
535
536	/*
537	 * If there is more in the output queue, just send it now.
538	 * We are being called in lieu of ttstart and must do what
539	 * it would.
540	 */
541	if (tp->t_outq.c_cc != 0) {
542		(*tp->t_oproc)(tp);
543		if (tp->t_outq.c_cc > SLIP_HIWAT)
544			return (0);
545	}
546
547	/*
548	 * This happens briefly when the line shuts down.
549	 */
550	if (sc == NULL)
551		return (0);
552#ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
553	softintr_schedule(sc->sc_si);
554#else
555    {
556	int s = splhigh();
557	schednetisr(NETISR_SLIP);
558	splx(s);
559    }
560#endif
561	return (0);
562}
563
564/*
565 * Copy data buffer to mbuf chain; add ifnet pointer.
566 */
567static struct mbuf *
568sl_btom(struct sl_softc *sc, int len)
569{
570	struct mbuf *m;
571
572	/*
573	 * Allocate a new input buffer and swap.
574	 */
575	m = sc->sc_mbuf;
576	MGETHDR(sc->sc_mbuf, M_DONTWAIT, MT_DATA);
577	if (sc->sc_mbuf == NULL) {
578		sc->sc_mbuf = m;
579		return (NULL);
580	}
581	MCLGET(sc->sc_mbuf, M_DONTWAIT);
582	if ((sc->sc_mbuf->m_flags & M_EXT) == 0) {
583		m_freem(sc->sc_mbuf);
584		sc->sc_mbuf = m;
585		return (NULL);
586	}
587	sc->sc_ep = (u_char *) sc->sc_mbuf->m_ext.ext_buf +
588	    sc->sc_mbuf->m_ext.ext_size;
589
590	m->m_data = sc->sc_pktstart;
591
592	m->m_pkthdr.len = m->m_len = len;
593	m->m_pkthdr.rcvif = &sc->sc_if;
594	return (m);
595}
596
597/*
598 * tty interface receiver interrupt.
599 */
600static int
601slinput(int c, struct tty *tp)
602{
603	struct sl_softc *sc;
604	struct mbuf *m;
605	int len;
606
607	tk_nin++;
608	sc = (struct sl_softc *)tp->t_sc;
609	if (sc == NULL)
610		return (0);
611	if ((c & TTY_ERRORMASK) || ((tp->t_state & TS_CARR_ON) == 0 &&
612	    (tp->t_cflag & CLOCAL) == 0)) {
613		sc->sc_flags |= SC_ERROR;
614		return (0);
615	}
616	c &= TTY_CHARMASK;
617
618	++sc->sc_if.if_ibytes;
619
620	if (sc->sc_if.if_flags & IFF_DEBUG) {
621		if (c == ABT_ESC) {
622			/*
623			 * If we have a previous abort, see whether
624			 * this one is within the time limit.
625			 */
626			if (sc->sc_abortcount &&
627			    time.tv_sec >= sc->sc_starttime + ABT_WINDOW)
628				sc->sc_abortcount = 0;
629			/*
630			 * If we see an abort after "idle" time, count it;
631			 * record when the first abort escape arrived.
632			 */
633			if (time.tv_sec >= sc->sc_lasttime + ABT_IDLE) {
634				if (++sc->sc_abortcount == 1)
635					sc->sc_starttime = time.tv_sec;
636				if (sc->sc_abortcount >= ABT_COUNT) {
637					slclose(tp, 0);
638					return (0);
639				}
640			}
641		} else
642			sc->sc_abortcount = 0;
643		sc->sc_lasttime = time.tv_sec;
644	}
645
646	switch (c) {
647
648	case TRANS_FRAME_ESCAPE:
649		if (sc->sc_escape)
650			c = FRAME_ESCAPE;
651		break;
652
653	case TRANS_FRAME_END:
654		if (sc->sc_escape)
655			c = FRAME_END;
656		break;
657
658	case FRAME_ESCAPE:
659		sc->sc_escape = 1;
660		return (0);
661
662	case FRAME_END:
663		if(sc->sc_flags & SC_ERROR) {
664			sc->sc_flags &= ~SC_ERROR;
665			goto newpack;
666		}
667		len = sc->sc_mp - sc->sc_pktstart;
668		if (len < 3)
669			/* less than min length packet - ignore */
670			goto newpack;
671
672		m = sl_btom(sc, len);
673		if (m == NULL)
674			goto error;
675
676		IF_ENQUEUE(&sc->sc_inq, m);
677#ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
678		softintr_schedule(sc->sc_si);
679#else
680	    {
681		int s = splhigh();
682		schednetisr(NETISR_SLIP);
683		splx(s);
684	    }
685#endif
686		goto newpack;
687	}
688	if (sc->sc_mp < sc->sc_ep) {
689		*sc->sc_mp++ = c;
690		sc->sc_escape = 0;
691		return (0);
692	}
693
694	/* can't put lower; would miss an extra frame */
695	sc->sc_flags |= SC_ERROR;
696
697error:
698	sc->sc_if.if_ierrors++;
699newpack:
700	sc->sc_mp = sc->sc_pktstart = (u_char *) sc->sc_mbuf->m_ext.ext_buf +
701	    BUFOFFSET;
702	sc->sc_escape = 0;
703
704	return (0);
705}
706
707#ifndef __HAVE_GENERIC_SOFT_INTERRUPTS
708void
709slnetisr(void)
710{
711	struct sl_softc *sc;
712
713	LIST_FOREACH(sc, &sl_softc_list, sc_iflist) {
714		if (sc->sc_ttyp == NULL)
715			continue;
716		slintr(sc);
717	}
718}
719#endif
720
721static void
722slintr(void *arg)
723{
724	struct sl_softc *sc = arg;
725	struct tty *tp = sc->sc_ttyp;
726	struct mbuf *m;
727	int s, len;
728	u_char *pktstart;
729#ifdef INET
730	u_char c;
731#endif
732#if NBPFILTER > 0
733	u_char chdr[CHDR_LEN];
734#endif
735
736	KASSERT(tp != NULL);
737
738	/*
739	 * Output processing loop.
740	 */
741	for (;;) {
742#ifdef INET
743		struct ip *ip;
744#endif
745		struct mbuf *m2;
746#if NBPFILTER > 0
747		struct mbuf *bpf_m;
748#endif
749
750		/*
751		 * Do not remove the packet from the queue if it
752		 * doesn't look like it will fit into the current
753		 * serial output queue.  With a packet full of
754		 * escapes, this could be as bad as MTU*2+2.
755		 */
756		s = spltty();
757		if (tp->t_outq.c_cn - tp->t_outq.c_cc <
758		    2*sc->sc_if.if_mtu+2) {
759			splx(s);
760			break;
761		}
762		splx(s);
763
764		/*
765		 * Get a packet and send it to the interface.
766		 */
767		s = splnet();
768		IF_DEQUEUE(&sc->sc_fastq, m);
769		if (m)
770			sc->sc_if.if_omcasts++;	/* XXX */
771		else
772			IFQ_DEQUEUE(&sc->sc_if.if_snd, m);
773		splx(s);
774
775		if (m == NULL)
776			break;
777
778		/*
779		 * We do the header compression here rather than in
780		 * sloutput() because the packets will be out of order
781		 * if we are using TOS queueing, and the connection
782		 * ID compression will get munged when this happens.
783		 */
784#if NBPFILTER > 0
785		if (sc->sc_if.if_bpf) {
786			/*
787			 * We need to save the TCP/IP header before
788			 * it's compressed.  To avoid complicated
789			 * code, we just make a deep copy of the
790			 * entire packet (since this is a serial
791			 * line, packets should be short and/or the
792			 * copy should be negligible cost compared
793			 * to the packet transmission time).
794			 */
795			bpf_m = m_dup(m, 0, M_COPYALL, M_DONTWAIT);
796		} else
797			bpf_m = NULL;
798#endif
799#ifdef INET
800		if ((ip = mtod(m, struct ip *))->ip_p == IPPROTO_TCP) {
801			if (sc->sc_if.if_flags & SC_COMPRESS)
802				*mtod(m, u_char *) |=
803				    sl_compress_tcp(m, ip,
804				    &sc->sc_comp, 1);
805		}
806#endif
807#if NBPFILTER > 0
808		if (sc->sc_if.if_bpf && bpf_m != NULL)
809			bpf_mtap_sl_out(sc->sc_if.if_bpf, mtod(m, u_char *),
810			    bpf_m);
811#endif
812		sc->sc_lastpacket = time;
813
814		s = spltty();
815
816		/*
817		 * The extra FRAME_END will start up a new packet,
818		 * and thus will flush any accumulated garbage.  We
819		 * do this whenever the line may have been idle for
820		 * some time.
821		 */
822		if (tp->t_outq.c_cc == 0) {
823			sc->sc_if.if_obytes++;
824			(void) putc(FRAME_END, &tp->t_outq);
825		}
826
827		while (m) {
828			u_char *bp, *cp, *ep;
829
830			bp = cp = mtod(m, u_char *);
831			ep = cp + m->m_len;
832			while (cp < ep) {
833				/*
834				 * Find out how many bytes in the
835				 * string we can handle without
836				 * doing something special.
837				 */
838				while (cp < ep) {
839					switch (*cp++) {
840					case FRAME_ESCAPE:
841					case FRAME_END:
842						cp--;
843						goto out;
844					}
845				}
846				out:
847				if (cp > bp) {
848					/*
849					 * Put N characters at once
850					 * into the tty output queue.
851					 */
852					if (b_to_q(bp, cp - bp,
853					    &tp->t_outq))
854						break;
855					sc->sc_if.if_obytes += cp - bp;
856				}
857				/*
858				 * If there are characters left in
859				 * the mbuf, the first one must be
860				 * special..  Put it out in a different
861				 * form.
862				 */
863				if (cp < ep) {
864					if (putc(FRAME_ESCAPE,
865					    &tp->t_outq))
866						break;
867					if (putc(*cp++ == FRAME_ESCAPE ?
868					    TRANS_FRAME_ESCAPE :
869					    TRANS_FRAME_END,
870					    &tp->t_outq)) {
871						(void)
872						   unputc(&tp->t_outq);
873						break;
874					}
875					sc->sc_if.if_obytes += 2;
876				}
877				bp = cp;
878			}
879			MFREE(m, m2);
880			m = m2;
881		}
882
883		if (putc(FRAME_END, &tp->t_outq)) {
884			/*
885			 * Not enough room.  Remove a char to make
886			 * room and end the packet normally.  If
887			 * you get many collisions (more than one
888			 * or two a day), you probably do not have
889			 * enough clists and you should increase
890			 * "nclist" in param.c
891			 */
892			(void) unputc(&tp->t_outq);
893			(void) putc(FRAME_END, &tp->t_outq);
894			sc->sc_if.if_collisions++;
895		} else {
896			sc->sc_if.if_obytes++;
897			sc->sc_if.if_opackets++;
898		}
899
900		/*
901		 * We now have characters in the output queue,
902		 * kick the serial port.
903		 */
904		(*tp->t_oproc)(tp);
905		splx(s);
906	}
907
908	/*
909	 * Input processing loop.
910	 */
911	for (;;) {
912		s = spltty();
913		IF_DEQUEUE(&sc->sc_inq, m);
914		splx(s);
915		if (m == NULL)
916			break;
917		pktstart = mtod(m, u_char *);
918		len = m->m_pkthdr.len;
919#if NBPFILTER > 0
920		if (sc->sc_if.if_bpf) {
921			/*
922			 * Save the compressed header, so we
923			 * can tack it on later.  Note that we
924			 * will end up copying garbage in some
925			 * cases but this is okay.  We remember
926			 * where the buffer started so we can
927			 * compute the new header length.
928			 */
929			memcpy(chdr, pktstart, CHDR_LEN);
930		}
931#endif /* NBPFILTER > 0 */
932#ifdef INET
933		if ((c = (*pktstart & 0xf0)) != (IPVERSION << 4)) {
934			if (c & 0x80)
935				c = TYPE_COMPRESSED_TCP;
936			else if (c == TYPE_UNCOMPRESSED_TCP)
937				*pktstart &= 0x4f; /* XXX */
938			/*
939			 * We've got something that's not an IP
940			 * packet.  If compression is enabled,
941			 * try to decompress it.  Otherwise, if
942			 * `auto-enable' compression is on and
943			 * it's a reasonable packet, decompress
944			 * it and then enable compression.
945			 * Otherwise, drop it.
946			 */
947			if (sc->sc_if.if_flags & SC_COMPRESS) {
948				len = sl_uncompress_tcp(&pktstart, len,
949				    (u_int)c, &sc->sc_comp);
950				if (len <= 0) {
951					m_freem(m);
952					continue;
953				}
954			} else if ((sc->sc_if.if_flags & SC_AUTOCOMP) &&
955			    c == TYPE_UNCOMPRESSED_TCP && len >= 40) {
956				len = sl_uncompress_tcp(&pktstart, len,
957				    (u_int)c, &sc->sc_comp);
958				if (len <= 0) {
959					m_freem(m);
960					continue;
961				}
962				sc->sc_if.if_flags |= SC_COMPRESS;
963			} else {
964				m_freem(m);
965				continue;
966			}
967		}
968#endif
969		m->m_data = (caddr_t) pktstart;
970		m->m_pkthdr.len = m->m_len = len;
971#if NBPFILTER > 0
972		if (sc->sc_if.if_bpf) {
973			bpf_mtap_sl_in(sc->sc_if.if_bpf, chdr, &m);
974			if (m == NULL)
975				continue;
976		}
977#endif /* NBPFILTER > 0 */
978		/*
979		 * If the packet will fit into a single
980		 * header mbuf, copy it into one, to save
981		 * memory.
982		 */
983		if (m->m_pkthdr.len < MHLEN) {
984			struct mbuf *n;
985			int pktlen;
986
987			MGETHDR(n, M_DONTWAIT, MT_DATA);
988			pktlen = m->m_pkthdr.len;
989			M_MOVE_PKTHDR(n, m);
990			memcpy(mtod(n, caddr_t), mtod(m, caddr_t), pktlen);
991			n->m_len = m->m_len;
992			m_freem(m);
993			m = n;
994		}
995
996		sc->sc_if.if_ipackets++;
997		sc->sc_lastpacket = time;
998
999#ifdef INET
1000		s = splnet();
1001		if (IF_QFULL(&ipintrq)) {
1002			IF_DROP(&ipintrq);
1003			sc->sc_if.if_ierrors++;
1004			sc->sc_if.if_iqdrops++;
1005			m_freem(m);
1006		} else {
1007			IF_ENQUEUE(&ipintrq, m);
1008			schednetisr(NETISR_IP);
1009		}
1010		splx(s);
1011#endif
1012	}
1013}
1014
1015/*
1016 * Process an ioctl request.
1017 */
1018static int
1019slioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1020{
1021	struct ifaddr *ifa = (struct ifaddr *)data;
1022	struct ifreq *ifr = (struct ifreq *)data;
1023	int s = splnet(), error = 0;
1024	struct sl_softc *sc = ifp->if_softc;
1025	struct ppp_stats *psp;
1026	struct ppp_comp_stats *pcp;
1027
1028	switch (cmd) {
1029
1030	case SIOCSIFADDR:
1031		if (ifa->ifa_addr->sa_family == AF_INET)
1032			ifp->if_flags |= IFF_UP;
1033		else
1034			error = EAFNOSUPPORT;
1035		break;
1036
1037	case SIOCSIFDSTADDR:
1038		if (ifa->ifa_addr->sa_family != AF_INET)
1039			error = EAFNOSUPPORT;
1040		break;
1041
1042	case SIOCSIFMTU:
1043		if ((ifr->ifr_mtu < 3) || (ifr->ifr_mtu > SLMAX)) {
1044		    error = EINVAL;
1045		    break;
1046		}
1047		sc->sc_if.if_mtu = ifr->ifr_mtu;
1048		break;
1049
1050	case SIOCGIFMTU:
1051		ifr->ifr_mtu = sc->sc_if.if_mtu;
1052		break;
1053
1054	case SIOCADDMULTI:
1055	case SIOCDELMULTI:
1056		if (ifr == 0) {
1057			error = EAFNOSUPPORT;		/* XXX */
1058			break;
1059		}
1060		switch (ifr->ifr_addr.sa_family) {
1061
1062#ifdef INET
1063		case AF_INET:
1064			break;
1065#endif
1066
1067		default:
1068			error = EAFNOSUPPORT;
1069			break;
1070		}
1071		break;
1072
1073	case SIOCGPPPSTATS:
1074		psp = &((struct ifpppstatsreq *) data)->stats;
1075		(void)memset(psp, 0, sizeof(*psp));
1076		psp->p.ppp_ibytes = sc->sc_if.if_ibytes;
1077		psp->p.ppp_ipackets = sc->sc_if.if_ipackets;
1078		psp->p.ppp_ierrors = sc->sc_if.if_ierrors;
1079		psp->p.ppp_obytes = sc->sc_if.if_obytes;
1080		psp->p.ppp_opackets = sc->sc_if.if_opackets;
1081		psp->p.ppp_oerrors = sc->sc_if.if_oerrors;
1082#ifdef INET
1083		psp->vj.vjs_packets = sc->sc_comp.sls_packets;
1084		psp->vj.vjs_compressed = sc->sc_comp.sls_compressed;
1085		psp->vj.vjs_searches = sc->sc_comp.sls_searches;
1086		psp->vj.vjs_misses = sc->sc_comp.sls_misses;
1087		psp->vj.vjs_uncompressedin = sc->sc_comp.sls_uncompressedin;
1088		psp->vj.vjs_compressedin = sc->sc_comp.sls_compressedin;
1089		psp->vj.vjs_errorin = sc->sc_comp.sls_errorin;
1090		psp->vj.vjs_tossed = sc->sc_comp.sls_tossed;
1091#endif
1092		break;
1093
1094	case SIOCGPPPCSTATS:
1095		pcp = &((struct ifpppcstatsreq *) data)->stats;
1096		(void)memset(pcp, 0, sizeof(*pcp));
1097		break;
1098
1099	default:
1100		error = EINVAL;
1101	}
1102	splx(s);
1103	return (error);
1104}
1105