NetTribute library with debug turned on in FShandler Donatien Garner -> Segundo Equipo -> this version

Committer:
hexley
Date:
Fri Nov 19 01:54:45 2010 +0000
Revision:
0:281d6ff68967

        

Who changed what in which revision?

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