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