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