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