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