2 * fsm.c - {Link, IP} Control Protocol Finite State Machine.
4 * Copyright (c) 1989 Carnegie Mellon University.
7 * Redistribution and use in source and binary forms are permitted
8 * provided that the above copyright notice and this paragraph are
9 * duplicated in all such forms and that any documentation,
10 * advertising materials, and other materials related to such
11 * distribution and use acknowledge that the software was developed
12 * by Carnegie Mellon University. The name of the
13 * University may not be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 #define RCSID "$Id: fsm.c,v 1.18 2002/06/24 12:57:15 dfs Exp $"
24 * Randomize fsm id on link/init.
25 * Deal with variable outgoing MTU.
30 #include <sys/types.h>
35 static const char rcsid[] = RCSID;
37 static void fsm_timeout __P((void *));
38 static void fsm_rconfreq __P((fsm *, int, u_char *, int));
39 static void fsm_rconfack __P((fsm *, int, u_char *, int));
40 static void fsm_rconfnakrej __P((fsm *, int, int, u_char *, int));
41 static void fsm_rtermreq __P((fsm *, int, u_char *, int));
42 static void fsm_rtermack __P((fsm *));
43 static void fsm_rcoderej __P((fsm *, u_char *, int));
44 static void fsm_sconfreq __P((fsm *, int));
46 #define PROTO_NAME(f) ((f)->callbacks->proto_name)
48 int peer_mru[NUM_PPP];
52 * fsm_init - Initialize fsm.
54 * Initialize fsm state.
62 f->id = 0; /* XXX Start with random id? */
63 f->timeouttime = DEFTIMEOUT;
64 f->maxconfreqtransmits = DEFMAXCONFREQS;
65 f->maxtermtransmits = DEFMAXTERMREQS;
66 f->maxnakloops = DEFMAXNAKLOOPS;
67 f->term_reason_len = 0;
72 * fsm_lowerup - The lower layer is up.
84 if( f->flags & OPT_SILENT )
87 /* Send an initial configure-request */
94 FSMDEBUG(("%s: Up event in state %d!", PROTO_NAME(f), f->state));
100 * fsm_lowerdown - The lower layer is down.
102 * Cancel all timeouts and inform upper layers.
115 if( f->callbacks->starting )
116 (*f->callbacks->starting)(f);
121 UNTIMEOUT(fsm_timeout, f); /* Cancel timeout */
129 UNTIMEOUT(fsm_timeout, f); /* Cancel timeout */
133 if( f->callbacks->down )
134 (*f->callbacks->down)(f);
139 FSMDEBUG(("%s: Down event in state %d!", PROTO_NAME(f), f->state));
145 * fsm_open - Link is allowed to come up.
154 if( f->callbacks->starting )
155 (*f->callbacks->starting)(f);
159 if( f->flags & OPT_SILENT )
162 /* Send an initial configure-request */
173 if( f->flags & OPT_RESTART ){
183 * fsm_close - Start closing connection.
185 * Cancel timeouts and either initiate close or possibly go directly to
193 f->term_reason = reason;
194 f->term_reason_len = (reason == NULL? 0: strlen(reason));
210 if( f->state != OPENED )
211 UNTIMEOUT(fsm_timeout, f); /* Cancel timeout */
212 else if( f->callbacks->down )
213 (*f->callbacks->down)(f); /* Inform upper layers we're down */
215 /* Init restart counter, send Terminate-Request */
216 f->retransmits = f->maxtermtransmits;
217 fsm_sdata(f, TERMREQ, f->reqid = ++f->id,
218 (u_char *) f->term_reason, f->term_reason_len);
219 TIMEOUT(fsm_timeout, f, f->timeouttime);
229 * fsm_timeout - Timeout expired.
235 fsm *f = (fsm *) arg;
240 if( f->retransmits <= 0 ){
242 * We've waited for an ack long enough. Peer probably heard us.
244 f->state = (f->state == CLOSING)? CLOSED: STOPPED;
245 if( f->callbacks->finished )
246 (*f->callbacks->finished)(f);
248 /* Send Terminate-Request */
249 fsm_sdata(f, TERMREQ, f->reqid = ++f->id,
250 (u_char *) f->term_reason, f->term_reason_len);
251 TIMEOUT(fsm_timeout, f, f->timeouttime);
259 if (f->retransmits <= 0) {
260 warn("%s: timeout sending Config-Requests\n", PROTO_NAME(f));
262 if( (f->flags & OPT_PASSIVE) == 0 && f->callbacks->finished )
263 (*f->callbacks->finished)(f);
266 /* Retransmit the configure-request */
267 if (f->callbacks->retransmit)
268 (*f->callbacks->retransmit)(f);
269 fsm_sconfreq(f, 1); /* Re-send Configure-Request */
270 if( f->state == ACKRCVD )
276 FSMDEBUG(("%s: Timeout event in state %d!", PROTO_NAME(f), f->state));
282 * fsm_input - Input packet.
285 fsm_input(f, inpacket, l)
295 * Parse header (code, id and length).
296 * If packet too short, drop it.
300 FSMDEBUG(("fsm_input(%x): Rcvd short header.", f->protocol));
306 if (len < HEADERLEN) {
307 FSMDEBUG(("fsm_input(%x): Rcvd illegal length.", f->protocol));
311 FSMDEBUG(("fsm_input(%x): Rcvd short packet.", f->protocol));
314 len -= HEADERLEN; /* subtract header length */
316 if( f->state == INITIAL || f->state == STARTING ){
317 FSMDEBUG(("fsm_input(%x): Rcvd packet in state %d.",
318 f->protocol, f->state));
323 * Action depends on code.
327 fsm_rconfreq(f, id, inp, len);
331 fsm_rconfack(f, id, inp, len);
336 fsm_rconfnakrej(f, code, id, inp, len);
340 fsm_rtermreq(f, id, inp, len);
348 fsm_rcoderej(f, inp, len);
352 if( !f->callbacks->extcode
353 || !(*f->callbacks->extcode)(f, code, id, inp, len) )
354 fsm_sdata(f, CODEREJ, ++f->id, inpacket, len + HEADERLEN);
361 * fsm_rconfreq - Receive Configure-Request.
364 fsm_rconfreq(f, id, inp, len)
370 int code, reject_if_disagree;
374 /* Go away, we're closed */
375 fsm_sdata(f, TERMACK, id, NULL, 0);
382 /* Go down and restart negotiation */
383 if( f->callbacks->down )
384 (*f->callbacks->down)(f); /* Inform upper layers */
385 fsm_sconfreq(f, 0); /* Send initial Configure-Request */
389 /* Negotiation started by our peer */
390 fsm_sconfreq(f, 0); /* Send initial Configure-Request */
396 * Pass the requested configuration options
397 * to protocol-specific code for checking.
399 if (f->callbacks->reqci){ /* Check CI */
400 reject_if_disagree = (f->nakloops >= f->maxnakloops);
401 code = (*f->callbacks->reqci)(f, inp, &len, reject_if_disagree);
403 code = CONFREJ; /* Reject all CI */
407 /* send the Ack, Nak or Rej to the peer */
408 fsm_sdata(f, code, id, inp, len);
410 if (code == CONFACK) {
411 if (f->state == ACKRCVD) {
412 UNTIMEOUT(fsm_timeout, f); /* Cancel timeout */
414 if (f->callbacks->up)
415 (*f->callbacks->up)(f); /* Inform upper layers */
421 /* we sent CONFACK or CONFREJ */
422 if (f->state != ACKRCVD)
424 if( code == CONFNAK )
431 * fsm_rconfack - Receive Configure-Ack.
434 fsm_rconfack(f, id, inp, len)
440 if (id != f->reqid || f->seen_ack) /* Expected id? */
441 return; /* Nope, toss... */
442 if( !(f->callbacks->ackci? (*f->callbacks->ackci)(f, inp, len):
444 /* Ack is bad - ignore it */
445 error("Received bad configure-ack: %P", inp, len);
453 fsm_sdata(f, TERMACK, id, NULL, 0);
458 f->retransmits = f->maxconfreqtransmits;
462 /* Huh? an extra valid Ack? oh well... */
463 UNTIMEOUT(fsm_timeout, f); /* Cancel timeout */
469 UNTIMEOUT(fsm_timeout, f); /* Cancel timeout */
471 f->retransmits = f->maxconfreqtransmits;
472 if (f->callbacks->up)
473 (*f->callbacks->up)(f); /* Inform upper layers */
477 /* Go down and restart negotiation */
478 if (f->callbacks->down)
479 (*f->callbacks->down)(f); /* Inform upper layers */
480 fsm_sconfreq(f, 0); /* Send initial Configure-Request */
488 * fsm_rconfnakrej - Receive Configure-Nak or Configure-Reject.
491 fsm_rconfnakrej(f, code, id, inp, len)
497 int (*proc) __P((fsm *, u_char *, int));
500 if (id != f->reqid || f->seen_ack) /* Expected id? */
501 return; /* Nope, toss... */
502 proc = (code == CONFNAK)? f->callbacks->nakci: f->callbacks->rejci;
503 if (!proc || !(ret = proc(f, inp, len))) {
504 /* Nak/reject is bad - ignore it */
505 error("Received bad configure-nak/rej: %P", inp, len);
513 fsm_sdata(f, TERMACK, id, NULL, 0);
518 /* They didn't agree to what we wanted - try another request */
519 UNTIMEOUT(fsm_timeout, f); /* Cancel timeout */
521 f->state = STOPPED; /* kludge for stopping CCP */
523 fsm_sconfreq(f, 0); /* Send Configure-Request */
527 /* Got a Nak/reject when we had already had an Ack?? oh well... */
528 UNTIMEOUT(fsm_timeout, f); /* Cancel timeout */
534 /* Go down and restart negotiation */
535 if (f->callbacks->down)
536 (*f->callbacks->down)(f); /* Inform upper layers */
537 fsm_sconfreq(f, 0); /* Send initial Configure-Request */
545 * fsm_rtermreq - Receive Terminate-Req.
548 fsm_rtermreq(f, id, p, len)
557 f->state = REQSENT; /* Start over but keep trying */
562 info("%s terminated by peer (%0.*v)", PROTO_NAME(f), len, p);
564 info("%s terminated by peer", PROTO_NAME(f));
567 if (f->callbacks->down)
568 (*f->callbacks->down)(f); /* Inform upper layers */
569 TIMEOUT(fsm_timeout, f, f->timeouttime);
573 fsm_sdata(f, TERMACK, id, NULL, 0);
578 * fsm_rtermack - Receive Terminate-Ack.
586 UNTIMEOUT(fsm_timeout, f);
588 if( f->callbacks->finished )
589 (*f->callbacks->finished)(f);
592 UNTIMEOUT(fsm_timeout, f);
594 if( f->callbacks->finished )
595 (*f->callbacks->finished)(f);
603 if (f->callbacks->down)
604 (*f->callbacks->down)(f); /* Inform upper layers */
612 * fsm_rcoderej - Receive an Code-Reject.
615 fsm_rcoderej(f, inp, len)
622 if (len < HEADERLEN) {
623 FSMDEBUG(("fsm_rcoderej: Rcvd short Code-Reject packet!"));
628 warn("%s: Rcvd Code-Reject for code %d, id %d", PROTO_NAME(f), code, id);
630 if( f->state == ACKRCVD )
636 * fsm_protreject - Peer doesn't speak this protocol.
638 * Treat this as a catastrophic error (RXJ-).
646 UNTIMEOUT(fsm_timeout, f); /* Cancel timeout */
650 if( f->callbacks->finished )
651 (*f->callbacks->finished)(f);
658 UNTIMEOUT(fsm_timeout, f); /* Cancel timeout */
662 if( f->callbacks->finished )
663 (*f->callbacks->finished)(f);
667 if( f->callbacks->down )
668 (*f->callbacks->down)(f);
670 /* Init restart counter, send Terminate-Request */
671 f->retransmits = f->maxtermtransmits;
672 fsm_sdata(f, TERMREQ, f->reqid = ++f->id,
673 (u_char *) f->term_reason, f->term_reason_len);
674 TIMEOUT(fsm_timeout, f, f->timeouttime);
681 FSMDEBUG(("%s: Protocol-reject event in state %d!",
682 PROTO_NAME(f), f->state));
688 * fsm_sconfreq - Send a Configure-Request.
691 fsm_sconfreq(f, retransmit)
698 if( f->state != REQSENT && f->state != ACKRCVD && f->state != ACKSENT ){
699 /* Not currently negotiating - reset options */
700 if( f->callbacks->resetci )
701 (*f->callbacks->resetci)(f);
706 /* New request - reset retransmission counter, use new ID */
707 f->retransmits = f->maxconfreqtransmits;
714 * Make up the request packet
716 outp = outpacket_buf + PPP_HDRLEN + HEADERLEN;
717 if( f->callbacks->cilen && f->callbacks->addci ){
718 cilen = (*f->callbacks->cilen)(f);
719 if( cilen > peer_mru[f->unit] - HEADERLEN )
720 cilen = peer_mru[f->unit] - HEADERLEN;
721 if (f->callbacks->addci)
722 (*f->callbacks->addci)(f, outp, &cilen);
726 /* send the request to our peer */
727 fsm_sdata(f, CONFREQ, f->reqid, outp, cilen);
729 /* start the retransmit timer */
731 TIMEOUT(fsm_timeout, f, f->timeouttime);
736 * fsm_sdata - Send some data.
738 * Used for all packets sent to our peer by this module.
741 fsm_sdata(f, code, id, data, datalen)
750 /* Adjust length to be smaller than MTU */
751 outp = outpacket_buf;
752 if (datalen > peer_mru[f->unit] - HEADERLEN)
753 datalen = peer_mru[f->unit] - HEADERLEN;
754 if (datalen && data != outp + PPP_HDRLEN + HEADERLEN)
755 BCOPY(data, outp + PPP_HDRLEN + HEADERLEN, datalen);
756 outlen = datalen + HEADERLEN;
757 MAKEHEADER(outp, f->protocol);
760 PUTSHORT(outlen, outp);
761 output(f->unit, outpacket_buf, outlen + PPP_HDRLEN);