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