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