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