I have a problem getting this to work. Server only recieves half of the data being sent. Whats wrong

Dependencies:   mbed

Committer:
tax
Date:
Tue Mar 29 13:20:15 2011 +0000
Revision:
0:66300c77c6e9

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tax 0:66300c77c6e9 1 /*** WARNING - THIS HAS NEVER BEEN FINISHED ***/
tax 0:66300c77c6e9 2 /*****************************************************************************
tax 0:66300c77c6e9 3 * chap.c - Network Challenge Handshake Authentication Protocol program file.
tax 0:66300c77c6e9 4 *
tax 0:66300c77c6e9 5 * Copyright (c) 2003 by Marc Boucher, Services Informatiques (MBSI) inc.
tax 0:66300c77c6e9 6 * portions Copyright (c) 1997 by Global Election Systems Inc.
tax 0:66300c77c6e9 7 *
tax 0:66300c77c6e9 8 * The authors hereby grant permission to use, copy, modify, distribute,
tax 0:66300c77c6e9 9 * and license this software and its documentation for any purpose, provided
tax 0:66300c77c6e9 10 * that existing copyright notices are retained in all copies and that this
tax 0:66300c77c6e9 11 * notice and the following disclaimer are included verbatim in any
tax 0:66300c77c6e9 12 * distributions. No written agreement, license, or royalty fee is required
tax 0:66300c77c6e9 13 * for any of the authorized uses.
tax 0:66300c77c6e9 14 *
tax 0:66300c77c6e9 15 * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS *AS IS* AND ANY EXPRESS OR
tax 0:66300c77c6e9 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
tax 0:66300c77c6e9 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
tax 0:66300c77c6e9 18 * IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
tax 0:66300c77c6e9 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
tax 0:66300c77c6e9 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
tax 0:66300c77c6e9 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
tax 0:66300c77c6e9 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
tax 0:66300c77c6e9 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
tax 0:66300c77c6e9 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
tax 0:66300c77c6e9 25 *
tax 0:66300c77c6e9 26 ******************************************************************************
tax 0:66300c77c6e9 27 * REVISION HISTORY
tax 0:66300c77c6e9 28 *
tax 0:66300c77c6e9 29 * 03-01-01 Marc Boucher <marc@mbsi.ca>
tax 0:66300c77c6e9 30 * Ported to lwIP.
tax 0:66300c77c6e9 31 * 97-12-04 Guy Lancaster <lancasterg@acm.org>, Global Election Systems Inc.
tax 0:66300c77c6e9 32 * Original based on BSD chap.c.
tax 0:66300c77c6e9 33 *****************************************************************************/
tax 0:66300c77c6e9 34 /*
tax 0:66300c77c6e9 35 * chap.c - Challenge Handshake Authentication Protocol.
tax 0:66300c77c6e9 36 *
tax 0:66300c77c6e9 37 * Copyright (c) 1993 The Australian National University.
tax 0:66300c77c6e9 38 * All rights reserved.
tax 0:66300c77c6e9 39 *
tax 0:66300c77c6e9 40 * Redistribution and use in source and binary forms are permitted
tax 0:66300c77c6e9 41 * provided that the above copyright notice and this paragraph are
tax 0:66300c77c6e9 42 * duplicated in all such forms and that any documentation,
tax 0:66300c77c6e9 43 * advertising materials, and other materials related to such
tax 0:66300c77c6e9 44 * distribution and use acknowledge that the software was developed
tax 0:66300c77c6e9 45 * by the Australian National University. The name of the University
tax 0:66300c77c6e9 46 * may not be used to endorse or promote products derived from this
tax 0:66300c77c6e9 47 * software without specific prior written permission.
tax 0:66300c77c6e9 48 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
tax 0:66300c77c6e9 49 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
tax 0:66300c77c6e9 50 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
tax 0:66300c77c6e9 51 *
tax 0:66300c77c6e9 52 * Copyright (c) 1991 Gregory M. Christy.
tax 0:66300c77c6e9 53 * All rights reserved.
tax 0:66300c77c6e9 54 *
tax 0:66300c77c6e9 55 * Redistribution and use in source and binary forms are permitted
tax 0:66300c77c6e9 56 * provided that the above copyright notice and this paragraph are
tax 0:66300c77c6e9 57 * duplicated in all such forms and that any documentation,
tax 0:66300c77c6e9 58 * advertising materials, and other materials related to such
tax 0:66300c77c6e9 59 * distribution and use acknowledge that the software was developed
tax 0:66300c77c6e9 60 * by Gregory M. Christy. The name of the author may not be used to
tax 0:66300c77c6e9 61 * endorse or promote products derived from this software without
tax 0:66300c77c6e9 62 * specific prior written permission.
tax 0:66300c77c6e9 63 *
tax 0:66300c77c6e9 64 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
tax 0:66300c77c6e9 65 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
tax 0:66300c77c6e9 66 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
tax 0:66300c77c6e9 67 */
tax 0:66300c77c6e9 68
tax 0:66300c77c6e9 69 #include "lwip/opt.h"
tax 0:66300c77c6e9 70
tax 0:66300c77c6e9 71 #if PPP_SUPPORT /* don't build if not configured for use in lwipopts.h */
tax 0:66300c77c6e9 72
tax 0:66300c77c6e9 73 #if CHAP_SUPPORT /* don't build if not configured for use in lwipopts.h */
tax 0:66300c77c6e9 74
tax 0:66300c77c6e9 75 #include "ppp.h"
tax 0:66300c77c6e9 76 #include "pppdebug.h"
tax 0:66300c77c6e9 77
tax 0:66300c77c6e9 78 #include "magic.h"
tax 0:66300c77c6e9 79 #include "randm.h"
tax 0:66300c77c6e9 80 #include "auth.h"
tax 0:66300c77c6e9 81 #include "md5.h"
tax 0:66300c77c6e9 82 #include "chap.h"
tax 0:66300c77c6e9 83 #include "chpms.h"
tax 0:66300c77c6e9 84
tax 0:66300c77c6e9 85 #include <string.h>
tax 0:66300c77c6e9 86
tax 0:66300c77c6e9 87 #if 0 /* UNUSED */
tax 0:66300c77c6e9 88 /*
tax 0:66300c77c6e9 89 * Command-line options.
tax 0:66300c77c6e9 90 */
tax 0:66300c77c6e9 91 static option_t chap_option_list[] = {
tax 0:66300c77c6e9 92 { "chap-restart", o_int, &chap[0].timeouttime,
tax 0:66300c77c6e9 93 "Set timeout for CHAP" },
tax 0:66300c77c6e9 94 { "chap-max-challenge", o_int, &chap[0].max_transmits,
tax 0:66300c77c6e9 95 "Set max #xmits for challenge" },
tax 0:66300c77c6e9 96 { "chap-interval", o_int, &chap[0].chal_interval,
tax 0:66300c77c6e9 97 "Set interval for rechallenge" },
tax 0:66300c77c6e9 98 #ifdef MSLANMAN
tax 0:66300c77c6e9 99 { "ms-lanman", o_bool, &ms_lanman,
tax 0:66300c77c6e9 100 "Use LanMan passwd when using MS-CHAP", 1 },
tax 0:66300c77c6e9 101 #endif
tax 0:66300c77c6e9 102 { NULL }
tax 0:66300c77c6e9 103 };
tax 0:66300c77c6e9 104 #endif /* UNUSED */
tax 0:66300c77c6e9 105
tax 0:66300c77c6e9 106 /*
tax 0:66300c77c6e9 107 * Protocol entry points.
tax 0:66300c77c6e9 108 */
tax 0:66300c77c6e9 109 static void ChapInit (int);
tax 0:66300c77c6e9 110 static void ChapLowerUp (int);
tax 0:66300c77c6e9 111 static void ChapLowerDown (int);
tax 0:66300c77c6e9 112 static void ChapInput (int, u_char *, int);
tax 0:66300c77c6e9 113 static void ChapProtocolReject (int);
tax 0:66300c77c6e9 114 #if PPP_ADDITIONAL_CALLBACKS
tax 0:66300c77c6e9 115 static int ChapPrintPkt (u_char *, int, void (*) (void *, char *, ...), void *);
tax 0:66300c77c6e9 116 #endif
tax 0:66300c77c6e9 117
tax 0:66300c77c6e9 118 struct protent chap_protent = {
tax 0:66300c77c6e9 119 PPP_CHAP,
tax 0:66300c77c6e9 120 ChapInit,
tax 0:66300c77c6e9 121 ChapInput,
tax 0:66300c77c6e9 122 ChapProtocolReject,
tax 0:66300c77c6e9 123 ChapLowerUp,
tax 0:66300c77c6e9 124 ChapLowerDown,
tax 0:66300c77c6e9 125 NULL,
tax 0:66300c77c6e9 126 NULL,
tax 0:66300c77c6e9 127 #if PPP_ADDITIONAL_CALLBACKS
tax 0:66300c77c6e9 128 ChapPrintPkt,
tax 0:66300c77c6e9 129 NULL,
tax 0:66300c77c6e9 130 #endif /* PPP_ADDITIONAL_CALLBACKS */
tax 0:66300c77c6e9 131 1,
tax 0:66300c77c6e9 132 "CHAP",
tax 0:66300c77c6e9 133 #if PPP_ADDITIONAL_CALLBACKS
tax 0:66300c77c6e9 134 NULL,
tax 0:66300c77c6e9 135 NULL,
tax 0:66300c77c6e9 136 NULL
tax 0:66300c77c6e9 137 #endif /* PPP_ADDITIONAL_CALLBACKS */
tax 0:66300c77c6e9 138 };
tax 0:66300c77c6e9 139
tax 0:66300c77c6e9 140 chap_state chap[NUM_PPP]; /* CHAP state; one for each unit */
tax 0:66300c77c6e9 141
tax 0:66300c77c6e9 142 static void ChapChallengeTimeout (void *);
tax 0:66300c77c6e9 143 static void ChapResponseTimeout (void *);
tax 0:66300c77c6e9 144 static void ChapReceiveChallenge (chap_state *, u_char *, u_char, int);
tax 0:66300c77c6e9 145 static void ChapRechallenge (void *);
tax 0:66300c77c6e9 146 static void ChapReceiveResponse (chap_state *, u_char *, int, int);
tax 0:66300c77c6e9 147 static void ChapReceiveSuccess(chap_state *cstate, u_char *inp, u_char id, int len);
tax 0:66300c77c6e9 148 static void ChapReceiveFailure(chap_state *cstate, u_char *inp, u_char id, int len);
tax 0:66300c77c6e9 149 static void ChapSendStatus (chap_state *, int);
tax 0:66300c77c6e9 150 static void ChapSendChallenge (chap_state *);
tax 0:66300c77c6e9 151 static void ChapSendResponse (chap_state *);
tax 0:66300c77c6e9 152 static void ChapGenChallenge (chap_state *);
tax 0:66300c77c6e9 153
tax 0:66300c77c6e9 154 /*
tax 0:66300c77c6e9 155 * ChapInit - Initialize a CHAP unit.
tax 0:66300c77c6e9 156 */
tax 0:66300c77c6e9 157 static void
tax 0:66300c77c6e9 158 ChapInit(int unit)
tax 0:66300c77c6e9 159 {
tax 0:66300c77c6e9 160 chap_state *cstate = &chap[unit];
tax 0:66300c77c6e9 161
tax 0:66300c77c6e9 162 BZERO(cstate, sizeof(*cstate));
tax 0:66300c77c6e9 163 cstate->unit = unit;
tax 0:66300c77c6e9 164 cstate->clientstate = CHAPCS_INITIAL;
tax 0:66300c77c6e9 165 cstate->serverstate = CHAPSS_INITIAL;
tax 0:66300c77c6e9 166 cstate->timeouttime = CHAP_DEFTIMEOUT;
tax 0:66300c77c6e9 167 cstate->max_transmits = CHAP_DEFTRANSMITS;
tax 0:66300c77c6e9 168 /* random number generator is initialized in magic_init */
tax 0:66300c77c6e9 169 }
tax 0:66300c77c6e9 170
tax 0:66300c77c6e9 171
tax 0:66300c77c6e9 172 /*
tax 0:66300c77c6e9 173 * ChapAuthWithPeer - Authenticate us with our peer (start client).
tax 0:66300c77c6e9 174 *
tax 0:66300c77c6e9 175 */
tax 0:66300c77c6e9 176 void
tax 0:66300c77c6e9 177 ChapAuthWithPeer(int unit, char *our_name, u_char digest)
tax 0:66300c77c6e9 178 {
tax 0:66300c77c6e9 179 chap_state *cstate = &chap[unit];
tax 0:66300c77c6e9 180
tax 0:66300c77c6e9 181 cstate->resp_name = our_name;
tax 0:66300c77c6e9 182 cstate->resp_type = digest;
tax 0:66300c77c6e9 183
tax 0:66300c77c6e9 184 if (cstate->clientstate == CHAPCS_INITIAL ||
tax 0:66300c77c6e9 185 cstate->clientstate == CHAPCS_PENDING) {
tax 0:66300c77c6e9 186 /* lower layer isn't up - wait until later */
tax 0:66300c77c6e9 187 cstate->clientstate = CHAPCS_PENDING;
tax 0:66300c77c6e9 188 return;
tax 0:66300c77c6e9 189 }
tax 0:66300c77c6e9 190
tax 0:66300c77c6e9 191 /*
tax 0:66300c77c6e9 192 * We get here as a result of LCP coming up.
tax 0:66300c77c6e9 193 * So even if CHAP was open before, we will
tax 0:66300c77c6e9 194 * have to re-authenticate ourselves.
tax 0:66300c77c6e9 195 */
tax 0:66300c77c6e9 196 cstate->clientstate = CHAPCS_LISTEN;
tax 0:66300c77c6e9 197 }
tax 0:66300c77c6e9 198
tax 0:66300c77c6e9 199
tax 0:66300c77c6e9 200 /*
tax 0:66300c77c6e9 201 * ChapAuthPeer - Authenticate our peer (start server).
tax 0:66300c77c6e9 202 */
tax 0:66300c77c6e9 203 void
tax 0:66300c77c6e9 204 ChapAuthPeer(int unit, char *our_name, u_char digest)
tax 0:66300c77c6e9 205 {
tax 0:66300c77c6e9 206 chap_state *cstate = &chap[unit];
tax 0:66300c77c6e9 207
tax 0:66300c77c6e9 208 cstate->chal_name = our_name;
tax 0:66300c77c6e9 209 cstate->chal_type = digest;
tax 0:66300c77c6e9 210
tax 0:66300c77c6e9 211 if (cstate->serverstate == CHAPSS_INITIAL ||
tax 0:66300c77c6e9 212 cstate->serverstate == CHAPSS_PENDING) {
tax 0:66300c77c6e9 213 /* lower layer isn't up - wait until later */
tax 0:66300c77c6e9 214 cstate->serverstate = CHAPSS_PENDING;
tax 0:66300c77c6e9 215 return;
tax 0:66300c77c6e9 216 }
tax 0:66300c77c6e9 217
tax 0:66300c77c6e9 218 ChapGenChallenge(cstate);
tax 0:66300c77c6e9 219 ChapSendChallenge(cstate); /* crank it up dude! */
tax 0:66300c77c6e9 220 cstate->serverstate = CHAPSS_INITIAL_CHAL;
tax 0:66300c77c6e9 221 }
tax 0:66300c77c6e9 222
tax 0:66300c77c6e9 223
tax 0:66300c77c6e9 224 /*
tax 0:66300c77c6e9 225 * ChapChallengeTimeout - Timeout expired on sending challenge.
tax 0:66300c77c6e9 226 */
tax 0:66300c77c6e9 227 static void
tax 0:66300c77c6e9 228 ChapChallengeTimeout(void *arg)
tax 0:66300c77c6e9 229 {
tax 0:66300c77c6e9 230 chap_state *cstate = (chap_state *) arg;
tax 0:66300c77c6e9 231
tax 0:66300c77c6e9 232 /* if we aren't sending challenges, don't worry. then again we */
tax 0:66300c77c6e9 233 /* probably shouldn't be here either */
tax 0:66300c77c6e9 234 if (cstate->serverstate != CHAPSS_INITIAL_CHAL &&
tax 0:66300c77c6e9 235 cstate->serverstate != CHAPSS_RECHALLENGE) {
tax 0:66300c77c6e9 236 return;
tax 0:66300c77c6e9 237 }
tax 0:66300c77c6e9 238
tax 0:66300c77c6e9 239 if (cstate->chal_transmits >= cstate->max_transmits) {
tax 0:66300c77c6e9 240 /* give up on peer */
tax 0:66300c77c6e9 241 CHAPDEBUG(LOG_ERR, ("Peer failed to respond to CHAP challenge\n"));
tax 0:66300c77c6e9 242 cstate->serverstate = CHAPSS_BADAUTH;
tax 0:66300c77c6e9 243 auth_peer_fail(cstate->unit, PPP_CHAP);
tax 0:66300c77c6e9 244 return;
tax 0:66300c77c6e9 245 }
tax 0:66300c77c6e9 246
tax 0:66300c77c6e9 247 ChapSendChallenge(cstate); /* Re-send challenge */
tax 0:66300c77c6e9 248 }
tax 0:66300c77c6e9 249
tax 0:66300c77c6e9 250
tax 0:66300c77c6e9 251 /*
tax 0:66300c77c6e9 252 * ChapResponseTimeout - Timeout expired on sending response.
tax 0:66300c77c6e9 253 */
tax 0:66300c77c6e9 254 static void
tax 0:66300c77c6e9 255 ChapResponseTimeout(void *arg)
tax 0:66300c77c6e9 256 {
tax 0:66300c77c6e9 257 chap_state *cstate = (chap_state *) arg;
tax 0:66300c77c6e9 258
tax 0:66300c77c6e9 259 /* if we aren't sending a response, don't worry. */
tax 0:66300c77c6e9 260 if (cstate->clientstate != CHAPCS_RESPONSE) {
tax 0:66300c77c6e9 261 return;
tax 0:66300c77c6e9 262 }
tax 0:66300c77c6e9 263
tax 0:66300c77c6e9 264 ChapSendResponse(cstate); /* re-send response */
tax 0:66300c77c6e9 265 }
tax 0:66300c77c6e9 266
tax 0:66300c77c6e9 267
tax 0:66300c77c6e9 268 /*
tax 0:66300c77c6e9 269 * ChapRechallenge - Time to challenge the peer again.
tax 0:66300c77c6e9 270 */
tax 0:66300c77c6e9 271 static void
tax 0:66300c77c6e9 272 ChapRechallenge(void *arg)
tax 0:66300c77c6e9 273 {
tax 0:66300c77c6e9 274 chap_state *cstate = (chap_state *) arg;
tax 0:66300c77c6e9 275
tax 0:66300c77c6e9 276 /* if we aren't sending a response, don't worry. */
tax 0:66300c77c6e9 277 if (cstate->serverstate != CHAPSS_OPEN) {
tax 0:66300c77c6e9 278 return;
tax 0:66300c77c6e9 279 }
tax 0:66300c77c6e9 280
tax 0:66300c77c6e9 281 ChapGenChallenge(cstate);
tax 0:66300c77c6e9 282 ChapSendChallenge(cstate);
tax 0:66300c77c6e9 283 cstate->serverstate = CHAPSS_RECHALLENGE;
tax 0:66300c77c6e9 284 }
tax 0:66300c77c6e9 285
tax 0:66300c77c6e9 286
tax 0:66300c77c6e9 287 /*
tax 0:66300c77c6e9 288 * ChapLowerUp - The lower layer is up.
tax 0:66300c77c6e9 289 *
tax 0:66300c77c6e9 290 * Start up if we have pending requests.
tax 0:66300c77c6e9 291 */
tax 0:66300c77c6e9 292 static void
tax 0:66300c77c6e9 293 ChapLowerUp(int unit)
tax 0:66300c77c6e9 294 {
tax 0:66300c77c6e9 295 chap_state *cstate = &chap[unit];
tax 0:66300c77c6e9 296
tax 0:66300c77c6e9 297 if (cstate->clientstate == CHAPCS_INITIAL) {
tax 0:66300c77c6e9 298 cstate->clientstate = CHAPCS_CLOSED;
tax 0:66300c77c6e9 299 } else if (cstate->clientstate == CHAPCS_PENDING) {
tax 0:66300c77c6e9 300 cstate->clientstate = CHAPCS_LISTEN;
tax 0:66300c77c6e9 301 }
tax 0:66300c77c6e9 302
tax 0:66300c77c6e9 303 if (cstate->serverstate == CHAPSS_INITIAL) {
tax 0:66300c77c6e9 304 cstate->serverstate = CHAPSS_CLOSED;
tax 0:66300c77c6e9 305 } else if (cstate->serverstate == CHAPSS_PENDING) {
tax 0:66300c77c6e9 306 ChapGenChallenge(cstate);
tax 0:66300c77c6e9 307 ChapSendChallenge(cstate);
tax 0:66300c77c6e9 308 cstate->serverstate = CHAPSS_INITIAL_CHAL;
tax 0:66300c77c6e9 309 }
tax 0:66300c77c6e9 310 }
tax 0:66300c77c6e9 311
tax 0:66300c77c6e9 312
tax 0:66300c77c6e9 313 /*
tax 0:66300c77c6e9 314 * ChapLowerDown - The lower layer is down.
tax 0:66300c77c6e9 315 *
tax 0:66300c77c6e9 316 * Cancel all timeouts.
tax 0:66300c77c6e9 317 */
tax 0:66300c77c6e9 318 static void
tax 0:66300c77c6e9 319 ChapLowerDown(int unit)
tax 0:66300c77c6e9 320 {
tax 0:66300c77c6e9 321 chap_state *cstate = &chap[unit];
tax 0:66300c77c6e9 322
tax 0:66300c77c6e9 323 /* Timeout(s) pending? Cancel if so. */
tax 0:66300c77c6e9 324 if (cstate->serverstate == CHAPSS_INITIAL_CHAL ||
tax 0:66300c77c6e9 325 cstate->serverstate == CHAPSS_RECHALLENGE) {
tax 0:66300c77c6e9 326 UNTIMEOUT(ChapChallengeTimeout, cstate);
tax 0:66300c77c6e9 327 } else if (cstate->serverstate == CHAPSS_OPEN
tax 0:66300c77c6e9 328 && cstate->chal_interval != 0) {
tax 0:66300c77c6e9 329 UNTIMEOUT(ChapRechallenge, cstate);
tax 0:66300c77c6e9 330 }
tax 0:66300c77c6e9 331 if (cstate->clientstate == CHAPCS_RESPONSE) {
tax 0:66300c77c6e9 332 UNTIMEOUT(ChapResponseTimeout, cstate);
tax 0:66300c77c6e9 333 }
tax 0:66300c77c6e9 334 cstate->clientstate = CHAPCS_INITIAL;
tax 0:66300c77c6e9 335 cstate->serverstate = CHAPSS_INITIAL;
tax 0:66300c77c6e9 336 }
tax 0:66300c77c6e9 337
tax 0:66300c77c6e9 338
tax 0:66300c77c6e9 339 /*
tax 0:66300c77c6e9 340 * ChapProtocolReject - Peer doesn't grok CHAP.
tax 0:66300c77c6e9 341 */
tax 0:66300c77c6e9 342 static void
tax 0:66300c77c6e9 343 ChapProtocolReject(int unit)
tax 0:66300c77c6e9 344 {
tax 0:66300c77c6e9 345 chap_state *cstate = &chap[unit];
tax 0:66300c77c6e9 346
tax 0:66300c77c6e9 347 if (cstate->serverstate != CHAPSS_INITIAL &&
tax 0:66300c77c6e9 348 cstate->serverstate != CHAPSS_CLOSED) {
tax 0:66300c77c6e9 349 auth_peer_fail(unit, PPP_CHAP);
tax 0:66300c77c6e9 350 }
tax 0:66300c77c6e9 351 if (cstate->clientstate != CHAPCS_INITIAL &&
tax 0:66300c77c6e9 352 cstate->clientstate != CHAPCS_CLOSED) {
tax 0:66300c77c6e9 353 auth_withpeer_fail(unit, PPP_CHAP); /* lwip: just sets the PPP error code on this unit to PPPERR_AUTHFAIL */
tax 0:66300c77c6e9 354 }
tax 0:66300c77c6e9 355 ChapLowerDown(unit); /* shutdown chap */
tax 0:66300c77c6e9 356 }
tax 0:66300c77c6e9 357
tax 0:66300c77c6e9 358
tax 0:66300c77c6e9 359 /*
tax 0:66300c77c6e9 360 * ChapInput - Input CHAP packet.
tax 0:66300c77c6e9 361 */
tax 0:66300c77c6e9 362 static void
tax 0:66300c77c6e9 363 ChapInput(int unit, u_char *inpacket, int packet_len)
tax 0:66300c77c6e9 364 {
tax 0:66300c77c6e9 365 chap_state *cstate = &chap[unit];
tax 0:66300c77c6e9 366 u_char *inp;
tax 0:66300c77c6e9 367 u_char code, id;
tax 0:66300c77c6e9 368 int len;
tax 0:66300c77c6e9 369
tax 0:66300c77c6e9 370 /*
tax 0:66300c77c6e9 371 * Parse header (code, id and length).
tax 0:66300c77c6e9 372 * If packet too short, drop it.
tax 0:66300c77c6e9 373 */
tax 0:66300c77c6e9 374 inp = inpacket;
tax 0:66300c77c6e9 375 if (packet_len < CHAP_HEADERLEN) {
tax 0:66300c77c6e9 376 CHAPDEBUG(LOG_INFO, ("ChapInput: rcvd short header.\n"));
tax 0:66300c77c6e9 377 return;
tax 0:66300c77c6e9 378 }
tax 0:66300c77c6e9 379 GETCHAR(code, inp);
tax 0:66300c77c6e9 380 GETCHAR(id, inp);
tax 0:66300c77c6e9 381 GETSHORT(len, inp);
tax 0:66300c77c6e9 382 if (len < CHAP_HEADERLEN) {
tax 0:66300c77c6e9 383 CHAPDEBUG(LOG_INFO, ("ChapInput: rcvd illegal length.\n"));
tax 0:66300c77c6e9 384 return;
tax 0:66300c77c6e9 385 }
tax 0:66300c77c6e9 386 if (len > packet_len) {
tax 0:66300c77c6e9 387 CHAPDEBUG(LOG_INFO, ("ChapInput: rcvd short packet.\n"));
tax 0:66300c77c6e9 388 return;
tax 0:66300c77c6e9 389 }
tax 0:66300c77c6e9 390 len -= CHAP_HEADERLEN;
tax 0:66300c77c6e9 391
tax 0:66300c77c6e9 392 /*
tax 0:66300c77c6e9 393 * Action depends on code (as in fact it usually does :-).
tax 0:66300c77c6e9 394 */
tax 0:66300c77c6e9 395 switch (code) {
tax 0:66300c77c6e9 396 case CHAP_CHALLENGE:
tax 0:66300c77c6e9 397 ChapReceiveChallenge(cstate, inp, id, len);
tax 0:66300c77c6e9 398 break;
tax 0:66300c77c6e9 399
tax 0:66300c77c6e9 400 case CHAP_RESPONSE:
tax 0:66300c77c6e9 401 ChapReceiveResponse(cstate, inp, id, len);
tax 0:66300c77c6e9 402 break;
tax 0:66300c77c6e9 403
tax 0:66300c77c6e9 404 case CHAP_FAILURE:
tax 0:66300c77c6e9 405 ChapReceiveFailure(cstate, inp, id, len);
tax 0:66300c77c6e9 406 break;
tax 0:66300c77c6e9 407
tax 0:66300c77c6e9 408 case CHAP_SUCCESS:
tax 0:66300c77c6e9 409 ChapReceiveSuccess(cstate, inp, id, len);
tax 0:66300c77c6e9 410 break;
tax 0:66300c77c6e9 411
tax 0:66300c77c6e9 412 default: /* Need code reject? */
tax 0:66300c77c6e9 413 CHAPDEBUG(LOG_WARNING, ("Unknown CHAP code (%d) received.\n", code));
tax 0:66300c77c6e9 414 break;
tax 0:66300c77c6e9 415 }
tax 0:66300c77c6e9 416 }
tax 0:66300c77c6e9 417
tax 0:66300c77c6e9 418
tax 0:66300c77c6e9 419 /*
tax 0:66300c77c6e9 420 * ChapReceiveChallenge - Receive Challenge and send Response.
tax 0:66300c77c6e9 421 */
tax 0:66300c77c6e9 422 static void
tax 0:66300c77c6e9 423 ChapReceiveChallenge(chap_state *cstate, u_char *inp, u_char id, int len)
tax 0:66300c77c6e9 424 {
tax 0:66300c77c6e9 425 int rchallenge_len;
tax 0:66300c77c6e9 426 u_char *rchallenge;
tax 0:66300c77c6e9 427 int secret_len;
tax 0:66300c77c6e9 428 char secret[MAXSECRETLEN];
tax 0:66300c77c6e9 429 char rhostname[256];
tax 0:66300c77c6e9 430 MD5_CTX mdContext;
tax 0:66300c77c6e9 431 u_char hash[MD5_SIGNATURE_SIZE];
tax 0:66300c77c6e9 432
tax 0:66300c77c6e9 433 CHAPDEBUG(LOG_INFO, ("ChapReceiveChallenge: Rcvd id %d.\n", id));
tax 0:66300c77c6e9 434 if (cstate->clientstate == CHAPCS_CLOSED ||
tax 0:66300c77c6e9 435 cstate->clientstate == CHAPCS_PENDING) {
tax 0:66300c77c6e9 436 CHAPDEBUG(LOG_INFO, ("ChapReceiveChallenge: in state %d\n",
tax 0:66300c77c6e9 437 cstate->clientstate));
tax 0:66300c77c6e9 438 return;
tax 0:66300c77c6e9 439 }
tax 0:66300c77c6e9 440
tax 0:66300c77c6e9 441 if (len < 2) {
tax 0:66300c77c6e9 442 CHAPDEBUG(LOG_INFO, ("ChapReceiveChallenge: rcvd short packet.\n"));
tax 0:66300c77c6e9 443 return;
tax 0:66300c77c6e9 444 }
tax 0:66300c77c6e9 445
tax 0:66300c77c6e9 446 GETCHAR(rchallenge_len, inp);
tax 0:66300c77c6e9 447 len -= sizeof (u_char) + rchallenge_len; /* now name field length */
tax 0:66300c77c6e9 448 if (len < 0) {
tax 0:66300c77c6e9 449 CHAPDEBUG(LOG_INFO, ("ChapReceiveChallenge: rcvd short packet.\n"));
tax 0:66300c77c6e9 450 return;
tax 0:66300c77c6e9 451 }
tax 0:66300c77c6e9 452 rchallenge = inp;
tax 0:66300c77c6e9 453 INCPTR(rchallenge_len, inp);
tax 0:66300c77c6e9 454
tax 0:66300c77c6e9 455 if (len >= (int)sizeof(rhostname)) {
tax 0:66300c77c6e9 456 len = sizeof(rhostname) - 1;
tax 0:66300c77c6e9 457 }
tax 0:66300c77c6e9 458 BCOPY(inp, rhostname, len);
tax 0:66300c77c6e9 459 rhostname[len] = '\000';
tax 0:66300c77c6e9 460
tax 0:66300c77c6e9 461 CHAPDEBUG(LOG_INFO, ("ChapReceiveChallenge: received name field '%s'\n",
tax 0:66300c77c6e9 462 rhostname));
tax 0:66300c77c6e9 463
tax 0:66300c77c6e9 464 /* Microsoft doesn't send their name back in the PPP packet */
tax 0:66300c77c6e9 465 if (ppp_settings.remote_name[0] != 0 && (ppp_settings.explicit_remote || rhostname[0] == 0)) {
tax 0:66300c77c6e9 466 strncpy(rhostname, ppp_settings.remote_name, sizeof(rhostname));
tax 0:66300c77c6e9 467 rhostname[sizeof(rhostname) - 1] = 0;
tax 0:66300c77c6e9 468 CHAPDEBUG(LOG_INFO, ("ChapReceiveChallenge: using '%s' as remote name\n",
tax 0:66300c77c6e9 469 rhostname));
tax 0:66300c77c6e9 470 }
tax 0:66300c77c6e9 471
tax 0:66300c77c6e9 472 /* get secret for authenticating ourselves with the specified host */
tax 0:66300c77c6e9 473 if (!get_secret(cstate->unit, cstate->resp_name, rhostname,
tax 0:66300c77c6e9 474 secret, &secret_len, 0)) {
tax 0:66300c77c6e9 475 secret_len = 0; /* assume null secret if can't find one */
tax 0:66300c77c6e9 476 CHAPDEBUG(LOG_WARNING, ("No CHAP secret found for authenticating us to %s\n",
tax 0:66300c77c6e9 477 rhostname));
tax 0:66300c77c6e9 478 }
tax 0:66300c77c6e9 479
tax 0:66300c77c6e9 480 /* cancel response send timeout if necessary */
tax 0:66300c77c6e9 481 if (cstate->clientstate == CHAPCS_RESPONSE) {
tax 0:66300c77c6e9 482 UNTIMEOUT(ChapResponseTimeout, cstate);
tax 0:66300c77c6e9 483 }
tax 0:66300c77c6e9 484
tax 0:66300c77c6e9 485 cstate->resp_id = id;
tax 0:66300c77c6e9 486 cstate->resp_transmits = 0;
tax 0:66300c77c6e9 487
tax 0:66300c77c6e9 488 /* generate MD based on negotiated type */
tax 0:66300c77c6e9 489 switch (cstate->resp_type) {
tax 0:66300c77c6e9 490
tax 0:66300c77c6e9 491 case CHAP_DIGEST_MD5:
tax 0:66300c77c6e9 492 MD5Init(&mdContext);
tax 0:66300c77c6e9 493 MD5Update(&mdContext, &cstate->resp_id, 1);
tax 0:66300c77c6e9 494 MD5Update(&mdContext, (u_char*)secret, secret_len);
tax 0:66300c77c6e9 495 MD5Update(&mdContext, rchallenge, rchallenge_len);
tax 0:66300c77c6e9 496 MD5Final(hash, &mdContext);
tax 0:66300c77c6e9 497 BCOPY(hash, cstate->response, MD5_SIGNATURE_SIZE);
tax 0:66300c77c6e9 498 cstate->resp_length = MD5_SIGNATURE_SIZE;
tax 0:66300c77c6e9 499 break;
tax 0:66300c77c6e9 500
tax 0:66300c77c6e9 501 #if MSCHAP_SUPPORT
tax 0:66300c77c6e9 502 case CHAP_MICROSOFT:
tax 0:66300c77c6e9 503 ChapMS(cstate, rchallenge, rchallenge_len, secret, secret_len);
tax 0:66300c77c6e9 504 break;
tax 0:66300c77c6e9 505 #endif
tax 0:66300c77c6e9 506
tax 0:66300c77c6e9 507 default:
tax 0:66300c77c6e9 508 CHAPDEBUG(LOG_INFO, ("unknown digest type %d\n", cstate->resp_type));
tax 0:66300c77c6e9 509 return;
tax 0:66300c77c6e9 510 }
tax 0:66300c77c6e9 511
tax 0:66300c77c6e9 512 BZERO(secret, sizeof(secret));
tax 0:66300c77c6e9 513 ChapSendResponse(cstate);
tax 0:66300c77c6e9 514 }
tax 0:66300c77c6e9 515
tax 0:66300c77c6e9 516
tax 0:66300c77c6e9 517 /*
tax 0:66300c77c6e9 518 * ChapReceiveResponse - Receive and process response.
tax 0:66300c77c6e9 519 */
tax 0:66300c77c6e9 520 static void
tax 0:66300c77c6e9 521 ChapReceiveResponse(chap_state *cstate, u_char *inp, int id, int len)
tax 0:66300c77c6e9 522 {
tax 0:66300c77c6e9 523 u_char *remmd, remmd_len;
tax 0:66300c77c6e9 524 int secret_len, old_state;
tax 0:66300c77c6e9 525 int code;
tax 0:66300c77c6e9 526 char rhostname[256];
tax 0:66300c77c6e9 527 MD5_CTX mdContext;
tax 0:66300c77c6e9 528 char secret[MAXSECRETLEN];
tax 0:66300c77c6e9 529 u_char hash[MD5_SIGNATURE_SIZE];
tax 0:66300c77c6e9 530
tax 0:66300c77c6e9 531 CHAPDEBUG(LOG_INFO, ("ChapReceiveResponse: Rcvd id %d.\n", id));
tax 0:66300c77c6e9 532
tax 0:66300c77c6e9 533 if (cstate->serverstate == CHAPSS_CLOSED ||
tax 0:66300c77c6e9 534 cstate->serverstate == CHAPSS_PENDING) {
tax 0:66300c77c6e9 535 CHAPDEBUG(LOG_INFO, ("ChapReceiveResponse: in state %d\n",
tax 0:66300c77c6e9 536 cstate->serverstate));
tax 0:66300c77c6e9 537 return;
tax 0:66300c77c6e9 538 }
tax 0:66300c77c6e9 539
tax 0:66300c77c6e9 540 if (id != cstate->chal_id) {
tax 0:66300c77c6e9 541 return; /* doesn't match ID of last challenge */
tax 0:66300c77c6e9 542 }
tax 0:66300c77c6e9 543
tax 0:66300c77c6e9 544 /*
tax 0:66300c77c6e9 545 * If we have received a duplicate or bogus Response,
tax 0:66300c77c6e9 546 * we have to send the same answer (Success/Failure)
tax 0:66300c77c6e9 547 * as we did for the first Response we saw.
tax 0:66300c77c6e9 548 */
tax 0:66300c77c6e9 549 if (cstate->serverstate == CHAPSS_OPEN) {
tax 0:66300c77c6e9 550 ChapSendStatus(cstate, CHAP_SUCCESS);
tax 0:66300c77c6e9 551 return;
tax 0:66300c77c6e9 552 }
tax 0:66300c77c6e9 553 if (cstate->serverstate == CHAPSS_BADAUTH) {
tax 0:66300c77c6e9 554 ChapSendStatus(cstate, CHAP_FAILURE);
tax 0:66300c77c6e9 555 return;
tax 0:66300c77c6e9 556 }
tax 0:66300c77c6e9 557
tax 0:66300c77c6e9 558 if (len < 2) {
tax 0:66300c77c6e9 559 CHAPDEBUG(LOG_INFO, ("ChapReceiveResponse: rcvd short packet.\n"));
tax 0:66300c77c6e9 560 return;
tax 0:66300c77c6e9 561 }
tax 0:66300c77c6e9 562 GETCHAR(remmd_len, inp); /* get length of MD */
tax 0:66300c77c6e9 563 remmd = inp; /* get pointer to MD */
tax 0:66300c77c6e9 564 INCPTR(remmd_len, inp);
tax 0:66300c77c6e9 565
tax 0:66300c77c6e9 566 len -= sizeof (u_char) + remmd_len;
tax 0:66300c77c6e9 567 if (len < 0) {
tax 0:66300c77c6e9 568 CHAPDEBUG(LOG_INFO, ("ChapReceiveResponse: rcvd short packet.\n"));
tax 0:66300c77c6e9 569 return;
tax 0:66300c77c6e9 570 }
tax 0:66300c77c6e9 571
tax 0:66300c77c6e9 572 UNTIMEOUT(ChapChallengeTimeout, cstate);
tax 0:66300c77c6e9 573
tax 0:66300c77c6e9 574 if (len >= (int)sizeof(rhostname)) {
tax 0:66300c77c6e9 575 len = sizeof(rhostname) - 1;
tax 0:66300c77c6e9 576 }
tax 0:66300c77c6e9 577 BCOPY(inp, rhostname, len);
tax 0:66300c77c6e9 578 rhostname[len] = '\000';
tax 0:66300c77c6e9 579
tax 0:66300c77c6e9 580 CHAPDEBUG(LOG_INFO, ("ChapReceiveResponse: received name field: %s\n",
tax 0:66300c77c6e9 581 rhostname));
tax 0:66300c77c6e9 582
tax 0:66300c77c6e9 583 /*
tax 0:66300c77c6e9 584 * Get secret for authenticating them with us,
tax 0:66300c77c6e9 585 * do the hash ourselves, and compare the result.
tax 0:66300c77c6e9 586 */
tax 0:66300c77c6e9 587 code = CHAP_FAILURE;
tax 0:66300c77c6e9 588 if (!get_secret(cstate->unit, rhostname, cstate->chal_name,
tax 0:66300c77c6e9 589 secret, &secret_len, 1)) {
tax 0:66300c77c6e9 590 CHAPDEBUG(LOG_WARNING, ("No CHAP secret found for authenticating %s\n",
tax 0:66300c77c6e9 591 rhostname));
tax 0:66300c77c6e9 592 } else {
tax 0:66300c77c6e9 593 /* generate MD based on negotiated type */
tax 0:66300c77c6e9 594 switch (cstate->chal_type) {
tax 0:66300c77c6e9 595
tax 0:66300c77c6e9 596 case CHAP_DIGEST_MD5: /* only MD5 is defined for now */
tax 0:66300c77c6e9 597 if (remmd_len != MD5_SIGNATURE_SIZE) {
tax 0:66300c77c6e9 598 break; /* it's not even the right length */
tax 0:66300c77c6e9 599 }
tax 0:66300c77c6e9 600 MD5Init(&mdContext);
tax 0:66300c77c6e9 601 MD5Update(&mdContext, &cstate->chal_id, 1);
tax 0:66300c77c6e9 602 MD5Update(&mdContext, (u_char*)secret, secret_len);
tax 0:66300c77c6e9 603 MD5Update(&mdContext, cstate->challenge, cstate->chal_len);
tax 0:66300c77c6e9 604 MD5Final(hash, &mdContext);
tax 0:66300c77c6e9 605
tax 0:66300c77c6e9 606 /* compare local and remote MDs and send the appropriate status */
tax 0:66300c77c6e9 607 if (memcmp (hash, remmd, MD5_SIGNATURE_SIZE) == 0) {
tax 0:66300c77c6e9 608 code = CHAP_SUCCESS; /* they are the same! */
tax 0:66300c77c6e9 609 }
tax 0:66300c77c6e9 610 break;
tax 0:66300c77c6e9 611
tax 0:66300c77c6e9 612 default:
tax 0:66300c77c6e9 613 CHAPDEBUG(LOG_INFO, ("unknown digest type %d\n", cstate->chal_type));
tax 0:66300c77c6e9 614 }
tax 0:66300c77c6e9 615 }
tax 0:66300c77c6e9 616
tax 0:66300c77c6e9 617 BZERO(secret, sizeof(secret));
tax 0:66300c77c6e9 618 ChapSendStatus(cstate, code);
tax 0:66300c77c6e9 619
tax 0:66300c77c6e9 620 if (code == CHAP_SUCCESS) {
tax 0:66300c77c6e9 621 old_state = cstate->serverstate;
tax 0:66300c77c6e9 622 cstate->serverstate = CHAPSS_OPEN;
tax 0:66300c77c6e9 623 if (old_state == CHAPSS_INITIAL_CHAL) {
tax 0:66300c77c6e9 624 auth_peer_success(cstate->unit, PPP_CHAP, rhostname, len);
tax 0:66300c77c6e9 625 }
tax 0:66300c77c6e9 626 if (cstate->chal_interval != 0) {
tax 0:66300c77c6e9 627 TIMEOUT(ChapRechallenge, cstate, cstate->chal_interval);
tax 0:66300c77c6e9 628 }
tax 0:66300c77c6e9 629 } else {
tax 0:66300c77c6e9 630 CHAPDEBUG(LOG_ERR, ("CHAP peer authentication failed\n"));
tax 0:66300c77c6e9 631 cstate->serverstate = CHAPSS_BADAUTH;
tax 0:66300c77c6e9 632 auth_peer_fail(cstate->unit, PPP_CHAP);
tax 0:66300c77c6e9 633 }
tax 0:66300c77c6e9 634 }
tax 0:66300c77c6e9 635
tax 0:66300c77c6e9 636 /*
tax 0:66300c77c6e9 637 * ChapReceiveSuccess - Receive Success
tax 0:66300c77c6e9 638 */
tax 0:66300c77c6e9 639 static void
tax 0:66300c77c6e9 640 ChapReceiveSuccess(chap_state *cstate, u_char *inp, u_char id, int len)
tax 0:66300c77c6e9 641 {
tax 0:66300c77c6e9 642 LWIP_UNUSED_ARG(id);
tax 0:66300c77c6e9 643 LWIP_UNUSED_ARG(inp);
tax 0:66300c77c6e9 644
tax 0:66300c77c6e9 645 CHAPDEBUG(LOG_INFO, ("ChapReceiveSuccess: Rcvd id %d.\n", id));
tax 0:66300c77c6e9 646
tax 0:66300c77c6e9 647 if (cstate->clientstate == CHAPCS_OPEN) {
tax 0:66300c77c6e9 648 /* presumably an answer to a duplicate response */
tax 0:66300c77c6e9 649 return;
tax 0:66300c77c6e9 650 }
tax 0:66300c77c6e9 651
tax 0:66300c77c6e9 652 if (cstate->clientstate != CHAPCS_RESPONSE) {
tax 0:66300c77c6e9 653 /* don't know what this is */
tax 0:66300c77c6e9 654 CHAPDEBUG(LOG_INFO, ("ChapReceiveSuccess: in state %d\n",
tax 0:66300c77c6e9 655 cstate->clientstate));
tax 0:66300c77c6e9 656 return;
tax 0:66300c77c6e9 657 }
tax 0:66300c77c6e9 658
tax 0:66300c77c6e9 659 UNTIMEOUT(ChapResponseTimeout, cstate);
tax 0:66300c77c6e9 660
tax 0:66300c77c6e9 661 /*
tax 0:66300c77c6e9 662 * Print message.
tax 0:66300c77c6e9 663 */
tax 0:66300c77c6e9 664 if (len > 0) {
tax 0:66300c77c6e9 665 PRINTMSG(inp, len);
tax 0:66300c77c6e9 666 }
tax 0:66300c77c6e9 667
tax 0:66300c77c6e9 668 cstate->clientstate = CHAPCS_OPEN;
tax 0:66300c77c6e9 669
tax 0:66300c77c6e9 670 auth_withpeer_success(cstate->unit, PPP_CHAP);
tax 0:66300c77c6e9 671 }
tax 0:66300c77c6e9 672
tax 0:66300c77c6e9 673
tax 0:66300c77c6e9 674 /*
tax 0:66300c77c6e9 675 * ChapReceiveFailure - Receive failure.
tax 0:66300c77c6e9 676 */
tax 0:66300c77c6e9 677 static void
tax 0:66300c77c6e9 678 ChapReceiveFailure(chap_state *cstate, u_char *inp, u_char id, int len)
tax 0:66300c77c6e9 679 {
tax 0:66300c77c6e9 680 LWIP_UNUSED_ARG(id);
tax 0:66300c77c6e9 681 LWIP_UNUSED_ARG(inp);
tax 0:66300c77c6e9 682
tax 0:66300c77c6e9 683 CHAPDEBUG(LOG_INFO, ("ChapReceiveFailure: Rcvd id %d.\n", id));
tax 0:66300c77c6e9 684
tax 0:66300c77c6e9 685 if (cstate->clientstate != CHAPCS_RESPONSE) {
tax 0:66300c77c6e9 686 /* don't know what this is */
tax 0:66300c77c6e9 687 CHAPDEBUG(LOG_INFO, ("ChapReceiveFailure: in state %d\n",
tax 0:66300c77c6e9 688 cstate->clientstate));
tax 0:66300c77c6e9 689 return;
tax 0:66300c77c6e9 690 }
tax 0:66300c77c6e9 691
tax 0:66300c77c6e9 692 UNTIMEOUT(ChapResponseTimeout, cstate);
tax 0:66300c77c6e9 693
tax 0:66300c77c6e9 694 /*
tax 0:66300c77c6e9 695 * Print message.
tax 0:66300c77c6e9 696 */
tax 0:66300c77c6e9 697 if (len > 0) {
tax 0:66300c77c6e9 698 PRINTMSG(inp, len);
tax 0:66300c77c6e9 699 }
tax 0:66300c77c6e9 700
tax 0:66300c77c6e9 701 CHAPDEBUG(LOG_ERR, ("CHAP authentication failed\n"));
tax 0:66300c77c6e9 702 auth_withpeer_fail(cstate->unit, PPP_CHAP); /* lwip: just sets the PPP error code on this unit to PPPERR_AUTHFAIL */
tax 0:66300c77c6e9 703 }
tax 0:66300c77c6e9 704
tax 0:66300c77c6e9 705
tax 0:66300c77c6e9 706 /*
tax 0:66300c77c6e9 707 * ChapSendChallenge - Send an Authenticate challenge.
tax 0:66300c77c6e9 708 */
tax 0:66300c77c6e9 709 static void
tax 0:66300c77c6e9 710 ChapSendChallenge(chap_state *cstate)
tax 0:66300c77c6e9 711 {
tax 0:66300c77c6e9 712 u_char *outp;
tax 0:66300c77c6e9 713 int chal_len, name_len;
tax 0:66300c77c6e9 714 int outlen;
tax 0:66300c77c6e9 715
tax 0:66300c77c6e9 716 chal_len = cstate->chal_len;
tax 0:66300c77c6e9 717 name_len = (int)strlen(cstate->chal_name);
tax 0:66300c77c6e9 718 outlen = CHAP_HEADERLEN + sizeof (u_char) + chal_len + name_len;
tax 0:66300c77c6e9 719 outp = outpacket_buf[cstate->unit];
tax 0:66300c77c6e9 720
tax 0:66300c77c6e9 721 MAKEHEADER(outp, PPP_CHAP); /* paste in a CHAP header */
tax 0:66300c77c6e9 722
tax 0:66300c77c6e9 723 PUTCHAR(CHAP_CHALLENGE, outp);
tax 0:66300c77c6e9 724 PUTCHAR(cstate->chal_id, outp);
tax 0:66300c77c6e9 725 PUTSHORT(outlen, outp);
tax 0:66300c77c6e9 726
tax 0:66300c77c6e9 727 PUTCHAR(chal_len, outp); /* put length of challenge */
tax 0:66300c77c6e9 728 BCOPY(cstate->challenge, outp, chal_len);
tax 0:66300c77c6e9 729 INCPTR(chal_len, outp);
tax 0:66300c77c6e9 730
tax 0:66300c77c6e9 731 BCOPY(cstate->chal_name, outp, name_len); /* append hostname */
tax 0:66300c77c6e9 732
tax 0:66300c77c6e9 733 pppWrite(cstate->unit, outpacket_buf[cstate->unit], outlen + PPP_HDRLEN);
tax 0:66300c77c6e9 734
tax 0:66300c77c6e9 735 CHAPDEBUG(LOG_INFO, ("ChapSendChallenge: Sent id %d.\n", cstate->chal_id));
tax 0:66300c77c6e9 736
tax 0:66300c77c6e9 737 TIMEOUT(ChapChallengeTimeout, cstate, cstate->timeouttime);
tax 0:66300c77c6e9 738 ++cstate->chal_transmits;
tax 0:66300c77c6e9 739 }
tax 0:66300c77c6e9 740
tax 0:66300c77c6e9 741
tax 0:66300c77c6e9 742 /*
tax 0:66300c77c6e9 743 * ChapSendStatus - Send a status response (ack or nak).
tax 0:66300c77c6e9 744 */
tax 0:66300c77c6e9 745 static void
tax 0:66300c77c6e9 746 ChapSendStatus(chap_state *cstate, int code)
tax 0:66300c77c6e9 747 {
tax 0:66300c77c6e9 748 u_char *outp;
tax 0:66300c77c6e9 749 int outlen, msglen;
tax 0:66300c77c6e9 750 char msg[256]; /* @todo: this can be a char*, no strcpy needed */
tax 0:66300c77c6e9 751
tax 0:66300c77c6e9 752 if (code == CHAP_SUCCESS) {
tax 0:66300c77c6e9 753 strcpy(msg, "Welcome!");
tax 0:66300c77c6e9 754 } else {
tax 0:66300c77c6e9 755 strcpy(msg, "I don't like you. Go 'way.");
tax 0:66300c77c6e9 756 }
tax 0:66300c77c6e9 757 msglen = (int)strlen(msg);
tax 0:66300c77c6e9 758
tax 0:66300c77c6e9 759 outlen = CHAP_HEADERLEN + msglen;
tax 0:66300c77c6e9 760 outp = outpacket_buf[cstate->unit];
tax 0:66300c77c6e9 761
tax 0:66300c77c6e9 762 MAKEHEADER(outp, PPP_CHAP); /* paste in a header */
tax 0:66300c77c6e9 763
tax 0:66300c77c6e9 764 PUTCHAR(code, outp);
tax 0:66300c77c6e9 765 PUTCHAR(cstate->chal_id, outp);
tax 0:66300c77c6e9 766 PUTSHORT(outlen, outp);
tax 0:66300c77c6e9 767 BCOPY(msg, outp, msglen);
tax 0:66300c77c6e9 768 pppWrite(cstate->unit, outpacket_buf[cstate->unit], outlen + PPP_HDRLEN);
tax 0:66300c77c6e9 769
tax 0:66300c77c6e9 770 CHAPDEBUG(LOG_INFO, ("ChapSendStatus: Sent code %d, id %d.\n", code,
tax 0:66300c77c6e9 771 cstate->chal_id));
tax 0:66300c77c6e9 772 }
tax 0:66300c77c6e9 773
tax 0:66300c77c6e9 774 /*
tax 0:66300c77c6e9 775 * ChapGenChallenge is used to generate a pseudo-random challenge string of
tax 0:66300c77c6e9 776 * a pseudo-random length between min_len and max_len. The challenge
tax 0:66300c77c6e9 777 * string and its length are stored in *cstate, and various other fields of
tax 0:66300c77c6e9 778 * *cstate are initialized.
tax 0:66300c77c6e9 779 */
tax 0:66300c77c6e9 780
tax 0:66300c77c6e9 781 static void
tax 0:66300c77c6e9 782 ChapGenChallenge(chap_state *cstate)
tax 0:66300c77c6e9 783 {
tax 0:66300c77c6e9 784 int chal_len;
tax 0:66300c77c6e9 785 u_char *ptr = cstate->challenge;
tax 0:66300c77c6e9 786 int i;
tax 0:66300c77c6e9 787
tax 0:66300c77c6e9 788 /* pick a random challenge length between MIN_CHALLENGE_LENGTH and
tax 0:66300c77c6e9 789 MAX_CHALLENGE_LENGTH */
tax 0:66300c77c6e9 790 chal_len = (unsigned)
tax 0:66300c77c6e9 791 ((((magic() >> 16) *
tax 0:66300c77c6e9 792 (MAX_CHALLENGE_LENGTH - MIN_CHALLENGE_LENGTH)) >> 16)
tax 0:66300c77c6e9 793 + MIN_CHALLENGE_LENGTH);
tax 0:66300c77c6e9 794 LWIP_ASSERT("chal_len <= 0xff", chal_len <= 0xffff);
tax 0:66300c77c6e9 795 cstate->chal_len = (u_char)chal_len;
tax 0:66300c77c6e9 796 cstate->chal_id = ++cstate->id;
tax 0:66300c77c6e9 797 cstate->chal_transmits = 0;
tax 0:66300c77c6e9 798
tax 0:66300c77c6e9 799 /* generate a random string */
tax 0:66300c77c6e9 800 for (i = 0; i < chal_len; i++ ) {
tax 0:66300c77c6e9 801 *ptr++ = (char) (magic() & 0xff);
tax 0:66300c77c6e9 802 }
tax 0:66300c77c6e9 803 }
tax 0:66300c77c6e9 804
tax 0:66300c77c6e9 805 /*
tax 0:66300c77c6e9 806 * ChapSendResponse - send a response packet with values as specified
tax 0:66300c77c6e9 807 * in *cstate.
tax 0:66300c77c6e9 808 */
tax 0:66300c77c6e9 809 /* ARGSUSED */
tax 0:66300c77c6e9 810 static void
tax 0:66300c77c6e9 811 ChapSendResponse(chap_state *cstate)
tax 0:66300c77c6e9 812 {
tax 0:66300c77c6e9 813 u_char *outp;
tax 0:66300c77c6e9 814 int outlen, md_len, name_len;
tax 0:66300c77c6e9 815
tax 0:66300c77c6e9 816 md_len = cstate->resp_length;
tax 0:66300c77c6e9 817 name_len = (int)strlen(cstate->resp_name);
tax 0:66300c77c6e9 818 outlen = CHAP_HEADERLEN + sizeof (u_char) + md_len + name_len;
tax 0:66300c77c6e9 819 outp = outpacket_buf[cstate->unit];
tax 0:66300c77c6e9 820
tax 0:66300c77c6e9 821 MAKEHEADER(outp, PPP_CHAP);
tax 0:66300c77c6e9 822
tax 0:66300c77c6e9 823 PUTCHAR(CHAP_RESPONSE, outp); /* we are a response */
tax 0:66300c77c6e9 824 PUTCHAR(cstate->resp_id, outp); /* copy id from challenge packet */
tax 0:66300c77c6e9 825 PUTSHORT(outlen, outp); /* packet length */
tax 0:66300c77c6e9 826
tax 0:66300c77c6e9 827 PUTCHAR(md_len, outp); /* length of MD */
tax 0:66300c77c6e9 828 BCOPY(cstate->response, outp, md_len); /* copy MD to buffer */
tax 0:66300c77c6e9 829 INCPTR(md_len, outp);
tax 0:66300c77c6e9 830
tax 0:66300c77c6e9 831 BCOPY(cstate->resp_name, outp, name_len); /* append our name */
tax 0:66300c77c6e9 832
tax 0:66300c77c6e9 833 /* send the packet */
tax 0:66300c77c6e9 834 pppWrite(cstate->unit, outpacket_buf[cstate->unit], outlen + PPP_HDRLEN);
tax 0:66300c77c6e9 835
tax 0:66300c77c6e9 836 cstate->clientstate = CHAPCS_RESPONSE;
tax 0:66300c77c6e9 837 TIMEOUT(ChapResponseTimeout, cstate, cstate->timeouttime);
tax 0:66300c77c6e9 838 ++cstate->resp_transmits;
tax 0:66300c77c6e9 839 }
tax 0:66300c77c6e9 840
tax 0:66300c77c6e9 841 #if PPP_ADDITIONAL_CALLBACKS
tax 0:66300c77c6e9 842 static char *ChapCodenames[] = {
tax 0:66300c77c6e9 843 "Challenge", "Response", "Success", "Failure"
tax 0:66300c77c6e9 844 };
tax 0:66300c77c6e9 845 /*
tax 0:66300c77c6e9 846 * ChapPrintPkt - print the contents of a CHAP packet.
tax 0:66300c77c6e9 847 */
tax 0:66300c77c6e9 848 static int
tax 0:66300c77c6e9 849 ChapPrintPkt( u_char *p, int plen, void (*printer) (void *, char *, ...), void *arg)
tax 0:66300c77c6e9 850 {
tax 0:66300c77c6e9 851 int code, id, len;
tax 0:66300c77c6e9 852 int clen, nlen;
tax 0:66300c77c6e9 853 u_char x;
tax 0:66300c77c6e9 854
tax 0:66300c77c6e9 855 if (plen < CHAP_HEADERLEN) {
tax 0:66300c77c6e9 856 return 0;
tax 0:66300c77c6e9 857 }
tax 0:66300c77c6e9 858 GETCHAR(code, p);
tax 0:66300c77c6e9 859 GETCHAR(id, p);
tax 0:66300c77c6e9 860 GETSHORT(len, p);
tax 0:66300c77c6e9 861 if (len < CHAP_HEADERLEN || len > plen) {
tax 0:66300c77c6e9 862 return 0;
tax 0:66300c77c6e9 863 }
tax 0:66300c77c6e9 864
tax 0:66300c77c6e9 865 if (code >= 1 && code <= sizeof(ChapCodenames) / sizeof(char *)) {
tax 0:66300c77c6e9 866 printer(arg, " %s", ChapCodenames[code-1]);
tax 0:66300c77c6e9 867 } else {
tax 0:66300c77c6e9 868 printer(arg, " code=0x%x", code);
tax 0:66300c77c6e9 869 }
tax 0:66300c77c6e9 870 printer(arg, " id=0x%x", id);
tax 0:66300c77c6e9 871 len -= CHAP_HEADERLEN;
tax 0:66300c77c6e9 872 switch (code) {
tax 0:66300c77c6e9 873 case CHAP_CHALLENGE:
tax 0:66300c77c6e9 874 case CHAP_RESPONSE:
tax 0:66300c77c6e9 875 if (len < 1) {
tax 0:66300c77c6e9 876 break;
tax 0:66300c77c6e9 877 }
tax 0:66300c77c6e9 878 clen = p[0];
tax 0:66300c77c6e9 879 if (len < clen + 1) {
tax 0:66300c77c6e9 880 break;
tax 0:66300c77c6e9 881 }
tax 0:66300c77c6e9 882 ++p;
tax 0:66300c77c6e9 883 nlen = len - clen - 1;
tax 0:66300c77c6e9 884 printer(arg, " <");
tax 0:66300c77c6e9 885 for (; clen > 0; --clen) {
tax 0:66300c77c6e9 886 GETCHAR(x, p);
tax 0:66300c77c6e9 887 printer(arg, "%.2x", x);
tax 0:66300c77c6e9 888 }
tax 0:66300c77c6e9 889 printer(arg, ">, name = %.*Z", nlen, p);
tax 0:66300c77c6e9 890 break;
tax 0:66300c77c6e9 891 case CHAP_FAILURE:
tax 0:66300c77c6e9 892 case CHAP_SUCCESS:
tax 0:66300c77c6e9 893 printer(arg, " %.*Z", len, p);
tax 0:66300c77c6e9 894 break;
tax 0:66300c77c6e9 895 default:
tax 0:66300c77c6e9 896 for (clen = len; clen > 0; --clen) {
tax 0:66300c77c6e9 897 GETCHAR(x, p);
tax 0:66300c77c6e9 898 printer(arg, " %.2x", x);
tax 0:66300c77c6e9 899 }
tax 0:66300c77c6e9 900 }
tax 0:66300c77c6e9 901
tax 0:66300c77c6e9 902 return len + CHAP_HEADERLEN;
tax 0:66300c77c6e9 903 }
tax 0:66300c77c6e9 904 #endif /* PPP_ADDITIONAL_CALLBACKS */
tax 0:66300c77c6e9 905
tax 0:66300c77c6e9 906 #endif /* CHAP_SUPPORT */
tax 0:66300c77c6e9 907
tax 0:66300c77c6e9 908 #endif /* PPP_SUPPORT */