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

Cache object: 66b2e78eb226b1ee2f94f453ccb83a4e


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