]> git.ozlabs.org Git - ppp.git/blob - aix4/ppp_comp.c
add line to /etc/minor_perm; compile with -O
[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.5 1995/05/01 01:42:38 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 #if DO_BSD_COMPRESS
90     &ppp_bsd_compress,
91 #endif
92     NULL
93 };
94
95 strconf_t pppcompconf = {
96     "pppcomp", &ppp_compinfo, STR_NEW_OPEN, 0, SQLVL_DEFAULT, (void *) 0
97 };
98
99 int pppcomp_load(int cmd, struct uio *uiop)
100 {
101     int rc = 0;
102
103     switch (cmd) {
104         case CFG_INIT:
105             rc = str_install(STR_LOAD_MOD, &pppcompconf);
106             break;
107         case CFG_TERM:
108             rc = str_install(STR_UNLOAD_MOD, &pppcompconf);
109             break;
110         default:
111             rc = EINVAL;
112             break;
113     }
114     return(rc);
115 }
116
117 static int
118 ppp_comp_open(q, dev, flag, sflag)
119     queue_t *q;
120     dev_t dev;
121     int flag;
122     int sflag;
123 {
124     struct ppp_comp_state *cp;
125
126     if (q->q_ptr == NULL) {
127         cp = (struct ppp_comp_state *)
128             xmalloc(sizeof(struct ppp_comp_state), 0, pinned_heap);
129         if (cp == NULL) {
130             return(ENOSR);
131         }
132         bzero(cp, sizeof(struct ppp_comp_state));
133         OTHERQ(q)->q_ptr = q->q_ptr = (caddr_t) cp;
134         cp->ccp_state = 0;
135         cp->debug = 0;
136         cp->mru = PPP_MRU;
137         cp->xstate = NULL;
138         cp->rstate = NULL;
139     }
140     return 0;
141 }
142
143 static int
144 ppp_comp_close(q)
145     queue_t *q;
146 {
147     struct ppp_comp_state *cp;
148
149     cp = (struct ppp_comp_state *) q->q_ptr;
150     if (cp != NULL) {
151         if (cp->xstate != NULL)
152             (*cp->xcomp->comp_free)(cp->xstate);
153         if (cp->rstate != NULL)
154             (*cp->rcomp->decomp_free)(cp->rstate);
155         xmfree(cp, pinned_heap);
156         q->q_ptr = NULL;
157         OTHERQ(q)->q_ptr = NULL;
158     }
159     return 0;
160 }
161
162 static int
163 ppp_comp_wput(q, mp)
164     queue_t *q;
165     mblk_t *mp;
166 {
167     struct iocblk *iop;
168     struct ppp_comp_state *cp;
169     mblk_t *cmp;
170     int error, len, proto, state;
171     struct ppp_option_data *odp;
172     struct compressor **comp;
173     struct ppp_comp_stats *pcp;
174
175     cp = (struct ppp_comp_state *) q->q_ptr;
176     switch (mp->b_datap->db_type) {
177
178     case M_CTL:
179         switch (*(u_char *) mp->b_rptr) {
180         case IF_GET_CSTATS:
181             freemsg(mp);
182             mp = allocb(sizeof(struct ppp_comp_stats) + sizeof(u_long),
183                         BPRI_HI);
184             if (mp != NULL) {
185                 *(u_char *) mp->b_wptr = IF_CSTATS;
186                 mp->b_wptr += sizeof(u_long); /* should be enough alignment */
187                 pcp = (struct ppp_comp_stats *) mp->b_wptr;
188                 mp->b_wptr += sizeof(struct ppp_comp_stats);
189                 bzero(pcp, sizeof(struct ppp_comp_stats));
190                 if (cp->xstate != NULL)
191                     (*cp->xcomp->comp_stat)(cp->xstate, &pcp->c);
192                 if (cp->rstate != NULL)
193                     (*cp->rcomp->decomp_stat)(cp->rstate, &pcp->d);
194                 qreply(q, mp);
195             }
196             break;
197         default:
198             putnext(q, mp);
199         }
200         break;
201
202     case M_DATA:
203         /* first find out what the protocol is */
204         if (mp->b_wptr - mp->b_rptr >= PPP_HDRLEN
205             || pullupmsg(mp, PPP_HDRLEN)) {
206             proto = PPP_PROTOCOL(mp->b_rptr);
207             if (proto == PPP_CCP)
208                 ppp_comp_ccp(q, mp, 0);
209             else if (proto != PPP_LCP && (cp->ccp_state & CCP_COMP_RUN)
210                      && cp->xstate != NULL) {
211                 len = msgdsize(mp);
212                 (*cp->xcomp->compress)(cp->xstate, &cmp, mp, len,
213                                        (cp->ccp_state & CCP_ISUP? len: 0));
214                 /* XXX we really want the MTU here, not len */
215                 if (cmp != NULL) {
216                     freemsg(mp);
217                     mp = cmp;
218                 }
219             }
220         }
221         putnext(q, mp);
222         break;
223
224     case M_IOCTL:
225         iop = (struct iocblk *) mp->b_rptr;
226         error = -1;
227         switch ((unsigned int)iop->ioc_cmd) {
228
229         case SIOCSIFCOMP:
230             /* set CCP state */
231             if ((iop->ioc_count != sizeof(int)) &&
232                 (iop->ioc_count != TRANSPARENT)) {
233                 error = EINVAL;
234                 break;
235             }
236             state = (*(int *) mp->b_cont->b_rptr) & (CCP_ISUP | CCP_ISOPEN);
237             if ((state & CCP_ISOPEN) == 0) {
238                 if (cp->xstate != NULL) {
239                     (*cp->xcomp->comp_free)(cp->xstate);
240                     cp->xstate = NULL;
241                 }
242                 if (cp->rstate != NULL) {
243                     (*cp->rcomp->decomp_free)(cp->rstate);
244                     cp->rstate = NULL;
245                 }
246                 cp->ccp_state = 0;
247             } else {
248                 cp->ccp_state = (cp->ccp_state & ~CCP_ISUP) | state;
249             }
250             if (cp->debug)
251                 bsdlog(LOG_INFO, "SIOCSIFCOMP %x, state = %x\n",
252                     *(int *) mp->b_cont->b_rptr, cp->ccp_state);
253             error = 0;
254             iop->ioc_count = 0;
255             break;
256
257         case SIOCGIFCOMP:
258             if ((mp->b_cont = allocb(sizeof(int), BPRI_MED)) == NULL) {
259                 error = ENOSR;
260                 break;
261             }
262             *(int *)mp->b_cont->b_wptr = cp->ccp_state;
263             mp->b_cont->b_wptr += iop->ioc_count = sizeof(int);
264             break;
265
266         case SIOCSCOMPRESS:
267             error = EINVAL;
268             if (iop->ioc_count != TRANSPARENT)
269                 break;
270             odp = *((struct ppp_option_data **) mp->b_cont->b_rptr);
271             len = sizeof(odp->opt_data);
272             if (len > odp->length)
273                 len = odp->length;
274             if (odp->opt_data[1] < 2 || odp->opt_data[1] > len)
275                 break;
276             for (comp = ppp_compressors; *comp != NULL; ++comp)
277                 if ((*comp)->compress_proto == odp->opt_data[0]) {
278                     /* here's the handler! */
279                     error = 0;
280                     if (odp->transmit) {
281                         if (cp->xstate != NULL)
282                             (*cp->xcomp->comp_free)(cp->xstate);
283                         cp->xcomp = *comp;
284                         cp->xstate = (*comp)->comp_alloc(odp->opt_data, len);
285                         if (cp->xstate == NULL)
286                             error = ENOSR;
287                     } else {
288                         if (cp->rstate != NULL)
289                             (*cp->rcomp->decomp_free)(cp->rstate);
290                         cp->rcomp = *comp;
291                         cp->rstate = (*comp)->decomp_alloc(odp->opt_data, len);
292                         if (cp->rstate == NULL)
293                             error = ENOSR;
294                     }
295                     if (cp->debug)
296                         bsdlog(LOG_INFO, "SIOCSCOMPRESS %s len=%d\n",
297                             odp->transmit? "xmit": "recv", len);
298                     break;
299                 }
300             iop->ioc_count = 0;
301             break;
302
303         case SIOCSIFDEBUG:
304             /* set our debug flag from this */
305             if ((iop->ioc_count == TRANSPARENT) ||
306                 (iop->ioc_count == sizeof(int))) {
307                 cp->debug = *(int *) mp->b_cont->b_rptr & 1;
308             }
309             break;
310
311         case SIOCSIFMRU:
312             /* remember this value */
313             if ((iop->ioc_count == TRANSPARENT) ||
314                 (iop->ioc_count == sizeof(int))) {
315                 cp->mru = *(int *) mp->b_cont->b_rptr;
316             }
317             break;
318
319         }
320
321         if (error < 0)
322             putnext(q, mp);
323         else if (error == 0) {
324             mp->b_datap->db_type = M_IOCACK;
325             qreply(q, mp);
326         } else {
327             mp->b_datap->db_type = M_IOCNAK;
328             iop->ioc_count = 0;
329             qreply(q, mp);
330         }
331         break;
332
333     default:
334         putnext(q, mp);
335     }
336 }
337
338 static int
339 ppp_comp_rput(q, mp)
340     queue_t *q;
341     mblk_t *mp;
342 {
343     int proto, rv;
344     mblk_t *dmp;
345     struct ppp_comp_state *cp;
346
347     cp = (struct ppp_comp_state *) q->q_ptr;
348     switch (mp->b_datap->db_type) {
349
350     case M_DATA:
351         /* possibly a compressed packet to decompress,
352            or a CCP packet to take notice of. */
353         if (mp->b_wptr - mp->b_rptr >= PPP_HDRLEN
354             || pullupmsg(mp, PPP_HDRLEN)) {
355             proto = PPP_PROTOCOL(mp->b_rptr);
356             if (proto == PPP_CCP)
357                 ppp_comp_ccp(q, mp, 1);
358             else if (proto == PPP_COMP) {
359                 if ((cp->ccp_state & CCP_ISUP)
360                     && (cp->ccp_state & CCP_DECOMP_RUN) && cp->rstate
361                     && (cp->ccp_state & CCP_ERR) == 0) {
362                     rv = (*cp->rcomp->decompress)(cp->rstate, mp, &dmp);
363                     if (dmp != NULL) {
364                         freemsg(mp);
365                         mp = dmp;
366                     } else {
367                         switch (rv) {
368                         case DECOMP_OK:
369                             /* no error, but no packet returned */
370                             freemsg(mp);
371                             mp = NULL;
372                             break;
373                         case DECOMP_ERROR:
374                             cp->ccp_state |= CCP_ERROR;
375                             break;
376                         case DECOMP_FATALERROR:
377                             cp->ccp_state |= CCP_FATALERROR;
378                             break;
379                         }
380                     }
381                 }
382             } else if (cp->rstate && (cp->ccp_state & CCP_DECOMP_RUN)) {
383                 (*cp->rcomp->incomp)(cp->rstate, mp);
384             }
385         }
386         if (mp != NULL)
387             putnext(q, mp);
388         break;
389
390     default:
391         putnext(q, mp);
392     }
393 }
394
395 static void
396 ppp_comp_ccp(q, mp, rcvd)
397     queue_t *q;
398     mblk_t *mp;
399     int rcvd;
400 {
401     int len, clen;
402     struct ppp_comp_state *cp;
403     unsigned char *dp;
404
405     len = msgdsize(mp);
406     if (len < PPP_HDRLEN + CCP_HDRLEN || !pullupmsg(mp, len))
407         return;
408     cp = (struct ppp_comp_state *) q->q_ptr;
409     dp = mp->b_rptr + PPP_HDRLEN;
410     len -= PPP_HDRLEN;
411     clen = CCP_LENGTH(dp);
412     if (clen > len)
413         return;
414     if (cp->debug)
415         bsdlog(LOG_INFO, "CCP %s: code=%x len=%d\n", rcvd? "rcvd": "sent",
416             CCP_CODE(dp), clen);
417
418     switch (CCP_CODE(dp)) {
419     case CCP_CONFREQ:
420     case CCP_TERMREQ:
421     case CCP_TERMACK:
422         cp->ccp_state &= ~CCP_ISUP;
423         break;
424
425     case CCP_CONFACK:
426         if ((cp->ccp_state & (CCP_ISOPEN | CCP_ISUP)) == CCP_ISOPEN
427             && clen >= CCP_HDRLEN + CCP_OPT_MINLEN
428             && clen >= CCP_HDRLEN + CCP_OPT_LENGTH(dp + CCP_HDRLEN)) {
429             if (!rcvd) {
430                 if (cp->xstate != NULL
431                     && (*cp->xcomp->comp_init)
432                         (cp->xstate, dp + CCP_HDRLEN, clen - CCP_HDRLEN,
433                          0, /* XXX: should be unit */ 0,
434                          cp->debug))
435                     cp->ccp_state |= CCP_COMP_RUN;
436             } else {
437                 if (cp->rstate != NULL
438                     && (*cp->rcomp->decomp_init)
439                         (cp->rstate, dp + CCP_HDRLEN, clen - CCP_HDRLEN,
440                          0/* unit */, 0, cp->mru, cp->debug))
441                     cp->ccp_state = (cp->ccp_state & ~CCP_ERR)
442                         | CCP_DECOMP_RUN;
443             }
444         }
445         break;
446
447     case CCP_RESETACK:
448         if (cp->ccp_state & CCP_ISUP) {
449             if (!rcvd) {
450                 if (cp->xstate && (cp->ccp_state & CCP_COMP_RUN))
451                     (*cp->xcomp->comp_reset)(cp->xstate);
452             } else {
453                 if (cp->rstate && (cp->ccp_state & CCP_DECOMP_RUN)) {
454                     (*cp->rcomp->decomp_reset)(cp->rstate);
455                     cp->ccp_state &= ~CCP_ERROR;
456                 }
457             }
458         }
459         break;
460     }
461
462     if (cp->debug)
463         bsdlog(LOG_INFO, "ccp_state = %x\n", cp->ccp_state);
464 }