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