1 /*****************************************************************************
2 * fsm.c - Network Control Protocol Finite State Machine program file.
4 * Copyright (c) 2003 by Marc Boucher, Services Informatiques (MBSI) inc.
5 * portions Copyright (c) 1997 by Global Election Systems Inc.
7 * The authors hereby grant permission to use, copy, modify, distribute,
8 * and license this software and its documentation for any purpose, provided
9 * that existing copyright notices are retained in all copies and that this
10 * notice and the following disclaimer are included verbatim in any
11 * distributions. No written agreement, license, or royalty fee is required
12 * for any of the authorized uses.
14 * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS *AS IS* AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 ******************************************************************************
28 * 03-01-01 Marc Boucher <marc@mbsi.ca>
30 * 97-12-01 Guy Lancaster <lancasterg@acm.org>, Global Election Systems Inc.
31 * Original based on BSD fsm.c.
32 *****************************************************************************/
34 * fsm.c - {Link, IP} Control Protocol Finite State Machine.
36 * Copyright (c) 1989 Carnegie Mellon University.
37 * All rights reserved.
39 * Redistribution and use in source and binary forms are permitted
40 * provided that the above copyright notice and this paragraph are
41 * duplicated in all such forms and that any documentation,
42 * advertising materials, and other materials related to such
43 * distribution and use acknowledge that the software was developed
44 * by Carnegie Mellon University. The name of the
45 * University may not be used to endorse or promote products derived
46 * from this software without specific prior written permission.
47 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
48 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
49 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
54 * Randomize fsm id on link/init.
55 * Deal with variable outgoing MTU.
60 #if PPP_SUPPORT /* don't build if not configured for use in lwipopts.h */
68 /*************************/
69 /*** LOCAL DEFINITIONS ***/
70 /*************************/
74 static const char *ppperr_strerr[] = {
75 "LS_INITIAL", /* LS_INITIAL 0 */
76 "LS_STARTING", /* LS_STARTING 1 */
77 "LS_CLOSED", /* LS_CLOSED 2 */
78 "LS_STOPPED", /* LS_STOPPED 3 */
79 "LS_CLOSING", /* LS_CLOSING 4 */
80 "LS_STOPPING", /* LS_STOPPING 5 */
81 "LS_REQSENT", /* LS_REQSENT 6 */
82 "LS_ACKRCVD", /* LS_ACKRCVD 7 */
83 "LS_ACKSENT", /* LS_ACKSENT 8 */
84 "LS_OPENED" /* LS_OPENED 9 */
87 #endif /* PPP_DEBUG */
89 /************************/
90 /*** LOCAL DATA TYPES ***/
91 /************************/
94 /***********************************/
95 /*** LOCAL FUNCTION DECLARATIONS ***/
96 /***********************************/
97 static void fsm_timeout (void *);
98 static void fsm_rconfreq (fsm *, u_char, u_char *, int);
99 static void fsm_rconfack (fsm *, int, u_char *, int);
100 static void fsm_rconfnakrej (fsm *, int, int, u_char *, int);
101 static void fsm_rtermreq (fsm *, int, u_char *, int);
102 static void fsm_rtermack (fsm *);
103 static void fsm_rcoderej (fsm *, u_char *, int);
104 static void fsm_sconfreq (fsm *, int);
106 #define PROTO_NAME(f) ((f)->callbacks->proto_name)
109 /******************************/
110 /*** PUBLIC DATA STRUCTURES ***/
111 /******************************/
114 /*****************************/
115 /*** LOCAL DATA STRUCTURES ***/
116 /*****************************/
117 int peer_mru[NUM_PPP];
120 /***********************************/
121 /*** PUBLIC FUNCTION DEFINITIONS ***/
122 /***********************************/
125 * fsm_init - Initialize fsm.
127 * Initialize fsm state.
132 f->state = LS_INITIAL;
134 f->id = 0; /* XXX Start with random id? */
135 f->timeouttime = FSM_DEFTIMEOUT;
136 f->maxconfreqtransmits = FSM_DEFMAXCONFREQS;
137 f->maxtermtransmits = FSM_DEFMAXTERMREQS;
138 f->maxnakloops = FSM_DEFMAXNAKLOOPS;
139 f->term_reason_len = 0;
144 * fsm_lowerup - The lower layer is up.
149 int oldState = f->state;
151 LWIP_UNUSED_ARG(oldState);
155 f->state = LS_CLOSED;
159 if( f->flags & OPT_SILENT ) {
160 f->state = LS_STOPPED;
162 /* Send an initial configure-request */
164 f->state = LS_REQSENT;
169 FSMDEBUG((LOG_INFO, "%s: Up event in state %d (%s)!\n",
170 PROTO_NAME(f), f->state, ppperr_strerr[f->state]));
173 FSMDEBUG((LOG_INFO, "%s: lowerup state %d (%s) -> %d (%s)\n",
174 PROTO_NAME(f), oldState, ppperr_strerr[oldState], f->state, ppperr_strerr[f->state]));
179 * fsm_lowerdown - The lower layer is down.
181 * Cancel all timeouts and inform upper layers.
184 fsm_lowerdown(fsm *f)
186 int oldState = f->state;
188 LWIP_UNUSED_ARG(oldState);
192 f->state = LS_INITIAL;
196 f->state = LS_STARTING;
197 if( f->callbacks->starting ) {
198 (*f->callbacks->starting)(f);
203 f->state = LS_INITIAL;
204 UNTIMEOUT(fsm_timeout, f); /* Cancel timeout */
211 f->state = LS_STARTING;
212 UNTIMEOUT(fsm_timeout, f); /* Cancel timeout */
216 if( f->callbacks->down ) {
217 (*f->callbacks->down)(f);
219 f->state = LS_STARTING;
223 FSMDEBUG((LOG_INFO, "%s: Down event in state %d (%s)!\n",
224 PROTO_NAME(f), f->state, ppperr_strerr[f->state]));
227 FSMDEBUG((LOG_INFO, "%s: lowerdown state %d (%s) -> %d (%s)\n",
228 PROTO_NAME(f), oldState, ppperr_strerr[oldState], f->state, ppperr_strerr[f->state]));
233 * fsm_open - Link is allowed to come up.
238 int oldState = f->state;
240 LWIP_UNUSED_ARG(oldState);
244 f->state = LS_STARTING;
245 if( f->callbacks->starting ) {
246 (*f->callbacks->starting)(f);
251 if( f->flags & OPT_SILENT ) {
252 f->state = LS_STOPPED;
254 /* Send an initial configure-request */
256 f->state = LS_REQSENT;
261 f->state = LS_STOPPING;
265 if( f->flags & OPT_RESTART ) {
272 FSMDEBUG((LOG_INFO, "%s: open state %d (%s) -> %d (%s)\n",
273 PROTO_NAME(f), oldState, ppperr_strerr[oldState], f->state, ppperr_strerr[f->state]));
278 * fsm_close - Start closing connection.
280 * Cancel timeouts and either initiate close or possibly go directly to
281 * the LS_CLOSED state.
284 fsm_close(fsm *f, char *reason)
286 int oldState = f->state;
288 LWIP_UNUSED_ARG(oldState);
290 f->term_reason = reason;
291 f->term_reason_len = (reason == NULL? 0: strlen(reason));
294 f->state = LS_INITIAL;
297 f->state = LS_CLOSED;
300 f->state = LS_CLOSING;
307 if( f->state != LS_OPENED ) {
308 UNTIMEOUT(fsm_timeout, f); /* Cancel timeout */
309 } else if( f->callbacks->down ) {
310 (*f->callbacks->down)(f); /* Inform upper layers we're down */
312 /* Init restart counter, send Terminate-Request */
313 f->retransmits = f->maxtermtransmits;
314 fsm_sdata(f, TERMREQ, f->reqid = ++f->id,
315 (u_char *) f->term_reason, f->term_reason_len);
316 TIMEOUT(fsm_timeout, f, f->timeouttime);
319 f->state = LS_CLOSING;
323 FSMDEBUG((LOG_INFO, "%s: close reason=%s state %d (%s) -> %d (%s)\n",
324 PROTO_NAME(f), reason, oldState, ppperr_strerr[oldState], f->state, ppperr_strerr[f->state]));
329 * fsm_sdata - Send some data.
331 * Used for all packets sent to our peer by this module.
334 fsm_sdata( fsm *f, u_char code, u_char id, u_char *data, int datalen)
339 /* Adjust length to be smaller than MTU */
340 outp = outpacket_buf[f->unit];
341 if (datalen > peer_mru[f->unit] - (int)HEADERLEN) {
342 datalen = peer_mru[f->unit] - HEADERLEN;
344 if (datalen && data != outp + PPP_HDRLEN + HEADERLEN) {
345 BCOPY(data, outp + PPP_HDRLEN + HEADERLEN, datalen);
347 outlen = datalen + HEADERLEN;
348 MAKEHEADER(outp, f->protocol);
351 PUTSHORT(outlen, outp);
352 pppWrite(f->unit, outpacket_buf[f->unit], outlen + PPP_HDRLEN);
353 FSMDEBUG((LOG_INFO, "fsm_sdata(%s): Sent code %d,%d,%d.\n",
354 PROTO_NAME(f), code, id, outlen));
359 * fsm_input - Input packet.
362 fsm_input(fsm *f, u_char *inpacket, int l)
364 u_char *inp = inpacket;
369 * Parse header (code, id and length).
370 * If packet too short, drop it.
373 FSMDEBUG((LOG_WARNING, "fsm_input(%x): Rcvd short header.\n",
380 if (len < HEADERLEN) {
381 FSMDEBUG((LOG_INFO, "fsm_input(%x): Rcvd illegal length.\n",
386 FSMDEBUG((LOG_INFO, "fsm_input(%x): Rcvd short packet.\n",
390 len -= HEADERLEN; /* subtract header length */
392 if( f->state == LS_INITIAL || f->state == LS_STARTING ) {
393 FSMDEBUG((LOG_INFO, "fsm_input(%x): Rcvd packet in state %d (%s).\n",
394 f->protocol, f->state, ppperr_strerr[f->state]));
397 FSMDEBUG((LOG_INFO, "fsm_input(%s):%d,%d,%d\n", PROTO_NAME(f), code, id, l));
399 * Action depends on code.
403 fsm_rconfreq(f, id, inp, len);
407 fsm_rconfack(f, id, inp, len);
412 fsm_rconfnakrej(f, code, id, inp, len);
416 fsm_rtermreq(f, id, inp, len);
424 fsm_rcoderej(f, inp, len);
428 if( !f->callbacks->extcode ||
429 !(*f->callbacks->extcode)(f, code, id, inp, len) ) {
430 fsm_sdata(f, CODEREJ, ++f->id, inpacket, len + HEADERLEN);
438 * fsm_protreject - Peer doesn't speak this protocol.
440 * Treat this as a catastrophic error (RXJ-).
443 fsm_protreject(fsm *f)
447 UNTIMEOUT(fsm_timeout, f); /* Cancel timeout */
450 f->state = LS_CLOSED;
451 if( f->callbacks->finished ) {
452 (*f->callbacks->finished)(f);
460 UNTIMEOUT(fsm_timeout, f); /* Cancel timeout */
463 f->state = LS_STOPPED;
464 if( f->callbacks->finished ) {
465 (*f->callbacks->finished)(f);
470 if( f->callbacks->down ) {
471 (*f->callbacks->down)(f);
473 /* Init restart counter, send Terminate-Request */
474 f->retransmits = f->maxtermtransmits;
475 fsm_sdata(f, TERMREQ, f->reqid = ++f->id,
476 (u_char *) f->term_reason, f->term_reason_len);
477 TIMEOUT(fsm_timeout, f, f->timeouttime);
480 f->state = LS_STOPPING;
484 FSMDEBUG((LOG_INFO, "%s: Protocol-reject event in state %d (%s)!\n",
485 PROTO_NAME(f), f->state, ppperr_strerr[f->state]));
493 /**********************************/
494 /*** LOCAL FUNCTION DEFINITIONS ***/
495 /**********************************/
498 * fsm_timeout - Timeout expired.
501 fsm_timeout(void *arg)
503 fsm *f = (fsm *) arg;
508 if( f->retransmits <= 0 ) {
509 FSMDEBUG((LOG_WARNING, "%s: timeout sending Terminate-Request state=%d (%s)\n",
510 PROTO_NAME(f), f->state, ppperr_strerr[f->state]));
512 * We've waited for an ack long enough. Peer probably heard us.
514 f->state = (f->state == LS_CLOSING)? LS_CLOSED: LS_STOPPED;
515 if( f->callbacks->finished ) {
516 (*f->callbacks->finished)(f);
519 FSMDEBUG((LOG_WARNING, "%s: timeout resending Terminate-Requests state=%d (%s)\n",
520 PROTO_NAME(f), f->state, ppperr_strerr[f->state]));
521 /* Send Terminate-Request */
522 fsm_sdata(f, TERMREQ, f->reqid = ++f->id,
523 (u_char *) f->term_reason, f->term_reason_len);
524 TIMEOUT(fsm_timeout, f, f->timeouttime);
532 if (f->retransmits <= 0) {
533 FSMDEBUG((LOG_WARNING, "%s: timeout sending Config-Requests state=%d (%s)\n",
534 PROTO_NAME(f), f->state, ppperr_strerr[f->state]));
535 f->state = LS_STOPPED;
536 if( (f->flags & OPT_PASSIVE) == 0 && f->callbacks->finished ) {
537 (*f->callbacks->finished)(f);
540 FSMDEBUG((LOG_WARNING, "%s: timeout resending Config-Request state=%d (%s)\n",
541 PROTO_NAME(f), f->state, ppperr_strerr[f->state]));
542 /* Retransmit the configure-request */
543 if (f->callbacks->retransmit) {
544 (*f->callbacks->retransmit)(f);
546 fsm_sconfreq(f, 1); /* Re-send Configure-Request */
547 if( f->state == LS_ACKRCVD ) {
548 f->state = LS_REQSENT;
554 FSMDEBUG((LOG_INFO, "%s: Timeout event in state %d (%s)!\n",
555 PROTO_NAME(f), f->state, ppperr_strerr[f->state]));
561 * fsm_rconfreq - Receive Configure-Request.
564 fsm_rconfreq(fsm *f, u_char id, u_char *inp, int len)
566 int code, reject_if_disagree;
568 FSMDEBUG((LOG_INFO, "fsm_rconfreq(%s): Rcvd id %d state=%d (%s)\n",
569 PROTO_NAME(f), id, f->state, ppperr_strerr[f->state]));
572 /* Go away, we're closed */
573 fsm_sdata(f, TERMACK, id, NULL, 0);
580 /* Go down and restart negotiation */
581 if( f->callbacks->down ) {
582 (*f->callbacks->down)(f); /* Inform upper layers */
584 fsm_sconfreq(f, 0); /* Send initial Configure-Request */
588 /* Negotiation started by our peer */
589 fsm_sconfreq(f, 0); /* Send initial Configure-Request */
590 f->state = LS_REQSENT;
595 * Pass the requested configuration options
596 * to protocol-specific code for checking.
598 if (f->callbacks->reqci) { /* Check CI */
599 reject_if_disagree = (f->nakloops >= f->maxnakloops);
600 code = (*f->callbacks->reqci)(f, inp, &len, reject_if_disagree);
602 code = CONFREJ; /* Reject all CI */
607 /* send the Ack, Nak or Rej to the peer */
608 fsm_sdata(f, (u_char)code, id, inp, len);
610 if (code == CONFACK) {
611 if (f->state == LS_ACKRCVD) {
612 UNTIMEOUT(fsm_timeout, f); /* Cancel timeout */
613 f->state = LS_OPENED;
614 if (f->callbacks->up) {
615 (*f->callbacks->up)(f); /* Inform upper layers */
618 f->state = LS_ACKSENT;
622 /* we sent CONFACK or CONFREJ */
623 if (f->state != LS_ACKRCVD) {
624 f->state = LS_REQSENT;
626 if( code == CONFNAK ) {
634 * fsm_rconfack - Receive Configure-Ack.
637 fsm_rconfack(fsm *f, int id, u_char *inp, int len)
639 FSMDEBUG((LOG_INFO, "fsm_rconfack(%s): Rcvd id %d state=%d (%s)\n",
640 PROTO_NAME(f), id, f->state, ppperr_strerr[f->state]));
642 if (id != f->reqid || f->seen_ack) { /* Expected id? */
643 return; /* Nope, toss... */
645 if( !(f->callbacks->ackci? (*f->callbacks->ackci)(f, inp, len): (len == 0)) ) {
646 /* Ack is bad - ignore it */
647 FSMDEBUG((LOG_INFO, "%s: received bad Ack (length %d)\n",
648 PROTO_NAME(f), len));
656 fsm_sdata(f, TERMACK, (u_char)id, NULL, 0);
660 f->state = LS_ACKRCVD;
661 f->retransmits = f->maxconfreqtransmits;
665 /* Huh? an extra valid Ack? oh well... */
666 UNTIMEOUT(fsm_timeout, f); /* Cancel timeout */
668 f->state = LS_REQSENT;
672 UNTIMEOUT(fsm_timeout, f); /* Cancel timeout */
673 f->state = LS_OPENED;
674 f->retransmits = f->maxconfreqtransmits;
675 if (f->callbacks->up) {
676 (*f->callbacks->up)(f); /* Inform upper layers */
681 /* Go down and restart negotiation */
682 if (f->callbacks->down) {
683 (*f->callbacks->down)(f); /* Inform upper layers */
685 fsm_sconfreq(f, 0); /* Send initial Configure-Request */
686 f->state = LS_REQSENT;
693 * fsm_rconfnakrej - Receive Configure-Nak or Configure-Reject.
696 fsm_rconfnakrej(fsm *f, int code, int id, u_char *inp, int len)
698 int (*proc) (fsm *, u_char *, int);
701 FSMDEBUG((LOG_INFO, "fsm_rconfnakrej(%s): Rcvd id %d state=%d (%s)\n",
702 PROTO_NAME(f), id, f->state, ppperr_strerr[f->state]));
704 if (id != f->reqid || f->seen_ack) { /* Expected id? */
705 return; /* Nope, toss... */
707 proc = (code == CONFNAK)? f->callbacks->nakci: f->callbacks->rejci;
708 if (!proc || !((ret = proc(f, inp, len)))) {
709 /* Nak/reject is bad - ignore it */
710 FSMDEBUG((LOG_INFO, "%s: received bad %s (length %d)\n",
711 PROTO_NAME(f), (code==CONFNAK? "Nak": "reject"), len));
719 fsm_sdata(f, TERMACK, (u_char)id, NULL, 0);
724 /* They didn't agree to what we wanted - try another request */
725 UNTIMEOUT(fsm_timeout, f); /* Cancel timeout */
727 f->state = LS_STOPPED; /* kludge for stopping CCP */
729 fsm_sconfreq(f, 0); /* Send Configure-Request */
734 /* Got a Nak/reject when we had already had an Ack?? oh well... */
735 UNTIMEOUT(fsm_timeout, f); /* Cancel timeout */
737 f->state = LS_REQSENT;
741 /* Go down and restart negotiation */
742 if (f->callbacks->down) {
743 (*f->callbacks->down)(f); /* Inform upper layers */
745 fsm_sconfreq(f, 0); /* Send initial Configure-Request */
746 f->state = LS_REQSENT;
753 * fsm_rtermreq - Receive Terminate-Req.
756 fsm_rtermreq(fsm *f, int id, u_char *p, int len)
760 FSMDEBUG((LOG_INFO, "fsm_rtermreq(%s): Rcvd id %d state=%d (%s)\n",
761 PROTO_NAME(f), id, f->state, ppperr_strerr[f->state]));
766 f->state = LS_REQSENT; /* Start over but keep trying */
771 FSMDEBUG((LOG_INFO, "%s terminated by peer (%x)\n", PROTO_NAME(f), p));
773 FSMDEBUG((LOG_INFO, "%s terminated by peer\n", PROTO_NAME(f)));
775 if (f->callbacks->down) {
776 (*f->callbacks->down)(f); /* Inform upper layers */
779 f->state = LS_STOPPING;
780 TIMEOUT(fsm_timeout, f, f->timeouttime);
784 fsm_sdata(f, TERMACK, (u_char)id, NULL, 0);
789 * fsm_rtermack - Receive Terminate-Ack.
794 FSMDEBUG((LOG_INFO, "fsm_rtermack(%s): state=%d (%s)\n",
795 PROTO_NAME(f), f->state, ppperr_strerr[f->state]));
799 UNTIMEOUT(fsm_timeout, f);
800 f->state = LS_CLOSED;
801 if( f->callbacks->finished ) {
802 (*f->callbacks->finished)(f);
807 UNTIMEOUT(fsm_timeout, f);
808 f->state = LS_STOPPED;
809 if( f->callbacks->finished ) {
810 (*f->callbacks->finished)(f);
815 f->state = LS_REQSENT;
819 if (f->callbacks->down) {
820 (*f->callbacks->down)(f); /* Inform upper layers */
829 * fsm_rcoderej - Receive an Code-Reject.
832 fsm_rcoderej(fsm *f, u_char *inp, int len)
836 FSMDEBUG((LOG_INFO, "fsm_rcoderej(%s): state=%d (%s)\n",
837 PROTO_NAME(f), f->state, ppperr_strerr[f->state]));
839 if (len < HEADERLEN) {
840 FSMDEBUG((LOG_INFO, "fsm_rcoderej: Rcvd short Code-Reject packet!\n"));
845 FSMDEBUG((LOG_WARNING, "%s: Rcvd Code-Reject for code %d, id %d\n",
846 PROTO_NAME(f), code, id));
848 if( f->state == LS_ACKRCVD ) {
849 f->state = LS_REQSENT;
855 * fsm_sconfreq - Send a Configure-Request.
858 fsm_sconfreq(fsm *f, int retransmit)
863 if( f->state != LS_REQSENT && f->state != LS_ACKRCVD && f->state != LS_ACKSENT ) {
864 /* Not currently negotiating - reset options */
865 if( f->callbacks->resetci ) {
866 (*f->callbacks->resetci)(f);
872 /* New request - reset retransmission counter, use new ID */
873 f->retransmits = f->maxconfreqtransmits;
880 * Make up the request packet
882 outp = outpacket_buf[f->unit] + PPP_HDRLEN + HEADERLEN;
883 if( f->callbacks->cilen && f->callbacks->addci ) {
884 cilen = (*f->callbacks->cilen)(f);
885 if( cilen > peer_mru[f->unit] - (int)HEADERLEN ) {
886 cilen = peer_mru[f->unit] - HEADERLEN;
888 if (f->callbacks->addci) {
889 (*f->callbacks->addci)(f, outp, &cilen);
895 /* send the request to our peer */
896 fsm_sdata(f, CONFREQ, f->reqid, outp, cilen);
898 /* start the retransmit timer */
900 TIMEOUT(fsm_timeout, f, f->timeouttime);
902 FSMDEBUG((LOG_INFO, "%s: sending Configure-Request, id %d\n",
903 PROTO_NAME(f), f->reqid));
906 #endif /* PPP_SUPPORT */