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.

494 lines
10 KiB

  1. #include "precomp.h"
  2. DWORD
  3. PAAddTnFilterSpecs(
  4. PIPSEC_NFA_DATA pIpsecNFAData
  5. )
  6. {
  7. DWORD dwError = 0;
  8. PIPSEC_NEGPOL_DATA pIpsecNegPolData = NULL;
  9. PIPSEC_FILTER_DATA pIpsecFilterData = NULL;
  10. PQMPOLICYSTATE pQMPolicyState = NULL;
  11. DWORD dwNumFilterSpecs = 0;
  12. PIPSEC_FILTER_SPEC * ppFilterSpecs = NULL;
  13. DWORD i = 0;
  14. PTNFILTERSTATE pTnFilterState = NULL;
  15. PTUNNEL_FILTER pSPDTnFilter = NULL;
  16. LPWSTR pServerName = NULL;
  17. DWORD dwPersist = 0;
  18. pIpsecNegPolData = pIpsecNFAData->pIpsecNegPolData;
  19. if (!memcmp(
  20. &(pIpsecNegPolData->NegPolType),
  21. &(GUID_NEGOTIATION_TYPE_DEFAULT),
  22. sizeof(GUID))) {
  23. dwError = ERROR_SUCCESS;
  24. return (dwError);
  25. }
  26. pIpsecFilterData = pIpsecNFAData->pIpsecFilterData;
  27. if (!pIpsecFilterData) {
  28. dwError = ERROR_SUCCESS;
  29. return (dwError);
  30. }
  31. pQMPolicyState = FindQMPolicyState(
  32. pIpsecNegPolData->NegPolIdentifier
  33. );
  34. if (!pQMPolicyState) {
  35. dwError = ERROR_INVALID_PARAMETER;
  36. return (dwError);
  37. }
  38. if (!IsClearOnly(pQMPolicyState->gNegPolAction) &&
  39. !IsBlocking(pQMPolicyState->gNegPolAction) &&
  40. !(pQMPolicyState->bInSPD)) {
  41. dwError = ERROR_INVALID_PARAMETER;
  42. return (dwError);
  43. }
  44. dwNumFilterSpecs = pIpsecFilterData->dwNumFilterSpecs;
  45. ppFilterSpecs = pIpsecFilterData->ppFilterSpecs;
  46. for (i = 0; i < dwNumFilterSpecs; i++) {
  47. dwError = PACreateTnFilterState(
  48. pIpsecNegPolData,
  49. pIpsecNFAData,
  50. *(ppFilterSpecs + i),
  51. &pTnFilterState
  52. );
  53. if (dwError) {
  54. continue;
  55. }
  56. dwError = PACreateTnFilter(
  57. pIpsecNegPolData,
  58. pIpsecNFAData,
  59. *(ppFilterSpecs + i),
  60. pQMPolicyState,
  61. &pSPDTnFilter
  62. );
  63. if (dwError) {
  64. pTnFilterState->hTnFilter = NULL;
  65. pTnFilterState->pNext = gpTnFilterState;
  66. gpTnFilterState = pTnFilterState;
  67. continue;
  68. }
  69. dwError = AddTunnelFilter(
  70. pServerName,
  71. dwPersist,
  72. pSPDTnFilter,
  73. &(pTnFilterState->hTnFilter)
  74. );
  75. pTnFilterState->pNext = gpTnFilterState;
  76. gpTnFilterState = pTnFilterState;
  77. PAFreeTnFilter(pSPDTnFilter);
  78. }
  79. return (dwError);
  80. }
  81. DWORD
  82. PACreateTnFilterState(
  83. PIPSEC_NEGPOL_DATA pIpsecNegPolData,
  84. PIPSEC_NFA_DATA pIpsecNFAData,
  85. PIPSEC_FILTER_SPEC pFilterSpec,
  86. PTNFILTERSTATE * ppTnFilterState
  87. )
  88. {
  89. DWORD dwError = 0;
  90. PTNFILTERSTATE pTnFilterState = NULL;
  91. dwError = AllocateSPDMemory(
  92. sizeof(TNFILTERSTATE),
  93. &pTnFilterState
  94. );
  95. BAIL_ON_WIN32_ERROR(dwError);
  96. memcpy(
  97. &(pTnFilterState->gFilterID),
  98. &(pFilterSpec->FilterSpecGUID),
  99. sizeof(GUID)
  100. );
  101. memcpy(
  102. &(pTnFilterState->gPolicyID),
  103. &(pIpsecNegPolData->NegPolIdentifier),
  104. sizeof(GUID)
  105. );
  106. pTnFilterState->hTnFilter = NULL;
  107. pTnFilterState->pNext = NULL;
  108. *ppTnFilterState = pTnFilterState;
  109. return (dwError);
  110. error:
  111. *ppTnFilterState = NULL;
  112. return (dwError);
  113. }
  114. DWORD
  115. PACreateTnFilter(
  116. PIPSEC_NEGPOL_DATA pIpsecNegPolData,
  117. PIPSEC_NFA_DATA pIpsecNFAData,
  118. PIPSEC_FILTER_SPEC pFilterSpec,
  119. PQMPOLICYSTATE pQMPolicyState,
  120. PTUNNEL_FILTER * ppSPDTnFilter
  121. )
  122. {
  123. DWORD dwError = 0;
  124. PTUNNEL_FILTER pSPDTnFilter = NULL;
  125. WCHAR pszName[512];
  126. dwError = AllocateSPDMemory(
  127. sizeof(TUNNEL_FILTER),
  128. &pSPDTnFilter
  129. );
  130. BAIL_ON_WIN32_ERROR(dwError);
  131. memcpy(
  132. &(pSPDTnFilter->gFilterID),
  133. &(pFilterSpec->FilterSpecGUID),
  134. sizeof(GUID)
  135. );
  136. if (pFilterSpec->pszDescription && *(pFilterSpec->pszDescription)) {
  137. dwError = AllocateSPDString(
  138. pFilterSpec->pszDescription,
  139. &(pSPDTnFilter->pszFilterName)
  140. );
  141. BAIL_ON_WIN32_ERROR(dwError);
  142. }
  143. else {
  144. wsprintf(pszName, L"%d", ++gdwTnFilterCounter);
  145. dwError = AllocateSPDString(
  146. pszName,
  147. &(pSPDTnFilter->pszFilterName)
  148. );
  149. BAIL_ON_WIN32_ERROR(dwError);
  150. }
  151. PASetInterfaceType(
  152. pIpsecNFAData->dwInterfaceType,
  153. &(pSPDTnFilter->InterfaceType)
  154. );
  155. pSPDTnFilter->bCreateMirror = FALSE;
  156. pSPDTnFilter->dwFlags = 0;
  157. PASetAddress(
  158. pFilterSpec->Filter.SrcMask,
  159. pFilterSpec->Filter.SrcAddr,
  160. &(pSPDTnFilter->SrcAddr)
  161. );
  162. PASetAddress(
  163. pFilterSpec->Filter.DestMask,
  164. pFilterSpec->Filter.DestAddr,
  165. &(pSPDTnFilter->DesAddr)
  166. );
  167. PASetAddress(
  168. SUBNET_MASK_ANY,
  169. SUBNET_ADDRESS_ANY,
  170. &(pSPDTnFilter->SrcTunnelAddr)
  171. );
  172. PASetTunnelAddress(
  173. ((ULONG) pIpsecNFAData->dwTunnelIpAddr),
  174. &(pSPDTnFilter->DesTunnelAddr)
  175. );
  176. pSPDTnFilter->Protocol.ProtocolType = PROTOCOL_UNIQUE;
  177. pSPDTnFilter->Protocol.dwProtocol = pFilterSpec->Filter.Protocol;
  178. pSPDTnFilter->SrcPort.PortType = PORT_UNIQUE;
  179. pSPDTnFilter->SrcPort.wPort = pFilterSpec->Filter.SrcPort;
  180. pSPDTnFilter->DesPort.PortType = PORT_UNIQUE;
  181. pSPDTnFilter->DesPort.wPort = pFilterSpec->Filter.DestPort;
  182. SetFilterActions(
  183. pQMPolicyState,
  184. &(pSPDTnFilter->InboundFilterFlag),
  185. &(pSPDTnFilter->OutboundFilterFlag)
  186. );
  187. pSPDTnFilter->dwDirection = 0;
  188. pSPDTnFilter->dwWeight = 0;
  189. memcpy(
  190. &(pSPDTnFilter->gPolicyID),
  191. &(pIpsecNegPolData->NegPolIdentifier),
  192. sizeof(GUID)
  193. );
  194. *ppSPDTnFilter = pSPDTnFilter;
  195. return (dwError);
  196. error:
  197. if (pSPDTnFilter) {
  198. PAFreeTnFilter(
  199. pSPDTnFilter
  200. );
  201. }
  202. *ppSPDTnFilter = NULL;
  203. return (dwError);
  204. }
  205. VOID
  206. PAFreeTnFilter(
  207. PTUNNEL_FILTER pSPDTnFilter
  208. )
  209. {
  210. if (pSPDTnFilter) {
  211. if (pSPDTnFilter->pszFilterName) {
  212. FreeSPDString(pSPDTnFilter->pszFilterName);
  213. }
  214. FreeSPDMemory(pSPDTnFilter);
  215. }
  216. return;
  217. }
  218. DWORD
  219. PADeleteAllTnFilters(
  220. )
  221. {
  222. DWORD dwError = 0;
  223. PTNFILTERSTATE pTnFilterState = NULL;
  224. PTNFILTERSTATE pTemp = NULL;
  225. PTNFILTERSTATE pLeftTnFilterState = NULL;
  226. pTnFilterState = gpTnFilterState;
  227. while (pTnFilterState) {
  228. if (pTnFilterState->hTnFilter) {
  229. dwError = DeleteTunnelFilter(
  230. pTnFilterState->hTnFilter
  231. );
  232. if (!dwError) {
  233. pTemp = pTnFilterState;
  234. pTnFilterState = pTnFilterState->pNext;
  235. FreeSPDMemory(pTemp);
  236. }
  237. else {
  238. pTemp = pTnFilterState;
  239. pTnFilterState = pTnFilterState->pNext;
  240. pTemp->pNext = pLeftTnFilterState;
  241. pLeftTnFilterState = pTemp;
  242. }
  243. }
  244. else {
  245. pTemp = pTnFilterState;
  246. pTnFilterState = pTnFilterState->pNext;
  247. FreeSPDMemory(pTemp);
  248. }
  249. }
  250. gpTnFilterState = pLeftTnFilterState;
  251. return (dwError);
  252. }
  253. VOID
  254. PAFreeTnFilterStateList(
  255. PTNFILTERSTATE pTnFilterState
  256. )
  257. {
  258. PTNFILTERSTATE pTemp = NULL;
  259. while (pTnFilterState) {
  260. pTemp = pTnFilterState;
  261. pTnFilterState = pTnFilterState->pNext;
  262. FreeSPDMemory(pTemp);
  263. }
  264. }
  265. DWORD
  266. PADeleteTnFilterSpecs(
  267. PIPSEC_NFA_DATA pIpsecNFAData
  268. )
  269. {
  270. DWORD dwError = 0;
  271. PIPSEC_NEGPOL_DATA pIpsecNegPolData = NULL;
  272. PIPSEC_FILTER_DATA pIpsecFilterData = NULL;
  273. DWORD dwNumFilterSpecs = 0;
  274. PIPSEC_FILTER_SPEC * ppFilterSpecs = NULL;
  275. DWORD i = 0;
  276. PIPSEC_FILTER_SPEC pFilterSpec = NULL;
  277. pIpsecNegPolData = pIpsecNFAData->pIpsecNegPolData;
  278. if (!memcmp(
  279. &(pIpsecNegPolData->NegPolType),
  280. &(GUID_NEGOTIATION_TYPE_DEFAULT),
  281. sizeof(GUID))) {
  282. dwError = ERROR_SUCCESS;
  283. return (dwError);
  284. }
  285. pIpsecFilterData = pIpsecNFAData->pIpsecFilterData;
  286. if (!pIpsecFilterData) {
  287. dwError = ERROR_SUCCESS;
  288. return (dwError);
  289. }
  290. dwNumFilterSpecs = pIpsecFilterData->dwNumFilterSpecs;
  291. ppFilterSpecs = pIpsecFilterData->ppFilterSpecs;
  292. for (i = 0; i < dwNumFilterSpecs; i++) {
  293. pFilterSpec = *(ppFilterSpecs + i);
  294. dwError = PADeleteTnFilter(
  295. pFilterSpec->FilterSpecGUID
  296. );
  297. }
  298. return (dwError);
  299. }
  300. DWORD
  301. PADeleteTnFilter(
  302. GUID gFilterID
  303. )
  304. {
  305. DWORD dwError = 0;
  306. PTNFILTERSTATE pTnFilterState = NULL;
  307. pTnFilterState = FindTnFilterState(
  308. gFilterID
  309. );
  310. if (!pTnFilterState) {
  311. dwError = ERROR_SUCCESS;
  312. return (dwError);
  313. }
  314. if (pTnFilterState->hTnFilter) {
  315. dwError = DeleteTunnelFilter(
  316. pTnFilterState->hTnFilter
  317. );
  318. BAIL_ON_WIN32_ERROR(dwError);
  319. }
  320. PADeleteTnFilterState(pTnFilterState);
  321. error:
  322. return (dwError);
  323. }
  324. VOID
  325. PADeleteTnFilterState(
  326. PTNFILTERSTATE pTnFilterState
  327. )
  328. {
  329. PTNFILTERSTATE * ppTemp = NULL;
  330. ppTemp = &gpTnFilterState;
  331. while (*ppTemp) {
  332. if (*ppTemp == pTnFilterState) {
  333. break;
  334. }
  335. ppTemp = &((*ppTemp)->pNext);
  336. }
  337. if (*ppTemp) {
  338. *ppTemp = pTnFilterState->pNext;
  339. }
  340. FreeSPDMemory(pTnFilterState);
  341. return;
  342. }
  343. PTNFILTERSTATE
  344. FindTnFilterState(
  345. GUID gFilterID
  346. )
  347. {
  348. PTNFILTERSTATE pTnFilterState = NULL;
  349. pTnFilterState = gpTnFilterState;
  350. while (pTnFilterState) {
  351. if (!memcmp(&(pTnFilterState->gFilterID), &gFilterID, sizeof(GUID))) {
  352. return (pTnFilterState);
  353. }
  354. pTnFilterState = pTnFilterState->pNext;
  355. }
  356. return (NULL);
  357. }