Webserver+3d print

Dependents:   Nucleo

Committer:
Sergunb
Date:
Sat Feb 04 18:15:49 2017 +0000
Revision:
0:8918a71cdbe9
nothing else

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Sergunb 0:8918a71cdbe9 1 /**
Sergunb 0:8918a71cdbe9 2 * @file ppp_debug.c
Sergunb 0:8918a71cdbe9 3 * @brief Data logging functions for debugging purpose (PPP)
Sergunb 0:8918a71cdbe9 4 *
Sergunb 0:8918a71cdbe9 5 * @section License
Sergunb 0:8918a71cdbe9 6 *
Sergunb 0:8918a71cdbe9 7 * Copyright (C) 2010-2017 Oryx Embedded SARL. All rights reserved.
Sergunb 0:8918a71cdbe9 8 *
Sergunb 0:8918a71cdbe9 9 * This file is part of CycloneTCP Open.
Sergunb 0:8918a71cdbe9 10 *
Sergunb 0:8918a71cdbe9 11 * This program is free software; you can redistribute it and/or
Sergunb 0:8918a71cdbe9 12 * modify it under the terms of the GNU General Public License
Sergunb 0:8918a71cdbe9 13 * as published by the Free Software Foundation; either version 2
Sergunb 0:8918a71cdbe9 14 * of the License, or (at your option) any later version.
Sergunb 0:8918a71cdbe9 15 *
Sergunb 0:8918a71cdbe9 16 * This program is distributed in the hope that it will be useful,
Sergunb 0:8918a71cdbe9 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Sergunb 0:8918a71cdbe9 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Sergunb 0:8918a71cdbe9 19 * GNU General Public License for more details.
Sergunb 0:8918a71cdbe9 20 *
Sergunb 0:8918a71cdbe9 21 * You should have received a copy of the GNU General Public License
Sergunb 0:8918a71cdbe9 22 * along with this program; if not, write to the Free Software Foundation,
Sergunb 0:8918a71cdbe9 23 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Sergunb 0:8918a71cdbe9 24 *
Sergunb 0:8918a71cdbe9 25 * @author Oryx Embedded SARL (www.oryx-embedded.com)
Sergunb 0:8918a71cdbe9 26 * @version 1.7.6
Sergunb 0:8918a71cdbe9 27 **/
Sergunb 0:8918a71cdbe9 28
Sergunb 0:8918a71cdbe9 29 //Dependencies
Sergunb 0:8918a71cdbe9 30 #include "core/net.h"
Sergunb 0:8918a71cdbe9 31 #include "ppp/ppp_debug.h"
Sergunb 0:8918a71cdbe9 32 #include "debug.h"
Sergunb 0:8918a71cdbe9 33
Sergunb 0:8918a71cdbe9 34 //Check TCP/IP stack configuration
Sergunb 0:8918a71cdbe9 35 #if (PPP_SUPPORT == ENABLED && PPP_TRACE_LEVEL >= TRACE_LEVEL_DEBUG)
Sergunb 0:8918a71cdbe9 36
Sergunb 0:8918a71cdbe9 37 //LCP codes
Sergunb 0:8918a71cdbe9 38 static const char_t *lcpCodeLabel[] =
Sergunb 0:8918a71cdbe9 39 {
Sergunb 0:8918a71cdbe9 40 "", //0
Sergunb 0:8918a71cdbe9 41 "Configure-Request", //1
Sergunb 0:8918a71cdbe9 42 "Configure-Ack", //2
Sergunb 0:8918a71cdbe9 43 "Configure-Nak", //3
Sergunb 0:8918a71cdbe9 44 "Configure-Reject", //4
Sergunb 0:8918a71cdbe9 45 "Terminate-Request", //5
Sergunb 0:8918a71cdbe9 46 "Terminate-Ack", //6
Sergunb 0:8918a71cdbe9 47 "Code-Reject", //7
Sergunb 0:8918a71cdbe9 48 "Protocol-Reject", //8
Sergunb 0:8918a71cdbe9 49 "Echo-Request", //9
Sergunb 0:8918a71cdbe9 50 "Echo-Reply", //10
Sergunb 0:8918a71cdbe9 51 "Discard-Request" //11
Sergunb 0:8918a71cdbe9 52 };
Sergunb 0:8918a71cdbe9 53
Sergunb 0:8918a71cdbe9 54 //NCP codes
Sergunb 0:8918a71cdbe9 55 static const char_t *ncpCodeLabel[] =
Sergunb 0:8918a71cdbe9 56 {
Sergunb 0:8918a71cdbe9 57 "", //0
Sergunb 0:8918a71cdbe9 58 "Configure-Request", //1
Sergunb 0:8918a71cdbe9 59 "Configure-Ack", //2
Sergunb 0:8918a71cdbe9 60 "Configure-Nak", //3
Sergunb 0:8918a71cdbe9 61 "Configure-Reject", //4
Sergunb 0:8918a71cdbe9 62 "Terminate-Request", //5
Sergunb 0:8918a71cdbe9 63 "Terminate-Ack", //6
Sergunb 0:8918a71cdbe9 64 "Code-Reject" //7
Sergunb 0:8918a71cdbe9 65 };
Sergunb 0:8918a71cdbe9 66
Sergunb 0:8918a71cdbe9 67 //PAP codes
Sergunb 0:8918a71cdbe9 68 static const char_t *papCodeLabel[] =
Sergunb 0:8918a71cdbe9 69 {
Sergunb 0:8918a71cdbe9 70 "", //0
Sergunb 0:8918a71cdbe9 71 "Authenticate-Request", //1
Sergunb 0:8918a71cdbe9 72 "Authenticate-Ack", //2
Sergunb 0:8918a71cdbe9 73 "Authenticate-Nak" //3
Sergunb 0:8918a71cdbe9 74 };
Sergunb 0:8918a71cdbe9 75
Sergunb 0:8918a71cdbe9 76 //CHAP codes
Sergunb 0:8918a71cdbe9 77 static const char_t *chapCodeLabel[] =
Sergunb 0:8918a71cdbe9 78 {
Sergunb 0:8918a71cdbe9 79 "", //0
Sergunb 0:8918a71cdbe9 80 "Challenge", //1
Sergunb 0:8918a71cdbe9 81 "Response", //2
Sergunb 0:8918a71cdbe9 82 "Success", //3
Sergunb 0:8918a71cdbe9 83 "Failure" //4
Sergunb 0:8918a71cdbe9 84 };
Sergunb 0:8918a71cdbe9 85
Sergunb 0:8918a71cdbe9 86 //LCP options
Sergunb 0:8918a71cdbe9 87 static const char_t *lcpOptionLabel[] =
Sergunb 0:8918a71cdbe9 88 {
Sergunb 0:8918a71cdbe9 89 "", //0
Sergunb 0:8918a71cdbe9 90 "Maximum-Receive-Unit", //1
Sergunb 0:8918a71cdbe9 91 "Async-Control-Character-Map", //2
Sergunb 0:8918a71cdbe9 92 "Authentication-Protocol", //3
Sergunb 0:8918a71cdbe9 93 "Quality-Protocol", //4
Sergunb 0:8918a71cdbe9 94 "Magic-Number", //5
Sergunb 0:8918a71cdbe9 95 "", //6
Sergunb 0:8918a71cdbe9 96 "Protocol-Field-Compression", //7
Sergunb 0:8918a71cdbe9 97 "Address-and-Control-Field-Compression", //8
Sergunb 0:8918a71cdbe9 98 "FCS-Alternatives", //9
Sergunb 0:8918a71cdbe9 99 "Self-Describing-Pad", //10
Sergunb 0:8918a71cdbe9 100 "Numbered-Mode", //11
Sergunb 0:8918a71cdbe9 101 "", //12
Sergunb 0:8918a71cdbe9 102 "Callback" //13
Sergunb 0:8918a71cdbe9 103 };
Sergunb 0:8918a71cdbe9 104
Sergunb 0:8918a71cdbe9 105 //IPCP options
Sergunb 0:8918a71cdbe9 106 static const char_t *ipcpOptionLabel[] =
Sergunb 0:8918a71cdbe9 107 {
Sergunb 0:8918a71cdbe9 108 "", //0
Sergunb 0:8918a71cdbe9 109 "IP-Addresses", //1
Sergunb 0:8918a71cdbe9 110 "IP-Compression-Protocol", //2
Sergunb 0:8918a71cdbe9 111 "IP-Address", //3
Sergunb 0:8918a71cdbe9 112 "Mobile-IPv4", //4
Sergunb 0:8918a71cdbe9 113 };
Sergunb 0:8918a71cdbe9 114
Sergunb 0:8918a71cdbe9 115 static const char_t *ipcpOptionLabel2[] =
Sergunb 0:8918a71cdbe9 116 {
Sergunb 0:8918a71cdbe9 117 "", //128
Sergunb 0:8918a71cdbe9 118 "Primary-DNS-Server-Address", //129
Sergunb 0:8918a71cdbe9 119 "Primary-NBNS-Server-Address", //130
Sergunb 0:8918a71cdbe9 120 "Secondary-DNS-Server-Address", //131
Sergunb 0:8918a71cdbe9 121 "Secondary-NBNS-Server-Address" //132
Sergunb 0:8918a71cdbe9 122 };
Sergunb 0:8918a71cdbe9 123
Sergunb 0:8918a71cdbe9 124 //IPV6CP options
Sergunb 0:8918a71cdbe9 125 static const char_t *ipv6cpOptionLabel[] =
Sergunb 0:8918a71cdbe9 126 {
Sergunb 0:8918a71cdbe9 127 "", //0
Sergunb 0:8918a71cdbe9 128 "Interface-Identifier", //1
Sergunb 0:8918a71cdbe9 129 "IPv6-Compression-Protocol" //2
Sergunb 0:8918a71cdbe9 130 };
Sergunb 0:8918a71cdbe9 131
Sergunb 0:8918a71cdbe9 132
Sergunb 0:8918a71cdbe9 133 /**
Sergunb 0:8918a71cdbe9 134 * @brief Dump LCP/NCP packet for debugging purpose
Sergunb 0:8918a71cdbe9 135 * @param[in] packet Pointer to the LCP packet
Sergunb 0:8918a71cdbe9 136 * @param[in] length Length of the packet, in bytes
Sergunb 0:8918a71cdbe9 137 * @param[in] protocol Protocol field
Sergunb 0:8918a71cdbe9 138 * @return Error code
Sergunb 0:8918a71cdbe9 139 **/
Sergunb 0:8918a71cdbe9 140
Sergunb 0:8918a71cdbe9 141 error_t pppDumpPacket(const PppPacket *packet, size_t length, PppProtocol protocol)
Sergunb 0:8918a71cdbe9 142 {
Sergunb 0:8918a71cdbe9 143 error_t error;
Sergunb 0:8918a71cdbe9 144
Sergunb 0:8918a71cdbe9 145 //Check protocol field
Sergunb 0:8918a71cdbe9 146 switch(protocol)
Sergunb 0:8918a71cdbe9 147 {
Sergunb 0:8918a71cdbe9 148 //LCP packet?
Sergunb 0:8918a71cdbe9 149 case PPP_PROTOCOL_LCP:
Sergunb 0:8918a71cdbe9 150 error = lcpDumpPacket(packet, length);
Sergunb 0:8918a71cdbe9 151 break;
Sergunb 0:8918a71cdbe9 152 //NCP packet?
Sergunb 0:8918a71cdbe9 153 case PPP_PROTOCOL_IPCP:
Sergunb 0:8918a71cdbe9 154 case PPP_PROTOCOL_IPV6CP:
Sergunb 0:8918a71cdbe9 155 error = ncpDumpPacket(packet, length, protocol);
Sergunb 0:8918a71cdbe9 156 break;
Sergunb 0:8918a71cdbe9 157 //PAP packet?
Sergunb 0:8918a71cdbe9 158 case PPP_PROTOCOL_PAP:
Sergunb 0:8918a71cdbe9 159 error = papDumpPacket(packet, length);
Sergunb 0:8918a71cdbe9 160 break;
Sergunb 0:8918a71cdbe9 161 //CHAP packet?
Sergunb 0:8918a71cdbe9 162 case PPP_PROTOCOL_CHAP:
Sergunb 0:8918a71cdbe9 163 error = chapDumpPacket(packet, length);
Sergunb 0:8918a71cdbe9 164 break;
Sergunb 0:8918a71cdbe9 165 //Unknown protocol?
Sergunb 0:8918a71cdbe9 166 default:
Sergunb 0:8918a71cdbe9 167 error = ERROR_FAILURE;
Sergunb 0:8918a71cdbe9 168 break;
Sergunb 0:8918a71cdbe9 169 }
Sergunb 0:8918a71cdbe9 170
Sergunb 0:8918a71cdbe9 171 //Return status code
Sergunb 0:8918a71cdbe9 172 return error;
Sergunb 0:8918a71cdbe9 173 }
Sergunb 0:8918a71cdbe9 174
Sergunb 0:8918a71cdbe9 175
Sergunb 0:8918a71cdbe9 176 /**
Sergunb 0:8918a71cdbe9 177 * @brief Dump LCP packet for debugging purpose
Sergunb 0:8918a71cdbe9 178 * @param[in] packet Pointer to the LCP packet
Sergunb 0:8918a71cdbe9 179 * @param[in] length Length of the packet, in bytes
Sergunb 0:8918a71cdbe9 180 * @return Error code
Sergunb 0:8918a71cdbe9 181 **/
Sergunb 0:8918a71cdbe9 182
Sergunb 0:8918a71cdbe9 183 error_t lcpDumpPacket(const PppPacket *packet, size_t length)
Sergunb 0:8918a71cdbe9 184 {
Sergunb 0:8918a71cdbe9 185 error_t error;
Sergunb 0:8918a71cdbe9 186 const char_t *label;
Sergunb 0:8918a71cdbe9 187
Sergunb 0:8918a71cdbe9 188 //Make sure the LCP packet is valid
Sergunb 0:8918a71cdbe9 189 if(length < sizeof(PppPacket))
Sergunb 0:8918a71cdbe9 190 return ERROR_INVALID_LENGTH;
Sergunb 0:8918a71cdbe9 191
Sergunb 0:8918a71cdbe9 192 //Check the length field
Sergunb 0:8918a71cdbe9 193 if(ntohs(packet->length) > length)
Sergunb 0:8918a71cdbe9 194 return ERROR_INVALID_LENGTH;
Sergunb 0:8918a71cdbe9 195 if(ntohs(packet->length) < sizeof(PppPacket))
Sergunb 0:8918a71cdbe9 196 return ERROR_INVALID_LENGTH;
Sergunb 0:8918a71cdbe9 197
Sergunb 0:8918a71cdbe9 198 //Save the length of the LCP packet
Sergunb 0:8918a71cdbe9 199 length = ntohs(packet->length);
Sergunb 0:8918a71cdbe9 200
Sergunb 0:8918a71cdbe9 201 //Retrieve the name of the LCP packet
Sergunb 0:8918a71cdbe9 202 if(packet->code < arraysize(lcpCodeLabel))
Sergunb 0:8918a71cdbe9 203 label = lcpCodeLabel[packet->code];
Sergunb 0:8918a71cdbe9 204 else
Sergunb 0:8918a71cdbe9 205 label = "Unknown";
Sergunb 0:8918a71cdbe9 206
Sergunb 0:8918a71cdbe9 207 //Dump LCP packet header
Sergunb 0:8918a71cdbe9 208 TRACE_DEBUG(" Code = %" PRIu8 " (%s)\r\n", packet->code, label);
Sergunb 0:8918a71cdbe9 209 TRACE_DEBUG(" Identifier = %" PRIu8 "\r\n", packet->identifier);
Sergunb 0:8918a71cdbe9 210 TRACE_DEBUG(" Length = %" PRIu16 "\r\n", ntohs(packet->length));
Sergunb 0:8918a71cdbe9 211
Sergunb 0:8918a71cdbe9 212 //Configure-Request, Configure-Ack, Configure-Nak or Configure-Reject packet?
Sergunb 0:8918a71cdbe9 213 if(packet->code == PPP_CODE_CONFIGURE_REQ ||
Sergunb 0:8918a71cdbe9 214 packet->code == PPP_CODE_CONFIGURE_ACK ||
Sergunb 0:8918a71cdbe9 215 packet->code == PPP_CODE_CONFIGURE_NAK ||
Sergunb 0:8918a71cdbe9 216 packet->code == PPP_CODE_CONFIGURE_REJ)
Sergunb 0:8918a71cdbe9 217 {
Sergunb 0:8918a71cdbe9 218 //Cast LCP packet
Sergunb 0:8918a71cdbe9 219 PppConfigurePacket *p = (PppConfigurePacket *) packet;
Sergunb 0:8918a71cdbe9 220
Sergunb 0:8918a71cdbe9 221 //Valid packet length?
Sergunb 0:8918a71cdbe9 222 if(length < sizeof(PppConfigurePacket))
Sergunb 0:8918a71cdbe9 223 return ERROR_INVALID_LENGTH;
Sergunb 0:8918a71cdbe9 224
Sergunb 0:8918a71cdbe9 225 //Retrieve the length of the option list
Sergunb 0:8918a71cdbe9 226 length -= sizeof(PppConfigurePacket);
Sergunb 0:8918a71cdbe9 227
Sergunb 0:8918a71cdbe9 228 //Dump options
Sergunb 0:8918a71cdbe9 229 error = lcpDumpOptions((PppOption *) p->options, length);
Sergunb 0:8918a71cdbe9 230 //Any error to report?
Sergunb 0:8918a71cdbe9 231 if(error)
Sergunb 0:8918a71cdbe9 232 return error;
Sergunb 0:8918a71cdbe9 233 }
Sergunb 0:8918a71cdbe9 234 //Terminate-Request or Terminate-Ack packet?
Sergunb 0:8918a71cdbe9 235 else if(packet->code == PPP_CODE_TERMINATE_REQ ||
Sergunb 0:8918a71cdbe9 236 packet->code == PPP_CODE_TERMINATE_ACK)
Sergunb 0:8918a71cdbe9 237 {
Sergunb 0:8918a71cdbe9 238 //Cast LCP packet
Sergunb 0:8918a71cdbe9 239 PppTerminatePacket *p = (PppTerminatePacket *) packet;
Sergunb 0:8918a71cdbe9 240
Sergunb 0:8918a71cdbe9 241 //Valid packet length?
Sergunb 0:8918a71cdbe9 242 if(length < sizeof(PppTerminatePacket))
Sergunb 0:8918a71cdbe9 243 return ERROR_INVALID_LENGTH;
Sergunb 0:8918a71cdbe9 244
Sergunb 0:8918a71cdbe9 245 //Retrieve the length of data
Sergunb 0:8918a71cdbe9 246 length -= sizeof(PppTerminatePacket);
Sergunb 0:8918a71cdbe9 247
Sergunb 0:8918a71cdbe9 248 //Any data?
Sergunb 0:8918a71cdbe9 249 if(length > 0)
Sergunb 0:8918a71cdbe9 250 {
Sergunb 0:8918a71cdbe9 251 //Dump data
Sergunb 0:8918a71cdbe9 252 TRACE_DEBUG(" Data (%" PRIuSIZE " bytes)\r\n", length);
Sergunb 0:8918a71cdbe9 253 TRACE_DEBUG_ARRAY(" ", p->data, length);
Sergunb 0:8918a71cdbe9 254 }
Sergunb 0:8918a71cdbe9 255 }
Sergunb 0:8918a71cdbe9 256 //Code-Reject packet?
Sergunb 0:8918a71cdbe9 257 else if(packet->code == PPP_CODE_CODE_REJ)
Sergunb 0:8918a71cdbe9 258 {
Sergunb 0:8918a71cdbe9 259 //Cast LCP packet
Sergunb 0:8918a71cdbe9 260 PppCodeRejPacket *p = (PppCodeRejPacket *) packet;
Sergunb 0:8918a71cdbe9 261
Sergunb 0:8918a71cdbe9 262 //Valid packet length?
Sergunb 0:8918a71cdbe9 263 if(length < sizeof(PppCodeRejPacket))
Sergunb 0:8918a71cdbe9 264 return ERROR_INVALID_LENGTH;
Sergunb 0:8918a71cdbe9 265
Sergunb 0:8918a71cdbe9 266 //Retrieve the length of Rejected-Packet field
Sergunb 0:8918a71cdbe9 267 length -= sizeof(PppCodeRejPacket);
Sergunb 0:8918a71cdbe9 268
Sergunb 0:8918a71cdbe9 269 //Rejected-Packet
Sergunb 0:8918a71cdbe9 270 TRACE_DEBUG(" Rejected-Packet (%" PRIuSIZE " bytes)\r\n", length);
Sergunb 0:8918a71cdbe9 271 TRACE_DEBUG_ARRAY(" ", p->rejectedPacket, length);
Sergunb 0:8918a71cdbe9 272 }
Sergunb 0:8918a71cdbe9 273 //Protocol-Reject packet?
Sergunb 0:8918a71cdbe9 274 else if(packet->code == PPP_CODE_PROTOCOL_REJ)
Sergunb 0:8918a71cdbe9 275 {
Sergunb 0:8918a71cdbe9 276 //Cast LCP packet
Sergunb 0:8918a71cdbe9 277 PppProtocolRejPacket *p = (PppProtocolRejPacket *) packet;
Sergunb 0:8918a71cdbe9 278
Sergunb 0:8918a71cdbe9 279 //Valid packet length?
Sergunb 0:8918a71cdbe9 280 if(length < sizeof(PppProtocolRejPacket))
Sergunb 0:8918a71cdbe9 281 return ERROR_INVALID_LENGTH;
Sergunb 0:8918a71cdbe9 282
Sergunb 0:8918a71cdbe9 283 //Retrieve the length of Rejected-Information field
Sergunb 0:8918a71cdbe9 284 length -= sizeof(PppProtocolRejPacket);
Sergunb 0:8918a71cdbe9 285
Sergunb 0:8918a71cdbe9 286 //Rejected-Protocol
Sergunb 0:8918a71cdbe9 287 TRACE_DEBUG(" Rejected-Protocol = %" PRIu16 "\r\n", htons(p->rejectedProtocol));
Sergunb 0:8918a71cdbe9 288 //Rejected-Information
Sergunb 0:8918a71cdbe9 289 TRACE_DEBUG(" Rejected-Information (%" PRIuSIZE " bytes)\r\n", length);
Sergunb 0:8918a71cdbe9 290 TRACE_DEBUG_ARRAY(" ", p->rejectedInfo, length);
Sergunb 0:8918a71cdbe9 291 }
Sergunb 0:8918a71cdbe9 292 //Echo-Request, Echo-Reply or Discard-Request packet?
Sergunb 0:8918a71cdbe9 293 else if(packet->code == PPP_CODE_ECHO_REQ ||
Sergunb 0:8918a71cdbe9 294 packet->code == PPP_CODE_ECHO_REP ||
Sergunb 0:8918a71cdbe9 295 packet->code == PPP_CODE_DISCARD_REQ)
Sergunb 0:8918a71cdbe9 296 {
Sergunb 0:8918a71cdbe9 297 //Cast LCP packet
Sergunb 0:8918a71cdbe9 298 PppEchoPacket *p = (PppEchoPacket *) packet;
Sergunb 0:8918a71cdbe9 299
Sergunb 0:8918a71cdbe9 300 //Valid packet length?
Sergunb 0:8918a71cdbe9 301 if(length < sizeof(PppEchoPacket))
Sergunb 0:8918a71cdbe9 302 return ERROR_INVALID_LENGTH;
Sergunb 0:8918a71cdbe9 303
Sergunb 0:8918a71cdbe9 304 //Retrieve the length of data
Sergunb 0:8918a71cdbe9 305 length -= sizeof(PppEchoPacket);
Sergunb 0:8918a71cdbe9 306
Sergunb 0:8918a71cdbe9 307 //Magic-Number
Sergunb 0:8918a71cdbe9 308 TRACE_DEBUG(" Magic-Number = %" PRIu32 "\r\n", htonl(p->magicNumber));
Sergunb 0:8918a71cdbe9 309 //Data
Sergunb 0:8918a71cdbe9 310 TRACE_DEBUG(" Data (%" PRIuSIZE " bytes)\r\n", length);
Sergunb 0:8918a71cdbe9 311 TRACE_DEBUG_ARRAY(" ", p->data, length);
Sergunb 0:8918a71cdbe9 312 }
Sergunb 0:8918a71cdbe9 313 //Unknown packet?
Sergunb 0:8918a71cdbe9 314 else
Sergunb 0:8918a71cdbe9 315 {
Sergunb 0:8918a71cdbe9 316 //Retrieve the length of data
Sergunb 0:8918a71cdbe9 317 length -= sizeof(PppPacket);
Sergunb 0:8918a71cdbe9 318
Sergunb 0:8918a71cdbe9 319 //Any data?
Sergunb 0:8918a71cdbe9 320 if(length > 0)
Sergunb 0:8918a71cdbe9 321 {
Sergunb 0:8918a71cdbe9 322 //Dump data
Sergunb 0:8918a71cdbe9 323 TRACE_DEBUG(" Data (%" PRIuSIZE " bytes)\r\n", length);
Sergunb 0:8918a71cdbe9 324 TRACE_DEBUG_ARRAY(" ", packet->data, length);
Sergunb 0:8918a71cdbe9 325 }
Sergunb 0:8918a71cdbe9 326 }
Sergunb 0:8918a71cdbe9 327
Sergunb 0:8918a71cdbe9 328 //No error to report
Sergunb 0:8918a71cdbe9 329 return NO_ERROR;
Sergunb 0:8918a71cdbe9 330 }
Sergunb 0:8918a71cdbe9 331
Sergunb 0:8918a71cdbe9 332
Sergunb 0:8918a71cdbe9 333 /**
Sergunb 0:8918a71cdbe9 334 * @brief Dump NCP packet for debugging purpose
Sergunb 0:8918a71cdbe9 335 * @param[in] packet Pointer to the NCP packet
Sergunb 0:8918a71cdbe9 336 * @param[in] length Length of the packet, in bytes
Sergunb 0:8918a71cdbe9 337 * @param[in] protocol Protocol field
Sergunb 0:8918a71cdbe9 338 * @return Error code
Sergunb 0:8918a71cdbe9 339 **/
Sergunb 0:8918a71cdbe9 340
Sergunb 0:8918a71cdbe9 341 error_t ncpDumpPacket(const PppPacket *packet, size_t length, PppProtocol protocol)
Sergunb 0:8918a71cdbe9 342 {
Sergunb 0:8918a71cdbe9 343 error_t error;
Sergunb 0:8918a71cdbe9 344 const char_t *label;
Sergunb 0:8918a71cdbe9 345
Sergunb 0:8918a71cdbe9 346 //Make sure the NDP packet is valid
Sergunb 0:8918a71cdbe9 347 if(length < sizeof(PppPacket))
Sergunb 0:8918a71cdbe9 348 return ERROR_INVALID_LENGTH;
Sergunb 0:8918a71cdbe9 349
Sergunb 0:8918a71cdbe9 350 //Check the length field
Sergunb 0:8918a71cdbe9 351 if(ntohs(packet->length) > length)
Sergunb 0:8918a71cdbe9 352 return ERROR_INVALID_LENGTH;
Sergunb 0:8918a71cdbe9 353 if(ntohs(packet->length) < sizeof(PppPacket))
Sergunb 0:8918a71cdbe9 354 return ERROR_INVALID_LENGTH;
Sergunb 0:8918a71cdbe9 355
Sergunb 0:8918a71cdbe9 356 //Save the length of the NDP packet
Sergunb 0:8918a71cdbe9 357 length = ntohs(packet->length);
Sergunb 0:8918a71cdbe9 358
Sergunb 0:8918a71cdbe9 359 //Retrieve the name of the NDP packet
Sergunb 0:8918a71cdbe9 360 if(packet->code < arraysize(ncpCodeLabel))
Sergunb 0:8918a71cdbe9 361 label = ncpCodeLabel[packet->code];
Sergunb 0:8918a71cdbe9 362 else
Sergunb 0:8918a71cdbe9 363 label = "Unknown";
Sergunb 0:8918a71cdbe9 364
Sergunb 0:8918a71cdbe9 365 //Dump NDP packet header
Sergunb 0:8918a71cdbe9 366 TRACE_DEBUG(" Code = %" PRIu8 " (%s)\r\n", packet->code, label);
Sergunb 0:8918a71cdbe9 367 TRACE_DEBUG(" Identifier = %" PRIu8 "\r\n", packet->identifier);
Sergunb 0:8918a71cdbe9 368 TRACE_DEBUG(" Length = %" PRIu16 "\r\n", ntohs(packet->length));
Sergunb 0:8918a71cdbe9 369
Sergunb 0:8918a71cdbe9 370 //Configure-Request, Configure-Ack, Configure-Nak or Configure-Reject packet?
Sergunb 0:8918a71cdbe9 371 if(packet->code == PPP_CODE_CONFIGURE_REQ ||
Sergunb 0:8918a71cdbe9 372 packet->code == PPP_CODE_CONFIGURE_ACK ||
Sergunb 0:8918a71cdbe9 373 packet->code == PPP_CODE_CONFIGURE_NAK ||
Sergunb 0:8918a71cdbe9 374 packet->code == PPP_CODE_CONFIGURE_REJ)
Sergunb 0:8918a71cdbe9 375 {
Sergunb 0:8918a71cdbe9 376 //Cast NDP packet
Sergunb 0:8918a71cdbe9 377 PppConfigurePacket *p = (PppConfigurePacket *) packet;
Sergunb 0:8918a71cdbe9 378
Sergunb 0:8918a71cdbe9 379 //Valid packet length?
Sergunb 0:8918a71cdbe9 380 if(length < sizeof(PppConfigurePacket))
Sergunb 0:8918a71cdbe9 381 return ERROR_INVALID_LENGTH;
Sergunb 0:8918a71cdbe9 382
Sergunb 0:8918a71cdbe9 383 //Retrieve the length of the option list
Sergunb 0:8918a71cdbe9 384 length -= sizeof(PppConfigurePacket);
Sergunb 0:8918a71cdbe9 385
Sergunb 0:8918a71cdbe9 386 //IPCP protocol?
Sergunb 0:8918a71cdbe9 387 if(protocol == PPP_PROTOCOL_IPCP)
Sergunb 0:8918a71cdbe9 388 {
Sergunb 0:8918a71cdbe9 389 //Dump options
Sergunb 0:8918a71cdbe9 390 error = ipcpDumpOptions((PppOption *) p->options, length);
Sergunb 0:8918a71cdbe9 391 //Any error to report?
Sergunb 0:8918a71cdbe9 392 if(error)
Sergunb 0:8918a71cdbe9 393 return error;
Sergunb 0:8918a71cdbe9 394 }
Sergunb 0:8918a71cdbe9 395 //IPV6CP protocol?
Sergunb 0:8918a71cdbe9 396 else if(protocol == PPP_PROTOCOL_IPV6CP)
Sergunb 0:8918a71cdbe9 397 {
Sergunb 0:8918a71cdbe9 398 //Dump options
Sergunb 0:8918a71cdbe9 399 error = ipv6cpDumpOptions((PppOption *) p->options, length);
Sergunb 0:8918a71cdbe9 400 //Any error to report?
Sergunb 0:8918a71cdbe9 401 if(error)
Sergunb 0:8918a71cdbe9 402 return error;
Sergunb 0:8918a71cdbe9 403 }
Sergunb 0:8918a71cdbe9 404 }
Sergunb 0:8918a71cdbe9 405 //Terminate-Request or Terminate-Ack packet?
Sergunb 0:8918a71cdbe9 406 else if(packet->code == PPP_CODE_TERMINATE_REQ ||
Sergunb 0:8918a71cdbe9 407 packet->code == PPP_CODE_TERMINATE_ACK)
Sergunb 0:8918a71cdbe9 408 {
Sergunb 0:8918a71cdbe9 409 //Cast NDP packet
Sergunb 0:8918a71cdbe9 410 PppTerminatePacket *p = (PppTerminatePacket *) packet;
Sergunb 0:8918a71cdbe9 411
Sergunb 0:8918a71cdbe9 412 //Valid packet length?
Sergunb 0:8918a71cdbe9 413 if(length < sizeof(PppTerminatePacket))
Sergunb 0:8918a71cdbe9 414 return ERROR_INVALID_LENGTH;
Sergunb 0:8918a71cdbe9 415
Sergunb 0:8918a71cdbe9 416 //Retrieve the length of data
Sergunb 0:8918a71cdbe9 417 length -= sizeof(PppTerminatePacket);
Sergunb 0:8918a71cdbe9 418
Sergunb 0:8918a71cdbe9 419 //Any data?
Sergunb 0:8918a71cdbe9 420 if(length > 0)
Sergunb 0:8918a71cdbe9 421 {
Sergunb 0:8918a71cdbe9 422 //Dump data
Sergunb 0:8918a71cdbe9 423 TRACE_DEBUG(" Data (%" PRIuSIZE " bytes)\r\n", length);
Sergunb 0:8918a71cdbe9 424 TRACE_DEBUG_ARRAY(" ", p->data, length);
Sergunb 0:8918a71cdbe9 425 }
Sergunb 0:8918a71cdbe9 426 }
Sergunb 0:8918a71cdbe9 427 //Code-Reject packet?
Sergunb 0:8918a71cdbe9 428 else if(packet->code == PPP_CODE_CODE_REJ)
Sergunb 0:8918a71cdbe9 429 {
Sergunb 0:8918a71cdbe9 430 //Cast NDP packet
Sergunb 0:8918a71cdbe9 431 PppCodeRejPacket *p = (PppCodeRejPacket *) packet;
Sergunb 0:8918a71cdbe9 432
Sergunb 0:8918a71cdbe9 433 //Valid packet length?
Sergunb 0:8918a71cdbe9 434 if(length < sizeof(PppCodeRejPacket))
Sergunb 0:8918a71cdbe9 435 return ERROR_INVALID_LENGTH;
Sergunb 0:8918a71cdbe9 436
Sergunb 0:8918a71cdbe9 437 //Retrieve the length of Rejected-Packet field
Sergunb 0:8918a71cdbe9 438 length -= sizeof(PppCodeRejPacket);
Sergunb 0:8918a71cdbe9 439
Sergunb 0:8918a71cdbe9 440 //Rejected-Packet
Sergunb 0:8918a71cdbe9 441 TRACE_DEBUG(" Rejected-Packet (%" PRIuSIZE " bytes)\r\n", length);
Sergunb 0:8918a71cdbe9 442 TRACE_DEBUG_ARRAY(" ", p->rejectedPacket, length);
Sergunb 0:8918a71cdbe9 443 }
Sergunb 0:8918a71cdbe9 444 //Unknown packet?
Sergunb 0:8918a71cdbe9 445 else
Sergunb 0:8918a71cdbe9 446 {
Sergunb 0:8918a71cdbe9 447 //Retrieve the length of data
Sergunb 0:8918a71cdbe9 448 length -= sizeof(PppPacket);
Sergunb 0:8918a71cdbe9 449
Sergunb 0:8918a71cdbe9 450 //Any data?
Sergunb 0:8918a71cdbe9 451 if(length > 0)
Sergunb 0:8918a71cdbe9 452 {
Sergunb 0:8918a71cdbe9 453 //Dump data
Sergunb 0:8918a71cdbe9 454 TRACE_DEBUG(" Data (%" PRIuSIZE " bytes)\r\n", length);
Sergunb 0:8918a71cdbe9 455 TRACE_DEBUG_ARRAY(" ", packet->data, length);
Sergunb 0:8918a71cdbe9 456 }
Sergunb 0:8918a71cdbe9 457 }
Sergunb 0:8918a71cdbe9 458
Sergunb 0:8918a71cdbe9 459 //No error to report
Sergunb 0:8918a71cdbe9 460 return NO_ERROR;
Sergunb 0:8918a71cdbe9 461 }
Sergunb 0:8918a71cdbe9 462
Sergunb 0:8918a71cdbe9 463
Sergunb 0:8918a71cdbe9 464 /**
Sergunb 0:8918a71cdbe9 465 * @brief Dump PAP packet for debugging purpose
Sergunb 0:8918a71cdbe9 466 * @param[in] packet Pointer to the PAP packet
Sergunb 0:8918a71cdbe9 467 * @param[in] length Length of the packet, in bytes
Sergunb 0:8918a71cdbe9 468 * @return Error code
Sergunb 0:8918a71cdbe9 469 **/
Sergunb 0:8918a71cdbe9 470
Sergunb 0:8918a71cdbe9 471 error_t papDumpPacket(const PppPacket *packet, size_t length)
Sergunb 0:8918a71cdbe9 472 {
Sergunb 0:8918a71cdbe9 473 const char_t *label;
Sergunb 0:8918a71cdbe9 474
Sergunb 0:8918a71cdbe9 475 //Make sure the PAP packet is valid
Sergunb 0:8918a71cdbe9 476 if(length < sizeof(PppPacket))
Sergunb 0:8918a71cdbe9 477 return ERROR_INVALID_LENGTH;
Sergunb 0:8918a71cdbe9 478
Sergunb 0:8918a71cdbe9 479 //Check the length field
Sergunb 0:8918a71cdbe9 480 if(ntohs(packet->length) > length)
Sergunb 0:8918a71cdbe9 481 return ERROR_INVALID_LENGTH;
Sergunb 0:8918a71cdbe9 482 if(ntohs(packet->length) < sizeof(PppPacket))
Sergunb 0:8918a71cdbe9 483 return ERROR_INVALID_LENGTH;
Sergunb 0:8918a71cdbe9 484
Sergunb 0:8918a71cdbe9 485 //Save the length of the PAP packet
Sergunb 0:8918a71cdbe9 486 length = ntohs(packet->length);
Sergunb 0:8918a71cdbe9 487
Sergunb 0:8918a71cdbe9 488 //Retrieve the name of the PAP packet
Sergunb 0:8918a71cdbe9 489 if(packet->code < arraysize(papCodeLabel))
Sergunb 0:8918a71cdbe9 490 label = papCodeLabel[packet->code];
Sergunb 0:8918a71cdbe9 491 else
Sergunb 0:8918a71cdbe9 492 label = "Unknown";
Sergunb 0:8918a71cdbe9 493
Sergunb 0:8918a71cdbe9 494 //Dump PAP packet header
Sergunb 0:8918a71cdbe9 495 TRACE_DEBUG(" Code = %" PRIu8 " (%s)\r\n", packet->code, label);
Sergunb 0:8918a71cdbe9 496 TRACE_DEBUG(" Identifier = %" PRIu8 "\r\n", packet->identifier);
Sergunb 0:8918a71cdbe9 497 TRACE_DEBUG(" Length = %" PRIu16 "\r\n", ntohs(packet->length));
Sergunb 0:8918a71cdbe9 498
Sergunb 0:8918a71cdbe9 499 //Authenticate-Request packet?
Sergunb 0:8918a71cdbe9 500 if(packet->code == PAP_CODE_AUTH_REQ)
Sergunb 0:8918a71cdbe9 501 {
Sergunb 0:8918a71cdbe9 502 uint8_t *q;
Sergunb 0:8918a71cdbe9 503 PapAuthReqPacket *p;
Sergunb 0:8918a71cdbe9 504
Sergunb 0:8918a71cdbe9 505 //Cast PAP packet
Sergunb 0:8918a71cdbe9 506 p = (PapAuthReqPacket *) packet;
Sergunb 0:8918a71cdbe9 507
Sergunb 0:8918a71cdbe9 508 //Valid packet length?
Sergunb 0:8918a71cdbe9 509 if(length < sizeof(PapAuthReqPacket))
Sergunb 0:8918a71cdbe9 510 return ERROR_INVALID_LENGTH;
Sergunb 0:8918a71cdbe9 511
Sergunb 0:8918a71cdbe9 512 //Peer-ID-Length field
Sergunb 0:8918a71cdbe9 513 TRACE_DEBUG(" Peer-ID-Length = %" PRIu8 "\r\n", p->peerIdLength);
Sergunb 0:8918a71cdbe9 514
Sergunb 0:8918a71cdbe9 515 //Check the length of the Peer-ID field
Sergunb 0:8918a71cdbe9 516 if(length < (sizeof(PapAuthAckPacket) + 1 + p->peerIdLength))
Sergunb 0:8918a71cdbe9 517 return ERROR_INVALID_LENGTH;
Sergunb 0:8918a71cdbe9 518
Sergunb 0:8918a71cdbe9 519 //Peer-ID field
Sergunb 0:8918a71cdbe9 520 TRACE_DEBUG(" Peer-ID\r\n");
Sergunb 0:8918a71cdbe9 521 TRACE_DEBUG_ARRAY(" ", p->peerId, p->peerIdLength);
Sergunb 0:8918a71cdbe9 522
Sergunb 0:8918a71cdbe9 523 //Point to the Passwd-Length field
Sergunb 0:8918a71cdbe9 524 q = p->peerId + p->peerIdLength;
Sergunb 0:8918a71cdbe9 525
Sergunb 0:8918a71cdbe9 526 //Passwd-Length field
Sergunb 0:8918a71cdbe9 527 TRACE_DEBUG(" Passwd-Length = %" PRIu8 "\r\n", q[0]);
Sergunb 0:8918a71cdbe9 528
Sergunb 0:8918a71cdbe9 529 //Check the length of the Password field
Sergunb 0:8918a71cdbe9 530 if(length < (sizeof(PapAuthAckPacket) + 1 + p->peerIdLength + q[0]))
Sergunb 0:8918a71cdbe9 531 return ERROR_INVALID_LENGTH;
Sergunb 0:8918a71cdbe9 532
Sergunb 0:8918a71cdbe9 533 //Password field
Sergunb 0:8918a71cdbe9 534 TRACE_DEBUG(" Password\r\n");
Sergunb 0:8918a71cdbe9 535 TRACE_DEBUG_ARRAY(" ", q + 1, q[0]);
Sergunb 0:8918a71cdbe9 536 }
Sergunb 0:8918a71cdbe9 537 //Authenticate-Ack or Authenticate-Nak packet?
Sergunb 0:8918a71cdbe9 538 else if(packet->code == PAP_CODE_AUTH_ACK ||
Sergunb 0:8918a71cdbe9 539 packet->code == PAP_CODE_AUTH_NAK)
Sergunb 0:8918a71cdbe9 540 {
Sergunb 0:8918a71cdbe9 541 PapAuthAckPacket *p;
Sergunb 0:8918a71cdbe9 542
Sergunb 0:8918a71cdbe9 543 //Cast PAP packet
Sergunb 0:8918a71cdbe9 544 p = (PapAuthAckPacket *) packet;
Sergunb 0:8918a71cdbe9 545
Sergunb 0:8918a71cdbe9 546 //Valid packet length?
Sergunb 0:8918a71cdbe9 547 if(length < sizeof(PapAuthAckPacket))
Sergunb 0:8918a71cdbe9 548 return ERROR_INVALID_LENGTH;
Sergunb 0:8918a71cdbe9 549
Sergunb 0:8918a71cdbe9 550 //Msg-Length field
Sergunb 0:8918a71cdbe9 551 TRACE_DEBUG(" Msg-Length = %" PRIu8 "\r\n", p->msgLength);
Sergunb 0:8918a71cdbe9 552
Sergunb 0:8918a71cdbe9 553 //Check the length of the Message field
Sergunb 0:8918a71cdbe9 554 if(length < (sizeof(PapAuthAckPacket) + p->msgLength))
Sergunb 0:8918a71cdbe9 555 return ERROR_INVALID_LENGTH;
Sergunb 0:8918a71cdbe9 556
Sergunb 0:8918a71cdbe9 557 if(length > 0)
Sergunb 0:8918a71cdbe9 558 {
Sergunb 0:8918a71cdbe9 559 //Message field
Sergunb 0:8918a71cdbe9 560 TRACE_DEBUG(" Message\r\n");
Sergunb 0:8918a71cdbe9 561 TRACE_DEBUG_ARRAY(" ", p->message, p->msgLength);
Sergunb 0:8918a71cdbe9 562 }
Sergunb 0:8918a71cdbe9 563 }
Sergunb 0:8918a71cdbe9 564 //Unknown packet?
Sergunb 0:8918a71cdbe9 565 else
Sergunb 0:8918a71cdbe9 566 {
Sergunb 0:8918a71cdbe9 567 //Retrieve the length of data
Sergunb 0:8918a71cdbe9 568 length -= sizeof(PppPacket);
Sergunb 0:8918a71cdbe9 569
Sergunb 0:8918a71cdbe9 570 //Any data?
Sergunb 0:8918a71cdbe9 571 if(length > 0)
Sergunb 0:8918a71cdbe9 572 {
Sergunb 0:8918a71cdbe9 573 //Dump data
Sergunb 0:8918a71cdbe9 574 TRACE_DEBUG(" Data (%" PRIuSIZE " bytes)\r\n", length);
Sergunb 0:8918a71cdbe9 575 TRACE_DEBUG_ARRAY(" ", packet->data, length);
Sergunb 0:8918a71cdbe9 576 }
Sergunb 0:8918a71cdbe9 577 }
Sergunb 0:8918a71cdbe9 578
Sergunb 0:8918a71cdbe9 579 //No error to report
Sergunb 0:8918a71cdbe9 580 return NO_ERROR;
Sergunb 0:8918a71cdbe9 581 }
Sergunb 0:8918a71cdbe9 582
Sergunb 0:8918a71cdbe9 583
Sergunb 0:8918a71cdbe9 584 /**
Sergunb 0:8918a71cdbe9 585 * @brief Dump CHAP packet for debugging purpose
Sergunb 0:8918a71cdbe9 586 * @param[in] packet Pointer to the PAP packet
Sergunb 0:8918a71cdbe9 587 * @param[in] length Length of the packet, in bytes
Sergunb 0:8918a71cdbe9 588 * @return Error code
Sergunb 0:8918a71cdbe9 589 **/
Sergunb 0:8918a71cdbe9 590
Sergunb 0:8918a71cdbe9 591 error_t chapDumpPacket(const PppPacket *packet, size_t length)
Sergunb 0:8918a71cdbe9 592 {
Sergunb 0:8918a71cdbe9 593 const char_t *label;
Sergunb 0:8918a71cdbe9 594
Sergunb 0:8918a71cdbe9 595 //Make sure the CHAP packet is valid
Sergunb 0:8918a71cdbe9 596 if(length < sizeof(PppPacket))
Sergunb 0:8918a71cdbe9 597 return ERROR_INVALID_LENGTH;
Sergunb 0:8918a71cdbe9 598
Sergunb 0:8918a71cdbe9 599 //Check the length field
Sergunb 0:8918a71cdbe9 600 if(ntohs(packet->length) > length)
Sergunb 0:8918a71cdbe9 601 return ERROR_INVALID_LENGTH;
Sergunb 0:8918a71cdbe9 602 if(ntohs(packet->length) < sizeof(PppPacket))
Sergunb 0:8918a71cdbe9 603 return ERROR_INVALID_LENGTH;
Sergunb 0:8918a71cdbe9 604
Sergunb 0:8918a71cdbe9 605 //Save the length of the CHAP packet
Sergunb 0:8918a71cdbe9 606 length = ntohs(packet->length);
Sergunb 0:8918a71cdbe9 607
Sergunb 0:8918a71cdbe9 608 //Retrieve the name of the CHAP packet
Sergunb 0:8918a71cdbe9 609 if(packet->code < arraysize(chapCodeLabel))
Sergunb 0:8918a71cdbe9 610 label = chapCodeLabel[packet->code];
Sergunb 0:8918a71cdbe9 611 else
Sergunb 0:8918a71cdbe9 612 label = "Unknown";
Sergunb 0:8918a71cdbe9 613
Sergunb 0:8918a71cdbe9 614 //Dump CHAP packet header
Sergunb 0:8918a71cdbe9 615 TRACE_DEBUG(" Code = %" PRIu8 " (%s)\r\n", packet->code, label);
Sergunb 0:8918a71cdbe9 616 TRACE_DEBUG(" Identifier = %" PRIu8 "\r\n", packet->identifier);
Sergunb 0:8918a71cdbe9 617 TRACE_DEBUG(" Length = %" PRIu16 "\r\n", ntohs(packet->length));
Sergunb 0:8918a71cdbe9 618
Sergunb 0:8918a71cdbe9 619 //Challenge or Response packet?
Sergunb 0:8918a71cdbe9 620 if(packet->code == CHAP_CODE_CHALLENGE ||
Sergunb 0:8918a71cdbe9 621 packet->code == CHAP_CODE_RESPONSE)
Sergunb 0:8918a71cdbe9 622 {
Sergunb 0:8918a71cdbe9 623 uint8_t *q;
Sergunb 0:8918a71cdbe9 624 ChapChallengePacket *p;
Sergunb 0:8918a71cdbe9 625
Sergunb 0:8918a71cdbe9 626 //Cast CHAP packet
Sergunb 0:8918a71cdbe9 627 p = (ChapChallengePacket *) packet;
Sergunb 0:8918a71cdbe9 628
Sergunb 0:8918a71cdbe9 629 //Valid packet length?
Sergunb 0:8918a71cdbe9 630 if(length < sizeof(ChapChallengePacket))
Sergunb 0:8918a71cdbe9 631 return ERROR_INVALID_LENGTH;
Sergunb 0:8918a71cdbe9 632
Sergunb 0:8918a71cdbe9 633 //Value-Size field
Sergunb 0:8918a71cdbe9 634 TRACE_DEBUG(" Value-Size = %" PRIu8 "\r\n", p->valueSize);
Sergunb 0:8918a71cdbe9 635
Sergunb 0:8918a71cdbe9 636 //Check the length of the Value field
Sergunb 0:8918a71cdbe9 637 if(length < (sizeof(ChapChallengePacket) + p->valueSize))
Sergunb 0:8918a71cdbe9 638 return ERROR_INVALID_LENGTH;
Sergunb 0:8918a71cdbe9 639
Sergunb 0:8918a71cdbe9 640 //Value field
Sergunb 0:8918a71cdbe9 641 TRACE_DEBUG(" Value\r\n");
Sergunb 0:8918a71cdbe9 642 TRACE_DEBUG_ARRAY(" ", p->value, p->valueSize);
Sergunb 0:8918a71cdbe9 643
Sergunb 0:8918a71cdbe9 644 //Point to the Name field
Sergunb 0:8918a71cdbe9 645 q = p->value + p->valueSize;
Sergunb 0:8918a71cdbe9 646 //Retrieve the length of the Name field
Sergunb 0:8918a71cdbe9 647 length -= sizeof(ChapChallengePacket) + p->valueSize;
Sergunb 0:8918a71cdbe9 648
Sergunb 0:8918a71cdbe9 649 //Name field
Sergunb 0:8918a71cdbe9 650 TRACE_DEBUG(" Name (%" PRIuSIZE " bytes)\r\n", length);
Sergunb 0:8918a71cdbe9 651 TRACE_DEBUG_ARRAY(" ", q, length);
Sergunb 0:8918a71cdbe9 652 }
Sergunb 0:8918a71cdbe9 653 //Success or Failure packet?
Sergunb 0:8918a71cdbe9 654 else if(packet->code == CHAP_CODE_SUCCESS ||
Sergunb 0:8918a71cdbe9 655 packet->code == CHAP_CODE_FAILURE)
Sergunb 0:8918a71cdbe9 656 {
Sergunb 0:8918a71cdbe9 657 ChapSuccessPacket *p;
Sergunb 0:8918a71cdbe9 658
Sergunb 0:8918a71cdbe9 659 //Cast CHAP packet
Sergunb 0:8918a71cdbe9 660 p = (ChapSuccessPacket *) packet;
Sergunb 0:8918a71cdbe9 661
Sergunb 0:8918a71cdbe9 662 //Valid packet length?
Sergunb 0:8918a71cdbe9 663 if(length < sizeof(ChapSuccessPacket))
Sergunb 0:8918a71cdbe9 664 return ERROR_INVALID_LENGTH;
Sergunb 0:8918a71cdbe9 665
Sergunb 0:8918a71cdbe9 666 //Retrieve the length of Message field
Sergunb 0:8918a71cdbe9 667 length -= sizeof(ChapSuccessPacket);
Sergunb 0:8918a71cdbe9 668
Sergunb 0:8918a71cdbe9 669 //Message field
Sergunb 0:8918a71cdbe9 670 TRACE_DEBUG(" Message (%" PRIuSIZE " bytes)\r\n", length);
Sergunb 0:8918a71cdbe9 671 TRACE_DEBUG_ARRAY(" ", p->message, length);
Sergunb 0:8918a71cdbe9 672 }
Sergunb 0:8918a71cdbe9 673 //Unknown packet?
Sergunb 0:8918a71cdbe9 674 else
Sergunb 0:8918a71cdbe9 675 {
Sergunb 0:8918a71cdbe9 676 //Retrieve the length of data
Sergunb 0:8918a71cdbe9 677 length -= sizeof(PppPacket);
Sergunb 0:8918a71cdbe9 678
Sergunb 0:8918a71cdbe9 679 //Any data?
Sergunb 0:8918a71cdbe9 680 if(length > 0)
Sergunb 0:8918a71cdbe9 681 {
Sergunb 0:8918a71cdbe9 682 //Dump data
Sergunb 0:8918a71cdbe9 683 TRACE_DEBUG(" Data (%" PRIuSIZE " bytes)\r\n", length);
Sergunb 0:8918a71cdbe9 684 TRACE_DEBUG_ARRAY(" ", packet->data, length);
Sergunb 0:8918a71cdbe9 685 }
Sergunb 0:8918a71cdbe9 686 }
Sergunb 0:8918a71cdbe9 687
Sergunb 0:8918a71cdbe9 688 //No error to report
Sergunb 0:8918a71cdbe9 689 return NO_ERROR;
Sergunb 0:8918a71cdbe9 690 }
Sergunb 0:8918a71cdbe9 691
Sergunb 0:8918a71cdbe9 692
Sergunb 0:8918a71cdbe9 693 /**
Sergunb 0:8918a71cdbe9 694 * @brief Dump LCP options for debugging purpose
Sergunb 0:8918a71cdbe9 695 * @param[in] option Pointer to the option list
Sergunb 0:8918a71cdbe9 696 * @param[in] length Length of the option list, in bytes
Sergunb 0:8918a71cdbe9 697 * @return Error code
Sergunb 0:8918a71cdbe9 698 **/
Sergunb 0:8918a71cdbe9 699
Sergunb 0:8918a71cdbe9 700 error_t lcpDumpOptions(const PppOption *option, size_t length)
Sergunb 0:8918a71cdbe9 701 {
Sergunb 0:8918a71cdbe9 702 uint32_t value;
Sergunb 0:8918a71cdbe9 703
Sergunb 0:8918a71cdbe9 704 //Parse options
Sergunb 0:8918a71cdbe9 705 while(length > 0)
Sergunb 0:8918a71cdbe9 706 {
Sergunb 0:8918a71cdbe9 707 //Malformed LCP packet?
Sergunb 0:8918a71cdbe9 708 if(length < sizeof(PppOption))
Sergunb 0:8918a71cdbe9 709 return ERROR_INVALID_LENGTH;
Sergunb 0:8918a71cdbe9 710
Sergunb 0:8918a71cdbe9 711 //Check option length
Sergunb 0:8918a71cdbe9 712 if(option->length < sizeof(PppOption))
Sergunb 0:8918a71cdbe9 713 return ERROR_INVALID_LENGTH;
Sergunb 0:8918a71cdbe9 714 if(option->length > length)
Sergunb 0:8918a71cdbe9 715 return ERROR_INVALID_LENGTH;
Sergunb 0:8918a71cdbe9 716
Sergunb 0:8918a71cdbe9 717 //Display the name of the current option
Sergunb 0:8918a71cdbe9 718 if(option->type < arraysize(lcpOptionLabel))
Sergunb 0:8918a71cdbe9 719 TRACE_DEBUG(" %s option (%" PRIu8 " bytes)\r\n", lcpOptionLabel[option->type], option->length);
Sergunb 0:8918a71cdbe9 720 else
Sergunb 0:8918a71cdbe9 721 TRACE_DEBUG(" Option %" PRIu8 " (%" PRIu8 " bytes)\r\n", option->type, option->length);
Sergunb 0:8918a71cdbe9 722
Sergunb 0:8918a71cdbe9 723 //Check option code
Sergunb 0:8918a71cdbe9 724 switch(option->type)
Sergunb 0:8918a71cdbe9 725 {
Sergunb 0:8918a71cdbe9 726 //16-bit unsigned value?
Sergunb 0:8918a71cdbe9 727 case LCP_OPTION_MRU:
Sergunb 0:8918a71cdbe9 728 //Check length field
Sergunb 0:8918a71cdbe9 729 if(option->length != (sizeof(PppOption) + sizeof(uint16_t)))
Sergunb 0:8918a71cdbe9 730 return ERROR_INVALID_OPTION;
Sergunb 0:8918a71cdbe9 731 //Retrieve 16-bit value
Sergunb 0:8918a71cdbe9 732 value = LOAD16BE(option->data);
Sergunb 0:8918a71cdbe9 733 //Dump option contents
Sergunb 0:8918a71cdbe9 734 TRACE_DEBUG(" %" PRIu32 "\r\n", value);
Sergunb 0:8918a71cdbe9 735 break;
Sergunb 0:8918a71cdbe9 736
Sergunb 0:8918a71cdbe9 737 //32-bit unsigned value?
Sergunb 0:8918a71cdbe9 738 case LCP_OPTION_ACCM:
Sergunb 0:8918a71cdbe9 739 case LCP_OPTION_MAGIC_NUMBER:
Sergunb 0:8918a71cdbe9 740 //Check length field
Sergunb 0:8918a71cdbe9 741 if(option->length != (sizeof(PppOption) + sizeof(uint32_t)))
Sergunb 0:8918a71cdbe9 742 return ERROR_INVALID_OPTION;
Sergunb 0:8918a71cdbe9 743 //Retrieve 32-bit value
Sergunb 0:8918a71cdbe9 744 value = LOAD32BE(option->data);
Sergunb 0:8918a71cdbe9 745 //Dump option contents
Sergunb 0:8918a71cdbe9 746 TRACE_DEBUG(" 0x%08" PRIX32 "\r\n", value);
Sergunb 0:8918a71cdbe9 747 break;
Sergunb 0:8918a71cdbe9 748
Sergunb 0:8918a71cdbe9 749 //Raw data?
Sergunb 0:8918a71cdbe9 750 default:
Sergunb 0:8918a71cdbe9 751 //Dump option contents
Sergunb 0:8918a71cdbe9 752 TRACE_DEBUG_ARRAY(" ", option->data, option->length - sizeof(PppOption));
Sergunb 0:8918a71cdbe9 753 break;
Sergunb 0:8918a71cdbe9 754 }
Sergunb 0:8918a71cdbe9 755
Sergunb 0:8918a71cdbe9 756 //Remaining bytes to process
Sergunb 0:8918a71cdbe9 757 length -= option->length;
Sergunb 0:8918a71cdbe9 758 //Jump to the next option
Sergunb 0:8918a71cdbe9 759 option = (PppOption *) ((uint8_t *) option + option->length);
Sergunb 0:8918a71cdbe9 760 }
Sergunb 0:8918a71cdbe9 761
Sergunb 0:8918a71cdbe9 762 //No error to report
Sergunb 0:8918a71cdbe9 763 return NO_ERROR;
Sergunb 0:8918a71cdbe9 764 }
Sergunb 0:8918a71cdbe9 765
Sergunb 0:8918a71cdbe9 766 /**
Sergunb 0:8918a71cdbe9 767 * @brief Dump IPCP options for debugging purpose
Sergunb 0:8918a71cdbe9 768 * @param[in] option Pointer to the option list
Sergunb 0:8918a71cdbe9 769 * @param[in] length Length of the option list, in bytes
Sergunb 0:8918a71cdbe9 770 * @return Error code
Sergunb 0:8918a71cdbe9 771 **/
Sergunb 0:8918a71cdbe9 772
Sergunb 0:8918a71cdbe9 773 error_t ipcpDumpOptions(const PppOption *option, size_t length)
Sergunb 0:8918a71cdbe9 774 {
Sergunb 0:8918a71cdbe9 775 #if (IPV4_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 776 Ipv4Addr ipAddr;
Sergunb 0:8918a71cdbe9 777 #endif
Sergunb 0:8918a71cdbe9 778
Sergunb 0:8918a71cdbe9 779 //Parse options
Sergunb 0:8918a71cdbe9 780 while(length > 0)
Sergunb 0:8918a71cdbe9 781 {
Sergunb 0:8918a71cdbe9 782 //Malformed IPCP packet?
Sergunb 0:8918a71cdbe9 783 if(length < sizeof(PppOption))
Sergunb 0:8918a71cdbe9 784 return ERROR_INVALID_LENGTH;
Sergunb 0:8918a71cdbe9 785
Sergunb 0:8918a71cdbe9 786 //Check option length
Sergunb 0:8918a71cdbe9 787 if(option->length < sizeof(PppOption))
Sergunb 0:8918a71cdbe9 788 return ERROR_INVALID_LENGTH;
Sergunb 0:8918a71cdbe9 789 if(option->length > length)
Sergunb 0:8918a71cdbe9 790 return ERROR_INVALID_LENGTH;
Sergunb 0:8918a71cdbe9 791
Sergunb 0:8918a71cdbe9 792 //Display the name of the current option
Sergunb 0:8918a71cdbe9 793 if(option->type < arraysize(ipcpOptionLabel))
Sergunb 0:8918a71cdbe9 794 TRACE_DEBUG(" %s option (%" PRIu8 " bytes)\r\n", ipcpOptionLabel[option->type], option->length);
Sergunb 0:8918a71cdbe9 795 else if(option->type >= 128 && option->type < (128 + arraysize(ipcpOptionLabel2)))
Sergunb 0:8918a71cdbe9 796 TRACE_DEBUG(" %s option (%" PRIu8 " bytes)\r\n", ipcpOptionLabel2[option->type - 128], option->length);
Sergunb 0:8918a71cdbe9 797 else
Sergunb 0:8918a71cdbe9 798 TRACE_DEBUG(" Option %" PRIu8 " (%" PRIu8 " bytes)\r\n", option->type, option->length);
Sergunb 0:8918a71cdbe9 799
Sergunb 0:8918a71cdbe9 800 //Check option code
Sergunb 0:8918a71cdbe9 801 switch(option->type)
Sergunb 0:8918a71cdbe9 802 {
Sergunb 0:8918a71cdbe9 803 #if (IPV4_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 804 //IP address?
Sergunb 0:8918a71cdbe9 805 case IPCP_OPTION_IP_ADDRESS:
Sergunb 0:8918a71cdbe9 806 case IPCP_OPTION_PRIMARY_DNS:
Sergunb 0:8918a71cdbe9 807 case IPCP_OPTION_PRIMARY_NBNS:
Sergunb 0:8918a71cdbe9 808 case IPCP_OPTION_SECONDARY_DNS:
Sergunb 0:8918a71cdbe9 809 case IPCP_OPTION_SECONDARY_NBNS:
Sergunb 0:8918a71cdbe9 810 //Check length field
Sergunb 0:8918a71cdbe9 811 if(option->length != (sizeof(PppOption) + sizeof(Ipv4Addr)))
Sergunb 0:8918a71cdbe9 812 return ERROR_INVALID_OPTION;
Sergunb 0:8918a71cdbe9 813 //Retrieve IPv4 address
Sergunb 0:8918a71cdbe9 814 ipv4CopyAddr(&ipAddr, option->data);
Sergunb 0:8918a71cdbe9 815 //Dump option contents
Sergunb 0:8918a71cdbe9 816 TRACE_DEBUG(" %s\r\n", ipv4AddrToString(ipAddr, NULL));
Sergunb 0:8918a71cdbe9 817 break;
Sergunb 0:8918a71cdbe9 818 #endif
Sergunb 0:8918a71cdbe9 819 //Raw data?
Sergunb 0:8918a71cdbe9 820 default:
Sergunb 0:8918a71cdbe9 821 //Dump option contents
Sergunb 0:8918a71cdbe9 822 TRACE_DEBUG_ARRAY(" ", option->data, option->length - sizeof(PppOption));
Sergunb 0:8918a71cdbe9 823 break;
Sergunb 0:8918a71cdbe9 824 }
Sergunb 0:8918a71cdbe9 825
Sergunb 0:8918a71cdbe9 826 //Remaining bytes to process
Sergunb 0:8918a71cdbe9 827 length -= option->length;
Sergunb 0:8918a71cdbe9 828 //Jump to the next option
Sergunb 0:8918a71cdbe9 829 option = (PppOption *) ((uint8_t *) option + option->length);
Sergunb 0:8918a71cdbe9 830 }
Sergunb 0:8918a71cdbe9 831
Sergunb 0:8918a71cdbe9 832 //No error to report
Sergunb 0:8918a71cdbe9 833 return NO_ERROR;
Sergunb 0:8918a71cdbe9 834 }
Sergunb 0:8918a71cdbe9 835
Sergunb 0:8918a71cdbe9 836
Sergunb 0:8918a71cdbe9 837 /**
Sergunb 0:8918a71cdbe9 838 * @brief Dump IPV6CP options for debugging purpose
Sergunb 0:8918a71cdbe9 839 * @param[in] option Pointer to the option list
Sergunb 0:8918a71cdbe9 840 * @param[in] length Length of the option list, in bytes
Sergunb 0:8918a71cdbe9 841 * @return Error code
Sergunb 0:8918a71cdbe9 842 **/
Sergunb 0:8918a71cdbe9 843
Sergunb 0:8918a71cdbe9 844 error_t ipv6cpDumpOptions(const PppOption *option, size_t length)
Sergunb 0:8918a71cdbe9 845 {
Sergunb 0:8918a71cdbe9 846 //Parse options
Sergunb 0:8918a71cdbe9 847 while(length > 0)
Sergunb 0:8918a71cdbe9 848 {
Sergunb 0:8918a71cdbe9 849 //Malformed IPV6CP packet?
Sergunb 0:8918a71cdbe9 850 if(length < sizeof(PppOption))
Sergunb 0:8918a71cdbe9 851 return ERROR_INVALID_LENGTH;
Sergunb 0:8918a71cdbe9 852
Sergunb 0:8918a71cdbe9 853 //Check option length
Sergunb 0:8918a71cdbe9 854 if(option->length < sizeof(PppOption))
Sergunb 0:8918a71cdbe9 855 return ERROR_INVALID_LENGTH;
Sergunb 0:8918a71cdbe9 856 if(option->length > length)
Sergunb 0:8918a71cdbe9 857 return ERROR_INVALID_LENGTH;
Sergunb 0:8918a71cdbe9 858
Sergunb 0:8918a71cdbe9 859 //Display the name of the current option
Sergunb 0:8918a71cdbe9 860 if(option->type < arraysize(ipv6cpOptionLabel))
Sergunb 0:8918a71cdbe9 861 TRACE_DEBUG(" %s option (%" PRIu8 " bytes)\r\n", ipv6cpOptionLabel[option->type], option->length);
Sergunb 0:8918a71cdbe9 862 else
Sergunb 0:8918a71cdbe9 863 TRACE_DEBUG(" Option %" PRIu8 " (%" PRIu8 " bytes)\r\n", option->type, option->length);
Sergunb 0:8918a71cdbe9 864
Sergunb 0:8918a71cdbe9 865 //Check option code
Sergunb 0:8918a71cdbe9 866 switch(option->type)
Sergunb 0:8918a71cdbe9 867 {
Sergunb 0:8918a71cdbe9 868 //Raw data?
Sergunb 0:8918a71cdbe9 869 default:
Sergunb 0:8918a71cdbe9 870 //Dump option contents
Sergunb 0:8918a71cdbe9 871 TRACE_DEBUG_ARRAY(" ", option->data, option->length - sizeof(PppOption));
Sergunb 0:8918a71cdbe9 872 break;
Sergunb 0:8918a71cdbe9 873 }
Sergunb 0:8918a71cdbe9 874
Sergunb 0:8918a71cdbe9 875 //Remaining bytes to process
Sergunb 0:8918a71cdbe9 876 length -= option->length;
Sergunb 0:8918a71cdbe9 877 //Jump to the next option
Sergunb 0:8918a71cdbe9 878 option = (PppOption *) ((uint8_t *) option + option->length);
Sergunb 0:8918a71cdbe9 879 }
Sergunb 0:8918a71cdbe9 880
Sergunb 0:8918a71cdbe9 881 //No error to report
Sergunb 0:8918a71cdbe9 882 return NO_ERROR;
Sergunb 0:8918a71cdbe9 883 }
Sergunb 0:8918a71cdbe9 884
Sergunb 0:8918a71cdbe9 885 #endif
Sergunb 0:8918a71cdbe9 886