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