mikroSDK Reference Manual
ppp.h
Go to the documentation of this file.
1
31#ifndef _PPP_H
32#define _PPP_H
33
34//Forward declaration of structures
35struct _PppPacket;
36struct _PppContext;
37#define PppPacket struct _PppPacket
38#define PppContext struct _PppContext
39
40//Dependencies
41#include "core/net.h"
42#include "ppp/pap.h"
43#include "ppp/chap.h"
44
45//PPP support
46#ifndef PPP_SUPPORT
47 #define PPP_SUPPORT DISABLED
48#elif (PPP_SUPPORT != ENABLED && PPP_SUPPORT != DISABLED)
49 #error PPP_SUPPORT parameter is not valid
50#endif
51
52//TX buffer size
53#ifndef PPP_TX_BUFFER_SIZE
54 #define PPP_TX_BUFFER_SIZE 4096
55#elif (PPP_TX_BUFFER_SIZE < 3006)
56 #error PPP_TX_BUFFER_SIZE parameter is not valid
57#endif
58
59//RX buffer size
60#ifndef PPP_RX_BUFFER_SIZE
61 #define PPP_RX_BUFFER_SIZE 8192
62#elif (PPP_RX_BUFFER_SIZE < 3006)
63 #error PPP_RX_BUFFER_SIZE parameter is not valid
64#endif
65
66//Maximum user name length
67#ifndef PPP_MAX_USERNAME_LEN
68 #define PPP_MAX_USERNAME_LEN 31
69#elif (PPP_MAX_USERNAME_LEN < 7)
70 #error PPP_MAX_USERNAME_LEN parameter is not valid
71#endif
72
73//Maximum password length
74#ifndef PPP_MAX_PASSWORD_LEN
75 #define PPP_MAX_PASSWORD_LEN 31
76#elif (PPP_MAX_PASSWORD_LEN < 7)
77 #error PPP_MAX_PASSWORD_LEN parameter is not valid
78#endif
79
80//PPP tick interval
81#ifndef PPP_TICK_INTERVAL
82 #define PPP_TICK_INTERVAL 500
83#elif (PPP_TICK_INTERVAL < 10)
84 #error PPP_TICK_INTERVAL parameter is not valid
85#endif
86
87//Polling interval for blocking functions
88#ifndef PPP_POLLING_INTERVAL
89 #define PPP_POLLING_INTERVAL 50
90#elif (PPP_POLLING_INTERVAL < 1)
91 #error PPP_POLLING_INTERVAL parameter is not valid
92#endif
93
94//Restart timer
95#ifndef PPP_RESTART_TIMER
96 #define PPP_RESTART_TIMER 3000
97#elif (PPP_RESTART_TIMER < 1000)
98 #error PPP_RESTART_TIMER parameter is not valid
99#endif
100
101//Maximum number of retransmissions for Configure-Requests
102#ifndef PPP_MAX_CONFIGURE
103 #define PPP_MAX_CONFIGURE 10
104#elif (PPP_MAX_CONFIGURE < 1)
105 #error PPP_MAX_CONFIGURE parameter is not valid
106#endif
107
108//Maximum number of retransmissions for Terminate-Requests
109#ifndef PPP_MAX_TERMINATE
110 #define PPP_MAX_TERMINATE 2
111#elif (PPP_MAX_TERMINATE < 1)
112 #error PPP_MAX_TERMINATE parameter is not valid
113#endif
114
115//Maximum number of Configure-Nak packets sent
116#ifndef PPP_MAX_FAILURE
117 #define PPP_MAX_FAILURE 5
118#elif (PPP_MAX_FAILURE < 1)
119 #error PPP_MAX_FAILURE parameter is not valid
120#endif
121
122//PPP special characters
123#define PPP_MASK_CHAR 0x20
124#define PPP_ESC_CHAR 0x7D
125#define PPP_FLAG_CHAR 0x7E
126
127//PPP default MRU
128#define PPP_DEFAULT_MRU 1500
129//PPP default async control character map
130#define PPP_DEFAULT_ACCM 0xFFFFFFFF
131//PPP default magic number
132#define PPP_DEFAULT_MAGIC_NUMBER 0
133
134//Minimum acceptable value for MRU
135#define PPP_MIN_MRU 32
136//Maximum acceptable value for MRU
137#define PPP_MAX_MRU 1500
138
139//Maximum size of Configure-Request packets
140#define PPP_MAX_CONF_REQ_SIZE 128
141
142//Maximum size of PPP frame header
143#define PPP_FRAME_HEADER_SIZE 4
144//FCS field size
145#define PPP_FCS_SIZE 2
146//Maximum size of PPP frames
147#define PPP_MAX_FRAME_SIZE (PPP_FRAME_HEADER_SIZE + PPP_MAX_MRU + PPP_FCS_SIZE)
148
149//PPP Address field
150#define PPP_ADDR_FIELD 0xFF
152#define PPP_CTRL_FIELD 0x03
153
154//C++ guard
155#ifdef __cplusplus
156extern "C" {
157#endif
158
159
172
173
178typedef enum
179{
180 PPP_STATE_0_INITIAL = 0,
181 PPP_STATE_1_STARTING = 1,
182 PPP_STATE_2_CLOSED = 2,
183 PPP_STATE_3_STOPPED = 3,
184 PPP_STATE_4_CLOSING = 4,
185 PPP_STATE_5_STOPPING = 5,
186 PPP_STATE_6_REQ_SENT = 6,
187 PPP_STATE_7_ACK_RCVD = 7,
188 PPP_STATE_8_ACK_SENT = 8,
189 PPP_STATE_9_OPENED = 9
190} PppState;
191
192
208
209
228
229
234typedef enum
235{
236 PPP_AUTH_PROTOCOL_PAP = 0x01, //PAP
237 PPP_AUTH_PROTOCOL_CHAP_MD5 = 0x02, //CHAP with MD5
238 PPP_AUTH_PROTOCOL_MS_CHAP = 0x04, //MS-CHAP
239 PPP_AUTH_PROTOCOL_MS_CHAP_2 = 0x08 //MS-CHAP-2
241
242
243//CodeWarrior or Win32 compiler?
244#if defined(__CWCC__) || defined(_WIN32)
245 #pragma pack(push, 1)
246#endif
247
248
254{
255 uint8_t code; //0
256 uint8_t identifier; //1
257 uint16_t length; //2-3
258 uint8_t data[]; //4
259};
260
261
267{
268 uint8_t code; //0
269 uint8_t identifier; //1
270 uint16_t length; //2-3
271 uint8_t options[]; //4
272} PppConfigurePacket;
273
274
279typedef __packed_struct
280{
281 uint8_t code; //0
282 uint8_t identifier; //1
283 uint16_t length; //2-3
284 uint8_t data[]; //4
285} PppTerminatePacket;
286
287
292typedef __packed_struct
293{
294 uint8_t code; //0
295 uint8_t identifier; //1
296 uint16_t length; //2-3
297 uint8_t rejectedPacket[]; //4
298} PppCodeRejPacket;
299
300
305typedef __packed_struct
306{
307 uint8_t code; //0
308 uint8_t identifier; //1
309 uint16_t length; //2-3
310 uint16_t rejectedProtocol; //4-5
311 uint8_t rejectedInfo[]; //6
312} PppProtocolRejPacket;
313
314
319typedef __packed_struct
320{
321 uint8_t code; //0
322 uint8_t identifier; //1
323 uint16_t length; //2-3
324 uint32_t magicNumber; //4-7
325 uint8_t data[]; //8
326} PppEchoPacket;
327
328
333typedef __packed_struct
334{
335 uint8_t code; //0
336 uint8_t identifier; //1
337 uint16_t length; //2-3
338 uint32_t magicNumber; //4-7
339 uint8_t data[]; //8
340} PppDiscardReqPacket;
341
342
347typedef __packed_struct
348{
349 uint8_t type; //0
350 uint8_t length; //1
351 uint8_t data[]; //2
352} PppOption;
353
354
355//CodeWarrior or Win32 compiler?
356#if defined(__CWCC__) || defined(_WIN32)
357 #pragma pack(pop)
358#endif
359
360
365typedef error_t (*PppRandCallback)(uint8_t *data, size_t length);
366
367
372typedef bool_t (*PppAuthCallback)(NetInterface *interface,
373 const char_t *username);
374
375
389
390
395typedef struct
396{
397 uint_t state;
398 uint8_t identifier;
402} PppFsm;
403
404
409typedef struct
410{
411 uint16_t mru;
412 bool_t mruRejected;
413 uint32_t accm;
414 bool_t accmRejected;
415 uint16_t authProtocol;
416 uint8_t authAlgo;
417 bool_t authProtocolRejected;
418 uint32_t magicNumber;
419 bool_t magicNumberRejected;
420 bool_t pfc;
421 bool_t pfcRejected;
422 bool_t acfc;
423 bool_t acfcRejected;
424#if (IPV4_SUPPORT == ENABLED)
425 Ipv4Addr ipAddr;
426 bool_t ipAddrRejected;
427 Ipv4Addr primaryDns;
428 bool_t primaryDnsRejected;
429 Ipv4Addr secondaryDns;
430 bool_t secondaryDnsRejected;
431#endif
432#if (IPV6_SUPPORT == ENABLED)
433 Eui64 interfaceId;
434 bool_t interfaceIdRejected;
435#endif
436} PppConfig;
437
438
444{
445 PppSettings settings;
446 NetInterface *interface;
448
449 char_t username[PPP_MAX_USERNAME_LEN + 1];
450 char_t password[PPP_MAX_PASSWORD_LEN + 1];
451 char_t peerName[PPP_MAX_USERNAME_LEN + 1];
452
453 bool_t localAuthDone;
454 bool_t peerAuthDone;
455
458#if (IPV4_SUPPORT == ENABLED)
460#endif
461#if (IPV6_SUPPORT == ENABLED)
463#endif
464#if (PAP_SUPPORT == ENABLED)
466#endif
467#if (CHAP_SUPPORT == ENABLED)
469#endif
472 bool_t ipRejected;
474
475 uint8_t frame[PPP_MAX_FRAME_SIZE];
476
477 uint8_t txBuffer[PPP_TX_BUFFER_SIZE];
478 uint_t txBufferLen;
479 uint_t txWriteIndex;
480 uint_t txReadIndex;
481
482 uint8_t rxBuffer[PPP_RX_BUFFER_SIZE];
483 uint_t rxBufferLen;
484 uint_t rxWriteIndex;
485 uint_t rxReadIndex;
486 uint_t rxFrameCount;
487};
488
489
490//Tick counter to handle periodic operations
491extern systime_t pppTickCounter;
492
493//PPP related functions
494void pppGetDefaultSettings(PppSettings *settings);
495error_t pppInit(PppContext *context, const PppSettings *settings);
496
497error_t pppSetTimeout(NetInterface *interface, systime_t timeout);
498
499error_t pppSetAuthInfo(NetInterface *interface, const char_t *username,
500 const char_t *password);
501
502bool_t pppCheckPassword(NetInterface *interface, const char_t *password);
503
504error_t pppSendAtCommand(NetInterface *interface, const char_t *data);
505error_t pppReceiveAtCommand(NetInterface *interface, char_t *data, size_t size);
506
507error_t pppConnect(NetInterface *interface);
508error_t pppClose(NetInterface *interface);
509
510void pppTick(NetInterface *interface);
511
512void pppProcessFrame(NetInterface *interface, uint8_t *frame, size_t length,
513 NetRxAncillary *ancillary);
514
515error_t pppSendFrame(NetInterface *interface, NetBuffer *buffer, size_t offset,
516 uint16_t protocol);
517
518size_t pppParseFrameHeader(const uint8_t *frame, size_t length, uint16_t *protocol);
519
520uint16_t pppCalcFcs(const uint8_t *data, size_t length);
521uint16_t pppCalcFcsEx(const NetBuffer *buffer, size_t offset, size_t length);
522
523NetBuffer *pppAllocBuffer(size_t length, size_t *offset);
524
525//C++ guard
526#ifdef __cplusplus
527}
528#endif
529
530#endif
CHAP (Challenge Handshake Authentication Protocol)
error_t
Error codes.
Definition error.h:43
uint32_t Ipv4Addr
IPv4 network address.
Definition ipv4.h:267
TCP/IP stack core.
uint32_t systime_t
System time.
Definition os_port_none.h:90
PAP (Password Authentication Protocol)
error_t(* PppRandCallback)(uint8_t *data, size_t length)
Random data generation callback function.
Definition ppp.h:365
PppState
LCP/NCP states.
Definition ppp.h:179
PppProtocol
Protocol field values.
Definition ppp.h:198
@ PPP_PROTOCOL_IPCP
IP Control Protocol.
Definition ppp.h:201
@ PPP_PROTOCOL_LQR
Link Quality Report.
Definition ppp.h:205
@ PPP_PROTOCOL_LCP
Link Control Protocol.
Definition ppp.h:203
@ PPP_PROTOCOL_IP
Internet Protocol.
Definition ppp.h:199
@ PPP_PROTOCOL_IPV6CP
IPv6 Control Protocol.
Definition ppp.h:202
@ PPP_PROTOCOL_CHAP
Challenge Handshake Authentication Protocol.
Definition ppp.h:206
@ PPP_PROTOCOL_PAP
Password Authentication Protocol.
Definition ppp.h:204
@ PPP_PROTOCOL_IPV6
Internet Protocol version 6.
Definition ppp.h:200
PppAuthProtocol
PPP authentication protocols.
Definition ppp.h:235
PppCode
Code field values.
Definition ppp.h:215
@ PPP_CODE_ECHO_REP
Echo-Reply.
Definition ppp.h:225
@ PPP_CODE_CONFIGURE_REQ
Configure-Request.
Definition ppp.h:216
@ PPP_CODE_CONFIGURE_REJ
Configure-Reject.
Definition ppp.h:219
@ PPP_CODE_PROTOCOL_REJ
Protocol-Reject.
Definition ppp.h:223
@ PPP_CODE_TERMINATE_ACK
Terminate-Ack.
Definition ppp.h:221
@ PPP_CODE_TERMINATE_REQ
Terminate-Request.
Definition ppp.h:220
@ PPP_CODE_CODE_REJ
Code-Reject.
Definition ppp.h:222
@ PPP_CODE_DISCARD_REQ
Discard-Request.
Definition ppp.h:226
@ PPP_CODE_CONFIGURE_ACK
Configure-Ack.
Definition ppp.h:217
@ PPP_CODE_ECHO_REQ
Echo-Request.
Definition ppp.h:224
@ PPP_CODE_CONFIGURE_NAK
Configure-Nak.
Definition ppp.h:218
PppPhase
PPP phases.
Definition ppp.h:165
@ PPP_PHASE_TERMINATE
Link termination phase.
Definition ppp.h:170
@ PPP_PHASE_ESTABLISH
Link establishment phase.
Definition ppp.h:167
@ PPP_PHASE_AUTHENTICATE
Authentication phase.
Definition ppp.h:168
@ PPP_PHASE_DEAD
Link dead.
Definition ppp.h:166
@ PPP_PHASE_NETWORK
Network-layer protocol phase.
Definition ppp.h:169
bool_t(* PppAuthCallback)(NetInterface *interface, const char_t *username)
PPP authentication callback function.
Definition ppp.h:372
typedef __packed_struct
Configure-Request, Configure-Ack, Configure-Nak and Configure-Reject packets.
Definition ppp.h:267
__packed_struct _PppPacket
LCP/NCP packet header.
Definition ppp.h:254
CHAP finite state machine.
Definition chap.h:180
Structure describing a buffer that spans multiple chunks.
Definition net_mem.h:89
PAP finite state machine.
Definition pap.h:153
PPP configuration options.
Definition ppp.h:410
PPP finite state machine.
Definition ppp.h:396
uint_t restartCounter
Restart counter.
Definition ppp.h:399
systime_t timestamp
Timestamp to manage retransmissions.
Definition ppp.h:401
uint8_t identifier
Identifier used to match requests and replies.
Definition ppp.h:398
uint_t state
FSM state.
Definition ppp.h:397
uint_t failureCounter
Failure counter.
Definition ppp.h:400
PPP settings.
Definition ppp.h:381
PppRandCallback randCallback
Random data generation callback function.
Definition ppp.h:386
NetInterface * interface
Underlying network interface.
Definition ppp.h:382
PppAuthCallback authCallback
PPP authentication callback function.
Definition ppp.h:387
uint32_t accm
Default async control character map.
Definition ppp.h:384
uint16_t mru
Default MRU.
Definition ppp.h:383
uint_t authProtocol
Allowed authentication protocols.
Definition ppp.h:385
PPP context.
Definition ppp.h:444
bool_t ipv6Rejected
IPv6 protocol is not support by the peer.
Definition ppp.h:473
ChapFsm chapFsm
CHAP finite state machine.
Definition ppp.h:468
char_t peerName[PPP_MAX_USERNAME_LEN+1]
Peer's name.
Definition ppp.h:451
char_t password[PPP_MAX_PASSWORD_LEN+1]
Password.
Definition ppp.h:450
uint8_t frame[PPP_MAX_FRAME_SIZE]
Incoming PPP frame.
Definition ppp.h:475
systime_t timeout
Timeout for blocking operations.
Definition ppp.h:447
PppConfig localConfig
Local configuration options.
Definition ppp.h:470
PppFsm ipv6cpFsm
IPV6CP finite state machine.
Definition ppp.h:462
uint8_t txBuffer[PPP_TX_BUFFER_SIZE]
Transmit buffer.
Definition ppp.h:477
PppConfig peerConfig
Peer configuration options.
Definition ppp.h:471
PapFsm papFsm
PAP finite state machine.
Definition ppp.h:465
PppFsm lcpFsm
LCP finite state machine.
Definition ppp.h:457
PppFsm ipcpFsm
IPCP finite state machine.
Definition ppp.h:459
bool_t ipRejected
IPv4 protocol is not supported by the peer.
Definition ppp.h:472
NetInterface * interface
PPP settings.
Definition ppp.h:446
char_t username[PPP_MAX_USERNAME_LEN+1]
User name.
Definition ppp.h:449
uint8_t rxBuffer[PPP_RX_BUFFER_SIZE]
Receive buffer.
Definition ppp.h:482
PppPhase pppPhase
PPP phase.
Definition ppp.h:456