Home | History | Annotate | Line # | Download | only in net
if_sl.c revision 1.107
      1 /*	$NetBSD: if_sl.c,v 1.107 2007/09/01 04:32:50 dyoung 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 (at) seismo.ARPA
     43  * seismo!rick
     44  *
     45  * Pounded on heavily by Chris Torek (chris (at) 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 (at) 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.107 2007/09/01 04:32:50 dyoung 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 #if __NetBSD__
     81 #include <sys/systm.h>
     82 #include <sys/kauth.h>
     83 #endif
     84 
     85 #include <machine/cpu.h>
     86 #include <machine/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 	MALLOC(sc, struct sl_softc *, sizeof(*sc), M_DEVBUF, M_WAIT|M_ZERO);
    235 	sc->sc_unit = unit;
    236 	(void)snprintf(sc->sc_if.if_xname, sizeof(sc->sc_if.if_xname),
    237 	    "%s%d", ifc->ifc_name, unit);
    238 	sc->sc_if.if_softc = sc;
    239 	sc->sc_if.if_mtu = SLMTU;
    240 	sc->sc_if.if_flags = IFF_POINTOPOINT | SC_AUTOCOMP | IFF_MULTICAST;
    241 	sc->sc_if.if_type = IFT_SLIP;
    242 	sc->sc_if.if_ioctl = slioctl;
    243 	sc->sc_if.if_output = sloutput;
    244 	sc->sc_if.if_dlt = DLT_SLIP;
    245 	sc->sc_fastq.ifq_maxlen = 32;
    246 	IFQ_SET_READY(&sc->sc_if.if_snd);
    247 	if_attach(&sc->sc_if);
    248 	if_alloc_sadl(&sc->sc_if);
    249 #if NBPFILTER > 0
    250 	bpfattach(&sc->sc_if, DLT_SLIP, SLIP_HDRLEN);
    251 #endif
    252 	LIST_INSERT_HEAD(&sl_softc_list, sc, sc_iflist);
    253 	return 0;
    254 }
    255 
    256 static int
    257 sl_clone_destroy(struct ifnet *ifp)
    258 {
    259 	struct sl_softc *sc = (struct sl_softc *)ifp->if_softc;
    260 
    261 	if (sc->sc_ttyp != NULL)
    262 		return EBUSY;	/* Not removing it */
    263 
    264 	LIST_REMOVE(sc, sc_iflist);
    265 
    266 #if NBPFILTER > 0
    267 	bpfdetach(ifp);
    268 #endif
    269 	if_detach(ifp);
    270 
    271 	FREE(sc, M_DEVBUF);
    272 	return 0;
    273 }
    274 
    275 static int
    276 slinit(struct sl_softc *sc)
    277 {
    278 
    279 	if (sc->sc_mbuf == NULL) {
    280 		sc->sc_mbuf = m_gethdr(M_WAIT, MT_DATA);
    281 		m_clget(sc->sc_mbuf, M_WAIT);
    282 	}
    283 	sc->sc_ep = (u_char *)sc->sc_mbuf->m_ext.ext_buf +
    284 	    sc->sc_mbuf->m_ext.ext_size;
    285 	sc->sc_mp = sc->sc_pktstart = (u_char *)sc->sc_mbuf->m_ext.ext_buf +
    286 	    BUFOFFSET;
    287 
    288 #ifdef INET
    289 	sl_compress_init(&sc->sc_comp);
    290 #endif
    291 
    292 	return 1;
    293 }
    294 
    295 /*
    296  * Line specific open routine.
    297  * Attach the given tty to the first available sl unit.
    298  */
    299 /* ARGSUSED */
    300 static int
    301 slopen(dev_t dev, struct tty *tp)
    302 {
    303 	struct lwp *l = curlwp;		/* XXX */
    304 	struct sl_softc *sc;
    305 	int error;
    306 	int s;
    307 
    308 	if ((error = kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER,
    309 	    NULL)) != 0)
    310 		return error;
    311 
    312 	if (tp->t_linesw == &slip_disc)
    313 		return 0;
    314 
    315 	LIST_FOREACH(sc, &sl_softc_list, sc_iflist)
    316 		if (sc->sc_ttyp == NULL) {
    317 			sc->sc_si = softintr_establish(IPL_SOFTNET,
    318 			    slintr, sc);
    319 			if (sc->sc_si == NULL)
    320 				return ENOMEM;
    321 			if (slinit(sc) == 0) {
    322 				softintr_disestablish(sc->sc_si);
    323 				return ENOBUFS;
    324 			}
    325 			tp->t_sc = (void *)sc;
    326 			sc->sc_ttyp = tp;
    327 			sc->sc_if.if_baudrate = tp->t_ospeed;
    328 			s = spltty();
    329 			tp->t_state |= TS_ISOPEN | TS_XCLUDE;
    330 			splx(s);
    331 			ttyflush(tp, FREAD | FWRITE);
    332 #ifdef __NetBSD__
    333 			/*
    334 			 * make sure tty output queue is large enough
    335 			 * to hold a full-sized packet (including frame
    336 			 * end, and a possible extra frame end).  full-sized
    337 			 * packet occupies a max of 2*SLMAX bytes (because
    338 			 * of possible escapes), and add two on for frame
    339 			 * ends.
    340 			 */
    341 			s = spltty();
    342 			if (tp->t_outq.c_cn < 2 * SLMAX + 2) {
    343 				sc->sc_oldbufsize = tp->t_outq.c_cn;
    344 				sc->sc_oldbufquot = tp->t_outq.c_cq != 0;
    345 
    346 				clfree(&tp->t_outq);
    347 				error = clalloc(&tp->t_outq, 2 * SLMAX + 2, 0);
    348 				if (error) {
    349 					splx(s);
    350 					softintr_disestablish(sc->sc_si);
    351 					/*
    352 					 * clalloc() might return -1 which
    353 					 * is no good, so we need to return
    354 					 * something else.
    355 					 */
    356 					return ENOMEM; /* XXX ?! */
    357 				}
    358 			} else
    359 				sc->sc_oldbufsize = sc->sc_oldbufquot = 0;
    360 			splx(s);
    361 #endif /* __NetBSD__ */
    362 			return 0;
    363 		}
    364 	return ENXIO;
    365 }
    366 
    367 /*
    368  * Line specific close routine.
    369  * Detach the tty from the sl unit.
    370  */
    371 static int
    372 slclose(struct tty *tp, int flag)
    373 {
    374 	struct sl_softc *sc;
    375 	int s;
    376 
    377 	ttywflush(tp);
    378 	sc = tp->t_sc;
    379 
    380 	if (sc != NULL) {
    381 		softintr_disestablish(sc->sc_si);
    382 		s = splnet();
    383 		if_down(&sc->sc_if);
    384 		IF_PURGE(&sc->sc_fastq);
    385 		splx(s);
    386 
    387 		s = spltty();
    388 		ttyldisc_release(tp->t_linesw);
    389 		tp->t_linesw = ttyldisc_default();
    390 		tp->t_state = 0;
    391 
    392 		sc->sc_ttyp = NULL;
    393 		tp->t_sc = NULL;
    394 
    395 		m_freem(sc->sc_mbuf);
    396 		sc->sc_mbuf = NULL;
    397 		sc->sc_ep = sc->sc_mp = sc->sc_pktstart = NULL;
    398 		IF_PURGE(&sc->sc_inq);
    399 
    400 		/*
    401 		 * If necessary, install a new outq buffer of the
    402 		 * appropriate size.
    403 		 */
    404 		if (sc->sc_oldbufsize != 0) {
    405 			clfree(&tp->t_outq);
    406 			clalloc(&tp->t_outq, sc->sc_oldbufsize,
    407 			    sc->sc_oldbufquot);
    408 		}
    409 		splx(s);
    410 	}
    411 
    412 	return 0;
    413 }
    414 
    415 /*
    416  * Line specific (tty) ioctl routine.
    417  * Provide a way to get the sl unit number.
    418  */
    419 /* ARGSUSED */
    420 static int
    421 sltioctl(struct tty *tp, u_long cmd, void *data, int flag,
    422     struct lwp *l)
    423 {
    424 	struct sl_softc *sc = (struct sl_softc *)tp->t_sc;
    425 
    426 	switch (cmd) {
    427 	case SLIOCGUNIT:
    428 		*(int *)data = sc->sc_unit;	/* XXX */
    429 		break;
    430 
    431 	default:
    432 		return EPASSTHROUGH;
    433 	}
    434 	return 0;
    435 }
    436 
    437 /*
    438  * Queue a packet.  Start transmission if not active.
    439  * Compression happens in slintr(); if we do it here, IP TOS
    440  * will cause us to not compress "background" packets, because
    441  * ordering gets trashed.  It can be done for all packets in slintr().
    442  */
    443 static int
    444 sloutput(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
    445     struct rtentry *rtp)
    446 {
    447 	struct sl_softc *sc = ifp->if_softc;
    448 	struct ip *ip;
    449 	struct ifqueue *ifq = NULL;
    450 	int s, error;
    451 	ALTQ_DECL(struct altq_pktattr pktattr;)
    452 
    453 	IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family, &pktattr);
    454 
    455 	/*
    456 	 * `Cannot happen' (see slioctl).  Someday we will extend
    457 	 * the line protocol to support other address families.
    458 	 */
    459 	if (dst->sa_family != AF_INET) {
    460 		printf("%s: af%d not supported\n", sc->sc_if.if_xname,
    461 		    dst->sa_family);
    462 		m_freem(m);
    463 		sc->sc_if.if_noproto++;
    464 		return EAFNOSUPPORT;
    465 	}
    466 
    467 	if (sc->sc_ttyp == NULL) {
    468 		m_freem(m);
    469 		return ENETDOWN;	/* sort of */
    470 	}
    471 	if ((sc->sc_ttyp->t_state & TS_CARR_ON) == 0 &&
    472 	    (sc->sc_ttyp->t_cflag & CLOCAL) == 0) {
    473 		m_freem(m);
    474 		printf("%s: no carrier and not local\n", sc->sc_if.if_xname);
    475 		return EHOSTUNREACH;
    476 	}
    477 	ip = mtod(m, struct ip *);
    478 #ifdef INET
    479 	if (sc->sc_if.if_flags & SC_NOICMP && ip->ip_p == IPPROTO_ICMP) {
    480 		m_freem(m);
    481 		return ENETRESET;		/* XXX ? */
    482 	}
    483 #endif
    484 
    485 	s = spltty();
    486 	if (sc->sc_oqlen && sc->sc_ttyp->t_outq.c_cc == sc->sc_oqlen) {
    487 		struct bintime bt;
    488 
    489 		/* if output's been stalled for too long, and restart */
    490 		getbinuptime(&bt);
    491 		bintime_sub(&bt, &sc->sc_lastpacket);
    492 		if (bt.sec > 0) {
    493 			sc->sc_otimeout++;
    494 			slstart(sc->sc_ttyp);
    495 		}
    496 	}
    497 	splx(s);
    498 
    499 	s = splnet();
    500 #ifdef INET
    501 	if ((ip->ip_tos & IPTOS_LOWDELAY) != 0)
    502 		ifq = &sc->sc_fastq;
    503 #endif
    504 	if ((error = ifq_enqueue2(ifp, ifq, m ALTQ_COMMA
    505 	    ALTQ_DECL(&pktattr))) != 0) {
    506 		splx(s);
    507 		return error;
    508 	}
    509 	getbinuptime(&sc->sc_lastpacket);
    510 	splx(s);
    511 
    512 	s = spltty();
    513 	if ((sc->sc_oqlen = sc->sc_ttyp->t_outq.c_cc) == 0)
    514 		slstart(sc->sc_ttyp);
    515 	splx(s);
    516 
    517 	return 0;
    518 }
    519 
    520 /*
    521  * Start output on interface.  Get another datagram
    522  * to send from the interface queue and map it to
    523  * the interface before starting output.
    524  */
    525 static int
    526 slstart(struct tty *tp)
    527 {
    528 	struct sl_softc *sc = tp->t_sc;
    529 
    530 	/*
    531 	 * If there is more in the output queue, just send it now.
    532 	 * We are being called in lieu of ttstart and must do what
    533 	 * it would.
    534 	 */
    535 	if (tp->t_outq.c_cc != 0) {
    536 		(*tp->t_oproc)(tp);
    537 		if (tp->t_outq.c_cc > SLIP_HIWAT)
    538 			return 0;
    539 	}
    540 
    541 	/*
    542 	 * This happens briefly when the line shuts down.
    543 	 */
    544 	if (sc == NULL)
    545 		return 0;
    546 	softintr_schedule(sc->sc_si);
    547 	return 0;
    548 }
    549 
    550 /*
    551  * Copy data buffer to mbuf chain; add ifnet pointer.
    552  */
    553 static struct mbuf *
    554 sl_btom(struct sl_softc *sc, int len)
    555 {
    556 	struct mbuf *m;
    557 
    558 	/*
    559 	 * Allocate a new input buffer and swap.
    560 	 */
    561 	m = sc->sc_mbuf;
    562 	MGETHDR(sc->sc_mbuf, M_DONTWAIT, MT_DATA);
    563 	if (sc->sc_mbuf == NULL) {
    564 		sc->sc_mbuf = m;
    565 		return NULL;
    566 	}
    567 	MCLGET(sc->sc_mbuf, M_DONTWAIT);
    568 	if ((sc->sc_mbuf->m_flags & M_EXT) == 0) {
    569 		m_freem(sc->sc_mbuf);
    570 		sc->sc_mbuf = m;
    571 		return NULL;
    572 	}
    573 	sc->sc_ep = (u_char *)sc->sc_mbuf->m_ext.ext_buf +
    574 	    sc->sc_mbuf->m_ext.ext_size;
    575 
    576 	m->m_data = sc->sc_pktstart;
    577 
    578 	m->m_pkthdr.len = m->m_len = len;
    579 	m->m_pkthdr.rcvif = &sc->sc_if;
    580 	return m;
    581 }
    582 
    583 /*
    584  * tty interface receiver interrupt.
    585  */
    586 static int
    587 slinput(int c, struct tty *tp)
    588 {
    589 	struct sl_softc *sc;
    590 	struct mbuf *m;
    591 	int len;
    592 
    593 	tk_nin++;
    594 	sc = (struct sl_softc *)tp->t_sc;
    595 	if (sc == NULL)
    596 		return 0;
    597 	if ((c & TTY_ERRORMASK) || ((tp->t_state & TS_CARR_ON) == 0 &&
    598 	    (tp->t_cflag & CLOCAL) == 0)) {
    599 		sc->sc_flags |= SC_ERROR;
    600 		return 0;
    601 	}
    602 	c &= TTY_CHARMASK;
    603 
    604 	++sc->sc_if.if_ibytes;
    605 
    606 	if (sc->sc_if.if_flags & IFF_DEBUG) {
    607 		if (c == ABT_ESC) {
    608 			/*
    609 			 * If we have a previous abort, see whether
    610 			 * this one is within the time limit.
    611 			 */
    612 			if (sc->sc_abortcount &&
    613 			    time_second >= sc->sc_starttime + ABT_WINDOW)
    614 				sc->sc_abortcount = 0;
    615 			/*
    616 			 * If we see an abort after "idle" time, count it;
    617 			 * record when the first abort escape arrived.
    618 			 */
    619 			if (time_second >= sc->sc_lasttime + ABT_IDLE) {
    620 				if (++sc->sc_abortcount == 1)
    621 					sc->sc_starttime = time_second;
    622 				if (sc->sc_abortcount >= ABT_COUNT) {
    623 					slclose(tp, 0);
    624 					return 0;
    625 				}
    626 			}
    627 		} else
    628 			sc->sc_abortcount = 0;
    629 		sc->sc_lasttime = time_second;
    630 	}
    631 
    632 	switch (c) {
    633 
    634 	case TRANS_FRAME_ESCAPE:
    635 		if (sc->sc_escape)
    636 			c = FRAME_ESCAPE;
    637 		break;
    638 
    639 	case TRANS_FRAME_END:
    640 		if (sc->sc_escape)
    641 			c = FRAME_END;
    642 		break;
    643 
    644 	case FRAME_ESCAPE:
    645 		sc->sc_escape = 1;
    646 		return 0;
    647 
    648 	case FRAME_END:
    649 		if (sc->sc_flags & SC_ERROR) {
    650 			sc->sc_flags &= ~SC_ERROR;
    651 			goto newpack;
    652 		}
    653 		len = sc->sc_mp - sc->sc_pktstart;
    654 		if (len < 3)
    655 			/* less than min length packet - ignore */
    656 			goto newpack;
    657 
    658 		m = sl_btom(sc, len);
    659 		if (m == NULL)
    660 			goto error;
    661 
    662 		IF_ENQUEUE(&sc->sc_inq, m);
    663 		softintr_schedule(sc->sc_si);
    664 		goto newpack;
    665 	}
    666 	if (sc->sc_mp < sc->sc_ep) {
    667 		*sc->sc_mp++ = c;
    668 		sc->sc_escape = 0;
    669 		return 0;
    670 	}
    671 
    672 	/* can't put lower; would miss an extra frame */
    673 	sc->sc_flags |= SC_ERROR;
    674 
    675 error:
    676 	sc->sc_if.if_ierrors++;
    677 newpack:
    678 	sc->sc_mp = sc->sc_pktstart = (u_char *)sc->sc_mbuf->m_ext.ext_buf +
    679 	    BUFOFFSET;
    680 	sc->sc_escape = 0;
    681 
    682 	return 0;
    683 }
    684 
    685 static void
    686 slintr(void *arg)
    687 {
    688 	struct sl_softc *sc = arg;
    689 	struct tty *tp = sc->sc_ttyp;
    690 	struct mbuf *m;
    691 	int s, len;
    692 	u_char *pktstart;
    693 #ifdef INET
    694 	u_char c;
    695 #endif
    696 #if NBPFILTER > 0
    697 	u_char chdr[CHDR_LEN];
    698 #endif
    699 
    700 	KASSERT(tp != NULL);
    701 
    702 	/*
    703 	 * Output processing loop.
    704 	 */
    705 	for (;;) {
    706 #ifdef INET
    707 		struct ip *ip;
    708 #endif
    709 		struct mbuf *m2;
    710 #if NBPFILTER > 0
    711 		struct mbuf *bpf_m;
    712 #endif
    713 
    714 		/*
    715 		 * Do not remove the packet from the queue if it
    716 		 * doesn't look like it will fit into the current
    717 		 * serial output queue.  With a packet full of
    718 		 * escapes, this could be as bad as MTU*2+2.
    719 		 */
    720 		s = spltty();
    721 		if (tp->t_outq.c_cn - tp->t_outq.c_cc <
    722 		    2 * sc->sc_if.if_mtu + 2) {
    723 			splx(s);
    724 			break;
    725 		}
    726 		splx(s);
    727 
    728 		/*
    729 		 * Get a packet and send it to the interface.
    730 		 */
    731 		s = splnet();
    732 		IF_DEQUEUE(&sc->sc_fastq, m);
    733 		if (m)
    734 			sc->sc_if.if_omcasts++;	/* XXX */
    735 		else
    736 			IFQ_DEQUEUE(&sc->sc_if.if_snd, m);
    737 		splx(s);
    738 
    739 		if (m == NULL)
    740 			break;
    741 
    742 		/*
    743 		 * We do the header compression here rather than in
    744 		 * sloutput() because the packets will be out of order
    745 		 * if we are using TOS queueing, and the connection
    746 		 * ID compression will get munged when this happens.
    747 		 */
    748 #if NBPFILTER > 0
    749 		if (sc->sc_if.if_bpf) {
    750 			/*
    751 			 * We need to save the TCP/IP header before
    752 			 * it's compressed.  To avoid complicated
    753 			 * code, we just make a deep copy of the
    754 			 * entire packet (since this is a serial
    755 			 * line, packets should be short and/or the
    756 			 * copy should be negligible cost compared
    757 			 * to the packet transmission time).
    758 			 */
    759 			bpf_m = m_dup(m, 0, M_COPYALL, M_DONTWAIT);
    760 		} else
    761 			bpf_m = NULL;
    762 #endif
    763 #ifdef INET
    764 		if ((ip = mtod(m, struct ip *))->ip_p == IPPROTO_TCP) {
    765 			if (sc->sc_if.if_flags & SC_COMPRESS)
    766 				*mtod(m, u_char *) |=
    767 				    sl_compress_tcp(m, ip, &sc->sc_comp, 1);
    768 		}
    769 #endif
    770 #if NBPFILTER > 0
    771 		if (sc->sc_if.if_bpf && bpf_m != NULL)
    772 			bpf_mtap_sl_out(sc->sc_if.if_bpf, mtod(m, u_char *),
    773 			    bpf_m);
    774 #endif
    775 		getbinuptime(&sc->sc_lastpacket);
    776 
    777 		s = spltty();
    778 
    779 		/*
    780 		 * The extra FRAME_END will start up a new packet,
    781 		 * and thus will flush any accumulated garbage.  We
    782 		 * do this whenever the line may have been idle for
    783 		 * some time.
    784 		 */
    785 		if (tp->t_outq.c_cc == 0) {
    786 			sc->sc_if.if_obytes++;
    787 			(void)putc(FRAME_END, &tp->t_outq);
    788 		}
    789 
    790 		while (m) {
    791 			u_char *bp, *cp, *ep;
    792 
    793 			bp = cp = mtod(m, u_char *);
    794 			ep = cp + m->m_len;
    795 			while (cp < ep) {
    796 				/*
    797 				 * Find out how many bytes in the
    798 				 * string we can handle without
    799 				 * doing something special.
    800 				 */
    801 				while (cp < ep) {
    802 					switch (*cp++) {
    803 					case FRAME_ESCAPE:
    804 					case FRAME_END:
    805 						cp--;
    806 						goto out;
    807 					}
    808 				}
    809 				out:
    810 				if (cp > bp) {
    811 					/*
    812 					 * Put N characters at once
    813 					 * into the tty output queue.
    814 					 */
    815 					if (b_to_q(bp, cp - bp, &tp->t_outq))
    816 						break;
    817 					sc->sc_if.if_obytes += cp - bp;
    818 				}
    819 				/*
    820 				 * If there are characters left in
    821 				 * the mbuf, the first one must be
    822 				 * special..  Put it out in a different
    823 				 * form.
    824 				 */
    825 				if (cp < ep) {
    826 					if (putc(FRAME_ESCAPE, &tp->t_outq))
    827 						break;
    828 					if (putc(*cp++ == FRAME_ESCAPE ?
    829 					    TRANS_FRAME_ESCAPE :
    830 					    TRANS_FRAME_END,
    831 					    &tp->t_outq)) {
    832 						(void)unputc(&tp->t_outq);
    833 						break;
    834 					}
    835 					sc->sc_if.if_obytes += 2;
    836 				}
    837 				bp = cp;
    838 			}
    839 			MFREE(m, m2);
    840 			m = m2;
    841 		}
    842 
    843 		if (putc(FRAME_END, &tp->t_outq)) {
    844 			/*
    845 			 * Not enough room.  Remove a char to make
    846 			 * room and end the packet normally.  If
    847 			 * you get many collisions (more than one
    848 			 * or two a day), you probably do not have
    849 			 * enough clists and you should increase
    850 			 * "nclist" in param.c
    851 			 */
    852 			(void)unputc(&tp->t_outq);
    853 			(void)putc(FRAME_END, &tp->t_outq);
    854 			sc->sc_if.if_collisions++;
    855 		} else {
    856 			sc->sc_if.if_obytes++;
    857 			sc->sc_if.if_opackets++;
    858 		}
    859 
    860 		/*
    861 		 * We now have characters in the output queue,
    862 		 * kick the serial port.
    863 		 */
    864 		(*tp->t_oproc)(tp);
    865 		splx(s);
    866 	}
    867 
    868 	/*
    869 	 * Input processing loop.
    870 	 */
    871 	for (;;) {
    872 		s = spltty();
    873 		IF_DEQUEUE(&sc->sc_inq, m);
    874 		splx(s);
    875 		if (m == NULL)
    876 			break;
    877 		pktstart = mtod(m, u_char *);
    878 		len = m->m_pkthdr.len;
    879 #if NBPFILTER > 0
    880 		if (sc->sc_if.if_bpf) {
    881 			/*
    882 			 * Save the compressed header, so we
    883 			 * can tack it on later.  Note that we
    884 			 * will end up copying garbage in some
    885 			 * cases but this is okay.  We remember
    886 			 * where the buffer started so we can
    887 			 * compute the new header length.
    888 			 */
    889 			memcpy(chdr, pktstart, CHDR_LEN);
    890 		}
    891 #endif /* NBPFILTER > 0 */
    892 #ifdef INET
    893 		if ((c = (*pktstart & 0xf0)) != (IPVERSION << 4)) {
    894 			if (c & 0x80)
    895 				c = TYPE_COMPRESSED_TCP;
    896 			else if (c == TYPE_UNCOMPRESSED_TCP)
    897 				*pktstart &= 0x4f; /* XXX */
    898 			/*
    899 			 * We've got something that's not an IP
    900 			 * packet.  If compression is enabled,
    901 			 * try to decompress it.  Otherwise, if
    902 			 * `auto-enable' compression is on and
    903 			 * it's a reasonable packet, decompress
    904 			 * it and then enable compression.
    905 			 * Otherwise, drop it.
    906 			 */
    907 			if (sc->sc_if.if_flags & SC_COMPRESS) {
    908 				len = sl_uncompress_tcp(&pktstart, len,
    909 				    (u_int)c, &sc->sc_comp);
    910 				if (len <= 0) {
    911 					m_freem(m);
    912 					continue;
    913 				}
    914 			} else if ((sc->sc_if.if_flags & SC_AUTOCOMP) &&
    915 			    c == TYPE_UNCOMPRESSED_TCP && len >= 40) {
    916 				len = sl_uncompress_tcp(&pktstart, len,
    917 				    (u_int)c, &sc->sc_comp);
    918 				if (len <= 0) {
    919 					m_freem(m);
    920 					continue;
    921 				}
    922 				sc->sc_if.if_flags |= SC_COMPRESS;
    923 			} else {
    924 				m_freem(m);
    925 				continue;
    926 			}
    927 		}
    928 #endif
    929 		m->m_data = (void *) pktstart;
    930 		m->m_pkthdr.len = m->m_len = len;
    931 #if NBPFILTER > 0
    932 		if (sc->sc_if.if_bpf) {
    933 			bpf_mtap_sl_in(sc->sc_if.if_bpf, chdr, &m);
    934 			if (m == NULL)
    935 				continue;
    936 		}
    937 #endif /* NBPFILTER > 0 */
    938 		/*
    939 		 * If the packet will fit into a single
    940 		 * header mbuf, copy it into one, to save
    941 		 * memory.
    942 		 */
    943 		if (m->m_pkthdr.len < MHLEN) {
    944 			struct mbuf *n;
    945 			int pktlen;
    946 
    947 			MGETHDR(n, M_DONTWAIT, MT_DATA);
    948 			pktlen = m->m_pkthdr.len;
    949 			M_MOVE_PKTHDR(n, m);
    950 			memcpy(mtod(n, void *), mtod(m, void *), pktlen);
    951 			n->m_len = m->m_len;
    952 			m_freem(m);
    953 			m = n;
    954 		}
    955 
    956 		sc->sc_if.if_ipackets++;
    957 		getbinuptime(&sc->sc_lastpacket);
    958 
    959 #ifdef INET
    960 		s = splnet();
    961 		if (IF_QFULL(&ipintrq)) {
    962 			IF_DROP(&ipintrq);
    963 			sc->sc_if.if_ierrors++;
    964 			sc->sc_if.if_iqdrops++;
    965 			m_freem(m);
    966 		} else {
    967 			IF_ENQUEUE(&ipintrq, m);
    968 			schednetisr(NETISR_IP);
    969 		}
    970 		splx(s);
    971 #endif
    972 	}
    973 }
    974 
    975 /*
    976  * Process an ioctl request.
    977  */
    978 static int
    979 slioctl(struct ifnet *ifp, u_long cmd, void *data)
    980 {
    981 	struct ifaddr *ifa = (struct ifaddr *)data;
    982 	struct ifreq *ifr = (struct ifreq *)data;
    983 	int s = splnet(), error = 0;
    984 	struct sl_softc *sc = ifp->if_softc;
    985 	struct ppp_stats *psp;
    986 	struct ppp_comp_stats *pcp;
    987 
    988 	switch (cmd) {
    989 
    990 	case SIOCSIFADDR:
    991 		if (ifa->ifa_addr->sa_family == AF_INET)
    992 			ifp->if_flags |= IFF_UP;
    993 		else
    994 			error = EAFNOSUPPORT;
    995 		break;
    996 
    997 	case SIOCSIFDSTADDR:
    998 		if (ifa->ifa_addr->sa_family != AF_INET)
    999 			error = EAFNOSUPPORT;
   1000 		break;
   1001 
   1002 	case SIOCSIFMTU:
   1003 		if ((ifr->ifr_mtu < 3) || (ifr->ifr_mtu > SLMAX)) {
   1004 		    error = EINVAL;
   1005 		    break;
   1006 		}
   1007 		sc->sc_if.if_mtu = ifr->ifr_mtu;
   1008 		break;
   1009 
   1010 	case SIOCGIFMTU:
   1011 		ifr->ifr_mtu = sc->sc_if.if_mtu;
   1012 		break;
   1013 
   1014 	case SIOCADDMULTI:
   1015 	case SIOCDELMULTI:
   1016 		if (ifr == 0) {
   1017 			error = EAFNOSUPPORT;		/* XXX */
   1018 			break;
   1019 		}
   1020 		switch (ifreq_getaddr(cmd, ifr)->sa_family) {
   1021 
   1022 #ifdef INET
   1023 		case AF_INET:
   1024 			break;
   1025 #endif
   1026 
   1027 		default:
   1028 			error = EAFNOSUPPORT;
   1029 			break;
   1030 		}
   1031 		break;
   1032 
   1033 	case SIOCGPPPSTATS:
   1034 		psp = &((struct ifpppstatsreq *) data)->stats;
   1035 		(void)memset(psp, 0, sizeof(*psp));
   1036 		psp->p.ppp_ibytes = sc->sc_if.if_ibytes;
   1037 		psp->p.ppp_ipackets = sc->sc_if.if_ipackets;
   1038 		psp->p.ppp_ierrors = sc->sc_if.if_ierrors;
   1039 		psp->p.ppp_obytes = sc->sc_if.if_obytes;
   1040 		psp->p.ppp_opackets = sc->sc_if.if_opackets;
   1041 		psp->p.ppp_oerrors = sc->sc_if.if_oerrors;
   1042 #ifdef INET
   1043 		psp->vj.vjs_packets = sc->sc_comp.sls_packets;
   1044 		psp->vj.vjs_compressed = sc->sc_comp.sls_compressed;
   1045 		psp->vj.vjs_searches = sc->sc_comp.sls_searches;
   1046 		psp->vj.vjs_misses = sc->sc_comp.sls_misses;
   1047 		psp->vj.vjs_uncompressedin = sc->sc_comp.sls_uncompressedin;
   1048 		psp->vj.vjs_compressedin = sc->sc_comp.sls_compressedin;
   1049 		psp->vj.vjs_errorin = sc->sc_comp.sls_errorin;
   1050 		psp->vj.vjs_tossed = sc->sc_comp.sls_tossed;
   1051 #endif
   1052 		break;
   1053 
   1054 	case SIOCGPPPCSTATS:
   1055 		pcp = &((struct ifpppcstatsreq *) data)->stats;
   1056 		(void)memset(pcp, 0, sizeof(*pcp));
   1057 		break;
   1058 
   1059 	default:
   1060 		error = EINVAL;
   1061 	}
   1062 	splx(s);
   1063 	return error;
   1064 }
   1065