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