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