Merge branch 'master' of ssh://crater.dragonflybsd.org/repository/git/dragonfly
[dragonfly.git] / usr.sbin / pppd / ccp.c
blob369baa3ead1944e652fdb1523bbbfba85e6ee2fc
1 /*
2 * ccp.c - PPP Compression Control Protocol.
4 * Copyright (c) 1994 The Australian National University.
5 * All rights reserved.
7 * Permission to use, copy, modify, and distribute this software and its
8 * documentation is hereby granted, provided that the above copyright
9 * notice appears in all copies. This software is provided without any
10 * warranty, express or implied. The Australian National University
11 * makes no representations about the suitability of this software for
12 * any purpose.
14 * IN NO EVENT SHALL THE AUSTRALIAN NATIONAL UNIVERSITY BE LIABLE TO ANY
15 * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
16 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
17 * THE AUSTRALIAN NATIONAL UNIVERSITY HAVE BEEN ADVISED OF THE POSSIBILITY
18 * OF SUCH DAMAGE.
20 * THE AUSTRALIAN NATIONAL UNIVERSITY SPECIFICALLY DISCLAIMS ANY WARRANTIES,
21 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
22 * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
23 * ON AN "AS IS" BASIS, AND THE AUSTRALIAN NATIONAL UNIVERSITY HAS NO
24 * OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
25 * OR MODIFICATIONS.
27 * $FreeBSD: src/usr.sbin/pppd/ccp.c,v 1.10 1999/08/28 01:19:00 peter Exp $
28 * $DragonFly: src/usr.sbin/pppd/ccp.c,v 1.5 2005/11/24 23:42:54 swildner Exp $
31 #include <string.h>
32 #include <syslog.h>
33 #include <sys/ioctl.h>
34 #include <sys/types.h>
36 #include "pppd.h"
37 #include "fsm.h"
38 #include "ccp.h"
39 #include <net/ppp_layer/ppp_comp.h>
42 * Protocol entry points from main code.
44 static void ccp_init(int unit);
45 static void ccp_open(int unit);
46 static void ccp_close(int unit, char *);
47 static void ccp_lowerup(int unit);
48 static void ccp_lowerdown(int);
49 static void ccp_input(int unit, u_char *pkt, int len);
50 static void ccp_protrej(int unit);
51 static int ccp_printpkt(u_char *pkt, int len,
52 void (*printer)(void *, char *, ...),
53 void *arg);
54 static void ccp_datainput(int unit, u_char *pkt, int len);
56 struct protent ccp_protent = {
57 PPP_CCP,
58 ccp_init,
59 ccp_input,
60 ccp_protrej,
61 ccp_lowerup,
62 ccp_lowerdown,
63 ccp_open,
64 ccp_close,
65 ccp_printpkt,
66 ccp_datainput,
68 "CCP",
69 NULL,
70 NULL,
71 NULL
74 fsm ccp_fsm[NUM_PPP];
75 ccp_options ccp_wantoptions[NUM_PPP]; /* what to request the peer to use */
76 ccp_options ccp_gotoptions[NUM_PPP]; /* what the peer agreed to do */
77 ccp_options ccp_allowoptions[NUM_PPP]; /* what we'll agree to do */
78 ccp_options ccp_hisoptions[NUM_PPP]; /* what we agreed to do */
81 * Callbacks for fsm code.
83 static void ccp_resetci(fsm *);
84 static int ccp_cilen(fsm *);
85 static void ccp_addci(fsm *, u_char *, int *);
86 static int ccp_ackci(fsm *, u_char *, int);
87 static int ccp_nakci(fsm *, u_char *, int);
88 static int ccp_rejci(fsm *, u_char *, int);
89 static int ccp_reqci(fsm *, u_char *, int *, int);
90 static void ccp_up(fsm *);
91 static void ccp_down(fsm *);
92 static int ccp_extcode(fsm *, int, int, u_char *, int);
93 static void ccp_rack_timeout(void *);
94 static char *method_name(ccp_options *, ccp_options *);
96 static fsm_callbacks ccp_callbacks = {
97 ccp_resetci,
98 ccp_cilen,
99 ccp_addci,
100 ccp_ackci,
101 ccp_nakci,
102 ccp_rejci,
103 ccp_reqci,
104 ccp_up,
105 ccp_down,
106 NULL,
107 NULL,
108 NULL,
109 NULL,
110 ccp_extcode,
111 "CCP"
115 * Do we want / did we get any compression?
117 #define ANY_COMPRESS(opt) ((opt).deflate || (opt).bsd_compress \
118 || (opt).predictor_1 || (opt).predictor_2)
121 * Local state (mainly for handling reset-reqs and reset-acks).
123 static int ccp_localstate[NUM_PPP];
124 #define RACK_PENDING 1 /* waiting for reset-ack */
125 #define RREQ_REPEAT 2 /* send another reset-req if no reset-ack */
127 #define RACKTIMEOUT 1 /* second */
129 static int all_rejected[NUM_PPP]; /* we rejected all peer's options */
132 * ccp_init - initialize CCP.
134 static void
135 ccp_init(int unit)
137 fsm *f = &ccp_fsm[unit];
139 f->unit = unit;
140 f->protocol = PPP_CCP;
141 f->callbacks = &ccp_callbacks;
142 fsm_init(f);
144 memset(&ccp_wantoptions[unit], 0, sizeof(ccp_options));
145 memset(&ccp_gotoptions[unit], 0, sizeof(ccp_options));
146 memset(&ccp_allowoptions[unit], 0, sizeof(ccp_options));
147 memset(&ccp_hisoptions[unit], 0, sizeof(ccp_options));
149 ccp_wantoptions[0].deflate = 1;
150 ccp_wantoptions[0].deflate_size = DEFLATE_MAX_SIZE;
151 ccp_wantoptions[0].deflate_correct = 1;
152 ccp_wantoptions[0].deflate_draft = 1;
153 ccp_allowoptions[0].deflate = 1;
154 ccp_allowoptions[0].deflate_size = DEFLATE_MAX_SIZE;
155 ccp_allowoptions[0].deflate_correct = 1;
156 ccp_allowoptions[0].deflate_draft = 1;
158 ccp_wantoptions[0].bsd_compress = 1;
159 ccp_wantoptions[0].bsd_bits = BSD_MAX_BITS;
160 ccp_allowoptions[0].bsd_compress = 1;
161 ccp_allowoptions[0].bsd_bits = BSD_MAX_BITS;
163 ccp_allowoptions[0].predictor_1 = 1;
167 * ccp_open - CCP is allowed to come up.
169 static void
170 ccp_open(int unit)
172 fsm *f = &ccp_fsm[unit];
174 if (f->state != OPENED)
175 ccp_flags_set(unit, 1, 0);
178 * Find out which compressors the kernel supports before
179 * deciding whether to open in silent mode.
181 ccp_resetci(f);
182 if (!ANY_COMPRESS(ccp_gotoptions[unit]))
183 f->flags |= OPT_SILENT;
185 fsm_open(f);
189 * ccp_close - Terminate CCP.
191 static void
192 ccp_close(int unit, char *reason)
194 ccp_flags_set(unit, 0, 0);
195 fsm_close(&ccp_fsm[unit], reason);
199 * ccp_lowerup - we may now transmit CCP packets.
201 static void
202 ccp_lowerup(int unit)
204 fsm_lowerup(&ccp_fsm[unit]);
208 * ccp_lowerdown - we may not transmit CCP packets.
210 static void
211 ccp_lowerdown(int unit)
213 fsm_lowerdown(&ccp_fsm[unit]);
217 * ccp_input - process a received CCP packet.
219 static void
220 ccp_input(int unit, u_char *p, int len)
222 fsm *f = &ccp_fsm[unit];
223 int oldstate;
226 * Check for a terminate-request so we can print a message.
228 oldstate = f->state;
229 fsm_input(f, p, len);
230 if (oldstate == OPENED && p[0] == TERMREQ && f->state != OPENED)
231 syslog(LOG_NOTICE, "Compression disabled by peer.");
234 * If we get a terminate-ack and we're not asking for compression,
235 * close CCP.
237 if (oldstate == REQSENT && p[0] == TERMACK
238 && !ANY_COMPRESS(ccp_gotoptions[unit]))
239 ccp_close(unit, "No compression negotiated");
243 * Handle a CCP-specific code.
245 static int
246 ccp_extcode(fsm *f, int code, int id, u_char *p, int len)
248 switch (code) {
249 case CCP_RESETREQ:
250 if (f->state != OPENED)
251 break;
252 /* send a reset-ack, which the transmitter will see and
253 reset its compression state. */
254 fsm_sdata(f, CCP_RESETACK, id, NULL, 0);
255 break;
257 case CCP_RESETACK:
258 if (ccp_localstate[f->unit] & RACK_PENDING && id == f->reqid) {
259 ccp_localstate[f->unit] &= ~(RACK_PENDING | RREQ_REPEAT);
260 UNTIMEOUT(ccp_rack_timeout, f);
262 break;
264 default:
265 return 0;
268 return 1;
272 * ccp_protrej - peer doesn't talk CCP.
274 static void
275 ccp_protrej(int unit)
277 ccp_flags_set(unit, 0, 0);
278 fsm_lowerdown(&ccp_fsm[unit]);
282 * ccp_resetci - initialize at start of negotiation.
284 static void
285 ccp_resetci(fsm *f)
287 ccp_options *go = &ccp_gotoptions[f->unit];
288 u_char opt_buf[16];
290 *go = ccp_wantoptions[f->unit];
291 all_rejected[f->unit] = 0;
294 * Check whether the kernel knows about the various
295 * compression methods we might request.
297 if (go->bsd_compress) {
298 opt_buf[0] = CI_BSD_COMPRESS;
299 opt_buf[1] = CILEN_BSD_COMPRESS;
300 opt_buf[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, BSD_MIN_BITS);
301 if (ccp_test(f->unit, opt_buf, CILEN_BSD_COMPRESS, 0) <= 0)
302 go->bsd_compress = 0;
304 if (go->deflate) {
305 if (go->deflate_correct) {
306 opt_buf[0] = CI_DEFLATE;
307 opt_buf[1] = CILEN_DEFLATE;
308 opt_buf[2] = DEFLATE_MAKE_OPT(DEFLATE_MIN_SIZE);
309 opt_buf[3] = DEFLATE_CHK_SEQUENCE;
310 if (ccp_test(f->unit, opt_buf, CILEN_DEFLATE, 0) <= 0)
311 go->deflate_correct = 0;
313 if (go->deflate_draft) {
314 opt_buf[0] = CI_DEFLATE_DRAFT;
315 opt_buf[1] = CILEN_DEFLATE;
316 opt_buf[2] = DEFLATE_MAKE_OPT(DEFLATE_MIN_SIZE);
317 opt_buf[3] = DEFLATE_CHK_SEQUENCE;
318 if (ccp_test(f->unit, opt_buf, CILEN_DEFLATE, 0) <= 0)
319 go->deflate_draft = 0;
321 if (!go->deflate_correct && !go->deflate_draft)
322 go->deflate = 0;
324 if (go->predictor_1) {
325 opt_buf[0] = CI_PREDICTOR_1;
326 opt_buf[1] = CILEN_PREDICTOR_1;
327 if (ccp_test(f->unit, opt_buf, CILEN_PREDICTOR_1, 0) <= 0)
328 go->predictor_1 = 0;
330 if (go->predictor_2) {
331 opt_buf[0] = CI_PREDICTOR_2;
332 opt_buf[1] = CILEN_PREDICTOR_2;
333 if (ccp_test(f->unit, opt_buf, CILEN_PREDICTOR_2, 0) <= 0)
334 go->predictor_2 = 0;
339 * ccp_cilen - Return total length of our configuration info.
341 static int
342 ccp_cilen(fsm *f)
344 ccp_options *go = &ccp_gotoptions[f->unit];
346 return (go->bsd_compress? CILEN_BSD_COMPRESS: 0)
347 + (go->deflate? CILEN_DEFLATE: 0)
348 + (go->predictor_1? CILEN_PREDICTOR_1: 0)
349 + (go->predictor_2? CILEN_PREDICTOR_2: 0);
353 * ccp_addci - put our requests in a packet.
355 static void
356 ccp_addci(fsm *f, u_char *p, int *lenp)
358 int res;
359 ccp_options *go = &ccp_gotoptions[f->unit];
360 u_char *p0 = p;
363 * Add the compression types that we can receive, in decreasing
364 * preference order. Get the kernel to allocate the first one
365 * in case it gets Acked.
367 if (go->deflate) {
368 p[0] = go->deflate_correct? CI_DEFLATE: CI_DEFLATE_DRAFT;
369 p[1] = CILEN_DEFLATE;
370 p[2] = DEFLATE_MAKE_OPT(go->deflate_size);
371 p[3] = DEFLATE_CHK_SEQUENCE;
372 for (;;) {
373 res = ccp_test(f->unit, p, CILEN_DEFLATE, 0);
374 if (res > 0) {
375 p += CILEN_DEFLATE;
376 break;
378 if (res < 0 || go->deflate_size <= DEFLATE_MIN_SIZE) {
379 go->deflate = 0;
380 break;
382 --go->deflate_size;
383 p[2] = DEFLATE_MAKE_OPT(go->deflate_size);
385 if (p != p0 && go->deflate_correct && go->deflate_draft) {
386 p[0] = CI_DEFLATE_DRAFT;
387 p[1] = CILEN_DEFLATE;
388 p[2] = p[2 - CILEN_DEFLATE];
389 p[3] = DEFLATE_CHK_SEQUENCE;
390 p += CILEN_DEFLATE;
393 if (go->bsd_compress) {
394 p[0] = CI_BSD_COMPRESS;
395 p[1] = CILEN_BSD_COMPRESS;
396 p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits);
397 if (p != p0) {
398 p += CILEN_BSD_COMPRESS; /* not the first option */
399 } else {
400 for (;;) {
401 res = ccp_test(f->unit, p, CILEN_BSD_COMPRESS, 0);
402 if (res > 0) {
403 p += CILEN_BSD_COMPRESS;
404 break;
406 if (res < 0 || go->bsd_bits <= BSD_MIN_BITS) {
407 go->bsd_compress = 0;
408 break;
410 --go->bsd_bits;
411 p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits);
415 /* XXX Should Predictor 2 be preferable to Predictor 1? */
416 if (go->predictor_1) {
417 p[0] = CI_PREDICTOR_1;
418 p[1] = CILEN_PREDICTOR_1;
419 if (p == p0 && ccp_test(f->unit, p, CILEN_PREDICTOR_1, 0) <= 0) {
420 go->predictor_1 = 0;
421 } else {
422 p += CILEN_PREDICTOR_1;
425 if (go->predictor_2) {
426 p[0] = CI_PREDICTOR_2;
427 p[1] = CILEN_PREDICTOR_2;
428 if (p == p0 && ccp_test(f->unit, p, CILEN_PREDICTOR_2, 0) <= 0) {
429 go->predictor_2 = 0;
430 } else {
431 p += CILEN_PREDICTOR_2;
435 go->method = (p > p0)? p0[0]: -1;
437 *lenp = p - p0;
441 * ccp_ackci - process a received configure-ack, and return
442 * 1 iff the packet was OK.
444 static int
445 ccp_ackci(fsm *f, u_char *p, int len)
447 ccp_options *go = &ccp_gotoptions[f->unit];
448 u_char *p0 = p;
450 if (go->deflate) {
451 if (len < CILEN_DEFLATE
452 || p[0] != (go->deflate_correct? CI_DEFLATE: CI_DEFLATE_DRAFT)
453 || p[1] != CILEN_DEFLATE
454 || p[2] != DEFLATE_MAKE_OPT(go->deflate_size)
455 || p[3] != DEFLATE_CHK_SEQUENCE)
456 return 0;
457 p += CILEN_DEFLATE;
458 len -= CILEN_DEFLATE;
459 /* XXX Cope with first/fast ack */
460 if (len == 0)
461 return 1;
462 if (go->deflate_correct && go->deflate_draft) {
463 if (len < CILEN_DEFLATE
464 || p[0] != CI_DEFLATE_DRAFT
465 || p[1] != CILEN_DEFLATE
466 || p[2] != DEFLATE_MAKE_OPT(go->deflate_size)
467 || p[3] != DEFLATE_CHK_SEQUENCE)
468 return 0;
469 p += CILEN_DEFLATE;
470 len -= CILEN_DEFLATE;
473 if (go->bsd_compress) {
474 if (len < CILEN_BSD_COMPRESS
475 || p[0] != CI_BSD_COMPRESS || p[1] != CILEN_BSD_COMPRESS
476 || p[2] != BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits))
477 return 0;
478 p += CILEN_BSD_COMPRESS;
479 len -= CILEN_BSD_COMPRESS;
480 /* XXX Cope with first/fast ack */
481 if (p == p0 && len == 0)
482 return 1;
484 if (go->predictor_1) {
485 if (len < CILEN_PREDICTOR_1
486 || p[0] != CI_PREDICTOR_1 || p[1] != CILEN_PREDICTOR_1)
487 return 0;
488 p += CILEN_PREDICTOR_1;
489 len -= CILEN_PREDICTOR_1;
490 /* XXX Cope with first/fast ack */
491 if (p == p0 && len == 0)
492 return 1;
494 if (go->predictor_2) {
495 if (len < CILEN_PREDICTOR_2
496 || p[0] != CI_PREDICTOR_2 || p[1] != CILEN_PREDICTOR_2)
497 return 0;
498 p += CILEN_PREDICTOR_2;
499 len -= CILEN_PREDICTOR_2;
500 /* XXX Cope with first/fast ack */
501 if (p == p0 && len == 0)
502 return 1;
505 if (len != 0)
506 return 0;
507 return 1;
511 * ccp_nakci - process received configure-nak.
512 * Returns 1 iff the nak was OK.
514 static int
515 ccp_nakci(fsm *f, u_char *p, int len)
517 ccp_options *go = &ccp_gotoptions[f->unit];
518 ccp_options no; /* options we've seen already */
519 ccp_options try; /* options to ask for next time */
521 memset(&no, 0, sizeof(no));
522 try = *go;
524 if (go->deflate && len >= CILEN_DEFLATE
525 && p[0] == (go->deflate_correct? CI_DEFLATE: CI_DEFLATE_DRAFT)
526 && p[1] == CILEN_DEFLATE) {
527 no.deflate = 1;
529 * Peer wants us to use a different code size or something.
530 * Stop asking for Deflate if we don't understand his suggestion.
532 if (DEFLATE_METHOD(p[2]) != DEFLATE_METHOD_VAL
533 || DEFLATE_SIZE(p[2]) < DEFLATE_MIN_SIZE
534 || p[3] != DEFLATE_CHK_SEQUENCE)
535 try.deflate = 0;
536 else if (DEFLATE_SIZE(p[2]) < go->deflate_size)
537 try.deflate_size = DEFLATE_SIZE(p[2]);
538 p += CILEN_DEFLATE;
539 len -= CILEN_DEFLATE;
540 if (go->deflate_correct && go->deflate_draft
541 && len >= CILEN_DEFLATE && p[0] == CI_DEFLATE_DRAFT
542 && p[1] == CILEN_DEFLATE) {
543 p += CILEN_DEFLATE;
544 len -= CILEN_DEFLATE;
548 if (go->bsd_compress && len >= CILEN_BSD_COMPRESS
549 && p[0] == CI_BSD_COMPRESS && p[1] == CILEN_BSD_COMPRESS) {
550 no.bsd_compress = 1;
552 * Peer wants us to use a different number of bits
553 * or a different version.
555 if (BSD_VERSION(p[2]) != BSD_CURRENT_VERSION)
556 try.bsd_compress = 0;
557 else if (BSD_NBITS(p[2]) < go->bsd_bits)
558 try.bsd_bits = BSD_NBITS(p[2]);
559 p += CILEN_BSD_COMPRESS;
560 len -= CILEN_BSD_COMPRESS;
564 * Predictor-1 and 2 have no options, so they can't be Naked.
566 * XXX What should we do with any remaining options?
569 if (len != 0)
570 return 0;
572 if (f->state != OPENED)
573 *go = try;
574 return 1;
578 * ccp_rejci - reject some of our suggested compression methods.
580 static int
581 ccp_rejci(fsm *f, u_char *p, int len)
583 ccp_options *go = &ccp_gotoptions[f->unit];
584 ccp_options try; /* options to request next time */
586 try = *go;
589 * Cope with empty configure-rejects by ceasing to send
590 * configure-requests.
592 if (len == 0 && all_rejected[f->unit])
593 return -1;
595 if (go->deflate && len >= CILEN_DEFLATE
596 && p[0] == (go->deflate_correct? CI_DEFLATE: CI_DEFLATE_DRAFT)
597 && p[1] == CILEN_DEFLATE) {
598 if (p[2] != DEFLATE_MAKE_OPT(go->deflate_size)
599 || p[3] != DEFLATE_CHK_SEQUENCE)
600 return 0; /* Rej is bad */
601 if (go->deflate_correct)
602 try.deflate_correct = 0;
603 else
604 try.deflate_draft = 0;
605 p += CILEN_DEFLATE;
606 len -= CILEN_DEFLATE;
607 if (go->deflate_correct && go->deflate_draft
608 && len >= CILEN_DEFLATE && p[0] == CI_DEFLATE_DRAFT
609 && p[1] == CILEN_DEFLATE) {
610 if (p[2] != DEFLATE_MAKE_OPT(go->deflate_size)
611 || p[3] != DEFLATE_CHK_SEQUENCE)
612 return 0; /* Rej is bad */
613 try.deflate_draft = 0;
614 p += CILEN_DEFLATE;
615 len -= CILEN_DEFLATE;
617 if (!try.deflate_correct && !try.deflate_draft)
618 try.deflate = 0;
620 if (go->bsd_compress && len >= CILEN_BSD_COMPRESS
621 && p[0] == CI_BSD_COMPRESS && p[1] == CILEN_BSD_COMPRESS) {
622 if (p[2] != BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits))
623 return 0;
624 try.bsd_compress = 0;
625 p += CILEN_BSD_COMPRESS;
626 len -= CILEN_BSD_COMPRESS;
628 if (go->predictor_1 && len >= CILEN_PREDICTOR_1
629 && p[0] == CI_PREDICTOR_1 && p[1] == CILEN_PREDICTOR_1) {
630 try.predictor_1 = 0;
631 p += CILEN_PREDICTOR_1;
632 len -= CILEN_PREDICTOR_1;
634 if (go->predictor_2 && len >= CILEN_PREDICTOR_2
635 && p[0] == CI_PREDICTOR_2 && p[1] == CILEN_PREDICTOR_2) {
636 try.predictor_2 = 0;
637 p += CILEN_PREDICTOR_2;
638 len -= CILEN_PREDICTOR_2;
641 if (len != 0)
642 return 0;
644 if (f->state != OPENED)
645 *go = try;
647 return 1;
651 * ccp_reqci - processed a received configure-request.
652 * Returns CONFACK, CONFNAK or CONFREJ and the packet modified
653 * appropriately.
655 static int
656 ccp_reqci(fsm *f, u_char *p, int *lenp, int dont_nak)
658 int ret, newret, res;
659 u_char *p0, *retp;
660 int len, clen, type, nb;
661 ccp_options *ho = &ccp_hisoptions[f->unit];
662 ccp_options *ao = &ccp_allowoptions[f->unit];
664 ret = CONFACK;
665 retp = p0 = p;
666 len = *lenp;
668 memset(ho, 0, sizeof(ccp_options));
669 ho->method = (len > 0)? p[0]: -1;
671 while (len > 0) {
672 newret = CONFACK;
673 if (len < 2 || p[1] < 2 || p[1] > len) {
674 /* length is bad */
675 clen = len;
676 newret = CONFREJ;
678 } else {
679 type = p[0];
680 clen = p[1];
682 switch (type) {
683 case CI_DEFLATE:
684 case CI_DEFLATE_DRAFT:
685 if (!ao->deflate || clen != CILEN_DEFLATE
686 || (!ao->deflate_correct && type == CI_DEFLATE)
687 || (!ao->deflate_draft && type == CI_DEFLATE_DRAFT)) {
688 newret = CONFREJ;
689 break;
692 ho->deflate = 1;
693 ho->deflate_size = nb = DEFLATE_SIZE(p[2]);
694 if (DEFLATE_METHOD(p[2]) != DEFLATE_METHOD_VAL
695 || p[3] != DEFLATE_CHK_SEQUENCE
696 || nb > ao->deflate_size || nb < DEFLATE_MIN_SIZE) {
697 newret = CONFNAK;
698 if (!dont_nak) {
699 p[2] = DEFLATE_MAKE_OPT(ao->deflate_size);
700 p[3] = DEFLATE_CHK_SEQUENCE;
701 /* fall through to test this #bits below */
702 } else
703 break;
707 * Check whether we can do Deflate with the window
708 * size they want. If the window is too big, reduce
709 * it until the kernel can cope and nak with that.
710 * We only check this for the first option.
712 if (p == p0) {
713 for (;;) {
714 res = ccp_test(f->unit, p, CILEN_DEFLATE, 1);
715 if (res > 0)
716 break; /* it's OK now */
717 if (res < 0 || nb == DEFLATE_MIN_SIZE || dont_nak) {
718 newret = CONFREJ;
719 p[2] = DEFLATE_MAKE_OPT(ho->deflate_size);
720 break;
722 newret = CONFNAK;
723 --nb;
724 p[2] = DEFLATE_MAKE_OPT(nb);
727 break;
729 case CI_BSD_COMPRESS:
730 if (!ao->bsd_compress || clen != CILEN_BSD_COMPRESS) {
731 newret = CONFREJ;
732 break;
735 ho->bsd_compress = 1;
736 ho->bsd_bits = nb = BSD_NBITS(p[2]);
737 if (BSD_VERSION(p[2]) != BSD_CURRENT_VERSION
738 || nb > ao->bsd_bits || nb < BSD_MIN_BITS) {
739 newret = CONFNAK;
740 if (!dont_nak) {
741 p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, ao->bsd_bits);
742 /* fall through to test this #bits below */
743 } else
744 break;
748 * Check whether we can do BSD-Compress with the code
749 * size they want. If the code size is too big, reduce
750 * it until the kernel can cope and nak with that.
751 * We only check this for the first option.
753 if (p == p0) {
754 for (;;) {
755 res = ccp_test(f->unit, p, CILEN_BSD_COMPRESS, 1);
756 if (res > 0)
757 break;
758 if (res < 0 || nb == BSD_MIN_BITS || dont_nak) {
759 newret = CONFREJ;
760 p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION,
761 ho->bsd_bits);
762 break;
764 newret = CONFNAK;
765 --nb;
766 p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, nb);
769 break;
771 case CI_PREDICTOR_1:
772 if (!ao->predictor_1 || clen != CILEN_PREDICTOR_1) {
773 newret = CONFREJ;
774 break;
777 ho->predictor_1 = 1;
778 if (p == p0
779 && ccp_test(f->unit, p, CILEN_PREDICTOR_1, 1) <= 0) {
780 newret = CONFREJ;
782 break;
784 case CI_PREDICTOR_2:
785 if (!ao->predictor_2 || clen != CILEN_PREDICTOR_2) {
786 newret = CONFREJ;
787 break;
790 ho->predictor_2 = 1;
791 if (p == p0
792 && ccp_test(f->unit, p, CILEN_PREDICTOR_2, 1) <= 0) {
793 newret = CONFREJ;
795 break;
797 default:
798 newret = CONFREJ;
802 if (newret == CONFNAK && dont_nak)
803 newret = CONFREJ;
804 if (!(newret == CONFACK || (newret == CONFNAK && ret == CONFREJ))) {
805 /* we're returning this option */
806 if (newret == CONFREJ && ret == CONFNAK)
807 retp = p0;
808 ret = newret;
809 if (p != retp)
810 BCOPY(p, retp, clen);
811 retp += clen;
814 p += clen;
815 len -= clen;
818 if (ret != CONFACK) {
819 if (ret == CONFREJ && *lenp == retp - p0)
820 all_rejected[f->unit] = 1;
821 else
822 *lenp = retp - p0;
824 return ret;
828 * Make a string name for a compression method (or 2).
830 static char *
831 method_name(ccp_options *opt, ccp_options *opt2)
833 static char result[64];
835 if (!ANY_COMPRESS(*opt))
836 return "(none)";
837 switch (opt->method) {
838 case CI_DEFLATE:
839 case CI_DEFLATE_DRAFT:
840 if (opt2 != NULL && opt2->deflate_size != opt->deflate_size)
841 sprintf(result, "Deflate%s (%d/%d)",
842 (opt->method == CI_DEFLATE_DRAFT? "(old#)": ""),
843 opt->deflate_size, opt2->deflate_size);
844 else
845 sprintf(result, "Deflate%s (%d)",
846 (opt->method == CI_DEFLATE_DRAFT? "(old#)": ""),
847 opt->deflate_size);
848 break;
849 case CI_BSD_COMPRESS:
850 if (opt2 != NULL && opt2->bsd_bits != opt->bsd_bits)
851 sprintf(result, "BSD-Compress (%d/%d)", opt->bsd_bits,
852 opt2->bsd_bits);
853 else
854 sprintf(result, "BSD-Compress (%d)", opt->bsd_bits);
855 break;
856 case CI_PREDICTOR_1:
857 return "Predictor 1";
858 case CI_PREDICTOR_2:
859 return "Predictor 2";
860 default:
861 sprintf(result, "Method %d", opt->method);
863 return result;
867 * CCP has come up - inform the kernel driver and log a message.
869 static void
870 ccp_up(fsm *f)
872 ccp_options *go = &ccp_gotoptions[f->unit];
873 ccp_options *ho = &ccp_hisoptions[f->unit];
874 char method1[64];
876 ccp_flags_set(f->unit, 1, 1);
877 if (ANY_COMPRESS(*go)) {
878 if (ANY_COMPRESS(*ho)) {
879 if (go->method == ho->method) {
880 syslog(LOG_NOTICE, "%s compression enabled",
881 method_name(go, ho));
882 } else {
883 strcpy(method1, method_name(go, NULL));
884 syslog(LOG_NOTICE, "%s / %s compression enabled",
885 method1, method_name(ho, NULL));
887 } else
888 syslog(LOG_NOTICE, "%s receive compression enabled",
889 method_name(go, NULL));
890 } else if (ANY_COMPRESS(*ho))
891 syslog(LOG_NOTICE, "%s transmit compression enabled",
892 method_name(ho, NULL));
896 * CCP has gone down - inform the kernel driver.
898 static void
899 ccp_down(fsm *f)
901 if (ccp_localstate[f->unit] & RACK_PENDING)
902 UNTIMEOUT(ccp_rack_timeout, f);
903 ccp_localstate[f->unit] = 0;
904 ccp_flags_set(f->unit, 1, 0);
908 * Print the contents of a CCP packet.
910 static char *ccp_codenames[] = {
911 "ConfReq", "ConfAck", "ConfNak", "ConfRej",
912 "TermReq", "TermAck", "CodeRej",
913 NULL, NULL, NULL, NULL, NULL, NULL,
914 "ResetReq", "ResetAck",
917 static int
918 ccp_printpkt(u_char *p, int plen, void (*printer)(void *, char *, ...),
919 void *arg)
921 u_char *p0, *optend;
922 int code, id, len;
923 int optlen;
925 p0 = p;
926 if (plen < HEADERLEN)
927 return 0;
928 code = p[0];
929 id = p[1];
930 len = (p[2] << 8) + p[3];
931 if (len < HEADERLEN || len > plen)
932 return 0;
934 if (code >= 1 && code <= sizeof(ccp_codenames) / sizeof(char *)
935 && ccp_codenames[code-1] != NULL)
936 printer(arg, " %s", ccp_codenames[code-1]);
937 else
938 printer(arg, " code=0x%x", code);
939 printer(arg, " id=0x%x", id);
940 len -= HEADERLEN;
941 p += HEADERLEN;
943 switch (code) {
944 case CONFREQ:
945 case CONFACK:
946 case CONFNAK:
947 case CONFREJ:
948 /* print list of possible compression methods */
949 while (len >= 2) {
950 code = p[0];
951 optlen = p[1];
952 if (optlen < 2 || optlen > len)
953 break;
954 printer(arg, " <");
955 len -= optlen;
956 optend = p + optlen;
957 switch (code) {
958 case CI_DEFLATE:
959 case CI_DEFLATE_DRAFT:
960 if (optlen >= CILEN_DEFLATE) {
961 printer(arg, "deflate%s %d",
962 (code == CI_DEFLATE_DRAFT? "(old#)": ""),
963 DEFLATE_SIZE(p[2]));
964 if (DEFLATE_METHOD(p[2]) != DEFLATE_METHOD_VAL)
965 printer(arg, " method %d", DEFLATE_METHOD(p[2]));
966 if (p[3] != DEFLATE_CHK_SEQUENCE)
967 printer(arg, " check %d", p[3]);
968 p += CILEN_DEFLATE;
970 break;
971 case CI_BSD_COMPRESS:
972 if (optlen >= CILEN_BSD_COMPRESS) {
973 printer(arg, "bsd v%d %d", BSD_VERSION(p[2]),
974 BSD_NBITS(p[2]));
975 p += CILEN_BSD_COMPRESS;
977 break;
978 case CI_PREDICTOR_1:
979 if (optlen >= CILEN_PREDICTOR_1) {
980 printer(arg, "predictor 1");
981 p += CILEN_PREDICTOR_1;
983 break;
984 case CI_PREDICTOR_2:
985 if (optlen >= CILEN_PREDICTOR_2) {
986 printer(arg, "predictor 2");
987 p += CILEN_PREDICTOR_2;
989 break;
991 while (p < optend)
992 printer(arg, " %.2x", *p++);
993 printer(arg, ">");
995 break;
997 case TERMACK:
998 case TERMREQ:
999 if (len > 0 && *p >= ' ' && *p < 0x7f) {
1000 print_string(p, len, printer, arg);
1001 p += len;
1002 len = 0;
1004 break;
1007 /* dump out the rest of the packet in hex */
1008 while (--len >= 0)
1009 printer(arg, " %.2x", *p++);
1011 return p - p0;
1015 * We have received a packet that the decompressor failed to
1016 * decompress. Here we would expect to issue a reset-request, but
1017 * Motorola has a patent on resetting the compressor as a result of
1018 * detecting an error in the decompressed data after decompression.
1019 * (See US patent 5,130,993; international patent publication number
1020 * WO 91/10289; Australian patent 73296/91.)
1022 * So we ask the kernel whether the error was detected after
1023 * decompression; if it was, we take CCP down, thus disabling
1024 * compression :-(, otherwise we issue the reset-request.
1026 static void
1027 ccp_datainput(int unit, u_char *pkt, int len)
1029 fsm *f;
1031 f = &ccp_fsm[unit];
1032 if (f->state == OPENED) {
1033 if (ccp_fatal_error(unit)) {
1035 * Disable compression by taking CCP down.
1037 syslog(LOG_ERR, "Lost compression sync: disabling compression");
1038 ccp_close(unit, "Lost compression sync");
1039 } else {
1041 * Send a reset-request to reset the peer's compressor.
1042 * We don't do that if we are still waiting for an
1043 * acknowledgement to a previous reset-request.
1045 if (!(ccp_localstate[f->unit] & RACK_PENDING)) {
1046 fsm_sdata(f, CCP_RESETREQ, f->reqid = ++f->id, NULL, 0);
1047 TIMEOUT(ccp_rack_timeout, f, RACKTIMEOUT);
1048 ccp_localstate[f->unit] |= RACK_PENDING;
1049 } else
1050 ccp_localstate[f->unit] |= RREQ_REPEAT;
1056 * Timeout waiting for reset-ack.
1058 static void
1059 ccp_rack_timeout(void *arg)
1061 fsm *f = arg;
1063 if (f->state == OPENED && ccp_localstate[f->unit] & RREQ_REPEAT) {
1064 fsm_sdata(f, CCP_RESETREQ, f->reqid, NULL, 0);
1065 TIMEOUT(ccp_rack_timeout, f, RACKTIMEOUT);
1066 ccp_localstate[f->unit] &= ~RREQ_REPEAT;
1067 } else
1068 ccp_localstate[f->unit] &= ~RACK_PENDING;