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