2 * ppp_comp.c - STREAMS module for kernel-level compression and CCP support.
4 * Copyright (c) 1994 The Australian National University.
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
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 HAVE BEEN ADVISED OF THE POSSIBILITY
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,
27 * $Id: ppp_comp.c,v 1.5 1995/10/27 03:56:19 paulus Exp $
31 * This file is used under SVR4 and Solaris 2.
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <sys/errno.h>
37 #include <sys/stream.h>
40 #include <sys/cmn_err.h>
43 #include <sys/modctl.h>
44 #include <sys/sunddi.h>
46 #include <net/ppp_defs.h>
47 #include <net/pppio.h>
48 #include <netinet/in.h>
49 #include <netinet/in_systm.h>
50 #include <netinet/ip.h>
51 #include <net/vjcompress.h>
53 #define PACKETPTR mblk_t *
54 #include <net/ppp-comp.h>
56 static int ppp_comp_open __P((queue_t *, dev_t *, int, int, cred_t *));
57 static int ppp_comp_close __P((queue_t *, int, cred_t *));
58 static int ppp_comp_rput __P((queue_t *, mblk_t *));
59 static int ppp_comp_rsrv __P((queue_t *));
60 static int ppp_comp_wput __P((queue_t *, mblk_t *));
61 static int ppp_comp_wsrv __P((queue_t *));
62 static void ppp_comp_ccp __P((queue_t *, mblk_t *, int));
64 static struct module_info minfo = {
65 0xbadf, "ppp_comp", 0, INFPSZ, 16384, 4096,
68 static struct qinit r_init = {
69 ppp_comp_rput, ppp_comp_rsrv, ppp_comp_open, ppp_comp_close,
73 static struct qinit w_init = {
74 ppp_comp_wput, ppp_comp_wsrv, NULL, NULL, NULL, &minfo, NULL
77 static struct streamtab ppp_compinfo = {
78 &r_init, &w_init, NULL, NULL
81 #if defined(sun) && defined(svr4) /* Solaris 2 */
82 static struct fmodsw fsw = {
85 D_NEW | D_MP | D_MTQPAIR
88 extern struct mod_ops mod_strmodops;
90 static struct modlstrmod modlstrmod = {
92 "PPP compression module",
96 static struct modlinkage modlinkage = {
103 typedef struct comp_state {
108 struct compressor *xcomp;
110 struct compressor *rcomp;
112 struct vjcompress vj_comp;
114 struct pppstat stats;
117 /* Bits in flags are as defined in pppio.h. */
118 #define CCP_ERR (CCP_ERROR | CCP_FATALERROR)
119 #define LAST_MOD 0x1000000 /* no ppp modules below us */
121 #define MAX_IPHDR 128 /* max TCP/IP header size */
122 #define MAX_VJHDR 20 /* max VJ compressed header size (?) */
124 #undef MIN /* just in case */
125 #define MIN(a, b) ((a) < (b)? (a): (b))
128 /* Use msgpullup if we have other multithreading support. */
129 #define PULLUP(mp, len) \
131 mblk_t *np = msgpullup((mp), (len)); \
137 /* Use pullupmsg if we don't have any multithreading support. */
138 #define PULLUP(mp, len) \
140 if (!pullupmsg((mp), (len))) { \
150 * List of compressors we know about.
153 extern struct compressor ppp_bsd_compress;
155 struct compressor *ppp_compressors[] = {
164 * Entry points for modloading.
169 return mod_install(&modlinkage);
175 return mod_remove(&modlinkage);
182 return mod_info(&modlinkage, mip);
188 # define qprocsoff(q)
189 #define canputnext(q) canput((q)->q_next)
193 * STREAMS module entry points.
196 ppp_comp_open(q, devp, flag, sflag, credp)
204 if (q->q_ptr == NULL) {
205 cp = (comp_state_t *) kmem_zalloc(sizeof(comp_state_t), KM_SLEEP);
208 WR(q)->q_ptr = q->q_ptr = cp;
209 bzero((caddr_t)cp, sizeof(comp_state_t));
214 vj_compress_init(&cp->vj_comp, -1);
221 ppp_comp_close(q, flag, credp)
229 cp = (comp_state_t *) q->q_ptr;
231 if (cp->xstate != NULL)
232 (*cp->xcomp->comp_free)(cp->xstate);
233 if (cp->rstate != NULL)
234 (*cp->rcomp->decomp_free)(cp->rstate);
235 kmem_free(cp, sizeof(comp_state_t));
237 OTHERQ(q)->q_ptr = NULL;
252 struct compressor **comp;
253 struct ppp_stats *psp;
254 struct ppp_comp_stats *csp;
255 unsigned char *opt_data;
256 int nxslots, nrslots;
258 cp = (comp_state_t *) q->q_ptr;
259 switch (mp->b_datap->db_type) {
266 iop = (struct iocblk *) mp->b_rptr;
268 switch (iop->ioc_cmd) {
271 /* set/get CCP state */
272 if (iop->ioc_count != 2 * sizeof(int))
274 flags = ((int *) mp->b_cont->b_rptr)[0];
275 mask = ((int *) mp->b_cont->b_rptr)[1];
276 cp->flags = (cp->flags & ~mask) | (flags & mask);
277 if ((mask & CCP_ISOPEN) && (flags & CCP_ISOPEN) == 0) {
278 if (cp->xstate != NULL) {
279 (*cp->xcomp->comp_free)(cp->xstate);
282 if (cp->rstate != NULL) {
283 (*cp->rcomp->decomp_free)(cp->rstate);
286 cp->flags &= ~CCP_ISUP;
289 iop->ioc_count = sizeof(int);
290 ((int *) mp->b_cont->b_rptr)[0] = cp->flags;
291 mp->b_cont->b_wptr = mp->b_cont->b_rptr + sizeof(int);
296 * Initialize VJ compressor/decompressor
298 if (iop->ioc_count != 2)
300 nxslots = mp->b_cont->b_rptr[0] + 1;
301 nrslots = mp->b_cont->b_rptr[1] + 1;
302 if (nxslots > MAX_STATES || nrslots > MAX_STATES)
304 vj_compress_init(&cp->vj_comp, nxslots);
305 cp->vj_last_ierrors = cp->stats.ppp_ierrors;
312 if (iop->ioc_count <= 0)
314 opt_data = mp->b_cont->b_rptr;
315 len = mp->b_cont->b_wptr - opt_data;
316 if (len > iop->ioc_count)
317 len = iop->ioc_count;
318 if (opt_data[1] < 2 || opt_data[1] > len)
320 for (comp = ppp_compressors; *comp != NULL; ++comp)
321 if ((*comp)->compress_proto == opt_data[0]) {
322 /* here's the handler! */
324 if (iop->ioc_cmd == PPPIO_XCOMP) {
325 if (cp->xstate != NULL)
326 (*cp->xcomp->comp_free)(cp->xstate);
328 cp->xstate = (*comp)->comp_alloc(opt_data, len);
329 if (cp->xstate == NULL)
332 if (cp->rstate != NULL)
333 (*cp->rcomp->decomp_free)(cp->rstate);
335 cp->rstate = (*comp)->decomp_alloc(opt_data, len);
336 if (cp->rstate == NULL)
345 if ((cp->flags & LAST_MOD) == 0) {
346 error = -1; /* let the ppp_ahdl module handle it */
349 np = allocb(sizeof(struct ppp_stats), BPRI_HI);
357 psp = (struct ppp_stats *) np->b_wptr;
358 np->b_wptr += sizeof(struct ppp_stats);
359 iop->ioc_count = sizeof(struct ppp_stats);
361 psp->vj = cp->vj_comp.stats;
366 np = allocb(sizeof(struct ppp_comp_stats), BPRI_HI);
374 csp = (struct ppp_comp_stats *) np->b_wptr;
375 np->b_wptr += sizeof(struct ppp_comp_stats);
376 iop->ioc_count = sizeof(struct ppp_comp_stats);
377 bzero((caddr_t)csp, sizeof(struct ppp_comp_stats));
379 (*cp->xcomp->comp_stat)(cp->xstate, &csp->c);
381 (*cp->rcomp->decomp_stat)(cp->rstate, &csp->d);
386 cp->flags |= LAST_MOD;
397 else if (error == 0) {
398 mp->b_datap->db_type = M_IOCACK;
401 mp->b_datap->db_type = M_IOCNAK;
402 iop->ioc_error = error;
409 switch (*mp->b_rptr) {
411 cp->mtu = ((unsigned short *)mp->b_rptr)[1];
414 cp->mru = ((unsigned short *)mp->b_rptr)[1];
417 cp->unit = mp->b_rptr[1];
432 mblk_t *mp, *cmp = NULL, *np;
434 int len, proto, type;
436 unsigned char *vjhdr, *dp;
438 cp = (comp_state_t *) q->q_ptr;
439 while ((mp = getq(q)) != 0) {
440 /* assert(mp->b_datap->db_type == M_DATA) */
441 if (!canputnext(q)) {
447 * Make sure we've got a reasonable amount in the first
448 * mblk and that we are its only user.
449 * Then find out what the protocol is.
452 if (len > PPP_HDRLEN + MAX_IPHDR)
453 len = PPP_HDRLEN + MAX_IPHDR;
454 if (mp->b_wptr < mp->b_rptr + len || mp->b_datap->db_ref > 1) {
458 cmn_err(CE_CONT, "ppp_comp_wsrv: pullup failed\n");
460 cp->stats.ppp_oerrors++;
461 putctl1(RD(q)->q_next, M_CTL, PPPCTL_OERROR);
465 proto = PPP_PROTOCOL(mp->b_rptr);
468 * Do VJ compression if requested.
470 if (proto == PPP_IP && (cp->flags & COMP_VJC)) {
471 ip = (struct ip *) (mp->b_rptr + PPP_HDRLEN);
472 if (ip->ip_p == IPPROTO_TCP) {
473 type = vj_compress_tcp(ip, len - PPP_HDRLEN, &cp->vj_comp,
474 (cp->flags & COMP_VJCCID), &vjhdr);
476 case TYPE_UNCOMPRESSED_TCP:
477 mp->b_rptr[3] = proto = PPP_VJC_UNCOMP;
479 case TYPE_COMPRESSED_TCP:
480 dp = vjhdr - PPP_HDRLEN;
481 dp[1] = mp->b_rptr[1]; /* copy control field */
482 dp[0] = mp->b_rptr[0]; /* copy address field */
483 dp[2] = 0; /* set protocol field */
484 dp[3] = proto = PPP_VJC_COMP;
492 * Do packet compression if enabled.
494 if (proto == PPP_CCP)
495 ppp_comp_ccp(q, mp, 0);
496 else if (proto != PPP_LCP && (cp->flags & CCP_COMP_RUN)
497 && cp->xstate != NULL) {
499 (*cp->xcomp->compress)(cp->xstate, &cmp, mp, len,
500 (cp->flags & CCP_ISUP? cp->mtu: 0));
508 * Do address/control and protocol compression if enabled.
510 if (proto != PPP_LCP && (cp->flags & COMP_AC)) {
511 mp->b_rptr += 2; /* drop the address & ctrl fields */
512 if (proto < 0x100 && (cp->flags & COMP_PROT))
513 ++mp->b_rptr; /* drop the high protocol byte */
514 } else if (proto < 0x100 && (cp->flags & COMP_PROT)) {
515 /* shuffle up the address & ctrl fields */
516 mp->b_rptr[2] = mp->b_rptr[1];
517 mp->b_rptr[1] = mp->b_rptr[0];
521 cp->stats.ppp_opackets++;
522 cp->stats.ppp_obytes += msgdsize(mp);
534 struct ppp_stats *psp;
536 cp = (comp_state_t *) q->q_ptr;
537 switch (mp->b_datap->db_type) {
544 iop = (struct iocblk *) mp->b_rptr;
545 switch (iop->ioc_cmd) {
548 * Catch this on the way back from the ppp_ahdl module
549 * so we can fill in the VJ stats.
551 if (mp->b_cont == 0 || iop->ioc_count != sizeof(struct ppp_stats))
553 psp = (struct ppp_stats *) mp->b_cont->b_rptr;
554 psp->vj = cp->vj_comp.stats;
561 switch (mp->b_rptr[0]) {
563 ++cp->stats.ppp_ierrors;
566 ++cp->stats.ppp_oerrors;
582 mblk_t *mp, *dmp = NULL, *np;
585 int len, hlen, vjlen, iphlen;
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)) {
596 cp->stats.ppp_ibytes += len;
597 cp->stats.ppp_ipackets++;
600 * First do address/control and protocol "decompression".
602 hlen = MIN(len, PPP_HDRLEN);
603 if (mp->b_wptr < mp->b_rptr + hlen) {
609 if (PPP_ADDRESS(dp) == PPP_ALLSTATIONS
610 && PPP_CONTROL(dp) == PPP_UI)
611 dp += 2; /* skip address/control */
612 else if ((cp->flags & DECOMP_AC) == 0) {
618 proto = *dp++ << 8; /* grab high byte of protocol */
619 else if ((cp->flags & DECOMP_PROT) == 0) {
623 proto += *dp++; /* grab low byte of protocol */
625 goto bad; /* short/bogus packet */
627 if (dp != mp->b_rptr) {
629 * We need to put some bytes on the front of the packet
630 * to make a full-length PPP header.
631 * If we can put them in *mp, we do, otherwise we
632 * tack another mblk on the front.
633 * XXX we really shouldn't need to carry around
634 * the address and control at this stage.
636 if (dp < mp->b_datap->db_base || mp->b_datap->db_ref > 1) {
637 np = allocb(PPP_HDRLEN, BPRI_MED);
641 mp->b_rptr = dp + PPP_HDRLEN;
644 mp->b_wptr += PPP_HDRLEN;
648 dp[0] = PPP_ALLSTATIONS;
655 * Now see if we have a compressed packet to decompress,
656 * or a CCP packet to take notice of.
658 proto = PPP_PROTOCOL(mp->b_rptr);
659 if (proto == PPP_CCP)
660 ppp_comp_ccp(q, mp, 1);
661 else if (proto == PPP_COMP) {
662 if ((cp->flags & CCP_ISUP)
663 && (cp->flags & CCP_DECOMP_RUN) && cp->rstate
664 && (cp->flags & CCP_ERR) == 0) {
665 rv = (*cp->rcomp->decompress)(cp->rstate, mp, &dmp);
671 /* no error, but no packet returned either. */
676 cp->flags |= CCP_ERROR;
677 ++cp->stats.ppp_ierrors;
678 putctl1(q->q_next, M_CTL, PPPCTL_IERROR);
680 case DECOMP_FATALERROR:
681 cp->flags |= CCP_FATALERROR;
682 ++cp->stats.ppp_ierrors;
683 putctl1(q->q_next, M_CTL, PPPCTL_IERROR);
687 } else if (cp->rstate && (cp->flags & CCP_DECOMP_RUN)) {
688 (*cp->rcomp->incomp)(cp->rstate, mp);
692 * Now do VJ decompression.
694 proto = PPP_PROTOCOL(mp->b_rptr);
695 if (proto == PPP_VJC_COMP || proto == PPP_VJC_UNCOMP) {
696 len = msgdsize(mp) - PPP_HDRLEN;
697 if ((cp->flags & DECOMP_VJC) == 0 || len <= 0)
701 * Advance past the ppp header.
702 * Here we assume that the whole PPP header is in the first mblk.
705 dp = np->b_rptr + PPP_HDRLEN;
706 if (dp >= mp->b_wptr) {
711 if (proto == PPP_VJC_COMP) {
712 hlen = MIN(len, MAX_VJHDR);
713 if (np->b_wptr < dp + hlen) {
714 PULLUP(mp, hlen + PPP_HDRLEN);
718 dp = np->b_rptr + PPP_HDRLEN;
721 if (cp->stats.ppp_ierrors != cp->vj_last_ierrors) {
722 vj_uncompress_err(&cp->vj_comp);
723 cp->vj_last_ierrors = cp->stats.ppp_ierrors;
726 vjlen = vj_uncompress_tcp(dp, np->b_wptr - dp, len,
727 &cp->vj_comp, &iphdr, &iphlen);
731 /* drop ppp and vj headers off */
736 mp->b_rptr = dp + vjlen;
738 /* allocate a new mblk for the ppp and ip headers */
739 if ((np = allocb(iphlen + PPP_HDRLEN + 4, BPRI_MED)) == 0)
741 dp = np->b_rptr; /* prepend mblk with TCP/IP hdr */
742 dp[0] = PPP_ALLSTATIONS; /* reconstruct PPP header */
746 bcopy(iphdr, dp + PPP_HDRLEN, iphlen);
747 np->b_wptr = dp + iphlen + PPP_HDRLEN;
750 /* XXX there seems to be a bug which causes panics in strread
751 if we make an mbuf with only the IP header in it :-( */
752 if (mp->b_wptr - mp->b_rptr > 4) {
753 bcopy(mp->b_rptr, np->b_wptr, 4);
757 bcopy(mp->b_rptr, np->b_wptr, mp->b_wptr - mp->b_rptr);
758 np->b_wptr += mp->b_wptr - mp->b_rptr;
759 np->b_cont = mp->b_cont;
766 hlen = MIN(len, MAX_IPHDR);
767 if (np->b_wptr < dp + hlen || np->b_datap->db_ref > 1
768 || mp->b_datap->db_ref > 1) {
769 PULLUP(mp, hlen + PPP_HDRLEN);
773 dp = np->b_rptr + PPP_HDRLEN;
776 if (!vj_uncompress_uncomp(dp, &cp->vj_comp))
778 mp->b_rptr[3] = PPP_IP; /* fix up the PPP protocol field */
788 cp->stats.ppp_ierrors++;
789 putctl1(q->q_next, M_CTL, PPPCTL_IERROR);
794 * Handle a CCP packet being sent or received.
797 ppp_comp_ccp(q, mp, rcvd)
808 if (len < PPP_HDRLEN + CCP_HDRLEN)
810 if (mp->b_wptr < mp->b_rptr + len) {
811 /* XXX this isn't right, because it may free mp */
814 cmn_err(CE_CONT, "ppp_comp_ccp: pullup failed\n");
820 cp = (comp_state_t *) q->q_ptr;
821 dp = mp->b_rptr + PPP_HDRLEN;
823 clen = CCP_LENGTH(dp);
827 switch (CCP_CODE(dp)) {
831 cp->flags &= ~CCP_ISUP;
835 if ((cp->flags & (CCP_ISOPEN | CCP_ISUP)) == CCP_ISOPEN
836 && clen >= CCP_HDRLEN + CCP_OPT_MINLEN
837 && clen >= CCP_HDRLEN + CCP_OPT_LENGTH(dp + CCP_HDRLEN)) {
839 if (cp->xstate != NULL
840 && (*cp->xcomp->comp_init)
841 (cp->xstate, dp + CCP_HDRLEN, clen - CCP_HDRLEN,
843 cp->flags |= CCP_COMP_RUN;
845 if (cp->rstate != NULL
846 && (*cp->rcomp->decomp_init)
847 (cp->rstate, dp + CCP_HDRLEN, clen - CCP_HDRLEN,
848 cp->unit, 0, cp->mru, 0))
849 cp->flags = (cp->flags & ~CCP_ERR)
856 if (cp->flags & CCP_ISUP) {
858 if (cp->xstate && (cp->flags & CCP_COMP_RUN))
859 (*cp->xcomp->comp_reset)(cp->xstate);
861 if (cp->rstate && (cp->flags & CCP_DECOMP_RUN)) {
862 (*cp->rcomp->decomp_reset)(cp->rstate);
863 cp->flags &= ~CCP_ERROR;
883 cmn_err(CE_CONT, "mp=%x cont=%x rptr=%x wptr=%x datap=%x\n",
884 mp, mp->b_cont, mp->b_rptr, mp->b_wptr, db);
885 cmn_err(CE_CONT, " base=%x lim=%x ref=%d type=%d struioflag=%d\n",
886 db->db_base, db->db_lim, db->db_ref, db->db_type,