Forked from STM32F7 internet for nucleo F746ZG

Dependents:   Nucleo_F746ZG_Ethernet_MQTT_Ultrasound

Fork of F7_Ethernet by Dieter Graef

Committer:
EmbeddedSam
Date:
Mon Oct 24 12:59:21 2016 +0000
Revision:
2:b4727195c450
Parent:
0:d26c1b55cfca
working with 1 ultrasound sensor approx 10hz update rate on mqtt

Who changed what in which revision?

UserRevisionLine numberNew contents of line
DieterGraef 0:d26c1b55cfca 1 /*****************************************************************************
DieterGraef 0:d26c1b55cfca 2 * fsm.c - Network Control Protocol Finite State Machine program file.
DieterGraef 0:d26c1b55cfca 3 *
DieterGraef 0:d26c1b55cfca 4 * Copyright (c) 2003 by Marc Boucher, Services Informatiques (MBSI) inc.
DieterGraef 0:d26c1b55cfca 5 * portions Copyright (c) 1997 by Global Election Systems Inc.
DieterGraef 0:d26c1b55cfca 6 *
DieterGraef 0:d26c1b55cfca 7 * The authors hereby grant permission to use, copy, modify, distribute,
DieterGraef 0:d26c1b55cfca 8 * and license this software and its documentation for any purpose, provided
DieterGraef 0:d26c1b55cfca 9 * that existing copyright notices are retained in all copies and that this
DieterGraef 0:d26c1b55cfca 10 * notice and the following disclaimer are included verbatim in any
DieterGraef 0:d26c1b55cfca 11 * distributions. No written agreement, license, or royalty fee is required
DieterGraef 0:d26c1b55cfca 12 * for any of the authorized uses.
DieterGraef 0:d26c1b55cfca 13 *
DieterGraef 0:d26c1b55cfca 14 * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS *AS IS* AND ANY EXPRESS OR
DieterGraef 0:d26c1b55cfca 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
DieterGraef 0:d26c1b55cfca 16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
DieterGraef 0:d26c1b55cfca 17 * IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
DieterGraef 0:d26c1b55cfca 18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
DieterGraef 0:d26c1b55cfca 19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DieterGraef 0:d26c1b55cfca 20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
DieterGraef 0:d26c1b55cfca 21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
DieterGraef 0:d26c1b55cfca 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
DieterGraef 0:d26c1b55cfca 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
DieterGraef 0:d26c1b55cfca 24 *
DieterGraef 0:d26c1b55cfca 25 ******************************************************************************
DieterGraef 0:d26c1b55cfca 26 * REVISION HISTORY
DieterGraef 0:d26c1b55cfca 27 *
DieterGraef 0:d26c1b55cfca 28 * 03-01-01 Marc Boucher <marc@mbsi.ca>
DieterGraef 0:d26c1b55cfca 29 * Ported to lwIP.
DieterGraef 0:d26c1b55cfca 30 * 97-12-01 Guy Lancaster <lancasterg@acm.org>, Global Election Systems Inc.
DieterGraef 0:d26c1b55cfca 31 * Original based on BSD fsm.c.
DieterGraef 0:d26c1b55cfca 32 *****************************************************************************/
DieterGraef 0:d26c1b55cfca 33 /*
DieterGraef 0:d26c1b55cfca 34 * fsm.c - {Link, IP} Control Protocol Finite State Machine.
DieterGraef 0:d26c1b55cfca 35 *
DieterGraef 0:d26c1b55cfca 36 * Copyright (c) 1989 Carnegie Mellon University.
DieterGraef 0:d26c1b55cfca 37 * All rights reserved.
DieterGraef 0:d26c1b55cfca 38 *
DieterGraef 0:d26c1b55cfca 39 * Redistribution and use in source and binary forms are permitted
DieterGraef 0:d26c1b55cfca 40 * provided that the above copyright notice and this paragraph are
DieterGraef 0:d26c1b55cfca 41 * duplicated in all such forms and that any documentation,
DieterGraef 0:d26c1b55cfca 42 * advertising materials, and other materials related to such
DieterGraef 0:d26c1b55cfca 43 * distribution and use acknowledge that the software was developed
DieterGraef 0:d26c1b55cfca 44 * by Carnegie Mellon University. The name of the
DieterGraef 0:d26c1b55cfca 45 * University may not be used to endorse or promote products derived
DieterGraef 0:d26c1b55cfca 46 * from this software without specific prior written permission.
DieterGraef 0:d26c1b55cfca 47 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
DieterGraef 0:d26c1b55cfca 48 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
DieterGraef 0:d26c1b55cfca 49 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
DieterGraef 0:d26c1b55cfca 50 */
DieterGraef 0:d26c1b55cfca 51
DieterGraef 0:d26c1b55cfca 52 /*
DieterGraef 0:d26c1b55cfca 53 * TODO:
DieterGraef 0:d26c1b55cfca 54 * Randomize fsm id on link/init.
DieterGraef 0:d26c1b55cfca 55 * Deal with variable outgoing MTU.
DieterGraef 0:d26c1b55cfca 56 */
DieterGraef 0:d26c1b55cfca 57
DieterGraef 0:d26c1b55cfca 58 #include "lwip/opt.h"
DieterGraef 0:d26c1b55cfca 59
DieterGraef 0:d26c1b55cfca 60 #if PPP_SUPPORT /* don't build if not configured for use in lwipopts.h */
DieterGraef 0:d26c1b55cfca 61
DieterGraef 0:d26c1b55cfca 62 #include "ppp_impl.h"
DieterGraef 0:d26c1b55cfca 63 #include "pppdebug.h"
DieterGraef 0:d26c1b55cfca 64
DieterGraef 0:d26c1b55cfca 65 #include "fsm.h"
DieterGraef 0:d26c1b55cfca 66
DieterGraef 0:d26c1b55cfca 67 #include <string.h>
DieterGraef 0:d26c1b55cfca 68
DieterGraef 0:d26c1b55cfca 69 #if PPP_DEBUG
DieterGraef 0:d26c1b55cfca 70 static const char *ppperr_strerr[] = {
DieterGraef 0:d26c1b55cfca 71 "LS_INITIAL", /* LS_INITIAL 0 */
DieterGraef 0:d26c1b55cfca 72 "LS_STARTING", /* LS_STARTING 1 */
DieterGraef 0:d26c1b55cfca 73 "LS_CLOSED", /* LS_CLOSED 2 */
DieterGraef 0:d26c1b55cfca 74 "LS_STOPPED", /* LS_STOPPED 3 */
DieterGraef 0:d26c1b55cfca 75 "LS_CLOSING", /* LS_CLOSING 4 */
DieterGraef 0:d26c1b55cfca 76 "LS_STOPPING", /* LS_STOPPING 5 */
DieterGraef 0:d26c1b55cfca 77 "LS_REQSENT", /* LS_REQSENT 6 */
DieterGraef 0:d26c1b55cfca 78 "LS_ACKRCVD", /* LS_ACKRCVD 7 */
DieterGraef 0:d26c1b55cfca 79 "LS_ACKSENT", /* LS_ACKSENT 8 */
DieterGraef 0:d26c1b55cfca 80 "LS_OPENED" /* LS_OPENED 9 */
DieterGraef 0:d26c1b55cfca 81 };
DieterGraef 0:d26c1b55cfca 82 #endif /* PPP_DEBUG */
DieterGraef 0:d26c1b55cfca 83
DieterGraef 0:d26c1b55cfca 84 static void fsm_timeout (void *);
DieterGraef 0:d26c1b55cfca 85 static void fsm_rconfreq (fsm *, u_char, u_char *, int);
DieterGraef 0:d26c1b55cfca 86 static void fsm_rconfack (fsm *, int, u_char *, int);
DieterGraef 0:d26c1b55cfca 87 static void fsm_rconfnakrej (fsm *, int, int, u_char *, int);
DieterGraef 0:d26c1b55cfca 88 static void fsm_rtermreq (fsm *, int, u_char *, int);
DieterGraef 0:d26c1b55cfca 89 static void fsm_rtermack (fsm *);
DieterGraef 0:d26c1b55cfca 90 static void fsm_rcoderej (fsm *, u_char *, int);
DieterGraef 0:d26c1b55cfca 91 static void fsm_sconfreq (fsm *, int);
DieterGraef 0:d26c1b55cfca 92
DieterGraef 0:d26c1b55cfca 93 #define PROTO_NAME(f) ((f)->callbacks->proto_name)
DieterGraef 0:d26c1b55cfca 94
DieterGraef 0:d26c1b55cfca 95 int peer_mru[NUM_PPP];
DieterGraef 0:d26c1b55cfca 96
DieterGraef 0:d26c1b55cfca 97
DieterGraef 0:d26c1b55cfca 98 /*
DieterGraef 0:d26c1b55cfca 99 * fsm_init - Initialize fsm.
DieterGraef 0:d26c1b55cfca 100 *
DieterGraef 0:d26c1b55cfca 101 * Initialize fsm state.
DieterGraef 0:d26c1b55cfca 102 */
DieterGraef 0:d26c1b55cfca 103 void
DieterGraef 0:d26c1b55cfca 104 fsm_init(fsm *f)
DieterGraef 0:d26c1b55cfca 105 {
DieterGraef 0:d26c1b55cfca 106 f->state = LS_INITIAL;
DieterGraef 0:d26c1b55cfca 107 f->flags = 0;
DieterGraef 0:d26c1b55cfca 108 f->id = 0; /* XXX Start with random id? */
DieterGraef 0:d26c1b55cfca 109 f->timeouttime = FSM_DEFTIMEOUT;
DieterGraef 0:d26c1b55cfca 110 f->maxconfreqtransmits = FSM_DEFMAXCONFREQS;
DieterGraef 0:d26c1b55cfca 111 f->maxtermtransmits = FSM_DEFMAXTERMREQS;
DieterGraef 0:d26c1b55cfca 112 f->maxnakloops = FSM_DEFMAXNAKLOOPS;
DieterGraef 0:d26c1b55cfca 113 f->term_reason_len = 0;
DieterGraef 0:d26c1b55cfca 114 }
DieterGraef 0:d26c1b55cfca 115
DieterGraef 0:d26c1b55cfca 116
DieterGraef 0:d26c1b55cfca 117 /*
DieterGraef 0:d26c1b55cfca 118 * fsm_lowerup - The lower layer is up.
DieterGraef 0:d26c1b55cfca 119 */
DieterGraef 0:d26c1b55cfca 120 void
DieterGraef 0:d26c1b55cfca 121 fsm_lowerup(fsm *f)
DieterGraef 0:d26c1b55cfca 122 {
DieterGraef 0:d26c1b55cfca 123 int oldState = f->state;
DieterGraef 0:d26c1b55cfca 124
DieterGraef 0:d26c1b55cfca 125 LWIP_UNUSED_ARG(oldState);
DieterGraef 0:d26c1b55cfca 126
DieterGraef 0:d26c1b55cfca 127 switch( f->state ) {
DieterGraef 0:d26c1b55cfca 128 case LS_INITIAL:
DieterGraef 0:d26c1b55cfca 129 f->state = LS_CLOSED;
DieterGraef 0:d26c1b55cfca 130 break;
DieterGraef 0:d26c1b55cfca 131
DieterGraef 0:d26c1b55cfca 132 case LS_STARTING:
DieterGraef 0:d26c1b55cfca 133 if( f->flags & OPT_SILENT ) {
DieterGraef 0:d26c1b55cfca 134 f->state = LS_STOPPED;
DieterGraef 0:d26c1b55cfca 135 } else {
DieterGraef 0:d26c1b55cfca 136 /* Send an initial configure-request */
DieterGraef 0:d26c1b55cfca 137 fsm_sconfreq(f, 0);
DieterGraef 0:d26c1b55cfca 138 f->state = LS_REQSENT;
DieterGraef 0:d26c1b55cfca 139 }
DieterGraef 0:d26c1b55cfca 140 break;
DieterGraef 0:d26c1b55cfca 141
DieterGraef 0:d26c1b55cfca 142 default:
DieterGraef 0:d26c1b55cfca 143 FSMDEBUG(LOG_INFO, ("%s: Up event in state %d (%s)!\n",
DieterGraef 0:d26c1b55cfca 144 PROTO_NAME(f), f->state, ppperr_strerr[f->state]));
DieterGraef 0:d26c1b55cfca 145 }
DieterGraef 0:d26c1b55cfca 146
DieterGraef 0:d26c1b55cfca 147 FSMDEBUG(LOG_INFO, ("%s: lowerup state %d (%s) -> %d (%s)\n",
DieterGraef 0:d26c1b55cfca 148 PROTO_NAME(f), oldState, ppperr_strerr[oldState], f->state, ppperr_strerr[f->state]));
DieterGraef 0:d26c1b55cfca 149 }
DieterGraef 0:d26c1b55cfca 150
DieterGraef 0:d26c1b55cfca 151
DieterGraef 0:d26c1b55cfca 152 /*
DieterGraef 0:d26c1b55cfca 153 * fsm_lowerdown - The lower layer is down.
DieterGraef 0:d26c1b55cfca 154 *
DieterGraef 0:d26c1b55cfca 155 * Cancel all timeouts and inform upper layers.
DieterGraef 0:d26c1b55cfca 156 */
DieterGraef 0:d26c1b55cfca 157 void
DieterGraef 0:d26c1b55cfca 158 fsm_lowerdown(fsm *f)
DieterGraef 0:d26c1b55cfca 159 {
DieterGraef 0:d26c1b55cfca 160 int oldState = f->state;
DieterGraef 0:d26c1b55cfca 161
DieterGraef 0:d26c1b55cfca 162 LWIP_UNUSED_ARG(oldState);
DieterGraef 0:d26c1b55cfca 163
DieterGraef 0:d26c1b55cfca 164 switch( f->state ) {
DieterGraef 0:d26c1b55cfca 165 case LS_CLOSED:
DieterGraef 0:d26c1b55cfca 166 f->state = LS_INITIAL;
DieterGraef 0:d26c1b55cfca 167 break;
DieterGraef 0:d26c1b55cfca 168
DieterGraef 0:d26c1b55cfca 169 case LS_STOPPED:
DieterGraef 0:d26c1b55cfca 170 f->state = LS_STARTING;
DieterGraef 0:d26c1b55cfca 171 if( f->callbacks->starting ) {
DieterGraef 0:d26c1b55cfca 172 (*f->callbacks->starting)(f);
DieterGraef 0:d26c1b55cfca 173 }
DieterGraef 0:d26c1b55cfca 174 break;
DieterGraef 0:d26c1b55cfca 175
DieterGraef 0:d26c1b55cfca 176 case LS_CLOSING:
DieterGraef 0:d26c1b55cfca 177 f->state = LS_INITIAL;
DieterGraef 0:d26c1b55cfca 178 UNTIMEOUT(fsm_timeout, f); /* Cancel timeout */
DieterGraef 0:d26c1b55cfca 179 break;
DieterGraef 0:d26c1b55cfca 180
DieterGraef 0:d26c1b55cfca 181 case LS_STOPPING:
DieterGraef 0:d26c1b55cfca 182 case LS_REQSENT:
DieterGraef 0:d26c1b55cfca 183 case LS_ACKRCVD:
DieterGraef 0:d26c1b55cfca 184 case LS_ACKSENT:
DieterGraef 0:d26c1b55cfca 185 f->state = LS_STARTING;
DieterGraef 0:d26c1b55cfca 186 UNTIMEOUT(fsm_timeout, f); /* Cancel timeout */
DieterGraef 0:d26c1b55cfca 187 break;
DieterGraef 0:d26c1b55cfca 188
DieterGraef 0:d26c1b55cfca 189 case LS_OPENED:
DieterGraef 0:d26c1b55cfca 190 if( f->callbacks->down ) {
DieterGraef 0:d26c1b55cfca 191 (*f->callbacks->down)(f);
DieterGraef 0:d26c1b55cfca 192 }
DieterGraef 0:d26c1b55cfca 193 f->state = LS_STARTING;
DieterGraef 0:d26c1b55cfca 194 break;
DieterGraef 0:d26c1b55cfca 195
DieterGraef 0:d26c1b55cfca 196 default:
DieterGraef 0:d26c1b55cfca 197 FSMDEBUG(LOG_INFO, ("%s: Down event in state %d (%s)!\n",
DieterGraef 0:d26c1b55cfca 198 PROTO_NAME(f), f->state, ppperr_strerr[f->state]));
DieterGraef 0:d26c1b55cfca 199 }
DieterGraef 0:d26c1b55cfca 200
DieterGraef 0:d26c1b55cfca 201 FSMDEBUG(LOG_INFO, ("%s: lowerdown state %d (%s) -> %d (%s)\n",
DieterGraef 0:d26c1b55cfca 202 PROTO_NAME(f), oldState, ppperr_strerr[oldState], f->state, ppperr_strerr[f->state]));
DieterGraef 0:d26c1b55cfca 203 }
DieterGraef 0:d26c1b55cfca 204
DieterGraef 0:d26c1b55cfca 205
DieterGraef 0:d26c1b55cfca 206 /*
DieterGraef 0:d26c1b55cfca 207 * fsm_open - Link is allowed to come up.
DieterGraef 0:d26c1b55cfca 208 */
DieterGraef 0:d26c1b55cfca 209 void
DieterGraef 0:d26c1b55cfca 210 fsm_open(fsm *f)
DieterGraef 0:d26c1b55cfca 211 {
DieterGraef 0:d26c1b55cfca 212 int oldState = f->state;
DieterGraef 0:d26c1b55cfca 213
DieterGraef 0:d26c1b55cfca 214 LWIP_UNUSED_ARG(oldState);
DieterGraef 0:d26c1b55cfca 215
DieterGraef 0:d26c1b55cfca 216 switch( f->state ) {
DieterGraef 0:d26c1b55cfca 217 case LS_INITIAL:
DieterGraef 0:d26c1b55cfca 218 f->state = LS_STARTING;
DieterGraef 0:d26c1b55cfca 219 if( f->callbacks->starting ) {
DieterGraef 0:d26c1b55cfca 220 (*f->callbacks->starting)(f);
DieterGraef 0:d26c1b55cfca 221 }
DieterGraef 0:d26c1b55cfca 222 break;
DieterGraef 0:d26c1b55cfca 223
DieterGraef 0:d26c1b55cfca 224 case LS_CLOSED:
DieterGraef 0:d26c1b55cfca 225 if( f->flags & OPT_SILENT ) {
DieterGraef 0:d26c1b55cfca 226 f->state = LS_STOPPED;
DieterGraef 0:d26c1b55cfca 227 } else {
DieterGraef 0:d26c1b55cfca 228 /* Send an initial configure-request */
DieterGraef 0:d26c1b55cfca 229 fsm_sconfreq(f, 0);
DieterGraef 0:d26c1b55cfca 230 f->state = LS_REQSENT;
DieterGraef 0:d26c1b55cfca 231 }
DieterGraef 0:d26c1b55cfca 232 break;
DieterGraef 0:d26c1b55cfca 233
DieterGraef 0:d26c1b55cfca 234 case LS_CLOSING:
DieterGraef 0:d26c1b55cfca 235 f->state = LS_STOPPING;
DieterGraef 0:d26c1b55cfca 236 /* fall through */
DieterGraef 0:d26c1b55cfca 237 case LS_STOPPED:
DieterGraef 0:d26c1b55cfca 238 case LS_OPENED:
DieterGraef 0:d26c1b55cfca 239 if( f->flags & OPT_RESTART ) {
DieterGraef 0:d26c1b55cfca 240 fsm_lowerdown(f);
DieterGraef 0:d26c1b55cfca 241 fsm_lowerup(f);
DieterGraef 0:d26c1b55cfca 242 }
DieterGraef 0:d26c1b55cfca 243 break;
DieterGraef 0:d26c1b55cfca 244 }
DieterGraef 0:d26c1b55cfca 245
DieterGraef 0:d26c1b55cfca 246 FSMDEBUG(LOG_INFO, ("%s: open state %d (%s) -> %d (%s)\n",
DieterGraef 0:d26c1b55cfca 247 PROTO_NAME(f), oldState, ppperr_strerr[oldState], f->state, ppperr_strerr[f->state]));
DieterGraef 0:d26c1b55cfca 248 }
DieterGraef 0:d26c1b55cfca 249
DieterGraef 0:d26c1b55cfca 250 #if 0 /* backport pppd 2.4.4b1; */
DieterGraef 0:d26c1b55cfca 251 /*
DieterGraef 0:d26c1b55cfca 252 * terminate_layer - Start process of shutting down the FSM
DieterGraef 0:d26c1b55cfca 253 *
DieterGraef 0:d26c1b55cfca 254 * Cancel any timeout running, notify upper layers we're done, and
DieterGraef 0:d26c1b55cfca 255 * send a terminate-request message as configured.
DieterGraef 0:d26c1b55cfca 256 */
DieterGraef 0:d26c1b55cfca 257 static void
DieterGraef 0:d26c1b55cfca 258 terminate_layer(fsm *f, int nextstate)
DieterGraef 0:d26c1b55cfca 259 {
DieterGraef 0:d26c1b55cfca 260 /* @todo */
DieterGraef 0:d26c1b55cfca 261 }
DieterGraef 0:d26c1b55cfca 262 #endif
DieterGraef 0:d26c1b55cfca 263
DieterGraef 0:d26c1b55cfca 264 /*
DieterGraef 0:d26c1b55cfca 265 * fsm_close - Start closing connection.
DieterGraef 0:d26c1b55cfca 266 *
DieterGraef 0:d26c1b55cfca 267 * Cancel timeouts and either initiate close or possibly go directly to
DieterGraef 0:d26c1b55cfca 268 * the LS_CLOSED state.
DieterGraef 0:d26c1b55cfca 269 */
DieterGraef 0:d26c1b55cfca 270 void
DieterGraef 0:d26c1b55cfca 271 fsm_close(fsm *f, char *reason)
DieterGraef 0:d26c1b55cfca 272 {
DieterGraef 0:d26c1b55cfca 273 int oldState = f->state;
DieterGraef 0:d26c1b55cfca 274
DieterGraef 0:d26c1b55cfca 275 LWIP_UNUSED_ARG(oldState);
DieterGraef 0:d26c1b55cfca 276
DieterGraef 0:d26c1b55cfca 277 f->term_reason = reason;
DieterGraef 0:d26c1b55cfca 278 f->term_reason_len = (reason == NULL ? 0 : (int)strlen(reason));
DieterGraef 0:d26c1b55cfca 279 switch( f->state ) {
DieterGraef 0:d26c1b55cfca 280 case LS_STARTING:
DieterGraef 0:d26c1b55cfca 281 f->state = LS_INITIAL;
DieterGraef 0:d26c1b55cfca 282 break;
DieterGraef 0:d26c1b55cfca 283 case LS_STOPPED:
DieterGraef 0:d26c1b55cfca 284 f->state = LS_CLOSED;
DieterGraef 0:d26c1b55cfca 285 break;
DieterGraef 0:d26c1b55cfca 286 case LS_STOPPING:
DieterGraef 0:d26c1b55cfca 287 f->state = LS_CLOSING;
DieterGraef 0:d26c1b55cfca 288 break;
DieterGraef 0:d26c1b55cfca 289
DieterGraef 0:d26c1b55cfca 290 case LS_REQSENT:
DieterGraef 0:d26c1b55cfca 291 case LS_ACKRCVD:
DieterGraef 0:d26c1b55cfca 292 case LS_ACKSENT:
DieterGraef 0:d26c1b55cfca 293 case LS_OPENED:
DieterGraef 0:d26c1b55cfca 294 if( f->state != LS_OPENED ) {
DieterGraef 0:d26c1b55cfca 295 UNTIMEOUT(fsm_timeout, f); /* Cancel timeout */
DieterGraef 0:d26c1b55cfca 296 } else if( f->callbacks->down ) {
DieterGraef 0:d26c1b55cfca 297 (*f->callbacks->down)(f); /* Inform upper layers we're down */
DieterGraef 0:d26c1b55cfca 298 }
DieterGraef 0:d26c1b55cfca 299 /* Init restart counter, send Terminate-Request */
DieterGraef 0:d26c1b55cfca 300 f->retransmits = f->maxtermtransmits;
DieterGraef 0:d26c1b55cfca 301 fsm_sdata(f, TERMREQ, f->reqid = ++f->id,
DieterGraef 0:d26c1b55cfca 302 (u_char *) f->term_reason, f->term_reason_len);
DieterGraef 0:d26c1b55cfca 303 TIMEOUT(fsm_timeout, f, f->timeouttime);
DieterGraef 0:d26c1b55cfca 304 --f->retransmits;
DieterGraef 0:d26c1b55cfca 305
DieterGraef 0:d26c1b55cfca 306 f->state = LS_CLOSING;
DieterGraef 0:d26c1b55cfca 307 break;
DieterGraef 0:d26c1b55cfca 308 }
DieterGraef 0:d26c1b55cfca 309
DieterGraef 0:d26c1b55cfca 310 FSMDEBUG(LOG_INFO, ("%s: close reason=%s state %d (%s) -> %d (%s)\n",
DieterGraef 0:d26c1b55cfca 311 PROTO_NAME(f), reason, oldState, ppperr_strerr[oldState], f->state, ppperr_strerr[f->state]));
DieterGraef 0:d26c1b55cfca 312 }
DieterGraef 0:d26c1b55cfca 313
DieterGraef 0:d26c1b55cfca 314
DieterGraef 0:d26c1b55cfca 315 /*
DieterGraef 0:d26c1b55cfca 316 * fsm_timeout - Timeout expired.
DieterGraef 0:d26c1b55cfca 317 */
DieterGraef 0:d26c1b55cfca 318 static void
DieterGraef 0:d26c1b55cfca 319 fsm_timeout(void *arg)
DieterGraef 0:d26c1b55cfca 320 {
DieterGraef 0:d26c1b55cfca 321 fsm *f = (fsm *) arg;
DieterGraef 0:d26c1b55cfca 322
DieterGraef 0:d26c1b55cfca 323 switch (f->state) {
DieterGraef 0:d26c1b55cfca 324 case LS_CLOSING:
DieterGraef 0:d26c1b55cfca 325 case LS_STOPPING:
DieterGraef 0:d26c1b55cfca 326 if( f->retransmits <= 0 ) {
DieterGraef 0:d26c1b55cfca 327 FSMDEBUG(LOG_WARNING, ("%s: timeout sending Terminate-Request state=%d (%s)\n",
DieterGraef 0:d26c1b55cfca 328 PROTO_NAME(f), f->state, ppperr_strerr[f->state]));
DieterGraef 0:d26c1b55cfca 329 /*
DieterGraef 0:d26c1b55cfca 330 * We've waited for an ack long enough. Peer probably heard us.
DieterGraef 0:d26c1b55cfca 331 */
DieterGraef 0:d26c1b55cfca 332 f->state = (f->state == LS_CLOSING)? LS_CLOSED: LS_STOPPED;
DieterGraef 0:d26c1b55cfca 333 if( f->callbacks->finished ) {
DieterGraef 0:d26c1b55cfca 334 (*f->callbacks->finished)(f);
DieterGraef 0:d26c1b55cfca 335 }
DieterGraef 0:d26c1b55cfca 336 } else {
DieterGraef 0:d26c1b55cfca 337 FSMDEBUG(LOG_WARNING, ("%s: timeout resending Terminate-Requests state=%d (%s)\n",
DieterGraef 0:d26c1b55cfca 338 PROTO_NAME(f), f->state, ppperr_strerr[f->state]));
DieterGraef 0:d26c1b55cfca 339 /* Send Terminate-Request */
DieterGraef 0:d26c1b55cfca 340 fsm_sdata(f, TERMREQ, f->reqid = ++f->id,
DieterGraef 0:d26c1b55cfca 341 (u_char *) f->term_reason, f->term_reason_len);
DieterGraef 0:d26c1b55cfca 342 TIMEOUT(fsm_timeout, f, f->timeouttime);
DieterGraef 0:d26c1b55cfca 343 --f->retransmits;
DieterGraef 0:d26c1b55cfca 344 }
DieterGraef 0:d26c1b55cfca 345 break;
DieterGraef 0:d26c1b55cfca 346
DieterGraef 0:d26c1b55cfca 347 case LS_REQSENT:
DieterGraef 0:d26c1b55cfca 348 case LS_ACKRCVD:
DieterGraef 0:d26c1b55cfca 349 case LS_ACKSENT:
DieterGraef 0:d26c1b55cfca 350 if (f->retransmits <= 0) {
DieterGraef 0:d26c1b55cfca 351 FSMDEBUG(LOG_WARNING, ("%s: timeout sending Config-Requests state=%d (%s)\n",
DieterGraef 0:d26c1b55cfca 352 PROTO_NAME(f), f->state, ppperr_strerr[f->state]));
DieterGraef 0:d26c1b55cfca 353 f->state = LS_STOPPED;
DieterGraef 0:d26c1b55cfca 354 if( (f->flags & OPT_PASSIVE) == 0 && f->callbacks->finished ) {
DieterGraef 0:d26c1b55cfca 355 (*f->callbacks->finished)(f);
DieterGraef 0:d26c1b55cfca 356 }
DieterGraef 0:d26c1b55cfca 357 } else {
DieterGraef 0:d26c1b55cfca 358 FSMDEBUG(LOG_WARNING, ("%s: timeout resending Config-Request state=%d (%s)\n",
DieterGraef 0:d26c1b55cfca 359 PROTO_NAME(f), f->state, ppperr_strerr[f->state]));
DieterGraef 0:d26c1b55cfca 360 /* Retransmit the configure-request */
DieterGraef 0:d26c1b55cfca 361 if (f->callbacks->retransmit) {
DieterGraef 0:d26c1b55cfca 362 (*f->callbacks->retransmit)(f);
DieterGraef 0:d26c1b55cfca 363 }
DieterGraef 0:d26c1b55cfca 364 fsm_sconfreq(f, 1); /* Re-send Configure-Request */
DieterGraef 0:d26c1b55cfca 365 if( f->state == LS_ACKRCVD ) {
DieterGraef 0:d26c1b55cfca 366 f->state = LS_REQSENT;
DieterGraef 0:d26c1b55cfca 367 }
DieterGraef 0:d26c1b55cfca 368 }
DieterGraef 0:d26c1b55cfca 369 break;
DieterGraef 0:d26c1b55cfca 370
DieterGraef 0:d26c1b55cfca 371 default:
DieterGraef 0:d26c1b55cfca 372 FSMDEBUG(LOG_INFO, ("%s: UNHANDLED timeout event in state %d (%s)!\n",
DieterGraef 0:d26c1b55cfca 373 PROTO_NAME(f), f->state, ppperr_strerr[f->state]));
DieterGraef 0:d26c1b55cfca 374 }
DieterGraef 0:d26c1b55cfca 375 }
DieterGraef 0:d26c1b55cfca 376
DieterGraef 0:d26c1b55cfca 377
DieterGraef 0:d26c1b55cfca 378 /*
DieterGraef 0:d26c1b55cfca 379 * fsm_input - Input packet.
DieterGraef 0:d26c1b55cfca 380 */
DieterGraef 0:d26c1b55cfca 381 void
DieterGraef 0:d26c1b55cfca 382 fsm_input(fsm *f, u_char *inpacket, int l)
DieterGraef 0:d26c1b55cfca 383 {
DieterGraef 0:d26c1b55cfca 384 u_char *inp = inpacket;
DieterGraef 0:d26c1b55cfca 385 u_char code, id;
DieterGraef 0:d26c1b55cfca 386 int len;
DieterGraef 0:d26c1b55cfca 387
DieterGraef 0:d26c1b55cfca 388 /*
DieterGraef 0:d26c1b55cfca 389 * Parse header (code, id and length).
DieterGraef 0:d26c1b55cfca 390 * If packet too short, drop it.
DieterGraef 0:d26c1b55cfca 391 */
DieterGraef 0:d26c1b55cfca 392 if (l < HEADERLEN) {
DieterGraef 0:d26c1b55cfca 393 FSMDEBUG(LOG_WARNING, ("fsm_input(%x): Rcvd short header.\n",
DieterGraef 0:d26c1b55cfca 394 f->protocol));
DieterGraef 0:d26c1b55cfca 395 return;
DieterGraef 0:d26c1b55cfca 396 }
DieterGraef 0:d26c1b55cfca 397 GETCHAR(code, inp);
DieterGraef 0:d26c1b55cfca 398 GETCHAR(id, inp);
DieterGraef 0:d26c1b55cfca 399 GETSHORT(len, inp);
DieterGraef 0:d26c1b55cfca 400 if (len < HEADERLEN) {
DieterGraef 0:d26c1b55cfca 401 FSMDEBUG(LOG_INFO, ("fsm_input(%x): Rcvd illegal length.\n",
DieterGraef 0:d26c1b55cfca 402 f->protocol));
DieterGraef 0:d26c1b55cfca 403 return;
DieterGraef 0:d26c1b55cfca 404 }
DieterGraef 0:d26c1b55cfca 405 if (len > l) {
DieterGraef 0:d26c1b55cfca 406 FSMDEBUG(LOG_INFO, ("fsm_input(%x): Rcvd short packet.\n",
DieterGraef 0:d26c1b55cfca 407 f->protocol));
DieterGraef 0:d26c1b55cfca 408 return;
DieterGraef 0:d26c1b55cfca 409 }
DieterGraef 0:d26c1b55cfca 410 len -= HEADERLEN; /* subtract header length */
DieterGraef 0:d26c1b55cfca 411
DieterGraef 0:d26c1b55cfca 412 if( f->state == LS_INITIAL || f->state == LS_STARTING ) {
DieterGraef 0:d26c1b55cfca 413 FSMDEBUG(LOG_INFO, ("fsm_input(%x): Rcvd packet in state %d (%s).\n",
DieterGraef 0:d26c1b55cfca 414 f->protocol, f->state, ppperr_strerr[f->state]));
DieterGraef 0:d26c1b55cfca 415 return;
DieterGraef 0:d26c1b55cfca 416 }
DieterGraef 0:d26c1b55cfca 417 FSMDEBUG(LOG_INFO, ("fsm_input(%s):%d,%d,%d\n", PROTO_NAME(f), code, id, l));
DieterGraef 0:d26c1b55cfca 418 /*
DieterGraef 0:d26c1b55cfca 419 * Action depends on code.
DieterGraef 0:d26c1b55cfca 420 */
DieterGraef 0:d26c1b55cfca 421 switch (code) {
DieterGraef 0:d26c1b55cfca 422 case CONFREQ:
DieterGraef 0:d26c1b55cfca 423 fsm_rconfreq(f, id, inp, len);
DieterGraef 0:d26c1b55cfca 424 break;
DieterGraef 0:d26c1b55cfca 425
DieterGraef 0:d26c1b55cfca 426 case CONFACK:
DieterGraef 0:d26c1b55cfca 427 fsm_rconfack(f, id, inp, len);
DieterGraef 0:d26c1b55cfca 428 break;
DieterGraef 0:d26c1b55cfca 429
DieterGraef 0:d26c1b55cfca 430 case CONFNAK:
DieterGraef 0:d26c1b55cfca 431 case CONFREJ:
DieterGraef 0:d26c1b55cfca 432 fsm_rconfnakrej(f, code, id, inp, len);
DieterGraef 0:d26c1b55cfca 433 break;
DieterGraef 0:d26c1b55cfca 434
DieterGraef 0:d26c1b55cfca 435 case TERMREQ:
DieterGraef 0:d26c1b55cfca 436 fsm_rtermreq(f, id, inp, len);
DieterGraef 0:d26c1b55cfca 437 break;
DieterGraef 0:d26c1b55cfca 438
DieterGraef 0:d26c1b55cfca 439 case TERMACK:
DieterGraef 0:d26c1b55cfca 440 fsm_rtermack(f);
DieterGraef 0:d26c1b55cfca 441 break;
DieterGraef 0:d26c1b55cfca 442
DieterGraef 0:d26c1b55cfca 443 case CODEREJ:
DieterGraef 0:d26c1b55cfca 444 fsm_rcoderej(f, inp, len);
DieterGraef 0:d26c1b55cfca 445 break;
DieterGraef 0:d26c1b55cfca 446
DieterGraef 0:d26c1b55cfca 447 default:
DieterGraef 0:d26c1b55cfca 448 FSMDEBUG(LOG_INFO, ("fsm_input(%s): default: \n", PROTO_NAME(f)));
DieterGraef 0:d26c1b55cfca 449 if( !f->callbacks->extcode ||
DieterGraef 0:d26c1b55cfca 450 !(*f->callbacks->extcode)(f, code, id, inp, len) ) {
DieterGraef 0:d26c1b55cfca 451 fsm_sdata(f, CODEREJ, ++f->id, inpacket, len + HEADERLEN);
DieterGraef 0:d26c1b55cfca 452 }
DieterGraef 0:d26c1b55cfca 453 break;
DieterGraef 0:d26c1b55cfca 454 }
DieterGraef 0:d26c1b55cfca 455 }
DieterGraef 0:d26c1b55cfca 456
DieterGraef 0:d26c1b55cfca 457
DieterGraef 0:d26c1b55cfca 458 /*
DieterGraef 0:d26c1b55cfca 459 * fsm_rconfreq - Receive Configure-Request.
DieterGraef 0:d26c1b55cfca 460 */
DieterGraef 0:d26c1b55cfca 461 static void
DieterGraef 0:d26c1b55cfca 462 fsm_rconfreq(fsm *f, u_char id, u_char *inp, int len)
DieterGraef 0:d26c1b55cfca 463 {
DieterGraef 0:d26c1b55cfca 464 int code, reject_if_disagree;
DieterGraef 0:d26c1b55cfca 465
DieterGraef 0:d26c1b55cfca 466 FSMDEBUG(LOG_INFO, ("fsm_rconfreq(%s): Rcvd id %d state=%d (%s)\n",
DieterGraef 0:d26c1b55cfca 467 PROTO_NAME(f), id, f->state, ppperr_strerr[f->state]));
DieterGraef 0:d26c1b55cfca 468 switch( f->state ) {
DieterGraef 0:d26c1b55cfca 469 case LS_CLOSED:
DieterGraef 0:d26c1b55cfca 470 /* Go away, we're closed */
DieterGraef 0:d26c1b55cfca 471 fsm_sdata(f, TERMACK, id, NULL, 0);
DieterGraef 0:d26c1b55cfca 472 return;
DieterGraef 0:d26c1b55cfca 473 case LS_CLOSING:
DieterGraef 0:d26c1b55cfca 474 case LS_STOPPING:
DieterGraef 0:d26c1b55cfca 475 return;
DieterGraef 0:d26c1b55cfca 476
DieterGraef 0:d26c1b55cfca 477 case LS_OPENED:
DieterGraef 0:d26c1b55cfca 478 /* Go down and restart negotiation */
DieterGraef 0:d26c1b55cfca 479 if( f->callbacks->down ) {
DieterGraef 0:d26c1b55cfca 480 (*f->callbacks->down)(f); /* Inform upper layers */
DieterGraef 0:d26c1b55cfca 481 }
DieterGraef 0:d26c1b55cfca 482 fsm_sconfreq(f, 0); /* Send initial Configure-Request */
DieterGraef 0:d26c1b55cfca 483 break;
DieterGraef 0:d26c1b55cfca 484
DieterGraef 0:d26c1b55cfca 485 case LS_STOPPED:
DieterGraef 0:d26c1b55cfca 486 /* Negotiation started by our peer */
DieterGraef 0:d26c1b55cfca 487 fsm_sconfreq(f, 0); /* Send initial Configure-Request */
DieterGraef 0:d26c1b55cfca 488 f->state = LS_REQSENT;
DieterGraef 0:d26c1b55cfca 489 break;
DieterGraef 0:d26c1b55cfca 490 }
DieterGraef 0:d26c1b55cfca 491
DieterGraef 0:d26c1b55cfca 492 /*
DieterGraef 0:d26c1b55cfca 493 * Pass the requested configuration options
DieterGraef 0:d26c1b55cfca 494 * to protocol-specific code for checking.
DieterGraef 0:d26c1b55cfca 495 */
DieterGraef 0:d26c1b55cfca 496 if (f->callbacks->reqci) { /* Check CI */
DieterGraef 0:d26c1b55cfca 497 reject_if_disagree = (f->nakloops >= f->maxnakloops);
DieterGraef 0:d26c1b55cfca 498 code = (*f->callbacks->reqci)(f, inp, &len, reject_if_disagree);
DieterGraef 0:d26c1b55cfca 499 } else if (len) {
DieterGraef 0:d26c1b55cfca 500 code = CONFREJ; /* Reject all CI */
DieterGraef 0:d26c1b55cfca 501 } else {
DieterGraef 0:d26c1b55cfca 502 code = CONFACK;
DieterGraef 0:d26c1b55cfca 503 }
DieterGraef 0:d26c1b55cfca 504
DieterGraef 0:d26c1b55cfca 505 /* send the Ack, Nak or Rej to the peer */
DieterGraef 0:d26c1b55cfca 506 fsm_sdata(f, (u_char)code, id, inp, len);
DieterGraef 0:d26c1b55cfca 507
DieterGraef 0:d26c1b55cfca 508 if (code == CONFACK) {
DieterGraef 0:d26c1b55cfca 509 if (f->state == LS_ACKRCVD) {
DieterGraef 0:d26c1b55cfca 510 UNTIMEOUT(fsm_timeout, f); /* Cancel timeout */
DieterGraef 0:d26c1b55cfca 511 f->state = LS_OPENED;
DieterGraef 0:d26c1b55cfca 512 if (f->callbacks->up) {
DieterGraef 0:d26c1b55cfca 513 (*f->callbacks->up)(f); /* Inform upper layers */
DieterGraef 0:d26c1b55cfca 514 }
DieterGraef 0:d26c1b55cfca 515 } else {
DieterGraef 0:d26c1b55cfca 516 f->state = LS_ACKSENT;
DieterGraef 0:d26c1b55cfca 517 }
DieterGraef 0:d26c1b55cfca 518 f->nakloops = 0;
DieterGraef 0:d26c1b55cfca 519 } else {
DieterGraef 0:d26c1b55cfca 520 /* we sent CONFACK or CONFREJ */
DieterGraef 0:d26c1b55cfca 521 if (f->state != LS_ACKRCVD) {
DieterGraef 0:d26c1b55cfca 522 f->state = LS_REQSENT;
DieterGraef 0:d26c1b55cfca 523 }
DieterGraef 0:d26c1b55cfca 524 if( code == CONFNAK ) {
DieterGraef 0:d26c1b55cfca 525 ++f->nakloops;
DieterGraef 0:d26c1b55cfca 526 }
DieterGraef 0:d26c1b55cfca 527 }
DieterGraef 0:d26c1b55cfca 528 }
DieterGraef 0:d26c1b55cfca 529
DieterGraef 0:d26c1b55cfca 530
DieterGraef 0:d26c1b55cfca 531 /*
DieterGraef 0:d26c1b55cfca 532 * fsm_rconfack - Receive Configure-Ack.
DieterGraef 0:d26c1b55cfca 533 */
DieterGraef 0:d26c1b55cfca 534 static void
DieterGraef 0:d26c1b55cfca 535 fsm_rconfack(fsm *f, int id, u_char *inp, int len)
DieterGraef 0:d26c1b55cfca 536 {
DieterGraef 0:d26c1b55cfca 537 FSMDEBUG(LOG_INFO, ("fsm_rconfack(%s): Rcvd id %d state=%d (%s)\n",
DieterGraef 0:d26c1b55cfca 538 PROTO_NAME(f), id, f->state, ppperr_strerr[f->state]));
DieterGraef 0:d26c1b55cfca 539
DieterGraef 0:d26c1b55cfca 540 if (id != f->reqid || f->seen_ack) { /* Expected id? */
DieterGraef 0:d26c1b55cfca 541 return; /* Nope, toss... */
DieterGraef 0:d26c1b55cfca 542 }
DieterGraef 0:d26c1b55cfca 543 if( !(f->callbacks->ackci? (*f->callbacks->ackci)(f, inp, len): (len == 0)) ) {
DieterGraef 0:d26c1b55cfca 544 /* Ack is bad - ignore it */
DieterGraef 0:d26c1b55cfca 545 FSMDEBUG(LOG_INFO, ("%s: received bad Ack (length %d)\n",
DieterGraef 0:d26c1b55cfca 546 PROTO_NAME(f), len));
DieterGraef 0:d26c1b55cfca 547 return;
DieterGraef 0:d26c1b55cfca 548 }
DieterGraef 0:d26c1b55cfca 549 f->seen_ack = 1;
DieterGraef 0:d26c1b55cfca 550
DieterGraef 0:d26c1b55cfca 551 switch (f->state) {
DieterGraef 0:d26c1b55cfca 552 case LS_CLOSED:
DieterGraef 0:d26c1b55cfca 553 case LS_STOPPED:
DieterGraef 0:d26c1b55cfca 554 fsm_sdata(f, TERMACK, (u_char)id, NULL, 0);
DieterGraef 0:d26c1b55cfca 555 break;
DieterGraef 0:d26c1b55cfca 556
DieterGraef 0:d26c1b55cfca 557 case LS_REQSENT:
DieterGraef 0:d26c1b55cfca 558 f->state = LS_ACKRCVD;
DieterGraef 0:d26c1b55cfca 559 f->retransmits = f->maxconfreqtransmits;
DieterGraef 0:d26c1b55cfca 560 break;
DieterGraef 0:d26c1b55cfca 561
DieterGraef 0:d26c1b55cfca 562 case LS_ACKRCVD:
DieterGraef 0:d26c1b55cfca 563 /* Huh? an extra valid Ack? oh well... */
DieterGraef 0:d26c1b55cfca 564 UNTIMEOUT(fsm_timeout, f); /* Cancel timeout */
DieterGraef 0:d26c1b55cfca 565 fsm_sconfreq(f, 0);
DieterGraef 0:d26c1b55cfca 566 f->state = LS_REQSENT;
DieterGraef 0:d26c1b55cfca 567 break;
DieterGraef 0:d26c1b55cfca 568
DieterGraef 0:d26c1b55cfca 569 case LS_ACKSENT:
DieterGraef 0:d26c1b55cfca 570 UNTIMEOUT(fsm_timeout, f); /* Cancel timeout */
DieterGraef 0:d26c1b55cfca 571 f->state = LS_OPENED;
DieterGraef 0:d26c1b55cfca 572 f->retransmits = f->maxconfreqtransmits;
DieterGraef 0:d26c1b55cfca 573 if (f->callbacks->up) {
DieterGraef 0:d26c1b55cfca 574 (*f->callbacks->up)(f); /* Inform upper layers */
DieterGraef 0:d26c1b55cfca 575 }
DieterGraef 0:d26c1b55cfca 576 break;
DieterGraef 0:d26c1b55cfca 577
DieterGraef 0:d26c1b55cfca 578 case LS_OPENED:
DieterGraef 0:d26c1b55cfca 579 /* Go down and restart negotiation */
DieterGraef 0:d26c1b55cfca 580 if (f->callbacks->down) {
DieterGraef 0:d26c1b55cfca 581 (*f->callbacks->down)(f); /* Inform upper layers */
DieterGraef 0:d26c1b55cfca 582 }
DieterGraef 0:d26c1b55cfca 583 fsm_sconfreq(f, 0); /* Send initial Configure-Request */
DieterGraef 0:d26c1b55cfca 584 f->state = LS_REQSENT;
DieterGraef 0:d26c1b55cfca 585 break;
DieterGraef 0:d26c1b55cfca 586 }
DieterGraef 0:d26c1b55cfca 587 }
DieterGraef 0:d26c1b55cfca 588
DieterGraef 0:d26c1b55cfca 589
DieterGraef 0:d26c1b55cfca 590 /*
DieterGraef 0:d26c1b55cfca 591 * fsm_rconfnakrej - Receive Configure-Nak or Configure-Reject.
DieterGraef 0:d26c1b55cfca 592 */
DieterGraef 0:d26c1b55cfca 593 static void
DieterGraef 0:d26c1b55cfca 594 fsm_rconfnakrej(fsm *f, int code, int id, u_char *inp, int len)
DieterGraef 0:d26c1b55cfca 595 {
DieterGraef 0:d26c1b55cfca 596 int (*proc) (fsm *, u_char *, int);
DieterGraef 0:d26c1b55cfca 597 int ret;
DieterGraef 0:d26c1b55cfca 598
DieterGraef 0:d26c1b55cfca 599 FSMDEBUG(LOG_INFO, ("fsm_rconfnakrej(%s): Rcvd id %d state=%d (%s)\n",
DieterGraef 0:d26c1b55cfca 600 PROTO_NAME(f), id, f->state, ppperr_strerr[f->state]));
DieterGraef 0:d26c1b55cfca 601
DieterGraef 0:d26c1b55cfca 602 if (id != f->reqid || f->seen_ack) { /* Expected id? */
DieterGraef 0:d26c1b55cfca 603 return; /* Nope, toss... */
DieterGraef 0:d26c1b55cfca 604 }
DieterGraef 0:d26c1b55cfca 605 proc = (code == CONFNAK)? f->callbacks->nakci: f->callbacks->rejci;
DieterGraef 0:d26c1b55cfca 606 if (!proc || !((ret = proc(f, inp, len)))) {
DieterGraef 0:d26c1b55cfca 607 /* Nak/reject is bad - ignore it */
DieterGraef 0:d26c1b55cfca 608 FSMDEBUG(LOG_INFO, ("%s: received bad %s (length %d)\n",
DieterGraef 0:d26c1b55cfca 609 PROTO_NAME(f), (code==CONFNAK? "Nak": "reject"), len));
DieterGraef 0:d26c1b55cfca 610 return;
DieterGraef 0:d26c1b55cfca 611 }
DieterGraef 0:d26c1b55cfca 612 f->seen_ack = 1;
DieterGraef 0:d26c1b55cfca 613
DieterGraef 0:d26c1b55cfca 614 switch (f->state) {
DieterGraef 0:d26c1b55cfca 615 case LS_CLOSED:
DieterGraef 0:d26c1b55cfca 616 case LS_STOPPED:
DieterGraef 0:d26c1b55cfca 617 fsm_sdata(f, TERMACK, (u_char)id, NULL, 0);
DieterGraef 0:d26c1b55cfca 618 break;
DieterGraef 0:d26c1b55cfca 619
DieterGraef 0:d26c1b55cfca 620 case LS_REQSENT:
DieterGraef 0:d26c1b55cfca 621 case LS_ACKSENT:
DieterGraef 0:d26c1b55cfca 622 /* They didn't agree to what we wanted - try another request */
DieterGraef 0:d26c1b55cfca 623 UNTIMEOUT(fsm_timeout, f); /* Cancel timeout */
DieterGraef 0:d26c1b55cfca 624 if (ret < 0) {
DieterGraef 0:d26c1b55cfca 625 f->state = LS_STOPPED; /* kludge for stopping CCP */
DieterGraef 0:d26c1b55cfca 626 } else {
DieterGraef 0:d26c1b55cfca 627 fsm_sconfreq(f, 0); /* Send Configure-Request */
DieterGraef 0:d26c1b55cfca 628 }
DieterGraef 0:d26c1b55cfca 629 break;
DieterGraef 0:d26c1b55cfca 630
DieterGraef 0:d26c1b55cfca 631 case LS_ACKRCVD:
DieterGraef 0:d26c1b55cfca 632 /* Got a Nak/reject when we had already had an Ack?? oh well... */
DieterGraef 0:d26c1b55cfca 633 UNTIMEOUT(fsm_timeout, f); /* Cancel timeout */
DieterGraef 0:d26c1b55cfca 634 fsm_sconfreq(f, 0);
DieterGraef 0:d26c1b55cfca 635 f->state = LS_REQSENT;
DieterGraef 0:d26c1b55cfca 636 break;
DieterGraef 0:d26c1b55cfca 637
DieterGraef 0:d26c1b55cfca 638 case LS_OPENED:
DieterGraef 0:d26c1b55cfca 639 /* Go down and restart negotiation */
DieterGraef 0:d26c1b55cfca 640 if (f->callbacks->down) {
DieterGraef 0:d26c1b55cfca 641 (*f->callbacks->down)(f); /* Inform upper layers */
DieterGraef 0:d26c1b55cfca 642 }
DieterGraef 0:d26c1b55cfca 643 fsm_sconfreq(f, 0); /* Send initial Configure-Request */
DieterGraef 0:d26c1b55cfca 644 f->state = LS_REQSENT;
DieterGraef 0:d26c1b55cfca 645 break;
DieterGraef 0:d26c1b55cfca 646 }
DieterGraef 0:d26c1b55cfca 647 }
DieterGraef 0:d26c1b55cfca 648
DieterGraef 0:d26c1b55cfca 649
DieterGraef 0:d26c1b55cfca 650 /*
DieterGraef 0:d26c1b55cfca 651 * fsm_rtermreq - Receive Terminate-Req.
DieterGraef 0:d26c1b55cfca 652 */
DieterGraef 0:d26c1b55cfca 653 static void
DieterGraef 0:d26c1b55cfca 654 fsm_rtermreq(fsm *f, int id, u_char *p, int len)
DieterGraef 0:d26c1b55cfca 655 {
DieterGraef 0:d26c1b55cfca 656 LWIP_UNUSED_ARG(p);
DieterGraef 0:d26c1b55cfca 657
DieterGraef 0:d26c1b55cfca 658 FSMDEBUG(LOG_INFO, ("fsm_rtermreq(%s): Rcvd id %d state=%d (%s)\n",
DieterGraef 0:d26c1b55cfca 659 PROTO_NAME(f), id, f->state, ppperr_strerr[f->state]));
DieterGraef 0:d26c1b55cfca 660
DieterGraef 0:d26c1b55cfca 661 switch (f->state) {
DieterGraef 0:d26c1b55cfca 662 case LS_ACKRCVD:
DieterGraef 0:d26c1b55cfca 663 case LS_ACKSENT:
DieterGraef 0:d26c1b55cfca 664 f->state = LS_REQSENT; /* Start over but keep trying */
DieterGraef 0:d26c1b55cfca 665 break;
DieterGraef 0:d26c1b55cfca 666
DieterGraef 0:d26c1b55cfca 667 case LS_OPENED:
DieterGraef 0:d26c1b55cfca 668 if (len > 0) {
DieterGraef 0:d26c1b55cfca 669 FSMDEBUG(LOG_INFO, ("%s terminated by peer (%p)\n", PROTO_NAME(f), p));
DieterGraef 0:d26c1b55cfca 670 } else {
DieterGraef 0:d26c1b55cfca 671 FSMDEBUG(LOG_INFO, ("%s terminated by peer\n", PROTO_NAME(f)));
DieterGraef 0:d26c1b55cfca 672 }
DieterGraef 0:d26c1b55cfca 673 if (f->callbacks->down) {
DieterGraef 0:d26c1b55cfca 674 (*f->callbacks->down)(f); /* Inform upper layers */
DieterGraef 0:d26c1b55cfca 675 }
DieterGraef 0:d26c1b55cfca 676 f->retransmits = 0;
DieterGraef 0:d26c1b55cfca 677 f->state = LS_STOPPING;
DieterGraef 0:d26c1b55cfca 678 TIMEOUT(fsm_timeout, f, f->timeouttime);
DieterGraef 0:d26c1b55cfca 679 break;
DieterGraef 0:d26c1b55cfca 680 }
DieterGraef 0:d26c1b55cfca 681
DieterGraef 0:d26c1b55cfca 682 fsm_sdata(f, TERMACK, (u_char)id, NULL, 0);
DieterGraef 0:d26c1b55cfca 683 }
DieterGraef 0:d26c1b55cfca 684
DieterGraef 0:d26c1b55cfca 685
DieterGraef 0:d26c1b55cfca 686 /*
DieterGraef 0:d26c1b55cfca 687 * fsm_rtermack - Receive Terminate-Ack.
DieterGraef 0:d26c1b55cfca 688 */
DieterGraef 0:d26c1b55cfca 689 static void
DieterGraef 0:d26c1b55cfca 690 fsm_rtermack(fsm *f)
DieterGraef 0:d26c1b55cfca 691 {
DieterGraef 0:d26c1b55cfca 692 FSMDEBUG(LOG_INFO, ("fsm_rtermack(%s): state=%d (%s)\n",
DieterGraef 0:d26c1b55cfca 693 PROTO_NAME(f), f->state, ppperr_strerr[f->state]));
DieterGraef 0:d26c1b55cfca 694
DieterGraef 0:d26c1b55cfca 695 switch (f->state) {
DieterGraef 0:d26c1b55cfca 696 case LS_CLOSING:
DieterGraef 0:d26c1b55cfca 697 UNTIMEOUT(fsm_timeout, f);
DieterGraef 0:d26c1b55cfca 698 f->state = LS_CLOSED;
DieterGraef 0:d26c1b55cfca 699 if( f->callbacks->finished ) {
DieterGraef 0:d26c1b55cfca 700 (*f->callbacks->finished)(f);
DieterGraef 0:d26c1b55cfca 701 }
DieterGraef 0:d26c1b55cfca 702 break;
DieterGraef 0:d26c1b55cfca 703
DieterGraef 0:d26c1b55cfca 704 case LS_STOPPING:
DieterGraef 0:d26c1b55cfca 705 UNTIMEOUT(fsm_timeout, f);
DieterGraef 0:d26c1b55cfca 706 f->state = LS_STOPPED;
DieterGraef 0:d26c1b55cfca 707 if( f->callbacks->finished ) {
DieterGraef 0:d26c1b55cfca 708 (*f->callbacks->finished)(f);
DieterGraef 0:d26c1b55cfca 709 }
DieterGraef 0:d26c1b55cfca 710 break;
DieterGraef 0:d26c1b55cfca 711
DieterGraef 0:d26c1b55cfca 712 case LS_ACKRCVD:
DieterGraef 0:d26c1b55cfca 713 f->state = LS_REQSENT;
DieterGraef 0:d26c1b55cfca 714 break;
DieterGraef 0:d26c1b55cfca 715
DieterGraef 0:d26c1b55cfca 716 case LS_OPENED:
DieterGraef 0:d26c1b55cfca 717 if (f->callbacks->down) {
DieterGraef 0:d26c1b55cfca 718 (*f->callbacks->down)(f); /* Inform upper layers */
DieterGraef 0:d26c1b55cfca 719 }
DieterGraef 0:d26c1b55cfca 720 fsm_sconfreq(f, 0);
DieterGraef 0:d26c1b55cfca 721 break;
DieterGraef 0:d26c1b55cfca 722 default:
DieterGraef 0:d26c1b55cfca 723 FSMDEBUG(LOG_INFO, ("fsm_rtermack(%s): UNHANDLED state=%d (%s)!!!\n",
DieterGraef 0:d26c1b55cfca 724 PROTO_NAME(f), f->state, ppperr_strerr[f->state]));
DieterGraef 0:d26c1b55cfca 725 }
DieterGraef 0:d26c1b55cfca 726 }
DieterGraef 0:d26c1b55cfca 727
DieterGraef 0:d26c1b55cfca 728
DieterGraef 0:d26c1b55cfca 729 /*
DieterGraef 0:d26c1b55cfca 730 * fsm_rcoderej - Receive an Code-Reject.
DieterGraef 0:d26c1b55cfca 731 */
DieterGraef 0:d26c1b55cfca 732 static void
DieterGraef 0:d26c1b55cfca 733 fsm_rcoderej(fsm *f, u_char *inp, int len)
DieterGraef 0:d26c1b55cfca 734 {
DieterGraef 0:d26c1b55cfca 735 u_char code, id;
DieterGraef 0:d26c1b55cfca 736
DieterGraef 0:d26c1b55cfca 737 FSMDEBUG(LOG_INFO, ("fsm_rcoderej(%s): state=%d (%s)\n",
DieterGraef 0:d26c1b55cfca 738 PROTO_NAME(f), f->state, ppperr_strerr[f->state]));
DieterGraef 0:d26c1b55cfca 739
DieterGraef 0:d26c1b55cfca 740 if (len < HEADERLEN) {
DieterGraef 0:d26c1b55cfca 741 FSMDEBUG(LOG_INFO, ("fsm_rcoderej: Rcvd short Code-Reject packet!\n"));
DieterGraef 0:d26c1b55cfca 742 return;
DieterGraef 0:d26c1b55cfca 743 }
DieterGraef 0:d26c1b55cfca 744 GETCHAR(code, inp);
DieterGraef 0:d26c1b55cfca 745 GETCHAR(id, inp);
DieterGraef 0:d26c1b55cfca 746 FSMDEBUG(LOG_WARNING, ("%s: Rcvd Code-Reject for code %d, id %d\n",
DieterGraef 0:d26c1b55cfca 747 PROTO_NAME(f), code, id));
DieterGraef 0:d26c1b55cfca 748
DieterGraef 0:d26c1b55cfca 749 if( f->state == LS_ACKRCVD ) {
DieterGraef 0:d26c1b55cfca 750 f->state = LS_REQSENT;
DieterGraef 0:d26c1b55cfca 751 }
DieterGraef 0:d26c1b55cfca 752 }
DieterGraef 0:d26c1b55cfca 753
DieterGraef 0:d26c1b55cfca 754
DieterGraef 0:d26c1b55cfca 755 /*
DieterGraef 0:d26c1b55cfca 756 * fsm_protreject - Peer doesn't speak this protocol.
DieterGraef 0:d26c1b55cfca 757 *
DieterGraef 0:d26c1b55cfca 758 * Treat this as a catastrophic error (RXJ-).
DieterGraef 0:d26c1b55cfca 759 */
DieterGraef 0:d26c1b55cfca 760 void
DieterGraef 0:d26c1b55cfca 761 fsm_protreject(fsm *f)
DieterGraef 0:d26c1b55cfca 762 {
DieterGraef 0:d26c1b55cfca 763 switch( f->state ) {
DieterGraef 0:d26c1b55cfca 764 case LS_CLOSING:
DieterGraef 0:d26c1b55cfca 765 UNTIMEOUT(fsm_timeout, f); /* Cancel timeout */
DieterGraef 0:d26c1b55cfca 766 /* fall through */
DieterGraef 0:d26c1b55cfca 767 case LS_CLOSED:
DieterGraef 0:d26c1b55cfca 768 f->state = LS_CLOSED;
DieterGraef 0:d26c1b55cfca 769 if( f->callbacks->finished ) {
DieterGraef 0:d26c1b55cfca 770 (*f->callbacks->finished)(f);
DieterGraef 0:d26c1b55cfca 771 }
DieterGraef 0:d26c1b55cfca 772 break;
DieterGraef 0:d26c1b55cfca 773
DieterGraef 0:d26c1b55cfca 774 case LS_STOPPING:
DieterGraef 0:d26c1b55cfca 775 case LS_REQSENT:
DieterGraef 0:d26c1b55cfca 776 case LS_ACKRCVD:
DieterGraef 0:d26c1b55cfca 777 case LS_ACKSENT:
DieterGraef 0:d26c1b55cfca 778 UNTIMEOUT(fsm_timeout, f); /* Cancel timeout */
DieterGraef 0:d26c1b55cfca 779 /* fall through */
DieterGraef 0:d26c1b55cfca 780 case LS_STOPPED:
DieterGraef 0:d26c1b55cfca 781 f->state = LS_STOPPED;
DieterGraef 0:d26c1b55cfca 782 if( f->callbacks->finished ) {
DieterGraef 0:d26c1b55cfca 783 (*f->callbacks->finished)(f);
DieterGraef 0:d26c1b55cfca 784 }
DieterGraef 0:d26c1b55cfca 785 break;
DieterGraef 0:d26c1b55cfca 786
DieterGraef 0:d26c1b55cfca 787 case LS_OPENED:
DieterGraef 0:d26c1b55cfca 788 if( f->callbacks->down ) {
DieterGraef 0:d26c1b55cfca 789 (*f->callbacks->down)(f);
DieterGraef 0:d26c1b55cfca 790 }
DieterGraef 0:d26c1b55cfca 791 /* Init restart counter, send Terminate-Request */
DieterGraef 0:d26c1b55cfca 792 f->retransmits = f->maxtermtransmits;
DieterGraef 0:d26c1b55cfca 793 fsm_sdata(f, TERMREQ, f->reqid = ++f->id,
DieterGraef 0:d26c1b55cfca 794 (u_char *) f->term_reason, f->term_reason_len);
DieterGraef 0:d26c1b55cfca 795 TIMEOUT(fsm_timeout, f, f->timeouttime);
DieterGraef 0:d26c1b55cfca 796 --f->retransmits;
DieterGraef 0:d26c1b55cfca 797
DieterGraef 0:d26c1b55cfca 798 f->state = LS_STOPPING;
DieterGraef 0:d26c1b55cfca 799 break;
DieterGraef 0:d26c1b55cfca 800
DieterGraef 0:d26c1b55cfca 801 default:
DieterGraef 0:d26c1b55cfca 802 FSMDEBUG(LOG_INFO, ("%s: Protocol-reject event in state %d (%s)!\n",
DieterGraef 0:d26c1b55cfca 803 PROTO_NAME(f), f->state, ppperr_strerr[f->state]));
DieterGraef 0:d26c1b55cfca 804 }
DieterGraef 0:d26c1b55cfca 805 }
DieterGraef 0:d26c1b55cfca 806
DieterGraef 0:d26c1b55cfca 807
DieterGraef 0:d26c1b55cfca 808 /*
DieterGraef 0:d26c1b55cfca 809 * fsm_sconfreq - Send a Configure-Request.
DieterGraef 0:d26c1b55cfca 810 */
DieterGraef 0:d26c1b55cfca 811 static void
DieterGraef 0:d26c1b55cfca 812 fsm_sconfreq(fsm *f, int retransmit)
DieterGraef 0:d26c1b55cfca 813 {
DieterGraef 0:d26c1b55cfca 814 u_char *outp;
DieterGraef 0:d26c1b55cfca 815 int cilen;
DieterGraef 0:d26c1b55cfca 816
DieterGraef 0:d26c1b55cfca 817 if( f->state != LS_REQSENT && f->state != LS_ACKRCVD && f->state != LS_ACKSENT ) {
DieterGraef 0:d26c1b55cfca 818 /* Not currently negotiating - reset options */
DieterGraef 0:d26c1b55cfca 819 if( f->callbacks->resetci ) {
DieterGraef 0:d26c1b55cfca 820 (*f->callbacks->resetci)(f);
DieterGraef 0:d26c1b55cfca 821 }
DieterGraef 0:d26c1b55cfca 822 f->nakloops = 0;
DieterGraef 0:d26c1b55cfca 823 }
DieterGraef 0:d26c1b55cfca 824
DieterGraef 0:d26c1b55cfca 825 if( !retransmit ) {
DieterGraef 0:d26c1b55cfca 826 /* New request - reset retransmission counter, use new ID */
DieterGraef 0:d26c1b55cfca 827 f->retransmits = f->maxconfreqtransmits;
DieterGraef 0:d26c1b55cfca 828 f->reqid = ++f->id;
DieterGraef 0:d26c1b55cfca 829 }
DieterGraef 0:d26c1b55cfca 830
DieterGraef 0:d26c1b55cfca 831 f->seen_ack = 0;
DieterGraef 0:d26c1b55cfca 832
DieterGraef 0:d26c1b55cfca 833 /*
DieterGraef 0:d26c1b55cfca 834 * Make up the request packet
DieterGraef 0:d26c1b55cfca 835 */
DieterGraef 0:d26c1b55cfca 836 outp = outpacket_buf[f->unit] + PPP_HDRLEN + HEADERLEN;
DieterGraef 0:d26c1b55cfca 837 if( f->callbacks->cilen && f->callbacks->addci ) {
DieterGraef 0:d26c1b55cfca 838 cilen = (*f->callbacks->cilen)(f);
DieterGraef 0:d26c1b55cfca 839 if( cilen > peer_mru[f->unit] - (int)HEADERLEN ) {
DieterGraef 0:d26c1b55cfca 840 cilen = peer_mru[f->unit] - HEADERLEN;
DieterGraef 0:d26c1b55cfca 841 }
DieterGraef 0:d26c1b55cfca 842 if (f->callbacks->addci) {
DieterGraef 0:d26c1b55cfca 843 (*f->callbacks->addci)(f, outp, &cilen);
DieterGraef 0:d26c1b55cfca 844 }
DieterGraef 0:d26c1b55cfca 845 } else {
DieterGraef 0:d26c1b55cfca 846 cilen = 0;
DieterGraef 0:d26c1b55cfca 847 }
DieterGraef 0:d26c1b55cfca 848
DieterGraef 0:d26c1b55cfca 849 /* send the request to our peer */
DieterGraef 0:d26c1b55cfca 850 fsm_sdata(f, CONFREQ, f->reqid, outp, cilen);
DieterGraef 0:d26c1b55cfca 851
DieterGraef 0:d26c1b55cfca 852 /* start the retransmit timer */
DieterGraef 0:d26c1b55cfca 853 --f->retransmits;
DieterGraef 0:d26c1b55cfca 854 TIMEOUT(fsm_timeout, f, f->timeouttime);
DieterGraef 0:d26c1b55cfca 855
DieterGraef 0:d26c1b55cfca 856 FSMDEBUG(LOG_INFO, ("%s: sending Configure-Request, id %d\n",
DieterGraef 0:d26c1b55cfca 857 PROTO_NAME(f), f->reqid));
DieterGraef 0:d26c1b55cfca 858 }
DieterGraef 0:d26c1b55cfca 859
DieterGraef 0:d26c1b55cfca 860
DieterGraef 0:d26c1b55cfca 861 /*
DieterGraef 0:d26c1b55cfca 862 * fsm_sdata - Send some data.
DieterGraef 0:d26c1b55cfca 863 *
DieterGraef 0:d26c1b55cfca 864 * Used for all packets sent to our peer by this module.
DieterGraef 0:d26c1b55cfca 865 */
DieterGraef 0:d26c1b55cfca 866 void
DieterGraef 0:d26c1b55cfca 867 fsm_sdata( fsm *f, u_char code, u_char id, u_char *data, int datalen)
DieterGraef 0:d26c1b55cfca 868 {
DieterGraef 0:d26c1b55cfca 869 u_char *outp;
DieterGraef 0:d26c1b55cfca 870 int outlen;
DieterGraef 0:d26c1b55cfca 871
DieterGraef 0:d26c1b55cfca 872 /* Adjust length to be smaller than MTU */
DieterGraef 0:d26c1b55cfca 873 outp = outpacket_buf[f->unit];
DieterGraef 0:d26c1b55cfca 874 if (datalen > peer_mru[f->unit] - (int)HEADERLEN) {
DieterGraef 0:d26c1b55cfca 875 datalen = peer_mru[f->unit] - HEADERLEN;
DieterGraef 0:d26c1b55cfca 876 }
DieterGraef 0:d26c1b55cfca 877 if (datalen && data != outp + PPP_HDRLEN + HEADERLEN) {
DieterGraef 0:d26c1b55cfca 878 BCOPY(data, outp + PPP_HDRLEN + HEADERLEN, datalen);
DieterGraef 0:d26c1b55cfca 879 }
DieterGraef 0:d26c1b55cfca 880 outlen = datalen + HEADERLEN;
DieterGraef 0:d26c1b55cfca 881 MAKEHEADER(outp, f->protocol);
DieterGraef 0:d26c1b55cfca 882 PUTCHAR(code, outp);
DieterGraef 0:d26c1b55cfca 883 PUTCHAR(id, outp);
DieterGraef 0:d26c1b55cfca 884 PUTSHORT(outlen, outp);
DieterGraef 0:d26c1b55cfca 885 pppWrite(f->unit, outpacket_buf[f->unit], outlen + PPP_HDRLEN);
DieterGraef 0:d26c1b55cfca 886 FSMDEBUG(LOG_INFO, ("fsm_sdata(%s): Sent code %d,%d,%d.\n",
DieterGraef 0:d26c1b55cfca 887 PROTO_NAME(f), code, id, outlen));
DieterGraef 0:d26c1b55cfca 888 }
DieterGraef 0:d26c1b55cfca 889
DieterGraef 0:d26c1b55cfca 890 #endif /* PPP_SUPPORT */