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