xref: /netbsd-src/sys/net/if_sl.c (revision 3b01aba77a7a698587faaae455bbfe740923c1f5)
1 /*	$NetBSD: if_sl.c,v 1.77 2001/07/15 21:17:29 martin 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. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *	@(#)if_sl.c	8.9 (Berkeley) 1/9/95
36  */
37 
38 /*
39  * Serial Line interface
40  *
41  * Rick Adams
42  * Center for Seismic Studies
43  * 1300 N 17th Street, Suite 1450
44  * Arlington, Virginia 22209
45  * (703)276-7900
46  * rick@seismo.ARPA
47  * seismo!rick
48  *
49  * Pounded on heavily by Chris Torek (chris@mimsy.umd.edu, umcp-cs!chris).
50  * N.B.: this belongs in netinet, not net, the way it stands now.
51  * Should have a link-layer type designation, but wouldn't be
52  * backwards-compatible.
53  *
54  * Converted to 4.3BSD Beta by Chris Torek.
55  * Other changes made at Berkeley, based in part on code by Kirk Smith.
56  * W. Jolitz added slip abort.
57  *
58  * Hacked almost beyond recognition by Van Jacobson (van@helios.ee.lbl.gov).
59  * Added priority queuing for "interactive" traffic; hooks for TCP
60  * header compression; ICMP filtering (at 2400 baud, some cretin
61  * pinging you can use up all your bandwidth).  Made low clist behavior
62  * more robust and slightly less likely to hang serial line.
63  * Sped up a bunch of things.
64  */
65 
66 #include "sl.h"
67 #if NSL > 0
68 
69 #include "opt_inet.h"
70 #include "bpfilter.h"
71 
72 #include <sys/param.h>
73 #include <sys/proc.h>
74 #include <sys/malloc.h>
75 #include <sys/mbuf.h>
76 #include <sys/buf.h>
77 #include <sys/dkstat.h>
78 #include <sys/socket.h>
79 #include <sys/ioctl.h>
80 #include <sys/file.h>
81 #include <sys/conf.h>
82 #include <sys/tty.h>
83 #include <sys/kernel.h>
84 #if __NetBSD__
85 #include <sys/systm.h>
86 #endif
87 
88 #include <machine/cpu.h>
89 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
90 #include <machine/intr.h>
91 #endif
92 
93 #include <net/if.h>
94 #include <net/if_types.h>
95 #include <net/netisr.h>
96 #include <net/route.h>
97 
98 #ifdef INET
99 #include <netinet/in.h>
100 #include <netinet/in_systm.h>
101 #include <netinet/in_var.h>
102 #include <netinet/ip.h>
103 #else
104 #error Slip without inet?
105 #endif
106 
107 #include <net/slcompress.h>
108 #include <net/if_slvar.h>
109 #include <net/slip.h>
110 
111 #if NBPFILTER > 0
112 #include <sys/time.h>
113 #include <net/bpf.h>
114 #endif
115 
116 /*
117  * SLMAX is a hard limit on input packet size.  To simplify the code
118  * and improve performance, we require that packets fit in an mbuf
119  * cluster, and if we get a compressed packet, there's enough extra
120  * room to expand the header into a max length tcp/ip header (128
121  * bytes).  So, SLMAX can be at most
122  *	MCLBYTES - 128
123  *
124  * SLMTU is a hard limit on output packet size.  To insure good
125  * interactive response, SLMTU wants to be the smallest size that
126  * amortizes the header cost.  (Remember that even with
127  * type-of-service queuing, we have to wait for any in-progress
128  * packet to finish.  I.e., we wait, on the average, 1/2 * mtu /
129  * cps, where cps is the line speed in characters per second.
130  * E.g., 533ms wait for a 1024 byte MTU on a 9600 baud line.  The
131  * average compressed header size is 6-8 bytes so any MTU > 90
132  * bytes will give us 90% of the line bandwidth.  A 100ms wait is
133  * tolerable (500ms is not), so want an MTU around 296.  (Since TCP
134  * will send 256 byte segments (to allow for 40 byte headers), the
135  * typical packet size on the wire will be around 260 bytes).  In
136  * 4.3tahoe+ systems, we can set an MTU in a route so we do that &
137  * leave the interface MTU relatively high (so we don't IP fragment
138  * when acting as a gateway to someone using a stupid MTU).
139  *
140  * Similar considerations apply to SLIP_HIWAT:  It's the amount of
141  * data that will be queued 'downstream' of us (i.e., in clists
142  * waiting to be picked up by the tty output interrupt).  If we
143  * queue a lot of data downstream, it's immune to our t.o.s. queuing.
144  * E.g., if SLIP_HIWAT is 1024, the interactive traffic in mixed
145  * telnet/ftp will see a 1 sec wait, independent of the mtu (the
146  * wait is dependent on the ftp window size but that's typically
147  * 1k - 4k).  So, we want SLIP_HIWAT just big enough to amortize
148  * the cost (in idle time on the wire) of the tty driver running
149  * off the end of its clists & having to call back slstart for a
150  * new packet.  For a tty interface with any buffering at all, this
151  * cost will be zero.  Even with a totally brain dead interface (like
152  * the one on a typical workstation), the cost will be <= 1 character
153  * time.  So, setting SLIP_HIWAT to ~100 guarantees that we'll lose
154  * at most 1% while maintaining good interactive response.
155  */
156 #define	BUFOFFSET	(128+sizeof(struct ifnet **)+SLIP_HDRLEN)
157 #define	SLMAX		(MCLBYTES - BUFOFFSET)
158 #define	SLBUFSIZE	(SLMAX + BUFOFFSET)
159 #ifndef SLMTU
160 #define	SLMTU		296
161 #endif
162 #if (SLMTU < 3)
163 #error SLMTU way too small.
164 #endif
165 #define	SLIP_HIWAT	roundup(50,CBSIZE)
166 #ifndef __NetBSD__					/* XXX - cgd */
167 #define	CLISTRESERVE	1024	/* Can't let clists get too low */
168 #endif	/* !__NetBSD__ */
169 
170 /*
171  * SLIP ABORT ESCAPE MECHANISM:
172  *	(inspired by HAYES modem escape arrangement)
173  *	1sec escape 1sec escape 1sec escape { 1sec escape 1sec escape }
174  *	within window time signals a "soft" exit from slip mode by remote end
175  *	if the IFF_DEBUG flag is on.
176  */
177 #define	ABT_ESC		'\033'	/* can't be t_intr - distant host must know it*/
178 #define	ABT_IDLE	1	/* in seconds - idle before an escape */
179 #define	ABT_COUNT	3	/* count of escapes for abort */
180 #define	ABT_WINDOW	(ABT_COUNT*2+2)	/* in seconds - time to count */
181 
182 struct sl_softc sl_softc[NSL];
183 
184 #define FRAME_END	 	0xc0		/* Frame End */
185 #define FRAME_ESCAPE		0xdb		/* Frame Esc */
186 #define TRANS_FRAME_END	 	0xdc		/* transposed frame end */
187 #define TRANS_FRAME_ESCAPE 	0xdd		/* transposed frame esc */
188 
189 #ifndef __HAVE_GENERIC_SOFT_INTERRUPTS
190 void	slnetisr(void);
191 #endif
192 void	slintr(void *);
193 
194 static int slinit __P((struct sl_softc *));
195 static struct mbuf *sl_btom __P((struct sl_softc *, int));
196 
197 /*
198  * Called from boot code to establish sl interfaces.
199  */
200 void
201 slattach()
202 {
203 	struct sl_softc *sc;
204 	int i = 0;
205 
206 	for (sc = sl_softc; i < NSL; sc++) {
207 		sc->sc_unit = i;		/* XXX */
208 		sprintf(sc->sc_if.if_xname, "sl%d", i++);
209 		sc->sc_if.if_softc = sc;
210 		sc->sc_if.if_mtu = SLMTU;
211 		sc->sc_if.if_flags =
212 		    IFF_POINTOPOINT | SC_AUTOCOMP | IFF_MULTICAST;
213 		sc->sc_if.if_type = IFT_SLIP;
214 		sc->sc_if.if_ioctl = slioctl;
215 		sc->sc_if.if_output = sloutput;
216 		sc->sc_if.if_dlt = DLT_SLIP;
217 		sc->sc_fastq.ifq_maxlen = 32;
218 		IFQ_SET_READY(&sc->sc_if.if_snd);
219 		if_attach(&sc->sc_if);
220 		if_alloc_sadl(&sc->sc_if);
221 #if NBPFILTER > 0
222 		bpfattach(&sc->sc_if, DLT_SLIP, SLIP_HDRLEN);
223 #endif
224 	}
225 }
226 
227 static int
228 slinit(sc)
229 	struct sl_softc *sc;
230 {
231 
232 	if (sc->sc_mbuf == NULL) {
233 		MGETHDR(sc->sc_mbuf, M_WAIT, MT_DATA);
234 		MCLGET(sc->sc_mbuf, M_WAIT);
235 	}
236 	sc->sc_ep = (u_char *) sc->sc_mbuf->m_ext.ext_buf +
237 	    sc->sc_mbuf->m_ext.ext_size;
238 	sc->sc_mp = sc->sc_pktstart = (u_char *) sc->sc_mbuf->m_ext.ext_buf +
239 	    BUFOFFSET;
240 
241 	sl_compress_init(&sc->sc_comp);
242 
243 	return (1);
244 }
245 
246 /*
247  * Line specific open routine.
248  * Attach the given tty to the first available sl unit.
249  */
250 /* ARGSUSED */
251 int
252 slopen(dev, tp)
253 	dev_t dev;
254 	struct tty *tp;
255 {
256 	struct proc *p = curproc;		/* XXX */
257 	struct sl_softc *sc;
258 	int nsl;
259 	int error;
260 	int s;
261 
262 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
263 		return (error);
264 
265 	if (tp->t_linesw->l_no == SLIPDISC)
266 		return (0);
267 
268 	for (nsl = NSL, sc = sl_softc; --nsl >= 0; sc++)
269 		if (sc->sc_ttyp == NULL) {
270 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
271 			sc->sc_si = softintr_establish(IPL_SOFTNET,
272 			    slintr, sc);
273 			if (sc->sc_si == NULL)
274 				return (ENOMEM);
275 #endif
276 			if (slinit(sc) == 0) {
277 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
278 				softintr_disestablish(sc->sc_si);
279 #endif
280 				return (ENOBUFS);
281 			}
282 			tp->t_sc = (caddr_t)sc;
283 			sc->sc_ttyp = tp;
284 			sc->sc_if.if_baudrate = tp->t_ospeed;
285 			s = spltty();
286 			tp->t_state |= TS_ISOPEN | TS_XCLUDE;
287 			splx(s);
288 			ttyflush(tp, FREAD | FWRITE);
289 #ifdef __NetBSD__
290 			/*
291 			 * make sure tty output queue is large enough
292 			 * to hold a full-sized packet (including frame
293 			 * end, and a possible extra frame end).  full-sized
294 			 * packet occupies a max of 2*SLMAX bytes (because
295 			 * of possible escapes), and add two on for frame
296 			 * ends.
297 			 */
298 			s = spltty();
299 			if (tp->t_outq.c_cn < 2*SLMAX+2) {
300 				sc->sc_oldbufsize = tp->t_outq.c_cn;
301 				sc->sc_oldbufquot = tp->t_outq.c_cq != 0;
302 
303 				clfree(&tp->t_outq);
304 				error = clalloc(&tp->t_outq, 2*SLMAX+2, 0);
305 				if (error) {
306 					splx(s);
307 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
308 					softintr_disestablish(sc->sc_si);
309 #endif
310 					return(error);
311 				}
312 			} else
313 				sc->sc_oldbufsize = sc->sc_oldbufquot = 0;
314 			splx(s);
315 #endif /* __NetBSD__ */
316 			return (0);
317 		}
318 	return (ENXIO);
319 }
320 
321 /*
322  * Line specific close routine.
323  * Detach the tty from the sl unit.
324  */
325 void
326 slclose(tp)
327 	struct tty *tp;
328 {
329 	struct sl_softc *sc;
330 	int s;
331 
332 	ttywflush(tp);
333 	sc = tp->t_sc;
334 
335 	if (sc != NULL) {
336 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
337 		softintr_disestablish(sc->sc_si);
338 #endif
339 		s = splnet();
340 		if_down(&sc->sc_if);
341 		IF_PURGE(&sc->sc_fastq);
342 		splx(s);
343 
344 		s = spltty();
345 		tp->t_linesw = linesw[0];	/* default line disc. */
346 		tp->t_state = 0;
347 
348 		sc->sc_ttyp = NULL;
349 		tp->t_sc = NULL;
350 
351 		m_freem(sc->sc_mbuf);
352 		sc->sc_mbuf = NULL;
353 		sc->sc_ep = sc->sc_mp = sc->sc_pktstart = NULL;
354 		IF_PURGE(&sc->sc_inq);
355 
356 		/*
357 		 * If necessary, install a new outq buffer of the
358 		 * appropriate size.
359 		 */
360 		if (sc->sc_oldbufsize != 0) {
361 			clfree(&tp->t_outq);
362 			clalloc(&tp->t_outq, sc->sc_oldbufsize,
363 			    sc->sc_oldbufquot);
364 		}
365 		splx(s);
366 	}
367 }
368 
369 /*
370  * Line specific (tty) ioctl routine.
371  * Provide a way to get the sl unit number.
372  */
373 /* ARGSUSED */
374 int
375 sltioctl(tp, cmd, data, flag)
376 	struct tty *tp;
377 	u_long cmd;
378 	caddr_t data;
379 	int flag;
380 {
381 	struct sl_softc *sc = (struct sl_softc *)tp->t_sc;
382 
383 	switch (cmd) {
384 	case SLIOCGUNIT:
385 		*(int *)data = sc->sc_unit;	/* XXX */
386 		break;
387 
388 	default:
389 		return (-1);
390 	}
391 	return (0);
392 }
393 
394 /*
395  * Queue a packet.  Start transmission if not active.
396  * Compression happens in slintr(); if we do it here, IP TOS
397  * will cause us to not compress "background" packets, because
398  * ordering gets trashed.  It can be done for all packets in slintr().
399  */
400 int
401 sloutput(ifp, m, dst, rtp)
402 	struct ifnet *ifp;
403 	struct mbuf *m;
404 	struct sockaddr *dst;
405 	struct rtentry *rtp;
406 {
407 	struct sl_softc *sc = ifp->if_softc;
408 	struct ip *ip;
409 	int s, error;
410 	ALTQ_DECL(struct altq_pktattr pktattr;)
411 
412 	IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family, &pktattr);
413 
414 	/*
415 	 * `Cannot happen' (see slioctl).  Someday we will extend
416 	 * the line protocol to support other address families.
417 	 */
418 	if (dst->sa_family != AF_INET) {
419 		printf("%s: af%d not supported\n", sc->sc_if.if_xname,
420 		    dst->sa_family);
421 		m_freem(m);
422 		sc->sc_if.if_noproto++;
423 		return (EAFNOSUPPORT);
424 	}
425 
426 	if (sc->sc_ttyp == NULL) {
427 		m_freem(m);
428 		return (ENETDOWN);	/* sort of */
429 	}
430 	if ((sc->sc_ttyp->t_state & TS_CARR_ON) == 0 &&
431 	    (sc->sc_ttyp->t_cflag & CLOCAL) == 0) {
432 		m_freem(m);
433 		printf("%s: no carrier and not local\n", sc->sc_if.if_xname);
434 		return (EHOSTUNREACH);
435 	}
436 	ip = mtod(m, struct ip *);
437 	if (sc->sc_if.if_flags & SC_NOICMP && ip->ip_p == IPPROTO_ICMP) {
438 		m_freem(m);
439 		return (ENETRESET);		/* XXX ? */
440 	}
441 
442 	s = spltty();
443 	if (sc->sc_oqlen && sc->sc_ttyp->t_outq.c_cc == sc->sc_oqlen) {
444 		struct timeval tv;
445 
446 		/* if output's been stalled for too long, and restart */
447 		timersub(&time, &sc->sc_lastpacket, &tv);
448 		if (tv.tv_sec > 0) {
449 			sc->sc_otimeout++;
450 			slstart(sc->sc_ttyp);
451 		}
452 	}
453 	splx(s);
454 
455 	s = splnet();
456 	if ((ip->ip_tos & IPTOS_LOWDELAY) != 0
457 #ifdef ALTQ
458 	    && ALTQ_IS_ENABLED(&ifp->if_snd) == 0
459 #endif
460 	    ) {
461 		if (IF_QFULL(&sc->sc_fastq)) {
462 			IF_DROP(&sc->sc_fastq);
463 			m_freem(m);
464 			error = ENOBUFS;
465 		} else {
466 			IF_ENQUEUE(&sc->sc_fastq, m);
467 			error = 0;
468 		}
469 	} else
470 		IFQ_ENQUEUE(&ifp->if_snd, m, &pktattr, error);
471 	if (error) {
472 		splx(s);
473 		ifp->if_oerrors++;
474 		return (error);
475 	}
476 	sc->sc_lastpacket = time;
477 	splx(s);
478 
479 	s = spltty();
480 	if ((sc->sc_oqlen = sc->sc_ttyp->t_outq.c_cc) == 0)
481 		slstart(sc->sc_ttyp);
482 	splx(s);
483 
484 	return (0);
485 }
486 
487 /*
488  * Start output on interface.  Get another datagram
489  * to send from the interface queue and map it to
490  * the interface before starting output.
491  */
492 void
493 slstart(tp)
494 	struct tty *tp;
495 {
496 	struct sl_softc *sc = tp->t_sc;
497 
498 	/*
499 	 * If there is more in the output queue, just send it now.
500 	 * We are being called in lieu of ttstart and must do what
501 	 * it would.
502 	 */
503 	if (tp->t_outq.c_cc != 0) {
504 		(*tp->t_oproc)(tp);
505 		if (tp->t_outq.c_cc > SLIP_HIWAT)
506 			return;
507 	}
508 
509 	/*
510 	 * This happens briefly when the line shuts down.
511 	 */
512 	if (sc == NULL)
513 		return;
514 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
515 	softintr_schedule(sc->sc_si);
516 #else
517     {
518 	int s = splhigh();
519 	schednetisr(NETISR_SLIP);
520 	splx(s);
521     }
522 #endif
523 }
524 
525 /*
526  * Copy data buffer to mbuf chain; add ifnet pointer.
527  */
528 static struct mbuf *
529 sl_btom(sc, len)
530 	struct sl_softc *sc;
531 	int len;
532 {
533 	struct mbuf *m;
534 
535 	/*
536 	 * Allocate a new input buffer and swap.
537 	 */
538 	m = sc->sc_mbuf;
539 	MGETHDR(sc->sc_mbuf, M_DONTWAIT, MT_DATA);
540 	if (sc->sc_mbuf == NULL) {
541 		sc->sc_mbuf = m;
542 		return (NULL);
543 	}
544 	MCLGET(sc->sc_mbuf, M_DONTWAIT);
545 	if ((sc->sc_mbuf->m_flags & M_EXT) == 0) {
546 		m_freem(sc->sc_mbuf);
547 		sc->sc_mbuf = m;
548 		return (NULL);
549 	}
550 	sc->sc_ep = (u_char *) sc->sc_mbuf->m_ext.ext_buf +
551 	    sc->sc_mbuf->m_ext.ext_size;
552 
553 	m->m_data = sc->sc_pktstart;
554 
555 	m->m_pkthdr.len = m->m_len = len;
556 	m->m_pkthdr.rcvif = &sc->sc_if;
557 	return (m);
558 }
559 
560 /*
561  * tty interface receiver interrupt.
562  */
563 void
564 slinput(c, tp)
565 	int c;
566 	struct tty *tp;
567 {
568 	struct sl_softc *sc;
569 	struct mbuf *m;
570 	int len;
571 
572 	tk_nin++;
573 	sc = (struct sl_softc *)tp->t_sc;
574 	if (sc == NULL)
575 		return;
576 	if ((c & TTY_ERRORMASK) || ((tp->t_state & TS_CARR_ON) == 0 &&
577 	    (tp->t_cflag & CLOCAL) == 0)) {
578 		sc->sc_flags |= SC_ERROR;
579 		return;
580 	}
581 	c &= TTY_CHARMASK;
582 
583 	++sc->sc_if.if_ibytes;
584 
585 	if (sc->sc_if.if_flags & IFF_DEBUG) {
586 		if (c == ABT_ESC) {
587 			/*
588 			 * If we have a previous abort, see whether
589 			 * this one is within the time limit.
590 			 */
591 			if (sc->sc_abortcount &&
592 			    time.tv_sec >= sc->sc_starttime + ABT_WINDOW)
593 				sc->sc_abortcount = 0;
594 			/*
595 			 * If we see an abort after "idle" time, count it;
596 			 * record when the first abort escape arrived.
597 			 */
598 			if (time.tv_sec >= sc->sc_lasttime + ABT_IDLE) {
599 				if (++sc->sc_abortcount == 1)
600 					sc->sc_starttime = time.tv_sec;
601 				if (sc->sc_abortcount >= ABT_COUNT) {
602 					slclose(tp);
603 					return;
604 				}
605 			}
606 		} else
607 			sc->sc_abortcount = 0;
608 		sc->sc_lasttime = time.tv_sec;
609 	}
610 
611 	switch (c) {
612 
613 	case TRANS_FRAME_ESCAPE:
614 		if (sc->sc_escape)
615 			c = FRAME_ESCAPE;
616 		break;
617 
618 	case TRANS_FRAME_END:
619 		if (sc->sc_escape)
620 			c = FRAME_END;
621 		break;
622 
623 	case FRAME_ESCAPE:
624 		sc->sc_escape = 1;
625 		return;
626 
627 	case FRAME_END:
628 		if(sc->sc_flags & SC_ERROR) {
629 			sc->sc_flags &= ~SC_ERROR;
630 			goto newpack;
631 		}
632 		len = sc->sc_mp - sc->sc_pktstart;
633 		if (len < 3)
634 			/* less than min length packet - ignore */
635 			goto newpack;
636 
637 		m = sl_btom(sc, len);
638 		if (m == NULL)
639 			goto error;
640 
641 		IF_ENQUEUE(&sc->sc_inq, m);
642 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
643 		softintr_schedule(sc->sc_si);
644 #else
645 	    {
646 		int s = splhigh();
647 		schednetisr(NETISR_SLIP);
648 		splx(s);
649 	    }
650 #endif
651 		goto newpack;
652 	}
653 	if (sc->sc_mp < sc->sc_ep) {
654 		*sc->sc_mp++ = c;
655 		sc->sc_escape = 0;
656 		return;
657 	}
658 
659 	/* can't put lower; would miss an extra frame */
660 	sc->sc_flags |= SC_ERROR;
661 
662 error:
663 	sc->sc_if.if_ierrors++;
664 newpack:
665 	sc->sc_mp = sc->sc_pktstart = (u_char *) sc->sc_mbuf->m_ext.ext_buf +
666 	    BUFOFFSET;
667 	sc->sc_escape = 0;
668 }
669 
670 #ifndef __HAVE_GENERIC_SOFT_INTERRUPTS
671 void
672 slnetisr(void)
673 {
674 	struct sl_softc *sc;
675 	int i;
676 
677 	for (i = 0; i < NSL; i++) {
678 		sc = &sl_softc[i];
679 		if (sc->sc_ttyp == NULL)
680 			continue;
681 		slintr(sc);
682 	}
683 }
684 #endif
685 
686 void
687 slintr(void *arg)
688 {
689 	struct sl_softc *sc = arg;
690 	struct tty *tp = sc->sc_ttyp;
691 	struct mbuf *m;
692 	int s, len;
693 	u_char *pktstart, c;
694 #if NBPFILTER > 0
695 	u_char chdr[CHDR_LEN];
696 #endif
697 
698 	KASSERT(tp != NULL);
699 
700 	/*
701 	 * Output processing loop.
702 	 */
703 	for (;;) {
704 		struct ip *ip;
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 		if ((ip = mtod(m, struct ip *))->ip_p == IPPROTO_TCP) {
760 			if (sc->sc_if.if_flags & SC_COMPRESS)
761 				*mtod(m, u_char *) |=
762 				    sl_compress_tcp(m, ip,
763 				    &sc->sc_comp, 1);
764 		}
765 #if NBPFILTER > 0
766 		if (sc->sc_if.if_bpf && bpf_m != NULL) {
767 			/*
768 			 * Put the SLIP pseudo-"link header" in
769 			 * place.  The compressed header is now
770 			 * at the beginning of the mbuf.
771 			 */
772 			struct mbuf n;
773 			u_char *hp;
774 
775 			n.m_next = bpf_m;
776 			n.m_data = n.m_dat;
777 			n.m_len = SLIP_HDRLEN;
778 
779 			hp = mtod(&n, u_char *);
780 
781 			hp[SLX_DIR] = SLIPDIR_OUT;
782 			memcpy(&hp[SLX_CHDR], mtod(m, caddr_t),
783 			    CHDR_LEN);
784 
785 			s = splnet();
786 			bpf_mtap(sc->sc_if.if_bpf, &n);
787 			splx(s);
788 			m_freem(bpf_m);
789 		}
790 #endif
791 		sc->sc_lastpacket = time;
792 
793 		s = spltty();
794 
795 		/*
796 		 * The extra FRAME_END will start up a new packet,
797 		 * and thus will flush any accumulated garbage.  We
798 		 * do this whenever the line may have been idle for
799 		 * some time.
800 		 */
801 		if (tp->t_outq.c_cc == 0) {
802 			sc->sc_if.if_obytes++;
803 			(void) putc(FRAME_END, &tp->t_outq);
804 		}
805 
806 		while (m) {
807 			u_char *bp, *cp, *ep;
808 
809 			bp = cp = mtod(m, u_char *);
810 			ep = cp + m->m_len;
811 			while (cp < ep) {
812 				/*
813 				 * Find out how many bytes in the
814 				 * string we can handle without
815 				 * doing something special.
816 				 */
817 				while (cp < ep) {
818 					switch (*cp++) {
819 					case FRAME_ESCAPE:
820 					case FRAME_END:
821 						cp--;
822 						goto out;
823 					}
824 				}
825 				out:
826 				if (cp > bp) {
827 					/*
828 					 * Put N characters at once
829 					 * into the tty output queue.
830 					 */
831 					if (b_to_q(bp, cp - bp,
832 					    &tp->t_outq))
833 						break;
834 					sc->sc_if.if_obytes += cp - bp;
835 				}
836 				/*
837 				 * If there are characters left in
838 				 * the mbuf, the first one must be
839 				 * special..  Put it out in a different
840 				 * form.
841 				 */
842 				if (cp < ep) {
843 					if (putc(FRAME_ESCAPE,
844 					    &tp->t_outq))
845 						break;
846 					if (putc(*cp++ == FRAME_ESCAPE ?
847 					    TRANS_FRAME_ESCAPE :
848 					    TRANS_FRAME_END,
849 					    &tp->t_outq)) {
850 						(void)
851 						   unputc(&tp->t_outq);
852 						break;
853 					}
854 					sc->sc_if.if_obytes += 2;
855 				}
856 				bp = cp;
857 			}
858 			MFREE(m, m2);
859 			m = m2;
860 		}
861 
862 		if (putc(FRAME_END, &tp->t_outq)) {
863 			/*
864 			 * Not enough room.  Remove a char to make
865 			 * room and end the packet normally.  If
866 			 * you get many collisions (more than one
867 			 * or two a day), you probably do not have
868 			 * enough clists and you should increase
869 			 * "nclist" in param.c
870 			 */
871 			(void) unputc(&tp->t_outq);
872 			(void) putc(FRAME_END, &tp->t_outq);
873 			sc->sc_if.if_collisions++;
874 		} else {
875 			sc->sc_if.if_obytes++;
876 			sc->sc_if.if_opackets++;
877 		}
878 
879 		/*
880 		 * We now have characters in the output queue,
881 		 * kick the serial port.
882 		 */
883 		(*tp->t_oproc)(tp);
884 		splx(s);
885 	}
886 
887 	/*
888 	 * Input processing loop.
889 	 */
890 	for (;;) {
891 		s = spltty();
892 		IF_DEQUEUE(&sc->sc_inq, m);
893 		splx(s);
894 		if (m == NULL)
895 			break;
896 		pktstart = mtod(m, u_char *);
897 		len = m->m_pkthdr.len;
898 #if NBPFILTER > 0
899 		if (sc->sc_if.if_bpf) {
900 			/*
901 			 * Save the compressed header, so we
902 			 * can tack it on later.  Note that we
903 			 * will end up copying garbage in some
904 			 * cases but this is okay.  We remember
905 			 * where the buffer started so we can
906 			 * compute the new header length.
907 			 */
908 			memcpy(chdr, pktstart, CHDR_LEN);
909 		}
910 #endif /* NBPFILTER > 0 */
911 		if ((c = (*pktstart & 0xf0)) != (IPVERSION << 4)) {
912 			if (c & 0x80)
913 				c = TYPE_COMPRESSED_TCP;
914 			else if (c == TYPE_UNCOMPRESSED_TCP)
915 				*pktstart &= 0x4f; /* XXX */
916 			/*
917 			 * We've got something that's not an IP
918 			 * packet.  If compression is enabled,
919 			 * try to decompress it.  Otherwise, if
920 			 * `auto-enable' compression is on and
921 			 * it's a reasonable packet, decompress
922 			 * it and then enable compression.
923 			 * Otherwise, drop it.
924 			 */
925 			if (sc->sc_if.if_flags & SC_COMPRESS) {
926 				len = sl_uncompress_tcp(&pktstart, len,
927 				    (u_int)c, &sc->sc_comp);
928 				if (len <= 0) {
929 					m_freem(m);
930 					continue;
931 				}
932 			} else if ((sc->sc_if.if_flags & SC_AUTOCOMP) &&
933 			    c == TYPE_UNCOMPRESSED_TCP && len >= 40) {
934 				len = sl_uncompress_tcp(&pktstart, len,
935 				    (u_int)c, &sc->sc_comp);
936 				if (len <= 0) {
937 					m_freem(m);
938 					continue;
939 				}
940 				sc->sc_if.if_flags |= SC_COMPRESS;
941 			} else {
942 				m_freem(m);
943 				continue;
944 			}
945 		}
946 		m->m_data = (caddr_t) pktstart;
947 		m->m_pkthdr.len = m->m_len = len;
948 #if NBPFILTER > 0
949 		if (sc->sc_if.if_bpf) {
950 			/*
951 			 * Put the SLIP pseudo-"link header" in place.
952 			 * Note this M_PREPEND() should bever fail,
953 			 * since we know we always have enough space
954 			 * in the input buffer.
955 			 */
956 			u_char *hp;
957 
958 			M_PREPEND(m, SLIP_HDRLEN, M_DONTWAIT);
959 			if (m == NULL)
960 				continue;
961 
962 			hp = mtod(m, u_char *);
963 			hp[SLX_DIR] = SLIPDIR_IN;
964 			memcpy(&hp[SLX_CHDR], chdr, CHDR_LEN);
965 
966 			s = splnet();
967 			bpf_mtap(sc->sc_if.if_bpf, m);
968 			splx(s);
969 
970 			m_adj(m, SLIP_HDRLEN);
971 		}
972 #endif /* NBPFILTER > 0 */
973 		/*
974 		 * If the packet will fit into a single
975 		 * header mbuf, copy it into one, to save
976 		 * memory.
977 		 */
978 		if (m->m_pkthdr.len < MHLEN) {
979 			struct mbuf *n;
980 
981 			MGETHDR(n, M_DONTWAIT, MT_DATA);
982 			M_COPY_PKTHDR(n, m);
983 			memcpy(mtod(n, caddr_t), mtod(m, caddr_t),
984 			    m->m_pkthdr.len);
985 			n->m_len = m->m_len;
986 			m_freem(m);
987 			m = n;
988 		}
989 
990 		sc->sc_if.if_ipackets++;
991 		sc->sc_lastpacket = time;
992 
993 		s = splnet();
994 		if (IF_QFULL(&ipintrq)) {
995 			IF_DROP(&ipintrq);
996 			sc->sc_if.if_ierrors++;
997 			sc->sc_if.if_iqdrops++;
998 			m_freem(m);
999 		} else {
1000 			IF_ENQUEUE(&ipintrq, m);
1001 			schednetisr(NETISR_IP);
1002 		}
1003 		splx(s);
1004 	}
1005 }
1006 
1007 /*
1008  * Process an ioctl request.
1009  */
1010 int
1011 slioctl(ifp, cmd, data)
1012 	struct ifnet *ifp;
1013 	u_long cmd;
1014 	caddr_t data;
1015 {
1016 	struct ifaddr *ifa = (struct ifaddr *)data;
1017 	struct ifreq *ifr = (struct ifreq *)data;
1018 	int s = splnet(), error = 0;
1019 	struct sl_softc *sc = ifp->if_softc;
1020 
1021 	switch (cmd) {
1022 
1023 	case SIOCSIFADDR:
1024 		if (ifa->ifa_addr->sa_family == AF_INET)
1025 			ifp->if_flags |= IFF_UP;
1026 		else
1027 			error = EAFNOSUPPORT;
1028 		break;
1029 
1030 	case SIOCSIFDSTADDR:
1031 		if (ifa->ifa_addr->sa_family != AF_INET)
1032 			error = EAFNOSUPPORT;
1033 		break;
1034 
1035 	case SIOCSIFMTU:
1036 		if ((ifr->ifr_mtu < 3) || (ifr->ifr_mtu > SLMAX)) {
1037 		    error = EINVAL;
1038 		    break;
1039 		}
1040 		sc->sc_if.if_mtu = ifr->ifr_mtu;
1041 		break;
1042 
1043 	case SIOCGIFMTU:
1044 		ifr->ifr_mtu = sc->sc_if.if_mtu;
1045 		break;
1046 
1047 	case SIOCADDMULTI:
1048 	case SIOCDELMULTI:
1049 		if (ifr == 0) {
1050 			error = EAFNOSUPPORT;		/* XXX */
1051 			break;
1052 		}
1053 		switch (ifr->ifr_addr.sa_family) {
1054 
1055 #ifdef INET
1056 		case AF_INET:
1057 			break;
1058 #endif
1059 
1060 		default:
1061 			error = EAFNOSUPPORT;
1062 			break;
1063 		}
1064 		break;
1065 
1066 	default:
1067 		error = EINVAL;
1068 	}
1069 	splx(s);
1070 	return (error);
1071 }
1072 #endif
1073