1 // SoftEther VPN Source Code - Developer Edition Master Branch
2 // Cedar Communication Module
3 
4 
5 // UdpAccel.h
6 // Header of UdpAccel.c
7 
8 #ifndef	UDPACCEL_H
9 #define	UDPACCEL_H
10 
11 #include "CedarType.h"
12 
13 #include "Mayaqua/Network.h"
14 
15 // Constants
16 #define	UDP_ACCELERATION_COMMON_KEY_SIZE_V1	20			// V1: Common key size
17 #define	UDP_ACCELERATION_PACKET_KEY_SIZE_V1	20			// V1: Key size for the packet
18 #define	UDP_ACCELERATION_PACKET_IV_SIZE_V1	20			// V1: IV size for the packet
19 
20 #define	UDP_ACCELERATION_COMMON_KEY_SIZE_V2	128			// V2: Common key size
21 #define	UDP_ACCELERATION_PACKET_IV_SIZE_V2	12			// V2: IV size for the packet
22 #define	UDP_ACCELERATION_PACKET_MAC_SIZE_V2	16			// V2: MAC size for the packet
23 
24 #define	UDP_ACCELERATION_TMP_BUF_SIZE		2048		// Temporary buffer size
25 #define	UDP_ACCELERATION_WINDOW_SIZE_MSEC	(30 * 1000)	// Receive window size (in milliseconds)
26 
27 #define	UDP_ACCELERATION_SUPPORTED_MAX_PAYLOAD_SIZE	1600	// Maximum supported payload size
28 #define	UDP_ACCELERATION_MAX_PADDING_SIZE	32			// Maximum padding size
29 
30 #define	UDP_ACCELERATION_REQUIRE_CONTINUOUS	(10 * 1000)	// Not to use if stable communication is not continued at least for this time
31 
32 // Time constant for Build 8534 or earlier
33 #define	UDP_ACCELERATION_KEEPALIVE_INTERVAL_MIN	(1 * 1000)	// Keep Alive Interval (minimum)
34 #define	UDP_ACCELERATION_KEEPALIVE_INTERVAL_MAX	(3 * 1000)	// Keep Alive Interval (maximum)
35 #define	UDP_ACCELERATION_KEEPALIVE_TIMEOUT		(9 * 1000)	// Time to disconnect time by non-communication
36 
37 // Time constant for Build 8535 or later
38 #define	UDP_ACCELERATION_KEEPALIVE_INTERVAL_MIN_FAST	(500)	// Keep Alive Interval (minimum)
39 #define	UDP_ACCELERATION_KEEPALIVE_INTERVAL_MAX_FAST	(1000)	// Keep Alive Interval (maximum)
40 #define	UDP_ACCELERATION_KEEPALIVE_TIMEOUT_FAST			(2100)	// Time to disconnect time by non-communication
41 
42 // Range of port numbers
43 #define	UDP_SERVER_PORT_LOWER				40000		// Minimum port
44 #define	UDP_SERVER_PORT_HIGHER				44999		// Maximum port
45 
46 // NAT-T signatures to be embedded in the Keep Alive of the session
47 #define	UDP_NAT_T_IP_SIGNATURE_IN_KEEP_ALIVE			"NATT_MY_IP"
48 #define	UDP_NAT_T_PORT_SIGNATURE_IN_KEEP_ALIVE			"NATT_MY_PORT"
49 
50 // UDP Acceleration Mode
51 struct UDP_ACCEL
52 {
53 	CEDAR *Cedar;										// Cedar
54 	bool NoNatT;										// Not to communicate with the NAT-T server (To communicate with the query server instead)
55 	bool ClientMode;									// Whether client mode
56 	bool IsInCedarPortList;								// Whether included in the port list of the Cedar
57 	UINT64 Now;											// Current time
58 	CIPHER *CipherEncrypt;								// Encryption context
59 	CIPHER *CipherDecrypt;								// Decryption context
60 	UCHAR MyKey[UDP_ACCELERATION_COMMON_KEY_SIZE_V1];	// Send-direction common key
61 	UCHAR YourKey[UDP_ACCELERATION_COMMON_KEY_SIZE_V1];	// Receive-direction common key
62 	SOCK *UdpSock;										// UDP socket
63 	IP MyIp;											// My IP address
64 	IP MyIpNatT;										// My IP address, found via the NAT-T server
65 	USHORT MyPort;										// My port number
66 	USHORT MyPortNatT;									// My port number, found via the NAT-T server
67 	bool MyIpOrPortNatTChanged;							// NAT-T server reported a new IP or port for me
68 	IP YourIp;											// IP address of the peer (current)
69 	IP YourIpReported;									// IP address of the peer (reported)
70 	IP YourIpNatT;										// IP address of the peer, found via the NAT-T server
71 	USHORT YourPort;									// Port number of the peer (current)
72 	USHORT YourPortReported;							// Port number of the peer (reported)
73 	USHORT YourPortNatT;								// Port number of the peer, found via the NAT-T server
74 	bool YourIpOrPortNatTChanged;						// NAT-T server reported a new IP or port for the peer
75 	bool IsIPv6;										// Whether it's an IPv6
76 	UCHAR TmpBuf[UDP_ACCELERATION_TMP_BUF_SIZE];		// Temporary buffer
77 	UINT64 LastRecvYourTick;							// Opponent's tick value of the last reception
78 	UINT64 LastRecvMyTick;								// My tick value of the last reception
79 	QUEUE *RecvBlockQueue;								// Reception block queue
80 	bool UseHMac;										// Flag to use the HMAC
81 	bool PlainTextMode;									// No encryption
82 	UINT64 LastSetSrcIpAndPortTick;						// Opponent's tick ??value at the time of storing the IP address and port number of the opponent at the end
83 	UINT64 LastRecvTick;								// Tick when data has received at the end
84 	UINT64 NextSendKeepAlive;							// Next time to send a KeepAlive packet
85 	UCHAR NextIv[UDP_ACCELERATION_PACKET_IV_SIZE_V1];	// IV to be used next
86 	UINT MyCookie;										// My cookie
87 	UINT YourCookie;									// Cookie of the other party
88 	bool Inited;										// Initialized flag
89 	UINT Mss;											// Optimal MSS
90 	UINT MaxUdpPacketSize;								// Get the maximum transmittable UDP size
91 	LOCK *NatT_Lock;									// Lock the IP address field of NAT-T server
92 	IP NatT_IP;											// IP address of the NAT-T server
93 	THREAD *NatT_GetIpThread;							// IP address acquisition thread of NAT-T server
94 	bool NatT_Halt;										// Halting flag of IP address acquisition thread of NAT-T server
95 	EVENT *NatT_HaltEvent;								// Halting event of IP address acquisition thread of NAT-T server
96 	UINT64 NextPerformNatTTick;							// Time to communicate with NAT-T server next time
97 	UINT CommToNatT_NumFail;							// Number of failures to communicate with NAT-T server
98 	bool FatalError;									// A fatal error occurred
99 	bool NatT_IP_Changed;								// IP address of the NAT-T server has changed
100 	UINT64 NatT_TranId;									// Transaction ID to be exchanged with the NAT-T server
101 	bool IsReachedOnce;									// It is true if it succeeds in mutual transmission and reception of packets at least once
102 	UINT64 CreatedTick;									// Object creation time
103 	bool FastDetect;									// Fast disconnection detection mode
104 	UINT64 FirstStableReceiveTick;						// Start time of current stable continued receivable period
105 	bool UseSuperRelayQuery;							// Use the super relay query
106 	bool UseUdpIpQuery;									// Use the self IP address query by UDP
107 	IP UdpIpQueryHost;									// Host for the self IP address query by UDP
108 	UINT UdpIpQueryPort;								// Port number for self IP address for query by UDP
109 	UCHAR UdpIpQueryPacketData[16];						// Query packet data (final transmission)
110 	UINT UdpIpQueryPacketSize;							// Query packet data size (final transmission)
111 	UCHAR UdpHostUniqueKey[SHA1_SIZE];					// Unique key for UDP self endpoint query
112 	UINT Version;										// Version
113 	UCHAR MyKey_V2[UDP_ACCELERATION_COMMON_KEY_SIZE_V2];	// Send-direction common key (version 2)
114 	UCHAR NextIv_V2[UDP_ACCELERATION_PACKET_IV_SIZE_V2];	// IV to be used next (version 2)
115 	bool ReadRawFlagMode;								// Read raw flag mode
116 };
117 
118 // Function prototype
119 UDP_ACCEL *NewUdpAccel(CEDAR *cedar, IP *ip, bool client_mode, bool random_port, bool no_nat_t);
120 void FreeUdpAccel(UDP_ACCEL *a);
121 bool UdpAccelInitClient(UDP_ACCEL *a, UCHAR *key, IP *detected_ip, IP *reported_ip, USHORT port, UINT cookie, UINT my_cookie);
122 bool UdpAccelInitServer(UDP_ACCEL *a, UCHAR *key, IP *detected_ip, IP *reported_ip, USHORT port);
123 void UdpAccelPoll(UDP_ACCEL *a);
124 void UdpAccelSetTick(UDP_ACCEL *a, UINT64 tick64);
125 BLOCK *UdpAccelProcessRecvPacket(UDP_ACCEL *a, UCHAR *buf, UINT size, IP *src_ip, UINT src_port);
126 void UdpAccelCalcKeyV1(UCHAR *key, UCHAR *common_key, UCHAR *iv);
127 bool UdpAccelIsSendReady(UDP_ACCEL *a, bool check_keepalive);
128 void UdpAccelSend(UDP_ACCEL *a, UCHAR *data, UINT data_size, UCHAR flag, UINT max_size, bool high_priority);
129 void UdpAccelSendBlock(UDP_ACCEL *a, BLOCK *b);
130 UINT UdpAccelCalcMss(UDP_ACCEL *a);
131 void NatT_GetIpThread(THREAD *thread, void *param);
132 
133 #endif	// UDPACCEL_H
134