minor cleanups
[unix-history] / usr / src / sys / net / if_sl.c
index ad559a8..af2fb61 100644 (file)
@@ -1,3 +1,22 @@
+/*
+ * Copyright (c) 1987, 1989 Regents of the University of California.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms are permitted
+ * provided that the above copyright notice and this paragraph are
+ * duplicated in all such forms and that any documentation,
+ * advertising materials, and other materials related to such
+ * distribution and use acknowledge that the software was developed
+ * by the University of California, Berkeley.  The name of the
+ * University may not be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
+ * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ *     @(#)if_sl.c     7.17 (Berkeley) %G%
+ */
+
 /*
  * Serial Line interface
  *
 /*
  * Serial Line interface
  *
  * rick@seismo.ARPA
  * seismo!rick
  *
  * rick@seismo.ARPA
  * seismo!rick
  *
- * Some things done here could obviously be done in a better way,
- * but they were done this way to minimize the number of files
- * that had to be changed to accomodate this device.
- * A lot of this code belongs in the tty driver.
- *
  * Pounded on heavily by Chris Torek (chris@mimsy.umd.edu, umcp-cs!chris).
  * Pounded on heavily by Chris Torek (chris@mimsy.umd.edu, umcp-cs!chris).
- * N.B.: this belongs in netinet, not vaxif, the way it stands now.
+ * N.B.: this belongs in netinet, not net, the way it stands now.
+ * Should have a link-layer type designation, but wouldn't be
+ * backwards-compatible.
  *
  * Converted to 4.3BSD Beta by Chris Torek.
  *
  * 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 $ */
+/* $Header: if_sl.c,v 1.7 89/05/31 02:24:52 van Exp $ */
 /* from if_sl.c,v 1.11 84/10/04 12:54:47 rick Exp */
 
 #include "sl.h"
 #if NSL > 0
 
 #include "param.h"
 /* from if_sl.c,v 1.11 84/10/04 12:54:47 rick Exp */
 
 #include "sl.h"
 #if NSL > 0
 
 #include "param.h"
+#include "dir.h"
+#include "user.h"
 #include "mbuf.h"
 #include "buf.h"
 #include "mbuf.h"
 #include "buf.h"
+#include "dk.h"
 #include "socket.h"
 #include "ioctl.h"
 #include "socket.h"
 #include "ioctl.h"
+#include "file.h"
 #include "tty.h"
 #include "tty.h"
+#include "kernel.h"
+#include "conf.h"
 #include "errno.h"
 
 #include "errno.h"
 
-#include "../net/if.h"
-#include "../net/netisr.h"
-#include "../net/route.h"
+#include "if.h"
+#include "netisr.h"
+#include "route.h"
+#if INET
 #include "../netinet/in.h"
 #include "../netinet/in_systm.h"
 #include "../netinet/in.h"
 #include "../netinet/in_systm.h"
+#include "../netinet/in_var.h"
 #include "../netinet/ip.h"
 #include "../netinet/ip.h"
-#include "../netinet/ip_var.h"
+#endif
 
 
-#ifdef vax
-#include "../vax/mtpr.h"
-#endif vax
+#include "machine/mtpr.h"
+
+#include "slcompress.h"
+#include "if_slvar.h"
 
 /*
 
 /*
- * N.B.: SLMTU is now a hard limit on input packet size.  Some limit
- * is required, lest we use up all mbufs in the case of deleterious data
- * dribbling down the line.
+ * SLMTU is a hard limit on input packet size.  To simplify the code
+ * and improve performance, we require that packets fit in an mbuf
+ * cluster, that there be enough extra room for the ifnet pointer that
+ * IP input requires 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, SLMTU can be at most
+ *     MCLBYTES - 128 
+ *
+ * To insure we get good interactive response, the MTU 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 256.
+ * (Since TCP will send 212 byte segments (to allow for 40 byte
+ * headers), the typical packet size on the wire will be around 220
+ * 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).
  */
  */
