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.6 1994/10/24 04:31:11 paulus Exp $";
33 #include <sys/ioctl.h>
40 ccp_options ccp_wantoptions[NUM_PPP]; /* what to request the peer to use */
41 ccp_options ccp_gotoptions[NUM_PPP]; /* what the peer agreed to do */
42 ccp_options ccp_allowoptions[NUM_PPP]; /* what we'll agree to do */
43 ccp_options ccp_hisoptions[NUM_PPP]; /* what we agreed to do */
46 * Callbacks for fsm code.
48 static void ccp_resetci __P((fsm *));
49 static int ccp_cilen __P((fsm *));
50 static void ccp_addci __P((fsm *, u_char *, int *));
51 static int ccp_ackci __P((fsm *, u_char *, int));
52 static int ccp_nakci __P((fsm *, u_char *, int));
53 static int ccp_rejci __P((fsm *, u_char *, int));
54 static int ccp_reqci __P((fsm *, u_char *, int *, int));
55 static void ccp_up __P((fsm *));
56 static void ccp_down __P((fsm *));
57 static int ccp_extcode __P((fsm *, int, int, u_char *, int));
58 static void ccp_rack_timeout __P(());
60 static fsm_callbacks ccp_callbacks = {
79 * Length of configuration options, which describe possible
80 * compression methods.
85 * Configuration option values for compression methods.
87 #define CI_BSD_COMPRESS 0x21
90 * Information relating to BSD Compress configuration options.
92 #define BSD_NBITS(x) ((x) & 0x1F)
93 #define BSD_VERSION(x) ((x) >> 5)
94 #define BSD_CURRENT_VERSION 1
95 #define BSD_MAKE_OPT(v, n) (((v) << 5) | (n))
98 * Do we want / did we get any compression?
100 #define ANY_COMPRESS(opt) ((opt).bsd_compress)
103 * Local state (mainly for handling reset-reqs and reset-acks
105 static int ccp_localstate[NUM_PPP];
106 #define RACK_PENDING 1 /* waiting for reset-ack */
107 #define RREQ_REPEAT 2 /* send another reset-req if no reset-ack */
109 #define RACKTIMEOUT 1 /* second */
112 * ccp_init - initialize CCP.
118 fsm *f = &ccp_fsm[unit];
121 f->protocol = PPP_CCP;
122 f->callbacks = &ccp_callbacks;
125 memset(&ccp_wantoptions[unit], 0, sizeof(ccp_options));
126 memset(&ccp_gotoptions[unit], 0, sizeof(ccp_options));
127 memset(&ccp_allowoptions[unit], 0, sizeof(ccp_options));
128 memset(&ccp_hisoptions[unit], 0, sizeof(ccp_options));
130 ccp_wantoptions[0].bsd_bits = 12; /* default value */
132 ccp_allowoptions[0].bsd_compress = 1;
133 ccp_allowoptions[0].bsd_bits = MAX_BSD_BITS;
137 * ccp_open - CCP is allowed to come up.
143 fsm *f = &ccp_fsm[unit];
145 if (f->state != OPENED)
146 ccp_flags_set(unit, 1, 0);
147 if (!ANY_COMPRESS(ccp_wantoptions[unit]))
148 f->flags |= OPT_SILENT;
153 * ccp_close - Terminate CCP.
159 ccp_flags_set(unit, 0, 0);
160 fsm_close(&ccp_fsm[unit]);
164 * ccp_lowerup - we may now transmit CCP packets.
170 fsm_lowerup(&ccp_fsm[unit]);
174 * ccp_lowerdown - we may not transmit CCP packets.
180 fsm_lowerdown(&ccp_fsm[unit]);
184 * ccp_input - process a received CCP packet.
187 ccp_input(unit, p, len)
192 fsm *f = &ccp_fsm[unit];
196 * Check for a terminate-request so we can print a message.
199 fsm_input(f, p, len);
200 if (oldstate == OPENED && p[0] == TERMREQ && f->state != OPENED)
201 syslog(LOG_NOTICE, "Compression disabled by peer.");
205 * Handle a CCP-specific code.
208 ccp_extcode(f, code, id, p, len)
216 if (f->state != OPENED)
218 /* send a reset-ack, which the transmitter will see and
219 reset its compression state. */
220 fsm_sdata(f, RESETACK, id, NULL, 0);
224 if (ccp_localstate[f->unit] & RACK_PENDING && id == f->reqid) {
225 ccp_localstate[f->unit] &= ~(RACK_PENDING | RREQ_REPEAT);
226 UNTIMEOUT(ccp_rack_timeout, (caddr_t) f);
238 * ccp_protrej - peer doesn't talk CCP.
244 ccp_flags_set(unit, 0, 0);
245 fsm_lowerdown(&ccp_fsm[unit]);
249 * ccp_resetci - initialize at start of negotiation.
255 ccp_options *go = &ccp_gotoptions[f->unit];
258 *go = ccp_wantoptions[f->unit];
259 if (go->bsd_compress) {
260 opt_buf[0] = CI_BSD_COMPRESS;
261 opt_buf[1] = CILEN_BSD;
262 opt_buf[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits);
263 if (!ccp_test(f->unit, opt_buf, CILEN_BSD, 0))
264 go->bsd_compress = 0;
269 * ccp_cilen - Return total length of our configuration info.
275 ccp_options *go = &ccp_gotoptions[f->unit];
277 return (go->bsd_compress? CILEN_BSD: 0);
281 * ccp_addci - put our requests in a packet.
284 ccp_addci(f, p, lenp)
289 ccp_options *go = &ccp_gotoptions[f->unit];
292 if (go->bsd_compress) {
293 p[0] = CI_BSD_COMPRESS;
295 p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits);
296 if (ccp_test(f->unit, p, CILEN_BSD, 0))
299 go->bsd_compress = 0;
305 * ccp_ackci - process a received configure-ack, and return
306 * 1 iff the packet was OK.
314 ccp_options *go = &ccp_gotoptions[f->unit];
316 if (go->bsd_compress) {
317 if (len < CILEN_BSD || p[0] != CI_BSD_COMPRESS || p[1] != CILEN_BSD
318 || p[2] != BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits))
329 * ccp_nakci - process received configure-nak.
330 * Returns 1 iff the nak was OK.
338 ccp_options *go = &ccp_gotoptions[f->unit];
339 ccp_options no; /* options we've seen already */
340 ccp_options try; /* options to ask for next time */
342 memset(&no, 0, sizeof(no));
345 if (go->bsd_compress && !no.bsd_compress && len >= CILEN_BSD
346 && p[0] == CI_BSD_COMPRESS && p[1] == CILEN_BSD) {
349 * Peer wants us to use a different number of bits
350 * or a different version.
352 if (BSD_VERSION(p[2]) != BSD_CURRENT_VERSION)
353 try.bsd_compress = 0;
354 else if (BSD_NBITS(p[2]) < go->bsd_bits)
355 try.bsd_bits = BSD_NBITS(p[2]);
361 * Have a look at any remaining options...???
367 if (f->state != OPENED)
373 * ccp_rejci - reject some of our suggested compression methods.
381 ccp_options *go = &ccp_gotoptions[f->unit];
382 ccp_options try; /* options to request next time */
386 if (go->bsd_compress && len >= CILEN_BSD
387 && p[0] == CI_BSD_COMPRESS && p[1] == CILEN_BSD) {
388 if (p[2] != BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits))
390 try.bsd_compress = 0;
398 if (f->state != OPENED)
405 * ccp_reqci - processed a received configure-request.
406 * Returns CONFACK, CONFNAK or CONFREJ and the packet modified
410 ccp_reqci(f, p, lenp, dont_nak)
418 int len, clen, type, nb;
419 ccp_options *ho = &ccp_hisoptions[f->unit];
420 ccp_options *ao = &ccp_allowoptions[f->unit];
426 memset(ho, 0, sizeof(ccp_options));
430 if (len < 2 || p[1] < 2 || p[1] > len) {
440 case CI_BSD_COMPRESS:
441 if (!ao->bsd_compress || clen != CILEN_BSD) {
446 ho->bsd_compress = 1;
447 ho->bsd_bits = nb = BSD_NBITS(p[2]);
448 if (BSD_VERSION(p[2]) != BSD_CURRENT_VERSION
449 || nb > ao->bsd_bits) {
452 } else if (nb < MIN_BSD_BITS) {
454 } else if (!ccp_test(f->unit, p, CILEN_BSD, 1)) {
455 if (nb > MIN_BSD_BITS) {
461 if (newret == CONFNAK && !dont_nak) {
462 p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, nb);
472 if (!(newret == CONFACK || newret == CONFNAK && ret == CONFREJ)) {
473 /* we're returning this option */
476 BCOPY(p, retp, clen);
490 * CCP has come up - inform the kernel driver.
496 ccp_options *go = &ccp_gotoptions[f->unit];
497 ccp_options *ho = &ccp_hisoptions[f->unit];
499 ccp_flags_set(f->unit, 1, 1);
500 if (go->bsd_compress || ho->bsd_compress)
501 syslog(LOG_NOTICE, "%s enabled",
502 go->bsd_compress? ho->bsd_compress? "Compression":
503 "Receive compression": "Transmit compression");
507 * CCP has gone down - inform the kernel driver.
513 if (ccp_localstate[f->unit] & RACK_PENDING)
514 UNTIMEOUT(ccp_rack_timeout, (caddr_t) f);
515 ccp_localstate[f->unit] = 0;
516 ccp_flags_set(f->unit, 1, 0);
520 * Print the contents of a CCP packet.
522 char *ccp_codenames[] = {
523 "ConfReq", "ConfAck", "ConfNak", "ConfRej",
524 "TermReq", "TermAck", "CodeRej",
525 NULL, NULL, NULL, NULL, NULL, NULL,
526 "ResetReq", "ResetAck",
530 ccp_printpkt(p, plen, printer, arg)
533 void (*printer) __P((void *, char *, ...));
541 if (plen < HEADERLEN)
545 len = (p[2] << 8) + p[3];
546 if (len < HEADERLEN || len > plen)
549 if (code >= 1 && code <= sizeof(ccp_codenames) / sizeof(char *)
550 && ccp_codenames[code-1] != NULL)
551 printer(arg, " %s", ccp_codenames[code-1]);
553 printer(arg, " code=0x%x", code);
554 printer(arg, " id=0x%x", id);
563 /* print list of possible compression methods */
567 if (optlen < 2 || optlen > len)
573 case CI_BSD_COMPRESS:
574 if (optlen >= CILEN_BSD) {
575 printer(arg, "bsd v%d %d", BSD_VERSION(p[2]),
582 printer(arg, " %.2x", *p++);
588 /* dump out the rest of the packet in hex */
590 printer(arg, " %.2x", *p++);
596 * We have received a packet that the decompressor failed to
597 * decompress. Here we would expect to issue a reset-request, but
598 * Motorola has a patent on resetting the compressor as a result of
599 * detecting an error in the decompressed data after decompression.
600 * (See US patent 5,130,993; international patent publication number
601 * WO 91/10289; Australian patent 73296/91.)
603 * So we ask the kernel whether the error was detected after
604 * decompression; if it was, we take CCP down, thus disabling
605 * compression :-(, otherwise we issue the reset-request.
608 ccp_datainput(unit, pkt, len)
616 if (f->state == OPENED) {
617 if (ccp_fatal_error(unit)) {
619 * Disable compression by taking CCP down.
621 syslog(LOG_ERR, "Lost compression sync: disabling compression");
625 * Send a reset-request to reset the peer's compressor.
626 * We don't do that if we are still waiting for an
627 * acknowledgement to a previous reset-request.
629 if (!(ccp_localstate[f->unit] & RACK_PENDING)) {
630 fsm_sdata(f, RESETREQ, f->reqid = ++f->id, NULL, 0);
631 TIMEOUT(ccp_rack_timeout, (caddr_t) f, RACKTIMEOUT);
632 ccp_localstate[f->unit] |= RACK_PENDING;
634 ccp_localstate[f->unit] |= RREQ_REPEAT;
640 * Timeout waiting for reset-ack.
643 ccp_rack_timeout(arg)
646 fsm *f = (fsm *) arg;
648 if (f->state == OPENED && ccp_localstate[f->unit] & RREQ_REPEAT) {
649 fsm_sdata(f, RESETREQ, f->reqid, NULL, 0);
650 TIMEOUT(ccp_rack_timeout, (caddr_t) f, RACKTIMEOUT);
651 ccp_localstate[f->unit] &= ~RREQ_REPEAT;
653 ccp_localstate[f->unit] &= ~RACK_PENDING;