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

Dependents:   UDPSocketExample 24LCxx_I2CApp WeatherPlatform_pachube HvZServerLib ... more

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

Who changed what in which revision?

UserRevisionLine numberNew contents of line
simon 0:350011bf8be7 1 /*****************************************************************************
simon 0:350011bf8be7 2 * pap.c - Network Password Authentication Protocol program file.
simon 0:350011bf8be7 3 *
simon 0:350011bf8be7 4 * Copyright (c) 2003 by Marc Boucher, Services Informatiques (MBSI) inc.
simon 0:350011bf8be7 5 * portions Copyright (c) 1997 by Global Election Systems Inc.
simon 0:350011bf8be7 6 *
simon 0:350011bf8be7 7 * The authors hereby grant permission to use, copy, modify, distribute,
simon 0:350011bf8be7 8 * and license this software and its documentation for any purpose, provided
simon 0:350011bf8be7 9 * that existing copyright notices are retained in all copies and that this
simon 0:350011bf8be7 10 * notice and the following disclaimer are included verbatim in any
simon 0:350011bf8be7 11 * distributions. No written agreement, license, or royalty fee is required
simon 0:350011bf8be7 12 * for any of the authorized uses.
simon 0:350011bf8be7 13 *
simon 0:350011bf8be7 14 * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS *AS IS* AND ANY EXPRESS OR
simon 0:350011bf8be7 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
simon 0:350011bf8be7 16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
simon 0:350011bf8be7 17 * IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
simon 0:350011bf8be7 18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
simon 0:350011bf8be7 19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
simon 0:350011bf8be7 20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
simon 0:350011bf8be7 21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
simon 0:350011bf8be7 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
simon 0:350011bf8be7 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
simon 0:350011bf8be7 24 *
simon 0:350011bf8be7 25 ******************************************************************************
simon 0:350011bf8be7 26 * REVISION HISTORY
simon 0:350011bf8be7 27 *
simon 0:350011bf8be7 28 * 03-01-01 Marc Boucher <marc@mbsi.ca>
simon 0:350011bf8be7 29 * Ported to lwIP.
simon 0:350011bf8be7 30 * 97-12-12 Guy Lancaster <lancasterg@acm.org>, Global Election Systems Inc.
simon 0:350011bf8be7 31 * Original.
simon 0:350011bf8be7 32 *****************************************************************************/
simon 0:350011bf8be7 33 /*
simon 0:350011bf8be7 34 * upap.c - User/Password Authentication Protocol.
simon 0:350011bf8be7 35 *
simon 0:350011bf8be7 36 * Copyright (c) 1989 Carnegie Mellon University.
simon 0:350011bf8be7 37 * All rights reserved.
simon 0:350011bf8be7 38 *
simon 0:350011bf8be7 39 * Redistribution and use in source and binary forms are permitted
simon 0:350011bf8be7 40 * provided that the above copyright notice and this paragraph are
simon 0:350011bf8be7 41 * duplicated in all such forms and that any documentation,
simon 0:350011bf8be7 42 * advertising materials, and other materials related to such
simon 0:350011bf8be7 43 * distribution and use acknowledge that the software was developed
simon 0:350011bf8be7 44 * by Carnegie Mellon University. The name of the
simon 0:350011bf8be7 45 * University may not be used to endorse or promote products derived
simon 0:350011bf8be7 46 * from this software without specific prior written permission.
simon 0:350011bf8be7 47 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
simon 0:350011bf8be7 48 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
simon 0:350011bf8be7 49 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
simon 0:350011bf8be7 50 */
simon 0:350011bf8be7 51
simon 0:350011bf8be7 52 #include "lwip/opt.h"
simon 0:350011bf8be7 53
simon 0:350011bf8be7 54 #if PPP_SUPPORT /* don't build if not configured for use in lwipopts.h */
simon 0:350011bf8be7 55
simon 0:350011bf8be7 56 #if PAP_SUPPORT /* don't build if not configured for use in lwipopts.h */
simon 0:350011bf8be7 57
simon 0:350011bf8be7 58 #include "ppp.h"
simon 0:350011bf8be7 59 #include "pppdebug.h"
simon 0:350011bf8be7 60
simon 0:350011bf8be7 61 #include "auth.h"
simon 0:350011bf8be7 62 #include "pap.h"
simon 0:350011bf8be7 63
simon 0:350011bf8be7 64 #include <string.h>
simon 0:350011bf8be7 65
simon 0:350011bf8be7 66 #if 0 /* UNUSED */
simon 0:350011bf8be7 67 static bool hide_password = 1;
simon 0:350011bf8be7 68
simon 0:350011bf8be7 69 /*
simon 0:350011bf8be7 70 * Command-line options.
simon 0:350011bf8be7 71 */
simon 0:350011bf8be7 72 static option_t pap_option_list[] = {
simon 0:350011bf8be7 73 { "hide-password", o_bool, &hide_password,
simon 0:350011bf8be7 74 "Don't output passwords to log", 1 },
simon 0:350011bf8be7 75 { "show-password", o_bool, &hide_password,
simon 0:350011bf8be7 76 "Show password string in debug log messages", 0 },
simon 0:350011bf8be7 77 { "pap-restart", o_int, &upap[0].us_timeouttime,
simon 0:350011bf8be7 78 "Set retransmit timeout for PAP" },
simon 0:350011bf8be7 79 { "pap-max-authreq", o_int, &upap[0].us_maxtransmits,
simon 0:350011bf8be7 80 "Set max number of transmissions for auth-reqs" },
simon 0:350011bf8be7 81 { "pap-timeout", o_int, &upap[0].us_reqtimeout,
simon 0:350011bf8be7 82 "Set time limit for peer PAP authentication" },
simon 0:350011bf8be7 83 { NULL }
simon 0:350011bf8be7 84 };
simon 0:350011bf8be7 85 #endif
simon 0:350011bf8be7 86
simon 0:350011bf8be7 87 /*
simon 0:350011bf8be7 88 * Protocol entry points.
simon 0:350011bf8be7 89 */
simon 0:350011bf8be7 90 static void upap_init (int);
simon 0:350011bf8be7 91 static void upap_lowerup (int);
simon 0:350011bf8be7 92 static void upap_lowerdown (int);
simon 0:350011bf8be7 93 static void upap_input (int, u_char *, int);
simon 0:350011bf8be7 94 static void upap_protrej (int);
simon 0:350011bf8be7 95 #if PPP_ADDITIONAL_CALLBACKS
simon 0:350011bf8be7 96 static int upap_printpkt (u_char *, int, void (*)(void *, char *, ...), void *);
simon 0:350011bf8be7 97 #endif /* PPP_ADDITIONAL_CALLBACKS */
simon 0:350011bf8be7 98
simon 0:350011bf8be7 99 struct protent pap_protent = {
simon 0:350011bf8be7 100 PPP_PAP,
simon 0:350011bf8be7 101 upap_init,
simon 0:350011bf8be7 102 upap_input,
simon 0:350011bf8be7 103 upap_protrej,
simon 0:350011bf8be7 104 upap_lowerup,
simon 0:350011bf8be7 105 upap_lowerdown,
simon 0:350011bf8be7 106 NULL,
simon 0:350011bf8be7 107 NULL,
simon 0:350011bf8be7 108 #if PPP_ADDITIONAL_CALLBACKS
simon 0:350011bf8be7 109 upap_printpkt,
simon 0:350011bf8be7 110 NULL,
simon 0:350011bf8be7 111 #endif /* PPP_ADDITIONAL_CALLBACKS */
simon 0:350011bf8be7 112 1,
simon 0:350011bf8be7 113 "PAP",
simon 0:350011bf8be7 114 #if PPP_ADDITIONAL_CALLBACKS
simon 0:350011bf8be7 115 NULL,
simon 0:350011bf8be7 116 NULL,
simon 0:350011bf8be7 117 NULL
simon 0:350011bf8be7 118 #endif /* PPP_ADDITIONAL_CALLBACKS */
simon 0:350011bf8be7 119 };
simon 0:350011bf8be7 120
simon 0:350011bf8be7 121 upap_state upap[NUM_PPP]; /* UPAP state; one for each unit */
simon 0:350011bf8be7 122
simon 0:350011bf8be7 123 static void upap_timeout (void *);
simon 0:350011bf8be7 124 static void upap_reqtimeout(void *);
simon 0:350011bf8be7 125 static void upap_rauthreq (upap_state *, u_char *, u_char, int);
simon 0:350011bf8be7 126 static void upap_rauthack (upap_state *, u_char *, int, int);
simon 0:350011bf8be7 127 static void upap_rauthnak (upap_state *, u_char *, int, int);
simon 0:350011bf8be7 128 static void upap_sauthreq (upap_state *);
simon 0:350011bf8be7 129 static void upap_sresp (upap_state *, u_char, u_char, char *, int);
simon 0:350011bf8be7 130
simon 0:350011bf8be7 131
simon 0:350011bf8be7 132 /*
simon 0:350011bf8be7 133 * upap_init - Initialize a UPAP unit.
simon 0:350011bf8be7 134 */
simon 0:350011bf8be7 135 static void
simon 0:350011bf8be7 136 upap_init(int unit)
simon 0:350011bf8be7 137 {
simon 0:350011bf8be7 138 upap_state *u = &upap[unit];
simon 0:350011bf8be7 139
simon 0:350011bf8be7 140 UPAPDEBUG(LOG_INFO, ("upap_init: %d\n", unit));
simon 0:350011bf8be7 141 u->us_unit = unit;
simon 0:350011bf8be7 142 u->us_user = NULL;
simon 0:350011bf8be7 143 u->us_userlen = 0;
simon 0:350011bf8be7 144 u->us_passwd = NULL;
simon 0:350011bf8be7 145 u->us_passwdlen = 0;
simon 0:350011bf8be7 146 u->us_clientstate = UPAPCS_INITIAL;
simon 0:350011bf8be7 147 u->us_serverstate = UPAPSS_INITIAL;
simon 0:350011bf8be7 148 u->us_id = 0;
simon 0:350011bf8be7 149 u->us_timeouttime = UPAP_DEFTIMEOUT;
simon 0:350011bf8be7 150 u->us_maxtransmits = 10;
simon 0:350011bf8be7 151 u->us_reqtimeout = UPAP_DEFREQTIME;
simon 0:350011bf8be7 152 }
simon 0:350011bf8be7 153
simon 0:350011bf8be7 154 /*
simon 0:350011bf8be7 155 * upap_authwithpeer - Authenticate us with our peer (start client).
simon 0:350011bf8be7 156 *
simon 0:350011bf8be7 157 * Set new state and send authenticate's.
simon 0:350011bf8be7 158 */
simon 0:350011bf8be7 159 void
simon 0:350011bf8be7 160 upap_authwithpeer(int unit, char *user, char *password)
simon 0:350011bf8be7 161 {
simon 0:350011bf8be7 162 upap_state *u = &upap[unit];
simon 0:350011bf8be7 163
simon 0:350011bf8be7 164 UPAPDEBUG(LOG_INFO, ("upap_authwithpeer: %d user=%s password=%s s=%d\n",
simon 0:350011bf8be7 165 unit, user, password, u->us_clientstate));
simon 0:350011bf8be7 166
simon 0:350011bf8be7 167 /* Save the username and password we're given */
simon 0:350011bf8be7 168 u->us_user = user;
simon 0:350011bf8be7 169 u->us_userlen = (int)strlen(user);
simon 0:350011bf8be7 170 u->us_passwd = password;
simon 0:350011bf8be7 171 u->us_passwdlen = (int)strlen(password);
simon 0:350011bf8be7 172
simon 0:350011bf8be7 173 u->us_transmits = 0;
simon 0:350011bf8be7 174
simon 0:350011bf8be7 175 /* Lower layer up yet? */
simon 0:350011bf8be7 176 if (u->us_clientstate == UPAPCS_INITIAL ||
simon 0:350011bf8be7 177 u->us_clientstate == UPAPCS_PENDING) {
simon 0:350011bf8be7 178 u->us_clientstate = UPAPCS_PENDING;
simon 0:350011bf8be7 179 return;
simon 0:350011bf8be7 180 }
simon 0:350011bf8be7 181
simon 0:350011bf8be7 182 upap_sauthreq(u); /* Start protocol */
simon 0:350011bf8be7 183 }
simon 0:350011bf8be7 184
simon 0:350011bf8be7 185
simon 0:350011bf8be7 186 /*
simon 0:350011bf8be7 187 * upap_authpeer - Authenticate our peer (start server).
simon 0:350011bf8be7 188 *
simon 0:350011bf8be7 189 * Set new state.
simon 0:350011bf8be7 190 */
simon 0:350011bf8be7 191 void
simon 0:350011bf8be7 192 upap_authpeer(int unit)
simon 0:350011bf8be7 193 {
simon 0:350011bf8be7 194 upap_state *u = &upap[unit];
simon 0:350011bf8be7 195
simon 0:350011bf8be7 196 /* Lower layer up yet? */
simon 0:350011bf8be7 197 if (u->us_serverstate == UPAPSS_INITIAL ||
simon 0:350011bf8be7 198 u->us_serverstate == UPAPSS_PENDING) {
simon 0:350011bf8be7 199 u->us_serverstate = UPAPSS_PENDING;
simon 0:350011bf8be7 200 return;
simon 0:350011bf8be7 201 }
simon 0:350011bf8be7 202
simon 0:350011bf8be7 203 u->us_serverstate = UPAPSS_LISTEN;
simon 0:350011bf8be7 204 if (u->us_reqtimeout > 0) {
simon 0:350011bf8be7 205 TIMEOUT(upap_reqtimeout, u, u->us_reqtimeout);
simon 0:350011bf8be7 206 }
simon 0:350011bf8be7 207 }
simon 0:350011bf8be7 208
simon 0:350011bf8be7 209 /*
simon 0:350011bf8be7 210 * upap_timeout - Retransmission timer for sending auth-reqs expired.
simon 0:350011bf8be7 211 */
simon 0:350011bf8be7 212 static void
simon 0:350011bf8be7 213 upap_timeout(void *arg)
simon 0:350011bf8be7 214 {
simon 0:350011bf8be7 215 upap_state *u = (upap_state *) arg;
simon 0:350011bf8be7 216
simon 0:350011bf8be7 217 UPAPDEBUG(LOG_INFO, ("upap_timeout: %d timeout %d expired s=%d\n",
simon 0:350011bf8be7 218 u->us_unit, u->us_timeouttime, u->us_clientstate));
simon 0:350011bf8be7 219
simon 0:350011bf8be7 220 if (u->us_clientstate != UPAPCS_AUTHREQ) {
simon 0:350011bf8be7 221 UPAPDEBUG(LOG_INFO, ("upap_timeout: not in AUTHREQ state!\n"));
simon 0:350011bf8be7 222 return;
simon 0:350011bf8be7 223 }
simon 0:350011bf8be7 224
simon 0:350011bf8be7 225 if (u->us_transmits >= u->us_maxtransmits) {
simon 0:350011bf8be7 226 /* give up in disgust */
simon 0:350011bf8be7 227 UPAPDEBUG(LOG_ERR, ("No response to PAP authenticate-requests\n"));
simon 0:350011bf8be7 228 u->us_clientstate = UPAPCS_BADAUTH;
simon 0:350011bf8be7 229 auth_withpeer_fail(u->us_unit, PPP_PAP);
simon 0:350011bf8be7 230 return;
simon 0:350011bf8be7 231 }
simon 0:350011bf8be7 232
simon 0:350011bf8be7 233 upap_sauthreq(u); /* Send Authenticate-Request and set upap timeout*/
simon 0:350011bf8be7 234 }
simon 0:350011bf8be7 235
simon 0:350011bf8be7 236
simon 0:350011bf8be7 237 /*
simon 0:350011bf8be7 238 * upap_reqtimeout - Give up waiting for the peer to send an auth-req.
simon 0:350011bf8be7 239 */
simon 0:350011bf8be7 240 static void
simon 0:350011bf8be7 241 upap_reqtimeout(void *arg)
simon 0:350011bf8be7 242 {
simon 0:350011bf8be7 243 upap_state *u = (upap_state *) arg;
simon 0:350011bf8be7 244
simon 0:350011bf8be7 245 if (u->us_serverstate != UPAPSS_LISTEN) {
simon 0:350011bf8be7 246 return; /* huh?? */
simon 0:350011bf8be7 247 }
simon 0:350011bf8be7 248
simon 0:350011bf8be7 249 auth_peer_fail(u->us_unit, PPP_PAP);
simon 0:350011bf8be7 250 u->us_serverstate = UPAPSS_BADAUTH;
simon 0:350011bf8be7 251 }
simon 0:350011bf8be7 252
simon 0:350011bf8be7 253
simon 0:350011bf8be7 254 /*
simon 0:350011bf8be7 255 * upap_lowerup - The lower layer is up.
simon 0:350011bf8be7 256 *
simon 0:350011bf8be7 257 * Start authenticating if pending.
simon 0:350011bf8be7 258 */
simon 0:350011bf8be7 259 static void
simon 0:350011bf8be7 260 upap_lowerup(int unit)
simon 0:350011bf8be7 261 {
simon 0:350011bf8be7 262 upap_state *u = &upap[unit];
simon 0:350011bf8be7 263
simon 0:350011bf8be7 264 UPAPDEBUG(LOG_INFO, ("upap_lowerup: init %d clientstate s=%d\n", unit, u->us_clientstate));
simon 0:350011bf8be7 265
simon 0:350011bf8be7 266 if (u->us_clientstate == UPAPCS_INITIAL) {
simon 0:350011bf8be7 267 u->us_clientstate = UPAPCS_CLOSED;
simon 0:350011bf8be7 268 } else if (u->us_clientstate == UPAPCS_PENDING) {
simon 0:350011bf8be7 269 upap_sauthreq(u); /* send an auth-request */
simon 0:350011bf8be7 270 /* now client state is UPAPCS__AUTHREQ */
simon 0:350011bf8be7 271 }
simon 0:350011bf8be7 272
simon 0:350011bf8be7 273 if (u->us_serverstate == UPAPSS_INITIAL) {
simon 0:350011bf8be7 274 u->us_serverstate = UPAPSS_CLOSED;
simon 0:350011bf8be7 275 } else if (u->us_serverstate == UPAPSS_PENDING) {
simon 0:350011bf8be7 276 u->us_serverstate = UPAPSS_LISTEN;
simon 0:350011bf8be7 277 if (u->us_reqtimeout > 0) {
simon 0:350011bf8be7 278 TIMEOUT(upap_reqtimeout, u, u->us_reqtimeout);
simon 0:350011bf8be7 279 }
simon 0:350011bf8be7 280 }
simon 0:350011bf8be7 281 }
simon 0:350011bf8be7 282
simon 0:350011bf8be7 283
simon 0:350011bf8be7 284 /*
simon 0:350011bf8be7 285 * upap_lowerdown - The lower layer is down.
simon 0:350011bf8be7 286 *
simon 0:350011bf8be7 287 * Cancel all timeouts.
simon 0:350011bf8be7 288 */
simon 0:350011bf8be7 289 static void
simon 0:350011bf8be7 290 upap_lowerdown(int unit)
simon 0:350011bf8be7 291 {
simon 0:350011bf8be7 292 upap_state *u = &upap[unit];
simon 0:350011bf8be7 293
simon 0:350011bf8be7 294 UPAPDEBUG(LOG_INFO, ("upap_lowerdown: %d s=%d\n", unit, u->us_clientstate));
simon 0:350011bf8be7 295
simon 0:350011bf8be7 296 if (u->us_clientstate == UPAPCS_AUTHREQ) { /* Timeout pending? */
simon 0:350011bf8be7 297 UNTIMEOUT(upap_timeout, u); /* Cancel timeout */
simon 0:350011bf8be7 298 }
simon 0:350011bf8be7 299 if (u->us_serverstate == UPAPSS_LISTEN && u->us_reqtimeout > 0) {
simon 0:350011bf8be7 300 UNTIMEOUT(upap_reqtimeout, u);
simon 0:350011bf8be7 301 }
simon 0:350011bf8be7 302
simon 0:350011bf8be7 303 u->us_clientstate = UPAPCS_INITIAL;
simon 0:350011bf8be7 304 u->us_serverstate = UPAPSS_INITIAL;
simon 0:350011bf8be7 305 }
simon 0:350011bf8be7 306
simon 0:350011bf8be7 307
simon 0:350011bf8be7 308 /*
simon 0:350011bf8be7 309 * upap_protrej - Peer doesn't speak this protocol.
simon 0:350011bf8be7 310 *
simon 0:350011bf8be7 311 * This shouldn't happen. In any case, pretend lower layer went down.
simon 0:350011bf8be7 312 */
simon 0:350011bf8be7 313 static void
simon 0:350011bf8be7 314 upap_protrej(int unit)
simon 0:350011bf8be7 315 {
simon 0:350011bf8be7 316 upap_state *u = &upap[unit];
simon 0:350011bf8be7 317
simon 0:350011bf8be7 318 if (u->us_clientstate == UPAPCS_AUTHREQ) {
simon 0:350011bf8be7 319 UPAPDEBUG(LOG_ERR, ("PAP authentication failed due to protocol-reject\n"));
simon 0:350011bf8be7 320 auth_withpeer_fail(unit, PPP_PAP);
simon 0:350011bf8be7 321 }
simon 0:350011bf8be7 322 if (u->us_serverstate == UPAPSS_LISTEN) {
simon 0:350011bf8be7 323 UPAPDEBUG(LOG_ERR, ("PAP authentication of peer failed (protocol-reject)\n"));
simon 0:350011bf8be7 324 auth_peer_fail(unit, PPP_PAP);
simon 0:350011bf8be7 325 }
simon 0:350011bf8be7 326 upap_lowerdown(unit);
simon 0:350011bf8be7 327 }
simon 0:350011bf8be7 328
simon 0:350011bf8be7 329
simon 0:350011bf8be7 330 /*
simon 0:350011bf8be7 331 * upap_input - Input UPAP packet.
simon 0:350011bf8be7 332 */
simon 0:350011bf8be7 333 static void
simon 0:350011bf8be7 334 upap_input(int unit, u_char *inpacket, int l)
simon 0:350011bf8be7 335 {
simon 0:350011bf8be7 336 upap_state *u = &upap[unit];
simon 0:350011bf8be7 337 u_char *inp;
simon 0:350011bf8be7 338 u_char code, id;
simon 0:350011bf8be7 339 int len;
simon 0:350011bf8be7 340
simon 0:350011bf8be7 341 /*
simon 0:350011bf8be7 342 * Parse header (code, id and length).
simon 0:350011bf8be7 343 * If packet too short, drop it.
simon 0:350011bf8be7 344 */
simon 0:350011bf8be7 345 inp = inpacket;
simon 0:350011bf8be7 346 if (l < (int)UPAP_HEADERLEN) {
simon 0:350011bf8be7 347 UPAPDEBUG(LOG_INFO, ("pap_input: rcvd short header.\n"));
simon 0:350011bf8be7 348 return;
simon 0:350011bf8be7 349 }
simon 0:350011bf8be7 350 GETCHAR(code, inp);
simon 0:350011bf8be7 351 GETCHAR(id, inp);
simon 0:350011bf8be7 352 GETSHORT(len, inp);
simon 0:350011bf8be7 353 if (len < (int)UPAP_HEADERLEN) {
simon 0:350011bf8be7 354 UPAPDEBUG(LOG_INFO, ("pap_input: rcvd illegal length.\n"));
simon 0:350011bf8be7 355 return;
simon 0:350011bf8be7 356 }
simon 0:350011bf8be7 357 if (len > l) {
simon 0:350011bf8be7 358 UPAPDEBUG(LOG_INFO, ("pap_input: rcvd short packet.\n"));
simon 0:350011bf8be7 359 return;
simon 0:350011bf8be7 360 }
simon 0:350011bf8be7 361 len -= UPAP_HEADERLEN;
simon 0:350011bf8be7 362
simon 0:350011bf8be7 363 /*
simon 0:350011bf8be7 364 * Action depends on code.
simon 0:350011bf8be7 365 */
simon 0:350011bf8be7 366 switch (code) {
simon 0:350011bf8be7 367 case UPAP_AUTHREQ:
simon 0:350011bf8be7 368 upap_rauthreq(u, inp, id, len);
simon 0:350011bf8be7 369 break;
simon 0:350011bf8be7 370
simon 0:350011bf8be7 371 case UPAP_AUTHACK:
simon 0:350011bf8be7 372 upap_rauthack(u, inp, id, len);
simon 0:350011bf8be7 373 break;
simon 0:350011bf8be7 374
simon 0:350011bf8be7 375 case UPAP_AUTHNAK:
simon 0:350011bf8be7 376 upap_rauthnak(u, inp, id, len);
simon 0:350011bf8be7 377 break;
simon 0:350011bf8be7 378
simon 0:350011bf8be7 379 default: /* XXX Need code reject */
simon 0:350011bf8be7 380 UPAPDEBUG(LOG_INFO, ("pap_input: UNHANDLED default: code: %d, id: %d, len: %d.\n", code, id, len));
simon 0:350011bf8be7 381 break;
simon 0:350011bf8be7 382 }
simon 0:350011bf8be7 383 }
simon 0:350011bf8be7 384
simon 0:350011bf8be7 385
simon 0:350011bf8be7 386 /*
simon 0:350011bf8be7 387 * upap_rauth - Receive Authenticate.
simon 0:350011bf8be7 388 */
simon 0:350011bf8be7 389 static void
simon 0:350011bf8be7 390 upap_rauthreq(upap_state *u, u_char *inp, u_char id, int len)
simon 0:350011bf8be7 391 {
simon 0:350011bf8be7 392 u_char ruserlen, rpasswdlen;
simon 0:350011bf8be7 393 char *ruser, *rpasswd;
simon 0:350011bf8be7 394 u_char retcode;
simon 0:350011bf8be7 395 char *msg;
simon 0:350011bf8be7 396 int msglen;
simon 0:350011bf8be7 397
simon 0:350011bf8be7 398 UPAPDEBUG(LOG_INFO, ("pap_rauth: Rcvd id %d.\n", id));
simon 0:350011bf8be7 399
simon 0:350011bf8be7 400 if (u->us_serverstate < UPAPSS_LISTEN) {
simon 0:350011bf8be7 401 return;
simon 0:350011bf8be7 402 }
simon 0:350011bf8be7 403
simon 0:350011bf8be7 404 /*
simon 0:350011bf8be7 405 * If we receive a duplicate authenticate-request, we are
simon 0:350011bf8be7 406 * supposed to return the same status as for the first request.
simon 0:350011bf8be7 407 */
simon 0:350011bf8be7 408 if (u->us_serverstate == UPAPSS_OPEN) {
simon 0:350011bf8be7 409 upap_sresp(u, UPAP_AUTHACK, id, "", 0); /* return auth-ack */
simon 0:350011bf8be7 410 return;
simon 0:350011bf8be7 411 }
simon 0:350011bf8be7 412 if (u->us_serverstate == UPAPSS_BADAUTH) {
simon 0:350011bf8be7 413 upap_sresp(u, UPAP_AUTHNAK, id, "", 0); /* return auth-nak */
simon 0:350011bf8be7 414 return;
simon 0:350011bf8be7 415 }
simon 0:350011bf8be7 416
simon 0:350011bf8be7 417 /*
simon 0:350011bf8be7 418 * Parse user/passwd.
simon 0:350011bf8be7 419 */
simon 0:350011bf8be7 420 if (len < (int)sizeof (u_char)) {
simon 0:350011bf8be7 421 UPAPDEBUG(LOG_INFO, ("pap_rauth: rcvd short packet.\n"));
simon 0:350011bf8be7 422 return;
simon 0:350011bf8be7 423 }
simon 0:350011bf8be7 424 GETCHAR(ruserlen, inp);
simon 0:350011bf8be7 425 len -= sizeof (u_char) + ruserlen + sizeof (u_char);
simon 0:350011bf8be7 426 if (len < 0) {
simon 0:350011bf8be7 427 UPAPDEBUG(LOG_INFO, ("pap_rauth: rcvd short packet.\n"));
simon 0:350011bf8be7 428 return;
simon 0:350011bf8be7 429 }
simon 0:350011bf8be7 430 ruser = (char *) inp;
simon 0:350011bf8be7 431 INCPTR(ruserlen, inp);
simon 0:350011bf8be7 432 GETCHAR(rpasswdlen, inp);
simon 0:350011bf8be7 433 if (len < rpasswdlen) {
simon 0:350011bf8be7 434 UPAPDEBUG(LOG_INFO, ("pap_rauth: rcvd short packet.\n"));
simon 0:350011bf8be7 435 return;
simon 0:350011bf8be7 436 }
simon 0:350011bf8be7 437 rpasswd = (char *) inp;
simon 0:350011bf8be7 438
simon 0:350011bf8be7 439 /*
simon 0:350011bf8be7 440 * Check the username and password given.
simon 0:350011bf8be7 441 */
simon 0:350011bf8be7 442 retcode = check_passwd(u->us_unit, ruser, ruserlen, rpasswd, rpasswdlen, &msg, &msglen);
simon 0:350011bf8be7 443 /* lwip: currently retcode is always UPAP_AUTHACK */
simon 0:350011bf8be7 444 BZERO(rpasswd, rpasswdlen);
simon 0:350011bf8be7 445
simon 0:350011bf8be7 446 upap_sresp(u, retcode, id, msg, msglen);
simon 0:350011bf8be7 447
simon 0:350011bf8be7 448 if (retcode == UPAP_AUTHACK) {
simon 0:350011bf8be7 449 u->us_serverstate = UPAPSS_OPEN;
simon 0:350011bf8be7 450 auth_peer_success(u->us_unit, PPP_PAP, ruser, ruserlen);
simon 0:350011bf8be7 451 } else {
simon 0:350011bf8be7 452 u->us_serverstate = UPAPSS_BADAUTH;
simon 0:350011bf8be7 453 auth_peer_fail(u->us_unit, PPP_PAP);
simon 0:350011bf8be7 454 }
simon 0:350011bf8be7 455
simon 0:350011bf8be7 456 if (u->us_reqtimeout > 0) {
simon 0:350011bf8be7 457 UNTIMEOUT(upap_reqtimeout, u);
simon 0:350011bf8be7 458 }
simon 0:350011bf8be7 459 }
simon 0:350011bf8be7 460
simon 0:350011bf8be7 461
simon 0:350011bf8be7 462 /*
simon 0:350011bf8be7 463 * upap_rauthack - Receive Authenticate-Ack.
simon 0:350011bf8be7 464 */
simon 0:350011bf8be7 465 static void
simon 0:350011bf8be7 466 upap_rauthack(upap_state *u, u_char *inp, int id, int len)
simon 0:350011bf8be7 467 {
simon 0:350011bf8be7 468 u_char msglen;
simon 0:350011bf8be7 469 char *msg;
simon 0:350011bf8be7 470
simon 0:350011bf8be7 471 LWIP_UNUSED_ARG(id);
simon 0:350011bf8be7 472
simon 0:350011bf8be7 473 UPAPDEBUG(LOG_INFO, ("pap_rauthack: Rcvd id %d s=%d\n", id, u->us_clientstate));
simon 0:350011bf8be7 474
simon 0:350011bf8be7 475 if (u->us_clientstate != UPAPCS_AUTHREQ) { /* XXX */
simon 0:350011bf8be7 476 UPAPDEBUG(LOG_INFO, ("pap_rauthack: us_clientstate != UPAPCS_AUTHREQ\n"));
simon 0:350011bf8be7 477 return;
simon 0:350011bf8be7 478 }
simon 0:350011bf8be7 479
simon 0:350011bf8be7 480 /*
simon 0:350011bf8be7 481 * Parse message.
simon 0:350011bf8be7 482 */
simon 0:350011bf8be7 483 if (len < (int)sizeof (u_char)) {
simon 0:350011bf8be7 484 UPAPDEBUG(LOG_INFO, ("pap_rauthack: ignoring missing msg-length.\n"));
simon 0:350011bf8be7 485 } else {
simon 0:350011bf8be7 486 GETCHAR(msglen, inp);
simon 0:350011bf8be7 487 if (msglen > 0) {
simon 0:350011bf8be7 488 len -= sizeof (u_char);
simon 0:350011bf8be7 489 if (len < msglen) {
simon 0:350011bf8be7 490 UPAPDEBUG(LOG_INFO, ("pap_rauthack: rcvd short packet.\n"));
simon 0:350011bf8be7 491 return;
simon 0:350011bf8be7 492 }
simon 0:350011bf8be7 493 msg = (char *) inp;
simon 0:350011bf8be7 494 PRINTMSG(msg, msglen);
simon 0:350011bf8be7 495 }
simon 0:350011bf8be7 496 }
simon 0:350011bf8be7 497 UNTIMEOUT(upap_timeout, u); /* Cancel timeout */
simon 0:350011bf8be7 498 u->us_clientstate = UPAPCS_OPEN;
simon 0:350011bf8be7 499
simon 0:350011bf8be7 500 auth_withpeer_success(u->us_unit, PPP_PAP);
simon 0:350011bf8be7 501 }
simon 0:350011bf8be7 502
simon 0:350011bf8be7 503
simon 0:350011bf8be7 504 /*
simon 0:350011bf8be7 505 * upap_rauthnak - Receive Authenticate-Nak.
simon 0:350011bf8be7 506 */
simon 0:350011bf8be7 507 static void
simon 0:350011bf8be7 508 upap_rauthnak(upap_state *u, u_char *inp, int id, int len)
simon 0:350011bf8be7 509 {
simon 0:350011bf8be7 510 u_char msglen;
simon 0:350011bf8be7 511 char *msg;
simon 0:350011bf8be7 512
simon 0:350011bf8be7 513 LWIP_UNUSED_ARG(id);
simon 0:350011bf8be7 514
simon 0:350011bf8be7 515 UPAPDEBUG(LOG_INFO, ("pap_rauthnak: Rcvd id %d s=%d\n", id, u->us_clientstate));
simon 0:350011bf8be7 516
simon 0:350011bf8be7 517 if (u->us_clientstate != UPAPCS_AUTHREQ) { /* XXX */
simon 0:350011bf8be7 518 return;
simon 0:350011bf8be7 519 }
simon 0:350011bf8be7 520
simon 0:350011bf8be7 521 /*
simon 0:350011bf8be7 522 * Parse message.
simon 0:350011bf8be7 523 */
simon 0:350011bf8be7 524 if (len < sizeof (u_char)) {
simon 0:350011bf8be7 525 UPAPDEBUG(LOG_INFO, ("pap_rauthnak: ignoring missing msg-length.\n"));
simon 0:350011bf8be7 526 } else {
simon 0:350011bf8be7 527 GETCHAR(msglen, inp);
simon 0:350011bf8be7 528 if(msglen > 0) {
simon 0:350011bf8be7 529 len -= sizeof (u_char);
simon 0:350011bf8be7 530 if (len < msglen) {
simon 0:350011bf8be7 531 UPAPDEBUG(LOG_INFO, ("pap_rauthnak: rcvd short packet.\n"));
simon 0:350011bf8be7 532 return;
simon 0:350011bf8be7 533 }
simon 0:350011bf8be7 534 msg = (char *) inp;
simon 0:350011bf8be7 535 PRINTMSG(msg, msglen);
simon 0:350011bf8be7 536 }
simon 0:350011bf8be7 537 }
simon 0:350011bf8be7 538
simon 0:350011bf8be7 539 u->us_clientstate = UPAPCS_BADAUTH;
simon 0:350011bf8be7 540
simon 0:350011bf8be7 541 UPAPDEBUG(LOG_ERR, ("PAP authentication failed\n"));
simon 0:350011bf8be7 542 auth_withpeer_fail(u->us_unit, PPP_PAP);
simon 0:350011bf8be7 543 }
simon 0:350011bf8be7 544
simon 0:350011bf8be7 545
simon 0:350011bf8be7 546 /*
simon 0:350011bf8be7 547 * upap_sauthreq - Send an Authenticate-Request.
simon 0:350011bf8be7 548 */
simon 0:350011bf8be7 549 static void
simon 0:350011bf8be7 550 upap_sauthreq(upap_state *u)
simon 0:350011bf8be7 551 {
simon 0:350011bf8be7 552 u_char *outp;
simon 0:350011bf8be7 553 int outlen;
simon 0:350011bf8be7 554
simon 0:350011bf8be7 555 outlen = UPAP_HEADERLEN + 2 * sizeof (u_char)
simon 0:350011bf8be7 556 + u->us_userlen + u->us_passwdlen;
simon 0:350011bf8be7 557 outp = outpacket_buf[u->us_unit];
simon 0:350011bf8be7 558
simon 0:350011bf8be7 559 MAKEHEADER(outp, PPP_PAP);
simon 0:350011bf8be7 560
simon 0:350011bf8be7 561 PUTCHAR(UPAP_AUTHREQ, outp);
simon 0:350011bf8be7 562 PUTCHAR(++u->us_id, outp);
simon 0:350011bf8be7 563 PUTSHORT(outlen, outp);
simon 0:350011bf8be7 564 PUTCHAR(u->us_userlen, outp);
simon 0:350011bf8be7 565 BCOPY(u->us_user, outp, u->us_userlen);
simon 0:350011bf8be7 566 INCPTR(u->us_userlen, outp);
simon 0:350011bf8be7 567 PUTCHAR(u->us_passwdlen, outp);
simon 0:350011bf8be7 568 BCOPY(u->us_passwd, outp, u->us_passwdlen);
simon 0:350011bf8be7 569
simon 0:350011bf8be7 570 pppWrite(u->us_unit, outpacket_buf[u->us_unit], outlen + PPP_HDRLEN);
simon 0:350011bf8be7 571
simon 0:350011bf8be7 572 UPAPDEBUG(LOG_INFO, ("pap_sauth: Sent id %d\n", u->us_id));
simon 0:350011bf8be7 573
simon 0:350011bf8be7 574 TIMEOUT(upap_timeout, u, u->us_timeouttime);
simon 0:350011bf8be7 575 ++u->us_transmits;
simon 0:350011bf8be7 576 u->us_clientstate = UPAPCS_AUTHREQ;
simon 0:350011bf8be7 577 }
simon 0:350011bf8be7 578
simon 0:350011bf8be7 579
simon 0:350011bf8be7 580 /*
simon 0:350011bf8be7 581 * upap_sresp - Send a response (ack or nak).
simon 0:350011bf8be7 582 */
simon 0:350011bf8be7 583 static void
simon 0:350011bf8be7 584 upap_sresp(upap_state *u, u_char code, u_char id, char *msg, int msglen)
simon 0:350011bf8be7 585 {
simon 0:350011bf8be7 586 u_char *outp;
simon 0:350011bf8be7 587 int outlen;
simon 0:350011bf8be7 588
simon 0:350011bf8be7 589 outlen = UPAP_HEADERLEN + sizeof (u_char) + msglen;
simon 0:350011bf8be7 590 outp = outpacket_buf[u->us_unit];
simon 0:350011bf8be7 591 MAKEHEADER(outp, PPP_PAP);
simon 0:350011bf8be7 592
simon 0:350011bf8be7 593 PUTCHAR(code, outp);
simon 0:350011bf8be7 594 PUTCHAR(id, outp);
simon 0:350011bf8be7 595 PUTSHORT(outlen, outp);
simon 0:350011bf8be7 596 PUTCHAR(msglen, outp);
simon 0:350011bf8be7 597 BCOPY(msg, outp, msglen);
simon 0:350011bf8be7 598 pppWrite(u->us_unit, outpacket_buf[u->us_unit], outlen + PPP_HDRLEN);
simon 0:350011bf8be7 599
simon 0:350011bf8be7 600 UPAPDEBUG(LOG_INFO, ("pap_sresp: Sent code %d, id %d s=%d\n", code, id, u->us_clientstate));
simon 0:350011bf8be7 601 }
simon 0:350011bf8be7 602
simon 0:350011bf8be7 603 #if PPP_ADDITIONAL_CALLBACKS
simon 0:350011bf8be7 604 static char *upap_codenames[] = {
simon 0:350011bf8be7 605 "AuthReq", "AuthAck", "AuthNak"
simon 0:350011bf8be7 606 };
simon 0:350011bf8be7 607
simon 0:350011bf8be7 608 /*
simon 0:350011bf8be7 609 * upap_printpkt - print the contents of a PAP packet.
simon 0:350011bf8be7 610 */
simon 0:350011bf8be7 611 static int upap_printpkt(
simon 0:350011bf8be7 612 u_char *p,
simon 0:350011bf8be7 613 int plen,
simon 0:350011bf8be7 614 void (*printer) (void *, char *, ...),
simon 0:350011bf8be7 615 void *arg
simon 0:350011bf8be7 616 )
simon 0:350011bf8be7 617 {
simon 0:350011bf8be7 618 LWIP_UNUSED_ARG(p);
simon 0:350011bf8be7 619 LWIP_UNUSED_ARG(plen);
simon 0:350011bf8be7 620 LWIP_UNUSED_ARG(printer);
simon 0:350011bf8be7 621 LWIP_UNUSED_ARG(arg);
simon 0:350011bf8be7 622 return 0;
simon 0:350011bf8be7 623 }
simon 0:350011bf8be7 624 #endif /* PPP_ADDITIONAL_CALLBACKS */
simon 0:350011bf8be7 625
simon 0:350011bf8be7 626 #endif /* PAP_SUPPORT */
simon 0:350011bf8be7 627
simon 0:350011bf8be7 628 #endif /* PPP_SUPPORT */