The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/net/if_strip.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*      $NetBSD: if_strip.c,v 1.58 2005/02/26 22:45:09 perry Exp $      */
    2 /*      from: NetBSD: if_sl.c,v 1.38 1996/02/13 22:00:23 christos Exp $ */
    3 
    4 /*
    5  * Copyright 1996 The Board of Trustees of The Leland Stanford
    6  * Junior University. All Rights Reserved.
    7  *
    8  * Permission to use, copy, modify, and distribute this
    9  * software and its documentation for any purpose and without
   10  * fee is hereby granted, provided that the above copyright
   11  * notice appear in all copies.  Stanford University
   12  * makes no representations about the suitability of this
   13  * software for any purpose.  It is provided "as is" without
   14  * express or implied warranty.
   15  *
   16  *
   17  * This driver was contributed by Jonathan Stone.
   18  *
   19  * Starmode Radio IP interface (STRIP) for Metricom wireless radio.
   20  * This STRIP driver assumes address resolution of IP addresses to
   21  * Metricom MAC addresses is done via local link-level routes.
   22  * The link-level addresses are entered as an 8-digit packed BCD number.
   23  * To add a route for a radio at IP address 10.1.2.3, with radio
   24  * address '1234-5678', reachable via interface strip0, use the command
   25  *
   26  *      route add -host 10.1.2.3  -link strip0:12:34:56:78
   27  */
   28 
   29 
   30 /*
   31  * Copyright (c) 1987, 1989, 1992, 1993
   32  *      The Regents of the University of California.  All rights reserved.
   33  *
   34  * Redistribution and use in source and binary forms, with or without
   35  * modification, are permitted provided that the following conditions
   36  * are met:
   37  * 1. Redistributions of source code must retain the above copyright
   38  *    notice, this list of conditions and the following disclaimer.
   39  * 2. Redistributions in binary form must reproduce the above copyright
   40  *    notice, this list of conditions and the following disclaimer in the
   41  *    documentation and/or other materials provided with the distribution.
   42  * 3. Neither the name of the University nor the names of its contributors
   43  *    may be used to endorse or promote products derived from this software
   44  *    without specific prior written permission.
   45  *
   46  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   47  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   48  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   49  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   50  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   51  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   52  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   53  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   54  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   55  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   56  * SUCH DAMAGE.
   57  *
   58  *      @(#)if_sl.c     8.6 (Berkeley) 2/1/94
   59  */
   60 
   61 /*
   62  * Derived from: Serial Line interface written by Rick Adams (rick@seismo.gov)
   63  *
   64  * Rick Adams
   65  * Center for Seismic Studies
   66  * 1300 N 17th Street, Suite 1450
   67  * Arlington, Virginia 22209
   68  * (703)276-7900
   69  * rick@seismo.ARPA
   70  * seismo!rick
   71  *
   72  * Pounded on heavily by Chris Torek (chris@mimsy.umd.edu, umcp-cs!chris).
   73  * N.B.: this belongs in netinet, not net, the way it stands now.
   74  * Should have a link-layer type designation, but wouldn't be
   75  * backwards-compatible.
   76  *
   77  * Converted to 4.3BSD Beta by Chris Torek.
   78  * Other changes made at Berkeley, based in part on code by Kirk Smith.
   79  * W. Jolitz added slip abort.
   80  *
   81  * Hacked almost beyond recognition by Van Jacobson (van@helios.ee.lbl.gov).
   82  * Added priority queuing for "interactive" traffic; hooks for TCP
   83  * header compression; ICMP filtering (at 2400 baud, some cretin
   84  * pinging you can use up all your bandwidth).  Made low clist behavior
   85  * more robust and slightly less likely to hang serial line.
   86  * Sped up a bunch of things.
   87  */
   88 
   89 #include <sys/cdefs.h>
   90 __KERNEL_RCSID(0, "$NetBSD: if_strip.c,v 1.58 2005/02/26 22:45:09 perry Exp $");
   91 
   92 #include "opt_inet.h"
   93 #include "bpfilter.h"
   94 
   95 #include <sys/param.h>
   96 #include <sys/proc.h>
   97 #include <sys/mbuf.h>
   98 #include <sys/buf.h>
   99 #include <sys/dkstat.h>
  100 #include <sys/socket.h>
  101 #include <sys/ioctl.h>
  102 #include <sys/file.h>
  103 #include <sys/conf.h>
  104 #include <sys/tty.h>
  105 #include <sys/kernel.h>
  106 #if __NetBSD__
  107 #include <sys/systm.h>
  108 #include <sys/callout.h>
  109 #endif
  110 #include <sys/syslog.h>
  111 
  112 #include <machine/cpu.h>
  113 #include <machine/intr.h>
  114 
  115 #include <net/if.h>
  116 #include <net/if_dl.h>
  117 #include <net/if_types.h>
  118 #include <net/netisr.h>
  119 #include <net/route.h>
  120 
  121 #ifdef INET
  122 #include <netinet/in.h>
  123 #include <netinet/in_systm.h>
  124 #include <netinet/in_var.h>
  125 #include <netinet/ip.h>
  126 #endif
  127 
  128 #include <net/slcompress.h>
  129 #include <net/if_stripvar.h>
  130 #include <net/slip.h>
  131 
  132 #ifdef __NetBSD__       /* XXX -- jrs */
  133 typedef u_char ttychar_t;
  134 #else
  135 typedef char ttychar_t;
  136 #endif
  137 
  138 #if NBPFILTER > 0
  139 #include <sys/time.h>
  140 #include <net/bpf.h>
  141 #endif
  142 
  143 /*
  144  * SLMAX is a hard limit on input packet size.  To simplify the code
  145  * and improve performance, we require that packets fit in an mbuf
  146  * cluster, and if we get a compressed packet, there's enough extra
  147  * room to expand the header into a max length tcp/ip header (128
  148  * bytes).  So, SLMAX can be at most
  149  *      MCLBYTES - 128
  150  *
  151  * SLMTU is a hard limit on output packet size.  To insure good
  152  * interactive response, SLMTU wants to be the smallest size that
  153  * amortizes the header cost.  Remember that even with
  154  * type-of-service queuing, we have to wait for any in-progress
  155  * packet to finish.  I.e., we wait, on the average, 1/2 * mtu /
  156  * cps, where cps is the line speed in characters per second.
  157  * E.g., 533ms wait for a 1024 byte MTU on a 9600 baud line.  The
  158  * average compressed header size is 6-8 bytes so any MTU > 90
  159  * bytes will give us 90% of the line bandwidth.  A 100ms wait is
  160  * tolerable (500ms is not), so want an MTU around 296.  (Since TCP
  161  * will send 256 byte segments (to allow for 40 byte headers), the
  162  * typical packet size on the wire will be around 260 bytes).  In
  163  * 4.3tahoe+ systems, we can set an MTU in a route so we do that &
  164  * leave the interface MTU relatively high (so we don't IP fragment
  165  * when acting as a gateway to someone using a stupid MTU).
  166  *
  167  * Similar considerations apply to SLIP_HIWAT:  It's the amount of
  168  * data that will be queued 'downstream' of us (i.e., in clists
  169  * waiting to be picked up by the tty output interrupt).  If we
  170  * queue a lot of data downstream, it's immune to our t.o.s. queuing.
  171  * E.g., if SLIP_HIWAT is 1024, the interactive traffic in mixed
  172  * telnet/ftp will see a 1 sec wait, independent of the mtu (the
  173  * wait is dependent on the ftp window size but that's typically
  174  * 1k - 4k).  So, we want SLIP_HIWAT just big enough to amortize
  175  * the cost (in idle time on the wire) of the tty driver running
  176  * off the end of its clists & having to call back slstart for a
  177  * new packet.  For a tty interface with any buffering at all, this
  178  * cost will be zero.  Even with a totally brain dead interface (like
  179  * the one on a typical workstation), the cost will be <= 1 character
  180  * time.  So, setting SLIP_HIWAT to ~100 guarantees that we'll lose
  181  * at most 1% while maintaining good interactive response.
  182  */
  183 #define BUFOFFSET       (128+sizeof(struct ifnet **)+SLIP_HDRLEN)
  184 #define SLMAX           (MCLBYTES - BUFOFFSET)
  185 #define SLBUFSIZE       (SLMAX + BUFOFFSET)
  186 #define SLMTU           1100 /* XXX -- appromaximated. 1024 may be safer. */
  187 
  188 #define STRIP_MTU_ONWIRE (SLMTU + 20 + STRIP_HDRLEN) /* (2*SLMTU+2 in sl.c */
  189 
  190 
  191 #define SLIP_HIWAT      roundup(50,CBSIZE)
  192 
  193 /* This is a NetBSD-1.0 or later kernel. */
  194 #define CCOUNT(q)       ((q)->c_cc)
  195 
  196 
  197 #ifndef __NetBSD__                                      /* XXX - cgd */
  198 #define CLISTRESERVE    1024    /* Can't let clists get too low */
  199 #endif  /* !__NetBSD__ */
  200 
  201 /*
  202  * SLIP ABORT ESCAPE MECHANISM:
  203  *      (inspired by HAYES modem escape arrangement)
  204  *      1sec escape 1sec escape 1sec escape { 1sec escape 1sec escape }
  205  *      within window time signals a "soft" exit from slip mode by remote end
  206  *      if the IFF_DEBUG flag is on.
  207  */
  208 #define ABT_ESC         '\033'  /* can't be t_intr - distant host must know it*/
  209 #define ABT_IDLE        1       /* in seconds - idle before an escape */
  210 #define ABT_COUNT       3       /* count of escapes for abort */
  211 #define ABT_WINDOW      (ABT_COUNT*2+2) /* in seconds - time to count */
  212 
  213 static int              strip_clone_create(struct if_clone *, int);
  214 static int              strip_clone_destroy(struct ifnet *);
  215 
  216 static LIST_HEAD(, strip_softc) strip_softc_list;
  217 
  218 struct if_clone strip_cloner =
  219     IF_CLONE_INITIALIZER("strip", strip_clone_create, strip_clone_destroy);
  220 
  221 #define STRIP_FRAME_END         0x0D            /* carriage return */
  222 
  223 #ifndef __HAVE_GENERIC_SOFT_INTERRUPTS
  224 void    stripnetisr(void);
  225 #endif
  226 void    stripintr(void *);
  227 
  228 static int stripinit __P((struct strip_softc *));
  229 static  struct mbuf *strip_btom __P((struct strip_softc *, int));
  230 
  231 /*
  232  * STRIP header: '*' + modem address (dddd-dddd) + '*' + mactype ('SIP0')
  233  * A Metricom packet looks like this: *<address>*<key><payload><CR>
  234  *   eg. *0000-1164*SIP0<payload><CR>
  235  *
  236  */
  237 
  238 #define STRIP_ENCAP_SIZE(X) ((36) + (X)*65/64 + 2)
  239 #define STRIP_HDRLEN 15
  240 #define STRIP_MAC_ADDR_LEN 9
  241 
  242 /*
  243  * Star mode packet header.
  244  * (may be used for encapsulations other than STRIP.)
  245  */
  246 #define STARMODE_ADDR_LEN 11
  247 struct st_header {
  248         u_char starmode_addr[STARMODE_ADDR_LEN];
  249         u_char starmode_type[4];
  250 };
  251 
  252 /*
  253  * Forward declarations for Metricom-specific functions.
  254  * Ideally, these would be in a library and shared across
  255  * different STRIP implementations: *BSD, Linux, etc.
  256  *
  257  */
  258 static u_char* UnStuffData __P((u_char *src, u_char *end, u_char
  259                                 *dest, u_long dest_length));
  260 
  261 static u_char* StuffData __P((u_char *src, u_long length, u_char *dest,
  262                               u_char **code_ptr_ptr));
  263 
  264 static void RecvErr __P((char *msg, struct strip_softc *sc));
  265 static void RecvErr_Message __P((struct strip_softc *strip_info,
  266                                 u_char *sendername, u_char *msg));
  267 void    strip_resetradio __P((struct strip_softc *sc, struct tty *tp));
  268 void    strip_proberadio __P((struct strip_softc *sc, struct tty *tp));
  269 void    strip_watchdog __P((struct ifnet *ifp));
  270 void    strip_sendbody __P((struct strip_softc *sc, struct mbuf *m));
  271 int     strip_newpacket __P((struct strip_softc *sc, u_char *ptr, u_char *end));
  272 void    strip_send __P((struct strip_softc *sc, struct mbuf *m0));
  273 
  274 void    strip_timeout __P((void *x));
  275 
  276 #ifdef DEBUG
  277 #define DPRINTF(x)      printf x
  278 #else
  279 #define DPRINTF(x)
  280 #endif
  281 
  282 
  283 
  284 /*
  285  * Radio reset macros.
  286  * The Metricom radios are not particularly well-designed for
  287  * use in packet mode (starmode).  There's no easy way to tell
  288  * when the radio is in starmode.  Worse, when the radios are reset
  289  * or power-cycled, they come back up in Hayes AT-emulation mode,
  290  * and there's no good way for this driver to tell.
  291  * We deal with this by peridically tickling the radio
  292  * with an invalid starmode command.  If the radio doesn't
  293  * respond with an error, the driver knows to reset the radio.
  294  */
  295 
  296 /* Radio-reset finite state machine (if_watchdog) callback rate, in seconds */
  297 #define STRIP_WATCHDOG_INTERVAL 5
  298 
  299 /* Period between intrusive radio probes, in seconds */
  300 #define ST_PROBE_INTERVAL 10
  301 
  302 /* Grace period for radio to answer probe, in seconds */
  303 #define ST_PROBERESPONSE_INTERVAL 2
  304 
  305 /* Be less agressive about repeated resetting. */
  306 #define STRIP_RESET_INTERVAL 5
  307 
  308 /*
  309  * We received a response from the radio that indicates it's in
  310  * star mode.  Clear any pending probe or reset timer.
  311  * Don't  probe radio again for standard polling interval.
  312  */
  313 #define CLEAR_RESET_TIMER(sc) \
  314  do {\
  315     (sc)->sc_state = ST_ALIVE;  \
  316     (sc)->sc_statetimo = time.tv_sec + ST_PROBE_INTERVAL;       \
  317 } while (/*CONSTCOND*/ 0)
  318 
  319 /*
  320  * we received a response from the radio that indicates it's crashed
  321  * out of starmode into Hayse mode. Reset it ASAP.
  322  */
  323 #define FORCE_RESET(sc) \
  324  do {\
  325     (sc)->sc_statetimo = time.tv_sec - 1; \
  326     (sc)->sc_state = ST_DEAD;   \
  327     /*(sc)->sc_if.if_timer = 0;*/ \
  328  } while (/*CONSTCOND*/ 0)
  329 
  330 #define RADIO_PROBE_TIMEOUT(sc) \
  331          ((sc)-> sc_statetimo > time.tv_sec)
  332 
  333 
  334 
  335 void
  336 stripattach(void)
  337 {
  338         LIST_INIT(&strip_softc_list);
  339         if_clone_attach(&strip_cloner);
  340 }
  341 
  342 static int
  343 strip_clone_create(struct if_clone *ifc, int unit)
  344 {
  345         struct strip_softc *sc;
  346 
  347         MALLOC(sc, struct strip_softc *, sizeof(*sc), M_DEVBUF, M_WAIT|M_ZERO);
  348         sc->sc_unit = unit;
  349         (void)snprintf(sc->sc_if.if_xname, sizeof(sc->sc_if.if_xname),
  350             "%s%d", ifc->ifc_name, unit);
  351         callout_init(&sc->sc_timo_ch);
  352         sc->sc_if.if_softc = sc;
  353         sc->sc_if.if_mtu = SLMTU;
  354         sc->sc_if.if_flags = 0;
  355         sc->sc_if.if_type = IFT_OTHER;
  356 #if 0
  357         sc->sc_if.if_flags |= SC_AUTOCOMP /* | IFF_POINTOPOINT | IFF_MULTICAST*/;
  358 #endif
  359         sc->sc_if.if_type = IFT_SLIP;
  360         sc->sc_if.if_ioctl = stripioctl;
  361         sc->sc_if.if_output = stripoutput;
  362         sc->sc_if.if_dlt = DLT_SLIP;
  363         sc->sc_fastq.ifq_maxlen = 32;
  364         IFQ_SET_READY(&sc->sc_if.if_snd);
  365 
  366         sc->sc_if.if_watchdog = strip_watchdog;
  367         if_attach(&sc->sc_if);
  368         if_alloc_sadl(&sc->sc_if);
  369 #if NBPFILTER > 0
  370         bpfattach(&sc->sc_if, DLT_SLIP, SLIP_HDRLEN);
  371 #endif
  372         LIST_INSERT_HEAD(&strip_softc_list, sc, sc_iflist);
  373         return 0;
  374 }
  375 
  376 static int
  377 strip_clone_destroy(struct ifnet *ifp)
  378 {
  379         struct strip_softc *sc = (struct strip_softc *)ifp->if_softc;
  380 
  381         if (sc->sc_ttyp != NULL)
  382                 return EBUSY;   /* Not removing it */
  383 
  384         LIST_REMOVE(sc, sc_iflist);
  385 
  386 #if NBPFILTER > 0
  387         bpfdetach(ifp);
  388 #endif
  389         if_detach(ifp);
  390 
  391         FREE(sc, M_DEVBUF);
  392         return 0;
  393 }
  394 
  395 static int
  396 stripinit(sc)
  397         struct strip_softc *sc;
  398 {
  399         u_char *p;
  400 
  401         if (sc->sc_mbuf == NULL) {
  402                 sc->sc_mbuf = m_get(M_WAIT, MT_DATA);
  403                 m_clget(sc->sc_mbuf, M_WAIT);
  404         }
  405         sc->sc_ep = (u_char *) sc->sc_mbuf->m_ext.ext_buf +
  406             sc->sc_mbuf->m_ext.ext_size;
  407         sc->sc_mp = sc->sc_pktstart = (u_char *) sc->sc_mbuf->m_ext.ext_buf +
  408             BUFOFFSET;
  409 
  410         /* Get contiguous buffer in which to de-bytestuff/rll-decode input */
  411         if (sc->sc_rxbuf == NULL) {
  412                 p = (u_char *)malloc(MCLBYTES, M_DEVBUF, M_WAITOK);
  413                 if (p)
  414                         sc->sc_rxbuf = p + SLBUFSIZE - SLMAX;
  415                 else {
  416                         printf("%s: can't allocate input buffer\n",
  417                             sc->sc_if.if_xname);
  418                         sc->sc_if.if_flags &= ~IFF_UP;
  419                         return (0);
  420                 }
  421         }
  422 
  423         /* Get contiguous buffer in which to bytestuff/rll-encode output */
  424         if (sc->sc_txbuf == NULL) {
  425                 p = (u_char *)malloc(MCLBYTES, M_DEVBUF, M_WAITOK);
  426                 if (p)
  427                         sc->sc_txbuf = (u_char *)p + SLBUFSIZE - SLMAX;
  428                 else {
  429                         printf("%s: can't allocate buffer\n",
  430                             sc->sc_if.if_xname);
  431 
  432                         sc->sc_if.if_flags &= ~IFF_UP;
  433                         return (0);
  434                 }
  435         }
  436 
  437 #ifdef INET
  438         sl_compress_init(&sc->sc_comp);
  439 #endif
  440 
  441         /* Initialize radio probe/reset state machine */
  442         sc->sc_state = ST_DEAD;         /* assumet the worst. */
  443         sc->sc_statetimo = time.tv_sec; /* do reset immediately */
  444 
  445         return (1);
  446 }
  447 
  448 /*
  449  * Line specific open routine.
  450  * Attach the given tty to the first available sl unit.
  451  */
  452 /* ARGSUSED */
  453 int
  454 stripopen(dev, tp)
  455         dev_t dev;
  456         struct tty *tp;
  457 {
  458         struct proc *p = curproc;               /* XXX */
  459         struct strip_softc *sc;
  460         int error;
  461 #ifdef __NetBSD__
  462         int s;
  463 #endif
  464 
  465         if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
  466                 return (error);
  467 
  468         if (tp->t_linesw->l_no == STRIPDISC)
  469                 return (0);
  470 
  471         LIST_FOREACH(sc, &strip_softc_list, sc_iflist) {
  472                 if (sc->sc_ttyp == NULL) {
  473 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
  474                         sc->sc_si = softintr_establish(IPL_SOFTNET,
  475                             stripintr, sc);
  476 #endif
  477                         if (stripinit(sc) == 0) {
  478 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
  479                                 softintr_disestablish(sc->sc_si);
  480 #endif
  481                                 return (ENOBUFS);
  482                         }
  483                         tp->t_sc = (caddr_t)sc;
  484                         sc->sc_ttyp = tp;
  485                         sc->sc_if.if_baudrate = tp->t_ospeed;
  486                         ttyflush(tp, FREAD | FWRITE);
  487 #ifdef __NetBSD__
  488                         /*
  489                          * Make sure tty output queue is large enough
  490                          * to hold a full-sized packet (including frame
  491                          * end, and a possible extra frame end).
  492                          * A   full-sized   of 65/64) *SLMTU bytes (because
  493                          * of escapes and clever RLL bytestuffing),
  494                          * plus frame header, and add two on for frame ends.
  495                          */
  496                         s = spltty();
  497                         if (tp->t_outq.c_cn < STRIP_MTU_ONWIRE) {
  498                                 sc->sc_oldbufsize = tp->t_outq.c_cn;
  499                                 sc->sc_oldbufquot = tp->t_outq.c_cq != 0;
  500 
  501                                 clfree(&tp->t_outq);
  502                                 error = clalloc(&tp->t_outq, 3*SLMTU, 0);
  503                                 if (error) {
  504                                         splx(s);
  505 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
  506                                         softintr_disestablish(sc->sc_si);
  507 #endif
  508                                         /*
  509                                          * clalloc() might return -1 which
  510                                          * is no good, so we need to return
  511                                          * something else.
  512                                          */
  513                                         return (ENOMEM);
  514                                 }
  515                         } else
  516                                 sc->sc_oldbufsize = sc->sc_oldbufquot = 0;
  517                         splx(s);
  518 #endif /* __NetBSD__ */
  519                         s = spltty();
  520                         strip_resetradio(sc, tp);
  521                         splx(s);
  522 
  523                         /*
  524                          * Start the watchdog timer to get the radio
  525                          * "probe-for-death"/reset machine going.
  526                          */
  527                         sc->sc_if.if_timer = STRIP_WATCHDOG_INTERVAL;
  528 
  529                         return (0);
  530                 }
  531         }
  532         return (ENXIO);
  533 }
  534 
  535 /*
  536  * Line specific close routine.
  537  * Detach the tty from the strip unit.
  538  */
  539 void
  540 stripclose(tp)
  541         struct tty *tp;
  542 {
  543         struct strip_softc *sc;
  544         int s;
  545 
  546         ttywflush(tp);
  547         sc = tp->t_sc;
  548 
  549         if (sc != NULL) {
  550 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
  551                 softintr_disestablish(sc->sc_si);
  552 #endif
  553                 s = splnet();
  554                 /*
  555                  * Cancel watchdog timer, which stops the "probe-for-death"/
  556                  * reset machine.
  557                  */
  558                 sc->sc_if.if_timer = 0;
  559                 if_down(&sc->sc_if);
  560                 IF_PURGE(&sc->sc_fastq);
  561                 splx(s);
  562 
  563                 s = spltty();
  564                 tp->t_linesw = linesw[0];       /* default line disc. */
  565                 tp->t_state = 0;
  566 
  567                 sc->sc_ttyp = NULL;
  568                 tp->t_sc = NULL;
  569 
  570                 m_freem(sc->sc_mbuf);
  571                 sc->sc_mbuf = NULL;
  572                 sc->sc_ep = sc->sc_mp = sc->sc_pktstart = NULL;
  573                 IF_PURGE(&sc->sc_inq);
  574 
  575                 /* XXX */
  576                 free((caddr_t)(sc->sc_rxbuf - SLBUFSIZE + SLMAX), M_DEVBUF);
  577                 sc->sc_rxbuf = NULL;
  578 
  579                 /* XXX */
  580                 free((caddr_t)(sc->sc_txbuf - SLBUFSIZE + SLMAX), M_DEVBUF);
  581                 sc->sc_txbuf = NULL;
  582 
  583                 if (sc->sc_flags & SC_TIMEOUT) {
  584                         callout_stop(&sc->sc_timo_ch);
  585                         sc->sc_flags &= ~SC_TIMEOUT;
  586                 }
  587 
  588                 /*
  589                  * If necessary, install a new outq buffer of the
  590                  * appropriate size.
  591                  */
  592                 if (sc->sc_oldbufsize != 0) {
  593                         clfree(&tp->t_outq);
  594                         clalloc(&tp->t_outq, sc->sc_oldbufsize,
  595                             sc->sc_oldbufquot);
  596                 }
  597                 splx(s);
  598         }
  599 }
  600 
  601 /*
  602  * Line specific (tty) ioctl routine.
  603  * Provide a way to get the sl unit number.
  604  */
  605 /* ARGSUSED */
  606 int
  607 striptioctl(tp, cmd, data, flag)
  608         struct tty *tp;
  609         u_long cmd;
  610         caddr_t data;
  611         int flag;
  612 {
  613         struct strip_softc *sc = (struct strip_softc *)tp->t_sc;
  614 
  615         switch (cmd) {
  616         case SLIOCGUNIT:
  617                 *(int *)data = sc->sc_unit;
  618                 break;
  619 
  620         default:
  621                 return (EPASSTHROUGH);
  622         }
  623         return (0);
  624 }
  625 
  626 /*
  627  * Take an mbuf chain  containing a STRIP packet (no link-level header),
  628  * byte-stuff (escape) it, and enqueue it on the tty send queue.
  629  */
  630 void
  631 strip_sendbody(sc, m)
  632         struct strip_softc  *sc;
  633         struct mbuf *m;
  634 {
  635         struct tty *tp = sc->sc_ttyp;
  636         u_char *dp = sc->sc_txbuf;
  637         struct mbuf *m2;
  638         int len;
  639         u_char *rllstate_ptr = NULL;
  640 
  641         while (m) {
  642                 if (m->m_len != 0) {
  643                         /*
  644                          * Byte-stuff/run-length encode this mbuf's data
  645                          * into the output buffer.
  646                          * XXX Note that chained calls to stuffdata()
  647                          * require that the stuffed data be left in the
  648                          * output buffer until the entire packet is encoded.
  649                          */
  650                         dp = StuffData(mtod(m, u_char *), m->m_len, dp,
  651                             &rllstate_ptr);
  652                 }
  653                 MFREE(m, m2);
  654                 m = m2;
  655         }
  656 
  657         /*
  658          * Put the entire stuffed packet into the tty output queue.
  659          */
  660         len = dp - sc->sc_txbuf;
  661         if (b_to_q((ttychar_t *)sc->sc_txbuf, len, &tp->t_outq)) {
  662                 if (sc->sc_if.if_flags & IFF_DEBUG)
  663                         addlog("%s: tty output overflow\n",
  664                             sc->sc_if.if_xname);
  665                 return;
  666         }
  667         sc->sc_if.if_obytes += len;
  668 }
  669 
  670 /*
  671  * Send a STRIP packet.  Must be called at spltty().
  672  */
  673 void
  674 strip_send(sc, m0)
  675         struct strip_softc *sc;
  676         struct mbuf *m0;
  677 {
  678         struct tty *tp = sc->sc_ttyp;
  679         struct st_header *hdr;
  680 
  681         /*
  682          * Send starmode header (unstuffed).
  683          */
  684         hdr = mtod(m0, struct st_header *);
  685         if (b_to_q((ttychar_t *)hdr, STRIP_HDRLEN, &tp->t_outq)) {
  686                 if (sc->sc_if.if_flags & IFF_DEBUG)
  687                         addlog("%s: outq overflow writing header\n",
  688                                  sc->sc_if.if_xname);
  689                 m_freem(m0);
  690                 return;
  691         }
  692 
  693         m_adj(m0, sizeof(struct st_header));
  694 
  695         /* Byte-stuff and run-length encode the remainder of the packet. */
  696         strip_sendbody(sc, m0);
  697 
  698         if (putc(STRIP_FRAME_END, &tp->t_outq)) {
  699                 /*
  700                  * Not enough room.  Remove a char to make room
  701                  * and end the packet normally.
  702                  * If you get many collisions (more than one or two
  703                  * a day) you probably do not have enough clists
  704                  * and you should increase "nclist" in param.c.
  705                  */
  706                 (void) unputc(&tp->t_outq);
  707                 (void) putc(STRIP_FRAME_END, &tp->t_outq);
  708                 sc->sc_if.if_collisions++;
  709         } else {
  710                 ++sc->sc_if.if_obytes;
  711                 sc->sc_if.if_opackets++;
  712         }
  713 
  714         /*
  715          * If a radio probe is due now, append it to this packet rather
  716          * than waiting until the watchdog routine next runs.
  717          */
  718         if (time.tv_sec >= sc->sc_statetimo && sc->sc_state == ST_ALIVE)
  719                 strip_proberadio(sc, tp);
  720 }
  721 
  722 /*
  723  * Queue a packet.  Start transmission if not active.
  724  * Compression happens in stripintr(); if we do it here, IP TOS
  725  * will cause us to not compress "background" packets, because
  726  * ordering gets trashed.  It can be done for all packets in stripintr().
  727  */
  728 int
  729 stripoutput(ifp, m, dst, rt)
  730         struct ifnet *ifp;
  731         struct mbuf *m;
  732         struct sockaddr *dst;
  733         struct rtentry *rt;
  734 {
  735         struct strip_softc *sc = ifp->if_softc;
  736         struct ip *ip;
  737         struct st_header *shp;
  738         const u_char *dldst;            /* link-level next-hop */
  739         struct ifqueue *ifq;
  740         int s, error;
  741         u_char dl_addrbuf[STARMODE_ADDR_LEN+1];
  742         ALTQ_DECL(struct altq_pktattr pktattr;)
  743 
  744         /*
  745          * Verify tty line is up and alive.
  746          */
  747         if (sc->sc_ttyp == NULL) {
  748                 m_freem(m);
  749                 return (ENETDOWN);      /* sort of */
  750         }
  751         if ((sc->sc_ttyp->t_state & TS_CARR_ON) == 0 &&
  752             (sc->sc_ttyp->t_cflag & CLOCAL) == 0) {
  753                 m_freem(m);
  754                 return (EHOSTUNREACH);
  755         }
  756 
  757 #define SDL(a)          ((struct sockaddr_dl *) (a))
  758 
  759 #ifdef DEBUG
  760         if (rt) {
  761                 printf("stripout, rt: dst af%d gw af%d",
  762                     rt_key(rt)->sa_family, rt->rt_gateway->sa_family);
  763                 if (rt_key(rt)->sa_family == AF_INET)
  764                   printf(" dst %x",
  765                       ((struct sockaddr_in *)rt_key(rt))->sin_addr.s_addr);
  766                 printf("\n");
  767         }
  768 #endif
  769         switch (dst->sa_family) {
  770         case AF_INET:
  771                 if (rt != NULL && rt->rt_gwroute != NULL)
  772                         rt = rt->rt_gwroute;
  773 
  774                 /* assume rt is never NULL */
  775                 if (rt == NULL || rt->rt_gateway->sa_family != AF_LINK
  776                     || SDL(rt->rt_gateway)->sdl_alen != ifp->if_addrlen) {
  777                         DPRINTF(("strip: could not arp starmode addr %x\n",
  778                          ((struct sockaddr_in *)dst)->sin_addr.s_addr));
  779                         m_freem(m);
  780                         return (EHOSTUNREACH);
  781                 }
  782                 /*bcopy(LLADDR(SDL(rt->rt_gateway)), dldst, ifp->if_addrlen);*/
  783                 dldst = LLADDR(SDL(rt->rt_gateway));
  784                 break;
  785 
  786         case AF_LINK:
  787                 /*bcopy(LLADDR(SDL(rt->rt_gateway)), dldst, ifp->if_addrlen);*/
  788                 dldst = LLADDR(SDL(dst));
  789                 break;
  790 
  791         default:
  792                 /*
  793                  * `Cannot happen' (see stripioctl).  Someday we will extend
  794                  * the line protocol to support other address families.
  795                  */
  796                 printf("%s: af %d not supported\n", sc->sc_if.if_xname,
  797                     dst->sa_family);
  798                 m_freem(m);
  799                 sc->sc_if.if_noproto++;
  800                 return (EAFNOSUPPORT);
  801         }
  802 
  803         ip = mtod(m, struct ip *);
  804 #ifdef INET
  805         if (sc->sc_if.if_flags & SC_NOICMP && ip->ip_p == IPPROTO_ICMP) {
  806                 m_freem(m);
  807                 return (ENETRESET);             /* XXX ? */
  808         }
  809         if ((ip->ip_tos & IPTOS_LOWDELAY) != 0
  810 #ifdef ALTQ
  811             && ALTQ_IS_ENABLED(&ifp->if_snd) == 0
  812 #endif
  813             )
  814                 ifq = &sc->sc_fastq;
  815         else
  816 #endif
  817                 ifq = NULL;
  818 
  819         /*
  820          * Add local net header.  If no space in first mbuf,
  821          * add another.
  822          */
  823         M_PREPEND(m, sizeof(struct st_header), M_DONTWAIT);
  824         if (m == 0) {
  825                 DPRINTF(("strip: could not prepend starmode header\n"));
  826                 return (ENOBUFS);
  827         }
  828 
  829         /*
  830          * Unpack BCD route entry into an ASCII starmode address.
  831          */
  832         dl_addrbuf[0] = '*';
  833 
  834         dl_addrbuf[1] = ((dldst[0] >> 4) & 0x0f) + '';
  835         dl_addrbuf[2] = ((dldst[0]     ) & 0x0f) + '';
  836 
  837         dl_addrbuf[3] = ((dldst[1] >> 4) & 0x0f) + '';
  838         dl_addrbuf[4] = ((dldst[1]     ) & 0x0f) + '';
  839 
  840         dl_addrbuf[5] = '-';
  841 
  842         dl_addrbuf[6] = ((dldst[2] >> 4) & 0x0f) + '';
  843         dl_addrbuf[7] = ((dldst[2]     ) & 0x0f) + '';
  844 
  845         dl_addrbuf[8] = ((dldst[3] >> 4) & 0x0f) + '';
  846         dl_addrbuf[9] = ((dldst[3]     ) & 0x0f) + '';
  847 
  848         dl_addrbuf[10] = '*';
  849         dl_addrbuf[11] = 0;
  850         dldst = dl_addrbuf;
  851 
  852         shp = mtod(m, struct st_header *);
  853         bcopy((caddr_t)"SIP0", (caddr_t)&shp->starmode_type,
  854                 sizeof(shp->starmode_type));
  855 
  856         bcopy((const char *)dldst, (caddr_t)shp->starmode_addr,
  857                 sizeof (shp->starmode_addr));
  858 
  859         s = spltty();
  860         if (sc->sc_oqlen && sc->sc_ttyp->t_outq.c_cc == sc->sc_oqlen) {
  861                 struct timeval tv;
  862 
  863                 /* if output's been stalled for too long, and restart */
  864                 timersub(&time, &sc->sc_lastpacket, &tv);
  865                 if (tv.tv_sec > 0) {
  866                         DPRINTF(("stripoutput: stalled, resetting\n"));
  867                         sc->sc_otimeout++;
  868                         stripstart(sc->sc_ttyp);
  869                 }
  870         }
  871         splx(s);
  872 
  873         s = splnet();
  874         if (ifq != NULL) {
  875                 if (IF_QFULL(ifq)) {
  876                         IF_DROP(ifq);
  877                         m_freem(m);
  878                         error = ENOBUFS;
  879                 } else {
  880                         IF_ENQUEUE(ifq, m);
  881                         error = 0;
  882                 }
  883         } else
  884                 IFQ_ENQUEUE(&ifp->if_snd, m, &pktattr, error);
  885         if (error) {
  886                 splx(s);
  887                 ifp->if_oerrors++;
  888                 return (error);
  889         }
  890         sc->sc_lastpacket = time;
  891         splx(s);
  892 
  893         s = spltty();
  894         stripstart(sc->sc_ttyp);
  895         splx(s);
  896 
  897         return (0);
  898 }
  899 
  900 
  901 /*
  902  * Start output on interface.  Get another datagram
  903  * to send from the interface queue and map it to
  904  * the interface before starting output.
  905  *
  906  */
  907 void
  908 stripstart(tp)
  909         struct tty *tp;
  910 {
  911         struct strip_softc *sc = tp->t_sc;
  912 
  913         /*
  914          * If there is more in the output queue, just send it now.
  915          * We are being called in lieu of ttstart and must do what
  916          * it would.
  917          */
  918         if (tp->t_outq.c_cc != 0) {
  919                 (*tp->t_oproc)(tp);
  920                 if (tp->t_outq.c_cc > SLIP_HIWAT)
  921                         return;
  922         }
  923 
  924         /*
  925          * This happens briefly when the line shuts down.
  926          */
  927         if (sc == NULL)
  928                 return;
  929 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
  930         softintr_schedule(sc->sc_si);
  931 #else
  932     {
  933         int s = splhigh();
  934         schednetisr(NETISR_STRIP);
  935         splx(s);
  936     }
  937 #endif
  938 }
  939 
  940 /*
  941  * Copy data buffer to mbuf chain; add ifnet pointer.
  942  */
  943 static struct mbuf *
  944 strip_btom(sc, len)
  945         struct strip_softc *sc;
  946         int len;
  947 {
  948         struct mbuf *m;
  949 
  950         /*
  951          * Allocate a new input buffer and swap.
  952          */
  953         m = sc->sc_mbuf;
  954         MGETHDR(sc->sc_mbuf, M_DONTWAIT, MT_DATA);
  955         if (sc->sc_mbuf == NULL) {
  956                 sc->sc_mbuf = m;
  957                 return (NULL);
  958         }
  959         MCLGET(sc->sc_mbuf, M_DONTWAIT);
  960         if ((sc->sc_mbuf->m_flags & M_EXT) == 0) {
  961                 m_freem(sc->sc_mbuf);
  962                 sc->sc_mbuf = m;
  963                 return (NULL);
  964         }
  965         sc->sc_ep = (u_char *) sc->sc_mbuf->m_ext.ext_buf +
  966             sc->sc_mbuf->m_ext.ext_size;
  967 
  968         m->m_data = sc->sc_pktstart;
  969 
  970         m->m_pkthdr.len = m->m_len = len;
  971         m->m_pkthdr.rcvif = &sc->sc_if;
  972         return (m);
  973 }
  974 
  975 /*
  976  * tty interface receiver interrupt.
  977  *
  978  * Called with a single char from the tty receiver interrupt; put
  979  * the char into the buffer containing a partial packet. If the
  980  * char is a packet delimiter, decapsulate the packet, wrap it in
  981  * an mbuf, and put it on the protocol input queue.
  982 */
  983 void
  984 stripinput(c, tp)
  985         int c;
  986         struct tty *tp;
  987 {
  988         struct strip_softc *sc;
  989         struct mbuf *m;
  990         int len;
  991 
  992         tk_nin++;
  993         sc = (struct strip_softc *)tp->t_sc;
  994         if (sc == NULL)
  995                 return;
  996         if (c & TTY_ERRORMASK || ((tp->t_state & TS_CARR_ON) == 0 &&
  997             (tp->t_cflag & CLOCAL) == 0)) {
  998                 sc->sc_flags |= SC_ERROR;
  999                 DPRINTF(("strip: input, error %x\n", c));        /* XXX */
 1000                 return;
 1001         }
 1002         c &= TTY_CHARMASK;
 1003 
 1004         ++sc->sc_if.if_ibytes;
 1005 
 1006         /*
 1007          * Accumulate characters until we see a frame terminator (\r).
 1008          */
 1009         switch (c) {
 1010 
 1011         case '\n':
 1012                 /*
 1013                  * Error message strings from the modem are terminated with
 1014                  * \r\n. This driver interprets the  \r as a packet terminator.
 1015                  * If the first character in a packet is a \n, drop it.
 1016                  * (it can never be the first char of a vaild frame).
 1017                  */
 1018                 if (sc->sc_mp - sc->sc_pktstart == 0)
 1019                         break;
 1020 
 1021         /* Fall through to */
 1022 
 1023         default:
 1024                 if (sc->sc_mp < sc->sc_ep) {
 1025                         *sc->sc_mp++ = c;
 1026                 } else {
 1027                         sc->sc_flags |= SC_ERROR;
 1028                         goto error;
 1029                 }
 1030                 return;
 1031 
 1032         case STRIP_FRAME_END:
 1033                 break;
 1034         }
 1035 
 1036 
 1037         /*
 1038          * We only reach here if we see a CR delimiting a packet.
 1039          */
 1040 
 1041 
 1042         len = sc->sc_mp - sc->sc_pktstart;
 1043 
 1044 #ifdef XDEBUG
 1045         if (len < 15 || sc->sc_flags & SC_ERROR)
 1046                 printf("stripinput: end of pkt, len %d, err %d\n",
 1047                     len, sc->sc_flags & SC_ERROR); /*XXX*/
 1048 #endif
 1049         if(sc->sc_flags & SC_ERROR) {
 1050                 sc->sc_flags &= ~SC_ERROR;
 1051                 addlog("%s: sc error flag set. terminating packet\n",
 1052                         sc->sc_if.if_xname);
 1053                 goto newpack;
 1054         }
 1055 
 1056         /*
 1057          * We have a frame.
 1058          * Process an IP packet, ARP packet, AppleTalk packet,
 1059          * AT command resposne, or Starmode error.
 1060          */
 1061         len = strip_newpacket(sc, sc->sc_pktstart, sc->sc_mp);
 1062         if (len <= 1)
 1063                 /* less than min length packet - ignore */
 1064                 goto newpack;
 1065 
 1066         m = strip_btom(sc, len);
 1067         if (m == NULL)
 1068                 goto error;
 1069 
 1070         IF_ENQUEUE(&sc->sc_inq, m);
 1071 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
 1072         softintr_schedule(sc->sc_si);
 1073 #else
 1074     {
 1075         int s = splhigh();
 1076         schednetisr(NETISR_STRIP);
 1077         splx(s);
 1078     }
 1079 #endif
 1080         goto newpack;
 1081 
 1082 error:
 1083         sc->sc_if.if_ierrors++;
 1084 
 1085 newpack:
 1086         sc->sc_mp = sc->sc_pktstart = (u_char *) sc->sc_mbuf->m_ext.ext_buf +
 1087             BUFOFFSET;
 1088 }
 1089 
 1090 #ifndef __HAVE_GENERIC_SOFT_INTERRUPTS
 1091 void
 1092 stripnetisr(void)
 1093 {
 1094         struct strip_softc *sc;
 1095 
 1096         LIST_FOREACH(sc, &strip_softc_list, sc_iflist) {
 1097                 if (sc->sc_ttyp == NULL)
 1098                         continue;
 1099                 stripintr(sc);
 1100         }
 1101 }
 1102 #endif
 1103 
 1104 void
 1105 stripintr(void *arg)
 1106 {
 1107         struct strip_softc *sc = arg;
 1108         struct tty *tp = sc->sc_ttyp;
 1109         struct mbuf *m;
 1110         int s, len;
 1111         u_char *pktstart;
 1112 #ifdef INET
 1113         u_char c;
 1114 #endif
 1115 #if NBPFILTER > 0
 1116         u_char chdr[CHDR_LEN];
 1117 #endif
 1118 
 1119         KASSERT(tp != NULL);
 1120 
 1121         /*
 1122          * Output processing loop.
 1123          */
 1124         for (;;) {
 1125 #ifdef INET
 1126                 struct ip *ip;
 1127 #endif
 1128 #if NBPFILTER > 0
 1129                 struct mbuf *bpf_m;
 1130 #endif
 1131 
 1132                 /*
 1133                  * Do not remove the packet from the queue if it
 1134                  * doesn't look like it will fit into the current
 1135                  * serial output queue (STRIP_MTU_ONWIRE, or
 1136                  * Starmode header + 20 bytes + 4 bytes in case we
 1137                  * have to probe the radio).
 1138                  */
 1139                 s = spltty();
 1140                 if (tp->t_outq.c_cn - tp->t_outq.c_cc <
 1141                     STRIP_MTU_ONWIRE + 4) {
 1142                         splx(s);
 1143                         break;
 1144                 }
 1145                 splx(s);
 1146 
 1147                 /*
 1148                  * Get a packet and send it to the radio.
 1149                  */
 1150                 s = splnet();
 1151                 IF_DEQUEUE(&sc->sc_fastq, m);
 1152                 if (m)
 1153                         sc->sc_if.if_omcasts++; /* XXX */
 1154                 else
 1155                         IFQ_DEQUEUE(&sc->sc_if.if_snd, m);
 1156                 splx(s);
 1157 
 1158                 if (m == NULL)
 1159                         break;
 1160 
 1161                 /*
 1162                  * We do the header compression here rather than in
 1163                  * stripoutput() because the packets will be out of
 1164                  * order if we are using TOS queueing, and the
 1165                  * connection ID compression will get munged when
 1166                  * this happens.
 1167                  */
 1168 #if NBPFILTER > 0
 1169                 if (sc->sc_if.if_bpf) {
 1170                         /*
 1171                          * We need to save the TCP/IP header before
 1172                          * it's compressed.  To avoid complicated
 1173                          * code, we just make a deep copy of the
 1174                          * entire packet (since this is a serial
 1175                          * line, packets should be short and/or the
 1176                          * copy should be negligible cost compared
 1177                          * to the packet transmission time).
 1178                          */
 1179                         bpf_m = m_dup(m, 0, M_COPYALL, M_DONTWAIT);
 1180                 } else
 1181                         bpf_m = NULL;
 1182 #endif
 1183 #ifdef INET
 1184                 if ((ip = mtod(m, struct ip *))->ip_p == IPPROTO_TCP) {
 1185                         if (sc->sc_if.if_flags & SC_COMPRESS)
 1186                                 *mtod(m, u_char *) |=
 1187                                     sl_compress_tcp(m, ip,
 1188                                     &sc->sc_comp, 1);
 1189                 }
 1190 #endif
 1191 #if NBPFILTER > 0
 1192                 if (sc->sc_if.if_bpf && bpf_m != NULL)
 1193                         bpf_mtap_sl_out(sc->sc_if.if_bpf, mtod(m, u_char *),
 1194                             bpf_m);
 1195 #endif
 1196                 sc->sc_lastpacket = time;
 1197 
 1198                 s = spltty();
 1199                 strip_send(sc, m);
 1200 
 1201                 /*
 1202                  * We now have characters in the output queue,
 1203                  * kick the serial port.
 1204                  */
 1205                 if (tp->t_outq.c_cc != 0)
 1206                         (*tp->t_oproc)(tp);
 1207                 splx(s);
 1208         }
 1209 
 1210         /*
 1211          * Input processing loop.
 1212          */
 1213         for (;;) {
 1214                 s = spltty();
 1215                 IF_DEQUEUE(&sc->sc_inq, m);
 1216                 splx(s);
 1217                 if (m == NULL)
 1218                         break;
 1219                 pktstart = mtod(m, u_char *);
 1220                 len = m->m_pkthdr.len;
 1221 #if NBPFILTER > 0
 1222                 if (sc->sc_if.if_bpf) {
 1223                         /*
 1224                          * Save the compressed header, so we
 1225                          * can tack it on later.  Note that we
 1226                          * will end up copying garbage in come
 1227                          * cases but this is okay.  We remember
 1228                          * where the buffer started so we can
 1229                          * compute the new header length.
 1230                          */
 1231                         memcpy(chdr, pktstart, CHDR_LEN);
 1232                 }
 1233 #endif /* NBPFILTER > 0 */
 1234 #ifdef INET
 1235                 if ((c = (*pktstart & 0xf0)) != (IPVERSION << 4)) {
 1236                         if (c & 0x80)
 1237                                 c = TYPE_COMPRESSED_TCP;
 1238                         else if (c == TYPE_UNCOMPRESSED_TCP)
 1239                                 *pktstart &= 0x4f; /* XXX */
 1240                         /*
 1241                          * We've got something that's not an IP
 1242                          * packet.  If compression is enabled,
 1243                          * try to decompress it.  Otherwise, if
 1244                          * `auto-enable' compression is on and
 1245                          * it's a reasonable packet, decompress
 1246                          * it and then enable compression.
 1247                          * Otherwise, drop it.
 1248                          */
 1249                         if (sc->sc_if.if_flags & SC_COMPRESS) {
 1250                                 len = sl_uncompress_tcp(&pktstart, len,
 1251                                     (u_int)c, &sc->sc_comp);
 1252                                 if (len <= 0) {
 1253                                         m_freem(m);
 1254                                         continue;
 1255                                 }
 1256                         } else if ((sc->sc_if.if_flags & SC_AUTOCOMP) &&
 1257                             c == TYPE_UNCOMPRESSED_TCP && len >= 40) {
 1258                                 len = sl_uncompress_tcp(&pktstart, len,
 1259                                     (u_int)c, &sc->sc_comp);
 1260                                 if (len <= 0) {
 1261                                         m_freem(m);
 1262                                         continue;
 1263                                 }
 1264                                 sc->sc_if.if_flags |= SC_COMPRESS;
 1265                         } else {
 1266                                 m_freem(m);
 1267                                 continue;
 1268                         }
 1269                 }
 1270 #endif
 1271                 m->m_data = (caddr_t) pktstart;
 1272                 m->m_pkthdr.len = m->m_len = len;
 1273 #if NPBFILTER > 0
 1274                 if (sc->sc_if.if_bpf) {
 1275                         bpf_mtap_sl_in(sc->sc_if.if_bpf, chdr, &m);
 1276                         if (m == NULL)
 1277                                 continue;
 1278                 }
 1279 #endif
 1280                 /*
 1281                  * If the packet will fit into a single
 1282                  * header mbuf, copy it into one, to save
 1283                  * memory.
 1284                  */
 1285                 if (m->m_pkthdr.len < MHLEN) {
 1286                         struct mbuf *n;
 1287 
 1288                         MGETHDR(n, M_DONTWAIT, MT_DATA);
 1289                         M_COPY_PKTHDR(n, m);
 1290                         memcpy(mtod(n, caddr_t), mtod(m, caddr_t),
 1291                             m->m_pkthdr.len);
 1292                         n->m_len = m->m_len;
 1293                         m_freem(m);
 1294                         m = n;
 1295                 }
 1296 
 1297                 sc->sc_if.if_ipackets++;
 1298                 sc->sc_lastpacket = time;
 1299 
 1300 #ifdef INET
 1301                 s = splnet();
 1302                 if (IF_QFULL(&ipintrq)) {
 1303                         IF_DROP(&ipintrq);
 1304                         sc->sc_if.if_ierrors++;
 1305                         sc->sc_if.if_iqdrops++;
 1306                         m_freem(m);
 1307                 } else {
 1308                         IF_ENQUEUE(&ipintrq, m);
 1309                         schednetisr(NETISR_IP);
 1310                 }
 1311                 splx(s);
 1312 #endif
 1313         }
 1314 }
 1315 
 1316 /*
 1317  * Process an ioctl request.
 1318  */
 1319 int
 1320 stripioctl(ifp, cmd, data)
 1321         struct ifnet *ifp;
 1322         u_long cmd;
 1323         caddr_t data;
 1324 {
 1325         struct ifaddr *ifa = (struct ifaddr *)data;
 1326         struct ifreq *ifr;
 1327         int s, error = 0;
 1328 
 1329         s = splnet();
 1330 
 1331         switch (cmd) {
 1332 
 1333         case SIOCSIFADDR:
 1334                 if (ifa->ifa_addr->sa_family == AF_INET)
 1335                         ifp->if_flags |= IFF_UP;
 1336                 else
 1337                         error = EAFNOSUPPORT;
 1338                 break;
 1339 
 1340         case SIOCSIFDSTADDR:
 1341                 if (ifa->ifa_addr->sa_family != AF_INET)
 1342                         error = EAFNOSUPPORT;
 1343                 break;
 1344 
 1345         case SIOCADDMULTI:
 1346         case SIOCDELMULTI:
 1347                 ifr = (struct ifreq *)data;
 1348                 if (ifr == 0) {
 1349                         error = EAFNOSUPPORT;           /* XXX */
 1350                         break;
 1351                 }
 1352                 switch (ifr->ifr_addr.sa_family) {
 1353 
 1354 #ifdef INET
 1355                 case AF_INET:
 1356                         break;
 1357 #endif
 1358 
 1359                 default:
 1360                         error = EAFNOSUPPORT;
 1361                         break;
 1362                 }
 1363                 break;
 1364 
 1365         default:
 1366                 error = EINVAL;
 1367         }
 1368         splx(s);
 1369         return (error);
 1370 }
 1371 
 1372 
 1373 /*
 1374  * Strip subroutines
 1375  */
 1376 
 1377 /*
 1378  * Set a radio into starmode.
 1379  * Must be called at spltty().
 1380  */
 1381 void
 1382 strip_resetradio(sc, tp)
 1383         struct strip_softc *sc;
 1384         struct tty *tp;
 1385 {
 1386 #if 0
 1387         static ttychar_t InitString[] =
 1388                 "\r\n\r\n\r\nat\r\n\r\n\r\nate0dt**starmode\r\n**\r\n";
 1389 #else
 1390         static ttychar_t InitString[] =
 1391                 "\r\rat\r\r\rate0q1dt**starmode\r**\r";
 1392 #endif
 1393         int i;
 1394 
 1395         /*
 1396          * XXX Perhaps flush  tty output queue?
 1397          */
 1398 
 1399         if ((i = b_to_q(InitString, sizeof(InitString) - 1, &tp->t_outq))) {
 1400                 printf("resetradio: %d chars didn't fit in tty queue\n", i);
 1401                 return;
 1402         }
 1403         sc->sc_if.if_obytes += sizeof(InitString) - 1;
 1404 
 1405         /*
 1406          * Assume the radio is still dead, so we can detect repeated
 1407          * resets (perhaps the radio is disconnected, powered off, or
 1408          * is so badlyhung it needs  powercycling.
 1409          */
 1410         sc->sc_state = ST_DEAD;
 1411         sc->sc_lastpacket = time;
 1412         sc->sc_statetimo = time.tv_sec + STRIP_RESET_INTERVAL;
 1413 
 1414         /*
 1415          * XXX Does calling the tty output routine now help resets?
 1416          */
 1417         (*sc->sc_ttyp->t_oproc)(tp);
 1418 }
 1419 
 1420 
 1421 /*
 1422  * Send an invalid starmode packet to the radio, to induce an error message
 1423  * indicating the radio is in starmode.
 1424  * Update the state machine to indicate a response is expected.
 1425  * Either the radio answers, which will be caught by the parser,
 1426  * or the watchdog will start resetting.
 1427  *
 1428  * NOTE: drops chars directly on the tty output queue.
 1429  * should be caled at spl >= spltty.
 1430  */
 1431 void
 1432 strip_proberadio(sc, tp)
 1433         struct strip_softc *sc;
 1434         struct tty *tp;
 1435 {
 1436 
 1437         int overflow;
 1438         const char *strip_probestr = "**";
 1439 
 1440         if (sc->sc_if.if_flags & IFF_DEBUG)
 1441                 addlog("%s: attempting to probe radio\n", sc->sc_if.if_xname);
 1442 
 1443         overflow = b_to_q((const ttychar_t *)strip_probestr, 2, &tp->t_outq);
 1444         if (overflow == 0) {
 1445                 if (sc->sc_if.if_flags & IFF_DEBUG)
 1446                         addlog("%s:: sent probe  to radio\n",
 1447                                sc->sc_if.if_xname);
 1448                 /* Go to probe-sent state, set timeout accordingly. */
 1449                 sc->sc_state = ST_PROBE_SENT;
 1450                 sc->sc_statetimo = time.tv_sec + ST_PROBERESPONSE_INTERVAL;
 1451         } else {
 1452                 addlog("%s: incomplete probe, tty queue %d bytes overfull\n",
 1453                         sc->sc_if.if_xname, overflow);
 1454         }
 1455 }
 1456 
 1457 
 1458 #ifdef DEBUG
 1459 static char *strip_statenames[] = {
 1460         "Alive",
 1461         "Probe sent, awaiting answer",
 1462         "Probe not answered, resetting"
 1463 };
 1464 #endif
 1465 
 1466 
 1467 /*
 1468  * Timeout routine -- try to start more output.
 1469  * Will be needed to make strip work on ptys.
 1470  */
 1471 void
 1472 strip_timeout(x)
 1473     void *x;
 1474 {
 1475     struct strip_softc *sc = (struct strip_softc *) x;
 1476     struct tty *tp =  sc->sc_ttyp;
 1477     int s;
 1478 
 1479     s = spltty();
 1480     sc->sc_flags &= ~SC_TIMEOUT;
 1481     stripstart(tp);
 1482     splx(s);
 1483 }
 1484 
 1485 
 1486 /*
 1487  * Strip watchdog routine.
 1488  * The radio hardware is balky. When sent long packets or bursts of small
 1489  * packets, the radios crash and reboots into Hayes-emulation mode.
 1490  * The transmit-side machinery, the error parser, and strip_watchdog()
 1491  * implement a simple finite state machine.
 1492  *
 1493  * We attempt to send a probe to the radio every ST_PROBE seconds. There
 1494  * is no direct way to tell if the radio is in starmode, so we send it a
 1495  * malformed starmode packet -- a frame with no destination address --
 1496  * and expect to an "name missing" error response from the radio within
 1497  * 1 second. If we hear such a response, we assume the radio is alive
 1498  * for the next ST_PROBE seconds.
 1499  * If we don't hear a starmode-error response from  the radio, we reset it.
 1500  *
 1501  * Probes, and parsing of error responses,  are normally done inside the send
 1502  * and receive side respectively. This watchdog routine examines the
 1503  * state-machine variables. If there are no packets to send to the radio
 1504  * during an entire probe interval, strip_output  will not be called,
 1505  * so we send a probe on its behalf.
 1506  */
 1507 void
 1508 strip_watchdog(ifp)
 1509         struct ifnet *ifp;
 1510 {
 1511         struct strip_softc *sc = ifp->if_softc;
 1512         struct tty *tp =  sc->sc_ttyp;
 1513 
 1514         /*
 1515          * Just punt if the line has been closed.
 1516          */
 1517         if (tp == NULL)
 1518                 return;
 1519 
 1520 #ifdef DEBUG
 1521         if (ifp->if_flags & IFF_DEBUG)
 1522                 addlog("\n%s: in watchdog, state %s timeout %ld\n",
 1523                        ifp->if_xname,
 1524                        ((unsigned) sc->sc_state < 3) ?
 1525                        strip_statenames[sc->sc_state] : "<<illegal state>>",
 1526                        sc->sc_statetimo - time.tv_sec);
 1527 #endif
 1528 
 1529         /*
 1530          * If time in this state hasn't yet expired, return.
 1531          */
 1532         if ((ifp->if_flags & IFF_UP) ==  0 || sc->sc_statetimo > time.tv_sec) {
 1533                 goto done;
 1534         }
 1535 
 1536         /*
 1537          * The time in the current state has expired.
 1538          * Take appropriate action and advance FSA to the next state.
 1539          */
 1540         switch (sc->sc_state) {
 1541               case ST_ALIVE:
 1542                 /*
 1543                  * A probe is due but we haven't piggybacked one on a packet.
 1544                  * Send a probe now.
 1545                  */
 1546                 strip_proberadio(sc, sc->sc_ttyp);
 1547                 (*tp->t_oproc)(tp);
 1548                 break;
 1549 
 1550               case ST_PROBE_SENT:
 1551                 /*
 1552                  * Probe sent but no response within timeout. Reset.
 1553                  */
 1554                 addlog("%s: no answer to probe, resetting radio\n",
 1555                        ifp->if_xname);
 1556                 strip_resetradio(sc, sc->sc_ttyp);
 1557                 ifp->if_oerrors++;
 1558                 break;
 1559 
 1560               case ST_DEAD:
 1561                 /*
 1562                  * The radio has been sent a reset but didn't respond.
 1563                  * XXX warn user to remove AC adaptor and battery,
 1564                  * wait  5 secs, and replace.
 1565                  */
 1566                 addlog("%s: radio reset but not responding, Trying again\n",
 1567                        ifp->if_xname);
 1568                 strip_resetradio(sc, sc->sc_ttyp);
 1569                 ifp->if_oerrors++;
 1570                 break;
 1571 
 1572               default:
 1573                 /* Cannot happen. To be safe, do  a reset. */
 1574                 addlog("%s: %s %d, resetting\n",
 1575                        sc->sc_if.if_xname,
 1576                        "radio-reset finite-state machine in invalid state",
 1577                        sc->sc_state);
 1578                 strip_resetradio(sc, sc->sc_ttyp);
 1579                 sc->sc_state = ST_DEAD;
 1580                 break;
 1581         }
 1582 
 1583       done:
 1584         ifp->if_timer = STRIP_WATCHDOG_INTERVAL;
 1585         return;
 1586 }
 1587 
 1588 
 1589 /*
 1590  * The following bytestuffing and run-length encoding/decoding
 1591  * functions are taken, with permission from Stuart Cheshire,
 1592  * from the MosquitonNet strip driver for Linux.
 1593  * XXX Linux style left intact, to ease folding in updates from
 1594  * the Mosquitonet group.
 1595  */
 1596 
 1597 
 1598 /*
 1599  * Process a received packet.
 1600  */
 1601 int
 1602 strip_newpacket(sc, ptr, end)
 1603         struct strip_softc *sc;
 1604         u_char *ptr, *end;
 1605 {
 1606         int len = ptr - end;
 1607         u_char *name, *name_end;
 1608         u_int packetlen;
 1609 
 1610         /* Ignore empty lines */
 1611         if (len == 0) return 0;
 1612 
 1613         /* Catch 'OK' responses which show radio has fallen out of starmode */
 1614         if (len >= 2 && ptr[0] == 'O' && ptr[1] == 'K') {
 1615                 printf("%s: Radio is back in AT command mode: will reset\n",
 1616                     sc->sc_if.if_xname);
 1617                 FORCE_RESET(sc);                /* Do reset ASAP */
 1618         return 0;
 1619         }
 1620 
 1621         /* Check for start of address marker, and then skip over it */
 1622         if (*ptr != '*') {
 1623                 /* Catch other error messages */
 1624                 if (ptr[0] == 'E' && ptr[1] == 'R' && ptr[2] == 'R' && ptr[3] == '_')
 1625                         RecvErr_Message(sc, NULL, ptr+4);
 1626                          /* XXX what should the message above be? */
 1627                 else {
 1628                         RecvErr("No initial *", sc);
 1629                         addlog("(len = %d)\n", len);
 1630                      }
 1631                 return 0;
 1632         }
 1633 
 1634         /* skip the '*' */
 1635         ptr++;
 1636 
 1637         /* Skip the return address */
 1638         name = ptr;
 1639         while (ptr < end && *ptr != '*')
 1640                 ptr++;
 1641 
 1642         /* Check for end of address marker, and skip over it */
 1643         if (ptr == end) {
 1644                 RecvErr("No second *", sc);
 1645                 return 0;
 1646         }
 1647         name_end = ptr++;
 1648 
 1649         /* Check for SRIP key, and skip over it */
 1650         if (ptr[0] != 'S' || ptr[1] != 'I' || ptr[2] != 'P' || ptr[3] != '') {
 1651                 if (ptr[0] == 'E' && ptr[1] == 'R' && ptr[2] == 'R' &&
 1652                     ptr[3] == '_') {
 1653                         *name_end = 0;
 1654                         RecvErr_Message(sc, name, ptr+4);
 1655                  }
 1656                 else RecvErr("No SRIP key", sc);
 1657                 return 0;
 1658         }
 1659         ptr += 4;
 1660 
 1661         /* Decode start of the IP packet header */
 1662         ptr = UnStuffData(ptr, end, sc->sc_rxbuf, 4);
 1663         if (ptr == 0) {
 1664                 RecvErr("Runt packet (hdr)", sc);
 1665                 return 0;
 1666         }
 1667 
 1668         /*
 1669          * The STRIP bytestuff/RLL encoding has no explicit length
 1670          * of the decoded packet.  Decode start of IP header, get the
 1671          * IP header length and decode that many bytes in total.
 1672          */
 1673         packetlen = ((u_int16_t)sc->sc_rxbuf[2] << 8) | sc->sc_rxbuf[3];
 1674 
 1675 #ifdef DIAGNOSTIC
 1676 #if 0
 1677         printf("Packet %02x.%02x.%02x.%02x\n",
 1678                 sc->sc_rxbuf[0], sc->sc_rxbuf[1],
 1679                 sc->sc_rxbuf[2], sc->sc_rxbuf[3]);
 1680         printf("Got %d byte packet\n", packetlen);
 1681 #endif
 1682 #endif
 1683 
 1684         /* Decode remainder of the IP packer */
 1685         ptr = UnStuffData(ptr, end, sc->sc_rxbuf+4, packetlen-4);
 1686         if (ptr == 0) {
 1687                 RecvErr("Short packet", sc);
 1688                 return 0;
 1689         }
 1690 
 1691         /* XXX redundant copy */
 1692         bcopy(sc->sc_rxbuf, sc->sc_pktstart, packetlen );
 1693         return (packetlen);
 1694 }
 1695 
 1696 
 1697 /*
 1698  * Stuffing scheme:
 1699  * 00    Unused (reserved character)
 1700  * 01-3F Run of 2-64 different characters
 1701  * 40-7F Run of 1-64 different characters plus a single zero at the end
 1702  * 80-BF Run of 1-64 of the same character
 1703  * C0-FF Run of 1-64 zeroes (ASCII 0)
 1704 */
 1705 typedef enum
 1706 {
 1707         Stuff_Diff      = 0x00,
 1708         Stuff_DiffZero  = 0x40,
 1709         Stuff_Same      = 0x80,
 1710         Stuff_Zero      = 0xC0,
 1711         Stuff_NoCode    = 0xFF,         /* Special code, meaning no code selected */
 1712 
 1713         Stuff_CodeMask  = 0xC0,
 1714         Stuff_CountMask = 0x3F,
 1715         Stuff_MaxCount  = 0x3F,
 1716         Stuff_Magic     = 0x0D          /* The value we are eliminating */
 1717 } StuffingCode;
 1718 
 1719 /*
 1720  * StuffData encodes the data starting at "src" for "length" bytes.
 1721  * It writes it to the buffer pointed to by "dest" (which must be at least
 1722  * as long as 1 + 65/64 of the input length). The output may be up to 1.6%
 1723  * larger than the input for pathological input, but will usually be smaller.
 1724  * StuffData returns the new value of the dest pointer as its result.
 1725  *
 1726  * "code_ptr_ptr" points to a "u_char *" which is used to hold
 1727  * encoding state between calls, allowing an encoded packet to be
 1728  * incrementally built up from small parts.
 1729  * On the first call, the "u_char *" pointed to should be initialized
 1730  * to NULL;  between subsequent calls the calling routine should leave
 1731  * the value alone and simply pass it back unchanged so that the
 1732  * encoder can recover its current state.
 1733  */
 1734 
 1735 #define StuffData_FinishBlock(X) \
 1736         (*code_ptr = (X) ^ Stuff_Magic, code = Stuff_NoCode)
 1737 
 1738 static u_char*
 1739 StuffData(u_char *src, u_long length, u_char *dest, u_char **code_ptr_ptr)
 1740 {
 1741         u_char *end = src + length;
 1742         u_char *code_ptr = *code_ptr_ptr;
 1743         u_char code = Stuff_NoCode, count = 0;
 1744 
 1745         if (!length) return (dest);
 1746 
 1747         if (code_ptr) { /* Recover state from last call, if applicable */
 1748                 code  = (*code_ptr ^ Stuff_Magic) & Stuff_CodeMask;
 1749                 count = (*code_ptr ^ Stuff_Magic) & Stuff_CountMask;
 1750         }
 1751 
 1752         while (src < end) {
 1753                 switch (code) {
 1754                 /*
 1755                  * Stuff_NoCode: If no current code, select one
 1756                  */
 1757                 case Stuff_NoCode:
 1758                         code_ptr = dest++;      /* Record where we're going to put this code */
 1759                         count = 0;              /* Reset the count (zero means one instance) */
 1760                                                         /* Tentatively start a new block */
 1761                         if (*src == 0) {
 1762                                 code = Stuff_Zero;
 1763                                 src++;
 1764                         } else {
 1765                                 code = Stuff_Same;
 1766                                 *dest++ = *src++ ^ Stuff_Magic;
 1767                         }
 1768                         /* Note: We optimistically assume run of same -- which will be */
 1769                         /* fixed later in Stuff_Same if it turns out not to be true. */
 1770                         break;
 1771 
 1772                 /*
 1773                  * Stuff_Zero: We already have at least one zero encoded
 1774                  */
 1775                 case Stuff_Zero:
 1776 
 1777                         /* If another zero, count it, else finish this code block */
 1778                         if (*src == 0) {
 1779                                 count++;
 1780                                 src++;
 1781                         } else
 1782                                 StuffData_FinishBlock(Stuff_Zero + count);
 1783                         break;
 1784 
 1785                 /*
 1786                  * Stuff_Same: We already have at least one byte encoded
 1787                  */
 1788                 case Stuff_Same:
 1789                         /* If another one the same, count it */
 1790                         if ((*src ^ Stuff_Magic) == code_ptr[1]) {
 1791                                 count++;
 1792                                 src++;
 1793                                 break;
 1794                         }
 1795                         /* else, this byte does not match this block. */
 1796                         /* If we already have two or more bytes encoded, finish this code block */
 1797                         if (count) {
 1798                                 StuffData_FinishBlock(Stuff_Same + count);
 1799                                 break;
 1800                         }
 1801                         /* else, we only have one so far, so switch to Stuff_Diff code */
 1802                         code = Stuff_Diff; /* and fall through to Stuff_Diff case below */
 1803 
 1804                 case Stuff_Diff:        /* Stuff_Diff: We have at least two *different* bytes encoded */
 1805                         /* If this is a zero, must encode a Stuff_DiffZero, and begin a new block */
 1806                         if (*src == 0)
 1807                                 StuffData_FinishBlock(Stuff_DiffZero + count);
 1808                         /* else, if we have three in a row, it is worth starting a Stuff_Same block */
 1809                         else if ((*src ^ Stuff_Magic) == dest[-1] && dest[-1] == dest[-2])
 1810                                 {
 1811                                 code += count-2;
 1812                                 if (code == Stuff_Diff)
 1813                                         code = Stuff_Same;
 1814                                 StuffData_FinishBlock(code);
 1815                                 code_ptr = dest-2;
 1816                                 /* dest[-1] already holds the correct value */
 1817                                 count = 2;              /* 2 means three bytes encoded */
 1818                                 code = Stuff_Same;
 1819                                 }
 1820                         /* else, another different byte, so add it to the block */
 1821                         else {
 1822                                 *dest++ = *src ^ Stuff_Magic;
 1823                                 count++;
 1824                         }
 1825                         src++;  /* Consume the byte */
 1826                         break;
 1827                 }
 1828 
 1829                 if (count == Stuff_MaxCount)
 1830                         StuffData_FinishBlock(code + count);
 1831                 }
 1832         if (code == Stuff_NoCode)
 1833                 *code_ptr_ptr = NULL;
 1834         else {
 1835                 *code_ptr_ptr = code_ptr;
 1836                 StuffData_FinishBlock(code + count);
 1837         }
 1838 
 1839         return (dest);
 1840 }
 1841 
 1842 
 1843 
 1844 /*
 1845  * UnStuffData decodes the data at "src", up to (but not including)
 1846  * "end".  It writes the decoded data into the buffer pointed to by
 1847  * "dst", up to a  maximum of "dst_length", and returns the new
 1848  * value of "src" so that a follow-on call can read more data,
 1849  * continuing from where the first left off.
 1850  *
 1851  * There are three types of results:
 1852  * 1. The source data runs out before extracting "dst_length" bytes:
 1853  *    UnStuffData returns NULL to indicate failure.
 1854  * 2. The source data produces exactly "dst_length" bytes:
 1855  *    UnStuffData returns new_src = end to indicate that all bytes
 1856  *    were consumed.
 1857  * 3. "dst_length" bytes are extracted, with more
 1858  *     remaining. UnStuffData returns new_src < end to indicate that
 1859  *     there are more bytes to be read.
 1860  *
 1861  * Note: The decoding may be dstructive, in that it may alter the
 1862  * source data in the process of decoding it (this is necessary to
 1863  * allow a follow-on  call to resume correctly).
 1864  */
 1865 
 1866 static u_char*
 1867 UnStuffData(u_char *src, u_char *end, u_char *dst, u_long dst_length)
 1868 {
 1869         u_char *dst_end = dst + dst_length;
 1870 
 1871         /* Sanity check */
 1872         if (!src || !end || !dst || !dst_length)
 1873                 return (NULL);
 1874 
 1875         while (src < end && dst < dst_end)
 1876         {
 1877                 int count = (*src ^ Stuff_Magic) & Stuff_CountMask;
 1878                 switch ((*src ^ Stuff_Magic) & Stuff_CodeMask)
 1879                         {
 1880                         case Stuff_Diff:
 1881                                 if (src+1+count >= end)
 1882                                         return (NULL);
 1883                                 do
 1884                                 {
 1885                                         *dst++ = *++src ^ Stuff_Magic;
 1886                                 }
 1887                                 while(--count >= 0 && dst < dst_end);
 1888                                 if (count < 0)
 1889                                         src += 1;
 1890                                 else
 1891                                  if (count == 0)
 1892                                         *src = Stuff_Same ^ Stuff_Magic;
 1893                                 else
 1894                                         *src = (Stuff_Diff + count) ^ Stuff_Magic;
 1895                                 break;
 1896                         case Stuff_DiffZero:
 1897                                 if (src+1+count >= end)
 1898                                         return (NULL);
 1899                                 do
 1900                                 {
 1901                                         *dst++ = *++src ^ Stuff_Magic;
 1902                                 }
 1903                                 while(--count >= 0 && dst < dst_end);
 1904                                 if (count < 0)
 1905                                         *src = Stuff_Zero ^ Stuff_Magic;
 1906                                 else
 1907                                         *src = (Stuff_DiffZero + count) ^ Stuff_Magic;
 1908                                 break;
 1909                         case Stuff_Same:
 1910                                 if (src+1 >= end)
 1911                                         return (NULL);
 1912                                 do
 1913                                 {
 1914                                         *dst++ = src[1] ^ Stuff_Magic;
 1915                                 }
 1916                                 while(--count >= 0 && dst < dst_end);
 1917                                 if (count < 0)
 1918                                         src += 2;
 1919                                 else
 1920                                         *src = (Stuff_Same + count) ^ Stuff_Magic;
 1921                                 break;
 1922                         case Stuff_Zero:
 1923                                 do
 1924                                 {
 1925                                         *dst++ = 0;
 1926                                 }
 1927                                 while(--count >= 0 && dst < dst_end);
 1928                                 if (count < 0)
 1929                                         src += 1;
 1930                                 else
 1931                                         *src = (Stuff_Zero + count) ^ Stuff_Magic;
 1932                                 break;
 1933                         }
 1934         }
 1935 
 1936         if (dst < dst_end)
 1937                 return (NULL);
 1938         else
 1939                 return (src);
 1940 }
 1941 
 1942 
 1943 
 1944 /*
 1945  * Log an error mesesage (for a packet received with errors?)
 1946  * from the STRIP driver.
 1947  */
 1948 static void
 1949 RecvErr(msg, sc)
 1950         char *msg;
 1951         struct strip_softc *sc;
 1952 {
 1953 #define MAX_RecErr      80
 1954         u_char *ptr = sc->sc_pktstart;
 1955         u_char *end = sc->sc_mp;
 1956         u_char pkt_text[MAX_RecErr], *p = pkt_text;
 1957         *p++ = '\"';
 1958         while (ptr < end && p < &pkt_text[MAX_RecErr-4]) {
 1959                 if (*ptr == '\\') {
 1960                         *p++ = '\\';
 1961                         *p++ = '\\';
 1962                 } else if (*ptr >= 32 && *ptr <= 126)
 1963                         *p++ = *ptr;
 1964                 else {
 1965                         snprintf(p, sizeof(pkt_text) - (p - pkt_text),
 1966                             "\\%02x", *ptr);
 1967                         p += 3;
 1968                 }
 1969                 ptr++;
 1970         }
 1971 
 1972         if (ptr == end) *p++ = '\"';
 1973         *p++ = 0;
 1974         addlog("%s: %13s : %s\n", sc->sc_if.if_xname, msg, pkt_text);
 1975 
 1976         sc->sc_if.if_ierrors++;
 1977 }
 1978 
 1979 
 1980 /*
 1981  * Parse an error message from the radio.
 1982  */
 1983 static void
 1984 RecvErr_Message(strip_info, sendername, msg)
 1985         struct strip_softc *strip_info;
 1986         u_char *sendername;
 1987         /*const*/ u_char *msg;
 1988 {
 1989         static const char ERR_001[] = "001"; /* Not in StarMode! */
 1990         static const char ERR_002[] = "002"; /* Remap handle */
 1991         static const char ERR_003[] = "003"; /* Can't resolve name */
 1992         static const char ERR_004[] = "004"; /* Name too small or missing */
 1993         static const char ERR_005[] = "005"; /* Bad count specification */
 1994         static const char ERR_006[] = "006"; /* Header too big */
 1995         static const char ERR_007[] = "007"; /* Body too big */
 1996         static const char ERR_008[] = "008"; /* Bad character in name */
 1997         static const char ERR_009[] = "009"; /* No count or line terminator */
 1998 
 1999         char * if_name;
 2000 
 2001         if_name = strip_info->sc_if.if_xname;
 2002 
 2003         if (!strncmp(msg, ERR_001, sizeof(ERR_001)-1))
 2004         {
 2005                 RecvErr("radio error message:", strip_info);
 2006                 addlog("%s: Radio %s is not in StarMode\n",
 2007                         if_name, sendername);
 2008         }
 2009         else if (!strncmp(msg, ERR_002, sizeof(ERR_002)-1))
 2010         {
 2011                 RecvErr("radio error message:", strip_info);
 2012 #ifdef notyet           /*Kernel doesn't have scanf!*/
 2013                 int handle;
 2014                 u_char newname[64];
 2015                 sscanf(msg, "ERR_002 Remap handle &%d to name %s", &handle, newname);
 2016                 addlog("%s: Radio name %s is handle %d\n",
 2017                         if_name, newname, handle);
 2018 #endif
 2019         }
 2020         else if (!strncmp(msg, ERR_003, sizeof(ERR_003)-1))
 2021         {
 2022                 RecvErr("radio error message:", strip_info);
 2023                 addlog("%s: Destination radio name is unknown\n", if_name);
 2024         }
 2025         else if (!strncmp(msg, ERR_004, sizeof(ERR_004)-1)) {
 2026                 /*
 2027                  * The radio reports it got a badly-framed starmode packet
 2028                  * from us; so it must me in starmode.
 2029                  */
 2030                 if (strip_info->sc_if.if_flags & IFF_DEBUG)
 2031                         addlog("%s: radio responded to probe\n", if_name);
 2032                 if (strip_info->sc_state == ST_DEAD) {
 2033                         /* A successful reset... */
 2034                         addlog("%s: Radio back in starmode\n", if_name);
 2035                 }
 2036                 CLEAR_RESET_TIMER(strip_info);
 2037         }
 2038         else if (!strncmp(msg, ERR_005, sizeof(ERR_005)-1))
 2039                 RecvErr("radio error message:", strip_info);
 2040         else if (!strncmp(msg, ERR_006, sizeof(ERR_006)-1))
 2041                 RecvErr("radio error message:", strip_info);
 2042         else if (!strncmp(msg, ERR_007, sizeof(ERR_007)-1))
 2043          {
 2044                 /*
 2045                  *      Note: This error knocks the radio back into
 2046                  *      command mode.
 2047                  */
 2048                 RecvErr("radio error message:", strip_info);
 2049                 printf("%s: Error! Packet size too big for radio.",
 2050                     if_name);
 2051                 FORCE_RESET(strip_info);
 2052         }
 2053         else if (!strncmp(msg, ERR_008, sizeof(ERR_008)-1))
 2054         {
 2055                 RecvErr("radio error message:", strip_info);
 2056                 printf("%s: Radio name contains illegal character\n",
 2057                     if_name);
 2058         }
 2059         else if (!strncmp(msg, ERR_009, sizeof(ERR_009)-1))
 2060                 RecvErr("radio error message:", strip_info);
 2061         else {
 2062                 addlog("failed to parse ]%3s[\n", msg);
 2063                 RecvErr("unparsed radio error message:", strip_info);
 2064         }
 2065 }

Cache object: 1f2a43f9b413231b508c6b1e947747aa


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.