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