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