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