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