2 * ccp.c - PPP Compression Control Protocol.
4 * Copyright (c) 1994 The Australian National University.
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
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
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,
29 static char rcsid[] = "$Id: ccp.c,v 1.15 1996/01/18 03:20:25 paulus Exp $";
33 #include <sys/ioctl.h>
34 #include <net/ppp-comp.h>
40 struct protent ccp_protent = {
41 PPP_CCP, ccp_init, ccp_input, ccp_protrej,
42 ccp_lowerup, ccp_lowerdown, ccp_open, ccp_close,
43 ccp_printpkt, ccp_datainput, 1, "CCP", NULL, NULL
47 ccp_options ccp_wantoptions[NUM_PPP]; /* what to request the peer to use */
48 ccp_options ccp_gotoptions[NUM_PPP]; /* what the peer agreed to do */
49 ccp_options ccp_allowoptions[NUM_PPP]; /* what we'll agree to do */
50 ccp_options ccp_hisoptions[NUM_PPP]; /* what we agreed to do */
53 * Callbacks for fsm code.
55 static void ccp_resetci __P((fsm *));
56 static int ccp_cilen __P((fsm *));
57 static void ccp_addci __P((fsm *, u_char *, int *));
58 static int ccp_ackci __P((fsm *, u_char *, int));
59 static int ccp_nakci __P((fsm *, u_char *, int));
60 static int ccp_rejci __P((fsm *, u_char *, int));
61 static int ccp_reqci __P((fsm *, u_char *, int *, int));
62 static void ccp_up __P((fsm *));
63 static void ccp_down __P((fsm *));
64 static int ccp_extcode __P((fsm *, int, int, u_char *, int));
65 static void ccp_rack_timeout __P(());
67 static fsm_callbacks ccp_callbacks = {
86 * Do we want / did we get any compression?
88 #define ANY_COMPRESS(opt) ((opt).deflate || (opt).bsd_compress \
89 || (opt).predictor_1 || (opt).predictor_2)
92 * Local state (mainly for handling reset-reqs and reset-acks).
94 static int ccp_localstate[NUM_PPP];
95 #define RACK_PENDING 1 /* waiting for reset-ack */
96 #define RREQ_REPEAT 2 /* send another reset-req if no reset-ack */
98 #define RACKTIMEOUT 1 /* second */
100 static int all_rejected[NUM_PPP]; /* we rejected all peer's options */
103 * ccp_init - initialize CCP.
109 fsm *f = &ccp_fsm[unit];
112 f->protocol = PPP_CCP;
113 f->callbacks = &ccp_callbacks;
116 memset(&ccp_wantoptions[unit], 0, sizeof(ccp_options));
117 memset(&ccp_gotoptions[unit], 0, sizeof(ccp_options));
118 memset(&ccp_allowoptions[unit], 0, sizeof(ccp_options));
119 memset(&ccp_hisoptions[unit], 0, sizeof(ccp_options));
121 ccp_wantoptions[0].deflate = 1;
122 ccp_wantoptions[0].deflate_size = DEFLATE_MAX_SIZE;
123 ccp_allowoptions[0].deflate = 1;
124 ccp_allowoptions[0].deflate_size = DEFLATE_MAX_SIZE;
126 ccp_wantoptions[0].bsd_compress = 1;
127 ccp_wantoptions[0].bsd_bits = BSD_MAX_BITS;
128 ccp_allowoptions[0].bsd_compress = 1;
129 ccp_allowoptions[0].bsd_bits = BSD_MAX_BITS;
131 ccp_allowoptions[0].predictor_1 = 1;
135 * ccp_open - CCP is allowed to come up.
141 fsm *f = &ccp_fsm[unit];
143 if (f->state != OPENED)
144 ccp_flags_set(unit, 1, 0);
145 if (!ANY_COMPRESS(ccp_wantoptions[unit]))
146 f->flags |= OPT_SILENT;
151 * ccp_close - Terminate CCP.
154 ccp_close(unit, reason)
158 ccp_flags_set(unit, 0, 0);
159 fsm_close(&ccp_fsm[unit], reason);
163 * ccp_lowerup - we may now transmit CCP packets.
169 fsm_lowerup(&ccp_fsm[unit]);
173 * ccp_lowerdown - we may not transmit CCP packets.
179 fsm_lowerdown(&ccp_fsm[unit]);
183 * ccp_input - process a received CCP packet.
186 ccp_input(unit, p, len)
191 fsm *f = &ccp_fsm[unit];
195 * Check for a terminate-request so we can print a message.
198 fsm_input(f, p, len);
199 if (oldstate == OPENED && p[0] == TERMREQ && f->state != OPENED)
200 syslog(LOG_NOTICE, "Compression disabled by peer.");
203 * If we get a terminate-ack and we're not asking for compression,
206 if (oldstate == REQSENT && p[0] == TERMACK
207 && !ANY_COMPRESS(ccp_gotoptions[unit]))
208 ccp_close(unit, "No compression negotiated");
212 * Handle a CCP-specific code.
215 ccp_extcode(f, code, id, p, len)
223 if (f->state != OPENED)
225 /* send a reset-ack, which the transmitter will see and
226 reset its compression state. */
227 fsm_sdata(f, CCP_RESETACK, id, NULL, 0);
231 if (ccp_localstate[f->unit] & RACK_PENDING && id == f->reqid) {
232 ccp_localstate[f->unit] &= ~(RACK_PENDING | RREQ_REPEAT);
233 UNTIMEOUT(ccp_rack_timeout, (caddr_t) f);
245 * ccp_protrej - peer doesn't talk CCP.
251 ccp_flags_set(unit, 0, 0);
252 fsm_lowerdown(&ccp_fsm[unit]);
256 * ccp_resetci - initialize at start of negotiation.
263 ccp_options *go = &ccp_gotoptions[f->unit];
266 *go = ccp_wantoptions[f->unit];
267 all_rejected[f->unit] = 0;
270 * Check whether the kernel knows about the various
271 * compression methods we might request.
273 if (go->bsd_compress) {
274 opt_buf[0] = CI_BSD_COMPRESS;
275 opt_buf[1] = CILEN_BSD_COMPRESS;
276 opt_buf[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, BSD_MIN_BITS);
277 if (ccp_test(f->unit, opt_buf, CILEN_BSD_COMPRESS, 0) <= 0)
278 go->bsd_compress = 0;
281 opt_buf[0] = CI_DEFLATE;
282 opt_buf[1] = CILEN_DEFLATE;
283 opt_buf[2] = DEFLATE_MAKE_OPT(DEFLATE_MIN_SIZE);
284 opt_buf[3] = DEFLATE_CHK_SEQUENCE;
285 if (ccp_test(f->unit, opt_buf, CILEN_DEFLATE, 0) <= 0)
288 if (go->predictor_1) {
289 opt_buf[0] = CI_PREDICTOR_1;
290 opt_buf[1] = CILEN_PREDICTOR_1;
291 if (ccp_test(f->unit, opt_buf, CILEN_PREDICTOR_1, 0) <= 0)
294 if (go->predictor_2) {
295 opt_buf[0] = CI_PREDICTOR_2;
296 opt_buf[1] = CILEN_PREDICTOR_2;
297 if (ccp_test(f->unit, opt_buf, CILEN_PREDICTOR_2, 0) <= 0)
303 * ccp_cilen - Return total length of our configuration info.
309 ccp_options *go = &ccp_gotoptions[f->unit];
311 return (go->bsd_compress? CILEN_BSD_COMPRESS: 0)
312 + (go->deflate? CILEN_DEFLATE: 0)
313 + (go->predictor_1? CILEN_PREDICTOR_1: 0)
314 + (go->predictor_2? CILEN_PREDICTOR_2: 0);
318 * ccp_addci - put our requests in a packet.
321 ccp_addci(f, p, lenp)
327 ccp_options *go = &ccp_gotoptions[f->unit];
331 * Add the compression types that we can receive, in decreasing
332 * preference order. Get the kernel to allocate the first one
333 * in case it gets Acked.
337 p[1] = CILEN_DEFLATE;
338 p[2] = DEFLATE_MAKE_OPT(go->deflate_size);
339 p[3] = DEFLATE_CHK_SEQUENCE;
341 res = ccp_test(f->unit, p, CILEN_DEFLATE, 0);
346 if (res < 0 || go->deflate_size <= DEFLATE_MIN_SIZE) {
351 p[2] = DEFLATE_MAKE_OPT(go->deflate_size);
354 if (go->bsd_compress) {
355 p[0] = CI_BSD_COMPRESS;
356 p[1] = CILEN_BSD_COMPRESS;
357 p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits);
359 p += CILEN_BSD_COMPRESS; /* not the first option */
362 res = ccp_test(f->unit, p, CILEN_BSD_COMPRESS, 0);
364 p += CILEN_BSD_COMPRESS;
367 if (res < 0 || go->bsd_bits <= BSD_MIN_BITS) {
368 go->bsd_compress = 0;
372 p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits);
376 /* XXX Should Predictor 2 be preferable to Predictor 1? */
377 if (go->predictor_1) {
378 p[0] = CI_PREDICTOR_1;
379 p[1] = CILEN_PREDICTOR_1;
380 if (p == p0 && ccp_test(f->unit, p, CILEN_PREDICTOR_1, 0) <= 0) {
383 p += CILEN_PREDICTOR_1;
386 if (go->predictor_2) {
387 p[0] = CI_PREDICTOR_2;
388 p[1] = CILEN_PREDICTOR_2;
389 if (p == p0 && ccp_test(f->unit, p, CILEN_PREDICTOR_2, 0) <= 0) {
392 p += CILEN_PREDICTOR_2;
400 * ccp_ackci - process a received configure-ack, and return
401 * 1 iff the packet was OK.
409 ccp_options *go = &ccp_gotoptions[f->unit];
413 if (len < CILEN_DEFLATE
414 || p[0] != CI_DEFLATE || p[1] != CILEN_DEFLATE
415 || p[2] != DEFLATE_MAKE_OPT(go->deflate_size)
416 || p[3] != DEFLATE_CHK_SEQUENCE)
419 len -= CILEN_DEFLATE;
420 /* XXX Cope with first/fast ack */
424 if (go->bsd_compress) {
425 if (len < CILEN_BSD_COMPRESS
426 || p[0] != CI_BSD_COMPRESS || p[1] != CILEN_BSD_COMPRESS
427 || p[2] != BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits))
429 p += CILEN_BSD_COMPRESS;
430 len -= CILEN_BSD_COMPRESS;
431 /* XXX Cope with first/fast ack */
432 if (p == p0 && len == 0)
435 if (go->predictor_1) {
436 if (len < CILEN_PREDICTOR_1
437 || p[0] != CI_PREDICTOR_1 || p[1] != CILEN_PREDICTOR_1)
439 p += CILEN_PREDICTOR_1;
440 len -= CILEN_PREDICTOR_1;
441 /* XXX Cope with first/fast ack */
442 if (p == p0 && len == 0)
445 if (go->predictor_2) {
446 if (len < CILEN_PREDICTOR_2
447 || p[0] != CI_PREDICTOR_2 || p[1] != CILEN_PREDICTOR_2)
449 p += CILEN_PREDICTOR_2;
450 len -= CILEN_PREDICTOR_2;
451 /* XXX Cope with first/fast ack */
452 if (p == p0 && len == 0)
462 * ccp_nakci - process received configure-nak.
463 * Returns 1 iff the nak was OK.
471 ccp_options *go = &ccp_gotoptions[f->unit];
472 ccp_options no; /* options we've seen already */
473 ccp_options try; /* options to ask for next time */
475 memset(&no, 0, sizeof(no));
478 if (go->deflate && len >= CILEN_DEFLATE
479 && p[0] == CI_DEFLATE && p[1] == CILEN_DEFLATE) {
482 * Peer wants us to use a different code size or something.
483 * Stop asking for Deflate if we don't understand his suggestion.
485 if (DEFLATE_METHOD(p[2]) != DEFLATE_METHOD_VAL
486 || DEFLATE_SIZE(p[2]) < DEFLATE_MIN_SIZE
487 || p[3] != DEFLATE_CHK_SEQUENCE)
489 else if (DEFLATE_SIZE(p[2]) < go->deflate_size)
490 go->deflate_size = DEFLATE_SIZE(p[2]);
492 len -= CILEN_DEFLATE;
495 if (go->bsd_compress && len >= CILEN_BSD_COMPRESS
496 && p[0] == CI_BSD_COMPRESS && p[1] == CILEN_BSD_COMPRESS) {
499 * Peer wants us to use a different number of bits
500 * or a different version.
502 if (BSD_VERSION(p[2]) != BSD_CURRENT_VERSION)
503 try.bsd_compress = 0;
504 else if (BSD_NBITS(p[2]) < go->bsd_bits)
505 try.bsd_bits = BSD_NBITS(p[2]);
506 p += CILEN_BSD_COMPRESS;
507 len -= CILEN_BSD_COMPRESS;
511 * Predictor-1 and 2 have no options, so they can't be Naked.
513 * XXX What should we do with any remaining options?
519 if (f->state != OPENED)
525 * ccp_rejci - reject some of our suggested compression methods.
533 ccp_options *go = &ccp_gotoptions[f->unit];
534 ccp_options try; /* options to request next time */
539 * Cope with empty configure-rejects by ceasing to send
540 * configure-requests.
542 if (len == 0 && all_rejected[f->unit])
545 if (go->deflate && len >= CILEN_DEFLATE
546 && p[0] == CI_DEFLATE && p[1] == CILEN_DEFLATE) {
547 if (p[2] != DEFLATE_MAKE_OPT(go->deflate_size)
548 || p[3] != DEFLATE_CHK_SEQUENCE)
549 return 0; /* Rej is bad */
552 len -= CILEN_DEFLATE;
554 if (go->bsd_compress && len >= CILEN_BSD_COMPRESS
555 && p[0] == CI_BSD_COMPRESS && p[1] == CILEN_BSD_COMPRESS) {
556 if (p[2] != BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits))
558 try.bsd_compress = 0;
559 p += CILEN_BSD_COMPRESS;
560 len -= CILEN_BSD_COMPRESS;
562 if (go->predictor_1 && len >= CILEN_PREDICTOR_1
563 && p[0] == CI_PREDICTOR_1 && p[1] == CILEN_PREDICTOR_1) {
565 p += CILEN_PREDICTOR_1;
566 len -= CILEN_PREDICTOR_1;
568 if (go->predictor_2 && len >= CILEN_PREDICTOR_2
569 && p[0] == CI_PREDICTOR_2 && p[1] == CILEN_PREDICTOR_2) {
571 p += CILEN_PREDICTOR_2;
572 len -= CILEN_PREDICTOR_2;
578 if (f->state != OPENED)
585 * ccp_reqci - processed a received configure-request.
586 * Returns CONFACK, CONFNAK or CONFREJ and the packet modified
590 ccp_reqci(f, p, lenp, dont_nak)
596 int ret, newret, res;
598 int len, clen, type, nb;
599 ccp_options *ho = &ccp_hisoptions[f->unit];
600 ccp_options *ao = &ccp_allowoptions[f->unit];
606 memset(ho, 0, sizeof(ccp_options));
610 if (len < 2 || p[1] < 2 || p[1] > len) {
621 if (!ao->deflate || clen != CILEN_DEFLATE) {
627 ho->deflate_size = nb = DEFLATE_SIZE(p[2]);
628 if (DEFLATE_METHOD(p[2]) != DEFLATE_METHOD_VAL
629 || p[3] != DEFLATE_CHK_SEQUENCE
630 || nb > ao->deflate_size || nb < DEFLATE_MIN_SIZE) {
633 p[2] = DEFLATE_MAKE_OPT(ao->deflate_size);
634 p[3] = DEFLATE_CHK_SEQUENCE;
640 * Check whether we can do Deflate with the window
641 * size they want. If the window is too big, reduce
642 * it until the kernel can cope and nak with that.
643 * We only check this for the first option.
647 res = ccp_test(f->unit, p, CILEN_DEFLATE, 1);
649 break; /* it's OK now */
650 if (res < 0 || nb == DEFLATE_MIN_SIZE || dont_nak) {
652 p[2] = DEFLATE_MAKE_OPT(ho->deflate_size);
657 p[2] = DEFLATE_MAKE_OPT(nb);
662 case CI_BSD_COMPRESS:
663 if (!ao->bsd_compress || clen != CILEN_BSD_COMPRESS) {
668 ho->bsd_compress = 1;
669 ho->bsd_bits = nb = BSD_NBITS(p[2]);
670 if (BSD_VERSION(p[2]) != BSD_CURRENT_VERSION
671 || nb > ao->bsd_bits || nb < BSD_MIN_BITS) {
674 p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, ao->bsd_bits);
679 * Check whether we can do BSD-Compress with the code
680 * size they want. If the code size is too big, reduce
681 * it until the kernel can cope and nak with that.
682 * We only check this for the first option.
686 res = ccp_test(f->unit, p, CILEN_BSD_COMPRESS, 1);
689 if (res < 0 || nb == BSD_MIN_BITS || dont_nak) {
691 p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION,
697 p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, nb);
703 if (!ao->predictor_1 || clen != CILEN_PREDICTOR_1) {
710 && ccp_test(f->unit, p, CILEN_PREDICTOR_1, 1) <= 0) {
716 if (!ao->predictor_2 || clen != CILEN_PREDICTOR_2) {
723 && ccp_test(f->unit, p, CILEN_PREDICTOR_2, 1) <= 0) {
733 if (newret == CONFNAK && dont_nak)
735 if (!(newret == CONFACK || newret == CONFNAK && ret == CONFREJ)) {
736 /* we're returning this option */
737 if (newret == CONFREJ && ret == CONFNAK)
741 BCOPY(p, retp, clen);
749 if (ret != CONFACK) {
750 if (ret == CONFREJ && *lenp == retp - p0)
751 all_rejected[f->unit] = 1;
759 * CCP has come up - inform the kernel driver.
765 ccp_options *go = &ccp_gotoptions[f->unit];
766 ccp_options *ho = &ccp_hisoptions[f->unit];
768 ccp_flags_set(f->unit, 1, 1);
769 if (ANY_COMPRESS(*go) || ANY_COMPRESS(*ho))
770 syslog(LOG_NOTICE, "%s enabled",
771 ANY_COMPRESS(*go)? ANY_COMPRESS(*ho)? "Compression":
772 "Receive compression": "Transmit compression");
776 * CCP has gone down - inform the kernel driver.
782 if (ccp_localstate[f->unit] & RACK_PENDING)
783 UNTIMEOUT(ccp_rack_timeout, (caddr_t) f);
784 ccp_localstate[f->unit] = 0;
785 ccp_flags_set(f->unit, 1, 0);
789 * Print the contents of a CCP packet.
791 char *ccp_codenames[] = {
792 "ConfReq", "ConfAck", "ConfNak", "ConfRej",
793 "TermReq", "TermAck", "CodeRej",
794 NULL, NULL, NULL, NULL, NULL, NULL,
795 "ResetReq", "ResetAck",
799 ccp_printpkt(p, plen, printer, arg)
802 void (*printer) __P((void *, char *, ...));
810 if (plen < HEADERLEN)
814 len = (p[2] << 8) + p[3];
815 if (len < HEADERLEN || len > plen)
818 if (code >= 1 && code <= sizeof(ccp_codenames) / sizeof(char *)
819 && ccp_codenames[code-1] != NULL)
820 printer(arg, " %s", ccp_codenames[code-1]);
822 printer(arg, " code=0x%x", code);
823 printer(arg, " id=0x%x", id);
832 /* print list of possible compression methods */
836 if (optlen < 2 || optlen > len)
843 if (optlen >= CILEN_DEFLATE) {
844 printer(arg, "deflate %d", DEFLATE_SIZE(p[2]));
845 if (DEFLATE_METHOD(p[2]) != DEFLATE_METHOD_VAL)
846 printer(arg, " method %d", DEFLATE_METHOD(p[2]));
847 if (p[3] != DEFLATE_CHK_SEQUENCE)
848 printer(arg, " check %d", p[3]);
852 case CI_BSD_COMPRESS:
853 if (optlen >= CILEN_BSD_COMPRESS) {
854 printer(arg, "bsd v%d %d", BSD_VERSION(p[2]),
856 p += CILEN_BSD_COMPRESS;
860 if (optlen >= CILEN_PREDICTOR_1) {
861 printer(arg, "predictor 1");
862 p += CILEN_PREDICTOR_1;
866 if (optlen >= CILEN_PREDICTOR_2) {
867 printer(arg, "predictor 2");
868 p += CILEN_PREDICTOR_2;
873 printer(arg, " %.2x", *p++);
879 /* dump out the rest of the packet in hex */
881 printer(arg, " %.2x", *p++);
887 * We have received a packet that the decompressor failed to
888 * decompress. Here we would expect to issue a reset-request, but
889 * Motorola has a patent on resetting the compressor as a result of
890 * detecting an error in the decompressed data after decompression.
891 * (See US patent 5,130,993; international patent publication number
892 * WO 91/10289; Australian patent 73296/91.)
894 * So we ask the kernel whether the error was detected after
895 * decompression; if it was, we take CCP down, thus disabling
896 * compression :-(, otherwise we issue the reset-request.
899 ccp_datainput(unit, pkt, len)
907 if (f->state == OPENED) {
908 if (ccp_fatal_error(unit)) {
910 * Disable compression by taking CCP down.
912 syslog(LOG_ERR, "Lost compression sync: disabling compression");
913 ccp_close(unit, "Lost compression sync");
916 * Send a reset-request to reset the peer's compressor.
917 * We don't do that if we are still waiting for an
918 * acknowledgement to a previous reset-request.
920 if (!(ccp_localstate[f->unit] & RACK_PENDING)) {
921 fsm_sdata(f, CCP_RESETREQ, f->reqid = ++f->id, NULL, 0);
922 TIMEOUT(ccp_rack_timeout, (caddr_t) f, RACKTIMEOUT);
923 ccp_localstate[f->unit] |= RACK_PENDING;
925 ccp_localstate[f->unit] |= RREQ_REPEAT;
931 * Timeout waiting for reset-ack.
934 ccp_rack_timeout(arg)
937 fsm *f = (fsm *) arg;
939 if (f->state == OPENED && ccp_localstate[f->unit] & RREQ_REPEAT) {
940 fsm_sdata(f, CCP_RESETREQ, f->reqid, NULL, 0);
941 TIMEOUT(ccp_rack_timeout, (caddr_t) f, RACKTIMEOUT);
942 ccp_localstate[f->unit] &= ~RREQ_REPEAT;
944 ccp_localstate[f->unit] &= ~RACK_PENDING;