Leaked source code of windows server 2003
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.

700 lines
16 KiB

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