source: rtems/cpukit/pppd/ccp.c @ 6ac6a5c8

5
Last change on this file since 6ac6a5c8 was 33a1a4db, checked in by Peng Fan <van.freenix@…>, on 04/05/16 at 12:45:55

cpukit: pppd: fix compile warning

rcsid is defined, but not used. So discard it.

Signed-off-by: Peng Fan <van.freenix@…>

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