sdhci - Implement ADMA2 transfer support. Keep SDMA support for now.
[dragonfly.git] / usr.sbin / ppp / ccp.c
blob2b9606140d17d801aac755b3a1ab95ac447e240e
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 || (unsigned)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 unsigned 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 unsigned f;
352 int alloc;
354 cp = buff;
355 o = &ccp->out.opt;
356 alloc = ccp->his_reject == 0 && ccp->out.opt == NULL;
357 ccp->my_proto = -1;
358 ccp->out.algorithm = -1;
359 for (f = 0; f < NALGORITHMS; f++)
360 if (IsEnabled(ccp->cfg.neg[algorithm[f]->Neg]) &&
361 !REJECTED(ccp, algorithm[f]->id) &&
362 (*algorithm[f]->Usable)(fp)) {
364 if (!alloc)
365 for (o = &ccp->out.opt; *o != NULL; o = &(*o)->next)
366 if ((*o)->val.hdr.id == algorithm[f]->id && (*o)->algorithm == (int)f)
367 break;
369 if (alloc || *o == NULL) {
370 *o = (struct ccp_opt *)malloc(sizeof(struct ccp_opt));
371 (*o)->val.hdr.id = algorithm[f]->id;
372 (*o)->val.hdr.len = 2;
373 (*o)->next = NULL;
374 (*o)->algorithm = f;
375 (*algorithm[f]->o.OptInit)(fp->bundle, &(*o)->val, &ccp->cfg);
378 if (cp + (*o)->val.hdr.len > buff + sizeof buff) {
379 log_Printf(LogERROR, "%s: CCP REQ buffer overrun !\n", fp->link->name);
380 break;
382 memcpy(cp, &(*o)->val, (*o)->val.hdr.len);
383 cp += (*o)->val.hdr.len;
385 ccp->my_proto = (*o)->val.hdr.id;
386 ccp->out.algorithm = f;
388 if (alloc)
389 o = &(*o)->next;
392 fsm_Output(fp, CODE_CONFIGREQ, fp->reqid, buff, cp - buff, MB_CCPOUT);
395 void
396 ccp_SendResetReq(struct fsm *fp)
398 /* We can't read our input - ask peer to reset */
399 struct ccp *ccp = fsm2ccp(fp);
401 ccp->reset_sent = fp->reqid;
402 ccp->last_reset = -1;
403 fsm_Output(fp, CODE_RESETREQ, fp->reqid, NULL, 0, MB_CCPOUT);
406 static void
407 CcpSentTerminateReq(struct fsm *fp __unused)
409 /* Term REQ just sent by FSM */
412 static void
413 CcpSendTerminateAck(struct fsm *fp, u_char id)
415 /* Send Term ACK please */
416 fsm_Output(fp, CODE_TERMACK, id, NULL, 0, MB_CCPOUT);
419 static int
420 CcpRecvResetReq(struct fsm *fp)
422 /* Got a reset REQ, reset outgoing dictionary */
423 struct ccp *ccp = fsm2ccp(fp);
424 if (ccp->out.state == NULL)
425 return 1;
426 return (*algorithm[ccp->out.algorithm]->o.Reset)(ccp->out.state);
429 static void
430 CcpLayerStart(struct fsm *fp)
432 /* We're about to start up ! */
433 struct ccp *ccp = fsm2ccp(fp);
435 log_Printf(LogCCP, "%s: LayerStart.\n", fp->link->name);
436 fp->more.reqs = fp->more.naks = fp->more.rejs = ccp->cfg.fsm.maxreq * 3;
439 static void
440 CcpLayerDown(struct fsm *fp)
442 /* About to come down */
443 struct ccp *ccp = fsm2ccp(fp);
444 struct ccp_opt *next;
446 log_Printf(LogCCP, "%s: LayerDown.\n", fp->link->name);
447 if (ccp->in.state != NULL) {
448 (*algorithm[ccp->in.algorithm]->i.Term)(ccp->in.state);
449 ccp->in.state = NULL;
450 ccp->in.algorithm = -1;
452 if (ccp->out.state != NULL) {
453 (*algorithm[ccp->out.algorithm]->o.Term)(ccp->out.state);
454 ccp->out.state = NULL;
455 ccp->out.algorithm = -1;
457 ccp->his_reject = ccp->my_reject = 0;
459 while (ccp->out.opt) {
460 next = ccp->out.opt->next;
461 free(ccp->out.opt);
462 ccp->out.opt = next;
464 ccp_Setup(ccp);
467 static void
468 CcpLayerFinish(struct fsm *fp)
470 /* We're now down */
471 struct ccp *ccp = fsm2ccp(fp);
472 struct ccp_opt *next;
474 log_Printf(LogCCP, "%s: LayerFinish.\n", fp->link->name);
477 * Nuke options that may be left over from sending a REQ but never
478 * coming up.
480 while (ccp->out.opt) {
481 next = ccp->out.opt->next;
482 free(ccp->out.opt);
483 ccp->out.opt = next;
486 if (ccp_Required(ccp)) {
487 if (fp->link->lcp.fsm.state == ST_OPENED)
488 log_Printf(LogLCP, "%s: Closing due to CCP completion\n", fp->link->name);
489 fsm_Close(&fp->link->lcp.fsm);
493 /* Called when CCP has reached the OPEN state */
494 static int
495 CcpLayerUp(struct fsm *fp)
497 /* We're now up */
498 struct ccp *ccp = fsm2ccp(fp);
499 struct ccp_opt **o;
500 unsigned f, fail;
502 for (f = fail = 0; f < NALGORITHMS; f++)
503 if (IsEnabled(ccp->cfg.neg[algorithm[f]->Neg]) &&
504 (*algorithm[f]->Required)(&ccp->fsm) &&
505 (ccp->in.algorithm != (int)f || ccp->out.algorithm != (int)f)) {
506 /* Blow it all away - we haven't negotiated a required algorithm */
507 log_Printf(LogWARN, "%s: Failed to negotiate (required) %s\n",
508 fp->link->name, protoname(algorithm[f]->id));
509 fail = 1;
512 if (fail) {
513 ccp->his_proto = ccp->my_proto = -1;
514 fsm_Close(fp);
515 fsm_Close(&fp->link->lcp.fsm);
516 return 0;
519 log_Printf(LogCCP, "%s: LayerUp.\n", fp->link->name);
521 if (ccp->in.state == NULL && ccp->in.algorithm >= 0 &&
522 ccp->in.algorithm < (int)NALGORITHMS) {
523 ccp->in.state = (*algorithm[ccp->in.algorithm]->i.Init)
524 (fp->bundle, &ccp->in.opt);
525 if (ccp->in.state == NULL) {
526 log_Printf(LogERROR, "%s: %s (in) initialisation failure\n",
527 fp->link->name, protoname(ccp->his_proto));
528 ccp->his_proto = ccp->my_proto = -1;
529 fsm_Close(fp);
530 return 0;
534 o = &ccp->out.opt;
535 if (ccp->out.algorithm > 0)
536 for (f = 0; f < (unsigned)ccp->out.algorithm; f++)
537 if (IsEnabled(ccp->cfg.neg[algorithm[f]->Neg]))
538 o = &(*o)->next;
540 if (ccp->out.state == NULL && ccp->out.algorithm >= 0 &&
541 ccp->out.algorithm < (int)NALGORITHMS) {
542 ccp->out.state = (*algorithm[ccp->out.algorithm]->o.Init)
543 (fp->bundle, &(*o)->val);
544 if (ccp->out.state == NULL) {
545 log_Printf(LogERROR, "%s: %s (out) initialisation failure\n",
546 fp->link->name, protoname(ccp->my_proto));
547 ccp->his_proto = ccp->my_proto = -1;
548 fsm_Close(fp);
549 return 0;
553 fp->more.reqs = fp->more.naks = fp->more.rejs = ccp->cfg.fsm.maxreq * 3;
555 log_Printf(LogCCP, "%s: Out = %s[%d], In = %s[%d]\n",
556 fp->link->name, protoname(ccp->my_proto), ccp->my_proto,
557 protoname(ccp->his_proto), ccp->his_proto);
559 return 1;
562 static void
563 CcpDecodeConfig(struct fsm *fp, u_char *cp, u_char *end, int mode_type,
564 struct fsm_decode *dec)
566 /* Deal with incoming data */
567 struct ccp *ccp = fsm2ccp(fp);
568 int f;
569 const char *disp;
570 struct fsm_opt *opt;
572 if (mode_type == MODE_REQ)
573 ccp->in.algorithm = -1; /* In case we've received two REQs in a row */
575 while (end >= cp + sizeof(opt->hdr)) {
576 if ((opt = fsm_readopt(&cp)) == NULL)
577 break;
579 for (f = NALGORITHMS-1; f > -1; f--)
580 if (algorithm[f]->id == opt->hdr.id)
581 break;
583 disp = f == -1 ? "" : (*algorithm[f]->Disp)(opt);
584 if (disp == NULL)
585 disp = "";
587 log_Printf(LogCCP, " %s[%d] %s\n", protoname(opt->hdr.id),
588 opt->hdr.len, disp);
590 if (f == -1) {
591 /* Don't understand that :-( */
592 if (mode_type == MODE_REQ) {
593 ccp->my_reject |= (1 << opt->hdr.id);
594 fsm_rej(dec, opt);
596 } else {
597 struct ccp_opt *o;
599 switch (mode_type) {
600 case MODE_REQ:
601 if (IsAccepted(ccp->cfg.neg[algorithm[f]->Neg]) &&
602 (*algorithm[f]->Usable)(fp) &&
603 ccp->in.algorithm == -1) {
604 memcpy(&ccp->in.opt, opt, opt->hdr.len);
605 switch ((*algorithm[f]->i.Set)(fp->bundle, &ccp->in.opt, &ccp->cfg)) {
606 case MODE_REJ:
607 fsm_rej(dec, &ccp->in.opt);
608 break;
609 case MODE_NAK:
610 fsm_nak(dec, &ccp->in.opt);
611 break;
612 case MODE_ACK:
613 fsm_ack(dec, &ccp->in.opt);
614 ccp->his_proto = opt->hdr.id;
615 ccp->in.algorithm = (int)f; /* This one'll do :-) */
616 break;
618 } else {
619 fsm_rej(dec, opt);
621 break;
622 case MODE_NAK:
623 for (o = ccp->out.opt; o != NULL; o = o->next)
624 if (o->val.hdr.id == opt->hdr.id)
625 break;
626 if (o == NULL)
627 log_Printf(LogCCP, "%s: Warning: Ignoring peer NAK of unsent"
628 " option\n", fp->link->name);
629 else {
630 memcpy(&o->val, opt, opt->hdr.len);
631 if ((*algorithm[f]->o.Set)(fp->bundle, &o->val, &ccp->cfg) ==
632 MODE_ACK)
633 ccp->my_proto = algorithm[f]->id;
634 else {
635 ccp->his_reject |= (1 << opt->hdr.id);
636 ccp->my_proto = -1;
637 if (algorithm[f]->Required(fp)) {
638 log_Printf(LogWARN, "%s: Cannot understand peers (required)"
639 " %s negotiation\n", fp->link->name,
640 protoname(algorithm[f]->id));
641 fsm_Close(&fp->link->lcp.fsm);
645 break;
646 case MODE_REJ:
647 ccp->his_reject |= (1 << opt->hdr.id);
648 ccp->my_proto = -1;
649 if (algorithm[f]->Required(fp)) {
650 log_Printf(LogWARN, "%s: Peer rejected (required) %s negotiation\n",
651 fp->link->name, protoname(algorithm[f]->id));
652 fsm_Close(&fp->link->lcp.fsm);
654 break;
659 if (mode_type != MODE_NOP) {
660 fsm_opt_normalise(dec);
661 if (dec->rejend != dec->rej || dec->nakend != dec->nak) {
662 if (ccp->in.state == NULL) {
663 ccp->his_proto = -1;
664 ccp->in.algorithm = -1;
670 extern struct mbuf *
671 ccp_Input(struct bundle *bundle, struct link *l, struct mbuf *bp)
673 /* Got PROTO_CCP from link */
674 m_settype(bp, MB_CCPIN);
675 if (bundle_Phase(bundle) == PHASE_NETWORK)
676 fsm_Input(&l->ccp.fsm, bp);
677 else {
678 if (bundle_Phase(bundle) < PHASE_NETWORK)
679 log_Printf(LogCCP, "%s: Error: Unexpected CCP in phase %s (ignored)\n",
680 l->ccp.fsm.link->name, bundle_PhaseName(bundle));
681 m_freem(bp);
683 return NULL;
686 static void
687 CcpRecvResetAck(struct fsm *fp, u_char id)
689 /* Got a reset ACK, reset incoming dictionary */
690 struct ccp *ccp = fsm2ccp(fp);
692 if (ccp->reset_sent != -1) {
693 if (id != ccp->reset_sent) {
694 log_Printf(LogCCP, "%s: Incorrect ResetAck (id %d, not %d)"
695 " ignored\n", fp->link->name, id, ccp->reset_sent);
696 return;
698 /* Whaddaya know - a correct reset ack */
699 } else if (id == ccp->last_reset)
700 log_Printf(LogCCP, "%s: Duplicate ResetAck (resetting again)\n",
701 fp->link->name);
702 else {
703 log_Printf(LogCCP, "%s: Unexpected ResetAck (id %d) ignored\n",
704 fp->link->name, id);
705 return;
708 ccp->last_reset = ccp->reset_sent;
709 ccp->reset_sent = -1;
710 if (ccp->in.state != NULL)
711 (*algorithm[ccp->in.algorithm]->i.Reset)(ccp->in.state);
714 static struct mbuf *
715 ccp_LayerPush(struct bundle *b __unused, struct link *l, struct mbuf *bp,
716 int pri, u_short *proto)
718 if (PROTO_COMPRESSIBLE(*proto)) {
719 if (l->ccp.fsm.state != ST_OPENED) {
720 if (ccp_Required(&l->ccp)) {
721 /* The NCP layer shouldn't have let this happen ! */
722 log_Printf(LogERROR, "%s: Unexpected attempt to use an unopened and"
723 " required CCP layer\n", l->name);
724 m_freem(bp);
725 bp = NULL;
727 } else if (l->ccp.out.state != NULL) {
728 bp = (*algorithm[l->ccp.out.algorithm]->o.Write)
729 (l->ccp.out.state, &l->ccp, l, pri, proto, bp);
730 switch (*proto) {
731 case PROTO_ICOMPD:
732 m_settype(bp, MB_ICOMPDOUT);
733 break;
734 case PROTO_COMPD:
735 m_settype(bp, MB_COMPDOUT);
736 break;
741 return bp;
744 static struct mbuf *
745 ccp_LayerPull(struct bundle *b __unused, struct link *l, struct mbuf *bp,
746 u_short *proto)
749 * If proto isn't PROTO_[I]COMPD, we still want to pass it to the
750 * decompression routines so that the dictionary's updated
752 if (l->ccp.fsm.state == ST_OPENED) {
753 if (*proto == PROTO_COMPD || *proto == PROTO_ICOMPD) {
754 /* Decompress incoming data */
755 if (l->ccp.reset_sent != -1)
756 /* Send another REQ and put the packet in the bit bucket */
757 fsm_Output(&l->ccp.fsm, CODE_RESETREQ, l->ccp.reset_sent, NULL, 0,
758 MB_CCPOUT);
759 else if (l->ccp.in.state != NULL) {
760 bp = (*algorithm[l->ccp.in.algorithm]->i.Read)
761 (l->ccp.in.state, &l->ccp, proto, bp);
762 switch (*proto) {
763 case PROTO_ICOMPD:
764 m_settype(bp, MB_ICOMPDIN);
765 break;
766 case PROTO_COMPD:
767 m_settype(bp, MB_COMPDIN);
768 break;
770 return bp;
772 m_freem(bp);
773 bp = NULL;
774 } else if (PROTO_COMPRESSIBLE(*proto) && l->ccp.in.state != NULL) {
775 /* Add incoming Network Layer traffic to our dictionary */
776 (*algorithm[l->ccp.in.algorithm]->i.DictSetup)
777 (l->ccp.in.state, &l->ccp, *proto, bp);
781 return bp;
784 u_short
785 ccp_Proto(struct ccp *ccp)
787 return !link2physical(ccp->fsm.link) || !ccp->fsm.bundle->ncp.mp.active ?
788 PROTO_COMPD : PROTO_ICOMPD;
792 ccp_SetOpenMode(struct ccp *ccp)
794 int f;
796 for (f = 0; f < CCP_NEG_TOTAL; f++)
797 if (IsEnabled(ccp->cfg.neg[f])) {
798 ccp->fsm.open_mode = 0;
799 return 1;
802 ccp->fsm.open_mode = OPEN_PASSIVE; /* Go straight to ST_STOPPED ? */
804 for (f = 0; f < CCP_NEG_TOTAL; f++)
805 if (IsAccepted(ccp->cfg.neg[f]))
806 return 1;
808 return 0; /* No CCP at all */
812 ccp_DefaultUsable(struct fsm *fp __unused)
814 return 1;
818 ccp_DefaultRequired(struct fsm *fp __unused)
820 return 0;
823 struct layer ccplayer = { LAYER_CCP, "ccp", ccp_LayerPush, ccp_LayerPull };