]> git.ozlabs.org Git - ppp.git/blob - pppd/ccp.c
Makefile.am: Add explicit openssl directory to pppd include path
[ppp.git] / pppd / ccp.c
1 /*
2  * ccp.c - PPP Compression Control Protocol.
3  *
4  * Copyright (c) 1994-2002 Paul Mackerras. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
13  * 2. The name(s) of the authors of this software must not be used to
14  *    endorse or promote products derived from this software without
15  *    prior written permission.
16  *
17  * 3. Redistributions of any form whatsoever must retain the following
18  *    acknowledgment:
19  *    "This product includes software developed by Paul Mackerras
20  *     <paulus@samba.org>".
21  *
22  * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
23  * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
24  * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
25  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
26  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
27  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
28  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
29  */
30
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
34
35 #define RCSID   "$Id: ccp.c,v 1.50 2005/06/26 19:34:41 carlsonj Exp $"
36
37 #include <stdlib.h>
38 #include <string.h>
39 #if defined(SOL2)
40 #include <net/ppp-comp.h>
41 #else
42 #include <linux/ppp-comp.h>
43 #endif
44
45 #include "pppd-private.h"
46 #include "options.h"
47 #include "fsm.h"
48 #include "ccp.h"
49
50 #include "chap_ms.h"
51 #include "mppe.h"
52 #include "lcp.h"        /* lcp_close(), lcp_fsm */
53
54
55 /*
56  * Unfortunately there is a bug in zlib which means that using a
57  * size of 8 (window size = 256) for Deflate compression will cause
58  * buffer overruns and kernel crashes in the deflate module.
59  * Until this is fixed we only accept sizes in the range 9 .. 15.
60  * Thanks to James Carlson for pointing this out.
61  */
62 #define DEFLATE_MIN_WORKS       9
63
64 /*
65  * Command-line options.
66  */
67 static int setbsdcomp (char **);
68 static int setdeflate (char **);
69 static char bsd_value[8];
70 static char deflate_value[8];
71
72 /*
73  * Option variables.
74  */
75 #ifdef PPP_WITH_MPPE
76 bool refuse_mppe_stateful = 1;          /* Allow stateful mode? */
77 #endif
78
79 static struct option ccp_option_list[] = {
80     { "noccp", o_bool, &ccp_protent.enabled_flag,
81       "Disable CCP negotiation" },
82     { "-ccp", o_bool, &ccp_protent.enabled_flag,
83       "Disable CCP negotiation", OPT_ALIAS },
84
85     { "bsdcomp", o_special, (void *)setbsdcomp,
86       "Request BSD-Compress packet compression",
87       OPT_PRIO | OPT_A2STRVAL | OPT_STATIC, bsd_value },
88     { "nobsdcomp", o_bool, &ccp_wantoptions[0].bsd_compress,
89       "don't allow BSD-Compress", OPT_PRIOSUB | OPT_A2CLR,
90       &ccp_allowoptions[0].bsd_compress },
91     { "-bsdcomp", o_bool, &ccp_wantoptions[0].bsd_compress,
92       "don't allow BSD-Compress", OPT_ALIAS | OPT_PRIOSUB | OPT_A2CLR,
93       &ccp_allowoptions[0].bsd_compress },
94
95     { "deflate", o_special, (void *)setdeflate,
96       "request Deflate compression",
97       OPT_PRIO | OPT_A2STRVAL | OPT_STATIC, deflate_value },
98     { "nodeflate", o_bool, &ccp_wantoptions[0].deflate,
99       "don't allow Deflate compression", OPT_PRIOSUB | OPT_A2CLR,
100       &ccp_allowoptions[0].deflate },
101     { "-deflate", o_bool, &ccp_wantoptions[0].deflate,
102       "don't allow Deflate compression", OPT_ALIAS | OPT_PRIOSUB | OPT_A2CLR,
103       &ccp_allowoptions[0].deflate },
104
105     { "nodeflatedraft", o_bool, &ccp_wantoptions[0].deflate_draft,
106       "don't use draft deflate #", OPT_A2COPY,
107       &ccp_allowoptions[0].deflate_draft },
108
109     { "predictor1", o_bool, &ccp_wantoptions[0].predictor_1,
110       "request Predictor-1", OPT_PRIO | 1 },
111     { "nopredictor1", o_bool, &ccp_wantoptions[0].predictor_1,
112       "don't allow Predictor-1", OPT_PRIOSUB | OPT_A2CLR,
113       &ccp_allowoptions[0].predictor_1 },
114     { "-predictor1", o_bool, &ccp_wantoptions[0].predictor_1,
115       "don't allow Predictor-1", OPT_ALIAS | OPT_PRIOSUB | OPT_A2CLR,
116       &ccp_allowoptions[0].predictor_1 },
117
118 #ifdef PPP_WITH_MPPE
119     /* MPPE options are symmetrical ... we only set wantoptions here */
120     { "require-mppe", o_bool, &ccp_wantoptions[0].mppe,
121       "require MPPE encryption",
122       OPT_PRIO | MPPE_OPT_40 | MPPE_OPT_128 },
123     { "+mppe", o_bool, &ccp_wantoptions[0].mppe,
124       "require MPPE encryption",
125       OPT_ALIAS | OPT_PRIO | MPPE_OPT_40 | MPPE_OPT_128 },
126     { "nomppe", o_bool, &ccp_wantoptions[0].mppe,
127       "don't allow MPPE encryption", OPT_PRIO },
128     { "-mppe", o_bool, &ccp_wantoptions[0].mppe,
129       "don't allow MPPE encryption", OPT_ALIAS | OPT_PRIO },
130
131     /* We use ccp_allowoptions[0].mppe as a junk var ... it is reset later */
132     { "require-mppe-40", o_bool, &ccp_allowoptions[0].mppe,
133       "require MPPE 40-bit encryption", OPT_PRIO | OPT_A2OR | MPPE_OPT_40,
134       &ccp_wantoptions[0].mppe },
135     { "+mppe-40", o_bool, &ccp_allowoptions[0].mppe,
136       "require MPPE 40-bit encryption", OPT_PRIO | OPT_A2OR | MPPE_OPT_40,
137       &ccp_wantoptions[0].mppe },
138     { "nomppe-40", o_bool, &ccp_allowoptions[0].mppe,
139       "don't allow MPPE 40-bit encryption",
140       OPT_PRIOSUB | OPT_A2CLRB | MPPE_OPT_40, &ccp_wantoptions[0].mppe },
141     { "-mppe-40", o_bool, &ccp_allowoptions[0].mppe,
142       "don't allow MPPE 40-bit encryption",
143       OPT_ALIAS | OPT_PRIOSUB | OPT_A2CLRB | MPPE_OPT_40,
144       &ccp_wantoptions[0].mppe },
145
146     { "require-mppe-128", o_bool, &ccp_allowoptions[0].mppe,
147       "require MPPE 128-bit encryption", OPT_PRIO | OPT_A2OR | MPPE_OPT_128,
148       &ccp_wantoptions[0].mppe },
149     { "+mppe-128", o_bool, &ccp_allowoptions[0].mppe,
150       "require MPPE 128-bit encryption",
151       OPT_ALIAS | OPT_PRIO | OPT_A2OR | MPPE_OPT_128,
152       &ccp_wantoptions[0].mppe },
153     { "nomppe-128", o_bool, &ccp_allowoptions[0].mppe,
154       "don't allow MPPE 128-bit encryption",
155       OPT_PRIOSUB | OPT_A2CLRB | MPPE_OPT_128, &ccp_wantoptions[0].mppe },
156     { "-mppe-128", o_bool, &ccp_allowoptions[0].mppe,
157       "don't allow MPPE 128-bit encryption",
158       OPT_ALIAS | OPT_PRIOSUB | OPT_A2CLRB | MPPE_OPT_128,
159       &ccp_wantoptions[0].mppe },
160
161     /* strange one; we always request stateless, but will we allow stateful? */
162     { "mppe-stateful", o_bool, &refuse_mppe_stateful,
163       "allow MPPE stateful mode", OPT_PRIO },
164     { "nomppe-stateful", o_bool, &refuse_mppe_stateful,
165       "disallow MPPE stateful mode", OPT_PRIO | 1 },
166 #endif /* MPPE */
167
168     { NULL }
169 };
170
171 /*
172  * Protocol entry points from main code.
173  */
174 static void ccp_init (int unit);
175 static void ccp_open (int unit);
176 static void ccp_close (int unit, char *);
177 static void ccp_lowerup (int unit);
178 static void ccp_lowerdown (int);
179 static void ccp_input (int unit, u_char *pkt, int len);
180 static void ccp_protrej (int unit);
181 static int  ccp_printpkt (u_char *pkt, int len,
182                           void (*printer)(void *, char *, ...),
183                           void *arg);
184 static void ccp_datainput (int unit, u_char *pkt, int len);
185
186 struct protent ccp_protent = {
187     PPP_CCP,
188     ccp_init,
189     ccp_input,
190     ccp_protrej,
191     ccp_lowerup,
192     ccp_lowerdown,
193     ccp_open,
194     ccp_close,
195     ccp_printpkt,
196     ccp_datainput,
197     1,
198     "CCP",
199     "Compressed",
200     ccp_option_list,
201     NULL,
202     NULL,
203     NULL
204 };
205
206 fsm ccp_fsm[NUM_PPP];
207 ccp_options ccp_wantoptions[NUM_PPP];   /* what to request the peer to use */
208 ccp_options ccp_gotoptions[NUM_PPP];    /* what the peer agreed to do */
209 ccp_options ccp_allowoptions[NUM_PPP];  /* what we'll agree to do */
210 ccp_options ccp_hisoptions[NUM_PPP];    /* what we agreed to do */
211
212 /*
213  * Callbacks for fsm code.
214  */
215 static void ccp_resetci (fsm *);
216 static int  ccp_cilen (fsm *);
217 static void ccp_addci (fsm *, u_char *, int *);
218 static int  ccp_ackci (fsm *, u_char *, int);
219 static int  ccp_nakci (fsm *, u_char *, int, int);
220 static int  ccp_rejci (fsm *, u_char *, int);
221 static int  ccp_reqci (fsm *, u_char *, int *, int);
222 static void ccp_up (fsm *);
223 static void ccp_down (fsm *);
224 static int  ccp_extcode (fsm *, int, int, u_char *, int);
225 static void ccp_rack_timeout (void *);
226 static char *method_name (ccp_options *, ccp_options *);
227
228 static fsm_callbacks ccp_callbacks = {
229     ccp_resetci,
230     ccp_cilen,
231     ccp_addci,
232     ccp_ackci,
233     ccp_nakci,
234     ccp_rejci,
235     ccp_reqci,
236     ccp_up,
237     ccp_down,
238     NULL,
239     NULL,
240     NULL,
241     NULL,
242     ccp_extcode,
243     "CCP"
244 };
245
246 /*
247  * Do we want / did we get any compression?
248  */
249 #define ANY_COMPRESS(opt)       ((opt).deflate || (opt).bsd_compress \
250                                  || (opt).predictor_1 || (opt).predictor_2 \
251                                  || (opt).mppe)
252
253 /*
254  * Local state (mainly for handling reset-reqs and reset-acks).
255  */
256 static int ccp_localstate[NUM_PPP];
257 #define RACK_PENDING    1       /* waiting for reset-ack */
258 #define RREQ_REPEAT     2       /* send another reset-req if no reset-ack */
259
260 #define RACKTIMEOUT     1       /* second */
261
262 static int all_rejected[NUM_PPP];       /* we rejected all peer's options */
263
264 /*
265  * Option parsing.
266  */
267 static int
268 setbsdcomp(char **argv)
269 {
270     int rbits, abits;
271     char *str, *endp;
272
273     str = *argv;
274     abits = rbits = strtol(str, &endp, 0);
275     if (endp != str && *endp == ',') {
276         str = endp + 1;
277         abits = strtol(str, &endp, 0);
278     }
279     if (*endp != 0 || endp == str) {
280         ppp_option_error("invalid parameter '%s' for bsdcomp option", *argv);
281         return 0;
282     }
283     if ((rbits != 0 && (rbits < BSD_MIN_BITS || rbits > BSD_MAX_BITS))
284         || (abits != 0 && (abits < BSD_MIN_BITS || abits > BSD_MAX_BITS))) {
285         ppp_option_error("bsdcomp option values must be 0 or %d .. %d",
286                      BSD_MIN_BITS, BSD_MAX_BITS);
287         return 0;
288     }
289     if (rbits > 0) {
290         ccp_wantoptions[0].bsd_compress = 1;
291         ccp_wantoptions[0].bsd_bits = rbits;
292     } else
293         ccp_wantoptions[0].bsd_compress = 0;
294     if (abits > 0) {
295         ccp_allowoptions[0].bsd_compress = 1;
296         ccp_allowoptions[0].bsd_bits = abits;
297     } else
298         ccp_allowoptions[0].bsd_compress = 0;
299     slprintf(bsd_value, sizeof(bsd_value),
300              rbits == abits? "%d": "%d,%d", rbits, abits);
301
302     return 1;
303 }
304
305 static int
306 setdeflate(char **argv)
307 {
308     int rbits, abits;
309     char *str, *endp;
310
311     str = *argv;
312     abits = rbits = strtol(str, &endp, 0);
313     if (endp != str && *endp == ',') {
314         str = endp + 1;
315         abits = strtol(str, &endp, 0);
316     }
317     if (*endp != 0 || endp == str) {
318         ppp_option_error("invalid parameter '%s' for deflate option", *argv);
319         return 0;
320     }
321     if ((rbits != 0 && (rbits < DEFLATE_MIN_SIZE || rbits > DEFLATE_MAX_SIZE))
322         || (abits != 0 && (abits < DEFLATE_MIN_SIZE
323                           || abits > DEFLATE_MAX_SIZE))) {
324         ppp_option_error("deflate option values must be 0 or %d .. %d",
325                      DEFLATE_MIN_SIZE, DEFLATE_MAX_SIZE);
326         return 0;
327     }
328     if (rbits == DEFLATE_MIN_SIZE || abits == DEFLATE_MIN_SIZE) {
329         if (rbits == DEFLATE_MIN_SIZE)
330             rbits = DEFLATE_MIN_WORKS;
331         if (abits == DEFLATE_MIN_SIZE)
332             abits = DEFLATE_MIN_WORKS;
333         warn("deflate option value of %d changed to %d to avoid zlib bug",
334              DEFLATE_MIN_SIZE, DEFLATE_MIN_WORKS);
335     }
336     if (rbits > 0) {
337         ccp_wantoptions[0].deflate = 1;
338         ccp_wantoptions[0].deflate_size = rbits;
339     } else
340         ccp_wantoptions[0].deflate = 0;
341     if (abits > 0) {
342         ccp_allowoptions[0].deflate = 1;
343         ccp_allowoptions[0].deflate_size = abits;
344     } else
345         ccp_allowoptions[0].deflate = 0;
346     slprintf(deflate_value, sizeof(deflate_value),
347              rbits == abits? "%d": "%d,%d", rbits, abits);
348
349     return 1;
350 }
351
352 /*
353  * ccp_init - initialize CCP.
354  */
355 static void
356 ccp_init(int unit)
357 {
358     fsm *f = &ccp_fsm[unit];
359
360     f->unit = unit;
361     f->protocol = PPP_CCP;
362     f->callbacks = &ccp_callbacks;
363     fsm_init(f);
364
365     memset(&ccp_wantoptions[unit],  0, sizeof(ccp_options));
366     memset(&ccp_gotoptions[unit],   0, sizeof(ccp_options));
367     memset(&ccp_allowoptions[unit], 0, sizeof(ccp_options));
368     memset(&ccp_hisoptions[unit],   0, sizeof(ccp_options));
369
370     ccp_wantoptions[0].deflate = 1;
371     ccp_wantoptions[0].deflate_size = DEFLATE_MAX_SIZE;
372     ccp_wantoptions[0].deflate_correct = 1;
373     ccp_wantoptions[0].deflate_draft = 1;
374     ccp_allowoptions[0].deflate = 1;
375     ccp_allowoptions[0].deflate_size = DEFLATE_MAX_SIZE;
376     ccp_allowoptions[0].deflate_correct = 1;
377     ccp_allowoptions[0].deflate_draft = 1;
378
379     ccp_wantoptions[0].bsd_compress = 1;
380     ccp_wantoptions[0].bsd_bits = BSD_MAX_BITS;
381     ccp_allowoptions[0].bsd_compress = 1;
382     ccp_allowoptions[0].bsd_bits = BSD_MAX_BITS;
383
384     ccp_allowoptions[0].predictor_1 = 1;
385 }
386
387 /*
388  * ccp_open - CCP is allowed to come up.
389  */
390 static void
391 ccp_open(int unit)
392 {
393     fsm *f = &ccp_fsm[unit];
394
395     if (f->state != OPENED)
396         ccp_flags_set(unit, 1, 0);
397
398     /*
399      * Find out which compressors the kernel supports before
400      * deciding whether to open in silent mode.
401      */
402     ccp_resetci(f);
403     if (!ANY_COMPRESS(ccp_gotoptions[unit]))
404         f->flags |= OPT_SILENT;
405
406     fsm_open(f);
407 }
408
409 /*
410  * ccp_close - Terminate CCP.
411  */
412 static void
413 ccp_close(int unit, char *reason)
414 {
415     ccp_flags_set(unit, 0, 0);
416     fsm_close(&ccp_fsm[unit], reason);
417 }
418
419 /*
420  * ccp_lowerup - we may now transmit CCP packets.
421  */
422 static void
423 ccp_lowerup(int unit)
424 {
425     fsm_lowerup(&ccp_fsm[unit]);
426 }
427
428 /*
429  * ccp_lowerdown - we may not transmit CCP packets.
430  */
431 static void
432 ccp_lowerdown(int unit)
433 {
434     fsm_lowerdown(&ccp_fsm[unit]);
435 }
436
437 /*
438  * ccp_input - process a received CCP packet.
439  */
440 static void
441 ccp_input(int unit, u_char *p, int len)
442 {
443     fsm *f = &ccp_fsm[unit];
444     int oldstate;
445
446     /*
447      * Check for a terminate-request so we can print a message.
448      */
449     oldstate = f->state;
450     fsm_input(f, p, len);
451     if (oldstate == OPENED && p[0] == TERMREQ && f->state != OPENED) {
452         notice("Compression disabled by peer.");
453 #ifdef PPP_WITH_MPPE
454         if (ccp_gotoptions[unit].mppe) {
455             error("MPPE disabled, closing LCP");
456             lcp_close(unit, "MPPE disabled by peer");
457         }
458 #endif
459     }
460
461     /*
462      * If we get a terminate-ack and we're not asking for compression,
463      * close CCP.
464      */
465     if (oldstate == REQSENT && p[0] == TERMACK
466         && !ANY_COMPRESS(ccp_gotoptions[unit]))
467         ccp_close(unit, "No compression negotiated");
468 }
469
470 /*
471  * Handle a CCP-specific code.
472  */
473 static int
474 ccp_extcode(fsm *f, int code, int id, u_char *p, int len)
475 {
476     switch (code) {
477     case CCP_RESETREQ:
478         if (f->state != OPENED)
479             break;
480         /* send a reset-ack, which the transmitter will see and
481            reset its compression state. */
482         fsm_sdata(f, CCP_RESETACK, id, NULL, 0);
483         break;
484
485     case CCP_RESETACK:
486         if (ccp_localstate[f->unit] & RACK_PENDING && id == f->reqid) {
487             ccp_localstate[f->unit] &= ~(RACK_PENDING | RREQ_REPEAT);
488             UNTIMEOUT(ccp_rack_timeout, f);
489         }
490         break;
491
492     default:
493         return 0;
494     }
495
496     return 1;
497 }
498
499 /*
500  * ccp_protrej - peer doesn't talk CCP.
501  */
502 static void
503 ccp_protrej(int unit)
504 {
505     ccp_flags_set(unit, 0, 0);
506     fsm_lowerdown(&ccp_fsm[unit]);
507
508 #ifdef PPP_WITH_MPPE
509     if (ccp_gotoptions[unit].mppe) {
510         error("MPPE required but peer negotiation failed");
511         lcp_close(unit, "MPPE required but peer negotiation failed");
512     }
513 #endif
514
515 }
516
517 /*
518  * ccp_resetci - initialize at start of negotiation.
519  */
520 static void
521 ccp_resetci(fsm *f)
522 {
523     ccp_options *go = &ccp_gotoptions[f->unit];
524     u_char opt_buf[CCP_MAX_OPTION_LENGTH];
525
526     *go = ccp_wantoptions[f->unit];
527     all_rejected[f->unit] = 0;
528
529 #ifdef PPP_WITH_MPPE
530     if (go->mppe) {
531         ccp_options *ao = &ccp_allowoptions[f->unit];
532         int auth_mschap_bits = auth_done[f->unit];
533 #ifdef PPP_WITH_EAPTLS
534         int auth_eap_bits = auth_done[f->unit];
535 #endif
536         int numbits;
537
538         /*
539          * Start with a basic sanity check: mschap[v2] auth must be in
540          * exactly one direction.  RFC 3079 says that the keys are
541          * 'derived from the credentials of the peer that initiated the call',
542          * however the PPP protocol doesn't have such a concept, and pppd
543          * cannot get this info externally.  Instead we do the best we can.
544          * NB: If MPPE is required, all other compression opts are invalid.
545          *     So, we return right away if we can't do it.
546          */
547
548         /* Leave only the mschap auth bits set */
549         auth_mschap_bits &= (CHAP_MS_WITHPEER  | CHAP_MS_PEER |
550                              CHAP_MS2_WITHPEER | CHAP_MS2_PEER);
551         /* Count the mschap auths */
552         auth_mschap_bits >>= CHAP_MS_SHIFT;
553         numbits = 0;
554         do {
555             numbits += auth_mschap_bits & 1;
556             auth_mschap_bits >>= 1;
557         } while (auth_mschap_bits);
558         if (numbits > 1) {
559             error("MPPE required, but auth done in both directions.");
560             lcp_close(f->unit, "MPPE required but not available");
561             return;
562         }
563
564 #ifdef PPP_WITH_EAPTLS
565     /*
566      * MPPE is also possible in combination with EAP-TLS.
567      * It is not possible to detect if we're doing EAP or EAP-TLS
568      * at this stage, hence we accept all forms of EAP. If TLS is
569      * not used then the MPPE keys will not be derived anyway.
570      */
571         /* Leave only the eap auth bits set */
572         auth_eap_bits &= (EAP_WITHPEER | EAP_PEER );
573
574         if ((numbits == 0) && (auth_eap_bits == 0)) {
575             error("MPPE required, but MS-CHAP[v2] nor EAP-TLS auth are performed.");
576 #else
577         if (!numbits) {
578             error("MPPE required, but MS-CHAP[v2] auth not performed.");
579 #endif
580             lcp_close(f->unit, "MPPE required but not available");
581             return;
582         }
583
584         /* A plugin (eg radius) may not have obtained key material. */
585         if (!mppe_keys_isset()) {
586             error("MPPE required, but keys are not available.  "
587                   "Possible plugin problem?");
588             lcp_close(f->unit, "MPPE required but not available");
589             return;
590         }
591
592         /* LM auth not supported for MPPE */
593         if (auth_done[f->unit] & (CHAP_MS_WITHPEER | CHAP_MS_PEER)) {
594             /* This might be noise */
595             if (go->mppe & MPPE_OPT_40) {
596                 notice("Disabling 40-bit MPPE; MS-CHAP LM not supported");
597                 go->mppe &= ~MPPE_OPT_40;
598                 ccp_wantoptions[f->unit].mppe &= ~MPPE_OPT_40;
599             }
600         }
601
602         /* Last check: can we actually negotiate something? */
603         if (!(go->mppe & (MPPE_OPT_40 | MPPE_OPT_128))) {
604             /* Could be misconfig, could be 40-bit disabled above. */
605             error("MPPE required, but both 40-bit and 128-bit disabled.");
606             lcp_close(f->unit, "MPPE required but not available");
607             return;
608         }
609
610         /* sync options */
611         ao->mppe = go->mppe;
612         /* MPPE is not compatible with other compression types */
613         ao->bsd_compress = go->bsd_compress = 0;
614         ao->predictor_1  = go->predictor_1  = 0;
615         ao->predictor_2  = go->predictor_2  = 0;
616         ao->deflate      = go->deflate      = 0;
617     }
618
619     /*
620      * Check whether the kernel knows about the various
621      * compression methods we might request.
622      */
623     if (go->mppe) {
624         opt_buf[0] = CI_MPPE;
625         opt_buf[1] = CILEN_MPPE;
626         MPPE_OPTS_TO_CI(go->mppe, &opt_buf[2]);
627         /* Key material unimportant here. */
628         if (ccp_test(f->unit, opt_buf, CILEN_MPPE + MPPE_MAX_KEY_LEN, 0) <= 0) {
629             error("MPPE required, but kernel has no support.");
630             lcp_close(f->unit, "MPPE required but not available");
631         }
632     }
633 #endif /* PPP_WITH_MPPE */
634     if (go->bsd_compress) {
635         opt_buf[0] = CI_BSD_COMPRESS;
636         opt_buf[1] = CILEN_BSD_COMPRESS;
637         opt_buf[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, BSD_MIN_BITS);
638         if (ccp_test(f->unit, opt_buf, CILEN_BSD_COMPRESS, 0) <= 0)
639             go->bsd_compress = 0;
640     }
641     if (go->deflate) {
642         if (go->deflate_correct) {
643             opt_buf[0] = CI_DEFLATE;
644             opt_buf[1] = CILEN_DEFLATE;
645             opt_buf[2] = DEFLATE_MAKE_OPT(DEFLATE_MIN_WORKS);
646             opt_buf[3] = DEFLATE_CHK_SEQUENCE;
647             if (ccp_test(f->unit, opt_buf, CILEN_DEFLATE, 0) <= 0)
648                 go->deflate_correct = 0;
649         }
650         if (go->deflate_draft) {
651             opt_buf[0] = CI_DEFLATE_DRAFT;
652             opt_buf[1] = CILEN_DEFLATE;
653             opt_buf[2] = DEFLATE_MAKE_OPT(DEFLATE_MIN_WORKS);
654             opt_buf[3] = DEFLATE_CHK_SEQUENCE;
655             if (ccp_test(f->unit, opt_buf, CILEN_DEFLATE, 0) <= 0)
656                 go->deflate_draft = 0;
657         }
658         if (!go->deflate_correct && !go->deflate_draft)
659             go->deflate = 0;
660     }
661     if (go->predictor_1) {
662         opt_buf[0] = CI_PREDICTOR_1;
663         opt_buf[1] = CILEN_PREDICTOR_1;
664         if (ccp_test(f->unit, opt_buf, CILEN_PREDICTOR_1, 0) <= 0)
665             go->predictor_1 = 0;
666     }
667     if (go->predictor_2) {
668         opt_buf[0] = CI_PREDICTOR_2;
669         opt_buf[1] = CILEN_PREDICTOR_2;
670         if (ccp_test(f->unit, opt_buf, CILEN_PREDICTOR_2, 0) <= 0)
671             go->predictor_2 = 0;
672     }
673 }
674
675 /*
676  * ccp_cilen - Return total length of our configuration info.
677  */
678 static int
679   ccp_cilen(fsm *f)
680 {
681     ccp_options *go = &ccp_gotoptions[f->unit];
682
683     return (go->bsd_compress? CILEN_BSD_COMPRESS: 0)
684         + (go->deflate && go->deflate_correct? CILEN_DEFLATE: 0)
685         + (go->deflate && go->deflate_draft? CILEN_DEFLATE: 0)
686         + (go->predictor_1? CILEN_PREDICTOR_1: 0)
687         + (go->predictor_2? CILEN_PREDICTOR_2: 0)
688         + (go->mppe? CILEN_MPPE: 0);
689 }
690
691 /*
692  * ccp_addci - put our requests in a packet.
693  */
694 static void
695   ccp_addci(fsm *f, u_char *p, int *lenp)
696 {
697     int res;
698     ccp_options *go = &ccp_gotoptions[f->unit];
699     u_char *p0 = p;
700
701     /*
702      * Add the compression types that we can receive, in decreasing
703      * preference order.  Get the kernel to allocate the first one
704      * in case it gets Acked.
705      */
706 #ifdef PPP_WITH_MPPE
707     if (go->mppe) {
708         u_char opt_buf[CILEN_MPPE + MPPE_MAX_KEY_LEN];
709
710         p[0] = opt_buf[0] = CI_MPPE;
711         p[1] = opt_buf[1] = CILEN_MPPE;
712         MPPE_OPTS_TO_CI(go->mppe, &p[2]);
713         MPPE_OPTS_TO_CI(go->mppe, &opt_buf[2]);
714         mppe_get_recv_key(&opt_buf[CILEN_MPPE], MPPE_MAX_KEY_LEN);
715         res = ccp_test(f->unit, opt_buf, CILEN_MPPE + MPPE_MAX_KEY_LEN, 0);
716         if (res > 0)
717             p += CILEN_MPPE;
718         else
719             /* This shouldn't happen, we've already tested it! */
720             lcp_close(f->unit, "MPPE required but not available in kernel");
721     }
722 #endif
723     if (go->deflate) {
724         p[0] = go->deflate_correct? CI_DEFLATE: CI_DEFLATE_DRAFT;
725         p[1] = CILEN_DEFLATE;
726         p[2] = DEFLATE_MAKE_OPT(go->deflate_size);
727         p[3] = DEFLATE_CHK_SEQUENCE;
728         if (p != p0) {
729             p += CILEN_DEFLATE;
730         } else {
731             for (;;) {
732                 if (go->deflate_size < DEFLATE_MIN_WORKS) {
733                     go->deflate = 0;
734                     break;
735                 }
736                 res = ccp_test(f->unit, p, CILEN_DEFLATE, 0);
737                 if (res > 0) {
738                     p += CILEN_DEFLATE;
739                     break;
740                 } else if (res < 0) {
741                     go->deflate = 0;
742                     break;
743                 }
744                 --go->deflate_size;
745                 p[2] = DEFLATE_MAKE_OPT(go->deflate_size);
746             }
747         }
748         if (p != p0 && go->deflate_correct && go->deflate_draft) {
749             p[0] = CI_DEFLATE_DRAFT;
750             p[1] = CILEN_DEFLATE;
751             p[2] = p[2 - CILEN_DEFLATE];
752             p[3] = DEFLATE_CHK_SEQUENCE;
753             p += CILEN_DEFLATE;
754         }
755     }
756     if (go->bsd_compress) {
757         p[0] = CI_BSD_COMPRESS;
758         p[1] = CILEN_BSD_COMPRESS;
759         p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits);
760         if (p != p0) {
761             p += CILEN_BSD_COMPRESS;    /* not the first option */
762         } else {
763             for (;;) {
764                 if (go->bsd_bits < BSD_MIN_BITS) {
765                     go->bsd_compress = 0;
766                     break;
767                 }
768                 res = ccp_test(f->unit, p, CILEN_BSD_COMPRESS, 0);
769                 if (res > 0) {
770                     p += CILEN_BSD_COMPRESS;
771                     break;
772                 } else if (res < 0) {
773                     go->bsd_compress = 0;
774                     break;
775                 }
776                 --go->bsd_bits;
777                 p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits);
778             }
779         }
780     }
781     /* XXX Should Predictor 2 be preferable to Predictor 1? */
782     if (go->predictor_1) {
783         p[0] = CI_PREDICTOR_1;
784         p[1] = CILEN_PREDICTOR_1;
785         if (p == p0 && ccp_test(f->unit, p, CILEN_PREDICTOR_1, 0) <= 0) {
786             go->predictor_1 = 0;
787         } else {
788             p += CILEN_PREDICTOR_1;
789         }
790     }
791     if (go->predictor_2) {
792         p[0] = CI_PREDICTOR_2;
793         p[1] = CILEN_PREDICTOR_2;
794         if (p == p0 && ccp_test(f->unit, p, CILEN_PREDICTOR_2, 0) <= 0) {
795             go->predictor_2 = 0;
796         } else {
797             p += CILEN_PREDICTOR_2;
798         }
799     }
800
801     go->method = (p > p0)? p0[0]: -1;
802
803     *lenp = p - p0;
804 }
805
806 /*
807  * ccp_ackci - process a received configure-ack, and return
808  * 1 iff the packet was OK.
809  */
810 static int
811   ccp_ackci(fsm *f, u_char *p, int len)
812 {
813     ccp_options *go = &ccp_gotoptions[f->unit];
814     u_char *p0 = p;
815
816 #ifdef PPP_WITH_MPPE
817     if (go->mppe) {
818         u_char opt_buf[CILEN_MPPE];
819
820         opt_buf[0] = CI_MPPE;
821         opt_buf[1] = CILEN_MPPE;
822         MPPE_OPTS_TO_CI(go->mppe, &opt_buf[2]);
823         if (len < CILEN_MPPE || memcmp(opt_buf, p, CILEN_MPPE))
824             return 0;
825         p += CILEN_MPPE;
826         len -= CILEN_MPPE;
827         /* XXX Cope with first/fast ack */
828         if (len == 0)
829             return 1;
830     }
831 #endif
832     if (go->deflate) {
833         if (len < CILEN_DEFLATE
834             || p[0] != (go->deflate_correct? CI_DEFLATE: CI_DEFLATE_DRAFT)
835             || p[1] != CILEN_DEFLATE
836             || p[2] != DEFLATE_MAKE_OPT(go->deflate_size)
837             || p[3] != DEFLATE_CHK_SEQUENCE)
838             return 0;
839         p += CILEN_DEFLATE;
840         len -= CILEN_DEFLATE;
841         /* XXX Cope with first/fast ack */
842         if (len == 0)
843             return 1;
844         if (go->deflate_correct && go->deflate_draft) {
845             if (len < CILEN_DEFLATE
846                 || p[0] != CI_DEFLATE_DRAFT
847                 || p[1] != CILEN_DEFLATE
848                 || p[2] != DEFLATE_MAKE_OPT(go->deflate_size)
849                 || p[3] != DEFLATE_CHK_SEQUENCE)
850                 return 0;
851             p += CILEN_DEFLATE;
852             len -= CILEN_DEFLATE;
853         }
854     }
855     if (go->bsd_compress) {
856         if (len < CILEN_BSD_COMPRESS
857             || p[0] != CI_BSD_COMPRESS || p[1] != CILEN_BSD_COMPRESS
858             || p[2] != BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits))
859             return 0;
860         p += CILEN_BSD_COMPRESS;
861         len -= CILEN_BSD_COMPRESS;
862         /* XXX Cope with first/fast ack */
863         if (p == p0 && len == 0)
864             return 1;
865     }
866     if (go->predictor_1) {
867         if (len < CILEN_PREDICTOR_1
868             || p[0] != CI_PREDICTOR_1 || p[1] != CILEN_PREDICTOR_1)
869             return 0;
870         p += CILEN_PREDICTOR_1;
871         len -= CILEN_PREDICTOR_1;
872         /* XXX Cope with first/fast ack */
873         if (p == p0 && len == 0)
874             return 1;
875     }
876     if (go->predictor_2) {
877         if (len < CILEN_PREDICTOR_2
878             || p[0] != CI_PREDICTOR_2 || p[1] != CILEN_PREDICTOR_2)
879             return 0;
880         p += CILEN_PREDICTOR_2;
881         len -= CILEN_PREDICTOR_2;
882         /* XXX Cope with first/fast ack */
883         if (p == p0 && len == 0)
884             return 1;
885     }
886
887     if (len != 0)
888         return 0;
889     return 1;
890 }
891
892 /*
893  * ccp_nakci - process received configure-nak.
894  * Returns 1 iff the nak was OK.
895  */
896 static int
897   ccp_nakci(fsm *f, u_char *p, int len, int treat_as_reject)
898 {
899     ccp_options *go = &ccp_gotoptions[f->unit];
900     ccp_options no;             /* options we've seen already */
901     ccp_options try;            /* options to ask for next time */
902
903     memset(&no, 0, sizeof(no));
904     try = *go;
905
906 #ifdef PPP_WITH_MPPE
907     if (go->mppe && len >= CILEN_MPPE
908         && p[0] == CI_MPPE && p[1] == CILEN_MPPE) {
909         no.mppe = 1;
910         /*
911          * Peer wants us to use a different strength or other setting.
912          * Fail if we aren't willing to use his suggestion.
913          */
914         MPPE_CI_TO_OPTS(&p[2], try.mppe);
915         if ((try.mppe & MPPE_OPT_STATEFUL) && refuse_mppe_stateful) {
916             error("Refusing MPPE stateful mode offered by peer");
917             try.mppe = 0;
918         } else if (((go->mppe | MPPE_OPT_STATEFUL) & try.mppe) != try.mppe) {
919             /* Peer must have set options we didn't request (suggest) */
920             try.mppe = 0;
921         }
922
923         if (!try.mppe) {
924             error("MPPE required but peer negotiation failed");
925             lcp_close(f->unit, "MPPE required but peer negotiation failed");
926         }
927     }
928 #endif /* PPP_WITH_MPPE */
929     if (go->deflate && len >= CILEN_DEFLATE
930         && p[0] == (go->deflate_correct? CI_DEFLATE: CI_DEFLATE_DRAFT)
931         && p[1] == CILEN_DEFLATE) {
932         no.deflate = 1;
933         /*
934          * Peer wants us to use a different code size or something.
935          * Stop asking for Deflate if we don't understand his suggestion.
936          */
937         if (DEFLATE_METHOD(p[2]) != DEFLATE_METHOD_VAL
938             || DEFLATE_SIZE(p[2]) < DEFLATE_MIN_WORKS
939             || p[3] != DEFLATE_CHK_SEQUENCE)
940             try.deflate = 0;
941         else if (DEFLATE_SIZE(p[2]) < go->deflate_size)
942             try.deflate_size = DEFLATE_SIZE(p[2]);
943         p += CILEN_DEFLATE;
944         len -= CILEN_DEFLATE;
945         if (go->deflate_correct && go->deflate_draft
946             && len >= CILEN_DEFLATE && p[0] == CI_DEFLATE_DRAFT
947             && p[1] == CILEN_DEFLATE) {
948             p += CILEN_DEFLATE;
949             len -= CILEN_DEFLATE;
950         }
951     }
952
953     if (go->bsd_compress && len >= CILEN_BSD_COMPRESS
954         && p[0] == CI_BSD_COMPRESS && p[1] == CILEN_BSD_COMPRESS) {
955         no.bsd_compress = 1;
956         /*
957          * Peer wants us to use a different number of bits
958          * or a different version.
959          */
960         if (BSD_VERSION(p[2]) != BSD_CURRENT_VERSION)
961             try.bsd_compress = 0;
962         else if (BSD_NBITS(p[2]) < go->bsd_bits)
963             try.bsd_bits = BSD_NBITS(p[2]);
964         p += CILEN_BSD_COMPRESS;
965         len -= CILEN_BSD_COMPRESS;
966     }
967
968     /*
969      * Predictor-1 and 2 have no options, so they can't be Naked.
970      *
971      * There may be remaining options but we ignore them.
972      */
973
974     if (f->state != OPENED)
975         *go = try;
976     return 1;
977 }
978
979 /*
980  * ccp_rejci - reject some of our suggested compression methods.
981  */
982 static int
983 ccp_rejci(fsm *f, u_char *p, int len)
984 {
985     ccp_options *go = &ccp_gotoptions[f->unit];
986     ccp_options try;            /* options to request next time */
987
988     try = *go;
989
990     /*
991      * Cope with empty configure-rejects by ceasing to send
992      * configure-requests.
993      */
994     if (len == 0 && all_rejected[f->unit])
995         return -1;
996
997 #ifdef PPP_WITH_MPPE
998     if (go->mppe && len >= CILEN_MPPE
999         && p[0] == CI_MPPE && p[1] == CILEN_MPPE) {
1000         error("MPPE required but peer refused");
1001         lcp_close(f->unit, "MPPE required but peer refused");
1002         p += CILEN_MPPE;
1003         len -= CILEN_MPPE;
1004     }
1005 #endif
1006     if (go->deflate_correct && len >= CILEN_DEFLATE
1007         && p[0] == CI_DEFLATE && p[1] == CILEN_DEFLATE) {
1008         if (p[2] != DEFLATE_MAKE_OPT(go->deflate_size)
1009             || p[3] != DEFLATE_CHK_SEQUENCE)
1010             return 0;           /* Rej is bad */
1011         try.deflate_correct = 0;
1012         p += CILEN_DEFLATE;
1013         len -= CILEN_DEFLATE;
1014     }
1015     if (go->deflate_draft && len >= CILEN_DEFLATE
1016         && p[0] == CI_DEFLATE_DRAFT && p[1] == CILEN_DEFLATE) {
1017         if (p[2] != DEFLATE_MAKE_OPT(go->deflate_size)
1018             || p[3] != DEFLATE_CHK_SEQUENCE)
1019             return 0;           /* Rej is bad */
1020         try.deflate_draft = 0;
1021         p += CILEN_DEFLATE;
1022         len -= CILEN_DEFLATE;
1023     }
1024     if (!try.deflate_correct && !try.deflate_draft)
1025         try.deflate = 0;
1026     if (go->bsd_compress && len >= CILEN_BSD_COMPRESS
1027         && p[0] == CI_BSD_COMPRESS && p[1] == CILEN_BSD_COMPRESS) {
1028         if (p[2] != BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits))
1029             return 0;
1030         try.bsd_compress = 0;
1031         p += CILEN_BSD_COMPRESS;
1032         len -= CILEN_BSD_COMPRESS;
1033     }
1034     if (go->predictor_1 && len >= CILEN_PREDICTOR_1
1035         && p[0] == CI_PREDICTOR_1 && p[1] == CILEN_PREDICTOR_1) {
1036         try.predictor_1 = 0;
1037         p += CILEN_PREDICTOR_1;
1038         len -= CILEN_PREDICTOR_1;
1039     }
1040     if (go->predictor_2 && len >= CILEN_PREDICTOR_2
1041         && p[0] == CI_PREDICTOR_2 && p[1] == CILEN_PREDICTOR_2) {
1042         try.predictor_2 = 0;
1043         p += CILEN_PREDICTOR_2;
1044         len -= CILEN_PREDICTOR_2;
1045     }
1046
1047     if (len != 0)
1048         return 0;
1049
1050     if (f->state != OPENED)
1051         *go = try;
1052
1053     return 1;
1054 }
1055
1056 /*
1057  * ccp_reqci - processed a received configure-request.
1058  * Returns CONFACK, CONFNAK or CONFREJ and the packet modified
1059  * appropriately.
1060  */
1061 static int
1062 ccp_reqci(fsm *f, u_char *p, int *lenp, int dont_nak)
1063 {
1064     int ret, newret, res;
1065     u_char *p0, *retp;
1066     int len, clen, type, nb;
1067     ccp_options *ho = &ccp_hisoptions[f->unit];
1068     ccp_options *ao = &ccp_allowoptions[f->unit];
1069 #ifdef PPP_WITH_MPPE
1070     bool rej_for_ci_mppe = 1;   /* Are we rejecting based on a bad/missing */
1071                                 /* CI_MPPE, or due to other options?       */
1072 #endif
1073
1074     ret = CONFACK;
1075     retp = p0 = p;
1076     len = *lenp;
1077
1078     memset(ho, 0, sizeof(ccp_options));
1079     ho->method = (len > 0)? p[0]: -1;
1080
1081     while (len > 0) {
1082         newret = CONFACK;
1083         if (len < 2 || p[1] < 2 || p[1] > len) {
1084             /* length is bad */
1085             clen = len;
1086             newret = CONFREJ;
1087
1088         } else {
1089             type = p[0];
1090             clen = p[1];
1091
1092             switch (type) {
1093 #ifdef PPP_WITH_MPPE
1094             case CI_MPPE:
1095                 if (!ao->mppe || clen != CILEN_MPPE) {
1096                     newret = CONFREJ;
1097                     break;
1098                 }
1099                 MPPE_CI_TO_OPTS(&p[2], ho->mppe);
1100
1101                 /* Nak if anything unsupported or unknown are set. */
1102                 if (ho->mppe & MPPE_OPT_UNSUPPORTED) {
1103                     newret = CONFNAK;
1104                     ho->mppe &= ~MPPE_OPT_UNSUPPORTED;
1105                 }
1106                 if (ho->mppe & MPPE_OPT_UNKNOWN) {
1107                     newret = CONFNAK;
1108                     ho->mppe &= ~MPPE_OPT_UNKNOWN;
1109                 }
1110
1111                 /* Check state opt */
1112                 if (ho->mppe & MPPE_OPT_STATEFUL) {
1113                     /*
1114                      * We can Nak and request stateless, but it's a
1115                      * lot easier to just assume the peer will request
1116                      * it if he can do it; stateful mode is bad over
1117                      * the Internet -- which is where we expect MPPE.
1118                      */
1119                    if (refuse_mppe_stateful) {
1120                         error("Refusing MPPE stateful mode offered by peer");
1121                         newret = CONFREJ;
1122                         break;
1123                     }
1124                 }
1125
1126                 /* Find out which of {S,L} are set. */
1127                 if ((ho->mppe & MPPE_OPT_128)
1128                      && (ho->mppe & MPPE_OPT_40)) {
1129                     /* Both are set, negotiate the strongest. */
1130                     newret = CONFNAK;
1131                     if (ao->mppe & MPPE_OPT_128)
1132                         ho->mppe &= ~MPPE_OPT_40;
1133                     else if (ao->mppe & MPPE_OPT_40)
1134                         ho->mppe &= ~MPPE_OPT_128;
1135                     else {
1136                         newret = CONFREJ;
1137                         break;
1138                     }
1139                 } else if (ho->mppe & MPPE_OPT_128) {
1140                     if (!(ao->mppe & MPPE_OPT_128)) {
1141                         newret = CONFREJ;
1142                         break;
1143                     }
1144                 } else if (ho->mppe & MPPE_OPT_40) {
1145                     if (!(ao->mppe & MPPE_OPT_40)) {
1146                         newret = CONFREJ;
1147                         break;
1148                     }
1149                 } else {
1150                     /* Neither are set. */
1151                     /* We cannot accept this.  */
1152                     newret = CONFNAK;
1153                     /* Give the peer our idea of what can be used,
1154                        so it can choose and confirm */
1155                     ho->mppe = ao->mppe;
1156                 }
1157
1158                 /* rebuild the opts */
1159                 MPPE_OPTS_TO_CI(ho->mppe, &p[2]);
1160                 if (newret == CONFACK) {
1161                     u_char opt_buf[CILEN_MPPE + MPPE_MAX_KEY_LEN];
1162                     int mtu;
1163
1164                     BCOPY(p, opt_buf, CILEN_MPPE);
1165                     mppe_get_send_key(&opt_buf[CILEN_MPPE], MPPE_MAX_KEY_LEN);
1166                     if (ccp_test(f->unit, opt_buf,
1167                                  CILEN_MPPE + MPPE_MAX_KEY_LEN, 1) <= 0) {
1168                         /* This shouldn't happen, we've already tested it! */
1169                         error("MPPE required, but kernel has no support.");
1170                         lcp_close(f->unit, "MPPE required but not available");
1171                         newret = CONFREJ;
1172                         break;
1173                     }
1174                     /*
1175                      * We need to decrease the interface MTU by MPPE_PAD
1176                      * because MPPE frames **grow**.  The kernel [must]
1177                      * allocate MPPE_PAD extra bytes in xmit buffers.
1178                      */
1179                     mtu = ppp_get_mtu(f->unit);
1180                     if (mtu)
1181                         ppp_set_mtu(f->unit, mtu - MPPE_PAD);
1182                     else
1183                         newret = CONFREJ;
1184                 }
1185
1186                 /*
1187                  * We have accepted MPPE or are willing to negotiate
1188                  * MPPE parameters.  A CONFREJ is due to subsequent
1189                  * (non-MPPE) processing.
1190                  */
1191                 rej_for_ci_mppe = 0;
1192                 break;
1193 #endif /* PPP_WITH_MPPE */
1194             case CI_DEFLATE:
1195             case CI_DEFLATE_DRAFT:
1196                 if (!ao->deflate || clen != CILEN_DEFLATE
1197                     || (!ao->deflate_correct && type == CI_DEFLATE)
1198                     || (!ao->deflate_draft && type == CI_DEFLATE_DRAFT)) {
1199                     newret = CONFREJ;
1200                     break;
1201                 }
1202
1203                 ho->deflate = 1;
1204                 ho->deflate_size = nb = DEFLATE_SIZE(p[2]);
1205                 if (DEFLATE_METHOD(p[2]) != DEFLATE_METHOD_VAL
1206                     || p[3] != DEFLATE_CHK_SEQUENCE
1207                     || nb > ao->deflate_size || nb < DEFLATE_MIN_WORKS) {
1208                     newret = CONFNAK;
1209                     if (!dont_nak) {
1210                         p[2] = DEFLATE_MAKE_OPT(ao->deflate_size);
1211                         p[3] = DEFLATE_CHK_SEQUENCE;
1212                         /* fall through to test this #bits below */
1213                     } else
1214                         break;
1215                 }
1216
1217                 /*
1218                  * Check whether we can do Deflate with the window
1219                  * size they want.  If the window is too big, reduce
1220                  * it until the kernel can cope and nak with that.
1221                  * We only check this for the first option.
1222                  */
1223                 if (p == p0) {
1224                     for (;;) {
1225                         res = ccp_test(f->unit, p, CILEN_DEFLATE, 1);
1226                         if (res > 0)
1227                             break;              /* it's OK now */
1228                         if (res < 0 || nb == DEFLATE_MIN_WORKS || dont_nak) {
1229                             newret = CONFREJ;
1230                             p[2] = DEFLATE_MAKE_OPT(ho->deflate_size);
1231                             break;
1232                         }
1233                         newret = CONFNAK;
1234                         --nb;
1235                         p[2] = DEFLATE_MAKE_OPT(nb);
1236                     }
1237                 }
1238                 break;
1239
1240             case CI_BSD_COMPRESS:
1241                 if (!ao->bsd_compress || clen != CILEN_BSD_COMPRESS) {
1242                     newret = CONFREJ;
1243                     break;
1244                 }
1245
1246                 ho->bsd_compress = 1;
1247                 ho->bsd_bits = nb = BSD_NBITS(p[2]);
1248                 if (BSD_VERSION(p[2]) != BSD_CURRENT_VERSION
1249                     || nb > ao->bsd_bits || nb < BSD_MIN_BITS) {
1250                     newret = CONFNAK;
1251                     if (!dont_nak) {
1252                         p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, ao->bsd_bits);
1253                         /* fall through to test this #bits below */
1254                     } else
1255                         break;
1256                 }
1257
1258                 /*
1259                  * Check whether we can do BSD-Compress with the code
1260                  * size they want.  If the code size is too big, reduce
1261                  * it until the kernel can cope and nak with that.
1262                  * We only check this for the first option.
1263                  */
1264                 if (p == p0) {
1265                     for (;;) {
1266                         res = ccp_test(f->unit, p, CILEN_BSD_COMPRESS, 1);
1267                         if (res > 0)
1268                             break;
1269                         if (res < 0 || nb == BSD_MIN_BITS || dont_nak) {
1270                             newret = CONFREJ;
1271                             p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION,
1272                                                 ho->bsd_bits);
1273                             break;
1274                         }
1275                         newret = CONFNAK;
1276                         --nb;
1277                         p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, nb);
1278                     }
1279                 }
1280                 break;
1281
1282             case CI_PREDICTOR_1:
1283                 if (!ao->predictor_1 || clen != CILEN_PREDICTOR_1) {
1284                     newret = CONFREJ;
1285                     break;
1286                 }
1287
1288                 ho->predictor_1 = 1;
1289                 if (p == p0
1290                     && ccp_test(f->unit, p, CILEN_PREDICTOR_1, 1) <= 0) {
1291                     newret = CONFREJ;
1292                 }
1293                 break;
1294
1295             case CI_PREDICTOR_2:
1296                 if (!ao->predictor_2 || clen != CILEN_PREDICTOR_2) {
1297                     newret = CONFREJ;
1298                     break;
1299                 }
1300
1301                 ho->predictor_2 = 1;
1302                 if (p == p0
1303                     && ccp_test(f->unit, p, CILEN_PREDICTOR_2, 1) <= 0) {
1304                     newret = CONFREJ;
1305                 }
1306                 break;
1307
1308             default:
1309                 newret = CONFREJ;
1310             }
1311         }
1312
1313         if (newret == CONFNAK && dont_nak)
1314             newret = CONFREJ;
1315         if (!(newret == CONFACK || (newret == CONFNAK && ret == CONFREJ))) {
1316             /* we're returning this option */
1317             if (newret == CONFREJ && ret == CONFNAK)
1318                 retp = p0;
1319             ret = newret;
1320             if (p != retp)
1321                 BCOPY(p, retp, clen);
1322             retp += clen;
1323         }
1324
1325         p += clen;
1326         len -= clen;
1327     }
1328
1329     if (ret != CONFACK) {
1330         if (ret == CONFREJ && *lenp == retp - p0)
1331             all_rejected[f->unit] = 1;
1332         else
1333             *lenp = retp - p0;
1334     }
1335 #ifdef PPP_WITH_MPPE
1336     if (ret == CONFREJ && ao->mppe && rej_for_ci_mppe) {
1337         error("MPPE required but peer negotiation failed");
1338         lcp_close(f->unit, "MPPE required but peer negotiation failed");
1339     }
1340 #endif
1341     return ret;
1342 }
1343
1344 /*
1345  * Make a string name for a compression method (or 2).
1346  */
1347 static char *
1348 method_name(ccp_options *opt, ccp_options *opt2)
1349 {
1350     static char result[64];
1351
1352     if (!ANY_COMPRESS(*opt))
1353         return "(none)";
1354     switch (opt->method) {
1355 #ifdef PPP_WITH_MPPE
1356     case CI_MPPE:
1357     {
1358         char *p = result;
1359         char *q = result + sizeof(result); /* 1 past result */
1360
1361         slprintf(p, q - p, "MPPE ");
1362         p += 5;
1363         if (opt->mppe & MPPE_OPT_128) {
1364             slprintf(p, q - p, "128-bit ");
1365             p += 8;
1366         }
1367         if (opt->mppe & MPPE_OPT_40) {
1368             slprintf(p, q - p, "40-bit ");
1369             p += 7;
1370         }
1371         if (opt->mppe & MPPE_OPT_STATEFUL)
1372             slprintf(p, q - p, "stateful");
1373         else
1374             slprintf(p, q - p, "stateless");
1375
1376         break;
1377     }
1378 #endif
1379     case CI_DEFLATE:
1380     case CI_DEFLATE_DRAFT:
1381         if (opt2 != NULL && opt2->deflate_size != opt->deflate_size)
1382             slprintf(result, sizeof(result), "Deflate%s (%d/%d)",
1383                      (opt->method == CI_DEFLATE_DRAFT? "(old#)": ""),
1384                      opt->deflate_size, opt2->deflate_size);
1385         else
1386             slprintf(result, sizeof(result), "Deflate%s (%d)",
1387                      (opt->method == CI_DEFLATE_DRAFT? "(old#)": ""),
1388                      opt->deflate_size);
1389         break;
1390     case CI_BSD_COMPRESS:
1391         if (opt2 != NULL && opt2->bsd_bits != opt->bsd_bits)
1392             slprintf(result, sizeof(result), "BSD-Compress (%d/%d)",
1393                      opt->bsd_bits, opt2->bsd_bits);
1394         else
1395             slprintf(result, sizeof(result), "BSD-Compress (%d)",
1396                      opt->bsd_bits);
1397         break;
1398     case CI_PREDICTOR_1:
1399         return "Predictor 1";
1400     case CI_PREDICTOR_2:
1401         return "Predictor 2";
1402     default:
1403         slprintf(result, sizeof(result), "Method %d", opt->method);
1404     }
1405     return result;
1406 }
1407
1408 /*
1409  * CCP has come up - inform the kernel driver and log a message.
1410  */
1411 static void
1412 ccp_up(fsm *f)
1413 {
1414     ccp_options *go = &ccp_gotoptions[f->unit];
1415     ccp_options *ho = &ccp_hisoptions[f->unit];
1416     char method1[64];
1417
1418     ccp_flags_set(f->unit, 1, 1);
1419     if (ANY_COMPRESS(*go)) {
1420         if (ANY_COMPRESS(*ho)) {
1421             if (go->method == ho->method) {
1422                 notice("%s compression enabled", method_name(go, ho));
1423             } else {
1424                 strlcpy(method1, method_name(go, NULL), sizeof(method1));
1425                 notice("%s / %s compression enabled",
1426                        method1, method_name(ho, NULL));
1427             }
1428         } else
1429             notice("%s receive compression enabled", method_name(go, NULL));
1430     } else if (ANY_COMPRESS(*ho))
1431         notice("%s transmit compression enabled", method_name(ho, NULL));
1432 #ifdef PPP_WITH_MPPE
1433     if (go->mppe) {
1434         mppe_clear_keys();
1435         continue_networks(f->unit);             /* Bring up IP et al */
1436     }
1437 #endif
1438 }
1439
1440 /*
1441  * CCP has gone down - inform the kernel driver.
1442  */
1443 static void
1444 ccp_down(fsm *f)
1445 {
1446     if (ccp_localstate[f->unit] & RACK_PENDING)
1447         UNTIMEOUT(ccp_rack_timeout, f);
1448     ccp_localstate[f->unit] = 0;
1449     ccp_flags_set(f->unit, 1, 0);
1450 #ifdef PPP_WITH_MPPE
1451     if (ccp_gotoptions[f->unit].mppe) {
1452         ccp_gotoptions[f->unit].mppe = 0;
1453         if (lcp_fsm[f->unit].state == OPENED) {
1454             /* If LCP is not already going down, make sure it does. */
1455             error("MPPE disabled");
1456             lcp_close(f->unit, "MPPE disabled");
1457         }
1458     }
1459 #endif
1460 }
1461
1462 /*
1463  * Print the contents of a CCP packet.
1464  */
1465 static char *ccp_codenames[] = {
1466     "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1467     "TermReq", "TermAck", "CodeRej",
1468     NULL, NULL, NULL, NULL, NULL, NULL,
1469     "ResetReq", "ResetAck",
1470 };
1471
1472 static int
1473 ccp_printpkt(u_char *p, int plen,
1474              void (*printer) (void *, char *, ...), void *arg)
1475 {
1476     u_char *p0, *optend;
1477     int code, id, len;
1478     int optlen;
1479
1480     p0 = p;
1481     if (plen < HEADERLEN)
1482         return 0;
1483     code = p[0];
1484     id = p[1];
1485     len = (p[2] << 8) + p[3];
1486     if (len < HEADERLEN || len > plen)
1487         return 0;
1488
1489     if (code >= 1 && code <= sizeof(ccp_codenames) / sizeof(char *)
1490         && ccp_codenames[code-1] != NULL)
1491         printer(arg, " %s", ccp_codenames[code-1]);
1492     else
1493         printer(arg, " code=0x%x", code);
1494     printer(arg, " id=0x%x", id);
1495     len -= HEADERLEN;
1496     p += HEADERLEN;
1497
1498     switch (code) {
1499     case CONFREQ:
1500     case CONFACK:
1501     case CONFNAK:
1502     case CONFREJ:
1503         /* print list of possible compression methods */
1504         while (len >= 2) {
1505             code = p[0];
1506             optlen = p[1];
1507             if (optlen < 2 || optlen > len)
1508                 break;
1509             printer(arg, " <");
1510             len -= optlen;
1511             optend = p + optlen;
1512             switch (code) {
1513 #ifdef PPP_WITH_MPPE
1514             case CI_MPPE:
1515                 if (optlen >= CILEN_MPPE) {
1516                     u_char mppe_opts;
1517
1518                     MPPE_CI_TO_OPTS(&p[2], mppe_opts);
1519                     printer(arg, "mppe %s %s %s %s %s %s%s",
1520                             (p[2] & MPPE_H_BIT)? "+H": "-H",
1521                             (p[5] & MPPE_M_BIT)? "+M": "-M",
1522                             (p[5] & MPPE_S_BIT)? "+S": "-S",
1523                             (p[5] & MPPE_L_BIT)? "+L": "-L",
1524                             (p[5] & MPPE_D_BIT)? "+D": "-D",
1525                             (p[5] & MPPE_C_BIT)? "+C": "-C",
1526                             (mppe_opts & MPPE_OPT_UNKNOWN)? " +U": "");
1527                     if (mppe_opts & MPPE_OPT_UNKNOWN)
1528                         printer(arg, " (%.2x %.2x %.2x %.2x)",
1529                                 p[2], p[3], p[4], p[5]);
1530                     p += CILEN_MPPE;
1531                 }
1532                 break;
1533 #endif
1534             case CI_DEFLATE:
1535             case CI_DEFLATE_DRAFT:
1536                 if (optlen >= CILEN_DEFLATE) {
1537                     printer(arg, "deflate%s %d",
1538                             (code == CI_DEFLATE_DRAFT? "(old#)": ""),
1539                             DEFLATE_SIZE(p[2]));
1540                     if (DEFLATE_METHOD(p[2]) != DEFLATE_METHOD_VAL)
1541                         printer(arg, " method %d", DEFLATE_METHOD(p[2]));
1542                     if (p[3] != DEFLATE_CHK_SEQUENCE)
1543                         printer(arg, " check %d", p[3]);
1544                     p += CILEN_DEFLATE;
1545                 }
1546                 break;
1547             case CI_BSD_COMPRESS:
1548                 if (optlen >= CILEN_BSD_COMPRESS) {
1549                     printer(arg, "bsd v%d %d", BSD_VERSION(p[2]),
1550                             BSD_NBITS(p[2]));
1551                     p += CILEN_BSD_COMPRESS;
1552                 }
1553                 break;
1554             case CI_PREDICTOR_1:
1555                 if (optlen >= CILEN_PREDICTOR_1) {
1556                     printer(arg, "predictor 1");
1557                     p += CILEN_PREDICTOR_1;
1558                 }
1559                 break;
1560             case CI_PREDICTOR_2:
1561                 if (optlen >= CILEN_PREDICTOR_2) {
1562                     printer(arg, "predictor 2");
1563                     p += CILEN_PREDICTOR_2;
1564                 }
1565                 break;
1566             }
1567             while (p < optend)
1568                 printer(arg, " %.2x", *p++);
1569             printer(arg, ">");
1570         }
1571         break;
1572
1573     case TERMACK:
1574     case TERMREQ:
1575         if (len > 0 && *p >= ' ' && *p < 0x7f) {
1576             print_string((char *)p, len, printer, arg);
1577             p += len;
1578             len = 0;
1579         }
1580         break;
1581     }
1582
1583     /* dump out the rest of the packet in hex */
1584     while (--len >= 0)
1585         printer(arg, " %.2x", *p++);
1586
1587     return p - p0;
1588 }
1589
1590 /*
1591  * We have received a packet that the decompressor failed to
1592  * decompress.  Here we would expect to issue a reset-request, but
1593  * Motorola has a patent on resetting the compressor as a result of
1594  * detecting an error in the decompressed data after decompression.
1595  * (See US patent 5,130,993; international patent publication number
1596  * WO 91/10289; Australian patent 73296/91.)
1597  *
1598  * So we ask the kernel whether the error was detected after
1599  * decompression; if it was, we take CCP down, thus disabling
1600  * compression :-(, otherwise we issue the reset-request.
1601  */
1602 static void
1603 ccp_datainput(int unit, u_char *pkt, int len)
1604 {
1605     fsm *f;
1606
1607     f = &ccp_fsm[unit];
1608     if (f->state == OPENED) {
1609         if (ccp_fatal_error(unit)) {
1610             /*
1611              * Disable compression by taking CCP down.
1612              */
1613             error("Lost compression sync: disabling compression");
1614             ccp_close(unit, "Lost compression sync");
1615 #ifdef PPP_WITH_MPPE
1616             /*
1617              * If we were doing MPPE, we must also take the link down.
1618              */
1619             if (ccp_gotoptions[unit].mppe) {
1620                 error("Too many MPPE errors, closing LCP");
1621                 lcp_close(unit, "Too many MPPE errors");
1622             }
1623 #endif
1624         } else {
1625             /*
1626              * Send a reset-request to reset the peer's compressor.
1627              * We don't do that if we are still waiting for an
1628              * acknowledgement to a previous reset-request.
1629              */
1630             if (!(ccp_localstate[f->unit] & RACK_PENDING)) {
1631                 fsm_sdata(f, CCP_RESETREQ, f->reqid = ++f->id, NULL, 0);
1632                 TIMEOUT(ccp_rack_timeout, f, RACKTIMEOUT);
1633                 ccp_localstate[f->unit] |= RACK_PENDING;
1634             } else
1635                 ccp_localstate[f->unit] |= RREQ_REPEAT;
1636         }
1637     }
1638 }
1639
1640 /*
1641  * Timeout waiting for reset-ack.
1642  */
1643 static void
1644 ccp_rack_timeout(void *arg)
1645 {
1646     fsm *f = arg;
1647
1648     if (f->state == OPENED && ccp_localstate[f->unit] & RREQ_REPEAT) {
1649         fsm_sdata(f, CCP_RESETREQ, f->reqid, NULL, 0);
1650         TIMEOUT(ccp_rack_timeout, f, RACKTIMEOUT);
1651         ccp_localstate[f->unit] &= ~RREQ_REPEAT;
1652     } else
1653         ccp_localstate[f->unit] &= ~RACK_PENDING;
1654 }
1655