IPsec_PPP.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. // SoftEther VPN Source Code
  2. // Cedar Communication Module
  3. //
  4. // SoftEther VPN Server, Client and Bridge are free software under GPLv2.
  5. //
  6. // Copyright (c) 2012-2014 Daiyuu Nobori.
  7. // Copyright (c) 2012-2014 SoftEther VPN Project, University of Tsukuba, Japan.
  8. // Copyright (c) 2012-2014 SoftEther Corporation.
  9. //
  10. // All Rights Reserved.
  11. //
  12. // http://www.softether.org/
  13. //
  14. // Author: Daiyuu Nobori
  15. // Comments: Tetsuo Sugiyama, Ph.D.
  16. //
  17. // This program is free software; you can redistribute it and/or
  18. // modify it under the terms of the GNU General Public License
  19. // version 2 as published by the Free Software Foundation.
  20. //
  21. // This program is distributed in the hope that it will be useful,
  22. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. // GNU General Public License for more details.
  25. //
  26. // You should have received a copy of the GNU General Public License version 2
  27. // along with this program; if not, write to the Free Software
  28. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  29. //
  30. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  31. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  32. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  33. // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  34. // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  35. // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  36. // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  37. //
  38. // THE LICENSE AGREEMENT IS ATTACHED ON THE SOURCE-CODE PACKAGE
  39. // AS "LICENSE.TXT" FILE. READ THE TEXT FILE IN ADVANCE TO USE THE SOFTWARE.
  40. //
  41. //
  42. // THIS SOFTWARE IS DEVELOPED IN JAPAN, AND DISTRIBUTED FROM JAPAN,
  43. // UNDER JAPANESE LAWS. YOU MUST AGREE IN ADVANCE TO USE, COPY, MODIFY,
  44. // MERGE, PUBLISH, DISTRIBUTE, SUBLICENSE, AND/OR SELL COPIES OF THIS
  45. // SOFTWARE, THAT ANY JURIDICAL DISPUTES WHICH ARE CONCERNED TO THIS
  46. // SOFTWARE OR ITS CONTENTS, AGAINST US (SOFTETHER PROJECT, SOFTETHER
  47. // CORPORATION, DAIYUU NOBORI OR OTHER SUPPLIERS), OR ANY JURIDICAL
  48. // DISPUTES AGAINST US WHICH ARE CAUSED BY ANY KIND OF USING, COPYING,
  49. // MODIFYING, MERGING, PUBLISHING, DISTRIBUTING, SUBLICENSING, AND/OR
  50. // SELLING COPIES OF THIS SOFTWARE SHALL BE REGARDED AS BE CONSTRUED AND
  51. // CONTROLLED BY JAPANESE LAWS, AND YOU MUST FURTHER CONSENT TO
  52. // EXCLUSIVE JURISDICTION AND VENUE IN THE COURTS SITTING IN TOKYO,
  53. // JAPAN. YOU MUST WAIVE ALL DEFENSES OF LACK OF PERSONAL JURISDICTION
  54. // AND FORUM NON CONVENIENS. PROCESS MAY BE SERVED ON EITHER PARTY IN
  55. // THE MANNER AUTHORIZED BY APPLICABLE LAW OR COURT RULE.
  56. //
  57. // USE ONLY IN JAPAN. DO NOT USE THIS SOFTWARE IN ANOTHER COUNTRY UNLESS
  58. // YOU HAVE A CONFIRMATION THAT THIS SOFTWARE DOES NOT VIOLATE ANY
  59. // CRIMINAL LAWS OR CIVIL RIGHTS IN THAT PARTICULAR COUNTRY. USING THIS
  60. // SOFTWARE IN OTHER COUNTRIES IS COMPLETELY AT YOUR OWN RISK. THE
  61. // SOFTETHER VPN PROJECT HAS DEVELOPED AND DISTRIBUTED THIS SOFTWARE TO
  62. // COMPLY ONLY WITH THE JAPANESE LAWS AND EXISTING CIVIL RIGHTS INCLUDING
  63. // PATENTS WHICH ARE SUBJECTS APPLY IN JAPAN. OTHER COUNTRIES' LAWS OR
  64. // CIVIL RIGHTS ARE NONE OF OUR CONCERNS NOR RESPONSIBILITIES. WE HAVE
  65. // NEVER INVESTIGATED ANY CRIMINAL REGULATIONS, CIVIL LAWS OR
  66. // INTELLECTUAL PROPERTY RIGHTS INCLUDING PATENTS IN ANY OF OTHER 200+
  67. // COUNTRIES AND TERRITORIES. BY NATURE, THERE ARE 200+ REGIONS IN THE
  68. // WORLD, WITH DIFFERENT LAWS. IT IS IMPOSSIBLE TO VERIFY EVERY
  69. // COUNTRIES' LAWS, REGULATIONS AND CIVIL RIGHTS TO MAKE THE SOFTWARE
  70. // COMPLY WITH ALL COUNTRIES' LAWS BY THE PROJECT. EVEN IF YOU WILL BE
  71. // SUED BY A PRIVATE ENTITY OR BE DAMAGED BY A PUBLIC SERVANT IN YOUR
  72. // COUNTRY, THE DEVELOPERS OF THIS SOFTWARE WILL NEVER BE LIABLE TO
  73. // RECOVER OR COMPENSATE SUCH DAMAGES, CRIMINAL OR CIVIL
  74. // RESPONSIBILITIES. NOTE THAT THIS LINE IS NOT LICENSE RESTRICTION BUT
  75. // JUST A STATEMENT FOR WARNING AND DISCLAIMER.
  76. //
  77. //
  78. // SOURCE CODE CONTRIBUTION
  79. // ------------------------
  80. //
  81. // Your contribution to SoftEther VPN Project is much appreciated.
  82. // Please send patches to us through GitHub.
  83. // Read the SoftEther VPN Patch Acceptance Policy in advance:
  84. // http://www.softether.org/5-download/src/9.patch
  85. //
  86. //
  87. // DEAR SECURITY EXPERTS
  88. // ---------------------
  89. //
  90. // If you find a bug or a security vulnerability please kindly inform us
  91. // about the problem immediately so that we can fix the security problem
  92. // to protect a lot of users around the world as soon as possible.
  93. //
  94. // Our e-mail address for security reports is:
  95. // softether-vpn-security [at] softether.org
  96. //
  97. // Please note that the above e-mail address is not a technical support
  98. // inquiry address. If you need technical assistance, please visit
  99. // http://www.softether.org/ and ask your question on the users forum.
  100. //
  101. // Thank you for your cooperation.
  102. //
  103. //
  104. // NO MEMORY OR RESOURCE LEAKS
  105. // ---------------------------
  106. //
  107. // The memory-leaks and resource-leaks verification under the stress
  108. // test has been passed before release this source code.
  109. // IPsec_PPP.h
  110. // Header of IPsec_PPP.c
  111. #ifndef IPSEC_PPP_H
  112. #define IPSEC_PPP_H
  113. //// Macro
  114. #define PPP_LCP_CODE_IS_NEGATIVE(c) ((c) == PPP_LCP_CODE_NAK || (c) == PPP_LCP_CODE_REJECT || (c) == PPP_LCP_CODE_CODE_REJECT || (c) == PPP_LCP_CODE_PROTOCOL_REJECT)
  115. #define PPP_LCP_CODE_IS_REQUEST(c) ((c) == PPP_LCP_CODE_REQ)
  116. #define PPP_LCP_CODE_IS_RESPONSE(c) ((c) == PPP_LCP_CODE_ACK || (c) == PPP_LCP_CODE_NAK || (c) == PPP_LCP_CODE_REJECT || (c) == PPP_LCP_CODE_PROTOCOL_REJECT)
  117. #define PPP_LCP_CODE_IS_WITH_OPTION_LIST(c) ((c) == PPP_LCP_CODE_REQ || (c) == PPP_LCP_CODE_ACK || (c) == PPP_LCP_CODE_NAK)
  118. #define PPP_PAP_CODE_IS_REQUEST(c) ((c) == PPP_PAP_CODE_REQ)
  119. #define PPP_PAP_CODE_IS_RESPONSE(c) ((c) == PPP_PAP_CODE_ACK || (c) == PPP_PAP_CODE_NAK)
  120. #define PPP_CODE_IS_RESPONSE(protocol, c) ((((protocol) == PPP_PROTOCOL_LCP || (protocol) == PPP_PROTOCOL_IPCP) && PPP_LCP_CODE_IS_RESPONSE(c)) || (((protocol) == PPP_PROTOCOL_PAP) && PPP_PAP_CODE_IS_RESPONSE(c)))
  121. #define PPP_CODE_IS_REQUEST(protocol, c) ((((protocol) == PPP_PROTOCOL_LCP || (protocol) == PPP_PROTOCOL_IPCP) && PPP_LCP_CODE_IS_REQUEST(c)) || (((protocol) == PPP_PROTOCOL_PAP) && PPP_PAP_CODE_IS_REQUEST(c)) || ((protocol) == PPP_PROTOCOL_CHAP))
  122. #define PPP_CODE_IS_WITH_OPTION_LIST(protocol, c) ((((protocol) == PPP_PROTOCOL_LCP || (protocol) == PPP_PROTOCOL_IPCP) && PPP_LCP_CODE_IS_WITH_OPTION_LIST(c)) || false)
  123. #define PPP_IS_SUPPORTED_PROTOCOL(p) ((p) == PPP_PROTOCOL_LCP || (p) == PPP_PROTOCOL_PAP || (p) == PPP_PROTOCOL_CHAP || (p) == PPP_PROTOCOL_IPCP || (p) == PPP_PROTOCOL_IP)
  124. //// Constants
  125. // Time-out value
  126. #define PPP_PACKET_RECV_TIMEOUT 10000 // Timeout until the next packet is received
  127. #define PPP_PACKET_RESEND_INTERVAL 1000 // Retransmission interval of the last packet
  128. #define PPP_TERMINATE_TIMEOUT 2000 // Timeout value to complete disconnection after requesting to disconnect in the PPP
  129. #define PPP_ECHO_SEND_INTERVAL 4792 // Transmission interval of PPP Echo Request
  130. #define PPP_DATA_TIMEOUT (20 * 1000) // Communication time-out
  131. // MRU
  132. #define PPP_MRU_DEFAULT 1500 // Default value
  133. #define PPP_MRU_MIN 100 // Minimum value
  134. #define PPP_MRU_MAX 1500 // Maximum value
  135. // PPP protocol (for control)
  136. #define PPP_PROTOCOL_LCP 0xc021
  137. #define PPP_PROTOCOL_PAP 0xc023
  138. #define PPP_PROTOCOL_IPCP 0x8021
  139. #define PPP_PROTOCOL_CHAP 0xc223
  140. // PPP protocol (for transfer)
  141. #define PPP_PROTOCOL_IP 0x0021
  142. // LCP code
  143. #define PPP_LCP_CODE_REQ 1
  144. #define PPP_LCP_CODE_ACK 2
  145. #define PPP_LCP_CODE_NAK 3
  146. #define PPP_LCP_CODE_REJECT 4
  147. #define PPP_LCP_CODE_TERMINATE_REQ 5
  148. #define PPP_LCP_CODE_TERMINATE_ACK 6
  149. #define PPP_LCP_CODE_CODE_REJECT 7
  150. #define PPP_LCP_CODE_PROTOCOL_REJECT 8
  151. #define PPP_LCP_CODE_ECHO_REQUEST 9
  152. #define PPP_LCP_CODE_ECHO_RESPONSE 10
  153. #define PPP_LCP_CODE_DROP 11
  154. #define PPP_LCP_CODE_IDENTIFICATION 12
  155. // PAP Code
  156. #define PPP_PAP_CODE_REQ 1
  157. #define PPP_PAP_CODE_ACK 2
  158. #define PPP_PAP_CODE_NAK 3
  159. // CHAP code
  160. #define PPP_CHAP_CODE_CHALLENGE 1
  161. #define PPP_CHAP_CODE_RESPONSE 2
  162. #define PPP_CHAP_CODE_SUCCESS 3
  163. #define PPP_CHAP_CODE_FAILURE 4
  164. // LCP Option Type
  165. #define PPP_LCP_OPTION_MRU 1
  166. #define PPP_LCP_OPTION_AUTH 3
  167. // IPCP option type
  168. #define PPP_IPCP_OPTION_IP 3
  169. #define PPP_IPCP_OPTION_DNS1 129
  170. #define PPP_IPCP_OPTION_DNS2 131
  171. #define PPP_IPCP_OPTION_WINS1 130
  172. #define PPP_IPCP_OPTION_WINS2 132
  173. // Authentication protocol
  174. #define PPP_LCP_AUTH_PAP PPP_PROTOCOL_PAP
  175. #define PPP_LCP_AUTH_CHAP PPP_PROTOCOL_CHAP
  176. // Algorithm of CHAP
  177. #define PPP_CHAP_ALG_MS_CHAP_V2 0x81
  178. //// Type
  179. // IP options used in the PPP
  180. struct PPP_IPOPTION
  181. {
  182. IP IpAddress; // IP address
  183. IP DnsServer1, DnsServer2; // DNS server address
  184. IP WinsServer1, WinsServer2; // WINS server address
  185. };
  186. // PPP packet
  187. struct PPP_PACKET
  188. {
  189. USHORT Protocol; // Protocol
  190. bool IsControl; // Whether or not the control packet
  191. PPP_LCP *Lcp; // LCP packet data
  192. UINT DataSize; // Data size
  193. void *Data; // Data body
  194. };
  195. // PPP LCP packet
  196. struct PPP_LCP
  197. {
  198. UCHAR Code; // Code
  199. UCHAR Id; // ID
  200. UCHAR MagicNumber[4]; // Magic number
  201. LIST *OptionList; // PPP options list
  202. void *Data; // Data
  203. UINT DataSize; // Data size
  204. };
  205. // PPP Options
  206. struct PPP_OPTION
  207. {
  208. UCHAR Type; // Type of option
  209. UINT DataSize; // Data size
  210. UCHAR Data[254]; // Data
  211. bool IsSupported; // Flag of whether it is supported
  212. bool IsAccepted; // Flag for whether accepted
  213. UCHAR AltData[254]; // Alternate data when it isn't accepted
  214. UINT AltDataSize; // Alternate data size
  215. };
  216. // PPP session
  217. struct PPP_SESSION
  218. {
  219. CEDAR *Cedar; // Cedar
  220. IP ClientIP; // Client IP address
  221. UINT ClientPort; // Client port
  222. IP ServerIP; // Server IP address
  223. UINT ServerPort; // Server port
  224. TUBE *TubeSend; // Sending tube
  225. TUBE *TubeRecv; // Receiving tube
  226. UCHAR NextId; // ID to be used next
  227. UINT Mru1; // MRU (server -> client)
  228. UINT Mru2; // MRU (client -> server)
  229. LIST *RecvPacketList; // Received packet list
  230. PPP_PACKET *LastStoredPacket; // Packet that is stored at the last
  231. bool IsTerminateReceived; // Whether a Terminate has been received
  232. UINT DisconnectCauseCode; // L2TP disconnect cause code
  233. UINT DisconnectCauseDirection; // L2TP disconnect cause direction code
  234. IPC *Ipc; // IPC
  235. bool ClientLCPOptionDetermined; // LCP option from the client has been determined
  236. char Postfix[MAX_SIZE]; // Postfix of the session name
  237. char ClientHostname[MAX_SIZE]; // Client host name
  238. char ClientSoftwareName[MAX_SIZE]; // Client software name
  239. UINT64 NextEchoSendTime; // Time to send Echo Request next
  240. UINT64 LastRecvTime; // Time which the data has been received last
  241. DHCP_OPTION_LIST ClientAddressOption; // Client address option
  242. bool DhcpIpAllocTried; // Whether the request for an IP address is already attempted by DHCP
  243. bool DhcpIpInformTried; // Whether the acquirement for an IP information is already attempted by DHCP
  244. bool DhcpAllocated; // IP address is assigned by DHCP
  245. bool UseStaticIPAddress; // Use a static IP address that is specified by the client
  246. UINT64 DhcpRenewInterval; // DHCP update interval
  247. UINT64 DhcpNextRenewTime; // DHCP renewal time of the next
  248. char CryptName[MAX_SIZE]; // Cipher algorithm name
  249. UINT AdjustMss; // MSS value
  250. TUBE_FLUSH_LIST *FlushList; // Tube Flush List
  251. bool EnableMSCHAPv2; // Enable the MS-CHAP v2
  252. USHORT AuthProtocol; // Authentication protocol
  253. bool AuthOk; // Flag for whether the authentication was successful
  254. UCHAR MsChapV2_ServerChallenge[16]; // MS-CHAPv2 Server Challenge
  255. UCHAR MsChapV2_ClientChallenge[16]; // MS-CHAPv2 Client Challenge
  256. UCHAR MsChapV2_ClientResponse[24]; // MS-CHAPv2 Client Response
  257. UCHAR MsChapV2_ServerResponse[20]; // MS-CHAPv2 Server Response
  258. UINT MsChapV2_ErrorCode; // Authentication failure error code of MS-CHAPv2
  259. };
  260. // Function prototype
  261. THREAD *NewPPPSession(CEDAR *cedar, IP *client_ip, UINT client_port, IP *server_ip, UINT server_port, TUBE *send_tube, TUBE *recv_tube, char *postfix, char *client_software_name, char *client_hostname, char *crypt_name, UINT adjust_mss);
  262. void PPPThread(THREAD *thread, void *param);
  263. void FreePPPSession(PPP_SESSION *p);
  264. void FreePPPOptionList(LIST *o);
  265. void FreePPPLCP(PPP_LCP *c);
  266. PPP_LCP *NewPPPLCP(UCHAR code, UCHAR id);
  267. PPP_LCP *ParseLCP(USHORT protocol, void *data, UINT size);
  268. BUF *BuildLCPData(PPP_LCP *c);
  269. PPP_OPTION *GetOptionValue(PPP_LCP *c, UCHAR type);
  270. PPP_PACKET *ParsePPPPacket(void *data, UINT size);
  271. void FreePPPPacket(PPP_PACKET *pp);
  272. void FreePPPPacketEx(PPP_PACKET *pp, bool no_free_struct);
  273. BUF *BuildPPPPacketData(PPP_PACKET *pp);
  274. PPP_OPTION *NewPPPOption(UCHAR type, void *data, UINT size);
  275. bool PPPSendPacket(PPP_SESSION *p, PPP_PACKET *pp);
  276. bool PPPSendPacketEx(PPP_SESSION *p, PPP_PACKET *pp, bool no_flush);
  277. PPP_PACKET *PPPRecvPacket(PPP_SESSION *p, bool async);
  278. PPP_PACKET *PPPRecvPacketWithLowLayerProcessing(PPP_SESSION *p, bool async);
  279. PPP_PACKET *PPPRecvPacketForCommunication(PPP_SESSION *p);
  280. void PPPStoreLastPacket(PPP_SESSION *p, PPP_PACKET *pp);
  281. void PPPCleanTerminate(PPP_SESSION *p);
  282. bool PPPGetIPOptionFromLCP(PPP_IPOPTION *o, PPP_LCP *c);
  283. bool PPPSetIPOptionToLCP(PPP_IPOPTION *o, PPP_LCP *c, bool only_modify);
  284. bool PPPGetIPAddressValueFromLCP(PPP_LCP *c, UINT type, IP *ip);
  285. bool PPPSetIPAddressValueToLCP(PPP_LCP *c, UINT type, IP *ip, bool only_modify);
  286. bool PPPSendRequest(PPP_SESSION *p, USHORT protocol, PPP_LCP *c);
  287. USHORT PPPContinueCurrentProtocolRequestListening(PPP_SESSION *p, USHORT protocol);
  288. bool PPPContinueUntilFinishAllLCPOptionRequestsDetermined(PPP_SESSION *p);
  289. PPP_PACKET *PPPRecvResponsePacket(PPP_SESSION *p, PPP_PACKET *req, USHORT expected_protocol, USHORT *received_protocol, bool finish_when_all_lcp_acked);
  290. PPP_PACKET *PPPProcessRequestPacket(PPP_SESSION *p, PPP_PACKET *req);
  291. void PPPSendEchoRequest(PPP_SESSION *p);
  292. bool PPPParseUsername(CEDAR *cedar, char *src, ETHERIP_ID *dst);
  293. bool IsHubExistsWithLock(CEDAR *cedar, char *hubname);
  294. void GenerateNtPasswordHash(UCHAR *dst, char *password);
  295. void GenerateNtPasswordHashHash(UCHAR *dst_hash, UCHAR *src_hash);
  296. void MsChapV2Server_GenerateChallenge(UCHAR *dst);
  297. void MsChapV2Client_GenerateChallenge(UCHAR *dst);
  298. void MsChapV2_GenerateChallenge8(UCHAR *dst, UCHAR *client_challenge, UCHAR *server_challenge, char *username);
  299. void MsChapV2Client_GenerateResponse(UCHAR *dst, UCHAR *challenge8, UCHAR *nt_password_hash);
  300. void MsChapV2Server_GenerateResponse(UCHAR *dst, UCHAR *nt_password_hash_hash, UCHAR *client_response, UCHAR *challenge8);
  301. bool MsChapV2VerityPassword(IPC_MSCHAP_V2_AUTHINFO *d, char *password);
  302. char *MsChapV2DoBruteForce(IPC_MSCHAP_V2_AUTHINFO *d, LIST *password_list);
  303. #endif // IPSEC_PPP_H
  304. // Developed by SoftEther VPN Project at University of Tsukuba in Japan.
  305. // Department of Computer Science has dozens of overly-enthusiastic geeks.
  306. // Join us: http://www.tsukuba.ac.jp/english/admission/