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