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