resource manager: add optional resource id.
[dragonfly.git] / usr.sbin / ppp / ccp.c
blob991a43a13f3de781b931f09574fb4da7f06f306a
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 $
29 * $DragonFly: src/usr.sbin/ppp/ccp.c,v 1.2 2003/06/17 04:30:00 dillon Exp $
32 #include <sys/param.h>
33 #include <netinet/in.h>
34 #include <netinet/in_systm.h>
35 #include <netinet/ip.h>
36 #include <sys/socket.h>
37 #include <sys/un.h>
39 #include <stdarg.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h> /* memcpy() on some archs */
43 #include <termios.h>
45 #include "layer.h"
46 #include "defs.h"
47 #include "command.h"
48 #include "mbuf.h"
49 #include "log.h"
50 #include "timer.h"
51 #include "fsm.h"
52 #include "proto.h"
53 #include "pred.h"
54 #include "deflate.h"
55 #include "throughput.h"
56 #include "iplist.h"
57 #include "slcompress.h"
58 #include "lqr.h"
59 #include "hdlc.h"
60 #include "lcp.h"
61 #include "ccp.h"
62 #include "ncpaddr.h"
63 #include "ipcp.h"
64 #include "filter.h"
65 #include "descriptor.h"
66 #include "prompt.h"
67 #include "link.h"
68 #include "mp.h"
69 #include "async.h"
70 #include "physical.h"
71 #ifndef NORADIUS
72 #include "radius.h"
73 #endif
74 #ifndef NODES
75 #include "mppe.h"
76 #endif
77 #include "ipv6cp.h"
78 #include "ncp.h"
79 #include "bundle.h"
81 static void CcpSendConfigReq(struct fsm *);
82 static void CcpSentTerminateReq(struct fsm *);
83 static void CcpSendTerminateAck(struct fsm *, u_char);
84 static void CcpDecodeConfig(struct fsm *, u_char *, u_char *, int,
85 struct fsm_decode *);
86 static void CcpLayerStart(struct fsm *);
87 static void CcpLayerFinish(struct fsm *);
88 static int CcpLayerUp(struct fsm *);
89 static void CcpLayerDown(struct fsm *);
90 static void CcpInitRestartCounter(struct fsm *, int);
91 static int CcpRecvResetReq(struct fsm *);
92 static void CcpRecvResetAck(struct fsm *, u_char);
94 static struct fsm_callbacks ccp_Callbacks = {
95 CcpLayerUp,
96 CcpLayerDown,
97 CcpLayerStart,
98 CcpLayerFinish,
99 CcpInitRestartCounter,
100 CcpSendConfigReq,
101 CcpSentTerminateReq,
102 CcpSendTerminateAck,
103 CcpDecodeConfig,
104 CcpRecvResetReq,
105 CcpRecvResetAck
108 static const char * const ccp_TimerNames[] =
109 {"CCP restart", "CCP openmode", "CCP stopped"};
111 static const char *
112 protoname(int proto)
114 static char const * const cftypes[] = {
115 /* Check out the latest ``Compression Control Protocol'' rfc (1962) */
116 "OUI", /* 0: OUI */
117 "PRED1", /* 1: Predictor type 1 */
118 "PRED2", /* 2: Predictor type 2 */
119 "PUDDLE", /* 3: Puddle Jumber */
120 NULL, NULL, NULL, NULL, NULL, NULL,
121 NULL, NULL, NULL, NULL, NULL, NULL,
122 "HWPPC", /* 16: Hewlett-Packard PPC */
123 "STAC", /* 17: Stac Electronics LZS (rfc1974) */
124 "MPPE", /* 18: Microsoft PPC (rfc2118) and */
125 /* Microsoft PPE (draft-ietf-pppext-mppe) */
126 "GAND", /* 19: Gandalf FZA (rfc1993) */
127 "V42BIS", /* 20: ARG->DATA.42bis compression */
128 "BSD", /* 21: BSD LZW Compress */
129 NULL,
130 "LZS-DCP", /* 23: LZS-DCP Compression Protocol (rfc1967) */
131 "MAGNALINK/DEFLATE",/* 24: Magnalink Variable Resource (rfc1975) */
132 /* 24: Deflate (according to pppd-2.3.*) */
133 "DCE", /* 25: Data Circuit-Terminating Equip (rfc1976) */
134 "DEFLATE", /* 26: Deflate (rfc1979) */
137 if (proto < 0 || proto > sizeof cftypes / sizeof *cftypes ||
138 cftypes[proto] == NULL) {
139 if (proto == -1)
140 return "none";
141 return HexStr(proto, NULL, 0);
144 return cftypes[proto];
147 /* We support these algorithms, and Req them in the given order */
148 static const struct ccp_algorithm * const algorithm[] = {
149 &DeflateAlgorithm,
150 &Pred1Algorithm,
151 &PppdDeflateAlgorithm
152 #ifndef NODES
153 , &MPPEAlgorithm
154 #endif
157 #define NALGORITHMS (sizeof algorithm/sizeof algorithm[0])
160 ccp_ReportStatus(struct cmdargs const *arg)
162 struct ccp_opt **o;
163 struct link *l;
164 struct ccp *ccp;
165 int f;
167 l = command_ChooseLink(arg);
168 ccp = &l->ccp;
170 prompt_Printf(arg->prompt, "%s: %s [%s]\n", l->name, ccp->fsm.name,
171 State2Nam(ccp->fsm.state));
172 if (ccp->fsm.state == ST_OPENED) {
173 prompt_Printf(arg->prompt, " My protocol = %s, His protocol = %s\n",
174 protoname(ccp->my_proto), protoname(ccp->his_proto));
175 prompt_Printf(arg->prompt, " Output: %ld --> %ld, Input: %ld --> %ld\n",
176 ccp->uncompout, ccp->compout,
177 ccp->compin, ccp->uncompin);
180 if (ccp->in.algorithm != -1)
181 prompt_Printf(arg->prompt, "\n Input Options: %s\n",
182 (*algorithm[ccp->in.algorithm]->Disp)(&ccp->in.opt));
184 if (ccp->out.algorithm != -1) {
185 o = &ccp->out.opt;
186 for (f = 0; f < ccp->out.algorithm; f++)
187 if (IsEnabled(ccp->cfg.neg[algorithm[f]->Neg]))
188 o = &(*o)->next;
189 prompt_Printf(arg->prompt, " Output Options: %s\n",
190 (*algorithm[ccp->out.algorithm]->Disp)(&(*o)->val));
193 prompt_Printf(arg->prompt, "\n Defaults: ");
194 prompt_Printf(arg->prompt, "FSM retry = %us, max %u Config"
195 " REQ%s, %u Term REQ%s\n", ccp->cfg.fsm.timeout,
196 ccp->cfg.fsm.maxreq, ccp->cfg.fsm.maxreq == 1 ? "" : "s",
197 ccp->cfg.fsm.maxtrm, ccp->cfg.fsm.maxtrm == 1 ? "" : "s");
198 prompt_Printf(arg->prompt, " deflate windows: ");
199 prompt_Printf(arg->prompt, "incoming = %d, ", ccp->cfg.deflate.in.winsize);
200 prompt_Printf(arg->prompt, "outgoing = %d\n", ccp->cfg.deflate.out.winsize);
201 #ifndef NODES
202 prompt_Printf(arg->prompt, " MPPE: ");
203 if (ccp->cfg.mppe.keybits)
204 prompt_Printf(arg->prompt, "%d bits, ", ccp->cfg.mppe.keybits);
205 else
206 prompt_Printf(arg->prompt, "any bits, ");
207 switch (ccp->cfg.mppe.state) {
208 case MPPE_STATEFUL:
209 prompt_Printf(arg->prompt, "stateful");
210 break;
211 case MPPE_STATELESS:
212 prompt_Printf(arg->prompt, "stateless");
213 break;
214 case MPPE_ANYSTATE:
215 prompt_Printf(arg->prompt, "any state");
216 break;
218 prompt_Printf(arg->prompt, "%s\n",
219 ccp->cfg.mppe.required ? ", required" : "");
220 #endif
222 prompt_Printf(arg->prompt, "\n DEFLATE: %s\n",
223 command_ShowNegval(ccp->cfg.neg[CCP_NEG_DEFLATE]));
224 prompt_Printf(arg->prompt, " PREDICTOR1: %s\n",
225 command_ShowNegval(ccp->cfg.neg[CCP_NEG_PRED1]));
226 prompt_Printf(arg->prompt, " DEFLATE24: %s\n",
227 command_ShowNegval(ccp->cfg.neg[CCP_NEG_DEFLATE24]));
228 #ifndef NODES
229 prompt_Printf(arg->prompt, " MPPE: %s\n",
230 command_ShowNegval(ccp->cfg.neg[CCP_NEG_MPPE]));
231 #endif
232 return 0;
235 void
236 ccp_SetupCallbacks(struct ccp *ccp)
238 ccp->fsm.fn = &ccp_Callbacks;
239 ccp->fsm.FsmTimer.name = ccp_TimerNames[0];
240 ccp->fsm.OpenTimer.name = ccp_TimerNames[1];
241 ccp->fsm.StoppedTimer.name = ccp_TimerNames[2];
244 void
245 ccp_Init(struct ccp *ccp, struct bundle *bundle, struct link *l,
246 const struct fsm_parent *parent)
248 /* Initialise ourselves */
250 fsm_Init(&ccp->fsm, "CCP", PROTO_CCP, 1, CCP_MAXCODE, LogCCP,
251 bundle, l, parent, &ccp_Callbacks, ccp_TimerNames);
253 ccp->cfg.deflate.in.winsize = 0;
254 ccp->cfg.deflate.out.winsize = 15;
255 ccp->cfg.fsm.timeout = DEF_FSMRETRY;
256 ccp->cfg.fsm.maxreq = DEF_FSMTRIES;
257 ccp->cfg.fsm.maxtrm = DEF_FSMTRIES;
258 ccp->cfg.neg[CCP_NEG_DEFLATE] = NEG_ENABLED|NEG_ACCEPTED;
259 ccp->cfg.neg[CCP_NEG_PRED1] = NEG_ENABLED|NEG_ACCEPTED;
260 ccp->cfg.neg[CCP_NEG_DEFLATE24] = 0;
261 #ifndef NODES
262 ccp->cfg.mppe.keybits = 0;
263 ccp->cfg.mppe.state = MPPE_ANYSTATE;
264 ccp->cfg.mppe.required = 0;
265 ccp->cfg.neg[CCP_NEG_MPPE] = NEG_ENABLED|NEG_ACCEPTED;
266 #endif
268 ccp_Setup(ccp);
271 void
272 ccp_Setup(struct ccp *ccp)
274 /* Set ourselves up for a startup */
275 ccp->fsm.open_mode = 0;
276 ccp->his_proto = ccp->my_proto = -1;
277 ccp->reset_sent = ccp->last_reset = -1;
278 ccp->in.algorithm = ccp->out.algorithm = -1;
279 ccp->in.state = ccp->out.state = NULL;
280 ccp->in.opt.hdr.id = -1;
281 ccp->out.opt = NULL;
282 ccp->his_reject = ccp->my_reject = 0;
283 ccp->uncompout = ccp->compout = 0;
284 ccp->uncompin = ccp->compin = 0;
288 * Is ccp *REQUIRED* ?
289 * We ask each of the configured ccp protocols if they're required and
290 * return TRUE if they are.
292 * It's not possible for the peer to reject a required ccp protocol
293 * without our state machine bringing the supporting lcp layer down.
295 * If ccp is required but not open, the NCP layer should not push
296 * any data into the link.
299 ccp_Required(struct ccp *ccp)
301 int f;
303 for (f = 0; f < NALGORITHMS; f++)
304 if (IsEnabled(ccp->cfg.neg[algorithm[f]->Neg]) &&
305 (*algorithm[f]->Required)(&ccp->fsm))
306 return 1;
308 return 0;
312 * Report whether it's possible to increase a packet's size after
313 * compression (and by how much).
316 ccp_MTUOverhead(struct ccp *ccp)
318 if (ccp->fsm.state == ST_OPENED && ccp->out.algorithm >= 0)
319 return algorithm[ccp->out.algorithm]->o.MTUOverhead;
321 return 0;
324 static void
325 CcpInitRestartCounter(struct fsm *fp, int what)
327 /* Set fsm timer load */
328 struct ccp *ccp = fsm2ccp(fp);
330 fp->FsmTimer.load = ccp->cfg.fsm.timeout * SECTICKS;
331 switch (what) {
332 case FSM_REQ_TIMER:
333 fp->restart = ccp->cfg.fsm.maxreq;
334 break;
335 case FSM_TRM_TIMER:
336 fp->restart = ccp->cfg.fsm.maxtrm;
337 break;
338 default:
339 fp->restart = 1;
340 break;
344 static void
345 CcpSendConfigReq(struct fsm *fp)
347 /* Send config REQ please */
348 struct ccp *ccp = fsm2ccp(fp);
349 struct ccp_opt **o;
350 u_char *cp, buff[100];
351 int f, 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 == 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)
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 int 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 != f || ccp->out.algorithm != 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 < 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 for (f = 0; f < ccp->out.algorithm; f++)
535 if (IsEnabled(ccp->cfg.neg[algorithm[f]->Neg]))
536 o = &(*o)->next;
538 if (ccp->out.state == NULL && ccp->out.algorithm >= 0 &&
539 ccp->out.algorithm < NALGORITHMS) {
540 ccp->out.state = (*algorithm[ccp->out.algorithm]->o.Init)
541 (fp->bundle, &(*o)->val);
542 if (ccp->out.state == NULL) {
543 log_Printf(LogERROR, "%s: %s (out) initialisation failure\n",
544 fp->link->name, protoname(ccp->my_proto));
545 ccp->his_proto = ccp->my_proto = -1;
546 fsm_Close(fp);
547 return 0;
551 fp->more.reqs = fp->more.naks = fp->more.rejs = ccp->cfg.fsm.maxreq * 3;
553 log_Printf(LogCCP, "%s: Out = %s[%d], In = %s[%d]\n",
554 fp->link->name, protoname(ccp->my_proto), ccp->my_proto,
555 protoname(ccp->his_proto), ccp->his_proto);
557 return 1;
560 static void
561 CcpDecodeConfig(struct fsm *fp, u_char *cp, u_char *end, int mode_type,
562 struct fsm_decode *dec)
564 /* Deal with incoming data */
565 struct ccp *ccp = fsm2ccp(fp);
566 int f;
567 const char *disp;
568 struct fsm_opt *opt;
570 if (mode_type == MODE_REQ)
571 ccp->in.algorithm = -1; /* In case we've received two REQs in a row */
573 while (end - cp >= sizeof(opt->hdr)) {
574 if ((opt = fsm_readopt(&cp)) == NULL)
575 break;
577 for (f = NALGORITHMS-1; f > -1; f--)
578 if (algorithm[f]->id == opt->hdr.id)
579 break;
581 disp = f == -1 ? "" : (*algorithm[f]->Disp)(opt);
582 if (disp == NULL)
583 disp = "";
585 log_Printf(LogCCP, " %s[%d] %s\n", protoname(opt->hdr.id),
586 opt->hdr.len, disp);
588 if (f == -1) {
589 /* Don't understand that :-( */
590 if (mode_type == MODE_REQ) {
591 ccp->my_reject |= (1 << opt->hdr.id);
592 fsm_rej(dec, opt);
594 } else {
595 struct ccp_opt *o;
597 switch (mode_type) {
598 case MODE_REQ:
599 if (IsAccepted(ccp->cfg.neg[algorithm[f]->Neg]) &&
600 (*algorithm[f]->Usable)(fp) &&
601 ccp->in.algorithm == -1) {
602 memcpy(&ccp->in.opt, opt, opt->hdr.len);
603 switch ((*algorithm[f]->i.Set)(fp->bundle, &ccp->in.opt, &ccp->cfg)) {
604 case MODE_REJ:
605 fsm_rej(dec, &ccp->in.opt);
606 break;
607 case MODE_NAK:
608 fsm_nak(dec, &ccp->in.opt);
609 break;
610 case MODE_ACK:
611 fsm_ack(dec, &ccp->in.opt);
612 ccp->his_proto = opt->hdr.id;
613 ccp->in.algorithm = f; /* This one'll do :-) */
614 break;
616 } else {
617 fsm_rej(dec, opt);
619 break;
620 case MODE_NAK:
621 for (o = ccp->out.opt; o != NULL; o = o->next)
622 if (o->val.hdr.id == opt->hdr.id)
623 break;
624 if (o == NULL)
625 log_Printf(LogCCP, "%s: Warning: Ignoring peer NAK of unsent"
626 " option\n", fp->link->name);
627 else {
628 memcpy(&o->val, opt, opt->hdr.len);
629 if ((*algorithm[f]->o.Set)(fp->bundle, &o->val, &ccp->cfg) ==
630 MODE_ACK)
631 ccp->my_proto = algorithm[f]->id;
632 else {
633 ccp->his_reject |= (1 << opt->hdr.id);
634 ccp->my_proto = -1;
635 if (algorithm[f]->Required(fp)) {
636 log_Printf(LogWARN, "%s: Cannot understand peers (required)"
637 " %s negotiation\n", fp->link->name,
638 protoname(algorithm[f]->id));
639 fsm_Close(&fp->link->lcp.fsm);
643 break;
644 case MODE_REJ:
645 ccp->his_reject |= (1 << opt->hdr.id);
646 ccp->my_proto = -1;
647 if (algorithm[f]->Required(fp)) {
648 log_Printf(LogWARN, "%s: Peer rejected (required) %s negotiation\n",
649 fp->link->name, protoname(algorithm[f]->id));
650 fsm_Close(&fp->link->lcp.fsm);
652 break;
657 if (mode_type != MODE_NOP) {
658 fsm_opt_normalise(dec);
659 if (dec->rejend != dec->rej || dec->nakend != dec->nak) {
660 if (ccp->in.state == NULL) {
661 ccp->his_proto = -1;
662 ccp->in.algorithm = -1;
668 extern struct mbuf *
669 ccp_Input(struct bundle *bundle, struct link *l, struct mbuf *bp)
671 /* Got PROTO_CCP from link */
672 m_settype(bp, MB_CCPIN);
673 if (bundle_Phase(bundle) == PHASE_NETWORK)
674 fsm_Input(&l->ccp.fsm, bp);
675 else {
676 if (bundle_Phase(bundle) < PHASE_NETWORK)
677 log_Printf(LogCCP, "%s: Error: Unexpected CCP in phase %s (ignored)\n",
678 l->ccp.fsm.link->name, bundle_PhaseName(bundle));
679 m_freem(bp);
681 return NULL;
684 static void
685 CcpRecvResetAck(struct fsm *fp, u_char id)
687 /* Got a reset ACK, reset incoming dictionary */
688 struct ccp *ccp = fsm2ccp(fp);
690 if (ccp->reset_sent != -1) {
691 if (id != ccp->reset_sent) {
692 log_Printf(LogCCP, "%s: Incorrect ResetAck (id %d, not %d)"
693 " ignored\n", fp->link->name, id, ccp->reset_sent);
694 return;
696 /* Whaddaya know - a correct reset ack */
697 } else if (id == ccp->last_reset)
698 log_Printf(LogCCP, "%s: Duplicate ResetAck (resetting again)\n",
699 fp->link->name);
700 else {
701 log_Printf(LogCCP, "%s: Unexpected ResetAck (id %d) ignored\n",
702 fp->link->name, id);
703 return;
706 ccp->last_reset = ccp->reset_sent;
707 ccp->reset_sent = -1;
708 if (ccp->in.state != NULL)
709 (*algorithm[ccp->in.algorithm]->i.Reset)(ccp->in.state);
712 static struct mbuf *
713 ccp_LayerPush(struct bundle *b, struct link *l, struct mbuf *bp,
714 int pri, u_short *proto)
716 if (PROTO_COMPRESSIBLE(*proto)) {
717 if (l->ccp.fsm.state != ST_OPENED) {
718 if (ccp_Required(&l->ccp)) {
719 /* The NCP layer shouldn't have let this happen ! */
720 log_Printf(LogERROR, "%s: Unexpected attempt to use an unopened and"
721 " required CCP layer\n", l->name);
722 m_freem(bp);
723 bp = NULL;
725 } else if (l->ccp.out.state != NULL) {
726 bp = (*algorithm[l->ccp.out.algorithm]->o.Write)
727 (l->ccp.out.state, &l->ccp, l, pri, proto, bp);
728 switch (*proto) {
729 case PROTO_ICOMPD:
730 m_settype(bp, MB_ICOMPDOUT);
731 break;
732 case PROTO_COMPD:
733 m_settype(bp, MB_COMPDOUT);
734 break;
739 return bp;
742 static struct mbuf *
743 ccp_LayerPull(struct bundle *b, struct link *l, struct mbuf *bp, u_short *proto)
746 * If proto isn't PROTO_[I]COMPD, we still want to pass it to the
747 * decompression routines so that the dictionary's updated
749 if (l->ccp.fsm.state == ST_OPENED) {
750 if (*proto == PROTO_COMPD || *proto == PROTO_ICOMPD) {
751 /* Decompress incoming data */
752 if (l->ccp.reset_sent != -1)
753 /* Send another REQ and put the packet in the bit bucket */
754 fsm_Output(&l->ccp.fsm, CODE_RESETREQ, l->ccp.reset_sent, NULL, 0,
755 MB_CCPOUT);
756 else if (l->ccp.in.state != NULL) {
757 bp = (*algorithm[l->ccp.in.algorithm]->i.Read)
758 (l->ccp.in.state, &l->ccp, proto, bp);
759 switch (*proto) {
760 case PROTO_ICOMPD:
761 m_settype(bp, MB_ICOMPDIN);
762 break;
763 case PROTO_COMPD:
764 m_settype(bp, MB_COMPDIN);
765 break;
767 return bp;
769 m_freem(bp);
770 bp = NULL;
771 } else if (PROTO_COMPRESSIBLE(*proto) && l->ccp.in.state != NULL) {
772 /* Add incoming Network Layer traffic to our dictionary */
773 (*algorithm[l->ccp.in.algorithm]->i.DictSetup)
774 (l->ccp.in.state, &l->ccp, *proto, bp);
778 return bp;
781 u_short
782 ccp_Proto(struct ccp *ccp)
784 return !link2physical(ccp->fsm.link) || !ccp->fsm.bundle->ncp.mp.active ?
785 PROTO_COMPD : PROTO_ICOMPD;
789 ccp_SetOpenMode(struct ccp *ccp)
791 int f;
793 for (f = 0; f < CCP_NEG_TOTAL; f++)
794 if (IsEnabled(ccp->cfg.neg[f])) {
795 ccp->fsm.open_mode = 0;
796 return 1;
799 ccp->fsm.open_mode = OPEN_PASSIVE; /* Go straight to ST_STOPPED ? */
801 for (f = 0; f < CCP_NEG_TOTAL; f++)
802 if (IsAccepted(ccp->cfg.neg[f]))
803 return 1;
805 return 0; /* No CCP at all */
809 ccp_DefaultUsable(struct fsm *fp)
811 return 1;
815 ccp_DefaultRequired(struct fsm *fp)
817 return 0;
820 struct layer ccplayer = { LAYER_CCP, "ccp", ccp_LayerPush, ccp_LayerPull };