xref: /netbsd-src/sys/net/if_sl.c (revision 481fca6e59249d8ffcf24fef7cfbe7b131bfb080)
1 /*	$NetBSD: if_sl.c,v 1.58 2000/07/12 23:02:38 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. 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  * Note that splimp() is used throughout to block both (tty) input
66  * interrupts and network activity; thus, splimp must be >= spltty.
67  */
68 
69 #include "sl.h"
70 #if NSL > 0
71 
72 #include "opt_inet.h"
73 #include "bpfilter.h"
74 
75 #include <sys/param.h>
76 #include <sys/proc.h>
77 #include <sys/malloc.h>
78 #include <sys/mbuf.h>
79 #include <sys/buf.h>
80 #include <sys/dkstat.h>
81 #include <sys/socket.h>
82 #include <sys/ioctl.h>
83 #include <sys/file.h>
84 #include <sys/tty.h>
85 #include <sys/kernel.h>
86 #if __NetBSD__
87 #include <sys/systm.h>
88 #endif
89 
90 #include <machine/cpu.h>
91 
92 #include <net/if.h>
93 #include <net/if_types.h>
94 #include <net/netisr.h>
95 #include <net/route.h>
96 
97 #ifdef INET
98 #include <netinet/in.h>
99 #include <netinet/in_systm.h>
100 #include <netinet/in_var.h>
101 #include <netinet/ip.h>
102 #else
103 #error Slip without inet?
104 #endif
105 
106 #include <net/slcompress.h>
107 #include <net/if_slvar.h>
108 #include <net/slip.h>
109 
110 #if NBPFILTER > 0
111 #include <sys/time.h>
112 #include <net/bpf.h>
113 #endif
114 
115 /*
116  * SLMAX is a hard limit on input packet size.  To simplify the code
117  * and improve performance, we require that packets fit in an mbuf
118  * cluster, and if we get a compressed packet, there's enough extra
119  * room to expand the header into a max length tcp/ip header (128
120  * bytes).  So, SLMAX can be at most
121  *	MCLBYTES - 128
122  *
123  * SLMTU is a hard limit on output packet size.  To insure good
124  * interactive response, SLMTU wants to be the smallest size that
125  * amortizes the header cost.  (Remember that even with
126  * type-of-service queuing, we have to wait for any in-progress
127  * packet to finish.  I.e., we wait, on the average, 1/2 * mtu /
128  * cps, where cps is the line speed in characters per second.
129  * E.g., 533ms wait for a 1024 byte MTU on a 9600 baud line.  The
130  * average compressed header size is 6-8 bytes so any MTU > 90
131  * bytes will give us 90% of the line bandwidth.  A 100ms wait is
132  * tolerable (500ms is not), so want an MTU around 296.  (Since TCP
133  * will send 256 byte segments (to allow for 40 byte headers), the
134  * typical packet size on the wire will be around 260 bytes).  In
135  * 4.3tahoe+ systems, we can set an MTU in a route so we do that &
136  * leave the interface MTU relatively high (so we don't IP fragment
137  * when acting as a gateway to someone using a stupid MTU).
138  *
139  * Similar considerations apply to SLIP_HIWAT:  It's the amount of
140  * data that will be queued 'downstream' of us (i.e., in clists
141  * waiting to be picked up by the tty output interrupt).  If we
142  * queue a lot of data downstream, it's immune to our t.o.s. queuing.
143  * E.g., if SLIP_HIWAT is 1024, the interactive traffic in mixed
144  * telnet/ftp will see a 1 sec wait, independent of the mtu (the
145  * wait is dependent on the ftp window size but that's typically
146  * 1k - 4k).  So, we want SLIP_HIWAT just big enough to amortize
147  * the cost (in idle time on the wire) of the tty driver running
148  * off the end of its clists & having to call back slstart for a
149  * new packet.  For a tty interface with any buffering at all, this
150  * cost will be zero.  Even with a totally brain dead interface (like
151  * the one on a typical workstation), the cost will be <= 1 character
152  * time.  So, setting SLIP_HIWAT to ~100 guarantees that we'll lose
153  * at most 1% while maintaining good interactive response.
154  */
155 #if NBPFILTER > 0
156 #define	BUFOFFSET	(128+sizeof(struct ifnet **)+SLIP_HDRLEN)
157 #else
158 #define	BUFOFFSET	(128+sizeof(struct ifnet **))
159 #endif
160 #define	SLMAX		(MCLBYTES - BUFOFFSET)
161 #define	SLBUFSIZE	(SLMAX + BUFOFFSET)
162 #ifndef SLMTU
163 #define	SLMTU		296
164 #endif
165 #if (SLMTU < 3)
166 #error SLMTU way too small.
167 #endif
168 #define	SLIP_HIWAT	roundup(50,CBSIZE)
169 #ifndef __NetBSD__					/* XXX - cgd */
170 #define	CLISTRESERVE	1024	/* Can't let clists get too low */
171 #endif	/* !__NetBSD__ */
172 
173 /*
174  * SLIP ABORT ESCAPE MECHANISM:
175  *	(inspired by HAYES modem escape arrangement)
176  *	1sec escape 1sec escape 1sec escape { 1sec escape 1sec escape }
177  *	within window time signals a "soft" exit from slip mode by remote end
178  *	if the IFF_DEBUG flag is on.
179  */
180 #define	ABT_ESC		'\033'	/* can't be t_intr - distant host must know it*/
181 #define	ABT_IDLE	1	/* in seconds - idle before an escape */
182 #define	ABT_COUNT	3	/* count of escapes for abort */
183 #define	ABT_WINDOW	(ABT_COUNT*2+2)	/* in seconds - time to count */
184 
185 struct sl_softc sl_softc[NSL];
186 
187 #define FRAME_END	 	0xc0		/* Frame End */
188 #define FRAME_ESCAPE		0xdb		/* Frame Esc */
189 #define TRANS_FRAME_END	 	0xdc		/* transposed frame end */
190 #define TRANS_FRAME_ESCAPE 	0xdd		/* transposed frame esc */
191 
192 static int slinit __P((struct sl_softc *));
193 static struct mbuf *sl_btom __P((struct sl_softc *, int));
194 
195 /*
196  * Called from boot code to establish sl interfaces.
197  */
198 void
199 slattach()
200 {
201 	struct sl_softc *sc;
202 	int i = 0;
203 
204 	for (sc = sl_softc; i < NSL; sc++) {
205 		sc->sc_unit = i;		/* XXX */
206 		sprintf(sc->sc_if.if_xname, "sl%d", i++);
207 		sc->sc_if.if_softc = sc;
208 		sc->sc_if.if_mtu = SLMTU;
209 		sc->sc_if.if_flags =
210 		    IFF_POINTOPOINT | SC_AUTOCOMP | IFF_MULTICAST;
211 		sc->sc_if.if_type = IFT_SLIP;
212 		sc->sc_if.if_ioctl = slioctl;
213 		sc->sc_if.if_output = sloutput;
214 		sc->sc_if.if_snd.ifq_maxlen = 50;
215 		sc->sc_fastq.ifq_maxlen = 32;
216 		if_attach(&sc->sc_if);
217 #if NBPFILTER > 0
218 		bpfattach(&sc->sc_if.if_bpf, &sc->sc_if, DLT_SLIP, SLIP_HDRLEN);
219 #endif
220 	}
221 }
222 
223 static int
224 slinit(sc)
225 	struct sl_softc *sc;
226 {
227 
228 	if (sc->sc_ep == NULL) {
229 		/*
230 		 * XXX the trick this is used for is evil...
231 		 */
232 		sc->sc_xxx = (u_char *)malloc(MCLBYTES, M_MBUF, M_WAITOK);
233 		if (sc->sc_xxx)
234 			sc->sc_ep = sc->sc_xxx + SLBUFSIZE;
235 		else {
236 			printf("sl%d: can't allocate buffer\n", sc->sc_unit);
237 			sc->sc_if.if_flags &= ~IFF_UP;
238 			return (0);
239 		}
240 	}
241 	sc->sc_buf = sc->sc_ep - SLMAX;
242 	sc->sc_mp = sc->sc_buf;
243 	sl_compress_init(&sc->sc_comp);
244 	return (1);
245 }
246 
247 /*
248  * Line specific open routine.
249  * Attach the given tty to the first available sl unit.
250  */
251 /* ARGSUSED */
252 int
253 slopen(dev, tp)
254 	dev_t dev;
255 	struct tty *tp;
256 {
257 	struct proc *p = curproc;		/* XXX */
258 	struct sl_softc *sc;
259 	int nsl;
260 	int error;
261 	int s;
262 
263 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
264 		return (error);
265 
266 	if (tp->t_line == SLIPDISC)
267 		return (0);
268 
269 	for (nsl = NSL, sc = sl_softc; --nsl >= 0; sc++)
270 		if (sc->sc_ttyp == NULL) {
271 			if (slinit(sc) == 0)
272 				return (ENOBUFS);
273 			tp->t_sc = (caddr_t)sc;
274 			sc->sc_ttyp = tp;
275 			sc->sc_if.if_baudrate = tp->t_ospeed;
276 			s = spltty();
277 			tp->t_state |= TS_ISOPEN | TS_XCLUDE;
278 			splx(s);
279 			ttyflush(tp, FREAD | FWRITE);
280 #ifdef __NetBSD__
281 			/*
282 			 * make sure tty output queue is large enough
283 			 * to hold a full-sized packet (including frame
284 			 * end, and a possible extra frame end).  full-sized
285 			 * packet occupies a max of 2*SLMAX bytes (because
286 			 * of possible escapes), and add two on for frame
287 			 * ends.
288 			 */
289 			s = spltty();
290 			if (tp->t_outq.c_cn < 2*SLMAX+2) {
291 				sc->sc_oldbufsize = tp->t_outq.c_cn;
292 				sc->sc_oldbufquot = tp->t_outq.c_cq != 0;
293 
294 				clfree(&tp->t_outq);
295 				error = clalloc(&tp->t_outq, 2*SLMAX+2, 0);
296 				if (error) {
297 					splx(s);
298 					return(error);
299 				}
300 			} else
301 				sc->sc_oldbufsize = sc->sc_oldbufquot = 0;
302 			splx(s);
303 #endif /* __NetBSD__ */
304 			return (0);
305 		}
306 	return (ENXIO);
307 }
308 
309 /*
310  * Line specific close routine.
311  * Detach the tty from the sl unit.
312  */
313 void
314 slclose(tp)
315 	struct tty *tp;
316 {
317 	struct sl_softc *sc;
318 	int s;
319 
320 	ttywflush(tp);
321 	s = splimp();		/* actually, max(spltty, splsoftnet) */
322 	tp->t_line = 0;
323 	tp->t_state = 0;
324 	sc = (struct sl_softc *)tp->t_sc;
325 	if (sc != NULL) {
326 		if_down(&sc->sc_if);
327 		sc->sc_ttyp = NULL;
328 		tp->t_sc = NULL;
329 		free((caddr_t)(sc->sc_ep - SLBUFSIZE), M_MBUF);
330 		sc->sc_ep = 0;
331 		sc->sc_mp = 0;
332 		sc->sc_buf = 0;
333 	}
334 #ifdef __NetBSD__
335 	/* if necessary, install a new outq buffer of the appropriate size */
336 	if (sc->sc_oldbufsize != 0) {
337 		clfree(&tp->t_outq);
338 		clalloc(&tp->t_outq, sc->sc_oldbufsize, sc->sc_oldbufquot);
339 	}
340 #endif
341 	splx(s);
342 }
343 
344 /*
345  * Line specific (tty) ioctl routine.
346  * Provide a way to get the sl unit number.
347  */
348 /* ARGSUSED */
349 int
350 sltioctl(tp, cmd, data, flag)
351 	struct tty *tp;
352 	u_long cmd;
353 	caddr_t data;
354 	int flag;
355 {
356 	struct sl_softc *sc = (struct sl_softc *)tp->t_sc;
357 
358 	switch (cmd) {
359 	case SLIOCGUNIT:
360 		*(int *)data = sc->sc_unit;	/* XXX */
361 		break;
362 
363 	default:
364 		return (-1);
365 	}
366 	return (0);
367 }
368 
369 /*
370  * Queue a packet.  Start transmission if not active.
371  * Compression happens in slstart; if we do it here, IP TOS
372  * will cause us to not compress "background" packets, because
373  * ordering gets trashed.  It can be done for all packets in slstart.
374  */
375 int
376 sloutput(ifp, m, dst, rtp)
377 	struct ifnet *ifp;
378 	struct mbuf *m;
379 	struct sockaddr *dst;
380 	struct rtentry *rtp;
381 {
382 	struct sl_softc *sc = ifp->if_softc;
383 	struct ip *ip;
384 	struct ifqueue *ifq;
385 	int s;
386 
387 	/*
388 	 * `Cannot happen' (see slioctl).  Someday we will extend
389 	 * the line protocol to support other address families.
390 	 */
391 	if (dst->sa_family != AF_INET) {
392 		printf("%s: af%d not supported\n", sc->sc_if.if_xname,
393 		    dst->sa_family);
394 		m_freem(m);
395 		sc->sc_if.if_noproto++;
396 		return (EAFNOSUPPORT);
397 	}
398 
399 	if (sc->sc_ttyp == NULL) {
400 		m_freem(m);
401 		return (ENETDOWN);	/* sort of */
402 	}
403 	if ((sc->sc_ttyp->t_state & TS_CARR_ON) == 0 &&
404 	    (sc->sc_ttyp->t_cflag & CLOCAL) == 0) {
405 		m_freem(m);
406 		printf("%s: no carrier and not local\n", sc->sc_if.if_xname);
407 		return (EHOSTUNREACH);
408 	}
409 	ifq = &sc->sc_if.if_snd;
410 	ip = mtod(m, struct ip *);
411 	if (sc->sc_if.if_flags & SC_NOICMP && ip->ip_p == IPPROTO_ICMP) {
412 		m_freem(m);
413 		return (ENETRESET);		/* XXX ? */
414 	}
415 	if (ip->ip_tos & IPTOS_LOWDELAY)
416 		ifq = &sc->sc_fastq;
417 	s = splimp();
418 	if (sc->sc_oqlen && sc->sc_ttyp->t_outq.c_cc == sc->sc_oqlen) {
419 		struct timeval tv;
420 
421 		/* if output's been stalled for too long, and restart */
422 		timersub(&time, &sc->sc_if.if_lastchange, &tv);
423 		if (tv.tv_sec > 0) {
424 			sc->sc_otimeout++;
425 			slstart(sc->sc_ttyp);
426 		}
427 	}
428 	if (IF_QFULL(ifq)) {
429 		IF_DROP(ifq);
430 		m_freem(m);
431 		splx(s);
432 		sc->sc_if.if_oerrors++;
433 		return (ENOBUFS);
434 	}
435 	IF_ENQUEUE(ifq, m);
436 	sc->sc_if.if_lastchange = time;
437 	if ((sc->sc_oqlen = sc->sc_ttyp->t_outq.c_cc) == 0)
438 		slstart(sc->sc_ttyp);
439 	splx(s);
440 	return (0);
441 }
442 
443 /*
444  * Start output on interface.  Get another datagram
445  * to send from the interface queue and map it to
446  * the interface before starting output.
447  */
448 void
449 slstart(tp)
450 	struct tty *tp;
451 {
452 	struct sl_softc *sc = (struct sl_softc *)tp->t_sc;
453 	struct mbuf *m;
454 	u_char *cp;
455 	struct ip *ip;
456 	int s;
457 	struct mbuf *m2;
458 #if NBPFILTER > 0
459 	u_char *bpfbuf;
460 	int len = 0;
461 #endif
462 #ifndef __NetBSD__					/* XXX - cgd */
463 	extern int cfreecount;
464 #endif
465 #if NBPFILTER > 0
466 	MALLOC(bpfbuf, u_char *, SLMAX + SLIP_HDRLEN, M_DEVBUF, M_NOWAIT);
467 #endif
468 
469 	for (;;) {
470 		/*
471 		 * If there is more in the output queue, just send it now.
472 		 * We are being called in lieu of ttstart and must do what
473 		 * it would.
474 		 */
475 		if (tp->t_outq.c_cc != 0) {
476 			(*tp->t_oproc)(tp);
477 			if (tp->t_outq.c_cc > SLIP_HIWAT) {
478 #if NBPFILTER > 0
479 				if (bpfbuf != NULL) FREE(bpfbuf, M_DEVBUF);
480 #endif
481 				return;
482 			}
483 		}
484 		/*
485 		 * This happens briefly when the line shuts down.
486 		 */
487 		if (sc == NULL) {
488 #if NBPFILTER > 0
489 			if (bpfbuf != NULL) FREE(bpfbuf, M_DEVBUF);
490 #endif
491 			return;
492 		}
493 
494 #ifdef __NetBSD__					/* XXX - cgd */
495 		/*
496 		 * Do not remove the packet from the IP queue if it
497 		 * doesn't look like the packet will fit into the
498 		 * current serial output queue, with a packet full of
499 		 * escapes this could be as bad as MTU*2+2.
500 		 */
501 		if (tp->t_outq.c_cn - tp->t_outq.c_cc < 2*sc->sc_if.if_mtu+2) {
502 #if NBPFILTER > 0
503 			if (bpfbuf != NULL) FREE(bpfbuf, M_DEVBUF);
504 #endif
505 			return;
506 		}
507 #endif /* __NetBSD__ */
508 
509 		/*
510 		 * Get a packet and send it to the interface.
511 		 */
512 		s = splimp();
513 		IF_DEQUEUE(&sc->sc_fastq, m);
514 		if (m)
515 			sc->sc_if.if_omcasts++;		/* XXX */
516 		else
517 			IF_DEQUEUE(&sc->sc_if.if_snd, m);
518 		splx(s);
519 		if (m == NULL) {
520 #if NBPFILTER > 0
521 			if (bpfbuf != NULL) FREE(bpfbuf, M_DEVBUF);
522 #endif
523 			return;
524 		}
525 
526 		/*
527 		 * We do the header compression here rather than in sloutput
528 		 * because the packets will be out of order if we are using TOS
529 		 * queueing, and the connection id compression will get
530 		 * munged when this happens.
531 		 */
532 #if NBPFILTER > 0
533 		if (sc->sc_if.if_bpf && bpfbuf != NULL) {
534 			/*
535 			 * We need to save the TCP/IP header before it's
536 			 * compressed.  To avoid complicated code, we just
537 			 * copy the entire packet into a stack buffer (since
538 			 * this is a serial line, packets should be short
539 			 * and/or the copy should be negligible cost compared
540 			 * to the packet transmission time).
541 			 */
542 			struct mbuf *m1 = m;
543 			u_char *cp = bpfbuf + SLIP_HDRLEN;
544 
545 			len = 0;
546 			do {
547 				int mlen = m1->m_len;
548 
549 				bcopy(mtod(m1, caddr_t), cp, mlen);
550 				cp += mlen;
551 				len += mlen;
552 			} while ((m1 = m1->m_next) != NULL);
553 		}
554 #endif
555 		if ((ip = mtod(m, struct ip *))->ip_p == IPPROTO_TCP) {
556 			if (sc->sc_if.if_flags & SC_COMPRESS)
557 				*mtod(m, u_char *) |= sl_compress_tcp(m, ip,
558 				    &sc->sc_comp, 1);
559 		}
560 #if NBPFILTER > 0
561 		if (sc->sc_if.if_bpf && bpfbuf != NULL) {
562 			/*
563 			 * Put the SLIP pseudo-"link header" in place.  The
564 			 * compressed header is now at the beginning of the
565 			 * mbuf.
566 			 */
567 			bpfbuf[SLX_DIR] = SLIPDIR_OUT;
568 			bcopy(mtod(m, caddr_t), &bpfbuf[SLX_CHDR], CHDR_LEN);
569 			bpf_tap(sc->sc_if.if_bpf, bpfbuf, len + SLIP_HDRLEN);
570 		}
571 #endif
572 		sc->sc_if.if_lastchange = time;
573 
574 #ifndef __NetBSD__					/* XXX - cgd */
575 		/*
576 		 * If system is getting low on clists, just flush our
577 		 * output queue (if the stuff was important, it'll get
578 		 * retransmitted).
579 		 */
580 		if (cfreecount < CLISTRESERVE + SLMTU) {
581 			m_freem(m);
582 			sc->sc_if.if_collisions++;
583 			continue;
584 		}
585 #endif /* !__NetBSD__ */
586 		/*
587 		 * The extra FRAME_END will start up a new packet, and thus
588 		 * will flush any accumulated garbage.  We do this whenever
589 		 * the line may have been idle for some time.
590 		 */
591 		if (tp->t_outq.c_cc == 0) {
592 			++sc->sc_if.if_obytes;
593 			(void) putc(FRAME_END, &tp->t_outq);
594 		}
595 
596 		while (m) {
597 			u_char *ep;
598 
599 			cp = mtod(m, u_char *); ep = cp + m->m_len;
600 			while (cp < ep) {
601 				/*
602 				 * Find out how many bytes in the string we can
603 				 * handle without doing something special.
604 				 */
605 				u_char *bp = cp;
606 
607 				while (cp < ep) {
608 					switch (*cp++) {
609 					case FRAME_ESCAPE:
610 					case FRAME_END:
611 						--cp;
612 						goto out;
613 					}
614 				}
615 				out:
616 				if (cp > bp) {
617 					/*
618 					 * Put n characters at once
619 					 * into the tty output queue.
620 					 */
621 #ifdef __NetBSD__					/* XXX - cgd */
622 					if (b_to_q((u_char *)bp, cp - bp,
623 #else
624 					if (b_to_q((char *)bp, cp - bp,
625 #endif
626 					    &tp->t_outq))
627 						break;
628 					sc->sc_if.if_obytes += cp - bp;
629 				}
630 				/*
631 				 * If there are characters left in the mbuf,
632 				 * the first one must be special..
633 				 * Put it out in a different form.
634 				 */
635 				if (cp < ep) {
636 					if (putc(FRAME_ESCAPE, &tp->t_outq))
637 						break;
638 					if (putc(*cp++ == FRAME_ESCAPE ?
639 					   TRANS_FRAME_ESCAPE : TRANS_FRAME_END,
640 					   &tp->t_outq)) {
641 						(void) unputc(&tp->t_outq);
642 						break;
643 					}
644 					sc->sc_if.if_obytes += 2;
645 				}
646 			}
647 			MFREE(m, m2);
648 			m = m2;
649 		}
650 
651 		if (putc(FRAME_END, &tp->t_outq)) {
652 			/*
653 			 * Not enough room.  Remove a char to make room
654 			 * and end the packet normally.
655 			 * If you get many collisions (more than one or two
656 			 * a day) you probably do not have enough clists
657 			 * and you should increase "nclist" in param.c.
658 			 */
659 			(void) unputc(&tp->t_outq);
660 			(void) putc(FRAME_END, &tp->t_outq);
661 			sc->sc_if.if_collisions++;
662 		} else {
663 			++sc->sc_if.if_obytes;
664 			sc->sc_if.if_opackets++;
665 		}
666 	}
667 }
668 
669 /*
670  * Copy data buffer to mbuf chain; add ifnet pointer.
671  */
672 static struct mbuf *
673 sl_btom(sc, len)
674 	struct sl_softc *sc;
675 	int len;
676 {
677 	struct mbuf *m;
678 	u_char *p;
679 
680 	MGETHDR(m, M_DONTWAIT, MT_DATA);
681 	if (m == NULL)
682 		return (NULL);
683 
684 	/*
685 	 * If we have more than MHLEN bytes, it's cheaper to
686 	 * queue the cluster we just filled & allocate a new one
687 	 * for the input buffer.  Otherwise, fill the mbuf we
688 	 * allocated above.  Note that code in the input routine
689 	 * guarantees that packet will fit in a cluster.
690 	 */
691 	if (len >= MHLEN) {
692 		/*
693 		 * XXX this is that evil trick I mentioned...
694 		 */
695 		p = sc->sc_xxx;
696 		sc->sc_xxx = (u_char *)malloc(MCLBYTES, M_MBUF, M_NOWAIT);
697 		if (sc->sc_xxx == NULL) {
698 			/*
699 			 * We couldn't allocate a new buffer - if
700 			 * memory's this low, it's time to start
701 			 * dropping packets.
702 			 */
703 			(void) m_free(m);
704 			return (NULL);
705 		}
706 		sc->sc_ep = sc->sc_xxx + SLBUFSIZE;
707 		MEXTADD(m, p, MCLBYTES, M_MBUF, NULL, NULL);
708 		m->m_data = (caddr_t)sc->sc_buf;
709 	} else
710 		bcopy((caddr_t)sc->sc_buf, mtod(m, caddr_t), len);
711 
712 	m->m_len = len;
713 	m->m_pkthdr.len = len;
714 	m->m_pkthdr.rcvif = &sc->sc_if;
715 	return (m);
716 }
717 
718 /*
719  * tty interface receiver interrupt.
720  */
721 void
722 slinput(c, tp)
723 	int c;
724 	struct tty *tp;
725 {
726 	struct sl_softc *sc;
727 	struct mbuf *m;
728 	int len;
729 	int s;
730 #if NBPFILTER > 0
731 	u_char chdr[CHDR_LEN];
732 #endif
733 
734 	tk_nin++;
735 	sc = (struct sl_softc *)tp->t_sc;
736 	if (sc == NULL)
737 		return;
738 	if ((c & TTY_ERRORMASK) || ((tp->t_state & TS_CARR_ON) == 0 &&
739 	    (tp->t_cflag & CLOCAL) == 0)) {
740 		sc->sc_flags |= SC_ERROR;
741 		return;
742 	}
743 	c &= TTY_CHARMASK;
744 
745 	++sc->sc_if.if_ibytes;
746 
747 	if (sc->sc_if.if_flags & IFF_DEBUG) {
748 		if (c == ABT_ESC) {
749 			/*
750 			 * If we have a previous abort, see whether
751 			 * this one is within the time limit.
752 			 */
753 			if (sc->sc_abortcount &&
754 			    time.tv_sec >= sc->sc_starttime + ABT_WINDOW)
755 				sc->sc_abortcount = 0;
756 			/*
757 			 * If we see an abort after "idle" time, count it;
758 			 * record when the first abort escape arrived.
759 			 */
760 			if (time.tv_sec >= sc->sc_lasttime + ABT_IDLE) {
761 				if (++sc->sc_abortcount == 1)
762 					sc->sc_starttime = time.tv_sec;
763 				if (sc->sc_abortcount >= ABT_COUNT) {
764 					slclose(tp);
765 					return;
766 				}
767 			}
768 		} else
769 			sc->sc_abortcount = 0;
770 		sc->sc_lasttime = time.tv_sec;
771 	}
772 
773 	switch (c) {
774 
775 	case TRANS_FRAME_ESCAPE:
776 		if (sc->sc_escape)
777 			c = FRAME_ESCAPE;
778 		break;
779 
780 	case TRANS_FRAME_END:
781 		if (sc->sc_escape)
782 			c = FRAME_END;
783 		break;
784 
785 	case FRAME_ESCAPE:
786 		sc->sc_escape = 1;
787 		return;
788 
789 	case FRAME_END:
790 		if(sc->sc_flags & SC_ERROR) {
791 			sc->sc_flags &= ~SC_ERROR;
792 			goto newpack;
793 		}
794 		len = sc->sc_mp - sc->sc_buf;
795 		if (len < 3)
796 			/* less than min length packet - ignore */
797 			goto newpack;
798 
799 #if NBPFILTER > 0
800 		if (sc->sc_if.if_bpf) {
801 			/*
802 			 * Save the compressed header, so we
803 			 * can tack it on later.  Note that we
804 			 * will end up copying garbage in some
805 			 * cases but this is okay.  We remember
806 			 * where the buffer started so we can
807 			 * compute the new header length.
808 			 */
809 			bcopy(sc->sc_buf, chdr, CHDR_LEN);
810 		}
811 #endif
812 
813 		if ((c = (*sc->sc_buf & 0xf0)) != (IPVERSION << 4)) {
814 			if (c & 0x80)
815 				c = TYPE_COMPRESSED_TCP;
816 			else if (c == TYPE_UNCOMPRESSED_TCP)
817 				*sc->sc_buf &= 0x4f; /* XXX */
818 			/*
819 			 * We've got something that's not an IP packet.
820 			 * If compression is enabled, try to decompress it.
821 			 * Otherwise, if `auto-enable' compression is on and
822 			 * it's a reasonable packet, decompress it and then
823 			 * enable compression.  Otherwise, drop it.
824 			 */
825 			if (sc->sc_if.if_flags & SC_COMPRESS) {
826 				len = sl_uncompress_tcp(&sc->sc_buf, len,
827 							(u_int)c, &sc->sc_comp);
828 				if (len <= 0)
829 					goto error;
830 			} else if ((sc->sc_if.if_flags & SC_AUTOCOMP) &&
831 			    c == TYPE_UNCOMPRESSED_TCP && len >= 40) {
832 				len = sl_uncompress_tcp(&sc->sc_buf, len,
833 							(u_int)c, &sc->sc_comp);
834 				if (len <= 0)
835 					goto error;
836 				sc->sc_if.if_flags |= SC_COMPRESS;
837 			} else
838 				goto error;
839 		}
840 #if NBPFILTER > 0
841 		if (sc->sc_if.if_bpf) {
842 			/*
843 			 * Put the SLIP pseudo-"link header" in place.
844 			 * We couldn't do this any earlier since
845 			 * decompression probably moved the buffer
846 			 * pointer.  Then, invoke BPF.
847 			 */
848 			u_char *hp = sc->sc_buf - SLIP_HDRLEN;
849 
850 			hp[SLX_DIR] = SLIPDIR_IN;
851 			bcopy(chdr, &hp[SLX_CHDR], CHDR_LEN);
852 			bpf_tap(sc->sc_if.if_bpf, hp, len + SLIP_HDRLEN);
853 		}
854 #endif
855 		m = sl_btom(sc, len);
856 		if (m == NULL)
857 			goto error;
858 
859 		sc->sc_if.if_ipackets++;
860 		sc->sc_if.if_lastchange = time;
861 		s = splimp();
862 		if (IF_QFULL(&ipintrq)) {
863 			IF_DROP(&ipintrq);
864 			sc->sc_if.if_ierrors++;
865 			sc->sc_if.if_iqdrops++;
866 			m_freem(m);
867 		} else {
868 			IF_ENQUEUE(&ipintrq, m);
869 			schednetisr(NETISR_IP);
870 		}
871 		splx(s);
872 		goto newpack;
873 	}
874 	if (sc->sc_mp < sc->sc_ep) {
875 		*sc->sc_mp++ = c;
876 		sc->sc_escape = 0;
877 		return;
878 	}
879 
880 	/* can't put lower; would miss an extra frame */
881 	sc->sc_flags |= SC_ERROR;
882 
883 error:
884 	sc->sc_if.if_ierrors++;
885 newpack:
886 	sc->sc_mp = sc->sc_buf = sc->sc_ep - SLMAX;
887 	sc->sc_escape = 0;
888 }
889 
890 /*
891  * Process an ioctl request.
892  */
893 int
894 slioctl(ifp, cmd, data)
895 	struct ifnet *ifp;
896 	u_long cmd;
897 	caddr_t data;
898 {
899 	struct ifaddr *ifa = (struct ifaddr *)data;
900 	struct ifreq *ifr = (struct ifreq *)data;
901 	int s = splimp(), error = 0;
902 	struct sl_softc *sc = ifp->if_softc;
903 
904 	switch (cmd) {
905 
906 	case SIOCSIFADDR:
907 		if (ifa->ifa_addr->sa_family == AF_INET)
908 			ifp->if_flags |= IFF_UP;
909 		else
910 			error = EAFNOSUPPORT;
911 		break;
912 
913 	case SIOCSIFDSTADDR:
914 		if (ifa->ifa_addr->sa_family != AF_INET)
915 			error = EAFNOSUPPORT;
916 		break;
917 
918 	case SIOCSIFMTU:
919 		if ((ifr->ifr_mtu < 3) || (ifr->ifr_mtu > SLMAX)) {
920 		    error = EINVAL;
921 		    break;
922 		}
923 		sc->sc_if.if_mtu = ifr->ifr_mtu;
924 		break;
925 
926 	case SIOCGIFMTU:
927 		ifr->ifr_mtu = sc->sc_if.if_mtu;
928 		break;
929 
930 	case SIOCADDMULTI:
931 	case SIOCDELMULTI:
932 		if (ifr == 0) {
933 			error = EAFNOSUPPORT;		/* XXX */
934 			break;
935 		}
936 		switch (ifr->ifr_addr.sa_family) {
937 
938 #ifdef INET
939 		case AF_INET:
940 			break;
941 #endif
942 
943 		default:
944 			error = EAFNOSUPPORT;
945 			break;
946 		}
947 		break;
948 
949 	default:
950 		error = EINVAL;
951 	}
952 	splx(s);
953 	return (error);
954 }
955 #endif
956