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