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