]> git.ozlabs.org Git - ppp.git/blob - pppd/ccp.c
Mods from Al Longyear, including improvements to option file word parsing,
[ppp.git] / pppd / ccp.c
1 /*
2  * ccp.c - PPP Compression Control Protocol.
3  *
4  * Copyright (c) 1994 The Australian National University.
5  * All rights reserved.
6  *
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
12  * any purpose.
13  *
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
18  * OF SUCH DAMAGE.
19  *
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,
25  * OR MODIFICATIONS.
26  */
27
28 #ifndef lint
29 static char rcsid[] = "$Id: ccp.c,v 1.10 1995/06/06 01:52:23 paulus Exp $";
30 #endif
31
32 #include <syslog.h>
33 #include <sys/ioctl.h>
34 #include <net/ppp-comp.h>
35
36 #include "pppd.h"
37 #include "fsm.h"
38 #include "ccp.h"
39
40 fsm ccp_fsm[NUM_PPP];
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 */
45
46 /*
47  * Callbacks for fsm code.
48  */
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(());
60
61 static fsm_callbacks ccp_callbacks = {
62     ccp_resetci,
63     ccp_cilen,
64     ccp_addci,
65     ccp_ackci,
66     ccp_nakci,
67     ccp_rejci,
68     ccp_reqci,
69     ccp_up,
70     ccp_down,
71     NULL,
72     NULL,
73     NULL,
74     NULL,
75     ccp_extcode,
76     "CCP"
77 };
78
79 /*
80  * Do we want / did we get any compression?
81  */
82 #define ANY_COMPRESS(opt)       ((opt).bsd_compress)
83
84 /*
85  * Local state (mainly for handling reset-reqs and reset-acks
86  */
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 */
90
91 #define RACKTIMEOUT     1       /* second */
92
93 /*
94  * ccp_init - initialize CCP.
95  */
96 void
97 ccp_init(unit)
98     int unit;
99 {
100     fsm *f = &ccp_fsm[unit];
101
102     f->unit = unit;
103     f->protocol = PPP_CCP;
104     f->callbacks = &ccp_callbacks;
105     fsm_init(f);
106
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));
111
112     ccp_wantoptions[0].bsd_compress = 1;
113     ccp_wantoptions[0].bsd_bits = 12;   /* default value */
114
115     ccp_allowoptions[0].bsd_compress = 1;
116     ccp_allowoptions[0].bsd_bits = BSD_MAX_BITS;
117 }
118
119 /*
120  * ccp_open - CCP is allowed to come up.
121  */
122 void
123 ccp_open(unit)
124     int unit;
125 {
126     fsm *f = &ccp_fsm[unit];
127
128     if (f->state != OPENED)
129         ccp_flags_set(unit, 1, 0);
130     if (!ANY_COMPRESS(ccp_wantoptions[unit]))
131         f->flags |= OPT_SILENT;
132     fsm_open(f);
133 }
134
135 /*
136  * ccp_close - Terminate CCP.
137  */
138 void
139 ccp_close(unit)
140     int unit;
141 {
142     ccp_flags_set(unit, 0, 0);
143     fsm_close(&ccp_fsm[unit]);
144 }
145
146 /*
147  * ccp_lowerup - we may now transmit CCP packets.
148  */
149 void
150 ccp_lowerup(unit)
151     int unit;
152 {
153     fsm_lowerup(&ccp_fsm[unit]);
154 }
155
156 /*
157  * ccp_lowerdown - we may not transmit CCP packets.
158  */
159 void
160 ccp_lowerdown(unit)
161     int unit;
162 {
163     fsm_lowerdown(&ccp_fsm[unit]);
164 }
165
166 /*
167  * ccp_input - process a received CCP packet.
168  */
169 void
170 ccp_input(unit, p, len)
171     int unit;
172     u_char *p;
173     int len;
174 {
175     fsm *f = &ccp_fsm[unit];
176     int oldstate;
177
178     /*
179      * Check for a terminate-request so we can print a message.
180      */
181     oldstate = f->state;
182     fsm_input(f, p, len);
183     if (oldstate == OPENED && p[0] == TERMREQ && f->state != OPENED)
184         syslog(LOG_NOTICE, "Compression disabled by peer.");
185 }
186
187 /*
188  * Handle a CCP-specific code.
189  */
190 static int
191 ccp_extcode(f, code, id, p, len)
192     fsm *f;
193     int code, id;
194     u_char *p;
195     int len;
196 {
197     switch (code) {
198     case CCP_RESETREQ:
199         if (f->state != OPENED)
200             break;
201         /* send a reset-ack, which the transmitter will see and
202            reset its compression state. */
203         fsm_sdata(f, CCP_RESETACK, id, NULL, 0);
204         break;
205
206     case CCP_RESETACK:
207         if (ccp_localstate[f->unit] & RACK_PENDING && id == f->reqid) {
208             ccp_localstate[f->unit] &= ~(RACK_PENDING | RREQ_REPEAT);
209             UNTIMEOUT(ccp_rack_timeout, (caddr_t) f);
210         }
211         break;
212
213     default:
214         return 0;
215     }
216
217     return 1;
218 }
219
220 /*
221  * ccp_protrej - peer doesn't talk CCP.
222  */
223 void
224 ccp_protrej(unit)
225     int unit;
226 {
227     ccp_flags_set(unit, 0, 0);
228     fsm_lowerdown(&ccp_fsm[unit]);
229 }
230
231 /*
232  * ccp_resetci - initialize at start of negotiation.
233  */
234 static void
235 ccp_resetci(f)
236     fsm *f;
237 {
238     ccp_options *go = &ccp_gotoptions[f->unit];
239     u_char opt_buf[16];
240
241     *go = ccp_wantoptions[f->unit];
242     if (go->bsd_compress) {
243         opt_buf[0] = CI_BSD_COMPRESS;
244         opt_buf[1] = CILEN_BSD_COMPRESS;
245         opt_buf[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits);
246         if (!ccp_test(f->unit, opt_buf, CILEN_BSD_COMPRESS, 0))
247             go->bsd_compress = 0;
248     }
249 }
250
251 /*
252  * ccp_cilen - Return total length of our configuration info.
253  */
254 static int
255 ccp_cilen(f)
256     fsm *f;
257 {
258     ccp_options *go = &ccp_gotoptions[f->unit];
259
260     return (go->bsd_compress? CILEN_BSD_COMPRESS: 0);
261 }
262
263 /*
264  * ccp_addci - put our requests in a packet.
265  */
266 static void
267 ccp_addci(f, p, lenp)
268     fsm *f;
269     u_char *p;
270     int *lenp;
271 {
272     ccp_options *go = &ccp_gotoptions[f->unit];
273     u_char *p0 = p;
274
275     if (go->bsd_compress) {
276         p[0] = CI_BSD_COMPRESS;
277         p[1] = CILEN_BSD_COMPRESS;
278         p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits);
279         if (ccp_test(f->unit, p, CILEN_BSD_COMPRESS, 0))
280             p += CILEN_BSD_COMPRESS;
281         else
282             go->bsd_compress = 0;
283     }
284     *lenp = p - p0;
285 }
286
287 /*
288  * ccp_ackci - process a received configure-ack, and return
289  * 1 iff the packet was OK.
290  */
291 static int
292 ccp_ackci(f, p, len)
293     fsm *f;
294     u_char *p;
295     int len;
296 {
297     ccp_options *go = &ccp_gotoptions[f->unit];
298
299     if (go->bsd_compress) {
300         if (len < CILEN_BSD_COMPRESS
301             || p[0] != CI_BSD_COMPRESS || p[1] != CILEN_BSD_COMPRESS
302             || p[2] != BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits))
303             return 0;
304         p += CILEN_BSD_COMPRESS;
305         len -= CILEN_BSD_COMPRESS;
306     }
307     if (len != 0)
308         return 0;
309     return 1;
310 }
311
312 /*
313  * ccp_nakci - process received configure-nak.
314  * Returns 1 iff the nak was OK.
315  */
316 static int
317 ccp_nakci(f, p, len)
318     fsm *f;
319     u_char *p;
320     int len;
321 {
322     ccp_options *go = &ccp_gotoptions[f->unit];
323     ccp_options no;             /* options we've seen already */
324     ccp_options try;            /* options to ask for next time */
325
326     memset(&no, 0, sizeof(no));
327     try = *go;
328
329     if (go->bsd_compress && !no.bsd_compress && len >= CILEN_BSD_COMPRESS
330         && p[0] == CI_BSD_COMPRESS && p[1] == CILEN_BSD_COMPRESS) {
331         no.bsd_compress = 1;
332         /*
333          * Peer wants us to use a different number of bits
334          * or a different version.
335          */
336         if (BSD_VERSION(p[2]) != BSD_CURRENT_VERSION)
337             try.bsd_compress = 0;
338         else if (BSD_NBITS(p[2]) < go->bsd_bits)
339             try.bsd_bits = BSD_NBITS(p[2]);
340         p += CILEN_BSD_COMPRESS;
341         len -= CILEN_BSD_COMPRESS;
342     }
343
344     /*
345      * Have a look at any remaining options...???
346      */
347
348     if (len != 0)
349         return 0;
350
351     if (f->state != OPENED)
352         *go = try;
353     return 1;
354 }
355
356 /*
357  * ccp_rejci - reject some of our suggested compression methods.
358  */
359 static int
360 ccp_rejci(f, p, len)
361     fsm *f;
362     u_char *p;
363     int len;
364 {
365     ccp_options *go = &ccp_gotoptions[f->unit];
366     ccp_options try;            /* options to request next time */
367
368     try = *go;
369
370     if (go->bsd_compress && len >= CILEN_BSD_COMPRESS
371         && p[0] == CI_BSD_COMPRESS && p[1] == CILEN_BSD_COMPRESS) {
372         if (p[2] != BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits))
373             return 0;
374         try.bsd_compress = 0;
375         p += CILEN_BSD_COMPRESS;
376         len -= CILEN_BSD_COMPRESS;
377     }
378
379     if (len != 0)
380         return 0;
381
382     if (f->state != OPENED)
383         *go = try;
384
385     return 1;
386 }
387
388 /*
389  * ccp_reqci - processed a received configure-request.
390  * Returns CONFACK, CONFNAK or CONFREJ and the packet modified
391  * appropriately.
392  */
393 static int
394 ccp_reqci(f, p, lenp, dont_nak)
395     fsm *f;
396     u_char *p;
397     int *lenp;
398     int dont_nak;
399 {
400     int ret, newret;
401     u_char *p0, *retp;
402     int len, clen, type, nb;
403     ccp_options *ho = &ccp_hisoptions[f->unit];
404     ccp_options *ao = &ccp_allowoptions[f->unit];
405
406     ret = CONFACK;
407     retp = p0 = p;
408     len = *lenp;
409
410     memset(ho, 0, sizeof(ccp_options));
411
412     while (len > 0) {
413         newret = CONFACK;
414         if (len < 2 || p[1] < 2 || p[1] > len) {
415             /* length is bad */
416             clen = len;
417             newret = CONFREJ;
418
419         } else {
420             type = p[0];
421             clen = p[1];
422
423             switch (type) {
424             case CI_BSD_COMPRESS:
425                 if (!ao->bsd_compress || clen != CILEN_BSD_COMPRESS) {
426                     newret = CONFREJ;
427                     break;
428                 }
429
430                 ho->bsd_compress = 1;
431                 ho->bsd_bits = nb = BSD_NBITS(p[2]);
432                 if (BSD_VERSION(p[2]) != BSD_CURRENT_VERSION
433                     || nb > ao->bsd_bits) {
434                     newret = CONFNAK;
435                     nb = ao->bsd_bits;
436                 } else if (nb < BSD_MIN_BITS) {
437                     newret = CONFREJ;
438                 } else if (!ccp_test(f->unit, p, CILEN_BSD_COMPRESS, 1)) {
439                     if (nb > BSD_MIN_BITS) {
440                         --nb;
441                         newret = CONFNAK;
442                     } else
443                         newret = CONFREJ;
444                 }
445                 if (newret == CONFNAK && !dont_nak) {
446                     p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, nb);
447                 }
448
449                 break;
450
451             default:
452                 newret = CONFREJ;
453             }
454         }
455
456         if (newret == CONFNAK && dont_nak)
457             newret = CONFREJ;
458         if (!(newret == CONFACK || newret == CONFNAK && ret == CONFREJ)) {
459             /* we're returning this option */
460             if (newret == CONFREJ && ret == CONFNAK)
461                 retp = p0;
462             ret = newret;
463             if (p != retp)
464                 BCOPY(p, retp, clen);
465             retp += clen;
466         }
467
468         p += clen;
469         len -= clen;
470     }
471
472     if (ret != CONFACK)
473         *lenp = retp - p0;
474     return ret;
475 }
476
477 /*
478  * CCP has come up - inform the kernel driver.
479  */
480 static void
481 ccp_up(f)
482     fsm *f;
483 {
484     ccp_options *go = &ccp_gotoptions[f->unit];
485     ccp_options *ho = &ccp_hisoptions[f->unit];
486
487     ccp_flags_set(f->unit, 1, 1);
488     if (go->bsd_compress || ho->bsd_compress)
489         syslog(LOG_NOTICE, "%s enabled",
490                go->bsd_compress? ho->bsd_compress? "Compression":
491                "Receive compression": "Transmit compression");
492 }
493
494 /*
495  * CCP has gone down - inform the kernel driver.
496  */
497 static void
498 ccp_down(f)
499     fsm *f;
500 {
501     if (ccp_localstate[f->unit] & RACK_PENDING)
502         UNTIMEOUT(ccp_rack_timeout, (caddr_t) f);
503     ccp_localstate[f->unit] = 0;
504     ccp_flags_set(f->unit, 1, 0);
505 }
506
507 /*
508  * Print the contents of a CCP packet.
509  */
510 char *ccp_codenames[] = {
511     "ConfReq", "ConfAck", "ConfNak", "ConfRej",
512     "TermReq", "TermAck", "CodeRej",
513     NULL, NULL, NULL, NULL, NULL, NULL,
514     "ResetReq", "ResetAck",
515 };
516
517 int
518 ccp_printpkt(p, plen, printer, arg)
519     u_char *p;
520     int plen;
521     void (*printer) __P((void *, char *, ...));
522     void *arg;
523 {
524     u_char *p0, *optend;
525     int code, id, len;
526     int optlen;
527
528     p0 = p;
529     if (plen < HEADERLEN)
530         return 0;
531     code = p[0];
532     id = p[1];
533     len = (p[2] << 8) + p[3];
534     if (len < HEADERLEN || len > plen)
535         return 0;
536
537     if (code >= 1 && code <= sizeof(ccp_codenames) / sizeof(char *)
538         && ccp_codenames[code-1] != NULL)
539         printer(arg, " %s", ccp_codenames[code-1]);
540     else
541         printer(arg, " code=0x%x", code);
542     printer(arg, " id=0x%x", id);
543     len -= HEADERLEN;
544     p += HEADERLEN;
545
546     switch (code) {
547     case CONFREQ:
548     case CONFACK:
549     case CONFNAK:
550     case CONFREJ:
551         /* print list of possible compression methods */
552         while (len >= 2) {
553             code = p[0];
554             optlen = p[1];
555             if (optlen < 2 || optlen > len)
556                 break;
557             printer(arg, " <");
558             len -= optlen;
559             optend = p + optlen;
560             switch (code) {
561             case CI_BSD_COMPRESS:
562                 if (optlen >= CILEN_BSD_COMPRESS) {
563                     printer(arg, "bsd v%d %d", BSD_VERSION(p[2]),
564                             BSD_NBITS(p[2]));
565                     p += CILEN_BSD_COMPRESS;
566                 }
567                 break;
568             }
569             while (p < optend)
570                 printer(arg, " %.2x", *p++);
571             printer(arg, ">");
572         }
573         break;
574     }
575
576     /* dump out the rest of the packet in hex */
577     while (--len >= 0)
578         printer(arg, " %.2x", *p++);
579
580     return p - p0;
581 }
582
583 /*
584  * We have received a packet that the decompressor failed to
585  * decompress.  Here we would expect to issue a reset-request, but
586  * Motorola has a patent on resetting the compressor as a result of
587  * detecting an error in the decompressed data after decompression.
588  * (See US patent 5,130,993; international patent publication number
589  * WO 91/10289; Australian patent 73296/91.)
590  *
591  * So we ask the kernel whether the error was detected after
592  * decompression; if it was, we take CCP down, thus disabling
593  * compression :-(, otherwise we issue the reset-request.
594  */
595 void
596 ccp_datainput(unit, pkt, len)
597     int unit;
598     u_char *pkt;
599     int len;
600 {
601     fsm *f;
602
603     f = &ccp_fsm[unit];
604     if (f->state == OPENED) {
605         if (ccp_fatal_error(unit)) {
606             /*
607              * Disable compression by taking CCP down.
608              */
609             syslog(LOG_ERR, "Lost compression sync: disabling compression");
610             ccp_close(unit);
611         } else {
612             /*
613              * Send a reset-request to reset the peer's compressor.
614              * We don't do that if we are still waiting for an
615              * acknowledgement to a previous reset-request.
616              */
617             if (!(ccp_localstate[f->unit] & RACK_PENDING)) {
618                 fsm_sdata(f, CCP_RESETREQ, f->reqid = ++f->id, NULL, 0);
619                 TIMEOUT(ccp_rack_timeout, (caddr_t) f, RACKTIMEOUT);
620                 ccp_localstate[f->unit] |= RACK_PENDING;
621             } else
622                 ccp_localstate[f->unit] |= RREQ_REPEAT;
623         }
624     }
625 }
626
627 /*
628  * Timeout waiting for reset-ack.
629  */
630 static void
631 ccp_rack_timeout(arg)
632     caddr_t arg;
633 {
634     fsm *f = (fsm *) arg;
635
636     if (f->state == OPENED && ccp_localstate[f->unit] & RREQ_REPEAT) {
637         fsm_sdata(f, CCP_RESETREQ, f->reqid, NULL, 0);
638         TIMEOUT(ccp_rack_timeout, (caddr_t) f, RACKTIMEOUT);
639         ccp_localstate[f->unit] &= ~RREQ_REPEAT;
640     } else
641         ccp_localstate[f->unit] &= ~RACK_PENDING;
642 }
643