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.

734 lines
18 KiB

  1. #include "precomp.h"
  2. #ifdef TRACE_ON
  3. #include "patx-fil.tmh"
  4. #endif
  5. DWORD
  6. PAAddQMFilters(
  7. PIPSEC_NFA_DATA * ppIpsecNFAData,
  8. DWORD dwNumNFACount,
  9. DWORD dwSource,
  10. BOOL * pbHardError
  11. )
  12. {
  13. DWORD dwError = 0;
  14. DWORD i = 0;
  15. PIPSEC_NFA_DATA pIpsecNFAData = NULL;
  16. BOOL bHardError = FALSE;
  17. BOOL bTempHardError = FALSE;
  18. for (i = 0; i < dwNumNFACount; i++) {
  19. pIpsecNFAData = *(ppIpsecNFAData + i);
  20. if (!(pIpsecNFAData->dwTunnelFlags)) {
  21. dwError = PAAddTxFilterSpecs(
  22. pIpsecNFAData,
  23. dwSource,
  24. &bTempHardError
  25. );
  26. }
  27. else {
  28. dwError = PAAddTnFilterSpecs(
  29. pIpsecNFAData,
  30. dwSource,
  31. &bTempHardError
  32. );
  33. }
  34. if (bTempHardError) {
  35. bHardError = TRUE;
  36. AuditIPSecPolicyErrorEvent(
  37. SE_CATEGID_POLICY_CHANGE,
  38. SE_AUDITID_IPSEC_POLICY_CHANGED,
  39. PASTORE_ADD_QM_FILTER_FAIL,
  40. pIpsecNFAData->pszIpsecName,
  41. dwError,
  42. FALSE,
  43. TRUE
  44. );
  45. }
  46. }
  47. (*pbHardError) = bHardError;
  48. return (dwError);
  49. }
  50. DWORD
  51. PAAddTxFilterSpecs(
  52. PIPSEC_NFA_DATA pIpsecNFAData,
  53. DWORD dwSource,
  54. BOOL * pbHardError
  55. )
  56. {
  57. DWORD dwError = 0;
  58. PIPSEC_NEGPOL_DATA pIpsecNegPolData = NULL;
  59. PIPSEC_FILTER_DATA pIpsecFilterData = NULL;
  60. PQMPOLICYSTATE pQMPolicyState = NULL;
  61. DWORD dwNumFilterSpecs = 0;
  62. PIPSEC_FILTER_SPEC * ppFilterSpecs = NULL;
  63. DWORD i = 0;
  64. PTXFILTERSTATE pTxFilterState = NULL;
  65. PTRANSPORT_FILTER pSPDTxFilter = NULL;
  66. LPWSTR pServerName = NULL;
  67. DWORD dwVersion = 0;
  68. BOOL bHardError = FALSE;
  69. TRACE(TRC_INFORMATION, ("Pastore adding transport filters for rule %!guid!.", &pIpsecNFAData->NFAIdentifier));
  70. pIpsecNegPolData = pIpsecNFAData->pIpsecNegPolData;
  71. if (!memcmp(
  72. &(pIpsecNegPolData->NegPolType),
  73. &(GUID_NEGOTIATION_TYPE_DEFAULT),
  74. sizeof(GUID))) {
  75. TRACE(TRC_INFORMATION, ("Pastore found default response rule: not adding an associated transport filter."));
  76. dwError = ERROR_SUCCESS;
  77. BAIL_OUT;
  78. }
  79. pQMPolicyState = FindQMPolicyState(
  80. pIpsecNegPolData->NegPolIdentifier
  81. );
  82. if (!pQMPolicyState) {
  83. bHardError = TRUE;
  84. dwError = ERROR_INVALID_PARAMETER;
  85. TRACE(
  86. TRC_ERROR,
  87. ("Pastore failed to find associated QM policy %!guid! when adding rule %!guid!.",
  88. &pIpsecNegPolData->NegPolIdentifier,
  89. &pIpsecNFAData->NFAIdentifier)
  90. );
  91. BAIL_ON_WIN32_ERROR(dwError);
  92. }
  93. if (!IsClearOnly(pQMPolicyState->gNegPolAction) &&
  94. !IsBlocking(pQMPolicyState->gNegPolAction) &&
  95. !(pQMPolicyState->bInSPD)) {
  96. bHardError = TRUE;
  97. dwError = ERROR_INVALID_PARAMETER;
  98. TRACE(
  99. TRC_ERROR,
  100. ("Pastore failed to get associated QM policy in SPD %!guid! when adding rule %!guid!.",
  101. &pIpsecNFAData->NFAIdentifier,
  102. &pIpsecNegPolData->NegPolIdentifier)
  103. );
  104. BAIL_ON_WIN32_ERROR(dwError);
  105. }
  106. pIpsecFilterData = pIpsecNFAData->pIpsecFilterData;
  107. if (!pIpsecFilterData) {
  108. TRACE(
  109. TRC_ERROR,
  110. ("Pastore found no associated filter data when adding rule %!guid!.",
  111. &pIpsecNFAData->NFAIdentifier)
  112. );
  113. dwError = ERROR_INVALID_PARAMETER;
  114. SET_IF_HARD_ERROR(
  115. dwError,
  116. pQMPolicyState->gNegPolAction,
  117. bHardError
  118. );
  119. if (!bHardError) {
  120. dwError = ERROR_SUCCESS;
  121. }
  122. BAIL_OUT;
  123. }
  124. dwNumFilterSpecs = pIpsecFilterData->dwNumFilterSpecs;
  125. ppFilterSpecs = pIpsecFilterData->ppFilterSpecs;
  126. for (i = 0; i < dwNumFilterSpecs; i++) {
  127. dwError = PACreateTxFilterState(
  128. pIpsecNegPolData,
  129. pIpsecNFAData,
  130. *(ppFilterSpecs + i),
  131. &pTxFilterState
  132. );
  133. if (dwError) {
  134. SET_IF_HARD_ERROR(
  135. dwError,
  136. pQMPolicyState->gNegPolAction,
  137. bHardError
  138. );
  139. continue;
  140. }
  141. dwError = PACreateTxFilter(
  142. pIpsecNegPolData,
  143. pIpsecNFAData,
  144. *(ppFilterSpecs + i),
  145. pQMPolicyState,
  146. &pSPDTxFilter
  147. );
  148. if (dwError) {
  149. pTxFilterState->hTxFilter = NULL;
  150. pTxFilterState->pNext = gpTxFilterState;
  151. gpTxFilterState = pTxFilterState;
  152. SET_IF_HARD_ERROR(
  153. dwError,
  154. pQMPolicyState->gNegPolAction,
  155. bHardError
  156. );
  157. continue;
  158. }
  159. dwError = AddTransportFilterInternal(
  160. pServerName,
  161. dwVersion,
  162. 0,
  163. dwSource,
  164. pSPDTxFilter,
  165. NULL,
  166. &(pTxFilterState->hTxFilter)
  167. );
  168. // Catch the driver error that can happen because of adding duplicated
  169. // expanded filters. We don't want this to be a hard error.
  170. //
  171. if (dwError == STATUS_DUPLICATE_OBJECTID
  172. || dwError == GPC_STATUS_CONFLICT)
  173. {
  174. AuditIPSecPolicyErrorEvent(
  175. SE_CATEGID_POLICY_CHANGE,
  176. SE_AUDITID_IPSEC_POLICY_CHANGED,
  177. PASTORE_ADD_QM_FILTER_FAIL,
  178. pIpsecNFAData->pszIpsecName,
  179. dwError,
  180. FALSE,
  181. TRUE
  182. );
  183. } else {
  184. SET_IF_HARD_ERROR(
  185. dwError,
  186. pQMPolicyState->gNegPolAction,
  187. bHardError
  188. );
  189. }
  190. pTxFilterState->pNext = gpTxFilterState;
  191. gpTxFilterState = pTxFilterState;
  192. PAFreeTxFilter(pSPDTxFilter);
  193. }
  194. error:
  195. *pbHardError = bHardError;
  196. return (dwError);
  197. }
  198. DWORD
  199. PACreateTxFilterState(
  200. PIPSEC_NEGPOL_DATA pIpsecNegPolData,
  201. PIPSEC_NFA_DATA pIpsecNFAData,
  202. PIPSEC_FILTER_SPEC pFilterSpec,
  203. PTXFILTERSTATE * ppTxFilterState
  204. )
  205. {
  206. DWORD dwError = 0;
  207. PTXFILTERSTATE pTxFilterState = NULL;
  208. dwError = AllocateSPDMemory(
  209. sizeof(TXFILTERSTATE),
  210. &pTxFilterState
  211. );
  212. BAIL_ON_WIN32_ERROR(dwError);
  213. memcpy(
  214. &(pTxFilterState->gFilterID),
  215. &(pFilterSpec->FilterSpecGUID),
  216. sizeof(GUID)
  217. );
  218. memcpy(
  219. &(pTxFilterState->gNFAIdentifier),
  220. &(pIpsecNFAData->NFAIdentifier),
  221. sizeof(GUID)
  222. );
  223. memcpy(
  224. &(pTxFilterState->gPolicyID),
  225. &(pIpsecNegPolData->NegPolIdentifier),
  226. sizeof(GUID)
  227. );
  228. pTxFilterState->hTxFilter = NULL;
  229. pTxFilterState->pNext = NULL;
  230. *ppTxFilterState = pTxFilterState;
  231. return (dwError);
  232. error:
  233. TRACE(
  234. TRC_ERROR,
  235. ("Pastore failed to create state node for transport filter %!guid!. %!winerr!",
  236. &pFilterSpec->FilterSpecGUID,
  237. dwError)
  238. );
  239. *ppTxFilterState = NULL;
  240. return (dwError);
  241. }
  242. DWORD
  243. PACreateTxFilter(
  244. PIPSEC_NEGPOL_DATA pIpsecNegPolData,
  245. PIPSEC_NFA_DATA pIpsecNFAData,
  246. PIPSEC_FILTER_SPEC pFilterSpec,
  247. PQMPOLICYSTATE pQMPolicyState,
  248. PTRANSPORT_FILTER * ppSPDTxFilter
  249. )
  250. {
  251. DWORD dwError = 0;
  252. PTRANSPORT_FILTER pSPDTxFilter = NULL;
  253. WCHAR pszName[512];
  254. dwError = AllocateSPDMemory(
  255. sizeof(TRANSPORT_FILTER),
  256. &pSPDTxFilter
  257. );
  258. BAIL_ON_WIN32_ERROR(dwError);
  259. pSPDTxFilter->IpVersion = IPSEC_PROTOCOL_V4;
  260. memcpy(
  261. &(pSPDTxFilter->gFilterID),
  262. &(pFilterSpec->FilterSpecGUID),
  263. sizeof(GUID)
  264. );
  265. if (pFilterSpec->pszDescription && *(pFilterSpec->pszDescription)) {
  266. dwError = AllocateSPDString(
  267. pFilterSpec->pszDescription,
  268. &(pSPDTxFilter->pszFilterName)
  269. );
  270. BAIL_ON_WIN32_ERROR(dwError);
  271. }
  272. else {
  273. wsprintf(pszName, L"%d", ++gdwTxFilterCounter);
  274. dwError = AllocateSPDString(
  275. pszName,
  276. &(pSPDTxFilter->pszFilterName)
  277. );
  278. BAIL_ON_WIN32_ERROR(dwError);
  279. }
  280. PASetInterfaceType(
  281. pIpsecNFAData->dwInterfaceType,
  282. &(pSPDTxFilter->InterfaceType)
  283. );
  284. pSPDTxFilter->bCreateMirror = (BOOL) pFilterSpec->dwMirrorFlag;
  285. pSPDTxFilter->dwFlags = 0;
  286. PASetAddress(
  287. pFilterSpec->Filter.SrcMask,
  288. pFilterSpec->Filter.SrcAddr,
  289. &(pSPDTxFilter->SrcAddr)
  290. );
  291. PASetAddress(
  292. pFilterSpec->Filter.DestMask,
  293. pFilterSpec->Filter.DestAddr,
  294. &(pSPDTxFilter->DesAddr)
  295. );
  296. if (pFilterSpec->Filter.ExType) {
  297. if (pFilterSpec->Filter.ExType & EXT_DEST) {
  298. pSPDTxFilter->DesAddr.AddrType = ExTypeToAddrType(
  299. pFilterSpec->Filter.ExType
  300. );
  301. } else {
  302. pSPDTxFilter->SrcAddr.AddrType = ExTypeToAddrType(
  303. pFilterSpec->Filter.ExType
  304. );
  305. }
  306. }
  307. pSPDTxFilter->Protocol.ProtocolType = PROTOCOL_UNIQUE;
  308. pSPDTxFilter->Protocol.dwProtocol = pFilterSpec->Filter.Protocol;
  309. pSPDTxFilter->SrcPort.PortType = PORT_UNIQUE;
  310. pSPDTxFilter->SrcPort.wPort = pFilterSpec->Filter.SrcPort;
  311. pSPDTxFilter->DesPort.PortType = PORT_UNIQUE;
  312. pSPDTxFilter->DesPort.wPort = pFilterSpec->Filter.DestPort;
  313. SetFilterActions(
  314. pQMPolicyState,
  315. &(pSPDTxFilter->InboundFilterAction),
  316. &(pSPDTxFilter->OutboundFilterAction)
  317. );
  318. pSPDTxFilter->dwDirection = 0;
  319. pSPDTxFilter->dwWeight = 0;
  320. memcpy(
  321. &(pSPDTxFilter->gPolicyID),
  322. &(pIpsecNegPolData->NegPolIdentifier),
  323. sizeof(GUID)
  324. );
  325. *ppSPDTxFilter = pSPDTxFilter;
  326. return (dwError);
  327. error:
  328. TRACE(
  329. TRC_WARNING,
  330. ("Pastore failed to create transport filter %!guid!. %!winerr!",
  331. &pFilterSpec->FilterSpecGUID,
  332. dwError)
  333. );
  334. if (pSPDTxFilter) {
  335. PAFreeTxFilter(
  336. pSPDTxFilter
  337. );
  338. }
  339. *ppSPDTxFilter = NULL;
  340. return (dwError);
  341. }
  342. VOID
  343. SetFilterActions(
  344. PQMPOLICYSTATE pQMPolicyState,
  345. PFILTER_ACTION pInboundFilterFlag,
  346. PFILTER_ACTION pOutboundFilterFlag
  347. )
  348. {
  349. *pInboundFilterFlag = NEGOTIATE_SECURITY;
  350. *pOutboundFilterFlag = NEGOTIATE_SECURITY;
  351. if (IsBlocking(pQMPolicyState->gNegPolAction)) {
  352. *pInboundFilterFlag = BLOCKING;
  353. *pOutboundFilterFlag = BLOCKING;
  354. }
  355. else if (IsClearOnly(pQMPolicyState->gNegPolAction)) {
  356. *pInboundFilterFlag = PASS_THRU;
  357. *pOutboundFilterFlag = PASS_THRU;
  358. }
  359. else if (IsInboundPassThru(pQMPolicyState->gNegPolAction)) {
  360. *pInboundFilterFlag = PASS_THRU;
  361. }
  362. if (pQMPolicyState->bAllowsSoft && gbBackwardSoftSA) {
  363. *pInboundFilterFlag = PASS_THRU;
  364. }
  365. }
  366. VOID
  367. PAFreeTxFilter(
  368. PTRANSPORT_FILTER pSPDTxFilter
  369. )
  370. {
  371. if (pSPDTxFilter) {
  372. if (pSPDTxFilter->pszFilterName) {
  373. FreeSPDString(pSPDTxFilter->pszFilterName);
  374. }
  375. FreeSPDMemory(pSPDTxFilter);
  376. }
  377. return;
  378. }
  379. DWORD
  380. PADeleteAllTxFilters(
  381. )
  382. {
  383. DWORD dwError = 0;
  384. PTXFILTERSTATE pTxFilterState = NULL;
  385. PTXFILTERSTATE pTemp = NULL;
  386. PTXFILTERSTATE pLeftTxFilterState = NULL;
  387. TRACE(TRC_INFORMATION, (L"Pastore deleting all its transport filters"));
  388. pTxFilterState = gpTxFilterState;
  389. while (pTxFilterState) {
  390. if (pTxFilterState->hTxFilter) {
  391. dwError = DeleteTransportFilter(
  392. pTxFilterState->hTxFilter
  393. );
  394. if (!dwError) {
  395. pTemp = pTxFilterState;
  396. pTxFilterState = pTxFilterState->pNext;
  397. FreeSPDMemory(pTemp);
  398. }
  399. else {
  400. pTemp = pTxFilterState;
  401. pTxFilterState = pTxFilterState->pNext;
  402. pTemp->pNext = pLeftTxFilterState;
  403. pLeftTxFilterState = pTemp;
  404. }
  405. }
  406. else {
  407. pTemp = pTxFilterState;
  408. pTxFilterState = pTxFilterState->pNext;
  409. FreeSPDMemory(pTemp);
  410. }
  411. }
  412. gpTxFilterState = pLeftTxFilterState;
  413. return (dwError);
  414. }
  415. VOID
  416. PAFreeTxFilterStateList(
  417. PTXFILTERSTATE pTxFilterState
  418. )
  419. {
  420. PTXFILTERSTATE pTemp = NULL;
  421. while (pTxFilterState) {
  422. pTemp = pTxFilterState;
  423. pTxFilterState = pTxFilterState->pNext;
  424. FreeSPDMemory(pTemp);
  425. }
  426. }
  427. DWORD
  428. PADeleteQMFilters(
  429. PIPSEC_NFA_DATA * ppIpsecNFAData,
  430. DWORD dwNumNFACount
  431. )
  432. {
  433. DWORD dwError = 0;
  434. DWORD i = 0;
  435. PIPSEC_NFA_DATA pIpsecNFAData = NULL;
  436. for (i = 0; i < dwNumNFACount; i++) {
  437. pIpsecNFAData = *(ppIpsecNFAData + i);
  438. TRACE(
  439. TRC_INFORMATION,
  440. ("Pastore deleting transport/tunnel filters generated from rule %!guid!",
  441. &pIpsecNFAData->NFAIdentifier)
  442. );
  443. if (!(pIpsecNFAData->dwTunnelFlags)) {
  444. dwError = PADeleteTxFilterSpecs(
  445. pIpsecNFAData
  446. );
  447. }
  448. else {
  449. dwError = PADeleteTnFilterSpecs(
  450. pIpsecNFAData
  451. );
  452. }
  453. }
  454. return (dwError);
  455. }
  456. DWORD
  457. PADeleteTxFilterSpecs(
  458. PIPSEC_NFA_DATA pIpsecNFAData
  459. )
  460. {
  461. DWORD dwError = 0;
  462. PIPSEC_NEGPOL_DATA pIpsecNegPolData = NULL;
  463. PIPSEC_FILTER_DATA pIpsecFilterData = NULL;
  464. DWORD dwNumFilterSpecs = 0;
  465. PIPSEC_FILTER_SPEC * ppFilterSpecs = NULL;
  466. DWORD i = 0;
  467. PIPSEC_FILTER_SPEC pFilterSpec = NULL;
  468. pIpsecNegPolData = pIpsecNFAData->pIpsecNegPolData;
  469. if (!memcmp(
  470. &(pIpsecNegPolData->NegPolType),
  471. &(GUID_NEGOTIATION_TYPE_DEFAULT),
  472. sizeof(GUID))) {
  473. dwError = ERROR_SUCCESS;
  474. return (dwError);
  475. }
  476. pIpsecFilterData = pIpsecNFAData->pIpsecFilterData;
  477. if (!pIpsecFilterData) {
  478. dwError = ERROR_SUCCESS;
  479. return (dwError);
  480. }
  481. dwNumFilterSpecs = pIpsecFilterData->dwNumFilterSpecs;
  482. ppFilterSpecs = pIpsecFilterData->ppFilterSpecs;
  483. for (i = 0; i < dwNumFilterSpecs; i++) {
  484. pFilterSpec = *(ppFilterSpecs + i);
  485. dwError = PADeleteTxFilter(
  486. pFilterSpec->FilterSpecGUID,
  487. pIpsecNFAData->NFAIdentifier
  488. );
  489. }
  490. return (dwError);
  491. }
  492. DWORD
  493. PADeleteTxFilter(
  494. GUID gFilterID,
  495. GUID gNFAIdentifier
  496. )
  497. {
  498. DWORD dwError = 0;
  499. PTXFILTERSTATE pTxFilterState = NULL;
  500. pTxFilterState = FindTxFilterState(
  501. gFilterID,
  502. gNFAIdentifier
  503. );
  504. if (!pTxFilterState) {
  505. dwError = ERROR_SUCCESS;
  506. return (dwError);
  507. }
  508. if (pTxFilterState->hTxFilter) {
  509. dwError = DeleteTransportFilter(
  510. pTxFilterState->hTxFilter
  511. );
  512. BAIL_ON_WIN32_ERROR(dwError);
  513. }
  514. PADeleteTxFilterState(pTxFilterState);
  515. error:
  516. #ifdef TRACE_ON
  517. if (dwError) {
  518. TRACE(
  519. TRC_WARNING,
  520. ("Pastore failed to delete transport filter %!guid!. %!winerr!",
  521. &gFilterID,
  522. dwError)
  523. );
  524. }
  525. #endif
  526. return (dwError);
  527. }
  528. VOID
  529. PADeleteTxFilterState(
  530. PTXFILTERSTATE pTxFilterState
  531. )
  532. {
  533. PTXFILTERSTATE * ppTemp = NULL;
  534. ppTemp = &gpTxFilterState;
  535. while (*ppTemp) {
  536. if (*ppTemp == pTxFilterState) {
  537. break;
  538. }
  539. ppTemp = &((*ppTemp)->pNext);
  540. }
  541. if (*ppTemp) {
  542. *ppTemp = pTxFilterState->pNext;
  543. }
  544. FreeSPDMemory(pTxFilterState);
  545. return;
  546. }
  547. PTXFILTERSTATE
  548. FindTxFilterState(
  549. GUID gFilterID,
  550. GUID gNFAIdentifier
  551. )
  552. {
  553. PTXFILTERSTATE pTxFilterState = NULL;
  554. pTxFilterState = gpTxFilterState;
  555. while (pTxFilterState) {
  556. if (!memcmp(&(pTxFilterState->gFilterID), &gFilterID, sizeof(GUID))
  557. && !memcmp(&(pTxFilterState->gNFAIdentifier), &gNFAIdentifier, sizeof(GUID)))
  558. {
  559. return (pTxFilterState);
  560. }
  561. pTxFilterState = pTxFilterState->pNext;
  562. }
  563. return (NULL);
  564. }