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

Cache object: 1ee73c9edd7cc2672fde90b00ccc3c8e


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