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