BSD 4_4 release
[unix-history] / usr / src / sys / net / if_sl.c
index 02d87ab..0f51fc2 100644 (file)
@@ -1,4 +1,37 @@
-/*     @(#)if_sl.c     5.7 (Berkeley) %G% */
+/*
+ * Copyright (c) 1987, 1989, 1992, 1993
+ *     The Regents of the University of California.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ *     @(#)if_sl.c     8.1 (Berkeley) 6/10/93
+ */
 
 /*
  * Serial Line interface
 
 /*
  * Serial Line interface
  *
  * Converted to 4.3BSD Beta by Chris Torek.
  * Other changes made at Berkeley, based in part on code by Kirk Smith.
  *
  * Converted to 4.3BSD Beta by Chris Torek.
  * Other changes made at Berkeley, based in part on code by Kirk Smith.
+ * W. Jolitz added slip abort.
+ *
+ * Hacked almost beyond recognition by Van Jacobson (van@helios.ee.lbl.gov).
+ * Added priority queuing for "interactive" traffic; hooks for TCP
+ * header compression; ICMP filtering (at 2400 baud, some cretin
+ * pinging you can use up all your bandwidth).  Made low clist behavior
+ * more robust and slightly less likely to hang serial line.
+ * Sped up a bunch of things.
+ * 
+ * Note that splimp() is used throughout to block both (tty) input
+ * interrupts and network activity; thus, splimp must be >= spltty.
  */
 
  */
 
-/* $Header: if_sl.c,v 1.12 85/12/20 21:54:55 chris Exp $ */
-/* from if_sl.c,v 1.11 84/10/04 12:54:47 rick Exp */
-
 #include "sl.h"
 #if NSL > 0
 
 #include "sl.h"
 #if NSL > 0
 
-#include "param.h"
-#include "mbuf.h"
-#include "buf.h"
-#include "dk.h"
-#include "socket.h"
-#include "ioctl.h"
-#include "file.h"
-#include "tty.h"
-#include "errno.h"
-
-#include "if.h"
-#include "netisr.h"
-#include "route.h"
+#include <sys/param.h>
+#include <sys/proc.h>
+#include <sys/mbuf.h>
+#include <sys/buf.h>
+#include <sys/dkstat.h>
+#include <sys/socket.h>
+#include <sys/ioctl.h>
+#include <sys/file.h>
+#include <sys/tty.h>
+#include <sys/kernel.h>
+#include <sys/conf.h>
+
+#include <machine/cpu.h>
+
+#include <net/if.h>
+#include <net/if_types.h>
+#include <net/netisr.h>
+#include <net/route.h>
+
 #if INET
 #if INET
-#include "../netinet/in.h"
-#include "../netinet/in_systm.h"
-#include "../netinet/ip.h"
-#include "../netinet/ip_var.h"
+#include <netinet/in.h>
+#include <netinet/in_systm.h>
+#include <netinet/in_var.h>
+#include <netinet/ip.h>
+#else
+Huh? Slip without inet?
 #endif
 
 #endif
 
-#ifdef vax
-#include "../vax/mtpr.h"
-#endif vax
+#include <net/slcompress.h>
+#include <net/if_slvar.h>
+
+/*
+ * SLMAX is a hard limit on input packet size.  To simplify the code
+ * and improve performance, we require that packets fit in an mbuf
+ * cluster, and if we get a compressed packet, there's enough extra
+ * room to expand the header into a max length tcp/ip header (128
+ * bytes).  So, SLMAX can be at most
+ *     MCLBYTES - 128
+ *
+ * SLMTU is a hard limit on output packet size.  To insure good
+ * interactive response, SLMTU wants to be the smallest size that
+ * amortizes the header cost.  (Remember that even with
+ * type-of-service queuing, we have to wait for any in-progress
+ * packet to finish.  I.e., we wait, on the average, 1/2 * mtu /
+ * cps, where cps is the line speed in characters per second.
+ * E.g., 533ms wait for a 1024 byte MTU on a 9600 baud line.  The
+ * average compressed header size is 6-8 bytes so any MTU > 90
+ * bytes will give us 90% of the line bandwidth.  A 100ms wait is
+ * tolerable (500ms is not), so want an MTU around 296.  (Since TCP
+ * will send 256 byte segments (to allow for 40 byte headers), the
+ * typical packet size on the wire will be around 260 bytes).  In
+ * 4.3tahoe+ systems, we can set an MTU in a route so we do that &
+ * leave the interface MTU relatively high (so we don't IP fragment
+ * when acting as a gateway to someone using a stupid MTU).
+ *
+ * Similar considerations apply to SLIP_HIWAT:  It's the amount of
+ * data that will be queued 'downstream' of us (i.e., in clists
+ * waiting to be picked up by the tty output interrupt).  If we
+ * queue a lot of data downstream, it's immune to our t.o.s. queuing.
+ * E.g., if SLIP_HIWAT is 1024, the interactive traffic in mixed
+ * telnet/ftp will see a 1 sec wait, independent of the mtu (the
+ * wait is dependent on the ftp window size but that's typically
+ * 1k - 4k).  So, we want SLIP_HIWAT just big enough to amortize
+ * the cost (in idle time on the wire) of the tty driver running
+ * off the end of its clists & having to call back slstart for a
+ * new packet.  For a tty interface with any buffering at all, this
+ * cost will be zero.  Even with a totally brain dead interface (like
+ * the one on a typical workstation), the cost will be <= 1 character
+ * time.  So, setting SLIP_HIWAT to ~100 guarantees that we'll lose
+ * at most 1% while maintaining good interactive response.
+ */
+#define BUFOFFSET      128
+#define        SLMAX           (MCLBYTES - BUFOFFSET)
+#define        SLBUFSIZE       (SLMAX + BUFOFFSET)
+#define        SLMTU           296
+#define        SLIP_HIWAT      roundup(50,CBSIZE)
+#define        CLISTRESERVE    1024    /* Can't let clists get too low */
 
 /*
 
 /*
- * N.B.: SLMTU is now a hard limit on input packet size.
- * SLMTU must be <= CLBYTES - sizeof(struct ifnet *).
+ * SLIP ABORT ESCAPE MECHANISM:
+ *     (inspired by HAYES modem escape arrangement)
+ *     1sec escape 1sec escape 1sec escape { 1sec escape 1sec escape }
+ *     within window time signals a "soft" exit from slip mode by remote end
+ *     if the IFF_DEBUG flag is on.
  */
  */
