pppd patches from wl500g.googlecode.com project
[tomato.git] / release / src / router / pppd / pppd / ccp.c
blob99eaa47cdeb4d20ba49ef8fad49ebfc0a1bf6511
1 /*
2 * ccp.c - PPP Compression Control Protocol.
4 * Copyright (c) 1994-2002 Paul Mackerras. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. The name(s) of the authors of this software must not be used to
14 * endorse or promote products derived from this software without
15 * prior written permission.
17 * 3. Redistributions of any form whatsoever must retain the following
18 * acknowledgment:
19 * "This product includes software developed by Paul Mackerras
20 * <paulus@samba.org>".
22 * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
23 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
24 * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
25 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
26 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
27 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
28 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
31 #define RCSID "$Id: ccp.c,v 1.50 2005/06/26 19:34:41 carlsonj Exp $"
33 #include <stdlib.h>
34 #include <string.h>
36 #include "pppd.h"
37 #include "fsm.h"
38 #include "ccp.h"
39 #include <net/ppp-comp.h>
41 #ifdef MPPE
42 #include "chap_ms.h" /* mppe_xxxx_key, mppe_keys_set */
43 #include "lcp.h" /* lcp_close(), lcp_fsm */
44 #endif
46 static const char rcsid[] = RCSID;
49 * Unfortunately there is a bug in zlib which means that using a
50 * size of 8 (window size = 256) for Deflate compression will cause
51 * buffer overruns and kernel crashes in the deflate module.
52 * Until this is fixed we only accept sizes in the range 9 .. 15.
53 * Thanks to James Carlson for pointing this out.
55 #define DEFLATE_MIN_WORKS 9
58 * Command-line options.
60 static int setbsdcomp __P((char **));
61 static int setdeflate __P((char **));
62 static char bsd_value[8];
63 static char deflate_value[8];
65 static option_t ccp_option_list[] = {
66 { "noccp", o_bool, &ccp_protent.enabled_flag,
67 "Disable CCP negotiation" },
68 { "-ccp", o_bool, &ccp_protent.enabled_flag,
69 "Disable CCP negotiation", OPT_ALIAS },
71 { "bsdcomp", o_special, (void *)setbsdcomp,
72 "Request BSD-Compress packet compression",
73 OPT_PRIO | OPT_A2STRVAL | OPT_STATIC, bsd_value },
74 { "nobsdcomp", o_bool, &ccp_wantoptions[0].bsd_compress,
75 "don't allow BSD-Compress", OPT_PRIOSUB | OPT_A2CLR,
76 &ccp_allowoptions[0].bsd_compress },
77 { "-bsdcomp", o_bool, &ccp_wantoptions[0].bsd_compress,
78 "don't allow BSD-Compress", OPT_ALIAS | OPT_PRIOSUB | OPT_A2CLR,
79 &ccp_allowoptions[0].bsd_compress },
81 { "deflate", o_special, (void *)setdeflate,
82 "request Deflate compression",
83 OPT_PRIO | OPT_A2STRVAL | OPT_STATIC, deflate_value },
84 { "nodeflate", o_bool, &ccp_wantoptions[0].deflate,
85 "don't allow Deflate compression", OPT_PRIOSUB | OPT_A2CLR,
86 &ccp_allowoptions[0].deflate },
87 { "-deflate", o_bool, &ccp_wantoptions[0].deflate,
88 "don't allow Deflate compression", OPT_ALIAS | OPT_PRIOSUB | OPT_A2CLR,
89 &ccp_allowoptions[0].deflate },
91 { "nodeflatedraft", o_bool, &ccp_wantoptions[0].deflate_draft,
92 "don't use draft deflate #", OPT_A2COPY,
93 &ccp_allowoptions[0].deflate_draft },
95 { "predictor1", o_bool, &ccp_wantoptions[0].predictor_1,
96 "request Predictor-1", OPT_PRIO | 1 },
97 { "nopredictor1", o_bool, &ccp_wantoptions[0].predictor_1,
98 "don't allow Predictor-1", OPT_PRIOSUB | OPT_A2CLR,
99 &ccp_allowoptions[0].predictor_1 },
100 { "-predictor1", o_bool, &ccp_wantoptions[0].predictor_1,
101 "don't allow Predictor-1", OPT_ALIAS | OPT_PRIOSUB | OPT_A2CLR,
102 &ccp_allowoptions[0].predictor_1 },
104 { "lzs", o_bool, &ccp_wantoptions[0].lzs,
105 "request Stac LZS", 1, &ccp_allowoptions[0].lzs, OPT_PRIO },
106 { "+lzs", o_bool, &ccp_wantoptions[0].lzs,
107 "request Stac LZS", 1, &ccp_allowoptions[0].lzs, OPT_ALIAS | OPT_PRIO },
108 { "nolzs", o_bool, &ccp_wantoptions[0].lzs,
109 "don't allow Stac LZS", OPT_PRIOSUB | OPT_A2CLR,
110 &ccp_allowoptions[0].lzs },
111 { "-lzs", o_bool, &ccp_wantoptions[0].lzs,
112 "don't allow Stac LZS", OPT_ALIAS | OPT_PRIOSUB | OPT_A2CLR,
113 &ccp_allowoptions[0].lzs },
115 #ifdef MPPE
116 { "mppc", o_bool, &ccp_wantoptions[0].mppc,
117 "request MPPC compression", 1, &ccp_allowoptions[0].mppc, OPT_PRIO },
118 { "+mppc", o_bool, &ccp_wantoptions[0].mppc,
119 "request MPPC compression", 1, &ccp_allowoptions[0].mppc,
120 OPT_ALIAS | OPT_PRIO },
121 { "nomppc", o_bool, &ccp_wantoptions[0].mppc,
122 "don't allow MPPC compression", OPT_PRIOSUB | OPT_A2CLR,
123 &ccp_allowoptions[0].mppc },
124 { "-mppc", o_bool, &ccp_wantoptions[0].mppc,
125 "don't allow MPPC compression", OPT_ALIAS | OPT_PRIOSUB | OPT_A2CLR,
126 &ccp_allowoptions[0].mppc },
128 { "require-mppe", o_bool, &ccp_wantoptions[0].mppe,
129 "require MPPE encryption", 1, &ccp_allowoptions[0].mppe, OPT_PRIO },
130 { "+mppe", o_bool, &ccp_wantoptions[0].mppe,
131 "require MPPE encryption", 1, &ccp_allowoptions[0].mppe,
132 OPT_ALIAS | OPT_PRIO },
133 { "nomppe", o_bool, &ccp_wantoptions[0].mppe,
134 "don't allow MPPE encryption", OPT_PRIOSUB | OPT_A2CLR,
135 &ccp_allowoptions[0].mppe },
136 { "-mppe", o_bool, &ccp_wantoptions[0].mppe,
137 "don't allow MPPE encryption", OPT_ALIAS | OPT_PRIOSUB | OPT_A2CLR,
138 &ccp_allowoptions[0].mppe },
140 { "require-mppe-40", o_bool, &ccp_wantoptions[0].mppe_40,
141 "require MPPE 40-bit encryption", 1, &ccp_allowoptions[0].mppe_40,
142 OPT_PRIO },
143 { "+mppe-40", o_bool, &ccp_wantoptions[0].mppe_40,
144 "require MPPE 40-bit encryption", 1, &ccp_allowoptions[0].mppe_40,
145 OPT_ALIAS | OPT_PRIO },
146 { "nomppe-40", o_bool, &ccp_wantoptions[0].mppe_40,
147 "don't allow MPPE 40-bit encryption", OPT_PRIOSUB | OPT_A2CLR,
148 &ccp_allowoptions[0].mppe_40 },
149 { "-mppe-40", o_bool, &ccp_wantoptions[0].mppe_40,
150 "don't allow MPPE 40-bit encryption", OPT_ALIAS | OPT_PRIOSUB | OPT_A2CLR,
151 &ccp_allowoptions[0].mppe_40 },
153 { "require-mppe-56", o_bool, &ccp_wantoptions[0].mppe_56,
154 "require MPPE 56-bit encryption", 1, &ccp_allowoptions[0].mppe_56,
155 OPT_PRIO },
156 { "+mppe-56", o_bool, &ccp_wantoptions[0].mppe_56,
157 "require MPPE 56-bit encryption", 1, &ccp_allowoptions[0].mppe_56,
158 OPT_ALIAS | OPT_PRIO },
159 { "nomppe-56", o_bool, &ccp_wantoptions[0].mppe_56,
160 "don't allow MPPE 56-bit encryption", OPT_PRIOSUB | OPT_A2CLR,
161 &ccp_allowoptions[0].mppe_56 },
162 { "-mppe-56", o_bool, &ccp_wantoptions[0].mppe_56,
163 "don't allow MPPE 56-bit encryption", OPT_ALIAS | OPT_PRIOSUB | OPT_A2CLR,
164 &ccp_allowoptions[0].mppe_56 },
166 { "require-mppe-128", o_bool, &ccp_wantoptions[0].mppe_128,
167 "require MPPE 128-bit encryption", 1, &ccp_allowoptions[0].mppe_128,
168 OPT_PRIO },
169 { "+mppe-128", o_bool, &ccp_wantoptions[0].mppe_128,
170 "require MPPE 128-bit encryption", 1, &ccp_allowoptions[0].mppe_128,
171 OPT_ALIAS | OPT_PRIO },
172 { "nomppe-128", o_bool, &ccp_wantoptions[0].mppe_40,
173 "don't allow MPPE 128-bit encryption", OPT_PRIOSUB | OPT_A2CLR,
174 &ccp_allowoptions[0].mppe_128 },
175 { "-mppe-128", o_bool, &ccp_wantoptions[0].mppe_128,
176 "don't allow MPPE 128-bit encryption", OPT_ALIAS | OPT_PRIOSUB | OPT_A2CLR,
177 &ccp_allowoptions[0].mppe_128 },
179 { "nomppe-stateful", o_bool, &ccp_wantoptions[0].mppe_stateless,
180 "disallow MPPE stateful mode", 1, &ccp_allowoptions[0].mppe_stateless,
181 OPT_PRIO },
182 { "mppe-stateful", o_bool, &ccp_wantoptions[0].mppe_stateless,
183 "allow MPPE stateful mode", OPT_PRIOSUB | OPT_A2CLR,
184 &ccp_allowoptions[0].mppe_stateless },
185 #endif /* MPPE */
187 { NULL }
191 * Protocol entry points from main code.
193 static void ccp_init __P((int unit));
194 static void ccp_open __P((int unit));
195 static void ccp_close __P((int unit, char *));
196 static void ccp_lowerup __P((int unit));
197 static void ccp_lowerdown __P((int));
198 static void ccp_input __P((int unit, u_char *pkt, int len));
199 static void ccp_protrej __P((int unit));
200 static int ccp_printpkt __P((u_char *pkt, int len,
201 void (*printer) __P((void *, char *, ...)),
202 void *arg));
203 static void ccp_datainput __P((int unit, u_char *pkt, int len));
205 struct protent ccp_protent = {
206 PPP_CCP,
207 ccp_init,
208 ccp_input,
209 ccp_protrej,
210 ccp_lowerup,
211 ccp_lowerdown,
212 ccp_open,
213 ccp_close,
214 ccp_printpkt,
215 ccp_datainput,
217 "CCP",
218 "Compressed",
219 ccp_option_list,
220 NULL,
221 NULL,
222 NULL
225 fsm ccp_fsm[NUM_PPP];
226 ccp_options ccp_wantoptions[NUM_PPP]; /* what to request the peer to use */
227 ccp_options ccp_gotoptions[NUM_PPP]; /* what the peer agreed to do */
228 ccp_options ccp_allowoptions[NUM_PPP]; /* what we'll agree to do */
229 ccp_options ccp_hisoptions[NUM_PPP]; /* what we agreed to do */
232 * Callbacks for fsm code.
234 static void ccp_resetci __P((fsm *));
235 static int ccp_cilen __P((fsm *));
236 static void ccp_addci __P((fsm *, u_char *, int *));
237 static int ccp_ackci __P((fsm *, u_char *, int));
238 static int ccp_nakci __P((fsm *, u_char *, int, int));
239 static int ccp_rejci __P((fsm *, u_char *, int));
240 static int ccp_reqci __P((fsm *, u_char *, int *, int));
241 static void ccp_up __P((fsm *));
242 static void ccp_down __P((fsm *));
243 static int ccp_extcode __P((fsm *, int, int, u_char *, int));
244 static void ccp_rack_timeout __P((void *));
245 static char *method_name __P((ccp_options *, ccp_options *));
247 static fsm_callbacks ccp_callbacks = {
248 ccp_resetci,
249 ccp_cilen,
250 ccp_addci,
251 ccp_ackci,
252 ccp_nakci,
253 ccp_rejci,
254 ccp_reqci,
255 ccp_up,
256 ccp_down,
257 NULL,
258 NULL,
259 NULL,
260 NULL,
261 ccp_extcode,
262 "CCP"
266 * Do we want / did we get any compression?
268 #define ANY_COMPRESS(opt) ((opt).deflate || (opt).bsd_compress \
269 || (opt).predictor_1 || (opt).predictor_2 \
270 || (opt).lzs || (opt).mppc || (opt).mppe)
273 * Local state (mainly for handling reset-reqs and reset-acks).
275 static int ccp_localstate[NUM_PPP];
276 #define RACK_PENDING 1 /* waiting for reset-ack */
277 #define RREQ_REPEAT 2 /* send another reset-req if no reset-ack */
279 #define RACKTIMEOUT 1 /* second */
281 static int all_rejected[NUM_PPP]; /* we rejected all peer's options */
284 * Option parsing.
286 static int
287 setbsdcomp(argv)
288 char **argv;
290 int rbits, abits;
291 char *str, *endp;
293 str = *argv;
294 abits = rbits = strtol(str, &endp, 0);
295 if (endp != str && *endp == ',') {
296 str = endp + 1;
297 abits = strtol(str, &endp, 0);
299 if (*endp != 0 || endp == str) {
300 option_error("invalid parameter '%s' for bsdcomp option", *argv);
301 return 0;
303 if ((rbits != 0 && (rbits < BSD_MIN_BITS || rbits > BSD_MAX_BITS))
304 || (abits != 0 && (abits < BSD_MIN_BITS || abits > BSD_MAX_BITS))) {
305 option_error("bsdcomp option values must be 0 or %d .. %d",
306 BSD_MIN_BITS, BSD_MAX_BITS);
307 return 0;
309 if (rbits > 0) {
310 ccp_wantoptions[0].bsd_compress = 1;
311 ccp_wantoptions[0].bsd_bits = rbits;
312 } else
313 ccp_wantoptions[0].bsd_compress = 0;
314 if (abits > 0) {
315 ccp_allowoptions[0].bsd_compress = 1;
316 ccp_allowoptions[0].bsd_bits = abits;
317 } else
318 ccp_allowoptions[0].bsd_compress = 0;
319 slprintf(bsd_value, sizeof(bsd_value),
320 rbits == abits? "%d": "%d,%d", rbits, abits);
322 return 1;
325 static int
326 setdeflate(argv)
327 char **argv;
329 int rbits, abits;
330 char *str, *endp;
332 str = *argv;
333 abits = rbits = strtol(str, &endp, 0);
334 if (endp != str && *endp == ',') {
335 str = endp + 1;
336 abits = strtol(str, &endp, 0);
338 if (*endp != 0 || endp == str) {
339 option_error("invalid parameter '%s' for deflate option", *argv);
340 return 0;
342 if ((rbits != 0 && (rbits < DEFLATE_MIN_SIZE || rbits > DEFLATE_MAX_SIZE))
343 || (abits != 0 && (abits < DEFLATE_MIN_SIZE
344 || abits > DEFLATE_MAX_SIZE))) {
345 option_error("deflate option values must be 0 or %d .. %d",
346 DEFLATE_MIN_SIZE, DEFLATE_MAX_SIZE);
347 return 0;
349 if (rbits == DEFLATE_MIN_SIZE || abits == DEFLATE_MIN_SIZE) {
350 if (rbits == DEFLATE_MIN_SIZE)
351 rbits = DEFLATE_MIN_WORKS;
352 if (abits == DEFLATE_MIN_SIZE)
353 abits = DEFLATE_MIN_WORKS;
354 warn("deflate option value of %d changed to %d to avoid zlib bug",
355 DEFLATE_MIN_SIZE, DEFLATE_MIN_WORKS);
357 if (rbits > 0) {
358 ccp_wantoptions[0].deflate = 1;
359 ccp_wantoptions[0].deflate_size = rbits;
360 } else
361 ccp_wantoptions[0].deflate = 0;
362 if (abits > 0) {
363 ccp_allowoptions[0].deflate = 1;
364 ccp_allowoptions[0].deflate_size = abits;
365 } else
366 ccp_allowoptions[0].deflate = 0;
367 slprintf(deflate_value, sizeof(deflate_value),
368 rbits == abits? "%d": "%d,%d", rbits, abits);
370 return 1;
374 * ccp_init - initialize CCP.
376 static void
377 ccp_init(unit)
378 int unit;
380 fsm *f = &ccp_fsm[unit];
382 f->unit = unit;
383 f->protocol = PPP_CCP;
384 f->callbacks = &ccp_callbacks;
385 fsm_init(f);
387 memset(&ccp_wantoptions[unit], 0, sizeof(ccp_options));
388 memset(&ccp_gotoptions[unit], 0, sizeof(ccp_options));
389 memset(&ccp_allowoptions[unit], 0, sizeof(ccp_options));
390 memset(&ccp_hisoptions[unit], 0, sizeof(ccp_options));
392 ccp_wantoptions[0].deflate = 1;
393 ccp_wantoptions[0].deflate_size = DEFLATE_MAX_SIZE;
394 ccp_wantoptions[0].deflate_correct = 1;
395 ccp_wantoptions[0].deflate_draft = 1;
396 ccp_allowoptions[0].deflate = 1;
397 ccp_allowoptions[0].deflate_size = DEFLATE_MAX_SIZE;
398 ccp_allowoptions[0].deflate_correct = 1;
399 ccp_allowoptions[0].deflate_draft = 1;
401 ccp_wantoptions[0].bsd_compress = 1;
402 ccp_wantoptions[0].bsd_bits = BSD_MAX_BITS;
403 ccp_allowoptions[0].bsd_compress = 1;
404 ccp_allowoptions[0].bsd_bits = BSD_MAX_BITS;
406 ccp_allowoptions[0].predictor_1 = 1;
408 ccp_wantoptions[0].lzs = 0; /* Stac LZS - will be enabled in the future */
409 ccp_wantoptions[0].lzs_mode = LZS_MODE_SEQ;
410 ccp_wantoptions[0].lzs_hists = 1;
411 ccp_allowoptions[0].lzs = 0; /* Stac LZS - will be enabled in the future */
412 ccp_allowoptions[0].lzs_mode = LZS_MODE_SEQ;
413 ccp_allowoptions[0].lzs_hists = 1;
415 #ifdef MPPE
416 /* by default allow and request MPPC... */
417 ccp_wantoptions[0].mppc = ccp_allowoptions[0].mppc = 1;
419 /* ... and allow but don't request MPPE */
420 ccp_allowoptions[0].mppe = 1;
421 ccp_allowoptions[0].mppe_40 = 1;
422 ccp_allowoptions[0].mppe_56 = 1;
423 ccp_allowoptions[0].mppe_128 = 1;
424 ccp_allowoptions[0].mppe_stateless = 1;
425 ccp_wantoptions[0].mppe = 0;
426 ccp_wantoptions[0].mppe_40 = 0;
427 ccp_wantoptions[0].mppe_56 = 0;
428 ccp_wantoptions[0].mppe_128 = 0;
429 ccp_wantoptions[0].mppe_stateless = 0;
430 #endif /* MPPE */
434 * ccp_open - CCP is allowed to come up.
436 static void
437 ccp_open(unit)
438 int unit;
440 fsm *f = &ccp_fsm[unit];
442 if (f->state != OPENED)
443 ccp_flags_set(unit, 1, 0);
446 * Find out which compressors the kernel supports before
447 * deciding whether to open in silent mode.
449 ccp_resetci(f);
450 if (!ANY_COMPRESS(ccp_gotoptions[unit]))
451 f->flags |= OPT_SILENT;
453 fsm_open(f);
457 * ccp_close - Terminate CCP.
459 static void
460 ccp_close(unit, reason)
461 int unit;
462 char *reason;
464 ccp_flags_set(unit, 0, 0);
465 fsm_close(&ccp_fsm[unit], reason);
469 * ccp_lowerup - we may now transmit CCP packets.
471 static void
472 ccp_lowerup(unit)
473 int unit;
475 fsm_lowerup(&ccp_fsm[unit]);
479 * ccp_lowerdown - we may not transmit CCP packets.
481 static void
482 ccp_lowerdown(unit)
483 int unit;
485 fsm_lowerdown(&ccp_fsm[unit]);
489 * ccp_input - process a received CCP packet.
491 static void
492 ccp_input(unit, p, len)
493 int unit;
494 u_char *p;
495 int len;
497 fsm *f = &ccp_fsm[unit];
498 int oldstate;
501 * Check for a terminate-request so we can print a message.
503 oldstate = f->state;
504 fsm_input(f, p, len);
505 if (oldstate == OPENED && p[0] == TERMREQ && f->state != OPENED) {
506 notice("Compression disabled by peer.");
507 #ifdef MPPE
508 if (ccp_wantoptions[unit].mppe) {
509 error("MPPE disabled, closing LCP");
510 lcp_close(unit, "MPPE disabled by peer");
512 #endif /* MPPE */
516 * If we get a terminate-ack and we're not asking for compression,
517 * close CCP.
519 if (oldstate == REQSENT && p[0] == TERMACK
520 && !ANY_COMPRESS(ccp_gotoptions[unit]))
521 ccp_close(unit, "No compression negotiated");
525 * Handle a CCP-specific code.
527 static int
528 ccp_extcode(f, code, id, p, len)
529 fsm *f;
530 int code, id;
531 u_char *p;
532 int len;
534 switch (code) {
535 case CCP_RESETREQ:
536 if (f->state != OPENED)
537 break;
538 /* send a reset-ack, which the transmitter will see and
539 reset its compression state. */
541 /* In case of MPPE/MPPC or LZS we shouldn't send CCP_RESETACK,
542 but we do it in order to reset compressor; CCP_RESETACK is
543 then silently discarded. See functions ppp_send_frame and
544 ppp_ccp_peek in ppp_generic.c (Linux only !!!). All the
545 confusion is caused by the fact that CCP code is splited
546 into two parts - one part is handled by pppd, the other one
547 is handled by kernel. */
549 fsm_sdata(f, CCP_RESETACK, id, NULL, 0);
550 break;
552 case CCP_RESETACK:
553 if (ccp_localstate[f->unit] & RACK_PENDING && id == f->reqid) {
554 ccp_localstate[f->unit] &= ~(RACK_PENDING | RREQ_REPEAT);
555 UNTIMEOUT(ccp_rack_timeout, f);
557 break;
559 default:
560 return 0;
563 return 1;
567 * ccp_protrej - peer doesn't talk CCP.
569 static void
570 ccp_protrej(unit)
571 int unit;
573 ccp_flags_set(unit, 0, 0);
574 fsm_lowerdown(&ccp_fsm[unit]);
576 #ifdef MPPE
577 if (ccp_wantoptions[unit].mppe) {
578 error("MPPE required but peer negotiation failed");
579 lcp_close(unit, "MPPE required but peer negotiation failed");
581 #endif /* MPPE */
585 * ccp_resetci - initialize at start of negotiation.
587 static void
588 ccp_resetci(f)
589 fsm *f;
591 ccp_options *go = &ccp_gotoptions[f->unit];
592 u_char opt_buf[CCP_MAX_OPTION_LENGTH];
594 *go = ccp_wantoptions[f->unit];
595 all_rejected[f->unit] = 0;
597 #ifdef MPPE
598 if (go->mppe || go->mppc) {
599 ccp_options *ao = &ccp_allowoptions[f->unit];
600 int auth_mschap_bits = auth_done[f->unit];
601 int numbits;
604 * Start with a basic sanity check: mschap[v2] auth must be in
605 * exactly one direction. RFC 3079 says that the keys are
606 * 'derived from the credentials of the peer that initiated the call',
607 * however the PPP protocol doesn't have such a concept, and pppd
608 * cannot get this info externally. Instead we do the best we can.
609 * NB: If MPPE is required, all other compression opts are invalid.
610 * So, we return right away if we can't do it.
612 if (ccp_wantoptions[f->unit].mppe) {
613 /* Leave only the mschap auth bits set */
614 auth_mschap_bits &= (CHAP_MS_WITHPEER | CHAP_MS_PEER |
615 CHAP_MS2_WITHPEER | CHAP_MS2_PEER);
616 /* Count the mschap auths */
617 auth_mschap_bits >>= CHAP_MS_SHIFT;
618 numbits = 0;
619 do {
620 numbits += auth_mschap_bits & 1;
621 auth_mschap_bits >>= 1;
622 } while (auth_mschap_bits);
623 if (numbits > 1) {
624 error("MPPE required, but auth done in both directions.");
625 lcp_close(f->unit, "MPPE required but not available");
626 return;
628 if (!numbits) {
629 error("MPPE required, but MS-CHAP[v2] auth not performed.");
630 lcp_close(f->unit, "MPPE required but not available");
631 return;
634 /* A plugin (eg radius) may not have obtained key material. */
635 if (!mppe_keys_set) {
636 error("MPPE required, but keys are not available. "
637 "Possible plugin problem?");
638 lcp_close(f->unit, "MPPE required but not available");
639 return;
644 * Check whether the kernel knows about the various
645 * compression methods we might request. Key material
646 * unimportant here.
648 if (go->mppc) {
649 opt_buf[0] = CI_MPPE;
650 opt_buf[1] = CILEN_MPPE;
651 opt_buf[2] = 0;
652 opt_buf[3] = 0;
653 opt_buf[4] = 0;
654 opt_buf[5] = MPPE_MPPC;
655 if (ccp_test(f->unit, opt_buf, CILEN_MPPE, 0) <= 0)
656 go->mppc = 0;
658 if (go->mppe_40) {
659 opt_buf[0] = CI_MPPE;
660 opt_buf[1] = CILEN_MPPE;
661 opt_buf[2] = MPPE_STATELESS;
662 opt_buf[3] = 0;
663 opt_buf[4] = 0;
664 opt_buf[5] = MPPE_40BIT;
665 if (ccp_test(f->unit, opt_buf, CILEN_MPPE + MPPE_MAX_KEY_LEN, 0) <= 0)
666 go->mppe_40 = 0;
668 if (go->mppe_56) {
669 opt_buf[0] = CI_MPPE;
670 opt_buf[1] = CILEN_MPPE;
671 opt_buf[2] = MPPE_STATELESS;
672 opt_buf[3] = 0;
673 opt_buf[4] = 0;
674 opt_buf[5] = MPPE_56BIT;
675 if (ccp_test(f->unit, opt_buf, CILEN_MPPE + MPPE_MAX_KEY_LEN, 0) <= 0)
676 go->mppe_56 = 0;
678 if (go->mppe_128) {
679 opt_buf[0] = CI_MPPE;
680 opt_buf[1] = CILEN_MPPE;
681 opt_buf[2] = MPPE_STATELESS;
682 opt_buf[3] = 0;
683 opt_buf[4] = 0;
684 opt_buf[5] = MPPE_128BIT;
685 if (ccp_test(f->unit, opt_buf, CILEN_MPPE + MPPE_MAX_KEY_LEN, 0) <= 0)
686 go->mppe_128 = 0;
688 if (!go->mppe_40 && !go->mppe_56 && !go->mppe_128) {
689 if (ccp_wantoptions[f->unit].mppe) {
690 error("MPPE required, but kernel has no support.");
691 lcp_close(f->unit, "MPPE required but not available");
693 go->mppe = go->mppe_stateless = 0;
694 } else {
695 /* MPPE is not compatible with other compression types */
696 if (ccp_wantoptions[f->unit].mppe) {
697 ao->bsd_compress = go->bsd_compress = 0;
698 ao->predictor_1 = go->predictor_1 = 0;
699 ao->predictor_2 = go->predictor_2 = 0;
700 ao->deflate = go->deflate = 0;
701 ao->lzs = go->lzs = 0;
705 #endif /* MPPE */
706 if (go->lzs) {
707 opt_buf[0] = CI_LZS;
708 opt_buf[1] = CILEN_LZS;
709 opt_buf[2] = go->lzs_hists >> 8;
710 opt_buf[3] = go->lzs_hists & 0xff;
711 opt_buf[4] = LZS_MODE_SEQ;
712 if (ccp_test(f->unit, opt_buf, CILEN_LZS, 0) <= 0)
713 go->lzs = 0;
715 if (go->bsd_compress) {
716 opt_buf[0] = CI_BSD_COMPRESS;
717 opt_buf[1] = CILEN_BSD_COMPRESS;
718 opt_buf[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, BSD_MIN_BITS);
719 if (ccp_test(f->unit, opt_buf, CILEN_BSD_COMPRESS, 0) <= 0)
720 go->bsd_compress = 0;
722 if (go->deflate) {
723 if (go->deflate_correct) {
724 opt_buf[0] = CI_DEFLATE;
725 opt_buf[1] = CILEN_DEFLATE;
726 opt_buf[2] = DEFLATE_MAKE_OPT(DEFLATE_MIN_WORKS);
727 opt_buf[3] = DEFLATE_CHK_SEQUENCE;
728 if (ccp_test(f->unit, opt_buf, CILEN_DEFLATE, 0) <= 0)
729 go->deflate_correct = 0;
731 if (go->deflate_draft) {
732 opt_buf[0] = CI_DEFLATE_DRAFT;
733 opt_buf[1] = CILEN_DEFLATE;
734 opt_buf[2] = DEFLATE_MAKE_OPT(DEFLATE_MIN_WORKS);
735 opt_buf[3] = DEFLATE_CHK_SEQUENCE;
736 if (ccp_test(f->unit, opt_buf, CILEN_DEFLATE, 0) <= 0)
737 go->deflate_draft = 0;
739 if (!go->deflate_correct && !go->deflate_draft)
740 go->deflate = 0;
742 if (go->predictor_1) {
743 opt_buf[0] = CI_PREDICTOR_1;
744 opt_buf[1] = CILEN_PREDICTOR_1;
745 if (ccp_test(f->unit, opt_buf, CILEN_PREDICTOR_1, 0) <= 0)
746 go->predictor_1 = 0;
748 if (go->predictor_2) {
749 opt_buf[0] = CI_PREDICTOR_2;
750 opt_buf[1] = CILEN_PREDICTOR_2;
751 if (ccp_test(f->unit, opt_buf, CILEN_PREDICTOR_2, 0) <= 0)
752 go->predictor_2 = 0;
757 * ccp_cilen - Return total length of our configuration info.
759 static int
760 ccp_cilen(f)
761 fsm *f;
763 ccp_options *go = &ccp_gotoptions[f->unit];
765 return (go->bsd_compress? CILEN_BSD_COMPRESS: 0)
766 + (go->deflate? CILEN_DEFLATE: 0)
767 + (go->predictor_1? CILEN_PREDICTOR_1: 0)
768 + (go->predictor_2? CILEN_PREDICTOR_2: 0)
769 + (go->lzs? CILEN_LZS: 0)
770 + ((go->mppe || go->mppc)? CILEN_MPPE: 0);
774 * ccp_addci - put our requests in a packet.
776 static void
777 ccp_addci(f, p, lenp)
778 fsm *f;
779 u_char *p;
780 int *lenp;
782 int res;
783 ccp_options *go = &ccp_gotoptions[f->unit];
784 ccp_options *ao = &ccp_allowoptions[f->unit];
785 ccp_options *wo = &ccp_wantoptions[f->unit];
786 u_char *p0 = p;
789 * Add the compression types that we can receive, in decreasing
790 * preference order. Get the kernel to allocate the first one
791 * in case it gets Acked.
793 #ifdef MPPE
794 if (go->mppe || go->mppc || (!wo->mppe && ao->mppe)) {
795 u_char opt_buf[CILEN_MPPE + MPPE_MAX_KEY_LEN];
797 p[0] = CI_MPPE;
798 p[1] = CILEN_MPPE;
799 p[2] = (go->mppe_stateless ? MPPE_STATELESS : 0);
800 p[3] = 0;
801 p[4] = 0;
802 p[5] = (go->mppe_40 ? MPPE_40BIT : 0) | (go->mppe_56 ? MPPE_56BIT : 0) |
803 (go->mppe_128 ? MPPE_128BIT : 0) | (go->mppc ? MPPE_MPPC : 0);
805 BCOPY(p, opt_buf, CILEN_MPPE);
806 BCOPY(mppe_recv_key, &opt_buf[CILEN_MPPE], MPPE_MAX_KEY_LEN);
807 res = ccp_test(f->unit, opt_buf, CILEN_MPPE + MPPE_MAX_KEY_LEN, 0);
808 if (res > 0) {
809 p += CILEN_MPPE;
810 } else {
811 /* This shouldn't happen, we've already tested it! */
812 go->mppe = go->mppe_40 = go->mppe_56 = go->mppe_128 =
813 go->mppe_stateless = go->mppc = 0;
814 if (ccp_wantoptions[f->unit].mppe)
815 lcp_close(f->unit, "MPPE required but not available in kernel");
818 #endif /* MPPE */
819 if (go->lzs) {
820 p[0] = CI_LZS;
821 p[1] = CILEN_LZS;
822 p[2] = go->lzs_hists >> 8;
823 p[3] = go->lzs_hists & 0xff;
824 p[4] = LZS_MODE_SEQ;
825 res = ccp_test(f->unit, p, CILEN_LZS, 0);
826 if (res > 0) {
827 p += CILEN_LZS;
828 } else
829 go->lzs = 0;
831 if (go->deflate) {
832 p[0] = go->deflate_correct? CI_DEFLATE: CI_DEFLATE_DRAFT;
833 p[1] = CILEN_DEFLATE;
834 p[2] = DEFLATE_MAKE_OPT(go->deflate_size);
835 p[3] = DEFLATE_CHK_SEQUENCE;
836 if (p != p0) {
837 p += CILEN_DEFLATE;
838 } else {
839 for (;;) {
840 if (go->deflate_size < DEFLATE_MIN_WORKS) {
841 go->deflate = 0;
842 break;
844 res = ccp_test(f->unit, p, CILEN_DEFLATE, 0);
845 if (res > 0) {
846 p += CILEN_DEFLATE;
847 break;
848 } else if (res < 0) {
849 go->deflate = 0;
850 break;
852 --go->deflate_size;
853 p[2] = DEFLATE_MAKE_OPT(go->deflate_size);
856 if (p != p0 && go->deflate_correct && go->deflate_draft) {
857 p[0] = CI_DEFLATE_DRAFT;
858 p[1] = CILEN_DEFLATE;
859 p[2] = p[2 - CILEN_DEFLATE];
860 p[3] = DEFLATE_CHK_SEQUENCE;
861 p += CILEN_DEFLATE;
864 if (go->bsd_compress) {
865 p[0] = CI_BSD_COMPRESS;
866 p[1] = CILEN_BSD_COMPRESS;
867 p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits);
868 if (p != p0) {
869 p += CILEN_BSD_COMPRESS; /* not the first option */
870 } else {
871 for (;;) {
872 if (go->bsd_bits < BSD_MIN_BITS) {
873 go->bsd_compress = 0;
874 break;
876 res = ccp_test(f->unit, p, CILEN_BSD_COMPRESS, 0);
877 if (res > 0) {
878 p += CILEN_BSD_COMPRESS;
879 break;
880 } else if (res < 0) {
881 go->bsd_compress = 0;
882 break;
884 --go->bsd_bits;
885 p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits);
889 /* XXX Should Predictor 2 be preferable to Predictor 1? */
890 if (go->predictor_1) {
891 p[0] = CI_PREDICTOR_1;
892 p[1] = CILEN_PREDICTOR_1;
893 if (p == p0 && ccp_test(f->unit, p, CILEN_PREDICTOR_1, 0) <= 0) {
894 go->predictor_1 = 0;
895 } else {
896 p += CILEN_PREDICTOR_1;
899 if (go->predictor_2) {
900 p[0] = CI_PREDICTOR_2;
901 p[1] = CILEN_PREDICTOR_2;
902 if (p == p0 && ccp_test(f->unit, p, CILEN_PREDICTOR_2, 0) <= 0) {
903 go->predictor_2 = 0;
904 } else {
905 p += CILEN_PREDICTOR_2;
909 go->method = (p > p0)? p0[0]: -1;
911 *lenp = p - p0;
915 * ccp_ackci - process a received configure-ack, and return
916 * 1 if the packet was OK.
918 static int
919 ccp_ackci(f, p, len)
920 fsm *f;
921 u_char *p;
922 int len;
924 ccp_options *go = &ccp_gotoptions[f->unit];
925 ccp_options *ao = &ccp_allowoptions[f->unit];
926 ccp_options *wo = &ccp_wantoptions[f->unit];
927 u_char *p0 = p;
929 #ifdef MPPE
930 if (go->mppe || go->mppc || (!wo->mppe && ao->mppe)) {
931 if (len < CILEN_MPPE
932 || p[1] != CILEN_MPPE || p[0] != CI_MPPE
933 || p[2] != (go->mppe_stateless ? MPPE_STATELESS : 0)
934 || p[3] != 0
935 || p[4] != 0
936 || (p[5] != ((go->mppe_40 ? MPPE_40BIT : 0) |
937 (go->mppc ? MPPE_MPPC : 0))
938 && p[5] != ((go->mppe_56 ? MPPE_56BIT : 0) |
939 (go->mppc ? MPPE_MPPC : 0))
940 && p[5] != ((go->mppe_128 ? MPPE_128BIT : 0) |
941 (go->mppc ? MPPE_MPPC : 0))))
942 return 0;
943 if (go->mppe_40 || go->mppe_56 || go->mppe_128)
944 go->mppe = 1;
945 p += CILEN_MPPE;
946 len -= CILEN_MPPE;
947 /* Cope with first/fast ack */
948 if (p == p0 && len == 0)
949 return 1;
951 #endif /* MPPE */
952 if (go->lzs) {
953 if (len < CILEN_LZS || p[0] != CI_LZS || p[1] != CILEN_LZS
954 || p[2] != go->lzs_hists>>8 || p[3] != (go->lzs_hists&0xff)
955 || p[4] != LZS_MODE_SEQ)
956 return 0;
957 p += CILEN_LZS;
958 len -= CILEN_LZS;
959 /* XXX Cope with first/fast ack */
960 if (p == p0 && len == 0)
961 return 1;
963 if (go->deflate) {
964 if (len < CILEN_DEFLATE
965 || p[0] != (go->deflate_correct? CI_DEFLATE: CI_DEFLATE_DRAFT)
966 || p[1] != CILEN_DEFLATE
967 || p[2] != DEFLATE_MAKE_OPT(go->deflate_size)
968 || p[3] != DEFLATE_CHK_SEQUENCE)
969 return 0;
970 p += CILEN_DEFLATE;
971 len -= CILEN_DEFLATE;
972 /* XXX Cope with first/fast ack */
973 if (len == 0)
974 return 1;
975 if (go->deflate_correct && go->deflate_draft) {
976 if (len < CILEN_DEFLATE
977 || p[0] != CI_DEFLATE_DRAFT
978 || p[1] != CILEN_DEFLATE
979 || p[2] != DEFLATE_MAKE_OPT(go->deflate_size)
980 || p[3] != DEFLATE_CHK_SEQUENCE)
981 return 0;
982 p += CILEN_DEFLATE;
983 len -= CILEN_DEFLATE;
986 if (go->bsd_compress) {
987 if (len < CILEN_BSD_COMPRESS
988 || p[0] != CI_BSD_COMPRESS || p[1] != CILEN_BSD_COMPRESS
989 || p[2] != BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits))
990 return 0;
991 p += CILEN_BSD_COMPRESS;
992 len -= CILEN_BSD_COMPRESS;
993 /* XXX Cope with first/fast ack */
994 if (p == p0 && len == 0)
995 return 1;
997 if (go->predictor_1) {
998 if (len < CILEN_PREDICTOR_1
999 || p[0] != CI_PREDICTOR_1 || p[1] != CILEN_PREDICTOR_1)
1000 return 0;
1001 p += CILEN_PREDICTOR_1;
1002 len -= CILEN_PREDICTOR_1;
1003 /* XXX Cope with first/fast ack */
1004 if (p == p0 && len == 0)
1005 return 1;
1007 if (go->predictor_2) {
1008 if (len < CILEN_PREDICTOR_2
1009 || p[0] != CI_PREDICTOR_2 || p[1] != CILEN_PREDICTOR_2)
1010 return 0;
1011 p += CILEN_PREDICTOR_2;
1012 len -= CILEN_PREDICTOR_2;
1013 /* XXX Cope with first/fast ack */
1014 if (p == p0 && len == 0)
1015 return 1;
1018 if (len != 0)
1019 return 0;
1020 return 1;
1024 * ccp_nakci - process received configure-nak.
1025 * Returns 1 if the nak was OK.
1027 static int
1028 ccp_nakci(f, p, len, treat_as_reject)
1029 fsm *f;
1030 u_char *p;
1031 int len;
1032 int treat_as_reject;
1034 ccp_options *go = &ccp_gotoptions[f->unit];
1035 ccp_options *ao = &ccp_allowoptions[f->unit];
1036 ccp_options *wo = &ccp_wantoptions[f->unit];
1037 ccp_options no; /* options we've seen already */
1038 ccp_options try; /* options to ask for next time */
1040 memset(&no, 0, sizeof(no));
1041 try = *go;
1043 #ifdef MPPE
1044 if ((go->mppe || go->mppc || (!wo->mppe && ao->mppe)) &&
1045 len >= CILEN_MPPE && p[0] == CI_MPPE && p[1] == CILEN_MPPE) {
1047 if (go->mppc) {
1048 no.mppc = 1;
1049 if (!(p[5] & MPPE_MPPC))
1050 try.mppc = 0;
1053 if (go->mppe)
1054 no.mppe = 1;
1055 if (go->mppe_40)
1056 no.mppe_40 = 1;
1057 if (go->mppe_56)
1058 no.mppe_56 = 1;
1059 if (go->mppe_128)
1060 no.mppe_128 = 1;
1061 if (go->mppe_stateless)
1062 no.mppe_stateless = 1;
1064 if (ao->mppe_40) {
1065 if ((p[5] & MPPE_40BIT))
1066 try.mppe_40 = 1;
1067 else
1068 try.mppe_40 = (p[5] == 0) ? 1 : 0;
1070 if (ao->mppe_56) {
1071 if ((p[5] & MPPE_56BIT))
1072 try.mppe_56 = 1;
1073 else
1074 try.mppe_56 = (p[5] == 0) ? 1 : 0;
1076 if (ao->mppe_128) {
1077 if ((p[5] & MPPE_128BIT))
1078 try.mppe_128 = 1;
1079 else
1080 try.mppe_128 = (p[5] == 0) ? 1 : 0;
1083 if (ao->mppe_stateless) {
1084 if ((p[2] & MPPE_STATELESS) || wo->mppe_stateless)
1085 try.mppe_stateless = 1;
1086 else
1087 try.mppe_stateless = 0;
1090 if (!try.mppe_56 && !try.mppe_40 && !try.mppe_128) {
1091 try.mppe = try.mppe_stateless = 0;
1092 if (wo->mppe) {
1093 /* we require encryption, but peer doesn't support it
1094 so we close connection */
1095 wo->mppc = wo->mppe = wo->mppe_stateless = wo->mppe_40 =
1096 wo->mppe_56 = wo->mppe_128 = 0;
1097 lcp_close(f->unit, "MPPE required but cannot negotiate MPPE "
1098 "key length");
1101 if (wo->mppe && (wo->mppe_40 != try.mppe_40) &&
1102 (wo->mppe_56 != try.mppe_56) && (wo->mppe_128 != try.mppe_128)) {
1103 /* cannot negotiate key length */
1104 wo->mppc = wo->mppe = wo->mppe_stateless = wo->mppe_40 =
1105 wo->mppe_56 = wo->mppe_128 = 0;
1106 lcp_close(f->unit, "Cannot negotiate MPPE key length");
1108 if (try.mppe_40 && try.mppe_56 && try.mppe_128)
1109 try.mppe_40 = try.mppe_56 = 0;
1110 else
1111 if (try.mppe_56 && try.mppe_128)
1112 try.mppe_56 = 0;
1113 else
1114 if (try.mppe_40 && try.mppe_128)
1115 try.mppe_40 = 0;
1116 else
1117 if (try.mppe_40 && try.mppe_56)
1118 try.mppe_40 = 0;
1120 p += CILEN_MPPE;
1121 len -= CILEN_MPPE;
1123 #endif /* MPPE */
1125 if (go->lzs && len >= CILEN_LZS && p[0] == CI_LZS && p[1] == CILEN_LZS) {
1126 no.lzs = 1;
1127 if (((p[2]<<8)|p[3]) > 1 || (p[4] != LZS_MODE_SEQ &&
1128 p[4] != LZS_MODE_EXT))
1129 try.lzs = 0;
1130 else {
1131 try.lzs_mode = p[4];
1132 try.lzs_hists = (p[2] << 8) | p[3];
1134 p += CILEN_LZS;
1135 len -= CILEN_LZS;
1138 if (go->deflate && len >= CILEN_DEFLATE
1139 && p[0] == (go->deflate_correct? CI_DEFLATE: CI_DEFLATE_DRAFT)
1140 && p[1] == CILEN_DEFLATE) {
1141 no.deflate = 1;
1143 * Peer wants us to use a different code size or something.
1144 * Stop asking for Deflate if we don't understand his suggestion.
1146 if (DEFLATE_METHOD(p[2]) != DEFLATE_METHOD_VAL
1147 || DEFLATE_SIZE(p[2]) < DEFLATE_MIN_WORKS
1148 || p[3] != DEFLATE_CHK_SEQUENCE)
1149 try.deflate = 0;
1150 else if (DEFLATE_SIZE(p[2]) < go->deflate_size)
1151 try.deflate_size = DEFLATE_SIZE(p[2]);
1152 p += CILEN_DEFLATE;
1153 len -= CILEN_DEFLATE;
1154 if (go->deflate_correct && go->deflate_draft
1155 && len >= CILEN_DEFLATE && p[0] == CI_DEFLATE_DRAFT
1156 && p[1] == CILEN_DEFLATE) {
1157 p += CILEN_DEFLATE;
1158 len -= CILEN_DEFLATE;
1162 if (go->bsd_compress && len >= CILEN_BSD_COMPRESS
1163 && p[0] == CI_BSD_COMPRESS && p[1] == CILEN_BSD_COMPRESS) {
1164 no.bsd_compress = 1;
1166 * Peer wants us to use a different number of bits
1167 * or a different version.
1169 if (BSD_VERSION(p[2]) != BSD_CURRENT_VERSION)
1170 try.bsd_compress = 0;
1171 else if (BSD_NBITS(p[2]) < go->bsd_bits)
1172 try.bsd_bits = BSD_NBITS(p[2]);
1173 p += CILEN_BSD_COMPRESS;
1174 len -= CILEN_BSD_COMPRESS;
1178 * Predictor-1 and 2 have no options, so they can't be Naked.
1180 * There may be remaining options but we ignore them.
1183 if (f->state != OPENED)
1184 *go = try;
1185 return 1;
1189 * ccp_rejci - reject some of our suggested compression methods.
1191 static int
1192 ccp_rejci(f, p, len)
1193 fsm *f;
1194 u_char *p;
1195 int len;
1197 ccp_options *go = &ccp_gotoptions[f->unit];
1198 ccp_options try; /* options to request next time */
1200 try = *go;
1203 * Cope with empty configure-rejects by ceasing to send
1204 * configure-requests.
1206 if (len == 0 && all_rejected[f->unit])
1207 return -1;
1209 #ifdef MPPE
1210 if ((go->mppe || go->mppc) && len >= CILEN_MPPE
1211 && p[0] == CI_MPPE && p[1] == CILEN_MPPE) {
1212 ccp_options *wo = &ccp_wantoptions[f->unit];
1213 if (p[2] != (go->mppe_stateless ? MPPE_STATELESS : 0) ||
1214 p[3] != 0 ||
1215 p[4] != 0 ||
1216 p[5] != ((go->mppe_40 ? MPPE_40BIT : 0) |
1217 (go->mppe_56 ? MPPE_56BIT : 0) |
1218 (go->mppe_128 ? MPPE_128BIT : 0) |
1219 (go->mppc ? MPPE_MPPC : 0)))
1220 return 0;
1221 if (go->mppc)
1222 try.mppc = 0;
1223 if (go->mppe) {
1224 try.mppe = 0;
1225 if (go->mppe_40)
1226 try.mppe_40 = 0;
1227 if (go->mppe_56)
1228 try.mppe_56 = 0;
1229 if (go->mppe_128)
1230 try.mppe_128 = 0;
1231 if (go->mppe_stateless)
1232 try.mppe_stateless = 0;
1233 if (!try.mppe_56 && !try.mppe_40 && !try.mppe_128)
1234 try.mppe = try.mppe_stateless = 0;
1235 if (wo->mppe) { /* we want MPPE but cannot negotiate key length */
1236 wo->mppc = wo->mppe = wo->mppe_stateless = wo->mppe_40 =
1237 wo->mppe_56 = wo->mppe_128 = 0;
1238 lcp_close(f->unit, "MPPE required but cannot negotiate MPPE "
1239 "key length");
1242 p += CILEN_MPPE;
1243 len -= CILEN_MPPE;
1245 #endif /* MPPE */
1246 if (go->lzs && len >= CILEN_LZS && p[0] == CI_LZS && p[1] == CILEN_LZS) {
1247 if (p[2] != go->lzs_hists>>8 || p[3] != (go->lzs_hists&0xff)
1248 || p[4] != go->lzs_mode)
1249 return 0;
1250 try.lzs = 0;
1251 p += CILEN_LZS;
1252 len -= CILEN_LZS;
1254 if (go->deflate_correct && len >= CILEN_DEFLATE
1255 && p[0] == CI_DEFLATE && p[1] == CILEN_DEFLATE) {
1256 if (p[2] != DEFLATE_MAKE_OPT(go->deflate_size)
1257 || p[3] != DEFLATE_CHK_SEQUENCE)
1258 return 0; /* Rej is bad */
1259 try.deflate_correct = 0;
1260 p += CILEN_DEFLATE;
1261 len -= CILEN_DEFLATE;
1263 if (go->deflate_draft && len >= CILEN_DEFLATE
1264 && p[0] == CI_DEFLATE_DRAFT && p[1] == CILEN_DEFLATE) {
1265 if (p[2] != DEFLATE_MAKE_OPT(go->deflate_size)
1266 || p[3] != DEFLATE_CHK_SEQUENCE)
1267 return 0; /* Rej is bad */
1268 try.deflate_draft = 0;
1269 p += CILEN_DEFLATE;
1270 len -= CILEN_DEFLATE;
1272 if (!try.deflate_correct && !try.deflate_draft)
1273 try.deflate = 0;
1274 if (go->bsd_compress && len >= CILEN_BSD_COMPRESS
1275 && p[0] == CI_BSD_COMPRESS && p[1] == CILEN_BSD_COMPRESS) {
1276 if (p[2] != BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits))
1277 return 0;
1278 try.bsd_compress = 0;
1279 p += CILEN_BSD_COMPRESS;
1280 len -= CILEN_BSD_COMPRESS;
1282 if (go->predictor_1 && len >= CILEN_PREDICTOR_1
1283 && p[0] == CI_PREDICTOR_1 && p[1] == CILEN_PREDICTOR_1) {
1284 try.predictor_1 = 0;
1285 p += CILEN_PREDICTOR_1;
1286 len -= CILEN_PREDICTOR_1;
1288 if (go->predictor_2 && len >= CILEN_PREDICTOR_2
1289 && p[0] == CI_PREDICTOR_2 && p[1] == CILEN_PREDICTOR_2) {
1290 try.predictor_2 = 0;
1291 p += CILEN_PREDICTOR_2;
1292 len -= CILEN_PREDICTOR_2;
1295 if (len != 0)
1296 return 0;
1298 if (f->state != OPENED)
1299 *go = try;
1301 return 1;
1305 * ccp_reqci - processed a received configure-request.
1306 * Returns CONFACK, CONFNAK or CONFREJ and the packet modified
1307 * appropriately.
1309 static int
1310 ccp_reqci(f, p, lenp, dont_nak)
1311 fsm *f;
1312 u_char *p;
1313 int *lenp;
1314 int dont_nak;
1316 int ret, newret, res;
1317 u_char *p0, *retp, p2, p5;
1318 int len, clen, type, nb;
1319 ccp_options *ho = &ccp_hisoptions[f->unit];
1320 ccp_options *ao = &ccp_allowoptions[f->unit];
1321 ccp_options *wo = &ccp_wantoptions[f->unit];
1322 #ifdef MPPE
1323 u_char opt_buf[CILEN_MPPE + MPPE_MAX_KEY_LEN];
1324 /* int mtu; */
1325 #endif /* MPPE */
1327 ret = CONFACK;
1328 retp = p0 = p;
1329 len = *lenp;
1331 memset(ho, 0, sizeof(ccp_options));
1332 ho->method = (len > 0)? p[0]: -1;
1334 while (len > 0) {
1335 newret = CONFACK;
1336 if (len < 2 || p[1] < 2 || p[1] > len) {
1337 /* length is bad */
1338 clen = len;
1339 newret = CONFREJ;
1341 } else {
1342 type = p[0];
1343 clen = p[1];
1345 switch (type) {
1346 #ifdef MPPE
1347 case CI_MPPE:
1348 if ((!ao->mppc && !ao->mppe) || clen != CILEN_MPPE) {
1349 newret = CONFREJ;
1350 break;
1352 p2 = p[2];
1353 p5 = p[5];
1354 /* not sure what they want, tell 'em what we got */
1355 if (((p[2] & ~MPPE_STATELESS) != 0 || p[3] != 0 || p[4] != 0 ||
1356 (p[5] & ~(MPPE_40BIT | MPPE_56BIT | MPPE_128BIT |
1357 MPPE_MPPC)) != 0 || p[5] == 0) ||
1358 (p[2] == 0 && p[3] == 0 && p[4] == 0 && p[5] == 0)) {
1359 newret = CONFNAK;
1362 if ((p[5] & MPPE_MPPC)) {
1363 if (ao->mppc) {
1364 ho->mppc = 1;
1365 BCOPY(p, opt_buf, CILEN_MPPE);
1366 opt_buf[2] = opt_buf[3] = opt_buf[4] = 0;
1367 opt_buf[5] = MPPE_MPPC;
1368 if (ccp_test(f->unit, opt_buf, CILEN_MPPE, 1) <= 0) {
1369 ho->mppc = 0;
1370 p[5] &= ~MPPE_MPPC;
1371 newret = CONFNAK;
1373 } else {
1374 newret = CONFREJ;
1375 if (wo->mppe || ao->mppe) {
1376 p[5] &= ~MPPE_MPPC;
1377 newret = CONFNAK;
1382 if (ao->mppe)
1383 ho->mppe = 1;
1385 if ((p[2] & MPPE_STATELESS)) {
1386 if (ao->mppe_stateless) {
1387 if (wo->mppe_stateless)
1388 ho->mppe_stateless = 1;
1389 else {
1390 newret = CONFNAK;
1391 if (!dont_nak)
1392 p[2] &= ~MPPE_STATELESS;
1394 } else {
1395 newret = CONFNAK;
1396 if (!dont_nak)
1397 p[2] &= ~MPPE_STATELESS;
1399 } else {
1400 if (wo->mppe_stateless && !dont_nak) {
1401 wo->mppe_stateless = 0;
1402 newret = CONFNAK;
1403 p[2] |= MPPE_STATELESS;
1407 if ((p[5] & ~MPPE_MPPC) == (MPPE_40BIT|MPPE_56BIT|MPPE_128BIT)) {
1408 newret = CONFNAK;
1409 if (ao->mppe_128) {
1410 ho->mppe_128 = 1;
1411 p[5] &= ~(MPPE_40BIT|MPPE_56BIT);
1412 BCOPY(p, opt_buf, CILEN_MPPE);
1413 BCOPY(mppe_send_key, &opt_buf[CILEN_MPPE],
1414 MPPE_MAX_KEY_LEN);
1415 if (ccp_test(f->unit, opt_buf, CILEN_MPPE +
1416 MPPE_MAX_KEY_LEN, 1) <= 0) {
1417 ho->mppe_128 = 0;
1418 p[5] |= (MPPE_40BIT|MPPE_56BIT);
1419 p[5] &= ~MPPE_128BIT;
1420 goto check_mppe_56_40;
1422 goto check_mppe;
1424 p[5] &= ~MPPE_128BIT;
1425 goto check_mppe_56_40;
1427 if ((p[5] & ~MPPE_MPPC) == (MPPE_56BIT|MPPE_128BIT)) {
1428 newret = CONFNAK;
1429 if (ao->mppe_128) {
1430 ho->mppe_128 = 1;
1431 p[5] &= ~MPPE_56BIT;
1432 BCOPY(p, opt_buf, CILEN_MPPE);
1433 BCOPY(mppe_send_key, &opt_buf[CILEN_MPPE],
1434 MPPE_MAX_KEY_LEN);
1435 if (ccp_test(f->unit, opt_buf, CILEN_MPPE +
1436 MPPE_MAX_KEY_LEN, 1) <= 0) {
1437 ho->mppe_128 = 0;
1438 p[5] |= MPPE_56BIT;
1439 p[5] &= ~MPPE_128BIT;
1440 goto check_mppe_56;
1442 goto check_mppe;
1444 p[5] &= ~MPPE_128BIT;
1445 goto check_mppe_56;
1447 if ((p[5] & ~MPPE_MPPC) == (MPPE_40BIT|MPPE_128BIT)) {
1448 newret = CONFNAK;
1449 if (ao->mppe_128) {
1450 ho->mppe_128 = 1;
1451 p[5] &= ~MPPE_40BIT;
1452 BCOPY(p, opt_buf, CILEN_MPPE);
1453 BCOPY(mppe_send_key, &opt_buf[CILEN_MPPE],
1454 MPPE_MAX_KEY_LEN);
1455 if (ccp_test(f->unit, opt_buf, CILEN_MPPE +
1456 MPPE_MAX_KEY_LEN, 1) <= 0) {
1457 ho->mppe_128 = 0;
1458 p[5] |= MPPE_40BIT;
1459 p[5] &= ~MPPE_128BIT;
1460 goto check_mppe_40;
1462 goto check_mppe;
1464 p[5] &= ~MPPE_128BIT;
1465 goto check_mppe_40;
1467 if ((p[5] & ~MPPE_MPPC) == MPPE_128BIT) {
1468 if (ao->mppe_128) {
1469 ho->mppe_128 = 1;
1470 BCOPY(p, opt_buf, CILEN_MPPE);
1471 BCOPY(mppe_send_key, &opt_buf[CILEN_MPPE],
1472 MPPE_MAX_KEY_LEN);
1473 if (ccp_test(f->unit, opt_buf, CILEN_MPPE +
1474 MPPE_MAX_KEY_LEN, 1) <= 0) {
1475 ho->mppe_128 = 0;
1476 p[5] &= ~MPPE_128BIT;
1477 newret = CONFNAK;
1479 goto check_mppe;
1481 p[5] &= ~MPPE_128BIT;
1482 newret = CONFNAK;
1483 goto check_mppe;
1485 check_mppe_56_40:
1486 if ((p[5] & ~MPPE_MPPC) == (MPPE_40BIT|MPPE_56BIT)) {
1487 newret = CONFNAK;
1488 if (ao->mppe_56) {
1489 ho->mppe_56 = 1;
1490 p[5] &= ~MPPE_40BIT;
1491 BCOPY(p, opt_buf, CILEN_MPPE);
1492 BCOPY(mppe_send_key, &opt_buf[CILEN_MPPE],
1493 MPPE_MAX_KEY_LEN);
1494 if (ccp_test(f->unit, opt_buf, CILEN_MPPE +
1495 MPPE_MAX_KEY_LEN, 1) <= 0) {
1496 ho->mppe_56 = 0;
1497 p[5] |= MPPE_40BIT;
1498 p[5] &= ~MPPE_56BIT;
1499 newret = CONFNAK;
1500 goto check_mppe_40;
1502 goto check_mppe;
1504 p[5] &= ~MPPE_56BIT;
1505 goto check_mppe_40;
1507 check_mppe_56:
1508 if ((p[5] & ~MPPE_MPPC) == MPPE_56BIT) {
1509 if (ao->mppe_56) {
1510 ho->mppe_56 = 1;
1511 BCOPY(p, opt_buf, CILEN_MPPE);
1512 BCOPY(mppe_send_key, &opt_buf[CILEN_MPPE],
1513 MPPE_MAX_KEY_LEN);
1514 if (ccp_test(f->unit, opt_buf, CILEN_MPPE +
1515 MPPE_MAX_KEY_LEN, 1) <= 0) {
1516 ho->mppe_56 = 0;
1517 p[5] &= ~MPPE_56BIT;
1518 newret = CONFNAK;
1520 goto check_mppe;
1522 p[5] &= ~MPPE_56BIT;
1523 newret = CONFNAK;
1524 goto check_mppe;
1526 check_mppe_40:
1527 if ((p[5] & ~MPPE_MPPC) == MPPE_40BIT) {
1528 if (ao->mppe_40) {
1529 ho->mppe_40 = 1;
1530 BCOPY(p, opt_buf, CILEN_MPPE);
1531 BCOPY(mppe_send_key, &opt_buf[CILEN_MPPE],
1532 MPPE_MAX_KEY_LEN);
1533 if (ccp_test(f->unit, opt_buf, CILEN_MPPE +
1534 MPPE_MAX_KEY_LEN, 1) <= 0) {
1535 ho->mppe_40 = 0;
1536 p[5] &= ~MPPE_40BIT;
1537 newret = CONFNAK;
1539 goto check_mppe;
1541 p[5] &= ~MPPE_40BIT;
1544 check_mppe:
1545 if (!ho->mppe_40 && !ho->mppe_56 && !ho->mppe_128) {
1546 if (wo->mppe_40 || wo->mppe_56 || wo->mppe_128) {
1547 newret = CONFNAK;
1548 p[2] |= (wo->mppe_stateless ? MPPE_STATELESS : 0);
1549 p[5] |= (wo->mppe_40 ? MPPE_40BIT : 0) |
1550 (wo->mppe_56 ? MPPE_56BIT : 0) |
1551 (wo->mppe_128 ? MPPE_128BIT : 0) |
1552 (wo->mppc ? MPPE_MPPC : 0);
1553 } else {
1554 ho->mppe = ho->mppe_stateless = 0;
1556 } else {
1557 /* MPPE is not compatible with other compression types */
1558 if (wo->mppe) {
1559 ao->bsd_compress = 0;
1560 ao->predictor_1 = 0;
1561 ao->predictor_2 = 0;
1562 ao->deflate = 0;
1563 ao->lzs = 0;
1566 if ((!ho->mppc || !ao->mppc) && !ho->mppe) {
1567 p[2] = p2;
1568 p[5] = p5;
1569 /* We cannot accept this. */
1570 newret = CONFREJ;
1571 /* Give the peer our idea of what can be used,
1572 so it can choose and confirm */
1573 ho->mppe = ao->mppe;
1577 * I have commented the code below because according to RFC1547
1578 * MTU is only information for higher level protocols about
1579 * "the maximum allowable length for a packet (q.v.) transmitted
1580 * over a point-to-point link without incurring network layer
1581 * fragmentation." Of course a PPP implementation should be able
1582 * to handle overhead added by MPPE - in our case apropriate code
1583 * is located in drivers/net/ppp_generic.c in the kernel sources.
1585 * According to RFC1661:
1586 * - when negotiated MRU is less than 1500 octets, a PPP
1587 * implementation must still be able to receive at least 1500
1588 * octets,
1589 * - when PFC is negotiated, a PPP implementation is still
1590 * required to receive frames with uncompressed protocol field.
1592 * So why not to handle MPPE overhead without changing MTU value?
1593 * I am sure that RFC3078, unfortunately silently, assumes that.
1597 * We need to decrease the interface MTU by MPPE_PAD
1598 * because MPPE frames **grow**. The kernel [must]
1599 * allocate MPPE_PAD extra bytes in xmit buffers.
1602 mtu = netif_get_mtu(f->unit);
1603 if (mtu) {
1604 netif_set_mtu(f->unit, mtu - MPPE_PAD);
1605 } else {
1606 newret = CONFREJ;
1607 if (ccp_wantoptions[f->unit].mppe) {
1608 error("Cannot adjust MTU needed by MPPE.");
1609 lcp_close(f->unit, "Cannot adjust MTU needed by MPPE.");
1613 break;
1614 #endif /* MPPE */
1616 case CI_LZS:
1617 if (!ao->lzs || clen != CILEN_LZS) {
1618 newret = CONFREJ;
1619 break;
1622 ho->lzs = 1;
1623 ho->lzs_hists = (p[2] << 8) | p[3];
1624 ho->lzs_mode = p[4];
1625 if ((ho->lzs_hists != ao->lzs_hists) ||
1626 (ho->lzs_mode != ao->lzs_mode)) {
1627 newret = CONFNAK;
1628 if (!dont_nak) {
1629 p[2] = ao->lzs_hists >> 8;
1630 p[3] = ao->lzs_hists & 0xff;
1631 p[4] = ao->lzs_mode;
1632 } else
1633 break;
1636 if (p == p0 && ccp_test(f->unit, p, CILEN_LZS, 1) <= 0) {
1637 newret = CONFREJ;
1639 break;
1641 case CI_DEFLATE:
1642 case CI_DEFLATE_DRAFT:
1643 if (!ao->deflate || clen != CILEN_DEFLATE
1644 || (!ao->deflate_correct && type == CI_DEFLATE)
1645 || (!ao->deflate_draft && type == CI_DEFLATE_DRAFT)) {
1646 newret = CONFREJ;
1647 break;
1650 ho->deflate = 1;
1651 ho->deflate_size = nb = DEFLATE_SIZE(p[2]);
1652 if (DEFLATE_METHOD(p[2]) != DEFLATE_METHOD_VAL
1653 || p[3] != DEFLATE_CHK_SEQUENCE
1654 || nb > ao->deflate_size || nb < DEFLATE_MIN_WORKS) {
1655 newret = CONFNAK;
1656 if (!dont_nak) {
1657 p[2] = DEFLATE_MAKE_OPT(ao->deflate_size);
1658 p[3] = DEFLATE_CHK_SEQUENCE;
1659 /* fall through to test this #bits below */
1660 } else
1661 break;
1665 * Check whether we can do Deflate with the window
1666 * size they want. If the window is too big, reduce
1667 * it until the kernel can cope and nak with that.
1668 * We only check this for the first option.
1670 if (p == p0) {
1671 for (;;) {
1672 res = ccp_test(f->unit, p, CILEN_DEFLATE, 1);
1673 if (res > 0)
1674 break; /* it's OK now */
1675 if (res < 0 || nb == DEFLATE_MIN_WORKS || dont_nak) {
1676 newret = CONFREJ;
1677 p[2] = DEFLATE_MAKE_OPT(ho->deflate_size);
1678 break;
1680 newret = CONFNAK;
1681 --nb;
1682 p[2] = DEFLATE_MAKE_OPT(nb);
1685 break;
1687 case CI_BSD_COMPRESS:
1688 if (!ao->bsd_compress || clen != CILEN_BSD_COMPRESS) {
1689 newret = CONFREJ;
1690 break;
1693 ho->bsd_compress = 1;
1694 ho->bsd_bits = nb = BSD_NBITS(p[2]);
1695 if (BSD_VERSION(p[2]) != BSD_CURRENT_VERSION
1696 || nb > ao->bsd_bits || nb < BSD_MIN_BITS) {
1697 newret = CONFNAK;
1698 if (!dont_nak) {
1699 p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, ao->bsd_bits);
1700 /* fall through to test this #bits below */
1701 } else
1702 break;
1706 * Check whether we can do BSD-Compress with the code
1707 * size they want. If the code size is too big, reduce
1708 * it until the kernel can cope and nak with that.
1709 * We only check this for the first option.
1711 if (p == p0) {
1712 for (;;) {
1713 res = ccp_test(f->unit, p, CILEN_BSD_COMPRESS, 1);
1714 if (res > 0)
1715 break;
1716 if (res < 0 || nb == BSD_MIN_BITS || dont_nak) {
1717 newret = CONFREJ;
1718 p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION,
1719 ho->bsd_bits);
1720 break;
1722 newret = CONFNAK;
1723 --nb;
1724 p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, nb);
1727 break;
1729 case CI_PREDICTOR_1:
1730 if (!ao->predictor_1 || clen != CILEN_PREDICTOR_1) {
1731 newret = CONFREJ;
1732 break;
1735 ho->predictor_1 = 1;
1736 if (p == p0
1737 && ccp_test(f->unit, p, CILEN_PREDICTOR_1, 1) <= 0) {
1738 newret = CONFREJ;
1740 break;
1742 case CI_PREDICTOR_2:
1743 if (!ao->predictor_2 || clen != CILEN_PREDICTOR_2) {
1744 newret = CONFREJ;
1745 break;
1748 ho->predictor_2 = 1;
1749 if (p == p0
1750 && ccp_test(f->unit, p, CILEN_PREDICTOR_2, 1) <= 0) {
1751 newret = CONFREJ;
1753 break;
1755 default:
1756 newret = CONFREJ;
1760 if (newret == CONFNAK && dont_nak)
1761 newret = CONFREJ;
1762 if (!(newret == CONFACK || (newret == CONFNAK && ret == CONFREJ))) {
1763 /* we're returning this option */
1764 if (newret == CONFREJ && ret == CONFNAK)
1765 retp = p0;
1766 ret = newret;
1767 if (p != retp)
1768 BCOPY(p, retp, clen);
1769 retp += clen;
1772 p += clen;
1773 len -= clen;
1776 if (ret != CONFACK) {
1777 if (ret == CONFREJ && *lenp == retp - p0)
1778 all_rejected[f->unit] = 1;
1779 else
1780 *lenp = retp - p0;
1782 return ret;
1786 * Make a string name for a compression method (or 2).
1788 static char *
1789 method_name(opt, opt2)
1790 ccp_options *opt, *opt2;
1792 static char result[64];
1794 if (!ANY_COMPRESS(*opt))
1795 return "(none)";
1796 switch (opt->method) {
1797 #ifdef MPPE
1798 case CI_MPPE:
1800 char *p = result;
1801 char *q = result + sizeof(result); /* 1 past result */
1803 if (opt->mppe) {
1804 if (opt->mppc) {
1805 slprintf(p, q - p, "MPPC/MPPE ");
1806 p += 10;
1807 } else {
1808 slprintf(p, q - p, "MPPE ");
1809 p += 5;
1811 if (opt->mppe_128) {
1812 slprintf(p, q - p, "128-bit ");
1813 p += 8;
1814 } else if (opt->mppe_56) {
1815 slprintf(p, q - p, "56-bit ");
1816 p += 7;
1817 } else if (opt->mppe_40) {
1818 slprintf(p, q - p, "40-bit ");
1819 p += 7;
1821 if (opt->mppe_stateless)
1822 slprintf(p, q - p, "stateless");
1823 else
1824 slprintf(p, q - p, "stateful");
1825 } else if (opt->mppc)
1826 slprintf(p, q - p, "MPPC");
1827 break;
1829 #endif /* MPPE */
1830 case CI_LZS:
1831 return "Stac LZS";
1832 case CI_DEFLATE:
1833 case CI_DEFLATE_DRAFT:
1834 if (opt2 != NULL && opt2->deflate_size != opt->deflate_size)
1835 slprintf(result, sizeof(result), "Deflate%s (%d/%d)",
1836 (opt->method == CI_DEFLATE_DRAFT? "(old#)": ""),
1837 opt->deflate_size, opt2->deflate_size);
1838 else
1839 slprintf(result, sizeof(result), "Deflate%s (%d)",
1840 (opt->method == CI_DEFLATE_DRAFT? "(old#)": ""),
1841 opt->deflate_size);
1842 break;
1843 case CI_BSD_COMPRESS:
1844 if (opt2 != NULL && opt2->bsd_bits != opt->bsd_bits)
1845 slprintf(result, sizeof(result), "BSD-Compress (%d/%d)",
1846 opt->bsd_bits, opt2->bsd_bits);
1847 else
1848 slprintf(result, sizeof(result), "BSD-Compress (%d)",
1849 opt->bsd_bits);
1850 break;
1851 case CI_PREDICTOR_1:
1852 return "Predictor 1";
1853 case CI_PREDICTOR_2:
1854 return "Predictor 2";
1855 default:
1856 slprintf(result, sizeof(result), "Method %d", opt->method);
1858 return result;
1862 * CCP has come up - inform the kernel driver and log a message.
1864 static void
1865 ccp_up(f)
1866 fsm *f;
1868 ccp_options *go = &ccp_gotoptions[f->unit];
1869 ccp_options *ho = &ccp_hisoptions[f->unit];
1870 char method1[64];
1872 ccp_flags_set(f->unit, 1, 1);
1873 if (ANY_COMPRESS(*go)) {
1874 if (ANY_COMPRESS(*ho)) {
1875 if (go->method == ho->method) {
1876 notice("%s compression enabled", method_name(go, ho));
1877 } else {
1878 strlcpy(method1, method_name(go, NULL), sizeof(method1));
1879 notice("%s / %s compression enabled",
1880 method1, method_name(ho, NULL));
1882 } else
1883 notice("%s receive compression enabled", method_name(go, NULL));
1884 } else if (ANY_COMPRESS(*ho))
1885 notice("%s transmit compression enabled", method_name(ho, NULL));
1886 #ifdef MPPE
1887 if (go->mppe || go->mppc) {
1888 BZERO(mppe_recv_key, MPPE_MAX_KEY_LEN);
1889 BZERO(mppe_send_key, MPPE_MAX_KEY_LEN);
1890 continue_networks(f->unit); /* Bring up IP et al */
1892 #endif /* MPPE */
1896 * CCP has gone down - inform the kernel driver.
1898 static void
1899 ccp_down(f)
1900 fsm *f;
1902 if (ccp_localstate[f->unit] & RACK_PENDING)
1903 UNTIMEOUT(ccp_rack_timeout, f);
1904 ccp_localstate[f->unit] = 0;
1905 ccp_flags_set(f->unit, 1, 0);
1906 #ifdef MPPE
1907 if (ccp_gotoptions[f->unit].mppe) {
1908 ccp_gotoptions[f->unit].mppe = 0;
1909 if (lcp_fsm[f->unit].state == OPENED) {
1910 /* If LCP is not already going down, make sure it does. */
1911 error("MPPE disabled");
1912 lcp_close(f->unit, "MPPE disabled");
1915 #endif /* MPPE */
1919 * Print the contents of a CCP packet.
1921 static char *ccp_codenames[] = {
1922 "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1923 "TermReq", "TermAck", "CodeRej",
1924 NULL, NULL, NULL, NULL, NULL, NULL,
1925 "ResetReq", "ResetAck",
1928 static int
1929 ccp_printpkt(p, plen, printer, arg)
1930 u_char *p;
1931 int plen;
1932 void (*printer) __P((void *, char *, ...));
1933 void *arg;
1935 u_char *p0, *optend;
1936 int code, id, len;
1937 int optlen;
1939 p0 = p;
1940 if (plen < HEADERLEN)
1941 return 0;
1942 code = p[0];
1943 id = p[1];
1944 len = (p[2] << 8) + p[3];
1945 if (len < HEADERLEN || len > plen)
1946 return 0;
1948 if (code >= 1 && code <= sizeof(ccp_codenames) / sizeof(char *)
1949 && ccp_codenames[code-1] != NULL)
1950 printer(arg, " %s", ccp_codenames[code-1]);
1951 else
1952 printer(arg, " code=0x%x", code);
1953 printer(arg, " id=0x%x", id);
1954 len -= HEADERLEN;
1955 p += HEADERLEN;
1957 switch (code) {
1958 case CONFREQ:
1959 case CONFACK:
1960 case CONFNAK:
1961 case CONFREJ:
1962 /* print list of possible compression methods */
1963 while (len >= 2) {
1964 code = p[0];
1965 optlen = p[1];
1966 if (optlen < 2 || optlen > len)
1967 break;
1968 printer(arg, " <");
1969 len -= optlen;
1970 optend = p + optlen;
1971 switch (code) {
1972 #ifdef MPPE
1973 case CI_MPPE:
1974 if (optlen >= CILEN_MPPE) {
1975 printer(arg, "mppe %s %s %s %s %s %s",
1976 (p[2] & MPPE_STATELESS)? "+H": "-H",
1977 (p[5] & MPPE_56BIT)? "+M": "-M",
1978 (p[5] & MPPE_128BIT)? "+S": "-S",
1979 (p[5] & MPPE_40BIT)? "+L": "-L",
1980 (p[5] & MPPE_D_BIT)? "+D": "-D",
1981 (p[5] & MPPE_MPPC)? "+C": "-C");
1982 if ((p[5] & ~(MPPE_56BIT | MPPE_128BIT | MPPE_40BIT |
1983 MPPE_D_BIT | MPPE_MPPC)) ||
1984 (p[2] & ~MPPE_STATELESS))
1985 printer(arg, " (%.2x %.2x %.2x %.2x)",
1986 p[2], p[3], p[4], p[5]);
1987 p += CILEN_MPPE;
1989 break;
1990 #endif /* MPPE */
1991 case CI_LZS:
1992 if (optlen >= CILEN_LZS) {
1993 printer(arg, "lzs %.2x %.2x %.2x", p[2], p[3], p[4]);
1994 p += CILEN_LZS;
1996 break;
1997 case CI_DEFLATE:
1998 case CI_DEFLATE_DRAFT:
1999 if (optlen >= CILEN_DEFLATE) {
2000 printer(arg, "deflate%s %d",
2001 (code == CI_DEFLATE_DRAFT? "(old#)": ""),
2002 DEFLATE_SIZE(p[2]));
2003 if (DEFLATE_METHOD(p[2]) != DEFLATE_METHOD_VAL)
2004 printer(arg, " method %d", DEFLATE_METHOD(p[2]));
2005 if (p[3] != DEFLATE_CHK_SEQUENCE)
2006 printer(arg, " check %d", p[3]);
2007 p += CILEN_DEFLATE;
2009 break;
2010 case CI_BSD_COMPRESS:
2011 if (optlen >= CILEN_BSD_COMPRESS) {
2012 printer(arg, "bsd v%d %d", BSD_VERSION(p[2]),
2013 BSD_NBITS(p[2]));
2014 p += CILEN_BSD_COMPRESS;
2016 break;
2017 case CI_PREDICTOR_1:
2018 if (optlen >= CILEN_PREDICTOR_1) {
2019 printer(arg, "predictor 1");
2020 p += CILEN_PREDICTOR_1;
2022 break;
2023 case CI_PREDICTOR_2:
2024 if (optlen >= CILEN_PREDICTOR_2) {
2025 printer(arg, "predictor 2");
2026 p += CILEN_PREDICTOR_2;
2028 break;
2030 while (p < optend)
2031 printer(arg, " %.2x", *p++);
2032 printer(arg, ">");
2034 break;
2036 case TERMACK:
2037 case TERMREQ:
2038 if (len > 0 && *p >= ' ' && *p < 0x7f) {
2039 print_string((char *)p, len, printer, arg);
2040 p += len;
2041 len = 0;
2043 break;
2046 /* dump out the rest of the packet in hex */
2047 while (--len >= 0)
2048 printer(arg, " %.2x", *p++);
2050 return p - p0;
2054 * We have received a packet that the decompressor failed to
2055 * decompress. Here we would expect to issue a reset-request, but
2056 * Motorola has a patent on resetting the compressor as a result of
2057 * detecting an error in the decompressed data after decompression.
2058 * (See US patent 5,130,993; international patent publication number
2059 * WO 91/10289; Australian patent 73296/91.)
2061 * So we ask the kernel whether the error was detected after
2062 * decompression; if it was, we take CCP down, thus disabling
2063 * compression :-(, otherwise we issue the reset-request.
2065 static void
2066 ccp_datainput(unit, pkt, len)
2067 int unit;
2068 u_char *pkt;
2069 int len;
2071 fsm *f;
2073 f = &ccp_fsm[unit];
2074 if (f->state == OPENED) {
2075 if (ccp_fatal_error(unit)) {
2077 * Disable compression by taking CCP down.
2079 error("Lost compression sync: disabling compression");
2080 ccp_close(unit, "Lost compression sync");
2081 #ifdef MPPE
2082 /* My module dosn't need this. J.D., 2003-07-06 */
2084 * If we were doing MPPE, we must also take the link down.
2086 if (ccp_gotoptions[unit].mppe) {
2087 error("Too many MPPE errors, closing LCP");
2088 lcp_close(unit, "Too many MPPE errors");
2090 #endif /* MPPE */
2091 } else {
2093 * When LZS or MPPE/MPPC is negotiated we just send CCP_RESETREQ
2094 * and don't wait for CCP_RESETACK
2096 if ((ccp_gotoptions[f->unit].method == CI_LZS) ||
2097 (ccp_gotoptions[f->unit].method == CI_MPPE)) {
2098 fsm_sdata(f, CCP_RESETREQ, f->reqid = ++f->id, NULL, 0);
2099 return;
2102 * Send a reset-request to reset the peer's compressor.
2103 * We don't do that if we are still waiting for an
2104 * acknowledgement to a previous reset-request.
2106 if (!(ccp_localstate[f->unit] & RACK_PENDING)) {
2107 fsm_sdata(f, CCP_RESETREQ, f->reqid = ++f->id, NULL, 0);
2108 TIMEOUT(ccp_rack_timeout, f, RACKTIMEOUT);
2109 ccp_localstate[f->unit] |= RACK_PENDING;
2110 } else
2111 ccp_localstate[f->unit] |= RREQ_REPEAT;
2117 * Timeout waiting for reset-ack.
2119 static void
2120 ccp_rack_timeout(arg)
2121 void *arg;
2123 fsm *f = arg;
2125 if (f->state == OPENED && ccp_localstate[f->unit] & RREQ_REPEAT) {
2126 fsm_sdata(f, CCP_RESETREQ, f->reqid, NULL, 0);
2127 TIMEOUT(ccp_rack_timeout, f, RACKTIMEOUT);
2128 ccp_localstate[f->unit] &= ~RREQ_REPEAT;
2129 } else
2130 ccp_localstate[f->unit] &= ~RACK_PENDING;