xref: /openbsd-src/sys/net/if_ppp.c (revision 2b0358df1d88d06ef4139321dd05bd5e05d91eaf)
1 /*	$OpenBSD: if_ppp.c,v 1.53 2009/03/15 19:40:41 miod 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     /*
731      * Compute PPP header.
732      */
733     m0->m_flags &= ~M_HIGHPRI;
734     switch (dst->sa_family) {
735 #ifdef INET
736     case AF_INET:
737 	address = PPP_ALLSTATIONS;
738 	control = PPP_UI;
739 	protocol = PPP_IP;
740 	mode = sc->sc_npmode[NP_IP];
741 
742 	/*
743 	 * If this packet has the "low delay" bit set in the IP header,
744 	 * put it on the fastq instead.
745 	 */
746 	ip = mtod(m0, struct ip *);
747 	if (ip->ip_tos & IPTOS_LOWDELAY)
748 	    m0->m_flags |= M_HIGHPRI;
749 	break;
750 #endif
751     case AF_UNSPEC:
752 	address = PPP_ADDRESS(dst->sa_data);
753 	control = PPP_CONTROL(dst->sa_data);
754 	protocol = PPP_PROTOCOL(dst->sa_data);
755 	mode = NPMODE_PASS;
756 	break;
757     default:
758 	printf("%s: af%d not supported\n", ifp->if_xname, dst->sa_family);
759 	error = EAFNOSUPPORT;
760 	goto bad;
761     }
762 
763     /*
764      * Drop this packet, or return an error, if necessary.
765      */
766     if (mode == NPMODE_ERROR) {
767 	error = ENETDOWN;
768 	goto bad;
769     }
770     if (mode == NPMODE_DROP) {
771 	error = 0;
772 	goto bad;
773     }
774 
775     /*
776      * Add PPP header.  If no space in first mbuf, allocate another.
777      * (This assumes M_LEADINGSPACE is always 0 for a cluster mbuf.)
778      */
779     M_PREPEND(m0, PPP_HDRLEN, M_DONTWAIT);
780     if (m0 == 0) {
781 	error = ENOBUFS;
782 	goto bad;
783     }
784 
785     cp = mtod(m0, u_char *);
786     *cp++ = address;
787     *cp++ = control;
788     *cp++ = protocol >> 8;
789     *cp++ = protocol & 0xff;
790 
791     if ((m0->m_flags & M_PKTHDR) == 0)
792 	    panic("mbuf packet without packet header!");
793     len = m0->m_pkthdr.len;
794 
795     if (sc->sc_flags & SC_LOG_OUTPKT) {
796 	printf("%s output: ", ifp->if_xname);
797 	pppdumpm(m0);
798     }
799 
800     if ((protocol & 0x8000) == 0) {
801 #if NBPFILTER > 0
802 	/*
803 	 * Apply the pass and active filters to the packet,
804 	 * but only if it is a data packet.
805 	 */
806 	*mtod(m0, u_char *) = 1;	/* indicates outbound */
807 	if (sc->sc_pass_filt.bf_insns != 0
808 	    && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m0,
809 			  len, 0) == 0) {
810 	    error = 0;		/* drop this packet */
811 	    goto bad;
812 	}
813 
814 	/*
815 	 * Update the time we sent the most recent packet.
816 	 */
817 	if (sc->sc_active_filt.bf_insns == 0
818 	    || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m0, len, 0))
819 	    sc->sc_last_sent = time_second;
820 
821 	*mtod(m0, u_char *) = address;
822 #else
823 	/*
824 	 * Update the time we sent the most recent packet.
825 	 */
826 	sc->sc_last_sent = time_second;
827 #endif
828     }
829 
830 #if NBPFILTER > 0
831     /*
832      * See if bpf wants to look at the packet.
833      */
834     if (sc->sc_bpf)
835 	bpf_mtap(sc->sc_bpf, m0, BPF_DIRECTION_OUT);
836 #endif
837 
838     /*
839      * Put the packet on the appropriate queue.
840      */
841     s = splsoftnet();
842     if (mode == NPMODE_QUEUE) {
843 	/* XXX we should limit the number of packets on this queue */
844 	*sc->sc_npqtail = m0;
845 	m0->m_nextpkt = NULL;
846 	sc->sc_npqtail = &m0->m_nextpkt;
847     } else {
848 	if ((m0->m_flags & M_HIGHPRI)
849 #ifdef ALTQ
850 	    && ALTQ_IS_ENABLED(&sc->sc_if.if_snd) == 0
851 #endif
852 	    ) {
853 	    ifq = &sc->sc_fastq;
854 	    if (IF_QFULL(ifq) && dst->sa_family != AF_UNSPEC) {
855 		IF_DROP(ifq);
856 		m_freem(m0);
857 		error = ENOBUFS;
858 	    }
859 	    else {
860 		IF_ENQUEUE(ifq, m0);
861 		error = 0;
862 	    }
863 	} else
864 	    IFQ_ENQUEUE(&sc->sc_if.if_snd, m0, NULL, error);
865 	if (error) {
866 	    splx(s);
867 	    sc->sc_if.if_oerrors++;
868 	    sc->sc_stats.ppp_oerrors++;
869 	    return (error);
870 	}
871 	(*sc->sc_start)(sc);
872     }
873     ifp->if_opackets++;
874     ifp->if_obytes += len;
875 
876     splx(s);
877     return (0);
878 
879 bad:
880     m_freem(m0);
881     return (error);
882 }
883 
884 /*
885  * After a change in the NPmode for some NP, move packets from the
886  * npqueue to the send queue or the fast queue as appropriate.
887  * Should be called at splsoftnet.
888  */
889 static void
890 ppp_requeue(sc)
891     struct ppp_softc *sc;
892 {
893     struct mbuf *m, **mpp;
894     struct ifqueue *ifq;
895     enum NPmode mode;
896     int error;
897 
898     splsoftassert(IPL_SOFTNET);
899 
900     for (mpp = &sc->sc_npqueue; (m = *mpp) != NULL; ) {
901 	switch (PPP_PROTOCOL(mtod(m, u_char *))) {
902 	case PPP_IP:
903 	    mode = sc->sc_npmode[NP_IP];
904 	    break;
905 	default:
906 	    mode = NPMODE_PASS;
907 	}
908 
909 	switch (mode) {
910 	case NPMODE_PASS:
911 	    /*
912 	     * This packet can now go on one of the queues to be sent.
913 	     */
914 	    *mpp = m->m_nextpkt;
915 	    m->m_nextpkt = NULL;
916 	    if ((m->m_flags & M_HIGHPRI)
917 #ifdef ALTQ
918 		&& ALTQ_IS_ENABLED(&sc->sc_if.if_snd) == 0
919 #endif
920 		) {
921 		ifq = &sc->sc_fastq;
922 		if (IF_QFULL(ifq)) {
923 		    IF_DROP(ifq);
924 		    m_freem(m);
925 		    error = ENOBUFS;
926 		}
927 		else {
928 		    IF_ENQUEUE(ifq, m);
929 		    error = 0;
930 		}
931 	    } else
932 		IFQ_ENQUEUE(&sc->sc_if.if_snd, m, NULL, error);
933 	    if (error) {
934 		sc->sc_if.if_oerrors++;
935 		sc->sc_stats.ppp_oerrors++;
936 	    }
937 	    break;
938 
939 	case NPMODE_DROP:
940 	case NPMODE_ERROR:
941 	    *mpp = m->m_nextpkt;
942 	    m_freem(m);
943 	    break;
944 
945 	case NPMODE_QUEUE:
946 	    mpp = &m->m_nextpkt;
947 	    break;
948 	}
949     }
950     sc->sc_npqtail = mpp;
951 }
952 
953 /*
954  * Transmitter has finished outputting some stuff;
955  * remember to call sc->sc_start later at splsoftnet.
956  */
957 void
958 ppp_restart(sc)
959     struct ppp_softc *sc;
960 {
961     int s = splnet();
962 
963     sc->sc_flags &= ~SC_TBUSY;
964     schednetisr(NETISR_PPP);
965     splx(s);
966 }
967 
968 /*
969  * Get a packet to send.  This procedure is intended to be called at
970  * splsoftnet, since it may involve time-consuming operations such as
971  * applying VJ compression, packet compression, address/control and/or
972  * protocol field compression to the packet.
973  */
974 struct mbuf *
975 ppp_dequeue(sc)
976     struct ppp_softc *sc;
977 {
978     struct mbuf *m, *mp;
979     u_char *cp;
980     int address, control, protocol;
981 
982     /*
983      * Grab a packet to send: first try the fast queue, then the
984      * normal queue.
985      */
986     IF_DEQUEUE(&sc->sc_fastq, m);
987     if (m == NULL)
988 	IFQ_DEQUEUE(&sc->sc_if.if_snd, m);
989     if (m == NULL)
990       return NULL;
991 
992     ++sc->sc_stats.ppp_opackets;
993 
994     /*
995      * Extract the ppp header of the new packet.
996      * The ppp header will be in one mbuf.
997      */
998     cp = mtod(m, u_char *);
999     address = PPP_ADDRESS(cp);
1000     control = PPP_CONTROL(cp);
1001     protocol = PPP_PROTOCOL(cp);
1002 
1003     switch (protocol) {
1004     case PPP_IP:
1005 #ifdef VJC
1006 	/*
1007 	 * If the packet is a TCP/IP packet, see if we can compress it.
1008 	 */
1009 	if ((sc->sc_flags & SC_COMP_TCP) && sc->sc_comp != NULL) {
1010 	    struct ip *ip;
1011 	    int type;
1012 
1013 	    mp = m;
1014 	    ip = (struct ip *) (cp + PPP_HDRLEN);
1015 	    if (mp->m_len <= PPP_HDRLEN) {
1016 		mp = mp->m_next;
1017 		if (mp == NULL)
1018 		    break;
1019 		ip = mtod(mp, struct ip *);
1020 	    }
1021 	    /* this code assumes the IP/TCP header is in one non-shared mbuf */
1022 	    if (ip->ip_p == IPPROTO_TCP) {
1023 		type = sl_compress_tcp(mp, ip, sc->sc_comp,
1024 				       !(sc->sc_flags & SC_NO_TCP_CCID));
1025 		switch (type) {
1026 		case TYPE_UNCOMPRESSED_TCP:
1027 		    protocol = PPP_VJC_UNCOMP;
1028 		    break;
1029 		case TYPE_COMPRESSED_TCP:
1030 		    protocol = PPP_VJC_COMP;
1031 		    cp = mtod(m, u_char *);
1032 		    cp[0] = address;	/* header has moved */
1033 		    cp[1] = control;
1034 		    cp[2] = 0;
1035 		    break;
1036 		}
1037 		cp[3] = protocol;	/* update protocol in PPP header */
1038 	    }
1039 	}
1040 #endif	/* VJC */
1041 	break;
1042 
1043 #ifdef PPP_COMPRESS
1044     case PPP_CCP:
1045 	ppp_ccp(sc, m, 0);
1046 	break;
1047 #endif	/* PPP_COMPRESS */
1048     }
1049 
1050 #ifdef PPP_COMPRESS
1051     if (protocol != PPP_LCP && protocol != PPP_CCP
1052 	&& sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
1053 	struct mbuf *mcomp = NULL;
1054 	int slen;
1055 
1056 	slen = 0;
1057 	for (mp = m; mp != NULL; mp = mp->m_next)
1058 	    slen += mp->m_len;
1059 	(*sc->sc_xcomp->compress)
1060 	    (sc->sc_xc_state, &mcomp, m, slen,
1061 	     (sc->sc_flags & SC_CCP_UP ? sc->sc_if.if_mtu + PPP_HDRLEN : 0));
1062 	if (mcomp != NULL) {
1063 	    if (sc->sc_flags & SC_CCP_UP) {
1064 		/* Send the compressed packet instead of the original. */
1065 		m_freem(m);
1066 		m = mcomp;
1067 		cp = mtod(m, u_char *);
1068 		protocol = cp[3];
1069 	    } else {
1070 		/* Can't transmit compressed packets until CCP is up. */
1071 		m_freem(mcomp);
1072 	    }
1073 	}
1074     }
1075 #endif	/* PPP_COMPRESS */
1076 
1077     /*
1078      * Compress the address/control and protocol, if possible.
1079      */
1080     if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
1081 	control == PPP_UI && protocol != PPP_ALLSTATIONS &&
1082 	protocol != PPP_LCP) {
1083 	/* can compress address/control */
1084 	m->m_data += 2;
1085 	m->m_len -= 2;
1086     }
1087     if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
1088 	/* can compress protocol */
1089 	if (mtod(m, u_char *) == cp) {
1090 	    cp[2] = cp[1];	/* move address/control up */
1091 	    cp[1] = cp[0];
1092 	}
1093 	++m->m_data;
1094 	--m->m_len;
1095     }
1096 
1097     return m;
1098 }
1099 
1100 /*
1101  * Software interrupt routine, called at splsoftnet.
1102  */
1103 void
1104 pppintr()
1105 {
1106     struct ppp_softc *sc;
1107     int s, s2;
1108     struct mbuf *m;
1109 
1110     splsoftassert(IPL_SOFTNET);
1111 
1112     s = splsoftnet();	/* XXX - what's the point of this? see comment above */
1113     LIST_FOREACH(sc, &ppp_softc_list, sc_list) {
1114 	if (!(sc->sc_flags & SC_TBUSY)
1115 	    && (IFQ_IS_EMPTY(&sc->sc_if.if_snd) == 0 || sc->sc_fastq.ifq_head)) {
1116 	    s2 = splnet();
1117 	    sc->sc_flags |= SC_TBUSY;
1118 	    splx(s2);
1119 	    (*sc->sc_start)(sc);
1120 	}
1121 	while (sc->sc_rawq.ifq_head) {
1122 	    s2 = splnet();
1123 	    IF_DEQUEUE(&sc->sc_rawq, m);
1124 	    splx(s2);
1125 	    if (m == NULL)
1126 		break;
1127 	    ppp_inproc(sc, m);
1128 	}
1129     }
1130     splx(s);
1131 }
1132 
1133 #ifdef PPP_COMPRESS
1134 /*
1135  * Handle a CCP packet.  `rcvd' is 1 if the packet was received,
1136  * 0 if it is about to be transmitted.
1137  */
1138 static void
1139 ppp_ccp(sc, m, rcvd)
1140     struct ppp_softc *sc;
1141     struct mbuf *m;
1142     int rcvd;
1143 {
1144     u_char *dp, *ep;
1145     struct mbuf *mp;
1146     int slen, s;
1147 
1148     /*
1149      * Get a pointer to the data after the PPP header.
1150      */
1151     if (m->m_len <= PPP_HDRLEN) {
1152 	mp = m->m_next;
1153 	if (mp == NULL)
1154 	    return;
1155 	dp = (mp != NULL)? mtod(mp, u_char *): NULL;
1156     } else {
1157 	mp = m;
1158 	dp = mtod(mp, u_char *) + PPP_HDRLEN;
1159     }
1160 
1161     ep = mtod(mp, u_char *) + mp->m_len;
1162     if (dp + CCP_HDRLEN > ep)
1163 	return;
1164     slen = CCP_LENGTH(dp);
1165     if (dp + slen > ep) {
1166 	if (sc->sc_flags & SC_DEBUG)
1167 	    printf("if_ppp/ccp: not enough data in mbuf (%p+%x > %p+%x)\n",
1168 		dp, slen, mtod(mp, u_char *), mp->m_len);
1169 	return;
1170     }
1171 
1172     switch (CCP_CODE(dp)) {
1173     case CCP_CONFREQ:
1174     case CCP_TERMREQ:
1175     case CCP_TERMACK:
1176 	/* CCP must be going down - disable compression */
1177 	if (sc->sc_flags & SC_CCP_UP) {
1178 	    s = splnet();
1179 	    sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
1180 	    splx(s);
1181 	}
1182 	break;
1183 
1184     case CCP_CONFACK:
1185 	if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
1186 	    && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
1187 	    && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
1188 	    if (!rcvd) {
1189 		/* we're agreeing to send compressed packets. */
1190 		if (sc->sc_xc_state != NULL
1191 		    && (*sc->sc_xcomp->comp_init)
1192 			(sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1193 			 sc->sc_unit, 0, sc->sc_flags & SC_DEBUG)) {
1194 		    s = splnet();
1195 		    sc->sc_flags |= SC_COMP_RUN;
1196 		    splx(s);
1197 		}
1198 	    } else {
1199 		/* peer is agreeing to send compressed packets. */
1200 		if (sc->sc_rc_state != NULL
1201 		    && (*sc->sc_rcomp->decomp_init)
1202 			(sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1203 			 sc->sc_unit, 0, sc->sc_mru,
1204 			 sc->sc_flags & SC_DEBUG)) {
1205 		    s = splnet();
1206 		    sc->sc_flags |= SC_DECOMP_RUN;
1207 		    sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1208 		    splx(s);
1209 		}
1210 	    }
1211 	}
1212 	break;
1213 
1214     case CCP_RESETACK:
1215 	if (sc->sc_flags & SC_CCP_UP) {
1216 	    if (!rcvd) {
1217 		if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN))
1218 		    (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
1219 	    } else {
1220 		if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1221 		    (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
1222 		    s = splnet();
1223 		    sc->sc_flags &= ~SC_DC_ERROR;
1224 		    splx(s);
1225 		}
1226 	    }
1227 	}
1228 	break;
1229     }
1230 }
1231 
1232 /*
1233  * CCP is down; free (de)compressor state if necessary.
1234  */
1235 static void
1236 ppp_ccp_closed(sc)
1237     struct ppp_softc *sc;
1238 {
1239     if (sc->sc_xc_state) {
1240 	(*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
1241 	sc->sc_xc_state = NULL;
1242     }
1243     if (sc->sc_rc_state) {
1244 	(*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
1245 	sc->sc_rc_state = NULL;
1246     }
1247 }
1248 #endif /* PPP_COMPRESS */
1249 
1250 /*
1251  * PPP packet input routine.
1252  * The caller has checked and removed the FCS and has inserted
1253  * the address/control bytes and the protocol high byte if they
1254  * were omitted.
1255  */
1256 void
1257 ppppktin(sc, m, lost)
1258     struct ppp_softc *sc;
1259     struct mbuf *m;
1260     int lost;
1261 {
1262     int s = splnet();
1263 
1264     if (lost)
1265 	m->m_flags |= M_ERRMARK;
1266     IF_ENQUEUE(&sc->sc_rawq, m);
1267     schednetisr(NETISR_PPP);
1268     splx(s);
1269 }
1270 
1271 /*
1272  * Process a received PPP packet, doing decompression as necessary.
1273  * Should be called at splsoftnet.
1274  */
1275 #define COMPTYPE(proto)	((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
1276 			 TYPE_UNCOMPRESSED_TCP)
1277 
1278 static void
1279 ppp_inproc(sc, m)
1280     struct ppp_softc *sc;
1281     struct mbuf *m;
1282 {
1283     struct ifnet *ifp = &sc->sc_if;
1284     struct ifqueue *inq;
1285     int s, ilen, xlen, proto, rv;
1286     u_char *cp, adrs, ctrl;
1287     struct mbuf *mp, *dmp = NULL;
1288     u_char *iphdr;
1289     u_int hlen;
1290 
1291     sc->sc_stats.ppp_ipackets++;
1292 
1293     if (sc->sc_flags & SC_LOG_INPKT) {
1294 	ilen = 0;
1295 	for (mp = m; mp != NULL; mp = mp->m_next)
1296 	    ilen += mp->m_len;
1297 	printf("%s: got %d bytes\n", ifp->if_xname, ilen);
1298 	pppdumpm(m);
1299     }
1300 
1301     cp = mtod(m, u_char *);
1302     adrs = PPP_ADDRESS(cp);
1303     ctrl = PPP_CONTROL(cp);
1304     proto = PPP_PROTOCOL(cp);
1305 
1306     if (m->m_flags & M_ERRMARK) {
1307 	m->m_flags &= ~M_ERRMARK;
1308 	s = splnet();
1309 	sc->sc_flags |= SC_VJ_RESET;
1310 	splx(s);
1311     }
1312 
1313 #ifdef PPP_COMPRESS
1314     /*
1315      * Decompress this packet if necessary, update the receiver's
1316      * dictionary, or take appropriate action on a CCP packet.
1317      */
1318     if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
1319 	&& !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
1320 	/* decompress this packet */
1321 	rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
1322 	if (rv == DECOMP_OK) {
1323 	    m_freem(m);
1324 	    if (dmp == NULL) {
1325 		/* no error, but no decompressed packet produced */
1326 		return;
1327 	    }
1328 	    m = dmp;
1329 	    cp = mtod(m, u_char *);
1330 	    proto = PPP_PROTOCOL(cp);
1331 
1332 	} else {
1333 	    /*
1334 	     * An error has occurred in decompression.
1335 	     * Pass the compressed packet up to pppd, which may take
1336 	     * CCP down or issue a Reset-Req.
1337 	     */
1338 	    if (sc->sc_flags & SC_DEBUG)
1339 		printf("%s: decompress failed %d\n", ifp->if_xname, rv);
1340 	    s = splnet();
1341 	    sc->sc_flags |= SC_VJ_RESET;
1342 	    if (rv == DECOMP_ERROR)
1343 		sc->sc_flags |= SC_DC_ERROR;
1344 	    else
1345 		sc->sc_flags |= SC_DC_FERROR;
1346 	    splx(s);
1347 	}
1348 
1349     } else {
1350 	if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1351 	    (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
1352 	}
1353 	if (proto == PPP_CCP) {
1354 	    ppp_ccp(sc, m, 1);
1355 	}
1356     }
1357 #endif
1358 
1359     ilen = 0;
1360     for (mp = m; mp != NULL; mp = mp->m_next)
1361 	ilen += mp->m_len;
1362 
1363 #ifdef VJC
1364     if (sc->sc_flags & SC_VJ_RESET) {
1365 	/*
1366 	 * If we've missed a packet, we must toss subsequent compressed
1367 	 * packets which don't have an explicit connection ID.
1368 	 */
1369 	if (sc->sc_comp)
1370 	    sl_uncompress_tcp(NULL, 0, TYPE_ERROR, sc->sc_comp);
1371 	s = splnet();
1372 	sc->sc_flags &= ~SC_VJ_RESET;
1373 	splx(s);
1374     }
1375 
1376     /*
1377      * See if we have a VJ-compressed packet to uncompress.
1378      */
1379     if (proto == PPP_VJC_COMP) {
1380 	if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1381 	    goto bad;
1382 
1383 	xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1384 				      ilen - PPP_HDRLEN, TYPE_COMPRESSED_TCP,
1385 				      sc->sc_comp, &iphdr, &hlen);
1386 
1387 	if (xlen <= 0) {
1388 	    if (sc->sc_flags & SC_DEBUG)
1389 		printf("%s: VJ uncompress failed on type comp\n",
1390 		    ifp->if_xname);
1391 	    goto bad;
1392 	}
1393 
1394 	/* Copy the PPP and IP headers into a new mbuf. */
1395 	MGETHDR(mp, M_DONTWAIT, MT_DATA);
1396 	if (mp == NULL)
1397 	    goto bad;
1398 	mp->m_len = 0;
1399 	mp->m_next = NULL;
1400 	if (hlen + PPP_HDRLEN > MHLEN) {
1401 	    MCLGET(mp, M_DONTWAIT);
1402 	    if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) {
1403 		m_freem(mp);
1404 		goto bad;	/* lose if big headers and no clusters */
1405 	    }
1406 	}
1407 	if (m->m_flags & M_PKTHDR)
1408 		M_MOVE_HDR(mp, m);
1409 	cp = mtod(mp, u_char *);
1410 	cp[0] = adrs;
1411 	cp[1] = ctrl;
1412 	cp[2] = 0;
1413 	cp[3] = PPP_IP;
1414 	proto = PPP_IP;
1415 	bcopy(iphdr, cp + PPP_HDRLEN, hlen);
1416 	mp->m_len = hlen + PPP_HDRLEN;
1417 
1418 	/*
1419 	 * Trim the PPP and VJ headers off the old mbuf
1420 	 * and stick the new and old mbufs together.
1421 	 */
1422 	m->m_data += PPP_HDRLEN + xlen;
1423 	m->m_len -= PPP_HDRLEN + xlen;
1424 	if (m->m_len <= M_TRAILINGSPACE(mp)) {
1425 	    bcopy(mtod(m, u_char *), mtod(mp, u_char *) + mp->m_len, m->m_len);
1426 	    mp->m_len += m->m_len;
1427 	    MFREE(m, mp->m_next);
1428 	} else
1429 	    mp->m_next = m;
1430 	m = mp;
1431 	ilen += hlen - xlen;
1432 
1433     } else if (proto == PPP_VJC_UNCOMP) {
1434 	if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1435 	    goto bad;
1436 
1437 	xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1438 				      ilen - PPP_HDRLEN, TYPE_UNCOMPRESSED_TCP,
1439 				      sc->sc_comp, &iphdr, &hlen);
1440 
1441 	if (xlen < 0) {
1442 	    if (sc->sc_flags & SC_DEBUG)
1443 		printf("%s: VJ uncompress failed on type uncomp\n",
1444 		    ifp->if_xname);
1445 	    goto bad;
1446 	}
1447 
1448 	proto = PPP_IP;
1449 	cp[3] = PPP_IP;
1450     }
1451 #endif /* VJC */
1452 
1453     /*
1454      * If the packet will fit in a header mbuf, don't waste a
1455      * whole cluster on it.
1456      */
1457     if (ilen <= MHLEN && M_IS_CLUSTER(m)) {
1458 	MGETHDR(mp, M_DONTWAIT, MT_DATA);
1459 	if (mp != NULL) {
1460 	    m_copydata(m, 0, ilen, mtod(mp, caddr_t));
1461 	    m_freem(m);
1462 	    m = mp;
1463 	    m->m_len = ilen;
1464 	}
1465     }
1466     m->m_pkthdr.len = ilen;
1467     m->m_pkthdr.rcvif = ifp;
1468 
1469     if ((proto & 0x8000) == 0) {
1470 #if NBPFILTER > 0
1471 	/*
1472 	 * See whether we want to pass this packet, and
1473 	 * if it counts as link activity.
1474 	 */
1475 	adrs = *mtod(m, u_char *);	/* save address field */
1476 	*mtod(m, u_char *) = 0;		/* indicate inbound */
1477 	if (sc->sc_pass_filt.bf_insns != 0
1478 	    && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m,
1479 			  ilen, 0) == 0) {
1480 	    /* drop this packet */
1481 	    m_freem(m);
1482 	    return;
1483 	}
1484 	if (sc->sc_active_filt.bf_insns == 0
1485 	    || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m, ilen, 0))
1486 	    sc->sc_last_recv = time_second;
1487 
1488 	*mtod(m, u_char *) = adrs;
1489 #else
1490 	/*
1491 	 * Record the time that we received this packet.
1492 	 */
1493 	sc->sc_last_recv = time_second;
1494 #endif
1495     }
1496 
1497 #if NBPFILTER > 0
1498     /* See if bpf wants to look at the packet. */
1499     if (sc->sc_bpf)
1500 	bpf_mtap(sc->sc_bpf, m, BPF_DIRECTION_IN);
1501 #endif
1502 
1503     rv = 0;
1504     switch (proto) {
1505 #ifdef INET
1506     case PPP_IP:
1507 	/*
1508 	 * IP packet - take off the ppp header and pass it up to IP.
1509 	 */
1510 	if ((ifp->if_flags & IFF_UP) == 0
1511 	    || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
1512 	    /* interface is down - drop the packet. */
1513 	    m_freem(m);
1514 	    return;
1515 	}
1516 	m->m_pkthdr.len -= PPP_HDRLEN;
1517 	m->m_data += PPP_HDRLEN;
1518 	m->m_len -= PPP_HDRLEN;
1519 	schednetisr(NETISR_IP);
1520 	inq = &ipintrq;
1521 	break;
1522 #endif
1523 
1524     default:
1525 	/*
1526 	 * Some other protocol - place on input queue for read().
1527 	 */
1528 	inq = &sc->sc_inq;
1529 	rv = 1;
1530 	break;
1531     }
1532 
1533     /*
1534      * Put the packet on the appropriate input queue.
1535      */
1536     s = splnet();
1537     if (IF_QFULL(inq)) {
1538 	IF_DROP(inq);
1539 	splx(s);
1540 	if (sc->sc_flags & SC_DEBUG)
1541 	    printf("%s: input queue full\n", ifp->if_xname);
1542 	ifp->if_iqdrops++;
1543 	if (!inq->ifq_congestion)
1544 		if_congestion(inq);
1545 	goto bad;
1546     }
1547     IF_ENQUEUE(inq, m);
1548     splx(s);
1549     ifp->if_ipackets++;
1550     ifp->if_ibytes += ilen;
1551 
1552     if (rv)
1553 	(*sc->sc_ctlp)(sc);
1554 
1555     return;
1556 
1557  bad:
1558     m_freem(m);
1559     sc->sc_if.if_ierrors++;
1560     sc->sc_stats.ppp_ierrors++;
1561 }
1562 
1563 #define MAX_DUMP_BYTES	128
1564 
1565 static void
1566 pppdumpm(m0)
1567     struct mbuf *m0;
1568 {
1569     char buf[3*MAX_DUMP_BYTES+4];
1570     char *bp = buf;
1571     struct mbuf *m;
1572     static char digits[] = "0123456789abcdef";
1573 
1574     for (m = m0; m; m = m->m_next) {
1575 	int l = m->m_len;
1576 	u_char *rptr = (u_char *)m->m_data;
1577 
1578 	while (l--) {
1579 	    if (bp > buf + sizeof(buf) - 4)
1580 		goto done;
1581 	    *bp++ = digits[*rptr >> 4]; /* convert byte to ascii hex */
1582 	    *bp++ = digits[*rptr++ & 0xf];
1583 	}
1584 
1585 	if (m->m_next) {
1586 	    if (bp > buf + sizeof(buf) - 3)
1587 		goto done;
1588 	    *bp++ = '|';
1589 	} else
1590 	    *bp++ = ' ';
1591     }
1592 done:
1593     if (m)
1594 	*bp++ = '>';
1595     *bp = 0;
1596     printf("%s\n", buf);
1597 }
1598 
1599 #ifdef ALTQ
1600 /*
1601  * a wrapper to transmit a packet from if_start since ALTQ uses
1602  * if_start to send a packet.
1603  */
1604 static void
1605 ppp_ifstart(ifp)
1606 	struct ifnet *ifp;
1607 {
1608 	struct ppp_softc *sc;
1609 
1610 	sc = ifp->if_softc;
1611 	(*sc->sc_start)(sc);
1612 }
1613 #endif
1614 
1615 #endif	/* NPPP > 0 */
1616