2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1996 - 2001 Brian Somers <brian@Awfulhak.org>
5 * based on work by Toshiharu OHNO <tony-o@iij.ad.jp>
6 * Internet Initiative Japan, Inc (IIJ)
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/param.h>
34 #include <netinet/in.h>
35 #include <netinet/in_systm.h>
36 #include <netinet/ip.h>
37 #include <sys/socket.h>
53 #include "throughput.h"
54 #include "slcompress.h"
58 #include "descriptor.h"
73 static void FsmSendConfigReq(struct fsm *);
74 static void FsmSendTerminateReq(struct fsm *);
75 static void FsmInitRestartCounter(struct fsm *, int);
77 typedef void (recvfn)(struct fsm *, struct fsmheader *, struct mbuf *);
78 static recvfn FsmRecvConfigReq, FsmRecvConfigAck, FsmRecvConfigNak,
79 FsmRecvConfigRej, FsmRecvTermReq, FsmRecvTermAck,
80 FsmRecvCodeRej, FsmRecvProtoRej, FsmRecvEchoReq,
81 FsmRecvEchoRep, FsmRecvDiscReq, FsmRecvIdent,
82 FsmRecvTimeRemain, FsmRecvResetReq, FsmRecvResetAck;
84 static const struct fsmcodedesc {
86 unsigned check_reqid : 1;
87 unsigned inc_reqid : 1;
90 { FsmRecvConfigReq, 0, 0, "ConfigReq" },
91 { FsmRecvConfigAck, 1, 1, "ConfigAck" },
92 { FsmRecvConfigNak, 1, 1, "ConfigNak" },
93 { FsmRecvConfigRej, 1, 1, "ConfigRej" },
94 { FsmRecvTermReq, 0, 0, "TerminateReq" },
95 { FsmRecvTermAck, 1, 1, "TerminateAck" },
96 { FsmRecvCodeRej, 0, 0, "CodeRej" },
97 { FsmRecvProtoRej, 0, 0, "ProtocolRej" },
98 { FsmRecvEchoReq, 0, 0, "EchoRequest" },
99 { FsmRecvEchoRep, 0, 0, "EchoReply" },
100 { FsmRecvDiscReq, 0, 0, "DiscardReq" },
101 { FsmRecvIdent, 0, 1, "Ident" },
102 { FsmRecvTimeRemain,0, 0, "TimeRemain" },
103 { FsmRecvResetReq, 0, 0, "ResetReq" },
104 { FsmRecvResetAck, 0, 1, "ResetAck" }
110 if (code == 0 || code > sizeof FsmCodes / sizeof FsmCodes[0])
112 return FsmCodes[code-1].name;
116 State2Nam(u_int state)
118 static const char * const StateNames[] = {
119 "Initial", "Starting", "Closed", "Stopped", "Closing", "Stopping",
120 "Req-Sent", "Ack-Rcvd", "Ack-Sent", "Opened",
123 if (state >= sizeof StateNames / sizeof StateNames[0])
125 return StateNames[state];
129 StoppedTimeout(void *v)
131 struct fsm *fp = (struct fsm *)v;
133 log_Printf(fp->LogLevel, "%s: Stopped timer expired\n", fp->link->name);
134 if (fp->OpenTimer.state == TIMER_RUNNING) {
135 log_Printf(LogWARN, "%s: %s: aborting open delay due to stopped timer\n",
136 fp->link->name, fp->name);
137 timer_Stop(&fp->OpenTimer);
139 if (fp->state == ST_STOPPED)
144 fsm_Init(struct fsm *fp, const char *name, u_short proto, int mincode,
145 int maxcode, int LogLevel, struct bundle *bundle,
146 struct link *l, const struct fsm_parent *parent,
147 struct fsm_callbacks *fn, const char * const timer_names[3])
151 fp->min_code = mincode;
152 fp->max_code = maxcode;
153 fp->state = fp->min_code > CODE_TERMACK ? ST_OPENED : ST_INITIAL;
156 fp->more.reqs = fp->more.naks = fp->more.rejs = 3;
157 memset(&fp->FsmTimer, '\0', sizeof fp->FsmTimer);
158 memset(&fp->OpenTimer, '\0', sizeof fp->OpenTimer);
159 memset(&fp->StoppedTimer, '\0', sizeof fp->StoppedTimer);
160 fp->LogLevel = LogLevel;
165 fp->FsmTimer.name = timer_names[0];
166 fp->OpenTimer.name = timer_names[1];
167 fp->StoppedTimer.name = timer_names[2];
171 NewState(struct fsm *fp, int new)
173 log_Printf(fp->LogLevel, "%s: State change %s --> %s\n",
174 fp->link->name, State2Nam(fp->state), State2Nam(new));
175 if (fp->state == ST_STOPPED && fp->StoppedTimer.state == TIMER_RUNNING)
176 timer_Stop(&fp->StoppedTimer);
178 if ((new >= ST_INITIAL && new <= ST_STOPPED) || (new == ST_OPENED)) {
179 timer_Stop(&fp->FsmTimer);
180 if (new == ST_STOPPED && fp->StoppedTimer.load) {
181 timer_Stop(&fp->StoppedTimer);
182 fp->StoppedTimer.func = StoppedTimeout;
183 fp->StoppedTimer.arg = (void *) fp;
184 timer_Start(&fp->StoppedTimer);
190 fsm_Output(struct fsm *fp, u_int code, u_int id, u_char *ptr, unsigned count,
197 if (log_IsKept(fp->LogLevel)) {
198 log_Printf(fp->LogLevel, "%s: Send%s(%d) state = %s\n",
199 fp->link->name, Code2Nam(code), id, State2Nam(fp->state));
205 (*fp->fn->DecodeConfig)(fp, ptr, ptr + count, MODE_NOP, NULL);
206 if (count < sizeof(struct fsm_opt_hdr))
207 log_Printf(fp->LogLevel, " [EMPTY]\n");
212 plen = sizeof(struct fsmheader) + count;
215 lh.length = htons(plen);
216 bp = m_get(plen, mtype);
217 memcpy(MBUF_CTOP(bp), &lh, sizeof(struct fsmheader));
219 memcpy(MBUF_CTOP(bp) + sizeof(struct fsmheader), ptr, count);
220 log_DumpBp(LogDEBUG, "fsm_Output", bp);
221 link_PushPacket(fp->link, bp, fp->bundle, LINK_QUEUES(fp->link) - 1,
224 if (code == CODE_CONFIGREJ)
225 lcp_SendIdentification(&fp->link->lcp);
231 struct fsm *fp = (struct fsm *)v;
233 timer_Stop(&fp->OpenTimer);
234 if (fp->state <= ST_STOPPED) {
235 if (fp->state != ST_STARTING) {
237 * In practice, we're only here in ST_STOPPED (when delaying the
238 * first config request) or ST_CLOSED (when openmode == 0).
240 * The ST_STOPPED bit is breaking the RFC already :-(
242 * According to the RFC (1661) state transition table, a TLS isn't
243 * required for an Open event when state == Closed, but the RFC
244 * must be wrong as TLS hasn't yet been called (since the last TLF)
245 * ie, Initial gets an `Up' event, Closing gets a RTA etc.
247 (*fp->fn->LayerStart)(fp);
248 (*fp->parent->LayerStart)(fp->parent->object, fp);
250 FsmInitRestartCounter(fp, FSM_REQ_TIMER);
251 FsmSendConfigReq(fp);
252 NewState(fp, ST_REQSENT);
257 fsm_Open(struct fsm *fp)
261 NewState(fp, ST_STARTING);
262 (*fp->fn->LayerStart)(fp);
263 (*fp->parent->LayerStart)(fp->parent->object, fp);
266 if (fp->open_mode == OPEN_PASSIVE) {
267 NewState(fp, ST_STOPPED); /* XXX: This is a hack ! */
268 } else if (fp->open_mode > 0) {
269 if (fp->open_mode > 1)
270 log_Printf(LogPHASE, "%s: Entering STOPPED state for %d seconds\n",
271 fp->link->name, fp->open_mode);
272 NewState(fp, ST_STOPPED); /* XXX: This is a not-so-bad hack ! */
273 timer_Stop(&fp->OpenTimer);
274 fp->OpenTimer.load = fp->open_mode * SECTICKS;
275 fp->OpenTimer.func = FsmOpenNow;
276 fp->OpenTimer.arg = (void *)fp;
277 timer_Start(&fp->OpenTimer);
281 case ST_STOPPED: /* XXX: restart option */
285 case ST_OPENED: /* XXX: restart option */
287 case ST_CLOSING: /* XXX: restart option */
288 case ST_STOPPING: /* XXX: restart option */
289 NewState(fp, ST_STOPPING);
295 fsm_Up(struct fsm *fp)
299 log_Printf(fp->LogLevel, "FSM: Using \"%s\" as a transport\n",
301 NewState(fp, ST_CLOSED);
304 FsmInitRestartCounter(fp, FSM_REQ_TIMER);
305 FsmSendConfigReq(fp);
306 NewState(fp, ST_REQSENT);
309 log_Printf(fp->LogLevel, "%s: Oops, Up at %s\n",
310 fp->link->name, State2Nam(fp->state));
316 fsm_Down(struct fsm *fp)
320 NewState(fp, ST_INITIAL);
323 /* This TLF contradicts the RFC (1661), which ``misses it out'' ! */
324 (*fp->fn->LayerFinish)(fp);
325 NewState(fp, ST_INITIAL);
326 (*fp->parent->LayerFinish)(fp->parent->object, fp);
329 NewState(fp, ST_STARTING);
330 (*fp->fn->LayerStart)(fp);
331 (*fp->parent->LayerStart)(fp->parent->object, fp);
337 NewState(fp, ST_STARTING);
340 (*fp->fn->LayerDown)(fp);
341 NewState(fp, ST_STARTING);
342 (*fp->parent->LayerDown)(fp->parent->object, fp);
348 fsm_Close(struct fsm *fp)
352 (*fp->fn->LayerFinish)(fp);
353 NewState(fp, ST_INITIAL);
354 (*fp->parent->LayerFinish)(fp->parent->object, fp);
357 NewState(fp, ST_CLOSED);
360 NewState(fp, ST_CLOSING);
363 (*fp->fn->LayerDown)(fp);
364 if (fp->state == ST_OPENED) {
365 FsmInitRestartCounter(fp, FSM_TRM_TIMER);
366 FsmSendTerminateReq(fp);
367 NewState(fp, ST_CLOSING);
368 (*fp->parent->LayerDown)(fp->parent->object, fp);
374 FsmInitRestartCounter(fp, FSM_TRM_TIMER);
375 FsmSendTerminateReq(fp);
376 NewState(fp, ST_CLOSING);
385 FsmSendConfigReq(struct fsm *fp)
387 if (fp->more.reqs-- > 0 && fp->restart-- > 0) {
388 (*fp->fn->SendConfigReq)(fp);
389 timer_Start(&fp->FsmTimer); /* Start restart timer */
391 if (fp->more.reqs < 0)
392 log_Printf(LogPHASE, "%s: Too many %s REQs sent - abandoning "
393 "negotiation\n", fp->link->name, fp->name);
394 lcp_SendIdentification(&fp->link->lcp);
400 FsmSendTerminateReq(struct fsm *fp)
402 fsm_Output(fp, CODE_TERMREQ, fp->reqid, NULL, 0, MB_UNKNOWN);
403 (*fp->fn->SentTerminateReq)(fp);
404 timer_Start(&fp->FsmTimer); /* Start restart timer */
405 fp->restart--; /* Decrement restart counter */
414 struct fsm *fp = (struct fsm *)v;
420 FsmSendTerminateReq(fp);
424 FsmSendConfigReq(fp);
427 FsmSendConfigReq(fp);
428 NewState(fp, ST_REQSENT);
431 timer_Start(&fp->FsmTimer);
435 (*fp->fn->LayerFinish)(fp);
436 NewState(fp, ST_CLOSED);
437 (*fp->parent->LayerFinish)(fp->parent->object, fp);
440 (*fp->fn->LayerFinish)(fp);
441 NewState(fp, ST_STOPPED);
442 (*fp->parent->LayerFinish)(fp->parent->object, fp);
444 case ST_REQSENT: /* XXX: 3p */
447 (*fp->fn->LayerFinish)(fp);
448 NewState(fp, ST_STOPPED);
449 (*fp->parent->LayerFinish)(fp->parent->object, fp);
456 FsmInitRestartCounter(struct fsm *fp, int what)
458 timer_Stop(&fp->FsmTimer);
459 fp->FsmTimer.func = FsmTimeout;
460 fp->FsmTimer.arg = (void *)fp;
461 (*fp->fn->InitRestartCounter)(fp, what);
465 * Actions when receive packets
468 FsmRecvConfigReq(struct fsm *fp, struct fsmheader *lhp, struct mbuf *bp)
471 struct fsm_decode dec;
478 flen = ntohs(lhp->length) - sizeof *lhp;
480 log_Printf(LogWARN, "%s: FsmRecvConfigReq: plen (%d) < flen (%d)\n",
481 fp->link->name, plen, flen);
486 /* Some things must be done before we Decode the packet */
489 (*fp->fn->LayerDown)(fp);
492 dec.ackend = dec.ack;
493 dec.nakend = dec.nak;
494 dec.rejend = dec.rej;
496 (*fp->fn->DecodeConfig)(fp, cp, cp + flen, MODE_REQ, &dec);
497 if (flen < (int)sizeof(struct fsm_opt_hdr))
498 log_Printf(fp->LogLevel, " [EMPTY]\n");
500 if (dec.nakend == dec.nak && dec.rejend == dec.rej)
503 /* Check and process easy case */
506 if (fp->proto == PROTO_CCP && fp->link->lcp.fsm.state == ST_OPENED) {
508 * ccp_SetOpenMode() leaves us in initial if we're disabling
509 * & denying everything.
511 bp = m_prepend(bp, lhp, sizeof *lhp, 2);
512 bp = proto_Prepend(bp, fp->proto, 0, 0);
514 lcp_SendProtoRej(&fp->link->lcp, MBUF_CTOP(bp), bp->m_len);
520 log_Printf(fp->LogLevel, "%s: Oops, RCR in %s.\n",
521 fp->link->name, State2Nam(fp->state));
525 (*fp->fn->SendTerminateAck)(fp, lhp->id);
529 log_Printf(fp->LogLevel, "%s: Error: Got ConfigReq while state = %s\n",
530 fp->link->name, State2Nam(fp->state));
535 FsmInitRestartCounter(fp, FSM_REQ_TIMER);
538 FsmSendConfigReq(fp);
542 if (dec.rejend != dec.rej)
543 fsm_Output(fp, CODE_CONFIGREJ, lhp->id, dec.rej, dec.rejend - dec.rej,
545 if (dec.nakend != dec.nak)
546 fsm_Output(fp, CODE_CONFIGNAK, lhp->id, dec.nak, dec.nakend - dec.nak,
549 fsm_Output(fp, CODE_CONFIGACK, lhp->id, dec.ack, dec.ackend - dec.ack,
555 * According to the RFC (1661) state transition table, a TLS isn't
556 * required for a RCR when state == ST_STOPPED, but the RFC
557 * must be wrong as TLS hasn't yet been called (since the last TLF)
559 (*fp->fn->LayerStart)(fp);
560 (*fp->parent->LayerStart)(fp->parent->object, fp);
565 NewState(fp, ST_ACKSENT);
567 NewState(fp, ST_REQSENT);
568 (*fp->parent->LayerDown)(fp->parent->object, fp);
572 NewState(fp, ST_ACKSENT);
576 NewState(fp, ST_OPENED);
577 if ((*fp->fn->LayerUp)(fp))
578 (*fp->parent->LayerUp)(fp->parent->object, fp);
580 (*fp->fn->LayerDown)(fp);
581 FsmInitRestartCounter(fp, FSM_TRM_TIMER);
582 FsmSendTerminateReq(fp);
583 NewState(fp, ST_CLOSING);
584 lcp_SendIdentification(&fp->link->lcp);
590 NewState(fp, ST_REQSENT);
595 if (dec.rejend != dec.rej && --fp->more.rejs <= 0) {
596 log_Printf(LogPHASE, "%s: Too many %s REJs sent - abandoning negotiation\n",
597 fp->link->name, fp->name);
598 lcp_SendIdentification(&fp->link->lcp);
602 if (dec.nakend != dec.nak && --fp->more.naks <= 0) {
603 log_Printf(LogPHASE, "%s: Too many %s NAKs sent - abandoning negotiation\n",
604 fp->link->name, fp->name);
605 lcp_SendIdentification(&fp->link->lcp);
611 FsmRecvConfigAck(struct fsm *fp, struct fsmheader *lhp, struct mbuf *bp)
614 struct fsm_decode dec;
619 flen = ntohs(lhp->length) - sizeof *lhp;
626 dec.ackend = dec.ack;
627 dec.nakend = dec.nak;
628 dec.rejend = dec.rej;
630 (*fp->fn->DecodeConfig)(fp, cp, cp + flen, MODE_ACK, &dec);
631 if (flen < (int)sizeof(struct fsm_opt_hdr))
632 log_Printf(fp->LogLevel, " [EMPTY]\n");
637 (*fp->fn->SendTerminateAck)(fp, lhp->id);
643 FsmInitRestartCounter(fp, FSM_REQ_TIMER);
644 NewState(fp, ST_ACKRCVD);
647 FsmSendConfigReq(fp);
648 NewState(fp, ST_REQSENT);
651 FsmInitRestartCounter(fp, FSM_REQ_TIMER);
652 NewState(fp, ST_OPENED);
653 if ((*fp->fn->LayerUp)(fp))
654 (*fp->parent->LayerUp)(fp->parent->object, fp);
656 (*fp->fn->LayerDown)(fp);
657 FsmInitRestartCounter(fp, FSM_TRM_TIMER);
658 FsmSendTerminateReq(fp);
659 NewState(fp, ST_CLOSING);
660 lcp_SendIdentification(&fp->link->lcp);
664 (*fp->fn->LayerDown)(fp);
665 FsmSendConfigReq(fp);
666 NewState(fp, ST_REQSENT);
667 (*fp->parent->LayerDown)(fp->parent->object, fp);
674 FsmRecvConfigNak(struct fsm *fp, struct fsmheader *lhp, struct mbuf *bp)
677 struct fsm_decode dec;
682 flen = ntohs(lhp->length) - sizeof *lhp;
689 * Check and process easy case
694 log_Printf(fp->LogLevel, "%s: Oops, RCN in %s.\n",
695 fp->link->name, State2Nam(fp->state));
700 (*fp->fn->SendTerminateAck)(fp, lhp->id);
710 dec.ackend = dec.ack;
711 dec.nakend = dec.nak;
712 dec.rejend = dec.rej;
714 (*fp->fn->DecodeConfig)(fp, cp, cp + flen, MODE_NAK, &dec);
715 if (flen < (int)sizeof(struct fsm_opt_hdr))
716 log_Printf(fp->LogLevel, " [EMPTY]\n");
721 FsmInitRestartCounter(fp, FSM_REQ_TIMER);
722 FsmSendConfigReq(fp);
725 (*fp->fn->LayerDown)(fp);
726 FsmSendConfigReq(fp);
727 NewState(fp, ST_REQSENT);
728 (*fp->parent->LayerDown)(fp->parent->object, fp);
731 FsmSendConfigReq(fp);
732 NewState(fp, ST_REQSENT);
740 FsmRecvTermReq(struct fsm *fp, struct fsmheader *lhp, struct mbuf *bp)
746 log_Printf(fp->LogLevel, "%s: Oops, RTR in %s\n",
747 fp->link->name, State2Nam(fp->state));
754 (*fp->fn->SendTerminateAck)(fp, lhp->id);
758 (*fp->fn->SendTerminateAck)(fp, lhp->id);
759 NewState(fp, ST_REQSENT);
762 (*fp->fn->LayerDown)(fp);
763 (*fp->fn->SendTerminateAck)(fp, lhp->id);
764 FsmInitRestartCounter(fp, FSM_TRM_TIMER);
765 timer_Start(&fp->FsmTimer); /* Start restart timer */
767 NewState(fp, ST_STOPPING);
768 (*fp->parent->LayerDown)(fp->parent->object, fp);
769 /* A delayed ST_STOPPED is now scheduled */
776 FsmRecvTermAck(struct fsm *fp, struct fsmheader *lhp __unused, struct mbuf *bp)
781 (*fp->fn->LayerFinish)(fp);
782 NewState(fp, ST_CLOSED);
783 (*fp->parent->LayerFinish)(fp->parent->object, fp);
786 (*fp->fn->LayerFinish)(fp);
787 NewState(fp, ST_STOPPED);
788 (*fp->parent->LayerFinish)(fp->parent->object, fp);
791 NewState(fp, ST_REQSENT);
794 (*fp->fn->LayerDown)(fp);
795 FsmSendConfigReq(fp);
796 NewState(fp, ST_REQSENT);
797 (*fp->parent->LayerDown)(fp->parent->object, fp);
804 FsmRecvConfigRej(struct fsm *fp, struct fsmheader *lhp, struct mbuf *bp)
807 struct fsm_decode dec;
813 flen = ntohs(lhp->length) - sizeof *lhp;
814 if ((int)plen < flen) {
819 lcp_SendIdentification(&fp->link->lcp);
822 * Check and process easy case
827 log_Printf(fp->LogLevel, "%s: Oops, RCJ in %s.\n",
828 fp->link->name, State2Nam(fp->state));
833 (*fp->fn->SendTerminateAck)(fp, lhp->id);
843 dec.ackend = dec.ack;
844 dec.nakend = dec.nak;
845 dec.rejend = dec.rej;
847 (*fp->fn->DecodeConfig)(fp, cp, cp + flen, MODE_REJ, &dec);
848 if (flen < (int)sizeof(struct fsm_opt_hdr))
849 log_Printf(fp->LogLevel, " [EMPTY]\n");
854 FsmInitRestartCounter(fp, FSM_REQ_TIMER);
855 FsmSendConfigReq(fp);
858 (*fp->fn->LayerDown)(fp);
859 FsmSendConfigReq(fp);
860 NewState(fp, ST_REQSENT);
861 (*fp->parent->LayerDown)(fp->parent->object, fp);
864 FsmSendConfigReq(fp);
865 NewState(fp, ST_REQSENT);
872 FsmRecvCodeRej(struct fsm *fp __unused, struct fsmheader *lhp __unused,
879 FsmRecvProtoRej(struct fsm *fp, struct fsmheader *lhp __unused, struct mbuf *bp)
881 struct physical *p = link2physical(fp->link);
884 if (m_length(bp) < 2) {
888 bp = mbuf_Read(bp, &proto, 2);
889 proto = ntohs(proto);
890 log_Printf(fp->LogLevel, "%s: -- Protocol 0x%04x (%s) was rejected!\n",
891 fp->link->name, proto, hdlc_Protocol2Nam(proto));
896 lqr_Stop(p, LQM_LQR);
898 log_Printf(LogERROR, "%s: FsmRecvProtoRej: Not a physical link !\n",
902 if (fp->proto == PROTO_LCP) {
903 fp = &fp->link->ccp.fsm;
904 /* Despite the RFC (1661), don't do an out-of-place TLF */
905 /* (*fp->fn->LayerFinish)(fp); */
909 NewState(fp, ST_CLOSED);
912 NewState(fp, ST_STOPPED);
916 /* (*fp->parent->LayerFinish)(fp->parent->object, fp); */
920 if (fp->proto == PROTO_LCP) {
921 log_Printf(LogPHASE, "%s: IPCP protocol reject closes IPCP !\n",
923 fsm_Close(&fp->bundle->ncp.ipcp.fsm);
928 if (fp->proto == PROTO_LCP) {
929 log_Printf(LogPHASE, "%s: IPV6CP protocol reject closes IPV6CP !\n",
931 fsm_Close(&fp->bundle->ncp.ipv6cp.fsm);
936 if (fp->proto == PROTO_LCP) {
937 struct lcp *lcp = fsm2lcp(fp);
939 if (lcp->want_mrru && lcp->his_mrru) {
940 log_Printf(LogPHASE, "%s: MP protocol reject is fatal !\n",
951 FsmRecvEchoReq(struct fsm *fp, struct fsmheader *lhp, struct mbuf *bp)
953 struct lcp *lcp = fsm2lcp(fp);
958 m_settype(bp, MB_ECHOIN);
960 if (lcp && ntohs(lhp->length) - sizeof *lhp >= 4) {
962 ua_ntohl(cp, &magic);
963 if (magic != lcp->his_magic) {
964 log_Printf(fp->LogLevel, "%s: RecvEchoReq: magic 0x%08lx is wrong,"
965 " expecting 0x%08lx\n", fp->link->name, (u_long)magic,
966 (u_long)lcp->his_magic);
967 /* XXX: We should send terminate request */
969 if (fp->state == ST_OPENED) {
970 ua_htonl(&lcp->want_magic, cp); /* local magic */
971 fsm_Output(fp, CODE_ECHOREP, lhp->id, cp,
972 ntohs(lhp->length) - sizeof *lhp, MB_ECHOOUT);
979 FsmRecvEchoRep(struct fsm *fp, struct fsmheader *lhp __unused, struct mbuf *bp)
982 bp = lqr_RecvEcho(fp, bp);
988 FsmRecvDiscReq(struct fsm *fp __unused, struct fsmheader *lhp __unused,
995 FsmRecvIdent(struct fsm *fp, struct fsmheader *lhp, struct mbuf *bp)
1001 len = ntohs(lhp->length) - sizeof *lhp;
1003 bp = m_pullup(m_append(bp, "", 1));
1005 ua_ntohl(cp, &magic);
1006 if (magic != fp->link->lcp.his_magic)
1007 log_Printf(fp->LogLevel, "%s: RecvIdent: magic 0x%08lx is wrong,"
1008 " expecting 0x%08lx\n", fp->link->name, (u_long)magic,
1009 (u_long)fp->link->lcp.his_magic);
1011 lcp_RecvIdentification(&fp->link->lcp, cp + 4);
1017 FsmRecvTimeRemain(struct fsm *fp __unused, struct fsmheader *lhp __unused,
1024 FsmRecvResetReq(struct fsm *fp, struct fsmheader *lhp, struct mbuf *bp)
1026 if ((*fp->fn->RecvResetReq)(fp)) {
1028 * All sendable compressed packets are queued in the first (lowest
1029 * priority) modem output queue.... dump 'em to the priority queue
1030 * so that they arrive at the peer before our ResetAck.
1032 link_SequenceQueue(fp->link);
1033 fsm_Output(fp, CODE_RESETACK, lhp->id, NULL, 0, MB_CCPOUT);
1039 FsmRecvResetAck(struct fsm *fp, struct fsmheader *lhp, struct mbuf *bp)
1041 (*fp->fn->RecvResetAck)(fp, lhp->id);
1046 fsm_Input(struct fsm *fp, struct mbuf *bp)
1049 struct fsmheader lh;
1050 const struct fsmcodedesc *codep;
1053 if (len < sizeof(struct fsmheader)) {
1057 bp = mbuf_Read(bp, &lh, sizeof lh);
1059 if (ntohs(lh.length) > len) {
1060 log_Printf(LogWARN, "%s: Oops: Got %zu bytes but %d byte payload "
1061 "- dropped\n", fp->link->name, len, (int)ntohs(lh.length));
1066 if (lh.code < fp->min_code || lh.code > fp->max_code ||
1067 lh.code > sizeof FsmCodes / sizeof *FsmCodes) {
1069 * Use a private id. This is really a response-type packet, but we
1070 * MUST send a unique id for each REQ....
1074 bp = m_prepend(bp, &lh, sizeof lh, 0);
1076 fsm_Output(fp, CODE_CODEREJ, id++, MBUF_CTOP(bp), bp->m_len, MB_UNKNOWN);
1081 codep = FsmCodes + lh.code - 1;
1082 if (lh.id != fp->reqid && codep->check_reqid &&
1083 Enabled(fp->bundle, OPT_IDCHECK)) {
1084 log_Printf(fp->LogLevel, "%s: Recv%s(%d), dropped (expected %d)\n",
1085 fp->link->name, codep->name, lh.id, fp->reqid);
1089 log_Printf(fp->LogLevel, "%s: Recv%s(%d) state = %s\n",
1090 fp->link->name, codep->name, lh.id, State2Nam(fp->state));
1092 if (codep->inc_reqid && (lh.id == fp->reqid ||
1093 (!Enabled(fp->bundle, OPT_IDCHECK) && codep->check_reqid)))
1094 fp->reqid++; /* That's the end of that ``exchange''.... */
1096 (*codep->recv)(fp, &lh, bp);
1100 fsm_NullRecvResetReq(struct fsm *fp)
1102 log_Printf(fp->LogLevel, "%s: Oops - received unexpected reset req\n",
1108 fsm_NullRecvResetAck(struct fsm *fp, u_char id __unused)
1110 log_Printf(fp->LogLevel, "%s: Oops - received unexpected reset ack\n",
1115 fsm_Reopen(struct fsm *fp)
1117 if (fp->state == ST_OPENED) {
1118 (*fp->fn->LayerDown)(fp);
1119 FsmInitRestartCounter(fp, FSM_REQ_TIMER);
1120 FsmSendConfigReq(fp);
1121 NewState(fp, ST_REQSENT);
1122 (*fp->parent->LayerDown)(fp->parent->object, fp);
1127 fsm2initial(struct fsm *fp)
1129 timer_Stop(&fp->FsmTimer);
1130 timer_Stop(&fp->OpenTimer);
1131 timer_Stop(&fp->StoppedTimer);
1132 if (fp->state == ST_STOPPED)
1134 if (fp->state > ST_INITIAL)
1136 if (fp->state > ST_INITIAL)
1141 fsm_readopt(u_char **cp)
1143 struct fsm_opt *o = (struct fsm_opt *)*cp;
1145 if (o->hdr.len < sizeof(struct fsm_opt_hdr)) {
1146 log_Printf(LogERROR, "Bad option length %d (out of phase?)\n", o->hdr.len);
1152 if (o->hdr.len > sizeof(struct fsm_opt)) {
1153 log_Printf(LogERROR, "Warning: Truncating option length from %d to %d\n",
1154 o->hdr.len, (int)sizeof(struct fsm_opt));
1155 o->hdr.len = sizeof(struct fsm_opt);
1162 fsm_opt(u_char *opt, int optlen, const struct fsm_opt *o)
1164 unsigned cplen = o->hdr.len;
1166 if (optlen < (int)sizeof(struct fsm_opt_hdr))
1169 if ((int)cplen > optlen) {
1170 log_Printf(LogERROR, "Can't REJ length %d - trunating to %d\n",
1174 memcpy(opt, o, cplen);
1182 fsm_rej(struct fsm_decode *dec, const struct fsm_opt *o)
1186 dec->rejend += fsm_opt(dec->rejend, FSM_OPTLEN - (dec->rejend - dec->rej), o);
1190 fsm_ack(struct fsm_decode *dec, const struct fsm_opt *o)
1194 dec->ackend += fsm_opt(dec->ackend, FSM_OPTLEN - (dec->ackend - dec->ack), o);
1198 fsm_nak(struct fsm_decode *dec, const struct fsm_opt *o)
1202 dec->nakend += fsm_opt(dec->nakend, FSM_OPTLEN - (dec->nakend - dec->nak), o);
1206 fsm_opt_normalise(struct fsm_decode *dec)
1208 if (dec->rejend != dec->rej) {
1209 /* rejects are preferred */
1210 dec->ackend = dec->ack;
1211 dec->nakend = dec->nak;
1212 } else if (dec->nakend != dec->nak)
1214 dec->ackend = dec->ack;