Version of http://mbed.org/cookbook/NetServicesTribute with setting set the same for LPC2368

Dependents:   UDPSocketExample 24LCxx_I2CApp WeatherPlatform_pachube HvZServerLib ... more

Committer:
simon
Date:
Tue Nov 23 14:15:36 2010 +0000
Revision:
0:350011bf8be7
Experimental version for testing UDP

Who changed what in which revision?

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