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.3 1995/05/29 06:43:50 paulus Exp $
31 * This file is used under 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
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 * List of compressors we know about.
131 extern struct compressor ppp_bsd_compress;
133 struct compressor *ppp_compressors[] = {
142 * Entry points for modloading.
147 return mod_install(&modlinkage);
153 return mod_remove(&modlinkage);
160 return mod_info(&modlinkage, mip);
166 # define qprocsoff(q)
170 * STREAMS module entry points.
173 ppp_comp_open(q, devp, flag, sflag, credp)
181 if (q->q_ptr == NULL) {
182 cp = (comp_state_t *) kmem_zalloc(sizeof(comp_state_t), KM_SLEEP);
185 WR(q)->q_ptr = q->q_ptr = cp;
186 bzero((caddr_t)cp, sizeof(comp_state_t));
191 vj_compress_init(&cp->vj_comp, -1);
198 ppp_comp_close(q, flag, credp)
206 cp = (comp_state_t *) q->q_ptr;
208 if (cp->xstate != NULL)
209 (*cp->xcomp->comp_free)(cp->xstate);
210 if (cp->rstate != NULL)
211 (*cp->rcomp->decomp_free)(cp->rstate);
212 kmem_free(cp, sizeof(comp_state_t));
214 OTHERQ(q)->q_ptr = NULL;
229 struct compressor **comp;
230 struct ppp_stats *psp;
231 struct ppp_comp_stats *csp;
232 unsigned char *opt_data;
233 int nxslots, nrslots;
235 cp = (comp_state_t *) q->q_ptr;
236 switch (mp->b_datap->db_type) {
243 iop = (struct iocblk *) mp->b_rptr;
245 switch (iop->ioc_cmd) {
248 /* set/get CCP state */
249 if (iop->ioc_count != 2 * sizeof(int))
251 flags = ((int *) mp->b_cont->b_rptr)[0];
252 mask = ((int *) mp->b_cont->b_rptr)[1];
253 cp->flags = (cp->flags & ~mask) | (flags & mask);
254 if ((mask & CCP_ISOPEN) && (flags & CCP_ISOPEN) == 0) {
255 if (cp->xstate != NULL) {
256 (*cp->xcomp->comp_free)(cp->xstate);
259 if (cp->rstate != NULL) {
260 (*cp->rcomp->decomp_free)(cp->rstate);
263 cp->flags &= ~CCP_ISUP;
266 iop->ioc_count = sizeof(int);
267 ((int *) mp->b_cont->b_rptr)[0] = cp->flags;
268 mp->b_cont->b_wptr = mp->b_cont->b_rptr + sizeof(int);
273 * Initialize VJ compressor/decompressor
275 if (iop->ioc_count != 2)
277 nxslots = mp->b_cont->b_rptr[0] + 1;
278 nrslots = mp->b_cont->b_rptr[1] + 1;
279 if (nxslots > MAX_STATES || nrslots > MAX_STATES)
281 vj_compress_init(&cp->vj_comp, nxslots);
282 cp->vj_last_ierrors = cp->stats.ppp_ierrors;
289 if (iop->ioc_count <= 0)
291 opt_data = mp->b_cont->b_rptr;
292 len = mp->b_cont->b_wptr - opt_data;
293 if (len > iop->ioc_count)
294 len = iop->ioc_count;
295 if (opt_data[1] < 2 || opt_data[1] > len)
297 for (comp = ppp_compressors; *comp != NULL; ++comp)
298 if ((*comp)->compress_proto == opt_data[0]) {
299 /* here's the handler! */
301 if (iop->ioc_cmd == PPPIO_XCOMP) {
302 if (cp->xstate != NULL)
303 (*cp->xcomp->comp_free)(cp->xstate);
305 cp->xstate = (*comp)->comp_alloc(opt_data, len);
306 if (cp->xstate == NULL)
309 if (cp->rstate != NULL)
310 (*cp->rcomp->decomp_free)(cp->rstate);
312 cp->rstate = (*comp)->decomp_alloc(opt_data, len);
313 if (cp->rstate == NULL)
322 if ((cp->flags & LAST_MOD) == 0) {
323 error = -1; /* let the ppp_ahdl module handle it */
326 np = allocb(sizeof(struct ppp_stats), BPRI_HI);
334 psp = (struct ppp_stats *) np->b_wptr;
335 np->b_wptr += sizeof(struct ppp_stats);
336 iop->ioc_count = sizeof(struct ppp_stats);
338 psp->vj = cp->vj_comp.stats;
343 np = allocb(sizeof(struct ppp_comp_stats), BPRI_HI);
351 csp = (struct ppp_comp_stats *) np->b_wptr;
352 np->b_wptr += sizeof(struct ppp_comp_stats);
353 iop->ioc_count = sizeof(struct ppp_comp_stats);
354 bzero((caddr_t)csp, sizeof(struct ppp_comp_stats));
356 (*cp->xcomp->comp_stat)(cp->xstate, &csp->c);
358 (*cp->rcomp->decomp_stat)(cp->rstate, &csp->d);
363 cp->flags |= LAST_MOD;
374 else if (error == 0) {
375 mp->b_datap->db_type = M_IOCACK;
378 mp->b_datap->db_type = M_IOCNAK;
379 iop->ioc_error = error;
386 switch (*mp->b_rptr) {
388 cp->mtu = ((unsigned short *)mp->b_rptr)[1];
391 cp->mru = ((unsigned short *)mp->b_rptr)[1];
394 cp->unit = mp->b_rptr[1];
409 mblk_t *mp, *cmp, *np;
411 int len, proto, type;
413 unsigned char *vjhdr, *dp;
415 cp = (comp_state_t *) q->q_ptr;
416 while ((mp = getq(q)) != 0) {
417 /* assert(mp->b_datap->db_type == M_DATA) */
418 if (!canputnext(q)) {
424 * Make sure we've got a reasonable amount in the first
425 * mblk and that we are its only user.
426 * Then find out what the protocol is.
429 if (len > PPP_HDRLEN + MAX_IPHDR)
430 len = PPP_HDRLEN + MAX_IPHDR;
431 if (mp->b_wptr < mp->b_rptr + len || mp->b_datap->db_ref > 1) {
432 np = msgpullup(mp, len);
436 cmn_err(CE_CONT, "ppp_comp_wsrv: msgpullup failed\n");
438 cp->stats.ppp_oerrors++;
439 putctl1(RD(q)->q_next, M_CTL, PPPCTL_OERROR);
444 proto = PPP_PROTOCOL(mp->b_rptr);
447 * Do VJ compression if requested.
449 if (proto == PPP_IP && (cp->flags & COMP_VJC)) {
450 ip = (struct ip *) (mp->b_rptr + PPP_HDRLEN);
451 if (ip->ip_p == IPPROTO_TCP) {
452 type = vj_compress_tcp(ip, len - PPP_HDRLEN, &cp->vj_comp,
453 (cp->flags & COMP_VJCCID), &vjhdr);
455 case TYPE_UNCOMPRESSED_TCP:
456 mp->b_rptr[3] = proto = PPP_VJC_UNCOMP;
458 case TYPE_COMPRESSED_TCP:
459 dp = vjhdr - PPP_HDRLEN;
460 dp[1] = mp->b_rptr[1]; /* copy control field */
461 dp[0] = mp->b_rptr[0]; /* copy address field */
462 dp[2] = 0; /* set protocol field */
463 dp[3] = proto = PPP_VJC_COMP;
471 * Do packet compression if enabled.
473 if (proto == PPP_CCP)
474 ppp_comp_ccp(q, mp, 0);
475 else if (proto != PPP_LCP && (cp->flags & CCP_COMP_RUN)
476 && cp->xstate != NULL) {
478 (*cp->xcomp->compress)(cp->xstate, &cmp, mp, len,
479 (cp->flags & CCP_ISUP? cp->mtu: 0));
487 * Do address/control and protocol compression if enabled.
489 if (proto != PPP_LCP && (cp->flags & COMP_AC)) {
490 mp->b_rptr += 2; /* drop the address & ctrl fields */
491 if (proto < 0x100 && (cp->flags & COMP_PROT))
492 ++mp->b_rptr; /* drop the high protocol byte */
493 } else if (proto < 0x100 && (cp->flags & COMP_PROT)) {
494 /* shuffle up the address & ctrl fields */
495 mp->b_rptr[2] = mp->b_rptr[1];
496 mp->b_rptr[1] = mp->b_rptr[0];
500 cp->stats.ppp_opackets++;
501 cp->stats.ppp_obytes += msgdsize(mp);
513 struct ppp_stats *psp;
515 cp = (comp_state_t *) q->q_ptr;
516 switch (mp->b_datap->db_type) {
523 iop = (struct iocblk *) mp->b_rptr;
524 switch (iop->ioc_cmd) {
527 * Catch this on the way back from the ppp_ahdl module
528 * so we can fill in the VJ stats.
530 if (mp->b_cont == 0 || iop->ioc_count != sizeof(struct ppp_stats))
532 psp = (struct ppp_stats *) mp->b_cont->b_rptr;
533 psp->vj = cp->vj_comp.stats;
540 switch (mp->b_rptr[0]) {
542 ++cp->stats.ppp_ierrors;
545 ++cp->stats.ppp_oerrors;
561 mblk_t *mp, *dmp, *np;
564 int len, hlen, vjlen, iphlen;
566 cp = (comp_state_t *) q->q_ptr;
567 while ((mp = getq(q)) != 0) {
568 /* assert(mp->b_datap->db_type == M_DATA) */
569 if (!canputnext(q)) {
575 cp->stats.ppp_ibytes += len;
576 cp->stats.ppp_ipackets++;
579 * First do address/control and protocol "decompression".
581 hlen = MIN(len, PPP_HDRLEN);
582 if (mp->b_wptr < mp->b_rptr + hlen) {
583 np = msgpullup(mp, hlen);
590 if (PPP_ADDRESS(dp) == PPP_ALLSTATIONS
591 && PPP_CONTROL(dp) == PPP_UI)
592 dp += 2; /* skip address/control */
593 else if ((cp->flags & DECOMP_AC) == 0) {
599 proto = *dp++ << 8; /* grab high byte of protocol */
600 else if ((cp->flags & DECOMP_PROT) == 0) {
604 proto += *dp++; /* grab low byte of protocol */
606 goto bad; /* short/bogus packet */
608 if (dp != mp->b_rptr) {
610 * We need to put some bytes on the front of the packet
611 * to make a full-length PPP header.
612 * If we can put them in *mp, we do, otherwise we
613 * tack another mblk on the front.
614 * XXX we really shouldn't need to carry around
615 * the address and control at this stage.
617 if (dp < mp->b_datap->db_base || mp->b_datap->db_ref > 1) {
618 np = allocb(PPP_HDRLEN, BPRI_MED);
622 mp->b_rptr = dp + PPP_HDRLEN;
625 mp->b_wptr += PPP_HDRLEN;
629 dp[0] = PPP_ALLSTATIONS;
636 * Now see if we have a compressed packet to decompress,
637 * or a CCP packet to take notice of.
639 proto = PPP_PROTOCOL(mp->b_rptr);
640 if (proto == PPP_CCP)
641 ppp_comp_ccp(q, mp, 1);
642 else if (proto == PPP_COMP) {
643 if ((cp->flags & CCP_ISUP)
644 && (cp->flags & CCP_DECOMP_RUN) && cp->rstate
645 && (cp->flags & CCP_ERR) == 0) {
646 rv = (*cp->rcomp->decompress)(cp->rstate, mp, &dmp);
653 /* no error, but no packet returned */
657 cp->flags |= CCP_ERROR;
658 ++cp->stats.ppp_ierrors;
659 putctl1(q->q_next, M_CTL, PPPCTL_IERROR);
661 case DECOMP_FATALERROR:
662 cp->flags |= CCP_FATALERROR;
663 ++cp->stats.ppp_ierrors;
664 putctl1(q->q_next, M_CTL, PPPCTL_IERROR);
669 } else if (cp->rstate && (cp->flags & CCP_DECOMP_RUN)) {
670 (*cp->rcomp->incomp)(cp->rstate, mp);
674 * Now do VJ decompression.
676 proto = PPP_PROTOCOL(mp->b_rptr);
677 if (proto == PPP_VJC_COMP || proto == PPP_VJC_UNCOMP) {
678 len = msgdsize(mp) - PPP_HDRLEN;
679 if ((cp->flags & DECOMP_VJC) == 0 || len <= 0)
683 * Advance past the ppp header.
684 * Here we assume that the whole PPP header is in the first mblk.
687 dp = np->b_rptr + PPP_HDRLEN;
688 if (dp >= mp->b_wptr) {
693 if (proto == PPP_VJC_COMP) {
694 hlen = MIN(len, MAX_VJHDR);
695 if (np->b_wptr < dp + hlen) {
696 np = msgpullup(mp, hlen + PPP_HDRLEN);
701 dp = np->b_rptr + PPP_HDRLEN;
704 if (cp->stats.ppp_ierrors != cp->vj_last_ierrors) {
705 vj_uncompress_err(&cp->vj_comp);
706 cp->vj_last_ierrors = cp->stats.ppp_ierrors;
709 vjlen = vj_uncompress_tcp(dp, np->b_wptr - dp, len,
710 &cp->vj_comp, &iphdr, &iphlen);
714 /* drop ppp and vj headers off */
719 mp->b_rptr = dp + vjlen;
721 /* allocate a new mblk for the ppp and ip headers */
722 if ((np = allocb(iphlen + PPP_HDRLEN + 4, BPRI_MED)) == 0)
724 dp = np->b_rptr; /* prepend mblk with TCP/IP hdr */
725 dp[0] = PPP_ALLSTATIONS; /* reconstruct PPP header */
729 bcopy(iphdr, dp + PPP_HDRLEN, iphlen);
730 np->b_wptr = dp + iphlen + PPP_HDRLEN;
733 /* XXX there seems to be a bug which causes panics in strread
734 if we make an mbuf with only the IP header in it :-( */
735 if (mp->b_wptr - mp->b_rptr > 4) {
736 bcopy(mp->b_rptr, np->b_wptr, 4);
740 bcopy(mp->b_rptr, np->b_wptr, mp->b_wptr - mp->b_rptr);
741 np->b_wptr += mp->b_wptr - mp->b_rptr;
742 np->b_cont = mp->b_cont;
749 hlen = MIN(len, MAX_IPHDR);
750 if (np->b_wptr < dp + hlen || np->b_datap->db_ref > 1
751 || mp->b_datap->db_ref > 1) {
752 np = msgpullup(mp, hlen + PPP_HDRLEN);
757 dp = np->b_rptr + PPP_HDRLEN;
760 if (!vj_uncompress_uncomp(dp, &cp->vj_comp))
762 mp->b_rptr[3] = PPP_IP; /* fix up the PPP protocol field */
772 cp->stats.ppp_ierrors++;
773 putctl1(q->q_next, M_CTL, PPPCTL_IERROR);
778 * Handle a CCP packet being sent or received.
781 ppp_comp_ccp(q, mp, rcvd)
792 if (len < PPP_HDRLEN + CCP_HDRLEN)
794 if (mp->b_wptr < mp->b_rptr + len) {
795 np = msgpullup(mp, len);
797 cmn_err(CE_CONT, "ppp_comp_ccp: msgpullup failed\n");
803 cp = (comp_state_t *) q->q_ptr;
804 dp = mp->b_rptr + PPP_HDRLEN;
806 clen = CCP_LENGTH(dp);
810 switch (CCP_CODE(dp)) {
814 cp->flags &= ~CCP_ISUP;
818 if ((cp->flags & (CCP_ISOPEN | CCP_ISUP)) == CCP_ISOPEN
819 && clen >= CCP_HDRLEN + CCP_OPT_MINLEN
820 && clen >= CCP_HDRLEN + CCP_OPT_LENGTH(dp + CCP_HDRLEN)) {
822 if (cp->xstate != NULL
823 && (*cp->xcomp->comp_init)
824 (cp->xstate, dp + CCP_HDRLEN, clen - CCP_HDRLEN,
826 cp->flags |= CCP_COMP_RUN;
828 if (cp->rstate != NULL
829 && (*cp->rcomp->decomp_init)
830 (cp->rstate, dp + CCP_HDRLEN, clen - CCP_HDRLEN,
831 cp->unit, 0, cp->mru, 0))
832 cp->flags = (cp->flags & ~CCP_ERR)
839 if (cp->flags & CCP_ISUP) {
841 if (cp->xstate && (cp->flags & CCP_COMP_RUN))
842 (*cp->xcomp->comp_reset)(cp->xstate);
844 if (cp->rstate && (cp->flags & CCP_DECOMP_RUN)) {
845 (*cp->rcomp->decomp_reset)(cp->rstate);
846 cp->flags &= ~CCP_ERROR;
866 cmn_err(CE_CONT, "mp=%x cont=%x rptr=%x wptr=%x datap=%x\n",
867 mp, mp->b_cont, mp->b_rptr, mp->b_wptr, db);
868 cmn_err(CE_CONT, " base=%x lim=%x ref=%d type=%d struioflag=%d\n",
869 db->db_base, db->db_lim, db->db_ref, db->db_type,