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