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