IBM Watson IoT / Mbed OS IBMIoTClientEthernetExample

Dependencies:   C12832 MQTT LM75B MMA7660

Dependents:   MFT_IoT_demo_USB400 IBM_RFID

Committer:
samdanbury
Date:
Wed Aug 20 12:45:14 2014 +0000
Revision:
6:37b6d0d56190
Code completely changed to improve the structure, flow and memory usage of the application

Who changed what in which revision?

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