-#define        SLMTU   1006
-
-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[SLMTU];  /* 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        SLMTU           576
+#define BUFOFFSET      128
+#define        SLBUFSIZE       (SLMTU + BUFOFFSET)
+#define        SLIP_HIWAT      1024    /* don't start a new packet if HIWAT on queue */
+#define        CLISTRESERVE    1024    /* Can't let clists get too low */
+
+/*
+ * SLIP ABORT ESCAPE MECHANISM:
+ *     (inspired by HAYES modem escape arrangement)
+ *     1sec escape 1sec escape 1sec escape { 1sec escape 1sec escape }
+ *     signals a "soft" exit from slip mode by usermode process
+ */
+
+#define        ABT_ESC         '\033'  /* can't be t_intr - distant host must know it*/
+#define ABT_WAIT       1       /* in seconds - idle before an escape & after */
+#define ABT_RECYCLE    (5*2+2) /* in seconds - time window processing abort */
+
+#define ABT_SOFT       3       /* count of escapes */
+
+/*
+ * The following disgusting hack gets around the problem that IP TOS
+ * can't be set yet.  We want to put "interactive" traffic on a high
+ * priority queue.  To decide if traffic is interactive, we check that
+ * a) it is TCP and b) one of its ports is telnet, rlogin or ftp control.
+ */
+static u_short interactive_ports[8] = {
+       0,      513,    0,      0,
+       0,      21,     0,      23,
+};
+#define INTERACTIVE(p) (interactive_ports[(p) & 7] == (p))
+
+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
 
@@ -90,36 +165,63 @@ slattach()
                sc->sc_if.if_flags = IFF_POINTOPOINT;
                sc->sc_if.if_ioctl = slioctl;
                sc->sc_if.if_output = sloutput;
                sc->sc_if.if_flags = IFF_POINTOPOINT;
                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 - SLMTU;
+       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.
  */
 /*
  * Line specific open routine.
  * Attach the given tty to the first available sl unit.
  */
+/* ARGSUSED */
 slopen(dev, tp)
        dev_t dev;
        register struct tty *tp;
 {
        register struct sl_softc *sc;
        register int nsl;
 slopen(dev, tp)
        dev_t dev;
        register struct tty *tp;
 {
        register struct sl_softc *sc;
        register int nsl;
+       int error;
+
+       if (error = suser(u.u_cred, &u.u_acflag))
+               return (error);
 
 
-       if (tp->t_sc != NULL)
-               return (EBUSY);
+       if (tp->t_line == SLIPDISC)
+               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;
-                       sc->sc_mp = sc->sc_buf;
+                       if (slinit(sc) == 0)
+                               return (ENOBUFS);
                        tp->t_sc = (caddr_t)sc;
                        sc->sc_ttyp = tp;
                        tp->t_sc = (caddr_t)sc;
                        sc->sc_ttyp = tp;
+                       ttyflush(tp, FREAD | FWRITE);
                        return (0);
                }
                        return (0);
                }
-
-       return (ENOSPC);
+       return (ENXIO);
 }
 
 /*
 }
 
 /*
@@ -134,13 +236,17 @@ 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((caddr_t)(sc->sc_ep - SLBUFSIZE));
+               sc->sc_ep = 0;
+               sc->sc_mp = 0;
+               sc->sc_buf = 0;
        }
        splx(s);
 }
        }
        splx(s);
 }
@@ -149,27 +255,49 @@ slclose(tp)
  * Line specific (tty) ioctl routine.
  * Provide a way to get the sl unit number.
  */
  * Line specific (tty) ioctl routine.
  * Provide a way to get the sl unit number.
  */
+/* ARGSUSED */
 sltioctl(tp, cmd, data, flag)
        struct tty *tp;
        caddr_t data;
 {
 sltioctl(tp, cmd, data, flag)
        struct tty *tp;
        caddr_t data;
 {
+       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 TIOCGETD:                          /* XXX */
+       case SLIOGUNIT:
+               *(int *)data = sc->sc_if.if_unit;
+               break;
+
+       case SLIOCGFLAGS:
+               *(int *)data = sc->sc_flags;
+               break;
+
+       case SLIOCSFLAGS:
+#define        SC_MASK (SC_COMPRESS|SC_NOICMP)
+               s = splimp();
+               sc->sc_flags =
+                   (sc->sc_flags &~ SC_MASK) | ((*(int *)data) & SC_MASK);
+               splx(s);
+               break;
+
+       default:
+               return (-1);
        }
        }
-       return (-1);
+       return (0);
 }
 
 /*
  * Queue a packet.  Start transmission if not active.
  */
 sloutput(ifp, m, dst)
 }
 
 /*
  * Queue a packet.  Start transmission if not active.
  */
 sloutput(ifp, m, dst)
