2 * ppp_comp.c - STREAMS module for kernel-level 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.2 1994/12/05 00:55:24 paulus Exp $
30 #include <net/net_globals.h>
31 #include <sys/types.h>
32 #include <sys/param.h>
33 #include <sys/errno.h>
35 #include <sys/stream.h>
36 #include <sys/stropts.h>
37 #include <sys/strconf.h>
38 #include <sys/device.h>
39 #include <sys/syslog.h>
40 #include <sys/socket.h>
42 #include <net/ppp_defs.h>
43 #include <net/ppp_str.h>
45 #define PACKETPTR mblk_t *
46 #include <net/ppp-comp.h>
48 static int ppp_comp_open(), ppp_comp_close();
49 static int ppp_comp_rput(), ppp_comp_wput();
50 static void ppp_comp_ccp();
52 static struct module_info minfo = {
53 0xbadf, "ppp_compress", 0, INFPSZ, 16384, 4096,
56 static struct qinit r_init = {
57 ppp_comp_rput, NULL, ppp_comp_open, ppp_comp_close,
61 static struct qinit w_init = {
62 ppp_comp_wput, NULL, NULL, NULL, NULL, &minfo, NULL
65 struct streamtab ppp_compinfo = {
66 &r_init, &w_init, NULL, NULL
69 struct ppp_comp_state {
73 struct compressor *xcomp;
75 struct compressor *rcomp;
79 /* Bits in ccp_state are as defined in ppp_str.h. */
80 #define CCP_ERR (CCP_ERROR | CCP_FATALERROR)
83 * List of compressors we know about.
86 extern struct compressor ppp_bsd_compress;
88 struct compressor *ppp_compressors[] = {
93 strconf_t pppcompconf = {
94 "pppcomp", &ppp_compinfo, STR_NEW_OPEN, 0, SQLVL_DEFAULT, (void *) 0
97 int pppcomp_load(int cmd, struct uio *uiop)
103 rc = str_install(STR_LOAD_MOD, &pppcompconf);
106 rc = str_install(STR_UNLOAD_MOD, &pppcompconf);
116 ppp_comp_open(q, dev, flag, sflag)
122 struct ppp_comp_state *cp;
124 if (q->q_ptr == NULL) {
125 cp = (struct ppp_comp_state *)
126 xmalloc(sizeof(struct ppp_comp_state), 0, pinned_heap);
130 bzero(cp, sizeof(struct ppp_comp_state));
131 OTHERQ(q)->q_ptr = q->q_ptr = (caddr_t) cp;
145 struct ppp_comp_state *cp;
147 cp = (struct ppp_comp_state *) q->q_ptr;
149 if (cp->xstate != NULL)
150 (*cp->xcomp->comp_free)(cp->xstate);
151 if (cp->rstate != NULL)
152 (*cp->rcomp->decomp_free)(cp->rstate);
153 xmfree(cp, pinned_heap);
155 OTHERQ(q)->q_ptr = NULL;
166 struct ppp_comp_state *cp;
168 int error, len, proto, state;
169 struct ppp_option_data *odp;
170 struct compressor **comp;
172 cp = (struct ppp_comp_state *) q->q_ptr;
173 switch (mp->b_datap->db_type) {
176 /* first find out what the protocol is */
177 if (mp->b_wptr - mp->b_rptr >= PPP_HDRLEN
178 || pullupmsg(mp, PPP_HDRLEN)) {
179 proto = PPP_PROTOCOL(mp->b_rptr);
180 if (proto == PPP_CCP)
181 ppp_comp_ccp(q, mp, 0);
182 else if (proto != PPP_LCP && (cp->ccp_state & CCP_COMP_RUN)
183 && cp->xstate != NULL) {
185 (*cp->xcomp->compress)(cp->xstate, &cmp, mp, len,
186 (cp->ccp_state & CCP_ISUP? len: 0));
187 /* XXX we really want the MTU here, not len */
198 iop = (struct iocblk *) mp->b_rptr;
200 switch ((unsigned int)iop->ioc_cmd) {
204 if ((iop->ioc_count != sizeof(int)) &&
205 (iop->ioc_count != TRANSPARENT)) {
209 state = (*(int *) mp->b_cont->b_rptr) & (CCP_ISUP | CCP_ISOPEN);
210 if ((state & CCP_ISOPEN) == 0) {
211 if (cp->xstate != NULL) {
212 (*cp->xcomp->comp_free)(cp->xstate);
215 if (cp->rstate != NULL) {
216 (*cp->rcomp->decomp_free)(cp->rstate);
221 cp->ccp_state = (cp->ccp_state & ~CCP_ISUP) | state;
224 bsdlog(LOG_INFO, "SIOCSIFCOMP %x, state = %x\n",
225 *(int *) mp->b_cont->b_rptr, cp->ccp_state);
231 if ((mp->b_cont = allocb(sizeof(int), BPRI_MED)) == NULL) {
235 *(int *)mp->b_cont->b_wptr = cp->ccp_state;
236 mp->b_cont->b_wptr += iop->ioc_count = sizeof(int);
241 if (iop->ioc_count != TRANSPARENT)
243 odp = *((struct ppp_option_data **) mp->b_cont->b_rptr);
244 len = sizeof(odp->opt_data);
245 if (len > odp->length)
247 if (odp->opt_data[1] < 2 || odp->opt_data[1] > len)
249 for (comp = ppp_compressors; *comp != NULL; ++comp)
250 if ((*comp)->compress_proto == odp->opt_data[0]) {
251 /* here's the handler! */
254 if (cp->xstate != NULL)
255 (*cp->xcomp->comp_free)(cp->xstate);
257 cp->xstate = (*comp)->comp_alloc(odp->opt_data, len);
258 if (cp->xstate == NULL)
261 if (cp->rstate != NULL)
262 (*cp->rcomp->decomp_free)(cp->rstate);
264 cp->rstate = (*comp)->decomp_alloc(odp->opt_data, len);
265 if (cp->rstate == NULL)
269 bsdlog(LOG_INFO, "SIOCSCOMPRESS %s len=%d\n",
270 odp->transmit? "xmit": "recv", len);
277 /* set our debug flag from this */
278 if ((iop->ioc_count == TRANSPARENT) ||
279 (iop->ioc_count == sizeof(int))) {
280 cp->debug = *(int *) mp->b_cont->b_rptr & 1;
285 /* remember this value */
286 if ((iop->ioc_count == TRANSPARENT) ||
287 (iop->ioc_count == sizeof(int))) {
288 cp->mru = *(int *) mp->b_cont->b_rptr;
296 else if (error == 0) {
297 mp->b_datap->db_type = M_IOCACK;
300 mp->b_datap->db_type = M_IOCNAK;
318 struct ppp_comp_state *cp;
320 cp = (struct ppp_comp_state *) q->q_ptr;
321 switch (mp->b_datap->db_type) {
324 /* possibly a compressed packet to decompress,
325 or a CCP packet to take notice of. */
326 if (mp->b_wptr - mp->b_rptr >= PPP_HDRLEN
327 || pullupmsg(mp, PPP_HDRLEN)) {
328 proto = PPP_PROTOCOL(mp->b_rptr);
329 if (proto == PPP_CCP)
330 ppp_comp_ccp(q, mp, 1);
331 else if (proto == PPP_COMP) {
332 if ((cp->ccp_state & CCP_ISUP)
333 && (cp->ccp_state & CCP_DECOMP_RUN) && cp->rstate
334 && (cp->ccp_state & CCP_ERR) == 0) {
335 rv = (*cp->rcomp->decompress)(cp->rstate, mp, &dmp);
342 /* no error, but no packet returned */
347 cp->ccp_state |= CCP_ERROR;
349 case DECOMP_FATALERROR:
350 cp->ccp_state |= CCP_FATALERROR;
355 } else if (cp->rstate && (cp->ccp_state & CCP_DECOMP_RUN)) {
356 (*cp->rcomp->incomp)(cp->rstate, mp);
369 ppp_comp_ccp(q, mp, rcvd)
375 struct ppp_comp_state *cp;
379 if (len < PPP_HDRLEN + CCP_HDRLEN || !pullupmsg(mp, len))
381 cp = (struct ppp_comp_state *) q->q_ptr;
382 dp = mp->b_rptr + PPP_HDRLEN;
384 clen = CCP_LENGTH(dp);
388 bsdlog(LOG_INFO, "CCP %s: code=%x len=%d\n", rcvd? "rcvd": "sent",
391 switch (CCP_CODE(dp)) {
395 cp->ccp_state &= ~CCP_ISUP;
399 if ((cp->ccp_state & (CCP_ISOPEN | CCP_ISUP)) == CCP_ISOPEN
400 && clen >= CCP_HDRLEN + CCP_OPT_MINLEN
401 && clen >= CCP_HDRLEN + CCP_OPT_LENGTH(dp + CCP_HDRLEN)) {
403 if (cp->xstate != NULL
404 && (*cp->xcomp->comp_init)
405 (cp->xstate, dp + CCP_HDRLEN, clen - CCP_HDRLEN,
406 0, /* XXX: should be unit */
408 cp->ccp_state |= CCP_COMP_RUN;
410 if (cp->rstate != NULL
411 && (*cp->rcomp->decomp_init)
412 (cp->rstate, dp + CCP_HDRLEN, clen - CCP_HDRLEN,
413 0/* unit */, 0, cp->mru, cp->debug))
414 cp->ccp_state = (cp->ccp_state & ~CCP_ERR)
421 if (cp->ccp_state & CCP_ISUP) {
423 if (cp->xstate && (cp->ccp_state & CCP_COMP_RUN))
424 (*cp->xcomp->comp_reset)(cp->xstate);
426 if (cp->rstate && (cp->ccp_state & CCP_DECOMP_RUN)) {
427 (*cp->rcomp->decomp_reset)(cp->rstate);
428 cp->ccp_state &= ~CCP_ERROR;
436 bsdlog(LOG_INFO, "ccp_state = %x\n", cp->ccp_state);