-#define        SLMTU   1006
-#define        SLIP_HIWAT      1000    /* don't start a new packet if HIWAT on queue */
-#define        CLISTRESERVE    1000    /* Can't let clists get too low */
-
-struct sl_softc {
-       struct  ifnet sc_if;    /* network-visible interface */
-       short   sc_flags;       /* see below */
-       short   sc_ilen;        /* length of input-packet-so-far */
-       struct  tty *sc_ttyp;   /* pointer to tty structure */
-       char    *sc_mp;         /* pointer to next available buf char */
-       char    *sc_buf;        /* input buffer */
-} sl_softc[NSL];
-
-/* flags */
-#define        SC_ESCAPED      0x0001  /* saw a FRAME_ESCAPE */
-#define        SC_OACTIVE      0x0002  /* output tty is active */
-
-#define FRAME_END              0300            /* Frame End */
-#define FRAME_ESCAPE           0333            /* Frame Esc */
-#define TRANS_FRAME_END                0334            /* transposed frame end */
-#define TRANS_FRAME_ESCAPE     0335            /* transposed frame esc */
+#define        ABT_ESC         '\033'  /* can't be t_intr - distant host must know it*/
+#define ABT_IDLE       1       /* in seconds - idle before an escape */
+#define ABT_COUNT      3       /* count of escapes for abort */
+#define ABT_WINDOW     (ABT_COUNT*2+2) /* in seconds - time to count */
+
+struct sl_softc sl_softc[NSL];
+
+#define FRAME_END              0xc0            /* Frame End */
+#define FRAME_ESCAPE           0xdb            /* Frame Esc */
+#define TRANS_FRAME_END                0xdc            /* transposed frame end */
+#define TRANS_FRAME_ESCAPE     0xdd            /* transposed frame esc */
 
 #define t_sc T_LINEP
 
 
 #define t_sc T_LINEP
 
-int sloutput(), slioctl(), ttrstrt();
+extern struct timeval time;
+
+static int slinit __P((struct sl_softc *));
+static struct mbuf *sl_btom __P((struct sl_softc *, int));
 
 /*
  * Called from boot code to establish sl interfaces.
  */
 
 /*
  * Called from boot code to establish sl interfaces.
  */
+void
 slattach()
 {
        register struct sl_softc *sc;
 slattach()
 {
        register struct sl_softc *sc;
@@ -90,53 +182,81 @@ slattach()
 
        for (sc = sl_softc; i < NSL; sc++) {
                sc->sc_if.if_name = "sl";
 
        for (sc = sl_softc; i < NSL; sc++) {
                sc->sc_if.if_name = "sl";
+               sc->sc_if.if_next = NULL;
                sc->sc_if.if_unit = i++;
                sc->sc_if.if_mtu = SLMTU;
                sc->sc_if.if_unit = i++;
                sc->sc_if.if_mtu = SLMTU;
-               sc->sc_if.if_flags = IFF_POINTOPOINT;
+               sc->sc_if.if_flags =
+                   IFF_POINTOPOINT | SC_AUTOCOMP | IFF_MULTICAST;
+               sc->sc_if.if_type = IFT_SLIP;
                sc->sc_if.if_ioctl = slioctl;
                sc->sc_if.if_output = sloutput;
                sc->sc_if.if_ioctl = slioctl;
                sc->sc_if.if_output = sloutput;
-               sc->sc_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
+               sc->sc_if.if_snd.ifq_maxlen = 50;
+               sc->sc_fastq.ifq_maxlen = 32;
                if_attach(&sc->sc_if);
        }
 }
 
                if_attach(&sc->sc_if);
        }
 }
 