-       register struct ifnet *ifp;
+       struct ifnet *ifp;
        register struct mbuf *m;
        struct sockaddr *dst;
 {
        register struct mbuf *m;
        struct sockaddr *dst;
 {
-       register struct sl_softc *sc;
+       register struct sl_softc *sc = &sl_softc[ifp->if_unit];
+       register struct ip *ip;
+       register struct ifqueue *ifq;
        int s;
 
        /*
        int s;
 
        /*
@@ -177,31 +305,54 @@ 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);
                return (EAFNOSUPPORT);
        }
 
                        dst->sa_family);
                m_freem(m);
                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 */
        }
+       if ((sc->sc_ttyp->t_state & TS_CARR_ON) == 0) {
+               m_freem(m);
+               return (EHOSTUNREACH);
+       }
+       ifq = &sc->sc_if.if_snd;
+       if ((ip = mtod(m, struct ip *))->ip_p == IPPROTO_TCP) {
+               register int p = ((int *)ip)[ip->ip_hl];
+
+               if (INTERACTIVE(p & 0xffff) || INTERACTIVE(p >> 16))
+                       ifq = &sc->sc_fastq;
+
+               if (sc->sc_flags & SC_COMPRESS) {
+                       /* if two copies of sl_compress_tcp are running
+                        * for the same line, the compression state can
+                        * get screwed up.  We're assuming that sloutput
+                        * was invoked at splnet so this isn't possible
+                        * (this assumption is correct for 4.xbsd, x<=4).
+                        * In a multi-threaded kernel, a lockout might
+                        * be needed here. */
+                       p = sl_compress_tcp(m, ip, &sc->sc_comp);
+                       *mtod(m, u_char *) |= p;
+               }
+       } else if (sc->sc_flags & SC_NOICMP && ip->ip_p == IPPROTO_ICMP) {
+               m_freem(m);
+               return (0);
+       }
        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);
-               sc->sc_if.if_collisions++;
+               splx(s);
+               sc->sc_if.if_oerrors++;
                return (ENOBUFS);
        }
                return (ENOBUFS);
        }
-       IF_ENQUEUE(&ifp->if_snd, m);
-       if ((sc->sc_flags & SC_OACTIVE) == 0) {
-               splx(s);
+       IF_ENQUEUE(ifq, m);
+       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);
 }
 
