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