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