@@ -215,141 +366,168 @@ slstart(tp)
 {
        register struct sl_softc *sc = (struct sl_softc *)tp->t_sc;
        register struct mbuf *m;
 {
        register struct sl_softc *sc = (struct sl_softc *)tp->t_sc;
        register struct mbuf *m;
-       register int c, len;
-       register u_char *mcp;
-       int flush;
-
-       /*
-        * If there is more in the output queue, just send it now.
-        * We are being called in lieu of ttstart and must do what
-        * it would.
-        */
-       if (tp->t_outq.c_cc > 0) {
-               ttstart(tp);
-               return;
-       }
-
-       /*
-        * This happens briefly when the line shuts down.
-        */
-       if (sc == NULL)
-               return;
+       register u_char *cp;
+       int s;
+       struct mbuf *m2;
+       extern int cfreecount;
 
 
-       /*
-        * Get a packet and map it to the interface.
-        */
-       c = splimp();
-       IF_DEQUEUE(&sc->sc_if.if_snd, m);
-       if (m == NULL) {
-               sc->sc_flags &= ~SC_OACTIVE;
-               splx(c);
-               return;
-       }
-       flush = !(sc->sc_flags & SC_OACTIVE);
-       sc->sc_flags |= SC_OACTIVE;
-       splx(c);
+       for (;;) {
+               /*
+                * If there is more in the output queue, just send it now.
+                * We are being called in lieu of ttstart and must do what
+                * it would.
+                */
+               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.
+                */
+               if (sc == NULL)
+                       return;
 
 
-       /*
-        * 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)
-               (void) putc(FRAME_END, &tp->t_outq);
-
-       while (m != NULL) {
-               len = m->m_len;
-               mcp = mtod(m, u_char *);
-               while (--len >= 0) {
-                       c = *mcp++;
-                       if (c == FRAME_ESCAPE || c == FRAME_END) {
-                               if (putc(FRAME_ESCAPE, &tp->t_outq))
-                                       goto full;
-                               c = c == FRAME_ESCAPE ? TRANS_FRAME_ESCAPE :
-                                                       TRANS_FRAME_END;
-                               if (putc(c, &tp->t_outq)) {
-                                       (void) unputc(&tp->t_outq);
-                                       goto full;
-                               }
-                       } else
-                               if (putc(c, &tp->t_outq))
-                                       goto full;
+               /*
+                * Get a packet and send it to the interface.
+                */
+               s = splimp();
+               IF_DEQUEUE(&sc->sc_fastq, m);
+               if (m == NULL)
+                       IF_DEQUEUE(&sc->sc_if.if_snd, m);
+               splx(s);
+               if (m == NULL)
+                       return;
+               /*
+                * If system is getting low on clists, just flush our
+                * output queue (if the stuff was important, it'll get
+                * retransmitted).
+                */
+               if (cfreecount < CLISTRESERVE + SLMTU) {
+                       m_freem(m);
+                       sc->sc_if.if_collisions++;
+                       continue;
                }
                }
-               m = m_free(m);
-       }
 
 
-       if (putc(FRAME_END, &tp->t_outq)) {
-full:
                /*
                /*
-                * If you get many oerrors (more than one or two a day)
-                * you probably do not have enough clists and you should 
-                * increase "nclist" in param.c.
+                * 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.
                 */
                 */
-               (void) unputc(&tp->t_outq);     /* make room */
-               putc(FRAME_END, &tp->t_outq);   /* end the packet */
-               sc->sc_if.if_oerrors++;
-       } else
-               sc->sc_if.if_opackets++;
+               if (tp->t_outq.c_cc == 0) {
+                       ++sc->sc_bytessent;
+                       (void) putc(FRAME_END, &tp->t_outq);
+               }
 
 
-       /*
-        * Start transmission.  Note that slstart, not ttstart, will be
-        * called when the transmission completes, be that after a single
-        * piece of what we have mapped, or be it after the entire thing
-        * has been sent.  That is why we need to check the output queue
-        * count at the top.
-        */
-       ttstart(tp);
+               while (m) {
+                       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.
+                                */
+                               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.
+                                        */
+                                       if (b_to_q((char *)bp, cp - bp, &tp->t_outq))
+                                               break;
+                                       sc->sc_bytessent += cp - bp;
+                               }
+                               /*
+                                * If there are characters left in the mbuf,
+                                * the first one must be special..
+                                * Put it out in a different form.
+                                */
+                               if (cp < ep) {
+                                       if (putc(FRAME_ESCAPE, &tp->t_outq))
+                                               break;
+                                       if (putc(*cp++ == FRAME_ESCAPE ?
+                                          TRANS_FRAME_ESCAPE : TRANS_FRAME_END,
+                                          &tp->t_outq)) {
+                                               (void) unputc(&tp->t_outq);
+                                               break;
+                                       }
+                                       sc->sc_bytessent += 2;
+                               }
+                       }
+                       MFREE(m, m2);
+                       m = m2;
+               }
+
+               if (putc(FRAME_END, &tp->t_outq)) {
+                       /*
+                        * Not enough room.  Remove a char to make room
+                        * and end the packet normally.
+                        * If you get many collisions (more than one or two
+                        * a day) you probably do not have enough clists
+                        * and you should increase "nclist" in param.c.
+                        */
+                       (void) unputc(&tp->t_outq);
+                       (void) putc(FRAME_END, &tp->t_outq);
+                       sc->sc_if.if_collisions++;
+               } else {
+                       ++sc->sc_bytessent;
+                       sc->sc_if.if_opackets++;
+               }
+       }
 }
 
 /*
 }
 
 /*
- * Copy data buffer to mbuf chain; add ifnet pointer ifp.
+ * Copy data buffer to mbuf chain; add ifnet pointer.
  */
  */
-struct mbuf *
-sl_btom(addr, len, ifp)
-       register caddr_t addr;
+static struct mbuf *
+sl_btom(sc, len)
+       register struct sl_softc *sc;
        register int len;
        register int len;
