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.

2246 lines
56 KiB

  1. ////////////////////////////////////////////////////////////////////////
  2. //
  3. // Module : Dynamic/DyanamicAdd.cpp
  4. //
  5. // Purpose : Dynamic Module Implementation.
  6. //
  7. //
  8. // Developers Name : Bharat/Radhika
  9. //
  10. // Description : All functions are related to add and set functionality.
  11. //
  12. //
  13. // History :
  14. //
  15. // Date Author Comments
  16. // 8-10-2001 Bharat Initial Version. V1.0
  17. //
  18. ////////////////////////////////////////////////////////////////////////
  19. #include "nshipsec.h"
  20. extern HINSTANCE g_hModule;
  21. extern HKEY g_hGlobalRegistryKey;
  22. extern _TCHAR* g_szDynamicMachine;
  23. ///////////////////////////////////////////////////////////////////////////////////////////
  24. //
  25. // Function : AddMainModePolicy
  26. //
  27. // Date of Creation: 09-22-01
  28. //
  29. // Parameters : IN LPTSTR pPolicyName,
  30. // IN IPSEC_MM_POLICY& MMPol
  31. //
  32. // Return : DWORD
  33. //
  34. // Description : This function adds a Main Mode policy into the SPD
  35. //
  36. // Revision History:
  37. //
  38. // Date Author Comments
  39. //
  40. //////////////////////////////////////////////////////////////////////////////////////////
  41. DWORD
  42. AddMainModePolicy(
  43. IN LPTSTR pPolicyName,
  44. IN IPSEC_MM_POLICY& MMPol
  45. )
  46. {
  47. PIPSEC_MM_POLICY pMMPol = NULL;
  48. RPC_STATUS RpcStat;
  49. DWORD dwReturn = ERROR_SUCCESS;
  50. DWORD dwVersion = 0;
  51. DWORD dwNameLen = 0;
  52. BOOL bExists = FALSE;
  53. //
  54. // check if policy already exists
  55. //
  56. dwReturn = GetMMPolicy(g_szDynamicMachine, dwVersion, pPolicyName, &pMMPol, NULL);
  57. if (dwReturn == ERROR_SUCCESS)
  58. {
  59. bExists = TRUE;
  60. BAIL_OUT;
  61. }
  62. //
  63. // allocate memory for the policy name
  64. //
  65. MMPol.pszPolicyName = NULL;
  66. dwNameLen = _tcslen(pPolicyName) + 1;
  67. MMPol.pszPolicyName = new _TCHAR[dwNameLen];
  68. if(MMPol.pszPolicyName == NULL)
  69. {
  70. dwReturn = ERROR_OUTOFMEMORY;
  71. BAIL_OUT;
  72. }
  73. _tcsncpy(MMPol.pszPolicyName,pPolicyName,dwNameLen);
  74. //
  75. // generate GUID for mmpolicy id
  76. //
  77. RpcStat = UuidCreate(&(MMPol.gPolicyID));
  78. if(!(RpcStat == RPC_S_OK || RpcStat == RPC_S_UUID_LOCAL_ONLY))
  79. {
  80. dwReturn = ERROR_INVALID_PARAMETER;
  81. BAIL_OUT;
  82. }
  83. //dwReturn value is checked in the parent function for success or failure
  84. dwReturn = AddMMPolicy(g_szDynamicMachine, dwVersion, 0, &MMPol, NULL);
  85. error:
  86. if(bExists)
  87. {
  88. //functionality error
  89. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_ADD_MMP_MMPOLICY_EXISTS);
  90. dwReturn = ERROR_NO_DISPLAY;
  91. }
  92. //error path clean up
  93. if(MMPol.pszPolicyName)
  94. {
  95. delete [] MMPol.pszPolicyName;
  96. MMPol.pszPolicyName = NULL;
  97. }
  98. if(pMMPol)
  99. {
  100. SPDApiBufferFree(pMMPol);
  101. pMMPol = NULL;
  102. }
  103. return dwReturn;
  104. }
  105. ///////////////////////////////////////////////////////////////////////////////////////////
  106. //
  107. // Function : SetMainModePolicy
  108. //
  109. // Date of Creation: 22-09-01
  110. //
  111. // Parameters : IN LPTSTR pPolicyName,
  112. // IN IPSEC_MM_POLICY& MMPol
  113. //
  114. // Return : DWORD
  115. //
  116. // Description : This Function sets a main mode policy. It sets all the parameters
  117. // except the name.
  118. //
  119. // Revision History:
  120. //
  121. // Date Author Comments
  122. //
  123. //////////////////////////////////////////////////////////////////////////////////////////
  124. DWORD
  125. SetMainModePolicy(
  126. IN LPTSTR pPolicyName,
  127. IN IPSEC_MM_POLICY& MMPol
  128. )
  129. {
  130. DWORD dwReturn = ERROR_SUCCESS;
  131. DWORD dwVersion = 0;
  132. //dwReturn value is checked in the parent function
  133. dwReturn = SetMMPolicy(g_szDynamicMachine, dwVersion, pPolicyName, &MMPol, NULL);
  134. return dwReturn;
  135. }
  136. ///////////////////////////////////////////////////////////////////////////////////////////
  137. //
  138. // Function : LoadMMOffersDefaults
  139. //
  140. // Date of Creation: 09-22-01
  141. //
  142. // Parameters : OUT IPSEC_MM_POLICY & MMPolicy
  143. //
  144. // Return : DWORD
  145. //
  146. // Description : Loads the Main Mode policy defaults into the IPSEC_MM_POLICY structure.
  147. //
  148. // Revision History:
  149. //
  150. // Date Author Comments
  151. //
  152. //////////////////////////////////////////////////////////////////////////////////////////
  153. DWORD
  154. LoadMMOffersDefaults(
  155. OUT IPSEC_MM_POLICY & MMPolicy
  156. )
  157. {
  158. DWORD dwReturn = ERROR_SUCCESS;
  159. MMPolicy.dwOfferCount = 3;
  160. MMPolicy.pOffers = NULL;
  161. MMPolicy.pOffers = new IPSEC_MM_OFFER[MMPolicy.dwOfferCount];
  162. if(MMPolicy.pOffers == NULL)
  163. {
  164. dwReturn = ERROR_OUTOFMEMORY;
  165. BAIL_OUT;
  166. }
  167. memset(MMPolicy.pOffers, 0, sizeof(IPSEC_MM_OFFER) * MMPolicy.dwOfferCount);
  168. //
  169. // initialize
  170. //
  171. for (UINT i = 0; i < MMPolicy.dwOfferCount; ++i)
  172. {
  173. MMPolicy.pOffers[i].dwQuickModeLimit = POTF_DEFAULT_P1REKEY_QMS;
  174. MMPolicy.pOffers[i].Lifetime.uKeyExpirationKBytes = 0;
  175. MMPolicy.pOffers[i].Lifetime.uKeyExpirationTime = POTF_DEFAULT_P1REKEY_TIME;
  176. }
  177. MMPolicy.pOffers[0].EncryptionAlgorithm.uAlgoIdentifier = CONF_ALGO_3_DES;
  178. MMPolicy.pOffers[0].EncryptionAlgorithm.uAlgoKeyLen = POTF_OAKLEY_ALGOKEYLEN;
  179. MMPolicy.pOffers[0].EncryptionAlgorithm.uAlgoRounds = POTF_OAKLEY_ALGOROUNDS;
  180. MMPolicy.pOffers[0].HashingAlgorithm.uAlgoIdentifier = AUTH_ALGO_SHA1;
  181. MMPolicy.pOffers[0].HashingAlgorithm.uAlgoKeyLen = POTF_OAKLEY_ALGOKEYLEN;
  182. MMPolicy.pOffers[0].dwDHGroup = (DWORD)POTF_OAKLEY_GROUP2;
  183. MMPolicy.pOffers[1].EncryptionAlgorithm.uAlgoIdentifier = CONF_ALGO_3_DES;
  184. MMPolicy.pOffers[1].EncryptionAlgorithm.uAlgoKeyLen = POTF_OAKLEY_ALGOKEYLEN;
  185. MMPolicy.pOffers[1].EncryptionAlgorithm.uAlgoRounds = POTF_OAKLEY_ALGOROUNDS;
  186. MMPolicy.pOffers[1].HashingAlgorithm.uAlgoIdentifier = AUTH_ALGO_MD5;
  187. MMPolicy.pOffers[1].HashingAlgorithm.uAlgoKeyLen = POTF_OAKLEY_ALGOKEYLEN;
  188. MMPolicy.pOffers[1].dwDHGroup = (DWORD)POTF_OAKLEY_GROUP2;
  189. MMPolicy.pOffers[2].EncryptionAlgorithm.uAlgoIdentifier = CONF_ALGO_3_DES;
  190. MMPolicy.pOffers[2].EncryptionAlgorithm.uAlgoKeyLen = POTF_OAKLEY_ALGOKEYLEN;
  191. MMPolicy.pOffers[2].EncryptionAlgorithm.uAlgoRounds = POTF_OAKLEY_ALGOROUNDS;
  192. MMPolicy.pOffers[2].HashingAlgorithm.uAlgoIdentifier = AUTH_ALGO_SHA1;
  193. MMPolicy.pOffers[2].HashingAlgorithm.uAlgoKeyLen = POTF_OAKLEY_ALGOKEYLEN;
  194. MMPolicy.pOffers[2].dwDHGroup = (DWORD)POTF_OAKLEY_GROUP2048;
  195. error:
  196. return dwReturn;
  197. }
  198. ///////////////////////////////////////////////////////////////////////////////////////////
  199. //
  200. // Function : AddQuickModePolicy
  201. //
  202. // Date of Creation: 09-22-01
  203. //
  204. // Parameters : IN LPTSTR pPolicyName,
  205. // IN BOOL bDefault,
  206. // IN BOOL bSoft,
  207. // IN IPSEC_QM_POLICY& QMPol
  208. // Return : DWORD
  209. //
  210. // Description : This function adds a quick mode policy into the SPD
  211. //
  212. // Revision History:
  213. //
  214. // Date Author Comments
  215. //
  216. //////////////////////////////////////////////////////////////////////////////////////////
  217. DWORD
  218. AddQuickModePolicy(
  219. IN LPTSTR pPolicyName,
  220. IN BOOL bDefault,
  221. IN BOOL bSoft,
  222. IN IPSEC_QM_POLICY& QMPol)
  223. {
  224. PIPSEC_QM_POLICY pQMPol = NULL;
  225. RPC_STATUS RpcStat = RPC_S_OK;
  226. DWORD dwReturn = ERROR_SUCCESS;
  227. DWORD dwVersion = 0;
  228. DWORD dwNameLen = 0;
  229. BOOL bExists = FALSE;
  230. //
  231. // Check if the policy already exists
  232. //
  233. dwReturn = GetQMPolicy(g_szDynamicMachine, dwVersion, pPolicyName, 0, &pQMPol, NULL);
  234. if (dwReturn == ERROR_SUCCESS)
  235. {
  236. bExists = TRUE;
  237. BAIL_OUT;
  238. }
  239. //
  240. // Fill up QM policy GUID
  241. //
  242. RpcStat = UuidCreate(&(QMPol.gPolicyID));
  243. if(!(RpcStat == RPC_S_OK || RpcStat == RPC_S_UUID_LOCAL_ONLY))
  244. {
  245. dwReturn = ERROR_INVALID_PARAMETER;
  246. BAIL_OUT;
  247. }
  248. dwNameLen = _tcslen(pPolicyName) + 1;
  249. //
  250. // Allocate memory for the name
  251. //
  252. QMPol.pszPolicyName = NULL;
  253. QMPol.pszPolicyName = new _TCHAR[dwNameLen];
  254. if(QMPol.pszPolicyName == NULL)
  255. {
  256. dwReturn = ERROR_OUTOFMEMORY;
  257. BAIL_OUT;
  258. }
  259. _tcsncpy(QMPol.pszPolicyName, pPolicyName, dwNameLen);
  260. if(bDefault)
  261. {
  262. QMPol.dwFlags |= IPSEC_QM_POLICY_DEFAULT_POLICY;
  263. }
  264. if(bSoft)
  265. {
  266. QMPol.dwFlags |= IPSEC_QM_POLICY_ALLOW_SOFT;
  267. }
  268. //
  269. // Add the QM Policy
  270. //
  271. dwReturn = AddQMPolicy(g_szDynamicMachine, dwVersion,0, &QMPol, NULL);
  272. error:
  273. if(bExists)
  274. {
  275. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_ADD_QMP_QMPOLICY_EXISTS);
  276. dwReturn = ERROR_NO_DISPLAY;
  277. }
  278. //error path clean up
  279. if(QMPol.pszPolicyName)
  280. {
  281. delete[] QMPol.pszPolicyName;
  282. QMPol.pszPolicyName = NULL;
  283. }
  284. if(pQMPol)
  285. {
  286. SPDApiBufferFree(pQMPol);
  287. pQMPol = NULL;
  288. }
  289. return dwReturn;
  290. }
  291. ///////////////////////////////////////////////////////////////////////////////////////////
  292. //
  293. // Function : SetQuickModePolicy
  294. //
  295. // Date of Creation: 09-22-01
  296. //
  297. // Parameters : IN LPTSTR pPolicyName,
  298. // IN PIPSEC_QM_POLICY pQMPol
  299. //
  300. // Return: DWORD
  301. //
  302. // Description : This sets the quick mode policy into the SPD.
  303. // Except the name, all other parameters can be modified
  304. //
  305. // Revision History:
  306. //
  307. // Date Author Comments
  308. //
  309. //////////////////////////////////////////////////////////////////////////////////////////
  310. DWORD
  311. SetQuickModePolicy(
  312. IN LPTSTR pPolicyName,
  313. IN PIPSEC_QM_POLICY pQMPol
  314. )
  315. {
  316. DWORD dwReturn = ERROR_SUCCESS;
  317. DWORD dwVersion = 0;
  318. dwReturn = SetQMPolicy(g_szDynamicMachine, dwVersion, pPolicyName, pQMPol, NULL);
  319. return dwReturn;
  320. }
  321. ///////////////////////////////////////////////////////////////////////////////////////////
  322. //
  323. // Function : AddQuickModeFilter
  324. //
  325. // Date of Creation: 09-22-01
  326. //
  327. // Parameters : IN LPTSTR pFilterName,
  328. // IN LPTSTR pPolicyName,
  329. // IN TRANSPORT_FILTER& TrpFltr
  330. //
  331. // Return : DWORD
  332. //
  333. // Description : This function adds the Quick Mode Transport Filter into SPD
  334. //
  335. // Revision History:
  336. //
  337. // Date Author Comments
  338. //
  339. //////////////////////////////////////////////////////////////////////////////////////////
  340. DWORD
  341. AddQuickModeFilter(
  342. IN LPTSTR pFilterName,
  343. IN LPTSTR pPolicyName,
  344. IN TRANSPORT_FILTER& TrpFltr
  345. )
  346. {
  347. PIPSEC_QM_POLICY pQMPol = NULL;
  348. RPC_STATUS RpcStat = RPC_S_OK;
  349. HANDLE hTrpFilter = NULL;
  350. DWORD dwReturn = ERROR_SUCCESS;
  351. DWORD dwNameLen = 0;
  352. DWORD dwVersion = 0;
  353. BOOL bQMPExists = FALSE;
  354. TrpFltr.pszFilterName = NULL;
  355. if(pPolicyName == NULL)
  356. {
  357. //
  358. // Create a NULL GUID if qmpolicy does not exist
  359. //
  360. RpcStat = UuidCreateNil(&(TrpFltr.gPolicyID));
  361. if(!(RpcStat == RPC_S_OK || RpcStat == RPC_S_UUID_LOCAL_ONLY))
  362. {
  363. dwReturn = ERROR_INVALID_PARAMETER;
  364. BAIL_OUT;
  365. }
  366. bQMPExists = TRUE;
  367. }
  368. else
  369. {
  370. //
  371. // Get the corresponding QMPolicy GUID if it exists
  372. //
  373. dwReturn = GetQMPolicy(g_szDynamicMachine, dwVersion, pPolicyName, 0, &pQMPol, NULL);
  374. if (!((dwReturn == ERROR_SUCCESS) && pQMPol))
  375. {
  376. BAIL_OUT;
  377. }
  378. TrpFltr.gPolicyID = pQMPol->gPolicyID;
  379. bQMPExists = TRUE;
  380. }
  381. //
  382. // Create Transport Filter GUID
  383. //
  384. RpcStat = UuidCreate(&(TrpFltr.gFilterID));
  385. if(!(RpcStat == RPC_S_OK || RpcStat == RPC_S_UUID_LOCAL_ONLY))
  386. {
  387. dwReturn = ERROR_INVALID_PARAMETER;
  388. BAIL_OUT;
  389. }
  390. dwNameLen = _tcslen(pFilterName) + 1;
  391. //
  392. // Allocate memory for the name field
  393. //
  394. TrpFltr.pszFilterName = new _TCHAR[dwNameLen];
  395. if(TrpFltr.pszFilterName == NULL)
  396. {
  397. dwReturn = ERROR_OUTOFMEMORY;
  398. BAIL_OUT;
  399. }
  400. _tcsncpy(TrpFltr.pszFilterName, pFilterName, dwNameLen);
  401. TrpFltr.dwFlags = 0;
  402. TrpFltr.IpVersion = IPSEC_PROTOCOL_V4;
  403. dwReturn = AddTransportFilter(g_szDynamicMachine, dwVersion, 0, &TrpFltr,NULL, &hTrpFilter);
  404. if (dwReturn == ERROR_SUCCESS)
  405. {
  406. dwReturn = CloseTransportFilterHandle(hTrpFilter);
  407. }
  408. error:
  409. if(!bQMPExists)
  410. {
  411. //functionality errors
  412. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_ADD_QMF_NO_QMPOLICY);
  413. dwReturn = ERROR_NO_DISPLAY;
  414. }
  415. //error path clean up
  416. if(TrpFltr.pszFilterName)
  417. {
  418. delete [] TrpFltr.pszFilterName;
  419. TrpFltr.pszFilterName = NULL;
  420. }
  421. if(pQMPol)
  422. {
  423. SPDApiBufferFree(pQMPol);
  424. pQMPol = NULL;
  425. }
  426. return dwReturn;
  427. }
  428. ///////////////////////////////////////////////////////////////////////////////////////////
  429. //
  430. //Function:AddQuickModeFilter
  431. //
  432. //Date of Creation:09-22-01
  433. //
  434. //Parameters: IN LPTSTR pFilterName,
  435. // IN LPTSTR pPolicyName,
  436. // IN TUNNEL_FILTER& TunnelFltr
  437. //
  438. //Return: DWORD
  439. //
  440. //Description:This function adds the quick mode tunnel filter into the SPD
  441. //
  442. //Revision History:
  443. //
  444. // Date Author Comments
  445. //
  446. //////////////////////////////////////////////////////////////////////////////////////////
  447. DWORD
  448. AddQuickModeFilter(
  449. IN LPTSTR pFilterName,
  450. IN LPTSTR pPolicyName,
  451. IN TUNNEL_FILTER& TunnelFltr
  452. )
  453. {
  454. PIPSEC_QM_POLICY pQMPol = NULL;
  455. RPC_STATUS RpcStat = RPC_S_OK;
  456. HANDLE hTrpFilter = NULL;
  457. DWORD dwReturn = ERROR_SUCCESS;
  458. DWORD dwNameLen = 0;
  459. DWORD dwVersion = 0;
  460. BOOL bQMPExists = FALSE;
  461. TunnelFltr.pszFilterName = NULL;
  462. if(pPolicyName == NULL)
  463. {
  464. //Create a NULL GUID if qmpolicy does not exist
  465. RpcStat = UuidCreateNil(&(TunnelFltr.gPolicyID));
  466. if(!(RpcStat == RPC_S_OK || RpcStat == RPC_S_UUID_LOCAL_ONLY))
  467. {
  468. dwReturn = ERROR_INVALID_PARAMETER;
  469. BAIL_OUT;
  470. }
  471. bQMPExists = TRUE;
  472. }
  473. else
  474. {
  475. //Get the corresponding QMPolicy GUID if it exists
  476. dwReturn = GetQMPolicy(g_szDynamicMachine,dwVersion, pPolicyName, 0, &pQMPol, NULL);
  477. if (!((dwReturn == ERROR_SUCCESS) && pQMPol))
  478. {
  479. BAIL_OUT;
  480. }
  481. TunnelFltr.gPolicyID = pQMPol->gPolicyID;
  482. bQMPExists = TRUE;
  483. }
  484. //Create Tunnel Filter GUID
  485. RpcStat = UuidCreate(&(TunnelFltr.gFilterID));
  486. if(!(RpcStat == RPC_S_OK || RpcStat == RPC_S_UUID_LOCAL_ONLY))
  487. {
  488. dwReturn = ERROR_INVALID_PARAMETER;
  489. BAIL_OUT;
  490. }
  491. dwNameLen = _tcslen(pFilterName) + 1;
  492. // Allocate memory for the name field
  493. TunnelFltr.pszFilterName = new _TCHAR[dwNameLen];
  494. if(TunnelFltr.pszFilterName == NULL)
  495. {
  496. dwReturn = ERROR_OUTOFMEMORY;
  497. BAIL_OUT;
  498. }
  499. _tcsncpy(TunnelFltr.pszFilterName, pFilterName, dwNameLen);
  500. TunnelFltr.dwFlags = 0;
  501. TunnelFltr.IpVersion = IPSEC_PROTOCOL_V4;
  502. dwReturn = AddTunnelFilter(g_szDynamicMachine, dwVersion, 0, &TunnelFltr, NULL, &hTrpFilter);
  503. if (dwReturn == ERROR_SUCCESS)
  504. {
  505. dwReturn = CloseTunnelFilterHandle(hTrpFilter);
  506. }
  507. error:
  508. if(!bQMPExists)
  509. {
  510. //functionality errors
  511. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_ADD_QMF_NO_QMPOLICY);
  512. dwReturn = ERROR_NO_DISPLAY;
  513. }
  514. //error path clean up
  515. if(pQMPol)
  516. {
  517. SPDApiBufferFree(pQMPol);
  518. pQMPol = NULL;
  519. }
  520. if(TunnelFltr.pszFilterName)
  521. {
  522. delete [] TunnelFltr.pszFilterName;
  523. TunnelFltr.pszFilterName = NULL;
  524. }
  525. return dwReturn;
  526. }
  527. ///////////////////////////////////////////////////////////////////////////////////////////
  528. //
  529. //Function:AddMainModeFilter
  530. //
  531. //Date of Creation:09-22-01
  532. //
  533. //Parameters: IN LPTSTR pFilterName,
  534. // IN LPTSTR pPolicyName,
  535. // IN MM_FILTER& MMFilter,
  536. // IN INT_MM_AUTH_METHODS& ParserAuthMethod
  537. //
  538. //Return: DWORD
  539. //
  540. //Description:This function adds the main mode filter into the SPD
  541. //
  542. //Revision History:
  543. //
  544. // Date Author Comments
  545. //
  546. //////////////////////////////////////////////////////////////////////////////////////////
  547. DWORD
  548. AddMainModeFilter(
  549. IN LPTSTR pFilterName,
  550. IN LPTSTR pPolicyName,
  551. IN MM_FILTER& MMFilter,
  552. IN STA_AUTH_METHODS& ParserAuthMethod
  553. )
  554. {
  555. PIPSEC_MM_POLICY pMMPol = NULL;
  556. RPC_STATUS RpcStat = RPC_S_OK;
  557. HANDLE hMMFilter = NULL;
  558. DWORD dwReturn = ERROR_SUCCESS;
  559. DWORD dwNameLen = 0;
  560. DWORD dwVersion = 0;
  561. BOOL bPolExists = FALSE;
  562. MMFilter.pszFilterName = NULL;
  563. //check if policy exists
  564. dwReturn = GetMMPolicy(g_szDynamicMachine,dwVersion, pPolicyName, &pMMPol,NULL);
  565. if(dwReturn != ERROR_SUCCESS)
  566. {
  567. BAIL_OUT;
  568. }
  569. bPolExists = TRUE;
  570. //
  571. // Generate GUID for Main mode filter
  572. //
  573. RpcStat = UuidCreate(&(MMFilter.gFilterID));
  574. if(!(RpcStat == RPC_S_OK || RpcStat == RPC_S_UUID_LOCAL_ONLY))
  575. {
  576. dwReturn = ERROR_INVALID_PARAMETER;
  577. BAIL_OUT;
  578. }
  579. dwNameLen = _tcslen(pFilterName) + 1;
  580. //
  581. // allocate memory for main mode filter name
  582. //
  583. MMFilter.pszFilterName = new _TCHAR[dwNameLen];
  584. if(MMFilter.pszFilterName == NULL)
  585. {
  586. dwReturn = ERROR_OUTOFMEMORY;
  587. BAIL_OUT;
  588. }
  589. _tcsncpy(MMFilter.pszFilterName,pFilterName, dwNameLen);
  590. //
  591. // Add the corresponding authentication methods to the main mode filter
  592. //
  593. dwReturn = AddAuthMethods(ParserAuthMethod);
  594. if(dwReturn != ERROR_SUCCESS)
  595. {
  596. BAIL_OUT;
  597. }
  598. MMFilter.gMMAuthID = ParserAuthMethod.gMMAuthID;
  599. MMFilter.gPolicyID = pMMPol->gPolicyID;
  600. MMFilter.IpVersion = IPSEC_PROTOCOL_V4;
  601. MMFilter.SrcAddr.pgInterfaceID = NULL;
  602. MMFilter.DesAddr.pgInterfaceID = NULL;
  603. dwReturn = AddMMFilter(g_szDynamicMachine, dwVersion, 0, &MMFilter, NULL, &hMMFilter);
  604. if(dwReturn == ERROR_SUCCESS)
  605. {
  606. dwReturn = CloseMMFilterHandle(hMMFilter);
  607. }
  608. error:
  609. // functionality errors
  610. if(!bPolExists)
  611. {
  612. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_ADD_MMF_NO_MMPOLICY);
  613. dwReturn = ERROR_NO_DISPLAY;
  614. }
  615. //error path clean up
  616. if(pMMPol)
  617. {
  618. SPDApiBufferFree(pMMPol);
  619. pMMPol = NULL;
  620. }
  621. if(MMFilter.pszFilterName)
  622. {
  623. delete [] MMFilter.pszFilterName;
  624. MMFilter.pszFilterName = NULL;
  625. }
  626. return dwReturn;
  627. }
  628. ///////////////////////////////////////////////////////////////////////////////////////////
  629. //
  630. // Function : AddAuthMethods
  631. //
  632. // Date of Creation : 09-22-01
  633. //
  634. // Parameters : IN MM_AUTH_METHODS& ParserAuthMethod
  635. //
  636. // Return : DWORD
  637. //
  638. // Description : This function adds authentication methods into the SPD.
  639. //
  640. // Revision History :
  641. //
  642. // Date Author Comments
  643. //
  644. //////////////////////////////////////////////////////////////////////////////////////////
  645. DWORD
  646. AddAuthMethods(
  647. IN STA_AUTH_METHODS& ParserAuthMethod
  648. )
  649. {
  650. DWORD dwReturn = 0;
  651. DWORD dwVersion = 0;
  652. RPC_STATUS RpcStat = RPC_S_OK;
  653. PMM_AUTH_METHODS pExtMMAuth;
  654. LPVOID lpVoid = NULL;
  655. //
  656. // Generate Authentication GUID
  657. //
  658. RpcStat = UuidCreate(&(ParserAuthMethod.gMMAuthID));
  659. if(!(RpcStat == RPC_S_OK || RpcStat == RPC_S_UUID_LOCAL_ONLY))
  660. {
  661. dwReturn = ERROR_INVALID_PARAMETER;
  662. BAIL_OUT;
  663. }
  664. //
  665. // Conversion between old and new data structures
  666. //
  667. INT_MM_AUTH_METHODS Methods;
  668. ZeroMemory(&Methods, sizeof(INT_MM_AUTH_METHODS));
  669. memcpy(&(Methods.gMMAuthID), &(ParserAuthMethod.gMMAuthID), sizeof(GUID));
  670. Methods.dwFlags = ParserAuthMethod.dwFlags;
  671. Methods.dwNumAuthInfos = ParserAuthMethod.dwNumAuthInfos;
  672. PINT_IPSEC_MM_AUTH_INFO pAuthInfos = new INT_IPSEC_MM_AUTH_INFO[Methods.dwNumAuthInfos];
  673. if (pAuthInfos == NULL)
  674. {
  675. dwReturn = ERROR_OUTOFMEMORY;
  676. BAIL_OUT;
  677. }
  678. for (size_t i = 0; i < Methods.dwNumAuthInfos; ++i)
  679. {
  680. memcpy(&pAuthInfos[i], ParserAuthMethod.pAuthMethodInfo[i].pAuthenticationInfo, sizeof(INT_IPSEC_MM_AUTH_INFO));
  681. }
  682. Methods.pAuthenticationInfo = pAuthInfos;
  683. ParserAuthMethod.dwFlags = 0;
  684. dwReturn = ConvertIntMMAuthToExt(&Methods, &pExtMMAuth);
  685. if (dwReturn != ERROR_SUCCESS)
  686. {
  687. BAIL_OUT;
  688. }
  689. dwReturn = AddMMAuthMethods(g_szDynamicMachine, dwVersion, 0, pExtMMAuth, lpVoid);
  690. if (dwReturn == ERROR_SUCCESS)
  691. {
  692. dwReturn = FreeExtMMAuthMethods(pExtMMAuth);
  693. }
  694. error:
  695. return dwReturn;
  696. }
  697. ///////////////////////////////////////////////////////////////////////////////////////////
  698. //
  699. // Function : ConnectDynamicMachine
  700. //
  701. // Date of Creation : 09-22-01
  702. //
  703. // Parameters : IN LPCWSTR pwszMachine
  704. //
  705. // Return : DWORD
  706. //
  707. // Description : This function is a call back for Connect.
  708. // Check for PA is running and reg connectivity.
  709. //
  710. // Revision History :
  711. //
  712. // Date Author Comments
  713. //
  714. //////////////////////////////////////////////////////////////////////////////////////////
  715. DWORD
  716. ConnectDynamicMachine(
  717. IN LPCWSTR pwszMachine
  718. )
  719. {
  720. DWORD dwReturn = ERROR_SUCCESS;
  721. HKEY hLocal = NULL;
  722. dwReturn = RegConnectRegistry(pwszMachine, HKEY_LOCAL_MACHINE, &hLocal );
  723. if(dwReturn != ERROR_SUCCESS)
  724. {
  725. BAIL_OUT;
  726. }
  727. if (g_hGlobalRegistryKey)
  728. {
  729. RegCloseKey(g_hGlobalRegistryKey);
  730. g_hGlobalRegistryKey = NULL;
  731. }
  732. //
  733. // Check if policy agent is running..
  734. //
  735. PAIsRunning(dwReturn, (LPTSTR)pwszMachine);
  736. g_hGlobalRegistryKey = hLocal;
  737. error:
  738. return dwReturn;
  739. }
  740. ///////////////////////////////////////////////////////////////////////////////////////////
  741. //
  742. // Function : SetDynamicMMFilterRule
  743. //
  744. // Date of Creation: 09-22-01
  745. //
  746. // Parameters : IN LPTSTR pszPolicyName,
  747. // IN MM_FILTER& ParserMMFilter,
  748. // IN INT_MM_AUTH_METHODS& MMAuthMethod
  749. //
  750. // Return : DWORD
  751. //
  752. // Description : This function sets MMFilter parameters.
  753. // Authentication methods and mmpolicy name only can be set
  754. //
  755. // Revision History:
  756. //
  757. // Date Author Comments
  758. //
  759. //////////////////////////////////////////////////////////////////////////////////////////
  760. DWORD
  761. SetDynamicMMFilterRule(
  762. IN LPTSTR pszPolicyName,
  763. IN MM_FILTER& ParserMMFilter,
  764. IN STA_AUTH_METHODS& MMAuthMethod
  765. )
  766. {
  767. DWORD dwReturn = ERROR_SUCCESS;
  768. DWORD dwVersion = 0;
  769. GUID gDefaultGUID = {0}; // NULL GUID value
  770. BOOL bPolExists = FALSE;
  771. PIPSEC_MM_POLICY pMMPol = NULL;
  772. HANDLE hFilter = NULL;
  773. LPVOID pvReserved = NULL;
  774. if(pszPolicyName)
  775. {
  776. //
  777. // Get the corresponding main mode policy to set the name parameter
  778. //
  779. dwReturn = GetMMPolicy(g_szDynamicMachine, dwVersion, pszPolicyName, &pMMPol, NULL);
  780. if (dwReturn != ERROR_SUCCESS)
  781. {
  782. BAIL_OUT;
  783. }
  784. else
  785. {
  786. bPolExists = TRUE;
  787. }
  788. }
  789. else
  790. {
  791. bPolExists = TRUE;
  792. }
  793. ParserMMFilter.IpVersion = IPSEC_PROTOCOL_V4;
  794. dwReturn = OpenMMFilterHandle(g_szDynamicMachine, dwVersion, &ParserMMFilter, NULL, &hFilter);
  795. if (dwReturn != ERROR_SUCCESS)
  796. {
  797. BAIL_OUT;
  798. }
  799. if(pszPolicyName)
  800. {
  801. ParserMMFilter.gPolicyID = pMMPol->gPolicyID;
  802. }
  803. //
  804. // set the new authentication methods.
  805. //
  806. if(MMAuthMethod.dwNumAuthInfos)
  807. {
  808. gDefaultGUID = ParserMMFilter.gMMAuthID;
  809. dwReturn = AddAuthMethods(MMAuthMethod);
  810. if (dwReturn != ERROR_SUCCESS)
  811. {
  812. BAIL_OUT;
  813. }
  814. ParserMMFilter.gMMAuthID = MMAuthMethod.gMMAuthID;
  815. }
  816. dwReturn = SetMMFilter(hFilter,dwVersion, &ParserMMFilter, pvReserved);
  817. if((dwReturn == ERROR_SUCCESS) && (MMAuthMethod.dwNumAuthInfos))
  818. {
  819. //
  820. // remove the orphan MMAuthMethods
  821. //
  822. dwReturn = DeleteMMAuthMethods(g_szDynamicMachine, dwVersion, gDefaultGUID, NULL);
  823. }
  824. error:
  825. if(!bPolExists)
  826. {
  827. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_SET_MMF_NO_MMPOLICY);
  828. dwReturn = ERROR_NO_DISPLAY;
  829. }
  830. //error path clean up
  831. if(hFilter)
  832. {
  833. CloseMMFilterHandle(hFilter);
  834. }
  835. if(pMMPol)
  836. {
  837. SPDApiBufferFree(pMMPol);
  838. pMMPol = NULL;
  839. }
  840. return dwReturn;
  841. }
  842. ///////////////////////////////////////////////////////////////////////////////////////////
  843. //
  844. // Function : SetTransportRule
  845. //
  846. // Date of Creation: 09-22-01
  847. //
  848. // Parameters : IN TRANSPORT_FILTER& TrpFltr,
  849. // IN LPTSTR pFilterActionName,
  850. // IN FILTER_ACTION Inbound,
  851. // IN FILTER_ACTION Outbound
  852. //
  853. // Return : DWORD
  854. //
  855. // Description : This function sets TransportFilter parameters.
  856. // Filteraction name, inbound and outbound filteraction can be set
  857. //
  858. // Revision History:
  859. //
  860. // Date Author Comments
  861. //
  862. //////////////////////////////////////////////////////////////////////////////////////////
  863. DWORD
  864. SetTransportRule(
  865. IN TRANSPORT_FILTER& TrpFltr,
  866. IN LPTSTR pFilterActionName,
  867. IN FILTER_ACTION Inbound,
  868. IN FILTER_ACTION Outbound
  869. )
  870. {
  871. PIPSEC_QM_POLICY pQMPol = NULL;
  872. HANDLE hTrpFilter = NULL;
  873. DWORD dwReturn = ERROR_SUCCESS;
  874. DWORD dwVersion = 0;
  875. BOOL bFAFound = FALSE;
  876. if(pFilterActionName)
  877. {
  878. //
  879. // Get the corresponding quick mode policy to set the name parameter
  880. //
  881. dwReturn = GetQMPolicy(g_szDynamicMachine,dwVersion, pFilterActionName, 0, &pQMPol, NULL);
  882. if (!((dwReturn == ERROR_SUCCESS) && pQMPol))
  883. {
  884. BAIL_OUT;
  885. }
  886. bFAFound = TRUE;
  887. }
  888. else
  889. {
  890. bFAFound = TRUE;
  891. }
  892. TrpFltr.IpVersion = IPSEC_PROTOCOL_V4;
  893. dwReturn = OpenTransportFilterHandle(g_szDynamicMachine,dwVersion, &TrpFltr, NULL, &hTrpFilter);
  894. if (dwReturn != ERROR_SUCCESS)
  895. {
  896. BAIL_OUT;
  897. }
  898. //
  899. // Set the new filteraction (quick mode policy name)
  900. //
  901. if(pFilterActionName)
  902. {
  903. TrpFltr.gPolicyID = pQMPol->gPolicyID;
  904. }
  905. //
  906. // Set inbound filter action
  907. //
  908. if(Inbound != FILTER_ACTION_MAX)
  909. {
  910. TrpFltr.InboundFilterAction = Inbound;
  911. }
  912. //
  913. // Set outbound filter action
  914. //
  915. if(Outbound != FILTER_ACTION_MAX)
  916. {
  917. TrpFltr.OutboundFilterAction = Outbound;
  918. }
  919. dwReturn = SetTransportFilter(hTrpFilter,dwVersion, &TrpFltr, NULL);
  920. if (dwReturn == ERROR_SUCCESS)
  921. {
  922. dwReturn = CloseTransportFilterHandle(hTrpFilter);
  923. }
  924. error:
  925. if(!bFAFound)
  926. {
  927. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_SET_QMF_NO_QMPOLICY);
  928. dwReturn = ERROR_NO_DISPLAY;
  929. }
  930. //error path clean up
  931. if(pQMPol)
  932. {
  933. SPDApiBufferFree(pQMPol);
  934. pQMPol = NULL;
  935. }
  936. return dwReturn;
  937. }
  938. ///////////////////////////////////////////////////////////////////////////////////////////
  939. //
  940. // Function : SetTunnelRule
  941. //
  942. // Date of Creation: 09-22-01
  943. //
  944. // Parameters : IN TUNNEL_FILTER& TunnelFltr,
  945. // IN LPTSTR pFilterActionName,
  946. // IN FILTER_ACTION Inbound,
  947. // IN FILTER_ACTION Outbound
  948. //
  949. // Return : DWORD
  950. //
  951. // Description : This function sets TunnelFilter parameters.
  952. // Filteraction name, inbound and outbound filteraction can be set
  953. //
  954. // Revision History:
  955. //
  956. // Date Author Comments
  957. //
  958. //////////////////////////////////////////////////////////////////////////////////////////
  959. DWORD
  960. SetTunnelRule(
  961. IN TUNNEL_FILTER& TunnelFltr,
  962. IN LPTSTR pFilterActionName,
  963. IN FILTER_ACTION Inbound,
  964. IN FILTER_ACTION Outbound
  965. )
  966. {
  967. PIPSEC_QM_POLICY pQMPol = NULL;
  968. HANDLE hTrpFilter = NULL;
  969. DWORD dwReturn = ERROR_SUCCESS;
  970. DWORD dwVersion = 0;
  971. BOOL bFAFound = FALSE;
  972. if(pFilterActionName)
  973. {
  974. //
  975. // Get the corresponding QM policy
  976. //
  977. dwReturn = GetQMPolicy(g_szDynamicMachine,dwVersion, pFilterActionName, 0, &pQMPol, NULL);
  978. if (!((dwReturn == ERROR_SUCCESS) && pQMPol))
  979. {
  980. BAIL_OUT;
  981. }
  982. bFAFound = TRUE;
  983. }
  984. else
  985. {
  986. bFAFound = TRUE;
  987. }
  988. TunnelFltr.IpVersion = IPSEC_PROTOCOL_V4;
  989. dwReturn = OpenTunnelFilterHandle(g_szDynamicMachine,dwVersion, &TunnelFltr, NULL, &hTrpFilter);
  990. if (dwReturn != ERROR_SUCCESS)
  991. {
  992. BAIL_OUT;
  993. }
  994. //
  995. // Set the new filteraction (quick mode policy name)
  996. //
  997. if(pFilterActionName)
  998. {
  999. TunnelFltr.gPolicyID = pQMPol->gPolicyID;
  1000. }
  1001. //
  1002. // Set inbound filter action
  1003. //
  1004. if(Inbound != FILTER_ACTION_MAX)
  1005. {
  1006. TunnelFltr.InboundFilterAction = Inbound;
  1007. }
  1008. //
  1009. // Set outbound filter action
  1010. //
  1011. if(Outbound != FILTER_ACTION_MAX)
  1012. {
  1013. TunnelFltr.OutboundFilterAction = Outbound;
  1014. }
  1015. dwReturn = SetTunnelFilter(hTrpFilter,dwVersion, &TunnelFltr, NULL);
  1016. if (dwReturn == ERROR_SUCCESS)
  1017. {
  1018. dwReturn = CloseTunnelFilterHandle(hTrpFilter);
  1019. }
  1020. error:
  1021. if(!bFAFound)
  1022. {
  1023. PrintErrorMessage(IPSEC_ERR,0,ERRCODE_SET_QMF_NO_QMPOLICY);
  1024. dwReturn = ERROR_NO_DISPLAY;
  1025. }
  1026. //error path clean up
  1027. if(pQMPol)
  1028. {
  1029. SPDApiBufferFree(pQMPol);
  1030. pQMPol = NULL;
  1031. }
  1032. return dwReturn;
  1033. }
  1034. ///////////////////////////////////////////////////////////////////////////////////////////
  1035. //
  1036. // Function : CreateName
  1037. //
  1038. // Date of Creation : 9-23-2001
  1039. //
  1040. // Parameters : IN LPTSTR * ppszName
  1041. //
  1042. // Return : DWORD
  1043. //
  1044. // Description : Creates a name for MMFilter, Transport and Tunnel Filter
  1045. //
  1046. // Revision History :
  1047. //
  1048. // Date Author Comments
  1049. //
  1050. //////////////////////////////////////////////////////////////////////////////////////////
  1051. DWORD
  1052. CreateName(IN LPTSTR* ppszName)
  1053. {
  1054. RPC_STATUS RpcStat = RPC_S_OK;
  1055. _TCHAR StringTxt[MAX_STR_LEN] = {0};
  1056. GUID gID = {0};
  1057. DWORD dwReturn = ERROR_SUCCESS;
  1058. DWORD dwNameLen = 0;
  1059. LPTSTR pName = NULL;
  1060. //
  1061. // The name is combination of keyword 'IPSEC' and the generated GUID.
  1062. //
  1063. RpcStat = UuidCreate(&gID);
  1064. if(!(RpcStat == RPC_S_OK || RpcStat == RPC_S_UUID_LOCAL_ONLY))
  1065. {
  1066. dwReturn = ERROR_INVALID_PARAMETER;
  1067. BAIL_OUT;
  1068. }
  1069. _tcsncpy(StringTxt, NAME_PREFIX, _tcslen(NAME_PREFIX)+1);
  1070. dwReturn = StringFromGUID2(gID, StringTxt + _tcslen(StringTxt), (MAX_STR_LEN - _tcslen(StringTxt)));
  1071. if(dwReturn != 0)
  1072. {
  1073. dwReturn = ERROR_SUCCESS;
  1074. }
  1075. else
  1076. {
  1077. dwReturn = GetLastError();
  1078. BAIL_OUT;
  1079. }
  1080. dwNameLen = _tcslen(StringTxt)+1;
  1081. pName = new _TCHAR[dwNameLen];
  1082. if(pName == NULL)
  1083. {
  1084. dwReturn = ERROR_OUTOFMEMORY;
  1085. BAIL_OUT;
  1086. }
  1087. else
  1088. {
  1089. _tcsncpy(pName, StringTxt,dwNameLen);
  1090. }
  1091. error:
  1092. if(dwReturn == ERROR_SUCCESS)
  1093. {
  1094. if(ppszName != NULL)
  1095. {
  1096. *ppszName = pName;
  1097. }
  1098. else
  1099. {
  1100. dwReturn = ERROR_OUTOFMEMORY;
  1101. BAIL_OUT;
  1102. }
  1103. }
  1104. return dwReturn;
  1105. }
  1106. ///////////////////////////////////////////////////////////////////////////////////////////
  1107. //
  1108. // Function : FindAndGetMMFilterRule
  1109. //
  1110. // Date of Creation: 09-22-01
  1111. //
  1112. // Parameters : IN ADDR SrcAddr,
  1113. // IN ADDR DstAddr,
  1114. // IN BOOL bMirror,
  1115. // IN IF_TYPE ConType,
  1116. // IN BOOL bSrcMask,
  1117. // IN BOOL bDstMask,
  1118. // OUT PMM_FILTER *pMMFilterRule
  1119. // IN OUT DWORD& dwStatus
  1120. //
  1121. // Return : BOOL
  1122. //
  1123. // Description : This function enumerates mmfilter and gets back filled filter structure.
  1124. //
  1125. // Revision History:
  1126. //
  1127. // Date Author Comments
  1128. //
  1129. //////////////////////////////////////////////////////////////////////////////////////////
  1130. BOOL
  1131. FindAndGetMMFilterRule(
  1132. IN ADDR SrcAddr,
  1133. IN ADDR DstAddr,
  1134. IN BOOL bMirror,
  1135. IN IF_TYPE ConType,
  1136. IN BOOL bSrcMask,
  1137. IN BOOL bDstMask,
  1138. IN OUT PMM_FILTER *pMMFilterRule,
  1139. IN OUT DWORD& dwStatus
  1140. )
  1141. {
  1142. PMM_FILTER pMMFilterRule_local = NULL;
  1143. DWORD dwResumeHandle = 0; // handle for continuation calls
  1144. DWORD dwCount = 0; // counting objects here
  1145. DWORD dwNameLen = 0;
  1146. GUID gDefaultGUID = {0}; // NULL GUID value
  1147. DWORD i=0, j=0;
  1148. DWORD dwVersion = 0;
  1149. BOOL bFoundFilter = FALSE;
  1150. PMM_FILTER pMMFilter = NULL;
  1151. for (i = 0; ;i+=dwCount)
  1152. {
  1153. dwStatus = EnumMMFilters(g_szDynamicMachine, dwVersion, NULL, ENUM_GENERIC_FILTERS,
  1154. gDefaultGUID, 0, &pMMFilter, &dwCount, &dwResumeHandle, NULL);
  1155. if ( (dwStatus == ERROR_NO_DATA) || (dwCount == 0) || (dwStatus != ERROR_SUCCESS))
  1156. {
  1157. dwStatus = ERROR_SUCCESS;
  1158. BAIL_OUT;
  1159. }
  1160. else if(!(pMMFilter && dwCount > 0))
  1161. {
  1162. BAIL_OUT; // not required to continue.
  1163. }
  1164. for (j = 0; j < dwCount; j++)
  1165. {
  1166. //
  1167. // Match the user given input with the enumerated structure to get the exact match.
  1168. //
  1169. if((pMMFilter[j].SrcAddr.uIpAddr == SrcAddr.uIpAddr) &&
  1170. (pMMFilter[j].SrcAddr.AddrType == SrcAddr.AddrType) &&
  1171. (pMMFilter[j].DesAddr.uIpAddr == DstAddr.uIpAddr) &&
  1172. (pMMFilter[j].DesAddr.AddrType == DstAddr.AddrType) &&
  1173. (pMMFilter[j].bCreateMirror == bMirror) &&
  1174. (pMMFilter[j].InterfaceType == ConType))
  1175. {
  1176. // If mask is an user input then validate for mask
  1177. ///////////////////////////////////////////////////
  1178. // If both source and destination mask are not given
  1179. if((!bDstMask) && (!bSrcMask))
  1180. {
  1181. pMMFilterRule_local = new MM_FILTER;
  1182. if(pMMFilterRule_local == NULL)
  1183. {
  1184. dwStatus = ERROR_OUTOFMEMORY;
  1185. BAIL_OUT;
  1186. }
  1187. memcpy(pMMFilterRule_local, &pMMFilter[j], sizeof(MM_FILTER));
  1188. dwNameLen = _tcslen(pMMFilter[j].pszFilterName) + 1;
  1189. pMMFilterRule_local->pszFilterName = NULL;
  1190. pMMFilterRule_local->pszFilterName = new _TCHAR[dwNameLen];
  1191. if((pMMFilterRule_local->pszFilterName) == NULL)
  1192. {
  1193. dwStatus = ERROR_OUTOFMEMORY;
  1194. BAIL_OUT;
  1195. }
  1196. _tcsncpy(pMMFilterRule_local->pszFilterName , pMMFilter[j].pszFilterName, dwNameLen);
  1197. bFoundFilter = TRUE;
  1198. break;
  1199. }
  1200. //
  1201. // If source mask is given
  1202. //
  1203. else if((!bDstMask) && (bSrcMask))
  1204. {
  1205. if(pMMFilter[j].SrcAddr.uSubNetMask == SrcAddr.uSubNetMask)
  1206. {
  1207. pMMFilterRule_local = new MM_FILTER;
  1208. if(pMMFilterRule_local == NULL)
  1209. {
  1210. dwStatus = ERROR_OUTOFMEMORY;
  1211. BAIL_OUT;
  1212. }
  1213. memcpy(pMMFilterRule_local, &pMMFilter[j],sizeof(MM_FILTER));
  1214. dwNameLen = _tcslen(pMMFilter[j].pszFilterName) + 1;
  1215. pMMFilterRule_local->pszFilterName = NULL;
  1216. pMMFilterRule_local->pszFilterName = new _TCHAR[dwNameLen];
  1217. if((pMMFilterRule_local->pszFilterName) == NULL)
  1218. {
  1219. dwStatus = ERROR_OUTOFMEMORY;
  1220. BAIL_OUT;
  1221. }
  1222. _tcsncpy(pMMFilterRule_local->pszFilterName , pMMFilter[j].pszFilterName, dwNameLen);
  1223. bFoundFilter = TRUE;
  1224. break;
  1225. }
  1226. }
  1227. //
  1228. // If destination mask is given
  1229. //
  1230. else if((bDstMask) && (!bSrcMask))
  1231. {
  1232. if(pMMFilter[j].DesAddr.uSubNetMask == DstAddr.uSubNetMask)
  1233. {
  1234. pMMFilterRule_local = new MM_FILTER;
  1235. if(pMMFilterRule_local == NULL)
  1236. {
  1237. dwStatus = ERROR_OUTOFMEMORY;
  1238. BAIL_OUT;
  1239. }
  1240. memcpy(pMMFilterRule_local, &pMMFilter[j],sizeof(MM_FILTER));
  1241. dwNameLen = _tcslen(pMMFilter[j].pszFilterName) + 1;
  1242. pMMFilterRule_local->pszFilterName = NULL;
  1243. pMMFilterRule_local->pszFilterName = new _TCHAR[dwNameLen];
  1244. if((pMMFilterRule_local->pszFilterName) == NULL)
  1245. {
  1246. dwStatus = ERROR_OUTOFMEMORY;
  1247. BAIL_OUT;
  1248. }
  1249. _tcsncpy(pMMFilterRule_local->pszFilterName , pMMFilter[j].pszFilterName, dwNameLen);
  1250. bFoundFilter = TRUE;
  1251. break;
  1252. }
  1253. }
  1254. //
  1255. // If source mask and destination mask are given
  1256. //
  1257. else if((bDstMask) && (bSrcMask))
  1258. {
  1259. if(pMMFilter[j].DesAddr.uSubNetMask == DstAddr.uSubNetMask)
  1260. {
  1261. if(pMMFilter[j].SrcAddr.uSubNetMask == SrcAddr.uSubNetMask)
  1262. {
  1263. pMMFilterRule_local = new MM_FILTER;
  1264. if(pMMFilterRule_local == NULL)
  1265. {
  1266. dwStatus = ERROR_OUTOFMEMORY;
  1267. BAIL_OUT;
  1268. }
  1269. memcpy(pMMFilterRule_local, &pMMFilter[j],sizeof(MM_FILTER));
  1270. dwNameLen = _tcslen(pMMFilter[j].pszFilterName) + 1;
  1271. pMMFilterRule_local->pszFilterName = NULL;
  1272. pMMFilterRule_local->pszFilterName = new _TCHAR[dwNameLen];
  1273. if((pMMFilterRule_local->pszFilterName) == NULL)
  1274. {
  1275. dwStatus = ERROR_OUTOFMEMORY;
  1276. BAIL_OUT;
  1277. }
  1278. _tcsncpy(pMMFilterRule_local->pszFilterName , pMMFilter[j].pszFilterName, dwNameLen);
  1279. bFoundFilter = TRUE;
  1280. break;
  1281. }
  1282. }
  1283. }
  1284. }
  1285. }
  1286. SPDApiBufferFree(pMMFilter);
  1287. pMMFilter = NULL;
  1288. if(bFoundFilter)
  1289. {
  1290. *pMMFilterRule = pMMFilterRule_local;
  1291. break;
  1292. }
  1293. }
  1294. error:
  1295. //error path clean up
  1296. if(pMMFilter)
  1297. {
  1298. SPDApiBufferFree(pMMFilter);
  1299. pMMFilter = NULL;
  1300. }
  1301. return bFoundFilter;
  1302. }
  1303. ///////////////////////////////////////////////////////////////////////////////////////////
  1304. //
  1305. // Function : FindAndGetTransportRule
  1306. //
  1307. // Date of Creation: 09-22-01
  1308. //
  1309. // Parameters : IN ADDR SrcAddr,
  1310. // IN ADDR DstAddr,
  1311. // IN BOOL bMirror,
  1312. // IN IF_TYPE ConType,
  1313. // IN DWORD dwProtocol,
  1314. // IN DWORD dwSrcPort,
  1315. // IN DWORD dwDstPort,
  1316. // IN BOOL bSrcMask,
  1317. // IN BOOL bDstMask,
  1318. // OUT PTRANSPORT_FILTER *pQMFilterRule
  1319. // IN OUT DWORD& dwStatus
  1320. //
  1321. //Return : BOOL
  1322. //
  1323. //Description : This function enumerates transport filter and gets filled transport filter.
  1324. //
  1325. //Revision History :
  1326. //
  1327. // Date Author Comments
  1328. //
  1329. //////////////////////////////////////////////////////////////////////////////////////////
  1330. BOOL
  1331. FindAndGetTransportRule(
  1332. IN ADDR SrcAddr,
  1333. IN ADDR DstAddr,
  1334. IN BOOL bMirror,
  1335. IN IF_TYPE ConType,
  1336. IN DWORD dwProtocol,
  1337. IN DWORD dwSrcPort,
  1338. IN DWORD dwDstPort,
  1339. IN BOOL bSrcMask,
  1340. IN BOOL bDstMask,
  1341. OUT PTRANSPORT_FILTER *pQMFilterRule,
  1342. IN OUT DWORD& dwStatus
  1343. )
  1344. {
  1345. DWORD dwResumeHandle = 0; // handle for continuation calls
  1346. DWORD dwCount = 0; // counting objects here
  1347. DWORD dwVersion = 0;
  1348. DWORD dwNameLen = 0;
  1349. GUID gDefaultGUID = {0}; // NULL GUID value
  1350. DWORD i=0, j=0;
  1351. BOOL bFoundFilter = FALSE;
  1352. PTRANSPORT_FILTER pQMFilterRule_local = NULL;
  1353. PTRANSPORT_FILTER pTransF = NULL;
  1354. for (i = 0; ;i+=dwCount)
  1355. {
  1356. dwStatus = EnumTransportFilters(g_szDynamicMachine, dwVersion, NULL, ENUM_GENERIC_FILTERS,
  1357. gDefaultGUID, 0, &pTransF, &dwCount, &dwResumeHandle, NULL);
  1358. if ( (dwStatus == ERROR_NO_DATA) || (dwCount == 0) || (dwStatus != ERROR_SUCCESS))
  1359. {
  1360. dwStatus = ERROR_SUCCESS;
  1361. BAIL_OUT;
  1362. }
  1363. else if(!(pTransF && dwCount > 0))
  1364. {
  1365. BAIL_OUT; // not required to continue.
  1366. }
  1367. for (j = 0; j < dwCount; j++)
  1368. {
  1369. // Match the user given input with the enumerated structure to get the exact match
  1370. if((pTransF[j].SrcAddr.uIpAddr == SrcAddr.uIpAddr) &&
  1371. (pTransF[j].SrcAddr.AddrType == SrcAddr.AddrType) &&
  1372. (pTransF[j].DesAddr.uIpAddr == DstAddr.uIpAddr) &&
  1373. (pTransF[j].DesAddr.AddrType == DstAddr.AddrType) &&
  1374. (pTransF[j].bCreateMirror == bMirror) &&
  1375. (pTransF[j].InterfaceType == ConType) &&
  1376. (pTransF[j].Protocol.dwProtocol== dwProtocol) &&
  1377. (pTransF[j].SrcPort.wPort == dwSrcPort) &&
  1378. (pTransF[j].DesPort.wPort == dwDstPort))
  1379. {
  1380. // if both source and destination mask are not given
  1381. if((!bDstMask) && (!bSrcMask))
  1382. {
  1383. pQMFilterRule_local = new TRANSPORT_FILTER;
  1384. if(pQMFilterRule_local == NULL)
  1385. {
  1386. dwStatus = ERROR_OUTOFMEMORY;
  1387. BAIL_OUT;
  1388. }
  1389. memcpy(pQMFilterRule_local, &pTransF[j],sizeof(TRANSPORT_FILTER));
  1390. dwNameLen = _tcslen(pTransF[j].pszFilterName) + 1;
  1391. pQMFilterRule_local->pszFilterName = NULL;
  1392. pQMFilterRule_local->pszFilterName = new _TCHAR[dwNameLen];
  1393. if((pQMFilterRule_local->pszFilterName) == NULL)
  1394. {
  1395. dwStatus = ERROR_OUTOFMEMORY;
  1396. BAIL_OUT;
  1397. }
  1398. _tcsncpy(pQMFilterRule_local->pszFilterName, pTransF[j].pszFilterName, dwNameLen);
  1399. bFoundFilter = TRUE;
  1400. break;
  1401. }
  1402. //
  1403. // If source mask is given
  1404. //
  1405. else if((!bDstMask) && (bSrcMask))
  1406. {
  1407. if(pTransF[j].SrcAddr.uSubNetMask == SrcAddr.uSubNetMask)
  1408. {
  1409. pQMFilterRule_local = new TRANSPORT_FILTER;
  1410. if(pQMFilterRule_local == NULL)
  1411. {
  1412. dwStatus = ERROR_OUTOFMEMORY;
  1413. BAIL_OUT;
  1414. }
  1415. memcpy(pQMFilterRule_local, &pTransF[j],sizeof(TRANSPORT_FILTER));
  1416. dwNameLen = _tcslen(pTransF[j].pszFilterName) + 1;
  1417. pQMFilterRule_local->pszFilterName = NULL;
  1418. pQMFilterRule_local->pszFilterName = new _TCHAR[dwNameLen];
  1419. if(pQMFilterRule_local->pszFilterName == NULL)
  1420. {
  1421. dwStatus = ERROR_OUTOFMEMORY;
  1422. BAIL_OUT;
  1423. }
  1424. _tcsncpy(pQMFilterRule_local->pszFilterName, pTransF[j].pszFilterName, dwNameLen);
  1425. bFoundFilter = TRUE;
  1426. break;
  1427. }
  1428. }
  1429. //
  1430. // If destination mask is given
  1431. //
  1432. else if((bDstMask) && (!bSrcMask))
  1433. {
  1434. if(pTransF[j].DesAddr.uSubNetMask == DstAddr.uSubNetMask)
  1435. {
  1436. pQMFilterRule_local = new TRANSPORT_FILTER;
  1437. if(pQMFilterRule_local == NULL)
  1438. {
  1439. dwStatus = ERROR_OUTOFMEMORY;
  1440. BAIL_OUT;
  1441. }
  1442. memcpy(pQMFilterRule_local, &pTransF[j],sizeof(TRANSPORT_FILTER));
  1443. dwNameLen = _tcslen(pTransF[j].pszFilterName) + 1;
  1444. pQMFilterRule_local->pszFilterName = NULL;
  1445. pQMFilterRule_local->pszFilterName = new _TCHAR[dwNameLen];
  1446. if((pQMFilterRule_local->pszFilterName) == NULL)
  1447. {
  1448. dwStatus = ERROR_OUTOFMEMORY;
  1449. BAIL_OUT;
  1450. }
  1451. _tcsncpy(pQMFilterRule_local->pszFilterName, pTransF[j].pszFilterName, dwNameLen);
  1452. bFoundFilter = TRUE;
  1453. break;
  1454. }
  1455. }
  1456. //
  1457. // If source mask and destination mask are given
  1458. //
  1459. else if((bDstMask) && (bSrcMask))
  1460. {
  1461. if(pTransF[j].DesAddr.uSubNetMask == DstAddr.uSubNetMask)
  1462. {
  1463. if(pTransF[j].SrcAddr.uSubNetMask == SrcAddr.uSubNetMask)
  1464. {
  1465. pQMFilterRule_local = new TRANSPORT_FILTER;
  1466. if(pQMFilterRule_local == NULL)
  1467. {
  1468. dwStatus = ERROR_OUTOFMEMORY;
  1469. BAIL_OUT;
  1470. }
  1471. memcpy(pQMFilterRule_local, &pTransF[j],sizeof(TRANSPORT_FILTER));
  1472. dwNameLen = _tcslen(pTransF[j].pszFilterName) + 1;
  1473. pQMFilterRule_local->pszFilterName = NULL;
  1474. pQMFilterRule_local->pszFilterName = new _TCHAR[dwNameLen];
  1475. if((pQMFilterRule_local->pszFilterName) == NULL)
  1476. {
  1477. dwStatus = ERROR_OUTOFMEMORY;
  1478. BAIL_OUT;
  1479. }
  1480. _tcsncpy(pQMFilterRule_local->pszFilterName, pTransF[j].pszFilterName, dwNameLen);
  1481. bFoundFilter = TRUE;
  1482. break;
  1483. }
  1484. }
  1485. }
  1486. }
  1487. }
  1488. SPDApiBufferFree(pTransF);
  1489. pTransF = NULL;
  1490. //
  1491. // copy the structure pointer.
  1492. //
  1493. if(bFoundFilter)
  1494. {
  1495. *pQMFilterRule = pQMFilterRule_local;
  1496. break;
  1497. }
  1498. }
  1499. error:
  1500. //error path cleanup
  1501. if(pTransF)
  1502. {
  1503. SPDApiBufferFree(pTransF);
  1504. pTransF = NULL;
  1505. }
  1506. return bFoundFilter;
  1507. }
  1508. ///////////////////////////////////////////////////////////////////////////////////////////
  1509. //
  1510. // Function : FindAndGetTunnelRule
  1511. //
  1512. // Date of Creation: 09-22-01
  1513. //
  1514. // Parameters : IN ADDR SrcAddr,
  1515. // IN ADDR DstAddr,
  1516. // IN BOOL bMirror,
  1517. // IN IF_TYPE ConType,
  1518. // IN DWORD dwProtocol,
  1519. // IN DWORD dwSrcPort,
  1520. // IN DWORD dwDstPort,
  1521. // IN BOOL bSrcMask,
  1522. // IN BOOL bDstMask,
  1523. // IN ADDR SrcTunnel,
  1524. // IN ADDR DstTunnel,
  1525. // OUT PTUNNEL_FILTER * pQMFilterRule,
  1526. // IN OUT DWORD& dwStatus
  1527. //
  1528. // Return : BOOL
  1529. //
  1530. // Description : This function enumerates tunnel filter and gets filled tunnel filter.
  1531. //
  1532. // Revision History:
  1533. //
  1534. // Date Author Comments
  1535. //
  1536. //////////////////////////////////////////////////////////////////////////////////////////
  1537. BOOL
  1538. FindAndGetTunnelRule(
  1539. IN ADDR SrcAddr,
  1540. IN ADDR DstAddr,
  1541. IN BOOL bMirror,
  1542. IN IF_TYPE ConType,
  1543. IN DWORD dwProtocol,
  1544. IN DWORD dwSrcPort,
  1545. IN DWORD dwDstPort,
  1546. IN BOOL bSrcMask,
  1547. IN BOOL bDstMask,
  1548. IN ADDR SrcTunnel,
  1549. IN ADDR DstTunnel,
  1550. OUT PTUNNEL_FILTER * pQMFilterRule,
  1551. OUT DWORD& dwStatus
  1552. )
  1553. {
  1554. DWORD dwResumeHandle = 0; // handle for continuation calls
  1555. DWORD dwCount = 0; // counting objects here
  1556. DWORD dwVersion = 0;
  1557. DWORD dwNameLen = 0;
  1558. GUID gDefaultGUID = {0}; // NULL GUID value
  1559. DWORD i=0, j=0;
  1560. BOOL bFoundFilter = FALSE;
  1561. PTUNNEL_FILTER pQMFilterRule_local = NULL;
  1562. PTUNNEL_FILTER pTransF = NULL;
  1563. for (i = 0; ;i+=dwCount)
  1564. {
  1565. dwStatus = EnumTunnelFilters(g_szDynamicMachine, dwVersion, NULL, ENUM_GENERIC_FILTERS,
  1566. gDefaultGUID, 0, &pTransF, &dwCount, &dwResumeHandle, NULL);
  1567. if ( (dwStatus == ERROR_NO_DATA) || (dwCount == 0) || (dwStatus != ERROR_SUCCESS))
  1568. {
  1569. dwStatus = ERROR_SUCCESS;
  1570. break;
  1571. }
  1572. else if(!(pTransF && dwCount > 0))
  1573. {
  1574. break; // not required to continue.
  1575. }
  1576. for (j = 0; j < dwCount; j++)
  1577. {
  1578. //
  1579. // Match the user given input with the enumerated structure to get the exact match
  1580. //
  1581. if((pTransF[j].SrcAddr.uIpAddr == SrcAddr.uIpAddr) &&
  1582. (pTransF[j].SrcAddr.AddrType == SrcAddr.AddrType) &&
  1583. (pTransF[j].DesAddr.uIpAddr == DstAddr.uIpAddr) &&
  1584. (pTransF[j].DesAddr.AddrType == DstAddr.AddrType) &&
  1585. (pTransF[j].bCreateMirror == bMirror) &&
  1586. (pTransF[j].InterfaceType == ConType) &&
  1587. (pTransF[j].Protocol.dwProtocol== dwProtocol) &&
  1588. (pTransF[j].SrcPort.wPort == dwSrcPort) &&
  1589. (pTransF[j].DesPort.wPort == dwDstPort) &&
  1590. (pTransF[j].DesTunnelAddr.uIpAddr == DstTunnel.uIpAddr) &&
  1591. (pTransF[j].DesTunnelAddr.AddrType == DstTunnel.AddrType))
  1592. {
  1593. // If both source and destination mask are not given
  1594. if((!bDstMask) && (!bSrcMask))
  1595. {
  1596. pQMFilterRule_local = new TUNNEL_FILTER;
  1597. if(pQMFilterRule_local == NULL)
  1598. {
  1599. dwStatus = ERROR_OUTOFMEMORY;
  1600. BAIL_OUT;
  1601. }
  1602. memcpy(pQMFilterRule_local, &pTransF[j],sizeof(TUNNEL_FILTER));
  1603. dwNameLen = _tcslen(pTransF[j].pszFilterName) + 1;
  1604. pQMFilterRule_local->pszFilterName = NULL;
  1605. pQMFilterRule_local->pszFilterName = new _TCHAR[dwNameLen];
  1606. if((pQMFilterRule_local->pszFilterName) == NULL)
  1607. {
  1608. dwStatus = ERROR_OUTOFMEMORY;
  1609. BAIL_OUT;
  1610. }
  1611. _tcsncpy(pQMFilterRule_local->pszFilterName, pTransF[j].pszFilterName, dwNameLen);
  1612. bFoundFilter = TRUE;
  1613. break;
  1614. }
  1615. //
  1616. // If source mask is given
  1617. //
  1618. else if((!bDstMask) && (bSrcMask))
  1619. {
  1620. if(pTransF[j].SrcAddr.uSubNetMask == SrcAddr.uSubNetMask)
  1621. {
  1622. pQMFilterRule_local = new TUNNEL_FILTER;
  1623. if(pQMFilterRule_local == NULL)
  1624. {
  1625. dwStatus = ERROR_OUTOFMEMORY;
  1626. BAIL_OUT;
  1627. }
  1628. memcpy(pQMFilterRule_local, &pTransF[j],sizeof(TUNNEL_FILTER));
  1629. dwNameLen = _tcslen(pTransF[j].pszFilterName) + 1;
  1630. pQMFilterRule_local->pszFilterName = NULL;
  1631. pQMFilterRule_local->pszFilterName = new _TCHAR[dwNameLen];
  1632. if((pQMFilterRule_local->pszFilterName) == NULL)
  1633. {
  1634. dwStatus = ERROR_OUTOFMEMORY;
  1635. BAIL_OUT;
  1636. }
  1637. _tcsncpy(pQMFilterRule_local->pszFilterName, pTransF[j].pszFilterName, dwNameLen);
  1638. bFoundFilter = TRUE;
  1639. break;
  1640. }
  1641. }
  1642. //
  1643. // If destination mask is given
  1644. //
  1645. else if((bDstMask) && (!bSrcMask))
  1646. {
  1647. if(pTransF[j].DesAddr.uSubNetMask == DstAddr.uSubNetMask)
  1648. {
  1649. pQMFilterRule_local = new TUNNEL_FILTER;
  1650. if(pQMFilterRule_local == NULL)
  1651. {
  1652. dwStatus = ERROR_OUTOFMEMORY;
  1653. BAIL_OUT;
  1654. }
  1655. memcpy(pQMFilterRule_local, &pTransF[j],sizeof(TUNNEL_FILTER));
  1656. dwNameLen = _tcslen(pTransF[j].pszFilterName) + 1;
  1657. pQMFilterRule_local->pszFilterName = NULL;
  1658. pQMFilterRule_local->pszFilterName = new _TCHAR[dwNameLen];
  1659. if((pQMFilterRule_local->pszFilterName) == NULL)
  1660. {
  1661. dwStatus = ERROR_OUTOFMEMORY;
  1662. BAIL_OUT;
  1663. }
  1664. _tcsncpy(pQMFilterRule_local->pszFilterName, pTransF[j].pszFilterName, dwNameLen);
  1665. bFoundFilter = TRUE;
  1666. break;
  1667. }
  1668. }
  1669. //
  1670. // If source mask and destination mask are given
  1671. //
  1672. else if((bDstMask) && (bSrcMask))
  1673. {
  1674. if(pTransF[j].DesAddr.uSubNetMask == DstAddr.uSubNetMask)
  1675. {
  1676. if(pTransF[j].SrcAddr.uSubNetMask == SrcAddr.uSubNetMask)
  1677. {
  1678. pQMFilterRule_local = new TUNNEL_FILTER;
  1679. if(pQMFilterRule_local == NULL)
  1680. {
  1681. dwStatus = ERROR_OUTOFMEMORY;
  1682. BAIL_OUT;
  1683. }
  1684. memcpy(pQMFilterRule_local, &pTransF[j],sizeof(TUNNEL_FILTER));
  1685. dwNameLen = _tcslen(pTransF[j].pszFilterName) + 1;
  1686. pQMFilterRule_local->pszFilterName = NULL;
  1687. pQMFilterRule_local->pszFilterName = new _TCHAR[dwNameLen];
  1688. if((pQMFilterRule_local->pszFilterName) == NULL)
  1689. {
  1690. dwStatus = ERROR_OUTOFMEMORY;
  1691. BAIL_OUT;
  1692. }
  1693. _tcsncpy(pQMFilterRule_local->pszFilterName, pTransF[j].pszFilterName, dwNameLen);
  1694. bFoundFilter = TRUE;
  1695. break;
  1696. }
  1697. }
  1698. }
  1699. }
  1700. }
  1701. SPDApiBufferFree(pTransF);
  1702. pTransF = NULL;
  1703. //
  1704. //copy the pointer structure
  1705. //
  1706. if(bFoundFilter)
  1707. {
  1708. *pQMFilterRule = pQMFilterRule_local;
  1709. break;
  1710. }
  1711. }
  1712. error:
  1713. //error path clean up
  1714. if(pTransF)
  1715. {
  1716. SPDApiBufferFree(pTransF);
  1717. }
  1718. return bFoundFilter;
  1719. }
  1720. ///////////////////////////////////////////////////////////////////////////////////////////
  1721. //
  1722. // Function : LoadMMFilterDefaults
  1723. //
  1724. // Date of Creation: 09-22-01
  1725. //
  1726. // Parameters : OUT MM_FILTER& MMFilter
  1727. //
  1728. // Return : DWORD
  1729. //
  1730. // Description : This function loads the Main mode filter structure defaults.
  1731. //
  1732. // Revision History:
  1733. //
  1734. // Date Author Comments
  1735. //
  1736. //////////////////////////////////////////////////////////////////////////////////////////
  1737. DWORD
  1738. LoadMMFilterDefaults(
  1739. OUT MM_FILTER& MMFilter
  1740. )
  1741. {
  1742. DWORD dwReturn = ERROR_SUCCESS;
  1743. GUID gInterfaceId = {0};
  1744. RPC_STATUS RpcStat = RPC_S_OK;
  1745. memset(&MMFilter, 0, sizeof(MM_FILTER));
  1746. MMFilter.InterfaceType = INTERFACE_TYPE_ALL;
  1747. MMFilter.bCreateMirror = TRUE;
  1748. MMFilter.dwFlags = 0;
  1749. MMFilter.dwDirection = FILTER_DIRECTION_OUTBOUND;
  1750. MMFilter.dwWeight = 0;
  1751. MMFilter.SrcAddr.AddrType = IP_ADDR_UNIQUE;
  1752. MMFilter.SrcAddr.uSubNetMask = IP_ADDRESS_MASK_NONE;
  1753. MMFilter.DesAddr.AddrType = IP_ADDR_UNIQUE;
  1754. MMFilter.DesAddr.uSubNetMask = IP_ADDRESS_MASK_NONE;
  1755. RpcStat = UuidCreateNil(&(gInterfaceId));
  1756. if(!(RpcStat == RPC_S_OK || RpcStat == RPC_S_UUID_LOCAL_ONLY))
  1757. {
  1758. dwReturn = GetLastError();
  1759. PrintErrorMessage(WIN32_ERR, dwReturn, NULL);
  1760. dwReturn = ERROR_NO_DISPLAY;
  1761. }
  1762. MMFilter.DesAddr.pgInterfaceID = NULL;
  1763. MMFilter.SrcAddr.pgInterfaceID = NULL;
  1764. return dwReturn;
  1765. }
  1766. ///////////////////////////////////////////////////////////////////////////////////////////
  1767. //
  1768. // Function : IsLastRuleOfMMFilter
  1769. //
  1770. // Date of Creation: 05-19-02
  1771. //
  1772. // Parameters : IN ADDR SrcAddr,
  1773. // IN ADDR DstAddr,
  1774. // IN BOOL bMirror,
  1775. // IN IF_TYPE ConType,
  1776. // IN BOOL bSrcMask,
  1777. // IN BOOL bDstMask,
  1778. // IN OUT DWORD& dwStatus
  1779. //
  1780. //Return : BOOL
  1781. //
  1782. //Description : Deterimines if there exists any transport or tunnel filters
  1783. // may require a MM filter. This is called before deleting an MM filter
  1784. // so that we make sure we don't delete any MM filter if more than
  1785. // on tranport or tunnel filters may be using it.
  1786. //
  1787. //Revision History :
  1788. //
  1789. // Date Author Comments
  1790. //
  1791. //////////////////////////////////////////////////////////////////////////////////////////
  1792. BOOL
  1793. IsLastRuleOfMMFilter(
  1794. IN ADDR SrcAddr,
  1795. IN ADDR DstAddr,
  1796. IN BOOL bMirror,
  1797. IN IF_TYPE ConType,
  1798. IN BOOL bSrcMask,
  1799. IN BOOL bDstMask,
  1800. IN OUT DWORD& dwStatus
  1801. )
  1802. {
  1803. DWORD dwTransFFloorCount = 0;
  1804. DWORD dwTunnFFloorCount = 0;
  1805. BOOL bLastRuleOfMMFilter = FALSE;
  1806. dwTransFFloorCount = FloorCountTransportRuleOfMMFilter(
  1807. SrcAddr,
  1808. DstAddr,
  1809. bMirror,
  1810. ConType,
  1811. bSrcMask,
  1812. bDstMask,
  1813. dwStatus
  1814. );
  1815. BAIL_ON_WIN32_ERROR(dwStatus);
  1816. dwTunnFFloorCount = FloorCountTunnelRuleOfMMFilter(
  1817. SrcAddr,
  1818. DstAddr,
  1819. bMirror,
  1820. ConType,
  1821. dwStatus
  1822. );
  1823. BAIL_ON_WIN32_ERROR(dwStatus);
  1824. error:
  1825. bLastRuleOfMMFilter = (dwTransFFloorCount + dwTunnFFloorCount == 0);
  1826. return bLastRuleOfMMFilter;
  1827. }
  1828. ///////////////////////////////////////////////////////////////////////////////////////////
  1829. //
  1830. // Function : FloorCountTransportRuleOfMMFilter
  1831. //
  1832. // Date of Creation: 05-19-02
  1833. //
  1834. // Parameters : IN ADDR SrcAddr,
  1835. // IN ADDR DstAddr,
  1836. // IN BOOL bMirror,
  1837. // IN IF_TYPE ConType,
  1838. // IN BOOL bSrcMask,
  1839. // IN BOOL bDstMask,
  1840. // IN OUT DWORD& dwStatus
  1841. //
  1842. //Return : DWORD
  1843. //
  1844. //Description : Counts if there is at least one transport filter that matches the
  1845. // given keys. We are not interested in getting the exact count,
  1846. // just whether we have more than one.
  1847. //
  1848. //
  1849. //
  1850. //
  1851. //Revision History :
  1852. //
  1853. // Date Author Comments
  1854. //
  1855. //////////////////////////////////////////////////////////////////////////////////////////
  1856. DWORD
  1857. FloorCountTransportRuleOfMMFilter(
  1858. IN ADDR SrcAddr,
  1859. IN ADDR DstAddr,
  1860. IN BOOL bMirror,
  1861. IN IF_TYPE ConType,
  1862. IN BOOL bSrcMask,
  1863. IN BOOL bDstMask,
  1864. IN OUT DWORD& dwStatus
  1865. )
  1866. {
  1867. const DWORD MIN_MATCH_REQUIRED = 1; // At least one filter required to match.
  1868. DWORD dwResumeHandle = 0; // handle for continuation calls
  1869. DWORD dwCount = 0; // counting objects here
  1870. DWORD dwVersion = 0;
  1871. DWORD dwNameLen = 0;
  1872. GUID gDefaultGUID = {0}; // NULL GUID value
  1873. DWORD i=0, j=0;
  1874. BOOL bFoundFilter = FALSE;
  1875. PTRANSPORT_FILTER pQMFilterRule_local = NULL;
  1876. PTRANSPORT_FILTER pTransF = NULL;
  1877. DWORD dwTransFFloorCount = 0;
  1878. BOOL bLastTransportRuleOfFilter = FALSE;
  1879. for (i = 0; ;i+=dwCount)
  1880. {
  1881. dwStatus = EnumTransportFilters(g_szDynamicMachine, dwVersion, NULL, ENUM_GENERIC_FILTERS,
  1882. gDefaultGUID, 0, &pTransF, &dwCount, &dwResumeHandle, NULL);
  1883. if ( (dwStatus == ERROR_NO_DATA) || (dwCount == 0) || (dwStatus != ERROR_SUCCESS))
  1884. {
  1885. dwStatus = ERROR_SUCCESS;
  1886. BAIL_OUT;
  1887. }
  1888. else if(!(pTransF && dwCount > 0))
  1889. {
  1890. BAIL_OUT; // not required to continue.
  1891. }
  1892. for (j = 0; j < dwCount; j++)
  1893. {
  1894. // Match the user given input with the enumerated structure to get the exact match
  1895. if((pTransF[j].SrcAddr.uIpAddr == SrcAddr.uIpAddr) &&
  1896. (pTransF[j].SrcAddr.AddrType == SrcAddr.AddrType) &&
  1897. (pTransF[j].DesAddr.uIpAddr == DstAddr.uIpAddr) &&
  1898. (pTransF[j].DesAddr.AddrType == DstAddr.AddrType) &&
  1899. (pTransF[j].bCreateMirror == bMirror) &&
  1900. (pTransF[j].InterfaceType == ConType))
  1901. {
  1902. // if both source and destination mask are not given
  1903. if((!bDstMask) && (!bSrcMask))
  1904. {
  1905. dwTransFFloorCount++;
  1906. if (dwTransFFloorCount >= MIN_MATCH_REQUIRED)
  1907. {
  1908. break;
  1909. }
  1910. }
  1911. //
  1912. // If source mask is given
  1913. //
  1914. else if((!bDstMask) && (bSrcMask))
  1915. {
  1916. if(pTransF[j].SrcAddr.uSubNetMask == SrcAddr.uSubNetMask)
  1917. {
  1918. dwTransFFloorCount++;
  1919. if (dwTransFFloorCount >= MIN_MATCH_REQUIRED)
  1920. {
  1921. break;
  1922. }
  1923. }
  1924. }
  1925. //
  1926. // If destination mask is given
  1927. //
  1928. else if((bDstMask) && (!bSrcMask))
  1929. {
  1930. if(pTransF[j].DesAddr.uSubNetMask == DstAddr.uSubNetMask)
  1931. {
  1932. dwTransFFloorCount++;
  1933. if (dwTransFFloorCount >= MIN_MATCH_REQUIRED)
  1934. {
  1935. break;
  1936. }
  1937. }
  1938. }
  1939. //
  1940. // If source mask and destination mask are given
  1941. //
  1942. else if((bDstMask) && (bSrcMask))
  1943. {
  1944. if(pTransF[j].DesAddr.uSubNetMask == DstAddr.uSubNetMask)
  1945. {
  1946. if(pTransF[j].SrcAddr.uSubNetMask == SrcAddr.uSubNetMask)
  1947. {
  1948. dwTransFFloorCount++;
  1949. if (dwTransFFloorCount >= MIN_MATCH_REQUIRED)
  1950. {
  1951. break;
  1952. }
  1953. }
  1954. }
  1955. }
  1956. }
  1957. }
  1958. SPDApiBufferFree(pTransF);
  1959. pTransF = NULL;
  1960. }
  1961. error:
  1962. if(pTransF)
  1963. {
  1964. SPDApiBufferFree(pTransF);
  1965. pTransF = NULL;
  1966. }
  1967. return dwTransFFloorCount;
  1968. }
  1969. ///////////////////////////////////////////////////////////////////////////////////////////
  1970. //
  1971. // Function : FloorCountTunnelRuleOfMMFilter
  1972. //
  1973. // Date of Creation: 05-19-02
  1974. //
  1975. // Parameters : IN ADDR SrcAddr,
  1976. // IN ADDR DstAddr,
  1977. // IN BOOL bMirror,
  1978. // IN IF_TYPE ConType,
  1979. // IN BOOL bSrcMask,
  1980. // IN BOOL bDstMask,
  1981. // IN OUT DWORD& dwStatus
  1982. //
  1983. //Return : DWORD
  1984. //
  1985. //Description : Counts if there is at least one tunnel filter that matches the
  1986. // given keys. We are not interested in getting the exact count,
  1987. // just whether we have more than one.
  1988. //
  1989. //
  1990. //
  1991. //
  1992. //Revision History :
  1993. //
  1994. // Date Author Comments
  1995. //
  1996. //////////////////////////////////////////////////////////////////////////////////////////
  1997. DWORD
  1998. FloorCountTunnelRuleOfMMFilter(
  1999. IN ADDR SrcTunnel,
  2000. IN ADDR DstTunnel,
  2001. IN BOOL bMirror,
  2002. IN IF_TYPE ConType,
  2003. OUT DWORD& dwStatus
  2004. )
  2005. {
  2006. const DWORD MIN_MATCH_REQUIRED = 1; // At least one filter required to match.
  2007. DWORD dwResumeHandle = 0; // handle for continuation calls
  2008. DWORD dwCount = 0; // counting objects here
  2009. DWORD dwVersion = 0;
  2010. DWORD dwNameLen = 0;
  2011. GUID gDefaultGUID = {0}; // NULL GUID value
  2012. DWORD i=0, j=0;
  2013. PTUNNEL_FILTER pTransF = NULL;
  2014. DWORD dwTransFFloorCount = 0;
  2015. for (i = 0; ;i+=dwCount)
  2016. {
  2017. dwStatus = EnumTunnelFilters(g_szDynamicMachine, dwVersion, NULL, ENUM_GENERIC_FILTERS,
  2018. gDefaultGUID, 0, &pTransF, &dwCount, &dwResumeHandle, NULL);
  2019. if ( (dwStatus == ERROR_NO_DATA) || (dwCount == 0) || (dwStatus != ERROR_SUCCESS))
  2020. {
  2021. dwStatus = ERROR_SUCCESS;
  2022. BAIL_OUT;
  2023. }
  2024. else if(!(pTransF && dwCount > 0))
  2025. {
  2026. break; // not required to continue.
  2027. }
  2028. for (j = 0; j < dwCount; j++)
  2029. {
  2030. //
  2031. // Match the user given input with the enumerated structure to get the exact match
  2032. //
  2033. if( (pTransF[j].bCreateMirror == bMirror) &&
  2034. (pTransF[j].InterfaceType == ConType) &&
  2035. (pTransF[j].DesTunnelAddr.uIpAddr == DstTunnel.uIpAddr) &&
  2036. (pTransF[j].DesTunnelAddr.AddrType == DstTunnel.AddrType) &&
  2037. (pTransF[j].SrcTunnelAddr.uIpAddr == SrcTunnel.uIpAddr) &&
  2038. (pTransF[j].SrcTunnelAddr.AddrType == SrcTunnel.AddrType))
  2039. {
  2040. dwTransFFloorCount++;
  2041. if (dwTransFFloorCount >= MIN_MATCH_REQUIRED)
  2042. {
  2043. break;
  2044. }
  2045. }
  2046. }
  2047. SPDApiBufferFree(pTransF);
  2048. pTransF = NULL;
  2049. }
  2050. error:
  2051. if(pTransF)
  2052. {
  2053. SPDApiBufferFree(pTransF);
  2054. }
  2055. return dwTransFFloorCount;
  2056. }