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