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