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