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