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