Adapt the netisr message handlers to accomodate the available error
[dragonfly.git] / sys / net / ppp / if_ppp.c
1 /*
2  * if_ppp.c - Point-to-Point Protocol (PPP) Asynchronous driver.
3  *
4  * Copyright (c) 1989 Carnegie Mellon University.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms are permitted
8  * provided that the above copyright notice and this paragraph are
9  * duplicated in all such forms and that any documentation,
10  * advertising materials, and other materials related to such
11  * distribution and use acknowledge that the software was developed
12  * by Carnegie Mellon University.  The name of the
13  * University may not be used to endorse or promote products derived
14  * from this software without specific prior written permission.
15  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18  *
19  * Drew D. Perkins
20  * Carnegie Mellon University
21  * 4910 Forbes Ave.
22  * Pittsburgh, PA 15213
23  * (412) 268-8576
24  * ddp@andrew.cmu.edu
25  *
26  * Based on:
27  *      @(#)if_sl.c     7.6.1.2 (Berkeley) 2/15/89
28  *
29  * Copyright (c) 1987 Regents of the University of California.
30  * All rights reserved.
31  *
32  * Redistribution and use in source and binary forms are permitted
33  * provided that the above copyright notice and this paragraph are
34  * duplicated in all such forms and that any documentation,
35  * advertising materials, and other materials related to such
36  * distribution and use acknowledge that the software was developed
37  * by the University of California, Berkeley.  The name of the
38  * University may not be used to endorse or promote products derived
39  * from this software without specific prior written permission.
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
41  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
42  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
43  *
44  * Serial Line interface
45  *
46  * Rick Adams
47  * Center for Seismic Studies
48  * 1300 N 17th Street, Suite 1450
49  * Arlington, Virginia 22209
50  * (703)276-7900
51  * rick@seismo.ARPA
52  * seismo!rick
53  *
54  * Pounded on heavily by Chris Torek (chris@mimsy.umd.edu, umcp-cs!chris).
55  * Converted to 4.3BSD Beta by Chris Torek.
56  * Other changes made at Berkeley, based in part on code by Kirk Smith.
57  *
58  * Converted to 4.3BSD+ 386BSD by Brad Parker (brad@cayman.com)
59  * Added VJ tcp header compression; more unified ioctls
60  *
61  * Extensively modified by Paul Mackerras (paulus@cs.anu.edu.au).
62  * Cleaned up a lot of the mbuf-related code to fix bugs that
63  * caused system crashes and packet corruption.  Changed pppstart
64  * so that it doesn't just give up with a collision if the whole
65  * packet doesn't fit in the output ring buffer.
66  *
67  * Added priority queueing for interactive IP packets, following
68  * the model of if_sl.c, plus hooks for bpf.
69  * Paul Mackerras (paulus@cs.anu.edu.au).
70  */
71
72 /* $FreeBSD: src/sys/net/if_ppp.c,v 1.67.2.4 2002/04/14 21:41:48 luigi Exp $ */
73 /* $DragonFly: src/sys/net/ppp/if_ppp.c,v 1.14 2004/04/02 12:32:27 hmp Exp $ */
74 /* from if_sl.c,v 1.11 84/10/04 12:54:47 rick Exp */
75 /* from NetBSD: if_ppp.c,v 1.15.2.2 1994/07/28 05:17:58 cgd Exp */
76
77 #include "use_ppp.h"
78
79 #include "opt_inet.h"
80 #include "opt_ipx.h"
81 #include "opt_ppp.h"
82
83 #ifdef INET
84 #define VJC
85 #endif
86 #define PPP_COMPRESS
87
88 #include <sys/param.h>
89 #include <sys/systm.h>
90 #include <sys/proc.h>
91 #include <sys/mbuf.h>
92 #include <sys/socket.h>
93 #include <sys/filio.h>
94 #include <sys/sockio.h>
95 #include <sys/kernel.h>
96 #include <sys/time.h>
97 #include <sys/malloc.h>
98
99 #include <net/if.h>
100 #include <net/if_types.h>
101 #include <net/netisr.h>
102 #include <net/bpf.h>
103
104 #ifdef INET
105 #include <netinet/in.h>
106 #include <netinet/in_systm.h>
107 #include <netinet/in_var.h>
108 #include <netinet/ip.h>
109 #endif
110
111 #ifdef IPX
112 #include <netproto/ipx/ipx.h>
113 #include <netproto/ipx/ipx_if.h>
114 #endif
115
116 #ifdef VJC
117 #include <net/slcompress.h>
118 #endif
119
120 #include "if_ppp.h"
121 #include "if_pppvar.h"
122
123 /* minimise diffs */
124 #ifndef splsoftnet
125 #define splsoftnet      splnet
126 #endif
127
128 #ifdef PPP_COMPRESS
129 #define PACKETPTR       struct mbuf *
130 #include <net/ppp_layer/ppp_comp.h>
131 #endif
132
133 struct ppp_softc ppp_softc[NPPP];
134
135 /* XXX layering violation */
136 extern void     pppasyncattach (void *);
137
138 static void     pppattach (void *);
139 PSEUDO_SET(pppattach, if_ppp);
140
141 static int      pppsioctl (struct ifnet *ifp, u_long cmd, caddr_t data,
142                            struct ucred *);
143
144 static void     ppp_requeue (struct ppp_softc *);
145 static void     ppp_ccp (struct ppp_softc *, struct mbuf *m, int rcvd);
146 static void     ppp_ccp_closed (struct ppp_softc *);
147 static void     ppp_inproc (struct ppp_softc *, struct mbuf *);
148 static void     pppdumpm (struct mbuf *m0);
149
150 /*
151  * Some useful mbuf macros not in mbuf.h.
152  */
153 #define M_IS_CLUSTER(m) ((m)->m_flags & M_EXT)
154
155 #define M_DATASTART(m)  \
156         (M_IS_CLUSTER(m) ? (m)->m_ext.ext_buf : \
157             (m)->m_flags & M_PKTHDR ? (m)->m_pktdat : (m)->m_dat)
158
159 #define M_DATASIZE(m)   \
160         (M_IS_CLUSTER(m) ? (m)->m_ext.ext_size : \
161             (m)->m_flags & M_PKTHDR ? MHLEN: MLEN)
162
163 /*
164  * We steal two bits in the mbuf m_flags, to mark high-priority packets
165  * for output, and received packets following lost/corrupted packets.
166  */
167 #define M_HIGHPRI       0x2000  /* output packet for sc_fastq */
168 #define M_ERRMARK       0x4000  /* steal a bit in mbuf m_flags */
169
170
171 #ifdef PPP_COMPRESS
172 /*
173  * List of compressors we know about.
174  * We leave some space so maybe we can modload compressors.
175  */
176
177 extern struct compressor ppp_bsd_compress;
178 extern struct compressor ppp_deflate, ppp_deflate_draft;
179
180 static struct compressor *ppp_compressors[8] = {
181 #if DO_BSD_COMPRESS && defined(PPP_BSDCOMP)
182     &ppp_bsd_compress,
183 #endif
184 #if DO_DEFLATE && defined(PPP_DEFLATE)
185     &ppp_deflate,
186     &ppp_deflate_draft,
187 #endif
188     NULL
189 };
190 #endif /* PPP_COMPRESS */
191
192 /*
193  * Software interrupt routine, called at spl[soft]net.
194  */
195 static int
196 pppintr(struct netmsg *msg)
197 {
198     struct mbuf *m = ((struct netmsg_packet *)msg)->nm_packet;
199     struct ppp_softc *sc;
200     int i, s;
201
202     sc = ppp_softc;
203     for (i = 0; i < NPPP; ++i, ++sc) {
204         s = splimp();
205         if (!(sc->sc_flags & SC_TBUSY)
206             && (sc->sc_if.if_snd.ifq_head || sc->sc_fastq.ifq_head)) {
207             sc->sc_flags |= SC_TBUSY;
208             splx(s);
209             (*sc->sc_start)(sc);
210         } else
211             splx(s);
212         for (;;) {
213             s = splimp();
214             IF_DEQUEUE(&sc->sc_rawq, m);
215             splx(s);
216             if (m == NULL)
217                 break;
218             ppp_inproc(sc, m);
219         }
220     }
221
222         return 0;
223 }
224
225 /*
226  * Called from boot code to establish ppp interfaces.
227  */
228 static void
229 pppattach(dummy)
230     void *dummy;
231 {
232     struct ppp_softc *sc;
233     int i = 0;
234
235     for (sc = ppp_softc; i < NPPP; sc++) {
236         if_initname(&(sc->sc_if), "ppp", i++);
237         sc->sc_if.if_mtu = PPP_MTU;
238         sc->sc_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
239         sc->sc_if.if_type = IFT_PPP;
240         sc->sc_if.if_hdrlen = PPP_HDRLEN;
241         sc->sc_if.if_ioctl = pppsioctl;
242         sc->sc_if.if_output = pppoutput;
243         sc->sc_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
244         sc->sc_inq.ifq_maxlen = IFQ_MAXLEN;
245         sc->sc_fastq.ifq_maxlen = IFQ_MAXLEN;
246         sc->sc_rawq.ifq_maxlen = IFQ_MAXLEN;
247         if_attach(&sc->sc_if);
248         bpfattach(&sc->sc_if, DLT_PPP, PPP_HDRLEN);
249     }
250     netisr_register(NETISR_PPP, cpu0_portfn, pppintr);
251     /*
252      * XXX layering violation - if_ppp can work over any lower level
253      * transport that cares to attach to it.
254      */
255     pppasyncattach(dummy);
256 }
257
258 /*
259  * Allocate a ppp interface unit and initialize it.
260  */
261 struct ppp_softc *
262 pppalloc(struct thread *td)
263 {
264     int nppp, i;
265     struct ppp_softc *sc;
266
267     for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
268         if (sc->sc_xfer == td) {
269             sc->sc_xfer = NULL;
270             return sc;
271         }
272     for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
273         if (sc->sc_devp == NULL)
274             break;
275     if (nppp >= NPPP)
276         return NULL;
277
278     sc->sc_flags = 0;
279     sc->sc_mru = PPP_MRU;
280     sc->sc_relinq = NULL;
281     bzero((char *)&sc->sc_stats, sizeof(sc->sc_stats));
282 #ifdef VJC
283     MALLOC(sc->sc_comp, struct slcompress *, sizeof(struct slcompress),
284            M_DEVBUF, M_NOWAIT);
285     if (sc->sc_comp)
286         sl_compress_init(sc->sc_comp, -1);
287 #endif
288 #ifdef PPP_COMPRESS
289     sc->sc_xc_state = NULL;
290     sc->sc_rc_state = NULL;
291 #endif /* PPP_COMPRESS */
292     for (i = 0; i < NUM_NP; ++i)
293         sc->sc_npmode[i] = NPMODE_ERROR;
294     sc->sc_npqueue = NULL;
295     sc->sc_npqtail = &sc->sc_npqueue;
296     sc->sc_last_sent = sc->sc_last_recv = time_second;
297
298     return sc;
299 }
300
301 /*
302  * Deallocate a ppp unit.  Must be called at splsoftnet or higher.
303  */
304 void
305 pppdealloc(sc)
306     struct ppp_softc *sc;
307 {
308     struct mbuf *m;
309
310     if_down(&sc->sc_if);
311     sc->sc_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
312     getmicrotime(&sc->sc_if.if_lastchange);
313     sc->sc_devp = NULL;
314     sc->sc_xfer = NULL;
315     for (;;) {
316         IF_DEQUEUE(&sc->sc_rawq, m);
317         if (m == NULL)
318             break;
319         m_freem(m);
320     }
321     for (;;) {
322         IF_DEQUEUE(&sc->sc_inq, m);
323         if (m == NULL)
324             break;
325         m_freem(m);
326     }
327     for (;;) {
328         IF_DEQUEUE(&sc->sc_fastq, m);
329         if (m == NULL)
330             break;
331         m_freem(m);
332     }
333     while ((m = sc->sc_npqueue) != NULL) {
334         sc->sc_npqueue = m->m_nextpkt;
335         m_freem(m);
336     }
337 #ifdef PPP_COMPRESS
338     ppp_ccp_closed(sc);
339     sc->sc_xc_state = NULL;
340     sc->sc_rc_state = NULL;
341 #endif /* PPP_COMPRESS */
342 #ifdef PPP_FILTER
343     if (sc->sc_pass_filt.bf_insns != 0) {
344         free(sc->sc_pass_filt.bf_insns, M_DEVBUF);
345         sc->sc_pass_filt.bf_insns = 0;
346         sc->sc_pass_filt.bf_len = 0;
347     }
348     if (sc->sc_active_filt.bf_insns != 0) {
349         free(sc->sc_active_filt.bf_insns, M_DEVBUF);
350         sc->sc_active_filt.bf_insns = 0;
351         sc->sc_active_filt.bf_len = 0;
352     }
353 #endif /* PPP_FILTER */
354 #ifdef VJC
355     if (sc->sc_comp != 0) {
356         free(sc->sc_comp, M_DEVBUF);
357         sc->sc_comp = 0;
358     }
359 #endif
360 }
361
362 /*
363  * Ioctl routine for generic ppp devices.
364  */
365 int
366 pppioctl(struct ppp_softc *sc, u_long cmd, caddr_t data,
367     int flag, struct thread *td)
368 {
369     int s, error, flags, mru, npx;
370     u_int nb;
371     struct ppp_option_data *odp;
372     struct compressor **cp;
373     struct npioctl *npi;
374     time_t t;
375 #ifdef PPP_FILTER
376     struct bpf_program *bp, *nbp;
377     struct bpf_insn *newcode, *oldcode;
378     int newcodelen;
379 #endif /* PPP_FILTER */
380 #ifdef  PPP_COMPRESS
381     u_char ccp_option[CCP_MAX_OPTION_LENGTH];
382 #endif
383
384     switch (cmd) {
385     case FIONREAD:
386         *(int *)data = sc->sc_inq.ifq_len;
387         break;
388
389     case PPPIOCGUNIT:
390         *(int *)data = sc->sc_if.if_dunit;
391         break;
392
393     case PPPIOCGFLAGS:
394         *(u_int *)data = sc->sc_flags;
395         break;
396
397     case PPPIOCSFLAGS:
398         if ((error = suser(td)) != 0)
399             return (error);
400         flags = *(int *)data & SC_MASK;
401         s = splsoftnet();
402 #ifdef PPP_COMPRESS
403         if (sc->sc_flags & SC_CCP_OPEN && !(flags & SC_CCP_OPEN))
404             ppp_ccp_closed(sc);
405 #endif
406         splimp();
407         sc->sc_flags = (sc->sc_flags & ~SC_MASK) | flags;
408         splx(s);
409         break;
410
411     case PPPIOCSMRU:
412         if ((error = suser(td)) != 0)
413             return (error);
414         mru = *(int *)data;
415         if (mru >= PPP_MRU && mru <= PPP_MAXMRU)
416             sc->sc_mru = mru;
417         break;
418
419     case PPPIOCGMRU:
420         *(int *)data = sc->sc_mru;
421         break;
422
423 #ifdef VJC
424     case PPPIOCSMAXCID:
425         if ((error = suser(td)) != 0)
426             return (error);
427         if (sc->sc_comp) {
428             s = splsoftnet();
429             sl_compress_init(sc->sc_comp, *(int *)data);
430             splx(s);
431         }
432         break;
433 #endif
434
435     case PPPIOCXFERUNIT:
436         if ((error = suser(td)) != 0)
437             return (error);
438         sc->sc_xfer = td;
439         break;
440
441 #ifdef PPP_COMPRESS
442     case PPPIOCSCOMPRESS:
443         if ((error = suser(td)) != 0)
444             return (error);
445         odp = (struct ppp_option_data *) data;
446         nb = odp->length;
447         if (nb > sizeof(ccp_option))
448             nb = sizeof(ccp_option);
449         if ((error = copyin(odp->ptr, ccp_option, nb)) != 0)
450             return (error);
451         if (ccp_option[1] < 2)  /* preliminary check on the length byte */
452             return (EINVAL);
453         for (cp = ppp_compressors; *cp != NULL; ++cp)
454             if ((*cp)->compress_proto == ccp_option[0]) {
455                 /*
456                  * Found a handler for the protocol - try to allocate
457                  * a compressor or decompressor.
458                  */
459                 error = 0;
460                 if (odp->transmit) {
461                     s = splsoftnet();
462                     if (sc->sc_xc_state != NULL)
463                         (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
464                     sc->sc_xcomp = *cp;
465                     sc->sc_xc_state = (*cp)->comp_alloc(ccp_option, nb);
466                     if (sc->sc_xc_state == NULL) {
467                         if (sc->sc_flags & SC_DEBUG)
468                             printf("%s: comp_alloc failed\n",
469                                sc->sc_if.if_xname);
470                         error = ENOBUFS;
471                     }
472                     splimp();
473                     sc->sc_flags &= ~SC_COMP_RUN;
474                     splx(s);
475                 } else {
476                     s = splsoftnet();
477                     if (sc->sc_rc_state != NULL)
478                         (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
479                     sc->sc_rcomp = *cp;
480                     sc->sc_rc_state = (*cp)->decomp_alloc(ccp_option, nb);
481                     if (sc->sc_rc_state == NULL) {
482                         if (sc->sc_flags & SC_DEBUG)
483                             printf("%s: decomp_alloc failed\n",
484                                sc->sc_if.if_xname);
485                         error = ENOBUFS;
486                     }
487                     splimp();
488                     sc->sc_flags &= ~SC_DECOMP_RUN;
489                     splx(s);
490                 }
491                 return (error);
492             }
493         if (sc->sc_flags & SC_DEBUG)
494             printf("%s: no compressor for [%x %x %x], %x\n",
495                    sc->sc_if.if_xname, ccp_option[0], ccp_option[1],
496                    ccp_option[2], nb);
497         return (EINVAL);        /* no handler found */
498 #endif /* PPP_COMPRESS */
499
500     case PPPIOCGNPMODE:
501     case PPPIOCSNPMODE:
502         npi = (struct npioctl *) data;
503         switch (npi->protocol) {
504         case PPP_IP:
505             npx = NP_IP;
506             break;
507         default:
508             return EINVAL;
509         }
510         if (cmd == PPPIOCGNPMODE) {
511             npi->mode = sc->sc_npmode[npx];
512         } else {
513             if ((error = suser(td)) != 0)
514                 return (error);
515             if (npi->mode != sc->sc_npmode[npx]) {
516                 s = splsoftnet();
517                 sc->sc_npmode[npx] = npi->mode;
518                 if (npi->mode != NPMODE_QUEUE) {
519                     ppp_requeue(sc);
520                     (*sc->sc_start)(sc);
521                 }
522                 splx(s);
523             }
524         }
525         break;
526
527     case PPPIOCGIDLE:
528         s = splsoftnet();
529         t = time_second;
530         ((struct ppp_idle *)data)->xmit_idle = t - sc->sc_last_sent;
531         ((struct ppp_idle *)data)->recv_idle = t - sc->sc_last_recv;
532         splx(s);
533         break;
534
535 #ifdef PPP_FILTER
536     case PPPIOCSPASS:
537     case PPPIOCSACTIVE:
538         nbp = (struct bpf_program *) data;
539         if ((unsigned) nbp->bf_len > BPF_MAXINSNS)
540             return EINVAL;
541         newcodelen = nbp->bf_len * sizeof(struct bpf_insn);
542         if (newcodelen != 0) {
543             MALLOC(newcode, struct bpf_insn *, newcodelen, M_DEVBUF, M_WAITOK);
544             if (newcode == 0) {
545                 return EINVAL;          /* or sumpin */
546             }
547             if ((error = copyin((caddr_t)nbp->bf_insns, (caddr_t)newcode,
548                                newcodelen)) != 0) {
549                 free(newcode, M_DEVBUF);
550                 return error;
551             }
552             if (!bpf_validate(newcode, nbp->bf_len)) {
553                 free(newcode, M_DEVBUF);
554                 return EINVAL;
555             }
556         } else
557             newcode = 0;
558         bp = (cmd == PPPIOCSPASS)? &sc->sc_pass_filt: &sc->sc_active_filt;
559         oldcode = bp->bf_insns;
560         s = splimp();
561         bp->bf_len = nbp->bf_len;
562         bp->bf_insns = newcode;
563         splx(s);
564         if (oldcode != 0)
565             free(oldcode, M_DEVBUF);
566         break;
567 #endif
568
569     default:
570         return (ENOIOCTL);
571     }
572     return (0);
573 }
574
575 /*
576  * Process an ioctl request to the ppp network interface.
577  */
578 static int
579 pppsioctl(ifp, cmd, data, cr)
580     struct ifnet *ifp;
581     u_long cmd;
582     caddr_t data;
583     struct ucred *cr;
584 {
585     struct thread *td = curthread;      /* XXX */
586     struct ppp_softc *sc = &ppp_softc[ifp->if_dunit];
587     struct ifaddr *ifa = (struct ifaddr *)data;
588     struct ifreq *ifr = (struct ifreq *)data;
589     struct ppp_stats *psp;
590 #ifdef  PPP_COMPRESS
591     struct ppp_comp_stats *pcp;
592 #endif
593     int s = splimp(), error = 0;
594
595     switch (cmd) {
596     case SIOCSIFFLAGS:
597         if ((ifp->if_flags & IFF_RUNNING) == 0)
598             ifp->if_flags &= ~IFF_UP;
599         break;
600
601     case SIOCSIFADDR:
602     case SIOCAIFADDR:
603         switch(ifa->ifa_addr->sa_family) {
604 #ifdef INET
605         case AF_INET:
606             break;
607 #endif
608 #ifdef IPX
609         case AF_IPX:
610             break;
611 #endif
612         default:
613             error = EAFNOSUPPORT;
614             break;
615         }
616         break;
617
618     case SIOCSIFDSTADDR:
619         switch(ifa->ifa_addr->sa_family) {
620 #ifdef INET
621         case AF_INET:
622             break;
623 #endif
624 #ifdef IPX
625         case AF_IPX:
626             break;
627 #endif
628         default:
629             error = EAFNOSUPPORT;
630             break;
631         }
632         break;
633
634     case SIOCSIFMTU:
635         if ((error = suser(td)) != 0)
636             break;
637         if (ifr->ifr_mtu > PPP_MAXMTU)
638             error = EINVAL;
639         else {
640             sc->sc_if.if_mtu = ifr->ifr_mtu;
641             if (sc->sc_setmtu)
642                     (*sc->sc_setmtu)(sc);
643         }
644         break;
645
646     case SIOCGIFMTU:
647         ifr->ifr_mtu = sc->sc_if.if_mtu;
648         break;
649
650     case SIOCADDMULTI:
651     case SIOCDELMULTI:
652         if (ifr == 0) {
653             error = EAFNOSUPPORT;
654             break;
655         }
656         switch(ifr->ifr_addr.sa_family) {
657 #ifdef INET
658         case AF_INET:
659             break;
660 #endif
661         default:
662             error = EAFNOSUPPORT;
663             break;
664         }
665         break;
666
667     case SIOCGPPPSTATS:
668         psp = &((struct ifpppstatsreq *) data)->stats;
669         bzero(psp, sizeof(*psp));
670         psp->p = sc->sc_stats;
671 #if defined(VJC) && !defined(SL_NO_STATS)
672         if (sc->sc_comp) {
673             psp->vj.vjs_packets = sc->sc_comp->sls_packets;
674             psp->vj.vjs_compressed = sc->sc_comp->sls_compressed;
675             psp->vj.vjs_searches = sc->sc_comp->sls_searches;
676             psp->vj.vjs_misses = sc->sc_comp->sls_misses;
677             psp->vj.vjs_uncompressedin = sc->sc_comp->sls_uncompressedin;
678             psp->vj.vjs_compressedin = sc->sc_comp->sls_compressedin;
679             psp->vj.vjs_errorin = sc->sc_comp->sls_errorin;
680             psp->vj.vjs_tossed = sc->sc_comp->sls_tossed;
681         }
682 #endif /* VJC */
683         break;
684
685 #ifdef PPP_COMPRESS
686     case SIOCGPPPCSTATS:
687         pcp = &((struct ifpppcstatsreq *) data)->stats;
688         bzero(pcp, sizeof(*pcp));
689         if (sc->sc_xc_state != NULL)
690             (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c);
691         if (sc->sc_rc_state != NULL)
692             (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d);
693         break;
694 #endif /* PPP_COMPRESS */
695
696     default:
697         error = ENOTTY;
698     }
699     splx(s);
700     return (error);
701 }
702
703 /*
704  * Queue a packet.  Start transmission if not active.
705  * Packet is placed in Information field of PPP frame.
706  * Called at splnet as the if->if_output handler.
707  * Called at splnet from pppwrite().
708  */
709 int
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 = &ppp_softc[ifp->if_dunit];
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 #ifdef IPX
754     case AF_IPX:
755         /*
756          * This is pretty bogus.. We dont have an ipxcp module in pppd
757          * yet to configure the link parameters.  Sigh. I guess a
758          * manual ifconfig would do....  -Peter
759          */
760         address = PPP_ALLSTATIONS;
761         control = PPP_UI;
762         protocol = PPP_IPX;
763         mode = NPMODE_PASS;
764         break;
765 #endif
766     case AF_UNSPEC:
767         address = PPP_ADDRESS(dst->sa_data);
768         control = PPP_CONTROL(dst->sa_data);
769         protocol = PPP_PROTOCOL(dst->sa_data);
770         mode = NPMODE_PASS;
771         break;
772     default:
773         printf("%s: af%d not supported\n", ifp->if_xname, dst->sa_family);
774         error = EAFNOSUPPORT;
775         goto bad;
776     }
777
778     /*
779      * Drop this packet, or return an error, if necessary.
780      */
781     if (mode == NPMODE_ERROR) {
782         error = ENETDOWN;
783         goto bad;
784     }
785     if (mode == NPMODE_DROP) {
786         error = 0;
787         goto bad;
788     }
789
790     /*
791      * Add PPP header.  If no space in first mbuf, allocate another.
792      * (This assumes M_LEADINGSPACE is always 0 for a cluster mbuf.)
793      */
794     if (M_LEADINGSPACE(m0) < PPP_HDRLEN) {
795         m0 = m_prepend(m0, PPP_HDRLEN, M_DONTWAIT);
796         if (m0 == 0) {
797             error = ENOBUFS;
798             goto bad;
799         }
800         m0->m_len = 0;
801     } else
802         m0->m_data -= PPP_HDRLEN;
803
804     cp = mtod(m0, u_char *);
805     *cp++ = address;
806     *cp++ = control;
807     *cp++ = protocol >> 8;
808     *cp++ = protocol & 0xff;
809     m0->m_len += PPP_HDRLEN;
810
811     len = 0;
812     for (m = m0; m != 0; m = m->m_next)
813         len += m->m_len;
814
815     if (sc->sc_flags & SC_LOG_OUTPKT) {
816         printf("%s output: ", ifp->if_xname);
817         pppdumpm(m0);
818     }
819
820     if ((protocol & 0x8000) == 0) {
821 #ifdef PPP_FILTER
822         /*
823          * Apply the pass and active filters to the packet,
824          * but only if it is a data packet.
825          */
826         *mtod(m0, u_char *) = 1;        /* indicates outbound */
827         if (sc->sc_pass_filt.bf_insns != 0
828             && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m0,
829                           len, 0) == 0) {
830             error = 0;          /* drop this packet */
831             goto bad;
832         }
833
834         /*
835          * Update the time we sent the most recent packet.
836          */
837         if (sc->sc_active_filt.bf_insns == 0
838             || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m0, len, 0))
839             sc->sc_last_sent = time_second;
840
841         *mtod(m0, u_char *) = address;
842 #else
843         /*
844          * Update the time we sent the most recent data packet.
845          */
846         sc->sc_last_sent = time_second;
847 #endif /* PPP_FILTER */
848     }
849
850     /*
851      * See if bpf wants to look at the packet.
852      */
853     if (ifp->if_bpf)
854         bpf_mtap(ifp, m0);
855
856     /*
857      * Put the packet on the appropriate queue.
858      */
859     s = splsoftnet();   /* redundant */
860     if (mode == NPMODE_QUEUE) {
861         /* XXX we should limit the number of packets on this queue */
862         *sc->sc_npqtail = m0;
863         m0->m_nextpkt = NULL;
864         sc->sc_npqtail = &m0->m_nextpkt;
865     } else {
866         /* fastq and if_snd are emptied at spl[soft]net now */
867         ifq = (m0->m_flags & M_HIGHPRI)? &sc->sc_fastq: &ifp->if_snd;
868         if (IF_QFULL(ifq) && dst->sa_family != AF_UNSPEC) {
869             IF_DROP(ifq);
870             splx(s);
871             sc->sc_if.if_oerrors++;
872             sc->sc_stats.ppp_oerrors++;
873             error = ENOBUFS;
874             goto bad;
875         }
876         IF_ENQUEUE(ifq, m0);
877         (*sc->sc_start)(sc);
878     }
879     getmicrotime(&ifp->if_lastchange);
880     ifp->if_opackets++;
881     ifp->if_obytes += len;
882
883     splx(s);
884     return (0);
885
886 bad:
887     m_freem(m0);
888     return (error);
889 }
890
891 /*
892  * After a change in the NPmode for some NP, move packets from the
893  * npqueue to the send queue or the fast queue as appropriate.
894  * Should be called at spl[soft]net.
895  */
896 static void
897 ppp_requeue(sc)
898     struct ppp_softc *sc;
899 {
900     struct mbuf *m, **mpp;
901     struct ifqueue *ifq;
902     enum NPmode mode;
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             ifq = (m->m_flags & M_HIGHPRI)? &sc->sc_fastq: &sc->sc_if.if_snd;
921             if (IF_QFULL(ifq)) {
922                 IF_DROP(ifq);
923                 sc->sc_if.if_oerrors++;
924                 sc->sc_stats.ppp_oerrors++;
925             } else
926                 IF_ENQUEUE(ifq, m);
927             break;
928
929         case NPMODE_DROP:
930         case NPMODE_ERROR:
931             *mpp = m->m_nextpkt;
932             m_freem(m);
933             break;
934
935         case NPMODE_QUEUE:
936             mpp = &m->m_nextpkt;
937             break;
938         }
939     }
940     sc->sc_npqtail = mpp;
941 }
942
943 /*
944  * Transmitter has finished outputting some stuff;
945  * remember to call sc->sc_start later at splsoftnet.
946  */
947 void
948 ppp_restart(sc)
949     struct ppp_softc *sc;
950 {
951     int s = splimp();
952
953     sc->sc_flags &= ~SC_TBUSY;
954     schednetisr(NETISR_PPP);
955     splx(s);
956 }
957
958
959 /*
960  * Get a packet to send.  This procedure is intended to be called at
961  * splsoftnet, since it may involve time-consuming operations such as
962  * applying VJ compression, packet compression, address/control and/or
963  * protocol field compression to the packet.
964  */
965 struct mbuf *
966 ppp_dequeue(sc)
967     struct ppp_softc *sc;
968 {
969     struct mbuf *m, *mp;
970     u_char *cp;
971     int address, control, protocol;
972
973     /*
974      * Grab a packet to send: first try the fast queue, then the
975      * normal queue.
976      */
977     IF_DEQUEUE(&sc->sc_fastq, m);
978     if (m == NULL)
979         IF_DEQUEUE(&sc->sc_if.if_snd, m);
980     if (m == NULL)
981         return NULL;
982
983     ++sc->sc_stats.ppp_opackets;
984
985     /*
986      * Extract the ppp header of the new packet.
987      * The ppp header will be in one mbuf.
988      */
989     cp = mtod(m, u_char *);
990     address = PPP_ADDRESS(cp);
991     control = PPP_CONTROL(cp);
992     protocol = PPP_PROTOCOL(cp);
993
994     switch (protocol) {
995     case PPP_IP:
996 #ifdef VJC
997         /*
998          * If the packet is a TCP/IP packet, see if we can compress it.
999          */
1000         if ((sc->sc_flags & SC_COMP_TCP) && sc->sc_comp != NULL) {
1001             struct ip *ip;
1002             int type;
1003
1004             mp = m;
1005             ip = (struct ip *) (cp + PPP_HDRLEN);
1006             if (mp->m_len <= PPP_HDRLEN) {
1007                 mp = mp->m_next;
1008                 if (mp == NULL)
1009                     break;
1010                 ip = mtod(mp, struct ip *);
1011             }
1012             /* this code assumes the IP/TCP header is in one non-shared mbuf */
1013             if (ip->ip_p == IPPROTO_TCP) {
1014                 type = sl_compress_tcp(mp, ip, sc->sc_comp,
1015                                        !(sc->sc_flags & SC_NO_TCP_CCID));
1016                 switch (type) {
1017                 case TYPE_UNCOMPRESSED_TCP:
1018                     protocol = PPP_VJC_UNCOMP;
1019                     break;
1020                 case TYPE_COMPRESSED_TCP:
1021                     protocol = PPP_VJC_COMP;
1022                     cp = mtod(m, u_char *);
1023                     cp[0] = address;    /* header has moved */
1024                     cp[1] = control;
1025                     cp[2] = 0;
1026                     break;
1027                 }
1028                 cp[3] = protocol;       /* update protocol in PPP header */
1029             }
1030         }
1031 #endif  /* VJC */
1032         break;
1033
1034 #ifdef PPP_COMPRESS
1035     case PPP_CCP:
1036         ppp_ccp(sc, m, 0);
1037         break;
1038 #endif  /* PPP_COMPRESS */
1039     }
1040
1041 #ifdef PPP_COMPRESS
1042     if (protocol != PPP_LCP && protocol != PPP_CCP
1043         && sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
1044         struct mbuf *mcomp = NULL;
1045         int slen, clen;
1046
1047         slen = 0;
1048         for (mp = m; mp != NULL; mp = mp->m_next)
1049             slen += mp->m_len;
1050         clen = (*sc->sc_xcomp->compress)
1051             (sc->sc_xc_state, &mcomp, m, slen, sc->sc_if.if_mtu + PPP_HDRLEN);
1052         if (mcomp != NULL) {
1053             if (sc->sc_flags & SC_CCP_UP) {
1054                 /* Send the compressed packet instead of the original. */
1055                 m_freem(m);
1056                 m = mcomp;
1057                 cp = mtod(m, u_char *);
1058                 protocol = cp[3];
1059             } else {
1060                 /* Can't transmit compressed packets until CCP is up. */
1061                 m_freem(mcomp);
1062             }
1063         }
1064     }
1065 #endif  /* PPP_COMPRESS */
1066
1067     /*
1068      * Compress the address/control and protocol, if possible.
1069      */
1070     if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
1071         control == PPP_UI && protocol != PPP_ALLSTATIONS &&
1072         protocol != PPP_LCP) {
1073         /* can compress address/control */
1074         m->m_data += 2;
1075         m->m_len -= 2;
1076     }
1077     if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
1078         /* can compress protocol */
1079         if (mtod(m, u_char *) == cp) {
1080             cp[2] = cp[1];      /* move address/control up */
1081             cp[1] = cp[0];
1082         }
1083         ++m->m_data;
1084         --m->m_len;
1085     }
1086
1087     return m;
1088 }
1089
1090 #ifdef PPP_COMPRESS
1091 /*
1092  * Handle a CCP packet.  `rcvd' is 1 if the packet was received,
1093  * 0 if it is about to be transmitted.
1094  */
1095 static void
1096 ppp_ccp(sc, m, rcvd)
1097     struct ppp_softc *sc;
1098     struct mbuf *m;
1099     int rcvd;
1100 {
1101     u_char *dp, *ep;
1102     struct mbuf *mp;
1103     int slen, s;
1104
1105     /*
1106      * Get a pointer to the data after the PPP header.
1107      */
1108     if (m->m_len <= PPP_HDRLEN) {
1109         mp = m->m_next;
1110         if (mp == NULL)
1111             return;
1112         dp = (mp != NULL)? mtod(mp, u_char *): NULL;
1113     } else {
1114         mp = m;
1115         dp = mtod(mp, u_char *) + PPP_HDRLEN;
1116     }
1117
1118     ep = mtod(mp, u_char *) + mp->m_len;
1119     if (dp + CCP_HDRLEN > ep)
1120         return;
1121     slen = CCP_LENGTH(dp);
1122     if (dp + slen > ep) {
1123         if (sc->sc_flags & SC_DEBUG)
1124             printf("if_ppp/ccp: not enough data in mbuf (%p+%x > %p+%x)\n",
1125                    dp, slen, mtod(mp, u_char *), mp->m_len);
1126         return;
1127     }
1128
1129     switch (CCP_CODE(dp)) {
1130     case CCP_CONFREQ:
1131     case CCP_TERMREQ:
1132     case CCP_TERMACK:
1133         /* CCP must be going down - disable compression */
1134         if (sc->sc_flags & SC_CCP_UP) {
1135             s = splimp();
1136             sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
1137             splx(s);
1138         }
1139         break;
1140
1141     case CCP_CONFACK:
1142         if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
1143             && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
1144             && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
1145             if (!rcvd) {
1146                 /* we're agreeing to send compressed packets. */
1147                 if (sc->sc_xc_state != NULL
1148                     && (*sc->sc_xcomp->comp_init)
1149                         (sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1150                          sc->sc_if.if_dunit, 0, sc->sc_flags & SC_DEBUG)) {
1151                     s = splimp();
1152                     sc->sc_flags |= SC_COMP_RUN;
1153                     splx(s);
1154                 }
1155             } else {
1156                 /* peer is agreeing to send compressed packets. */
1157                 if (sc->sc_rc_state != NULL
1158                     && (*sc->sc_rcomp->decomp_init)
1159                         (sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1160                          sc->sc_if.if_dunit, 0, sc->sc_mru,
1161                          sc->sc_flags & SC_DEBUG)) {
1162                     s = splimp();
1163                     sc->sc_flags |= SC_DECOMP_RUN;
1164                     sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1165                     splx(s);
1166                 }
1167             }
1168         }
1169         break;
1170
1171     case CCP_RESETACK:
1172         if (sc->sc_flags & SC_CCP_UP) {
1173             if (!rcvd) {
1174                 if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN))
1175                     (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
1176             } else {
1177                 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1178                     (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
1179                     s = splimp();
1180                     sc->sc_flags &= ~SC_DC_ERROR;
1181                     splx(s);
1182                 }
1183             }
1184         }
1185         break;
1186     }
1187 }
1188
1189 /*
1190  * CCP is down; free (de)compressor state if necessary.
1191  */
1192 static void
1193 ppp_ccp_closed(sc)
1194     struct ppp_softc *sc;
1195 {
1196     if (sc->sc_xc_state) {
1197         (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
1198         sc->sc_xc_state = NULL;
1199     }
1200     if (sc->sc_rc_state) {
1201         (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
1202         sc->sc_rc_state = NULL;
1203     }
1204 }
1205 #endif /* PPP_COMPRESS */
1206
1207 /*
1208  * PPP packet input routine.
1209  * The caller has checked and removed the FCS and has inserted
1210  * the address/control bytes and the protocol high byte if they
1211  * were omitted.
1212  */
1213 void
1214 ppppktin(sc, m, lost)
1215     struct ppp_softc *sc;
1216     struct mbuf *m;
1217     int lost;
1218 {
1219     int s = splimp();
1220
1221     if (lost)
1222         m->m_flags |= M_ERRMARK;
1223     IF_ENQUEUE(&sc->sc_rawq, m);
1224     schednetisr(NETISR_PPP);
1225     splx(s);
1226 }
1227
1228 /*
1229  * Process a received PPP packet, doing decompression as necessary.
1230  * Should be called at splsoftnet.
1231  */
1232 #define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
1233                          TYPE_UNCOMPRESSED_TCP)
1234
1235 static void
1236 ppp_inproc(sc, m)
1237     struct ppp_softc *sc;
1238     struct mbuf *m;
1239 {
1240     struct ifnet *ifp = &sc->sc_if;
1241     int isr;
1242     int s, ilen = 0, xlen, proto, rv;
1243     u_char *cp, adrs, ctrl;
1244     struct mbuf *mp, *dmp = NULL;
1245     u_char *iphdr;
1246     u_int hlen;
1247
1248     sc->sc_stats.ppp_ipackets++;
1249
1250     if (sc->sc_flags & SC_LOG_INPKT) {
1251         ilen = 0;
1252         for (mp = m; mp != NULL; mp = mp->m_next)
1253             ilen += mp->m_len;
1254         printf("%s: got %d bytes\n", ifp->if_xname, ilen);
1255         pppdumpm(m);
1256     }
1257
1258     cp = mtod(m, u_char *);
1259     adrs = PPP_ADDRESS(cp);
1260     ctrl = PPP_CONTROL(cp);
1261     proto = PPP_PROTOCOL(cp);
1262
1263     if (m->m_flags & M_ERRMARK) {
1264         m->m_flags &= ~M_ERRMARK;
1265         s = splimp();
1266         sc->sc_flags |= SC_VJ_RESET;
1267         splx(s);
1268     }
1269
1270 #ifdef PPP_COMPRESS
1271     /*
1272      * Decompress this packet if necessary, update the receiver's
1273      * dictionary, or take appropriate action on a CCP packet.
1274      */
1275     if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
1276         && !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
1277         /* decompress this packet */
1278         rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
1279         if (rv == DECOMP_OK) {
1280             m_freem(m);
1281             if (dmp == NULL) {
1282                 /* no error, but no decompressed packet produced */
1283                 return;
1284             }
1285             m = dmp;
1286             cp = mtod(m, u_char *);
1287             proto = PPP_PROTOCOL(cp);
1288
1289         } else {
1290             /*
1291              * An error has occurred in decompression.
1292              * Pass the compressed packet up to pppd, which may take
1293              * CCP down or issue a Reset-Req.
1294              */
1295             if (sc->sc_flags & SC_DEBUG)
1296                 printf("%s: decompress failed %d\n", ifp->if_xname, rv);
1297             s = splimp();
1298             sc->sc_flags |= SC_VJ_RESET;
1299             if (rv == DECOMP_ERROR)
1300                 sc->sc_flags |= SC_DC_ERROR;
1301             else
1302                 sc->sc_flags |= SC_DC_FERROR;
1303             splx(s);
1304         }
1305
1306     } else {
1307         if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1308             (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
1309         }
1310         if (proto == PPP_CCP) {
1311             ppp_ccp(sc, m, 1);
1312         }
1313     }
1314 #endif
1315
1316     ilen = 0;
1317     for (mp = m; mp != NULL; mp = mp->m_next)
1318         ilen += mp->m_len;
1319
1320 #ifdef VJC
1321     if (sc->sc_flags & SC_VJ_RESET) {
1322         /*
1323          * If we've missed a packet, we must toss subsequent compressed
1324          * packets which don't have an explicit connection ID.
1325          */
1326         if (sc->sc_comp)
1327             sl_uncompress_tcp(NULL, 0, TYPE_ERROR, sc->sc_comp);
1328         s = splimp();
1329         sc->sc_flags &= ~SC_VJ_RESET;
1330         splx(s);
1331     }
1332
1333     /*
1334      * See if we have a VJ-compressed packet to uncompress.
1335      */
1336     if (proto == PPP_VJC_COMP) {
1337         if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1338             goto bad;
1339
1340         xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1341                                       ilen - PPP_HDRLEN, TYPE_COMPRESSED_TCP,
1342                                       sc->sc_comp, &iphdr, &hlen);
1343
1344         if (xlen <= 0) {
1345             if (sc->sc_flags & SC_DEBUG)
1346                 printf("%s: VJ uncompress failed on type comp\n",
1347                         ifp->if_xname);
1348             goto bad;
1349         }
1350
1351         /* Copy the PPP and IP headers into a new mbuf. */
1352         MGETHDR(mp, M_DONTWAIT, MT_DATA);
1353         if (mp == NULL)
1354             goto bad;
1355         mp->m_len = 0;
1356         mp->m_next = NULL;
1357         if (hlen + PPP_HDRLEN > MHLEN) {
1358             MCLGET(mp, M_DONTWAIT);
1359             if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) {
1360                 m_freem(mp);
1361                 goto bad;       /* lose if big headers and no clusters */
1362             }
1363         }
1364         cp = mtod(mp, u_char *);
1365         cp[0] = adrs;
1366         cp[1] = ctrl;
1367         cp[2] = 0;
1368         cp[3] = PPP_IP;
1369         proto = PPP_IP;
1370         bcopy(iphdr, cp + PPP_HDRLEN, hlen);
1371         mp->m_len = hlen + PPP_HDRLEN;
1372
1373         /*
1374          * Trim the PPP and VJ headers off the old mbuf
1375          * and stick the new and old mbufs together.
1376          */
1377         m->m_data += PPP_HDRLEN + xlen;
1378         m->m_len -= PPP_HDRLEN + xlen;
1379         if (m->m_len <= M_TRAILINGSPACE(mp)) {
1380             bcopy(mtod(m, u_char *), mtod(mp, u_char *) + mp->m_len, m->m_len);
1381             mp->m_len += m->m_len;
1382             mp->m_next = m_free(m);
1383         } else {
1384             mp->m_next = m;
1385         }
1386         m = mp;
1387         ilen += hlen - xlen;
1388
1389     } else if (proto == PPP_VJC_UNCOMP) {
1390         if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1391             goto bad;
1392
1393         xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1394                                       ilen - PPP_HDRLEN, TYPE_UNCOMPRESSED_TCP,
1395                                       sc->sc_comp, &iphdr, &hlen);
1396
1397         if (xlen < 0) {
1398             if (sc->sc_flags & SC_DEBUG)
1399                 printf("%s: VJ uncompress failed on type uncomp\n",
1400                         ifp->if_xname);
1401             goto bad;
1402         }
1403
1404         proto = PPP_IP;
1405         cp[3] = PPP_IP;
1406     }
1407 #endif /* VJC */
1408
1409     /*
1410      * If the packet will fit in a header mbuf, don't waste a
1411      * whole cluster on it.
1412      */
1413     if (ilen <= MHLEN && M_IS_CLUSTER(m)) {
1414         MGETHDR(mp, M_DONTWAIT, MT_DATA);
1415         if (mp != NULL) {
1416             m_copydata(m, 0, ilen, mtod(mp, caddr_t));
1417             m_freem(m);
1418             m = mp;
1419             m->m_len = ilen;
1420         }
1421     }
1422     m->m_pkthdr.len = ilen;
1423     m->m_pkthdr.rcvif = ifp;
1424
1425     if ((proto & 0x8000) == 0) {
1426 #ifdef PPP_FILTER
1427         /*
1428          * See whether we want to pass this packet, and
1429          * if it counts as link activity.
1430          */
1431         adrs = *mtod(m, u_char *);      /* save address field */
1432         *mtod(m, u_char *) = 0;         /* indicate inbound */
1433         if (sc->sc_pass_filt.bf_insns != 0
1434             && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m,
1435                           ilen, 0) == 0) {
1436             /* drop this packet */
1437             m_freem(m);
1438             return;
1439         }
1440         if (sc->sc_active_filt.bf_insns == 0
1441             || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m, ilen, 0))
1442             sc->sc_last_recv = time_second;
1443
1444         *mtod(m, u_char *) = adrs;
1445 #else
1446         /*
1447          * Record the time that we received this packet.
1448          */
1449         sc->sc_last_recv = time_second;
1450 #endif /* PPP_FILTER */
1451     }
1452
1453     /* See if bpf wants to look at the packet. */
1454     if (sc->sc_if.if_bpf)
1455         bpf_mtap(&sc->sc_if, m);
1456
1457     isr = -1;
1458     switch (proto) {
1459 #ifdef INET
1460     case PPP_IP:
1461         /*
1462          * IP packet - take off the ppp header and pass it up to IP.
1463          */
1464         if ((ifp->if_flags & IFF_UP) == 0
1465             || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
1466             /* interface is down - drop the packet. */
1467             m_freem(m);
1468             return;
1469         }
1470         m->m_pkthdr.len -= PPP_HDRLEN;
1471         m->m_data += PPP_HDRLEN;
1472         m->m_len -= PPP_HDRLEN;
1473         if (ipflow_fastforward(m))
1474             return;
1475         isr = NETISR_IP;
1476         break;
1477 #endif
1478 #ifdef IPX
1479     case PPP_IPX:
1480         /*
1481          * IPX packet - take off the ppp header and pass it up to IPX.
1482          */
1483         if ((sc->sc_if.if_flags & IFF_UP) == 0
1484             /* XXX: || sc->sc_npmode[NP_IPX] != NPMODE_PASS*/) {
1485             /* interface is down - drop the packet. */
1486             m_freem(m);
1487             return;
1488         }
1489         m->m_pkthdr.len -= PPP_HDRLEN;
1490         m->m_data += PPP_HDRLEN;
1491         m->m_len -= PPP_HDRLEN;
1492         isr = NETISR_IPX;
1493         sc->sc_last_recv = time_second; /* update time of last pkt rcvd */
1494         break;
1495 #endif
1496
1497     default:
1498         /*
1499          * Some other protocol - place on input queue for read().
1500          */
1501         break;
1502     }
1503
1504     if (isr == -1)
1505         rv = IF_HANDOFF(&sc->sc_inq, m, NULL);
1506     else
1507         rv = (netisr_queue(isr, m) == 0);
1508     if (!rv) {
1509         if (sc->sc_flags & SC_DEBUG)
1510             printf("%s: input queue full\n", ifp->if_xname);
1511         ifp->if_iqdrops++;
1512         goto bad;
1513     }
1514
1515     ifp->if_ipackets++;
1516     ifp->if_ibytes += ilen;
1517     getmicrotime(&ifp->if_lastchange);
1518
1519     if (rv)
1520         (*sc->sc_ctlp)(sc);
1521
1522     return;
1523
1524  bad:
1525     m_freem(m);
1526     sc->sc_if.if_ierrors++;
1527     sc->sc_stats.ppp_ierrors++;
1528 }
1529
1530 #define MAX_DUMP_BYTES  128
1531
1532 static void
1533 pppdumpm(m0)
1534     struct mbuf *m0;
1535 {
1536     char buf[3*MAX_DUMP_BYTES+4];
1537     char *bp = buf;
1538     struct mbuf *m;
1539
1540     for (m = m0; m; m = m->m_next) {
1541         int l = m->m_len;
1542         u_char *rptr = (u_char *)m->m_data;
1543
1544         while (l--) {
1545             if (bp > buf + sizeof(buf) - 4)
1546                 goto done;
1547             *bp++ = hex2ascii(*rptr >> 4);
1548             *bp++ = hex2ascii(*rptr++ & 0xf);
1549         }
1550
1551         if (m->m_next) {
1552             if (bp > buf + sizeof(buf) - 3)
1553                 goto done;
1554             *bp++ = '|';
1555         } else
1556             *bp++ = ' ';
1557     }
1558 done:
1559     if (m)
1560         *bp++ = '>';
1561     *bp = 0;
1562     printf("%s\n", buf);
1563 }