Webserver+3d print

Dependents:   Nucleo

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ppp.h Source File

ppp.h

Go to the documentation of this file.
00001 /**
00002  * @file ppp.h
00003  * @brief PPP (Point-to-Point Protocol)
00004  *
00005  * @section License
00006  *
00007  * Copyright (C) 2010-2017 Oryx Embedded SARL. All rights reserved.
00008  *
00009  * This file is part of CycloneTCP Open.
00010  *
00011  * This program is free software; you can redistribute it and/or
00012  * modify it under the terms of the GNU General Public License
00013  * as published by the Free Software Foundation; either version 2
00014  * of the License, or (at your option) any later version.
00015  *
00016  * This program is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU General Public License
00022  * along with this program; if not, write to the Free Software Foundation,
00023  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00024  *
00025  * @author Oryx Embedded SARL (www.oryx-embedded.com)
00026  * @version 1.7.6
00027  **/
00028 
00029 #ifndef _PPP_H
00030 #define _PPP_H
00031 
00032 //Forward declaration of structures
00033 struct _PppPacket;
00034 struct _PppContext;
00035 #define PppPacket struct _PppPacket
00036 #define PppContext struct _PppContext
00037 
00038 //Dependencies
00039 #include "core/net.h"
00040 #include "ppp/pap.h"
00041 #include "ppp/chap.h"
00042 
00043 //PPP support
00044 #ifndef PPP_SUPPORT
00045    #define PPP_SUPPORT DISABLED
00046 #elif (PPP_SUPPORT != ENABLED && PPP_SUPPORT != DISABLED)
00047    #error PPP_SUPPORT parameter is not valid
00048 #endif
00049 
00050 //TX buffer size
00051 #ifndef PPP_TX_BUFFER_SIZE
00052    #define PPP_TX_BUFFER_SIZE 4096
00053 #elif (PPP_TX_BUFFER_SIZE < 3006)
00054    #error PPP_TX_BUFFER_SIZE parameter is not valid
00055 #endif
00056 
00057 //RX buffer size
00058 #ifndef PPP_RX_BUFFER_SIZE
00059    #define PPP_RX_BUFFER_SIZE 8192
00060 #elif (PPP_RX_BUFFER_SIZE < 3006)
00061    #error PPP_RX_BUFFER_SIZE parameter is not valid
00062 #endif
00063 
00064 //Maximum user name length
00065 #ifndef PPP_MAX_USERNAME_LEN
00066    #define PPP_MAX_USERNAME_LEN 31
00067 #elif (PPP_MAX_USERNAME_LEN < 7)
00068    #error PPP_MAX_USERNAME_LEN parameter is not valid
00069 #endif
00070 
00071 //Maximum password length
00072 #ifndef PPP_MAX_PASSWORD_LEN
00073    #define PPP_MAX_PASSWORD_LEN 31
00074 #elif (PPP_MAX_PASSWORD_LEN < 7)
00075    #error PPP_MAX_PASSWORD_LEN parameter is not valid
00076 #endif
00077 
00078 //PPP tick interval
00079 #ifndef PPP_TICK_INTERVAL
00080    #define PPP_TICK_INTERVAL 500
00081 #elif (PPP_TICK_INTERVAL < 10)
00082    #error PPP_TICK_INTERVAL parameter is not valid
00083 #endif
00084 
00085 //Polling interval for blocking functions
00086 #ifndef PPP_POLLING_INTERVAL
00087    #define PPP_POLLING_INTERVAL 50
00088 #elif (PPP_POLLING_INTERVAL < 1)
00089    #error PPP_POLLING_INTERVAL parameter is not valid
00090 #endif
00091 
00092 //Restart timer
00093 #ifndef PPP_RESTART_TIMER
00094    #define PPP_RESTART_TIMER 3000
00095 #elif (PPP_RESTART_TIMER < 1000)
00096    #error PPP_RESTART_TIMER parameter is not valid
00097 #endif
00098 
00099 //Maximum number of retransmissions for Configure-Requests
00100 #ifndef PPP_MAX_CONFIGURE
00101    #define PPP_MAX_CONFIGURE 10
00102 #elif (PPP_MAX_CONFIGURE < 1)
00103    #error PPP_MAX_CONFIGURE parameter is not valid
00104 #endif
00105 
00106 //Maximum number of retransmissions for Terminate-Requests
00107 #ifndef PPP_MAX_TERMINATE
00108    #define PPP_MAX_TERMINATE 2
00109 #elif (PPP_MAX_TERMINATE < 1)
00110    #error PPP_MAX_TERMINATE parameter is not valid
00111 #endif
00112 
00113 //Maximum number of Configure-Nak packets sent
00114 #ifndef PPP_MAX_FAILURE
00115    #define PPP_MAX_FAILURE 5
00116 #elif (PPP_MAX_FAILURE < 1)
00117    #error PPP_MAX_FAILURE parameter is not valid
00118 #endif
00119 
00120 //PPP special characters
00121 #define PPP_MASK_CHAR 0x20
00122 #define PPP_ESC_CHAR  0x7D
00123 #define PPP_FLAG_CHAR 0x7E
00124 
00125 //PPP default MRU
00126 #define PPP_DEFAULT_MRU 1500
00127 //PPP default async control character map
00128 #define PPP_DEFAULT_ACCM 0xFFFFFFFF
00129 //PPP default magic number
00130 #define PPP_DEFAULT_MAGIC_NUMBER 0
00131 
00132 //Minimum acceptable value for MRU
00133 #define PPP_MIN_MRU 32
00134 //Maximum acceptable value for MRU
00135 #define PPP_MAX_MRU 1500
00136 
00137 //Maximum size of Configure-Request packets
00138 #define PPP_MAX_CONF_REQ_SIZE 128
00139 
00140 //Maximum size of PPP frame header
00141 #define PPP_FRAME_HEADER_SIZE 4
00142 //FCS field size
00143 #define PPP_FCS_SIZE 2
00144 //Maximum size of PPP frames
00145 #define PPP_MAX_FRAME_SIZE (PPP_FRAME_HEADER_SIZE + PPP_MAX_MRU + PPP_FCS_SIZE)
00146 
00147 //PPP Address field
00148 #define PPP_ADDR_FIELD 0xFF
00149 ///PPP Control field
00150 #define PPP_CTRL_FIELD 0x03
00151 
00152 
00153 /**
00154  * @brief PPP phases
00155  **/
00156 
00157 typedef enum
00158 {
00159    PPP_PHASE_DEAD         = 0, ///<Link dead
00160    PPP_PHASE_ESTABLISH    = 1, ///<Link establishment phase
00161    PPP_PHASE_AUTHENTICATE = 2, ///<Authentication phase
00162    PPP_PHASE_NETWORK      = 3, ///<Network-layer protocol phase
00163    PPP_PHASE_TERMINATE    = 4  ///<Link termination phase
00164 } PppPhase;
00165 
00166 
00167 /**
00168  * @brief LCP/NCP states
00169  **/
00170 
00171 typedef enum
00172 {
00173    PPP_STATE_0_INITIAL  = 0,
00174    PPP_STATE_1_STARTING = 1,
00175    PPP_STATE_2_CLOSED   = 2,
00176    PPP_STATE_3_STOPPED  = 3,
00177    PPP_STATE_4_CLOSING  = 4,
00178    PPP_STATE_5_STOPPING = 5,
00179    PPP_STATE_6_REQ_SENT = 6,
00180    PPP_STATE_7_ACK_RCVD = 7,
00181    PPP_STATE_8_ACK_SENT = 8,
00182    PPP_STATE_9_OPENED   = 9
00183 } PppState;
00184 
00185 
00186 /**
00187  * @brief Protocol field values
00188  **/
00189 
00190 typedef enum
00191 {
00192    PPP_PROTOCOL_IP     = 0x0021, ///<Internet Protocol
00193    PPP_PROTOCOL_IPV6   = 0x0057, ///<Internet Protocol version 6
00194    PPP_PROTOCOL_IPCP   = 0x8021, ///<IP Control Protocol
00195    PPP_PROTOCOL_IPV6CP = 0x8057, ///<IPv6 Control Protocol
00196    PPP_PROTOCOL_LCP    = 0xC021, ///<Link Control Protocol
00197    PPP_PROTOCOL_PAP    = 0xC023, ///<Password Authentication Protocol
00198    PPP_PROTOCOL_LQR    = 0xC025, ///<Link Quality Report
00199    PPP_PROTOCOL_CHAP   = 0xC223  ///<Challenge Handshake Authentication Protocol
00200 } PppProtocol;
00201 
00202 
00203 /**
00204  * @brief Code field values
00205  **/
00206 
00207 typedef enum
00208 {
00209    PPP_CODE_CONFIGURE_REQ = 1,  ///<Configure-Request
00210    PPP_CODE_CONFIGURE_ACK = 2,  ///<Configure-Ack
00211    PPP_CODE_CONFIGURE_NAK = 3,  ///<Configure-Nak
00212    PPP_CODE_CONFIGURE_REJ = 4,  ///<Configure-Reject
00213    PPP_CODE_TERMINATE_REQ = 5,  ///<Terminate-Request
00214    PPP_CODE_TERMINATE_ACK = 6,  ///<Terminate-Ack
00215    PPP_CODE_CODE_REJ      = 7,  ///<Code-Reject
00216    PPP_CODE_PROTOCOL_REJ  = 8,  ///<Protocol-Reject
00217    PPP_CODE_ECHO_REQ      = 9,  ///<Echo-Request
00218    PPP_CODE_ECHO_REP      = 10, ///<Echo-Reply
00219    PPP_CODE_DISCARD_REQ   = 11  ///<Discard-Request
00220 } PppCode;
00221 
00222 
00223 /**
00224  * @brief PPP authentication protocols
00225  **/
00226 
00227 typedef enum
00228 {
00229    PPP_AUTH_PROTOCOL_PAP       = 0x01, //PAP
00230    PPP_AUTH_PROTOCOL_CHAP_MD5  = 0x02, //CHAP with MD5
00231    PPP_AUTH_PROTOCOL_MS_CHAP   = 0x04, //MS-CHAP
00232    PPP_AUTH_PROTOCOL_MS_CHAP_2 = 0x08  //MS-CHAP-2
00233 } PppAuthProtocol;
00234 
00235 
00236 //CodeWarrior or Win32 compiler?
00237 #if defined(__CWCC__) || defined(_WIN32)
00238    #pragma pack(push, 1)
00239 #endif
00240 
00241 
00242 /**
00243  * @brief LCP/NCP packet header
00244  **/
00245 
00246 __start_packed struct _PppPacket
00247 {
00248    uint8_t code;       //0
00249    uint8_t identifier; //1
00250    uint16_t length;    //2-3
00251    uint8_t data[];     //4
00252 } __end_packed;
00253 
00254 
00255 /**
00256  * @brief Configure-Request, Configure-Ack, Configure-Nak and Configure-Reject packets
00257  **/
00258 
00259 typedef __start_packed struct
00260 {
00261    uint8_t code;       //0
00262    uint8_t identifier; //1
00263    uint16_t length;    //2-3
00264    uint8_t options[];  //4
00265 } __end_packed PppConfigurePacket;
00266 
00267 
00268 /**
00269  * @brief Terminate-Request and Terminate-Ack packet
00270  **/
00271 
00272 typedef __start_packed struct
00273 {
00274    uint8_t code;       //0
00275    uint8_t identifier; //1
00276    uint16_t length;    //2-3
00277    uint8_t data[];     //4
00278 } __end_packed PppTerminatePacket;
00279 
00280 
00281 /**
00282  * @brief Code-Reject packet
00283  **/
00284 
00285 typedef __start_packed struct
00286 {
00287    uint8_t code;             //0
00288    uint8_t identifier;       //1
00289    uint16_t length;          //2-3
00290    uint8_t rejectedPacket[]; //4
00291 } __end_packed PppCodeRejPacket;
00292 
00293 
00294 /**
00295  * @brief Protocol-Reject packet
00296  **/
00297 
00298 typedef __start_packed struct
00299 {
00300    uint8_t code;              //0
00301    uint8_t identifier;        //1
00302    uint16_t length;           //2-3
00303    uint16_t rejectedProtocol; //4-5
00304    uint8_t rejectedInfo[];    //6
00305 } __end_packed PppProtocolRejPacket;
00306 
00307 
00308 /**
00309  * @brief Echo-Request and Echo-Reply packet
00310  **/
00311 
00312 typedef __start_packed struct
00313 {
00314    uint8_t code;         //0
00315    uint8_t identifier;   //1
00316    uint16_t length;      //2-3
00317    uint32_t magicNumber; //4-7
00318    uint8_t data[];       //8
00319 } __end_packed PppEchoPacket;
00320 
00321 
00322 /**
00323  * @brief PPP Discard-Request packet
00324  **/
00325 
00326 typedef __start_packed struct
00327 {
00328    uint8_t code;         //0
00329    uint8_t identifier;   //1
00330    uint16_t length;      //2-3
00331    uint32_t magicNumber; //4-7
00332    uint8_t data[];       //8
00333 } __end_packed PppDiscardReqPacket;
00334 
00335 
00336 /**
00337  * @brief LCP/NCP option
00338  **/
00339 
00340 typedef __start_packed struct
00341 {
00342    uint8_t type;   //0
00343    uint8_t length; //1
00344    uint8_t data[]; //2
00345 } __end_packed PppOption;
00346 
00347 
00348 //CodeWarrior or Win32 compiler?
00349 #if defined(__CWCC__) || defined(_WIN32)
00350    #pragma pack(pop)
00351 #endif
00352 
00353 
00354 /**
00355  * @brief Random data generation callback function
00356  **/
00357 
00358 typedef error_t (*PppRandCallback)(uint8_t *data, size_t length);
00359 
00360 
00361 /**
00362  * @brief PPP authentication callback function
00363  **/
00364 
00365 typedef bool_t (*PppAuthCallback)(NetInterface *interface,
00366    const char_t *username);
00367 
00368 
00369 /**
00370  * @brief PPP settings
00371  **/
00372 
00373 typedef struct
00374 {
00375    NetInterface *interface;      ///<Underlying network interface
00376    uint16_t mru;                 ///<Default MRU
00377    uint32_t accm;                ///<Default async control character map
00378    uint_t authProtocol;          ///<Allowed authentication protocols
00379    PppRandCallback randCallback; ///<Random data generation callback function
00380    PppAuthCallback authCallback; ///<PPP authentication callback function
00381 } PppSettings;
00382 
00383 
00384 /**
00385  * @brief PPP finite state machine
00386  **/
00387 
00388 typedef struct
00389 {
00390    uint_t state;          ///<FSM state
00391    uint8_t identifier;    ///<Identifier used to match requests and replies
00392    uint_t restartCounter; ///<Restart counter
00393    uint_t failureCounter; ///<Failure counter
00394    systime_t timestamp;   ///<Timestamp to manage retransmissions
00395 } PppFsm;
00396 
00397 
00398 /**
00399  * @brief PPP configuration options
00400  **/
00401 
00402 typedef struct
00403 {
00404    uint16_t mru;
00405    bool_t mruRejected;
00406    uint32_t accm;
00407    bool_t accmRejected;
00408    uint16_t authProtocol;
00409    uint8_t authAlgo;
00410    bool_t authProtocolRejected;
00411    uint32_t magicNumber;
00412    bool_t magicNumberRejected;
00413    bool_t pfc;
00414    bool_t pfcRejected;
00415    bool_t acfc;
00416    bool_t acfcRejected;
00417 #if (IPV4_SUPPORT == ENABLED)
00418    Ipv4Addr ipAddr;
00419    bool_t ipAddrRejected;
00420    Ipv4Addr primaryDns;
00421    bool_t primaryDnsRejected;
00422    Ipv4Addr secondaryDns;
00423    bool_t secondaryDnsRejected;
00424 #endif
00425 #if (IPV6_SUPPORT == ENABLED)
00426    Eui64 interfaceId;
00427    bool_t interfaceIdRejected;
00428 #endif
00429 } PppConfig;
00430 
00431 
00432 /**
00433  * @brief PPP context
00434  **/
00435 
00436 struct _PppContext
00437 {
00438    PppSettings settings;    ///PPP settings
00439    NetInterface *interface; ///<Underlying network interface
00440    systime_t timeout;       ///<Timeout for blocking operations
00441 
00442    char_t username[PPP_MAX_USERNAME_LEN + 1]; ///<User name
00443    char_t password[PPP_MAX_PASSWORD_LEN + 1]; ///<Password
00444    char_t peerName[PPP_MAX_USERNAME_LEN + 1]; ///<Peer's name
00445 
00446    bool_t localAuthDone;
00447    bool_t peerAuthDone;
00448 
00449    PppPhase pppPhase;       ///<PPP phase
00450    PppFsm lcpFsm;           ///<LCP finite state machine
00451 #if (IPV4_SUPPORT == ENABLED)
00452    PppFsm ipcpFsm;          ///<IPCP finite state machine
00453 #endif
00454 #if (IPV6_SUPPORT == ENABLED)
00455    PppFsm ipv6cpFsm;        ///<IPV6CP finite state machine
00456 #endif
00457 #if (PAP_SUPPORT == ENABLED)
00458    PapFsm papFsm;           ///<PAP finite state machine
00459 #endif
00460 #if (CHAP_SUPPORT == ENABLED)
00461    ChapFsm chapFsm;         ///<CHAP finite state machine
00462 #endif
00463    PppConfig localConfig;   ///<Local configuration options
00464    PppConfig peerConfig;    ///<Peer configuration options
00465    bool_t ipRejected;       ///<IPv4 protocol is not supported by the peer
00466    bool_t ipv6Rejected;     ///<IPv6 protocol is not support by the peer
00467 
00468    uint8_t frame[PPP_MAX_FRAME_SIZE]; ///<Incoming PPP frame
00469 
00470    uint8_t txBuffer[PPP_TX_BUFFER_SIZE]; ///<Transmit buffer
00471    uint_t txBufferLen;
00472    uint_t txWriteIndex;
00473    uint_t txReadIndex;
00474 
00475    uint8_t rxBuffer[PPP_RX_BUFFER_SIZE]; ///<Receive buffer
00476    uint_t rxBufferLen;
00477    uint_t rxWriteIndex;
00478    uint_t rxReadIndex;
00479    uint_t rxFrameCount;
00480 };
00481 
00482 
00483 //Tick counter to handle periodic operations
00484 extern systime_t pppTickCounter;
00485 
00486 //PPP related functions
00487 void pppGetDefaultSettings(PppSettings *settings);
00488 error_t pppInit(PppContext *context, const PppSettings *settings);
00489 
00490 error_t pppSetTimeout(NetInterface *interface, systime_t timeout);
00491 
00492 error_t pppSetAuthInfo(NetInterface *interface,
00493    const char_t *username, const char_t *password);
00494 
00495 bool_t pppCheckPassword(NetInterface *interface, const char_t *password);
00496 
00497 error_t pppSendAtCommand(NetInterface *interface, const char_t *data);
00498 error_t pppReceiveAtCommand(NetInterface *interface, char_t *data, size_t size);
00499 
00500 error_t pppConnect(NetInterface *interface);
00501 error_t pppClose(NetInterface *interface);
00502 
00503 void pppTick(NetInterface *interface);
00504 
00505 void pppProcessFrame(NetInterface *interface, uint8_t *frame, size_t length);
00506 
00507 error_t pppSendFrame(NetInterface *interface,
00508    NetBuffer *buffer, size_t offset, uint16_t protocol);
00509 
00510 size_t pppParseFrameHeader(const uint8_t *frame, size_t length, uint16_t *protocol);
00511 
00512 uint16_t pppCalcFcs(const uint8_t *data, size_t length);
00513 uint16_t pppCalcFcsEx(const NetBuffer *buffer, size_t offset, size_t length);
00514 
00515 NetBuffer *pppAllocBuffer(size_t length, size_t *offset);
00516 
00517 #endif
00518