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