mkinitrd.sh: Fix comment typo.
[dragonfly.git] / usr.sbin / ppp / ccp.c
blob74255b15a771f47f67f51dad147a30b12d2325a2
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.
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.
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.
28 * $FreeBSD: src/usr.sbin/ppp/ccp.c,v 1.54.2.7 2002/09/01 02:12:22 brian Exp $
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>
38 #include <stdarg.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h> /* memcpy() on some archs */
42 #include <termios.h>
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"
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);
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
107 static const char * const ccp_TimerNames[] =
108 {"CCP restart", "CCP openmode", "CCP stopped"};
110 static const char *
111 protoname(int proto)
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) */
136 if (proto < 0 || (unsigned)proto > NELEM(cftypes) ||
137 cftypes[proto] == NULL) {
138 if (proto == -1)
139 return "none";
140 return HexStr(proto, NULL, 0);
143 return cftypes[proto];
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
156 #define NALGORITHMS NELEM(algorithm)
159 ccp_ReportStatus(struct cmdargs const *arg)
161 struct ccp_opt **o;
162 struct link *l;
163 struct ccp *ccp;
164 int f;
166 l = command_ChooseLink(arg);
167 ccp = &l->ccp;
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);
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));
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));
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;
217 prompt_Printf(arg->prompt, "%s\n",
218 ccp->cfg.mppe.required ? ", required" : "");
219 #endif
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;
234 void
235 ccp_SetupCallbacks(struct ccp *ccp)
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];
243 void
244 ccp_Init(struct ccp *ccp, struct bundle *bundle, struct link *l,
245 const struct fsm_parent *parent)
247 /* Initialise ourselves */
249 fsm_Init(&ccp->fsm, "CCP", PROTO_CCP, 1, CCP_MAXCODE, LogCCP,
250 bundle, l, parent, &ccp_Callbacks, ccp_TimerNames);
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
267 ccp_Setup(ccp);
270 void
271 ccp_Setup(struct ccp *ccp)
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;
287 * Is ccp *REQUIRED* ?
288 * We ask each of the configured ccp protocols if they're required and
289 * return TRUE if they are.
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.
294 * If ccp is required but not open, the NCP layer should not push
295 * any data into the link.
298 ccp_Required(struct ccp *ccp)
300 unsigned f;
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;
307 return 0;
311 * Report whether it's possible to increase a packet's size after
312 * compression (and by how much).
315 ccp_MTUOverhead(struct ccp *ccp)
317 if (ccp->fsm.state == ST_OPENED && ccp->out.algorithm >= 0)
318 return algorithm[ccp->out.algorithm]->o.MTUOverhead;
320 return 0;
323 static void
324 CcpInitRestartCounter(struct fsm *fp, int what)
326 /* Set fsm timer load */
327 struct ccp *ccp = fsm2ccp(fp);
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;
343 static void
344 CcpSendConfigReq(struct fsm *fp)
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;
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)) {
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;
368 if (alloc || *o == NULL) {
369 *o = (struct ccp_opt *)malloc(sizeof(struct ccp_opt));
370 (*o)->val.hdr.id = algorithm[f]->id;
371 (*o)->val.hdr.len = 2;
372 (*o)->next = NULL;
373 (*o)->algorithm = f;
374 (*algorithm[f]->o.OptInit)(fp->bundle, &(*o)->val, &ccp->cfg);
377 if (cp + (*o)->val.hdr.len > buff + sizeof buff) {
378 log_Printf(LogERROR, "%s: CCP REQ buffer overrun !\n", fp->link->name);
379 break;
381 memcpy(cp, &(*o)->val, (*o)->val.hdr.len);
382 cp += (*o)->val.hdr.len;
384 ccp->my_proto = (*o)->val.hdr.id;
385 ccp->out.algorithm = f;
387 if (alloc)
388 o = &(*o)->next;
391 fsm_Output(fp, CODE_CONFIGREQ, fp->reqid, buff, cp - buff, MB_CCPOUT);
394 void
395 ccp_SendResetReq(struct fsm *fp)
397 /* We can't read our input - ask peer to reset */
398 struct ccp *ccp = fsm2ccp(fp);
400 ccp->reset_sent = fp->reqid;
401 ccp->last_reset = -1;
402 fsm_Output(fp, CODE_RESETREQ, fp->reqid, NULL, 0, MB_CCPOUT);
405 static void
406 CcpSentTerminateReq(struct fsm *fp __unused)
408 /* Term REQ just sent by FSM */
411 static void
412 CcpSendTerminateAck(struct fsm *fp, u_char id)
414 /* Send Term ACK please */
415 fsm_Output(fp, CODE_TERMACK, id, NULL, 0, MB_CCPOUT);
418 static int
419 CcpRecvResetReq(struct fsm *fp)
421 /* Got a reset REQ, reset outgoing dictionary */
422 struct ccp *ccp = fsm2ccp(fp);
423 if (ccp->out.state == NULL)
424 return 1;
425 return (*algorithm[ccp->out.algorithm]->o.Reset)(ccp->out.state);
428 static void
429 CcpLayerStart(struct fsm *fp)
431 /* We're about to start up ! */
432 struct ccp *ccp = fsm2ccp(fp);
434 log_Printf(LogCCP, "%s: LayerStart.\n", fp->link->name);
435 fp->more.reqs = fp->more.naks = fp->more.rejs = ccp->cfg.fsm.maxreq * 3;
438 static void
439 CcpLayerDown(struct fsm *fp)
441 /* About to come down */
442 struct ccp *ccp = fsm2ccp(fp);
443 struct ccp_opt *next;
445 log_Printf(LogCCP, "%s: LayerDown.\n", fp->link->name);
446 if (ccp->in.state != NULL) {
447 (*algorithm[ccp->in.algorithm]->i.Term)(ccp->in.state);
448 ccp->in.state = NULL;
449 ccp->in.algorithm = -1;
451 if (ccp->out.state != NULL) {
452 (*algorithm[ccp->out.algorithm]->o.Term)(ccp->out.state);
453 ccp->out.state = NULL;
454 ccp->out.algorithm = -1;
456 ccp->his_reject = ccp->my_reject = 0;
458 while (ccp->out.opt) {
459 next = ccp->out.opt->next;
460 free(ccp->out.opt);
461 ccp->out.opt = next;
463 ccp_Setup(ccp);
466 static void
467 CcpLayerFinish(struct fsm *fp)
469 /* We're now down */
470 struct ccp *ccp = fsm2ccp(fp);
471 struct ccp_opt *next;
473 log_Printf(LogCCP, "%s: LayerFinish.\n", fp->link->name);
476 * Nuke options that may be left over from sending a REQ but never
477 * coming up.
479 while (ccp->out.opt) {
480 next = ccp->out.opt->next;
481 free(ccp->out.opt);
482 ccp->out.opt = next;
485 if (ccp_Required(ccp)) {
486 if (fp->link->lcp.fsm.state == ST_OPENED)
487 log_Printf(LogLCP, "%s: Closing due to CCP completion\n", fp->link->name);
488 fsm_Close(&fp->link->lcp.fsm);
492 /* Called when CCP has reached the OPEN state */
493 static int
494 CcpLayerUp(struct fsm *fp)
496 /* We're now up */
497 struct ccp *ccp = fsm2ccp(fp);
498 struct ccp_opt **o;
499 unsigned f, fail;
501 for (f = fail = 0; f < NALGORITHMS; f++)
502 if (IsEnabled(ccp->cfg.neg[algorithm[f]->Neg]) &&
503 (*algorithm[f]->Required)(&ccp->fsm) &&
504 (ccp->in.algorithm != (int)f || ccp->out.algorithm != (int)f)) {
505 /* Blow it all away - we haven't negotiated a required algorithm */
506 log_Printf(LogWARN, "%s: Failed to negotiate (required) %s\n",
507 fp->link->name, protoname(algorithm[f]->id));
508 fail = 1;
511 if (fail) {
512 ccp->his_proto = ccp->my_proto = -1;
513 fsm_Close(fp);
514 fsm_Close(&fp->link->lcp.fsm);
515 return 0;
518 log_Printf(LogCCP, "%s: LayerUp.\n", fp->link->name);
520 if (ccp->in.state == NULL && ccp->in.algorithm >= 0 &&
521 ccp->in.algorithm < (int)NALGORITHMS) {
522 ccp->in.state = (*algorithm[ccp->in.algorithm]->i.Init)
523 (fp->bundle, &ccp->in.opt);
524 if (ccp->in.state == NULL) {
525 log_Printf(LogERROR, "%s: %s (in) initialisation failure\n",
526 fp->link->name, protoname(ccp->his_proto));
527 ccp->his_proto = ccp->my_proto = -1;
528 fsm_Close(fp);
529 return 0;
533 o = &ccp->out.opt;
534 if (ccp->out.algorithm > 0)
535 for (f = 0; f < (unsigned)ccp->out.algorithm; f++)
536 if (IsEnabled(ccp->cfg.neg[algorithm[f]->Neg]))
537 o = &(*o)->next;
539 if (ccp->out.state == NULL && ccp->out.algorithm >= 0 &&
540 ccp->out.algorithm < (int)NALGORITHMS) {
541 ccp->out.state = (*algorithm[ccp->out.algorithm]->o.Init)
542 (fp->bundle, &(*o)->val);
543 if (ccp->out.state == NULL) {
544 log_Printf(LogERROR, "%s: %s (out) initialisation failure\n",
545 fp->link->name, protoname(ccp->my_proto));
546 ccp->his_proto = ccp->my_proto = -1;
547 fsm_Close(fp);
548 return 0;
552 fp->more.reqs = fp->more.naks = fp->more.rejs = ccp->cfg.fsm.maxreq * 3;
554 log_Printf(LogCCP, "%s: Out = %s[%d], In = %s[%d]\n",
555 fp->link->name, protoname(ccp->my_proto), ccp->my_proto,
556 protoname(ccp->his_proto), ccp->his_proto);
558 return 1;
561 static void
562 CcpDecodeConfig(struct fsm *fp, u_char *cp, u_char *end, int mode_type,
563 struct fsm_decode *dec)
565 /* Deal with incoming data */
566 struct ccp *ccp = fsm2ccp(fp);
567 int f;
568 const char *disp;
569 struct fsm_opt *opt;
571 if (mode_type == MODE_REQ)
572 ccp->in.algorithm = -1; /* In case we've received two REQs in a row */
574 while (end >= cp + sizeof(opt->hdr)) {
575 if ((opt = fsm_readopt(&cp)) == NULL)
576 break;
578 for (f = NALGORITHMS-1; f > -1; f--)
579 if (algorithm[f]->id == opt->hdr.id)
580 break;
582 disp = f == -1 ? "" : (*algorithm[f]->Disp)(opt);
583 if (disp == NULL)
584 disp = "";
586 log_Printf(LogCCP, " %s[%d] %s\n", protoname(opt->hdr.id),
587 opt->hdr.len, disp);
589 if (f == -1) {
590 /* Don't understand that :-( */
591 if (mode_type == MODE_REQ) {
592 ccp->my_reject |= (1 << opt->hdr.id);
593 fsm_rej(dec, opt);
595 } else {
596 struct ccp_opt *o;
598 switch (mode_type) {
599 case MODE_REQ:
600 if (IsAccepted(ccp->cfg.neg[algorithm[f]->Neg]) &&
601 (*algorithm[f]->Usable)(fp) &&
602 ccp->in.algorithm == -1) {
603 memcpy(&ccp->in.opt, opt, opt->hdr.len);
604 switch ((*algorithm[f]->i.Set)(fp->bundle, &ccp->in.opt, &ccp->cfg)) {
605 case MODE_REJ:
606 fsm_rej(dec, &ccp->in.opt);
607 break;
608 case MODE_NAK:
609 fsm_nak(dec, &ccp->in.opt);
610 break;
611 case MODE_ACK:
612 fsm_ack(dec, &ccp->in.opt);
613 ccp->his_proto = opt->hdr.id;
614 ccp->in.algorithm = (int)f; /* This one'll do :-) */
615 break;
617 } else {
618 fsm_rej(dec, opt);
620 break;
621 case MODE_NAK:
622 for (o = ccp->out.opt; o != NULL; o = o->next)
623 if (o->val.hdr.id == opt->hdr.id)
624 break;
625 if (o == NULL)
626 log_Printf(LogCCP, "%s: Warning: Ignoring peer NAK of unsent"
627 " option\n", fp->link->name);
628 else {
629 memcpy(&o->val, opt, opt->hdr.len);
630 if ((*algorithm[f]->o.Set)(fp->bundle, &o->val, &ccp->cfg) ==
631 MODE_ACK)
632 ccp->my_proto = algorithm[f]->id;
633 else {
634 ccp->his_reject |= (1 << opt->hdr.id);
635 ccp->my_proto = -1;
636 if (algorithm[f]->Required(fp)) {
637 log_Printf(LogWARN, "%s: Cannot understand peers (required)"
638 " %s negotiation\n", fp->link->name,
639 protoname(algorithm[f]->id));
640 fsm_Close(&fp->link->lcp.fsm);
644 break;
645 case MODE_REJ:
646 ccp->his_reject |= (1 << opt->hdr.id);
647 ccp->my_proto = -1;
648 if (algorithm[f]->Required(fp)) {
649 log_Printf(LogWARN, "%s: Peer rejected (required) %s negotiation\n",
650 fp->link->name, protoname(algorithm[f]->id));
651 fsm_Close(&fp->link->lcp.fsm);
653 break;
658 if (mode_type != MODE_NOP) {
659 fsm_opt_normalise(dec);
660 if (dec->rejend != dec->rej || dec->nakend != dec->nak) {
661 if (ccp->in.state == NULL) {
662 ccp->his_proto = -1;
663 ccp->in.algorithm = -1;
669 extern struct mbuf *
670 ccp_Input(struct bundle *bundle, struct link *l, struct mbuf *bp)
672 /* Got PROTO_CCP from link */
673 m_settype(bp, MB_CCPIN);
674 if (bundle_Phase(bundle) == PHASE_NETWORK)
675 fsm_Input(&l->ccp.fsm, bp);
676 else {
677 if (bundle_Phase(bundle) < PHASE_NETWORK)
678 log_Printf(LogCCP, "%s: Error: Unexpected CCP in phase %s (ignored)\n",
679 l->ccp.fsm.link->name, bundle_PhaseName(bundle));
680 m_freem(bp);
682 return NULL;
685 static void
686 CcpRecvResetAck(struct fsm *fp, u_char id)
688 /* Got a reset ACK, reset incoming dictionary */
689 struct ccp *ccp = fsm2ccp(fp);
691 if (ccp->reset_sent != -1) {
692 if (id != ccp->reset_sent) {
693 log_Printf(LogCCP, "%s: Incorrect ResetAck (id %d, not %d)"
694 " ignored\n", fp->link->name, id, ccp->reset_sent);
695 return;
697 /* Whaddaya know - a correct reset ack */
698 } else if (id == ccp->last_reset)
699 log_Printf(LogCCP, "%s: Duplicate ResetAck (resetting again)\n",
700 fp->link->name);
701 else {
702 log_Printf(LogCCP, "%s: Unexpected ResetAck (id %d) ignored\n",
703 fp->link->name, id);
704 return;
707 ccp->last_reset = ccp->reset_sent;
708 ccp->reset_sent = -1;
709 if (ccp->in.state != NULL)
710 (*algorithm[ccp->in.algorithm]->i.Reset)(ccp->in.state);
713 static struct mbuf *
714 ccp_LayerPush(struct bundle *b __unused, struct link *l, struct mbuf *bp,
715 int pri, u_short *proto)
717 if (PROTO_COMPRESSIBLE(*proto)) {
718 if (l->ccp.fsm.state != ST_OPENED) {
719 if (ccp_Required(&l->ccp)) {
720 /* The NCP layer shouldn't have let this happen ! */
721 log_Printf(LogERROR, "%s: Unexpected attempt to use an unopened and"
722 " required CCP layer\n", l->name);
723 m_freem(bp);
724 bp = NULL;
726 } else if (l->ccp.out.state != NULL) {
727 bp = (*algorithm[l->ccp.out.algorithm]->o.Write)
728 (l->ccp.out.state, &l->ccp, l, pri, proto, bp);
729 switch (*proto) {
730 case PROTO_ICOMPD:
731 m_settype(bp, MB_ICOMPDOUT);
732 break;
733 case PROTO_COMPD:
734 m_settype(bp, MB_COMPDOUT);
735 break;
740 return bp;
743 static struct mbuf *
744 ccp_LayerPull(struct bundle *b __unused, struct link *l, struct mbuf *bp,
745 u_short *proto)
748 * If proto isn't PROTO_[I]COMPD, we still want to pass it to the
749 * decompression routines so that the dictionary's updated
751 if (l->ccp.fsm.state == ST_OPENED) {
752 if (*proto == PROTO_COMPD || *proto == PROTO_ICOMPD) {
753 /* Decompress incoming data */
754 if (l->ccp.reset_sent != -1)
755 /* Send another REQ and put the packet in the bit bucket */
756 fsm_Output(&l->ccp.fsm, CODE_RESETREQ, l->ccp.reset_sent, NULL, 0,
757 MB_CCPOUT);
758 else if (l->ccp.in.state != NULL) {
759 bp = (*algorithm[l->ccp.in.algorithm]->i.Read)
760 (l->ccp.in.state, &l->ccp, proto, bp);
761 switch (*proto) {
762 case PROTO_ICOMPD:
763 m_settype(bp, MB_ICOMPDIN);
764 break;
765 case PROTO_COMPD:
766 m_settype(bp, MB_COMPDIN);
767 break;
769 return bp;
771 m_freem(bp);
772 bp = NULL;
773 } else if (PROTO_COMPRESSIBLE(*proto) && l->ccp.in.state != NULL) {
774 /* Add incoming Network Layer traffic to our dictionary */
775 (*algorithm[l->ccp.in.algorithm]->i.DictSetup)
776 (l->ccp.in.state, &l->ccp, *proto, bp);
780 return bp;
783 u_short
784 ccp_Proto(struct ccp *ccp)
786 return !link2physical(ccp->fsm.link) || !ccp->fsm.bundle->ncp.mp.active ?
787 PROTO_COMPD : PROTO_ICOMPD;
791 ccp_SetOpenMode(struct ccp *ccp)
793 int f;
795 for (f = 0; f < CCP_NEG_TOTAL; f++)
796 if (IsEnabled(ccp->cfg.neg[f])) {
797 ccp->fsm.open_mode = 0;
798 return 1;
801 ccp->fsm.open_mode = OPEN_PASSIVE; /* Go straight to ST_STOPPED ? */
803 for (f = 0; f < CCP_NEG_TOTAL; f++)
804 if (IsAccepted(ccp->cfg.neg[f]))
805 return 1;
807 return 0; /* No CCP at all */
811 ccp_DefaultUsable(struct fsm *fp __unused)
813 return 1;
817 ccp_DefaultRequired(struct fsm *fp __unused)
819 return 0;
822 struct layer ccplayer = { LAYER_CCP, "ccp", ccp_LayerPush, ccp_LayerPull };