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