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