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