Home | History | Annotate | Line # | Download | only in net
if_ppp.c revision 1.120
      1 /*	$NetBSD: if_ppp.c,v 1.120 2008/01/04 21:18:15 ad Exp $	*/
      2 /*	Id: if_ppp.c,v 1.6 1997/03/04 03:33:00 paulus Exp 	*/
      3 
      4 /*
      5  * if_ppp.c - Point-to-Point Protocol (PPP) Asynchronous driver.
      6  *
      7  * Copyright (c) 1984-2000 Carnegie Mellon University. All rights reserved.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  *
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  *
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in
     18  *    the documentation and/or other materials provided with the
     19  *    distribution.
     20  *
     21  * 3. The name "Carnegie Mellon University" must not be used to
     22  *    endorse or promote products derived from this software without
     23  *    prior written permission. For permission or any legal
     24  *    details, please contact
     25  *      Office of Technology Transfer
     26  *      Carnegie Mellon University
     27  *      5000 Forbes Avenue
     28  *      Pittsburgh, PA  15213-3890
     29  *      (412) 268-4387, fax: (412) 268-7395
     30  *      tech-transfer (at) andrew.cmu.edu
     31  *
     32  * 4. Redistributions of any form whatsoever must retain the following
     33  *    acknowledgment:
     34  *    "This product includes software developed by Computing Services
     35  *     at Carnegie Mellon University (http://www.cmu.edu/computing/)."
     36  *
     37  * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
     38  * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
     39  * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
     40  * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     41  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
     42  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
     43  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     44  *
     45  * Based on:
     46  *	@(#)if_sl.c	7.6.1.2 (Berkeley) 2/15/89
     47  *
     48  * Copyright (c) 1987 Regents of the University of California.
     49  * All rights reserved.
     50  *
     51  * Redistribution and use in source and binary forms are permitted
     52  * provided that the above copyright notice and this paragraph are
     53  * duplicated in all such forms and that any documentation,
     54  * advertising materials, and other materials related to such
     55  * distribution and use acknowledge that the software was developed
     56  * by the University of California, Berkeley.  The name of the
     57  * University may not be used to endorse or promote products derived
     58  * from this software without specific prior written permission.
     59  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
     60  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
     61  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
     62  *
     63  * Serial Line interface
     64  *
     65  * Rick Adams
     66  * Center for Seismic Studies
     67  * 1300 N 17th Street, Suite 1450
     68  * Arlington, Virginia 22209
     69  * (703)276-7900
     70  * rick (at) seismo.ARPA
     71  * seismo!rick
     72  *
     73  * Pounded on heavily by Chris Torek (chris (at) mimsy.umd.edu, umcp-cs!chris).
     74  * Converted to 4.3BSD Beta by Chris Torek.
     75  * Other changes made at Berkeley, based in part on code by Kirk Smith.
     76  *
     77  * Converted to 4.3BSD+ 386BSD by Brad Parker (brad (at) cayman.com)
     78  * Added VJ tcp header compression; more unified ioctls
     79  *
     80  * Extensively modified by Paul Mackerras (paulus (at) cs.anu.edu.au).
     81  * Cleaned up a lot of the mbuf-related code to fix bugs that
     82  * caused system crashes and packet corruption.  Changed pppstart
     83  * so that it doesn't just give up with a collision if the whole
     84  * packet doesn't fit in the output ring buffer.
     85  *
     86  * Added priority queueing for interactive IP packets, following
     87  * the model of if_sl.c, plus hooks for bpf.
     88  * Paul Mackerras (paulus (at) cs.anu.edu.au).
     89  */
     90 
     91 /* from if_sl.c,v 1.11 84/10/04 12:54:47 rick Exp */
     92 /* from NetBSD: if_ppp.c,v 1.15.2.2 1994/07/28 05:17:58 cgd Exp */
     93 
     94 /*
     95  * XXX IMP ME HARDER
     96  *
     97  * This is an explanation of that comment.  This code used to use
     98  * splimp() to block both network and tty interrupts.  However,
     99  * that call is deprecated.  So, we have replaced the uses of
    100  * splimp() with splhigh() in order to applomplish what it needs
    101  * to accomplish, and added that happy little comment.
    102  */
    103 
    104 #include <sys/cdefs.h>
    105 __KERNEL_RCSID(0, "$NetBSD: if_ppp.c,v 1.120 2008/01/04 21:18:15 ad Exp $");
    106 
    107 #include "ppp.h"
    108 
    109 #include "opt_inet.h"
    110 #include "opt_gateway.h"
    111 #include "opt_ppp.h"
    112 
    113 #ifdef INET
    114 #define VJC
    115 #endif
    116 #define PPP_COMPRESS
    117 
    118 #include <sys/param.h>
    119 #include <sys/proc.h>
    120 #include <sys/mbuf.h>
    121 #include <sys/socket.h>
    122 #include <sys/ioctl.h>
    123 #include <sys/kernel.h>
    124 #include <sys/systm.h>
    125 #include <sys/time.h>
    126 #include <sys/malloc.h>
    127 #include <sys/conf.h>
    128 #include <sys/kauth.h>
    129 #include <sys/intr.h>
    130 #include <sys/simplelock.h>
    131 
    132 #include <net/if.h>
    133 #include <net/if_types.h>
    134 #include <net/netisr.h>
    135 #include <net/route.h>
    136 #ifdef PPP_FILTER
    137 #include <net/bpf.h>
    138 #endif
    139 
    140 #include <netinet/in.h>
    141 #include <netinet/in_systm.h>
    142 #include <netinet/in_var.h>
    143 #ifdef INET
    144 #include <netinet/ip.h>
    145 #endif
    146 
    147 #include "bpfilter.h"
    148 #if NBPFILTER > 0
    149 #include <net/bpf.h>
    150 #endif
    151 
    152 #if defined(PPP_FILTER) || NBPFILTER > 0
    153 #include <net/slip.h>
    154 #endif
    155 
    156 #ifdef VJC
    157 #include <net/slcompress.h>
    158 #endif
    159 
    160 #include <net/ppp_defs.h>
    161 #include <net/if_ppp.h>
    162 #include <net/if_pppvar.h>
    163 #include <sys/cpu.h>
    164 
    165 #ifdef PPP_COMPRESS
    166 #define PACKETPTR	struct mbuf *
    167 #include <net/ppp-comp.h>
    168 #endif
    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 
    210 struct if_clone ppp_cloner =
    211     IF_CLONE_INITIALIZER("ppp", ppp_clone_create, ppp_clone_destroy);
    212 
    213 static struct simplelock ppp_list_mutex = SIMPLELOCK_INITIALIZER;
    214 
    215 #ifdef PPP_COMPRESS
    216 /*
    217  * List of compressors we know about.
    218  * We leave some space so maybe we can modload compressors.
    219  */
    220 
    221 extern struct compressor ppp_bsd_compress;
    222 extern struct compressor ppp_deflate, ppp_deflate_draft;
    223 
    224 struct compressor *ppp_compressors[PPP_COMPRESSORS_MAX] = {
    225 #if DO_BSD_COMPRESS && defined(PPP_BSDCOMP)
    226     &ppp_bsd_compress,
    227 #endif
    228 #if DO_DEFLATE && defined(PPP_DEFLATE)
    229     &ppp_deflate,
    230     &ppp_deflate_draft,
    231 #endif
    232     NULL
    233 };
    234 #endif /* PPP_COMPRESS */
    235 
    236 
    237 /*
    238  * Called from boot code to establish ppp interfaces.
    239  */
    240 void
    241 pppattach(void)
    242 {
    243     extern struct linesw ppp_disc;
    244 
    245     if (ttyldisc_attach(&ppp_disc) != 0)
    246     	panic("pppattach");
    247     LIST_INIT(&ppp_softc_list);
    248     if_clone_attach(&ppp_cloner);
    249 }
    250 
    251 static struct ppp_softc *
    252 ppp_create(const char *name, int unit)
    253 {
    254     struct ppp_softc *sc, *sci, *scl = NULL;
    255 
    256     MALLOC(sc, struct ppp_softc *, sizeof(*sc), M_DEVBUF, M_WAIT|M_ZERO);
    257 
    258     simple_lock(&ppp_list_mutex);
    259     if (unit == -1) {
    260 	int i = 0;
    261 	LIST_FOREACH(sci, &ppp_softc_list, sc_iflist) {
    262 	    scl = sci;
    263 	    if (i < sci->sc_unit) {
    264 		unit = i;
    265 		break;
    266 	    } else {
    267 #ifdef DIAGNOSTIC
    268 		KASSERT(i == sci->sc_unit);
    269 #endif
    270 		i++;
    271 	    }
    272 	}
    273 	if (unit == -1)
    274 	    unit = i;
    275     } else {
    276 	LIST_FOREACH(sci, &ppp_softc_list, sc_iflist) {
    277 	    scl = sci;
    278 	    if (unit < sci->sc_unit)
    279 		break;
    280 	    else if (unit == sci->sc_unit) {
    281 		FREE(sc, M_DEVBUF);
    282 		return NULL;
    283 	    }
    284 	}
    285     }
    286 
    287     if (sci != NULL)
    288 	LIST_INSERT_BEFORE(sci, sc, sc_iflist);
    289     else if (scl != NULL)
    290 	LIST_INSERT_AFTER(scl, sc, sc_iflist);
    291     else
    292 	LIST_INSERT_HEAD(&ppp_softc_list, sc, sc_iflist);
    293 
    294     simple_unlock(&ppp_list_mutex);
    295 
    296     (void)snprintf(sc->sc_if.if_xname, sizeof(sc->sc_if.if_xname), "%s%d",
    297 	name, sc->sc_unit = unit);
    298     callout_init(&sc->sc_timo_ch, 0);
    299     sc->sc_if.if_softc = sc;
    300     sc->sc_if.if_mtu = PPP_MTU;
    301     sc->sc_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
    302     sc->sc_if.if_type = IFT_PPP;
    303     sc->sc_if.if_hdrlen = PPP_HDRLEN;
    304     sc->sc_if.if_dlt = DLT_NULL;
    305     sc->sc_if.if_ioctl = pppsioctl;
    306     sc->sc_if.if_output = pppoutput;
    307 #ifdef ALTQ
    308     sc->sc_if.if_start = ppp_ifstart;
    309 #endif
    310     IFQ_SET_MAXLEN(&sc->sc_if.if_snd, IFQ_MAXLEN);
    311     sc->sc_inq.ifq_maxlen = IFQ_MAXLEN;
    312     sc->sc_fastq.ifq_maxlen = IFQ_MAXLEN;
    313     sc->sc_rawq.ifq_maxlen = IFQ_MAXLEN;
    314     /* Ratio of 1:2 packets between the regular and the fast queue */
    315     sc->sc_maxfastq = 2;
    316     IFQ_SET_READY(&sc->sc_if.if_snd);
    317     if_attach(&sc->sc_if);
    318     if_alloc_sadl(&sc->sc_if);
    319 #if NBPFILTER > 0
    320     bpfattach(&sc->sc_if, DLT_NULL, 0);
    321 #endif
    322     return sc;
    323 }
    324 
    325 static int
    326 ppp_clone_create(struct if_clone *ifc, int unit)
    327 {
    328     return ppp_create(ifc->ifc_name, unit) == NULL ? EEXIST : 0;
    329 }
    330 
    331 static int
    332 ppp_clone_destroy(struct ifnet *ifp)
    333 {
    334     struct ppp_softc *sc = (struct ppp_softc *)ifp->if_softc;
    335 
    336     if (sc->sc_devp != NULL)
    337 	return EBUSY; /* Not removing it */
    338 
    339     simple_lock(&ppp_list_mutex);
    340     LIST_REMOVE(sc, sc_iflist);
    341     simple_unlock(&ppp_list_mutex);
    342 
    343 #if NBPFILTER > 0
    344     bpfdetach(ifp);
    345 #endif
    346     if_detach(ifp);
    347 
    348     FREE(sc, M_DEVBUF);
    349     return 0;
    350 }
    351 
    352 /*
    353  * Allocate a ppp interface unit and initialize it.
    354  */
    355 struct ppp_softc *
    356 pppalloc(pid_t pid)
    357 {
    358     struct ppp_softc *sc = NULL, *scf;
    359     int i;
    360 
    361     simple_lock(&ppp_list_mutex);
    362     for (scf = LIST_FIRST(&ppp_softc_list); scf != NULL;
    363 	scf = LIST_NEXT(scf, sc_iflist)) {
    364 	if (scf->sc_xfer == pid) {
    365 	    scf->sc_xfer = 0;
    366 	    simple_unlock(&ppp_list_mutex);
    367 	    return scf;
    368 	}
    369 	if (scf->sc_devp == NULL && sc == NULL)
    370 	    sc = scf;
    371     }
    372     simple_unlock(&ppp_list_mutex);
    373 
    374     if (sc == NULL)
    375 	sc = ppp_create(ppp_cloner.ifc_name, -1);
    376 
    377     sc->sc_si = softint_establish(SOFTINT_NET, pppintr, sc);
    378     if (sc->sc_si == NULL) {
    379 	printf("%s: unable to establish softintr\n", sc->sc_if.if_xname);
    380 	return (NULL);
    381     }
    382     sc->sc_flags = 0;
    383     sc->sc_mru = PPP_MRU;
    384     sc->sc_relinq = NULL;
    385     (void)memset(&sc->sc_stats, 0, sizeof(sc->sc_stats));
    386 #ifdef VJC
    387     MALLOC(sc->sc_comp, struct slcompress *, sizeof(struct slcompress),
    388 	   M_DEVBUF, M_NOWAIT);
    389     if (sc->sc_comp)
    390 	sl_compress_init(sc->sc_comp);
    391 #endif
    392 #ifdef PPP_COMPRESS
    393     sc->sc_xc_state = NULL;
    394     sc->sc_rc_state = NULL;
    395 #endif /* PPP_COMPRESS */
    396     for (i = 0; i < NUM_NP; ++i)
    397 	sc->sc_npmode[i] = NPMODE_ERROR;
    398     sc->sc_npqueue = NULL;
    399     sc->sc_npqtail = &sc->sc_npqueue;
    400     sc->sc_last_sent = sc->sc_last_recv = time_second;
    401 
    402     return sc;
    403 }
    404 
    405 /*
    406  * Deallocate a ppp unit.  Must be called at splsoftnet or higher.
    407  */
    408 void
    409 pppdealloc(struct ppp_softc *sc)
    410 {
    411     struct mbuf *m;
    412 
    413     softint_disestablish(sc->sc_si);
    414     if_down(&sc->sc_if);
    415     sc->sc_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
    416     sc->sc_devp = NULL;
    417     sc->sc_xfer = 0;
    418     for (;;) {
    419 	IF_DEQUEUE(&sc->sc_rawq, m);
    420 	if (m == NULL)
    421 	    break;
    422 	m_freem(m);
    423     }
    424     for (;;) {
    425 	IF_DEQUEUE(&sc->sc_inq, m);
    426 	if (m == NULL)
    427 	    break;
    428 	m_freem(m);
    429     }
    430     for (;;) {
    431 	IF_DEQUEUE(&sc->sc_fastq, m);
    432 	if (m == NULL)
    433 	    break;
    434 	m_freem(m);
    435     }
    436     while ((m = sc->sc_npqueue) != NULL) {
    437 	sc->sc_npqueue = m->m_nextpkt;
    438 	m_freem(m);
    439     }
    440     if (sc->sc_togo != NULL) {
    441 	m_freem(sc->sc_togo);
    442 	sc->sc_togo = NULL;
    443     }
    444 #ifdef PPP_COMPRESS
    445     ppp_ccp_closed(sc);
    446     sc->sc_xc_state = NULL;
    447     sc->sc_rc_state = NULL;
    448 #endif /* PPP_COMPRESS */
    449 #ifdef PPP_FILTER
    450     if (sc->sc_pass_filt_in.bf_insns != 0) {
    451 	FREE(sc->sc_pass_filt_in.bf_insns, M_DEVBUF);
    452 	sc->sc_pass_filt_in.bf_insns = 0;
    453 	sc->sc_pass_filt_in.bf_len = 0;
    454     }
    455     if (sc->sc_pass_filt_out.bf_insns != 0) {
    456 	FREE(sc->sc_pass_filt_out.bf_insns, M_DEVBUF);
    457 	sc->sc_pass_filt_out.bf_insns = 0;
    458 	sc->sc_pass_filt_out.bf_len = 0;
    459     }
    460     if (sc->sc_active_filt_in.bf_insns != 0) {
    461 	FREE(sc->sc_active_filt_in.bf_insns, M_DEVBUF);
    462 	sc->sc_active_filt_in.bf_insns = 0;
    463 	sc->sc_active_filt_in.bf_len = 0;
    464     }
    465     if (sc->sc_active_filt_out.bf_insns != 0) {
    466 	FREE(sc->sc_active_filt_out.bf_insns, M_DEVBUF);
    467 	sc->sc_active_filt_out.bf_insns = 0;
    468 	sc->sc_active_filt_out.bf_len = 0;
    469     }
    470 #endif /* PPP_FILTER */
    471 #ifdef VJC
    472     if (sc->sc_comp != 0) {
    473 	FREE(sc->sc_comp, M_DEVBUF);
    474 	sc->sc_comp = 0;
    475     }
    476 #endif
    477     (void)ppp_clone_destroy(&sc->sc_if);
    478 }
    479 
    480 /*
    481  * Ioctl routine for generic ppp devices.
    482  */
    483 int
    484 pppioctl(struct ppp_softc *sc, u_long cmd, void *data, int flag,
    485     struct lwp *l)
    486 {
    487     int s, error, flags, mru, npx;
    488     u_int nb;
    489     struct ppp_option_data *odp;
    490     struct compressor **cp;
    491     struct npioctl *npi;
    492     time_t t;
    493 #ifdef PPP_FILTER
    494     struct bpf_program *bp, *nbp;
    495     struct bpf_insn *newcode, *oldcode;
    496     int newcodelen;
    497 #endif /* PPP_FILTER */
    498 #ifdef	PPP_COMPRESS
    499     u_char ccp_option[CCP_MAX_OPTION_LENGTH];
    500 #endif
    501 
    502     switch (cmd) {
    503     case PPPIOCSFLAGS:
    504     case PPPIOCSMRU:
    505     case PPPIOCSMAXCID:
    506     case PPPIOCSCOMPRESS:
    507     case PPPIOCSNPMODE:
    508 	if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_INTERFACE,
    509 	    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, &sc->sc_if, (void *)cmd,
    510 	    NULL) != 0)
    511 		return (EPERM);
    512 	break;
    513     case PPPIOCXFERUNIT:
    514 	/* XXX: Why is this privileged?! */
    515 	if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_INTERFACE,
    516 	    KAUTH_REQ_NETWORK_INTERFACE_GETPRIV, &sc->sc_if, (void *)cmd,
    517 	    NULL) != 0)
    518 		return (EPERM);
    519 	break;
    520     default:
    521 	break;
    522     }
    523 
    524     switch (cmd) {
    525     case FIONREAD:
    526 	*(int *)data = sc->sc_inq.ifq_len;
    527 	break;
    528 
    529     case PPPIOCGUNIT:
    530 	*(int *)data = sc->sc_unit;
    531 	break;
    532 
    533     case PPPIOCGFLAGS:
    534 	*(u_int *)data = sc->sc_flags;
    535 	break;
    536 
    537     case PPPIOCGRAWIN:
    538 	{
    539 	    struct ppp_rawin *rwin = (struct ppp_rawin *)data;
    540 	    u_char c, q = 0;
    541 
    542 	    for (c = sc->sc_rawin_start; c < sizeof(sc->sc_rawin.buf);)
    543 		rwin->buf[q++] = sc->sc_rawin.buf[c++];
    544 
    545 	    for (c = 0; c < sc->sc_rawin_start;)
    546 		rwin->buf[q++] = sc->sc_rawin.buf[c++];
    547 
    548 	    rwin->count = sc->sc_rawin.count;
    549 	}
    550 	break;
    551 
    552     case PPPIOCSFLAGS:
    553 	flags = *(int *)data & SC_MASK;
    554 	s = splsoftnet();
    555 #ifdef PPP_COMPRESS
    556 	if (sc->sc_flags & SC_CCP_OPEN && !(flags & SC_CCP_OPEN))
    557 	    ppp_ccp_closed(sc);
    558 #endif
    559 	splhigh();	/* XXX IMP ME HARDER */
    560 	sc->sc_flags = (sc->sc_flags & ~SC_MASK) | flags;
    561 	splx(s);
    562 	break;
    563 
    564     case PPPIOCSMRU:
    565 	mru = *(int *)data;
    566 	if (mru >= PPP_MINMRU && mru <= PPP_MAXMRU)
    567 	    sc->sc_mru = mru;
    568 	break;
    569 
    570     case PPPIOCGMRU:
    571 	*(int *)data = sc->sc_mru;
    572 	break;
    573 
    574 #ifdef VJC
    575     case PPPIOCSMAXCID:
    576 	if (sc->sc_comp) {
    577 	    s = splsoftnet();
    578 	    sl_compress_setup(sc->sc_comp, *(int *)data);
    579 	    splx(s);
    580 	}
    581 	break;
    582 #endif
    583 
    584     case PPPIOCXFERUNIT:
    585 	sc->sc_xfer = l->l_proc->p_pid;
    586 	break;
    587 
    588 #ifdef PPP_COMPRESS
    589     case PPPIOCSCOMPRESS:
    590 	odp = (struct ppp_option_data *) data;
    591 	nb = odp->length;
    592 	if (nb > sizeof(ccp_option))
    593 	    nb = sizeof(ccp_option);
    594 	if ((error = copyin(odp->ptr, ccp_option, nb)) != 0)
    595 	    return (error);
    596 	if (ccp_option[1] < 2)	/* preliminary check on the length byte */
    597 	    return (EINVAL);
    598 	for (cp = ppp_compressors; *cp != NULL; ++cp)
    599 	    if ((*cp)->compress_proto == ccp_option[0]) {
    600 		/*
    601 		 * Found a handler for the protocol - try to allocate
    602 		 * a compressor or decompressor.
    603 		 */
    604 		error = 0;
    605 		if (odp->transmit) {
    606 		    s = splsoftnet();
    607 		    if (sc->sc_xc_state != NULL)
    608 			(*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
    609 		    sc->sc_xcomp = *cp;
    610 		    sc->sc_xc_state = (*cp)->comp_alloc(ccp_option, nb);
    611 		    if (sc->sc_xc_state == NULL) {
    612 			if (sc->sc_flags & SC_DEBUG)
    613 			    printf("%s: comp_alloc failed\n",
    614 				sc->sc_if.if_xname);
    615 			error = ENOBUFS;
    616 		    }
    617 		    splhigh();	/* XXX IMP ME HARDER */
    618 		    sc->sc_flags &= ~SC_COMP_RUN;
    619 		    splx(s);
    620 		} else {
    621 		    s = splsoftnet();
    622 		    if (sc->sc_rc_state != NULL)
    623 			(*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
    624 		    sc->sc_rcomp = *cp;
    625 		    sc->sc_rc_state = (*cp)->decomp_alloc(ccp_option, nb);
    626 		    if (sc->sc_rc_state == NULL) {
    627 			if (sc->sc_flags & SC_DEBUG)
    628 			    printf("%s: decomp_alloc failed\n",
    629 				sc->sc_if.if_xname);
    630 			error = ENOBUFS;
    631 		    }
    632 		    splhigh();	/* XXX IMP ME HARDER */
    633 		    sc->sc_flags &= ~SC_DECOMP_RUN;
    634 		    splx(s);
    635 		}
    636 		return (error);
    637 	    }
    638 	if (sc->sc_flags & SC_DEBUG)
    639 	    printf("%s: no compressor for [%x %x %x], %x\n",
    640 		sc->sc_if.if_xname, ccp_option[0], ccp_option[1],
    641 		ccp_option[2], nb);
    642 	return (EINVAL);	/* no handler found */
    643 #endif /* PPP_COMPRESS */
    644 
    645     case PPPIOCGNPMODE:
    646     case PPPIOCSNPMODE:
    647 	npi = (struct npioctl *) data;
    648 	switch (npi->protocol) {
    649 	case PPP_IP:
    650 	    npx = NP_IP;
    651 	    break;
    652 	case PPP_IPV6:
    653 	    npx = NP_IPV6;
    654 	    break;
    655 	default:
    656 	    return EINVAL;
    657 	}
    658 	if (cmd == PPPIOCGNPMODE) {
    659 	    npi->mode = sc->sc_npmode[npx];
    660 	} else {
    661 	    if (npi->mode != sc->sc_npmode[npx]) {
    662 		s = splnet();
    663 		sc->sc_npmode[npx] = npi->mode;
    664 		if (npi->mode != NPMODE_QUEUE) {
    665 		    ppp_requeue(sc);
    666 		    ppp_restart(sc);
    667 		}
    668 		splx(s);
    669 	    }
    670 	}
    671 	break;
    672 
    673     case PPPIOCGIDLE:
    674 	s = splsoftnet();
    675 	t = time_second;
    676 	((struct ppp_idle *)data)->xmit_idle = t - sc->sc_last_sent;
    677 	((struct ppp_idle *)data)->recv_idle = t - sc->sc_last_recv;
    678 	splx(s);
    679 	break;
    680 
    681 #ifdef PPP_FILTER
    682     case PPPIOCSPASS:
    683     case PPPIOCSACTIVE:
    684 	/* These are no longer supported. */
    685 	return EOPNOTSUPP;
    686 
    687     case PPPIOCSIPASS:
    688     case PPPIOCSOPASS:
    689     case PPPIOCSIACTIVE:
    690     case PPPIOCSOACTIVE:
    691 	nbp = (struct bpf_program *) data;
    692 	if ((unsigned) nbp->bf_len > BPF_MAXINSNS)
    693 	    return EINVAL;
    694 	newcodelen = nbp->bf_len * sizeof(struct bpf_insn);
    695 	if (newcodelen != 0) {
    696 	    newcode = malloc(newcodelen, M_DEVBUF, M_WAITOK);
    697 	    /* WAITOK -- malloc() never fails. */
    698 	    if ((error = copyin((void *)nbp->bf_insns, (void *)newcode,
    699 			       newcodelen)) != 0) {
    700 		free(newcode, M_DEVBUF);
    701 		return error;
    702 	    }
    703 	    if (!bpf_validate(newcode, nbp->bf_len)) {
    704 		free(newcode, M_DEVBUF);
    705 		return EINVAL;
    706 	    }
    707 	} else
    708 	    newcode = 0;
    709 	switch (cmd) {
    710 	case PPPIOCSIPASS:
    711 	    bp = &sc->sc_pass_filt_in;
    712 	    break;
    713 
    714 	case PPPIOCSOPASS:
    715 	    bp = &sc->sc_pass_filt_out;
    716 	    break;
    717 
    718 	case PPPIOCSIACTIVE:
    719 	    bp = &sc->sc_active_filt_in;
    720 	    break;
    721 
    722 	case PPPIOCSOACTIVE:
    723 	    bp = &sc->sc_active_filt_out;
    724 	    break;
    725 	default:
    726 	    free(newcode, M_DEVBUF);
    727 	    return (EPASSTHROUGH);
    728 	}
    729 	oldcode = bp->bf_insns;
    730 	s = splnet();
    731 	bp->bf_len = nbp->bf_len;
    732 	bp->bf_insns = newcode;
    733 	splx(s);
    734 	if (oldcode != 0)
    735 	    free(oldcode, M_DEVBUF);
    736 	break;
    737 #endif /* PPP_FILTER */
    738 
    739     default:
    740 	return (EPASSTHROUGH);
    741     }
    742     return (0);
    743 }
    744 
    745 /*
    746  * Process an ioctl request to the ppp network interface.
    747  */
    748 static int
    749 pppsioctl(struct ifnet *ifp, u_long cmd, void *data)
    750 {
    751     struct lwp *l = curlwp;	/* XXX */
    752     struct ppp_softc *sc = ifp->if_softc;
    753     struct ifaddr *ifa = (struct ifaddr *)data;
    754     struct ifreq *ifr = (struct ifreq *)data;
    755     struct ppp_stats *psp;
    756 #ifdef	PPP_COMPRESS
    757     struct ppp_comp_stats *pcp;
    758 #endif
    759     int s = splnet(), error = 0;
    760 
    761     switch (cmd) {
    762     case SIOCSIFFLAGS:
    763 	if ((ifp->if_flags & IFF_RUNNING) == 0)
    764 	    ifp->if_flags &= ~IFF_UP;
    765 	break;
    766 
    767     case SIOCSIFADDR:
    768 	switch (ifa->ifa_addr->sa_family) {
    769 #ifdef INET
    770 	case AF_INET:
    771 	    break;
    772 #endif
    773 #ifdef INET6
    774 	case AF_INET6:
    775 	    break;
    776 #endif
    777 	default:
    778 	    error = EAFNOSUPPORT;
    779 	    break;
    780 	}
    781 	break;
    782 
    783     case SIOCSIFDSTADDR:
    784 	switch (ifa->ifa_addr->sa_family) {
    785 #ifdef INET
    786 	case AF_INET:
    787 	    break;
    788 #endif
    789 #ifdef INET6
    790 	case AF_INET6:
    791 	    break;
    792 #endif
    793 	default:
    794 	    error = EAFNOSUPPORT;
    795 	    break;
    796 	}
    797 	break;
    798 
    799     case SIOCSIFMTU:
    800 	if ((error = kauth_authorize_network(l->l_cred,
    801 	    KAUTH_NETWORK_INTERFACE, KAUTH_REQ_NETWORK_INTERFACE_SETPRIV,
    802 	    ifp, (void *)cmd, NULL) != 0))
    803 	    break;
    804 	sc->sc_if.if_mtu = ifr->ifr_mtu;
    805 	break;
    806 
    807     case SIOCGIFMTU:
    808 	ifr->ifr_mtu = sc->sc_if.if_mtu;
    809 	break;
    810 
    811     case SIOCADDMULTI:
    812     case SIOCDELMULTI:
    813 	if (ifr == NULL) {
    814 	    error = EAFNOSUPPORT;
    815 	    break;
    816 	}
    817 	switch (ifreq_getaddr(cmd, ifr)->sa_family) {
    818 #ifdef INET
    819 	case AF_INET:
    820 	    break;
    821 #endif
    822 #ifdef INET6
    823 	case AF_INET6:
    824 	    break;
    825 #endif
    826 	default:
    827 	    error = EAFNOSUPPORT;
    828 	    break;
    829 	}
    830 	break;
    831 
    832     case SIOCGPPPSTATS:
    833 	psp = &((struct ifpppstatsreq *) data)->stats;
    834 	memset(psp, 0, sizeof(*psp));
    835 	psp->p = sc->sc_stats;
    836 #if defined(VJC) && !defined(SL_NO_STATS)
    837 	if (sc->sc_comp) {
    838 	    psp->vj.vjs_packets = sc->sc_comp->sls_packets;
    839 	    psp->vj.vjs_compressed = sc->sc_comp->sls_compressed;
    840 	    psp->vj.vjs_searches = sc->sc_comp->sls_searches;
    841 	    psp->vj.vjs_misses = sc->sc_comp->sls_misses;
    842 	    psp->vj.vjs_uncompressedin = sc->sc_comp->sls_uncompressedin;
    843 	    psp->vj.vjs_compressedin = sc->sc_comp->sls_compressedin;
    844 	    psp->vj.vjs_errorin = sc->sc_comp->sls_errorin;
    845 	    psp->vj.vjs_tossed = sc->sc_comp->sls_tossed;
    846 	}
    847 #endif /* VJC */
    848 	break;
    849 
    850 #ifdef PPP_COMPRESS
    851     case SIOCGPPPCSTATS:
    852 	pcp = &((struct ifpppcstatsreq *) data)->stats;
    853 	memset(pcp, 0, sizeof(*pcp));
    854 	if (sc->sc_xc_state != NULL)
    855 	    (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c);
    856 	if (sc->sc_rc_state != NULL)
    857 	    (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d);
    858 	break;
    859 #endif /* PPP_COMPRESS */
    860 
    861     default:
    862 	error = EINVAL;
    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     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     ALTQ_DECL(struct altq_pktattr pktattr;)
    887 
    888     if (sc->sc_devp == NULL || (ifp->if_flags & IFF_RUNNING) == 0
    889 	|| ((ifp->if_flags & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC)) {
    890 	error = ENETDOWN;	/* sort of */
    891 	goto bad;
    892     }
    893 
    894     IFQ_CLASSIFY(&ifp->if_snd, m0, dst->sa_family, &pktattr);
    895 
    896     /*
    897      * Compute PPP header.
    898      */
    899     m0->m_flags &= ~M_HIGHPRI;
    900     switch (dst->sa_family) {
    901 #ifdef INET
    902     case AF_INET:
    903 	address = PPP_ALLSTATIONS;
    904 	control = PPP_UI;
    905 	protocol = PPP_IP;
    906 	mode = sc->sc_npmode[NP_IP];
    907 
    908 	/*
    909 	 * If this packet has the "low delay" bit set in the IP header,
    910 	 * put it on the fastq instead.
    911 	 */
    912 	ip = mtod(m0, struct ip *);
    913 	if (ip->ip_tos & IPTOS_LOWDELAY)
    914 	    m0->m_flags |= M_HIGHPRI;
    915 	break;
    916 #endif
    917 #ifdef INET6
    918     case AF_INET6:
    919 	address = PPP_ALLSTATIONS;	/*XXX*/
    920 	control = PPP_UI;		/*XXX*/
    921 	protocol = PPP_IPV6;
    922 	mode = sc->sc_npmode[NP_IPV6];
    923 
    924 #if 0	/* XXX flowinfo/traffic class, maybe? */
    925 	/*
    926 	 * If this packet has the "low delay" bit set in the IP header,
    927 	 * put it on the fastq instead.
    928 	 */
    929 	ip = mtod(m0, struct ip *);
    930 	if (ip->ip_tos & IPTOS_LOWDELAY)
    931 	    m0->m_flags |= M_HIGHPRI;
    932 #endif
    933 	break;
    934 #endif
    935     case AF_UNSPEC:
    936 	address = PPP_ADDRESS(dst->sa_data);
    937 	control = PPP_CONTROL(dst->sa_data);
    938 	protocol = PPP_PROTOCOL(dst->sa_data);
    939 	mode = NPMODE_PASS;
    940 	break;
    941     default:
    942 	printf("%s: af%d not supported\n", ifp->if_xname, 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, (u_char *) m0,
    989 			  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, (u_char *) m0,
    999 	    		  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 #if NBPFILTER > 0
   1010     /*
   1011      * See if bpf wants to look at the packet.
   1012      */
   1013     if (sc->sc_if.if_bpf)
   1014 	bpf_mtap(sc->sc_if.if_bpf, m0);
   1015 #endif
   1016 
   1017     /*
   1018      * Put the packet on the appropriate queue.
   1019      */
   1020     s = splnet();
   1021     if (mode == NPMODE_QUEUE) {
   1022 	/* XXX we should limit the number of packets on this queue */
   1023 	*sc->sc_npqtail = m0;
   1024 	m0->m_nextpkt = NULL;
   1025 	sc->sc_npqtail = &m0->m_nextpkt;
   1026     } else {
   1027 	ifq = (m0->m_flags & M_HIGHPRI) ? &sc->sc_fastq : NULL;
   1028 	if ((error = ifq_enqueue2(&sc->sc_if, ifq, m0
   1029 		ALTQ_COMMA ALTQ_DECL(&pktattr))) != 0) {
   1030 	    splx(s);
   1031 	    sc->sc_if.if_oerrors++;
   1032 	    sc->sc_stats.ppp_oerrors++;
   1033 	    return (error);
   1034 	}
   1035 	ppp_restart(sc);
   1036     }
   1037     ifp->if_opackets++;
   1038     ifp->if_obytes += len;
   1039 
   1040     splx(s);
   1041     return (0);
   1042 
   1043 bad:
   1044     m_freem(m0);
   1045     return (error);
   1046 }
   1047 
   1048 /*
   1049  * After a change in the NPmode for some NP, move packets from the
   1050  * npqueue to the send queue or the fast queue as appropriate.
   1051  * Should be called at splnet, since we muck with the queues.
   1052  */
   1053 static void
   1054 ppp_requeue(struct ppp_softc *sc)
   1055 {
   1056     struct mbuf *m, **mpp;
   1057     struct ifqueue *ifq;
   1058     enum NPmode mode;
   1059     int error;
   1060 
   1061     for (mpp = &sc->sc_npqueue; (m = *mpp) != NULL; ) {
   1062 	switch (PPP_PROTOCOL(mtod(m, u_char *))) {
   1063 	case PPP_IP:
   1064 	    mode = sc->sc_npmode[NP_IP];
   1065 	    break;
   1066 	case PPP_IPV6:
   1067 	    mode = sc->sc_npmode[NP_IPV6];
   1068 	    break;
   1069 	default:
   1070 	    mode = NPMODE_PASS;
   1071 	}
   1072 
   1073 	switch (mode) {
   1074 	case NPMODE_PASS:
   1075 	    /*
   1076 	     * This packet can now go on one of the queues to be sent.
   1077 	     */
   1078 	    *mpp = m->m_nextpkt;
   1079 	    m->m_nextpkt = NULL;
   1080 	    ifq = (m->m_flags & M_HIGHPRI) ? &sc->sc_fastq : NULL;
   1081 	    if ((error = ifq_enqueue2(&sc->sc_if, ifq, m ALTQ_COMMA
   1082 		ALTQ_DECL(NULL))) != 0) {
   1083 		sc->sc_if.if_oerrors++;
   1084 		sc->sc_stats.ppp_oerrors++;
   1085 	    }
   1086 	    break;
   1087 
   1088 	case NPMODE_DROP:
   1089 	case NPMODE_ERROR:
   1090 	    *mpp = m->m_nextpkt;
   1091 	    m_freem(m);
   1092 	    break;
   1093 
   1094 	case NPMODE_QUEUE:
   1095 	    mpp = &m->m_nextpkt;
   1096 	    break;
   1097 	}
   1098     }
   1099     sc->sc_npqtail = mpp;
   1100 }
   1101 
   1102 /*
   1103  * Transmitter has finished outputting some stuff;
   1104  * remember to call sc->sc_start later at splsoftnet.
   1105  */
   1106 void
   1107 ppp_restart(struct ppp_softc *sc)
   1108 {
   1109     int s = splhigh();	/* XXX IMP ME HARDER */
   1110 
   1111     sc->sc_flags &= ~SC_TBUSY;
   1112     softint_schedule(sc->sc_si);
   1113     splx(s);
   1114 }
   1115 
   1116 /*
   1117  * Get a packet to send.  This procedure is intended to be called at
   1118  * splsoftnet, since it may involve time-consuming operations such as
   1119  * applying VJ compression, packet compression, address/control and/or
   1120  * protocol field compression to the packet.
   1121  */
   1122 struct mbuf *
   1123 ppp_dequeue(struct ppp_softc *sc)
   1124 {
   1125     struct mbuf *m, *mp;
   1126     u_char *cp;
   1127     int address, control, protocol;
   1128     int s;
   1129 
   1130     /*
   1131      * Grab a packet to send: first try the fast queue, then the
   1132      * normal queue.
   1133      */
   1134     s = splnet();
   1135     if (sc->sc_nfastq < sc->sc_maxfastq) {
   1136 	IF_DEQUEUE(&sc->sc_fastq, m);
   1137 	if (m != NULL)
   1138 	    sc->sc_nfastq++;
   1139 	else
   1140 	    IFQ_DEQUEUE(&sc->sc_if.if_snd, m);
   1141     } else {
   1142 	sc->sc_nfastq = 0;
   1143 	IFQ_DEQUEUE(&sc->sc_if.if_snd, m);
   1144 	if (m == NULL) {
   1145 	    IF_DEQUEUE(&sc->sc_fastq, m);
   1146 	    if (m != NULL)
   1147 		sc->sc_nfastq++;
   1148 	}
   1149     }
   1150     splx(s);
   1151 
   1152     if (m == NULL)
   1153 	return NULL;
   1154 
   1155     ++sc->sc_stats.ppp_opackets;
   1156 
   1157     /*
   1158      * Extract the ppp header of the new packet.
   1159      * The ppp header will be in one mbuf.
   1160      */
   1161     cp = mtod(m, u_char *);
   1162     address = PPP_ADDRESS(cp);
   1163     control = PPP_CONTROL(cp);
   1164     protocol = PPP_PROTOCOL(cp);
   1165 
   1166     switch (protocol) {
   1167     case PPP_IP:
   1168 #ifdef VJC
   1169 	/*
   1170 	 * If the packet is a TCP/IP packet, see if we can compress it.
   1171 	 */
   1172 	if ((sc->sc_flags & SC_COMP_TCP) && sc->sc_comp != NULL) {
   1173 	    struct ip *ip;
   1174 	    int type;
   1175 
   1176 	    mp = m;
   1177 	    ip = (struct ip *) (cp + PPP_HDRLEN);
   1178 	    if (mp->m_len <= PPP_HDRLEN) {
   1179 		mp = mp->m_next;
   1180 		if (mp == NULL)
   1181 		    break;
   1182 		ip = mtod(mp, struct ip *);
   1183 	    }
   1184 	    /* this code assumes the IP/TCP header is in one non-shared mbuf */
   1185 	    if (ip->ip_p == IPPROTO_TCP) {
   1186 		type = sl_compress_tcp(mp, ip, sc->sc_comp,
   1187 				       !(sc->sc_flags & SC_NO_TCP_CCID));
   1188 		switch (type) {
   1189 		case TYPE_UNCOMPRESSED_TCP:
   1190 		    protocol = PPP_VJC_UNCOMP;
   1191 		    break;
   1192 		case TYPE_COMPRESSED_TCP:
   1193 		    protocol = PPP_VJC_COMP;
   1194 		    cp = mtod(m, u_char *);
   1195 		    cp[0] = address;	/* header has moved */
   1196 		    cp[1] = control;
   1197 		    cp[2] = 0;
   1198 		    break;
   1199 		}
   1200 		cp[3] = protocol;	/* update protocol in PPP header */
   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 		/* Send the compressed packet instead of the original. */
   1227 		m_freem(m);
   1228 		m = mcomp;
   1229 		cp = mtod(m, u_char *);
   1230 		protocol = cp[3];
   1231 	    } else {
   1232 		/* Can't transmit compressed packets until CCP is up. */
   1233 		m_freem(mcomp);
   1234 	    }
   1235 	}
   1236     }
   1237 #endif	/* PPP_COMPRESS */
   1238 
   1239     /*
   1240      * Compress the address/control and protocol, if possible.
   1241      */
   1242     if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
   1243 	control == PPP_UI && protocol != PPP_ALLSTATIONS &&
   1244 	protocol != PPP_LCP) {
   1245 	/* can compress address/control */
   1246 	m->m_data += 2;
   1247 	m->m_len -= 2;
   1248     }
   1249     if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
   1250 	/* can compress protocol */
   1251 	if (mtod(m, u_char *) == cp) {
   1252 	    cp[2] = cp[1];	/* move address/control up */
   1253 	    cp[1] = cp[0];
   1254 	}
   1255 	++m->m_data;
   1256 	--m->m_len;
   1257     }
   1258 
   1259     return m;
   1260 }
   1261 
   1262 /*
   1263  * Software interrupt routine, called at splsoftnet.
   1264  */
   1265 static void
   1266 pppintr(void *arg)
   1267 {
   1268 	struct ppp_softc *sc = arg;
   1269 	struct mbuf *m;
   1270 	int s;
   1271 
   1272 	if (!(sc->sc_flags & SC_TBUSY)
   1273 	    && (IFQ_IS_EMPTY(&sc->sc_if.if_snd) == 0 || sc->sc_fastq.ifq_head
   1274 		|| sc->sc_outm)) {
   1275 		s = splhigh();	/* XXX IMP ME HARDER */
   1276 		sc->sc_flags |= SC_TBUSY;
   1277 		splx(s);
   1278 		(*sc->sc_start)(sc);
   1279 	}
   1280 	for (;;) {
   1281 		s = splnet();
   1282 		IF_DEQUEUE(&sc->sc_rawq, m);
   1283 		splx(s);
   1284 		if (m == NULL)
   1285 			break;
   1286 		ppp_inproc(sc, m);
   1287 	}
   1288 }
   1289 
   1290 #ifdef PPP_COMPRESS
   1291 /*
   1292  * Handle a CCP packet.  `rcvd' is 1 if the packet was received,
   1293  * 0 if it is about to be transmitted.
   1294  */
   1295 static void
   1296 ppp_ccp(struct ppp_softc *sc, struct mbuf *m, int rcvd)
   1297 {
   1298     u_char *dp, *ep;
   1299     struct mbuf *mp;
   1300     int slen, s;
   1301 
   1302     /*
   1303      * Get a pointer to the data after the PPP header.
   1304      */
   1305     if (m->m_len <= PPP_HDRLEN) {
   1306 	mp = m->m_next;
   1307 	if (mp == NULL)
   1308 	    return;
   1309 	dp = (mp != NULL)? mtod(mp, u_char *): NULL;
   1310     } else {
   1311 	mp = m;
   1312 	dp = mtod(mp, u_char *) + PPP_HDRLEN;
   1313     }
   1314 
   1315     ep = mtod(mp, u_char *) + mp->m_len;
   1316     if (dp + CCP_HDRLEN > ep)
   1317 	return;
   1318     slen = CCP_LENGTH(dp);
   1319     if (dp + slen > ep) {
   1320 	if (sc->sc_flags & SC_DEBUG)
   1321 	    printf("if_ppp/ccp: not enough data in mbuf (%p+%x > %p+%x)\n",
   1322 		dp, slen, mtod(mp, u_char *), mp->m_len);
   1323 	return;
   1324     }
   1325 
   1326     switch (CCP_CODE(dp)) {
   1327     case CCP_CONFREQ:
   1328     case CCP_TERMREQ:
   1329     case CCP_TERMACK:
   1330 	/* CCP must be going down - disable compression */
   1331 	if (sc->sc_flags & SC_CCP_UP) {
   1332 	    s = splhigh();	/* XXX IMP ME HARDER */
   1333 	    sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
   1334 	    splx(s);
   1335 	}
   1336 	break;
   1337 
   1338     case CCP_CONFACK:
   1339 	if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
   1340 	    && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
   1341 	    && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
   1342 	    if (!rcvd) {
   1343 		/* we're agreeing to send compressed packets. */
   1344 		if (sc->sc_xc_state != NULL
   1345 		    && (*sc->sc_xcomp->comp_init)
   1346 			(sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
   1347 			 sc->sc_unit, 0, sc->sc_flags & SC_DEBUG)) {
   1348 		    s = splhigh();	/* XXX IMP ME HARDER */
   1349 		    sc->sc_flags |= SC_COMP_RUN;
   1350 		    splx(s);
   1351 		}
   1352 	    } else {
   1353 		/* peer is agreeing to send compressed packets. */
   1354 		if (sc->sc_rc_state != NULL
   1355 		    && (*sc->sc_rcomp->decomp_init)
   1356 			(sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
   1357 			 sc->sc_unit, 0, sc->sc_mru,
   1358 			 sc->sc_flags & SC_DEBUG)) {
   1359 		    s = splhigh();	/* XXX IMP ME HARDER */
   1360 		    sc->sc_flags |= SC_DECOMP_RUN;
   1361 		    sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
   1362 		    splx(s);
   1363 		}
   1364 	    }
   1365 	}
   1366 	break;
   1367 
   1368     case CCP_RESETACK:
   1369 	if (sc->sc_flags & SC_CCP_UP) {
   1370 	    if (!rcvd) {
   1371 		if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN))
   1372 		    (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
   1373 	    } else {
   1374 		if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
   1375 		    (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
   1376 		    s = splhigh();	/* XXX IMP ME HARDER */
   1377 		    sc->sc_flags &= ~SC_DC_ERROR;
   1378 		    splx(s);
   1379 		}
   1380 	    }
   1381 	}
   1382 	break;
   1383     }
   1384 }
   1385 
   1386 /*
   1387  * CCP is down; free (de)compressor state if necessary.
   1388  */
   1389 static void
   1390 ppp_ccp_closed(struct ppp_softc *sc)
   1391 {
   1392     if (sc->sc_xc_state) {
   1393 	(*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
   1394 	sc->sc_xc_state = NULL;
   1395     }
   1396     if (sc->sc_rc_state) {
   1397 	(*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
   1398 	sc->sc_rc_state = NULL;
   1399     }
   1400 }
   1401 #endif /* PPP_COMPRESS */
   1402 
   1403 /*
   1404  * PPP packet input routine.
   1405  * The caller has checked and removed the FCS and has inserted
   1406  * the address/control bytes and the protocol high byte if they
   1407  * were omitted.
   1408  */
   1409 void
   1410 ppppktin(struct ppp_softc *sc, struct mbuf *m, int lost)
   1411 {
   1412     int s = splhigh();	/* XXX IMP ME HARDER */
   1413 
   1414     if (lost)
   1415 	m->m_flags |= M_ERRMARK;
   1416     IF_ENQUEUE(&sc->sc_rawq, m);
   1417     softint_schedule(sc->sc_si);
   1418     splx(s);
   1419 }
   1420 
   1421 /*
   1422  * Process a received PPP packet, doing decompression as necessary.
   1423  * Should be called at splsoftnet.
   1424  */
   1425 #define COMPTYPE(proto)	((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
   1426 			 TYPE_UNCOMPRESSED_TCP)
   1427 
   1428 static void
   1429 ppp_inproc(struct ppp_softc *sc, struct mbuf *m)
   1430 {
   1431     struct ifnet *ifp = &sc->sc_if;
   1432     struct ifqueue *inq;
   1433     int s, ilen, proto, rv;
   1434     u_char *cp, adrs, ctrl;
   1435     struct mbuf *mp, *dmp = NULL;
   1436 #ifdef VJC
   1437     int xlen;
   1438     u_char *iphdr;
   1439     u_int hlen;
   1440 #endif
   1441 
   1442     sc->sc_stats.ppp_ipackets++;
   1443 
   1444     if (sc->sc_flags & SC_LOG_INPKT) {
   1445 	ilen = 0;
   1446 	for (mp = m; mp != NULL; mp = mp->m_next)
   1447 	    ilen += mp->m_len;
   1448 	printf("%s: got %d bytes\n", ifp->if_xname, ilen);
   1449 	pppdumpm(m);
   1450     }
   1451 
   1452     cp = mtod(m, u_char *);
   1453     adrs = PPP_ADDRESS(cp);
   1454     ctrl = PPP_CONTROL(cp);
   1455     proto = PPP_PROTOCOL(cp);
   1456 
   1457     if (m->m_flags & M_ERRMARK) {
   1458 	m->m_flags &= ~M_ERRMARK;
   1459 	s = splhigh();	/* XXX IMP ME HARDER */
   1460 	sc->sc_flags |= SC_VJ_RESET;
   1461 	splx(s);
   1462     }
   1463 
   1464 #ifdef PPP_COMPRESS
   1465     /*
   1466      * Decompress this packet if necessary, update the receiver's
   1467      * dictionary, or take appropriate action on a CCP packet.
   1468      */
   1469     if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
   1470 	&& !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
   1471 	/* decompress this packet */
   1472 	rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
   1473 	if (rv == DECOMP_OK) {
   1474 	    m_freem(m);
   1475 	    if (dmp == NULL) {
   1476 		/* no error, but no decompressed packet produced */
   1477 		return;
   1478 	    }
   1479 	    m = dmp;
   1480 	    cp = mtod(m, u_char *);
   1481 	    proto = PPP_PROTOCOL(cp);
   1482 
   1483 	} else {
   1484 	    /*
   1485 	     * An error has occurred in decompression.
   1486 	     * Pass the compressed packet up to pppd, which may take
   1487 	     * CCP down or issue a Reset-Req.
   1488 	     */
   1489 	    if (sc->sc_flags & SC_DEBUG)
   1490 		printf("%s: decompress failed %d\n", ifp->if_xname, rv);
   1491 	    s = splhigh();	/* XXX IMP ME HARDER */
   1492 	    sc->sc_flags |= SC_VJ_RESET;
   1493 	    if (rv == DECOMP_ERROR)
   1494 		sc->sc_flags |= SC_DC_ERROR;
   1495 	    else
   1496 		sc->sc_flags |= SC_DC_FERROR;
   1497 	    splx(s);
   1498 	}
   1499 
   1500     } else {
   1501 	if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
   1502 	    (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
   1503 	}
   1504 	if (proto == PPP_CCP) {
   1505 	    ppp_ccp(sc, m, 1);
   1506 	}
   1507     }
   1508 #endif
   1509 
   1510     ilen = 0;
   1511     for (mp = m; mp != NULL; mp = mp->m_next)
   1512 	ilen += mp->m_len;
   1513 
   1514 #ifdef VJC
   1515     if (sc->sc_flags & SC_VJ_RESET) {
   1516 	/*
   1517 	 * If we've missed a packet, we must toss subsequent compressed
   1518 	 * packets which don't have an explicit connection ID.
   1519 	 */
   1520 	if (sc->sc_comp)
   1521 	    sl_uncompress_tcp(NULL, 0, TYPE_ERROR, sc->sc_comp);
   1522 	s = splhigh();	/* XXX IMP ME HARDER */
   1523 	sc->sc_flags &= ~SC_VJ_RESET;
   1524 	splx(s);
   1525     }
   1526 
   1527     /*
   1528      * See if we have a VJ-compressed packet to uncompress.
   1529      */
   1530     if (proto == PPP_VJC_COMP) {
   1531 	if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
   1532 	    goto bad;
   1533 
   1534 	xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
   1535 				      ilen - PPP_HDRLEN, TYPE_COMPRESSED_TCP,
   1536 				      sc->sc_comp, &iphdr, &hlen);
   1537 
   1538 	if (xlen <= 0) {
   1539 	    if (sc->sc_flags & SC_DEBUG)
   1540 		printf("%s: VJ uncompress failed on type comp\n",
   1541 		    ifp->if_xname);
   1542 	    goto bad;
   1543 	}
   1544 
   1545 	/* Copy the PPP and IP headers into a new mbuf. */
   1546 	MGETHDR(mp, M_DONTWAIT, MT_DATA);
   1547 	if (mp == NULL)
   1548 	    goto bad;
   1549 	mp->m_len = 0;
   1550 	mp->m_next = NULL;
   1551 	if (hlen + PPP_HDRLEN > MHLEN) {
   1552 	    MCLGET(mp, M_DONTWAIT);
   1553 	    if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) {
   1554 		m_freem(mp);
   1555 		goto bad;	/* lose if big headers and no clusters */
   1556 	    }
   1557 	}
   1558 	cp = mtod(mp, u_char *);
   1559 	cp[0] = adrs;
   1560 	cp[1] = ctrl;
   1561 	cp[2] = 0;
   1562 	cp[3] = PPP_IP;
   1563 	proto = PPP_IP;
   1564 	bcopy(iphdr, cp + PPP_HDRLEN, hlen);
   1565 	mp->m_len = hlen + PPP_HDRLEN;
   1566 
   1567 	/*
   1568 	 * Trim the PPP and VJ headers off the old mbuf
   1569 	 * and stick the new and old mbufs together.
   1570 	 */
   1571 	m->m_data += PPP_HDRLEN + xlen;
   1572 	m->m_len -= PPP_HDRLEN + xlen;
   1573 	if (m->m_len <= M_TRAILINGSPACE(mp)) {
   1574 	    bcopy(mtod(m, u_char *), mtod(mp, u_char *) + mp->m_len, m->m_len);
   1575 	    mp->m_len += m->m_len;
   1576 	    MFREE(m, mp->m_next);
   1577 	} else
   1578 	    mp->m_next = m;
   1579 	m = mp;
   1580 	ilen += hlen - xlen;
   1581 
   1582     } else if (proto == PPP_VJC_UNCOMP) {
   1583 	if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
   1584 	    goto bad;
   1585 
   1586 	xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
   1587 				      ilen - PPP_HDRLEN, TYPE_UNCOMPRESSED_TCP,
   1588 				      sc->sc_comp, &iphdr, &hlen);
   1589 
   1590 	if (xlen < 0) {
   1591 	    if (sc->sc_flags & SC_DEBUG)
   1592 		printf("%s: VJ uncompress failed on type uncomp\n",
   1593 		    ifp->if_xname);
   1594 	    goto bad;
   1595 	}
   1596 
   1597 	proto = PPP_IP;
   1598 	cp[3] = PPP_IP;
   1599     }
   1600 #endif /* VJC */
   1601 
   1602     /*
   1603      * If the packet will fit in a header mbuf, don't waste a
   1604      * whole cluster on it.
   1605      */
   1606     if (ilen <= MHLEN && M_IS_CLUSTER(m)) {
   1607 	MGETHDR(mp, M_DONTWAIT, MT_DATA);
   1608 	if (mp != NULL) {
   1609 	    m_copydata(m, 0, ilen, mtod(mp, void *));
   1610 	    m_freem(m);
   1611 	    m = mp;
   1612 	    m->m_len = ilen;
   1613 	}
   1614     }
   1615     m->m_pkthdr.len = ilen;
   1616     m->m_pkthdr.rcvif = ifp;
   1617 
   1618     if ((proto & 0x8000) == 0) {
   1619 #ifdef PPP_FILTER
   1620 	/*
   1621 	 * See whether we want to pass this packet, and
   1622 	 * if it counts as link activity.
   1623 	 */
   1624 	if (sc->sc_pass_filt_in.bf_insns != 0
   1625 	    && bpf_filter(sc->sc_pass_filt_in.bf_insns, (u_char *) m,
   1626 			  ilen, 0) == 0) {
   1627 	    /* drop this packet */
   1628 	    m_freem(m);
   1629 	    return;
   1630 	}
   1631 	if (sc->sc_active_filt_in.bf_insns == 0
   1632 	    || bpf_filter(sc->sc_active_filt_in.bf_insns, (u_char *) m,
   1633 	    		  ilen, 0))
   1634 	    sc->sc_last_recv = time_second;
   1635 #else
   1636 	/*
   1637 	 * Record the time that we received this packet.
   1638 	 */
   1639 	sc->sc_last_recv = time_second;
   1640 #endif /* PPP_FILTER */
   1641     }
   1642 
   1643 #if NBPFILTER > 0
   1644     /* See if bpf wants to look at the packet. */
   1645     if (sc->sc_if.if_bpf)
   1646 	bpf_mtap(sc->sc_if.if_bpf, m);
   1647 #endif
   1648 
   1649     rv = 0;
   1650     switch (proto) {
   1651 #ifdef INET
   1652     case PPP_IP:
   1653 	/*
   1654 	 * IP packet - take off the ppp header and pass it up to IP.
   1655 	 */
   1656 	if ((ifp->if_flags & IFF_UP) == 0
   1657 	    || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
   1658 	    /* interface is down - drop the packet. */
   1659 	    m_freem(m);
   1660 	    return;
   1661 	}
   1662 	m->m_pkthdr.len -= PPP_HDRLEN;
   1663 	m->m_data += PPP_HDRLEN;
   1664 	m->m_len -= PPP_HDRLEN;
   1665 #ifdef GATEWAY
   1666 	if (ipflow_fastforward(m))
   1667 		return;
   1668 #endif
   1669 	schednetisr(NETISR_IP);
   1670 	inq = &ipintrq;
   1671 	break;
   1672 #endif
   1673 
   1674 #ifdef INET6
   1675     case PPP_IPV6:
   1676 	/*
   1677 	 * IPv6 packet - take off the ppp header and pass it up to IPv6.
   1678 	 */
   1679 	if ((ifp->if_flags & IFF_UP) == 0
   1680 	    || sc->sc_npmode[NP_IPV6] != NPMODE_PASS) {
   1681 	    /* interface is down - drop the packet. */
   1682 	    m_freem(m);
   1683 	    return;
   1684 	}
   1685 	m->m_pkthdr.len -= PPP_HDRLEN;
   1686 	m->m_data += PPP_HDRLEN;
   1687 	m->m_len -= PPP_HDRLEN;
   1688 #ifdef GATEWAY
   1689 	if (ip6flow_fastforward(m))
   1690 		return;
   1691 #endif
   1692 	schednetisr(NETISR_IPV6);
   1693 	inq = &ip6intrq;
   1694 	break;
   1695 #endif
   1696 
   1697     default:
   1698 	/*
   1699 	 * Some other protocol - place on input queue for read().
   1700 	 */
   1701 	inq = &sc->sc_inq;
   1702 	rv = 1;
   1703 	break;
   1704     }
   1705 
   1706     /*
   1707      * Put the packet on the appropriate input queue.
   1708      */
   1709     s = splnet();
   1710     if (IF_QFULL(inq)) {
   1711 	IF_DROP(inq);
   1712 	splx(s);
   1713 	if (sc->sc_flags & SC_DEBUG)
   1714 	    printf("%s: input queue full\n", ifp->if_xname);
   1715 	ifp->if_iqdrops++;
   1716 	goto bad;
   1717     }
   1718     IF_ENQUEUE(inq, m);
   1719     splx(s);
   1720     ifp->if_ipackets++;
   1721     ifp->if_ibytes += ilen;
   1722 
   1723     if (rv)
   1724 	(*sc->sc_ctlp)(sc);
   1725 
   1726     return;
   1727 
   1728  bad:
   1729     m_freem(m);
   1730     sc->sc_if.if_ierrors++;
   1731     sc->sc_stats.ppp_ierrors++;
   1732 }
   1733 
   1734 #define MAX_DUMP_BYTES	128
   1735 
   1736 static void
   1737 pppdumpm(struct mbuf *m0)
   1738 {
   1739     char buf[3*MAX_DUMP_BYTES+4];
   1740     char *bp = buf;
   1741     struct mbuf *m;
   1742 
   1743     for (m = m0; m; m = m->m_next) {
   1744 	int l = m->m_len;
   1745 	u_char *rptr = (u_char *)m->m_data;
   1746 
   1747 	while (l--) {
   1748 	    if (bp > buf + sizeof(buf) - 4)
   1749 		goto done;
   1750 	    *bp++ = hexdigits[*rptr >> 4]; /* convert byte to ascii hex */
   1751 	    *bp++ = hexdigits[*rptr++ & 0xf];
   1752 	}
   1753 
   1754 	if (m->m_next) {
   1755 	    if (bp > buf + sizeof(buf) - 3)
   1756 		goto done;
   1757 	    *bp++ = '|';
   1758 	} else
   1759 	    *bp++ = ' ';
   1760     }
   1761 done:
   1762     if (m)
   1763 	*bp++ = '>';
   1764     *bp = 0;
   1765     printf("%s\n", buf);
   1766 }
   1767 
   1768 #ifdef ALTQ
   1769 /*
   1770  * a wrapper to transmit a packet from if_start since ALTQ uses
   1771  * if_start to send a packet.
   1772  */
   1773 static void
   1774 ppp_ifstart(struct ifnet *ifp)
   1775 {
   1776 	struct ppp_softc *sc;
   1777 
   1778 	sc = ifp->if_softc;
   1779 	(*sc->sc_start)(sc);
   1780 }
   1781 #endif
   1782