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