]> git.ozlabs.org Git - ppp.git/blob - svr4/ppp_comp.c
34eda2428ef1203f96d813941938f5a7948faff5
[ppp.git] / svr4 / ppp_comp.c
1 /*
2  * ppp_comp.c - STREAMS module for kernel-level compression and CCP support.
3  *
4  * Copyright (c) 1994 The Australian National University.
5  * All rights reserved.
6  *
7  * Permission to use, copy, modify, and distribute this software and its
8  * documentation is hereby granted, provided that the above copyright
9  * notice appears in all copies.  This software is provided without any
10  * warranty, express or implied. The Australian National University
11  * makes no representations about the suitability of this software for
12  * any purpose.
13  *
14  * IN NO EVENT SHALL THE AUSTRALIAN NATIONAL UNIVERSITY BE LIABLE TO ANY
15  * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
16  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
17  * THE AUSTRALIAN NATIONAL UNIVERSITY HAVE BEEN ADVISED OF THE POSSIBILITY
18  * OF SUCH DAMAGE.
19  *
20  * THE AUSTRALIAN NATIONAL UNIVERSITY SPECIFICALLY DISCLAIMS ANY WARRANTIES,
21  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
22  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
23  * ON AN "AS IS" BASIS, AND THE AUSTRALIAN NATIONAL UNIVERSITY HAS NO
24  * OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
25  * OR MODIFICATIONS.
26  *
27  * $Id: ppp_comp.c,v 1.5 1995/10/27 03:56:19 paulus Exp $
28  */
29
30 /*
31  * This file is used under SVR4 and Solaris 2.
32  */
33
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <sys/errno.h>
37 #include <sys/stream.h>
38 #include <sys/conf.h>
39 #include <sys/kmem.h>
40 #include <sys/cmn_err.h>
41 #include <sys/ddi.h>
42 #ifdef sun
43 #include <sys/modctl.h>
44 #include <sys/sunddi.h>
45 #endif
46 #include <net/ppp_defs.h>
47 #include <net/pppio.h>
48 #include <netinet/in.h>
49 #include <netinet/in_systm.h>
50 #include <netinet/ip.h>
51 #include <net/vjcompress.h>
52
53 #define PACKETPTR       mblk_t *
54 #include <net/ppp-comp.h>
55
56 static int ppp_comp_open __P((queue_t *, dev_t *, int, int, cred_t *));
57 static int ppp_comp_close __P((queue_t *, int, cred_t *));
58 static int ppp_comp_rput __P((queue_t *, mblk_t *));
59 static int ppp_comp_rsrv __P((queue_t *));
60 static int ppp_comp_wput __P((queue_t *, mblk_t *));
61 static int ppp_comp_wsrv __P((queue_t *));
62 static void ppp_comp_ccp __P((queue_t *, mblk_t *, int));
63
64 static struct module_info minfo = {
65     0xbadf, "ppp_comp", 0, INFPSZ, 16384, 4096,
66 };
67
68 static struct qinit r_init = {
69     ppp_comp_rput, ppp_comp_rsrv, ppp_comp_open, ppp_comp_close,
70     NULL, &minfo, NULL
71 };
72
73 static struct qinit w_init = {
74     ppp_comp_wput, ppp_comp_wsrv, NULL, NULL, NULL, &minfo, NULL
75 };
76
77 static struct streamtab ppp_compinfo = {
78     &r_init, &w_init, NULL, NULL
79 };
80
81 #if defined(sun) && defined(svr4)               /* Solaris 2 */
82 static struct fmodsw fsw = {
83     "ppp_comp",
84     &ppp_compinfo,
85     D_NEW | D_MP | D_MTQPAIR
86 };
87
88 extern struct mod_ops mod_strmodops;
89
90 static struct modlstrmod modlstrmod = {
91     &mod_strmodops,
92     "PPP compression module",
93     &fsw
94 };
95
96 static struct modlinkage modlinkage = {
97     MODREV_1,
98     (void *) &modlstrmod,
99     NULL
100 };
101 #endif
102
103 typedef struct comp_state {
104     int         flags;
105     int         mru;
106     int         mtu;
107     int         unit;
108     struct compressor *xcomp;
109     void        *xstate;
110     struct compressor *rcomp;
111     void        *rstate;
112     struct vjcompress vj_comp;
113     int         vj_last_ierrors;
114     struct pppstat stats;
115 } comp_state_t;
116
117 /* Bits in flags are as defined in pppio.h. */
118 #define CCP_ERR         (CCP_ERROR | CCP_FATALERROR)
119 #define LAST_MOD        0x1000000       /* no ppp modules below us */
120
121 #define MAX_IPHDR       128     /* max TCP/IP header size */
122 #define MAX_VJHDR       20      /* max VJ compressed header size (?) */
123
124 #undef MIN              /* just in case */
125 #define MIN(a, b)       ((a) < (b)? (a): (b))
126
127 #ifdef D_MP
128 /* Use msgpullup if we have other multithreading support. */
129 #define PULLUP(mp, len)                         \
130     do {                                        \
131         mblk_t *np = msgpullup((mp), (len));    \
132         freemsg(mp);                            \
133         mp = np;                                \
134     } while (0)
135
136 #else
137 /* Use pullupmsg if we don't have any multithreading support. */
138 #define PULLUP(mp, len)                 \
139     do {                                \
140         if (!pullupmsg((mp), (len))) {  \
141             freemsg(mp);                \
142             mp = 0;                     \
143         }                               \
144     } while (0)
145
146 #endif
147
148
149 /*
150  * List of compressors we know about.
151  */
152
153 extern struct compressor ppp_bsd_compress;
154
155 struct compressor *ppp_compressors[] = {
156 #if DO_BSD_COMPRESS
157     &ppp_bsd_compress,
158 #endif
159     NULL
160 };
161
162 #ifdef sun
163 /*
164  * Entry points for modloading.
165  */
166 int
167 _init(void)
168 {
169     return mod_install(&modlinkage);
170 }
171
172 int
173 _fini(void)
174 {
175     return mod_remove(&modlinkage);
176 }
177
178 int
179 _info(mip)
180     struct modinfo *mip;
181 {
182     return mod_info(&modlinkage, mip);
183 }
184 #endif
185
186 #ifndef sun
187 # define qprocson(q)
188 # define qprocsoff(q)
189 #define canputnext(q)   canput((q)->q_next)
190 #endif
191
192 /*
193  * STREAMS module entry points.
194  */
195 static int
196 ppp_comp_open(q, devp, flag, sflag, credp)
197     queue_t *q;
198     dev_t *devp;
199     int flag, sflag;
200     cred_t *credp;
201 {
202     comp_state_t *cp;
203
204     if (q->q_ptr == NULL) {
205         cp = (comp_state_t *) kmem_zalloc(sizeof(comp_state_t), KM_SLEEP);
206         if (cp == NULL)
207             return ENOSR;
208         WR(q)->q_ptr = q->q_ptr = cp;
209         bzero((caddr_t)cp, sizeof(comp_state_t));
210         cp->mru = PPP_MRU;
211         cp->mtu = PPP_MRU;
212         cp->xstate = NULL;
213         cp->rstate = NULL;
214         vj_compress_init(&cp->vj_comp, -1);
215         qprocson(q);
216     }
217     return 0;
218 }
219
220 static int
221 ppp_comp_close(q, flag, credp)
222     queue_t *q;
223     int flag;
224     cred_t *credp;
225 {
226     comp_state_t *cp;
227
228     qprocsoff(q);
229     cp = (comp_state_t *) q->q_ptr;
230     if (cp != NULL) {
231         if (cp->xstate != NULL)
232             (*cp->xcomp->comp_free)(cp->xstate);
233         if (cp->rstate != NULL)
234             (*cp->rcomp->decomp_free)(cp->rstate);
235         kmem_free(cp, sizeof(comp_state_t));
236         q->q_ptr = NULL;
237         OTHERQ(q)->q_ptr = NULL;
238     }
239     return 0;
240 }
241
242 static int
243 ppp_comp_wput(q, mp)
244     queue_t *q;
245     mblk_t *mp;
246 {
247     struct iocblk *iop;
248     comp_state_t *cp;
249     int error, len;
250     int flags, mask;
251     mblk_t *np;
252     struct compressor **comp;
253     struct ppp_stats *psp;
254     struct ppp_comp_stats *csp;
255     unsigned char *opt_data;
256     int nxslots, nrslots;
257
258     cp = (comp_state_t *) q->q_ptr;
259     switch (mp->b_datap->db_type) {
260
261     case M_DATA:
262         putq(q, mp);
263         break;
264
265     case M_IOCTL:
266         iop = (struct iocblk *) mp->b_rptr;
267         error = EINVAL;
268         switch (iop->ioc_cmd) {
269
270         case PPPIO_CFLAGS:
271             /* set/get CCP state */
272             if (iop->ioc_count != 2 * sizeof(int))
273                 break;
274             flags = ((int *) mp->b_cont->b_rptr)[0];
275             mask = ((int *) mp->b_cont->b_rptr)[1];
276             cp->flags = (cp->flags & ~mask) | (flags & mask);
277             if ((mask & CCP_ISOPEN) && (flags & CCP_ISOPEN) == 0) {
278                 if (cp->xstate != NULL) {
279                     (*cp->xcomp->comp_free)(cp->xstate);
280                     cp->xstate = NULL;
281                 }
282                 if (cp->rstate != NULL) {
283                     (*cp->rcomp->decomp_free)(cp->rstate);
284                     cp->rstate = NULL;
285                 }
286                 cp->flags &= ~CCP_ISUP;
287             }
288             error = 0;
289             iop->ioc_count = sizeof(int);
290             ((int *) mp->b_cont->b_rptr)[0] = cp->flags;
291             mp->b_cont->b_wptr = mp->b_cont->b_rptr + sizeof(int);
292             break;
293
294         case PPPIO_VJINIT:
295             /*
296              * Initialize VJ compressor/decompressor
297              */
298             if (iop->ioc_count != 2)
299                 break;
300             nxslots = mp->b_cont->b_rptr[0] + 1;
301             nrslots = mp->b_cont->b_rptr[1] + 1;
302             if (nxslots > MAX_STATES || nrslots > MAX_STATES)
303                 break;
304             vj_compress_init(&cp->vj_comp, nxslots);
305             cp->vj_last_ierrors = cp->stats.ppp_ierrors;
306             error = 0;
307             iop->ioc_count = 0;
308             break;
309
310         case PPPIO_XCOMP:
311         case PPPIO_RCOMP:
312             if (iop->ioc_count <= 0)
313                 break;
314             opt_data = mp->b_cont->b_rptr;
315             len = mp->b_cont->b_wptr - opt_data;
316             if (len > iop->ioc_count)
317                 len = iop->ioc_count;
318             if (opt_data[1] < 2 || opt_data[1] > len)
319                 break;
320             for (comp = ppp_compressors; *comp != NULL; ++comp)
321                 if ((*comp)->compress_proto == opt_data[0]) {
322                     /* here's the handler! */
323                     error = 0;
324                     if (iop->ioc_cmd == PPPIO_XCOMP) {
325                         if (cp->xstate != NULL)
326                             (*cp->xcomp->comp_free)(cp->xstate);
327                         cp->xcomp = *comp;
328                         cp->xstate = (*comp)->comp_alloc(opt_data, len);
329                         if (cp->xstate == NULL)
330                             error = ENOSR;
331                     } else {
332                         if (cp->rstate != NULL)
333                             (*cp->rcomp->decomp_free)(cp->rstate);
334                         cp->rcomp = *comp;
335                         cp->rstate = (*comp)->decomp_alloc(opt_data, len);
336                         if (cp->rstate == NULL)
337                             error = ENOSR;
338                     }
339                     break;
340                 }
341             iop->ioc_count = 0;
342             break;
343
344         case PPPIO_GETSTAT:
345             if ((cp->flags & LAST_MOD) == 0) {
346                 error = -1;     /* let the ppp_ahdl module handle it */
347                 break;
348             }
349             np = allocb(sizeof(struct ppp_stats), BPRI_HI);
350             if (np == 0) {
351                 error = ENOSR;
352                 break;
353             }
354             if (mp->b_cont != 0)
355                 freemsg(mp->b_cont);
356             mp->b_cont = np;
357             psp = (struct ppp_stats *) np->b_wptr;
358             np->b_wptr += sizeof(struct ppp_stats);
359             iop->ioc_count = sizeof(struct ppp_stats);
360             psp->p = cp->stats;
361             psp->vj = cp->vj_comp.stats;
362             error = 0;
363             break;
364
365         case PPPIO_GETCSTAT:
366             np = allocb(sizeof(struct ppp_comp_stats), BPRI_HI);
367             if (np == 0) {
368                 error = ENOSR;
369                 break;
370             }
371             if (mp->b_cont != 0)
372                 freemsg(mp->b_cont);
373             mp->b_cont = np;
374             csp = (struct ppp_comp_stats *) np->b_wptr;
375             np->b_wptr += sizeof(struct ppp_comp_stats);
376             iop->ioc_count = sizeof(struct ppp_comp_stats);
377             bzero((caddr_t)csp, sizeof(struct ppp_comp_stats));
378             if (cp->xstate != 0)
379                 (*cp->xcomp->comp_stat)(cp->xstate, &csp->c);
380             if (cp->rstate != 0)
381                 (*cp->rcomp->decomp_stat)(cp->rstate, &csp->d);
382             error = 0;
383             break;
384
385         case PPPIO_LASTMOD:
386             cp->flags |= LAST_MOD;
387             error = 0;
388             break;
389
390         default:
391             error = -1;
392             break;
393         }
394
395         if (error < 0)
396             putnext(q, mp);
397         else if (error == 0) {
398             mp->b_datap->db_type = M_IOCACK;
399             qreply(q, mp);
400         } else {
401             mp->b_datap->db_type = M_IOCNAK;
402             iop->ioc_error = error;
403             iop->ioc_count = 0;
404             qreply(q, mp);
405         }
406         break;
407
408     case M_CTL:
409         switch (*mp->b_rptr) {
410         case PPPCTL_MTU:
411             cp->mtu = ((unsigned short *)mp->b_rptr)[1];
412             break;
413         case PPPCTL_MRU:
414             cp->mru = ((unsigned short *)mp->b_rptr)[1];
415             break;
416         case PPPCTL_UNIT:
417             cp->unit = mp->b_rptr[1];
418             break;
419         }
420         putnext(q, mp);
421         break;
422
423     default:
424         putnext(q, mp);
425     }
426 }
427
428 static int
429 ppp_comp_wsrv(q)
430     queue_t *q;
431 {
432     mblk_t *mp, *cmp = NULL, *np;
433     comp_state_t *cp;
434     int len, proto, type;
435     struct ip *ip;
436     unsigned char *vjhdr, *dp;
437
438     cp = (comp_state_t *) q->q_ptr;
439     while ((mp = getq(q)) != 0) {
440         /* assert(mp->b_datap->db_type == M_DATA) */
441         if (!canputnext(q)) {
442             putbq(q, mp);
443             return;
444         }
445
446         /*
447          * Make sure we've got a reasonable amount in the first
448          * mblk and that we are its only user.
449          * Then find out what the protocol is.
450          */
451         len = msgdsize(mp);
452         if (len > PPP_HDRLEN + MAX_IPHDR)
453             len = PPP_HDRLEN + MAX_IPHDR;
454         if (mp->b_wptr < mp->b_rptr + len || mp->b_datap->db_ref > 1) {
455             PULLUP(mp, len);
456             if (mp == 0) {
457 #if DEBUG
458                 cmn_err(CE_CONT, "ppp_comp_wsrv: pullup failed\n");
459 #endif
460                 cp->stats.ppp_oerrors++;
461                 putctl1(RD(q)->q_next, M_CTL, PPPCTL_OERROR);
462                 continue;
463             }
464         }
465         proto = PPP_PROTOCOL(mp->b_rptr);
466
467         /*
468          * Do VJ compression if requested.
469          */
470         if (proto == PPP_IP && (cp->flags & COMP_VJC)) {
471             ip = (struct ip *) (mp->b_rptr + PPP_HDRLEN);
472             if (ip->ip_p == IPPROTO_TCP) {
473                 type = vj_compress_tcp(ip, len - PPP_HDRLEN, &cp->vj_comp,
474                                        (cp->flags & COMP_VJCCID), &vjhdr);
475                 switch (type) {
476                 case TYPE_UNCOMPRESSED_TCP:
477                     mp->b_rptr[3] = proto = PPP_VJC_UNCOMP;
478                     break;
479                 case TYPE_COMPRESSED_TCP:
480                     dp = vjhdr - PPP_HDRLEN;
481                     dp[1] = mp->b_rptr[1]; /* copy control field */
482                     dp[0] = mp->b_rptr[0]; /* copy address field */
483                     dp[2] = 0;             /* set protocol field */
484                     dp[3] = proto = PPP_VJC_COMP;
485                     mp->b_rptr = dp;
486                     break;
487                 }
488             }
489         }
490
491         /*
492          * Do packet compression if enabled.
493          */
494         if (proto == PPP_CCP)
495             ppp_comp_ccp(q, mp, 0);
496         else if (proto != PPP_LCP && (cp->flags & CCP_COMP_RUN)
497                  && cp->xstate != NULL) {
498             len = msgdsize(mp);
499             (*cp->xcomp->compress)(cp->xstate, &cmp, mp, len,
500                                    (cp->flags & CCP_ISUP? cp->mtu: 0));
501             if (cmp != NULL) {
502                 freemsg(mp);
503                 mp = cmp;
504             }
505         }
506
507         /*
508          * Do address/control and protocol compression if enabled.
509          */
510         if (proto != PPP_LCP && (cp->flags & COMP_AC)) {
511             mp->b_rptr += 2;    /* drop the address & ctrl fields */
512             if (proto < 0x100 && (cp->flags & COMP_PROT))
513                 ++mp->b_rptr;   /* drop the high protocol byte */
514         } else if (proto < 0x100 && (cp->flags & COMP_PROT)) {
515             /* shuffle up the address & ctrl fields */
516             mp->b_rptr[2] = mp->b_rptr[1];
517             mp->b_rptr[1] = mp->b_rptr[0];
518             ++mp->b_rptr;
519         }
520
521         cp->stats.ppp_opackets++;
522         cp->stats.ppp_obytes += msgdsize(mp);
523         putnext(q, mp);
524     }
525 }
526
527 static int
528 ppp_comp_rput(q, mp)
529     queue_t *q;
530     mblk_t *mp;
531 {
532     comp_state_t *cp;
533     struct iocblk *iop;
534     struct ppp_stats *psp;
535
536     cp = (comp_state_t *) q->q_ptr;
537     switch (mp->b_datap->db_type) {
538
539     case M_DATA:
540         putq(q, mp);
541         break;
542
543     case M_IOCACK:
544         iop = (struct iocblk *) mp->b_rptr;
545         switch (iop->ioc_cmd) {
546         case PPPIO_GETSTAT:
547             /*
548              * Catch this on the way back from the ppp_ahdl module
549              * so we can fill in the VJ stats.
550              */
551             if (mp->b_cont == 0 || iop->ioc_count != sizeof(struct ppp_stats))
552                 break;
553             psp = (struct ppp_stats *) mp->b_cont->b_rptr;
554             psp->vj = cp->vj_comp.stats;
555             break;
556         }
557         putnext(q, mp);
558         break;
559
560     case M_CTL:
561         switch (mp->b_rptr[0]) {
562         case PPPCTL_IERROR:
563             ++cp->stats.ppp_ierrors;
564             break;
565         case PPPCTL_OERROR:
566             ++cp->stats.ppp_oerrors;
567             break;
568         }
569         putnext(q, mp);
570         break;
571
572     default:
573         putnext(q, mp);
574     }
575 }
576
577 static int
578 ppp_comp_rsrv(q)
579     queue_t *q;
580 {
581     int proto, rv, i;
582     mblk_t *mp, *dmp = NULL, *np;
583     uchar_t *dp, *iphdr;
584     comp_state_t *cp;
585     int len, hlen, vjlen, iphlen;
586
587     cp = (comp_state_t *) q->q_ptr;
588     while ((mp = getq(q)) != 0) {
589         /* assert(mp->b_datap->db_type == M_DATA) */
590         if (!canputnext(q)) {
591             putbq(q, mp);
592             return;
593         }
594
595         len = msgdsize(mp);
596         cp->stats.ppp_ibytes += len;
597         cp->stats.ppp_ipackets++;
598
599         /*
600          * First do address/control and protocol "decompression".
601          */
602         hlen = MIN(len, PPP_HDRLEN);
603         if (mp->b_wptr < mp->b_rptr + hlen) {
604             PULLUP(mp, hlen);
605             if (mp == 0)
606                 goto bad;
607         }
608         dp = mp->b_rptr;
609         if (PPP_ADDRESS(dp) == PPP_ALLSTATIONS
610             && PPP_CONTROL(dp) == PPP_UI)
611             dp += 2;                    /* skip address/control */
612         else if ((cp->flags & DECOMP_AC) == 0) {
613             /* count these? */
614             goto bad;
615         }
616         proto = 0;
617         if ((*dp & 1) == 0)
618             proto = *dp++ << 8;         /* grab high byte of protocol */
619         else if ((cp->flags & DECOMP_PROT) == 0) {
620             /* count these? */
621             goto bad;
622         }
623         proto += *dp++;                 /* grab low byte of protocol */
624         if (dp > mp->b_wptr)
625             goto bad;                   /* short/bogus packet */
626         dp -= PPP_HDRLEN;
627         if (dp != mp->b_rptr) {
628             /*
629              * We need to put some bytes on the front of the packet
630              * to make a full-length PPP header.
631              * If we can put them in *mp, we do, otherwise we
632              * tack another mblk on the front.
633              * XXX we really shouldn't need to carry around
634              * the address and control at this stage.
635              */
636             if (dp < mp->b_datap->db_base || mp->b_datap->db_ref > 1) {
637                 np = allocb(PPP_HDRLEN, BPRI_MED);
638                 if (np == 0)
639                     goto bad;
640                 np->b_cont = mp;
641                 mp->b_rptr = dp + PPP_HDRLEN;
642                 mp = np;
643                 dp = mp->b_wptr;
644                 mp->b_wptr += PPP_HDRLEN;
645             } else
646                 mp->b_rptr = dp;
647
648             dp[0] = PPP_ALLSTATIONS;
649             dp[1] = PPP_UI;
650             dp[2] = proto >> 8;
651             dp[3] = proto;
652         }
653
654         /*
655          * Now see if we have a compressed packet to decompress,
656          * or a CCP packet to take notice of.
657          */
658         proto = PPP_PROTOCOL(mp->b_rptr);
659         if (proto == PPP_CCP)
660             ppp_comp_ccp(q, mp, 1);
661         else if (proto == PPP_COMP) {
662             if ((cp->flags & CCP_ISUP)
663                 && (cp->flags & CCP_DECOMP_RUN) && cp->rstate
664                 && (cp->flags & CCP_ERR) == 0) {
665                 rv = (*cp->rcomp->decompress)(cp->rstate, mp, &dmp);
666                 switch (rv) {
667                 case DECOMP_OK:
668                     freemsg(mp);
669                     mp = dmp;
670                     if (mp == NULL) {
671                         /* no error, but no packet returned either. */
672                         continue;
673                     }
674                     break;
675                 case DECOMP_ERROR:
676                     cp->flags |= CCP_ERROR;
677                     ++cp->stats.ppp_ierrors;
678                     putctl1(q->q_next, M_CTL, PPPCTL_IERROR);
679                     break;
680                 case DECOMP_FATALERROR:
681                     cp->flags |= CCP_FATALERROR;
682                     ++cp->stats.ppp_ierrors;
683                     putctl1(q->q_next, M_CTL, PPPCTL_IERROR);
684                     break;
685                 }
686             }
687         } else if (cp->rstate && (cp->flags & CCP_DECOMP_RUN)) {
688             (*cp->rcomp->incomp)(cp->rstate, mp);
689         }
690
691         /*
692          * Now do VJ decompression.
693          */
694         proto = PPP_PROTOCOL(mp->b_rptr);
695         if (proto == PPP_VJC_COMP || proto == PPP_VJC_UNCOMP) {
696             len = msgdsize(mp) - PPP_HDRLEN;
697             if ((cp->flags & DECOMP_VJC) == 0 || len <= 0)
698                 goto bad;
699
700             /*
701              * Advance past the ppp header.
702              * Here we assume that the whole PPP header is in the first mblk.
703              */
704             np = mp;
705             dp = np->b_rptr + PPP_HDRLEN;
706             if (dp >= mp->b_wptr) {
707                 np = np->b_cont;
708                 dp = np->b_rptr;
709             }
710
711             if (proto == PPP_VJC_COMP) {
712                 hlen = MIN(len, MAX_VJHDR);
713                 if (np->b_wptr < dp + hlen) {
714                     PULLUP(mp, hlen + PPP_HDRLEN);
715                     if (mp == 0)
716                         goto bad;
717                     np = mp;
718                     dp = np->b_rptr + PPP_HDRLEN;
719                 }
720
721                 if (cp->stats.ppp_ierrors != cp->vj_last_ierrors) {
722                     vj_uncompress_err(&cp->vj_comp);
723                     cp->vj_last_ierrors = cp->stats.ppp_ierrors;
724                 }
725
726                 vjlen = vj_uncompress_tcp(dp, np->b_wptr - dp, len,
727                                           &cp->vj_comp, &iphdr, &iphlen);
728                 if (vjlen < 0)
729                     goto bad;
730
731                 /* drop ppp and vj headers off */
732                 if (mp != np) {
733                     freeb(mp);
734                     mp = np;
735                 }
736                 mp->b_rptr = dp + vjlen;
737
738                 /* allocate a new mblk for the ppp and ip headers */
739                 if ((np = allocb(iphlen + PPP_HDRLEN + 4, BPRI_MED)) == 0)
740                     goto bad;
741                 dp = np->b_rptr;        /* prepend mblk with TCP/IP hdr */
742                 dp[0] = PPP_ALLSTATIONS; /* reconstruct PPP header */
743                 dp[1] = PPP_UI;
744                 dp[2] = PPP_IP >> 8;
745                 dp[3] = PPP_IP;
746                 bcopy(iphdr, dp + PPP_HDRLEN, iphlen);
747                 np->b_wptr = dp + iphlen + PPP_HDRLEN;
748                 np->b_cont = mp;
749
750                 /* XXX there seems to be a bug which causes panics in strread
751                    if we make an mbuf with only the IP header in it :-( */
752                 if (mp->b_wptr - mp->b_rptr > 4) {
753                     bcopy(mp->b_rptr, np->b_wptr, 4);
754                     mp->b_rptr += 4;
755                     np->b_wptr += 4;
756                 } else {
757                     bcopy(mp->b_rptr, np->b_wptr, mp->b_wptr - mp->b_rptr);
758                     np->b_wptr += mp->b_wptr - mp->b_rptr;
759                     np->b_cont = mp->b_cont;
760                     freeb(mp);
761                 }
762
763                 mp = np;
764
765             } else {
766                 hlen = MIN(len, MAX_IPHDR);
767                 if (np->b_wptr < dp + hlen || np->b_datap->db_ref > 1
768                     || mp->b_datap->db_ref > 1) {
769                     PULLUP(mp, hlen + PPP_HDRLEN);
770                     if (mp == 0)
771                         goto bad;
772                     np = mp;
773                     dp = np->b_rptr + PPP_HDRLEN;
774                 }
775
776                 if (!vj_uncompress_uncomp(dp, &cp->vj_comp))
777                     goto bad;
778                 mp->b_rptr[3] = PPP_IP; /* fix up the PPP protocol field */
779             }
780         }
781
782         putnext(q, mp);
783         continue;
784
785     bad:
786         if (mp != 0)
787             freemsg(mp);
788         cp->stats.ppp_ierrors++;
789         putctl1(q->q_next, M_CTL, PPPCTL_IERROR);
790     }
791 }
792
793 /*
794  * Handle a CCP packet being sent or received.
795  */
796 static void
797 ppp_comp_ccp(q, mp, rcvd)
798     queue_t *q;
799     mblk_t *mp;
800     int rcvd;
801 {
802     int len, clen;
803     comp_state_t *cp;
804     unsigned char *dp;
805     mblk_t *np;
806
807     len = msgdsize(mp);
808     if (len < PPP_HDRLEN + CCP_HDRLEN)
809         return;
810     if (mp->b_wptr < mp->b_rptr + len) {
811         /* XXX this isn't right, because it may free mp */
812         PULLUP(mp, len);
813         if (mp == 0) {
814             cmn_err(CE_CONT, "ppp_comp_ccp: pullup failed\n");
815             return;
816         }
817     }
818     np = mp;
819
820     cp = (comp_state_t *) q->q_ptr;
821     dp = mp->b_rptr + PPP_HDRLEN;
822     len -= PPP_HDRLEN;
823     clen = CCP_LENGTH(dp);
824     if (clen > len)
825         goto bad;
826
827     switch (CCP_CODE(dp)) {
828     case CCP_CONFREQ:
829     case CCP_TERMREQ:
830     case CCP_TERMACK:
831         cp->flags &= ~CCP_ISUP;
832         break;
833
834     case CCP_CONFACK:
835         if ((cp->flags & (CCP_ISOPEN | CCP_ISUP)) == CCP_ISOPEN
836             && clen >= CCP_HDRLEN + CCP_OPT_MINLEN
837             && clen >= CCP_HDRLEN + CCP_OPT_LENGTH(dp + CCP_HDRLEN)) {
838             if (!rcvd) {
839                 if (cp->xstate != NULL
840                     && (*cp->xcomp->comp_init)
841                         (cp->xstate, dp + CCP_HDRLEN, clen - CCP_HDRLEN,
842                          cp->unit, 0, 0))
843                     cp->flags |= CCP_COMP_RUN;
844             } else {
845                 if (cp->rstate != NULL
846                     && (*cp->rcomp->decomp_init)
847                         (cp->rstate, dp + CCP_HDRLEN, clen - CCP_HDRLEN,
848                          cp->unit, 0, cp->mru, 0))
849                     cp->flags = (cp->flags & ~CCP_ERR)
850                         | CCP_DECOMP_RUN;
851             }
852         }
853         break;
854
855     case CCP_RESETACK:
856         if (cp->flags & CCP_ISUP) {
857             if (!rcvd) {
858                 if (cp->xstate && (cp->flags & CCP_COMP_RUN))
859                     (*cp->xcomp->comp_reset)(cp->xstate);
860             } else {
861                 if (cp->rstate && (cp->flags & CCP_DECOMP_RUN)) {
862                     (*cp->rcomp->decomp_reset)(cp->rstate);
863                     cp->flags &= ~CCP_ERROR;
864                 }
865             }
866         }
867         break;
868     }
869
870  bad:
871     if (np != mp)
872         freemsg(np);
873 }
874
875 #if DEBUG
876 dump_msg(mp)
877     mblk_t *mp;
878 {
879     dblk_t *db;
880
881     while (mp != 0) {
882         db = mp->b_datap;
883         cmn_err(CE_CONT, "mp=%x cont=%x rptr=%x wptr=%x datap=%x\n",
884                 mp, mp->b_cont, mp->b_rptr, mp->b_wptr, db);
885         cmn_err(CE_CONT, "  base=%x lim=%x ref=%d type=%d struioflag=%d\n",
886                 db->db_base, db->db_lim, db->db_ref, db->db_type,
887                 db->db_struioflag);
888         mp = mp->b_cont;
889     }
890 }
891 #endif