Ethernet test for ECE 4180 and others to find your IP address and do a simple HTTP GET request over port 80.

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Committer:
mkersh3
Date:
Thu Apr 04 05:26:09 2013 +0000
Revision:
0:e7ca326e76ee
Ethernet Test for ECE4180 and others to find their IP Address and do a simple HTTP GET request over port 80.

Who changed what in which revision?

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