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