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