2 * PPP Finite State Machine for LCP/IPCP
4 * Written by Toshiharu OHNO (tony-o@iij.ad.jp)
6 * Copyright (C) 1993, Internet Initiative Japan, Inc. All rights reserverd.
8 * Redistribution and use in source and binary forms are permitted
9 * provided that the above copyright notice and this paragraph are
10 * duplicated in all such forms and that any documentation,
11 * advertising materials, and other materials related to such
12 * distribution and use acknowledge that the software was developed
13 * by the Internet Initiative Japan, Inc. The name of the
14 * IIJ may not be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
25 #include <sys/param.h>
26 #include <netinet/in.h>
27 #include <netinet/in_systm.h>
28 #include <netinet/ip.h>
44 #include "throughput.h"
45 #include "slcompress.h"
48 #include "descriptor.h"
61 static void FsmSendConfigReq(struct fsm *);
62 static void FsmSendTerminateReq(struct fsm *);
63 static void FsmInitRestartCounter(struct fsm *, int);
65 typedef void (recvfn)(struct fsm *, struct fsmheader *, struct mbuf *);
66 static recvfn FsmRecvConfigReq, FsmRecvConfigAck, FsmRecvConfigNak,
67 FsmRecvConfigRej, FsmRecvTermReq, FsmRecvTermAck,
68 FsmRecvCodeRej, FsmRecvProtoRej, FsmRecvEchoReq,
69 FsmRecvEchoRep, FsmRecvDiscReq, FsmRecvIdent,
70 FsmRecvTimeRemain, FsmRecvResetReq, FsmRecvResetAck;
72 static const struct fsmcodedesc {
74 unsigned check_reqid : 1;
75 unsigned inc_reqid : 1;
78 { FsmRecvConfigReq, 0, 0, "ConfigReq" },
79 { FsmRecvConfigAck, 1, 1, "ConfigAck" },
80 { FsmRecvConfigNak, 1, 1, "ConfigNak" },
81 { FsmRecvConfigRej, 1, 1, "ConfigRej" },
82 { FsmRecvTermReq, 0, 0, "TerminateReq" },
83 { FsmRecvTermAck, 1, 1, "TerminateAck" },
84 { FsmRecvCodeRej, 0, 0, "CodeRej" },
85 { FsmRecvProtoRej, 0, 0, "ProtocolRej" },
86 { FsmRecvEchoReq, 0, 0, "EchoRequest" },
87 { FsmRecvEchoRep, 0, 0, "EchoReply" },
88 { FsmRecvDiscReq, 0, 0, "DiscardReq" },
89 { FsmRecvIdent, 0, 1, "Ident" },
90 { FsmRecvTimeRemain,0, 0, "TimeRemain" },
91 { FsmRecvResetReq, 0, 0, "ResetReq" },
92 { FsmRecvResetAck, 0, 1, "ResetAck" }
98 if (code == 0 || code > sizeof FsmCodes / sizeof FsmCodes[0])
100 return FsmCodes[code-1].name;
104 State2Nam(u_int state)
106 static const char * const StateNames[] = {
107 "Initial", "Starting", "Closed", "Stopped", "Closing", "Stopping",
108 "Req-Sent", "Ack-Rcvd", "Ack-Sent", "Opened",
111 if (state >= sizeof StateNames / sizeof StateNames[0])
113 return StateNames[state];
117 StoppedTimeout(void *v)
119 struct fsm *fp = (struct fsm *)v;
121 log_Printf(fp->LogLevel, "%s: Stopped timer expired\n", fp->link->name);
122 if (fp->OpenTimer.state == TIMER_RUNNING) {
123 log_Printf(LogWARN, "%s: %s: aborting open delay due to stopped timer\n",
124 fp->link->name, fp->name);
125 timer_Stop(&fp->OpenTimer);
127 if (fp->state == ST_STOPPED)
132 fsm_Init(struct fsm *fp, const char *name, u_short proto, int mincode,
133 int maxcode, int LogLevel, struct bundle *bundle,
134 struct link *l, const struct fsm_parent *parent,
135 struct fsm_callbacks *fn, const char * const timer_names[3])
139 fp->min_code = mincode;
140 fp->max_code = maxcode;
141 fp->state = fp->min_code > CODE_TERMACK ? ST_OPENED : ST_INITIAL;
144 fp->more.reqs = fp->more.naks = fp->more.rejs = 3;
145 memset(&fp->FsmTimer, '\0', sizeof fp->FsmTimer);
146 memset(&fp->OpenTimer, '\0', sizeof fp->OpenTimer);
147 memset(&fp->StoppedTimer, '\0', sizeof fp->StoppedTimer);
148 fp->LogLevel = LogLevel;
153 fp->FsmTimer.name = timer_names[0];
154 fp->OpenTimer.name = timer_names[1];
155 fp->StoppedTimer.name = timer_names[2];
159 NewState(struct fsm *fp, int new)
161 log_Printf(fp->LogLevel, "%s: State change %s --> %s\n",
162 fp->link->name, State2Nam(fp->state), State2Nam(new));
163 if (fp->state == ST_STOPPED && fp->StoppedTimer.state == TIMER_RUNNING)
164 timer_Stop(&fp->StoppedTimer);
166 if ((new >= ST_INITIAL && new <= ST_STOPPED) || (new == ST_OPENED)) {
167 timer_Stop(&fp->FsmTimer);
168 if (new == ST_STOPPED && fp->StoppedTimer.load) {
169 timer_Stop(&fp->StoppedTimer);
170 fp->StoppedTimer.func = StoppedTimeout;
171 fp->StoppedTimer.arg = (void *) fp;
172 timer_Start(&fp->StoppedTimer);
178 fsm_Output(struct fsm *fp, u_int code, u_int id, u_char *ptr, int count,
185 if (log_IsKept(fp->LogLevel)) {
186 log_Printf(fp->LogLevel, "%s: Send%s(%d) state = %s\n",
187 fp->link->name, Code2Nam(code), id, State2Nam(fp->state));
193 (*fp->fn->DecodeConfig)(fp, ptr, count, MODE_NOP, NULL);
194 if (count < sizeof(struct fsmconfig))
195 log_Printf(fp->LogLevel, " [EMPTY]\n");
200 plen = sizeof(struct fsmheader) + count;
203 lh.length = htons(plen);
204 bp = m_get(plen, mtype);
205 memcpy(MBUF_CTOP(bp), &lh, sizeof(struct fsmheader));
207 memcpy(MBUF_CTOP(bp) + sizeof(struct fsmheader), ptr, count);
208 log_DumpBp(LogDEBUG, "fsm_Output", bp);
209 link_PushPacket(fp->link, bp, fp->bundle, LINK_QUEUES(fp->link) - 1,
212 if (code == CODE_CONFIGREJ)
213 lcp_SendIdentification(&fp->link->lcp);
219 struct fsm *fp = (struct fsm *)v;
221 timer_Stop(&fp->OpenTimer);
222 if (fp->state <= ST_STOPPED) {
223 if (fp->state != ST_STARTING) {
225 * In practice, we're only here in ST_STOPPED (when delaying the
226 * first config request) or ST_CLOSED (when openmode == 0).
228 * The ST_STOPPED bit is breaking the RFC already :-(
230 * According to the RFC (1661) state transition table, a TLS isn't
231 * required for an Open event when state == Closed, but the RFC
232 * must be wrong as TLS hasn't yet been called (since the last TLF)
233 * ie, Initial gets an `Up' event, Closing gets a RTA etc.
235 (*fp->fn->LayerStart)(fp);
236 (*fp->parent->LayerStart)(fp->parent->object, fp);
238 FsmInitRestartCounter(fp, FSM_REQ_TIMER);
239 FsmSendConfigReq(fp);
240 NewState(fp, ST_REQSENT);
245 fsm_Open(struct fsm *fp)
249 NewState(fp, ST_STARTING);
250 (*fp->fn->LayerStart)(fp);
251 (*fp->parent->LayerStart)(fp->parent->object, fp);
254 if (fp->open_mode == OPEN_PASSIVE) {
255 NewState(fp, ST_STOPPED); /* XXX: This is a hack ! */
256 } else if (fp->open_mode > 0) {
257 if (fp->open_mode > 1)
258 log_Printf(LogPHASE, "%s: Entering STOPPED state for %d seconds\n",
259 fp->link->name, fp->open_mode);
260 NewState(fp, ST_STOPPED); /* XXX: This is a not-so-bad hack ! */
261 timer_Stop(&fp->OpenTimer);
262 fp->OpenTimer.load = fp->open_mode * SECTICKS;
263 fp->OpenTimer.func = FsmOpenNow;
264 fp->OpenTimer.arg = (void *)fp;
265 timer_Start(&fp->OpenTimer);
269 case ST_STOPPED: /* XXX: restart option */
273 case ST_OPENED: /* XXX: restart option */
275 case ST_CLOSING: /* XXX: restart option */
276 case ST_STOPPING: /* XXX: restart option */
277 NewState(fp, ST_STOPPING);
283 fsm_Up(struct fsm *fp)
287 log_Printf(fp->LogLevel, "FSM: Using \"%s\" as a transport\n",
289 NewState(fp, ST_CLOSED);
292 FsmInitRestartCounter(fp, FSM_REQ_TIMER);
293 FsmSendConfigReq(fp);
294 NewState(fp, ST_REQSENT);
297 log_Printf(fp->LogLevel, "%s: Oops, Up at %s\n",
298 fp->link->name, State2Nam(fp->state));
304 fsm_Down(struct fsm *fp)
308 NewState(fp, ST_INITIAL);
311 /* This TLF contradicts the RFC (1661), which ``misses it out'' ! */
312 (*fp->fn->LayerFinish)(fp);
313 NewState(fp, ST_INITIAL);
314 (*fp->parent->LayerFinish)(fp->parent->object, fp);
317 NewState(fp, ST_STARTING);
318 (*fp->fn->LayerStart)(fp);
319 (*fp->parent->LayerStart)(fp->parent->object, fp);
325 NewState(fp, ST_STARTING);
328 (*fp->fn->LayerDown)(fp);
329 NewState(fp, ST_STARTING);
330 (*fp->parent->LayerDown)(fp->parent->object, fp);
336 fsm_Close(struct fsm *fp)
340 (*fp->fn->LayerFinish)(fp);
341 NewState(fp, ST_INITIAL);
342 (*fp->parent->LayerFinish)(fp->parent->object, fp);
345 NewState(fp, ST_CLOSED);
348 NewState(fp, ST_CLOSING);
351 (*fp->fn->LayerDown)(fp);
352 if (fp->state == ST_OPENED) {
353 FsmInitRestartCounter(fp, FSM_TRM_TIMER);
354 FsmSendTerminateReq(fp);
355 NewState(fp, ST_CLOSING);
356 (*fp->parent->LayerDown)(fp->parent->object, fp);
362 FsmInitRestartCounter(fp, FSM_TRM_TIMER);
363 FsmSendTerminateReq(fp);
364 NewState(fp, ST_CLOSING);
373 FsmSendConfigReq(struct fsm *fp)
375 if (fp->more.reqs-- > 0 && fp->restart-- > 0) {
376 (*fp->fn->SendConfigReq)(fp);
377 timer_Start(&fp->FsmTimer); /* Start restart timer */
379 if (fp->more.reqs < 0)
380 log_Printf(LogPHASE, "%s: Too many %s REQs sent - abandoning "
381 "negotiation\n", fp->link->name, fp->name);
382 lcp_SendIdentification(&fp->link->lcp);
388 FsmSendTerminateReq(struct fsm *fp)
390 fsm_Output(fp, CODE_TERMREQ, fp->reqid, NULL, 0, MB_UNKNOWN);
391 (*fp->fn->SentTerminateReq)(fp);
392 timer_Start(&fp->FsmTimer); /* Start restart timer */
393 fp->restart--; /* Decrement restart counter */
402 struct fsm *fp = (struct fsm *)v;
408 FsmSendTerminateReq(fp);
412 FsmSendConfigReq(fp);
415 FsmSendConfigReq(fp);
416 NewState(fp, ST_REQSENT);
419 timer_Start(&fp->FsmTimer);
423 (*fp->fn->LayerFinish)(fp);
424 NewState(fp, ST_CLOSED);
425 (*fp->parent->LayerFinish)(fp->parent->object, fp);
428 (*fp->fn->LayerFinish)(fp);
429 NewState(fp, ST_STOPPED);
430 (*fp->parent->LayerFinish)(fp->parent->object, fp);
432 case ST_REQSENT: /* XXX: 3p */
435 (*fp->fn->LayerFinish)(fp);
436 NewState(fp, ST_STOPPED);
437 (*fp->parent->LayerFinish)(fp->parent->object, fp);
444 FsmInitRestartCounter(struct fsm *fp, int what)
446 timer_Stop(&fp->FsmTimer);
447 fp->FsmTimer.func = FsmTimeout;
448 fp->FsmTimer.arg = (void *)fp;
449 (*fp->fn->InitRestartCounter)(fp, what);
453 * Actions when receive packets
456 FsmRecvConfigReq(struct fsm *fp, struct fsmheader *lhp, struct mbuf *bp)
459 struct fsm_decode dec;
464 flen = ntohs(lhp->length) - sizeof *lhp;
466 log_Printf(LogWARN, "%s: FsmRecvConfigReq: plen (%d) < flen (%d)\n",
467 fp->link->name, plen, flen);
472 /* Check and process easy case */
475 if (fp->proto == PROTO_CCP && fp->link->lcp.fsm.state == ST_OPENED) {
477 * ccp_SetOpenMode() leaves us in initial if we're disabling
478 * & denying everything.
480 bp = m_prepend(bp, lhp, sizeof *lhp, 2);
481 bp = proto_Prepend(bp, fp->proto, 0, 0);
483 lcp_SendProtoRej(&fp->link->lcp, MBUF_CTOP(bp), bp->m_len);
489 log_Printf(fp->LogLevel, "%s: Oops, RCR in %s.\n",
490 fp->link->name, State2Nam(fp->state));
494 (*fp->fn->SendTerminateAck)(fp, lhp->id);
498 log_Printf(fp->LogLevel, "%s: Error: Got ConfigReq while state = %s\n",
499 fp->link->name, State2Nam(fp->state));
504 (*fp->fn->LayerDown)(fp);
505 (*fp->parent->LayerDown)(fp->parent->object, fp);
510 dec.ackend = dec.ack;
511 dec.nakend = dec.nak;
512 dec.rejend = dec.rej;
513 (*fp->fn->DecodeConfig)(fp, MBUF_CTOP(bp), flen, MODE_REQ, &dec);
514 if (flen < sizeof(struct fsmconfig))
515 log_Printf(fp->LogLevel, " [EMPTY]\n");
517 if (dec.nakend == dec.nak && dec.rejend == dec.rej)
522 FsmInitRestartCounter(fp, FSM_REQ_TIMER);
526 FsmSendConfigReq(fp);
530 if (dec.rejend != dec.rej)
531 fsm_Output(fp, CODE_CONFIGREJ, lhp->id, dec.rej, dec.rejend - dec.rej,
533 if (dec.nakend != dec.nak)
534 fsm_Output(fp, CODE_CONFIGNAK, lhp->id, dec.nak, dec.nakend - dec.nak,
537 fsm_Output(fp, CODE_CONFIGACK, lhp->id, dec.ack, dec.ackend - dec.ack,
543 * According to the RFC (1661) state transition table, a TLS isn't
544 * required for a RCR when state == ST_STOPPED, but the RFC
545 * must be wrong as TLS hasn't yet been called (since the last TLF)
547 (*fp->fn->LayerStart)(fp);
548 (*fp->parent->LayerStart)(fp->parent->object, fp);
553 NewState(fp, ST_ACKSENT);
555 NewState(fp, ST_REQSENT);
559 NewState(fp, ST_ACKSENT);
563 NewState(fp, ST_OPENED);
564 if ((*fp->fn->LayerUp)(fp))
565 (*fp->parent->LayerUp)(fp->parent->object, fp);
567 (*fp->fn->LayerDown)(fp);
568 FsmInitRestartCounter(fp, FSM_TRM_TIMER);
569 FsmSendTerminateReq(fp);
570 NewState(fp, ST_CLOSING);
571 lcp_SendIdentification(&fp->link->lcp);
577 NewState(fp, ST_REQSENT);
582 if (dec.rejend != dec.rej && --fp->more.rejs <= 0) {
583 log_Printf(LogPHASE, "%s: Too many %s REJs sent - abandoning negotiation\n",
584 fp->link->name, fp->name);
585 lcp_SendIdentification(&fp->link->lcp);
589 if (dec.nakend != dec.nak && --fp->more.naks <= 0) {
590 log_Printf(LogPHASE, "%s: Too many %s NAKs sent - abandoning negotiation\n",
591 fp->link->name, fp->name);
592 lcp_SendIdentification(&fp->link->lcp);
598 FsmRecvConfigAck(struct fsm *fp, struct fsmheader *lhp, struct mbuf *bp)
604 (*fp->fn->SendTerminateAck)(fp, lhp->id);
610 FsmInitRestartCounter(fp, FSM_REQ_TIMER);
611 NewState(fp, ST_ACKRCVD);
614 FsmSendConfigReq(fp);
615 NewState(fp, ST_REQSENT);
618 FsmInitRestartCounter(fp, FSM_REQ_TIMER);
619 NewState(fp, ST_OPENED);
620 if ((*fp->fn->LayerUp)(fp))
621 (*fp->parent->LayerUp)(fp->parent->object, fp);
623 (*fp->fn->LayerDown)(fp);
624 FsmInitRestartCounter(fp, FSM_TRM_TIMER);
625 FsmSendTerminateReq(fp);
626 NewState(fp, ST_CLOSING);
627 lcp_SendIdentification(&fp->link->lcp);
631 (*fp->fn->LayerDown)(fp);
632 FsmSendConfigReq(fp);
633 NewState(fp, ST_REQSENT);
634 (*fp->parent->LayerDown)(fp->parent->object, fp);
641 FsmRecvConfigNak(struct fsm *fp, struct fsmheader *lhp, struct mbuf *bp)
644 struct fsm_decode dec;
648 flen = ntohs(lhp->length) - sizeof *lhp;
655 * Check and process easy case
660 log_Printf(fp->LogLevel, "%s: Oops, RCN in %s.\n",
661 fp->link->name, State2Nam(fp->state));
666 (*fp->fn->SendTerminateAck)(fp, lhp->id);
676 dec.ackend = dec.ack;
677 dec.nakend = dec.nak;
678 dec.rejend = dec.rej;
679 (*fp->fn->DecodeConfig)(fp, MBUF_CTOP(bp), flen, MODE_NAK, &dec);
680 if (flen < sizeof(struct fsmconfig))
681 log_Printf(fp->LogLevel, " [EMPTY]\n");
686 FsmInitRestartCounter(fp, FSM_REQ_TIMER);
687 FsmSendConfigReq(fp);
690 (*fp->fn->LayerDown)(fp);
691 FsmSendConfigReq(fp);
692 NewState(fp, ST_REQSENT);
693 (*fp->parent->LayerDown)(fp->parent->object, fp);
696 FsmSendConfigReq(fp);
697 NewState(fp, ST_REQSENT);
705 FsmRecvTermReq(struct fsm *fp, struct fsmheader *lhp, struct mbuf *bp)
711 log_Printf(fp->LogLevel, "%s: Oops, RTR in %s\n",
712 fp->link->name, State2Nam(fp->state));
719 (*fp->fn->SendTerminateAck)(fp, lhp->id);
723 (*fp->fn->SendTerminateAck)(fp, lhp->id);
724 NewState(fp, ST_REQSENT);
727 (*fp->fn->LayerDown)(fp);
728 (*fp->fn->SendTerminateAck)(fp, lhp->id);
729 FsmInitRestartCounter(fp, FSM_TRM_TIMER);
730 timer_Start(&fp->FsmTimer); /* Start restart timer */
732 NewState(fp, ST_STOPPING);
733 (*fp->parent->LayerDown)(fp->parent->object, fp);
734 /* A delayed ST_STOPPED is now scheduled */
741 FsmRecvTermAck(struct fsm *fp, struct fsmheader *lhp, struct mbuf *bp)
746 (*fp->fn->LayerFinish)(fp);
747 NewState(fp, ST_CLOSED);
748 (*fp->parent->LayerFinish)(fp->parent->object, fp);
751 (*fp->fn->LayerFinish)(fp);
752 NewState(fp, ST_STOPPED);
753 (*fp->parent->LayerFinish)(fp->parent->object, fp);
756 NewState(fp, ST_REQSENT);
759 (*fp->fn->LayerDown)(fp);
760 FsmSendConfigReq(fp);
761 NewState(fp, ST_REQSENT);
762 (*fp->parent->LayerDown)(fp->parent->object, fp);
769 FsmRecvConfigRej(struct fsm *fp, struct fsmheader *lhp, struct mbuf *bp)
772 struct fsm_decode dec;
776 flen = ntohs(lhp->length) - sizeof *lhp;
782 lcp_SendIdentification(&fp->link->lcp);
785 * Check and process easy case
790 log_Printf(fp->LogLevel, "%s: Oops, RCJ in %s.\n",
791 fp->link->name, State2Nam(fp->state));
796 (*fp->fn->SendTerminateAck)(fp, lhp->id);
806 dec.ackend = dec.ack;
807 dec.nakend = dec.nak;
808 dec.rejend = dec.rej;
809 (*fp->fn->DecodeConfig)(fp, MBUF_CTOP(bp), flen, MODE_REJ, &dec);
810 if (flen < sizeof(struct fsmconfig))
811 log_Printf(fp->LogLevel, " [EMPTY]\n");
816 FsmInitRestartCounter(fp, FSM_REQ_TIMER);
817 FsmSendConfigReq(fp);
820 (*fp->fn->LayerDown)(fp);
821 FsmSendConfigReq(fp);
822 NewState(fp, ST_REQSENT);
823 (*fp->parent->LayerDown)(fp->parent->object, fp);
826 FsmSendConfigReq(fp);
827 NewState(fp, ST_REQSENT);
834 FsmRecvCodeRej(struct fsm *fp, struct fsmheader *lhp, struct mbuf *bp)
840 FsmRecvProtoRej(struct fsm *fp, struct fsmheader *lhp, struct mbuf *bp)
842 struct physical *p = link2physical(fp->link);
845 if (m_length(bp) < 2) {
849 bp = mbuf_Read(bp, &proto, 2);
850 proto = ntohs(proto);
851 log_Printf(fp->LogLevel, "%s: -- Protocol 0x%04x (%s) was rejected!\n",
852 fp->link->name, proto, hdlc_Protocol2Nam(proto));
857 lqr_Stop(p, LQM_LQR);
859 log_Printf(LogERROR, "%s: FsmRecvProtoRej: Not a physical link !\n",
863 if (fp->proto == PROTO_LCP) {
864 fp = &fp->link->ccp.fsm;
865 /* Despite the RFC (1661), don't do an out-of-place TLF */
866 /* (*fp->fn->LayerFinish)(fp); */
870 NewState(fp, ST_CLOSED);
872 NewState(fp, ST_STOPPED);
876 /* (*fp->parent->LayerFinish)(fp->parent->object, fp); */
880 if (fp->proto == PROTO_LCP) {
881 log_Printf(LogPHASE, "%s: IPCP protocol reject closes IPCP !\n",
883 fsm_Close(&fp->bundle->ncp.ipcp.fsm);
887 if (fp->proto == PROTO_LCP) {
888 struct lcp *lcp = fsm2lcp(fp);
890 if (lcp->want_mrru && lcp->his_mrru) {
891 log_Printf(LogPHASE, "%s: MP protocol reject is fatal !\n",
902 FsmRecvEchoReq(struct fsm *fp, struct fsmheader *lhp, struct mbuf *bp)
904 struct lcp *lcp = fsm2lcp(fp);
909 m_settype(bp, MB_ECHOIN);
911 if (lcp && ntohs(lhp->length) - sizeof *lhp >= 4) {
913 ua_ntohl(cp, &magic);
914 if (magic != lcp->his_magic) {
915 log_Printf(fp->LogLevel, "%s: RecvEchoReq: magic 0x%08lx is wrong,"
916 " expecting 0x%08lx\n", fp->link->name, (u_long)magic,
917 (u_long)lcp->his_magic);
918 /* XXX: We should send terminate request */
920 if (fp->state == ST_OPENED) {
921 ua_htonl(&lcp->want_magic, cp); /* local magic */
922 fsm_Output(fp, CODE_ECHOREP, lhp->id, cp,
923 ntohs(lhp->length) - sizeof *lhp, MB_ECHOOUT);
930 FsmRecvEchoRep(struct fsm *fp, struct fsmheader *lhp, struct mbuf *bp)
933 bp = lqr_RecvEcho(fp, bp);
939 FsmRecvDiscReq(struct fsm *fp, struct fsmheader *lhp, struct mbuf *bp)
945 FsmRecvIdent(struct fsm *fp, struct fsmheader *lhp, struct mbuf *bp)
951 len = ntohs(lhp->length) - sizeof *lhp;
953 bp = m_pullup(m_append(bp, "", 1));
955 ua_ntohl(cp, &magic);
956 if (magic != fp->link->lcp.his_magic)
957 log_Printf(fp->LogLevel, "%s: RecvIdent: magic 0x%08lx is wrong,"
958 " expecting 0x%08lx\n", fp->link->name, (u_long)magic,
959 (u_long)fp->link->lcp.his_magic);
961 lcp_RecvIdentification(&fp->link->lcp, cp + 4);
967 FsmRecvTimeRemain(struct fsm *fp, struct fsmheader *lhp, struct mbuf *bp)
973 FsmRecvResetReq(struct fsm *fp, struct fsmheader *lhp, struct mbuf *bp)
975 (*fp->fn->RecvResetReq)(fp);
977 * All sendable compressed packets are queued in the first (lowest
978 * priority) modem output queue.... dump 'em to the priority queue
979 * so that they arrive at the peer before our ResetAck.
981 link_SequenceQueue(fp->link);
982 fsm_Output(fp, CODE_RESETACK, lhp->id, NULL, 0, MB_CCPOUT);
987 FsmRecvResetAck(struct fsm *fp, struct fsmheader *lhp, struct mbuf *bp)
989 (*fp->fn->RecvResetAck)(fp, lhp->id);
994 fsm_Input(struct fsm *fp, struct mbuf *bp)
998 const struct fsmcodedesc *codep;
1001 if (len < sizeof(struct fsmheader)) {
1005 bp = mbuf_Read(bp, &lh, sizeof lh);
1007 if (ntohs(lh.length) > len) {
1008 log_Printf(LogWARN, "%s: Oops: Got %d bytes but %d byte payload "
1009 "- dropped\n", fp->link->name, len, (int)ntohs(lh.length));
1014 if (lh.code < fp->min_code || lh.code > fp->max_code ||
1015 lh.code > sizeof FsmCodes / sizeof *FsmCodes) {
1017 * Use a private id. This is really a response-type packet, but we
1018 * MUST send a unique id for each REQ....
1022 bp = m_prepend(bp, &lh, sizeof lh, 0);
1024 fsm_Output(fp, CODE_CODEREJ, id++, MBUF_CTOP(bp), bp->m_len, MB_UNKNOWN);
1029 codep = FsmCodes + lh.code - 1;
1030 if (lh.id != fp->reqid && codep->check_reqid &&
1031 Enabled(fp->bundle, OPT_IDCHECK)) {
1032 log_Printf(fp->LogLevel, "%s: Recv%s(%d), dropped (expected %d)\n",
1033 fp->link->name, codep->name, lh.id, fp->reqid);
1037 log_Printf(fp->LogLevel, "%s: Recv%s(%d) state = %s\n",
1038 fp->link->name, codep->name, lh.id, State2Nam(fp->state));
1040 if (codep->inc_reqid && (lh.id == fp->reqid ||
1041 (!Enabled(fp->bundle, OPT_IDCHECK) && codep->check_reqid)))
1042 fp->reqid++; /* That's the end of that ``exchange''.... */
1044 (*codep->recv)(fp, &lh, bp);
1048 fsm_NullRecvResetReq(struct fsm *fp)
1050 log_Printf(fp->LogLevel, "%s: Oops - received unexpected reset req\n",
1055 fsm_NullRecvResetAck(struct fsm *fp, u_char id)
1057 log_Printf(fp->LogLevel, "%s: Oops - received unexpected reset ack\n",
1062 fsm_Reopen(struct fsm *fp)
1064 if (fp->state == ST_OPENED) {
1065 (*fp->fn->LayerDown)(fp);
1066 FsmInitRestartCounter(fp, FSM_REQ_TIMER);
1067 FsmSendConfigReq(fp);
1068 NewState(fp, ST_REQSENT);
1069 (*fp->parent->LayerDown)(fp->parent->object, fp);
1074 fsm2initial(struct fsm *fp)
1076 timer_Stop(&fp->FsmTimer);
1077 timer_Stop(&fp->OpenTimer);
1078 timer_Stop(&fp->StoppedTimer);
1079 if (fp->state == ST_STOPPED)
1081 if (fp->state > ST_INITIAL)
1083 if (fp->state > ST_INITIAL)