xref: /original-bsd/sys/net/if_sl.c (revision 7a38d872)
1 /*
2  * Copyright (c) 1987, 1989, 1992, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  *	@(#)if_sl.c	8.5 (Berkeley) 01/12/94
8  */
9 
10 /*
11  * Serial Line interface
12  *
13  * Rick Adams
14  * Center for Seismic Studies
15  * 1300 N 17th Street, Suite 1450
16  * Arlington, Virginia 22209
17  * (703)276-7900
18  * rick@seismo.ARPA
19  * seismo!rick
20  *
21  * Pounded on heavily by Chris Torek (chris@mimsy.umd.edu, umcp-cs!chris).
22  * N.B.: this belongs in netinet, not net, the way it stands now.
23  * Should have a link-layer type designation, but wouldn't be
24  * backwards-compatible.
25  *
26  * Converted to 4.3BSD Beta by Chris Torek.
27  * Other changes made at Berkeley, based in part on code by Kirk Smith.
28  * W. Jolitz added slip abort.
29  *
30  * Hacked almost beyond recognition by Van Jacobson (van@helios.ee.lbl.gov).
31  * Added priority queuing for "interactive" traffic; hooks for TCP
32  * header compression; ICMP filtering (at 2400 baud, some cretin
33  * pinging you can use up all your bandwidth).  Made low clist behavior
34  * more robust and slightly less likely to hang serial line.
35  * Sped up a bunch of things.
36  *
37  * Note that splimp() is used throughout to block both (tty) input
38  * interrupts and network activity; thus, splimp must be >= spltty.
39  */
40 
41 #include "sl.h"
42 #if NSL > 0
43 
44 #include "bpfilter.h"
45 
46 #include <sys/param.h>
47 #include <sys/proc.h>
48 #include <sys/mbuf.h>
49 #include <sys/buf.h>
50 #include <sys/dkstat.h>
51 #include <sys/socket.h>
52 #include <sys/ioctl.h>
53 #include <sys/file.h>
54 #include <sys/tty.h>
55 #include <sys/kernel.h>
56 #include <sys/conf.h>
57 
58 #include <machine/cpu.h>
59 
60 #include <net/if.h>
61 #include <net/if_types.h>
62 #include <net/netisr.h>
63 #include <net/route.h>
64 
65 #if INET
66 #include <netinet/in.h>
67 #include <netinet/in_systm.h>
68 #include <netinet/in_var.h>
69 #include <netinet/ip.h>
70 #else
71 Huh? Slip without inet?
72 #endif
73 
74 #include <net/slcompress.h>
75 #include <net/if_slvar.h>
76 
77 #if NBPFILTER > 0
78 #include <sys/time.h>
79 #include <net/bpf.h>
80 #endif
81 
82 /*
83  * SLMAX is a hard limit on input packet size.  To simplify the code
84  * and improve performance, we require that packets fit in an mbuf
85  * cluster, and if we get a compressed packet, there's enough extra
86  * room to expand the header into a max length tcp/ip header (128
87  * bytes).  So, SLMAX can be at most
88  *	MCLBYTES - 128
89  *
90  * SLMTU is a hard limit on output packet size.  To insure good
91  * interactive response, SLMTU wants to be the smallest size that
92  * amortizes the header cost.  (Remember that even with
93  * type-of-service queuing, we have to wait for any in-progress
94  * packet to finish.  I.e., we wait, on the average, 1/2 * mtu /
95  * cps, where cps is the line speed in characters per second.
96  * E.g., 533ms wait for a 1024 byte MTU on a 9600 baud line.  The
97  * average compressed header size is 6-8 bytes so any MTU > 90
98  * bytes will give us 90% of the line bandwidth.  A 100ms wait is
99  * tolerable (500ms is not), so want an MTU around 296.  (Since TCP
100  * will send 256 byte segments (to allow for 40 byte headers), the
101  * typical packet size on the wire will be around 260 bytes).  In
102  * 4.3tahoe+ systems, we can set an MTU in a route so we do that &
103  * leave the interface MTU relatively high (so we don't IP fragment
104  * when acting as a gateway to someone using a stupid MTU).
105  *
106  * Similar considerations apply to SLIP_HIWAT:  It's the amount of
107  * data that will be queued 'downstream' of us (i.e., in clists
108  * waiting to be picked up by the tty output interrupt).  If we
109  * queue a lot of data downstream, it's immune to our t.o.s. queuing.
110  * E.g., if SLIP_HIWAT is 1024, the interactive traffic in mixed
111  * telnet/ftp will see a 1 sec wait, independent of the mtu (the
112  * wait is dependent on the ftp window size but that's typically
113  * 1k - 4k).  So, we want SLIP_HIWAT just big enough to amortize
114  * the cost (in idle time on the wire) of the tty driver running
115  * off the end of its clists & having to call back slstart for a
116  * new packet.  For a tty interface with any buffering at all, this
117  * cost will be zero.  Even with a totally brain dead interface (like
118  * the one on a typical workstation), the cost will be <= 1 character
119  * time.  So, setting SLIP_HIWAT to ~100 guarantees that we'll lose
120  * at most 1% while maintaining good interactive response.
121  */
122 #if NBPFILTER > 0
123 #define	BUFOFFSET	(128+sizeof(struct ifnet **)+SLIP_HDRLEN)
124 #else
125 #define	BUFOFFSET	(128+sizeof(struct ifnet **))
126 #endif
127 #define	SLMAX		(MCLBYTES - BUFOFFSET)
128 #define	SLBUFSIZE	(SLMAX + BUFOFFSET)
129 #define	SLMTU		296
130 #define	SLIP_HIWAT	roundup(50,CBSIZE)
131 #define	CLISTRESERVE	1024	/* Can't let clists get too low */
132 
133 /*
134  * SLIP ABORT ESCAPE MECHANISM:
135  *	(inspired by HAYES modem escape arrangement)
136  *	1sec escape 1sec escape 1sec escape { 1sec escape 1sec escape }
137  *	within window time signals a "soft" exit from slip mode by remote end
138  *	if the IFF_DEBUG flag is on.
139  */
140 #define	ABT_ESC		'\033'	/* can't be t_intr - distant host must know it*/
141 #define	ABT_IDLE	1	/* in seconds - idle before an escape */
142 #define	ABT_COUNT	3	/* count of escapes for abort */
143 #define	ABT_WINDOW	(ABT_COUNT*2+2)	/* in seconds - time to count */
144 
145 struct sl_softc sl_softc[NSL];
146 
147 #define FRAME_END	 	0xc0		/* Frame End */
148 #define FRAME_ESCAPE		0xdb		/* Frame Esc */
149 #define TRANS_FRAME_END	 	0xdc		/* transposed frame end */
150 #define TRANS_FRAME_ESCAPE 	0xdd		/* transposed frame esc */
151 
152 extern struct timeval time;
153 
154 static int slinit __P((struct sl_softc *));
155 static struct mbuf *sl_btom __P((struct sl_softc *, int));
156 
157 /*
158  * Called from boot code to establish sl interfaces.
159  */
160 void
161 slattach()
162 {
163 	register struct sl_softc *sc;
164 	register int i = 0;
165 
166 	for (sc = sl_softc; i < NSL; sc++) {
167 		sc->sc_if.if_name = "sl";
168 		sc->sc_if.if_next = NULL;
169 		sc->sc_if.if_unit = i++;
170 		sc->sc_if.if_mtu = SLMTU;
171 		sc->sc_if.if_flags =
172 		    IFF_POINTOPOINT | SC_AUTOCOMP | IFF_MULTICAST;
173 		sc->sc_if.if_type = IFT_SLIP;
174 		sc->sc_if.if_ioctl = slioctl;
175 		sc->sc_if.if_output = sloutput;
176 		sc->sc_if.if_snd.ifq_maxlen = 50;
177 		sc->sc_fastq.ifq_maxlen = 32;
178 		if_attach(&sc->sc_if);
179 #if NBPFILTER > 0
180 		bpfattach(&sc->sc_bpf, &sc->sc_if, DLT_SLIP, SLIP_HDRLEN);
181 #endif
182 	}
183 }
184 
185 static int
186 slinit(sc)
187 	register struct sl_softc *sc;
188 {
189 	register caddr_t p;
190 
191 	if (sc->sc_ep == (u_char *) 0) {
192 		MCLALLOC(p, M_WAIT);
193 		if (p)
194 			sc->sc_ep = (u_char *)p + SLBUFSIZE;
195 		else {
196 			printf("sl%d: can't allocate buffer\n", sc - sl_softc);
197 			sc->sc_if.if_flags &= ~IFF_UP;
198 			return (0);
199 		}
200 	}
201 	sc->sc_buf = sc->sc_ep - SLMAX;
202 	sc->sc_mp = sc->sc_buf;
203 	sl_compress_init(&sc->sc_comp);
204 	return (1);
205 }
206 
207 /*
208  * Line specific open routine.
209  * Attach the given tty to the first available sl unit.
210  */
211 /* ARGSUSED */
212 int
213 slopen(dev, tp)
214 	dev_t dev;
215 	register struct tty *tp;
216 {
217 	struct proc *p = curproc;		/* XXX */
218 	register struct sl_softc *sc;
219 	register int nsl;
220 	int error;
221 
222 	if (error = suser(p->p_ucred, &p->p_acflag))
223 		return (error);
224 
225 	if (tp->t_line == SLIPDISC)
226 		return (0);
227 
228 	for (nsl = NSL, sc = sl_softc; --nsl >= 0; sc++)
229 		if (sc->sc_ttyp == NULL) {
230 			if (slinit(sc) == 0)
231 				return (ENOBUFS);
232 			tp->t_sc = (caddr_t)sc;
233 			sc->sc_ttyp = tp;
234 			sc->sc_if.if_baudrate = tp->t_ospeed;
235 			ttyflush(tp, FREAD | FWRITE);
236 			return (0);
237 		}
238 	return (ENXIO);
239 }
240 
241 /*
242  * Line specific close routine.
243  * Detach the tty from the sl unit.
244  */
245 void
246 slclose(tp)
247 	struct tty *tp;
248 {
249 	register struct sl_softc *sc;
250 	int s;
251 
252 	ttywflush(tp);
253 	s = splimp();		/* actually, max(spltty, splnet) */
254 	tp->t_line = 0;
255 	sc = (struct sl_softc *)tp->t_sc;
256 	if (sc != NULL) {
257 		if_down(&sc->sc_if);
258 		sc->sc_ttyp = NULL;
259 		tp->t_sc = NULL;
260 		MCLFREE((caddr_t)(sc->sc_ep - SLBUFSIZE));
261 		sc->sc_ep = 0;
262 		sc->sc_mp = 0;
263 		sc->sc_buf = 0;
264 	}
265 	splx(s);
266 }
267 
268 /*
269  * Line specific (tty) ioctl routine.
270  * Provide a way to get the sl unit number.
271  */
272 /* ARGSUSED */
273 int
274 sltioctl(tp, cmd, data, flag)
275 	struct tty *tp;
276 	int cmd;
277 	caddr_t data;
278 	int flag;
279 {
280 	struct sl_softc *sc = (struct sl_softc *)tp->t_sc;
281 
282 	switch (cmd) {
283 	case SLIOCGUNIT:
284 		*(int *)data = sc->sc_if.if_unit;
285 		break;
286 
287 	default:
288 		return (-1);
289 	}
290 	return (0);
291 }
292 
293 /*
294  * Queue a packet.  Start transmission if not active.
295  * Compression happens in slstart; if we do it here, IP TOS
296  * will cause us to not compress "background" packets, because
297  * ordering gets trashed.  It can be done for all packets in slstart.
298  */
299 int
300 sloutput(ifp, m, dst, rtp)
301 	struct ifnet *ifp;
302 	register struct mbuf *m;
303 	struct sockaddr *dst;
304 	struct rtentry *rtp;
305 {
306 	register struct sl_softc *sc = &sl_softc[ifp->if_unit];
307 	register struct ip *ip;
308 	register struct ifqueue *ifq;
309 	int s;
310 
311 	/*
312 	 * `Cannot happen' (see slioctl).  Someday we will extend
313 	 * the line protocol to support other address families.
314 	 */
315 	if (dst->sa_family != AF_INET) {
316 		printf("sl%d: af%d not supported\n", sc->sc_if.if_unit,
317 			dst->sa_family);
318 		m_freem(m);
319 		sc->sc_if.if_noproto++;
320 		return (EAFNOSUPPORT);
321 	}
322 
323 	if (sc->sc_ttyp == NULL) {
324 		m_freem(m);
325 		return (ENETDOWN);	/* sort of */
326 	}
327 	if ((sc->sc_ttyp->t_state & TS_CARR_ON) == 0 &&
328 	    (sc->sc_ttyp->t_cflag & CLOCAL) == 0) {
329 		m_freem(m);
330 		return (EHOSTUNREACH);
331 	}
332 	ifq = &sc->sc_if.if_snd;
333 	ip = mtod(m, struct ip *);
334 	if (sc->sc_if.if_flags & SC_NOICMP && ip->ip_p == IPPROTO_ICMP) {
335 		m_freem(m);
336 		return (ENETRESET);		/* XXX ? */
337 	}
338 	if (ip->ip_tos & IPTOS_LOWDELAY)
339 		ifq = &sc->sc_fastq;
340 	s = splimp();
341 	if (IF_QFULL(ifq)) {
342 		IF_DROP(ifq);
343 		m_freem(m);
344 		splx(s);
345 		sc->sc_if.if_oerrors++;
346 		return (ENOBUFS);
347 	}
348 	IF_ENQUEUE(ifq, m);
349 	sc->sc_if.if_lastchange = time;
350 	if (sc->sc_ttyp->t_outq.c_cc == 0)
351 		slstart(sc->sc_ttyp);
352 	splx(s);
353 	return (0);
354 }
355 
356 /*
357  * Start output on interface.  Get another datagram
358  * to send from the interface queue and map it to
359  * the interface before starting output.
360  */
361 void
362 slstart(tp)
363 	register struct tty *tp;
364 {
365 	register struct sl_softc *sc = (struct sl_softc *)tp->t_sc;
366 	register struct mbuf *m;
367 	register u_char *cp;
368 	register struct ip *ip;
369 	int s;
370 	struct mbuf *m2;
371 #if NBPFILTER > 0
372 	u_char bpfbuf[SLMTU + SLIP_HDRLEN];
373 	register int len;
374 #endif
375 	extern int cfreecount;
376 
377 	for (;;) {
378 		/*
379 		 * If there is more in the output queue, just send it now.
380 		 * We are being called in lieu of ttstart and must do what
381 		 * it would.
382 		 */
383 		if (tp->t_outq.c_cc != 0) {
384 			(*tp->t_oproc)(tp);
385 			if (tp->t_outq.c_cc > SLIP_HIWAT)
386 				return;
387 		}
388 		/*
389 		 * This happens briefly when the line shuts down.
390 		 */
391 		if (sc == NULL)
392 			return;
393 
394 		/*
395 		 * Get a packet and send it to the interface.
396 		 */
397 		s = splimp();
398 		IF_DEQUEUE(&sc->sc_fastq, m);
399 		if (m)
400 			sc->sc_if.if_omcasts++;		/* XXX */
401 		else
402 			IF_DEQUEUE(&sc->sc_if.if_snd, m);
403 		splx(s);
404 		if (m == NULL)
405 			return;
406 
407 		/*
408 		 * We do the header compression here rather than in sloutput
409 		 * because the packets will be out of order if we are using TOS
410 		 * queueing, and the connection id compression will get
411 		 * munged when this happens.
412 		 */
413 #if NBPFILTER > 0
414 		if (sc->sc_bpf) {
415 			/*
416 			 * We need to save the TCP/IP header before it's
417 			 * compressed.  To avoid complicated code, we just
418 			 * copy the entire packet into a stack buffer (since
419 			 * this is a serial line, packets should be short
420 			 * and/or the copy should be negligible cost compared
421 			 * to the packet transmission time).
422 			 */
423 			register struct mbuf *m1 = m;
424 			register u_char *cp = bpfbuf + SLIP_HDRLEN;
425 
426 			len = 0;
427 			do {
428 				register int mlen = m1->m_len;
429 
430 				bcopy(mtod(m1, caddr_t), cp, mlen);
431 				cp += mlen;
432 				len += mlen;
433 			} while (m1 = m1->m_next);
434 		}
435 #endif
436 		if ((ip = mtod(m, struct ip *))->ip_p == IPPROTO_TCP) {
437 			if (sc->sc_if.if_flags & SC_COMPRESS)
438 				*mtod(m, u_char *) |= sl_compress_tcp(m, ip,
439 				    &sc->sc_comp, 1);
440 		}
441 #if NBPFILTER > 0
442 		if (sc->sc_bpf) {
443 			/*
444 			 * Put the SLIP pseudo-"link header" in place.  The
445 			 * compressed header is now at the beginning of the
446 			 * mbuf.
447 			 */
448 			bpfbuf[SLX_DIR] = SLIPDIR_OUT;
449 			bcopy(mtod(m, caddr_t), &bpfbuf[SLX_CHDR], CHDR_LEN);
450 			bpf_tap(sc->sc_bpf, bpfbuf, len + SLIP_HDRLEN);
451 		}
452 #endif
453 		sc->sc_if.if_lastchange = time;
454 
455 		/*
456 		 * If system is getting low on clists, just flush our
457 		 * output queue (if the stuff was important, it'll get
458 		 * retransmitted).
459 		 */
460 		if (cfreecount < CLISTRESERVE + SLMTU) {
461 			m_freem(m);
462 			sc->sc_if.if_collisions++;
463 			continue;
464 		}
465 		/*
466 		 * The extra FRAME_END will start up a new packet, and thus
467 		 * will flush any accumulated garbage.  We do this whenever
468 		 * the line may have been idle for some time.
469 		 */
470 		if (tp->t_outq.c_cc == 0) {
471 			++sc->sc_if.if_obytes;
472 			(void) putc(FRAME_END, &tp->t_outq);
473 		}
474 
475 		while (m) {
476 			register u_char *ep;
477 
478 			cp = mtod(m, u_char *); ep = cp + m->m_len;
479 			while (cp < ep) {
480 				/*
481 				 * Find out how many bytes in the string we can
482 				 * handle without doing something special.
483 				 */
484 				register u_char *bp = cp;
485 
486 				while (cp < ep) {
487 					switch (*cp++) {
488 					case FRAME_ESCAPE:
489 					case FRAME_END:
490 						--cp;
491 						goto out;
492 					}
493 				}
494 				out:
495 				if (cp > bp) {
496 					/*
497 					 * Put n characters at once
498 					 * into the tty output queue.
499 					 */
500 					if (b_to_q((char *)bp, cp - bp,
501 					    &tp->t_outq))
502 						break;
503 					sc->sc_if.if_obytes += cp - bp;
504 				}
505 				/*
506 				 * If there are characters left in the mbuf,
507 				 * the first one must be special..
508 				 * Put it out in a different form.
509 				 */
510 				if (cp < ep) {
511 					if (putc(FRAME_ESCAPE, &tp->t_outq))
512 						break;
513 					if (putc(*cp++ == FRAME_ESCAPE ?
514 					   TRANS_FRAME_ESCAPE : TRANS_FRAME_END,
515 					   &tp->t_outq)) {
516 						(void) unputc(&tp->t_outq);
517 						break;
518 					}
519 					sc->sc_if.if_obytes += 2;
520 				}
521 			}
522 			MFREE(m, m2);
523 			m = m2;
524 		}
525 
526 		if (putc(FRAME_END, &tp->t_outq)) {
527 			/*
528 			 * Not enough room.  Remove a char to make room
529 			 * and end the packet normally.
530 			 * If you get many collisions (more than one or two
531 			 * a day) you probably do not have enough clists
532 			 * and you should increase "nclist" in param.c.
533 			 */
534 			(void) unputc(&tp->t_outq);
535 			(void) putc(FRAME_END, &tp->t_outq);
536 			sc->sc_if.if_collisions++;
537 		} else {
538 			++sc->sc_if.if_obytes;
539 			sc->sc_if.if_opackets++;
540 		}
541 	}
542 }
543 
544 /*
545  * Copy data buffer to mbuf chain; add ifnet pointer.
546  */
547 static struct mbuf *
548 sl_btom(sc, len)
549 	register struct sl_softc *sc;
550 	register int len;
551 {
552 	register struct mbuf *m;
553 
554 	MGETHDR(m, M_DONTWAIT, MT_DATA);
555 	if (m == NULL)
556 		return (NULL);
557 
558 	/*
559 	 * If we have more than MHLEN bytes, it's cheaper to
560 	 * queue the cluster we just filled & allocate a new one
561 	 * for the input buffer.  Otherwise, fill the mbuf we
562 	 * allocated above.  Note that code in the input routine
563 	 * guarantees that packet will fit in a cluster.
564 	 */
565 	if (len >= MHLEN) {
566 		MCLGET(m, M_DONTWAIT);
567 		if ((m->m_flags & M_EXT) == 0) {
568 			/*
569 			 * we couldn't get a cluster - if memory's this
570 			 * low, it's time to start dropping packets.
571 			 */
572 			(void) m_free(m);
573 			return (NULL);
574 		}
575 		sc->sc_ep = mtod(m, u_char *) + SLBUFSIZE;
576 		m->m_data = (caddr_t)sc->sc_buf;
577 		m->m_ext.ext_buf = (caddr_t)((int)sc->sc_buf &~ MCLOFSET);
578 	} else
579 		bcopy((caddr_t)sc->sc_buf, mtod(m, caddr_t), len);
580 
581 	m->m_len = len;
582 	m->m_pkthdr.len = len;
583 	m->m_pkthdr.rcvif = &sc->sc_if;
584 	return (m);
585 }
586 
587 /*
588  * tty interface receiver interrupt.
589  */
590 void
591 slinput(c, tp)
592 	register int c;
593 	register struct tty *tp;
594 {
595 	register struct sl_softc *sc;
596 	register struct mbuf *m;
597 	register int len;
598 	int s;
599 #if NBPFILTER > 0
600 	u_char chdr[CHDR_LEN];
601 #endif
602 
603 	tk_nin++;
604 	sc = (struct sl_softc *)tp->t_sc;
605 	if (sc == NULL)
606 		return;
607 	if (c & TTY_ERRORMASK || ((tp->t_state & TS_CARR_ON) == 0 &&
608 	    (tp->t_cflag & CLOCAL) == 0)) {
609 		sc->sc_flags |= SC_ERROR;
610 		return;
611 	}
612 	c &= TTY_CHARMASK;
613 
614 	++sc->sc_if.if_ibytes;
615 
616 	if (sc->sc_if.if_flags & IFF_DEBUG) {
617 		if (c == ABT_ESC) {
618 			/*
619 			 * If we have a previous abort, see whether
620 			 * this one is within the time limit.
621 			 */
622 			if (sc->sc_abortcount &&
623 			    time.tv_sec >= sc->sc_starttime + ABT_WINDOW)
624 				sc->sc_abortcount = 0;
625 			/*
626 			 * If we see an abort after "idle" time, count it;
627 			 * record when the first abort escape arrived.
628 			 */
629 			if (time.tv_sec >= sc->sc_lasttime + ABT_IDLE) {
630 				if (++sc->sc_abortcount == 1)
631 					sc->sc_starttime = time.tv_sec;
632 				if (sc->sc_abortcount >= ABT_COUNT) {
633 					slclose(tp);
634 					return;
635 				}
636 			}
637 		} else
638 			sc->sc_abortcount = 0;
639 		sc->sc_lasttime = time.tv_sec;
640 	}
641 
642 	switch (c) {
643 
644 	case TRANS_FRAME_ESCAPE:
645 		if (sc->sc_escape)
646 			c = FRAME_ESCAPE;
647 		break;
648 
649 	case TRANS_FRAME_END:
650 		if (sc->sc_escape)
651 			c = FRAME_END;
652 		break;
653 
654 	case FRAME_ESCAPE:
655 		sc->sc_escape = 1;
656 		return;
657 
658 	case FRAME_END:
659 		if(sc->sc_flags & SC_ERROR) {
660 			sc->sc_flags &= ~SC_ERROR;
661 			goto newpack;
662 		}
663 		len = sc->sc_mp - sc->sc_buf;
664 		if (len < 3)
665 			/* less than min length packet - ignore */
666 			goto newpack;
667 
668 #if NBPFILTER > 0
669 		if (sc->sc_bpf) {
670 			/*
671 			 * Save the compressed header, so we
672 			 * can tack it on later.  Note that we
673 			 * will end up copying garbage in some
674 			 * cases but this is okay.  We remember
675 			 * where the buffer started so we can
676 			 * compute the new header length.
677 			 */
678 			bcopy(sc->sc_buf, chdr, CHDR_LEN);
679 		}
680 #endif
681 
682 		if ((c = (*sc->sc_buf & 0xf0)) != (IPVERSION << 4)) {
683 			if (c & 0x80)
684 				c = TYPE_COMPRESSED_TCP;
685 			else if (c == TYPE_UNCOMPRESSED_TCP)
686 				*sc->sc_buf &= 0x4f; /* XXX */
687 			/*
688 			 * We've got something that's not an IP packet.
689 			 * If compression is enabled, try to decompress it.
690 			 * Otherwise, if `auto-enable' compression is on and
691 			 * it's a reasonable packet, decompress it and then
692 			 * enable compression.  Otherwise, drop it.
693 			 */
694 			if (sc->sc_if.if_flags & SC_COMPRESS) {
695 				len = sl_uncompress_tcp(&sc->sc_buf, len,
696 							(u_int)c, &sc->sc_comp);
697 				if (len <= 0)
698 					goto error;
699 			} else if ((sc->sc_if.if_flags & SC_AUTOCOMP) &&
700 			    c == TYPE_UNCOMPRESSED_TCP && len >= 40) {
701 				len = sl_uncompress_tcp(&sc->sc_buf, len,
702 							(u_int)c, &sc->sc_comp);
703 				if (len <= 0)
704 					goto error;
705 				sc->sc_if.if_flags |= SC_COMPRESS;
706 			} else
707 				goto error;
708 		}
709 #if NBPFILTER > 0
710 		if (sc->sc_bpf) {
711 			/*
712 			 * Put the SLIP pseudo-"link header" in place.
713 			 * We couldn't do this any earlier since
714 			 * decompression probably moved the buffer
715 			 * pointer.  Then, invoke BPF.
716 			 */
717 			register u_char *hp = sc->sc_buf - SLIP_HDRLEN;
718 
719 			hp[SLX_DIR] = SLIPDIR_IN;
720 			bcopy(chdr, &hp[SLX_CHDR], CHDR_LEN);
721 			bpf_tap(sc->sc_bpf, hp, len + SLIP_HDRLEN);
722 		}
723 #endif
724 		m = sl_btom(sc, len);
725 		if (m == NULL)
726 			goto error;
727 
728 		sc->sc_if.if_ipackets++;
729 		sc->sc_if.if_lastchange = time;
730 		s = splimp();
731 		if (IF_QFULL(&ipintrq)) {
732 			IF_DROP(&ipintrq);
733 			sc->sc_if.if_ierrors++;
734 			sc->sc_if.if_iqdrops++;
735 			m_freem(m);
736 		} else {
737 			IF_ENQUEUE(&ipintrq, m);
738 			schednetisr(NETISR_IP);
739 		}
740 		splx(s);
741 		goto newpack;
742 	}
743 	if (sc->sc_mp < sc->sc_ep) {
744 		*sc->sc_mp++ = c;
745 		sc->sc_escape = 0;
746 		return;
747 	}
748 
749 	/* can't put lower; would miss an extra frame */
750 	sc->sc_flags |= SC_ERROR;
751 
752 error:
753 	sc->sc_if.if_ierrors++;
754 newpack:
755 	sc->sc_mp = sc->sc_buf = sc->sc_ep - SLMAX;
756 	sc->sc_escape = 0;
757 }
758 
759 /*
760  * Process an ioctl request.
761  */
762 int
763 slioctl(ifp, cmd, data)
764 	register struct ifnet *ifp;
765 	int cmd;
766 	caddr_t data;
767 {
768 	register struct ifaddr *ifa = (struct ifaddr *)data;
769 	register struct ifreq *ifr;
770 	register int s = splimp(), error = 0;
771 
772 	switch (cmd) {
773 
774 	case SIOCSIFADDR:
775 		if (ifa->ifa_addr->sa_family == AF_INET)
776 			ifp->if_flags |= IFF_UP;
777 		else
778 			error = EAFNOSUPPORT;
779 		break;
780 
781 	case SIOCSIFDSTADDR:
782 		if (ifa->ifa_addr->sa_family != AF_INET)
783 			error = EAFNOSUPPORT;
784 		break;
785 
786 	case SIOCADDMULTI:
787 	case SIOCDELMULTI:
788 		ifr = (struct ifreq *)data;
789 		if (ifr == 0) {
790 			error = EAFNOSUPPORT;		/* XXX */
791 			break;
792 		}
793 		switch (ifr->ifr_addr.sa_family) {
794 
795 #ifdef INET
796 		case AF_INET:
797 			break;
798 #endif
799 
800 		default:
801 			error = EAFNOSUPPORT;
802 			break;
803 		}
804 		break;
805 
806 	default:
807 		error = EINVAL;
808 	}
809 	splx(s);
810 	return (error);
811 }
812 #endif
813