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