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