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.

618 lines
12 KiB

  1. #include "precomp.h"
  2. DWORD
  3. PAAddMMFilters(
  4. PIPSEC_ISAKMP_DATA pIpsecISAKMPData,
  5. PIPSEC_NFA_DATA * ppIpsecNFAData,
  6. DWORD dwNumNFACount
  7. )
  8. {
  9. DWORD dwError = 0;
  10. DWORD i = 0;
  11. for (i = 0; i < dwNumNFACount; i++) {
  12. dwError = PAAddMMFilterSpecs(
  13. pIpsecISAKMPData,
  14. *(ppIpsecNFAData + i)
  15. );
  16. }
  17. return (dwError);
  18. }
  19. DWORD
  20. PAAddMMFilterSpecs(
  21. PIPSEC_ISAKMP_DATA pIpsecISAKMPData,
  22. PIPSEC_NFA_DATA pIpsecNFAData
  23. )
  24. {
  25. DWORD dwError = 0;
  26. PIPSEC_NEGPOL_DATA pIpsecNegPolData = NULL;
  27. PIPSEC_FILTER_DATA pIpsecFilterData = NULL;
  28. PMMPOLICYSTATE pMMPolicyState = NULL;
  29. PMMAUTHSTATE pMMAuthState = NULL;
  30. DWORD dwNumFilterSpecs = 0;
  31. PIPSEC_FILTER_SPEC * ppFilterSpecs = NULL;
  32. DWORD i = 0;
  33. PMMFILTERSTATE pMMFilterState = NULL;
  34. PMM_FILTER pSPDMMFilter = NULL;
  35. LPWSTR pServerName = NULL;
  36. DWORD dwPersist = 0;
  37. pIpsecNegPolData = pIpsecNFAData->pIpsecNegPolData;
  38. if (!memcmp(
  39. &(pIpsecNegPolData->NegPolType),
  40. &(GUID_NEGOTIATION_TYPE_DEFAULT),
  41. sizeof(GUID))) {
  42. dwError = ERROR_SUCCESS;
  43. return (dwError);
  44. }
  45. if (IsClearOnly(pIpsecNegPolData->NegPolAction) ||
  46. IsBlocking(pIpsecNegPolData->NegPolAction)) {
  47. dwError = ERROR_SUCCESS;
  48. return (dwError);
  49. }
  50. pIpsecFilterData = pIpsecNFAData->pIpsecFilterData;
  51. if (!pIpsecFilterData) {
  52. dwError = ERROR_SUCCESS;
  53. return (dwError);
  54. }
  55. pMMPolicyState = FindMMPolicyState(
  56. pIpsecISAKMPData->ISAKMPIdentifier
  57. );
  58. if (!pMMPolicyState || !(pMMPolicyState->bInSPD)) {
  59. dwError = ERROR_INVALID_PARAMETER;
  60. return (dwError);
  61. }
  62. pMMAuthState = FindMMAuthState(
  63. pIpsecNFAData->NFAIdentifier
  64. );
  65. if (!pMMAuthState || !(pMMAuthState->bInSPD)) {
  66. dwError = ERROR_INVALID_PARAMETER;
  67. return (dwError);
  68. }
  69. dwNumFilterSpecs = pIpsecFilterData->dwNumFilterSpecs;
  70. ppFilterSpecs = pIpsecFilterData->ppFilterSpecs;
  71. for (i = 0; i < dwNumFilterSpecs; i++) {
  72. dwError = PACreateMMFilterState(
  73. pIpsecISAKMPData,
  74. pIpsecNFAData,
  75. *(ppFilterSpecs + i),
  76. &pMMFilterState
  77. );
  78. if (dwError) {
  79. continue;
  80. }
  81. dwError = PACreateMMFilter(
  82. pIpsecISAKMPData,
  83. pIpsecNFAData,
  84. *(ppFilterSpecs + i),
  85. &pSPDMMFilter
  86. );
  87. if (dwError) {
  88. pMMFilterState->hMMFilter = NULL;
  89. pMMFilterState->pNext = gpMMFilterState;
  90. gpMMFilterState = pMMFilterState;
  91. continue;
  92. }
  93. dwError = AddMMFilter(
  94. pServerName,
  95. dwPersist,
  96. pSPDMMFilter,
  97. &(pMMFilterState->hMMFilter)
  98. );
  99. pMMFilterState->pNext = gpMMFilterState;
  100. gpMMFilterState = pMMFilterState;
  101. PAFreeMMFilter(pSPDMMFilter);
  102. }
  103. return (dwError);
  104. }
  105. DWORD
  106. PACreateMMFilterState(
  107. PIPSEC_ISAKMP_DATA pIpsecISAKMPData,
  108. PIPSEC_NFA_DATA pIpsecNFAData,
  109. PIPSEC_FILTER_SPEC pFilterSpec,
  110. PMMFILTERSTATE * ppMMFilterState
  111. )
  112. {
  113. DWORD dwError = 0;
  114. PMMFILTERSTATE pMMFilterState = NULL;
  115. dwError = AllocateSPDMemory(
  116. sizeof(MMFILTERSTATE),
  117. &pMMFilterState
  118. );
  119. BAIL_ON_WIN32_ERROR(dwError);
  120. memcpy(
  121. &(pMMFilterState->gFilterID),
  122. &(pFilterSpec->FilterSpecGUID),
  123. sizeof(GUID)
  124. );
  125. memcpy(
  126. &(pMMFilterState->gPolicyID),
  127. &(pIpsecISAKMPData->ISAKMPIdentifier),
  128. sizeof(GUID)
  129. );
  130. memcpy(
  131. &(pMMFilterState->gMMAuthID),
  132. &(pIpsecNFAData->NFAIdentifier),
  133. sizeof(GUID)
  134. );
  135. pMMFilterState->hMMFilter = NULL;
  136. pMMFilterState->pNext = NULL;
  137. *ppMMFilterState = pMMFilterState;
  138. return (dwError);
  139. error:
  140. *ppMMFilterState = NULL;
  141. return (dwError);
  142. }
  143. DWORD
  144. PACreateMMFilter(
  145. PIPSEC_ISAKMP_DATA pIpsecISAKMPData,
  146. PIPSEC_NFA_DATA pIpsecNFAData,
  147. PIPSEC_FILTER_SPEC pFilterSpec,
  148. PMM_FILTER * ppSPDMMFilter
  149. )
  150. {
  151. DWORD dwError = 0;
  152. PMM_FILTER pSPDMMFilter = NULL;
  153. WCHAR pszName[512];
  154. dwError = AllocateSPDMemory(
  155. sizeof(MM_FILTER),
  156. &pSPDMMFilter
  157. );
  158. BAIL_ON_WIN32_ERROR(dwError);
  159. memcpy(
  160. &(pSPDMMFilter->gFilterID),
  161. &(pFilterSpec->FilterSpecGUID),
  162. sizeof(GUID)
  163. );
  164. if (pFilterSpec->pszDescription && *(pFilterSpec->pszDescription)) {
  165. dwError = AllocateSPDString(
  166. pFilterSpec->pszDescription,
  167. &(pSPDMMFilter->pszFilterName)
  168. );
  169. BAIL_ON_WIN32_ERROR(dwError);
  170. }
  171. else {
  172. wsprintf(pszName, L"%d", ++gdwMMFilterCounter);
  173. dwError = AllocateSPDString(
  174. pszName,
  175. &(pSPDMMFilter->pszFilterName)
  176. );
  177. BAIL_ON_WIN32_ERROR(dwError);
  178. }
  179. PASetInterfaceType(
  180. pIpsecNFAData->dwInterfaceType,
  181. &(pSPDMMFilter->InterfaceType)
  182. );
  183. pSPDMMFilter->bCreateMirror = (BOOL) pFilterSpec->dwMirrorFlag;
  184. pSPDMMFilter->dwFlags = 0;
  185. if (!(pIpsecNFAData->dwTunnelFlags)) {
  186. PASetAddress(
  187. pFilterSpec->Filter.SrcMask,
  188. pFilterSpec->Filter.SrcAddr,
  189. &(pSPDMMFilter->SrcAddr)
  190. );
  191. PASetAddress(
  192. pFilterSpec->Filter.DestMask,
  193. pFilterSpec->Filter.DestAddr,
  194. &(pSPDMMFilter->DesAddr)
  195. );
  196. }
  197. else {
  198. PASetAddress(
  199. IP_ADDRESS_MASK_NONE,
  200. IP_ADDRESS_ME,
  201. &(pSPDMMFilter->SrcAddr)
  202. );
  203. pSPDMMFilter->bCreateMirror = TRUE;
  204. PASetTunnelAddress(
  205. ((ULONG) pIpsecNFAData->dwTunnelIpAddr),
  206. &(pSPDMMFilter->DesAddr)
  207. );
  208. }
  209. pSPDMMFilter->dwDirection = 0;
  210. pSPDMMFilter->dwWeight = 0;
  211. memcpy(
  212. &(pSPDMMFilter->gPolicyID),
  213. &(pIpsecISAKMPData->ISAKMPIdentifier),
  214. sizeof(GUID)
  215. );
  216. memcpy(
  217. &(pSPDMMFilter->gMMAuthID),
  218. &(pIpsecNFAData->NFAIdentifier),
  219. sizeof(GUID)
  220. );
  221. *ppSPDMMFilter = pSPDMMFilter;
  222. return (dwError);
  223. error:
  224. if (pSPDMMFilter) {
  225. PAFreeMMFilter(
  226. pSPDMMFilter
  227. );
  228. }
  229. *ppSPDMMFilter = NULL;
  230. return (dwError);
  231. }
  232. VOID
  233. PASetInterfaceType(
  234. DWORD dwInterfaceType,
  235. PIF_TYPE pInterfaceType
  236. )
  237. {
  238. if (dwInterfaceType == PASTORE_IF_TYPE_DIALUP) {
  239. *pInterfaceType = INTERFACE_TYPE_DIALUP;
  240. }
  241. else if (dwInterfaceType == PASTORE_IF_TYPE_LAN) {
  242. *pInterfaceType = INTERFACE_TYPE_LAN;
  243. }
  244. else {
  245. *pInterfaceType = INTERFACE_TYPE_ALL;
  246. }
  247. }
  248. VOID
  249. PASetAddress(
  250. ULONG uMask,
  251. ULONG uAddr,
  252. PADDR pAddr
  253. )
  254. {
  255. if (uMask == IP_ADDRESS_MASK_NONE) {
  256. pAddr->AddrType = IP_ADDR_UNIQUE;
  257. pAddr->uIpAddr = uAddr;
  258. pAddr->uSubNetMask = uMask;
  259. }
  260. else {
  261. pAddr->AddrType = IP_ADDR_SUBNET;
  262. pAddr->uIpAddr = uAddr;
  263. pAddr->uSubNetMask = uMask;
  264. }
  265. }
  266. VOID
  267. PASetTunnelAddress(
  268. ULONG uAddr,
  269. PADDR pAddr
  270. )
  271. {
  272. if (uAddr == SUBNET_ADDRESS_ANY) {
  273. pAddr->AddrType = IP_ADDR_SUBNET;
  274. pAddr->uIpAddr = uAddr;
  275. pAddr->uSubNetMask = SUBNET_MASK_ANY;
  276. }
  277. else {
  278. pAddr->AddrType = IP_ADDR_UNIQUE;
  279. pAddr->uIpAddr = uAddr;
  280. pAddr->uSubNetMask = IP_ADDRESS_MASK_NONE;
  281. }
  282. }
  283. VOID
  284. PAFreeMMFilter(
  285. PMM_FILTER pSPDMMFilter
  286. )
  287. {
  288. if (pSPDMMFilter) {
  289. if (pSPDMMFilter->pszFilterName) {
  290. FreeSPDString(pSPDMMFilter->pszFilterName);
  291. }
  292. FreeSPDMemory(pSPDMMFilter);
  293. }
  294. return;
  295. }
  296. DWORD
  297. PADeleteAllMMFilters(
  298. )
  299. {
  300. DWORD dwError = 0;
  301. PMMFILTERSTATE pMMFilterState = NULL;
  302. PMMFILTERSTATE pTemp = NULL;
  303. PMMFILTERSTATE pLeftMMFilterState = NULL;
  304. pMMFilterState = gpMMFilterState;
  305. while (pMMFilterState) {
  306. if (pMMFilterState->hMMFilter) {
  307. dwError = DeleteMMFilter(
  308. pMMFilterState->hMMFilter
  309. );
  310. if (!dwError) {
  311. pTemp = pMMFilterState;
  312. pMMFilterState = pMMFilterState->pNext;
  313. FreeSPDMemory(pTemp);
  314. }
  315. else {
  316. pTemp = pMMFilterState;
  317. pMMFilterState = pMMFilterState->pNext;
  318. pTemp->pNext = pLeftMMFilterState;
  319. pLeftMMFilterState = pTemp;
  320. }
  321. }
  322. else {
  323. pTemp = pMMFilterState;
  324. pMMFilterState = pMMFilterState->pNext;
  325. FreeSPDMemory(pTemp);
  326. }
  327. }
  328. gpMMFilterState = pLeftMMFilterState;
  329. return (dwError);
  330. }
  331. VOID
  332. PAFreeMMFilterStateList(
  333. PMMFILTERSTATE pMMFilterState
  334. )
  335. {
  336. PMMFILTERSTATE pTemp = NULL;
  337. while (pMMFilterState) {
  338. pTemp = pMMFilterState;
  339. pMMFilterState = pMMFilterState->pNext;
  340. FreeSPDMemory(pTemp);
  341. }
  342. }
  343. DWORD
  344. PADeleteMMFilters(
  345. PIPSEC_ISAKMP_DATA pIpsecISAKMPData,
  346. PIPSEC_NFA_DATA * ppIpsecNFAData,
  347. DWORD dwNumNFACount
  348. )
  349. {
  350. DWORD dwError = 0;
  351. DWORD i = 0;
  352. for (i = 0; i < dwNumNFACount; i++) {
  353. dwError = PADeleteMMFilterSpecs(
  354. pIpsecISAKMPData,
  355. *(ppIpsecNFAData + i)
  356. );
  357. }
  358. return (dwError);
  359. }
  360. DWORD
  361. PADeleteMMFilterSpecs(
  362. PIPSEC_ISAKMP_DATA pIpsecISAKMPData,
  363. PIPSEC_NFA_DATA pIpsecNFAData
  364. )
  365. {
  366. DWORD dwError = 0;
  367. PIPSEC_NEGPOL_DATA pIpsecNegPolData = NULL;
  368. PIPSEC_FILTER_DATA pIpsecFilterData = NULL;
  369. DWORD dwNumFilterSpecs = 0;
  370. PIPSEC_FILTER_SPEC * ppFilterSpecs = NULL;
  371. DWORD i = 0;
  372. PIPSEC_FILTER_SPEC pFilterSpec = NULL;
  373. pIpsecNegPolData = pIpsecNFAData->pIpsecNegPolData;
  374. if (!memcmp(
  375. &(pIpsecNegPolData->NegPolType),
  376. &(GUID_NEGOTIATION_TYPE_DEFAULT),
  377. sizeof(GUID))) {
  378. dwError = ERROR_SUCCESS;
  379. return (dwError);
  380. }
  381. if (IsClearOnly(pIpsecNegPolData->NegPolAction) ||
  382. IsBlocking(pIpsecNegPolData->NegPolAction)) {
  383. dwError = ERROR_SUCCESS;
  384. return (dwError);
  385. }
  386. pIpsecFilterData = pIpsecNFAData->pIpsecFilterData;
  387. if (!pIpsecFilterData) {
  388. dwError = ERROR_SUCCESS;
  389. return (dwError);
  390. }
  391. dwNumFilterSpecs = pIpsecFilterData->dwNumFilterSpecs;
  392. ppFilterSpecs = pIpsecFilterData->ppFilterSpecs;
  393. for (i = 0; i < dwNumFilterSpecs; i++) {
  394. pFilterSpec = *(ppFilterSpecs + i);
  395. dwError = PADeleteMMFilter(
  396. pFilterSpec->FilterSpecGUID
  397. );
  398. }
  399. return (dwError);
  400. }
  401. DWORD
  402. PADeleteMMFilter(
  403. GUID gFilterID
  404. )
  405. {
  406. DWORD dwError = 0;
  407. PMMFILTERSTATE pMMFilterState = NULL;
  408. pMMFilterState = FindMMFilterState(
  409. gFilterID
  410. );
  411. if (!pMMFilterState) {
  412. dwError = ERROR_SUCCESS;
  413. return (dwError);
  414. }
  415. if (pMMFilterState->hMMFilter) {
  416. dwError = DeleteMMFilter(
  417. pMMFilterState->hMMFilter
  418. );
  419. BAIL_ON_WIN32_ERROR(dwError);
  420. }
  421. PADeleteMMFilterState(pMMFilterState);
  422. error:
  423. return (dwError);
  424. }
  425. VOID
  426. PADeleteMMFilterState(
  427. PMMFILTERSTATE pMMFilterState
  428. )
  429. {
  430. PMMFILTERSTATE * ppTemp = NULL;
  431. ppTemp = &gpMMFilterState;
  432. while (*ppTemp) {
  433. if (*ppTemp == pMMFilterState) {
  434. break;
  435. }
  436. ppTemp = &((*ppTemp)->pNext);
  437. }
  438. if (*ppTemp) {
  439. *ppTemp = pMMFilterState->pNext;
  440. }
  441. FreeSPDMemory(pMMFilterState);
  442. return;
  443. }
  444. PMMFILTERSTATE
  445. FindMMFilterState(
  446. GUID gFilterID
  447. )
  448. {
  449. PMMFILTERSTATE pMMFilterState = NULL;
  450. pMMFilterState = gpMMFilterState;
  451. while (pMMFilterState) {
  452. if (!memcmp(&(pMMFilterState->gFilterID), &gFilterID, sizeof(GUID))) {
  453. return (pMMFilterState);
  454. }
  455. pMMFilterState = pMMFilterState->pNext;
  456. }
  457. return (NULL);
  458. }