Home | History | Annotate | Line # | Download | only in net
if_ppp.c revision 1.52
      1 /*	$NetBSD: if_ppp.c,v 1.52 1999/05/12 18:50:51 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 	/* These are no longer supported. */
    505 	return EOPNOTSUPP;
    506 
    507     case PPPIOCSIPASS:
    508     case PPPIOCSOPASS:
    509     case PPPIOCSIACTIVE:
    510     case PPPIOCSOACTIVE:
    511 	nbp = (struct bpf_program *) data;
    512 	if ((unsigned) nbp->bf_len > BPF_MAXINSNS)
    513 	    return EINVAL;
    514 	newcodelen = nbp->bf_len * sizeof(struct bpf_insn);
    515 	if (newcodelen != 0) {
    516 	    newcode = malloc(newcodelen, M_DEVBUF, M_WAITOK);
    517 	    /* WAITOK -- malloc() never fails. */
    518 	    if ((error = copyin((caddr_t)nbp->bf_insns, (caddr_t)newcode,
    519 			       newcodelen)) != 0) {
    520 		free(newcode, M_DEVBUF);
    521 		return error;
    522 	    }
    523 	    if (!bpf_validate(newcode, nbp->bf_len)) {
    524 		free(newcode, M_DEVBUF);
    525 		return EINVAL;
    526 	    }
    527 	} else
    528 	    newcode = 0;
    529 	switch (cmd) {
    530 	case PPPIOCSIPASS:
    531 	    bp = &sc->sc_pass_filt_in;
    532 	    break;
    533 
    534 	case PPPIOCSOPASS:
    535 	    bp = &sc->sc_pass_filt_out;
    536 	    break;
    537 
    538 	case PPPIOCSIACTIVE:
    539 	    bp = &sc->sc_active_filt_in;
    540 	    break;
    541 
    542 	case PPPIOCSOACTIVE:
    543 	    bp = &sc->sc_active_filt_out;
    544 	    break;
    545 	}
    546 	oldcode = bp->bf_insns;
    547 	s = splimp();
    548 	bp->bf_len = nbp->bf_len;
    549 	bp->bf_insns = newcode;
    550 	splx(s);
    551 	if (oldcode != 0)
    552 	    free(oldcode, M_DEVBUF);
    553 	break;
    554 #endif /* PPP_FILTER */
    555 
    556     default:
    557 	return (-1);
    558     }
    559     return (0);
    560 }
    561 
    562 /*
    563  * Process an ioctl request to the ppp network interface.
    564  */
    565 static int
    566 pppsioctl(ifp, cmd, data)
    567     register struct ifnet *ifp;
    568     u_long cmd;
    569     caddr_t data;
    570 {
    571     register struct proc *p = curproc;	/* XXX */
    572     register struct ppp_softc *sc = ifp->if_softc;
    573     register struct ifaddr *ifa = (struct ifaddr *)data;
    574     register struct ifreq *ifr = (struct ifreq *)data;
    575     struct ppp_stats *psp;
    576 #ifdef	PPP_COMPRESS
    577     struct ppp_comp_stats *pcp;
    578 #endif
    579     int s = splimp(), error = 0;
    580 
    581     switch (cmd) {
    582     case SIOCSIFFLAGS:
    583 	if ((ifp->if_flags & IFF_RUNNING) == 0)
    584 	    ifp->if_flags &= ~IFF_UP;
    585 	break;
    586 
    587     case SIOCSIFADDR:
    588 	if (ifa->ifa_addr->sa_family != AF_INET)
    589 	    error = EAFNOSUPPORT;
    590 	break;
    591 
    592     case SIOCSIFDSTADDR:
    593 	if (ifa->ifa_addr->sa_family != AF_INET)
    594 	    error = EAFNOSUPPORT;
    595 	break;
    596 
    597     case SIOCSIFMTU:
    598 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    599 	    break;
    600 	sc->sc_if.if_mtu = ifr->ifr_mtu;
    601 	break;
    602 
    603     case SIOCGIFMTU:
    604 	ifr->ifr_mtu = sc->sc_if.if_mtu;
    605 	break;
    606 
    607     case SIOCADDMULTI:
    608     case SIOCDELMULTI:
    609 	if (ifr == 0) {
    610 	    error = EAFNOSUPPORT;
    611 	    break;
    612 	}
    613 	switch(ifr->ifr_addr.sa_family) {
    614 #ifdef INET
    615 	case AF_INET:
    616 	    break;
    617 #endif
    618 	default:
    619 	    error = EAFNOSUPPORT;
    620 	    break;
    621 	}
    622 	break;
    623 
    624     case SIOCGPPPSTATS:
    625 	psp = &((struct ifpppstatsreq *) data)->stats;
    626 	bzero(psp, sizeof(*psp));
    627 	psp->p = sc->sc_stats;
    628 #if defined(VJC) && !defined(SL_NO_STATS)
    629 	if (sc->sc_comp) {
    630 	    psp->vj.vjs_packets = sc->sc_comp->sls_packets;
    631 	    psp->vj.vjs_compressed = sc->sc_comp->sls_compressed;
    632 	    psp->vj.vjs_searches = sc->sc_comp->sls_searches;
    633 	    psp->vj.vjs_misses = sc->sc_comp->sls_misses;
    634 	    psp->vj.vjs_uncompressedin = sc->sc_comp->sls_uncompressedin;
    635 	    psp->vj.vjs_compressedin = sc->sc_comp->sls_compressedin;
    636 	    psp->vj.vjs_errorin = sc->sc_comp->sls_errorin;
    637 	    psp->vj.vjs_tossed = sc->sc_comp->sls_tossed;
    638 	}
    639 #endif /* VJC */
    640 	break;
    641 
    642 #ifdef PPP_COMPRESS
    643     case SIOCGPPPCSTATS:
    644 	pcp = &((struct ifpppcstatsreq *) data)->stats;
    645 	bzero(pcp, sizeof(*pcp));
    646 	if (sc->sc_xc_state != NULL)
    647 	    (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c);
    648 	if (sc->sc_rc_state != NULL)
    649 	    (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d);
    650 	break;
    651 #endif /* PPP_COMPRESS */
    652 
    653     default:
    654 	error = EINVAL;
    655     }
    656     splx(s);
    657     return (error);
    658 }
    659 
    660 /*
    661  * Queue a packet.  Start transmission if not active.
    662  * Packet is placed in Information field of PPP frame.
    663  */
    664 int
    665 pppoutput(ifp, m0, dst, rtp)
    666     struct ifnet *ifp;
    667     struct mbuf *m0;
    668     struct sockaddr *dst;
    669     struct rtentry *rtp;
    670 {
    671     register struct ppp_softc *sc = ifp->if_softc;
    672     int protocol, address, control;
    673     u_char *cp;
    674     int s, error;
    675     struct ip *ip;
    676     struct ifqueue *ifq;
    677     enum NPmode mode;
    678     int len;
    679     struct mbuf *m;
    680 
    681     if (sc->sc_devp == NULL || (ifp->if_flags & IFF_RUNNING) == 0
    682 	|| ((ifp->if_flags & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC)) {
    683 	error = ENETDOWN;	/* sort of */
    684 	goto bad;
    685     }
    686 
    687     /*
    688      * Compute PPP header.
    689      */
    690     m0->m_flags &= ~M_HIGHPRI;
    691     switch (dst->sa_family) {
    692 #ifdef INET
    693     case AF_INET:
    694 	address = PPP_ALLSTATIONS;
    695 	control = PPP_UI;
    696 	protocol = PPP_IP;
    697 	mode = sc->sc_npmode[NP_IP];
    698 
    699 	/*
    700 	 * If this packet has the "low delay" bit set in the IP header,
    701 	 * put it on the fastq instead.
    702 	 */
    703 	ip = mtod(m0, struct ip *);
    704 	if (ip->ip_tos & IPTOS_LOWDELAY)
    705 	    m0->m_flags |= M_HIGHPRI;
    706 	break;
    707 #endif
    708     case AF_UNSPEC:
    709 	address = PPP_ADDRESS(dst->sa_data);
    710 	control = PPP_CONTROL(dst->sa_data);
    711 	protocol = PPP_PROTOCOL(dst->sa_data);
    712 	mode = NPMODE_PASS;
    713 	break;
    714     default:
    715 	printf("%s: af%d not supported\n", ifp->if_xname, dst->sa_family);
    716 	error = EAFNOSUPPORT;
    717 	goto bad;
    718     }
    719 
    720     /*
    721      * Drop this packet, or return an error, if necessary.
    722      */
    723     if (mode == NPMODE_ERROR) {
    724 	error = ENETDOWN;
    725 	goto bad;
    726     }
    727     if (mode == NPMODE_DROP) {
    728 	error = 0;
    729 	goto bad;
    730     }
    731 
    732     /*
    733      * Add PPP header.  If no space in first mbuf, allocate another.
    734      * (This assumes M_LEADINGSPACE is always 0 for a cluster mbuf.)
    735      */
    736     if (M_LEADINGSPACE(m0) < PPP_HDRLEN) {
    737 	m0 = m_prepend(m0, PPP_HDRLEN, M_DONTWAIT);
    738 	if (m0 == 0) {
    739 	    error = ENOBUFS;
    740 	    goto bad;
    741 	}
    742 	m0->m_len = 0;
    743     } else
    744 	m0->m_data -= PPP_HDRLEN;
    745 
    746     cp = mtod(m0, u_char *);
    747     *cp++ = address;
    748     *cp++ = control;
    749     *cp++ = protocol >> 8;
    750     *cp++ = protocol & 0xff;
    751     m0->m_len += PPP_HDRLEN;
    752 
    753     len = 0;
    754     for (m = m0; m != 0; m = m->m_next)
    755 	len += m->m_len;
    756 
    757     if (sc->sc_flags & SC_LOG_OUTPKT) {
    758 	printf("%s output: ", ifp->if_xname);
    759 	pppdumpm(m0);
    760     }
    761 
    762     if ((protocol & 0x8000) == 0) {
    763 #ifdef PPP_FILTER
    764 	/*
    765 	 * Apply the pass and active filters to the packet,
    766 	 * but only if it is a data packet.
    767 	 */
    768 	if (sc->sc_pass_filt_out.bf_insns != 0
    769 	    && bpf_filter(sc->sc_pass_filt_out.bf_insns, (u_char *) m0,
    770 			  len, 0) == 0) {
    771 	    error = 0;		/* drop this packet */
    772 	    goto bad;
    773 	}
    774 
    775 	/*
    776 	 * Update the time we sent the most recent packet.
    777 	 */
    778 	if (sc->sc_active_filt_out.bf_insns == 0
    779 	    || bpf_filter(sc->sc_active_filt_out.bf_insns, (u_char *) m0,
    780 	    		  len, 0))
    781 	    sc->sc_last_sent = time.tv_sec;
    782 #else
    783 	/*
    784 	 * Update the time we sent the most recent packet.
    785 	 */
    786 	sc->sc_last_sent = time.tv_sec;
    787 #endif /* PPP_FILTER */
    788     }
    789 
    790 #if NBPFILTER > 0
    791     /*
    792      * See if bpf wants to look at the packet.
    793      */
    794     if (sc->sc_bpf)
    795 	bpf_mtap(sc->sc_bpf, m0);
    796 #endif
    797 
    798     /*
    799      * Put the packet on the appropriate queue.
    800      */
    801     s = splimp();
    802     if (mode == NPMODE_QUEUE) {
    803 	/* XXX we should limit the number of packets on this queue */
    804 	*sc->sc_npqtail = m0;
    805 	m0->m_nextpkt = NULL;
    806 	sc->sc_npqtail = &m0->m_nextpkt;
    807     } else {
    808 	ifq = (m0->m_flags & M_HIGHPRI)? &sc->sc_fastq: &ifp->if_snd;
    809 	if (IF_QFULL(ifq) && dst->sa_family != AF_UNSPEC) {
    810 	    IF_DROP(ifq);
    811 	    splx(s);
    812 	    sc->sc_if.if_oerrors++;
    813 	    sc->sc_stats.ppp_oerrors++;
    814 	    error = ENOBUFS;
    815 	    goto bad;
    816 	}
    817 	IF_ENQUEUE(ifq, m0);
    818 	ppp_restart(sc);
    819     }
    820     ifp->if_lastchange = time;
    821     ifp->if_opackets++;
    822     ifp->if_obytes += len;
    823 
    824     splx(s);
    825     return (0);
    826 
    827 bad:
    828     m_freem(m0);
    829     return (error);
    830 }
    831 
    832 /*
    833  * After a change in the NPmode for some NP, move packets from the
    834  * npqueue to the send queue or the fast queue as appropriate.
    835  * Should be called at splimp, since we muck with the queues.
    836  */
    837 static void
    838 ppp_requeue(sc)
    839     struct ppp_softc *sc;
    840 {
    841     struct mbuf *m, **mpp;
    842     struct ifqueue *ifq;
    843     enum NPmode mode;
    844 
    845     for (mpp = &sc->sc_npqueue; (m = *mpp) != NULL; ) {
    846 	switch (PPP_PROTOCOL(mtod(m, u_char *))) {
    847 	case PPP_IP:
    848 	    mode = sc->sc_npmode[NP_IP];
    849 	    break;
    850 	default:
    851 	    mode = NPMODE_PASS;
    852 	}
    853 
    854 	switch (mode) {
    855 	case NPMODE_PASS:
    856 	    /*
    857 	     * This packet can now go on one of the queues to be sent.
    858 	     */
    859 	    *mpp = m->m_nextpkt;
    860 	    m->m_nextpkt = NULL;
    861 	    ifq = (m->m_flags & M_HIGHPRI)? &sc->sc_fastq: &sc->sc_if.if_snd;
    862 	    if (IF_QFULL(ifq)) {
    863 		IF_DROP(ifq);
    864 		sc->sc_if.if_oerrors++;
    865 		sc->sc_stats.ppp_oerrors++;
    866 	    } else
    867 		IF_ENQUEUE(ifq, m);
    868 	    break;
    869 
    870 	case NPMODE_DROP:
    871 	case NPMODE_ERROR:
    872 	    *mpp = m->m_nextpkt;
    873 	    m_freem(m);
    874 	    break;
    875 
    876 	case NPMODE_QUEUE:
    877 	    mpp = &m->m_nextpkt;
    878 	    break;
    879 	}
    880     }
    881     sc->sc_npqtail = mpp;
    882 }
    883 
    884 /*
    885  * Transmitter has finished outputting some stuff;
    886  * remember to call sc->sc_start later at splsoftnet.
    887  */
    888 void
    889 ppp_restart(sc)
    890     struct ppp_softc *sc;
    891 {
    892     int s = splimp();
    893 
    894     sc->sc_flags &= ~SC_TBUSY;
    895     schednetisr(NETISR_PPP);
    896     splx(s);
    897 }
    898 
    899 /*
    900  * Get a packet to send.  This procedure is intended to be called at
    901  * splsoftnet, since it may involve time-consuming operations such as
    902  * applying VJ compression, packet compression, address/control and/or
    903  * protocol field compression to the packet.
    904  */
    905 struct mbuf *
    906 ppp_dequeue(sc)
    907     struct ppp_softc *sc;
    908 {
    909     struct mbuf *m, *mp;
    910     u_char *cp;
    911     int address, control, protocol;
    912     int s;
    913 
    914     /*
    915      * Grab a packet to send: first try the fast queue, then the
    916      * normal queue.
    917      */
    918     s = splimp();
    919     IF_DEQUEUE(&sc->sc_fastq, m);
    920     if (m == NULL)
    921 	IF_DEQUEUE(&sc->sc_if.if_snd, m);
    922     splx(s);
    923 
    924     if (m == NULL)
    925 	return NULL;
    926 
    927     ++sc->sc_stats.ppp_opackets;
    928 
    929     /*
    930      * Extract the ppp header of the new packet.
    931      * The ppp header will be in one mbuf.
    932      */
    933     cp = mtod(m, u_char *);
    934     address = PPP_ADDRESS(cp);
    935     control = PPP_CONTROL(cp);
    936     protocol = PPP_PROTOCOL(cp);
    937 
    938     switch (protocol) {
    939     case PPP_IP:
    940 #ifdef VJC
    941 	/*
    942 	 * If the packet is a TCP/IP packet, see if we can compress it.
    943 	 */
    944 	if ((sc->sc_flags & SC_COMP_TCP) && sc->sc_comp != NULL) {
    945 	    struct ip *ip;
    946 	    int type;
    947 
    948 	    mp = m;
    949 	    ip = (struct ip *) (cp + PPP_HDRLEN);
    950 	    if (mp->m_len <= PPP_HDRLEN) {
    951 		mp = mp->m_next;
    952 		if (mp == NULL)
    953 		    break;
    954 		ip = mtod(mp, struct ip *);
    955 	    }
    956 	    /* this code assumes the IP/TCP header is in one non-shared mbuf */
    957 	    if (ip->ip_p == IPPROTO_TCP) {
    958 		type = sl_compress_tcp(mp, ip, sc->sc_comp,
    959 				       !(sc->sc_flags & SC_NO_TCP_CCID));
    960 		switch (type) {
    961 		case TYPE_UNCOMPRESSED_TCP:
    962 		    protocol = PPP_VJC_UNCOMP;
    963 		    break;
    964 		case TYPE_COMPRESSED_TCP:
    965 		    protocol = PPP_VJC_COMP;
    966 		    cp = mtod(m, u_char *);
    967 		    cp[0] = address;	/* header has moved */
    968 		    cp[1] = control;
    969 		    cp[2] = 0;
    970 		    break;
    971 		}
    972 		cp[3] = protocol;	/* update protocol in PPP header */
    973 	    }
    974 	}
    975 #endif	/* VJC */
    976 	break;
    977 
    978 #ifdef PPP_COMPRESS
    979     case PPP_CCP:
    980 	ppp_ccp(sc, m, 0);
    981 	break;
    982 #endif	/* PPP_COMPRESS */
    983     }
    984 
    985 #ifdef PPP_COMPRESS
    986     if (protocol != PPP_LCP && protocol != PPP_CCP
    987 	&& sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
    988 	struct mbuf *mcomp = NULL;
    989 	int slen, clen;
    990 
    991 	slen = 0;
    992 	for (mp = m; mp != NULL; mp = mp->m_next)
    993 	    slen += mp->m_len;
    994 	clen = (*sc->sc_xcomp->compress)
    995 	    (sc->sc_xc_state, &mcomp, m, slen, sc->sc_if.if_mtu + PPP_HDRLEN);
    996 	if (mcomp != NULL) {
    997 	    if (sc->sc_flags & SC_CCP_UP) {
    998 		/* Send the compressed packet instead of the original. */
    999 		m_freem(m);
   1000 		m = mcomp;
   1001 		cp = mtod(m, u_char *);
   1002 		protocol = cp[3];
   1003 	    } else {
   1004 		/* Can't transmit compressed packets until CCP is up. */
   1005 		m_freem(mcomp);
   1006 	    }
   1007 	}
   1008     }
   1009 #endif	/* PPP_COMPRESS */
   1010 
   1011     /*
   1012      * Compress the address/control and protocol, if possible.
   1013      */
   1014     if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
   1015 	control == PPP_UI && protocol != PPP_ALLSTATIONS &&
   1016 	protocol != PPP_LCP) {
   1017 	/* can compress address/control */
   1018 	m->m_data += 2;
   1019 	m->m_len -= 2;
   1020     }
   1021     if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
   1022 	/* can compress protocol */
   1023 	if (mtod(m, u_char *) == cp) {
   1024 	    cp[2] = cp[1];	/* move address/control up */
   1025 	    cp[1] = cp[0];
   1026 	}
   1027 	++m->m_data;
   1028 	--m->m_len;
   1029     }
   1030 
   1031     return m;
   1032 }
   1033 
   1034 /*
   1035  * Software interrupt routine, called at splsoftnet.
   1036  */
   1037 void
   1038 pppintr()
   1039 {
   1040     struct ppp_softc *sc;
   1041     int i, s, s2;
   1042     struct mbuf *m;
   1043 
   1044     sc = ppp_softc;
   1045     s = splsoftnet();
   1046     for (i = 0; i < NPPP; ++i, ++sc) {
   1047 	if (!(sc->sc_flags & SC_TBUSY)
   1048 	    && (sc->sc_if.if_snd.ifq_head || sc->sc_fastq.ifq_head
   1049 		|| sc->sc_outm)) {
   1050 	    s2 = splimp();
   1051 	    sc->sc_flags |= SC_TBUSY;
   1052 	    splx(s2);
   1053 	    (*sc->sc_start)(sc);
   1054 	}
   1055 	for (;;) {
   1056 	    s2 = splimp();
   1057 	    IF_DEQUEUE(&sc->sc_rawq, m);
   1058 	    splx(s2);
   1059 	    if (m == NULL)
   1060 		break;
   1061 	    ppp_inproc(sc, m);
   1062 	}
   1063     }
   1064     splx(s);
   1065 }
   1066 
   1067 #ifdef PPP_COMPRESS
   1068 /*
   1069  * Handle a CCP packet.  `rcvd' is 1 if the packet was received,
   1070  * 0 if it is about to be transmitted.
   1071  */
   1072 static void
   1073 ppp_ccp(sc, m, rcvd)
   1074     struct ppp_softc *sc;
   1075     struct mbuf *m;
   1076     int rcvd;
   1077 {
   1078     u_char *dp, *ep;
   1079     struct mbuf *mp;
   1080     int slen, s;
   1081 
   1082     /*
   1083      * Get a pointer to the data after the PPP header.
   1084      */
   1085     if (m->m_len <= PPP_HDRLEN) {
   1086 	mp = m->m_next;
   1087 	if (mp == NULL)
   1088 	    return;
   1089 	dp = (mp != NULL)? mtod(mp, u_char *): NULL;
   1090     } else {
   1091 	mp = m;
   1092 	dp = mtod(mp, u_char *) + PPP_HDRLEN;
   1093     }
   1094 
   1095     ep = mtod(mp, u_char *) + mp->m_len;
   1096     if (dp + CCP_HDRLEN > ep)
   1097 	return;
   1098     slen = CCP_LENGTH(dp);
   1099     if (dp + slen > ep) {
   1100 	if (sc->sc_flags & SC_DEBUG)
   1101 	    printf("if_ppp/ccp: not enough data in mbuf (%p+%x > %p+%x)\n",
   1102 		dp, slen, mtod(mp, u_char *), mp->m_len);
   1103 	return;
   1104     }
   1105 
   1106     switch (CCP_CODE(dp)) {
   1107     case CCP_CONFREQ:
   1108     case CCP_TERMREQ:
   1109     case CCP_TERMACK:
   1110 	/* CCP must be going down - disable compression */
   1111 	if (sc->sc_flags & SC_CCP_UP) {
   1112 	    s = splimp();
   1113 	    sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
   1114 	    splx(s);
   1115 	}
   1116 	break;
   1117 
   1118     case CCP_CONFACK:
   1119 	if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
   1120 	    && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
   1121 	    && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
   1122 	    if (!rcvd) {
   1123 		/* we're agreeing to send compressed packets. */
   1124 		if (sc->sc_xc_state != NULL
   1125 		    && (*sc->sc_xcomp->comp_init)
   1126 			(sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
   1127 			 sc->sc_unit, 0, sc->sc_flags & SC_DEBUG)) {
   1128 		    s = splimp();
   1129 		    sc->sc_flags |= SC_COMP_RUN;
   1130 		    splx(s);
   1131 		}
   1132 	    } else {
   1133 		/* peer is agreeing to send compressed packets. */
   1134 		if (sc->sc_rc_state != NULL
   1135 		    && (*sc->sc_rcomp->decomp_init)
   1136 			(sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
   1137 			 sc->sc_unit, 0, sc->sc_mru,
   1138 			 sc->sc_flags & SC_DEBUG)) {
   1139 		    s = splimp();
   1140 		    sc->sc_flags |= SC_DECOMP_RUN;
   1141 		    sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
   1142 		    splx(s);
   1143 		}
   1144 	    }
   1145 	}
   1146 	break;
   1147 
   1148     case CCP_RESETACK:
   1149 	if (sc->sc_flags & SC_CCP_UP) {
   1150 	    if (!rcvd) {
   1151 		if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN))
   1152 		    (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
   1153 	    } else {
   1154 		if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
   1155 		    (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
   1156 		    s = splimp();
   1157 		    sc->sc_flags &= ~SC_DC_ERROR;
   1158 		    splx(s);
   1159 		}
   1160 	    }
   1161 	}
   1162 	break;
   1163     }
   1164 }
   1165 
   1166 /*
   1167  * CCP is down; free (de)compressor state if necessary.
   1168  */
   1169 static void
   1170 ppp_ccp_closed(sc)
   1171     struct ppp_softc *sc;
   1172 {
   1173     if (sc->sc_xc_state) {
   1174 	(*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
   1175 	sc->sc_xc_state = NULL;
   1176     }
   1177     if (sc->sc_rc_state) {
   1178 	(*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
   1179 	sc->sc_rc_state = NULL;
   1180     }
   1181 }
   1182 #endif /* PPP_COMPRESS */
   1183 
   1184 /*
   1185  * PPP packet input routine.
   1186  * The caller has checked and removed the FCS and has inserted
   1187  * the address/control bytes and the protocol high byte if they
   1188  * were omitted.
   1189  */
   1190 void
   1191 ppppktin(sc, m, lost)
   1192     struct ppp_softc *sc;
   1193     struct mbuf *m;
   1194     int lost;
   1195 {
   1196     int s = splimp();
   1197 
   1198     if (lost)
   1199 	m->m_flags |= M_ERRMARK;
   1200     IF_ENQUEUE(&sc->sc_rawq, m);
   1201     schednetisr(NETISR_PPP);
   1202     splx(s);
   1203 }
   1204 
   1205 /*
   1206  * Process a received PPP packet, doing decompression as necessary.
   1207  * Should be called at splsoftnet.
   1208  */
   1209 #define COMPTYPE(proto)	((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
   1210 			 TYPE_UNCOMPRESSED_TCP)
   1211 
   1212 static void
   1213 ppp_inproc(sc, m)
   1214     struct ppp_softc *sc;
   1215     struct mbuf *m;
   1216 {
   1217     struct ifnet *ifp = &sc->sc_if;
   1218     struct ifqueue *inq;
   1219     int s, ilen, xlen, proto, rv;
   1220     u_char *cp, adrs, ctrl;
   1221     struct mbuf *mp, *dmp = NULL;
   1222     u_char *iphdr;
   1223     u_int hlen;
   1224 
   1225     sc->sc_stats.ppp_ipackets++;
   1226 
   1227     if (sc->sc_flags & SC_LOG_INPKT) {
   1228 	ilen = 0;
   1229 	for (mp = m; mp != NULL; mp = mp->m_next)
   1230 	    ilen += mp->m_len;
   1231 	printf("%s: got %d bytes\n", ifp->if_xname, ilen);
   1232 	pppdumpm(m);
   1233     }
   1234 
   1235     cp = mtod(m, u_char *);
   1236     adrs = PPP_ADDRESS(cp);
   1237     ctrl = PPP_CONTROL(cp);
   1238     proto = PPP_PROTOCOL(cp);
   1239 
   1240     if (m->m_flags & M_ERRMARK) {
   1241 	m->m_flags &= ~M_ERRMARK;
   1242 	s = splimp();
   1243 	sc->sc_flags |= SC_VJ_RESET;
   1244 	splx(s);
   1245     }
   1246 
   1247 #ifdef PPP_COMPRESS
   1248     /*
   1249      * Decompress this packet if necessary, update the receiver's
   1250      * dictionary, or take appropriate action on a CCP packet.
   1251      */
   1252     if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
   1253 	&& !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
   1254 	/* decompress this packet */
   1255 	rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
   1256 	if (rv == DECOMP_OK) {
   1257 	    m_freem(m);
   1258 	    if (dmp == NULL) {
   1259 		/* no error, but no decompressed packet produced */
   1260 		return;
   1261 	    }
   1262 	    m = dmp;
   1263 	    cp = mtod(m, u_char *);
   1264 	    proto = PPP_PROTOCOL(cp);
   1265 
   1266 	} else {
   1267 	    /*
   1268 	     * An error has occurred in decompression.
   1269 	     * Pass the compressed packet up to pppd, which may take
   1270 	     * CCP down or issue a Reset-Req.
   1271 	     */
   1272 	    if (sc->sc_flags & SC_DEBUG)
   1273 		printf("%s: decompress failed %d\n", ifp->if_xname, rv);
   1274 	    s = splimp();
   1275 	    sc->sc_flags |= SC_VJ_RESET;
   1276 	    if (rv == DECOMP_ERROR)
   1277 		sc->sc_flags |= SC_DC_ERROR;
   1278 	    else
   1279 		sc->sc_flags |= SC_DC_FERROR;
   1280 	    splx(s);
   1281 	}
   1282 
   1283     } else {
   1284 	if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
   1285 	    (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
   1286 	}
   1287 	if (proto == PPP_CCP) {
   1288 	    ppp_ccp(sc, m, 1);
   1289 	}
   1290     }
   1291 #endif
   1292 
   1293     ilen = 0;
   1294     for (mp = m; mp != NULL; mp = mp->m_next)
   1295 	ilen += mp->m_len;
   1296 
   1297 #ifdef VJC
   1298     if (sc->sc_flags & SC_VJ_RESET) {
   1299 	/*
   1300 	 * If we've missed a packet, we must toss subsequent compressed
   1301 	 * packets which don't have an explicit connection ID.
   1302 	 */
   1303 	if (sc->sc_comp)
   1304 	    sl_uncompress_tcp(NULL, 0, TYPE_ERROR, sc->sc_comp);
   1305 	s = splimp();
   1306 	sc->sc_flags &= ~SC_VJ_RESET;
   1307 	splx(s);
   1308     }
   1309 
   1310     /*
   1311      * See if we have a VJ-compressed packet to uncompress.
   1312      */
   1313     if (proto == PPP_VJC_COMP) {
   1314 	if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
   1315 	    goto bad;
   1316 
   1317 	xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
   1318 				      ilen - PPP_HDRLEN, TYPE_COMPRESSED_TCP,
   1319 				      sc->sc_comp, &iphdr, &hlen);
   1320 
   1321 	if (xlen <= 0) {
   1322 	    if (sc->sc_flags & SC_DEBUG)
   1323 		printf("%s: VJ uncompress failed on type comp\n",
   1324 		    ifp->if_xname);
   1325 	    goto bad;
   1326 	}
   1327 
   1328 	/* Copy the PPP and IP headers into a new mbuf. */
   1329 	MGETHDR(mp, M_DONTWAIT, MT_DATA);
   1330 	if (mp == NULL)
   1331 	    goto bad;
   1332 	mp->m_len = 0;
   1333 	mp->m_next = NULL;
   1334 	if (hlen + PPP_HDRLEN > MHLEN) {
   1335 	    MCLGET(mp, M_DONTWAIT);
   1336 	    if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) {
   1337 		m_freem(mp);
   1338 		goto bad;	/* lose if big headers and no clusters */
   1339 	    }
   1340 	}
   1341 	cp = mtod(mp, u_char *);
   1342 	cp[0] = adrs;
   1343 	cp[1] = ctrl;
   1344 	cp[2] = 0;
   1345 	cp[3] = PPP_IP;
   1346 	proto = PPP_IP;
   1347 	bcopy(iphdr, cp + PPP_HDRLEN, hlen);
   1348 	mp->m_len = hlen + PPP_HDRLEN;
   1349 
   1350 	/*
   1351 	 * Trim the PPP and VJ headers off the old mbuf
   1352 	 * and stick the new and old mbufs together.
   1353 	 */
   1354 	m->m_data += PPP_HDRLEN + xlen;
   1355 	m->m_len -= PPP_HDRLEN + xlen;
   1356 	if (m->m_len <= M_TRAILINGSPACE(mp)) {
   1357 	    bcopy(mtod(m, u_char *), mtod(mp, u_char *) + mp->m_len, m->m_len);
   1358 	    mp->m_len += m->m_len;
   1359 	    MFREE(m, mp->m_next);
   1360 	} else
   1361 	    mp->m_next = m;
   1362 	m = mp;
   1363 	ilen += hlen - xlen;
   1364 
   1365     } else if (proto == PPP_VJC_UNCOMP) {
   1366 	if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
   1367 	    goto bad;
   1368 
   1369 	xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
   1370 				      ilen - PPP_HDRLEN, TYPE_UNCOMPRESSED_TCP,
   1371 				      sc->sc_comp, &iphdr, &hlen);
   1372 
   1373 	if (xlen < 0) {
   1374 	    if (sc->sc_flags & SC_DEBUG)
   1375 		printf("%s: VJ uncompress failed on type uncomp\n",
   1376 		    ifp->if_xname);
   1377 	    goto bad;
   1378 	}
   1379 
   1380 	proto = PPP_IP;
   1381 	cp[3] = PPP_IP;
   1382     }
   1383 #endif /* VJC */
   1384 
   1385     /*
   1386      * If the packet will fit in a header mbuf, don't waste a
   1387      * whole cluster on it.
   1388      */
   1389     if (ilen <= MHLEN && M_IS_CLUSTER(m)) {
   1390 	MGETHDR(mp, M_DONTWAIT, MT_DATA);
   1391 	if (mp != NULL) {
   1392 	    m_copydata(m, 0, ilen, mtod(mp, caddr_t));
   1393 	    m_freem(m);
   1394 	    m = mp;
   1395 	    m->m_len = ilen;
   1396 	}
   1397     }
   1398     m->m_pkthdr.len = ilen;
   1399     m->m_pkthdr.rcvif = ifp;
   1400 
   1401     if ((proto & 0x8000) == 0) {
   1402 #ifdef PPP_FILTER
   1403 	/*
   1404 	 * See whether we want to pass this packet, and
   1405 	 * if it counts as link activity.
   1406 	 */
   1407 	if (sc->sc_pass_filt_in.bf_insns != 0
   1408 	    && bpf_filter(sc->sc_pass_filt_in.bf_insns, (u_char *) m,
   1409 			  ilen, 0) == 0) {
   1410 	    /* drop this packet */
   1411 	    m_freem(m);
   1412 	    return;
   1413 	}
   1414 	if (sc->sc_active_filt_in.bf_insns == 0
   1415 	    || bpf_filter(sc->sc_active_filt_in.bf_insns, (u_char *) m,
   1416 	    		  ilen, 0))
   1417 	    sc->sc_last_recv = time.tv_sec;
   1418 #else
   1419 	/*
   1420 	 * Record the time that we received this packet.
   1421 	 */
   1422 	sc->sc_last_recv = time.tv_sec;
   1423 #endif /* PPP_FILTER */
   1424     }
   1425 
   1426 #if NBPFILTER > 0
   1427     /* See if bpf wants to look at the packet. */
   1428     if (sc->sc_bpf)
   1429 	bpf_mtap(sc->sc_bpf, m);
   1430 #endif
   1431 
   1432     rv = 0;
   1433     switch (proto) {
   1434 #ifdef INET
   1435     case PPP_IP:
   1436 	/*
   1437 	 * IP packet - take off the ppp header and pass it up to IP.
   1438 	 */
   1439 	if ((ifp->if_flags & IFF_UP) == 0
   1440 	    || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
   1441 	    /* interface is down - drop the packet. */
   1442 	    m_freem(m);
   1443 	    return;
   1444 	}
   1445 	m->m_pkthdr.len -= PPP_HDRLEN;
   1446 	m->m_data += PPP_HDRLEN;
   1447 	m->m_len -= PPP_HDRLEN;
   1448 #ifdef GATEWAY
   1449 	if (ipflow_fastforward(m))
   1450 		return;
   1451 #endif
   1452 	schednetisr(NETISR_IP);
   1453 	inq = &ipintrq;
   1454 	break;
   1455 #endif
   1456 
   1457     default:
   1458 	/*
   1459 	 * Some other protocol - place on input queue for read().
   1460 	 */
   1461 	inq = &sc->sc_inq;
   1462 	rv = 1;
   1463 	break;
   1464     }
   1465 
   1466     /*
   1467      * Put the packet on the appropriate input queue.
   1468      */
   1469     s = splimp();
   1470     if (IF_QFULL(inq)) {
   1471 	IF_DROP(inq);
   1472 	splx(s);
   1473 	if (sc->sc_flags & SC_DEBUG)
   1474 	    printf("%s: input queue full\n", ifp->if_xname);
   1475 	ifp->if_iqdrops++;
   1476 	goto bad;
   1477     }
   1478     IF_ENQUEUE(inq, m);
   1479     splx(s);
   1480     ifp->if_ipackets++;
   1481     ifp->if_ibytes += ilen;
   1482     ifp->if_lastchange = time;
   1483 
   1484     if (rv)
   1485 	(*sc->sc_ctlp)(sc);
   1486 
   1487     return;
   1488 
   1489  bad:
   1490     m_freem(m);
   1491     sc->sc_if.if_ierrors++;
   1492     sc->sc_stats.ppp_ierrors++;
   1493 }
   1494 
   1495 #define MAX_DUMP_BYTES	128
   1496 
   1497 static void
   1498 pppdumpm(m0)
   1499     struct mbuf *m0;
   1500 {
   1501     char buf[3*MAX_DUMP_BYTES+4];
   1502     char *bp = buf;
   1503     struct mbuf *m;
   1504     static char digits[] = "0123456789abcdef";
   1505 
   1506     for (m = m0; m; m = m->m_next) {
   1507 	int l = m->m_len;
   1508 	u_char *rptr = (u_char *)m->m_data;
   1509 
   1510 	while (l--) {
   1511 	    if (bp > buf + sizeof(buf) - 4)
   1512 		goto done;
   1513 	    *bp++ = digits[*rptr >> 4]; /* convert byte to ascii hex */
   1514 	    *bp++ = digits[*rptr++ & 0xf];
   1515 	}
   1516 
   1517 	if (m->m_next) {
   1518 	    if (bp > buf + sizeof(buf) - 3)
   1519 		goto done;
   1520 	    *bp++ = '|';
   1521 	} else
   1522 	    *bp++ = ' ';
   1523     }
   1524 done:
   1525     if (m)
   1526 	*bp++ = '>';
   1527     *bp = 0;
   1528     printf("%s\n", buf);
   1529 }
   1530 
   1531 #endif	/* NPPP > 0 */
   1532