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