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