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