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