Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1327 lines
46 KiB

  1. /*++
  2. Copyright (c) 1997-2001 Microsoft Corporation
  3. Module Name:
  4. macros.h
  5. Abstract:
  6. Contains all the macros.
  7. Author:
  8. Sanjay Anand (SanjayAn) 2-January-1997
  9. ChunYe
  10. Environment:
  11. Kernel mode
  12. Revision History:
  13. --*/
  14. #ifndef _MACROS_H
  15. #define _MACROS_H
  16. #define REGISTER register
  17. #define EXTENDED_MULTIPLY RtlExtendedIntegerMultiply
  18. #ifndef MAX
  19. #define MAX(x,y) ((x) < (y)) ? (y) : (x)
  20. #endif
  21. #ifndef MIN
  22. #define MIN(x,y) ((x) < (y)) ? (x) : (y)
  23. #endif
  24. #define MAX_IP_DATA_LENGTH ((USHORT)0xfffff)
  25. #define MAX_AH_OUTPUT_LEN MAX(MD5DIGESTLEN, A_SHA_DIGEST_LEN)
  26. //
  27. // This macro adds a ULONG to a LARGE_INTEGER.
  28. //
  29. #define ADD_TO_LARGE_INTEGER(_LargeInteger,_Ulong) \
  30. ExInterlockedAddLargeStatistic((PLARGE_INTEGER)(_LargeInteger),(ULONG)(_Ulong))
  31. #define IPSecEqualMemory(_p1, _p2, _len) RtlEqualMemory(_p1, _p2, _len)
  32. #define IPSecMoveMemory(_p1, _p2, _len) RtlMoveMemory(_p1, _p2, _len)
  33. #define IPSecZeroMemory(_p1, _len) RtlZeroMemory(_p1, _len)
  34. //
  35. // Truncates _src to _numbytes and copies into _dest
  36. // then zeroes out the rest in _dest
  37. //
  38. #define TRUNCATE(_dest, _src, _numbytes, _destlen) { \
  39. IPSecZeroMemory ( _dest+_numbytes, _destlen - _numbytes); \
  40. }
  41. //
  42. // Some macros
  43. //
  44. #ifdef NET_SHORT
  45. #undef NET_SHORT
  46. #endif
  47. __inline
  48. USHORT
  49. FASTCALL
  50. NET_SHORT(USHORT x)
  51. {
  52. #if (defined(_M_IX86) && (_MSC_FULL_VER > 13009037)) || ((defined(_M_AMD64) || defined(_M_IA64)) && (_MSC_FULL_VER > 13009175))
  53. return _byteswap_ushort(x);
  54. #else
  55. ASSERT(x <= 0xffff);
  56. return (x << 8) | (x >> 8);
  57. #endif
  58. }
  59. #define NET_TO_HOST_SHORT(Val) NET_SHORT(Val)
  60. #define HOST_TO_NET_SHORT(Val) NET_SHORT(Val)
  61. #ifdef NET_LONG
  62. #undef NET_LONG
  63. #endif
  64. __inline
  65. ULONG
  66. FASTCALL
  67. NET_LONG(ULONG x)
  68. {
  69. #if (defined(_M_IX86) && (_MSC_FULL_VER > 13009037)) || ((defined(_M_AMD64) || defined(_M_IA64)) && (_MSC_FULL_VER > 13009175))
  70. return _byteswap_ulong(x);
  71. #else
  72. REGISTER ULONG BytesSwapped;
  73. BytesSwapped = ((x & 0x00ff00ff) << 8) | ((x & 0xff00ff00) >> 8);
  74. return (BytesSwapped << 16) | (BytesSwapped >> 16);
  75. #endif
  76. }
  77. #define NET_TO_HOST_LONG(Val) NET_LONG(Val)
  78. #define HOST_TO_NET_LONG(Val) NET_LONG(Val)
  79. #define IPSEC_100NS_FACTOR 10000000
  80. #define IPSEC_CONVERT_SECS_TO_100NS(_li, _delta) { \
  81. (_li).LowPart = _delta; \
  82. (_li) = EXTENDED_MULTIPLY(_li, IPSEC_100NS_FACTOR); \
  83. }
  84. #define IS_CLASSD(i) (((long)(i) & 0xf0000000) == 0xe0000000)
  85. //
  86. // Check SA against Lifetime information - we try to anticipate in advance if the
  87. // SA is going to expire and start off a re-key so that when it actually does expire,
  88. // we have the new SA setup.
  89. //
  90. //
  91. // IPSEC_EXPIRE_TIME_PAD is the time before expiration when we start re-keying
  92. //
  93. #define IPSEC_EXPIRE_TIME_PAD_I (75 * IPSEC_100NS_FACTOR)
  94. #define IPSEC_EXPIRE_TIME_PAD_R (40 * IPSEC_100NS_FACTOR)
  95. #define IPSEC_EXPIRE_TIME_PAD_OAKLEY (1 * IPSEC_100NS_FACTOR)
  96. //
  97. // IPSEC_INBOUND_KEEPALIVE_TIME is the time an expired inboundSA will be kept
  98. // alive in the driver
  99. //
  100. #define IPSEC_INBOUND_KEEPALIVE_TIME (60)
  101. //
  102. // IPSEC_MAX_EXPIRE_TIME is the maximum phase-2 lifetime allowed in driver
  103. //
  104. #define IPSEC_MAX_EXPIRE_TIME (48 * 3600 - 1)
  105. //
  106. // IPSEC_MIN_EXPIRE_TIME is the minimum phase-2 lifetime allowed in driver
  107. //
  108. #define IPSEC_MIN_EXPIRE_TIME (60)
  109. #define IPSEC_EXPIRE_THRESHOLD_I (50)
  110. #define IPSEC_EXPIRE_THRESHOLD_R (75)
  111. //
  112. // skew the initiator and responder with Pads
  113. //
  114. #define IPSEC_DEFAULT_SA_IDLE_TIME_PAD_I 0
  115. #define IPSEC_DEFAULT_SA_IDLE_TIME_PAD_R 30
  116. //
  117. // The # of packets before which we start reneg. because of replay rollover
  118. // 1M bytes / 1500 packets
  119. //
  120. #define IPSEC_EXPIRE_REPLAY_MASK (0x80000000)
  121. #define MAX_ULONG ((ULONG) -1)
  122. #define MAX_LONG (0x7fffffff)
  123. //
  124. // Some constants used for POST_EXPIRE_NOTIFY
  125. //
  126. #define IPSEC_INVALID_SPI 0
  127. #define IPSEC_INVALID_ADDR (-1)
  128. //
  129. // Check the lifetime (kbytes and seconds) and replay rollover.
  130. // FALSE => expired
  131. //
  132. #define IPSEC_CHECK_PADDED_LIFETIME(__pSA, _status, _index) { \
  133. LARGE_INTEGER __curtime; \
  134. (_status) = TRUE; \
  135. if (((__pSA)->sa_ReplaySendSeq[0] & \
  136. IPSEC_EXPIRE_REPLAY_MASK) && \
  137. ((__pSA)->sa_Flags & FLAGS_SA_OUTBOUND)) { \
  138. _status = FALSE; \
  139. } else { \
  140. KeQuerySystemTime(&__curtime); \
  141. if (((__pSA)->sa_KeyExpirationTimeWithPad.QuadPart > 0i64) && \
  142. ((__pSA)->sa_KeyExpirationTimeWithPad.QuadPart < __curtime.QuadPart)) {\
  143. _status = FALSE; \
  144. } else if (((__pSA)->sa_KeyExpirationBytesWithPad.QuadPart > 0i64) && \
  145. ((__pSA)->sa_KeyExpirationBytesWithPad.QuadPart < (__pSA)->sa_TotalBytesTransformed.QuadPart)) { \
  146. _status = FALSE; \
  147. } \
  148. } \
  149. }
  150. #define IPSEC_CHECK_LIFETIME(__pSA, _status, _index) { \
  151. LARGE_INTEGER __curtime; \
  152. (_status) = TRUE; \
  153. if ((__pSA)->sa_ReplaySendSeq[_index] == MAX_ULONG) { \
  154. _status = FALSE; \
  155. } else { \
  156. KeQuerySystemTime(&__curtime); \
  157. if (((__pSA)->sa_KeyExpirationTime.QuadPart > 0i64) && \
  158. ((__pSA)->sa_KeyExpirationTime.QuadPart < __curtime.QuadPart)) { \
  159. _status = FALSE; \
  160. } else if (((__pSA)->sa_KeyExpirationBytes.QuadPart > 0i64) && \
  161. ((__pSA)->sa_KeyExpirationBytes.QuadPart < (__pSA)->sa_TotalBytesTransformed.QuadPart)) { \
  162. _status = FALSE; \
  163. } \
  164. } \
  165. }
  166. #define IPSEC_SA_EXPIRED(__pSA, __fexpired) { \
  167. LARGE_INTEGER __curtime; \
  168. KeQuerySystemTime(&__curtime); \
  169. (__fexpired) = FALSE; \
  170. __curtime.QuadPart -= pSA->sa_LastUsedTime.QuadPart;\
  171. if (__pSA->sa_IdleTime.QuadPart < __curtime.QuadPart) { \
  172. __fexpired = TRUE; \
  173. } \
  174. }
  175. //
  176. // Max tolerated collisions when trying to allocate SPIs.
  177. //
  178. #define MAX_SPI_RETRIES 50
  179. #define IPSEC_SPI_TO_ENTRY(_spi, _entry, _dst) { \
  180. KIRQL kIrql; \
  181. AcquireReadLock(&g_ipsec.SPIListLock, &kIrql); \
  182. *(_entry) = IPSecLookupSABySPIWithLock(_spi, _dst); \
  183. if (*(_entry)) { \
  184. IPSecRefSA((*(_entry))); \
  185. } \
  186. ReleaseReadLock(&g_ipsec.SPIListLock, kIrql); \
  187. }
  188. //
  189. // Generic memory allocators
  190. //
  191. #define IPSecAllocatePktInfo(__tag) \
  192. IPSecAllocateMemory(sizeof(NDIS_IPSEC_PACKET_INFO), __tag)
  193. #define IPSecFreePktInfo(__p) \
  194. IPSecFreeMemory(__p)
  195. #define IPSecAllocatePktExt(__tag) \
  196. IPSecAllocateMemory(sizeof(NDIS_IPSEC_PACKET_INFO), __tag)
  197. #define IPSecFreePktExt(__p) \
  198. IPSecFreeMemory(__p)
  199. #define IPSecAllocateBuffer(_ntstatus, _ppBuf, _ppData, _size, _tag) { \
  200. PIPSEC_LA_BUFFER __labuf; \
  201. *(_ntstatus) = STATUS_SUCCESS; \
  202. __labuf = IPSecGetBuffer(_size, _tag); \
  203. if (__labuf) { \
  204. if (ARGUMENT_PRESENT(_ppData)) { \
  205. *(PVOID *)(_ppData) = __labuf->Buffer; \
  206. } \
  207. *(_ppBuf) = __labuf->Mdl; \
  208. NdisAdjustBufferLength(__labuf->Mdl, _size); \
  209. NDIS_BUFFER_LINKAGE(__labuf->Mdl) = NULL; \
  210. } else { \
  211. *(_ntstatus) = STATUS_INSUFFICIENT_RESOURCES; \
  212. } \
  213. }
  214. #define IPSecFreeBuffer(_ntstatus, _pBuf) { \
  215. PIPSEC_LA_BUFFER __buffer; \
  216. *(_ntstatus) = STATUS_SUCCESS; \
  217. __buffer = CONTAINING_RECORD((_pBuf), IPSEC_LA_BUFFER, Data); \
  218. IPSecReturnBuffer(__buffer); \
  219. }
  220. #define IPSecAllocateSendCompleteCtx(__tag) \
  221. ExAllocateFromNPagedLookasideList(&g_ipsec.IPSecLookasideLists->SendCompleteCtxList)
  222. #define IPSecFreeSendCompleteCtx(_buffer) \
  223. ExFreeToNPagedLookasideList(&g_ipsec.IPSecLookasideLists->SendCompleteCtxList, _buffer); \
  224. IPSEC_DECREMENT(g_ipsec.NumSends);
  225. #define IPSEC_GET_TOTAL_LEN(_pbuf, _plen) { \
  226. PNDIS_BUFFER _ptemp = (PNDIS_BUFFER)(_pbuf); \
  227. *(_plen) = 0; \
  228. while (_ptemp) { \
  229. *(_plen) += _ptemp->ByteCount; \
  230. _ptemp = NDIS_BUFFER_LINKAGE(_ptemp); \
  231. } \
  232. }
  233. #define IPSEC_GET_TOTAL_LEN_RCV_BUF(_pbuf, _plen) { \
  234. IPRcvBuf *_ptemp = (_pbuf); \
  235. *(_plen) = 0; \
  236. while (_ptemp) { \
  237. *(_plen) += _ptemp->ipr_size; \
  238. _ptemp = IPSEC_BUFFER_LINKAGE(_ptemp); \
  239. } \
  240. }
  241. //
  242. // Copy len bytes from RcvBuf chain to mdl (only one MDL)
  243. //
  244. #define IPSEC_COPY_FROM_RCVBUF(_pMdl, _pRcvBuf, _len, _offset) { \
  245. IPRcvBuf *__pBuf=(_pRcvBuf); \
  246. PMDL __pMdl=(_pMdl); \
  247. ULONG __srclen; \
  248. ULONG __destlen; \
  249. ULONG __totallen=0; \
  250. PUCHAR __pSrc; \
  251. PUCHAR __pDest; \
  252. ULONG __curroffset = (_offset); \
  253. IPSecQueryNdisBuf(__pMdl, &__pDest, &__destlen); \
  254. while (__pBuf) { \
  255. IPSecQueryRcvBuf(__pBuf, &__pSrc, &__srclen); \
  256. if (__srclen > __curroffset) { \
  257. RtlCopyMemory(__pDest, __pSrc+__curroffset, __srclen-__curroffset); \
  258. __pDest += (__srclen - __curroffset); \
  259. __totallen += (__srclen - __curroffset); \
  260. } \
  261. __curroffset = 0; \
  262. __pBuf = IPSEC_BUFFER_LINKAGE(__pBuf); \
  263. } \
  264. ASSERT(__totallen == __destlen); \
  265. }
  266. //
  267. // Copy len bytes from Ndis Buffer chain to mdl (only one MDL)
  268. //
  269. #define IPSEC_COPY_FROM_NDISBUF(_pMdl, _pRcvBuf, _len, _offset) { \
  270. NDIS_BUFFER *__pBuf=(_pRcvBuf); \
  271. PMDL __pMdl=(_pMdl); \
  272. ULONG __srclen; \
  273. ULONG __destlen; \
  274. ULONG __totallen=0; \
  275. PUCHAR __pSrc; \
  276. PUCHAR __pDest; \
  277. ULONG __curroffset = (_offset); \
  278. IPSecQueryNdisBuf(__pMdl, &__pDest, &__destlen); \
  279. while (__pBuf) { \
  280. IPSecQueryNdisBuf(__pBuf, &__pSrc, &__srclen); \
  281. if (__srclen > __curroffset) { \
  282. RtlCopyMemory(__pDest, __pSrc+__curroffset, __srclen-__curroffset); \
  283. __pDest += (__srclen - __curroffset); \
  284. __totallen += (__srclen - __curroffset); \
  285. } \
  286. __curroffset = 0; \
  287. __pBuf = NDIS_BUFFER_LINKAGE(__pBuf); \
  288. } \
  289. ASSERT(__totallen == __destlen); \
  290. }
  291. #define IPSecAllocateMemory(_size, _tag) \
  292. ExAllocatePoolWithTag (NonPagedPool, _size, _tag)
  293. #define IPSecAllocateMemoryLowPriority(_size, _tag) \
  294. ExAllocatePoolWithTagPriority (NonPagedPool, _size, _tag, LowPoolPriority)
  295. #define IPSecFreeMemory(_addr) ExFreePool (_addr)
  296. #define IPSecQueryNdisBuf(_Buffer, _VirtualAddress, _Length) \
  297. { \
  298. PNDIS_BUFFER __Mdl = (PNDIS_BUFFER) (_Buffer); \
  299. if (ARGUMENT_PRESENT(_VirtualAddress)) { \
  300. *(PVOID *)(_VirtualAddress) = (__Mdl)->MappedSystemVa; \
  301. } \
  302. *(_Length) = (__Mdl)->ByteCount; \
  303. }
  304. #define IPSecQueryRcvBuf(_Buffer, _VirtualAddress, _Length) \
  305. { \
  306. IPRcvBuf *__buf = (IPRcvBuf *) (_Buffer); \
  307. if (ARGUMENT_PRESENT(_VirtualAddress)) { \
  308. *(PVOID *)(_VirtualAddress) = (__buf)->ipr_buffer; \
  309. } \
  310. *(_Length) = (__buf)->ipr_size; \
  311. }
  312. #define IPSEC_ADJUST_BUFFER_LEN(_pBuf, _len) \
  313. ((IPRcvBuf *)(_pBuf))->ipr_size = (_len)
  314. #define IPSEC_ADJUST_BUFFER(_pBuf, _offset) \
  315. ((IPRcvBuf *)(_pBuf))->ipr_buffer += (_offset)
  316. #define IPSEC_ADJUST_BUFFER_RCVOFFSET(_pBuf, _offset) \
  317. ((IPRcvBuf *)(_pBuf))->ipr_RcvOffset += (_offset)
  318. #define IPSEC_BUFFER_LINKAGE(_pBuf) \
  319. ((IPRcvBuf *)(_pBuf))->ipr_next
  320. #define IPSEC_BUFFER_LEN(_pBuf) \
  321. ((IPRcvBuf *)(_pBuf))->ipr_size
  322. #define IPSEC_BUFFER_OWNER(_pBuf) \
  323. (((IPRcvBuf *)(_pBuf))->ipr_owner)
  324. #define IPSEC_SET_OFFSET_IN_BUFFER(_pBuf, _offset) { \
  325. PUCHAR _p; \
  326. LONG _len; \
  327. IPSecQueryRcvBuf((_pBuf), &(_p), &(_len)); \
  328. if ((_offset) > 0 && (_offset) < (_len)) { \
  329. if (IPSEC_BUFFER_OWNER(_pBuf) == IPR_OWNER_FIREWALL) { \
  330. IPSecMoveMemory((_p), (_p) + (_offset), (_len) - (_offset));\
  331. } else { \
  332. IPSEC_ADJUST_BUFFER((_pBuf), (_offset)); \
  333. IPSEC_ADJUST_BUFFER_RCVOFFSET((_pBuf), (_offset)); \
  334. } \
  335. IPSEC_ADJUST_BUFFER_LEN((_pBuf), (_len) - (_offset)); \
  336. } else { \
  337. ASSERT(FALSE); \
  338. return STATUS_INVALID_PARAMETER; \
  339. } \
  340. }
  341. #define IPSEC_ADD_VALUE(_val, _inc) InterlockedExchangeAdd((PULONG)&(_val), _inc)
  342. #define IPSEC_INCREMENT(_val) InterlockedIncrement(&(_val))
  343. #define IPSEC_DECREMENT(_val) InterlockedDecrement(&(_val))
  344. #define IPSEC_GET_VALUE(_val) InterlockedExchangeAdd((PULONG)&(_val), 0)
  345. #define IPSEC_SET_VALUE(_target, _val) \
  346. InterlockedExchange((PULONG)&(_target), _val)
  347. #define IPSEC_DRIVER_IS_EMPTY() (g_ipsec.NumPolicies == 0)
  348. #define IPSEC_DRIVER_IS_INACTIVE() (g_ipsec.DriverUnloading || !g_ipsec.InitTcpip)
  349. #define IPSEC_DRIVER_UNLOADING() (g_ipsec.DriverUnloading)
  350. #define IPSEC_DRIVER_BOUND() (g_ipsec.BoundToIP)
  351. #define IPSEC_DRIVER_SEND_BOUND() (g_ipsec.SendBoundToIP)
  352. #define IPSEC_DRIVER_INIT_CRYPTO() (g_ipsec.InitCrypto)
  353. #define IPSEC_DRIVER_INIT_RNG() (g_ipsec.InitRNG)
  354. #define IPSEC_DRIVER_INIT_TCPIP() (g_ipsec.InitTcpip)
  355. #if FIPS
  356. #define IPSEC_DRIVER_INIT_FIPS() (g_ipsec.InitFips)
  357. #endif
  358. #if GPC
  359. #define IPSEC_DRIVER_INIT_GPC() (g_ipsec.InitGpc)
  360. #endif
  361. #if DBG
  362. #define IPSecRemoveEntryList(_x) \
  363. { \
  364. RemoveEntryList(_x); \
  365. (_x)->Flink = (_x)->Blink = (PLIST_ENTRY)__LINE__; \
  366. }
  367. #else
  368. #define IPSecRemoveEntryList(_x) RemoveEntryList(_x)
  369. #endif
  370. //
  371. // macros for filter list management
  372. //
  373. #define IS_TRANSPORT_FILTER(f) (!(f)->TunnelFilter)
  374. #define IS_TUNNEL_FILTER(f) ((f)->TunnelFilter)
  375. #define IS_INBOUND_FILTER(f) ((f)->Flags & FILTER_FLAGS_INBOUND)
  376. #define IS_OUTBOUND_FILTER(f) ((f)->Flags & FILTER_FLAGS_OUTBOUND)
  377. #define IS_EXEMPT_FILTER(f) (((f)->Flags & FILTER_FLAGS_DROP) || ((f)->Flags & FILTER_FLAGS_PASS_THRU))
  378. #define IS_MULTICAST_FILTER(f) (IS_CLASSD(NET_LONG((f)->SRC_ADDR)) || \
  379. IS_CLASSD(NET_LONG((f)->DEST_ADDR)))
  380. __inline
  381. PLIST_ENTRY
  382. IPSecResolveFilterList(
  383. IN BOOLEAN fTunnel,
  384. IN BOOLEAN fOutbound
  385. )
  386. {
  387. PLIST_ENTRY pEntry;
  388. if (fTunnel) {
  389. if (fOutbound) {
  390. pEntry = &g_ipsec.FilterList[OUTBOUND_TUNNEL_FILTER];
  391. } else {
  392. pEntry = &g_ipsec.FilterList[INBOUND_TUNNEL_FILTER];
  393. }
  394. } else {
  395. if (fOutbound) {
  396. pEntry = &g_ipsec.FilterList[OUTBOUND_TRANSPORT_FILTER];
  397. } else {
  398. pEntry = &g_ipsec.FilterList[INBOUND_TRANSPORT_FILTER];
  399. }
  400. }
  401. return pEntry;
  402. }
  403. //
  404. // Filter/SA Cache Table
  405. //
  406. #define CacheMatch(uliAddr, uliPort, pInCache) \
  407. ((uliAddr).QuadPart == pInCache->uliSrcDstAddr.QuadPart) && \
  408. ((uliPort).QuadPart == pInCache->uliProtoSrcDstPort.QuadPart)
  409. #define IS_VALID_CACHE_ENTRY(_entry) ((_entry)->pFilter != NULL)
  410. #define INVALIDATE_CACHE_ENTRY(_entry) \
  411. { \
  412. ((PFILTER_CACHE)(_entry))->pSAEntry = NULL; \
  413. ((PFILTER_CACHE)(_entry))->pNextSAEntry = NULL; \
  414. } \
  415. __inline
  416. ULONG
  417. FASTCALL
  418. CalcCacheIndex(
  419. IN IPAddr SrcAddr,
  420. IN IPAddr DestAddr,
  421. IN UCHAR Protocol,
  422. IN USHORT SrcPort,
  423. IN USHORT DestPort,
  424. IN BOOLEAN fOutbound
  425. )
  426. {
  427. REGISTER ULONG dwIndex;
  428. REGISTER ULONG Address;
  429. REGISTER USHORT Port;
  430. Address = SrcAddr ^ DestAddr;
  431. Port = SrcPort ^ DestPort;
  432. dwIndex = NET_TO_HOST_LONG(Address);
  433. dwIndex += Protocol;
  434. dwIndex += NET_TO_HOST_SHORT(Port);
  435. dwIndex %= g_ipsec.CacheHalfSize;
  436. if (fOutbound) {
  437. dwIndex += g_ipsec.CacheHalfSize;
  438. }
  439. return dwIndex;
  440. }
  441. __inline
  442. VOID
  443. CacheUpdate(
  444. IN ULARGE_INTEGER uliAddr,
  445. IN ULARGE_INTEGER uliPort,
  446. IN PVOID _pCtxt1,
  447. IN PVOID _pCtxt2,
  448. IN ULONG dwId,
  449. IN BOOLEAN fFilter
  450. )
  451. {
  452. PFILTER_CACHE __pCache;
  453. PFILTER_CACHE __pTempCache;
  454. PFILTER __pFilter;
  455. PSA_TABLE_ENTRY __pSA;
  456. PSA_TABLE_ENTRY __pNextSA;
  457. __pCache = g_ipsec.ppCache[(dwId)];
  458. if (IS_VALID_CACHE_ENTRY(__pCache)) {
  459. if (__pCache->FilterEntry) {
  460. __pCache->pFilter->FilterCache = NULL;
  461. } else {
  462. __pCache->pSAEntry->sa_FilterCache = NULL;
  463. if (__pCache->pNextSAEntry) {
  464. __pCache->pNextSAEntry->sa_FilterCache = NULL;
  465. __pCache->pNextSAEntry = NULL;
  466. }
  467. }
  468. }
  469. if (fFilter) {
  470. __pFilter = (PFILTER)(_pCtxt1);
  471. if (__pFilter->FilterCache) {
  472. INVALIDATE_CACHE_ENTRY(__pFilter->FilterCache);
  473. __pFilter->FilterCache = NULL;
  474. }
  475. __pCache->uliSrcDstAddr = (uliAddr);
  476. __pCache->uliProtoSrcDstPort = (uliPort);
  477. __pCache->FilterEntry = TRUE;
  478. __pCache->pFilter = __pFilter;
  479. __pFilter->FilterCache = __pCache;
  480. } else {
  481. __pSA = (PSA_TABLE_ENTRY)(_pCtxt1);
  482. __pNextSA = (PSA_TABLE_ENTRY)(_pCtxt2);
  483. if ((__pTempCache = __pSA->sa_FilterCache)) {
  484. __pTempCache->pSAEntry->sa_FilterCache = NULL;
  485. if (__pTempCache->pNextSAEntry) {
  486. __pTempCache->pNextSAEntry->sa_FilterCache = NULL;
  487. }
  488. INVALIDATE_CACHE_ENTRY(__pTempCache);
  489. }
  490. if (__pNextSA && (__pTempCache = __pNextSA->sa_FilterCache)) {
  491. __pTempCache->pSAEntry->sa_FilterCache = NULL;
  492. if (__pTempCache->pNextSAEntry) {
  493. __pTempCache->pNextSAEntry->sa_FilterCache = NULL;
  494. }
  495. INVALIDATE_CACHE_ENTRY(__pTempCache);
  496. }
  497. __pCache->uliSrcDstAddr = (uliAddr);
  498. __pCache->uliProtoSrcDstPort = (uliPort);
  499. __pCache->FilterEntry = FALSE;
  500. __pCache->pSAEntry = __pSA;
  501. __pSA->sa_FilterCache = __pCache;
  502. if (__pNextSA) {
  503. __pCache->pNextSAEntry = __pNextSA;
  504. __pNextSA->sa_FilterCache = __pCache;
  505. }
  506. }
  507. }
  508. __inline
  509. VOID
  510. IPSecInvalidateSACacheEntry(
  511. IN PSA_TABLE_ENTRY pSA
  512. )
  513. {
  514. PFILTER_CACHE pCache;
  515. pCache = pSA->sa_FilterCache;
  516. if (pCache) {
  517. ASSERT(IS_VALID_CACHE_ENTRY(pCache));
  518. ASSERT(pSA == pCache->pSAEntry || pSA == pCache->pNextSAEntry);
  519. pCache->pSAEntry->sa_FilterCache = NULL;
  520. if (pCache->pNextSAEntry) {
  521. pCache->pNextSAEntry->sa_FilterCache = NULL;
  522. }
  523. INVALIDATE_CACHE_ENTRY(pCache);
  524. }
  525. }
  526. __inline
  527. VOID
  528. IPSecInvalidateFilterCacheEntry(
  529. IN PFILTER pFilter
  530. )
  531. {
  532. PFILTER_CACHE pCache;
  533. pCache = pFilter->FilterCache;
  534. if (pCache) {
  535. ASSERT(IS_VALID_CACHE_ENTRY(pCache));
  536. ASSERT(IS_EXEMPT_FILTER(pFilter));
  537. pFilter->FilterCache = NULL;
  538. INVALIDATE_CACHE_ENTRY(pCache);
  539. }
  540. }
  541. __inline
  542. VOID
  543. IPSecStartSATimer(
  544. IN PSA_TABLE_ENTRY pSA,
  545. IN IPSEC_TIMEOUT_HANDLER TimeoutHandler,
  546. IN ULONG SecondsToGo
  547. )
  548. {
  549. if (pSA->sa_Flags & FLAGS_SA_TIMER_STARTED) {
  550. if (IPSecStopTimer(&pSA->sa_Timer)) {
  551. IPSecStartTimer(&pSA->sa_Timer,
  552. TimeoutHandler,
  553. SecondsToGo,
  554. (PVOID)pSA);
  555. }
  556. } else {
  557. pSA->sa_Flags |= FLAGS_SA_TIMER_STARTED;
  558. IPSecStartTimer(&pSA->sa_Timer,
  559. TimeoutHandler,
  560. SecondsToGo,
  561. (PVOID)pSA);
  562. }
  563. }
  564. __inline
  565. VOID
  566. IPSecStopSATimer(
  567. IN PSA_TABLE_ENTRY pSA
  568. )
  569. {
  570. if (pSA->sa_Flags & FLAGS_SA_TIMER_STARTED) {
  571. pSA->sa_Flags &= ~FLAGS_SA_TIMER_STARTED;
  572. IPSecStopTimer(&pSA->sa_Timer);
  573. }
  574. }
  575. __inline
  576. VOID
  577. IPSecStopTimerDerefSA(
  578. IN PSA_TABLE_ENTRY pSA
  579. )
  580. {
  581. if (pSA->sa_Flags & FLAGS_SA_TIMER_STARTED) {
  582. if (IPSecStopTimer(&pSA->sa_Timer)) {
  583. pSA->sa_Flags &= ~FLAGS_SA_TIMER_STARTED;
  584. IPSecDerefSA(pSA);
  585. }
  586. } else {
  587. IPSecDerefSA(pSA);
  588. }
  589. }
  590. __inline
  591. VOID
  592. IPSecDerefSANextSA(
  593. IN PSA_TABLE_ENTRY pSA,
  594. IN PSA_TABLE_ENTRY pNextSA
  595. )
  596. {
  597. if (pNextSA) {
  598. IPSecDerefSA(pNextSA);
  599. }
  600. IPSecDerefSA(pSA);
  601. }
  602. __inline
  603. VOID
  604. IPSecRemoveSPIEntry(
  605. IN PSA_TABLE_ENTRY pSA
  606. )
  607. {
  608. if (pSA->sa_Flags & FLAGS_SA_ON_SPI_HASH) {
  609. IPSecRemoveEntryList(&pSA->sa_SPILinkage);
  610. pSA->sa_Flags &= ~FLAGS_SA_ON_SPI_HASH;
  611. }
  612. }
  613. //
  614. // Packs the src/dest IP addrs in a large integer
  615. //
  616. #define IPSEC_BUILD_SRC_DEST_ADDR(_li, _src, _dest) { \
  617. (_li).LowPart = _src; \
  618. (_li).HighPart = _dest; \
  619. }
  620. #define IPSEC_BUILD_SRC_DEST_MASK IPSEC_BUILD_SRC_DEST_ADDR
  621. //
  622. // Packs the Proto and Src/Dest ports into a large int
  623. //
  624. //
  625. // Ports make sense only for TCP and UDP
  626. //
  627. //
  628. // TCP/UDP header
  629. // 0 15 16 31
  630. // |----|----|----|----|----|----|----|----|
  631. // | Source Port | Dst Port |
  632. //
  633. #define IPSEC_BUILD_PROTO_PORT_LI(_li, _proto, _sport, _dport) { \
  634. (_li).LowPart = \
  635. MAKELONG(MAKEWORD((_proto),0x00),0x0000); \
  636. switch((_li).LowPart) { \
  637. case 6: \
  638. case 17: { \
  639. (_li).HighPart = MAKELONG((_sport),(_dport)); \
  640. break; \
  641. } \
  642. default: { \
  643. (_li).HighPart = 0x00000000; \
  644. break; \
  645. } \
  646. } \
  647. }
  648. #define IPSecRefFilter(__pfilter) IPSEC_INCREMENT((__pfilter)->Reference)
  649. #define IPSecFreeFilter(__pfilter) IPSecFreeMemory(__pfilter)
  650. #define IPSecDerefFilter(__pfilter) \
  651. { \
  652. if (IPSEC_DECREMENT((__pfilter)->Reference) == 0) { \
  653. IPSecFreeFilter(__pfilter); \
  654. } \
  655. }
  656. #define IPSecAllocateKeyBuffer(_size) IPSecAllocateMemory(_size, IPSEC_TAG_KEY)
  657. #define IPSecFreeKeyBuffer(_key) IPSecFreeMemory(_key)
  658. #define IPSecAllocateLogBuffer(_size) IPSecAllocateMemory(_size, IPSEC_TAG_LOG)
  659. #define IPSecFreeLogBuffer(_key) IPSecFreeMemory(_key)
  660. #define IPSecFreeSA(_sa) { \
  661. LONG _i; \
  662. for (_i=0; _i<(_sa)->sa_NumOps; _i++) { \
  663. if ((_sa)->INT_KEY(_i)) { \
  664. IPSecFreeKeyBuffer((_sa)->INT_KEY(_i)); \
  665. } \
  666. if ((_sa)->CONF_KEY(_i)) { \
  667. IPSecFreeKeyBuffer((_sa)->CONF_KEY(_i));\
  668. } \
  669. } \
  670. IPSecFreeMemory(_sa); \
  671. }
  672. #define IPSecGetAcquireContext() IPSecAllocateMemory(sizeof(IPSEC_ACQUIRE_CONTEXT), IPSEC_TAG_ACQUIRE_CTX)
  673. #define IPSecFreeAcquireContext(_ctx) IPSecFreeMemory(_ctx)
  674. #define IPSecGetNotifyExpire() IPSecAllocateMemory(sizeof(IPSEC_NOTIFY_EXPIRE), IPSEC_TAG_ACQUIRE_CTX)
  675. //
  676. // Hashes <SPI, Dest>
  677. //
  678. #define IPSEC_HASH_SPI(_dest, _spi, _phash) { \
  679. DWORD dwIndex; \
  680. dwIndex = NET_TO_HOST_LONG(_dest) + (_spi); \
  681. dwIndex %= g_ipsec.SAHashSize; \
  682. pHash = &g_ipsec.pSADb[dwIndex]; \
  683. IPSEC_DEBUG(ALL, ("SPI: Index: %lx for S: %lx, D: %lx, pHash: %lx\n", \
  684. dwIndex, \
  685. (_spi), \
  686. (_dest), \
  687. pHash)); \
  688. }
  689. //
  690. // Hashes <Src, Dest>
  691. //
  692. #define IPSEC_HASH_ADDR(_src, _dest,_phash) { \
  693. DWORD dwIndex; \
  694. dwIndex = (_src)+(_dest); \
  695. dwIndex %= g_ipsec.SpFilterHashSize; \
  696. pHash = &g_ipsec.pSpFilter[dwIndex]; \
  697. IPSEC_DEBUG(ALL, ("ADDR: Index: %lx for S: %lx, D: %lx, pHash: %lx\n", \
  698. dwIndex, \
  699. (_src), \
  700. (_dest), \
  701. pHash)); \
  702. }
  703. #define SRC_ADDR uliSrcDstAddr.LowPart
  704. #define DEST_ADDR uliSrcDstAddr.HighPart
  705. #define SRC_MASK uliSrcDstMask.LowPart
  706. #define DEST_MASK uliSrcDstMask.HighPart
  707. #define PROTO uliProtoSrcDstPort.LowPart
  708. #define SRC_PORT LOWORD(uliProtoSrcDstPort.HighPart)
  709. #define DEST_PORT HIWORD(uliProtoSrcDstPort.HighPart)
  710. #define FI_SRC_PORT(_filter) LOWORD((_filter)->uliProtoSrcDstPort.HighPart)
  711. #define FI_DEST_PORT(_filter) HIWORD((_filter)->uliProtoSrcDstPort.HighPart)
  712. #define SA_SRC_ADDR sa_uliSrcDstAddr.LowPart
  713. #define SA_DEST_ADDR sa_uliSrcDstAddr.HighPart
  714. #define SA_SRC_MASK sa_uliSrcDstMask.LowPart
  715. #define SA_DEST_MASK sa_uliSrcDstMask.HighPart
  716. #define SA_PROTO sa_uliProtoSrcDstPort.LowPart
  717. #define SA_SRC_PORT(_psa) LOWORD((_psa)->sa_uliProtoSrcDstPort.HighPart)
  718. #define SA_DEST_PORT(_psa) HIWORD((_psa)->sa_uliProtoSrcDstPort.HighPart)
  719. #define FILTER_PROTO(ProtoId) MAKELONG(MAKEWORD((ProtoId),0x00),0x00000)
  720. #define FILTER_PROTO_ANY FILTER_PROTO(0x00)
  721. #define FILTER_PROTO_ICMP FILTER_PROTO(0x01)
  722. #define FILTER_PROTO_TCP FILTER_PROTO(0x06)
  723. #define FILTER_PROTO_UDP FILTER_PROTO(0x11)
  724. #define FILTER_TCPUDP_PORT_ANY (WORD)0x0000
  725. #define FILTER_ICMP_TYPE_ANY (BYTE)0xff
  726. #define FILTER_ICMP_CODE_ANY (BYTE)0xff
  727. #define FILTER_MASK_ALL (DWORD)0xffffffff
  728. #define FILTER_MASK_NONE (DWORD)0x00000000
  729. //
  730. // macros to parse the ALGORITHM structure
  731. //
  732. #define INT_ALGO(_i) sa_Algorithm[_i].integrityAlgo.algoIdentifier
  733. #define INT_KEY(_i) sa_Algorithm[_i].integrityAlgo.algoKey
  734. #define INT_KEYLEN(_i) sa_Algorithm[_i].integrityAlgo.algoKeylen
  735. #define INT_ROUNDS(_i) sa_Algorithm[_i].integrityAlgo.algoRounds
  736. #define CONF_ALGO(_i) sa_Algorithm[_i].confAlgo.algoIdentifier
  737. #define CONF_KEY(_i) sa_Algorithm[_i].confAlgo.algoKey
  738. #define CONF_KEYLEN(_i) sa_Algorithm[_i].confAlgo.algoKeylen
  739. #define CONF_ROUNDS(_i) sa_Algorithm[_i].confAlgo.algoRounds
  740. #define COMP_ALGO(_i) sa_Algorithm[_i].compAlgo.algoIdentifier
  741. #define EXT_INT_ALGO IntegrityAlgo.algoIdentifier
  742. #define EXT_INT_KEY IntegrityAlgo.algoKey
  743. #define EXT_INT_KEYLEN IntegrityAlgo.algoKeylen
  744. #define EXT_INT_ROUNDS IntegrityAlgo.algoRounds
  745. #define EXT_CONF_ALGO ConfAlgo.algoIdentifier
  746. #define EXT_CONF_KEY ConfAlgo.algoKey
  747. #define EXT_CONF_KEYLEN ConfAlgo.algoKeylen
  748. #define EXT_CONF_ROUNDS ConfAlgo.algoRounds
  749. #define EXT_INT_ALGO_EX(_i) AlgoInfo[_i].IntegrityAlgo.algoIdentifier
  750. #define EXT_INT_KEYLEN_EX(_i) AlgoInfo[_i].IntegrityAlgo.algoKeylen
  751. #define EXT_INT_ROUNDS_EX(_i) AlgoInfo[_i].IntegrityAlgo.algoRounds
  752. #define EXT_CONF_ALGO_EX(_i) AlgoInfo[_i].ConfAlgo.algoIdentifier
  753. #define EXT_CONF_KEYLEN_EX(_i) AlgoInfo[_i].ConfAlgo.algoKeylen
  754. #define EXT_CONF_ROUNDS_EX(_i) AlgoInfo[_i].ConfAlgo.algoRounds
  755. #define IS_AH_SA(_psa) ((_psa)->sa_Operation[0] == Auth || \
  756. (_psa)->sa_Operation[1] == Auth || \
  757. (_psa)->sa_Operation[2] == Auth)
  758. #define IS_ESP_SA(_psa) ((_psa)->sa_Operation[0] == Encrypt || \
  759. (_psa)->sa_Operation[1] == Encrypt || \
  760. (_psa)->sa_Operation[2] == Encrypt)
  761. //
  762. // Increment/decrement statistics
  763. //
  764. #define IPSEC_INC_STATISTIC(_stat) \
  765. (g_ipsec.Statistics.##_stat)++;
  766. #define IPSEC_DEC_STATISTIC(_stat) \
  767. (g_ipsec.Statistics.##_stat)--;
  768. #define IPSEC_INC_TUNNELS(_pSA) { \
  769. if ((_pSA)->sa_Flags & FLAGS_SA_TUNNEL) \
  770. g_ipsec.Statistics.dwNumActiveTunnels++; \
  771. }
  772. #define IPSEC_DEC_TUNNELS(_pSA) { \
  773. if ((_pSA)->sa_Flags & FLAGS_SA_TUNNEL) \
  774. g_ipsec.Statistics.dwNumActiveTunnels--; \
  775. }
  776. //
  777. // Macro to read a dword from registry and init the variable passed in.
  778. //
  779. #define IPSEC_REG_READ_DWORD(_hRegKey, _param, _var, _def, _max, _min) { \
  780. NTSTATUS __status; \
  781. __status = GetRegDWORDValue(_hRegKey, \
  782. _param, \
  783. (_var)); \
  784. \
  785. if (!NT_SUCCESS(__status)) { \
  786. *(_var) = _def; \
  787. } else if (*(_var) > _max) { \
  788. *(_var) = _max; \
  789. } else if (*(_var) <= _min) { \
  790. *(_var) = _min; \
  791. } \
  792. }
  793. //
  794. // Macro for computing incremental checksum (RFC 1624)
  795. //
  796. #define UpdateIPLength(_piph, _length) \
  797. { \
  798. ULONG _sum; \
  799. USHORT _old; \
  800. \
  801. _old = NET_SHORT((_piph)->iph_length); \
  802. (_piph)->iph_length = (_length); \
  803. _sum = (~NET_SHORT((_piph)->iph_xsum) & 0xffff) + \
  804. (~_old & 0xffff) + \
  805. NET_SHORT((_piph)->iph_length); \
  806. _sum = (_sum & 0xffff) + (_sum >> 16); \
  807. _sum += (_sum >> 16); \
  808. (_piph)->iph_xsum = NET_SHORT((USHORT)(~_sum & 0xffff)); \
  809. }
  810. #define UpdateIPProtocol(_piph, _proto) \
  811. { \
  812. ULONG _sum; \
  813. USHORT _old; \
  814. \
  815. _old = NET_SHORT(*(USHORT *)&(_piph)->iph_ttl); \
  816. (_piph)->iph_protocol = (_proto); \
  817. _sum = (~NET_SHORT((_piph)->iph_xsum) & 0xffff) + \
  818. (~_old & 0xffff) + \
  819. NET_SHORT(*(USHORT *)&(_piph)->iph_ttl); \
  820. _sum = (_sum & 0xffff) + (_sum >> 16); \
  821. _sum += (_sum >> 16); \
  822. (_piph)->iph_xsum = NET_SHORT((USHORT)(~_sum & 0xffff)); \
  823. }
  824. #define IPSecPrint4Long(_key) \
  825. DbgPrint("Key: %lx-%lx-%lx-%lx\n", \
  826. *(ULONG *)&(_key)[0], \
  827. *(ULONG *)&(_key)[4], \
  828. *(ULONG *)&(_key)[8], \
  829. *(ULONG *)&(_key)[12]);
  830. #define IPSEC_DELAY_INTERVAL ((LONGLONG)(-1 * 1000 * 1000)) // 1/10 sec.
  831. #define IPSEC_DELAY_EXECUTION() \
  832. { \
  833. IPSecDelayInterval.QuadPart = IPSEC_DELAY_INTERVAL; \
  834. KeDelayExecutionThread(UserMode, FALSE, &IPSecDelayInterval); \
  835. }
  836. #define IS_DRIVER_BLOCK() (g_ipsec.OperationMode == IPSEC_BLOCK_MODE)
  837. #define IS_DRIVER_BYPASS() (g_ipsec.OperationMode == IPSEC_BYPASS_MODE)
  838. #define IS_DRIVER_SECURE() (g_ipsec.OperationMode == IPSEC_SECURE_MODE)
  839. #define IS_DRIVER_DIAGNOSTIC() (g_ipsec.DiagnosticMode)
  840. #define LOG_EVENT NdisWriteEventLogEntry
  841. #define IPSEC_NULL_GUIDS "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
  842. #define IPSEC_EQUAL_GUIDS(_A, _B) \
  843. ((*(UNALIGNED ULONG *)((PUCHAR)(_A)) == *(UNALIGNED ULONG *)((PUCHAR)(_B))) && \
  844. (*(UNALIGNED ULONG *)(((PUCHAR)(_A))+4) == *(UNALIGNED ULONG *)(((PUCHAR)(_B))+4)) && \
  845. (*(UNALIGNED ULONG *)(((PUCHAR)(_A))+8) == *(UNALIGNED ULONG *)(((PUCHAR)(_B))+8)) && \
  846. (*(UNALIGNED ULONG *)(((PUCHAR)(_A))+12) == *(UNALIGNED ULONG *)(((PUCHAR)(_B))+12)))
  847. //
  848. // Get the next non-zero length NDIS buffer.
  849. //
  850. __inline
  851. PNDIS_BUFFER
  852. FASTCALL
  853. IPSEC_NEXT_BUFFER(
  854. IN PNDIS_BUFFER pBuffer
  855. )
  856. {
  857. PVOID *pDummy = NULL;
  858. ULONG Length = 0;
  859. if (!pBuffer) {
  860. ASSERT(FALSE);
  861. return NULL;
  862. }
  863. pBuffer = NDIS_BUFFER_LINKAGE(pBuffer);
  864. while (pBuffer) {
  865. IPSecQueryNdisBuf(pBuffer, &pDummy, &Length);
  866. if (Length == 0) {
  867. pBuffer = NDIS_BUFFER_LINKAGE(pBuffer);
  868. continue;
  869. } else {
  870. return pBuffer;
  871. }
  872. }
  873. return NULL;
  874. }
  875. #define SA_CHAIN_WIDTH 4
  876. //
  877. // Count number of 1's in the IP mask
  878. //
  879. __inline
  880. LONG
  881. CountNumberOfOnes(
  882. IN IPMask IpMask
  883. )
  884. {
  885. INT i;
  886. LONG NumberOfOnes = 0;
  887. IPMask Mask = IpMask;
  888. for (i = 0; i < sizeof(IPMask) * 8; i++) {
  889. if ((Mask & 0x1) == 0x1) {
  890. NumberOfOnes++;
  891. }
  892. Mask = Mask >> 1;
  893. }
  894. return NumberOfOnes;
  895. }
  896. __inline
  897. PLIST_ENTRY
  898. FASTCALL
  899. IPSecResolveSAChain(
  900. IN PFILTER pFilter,
  901. IN IPAddr IpAddr
  902. )
  903. {
  904. PLIST_ENTRY pEntry;
  905. ULONG Index;
  906. if (IS_TUNNEL_FILTER(pFilter)) {
  907. pEntry = &pFilter->SAChain[0];
  908. } else {
  909. Index = NET_TO_HOST_LONG(IpAddr) % pFilter->SAChainSize;
  910. pEntry = &pFilter->SAChain[Index];
  911. }
  912. return pEntry;
  913. }
  914. #if GPC
  915. #define IPSEC_GPC_MASK_ALL (0xff)
  916. #define IPSEC_GPC_MASK_NONE (0x0)
  917. #define GPC_REGISTER_CLIENT g_ipsec.GpcEntries.GpcRegisterClientHandler
  918. #define GPC_DEREGISTER_CLIENT g_ipsec.GpcEntries.GpcDeregisterClientHandler
  919. #define GPC_ADD_CFINFO g_ipsec.GpcEntries.GpcAddCfInfoHandler
  920. #define GPC_REMOVE_CFINFO g_ipsec.GpcEntries.GpcRemoveCfInfoHandler
  921. #define GPC_ADD_PATTERN g_ipsec.GpcEntries.GpcAddPatternHandler
  922. #define GPC_REMOVE_PATTERN g_ipsec.GpcEntries.GpcRemovePatternHandler
  923. #define GPC_CLASSIFY_PATTERN g_ipsec.GpcEntries.GpcClassifyPatternHandler
  924. #define GPC_GET_CLIENT_CONTEXT g_ipsec.GpcEntries.GpcGetCfInfoClientContextHandler
  925. #define IPSEC_GPC_ACTIVE (0x12345678)
  926. #define IPSEC_NUM_GPC_FILTERS() (g_ipsec.NumMaskedFilters)
  927. #define IS_GPC_ACTIVE() (g_ipsec.GpcActive == IPSEC_GPC_ACTIVE)
  928. #define IPSEC_SET_GPC_ACTIVE() \
  929. { \
  930. g_ipsec.GpcActive = IPSEC_GPC_ACTIVE; \
  931. }
  932. #define IPSEC_UNSET_GPC_ACTIVE() \
  933. { \
  934. g_ipsec.GpcActive = 0; \
  935. }
  936. __inline
  937. INT
  938. FASTCALL
  939. IPSecResolveGpcCf(
  940. IN BOOLEAN fOutbound
  941. )
  942. {
  943. return fOutbound? GPC_CF_IPSEC_OUT: GPC_CF_IPSEC_IN;
  944. }
  945. __inline
  946. PLIST_ENTRY
  947. FASTCALL
  948. IPSecResolveGpcFilterList(
  949. IN BOOLEAN fTunnel,
  950. IN BOOLEAN fOutbound
  951. )
  952. {
  953. PLIST_ENTRY pEntry;
  954. if (fTunnel) {
  955. if (fOutbound) {
  956. pEntry = &g_ipsec.GpcFilterList[OUTBOUND_TUNNEL_FILTER];
  957. } else {
  958. pEntry = &g_ipsec.GpcFilterList[INBOUND_TUNNEL_FILTER];
  959. }
  960. } else {
  961. if (fOutbound) {
  962. pEntry = &g_ipsec.GpcFilterList[OUTBOUND_TRANSPORT_FILTER];
  963. } else {
  964. pEntry = &g_ipsec.GpcFilterList[INBOUND_TRANSPORT_FILTER];
  965. }
  966. }
  967. return pEntry;
  968. }
  969. __inline
  970. VOID
  971. IPSEC_CLASSIFY_PACKET(
  972. IN INT GpcCf,
  973. IN ULARGE_INTEGER uliSrcDstAddr,
  974. IN ULARGE_INTEGER uliProtoSrcDstPort,
  975. OUT PFILTER *ppFilter,
  976. OUT PCLASSIFICATION_HANDLE pGpcHandle
  977. )
  978. {
  979. GPC_IP_PATTERN Pattern;
  980. *ppFilter = NULL;
  981. *pGpcHandle = 0;
  982. Pattern.SrcAddr = SRC_ADDR;
  983. Pattern.DstAddr = DEST_ADDR;
  984. Pattern.ProtocolId = (UCHAR)PROTO;
  985. if (PROTO == PROTOCOL_TCP || PROTO == PROTOCOL_UDP) {
  986. Pattern.gpcSrcPort = SRC_PORT;
  987. Pattern.gpcDstPort = DEST_PORT;
  988. } else {
  989. Pattern.gpcSrcPort = 0;
  990. Pattern.gpcDstPort = 0;
  991. }
  992. Pattern.InterfaceId.InterfaceId = 0;
  993. Pattern.InterfaceId.LinkId = 0;
  994. GPC_CLASSIFY_PATTERN( g_ipsec.GpcClients[GpcCf],
  995. GPC_PROTOCOL_TEMPLATE_IP,
  996. &Pattern,
  997. ppFilter,
  998. pGpcHandle,
  999. 0,
  1000. NULL,
  1001. TRUE);
  1002. }
  1003. #define IS_GPC_FILTER(f) (g_ipsec.InitGpc && \
  1004. f->SRC_MASK == FILTER_MASK_ALL && \
  1005. f->DEST_MASK == FILTER_MASK_ALL)
  1006. #endif
  1007. #if FIPS
  1008. #define IPSEC_DES_ALGO FIPS_CBC_DES
  1009. #define IPSEC_3DES_ALGO FIPS_CBC_3DES
  1010. #define IPSEC_SHA_INIT g_ipsec.FipsFunctionTable.FipsSHAInit
  1011. #define IPSEC_SHA_UPDATE g_ipsec.FipsFunctionTable.FipsSHAUpdate
  1012. #define IPSEC_SHA_FINAL g_ipsec.FipsFunctionTable.FipsSHAFinal
  1013. #define IPSEC_DES_KEY g_ipsec.FipsFunctionTable.FipsDesKey
  1014. #define IPSEC_3DES_KEY g_ipsec.FipsFunctionTable.Fips3Des3Key
  1015. #define IPSEC_CBC g_ipsec.FipsFunctionTable.FipsCBC
  1016. #define IPSEC_GEN_RANDOM g_ipsec.FipsFunctionTable.FIPSGenRandom
  1017. #define IPSEC_HMAC_SHA_INIT g_ipsec.FipsFunctionTable.FipsHmacSHAInit
  1018. #define IPSEC_HMAC_SHA_UPDATE g_ipsec.FipsFunctionTable.FipsHmacSHAUpdate
  1019. #define IPSEC_HMAC_SHA_FINAL g_ipsec.FipsFunctionTable.FipsHmacSHAFinal
  1020. #define IPSEC_HMAC_MD5_INIT g_ipsec.FipsFunctionTable.HmacMD5Init
  1021. #define IPSEC_HMAC_MD5_UPDATE g_ipsec.FipsFunctionTable.HmacMD5Update
  1022. #define IPSEC_HMAC_MD5_FINAL g_ipsec.FipsFunctionTable.HmacMD5Final
  1023. #else
  1024. #define IPSEC_DES_ALGO des
  1025. #define IPSEC_3DES_ALGO tripledes
  1026. #define IPSEC_SHA_INIT A_SHAInit
  1027. #define IPSEC_SHA_UPDATE A_SHAUpdate
  1028. #define IPSEC_SHA_FINAL A_SHAFinal
  1029. #define IPSEC_DES_KEY deskey
  1030. #define IPSEC_3DES_KEY tripledes3key
  1031. #define IPSEC_CBC(_EncryptionAlgo, _pOut, _pIn, _pKeyTable, _Operation, _pFeedback) \
  1032. CBC(_EncryptionAlgo, \
  1033. DES_BLOCKLEN, \
  1034. _pOut, \
  1035. _pIn, \
  1036. _pKeyTable, \
  1037. _Operation, \
  1038. _pFeedback)
  1039. #define IPSEC_GEN_RANDOM(_pBuf, _KeySize) NewGenRandom(NULL, NULL, _pBuf, _KeySize)
  1040. #endif
  1041. #define IPSEC_MD5_INIT MD5Init
  1042. #define IPSEC_MD5_UPDATE MD5Update
  1043. #define IPSEC_MD5_FINAL MD5Final
  1044. #define IPSEC_RC4_KEY rc4_key
  1045. #define IPSEC_RC4 rc4
  1046. #define IS_CLASS_D_ADDR(x) (((x) & 0x000000f0) == 0x000000e0)
  1047. #define DEFAULT_IPSEC_OPERATION_MODE IPSEC_BYPASS_MODE
  1048. #define TCPIP_FREE_BUFF g_ipsec.TcpipFreeBuff
  1049. #define TCPIP_ALLOC_BUFF g_ipsec.TcpipAllocBuff
  1050. #define TCPIP_GET_ADDRTYPE g_ipsec.TcpipGetAddrType
  1051. #define TCPIP_GET_INFO g_ipsec.TcpipGetInfo
  1052. #define TCPIP_NDIS_REQUEST g_ipsec.TcpipNdisRequest
  1053. #define TCPIP_REGISTER_PROTOCOL g_ipsec.TcpipRegisterProtocol
  1054. #define TCPIP_SET_IPSEC_STATUS g_ipsec.TcpipSetIPSecStatus
  1055. #define TCPIP_IP_TRANSMIT g_ipsec.TcpipIPTransmit
  1056. #define TCPIP_SET_IPSEC g_ipsec.TcpipSetIPSecPtr
  1057. #define TCPIP_UNSET_IPSEC g_ipsec.TcpipUnSetIPSecPtr
  1058. #define TCPIP_UNSET_IPSEC_SEND g_ipsec.TcpipUnSetIPSecSendPtr
  1059. #define TCPIP_TCP_XSUM g_ipsec.TcpipTCPXsum
  1060. #define TCPIP_GEN_IPID g_ipsec.TcpipGenIpId
  1061. #define TCPIP_DEREGISTER_PROTOCOL g_ipsec.TcpipDeRegisterProtocol
  1062. #ifdef xsum
  1063. #undef xsum
  1064. #define xsum(Buffer, Length) ((USHORT)TCPIP_TCP_XSUM(0, (PUCHAR)(Buffer), (Length)))
  1065. #endif
  1066. #define SAFETY_LEN (TRUNCATED_HASH_LEN+MAX_PAD_LEN)
  1067. //
  1068. // Compares the src/dest ports out of a word with the number input
  1069. //
  1070. #define IPSEC_COMPARE_SD_PORT(_pport, _port) \
  1071. ( ((_pport)[0] == (_port)) || \
  1072. ((_pport)[1] == (_port)))
  1073. #define IPSEC_COMPARE_D_PORT(_pport, _port) ((_pport)[1] == (_port))
  1074. //
  1075. // Bypass traffic logic for IKE, Kerberos and RSVP
  1076. //
  1077. #define IPSEC_KERBEROS_TRAFFIC() \
  1078. ((pIPHeader->iph_protocol == PROTOCOL_UDP || \
  1079. pIPHeader->iph_protocol == PROTOCOL_TCP) && \
  1080. IPSEC_COMPARE_SD_PORT(pwPort, IPSEC_KERBEROS_PORT))
  1081. #define IPSEC_ISAKMP_TRAFFIC() \
  1082. (pIPHeader->iph_protocol == PROTOCOL_UDP && \
  1083. IPSEC_COMPARE_D_PORT(pwPort, IPSEC_ISAKMP_PORT)) \
  1084. #define IPSEC_RSVP_TRAFFIC() \
  1085. (pIPHeader->iph_protocol == PROTOCOL_RSVP)
  1086. #define IPSEC_NO_UNICAST_EXEMPT 0x00000001
  1087. #define IPSEC_NO_MANDBCAST_EXEMPT 0x00000002
  1088. #define IPSEC_NO_DEFAULT_EXEMPT() (g_ipsec.NoDefaultExempt & IPSEC_NO_UNICAST_EXEMPT)
  1089. #define IPSEC_HANDLE_MANDBCAST() (g_ipsec.NoDefaultExempt & IPSEC_NO_MANDBCAST_EXEMPT)
  1090. #define IPSEC_MANDBCAST_PROCESS() (IPSEC_GET_VALUE(g_ipsec.NumMulticastFilters) || \
  1091. IPSEC_HANDLE_MANDBCAST())
  1092. #define IPSEC_BYPASS_TRAFFIC() \
  1093. (IPSEC_ISAKMP_TRAFFIC() || \
  1094. (!IPSEC_NO_DEFAULT_EXEMPT() && \
  1095. (IPSEC_KERBEROS_TRAFFIC() || \
  1096. IPSEC_RSVP_TRAFFIC())))
  1097. //
  1098. // Forwarding path is either reinject a detunneled forward packet or route
  1099. //
  1100. #define IPSEC_FORWARD_PATH() (fFWPacket || (fOutbound && TCPIP_GET_ADDRTYPE(pIPHeader->iph_src) != DEST_LOCAL))
  1101. /*++
  1102. Routine Description:
  1103. Fills in the DELETE_SA hw request from pSA
  1104. Arguments:
  1105. pSA - the SA
  1106. Buf - buffer to set info
  1107. Len - length
  1108. Return Value:
  1109. status of the operation
  1110. --*/
  1111. #define IPSecFillHwDelSA(_pSA, _Buf, _Len) \
  1112. ((POFFLOAD_IPSEC_DELETE_SA)(_Buf))->OffloadHandle = (_pSA)->sa_OffloadHandle;
  1113. #endif _MACROS_H