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