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