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