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