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