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