-       struct ifnet *ifp;
 {
 {
-       register struct mbuf *m, **mp;
-       register int count;
-       struct mbuf *top = NULL;
-
-       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);
-                       count = MIN(len, MLEN - sizeof(ifp));
-               } else {
-                       if (len >= NBPG) {
-                               struct mbuf *p;
-
-                               MCLGET(p, 1);
-                               if (p != NULL) {
-                                       count = MIN(len, CLBYTES);
-                                       m->m_off = (int)p - (int)m;
-                               } else
-                                       count = MIN(len, MLEN);
-                       } else
-                               count = MIN(len, MLEN);
-               }
-               bcopy(addr, mtod(m, caddr_t), count);
-               m->m_len = count;
-               if (ifp) {
-                       m->m_off -= sizeof(ifp);
-                       m->m_len += sizeof(ifp);
-                       *mtod(m, struct ifnet **) = ifp;
-                       ifp = NULL;
-               }
-               addr += 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);
 }
 
 /*
 }
 
 /*
@@ -361,69 +539,105 @@ slinput(c, tp)
 {
        register struct sl_softc *sc;
        register struct mbuf *m;
 {
        register struct sl_softc *sc;
        register struct mbuf *m;
+       register int len;
        int s;
 
        int s;
 
+       tk_nin++;
        sc = (struct sl_softc *)tp->t_sc;
        if (sc == NULL)
                return;
        sc = (struct sl_softc *)tp->t_sc;
        if (sc == NULL)
                return;
+       if (!(tp->t_state&TS_CARR_ON))  /* XXX */
+               return;
+
+       ++sc->sc_bytesrcvd;
+       c &= 0xff;                      /* XXX */
+
+#ifdef ABT_ESC
+       if (sc->sc_flags & SC_ABORT) {
+               /* if we see an abort after "idle" time, count it */
+               if (c == ABT_ESC && time.tv_sec >= sc->sc_lasttime + ABT_WAIT) {
+                       sc->sc_abortcount++;
+                       /* record when the first abort escape arrived */
+                       if (sc->sc_abortcount == 1)
+                               sc->sc_starttime = time.tv_sec;
+               }
+               /*
+                * if we have an abort, see that we have not run out of time,
+                * or that we have an "idle" time after the complete escape
+                * sequence
+                */
+               if (sc->sc_abortcount) {
+                       if (time.tv_sec >= sc->sc_starttime + ABT_RECYCLE)
+                               sc->sc_abortcount = 0;
+                       if (sc->sc_abortcount >= ABT_SOFT &&
+                           time.tv_sec >= sc->sc_lasttime + ABT_WAIT) {
+                               slclose(tp);
+                               return;
+                       }
+               }
+               sc->sc_lasttime = time.tv_sec;
+       }
+#endif
 
 
-       c &= 0xff;
-       if (sc->sc_flags & SC_ESCAPED) {
-               sc->sc_flags &= ~SC_ESCAPED;
-               switch (c) {
+       switch (c) {
 
 
-               case TRANS_FRAME_ESCAPE:
+       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;
-
-               default:
-                       sc->sc_if.if_ierrors++;
-                       sc->sc_mp = sc->sc_buf;
-                       sc->sc_ilen = 0;
-                       return;
-               }
-       } else {
-               switch (c) {
+               break;
 
 
-               case FRAME_END:
-                       if (sc->sc_ilen == 0)   /* ignore */
-                               return;
-                       m = sl_btom(sc->sc_buf, sc->sc_ilen, &sc->sc_if);
-                       if (m == NULL) {
-                               sc->sc_if.if_ierrors++;
-                               return;
-                       }
-                       sc->sc_mp = sc->sc_buf;
-                       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_ESCAPE:
+               sc->sc_escape = 1;
+               return;
 
 
-               case FRAME_ESCAPE:
-                       sc->sc_flags |= SC_ESCAPED;
-                       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 */
+                       len = sl_uncompress_tcp(&sc->sc_buf, len, (u_int)c,
+                                               &sc->sc_comp);
+                       if (len <= 0)
+                               goto error;
                }
                }
+               m = sl_btom(sc, len);
+               if (m == NULL)
+                       goto error;
+
+               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);
+               goto newpack;
        }
        }
-       if (++sc->sc_ilen >= SLMTU) {
-               sc->sc_if.if_ierrors++;
-               sc->sc_mp = sc->sc_buf;
-               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 - SLMTU;
+       sc->sc_escape = 0;
 }
 
 /*
 }
 
 /*
@@ -440,14 +654,14 @@ slioctl(ifp, cmd, data)
        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;