xref: /freebsd/usr.sbin/ppp/ccp.c (revision 1b6c76a2fe091c74f08427e6c870851025a9cf67)
1 /*-
2  * Copyright (c) 1996 - 2001 Brian Somers <brian@Awfulhak.org>
3  *          based on work by Toshiharu OHNO <tony-o@iij.ad.jp>
4  *                           Internet Initiative Japan, Inc (IIJ)
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30 
31 #include <sys/param.h>
32 #include <netinet/in.h>
33 #include <netinet/in_systm.h>
34 #include <netinet/ip.h>
35 #include <sys/un.h>
36 
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>	/* memcpy() on some archs */
40 #include <termios.h>
41 
42 #include "layer.h"
43 #include "defs.h"
44 #include "command.h"
45 #include "mbuf.h"
46 #include "log.h"
47 #include "timer.h"
48 #include "fsm.h"
49 #include "proto.h"
50 #include "pred.h"
51 #include "deflate.h"
52 #include "throughput.h"
53 #include "iplist.h"
54 #include "slcompress.h"
55 #include "lqr.h"
56 #include "hdlc.h"
57 #include "lcp.h"
58 #include "ccp.h"
59 #include "ipcp.h"
60 #include "filter.h"
61 #include "descriptor.h"
62 #include "prompt.h"
63 #include "link.h"
64 #include "mp.h"
65 #include "async.h"
66 #include "physical.h"
67 #ifndef NORADIUS
68 #include "radius.h"
69 #endif
70 #ifdef HAVE_DES
71 #include "mppe.h"
72 #endif
73 #include "bundle.h"
74 
75 static void CcpSendConfigReq(struct fsm *);
76 static void CcpSentTerminateReq(struct fsm *);
77 static void CcpSendTerminateAck(struct fsm *, u_char);
78 static void CcpDecodeConfig(struct fsm *, u_char *, int, int,
79                             struct fsm_decode *);
80 static void CcpLayerStart(struct fsm *);
81 static void CcpLayerFinish(struct fsm *);
82 static int CcpLayerUp(struct fsm *);
83 static void CcpLayerDown(struct fsm *);
84 static void CcpInitRestartCounter(struct fsm *, int);
85 static int CcpRecvResetReq(struct fsm *);
86 static void CcpRecvResetAck(struct fsm *, u_char);
87 
88 static struct fsm_callbacks ccp_Callbacks = {
89   CcpLayerUp,
90   CcpLayerDown,
91   CcpLayerStart,
92   CcpLayerFinish,
93   CcpInitRestartCounter,
94   CcpSendConfigReq,
95   CcpSentTerminateReq,
96   CcpSendTerminateAck,
97   CcpDecodeConfig,
98   CcpRecvResetReq,
99   CcpRecvResetAck
100 };
101 
102 static const char * const ccp_TimerNames[] =
103   {"CCP restart", "CCP openmode", "CCP stopped"};
104 
105 static const char *
106 protoname(int proto)
107 {
108   static char const * const cftypes[] = {
109     /* Check out the latest ``Compression Control Protocol'' rfc (1962) */
110     "OUI",		/* 0: OUI */
111     "PRED1",		/* 1: Predictor type 1 */
112     "PRED2",		/* 2: Predictor type 2 */
113     "PUDDLE",		/* 3: Puddle Jumber */
114     NULL, NULL, NULL, NULL, NULL, NULL,
115     NULL, NULL, NULL, NULL, NULL, NULL,
116     "HWPPC",		/* 16: Hewlett-Packard PPC */
117     "STAC",		/* 17: Stac Electronics LZS (rfc1974) */
118     "MPPE",		/* 18: Microsoft PPC (rfc2118) and */
119 			/*     Microsoft PPE (draft-ietf-pppext-mppe) */
120     "GAND",		/* 19: Gandalf FZA (rfc1993) */
121     "V42BIS",		/* 20: ARG->DATA.42bis compression */
122     "BSD",		/* 21: BSD LZW Compress */
123     NULL,
124     "LZS-DCP",		/* 23: LZS-DCP Compression Protocol (rfc1967) */
125     "MAGNALINK/DEFLATE",/* 24: Magnalink Variable Resource (rfc1975) */
126 			/* 24: Deflate (according to pppd-2.3.*) */
127     "DCE",		/* 25: Data Circuit-Terminating Equip (rfc1976) */
128     "DEFLATE",		/* 26: Deflate (rfc1979) */
129   };
130 
131   if (proto < 0 || proto > sizeof cftypes / sizeof *cftypes ||
132       cftypes[proto] == NULL)
133     return HexStr(proto, NULL, 0);
134 
135   return cftypes[proto];
136 }
137 
138 /* We support these algorithms, and Req them in the given order */
139 static const struct ccp_algorithm * const algorithm[] = {
140   &DeflateAlgorithm,
141   &Pred1Algorithm,
142   &PppdDeflateAlgorithm
143 #ifdef HAVE_DES
144   , &MPPEAlgorithm
145 #endif
146 };
147 
148 #define NALGORITHMS (sizeof algorithm/sizeof algorithm[0])
149 
150 int
151 ccp_ReportStatus(struct cmdargs const *arg)
152 {
153   struct ccp_opt **o;
154   struct link *l;
155   struct ccp *ccp;
156   int f;
157 
158   l = command_ChooseLink(arg);
159   ccp = &l->ccp;
160 
161   prompt_Printf(arg->prompt, "%s: %s [%s]\n", l->name, ccp->fsm.name,
162                 State2Nam(ccp->fsm.state));
163   if (ccp->fsm.state == ST_OPENED) {
164     prompt_Printf(arg->prompt, " My protocol = %s, His protocol = %s\n",
165                   protoname(ccp->my_proto), protoname(ccp->his_proto));
166     prompt_Printf(arg->prompt, " Output: %ld --> %ld,  Input: %ld --> %ld\n",
167                   ccp->uncompout, ccp->compout,
168                   ccp->compin, ccp->uncompin);
169   }
170 
171   if (ccp->in.algorithm != -1)
172     prompt_Printf(arg->prompt, "\n Input Options:  %s\n",
173                   (*algorithm[ccp->in.algorithm]->Disp)(&ccp->in.opt));
174 
175   if (ccp->out.algorithm != -1) {
176     o = &ccp->out.opt;
177     for (f = 0; f < ccp->out.algorithm; f++)
178       if (IsEnabled(ccp->cfg.neg[algorithm[f]->Neg]))
179         o = &(*o)->next;
180     prompt_Printf(arg->prompt, " Output Options: %s\n",
181                   (*algorithm[ccp->out.algorithm]->Disp)(&(*o)->val));
182   }
183 
184   prompt_Printf(arg->prompt, "\n Defaults: ");
185   prompt_Printf(arg->prompt, "FSM retry = %us, max %u Config"
186                 " REQ%s, %u Term REQ%s\n", ccp->cfg.fsm.timeout,
187                 ccp->cfg.fsm.maxreq, ccp->cfg.fsm.maxreq == 1 ? "" : "s",
188                 ccp->cfg.fsm.maxtrm, ccp->cfg.fsm.maxtrm == 1 ? "" : "s");
189   prompt_Printf(arg->prompt, "           deflate windows: ");
190   prompt_Printf(arg->prompt, "incoming = %d, ", ccp->cfg.deflate.in.winsize);
191   prompt_Printf(arg->prompt, "outgoing = %d\n", ccp->cfg.deflate.out.winsize);
192 #ifdef HAVE_DES
193   prompt_Printf(arg->prompt, "           MPPE: ");
194   if (ccp->cfg.mppe.keybits)
195     prompt_Printf(arg->prompt, "%d bits, ", ccp->cfg.mppe.keybits);
196   else
197     prompt_Printf(arg->prompt, "any bits, ");
198   switch (ccp->cfg.mppe.state) {
199   case MPPE_STATEFUL:
200     prompt_Printf(arg->prompt, "statefull");
201     break;
202   case MPPE_STATELESS:
203     prompt_Printf(arg->prompt, "stateless");
204     break;
205   case MPPE_ANYSTATE:
206     prompt_Printf(arg->prompt, "any state");
207     break;
208   }
209   prompt_Printf(arg->prompt, "%s\n",
210                 ccp->cfg.mppe.required ? ", required" : "");
211 #endif
212 
213   prompt_Printf(arg->prompt, "\n           DEFLATE:    %s\n",
214                 command_ShowNegval(ccp->cfg.neg[CCP_NEG_DEFLATE]));
215   prompt_Printf(arg->prompt, "           PREDICTOR1: %s\n",
216                 command_ShowNegval(ccp->cfg.neg[CCP_NEG_PRED1]));
217   prompt_Printf(arg->prompt, "           DEFLATE24:  %s\n",
218                 command_ShowNegval(ccp->cfg.neg[CCP_NEG_DEFLATE24]));
219 #ifdef HAVE_DES
220   prompt_Printf(arg->prompt, "           MPPE:       %s\n",
221                 command_ShowNegval(ccp->cfg.neg[CCP_NEG_MPPE]));
222 #endif
223   return 0;
224 }
225 
226 void
227 ccp_SetupCallbacks(struct ccp *ccp)
228 {
229   ccp->fsm.fn = &ccp_Callbacks;
230   ccp->fsm.FsmTimer.name = ccp_TimerNames[0];
231   ccp->fsm.OpenTimer.name = ccp_TimerNames[1];
232   ccp->fsm.StoppedTimer.name = ccp_TimerNames[2];
233 }
234 
235 void
236 ccp_Init(struct ccp *ccp, struct bundle *bundle, struct link *l,
237          const struct fsm_parent *parent)
238 {
239   /* Initialise ourselves */
240 
241   fsm_Init(&ccp->fsm, "CCP", PROTO_CCP, 1, CCP_MAXCODE, LogCCP,
242            bundle, l, parent, &ccp_Callbacks, ccp_TimerNames);
243 
244   ccp->cfg.deflate.in.winsize = 0;
245   ccp->cfg.deflate.out.winsize = 15;
246   ccp->cfg.fsm.timeout = DEF_FSMRETRY;
247   ccp->cfg.fsm.maxreq = DEF_FSMTRIES;
248   ccp->cfg.fsm.maxtrm = DEF_FSMTRIES;
249   ccp->cfg.neg[CCP_NEG_DEFLATE] = NEG_ENABLED|NEG_ACCEPTED;
250   ccp->cfg.neg[CCP_NEG_PRED1] = NEG_ENABLED|NEG_ACCEPTED;
251   ccp->cfg.neg[CCP_NEG_DEFLATE24] = 0;
252 #ifdef HAVE_DES
253   ccp->cfg.mppe.keybits = 0;
254   ccp->cfg.mppe.state = MPPE_ANYSTATE;
255   ccp->cfg.mppe.required = 0;
256   ccp->cfg.neg[CCP_NEG_MPPE] = NEG_ENABLED|NEG_ACCEPTED;
257 #endif
258 
259   ccp_Setup(ccp);
260 }
261 
262 void
263 ccp_Setup(struct ccp *ccp)
264 {
265   /* Set ourselves up for a startup */
266   ccp->fsm.open_mode = 0;
267   ccp->his_proto = ccp->my_proto = -1;
268   ccp->reset_sent = ccp->last_reset = -1;
269   ccp->in.algorithm = ccp->out.algorithm = -1;
270   ccp->in.state = ccp->out.state = NULL;
271   ccp->in.opt.id = -1;
272   ccp->out.opt = NULL;
273   ccp->his_reject = ccp->my_reject = 0;
274   ccp->uncompout = ccp->compout = 0;
275   ccp->uncompin = ccp->compin = 0;
276 }
277 
278 /*
279  * Is ccp *REQUIRED* ?
280  * We ask each of the configured ccp protocols if they're required and
281  * return TRUE if they are.
282  *
283  * It's not possible for the peer to reject a required ccp protocol
284  * without our state machine bringing the supporting lcp layer down.
285  *
286  * If ccp is required but not open, the NCP layer should not push
287  * any data into the link.
288  */
289 int
290 ccp_Required(struct ccp *ccp)
291 {
292   int f;
293 
294   for (f = 0; f < NALGORITHMS; f++)
295     if (IsEnabled(ccp->cfg.neg[algorithm[f]->Neg]) &&
296         (*algorithm[f]->Required)(&ccp->fsm))
297       return 1;
298 
299   return 0;
300 }
301 
302 static void
303 CcpInitRestartCounter(struct fsm *fp, int what)
304 {
305   /* Set fsm timer load */
306   struct ccp *ccp = fsm2ccp(fp);
307 
308   fp->FsmTimer.load = ccp->cfg.fsm.timeout * SECTICKS;
309   switch (what) {
310     case FSM_REQ_TIMER:
311       fp->restart = ccp->cfg.fsm.maxreq;
312       break;
313     case FSM_TRM_TIMER:
314       fp->restart = ccp->cfg.fsm.maxtrm;
315       break;
316     default:
317       fp->restart = 1;
318       break;
319   }
320 }
321 
322 static void
323 CcpSendConfigReq(struct fsm *fp)
324 {
325   /* Send config REQ please */
326   struct ccp *ccp = fsm2ccp(fp);
327   struct ccp_opt **o;
328   u_char *cp, buff[100];
329   int f, alloc;
330 
331   cp = buff;
332   o = &ccp->out.opt;
333   alloc = ccp->his_reject == 0 && ccp->out.opt == NULL;
334   ccp->my_proto = -1;
335   ccp->out.algorithm = -1;
336   for (f = 0; f < NALGORITHMS; f++)
337     if (IsEnabled(ccp->cfg.neg[algorithm[f]->Neg]) &&
338         !REJECTED(ccp, algorithm[f]->id) &&
339         (*algorithm[f]->Usable)(fp)) {
340 
341       if (!alloc)
342         for (o = &ccp->out.opt; *o != NULL; o = &(*o)->next)
343           if ((*o)->val.id == algorithm[f]->id && (*o)->algorithm == f)
344             break;
345 
346       if (alloc || *o == NULL) {
347         *o = (struct ccp_opt *)malloc(sizeof(struct ccp_opt));
348         (*o)->val.id = algorithm[f]->id;
349         (*o)->val.len = 2;
350         (*o)->next = NULL;
351         (*o)->algorithm = f;
352         (*algorithm[f]->o.OptInit)(&(*o)->val, &ccp->cfg);
353       }
354 
355       if (cp + (*o)->val.len > buff + sizeof buff) {
356         log_Printf(LogERROR, "%s: CCP REQ buffer overrun !\n", fp->link->name);
357         break;
358       }
359       memcpy(cp, &(*o)->val, (*o)->val.len);
360       cp += (*o)->val.len;
361 
362       ccp->my_proto = (*o)->val.id;
363       ccp->out.algorithm = f;
364 
365       if (alloc)
366         o = &(*o)->next;
367     }
368 
369   fsm_Output(fp, CODE_CONFIGREQ, fp->reqid, buff, cp - buff, MB_CCPOUT);
370 }
371 
372 void
373 ccp_SendResetReq(struct fsm *fp)
374 {
375   /* We can't read our input - ask peer to reset */
376   struct ccp *ccp = fsm2ccp(fp);
377 
378   ccp->reset_sent = fp->reqid;
379   ccp->last_reset = -1;
380   fsm_Output(fp, CODE_RESETREQ, fp->reqid, NULL, 0, MB_CCPOUT);
381 }
382 
383 static void
384 CcpSentTerminateReq(struct fsm *fp)
385 {
386   /* Term REQ just sent by FSM */
387 }
388 
389 static void
390 CcpSendTerminateAck(struct fsm *fp, u_char id)
391 {
392   /* Send Term ACK please */
393   fsm_Output(fp, CODE_TERMACK, id, NULL, 0, MB_CCPOUT);
394 }
395 
396 static int
397 CcpRecvResetReq(struct fsm *fp)
398 {
399   /* Got a reset REQ, reset outgoing dictionary */
400   struct ccp *ccp = fsm2ccp(fp);
401   if (ccp->out.state == NULL)
402     return 1;
403   return (*algorithm[ccp->out.algorithm]->o.Reset)(ccp->out.state);
404 }
405 
406 static void
407 CcpLayerStart(struct fsm *fp)
408 {
409   /* We're about to start up ! */
410   struct ccp *ccp = fsm2ccp(fp);
411 
412   log_Printf(LogCCP, "%s: LayerStart.\n", fp->link->name);
413   fp->more.reqs = fp->more.naks = fp->more.rejs = ccp->cfg.fsm.maxreq * 3;
414 }
415 
416 static void
417 CcpLayerDown(struct fsm *fp)
418 {
419   /* About to come down */
420   struct ccp *ccp = fsm2ccp(fp);
421   struct ccp_opt *next;
422 
423   log_Printf(LogCCP, "%s: LayerDown.\n", fp->link->name);
424   if (ccp->in.state != NULL) {
425     (*algorithm[ccp->in.algorithm]->i.Term)(ccp->in.state);
426     ccp->in.state = NULL;
427     ccp->in.algorithm = -1;
428   }
429   if (ccp->out.state != NULL) {
430     (*algorithm[ccp->out.algorithm]->o.Term)(ccp->out.state);
431     ccp->out.state = NULL;
432     ccp->out.algorithm = -1;
433   }
434   ccp->his_reject = ccp->my_reject = 0;
435 
436   while (ccp->out.opt) {
437     next = ccp->out.opt->next;
438     free(ccp->out.opt);
439     ccp->out.opt = next;
440   }
441   ccp_Setup(ccp);
442 }
443 
444 static void
445 CcpLayerFinish(struct fsm *fp)
446 {
447   /* We're now down */
448   struct ccp *ccp = fsm2ccp(fp);
449   struct ccp_opt *next;
450 
451   log_Printf(LogCCP, "%s: LayerFinish.\n", fp->link->name);
452 
453   /*
454    * Nuke options that may be left over from sending a REQ but never
455    * coming up.
456    */
457   while (ccp->out.opt) {
458     next = ccp->out.opt->next;
459     free(ccp->out.opt);
460     ccp->out.opt = next;
461   }
462 
463   if (ccp_Required(ccp)) {
464     if (fp->link->lcp.fsm.state == ST_OPENED)
465       log_Printf(LogLCP, "%s: Closing due to CCP completion\n", fp->link->name);
466     fsm_Close(&fp->link->lcp.fsm);
467   }
468 }
469 
470 /*  Called when CCP has reached the OPEN state */
471 static int
472 CcpLayerUp(struct fsm *fp)
473 {
474   /* We're now up */
475   struct ccp *ccp = fsm2ccp(fp);
476   struct ccp_opt **o;
477   int f;
478 
479   log_Printf(LogCCP, "%s: LayerUp.\n", fp->link->name);
480 
481   if (ccp->in.state == NULL && ccp->in.algorithm >= 0 &&
482       ccp->in.algorithm < NALGORITHMS) {
483     ccp->in.state = (*algorithm[ccp->in.algorithm]->i.Init)(&ccp->in.opt);
484     if (ccp->in.state == NULL) {
485       log_Printf(LogERROR, "%s: %s (in) initialisation failure\n",
486                 fp->link->name, protoname(ccp->his_proto));
487       ccp->his_proto = ccp->my_proto = -1;
488       fsm_Close(fp);
489       return 0;
490     }
491   }
492 
493   o = &ccp->out.opt;
494   for (f = 0; f < ccp->out.algorithm; f++)
495     if (IsEnabled(ccp->cfg.neg[algorithm[f]->Neg]))
496       o = &(*o)->next;
497 
498   if (ccp->out.state == NULL && ccp->out.algorithm >= 0 &&
499       ccp->out.algorithm < NALGORITHMS) {
500     ccp->out.state = (*algorithm[ccp->out.algorithm]->o.Init)(&(*o)->val);
501     if (ccp->out.state == NULL) {
502       log_Printf(LogERROR, "%s: %s (out) initialisation failure\n",
503                 fp->link->name, protoname(ccp->my_proto));
504       ccp->his_proto = ccp->my_proto = -1;
505       fsm_Close(fp);
506       return 0;
507     }
508   }
509 
510   fp->more.reqs = fp->more.naks = fp->more.rejs = ccp->cfg.fsm.maxreq * 3;
511 
512   log_Printf(LogCCP, "%s: Out = %s[%d], In = %s[%d]\n",
513             fp->link->name, protoname(ccp->my_proto), ccp->my_proto,
514             protoname(ccp->his_proto), ccp->his_proto);
515 
516   return 1;
517 }
518 
519 static void
520 CcpDecodeConfig(struct fsm *fp, u_char *cp, int plen, int mode_type,
521                 struct fsm_decode *dec)
522 {
523   /* Deal with incoming data */
524   struct ccp *ccp = fsm2ccp(fp);
525   int type, length, f;
526   const char *end;
527 
528   if (mode_type == MODE_REQ)
529     ccp->in.algorithm = -1;	/* In case we've received two REQs in a row */
530 
531   while (plen >= sizeof(struct fsmconfig)) {
532     type = *cp;
533     length = cp[1];
534 
535     if (length == 0) {
536       log_Printf(LogCCP, "%s: CCP size zero\n", fp->link->name);
537       break;
538     }
539 
540     if (length > sizeof(struct lcp_opt)) {
541       length = sizeof(struct lcp_opt);
542       log_Printf(LogCCP, "%s: Warning: Truncating length to %d\n",
543                 fp->link->name, length);
544     }
545 
546     for (f = NALGORITHMS-1; f > -1; f--)
547       if (algorithm[f]->id == type)
548         break;
549 
550     end = f == -1 ? "" : (*algorithm[f]->Disp)((struct lcp_opt *)cp);
551     if (end == NULL)
552       end = "";
553 
554     log_Printf(LogCCP, " %s[%d] %s\n", protoname(type), length, end);
555 
556     if (f == -1) {
557       /* Don't understand that :-( */
558       if (mode_type == MODE_REQ) {
559         ccp->my_reject |= (1 << type);
560         memcpy(dec->rejend, cp, length);
561         dec->rejend += length;
562       }
563     } else {
564       struct ccp_opt *o;
565 
566       switch (mode_type) {
567       case MODE_REQ:
568 	if (IsAccepted(ccp->cfg.neg[algorithm[f]->Neg]) &&
569             (*algorithm[f]->Usable)(fp) &&
570             ccp->in.algorithm == -1) {
571 	  memcpy(&ccp->in.opt, cp, length);
572           switch ((*algorithm[f]->i.Set)(&ccp->in.opt, &ccp->cfg)) {
573           case MODE_REJ:
574 	    memcpy(dec->rejend, &ccp->in.opt, ccp->in.opt.len);
575 	    dec->rejend += ccp->in.opt.len;
576             break;
577           case MODE_NAK:
578 	    memcpy(dec->nakend, &ccp->in.opt, ccp->in.opt.len);
579 	    dec->nakend += ccp->in.opt.len;
580             break;
581           case MODE_ACK:
582 	    memcpy(dec->ackend, cp, length);
583 	    dec->ackend += length;
584 	    ccp->his_proto = type;
585             ccp->in.algorithm = f;		/* This one'll do :-) */
586             break;
587           }
588 	} else {
589 	  memcpy(dec->rejend, cp, length);
590 	  dec->rejend += length;
591 	}
592 	break;
593       case MODE_NAK:
594         for (o = ccp->out.opt; o != NULL; o = o->next)
595           if (o->val.id == cp[0])
596             break;
597         if (o == NULL)
598           log_Printf(LogCCP, "%s: Warning: Ignoring peer NAK of unsent"
599                      " option\n", fp->link->name);
600         else {
601 	  memcpy(&o->val, cp, length);
602           if ((*algorithm[f]->o.Set)(&o->val, &ccp->cfg) == MODE_ACK)
603             ccp->my_proto = algorithm[f]->id;
604           else {
605 	    ccp->his_reject |= (1 << type);
606 	    ccp->my_proto = -1;
607             if (algorithm[f]->Required(fp)) {
608               log_Printf(LogWARN, "%s: Cannot understand peers (required)"
609                          " %s negotiation\n", fp->link->name,
610                          protoname(algorithm[f]->id));
611               fsm_Close(&fp->link->lcp.fsm);
612             }
613           }
614         }
615         break;
616       case MODE_REJ:
617 	ccp->his_reject |= (1 << type);
618 	ccp->my_proto = -1;
619         if (algorithm[f]->Required(fp)) {
620           log_Printf(LogWARN, "%s: Peer rejected (required) %s negotiation\n",
621                      fp->link->name, protoname(algorithm[f]->id));
622           fsm_Close(&fp->link->lcp.fsm);
623         }
624 	break;
625       }
626     }
627 
628     plen -= cp[1];
629     cp += cp[1];
630   }
631 
632   if (mode_type != MODE_NOP) {
633     if (dec->rejend != dec->rej) {
634       /* rejects are preferred */
635       dec->ackend = dec->ack;
636       dec->nakend = dec->nak;
637       if (ccp->in.state == NULL) {
638         ccp->his_proto = -1;
639         ccp->in.algorithm = -1;
640       }
641     } else if (dec->nakend != dec->nak) {
642       /* then NAKs */
643       dec->ackend = dec->ack;
644       if (ccp->in.state == NULL) {
645         ccp->his_proto = -1;
646         ccp->in.algorithm = -1;
647       }
648     }
649   }
650 }
651 
652 extern struct mbuf *
653 ccp_Input(struct bundle *bundle, struct link *l, struct mbuf *bp)
654 {
655   /* Got PROTO_CCP from link */
656   m_settype(bp, MB_CCPIN);
657   if (bundle_Phase(bundle) == PHASE_NETWORK)
658     fsm_Input(&l->ccp.fsm, bp);
659   else {
660     if (bundle_Phase(bundle) < PHASE_NETWORK)
661       log_Printf(LogCCP, "%s: Error: Unexpected CCP in phase %s (ignored)\n",
662                  l->ccp.fsm.link->name, bundle_PhaseName(bundle));
663     m_freem(bp);
664   }
665   return NULL;
666 }
667 
668 static void
669 CcpRecvResetAck(struct fsm *fp, u_char id)
670 {
671   /* Got a reset ACK, reset incoming dictionary */
672   struct ccp *ccp = fsm2ccp(fp);
673 
674   if (ccp->reset_sent != -1) {
675     if (id != ccp->reset_sent) {
676       log_Printf(LogCCP, "%s: Incorrect ResetAck (id %d, not %d)"
677                 " ignored\n", fp->link->name, id, ccp->reset_sent);
678       return;
679     }
680     /* Whaddaya know - a correct reset ack */
681   } else if (id == ccp->last_reset)
682     log_Printf(LogCCP, "%s: Duplicate ResetAck (resetting again)\n",
683                fp->link->name);
684   else {
685     log_Printf(LogCCP, "%s: Unexpected ResetAck (id %d) ignored\n",
686                fp->link->name, id);
687     return;
688   }
689 
690   ccp->last_reset = ccp->reset_sent;
691   ccp->reset_sent = -1;
692   if (ccp->in.state != NULL)
693     (*algorithm[ccp->in.algorithm]->i.Reset)(ccp->in.state);
694 }
695 
696 static struct mbuf *
697 ccp_LayerPush(struct bundle *b, struct link *l, struct mbuf *bp,
698               int pri, u_short *proto)
699 {
700   if (PROTO_COMPRESSIBLE(*proto)) {
701     if (l->ccp.fsm.state != ST_OPENED) {
702       if (ccp_Required(&l->ccp)) {
703         /* The NCP layer shouldn't have let this happen ! */
704         log_Printf(LogERROR, "%s: Unexpected attempt to use an unopened and"
705                    " required CCP layer\n", l->name);
706         m_freem(bp);
707         bp = NULL;
708       }
709     } else if (l->ccp.out.state != NULL) {
710       bp = (*algorithm[l->ccp.out.algorithm]->o.Write)
711              (l->ccp.out.state, &l->ccp, l, pri, proto, bp);
712       switch (*proto) {
713         case PROTO_ICOMPD:
714           m_settype(bp, MB_ICOMPDOUT);
715           break;
716         case PROTO_COMPD:
717           m_settype(bp, MB_COMPDOUT);
718           break;
719       }
720     }
721   }
722 
723   return bp;
724 }
725 
726 static struct mbuf *
727 ccp_LayerPull(struct bundle *b, struct link *l, struct mbuf *bp, u_short *proto)
728 {
729   /*
730    * If proto isn't PROTO_[I]COMPD, we still want to pass it to the
731    * decompression routines so that the dictionary's updated
732    */
733   if (l->ccp.fsm.state == ST_OPENED) {
734     if (*proto == PROTO_COMPD || *proto == PROTO_ICOMPD) {
735       log_Printf(LogDEBUG, "ccp_LayerPull: PROTO_%sCOMPDP -> PROTO_IP\n",
736                  *proto == PROTO_ICOMPD ? "I" : "");
737       /* Decompress incoming data */
738       if (l->ccp.reset_sent != -1)
739         /* Send another REQ and put the packet in the bit bucket */
740         fsm_Output(&l->ccp.fsm, CODE_RESETREQ, l->ccp.reset_sent, NULL, 0,
741                    MB_CCPOUT);
742       else if (l->ccp.in.state != NULL) {
743         bp = (*algorithm[l->ccp.in.algorithm]->i.Read)
744                (l->ccp.in.state, &l->ccp, proto, bp);
745         switch (*proto) {
746           case PROTO_ICOMPD:
747             m_settype(bp, MB_ICOMPDIN);
748             break;
749           case PROTO_COMPD:
750             m_settype(bp, MB_COMPDIN);
751             break;
752         }
753         return bp;
754       }
755       m_freem(bp);
756       bp = NULL;
757     } else if (PROTO_COMPRESSIBLE(*proto) && l->ccp.in.state != NULL) {
758       log_Printf(LogDEBUG, "ccp_LayerPull: Ignore packet (dict only)\n");
759       /* Add incoming Network Layer traffic to our dictionary */
760       (*algorithm[l->ccp.in.algorithm]->i.DictSetup)
761         (l->ccp.in.state, &l->ccp, *proto, bp);
762     } else
763       log_Printf(LogDEBUG, "ccp_LayerPull: Ignore packet\n");
764   }
765 
766   return bp;
767 }
768 
769 u_short
770 ccp_Proto(struct ccp *ccp)
771 {
772   return !link2physical(ccp->fsm.link) || !ccp->fsm.bundle->ncp.mp.active ?
773          PROTO_COMPD : PROTO_ICOMPD;
774 }
775 
776 int
777 ccp_SetOpenMode(struct ccp *ccp)
778 {
779   int f;
780 
781   for (f = 0; f < CCP_NEG_TOTAL; f++)
782     if (IsEnabled(ccp->cfg.neg[f])) {
783       ccp->fsm.open_mode = 0;
784       return 1;
785     }
786 
787   ccp->fsm.open_mode = OPEN_PASSIVE;	/* Go straight to ST_STOPPED ? */
788 
789   for (f = 0; f < CCP_NEG_TOTAL; f++)
790     if (IsAccepted(ccp->cfg.neg[f]))
791       return 1;
792 
793   return 0;				/* No CCP at all */
794 }
795 
796 int
797 ccp_DefaultUsable(struct fsm *fp)
798 {
799   return 1;
800 }
801 
802 int
803 ccp_DefaultRequired(struct fsm *fp)
804 {
805   return 0;
806 }
807 
808 struct layer ccplayer = { LAYER_CCP, "ccp", ccp_LayerPush, ccp_LayerPull };
809