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