]> git.ozlabs.org Git - ppp.git/blob - aix4/ppp_comp.c
fix some compile errors, add PPPIOC[GS]MTU
[ppp.git] / aix4 / ppp_comp.c
1 /*
2  * ppp_comp.c - STREAMS module for kernel-level 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 HAVE 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.2 1994/12/05 00:55:24 paulus Exp $
28  */
29
30 #include <net/net_globals.h>
31 #include <sys/types.h>
32 #include <sys/param.h>
33 #include <sys/errno.h>
34 #include <sys/user.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>
41 #include <net/if.h>
42 #include <net/ppp_defs.h>
43 #include <net/ppp_str.h>
44
45 #define PACKETPTR       mblk_t *
46 #include <net/ppp-comp.h>
47
48 static int ppp_comp_open(), ppp_comp_close();
49 static int ppp_comp_rput(), ppp_comp_wput();
50 static void ppp_comp_ccp();
51
52 static struct module_info minfo = {
53     0xbadf, "ppp_compress", 0, INFPSZ, 16384, 4096,
54 };
55
56 static struct qinit r_init = {
57     ppp_comp_rput, NULL, ppp_comp_open, ppp_comp_close,
58     NULL, &minfo, NULL
59 };
60
61 static struct qinit w_init = {
62     ppp_comp_wput, NULL, NULL, NULL, NULL, &minfo, NULL
63 };
64
65 struct streamtab ppp_compinfo = {
66     &r_init, &w_init, NULL, NULL
67 };
68
69 struct ppp_comp_state {
70     int         ccp_state;
71     int         debug;
72     int         mru;
73     struct compressor *xcomp;
74     void        *xstate;
75     struct compressor *rcomp;
76     void        *rstate;
77 };
78
79 /* Bits in ccp_state are as defined in ppp_str.h. */
80 #define CCP_ERR         (CCP_ERROR | CCP_FATALERROR)
81
82 /*
83  * List of compressors we know about.
84  */
85
86 extern struct compressor ppp_bsd_compress;
87
88 struct compressor *ppp_compressors[] = {
89     &ppp_bsd_compress,
90     NULL
91 };
92
93 strconf_t pppcompconf = {
94     "pppcomp", &ppp_compinfo, STR_NEW_OPEN, 0, SQLVL_DEFAULT, (void *) 0
95 };
96
97 int pppcomp_load(int cmd, struct uio *uiop)
98 {
99     int rc = 0;
100
101     switch (cmd) {
102         case CFG_INIT:
103             rc = str_install(STR_LOAD_MOD, &pppcompconf);
104             break;
105         case CFG_TERM:
106             rc = str_install(STR_UNLOAD_MOD, &pppcompconf);
107             break;
108         default:
109             rc = EINVAL;
110             break;
111     }
112     return(rc);
113 }
114
115 static int
116 ppp_comp_open(q, dev, flag, sflag)
117     queue_t *q;
118     dev_t dev;
119     int flag;
120     int sflag;
121 {
122     struct ppp_comp_state *cp;
123
124     if (q->q_ptr == NULL) {
125         cp = (struct ppp_comp_state *)
126             xmalloc(sizeof(struct ppp_comp_state), 0, pinned_heap);
127         if (cp == NULL) {
128             return(ENOSR);
129         }
130         bzero(cp, sizeof(struct ppp_comp_state));
131         OTHERQ(q)->q_ptr = q->q_ptr = (caddr_t) cp;
132         cp->ccp_state = 0;
133         cp->debug = 0;
134         cp->mru = PPP_MRU;
135         cp->xstate = NULL;
136         cp->rstate = NULL;
137     }
138     return 0;
139 }
140
141 static int
142 ppp_comp_close(q)
143     queue_t *q;
144 {
145     struct ppp_comp_state *cp;
146
147     cp = (struct ppp_comp_state *) q->q_ptr;
148     if (cp != NULL) {
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);
154         q->q_ptr = NULL;
155         OTHERQ(q)->q_ptr = NULL;
156     }
157     return 0;
158 }
159
160 static int
161 ppp_comp_wput(q, mp)
162     queue_t *q;
163     mblk_t *mp;
164 {
165     struct iocblk *iop;
166     struct ppp_comp_state *cp;
167     mblk_t *cmp;
168     int error, len, proto, state;
169     struct ppp_option_data *odp;
170     struct compressor **comp;
171
172     cp = (struct ppp_comp_state *) q->q_ptr;
173     switch (mp->b_datap->db_type) {
174
175     case M_DATA:
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) {
184                 len = msgdsize(mp);
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 */
188                 if (cmp != NULL) {
189                     freemsg(mp);
190                     mp = cmp;
191                 }
192             }
193         }
194         putnext(q, mp);
195         break;
196
197     case M_IOCTL:
198         iop = (struct iocblk *) mp->b_rptr;
199         error = -1;
200         switch ((unsigned int)iop->ioc_cmd) {
201
202         case SIOCSIFCOMP:
203             /* set CCP state */
204             if ((iop->ioc_count != sizeof(int)) &&
205                 (iop->ioc_count != TRANSPARENT)) {
206                 error = EINVAL;
207                 break;
208             }
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);
213                     cp->xstate = NULL;
214                 }
215                 if (cp->rstate != NULL) {
216                     (*cp->rcomp->decomp_free)(cp->rstate);
217                     cp->rstate = NULL;
218                 }
219                 cp->ccp_state = 0;
220             } else {
221                 cp->ccp_state = (cp->ccp_state & ~CCP_ISUP) | state;
222             }
223             if (cp->debug)
224                 bsdlog(LOG_INFO, "SIOCSIFCOMP %x, state = %x\n",
225                     *(int *) mp->b_cont->b_rptr, cp->ccp_state);
226             error = 0;
227             iop->ioc_count = 0;
228             break;
229
230         case SIOCGIFCOMP:
231             if ((mp->b_cont = allocb(sizeof(int), BPRI_MED)) == NULL) {
232                 error = ENOSR;
233                 break;
234             }
235             *(int *)mp->b_cont->b_wptr = cp->ccp_state;
236             mp->b_cont->b_wptr += iop->ioc_count = sizeof(int);
237             break;
238
239         case SIOCSCOMPRESS:
240             error = EINVAL;
241             if (iop->ioc_count != TRANSPARENT)
242                 break;
243             odp = *((struct ppp_option_data **) mp->b_cont->b_rptr);
244             len = sizeof(odp->opt_data);
245             if (len > odp->length)
246                 len = odp->length;
247             if (odp->opt_data[1] < 2 || odp->opt_data[1] > len)
248                 break;
249             for (comp = ppp_compressors; *comp != NULL; ++comp)
250                 if ((*comp)->compress_proto == odp->opt_data[0]) {
251                     /* here's the handler! */
252                     error = 0;
253                     if (odp->transmit) {
254                         if (cp->xstate != NULL)
255                             (*cp->xcomp->comp_free)(cp->xstate);
256                         cp->xcomp = *comp;
257                         cp->xstate = (*comp)->comp_alloc(odp->opt_data, len);
258                         if (cp->xstate == NULL)
259                             error = ENOSR;
260                     } else {
261                         if (cp->rstate != NULL)
262                             (*cp->rcomp->decomp_free)(cp->rstate);
263                         cp->rcomp = *comp;
264                         cp->rstate = (*comp)->decomp_alloc(odp->opt_data, len);
265                         if (cp->rstate == NULL)
266                             error = ENOSR;
267                     }
268                     if (cp->debug)
269                         bsdlog(LOG_INFO, "SIOCSCOMPRESS %s len=%d\n",
270                             odp->transmit? "xmit": "recv", len);
271                     break;
272                 }
273             iop->ioc_count = 0;
274             break;
275
276         case SIOCSIFDEBUG:
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;
281             }
282             break;
283
284         case SIOCSIFMRU:
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;
289             }
290             break;
291
292         }
293
294         if (error < 0)
295             putnext(q, mp);
296         else if (error == 0) {
297             mp->b_datap->db_type = M_IOCACK;
298             qreply(q, mp);
299         } else {
300             mp->b_datap->db_type = M_IOCNAK;
301             iop->ioc_count = 0;
302             qreply(q, mp);
303         }
304         break;
305
306     default:
307         putnext(q, mp);
308     }
309 }
310
311 static int
312 ppp_comp_rput(q, mp)
313     queue_t *q;
314     mblk_t *mp;
315 {
316     int proto, rv;
317     mblk_t *dmp;
318     struct ppp_comp_state *cp;
319
320     cp = (struct ppp_comp_state *) q->q_ptr;
321     switch (mp->b_datap->db_type) {
322
323     case M_DATA:
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);
336                     if (dmp != NULL) {
337                         freemsg(mp);
338                         mp = dmp;
339                     } else {
340                         switch (rv) {
341                         case DECOMP_OK:
342                             /* no error, but no packet returned */
343                             freemsg(mp);
344                             mp = NULL;
345                             break;
346                         case DECOMP_ERROR:
347                             cp->ccp_state |= CCP_ERROR;
348                             break;
349                         case DECOMP_FATALERROR:
350                             cp->ccp_state |= CCP_FATALERROR;
351                             break;
352                         }
353                     }
354                 }
355             } else if (cp->rstate && (cp->ccp_state & CCP_DECOMP_RUN)) {
356                 (*cp->rcomp->incomp)(cp->rstate, mp);
357             }
358         }
359         if (mp != NULL)
360             putnext(q, mp);
361         break;
362
363     default:
364         putnext(q, mp);
365     }
366 }
367
368 static void
369 ppp_comp_ccp(q, mp, rcvd)
370     queue_t *q;
371     mblk_t *mp;
372     int rcvd;
373 {
374     int len, clen;
375     struct ppp_comp_state *cp;
376     unsigned char *dp;
377
378     len = msgdsize(mp);
379     if (len < PPP_HDRLEN + CCP_HDRLEN || !pullupmsg(mp, len))
380         return;
381     cp = (struct ppp_comp_state *) q->q_ptr;
382     dp = mp->b_rptr + PPP_HDRLEN;
383     len -= PPP_HDRLEN;
384     clen = CCP_LENGTH(dp);
385     if (clen > len)
386         return;
387     if (cp->debug)
388         bsdlog(LOG_INFO, "CCP %s: code=%x len=%d\n", rcvd? "rcvd": "sent",
389             CCP_CODE(dp), clen);
390
391     switch (CCP_CODE(dp)) {
392     case CCP_CONFREQ:
393     case CCP_TERMREQ:
394     case CCP_TERMACK:
395         cp->ccp_state &= ~CCP_ISUP;
396         break;
397
398     case CCP_CONFACK:
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)) {
402             if (!rcvd) {
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 */
407                          cp->debug))
408                     cp->ccp_state |= CCP_COMP_RUN;
409             } else {
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)
415                         | CCP_DECOMP_RUN;
416             }
417         }
418         break;
419
420     case CCP_RESETACK:
421         if (cp->ccp_state & CCP_ISUP) {
422             if (!rcvd) {
423                 if (cp->xstate && (cp->ccp_state & CCP_COMP_RUN))
424                     (*cp->xcomp->comp_reset)(cp->xstate);
425             } else {
426                 if (cp->rstate && (cp->ccp_state & CCP_DECOMP_RUN)) {
427                     (*cp->rcomp->decomp_reset)(cp->rstate);
428                     cp->ccp_state &= ~CCP_ERROR;
429                 }
430             }
431         }
432         break;
433     }
434
435     if (cp->debug)
436         bsdlog(LOG_INFO, "ccp_state = %x\n", cp->ccp_state);
437 }