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 HAS 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 1996/05/28 00:55:44 paulus Exp $
31 * This file is used under SVR4, Solaris 2, SunOS 4, and OSF/1.
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <sys/errno.h>
37 #include <sys/stream.h>
41 #include <sys/cmn_err.h>
47 #include <net/ppp_defs.h>
48 #include <net/pppio.h>
51 #include <netinet/in.h>
52 #include <netinet/in_systm.h>
53 #include <netinet/ip.h>
54 #include <net/vjcompress.h>
56 #define PACKETPTR mblk_t *
57 #include <net/ppp-comp.h>
59 MOD_OPEN_DECL(ppp_comp_open);
60 MOD_CLOSE_DECL(ppp_comp_close);
61 static int ppp_comp_rput __P((queue_t *, mblk_t *));
62 static int ppp_comp_rsrv __P((queue_t *));
63 static int ppp_comp_wput __P((queue_t *, mblk_t *));
64 static int ppp_comp_wsrv __P((queue_t *));
65 static void ppp_comp_ccp __P((queue_t *, mblk_t *, int));
66 static int msg_byte __P((mblk_t *, unsigned int));
68 /* Extract byte i of message mp. */
69 #define MSG_BYTE(mp, i) ((i) < (mp)->b_wptr - (mp)->b_rptr? (mp)->b_rptr[i]: \
72 /* Is this LCP packet one we have to transmit using LCP defaults? */
73 #define LCP_USE_DFLT(mp) (1 <= (code = MSG_BYTE((mp), 4)) && code <= 7)
75 static struct module_info minfo = {
76 0xbadf, "ppp_comp", 0, INFPSZ, 16384, 4096,
79 static struct qinit r_init = {
80 ppp_comp_rput, ppp_comp_rsrv, ppp_comp_open, ppp_comp_close,
84 static struct qinit w_init = {
85 ppp_comp_wput, ppp_comp_wsrv, NULL, NULL, NULL, &minfo, NULL
88 struct streamtab ppp_compinfo = {
89 &r_init, &w_init, NULL, NULL
92 int ppp_comp_count; /* number of module instances in use */
94 typedef struct comp_state {
99 struct compressor *xcomp;
101 struct compressor *rcomp;
103 struct vjcompress vj_comp;
105 struct pppstat stats;
108 /* Bits in flags are as defined in pppio.h. */
109 #define CCP_ERR (CCP_ERROR | CCP_FATALERROR)
110 #define LAST_MOD 0x1000000 /* no ppp modules below us */
112 #define MAX_IPHDR 128 /* max TCP/IP header size */
113 #define MAX_VJHDR 20 /* max VJ compressed header size (?) */
115 #undef MIN /* just in case */
116 #define MIN(a, b) ((a) < (b)? (a): (b))
119 * List of compressors we know about.
122 extern struct compressor ppp_bsd_compress;
123 extern struct compressor ppp_deflate;
125 struct compressor *ppp_compressors[] = {
136 * STREAMS module entry points.
138 MOD_OPEN(ppp_comp_open)
142 if (q->q_ptr == NULL) {
143 cp = (comp_state_t *) ALLOC_SLEEP(sizeof(comp_state_t));
146 WR(q)->q_ptr = q->q_ptr = (caddr_t) cp;
147 bzero((caddr_t)cp, sizeof(comp_state_t));
152 vj_compress_init(&cp->vj_comp, -1);
159 MOD_CLOSE(ppp_comp_close)
164 cp = (comp_state_t *) q->q_ptr;
166 if (cp->xstate != NULL)
167 (*cp->xcomp->comp_free)(cp->xstate);
168 if (cp->rstate != NULL)
169 (*cp->rcomp->decomp_free)(cp->rstate);
170 FREE(cp, sizeof(comp_state_t));
172 OTHERQ(q)->q_ptr = NULL;
188 struct compressor **comp;
189 struct ppp_stats *psp;
190 struct ppp_comp_stats *csp;
191 unsigned char *opt_data;
192 int nxslots, nrslots;
194 cp = (comp_state_t *) q->q_ptr;
195 switch (mp->b_datap->db_type) {
202 iop = (struct iocblk *) mp->b_rptr;
204 switch (iop->ioc_cmd) {
207 /* set/get CCP state */
208 if (iop->ioc_count != 2 * sizeof(int))
210 flags = ((int *) mp->b_cont->b_rptr)[0];
211 mask = ((int *) mp->b_cont->b_rptr)[1];
212 cp->flags = (cp->flags & ~mask) | (flags & mask);
213 if ((mask & CCP_ISOPEN) && (flags & CCP_ISOPEN) == 0) {
214 if (cp->xstate != NULL) {
215 (*cp->xcomp->comp_free)(cp->xstate);
218 if (cp->rstate != NULL) {
219 (*cp->rcomp->decomp_free)(cp->rstate);
222 cp->flags &= ~CCP_ISUP;
225 iop->ioc_count = sizeof(int);
226 ((int *) mp->b_cont->b_rptr)[0] = cp->flags;
227 mp->b_cont->b_wptr = mp->b_cont->b_rptr + sizeof(int);
232 * Initialize VJ compressor/decompressor
234 if (iop->ioc_count != 2)
236 nxslots = mp->b_cont->b_rptr[0] + 1;
237 nrslots = mp->b_cont->b_rptr[1] + 1;
238 if (nxslots > MAX_STATES || nrslots > MAX_STATES)
240 vj_compress_init(&cp->vj_comp, nxslots);
241 cp->vj_last_ierrors = cp->stats.ppp_ierrors;
248 if (iop->ioc_count <= 0)
250 opt_data = mp->b_cont->b_rptr;
251 len = mp->b_cont->b_wptr - opt_data;
252 if (len > iop->ioc_count)
253 len = iop->ioc_count;
254 if (opt_data[1] < 2 || opt_data[1] > len)
256 for (comp = ppp_compressors; *comp != NULL; ++comp)
257 if ((*comp)->compress_proto == opt_data[0]) {
258 /* here's the handler! */
260 if (iop->ioc_cmd == PPPIO_XCOMP) {
261 if (cp->xstate != NULL)
262 (*cp->xcomp->comp_free)(cp->xstate);
264 cp->xstate = (*comp)->comp_alloc(opt_data, len);
265 if (cp->xstate == NULL)
268 if (cp->rstate != NULL)
269 (*cp->rcomp->decomp_free)(cp->rstate);
271 cp->rstate = (*comp)->decomp_alloc(opt_data, len);
272 if (cp->rstate == NULL)
281 if ((cp->flags & LAST_MOD) == 0) {
282 error = -1; /* let the ppp_ahdl module handle it */
285 np = allocb(sizeof(struct ppp_stats), BPRI_HI);
293 psp = (struct ppp_stats *) np->b_wptr;
294 np->b_wptr += sizeof(struct ppp_stats);
295 iop->ioc_count = sizeof(struct ppp_stats);
297 psp->vj = cp->vj_comp.stats;
302 np = allocb(sizeof(struct ppp_comp_stats), BPRI_HI);
310 csp = (struct ppp_comp_stats *) np->b_wptr;
311 np->b_wptr += sizeof(struct ppp_comp_stats);
312 iop->ioc_count = sizeof(struct ppp_comp_stats);
313 bzero((caddr_t)csp, sizeof(struct ppp_comp_stats));
315 (*cp->xcomp->comp_stat)(cp->xstate, &csp->c);
317 (*cp->rcomp->decomp_stat)(cp->rstate, &csp->d);
322 cp->flags |= LAST_MOD;
333 else if (error == 0) {
334 mp->b_datap->db_type = M_IOCACK;
337 mp->b_datap->db_type = M_IOCNAK;
338 iop->ioc_error = error;
345 switch (*mp->b_rptr) {
347 cp->mtu = ((unsigned short *)mp->b_rptr)[1];
350 cp->mru = ((unsigned short *)mp->b_rptr)[1];
353 cp->unit = mp->b_rptr[1];
368 mblk_t *mp, *cmp = NULL, *np;
370 int len, proto, type, hlen, code;
372 unsigned char *vjhdr, *dp;
374 cp = (comp_state_t *) q->q_ptr;
375 while ((mp = getq(q)) != 0) {
376 /* assert(mp->b_datap->db_type == M_DATA) */
377 if (!canputnext(q)) {
383 * First check the packet length and work out what the protocol is.
386 if (len < PPP_HDRLEN) {
387 DPRINT1("ppp_comp_wsrv: bogus short packet (%d)\n", len);
389 cp->stats.ppp_oerrors++;
390 putctl1(RD(q)->q_next, M_CTL, PPPCTL_OERROR);
393 proto = (MSG_BYTE(mp, 2) << 8) + MSG_BYTE(mp, 3);
396 * Make sure we've got enough data in the first mblk
397 * and that we are its only user.
401 else if (proto = PPP_IP)
402 hlen = PPP_HDRLEN + MAX_IPHDR;
407 if (mp->b_wptr < mp->b_rptr + hlen || mp->b_datap->db_ref > 1) {
410 DPRINT1("ppp_comp_wsrv: pullup failed (%d)\n", hlen);
411 cp->stats.ppp_oerrors++;
412 putctl1(RD(q)->q_next, M_CTL, PPPCTL_OERROR);
416 proto = PPP_PROTOCOL(mp->b_rptr);
419 * Do VJ compression if requested.
421 if (proto == PPP_IP && (cp->flags & COMP_VJC)) {
422 ip = (struct ip *) (mp->b_rptr + PPP_HDRLEN);
423 if (ip->ip_p == IPPROTO_TCP) {
424 type = vj_compress_tcp(ip, len - PPP_HDRLEN, &cp->vj_comp,
425 (cp->flags & COMP_VJCCID), &vjhdr);
427 case TYPE_UNCOMPRESSED_TCP:
428 mp->b_rptr[3] = proto = PPP_VJC_UNCOMP;
430 case TYPE_COMPRESSED_TCP:
431 dp = vjhdr - PPP_HDRLEN;
432 dp[1] = mp->b_rptr[1]; /* copy control field */
433 dp[0] = mp->b_rptr[0]; /* copy address field */
434 dp[2] = 0; /* set protocol field */
435 dp[3] = proto = PPP_VJC_COMP;
443 * Do packet compression if enabled.
445 if (proto == PPP_CCP)
446 ppp_comp_ccp(q, mp, 0);
447 else if (proto != PPP_LCP && (cp->flags & CCP_COMP_RUN)
448 && cp->xstate != NULL) {
450 (*cp->xcomp->compress)(cp->xstate, &cmp, mp, len,
451 (cp->flags & CCP_ISUP? cp->mtu: 0));
459 * Do address/control and protocol compression if enabled.
461 if ((cp->flags & COMP_AC)
462 && !(proto == PPP_LCP && LCP_USE_DFLT(mp))) {
463 mp->b_rptr += 2; /* drop the address & ctrl fields */
464 if (proto < 0x100 && (cp->flags & COMP_PROT))
465 ++mp->b_rptr; /* drop the high protocol byte */
466 } else if (proto < 0x100 && (cp->flags & COMP_PROT)) {
467 /* shuffle up the address & ctrl fields */
468 mp->b_rptr[2] = mp->b_rptr[1];
469 mp->b_rptr[1] = mp->b_rptr[0];
473 cp->stats.ppp_opackets++;
474 cp->stats.ppp_obytes += msgdsize(mp);
486 struct ppp_stats *psp;
488 cp = (comp_state_t *) q->q_ptr;
489 switch (mp->b_datap->db_type) {
496 iop = (struct iocblk *) mp->b_rptr;
497 switch (iop->ioc_cmd) {
500 * Catch this on the way back from the ppp_ahdl module
501 * so we can fill in the VJ stats.
503 if (mp->b_cont == 0 || iop->ioc_count != sizeof(struct ppp_stats))
505 psp = (struct ppp_stats *) mp->b_cont->b_rptr;
506 psp->vj = cp->vj_comp.stats;
513 switch (mp->b_rptr[0]) {
515 ++cp->stats.ppp_ierrors;
518 ++cp->stats.ppp_oerrors;
534 mblk_t *mp, *dmp = NULL, *np;
537 int len, hlen, vjlen;
540 cp = (comp_state_t *) q->q_ptr;
541 while ((mp = getq(q)) != 0) {
542 /* assert(mp->b_datap->db_type == M_DATA) */
543 if (!canputnext(q)) {
549 cp->stats.ppp_ibytes += len;
550 cp->stats.ppp_ipackets++;
553 * First work out the protocol and where the PPP header ends.
556 proto = MSG_BYTE(mp, 0);
557 if (proto == PPP_ALLSTATIONS) {
559 proto = MSG_BYTE(mp, 2);
561 if ((proto & 1) == 0) {
563 proto = (proto << 8) + MSG_BYTE(mp, i);
568 * Now reconstruct a complete, contiguous PPP header at the
569 * start of the packet.
571 if (hlen < ((cp->flags & DECOMP_AC)? 0: 2)
572 + ((cp->flags & DECOMP_PROT)? 1: 2)) {
576 if (mp->b_rptr + hlen > mp->b_wptr) {
577 adjmsg(mp, hlen); /* XXX check this call */
580 if (hlen != PPP_HDRLEN) {
582 * We need to put some bytes on the front of the packet
583 * to make a full-length PPP header.
584 * If we can put them in *mp, we do, otherwise we
585 * tack another mblk on the front.
586 * XXX we really shouldn't need to carry around
587 * the address and control at this stage.
589 dp = mp->b_rptr + hlen - PPP_HDRLEN;
590 if (dp < mp->b_datap->db_base || mp->b_datap->db_ref > 1) {
591 np = allocb(PPP_HDRLEN, BPRI_MED);
598 mp->b_wptr += PPP_HDRLEN;
602 dp[0] = PPP_ALLSTATIONS;
609 * Now see if we have a compressed packet to decompress,
610 * or a CCP packet to take notice of.
612 proto = PPP_PROTOCOL(mp->b_rptr);
613 if (proto == PPP_CCP) {
615 if (mp->b_wptr < mp->b_rptr + len) {
620 ppp_comp_ccp(q, mp, 1);
621 } else if (proto == PPP_COMP) {
622 if ((cp->flags & CCP_ISUP)
623 && (cp->flags & CCP_DECOMP_RUN) && cp->rstate
624 && (cp->flags & CCP_ERR) == 0) {
625 rv = (*cp->rcomp->decompress)(cp->rstate, mp, &dmp);
631 /* no error, but no packet returned either. */
636 cp->flags |= CCP_ERROR;
637 ++cp->stats.ppp_ierrors;
638 putctl1(q->q_next, M_CTL, PPPCTL_IERROR);
640 case DECOMP_FATALERROR:
641 cp->flags |= CCP_FATALERROR;
642 ++cp->stats.ppp_ierrors;
643 putctl1(q->q_next, M_CTL, PPPCTL_IERROR);
647 } else if (cp->rstate && (cp->flags & CCP_DECOMP_RUN)) {
648 (*cp->rcomp->incomp)(cp->rstate, mp);
652 * Now do VJ decompression.
654 proto = PPP_PROTOCOL(mp->b_rptr);
655 if (proto == PPP_VJC_COMP || proto == PPP_VJC_UNCOMP) {
656 len = msgdsize(mp) - PPP_HDRLEN;
657 if ((cp->flags & DECOMP_VJC) == 0 || len <= 0)
661 * Advance past the ppp header.
662 * Here we assume that the whole PPP header is in the first mblk.
665 dp = np->b_rptr + PPP_HDRLEN;
666 if (dp >= mp->b_wptr) {
672 * Make sure we have sufficient contiguous data at this point.
674 hlen = (proto == PPP_VJC_COMP)? MAX_VJHDR: MAX_IPHDR;
677 if (np->b_wptr < dp + hlen || np->b_datap->db_ref > 1) {
678 PULLUP(mp, hlen + PPP_HDRLEN);
682 dp = np->b_rptr + PPP_HDRLEN;
685 if (proto == PPP_VJC_COMP) {
687 * Decompress VJ-compressed packet.
688 * First reset compressor if an input error has occurred.
690 if (cp->stats.ppp_ierrors != cp->vj_last_ierrors) {
691 vj_uncompress_err(&cp->vj_comp);
692 cp->vj_last_ierrors = cp->stats.ppp_ierrors;
695 vjlen = vj_uncompress_tcp(dp, np->b_wptr - dp, len,
696 &cp->vj_comp, &iphdr, &iphlen);
700 /* drop ppp and vj headers off */
705 mp->b_rptr = dp + vjlen;
707 /* allocate a new mblk for the ppp and ip headers */
708 if ((np = allocb(iphlen + PPP_HDRLEN + 4, BPRI_MED)) == 0)
710 dp = np->b_rptr; /* prepend mblk with TCP/IP hdr */
711 dp[0] = PPP_ALLSTATIONS; /* reconstruct PPP header */
715 bcopy((caddr_t)iphdr, (caddr_t)dp + PPP_HDRLEN, iphlen);
716 np->b_wptr = dp + iphlen + PPP_HDRLEN;
719 /* XXX there seems to be a bug which causes panics in strread
720 if we make an mbuf with only the IP header in it :-( */
721 if (mp->b_wptr - mp->b_rptr > 4) {
722 bcopy((caddr_t)mp->b_rptr, (caddr_t)np->b_wptr, 4);
726 bcopy((caddr_t)mp->b_rptr, (caddr_t)np->b_wptr,
727 mp->b_wptr - mp->b_rptr);
728 np->b_wptr += mp->b_wptr - mp->b_rptr;
729 np->b_cont = mp->b_cont;
737 * "Decompress" a VJ-uncompressed packet.
739 if (!vj_uncompress_uncomp(dp, hlen, &cp->vj_comp))
741 mp->b_rptr[3] = PPP_IP; /* fix up the PPP protocol field */
751 cp->stats.ppp_ierrors++;
752 putctl1(q->q_next, M_CTL, PPPCTL_IERROR);
757 * Handle a CCP packet being sent or received.
758 * Here all the data in the packet is in a single mbuf.
761 ppp_comp_ccp(q, mp, rcvd)
771 if (len < PPP_HDRLEN + CCP_HDRLEN)
774 cp = (comp_state_t *) q->q_ptr;
775 dp = mp->b_rptr + PPP_HDRLEN;
777 clen = CCP_LENGTH(dp);
781 switch (CCP_CODE(dp)) {
785 cp->flags &= ~CCP_ISUP;
789 if ((cp->flags & (CCP_ISOPEN | CCP_ISUP)) == CCP_ISOPEN
790 && clen >= CCP_HDRLEN + CCP_OPT_MINLEN
791 && clen >= CCP_HDRLEN + CCP_OPT_LENGTH(dp + CCP_HDRLEN)) {
793 if (cp->xstate != NULL
794 && (*cp->xcomp->comp_init)
795 (cp->xstate, dp + CCP_HDRLEN, clen - CCP_HDRLEN,
797 cp->flags |= CCP_COMP_RUN;
799 if (cp->rstate != NULL
800 && (*cp->rcomp->decomp_init)
801 (cp->rstate, dp + CCP_HDRLEN, clen - CCP_HDRLEN,
802 cp->unit, 0, cp->mru, 0))
803 cp->flags = (cp->flags & ~CCP_ERR) | CCP_DECOMP_RUN;
809 if (cp->flags & CCP_ISUP) {
811 if (cp->xstate && (cp->flags & CCP_COMP_RUN))
812 (*cp->xcomp->comp_reset)(cp->xstate);
814 if (cp->rstate && (cp->flags & CCP_DECOMP_RUN)) {
815 (*cp->rcomp->decomp_reset)(cp->rstate);
816 cp->flags &= ~CCP_ERROR;
832 DPRINT2("mp=%x cont=%x ", mp, mp->b_cont);
833 DPRINT3("rptr=%x wptr=%x datap=%x\n", mp->b_rptr, mp->b_wptr, db);
834 DPRINT2(" base=%x lim=%x", db->db_base, db->db_lim);
835 DPRINT2(" ref=%d type=%d\n", db->db_ref, db->db_type);
846 while (mp != 0 && i >= mp->b_wptr - mp->b_rptr)
850 return mp->b_rptr[i];