]> git.ozlabs.org Git - ppp.git/blob - pppd/ccp.c
update for 2.3.2
[ppp.git] / pppd / ccp.c
1 /*
2  * ccp.c - PPP Compression Control Protocol.
3  *
4  * Copyright (c) 1994 The Australian National University.
5  * All rights reserved.
6  *
7  * Permission to use, copy, modify, and distribute this software and its
8  * documentation is hereby granted, provided that the above copyright
9  * notice appears in all copies.  This software is provided without any
10  * warranty, express or implied. The Australian National University
11  * makes no representations about the suitability of this software for
12  * any purpose.
13  *
14  * IN NO EVENT SHALL THE AUSTRALIAN NATIONAL UNIVERSITY BE LIABLE TO ANY
15  * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
16  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
17  * THE AUSTRALIAN NATIONAL UNIVERSITY HAVE BEEN ADVISED OF THE POSSIBILITY
18  * OF SUCH DAMAGE.
19  *
20  * THE AUSTRALIAN NATIONAL UNIVERSITY SPECIFICALLY DISCLAIMS ANY WARRANTIES,
21  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
22  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
23  * ON AN "AS IS" BASIS, AND THE AUSTRALIAN NATIONAL UNIVERSITY HAS NO
24  * OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
25  * OR MODIFICATIONS.
26  */
27
28 #ifndef lint
29 static char rcsid[] = "$Id: ccp.c,v 1.21 1997/05/22 06:45:59 paulus Exp $";
30 #endif
31
32 #include <string.h>
33 #include <syslog.h>
34 #include <sys/ioctl.h>
35 #include <sys/types.h>
36 #include <net/ppp_defs.h>
37 #include <net/ppp-comp.h>
38
39 #include "pppd.h"
40 #include "fsm.h"
41 #include "ccp.h"
42
43 /*
44  * Protocol entry points from main code.
45  */
46 static void ccp_init __P((int unit));
47 static void ccp_open __P((int unit));
48 static void ccp_close __P((int unit, char *));
49 static void ccp_lowerup __P((int unit));
50 static void ccp_lowerdown __P((int));
51 static void ccp_input __P((int unit, u_char *pkt, int len));
52 static void ccp_protrej __P((int unit));
53 static int  ccp_printpkt __P((u_char *pkt, int len,
54                               void (*printer) __P((void *, char *, ...)),
55                               void *arg));
56 static void ccp_datainput __P((int unit, u_char *pkt, int len));
57
58 struct protent ccp_protent = {
59     PPP_CCP,
60     ccp_init,
61     ccp_input,
62     ccp_protrej,
63     ccp_lowerup,
64     ccp_lowerdown,
65     ccp_open,
66     ccp_close,
67     ccp_printpkt,
68     ccp_datainput,
69     1,
70     "CCP",
71     NULL,
72     NULL,
73     NULL
74 };
75
76 fsm ccp_fsm[NUM_PPP];
77 ccp_options ccp_wantoptions[NUM_PPP];   /* what to request the peer to use */
78 ccp_options ccp_gotoptions[NUM_PPP];    /* what the peer agreed to do */
79 ccp_options ccp_allowoptions[NUM_PPP];  /* what we'll agree to do */
80 ccp_options ccp_hisoptions[NUM_PPP];    /* what we agreed to do */
81
82 /*
83  * Callbacks for fsm code.
84  */
85 static void ccp_resetci __P((fsm *));
86 static int  ccp_cilen __P((fsm *));
87 static void ccp_addci __P((fsm *, u_char *, int *));
88 static int  ccp_ackci __P((fsm *, u_char *, int));
89 static int  ccp_nakci __P((fsm *, u_char *, int));
90 static int  ccp_rejci __P((fsm *, u_char *, int));
91 static int  ccp_reqci __P((fsm *, u_char *, int *, int));
92 static void ccp_up __P((fsm *));
93 static void ccp_down __P((fsm *));
94 static int  ccp_extcode __P((fsm *, int, int, u_char *, int));
95 static void ccp_rack_timeout __P((void *));
96 static char *method_name __P((ccp_options *, ccp_options *));
97
98 static fsm_callbacks ccp_callbacks = {
99     ccp_resetci,
100     ccp_cilen,
101     ccp_addci,
102     ccp_ackci,
103     ccp_nakci,
104     ccp_rejci,
105     ccp_reqci,
106     ccp_up,
107     ccp_down,
108     NULL,
109     NULL,
110     NULL,
111     NULL,
112     ccp_extcode,
113     "CCP"
114 };
115
116 /*
117  * Do we want / did we get any compression?
118  */
119 #define ANY_COMPRESS(opt)       ((opt).deflate || (opt).bsd_compress \
120                                  || (opt).predictor_1 || (opt).predictor_2)
121
122 /*
123  * Local state (mainly for handling reset-reqs and reset-acks).
124  */
125 static int ccp_localstate[NUM_PPP];
126 #define RACK_PENDING    1       /* waiting for reset-ack */
127 #define RREQ_REPEAT     2       /* send another reset-req if no reset-ack */
128
129 #define RACKTIMEOUT     1       /* second */
130
131 static int all_rejected[NUM_PPP];       /* we rejected all peer's options */
132
133 /*
134  * ccp_init - initialize CCP.
135  */
136 static void
137 ccp_init(unit)
138     int unit;
139 {
140     fsm *f = &ccp_fsm[unit];
141
142     f->unit = unit;
143     f->protocol = PPP_CCP;
144     f->callbacks = &ccp_callbacks;
145     fsm_init(f);
146
147     memset(&ccp_wantoptions[unit],  0, sizeof(ccp_options));
148     memset(&ccp_gotoptions[unit],   0, sizeof(ccp_options));
149     memset(&ccp_allowoptions[unit], 0, sizeof(ccp_options));
150     memset(&ccp_hisoptions[unit],   0, sizeof(ccp_options));
151
152     ccp_wantoptions[0].deflate = 1;
153     ccp_wantoptions[0].deflate_size = DEFLATE_MAX_SIZE;
154     ccp_allowoptions[0].deflate = 1;
155     ccp_allowoptions[0].deflate_size = DEFLATE_MAX_SIZE;
156
157     ccp_wantoptions[0].bsd_compress = 1;
158     ccp_wantoptions[0].bsd_bits = BSD_MAX_BITS;
159     ccp_allowoptions[0].bsd_compress = 1;
160     ccp_allowoptions[0].bsd_bits = BSD_MAX_BITS;
161
162     ccp_allowoptions[0].predictor_1 = 1;
163 }
164
165 /*
166  * ccp_open - CCP is allowed to come up.
167  */
168 static void
169 ccp_open(unit)
170     int unit;
171 {
172     fsm *f = &ccp_fsm[unit];
173
174     if (f->state != OPENED)
175         ccp_flags_set(unit, 1, 0);
176
177     /*
178      * Find out which compressors the kernel supports before
179      * deciding whether to open in silent mode.
180      */
181     ccp_resetci(f);
182     if (!ANY_COMPRESS(ccp_gotoptions[unit]))
183         f->flags |= OPT_SILENT;
184
185     fsm_open(f);
186 }
187
188 /*
189  * ccp_close - Terminate CCP.
190  */
191 static void
192 ccp_close(unit, reason)
193     int unit;
194     char *reason;
195 {
196     ccp_flags_set(unit, 0, 0);
197     fsm_close(&ccp_fsm[unit], reason);
198 }
199
200 /*
201  * ccp_lowerup - we may now transmit CCP packets.
202  */
203 static void
204 ccp_lowerup(unit)
205     int unit;
206 {
207     fsm_lowerup(&ccp_fsm[unit]);
208 }
209
210 /*
211  * ccp_lowerdown - we may not transmit CCP packets.
212  */
213 static void
214 ccp_lowerdown(unit)
215     int unit;
216 {
217     fsm_lowerdown(&ccp_fsm[unit]);
218 }
219
220 /*
221  * ccp_input - process a received CCP packet.
222  */
223 static void
224 ccp_input(unit, p, len)
225     int unit;
226     u_char *p;
227     int len;
228 {
229     fsm *f = &ccp_fsm[unit];
230     int oldstate;
231
232     /*
233      * Check for a terminate-request so we can print a message.
234      */
235     oldstate = f->state;
236     fsm_input(f, p, len);
237     if (oldstate == OPENED && p[0] == TERMREQ && f->state != OPENED)
238         syslog(LOG_NOTICE, "Compression disabled by peer.");
239
240     /*
241      * If we get a terminate-ack and we're not asking for compression,
242      * close CCP.
243      */
244     if (oldstate == REQSENT && p[0] == TERMACK
245         && !ANY_COMPRESS(ccp_gotoptions[unit]))
246         ccp_close(unit, "No compression negotiated");
247 }
248
249 /*
250  * Handle a CCP-specific code.
251  */
252 static int
253 ccp_extcode(f, code, id, p, len)
254     fsm *f;
255     int code, id;
256     u_char *p;
257     int len;
258 {
259     switch (code) {
260     case CCP_RESETREQ:
261         if (f->state != OPENED)
262             break;
263         /* send a reset-ack, which the transmitter will see and
264            reset its compression state. */
265         fsm_sdata(f, CCP_RESETACK, id, NULL, 0);
266         break;
267
268     case CCP_RESETACK:
269         if (ccp_localstate[f->unit] & RACK_PENDING && id == f->reqid) {
270             ccp_localstate[f->unit] &= ~(RACK_PENDING | RREQ_REPEAT);
271             UNTIMEOUT(ccp_rack_timeout, f);
272         }
273         break;
274
275     default:
276         return 0;
277     }
278
279     return 1;
280 }
281
282 /*
283  * ccp_protrej - peer doesn't talk CCP.
284  */
285 static void
286 ccp_protrej(unit)
287     int unit;
288 {
289     ccp_flags_set(unit, 0, 0);
290     fsm_lowerdown(&ccp_fsm[unit]);
291 }
292
293 /*
294  * ccp_resetci - initialize at start of negotiation.
295  */
296 static void
297 ccp_resetci(f)
298     fsm *f;
299 {
300     ccp_options *go = &ccp_gotoptions[f->unit];
301     u_char opt_buf[16];
302
303     *go = ccp_wantoptions[f->unit];
304     all_rejected[f->unit] = 0;
305
306     /*
307      * Check whether the kernel knows about the various
308      * compression methods we might request.
309      */
310     if (go->bsd_compress) {
311         opt_buf[0] = CI_BSD_COMPRESS;
312         opt_buf[1] = CILEN_BSD_COMPRESS;
313         opt_buf[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, BSD_MIN_BITS);
314         if (ccp_test(f->unit, opt_buf, CILEN_BSD_COMPRESS, 0) <= 0)
315             go->bsd_compress = 0;
316     }
317     if (go->deflate) {
318         opt_buf[0] = CI_DEFLATE;
319         opt_buf[1] = CILEN_DEFLATE;
320         opt_buf[2] = DEFLATE_MAKE_OPT(DEFLATE_MIN_SIZE);
321         opt_buf[3] = DEFLATE_CHK_SEQUENCE;
322         if (ccp_test(f->unit, opt_buf, CILEN_DEFLATE, 0) <= 0)
323             go->deflate = 0;
324     }
325     if (go->predictor_1) {
326         opt_buf[0] = CI_PREDICTOR_1;
327         opt_buf[1] = CILEN_PREDICTOR_1;
328         if (ccp_test(f->unit, opt_buf, CILEN_PREDICTOR_1, 0) <= 0)
329             go->predictor_1 = 0;
330     }
331     if (go->predictor_2) {
332         opt_buf[0] = CI_PREDICTOR_2;
333         opt_buf[1] = CILEN_PREDICTOR_2;
334         if (ccp_test(f->unit, opt_buf, CILEN_PREDICTOR_2, 0) <= 0)
335             go->predictor_2 = 0;
336     }
337 }
338
339 /*
340  * ccp_cilen - Return total length of our configuration info.
341  */
342 static int
343 ccp_cilen(f)
344     fsm *f;
345 {
346     ccp_options *go = &ccp_gotoptions[f->unit];
347
348     return (go->bsd_compress? CILEN_BSD_COMPRESS: 0)
349         + (go->deflate? CILEN_DEFLATE: 0)
350         + (go->predictor_1? CILEN_PREDICTOR_1: 0)
351         + (go->predictor_2? CILEN_PREDICTOR_2: 0);
352 }
353
354 /*
355  * ccp_addci - put our requests in a packet.
356  */
357 static void
358 ccp_addci(f, p, lenp)
359     fsm *f;
360     u_char *p;
361     int *lenp;
362 {
363     int res;
364     ccp_options *go = &ccp_gotoptions[f->unit];
365     u_char *p0 = p;
366
367     /*
368      * Add the compression types that we can receive, in decreasing
369      * preference order.  Get the kernel to allocate the first one
370      * in case it gets Acked.
371      */
372     if (go->deflate) {
373         p[0] = CI_DEFLATE;
374         p[1] = CILEN_DEFLATE;
375         p[2] = DEFLATE_MAKE_OPT(go->deflate_size);
376         p[3] = DEFLATE_CHK_SEQUENCE;
377         for (;;) {
378             res = ccp_test(f->unit, p, CILEN_DEFLATE, 0);
379             if (res > 0) {
380                 p += CILEN_DEFLATE;
381                 break;
382             }
383             if (res < 0 || go->deflate_size <= DEFLATE_MIN_SIZE) {
384                 go->deflate = 0;
385                 break;
386             }
387             --go->deflate_size;
388             p[2] = DEFLATE_MAKE_OPT(go->deflate_size);
389         }
390     }
391     if (go->bsd_compress) {
392         p[0] = CI_BSD_COMPRESS;
393         p[1] = CILEN_BSD_COMPRESS;
394         p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits);
395         if (p != p0) {
396             p += CILEN_BSD_COMPRESS;    /* not the first option */
397         } else {
398             for (;;) {
399                 res = ccp_test(f->unit, p, CILEN_BSD_COMPRESS, 0);
400                 if (res > 0) {
401                     p += CILEN_BSD_COMPRESS;
402                     break;
403                 }
404                 if (res < 0 || go->bsd_bits <= BSD_MIN_BITS) {
405                     go->bsd_compress = 0;
406                     break;
407                 }
408                 --go->bsd_bits;
409                 p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits);
410             }
411         }
412     }
413     /* XXX Should Predictor 2 be preferable to Predictor 1? */
414     if (go->predictor_1) {
415         p[0] = CI_PREDICTOR_1;
416         p[1] = CILEN_PREDICTOR_1;
417         if (p == p0 && ccp_test(f->unit, p, CILEN_PREDICTOR_1, 0) <= 0) {
418             go->predictor_1 = 0;
419         } else {
420             p += CILEN_PREDICTOR_1;
421         }
422     }
423     if (go->predictor_2) {
424         p[0] = CI_PREDICTOR_2;
425         p[1] = CILEN_PREDICTOR_2;
426         if (p == p0 && ccp_test(f->unit, p, CILEN_PREDICTOR_2, 0) <= 0) {
427             go->predictor_2 = 0;
428         } else {
429             p += CILEN_PREDICTOR_2;
430         }
431     }
432
433     go->method = (p > p0)? p0[0]: -1;
434
435     *lenp = p - p0;
436 }
437
438 /*
439  * ccp_ackci - process a received configure-ack, and return
440  * 1 iff the packet was OK.
441  */
442 static int
443 ccp_ackci(f, p, len)
444     fsm *f;
445     u_char *p;
446     int len;
447 {
448     ccp_options *go = &ccp_gotoptions[f->unit];
449     u_char *p0 = p;
450
451     if (go->deflate) {
452         if (len < CILEN_DEFLATE
453             || p[0] != CI_DEFLATE || 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     }
463     if (go->bsd_compress) {
464         if (len < CILEN_BSD_COMPRESS
465             || p[0] != CI_BSD_COMPRESS || p[1] != CILEN_BSD_COMPRESS
466             || p[2] != BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits))
467             return 0;
468         p += CILEN_BSD_COMPRESS;
469         len -= CILEN_BSD_COMPRESS;
470         /* XXX Cope with first/fast ack */
471         if (p == p0 && len == 0)
472             return 1;
473     }
474     if (go->predictor_1) {
475         if (len < CILEN_PREDICTOR_1
476             || p[0] != CI_PREDICTOR_1 || p[1] != CILEN_PREDICTOR_1)
477             return 0;
478         p += CILEN_PREDICTOR_1;
479         len -= CILEN_PREDICTOR_1;
480         /* XXX Cope with first/fast ack */
481         if (p == p0 && len == 0)
482             return 1;
483     }
484     if (go->predictor_2) {
485         if (len < CILEN_PREDICTOR_2
486             || p[0] != CI_PREDICTOR_2 || p[1] != CILEN_PREDICTOR_2)
487             return 0;
488         p += CILEN_PREDICTOR_2;
489         len -= CILEN_PREDICTOR_2;
490         /* XXX Cope with first/fast ack */
491         if (p == p0 && len == 0)
492             return 1;
493     }
494
495     if (len != 0)
496         return 0;
497     return 1;
498 }
499
500 /*
501  * ccp_nakci - process received configure-nak.
502  * Returns 1 iff the nak was OK.
503  */
504 static int
505 ccp_nakci(f, p, len)
506     fsm *f;
507     u_char *p;
508     int len;
509 {
510     ccp_options *go = &ccp_gotoptions[f->unit];
511     ccp_options no;             /* options we've seen already */
512     ccp_options try;            /* options to ask for next time */
513
514     memset(&no, 0, sizeof(no));
515     try = *go;
516
517     if (go->deflate && len >= CILEN_DEFLATE
518         && p[0] == CI_DEFLATE && p[1] == CILEN_DEFLATE) {
519         no.deflate = 1;
520         /*
521          * Peer wants us to use a different code size or something.
522          * Stop asking for Deflate if we don't understand his suggestion.
523          */
524         if (DEFLATE_METHOD(p[2]) != DEFLATE_METHOD_VAL
525             || DEFLATE_SIZE(p[2]) < DEFLATE_MIN_SIZE
526             || p[3] != DEFLATE_CHK_SEQUENCE)
527             try.deflate = 0;
528         else if (DEFLATE_SIZE(p[2]) < go->deflate_size)
529             try.deflate_size = DEFLATE_SIZE(p[2]);
530         p += CILEN_DEFLATE;
531         len -= CILEN_DEFLATE;
532     }
533
534     if (go->bsd_compress && len >= CILEN_BSD_COMPRESS
535         && p[0] == CI_BSD_COMPRESS && p[1] == CILEN_BSD_COMPRESS) {
536         no.bsd_compress = 1;
537         /*
538          * Peer wants us to use a different number of bits
539          * or a different version.
540          */
541         if (BSD_VERSION(p[2]) != BSD_CURRENT_VERSION)
542             try.bsd_compress = 0;
543         else if (BSD_NBITS(p[2]) < go->bsd_bits)
544             try.bsd_bits = BSD_NBITS(p[2]);
545         p += CILEN_BSD_COMPRESS;
546         len -= CILEN_BSD_COMPRESS;
547     }
548
549     /*
550      * Predictor-1 and 2 have no options, so they can't be Naked.
551      *
552      * XXX What should we do with any remaining options?
553      */
554
555     if (len != 0)
556         return 0;
557
558     if (f->state != OPENED)
559         *go = try;
560     return 1;
561 }
562
563 /*
564  * ccp_rejci - reject some of our suggested compression methods.
565  */
566 static int
567 ccp_rejci(f, p, len)
568     fsm *f;
569     u_char *p;
570     int len;
571 {
572     ccp_options *go = &ccp_gotoptions[f->unit];
573     ccp_options try;            /* options to request next time */
574
575     try = *go;
576
577     /*
578      * Cope with empty configure-rejects by ceasing to send
579      * configure-requests.
580      */
581     if (len == 0 && all_rejected[f->unit])
582         return -1;
583
584     if (go->deflate && len >= CILEN_DEFLATE
585         && p[0] == CI_DEFLATE && p[1] == CILEN_DEFLATE) {
586         if (p[2] != DEFLATE_MAKE_OPT(go->deflate_size)
587             || p[3] != DEFLATE_CHK_SEQUENCE)
588             return 0;           /* Rej is bad */
589         try.deflate = 0;
590         p += CILEN_DEFLATE;
591         len -= CILEN_DEFLATE;
592     }
593     if (go->bsd_compress && len >= CILEN_BSD_COMPRESS
594         && p[0] == CI_BSD_COMPRESS && p[1] == CILEN_BSD_COMPRESS) {
595         if (p[2] != BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits))
596             return 0;
597         try.bsd_compress = 0;
598         p += CILEN_BSD_COMPRESS;
599         len -= CILEN_BSD_COMPRESS;
600     }
601     if (go->predictor_1 && len >= CILEN_PREDICTOR_1
602         && p[0] == CI_PREDICTOR_1 && p[1] == CILEN_PREDICTOR_1) {
603         try.predictor_1 = 0;
604         p += CILEN_PREDICTOR_1;
605         len -= CILEN_PREDICTOR_1;
606     }
607     if (go->predictor_2 && len >= CILEN_PREDICTOR_2
608         && p[0] == CI_PREDICTOR_2 && p[1] == CILEN_PREDICTOR_2) {
609         try.predictor_2 = 0;
610         p += CILEN_PREDICTOR_2;
611         len -= CILEN_PREDICTOR_2;
612     }
613
614     if (len != 0)
615         return 0;
616
617     if (f->state != OPENED)
618         *go = try;
619
620     return 1;
621 }
622
623 /*
624  * ccp_reqci - processed a received configure-request.
625  * Returns CONFACK, CONFNAK or CONFREJ and the packet modified
626  * appropriately.
627  */
628 static int
629 ccp_reqci(f, p, lenp, dont_nak)
630     fsm *f;
631     u_char *p;
632     int *lenp;
633     int dont_nak;
634 {
635     int ret, newret, res;
636     u_char *p0, *retp;
637     int len, clen, type, nb;
638     ccp_options *ho = &ccp_hisoptions[f->unit];
639     ccp_options *ao = &ccp_allowoptions[f->unit];
640
641     ret = CONFACK;
642     retp = p0 = p;
643     len = *lenp;
644
645     memset(ho, 0, sizeof(ccp_options));
646     ho->method = (len > 0)? p[0]: -1;
647
648     while (len > 0) {
649         newret = CONFACK;
650         if (len < 2 || p[1] < 2 || p[1] > len) {
651             /* length is bad */
652             clen = len;
653             newret = CONFREJ;
654
655         } else {
656             type = p[0];
657             clen = p[1];
658
659             switch (type) {
660             case CI_DEFLATE:
661                 if (!ao->deflate || clen != CILEN_DEFLATE) {
662                     newret = CONFREJ;
663                     break;
664                 }
665
666                 ho->deflate = 1;
667                 ho->deflate_size = nb = DEFLATE_SIZE(p[2]);
668                 if (DEFLATE_METHOD(p[2]) != DEFLATE_METHOD_VAL
669                     || p[3] != DEFLATE_CHK_SEQUENCE
670                     || nb > ao->deflate_size || nb < DEFLATE_MIN_SIZE) {
671                     newret = CONFNAK;
672                     if (!dont_nak) {
673                         p[2] = DEFLATE_MAKE_OPT(ao->deflate_size);
674                         p[3] = DEFLATE_CHK_SEQUENCE;
675                         /* fall through to test this #bits below */
676                     } else
677                         break;
678                 }
679
680                 /*
681                  * Check whether we can do Deflate with the window
682                  * size they want.  If the window is too big, reduce
683                  * it until the kernel can cope and nak with that.
684                  * We only check this for the first option.
685                  */
686                 if (p == p0) {
687                     for (;;) {
688                         res = ccp_test(f->unit, p, CILEN_DEFLATE, 1);
689                         if (res > 0)
690                             break;              /* it's OK now */
691                         if (res < 0 || nb == DEFLATE_MIN_SIZE || dont_nak) {
692                             newret = CONFREJ;
693                             p[2] = DEFLATE_MAKE_OPT(ho->deflate_size);
694                             break;
695                         }
696                         newret = CONFNAK;
697                         --nb;
698                         p[2] = DEFLATE_MAKE_OPT(nb);
699                     }
700                 }
701                 break;
702
703             case CI_BSD_COMPRESS:
704                 if (!ao->bsd_compress || clen != CILEN_BSD_COMPRESS) {
705                     newret = CONFREJ;
706                     break;
707                 }
708
709                 ho->bsd_compress = 1;
710                 ho->bsd_bits = nb = BSD_NBITS(p[2]);
711                 if (BSD_VERSION(p[2]) != BSD_CURRENT_VERSION
712                     || nb > ao->bsd_bits || nb < BSD_MIN_BITS) {
713                     newret = CONFNAK;
714                     if (!dont_nak) {
715                         p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, ao->bsd_bits);
716                         /* fall through to test this #bits below */
717                     } else
718                         break;
719                 }
720
721                 /*
722                  * Check whether we can do BSD-Compress with the code
723                  * size they want.  If the code size is too big, reduce
724                  * it until the kernel can cope and nak with that.
725                  * We only check this for the first option.
726                  */
727                 if (p == p0) {
728                     for (;;) {
729                         res = ccp_test(f->unit, p, CILEN_BSD_COMPRESS, 1);
730                         if (res > 0)
731                             break;
732                         if (res < 0 || nb == BSD_MIN_BITS || dont_nak) {
733                             newret = CONFREJ;
734                             p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION,
735                                                 ho->bsd_bits);
736                             break;
737                         }
738                         newret = CONFNAK;
739                         --nb;
740                         p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, nb);
741                     }
742                 }
743                 break;
744
745             case CI_PREDICTOR_1:
746                 if (!ao->predictor_1 || clen != CILEN_PREDICTOR_1) {
747                     newret = CONFREJ;
748                     break;
749                 }
750
751                 ho->predictor_1 = 1;
752                 if (p == p0
753                     && ccp_test(f->unit, p, CILEN_PREDICTOR_1, 1) <= 0) {
754                     newret = CONFREJ;
755                 }
756                 break;
757
758             case CI_PREDICTOR_2:
759                 if (!ao->predictor_2 || clen != CILEN_PREDICTOR_2) {
760                     newret = CONFREJ;
761                     break;
762                 }
763
764                 ho->predictor_2 = 1;
765                 if (p == p0
766                     && ccp_test(f->unit, p, CILEN_PREDICTOR_2, 1) <= 0) {
767                     newret = CONFREJ;
768                 }
769                 break;
770
771             default:
772                 newret = CONFREJ;
773             }
774         }
775
776         if (newret == CONFNAK && dont_nak)
777             newret = CONFREJ;
778         if (!(newret == CONFACK || (newret == CONFNAK && ret == CONFREJ))) {
779             /* we're returning this option */
780             if (newret == CONFREJ && ret == CONFNAK)
781                 retp = p0;
782             ret = newret;
783             if (p != retp)
784                 BCOPY(p, retp, clen);
785             retp += clen;
786         }
787
788         p += clen;
789         len -= clen;
790     }
791
792     if (ret != CONFACK) {
793         if (ret == CONFREJ && *lenp == retp - p0)
794             all_rejected[f->unit] = 1;
795         else
796             *lenp = retp - p0;
797     }
798     return ret;
799 }
800
801 /*
802  * Make a string name for a compression method (or 2).
803  */
804 static char *
805 method_name(opt, opt2)
806     ccp_options *opt, *opt2;
807 {
808     static char result[64];
809
810     if (!ANY_COMPRESS(*opt))
811         return "(none)";
812     switch (opt->method) {
813     case CI_DEFLATE:
814         if (opt2 != NULL && opt2->deflate_size != opt->deflate_size)
815             sprintf(result, "Deflate (%d/%d)", opt->deflate_size,
816                     opt2->deflate_size);
817         else
818             sprintf(result, "Deflate (%d)", opt->deflate_size);
819         break;
820     case CI_BSD_COMPRESS:
821         if (opt2 != NULL && opt2->bsd_bits != opt->bsd_bits)
822             sprintf(result, "BSD-Compress (%d/%d)", opt->bsd_bits,
823                     opt2->bsd_bits);
824         else
825             sprintf(result, "BSD-Compress (%d)", opt->bsd_bits);
826         break;
827     case CI_PREDICTOR_1:
828         return "Predictor 1";
829     case CI_PREDICTOR_2:
830         return "Predictor 2";
831     default:
832         sprintf(result, "Method %d", opt->method);
833     }
834     return result;
835 }
836
837 /*
838  * CCP has come up - inform the kernel driver and log a message.
839  */
840 static void
841 ccp_up(f)
842     fsm *f;
843 {
844     ccp_options *go = &ccp_gotoptions[f->unit];
845     ccp_options *ho = &ccp_hisoptions[f->unit];
846     char method1[64];
847
848     ccp_flags_set(f->unit, 1, 1);
849     if (ANY_COMPRESS(*go)) {
850         if (ANY_COMPRESS(*ho)) {
851             if (go->method == ho->method) {
852                 syslog(LOG_NOTICE, "%s compression enabled",
853                        method_name(go, ho));
854             } else {
855                 strcpy(method1, method_name(go, NULL));
856                 syslog(LOG_NOTICE, "%s / %s compression enabled",
857                        method1, method_name(ho, NULL));
858             }
859         } else
860             syslog(LOG_NOTICE, "%s receive compression enabled",
861                    method_name(go, NULL));
862     } else if (ANY_COMPRESS(*ho))
863         syslog(LOG_NOTICE, "%s transmit compression enabled",
864                method_name(ho, NULL));
865 }
866
867 /*
868  * CCP has gone down - inform the kernel driver.
869  */
870 static void
871 ccp_down(f)
872     fsm *f;
873 {
874     if (ccp_localstate[f->unit] & RACK_PENDING)
875         UNTIMEOUT(ccp_rack_timeout, f);
876     ccp_localstate[f->unit] = 0;
877     ccp_flags_set(f->unit, 1, 0);
878 }
879
880 /*
881  * Print the contents of a CCP packet.
882  */
883 static char *ccp_codenames[] = {
884     "ConfReq", "ConfAck", "ConfNak", "ConfRej",
885     "TermReq", "TermAck", "CodeRej",
886     NULL, NULL, NULL, NULL, NULL, NULL,
887     "ResetReq", "ResetAck",
888 };
889
890 static int
891 ccp_printpkt(p, plen, printer, arg)
892     u_char *p;
893     int plen;
894     void (*printer) __P((void *, char *, ...));
895     void *arg;
896 {
897     u_char *p0, *optend;
898     int code, id, len;
899     int optlen;
900
901     p0 = p;
902     if (plen < HEADERLEN)
903         return 0;
904     code = p[0];
905     id = p[1];
906     len = (p[2] << 8) + p[3];
907     if (len < HEADERLEN || len > plen)
908         return 0;
909
910     if (code >= 1 && code <= sizeof(ccp_codenames) / sizeof(char *)
911         && ccp_codenames[code-1] != NULL)
912         printer(arg, " %s", ccp_codenames[code-1]);
913     else
914         printer(arg, " code=0x%x", code);
915     printer(arg, " id=0x%x", id);
916     len -= HEADERLEN;
917     p += HEADERLEN;
918
919     switch (code) {
920     case CONFREQ:
921     case CONFACK:
922     case CONFNAK:
923     case CONFREJ:
924         /* print list of possible compression methods */
925         while (len >= 2) {
926             code = p[0];
927             optlen = p[1];
928             if (optlen < 2 || optlen > len)
929                 break;
930             printer(arg, " <");
931             len -= optlen;
932             optend = p + optlen;
933             switch (code) {
934             case CI_DEFLATE:
935                 if (optlen >= CILEN_DEFLATE) {
936                     printer(arg, "deflate %d", DEFLATE_SIZE(p[2]));
937                     if (DEFLATE_METHOD(p[2]) != DEFLATE_METHOD_VAL)
938                         printer(arg, " method %d", DEFLATE_METHOD(p[2]));
939                     if (p[3] != DEFLATE_CHK_SEQUENCE)
940                         printer(arg, " check %d", p[3]);
941                     p += CILEN_DEFLATE;
942                 }
943                 break;
944             case CI_BSD_COMPRESS:
945                 if (optlen >= CILEN_BSD_COMPRESS) {
946                     printer(arg, "bsd v%d %d", BSD_VERSION(p[2]),
947                             BSD_NBITS(p[2]));
948                     p += CILEN_BSD_COMPRESS;
949                 }
950                 break;
951             case CI_PREDICTOR_1:
952                 if (optlen >= CILEN_PREDICTOR_1) {
953                     printer(arg, "predictor 1");
954                     p += CILEN_PREDICTOR_1;
955                 }
956                 break;
957             case CI_PREDICTOR_2:
958                 if (optlen >= CILEN_PREDICTOR_2) {
959                     printer(arg, "predictor 2");
960                     p += CILEN_PREDICTOR_2;
961                 }
962                 break;
963             }
964             while (p < optend)
965                 printer(arg, " %.2x", *p++);
966             printer(arg, ">");
967         }
968         break;
969
970     case TERMACK:
971     case TERMREQ:
972         if (len > 0 && *p >= ' ' && *p < 0x7f) {
973             print_string(p, len, printer, arg);
974             p += len;
975             len = 0;
976         }
977         break;
978     }
979
980     /* dump out the rest of the packet in hex */
981     while (--len >= 0)
982         printer(arg, " %.2x", *p++);
983
984     return p - p0;
985 }
986
987 /*
988  * We have received a packet that the decompressor failed to
989  * decompress.  Here we would expect to issue a reset-request, but
990  * Motorola has a patent on resetting the compressor as a result of
991  * detecting an error in the decompressed data after decompression.
992  * (See US patent 5,130,993; international patent publication number
993  * WO 91/10289; Australian patent 73296/91.)
994  *
995  * So we ask the kernel whether the error was detected after
996  * decompression; if it was, we take CCP down, thus disabling
997  * compression :-(, otherwise we issue the reset-request.
998  */
999 static void
1000 ccp_datainput(unit, pkt, len)
1001     int unit;
1002     u_char *pkt;
1003     int len;
1004 {
1005     fsm *f;
1006
1007     f = &ccp_fsm[unit];
1008     if (f->state == OPENED) {
1009         if (ccp_fatal_error(unit)) {
1010             /*
1011              * Disable compression by taking CCP down.
1012              */
1013             syslog(LOG_ERR, "Lost compression sync: disabling compression");
1014             ccp_close(unit, "Lost compression sync");
1015         } else {
1016             /*
1017              * Send a reset-request to reset the peer's compressor.
1018              * We don't do that if we are still waiting for an
1019              * acknowledgement to a previous reset-request.
1020              */
1021             if (!(ccp_localstate[f->unit] & RACK_PENDING)) {
1022                 fsm_sdata(f, CCP_RESETREQ, f->reqid = ++f->id, NULL, 0);
1023                 TIMEOUT(ccp_rack_timeout, f, RACKTIMEOUT);
1024                 ccp_localstate[f->unit] |= RACK_PENDING;
1025             } else
1026                 ccp_localstate[f->unit] |= RREQ_REPEAT;
1027         }
1028     }
1029 }
1030
1031 /*
1032  * Timeout waiting for reset-ack.
1033  */
1034 static void
1035 ccp_rack_timeout(arg)
1036     void *arg;
1037 {
1038     fsm *f = arg;
1039
1040     if (f->state == OPENED && ccp_localstate[f->unit] & RREQ_REPEAT) {
1041         fsm_sdata(f, CCP_RESETREQ, f->reqid, NULL, 0);
1042         TIMEOUT(ccp_rack_timeout, f, RACKTIMEOUT);
1043         ccp_localstate[f->unit] &= ~RREQ_REPEAT;
1044     } else
1045         ccp_localstate[f->unit] &= ~RACK_PENDING;
1046 }
1047