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