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

Cache object: 1ff9ba020663faff6be83974992ad7fe


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