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