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