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