+static int
+slinit(sc)
+       register struct sl_softc *sc;
+{
+       register caddr_t p;
+
+       if (sc->sc_ep == (u_char *) 0) {
+               MCLALLOC(p, M_WAIT);
+               if (p)
+                       sc->sc_ep = (u_char *)p + SLBUFSIZE;
+               else {
+                       printf("sl%d: can't allocate buffer\n", sc - sl_softc);
+                       sc->sc_if.if_flags &= ~IFF_UP;
+                       return (0);
+               }
+       }
+       sc->sc_buf = sc->sc_ep - SLMAX;
+       sc->sc_mp = sc->sc_buf;
+       sl_compress_init(&sc->sc_comp);
+       return (1);
+}
+
 /*
  * Line specific open routine.
  * Attach the given tty to the first available sl unit.
  */
 /* ARGSUSED */
 /*
  * Line specific open routine.
  * Attach the given tty to the first available sl unit.
  */
 /* ARGSUSED */
+int
 slopen(dev, tp)
        dev_t dev;
        register struct tty *tp;
 {
 slopen(dev, tp)
        dev_t dev;
        register struct tty *tp;
 {
+       struct proc *p = curproc;               /* XXX */
        register struct sl_softc *sc;
        register int nsl;
        register struct sl_softc *sc;
        register int nsl;
+       int error;
+
+       if (error = suser(p->p_ucred, &p->p_acflag))
+               return (error);
 
 
-       if (!suser())
-               return (EPERM);
        if (tp->t_line == SLIPDISC)
        if (tp->t_line == SLIPDISC)
-               return (EBUSY);
+               return (0);
 
 
-       for (nsl = 0, sc = sl_softc; nsl < NSL; nsl++, sc++)
+       for (nsl = NSL, sc = sl_softc; --nsl >= 0; sc++)
                if (sc->sc_ttyp == NULL) {
                if (sc->sc_ttyp == NULL) {
-                       sc->sc_flags = 0;
-                       sc->sc_ilen = 0;
                        if (slinit(sc) == 0)
                                return (ENOBUFS);
                        tp->t_sc = (caddr_t)sc;
                        sc->sc_ttyp = tp;
                        if (slinit(sc) == 0)
                                return (ENOBUFS);
                        tp->t_sc = (caddr_t)sc;
                        sc->sc_ttyp = tp;
+                       sc->sc_if.if_baudrate = tp->t_ospeed;
                        ttyflush(tp, FREAD | FWRITE);
                        return (0);
                }
                        ttyflush(tp, FREAD | FWRITE);
                        return (0);
                }
-
        return (ENXIO);
 }
 
 /*
  * Line specific close routine.
  * Detach the tty from the sl unit.
        return (ENXIO);
 }
 
 /*
  * Line specific close routine.
  * Detach the tty from the sl unit.
- * Mimics part of ttyclose().
  */
  */
+void
 slclose(tp)
        struct tty *tp;
 {
 slclose(tp)
        struct tty *tp;
 {
@@ -144,14 +264,16 @@ slclose(tp)
        int s;
 
        ttywflush(tp);
        int s;
 
        ttywflush(tp);
+       s = splimp();           /* actually, max(spltty, splnet) */
        tp->t_line = 0;
        tp->t_line = 0;
-       s = splimp();           /* paranoid; splnet probably ok */
        sc = (struct sl_softc *)tp->t_sc;
        if (sc != NULL) {
                if_down(&sc->sc_if);
                sc->sc_ttyp = NULL;
                tp->t_sc = NULL;
        sc = (struct sl_softc *)tp->t_sc;
        if (sc != NULL) {
                if_down(&sc->sc_if);
                sc->sc_ttyp = NULL;
                tp->t_sc = NULL;
-               MCLFREE((struct mbuf *)sc->sc_buf);
+               MCLFREE((caddr_t)(sc->sc_ep - SLBUFSIZE));
+               sc->sc_ep = 0;
+               sc->sc_mp = 0;
                sc->sc_buf = 0;
        }
        splx(s);
                sc->sc_buf = 0;
        }
        splx(s);
@@ -162,27 +284,41 @@ slclose(tp)
  * Provide a way to get the sl unit number.
  */
 /* ARGSUSED */
  * Provide a way to get the sl unit number.
  */
 /* ARGSUSED */
+int
 sltioctl(tp, cmd, data, flag)
        struct tty *tp;
 sltioctl(tp, cmd, data, flag)
        struct tty *tp;
+       int cmd;
        caddr_t data;
        caddr_t data;
+       int flag;
 {
 {
+       struct sl_softc *sc = (struct sl_softc *)tp->t_sc;
+       int s;
 
 
-       if (cmd == TIOCGETD) {
-               *(int *)data = ((struct sl_softc *)tp->t_sc)->sc_if.if_unit;
-               return (0);
+       switch (cmd) {
+       case SLIOCGUNIT:
+               *(int *)data = sc->sc_if.if_unit;
+               break;
+
+       default:
+               return (-1);
        }
        }
-       return (-1);
+       return (0);
 }
 
 /*
  * Queue a packet.  Start transmission if not active.
  */
 }
 
 /*
  * Queue a packet.  Start transmission if not active.
  */
-sloutput(ifp, m, dst)
-       register struct ifnet *ifp;
+int
+sloutput(ifp, m, dst, rtp)
+       struct ifnet *ifp;
        register struct mbuf *m;
        struct sockaddr *dst;
        register struct mbuf *m;
        struct sockaddr *dst;
+       struct rtentry *rtp;
 {
 {
-       register struct sl_softc *sc;
+       register struct sl_softc *sc = &sl_softc[ifp->if_unit];
+       register struct ip *ip;
+       register struct ifqueue *ifq;
+       register int p;
        int s;
 
        /*
        int s;
 
        /*
@@ -190,13 +326,13 @@ sloutput(ifp, m, dst)
         * the line protocol to support other address families.
         */
        if (dst->sa_family != AF_INET) {
         * the line protocol to support other address families.
         */
        if (dst->sa_family != AF_INET) {
-               printf("sl%d: af%d not supported\n", ifp->if_unit,
+               printf("sl%d: af%d not supported\n", sc->sc_if.if_unit,
                        dst->sa_family);
                m_freem(m);
                        dst->sa_family);
                m_freem(m);
+               sc->sc_if.if_noproto++;
                return (EAFNOSUPPORT);
        }
 
                return (EAFNOSUPPORT);
        }
 
-       sc = &sl_softc[ifp->if_unit];
        if (sc->sc_ttyp == NULL) {
                m_freem(m);
                return (ENETDOWN);      /* sort of */
        if (sc->sc_ttyp == NULL) {
                m_freem(m);
                return (ENETDOWN);      /* sort of */
@@ -205,20 +341,42 @@ sloutput(ifp, m, dst)
                m_freem(m);
                return (EHOSTUNREACH);
        }
                m_freem(m);
                return (EHOSTUNREACH);
        }
+       ifq = &sc->sc_if.if_snd;
+       ip = mtod(m, struct ip *);
+       if (ip->ip_tos & IPTOS_LOWDELAY) {
+               ifq = &sc->sc_fastq;
+               p = 1;
+       } else
+               p = 0;
+       if (ip->ip_p == IPPROTO_TCP) {
+               if (sc->sc_if.if_flags & SC_COMPRESS) {
+                       /*
+                        * The last parameter turns off connection id
+                        * compression for background traffic:  Since
+                        * fastq traffic can jump ahead of the background
+                        * traffic, we don't know what order packets will
+                        * go on the line.
+                        */
+                       p = sl_compress_tcp(m, ip, &sc->sc_comp, p);
+                       *mtod(m, u_char *) |= p;
+               }
+       } else if (sc->sc_if.if_flags & SC_NOICMP && ip->ip_p == IPPROTO_ICMP) {
+               m_freem(m);
+               return (ENETRESET);             /* XXX ? */
+       }
        s = splimp();
        s = splimp();
-       if (IF_QFULL(&ifp->if_snd)) {
-               IF_DROP(&ifp->if_snd);
-               splx(s);
+       if (IF_QFULL(ifq)) {
+               IF_DROP(ifq);
                m_freem(m);
                m_freem(m);
+               splx(s);
                sc->sc_if.if_oerrors++;
                return (ENOBUFS);
        }
                sc->sc_if.if_oerrors++;
                return (ENOBUFS);
        }
-       IF_ENQUEUE(&ifp->if_snd, m);
-       if ((sc->sc_flags & SC_OACTIVE) == 0) {
-               splx(s);
+       IF_ENQUEUE(ifq, m);
+       sc->sc_if.if_lastchange = time;
+       if (sc->sc_ttyp->t_outq.c_cc == 0)
                slstart(sc->sc_ttyp);
                slstart(sc->sc_ttyp);
-       } else
-               splx(s);
+       splx(s);
        return (0);
 }
 
        return (0);
 }
 
@@ -227,14 +385,14 @@ sloutput(ifp, m, dst)
  * to send from the interface queue and map it to
  * the interface before starting output.
  */
  * to send from the interface queue and map it to
  * the interface before starting output.
  */
+void
 slstart(tp)
        register struct tty *tp;
 {
        register struct sl_softc *sc = (struct sl_softc *)tp->t_sc;
        register struct mbuf *m;
 slstart(tp)
        register struct tty *tp;
 {
        register struct sl_softc *sc = (struct sl_softc *)tp->t_sc;
        register struct mbuf *m;
-       register int len;
        register u_char *cp;
        register u_char *cp;
-       int flush, nd, np, n, s;
+       int s;
        struct mbuf *m2;
        extern int cfreecount;
 
        struct mbuf *m2;
        extern int cfreecount;
 
@@ -244,11 +402,11 @@ slstart(tp)
                 * We are being called in lieu of ttstart and must do what
                 * it would.
                 */
                 * We are being called in lieu of ttstart and must do what
                 * it would.
                 */
-               if (tp->t_outq.c_cc > 0)
-                       ttstart(tp);
-               if (tp->t_outq.c_cc > SLIP_HIWAT)
-                       return;
-
+               if (tp->t_outq.c_cc != 0) {
+                       (*tp->t_oproc)(tp);
+                       if (tp->t_outq.c_cc > SLIP_HIWAT)
+                               return;
+               }
                /*
                 * This happens briefly when the line shuts down.
                 */
                /*
                 * This happens briefly when the line shuts down.
                 */
@@ -256,73 +414,83 @@ slstart(tp)
                        return;
 
                /*
                        return;
 
                /*
-                * If system is getting low on clists
-                * and we have something running already, stop here.
+                * Get a packet and send it to the interface.
                 */
                 */
-               if (cfreecount < CLISTRESERVE + SLMTU &&
-                   sc->sc_flags & SC_OACTIVE)
+               s = splimp();
+               IF_DEQUEUE(&sc->sc_fastq, m);
+               if (m)
+                       sc->sc_if.if_omcasts++;         /* XXX */
+               else
+                       IF_DEQUEUE(&sc->sc_if.if_snd, m);
+               splx(s);
+               if (m == NULL)
                        return;
                        return;
+               sc->sc_if.if_lastchange = time;
 
                /*
 
                /*
-                * Get a packet and send it to the interface.
+                * If system is getting low on clists, just flush our
+                * output queue (if the stuff was important, it'll get
+                * retransmitted).
                 */
                 */
-               s = splimp();
-               IF_DEQUEUE(&sc->sc_if.if_snd, m);
-               if (m == NULL) {
-                       if (tp->t_outq.c_cc == 0)
-                               sc->sc_flags &= ~SC_OACTIVE;
-                       splx(s);
-                       return;
+               if (cfreecount < CLISTRESERVE + SLMTU) {
+                       m_freem(m);
+                       sc->sc_if.if_collisions++;
+                       continue;
                }
                }
-               flush = !(sc->sc_flags & SC_OACTIVE);
-               sc->sc_flags |= SC_OACTIVE;
-               splx(s);
-
                /*
                 * The extra FRAME_END will start up a new packet, and thus
                 * will flush any accumulated garbage.  We do this whenever
                 * the line may have been idle for some time.
                 */
                /*
                 * The extra FRAME_END will start up a new packet, and thus
                 * will flush any accumulated garbage.  We do this whenever
                 * the line may have been idle for some time.
                 */
-               if (flush)
+               if (tp->t_outq.c_cc == 0) {
+                       ++sc->sc_if.if_obytes;
                        (void) putc(FRAME_END, &tp->t_outq);
                        (void) putc(FRAME_END, &tp->t_outq);
+               }
 
                while (m) {
 
                while (m) {
-                       cp = mtod(m, u_char *);
-                       len = m->m_len;
-                       while (len > 0) {
+                       register u_char *ep;
+
+                       cp = mtod(m, u_char *); ep = cp + m->m_len;
+                       while (cp < ep) {
                                /*
                                 * Find out how many bytes in the string we can
                                 * handle without doing something special.
                                 */
                                /*
                                 * Find out how many bytes in the string we can
                                 * handle without doing something special.
                                 */
-                               nd = locc(FRAME_ESCAPE, len, cp);
-                               np = locc(FRAME_END, len, cp);
-                               n = len - MAX(nd, np);
-                               if (n) {
+                               register u_char *bp = cp;
+
+                               while (cp < ep) {
+                                       switch (*cp++) {
+                                       case FRAME_ESCAPE:
+                                       case FRAME_END:
+                                               --cp;
+                                               goto out;
+                                       }
+                               }
+                               out:
+                               if (cp > bp) {
                                        /*
                                         * Put n characters at once
                                         * into the tty output queue.
                                         */
                                        /*
                                         * Put n characters at once
                                         * into the tty output queue.
                                         */
-                                       if (b_to_q((char *)cp, n, &tp->t_outq))
+                                       if (b_to_q((char *)bp, cp - bp, &tp->t_outq))
                                                break;
                                                break;
-                                       len -= n;
-                                       cp += n;
+                                       sc->sc_if.if_obytes += cp - bp;
                                }
                                /*
                                 * If there are characters left in the mbuf,
                                 * the first one must be special..
                                 * Put it out in a different form.
                                 */
                                }
                                /*
                                 * If there are characters left in the mbuf,
                                 * the first one must be special..
                                 * Put it out in a different form.
                                 */
-                               if (len) {
+                               if (cp < ep) {
                                        if (putc(FRAME_ESCAPE, &tp->t_outq))
                                                break;
                                        if (putc(FRAME_ESCAPE, &tp->t_outq))
                                                break;
-                                       if (putc(*cp == FRAME_ESCAPE ?
+                                       if (putc(*cp++ == FRAME_ESCAPE ?
                                           TRANS_FRAME_ESCAPE : TRANS_FRAME_END,
                                           &tp->t_outq)) {
                                                (void) unputc(&tp->t_outq);
                                                break;
                                        }
                                           TRANS_FRAME_ESCAPE : TRANS_FRAME_END,
                                           &tp->t_outq)) {
                                                (void) unputc(&tp->t_outq);
                                                break;
                                        }
-                                       cp++;
-                                       len--;
+                                       sc->sc_if.if_obytes += 2;
                                }
                        }
                        MFREE(m, m2);
                                }
                        }
                        MFREE(m, m2);
@@ -340,200 +508,238 @@ slstart(tp)
                        (void) unputc(&tp->t_outq);
                        (void) putc(FRAME_END, &tp->t_outq);
                        sc->sc_if.if_collisions++;
                        (void) unputc(&tp->t_outq);
                        (void) putc(FRAME_END, &tp->t_outq);
                        sc->sc_if.if_collisions++;
-               } else
-                       sc->sc_if.if_opackets++;
-       }
-}
-
-slinit(sc)
-       register struct sl_softc *sc;
-{
-       struct mbuf *p;
-
-       if (sc->sc_buf == (char *) 0) {
-               MCLALLOC(p, 1);
-               if (p) {
-                       sc->sc_buf = (char *)p;
-                       sc->sc_mp = sc->sc_buf + sizeof(struct ifnet *);
                } else {
                } else {
-                       printf("sl%d: can't allocate buffer\n", sc - sl_softc);
-                       sc->sc_if.if_flags &= ~IFF_UP;
-                       return (0);
+                       ++sc->sc_if.if_obytes;
+                       sc->sc_if.if_opackets++;
                }
        }
                }
        }
-       return (1);
 }
 
 /*
 }
 
 /*
- * Copy data buffer to mbuf chain; add ifnet pointer ifp.
+ * Copy data buffer to mbuf chain; add ifnet pointer.
  */
  */
-struct mbuf *
-sl_btom(sc, len, ifp)
-       struct sl_softc *sc;
+static struct mbuf *
+sl_btom(sc, len)
+       register struct sl_softc *sc;
        register int len;
        register int len;
-       struct ifnet *ifp;
 {
 {
-       register caddr_t cp;
-       register struct mbuf *m, **mp;
-       register unsigned count;
-       struct mbuf *top = NULL;
-
-       cp = sc->sc_buf + sizeof(struct ifnet *);
-       mp = &top;
-       while (len > 0) {
-               MGET(m, M_DONTWAIT, MT_DATA);
-               if ((*mp = m) == NULL) {
-                       m_freem(top);
+       register struct mbuf *m;
+
+       MGETHDR(m, M_DONTWAIT, MT_DATA);
+       if (m == NULL)
+               return (NULL);
+
+       /*
+        * If we have more than MHLEN bytes, it's cheaper to
+        * queue the cluster we just filled & allocate a new one
+        * for the input buffer.  Otherwise, fill the mbuf we
+        * allocated above.  Note that code in the input routine
+        * guarantees that packet will fit in a cluster.
+        */
+       if (len >= MHLEN) {
+               MCLGET(m, M_DONTWAIT);
+               if ((m->m_flags & M_EXT) == 0) {
+                       /*
+                        * we couldn't get a cluster - if memory's this
+                        * low, it's time to start dropping packets.
+                        */
+                       (void) m_free(m);
                        return (NULL);
                }
                        return (NULL);
                }
-               if (ifp)
-                       m->m_off += sizeof(ifp);
-               /*
-                * If we have at least NBPG bytes,
-                * allocate a new page.  Swap the current buffer page
-                * with the new one.  We depend on having a space
-                * left at the beginning of the buffer
-                * for the interface pointer.
-                */
-               if (len >= NBPG) {
-                       MCLGET(m);
-                       if (m->m_len == CLBYTES) {
-                               cp = mtod(m, char *);
-                               m->m_off = (int)sc->sc_buf - (int)m;
-                               sc->sc_buf = cp;
-                               if (ifp) {
-                                       m->m_off += sizeof(ifp);
-                                       count = MIN(len,
-                                           CLBYTES - sizeof(struct ifnet *));
-                               } else
-                                       count = MIN(len, CLBYTES);
-                               goto nocopy;
-                       }
-               }
-               if (ifp)
-                       count = MIN(len, MLEN - sizeof(ifp));
-               else
-                       count = MIN(len, MLEN);
-               bcopy(cp, mtod(m, caddr_t), count);
-nocopy:
-               m->m_len = count;
-               if (ifp) {
-                       m->m_off -= sizeof(ifp);
-                       m->m_len += sizeof(ifp);
-                       *mtod(m, struct ifnet **) = ifp;
-                       ifp = NULL;
-               }
-               cp += count;
-               len -= count;
-               mp = &m->m_next;
-       }
-       return (top);
+               sc->sc_ep = mtod(m, u_char *) + SLBUFSIZE;
+               m->m_data = (caddr_t)sc->sc_buf;
+               m->m_ext.ext_buf = (caddr_t)((int)sc->sc_buf &~ MCLOFSET);
+       } else
+               bcopy((caddr_t)sc->sc_buf, mtod(m, caddr_t), len);
+
+       m->m_len = len;
+       m->m_pkthdr.len = len;
+       m->m_pkthdr.rcvif = &sc->sc_if;
+       return (m);
 }
 
 /*
  * tty interface receiver interrupt.
  */
 }
 
 /*
  * tty interface receiver interrupt.
  */
+void
 slinput(c, tp)
        register int c;
        register struct tty *tp;
 {
        register struct sl_softc *sc;
        register struct mbuf *m;
 slinput(c, tp)
        register int c;
        register struct tty *tp;
 {
        register struct sl_softc *sc;
        register struct mbuf *m;
+       register int len;
        int s;
 
        tk_nin++;
        sc = (struct sl_softc *)tp->t_sc;
        if (sc == NULL)
                return;
        int s;
 
        tk_nin++;
        sc = (struct sl_softc *)tp->t_sc;
        if (sc == NULL)
                return;
+       if (!(tp->t_state&TS_CARR_ON))  /* XXX */
+               return;
 
 
-       c &= 0xff;
-       if (sc->sc_flags & SC_ESCAPED) {
-               sc->sc_flags &= ~SC_ESCAPED;
-               switch (c) {
+       ++sc->sc_if.if_ibytes;
+       c &= 0xff;                      /* XXX */
 
 
-               case TRANS_FRAME_ESCAPE:
+#ifdef ABT_ESC
+       if (sc->sc_if.if_flags & IFF_DEBUG) {
+               if (c == ABT_ESC) {
+                       /*
+                        * If we have a previous abort, see whether
+                        * this one is within the time limit.
+                        */
+                       if (sc->sc_abortcount &&
+                           time.tv_sec >= sc->sc_starttime + ABT_WINDOW)
+                               sc->sc_abortcount = 0;
+                       /*
+                        * If we see an abort after "idle" time, count it;
+                        * record when the first abort escape arrived.
+                        */
+                       if (time.tv_sec >= sc->sc_lasttime + ABT_IDLE) {
+                               if (++sc->sc_abortcount == 1)
+                                       sc->sc_starttime = time.tv_sec;
+                               if (sc->sc_abortcount >= ABT_COUNT) {
+                                       slclose(tp);
+                                       return;
+                               }
+                       }
+               } else
+                       sc->sc_abortcount = 0;
+               sc->sc_lasttime = time.tv_sec;
+       }
+#endif
+
+       switch (c) {
+
+       case TRANS_FRAME_ESCAPE:
+               if (sc->sc_escape)
                        c = FRAME_ESCAPE;
                        c = FRAME_ESCAPE;
-                       break;
+               break;
 
 
-               case TRANS_FRAME_END:
+       case TRANS_FRAME_END:
+               if (sc->sc_escape)
                        c = FRAME_END;
                        c = FRAME_END;
-                       break;
+               break;
 
 
-               default:
-                       sc->sc_if.if_ierrors++;
-                       sc->sc_mp = sc->sc_buf + sizeof(struct ifnet *);
-                       sc->sc_ilen = 0;
-                       return;
-               }
-       } else {
-               switch (c) {
+       case FRAME_ESCAPE:
+               sc->sc_escape = 1;
+               return;
 
 
-               case FRAME_END:
-                       if (sc->sc_ilen == 0)   /* ignore */
-                               return;
-                       m = sl_btom(sc, sc->sc_ilen, &sc->sc_if);
-                       if (m == NULL) {
-                               sc->sc_if.if_ierrors++;
-                               return;
-                       }
-                       sc->sc_mp = sc->sc_buf + sizeof(struct ifnet *);
-                       sc->sc_ilen = 0;
-                       sc->sc_if.if_ipackets++;
-                       s = splimp();
-                       if (IF_QFULL(&ipintrq)) {
-                               IF_DROP(&ipintrq);
-                               sc->sc_if.if_ierrors++;
-                               m_freem(m);
-                       } else {
-                               IF_ENQUEUE(&ipintrq, m);
-                               schednetisr(NETISR_IP);
-                       }
-                       splx(s);
-                       return;
+       case FRAME_END:
+               len = sc->sc_mp - sc->sc_buf;
+               if (len < 3)
+                       /* less than min length packet - ignore */
+                       goto newpack;
+
+               if ((c = (*sc->sc_buf & 0xf0)) != (IPVERSION << 4)) {
+                       if (c & 0x80)
+                               c = TYPE_COMPRESSED_TCP;
+                       else if (c == TYPE_UNCOMPRESSED_TCP)
+                               *sc->sc_buf &= 0x4f; /* XXX */
+                       /*
+                        * We've got something that's not an IP packet.
+                        * If compression is enabled, try to decompress it.
+                        * Otherwise, if `auto-enable' compression is on and
+                        * it's a reasonable packet, decompress it and then
+                        * enable compression.  Otherwise, drop it.
+                        */
+                       if (sc->sc_if.if_flags & SC_COMPRESS) {
+                               len = sl_uncompress_tcp(&sc->sc_buf, len,
+                                                       (u_int)c, &sc->sc_comp);
+                               if (len <= 0)
+                                       goto error;
+                       } else if ((sc->sc_if.if_flags & SC_AUTOCOMP) &&
+                           c == TYPE_UNCOMPRESSED_TCP && len >= 40) {
+                               len = sl_uncompress_tcp(&sc->sc_buf, len,
+                                                       (u_int)c, &sc->sc_comp);
+                               if (len <= 0)
+                                       goto error;
+                               sc->sc_if.if_flags |= SC_COMPRESS;
+                       } else
+                               goto error;
+               }
+               m = sl_btom(sc, len);
+               if (m == NULL)
+                       goto error;
 
 
-               case FRAME_ESCAPE:
-                       sc->sc_flags |= SC_ESCAPED;
-                       return;
+               sc->sc_if.if_ipackets++;
+               sc->sc_if.if_lastchange = time;
+               s = splimp();
+               if (IF_QFULL(&ipintrq)) {
+                       IF_DROP(&ipintrq);
+                       sc->sc_if.if_ierrors++;
+                       sc->sc_if.if_iqdrops++;
+                       m_freem(m);
+               } else {
+                       IF_ENQUEUE(&ipintrq, m);
+                       schednetisr(NETISR_IP);
                }
                }
+               splx(s);
+               goto newpack;
        }
        }
-       if (++sc->sc_ilen > SLMTU) {
-               sc->sc_if.if_ierrors++;
-               sc->sc_mp = sc->sc_buf + sizeof(struct ifnet *);
-               sc->sc_ilen = 0;
+       if (sc->sc_mp < sc->sc_ep) {
+               *sc->sc_mp++ = c;
+               sc->sc_escape = 0;
                return;
        }
                return;
        }
-       *sc->sc_mp++ = c;
+error:
+       sc->sc_if.if_ierrors++;
+newpack:
+       sc->sc_mp = sc->sc_buf = sc->sc_ep - SLMAX;
+       sc->sc_escape = 0;
 }
 
 /*
  * Process an ioctl request.
  */
 }
 
 /*
  * Process an ioctl request.
  */
+int
 slioctl(ifp, cmd, data)
        register struct ifnet *ifp;
        int cmd;
        caddr_t data;
 {
        register struct ifaddr *ifa = (struct ifaddr *)data;
 slioctl(ifp, cmd, data)
        register struct ifnet *ifp;
        int cmd;
        caddr_t data;
 {
        register struct ifaddr *ifa = (struct ifaddr *)data;
-       int s = splimp(), error = 0;
+       register struct ifreq *ifr;
+       register int s = splimp(), error = 0;
 
        switch (cmd) {
 
        case SIOCSIFADDR:
 
        switch (cmd) {
 
        case SIOCSIFADDR:
-               if (ifa->ifa_addr.sa_family == AF_INET)
+               if (ifa->ifa_addr->sa_family == AF_INET)
                        ifp->if_flags |= IFF_UP;
                else
                        error = EAFNOSUPPORT;
                break;
 
        case SIOCSIFDSTADDR:
                        ifp->if_flags |= IFF_UP;
                else
                        error = EAFNOSUPPORT;
                break;
 
        case SIOCSIFDSTADDR:
-               if (ifa->ifa_addr.sa_family != AF_INET)
+               if (ifa->ifa_addr->sa_family != AF_INET)
                        error = EAFNOSUPPORT;
                break;
 
                        error = EAFNOSUPPORT;
                break;
 
+       case SIOCADDMULTI:
+       case SIOCDELMULTI:
+               ifr = (struct ifreq *)data;
+               if (ifr == 0) {
+                       error = EAFNOSUPPORT;           /* XXX */
+                       break;
+               }
+               switch (ifr->ifr_addr.sa_family) {
+
+#ifdef INET
+               case AF_INET:
+                       break;
+#endif
+
+               default:
+                       error = EAFNOSUPPORT;
+                       break;
+               }
+               break;
+#endif
+
        default:
                error = EINVAL;
        }
        splx(s);
        return (error);
 }
        default:
                error = EINVAL;
        }
        splx(s);
        return (error);
 }
-#endif