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