]> git.ozlabs.org Git - ppp.git/blob - pppd/ccp.c
First large MPPE patch 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.34 2002/04/02 13:54:59 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_xx_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
515 /*
516  * ccp_resetci - initialize at start of negotiation.
517  */
518 static void
519 ccp_resetci(f)
520     fsm *f;
521 {
522     ccp_options *go = &ccp_gotoptions[f->unit];
523     u_char opt_buf[CCP_MAX_OPTION_LENGTH];
524
525     *go = ccp_wantoptions[f->unit];
526     all_rejected[f->unit] = 0;
527
528 #ifdef MPPE
529     if (go->mppe) {
530         ccp_options *ao = &ccp_allowoptions[f->unit];
531         int auth_mschap_bits = auth_done[f->unit];
532         int numbits;
533
534         /*
535          * Start with a basic sanity check: mschap[v2] auth must be in
536          * exactly one direction.  RFC 3079 says that the keys are
537          * 'derived from the credentials of the peer that initiated the call',
538          * however the PPP protocol doesn't have such a concept, and pppd
539          * cannot get this info externally.  Instead we do the best we can.
540          * NB: If MPPE is required, all other compression opts are invalid.
541          *     So, we return right away if we can't do it.
542          */
543
544         /* Leave only the mschap auth bits set */
545         auth_mschap_bits &= ~(PAP_WITHPEER | PAP_PEER |
546                               CHAP_WITHPEER | CHAP_PEER |
547                               CHAP_MD5_WITHPEER | CHAP_MD5_PEER);
548         /* Count the mschap auths */
549         numbits = 0;
550         do {
551             numbits += auth_mschap_bits & 1;
552             auth_mschap_bits >>= 1;
553         } while (auth_mschap_bits);
554         if (numbits > 1) {
555             error("MPPE required, but auth done in both directions.");
556             lcp_close(f->unit, "MPPE required but not available");
557             return;
558         }
559         if (!numbits) {
560             error("MPPE required, but MS-CHAP[v2] auth not performed.");
561             lcp_close(f->unit, "MPPE required but not available");
562             return;
563         }
564
565         /* LM auth not supported for MPPE */
566         if (auth_done[f->unit] & (CHAP_MS_WITHPEER | CHAP_MS_PEER)) {
567             /* This might be noise */
568             if (go->mppe & MPPE_OPT_40) {
569                 notice("Disabling 40-bit MPPE; MS-CHAP LM not supported");
570                 go->mppe &= ~MPPE_OPT_40;
571             }
572         }
573
574         /* Last check: can we actually negotiate something? */
575         if (!(go->mppe & (MPPE_OPT_40 | MPPE_OPT_128))) {
576             /* Could be misconfig, could be 40-bit disabled above. */
577             error("MPPE required, but both 40-bit and 128-bit disabled.");
578             lcp_close(f->unit, "MPPE required but not available");
579             return;
580         }
581
582         /* sync options */
583         ao->mppe = go->mppe;
584         /* MPPE is not compatible with other compression types */
585         ao->bsd_compress = go->bsd_compress = 0;
586         ao->predictor_1  = go->predictor_1  = 0;
587         ao->predictor_2  = go->predictor_2  = 0;
588         ao->deflate      = go->deflate      = 0;
589     }
590 #endif /* MPPE */
591
592     /*
593      * Check whether the kernel knows about the various
594      * compression methods we might request.
595      */
596 #ifdef MPPE
597     if (go->mppe) {
598         opt_buf[0] = CI_MPPE;
599         opt_buf[1] = CILEN_MPPE;
600         MPPE_OPTS_TO_CI(go->mppe, &opt_buf[2]);
601         /* Key material unimportant here. */
602         if (ccp_test(f->unit, opt_buf, CILEN_MPPE + MPPE_MAX_KEY_LEN, 0) <= 0) {
603             error("MPPE required, but kernel has no support.");
604             lcp_close(f->unit, "MPPE required but not available");
605         }
606     }
607 #endif
608     if (go->bsd_compress) {
609         opt_buf[0] = CI_BSD_COMPRESS;
610         opt_buf[1] = CILEN_BSD_COMPRESS;
611         opt_buf[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, BSD_MIN_BITS);
612         if (ccp_test(f->unit, opt_buf, CILEN_BSD_COMPRESS, 0) <= 0)
613             go->bsd_compress = 0;
614     }
615     if (go->deflate) {
616         if (go->deflate_correct) {
617             opt_buf[0] = CI_DEFLATE;
618             opt_buf[1] = CILEN_DEFLATE;
619             opt_buf[2] = DEFLATE_MAKE_OPT(DEFLATE_MIN_WORKS);
620             opt_buf[3] = DEFLATE_CHK_SEQUENCE;
621             if (ccp_test(f->unit, opt_buf, CILEN_DEFLATE, 0) <= 0)
622                 go->deflate_correct = 0;
623         }
624         if (go->deflate_draft) {
625             opt_buf[0] = CI_DEFLATE_DRAFT;
626             opt_buf[1] = CILEN_DEFLATE;
627             opt_buf[2] = DEFLATE_MAKE_OPT(DEFLATE_MIN_WORKS);
628             opt_buf[3] = DEFLATE_CHK_SEQUENCE;
629             if (ccp_test(f->unit, opt_buf, CILEN_DEFLATE, 0) <= 0)
630                 go->deflate_draft = 0;
631         }
632         if (!go->deflate_correct && !go->deflate_draft)
633             go->deflate = 0;
634     }
635     if (go->predictor_1) {
636         opt_buf[0] = CI_PREDICTOR_1;
637         opt_buf[1] = CILEN_PREDICTOR_1;
638         if (ccp_test(f->unit, opt_buf, CILEN_PREDICTOR_1, 0) <= 0)
639             go->predictor_1 = 0;
640     }
641     if (go->predictor_2) {
642         opt_buf[0] = CI_PREDICTOR_2;
643         opt_buf[1] = CILEN_PREDICTOR_2;
644         if (ccp_test(f->unit, opt_buf, CILEN_PREDICTOR_2, 0) <= 0)
645             go->predictor_2 = 0;
646     }
647 }
648
649 /*
650  * ccp_cilen - Return total length of our configuration info.
651  */
652 static int
653 ccp_cilen(f)
654     fsm *f;
655 {
656     ccp_options *go = &ccp_gotoptions[f->unit];
657
658     return (go->bsd_compress? CILEN_BSD_COMPRESS: 0)
659         + (go->deflate? CILEN_DEFLATE: 0)
660         + (go->predictor_1? CILEN_PREDICTOR_1: 0)
661         + (go->predictor_2? CILEN_PREDICTOR_2: 0)
662         + (go->mppe? CILEN_MPPE: 0);
663 }
664
665 /*
666  * ccp_addci - put our requests in a packet.
667  */
668 static void
669 ccp_addci(f, p, lenp)
670     fsm *f;
671     u_char *p;
672     int *lenp;
673 {
674     int res;
675     ccp_options *go = &ccp_gotoptions[f->unit];
676     u_char *p0 = p;
677
678     /*
679      * Add the compression types that we can receive, in decreasing
680      * preference order.
681      */
682 #ifdef MPPE
683     if (go->mppe) {
684         u_char opt_buf[CILEN_MPPE + MPPE_MAX_KEY_LEN];
685
686         p[0] = opt_buf[0] = CI_MPPE;
687         p[1] = opt_buf[1] = CILEN_MPPE;
688         MPPE_OPTS_TO_CI(go->mppe, &p[2]);
689         MPPE_OPTS_TO_CI(go->mppe, &opt_buf[2]);
690         BCOPY(mppe_recv_key, &opt_buf[CILEN_MPPE], MPPE_MAX_KEY_LEN);
691         res = ccp_test(f->unit, opt_buf, CILEN_MPPE + MPPE_MAX_KEY_LEN, 0);
692         if (res > 0)
693             p += CILEN_MPPE;
694         else
695             /* This shouldn't happen, we've already tested it! */
696             lcp_close(f->unit, "MPPE required but not available in kernel");
697     }
698 #endif
699     if (go->deflate) {
700         p[0] = go->deflate_correct? CI_DEFLATE: CI_DEFLATE_DRAFT;
701         p[1] = CILEN_DEFLATE;
702         p[2] = DEFLATE_MAKE_OPT(go->deflate_size);
703         p[3] = DEFLATE_CHK_SEQUENCE;
704         for (;;) {
705             if (go->deflate_size < DEFLATE_MIN_WORKS) {
706                 go->deflate = 0;
707                 break;
708             }
709             res = ccp_test(f->unit, p, CILEN_DEFLATE, 0);
710             if (res > 0) {
711                 p += CILEN_DEFLATE;
712                 break;
713             } else if (res < 0) {
714                 go->deflate = 0;
715                 break;
716             }
717             --go->deflate_size;
718             p[2] = DEFLATE_MAKE_OPT(go->deflate_size);
719         }
720         if (p != p0 && go->deflate_correct && go->deflate_draft) {
721             p[0] = CI_DEFLATE_DRAFT;
722             p[1] = CILEN_DEFLATE;
723             p[2] = p[2 - CILEN_DEFLATE];
724             p[3] = DEFLATE_CHK_SEQUENCE;
725             p += CILEN_DEFLATE;
726         }
727     }
728     if (go->bsd_compress) {
729         p[0] = CI_BSD_COMPRESS;
730         p[1] = CILEN_BSD_COMPRESS;
731         p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits);
732         for (;;) {
733             if (go->bsd_bits < BSD_MIN_BITS) {
734                 go->bsd_compress = 0;
735                 break;
736             }
737             res = ccp_test(f->unit, p, CILEN_BSD_COMPRESS, 0);
738             if (res > 0) {
739                 p += CILEN_BSD_COMPRESS;
740                 break;
741             } else if (res < 0) {
742                 go->bsd_compress = 0;
743                 break;
744             }
745             --go->bsd_bits;
746             p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits);
747         }
748     }
749     /* XXX Should Predictor 2 be preferable to Predictor 1? */
750     if (go->predictor_1) {
751         p[0] = CI_PREDICTOR_1;
752         p[1] = CILEN_PREDICTOR_1;
753         if (p == p0 && ccp_test(f->unit, p, CILEN_PREDICTOR_1, 0) <= 0) {
754             go->predictor_1 = 0;
755         } else {
756             p += CILEN_PREDICTOR_1;
757         }
758     }
759     if (go->predictor_2) {
760         p[0] = CI_PREDICTOR_2;
761         p[1] = CILEN_PREDICTOR_2;
762         if (p == p0 && ccp_test(f->unit, p, CILEN_PREDICTOR_2, 0) <= 0) {
763             go->predictor_2 = 0;
764         } else {
765             p += CILEN_PREDICTOR_2;
766         }
767     }
768
769     go->method = (p > p0)? p0[0]: -1;
770
771     *lenp = p - p0;
772 }
773
774 /*
775  * ccp_ackci - process a received configure-ack, and return
776  * 1 iff the packet was OK.
777  */
778 static int
779 ccp_ackci(f, p, len)
780     fsm *f;
781     u_char *p;
782     int len;
783 {
784     ccp_options *go = &ccp_gotoptions[f->unit];
785     u_char *p0 = p;
786
787 #ifdef MPPE
788     if (go->mppe) {
789         u_char opt_buf[CILEN_MPPE];
790
791         opt_buf[0] = CI_MPPE;
792         opt_buf[1] = CILEN_MPPE;
793         MPPE_OPTS_TO_CI(go->mppe, &opt_buf[2]);
794         if (len < CILEN_MPPE || memcmp(opt_buf, p, CILEN_MPPE))
795             return 0;
796         p += CILEN_MPPE;
797         len -= CILEN_MPPE;
798         /* XXX Cope with first/fast ack */
799         if (len == 0)
800             return 1;
801     }
802 #endif
803     if (go->deflate) {
804         if (len < CILEN_DEFLATE
805             || p[0] != (go->deflate_correct? CI_DEFLATE: CI_DEFLATE_DRAFT)
806             || p[1] != CILEN_DEFLATE
807             || p[2] != DEFLATE_MAKE_OPT(go->deflate_size)
808             || p[3] != DEFLATE_CHK_SEQUENCE)
809             return 0;
810         p += CILEN_DEFLATE;
811         len -= CILEN_DEFLATE;
812         /* XXX Cope with first/fast ack */
813         if (len == 0)
814             return 1;
815         if (go->deflate_correct && go->deflate_draft) {
816             if (len < CILEN_DEFLATE
817                 || p[0] != CI_DEFLATE_DRAFT
818                 || p[1] != CILEN_DEFLATE
819                 || p[2] != DEFLATE_MAKE_OPT(go->deflate_size)
820                 || p[3] != DEFLATE_CHK_SEQUENCE)
821                 return 0;
822             p += CILEN_DEFLATE;
823             len -= CILEN_DEFLATE;
824         }
825     }
826     if (go->bsd_compress) {
827         if (len < CILEN_BSD_COMPRESS
828             || p[0] != CI_BSD_COMPRESS || p[1] != CILEN_BSD_COMPRESS
829             || p[2] != BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits))
830             return 0;
831         p += CILEN_BSD_COMPRESS;
832         len -= CILEN_BSD_COMPRESS;
833         /* XXX Cope with first/fast ack */
834         if (p == p0 && len == 0)
835             return 1;
836     }
837     if (go->predictor_1) {
838         if (len < CILEN_PREDICTOR_1
839             || p[0] != CI_PREDICTOR_1 || p[1] != CILEN_PREDICTOR_1)
840             return 0;
841         p += CILEN_PREDICTOR_1;
842         len -= CILEN_PREDICTOR_1;
843         /* XXX Cope with first/fast ack */
844         if (p == p0 && len == 0)
845             return 1;
846     }
847     if (go->predictor_2) {
848         if (len < CILEN_PREDICTOR_2
849             || p[0] != CI_PREDICTOR_2 || p[1] != CILEN_PREDICTOR_2)
850             return 0;
851         p += CILEN_PREDICTOR_2;
852         len -= CILEN_PREDICTOR_2;
853         /* XXX Cope with first/fast ack */
854         if (p == p0 && len == 0)
855             return 1;
856     }
857
858     if (len != 0)
859         return 0;
860     return 1;
861 }
862
863 /*
864  * ccp_nakci - process received configure-nak.
865  * Returns 1 iff the nak was OK.
866  */
867 static int
868 ccp_nakci(f, p, len)
869     fsm *f;
870     u_char *p;
871     int len;
872 {
873     ccp_options *go = &ccp_gotoptions[f->unit];
874     ccp_options no;             /* options we've seen already */
875     ccp_options try;            /* options to ask for next time */
876
877     memset(&no, 0, sizeof(no));
878     try = *go;
879
880 #ifdef MPPE
881     if (go->mppe && len >= CILEN_MPPE
882         && p[0] == CI_MPPE && p[1] == CILEN_MPPE) {
883         no.mppe = 1;
884         /*
885          * Peer wants us to use a different strength or other setting.
886          * Fail if we aren't willing to use his suggestion.
887          */
888         MPPE_CI_TO_OPTS(&p[2], try.mppe);
889         if ((try.mppe & MPPE_OPT_STATEFUL) && refuse_mppe_stateful)
890             try.mppe = 0;
891         else if ((go->mppe & try.mppe) != try.mppe)
892             /* Peer must have set options we didn't request (suggest) */
893             try.mppe = 0;
894
895         if (!try.mppe)
896             lcp_close(f->unit, "MPPE required but peer negotiation failed");
897     }
898 #endif /* MPPE */
899     if (go->deflate && len >= CILEN_DEFLATE
900         && p[0] == (go->deflate_correct? CI_DEFLATE: CI_DEFLATE_DRAFT)
901         && p[1] == CILEN_DEFLATE) {
902         no.deflate = 1;
903         /*
904          * Peer wants us to use a different code size or something.
905          * Stop asking for Deflate if we don't understand his suggestion.
906          */
907         if (DEFLATE_METHOD(p[2]) != DEFLATE_METHOD_VAL
908             || DEFLATE_SIZE(p[2]) < DEFLATE_MIN_WORKS
909             || p[3] != DEFLATE_CHK_SEQUENCE)
910             try.deflate = 0;
911         else if (DEFLATE_SIZE(p[2]) < go->deflate_size)
912             try.deflate_size = DEFLATE_SIZE(p[2]);
913         p += CILEN_DEFLATE;
914         len -= CILEN_DEFLATE;
915         if (go->deflate_correct && go->deflate_draft
916             && len >= CILEN_DEFLATE && p[0] == CI_DEFLATE_DRAFT
917             && p[1] == CILEN_DEFLATE) {
918             p += CILEN_DEFLATE;
919             len -= CILEN_DEFLATE;
920         }
921     }
922
923     if (go->bsd_compress && len >= CILEN_BSD_COMPRESS
924         && p[0] == CI_BSD_COMPRESS && p[1] == CILEN_BSD_COMPRESS) {
925         no.bsd_compress = 1;
926         /*
927          * Peer wants us to use a different number of bits
928          * or a different version.
929          */
930         if (BSD_VERSION(p[2]) != BSD_CURRENT_VERSION)
931             try.bsd_compress = 0;
932         else if (BSD_NBITS(p[2]) < go->bsd_bits)
933             try.bsd_bits = BSD_NBITS(p[2]);
934         p += CILEN_BSD_COMPRESS;
935         len -= CILEN_BSD_COMPRESS;
936     }
937
938     /*
939      * Predictor-1 and 2 have no options, so they can't be Naked.
940      *
941      * There may be remaining options but we ignore them.
942      */
943
944     if (f->state != OPENED)
945         *go = try;
946     return 1;
947 }
948
949 /*
950  * ccp_rejci - reject some of our suggested compression methods.
951  */
952 static int
953 ccp_rejci(f, p, len)
954     fsm *f;
955     u_char *p;
956     int len;
957 {
958     ccp_options *go = &ccp_gotoptions[f->unit];
959     ccp_options try;            /* options to request next time */
960
961     try = *go;
962
963     /*
964      * Cope with empty configure-rejects by ceasing to send
965      * configure-requests.
966      */
967     if (len == 0 && all_rejected[f->unit])
968         return -1;
969
970 #ifdef MPPE
971     if (go->mppe && len >= CILEN_MPPE
972         && p[0] == CI_MPPE && p[1] == CILEN_MPPE) {
973         lcp_close(f->unit, "MPPE required but peer refused");
974         p += CILEN_MPPE;
975         len -= CILEN_MPPE;
976     }
977 #endif
978     if (go->deflate && len >= CILEN_DEFLATE
979         && p[0] == (go->deflate_correct? CI_DEFLATE: CI_DEFLATE_DRAFT)
980         && p[1] == CILEN_DEFLATE) {
981         if (p[2] != DEFLATE_MAKE_OPT(go->deflate_size)
982             || p[3] != DEFLATE_CHK_SEQUENCE)
983             return 0;           /* Rej is bad */
984         if (go->deflate_correct)
985             try.deflate_correct = 0;
986         else
987             try.deflate_draft = 0;
988         p += CILEN_DEFLATE;
989         len -= CILEN_DEFLATE;
990         if (go->deflate_correct && go->deflate_draft
991             && len >= CILEN_DEFLATE && p[0] == CI_DEFLATE_DRAFT
992             && p[1] == CILEN_DEFLATE) {
993             if (p[2] != DEFLATE_MAKE_OPT(go->deflate_size)
994                 || p[3] != DEFLATE_CHK_SEQUENCE)
995                 return 0;               /* Rej is bad */
996             try.deflate_draft = 0;
997             p += CILEN_DEFLATE;
998             len -= CILEN_DEFLATE;
999         }
1000         if (!try.deflate_correct && !try.deflate_draft)
1001             try.deflate = 0;
1002     }
1003     if (go->bsd_compress && len >= CILEN_BSD_COMPRESS
1004         && p[0] == CI_BSD_COMPRESS && p[1] == CILEN_BSD_COMPRESS) {
1005         if (p[2] != BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits))
1006             return 0;
1007         try.bsd_compress = 0;
1008         p += CILEN_BSD_COMPRESS;
1009         len -= CILEN_BSD_COMPRESS;
1010     }
1011     if (go->predictor_1 && len >= CILEN_PREDICTOR_1
1012         && p[0] == CI_PREDICTOR_1 && p[1] == CILEN_PREDICTOR_1) {
1013         try.predictor_1 = 0;
1014         p += CILEN_PREDICTOR_1;
1015         len -= CILEN_PREDICTOR_1;
1016     }
1017     if (go->predictor_2 && len >= CILEN_PREDICTOR_2
1018         && p[0] == CI_PREDICTOR_2 && p[1] == CILEN_PREDICTOR_2) {
1019         try.predictor_2 = 0;
1020         p += CILEN_PREDICTOR_2;
1021         len -= CILEN_PREDICTOR_2;
1022     }
1023
1024     if (len != 0)
1025         return 0;
1026
1027     if (f->state != OPENED)
1028         *go = try;
1029
1030     return 1;
1031 }
1032
1033 /*
1034  * ccp_reqci - processed a received configure-request.
1035  * Returns CONFACK, CONFNAK or CONFREJ and the packet modified
1036  * appropriately.
1037  */
1038 static int
1039 ccp_reqci(f, p, lenp, dont_nak)
1040     fsm *f;
1041     u_char *p;
1042     int *lenp;
1043     int dont_nak;
1044 {
1045     int ret, newret, res;
1046     u_char *p0, *retp;
1047     int len, clen, type, nb;
1048     ccp_options *ho = &ccp_hisoptions[f->unit];
1049     ccp_options *ao = &ccp_allowoptions[f->unit];
1050
1051     ret = CONFACK;
1052     retp = p0 = p;
1053     len = *lenp;
1054
1055     memset(ho, 0, sizeof(ccp_options));
1056     ho->method = (len > 0)? p[0]: -1;
1057
1058     while (len > 0) {
1059         newret = CONFACK;
1060         if (len < 2 || p[1] < 2 || p[1] > len) {
1061             /* length is bad */
1062             clen = len;
1063             newret = CONFREJ;
1064
1065         } else {
1066             type = p[0];
1067             clen = p[1];
1068
1069             switch (type) {
1070 #ifdef MPPE
1071             case CI_MPPE:
1072                 if (!ao->mppe || clen != CILEN_MPPE) {
1073                     newret = CONFREJ;
1074                     break;
1075                 }
1076                 MPPE_CI_TO_OPTS(&p[2], ho->mppe);
1077
1078                 /* Nak if anything unsupported or unknown are set. */
1079                 if (ho->mppe & MPPE_OPT_UNSUPPORTED) {
1080                     newret = CONFNAK;
1081                     ho->mppe &= ~MPPE_OPT_UNSUPPORTED;
1082                 }
1083                 if (ho->mppe & MPPE_OPT_UNKNOWN) {
1084                     newret = CONFNAK;
1085                     ho->mppe &= ~MPPE_OPT_UNKNOWN;
1086                 }
1087
1088                 /* Check state opt */
1089                 if (ho->mppe & MPPE_OPT_STATEFUL) {
1090                    if (refuse_mppe_stateful) {
1091                         /*
1092                          * We can Nak and request stateless, but it's a
1093                          * lot easier to just assume the peer will request
1094                          * it if he can do it; stateful mode is bad over
1095                          * the Internet -- which is where we expect MPPE.
1096                          */
1097                         newret = CONFREJ;
1098                         break;
1099                     } else {
1100                         newret = CONFNAK;
1101                     }
1102                 }
1103
1104                 /* Find out which of {S,L} are set. */
1105                 if ((ho->mppe & MPPE_OPT_128)
1106                      && (ho->mppe & MPPE_OPT_40)) {
1107                     /* Both are set, negotiate the strongest. */
1108                     newret = CONFNAK;
1109                     if (ao->mppe & MPPE_OPT_128)
1110                         ho->mppe &= ~MPPE_OPT_40;
1111                     else if (ao->mppe & MPPE_OPT_40)
1112                         ho->mppe &= ~MPPE_OPT_128;
1113                     else {
1114                         newret = CONFREJ;
1115                         break;
1116                     }
1117                 } else if (ho->mppe & MPPE_OPT_128) {
1118                     if (!(ao->mppe & MPPE_OPT_128)) {
1119                         newret = CONFREJ;
1120                         break;
1121                     }
1122                 } else if (ho->mppe & MPPE_OPT_40) {
1123                     if (!(ao->mppe & MPPE_OPT_40)) {
1124                         newret = CONFREJ;
1125                         break;
1126                     }
1127                 } else {
1128                     /* Neither are set. */
1129                     newret = CONFREJ;
1130                     break;
1131                 }
1132
1133                 /* rebuild the opts */
1134                 MPPE_OPTS_TO_CI(ho->mppe, &p[2]);
1135                 if (newret == CONFACK) {
1136                     u_char opt_buf[CILEN_MPPE + MPPE_MAX_KEY_LEN];
1137                     int mtu;
1138
1139                     BCOPY(p, opt_buf, CILEN_MPPE);
1140                     BCOPY(mppe_send_key, &opt_buf[CILEN_MPPE],
1141                           MPPE_MAX_KEY_LEN);
1142                     if (ccp_test(f->unit, opt_buf,
1143                                  CILEN_MPPE + MPPE_MAX_KEY_LEN, 1) <= 0) {
1144                         /* This shouldn't happen, we've already tested it! */
1145                         error("MPPE required, but kernel has no support.");
1146                         lcp_close(f->unit, "MPPE required but not available");
1147                         newret = CONFREJ;
1148                         break;
1149                     }
1150                     /*
1151                      * We need to decrease the interface MTU by MPPE_PAD
1152                      * because MPPE frames **grow**.  The kernel [must]
1153                      * allocate MPPE_PAD extra bytes in xmit buffers.
1154                      */
1155                     mtu = netif_get_mtu(f->unit);
1156                     if (mtu)
1157                         netif_set_mtu(f->unit, mtu - MPPE_PAD);
1158                     else
1159                         newret = CONFREJ;
1160                 }
1161
1162                 break;
1163 #endif /* MPPE */
1164             case CI_DEFLATE:
1165             case CI_DEFLATE_DRAFT:
1166                 if (!ao->deflate || clen != CILEN_DEFLATE
1167                     || (!ao->deflate_correct && type == CI_DEFLATE)
1168                     || (!ao->deflate_draft && type == CI_DEFLATE_DRAFT)) {
1169                     newret = CONFREJ;
1170                     break;
1171                 }
1172
1173                 ho->deflate = 1;
1174                 ho->deflate_size = nb = DEFLATE_SIZE(p[2]);
1175                 if (DEFLATE_METHOD(p[2]) != DEFLATE_METHOD_VAL
1176                     || p[3] != DEFLATE_CHK_SEQUENCE
1177                     || nb > ao->deflate_size || nb < DEFLATE_MIN_WORKS) {
1178                     newret = CONFNAK;
1179                     if (!dont_nak) {
1180                         p[2] = DEFLATE_MAKE_OPT(ao->deflate_size);
1181                         p[3] = DEFLATE_CHK_SEQUENCE;
1182                         /* fall through to test this #bits below */
1183                     } else
1184                         break;
1185                 }
1186
1187                 /*
1188                  * Check whether we can do Deflate with the window
1189                  * size they want.  If the window is too big, reduce
1190                  * it until the kernel can cope and nak with that.
1191                  * We only check this for the first option.
1192                  */
1193                 if (p == p0) {
1194                     for (;;) {
1195                         res = ccp_test(f->unit, p, CILEN_DEFLATE, 1);
1196                         if (res > 0)
1197                             break;              /* it's OK now */
1198                         if (res < 0 || nb == DEFLATE_MIN_WORKS || dont_nak) {
1199                             newret = CONFREJ;
1200                             p[2] = DEFLATE_MAKE_OPT(ho->deflate_size);
1201                             break;
1202                         }
1203                         newret = CONFNAK;
1204                         --nb;
1205                         p[2] = DEFLATE_MAKE_OPT(nb);
1206                     }
1207                 }
1208                 break;
1209
1210             case CI_BSD_COMPRESS:
1211                 if (!ao->bsd_compress || clen != CILEN_BSD_COMPRESS) {
1212                     newret = CONFREJ;
1213                     break;
1214                 }
1215
1216                 ho->bsd_compress = 1;
1217                 ho->bsd_bits = nb = BSD_NBITS(p[2]);
1218                 if (BSD_VERSION(p[2]) != BSD_CURRENT_VERSION
1219                     || nb > ao->bsd_bits || nb < BSD_MIN_BITS) {
1220                     newret = CONFNAK;
1221                     if (!dont_nak) {
1222                         p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, ao->bsd_bits);
1223                         /* fall through to test this #bits below */
1224                     } else
1225                         break;
1226                 }
1227
1228                 /*
1229                  * Check whether we can do BSD-Compress with the code
1230                  * size they want.  If the code size is too big, reduce
1231                  * it until the kernel can cope and nak with that.
1232                  * We only check this for the first option.
1233                  */
1234                 if (p == p0) {
1235                     for (;;) {
1236                         res = ccp_test(f->unit, p, CILEN_BSD_COMPRESS, 1);
1237                         if (res > 0)
1238                             break;
1239                         if (res < 0 || nb == BSD_MIN_BITS || dont_nak) {
1240                             newret = CONFREJ;
1241                             p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION,
1242                                                 ho->bsd_bits);
1243                             break;
1244                         }
1245                         newret = CONFNAK;
1246                         --nb;
1247                         p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, nb);
1248                     }
1249                 }
1250                 break;
1251
1252             case CI_PREDICTOR_1:
1253                 if (!ao->predictor_1 || clen != CILEN_PREDICTOR_1) {
1254                     newret = CONFREJ;
1255                     break;
1256                 }
1257
1258                 ho->predictor_1 = 1;
1259                 if (p == p0
1260                     && ccp_test(f->unit, p, CILEN_PREDICTOR_1, 1) <= 0) {
1261                     newret = CONFREJ;
1262                 }
1263                 break;
1264
1265             case CI_PREDICTOR_2:
1266                 if (!ao->predictor_2 || clen != CILEN_PREDICTOR_2) {
1267                     newret = CONFREJ;
1268                     break;
1269                 }
1270
1271                 ho->predictor_2 = 1;
1272                 if (p == p0
1273                     && ccp_test(f->unit, p, CILEN_PREDICTOR_2, 1) <= 0) {
1274                     newret = CONFREJ;
1275                 }
1276                 break;
1277
1278             default:
1279                 newret = CONFREJ;
1280             }
1281         }
1282
1283         if (newret == CONFNAK && dont_nak)
1284             newret = CONFREJ;
1285         if (!(newret == CONFACK || (newret == CONFNAK && ret == CONFREJ))) {
1286             /* we're returning this option */
1287             if (newret == CONFREJ && ret == CONFNAK)
1288                 retp = p0;
1289             ret = newret;
1290             if (p != retp)
1291                 BCOPY(p, retp, clen);
1292             retp += clen;
1293         }
1294
1295         p += clen;
1296         len -= clen;
1297     }
1298
1299     if (ret != CONFACK) {
1300         if (ret == CONFREJ && *lenp == retp - p0)
1301             all_rejected[f->unit] = 1;
1302         else
1303             *lenp = retp - p0;
1304     }
1305     if (ret == CONFREJ && ao->mppe)
1306         lcp_close(f->unit, "MPPE required but peer negotiation failed");
1307     return ret;
1308 }
1309
1310 /*
1311  * Make a string name for a compression method (or 2).
1312  */
1313 static char *
1314 method_name(opt, opt2)
1315     ccp_options *opt, *opt2;
1316 {
1317     static char result[64];
1318
1319     if (!ANY_COMPRESS(*opt))
1320         return "(none)";
1321     switch (opt->method) {
1322 #ifdef MPPE
1323     case CI_MPPE:
1324     {
1325         char *p = result;
1326         char *q = result + sizeof(result); /* 1 past result */
1327
1328         slprintf(p, q - p, "MPPE ");
1329         p += 5;
1330         if (opt->mppe & MPPE_OPT_128) {
1331             slprintf(p, q - p, "128-bit ");
1332             p += 8;
1333         }
1334         if (opt->mppe & MPPE_OPT_40) {
1335             slprintf(p, q - p, "40-bit ");
1336             p += 7;
1337         }
1338         if (opt->mppe & MPPE_OPT_STATEFUL)
1339             slprintf(p, q - p, "stateful");
1340         else
1341             slprintf(p, q - p, "stateless");
1342
1343         break;
1344     }
1345 #endif
1346     case CI_DEFLATE:
1347     case CI_DEFLATE_DRAFT:
1348         if (opt2 != NULL && opt2->deflate_size != opt->deflate_size)
1349             slprintf(result, sizeof(result), "Deflate%s (%d/%d)",
1350                      (opt->method == CI_DEFLATE_DRAFT? "(old#)": ""),
1351                      opt->deflate_size, opt2->deflate_size);
1352         else
1353             slprintf(result, sizeof(result), "Deflate%s (%d)",
1354                      (opt->method == CI_DEFLATE_DRAFT? "(old#)": ""),
1355                      opt->deflate_size);
1356         break;
1357     case CI_BSD_COMPRESS:
1358         if (opt2 != NULL && opt2->bsd_bits != opt->bsd_bits)
1359             slprintf(result, sizeof(result), "BSD-Compress (%d/%d)",
1360                      opt->bsd_bits, opt2->bsd_bits);
1361         else
1362             slprintf(result, sizeof(result), "BSD-Compress (%d)",
1363                      opt->bsd_bits);
1364         break;
1365     case CI_PREDICTOR_1:
1366         return "Predictor 1";
1367     case CI_PREDICTOR_2:
1368         return "Predictor 2";
1369     default:
1370         slprintf(result, sizeof(result), "Method %d", opt->method);
1371     }
1372     return result;
1373 }
1374
1375 /*
1376  * CCP has come up - inform the kernel driver and log a message.
1377  */
1378 static void
1379 ccp_up(f)
1380     fsm *f;
1381 {
1382     ccp_options *go = &ccp_gotoptions[f->unit];
1383     ccp_options *ho = &ccp_hisoptions[f->unit];
1384     char method1[64];
1385
1386     ccp_flags_set(f->unit, 1, 1);
1387     if (ANY_COMPRESS(*go)) {
1388         if (ANY_COMPRESS(*ho)) {
1389             if (go->method == ho->method) {
1390                 notice("%s compression enabled", method_name(go, ho));
1391             } else {
1392                 strlcpy(method1, method_name(go, NULL), sizeof(method1));
1393                 notice("%s / %s compression enabled",
1394                        method1, method_name(ho, NULL));
1395             }
1396         } else
1397             notice("%s receive compression enabled", method_name(go, NULL));
1398     } else if (ANY_COMPRESS(*ho))
1399         notice("%s transmit compression enabled", method_name(ho, NULL));
1400 }
1401
1402 /*
1403  * CCP has gone down - inform the kernel driver.
1404  */
1405 static void
1406 ccp_down(f)
1407     fsm *f;
1408 {
1409     if (ccp_localstate[f->unit] & RACK_PENDING)
1410         UNTIMEOUT(ccp_rack_timeout, f);
1411     ccp_localstate[f->unit] = 0;
1412     ccp_flags_set(f->unit, 1, 0);
1413 }
1414
1415 /*
1416  * Print the contents of a CCP packet.
1417  */
1418 static char *ccp_codenames[] = {
1419     "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1420     "TermReq", "TermAck", "CodeRej",
1421     NULL, NULL, NULL, NULL, NULL, NULL,
1422     "ResetReq", "ResetAck",
1423 };
1424
1425 static int
1426 ccp_printpkt(p, plen, printer, arg)
1427     u_char *p;
1428     int plen;
1429     void (*printer) __P((void *, char *, ...));
1430     void *arg;
1431 {
1432     u_char *p0, *optend;
1433     int code, id, len;
1434     int optlen;
1435
1436     p0 = p;
1437     if (plen < HEADERLEN)
1438         return 0;
1439     code = p[0];
1440     id = p[1];
1441     len = (p[2] << 8) + p[3];
1442     if (len < HEADERLEN || len > plen)
1443         return 0;
1444
1445     if (code >= 1 && code <= sizeof(ccp_codenames) / sizeof(char *)
1446         && ccp_codenames[code-1] != NULL)
1447         printer(arg, " %s", ccp_codenames[code-1]);
1448     else
1449         printer(arg, " code=0x%x", code);
1450     printer(arg, " id=0x%x", id);
1451     len -= HEADERLEN;
1452     p += HEADERLEN;
1453
1454     switch (code) {
1455     case CONFREQ:
1456     case CONFACK:
1457     case CONFNAK:
1458     case CONFREJ:
1459         /* print list of possible compression methods */
1460         while (len >= 2) {
1461             code = p[0];
1462             optlen = p[1];
1463             if (optlen < 2 || optlen > len)
1464                 break;
1465             printer(arg, " <");
1466             len -= optlen;
1467             optend = p + optlen;
1468             switch (code) {
1469 #ifdef MPPE
1470             case CI_MPPE:
1471                 if (optlen >= CILEN_MPPE) {
1472                     u_char mppe_opts;
1473
1474                     MPPE_CI_TO_OPTS(&p[2], mppe_opts);
1475                     printer(arg, "mppe %s %s %s %s %s %s%s",
1476                             (p[2] & MPPE_H_BIT)? "+H": "-H",
1477                             (p[5] & MPPE_M_BIT)? "+M": "-M",
1478                             (p[5] & MPPE_S_BIT)? "+S": "-S",
1479                             (p[5] & MPPE_L_BIT)? "+L": "-L",
1480                             (p[5] & MPPE_D_BIT)? "+D": "-D",
1481                             (p[5] & MPPE_C_BIT)? "+C": "-C",
1482                             (mppe_opts & MPPE_OPT_UNKNOWN)? " +U": "");
1483                     p += CILEN_MPPE;
1484                 }
1485                 break;
1486 #endif
1487             case CI_DEFLATE:
1488             case CI_DEFLATE_DRAFT:
1489                 if (optlen >= CILEN_DEFLATE) {
1490                     printer(arg, "deflate%s %d",
1491                             (code == CI_DEFLATE_DRAFT? "(old#)": ""),
1492                             DEFLATE_SIZE(p[2]));
1493                     if (DEFLATE_METHOD(p[2]) != DEFLATE_METHOD_VAL)
1494                         printer(arg, " method %d", DEFLATE_METHOD(p[2]));
1495                     if (p[3] != DEFLATE_CHK_SEQUENCE)
1496                         printer(arg, " check %d", p[3]);
1497                     p += CILEN_DEFLATE;
1498                 }
1499                 break;
1500             case CI_BSD_COMPRESS:
1501                 if (optlen >= CILEN_BSD_COMPRESS) {
1502                     printer(arg, "bsd v%d %d", BSD_VERSION(p[2]),
1503                             BSD_NBITS(p[2]));
1504                     p += CILEN_BSD_COMPRESS;
1505                 }
1506                 break;
1507             case CI_PREDICTOR_1:
1508                 if (optlen >= CILEN_PREDICTOR_1) {
1509                     printer(arg, "predictor 1");
1510                     p += CILEN_PREDICTOR_1;
1511                 }
1512                 break;
1513             case CI_PREDICTOR_2:
1514                 if (optlen >= CILEN_PREDICTOR_2) {
1515                     printer(arg, "predictor 2");
1516                     p += CILEN_PREDICTOR_2;
1517                 }
1518                 break;
1519             }
1520             while (p < optend)
1521                 printer(arg, " %.2x", *p++);
1522             printer(arg, ">");
1523         }
1524         break;
1525
1526     case TERMACK:
1527     case TERMREQ:
1528         if (len > 0 && *p >= ' ' && *p < 0x7f) {
1529             print_string((char *)p, len, printer, arg);
1530             p += len;
1531             len = 0;
1532         }
1533         break;
1534     }
1535
1536     /* dump out the rest of the packet in hex */
1537     while (--len >= 0)
1538         printer(arg, " %.2x", *p++);
1539
1540     return p - p0;
1541 }
1542
1543 /*
1544  * We have received a packet that the decompressor failed to
1545  * decompress.  Here we would expect to issue a reset-request, but
1546  * Motorola has a patent on resetting the compressor as a result of
1547  * detecting an error in the decompressed data after decompression.
1548  * (See US patent 5,130,993; international patent publication number
1549  * WO 91/10289; Australian patent 73296/91.)
1550  *
1551  * So we ask the kernel whether the error was detected after
1552  * decompression; if it was, we take CCP down, thus disabling
1553  * compression :-(, otherwise we issue the reset-request.
1554  */
1555 static void
1556 ccp_datainput(unit, pkt, len)
1557     int unit;
1558     u_char *pkt;
1559     int len;
1560 {
1561     fsm *f;
1562
1563     f = &ccp_fsm[unit];
1564     if (f->state == OPENED) {
1565         if (ccp_fatal_error(unit)) {
1566             /*
1567              * Disable compression by taking CCP down.
1568              */
1569             error("Lost compression sync: disabling compression");
1570             ccp_close(unit, "Lost compression sync");
1571 #ifdef MPPE
1572             /*
1573              * If we were doing MPPE, we must also take the link down.
1574              */
1575             if (ccp_gotoptions[unit].mppe) {
1576                 error("Too many MPPE errors, closing LCP");
1577                 lcp_close(unit, "Too many MPPE errors");
1578             }
1579 #endif
1580         } else {
1581             /*
1582              * Send a reset-request to reset the peer's compressor.
1583              * We don't do that if we are still waiting for an
1584              * acknowledgement to a previous reset-request.
1585              */
1586             if (!(ccp_localstate[f->unit] & RACK_PENDING)) {
1587                 fsm_sdata(f, CCP_RESETREQ, f->reqid = ++f->id, NULL, 0);
1588                 TIMEOUT(ccp_rack_timeout, f, RACKTIMEOUT);
1589                 ccp_localstate[f->unit] |= RACK_PENDING;
1590             } else
1591                 ccp_localstate[f->unit] |= RREQ_REPEAT;
1592         }
1593     }
1594 }
1595
1596 /*
1597  * Timeout waiting for reset-ack.
1598  */
1599 static void
1600 ccp_rack_timeout(arg)
1601     void *arg;
1602 {
1603     fsm *f = arg;
1604
1605     if (f->state == OPENED && ccp_localstate[f->unit] & RREQ_REPEAT) {
1606         fsm_sdata(f, CCP_RESETREQ, f->reqid, NULL, 0);
1607         TIMEOUT(ccp_rack_timeout, f, RACKTIMEOUT);
1608         ccp_localstate[f->unit] &= ~RREQ_REPEAT;
1609     } else
1610         ccp_localstate[f->unit] &= ~RACK_PENDING;
1611 }
1612