LwIP with PPP & Ethernet integration

Dependents:   NetworkingCoreLib

This is the mbed port of the LwIP stack: http://savannah.nongnu.org/projects/lwip/

It includes contributed content from NXP's port for LPCxxxx devices: http://www.lpcware.com/content/project/lightweight-ip-lwip-networking-stack

Licence

LwIP is licenced under the BSD licence:

Copyright (c) 2001-2004 Swedish Institute of Computer Science. 
All rights reserved. 
Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met: 
1. Redistributions of source code must retain the above copyright notice, 
this list of conditions and the following disclaimer. 
2. Redistributions in binary form must reproduce the above copyright notice, 
this list of conditions and the following disclaimer in the documentation 
and/or other materials provided with the distribution. 
3. The name of the author may not be used to endorse or promote products 
derived from this software without specific prior written permission. 
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
OF SUCH DAMAGE.
Committer:
donatien
Date:
Fri May 25 08:56:35 2012 +0000
Revision:
2:1a87f74b8e3b
Parent:
0:8e01dca41002
Removed compilation of EMAC driver when using PPP

Who changed what in which revision?

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