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