1 /*	$OpenBSD: if_ppp.c,v 1.41 2004/04/25 18:50:01 henning Exp $	*/
2 /*	$NetBSD: if_ppp.c,v 1.39 1997/05/17 21:11:59 christos Exp $	*/
3 
4 /*
5  * if_ppp.c - Point-to-Point Protocol (PPP) Asynchronous driver.
6  *
7  * Copyright (c) 1984-2000 Carnegie Mellon University. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in
18  *    the documentation and/or other materials provided with the
19  *    distribution.
20  *
21  * 3. The name "Carnegie Mellon University" must not be used to
22  *    endorse or promote products derived from this software without
23  *    prior written permission. For permission or any legal
24  *    details, please contact
25  *      Office of Technology Transfer
26  *      Carnegie Mellon University
27  *      5000 Forbes Avenue
28  *      Pittsburgh, PA  15213-3890
29  *      (412) 268-4387, fax: (412) 268-7395
30  *      tech-transfer@andrew.cmu.edu
31  *
32  * 4. Redistributions of any form whatsoever must retain the following
33  *    acknowledgment:
34  *    "This product includes software developed by Computing Services
35  *     at Carnegie Mellon University (http://www.cmu.edu/computing/)."
36  *
37  * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
38  * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
39  * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
40  * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
41  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
42  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
43  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
44  *
45  * Based on:
46  *	@(#)if_sl.c	7.6.1.2 (Berkeley) 2/15/89
47  *
48  * Copyright (c) 1987, 1989, 1992, 1993
49  *	The Regents of the University of California.  All rights reserved.
50  *
51  * Redistribution and use in source and binary forms, with or without
52  * modification, are permitted provided that the following conditions
53  * are met:
54  * 1. Redistributions of source code must retain the above copyright
55  *    notice, this list of conditions and the following disclaimer.
56  * 2. Redistributions in binary form must reproduce the above copyright
57  *    notice, this list of conditions and the following disclaimer in the
58  *    documentation and/or other materials provided with the distribution.
59  * 3. Neither the name of the University nor the names of its contributors
60  *    may be used to endorse or promote products derived from this software
61  *    without specific prior written permission.
62  *
63  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
64  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
65  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
66  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
67  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
68  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
69  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
70  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
71  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
72  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
73  * SUCH DAMAGE.
74  *
75  * Serial Line interface
76  *
77  * Rick Adams
78  * Center for Seismic Studies
79  * 1300 N 17th Street, Suite 1450
80  * Arlington, Virginia 22209
81  * (703)276-7900
82  * rick@seismo.ARPA
83  * seismo!rick
84  *
85  * Pounded on heavily by Chris Torek (chris@mimsy.umd.edu, umcp-cs!chris).
86  * Converted to 4.3BSD Beta by Chris Torek.
87  * Other changes made at Berkeley, based in part on code by Kirk Smith.
88  *
89  * Converted to 4.3BSD+ 386BSD by Brad Parker (brad@cayman.com)
90  * Added VJ tcp header compression; more unified ioctls
91  *
92  * Extensively modified by Paul Mackerras (paulus@cs.anu.edu.au).
93  * Cleaned up a lot of the mbuf-related code to fix bugs that
94  * caused system crashes and packet corruption.  Changed pppstart
95  * so that it doesn't just give up with a collision if the whole
96  * packet doesn't fit in the output ring buffer.
97  *
98  * Added priority queueing for interactive IP packets, following
99  * the model of if_sl.c, plus hooks for bpf.
100  * Paul Mackerras (paulus@cs.anu.edu.au).
101  */
102 
103 /* from if_sl.c,v 1.11 84/10/04 12:54:47 rick Exp */
104 /* from NetBSD: if_ppp.c,v 1.15.2.2 1994/07/28 05:17:58 cgd Exp */
105 
106 #include "ppp.h"
107 #if NPPP > 0
108 
109 #define VJC
110 #define PPP_COMPRESS
111 
112 #include <sys/param.h>
113 #include <sys/proc.h>
114 #include <sys/mbuf.h>
115 #include <sys/socket.h>
116 #include <sys/ioctl.h>
117 #include <sys/kernel.h>
118 #include <sys/systm.h>
119 #include <sys/time.h>
120 #include <sys/malloc.h>
121 
122 #include <net/if.h>
123 #include <net/if_types.h>
124 #include <net/netisr.h>
125 #include <net/route.h>
126 #include <net/bpf.h>
127 
128 #if INET
129 #include <netinet/in.h>
130 #include <netinet/in_systm.h>
131 #include <netinet/in_var.h>
132 #include <netinet/ip.h>
133 #else
134 #ifdef _KERNEL
135 #ifdef VJC
136 #error ppp device with VJC assumes INET
137 #endif
138 #endif
139 #endif
140 
141 #include "bpfilter.h"
142 #if NBPFILTER > 0
143 #include <sys/time.h>
144 #include <net/bpf.h>
145 #endif
146 
147 #ifdef VJC
148 #include <net/slcompress.h>
149 #endif
150 
151 #include <net/ppp_defs.h>
152 #include <net/if_ppp.h>
153 #include <net/if_pppvar.h>
154 #include <machine/cpu.h>
155 
156 #ifdef PPP_COMPRESS
157 #define PACKETPTR	struct mbuf *
158 #include <net/ppp-comp.h>
159 #endif
160 
161 static int	pppsioctl(struct ifnet *, u_long, caddr_t);
162 static void	ppp_requeue(struct ppp_softc *);
163 static void	ppp_ccp(struct ppp_softc *, struct mbuf *m, int rcvd);
164 static void	ppp_ccp_closed(struct ppp_softc *);
165 static void	ppp_inproc(struct ppp_softc *, struct mbuf *);
166 static void	pppdumpm(struct mbuf *m0);
167 #ifdef ALTQ
168 static void	ppp_ifstart(struct ifnet *ifp);
169 #endif
170 int		ppp_clone_create(struct if_clone *, int);
171 int		ppp_clone_destroy(struct ifnet *);
172 
173 /*
174  * Some useful mbuf macros not in mbuf.h.
175  */
176 #define M_IS_CLUSTER(m)	((m)->m_flags & M_EXT)
177 
178 #define M_DATASTART(m)	\
179 	(M_IS_CLUSTER(m) ? (m)->m_ext.ext_buf : \
180 	    (m)->m_flags & M_PKTHDR ? (m)->m_pktdat : (m)->m_dat)
181 
182 #define M_DATASIZE(m)	\
183 	(M_IS_CLUSTER(m) ? (m)->m_ext.ext_size : \
184 	    (m)->m_flags & M_PKTHDR ? MHLEN: MLEN)
185 
186 /*
187  * We steal two bits in the mbuf m_flags, to mark high-priority packets
188  * for output, and received packets following lost/corrupted packets.
189  */
190 #define M_HIGHPRI	0x2000	/* output packet for sc_fastq */
191 #define M_ERRMARK	0x4000	/* steal a bit in mbuf m_flags */
192 
193 
194 #ifdef PPP_COMPRESS
195 /*
196  * List of compressors we know about.
197  * We leave some space so maybe we can modload compressors.
198  */
199 
200 extern struct compressor ppp_bsd_compress;
201 extern struct compressor ppp_deflate, ppp_deflate_draft;
202 
203 struct compressor *ppp_compressors[8] = {
204 #if DO_BSD_COMPRESS && defined(PPP_BSDCOMP)
205     &ppp_bsd_compress,
206 #endif
207 #if DO_DEFLATE && defined(PPP_DEFLATE)
208     &ppp_deflate,
209     &ppp_deflate_draft,
210 #endif
211     NULL
212 };
213 #endif /* PPP_COMPRESS */
214 
215 LIST_HEAD(, ppp_softc) ppp_softc_list;
216 struct if_clone ppp_cloner =
217     IF_CLONE_INITIALIZER("ppp", ppp_clone_create, ppp_clone_destroy);
218 
219 /*
220  * Called from boot code to establish ppp interfaces.
221  */
222 void
pppattach()223 pppattach()
224 {
225     LIST_INIT(&ppp_softc_list);
226     if_clone_attach(&ppp_cloner);
227 }
228 
229 int
ppp_clone_create(ifc,unit)230 ppp_clone_create(ifc, unit)
231     struct if_clone *ifc;
232     int unit;
233 {
234     extern int ifqmaxlen;
235     struct ppp_softc *sc;
236     int s;
237 
238     sc = malloc(sizeof(*sc), M_DEVBUF, M_NOWAIT);
239     if (!sc)
240 	return (ENOMEM);
241     bzero(sc, sizeof(*sc));
242 
243     sc->sc_unit = unit;
244     snprintf(sc->sc_if.if_xname, sizeof sc->sc_if.if_xname, "%s%d",
245 	ifc->ifc_name, unit);
246     sc->sc_if.if_softc = sc;
247     sc->sc_if.if_mtu = PPP_MTU;
248     sc->sc_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
249     sc->sc_if.if_type = IFT_PPP;
250     sc->sc_if.if_hdrlen = PPP_HDRLEN;
251     sc->sc_if.if_ioctl = pppsioctl;
252     sc->sc_if.if_output = pppoutput;
253 #ifdef ALTQ
254     sc->sc_if.if_start = ppp_ifstart;
255 #endif
256     IFQ_SET_MAXLEN(&sc->sc_if.if_snd, ifqmaxlen);
257     sc->sc_inq.ifq_maxlen = ifqmaxlen;
258     sc->sc_fastq.ifq_maxlen = ifqmaxlen;
259     sc->sc_rawq.ifq_maxlen = ifqmaxlen;
260     IFQ_SET_READY(&sc->sc_if.if_snd);
261     if_attach(&sc->sc_if);
262     if_alloc_sadl(&sc->sc_if);
263 #if NBPFILTER > 0
264     bpfattach(&sc->sc_bpf, &sc->sc_if, DLT_PPP, PPP_HDRLEN);
265 #endif
266     s = splimp();
267     LIST_INSERT_HEAD(&ppp_softc_list, sc, sc_list);
268     splx(s);
269 
270     return (0);
271 }
272 
273 int
ppp_clone_destroy(ifp)274 ppp_clone_destroy(ifp)
275     struct ifnet *ifp;
276 {
277     struct ppp_softc *sc = ifp->if_softc;
278     int s;
279 
280     if (sc->sc_devp != NULL)
281 	return (EBUSY);
282 
283     s = splimp();
284     LIST_REMOVE(sc, sc_list);
285     splx(s);
286 
287 #if NBPFILTER > 0
288     bpfdetach(ifp);
289 #endif
290     if_detach(ifp);
291 
292     free(sc, M_DEVBUF);
293     return (0);
294 }
295 
296 /*
297  * Allocate a ppp interface unit and initialize it.
298  */
299 struct ppp_softc *
pppalloc(pid)300 pppalloc(pid)
301     pid_t pid;
302 {
303     int i;
304     struct ppp_softc *sc;
305 
306     LIST_FOREACH(sc, &ppp_softc_list, sc_list)
307 	if (sc->sc_xfer == pid) {
308 	    sc->sc_xfer = 0;
309 	    return sc;
310 	}
311     LIST_FOREACH(sc, &ppp_softc_list, sc_list)
312 	if (sc->sc_devp == NULL)
313 	    break;
314     if (sc == NULL)
315 	return NULL;
316 
317     sc->sc_flags = 0;
318     sc->sc_mru = PPP_MRU;
319     sc->sc_relinq = NULL;
320     bzero((char *)&sc->sc_stats, sizeof(sc->sc_stats));
321 #ifdef VJC
322     MALLOC(sc->sc_comp, struct slcompress *, sizeof(struct slcompress),
323 	   M_DEVBUF, M_NOWAIT);
324     if (sc->sc_comp)
325 	sl_compress_init(sc->sc_comp);
326 #endif
327 #ifdef PPP_COMPRESS
328     sc->sc_xc_state = NULL;
329     sc->sc_rc_state = NULL;
330 #endif /* PPP_COMPRESS */
331     for (i = 0; i < NUM_NP; ++i)
332 	sc->sc_npmode[i] = NPMODE_ERROR;
333     sc->sc_npqueue = NULL;
334     sc->sc_npqtail = &sc->sc_npqueue;
335     sc->sc_last_sent = sc->sc_last_recv = time.tv_sec;
336 
337     return sc;
338 }
339 
340 /*
341  * Deallocate a ppp unit.  Must be called at splsoftnet or higher.
342  */
343 void
pppdealloc(sc)344 pppdealloc(sc)
345     struct ppp_softc *sc;
346 {
347     struct mbuf *m;
348 
349     splassert(IPL_SOFTNET);
350 
351     if_down(&sc->sc_if);
352     sc->sc_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
353     sc->sc_devp = NULL;
354     sc->sc_xfer = 0;
355     for (;;) {
356 	IF_DEQUEUE(&sc->sc_rawq, m);
357 	if (m == NULL)
358 	    break;
359 	m_freem(m);
360     }
361     for (;;) {
362 	IF_DEQUEUE(&sc->sc_inq, m);
363 	if (m == NULL)
364 	    break;
365 	m_freem(m);
366     }
367     for (;;) {
368 	IF_DEQUEUE(&sc->sc_fastq, m);
369 	if (m == NULL)
370 	    break;
371 	m_freem(m);
372     }
373     while ((m = sc->sc_npqueue) != NULL) {
374 	sc->sc_npqueue = m->m_nextpkt;
375 	m_freem(m);
376     }
377     if (sc->sc_togo != NULL) {
378 	m_freem(sc->sc_togo);
379 	sc->sc_togo = NULL;
380     }
381 #ifdef PPP_COMPRESS
382     ppp_ccp_closed(sc);
383     sc->sc_xc_state = NULL;
384     sc->sc_rc_state = NULL;
385 #endif /* PPP_COMPRESS */
386     if (sc->sc_pass_filt.bf_insns != 0) {
387 	FREE(sc->sc_pass_filt.bf_insns, M_DEVBUF);
388 	sc->sc_pass_filt.bf_insns = 0;
389 	sc->sc_pass_filt.bf_len = 0;
390     }
391     if (sc->sc_active_filt.bf_insns != 0) {
392 	FREE(sc->sc_active_filt.bf_insns, M_DEVBUF);
393 	sc->sc_active_filt.bf_insns = 0;
394 	sc->sc_active_filt.bf_len = 0;
395     }
396 #ifdef VJC
397     if (sc->sc_comp != 0) {
398 	FREE(sc->sc_comp, M_DEVBUF);
399 	sc->sc_comp = 0;
400     }
401 #endif
402 }
403 
404 /*
405  * Ioctl routine for generic ppp devices.
406  */
407 int
pppioctl(sc,cmd,data,flag,p)408 pppioctl(sc, cmd, data, flag, p)
409     struct ppp_softc *sc;
410     u_long cmd;
411     caddr_t data;
412     int flag;
413     struct proc *p;
414 {
415     int s, error, flags, mru, npx;
416     u_int nb;
417     struct ppp_option_data *odp;
418     struct compressor **cp;
419     struct npioctl *npi;
420     time_t t;
421     struct bpf_program *bp, *nbp;
422     struct bpf_insn *newcode, *oldcode;
423     int newcodelen;
424 #ifdef	PPP_COMPRESS
425     u_char ccp_option[CCP_MAX_OPTION_LENGTH];
426 #endif
427 
428     switch (cmd) {
429     case FIONREAD:
430 	*(int *)data = sc->sc_inq.ifq_len;
431 	break;
432 
433     case PPPIOCGUNIT:
434 	*(int *)data = sc->sc_unit;	/* XXX */
435 	break;
436 
437     case PPPIOCGFLAGS:
438 	*(u_int *)data = sc->sc_flags;
439 	break;
440 
441     case PPPIOCSFLAGS:
442 	if ((error = suser(p, 0)) != 0)
443 	    return (error);
444 	flags = *(int *)data & SC_MASK;
445 	s = splsoftnet();
446 #ifdef PPP_COMPRESS
447 	if (sc->sc_flags & SC_CCP_OPEN && !(flags & SC_CCP_OPEN))
448 	    ppp_ccp_closed(sc);
449 #endif
450 	splimp();
451 	sc->sc_flags = (sc->sc_flags & ~SC_MASK) | flags;
452 	splx(s);
453 	break;
454 
455     case PPPIOCSMRU:
456 	if ((error = suser(p, 0)) != 0)
457 	    return (error);
458 	mru = *(int *)data;
459 	if (mru >= PPP_MRU && mru <= PPP_MAXMRU)
460 	    sc->sc_mru = mru;
461 	break;
462 
463     case PPPIOCGMRU:
464 	*(int *)data = sc->sc_mru;
465 	break;
466 
467 #ifdef VJC
468     case PPPIOCSMAXCID:
469 	if ((error = suser(p, 0)) != 0)
470 	    return (error);
471 	if (sc->sc_comp) {
472 	    s = splsoftnet();
473 	    sl_compress_setup(sc->sc_comp, *(int *)data);
474 	    splx(s);
475 	}
476 	break;
477 #endif
478 
479     case PPPIOCXFERUNIT:
480 	if ((error = suser(p, 0)) != 0)
481 	    return (error);
482 	sc->sc_xfer = p->p_pid;
483 	break;
484 
485 #ifdef PPP_COMPRESS
486     case PPPIOCSCOMPRESS:
487 	if ((error = suser(p, 0)) != 0)
488 	    return (error);
489 	odp = (struct ppp_option_data *) data;
490 	nb = odp->length;
491 	if (nb > sizeof(ccp_option))
492 	    nb = sizeof(ccp_option);
493 	if ((error = copyin(odp->ptr, ccp_option, nb)) != 0)
494 	    return (error);
495 	if (ccp_option[1] < 2)	/* preliminary check on the length byte */
496 	    return (EINVAL);
497 	for (cp = ppp_compressors; *cp != NULL; ++cp)
498 	    if ((*cp)->compress_proto == ccp_option[0]) {
499 		/*
500 		 * Found a handler for the protocol - try to allocate
501 		 * a compressor or decompressor.
502 		 */
503 		error = 0;
504 		if (odp->transmit) {
505 		    s = splsoftnet();
506 		    if (sc->sc_xc_state != NULL)
507 			(*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
508 		    sc->sc_xcomp = *cp;
509 		    sc->sc_xc_state = (*cp)->comp_alloc(ccp_option, nb);
510 		    if (sc->sc_xc_state == NULL) {
511 			if (sc->sc_flags & SC_DEBUG)
512 			    printf("%s: comp_alloc failed\n",
513 				sc->sc_if.if_xname);
514 			error = ENOBUFS;
515 		    }
516 		    splimp();
517 		    sc->sc_flags &= ~SC_COMP_RUN;
518 		    splx(s);
519 		} else {
520 		    s = splsoftnet();
521 		    if (sc->sc_rc_state != NULL)
522 			(*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
523 		    sc->sc_rcomp = *cp;
524 		    sc->sc_rc_state = (*cp)->decomp_alloc(ccp_option, nb);
525 		    if (sc->sc_rc_state == NULL) {
526 			if (sc->sc_flags & SC_DEBUG)
527 			    printf("%s: decomp_alloc failed\n",
528 				sc->sc_if.if_xname);
529 			error = ENOBUFS;
530 		    }
531 		    splimp();
532 		    sc->sc_flags &= ~SC_DECOMP_RUN;
533 		    splx(s);
534 		}
535 		return (error);
536 	    }
537 	if (sc->sc_flags & SC_DEBUG)
538 	    printf("%s: no compressor for [%x %x %x], %x\n",
539 		sc->sc_if.if_xname, ccp_option[0], ccp_option[1],
540 		ccp_option[2], nb);
541 	return (EINVAL);	/* no handler found */
542 #endif /* PPP_COMPRESS */
543 
544     case PPPIOCGNPMODE:
545     case PPPIOCSNPMODE:
546 	npi = (struct npioctl *) data;
547 	switch (npi->protocol) {
548 	case PPP_IP:
549 	    npx = NP_IP;
550 	    break;
551 	default:
552 	    return EINVAL;
553 	}
554 	if (cmd == PPPIOCGNPMODE) {
555 	    npi->mode = sc->sc_npmode[npx];
556 	} else {
557 	    if ((error = suser(p, 0)) != 0)
558 		return (error);
559 	    if (npi->mode != sc->sc_npmode[npx]) {
560 		s = splsoftnet();
561 		sc->sc_npmode[npx] = npi->mode;
562 		if (npi->mode != NPMODE_QUEUE) {
563 		    ppp_requeue(sc);
564 		    (*sc->sc_start)(sc);
565 		}
566 		splx(s);
567 	    }
568 	}
569 	break;
570 
571     case PPPIOCGIDLE:
572 	s = splsoftnet();
573 	t = time.tv_sec;
574 	((struct ppp_idle *)data)->xmit_idle = t - sc->sc_last_sent;
575 	((struct ppp_idle *)data)->recv_idle = t - sc->sc_last_recv;
576 	splx(s);
577 	break;
578 
579     case PPPIOCSPASS:
580     case PPPIOCSACTIVE:
581 	nbp = (struct bpf_program *) data;
582 	if ((unsigned) nbp->bf_len > BPF_MAXINSNS)
583 	    return EINVAL;
584 	newcodelen = nbp->bf_len * sizeof(struct bpf_insn);
585 	if (newcodelen != 0) {
586 	    MALLOC(newcode, struct bpf_insn *, newcodelen, M_DEVBUF, M_WAITOK);
587 	    if ((error = copyin((caddr_t)nbp->bf_insns, (caddr_t)newcode,
588 			       newcodelen)) != 0) {
589 		FREE(newcode, M_DEVBUF);
590 		return error;
591 	    }
592 	    if (!bpf_validate(newcode, nbp->bf_len)) {
593 		FREE(newcode, M_DEVBUF);
594 		return EINVAL;
595 	    }
596 	} else
597 	    newcode = 0;
598 	bp = (cmd == PPPIOCSPASS)? &sc->sc_pass_filt: &sc->sc_active_filt;
599 	oldcode = bp->bf_insns;
600 	s = splimp();
601 	bp->bf_len = nbp->bf_len;
602 	bp->bf_insns = newcode;
603 	splx(s);
604 	if (oldcode != 0)
605 	    FREE(oldcode, M_DEVBUF);
606 	break;
607 
608     default:
609 	return (-1);
610     }
611     return (0);
612 }
613 
614 /*
615  * Process an ioctl request to the ppp network interface.
616  */
617 static int
pppsioctl(ifp,cmd,data)618 pppsioctl(ifp, cmd, data)
619     struct ifnet *ifp;
620     u_long cmd;
621     caddr_t data;
622 {
623     struct ppp_softc *sc = ifp->if_softc;
624     struct ifaddr *ifa = (struct ifaddr *)data;
625     struct ifreq *ifr = (struct ifreq *)data;
626     struct ppp_stats *psp;
627 #ifdef	PPP_COMPRESS
628     struct ppp_comp_stats *pcp;
629 #endif
630     int s = splimp(), error = 0;
631 
632     switch (cmd) {
633     case SIOCSIFFLAGS:
634 	if ((ifp->if_flags & IFF_RUNNING) == 0)
635 	    ifp->if_flags &= ~IFF_UP;
636 	break;
637 
638     case SIOCSIFADDR:
639 	if (ifa->ifa_addr->sa_family != AF_INET)
640 	    error = EAFNOSUPPORT;
641 	break;
642 
643     case SIOCSIFDSTADDR:
644 	if (ifa->ifa_addr->sa_family != AF_INET)
645 	    error = EAFNOSUPPORT;
646 	break;
647 
648     case SIOCSIFMTU:
649 	sc->sc_if.if_mtu = ifr->ifr_mtu;
650 	break;
651 
652     case SIOCADDMULTI:
653     case SIOCDELMULTI:
654 	if (ifr == 0) {
655 	    error = EAFNOSUPPORT;
656 	    break;
657 	}
658 	switch(ifr->ifr_addr.sa_family) {
659 #ifdef INET
660 	case AF_INET:
661 	    break;
662 #endif
663 	default:
664 	    error = EAFNOSUPPORT;
665 	    break;
666 	}
667 	break;
668 
669     case SIOCGPPPSTATS:
670 	psp = &((struct ifpppstatsreq *) data)->stats;
671 	bzero(psp, sizeof(*psp));
672 	psp->p = sc->sc_stats;
673 #if defined(VJC) && !defined(SL_NO_STATS)
674 	if (sc->sc_comp) {
675 	    psp->vj.vjs_packets = sc->sc_comp->sls_packets;
676 	    psp->vj.vjs_compressed = sc->sc_comp->sls_compressed;
677 	    psp->vj.vjs_searches = sc->sc_comp->sls_searches;
678 	    psp->vj.vjs_misses = sc->sc_comp->sls_misses;
679 	    psp->vj.vjs_uncompressedin = sc->sc_comp->sls_uncompressedin;
680 	    psp->vj.vjs_compressedin = sc->sc_comp->sls_compressedin;
681 	    psp->vj.vjs_errorin = sc->sc_comp->sls_errorin;
682 	    psp->vj.vjs_tossed = sc->sc_comp->sls_tossed;
683 	}
684 #endif /* VJC */
685 	break;
686 
687 #ifdef PPP_COMPRESS
688     case SIOCGPPPCSTATS:
689 	pcp = &((struct ifpppcstatsreq *) data)->stats;
690 	bzero(pcp, sizeof(*pcp));
691 	if (sc->sc_xc_state != NULL)
692 	    (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c);
693 	if (sc->sc_rc_state != NULL)
694 	    (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d);
695 	break;
696 #endif /* PPP_COMPRESS */
697 
698     default:
699 	error = EINVAL;
700     }
701     splx(s);
702     return (error);
703 }
704 
705 /*
706  * Queue a packet.  Start transmission if not active.
707  * Packet is placed in Information field of PPP frame.
708  */
709 int
pppoutput(ifp,m0,dst,rtp)710 pppoutput(ifp, m0, dst, rtp)
711     struct ifnet *ifp;
712     struct mbuf *m0;
713     struct sockaddr *dst;
714     struct rtentry *rtp;
715 {
716     struct ppp_softc *sc = ifp->if_softc;
717     int protocol, address, control;
718     u_char *cp;
719     int s, error;
720     struct ip *ip;
721     struct ifqueue *ifq;
722     enum NPmode mode;
723     int len;
724     struct mbuf *m;
725 
726     if (sc->sc_devp == NULL || (ifp->if_flags & IFF_RUNNING) == 0
727 	|| ((ifp->if_flags & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC)) {
728 	error = ENETDOWN;	/* sort of */
729 	goto bad;
730     }
731 
732     /*
733      * Compute PPP header.
734      */
735     m0->m_flags &= ~M_HIGHPRI;
736     switch (dst->sa_family) {
737 #ifdef INET
738     case AF_INET:
739 	address = PPP_ALLSTATIONS;
740 	control = PPP_UI;
741 	protocol = PPP_IP;
742 	mode = sc->sc_npmode[NP_IP];
743 
744 	/*
745 	 * If this packet has the "low delay" bit set in the IP header,
746 	 * put it on the fastq instead.
747 	 */
748 	ip = mtod(m0, struct ip *);
749 	if (ip->ip_tos & IPTOS_LOWDELAY)
750 	    m0->m_flags |= M_HIGHPRI;
751 	break;
752 #endif
753     case AF_UNSPEC:
754 	address = PPP_ADDRESS(dst->sa_data);
755 	control = PPP_CONTROL(dst->sa_data);
756 	protocol = PPP_PROTOCOL(dst->sa_data);
757 	mode = NPMODE_PASS;
758 	break;
759     default:
760 	printf("%s: af%d not supported\n", ifp->if_xname, dst->sa_family);
761 	error = EAFNOSUPPORT;
762 	goto bad;
763     }
764 
765     /*
766      * Drop this packet, or return an error, if necessary.
767      */
768     if (mode == NPMODE_ERROR) {
769 	error = ENETDOWN;
770 	goto bad;
771     }
772     if (mode == NPMODE_DROP) {
773 	error = 0;
774 	goto bad;
775     }
776 
777     /*
778      * Add PPP header.  If no space in first mbuf, allocate another.
779      * (This assumes M_LEADINGSPACE is always 0 for a cluster mbuf.)
780      */
781     if (M_LEADINGSPACE(m0) < PPP_HDRLEN) {
782 	m0 = m_prepend(m0, PPP_HDRLEN, M_DONTWAIT);
783 	if (m0 == 0) {
784 	    error = ENOBUFS;
785 	    goto bad;
786 	}
787 	m0->m_len = 0;
788     } else
789 	m0->m_data -= PPP_HDRLEN;
790 
791     cp = mtod(m0, u_char *);
792     *cp++ = address;
793     *cp++ = control;
794     *cp++ = protocol >> 8;
795     *cp++ = protocol & 0xff;
796     m0->m_len += PPP_HDRLEN;
797 
798     len = 0;
799     for (m = m0; m != 0; m = m->m_next)
800 	len += m->m_len;
801 
802     if (sc->sc_flags & SC_LOG_OUTPKT) {
803 	printf("%s output: ", ifp->if_xname);
804 	pppdumpm(m0);
805     }
806 
807     if ((protocol & 0x8000) == 0) {
808 	/*
809 	 * Apply the pass and active filters to the packet,
810 	 * but only if it is a data packet.
811 	 */
812 	*mtod(m0, u_char *) = 1;	/* indicates outbound */
813 	if (sc->sc_pass_filt.bf_insns != 0
814 	    && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m0,
815 			  len, 0) == 0) {
816 	    error = 0;		/* drop this packet */
817 	    goto bad;
818 	}
819 
820 	/*
821 	 * Update the time we sent the most recent packet.
822 	 */
823 	if (sc->sc_active_filt.bf_insns == 0
824 	    || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m0, len, 0))
825 	    sc->sc_last_sent = time.tv_sec;
826 
827 	*mtod(m0, u_char *) = address;
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);
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
ppp_requeue(sc)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     splassert(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
ppp_restart(sc)958 ppp_restart(sc)
959     struct ppp_softc *sc;
960 {
961     int s = splimp();
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 *
ppp_dequeue(sc)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, clen;
1055 
1056 	slen = 0;
1057 	for (mp = m; mp != NULL; mp = mp->m_next)
1058 	    slen += mp->m_len;
1059 	clen = (*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
pppintr()1104 pppintr()
1105 {
1106     struct ppp_softc *sc;
1107     int s, s2;
1108     struct mbuf *m;
1109 
1110     splassert(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 = splimp();
1117 	    sc->sc_flags |= SC_TBUSY;
1118 	    splx(s2);
1119 	    (*sc->sc_start)(sc);
1120 	}
1121 	for (;;) {
1122 	    s2 = splimp();
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
ppp_ccp(sc,m,rcvd)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 = splimp();
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 = splimp();
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 = splimp();
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 = splimp();
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
ppp_ccp_closed(sc)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
ppppktin(sc,m,lost)1257 ppppktin(sc, m, lost)
1258     struct ppp_softc *sc;
1259     struct mbuf *m;
1260     int lost;
1261 {
1262     int s = splimp();
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
ppp_inproc(sc,m)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 = splimp();
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 = splimp();
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 = splimp();
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 	/*
1471 	 * See whether we want to pass this packet, and
1472 	 * if it counts as link activity.
1473 	 */
1474 	adrs = *mtod(m, u_char *);	/* save address field */
1475 	*mtod(m, u_char *) = 0;		/* indicate inbound */
1476 	if (sc->sc_pass_filt.bf_insns != 0
1477 	    && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m,
1478 			  ilen, 0) == 0) {
1479 	    /* drop this packet */
1480 	    m_freem(m);
1481 	    return;
1482 	}
1483 	if (sc->sc_active_filt.bf_insns == 0
1484 	    || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m, ilen, 0))
1485 	    sc->sc_last_recv = time.tv_sec;
1486 
1487 	*mtod(m, u_char *) = adrs;
1488     }
1489 
1490 #if NBPFILTER > 0
1491     /* See if bpf wants to look at the packet. */
1492     if (sc->sc_bpf)
1493 	bpf_mtap(sc->sc_bpf, m);
1494 #endif
1495 
1496     rv = 0;
1497     switch (proto) {
1498 #ifdef INET
1499     case PPP_IP:
1500 	/*
1501 	 * IP packet - take off the ppp header and pass it up to IP.
1502 	 */
1503 	if ((ifp->if_flags & IFF_UP) == 0
1504 	    || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
1505 	    /* interface is down - drop the packet. */
1506 	    m_freem(m);
1507 	    return;
1508 	}
1509 	m->m_pkthdr.len -= PPP_HDRLEN;
1510 	m->m_data += PPP_HDRLEN;
1511 	m->m_len -= PPP_HDRLEN;
1512 	schednetisr(NETISR_IP);
1513 	inq = &ipintrq;
1514 	break;
1515 #endif
1516 
1517     default:
1518 	/*
1519 	 * Some other protocol - place on input queue for read().
1520 	 */
1521 	inq = &sc->sc_inq;
1522 	rv = 1;
1523 	break;
1524     }
1525 
1526     /*
1527      * Put the packet on the appropriate input queue.
1528      */
1529     s = splimp();
1530     if (IF_QFULL(inq)) {
1531 	IF_DROP(inq);
1532 	splx(s);
1533 	if (sc->sc_flags & SC_DEBUG)
1534 	    printf("%s: input queue full\n", ifp->if_xname);
1535 	ifp->if_iqdrops++;
1536 	if (!inq->ifq_congestion)
1537 		if_congestion(inq);
1538 	goto bad;
1539     }
1540     IF_ENQUEUE(inq, m);
1541     splx(s);
1542     ifp->if_ipackets++;
1543     ifp->if_ibytes += ilen;
1544 
1545     if (rv)
1546 	(*sc->sc_ctlp)(sc);
1547 
1548     return;
1549 
1550  bad:
1551     m_freem(m);
1552     sc->sc_if.if_ierrors++;
1553     sc->sc_stats.ppp_ierrors++;
1554 }
1555 
1556 #define MAX_DUMP_BYTES	128
1557 
1558 static void
pppdumpm(m0)1559 pppdumpm(m0)
1560     struct mbuf *m0;
1561 {
1562     char buf[3*MAX_DUMP_BYTES+4];
1563     char *bp = buf;
1564     struct mbuf *m;
1565     static char digits[] = "0123456789abcdef";
1566 
1567     for (m = m0; m; m = m->m_next) {
1568 	int l = m->m_len;
1569 	u_char *rptr = (u_char *)m->m_data;
1570 
1571 	while (l--) {
1572 	    if (bp > buf + sizeof(buf) - 4)
1573 		goto done;
1574 	    *bp++ = digits[*rptr >> 4]; /* convert byte to ascii hex */
1575 	    *bp++ = digits[*rptr++ & 0xf];
1576 	}
1577 
1578 	if (m->m_next) {
1579 	    if (bp > buf + sizeof(buf) - 3)
1580 		goto done;
1581 	    *bp++ = '|';
1582 	} else
1583 	    *bp++ = ' ';
1584     }
1585 done:
1586     if (m)
1587 	*bp++ = '>';
1588     *bp = 0;
1589     printf("%s\n", buf);
1590 }
1591 
1592 #ifdef ALTQ
1593 /*
1594  * a wrapper to transmit a packet from if_start since ALTQ uses
1595  * if_start to send a packet.
1596  */
1597 static void
ppp_ifstart(ifp)1598 ppp_ifstart(ifp)
1599 	struct ifnet *ifp;
1600 {
1601 	struct ppp_softc *sc;
1602 
1603 	sc = ifp->if_softc;
1604 	(*sc->sc_start)(sc);
1605 }
1606 #endif
1607 
1608 #endif	/* NPPP > 0 */
1609