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