xref: /freebsd/usr.sbin/ppp/ccp.c (revision 5521ff5a4d1929056e7ffc982fac3341ca54df7c)
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 /*
303  * Report whether it's possible to increase a packet's size after
304  * compression (and by how much).
305  */
306 int
307 ccp_MTUOverhead(struct ccp *ccp)
308 {
309   if (ccp->fsm.state == ST_OPENED)
310     return algorithm[ccp->out.algorithm]->o.MTUOverhead;
311 
312   return 0;
313 }
314 
315 static void
316 CcpInitRestartCounter(struct fsm *fp, int what)
317 {
318   /* Set fsm timer load */
319   struct ccp *ccp = fsm2ccp(fp);
320 
321   fp->FsmTimer.load = ccp->cfg.fsm.timeout * SECTICKS;
322   switch (what) {
323     case FSM_REQ_TIMER:
324       fp->restart = ccp->cfg.fsm.maxreq;
325       break;
326     case FSM_TRM_TIMER:
327       fp->restart = ccp->cfg.fsm.maxtrm;
328       break;
329     default:
330       fp->restart = 1;
331       break;
332   }
333 }
334 
335 static void
336 CcpSendConfigReq(struct fsm *fp)
337 {
338   /* Send config REQ please */
339   struct ccp *ccp = fsm2ccp(fp);
340   struct ccp_opt **o;
341   u_char *cp, buff[100];
342   int f, alloc;
343 
344   cp = buff;
345   o = &ccp->out.opt;
346   alloc = ccp->his_reject == 0 && ccp->out.opt == NULL;
347   ccp->my_proto = -1;
348   ccp->out.algorithm = -1;
349   for (f = 0; f < NALGORITHMS; f++)
350     if (IsEnabled(ccp->cfg.neg[algorithm[f]->Neg]) &&
351         !REJECTED(ccp, algorithm[f]->id) &&
352         (*algorithm[f]->Usable)(fp)) {
353 
354       if (!alloc)
355         for (o = &ccp->out.opt; *o != NULL; o = &(*o)->next)
356           if ((*o)->val.id == algorithm[f]->id && (*o)->algorithm == f)
357             break;
358 
359       if (alloc || *o == NULL) {
360         *o = (struct ccp_opt *)malloc(sizeof(struct ccp_opt));
361         (*o)->val.id = algorithm[f]->id;
362         (*o)->val.len = 2;
363         (*o)->next = NULL;
364         (*o)->algorithm = f;
365         (*algorithm[f]->o.OptInit)(&(*o)->val, &ccp->cfg);
366       }
367 
368       if (cp + (*o)->val.len > buff + sizeof buff) {
369         log_Printf(LogERROR, "%s: CCP REQ buffer overrun !\n", fp->link->name);
370         break;
371       }
372       memcpy(cp, &(*o)->val, (*o)->val.len);
373       cp += (*o)->val.len;
374 
375       ccp->my_proto = (*o)->val.id;
376       ccp->out.algorithm = f;
377 
378       if (alloc)
379         o = &(*o)->next;
380     }
381 
382   fsm_Output(fp, CODE_CONFIGREQ, fp->reqid, buff, cp - buff, MB_CCPOUT);
383 }
384 
385 void
386 ccp_SendResetReq(struct fsm *fp)
387 {
388   /* We can't read our input - ask peer to reset */
389   struct ccp *ccp = fsm2ccp(fp);
390 
391   ccp->reset_sent = fp->reqid;
392   ccp->last_reset = -1;
393   fsm_Output(fp, CODE_RESETREQ, fp->reqid, NULL, 0, MB_CCPOUT);
394 }
395 
396 static void
397 CcpSentTerminateReq(struct fsm *fp)
398 {
399   /* Term REQ just sent by FSM */
400 }
401 
402 static void
403 CcpSendTerminateAck(struct fsm *fp, u_char id)
404 {
405   /* Send Term ACK please */
406   fsm_Output(fp, CODE_TERMACK, id, NULL, 0, MB_CCPOUT);
407 }
408 
409 static int
410 CcpRecvResetReq(struct fsm *fp)
411 {
412   /* Got a reset REQ, reset outgoing dictionary */
413   struct ccp *ccp = fsm2ccp(fp);
414   if (ccp->out.state == NULL)
415     return 1;
416   return (*algorithm[ccp->out.algorithm]->o.Reset)(ccp->out.state);
417 }
418 
419 static void
420 CcpLayerStart(struct fsm *fp)
421 {
422   /* We're about to start up ! */
423   struct ccp *ccp = fsm2ccp(fp);
424 
425   log_Printf(LogCCP, "%s: LayerStart.\n", fp->link->name);
426   fp->more.reqs = fp->more.naks = fp->more.rejs = ccp->cfg.fsm.maxreq * 3;
427 }
428 
429 static void
430 CcpLayerDown(struct fsm *fp)
431 {
432   /* About to come down */
433   struct ccp *ccp = fsm2ccp(fp);
434   struct ccp_opt *next;
435 
436   log_Printf(LogCCP, "%s: LayerDown.\n", fp->link->name);
437   if (ccp->in.state != NULL) {
438     (*algorithm[ccp->in.algorithm]->i.Term)(ccp->in.state);
439     ccp->in.state = NULL;
440     ccp->in.algorithm = -1;
441   }
442   if (ccp->out.state != NULL) {
443     (*algorithm[ccp->out.algorithm]->o.Term)(ccp->out.state);
444     ccp->out.state = NULL;
445     ccp->out.algorithm = -1;
446   }
447   ccp->his_reject = ccp->my_reject = 0;
448 
449   while (ccp->out.opt) {
450     next = ccp->out.opt->next;
451     free(ccp->out.opt);
452     ccp->out.opt = next;
453   }
454   ccp_Setup(ccp);
455 }
456 
457 static void
458 CcpLayerFinish(struct fsm *fp)
459 {
460   /* We're now down */
461   struct ccp *ccp = fsm2ccp(fp);
462   struct ccp_opt *next;
463 
464   log_Printf(LogCCP, "%s: LayerFinish.\n", fp->link->name);
465 
466   /*
467    * Nuke options that may be left over from sending a REQ but never
468    * coming up.
469    */
470   while (ccp->out.opt) {
471     next = ccp->out.opt->next;
472     free(ccp->out.opt);
473     ccp->out.opt = next;
474   }
475 
476   if (ccp_Required(ccp)) {
477     if (fp->link->lcp.fsm.state == ST_OPENED)
478       log_Printf(LogLCP, "%s: Closing due to CCP completion\n", fp->link->name);
479     fsm_Close(&fp->link->lcp.fsm);
480   }
481 }
482 
483 /*  Called when CCP has reached the OPEN state */
484 static int
485 CcpLayerUp(struct fsm *fp)
486 {
487   /* We're now up */
488   struct ccp *ccp = fsm2ccp(fp);
489   struct ccp_opt **o;
490   int f, fail;
491 
492   for (f = fail = 0; f < NALGORITHMS; f++)
493     if (IsEnabled(ccp->cfg.neg[algorithm[f]->Neg]) &&
494         (*algorithm[f]->Required)(&ccp->fsm) &&
495         (ccp->in.algorithm != f || ccp->out.algorithm != f)) {
496       /* Blow it all away - we haven't negotiated a required algorithm */
497       log_Printf(LogWARN, "%s: Failed to negotiate (required) %s\n",
498                  fp->link->name, protoname(algorithm[f]->id));
499       fail = 1;
500     }
501 
502   if (fail) {
503     ccp->his_proto = ccp->my_proto = -1;
504     fsm_Close(fp);
505     fsm_Close(&fp->link->lcp.fsm);
506     return 0;
507   }
508 
509   log_Printf(LogCCP, "%s: LayerUp.\n", fp->link->name);
510 
511   if (ccp->in.state == NULL && ccp->in.algorithm >= 0 &&
512       ccp->in.algorithm < NALGORITHMS) {
513     ccp->in.state = (*algorithm[ccp->in.algorithm]->i.Init)(&ccp->in.opt);
514     if (ccp->in.state == NULL) {
515       log_Printf(LogERROR, "%s: %s (in) initialisation failure\n",
516                 fp->link->name, protoname(ccp->his_proto));
517       ccp->his_proto = ccp->my_proto = -1;
518       fsm_Close(fp);
519       return 0;
520     }
521   }
522 
523   o = &ccp->out.opt;
524   for (f = 0; f < ccp->out.algorithm; f++)
525     if (IsEnabled(ccp->cfg.neg[algorithm[f]->Neg]))
526       o = &(*o)->next;
527 
528   if (ccp->out.state == NULL && ccp->out.algorithm >= 0 &&
529       ccp->out.algorithm < NALGORITHMS) {
530     ccp->out.state = (*algorithm[ccp->out.algorithm]->o.Init)(&(*o)->val);
531     if (ccp->out.state == NULL) {
532       log_Printf(LogERROR, "%s: %s (out) initialisation failure\n",
533                 fp->link->name, protoname(ccp->my_proto));
534       ccp->his_proto = ccp->my_proto = -1;
535       fsm_Close(fp);
536       return 0;
537     }
538   }
539 
540   fp->more.reqs = fp->more.naks = fp->more.rejs = ccp->cfg.fsm.maxreq * 3;
541 
542   log_Printf(LogCCP, "%s: Out = %s[%d], In = %s[%d]\n",
543             fp->link->name, protoname(ccp->my_proto), ccp->my_proto,
544             protoname(ccp->his_proto), ccp->his_proto);
545 
546   return 1;
547 }
548 
549 static void
550 CcpDecodeConfig(struct fsm *fp, u_char *cp, int plen, int mode_type,
551                 struct fsm_decode *dec)
552 {
553   /* Deal with incoming data */
554   struct ccp *ccp = fsm2ccp(fp);
555   int type, length, f;
556   const char *end;
557 
558   if (mode_type == MODE_REQ)
559     ccp->in.algorithm = -1;	/* In case we've received two REQs in a row */
560 
561   while (plen >= sizeof(struct fsmconfig)) {
562     type = *cp;
563     length = cp[1];
564 
565     if (length == 0) {
566       log_Printf(LogCCP, "%s: CCP size zero\n", fp->link->name);
567       break;
568     }
569 
570     if (length > sizeof(struct lcp_opt)) {
571       length = sizeof(struct lcp_opt);
572       log_Printf(LogCCP, "%s: Warning: Truncating length to %d\n",
573                 fp->link->name, length);
574     }
575 
576     for (f = NALGORITHMS-1; f > -1; f--)
577       if (algorithm[f]->id == type)
578         break;
579 
580     end = f == -1 ? "" : (*algorithm[f]->Disp)((struct lcp_opt *)cp);
581     if (end == NULL)
582       end = "";
583 
584     log_Printf(LogCCP, " %s[%d] %s\n", protoname(type), length, end);
585 
586     if (f == -1) {
587       /* Don't understand that :-( */
588       if (mode_type == MODE_REQ) {
589         ccp->my_reject |= (1 << type);
590         memcpy(dec->rejend, cp, length);
591         dec->rejend += length;
592       }
593     } else {
594       struct ccp_opt *o;
595 
596       switch (mode_type) {
597       case MODE_REQ:
598 	if (IsAccepted(ccp->cfg.neg[algorithm[f]->Neg]) &&
599             (*algorithm[f]->Usable)(fp) &&
600             ccp->in.algorithm == -1) {
601 	  memcpy(&ccp->in.opt, cp, length);
602           switch ((*algorithm[f]->i.Set)(&ccp->in.opt, &ccp->cfg)) {
603           case MODE_REJ:
604 	    memcpy(dec->rejend, &ccp->in.opt, ccp->in.opt.len);
605 	    dec->rejend += ccp->in.opt.len;
606             break;
607           case MODE_NAK:
608 	    memcpy(dec->nakend, &ccp->in.opt, ccp->in.opt.len);
609 	    dec->nakend += ccp->in.opt.len;
610             break;
611           case MODE_ACK:
612 	    memcpy(dec->ackend, cp, length);
613 	    dec->ackend += length;
614 	    ccp->his_proto = type;
615             ccp->in.algorithm = f;		/* This one'll do :-) */
616             break;
617           }
618 	} else {
619 	  memcpy(dec->rejend, cp, length);
620 	  dec->rejend += length;
621 	}
622 	break;
623       case MODE_NAK:
624         for (o = ccp->out.opt; o != NULL; o = o->next)
625           if (o->val.id == cp[0])
626             break;
627         if (o == NULL)
628           log_Printf(LogCCP, "%s: Warning: Ignoring peer NAK of unsent"
629                      " option\n", fp->link->name);
630         else {
631 	  memcpy(&o->val, cp, length);
632           if ((*algorithm[f]->o.Set)(&o->val, &ccp->cfg) == MODE_ACK)
633             ccp->my_proto = algorithm[f]->id;
634           else {
635 	    ccp->his_reject |= (1 << type);
636 	    ccp->my_proto = -1;
637             if (algorithm[f]->Required(fp)) {
638               log_Printf(LogWARN, "%s: Cannot understand peers (required)"
639                          " %s negotiation\n", fp->link->name,
640                          protoname(algorithm[f]->id));
641               fsm_Close(&fp->link->lcp.fsm);
642             }
643           }
644         }
645         break;
646       case MODE_REJ:
647 	ccp->his_reject |= (1 << type);
648 	ccp->my_proto = -1;
649         if (algorithm[f]->Required(fp)) {
650           log_Printf(LogWARN, "%s: Peer rejected (required) %s negotiation\n",
651                      fp->link->name, protoname(algorithm[f]->id));
652           fsm_Close(&fp->link->lcp.fsm);
653         }
654 	break;
655       }
656     }
657 
658     plen -= cp[1];
659     cp += cp[1];
660   }
661 
662   if (mode_type != MODE_NOP) {
663     if (dec->rejend != dec->rej) {
664       /* rejects are preferred */
665       dec->ackend = dec->ack;
666       dec->nakend = dec->nak;
667       if (ccp->in.state == NULL) {
668         ccp->his_proto = -1;
669         ccp->in.algorithm = -1;
670       }
671     } else if (dec->nakend != dec->nak) {
672       /* then NAKs */
673       dec->ackend = dec->ack;
674       if (ccp->in.state == NULL) {
675         ccp->his_proto = -1;
676         ccp->in.algorithm = -1;
677       }
678     }
679   }
680 }
681 
682 extern struct mbuf *
683 ccp_Input(struct bundle *bundle, struct link *l, struct mbuf *bp)
684 {
685   /* Got PROTO_CCP from link */
686   m_settype(bp, MB_CCPIN);
687   if (bundle_Phase(bundle) == PHASE_NETWORK)
688     fsm_Input(&l->ccp.fsm, bp);
689   else {
690     if (bundle_Phase(bundle) < PHASE_NETWORK)
691       log_Printf(LogCCP, "%s: Error: Unexpected CCP in phase %s (ignored)\n",
692                  l->ccp.fsm.link->name, bundle_PhaseName(bundle));
693     m_freem(bp);
694   }
695   return NULL;
696 }
697 
698 static void
699 CcpRecvResetAck(struct fsm *fp, u_char id)
700 {
701   /* Got a reset ACK, reset incoming dictionary */
702   struct ccp *ccp = fsm2ccp(fp);
703 
704   if (ccp->reset_sent != -1) {
705     if (id != ccp->reset_sent) {
706       log_Printf(LogCCP, "%s: Incorrect ResetAck (id %d, not %d)"
707                 " ignored\n", fp->link->name, id, ccp->reset_sent);
708       return;
709     }
710     /* Whaddaya know - a correct reset ack */
711   } else if (id == ccp->last_reset)
712     log_Printf(LogCCP, "%s: Duplicate ResetAck (resetting again)\n",
713                fp->link->name);
714   else {
715     log_Printf(LogCCP, "%s: Unexpected ResetAck (id %d) ignored\n",
716                fp->link->name, id);
717     return;
718   }
719 
720   ccp->last_reset = ccp->reset_sent;
721   ccp->reset_sent = -1;
722   if (ccp->in.state != NULL)
723     (*algorithm[ccp->in.algorithm]->i.Reset)(ccp->in.state);
724 }
725 
726 static struct mbuf *
727 ccp_LayerPush(struct bundle *b, struct link *l, struct mbuf *bp,
728               int pri, u_short *proto)
729 {
730   if (PROTO_COMPRESSIBLE(*proto)) {
731     if (l->ccp.fsm.state != ST_OPENED) {
732       if (ccp_Required(&l->ccp)) {
733         /* The NCP layer shouldn't have let this happen ! */
734         log_Printf(LogERROR, "%s: Unexpected attempt to use an unopened and"
735                    " required CCP layer\n", l->name);
736         m_freem(bp);
737         bp = NULL;
738       }
739     } else if (l->ccp.out.state != NULL) {
740       bp = (*algorithm[l->ccp.out.algorithm]->o.Write)
741              (l->ccp.out.state, &l->ccp, l, pri, proto, bp);
742       switch (*proto) {
743         case PROTO_ICOMPD:
744           m_settype(bp, MB_ICOMPDOUT);
745           break;
746         case PROTO_COMPD:
747           m_settype(bp, MB_COMPDOUT);
748           break;
749       }
750     }
751   }
752 
753   return bp;
754 }
755 
756 static struct mbuf *
757 ccp_LayerPull(struct bundle *b, struct link *l, struct mbuf *bp, u_short *proto)
758 {
759   /*
760    * If proto isn't PROTO_[I]COMPD, we still want to pass it to the
761    * decompression routines so that the dictionary's updated
762    */
763   if (l->ccp.fsm.state == ST_OPENED) {
764     if (*proto == PROTO_COMPD || *proto == PROTO_ICOMPD) {
765       log_Printf(LogDEBUG, "ccp_LayerPull: PROTO_%sCOMPDP -> PROTO_IP\n",
766                  *proto == PROTO_ICOMPD ? "I" : "");
767       /* Decompress incoming data */
768       if (l->ccp.reset_sent != -1)
769         /* Send another REQ and put the packet in the bit bucket */
770         fsm_Output(&l->ccp.fsm, CODE_RESETREQ, l->ccp.reset_sent, NULL, 0,
771                    MB_CCPOUT);
772       else if (l->ccp.in.state != NULL) {
773         bp = (*algorithm[l->ccp.in.algorithm]->i.Read)
774                (l->ccp.in.state, &l->ccp, proto, bp);
775         switch (*proto) {
776           case PROTO_ICOMPD:
777             m_settype(bp, MB_ICOMPDIN);
778             break;
779           case PROTO_COMPD:
780             m_settype(bp, MB_COMPDIN);
781             break;
782         }
783         return bp;
784       }
785       m_freem(bp);
786       bp = NULL;
787     } else if (PROTO_COMPRESSIBLE(*proto) && l->ccp.in.state != NULL) {
788       log_Printf(LogDEBUG, "ccp_LayerPull: Ignore packet (dict only)\n");
789       /* Add incoming Network Layer traffic to our dictionary */
790       (*algorithm[l->ccp.in.algorithm]->i.DictSetup)
791         (l->ccp.in.state, &l->ccp, *proto, bp);
792     } else
793       log_Printf(LogDEBUG, "ccp_LayerPull: Ignore packet\n");
794   }
795 
796   return bp;
797 }
798 
799 u_short
800 ccp_Proto(struct ccp *ccp)
801 {
802   return !link2physical(ccp->fsm.link) || !ccp->fsm.bundle->ncp.mp.active ?
803          PROTO_COMPD : PROTO_ICOMPD;
804 }
805 
806 int
807 ccp_SetOpenMode(struct ccp *ccp)
808 {
809   int f;
810 
811   for (f = 0; f < CCP_NEG_TOTAL; f++)
812     if (IsEnabled(ccp->cfg.neg[f])) {
813       ccp->fsm.open_mode = 0;
814       return 1;
815     }
816 
817   ccp->fsm.open_mode = OPEN_PASSIVE;	/* Go straight to ST_STOPPED ? */
818 
819   for (f = 0; f < CCP_NEG_TOTAL; f++)
820     if (IsAccepted(ccp->cfg.neg[f]))
821       return 1;
822 
823   return 0;				/* No CCP at all */
824 }
825 
826 int
827 ccp_DefaultUsable(struct fsm *fp)
828 {
829   return 1;
830 }
831 
832 int
833 ccp_DefaultRequired(struct fsm *fp)
834 {
835   return 0;
836 }
837 
838 struct layer ccplayer = { LAYER_CCP, "ccp", ccp_LayerPush, ccp_LayerPull };
839