]> git.ozlabs.org Git - ppp.git/blob - pppd/ccp.c
use ppp_defs.h instead of ppp.h, args.h; change some names
[ppp.git] / pppd / ccp.c
1 /*
2  * ccp.c - PPP Compression Control Protocol.
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
28 #ifndef lint
29 static char rcsid[] = "$Id: ccp.c,v 1.5 1994/09/21 06:47:37 paulus Exp $";
30 #endif
31
32 #include <syslog.h>
33 #include <sys/ioctl.h>
34
35 #include "pppd.h"
36 #include "fsm.h"
37 #include "ccp.h"
38
39 fsm ccp_fsm[N_PPP];
40 ccp_options ccp_wantoptions[N_PPP];     /* what to request the peer to use */
41 ccp_options ccp_gotoptions[N_PPP];      /* what the peer agreed to do */
42 ccp_options ccp_allowoptions[N_PPP];    /* what we'll agree to do */
43 ccp_options ccp_hisoptions[N_PPP];      /* what we agreed to do */
44
45 /*
46  * Callbacks for fsm code.
47  */
48 static void ccp_resetci __P((fsm *));
49 static int  ccp_cilen __P((fsm *));
50 static void ccp_addci __P((fsm *, u_char *, int *));
51 static int  ccp_ackci __P((fsm *, u_char *, int));
52 static int  ccp_nakci __P((fsm *, u_char *, int));
53 static int  ccp_rejci __P((fsm *, u_char *, int));
54 static int  ccp_reqci __P((fsm *, u_char *, int *, int));
55 static void ccp_up __P((fsm *));
56 static void ccp_down __P((fsm *));
57 static int  ccp_extcode __P((fsm *, int, int, u_char *, int));
58 static void ccp_rack_timeout __P(());
59
60 static fsm_callbacks ccp_callbacks = {
61     ccp_resetci,
62     ccp_cilen,
63     ccp_addci,
64     ccp_ackci,
65     ccp_nakci,
66     ccp_rejci,
67     ccp_reqci,
68     ccp_up,
69     ccp_down,
70     NULL,
71     NULL,
72     NULL,
73     NULL,
74     ccp_extcode,
75     "CCP"
76 };
77
78 /*
79  * Length of configuration options, which describe possible
80  * compression methods.
81  */
82 #define CILEN_BSD       3
83
84 /*
85  * Configuration option values for compression methods.
86  */
87 #define CI_BSD_COMPRESS 0x21
88
89 /*
90  * Information relating to BSD Compress configuration options.
91  */
92 #define BSD_NBITS(x)            ((x) & 0x1F)
93 #define BSD_VERSION(x)          ((x) >> 5)
94 #define BSD_CURRENT_VERSION     1
95 #define BSD_MAKE_OPT(v, n)      (((v) << 5) | (n))
96
97 /*
98  * Do we want / did we get any compression?
99  */
100 #define ANY_COMPRESS(opt)       ((opt).bsd_compress)
101
102 /*
103  * Local state (mainly for handling reset-reqs and reset-acks
104  */
105 static int ccp_localstate[N_PPP];
106 #define RACK_PENDING    1       /* waiting for reset-ack */
107 #define RREQ_REPEAT     2       /* send another reset-req if no reset-ack */
108
109 #define RACKTIMEOUT     1       /* second */
110
111 /*
112  * ccp_init - initialize CCP.
113  */
114 void
115 ccp_init(unit)
116     int unit;
117 {
118     fsm *f = &ccp_fsm[unit];
119
120     f->unit = unit;
121     f->protocol = PPP_CCP;
122     f->callbacks = &ccp_callbacks;
123     fsm_init(f);
124
125     memset(&ccp_wantoptions[unit],  0, sizeof(ccp_options));
126     memset(&ccp_gotoptions[unit],   0, sizeof(ccp_options));
127     memset(&ccp_allowoptions[unit], 0, sizeof(ccp_options));
128     memset(&ccp_hisoptions[unit],   0, sizeof(ccp_options));
129
130     ccp_wantoptions[0].bsd_bits = 12;   /* default value */
131
132     ccp_allowoptions[0].bsd_compress = 1;
133     ccp_allowoptions[0].bsd_bits = MAX_BSD_BITS;
134 }
135
136 /*
137  * ccp_open - CCP is allowed to come up.
138  */
139 void
140 ccp_open(unit)
141     int unit;
142 {
143     fsm *f = &ccp_fsm[unit];
144
145     if (f->state != OPENED)
146         ccp_flags_set(unit, 1, 0);
147     if (!ANY_COMPRESS(ccp_wantoptions[unit]))
148         f->flags |= OPT_SILENT;
149     fsm_open(f);
150 }
151
152 /*
153  * ccp_close - Terminate CCP.
154  */
155 void
156 ccp_close(unit)
157     int unit;
158 {
159     ccp_flags_set(unit, 0, 0);
160     fsm_close(&ccp_fsm[unit]);
161 }
162
163 /*
164  * ccp_lowerup - we may now transmit CCP packets.
165  */
166 void
167 ccp_lowerup(unit)
168     int unit;
169 {
170     fsm_lowerup(&ccp_fsm[unit]);
171 }
172
173 /*
174  * ccp_lowerdown - we may not transmit CCP packets.
175  */
176 void
177 ccp_lowerdown(unit)
178     int unit;
179 {
180     fsm_lowerdown(&ccp_fsm[unit]);
181 }
182
183 /*
184  * ccp_input - process a received CCP packet.
185  */
186 void
187 ccp_input(unit, p, len)
188     int unit;
189     u_char *p;
190     int len;
191 {
192     fsm *f = &ccp_fsm[unit];
193     int oldstate;
194
195     /*
196      * Check for a terminate-request so we can print a message.
197      */
198     oldstate = f->state;
199     fsm_input(f, p, len);
200     if (oldstate == OPENED && p[0] == TERMREQ && f->state != OPENED)
201         syslog(LOG_NOTICE, "Compression disabled by peer.");
202 }
203
204 /*
205  * Handle a CCP-specific code.
206  */
207 static int
208 ccp_extcode(f, code, id, p, len)
209     fsm *f;
210     int code, id;
211     u_char *p;
212     int len;
213 {
214     switch (code) {
215     case RESETREQ:
216         if (f->state != OPENED)
217             break;
218         /* send a reset-ack, which the transmitter will see and
219            reset its compression state. */
220         fsm_sdata(f, RESETACK, id, NULL, 0);
221         break;
222
223     case RESETACK:
224         if (ccp_localstate[f->unit] & RACK_PENDING && id == f->reqid) {
225             ccp_localstate[f->unit] &= ~(RACK_PENDING | RREQ_REPEAT);
226             UNTIMEOUT(ccp_rack_timeout, (caddr_t) f);
227         }
228         break;
229
230     default:
231         return 0;
232     }
233
234     return 1;
235 }
236
237 /*
238  * ccp_protrej - peer doesn't talk CCP.
239  */
240 void
241 ccp_protrej(unit)
242     int unit;
243 {
244     ccp_flags_set(unit, 0, 0);
245     fsm_lowerdown(&ccp_fsm[unit]);
246 }
247
248 /*
249  * ccp_resetci - initialize at start of negotiation.
250  */
251 static void
252 ccp_resetci(f)
253     fsm *f;
254 {
255     ccp_options *go = &ccp_gotoptions[f->unit];
256     u_char opt_buf[16];
257
258     *go = ccp_wantoptions[f->unit];
259     if (go->bsd_compress) {
260         opt_buf[0] = CI_BSD_COMPRESS;
261         opt_buf[1] = CILEN_BSD;
262         opt_buf[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits);
263         if (!ccp_test(f->unit, opt_buf, CILEN_BSD, 0))
264             go->bsd_compress = 0;
265     }
266 }
267
268 /*
269  * ccp_cilen - Return total length of our configuration info.
270  */
271 static int
272 ccp_cilen(f)
273     fsm *f;
274 {
275     ccp_options *go = &ccp_gotoptions[f->unit];
276
277     return (go->bsd_compress? CILEN_BSD: 0);
278 }
279
280 /*
281  * ccp_addci - put our requests in a packet.
282  */
283 static void
284 ccp_addci(f, p, lenp)
285     fsm *f;
286     u_char *p;
287     int *lenp;
288 {
289     ccp_options *go = &ccp_gotoptions[f->unit];
290     u_char *p0 = p;
291
292     if (go->bsd_compress) {
293         p[0] = CI_BSD_COMPRESS;
294         p[1] = CILEN_BSD;
295         p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits);
296         if (ccp_test(f->unit, p, CILEN_BSD, 0))
297             p += CILEN_BSD;
298         else
299             go->bsd_compress = 0;
300     }
301     *lenp = p - p0;
302 }
303
304 /*
305  * ccp_ackci - process a received configure-ack, and return
306  * 1 iff the packet was OK.
307  */
308 static int
309 ccp_ackci(f, p, len)
310     fsm *f;
311     u_char *p;
312     int len;
313 {
314     ccp_options *go = &ccp_gotoptions[f->unit];
315
316     if (go->bsd_compress) {
317         if (len < CILEN_BSD || p[0] != CI_BSD_COMPRESS || p[1] != CILEN_BSD
318             || p[2] != BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits))
319             return 0;
320         p += CILEN_BSD;
321         len -= CILEN_BSD;
322     }
323     if (len != 0)
324         return 0;
325     return 1;
326 }
327
328 /*
329  * ccp_nakci - process received configure-nak.
330  * Returns 1 iff the nak was OK.
331  */
332 static int
333 ccp_nakci(f, p, len)
334     fsm *f;
335     u_char *p;
336     int len;
337 {
338     ccp_options *go = &ccp_gotoptions[f->unit];
339     ccp_options no;             /* options we've seen already */
340     ccp_options try;            /* options to ask for next time */
341
342     memset(&no, 0, sizeof(no));
343     try = *go;
344
345     if (go->bsd_compress && !no.bsd_compress && len >= CILEN_BSD
346         && p[0] == CI_BSD_COMPRESS && p[1] == CILEN_BSD) {
347         no.bsd_compress = 1;
348         /*
349          * Peer wants us to use a different number of bits
350          * or a different version.
351          */
352         if (BSD_VERSION(p[2]) != BSD_CURRENT_VERSION)
353             try.bsd_compress = 0;
354         else if (BSD_NBITS(p[2]) < go->bsd_bits)
355             try.bsd_bits = BSD_NBITS(p[2]);
356         p += CILEN_BSD;
357         len -= CILEN_BSD;
358     }
359
360     /*
361      * Have a look at any remaining options...???
362      */
363
364     if (len != 0)
365         return 0;
366
367     if (f->state != OPENED)
368         *go = try;
369     return 1;
370 }
371
372 /*
373  * ccp_rejci - reject some of our suggested compression methods.
374  */
375 static int
376 ccp_rejci(f, p, len)
377     fsm *f;
378     u_char *p;
379     int len;
380 {
381     ccp_options *go = &ccp_gotoptions[f->unit];
382     ccp_options try;            /* options to request next time */
383
384     try = *go;
385
386     if (go->bsd_compress && len >= CILEN_BSD
387         && p[0] == CI_BSD_COMPRESS && p[1] == CILEN_BSD) {
388         if (p[2] != BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits))
389             return 0;
390         try.bsd_compress = 0;
391         p += CILEN_BSD;
392         len -= CILEN_BSD;
393     }
394
395     if (len != 0)
396         return 0;
397
398     if (f->state != OPENED)
399         *go = try;
400
401     return 1;
402 }
403
404 /*
405  * ccp_reqci - processed a received configure-request.
406  * Returns CONFACK, CONFNAK or CONFREJ and the packet modified
407  * appropriately.
408  */
409 static int
410 ccp_reqci(f, p, lenp, dont_nak)
411     fsm *f;
412     u_char *p;
413     int *lenp;
414     int dont_nak;
415 {
416     int ret, newret;
417     u_char *p0, *retp;
418     int len, clen, type, nb;
419     ccp_options *ho = &ccp_hisoptions[f->unit];
420     ccp_options *ao = &ccp_allowoptions[f->unit];
421
422     ret = CONFACK;
423     retp = p0 = p;
424     len = *lenp;
425
426     memset(ho, 0, sizeof(ccp_options));
427
428     while (len > 0) {
429         newret = CONFACK;
430         if (len < 2 || p[1] < 2 || p[1] > len) {
431             /* length is bad */
432             clen = len;
433             newret = CONFREJ;
434
435         } else {
436             type = p[0];
437             clen = p[1];
438
439             switch (type) {
440             case CI_BSD_COMPRESS:
441                 if (!ao->bsd_compress || clen != CILEN_BSD) {
442                     newret = CONFREJ;
443                     break;
444                 }
445
446                 ho->bsd_compress = 1;
447                 ho->bsd_bits = nb = BSD_NBITS(p[2]);
448                 if (BSD_VERSION(p[2]) != BSD_CURRENT_VERSION
449                     || nb > ao->bsd_bits) {
450                     newret = CONFNAK;
451                     nb = ao->bsd_bits;
452                 } else if (nb < MIN_BSD_BITS) {
453                     newret = CONFREJ;
454                 } else if (!ccp_test(f->unit, p, CILEN_BSD, 1)) {
455                     if (nb > MIN_BSD_BITS) {
456                         --nb;
457                         newret = CONFNAK;
458                     } else
459                         newret = CONFREJ;
460                 }
461                 if (newret == CONFNAK && !dont_nak) {
462                     p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, nb);
463                 }
464
465                 break;
466
467             default:
468                 newret = CONFREJ;
469             }
470         }
471
472         if (!(newret == CONFACK || newret == CONFNAK && ret == CONFREJ)) {
473             /* we're returning this option */
474             ret = newret;
475             if (p != retp)
476                 BCOPY(p, retp, clen);
477             retp += clen;
478         }
479
480         p += clen;
481         len -= clen;
482     }
483
484     if (ret != CONFACK)
485         *lenp = retp - p0;
486     return ret;
487 }
488
489 /*
490  * CCP has come up - inform the kernel driver.
491  */
492 static void
493 ccp_up(f)
494     fsm *f;
495 {
496     ccp_options *go = &ccp_gotoptions[f->unit];
497     ccp_options *ho = &ccp_hisoptions[f->unit];
498
499     ccp_flags_set(f->unit, 1, 1);
500     if (go->bsd_compress || ho->bsd_compress)
501         syslog(LOG_NOTICE, "%s enabled",
502                go->bsd_compress? ho->bsd_compress? "Compression":
503                "Receive compression": "Transmit compression");
504 }
505
506 /*
507  * CCP has gone down - inform the kernel driver.
508  */
509 static void
510 ccp_down(f)
511     fsm *f;
512 {
513     if (ccp_localstate[f->unit] & RACK_PENDING)
514         UNTIMEOUT(ccp_rack_timeout, (caddr_t) f);
515     ccp_localstate[f->unit] = 0;
516     ccp_flags_set(f->unit, 1, 0);
517 }
518
519 /*
520  * Print the contents of a CCP packet.
521  */
522 char *ccp_codenames[] = {
523     "ConfReq", "ConfAck", "ConfNak", "ConfRej",
524     "TermReq", "TermAck", "CodeRej",
525     NULL, NULL, NULL, NULL, NULL, NULL,
526     "ResetReq", "ResetAck",
527 };
528
529 int
530 ccp_printpkt(p, plen, printer, arg)
531     u_char *p;
532     int plen;
533     void (*printer) __P((void *, char *, ...));
534     void *arg;
535 {
536     u_char *p0, *optend;
537     int code, id, len;
538     int optlen;
539
540     p0 = p;
541     if (plen < HEADERLEN)
542         return 0;
543     code = p[0];
544     id = p[1];
545     len = (p[2] << 8) + p[3];
546     if (len < HEADERLEN || len > plen)
547         return 0;
548
549     if (code >= 1 && code <= sizeof(ccp_codenames) / sizeof(char *)
550         && ccp_codenames[code-1] != NULL)
551         printer(arg, " %s", ccp_codenames[code-1]);
552     else
553         printer(arg, " code=0x%x", code);
554     printer(arg, " id=0x%x", id);
555     len -= HEADERLEN;
556     p += HEADERLEN;
557
558     switch (code) {
559     case CONFREQ:
560     case CONFACK:
561     case CONFNAK:
562     case CONFREJ:
563         /* print list of possible compression methods */
564         while (len >= 2) {
565             code = p[0];
566             optlen = p[1];
567             if (optlen < 2 || optlen > len)
568                 break;
569             printer(arg, " <");
570             len -= optlen;
571             optend = p + optlen;
572             switch (code) {
573             case CI_BSD_COMPRESS:
574                 if (optlen >= CILEN_BSD) {
575                     printer(arg, "bsd v%d %d", BSD_VERSION(p[2]),
576                             BSD_NBITS(p[2]));
577                     p += CILEN_BSD;
578                 }
579                 break;
580             }
581             while (p < optend)
582                 printer(arg, " %.2x", *p++);
583             printer(arg, ">");
584         }
585         break;
586     }
587
588     /* dump out the rest of the packet in hex */
589     while (--len >= 0)
590         printer(arg, " %.2x", *p++);
591
592     return p - p0;
593 }
594
595 /*
596  * We have received a packet that the decompressor failed to
597  * decompress.  Here we would expect to issue a reset-request, but
598  * Motorola has a patent on resetting the compressor as a result of
599  * detecting an error in the decompressed data after decompression.
600  * (See US patent 5,130,993; international patent publication number
601  * WO 91/10289; Australian patent 73296/91.)
602  *
603  * So we ask the kernel whether the error was detected after
604  * decompression; if it was, we take CCP down, thus disabling
605  * compression :-(, otherwise we issue the reset-request.
606  */
607 void
608 ccp_datainput(unit, pkt, len)
609     int unit;
610     u_char *pkt;
611     int len;
612 {
613     fsm *f;
614
615     f = &ccp_fsm[unit];
616     if (f->state == OPENED) {
617         if (ccp_fatal_error(unit)) {
618             /*
619              * Disable compression by taking CCP down.
620              */
621             syslog(LOG_ERR, "Lost compression sync: disabling compression");
622             ccp_close(unit);
623         } else {
624             /*
625              * Send a reset-request to reset the peer's compressor.
626              * We don't do that if we are still waiting for an
627              * acknowledgement to a previous reset-request.
628              */
629             if (!(ccp_localstate[f->unit] & RACK_PENDING)) {
630                 fsm_sdata(f, RESETREQ, f->reqid = ++f->id, NULL, 0);
631                 TIMEOUT(ccp_rack_timeout, (caddr_t) f, RACKTIMEOUT);
632                 ccp_localstate[f->unit] |= RACK_PENDING;
633             } else
634                 ccp_localstate[f->unit] |= RREQ_REPEAT;
635         }
636     }
637 }
638
639 /*
640  * Timeout waiting for reset-ack.
641  */
642 static void
643 ccp_rack_timeout(arg)
644     caddr_t arg;
645 {
646     fsm *f = (fsm *) arg;
647
648     if (f->state == OPENED && ccp_localstate[f->unit] & RREQ_REPEAT) {
649         fsm_sdata(f, RESETREQ, f->reqid, NULL, 0);
650         TIMEOUT(ccp_rack_timeout, (caddr_t) f, RACKTIMEOUT);
651         ccp_localstate[f->unit] &= ~RREQ_REPEAT;
652     } else
653         ccp_localstate[f->unit] &= ~RACK_PENDING;
654 }
655