pap.h
Go to the documentation of this file.
1 /**
2  * @file pap.h
3  * @brief PAP (Password Authentication Protocol)
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2024 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneTCP Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.4.0
29  **/
30 
31 #ifndef _PAP_H
32 #define _PAP_H
33 
34 //Dependencies
35 #include "core/net.h"
36 #include "ppp/ppp.h"
37 
38 //PAP authentication support
39 #ifndef PAP_SUPPORT
40  #define PAP_SUPPORT ENABLED
41 #elif (PAP_SUPPORT != ENABLED && PAP_SUPPORT != DISABLED)
42  #error PAP_SUPPORT parameter is not valid
43 #endif
44 
45 //Restart timer
46 #ifndef PAP_RESTART_TIMER
47  #define PAP_RESTART_TIMER 3000
48 #elif (PAP_RESTART_TIMER < 1000)
49  #error PAP_RESTART_TIMER parameter is not valid
50 #endif
51 
52 //Maximum number of retransmissions for Authenticate-Request packets
53 #ifndef PAP_MAX_REQUESTS
54  #define PAP_MAX_REQUESTS 5
55 #elif (PAP_MAX_REQUESTS < 1)
56  #error PAP_MAX_REQUESTS parameter is not valid
57 #endif
58 
59 //C++ guard
60 #ifdef __cplusplus
61 extern "C" {
62 #endif
63 
64 
65 /**
66  * @brief PAP states
67  **/
68 
69 typedef enum
70 {
80 
81 
82 /**
83  * @brief Code field values
84  **/
85 
86 typedef enum
87 {
88  PAP_CODE_AUTH_REQ = 1, ///<Authenticate-Request
89  PAP_CODE_AUTH_ACK = 2, ///<Authenticate-Ack
90  PAP_CODE_AUTH_NAK = 3 ///<Authenticate-Nak
92 
93 
94 //CC-RX, CodeWarrior or Win32 compiler?
95 #if defined(__CCRX__)
96  #pragma pack
97 #elif defined(__CWCC__) || defined(_WIN32)
98  #pragma pack(push, 1)
99 #endif
100 
101 
102 /**
103  * @brief Authenticate-Request packet
104  **/
105 
107 {
108  uint8_t code; //0
109  uint8_t identifier; //1
110  uint16_t length; //2-3
111  uint8_t peerIdLength; //4
112  uint8_t peerId[]; //5
114 
115 
116 /**
117  * @brief Authenticate-Ack packet
118  **/
119 
120 typedef __packed_struct
121 {
122  uint8_t code; //0
123  uint8_t identifier; //1
124  uint16_t length; //2-3
125  uint8_t msgLength; //4
126  uint8_t message[]; //5
128 
129 
130 /**
131  * @brief Authenticate-Nak packet
132  **/
133 
134 typedef __packed_struct
135 {
136  uint8_t code; //0
137  uint8_t identifier; //1
138  uint16_t length; //2-3
139  uint8_t msgLength; //4
140  uint8_t message[]; //5
142 
143 
144 //CC-RX, CodeWarrior or Win32 compiler?
145 #if defined(__CCRX__)
146  #pragma unpack
147 #elif defined(__CWCC__) || defined(_WIN32)
148  #pragma pack(pop)
149 #endif
150 
151 
152 /**
153  * @brief PAP finite state machine
154  **/
155 
156 typedef struct
157 {
158  uint_t localState; ///<Local state
159  uint_t peerState; ///<Peer state
160  uint8_t identifier; ///<Identifier used to match requests and replies
161  uint_t restartCounter; ///<Restart counter
162  systime_t timestamp; ///<Timestamp to manage retransmissions
163  const uint8_t *password; ///<Peer's password
164  size_t passwordLen; ///<Length of the password in bytes
165 } PapFsm;
166 
167 
168 //PAP related functions
171 
172 void papTick(PppContext *context);
173 
174 void papProcessPacket(PppContext *context,
175  const PppPacket *packet, size_t length);
176 
178  const PapAuthReqPacket *authReqPacket, size_t length);
179 
181  const PapAuthAckPacket *authAckPacket, size_t length);
182 
184  const PapAuthNakPacket *authNakPacket, size_t length);
185 
187 error_t papSendAuthAck(PppContext *context, uint8_t identifier);
188 error_t papSendAuthNak(PppContext *context, uint8_t identifier);
189 
190 bool_t papCheckPassword(PppContext *context, const char_t *password);
191 
192 //C++ guard
193 #ifdef __cplusplus
194 }
195 #endif
196 
197 #endif
uint8_t code
Definition: coap_common.h:179
unsigned int uint_t
Definition: compiler_port.h:50
char char_t
Definition: compiler_port.h:48
int bool_t
Definition: compiler_port.h:53
error_t
Error codes.
Definition: error.h:43
TCP/IP stack core.
uint32_t systime_t
System time.
uint8_t msgLength
Definition: pap.h:125
error_t papSendAuthAck(PppContext *context, uint8_t identifier)
Send Authenticate-Ack packet.
Definition: pap.c:500
error_t papProcessAuthNak(PppContext *context, const PapAuthNakPacket *authNakPacket, size_t length)
Process Authenticate-Nak packet.
Definition: pap.c:387
uint16_t length
Definition: pap.h:110
uint8_t peerIdLength
Definition: pap.h:111
error_t papSendAuthReq(PppContext *context)
Send Authenticate-Request packet.
Definition: pap.c:422
error_t papSendAuthNak(PppContext *context, uint8_t identifier)
Send Authenticate-Nak packet.
Definition: pap.c:551
uint8_t message[]
Definition: pap.h:126
error_t papStartAuth(PppContext *context)
Start PAP authentication.
Definition: pap.c:53
PapCode
Code field values.
Definition: pap.h:87
@ PAP_CODE_AUTH_REQ
Authenticate-Request.
Definition: pap.h:88
@ PAP_CODE_AUTH_NAK
Authenticate-Nak.
Definition: pap.h:90
@ PAP_CODE_AUTH_ACK
Authenticate-Ack.
Definition: pap.h:89
void papProcessPacket(PppContext *context, const PppPacket *packet, size_t length)
Process an incoming PAP packet.
Definition: pap.c:149
PapAuthAckPacket
Definition: pap.h:127
PapAuthReqPacket
Definition: pap.h:113
PapState
PAP states.
Definition: pap.h:70
@ PAP_STATE_7_NAK_RCVD
Definition: pap.h:78
@ PAP_STATE_1_STARTED
Definition: pap.h:72
@ PAP_STATE_4_ACK_SENT
Definition: pap.h:75
@ PAP_STATE_0_INITIAL
Definition: pap.h:71
@ PAP_STATE_5_ACK_RCVD
Definition: pap.h:76
@ PAP_STATE_2_REQ_SENT
Definition: pap.h:73
@ PAP_STATE_6_NAK_SENT
Definition: pap.h:77
@ PAP_STATE_3_REQ_RCVD
Definition: pap.h:74
uint8_t peerId[]
Definition: pap.h:112
void papTick(PppContext *context)
PAP timer handler.
Definition: pap.c:110
bool_t papCheckPassword(PppContext *context, const char_t *password)
Password verification.
Definition: pap.c:602
PapAuthNakPacket
Definition: pap.h:141
typedef __packed_struct
Authenticate-Request packet.
Definition: pap.h:107
uint8_t identifier
Definition: pap.h:109
error_t papAbortAuth(PppContext *context)
Abort PAP authentication.
Definition: pap.c:87
error_t papProcessAuthReq(PppContext *context, const PapAuthReqPacket *authReqPacket, size_t length)
Process Authenticate-Request packet.
Definition: pap.c:214
error_t papProcessAuthAck(PppContext *context, const PapAuthAckPacket *authAckPacket, size_t length)
Process Authenticate-Ack packet.
Definition: pap.c:330
PPP (Point-to-Point Protocol)
#define PppContext
Definition: ppp.h:38
#define PppPacket
Definition: ppp.h:37
PAP finite state machine.
Definition: pap.h:157
systime_t timestamp
Timestamp to manage retransmissions.
Definition: pap.h:162
uint_t localState
Local state.
Definition: pap.h:158
size_t passwordLen
Length of the password in bytes.
Definition: pap.h:164
const uint8_t * password
Peer's password.
Definition: pap.h:163
uint_t peerState
Peer state.
Definition: pap.h:159
uint_t restartCounter
Restart counter.
Definition: pap.h:161
uint8_t identifier
Identifier used to match requests and replies.
Definition: pap.h:160