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