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