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.

618 lines
15 KiB

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