File:  [DragonFly] / src / sys / net / ppp / if_ppp.c
Revision 1.16: download - view: text, annotated - select for diffs
Wed Apr 21 18:13:52 2004 UTC (10 years, 3 months ago) by dillon
Branches: MAIN
CVS tags: HEAD
Fix a netmsg memory leak in the ARP code.  Adjust all ms_cmd function
dispatches to return a proper error code.

Reported-by: multiple people

    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.16 2004/04/21 18:13:52 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_NOWAIT);
  287:     if (sc->sc_comp)
  288: 	sl_compress_init(sc->sc_comp, -1);
  289: #endif
  290: #ifdef PPP_COMPRESS
  291:     sc->sc_xc_state = NULL;
  292:     sc->sc_rc_state = NULL;
  293: #endif /* PPP_COMPRESS */
  294:     for (i = 0; i < NUM_NP; ++i)
  295: 	sc->sc_npmode[i] = NPMODE_ERROR;
  296:     sc->sc_npqueue = NULL;
  297:     sc->sc_npqtail = &sc->sc_npqueue;
  298:     sc->sc_last_sent = sc->sc_last_recv = time_second;
  299: 
  300:     return sc;
  301: }
  302: 
  303: /*
  304:  * Deallocate a ppp unit.  Must be called at splsoftnet or higher.
  305:  */
  306: void
  307: pppdealloc(sc)
  308:     struct ppp_softc *sc;
  309: {
  310:     struct mbuf *m;
  311: 
  312:     if_down(&sc->sc_if);
  313:     sc->sc_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
  314:     getmicrotime(&sc->sc_if.if_lastchange);
  315:     sc->sc_devp = NULL;
  316:     sc->sc_xfer = NULL;
  317:     for (;;) {
  318: 	IF_DEQUEUE(&sc->sc_rawq, m);
  319: 	if (m == NULL)
  320: 	    break;
  321: 	m_freem(m);
  322:     }
  323:     for (;;) {
  324: 	IF_DEQUEUE(&sc->sc_inq, m);
  325: 	if (m == NULL)
  326: 	    break;
  327: 	m_freem(m);
  328:     }
  329:     for (;;) {
  330: 	IF_DEQUEUE(&sc->sc_fastq, m);
  331: 	if (m == NULL)
  332: 	    break;
  333: 	m_freem(m);
  334:     }
  335:     while ((m = sc->sc_npqueue) != NULL) {
  336: 	sc->sc_npqueue = m->m_nextpkt;
  337: 	m_freem(m);
  338:     }
  339: #ifdef PPP_COMPRESS
  340:     ppp_ccp_closed(sc);
  341:     sc->sc_xc_state = NULL;
  342:     sc->sc_rc_state = NULL;
  343: #endif /* PPP_COMPRESS */
  344: #ifdef PPP_FILTER
  345:     if (sc->sc_pass_filt.bf_insns != 0) {
  346: 	free(sc->sc_pass_filt.bf_insns, M_DEVBUF);
  347: 	sc->sc_pass_filt.bf_insns = 0;
  348: 	sc->sc_pass_filt.bf_len = 0;
  349:     }
  350:     if (sc->sc_active_filt.bf_insns != 0) {
  351: 	free(sc->sc_active_filt.bf_insns, M_DEVBUF);
  352: 	sc->sc_active_filt.bf_insns = 0;
  353: 	sc->sc_active_filt.bf_len = 0;
  354:     }
  355: #endif /* PPP_FILTER */
  356: #ifdef VJC
  357:     if (sc->sc_comp != 0) {
  358: 	free(sc->sc_comp, M_DEVBUF);
  359: 	sc->sc_comp = 0;
  360:     }
  361: #endif
  362: }
  363: 
  364: /*
  365:  * Ioctl routine for generic ppp devices.
  366:  */
  367: int
  368: pppioctl(struct ppp_softc *sc, u_long cmd, caddr_t data,
  369:     int flag, struct thread *td)
  370: {
  371:     int s, error, flags, mru, npx;
  372:     u_int nb;
  373:     struct ppp_option_data *odp;
  374:     struct compressor **cp;
  375:     struct npioctl *npi;
  376:     time_t t;
  377: #ifdef PPP_FILTER
  378:     struct bpf_program *bp, *nbp;
  379:     struct bpf_insn *newcode, *oldcode;
  380:     int newcodelen;
  381: #endif /* PPP_FILTER */
  382: #ifdef	PPP_COMPRESS
  383:     u_char ccp_option[CCP_MAX_OPTION_LENGTH];
  384: #endif
  385: 
  386:     switch (cmd) {
  387:     case FIONREAD:
  388: 	*(int *)data = sc->sc_inq.ifq_len;
  389: 	break;
  390: 
  391:     case PPPIOCGUNIT:
  392: 	*(int *)data = sc->sc_if.if_dunit;
  393: 	break;
  394: 
  395:     case PPPIOCGFLAGS:
  396: 	*(u_int *)data = sc->sc_flags;
  397: 	break;
  398: 
  399:     case PPPIOCSFLAGS:
  400: 	if ((error = suser(td)) != 0)
  401: 	    return (error);
  402: 	flags = *(int *)data & SC_MASK;
  403: 	s = splsoftnet();
  404: #ifdef PPP_COMPRESS
  405: 	if (sc->sc_flags & SC_CCP_OPEN && !(flags & SC_CCP_OPEN))
  406: 	    ppp_ccp_closed(sc);
  407: #endif
  408: 	splimp();
  409: 	sc->sc_flags = (sc->sc_flags & ~SC_MASK) | flags;
  410: 	splx(s);
  411: 	break;
  412: 
  413:     case PPPIOCSMRU:
  414: 	if ((error = suser(td)) != 0)
  415: 	    return (error);
  416: 	mru = *(int *)data;
  417: 	if (mru >= PPP_MRU && mru <= PPP_MAXMRU)
  418: 	    sc->sc_mru = mru;
  419: 	break;
  420: 
  421:     case PPPIOCGMRU:
  422: 	*(int *)data = sc->sc_mru;
  423: 	break;
  424: 
  425: #ifdef VJC
  426:     case PPPIOCSMAXCID:
  427: 	if ((error = suser(td)) != 0)
  428: 	    return (error);
  429: 	if (sc->sc_comp) {
  430: 	    s = splsoftnet();
  431: 	    sl_compress_init(sc->sc_comp, *(int *)data);
  432: 	    splx(s);
  433: 	}
  434: 	break;
  435: #endif
  436: 
  437:     case PPPIOCXFERUNIT:
  438: 	if ((error = suser(td)) != 0)
  439: 	    return (error);
  440: 	sc->sc_xfer = td;
  441: 	break;
  442: 
  443: #ifdef PPP_COMPRESS
  444:     case PPPIOCSCOMPRESS:
  445: 	if ((error = suser(td)) != 0)
  446: 	    return (error);
  447: 	odp = (struct ppp_option_data *) data;
  448: 	nb = odp->length;
  449: 	if (nb > sizeof(ccp_option))
  450: 	    nb = sizeof(ccp_option);
  451: 	if ((error = copyin(odp->ptr, ccp_option, nb)) != 0)
  452: 	    return (error);
  453: 	if (ccp_option[1] < 2)	/* preliminary check on the length byte */
  454: 	    return (EINVAL);
  455: 	for (cp = ppp_compressors; *cp != NULL; ++cp)
  456: 	    if ((*cp)->compress_proto == ccp_option[0]) {
  457: 		/*
  458: 		 * Found a handler for the protocol - try to allocate
  459: 		 * a compressor or decompressor.
  460: 		 */
  461: 		error = 0;
  462: 		if (odp->transmit) {
  463: 		    s = splsoftnet();
  464: 		    if (sc->sc_xc_state != NULL)
  465: 			(*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
  466: 		    sc->sc_xcomp = *cp;
  467: 		    sc->sc_xc_state = (*cp)->comp_alloc(ccp_option, nb);
  468: 		    if (sc->sc_xc_state == NULL) {
  469: 			if (sc->sc_flags & SC_DEBUG)
  470: 			    printf("%s: comp_alloc failed\n",
  471: 			       sc->sc_if.if_xname);
  472: 			error = ENOBUFS;
  473: 		    }
  474: 		    splimp();
  475: 		    sc->sc_flags &= ~SC_COMP_RUN;
  476: 		    splx(s);
  477: 		} else {
  478: 		    s = splsoftnet();
  479: 		    if (sc->sc_rc_state != NULL)
  480: 			(*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
  481: 		    sc->sc_rcomp = *cp;
  482: 		    sc->sc_rc_state = (*cp)->decomp_alloc(ccp_option, nb);
  483: 		    if (sc->sc_rc_state == NULL) {
  484: 			if (sc->sc_flags & SC_DEBUG)
  485: 			    printf("%s: decomp_alloc failed\n",
  486: 			       sc->sc_if.if_xname);
  487: 			error = ENOBUFS;
  488: 		    }
  489: 		    splimp();
  490: 		    sc->sc_flags &= ~SC_DECOMP_RUN;
  491: 		    splx(s);
  492: 		}
  493: 		return (error);
  494: 	    }
  495: 	if (sc->sc_flags & SC_DEBUG)
  496: 	    printf("%s: no compressor for [%x %x %x], %x\n",
  497: 		   sc->sc_if.if_xname, ccp_option[0], ccp_option[1],
  498: 		   ccp_option[2], nb);
  499: 	return (EINVAL);	/* no handler found */
  500: #endif /* PPP_COMPRESS */
  501: 
  502:     case PPPIOCGNPMODE:
  503:     case PPPIOCSNPMODE:
  504: 	npi = (struct npioctl *) data;
  505: 	switch (npi->protocol) {
  506: 	case PPP_IP:
  507: 	    npx = NP_IP;
  508: 	    break;
  509: 	default:
  510: 	    return EINVAL;
  511: 	}
  512: 	if (cmd == PPPIOCGNPMODE) {
  513: 	    npi->mode = sc->sc_npmode[npx];
  514: 	} else {
  515: 	    if ((error = suser(td)) != 0)
  516: 		return (error);
  517: 	    if (npi->mode != sc->sc_npmode[npx]) {
  518: 		s = splsoftnet();
  519: 		sc->sc_npmode[npx] = npi->mode;
  520: 		if (npi->mode != NPMODE_QUEUE) {
  521: 		    ppp_requeue(sc);
  522: 		    (*sc->sc_start)(sc);
  523: 		}
  524: 		splx(s);
  525: 	    }
  526: 	}
  527: 	break;
  528: 
  529:     case PPPIOCGIDLE:
  530: 	s = splsoftnet();
  531: 	t = time_second;
  532: 	((struct ppp_idle *)data)->xmit_idle = t - sc->sc_last_sent;
  533: 	((struct ppp_idle *)data)->recv_idle = t - sc->sc_last_recv;
  534: 	splx(s);
  535: 	break;
  536: 
  537: #ifdef PPP_FILTER
  538:     case PPPIOCSPASS:
  539:     case PPPIOCSACTIVE:
  540: 	nbp = (struct bpf_program *) data;
  541: 	if ((unsigned) nbp->bf_len > BPF_MAXINSNS)
  542: 	    return EINVAL;
  543: 	newcodelen = nbp->bf_len * sizeof(struct bpf_insn);
  544: 	if (newcodelen != 0) {
  545: 	    MALLOC(newcode, struct bpf_insn *, newcodelen, M_DEVBUF, M_WAITOK);
  546: 	    if (newcode == 0) {
  547: 		return EINVAL;		/* or sumpin */
  548: 	    }
  549: 	    if ((error = copyin((caddr_t)nbp->bf_insns, (caddr_t)newcode,
  550: 			       newcodelen)) != 0) {
  551: 		free(newcode, M_DEVBUF);
  552: 		return error;
  553: 	    }
  554: 	    if (!bpf_validate(newcode, nbp->bf_len)) {
  555: 		free(newcode, M_DEVBUF);
  556: 		return EINVAL;
  557: 	    }
  558: 	} else
  559: 	    newcode = 0;
  560: 	bp = (cmd == PPPIOCSPASS)? &sc->sc_pass_filt: &sc->sc_active_filt;
  561: 	oldcode = bp->bf_insns;
  562: 	s = splimp();
  563: 	bp->bf_len = nbp->bf_len;
  564: 	bp->bf_insns = newcode;
  565: 	splx(s);
  566: 	if (oldcode != 0)
  567: 	    free(oldcode, M_DEVBUF);
  568: 	break;
  569: #endif
  570: 
  571:     default:
  572: 	return (ENOIOCTL);
  573:     }
  574:     return (0);
  575: }
  576: 
  577: /*
  578:  * Process an ioctl request to the ppp network interface.
  579:  */
  580: static int
  581: pppsioctl(ifp, cmd, data, cr)
  582:     struct ifnet *ifp;
  583:     u_long cmd;
  584:     caddr_t data;
  585:     struct ucred *cr;
  586: {
  587:     struct thread *td = curthread;	/* XXX */
  588:     struct ppp_softc *sc = &ppp_softc[ifp->if_dunit];
  589:     struct ifaddr *ifa = (struct ifaddr *)data;
  590:     struct ifreq *ifr = (struct ifreq *)data;
  591:     struct ppp_stats *psp;
  592: #ifdef	PPP_COMPRESS
  593:     struct ppp_comp_stats *pcp;
  594: #endif
  595:     int s = splimp(), error = 0;
  596: 
  597:     switch (cmd) {
  598:     case SIOCSIFFLAGS:
  599: 	if ((ifp->if_flags & IFF_RUNNING) == 0)
  600: 	    ifp->if_flags &= ~IFF_UP;
  601: 	break;
  602: 
  603:     case SIOCSIFADDR:
  604:     case SIOCAIFADDR:
  605: 	switch(ifa->ifa_addr->sa_family) {
  606: #ifdef INET
  607: 	case AF_INET:
  608: 	    break;
  609: #endif
  610: #ifdef IPX
  611: 	case AF_IPX:
  612: 	    break;
  613: #endif
  614: 	default:
  615: 	    error = EAFNOSUPPORT;
  616: 	    break;
  617: 	}
  618: 	break;
  619: 
  620:     case SIOCSIFDSTADDR:
  621: 	switch(ifa->ifa_addr->sa_family) {
  622: #ifdef INET
  623: 	case AF_INET:
  624: 	    break;
  625: #endif
  626: #ifdef IPX
  627: 	case AF_IPX:
  628: 	    break;
  629: #endif
  630: 	default:
  631: 	    error = EAFNOSUPPORT;
  632: 	    break;
  633: 	}
  634: 	break;
  635: 
  636:     case SIOCSIFMTU:
  637: 	if ((error = suser(td)) != 0)
  638: 	    break;
  639: 	if (ifr->ifr_mtu > PPP_MAXMTU)
  640: 	    error = EINVAL;
  641: 	else {
  642: 	    sc->sc_if.if_mtu = ifr->ifr_mtu;
  643: 	    if (sc->sc_setmtu)
  644: 		    (*sc->sc_setmtu)(sc);
  645: 	}
  646: 	break;
  647: 
  648:     case SIOCGIFMTU:
  649: 	ifr->ifr_mtu = sc->sc_if.if_mtu;
  650: 	break;
  651: 
  652:     case SIOCADDMULTI:
  653:     case SIOCDELMULTI:
  654: 	if (ifr == 0) {
  655: 	    error = EAFNOSUPPORT;
  656: 	    break;
  657: 	}
  658: 	switch(ifr->ifr_addr.sa_family) {
  659: #ifdef INET
  660: 	case AF_INET:
  661: 	    break;
  662: #endif
  663: 	default:
  664: 	    error = EAFNOSUPPORT;
  665: 	    break;
  666: 	}
  667: 	break;
  668: 
  669:     case SIOCGPPPSTATS:
  670: 	psp = &((struct ifpppstatsreq *) data)->stats;
  671: 	bzero(psp, sizeof(*psp));
  672: 	psp->p = sc->sc_stats;
  673: #if defined(VJC) && !defined(SL_NO_STATS)
  674: 	if (sc->sc_comp) {
  675: 	    psp->vj.vjs_packets = sc->sc_comp->sls_packets;
  676: 	    psp->vj.vjs_compressed = sc->sc_comp->sls_compressed;
  677: 	    psp->vj.vjs_searches = sc->sc_comp->sls_searches;
  678: 	    psp->vj.vjs_misses = sc->sc_comp->sls_misses;
  679: 	    psp->vj.vjs_uncompressedin = sc->sc_comp->sls_uncompressedin;
  680: 	    psp->vj.vjs_compressedin = sc->sc_comp->sls_compressedin;
  681: 	    psp->vj.vjs_errorin = sc->sc_comp->sls_errorin;
  682: 	    psp->vj.vjs_tossed = sc->sc_comp->sls_tossed;
  683: 	}
  684: #endif /* VJC */
  685: 	break;
  686: 
  687: #ifdef PPP_COMPRESS
  688:     case SIOCGPPPCSTATS:
  689: 	pcp = &((struct ifpppcstatsreq *) data)->stats;
  690: 	bzero(pcp, sizeof(*pcp));
  691: 	if (sc->sc_xc_state != NULL)
  692: 	    (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c);
  693: 	if (sc->sc_rc_state != NULL)
  694: 	    (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d);
  695: 	break;
  696: #endif /* PPP_COMPRESS */
  697: 
  698:     default:
  699: 	error = ENOTTY;
  700:     }
  701:     splx(s);
  702:     return (error);
  703: }
  704: 
  705: /*
  706:  * Queue a packet.  Start transmission if not active.
  707:  * Packet is placed in Information field of PPP frame.
  708:  * Called at splnet as the if->if_output handler.
  709:  * Called at splnet from pppwrite().
  710:  */
  711: int
  712: pppoutput(ifp, m0, dst, rtp)
  713:     struct ifnet *ifp;
  714:     struct mbuf *m0;
  715:     struct sockaddr *dst;
  716:     struct rtentry *rtp;
  717: {
  718:     struct ppp_softc *sc = &ppp_softc[ifp->if_dunit];
  719:     int protocol, address, control;
  720:     u_char *cp;
  721:     int s, error;
  722:     struct ip *ip;
  723:     struct ifqueue *ifq;
  724:     enum NPmode mode;
  725:     int len;
  726:     struct mbuf *m;
  727: 
  728:     if (sc->sc_devp == NULL || (ifp->if_flags & IFF_RUNNING) == 0
  729: 	|| ((ifp->if_flags & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC)) {
  730: 	error = ENETDOWN;	/* sort of */
  731: 	goto bad;
  732:     }
  733: 
  734:     /*
  735:      * Compute PPP header.
  736:      */
  737:     m0->m_flags &= ~M_HIGHPRI;
  738:     switch (dst->sa_family) {
  739: #ifdef INET
  740:     case AF_INET:
  741: 	address = PPP_ALLSTATIONS;
  742: 	control = PPP_UI;
  743: 	protocol = PPP_IP;
  744: 	mode = sc->sc_npmode[NP_IP];
  745: 
  746: 	/*
  747: 	 * If this packet has the "low delay" bit set in the IP header,
  748: 	 * put it on the fastq instead.
  749: 	 */
  750: 	ip = mtod(m0, struct ip *);
  751: 	if (ip->ip_tos & IPTOS_LOWDELAY)
  752: 	    m0->m_flags |= M_HIGHPRI;
  753: 	break;
  754: #endif
  755: #ifdef IPX
  756:     case AF_IPX:
  757: 	/*
  758: 	 * This is pretty bogus.. We dont have an ipxcp module in pppd
  759: 	 * yet to configure the link parameters.  Sigh. I guess a
  760: 	 * manual ifconfig would do....  -Peter
  761: 	 */
  762: 	address = PPP_ALLSTATIONS;
  763: 	control = PPP_UI;
  764: 	protocol = PPP_IPX;
  765: 	mode = NPMODE_PASS;
  766: 	break;
  767: #endif
  768:     case AF_UNSPEC:
  769: 	address = PPP_ADDRESS(dst->sa_data);
  770: 	control = PPP_CONTROL(dst->sa_data);
  771: 	protocol = PPP_PROTOCOL(dst->sa_data);
  772: 	mode = NPMODE_PASS;
  773: 	break;
  774:     default:
  775: 	printf("%s: af%d not supported\n", ifp->if_xname, dst->sa_family);
  776: 	error = EAFNOSUPPORT;
  777: 	goto bad;
  778:     }
  779: 
  780:     /*
  781:      * Drop this packet, or return an error, if necessary.
  782:      */
  783:     if (mode == NPMODE_ERROR) {
  784: 	error = ENETDOWN;
  785: 	goto bad;
  786:     }
  787:     if (mode == NPMODE_DROP) {
  788: 	error = 0;
  789: 	goto bad;
  790:     }
  791: 
  792:     /*
  793:      * Add PPP header.  If no space in first mbuf, allocate another.
  794:      * (This assumes M_LEADINGSPACE is always 0 for a cluster mbuf.)
  795:      */
  796:     if (M_LEADINGSPACE(m0) < PPP_HDRLEN) {
  797: 	m0 = m_prepend(m0, PPP_HDRLEN, M_DONTWAIT);
  798: 	if (m0 == 0) {
  799: 	    error = ENOBUFS;
  800: 	    goto bad;
  801: 	}
  802: 	m0->m_len = 0;
  803:     } else
  804: 	m0->m_data -= PPP_HDRLEN;
  805: 
  806:     cp = mtod(m0, u_char *);
  807:     *cp++ = address;
  808:     *cp++ = control;
  809:     *cp++ = protocol >> 8;
  810:     *cp++ = protocol & 0xff;
  811:     m0->m_len += PPP_HDRLEN;
  812: 
  813:     len = 0;
  814:     for (m = m0; m != 0; m = m->m_next)
  815: 	len += m->m_len;
  816: 
  817:     if (sc->sc_flags & SC_LOG_OUTPKT) {
  818: 	printf("%s output: ", ifp->if_xname);
  819: 	pppdumpm(m0);
  820:     }
  821: 
  822:     if ((protocol & 0x8000) == 0) {
  823: #ifdef PPP_FILTER
  824: 	/*
  825: 	 * Apply the pass and active filters to the packet,
  826: 	 * but only if it is a data packet.
  827: 	 */
  828: 	*mtod(m0, u_char *) = 1;	/* indicates outbound */
  829: 	if (sc->sc_pass_filt.bf_insns != 0
  830: 	    && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m0,
  831: 			  len, 0) == 0) {
  832: 	    error = 0;		/* drop this packet */
  833: 	    goto bad;
  834: 	}
  835: 
  836: 	/*
  837: 	 * Update the time we sent the most recent packet.
  838: 	 */
  839: 	if (sc->sc_active_filt.bf_insns == 0
  840: 	    || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m0, len, 0))
  841: 	    sc->sc_last_sent = time_second;
  842: 
  843: 	*mtod(m0, u_char *) = address;
  844: #else
  845: 	/*
  846: 	 * Update the time we sent the most recent data packet.
  847: 	 */
  848: 	sc->sc_last_sent = time_second;
  849: #endif /* PPP_FILTER */
  850:     }
  851: 
  852:     /*
  853:      * See if bpf wants to look at the packet.
  854:      */
  855:     if (ifp->if_bpf)
  856: 	bpf_mtap(ifp, m0);
  857: 
  858:     /*
  859:      * Put the packet on the appropriate queue.
  860:      */
  861:     s = splsoftnet();	/* redundant */
  862:     if (mode == NPMODE_QUEUE) {
  863: 	/* XXX we should limit the number of packets on this queue */
  864: 	*sc->sc_npqtail = m0;
  865: 	m0->m_nextpkt = NULL;
  866: 	sc->sc_npqtail = &m0->m_nextpkt;
  867:     } else {
  868: 	/* fastq and if_snd are emptied at spl[soft]net now */
  869: 	ifq = (m0->m_flags & M_HIGHPRI)? &sc->sc_fastq: &ifp->if_snd;
  870: 	if (IF_QFULL(ifq) && dst->sa_family != AF_UNSPEC) {
  871: 	    IF_DROP(ifq);
  872: 	    splx(s);
  873: 	    sc->sc_if.if_oerrors++;
  874: 	    sc->sc_stats.ppp_oerrors++;
  875: 	    error = ENOBUFS;
  876: 	    goto bad;
  877: 	}
  878: 	IF_ENQUEUE(ifq, m0);
  879: 	(*sc->sc_start)(sc);
  880:     }
  881:     getmicrotime(&ifp->if_lastchange);
  882:     ifp->if_opackets++;
  883:     ifp->if_obytes += len;
  884: 
  885:     splx(s);
  886:     return (0);
  887: 
  888: bad:
  889:     m_freem(m0);
  890:     return (error);
  891: }
  892: 
  893: /*
  894:  * After a change in the NPmode for some NP, move packets from the
  895:  * npqueue to the send queue or the fast queue as appropriate.
  896:  * Should be called at spl[soft]net.
  897:  */
  898: static void
  899: ppp_requeue(sc)
  900:     struct ppp_softc *sc;
  901: {
  902:     struct mbuf *m, **mpp;
  903:     struct ifqueue *ifq;
  904:     enum NPmode mode;
  905: 
  906:     for (mpp = &sc->sc_npqueue; (m = *mpp) != NULL; ) {
  907: 	switch (PPP_PROTOCOL(mtod(m, u_char *))) {
  908: 	case PPP_IP:
  909: 	    mode = sc->sc_npmode[NP_IP];
  910: 	    break;
  911: 	default:
  912: 	    mode = NPMODE_PASS;
  913: 	}
  914: 
  915: 	switch (mode) {
  916: 	case NPMODE_PASS:
  917: 	    /*
  918: 	     * This packet can now go on one of the queues to be sent.
  919: 	     */
  920: 	    *mpp = m->m_nextpkt;
  921: 	    m->m_nextpkt = NULL;
  922: 	    ifq = (m->m_flags & M_HIGHPRI)? &sc->sc_fastq: &sc->sc_if.if_snd;
  923: 	    if (IF_QFULL(ifq)) {
  924: 		IF_DROP(ifq);
  925: 		sc->sc_if.if_oerrors++;
  926: 		sc->sc_stats.ppp_oerrors++;
  927: 	    } else
  928: 		IF_ENQUEUE(ifq, m);
  929: 	    break;
  930: 
  931: 	case NPMODE_DROP:
  932: 	case NPMODE_ERROR:
  933: 	    *mpp = m->m_nextpkt;
  934: 	    m_freem(m);
  935: 	    break;
  936: 
  937: 	case NPMODE_QUEUE:
  938: 	    mpp = &m->m_nextpkt;
  939: 	    break;
  940: 	}
  941:     }
  942:     sc->sc_npqtail = mpp;
  943: }
  944: 
  945: /*
  946:  * Transmitter has finished outputting some stuff;
  947:  * remember to call sc->sc_start later at splsoftnet.
  948:  */
  949: void
  950: ppp_restart(sc)
  951:     struct ppp_softc *sc;
  952: {
  953:     int s = splimp();
  954: 
  955:     sc->sc_flags &= ~SC_TBUSY;
  956:     schednetisr(NETISR_PPP);
  957:     splx(s);
  958: }
  959: 
  960: 
  961: /*
  962:  * Get a packet to send.  This procedure is intended to be called at
  963:  * splsoftnet, since it may involve time-consuming operations such as
  964:  * applying VJ compression, packet compression, address/control and/or
  965:  * protocol field compression to the packet.
  966:  */
  967: struct mbuf *
  968: ppp_dequeue(sc)
  969:     struct ppp_softc *sc;
  970: {
  971:     struct mbuf *m, *mp;
  972:     u_char *cp;
  973:     int address, control, protocol;
  974: 
  975:     /*
  976:      * Grab a packet to send: first try the fast queue, then the
  977:      * normal queue.
  978:      */
  979:     IF_DEQUEUE(&sc->sc_fastq, m);
  980:     if (m == NULL)
  981: 	IF_DEQUEUE(&sc->sc_if.if_snd, m);
  982:     if (m == NULL)
  983: 	return NULL;
  984: 
  985:     ++sc->sc_stats.ppp_opackets;
  986: 
  987:     /*
  988:      * Extract the ppp header of the new packet.
  989:      * The ppp header will be in one mbuf.
  990:      */
  991:     cp = mtod(m, u_char *);
  992:     address = PPP_ADDRESS(cp);
  993:     control = PPP_CONTROL(cp);
  994:     protocol = PPP_PROTOCOL(cp);
  995: 
  996:     switch (protocol) {
  997:     case PPP_IP:
  998: #ifdef VJC
  999: 	/*
 1000: 	 * If the packet is a TCP/IP packet, see if we can compress it.
 1001: 	 */
 1002: 	if ((sc->sc_flags & SC_COMP_TCP) && sc->sc_comp != NULL) {
 1003: 	    struct ip *ip;
 1004: 	    int type;
 1005: 
 1006: 	    mp = m;
 1007: 	    ip = (struct ip *) (cp + PPP_HDRLEN);
 1008: 	    if (mp->m_len <= PPP_HDRLEN) {
 1009: 		mp = mp->m_next;
 1010: 		if (mp == NULL)
 1011: 		    break;
 1012: 		ip = mtod(mp, struct ip *);
 1013: 	    }
 1014: 	    /* this code assumes the IP/TCP header is in one non-shared mbuf */
 1015: 	    if (ip->ip_p == IPPROTO_TCP) {
 1016: 		type = sl_compress_tcp(mp, ip, sc->sc_comp,
 1017: 				       !(sc->sc_flags & SC_NO_TCP_CCID));
 1018: 		switch (type) {
 1019: 		case TYPE_UNCOMPRESSED_TCP:
 1020: 		    protocol = PPP_VJC_UNCOMP;
 1021: 		    break;
 1022: 		case TYPE_COMPRESSED_TCP:
 1023: 		    protocol = PPP_VJC_COMP;
 1024: 		    cp = mtod(m, u_char *);
 1025: 		    cp[0] = address;	/* header has moved */
 1026: 		    cp[1] = control;
 1027: 		    cp[2] = 0;
 1028: 		    break;
 1029: 		}
 1030: 		cp[3] = protocol;	/* update protocol in PPP header */
 1031: 	    }
 1032: 	}
 1033: #endif	/* VJC */
 1034: 	break;
 1035: 
 1036: #ifdef PPP_COMPRESS
 1037:     case PPP_CCP:
 1038: 	ppp_ccp(sc, m, 0);
 1039: 	break;
 1040: #endif	/* PPP_COMPRESS */
 1041:     }
 1042: 
 1043: #ifdef PPP_COMPRESS
 1044:     if (protocol != PPP_LCP && protocol != PPP_CCP
 1045: 	&& sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
 1046: 	struct mbuf *mcomp = NULL;
 1047: 	int slen, clen;
 1048: 
 1049: 	slen = 0;
 1050: 	for (mp = m; mp != NULL; mp = mp->m_next)
 1051: 	    slen += mp->m_len;
 1052: 	clen = (*sc->sc_xcomp->compress)
 1053: 	    (sc->sc_xc_state, &mcomp, m, slen, sc->sc_if.if_mtu + PPP_HDRLEN);
 1054: 	if (mcomp != NULL) {
 1055: 	    if (sc->sc_flags & SC_CCP_UP) {
 1056: 		/* Send the compressed packet instead of the original. */
 1057: 		m_freem(m);
 1058: 		m = mcomp;
 1059: 		cp = mtod(m, u_char *);
 1060: 		protocol = cp[3];
 1061: 	    } else {
 1062: 		/* Can't transmit compressed packets until CCP is up. */
 1063: 		m_freem(mcomp);
 1064: 	    }
 1065: 	}
 1066:     }
 1067: #endif	/* PPP_COMPRESS */
 1068: 
 1069:     /*
 1070:      * Compress the address/control and protocol, if possible.
 1071:      */
 1072:     if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
 1073: 	control == PPP_UI && protocol != PPP_ALLSTATIONS &&
 1074: 	protocol != PPP_LCP) {
 1075: 	/* can compress address/control */
 1076: 	m->m_data += 2;
 1077: 	m->m_len -= 2;
 1078:     }
 1079:     if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
 1080: 	/* can compress protocol */
 1081: 	if (mtod(m, u_char *) == cp) {
 1082: 	    cp[2] = cp[1];	/* move address/control up */
 1083: 	    cp[1] = cp[0];
 1084: 	}
 1085: 	++m->m_data;
 1086: 	--m->m_len;
 1087:     }
 1088: 
 1089:     return m;
 1090: }
 1091: 
 1092: #ifdef PPP_COMPRESS
 1093: /*
 1094:  * Handle a CCP packet.  `rcvd' is 1 if the packet was received,
 1095:  * 0 if it is about to be transmitted.
 1096:  */
 1097: static void
 1098: ppp_ccp(sc, m, rcvd)
 1099:     struct ppp_softc *sc;
 1100:     struct mbuf *m;
 1101:     int rcvd;
 1102: {
 1103:     u_char *dp, *ep;
 1104:     struct mbuf *mp;
 1105:     int slen, s;
 1106: 
 1107:     /*
 1108:      * Get a pointer to the data after the PPP header.
 1109:      */
 1110:     if (m->m_len <= PPP_HDRLEN) {
 1111: 	mp = m->m_next;
 1112: 	if (mp == NULL)
 1113: 	    return;
 1114: 	dp = (mp != NULL)? mtod(mp, u_char *): NULL;
 1115:     } else {
 1116: 	mp = m;
 1117: 	dp = mtod(mp, u_char *) + PPP_HDRLEN;
 1118:     }
 1119: 
 1120:     ep = mtod(mp, u_char *) + mp->m_len;
 1121:     if (dp + CCP_HDRLEN > ep)
 1122: 	return;
 1123:     slen = CCP_LENGTH(dp);
 1124:     if (dp + slen > ep) {
 1125: 	if (sc->sc_flags & SC_DEBUG)
 1126: 	    printf("if_ppp/ccp: not enough data in mbuf (%p+%x > %p+%x)\n",
 1127: 		   dp, slen, mtod(mp, u_char *), mp->m_len);
 1128: 	return;
 1129:     }
 1130: 
 1131:     switch (CCP_CODE(dp)) {
 1132:     case CCP_CONFREQ:
 1133:     case CCP_TERMREQ:
 1134:     case CCP_TERMACK:
 1135: 	/* CCP must be going down - disable compression */
 1136: 	if (sc->sc_flags & SC_CCP_UP) {
 1137: 	    s = splimp();
 1138: 	    sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
 1139: 	    splx(s);
 1140: 	}
 1141: 	break;
 1142: 
 1143:     case CCP_CONFACK:
 1144: 	if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
 1145: 	    && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
 1146: 	    && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
 1147: 	    if (!rcvd) {
 1148: 		/* we're agreeing to send compressed packets. */
 1149: 		if (sc->sc_xc_state != NULL
 1150: 		    && (*sc->sc_xcomp->comp_init)
 1151: 			(sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
 1152: 			 sc->sc_if.if_dunit, 0, sc->sc_flags & SC_DEBUG)) {
 1153: 		    s = splimp();
 1154: 		    sc->sc_flags |= SC_COMP_RUN;
 1155: 		    splx(s);
 1156: 		}
 1157: 	    } else {
 1158: 		/* peer is agreeing to send compressed packets. */
 1159: 		if (sc->sc_rc_state != NULL
 1160: 		    && (*sc->sc_rcomp->decomp_init)
 1161: 			(sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
 1162: 			 sc->sc_if.if_dunit, 0, sc->sc_mru,
 1163: 			 sc->sc_flags & SC_DEBUG)) {
 1164: 		    s = splimp();
 1165: 		    sc->sc_flags |= SC_DECOMP_RUN;
 1166: 		    sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
 1167: 		    splx(s);
 1168: 		}
 1169: 	    }
 1170: 	}
 1171: 	break;
 1172: 
 1173:     case CCP_RESETACK:
 1174: 	if (sc->sc_flags & SC_CCP_UP) {
 1175: 	    if (!rcvd) {
 1176: 		if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN))
 1177: 		    (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
 1178: 	    } else {
 1179: 		if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
 1180: 		    (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
 1181: 		    s = splimp();
 1182: 		    sc->sc_flags &= ~SC_DC_ERROR;
 1183: 		    splx(s);
 1184: 		}
 1185: 	    }
 1186: 	}
 1187: 	break;
 1188:     }
 1189: }
 1190: 
 1191: /*
 1192:  * CCP is down; free (de)compressor state if necessary.
 1193:  */
 1194: static void
 1195: ppp_ccp_closed(sc)
 1196:     struct ppp_softc *sc;
 1197: {
 1198:     if (sc->sc_xc_state) {
 1199: 	(*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
 1200: 	sc->sc_xc_state = NULL;
 1201:     }
 1202:     if (sc->sc_rc_state) {
 1203: 	(*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
 1204: 	sc->sc_rc_state = NULL;
 1205:     }
 1206: }
 1207: #endif /* PPP_COMPRESS */
 1208: 
 1209: /*
 1210:  * PPP packet input routine.
 1211:  * The caller has checked and removed the FCS and has inserted
 1212:  * the address/control bytes and the protocol high byte if they
 1213:  * were omitted.
 1214:  */
 1215: void
 1216: ppppktin(sc, m, lost)
 1217:     struct ppp_softc *sc;
 1218:     struct mbuf *m;
 1219:     int lost;
 1220: {
 1221:     int s = splimp();
 1222: 
 1223:     if (lost)
 1224: 	m->m_flags |= M_ERRMARK;
 1225:     IF_ENQUEUE(&sc->sc_rawq, m);
 1226:     schednetisr(NETISR_PPP);
 1227:     splx(s);
 1228: }
 1229: 
 1230: /*
 1231:  * Process a received PPP packet, doing decompression as necessary.
 1232:  * Should be called at splsoftnet.
 1233:  */
 1234: #define COMPTYPE(proto)	((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
 1235: 			 TYPE_UNCOMPRESSED_TCP)
 1236: 
 1237: static void
 1238: ppp_inproc(sc, m)
 1239:     struct ppp_softc *sc;
 1240:     struct mbuf *m;
 1241: {
 1242:     struct ifnet *ifp = &sc->sc_if;
 1243:     int isr;
 1244:     int s, ilen = 0, xlen, proto, rv;
 1245:     u_char *cp, adrs, ctrl;
 1246:     struct mbuf *mp, *dmp = NULL;
 1247:     u_char *iphdr;
 1248:     u_int hlen;
 1249: 
 1250:     sc->sc_stats.ppp_ipackets++;
 1251: 
 1252:     if (sc->sc_flags & SC_LOG_INPKT) {
 1253: 	ilen = 0;
 1254: 	for (mp = m; mp != NULL; mp = mp->m_next)
 1255: 	    ilen += mp->m_len;
 1256: 	printf("%s: got %d bytes\n", ifp->if_xname, ilen);
 1257: 	pppdumpm(m);
 1258:     }
 1259: 
 1260:     cp = mtod(m, u_char *);
 1261:     adrs = PPP_ADDRESS(cp);
 1262:     ctrl = PPP_CONTROL(cp);
 1263:     proto = PPP_PROTOCOL(cp);
 1264: 
 1265:     if (m->m_flags & M_ERRMARK) {
 1266: 	m->m_flags &= ~M_ERRMARK;
 1267: 	s = splimp();
 1268: 	sc->sc_flags |= SC_VJ_RESET;
 1269: 	splx(s);
 1270:     }
 1271: 
 1272: #ifdef PPP_COMPRESS
 1273:     /*
 1274:      * Decompress this packet if necessary, update the receiver's
 1275:      * dictionary, or take appropriate action on a CCP packet.
 1276:      */
 1277:     if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
 1278: 	&& !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
 1279: 	/* decompress this packet */
 1280: 	rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
 1281: 	if (rv == DECOMP_OK) {
 1282: 	    m_freem(m);
 1283: 	    if (dmp == NULL) {
 1284: 		/* no error, but no decompressed packet produced */
 1285: 		return;
 1286: 	    }
 1287: 	    m = dmp;
 1288: 	    cp = mtod(m, u_char *);
 1289: 	    proto = PPP_PROTOCOL(cp);
 1290: 
 1291: 	} else {
 1292: 	    /*
 1293: 	     * An error has occurred in decompression.
 1294: 	     * Pass the compressed packet up to pppd, which may take
 1295: 	     * CCP down or issue a Reset-Req.
 1296: 	     */
 1297: 	    if (sc->sc_flags & SC_DEBUG)
 1298: 		printf("%s: decompress failed %d\n", ifp->if_xname, rv);
 1299: 	    s = splimp();
 1300: 	    sc->sc_flags |= SC_VJ_RESET;
 1301: 	    if (rv == DECOMP_ERROR)
 1302: 		sc->sc_flags |= SC_DC_ERROR;
 1303: 	    else
 1304: 		sc->sc_flags |= SC_DC_FERROR;
 1305: 	    splx(s);
 1306: 	}
 1307: 
 1308:     } else {
 1309: 	if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
 1310: 	    (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
 1311: 	}
 1312: 	if (proto == PPP_CCP) {
 1313: 	    ppp_ccp(sc, m, 1);
 1314: 	}
 1315:     }
 1316: #endif
 1317: 
 1318:     ilen = 0;
 1319:     for (mp = m; mp != NULL; mp = mp->m_next)
 1320: 	ilen += mp->m_len;
 1321: 
 1322: #ifdef VJC
 1323:     if (sc->sc_flags & SC_VJ_RESET) {
 1324: 	/*
 1325: 	 * If we've missed a packet, we must toss subsequent compressed
 1326: 	 * packets which don't have an explicit connection ID.
 1327: 	 */
 1328: 	if (sc->sc_comp)
 1329: 	    sl_uncompress_tcp(NULL, 0, TYPE_ERROR, sc->sc_comp);
 1330: 	s = splimp();
 1331: 	sc->sc_flags &= ~SC_VJ_RESET;
 1332: 	splx(s);
 1333:     }
 1334: 
 1335:     /*
 1336:      * See if we have a VJ-compressed packet to uncompress.
 1337:      */
 1338:     if (proto == PPP_VJC_COMP) {
 1339: 	if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
 1340: 	    goto bad;
 1341: 
 1342: 	xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
 1343: 				      ilen - PPP_HDRLEN, TYPE_COMPRESSED_TCP,
 1344: 				      sc->sc_comp, &iphdr, &hlen);
 1345: 
 1346: 	if (xlen <= 0) {
 1347: 	    if (sc->sc_flags & SC_DEBUG)
 1348: 		printf("%s: VJ uncompress failed on type comp\n",
 1349: 			ifp->if_xname);
 1350: 	    goto bad;
 1351: 	}
 1352: 
 1353: 	/* Copy the PPP and IP headers into a new mbuf. */
 1354: 	MGETHDR(mp, M_DONTWAIT, MT_DATA);
 1355: 	if (mp == NULL)
 1356: 	    goto bad;
 1357: 	mp->m_len = 0;
 1358: 	mp->m_next = NULL;
 1359: 	if (hlen + PPP_HDRLEN > MHLEN) {
 1360: 	    MCLGET(mp, M_DONTWAIT);
 1361: 	    if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) {
 1362: 		m_freem(mp);
 1363: 		goto bad;	/* lose if big headers and no clusters */
 1364: 	    }
 1365: 	}
 1366: 	cp = mtod(mp, u_char *);
 1367: 	cp[0] = adrs;
 1368: 	cp[1] = ctrl;
 1369: 	cp[2] = 0;
 1370: 	cp[3] = PPP_IP;
 1371: 	proto = PPP_IP;
 1372: 	bcopy(iphdr, cp + PPP_HDRLEN, hlen);
 1373: 	mp->m_len = hlen + PPP_HDRLEN;
 1374: 
 1375: 	/*
 1376: 	 * Trim the PPP and VJ headers off the old mbuf
 1377: 	 * and stick the new and old mbufs together.
 1378: 	 */
 1379: 	m->m_data += PPP_HDRLEN + xlen;
 1380: 	m->m_len -= PPP_HDRLEN + xlen;
 1381: 	if (m->m_len <= M_TRAILINGSPACE(mp)) {
 1382: 	    bcopy(mtod(m, u_char *), mtod(mp, u_char *) + mp->m_len, m->m_len);
 1383: 	    mp->m_len += m->m_len;
 1384: 	    mp->m_next = m_free(m);
 1385: 	} else {
 1386: 	    mp->m_next = m;
 1387: 	}
 1388: 	m = mp;
 1389: 	ilen += hlen - xlen;
 1390: 
 1391:     } else if (proto == PPP_VJC_UNCOMP) {
 1392: 	if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
 1393: 	    goto bad;
 1394: 
 1395: 	xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
 1396: 				      ilen - PPP_HDRLEN, TYPE_UNCOMPRESSED_TCP,
 1397: 				      sc->sc_comp, &iphdr, &hlen);
 1398: 
 1399: 	if (xlen < 0) {
 1400: 	    if (sc->sc_flags & SC_DEBUG)
 1401: 		printf("%s: VJ uncompress failed on type uncomp\n",
 1402: 			ifp->if_xname);
 1403: 	    goto bad;
 1404: 	}
 1405: 
 1406: 	proto = PPP_IP;
 1407: 	cp[3] = PPP_IP;
 1408:     }
 1409: #endif /* VJC */
 1410: 
 1411:     /*
 1412:      * If the packet will fit in a header mbuf, don't waste a
 1413:      * whole cluster on it.
 1414:      */
 1415:     if (ilen <= MHLEN && M_IS_CLUSTER(m)) {
 1416: 	MGETHDR(mp, M_DONTWAIT, MT_DATA);
 1417: 	if (mp != NULL) {
 1418: 	    m_copydata(m, 0, ilen, mtod(mp, caddr_t));
 1419: 	    m_freem(m);
 1420: 	    m = mp;
 1421: 	    m->m_len = ilen;
 1422: 	}
 1423:     }
 1424:     m->m_pkthdr.len = ilen;
 1425:     m->m_pkthdr.rcvif = ifp;
 1426: 
 1427:     if ((proto & 0x8000) == 0) {
 1428: #ifdef PPP_FILTER
 1429: 	/*
 1430: 	 * See whether we want to pass this packet, and
 1431: 	 * if it counts as link activity.
 1432: 	 */
 1433: 	adrs = *mtod(m, u_char *);	/* save address field */
 1434: 	*mtod(m, u_char *) = 0;		/* indicate inbound */
 1435: 	if (sc->sc_pass_filt.bf_insns != 0
 1436: 	    && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m,
 1437: 			  ilen, 0) == 0) {
 1438: 	    /* drop this packet */
 1439: 	    m_freem(m);
 1440: 	    return;
 1441: 	}
 1442: 	if (sc->sc_active_filt.bf_insns == 0
 1443: 	    || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m, ilen, 0))
 1444: 	    sc->sc_last_recv = time_second;
 1445: 
 1446: 	*mtod(m, u_char *) = adrs;
 1447: #else
 1448: 	/*
 1449: 	 * Record the time that we received this packet.
 1450: 	 */
 1451: 	sc->sc_last_recv = time_second;
 1452: #endif /* PPP_FILTER */
 1453:     }
 1454: 
 1455:     /* See if bpf wants to look at the packet. */
 1456:     if (sc->sc_if.if_bpf)
 1457: 	bpf_mtap(&sc->sc_if, m);
 1458: 
 1459:     isr = -1;
 1460:     switch (proto) {
 1461: #ifdef INET
 1462:     case PPP_IP:
 1463: 	/*
 1464: 	 * IP packet - take off the ppp header and pass it up to IP.
 1465: 	 */
 1466: 	if ((ifp->if_flags & IFF_UP) == 0
 1467: 	    || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
 1468: 	    /* interface is down - drop the packet. */
 1469: 	    m_freem(m);
 1470: 	    return;
 1471: 	}
 1472: 	m->m_pkthdr.len -= PPP_HDRLEN;
 1473: 	m->m_data += PPP_HDRLEN;
 1474: 	m->m_len -= PPP_HDRLEN;
 1475: 	if (ipflow_fastforward(m))
 1476: 	    return;
 1477: 	isr = NETISR_IP;
 1478: 	break;
 1479: #endif
 1480: #ifdef IPX
 1481:     case PPP_IPX:
 1482: 	/*
 1483: 	 * IPX packet - take off the ppp header and pass it up to IPX.
 1484: 	 */
 1485: 	if ((sc->sc_if.if_flags & IFF_UP) == 0
 1486: 	    /* XXX: || sc->sc_npmode[NP_IPX] != NPMODE_PASS*/) {
 1487: 	    /* interface is down - drop the packet. */
 1488: 	    m_freem(m);
 1489: 	    return;
 1490: 	}
 1491: 	m->m_pkthdr.len -= PPP_HDRLEN;
 1492: 	m->m_data += PPP_HDRLEN;
 1493: 	m->m_len -= PPP_HDRLEN;
 1494: 	isr = NETISR_IPX;
 1495: 	sc->sc_last_recv = time_second;	/* update time of last pkt rcvd */
 1496: 	break;
 1497: #endif
 1498: 
 1499:     default:
 1500: 	/*
 1501: 	 * Some other protocol - place on input queue for read().
 1502: 	 */
 1503: 	break;
 1504:     }
 1505: 
 1506:     if (isr == -1)
 1507: 	rv = IF_HANDOFF(&sc->sc_inq, m, NULL);
 1508:     else
 1509: 	rv = (netisr_queue(isr, m) == 0);
 1510:     if (!rv) {
 1511: 	if (sc->sc_flags & SC_DEBUG)
 1512: 	    printf("%s: input queue full\n", ifp->if_xname);
 1513: 	ifp->if_iqdrops++;
 1514: 	goto bad;
 1515:     }
 1516: 
 1517:     ifp->if_ipackets++;
 1518:     ifp->if_ibytes += ilen;
 1519:     getmicrotime(&ifp->if_lastchange);
 1520: 
 1521:     if (rv)
 1522: 	(*sc->sc_ctlp)(sc);
 1523: 
 1524:     return;
 1525: 
 1526:  bad:
 1527:     m_freem(m);
 1528:     sc->sc_if.if_ierrors++;
 1529:     sc->sc_stats.ppp_ierrors++;
 1530: }
 1531: 
 1532: #define MAX_DUMP_BYTES	128
 1533: 
 1534: static void
 1535: pppdumpm(m0)
 1536:     struct mbuf *m0;
 1537: {
 1538:     char buf[3*MAX_DUMP_BYTES+4];
 1539:     char *bp = buf;
 1540:     struct mbuf *m;
 1541: 
 1542:     for (m = m0; m; m = m->m_next) {
 1543: 	int l = m->m_len;
 1544: 	u_char *rptr = (u_char *)m->m_data;
 1545: 
 1546: 	while (l--) {
 1547: 	    if (bp > buf + sizeof(buf) - 4)
 1548: 		goto done;
 1549: 	    *bp++ = hex2ascii(*rptr >> 4);
 1550: 	    *bp++ = hex2ascii(*rptr++ & 0xf);
 1551: 	}
 1552: 
 1553: 	if (m->m_next) {
 1554: 	    if (bp > buf + sizeof(buf) - 3)
 1555: 		goto done;
 1556: 	    *bp++ = '|';
 1557: 	} else
 1558: 	    *bp++ = ' ';
 1559:     }
 1560: done:
 1561:     if (m)
 1562: 	*bp++ = '>';
 1563:     *bp = 0;
 1564:     printf("%s\n", buf);
 1565: }