xref: /dragonfly/sys/net/sl/if_sl.c (revision e7d467f4)
1 /*
2  * (MPSAFE)
3  *
4  * Copyright (c) 1987, 1989, 1992, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *	@(#)if_sl.c	8.6 (Berkeley) 2/1/94
36  * $FreeBSD: src/sys/net/if_sl.c,v 1.84.2.2 2002/02/13 00:43:10 dillon Exp $
37  * $DragonFly: src/sys/net/sl/if_sl.c,v 1.32 2008/05/14 11:59:23 sephe Exp $
38  */
39 
40 /*
41  * Serial Line interface
42  *
43  * Rick Adams
44  * Center for Seismic Studies
45  * 1300 N 17th Street, Suite 1450
46  * Arlington, Virginia 22209
47  * (703)276-7900
48  * rick@seismo.ARPA
49  * seismo!rick
50  *
51  * Pounded on heavily by Chris Torek (chris@mimsy.umd.edu, umcp-cs!chris).
52  * N.B.: this belongs in netinet, not net, the way it stands now.
53  * Should have a link-layer type designation, but wouldn't be
54  * backwards-compatible.
55  *
56  * Converted to 4.3BSD Beta by Chris Torek.
57  * Other changes made at Berkeley, based in part on code by Kirk Smith.
58  * W. Jolitz added slip abort.
59  *
60  * Hacked almost beyond recognition by Van Jacobson (van@helios.ee.lbl.gov).
61  * Added priority queuing for "interactive" traffic; hooks for TCP
62  * header compression; ICMP filtering (at 2400 baud, some cretin
63  * pinging you can use up all your bandwidth).  Made low clist behavior
64  * more robust and slightly less likely to hang serial line.
65  * Sped up a bunch of things.
66  */
67 
68 #include "use_sl.h"
69 
70 #include "opt_inet.h"
71 #if !defined(KLD_MODULE)
72 #include "opt_slip.h"
73 #endif
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/proc.h>
77 #include <sys/priv.h>
78 #include <sys/malloc.h>
79 #include <sys/mbuf.h>
80 #include <sys/dkstat.h>
81 #include <sys/socket.h>
82 #include <sys/sockio.h>
83 #include <sys/fcntl.h>
84 #include <sys/signalvar.h>
85 #include <sys/tty.h>
86 #include <sys/clist.h>
87 #include <sys/kernel.h>
88 #include <sys/conf.h>
89 #include <sys/thread2.h>
90 
91 #include <net/if.h>
92 #include <net/if_types.h>
93 #include <net/ifq_var.h>
94 #include <net/netisr.h>
95 
96 #if INET
97 #include <netinet/in.h>
98 #include <netinet/in_systm.h>
99 #include <netinet/in_var.h>
100 #include <netinet/ip.h>
101 #else
102 #error "Huh? Slip without inet?"
103 #endif
104 
105 #include <net/slcompress.h>
106 #include "if_slvar.h"
107 #include <net/slip.h>
108 
109 #include <net/bpf.h>
110 
111 static void slattach (void *);
112 PSEUDO_SET(slattach, if_sl);
113 
114 /*
115  * SLRMAX is a hard limit on input packet size.  To simplify the code
116  * and improve performance, we require that packets fit in an mbuf
117  * cluster, and if we get a compressed packet, there's enough extra
118  * room to expand the header into a max length tcp/ip header (128
119  * bytes).  So, SLRMAX can be at most
120  *	MCLBYTES - 128
121  *
122  * SLMTU is the default transmit MTU. The transmit MTU should be kept
123  * small enough so that interactive use doesn't suffer, but large
124  * enough to provide good performance. 552 is a good choice for SLMTU
125  * because it is high enough to not fragment TCP packets being routed
126  * through this host. Packet fragmentation is bad with SLIP because
127  * fragment headers aren't compressed. The previous assumptions about
128  * the best MTU value don't really hold when using modern modems with
129  * BTLZ data compression because the modem buffers play a much larger
130  * role in interactive performance than the MTU. The MTU can be changed
131  * at any time to suit the specific environment with ifconfig(8), and
132  * its maximum value is defined as SLTMAX. SLTMAX must not be so large
133  * that it would overflow the stack if BPF is configured (XXX; if_ppp.c
134  * handles this better).
135  *
136  * SLIP_HIWAT is the amount of data that will be queued 'downstream'
137  * of us (i.e., in clists waiting to be picked up by the tty output
138  * interrupt).  If we queue a lot of data downstream, it's immune to
139  * our t.o.s. queuing.
140  * E.g., if SLIP_HIWAT is 1024, the interactive traffic in mixed
141  * telnet/ftp will see a 1 sec wait, independent of the mtu (the
142  * wait is dependent on the ftp window size but that's typically
143  * 1k - 4k).  So, we want SLIP_HIWAT just big enough to amortize
144  * the cost (in idle time on the wire) of the tty driver running
145  * off the end of its clists & having to call back slstart for a
146  * new packet.  For a tty interface with any buffering at all, this
147  * cost will be zero.  Even with a totally brain dead interface (like
148  * the one on a typical workstation), the cost will be <= 1 character
149  * time.  So, setting SLIP_HIWAT to ~100 guarantees that we'll lose
150  * at most 1% while maintaining good interactive response.
151  */
152 #define	BUFOFFSET	(128+sizeof(struct ifnet **)+SLIP_HDRLEN)
153 #define	SLRMAX		(MCLBYTES - BUFOFFSET)
154 #define	SLBUFSIZE	(SLRMAX + BUFOFFSET)
155 #ifndef SLMTU
156 #define	SLMTU		552		/* default MTU */
157 #endif
158 #define	SLTMAX		1500		/* maximum MTU */
159 #define	SLIP_HIWAT	roundup(50,CBSIZE)
160 #define	CLISTRESERVE	1024		/* Can't let clists get too low */
161 
162 /*
163  * SLIP ABORT ESCAPE MECHANISM:
164  *	(inspired by HAYES modem escape arrangement)
165  *	1sec escape 1sec escape 1sec escape { 1sec escape 1sec escape }
166  *	within window time signals a "soft" exit from slip mode by remote end
167  *	if the IFF_DEBUG flag is on.
168  */
169 #define	ABT_ESC		'\033'	/* can't be t_intr - distant host must know it*/
170 #define	ABT_IDLE	1	/* in seconds - idle before an escape */
171 #define	ABT_COUNT	3	/* count of escapes for abort */
172 #define	ABT_WINDOW	(ABT_COUNT*2+2)	/* in seconds - time to count */
173 
174 static struct sl_softc sl_softc[NSL];
175 
176 #define FRAME_END	 	0xc0		/* Frame End */
177 #define FRAME_ESCAPE		0xdb		/* Frame Esc */
178 #define TRANS_FRAME_END	 	0xdc		/* transposed frame end */
179 #define TRANS_FRAME_ESCAPE 	0xdd		/* transposed frame esc */
180 
181 static int slinit (struct sl_softc *);
182 static struct mbuf *sl_btom (struct sl_softc *, int);
183 static timeout_t sl_keepalive;
184 static timeout_t sl_outfill;
185 static int	slclose (struct tty *,int);
186 static int	slinput (int, struct tty *);
187 static int	slioctl (struct ifnet *, u_long, caddr_t, struct ucred *);
188 static int	sltioctl (struct tty *, u_long, caddr_t, int, struct ucred *);
189 static int	slopen (cdev_t, struct tty *);
190 static int	sloutput (struct ifnet *,
191 	    struct mbuf *, struct sockaddr *, struct rtentry *);
192 static int	slstart (struct tty *);
193 
194 static struct linesw slipdisc = {
195 	slopen,		slclose,	l_noread,	l_nowrite,
196 	sltioctl,	slinput,	slstart,	ttymodem,
197 	FRAME_END
198 };
199 
200 /*
201  * Called from boot code to establish sl interfaces.
202  */
203 static void
204 slattach(void *dummy)
205 {
206 	struct sl_softc *sc;
207 	int i = 0;
208 
209 	lwkt_gettoken(&tty_token);
210 	linesw[SLIPDISC] = slipdisc;
211 
212 	for (sc = sl_softc; i < NSL; sc++) {
213 		if_initname(&(sc->sc_if), "sl", i++);
214 		sc->sc_if.if_mtu = SLMTU;
215 		sc->sc_if.if_flags =
216 #ifdef SLIP_IFF_OPTS
217 		    SLIP_IFF_OPTS;
218 #else
219 		    IFF_POINTOPOINT | SC_AUTOCOMP | IFF_MULTICAST;
220 #endif
221 		sc->sc_if.if_type = IFT_SLIP;
222 		sc->sc_if.if_ioctl = slioctl;
223 		sc->sc_if.if_output = sloutput;
224 		ifq_set_maxlen(&sc->sc_if.if_snd, 50);
225 		ifq_set_ready(&sc->sc_if.if_snd);
226 		sc->sc_fastq.ifq_maxlen = 32;
227 		sc->sc_if.if_linkmib = sc;
228 		sc->sc_if.if_linkmiblen = sizeof *sc;
229 		callout_init_mp(&sc->sc_oftimeout);
230 		callout_init_mp(&sc->sc_katimeout);
231 		if_attach(&sc->sc_if, NULL);
232 		bpfattach(&sc->sc_if, DLT_SLIP, SLIP_HDRLEN);
233 	}
234 	lwkt_reltoken(&tty_token);
235 }
236 
237 static int
238 slinit(struct sl_softc *sc)
239 {
240 	lwkt_gettoken(&tty_token);
241 	if (sc->sc_ep == NULL)
242 		sc->sc_ep = kmalloc(SLBUFSIZE, M_DEVBUF, M_WAITOK);
243 	sc->sc_buf = sc->sc_ep + SLBUFSIZE - SLRMAX;
244 	sc->sc_mp = sc->sc_buf;
245 	sl_compress_init(&sc->sc_comp, -1);
246 	lwkt_reltoken(&tty_token);
247 	return (1);
248 }
249 
250 /*
251  * Line specific open routine.
252  * Attach the given tty to the first available sl unit.
253  */
254 /* ARGSUSED */
255 static int
256 slopen(cdev_t dev, struct tty *tp)
257 {
258 	struct sl_softc *sc;
259 	int nsl;
260 	int error;
261 	struct thread *td = curthread;	/* XXX */
262 
263 	error = priv_check(td, PRIV_ROOT);
264 	if (error)
265 		return (error);
266 
267 	lwkt_gettoken(&tty_token);
268 	if (tp->t_line == SLIPDISC) {
269 		lwkt_reltoken(&tty_token);
270 		return (0);
271 	}
272 
273 	for (nsl = NSL, sc = sl_softc; --nsl >= 0; sc++)
274 		if (sc->sc_ttyp == NULL && !(sc->sc_flags & SC_STATIC)) {
275 			if (slinit(sc) == 0) {
276 				lwkt_reltoken(&tty_token);
277 				return (ENOBUFS);
278 			}
279 			tp->t_sc = (caddr_t)sc;
280 			sc->sc_ttyp = tp;
281 			sc->sc_if.if_baudrate = tp->t_ospeed;
282 			ttyflush(tp, FREAD | FWRITE);
283 
284 			tp->t_line = SLIPDISC;
285 			/*
286 			 * We don't use t_canq or t_rawq, so reduce their
287 			 * cblock resources to 0.  Reserve enough cblocks
288 			 * for t_outq to guarantee that we can fit a full
289 			 * packet if the SLIP_HIWAT check allows slstart()
290 			 * to loop.  Use the same value for the cblock
291 			 * limit since the reserved blocks should always
292 			 * be enough.  Reserving cblocks probably makes
293 			 * the CLISTRESERVE check unnecessary and wasteful.
294 			 */
295 			clist_alloc_cblocks(&tp->t_canq, 0, 0);
296 			clist_alloc_cblocks(&tp->t_outq,
297 			    SLIP_HIWAT + 2 * sc->sc_if.if_mtu + 1,
298 			    SLIP_HIWAT + 2 * sc->sc_if.if_mtu + 1);
299 			clist_alloc_cblocks(&tp->t_rawq, 0, 0);
300 
301 			crit_enter();
302 			if_up(&sc->sc_if);
303 			crit_exit();
304 			lwkt_reltoken(&tty_token);
305 			return (0);
306 		}
307 	lwkt_reltoken(&tty_token);
308 	return (ENXIO);
309 }
310 
311 /*
312  * Line specific close routine.
313  * Detach the tty from the sl unit.
314  */
315 static int
316 slclose(struct tty *tp, int flag)
317 {
318 	struct sl_softc *sc;
319 
320 	lwkt_gettoken(&tty_token);
321 	ttyflush(tp, FREAD | FWRITE);
322 	crit_enter();
323 
324 	clist_free_cblocks(&tp->t_outq);
325 	tp->t_line = 0;
326 	sc = (struct sl_softc *)tp->t_sc;
327 	if (sc != NULL) {
328 		if (sc->sc_outfill) {
329 			sc->sc_outfill = 0;
330 			callout_stop(&sc->sc_oftimeout);
331 		}
332 		if (sc->sc_keepalive) {
333 			sc->sc_keepalive = 0;
334 			callout_stop(&sc->sc_katimeout);
335 		}
336 		if_down(&sc->sc_if);
337 		sc->sc_flags &= SC_STATIC;
338 		sc->sc_ttyp = NULL;
339 		tp->t_sc = NULL;
340 		if (sc->sc_ep) {
341 			kfree(sc->sc_ep, M_DEVBUF);
342 			sc->sc_ep = NULL;
343 		}
344 		sc->sc_mp = 0;
345 		sc->sc_buf = 0;
346 	}
347 	crit_exit();
348 	lwkt_reltoken(&tty_token);
349 	return 0;
350 }
351 
352 /*
353  * Line specific (tty) ioctl routine.
354  * Provide a way to get the sl unit number.
355  */
356 /* ARGSUSED */
357 static int
358 sltioctl(struct tty *tp, u_long cmd, caddr_t data, int flag, struct ucred *cred)
359 {
360 	struct sl_softc *sc = (struct sl_softc *)tp->t_sc, *nc, *tmpnc;
361 	int nsl;
362 
363 	crit_enter();
364 	lwkt_gettoken(&tty_token);
365 
366 	switch (cmd) {
367 	case SLIOCGUNIT:
368 		*(int *)data = sc->sc_if.if_dunit;
369 		break;
370 
371 	case SLIOCSUNIT:
372 		if (sc->sc_if.if_dunit != *(u_int *)data) {
373 			for (nsl = NSL, nc = sl_softc; --nsl >= 0; nc++) {
374 				if (   nc->sc_if.if_dunit == *(u_int *)data
375 				    && nc->sc_ttyp == NULL
376 				   ) {
377 					tmpnc = kmalloc(sizeof *tmpnc, M_TEMP,
378 						       M_WAITOK);
379 					*tmpnc = *nc;
380 					*nc = *sc;
381 					nc->sc_if = tmpnc->sc_if;
382 					tmpnc->sc_if = sc->sc_if;
383 					*sc = *tmpnc;
384 					kfree(tmpnc, M_TEMP);
385 					if (sc->sc_if.if_flags & IFF_UP) {
386 						if_down(&sc->sc_if);
387 						if (!(nc->sc_if.if_flags & IFF_UP))
388 							if_up(&nc->sc_if);
389 					} else if (nc->sc_if.if_flags & IFF_UP)
390 						if_down(&nc->sc_if);
391 					sc->sc_flags &= ~SC_STATIC;
392 					sc->sc_flags |= (nc->sc_flags & SC_STATIC);
393 					tp->t_sc = sc = nc;
394 					clist_alloc_cblocks(&tp->t_outq,
395 					    SLIP_HIWAT + 2 * sc->sc_if.if_mtu + 1,
396 					    SLIP_HIWAT + 2 * sc->sc_if.if_mtu + 1);
397 					sl_compress_init(&sc->sc_comp, -1);
398 					goto slfound;
399 				}
400 			}
401 			lwkt_reltoken(&tty_token);
402 			crit_exit();
403 			return (ENXIO);
404 		}
405 	slfound:
406 		sc->sc_flags |= SC_STATIC;
407 		break;
408 
409 	case SLIOCSKEEPAL:
410 		sc->sc_keepalive = *(u_int *)data * hz;
411 		if (sc->sc_keepalive) {
412 			sc->sc_flags |= SC_KEEPALIVE;
413 			callout_reset(&sc->sc_katimeout, sc->sc_keepalive,
414 					sl_keepalive, sc);
415 		} else {
416 			if ((sc->sc_flags & SC_KEEPALIVE) != 0) {
417 				callout_stop(&sc->sc_katimeout);
418 				sc->sc_flags &= ~SC_KEEPALIVE;
419 			}
420 		}
421 		break;
422 
423 	case SLIOCGKEEPAL:
424 		*(int *)data = sc->sc_keepalive / hz;
425 		break;
426 
427 	case SLIOCSOUTFILL:
428 		sc->sc_outfill = *(u_int *)data * hz;
429 		if (sc->sc_outfill) {
430 			sc->sc_flags |= SC_OUTWAIT;
431 			callout_reset(&sc->sc_oftimeout, sc->sc_outfill,
432 					sl_outfill, sc);
433 		} else {
434 			if ((sc->sc_flags & SC_OUTWAIT) != 0) {
435 				callout_stop(&sc->sc_oftimeout);
436 				sc->sc_flags &= ~SC_OUTWAIT;
437 			}
438 		}
439 		break;
440 
441 	case SLIOCGOUTFILL:
442 		*(int *)data = sc->sc_outfill / hz;
443 		break;
444 
445 	default:
446 		lwkt_reltoken(&tty_token);
447 		crit_exit();
448 		return (ENOIOCTL);
449 	}
450 	lwkt_reltoken(&tty_token);
451 	crit_exit();
452 	return (0);
453 }
454 
455 /*
456  * Queue a packet.  Start transmission if not active.
457  * Compression happens in slstart; if we do it here, IP TOS
458  * will cause us to not compress "background" packets, because
459  * ordering gets trashed.  It can be done for all packets in slstart.
460  */
461 static int
462 sloutput_serialized(struct ifnet *ifp, struct ifaltq_subque *ifsq,
463     struct mbuf *m, struct sockaddr *dst, struct rtentry *rtp)
464 {
465 	struct sl_softc *sc = &sl_softc[ifp->if_dunit];
466 	struct ip *ip;
467 	int error;
468 	struct altq_pktattr pktattr;
469 
470 	ifq_classify(&ifp->if_snd, m, dst->sa_family, &pktattr);
471 
472 	/*
473 	 * `Cannot happen' (see slioctl).  Someday we will extend
474 	 * the line protocol to support other address families.
475 	 */
476 	if (dst->sa_family != AF_INET) {
477 		kprintf("%s: af%d not supported\n", sc->sc_if.if_xname,
478 			dst->sa_family);
479 		m_freem(m);
480 		IFNET_STAT_INC(&sc->sc_if, noproto, 1);
481 		return (EAFNOSUPPORT);
482 	}
483 
484 	if (sc->sc_ttyp == NULL || !(ifp->if_flags & IFF_UP)) {
485 		m_freem(m);
486 		return (ENETDOWN);
487 	}
488 	if ((sc->sc_ttyp->t_state & TS_CONNECTED) == 0) {
489 		m_freem(m);
490 		return (EHOSTUNREACH);
491 	}
492 	ip = mtod(m, struct ip *);
493 	if (sc->sc_if.if_flags & SC_NOICMP && ip->ip_p == IPPROTO_ICMP) {
494 		m_freem(m);
495 		return (ENETRESET);		/* XXX ? */
496 	}
497 
498 	crit_enter();
499 
500 	if ((ip->ip_tos & IPTOS_LOWDELAY) && !ifq_is_enabled(&sc->sc_if.if_snd)) {
501 		if (IF_QFULL(&sc->sc_fastq)) {
502 			IF_DROP(&sc->sc_fastq);
503 			m_freem(m);
504 			error = ENOBUFS;
505 		} else {
506 			IF_ENQUEUE(&sc->sc_fastq, m);
507 			error = 0;
508 		}
509 	} else {
510 		error = ifsq_enqueue(ifsq, m, &pktattr);
511 	}
512 	if (error) {
513 		IFNET_STAT_INC(&sc->sc_if, oerrors, 1);
514 		crit_exit();
515 		return (error);
516 	}
517 	if (sc->sc_ttyp->t_outq.c_cc == 0)
518 		slstart(sc->sc_ttyp);
519 	crit_exit();
520 	return (0);
521 }
522 
523 static int
524 sloutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
525 	 struct rtentry *rtp)
526 {
527 	struct ifaltq_subque *ifsq = ifq_get_subq_default(&ifp->if_snd);
528 	int error;
529 
530 	ifsq_serialize_hw(ifsq);
531 	error = sloutput_serialized(ifp, ifsq, m, dst, rtp);
532 	ifsq_deserialize_hw(ifsq);
533 
534 	return error;
535 }
536 
537 /*
538  * Start output on interface.  Get another datagram
539  * to send from the interface queue and map it to
540  * the interface before starting output.
541  */
542 static int
543 slstart(struct tty *tp)
544 {
545 	struct sl_softc *sc = (struct sl_softc *)tp->t_sc;
546 	struct ifaltq_subque *ifsq = ifq_get_subq_default(&sc->sc_if.if_snd);
547 	struct mbuf *m;
548 	u_char *cp;
549 	struct ip *ip;
550 	u_char bpfbuf[SLTMAX + SLIP_HDRLEN];
551 	int len = 0;
552 
553 	lwkt_gettoken(&tty_token);
554 	for (;;) {
555 		/*
556 		 * Call output process whether or not there is more in the
557 		 * output queue.  We are being called in lieu of ttstart
558 		 * and must do what it would.
559 		 */
560 		(*tp->t_oproc)(tp);
561 
562 		if (tp->t_outq.c_cc != 0) {
563 			if (sc != NULL)
564 				sc->sc_flags &= ~SC_OUTWAIT;
565 			if (tp->t_outq.c_cc > SLIP_HIWAT) {
566 				lwkt_reltoken(&tty_token);
567 				return 0;
568 			}
569 		}
570 
571 		/*
572 		 * This happens briefly when the line shuts down.
573 		 */
574 		if (sc == NULL) {
575 			lwkt_reltoken(&tty_token);
576 			return 0;
577 		}
578 
579 		/*
580 		 * Get a packet and send it to the interface.
581 		 */
582 		crit_enter();
583 		IF_DEQUEUE(&sc->sc_fastq, m);
584 		if (m)
585 			IFNET_STAT_INC(&sc->sc_if, omcasts, 1);	/* XXX */
586 		else
587 			m = ifsq_dequeue(ifsq, NULL);
588 		crit_exit();
589 		if (m == NULL) {
590 			lwkt_reltoken(&tty_token);
591 			return 0;
592 		}
593 
594 		/*
595 		 * We do the header compression here rather than in sloutput
596 		 * because the packets will be out of order if we are using TOS
597 		 * queueing, and the connection id compression will get
598 		 * munged when this happens.
599 		 */
600 		if (sc->sc_if.if_bpf) {
601 			/*
602 			 * We need to save the TCP/IP header before it's
603 			 * compressed.  To avoid complicated code, we just
604 			 * copy the entire packet into a stack buffer (since
605 			 * this is a serial line, packets should be short
606 			 * and/or the copy should be negligible cost compared
607 			 * to the packet transmission time).
608 			 */
609 			struct mbuf *m1 = m;
610 			u_char *cp = bpfbuf + SLIP_HDRLEN;
611 
612 			len = 0;
613 			do {
614 				int mlen = m1->m_len;
615 
616 				bcopy(mtod(m1, caddr_t), cp, mlen);
617 				cp += mlen;
618 				len += mlen;
619 			} while ((m1 = m1->m_next) != NULL);
620 		}
621 		ip = mtod(m, struct ip *);
622 		if (ip->ip_v == IPVERSION && ip->ip_p == IPPROTO_TCP) {
623 			if (sc->sc_if.if_flags & SC_COMPRESS)
624 				*mtod(m, u_char *) |= sl_compress_tcp(m, ip,
625 				    &sc->sc_comp, 1);
626 		}
627 		if (sc->sc_if.if_bpf) {
628 			bpf_gettoken();
629 
630 			if (sc->sc_if.if_bpf) {
631 				/*
632 				 * Put the SLIP pseudo-"link header" in place.
633 				 * The compressed header is now at the
634 				 * beginning of the mbuf.
635 				 */
636 				bpfbuf[SLX_DIR] = SLIPDIR_OUT;
637 				bcopy(mtod(m, caddr_t), &bpfbuf[SLX_CHDR],
638 				    CHDR_LEN);
639 				bpf_tap(sc->sc_if.if_bpf, bpfbuf,
640 				    len + SLIP_HDRLEN);
641 			}
642 
643 			bpf_reltoken();
644 		}
645 
646 		/*
647 		 * If system is getting low on clists, just flush our
648 		 * output queue (if the stuff was important, it'll get
649 		 * retransmitted). Note that SLTMAX is used instead of
650 		 * the current if_mtu setting because connections that
651 		 * have already been established still use the original
652 		 * (possibly larger) mss.
653 		 */
654 		if (cfreecount < CLISTRESERVE + SLTMAX) {
655 			m_freem(m);
656 			IFNET_STAT_INC(&sc->sc_if, collisions, 1);
657 			continue;
658 		}
659 
660 		sc->sc_flags &= ~SC_OUTWAIT;
661 		/*
662 		 * The extra FRAME_END will start up a new packet, and thus
663 		 * will flush any accumulated garbage.  We do this whenever
664 		 * the line may have been idle for some time.
665 		 */
666 		if (tp->t_outq.c_cc == 0) {
667 			IFNET_STAT_INC(&sc->sc_if, obytes, 1);
668 			clist_putc(FRAME_END, &tp->t_outq);
669 		}
670 
671 		while (m) {
672 			u_char *ep;
673 
674 			cp = mtod(m, u_char *); ep = cp + m->m_len;
675 			while (cp < ep) {
676 				/*
677 				 * Find out how many bytes in the string we can
678 				 * handle without doing something special.
679 				 */
680 				u_char *bp = cp;
681 
682 				while (cp < ep) {
683 					switch (*cp++) {
684 					case FRAME_ESCAPE:
685 					case FRAME_END:
686 						--cp;
687 						goto out;
688 					}
689 				}
690 				out:
691 				if (cp > bp) {
692 					/*
693 					 * Put n characters at once
694 					 * into the tty output queue.
695 					 */
696 					if (b_to_q((char *)bp, cp - bp,
697 					    &tp->t_outq))
698 						break;
699 					IFNET_STAT_INC(&sc->sc_if, obytes,
700 					    cp - bp);
701 				}
702 				/*
703 				 * If there are characters left in the mbuf,
704 				 * the first one must be special..
705 				 * Put it out in a different form.
706 				 */
707 				if (cp < ep) {
708 					if (clist_putc(FRAME_ESCAPE, &tp->t_outq))
709 						break;
710 					if (clist_putc(*cp++ == FRAME_ESCAPE ?
711 					   TRANS_FRAME_ESCAPE : TRANS_FRAME_END,
712 					   &tp->t_outq)) {
713 						clist_unputc(&tp->t_outq);
714 						break;
715 					}
716 					IFNET_STAT_INC(&sc->sc_if, obytes, 2);
717 				}
718 			}
719 			m = m_free(m);
720 		}
721 
722 		if (clist_putc(FRAME_END, &tp->t_outq)) {
723 			/*
724 			 * Not enough room.  Remove a char to make room
725 			 * and end the packet normally.
726 			 * If you get many collisions (more than one or two
727 			 * a day) you probably do not have enough clists
728 			 * and you should increase "nclist" in param.c.
729 			 */
730 			clist_unputc(&tp->t_outq);
731 			clist_putc(FRAME_END, &tp->t_outq);
732 			IFNET_STAT_INC(&sc->sc_if, collisions, 1);
733 		} else {
734 			IFNET_STAT_INC(&sc->sc_if, obytes, 1);
735 			IFNET_STAT_INC(&sc->sc_if, opackets, 1);
736 		}
737 	}
738 	lwkt_reltoken(&tty_token);
739 	return 0;
740 }
741 
742 /*
743  * Copy data buffer to mbuf chain; add ifnet pointer.
744  */
745 static struct mbuf *
746 sl_btom(struct sl_softc *sc, int len)
747 {
748 	struct mbuf *m;
749 
750 	if (len >= MCLBYTES)
751 		return (NULL);
752 
753 	MGETHDR(m, MB_DONTWAIT, MT_DATA);
754 	if (m == NULL)
755 		return (NULL);
756 
757 	/*
758 	 * If we have more than MHLEN bytes, it's cheaper to
759 	 * queue the cluster we just filled & allocate a new one
760 	 * for the input buffer.  Otherwise, fill the mbuf we
761 	 * allocated above.  Note that code in the input routine
762 	 * guarantees that packet will fit in a cluster.
763 	 */
764 	if (len >= MHLEN) {
765 		MCLGET(m, MB_DONTWAIT);
766 		if ((m->m_flags & M_EXT) == 0) {
767 			/*
768 			 * we couldn't get a cluster - if memory's this
769 			 * low, it's time to start dropping packets.
770 			 */
771 			m_free(m);
772 			return (NULL);
773 		}
774 	}
775 	bcopy((caddr_t)sc->sc_buf, mtod(m, caddr_t), len);
776 	m->m_len = len;
777 	m->m_pkthdr.len = len;
778 	m->m_pkthdr.rcvif = &sc->sc_if;
779 	return (m);
780 }
781 
782 /*
783  * tty interface receiver interrupt.
784  */
785 static int
786 slinput(int c, struct tty *tp)
787 {
788 	struct sl_softc *sc;
789 	struct mbuf *m;
790 	int len;
791 	u_char chdr[CHDR_LEN];
792 
793 	lwkt_gettoken(&tty_token);
794 	tk_nin++;
795 	sc = (struct sl_softc *)tp->t_sc;
796 	if (sc == NULL) {
797 		lwkt_reltoken(&tty_token);
798 		return 0;
799 	}
800 	if (c & TTY_ERRORMASK || (tp->t_state & TS_CONNECTED) == 0) {
801 		sc->sc_flags |= SC_ERROR;
802 		lwkt_reltoken(&tty_token);
803 		return 0;
804 	}
805 	c &= TTY_CHARMASK;
806 
807 	IFNET_STAT_INC(&sc->sc_if, ibytes, 1);
808 
809 	if (sc->sc_if.if_flags & IFF_DEBUG) {
810 		if (c == ABT_ESC) {
811 			/*
812 			 * If we have a previous abort, see whether
813 			 * this one is within the time limit.
814 			 */
815 			if (sc->sc_abortcount &&
816 			    time_second >= sc->sc_starttime + ABT_WINDOW)
817 				sc->sc_abortcount = 0;
818 			/*
819 			 * If we see an abort after "idle" time, count it;
820 			 * record when the first abort escape arrived.
821 			 */
822 			if (time_second >= sc->sc_lasttime + ABT_IDLE) {
823 				if (++sc->sc_abortcount == 1)
824 					sc->sc_starttime = time_second;
825 				if (sc->sc_abortcount >= ABT_COUNT) {
826 					slclose(tp,0);
827 					lwkt_reltoken(&tty_token);
828 					return 0;
829 				}
830 			}
831 		} else
832 			sc->sc_abortcount = 0;
833 		sc->sc_lasttime = time_second;
834 	}
835 
836 	switch (c) {
837 
838 	case TRANS_FRAME_ESCAPE:
839 		if (sc->sc_escape)
840 			c = FRAME_ESCAPE;
841 		break;
842 
843 	case TRANS_FRAME_END:
844 		if (sc->sc_escape)
845 			c = FRAME_END;
846 		break;
847 
848 	case FRAME_ESCAPE:
849 		sc->sc_escape = 1;
850 		lwkt_reltoken(&tty_token);
851 		return 0;
852 
853 	case FRAME_END:
854 		sc->sc_flags &= ~SC_KEEPALIVE;
855 		if(sc->sc_flags & SC_ERROR) {
856 			sc->sc_flags &= ~SC_ERROR;
857 			goto newpack;
858 		}
859 		len = sc->sc_mp - sc->sc_buf;
860 		if (len < 3)
861 			/* less than min length packet - ignore */
862 			goto newpack;
863 
864 		if (sc->sc_if.if_bpf) {
865 			/*
866 			 * Save the compressed header, so we
867 			 * can tack it on later.  Note that we
868 			 * will end up copying garbage in some
869 			 * cases but this is okay.  We remember
870 			 * where the buffer started so we can
871 			 * compute the new header length.
872 			 */
873 			bcopy(sc->sc_buf, chdr, CHDR_LEN);
874 		}
875 
876 		if ((c = (*sc->sc_buf & 0xf0)) != (IPVERSION << 4)) {
877 			if (c & 0x80)
878 				c = TYPE_COMPRESSED_TCP;
879 			else if (c == TYPE_UNCOMPRESSED_TCP)
880 				*sc->sc_buf &= 0x4f; /* XXX */
881 			/*
882 			 * We've got something that's not an IP packet.
883 			 * If compression is enabled, try to decompress it.
884 			 * Otherwise, if `auto-enable' compression is on and
885 			 * it's a reasonable packet, decompress it and then
886 			 * enable compression.  Otherwise, drop it.
887 			 */
888 			if (sc->sc_if.if_flags & SC_COMPRESS) {
889 				len = sl_uncompress_tcp(&sc->sc_buf, len,
890 							(u_int)c, &sc->sc_comp);
891 				if (len <= 0)
892 					goto error;
893 			} else if ((sc->sc_if.if_flags & SC_AUTOCOMP) &&
894 			    c == TYPE_UNCOMPRESSED_TCP && len >= 40) {
895 				len = sl_uncompress_tcp(&sc->sc_buf, len,
896 							(u_int)c, &sc->sc_comp);
897 				if (len <= 0)
898 					goto error;
899 				sc->sc_if.if_flags |= SC_COMPRESS;
900 			} else
901 				goto error;
902 		}
903 		if (sc->sc_if.if_bpf) {
904 			bpf_gettoken();
905 
906 			if (sc->sc_if.if_bpf) {
907 				/*
908 				 * Put the SLIP pseudo-"link header" in place.
909 				 * We couldn't do this any earlier since
910 				 * decompression probably moved the buffer
911 				 * pointer.  Then, invoke BPF.
912 				 */
913 				u_char *hp = sc->sc_buf - SLIP_HDRLEN;
914 
915 				hp[SLX_DIR] = SLIPDIR_IN;
916 				bcopy(chdr, &hp[SLX_CHDR], CHDR_LEN);
917 				bpf_tap(sc->sc_if.if_bpf, hp,
918 				    len + SLIP_HDRLEN);
919 			}
920 
921 			bpf_reltoken();
922 		}
923 		m = sl_btom(sc, len);
924 		if (m == NULL)
925 			goto error;
926 
927 		IFNET_STAT_INC(&sc->sc_if, ipackets, 1);
928 
929 		if ((sc->sc_if.if_flags & IFF_UP) == 0) {
930 			m_freem(m);
931 			goto newpack;
932 		}
933 
934 		if (netisr_queue(NETISR_IP, m)) {
935 			IFNET_STAT_INC(&sc->sc_if, ierrors, 1);
936 			IFNET_STAT_INC(&sc->sc_if, iqdrops, 1);
937 		}
938 
939 		goto newpack;
940 	}
941 	if (sc->sc_mp < sc->sc_ep + SLBUFSIZE) {
942 		*sc->sc_mp++ = c;
943 		sc->sc_escape = 0;
944 		lwkt_reltoken(&tty_token);
945 		return 0;
946 	}
947 
948 	/* can't put lower; would miss an extra frame */
949 	sc->sc_flags |= SC_ERROR;
950 
951 error:
952 	IFNET_STAT_INC(&sc->sc_if, ierrors, 1);
953 newpack:
954 	sc->sc_mp = sc->sc_buf = sc->sc_ep + SLBUFSIZE - SLRMAX;
955 	sc->sc_escape = 0;
956 	lwkt_reltoken(&tty_token);
957 	return 0;
958 }
959 
960 /*
961  * Process an ioctl request.
962  */
963 static int
964 slioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
965 {
966 	struct ifaddr *ifa = (struct ifaddr *)data;
967 	struct ifreq *ifr = (struct ifreq *)data;
968 	int error = 0;
969 
970 	crit_enter();
971 
972 	switch (cmd) {
973 
974 	case SIOCSIFFLAGS:
975 		/*
976 		 * if.c will set the interface up even if we
977 		 * don't want it to.
978 		 */
979 		if (sl_softc[ifp->if_dunit].sc_ttyp == NULL) {
980 			ifp->if_flags &= ~IFF_UP;
981 		}
982 		break;
983 	case SIOCSIFADDR:
984 		/*
985 		 * This is "historical" - set the interface up when
986 		 * setting the address.
987 		 */
988 		if (ifa->ifa_addr->sa_family == AF_INET) {
989 			if (sl_softc[ifp->if_dunit].sc_ttyp != NULL)
990 				ifp->if_flags |= IFF_UP;
991 		} else {
992 			error = EAFNOSUPPORT;
993 		}
994 		break;
995 
996 	case SIOCSIFDSTADDR:
997 		if (ifa->ifa_addr->sa_family != AF_INET)
998 			error = EAFNOSUPPORT;
999 		break;
1000 
1001 	case SIOCADDMULTI:
1002 	case SIOCDELMULTI:
1003 		break;
1004 
1005 	case SIOCSIFMTU:
1006 		/*
1007 		 * Set the interface MTU.
1008 		 */
1009 		if (ifr->ifr_mtu > SLTMAX)
1010 			error = EINVAL;
1011 		else {
1012 			struct tty *tp;
1013 
1014 			ifp->if_mtu = ifr->ifr_mtu;
1015 			tp = sl_softc[ifp->if_dunit].sc_ttyp;
1016 			if (tp != NULL)
1017 				clist_alloc_cblocks(&tp->t_outq,
1018 				    SLIP_HIWAT + 2 * ifp->if_mtu + 1,
1019 				    SLIP_HIWAT + 2 * ifp->if_mtu + 1);
1020 		}
1021 		break;
1022 
1023 	default:
1024 		error = EINVAL;
1025 	}
1026 
1027 	crit_exit();
1028 	return (error);
1029 }
1030 
1031 static void
1032 sl_keepalive(void *chan)
1033 {
1034 	struct sl_softc *sc = chan;
1035 
1036 	lwkt_gettoken(&tty_token);
1037 	lwkt_gettoken(&proc_token);
1038 	if (sc->sc_keepalive) {
1039 		if (sc->sc_flags & SC_KEEPALIVE)
1040 			pgsignal (sc->sc_ttyp->t_pgrp, SIGURG, 1);
1041 		else
1042 			sc->sc_flags |= SC_KEEPALIVE;
1043 		callout_reset(&sc->sc_katimeout, sc->sc_keepalive,
1044 				sl_keepalive, sc);
1045 	} else {
1046 		sc->sc_flags &= ~SC_KEEPALIVE;
1047 	}
1048 	lwkt_reltoken(&proc_token);
1049 	lwkt_reltoken(&tty_token);
1050 }
1051 
1052 static void
1053 sl_outfill(void *chan)
1054 {
1055 	struct sl_softc *sc = chan;
1056 	struct tty *tp = sc->sc_ttyp;
1057 
1058 	lwkt_gettoken(&tty_token);
1059 
1060 	if (sc->sc_outfill && tp != NULL) {
1061 		if (sc->sc_flags & SC_OUTWAIT) {
1062 			crit_enter();
1063 			IFNET_STAT_INC(&sc->sc_if, obytes, 1);
1064 			clist_putc(FRAME_END, &tp->t_outq);
1065 			(*tp->t_oproc)(tp);
1066 			crit_exit();
1067 		} else
1068 			sc->sc_flags |= SC_OUTWAIT;
1069 		callout_reset(&sc->sc_oftimeout, sc->sc_outfill,
1070 				sl_outfill, sc);
1071 	} else {
1072 		sc->sc_flags &= ~SC_OUTWAIT;
1073 	}
1074 	lwkt_reltoken(&tty_token);
1075 }
1076