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