]> git.ozlabs.org Git - ppp.git/blob - modules/ppp_comp.c
support old draft rfc deflate number
[ppp.git] / modules / 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 HAS 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.8 1997/04/30 05:45:15 paulus Exp $
28  */
29
30 /*
31  * This file is used under SVR4, Solaris 2, SunOS 4, and Digital UNIX.
32  */
33
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <sys/errno.h>
37 #include <sys/stream.h>
38
39 #ifdef SVR4
40 #include <sys/conf.h>
41 #include <sys/cmn_err.h>
42 #include <sys/ddi.h>
43 #else
44 #include <sys/user.h>
45 #ifdef __osf__
46 #include <sys/cmn_err.h>
47 #endif
48 #endif /* SVR4 */
49
50 #include <net/ppp_defs.h>
51 #include <net/pppio.h>
52 #include "ppp_mod.h"
53
54 #ifdef __osf__
55 #include <sys/mbuf.h>
56 #include <sys/protosw.h>
57 #endif
58
59 #include <netinet/in.h>
60 #include <netinet/in_systm.h>
61 #include <netinet/ip.h>
62 #include <net/vjcompress.h>
63
64 #define PACKETPTR       mblk_t *
65 #include <net/ppp-comp.h>
66
67 MOD_OPEN_DECL(ppp_comp_open);
68 MOD_CLOSE_DECL(ppp_comp_close);
69 static int ppp_comp_rput __P((queue_t *, mblk_t *));
70 static int ppp_comp_rsrv __P((queue_t *));
71 static int ppp_comp_wput __P((queue_t *, mblk_t *));
72 static int ppp_comp_wsrv __P((queue_t *));
73 static void ppp_comp_ccp __P((queue_t *, mblk_t *, int));
74 static int msg_byte __P((mblk_t *, unsigned int));
75
76 /* Extract byte i of message mp. */
77 #define MSG_BYTE(mp, i) ((i) < (mp)->b_wptr - (mp)->b_rptr? (mp)->b_rptr[i]: \
78                          msg_byte((mp), (i)))
79
80 /* Is this LCP packet one we have to transmit using LCP defaults? */
81 #define LCP_USE_DFLT(mp)        (1 <= (code = MSG_BYTE((mp), 4)) && code <= 7)
82
83 #define PPP_COMP_ID 0xbadf
84 static struct module_info minfo = {
85 #ifdef PRIOQ
86     PPP_COMP_ID, "ppp_comp", 0, INFPSZ, 16512, 16384,
87 #else
88     PPP_COMP_ID, "ppp_comp", 0, INFPSZ, 16384, 4096,
89 #endif
90 };
91
92 static struct qinit r_init = {
93     ppp_comp_rput, ppp_comp_rsrv, ppp_comp_open, ppp_comp_close,
94     NULL, &minfo, NULL
95 };
96
97 static struct qinit w_init = {
98     ppp_comp_wput, ppp_comp_wsrv, NULL, NULL, NULL, &minfo, NULL
99 };
100
101 #if defined(SVR4) && !defined(SOL2)
102 int pcmpdevflag = 0;
103 #define ppp_compinfo pcmpinfo
104 #endif
105 struct streamtab ppp_compinfo = {
106     &r_init, &w_init, NULL, NULL
107 };
108
109 int ppp_comp_count;             /* number of module instances in use */
110
111 #ifdef __osf__
112
113 static void ppp_comp_alloc __P((comp_state_t *));
114 typedef struct memreq {
115     unsigned char comp_opts[20];
116     int cmd;
117     int thread_status;
118     char *returned_mem;
119 } memreq_t;
120
121 #endif
122
123 typedef struct comp_state {
124     int         flags;
125     int         mru;
126     int         mtu;
127     int         unit;
128     struct compressor *xcomp;
129     void        *xstate;
130     struct compressor *rcomp;
131     void        *rstate;
132     struct vjcompress vj_comp;
133     int         vj_last_ierrors;
134     struct pppstat stats;
135 #ifdef __osf__
136     memreq_t    memreq;
137     thread_t    thread;
138 #endif
139 } comp_state_t;
140
141
142 #ifdef __osf__
143 extern task_t first_task;
144 #endif
145
146 /* Bits in flags are as defined in pppio.h. */
147 #define CCP_ERR         (CCP_ERROR | CCP_FATALERROR)
148 #define LAST_MOD        0x1000000       /* no ppp modules below us */
149 #define DBGLOG          0x2000000       /* log debugging stuff */
150
151 #define MAX_IPHDR       128     /* max TCP/IP header size */
152 #define MAX_VJHDR       20      /* max VJ compressed header size (?) */
153
154 #undef MIN              /* just in case */
155 #define MIN(a, b)       ((a) < (b)? (a): (b))
156
157 /*
158  * List of compressors we know about.
159  */
160
161 #if DO_BSD_COMPRESS
162 extern struct compressor ppp_bsd_compress;
163 #endif
164 #if DO_DEFLATE
165 extern struct compressor ppp_deflate;
166 #endif
167
168 struct compressor *ppp_compressors[] = {
169 #if DO_BSD_COMPRESS
170     &ppp_bsd_compress,
171 #endif
172 #if DO_DEFLATE
173     &ppp_deflate,
174 #endif
175     NULL
176 };
177
178 /*
179  * STREAMS module entry points.
180  */
181 MOD_OPEN(ppp_comp_open)
182 {
183     comp_state_t *cp;
184 #ifdef __osf__
185     thread_t thread;
186 #endif
187
188     if (q->q_ptr == NULL) {
189         cp = (comp_state_t *) ALLOC_SLEEP(sizeof(comp_state_t));
190         if (cp == NULL)
191             OPEN_ERROR(ENOSR);
192         WR(q)->q_ptr = q->q_ptr = (caddr_t) cp;
193         bzero((caddr_t)cp, sizeof(comp_state_t));
194         cp->mru = PPP_MRU;
195         cp->mtu = PPP_MTU;
196         cp->xstate = NULL;
197         cp->rstate = NULL;
198         vj_compress_init(&cp->vj_comp, -1);
199         ++ppp_comp_count;
200         qprocson(q);
201 #ifdef __osf__
202         if (!(thread = kernel_thread_w_arg(first_task, ppp_comp_alloc, (void *)cp)))
203                 OPEN_ERROR(ENOSR);
204         cp->thread = thread;
205 #endif
206     }
207     return 0;
208 }
209
210 MOD_CLOSE(ppp_comp_close)
211 {
212     comp_state_t *cp;
213
214     qprocsoff(q);
215     cp = (comp_state_t *) q->q_ptr;
216     if (cp != NULL) {
217         if (cp->xstate != NULL)
218             (*cp->xcomp->comp_free)(cp->xstate);
219         if (cp->rstate != NULL)
220             (*cp->rcomp->decomp_free)(cp->rstate);
221 #ifdef __osf__
222         if (!cp->thread)
223             printf("ppp_comp_close: NULL thread!\n");
224         else
225             thread_terminate(cp->thread);
226 #endif
227         FREE(cp, sizeof(comp_state_t));
228         q->q_ptr = NULL;
229         OTHERQ(q)->q_ptr = NULL;
230         --ppp_comp_count;
231     }
232     return 0;
233 }
234
235 #ifdef __osf__
236
237 /* thread for calling back to a compressor's memory allocator
238  * Needed for Digital UNIX since it's VM can't handle requests
239  * for large amounts of memory without blocking.  The thread
240  * provides a context in which we can call a memory allocator
241  * that may block.
242  */
243 static void
244 ppp_comp_alloc(comp_state_t *cp)
245 {
246     int len, cmd;
247     unsigned char *compressor_options;
248     thread_t thread;
249     void *(*comp_allocator)();
250
251
252 #if defined(MAJOR_VERSION) && (MAJOR_VERSION <= 2)
253
254     /* In 2.x and earlier the argument gets passed
255      * in the thread structure itself.  Yuck.
256      */
257     thread = current_thread();
258     cp = thread->reply_port;
259     thread->reply_port = PORT_NULL;
260
261 #endif
262
263     for (;;) {
264         assert_wait((vm_offset_t)&cp->memreq.thread_status, TRUE);
265         thread_block();
266
267         if (thread_should_halt(current_thread()))
268             thread_halt_self();
269         cmd = cp->memreq.cmd;
270         compressor_options = &cp->memreq.comp_opts[0];
271         len = compressor_options[1];
272         if (cmd == PPPIO_XCOMP) {
273             cp->memreq.returned_mem = cp->xcomp->comp_alloc(compressor_options, len);
274             if (!cp->memreq.returned_mem) {
275                 cp->memreq.thread_status = ENOSR;
276             } else {
277                 cp->memreq.thread_status = 0;
278             }
279         } else {
280             cp->memreq.returned_mem = cp->rcomp->decomp_alloc(compressor_options, len);
281             if (!cp->memreq.returned_mem) {
282                 cp->memreq.thread_status = ENOSR;
283             } else {
284                 cp->memreq.thread_status = 0;
285             }
286         }
287     }
288 }
289
290 #endif /* __osf__ */
291
292 /* here's the deal with memory allocation under Digital UNIX.
293  * Some other may also benefit from this...
294  * We can't ask for huge chunks of memory in a context where
295  * the caller can't be put to sleep (like, here.)  The alloc
296  * is likely to fail.  Instead we do this: the first time we
297  * get called, kick off a thread to do the allocation.  Return
298  * immediately to the caller with EAGAIN, as an indication that
299  * they should send down the ioctl again.  By the time the
300  * second call comes in it's likely that the memory allocation
301  * thread will have returned with the requested memory.  We will
302  * continue to return EAGAIN however until the thread has completed.
303  * When it has, we return zero (and the memory) if the allocator
304  * was successful and ENOSR otherwise.
305  *
306  * Callers of the RCOMP and XCOMP ioctls are encouraged (but not
307  * required) to loop for some number of iterations with a small
308  * delay in the loop body (for instance a 1/10-th second "sleep"
309  * via select.)
310  */
311 static int
312 ppp_comp_wput(q, mp)
313     queue_t *q;
314     mblk_t *mp;
315 {
316     struct iocblk *iop;
317     comp_state_t *cp;
318     int error, len, n;
319     int flags, mask;
320     mblk_t *np;
321     struct compressor **comp;
322     struct ppp_stats *psp;
323     struct ppp_comp_stats *csp;
324     unsigned char *opt_data;
325     int nxslots, nrslots;
326
327     cp = (comp_state_t *) q->q_ptr;
328     switch (mp->b_datap->db_type) {
329
330     case M_DATA:
331         putq(q, mp);
332         break;
333
334     case M_IOCTL:
335         iop = (struct iocblk *) mp->b_rptr;
336         error = EINVAL;
337         switch (iop->ioc_cmd) {
338
339         case PPPIO_CFLAGS:
340             /* set/get CCP state */
341             if (iop->ioc_count != 2 * sizeof(int))
342                 break;
343             flags = ((int *) mp->b_cont->b_rptr)[0];
344             mask = ((int *) mp->b_cont->b_rptr)[1];
345             cp->flags = (cp->flags & ~mask) | (flags & mask);
346             if ((mask & CCP_ISOPEN) && (flags & CCP_ISOPEN) == 0) {
347                 if (cp->xstate != NULL) {
348                     (*cp->xcomp->comp_free)(cp->xstate);
349                     cp->xstate = NULL;
350                 }
351                 if (cp->rstate != NULL) {
352                     (*cp->rcomp->decomp_free)(cp->rstate);
353                     cp->rstate = NULL;
354                 }
355                 cp->flags &= ~CCP_ISUP;
356             }
357             error = 0;
358             iop->ioc_count = sizeof(int);
359             ((int *) mp->b_cont->b_rptr)[0] = cp->flags;
360             mp->b_cont->b_wptr = mp->b_cont->b_rptr + sizeof(int);
361             break;
362
363         case PPPIO_VJINIT:
364             /*
365              * Initialize VJ compressor/decompressor
366              */
367             if (iop->ioc_count != 2)
368                 break;
369             nxslots = mp->b_cont->b_rptr[0] + 1;
370             nrslots = mp->b_cont->b_rptr[1] + 1;
371             if (nxslots > MAX_STATES || nrslots > MAX_STATES)
372                 break;
373             vj_compress_init(&cp->vj_comp, nxslots);
374             cp->vj_last_ierrors = cp->stats.ppp_ierrors;
375             error = 0;
376             iop->ioc_count = 0;
377             break;
378
379         case PPPIO_XCOMP:
380         case PPPIO_RCOMP:
381             if (iop->ioc_count <= 0)
382                 break;
383             opt_data = mp->b_cont->b_rptr;
384             len = mp->b_cont->b_wptr - opt_data;
385             if (len > iop->ioc_count)
386                 len = iop->ioc_count;
387             if (opt_data[1] < 2 || opt_data[1] > len)
388                 break;
389             for (comp = ppp_compressors; *comp != NULL; ++comp)
390                 if ((*comp)->compress_proto == opt_data[0]) {
391                     /* here's the handler! */
392                     error = 0;
393 #ifndef __osf__
394                     if (iop->ioc_cmd == PPPIO_XCOMP) {
395                         /* A previous call may have fetched memory for a compressor
396                          * that's now being retired or reset.  Free it using it's
397                          * mechanism for freeing stuff.
398                          */
399                         if (cp->xstate != NULL) {
400                             (*cp->xcomp->comp_free)(cp->xstate);
401                             cp->xstate = NULL;
402                         }
403                         cp->xcomp = *comp;
404                         cp->xstate = (*comp)->comp_alloc(opt_data, len);
405                         if (cp->xstate == NULL)
406                             error = ENOSR;
407                     } else {
408                         if (cp->rstate != NULL) {
409                             (*cp->rcomp->decomp_free)(cp->rstate);
410                             cp->rstate = NULL;
411                         }
412                         cp->rcomp = *comp;
413                         cp->rstate = (*comp)->decomp_alloc(opt_data, len);
414                         if (cp->rstate == NULL)
415                             error = ENOSR;
416                     }
417 #else
418                     if ((error = cp->memreq.thread_status) != EAGAIN)
419                     if (iop->ioc_cmd == PPPIO_XCOMP) {
420                         if (cp->xstate) {
421                             (*cp->xcomp->comp_free)(cp->xstate);
422                             cp->xstate = 0;
423                         }
424                         /* sanity check for compressor options
425                          */
426                         if (sizeof (cp->memreq.comp_opts) < len) {
427                             printf("can't handle options for compressor %d (%d)\n", opt_data[0],
428                                 opt_data[1]);
429                             cp->memreq.thread_status = ENOSR;
430                             cp->memreq.returned_mem = 0;
431                         }
432                         /* fill in request for the thread and kick it off
433                          */
434                         if (cp->memreq.thread_status == 0 && !cp->memreq.returned_mem) {
435                             bcopy(opt_data, cp->memreq.comp_opts, len);
436                             cp->memreq.cmd = PPPIO_XCOMP;
437                             cp->xcomp = *comp;
438                             error = cp->memreq.thread_status = EAGAIN;
439                             thread_wakeup((vm_offset_t)&cp->memreq.thread_status);
440                         } else {
441                             cp->xstate = cp->memreq.returned_mem;
442                             cp->memreq.returned_mem = 0;
443                             cp->memreq.thread_status = 0;
444                         }
445                     } else {
446                         if (cp->rstate) {
447                             (*cp->rcomp->decomp_free)(cp->rstate);
448                             cp->rstate = NULL;
449                         }
450                         if (sizeof (cp->memreq.comp_opts) < len) {
451                             printf("can't handle options for compressor %d (%d)\n", opt_data[0],
452                                 opt_data[1]);
453                             cp->memreq.thread_status = ENOSR;
454                             cp->memreq.returned_mem = 0;
455                         }
456                         if (cp->memreq.thread_status == 0 && !cp->memreq.returned_mem) {
457                             bcopy(opt_data, cp->memreq.comp_opts, len);
458                             cp->memreq.cmd = PPPIO_RCOMP;
459                             cp->rcomp = *comp;
460                             error = cp->memreq.thread_status = EAGAIN;
461                             thread_wakeup((vm_offset_t)&cp->memreq.thread_status);
462                         } else {
463                             cp->rstate = cp->memreq.returned_mem;
464                             cp->memreq.returned_mem = 0;
465                             cp->memreq.thread_status = 0;
466                         }
467                     }
468 #endif
469                     break;
470                 }
471             iop->ioc_count = 0;
472             break;
473
474         case PPPIO_GETSTAT:
475             if ((cp->flags & LAST_MOD) == 0) {
476                 error = -1;     /* let the ppp_ahdl module handle it */
477                 break;
478             }
479             np = allocb(sizeof(struct ppp_stats), BPRI_HI);
480             if (np == 0) {
481                 error = ENOSR;
482                 break;
483             }
484             if (mp->b_cont != 0)
485                 freemsg(mp->b_cont);
486             mp->b_cont = np;
487             psp = (struct ppp_stats *) np->b_wptr;
488             np->b_wptr += sizeof(struct ppp_stats);
489             iop->ioc_count = sizeof(struct ppp_stats);
490             psp->p = cp->stats;
491             psp->vj = cp->vj_comp.stats;
492             error = 0;
493             break;
494
495         case PPPIO_GETCSTAT:
496             np = allocb(sizeof(struct ppp_comp_stats), BPRI_HI);
497             if (np == 0) {
498                 error = ENOSR;
499                 break;
500             }
501             if (mp->b_cont != 0)
502                 freemsg(mp->b_cont);
503             mp->b_cont = np;
504             csp = (struct ppp_comp_stats *) np->b_wptr;
505             np->b_wptr += sizeof(struct ppp_comp_stats);
506             iop->ioc_count = sizeof(struct ppp_comp_stats);
507             bzero((caddr_t)csp, sizeof(struct ppp_comp_stats));
508             if (cp->xstate != 0)
509                 (*cp->xcomp->comp_stat)(cp->xstate, &csp->c);
510             if (cp->rstate != 0)
511                 (*cp->rcomp->decomp_stat)(cp->rstate, &csp->d);
512             error = 0;
513             break;
514
515         case PPPIO_DEBUG:
516             if (iop->ioc_count != sizeof(int))
517                 break;
518             n = *(int *)mp->b_cont->b_rptr;
519             if (n == PPPDBG_LOG + PPPDBG_COMP) {
520                 DPRINT1("ppp_comp%d: debug log enabled\n", cp->unit);
521                 cp->flags |= DBGLOG;
522                 error = 0;
523                 iop->ioc_count = 0;
524             } else {
525                 error = -1;
526             }
527             break;
528
529         case PPPIO_LASTMOD:
530             cp->flags |= LAST_MOD;
531             error = 0;
532             break;
533
534         default:
535             error = -1;
536             break;
537         }
538
539         if (error < 0)
540             putnext(q, mp);
541         else if (error == 0) {
542             mp->b_datap->db_type = M_IOCACK;
543             qreply(q, mp);
544         } else {
545             mp->b_datap->db_type = M_IOCNAK;
546             iop->ioc_error = error;
547             iop->ioc_count = 0;
548             qreply(q, mp);
549         }
550         break;
551
552     case M_CTL:
553         switch (*mp->b_rptr) {
554         case PPPCTL_MTU:
555             cp->mtu = ((unsigned short *)mp->b_rptr)[1];
556             break;
557         case PPPCTL_MRU:
558             cp->mru = ((unsigned short *)mp->b_rptr)[1];
559             break;
560         case PPPCTL_UNIT:
561             cp->unit = mp->b_rptr[1];
562             break;
563         }
564         putnext(q, mp);
565         break;
566
567     default:
568         putnext(q, mp);
569     }
570 }
571
572 static int
573 ppp_comp_wsrv(q)
574     queue_t *q;
575 {
576     mblk_t *mp, *cmp = NULL, *np;
577     comp_state_t *cp;
578     int len, proto, type, hlen, code;
579     struct ip *ip;
580     unsigned char *vjhdr, *dp;
581
582     cp = (comp_state_t *) q->q_ptr;
583     while ((mp = getq(q)) != 0) {
584         /* assert(mp->b_datap->db_type == M_DATA) */
585 #ifdef PRIOQ
586         if (!bcanputnext(q,mp->b_band)) {
587 #else
588         if (!canputnext(q)) {
589 #endif PRIOQ
590             putbq(q, mp);
591             return;
592         }
593
594         /*
595          * First check the packet length and work out what the protocol is.
596          */
597         len = msgdsize(mp);
598         if (len < PPP_HDRLEN) {
599             DPRINT1("ppp_comp_wsrv: bogus short packet (%d)\n", len);
600             freemsg(mp);
601             cp->stats.ppp_oerrors++;
602             putctl1(RD(q)->q_next, M_CTL, PPPCTL_OERROR);
603             continue;
604         }
605         proto = (MSG_BYTE(mp, 2) << 8) + MSG_BYTE(mp, 3);
606
607         /*
608          * Make sure we've got enough data in the first mblk
609          * and that we are its only user.
610          */
611         if (proto == PPP_CCP)
612             hlen = len;
613         else if (proto == PPP_IP)
614             hlen = PPP_HDRLEN + MAX_IPHDR;
615         else
616             hlen = PPP_HDRLEN;
617         if (hlen > len)
618             hlen = len;
619         if (mp->b_wptr < mp->b_rptr + hlen || mp->b_datap->db_ref > 1) {
620             PULLUP(mp, hlen);
621             if (mp == 0) {
622                 DPRINT1("ppp_comp_wsrv: pullup failed (%d)\n", hlen);
623                 cp->stats.ppp_oerrors++;
624                 putctl1(RD(q)->q_next, M_CTL, PPPCTL_OERROR);
625                 continue;
626             }
627         }
628
629         /*
630          * Do VJ compression if requested.
631          */
632         if (proto == PPP_IP && (cp->flags & COMP_VJC)) {
633             ip = (struct ip *) (mp->b_rptr + PPP_HDRLEN);
634             if (ip->ip_p == IPPROTO_TCP) {
635                 type = vj_compress_tcp(ip, len - PPP_HDRLEN, &cp->vj_comp,
636                                        (cp->flags & COMP_VJCCID), &vjhdr);
637                 switch (type) {
638                 case TYPE_UNCOMPRESSED_TCP:
639                     mp->b_rptr[3] = proto = PPP_VJC_UNCOMP;
640                     break;
641                 case TYPE_COMPRESSED_TCP:
642                     dp = vjhdr - PPP_HDRLEN;
643                     dp[1] = mp->b_rptr[1]; /* copy control field */
644                     dp[0] = mp->b_rptr[0]; /* copy address field */
645                     dp[2] = 0;             /* set protocol field */
646                     dp[3] = proto = PPP_VJC_COMP;
647                     mp->b_rptr = dp;
648                     break;
649                 }
650             }
651         }
652
653         /*
654          * Do packet compression if enabled.
655          */
656         if (proto == PPP_CCP)
657             ppp_comp_ccp(q, mp, 0);
658         else if (proto != PPP_LCP && (cp->flags & CCP_COMP_RUN)
659                  && cp->xstate != NULL) {
660             len = msgdsize(mp);
661             (*cp->xcomp->compress)(cp->xstate, &cmp, mp, len,
662                                    (cp->flags & CCP_ISUP? cp->mtu: 0));
663             if (cmp != NULL) {
664 #ifdef PRIOQ
665                 cmp->b_band=mp->b_band;
666 #endif PRIOQ
667                 freemsg(mp);
668                 mp = cmp;
669             }
670         }
671
672         /*
673          * Do address/control and protocol compression if enabled.
674          */
675         if ((cp->flags & COMP_AC)
676             && !(proto == PPP_LCP && LCP_USE_DFLT(mp))) {
677             mp->b_rptr += 2;    /* drop the address & ctrl fields */
678             if (proto < 0x100 && (cp->flags & COMP_PROT))
679                 ++mp->b_rptr;   /* drop the high protocol byte */
680         } else if (proto < 0x100 && (cp->flags & COMP_PROT)) {
681             /* shuffle up the address & ctrl fields */
682             mp->b_rptr[2] = mp->b_rptr[1];
683             mp->b_rptr[1] = mp->b_rptr[0];
684             ++mp->b_rptr;
685         }
686
687         cp->stats.ppp_opackets++;
688         cp->stats.ppp_obytes += msgdsize(mp);
689         putnext(q, mp);
690     }
691 }
692
693 static int
694 ppp_comp_rput(q, mp)
695     queue_t *q;
696     mblk_t *mp;
697 {
698     comp_state_t *cp;
699     struct iocblk *iop;
700     struct ppp_stats *psp;
701
702     cp = (comp_state_t *) q->q_ptr;
703     switch (mp->b_datap->db_type) {
704
705     case M_DATA:
706         putq(q, mp);
707         break;
708
709     case M_IOCACK:
710         iop = (struct iocblk *) mp->b_rptr;
711         switch (iop->ioc_cmd) {
712         case PPPIO_GETSTAT:
713             /*
714              * Catch this on the way back from the ppp_ahdl module
715              * so we can fill in the VJ stats.
716              */
717             if (mp->b_cont == 0 || iop->ioc_count != sizeof(struct ppp_stats))
718                 break;
719             psp = (struct ppp_stats *) mp->b_cont->b_rptr;
720             psp->vj = cp->vj_comp.stats;
721             break;
722         }
723         putnext(q, mp);
724         break;
725
726     case M_CTL:
727         switch (mp->b_rptr[0]) {
728         case PPPCTL_IERROR:
729             ++cp->stats.ppp_ierrors;
730             break;
731         case PPPCTL_OERROR:
732             ++cp->stats.ppp_oerrors;
733             break;
734         }
735         putnext(q, mp);
736         break;
737
738     default:
739         putnext(q, mp);
740     }
741 }
742
743 static int
744 ppp_comp_rsrv(q)
745     queue_t *q;
746 {
747     int proto, rv, i;
748     mblk_t *mp, *dmp = NULL, *np;
749     uchar_t *dp, *iphdr;
750     comp_state_t *cp;
751     int len, hlen, vjlen;
752     u_int iphlen;
753
754     cp = (comp_state_t *) q->q_ptr;
755     while ((mp = getq(q)) != 0) {
756         /* assert(mp->b_datap->db_type == M_DATA) */
757         if (!canputnext(q)) {
758             putbq(q, mp);
759             return;
760         }
761
762         len = msgdsize(mp);
763         cp->stats.ppp_ibytes += len;
764         cp->stats.ppp_ipackets++;
765
766         /*
767          * First work out the protocol and where the PPP header ends.
768          */
769         i = 0;
770         proto = MSG_BYTE(mp, 0);
771         if (proto == PPP_ALLSTATIONS) {
772             i = 2;
773             proto = MSG_BYTE(mp, 2);
774         }
775         if ((proto & 1) == 0) {
776             ++i;
777             proto = (proto << 8) + MSG_BYTE(mp, i);
778         }
779         hlen = i + 1;
780
781         /*
782          * Now reconstruct a complete, contiguous PPP header at the
783          * start of the packet.
784          */
785         if (hlen < ((cp->flags & DECOMP_AC)? 0: 2)
786                    + ((cp->flags & DECOMP_PROT)? 1: 2)) {
787             /* count these? */
788             goto bad;
789         }
790         if (mp->b_rptr + hlen > mp->b_wptr) {
791             adjmsg(mp, hlen);   /* XXX check this call */
792             hlen = 0;
793         }
794         if (hlen != PPP_HDRLEN) {
795             /*
796              * We need to put some bytes on the front of the packet
797              * to make a full-length PPP header.
798              * If we can put them in *mp, we do, otherwise we
799              * tack another mblk on the front.
800              * XXX we really shouldn't need to carry around
801              * the address and control at this stage.
802              */
803             dp = mp->b_rptr + hlen - PPP_HDRLEN;
804             if (dp < mp->b_datap->db_base || mp->b_datap->db_ref > 1) {
805                 np = allocb(PPP_HDRLEN, BPRI_MED);
806                 if (np == 0)
807                     goto bad;
808                 np->b_cont = mp;
809                 mp->b_rptr += hlen;
810                 mp = np;
811                 dp = mp->b_wptr;
812                 mp->b_wptr += PPP_HDRLEN;
813             } else
814                 mp->b_rptr = dp;
815
816             dp[0] = PPP_ALLSTATIONS;
817             dp[1] = PPP_UI;
818             dp[2] = proto >> 8;
819             dp[3] = proto;
820         }
821
822         /*
823          * Now see if we have a compressed packet to decompress,
824          * or a CCP packet to take notice of.
825          */
826         proto = PPP_PROTOCOL(mp->b_rptr);
827         if (proto == PPP_CCP) {
828             len = msgdsize(mp);
829             if (mp->b_wptr < mp->b_rptr + len) {
830                 PULLUP(mp, len);
831                 if (mp == 0)
832                     goto bad;
833             }
834             ppp_comp_ccp(q, mp, 1);
835         } else if (proto == PPP_COMP) {
836             if ((cp->flags & CCP_ISUP)
837                 && (cp->flags & CCP_DECOMP_RUN) && cp->rstate
838                 && (cp->flags & CCP_ERR) == 0) {
839                 rv = (*cp->rcomp->decompress)(cp->rstate, mp, &dmp);
840                 switch (rv) {
841                 case DECOMP_OK:
842                     freemsg(mp);
843                     mp = dmp;
844                     if (mp == NULL) {
845                         /* no error, but no packet returned either. */
846                         continue;
847                     }
848                     break;
849                 case DECOMP_ERROR:
850                     cp->flags |= CCP_ERROR;
851                     ++cp->stats.ppp_ierrors;
852                     putctl1(q->q_next, M_CTL, PPPCTL_IERROR);
853                     break;
854                 case DECOMP_FATALERROR:
855                     cp->flags |= CCP_FATALERROR;
856                     ++cp->stats.ppp_ierrors;
857                     putctl1(q->q_next, M_CTL, PPPCTL_IERROR);
858                     break;
859                 }
860             }
861         } else if (cp->rstate && (cp->flags & CCP_DECOMP_RUN)) {
862             (*cp->rcomp->incomp)(cp->rstate, mp);
863         }
864
865         /*
866          * Now do VJ decompression.
867          */
868         proto = PPP_PROTOCOL(mp->b_rptr);
869         if (proto == PPP_VJC_COMP || proto == PPP_VJC_UNCOMP) {
870             len = msgdsize(mp) - PPP_HDRLEN;
871             if ((cp->flags & DECOMP_VJC) == 0 || len <= 0)
872                 goto bad;
873
874             /*
875              * Advance past the ppp header.
876              * Here we assume that the whole PPP header is in the first mblk.
877              */
878             np = mp;
879             dp = np->b_rptr + PPP_HDRLEN;
880             if (dp >= mp->b_wptr) {
881                 np = np->b_cont;
882                 dp = np->b_rptr;
883             }
884
885             /*
886              * Make sure we have sufficient contiguous data at this point.
887              */
888             hlen = (proto == PPP_VJC_COMP)? MAX_VJHDR: MAX_IPHDR;
889             if (hlen > len)
890                 hlen = len;
891             if (np->b_wptr < dp + hlen || np->b_datap->db_ref > 1) {
892                 PULLUP(mp, hlen + PPP_HDRLEN);
893                 if (mp == 0)
894                     goto bad;
895                 np = mp;
896                 dp = np->b_rptr + PPP_HDRLEN;
897             }
898
899             if (proto == PPP_VJC_COMP) {
900                 /*
901                  * Decompress VJ-compressed packet.
902                  * First reset compressor if an input error has occurred.
903                  */
904                 if (cp->stats.ppp_ierrors != cp->vj_last_ierrors) {
905                     if (cp->flags & DBGLOG)
906                         DPRINT1("ppp%d: resetting VJ\n", cp->unit);
907                     vj_uncompress_err(&cp->vj_comp);
908                     cp->vj_last_ierrors = cp->stats.ppp_ierrors;
909                 }
910
911                 vjlen = vj_uncompress_tcp(dp, np->b_wptr - dp, len,
912                                           &cp->vj_comp, &iphdr, &iphlen);
913                 if (vjlen < 0) {
914                     if (cp->flags & DBGLOG)
915                         DPRINT2("ppp%d: vj_uncomp_tcp failed, pkt len %d\n",
916                                 cp->unit, len);
917                     ++cp->vj_last_ierrors;  /* so we don't reset next time */
918                     goto bad;
919                 }
920
921                 /* drop ppp and vj headers off */
922                 if (mp != np) {
923                     freeb(mp);
924                     mp = np;
925                 }
926                 mp->b_rptr = dp + vjlen;
927
928                 /* allocate a new mblk for the ppp and ip headers */
929                 if ((np = allocb(iphlen + PPP_HDRLEN + 4, BPRI_MED)) == 0)
930                     goto bad;
931                 dp = np->b_rptr;        /* prepend mblk with TCP/IP hdr */
932                 dp[0] = PPP_ALLSTATIONS; /* reconstruct PPP header */
933                 dp[1] = PPP_UI;
934                 dp[2] = PPP_IP >> 8;
935                 dp[3] = PPP_IP;
936                 bcopy((caddr_t)iphdr, (caddr_t)dp + PPP_HDRLEN, iphlen);
937                 np->b_wptr = dp + iphlen + PPP_HDRLEN;
938                 np->b_cont = mp;
939
940                 /* XXX there seems to be a bug which causes panics in strread
941                    if we make an mbuf with only the IP header in it :-( */
942                 if (mp->b_wptr - mp->b_rptr > 4) {
943                     bcopy((caddr_t)mp->b_rptr, (caddr_t)np->b_wptr, 4);
944                     mp->b_rptr += 4;
945                     np->b_wptr += 4;
946                 } else {
947                     bcopy((caddr_t)mp->b_rptr, (caddr_t)np->b_wptr,
948                           mp->b_wptr - mp->b_rptr);
949                     np->b_wptr += mp->b_wptr - mp->b_rptr;
950                     np->b_cont = mp->b_cont;
951                     freeb(mp);
952                 }
953
954                 mp = np;
955
956             } else {
957                 /*
958                  * "Decompress" a VJ-uncompressed packet.
959                  */
960                 cp->vj_last_ierrors = cp->stats.ppp_ierrors;
961                 if (!vj_uncompress_uncomp(dp, hlen, &cp->vj_comp)) {
962                     if (cp->flags & DBGLOG)
963                         DPRINT2("ppp%d: vj_uncomp_uncomp failed, pkt len %d\n",
964                                 cp->unit, len);
965                     ++cp->vj_last_ierrors;  /* don't need to reset next time */
966                     goto bad;
967                 }
968                 mp->b_rptr[3] = PPP_IP; /* fix up the PPP protocol field */
969             }
970         }
971
972         putnext(q, mp);
973         continue;
974
975     bad:
976         if (mp != 0)
977             freemsg(mp);
978         cp->stats.ppp_ierrors++;
979         putctl1(q->q_next, M_CTL, PPPCTL_IERROR);
980     }
981 }
982
983 /*
984  * Handle a CCP packet being sent or received.
985  * Here all the data in the packet is in a single mbuf.
986  */
987 static void
988 ppp_comp_ccp(q, mp, rcvd)
989     queue_t *q;
990     mblk_t *mp;
991     int rcvd;
992 {
993     int len, clen;
994     comp_state_t *cp;
995     unsigned char *dp;
996
997     len = msgdsize(mp);
998     if (len < PPP_HDRLEN + CCP_HDRLEN)
999         return;
1000
1001     cp = (comp_state_t *) q->q_ptr;
1002     dp = mp->b_rptr + PPP_HDRLEN;
1003     len -= PPP_HDRLEN;
1004     clen = CCP_LENGTH(dp);
1005     if (clen > len)
1006         return;
1007
1008     switch (CCP_CODE(dp)) {
1009     case CCP_CONFREQ:
1010     case CCP_TERMREQ:
1011     case CCP_TERMACK:
1012         cp->flags &= ~CCP_ISUP;
1013         break;
1014
1015     case CCP_CONFACK:
1016         if ((cp->flags & (CCP_ISOPEN | CCP_ISUP)) == CCP_ISOPEN
1017             && clen >= CCP_HDRLEN + CCP_OPT_MINLEN
1018             && clen >= CCP_HDRLEN + CCP_OPT_LENGTH(dp + CCP_HDRLEN)) {
1019             if (!rcvd) {
1020                 if (cp->xstate != NULL
1021                     && (*cp->xcomp->comp_init)
1022                         (cp->xstate, dp + CCP_HDRLEN, clen - CCP_HDRLEN,
1023                          cp->unit, 0, ((cp->flags & DBGLOG) != 0)))
1024                     cp->flags |= CCP_COMP_RUN;
1025             } else {
1026                 if (cp->rstate != NULL
1027                     && (*cp->rcomp->decomp_init)
1028                         (cp->rstate, dp + CCP_HDRLEN, clen - CCP_HDRLEN,
1029                          cp->unit, 0, cp->mru, ((cp->flags & DBGLOG) != 0)))
1030                     cp->flags = (cp->flags & ~CCP_ERR) | CCP_DECOMP_RUN;
1031             }
1032         }
1033         break;
1034
1035     case CCP_RESETACK:
1036         if (cp->flags & CCP_ISUP) {
1037             if (!rcvd) {
1038                 if (cp->xstate && (cp->flags & CCP_COMP_RUN))
1039                     (*cp->xcomp->comp_reset)(cp->xstate);
1040             } else {
1041                 if (cp->rstate && (cp->flags & CCP_DECOMP_RUN)) {
1042                     (*cp->rcomp->decomp_reset)(cp->rstate);
1043                     cp->flags &= ~CCP_ERROR;
1044                 }
1045             }
1046         }
1047         break;
1048     }
1049 }
1050
1051 #if 0
1052 dump_msg(mp)
1053     mblk_t *mp;
1054 {
1055     dblk_t *db;
1056
1057     while (mp != 0) {
1058         db = mp->b_datap;
1059         DPRINT2("mp=%x cont=%x ", mp, mp->b_cont);
1060         DPRINT3("rptr=%x wptr=%x datap=%x\n", mp->b_rptr, mp->b_wptr, db);
1061         DPRINT2("  base=%x lim=%x", db->db_base, db->db_lim);
1062         DPRINT2(" ref=%d type=%d\n", db->db_ref, db->db_type);
1063         mp = mp->b_cont;
1064     }
1065 }
1066 #endif
1067
1068 static int
1069 msg_byte(mp, i)
1070     mblk_t *mp;
1071     unsigned int i;
1072 {
1073     while (mp != 0 && i >= mp->b_wptr - mp->b_rptr)
1074         mp = mp->b_cont;
1075     if (mp == 0)
1076         return -1;
1077     return mp->b_rptr[i];
1078 }