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.

583 lines
12 KiB

  1. #include "precomp.h"
  2. DWORD
  3. PAAddQMFilters(
  4. PIPSEC_NFA_DATA * ppIpsecNFAData,
  5. DWORD dwNumNFACount
  6. )
  7. {
  8. DWORD dwError = 0;
  9. DWORD i = 0;
  10. PIPSEC_NFA_DATA pIpsecNFAData = NULL;
  11. for (i = 0; i < dwNumNFACount; i++) {
  12. pIpsecNFAData = *(ppIpsecNFAData + i);
  13. if (!(pIpsecNFAData->dwTunnelFlags)) {
  14. dwError = PAAddTxFilterSpecs(
  15. pIpsecNFAData
  16. );
  17. }
  18. else {
  19. dwError = PAAddTnFilterSpecs(
  20. pIpsecNFAData
  21. );
  22. }
  23. }
  24. return (dwError);
  25. }
  26. DWORD
  27. PAAddTxFilterSpecs(
  28. PIPSEC_NFA_DATA pIpsecNFAData
  29. )
  30. {
  31. DWORD dwError = 0;
  32. PIPSEC_NEGPOL_DATA pIpsecNegPolData = NULL;
  33. PIPSEC_FILTER_DATA pIpsecFilterData = NULL;
  34. PQMPOLICYSTATE pQMPolicyState = NULL;
  35. DWORD dwNumFilterSpecs = 0;
  36. PIPSEC_FILTER_SPEC * ppFilterSpecs = NULL;
  37. DWORD i = 0;
  38. PTXFILTERSTATE pTxFilterState = NULL;
  39. PTRANSPORT_FILTER pSPDTxFilter = NULL;
  40. LPWSTR pServerName = NULL;
  41. DWORD dwPersist = 0;
  42. pIpsecNegPolData = pIpsecNFAData->pIpsecNegPolData;
  43. if (!memcmp(
  44. &(pIpsecNegPolData->NegPolType),
  45. &(GUID_NEGOTIATION_TYPE_DEFAULT),
  46. sizeof(GUID))) {
  47. dwError = ERROR_SUCCESS;
  48. return (dwError);
  49. }
  50. pIpsecFilterData = pIpsecNFAData->pIpsecFilterData;
  51. if (!pIpsecFilterData) {
  52. dwError = ERROR_SUCCESS;
  53. return (dwError);
  54. }
  55. pQMPolicyState = FindQMPolicyState(
  56. pIpsecNegPolData->NegPolIdentifier
  57. );
  58. if (!pQMPolicyState) {
  59. dwError = ERROR_INVALID_PARAMETER;
  60. return (dwError);
  61. }
  62. if (!IsClearOnly(pQMPolicyState->gNegPolAction) &&
  63. !IsBlocking(pQMPolicyState->gNegPolAction) &&
  64. !(pQMPolicyState->bInSPD)) {
  65. dwError = ERROR_INVALID_PARAMETER;
  66. return (dwError);
  67. }
  68. dwNumFilterSpecs = pIpsecFilterData->dwNumFilterSpecs;
  69. ppFilterSpecs = pIpsecFilterData->ppFilterSpecs;
  70. for (i = 0; i < dwNumFilterSpecs; i++) {
  71. dwError = PACreateTxFilterState(
  72. pIpsecNegPolData,
  73. pIpsecNFAData,
  74. *(ppFilterSpecs + i),
  75. &pTxFilterState
  76. );
  77. if (dwError) {
  78. continue;
  79. }
  80. dwError = PACreateTxFilter(
  81. pIpsecNegPolData,
  82. pIpsecNFAData,
  83. *(ppFilterSpecs + i),
  84. pQMPolicyState,
  85. &pSPDTxFilter
  86. );
  87. if (dwError) {
  88. pTxFilterState->hTxFilter = NULL;
  89. pTxFilterState->pNext = gpTxFilterState;
  90. gpTxFilterState = pTxFilterState;
  91. continue;
  92. }
  93. dwError = AddTransportFilter(
  94. pServerName,
  95. dwPersist,
  96. pSPDTxFilter,
  97. &(pTxFilterState->hTxFilter)
  98. );
  99. pTxFilterState->pNext = gpTxFilterState;
  100. gpTxFilterState = pTxFilterState;
  101. PAFreeTxFilter(pSPDTxFilter);
  102. }
  103. return (dwError);
  104. }
  105. DWORD
  106. PACreateTxFilterState(
  107. PIPSEC_NEGPOL_DATA pIpsecNegPolData,
  108. PIPSEC_NFA_DATA pIpsecNFAData,
  109. PIPSEC_FILTER_SPEC pFilterSpec,
  110. PTXFILTERSTATE * ppTxFilterState
  111. )
  112. {
  113. DWORD dwError = 0;
  114. PTXFILTERSTATE pTxFilterState = NULL;
  115. dwError = AllocateSPDMemory(
  116. sizeof(TXFILTERSTATE),
  117. &pTxFilterState
  118. );
  119. BAIL_ON_WIN32_ERROR(dwError);
  120. memcpy(
  121. &(pTxFilterState->gFilterID),
  122. &(pFilterSpec->FilterSpecGUID),
  123. sizeof(GUID)
  124. );
  125. memcpy(
  126. &(pTxFilterState->gPolicyID),
  127. &(pIpsecNegPolData->NegPolIdentifier),
  128. sizeof(GUID)
  129. );
  130. pTxFilterState->hTxFilter = NULL;
  131. pTxFilterState->pNext = NULL;
  132. *ppTxFilterState = pTxFilterState;
  133. return (dwError);
  134. error:
  135. *ppTxFilterState = NULL;
  136. return (dwError);
  137. }
  138. DWORD
  139. PACreateTxFilter(
  140. PIPSEC_NEGPOL_DATA pIpsecNegPolData,
  141. PIPSEC_NFA_DATA pIpsecNFAData,
  142. PIPSEC_FILTER_SPEC pFilterSpec,
  143. PQMPOLICYSTATE pQMPolicyState,
  144. PTRANSPORT_FILTER * ppSPDTxFilter
  145. )
  146. {
  147. DWORD dwError = 0;
  148. PTRANSPORT_FILTER pSPDTxFilter = NULL;
  149. WCHAR pszName[512];
  150. dwError = AllocateSPDMemory(
  151. sizeof(TRANSPORT_FILTER),
  152. &pSPDTxFilter
  153. );
  154. BAIL_ON_WIN32_ERROR(dwError);
  155. memcpy(
  156. &(pSPDTxFilter->gFilterID),
  157. &(pFilterSpec->FilterSpecGUID),
  158. sizeof(GUID)
  159. );
  160. if (pFilterSpec->pszDescription && *(pFilterSpec->pszDescription)) {
  161. dwError = AllocateSPDString(
  162. pFilterSpec->pszDescription,
  163. &(pSPDTxFilter->pszFilterName)
  164. );
  165. BAIL_ON_WIN32_ERROR(dwError);
  166. }
  167. else {
  168. wsprintf(pszName, L"%d", ++gdwTxFilterCounter);
  169. dwError = AllocateSPDString(
  170. pszName,
  171. &(pSPDTxFilter->pszFilterName)
  172. );
  173. BAIL_ON_WIN32_ERROR(dwError);
  174. }
  175. PASetInterfaceType(
  176. pIpsecNFAData->dwInterfaceType,
  177. &(pSPDTxFilter->InterfaceType)
  178. );
  179. pSPDTxFilter->bCreateMirror = (BOOL) pFilterSpec->dwMirrorFlag;
  180. pSPDTxFilter->dwFlags = 0;
  181. PASetAddress(
  182. pFilterSpec->Filter.SrcMask,
  183. pFilterSpec->Filter.SrcAddr,
  184. &(pSPDTxFilter->SrcAddr)
  185. );
  186. PASetAddress(
  187. pFilterSpec->Filter.DestMask,
  188. pFilterSpec->Filter.DestAddr,
  189. &(pSPDTxFilter->DesAddr)
  190. );
  191. pSPDTxFilter->Protocol.ProtocolType = PROTOCOL_UNIQUE;
  192. pSPDTxFilter->Protocol.dwProtocol = pFilterSpec->Filter.Protocol;
  193. pSPDTxFilter->SrcPort.PortType = PORT_UNIQUE;
  194. pSPDTxFilter->SrcPort.wPort = pFilterSpec->Filter.SrcPort;
  195. pSPDTxFilter->DesPort.PortType = PORT_UNIQUE;
  196. pSPDTxFilter->DesPort.wPort = pFilterSpec->Filter.DestPort;
  197. SetFilterActions(
  198. pQMPolicyState,
  199. &(pSPDTxFilter->InboundFilterFlag),
  200. &(pSPDTxFilter->OutboundFilterFlag)
  201. );
  202. pSPDTxFilter->dwDirection = 0;
  203. pSPDTxFilter->dwWeight = 0;
  204. memcpy(
  205. &(pSPDTxFilter->gPolicyID),
  206. &(pIpsecNegPolData->NegPolIdentifier),
  207. sizeof(GUID)
  208. );
  209. *ppSPDTxFilter = pSPDTxFilter;
  210. return (dwError);
  211. error:
  212. if (pSPDTxFilter) {
  213. PAFreeTxFilter(
  214. pSPDTxFilter
  215. );
  216. }
  217. *ppSPDTxFilter = NULL;
  218. return (dwError);
  219. }
  220. VOID
  221. SetFilterActions(
  222. PQMPOLICYSTATE pQMPolicyState,
  223. PFILTER_FLAG pInboundFilterFlag,
  224. PFILTER_FLAG pOutboundFilterFlag
  225. )
  226. {
  227. *pInboundFilterFlag = NEGOTIATE_SECURITY;
  228. *pOutboundFilterFlag = NEGOTIATE_SECURITY;
  229. if (IsBlocking(pQMPolicyState->gNegPolAction)) {
  230. *pInboundFilterFlag = BLOCKING;
  231. *pOutboundFilterFlag = BLOCKING;
  232. }
  233. else if (IsClearOnly(pQMPolicyState->gNegPolAction)) {
  234. *pInboundFilterFlag = PASS_THRU;
  235. *pOutboundFilterFlag = PASS_THRU;
  236. }
  237. else if (IsInboundPassThru(pQMPolicyState->gNegPolAction)) {
  238. *pInboundFilterFlag = PASS_THRU;
  239. }
  240. if (pQMPolicyState->bAllowsSoft && gbBackwardSoftSA) {
  241. *pInboundFilterFlag = PASS_THRU;
  242. }
  243. }
  244. VOID
  245. PAFreeTxFilter(
  246. PTRANSPORT_FILTER pSPDTxFilter
  247. )
  248. {
  249. if (pSPDTxFilter) {
  250. if (pSPDTxFilter->pszFilterName) {
  251. FreeSPDString(pSPDTxFilter->pszFilterName);
  252. }
  253. FreeSPDMemory(pSPDTxFilter);
  254. }
  255. return;
  256. }
  257. DWORD
  258. PADeleteAllTxFilters(
  259. )
  260. {
  261. DWORD dwError = 0;
  262. PTXFILTERSTATE pTxFilterState = NULL;
  263. PTXFILTERSTATE pTemp = NULL;
  264. PTXFILTERSTATE pLeftTxFilterState = NULL;
  265. pTxFilterState = gpTxFilterState;
  266. while (pTxFilterState) {
  267. if (pTxFilterState->hTxFilter) {
  268. dwError = DeleteTransportFilter(
  269. pTxFilterState->hTxFilter
  270. );
  271. if (!dwError) {
  272. pTemp = pTxFilterState;
  273. pTxFilterState = pTxFilterState->pNext;
  274. FreeSPDMemory(pTemp);
  275. }
  276. else {
  277. pTemp = pTxFilterState;
  278. pTxFilterState = pTxFilterState->pNext;
  279. pTemp->pNext = pLeftTxFilterState;
  280. pLeftTxFilterState = pTemp;
  281. }
  282. }
  283. else {
  284. pTemp = pTxFilterState;
  285. pTxFilterState = pTxFilterState->pNext;
  286. FreeSPDMemory(pTemp);
  287. }
  288. }
  289. gpTxFilterState = pLeftTxFilterState;
  290. return (dwError);
  291. }
  292. VOID
  293. PAFreeTxFilterStateList(
  294. PTXFILTERSTATE pTxFilterState
  295. )
  296. {
  297. PTXFILTERSTATE pTemp = NULL;
  298. while (pTxFilterState) {
  299. pTemp = pTxFilterState;
  300. pTxFilterState = pTxFilterState->pNext;
  301. FreeSPDMemory(pTemp);
  302. }
  303. }
  304. DWORD
  305. PADeleteQMFilters(
  306. PIPSEC_NFA_DATA * ppIpsecNFAData,
  307. DWORD dwNumNFACount
  308. )
  309. {
  310. DWORD dwError = 0;
  311. DWORD i = 0;
  312. PIPSEC_NFA_DATA pIpsecNFAData = NULL;
  313. for (i = 0; i < dwNumNFACount; i++) {
  314. pIpsecNFAData = *(ppIpsecNFAData + i);
  315. if (!(pIpsecNFAData->dwTunnelFlags)) {
  316. dwError = PADeleteTxFilterSpecs(
  317. pIpsecNFAData
  318. );
  319. }
  320. else {
  321. dwError = PADeleteTnFilterSpecs(
  322. pIpsecNFAData
  323. );
  324. }
  325. }
  326. return (dwError);
  327. }
  328. DWORD
  329. PADeleteTxFilterSpecs(
  330. PIPSEC_NFA_DATA pIpsecNFAData
  331. )
  332. {
  333. DWORD dwError = 0;
  334. PIPSEC_NEGPOL_DATA pIpsecNegPolData = NULL;
  335. PIPSEC_FILTER_DATA pIpsecFilterData = NULL;
  336. DWORD dwNumFilterSpecs = 0;
  337. PIPSEC_FILTER_SPEC * ppFilterSpecs = NULL;
  338. DWORD i = 0;
  339. PIPSEC_FILTER_SPEC pFilterSpec = NULL;
  340. pIpsecNegPolData = pIpsecNFAData->pIpsecNegPolData;
  341. if (!memcmp(
  342. &(pIpsecNegPolData->NegPolType),
  343. &(GUID_NEGOTIATION_TYPE_DEFAULT),
  344. sizeof(GUID))) {
  345. dwError = ERROR_SUCCESS;
  346. return (dwError);
  347. }
  348. pIpsecFilterData = pIpsecNFAData->pIpsecFilterData;
  349. if (!pIpsecFilterData) {
  350. dwError = ERROR_SUCCESS;
  351. return (dwError);
  352. }
  353. dwNumFilterSpecs = pIpsecFilterData->dwNumFilterSpecs;
  354. ppFilterSpecs = pIpsecFilterData->ppFilterSpecs;
  355. for (i = 0; i < dwNumFilterSpecs; i++) {
  356. pFilterSpec = *(ppFilterSpecs + i);
  357. dwError = PADeleteTxFilter(
  358. pFilterSpec->FilterSpecGUID
  359. );
  360. }
  361. return (dwError);
  362. }
  363. DWORD
  364. PADeleteTxFilter(
  365. GUID gFilterID
  366. )
  367. {
  368. DWORD dwError = 0;
  369. PTXFILTERSTATE pTxFilterState = NULL;
  370. pTxFilterState = FindTxFilterState(
  371. gFilterID
  372. );
  373. if (!pTxFilterState) {
  374. dwError = ERROR_SUCCESS;
  375. return (dwError);
  376. }
  377. if (pTxFilterState->hTxFilter) {
  378. dwError = DeleteTransportFilter(
  379. pTxFilterState->hTxFilter
  380. );
  381. BAIL_ON_WIN32_ERROR(dwError);
  382. }
  383. PADeleteTxFilterState(pTxFilterState);
  384. error:
  385. return (dwError);
  386. }
  387. VOID
  388. PADeleteTxFilterState(
  389. PTXFILTERSTATE pTxFilterState
  390. )
  391. {
  392. PTXFILTERSTATE * ppTemp = NULL;
  393. ppTemp = &gpTxFilterState;
  394. while (*ppTemp) {
  395. if (*ppTemp == pTxFilterState) {
  396. break;
  397. }
  398. ppTemp = &((*ppTemp)->pNext);
  399. }
  400. if (*ppTemp) {
  401. *ppTemp = pTxFilterState->pNext;
  402. }
  403. FreeSPDMemory(pTxFilterState);
  404. return;
  405. }
  406. PTXFILTERSTATE
  407. FindTxFilterState(
  408. GUID gFilterID
  409. )
  410. {
  411. PTXFILTERSTATE pTxFilterState = NULL;
  412. pTxFilterState = gpTxFilterState;
  413. while (pTxFilterState) {
  414. if (!memcmp(&(pTxFilterState->gFilterID), &gFilterID, sizeof(GUID))) {
  415. return (pTxFilterState);
  416. }
  417. pTxFilterState = pTxFilterState->pNext;
  418. }
  419. return (NULL);
  420. }