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