]> git.ozlabs.org Git - ppp.git/blob - pppd/ccp.c
Because of a bug in zlib, restrict deflate parameter to 9..15 for now.
[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 #define RCSID   "$Id: ccp.c,v 1.31 2001/02/22 03:10:06 paulus Exp $"
29
30 #include <stdlib.h>
31 #include <string.h>
32
33 #include "pppd.h"
34 #include "fsm.h"
35 #include "ccp.h"
36 #include <net/ppp-comp.h>
37
38 static const char rcsid[] = RCSID;
39
40 /*
41  * Unfortunately there is a bug in zlib which means that using a
42  * size of 8 (window size = 256) for Deflate compression will cause
43  * buffer overruns and kernel crashes in the deflate module.
44  * Until this is fixed we only accept sizes in the range 9 .. 15.
45  * Thanks to James Carlson for pointing this out.
46  */
47 #define DEFLATE_MIN_WORKS       9
48
49 /*
50  * Command-line options.
51  */
52 static int setbsdcomp __P((char **));
53 static int setdeflate __P((char **));
54
55 static option_t ccp_option_list[] = {
56     { "noccp", o_bool, &ccp_protent.enabled_flag,
57       "Disable CCP negotiation" },
58     { "-ccp", o_bool, &ccp_protent.enabled_flag,
59       "Disable CCP negotiation" },
60     { "bsdcomp", o_special, (void *)setbsdcomp,
61       "Request BSD-Compress packet compression" },
62     { "nobsdcomp", o_bool, &ccp_wantoptions[0].bsd_compress,
63       "don't allow BSD-Compress", OPT_A2COPY,
64       &ccp_allowoptions[0].bsd_compress },
65     { "-bsdcomp", o_bool, &ccp_wantoptions[0].bsd_compress,
66       "don't allow BSD-Compress", OPT_A2COPY,
67       &ccp_allowoptions[0].bsd_compress },
68     { "deflate", o_special, (void *)setdeflate,
69       "request Deflate compression" },
70     { "nodeflate", o_bool, &ccp_wantoptions[0].deflate,
71       "don't allow Deflate compression", OPT_A2COPY,
72       &ccp_allowoptions[0].deflate },
73     { "-deflate", o_bool, &ccp_wantoptions[0].deflate,
74       "don't allow Deflate compression", OPT_A2COPY,
75       &ccp_allowoptions[0].deflate },
76     { "nodeflatedraft", o_bool, &ccp_wantoptions[0].deflate_draft,
77       "don't use draft deflate #", OPT_A2COPY,
78       &ccp_allowoptions[0].deflate_draft },
79     { "predictor1", o_bool, &ccp_wantoptions[0].predictor_1,
80       "request Predictor-1", 1, &ccp_allowoptions[0].predictor_1 },
81     { "nopredictor1", o_bool, &ccp_wantoptions[0].predictor_1,
82       "don't allow Predictor-1", OPT_A2COPY,
83       &ccp_allowoptions[0].predictor_1 },
84     { "-predictor1", o_bool, &ccp_wantoptions[0].predictor_1,
85       "don't allow Predictor-1", OPT_A2COPY,
86       &ccp_allowoptions[0].predictor_1 },
87
88     { NULL }
89 };
90
91 /*
92  * Protocol entry points from main code.
93  */
94 static void ccp_init __P((int unit));
95 static void ccp_open __P((int unit));
96 static void ccp_close __P((int unit, char *));
97 static void ccp_lowerup __P((int unit));
98 static void ccp_lowerdown __P((int));
99 static void ccp_input __P((int unit, u_char *pkt, int len));
100 static void ccp_protrej __P((int unit));
101 static int  ccp_printpkt __P((u_char *pkt, int len,
102                               void (*printer) __P((void *, char *, ...)),
103                               void *arg));
104 static void ccp_datainput __P((int unit, u_char *pkt, int len));
105
106 struct protent ccp_protent = {
107     PPP_CCP,
108     ccp_init,
109     ccp_input,
110     ccp_protrej,
111     ccp_lowerup,
112     ccp_lowerdown,
113     ccp_open,
114     ccp_close,
115     ccp_printpkt,
116     ccp_datainput,
117     1,
118     "CCP",
119     "Compressed",
120     ccp_option_list,
121     NULL,
122     NULL,
123     NULL
124 };
125
126 fsm ccp_fsm[NUM_PPP];
127 ccp_options ccp_wantoptions[NUM_PPP];   /* what to request the peer to use */
128 ccp_options ccp_gotoptions[NUM_PPP];    /* what the peer agreed to do */
129 ccp_options ccp_allowoptions[NUM_PPP];  /* what we'll agree to do */
130 ccp_options ccp_hisoptions[NUM_PPP];    /* what we agreed to do */
131
132 /*
133  * Callbacks for fsm code.
134  */
135 static void ccp_resetci __P((fsm *));
136 static int  ccp_cilen __P((fsm *));
137 static void ccp_addci __P((fsm *, u_char *, int *));
138 static int  ccp_ackci __P((fsm *, u_char *, int));
139 static int  ccp_nakci __P((fsm *, u_char *, int));
140 static int  ccp_rejci __P((fsm *, u_char *, int));
141 static int  ccp_reqci __P((fsm *, u_char *, int *, int));
142 static void ccp_up __P((fsm *));
143 static void ccp_down __P((fsm *));
144 static int  ccp_extcode __P((fsm *, int, int, u_char *, int));
145 static void ccp_rack_timeout __P((void *));
146 static char *method_name __P((ccp_options *, ccp_options *));
147
148 static fsm_callbacks ccp_callbacks = {
149     ccp_resetci,
150     ccp_cilen,
151     ccp_addci,
152     ccp_ackci,
153     ccp_nakci,
154     ccp_rejci,
155     ccp_reqci,
156     ccp_up,
157     ccp_down,
158     NULL,
159     NULL,
160     NULL,
161     NULL,
162     ccp_extcode,
163     "CCP"
164 };
165
166 /*
167  * Do we want / did we get any compression?
168  */
169 #define ANY_COMPRESS(opt)       ((opt).deflate || (opt).bsd_compress \
170                                  || (opt).predictor_1 || (opt).predictor_2)
171
172 /*
173  * Local state (mainly for handling reset-reqs and reset-acks).
174  */
175 static int ccp_localstate[NUM_PPP];
176 #define RACK_PENDING    1       /* waiting for reset-ack */
177 #define RREQ_REPEAT     2       /* send another reset-req if no reset-ack */
178
179 #define RACKTIMEOUT     1       /* second */
180
181 static int all_rejected[NUM_PPP];       /* we rejected all peer's options */
182
183 /*
184  * Option parsing.
185  */
186 static int
187 setbsdcomp(argv)
188     char **argv;
189 {
190     int rbits, abits;
191     char *str, *endp;
192
193     str = *argv;
194     abits = rbits = strtol(str, &endp, 0);
195     if (endp != str && *endp == ',') {
196         str = endp + 1;
197         abits = strtol(str, &endp, 0);
198     }
199     if (*endp != 0 || endp == str) {
200         option_error("invalid parameter '%s' for bsdcomp option", *argv);
201         return 0;
202     }
203     if ((rbits != 0 && (rbits < BSD_MIN_BITS || rbits > BSD_MAX_BITS))
204         || (abits != 0 && (abits < BSD_MIN_BITS || abits > BSD_MAX_BITS))) {
205         option_error("bsdcomp option values must be 0 or %d .. %d",
206                      BSD_MIN_BITS, BSD_MAX_BITS);
207         return 0;
208     }
209     if (rbits > 0) {
210         ccp_wantoptions[0].bsd_compress = 1;
211         ccp_wantoptions[0].bsd_bits = rbits;
212     } else
213         ccp_wantoptions[0].bsd_compress = 0;
214     if (abits > 0) {
215         ccp_allowoptions[0].bsd_compress = 1;
216         ccp_allowoptions[0].bsd_bits = abits;
217     } else
218         ccp_allowoptions[0].bsd_compress = 0;
219     return 1;
220 }
221
222 static int
223 setdeflate(argv)
224     char **argv;
225 {
226     int rbits, abits;
227     char *str, *endp;
228
229     str = *argv;
230     abits = rbits = strtol(str, &endp, 0);
231     if (endp != str && *endp == ',') {
232         str = endp + 1;
233         abits = strtol(str, &endp, 0);
234     }
235     if (*endp != 0 || endp == str) {
236         option_error("invalid parameter '%s' for deflate option", *argv);
237         return 0;
238     }
239     if ((rbits != 0 && (rbits < DEFLATE_MIN_SIZE || rbits > DEFLATE_MAX_SIZE))
240         || (abits != 0 && (abits < DEFLATE_MIN_SIZE
241                           || abits > DEFLATE_MAX_SIZE))) {
242         option_error("deflate option values must be 0 or %d .. %d",
243                      DEFLATE_MIN_SIZE, DEFLATE_MAX_SIZE);
244         return 0;
245     }
246     if (rbits == DEFLATE_MIN_SIZE || abits == DEFLATE_MIN_SIZE) {
247         if (rbits == DEFLATE_MIN_SIZE)
248             rbits = DEFLATE_MIN_WORKS;
249         if (abits == DEFLATE_MIN_SIZE)
250             abits = DEFLATE_MIN_WORKS;
251         warn("deflate option value of %d changed to %d to avoid zlib bug",
252              DEFLATE_MIN_SIZE, DEFLATE_MIN_WORKS);
253     }
254     if (rbits > 0) {
255         ccp_wantoptions[0].deflate = 1;
256         ccp_wantoptions[0].deflate_size = rbits;
257     } else
258         ccp_wantoptions[0].deflate = 0;
259     if (abits > 0) {
260         ccp_allowoptions[0].deflate = 1;
261         ccp_allowoptions[0].deflate_size = abits;
262     } else
263         ccp_allowoptions[0].deflate = 0;
264     return 1;
265 }
266
267
268 /*
269  * ccp_init - initialize CCP.
270  */
271 static void
272 ccp_init(unit)
273     int unit;
274 {
275     fsm *f = &ccp_fsm[unit];
276
277     f->unit = unit;
278     f->protocol = PPP_CCP;
279     f->callbacks = &ccp_callbacks;
280     fsm_init(f);
281
282     memset(&ccp_wantoptions[unit],  0, sizeof(ccp_options));
283     memset(&ccp_gotoptions[unit],   0, sizeof(ccp_options));
284     memset(&ccp_allowoptions[unit], 0, sizeof(ccp_options));
285     memset(&ccp_hisoptions[unit],   0, sizeof(ccp_options));
286
287     ccp_wantoptions[0].deflate = 1;
288     ccp_wantoptions[0].deflate_size = DEFLATE_MAX_SIZE;
289     ccp_wantoptions[0].deflate_correct = 1;
290     ccp_wantoptions[0].deflate_draft = 1;
291     ccp_allowoptions[0].deflate = 1;
292     ccp_allowoptions[0].deflate_size = DEFLATE_MAX_SIZE;
293     ccp_allowoptions[0].deflate_correct = 1;
294     ccp_allowoptions[0].deflate_draft = 1;
295
296     ccp_wantoptions[0].bsd_compress = 1;
297     ccp_wantoptions[0].bsd_bits = BSD_MAX_BITS;
298     ccp_allowoptions[0].bsd_compress = 1;
299     ccp_allowoptions[0].bsd_bits = BSD_MAX_BITS;
300
301     ccp_allowoptions[0].predictor_1 = 1;
302 }
303
304 /*
305  * ccp_open - CCP is allowed to come up.
306  */
307 static void
308 ccp_open(unit)
309     int unit;
310 {
311     fsm *f = &ccp_fsm[unit];
312
313     if (f->state != OPENED)
314         ccp_flags_set(unit, 1, 0);
315
316     /*
317      * Find out which compressors the kernel supports before
318      * deciding whether to open in silent mode.
319      */
320     ccp_resetci(f);
321     if (!ANY_COMPRESS(ccp_gotoptions[unit]))
322         f->flags |= OPT_SILENT;
323
324     fsm_open(f);
325 }
326
327 /*
328  * ccp_close - Terminate CCP.
329  */
330 static void
331 ccp_close(unit, reason)
332     int unit;
333     char *reason;
334 {
335     ccp_flags_set(unit, 0, 0);
336     fsm_close(&ccp_fsm[unit], reason);
337 }
338
339 /*
340  * ccp_lowerup - we may now transmit CCP packets.
341  */
342 static void
343 ccp_lowerup(unit)
344     int unit;
345 {
346     fsm_lowerup(&ccp_fsm[unit]);
347 }
348
349 /*
350  * ccp_lowerdown - we may not transmit CCP packets.
351  */
352 static void
353 ccp_lowerdown(unit)
354     int unit;
355 {
356     fsm_lowerdown(&ccp_fsm[unit]);
357 }
358
359 /*
360  * ccp_input - process a received CCP packet.
361  */
362 static void
363 ccp_input(unit, p, len)
364     int unit;
365     u_char *p;
366     int len;
367 {
368     fsm *f = &ccp_fsm[unit];
369     int oldstate;
370
371     /*
372      * Check for a terminate-request so we can print a message.
373      */
374     oldstate = f->state;
375     fsm_input(f, p, len);
376     if (oldstate == OPENED && p[0] == TERMREQ && f->state != OPENED)
377         notice("Compression disabled by peer.");
378
379     /*
380      * If we get a terminate-ack and we're not asking for compression,
381      * close CCP.
382      */
383     if (oldstate == REQSENT && p[0] == TERMACK
384         && !ANY_COMPRESS(ccp_gotoptions[unit]))
385         ccp_close(unit, "No compression negotiated");
386 }
387
388 /*
389  * Handle a CCP-specific code.
390  */
391 static int
392 ccp_extcode(f, code, id, p, len)
393     fsm *f;
394     int code, id;
395     u_char *p;
396     int len;
397 {
398     switch (code) {
399     case CCP_RESETREQ:
400         if (f->state != OPENED)
401             break;
402         /* send a reset-ack, which the transmitter will see and
403            reset its compression state. */
404         fsm_sdata(f, CCP_RESETACK, id, NULL, 0);
405         break;
406
407     case CCP_RESETACK:
408         if (ccp_localstate[f->unit] & RACK_PENDING && id == f->reqid) {
409             ccp_localstate[f->unit] &= ~(RACK_PENDING | RREQ_REPEAT);
410             UNTIMEOUT(ccp_rack_timeout, f);
411         }
412         break;
413
414     default:
415         return 0;
416     }
417
418     return 1;
419 }
420
421 /*
422  * ccp_protrej - peer doesn't talk CCP.
423  */
424 static void
425 ccp_protrej(unit)
426     int unit;
427 {
428     ccp_flags_set(unit, 0, 0);
429     fsm_lowerdown(&ccp_fsm[unit]);
430 }
431
432 /*
433  * ccp_resetci - initialize at start of negotiation.
434  */
435 static void
436 ccp_resetci(f)
437     fsm *f;
438 {
439     ccp_options *go = &ccp_gotoptions[f->unit];
440     u_char opt_buf[16];
441
442     *go = ccp_wantoptions[f->unit];
443     all_rejected[f->unit] = 0;
444
445     /*
446      * Check whether the kernel knows about the various
447      * compression methods we might request.
448      */
449     if (go->bsd_compress) {
450         opt_buf[0] = CI_BSD_COMPRESS;
451         opt_buf[1] = CILEN_BSD_COMPRESS;
452         opt_buf[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, BSD_MIN_BITS);
453         if (ccp_test(f->unit, opt_buf, CILEN_BSD_COMPRESS, 0) <= 0)
454             go->bsd_compress = 0;
455     }
456     if (go->deflate) {
457         if (go->deflate_correct) {
458             opt_buf[0] = CI_DEFLATE;
459             opt_buf[1] = CILEN_DEFLATE;
460             opt_buf[2] = DEFLATE_MAKE_OPT(DEFLATE_MIN_WORKS);
461             opt_buf[3] = DEFLATE_CHK_SEQUENCE;
462             if (ccp_test(f->unit, opt_buf, CILEN_DEFLATE, 0) <= 0)
463                 go->deflate_correct = 0;
464         }
465         if (go->deflate_draft) {
466             opt_buf[0] = CI_DEFLATE_DRAFT;
467             opt_buf[1] = CILEN_DEFLATE;
468             opt_buf[2] = DEFLATE_MAKE_OPT(DEFLATE_MIN_WORKS);
469             opt_buf[3] = DEFLATE_CHK_SEQUENCE;
470             if (ccp_test(f->unit, opt_buf, CILEN_DEFLATE, 0) <= 0)
471                 go->deflate_draft = 0;
472         }
473         if (!go->deflate_correct && !go->deflate_draft)
474             go->deflate = 0;
475     }
476     if (go->predictor_1) {
477         opt_buf[0] = CI_PREDICTOR_1;
478         opt_buf[1] = CILEN_PREDICTOR_1;
479         if (ccp_test(f->unit, opt_buf, CILEN_PREDICTOR_1, 0) <= 0)
480             go->predictor_1 = 0;
481     }
482     if (go->predictor_2) {
483         opt_buf[0] = CI_PREDICTOR_2;
484         opt_buf[1] = CILEN_PREDICTOR_2;
485         if (ccp_test(f->unit, opt_buf, CILEN_PREDICTOR_2, 0) <= 0)
486             go->predictor_2 = 0;
487     }
488 }
489
490 /*
491  * ccp_cilen - Return total length of our configuration info.
492  */
493 static int
494 ccp_cilen(f)
495     fsm *f;
496 {
497     ccp_options *go = &ccp_gotoptions[f->unit];
498
499     return (go->bsd_compress? CILEN_BSD_COMPRESS: 0)
500         + (go->deflate? CILEN_DEFLATE: 0)
501         + (go->predictor_1? CILEN_PREDICTOR_1: 0)
502         + (go->predictor_2? CILEN_PREDICTOR_2: 0);
503 }
504
505 /*
506  * ccp_addci - put our requests in a packet.
507  */
508 static void
509 ccp_addci(f, p, lenp)
510     fsm *f;
511     u_char *p;
512     int *lenp;
513 {
514     int res;
515     ccp_options *go = &ccp_gotoptions[f->unit];
516     u_char *p0 = p;
517
518     /*
519      * Add the compression types that we can receive, in decreasing
520      * preference order.  Get the kernel to allocate the first one
521      * in case it gets Acked.
522      */
523     if (go->deflate) {
524         p[0] = go->deflate_correct? CI_DEFLATE: CI_DEFLATE_DRAFT;
525         p[1] = CILEN_DEFLATE;
526         p[2] = DEFLATE_MAKE_OPT(go->deflate_size);
527         p[3] = DEFLATE_CHK_SEQUENCE;
528         for (;;) {
529             res = ccp_test(f->unit, p, CILEN_DEFLATE, 0);
530             if (res > 0) {
531                 p += CILEN_DEFLATE;
532                 break;
533             }
534             if (res < 0 || go->deflate_size <= DEFLATE_MIN_WORKS) {
535                 go->deflate = 0;
536                 break;
537             }
538             --go->deflate_size;
539             p[2] = DEFLATE_MAKE_OPT(go->deflate_size);
540         }
541         if (p != p0 && go->deflate_correct && go->deflate_draft) {
542             p[0] = CI_DEFLATE_DRAFT;
543             p[1] = CILEN_DEFLATE;
544             p[2] = p[2 - CILEN_DEFLATE];
545             p[3] = DEFLATE_CHK_SEQUENCE;
546             p += CILEN_DEFLATE;
547         }
548     }
549     if (go->bsd_compress) {
550         p[0] = CI_BSD_COMPRESS;
551         p[1] = CILEN_BSD_COMPRESS;
552         p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits);
553         if (p != p0) {
554             p += CILEN_BSD_COMPRESS;    /* not the first option */
555         } else {
556             for (;;) {
557                 res = ccp_test(f->unit, p, CILEN_BSD_COMPRESS, 0);
558                 if (res > 0) {
559                     p += CILEN_BSD_COMPRESS;
560                     break;
561                 }
562                 if (res < 0 || go->bsd_bits <= BSD_MIN_BITS) {
563                     go->bsd_compress = 0;
564                     break;
565                 }
566                 --go->bsd_bits;
567                 p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits);
568             }
569         }
570     }
571     /* XXX Should Predictor 2 be preferable to Predictor 1? */
572     if (go->predictor_1) {
573         p[0] = CI_PREDICTOR_1;
574         p[1] = CILEN_PREDICTOR_1;
575         if (p == p0 && ccp_test(f->unit, p, CILEN_PREDICTOR_1, 0) <= 0) {
576             go->predictor_1 = 0;
577         } else {
578             p += CILEN_PREDICTOR_1;
579         }
580     }
581     if (go->predictor_2) {
582         p[0] = CI_PREDICTOR_2;
583         p[1] = CILEN_PREDICTOR_2;
584         if (p == p0 && ccp_test(f->unit, p, CILEN_PREDICTOR_2, 0) <= 0) {
585             go->predictor_2 = 0;
586         } else {
587             p += CILEN_PREDICTOR_2;
588         }
589     }
590
591     go->method = (p > p0)? p0[0]: -1;
592
593     *lenp = p - p0;
594 }
595
596 /*
597  * ccp_ackci - process a received configure-ack, and return
598  * 1 iff the packet was OK.
599  */
600 static int
601 ccp_ackci(f, p, len)
602     fsm *f;
603     u_char *p;
604     int len;
605 {
606     ccp_options *go = &ccp_gotoptions[f->unit];
607     u_char *p0 = p;
608
609     if (go->deflate) {
610         if (len < CILEN_DEFLATE
611             || p[0] != (go->deflate_correct? CI_DEFLATE: CI_DEFLATE_DRAFT)
612             || p[1] != CILEN_DEFLATE
613             || p[2] != DEFLATE_MAKE_OPT(go->deflate_size)
614             || p[3] != DEFLATE_CHK_SEQUENCE)
615             return 0;
616         p += CILEN_DEFLATE;
617         len -= CILEN_DEFLATE;
618         /* XXX Cope with first/fast ack */
619         if (len == 0)
620             return 1;
621         if (go->deflate_correct && go->deflate_draft) {
622             if (len < CILEN_DEFLATE
623                 || p[0] != CI_DEFLATE_DRAFT
624                 || p[1] != CILEN_DEFLATE
625                 || p[2] != DEFLATE_MAKE_OPT(go->deflate_size)
626                 || p[3] != DEFLATE_CHK_SEQUENCE)
627                 return 0;
628             p += CILEN_DEFLATE;
629             len -= CILEN_DEFLATE;
630         }
631     }
632     if (go->bsd_compress) {
633         if (len < CILEN_BSD_COMPRESS
634             || p[0] != CI_BSD_COMPRESS || p[1] != CILEN_BSD_COMPRESS
635             || p[2] != BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits))
636             return 0;
637         p += CILEN_BSD_COMPRESS;
638         len -= CILEN_BSD_COMPRESS;
639         /* XXX Cope with first/fast ack */
640         if (p == p0 && len == 0)
641             return 1;
642     }
643     if (go->predictor_1) {
644         if (len < CILEN_PREDICTOR_1
645             || p[0] != CI_PREDICTOR_1 || p[1] != CILEN_PREDICTOR_1)
646             return 0;
647         p += CILEN_PREDICTOR_1;
648         len -= CILEN_PREDICTOR_1;
649         /* XXX Cope with first/fast ack */
650         if (p == p0 && len == 0)
651             return 1;
652     }
653     if (go->predictor_2) {
654         if (len < CILEN_PREDICTOR_2
655             || p[0] != CI_PREDICTOR_2 || p[1] != CILEN_PREDICTOR_2)
656             return 0;
657         p += CILEN_PREDICTOR_2;
658         len -= CILEN_PREDICTOR_2;
659         /* XXX Cope with first/fast ack */
660         if (p == p0 && len == 0)
661             return 1;
662     }
663
664     if (len != 0)
665         return 0;
666     return 1;
667 }
668
669 /*
670  * ccp_nakci - process received configure-nak.
671  * Returns 1 iff the nak was OK.
672  */
673 static int
674 ccp_nakci(f, p, len)
675     fsm *f;
676     u_char *p;
677     int len;
678 {
679     ccp_options *go = &ccp_gotoptions[f->unit];
680     ccp_options no;             /* options we've seen already */
681     ccp_options try;            /* options to ask for next time */
682
683     memset(&no, 0, sizeof(no));
684     try = *go;
685
686     if (go->deflate && len >= CILEN_DEFLATE
687         && p[0] == (go->deflate_correct? CI_DEFLATE: CI_DEFLATE_DRAFT)
688         && p[1] == CILEN_DEFLATE) {
689         no.deflate = 1;
690         /*
691          * Peer wants us to use a different code size or something.
692          * Stop asking for Deflate if we don't understand his suggestion.
693          */
694         if (DEFLATE_METHOD(p[2]) != DEFLATE_METHOD_VAL
695             || DEFLATE_SIZE(p[2]) < DEFLATE_MIN_WORKS
696             || p[3] != DEFLATE_CHK_SEQUENCE)
697             try.deflate = 0;
698         else if (DEFLATE_SIZE(p[2]) < go->deflate_size)
699             try.deflate_size = DEFLATE_SIZE(p[2]);
700         p += CILEN_DEFLATE;
701         len -= CILEN_DEFLATE;
702         if (go->deflate_correct && go->deflate_draft
703             && len >= CILEN_DEFLATE && p[0] == CI_DEFLATE_DRAFT
704             && p[1] == CILEN_DEFLATE) {
705             p += CILEN_DEFLATE;
706             len -= CILEN_DEFLATE;
707         }
708     }
709
710     if (go->bsd_compress && len >= CILEN_BSD_COMPRESS
711         && p[0] == CI_BSD_COMPRESS && p[1] == CILEN_BSD_COMPRESS) {
712         no.bsd_compress = 1;
713         /*
714          * Peer wants us to use a different number of bits
715          * or a different version.
716          */
717         if (BSD_VERSION(p[2]) != BSD_CURRENT_VERSION)
718             try.bsd_compress = 0;
719         else if (BSD_NBITS(p[2]) < go->bsd_bits)
720             try.bsd_bits = BSD_NBITS(p[2]);
721         p += CILEN_BSD_COMPRESS;
722         len -= CILEN_BSD_COMPRESS;
723     }
724
725     /*
726      * Predictor-1 and 2 have no options, so they can't be Naked.
727      *
728      * There may be remaining options but we ignore them.
729      */
730
731     if (f->state != OPENED)
732         *go = try;
733     return 1;
734 }
735
736 /*
737  * ccp_rejci - reject some of our suggested compression methods.
738  */
739 static int
740 ccp_rejci(f, p, len)
741     fsm *f;
742     u_char *p;
743     int len;
744 {
745     ccp_options *go = &ccp_gotoptions[f->unit];
746     ccp_options try;            /* options to request next time */
747
748     try = *go;
749
750     /*
751      * Cope with empty configure-rejects by ceasing to send
752      * configure-requests.
753      */
754     if (len == 0 && all_rejected[f->unit])
755         return -1;
756
757     if (go->deflate && len >= CILEN_DEFLATE
758         && p[0] == (go->deflate_correct? CI_DEFLATE: CI_DEFLATE_DRAFT)
759         && p[1] == CILEN_DEFLATE) {
760         if (p[2] != DEFLATE_MAKE_OPT(go->deflate_size)
761             || p[3] != DEFLATE_CHK_SEQUENCE)
762             return 0;           /* Rej is bad */
763         if (go->deflate_correct)
764             try.deflate_correct = 0;
765         else
766             try.deflate_draft = 0;
767         p += CILEN_DEFLATE;
768         len -= CILEN_DEFLATE;
769         if (go->deflate_correct && go->deflate_draft
770             && len >= CILEN_DEFLATE && p[0] == CI_DEFLATE_DRAFT
771             && p[1] == CILEN_DEFLATE) {
772             if (p[2] != DEFLATE_MAKE_OPT(go->deflate_size)
773                 || p[3] != DEFLATE_CHK_SEQUENCE)
774                 return 0;               /* Rej is bad */
775             try.deflate_draft = 0;
776             p += CILEN_DEFLATE;
777             len -= CILEN_DEFLATE;
778         }
779         if (!try.deflate_correct && !try.deflate_draft)
780             try.deflate = 0;
781     }
782     if (go->bsd_compress && len >= CILEN_BSD_COMPRESS
783         && p[0] == CI_BSD_COMPRESS && p[1] == CILEN_BSD_COMPRESS) {
784         if (p[2] != BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits))
785             return 0;
786         try.bsd_compress = 0;
787         p += CILEN_BSD_COMPRESS;
788         len -= CILEN_BSD_COMPRESS;
789     }
790     if (go->predictor_1 && len >= CILEN_PREDICTOR_1
791         && p[0] == CI_PREDICTOR_1 && p[1] == CILEN_PREDICTOR_1) {
792         try.predictor_1 = 0;
793         p += CILEN_PREDICTOR_1;
794         len -= CILEN_PREDICTOR_1;
795     }
796     if (go->predictor_2 && len >= CILEN_PREDICTOR_2
797         && p[0] == CI_PREDICTOR_2 && p[1] == CILEN_PREDICTOR_2) {
798         try.predictor_2 = 0;
799         p += CILEN_PREDICTOR_2;
800         len -= CILEN_PREDICTOR_2;
801     }
802
803     if (len != 0)
804         return 0;
805
806     if (f->state != OPENED)
807         *go = try;
808
809     return 1;
810 }
811
812 /*
813  * ccp_reqci - processed a received configure-request.
814  * Returns CONFACK, CONFNAK or CONFREJ and the packet modified
815  * appropriately.
816  */
817 static int
818 ccp_reqci(f, p, lenp, dont_nak)
819     fsm *f;
820     u_char *p;
821     int *lenp;
822     int dont_nak;
823 {
824     int ret, newret, res;
825     u_char *p0, *retp;
826     int len, clen, type, nb;
827     ccp_options *ho = &ccp_hisoptions[f->unit];
828     ccp_options *ao = &ccp_allowoptions[f->unit];
829
830     ret = CONFACK;
831     retp = p0 = p;
832     len = *lenp;
833
834     memset(ho, 0, sizeof(ccp_options));
835     ho->method = (len > 0)? p[0]: -1;
836
837     while (len > 0) {
838         newret = CONFACK;
839         if (len < 2 || p[1] < 2 || p[1] > len) {
840             /* length is bad */
841             clen = len;
842             newret = CONFREJ;
843
844         } else {
845             type = p[0];
846             clen = p[1];
847
848             switch (type) {
849             case CI_DEFLATE:
850             case CI_DEFLATE_DRAFT:
851                 if (!ao->deflate || clen != CILEN_DEFLATE
852                     || (!ao->deflate_correct && type == CI_DEFLATE)
853                     || (!ao->deflate_draft && type == CI_DEFLATE_DRAFT)) {
854                     newret = CONFREJ;
855                     break;
856                 }
857
858                 ho->deflate = 1;
859                 ho->deflate_size = nb = DEFLATE_SIZE(p[2]);
860                 if (DEFLATE_METHOD(p[2]) != DEFLATE_METHOD_VAL
861                     || p[3] != DEFLATE_CHK_SEQUENCE
862                     || nb > ao->deflate_size || nb < DEFLATE_MIN_WORKS) {
863                     newret = CONFNAK;
864                     if (!dont_nak) {
865                         p[2] = DEFLATE_MAKE_OPT(ao->deflate_size);
866                         p[3] = DEFLATE_CHK_SEQUENCE;
867                         /* fall through to test this #bits below */
868                     } else
869                         break;
870                 }
871
872                 /*
873                  * Check whether we can do Deflate with the window
874                  * size they want.  If the window is too big, reduce
875                  * it until the kernel can cope and nak with that.
876                  * We only check this for the first option.
877                  */
878                 if (p == p0) {
879                     for (;;) {
880                         res = ccp_test(f->unit, p, CILEN_DEFLATE, 1);
881                         if (res > 0)
882                             break;              /* it's OK now */
883                         if (res < 0 || nb == DEFLATE_MIN_WORKS || dont_nak) {
884                             newret = CONFREJ;
885                             p[2] = DEFLATE_MAKE_OPT(ho->deflate_size);
886                             break;
887                         }
888                         newret = CONFNAK;
889                         --nb;
890                         p[2] = DEFLATE_MAKE_OPT(nb);
891                     }
892                 }
893                 break;
894
895             case CI_BSD_COMPRESS:
896                 if (!ao->bsd_compress || clen != CILEN_BSD_COMPRESS) {
897                     newret = CONFREJ;
898                     break;
899                 }
900
901                 ho->bsd_compress = 1;
902                 ho->bsd_bits = nb = BSD_NBITS(p[2]);
903                 if (BSD_VERSION(p[2]) != BSD_CURRENT_VERSION
904                     || nb > ao->bsd_bits || nb < BSD_MIN_BITS) {
905                     newret = CONFNAK;
906                     if (!dont_nak) {
907                         p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, ao->bsd_bits);
908                         /* fall through to test this #bits below */
909                     } else
910                         break;
911                 }
912
913                 /*
914                  * Check whether we can do BSD-Compress with the code
915                  * size they want.  If the code size is too big, reduce
916                  * it until the kernel can cope and nak with that.
917                  * We only check this for the first option.
918                  */
919                 if (p == p0) {
920                     for (;;) {
921                         res = ccp_test(f->unit, p, CILEN_BSD_COMPRESS, 1);
922                         if (res > 0)
923                             break;
924                         if (res < 0 || nb == BSD_MIN_BITS || dont_nak) {
925                             newret = CONFREJ;
926                             p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION,
927                                                 ho->bsd_bits);
928                             break;
929                         }
930                         newret = CONFNAK;
931                         --nb;
932                         p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, nb);
933                     }
934                 }
935                 break;
936
937             case CI_PREDICTOR_1:
938                 if (!ao->predictor_1 || clen != CILEN_PREDICTOR_1) {
939                     newret = CONFREJ;
940                     break;
941                 }
942
943                 ho->predictor_1 = 1;
944                 if (p == p0
945                     && ccp_test(f->unit, p, CILEN_PREDICTOR_1, 1) <= 0) {
946                     newret = CONFREJ;
947                 }
948                 break;
949
950             case CI_PREDICTOR_2:
951                 if (!ao->predictor_2 || clen != CILEN_PREDICTOR_2) {
952                     newret = CONFREJ;
953                     break;
954                 }
955
956                 ho->predictor_2 = 1;
957                 if (p == p0
958                     && ccp_test(f->unit, p, CILEN_PREDICTOR_2, 1) <= 0) {
959                     newret = CONFREJ;
960                 }
961                 break;
962
963             default:
964                 newret = CONFREJ;
965             }
966         }
967
968         if (newret == CONFNAK && dont_nak)
969             newret = CONFREJ;
970         if (!(newret == CONFACK || (newret == CONFNAK && ret == CONFREJ))) {
971             /* we're returning this option */
972             if (newret == CONFREJ && ret == CONFNAK)
973                 retp = p0;
974             ret = newret;
975             if (p != retp)
976                 BCOPY(p, retp, clen);
977             retp += clen;
978         }
979
980         p += clen;
981         len -= clen;
982     }
983
984     if (ret != CONFACK) {
985         if (ret == CONFREJ && *lenp == retp - p0)
986             all_rejected[f->unit] = 1;
987         else
988             *lenp = retp - p0;
989     }
990     return ret;
991 }
992
993 /*
994  * Make a string name for a compression method (or 2).
995  */
996 static char *
997 method_name(opt, opt2)
998     ccp_options *opt, *opt2;
999 {
1000     static char result[64];
1001
1002     if (!ANY_COMPRESS(*opt))
1003         return "(none)";
1004     switch (opt->method) {
1005     case CI_DEFLATE:
1006     case CI_DEFLATE_DRAFT:
1007         if (opt2 != NULL && opt2->deflate_size != opt->deflate_size)
1008             slprintf(result, sizeof(result), "Deflate%s (%d/%d)",
1009                      (opt->method == CI_DEFLATE_DRAFT? "(old#)": ""),
1010                      opt->deflate_size, opt2->deflate_size);
1011         else
1012             slprintf(result, sizeof(result), "Deflate%s (%d)",
1013                      (opt->method == CI_DEFLATE_DRAFT? "(old#)": ""),
1014                      opt->deflate_size);
1015         break;
1016     case CI_BSD_COMPRESS:
1017         if (opt2 != NULL && opt2->bsd_bits != opt->bsd_bits)
1018             slprintf(result, sizeof(result), "BSD-Compress (%d/%d)",
1019                      opt->bsd_bits, opt2->bsd_bits);
1020         else
1021             slprintf(result, sizeof(result), "BSD-Compress (%d)",
1022                      opt->bsd_bits);
1023         break;
1024     case CI_PREDICTOR_1:
1025         return "Predictor 1";
1026     case CI_PREDICTOR_2:
1027         return "Predictor 2";
1028     default:
1029         slprintf(result, sizeof(result), "Method %d", opt->method);
1030     }
1031     return result;
1032 }
1033
1034 /*
1035  * CCP has come up - inform the kernel driver and log a message.
1036  */
1037 static void
1038 ccp_up(f)
1039     fsm *f;
1040 {
1041     ccp_options *go = &ccp_gotoptions[f->unit];
1042     ccp_options *ho = &ccp_hisoptions[f->unit];
1043     char method1[64];
1044
1045     ccp_flags_set(f->unit, 1, 1);
1046     if (ANY_COMPRESS(*go)) {
1047         if (ANY_COMPRESS(*ho)) {
1048             if (go->method == ho->method) {
1049                 notice("%s compression enabled", method_name(go, ho));
1050             } else {
1051                 strlcpy(method1, method_name(go, NULL), sizeof(method1));
1052                 notice("%s / %s compression enabled",
1053                        method1, method_name(ho, NULL));
1054             }
1055         } else
1056             notice("%s receive compression enabled", method_name(go, NULL));
1057     } else if (ANY_COMPRESS(*ho))
1058         notice("%s transmit compression enabled", method_name(ho, NULL));
1059 }
1060
1061 /*
1062  * CCP has gone down - inform the kernel driver.
1063  */
1064 static void
1065 ccp_down(f)
1066     fsm *f;
1067 {
1068     if (ccp_localstate[f->unit] & RACK_PENDING)
1069         UNTIMEOUT(ccp_rack_timeout, f);
1070     ccp_localstate[f->unit] = 0;
1071     ccp_flags_set(f->unit, 1, 0);
1072 }
1073
1074 /*
1075  * Print the contents of a CCP packet.
1076  */
1077 static char *ccp_codenames[] = {
1078     "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1079     "TermReq", "TermAck", "CodeRej",
1080     NULL, NULL, NULL, NULL, NULL, NULL,
1081     "ResetReq", "ResetAck",
1082 };
1083
1084 static int
1085 ccp_printpkt(p, plen, printer, arg)
1086     u_char *p;
1087     int plen;
1088     void (*printer) __P((void *, char *, ...));
1089     void *arg;
1090 {
1091     u_char *p0, *optend;
1092     int code, id, len;
1093     int optlen;
1094
1095     p0 = p;
1096     if (plen < HEADERLEN)
1097         return 0;
1098     code = p[0];
1099     id = p[1];
1100     len = (p[2] << 8) + p[3];
1101     if (len < HEADERLEN || len > plen)
1102         return 0;
1103
1104     if (code >= 1 && code <= sizeof(ccp_codenames) / sizeof(char *)
1105         && ccp_codenames[code-1] != NULL)
1106         printer(arg, " %s", ccp_codenames[code-1]);
1107     else
1108         printer(arg, " code=0x%x", code);
1109     printer(arg, " id=0x%x", id);
1110     len -= HEADERLEN;
1111     p += HEADERLEN;
1112
1113     switch (code) {
1114     case CONFREQ:
1115     case CONFACK:
1116     case CONFNAK:
1117     case CONFREJ:
1118         /* print list of possible compression methods */
1119         while (len >= 2) {
1120             code = p[0];
1121             optlen = p[1];
1122             if (optlen < 2 || optlen > len)
1123                 break;
1124             printer(arg, " <");
1125             len -= optlen;
1126             optend = p + optlen;
1127             switch (code) {
1128             case CI_DEFLATE:
1129             case CI_DEFLATE_DRAFT:
1130                 if (optlen >= CILEN_DEFLATE) {
1131                     printer(arg, "deflate%s %d",
1132                             (code == CI_DEFLATE_DRAFT? "(old#)": ""),
1133                             DEFLATE_SIZE(p[2]));
1134                     if (DEFLATE_METHOD(p[2]) != DEFLATE_METHOD_VAL)
1135                         printer(arg, " method %d", DEFLATE_METHOD(p[2]));
1136                     if (p[3] != DEFLATE_CHK_SEQUENCE)
1137                         printer(arg, " check %d", p[3]);
1138                     p += CILEN_DEFLATE;
1139                 }
1140                 break;
1141             case CI_BSD_COMPRESS:
1142                 if (optlen >= CILEN_BSD_COMPRESS) {
1143                     printer(arg, "bsd v%d %d", BSD_VERSION(p[2]),
1144                             BSD_NBITS(p[2]));
1145                     p += CILEN_BSD_COMPRESS;
1146                 }
1147                 break;
1148             case CI_PREDICTOR_1:
1149                 if (optlen >= CILEN_PREDICTOR_1) {
1150                     printer(arg, "predictor 1");
1151                     p += CILEN_PREDICTOR_1;
1152                 }
1153                 break;
1154             case CI_PREDICTOR_2:
1155                 if (optlen >= CILEN_PREDICTOR_2) {
1156                     printer(arg, "predictor 2");
1157                     p += CILEN_PREDICTOR_2;
1158                 }
1159                 break;
1160             }
1161             while (p < optend)
1162                 printer(arg, " %.2x", *p++);
1163             printer(arg, ">");
1164         }
1165         break;
1166
1167     case TERMACK:
1168     case TERMREQ:
1169         if (len > 0 && *p >= ' ' && *p < 0x7f) {
1170             print_string((char *)p, len, printer, arg);
1171             p += len;
1172             len = 0;
1173         }
1174         break;
1175     }
1176
1177     /* dump out the rest of the packet in hex */
1178     while (--len >= 0)
1179         printer(arg, " %.2x", *p++);
1180
1181     return p - p0;
1182 }
1183
1184 /*
1185  * We have received a packet that the decompressor failed to
1186  * decompress.  Here we would expect to issue a reset-request, but
1187  * Motorola has a patent on resetting the compressor as a result of
1188  * detecting an error in the decompressed data after decompression.
1189  * (See US patent 5,130,993; international patent publication number
1190  * WO 91/10289; Australian patent 73296/91.)
1191  *
1192  * So we ask the kernel whether the error was detected after
1193  * decompression; if it was, we take CCP down, thus disabling
1194  * compression :-(, otherwise we issue the reset-request.
1195  */
1196 static void
1197 ccp_datainput(unit, pkt, len)
1198     int unit;
1199     u_char *pkt;
1200     int len;
1201 {
1202     fsm *f;
1203
1204     f = &ccp_fsm[unit];
1205     if (f->state == OPENED) {
1206         if (ccp_fatal_error(unit)) {
1207             /*
1208              * Disable compression by taking CCP down.
1209              */
1210             error("Lost compression sync: disabling compression");
1211             ccp_close(unit, "Lost compression sync");
1212         } else {
1213             /*
1214              * Send a reset-request to reset the peer's compressor.
1215              * We don't do that if we are still waiting for an
1216              * acknowledgement to a previous reset-request.
1217              */
1218             if (!(ccp_localstate[f->unit] & RACK_PENDING)) {
1219                 fsm_sdata(f, CCP_RESETREQ, f->reqid = ++f->id, NULL, 0);
1220                 TIMEOUT(ccp_rack_timeout, f, RACKTIMEOUT);
1221                 ccp_localstate[f->unit] |= RACK_PENDING;
1222             } else
1223                 ccp_localstate[f->unit] |= RREQ_REPEAT;
1224         }
1225     }
1226 }
1227
1228 /*
1229  * Timeout waiting for reset-ack.
1230  */
1231 static void
1232 ccp_rack_timeout(arg)
1233     void *arg;
1234 {
1235     fsm *f = arg;
1236
1237     if (f->state == OPENED && ccp_localstate[f->unit] & RREQ_REPEAT) {
1238         fsm_sdata(f, CCP_RESETREQ, f->reqid, NULL, 0);
1239         TIMEOUT(ccp_rack_timeout, f, RACKTIMEOUT);
1240         ccp_localstate[f->unit] &= ~RREQ_REPEAT;
1241     } else
1242         ccp_localstate[f->unit] &= ~RACK_PENDING;
1243 }
1244