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