]> git.ozlabs.org Git - ppp.git/blob - pppd/ccp.c
accommodate recent 2.1.x kernels
[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.20 1997/04/30 05:50:40 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             go->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                     }
676                     break;
677                 }
678
679                 /*
680                  * Check whether we can do Deflate with the window
681                  * size they want.  If the window is too big, reduce
682                  * it until the kernel can cope and nak with that.
683                  * We only check this for the first option.
684                  */
685                 if (p == p0) {
686                     for (;;) {
687                         res = ccp_test(f->unit, p, CILEN_DEFLATE, 1);
688                         if (res > 0)
689                             break;              /* it's OK now */
690                         if (res < 0 || nb == DEFLATE_MIN_SIZE || dont_nak) {
691                             newret = CONFREJ;
692                             p[2] = DEFLATE_MAKE_OPT(ho->deflate_size);
693                             break;
694                         }
695                         newret = CONFNAK;
696                         --nb;
697                         p[2] = DEFLATE_MAKE_OPT(nb);
698                     }
699                 }
700                 break;
701
702             case CI_BSD_COMPRESS:
703                 if (!ao->bsd_compress || clen != CILEN_BSD_COMPRESS) {
704                     newret = CONFREJ;
705                     break;
706                 }
707
708                 ho->bsd_compress = 1;
709                 ho->bsd_bits = nb = BSD_NBITS(p[2]);
710                 if (BSD_VERSION(p[2]) != BSD_CURRENT_VERSION
711                     || nb > ao->bsd_bits || nb < BSD_MIN_BITS) {
712                     newret = CONFNAK;
713                     if (!dont_nak)
714                         p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, ao->bsd_bits);
715                     break;
716                 }
717
718                 /*
719                  * Check whether we can do BSD-Compress with the code
720                  * size they want.  If the code size is too big, reduce
721                  * it until the kernel can cope and nak with that.
722                  * We only check this for the first option.
723                  */
724                 if (p == p0) {
725                     for (;;) {
726                         res = ccp_test(f->unit, p, CILEN_BSD_COMPRESS, 1);
727                         if (res > 0)
728                             break;
729                         if (res < 0 || nb == BSD_MIN_BITS || dont_nak) {
730                             newret = CONFREJ;
731                             p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION,
732                                                 ho->bsd_bits);
733                             break;
734                         }
735                         newret = CONFNAK;
736                         --nb;
737                         p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, nb);
738                     }
739                 }
740                 break;
741
742             case CI_PREDICTOR_1:
743                 if (!ao->predictor_1 || clen != CILEN_PREDICTOR_1) {
744                     newret = CONFREJ;
745                     break;
746                 }
747
748                 ho->predictor_1 = 1;
749                 if (p == p0
750                     && ccp_test(f->unit, p, CILEN_PREDICTOR_1, 1) <= 0) {
751                     newret = CONFREJ;
752                 }
753                 break;
754
755             case CI_PREDICTOR_2:
756                 if (!ao->predictor_2 || clen != CILEN_PREDICTOR_2) {
757                     newret = CONFREJ;
758                     break;
759                 }
760
761                 ho->predictor_2 = 1;
762                 if (p == p0
763                     && ccp_test(f->unit, p, CILEN_PREDICTOR_2, 1) <= 0) {
764                     newret = CONFREJ;
765                 }
766                 break;
767
768             default:
769                 newret = CONFREJ;
770             }
771         }
772
773         if (newret == CONFNAK && dont_nak)
774             newret = CONFREJ;
775         if (!(newret == CONFACK || (newret == CONFNAK && ret == CONFREJ))) {
776             /* we're returning this option */
777             if (newret == CONFREJ && ret == CONFNAK)
778                 retp = p0;
779             ret = newret;
780             if (p != retp)
781                 BCOPY(p, retp, clen);
782             retp += clen;
783         }
784
785         p += clen;
786         len -= clen;
787     }
788
789     if (ret != CONFACK) {
790         if (ret == CONFREJ && *lenp == retp - p0)
791             all_rejected[f->unit] = 1;
792         else
793             *lenp = retp - p0;
794     }
795     return ret;
796 }
797
798 /*
799  * Make a string name for a compression method (or 2).
800  */
801 static char *
802 method_name(opt, opt2)
803     ccp_options *opt, *opt2;
804 {
805     static char result[64];
806
807     if (!ANY_COMPRESS(*opt))
808         return "(none)";
809     switch (opt->method) {
810     case CI_DEFLATE:
811         if (opt2 != NULL && opt2->deflate_size != opt->deflate_size)
812             sprintf(result, "Deflate (%d/%d)", opt->deflate_size,
813                     opt2->deflate_size);
814         else
815             sprintf(result, "Deflate (%d)", opt->deflate_size);
816         break;
817     case CI_BSD_COMPRESS:
818         if (opt2 != NULL && opt2->bsd_bits != opt->bsd_bits)
819             sprintf(result, "BSD-Compress (%d/%d)", opt->bsd_bits,
820                     opt2->bsd_bits);
821         else
822             sprintf(result, "BSD-Compress (%d)", opt->bsd_bits);
823         break;
824     case CI_PREDICTOR_1:
825         return "Predictor 1";
826     case CI_PREDICTOR_2:
827         return "Predictor 2";
828     default:
829         sprintf(result, "Method %d", opt->method);
830     }
831     return result;
832 }
833
834 /*
835  * CCP has come up - inform the kernel driver and log a message.
836  */
837 static void
838 ccp_up(f)
839     fsm *f;
840 {
841     ccp_options *go = &ccp_gotoptions[f->unit];
842     ccp_options *ho = &ccp_hisoptions[f->unit];
843     char method1[64];
844
845     ccp_flags_set(f->unit, 1, 1);
846     if (ANY_COMPRESS(*go)) {
847         if (ANY_COMPRESS(*ho)) {
848             if (go->method == ho->method) {
849                 syslog(LOG_NOTICE, "%s compression enabled",
850                        method_name(go, ho));
851             } else {
852                 strcpy(method1, method_name(go, NULL));
853                 syslog(LOG_NOTICE, "%s / %s compression enabled",
854                        method1, method_name(ho, NULL));
855             }
856         } else
857             syslog(LOG_NOTICE, "%s receive compression enabled",
858                    method_name(go, NULL));
859     } else if (ANY_COMPRESS(*ho))
860         syslog(LOG_NOTICE, "%s transmit compression enabled",
861                method_name(ho, NULL));
862 }
863
864 /*
865  * CCP has gone down - inform the kernel driver.
866  */
867 static void
868 ccp_down(f)
869     fsm *f;
870 {
871     if (ccp_localstate[f->unit] & RACK_PENDING)
872         UNTIMEOUT(ccp_rack_timeout, f);
873     ccp_localstate[f->unit] = 0;
874     ccp_flags_set(f->unit, 1, 0);
875 }
876
877 /*
878  * Print the contents of a CCP packet.
879  */
880 static char *ccp_codenames[] = {
881     "ConfReq", "ConfAck", "ConfNak", "ConfRej",
882     "TermReq", "TermAck", "CodeRej",
883     NULL, NULL, NULL, NULL, NULL, NULL,
884     "ResetReq", "ResetAck",
885 };
886
887 static int
888 ccp_printpkt(p, plen, printer, arg)
889     u_char *p;
890     int plen;
891     void (*printer) __P((void *, char *, ...));
892     void *arg;
893 {
894     u_char *p0, *optend;
895     int code, id, len;
896     int optlen;
897
898     p0 = p;
899     if (plen < HEADERLEN)
900         return 0;
901     code = p[0];
902     id = p[1];
903     len = (p[2] << 8) + p[3];
904     if (len < HEADERLEN || len > plen)
905         return 0;
906
907     if (code >= 1 && code <= sizeof(ccp_codenames) / sizeof(char *)
908         && ccp_codenames[code-1] != NULL)
909         printer(arg, " %s", ccp_codenames[code-1]);
910     else
911         printer(arg, " code=0x%x", code);
912     printer(arg, " id=0x%x", id);
913     len -= HEADERLEN;
914     p += HEADERLEN;
915
916     switch (code) {
917     case CONFREQ:
918     case CONFACK:
919     case CONFNAK:
920     case CONFREJ:
921         /* print list of possible compression methods */
922         while (len >= 2) {
923             code = p[0];
924             optlen = p[1];
925             if (optlen < 2 || optlen > len)
926                 break;
927             printer(arg, " <");
928             len -= optlen;
929             optend = p + optlen;
930             switch (code) {
931             case CI_DEFLATE:
932                 if (optlen >= CILEN_DEFLATE) {
933                     printer(arg, "deflate %d", DEFLATE_SIZE(p[2]));
934                     if (DEFLATE_METHOD(p[2]) != DEFLATE_METHOD_VAL)
935                         printer(arg, " method %d", DEFLATE_METHOD(p[2]));
936                     if (p[3] != DEFLATE_CHK_SEQUENCE)
937                         printer(arg, " check %d", p[3]);
938                     p += CILEN_DEFLATE;
939                 }
940                 break;
941             case CI_BSD_COMPRESS:
942                 if (optlen >= CILEN_BSD_COMPRESS) {
943                     printer(arg, "bsd v%d %d", BSD_VERSION(p[2]),
944                             BSD_NBITS(p[2]));
945                     p += CILEN_BSD_COMPRESS;
946                 }
947                 break;
948             case CI_PREDICTOR_1:
949                 if (optlen >= CILEN_PREDICTOR_1) {
950                     printer(arg, "predictor 1");
951                     p += CILEN_PREDICTOR_1;
952                 }
953                 break;
954             case CI_PREDICTOR_2:
955                 if (optlen >= CILEN_PREDICTOR_2) {
956                     printer(arg, "predictor 2");
957                     p += CILEN_PREDICTOR_2;
958                 }
959                 break;
960             }
961             while (p < optend)
962                 printer(arg, " %.2x", *p++);
963             printer(arg, ">");
964         }
965         break;
966
967     case TERMACK:
968     case TERMREQ:
969         if (len > 0 && *p >= ' ' && *p < 0x7f) {
970             print_string(p, len, printer, arg);
971             p += len;
972             len = 0;
973         }
974         break;
975     }
976
977     /* dump out the rest of the packet in hex */
978     while (--len >= 0)
979         printer(arg, " %.2x", *p++);
980
981     return p - p0;
982 }
983
984 /*
985  * We have received a packet that the decompressor failed to
986  * decompress.  Here we would expect to issue a reset-request, but
987  * Motorola has a patent on resetting the compressor as a result of
988  * detecting an error in the decompressed data after decompression.
989  * (See US patent 5,130,993; international patent publication number
990  * WO 91/10289; Australian patent 73296/91.)
991  *
992  * So we ask the kernel whether the error was detected after
993  * decompression; if it was, we take CCP down, thus disabling
994  * compression :-(, otherwise we issue the reset-request.
995  */
996 static void
997 ccp_datainput(unit, pkt, len)
998     int unit;
999     u_char *pkt;
1000     int len;
1001 {
1002     fsm *f;
1003
1004     f = &ccp_fsm[unit];
1005     if (f->state == OPENED) {
1006         if (ccp_fatal_error(unit)) {
1007             /*
1008              * Disable compression by taking CCP down.
1009              */
1010             syslog(LOG_ERR, "Lost compression sync: disabling compression");
1011             ccp_close(unit, "Lost compression sync");
1012         } else {
1013             /*
1014              * Send a reset-request to reset the peer's compressor.
1015              * We don't do that if we are still waiting for an
1016              * acknowledgement to a previous reset-request.
1017              */
1018             if (!(ccp_localstate[f->unit] & RACK_PENDING)) {
1019                 fsm_sdata(f, CCP_RESETREQ, f->reqid = ++f->id, NULL, 0);
1020                 TIMEOUT(ccp_rack_timeout, f, RACKTIMEOUT);
1021                 ccp_localstate[f->unit] |= RACK_PENDING;
1022             } else
1023                 ccp_localstate[f->unit] |= RREQ_REPEAT;
1024         }
1025     }
1026 }
1027
1028 /*
1029  * Timeout waiting for reset-ack.
1030  */
1031 static void
1032 ccp_rack_timeout(arg)
1033     void *arg;
1034 {
1035     fsm *f = arg;
1036
1037     if (f->state == OPENED && ccp_localstate[f->unit] & RREQ_REPEAT) {
1038         fsm_sdata(f, CCP_RESETREQ, f->reqid, NULL, 0);
1039         TIMEOUT(ccp_rack_timeout, f, RACKTIMEOUT);
1040         ccp_localstate[f->unit] &= ~RREQ_REPEAT;
1041     } else
1042         ccp_localstate[f->unit] &= ~RACK_PENDING;
1043 }
1044