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,
27 * $Id: ccp.c,v 1.3 1994/09/01 00:16:20 paulus Exp $
31 static char rcsid[] = "$Id: ccp.c,v 1.3 1994/09/01 00:16:20 paulus Exp $";
35 #include <sys/ioctl.h>
43 ccp_options ccp_wantoptions[NPPP]; /* what to request the peer to use */
44 ccp_options ccp_gotoptions[NPPP]; /* what the peer agreed to do */
45 ccp_options ccp_allowoptions[NPPP]; /* what we'll agree to do */
46 ccp_options ccp_hisoptions[NPPP]; /* what we agreed to do */
49 * Callbacks for fsm code.
51 static void ccp_resetci __ARGS((fsm *));
52 static int ccp_cilen __ARGS((fsm *));
53 static void ccp_addci __ARGS((fsm *, u_char *, int *));
54 static int ccp_ackci __ARGS((fsm *, u_char *, int));
55 static int ccp_nakci __ARGS((fsm *, u_char *, int));
56 static int ccp_rejci __ARGS((fsm *, u_char *, int));
57 static int ccp_reqci __ARGS((fsm *, u_char *, int *, int));
58 static void ccp_up __ARGS((fsm *));
59 static void ccp_down __ARGS((fsm *));
60 static int ccp_extcode __ARGS((fsm *, int, int, u_char *, int));
61 static void ccp_rack_timeout __ARGS(());
63 static fsm_callbacks ccp_callbacks = {
82 * Length of configuration options, which describe possible
83 * compression methods.
88 * Configuration option values for compression methods.
90 #define CI_BSD_COMPRESS 0x21
93 * Local state (mainly for handling reset-reqs and reset-acks
95 static int ccp_localstate[NPPP];
96 #define RACK_PENDING 1 /* waiting for reset-ack */
97 #define RREQ_REPEAT 2 /* send another reset-req if no reset-ack */
99 #define RACKTIMEOUT 1 /* second */
102 * ccp_init - initialize CCP.
108 fsm *f = &ccp_fsm[unit];
112 f->callbacks = &ccp_callbacks;
115 memset(&ccp_wantoptions[unit], 0, sizeof(ccp_options));
116 memset(&ccp_gotoptions[unit], 0, sizeof(ccp_options));
117 memset(&ccp_allowoptions[unit], 0, sizeof(ccp_options));
118 memset(&ccp_hisoptions[unit], 0, sizeof(ccp_options));
120 ccp_wantoptions[0].bsd_bits = 12; /* default value */
122 ccp_allowoptions[0].bsd_compress = 1;
123 ccp_allowoptions[0].bsd_bits = MAX_BSD_BITS;
127 * ccp_open - CCP is allowed to come up.
133 fsm *f = &ccp_fsm[unit];
135 if (f->state != OPENED)
136 ccp_flags_set(unit, 1, 0);
137 if (!ccp_wantoptions[unit].bsd_compress)
138 f->flags |= OPT_SILENT;
143 * ccp_close - Terminate CCP.
149 ccp_flags_set(unit, 0, 0);
150 fsm_close(&ccp_fsm[unit]);
154 * ccp_lowerup - we may now transmit CCP packets.
160 fsm_lowerup(&ccp_fsm[unit]);
164 * ccp_lowerdown - we may not transmit CCP packets.
170 fsm_lowerdown(&ccp_fsm[unit]);
174 * ccp_input - process a received CCP packet.
177 ccp_input(unit, p, len)
182 fsm_input(&ccp_fsm[unit], p, len);
186 * Handle a CCP-specific code.
189 ccp_extcode(f, code, id, p, len)
197 if (f->state != OPENED)
199 /* send a reset-ack, which the transmitter will see and
200 reset its compression state. */
201 fsm_sdata(f, RESETACK, id, NULL, 0);
205 if (ccp_localstate[f->unit] & RACK_PENDING && id == f->reqid) {
206 ccp_localstate[f->unit] &= ~(RACK_PENDING | RREQ_REPEAT);
207 UNTIMEOUT(ccp_rack_timeout, (caddr_t) f);
219 * ccp_protrej - peer doesn't talk CCP.
225 ccp_flags_set(unit, 0, 0);
226 fsm_lowerdown(&ccp_fsm[unit]);
230 * ccp_resetci - initialize at start of negotiation.
236 ccp_options *go = &ccp_gotoptions[f->unit];
239 *go = ccp_wantoptions[f->unit];
240 if (go->bsd_compress) {
241 opt_buf[0] = CI_BSD_COMPRESS;
242 opt_buf[1] = CILEN_BSD;
243 opt_buf[2] = go->bsd_bits;
244 if (!ccp_test(f->unit, opt_buf, 3, 0))
245 go->bsd_compress = 0;
250 * ccp_cilen - Return total length of our configuration info.
256 ccp_options *go = &ccp_gotoptions[f->unit];
258 return (go->bsd_compress? CILEN_BSD: 0);
262 * ccp_addci - put our requests in a packet.
265 ccp_addci(f, p, lenp)
270 ccp_options *go = &ccp_gotoptions[f->unit];
273 if (go->bsd_compress) {
274 p[0] = CI_BSD_COMPRESS;
283 * ccp_ackci - process a received configure-ack, and return
284 * 1 iff the packet was OK.
292 ccp_options *go = &ccp_gotoptions[f->unit];
294 if (go->bsd_compress) {
295 if (len != 3 || p[0] != CI_BSD_COMPRESS
296 || p[1] != CILEN_BSD || p[2] != go->bsd_bits)
307 * ccp_nakci - process received configure-nak.
308 * Returns 1 iff the nak was OK.
316 ccp_options *go = &ccp_gotoptions[f->unit];
317 ccp_options no; /* options we've seen already */
318 ccp_options try; /* options to ask for next time */
320 memset(&no, 0, sizeof(no));
323 if (go->bsd_compress && len >= CILEN_BSD && p[0] == CI_BSD_COMPRESS
324 && p[1] == CILEN_BSD) {
327 * Peer wants us to use a different number of bits.
329 if (p[2] < go->bsd_bits)
336 * Have a look at any remaining options...???
342 if (f->state != OPENED)
348 * ccp_rejci - reject some of our suggested compression methods.
356 ccp_options *go = &ccp_gotoptions[f->unit];
357 ccp_options try; /* options to request next time */
361 if (go->bsd_compress && len >= CILEN_BSD && p[0] == CI_BSD_COMPRESS
362 && p[1] == CILEN_BSD) {
363 if (p[2] != go->bsd_bits)
365 try.bsd_compress = 0;
373 if (f->state != OPENED)
380 * ccp_reqci - processed a received configure-request.
381 * Returns CONFACK, CONFNAK or CONFREJ and the packet modified
385 ccp_reqci(f, p, lenp, dont_nak)
394 ccp_options *ho = &ccp_hisoptions[f->unit];
395 ccp_options *ao = &ccp_allowoptions[f->unit];
401 memset(ho, 0, sizeof(ccp_options));
405 if (len < 2 || p[1] < 2 || p[1] > len) {
415 case CI_BSD_COMPRESS:
416 if (!ao->bsd_compress || clen != CILEN_BSD) {
421 ho->bsd_compress = 1;
423 if (ho->bsd_bits < MIN_BSD_BITS
424 || ho->bsd_bits > ao->bsd_bits) {
426 } else if (!ccp_test(f->unit, p, CILEN_BSD, 1)) {
427 if (ho->bsd_bits > MIN_BSD_BITS)
432 if (newret == CONFNAK && !dont_nak) {
433 p[2] = (ho->bsd_bits < ao->bsd_bits? MIN_BSD_BITS:
444 if (!(newret == CONFACK || newret == CONFNAK && ret == CONFREJ)) {
445 /* we're returning this option */
448 BCOPY(p, retp, clen);
462 * CCP has come up - inform the kernel driver.
464 static char *up_strings[] = {
465 "no ", "receive ", "transmit ", ""
472 ccp_options *go = &ccp_gotoptions[f->unit];
473 ccp_options *ho = &ccp_hisoptions[f->unit];
475 ccp_flags_set(f->unit, 1, 1);
476 syslog(LOG_INFO, "%scompression enabled",
477 up_strings[(go->bsd_compress? 1: 0) + (ho->bsd_compress? 2: 0)]);
481 * CCP has gone down - inform the kernel driver.
487 if (ccp_localstate[f->unit] & RACK_PENDING)
488 UNTIMEOUT(ccp_rack_timeout, (caddr_t) f);
489 ccp_localstate[f->unit] = 0;
490 ccp_flags_set(f->unit, 1, 0);
491 syslog(LOG_NOTICE, "Compression disabled.");
495 * Print the contents of a CCP packet.
497 char *ccp_codenames[] = {
498 "ConfReq", "ConfAck", "ConfNak", "ConfRej",
499 "TermReq", "TermAck", "CodeRej",
500 NULL, NULL, NULL, NULL, NULL, NULL,
501 "ResetReq", "ResetAck",
505 ccp_printpkt(p, plen, printer, arg)
508 void (*printer) __ARGS((void *, char *, ...));
516 if (plen < HEADERLEN)
520 len = (p[2] << 8) + p[3];
521 if (len < HEADERLEN || len > plen)
524 if (code >= 1 && code <= sizeof(ccp_codenames) / sizeof(char *)
525 && ccp_codenames[code-1] != NULL)
526 printer(arg, " %s", ccp_codenames[code-1]);
528 printer(arg, " code=0x%x", code);
529 printer(arg, " id=0x%x", id);
538 /* print list of possible compression methods */
542 if (optlen < 2 || optlen > len)
548 case CI_BSD_COMPRESS:
549 if (optlen >= CILEN_BSD) {
550 printer(arg, "bsd %d", p[2]);
556 printer(arg, " %.2x", *p++);
562 /* dump out the rest of the packet in hex */
564 printer(arg, " %.2x", *p++);
570 * We have received a packet that the decompressor failed to
571 * decompress. Here we would expect to issue a reset-request,
572 * but Motorola has a patent on that, so instead we log a message
573 * and take CCP down :-(. (See US patent 5,130,993; international
574 * patent publication number WO 91/10289; Australian patent 73296/91.)
577 ccp_datainput(unit, pkt, len)
585 if (f->state == OPENED) {
586 syslog(LOG_ERR, "Lost compression sync: disabling compression");
592 * ccp_req_reset - Request that the peer reset the compression
599 if (f->state == OPENED) {
600 if (!(ccp_localstate[f->unit] & RACK_PENDING)) {
601 fsm_sdata(f, RESETREQ, f->reqid = ++f->id, NULL, 0);
602 TIMEOUT(ccp_rack_timeout, (caddr_t) f, RACKTIMEOUT);
603 ccp_localstate[f->unit] |= RACK_PENDING;
605 ccp_localstate[f->unit] |= RREQ_REPEAT;
610 * Timeout waiting for reset-ack.
613 ccp_rack_timeout(arg)
616 fsm *f = (fsm *) arg;
618 if (f->state == OPENED && ccp_localstate[f->unit] & RREQ_REPEAT) {
619 fsm_sdata(f, RESETREQ, f->reqid, NULL, 0);
620 TIMEOUT(ccp_rack_timeout, (caddr_t) f, RACKTIMEOUT);
621 ccp_localstate[f->unit] &= ~RREQ_REPEAT;
623 ccp_localstate[f->unit] &= ~RACK_PENDING;