]> git.ozlabs.org Git - ppp.git/blob - pppd/ccp.c
fix up prototypes; let sysdep stuff make device non-blocking
[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.11 1995/08/16 04:15:38 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      * If we get a terminate-ack and we're not asking for compression,
188      * close CCP.
189      */
190     if (oldstate == REQSENT && p[0] == TERMACK
191         && !ANY_COMPRESS(ccp_gotoptions[unit]))
192         ccp_close(unit);
193 }
194
195 /*
196  * Handle a CCP-specific code.
197  */
198 static int
199 ccp_extcode(f, code, id, p, len)
200     fsm *f;
201     int code, id;
202     u_char *p;
203     int len;
204 {
205     switch (code) {
206     case CCP_RESETREQ:
207         if (f->state != OPENED)
208             break;
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);
212         break;
213
214     case CCP_RESETACK:
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);
218         }
219         break;
220
221     default:
222         return 0;
223     }
224
225     return 1;
226 }
227
228 /*
229  * ccp_protrej - peer doesn't talk CCP.
230  */
231 void
232 ccp_protrej(unit)
233     int unit;
234 {
235     ccp_flags_set(unit, 0, 0);
236     fsm_lowerdown(&ccp_fsm[unit]);
237 }
238
239 /*
240  * ccp_resetci - initialize at start of negotiation.
241  */
242 static void
243 ccp_resetci(f)
244     fsm *f;
245 {
246     ccp_options *go = &ccp_gotoptions[f->unit];
247     u_char opt_buf[16];
248
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;
256     }
257 }
258
259 /*
260  * ccp_cilen - Return total length of our configuration info.
261  */
262 static int
263 ccp_cilen(f)
264     fsm *f;
265 {
266     ccp_options *go = &ccp_gotoptions[f->unit];
267
268     return (go->bsd_compress? CILEN_BSD_COMPRESS: 0);
269 }
270
271 /*
272  * ccp_addci - put our requests in a packet.
273  */
274 static void
275 ccp_addci(f, p, lenp)
276     fsm *f;
277     u_char *p;
278     int *lenp;
279 {
280     ccp_options *go = &ccp_gotoptions[f->unit];
281     u_char *p0 = p;
282
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;
289         else
290             go->bsd_compress = 0;
291     }
292     *lenp = p - p0;
293 }
294
295 /*
296  * ccp_ackci - process a received configure-ack, and return
297  * 1 iff the packet was OK.
298  */
299 static int
300 ccp_ackci(f, p, len)
301     fsm *f;
302     u_char *p;
303     int len;
304 {
305     ccp_options *go = &ccp_gotoptions[f->unit];
306
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))
311             return 0;
312         p += CILEN_BSD_COMPRESS;
313         len -= CILEN_BSD_COMPRESS;
314     }
315     if (len != 0)
316         return 0;
317     return 1;
318 }
319
320 /*
321  * ccp_nakci - process received configure-nak.
322  * Returns 1 iff the nak was OK.
323  */
324 static int
325 ccp_nakci(f, p, len)
326     fsm *f;
327     u_char *p;
328     int len;
329 {
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 */
333
334     memset(&no, 0, sizeof(no));
335     try = *go;
336
337     if (go->bsd_compress && !no.bsd_compress && len >= CILEN_BSD_COMPRESS
338         && p[0] == CI_BSD_COMPRESS && p[1] == CILEN_BSD_COMPRESS) {
339         no.bsd_compress = 1;
340         /*
341          * Peer wants us to use a different number of bits
342          * or a different version.
343          */
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;
350     }
351
352     /*
353      * Have a look at any remaining options...???
354      */
355
356     if (len != 0)
357         return 0;
358
359     if (f->state != OPENED)
360         *go = try;
361     return 1;
362 }
363
364 /*
365  * ccp_rejci - reject some of our suggested compression methods.
366  */
367 static int
368 ccp_rejci(f, p, len)
369     fsm *f;
370     u_char *p;
371     int len;
372 {
373     ccp_options *go = &ccp_gotoptions[f->unit];
374     ccp_options try;            /* options to request next time */
375
376     try = *go;
377
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))
381             return 0;
382         try.bsd_compress = 0;
383         p += CILEN_BSD_COMPRESS;
384         len -= CILEN_BSD_COMPRESS;
385     }
386
387     if (len != 0)
388         return 0;
389
390     if (f->state != OPENED)
391         *go = try;
392
393     return 1;
394 }
395
396 /*
397  * ccp_reqci - processed a received configure-request.
398  * Returns CONFACK, CONFNAK or CONFREJ and the packet modified
399  * appropriately.
400  */
401 static int
402 ccp_reqci(f, p, lenp, dont_nak)
403     fsm *f;
404     u_char *p;
405     int *lenp;
406     int dont_nak;
407 {
408     int ret, newret;
409     u_char *p0, *retp;
410     int len, clen, type, nb;
411     ccp_options *ho = &ccp_hisoptions[f->unit];
412     ccp_options *ao = &ccp_allowoptions[f->unit];
413
414     ret = CONFACK;
415     retp = p0 = p;
416     len = *lenp;
417
418     memset(ho, 0, sizeof(ccp_options));
419
420     while (len > 0) {
421         newret = CONFACK;
422         if (len < 2 || p[1] < 2 || p[1] > len) {
423             /* length is bad */
424             clen = len;
425             newret = CONFREJ;
426
427         } else {
428             type = p[0];
429             clen = p[1];
430
431             switch (type) {
432             case CI_BSD_COMPRESS:
433                 if (!ao->bsd_compress || clen != CILEN_BSD_COMPRESS) {
434                     newret = CONFREJ;
435                     break;
436                 }
437
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) {
442                     newret = CONFNAK;
443                     nb = ao->bsd_bits;
444                 } else if (nb < BSD_MIN_BITS) {
445                     newret = CONFREJ;
446                 } else if (!ccp_test(f->unit, p, CILEN_BSD_COMPRESS, 1)) {
447                     if (nb > BSD_MIN_BITS) {
448                         --nb;
449                         newret = CONFNAK;
450                     } else
451                         newret = CONFREJ;
452                 }
453                 if (newret == CONFNAK && !dont_nak) {
454                     p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, nb);
455                 }
456
457                 break;
458
459             default:
460                 newret = CONFREJ;
461             }
462         }
463
464         if (newret == CONFNAK && dont_nak)
465             newret = CONFREJ;
466         if (!(newret == CONFACK || newret == CONFNAK && ret == CONFREJ)) {
467             /* we're returning this option */
468             if (newret == CONFREJ && ret == CONFNAK)
469                 retp = p0;
470             ret = newret;
471             if (p != retp)
472                 BCOPY(p, retp, clen);
473             retp += clen;
474         }
475
476         p += clen;
477         len -= clen;
478     }
479
480     if (ret != CONFACK)
481         *lenp = retp - p0;
482     return ret;
483 }
484
485 /*
486  * CCP has come up - inform the kernel driver.
487  */
488 static void
489 ccp_up(f)
490     fsm *f;
491 {
492     ccp_options *go = &ccp_gotoptions[f->unit];
493     ccp_options *ho = &ccp_hisoptions[f->unit];
494
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");
500 }
501
502 /*
503  * CCP has gone down - inform the kernel driver.
504  */
505 static void
506 ccp_down(f)
507     fsm *f;
508 {
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);
513 }
514
515 /*
516  * Print the contents of a CCP packet.
517  */
518 char *ccp_codenames[] = {
519     "ConfReq", "ConfAck", "ConfNak", "ConfRej",
520     "TermReq", "TermAck", "CodeRej",
521     NULL, NULL, NULL, NULL, NULL, NULL,
522     "ResetReq", "ResetAck",
523 };
524
525 int
526 ccp_printpkt(p, plen, printer, arg)
527     u_char *p;
528     int plen;
529     void (*printer) __P((void *, char *, ...));
530     void *arg;
531 {
532     u_char *p0, *optend;
533     int code, id, len;
534     int optlen;
535
536     p0 = p;
537     if (plen < HEADERLEN)
538         return 0;
539     code = p[0];
540     id = p[1];
541     len = (p[2] << 8) + p[3];
542     if (len < HEADERLEN || len > plen)
543         return 0;
544
545     if (code >= 1 && code <= sizeof(ccp_codenames) / sizeof(char *)
546         && ccp_codenames[code-1] != NULL)
547         printer(arg, " %s", ccp_codenames[code-1]);
548     else
549         printer(arg, " code=0x%x", code);
550     printer(arg, " id=0x%x", id);
551     len -= HEADERLEN;
552     p += HEADERLEN;
553
554     switch (code) {
555     case CONFREQ:
556     case CONFACK:
557     case CONFNAK:
558     case CONFREJ:
559         /* print list of possible compression methods */
560         while (len >= 2) {
561             code = p[0];
562             optlen = p[1];
563             if (optlen < 2 || optlen > len)
564                 break;
565             printer(arg, " <");
566             len -= optlen;
567             optend = p + optlen;
568             switch (code) {
569             case CI_BSD_COMPRESS:
570                 if (optlen >= CILEN_BSD_COMPRESS) {
571                     printer(arg, "bsd v%d %d", BSD_VERSION(p[2]),
572                             BSD_NBITS(p[2]));
573                     p += CILEN_BSD_COMPRESS;
574                 }
575                 break;
576             }
577             while (p < optend)
578                 printer(arg, " %.2x", *p++);
579             printer(arg, ">");
580         }
581         break;
582     }
583
584     /* dump out the rest of the packet in hex */
585     while (--len >= 0)
586         printer(arg, " %.2x", *p++);
587
588     return p - p0;
589 }
590
591 /*
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.)
598  *
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.
602  */
603 void
604 ccp_datainput(unit, pkt, len)
605     int unit;
606     u_char *pkt;
607     int len;
608 {
609     fsm *f;
610
611     f = &ccp_fsm[unit];
612     if (f->state == OPENED) {
613         if (ccp_fatal_error(unit)) {
614             /*
615              * Disable compression by taking CCP down.
616              */
617             syslog(LOG_ERR, "Lost compression sync: disabling compression");
618             ccp_close(unit);
619         } else {
620             /*
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.
624              */
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;
629             } else
630                 ccp_localstate[f->unit] |= RREQ_REPEAT;
631         }
632     }
633 }
634
635 /*
636  * Timeout waiting for reset-ack.
637  */
638 static void
639 ccp_rack_timeout(arg)
640     caddr_t arg;
641 {
642     fsm *f = (fsm *) arg;
643
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;
648     } else
649         ccp_localstate[f->unit] &= ~RACK_PENDING;
650 }
651