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