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)
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
31 #include <sys/param.h>
32 #include <netinet/in.h>
33 #include <netinet/in_systm.h>
34 #include <netinet/ip.h>
50 #include "throughput.h"
51 #include "slcompress.h"
54 #include "descriptor.h"
67 static void FsmSendConfigReq(struct fsm *);
68 static void FsmSendTerminateReq(struct fsm *);
69 static void FsmInitRestartCounter(struct fsm *, int);
71 typedef void (recvfn)(struct fsm *, struct fsmheader *, struct mbuf *);
72 static recvfn FsmRecvConfigReq, FsmRecvConfigAck, FsmRecvConfigNak,
73 FsmRecvConfigRej, FsmRecvTermReq, FsmRecvTermAck,
74 FsmRecvCodeRej, FsmRecvProtoRej, FsmRecvEchoReq,
75 FsmRecvEchoRep, FsmRecvDiscReq, FsmRecvIdent,
76 FsmRecvTimeRemain, FsmRecvResetReq, FsmRecvResetAck;
78 static const struct fsmcodedesc {
80 unsigned check_reqid : 1;
81 unsigned inc_reqid : 1;
84 { FsmRecvConfigReq, 0, 0, "ConfigReq" },
85 { FsmRecvConfigAck, 1, 1, "ConfigAck" },
86 { FsmRecvConfigNak, 1, 1, "ConfigNak" },
87 { FsmRecvConfigRej, 1, 1, "ConfigRej" },
88 { FsmRecvTermReq, 0, 0, "TerminateReq" },
89 { FsmRecvTermAck, 1, 1, "TerminateAck" },
90 { FsmRecvCodeRej, 0, 0, "CodeRej" },
91 { FsmRecvProtoRej, 0, 0, "ProtocolRej" },
92 { FsmRecvEchoReq, 0, 0, "EchoRequest" },
93 { FsmRecvEchoRep, 0, 0, "EchoReply" },
94 { FsmRecvDiscReq, 0, 0, "DiscardReq" },
95 { FsmRecvIdent, 0, 1, "Ident" },
96 { FsmRecvTimeRemain,0, 0, "TimeRemain" },
97 { FsmRecvResetReq, 0, 0, "ResetReq" },
98 { FsmRecvResetAck, 0, 1, "ResetAck" }
104 if (code == 0 || code > sizeof FsmCodes / sizeof FsmCodes[0])
106 return FsmCodes[code-1].name;
110 State2Nam(u_int state)
112 static const char * const StateNames[] = {
113 "Initial", "Starting", "Closed", "Stopped", "Closing", "Stopping",
114 "Req-Sent", "Ack-Rcvd", "Ack-Sent", "Opened",
117 if (state >= sizeof StateNames / sizeof StateNames[0])
119 return StateNames[state];
123 StoppedTimeout(void *v)
125 struct fsm *fp = (struct fsm *)v;
127 log_Printf(fp->LogLevel, "%s: Stopped timer expired\n", fp->link->name);
128 if (fp->OpenTimer.state == TIMER_RUNNING) {
129 log_Printf(LogWARN, "%s: %s: aborting open delay due to stopped timer\n",
130 fp->link->name, fp->name);
131 timer_Stop(&fp->OpenTimer);
133 if (fp->state == ST_STOPPED)
138 fsm_Init(struct fsm *fp, const char *name, u_short proto, int mincode,
139 int maxcode, int LogLevel, struct bundle *bundle,
140 struct link *l, const struct fsm_parent *parent,
141 struct fsm_callbacks *fn, const char * const timer_names[3])
145 fp->min_code = mincode;
146 fp->max_code = maxcode;
147 fp->state = fp->min_code > CODE_TERMACK ? ST_OPENED : ST_INITIAL;
150 fp->more.reqs = fp->more.naks = fp->more.rejs = 3;
151 memset(&fp->FsmTimer, '\0', sizeof fp->FsmTimer);
152 memset(&fp->OpenTimer, '\0', sizeof fp->OpenTimer);
153 memset(&fp->StoppedTimer, '\0', sizeof fp->StoppedTimer);
154 fp->LogLevel = LogLevel;
159 fp->FsmTimer.name = timer_names[0];
160 fp->OpenTimer.name = timer_names[1];
161 fp->StoppedTimer.name = timer_names[2];
165 NewState(struct fsm *fp, int new)
167 log_Printf(fp->LogLevel, "%s: State change %s --> %s\n",
168 fp->link->name, State2Nam(fp->state), State2Nam(new));
169 if (fp->state == ST_STOPPED && fp->StoppedTimer.state == TIMER_RUNNING)
170 timer_Stop(&fp->StoppedTimer);
172 if ((new >= ST_INITIAL && new <= ST_STOPPED) || (new == ST_OPENED)) {
173 timer_Stop(&fp->FsmTimer);
174 if (new == ST_STOPPED && fp->StoppedTimer.load) {
175 timer_Stop(&fp->StoppedTimer);
176 fp->StoppedTimer.func = StoppedTimeout;
177 fp->StoppedTimer.arg = (void *) fp;
178 timer_Start(&fp->StoppedTimer);
184 fsm_Output(struct fsm *fp, u_int code, u_int id, u_char *ptr, int count,
191 if (log_IsKept(fp->LogLevel)) {
192 log_Printf(fp->LogLevel, "%s: Send%s(%d) state = %s\n",
193 fp->link->name, Code2Nam(code), id, State2Nam(fp->state));
199 (*fp->fn->DecodeConfig)(fp, ptr, count, MODE_NOP, NULL);
200 if (count < sizeof(struct fsmconfig))
201 log_Printf(fp->LogLevel, " [EMPTY]\n");
206 plen = sizeof(struct fsmheader) + count;
209 lh.length = htons(plen);
210 bp = m_get(plen, mtype);
211 memcpy(MBUF_CTOP(bp), &lh, sizeof(struct fsmheader));
213 memcpy(MBUF_CTOP(bp) + sizeof(struct fsmheader), ptr, count);
214 log_DumpBp(LogDEBUG, "fsm_Output", bp);
215 link_PushPacket(fp->link, bp, fp->bundle, LINK_QUEUES(fp->link) - 1,
218 if (code == CODE_CONFIGREJ)
219 lcp_SendIdentification(&fp->link->lcp);
225 struct fsm *fp = (struct fsm *)v;
227 timer_Stop(&fp->OpenTimer);
228 if (fp->state <= ST_STOPPED) {
229 if (fp->state != ST_STARTING) {
231 * In practice, we're only here in ST_STOPPED (when delaying the
232 * first config request) or ST_CLOSED (when openmode == 0).
234 * The ST_STOPPED bit is breaking the RFC already :-(
236 * According to the RFC (1661) state transition table, a TLS isn't
237 * required for an Open event when state == Closed, but the RFC
238 * must be wrong as TLS hasn't yet been called (since the last TLF)
239 * ie, Initial gets an `Up' event, Closing gets a RTA etc.
241 (*fp->fn->LayerStart)(fp);
242 (*fp->parent->LayerStart)(fp->parent->object, fp);
244 FsmInitRestartCounter(fp, FSM_REQ_TIMER);
245 FsmSendConfigReq(fp);
246 NewState(fp, ST_REQSENT);
251 fsm_Open(struct fsm *fp)
255 NewState(fp, ST_STARTING);
256 (*fp->fn->LayerStart)(fp);
257 (*fp->parent->LayerStart)(fp->parent->object, fp);
260 if (fp->open_mode == OPEN_PASSIVE) {
261 NewState(fp, ST_STOPPED); /* XXX: This is a hack ! */
262 } else if (fp->open_mode > 0) {
263 if (fp->open_mode > 1)
264 log_Printf(LogPHASE, "%s: Entering STOPPED state for %d seconds\n",
265 fp->link->name, fp->open_mode);
266 NewState(fp, ST_STOPPED); /* XXX: This is a not-so-bad hack ! */
267 timer_Stop(&fp->OpenTimer);
268 fp->OpenTimer.load = fp->open_mode * SECTICKS;
269 fp->OpenTimer.func = FsmOpenNow;
270 fp->OpenTimer.arg = (void *)fp;
271 timer_Start(&fp->OpenTimer);
275 case ST_STOPPED: /* XXX: restart option */
279 case ST_OPENED: /* XXX: restart option */
281 case ST_CLOSING: /* XXX: restart option */
282 case ST_STOPPING: /* XXX: restart option */
283 NewState(fp, ST_STOPPING);
289 fsm_Up(struct fsm *fp)
293 log_Printf(fp->LogLevel, "FSM: Using \"%s\" as a transport\n",
295 NewState(fp, ST_CLOSED);
298 FsmInitRestartCounter(fp, FSM_REQ_TIMER);
299 FsmSendConfigReq(fp);
300 NewState(fp, ST_REQSENT);
303 log_Printf(fp->LogLevel, "%s: Oops, Up at %s\n",
304 fp->link->name, State2Nam(fp->state));
310 fsm_Down(struct fsm *fp)
314 NewState(fp, ST_INITIAL);
317 /* This TLF contradicts the RFC (1661), which ``misses it out'' ! */
318 (*fp->fn->LayerFinish)(fp);
319 NewState(fp, ST_INITIAL);
320 (*fp->parent->LayerFinish)(fp->parent->object, fp);
323 NewState(fp, ST_STARTING);
324 (*fp->fn->LayerStart)(fp);
325 (*fp->parent->LayerStart)(fp->parent->object, fp);
331 NewState(fp, ST_STARTING);
334 (*fp->fn->LayerDown)(fp);
335 NewState(fp, ST_STARTING);
336 (*fp->parent->LayerDown)(fp->parent->object, fp);
342 fsm_Close(struct fsm *fp)
346 (*fp->fn->LayerFinish)(fp);
347 NewState(fp, ST_INITIAL);
348 (*fp->parent->LayerFinish)(fp->parent->object, fp);
351 NewState(fp, ST_CLOSED);
354 NewState(fp, ST_CLOSING);
357 (*fp->fn->LayerDown)(fp);
358 if (fp->state == ST_OPENED) {
359 FsmInitRestartCounter(fp, FSM_TRM_TIMER);
360 FsmSendTerminateReq(fp);
361 NewState(fp, ST_CLOSING);
362 (*fp->parent->LayerDown)(fp->parent->object, fp);
368 FsmInitRestartCounter(fp, FSM_TRM_TIMER);
369 FsmSendTerminateReq(fp);
370 NewState(fp, ST_CLOSING);
379 FsmSendConfigReq(struct fsm *fp)
381 if (fp->more.reqs-- > 0 && fp->restart-- > 0) {
382 (*fp->fn->SendConfigReq)(fp);
383 timer_Start(&fp->FsmTimer); /* Start restart timer */
385 if (fp->more.reqs < 0)
386 log_Printf(LogPHASE, "%s: Too many %s REQs sent - abandoning "
387 "negotiation\n", fp->link->name, fp->name);
388 lcp_SendIdentification(&fp->link->lcp);
394 FsmSendTerminateReq(struct fsm *fp)
396 fsm_Output(fp, CODE_TERMREQ, fp->reqid, NULL, 0, MB_UNKNOWN);
397 (*fp->fn->SentTerminateReq)(fp);
398 timer_Start(&fp->FsmTimer); /* Start restart timer */
399 fp->restart--; /* Decrement restart counter */
408 struct fsm *fp = (struct fsm *)v;
414 FsmSendTerminateReq(fp);
418 FsmSendConfigReq(fp);
421 FsmSendConfigReq(fp);
422 NewState(fp, ST_REQSENT);
425 timer_Start(&fp->FsmTimer);
429 (*fp->fn->LayerFinish)(fp);
430 NewState(fp, ST_CLOSED);
431 (*fp->parent->LayerFinish)(fp->parent->object, fp);
434 (*fp->fn->LayerFinish)(fp);
435 NewState(fp, ST_STOPPED);
436 (*fp->parent->LayerFinish)(fp->parent->object, fp);
438 case ST_REQSENT: /* XXX: 3p */
441 (*fp->fn->LayerFinish)(fp);
442 NewState(fp, ST_STOPPED);
443 (*fp->parent->LayerFinish)(fp->parent->object, fp);
450 FsmInitRestartCounter(struct fsm *fp, int what)
452 timer_Stop(&fp->FsmTimer);
453 fp->FsmTimer.func = FsmTimeout;
454 fp->FsmTimer.arg = (void *)fp;
455 (*fp->fn->InitRestartCounter)(fp, what);
459 * Actions when receive packets
462 FsmRecvConfigReq(struct fsm *fp, struct fsmheader *lhp, struct mbuf *bp)
465 struct fsm_decode dec;
470 flen = ntohs(lhp->length) - sizeof *lhp;
472 log_Printf(LogWARN, "%s: FsmRecvConfigReq: plen (%d) < flen (%d)\n",
473 fp->link->name, plen, flen);
478 /* Check and process easy case */
481 if (fp->proto == PROTO_CCP && fp->link->lcp.fsm.state == ST_OPENED) {
483 * ccp_SetOpenMode() leaves us in initial if we're disabling
484 * & denying everything.
486 bp = m_prepend(bp, lhp, sizeof *lhp, 2);
487 bp = proto_Prepend(bp, fp->proto, 0, 0);
489 lcp_SendProtoRej(&fp->link->lcp, MBUF_CTOP(bp), bp->m_len);
495 log_Printf(fp->LogLevel, "%s: Oops, RCR in %s.\n",
496 fp->link->name, State2Nam(fp->state));
500 (*fp->fn->SendTerminateAck)(fp, lhp->id);
504 log_Printf(fp->LogLevel, "%s: Error: Got ConfigReq while state = %s\n",
505 fp->link->name, State2Nam(fp->state));
510 (*fp->fn->LayerDown)(fp);
511 (*fp->parent->LayerDown)(fp->parent->object, fp);
516 dec.ackend = dec.ack;
517 dec.nakend = dec.nak;
518 dec.rejend = dec.rej;
519 (*fp->fn->DecodeConfig)(fp, MBUF_CTOP(bp), flen, MODE_REQ, &dec);
520 if (flen < sizeof(struct fsmconfig))
521 log_Printf(fp->LogLevel, " [EMPTY]\n");
523 if (dec.nakend == dec.nak && dec.rejend == dec.rej)
528 FsmInitRestartCounter(fp, FSM_REQ_TIMER);
532 FsmSendConfigReq(fp);
536 if (dec.rejend != dec.rej)
537 fsm_Output(fp, CODE_CONFIGREJ, lhp->id, dec.rej, dec.rejend - dec.rej,
539 if (dec.nakend != dec.nak)
540 fsm_Output(fp, CODE_CONFIGNAK, lhp->id, dec.nak, dec.nakend - dec.nak,
543 fsm_Output(fp, CODE_CONFIGACK, lhp->id, dec.ack, dec.ackend - dec.ack,
549 * According to the RFC (1661) state transition table, a TLS isn't
550 * required for a RCR when state == ST_STOPPED, but the RFC
551 * must be wrong as TLS hasn't yet been called (since the last TLF)
553 (*fp->fn->LayerStart)(fp);
554 (*fp->parent->LayerStart)(fp->parent->object, fp);
559 NewState(fp, ST_ACKSENT);
561 NewState(fp, ST_REQSENT);
565 NewState(fp, ST_ACKSENT);
569 NewState(fp, ST_OPENED);
570 if ((*fp->fn->LayerUp)(fp))
571 (*fp->parent->LayerUp)(fp->parent->object, fp);
573 (*fp->fn->LayerDown)(fp);
574 FsmInitRestartCounter(fp, FSM_TRM_TIMER);
575 FsmSendTerminateReq(fp);
576 NewState(fp, ST_CLOSING);
577 lcp_SendIdentification(&fp->link->lcp);
583 NewState(fp, ST_REQSENT);
588 if (dec.rejend != dec.rej && --fp->more.rejs <= 0) {
589 log_Printf(LogPHASE, "%s: Too many %s REJs sent - abandoning negotiation\n",
590 fp->link->name, fp->name);
591 lcp_SendIdentification(&fp->link->lcp);
595 if (dec.nakend != dec.nak && --fp->more.naks <= 0) {
596 log_Printf(LogPHASE, "%s: Too many %s NAKs sent - abandoning negotiation\n",
597 fp->link->name, fp->name);
598 lcp_SendIdentification(&fp->link->lcp);
604 FsmRecvConfigAck(struct fsm *fp, struct fsmheader *lhp, struct mbuf *bp)
610 (*fp->fn->SendTerminateAck)(fp, lhp->id);
616 FsmInitRestartCounter(fp, FSM_REQ_TIMER);
617 NewState(fp, ST_ACKRCVD);
620 FsmSendConfigReq(fp);
621 NewState(fp, ST_REQSENT);
624 FsmInitRestartCounter(fp, FSM_REQ_TIMER);
625 NewState(fp, ST_OPENED);
626 if ((*fp->fn->LayerUp)(fp))
627 (*fp->parent->LayerUp)(fp->parent->object, fp);
629 (*fp->fn->LayerDown)(fp);
630 FsmInitRestartCounter(fp, FSM_TRM_TIMER);
631 FsmSendTerminateReq(fp);
632 NewState(fp, ST_CLOSING);
633 lcp_SendIdentification(&fp->link->lcp);
637 (*fp->fn->LayerDown)(fp);
638 FsmSendConfigReq(fp);
639 NewState(fp, ST_REQSENT);
640 (*fp->parent->LayerDown)(fp->parent->object, fp);
647 FsmRecvConfigNak(struct fsm *fp, struct fsmheader *lhp, struct mbuf *bp)
650 struct fsm_decode dec;
654 flen = ntohs(lhp->length) - sizeof *lhp;
661 * Check and process easy case
666 log_Printf(fp->LogLevel, "%s: Oops, RCN in %s.\n",
667 fp->link->name, State2Nam(fp->state));
672 (*fp->fn->SendTerminateAck)(fp, lhp->id);
682 dec.ackend = dec.ack;
683 dec.nakend = dec.nak;
684 dec.rejend = dec.rej;
685 (*fp->fn->DecodeConfig)(fp, MBUF_CTOP(bp), flen, MODE_NAK, &dec);
686 if (flen < sizeof(struct fsmconfig))
687 log_Printf(fp->LogLevel, " [EMPTY]\n");
692 FsmInitRestartCounter(fp, FSM_REQ_TIMER);
693 FsmSendConfigReq(fp);
696 (*fp->fn->LayerDown)(fp);
697 FsmSendConfigReq(fp);
698 NewState(fp, ST_REQSENT);
699 (*fp->parent->LayerDown)(fp->parent->object, fp);
702 FsmSendConfigReq(fp);
703 NewState(fp, ST_REQSENT);
711 FsmRecvTermReq(struct fsm *fp, struct fsmheader *lhp, struct mbuf *bp)
717 log_Printf(fp->LogLevel, "%s: Oops, RTR in %s\n",
718 fp->link->name, State2Nam(fp->state));
725 (*fp->fn->SendTerminateAck)(fp, lhp->id);
729 (*fp->fn->SendTerminateAck)(fp, lhp->id);
730 NewState(fp, ST_REQSENT);
733 (*fp->fn->LayerDown)(fp);
734 (*fp->fn->SendTerminateAck)(fp, lhp->id);
735 FsmInitRestartCounter(fp, FSM_TRM_TIMER);
736 timer_Start(&fp->FsmTimer); /* Start restart timer */
738 NewState(fp, ST_STOPPING);
739 (*fp->parent->LayerDown)(fp->parent->object, fp);
740 /* A delayed ST_STOPPED is now scheduled */
747 FsmRecvTermAck(struct fsm *fp, struct fsmheader *lhp, struct mbuf *bp)
752 (*fp->fn->LayerFinish)(fp);
753 NewState(fp, ST_CLOSED);
754 (*fp->parent->LayerFinish)(fp->parent->object, fp);
757 (*fp->fn->LayerFinish)(fp);
758 NewState(fp, ST_STOPPED);
759 (*fp->parent->LayerFinish)(fp->parent->object, fp);
762 NewState(fp, ST_REQSENT);
765 (*fp->fn->LayerDown)(fp);
766 FsmSendConfigReq(fp);
767 NewState(fp, ST_REQSENT);
768 (*fp->parent->LayerDown)(fp->parent->object, fp);
775 FsmRecvConfigRej(struct fsm *fp, struct fsmheader *lhp, struct mbuf *bp)
778 struct fsm_decode dec;
782 flen = ntohs(lhp->length) - sizeof *lhp;
788 lcp_SendIdentification(&fp->link->lcp);
791 * Check and process easy case
796 log_Printf(fp->LogLevel, "%s: Oops, RCJ in %s.\n",
797 fp->link->name, State2Nam(fp->state));
802 (*fp->fn->SendTerminateAck)(fp, lhp->id);
812 dec.ackend = dec.ack;
813 dec.nakend = dec.nak;
814 dec.rejend = dec.rej;
815 (*fp->fn->DecodeConfig)(fp, MBUF_CTOP(bp), flen, MODE_REJ, &dec);
816 if (flen < sizeof(struct fsmconfig))
817 log_Printf(fp->LogLevel, " [EMPTY]\n");
822 FsmInitRestartCounter(fp, FSM_REQ_TIMER);
823 FsmSendConfigReq(fp);
826 (*fp->fn->LayerDown)(fp);
827 FsmSendConfigReq(fp);
828 NewState(fp, ST_REQSENT);
829 (*fp->parent->LayerDown)(fp->parent->object, fp);
832 FsmSendConfigReq(fp);
833 NewState(fp, ST_REQSENT);
840 FsmRecvCodeRej(struct fsm *fp, struct fsmheader *lhp, struct mbuf *bp)
846 FsmRecvProtoRej(struct fsm *fp, struct fsmheader *lhp, struct mbuf *bp)
848 struct physical *p = link2physical(fp->link);
851 if (m_length(bp) < 2) {
855 bp = mbuf_Read(bp, &proto, 2);
856 proto = ntohs(proto);
857 log_Printf(fp->LogLevel, "%s: -- Protocol 0x%04x (%s) was rejected!\n",
858 fp->link->name, proto, hdlc_Protocol2Nam(proto));
863 lqr_Stop(p, LQM_LQR);
865 log_Printf(LogERROR, "%s: FsmRecvProtoRej: Not a physical link !\n",
869 if (fp->proto == PROTO_LCP) {
870 fp = &fp->link->ccp.fsm;
871 /* Despite the RFC (1661), don't do an out-of-place TLF */
872 /* (*fp->fn->LayerFinish)(fp); */
876 NewState(fp, ST_CLOSED);
878 NewState(fp, ST_STOPPED);
882 /* (*fp->parent->LayerFinish)(fp->parent->object, fp); */
886 if (fp->proto == PROTO_LCP) {
887 log_Printf(LogPHASE, "%s: IPCP protocol reject closes IPCP !\n",
889 fsm_Close(&fp->bundle->ncp.ipcp.fsm);
893 if (fp->proto == PROTO_LCP) {
894 struct lcp *lcp = fsm2lcp(fp);
896 if (lcp->want_mrru && lcp->his_mrru) {
897 log_Printf(LogPHASE, "%s: MP protocol reject is fatal !\n",
908 FsmRecvEchoReq(struct fsm *fp, struct fsmheader *lhp, struct mbuf *bp)
910 struct lcp *lcp = fsm2lcp(fp);
915 m_settype(bp, MB_ECHOIN);
917 if (lcp && ntohs(lhp->length) - sizeof *lhp >= 4) {
919 ua_ntohl(cp, &magic);
920 if (magic != lcp->his_magic) {
921 log_Printf(fp->LogLevel, "%s: RecvEchoReq: magic 0x%08lx is wrong,"
922 " expecting 0x%08lx\n", fp->link->name, (u_long)magic,
923 (u_long)lcp->his_magic);
924 /* XXX: We should send terminate request */
926 if (fp->state == ST_OPENED) {
927 ua_htonl(&lcp->want_magic, cp); /* local magic */
928 fsm_Output(fp, CODE_ECHOREP, lhp->id, cp,
929 ntohs(lhp->length) - sizeof *lhp, MB_ECHOOUT);
936 FsmRecvEchoRep(struct fsm *fp, struct fsmheader *lhp, struct mbuf *bp)
939 bp = lqr_RecvEcho(fp, bp);
945 FsmRecvDiscReq(struct fsm *fp, struct fsmheader *lhp, struct mbuf *bp)
951 FsmRecvIdent(struct fsm *fp, struct fsmheader *lhp, struct mbuf *bp)
957 len = ntohs(lhp->length) - sizeof *lhp;
959 bp = m_pullup(m_append(bp, "", 1));
961 ua_ntohl(cp, &magic);
962 if (magic != fp->link->lcp.his_magic)
963 log_Printf(fp->LogLevel, "%s: RecvIdent: magic 0x%08lx is wrong,"
964 " expecting 0x%08lx\n", fp->link->name, (u_long)magic,
965 (u_long)fp->link->lcp.his_magic);
967 lcp_RecvIdentification(&fp->link->lcp, cp + 4);
973 FsmRecvTimeRemain(struct fsm *fp, struct fsmheader *lhp, struct mbuf *bp)
979 FsmRecvResetReq(struct fsm *fp, struct fsmheader *lhp, struct mbuf *bp)
981 if ((*fp->fn->RecvResetReq)(fp)) {
983 * All sendable compressed packets are queued in the first (lowest
984 * priority) modem output queue.... dump 'em to the priority queue
985 * so that they arrive at the peer before our ResetAck.
987 link_SequenceQueue(fp->link);
988 fsm_Output(fp, CODE_RESETACK, lhp->id, NULL, 0, MB_CCPOUT);
994 FsmRecvResetAck(struct fsm *fp, struct fsmheader *lhp, struct mbuf *bp)
996 (*fp->fn->RecvResetAck)(fp, lhp->id);
1001 fsm_Input(struct fsm *fp, struct mbuf *bp)
1004 struct fsmheader lh;
1005 const struct fsmcodedesc *codep;
1008 if (len < sizeof(struct fsmheader)) {
1012 bp = mbuf_Read(bp, &lh, sizeof lh);
1014 if (ntohs(lh.length) > len) {
1015 log_Printf(LogWARN, "%s: Oops: Got %d bytes but %d byte payload "
1016 "- dropped\n", fp->link->name, len, (int)ntohs(lh.length));
1021 if (lh.code < fp->min_code || lh.code > fp->max_code ||
1022 lh.code > sizeof FsmCodes / sizeof *FsmCodes) {
1024 * Use a private id. This is really a response-type packet, but we
1025 * MUST send a unique id for each REQ....
1029 bp = m_prepend(bp, &lh, sizeof lh, 0);
1031 fsm_Output(fp, CODE_CODEREJ, id++, MBUF_CTOP(bp), bp->m_len, MB_UNKNOWN);
1036 codep = FsmCodes + lh.code - 1;
1037 if (lh.id != fp->reqid && codep->check_reqid &&
1038 Enabled(fp->bundle, OPT_IDCHECK)) {
1039 log_Printf(fp->LogLevel, "%s: Recv%s(%d), dropped (expected %d)\n",
1040 fp->link->name, codep->name, lh.id, fp->reqid);
1044 log_Printf(fp->LogLevel, "%s: Recv%s(%d) state = %s\n",
1045 fp->link->name, codep->name, lh.id, State2Nam(fp->state));
1047 if (codep->inc_reqid && (lh.id == fp->reqid ||
1048 (!Enabled(fp->bundle, OPT_IDCHECK) && codep->check_reqid)))
1049 fp->reqid++; /* That's the end of that ``exchange''.... */
1051 (*codep->recv)(fp, &lh, bp);
1055 fsm_NullRecvResetReq(struct fsm *fp)
1057 log_Printf(fp->LogLevel, "%s: Oops - received unexpected reset req\n",
1063 fsm_NullRecvResetAck(struct fsm *fp, u_char id)
1065 log_Printf(fp->LogLevel, "%s: Oops - received unexpected reset ack\n",
1070 fsm_Reopen(struct fsm *fp)
1072 if (fp->state == ST_OPENED) {
1073 (*fp->fn->LayerDown)(fp);
1074 FsmInitRestartCounter(fp, FSM_REQ_TIMER);
1075 FsmSendConfigReq(fp);
1076 NewState(fp, ST_REQSENT);
1077 (*fp->parent->LayerDown)(fp->parent->object, fp);
1082 fsm2initial(struct fsm *fp)
1084 timer_Stop(&fp->FsmTimer);
1085 timer_Stop(&fp->OpenTimer);
1086 timer_Stop(&fp->StoppedTimer);
1087 if (fp->state == ST_STOPPED)
1089 if (fp->state > ST_INITIAL)
1091 if (fp->state > ST_INITIAL)