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_ppp.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 /*
    2  * if_ppp.c - Point-to-Point Protocol (PPP) Asynchronous driver.
    3  *
    4  * Copyright (c) 1989 Carnegie Mellon University.
    5  * All rights reserved.
    6  *
    7  * Redistribution and use in source and binary forms are permitted
    8  * provided that the above copyright notice and this paragraph are
    9  * duplicated in all such forms and that any documentation,
   10  * advertising materials, and other materials related to such
   11  * distribution and use acknowledge that the software was developed
   12  * by Carnegie Mellon University.  The name of the
   13  * University may not be used to endorse or promote products derived
   14  * from this software without specific prior written permission.
   15  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
   16  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
   17  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
   18  *
   19  * Drew D. Perkins
   20  * Carnegie Mellon University
   21  * 4910 Forbes Ave.
   22  * Pittsburgh, PA 15213
   23  * (412) 268-8576
   24  * ddp@andrew.cmu.edu
   25  *
   26  * Based on:
   27  *      @(#)if_sl.c     7.6.1.2 (Berkeley) 2/15/89
   28  *
   29  * Copyright (c) 1987 Regents of the University of California.
   30  * All rights reserved.
   31  *
   32  * Redistribution and use in source and binary forms are permitted
   33  * provided that the above copyright notice and this paragraph are
   34  * duplicated in all such forms and that any documentation,
   35  * advertising materials, and other materials related to such
   36  * distribution and use acknowledge that the software was developed
   37  * by the University of California, Berkeley.  The name of the
   38  * University may not be used to endorse or promote products derived
   39  * from this software without specific prior written permission.
   40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
   41  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
   42  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
   43  *
   44  * Serial Line interface
   45  *
   46  * Rick Adams
   47  * Center for Seismic Studies
   48  * 1300 N 17th Street, Suite 1450
   49  * Arlington, Virginia 22209
   50  * (703)276-7900
   51  * rick@seismo.ARPA
   52  * seismo!rick
   53  *
   54  * Pounded on heavily by Chris Torek (chris@mimsy.umd.edu, umcp-cs!chris).
   55  * Converted to 4.3BSD Beta by Chris Torek.
   56  * Other changes made at Berkeley, based in part on code by Kirk Smith.
   57  *
   58  * Converted to 4.3BSD+ 386BSD by Brad Parker (brad@cayman.com)
   59  * Added VJ tcp header compression; more unified ioctls
   60  *
   61  * Extensively modified by Paul Mackerras (paulus@cs.anu.edu.au).
   62  * Cleaned up a lot of the mbuf-related code to fix bugs that
   63  * caused system crashes and packet corruption.  Changed pppstart
   64  * so that it doesn't just give up with a collision if the whole
   65  * packet doesn't fit in the output ring buffer.
   66  *
   67  * Added priority queueing for interactive IP packets, following
   68  * the model of if_sl.c, plus hooks for bpf.
   69  * Paul Mackerras (paulus@cs.anu.edu.au).
   70  */
   71 
   72 /* $FreeBSD: releng/5.2/sys/net/if_ppp.c 122702 2003-11-14 21:02:22Z andre $ */
   73 /* from if_sl.c,v 1.11 84/10/04 12:54:47 rick Exp */
   74 /* from NetBSD: if_ppp.c,v 1.15.2.2 1994/07/28 05:17:58 cgd Exp */
   75 
   76 #include "opt_inet.h"
   77 #include "opt_ipx.h"
   78 #include "opt_mac.h"
   79 #include "opt_ppp.h"
   80 
   81 #ifdef INET
   82 #define VJC
   83 #endif
   84 #define PPP_COMPRESS
   85 
   86 #include <sys/param.h>
   87 #include <sys/systm.h>
   88 #include <sys/proc.h>
   89 #include <sys/mac.h>
   90 #include <sys/mbuf.h>
   91 #include <sys/socket.h>
   92 #include <sys/filio.h>
   93 #include <sys/sockio.h>
   94 #include <sys/kernel.h>
   95 #include <sys/time.h>
   96 #include <sys/malloc.h>
   97 #include <sys/module.h>
   98 
   99 #include <net/if.h>
  100 #include <net/if_types.h>
  101 #include <net/netisr.h>
  102 #include <net/bpf.h>
  103 
  104 #if INET
  105 #include <netinet/in.h>
  106 #include <netinet/in_systm.h>
  107 #include <netinet/in_var.h>
  108 #include <netinet/ip.h>
  109 #endif
  110 
  111 #if IPX
  112 #include <netipx/ipx.h>
  113 #include <netipx/ipx_if.h>
  114 #endif
  115 
  116 #ifdef VJC
  117 #include <net/slcompress.h>
  118 #endif
  119 
  120 #include <net/if_ppp.h>
  121 #include <net/if_pppvar.h>
  122 
  123 /* minimise diffs */
  124 #ifndef splsoftnet
  125 #define splsoftnet      splnet
  126 #endif
  127 
  128 #ifdef PPP_COMPRESS
  129 #define PACKETPTR       struct mbuf *
  130 #include <net/ppp_comp.h>
  131 #endif
  132 
  133 #define PPPNAME         "ppp"
  134 static MALLOC_DEFINE(M_PPP, PPPNAME, "PPP interface");
  135 static LIST_HEAD(, ppp_softc) ppp_softc_list;
  136 
  137 /* XXX layering violation */
  138 extern void     pppasyncattach(void *);
  139 extern void     pppasyncdetach(void);
  140 
  141 static int      pppsioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
  142 static void     pppintr(void);
  143 
  144 static void     ppp_requeue(struct ppp_softc *);
  145 static void     ppp_ccp(struct ppp_softc *, struct mbuf *m, int rcvd);
  146 static void     ppp_ccp_closed(struct ppp_softc *);
  147 static void     ppp_inproc(struct ppp_softc *, struct mbuf *);
  148 static void     pppdumpm(struct mbuf *m0);
  149 static int      ppp_clone_create(struct if_clone *, int);
  150 static void     ppp_clone_destroy(struct ifnet *);
  151 
  152 static struct if_clone ppp_cloner = IF_CLONE_INITIALIZER(PPPNAME,
  153     ppp_clone_create, ppp_clone_destroy, 0, IF_MAXUNIT);
  154 
  155 /*
  156  * Some useful mbuf macros not in mbuf.h.
  157  */
  158 #define M_IS_CLUSTER(m) ((m)->m_flags & M_EXT)
  159 
  160 #define M_DATASTART(m)  \
  161         (M_IS_CLUSTER(m) ? (m)->m_ext.ext_buf : \
  162             (m)->m_flags & M_PKTHDR ? (m)->m_pktdat : (m)->m_dat)
  163 
  164 #define M_DATASIZE(m)   \
  165         (M_IS_CLUSTER(m) ? (m)->m_ext.ext_size : \
  166             (m)->m_flags & M_PKTHDR ? MHLEN: MLEN)
  167 
  168 /*
  169  * We steal two bits in the mbuf m_flags, to mark high-priority packets
  170  * for output, and received packets following lost/corrupted packets.
  171  */
  172 #define M_HIGHPRI       0x2000  /* output packet for sc_fastq */
  173 #define M_ERRMARK       0x4000  /* steal a bit in mbuf m_flags */
  174 
  175 
  176 #ifdef PPP_COMPRESS
  177 /*
  178  * List of compressors we know about.
  179  * We leave some space so maybe we can modload compressors.
  180  */
  181 
  182 extern struct compressor ppp_bsd_compress;
  183 extern struct compressor ppp_deflate, ppp_deflate_draft;
  184 
  185 static struct compressor *ppp_compressors[8] = {
  186 #if DO_BSD_COMPRESS && defined(PPP_BSDCOMP)
  187     &ppp_bsd_compress,
  188 #endif
  189 #if DO_DEFLATE && defined(PPP_DEFLATE)
  190     &ppp_deflate,
  191     &ppp_deflate_draft,
  192 #endif
  193     NULL
  194 };
  195 #endif /* PPP_COMPRESS */
  196 
  197 static int
  198 ppp_clone_create(struct if_clone *ifc, int unit)
  199 {
  200         struct ppp_softc        *sc;
  201 
  202         sc = malloc(sizeof(struct ppp_softc), M_PPP, M_WAITOK | M_ZERO);
  203         sc->sc_if.if_softc = sc;
  204         if_initname(&sc->sc_if, ifc->ifc_name, unit);
  205         sc->sc_if.if_mtu = PPP_MTU;
  206         sc->sc_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
  207         sc->sc_if.if_type = IFT_PPP;
  208         sc->sc_if.if_hdrlen = PPP_HDRLEN;
  209         sc->sc_if.if_ioctl = pppsioctl;
  210         sc->sc_if.if_output = pppoutput;
  211         sc->sc_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
  212         sc->sc_inq.ifq_maxlen = IFQ_MAXLEN;
  213         sc->sc_fastq.ifq_maxlen = IFQ_MAXLEN;
  214         sc->sc_rawq.ifq_maxlen = IFQ_MAXLEN;
  215         mtx_init(&sc->sc_inq.ifq_mtx, "ppp_inq", NULL, MTX_DEF);
  216         mtx_init(&sc->sc_fastq.ifq_mtx, "ppp_fastq", NULL, MTX_DEF);
  217         mtx_init(&sc->sc_rawq.ifq_mtx, "ppp_rawq", NULL, MTX_DEF);
  218         if_attach(&sc->sc_if);
  219         bpfattach(&sc->sc_if, DLT_PPP, PPP_HDRLEN);
  220         LIST_INSERT_HEAD(&ppp_softc_list, sc, sc_list);
  221 
  222         return (0);
  223 }
  224 
  225 static void
  226 ppp_clone_destroy(struct ifnet *ifp)
  227 {
  228         struct ppp_softc        *sc;
  229 
  230         sc = ifp->if_softc;
  231 
  232         LIST_REMOVE(sc, sc_list);
  233         bpfdetach(ifp);
  234         if_detach(ifp);
  235         mtx_destroy(&sc->sc_rawq.ifq_mtx);
  236         mtx_destroy(&sc->sc_fastq.ifq_mtx);
  237         mtx_destroy(&sc->sc_inq.ifq_mtx);
  238 }
  239 
  240 static int
  241 ppp_modevent(module_t mod, int type, void *data) 
  242 { 
  243         switch (type) { 
  244         case MOD_LOAD: 
  245                 if_clone_attach(&ppp_cloner);
  246 
  247                 netisr_register(NETISR_PPP, (netisr_t *)pppintr, NULL, 0);
  248                 /*
  249                  * XXX layering violation - if_ppp can work over any lower
  250                  * level transport that cares to attach to it.
  251                  */
  252                 pppasyncattach(NULL);
  253                 break; 
  254         case MOD_UNLOAD: 
  255                 /* XXX: layering violation */
  256                 pppasyncdetach();
  257 
  258                 netisr_unregister(NETISR_PPP);
  259 
  260                 if_clone_detach(&ppp_cloner);
  261 
  262                 while (!LIST_EMPTY(&ppp_softc_list))
  263                         ppp_clone_destroy(
  264                             &LIST_FIRST(&ppp_softc_list)->sc_if);
  265                 break; 
  266         } 
  267         return 0; 
  268 } 
  269 
  270 static moduledata_t ppp_mod = { 
  271         "if_ppp", 
  272         ppp_modevent, 
  273         0
  274 }; 
  275 
  276 DECLARE_MODULE(if_ppp, ppp_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
  277 
  278 /*
  279  * Allocate a ppp interface unit and initialize it.
  280  */
  281 struct ppp_softc *
  282 pppalloc(pid)
  283     pid_t pid;
  284 {
  285     int i;
  286     char tmpname[IFNAMSIZ];
  287     struct ifnet *ifp;
  288     struct ppp_softc *sc;
  289 
  290     LIST_FOREACH(sc, &ppp_softc_list, sc_list) {
  291         if (sc->sc_xfer == pid) {
  292             sc->sc_xfer = 0;
  293             return sc;
  294         }
  295     }
  296     LIST_FOREACH(sc, &ppp_softc_list, sc_list) {
  297         if (sc->sc_devp == NULL)
  298             break;
  299     }
  300     /* Try to clone an interface if we don't have a free one */
  301     if (sc == NULL) {
  302         strcpy(tmpname, PPPNAME);
  303         if (if_clone_create(tmpname, sizeof(tmpname)) != 0)
  304             return NULL;
  305         ifp = ifunit(tmpname);
  306         if (ifp == NULL)
  307             return NULL;
  308         sc = ifp->if_softc;
  309     }
  310     if (sc == NULL || sc->sc_devp != NULL)
  311         return NULL;
  312 
  313     sc->sc_flags = 0;
  314     sc->sc_mru = PPP_MRU;
  315     sc->sc_relinq = NULL;
  316     bzero((char *)&sc->sc_stats, sizeof(sc->sc_stats));
  317 #ifdef VJC
  318     MALLOC(sc->sc_comp, struct slcompress *, sizeof(struct slcompress),
  319            M_DEVBUF, M_NOWAIT);
  320     if (sc->sc_comp)
  321         sl_compress_init(sc->sc_comp, -1);
  322 #endif
  323 #ifdef PPP_COMPRESS
  324     sc->sc_xc_state = NULL;
  325     sc->sc_rc_state = NULL;
  326 #endif /* PPP_COMPRESS */
  327     for (i = 0; i < NUM_NP; ++i)
  328         sc->sc_npmode[i] = NPMODE_ERROR;
  329     sc->sc_npqueue = NULL;
  330     sc->sc_npqtail = &sc->sc_npqueue;
  331     sc->sc_last_sent = sc->sc_last_recv = time_second;
  332 
  333     return sc;
  334 }
  335 
  336 /*
  337  * Deallocate a ppp unit.  Must be called at splsoftnet or higher.
  338  */
  339 void
  340 pppdealloc(sc)
  341     struct ppp_softc *sc;
  342 {
  343     struct mbuf *m;
  344 
  345     if_down(&sc->sc_if);
  346     sc->sc_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
  347     getmicrotime(&sc->sc_if.if_lastchange);
  348     sc->sc_devp = NULL;
  349     sc->sc_xfer = 0;
  350     IF_DRAIN(&sc->sc_rawq);
  351     IF_DRAIN(&sc->sc_inq);
  352     IF_DRAIN(&sc->sc_fastq);
  353     while ((m = sc->sc_npqueue) != NULL) {
  354         sc->sc_npqueue = m->m_nextpkt;
  355         m_freem(m);
  356     }
  357 #ifdef PPP_COMPRESS
  358     ppp_ccp_closed(sc);
  359     sc->sc_xc_state = NULL;
  360     sc->sc_rc_state = NULL;
  361 #endif /* PPP_COMPRESS */
  362 #ifdef PPP_FILTER
  363     if (sc->sc_pass_filt.bf_insns != 0) {
  364         free(sc->sc_pass_filt.bf_insns, M_DEVBUF);
  365         sc->sc_pass_filt.bf_insns = 0;
  366         sc->sc_pass_filt.bf_len = 0;
  367     }
  368     if (sc->sc_active_filt.bf_insns != 0) {
  369         free(sc->sc_active_filt.bf_insns, M_DEVBUF);
  370         sc->sc_active_filt.bf_insns = 0;
  371         sc->sc_active_filt.bf_len = 0;
  372     }
  373 #endif /* PPP_FILTER */
  374 #ifdef VJC
  375     if (sc->sc_comp != 0) {
  376         free(sc->sc_comp, M_DEVBUF);
  377         sc->sc_comp = 0;
  378     }
  379 #endif
  380 }
  381 
  382 /*
  383  * Ioctl routine for generic ppp devices.
  384  */
  385 int
  386 pppioctl(sc, cmd, data, flag, td)
  387     struct ppp_softc *sc;
  388     u_long cmd;
  389     caddr_t data;
  390     int flag;
  391     struct thread *td;
  392 {
  393     struct proc *p = td->td_proc;
  394     int s, flags, mru, npx;
  395     u_int nb;
  396     int error = 0;
  397     struct ppp_option_data *odp;
  398     struct compressor **cp;
  399     struct npioctl *npi;
  400     time_t t;
  401 #ifdef PPP_FILTER
  402     struct bpf_program *bp, *nbp;
  403     struct bpf_insn *newcode, *oldcode;
  404     int newcodelen;
  405 #endif /* PPP_FILTER */
  406 #ifdef  PPP_COMPRESS
  407     u_char ccp_option[CCP_MAX_OPTION_LENGTH];
  408 #endif
  409 
  410     switch (cmd) {
  411     case FIONREAD:
  412         *(int *)data = sc->sc_inq.ifq_len;
  413         break;
  414 
  415     case PPPIOCGUNIT:
  416         *(int *)data = sc->sc_if.if_dunit;
  417         break;
  418 
  419     case PPPIOCGFLAGS:
  420         *(u_int *)data = sc->sc_flags;
  421         break;
  422 
  423     case PPPIOCSFLAGS:
  424         if ((error = suser(td)) != 0)
  425             break;
  426         flags = *(int *)data & SC_MASK;
  427         s = splsoftnet();
  428 #ifdef PPP_COMPRESS
  429         if (sc->sc_flags & SC_CCP_OPEN && !(flags & SC_CCP_OPEN))
  430             ppp_ccp_closed(sc);
  431 #endif
  432         splimp();
  433         sc->sc_flags = (sc->sc_flags & ~SC_MASK) | flags;
  434         splx(s);
  435         break;
  436 
  437     case PPPIOCSMRU:
  438         if ((error = suser(td)) != 0)
  439             return (error);
  440         mru = *(int *)data;
  441         if (mru >= PPP_MRU && mru <= PPP_MAXMRU)
  442             sc->sc_mru = mru;
  443         break;
  444 
  445     case PPPIOCGMRU:
  446         *(int *)data = sc->sc_mru;
  447         break;
  448 
  449 #ifdef VJC
  450     case PPPIOCSMAXCID:
  451         if ((error = suser(td)) != 0)
  452             break;
  453         if (sc->sc_comp) {
  454             s = splsoftnet();
  455             sl_compress_init(sc->sc_comp, *(int *)data);
  456             splx(s);
  457         }
  458         break;
  459 #endif
  460 
  461     case PPPIOCXFERUNIT:
  462         if ((error = suser(td)) != 0)
  463             break;
  464         sc->sc_xfer = p->p_pid;
  465         break;
  466 
  467 #ifdef PPP_COMPRESS
  468     case PPPIOCSCOMPRESS:
  469         if ((error = suser(td)) != 0)
  470             break;
  471         odp = (struct ppp_option_data *) data;
  472         nb = odp->length;
  473         if (nb > sizeof(ccp_option))
  474             nb = sizeof(ccp_option);
  475         if ((error = copyin(odp->ptr, ccp_option, nb)) != 0)
  476             break;
  477         if (ccp_option[1] < 2) {  /* preliminary check on the length byte */
  478             error = EINVAL;
  479             break;
  480         }
  481         for (cp = ppp_compressors; *cp != NULL; ++cp)
  482             if ((*cp)->compress_proto == ccp_option[0]) {
  483                 /*
  484                  * Found a handler for the protocol - try to allocate
  485                  * a compressor or decompressor.
  486                  */
  487                 error = 0;
  488                 if (odp->transmit) {
  489                     s = splsoftnet();
  490                     if (sc->sc_xc_state != NULL)
  491                         (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
  492                     sc->sc_xcomp = *cp;
  493                     sc->sc_xc_state = (*cp)->comp_alloc(ccp_option, nb);
  494                     if (sc->sc_xc_state == NULL) {
  495                         if (sc->sc_flags & SC_DEBUG)
  496                             if_printf(&sc->sc_if, "comp_alloc failed\n");
  497                         error = ENOBUFS;
  498                     }
  499                     splimp();
  500                     sc->sc_flags &= ~SC_COMP_RUN;
  501                     splx(s);
  502                 } else {
  503                     s = splsoftnet();
  504                     if (sc->sc_rc_state != NULL)
  505                         (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
  506                     sc->sc_rcomp = *cp;
  507                     sc->sc_rc_state = (*cp)->decomp_alloc(ccp_option, nb);
  508                     if (sc->sc_rc_state == NULL) {
  509                         if (sc->sc_flags & SC_DEBUG)
  510                             if_printf(&sc->sc_if, "decomp_alloc failed\n");
  511                         error = ENOBUFS;
  512                     }
  513                     splimp();
  514                     sc->sc_flags &= ~SC_DECOMP_RUN;
  515                     splx(s);
  516                 }
  517                 break;
  518             }
  519         if (sc->sc_flags & SC_DEBUG)
  520             if_printf(&sc->sc_if, "no compressor for [%x %x %x], %x\n",
  521                    ccp_option[0], ccp_option[1], ccp_option[2], nb);
  522         error = EINVAL;         /* no handler found */
  523         break;
  524 #endif /* PPP_COMPRESS */
  525 
  526     case PPPIOCGNPMODE:
  527     case PPPIOCSNPMODE:
  528         npi = (struct npioctl *) data;
  529         npx = 0;        /* XXX: quiet gcc */
  530         switch (npi->protocol) {
  531         case PPP_IP:
  532             npx = NP_IP;
  533             break;
  534         default:
  535             error = EINVAL;
  536         }
  537         if (error)
  538             break;
  539         if (cmd == PPPIOCGNPMODE) {
  540             npi->mode = sc->sc_npmode[npx];
  541         } else {
  542             if ((error = suser(td)) != 0)
  543                 break;
  544             if (npi->mode != sc->sc_npmode[npx]) {
  545                 s = splsoftnet();
  546                 sc->sc_npmode[npx] = npi->mode;
  547                 if (npi->mode != NPMODE_QUEUE) {
  548                     ppp_requeue(sc);
  549                     (*sc->sc_start)(sc);
  550                 }
  551                 splx(s);
  552             }
  553         }
  554         break;
  555 
  556     case PPPIOCGIDLE:
  557         s = splsoftnet();
  558         t = time_second;
  559         ((struct ppp_idle *)data)->xmit_idle = t - sc->sc_last_sent;
  560         ((struct ppp_idle *)data)->recv_idle = t - sc->sc_last_recv;
  561         splx(s);
  562         break;
  563 
  564 #ifdef PPP_FILTER
  565     case PPPIOCSPASS:
  566     case PPPIOCSACTIVE:
  567         nbp = (struct bpf_program *) data;
  568         if ((unsigned) nbp->bf_len > BPF_MAXINSNS) {
  569             error = EINVAL;
  570             break;
  571         }
  572         newcodelen = nbp->bf_len * sizeof(struct bpf_insn);
  573         if (newcodelen != 0) {
  574             MALLOC(newcode, struct bpf_insn *, newcodelen, M_DEVBUF, M_WAITOK);
  575             if (newcode == 0) {
  576                 error = EINVAL;         /* or sumpin */
  577                 break;
  578             }
  579             if ((error = copyin((caddr_t)nbp->bf_insns, (caddr_t)newcode,
  580                                newcodelen)) != 0) {
  581                 free(newcode, M_DEVBUF);
  582                 break;
  583             }
  584             if (!bpf_validate(newcode, nbp->bf_len)) {
  585                 free(newcode, M_DEVBUF);
  586                 error = EINVAL;
  587                 break;
  588             }
  589         } else
  590             newcode = 0;
  591         bp = (cmd == PPPIOCSPASS)? &sc->sc_pass_filt: &sc->sc_active_filt;
  592         oldcode = bp->bf_insns;
  593         s = splimp();
  594         bp->bf_len = nbp->bf_len;
  595         bp->bf_insns = newcode;
  596         splx(s);
  597         if (oldcode != 0)
  598             free(oldcode, M_DEVBUF);
  599         break;
  600 #endif
  601 
  602     default:
  603         error = ENOIOCTL;
  604         break;
  605     }
  606     return (error);
  607 }
  608 
  609 /*
  610  * Process an ioctl request to the ppp network interface.
  611  */
  612 static int
  613 pppsioctl(ifp, cmd, data)
  614     register struct ifnet *ifp;
  615     u_long cmd;
  616     caddr_t data;
  617 {
  618     struct thread *td = curthread;      /* XXX */
  619     register struct ppp_softc *sc = ifp->if_softc;
  620     register struct ifaddr *ifa = (struct ifaddr *)data;
  621     register struct ifreq *ifr = (struct ifreq *)data;
  622     struct ppp_stats *psp;
  623 #ifdef  PPP_COMPRESS
  624     struct ppp_comp_stats *pcp;
  625 #endif
  626     int s = splimp(), error = 0;
  627 
  628     switch (cmd) {
  629     case SIOCSIFFLAGS:
  630         if ((ifp->if_flags & IFF_RUNNING) == 0)
  631             ifp->if_flags &= ~IFF_UP;
  632         break;
  633 
  634     case SIOCSIFADDR:
  635     case SIOCAIFADDR:
  636         switch(ifa->ifa_addr->sa_family) {
  637 #ifdef INET
  638         case AF_INET:
  639             break;
  640 #endif
  641 #ifdef IPX
  642         case AF_IPX:
  643             break;
  644 #endif
  645         default:
  646             error = EAFNOSUPPORT;
  647             break;
  648         }
  649         break;
  650 
  651     case SIOCSIFDSTADDR:
  652         switch(ifa->ifa_addr->sa_family) {
  653 #ifdef INET
  654         case AF_INET:
  655             break;
  656 #endif
  657 #ifdef IPX
  658         case AF_IPX:
  659             break;
  660 #endif
  661         default:
  662             error = EAFNOSUPPORT;
  663             break;
  664         }
  665         break;
  666 
  667     case SIOCSIFMTU:
  668         if ((error = suser(td)) != 0)
  669             break;
  670         if (ifr->ifr_mtu > PPP_MAXMTU)
  671             error = EINVAL;
  672         else {
  673             sc->sc_if.if_mtu = ifr->ifr_mtu;
  674             if (sc->sc_setmtu)
  675                     (*sc->sc_setmtu)(sc);
  676         }
  677         break;
  678 
  679     case SIOCGIFMTU:
  680         ifr->ifr_mtu = sc->sc_if.if_mtu;
  681         break;
  682 
  683     case SIOCADDMULTI:
  684     case SIOCDELMULTI:
  685         if (ifr == 0) {
  686             error = EAFNOSUPPORT;
  687             break;
  688         }
  689         switch(ifr->ifr_addr.sa_family) {
  690 #ifdef INET
  691         case AF_INET:
  692             break;
  693 #endif
  694         default:
  695             error = EAFNOSUPPORT;
  696             break;
  697         }
  698         break;
  699 
  700     case SIOCGPPPSTATS:
  701         psp = &((struct ifpppstatsreq *) data)->stats;
  702         bzero(psp, sizeof(*psp));
  703         psp->p = sc->sc_stats;
  704 #if defined(VJC) && !defined(SL_NO_STATS)
  705         if (sc->sc_comp) {
  706             psp->vj.vjs_packets = sc->sc_comp->sls_packets;
  707             psp->vj.vjs_compressed = sc->sc_comp->sls_compressed;
  708             psp->vj.vjs_searches = sc->sc_comp->sls_searches;
  709             psp->vj.vjs_misses = sc->sc_comp->sls_misses;
  710             psp->vj.vjs_uncompressedin = sc->sc_comp->sls_uncompressedin;
  711             psp->vj.vjs_compressedin = sc->sc_comp->sls_compressedin;
  712             psp->vj.vjs_errorin = sc->sc_comp->sls_errorin;
  713             psp->vj.vjs_tossed = sc->sc_comp->sls_tossed;
  714         }
  715 #endif /* VJC */
  716         break;
  717 
  718 #ifdef PPP_COMPRESS
  719     case SIOCGPPPCSTATS:
  720         pcp = &((struct ifpppcstatsreq *) data)->stats;
  721         bzero(pcp, sizeof(*pcp));
  722         if (sc->sc_xc_state != NULL)
  723             (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c);
  724         if (sc->sc_rc_state != NULL)
  725             (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d);
  726         break;
  727 #endif /* PPP_COMPRESS */
  728 
  729     default:
  730         error = ENOTTY;
  731     }
  732     splx(s);
  733     return (error);
  734 }
  735 
  736 /*
  737  * Queue a packet.  Start transmission if not active.
  738  * Packet is placed in Information field of PPP frame.
  739  * Called at splnet as the if->if_output handler.
  740  * Called at splnet from pppwrite().
  741  */
  742 int
  743 pppoutput(ifp, m0, dst, rtp)
  744     struct ifnet *ifp;
  745     struct mbuf *m0;
  746     struct sockaddr *dst;
  747     struct rtentry *rtp;
  748 {
  749     register struct ppp_softc *sc = ifp->if_softc;
  750     int protocol, address, control;
  751     u_char *cp;
  752     int s, error;
  753     struct ip *ip;
  754     struct ifqueue *ifq;
  755     enum NPmode mode;
  756     int len;
  757 
  758 #ifdef MAC
  759     error = mac_check_ifnet_transmit(ifp, m0);
  760     if (error)
  761         goto bad;
  762 #endif
  763 
  764     if (sc->sc_devp == NULL || (ifp->if_flags & IFF_RUNNING) == 0
  765         || ((ifp->if_flags & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC)) {
  766         error = ENETDOWN;       /* sort of */
  767         goto bad;
  768     }
  769 
  770     /*
  771      * Compute PPP header.
  772      */
  773     m0->m_flags &= ~M_HIGHPRI;
  774     switch (dst->sa_family) {
  775 #ifdef INET
  776     case AF_INET:
  777         address = PPP_ALLSTATIONS;
  778         control = PPP_UI;
  779         protocol = PPP_IP;
  780         mode = sc->sc_npmode[NP_IP];
  781 
  782         /*
  783          * If this packet has the "low delay" bit set in the IP header,
  784          * put it on the fastq instead.
  785          */
  786         ip = mtod(m0, struct ip *);
  787         if (ip->ip_tos & IPTOS_LOWDELAY)
  788             m0->m_flags |= M_HIGHPRI;
  789         break;
  790 #endif
  791 #ifdef IPX
  792     case AF_IPX:
  793         /*
  794          * This is pretty bogus.. We dont have an ipxcp module in pppd
  795          * yet to configure the link parameters.  Sigh. I guess a
  796          * manual ifconfig would do....  -Peter
  797          */
  798         address = PPP_ALLSTATIONS;
  799         control = PPP_UI;
  800         protocol = PPP_IPX;
  801         mode = NPMODE_PASS;
  802         break;
  803 #endif
  804     case AF_UNSPEC:
  805         address = PPP_ADDRESS(dst->sa_data);
  806         control = PPP_CONTROL(dst->sa_data);
  807         protocol = PPP_PROTOCOL(dst->sa_data);
  808         mode = NPMODE_PASS;
  809         break;
  810     default:
  811         if_printf(ifp, "af%d not supported\n", dst->sa_family);
  812         error = EAFNOSUPPORT;
  813         goto bad;
  814     }
  815 
  816     /*
  817      * Drop this packet, or return an error, if necessary.
  818      */
  819     if (mode == NPMODE_ERROR) {
  820         error = ENETDOWN;
  821         goto bad;
  822     }
  823     if (mode == NPMODE_DROP) {
  824         error = 0;
  825         goto bad;
  826     }
  827 
  828     /*
  829      * Add PPP header.  If no space in first mbuf, allocate another.
  830      * (This assumes M_LEADINGSPACE is always 0 for a cluster mbuf.)
  831      */
  832     if (M_LEADINGSPACE(m0) < PPP_HDRLEN) {
  833         m0 = m_prepend(m0, PPP_HDRLEN, M_DONTWAIT);
  834         if (m0 == 0) {
  835             error = ENOBUFS;
  836             goto bad;
  837         }
  838         m0->m_len = 0;
  839     } else
  840         m0->m_data -= PPP_HDRLEN;
  841 
  842     cp = mtod(m0, u_char *);
  843     *cp++ = address;
  844     *cp++ = control;
  845     *cp++ = protocol >> 8;
  846     *cp++ = protocol & 0xff;
  847     m0->m_len += PPP_HDRLEN;
  848 
  849     len = m_length(m0, NULL);
  850 
  851     if (sc->sc_flags & SC_LOG_OUTPKT) {
  852         printf("%s output: ", ifp->if_xname);
  853         pppdumpm(m0);
  854     }
  855 
  856     if ((protocol & 0x8000) == 0) {
  857 #ifdef PPP_FILTER
  858         /*
  859          * Apply the pass and active filters to the packet,
  860          * but only if it is a data packet.
  861          */
  862         *mtod(m0, u_char *) = 1;        /* indicates outbound */
  863         if (sc->sc_pass_filt.bf_insns != 0
  864             && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m0,
  865                           len, 0) == 0) {
  866             error = 0;          /* drop this packet */
  867             goto bad;
  868         }
  869 
  870         /*
  871          * Update the time we sent the most recent packet.
  872          */
  873         if (sc->sc_active_filt.bf_insns == 0
  874             || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m0, len, 0))
  875             sc->sc_last_sent = time_second;
  876 
  877         *mtod(m0, u_char *) = address;
  878 #else
  879         /*
  880          * Update the time we sent the most recent data packet.
  881          */
  882         sc->sc_last_sent = time_second;
  883 #endif /* PPP_FILTER */
  884     }
  885 
  886     /*
  887      * See if bpf wants to look at the packet.
  888      */
  889     BPF_MTAP(ifp, m0);
  890 
  891     /*
  892      * Put the packet on the appropriate queue.
  893      */
  894     s = splsoftnet();   /* redundant */
  895     if (mode == NPMODE_QUEUE) {
  896         /* XXX we should limit the number of packets on this queue */
  897         *sc->sc_npqtail = m0;
  898         m0->m_nextpkt = NULL;
  899         sc->sc_npqtail = &m0->m_nextpkt;
  900     } else {
  901         /* fastq and if_snd are emptied at spl[soft]net now */
  902         ifq = (m0->m_flags & M_HIGHPRI)? &sc->sc_fastq: &ifp->if_snd;
  903         IF_LOCK(ifq);
  904         if (_IF_QFULL(ifq) && dst->sa_family != AF_UNSPEC) {
  905             _IF_DROP(ifq);
  906             IF_UNLOCK(ifq);
  907             sc->sc_if.if_oerrors++;
  908             sc->sc_stats.ppp_oerrors++;
  909             error = ENOBUFS;
  910             goto bad;
  911         }
  912         _IF_ENQUEUE(ifq, m0);
  913         IF_UNLOCK(ifq);
  914         (*sc->sc_start)(sc);
  915     }
  916     getmicrotime(&ifp->if_lastchange);
  917     ifp->if_opackets++;
  918     ifp->if_obytes += len;
  919 
  920     splx(s);
  921     return (0);
  922 
  923 bad:
  924     m_freem(m0);
  925     return (error);
  926 }
  927 
  928 /*
  929  * After a change in the NPmode for some NP, move packets from the
  930  * npqueue to the send queue or the fast queue as appropriate.
  931  * Should be called at spl[soft]net.
  932  */
  933 static void
  934 ppp_requeue(sc)
  935     struct ppp_softc *sc;
  936 {
  937     struct mbuf *m, **mpp;
  938     struct ifqueue *ifq;
  939     enum NPmode mode;
  940 
  941     for (mpp = &sc->sc_npqueue; (m = *mpp) != NULL; ) {
  942         switch (PPP_PROTOCOL(mtod(m, u_char *))) {
  943         case PPP_IP:
  944             mode = sc->sc_npmode[NP_IP];
  945             break;
  946         default:
  947             mode = NPMODE_PASS;
  948         }
  949 
  950         switch (mode) {
  951         case NPMODE_PASS:
  952             /*
  953              * This packet can now go on one of the queues to be sent.
  954              */
  955             *mpp = m->m_nextpkt;
  956             m->m_nextpkt = NULL;
  957             ifq = (m->m_flags & M_HIGHPRI)? &sc->sc_fastq: &sc->sc_if.if_snd;
  958             if (! IF_HANDOFF(ifq, m, NULL)) {
  959                 sc->sc_if.if_oerrors++;
  960                 sc->sc_stats.ppp_oerrors++;
  961             }
  962             break;
  963 
  964         case NPMODE_DROP:
  965         case NPMODE_ERROR:
  966             *mpp = m->m_nextpkt;
  967             m_freem(m);
  968             break;
  969 
  970         case NPMODE_QUEUE:
  971             mpp = &m->m_nextpkt;
  972             break;
  973         }
  974     }
  975     sc->sc_npqtail = mpp;
  976 }
  977 
  978 /*
  979  * Transmitter has finished outputting some stuff;
  980  * remember to call sc->sc_start later at splsoftnet.
  981  */
  982 void
  983 ppp_restart(sc)
  984     struct ppp_softc *sc;
  985 {
  986     int s = splimp();
  987 
  988     sc->sc_flags &= ~SC_TBUSY;
  989     schednetisr(NETISR_PPP);
  990     splx(s);
  991 }
  992 
  993 
  994 /*
  995  * Get a packet to send.  This procedure is intended to be called at
  996  * splsoftnet, since it may involve time-consuming operations such as
  997  * applying VJ compression, packet compression, address/control and/or
  998  * protocol field compression to the packet.
  999  */
 1000 struct mbuf *
 1001 ppp_dequeue(sc)
 1002     struct ppp_softc *sc;
 1003 {
 1004     struct mbuf *m, *mp;
 1005     u_char *cp;
 1006     int address, control, protocol;
 1007 
 1008     /*
 1009      * Grab a packet to send: first try the fast queue, then the
 1010      * normal queue.
 1011      */
 1012     IF_DEQUEUE(&sc->sc_fastq, m);
 1013     if (m == NULL)
 1014         IF_DEQUEUE(&sc->sc_if.if_snd, m);
 1015     if (m == NULL)
 1016         return NULL;
 1017 
 1018     ++sc->sc_stats.ppp_opackets;
 1019 
 1020     /*
 1021      * Extract the ppp header of the new packet.
 1022      * The ppp header will be in one mbuf.
 1023      */
 1024     cp = mtod(m, u_char *);
 1025     address = PPP_ADDRESS(cp);
 1026     control = PPP_CONTROL(cp);
 1027     protocol = PPP_PROTOCOL(cp);
 1028 
 1029     switch (protocol) {
 1030     case PPP_IP:
 1031 #ifdef VJC
 1032         /*
 1033          * If the packet is a TCP/IP packet, see if we can compress it.
 1034          */
 1035         if ((sc->sc_flags & SC_COMP_TCP) && sc->sc_comp != NULL) {
 1036             struct ip *ip;
 1037             int type;
 1038 
 1039             mp = m;
 1040             ip = (struct ip *) (cp + PPP_HDRLEN);
 1041             if (mp->m_len <= PPP_HDRLEN) {
 1042                 mp = mp->m_next;
 1043                 if (mp == NULL)
 1044                     break;
 1045                 ip = mtod(mp, struct ip *);
 1046             }
 1047             /* this code assumes the IP/TCP header is in one non-shared mbuf */
 1048             if (ip->ip_p == IPPROTO_TCP) {
 1049                 type = sl_compress_tcp(mp, ip, sc->sc_comp,
 1050                                        !(sc->sc_flags & SC_NO_TCP_CCID));
 1051                 switch (type) {
 1052                 case TYPE_UNCOMPRESSED_TCP:
 1053                     protocol = PPP_VJC_UNCOMP;
 1054                     break;
 1055                 case TYPE_COMPRESSED_TCP:
 1056                     protocol = PPP_VJC_COMP;
 1057                     cp = mtod(m, u_char *);
 1058                     cp[0] = address;    /* header has moved */
 1059                     cp[1] = control;
 1060                     cp[2] = 0;
 1061                     break;
 1062                 }
 1063                 cp[3] = protocol;       /* update protocol in PPP header */
 1064             }
 1065         }
 1066 #endif  /* VJC */
 1067         break;
 1068 
 1069 #ifdef PPP_COMPRESS
 1070     case PPP_CCP:
 1071         ppp_ccp(sc, m, 0);
 1072         break;
 1073 #endif  /* PPP_COMPRESS */
 1074     }
 1075 
 1076 #ifdef PPP_COMPRESS
 1077     if (protocol != PPP_LCP && protocol != PPP_CCP
 1078         && sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
 1079         struct mbuf *mcomp = NULL;
 1080         int slen, clen;
 1081 
 1082         slen = m_length(m, NULL);
 1083         clen = (*sc->sc_xcomp->compress)
 1084             (sc->sc_xc_state, &mcomp, m, slen, sc->sc_if.if_mtu + PPP_HDRLEN);
 1085         if (mcomp != NULL) {
 1086             if (sc->sc_flags & SC_CCP_UP) {
 1087                 /* Send the compressed packet instead of the original. */
 1088                 m_freem(m);
 1089                 m = mcomp;
 1090                 cp = mtod(m, u_char *);
 1091                 protocol = cp[3];
 1092             } else {
 1093                 /* Can't transmit compressed packets until CCP is up. */
 1094                 m_freem(mcomp);
 1095             }
 1096         }
 1097     }
 1098 #endif  /* PPP_COMPRESS */
 1099 
 1100     /*
 1101      * Compress the address/control and protocol, if possible.
 1102      */
 1103     if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
 1104         control == PPP_UI && protocol != PPP_ALLSTATIONS &&
 1105         protocol != PPP_LCP) {
 1106         /* can compress address/control */
 1107         m->m_data += 2;
 1108         m->m_len -= 2;
 1109     }
 1110     if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
 1111         /* can compress protocol */
 1112         if (mtod(m, u_char *) == cp) {
 1113             cp[2] = cp[1];      /* move address/control up */
 1114             cp[1] = cp[0];
 1115         }
 1116         ++m->m_data;
 1117         --m->m_len;
 1118     }
 1119 
 1120     return m;
 1121 }
 1122 
 1123 /*
 1124  * Software interrupt routine, called at spl[soft]net.
 1125  */
 1126 static void
 1127 pppintr()
 1128 {
 1129     struct ppp_softc *sc;
 1130     int s;
 1131     struct mbuf *m;
 1132 
 1133     GIANT_REQUIRED;
 1134 
 1135     LIST_FOREACH(sc, &ppp_softc_list, sc_list) {
 1136         s = splimp();
 1137         if (!(sc->sc_flags & SC_TBUSY)
 1138             && (sc->sc_if.if_snd.ifq_head || sc->sc_fastq.ifq_head)) {
 1139             sc->sc_flags |= SC_TBUSY;
 1140             splx(s);
 1141             (*sc->sc_start)(sc);
 1142         } else
 1143             splx(s);
 1144         for (;;) {
 1145             s = splimp();
 1146             IF_DEQUEUE(&sc->sc_rawq, m);
 1147             splx(s);
 1148             if (m == NULL)
 1149                 break;
 1150 #ifdef MAC
 1151             mac_create_mbuf_from_ifnet(&sc->sc_if, m);
 1152 #endif
 1153             ppp_inproc(sc, m);
 1154         }
 1155     }
 1156 }
 1157 
 1158 #ifdef PPP_COMPRESS
 1159 /*
 1160  * Handle a CCP packet.  `rcvd' is 1 if the packet was received,
 1161  * 0 if it is about to be transmitted.
 1162  */
 1163 static void
 1164 ppp_ccp(sc, m, rcvd)
 1165     struct ppp_softc *sc;
 1166     struct mbuf *m;
 1167     int rcvd;
 1168 {
 1169     u_char *dp, *ep;
 1170     struct mbuf *mp;
 1171     int slen, s;
 1172 
 1173     /*
 1174      * Get a pointer to the data after the PPP header.
 1175      */
 1176     if (m->m_len <= PPP_HDRLEN) {
 1177         mp = m->m_next;
 1178         if (mp == NULL)
 1179             return;
 1180         dp = (mp != NULL)? mtod(mp, u_char *): NULL;
 1181     } else {
 1182         mp = m;
 1183         dp = mtod(mp, u_char *) + PPP_HDRLEN;
 1184     }
 1185 
 1186     ep = mtod(mp, u_char *) + mp->m_len;
 1187     if (dp + CCP_HDRLEN > ep)
 1188         return;
 1189     slen = CCP_LENGTH(dp);
 1190     if (dp + slen > ep) {
 1191         if (sc->sc_flags & SC_DEBUG)
 1192             printf("if_ppp/ccp: not enough data in mbuf (%p+%x > %p+%x)\n",
 1193                    dp, slen, mtod(mp, u_char *), mp->m_len);
 1194         return;
 1195     }
 1196 
 1197     switch (CCP_CODE(dp)) {
 1198     case CCP_CONFREQ:
 1199     case CCP_TERMREQ:
 1200     case CCP_TERMACK:
 1201         /* CCP must be going down - disable compression */
 1202         if (sc->sc_flags & SC_CCP_UP) {
 1203             s = splimp();
 1204             sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
 1205             splx(s);
 1206         }
 1207         break;
 1208 
 1209     case CCP_CONFACK:
 1210         if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
 1211             && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
 1212             && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
 1213             if (!rcvd) {
 1214                 /* we're agreeing to send compressed packets. */
 1215                 if (sc->sc_xc_state != NULL
 1216                     && (*sc->sc_xcomp->comp_init)
 1217                         (sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
 1218                          sc->sc_if.if_dunit, 0, sc->sc_flags & SC_DEBUG)) {
 1219                     s = splimp();
 1220                     sc->sc_flags |= SC_COMP_RUN;
 1221                     splx(s);
 1222                 }
 1223             } else {
 1224                 /* peer is agreeing to send compressed packets. */
 1225                 if (sc->sc_rc_state != NULL
 1226                     && (*sc->sc_rcomp->decomp_init)
 1227                         (sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
 1228                          sc->sc_if.if_dunit, 0, sc->sc_mru,
 1229                          sc->sc_flags & SC_DEBUG)) {
 1230                     s = splimp();
 1231                     sc->sc_flags |= SC_DECOMP_RUN;
 1232                     sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
 1233                     splx(s);
 1234                 }
 1235             }
 1236         }
 1237         break;
 1238 
 1239     case CCP_RESETACK:
 1240         if (sc->sc_flags & SC_CCP_UP) {
 1241             if (!rcvd) {
 1242                 if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN))
 1243                     (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
 1244             } else {
 1245                 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
 1246                     (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
 1247                     s = splimp();
 1248                     sc->sc_flags &= ~SC_DC_ERROR;
 1249                     splx(s);
 1250                 }
 1251             }
 1252         }
 1253         break;
 1254     }
 1255 }
 1256 
 1257 /*
 1258  * CCP is down; free (de)compressor state if necessary.
 1259  */
 1260 static void
 1261 ppp_ccp_closed(sc)
 1262     struct ppp_softc *sc;
 1263 {
 1264     if (sc->sc_xc_state) {
 1265         (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
 1266         sc->sc_xc_state = NULL;
 1267     }
 1268     if (sc->sc_rc_state) {
 1269         (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
 1270         sc->sc_rc_state = NULL;
 1271     }
 1272 }
 1273 #endif /* PPP_COMPRESS */
 1274 
 1275 /*
 1276  * PPP packet input routine.
 1277  * The caller has checked and removed the FCS and has inserted
 1278  * the address/control bytes and the protocol high byte if they
 1279  * were omitted.
 1280  */
 1281 void
 1282 ppppktin(sc, m, lost)
 1283     struct ppp_softc *sc;
 1284     struct mbuf *m;
 1285     int lost;
 1286 {
 1287     int s = splimp();
 1288 
 1289     if (lost)
 1290         m->m_flags |= M_ERRMARK;
 1291     IF_ENQUEUE(&sc->sc_rawq, m);
 1292     schednetisr(NETISR_PPP);
 1293     splx(s);
 1294 }
 1295 
 1296 /*
 1297  * Process a received PPP packet, doing decompression as necessary.
 1298  * Should be called at splsoftnet.
 1299  */
 1300 #define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
 1301                          TYPE_UNCOMPRESSED_TCP)
 1302 
 1303 static void
 1304 ppp_inproc(sc, m)
 1305     struct ppp_softc *sc;
 1306     struct mbuf *m;
 1307 {
 1308     struct ifnet *ifp = &sc->sc_if;
 1309     int isr;
 1310     int s, ilen = 0, xlen, proto, rv;
 1311     u_char *cp, adrs, ctrl;
 1312     struct mbuf *mp, *dmp = NULL;
 1313     u_char *iphdr;
 1314     u_int hlen;
 1315 
 1316     sc->sc_stats.ppp_ipackets++;
 1317 
 1318     if (sc->sc_flags & SC_LOG_INPKT) {
 1319         ilen = m_length(m, NULL);
 1320         if_printf(ifp, "got %d bytes\n", ilen);
 1321         pppdumpm(m);
 1322     }
 1323 
 1324     cp = mtod(m, u_char *);
 1325     adrs = PPP_ADDRESS(cp);
 1326     ctrl = PPP_CONTROL(cp);
 1327     proto = PPP_PROTOCOL(cp);
 1328 
 1329     if (m->m_flags & M_ERRMARK) {
 1330         m->m_flags &= ~M_ERRMARK;
 1331         s = splimp();
 1332         sc->sc_flags |= SC_VJ_RESET;
 1333         splx(s);
 1334     }
 1335 
 1336 #ifdef PPP_COMPRESS
 1337     /*
 1338      * Decompress this packet if necessary, update the receiver's
 1339      * dictionary, or take appropriate action on a CCP packet.
 1340      */
 1341     if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
 1342         && !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
 1343         /* decompress this packet */
 1344         rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
 1345         if (rv == DECOMP_OK) {
 1346             m_freem(m);
 1347             if (dmp == NULL) {
 1348                 /* no error, but no decompressed packet produced */
 1349                 return;
 1350             }
 1351             m = dmp;
 1352             cp = mtod(m, u_char *);
 1353             proto = PPP_PROTOCOL(cp);
 1354 
 1355         } else {
 1356             /*
 1357              * An error has occurred in decompression.
 1358              * Pass the compressed packet up to pppd, which may take
 1359              * CCP down or issue a Reset-Req.
 1360              */
 1361             if (sc->sc_flags & SC_DEBUG)
 1362                 if_printf(ifp, "decompress failed %d\n", rv);
 1363             s = splimp();
 1364             sc->sc_flags |= SC_VJ_RESET;
 1365             if (rv == DECOMP_ERROR)
 1366                 sc->sc_flags |= SC_DC_ERROR;
 1367             else
 1368                 sc->sc_flags |= SC_DC_FERROR;
 1369             splx(s);
 1370         }
 1371 
 1372     } else {
 1373         if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
 1374             (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
 1375         }
 1376         if (proto == PPP_CCP) {
 1377             ppp_ccp(sc, m, 1);
 1378         }
 1379     }
 1380 #endif
 1381 
 1382     ilen = m_length(m, NULL);
 1383 
 1384 #ifdef VJC
 1385     if (sc->sc_flags & SC_VJ_RESET) {
 1386         /*
 1387          * If we've missed a packet, we must toss subsequent compressed
 1388          * packets which don't have an explicit connection ID.
 1389          */
 1390         if (sc->sc_comp)
 1391             sl_uncompress_tcp(NULL, 0, TYPE_ERROR, sc->sc_comp);
 1392         s = splimp();
 1393         sc->sc_flags &= ~SC_VJ_RESET;
 1394         splx(s);
 1395     }
 1396 
 1397     /*
 1398      * See if we have a VJ-compressed packet to uncompress.
 1399      */
 1400     if (proto == PPP_VJC_COMP) {
 1401         if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
 1402             goto bad;
 1403 
 1404         xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
 1405                                       ilen - PPP_HDRLEN, TYPE_COMPRESSED_TCP,
 1406                                       sc->sc_comp, &iphdr, &hlen);
 1407 
 1408         if (xlen <= 0) {
 1409             if (sc->sc_flags & SC_DEBUG)
 1410                 if_printf(ifp, "VJ uncompress failed on type comp\n");
 1411             goto bad;
 1412         }
 1413 
 1414         /* Copy the PPP and IP headers into a new mbuf. */
 1415         MGETHDR(mp, M_DONTWAIT, MT_DATA);
 1416         if (mp == NULL)
 1417             goto bad;
 1418         mp->m_len = 0;
 1419         mp->m_next = NULL;
 1420         if (hlen + PPP_HDRLEN > MHLEN) {
 1421             MCLGET(mp, M_DONTWAIT);
 1422             if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) {
 1423                 m_freem(mp);
 1424                 goto bad;       /* lose if big headers and no clusters */
 1425             }
 1426         }
 1427 #ifdef MAC
 1428         mac_create_mbuf_from_mbuf(m, mp);
 1429 #endif
 1430         cp = mtod(mp, u_char *);
 1431         cp[0] = adrs;
 1432         cp[1] = ctrl;
 1433         cp[2] = 0;
 1434         cp[3] = PPP_IP;
 1435         proto = PPP_IP;
 1436         bcopy(iphdr, cp + PPP_HDRLEN, hlen);
 1437         mp->m_len = hlen + PPP_HDRLEN;
 1438 
 1439         /*
 1440          * Trim the PPP and VJ headers off the old mbuf
 1441          * and stick the new and old mbufs together.
 1442          */
 1443         m->m_data += PPP_HDRLEN + xlen;
 1444         m->m_len -= PPP_HDRLEN + xlen;
 1445         if (m->m_len <= M_TRAILINGSPACE(mp)) {
 1446             bcopy(mtod(m, u_char *), mtod(mp, u_char *) + mp->m_len, m->m_len);
 1447             mp->m_len += m->m_len;
 1448             mp->m_next = m_free(m);
 1449         } else {
 1450             mp->m_next = m;
 1451         }
 1452         m = mp;
 1453         ilen += hlen - xlen;
 1454 
 1455     } else if (proto == PPP_VJC_UNCOMP) {
 1456         if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
 1457             goto bad;
 1458 
 1459         xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
 1460                                       ilen - PPP_HDRLEN, TYPE_UNCOMPRESSED_TCP,
 1461                                       sc->sc_comp, &iphdr, &hlen);
 1462 
 1463         if (xlen < 0) {
 1464             if (sc->sc_flags & SC_DEBUG)
 1465                 if_printf(ifp, "VJ uncompress failed on type uncomp\n");
 1466             goto bad;
 1467         }
 1468 
 1469         proto = PPP_IP;
 1470         cp[3] = PPP_IP;
 1471     }
 1472 #endif /* VJC */
 1473 
 1474     /*
 1475      * If the packet will fit in a header mbuf, don't waste a
 1476      * whole cluster on it.
 1477      */
 1478     if (ilen <= MHLEN && M_IS_CLUSTER(m)) {
 1479         MGETHDR(mp, M_DONTWAIT, MT_DATA);
 1480         if (mp != NULL) {
 1481 #ifdef MAC
 1482             mac_create_mbuf_from_mbuf(m, mp);
 1483 #endif
 1484             m_copydata(m, 0, ilen, mtod(mp, caddr_t));
 1485             m_freem(m);
 1486             m = mp;
 1487             m->m_len = ilen;
 1488         }
 1489     }
 1490     m->m_pkthdr.len = ilen;
 1491     m->m_pkthdr.rcvif = ifp;
 1492 
 1493     if ((proto & 0x8000) == 0) {
 1494 #ifdef PPP_FILTER
 1495         /*
 1496          * See whether we want to pass this packet, and
 1497          * if it counts as link activity.
 1498          */
 1499         adrs = *mtod(m, u_char *);      /* save address field */
 1500         *mtod(m, u_char *) = 0;         /* indicate inbound */
 1501         if (sc->sc_pass_filt.bf_insns != 0
 1502             && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m,
 1503                           ilen, 0) == 0) {
 1504             /* drop this packet */
 1505             m_freem(m);
 1506             return;
 1507         }
 1508         if (sc->sc_active_filt.bf_insns == 0
 1509             || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m, ilen, 0))
 1510             sc->sc_last_recv = time_second;
 1511 
 1512         *mtod(m, u_char *) = adrs;
 1513 #else
 1514         /*
 1515          * Record the time that we received this packet.
 1516          */
 1517         sc->sc_last_recv = time_second;
 1518 #endif /* PPP_FILTER */
 1519     }
 1520 
 1521     /* See if bpf wants to look at the packet. */
 1522     BPF_MTAP(&sc->sc_if, m);
 1523 
 1524     isr = -1;
 1525     switch (proto) {
 1526 #ifdef INET
 1527     case PPP_IP:
 1528         /*
 1529          * IP packet - take off the ppp header and pass it up to IP.
 1530          */
 1531         if ((ifp->if_flags & IFF_UP) == 0
 1532             || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
 1533             /* interface is down - drop the packet. */
 1534             m_freem(m);
 1535             return;
 1536         }
 1537         m->m_pkthdr.len -= PPP_HDRLEN;
 1538         m->m_data += PPP_HDRLEN;
 1539         m->m_len -= PPP_HDRLEN;
 1540         if (ip_fastforward(m))
 1541             return;
 1542         isr = NETISR_IP;
 1543         break;
 1544 #endif
 1545 #ifdef IPX
 1546     case PPP_IPX:
 1547         /*
 1548          * IPX packet - take off the ppp header and pass it up to IPX.
 1549          */
 1550         if ((sc->sc_if.if_flags & IFF_UP) == 0
 1551             /* XXX: || sc->sc_npmode[NP_IPX] != NPMODE_PASS*/) {
 1552             /* interface is down - drop the packet. */
 1553             m_freem(m);
 1554             return;
 1555         }
 1556         m->m_pkthdr.len -= PPP_HDRLEN;
 1557         m->m_data += PPP_HDRLEN;
 1558         m->m_len -= PPP_HDRLEN;
 1559         isr = NETISR_IPX;
 1560         sc->sc_last_recv = time_second; /* update time of last pkt rcvd */
 1561         break;
 1562 #endif
 1563 
 1564     default:
 1565         /*
 1566          * Some other protocol - place on input queue for read().
 1567          */
 1568         break;
 1569     }
 1570 
 1571     if (isr == -1)
 1572       rv = IF_HANDOFF(&sc->sc_inq, m, NULL);
 1573     else
 1574       rv = netisr_queue(isr, m);
 1575     if (!rv) {
 1576         if (sc->sc_flags & SC_DEBUG)
 1577             if_printf(ifp, "input queue full\n");
 1578         ifp->if_iqdrops++;
 1579         goto bad;
 1580     }
 1581     ifp->if_ipackets++;
 1582     ifp->if_ibytes += ilen;
 1583     getmicrotime(&ifp->if_lastchange);
 1584 
 1585     if (isr == -1)
 1586         (*sc->sc_ctlp)(sc);
 1587 
 1588     return;
 1589 
 1590  bad:
 1591     if (m)
 1592         m_freem(m);
 1593     sc->sc_if.if_ierrors++;
 1594     sc->sc_stats.ppp_ierrors++;
 1595 }
 1596 
 1597 #define MAX_DUMP_BYTES  128
 1598 
 1599 static void
 1600 pppdumpm(m0)
 1601     struct mbuf *m0;
 1602 {
 1603     char buf[3*MAX_DUMP_BYTES+4];
 1604     char *bp = buf;
 1605     struct mbuf *m;
 1606 
 1607     for (m = m0; m; m = m->m_next) {
 1608         int l = m->m_len;
 1609         u_char *rptr = (u_char *)m->m_data;
 1610 
 1611         while (l--) {
 1612             if (bp > buf + (sizeof(buf) - 4))
 1613                 goto done;
 1614             *bp++ = hex2ascii(*rptr >> 4);
 1615             *bp++ = hex2ascii(*rptr++ & 0xf);
 1616         }
 1617 
 1618         if (m->m_next) {
 1619             if (bp > buf + (sizeof(buf) - 3))
 1620                 goto done;
 1621             *bp++ = '|';
 1622         } else
 1623             *bp++ = ' ';
 1624     }
 1625 done:
 1626     if (m)
 1627         *bp++ = '>';
 1628     *bp = 0;
 1629     printf("%s\n", buf);
 1630 }

Cache object: a88e4c060ccf6644876a814045cb7b00


[ 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.