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.11 1995/08/16 04:15:38 paulus Exp $";
33 #include <sys/ioctl.h>
34 #include <net/ppp-comp.h>
41 ccp_options ccp_wantoptions[NUM_PPP]; /* what to request the peer to use */
42 ccp_options ccp_gotoptions[NUM_PPP]; /* what the peer agreed to do */
43 ccp_options ccp_allowoptions[NUM_PPP]; /* what we'll agree to do */
44 ccp_options ccp_hisoptions[NUM_PPP]; /* what we agreed to do */
47 * Callbacks for fsm code.
49 static void ccp_resetci __P((fsm *));
50 static int ccp_cilen __P((fsm *));
51 static void ccp_addci __P((fsm *, u_char *, int *));
52 static int ccp_ackci __P((fsm *, u_char *, int));
53 static int ccp_nakci __P((fsm *, u_char *, int));
54 static int ccp_rejci __P((fsm *, u_char *, int));
55 static int ccp_reqci __P((fsm *, u_char *, int *, int));
56 static void ccp_up __P((fsm *));
57 static void ccp_down __P((fsm *));
58 static int ccp_extcode __P((fsm *, int, int, u_char *, int));
59 static void ccp_rack_timeout __P(());
61 static fsm_callbacks ccp_callbacks = {
80 * Do we want / did we get any compression?
82 #define ANY_COMPRESS(opt) ((opt).bsd_compress)
85 * Local state (mainly for handling reset-reqs and reset-acks
87 static int ccp_localstate[NUM_PPP];
88 #define RACK_PENDING 1 /* waiting for reset-ack */
89 #define RREQ_REPEAT 2 /* send another reset-req if no reset-ack */
91 #define RACKTIMEOUT 1 /* second */
94 * ccp_init - initialize CCP.
100 fsm *f = &ccp_fsm[unit];
103 f->protocol = PPP_CCP;
104 f->callbacks = &ccp_callbacks;
107 memset(&ccp_wantoptions[unit], 0, sizeof(ccp_options));
108 memset(&ccp_gotoptions[unit], 0, sizeof(ccp_options));
109 memset(&ccp_allowoptions[unit], 0, sizeof(ccp_options));
110 memset(&ccp_hisoptions[unit], 0, sizeof(ccp_options));
112 ccp_wantoptions[0].bsd_compress = 1;
113 ccp_wantoptions[0].bsd_bits = 12; /* default value */
115 ccp_allowoptions[0].bsd_compress = 1;
116 ccp_allowoptions[0].bsd_bits = BSD_MAX_BITS;
120 * ccp_open - CCP is allowed to come up.
126 fsm *f = &ccp_fsm[unit];
128 if (f->state != OPENED)
129 ccp_flags_set(unit, 1, 0);
130 if (!ANY_COMPRESS(ccp_wantoptions[unit]))
131 f->flags |= OPT_SILENT;
136 * ccp_close - Terminate CCP.
142 ccp_flags_set(unit, 0, 0);
143 fsm_close(&ccp_fsm[unit]);
147 * ccp_lowerup - we may now transmit CCP packets.
153 fsm_lowerup(&ccp_fsm[unit]);
157 * ccp_lowerdown - we may not transmit CCP packets.
163 fsm_lowerdown(&ccp_fsm[unit]);
167 * ccp_input - process a received CCP packet.
170 ccp_input(unit, p, len)
175 fsm *f = &ccp_fsm[unit];
179 * Check for a terminate-request so we can print a message.
182 fsm_input(f, p, len);
183 if (oldstate == OPENED && p[0] == TERMREQ && f->state != OPENED)
184 syslog(LOG_NOTICE, "Compression disabled by peer.");
187 * If we get a terminate-ack and we're not asking for compression,
190 if (oldstate == REQSENT && p[0] == TERMACK
191 && !ANY_COMPRESS(ccp_gotoptions[unit]))
196 * Handle a CCP-specific code.
199 ccp_extcode(f, code, id, p, len)
207 if (f->state != OPENED)
209 /* send a reset-ack, which the transmitter will see and
210 reset its compression state. */
211 fsm_sdata(f, CCP_RESETACK, id, NULL, 0);
215 if (ccp_localstate[f->unit] & RACK_PENDING && id == f->reqid) {
216 ccp_localstate[f->unit] &= ~(RACK_PENDING | RREQ_REPEAT);
217 UNTIMEOUT(ccp_rack_timeout, (caddr_t) f);
229 * ccp_protrej - peer doesn't talk CCP.
235 ccp_flags_set(unit, 0, 0);
236 fsm_lowerdown(&ccp_fsm[unit]);
240 * ccp_resetci - initialize at start of negotiation.
246 ccp_options *go = &ccp_gotoptions[f->unit];
249 *go = ccp_wantoptions[f->unit];
250 if (go->bsd_compress) {
251 opt_buf[0] = CI_BSD_COMPRESS;
252 opt_buf[1] = CILEN_BSD_COMPRESS;
253 opt_buf[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits);
254 if (!ccp_test(f->unit, opt_buf, CILEN_BSD_COMPRESS, 0))
255 go->bsd_compress = 0;
260 * ccp_cilen - Return total length of our configuration info.
266 ccp_options *go = &ccp_gotoptions[f->unit];
268 return (go->bsd_compress? CILEN_BSD_COMPRESS: 0);
272 * ccp_addci - put our requests in a packet.
275 ccp_addci(f, p, lenp)
280 ccp_options *go = &ccp_gotoptions[f->unit];
283 if (go->bsd_compress) {
284 p[0] = CI_BSD_COMPRESS;
285 p[1] = CILEN_BSD_COMPRESS;
286 p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits);
287 if (ccp_test(f->unit, p, CILEN_BSD_COMPRESS, 0))
288 p += CILEN_BSD_COMPRESS;
290 go->bsd_compress = 0;
296 * ccp_ackci - process a received configure-ack, and return
297 * 1 iff the packet was OK.
305 ccp_options *go = &ccp_gotoptions[f->unit];
307 if (go->bsd_compress) {
308 if (len < CILEN_BSD_COMPRESS
309 || p[0] != CI_BSD_COMPRESS || p[1] != CILEN_BSD_COMPRESS
310 || p[2] != BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits))
312 p += CILEN_BSD_COMPRESS;
313 len -= CILEN_BSD_COMPRESS;
321 * ccp_nakci - process received configure-nak.
322 * Returns 1 iff the nak was OK.
330 ccp_options *go = &ccp_gotoptions[f->unit];
331 ccp_options no; /* options we've seen already */
332 ccp_options try; /* options to ask for next time */
334 memset(&no, 0, sizeof(no));
337 if (go->bsd_compress && !no.bsd_compress && len >= CILEN_BSD_COMPRESS
338 && p[0] == CI_BSD_COMPRESS && p[1] == CILEN_BSD_COMPRESS) {
341 * Peer wants us to use a different number of bits
342 * or a different version.
344 if (BSD_VERSION(p[2]) != BSD_CURRENT_VERSION)
345 try.bsd_compress = 0;
346 else if (BSD_NBITS(p[2]) < go->bsd_bits)
347 try.bsd_bits = BSD_NBITS(p[2]);
348 p += CILEN_BSD_COMPRESS;
349 len -= CILEN_BSD_COMPRESS;
353 * Have a look at any remaining options...???
359 if (f->state != OPENED)
365 * ccp_rejci - reject some of our suggested compression methods.
373 ccp_options *go = &ccp_gotoptions[f->unit];
374 ccp_options try; /* options to request next time */
378 if (go->bsd_compress && len >= CILEN_BSD_COMPRESS
379 && p[0] == CI_BSD_COMPRESS && p[1] == CILEN_BSD_COMPRESS) {
380 if (p[2] != BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits))
382 try.bsd_compress = 0;
383 p += CILEN_BSD_COMPRESS;
384 len -= CILEN_BSD_COMPRESS;
390 if (f->state != OPENED)
397 * ccp_reqci - processed a received configure-request.
398 * Returns CONFACK, CONFNAK or CONFREJ and the packet modified
402 ccp_reqci(f, p, lenp, dont_nak)
410 int len, clen, type, nb;
411 ccp_options *ho = &ccp_hisoptions[f->unit];
412 ccp_options *ao = &ccp_allowoptions[f->unit];
418 memset(ho, 0, sizeof(ccp_options));
422 if (len < 2 || p[1] < 2 || p[1] > len) {
432 case CI_BSD_COMPRESS:
433 if (!ao->bsd_compress || clen != CILEN_BSD_COMPRESS) {
438 ho->bsd_compress = 1;
439 ho->bsd_bits = nb = BSD_NBITS(p[2]);
440 if (BSD_VERSION(p[2]) != BSD_CURRENT_VERSION
441 || nb > ao->bsd_bits) {
444 } else if (nb < BSD_MIN_BITS) {
446 } else if (!ccp_test(f->unit, p, CILEN_BSD_COMPRESS, 1)) {
447 if (nb > BSD_MIN_BITS) {
453 if (newret == CONFNAK && !dont_nak) {
454 p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, nb);
464 if (newret == CONFNAK && dont_nak)
466 if (!(newret == CONFACK || newret == CONFNAK && ret == CONFREJ)) {
467 /* we're returning this option */
468 if (newret == CONFREJ && ret == CONFNAK)
472 BCOPY(p, retp, clen);
486 * CCP has come up - inform the kernel driver.
492 ccp_options *go = &ccp_gotoptions[f->unit];
493 ccp_options *ho = &ccp_hisoptions[f->unit];
495 ccp_flags_set(f->unit, 1, 1);
496 if (go->bsd_compress || ho->bsd_compress)
497 syslog(LOG_NOTICE, "%s enabled",
498 go->bsd_compress? ho->bsd_compress? "Compression":
499 "Receive compression": "Transmit compression");
503 * CCP has gone down - inform the kernel driver.
509 if (ccp_localstate[f->unit] & RACK_PENDING)
510 UNTIMEOUT(ccp_rack_timeout, (caddr_t) f);
511 ccp_localstate[f->unit] = 0;
512 ccp_flags_set(f->unit, 1, 0);
516 * Print the contents of a CCP packet.
518 char *ccp_codenames[] = {
519 "ConfReq", "ConfAck", "ConfNak", "ConfRej",
520 "TermReq", "TermAck", "CodeRej",
521 NULL, NULL, NULL, NULL, NULL, NULL,
522 "ResetReq", "ResetAck",
526 ccp_printpkt(p, plen, printer, arg)
529 void (*printer) __P((void *, char *, ...));
537 if (plen < HEADERLEN)
541 len = (p[2] << 8) + p[3];
542 if (len < HEADERLEN || len > plen)
545 if (code >= 1 && code <= sizeof(ccp_codenames) / sizeof(char *)
546 && ccp_codenames[code-1] != NULL)
547 printer(arg, " %s", ccp_codenames[code-1]);
549 printer(arg, " code=0x%x", code);
550 printer(arg, " id=0x%x", id);
559 /* print list of possible compression methods */
563 if (optlen < 2 || optlen > len)
569 case CI_BSD_COMPRESS:
570 if (optlen >= CILEN_BSD_COMPRESS) {
571 printer(arg, "bsd v%d %d", BSD_VERSION(p[2]),
573 p += CILEN_BSD_COMPRESS;
578 printer(arg, " %.2x", *p++);
584 /* dump out the rest of the packet in hex */
586 printer(arg, " %.2x", *p++);
592 * We have received a packet that the decompressor failed to
593 * decompress. Here we would expect to issue a reset-request, but
594 * Motorola has a patent on resetting the compressor as a result of
595 * detecting an error in the decompressed data after decompression.
596 * (See US patent 5,130,993; international patent publication number
597 * WO 91/10289; Australian patent 73296/91.)
599 * So we ask the kernel whether the error was detected after
600 * decompression; if it was, we take CCP down, thus disabling
601 * compression :-(, otherwise we issue the reset-request.
604 ccp_datainput(unit, pkt, len)
612 if (f->state == OPENED) {
613 if (ccp_fatal_error(unit)) {
615 * Disable compression by taking CCP down.
617 syslog(LOG_ERR, "Lost compression sync: disabling compression");
621 * Send a reset-request to reset the peer's compressor.
622 * We don't do that if we are still waiting for an
623 * acknowledgement to a previous reset-request.
625 if (!(ccp_localstate[f->unit] & RACK_PENDING)) {
626 fsm_sdata(f, CCP_RESETREQ, f->reqid = ++f->id, NULL, 0);
627 TIMEOUT(ccp_rack_timeout, (caddr_t) f, RACKTIMEOUT);
628 ccp_localstate[f->unit] |= RACK_PENDING;
630 ccp_localstate[f->unit] |= RREQ_REPEAT;
636 * Timeout waiting for reset-ack.
639 ccp_rack_timeout(arg)
642 fsm *f = (fsm *) arg;
644 if (f->state == OPENED && ccp_localstate[f->unit] & RREQ_REPEAT) {
645 fsm_sdata(f, CCP_RESETREQ, f->reqid, NULL, 0);
646 TIMEOUT(ccp_rack_timeout, (caddr_t) f, RACKTIMEOUT);
647 ccp_localstate[f->unit] &= ~RREQ_REPEAT;
649 ccp_localstate[f->unit] &= ~RACK_PENDING;