File:  [DragonFly] / src / sys / net / ppp / if_ppp.c
Revision 1.17: download - view: text, annotated - select for diffs
Thu Apr 22 04:22:05 2004 UTC (10 years, 5 months ago) by dillon
Branches: MAIN
CVS tags: HEAD
M_NOWAIT -> M_WAITOK or M_INTWAIT conversions.  There is a whole lot of net
code that is improperly using M_NOWAIT.  Also remove now unneeded NULL checks
since malloc will panic rather then return NULL when M_NULLOK is not set.

Use M_INTWAIT|M_NULLOK in some cases (such as route table allocation) in
order to allow malloc to return NULL when the limit for the malloc type
is reached.

    1: /*
    2:  * if_ppp.c - Point-to-Point Protocol (PPP) Asynchronous driver.
    3:  *
    4:  * Copyright (c) 1989 Carnegie Mellon University.
    5:  * All rights reserved.
    6:  *
    7:  * Redistribution and use in source and binary forms are permitted
    8:  * provided that the above copyright notice and this paragraph are
    9:  * duplicated in all such forms and that any documentation,
   10:  * advertising materials, and other materials related to such
   11:  * distribution and use acknowledge that the software was developed
   12:  * by Carnegie Mellon University.  The name of the
   13:  * University may not be used to endorse or promote products derived
   14:  * from this software without specific prior written permission.
   15:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
   16:  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
   17:  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
   18:  *
   19:  * Drew D. Perkins
   20:  * Carnegie Mellon University
   21:  * 4910 Forbes Ave.
   22:  * Pittsburgh, PA 15213
   23:  * (412) 268-8576
   24:  * ddp@andrew.cmu.edu
   25:  *
   26:  * Based on:
   27:  *	@(#)if_sl.c	7.6.1.2 (Berkeley) 2/15/89
   28:  *
   29:  * Copyright (c) 1987 Regents of the University of California.
   30:  * All rights reserved.
   31:  *
   32:  * Redistribution and use in source and binary forms are permitted
   33:  * provided that the above copyright notice and this paragraph are
   34:  * duplicated in all such forms and that any documentation,
   35:  * advertising materials, and other materials related to such
   36:  * distribution and use acknowledge that the software was developed
   37:  * by the University of California, Berkeley.  The name of the
   38:  * University may not be used to endorse or promote products derived
   39:  * from this software without specific prior written permission.
   40:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
   41:  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
   42:  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
   43:  *
   44:  * Serial Line interface
   45:  *
   46:  * Rick Adams
   47:  * Center for Seismic Studies
   48:  * 1300 N 17th Street, Suite 1450
   49:  * Arlington, Virginia 22209
   50:  * (703)276-7900
   51:  * rick@seismo.ARPA
   52:  * seismo!rick
   53:  *
   54:  * Pounded on heavily by Chris Torek (chris@mimsy.umd.edu, umcp-cs!chris).
   55:  * Converted to 4.3BSD Beta by Chris Torek.
   56:  * Other changes made at Berkeley, based in part on code by Kirk Smith.
   57:  *
   58:  * Converted to 4.3BSD+ 386BSD by Brad Parker (brad@cayman.com)
   59:  * Added VJ tcp header compression; more unified ioctls
   60:  *
   61:  * Extensively modified by Paul Mackerras (paulus@cs.anu.edu.au).
   62:  * Cleaned up a lot of the mbuf-related code to fix bugs that
   63:  * caused system crashes and packet corruption.  Changed pppstart
   64:  * so that it doesn't just give up with a collision if the whole
   65:  * packet doesn't fit in the output ring buffer.
   66:  *
   67:  * Added priority queueing for interactive IP packets, following
   68:  * the model of if_sl.c, plus hooks for bpf.
   69:  * Paul Mackerras (paulus@cs.anu.edu.au).
   70:  */
   71: 
   72: /* $FreeBSD: src/sys/net/if_ppp.c,v 1.67.2.4 2002/04/14 21:41:48 luigi Exp $ */
   73: /* $DragonFly: src/sys/net/ppp/if_ppp.c,v 1.17 2004/04/22 04:22:05 dillon Exp $ */
   74: /* from if_sl.c,v 1.11 84/10/04 12:54:47 rick Exp */
   75: /* from NetBSD: if_ppp.c,v 1.15.2.2 1994/07/28 05:17:58 cgd Exp */
   76: 
   77: #include "use_ppp.h"
   78: 
   79: #include "opt_inet.h"
   80: #include "opt_ipx.h"
   81: #include "opt_ppp.h"
   82: 
   83: #ifdef INET
   84: #define VJC
   85: #endif
   86: #define PPP_COMPRESS
   87: 
   88: #include <sys/param.h>
   89: #include <sys/systm.h>
   90: #include <sys/proc.h>
   91: #include <sys/mbuf.h>
   92: #include <sys/socket.h>
   93: #include <sys/filio.h>
   94: #include <sys/sockio.h>
   95: #include <sys/kernel.h>
   96: #include <sys/time.h>
   97: #include <sys/malloc.h>
   98: 
   99: #include <sys/msgport2.h>
  100: 
  101: #include <net/if.h>
  102: #include <net/if_types.h>
  103: #include <net/netisr.h>
  104: #include <net/bpf.h>
  105: 
  106: #ifdef INET
  107: #include <netinet/in.h>
  108: #include <netinet/in_systm.h>
  109: #include <netinet/in_var.h>
  110: #include <netinet/ip.h>
  111: #endif
  112: 
  113: #ifdef IPX
  114: #include <netproto/ipx/ipx.h>
  115: #include <netproto/ipx/ipx_if.h>
  116: #endif
  117: 
  118: #ifdef VJC
  119: #include <net/slcompress.h>
  120: #endif
  121: 
  122: #include "if_ppp.h"
  123: #include "if_pppvar.h"
  124: 
  125: /* minimise diffs */
  126: #ifndef splsoftnet
  127: #define splsoftnet	splnet
  128: #endif
  129: 
  130: #ifdef PPP_COMPRESS
  131: #define PACKETPTR	struct mbuf *
  132: #include <net/ppp_layer/ppp_comp.h>
  133: #endif
  134: 
  135: struct ppp_softc ppp_softc[NPPP];
  136: 
  137: /* XXX layering violation */
  138: extern void	pppasyncattach (void *);
  139: 
  140: static void	pppattach (void *);
  141: PSEUDO_SET(pppattach, if_ppp);
  142: 
  143: static int	pppsioctl (struct ifnet *ifp, u_long cmd, caddr_t data,
  144: 			   struct ucred *);
  145: 
  146: static void	ppp_requeue (struct ppp_softc *);
  147: static void	ppp_ccp (struct ppp_softc *, struct mbuf *m, int rcvd);
  148: static void	ppp_ccp_closed (struct ppp_softc *);
  149: static void	ppp_inproc (struct ppp_softc *, struct mbuf *);
  150: static void	pppdumpm (struct mbuf *m0);
  151: 
  152: /*
  153:  * Some useful mbuf macros not in mbuf.h.
  154:  */
  155: #define M_IS_CLUSTER(m)	((m)->m_flags & M_EXT)
  156: 
  157: #define M_DATASTART(m)	\
  158: 	(M_IS_CLUSTER(m) ? (m)->m_ext.ext_buf : \
  159: 	    (m)->m_flags & M_PKTHDR ? (m)->m_pktdat : (m)->m_dat)
  160: 
  161: #define M_DATASIZE(m)	\
  162: 	(M_IS_CLUSTER(m) ? (m)->m_ext.ext_size : \
  163: 	    (m)->m_flags & M_PKTHDR ? MHLEN: MLEN)
  164: 
  165: /*
  166:  * We steal two bits in the mbuf m_flags, to mark high-priority packets
  167:  * for output, and received packets following lost/corrupted packets.
  168:  */
  169: #define M_HIGHPRI	0x2000	/* output packet for sc_fastq */
  170: #define M_ERRMARK	0x4000	/* steal a bit in mbuf m_flags */
  171: 
  172: 
  173: #ifdef PPP_COMPRESS
  174: /*
  175:  * List of compressors we know about.
  176:  * We leave some space so maybe we can modload compressors.
  177:  */
  178: 
  179: extern struct compressor ppp_bsd_compress;
  180: extern struct compressor ppp_deflate, ppp_deflate_draft;
  181: 
  182: static struct compressor *ppp_compressors[8] = {
  183: #if DO_BSD_COMPRESS && defined(PPP_BSDCOMP)
  184:     &ppp_bsd_compress,
  185: #endif
  186: #if DO_DEFLATE && defined(PPP_DEFLATE)
  187:     &ppp_deflate,
  188:     &ppp_deflate_draft,
  189: #endif
  190:     NULL
  191: };
  192: #endif /* PPP_COMPRESS */
  193: 
  194: /*
  195:  * Software interrupt routine, called at spl[soft]net.
  196:  */
  197: static int
  198: pppintr(struct netmsg *msg)
  199: {
  200:     struct mbuf *m = ((struct netmsg_packet *)msg)->nm_packet;
  201:     struct ppp_softc *sc;
  202:     int i, s;
  203: 
  204:     sc = ppp_softc;
  205:     for (i = 0; i < NPPP; ++i, ++sc) {
  206: 	s = splimp();
  207: 	if (!(sc->sc_flags & SC_TBUSY)
  208: 	    && (sc->sc_if.if_snd.ifq_head || sc->sc_fastq.ifq_head)) {
  209: 	    sc->sc_flags |= SC_TBUSY;
  210: 	    splx(s);
  211: 	    (*sc->sc_start)(sc);
  212: 	} else
  213: 	    splx(s);
  214: 	for (;;) {
  215: 	    s = splimp();
  216: 	    IF_DEQUEUE(&sc->sc_rawq, m);
  217: 	    splx(s);
  218: 	    if (m == NULL)
  219: 		break;
  220: 	    ppp_inproc(sc, m);
  221: 	}
  222:     }
  223:     lwkt_replymsg(&msg->nm_lmsg, 0);
  224:     return(EASYNC);
  225: }
  226: 
  227: /*
  228:  * Called from boot code to establish ppp interfaces.
  229:  */
  230: static void
  231: pppattach(dummy)
  232:     void *dummy;
  233: {
  234:     struct ppp_softc *sc;
  235:     int i = 0;
  236: 
  237:     for (sc = ppp_softc; i < NPPP; sc++) {
  238:     	if_initname(&(sc->sc_if), "ppp", i++);
  239: 	sc->sc_if.if_mtu = PPP_MTU;
  240: 	sc->sc_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
  241: 	sc->sc_if.if_type = IFT_PPP;
  242: 	sc->sc_if.if_hdrlen = PPP_HDRLEN;
  243: 	sc->sc_if.if_ioctl = pppsioctl;
  244: 	sc->sc_if.if_output = pppoutput;
  245: 	sc->sc_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
  246: 	sc->sc_inq.ifq_maxlen = IFQ_MAXLEN;
  247: 	sc->sc_fastq.ifq_maxlen = IFQ_MAXLEN;
  248: 	sc->sc_rawq.ifq_maxlen = IFQ_MAXLEN;
  249: 	if_attach(&sc->sc_if);
  250: 	bpfattach(&sc->sc_if, DLT_PPP, PPP_HDRLEN);
  251:     }
  252:     netisr_register(NETISR_PPP, cpu0_portfn, pppintr);
  253:     /*
  254:      * XXX layering violation - if_ppp can work over any lower level
  255:      * transport that cares to attach to it.
  256:      */
  257:     pppasyncattach(dummy);
  258: }
  259: 
  260: /*
  261:  * Allocate a ppp interface unit and initialize it.
  262:  */
  263: struct ppp_softc *
  264: pppalloc(struct thread *td)
  265: {
  266:     int nppp, i;
  267:     struct ppp_softc *sc;
  268: 
  269:     for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
  270: 	if (sc->sc_xfer == td) {
  271: 	    sc->sc_xfer = NULL;
  272: 	    return sc;
  273: 	}
  274:     for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
  275: 	if (sc->sc_devp == NULL)
  276: 	    break;
  277:     if (nppp >= NPPP)
  278: 	return NULL;
  279: 
  280:     sc->sc_flags = 0;
  281:     sc->sc_mru = PPP_MRU;
  282:     sc->sc_relinq = NULL;
  283:     bzero((char *)&sc->sc_stats, sizeof(sc->sc_stats));
  284: #ifdef VJC
  285:     MALLOC(sc->sc_comp, struct slcompress *, sizeof(struct slcompress),
  286: 	   M_DEVBUF, M_WAITOK);
  287:     sl_compress_init(sc->sc_comp, -1);
  288: #endif
  289: #ifdef PPP_COMPRESS
  290:     sc->sc_xc_state = NULL;
  291:     sc->sc_rc_state = NULL;
  292: #endif /* PPP_COMPRESS */
  293:     for (i = 0; i < NUM_NP; ++i)
  294: 	sc->sc_npmode[i] = NPMODE_ERROR;
  295:     sc->sc_npqueue = NULL;
  296:     sc->sc_npqtail = &sc->sc_npqueue;
  297:     sc->sc_last_sent = sc->sc_last_recv = time_second;
  298: 
  299:     return sc;
  300: }
  301: 
  302: /*
  303:  * Deallocate a ppp unit.  Must be called at splsoftnet or higher.
  304:  */
  305: void
  306: pppdealloc(sc)
  307:     struct ppp_softc *sc;
  308: {
  309:     struct mbuf *m;
  310: 
  311:     if_down(&sc->sc_if);
  312:     sc->sc_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
  313:     getmicrotime(&sc->sc_if.if_lastchange);
  314:     sc->sc_devp = NULL;
  315:     sc->sc_xfer = NULL;
  316:     for (;;) {
  317: 	IF_DEQUEUE(&sc->sc_rawq, m);
  318: 	if (m == NULL)
  319: 	    break;
  320: 	m_freem(m);
  321:     }
  322:     for (;;) {
  323: 	IF_DEQUEUE(&sc->sc_inq, m);
  324: 	if (m == NULL)
  325: 	    break;
  326: 	m_freem(m);
  327:     }
  328:     for (;;) {
  329: 	IF_DEQUEUE(&sc->sc_fastq, m);
  330: 	if (m == NULL)
  331: 	    break;
  332: 	m_freem(m);
  333:     }
  334:     while ((m = sc->sc_npqueue) != NULL) {
  335: 	sc->sc_npqueue = m->m_nextpkt;
  336: 	m_freem(m);
  337:     }
  338: #ifdef PPP_COMPRESS
  339:     ppp_ccp_closed(sc);
  340:     sc->sc_xc_state = NULL;
  341:     sc->sc_rc_state = NULL;
  342: #endif /* PPP_COMPRESS */
  343: #ifdef PPP_FILTER
  344:     if (sc->sc_pass_filt.bf_insns != 0) {
  345: 	free(sc->sc_pass_filt.bf_insns, M_DEVBUF);
  346: 	sc->sc_pass_filt.bf_insns = 0;
  347: 	sc->sc_pass_filt.bf_len = 0;
  348:     }
  349:     if (sc->sc_active_filt.bf_insns != 0) {
  350: 	free(sc->sc_active_filt.bf_insns, M_DEVBUF);
  351: 	sc->sc_active_filt.bf_insns = 0;
  352: 	sc->sc_active_filt.bf_len = 0;
  353:     }
  354: #endif /* PPP_FILTER */
  355: #ifdef VJC
  356:     if (sc->sc_comp != 0) {
  357: 	free(sc->sc_comp, M_DEVBUF);
  358: 	sc->sc_comp = 0;
  359:     }
  360: #endif
  361: }
  362: 
  363: /*
  364:  * Ioctl routine for generic ppp devices.
  365:  */
  366: int
  367: pppioctl(struct ppp_softc *sc, u_long cmd, caddr_t data,
  368:     int flag, struct thread *td)
  369: {
  370:     int s, error, flags, mru, npx;
  371:     u_int nb;
  372:     struct ppp_option_data *odp;
  373:     struct compressor **cp;
  374:     struct npioctl *npi;
  375:     time_t t;
  376: #ifdef PPP_FILTER
  377:     struct bpf_program *bp, *nbp;
  378:     struct bpf_insn *newcode, *oldcode;
  379:     int newcodelen;
  380: #endif /* PPP_FILTER */
  381: #ifdef	PPP_COMPRESS
  382:     u_char ccp_option[CCP_MAX_OPTION_LENGTH];
  383: #endif
  384: 
  385:     switch (cmd) {
  386:     case FIONREAD:
  387: 	*(int *)data = sc->sc_inq.ifq_len;
  388: 	break;
  389: 
  390:     case PPPIOCGUNIT:
  391: 	*(int *)data = sc->sc_if.if_dunit;
  392: 	break;
  393: 
  394:     case PPPIOCGFLAGS:
  395: 	*(u_int *)data = sc->sc_flags;
  396: 	break;
  397: 
  398:     case PPPIOCSFLAGS:
  399: 	if ((error = suser(td)) != 0)
  400: 	    return (error);
  401: 	flags = *(int *)data & SC_MASK;
  402: 	s = splsoftnet();
  403: #ifdef PPP_COMPRESS
  404: 	if (sc->sc_flags & SC_CCP_OPEN && !(flags & SC_CCP_OPEN))
  405: 	    ppp_ccp_closed(sc);
  406: #endif
  407: 	splimp();
  408: 	sc->sc_flags = (sc->sc_flags & ~SC_MASK) | flags;
  409: 	splx(s);
  410: 	break;
  411: 
  412:     case PPPIOCSMRU:
  413: 	if ((error = suser(td)) != 0)
  414: 	    return (error);
  415: 	mru = *(int *)data;
  416: 	if (mru >= PPP_MRU && mru <= PPP_MAXMRU)
  417: 	    sc->sc_mru = mru;
  418: 	break;
  419: 
  420:     case PPPIOCGMRU:
  421: 	*(int *)data = sc->sc_mru;
  422: 	break;
  423: 
  424: #ifdef VJC
  425:     case PPPIOCSMAXCID:
  426: 	if ((error = suser(td)) != 0)
  427: 	    return (error);
  428: 	if (sc->sc_comp) {
  429: 	    s = splsoftnet();
  430: 	    sl_compress_init(sc->sc_comp, *(int *)data);
  431: 	    splx(s);
  432: 	}
  433: 	break;
  434: #endif
  435: 
  436:     case PPPIOCXFERUNIT:
  437: 	if ((error = suser(td)) != 0)
  438: 	    return (error);
  439: 	sc->sc_xfer = td;
  440: 	break;
  441: 
  442: #ifdef PPP_COMPRESS
  443:     case PPPIOCSCOMPRESS:
  444: 	if ((error = suser(td)) != 0)
  445: 	    return (error);
  446: 	odp = (struct ppp_option_data *) data;
  447: 	nb = odp->length;
  448: 	if (nb > sizeof(ccp_option))
  449: 	    nb = sizeof(ccp_option);
  450: 	if ((error = copyin(odp->ptr, ccp_option, nb)) != 0)
  451: 	    return (error);
  452: 	if (ccp_option[1] < 2)	/* preliminary check on the length byte */
  453: 	    return (EINVAL);
  454: 	for (cp = ppp_compressors; *cp != NULL; ++cp)
  455: 	    if ((*cp)->compress_proto == ccp_option[0]) {
  456: 		/*
  457: 		 * Found a handler for the protocol - try to allocate
  458: 		 * a compressor or decompressor.
  459: 		 */
  460: 		error = 0;
  461: 		if (odp->transmit) {
  462: 		    s = splsoftnet();
  463: 		    if (sc->sc_xc_state != NULL)
  464: 			(*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
  465: 		    sc->sc_xcomp = *cp;
  466: 		    sc->sc_xc_state = (*cp)->comp_alloc(ccp_option, nb);
  467: 		    if (sc->sc_xc_state == NULL) {
  468: 			if (sc->sc_flags & SC_DEBUG)
  469: 			    printf("%s: comp_alloc failed\n",
  470: 			       sc->sc_if.if_xname);
  471: 			error = ENOBUFS;
  472: 		    }
  473: 		    splimp();
  474: 		    sc->sc_flags &= ~SC_COMP_RUN;
  475: 		    splx(s);
  476: 		} else {
  477: 		    s = splsoftnet();
  478: 		    if (sc->sc_rc_state != NULL)
  479: 			(*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
  480: 		    sc->sc_rcomp = *cp;
  481: 		    sc->sc_rc_state = (*cp)->decomp_alloc(ccp_option, nb);
  482: 		    if (sc->sc_rc_state == NULL) {
  483: 			if (sc->sc_flags & SC_DEBUG)
  484: 			    printf("%s: decomp_alloc failed\n",
  485: 			       sc->sc_if.if_xname);
  486: 			error = ENOBUFS;
  487: 		    }
  488: 		    splimp();
  489: 		    sc->sc_flags &= ~SC_DECOMP_RUN;
  490: 		    splx(s);
  491: 		}
  492: 		return (error);
  493: 	    }
  494: 	if (sc->sc_flags & SC_DEBUG)
  495: 	    printf("%s: no compressor for [%x %x %x], %x\n",
  496: 		   sc->sc_if.if_xname, ccp_option[0], ccp_option[1],
  497: 		   ccp_option[2], nb);
  498: 	return (EINVAL);	/* no handler found */
  499: #endif /* PPP_COMPRESS */
  500: 
  501:     case PPPIOCGNPMODE:
  502:     case PPPIOCSNPMODE:
  503: 	npi = (struct npioctl *) data;
  504: 	switch (npi->protocol) {
  505: 	case PPP_IP:
  506: 	    npx = NP_IP;
  507: 	    break;
  508: 	default:
  509: 	    return EINVAL;
  510: 	}
  511: 	if (cmd == PPPIOCGNPMODE) {
  512: 	    npi->mode = sc->sc_npmode[npx];
  513: 	} else {
  514: 	    if ((error = suser(td)) != 0)
  515: 		return (error);
  516: 	    if (npi->mode != sc->sc_npmode[npx]) {
  517: 		s = splsoftnet();
  518: 		sc->sc_npmode[npx] = npi->mode;
  519: 		if (npi->mode != NPMODE_QUEUE) {
  520: 		    ppp_requeue(sc);
  521: 		    (*sc->sc_start)(sc);
  522: 		}
  523: 		splx(s);
  524: 	    }
  525: 	}
  526: 	break;
  527: 
  528:     case PPPIOCGIDLE:
  529: 	s = splsoftnet();
  530: 	t = time_second;
  531: 	((struct ppp_idle *)data)->xmit_idle = t - sc->sc_last_sent;
  532: 	((struct ppp_idle *)data)->recv_idle = t - sc->sc_last_recv;
  533: 	splx(s);
  534: 	break;
  535: 
  536: #ifdef PPP_FILTER
  537:     case PPPIOCSPASS:
  538:     case PPPIOCSACTIVE:
  539: 	nbp = (struct bpf_program *) data;
  540: 	if ((unsigned) nbp->bf_len > BPF_MAXINSNS)
  541: 	    return EINVAL;
  542: 	newcodelen = nbp->bf_len * sizeof(struct bpf_insn);
  543: 	if (newcodelen != 0) {
  544: 	    MALLOC(newcode, struct bpf_insn *, newcodelen, M_DEVBUF, M_WAITOK);
  545: 	    if (newcode == 0) {
  546: 		return EINVAL;		/* or sumpin */
  547: 	    }
  548: 	    if ((error = copyin((caddr_t)nbp->bf_insns, (caddr_t)newcode,
  549: 			       newcodelen)) != 0) {
  550: 		free(newcode, M_DEVBUF);
  551: 		return error;
  552: 	    }
  553: 	    if (!bpf_validate(newcode, nbp->bf_len)) {
  554: 		free(newcode, M_DEVBUF);
  555: 		return EINVAL;
  556: 	    }
  557: 	} else
  558: 	    newcode = 0;
  559: 	bp = (cmd == PPPIOCSPASS)? &sc->sc_pass_filt: &sc->sc_active_filt;
  560: 	oldcode = bp->bf_insns;
  561: 	s = splimp();
  562: 	bp->bf_len = nbp->bf_len;
  563: 	bp->bf_insns = newcode;
  564: 	splx(s);
  565: 	if (oldcode != 0)
  566: 	    free(oldcode, M_DEVBUF);
  567: 	break;
  568: #endif
  569: 
  570:     default:
  571: 	return (ENOIOCTL);
  572:     }
  573:     return (0);
  574: }
  575: 
  576: /*
  577:  * Process an ioctl request to the ppp network interface.
  578:  */
  579: static int
  580: pppsioctl(ifp, cmd, data, cr)
  581:     struct ifnet *ifp;
  582:     u_long cmd;
  583:     caddr_t data;
  584:     struct ucred *cr;
  585: {
  586:     struct thread *td = curthread;	/* XXX */
  587:     struct ppp_softc *sc = &ppp_softc[ifp->if_dunit];
  588:     struct ifaddr *ifa = (struct ifaddr *)data;
  589:     struct ifreq *ifr = (struct ifreq *)data;
  590:     struct ppp_stats *psp;
  591: #ifdef	PPP_COMPRESS
  592:     struct ppp_comp_stats *pcp;
  593: #endif
  594:     int s = splimp(), error = 0;
  595: 
  596:     switch (cmd) {
  597:     case SIOCSIFFLAGS:
  598: 	if ((ifp->if_flags & IFF_RUNNING) == 0)
  599: 	    ifp->if_flags &= ~IFF_UP;
  600: 	break;
  601: 
  602:     case SIOCSIFADDR:
  603:     case SIOCAIFADDR:
  604: 	switch(ifa->ifa_addr->sa_family) {
  605: #ifdef INET
  606: 	case AF_INET:
  607: 	    break;
  608: #endif
  609: #ifdef IPX
  610: 	case AF_IPX:
  611: 	    break;
  612: #endif
  613: 	default:
  614: 	    error = EAFNOSUPPORT;
  615: 	    break;
  616: 	}
  617: 	break;
  618: 
  619:     case SIOCSIFDSTADDR:
  620: 	switch(ifa->ifa_addr->sa_family) {
  621: #ifdef INET
  622: 	case AF_INET:
  623: 	    break;
  624: #endif
  625: #ifdef IPX
  626: 	case AF_IPX:
  627: 	    break;
  628: #endif
  629: 	default:
  630: 	    error = EAFNOSUPPORT;
  631: 	    break;
  632: 	}
  633: 	break;
  634: 
  635:     case SIOCSIFMTU:
  636: 	if ((error = suser(td)) != 0)
  637: 	    break;
  638: 	if (ifr->ifr_mtu > PPP_MAXMTU)
  639: 	    error = EINVAL;
  640: 	else {
  641: 	    sc->sc_if.if_mtu = ifr->ifr_mtu;
  642: 	    if (sc->sc_setmtu)
  643: 		    (*sc->sc_setmtu)(sc);
  644: 	}
  645: 	break;
  646: 
  647:     case SIOCGIFMTU:
  648: 	ifr->ifr_mtu = sc->sc_if.if_mtu;
  649: 	break;
  650: 
  651:     case SIOCADDMULTI:
  652:     case SIOCDELMULTI:
  653: 	if (ifr == 0) {
  654: 	    error = EAFNOSUPPORT;
  655: 	    break;
  656: 	}
  657: 	switch(ifr->ifr_addr.sa_family) {
  658: #ifdef INET
  659: 	case AF_INET:
  660: 	    break;
  661: #endif
  662: 	default:
  663: 	    error = EAFNOSUPPORT;
  664: 	    break;
  665: 	}
  666: 	break;
  667: 
  668:     case SIOCGPPPSTATS:
  669: 	psp = &((struct ifpppstatsreq *) data)->stats;
  670: 	bzero(psp, sizeof(*psp));
  671: 	psp->p = sc->sc_stats;
  672: #if defined(VJC) && !defined(SL_NO_STATS)
  673: 	if (sc->sc_comp) {
  674: 	    psp->vj.vjs_packets = sc->sc_comp->sls_packets;
  675: 	    psp->vj.vjs_compressed = sc->sc_comp->sls_compressed;
  676: 	    psp->vj.vjs_searches = sc->sc_comp->sls_searches;
  677: 	    psp->vj.vjs_misses = sc->sc_comp->sls_misses;
  678: 	    psp->vj.vjs_uncompressedin = sc->sc_comp->sls_uncompressedin;
  679: 	    psp->vj.vjs_compressedin = sc->sc_comp->sls_compressedin;
  680: 	    psp->vj.vjs_errorin = sc->sc_comp->sls_errorin;
  681: 	    psp->vj.vjs_tossed = sc->sc_comp->sls_tossed;
  682: 	}
  683: #endif /* VJC */
  684: 	break;
  685: 
  686: #ifdef PPP_COMPRESS
  687:     case SIOCGPPPCSTATS:
  688: 	pcp = &((struct ifpppcstatsreq *) data)->stats;
  689: 	bzero(pcp, sizeof(*pcp));
  690: 	if (sc->sc_xc_state != NULL)
  691: 	    (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c);
  692: 	if (sc->sc_rc_state != NULL)
  693: 	    (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d);
  694: 	break;
  695: #endif /* PPP_COMPRESS */
  696: 
  697:     default:
  698: 	error = ENOTTY;
  699:     }
  700:     splx(s);
  701:     return (error);
  702: }
  703: 
  704: /*
  705:  * Queue a packet.  Start transmission if not active.
  706:  * Packet is placed in Information field of PPP frame.
  707:  * Called at splnet as the if->if_output handler.
  708:  * Called at splnet from pppwrite().
  709:  */
  710: int
  711: pppoutput(ifp, m0, dst, rtp)
  712:     struct ifnet *ifp;
  713:     struct mbuf *m0;
  714:     struct sockaddr *dst;
  715:     struct rtentry *rtp;
  716: {
  717:     struct ppp_softc *sc = &ppp_softc[ifp->if_dunit];
  718:     int protocol, address, control;
  719:     u_char *cp;
  720:     int s, error;
  721:     struct ip *ip;
  722:     struct ifqueue *ifq;
  723:     enum NPmode mode;
  724:     int len;
  725:     struct mbuf *m;
  726: 
  727:     if (sc->sc_devp == NULL || (ifp->if_flags & IFF_RUNNING) == 0
  728: 	|| ((ifp->if_flags & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC)) {
  729: 	error = ENETDOWN;	/* sort of */
  730: 	goto bad;
  731:     }
  732: 
  733:     /*
  734:      * Compute PPP header.
  735:      */
  736:     m0->m_flags &= ~M_HIGHPRI;
  737:     switch (dst->sa_family) {
  738: #ifdef INET
  739:     case AF_INET:
  740: 	address = PPP_ALLSTATIONS;
  741: 	control = PPP_UI;
  742: 	protocol = PPP_IP;
  743: 	mode = sc->sc_npmode[NP_IP];
  744: 
  745: 	/*
  746: 	 * If this packet has the "low delay" bit set in the IP header,
  747: 	 * put it on the fastq instead.
  748: 	 */
  749: 	ip = mtod(m0, struct ip *);
  750: 	if (ip->ip_tos & IPTOS_LOWDELAY)
  751: 	    m0->m_flags |= M_HIGHPRI;
  752: 	break;
  753: #endif
  754: #ifdef IPX
  755:     case AF_IPX:
  756: 	/*
  757: 	 * This is pretty bogus.. We dont have an ipxcp module in pppd
  758: 	 * yet to configure the link parameters.  Sigh. I guess a
  759: 	 * manual ifconfig would do....  -Peter
  760: 	 */
  761: 	address = PPP_ALLSTATIONS;
  762: 	control = PPP_UI;
  763: 	protocol = PPP_IPX;
  764: 	mode = NPMODE_PASS;
  765: 	break;
  766: #endif
  767:     case AF_UNSPEC:
  768: 	address = PPP_ADDRESS(dst->sa_data);
  769: 	control = PPP_CONTROL(dst->sa_data);
  770: 	protocol = PPP_PROTOCOL(dst->sa_data);
  771: 	mode = NPMODE_PASS;
  772: 	break;
  773:     default:
  774: 	printf("%s: af%d not supported\n", ifp->if_xname, dst->sa_family);
  775: 	error = EAFNOSUPPORT;
  776: 	goto bad;
  777:     }
  778: 
  779:     /*
  780:      * Drop this packet, or return an error, if necessary.
  781:      */
  782:     if (mode == NPMODE_ERROR) {
  783: 	error = ENETDOWN;
  784: 	goto bad;
  785:     }
  786:     if (mode == NPMODE_DROP) {
  787: 	error = 0;
  788: 	goto bad;
  789:     }
  790: 
  791:     /*
  792:      * Add PPP header.  If no space in first mbuf, allocate another.
  793:      * (This assumes M_LEADINGSPACE is always 0 for a cluster mbuf.)
  794:      */
  795:     if (M_LEADINGSPACE(m0) < PPP_HDRLEN) {
  796: 	m0 = m_prepend(m0, PPP_HDRLEN, M_DONTWAIT);
  797: 	if (m0 == 0) {
  798: 	    error = ENOBUFS;
  799: 	    goto bad;
  800: 	}
  801: 	m0->m_len = 0;
  802:     } else
  803: 	m0->m_data -= PPP_HDRLEN;
  804: 
  805:     cp = mtod(m0, u_char *);
  806:     *cp++ = address;
  807:     *cp++ = control;
  808:     *cp++ = protocol >> 8;
  809:     *cp++ = protocol & 0xff;
  810:     m0->m_len += PPP_HDRLEN;
  811: 
  812:     len = 0;
  813:     for (m = m0; m != 0; m = m->m_next)
  814: 	len += m->m_len;
  815: 
  816:     if (sc->sc_flags & SC_LOG_OUTPKT) {
  817: 	printf("%s output: ", ifp->if_xname);
  818: 	pppdumpm(m0);
  819:     }
  820: 
  821:     if ((protocol & 0x8000) == 0) {
  822: #ifdef PPP_FILTER
  823: 	/*
  824: 	 * Apply the pass and active filters to the packet,
  825: 	 * but only if it is a data packet.
  826: 	 */
  827: 	*mtod(m0, u_char *) = 1;	/* indicates outbound */
  828: 	if (sc->sc_pass_filt.bf_insns != 0
  829: 	    && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m0,
  830: 			  len, 0) == 0) {
  831: 	    error = 0;		/* drop this packet */
  832: 	    goto bad;
  833: 	}
  834: 
  835: 	/*
  836: 	 * Update the time we sent the most recent packet.
  837: 	 */
  838: 	if (sc->sc_active_filt.bf_insns == 0
  839: 	    || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m0, len, 0))
  840: 	    sc->sc_last_sent = time_second;
  841: 
  842: 	*mtod(m0, u_char *) = address;
  843: #else
  844: 	/*
  845: 	 * Update the time we sent the most recent data packet.
  846: 	 */
  847: 	sc->sc_last_sent = time_second;
  848: #endif /* PPP_FILTER */
  849:     }
  850: 
  851:     /*
  852:      * See if bpf wants to look at the packet.
  853:      */
  854:     if (ifp->if_bpf)
  855: 	bpf_mtap(ifp, m0);
  856: 
  857:     /*
  858:      * Put the packet on the appropriate queue.
  859:      */
  860:     s = splsoftnet();	/* redundant */
  861:     if (mode == NPMODE_QUEUE) {
  862: 	/* XXX we should limit the number of packets on this queue */
  863: 	*sc->sc_npqtail = m0;
  864: 	m0->m_nextpkt = NULL;
  865: 	sc->sc_npqtail = &m0->m_nextpkt;
  866:     } else {
  867: 	/* fastq and if_snd are emptied at spl[soft]net now */
  868: 	ifq = (m0->m_flags & M_HIGHPRI)? &sc->sc_fastq: &ifp->if_snd;
  869: 	if (IF_QFULL(ifq) && dst->sa_family != AF_UNSPEC) {
  870: 	    IF_DROP(ifq);
  871: 	    splx(s);
  872: 	    sc->sc_if.if_oerrors++;
  873: 	    sc->sc_stats.ppp_oerrors++;
  874: 	    error = ENOBUFS;
  875: 	    goto bad;
  876: 	}
  877: 	IF_ENQUEUE(ifq, m0);
  878: 	(*sc->sc_start)(sc);
  879:     }
  880:     getmicrotime(&ifp->if_lastchange);
  881:     ifp->if_opackets++;
  882:     ifp->if_obytes += len;
  883: 
  884:     splx(s);
  885:     return (0);
  886: 
  887: bad:
  888:     m_freem(m0);
  889:     return (error);
  890: }
  891: 
  892: /*
  893:  * After a change in the NPmode for some NP, move packets from the
  894:  * npqueue to the send queue or the fast queue as appropriate.
  895:  * Should be called at spl[soft]net.
  896:  */
  897: static void
  898: ppp_requeue(sc)
  899:     struct ppp_softc *sc;
  900: {
  901:     struct mbuf *m, **mpp;
  902:     struct ifqueue *ifq;
  903:     enum NPmode mode;
  904: 
  905:     for (mpp = &sc->sc_npqueue; (m = *mpp) != NULL; ) {
  906: 	switch (PPP_PROTOCOL(mtod(m, u_char *))) {
  907: 	case PPP_IP:
  908: 	    mode = sc->sc_npmode[NP_IP];
  909: 	    break;
  910: 	default:
  911: 	    mode = NPMODE_PASS;
  912: 	}
  913: 
  914: 	switch (mode) {
  915: 	case NPMODE_PASS:
  916: 	    /*
  917: 	     * This packet can now go on one of the queues to be sent.
  918: 	     */
  919: 	    *mpp = m->m_nextpkt;
  920: 	    m->m_nextpkt = NULL;
  921: 	    ifq = (m->m_flags & M_HIGHPRI)? &sc->sc_fastq: &sc->sc_if.if_snd;
  922: 	    if (IF_QFULL(ifq)) {
  923: 		IF_DROP(ifq);
  924: 		sc->sc_if.if_oerrors++;
  925: 		sc->sc_stats.ppp_oerrors++;
  926: 	    } else
  927: 		IF_ENQUEUE(ifq, m);
  928: 	    break;
  929: 
  930: 	case NPMODE_DROP:
  931: 	case NPMODE_ERROR:
  932: 	    *mpp = m->m_nextpkt;
  933: 	    m_freem(m);
  934: 	    break;
  935: 
  936: 	case NPMODE_QUEUE:
  937: 	    mpp = &m->m_nextpkt;
  938: 	    break;
  939: 	}
  940:     }
  941:     sc->sc_npqtail = mpp;
  942: }
  943: 
  944: /*
  945:  * Transmitter has finished outputting some stuff;
  946:  * remember to call sc->sc_start later at splsoftnet.
  947:  */
  948: void
  949: ppp_restart(sc)
  950:     struct ppp_softc *sc;
  951: {
  952:     int s = splimp();
  953: 
  954:     sc->sc_flags &= ~SC_TBUSY;
  955:     schednetisr(NETISR_PPP);
  956:     splx(s);
  957: }
  958: 
  959: 
  960: /*
  961:  * Get a packet to send.  This procedure is intended to be called at
  962:  * splsoftnet, since it may involve time-consuming operations such as
  963:  * applying VJ compression, packet compression, address/control and/or
  964:  * protocol field compression to the packet.
  965:  */
  966: struct mbuf *
  967: ppp_dequeue(sc)
  968:     struct ppp_softc *sc;
  969: {
  970:     struct mbuf *m, *mp;
  971:     u_char *cp;
  972:     int address, control, protocol;
  973: 
  974:     /*
  975:      * Grab a packet to send: first try the fast queue, then the
  976:      * normal queue.
  977:      */
  978:     IF_DEQUEUE(&sc->sc_fastq, m);
  979:     if (m == NULL)
  980: 	IF_DEQUEUE(&sc->sc_if.if_snd, m);
  981:     if (m == NULL)
  982: 	return NULL;
  983: 
  984:     ++sc->sc_stats.ppp_opackets;
  985: 
  986:     /*
  987:      * Extract the ppp header of the new packet.
  988:      * The ppp header will be in one mbuf.
  989:      */
  990:     cp = mtod(m, u_char *);
  991:     address = PPP_ADDRESS(cp);
  992:     control = PPP_CONTROL(cp);
  993:     protocol = PPP_PROTOCOL(cp);
  994: 
  995:     switch (protocol) {
  996:     case PPP_IP:
  997: #ifdef VJC
  998: 	/*
  999: 	 * If the packet is a TCP/IP packet, see if we can compress it.
 1000: 	 */
 1001: 	if ((sc->sc_flags & SC_COMP_TCP) && sc->sc_comp != NULL) {
 1002: 	    struct ip *ip;
 1003: 	    int type;
 1004: 
 1005: 	    mp = m;
 1006: 	    ip = (struct ip *) (cp + PPP_HDRLEN);
 1007: 	    if (mp->m_len <= PPP_HDRLEN) {
 1008: 		mp = mp->m_next;
 1009: 		if (mp == NULL)
 1010: 		    break;
 1011: 		ip = mtod(mp, struct ip *);
 1012: 	    }
 1013: 	    /* this code assumes the IP/TCP header is in one non-shared mbuf */
 1014: 	    if (ip->ip_p == IPPROTO_TCP) {
 1015: 		type = sl_compress_tcp(mp, ip, sc->sc_comp,
 1016: 				       !(sc->sc_flags & SC_NO_TCP_CCID));
 1017: 		switch (type) {
 1018: 		case TYPE_UNCOMPRESSED_TCP:
 1019: 		    protocol = PPP_VJC_UNCOMP;
 1020: 		    break;
 1021: 		case TYPE_COMPRESSED_TCP:
 1022: 		    protocol = PPP_VJC_COMP;
 1023: 		    cp = mtod(m, u_char *);
 1024: 		    cp[0] = address;	/* header has moved */
 1025: 		    cp[1] = control;
 1026: 		    cp[2] = 0;
 1027: 		    break;
 1028: 		}
 1029: 		cp[3] = protocol;	/* update protocol in PPP header */
 1030: 	    }
 1031: 	}
 1032: #endif	/* VJC */
 1033: 	break;
 1034: 
 1035: #ifdef PPP_COMPRESS
 1036:     case PPP_CCP:
 1037: 	ppp_ccp(sc, m, 0);
 1038: 	break;
 1039: #endif	/* PPP_COMPRESS */
 1040:     }
 1041: 
 1042: #ifdef PPP_COMPRESS
 1043:     if (protocol != PPP_LCP && protocol != PPP_CCP
 1044: 	&& sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
 1045: 	struct mbuf *mcomp = NULL;
 1046: 	int slen, clen;
 1047: 
 1048: 	slen = 0;
 1049: 	for (mp = m; mp != NULL; mp = mp->m_next)
 1050: 	    slen += mp->m_len;
 1051: 	clen = (*sc->sc_xcomp->compress)
 1052: 	    (sc->sc_xc_state, &mcomp, m, slen, sc->sc_if.if_mtu + PPP_HDRLEN);
 1053: 	if (mcomp != NULL) {
 1054: 	    if (sc->sc_flags & SC_CCP_UP) {
 1055: 		/* Send the compressed packet instead of the original. */
 1056: 		m_freem(m);
 1057: 		m = mcomp;
 1058: 		cp = mtod(m, u_char *);
 1059: 		protocol = cp[3];
 1060: 	    } else {
 1061: 		/* Can't transmit compressed packets until CCP is up. */
 1062: 		m_freem(mcomp);
 1063: 	    }
 1064: 	}
 1065:     }
 1066: #endif	/* PPP_COMPRESS */
 1067: 
 1068:     /*
 1069:      * Compress the address/control and protocol, if possible.
 1070:      */
 1071:     if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
 1072: 	control == PPP_UI && protocol != PPP_ALLSTATIONS &&
 1073: 	protocol != PPP_LCP) {
 1074: 	/* can compress address/control */
 1075: 	m->m_data += 2;
 1076: 	m->m_len -= 2;
 1077:     }
 1078:     if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
 1079: 	/* can compress protocol */
 1080: 	if (mtod(m, u_char *) == cp) {
 1081: 	    cp[2] = cp[1];	/* move address/control up */
 1082: 	    cp[1] = cp[0];
 1083: 	}
 1084: 	++m->m_data;
 1085: 	--m->m_len;
 1086:     }
 1087: 
 1088:     return m;
 1089: }
 1090: 
 1091: #ifdef PPP_COMPRESS
 1092: /*
 1093:  * Handle a CCP packet.  `rcvd' is 1 if the packet was received,
 1094:  * 0 if it is about to be transmitted.
 1095:  */
 1096: static void
 1097: ppp_ccp(sc, m, rcvd)
 1098:     struct ppp_softc *sc;
 1099:     struct mbuf *m;
 1100:     int rcvd;
 1101: {
 1102:     u_char *dp, *ep;
 1103:     struct mbuf *mp;
 1104:     int slen, s;
 1105: 
 1106:     /*
 1107:      * Get a pointer to the data after the PPP header.
 1108:      */
 1109:     if (m->m_len <= PPP_HDRLEN) {
 1110: 	mp = m->m_next;
 1111: 	if (mp == NULL)
 1112: 	    return;
 1113: 	dp = (mp != NULL)? mtod(mp, u_char *): NULL;
 1114:     } else {
 1115: 	mp = m;
 1116: 	dp = mtod(mp, u_char *) + PPP_HDRLEN;
 1117:     }
 1118: 
 1119:     ep = mtod(mp, u_char *) + mp->m_len;
 1120:     if (dp + CCP_HDRLEN > ep)
 1121: 	return;
 1122:     slen = CCP_LENGTH(dp);
 1123:     if (dp + slen > ep) {
 1124: 	if (sc->sc_flags & SC_DEBUG)
 1125: 	    printf("if_ppp/ccp: not enough data in mbuf (%p+%x > %p+%x)\n",
 1126: 		   dp, slen, mtod(mp, u_char *), mp->m_len);
 1127: 	return;
 1128:     }
 1129: 
 1130:     switch (CCP_CODE(dp)) {
 1131:     case CCP_CONFREQ:
 1132:     case CCP_TERMREQ:
 1133:     case CCP_TERMACK:
 1134: 	/* CCP must be going down - disable compression */
 1135: 	if (sc->sc_flags & SC_CCP_UP) {
 1136: 	    s = splimp();
 1137: 	    sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
 1138: 	    splx(s);
 1139: 	}
 1140: 	break;
 1141: 
 1142:     case CCP_CONFACK:
 1143: 	if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
 1144: 	    && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
 1145: 	    && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
 1146: 	    if (!rcvd) {
 1147: 		/* we're agreeing to send compressed packets. */
 1148: 		if (sc->sc_xc_state != NULL
 1149: 		    && (*sc->sc_xcomp->comp_init)
 1150: 			(sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
 1151: 			 sc->sc_if.if_dunit, 0, sc->sc_flags & SC_DEBUG)) {
 1152: 		    s = splimp();
 1153: 		    sc->sc_flags |= SC_COMP_RUN;
 1154: 		    splx(s);
 1155: 		}
 1156: 	    } else {
 1157: 		/* peer is agreeing to send compressed packets. */
 1158: 		if (sc->sc_rc_state != NULL
 1159: 		    && (*sc->sc_rcomp->decomp_init)
 1160: 			(sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
 1161: 			 sc->sc_if.if_dunit, 0, sc->sc_mru,
 1162: 			 sc->sc_flags & SC_DEBUG)) {
 1163: 		    s = splimp();
 1164: 		    sc->sc_flags |= SC_DECOMP_RUN;
 1165: 		    sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
 1166: 		    splx(s);
 1167: 		}
 1168: 	    }
 1169: 	}
 1170: 	break;
 1171: 
 1172:     case CCP_RESETACK:
 1173: 	if (sc->sc_flags & SC_CCP_UP) {
 1174: 	    if (!rcvd) {
 1175: 		if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN))
 1176: 		    (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
 1177: 	    } else {
 1178: 		if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
 1179: 		    (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
 1180: 		    s = splimp();
 1181: 		    sc->sc_flags &= ~SC_DC_ERROR;
 1182: 		    splx(s);
 1183: 		}
 1184: 	    }
 1185: 	}
 1186: 	break;
 1187:     }
 1188: }
 1189: 
 1190: /*
 1191:  * CCP is down; free (de)compressor state if necessary.
 1192:  */
 1193: static void
 1194: ppp_ccp_closed(sc)
 1195:     struct ppp_softc *sc;
 1196: {
 1197:     if (sc->sc_xc_state) {
 1198: 	(*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
 1199: 	sc->sc_xc_state = NULL;
 1200:     }
 1201:     if (sc->sc_rc_state) {
 1202: 	(*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
 1203: 	sc->sc_rc_state = NULL;
 1204:     }
 1205: }
 1206: #endif /* PPP_COMPRESS */
 1207: 
 1208: /*
 1209:  * PPP packet input routine.
 1210:  * The caller has checked and removed the FCS and has inserted
 1211:  * the address/control bytes and the protocol high byte if they
 1212:  * were omitted.
 1213:  */
 1214: void
 1215: ppppktin(sc, m, lost)
 1216:     struct ppp_softc *sc;
 1217:     struct mbuf *m;
 1218:     int lost;
 1219: {
 1220:     int s = splimp();
 1221: 
 1222:     if (lost)
 1223: 	m->m_flags |= M_ERRMARK;
 1224:     IF_ENQUEUE(&sc->sc_rawq, m);
 1225:     schednetisr(NETISR_PPP);
 1226:     splx(s);
 1227: }
 1228: 
 1229: /*
 1230:  * Process a received PPP packet, doing decompression as necessary.
 1231:  * Should be called at splsoftnet.
 1232:  */
 1233: #define COMPTYPE(proto)	((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
 1234: 			 TYPE_UNCOMPRESSED_TCP)
 1235: 
 1236: static void
 1237: ppp_inproc(sc, m)
 1238:     struct ppp_softc *sc;
 1239:     struct mbuf *m;
 1240: {
 1241:     struct ifnet *ifp = &sc->sc_if;
 1242:     int isr;
 1243:     int s, ilen = 0, xlen, proto, rv;
 1244:     u_char *cp, adrs, ctrl;
 1245:     struct mbuf *mp, *dmp = NULL;
 1246:     u_char *iphdr;
 1247:     u_int hlen;
 1248: 
 1249:     sc->sc_stats.ppp_ipackets++;
 1250: 
 1251:     if (sc->sc_flags & SC_LOG_INPKT) {
 1252: 	ilen = 0;
 1253: 	for (mp = m; mp != NULL; mp = mp->m_next)
 1254: 	    ilen += mp->m_len;
 1255: 	printf("%s: got %d bytes\n", ifp->if_xname, ilen);
 1256: 	pppdumpm(m);
 1257:     }
 1258: 
 1259:     cp = mtod(m, u_char *);
 1260:     adrs = PPP_ADDRESS(cp);
 1261:     ctrl = PPP_CONTROL(cp);
 1262:     proto = PPP_PROTOCOL(cp);
 1263: 
 1264:     if (m->m_flags & M_ERRMARK) {
 1265: 	m->m_flags &= ~M_ERRMARK;
 1266: 	s = splimp();
 1267: 	sc->sc_flags |= SC_VJ_RESET;
 1268: 	splx(s);
 1269:     }
 1270: 
 1271: #ifdef PPP_COMPRESS
 1272:     /*
 1273:      * Decompress this packet if necessary, update the receiver's
 1274:      * dictionary, or take appropriate action on a CCP packet.
 1275:      */
 1276:     if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
 1277: 	&& !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
 1278: 	/* decompress this packet */
 1279: 	rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
 1280: 	if (rv == DECOMP_OK) {
 1281: 	    m_freem(m);
 1282: 	    if (dmp == NULL) {
 1283: 		/* no error, but no decompressed packet produced */
 1284: 		return;
 1285: 	    }
 1286: 	    m = dmp;
 1287: 	    cp = mtod(m, u_char *);
 1288: 	    proto = PPP_PROTOCOL(cp);
 1289: 
 1290: 	} else {
 1291: 	    /*
 1292: 	     * An error has occurred in decompression.
 1293: 	     * Pass the compressed packet up to pppd, which may take
 1294: 	     * CCP down or issue a Reset-Req.
 1295: 	     */
 1296: 	    if (sc->sc_flags & SC_DEBUG)
 1297: 		printf("%s: decompress failed %d\n", ifp->if_xname, rv);
 1298: 	    s = splimp();
 1299: 	    sc->sc_flags |= SC_VJ_RESET;
 1300: 	    if (rv == DECOMP_ERROR)
 1301: 		sc->sc_flags |= SC_DC_ERROR;
 1302: 	    else
 1303: 		sc->sc_flags |= SC_DC_FERROR;
 1304: 	    splx(s);
 1305: 	}
 1306: 
 1307:     } else {
 1308: 	if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
 1309: 	    (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
 1310: 	}
 1311: 	if (proto == PPP_CCP) {
 1312: 	    ppp_ccp(sc, m, 1);
 1313: 	}
 1314:     }
 1315: #endif
 1316: 
 1317:     ilen = 0;
 1318:     for (mp = m; mp != NULL; mp = mp->m_next)
 1319: 	ilen += mp->m_len;
 1320: 
 1321: #ifdef VJC
 1322:     if (sc->sc_flags & SC_VJ_RESET) {
 1323: 	/*
 1324: 	 * If we've missed a packet, we must toss subsequent compressed
 1325: 	 * packets which don't have an explicit connection ID.
 1326: 	 */
 1327: 	if (sc->sc_comp)
 1328: 	    sl_uncompress_tcp(NULL, 0, TYPE_ERROR, sc->sc_comp);
 1329: 	s = splimp();
 1330: 	sc->sc_flags &= ~SC_VJ_RESET;
 1331: 	splx(s);
 1332:     }
 1333: 
 1334:     /*
 1335:      * See if we have a VJ-compressed packet to uncompress.
 1336:      */
 1337:     if (proto == PPP_VJC_COMP) {
 1338: 	if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
 1339: 	    goto bad;
 1340: 
 1341: 	xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
 1342: 				      ilen - PPP_HDRLEN, TYPE_COMPRESSED_TCP,
 1343: 				      sc->sc_comp, &iphdr, &hlen);
 1344: 
 1345: 	if (xlen <= 0) {
 1346: 	    if (sc->sc_flags & SC_DEBUG)
 1347: 		printf("%s: VJ uncompress failed on type comp\n",
 1348: 			ifp->if_xname);
 1349: 	    goto bad;
 1350: 	}
 1351: 
 1352: 	/* Copy the PPP and IP headers into a new mbuf. */
 1353: 	MGETHDR(mp, M_DONTWAIT, MT_DATA);
 1354: 	if (mp == NULL)
 1355: 	    goto bad;
 1356: 	mp->m_len = 0;
 1357: 	mp->m_next = NULL;
 1358: 	if (hlen + PPP_HDRLEN > MHLEN) {
 1359: 	    MCLGET(mp, M_DONTWAIT);
 1360: 	    if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) {
 1361: 		m_freem(mp);
 1362: 		goto bad;	/* lose if big headers and no clusters */
 1363: 	    }
 1364: 	}
 1365: 	cp = mtod(mp, u_char *);
 1366: 	cp[0] = adrs;
 1367: 	cp[1] = ctrl;
 1368: 	cp[2] = 0;
 1369: 	cp[3] = PPP_IP;
 1370: 	proto = PPP_IP;
 1371: 	bcopy(iphdr, cp + PPP_HDRLEN, hlen);
 1372: 	mp->m_len = hlen + PPP_HDRLEN;
 1373: 
 1374: 	/*
 1375: 	 * Trim the PPP and VJ headers off the old mbuf
 1376: 	 * and stick the new and old mbufs together.
 1377: 	 */
 1378: 	m->m_data += PPP_HDRLEN + xlen;
 1379: 	m->m_len -= PPP_HDRLEN + xlen;
 1380: 	if (m->m_len <= M_TRAILINGSPACE(mp)) {
 1381: 	    bcopy(mtod(m, u_char *), mtod(mp, u_char *) + mp->m_len, m->m_len);
 1382: 	    mp->m_len += m->m_len;
 1383: 	    mp->m_next = m_free(m);
 1384: 	} else {
 1385: 	    mp->m_next = m;
 1386: 	}
 1387: 	m = mp;
 1388: 	ilen += hlen - xlen;
 1389: 
 1390:     } else if (proto == PPP_VJC_UNCOMP) {
 1391: 	if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
 1392: 	    goto bad;
 1393: 
 1394: 	xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
 1395: 				      ilen - PPP_HDRLEN, TYPE_UNCOMPRESSED_TCP,
 1396: 				      sc->sc_comp, &iphdr, &hlen);
 1397: 
 1398: 	if (xlen < 0) {
 1399: 	    if (sc->sc_flags & SC_DEBUG)
 1400: 		printf("%s: VJ uncompress failed on type uncomp\n",
 1401: 			ifp->if_xname);
 1402: 	    goto bad;
 1403: 	}
 1404: 
 1405: 	proto = PPP_IP;
 1406: 	cp[3] = PPP_IP;
 1407:     }
 1408: #endif /* VJC */
 1409: 
 1410:     /*
 1411:      * If the packet will fit in a header mbuf, don't waste a
 1412:      * whole cluster on it.
 1413:      */
 1414:     if (ilen <= MHLEN && M_IS_CLUSTER(m)) {
 1415: 	MGETHDR(mp, M_DONTWAIT, MT_DATA);
 1416: 	if (mp != NULL) {
 1417: 	    m_copydata(m, 0, ilen, mtod(mp, caddr_t));
 1418: 	    m_freem(m);
 1419: 	    m = mp;
 1420: 	    m->m_len = ilen;
 1421: 	}
 1422:     }
 1423:     m->m_pkthdr.len = ilen;
 1424:     m->m_pkthdr.rcvif = ifp;
 1425: 
 1426:     if ((proto & 0x8000) == 0) {
 1427: #ifdef PPP_FILTER
 1428: 	/*
 1429: 	 * See whether we want to pass this packet, and
 1430: 	 * if it counts as link activity.
 1431: 	 */
 1432: 	adrs = *mtod(m, u_char *);	/* save address field */
 1433: 	*mtod(m, u_char *) = 0;		/* indicate inbound */
 1434: 	if (sc->sc_pass_filt.bf_insns != 0
 1435: 	    && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m,
 1436: 			  ilen, 0) == 0) {
 1437: 	    /* drop this packet */
 1438: 	    m_freem(m);
 1439: 	    return;
 1440: 	}
 1441: 	if (sc->sc_active_filt.bf_insns == 0
 1442: 	    || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m, ilen, 0))
 1443: 	    sc->sc_last_recv = time_second;
 1444: 
 1445: 	*mtod(m, u_char *) = adrs;
 1446: #else
 1447: 	/*
 1448: 	 * Record the time that we received this packet.
 1449: 	 */
 1450: 	sc->sc_last_recv = time_second;
 1451: #endif /* PPP_FILTER */
 1452:     }
 1453: 
 1454:     /* See if bpf wants to look at the packet. */
 1455:     if (sc->sc_if.if_bpf)
 1456: 	bpf_mtap(&sc->sc_if, m);
 1457: 
 1458:     isr = -1;
 1459:     switch (proto) {
 1460: #ifdef INET
 1461:     case PPP_IP:
 1462: 	/*
 1463: 	 * IP packet - take off the ppp header and pass it up to IP.
 1464: 	 */
 1465: 	if ((ifp->if_flags & IFF_UP) == 0
 1466: 	    || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
 1467: 	    /* interface is down - drop the packet. */
 1468: 	    m_freem(m);
 1469: 	    return;
 1470: 	}
 1471: 	m->m_pkthdr.len -= PPP_HDRLEN;
 1472: 	m->m_data += PPP_HDRLEN;
 1473: 	m->m_len -= PPP_HDRLEN;
 1474: 	if (ipflow_fastforward(m))
 1475: 	    return;
 1476: 	isr = NETISR_IP;
 1477: 	break;
 1478: #endif
 1479: #ifdef IPX
 1480:     case PPP_IPX:
 1481: 	/*
 1482: 	 * IPX packet - take off the ppp header and pass it up to IPX.
 1483: 	 */
 1484: 	if ((sc->sc_if.if_flags & IFF_UP) == 0
 1485: 	    /* XXX: || sc->sc_npmode[NP_IPX] != NPMODE_PASS*/) {
 1486: 	    /* interface is down - drop the packet. */
 1487: 	    m_freem(m);
 1488: 	    return;
 1489: 	}
 1490: 	m->m_pkthdr.len -= PPP_HDRLEN;
 1491: 	m->m_data += PPP_HDRLEN;
 1492: 	m->m_len -= PPP_HDRLEN;
 1493: 	isr = NETISR_IPX;
 1494: 	sc->sc_last_recv = time_second;	/* update time of last pkt rcvd */
 1495: 	break;
 1496: #endif
 1497: 
 1498:     default:
 1499: 	/*
 1500: 	 * Some other protocol - place on input queue for read().
 1501: 	 */
 1502: 	break;
 1503:     }
 1504: 
 1505:     if (isr == -1)
 1506: 	rv = IF_HANDOFF(&sc->sc_inq, m, NULL);
 1507:     else
 1508: 	rv = (netisr_queue(isr, m) == 0);
 1509:     if (!rv) {
 1510: 	if (sc->sc_flags & SC_DEBUG)
 1511: 	    printf("%s: input queue full\n", ifp->if_xname);
 1512: 	ifp->if_iqdrops++;
 1513: 	goto bad;
 1514:     }
 1515: 
 1516:     ifp->if_ipackets++;
 1517:     ifp->if_ibytes += ilen;
 1518:     getmicrotime(&ifp->if_lastchange);
 1519: 
 1520:     if (rv)
 1521: 	(*sc->sc_ctlp)(sc);
 1522: 
 1523:     return;
 1524: 
 1525:  bad:
 1526:     m_freem(m);
 1527:     sc->sc_if.if_ierrors++;
 1528:     sc->sc_stats.ppp_ierrors++;
 1529: }
 1530: 
 1531: #define MAX_DUMP_BYTES	128
 1532: 
 1533: static void
 1534: pppdumpm(m0)
 1535:     struct mbuf *m0;
 1536: {
 1537:     char buf[3*MAX_DUMP_BYTES+4];
 1538:     char *bp = buf;
 1539:     struct mbuf *m;
 1540: 
 1541:     for (m = m0; m; m = m->m_next) {
 1542: 	int l = m->m_len;
 1543: 	u_char *rptr = (u_char *)m->m_data;
 1544: 
 1545: 	while (l--) {
 1546: 	    if (bp > buf + sizeof(buf) - 4)
 1547: 		goto done;
 1548: 	    *bp++ = hex2ascii(*rptr >> 4);
 1549: 	    *bp++ = hex2ascii(*rptr++ & 0xf);
 1550: 	}
 1551: 
 1552: 	if (m->m_next) {
 1553: 	    if (bp > buf + sizeof(buf) - 3)
 1554: 		goto done;
 1555: 	    *bp++ = '|';
 1556: 	} else
 1557: 	    *bp++ = ' ';
 1558:     }
 1559: done:
 1560:     if (m)
 1561: 	*bp++ = '>';
 1562:     *bp = 0;
 1563:     printf("%s\n", buf);
 1564: }