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