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.

1342 lines
30 KiB

  1. #include "precomp.h"
  2. #include "Nsu.h"
  3. #define PADDR_NET_PORTION(pAddr) ((pAddr)->uSubNetMask & (pAddr)->uIpAddr)
  4. #define PADDR_HOST_PORTION(pAddr) ((0xffffffff ^ (pAddr)->uSubNetMask) & (pAddr)->uIpAddr)
  5. DWORD
  6. ValidateAddr(
  7. PADDR pAddr
  8. )
  9. {
  10. DWORD dwError = 0;
  11. if (pAddr->AddrType == IP_ADDR_INTERFACE) {
  12. if (!(pAddr->pgInterfaceID)) {
  13. dwError = ERROR_INVALID_PARAMETER;
  14. BAIL_ON_WIN32_ERROR(dwError);
  15. }
  16. }
  17. else {
  18. if (pAddr->pgInterfaceID) {
  19. dwError = ERROR_INVALID_PARAMETER;
  20. BAIL_ON_WIN32_ERROR(dwError);
  21. }
  22. }
  23. error:
  24. return (dwError);
  25. }
  26. BOOL
  27. IsMulticastAddress(PADDR pAddr)
  28. {
  29. ASSERT (pAddr != NULL);
  30. return IN_MULTICAST(pAddr->uIpAddr);
  31. }
  32. BOOL
  33. IsBroadcastAddress(PADDR pAddr)
  34. {
  35. ASSERT (pAddr != NULL);
  36. return (pAddr->uIpAddr == INADDR_BROADCAST || IsSubnetBroadcastAddress(pAddr));
  37. }
  38. BOOL
  39. IsSubnetBroadcastAddress(
  40. PADDR pAddr
  41. )
  42. {
  43. ASSERT (pAddr != NULL);
  44. return (((pAddr->uSubNetMask == 0) ||bIsValidIPMask(pAddr->uSubNetMask)) &&
  45. (PADDR_NET_PORTION(pAddr) != 0) &&
  46. (pAddr->uSubNetMask != 0xffffffff) &&
  47. ((PADDR_HOST_PORTION(pAddr) | pAddr->uSubNetMask) == 0xffffffff));
  48. }
  49. BOOL
  50. IsUnicastAddress(PADDR pAddr)
  51. {
  52. ASSERT (pAddr != NULL);
  53. return (!(IsMulticastAddress(pAddr) || IsBroadcastAddress(pAddr)));
  54. }
  55. // This will go away if/when we change the code to accept any unicast address; at that time IsUnicastAddress() will
  56. // be the correct function to call
  57. BOOL
  58. IsSupportedAddress(PADDR pAddr)
  59. {
  60. ASSERT (pAddr != NULL);
  61. if (!IsSpecialServer(pAddr) && (
  62. (IN_CLASSE(pAddr->uIpAddr)) ||
  63. ((IN_CLASSA_NET & pAddr->uIpAddr) == 0) || // first octet can't be 0
  64. (IsLoopbackAddress(pAddr))
  65. ))
  66. {
  67. return FALSE;
  68. }
  69. return TRUE;
  70. }
  71. BOOL
  72. IsLoopbackAddress(PADDR pAddr)
  73. {
  74. ASSERT (pAddr != NULL);
  75. return (pAddr->uIpAddr == INADDR_LOOPBACK);
  76. }
  77. BOOL
  78. IsValidTunnelEndpointAddress(PADDR pAddr)
  79. {
  80. ASSERT (pAddr != NULL);
  81. return IsUnicastAddress(pAddr);
  82. }
  83. BOOL
  84. IsSpecialServer(PADDR pAddr)
  85. {
  86. ASSERT (pAddr != NULL);
  87. return IsSpecialServ(pAddr->AddrType);
  88. }
  89. BOOL
  90. IsValidSubnet(
  91. PADDR pAddr
  92. )
  93. {
  94. ASSERT (pAddr != NULL);
  95. return ((PADDR_NET_PORTION(pAddr) != 0) && (PADDR_HOST_PORTION(pAddr) == 0));
  96. }
  97. BOOL
  98. IsValidSubnettedAddress(
  99. PADDR pAddr
  100. )
  101. {
  102. ASSERT (pAddr != NULL);
  103. if ((pAddr->AddrType != IP_ADDR_UNIQUE) || (!bIsValidIPMask(pAddr->uSubNetMask)))
  104. {
  105. return FALSE;
  106. }
  107. return ((PADDR_NET_PORTION(pAddr) != 0) && (PADDR_HOST_PORTION(pAddr) != 0));
  108. }
  109. DWORD
  110. ValidateMMPolicy(
  111. PIPSEC_MM_POLICY pMMPolicy
  112. )
  113. {
  114. DWORD dwError = 0;
  115. if (!pMMPolicy) {
  116. dwError = ERROR_INVALID_PARAMETER;
  117. BAIL_ON_WIN32_ERROR(dwError);
  118. }
  119. if (!(pMMPolicy->pszPolicyName) || !(*(pMMPolicy->pszPolicyName))) {
  120. dwError = ERROR_INVALID_PARAMETER;
  121. BAIL_ON_WIN32_ERROR(dwError);
  122. }
  123. dwError = ValidateMMOffers(
  124. pMMPolicy->dwOfferCount,
  125. pMMPolicy->pOffers
  126. );
  127. BAIL_ON_WIN32_ERROR(dwError);
  128. error:
  129. return (dwError);
  130. }
  131. DWORD
  132. ValidateMMOffers(
  133. DWORD dwOfferCount,
  134. PIPSEC_MM_OFFER pOffers
  135. )
  136. {
  137. DWORD dwError = 0;
  138. DWORD i = 0;
  139. if (!dwOfferCount || !pOffers || (dwOfferCount > IPSEC_MAX_MM_OFFERS)) {
  140. dwError = ERROR_INVALID_PARAMETER;
  141. BAIL_ON_WIN32_ERROR(dwError);
  142. }
  143. error:
  144. return (dwError);
  145. }
  146. DWORD CompareCertRootConfig(PCERT_ROOT_CONFIG pCertRootConfig1,
  147. PCERT_ROOT_CONFIG pCertRootConfig2)
  148. {
  149. DWORD dwError = ERROR_SUCCESS;
  150. if (pCertRootConfig1->dwCertDataSize != pCertRootConfig2->dwCertDataSize) {
  151. dwError = ERROR_NOT_SUPPORTED;
  152. BAIL_ON_WIN32_ERROR(dwError);
  153. }
  154. if (memcmp(pCertRootConfig1->pCertData,
  155. pCertRootConfig2->pCertData,
  156. pCertRootConfig1->dwCertDataSize) != 0) {
  157. dwError = ERROR_NOT_SUPPORTED;
  158. BAIL_ON_WIN32_ERROR(dwError);
  159. }
  160. // Don't need to compare AuthorizationData since this must currently always be 0.
  161. error:
  162. return (dwError);
  163. }
  164. DWORD ValidateCertRootConfig(PCERT_ROOT_CONFIG pCertRootConfig)
  165. {
  166. DWORD dwError = 0;
  167. if (pCertRootConfig->dwCertDataSize == 0 ||
  168. pCertRootConfig->pCertData == NULL) {
  169. dwError = ERROR_INVALID_PARAMETER;
  170. BAIL_ON_WIN32_ERROR(dwError);
  171. }
  172. if (pCertRootConfig->dwAuthorizationDataSize ||
  173. pCertRootConfig->pAuthorizationData) {
  174. dwError = ERROR_NOT_SUPPORTED;
  175. BAIL_ON_WIN32_ERROR(dwError);
  176. }
  177. error:
  178. return (dwError);
  179. }
  180. DWORD ValidateCertAuthInfo(PMM_CERT_INFO pCertInfo)
  181. {
  182. DWORD dwError = 0;
  183. DWORD i;
  184. if (pCertInfo->dwVersion != 0) {
  185. dwError = ERROR_NOT_SUPPORTED;
  186. BAIL_ON_WIN32_ERROR(dwError);
  187. }
  188. if (pCertInfo->dwMyCertHashSize || pCertInfo->pMyCertHash) {
  189. dwError = ERROR_NOT_SUPPORTED;
  190. BAIL_ON_WIN32_ERROR(dwError);
  191. }
  192. // Outbound root array MUST be idential to inbound
  193. if (pCertInfo->dwInboundRootArraySize != pCertInfo->dwOutboundRootArraySize) {
  194. dwError = ERROR_NOT_SUPPORTED;
  195. BAIL_ON_WIN32_ERROR(dwError);
  196. }
  197. for (i=0; i < pCertInfo->dwInboundRootArraySize; i++) {
  198. dwError = ValidateCertRootConfig(&pCertInfo->pInboundRootArray[i]);
  199. BAIL_ON_WIN32_ERROR(dwError);
  200. // Outbound root array MUST be idential to inbound
  201. dwError = CompareCertRootConfig(&pCertInfo->pInboundRootArray[i],
  202. &pCertInfo->pOutboundRootArray[i]);
  203. BAIL_ON_WIN32_ERROR(dwError);
  204. }
  205. error:
  206. return (dwError);
  207. }
  208. DWORD
  209. ValidateMMAuthMethods(
  210. PMM_AUTH_METHODS pMMAuthMethods
  211. )
  212. {
  213. DWORD dwError = 0;
  214. DWORD i = 0;
  215. PIPSEC_MM_AUTH_INFO pTemp = NULL;
  216. DWORD dwNumAuthInfos = 0;
  217. PIPSEC_MM_AUTH_INFO pAuthenticationInfo = NULL;
  218. BOOL bSSPI = FALSE;
  219. BOOL bPresharedKey = FALSE;
  220. BOOL bRSASig= FALSE;
  221. if (!pMMAuthMethods) {
  222. dwError = ERROR_INVALID_PARAMETER;
  223. BAIL_ON_WIN32_ERROR(dwError);
  224. }
  225. dwNumAuthInfos = pMMAuthMethods->dwNumAuthInfos;
  226. pAuthenticationInfo = pMMAuthMethods->pAuthenticationInfo;
  227. if (!dwNumAuthInfos || !pAuthenticationInfo) {
  228. dwError = ERROR_INVALID_PARAMETER;
  229. BAIL_ON_WIN32_ERROR(dwError);
  230. }
  231. //
  232. // Need to catch the exception when the number of auth infos
  233. // specified is more than the actual number of auth infos.
  234. //
  235. pTemp = pAuthenticationInfo;
  236. for (i = 0; i < dwNumAuthInfos; i++) {
  237. if ((pTemp->AuthMethod != IKE_PRESHARED_KEY) &&
  238. (pTemp->AuthMethod != IKE_RSA_SIGNATURE) &&
  239. (pTemp->AuthMethod != IKE_SSPI)) {
  240. dwError = ERROR_INVALID_PARAMETER;
  241. BAIL_ON_WIN32_ERROR(dwError);
  242. }
  243. if (pTemp->AuthMethod == IKE_RSA_SIGNATURE) {
  244. dwError = ValidateCertAuthInfo(&pTemp->CertAuthInfo);
  245. BAIL_ON_WIN32_ERROR(dwError);
  246. if (bRSASig) {
  247. dwError = ERROR_INVALID_PARAMETER;
  248. BAIL_ON_WIN32_ERROR(dwError);
  249. }
  250. bRSASig = TRUE;
  251. }
  252. if (pTemp->AuthMethod == IKE_SSPI) {
  253. if (bSSPI) {
  254. dwError = ERROR_INVALID_PARAMETER;
  255. BAIL_ON_WIN32_ERROR(dwError);
  256. }
  257. bSSPI = TRUE;
  258. }
  259. if (pTemp->AuthMethod == IKE_PRESHARED_KEY) {
  260. if (!(pTemp->GeneralAuthInfo.dwAuthInfoSize) ||
  261. !(pTemp->GeneralAuthInfo.pAuthInfo)) {
  262. dwError = ERROR_INVALID_PARAMETER;
  263. BAIL_ON_WIN32_ERROR(dwError);
  264. }
  265. if (bPresharedKey) {
  266. dwError = ERROR_INVALID_PARAMETER;
  267. BAIL_ON_WIN32_ERROR(dwError);
  268. }
  269. bPresharedKey = TRUE;
  270. }
  271. pTemp++;
  272. }
  273. error:
  274. return (dwError);
  275. }
  276. DWORD
  277. ValidateQMPolicy(
  278. PIPSEC_QM_POLICY pQMPolicy
  279. )
  280. {
  281. DWORD dwError = 0;
  282. if (!pQMPolicy) {
  283. dwError = ERROR_INVALID_PARAMETER;
  284. BAIL_ON_WIN32_ERROR(dwError);
  285. }
  286. if (!(pQMPolicy->pszPolicyName) || !(*(pQMPolicy->pszPolicyName))) {
  287. dwError = ERROR_INVALID_PARAMETER;
  288. BAIL_ON_WIN32_ERROR(dwError);
  289. }
  290. dwError = ValidateQMOffers(
  291. pQMPolicy->dwOfferCount,
  292. pQMPolicy->pOffers
  293. );
  294. BAIL_ON_WIN32_ERROR(dwError);
  295. error:
  296. return (dwError);
  297. }
  298. DWORD
  299. ValidateQMOffers(
  300. DWORD dwOfferCount,
  301. PIPSEC_QM_OFFER pOffers
  302. )
  303. {
  304. DWORD dwError = 0;
  305. if (!dwOfferCount || !pOffers || (dwOfferCount > IPSEC_MAX_QM_OFFERS)) {
  306. dwError = ERROR_INVALID_PARAMETER;
  307. BAIL_ON_WIN32_ERROR(dwError);
  308. }
  309. error:
  310. return (dwError);
  311. }
  312. DWORD
  313. ValidateMMFilter(
  314. PMM_FILTER pMMFilter
  315. )
  316. /*++
  317. Routine Description:
  318. This function validates an external generic MM filter.
  319. Arguments:
  320. pMMFilter - Filter to validate.
  321. Return Value:
  322. ERROR_SUCCESS - Success.
  323. Win32 Error - Failure.
  324. --*/
  325. {
  326. DWORD dwError = 0;
  327. BOOL bConflicts = FALSE;
  328. if (!pMMFilter) {
  329. dwError = ERROR_INVALID_PARAMETER;
  330. BAIL_ON_WIN32_ERROR(dwError);
  331. }
  332. dwError = VerifyAddresses(&(pMMFilter->SrcAddr), TRUE , FALSE);
  333. BAIL_ON_WIN32_ERROR(dwError);
  334. dwError = VerifyAddresses(&(pMMFilter->DesAddr), TRUE , TRUE);
  335. BAIL_ON_WIN32_ERROR(dwError);
  336. bConflicts = HtoNAddressesConflict(
  337. pMMFilter->SrcAddr,
  338. pMMFilter->DesAddr
  339. );
  340. if (bConflicts) {
  341. dwError = ERROR_INVALID_PARAMETER;
  342. BAIL_ON_WIN32_ERROR(dwError);
  343. }
  344. if (!(pMMFilter->pszFilterName) || !(*(pMMFilter->pszFilterName))) {
  345. dwError = ERROR_INVALID_PARAMETER;
  346. BAIL_ON_WIN32_ERROR(dwError);
  347. }
  348. dwError = ValidateInterfaceType(pMMFilter->InterfaceType);
  349. BAIL_ON_WIN32_ERROR(dwError);
  350. if (pMMFilter->dwFlags &&
  351. !(pMMFilter->dwFlags & IPSEC_MM_POLICY_DEFAULT_POLICY) &&
  352. !(pMMFilter->dwFlags & IPSEC_MM_AUTH_DEFAULT_AUTH)) {
  353. dwError = ERROR_INVALID_PARAMETER;
  354. BAIL_ON_WIN32_ERROR(dwError);
  355. }
  356. dwError = ApplyMulticastFilterValidation(
  357. pMMFilter->DesAddr,
  358. pMMFilter->bCreateMirror
  359. );
  360. BAIL_ON_WIN32_ERROR(dwError);
  361. error:
  362. return (dwError);
  363. }
  364. DWORD
  365. VerifyAddresses(
  366. PADDR pAddr,
  367. BOOL bAcceptMe,
  368. BOOL bIsDesAddr
  369. )
  370. {
  371. DWORD dwError = 0;
  372. BOOL bIsValid = FALSE;
  373. switch (pAddr->AddrType) {
  374. case IP_ADDR_UNIQUE:
  375. bIsValid = bIsValidIPAddress(
  376. ntohl(pAddr->uIpAddr),
  377. bAcceptMe,
  378. bIsDesAddr
  379. );
  380. if (!bIsValid) {
  381. dwError = ERROR_INVALID_PARAMETER;
  382. BAIL_ON_WIN32_ERROR(dwError);
  383. }
  384. if (pAddr->pgInterfaceID) {
  385. dwError = ERROR_INVALID_PARAMETER;
  386. BAIL_ON_WIN32_ERROR(dwError);
  387. }
  388. break;
  389. case IP_ADDR_SUBNET:
  390. dwError = VerifySubNetAddress(
  391. ntohl(pAddr->uIpAddr),
  392. ntohl(pAddr->uSubNetMask),
  393. bIsDesAddr
  394. );
  395. BAIL_ON_WIN32_ERROR(dwError);
  396. if (pAddr->pgInterfaceID) {
  397. dwError = ERROR_INVALID_PARAMETER;
  398. BAIL_ON_WIN32_ERROR(dwError);
  399. }
  400. break;
  401. case IP_ADDR_INTERFACE:
  402. if (pAddr->uIpAddr) {
  403. dwError = ERROR_INVALID_PARAMETER;
  404. BAIL_ON_WIN32_ERROR(dwError);
  405. }
  406. if (!(pAddr->pgInterfaceID)) {
  407. dwError = ERROR_INVALID_PARAMETER;
  408. BAIL_ON_WIN32_ERROR(dwError);
  409. }
  410. break;
  411. default:
  412. if (!IsSpecialServ(pAddr->AddrType)) {
  413. dwError = ERROR_INVALID_PARAMETER;
  414. BAIL_ON_WIN32_ERROR(dwError);
  415. }
  416. break;
  417. }
  418. error:
  419. return (dwError);
  420. }
  421. DWORD
  422. VerifySubNetAddress(
  423. ULONG uSubNetAddr,
  424. ULONG uSubNetMask,
  425. BOOL bIsDesAddr
  426. )
  427. {
  428. DWORD dwError = 0;
  429. BOOL bIsValid = FALSE;
  430. if (uSubNetAddr == SUBNET_ADDRESS_ANY) {
  431. if (uSubNetMask != SUBNET_MASK_ANY) {
  432. dwError = ERROR_INVALID_PARAMETER;
  433. BAIL_ON_WIN32_ERROR(dwError);
  434. }
  435. }
  436. else {
  437. bIsValid = bIsValidSubnet(
  438. uSubNetAddr,
  439. uSubNetMask,
  440. bIsDesAddr
  441. );
  442. if (!bIsValid) {
  443. dwError = ERROR_INVALID_PARAMETER;
  444. BAIL_ON_WIN32_ERROR(dwError);
  445. }
  446. }
  447. error:
  448. return (dwError);
  449. }
  450. BOOL
  451. bIsValidIPMask(
  452. ULONG uMask
  453. )
  454. {
  455. BOOL bValidMask = FALSE;
  456. ULONG uTestMask = 0;
  457. //
  458. // Mask must be contiguous bits.
  459. //
  460. for (uTestMask = 0xFFFFFFFF; uTestMask; uTestMask <<= 1) {
  461. if (uTestMask == uMask) {
  462. bValidMask = TRUE;
  463. break;
  464. }
  465. }
  466. return (bValidMask);
  467. }
  468. BOOL
  469. bIsValidIPAddress(
  470. ULONG uIpAddr,
  471. BOOL bAcceptMe,
  472. BOOL bIsDesAddr
  473. )
  474. {
  475. ULONG uHostMask = IN_CLASSA_HOST; // Default host mask.
  476. //
  477. // Accept the address if its "me".
  478. //
  479. if (bAcceptMe) {
  480. if (uIpAddr == IP_ADDRESS_ME) {
  481. return TRUE;
  482. }
  483. }
  484. //
  485. // Reject if its a multicast address and is not the
  486. // destination address.
  487. //
  488. if (IN_CLASSD(uIpAddr)) {
  489. if (bIsDesAddr) {
  490. return TRUE;
  491. }
  492. else {
  493. return FALSE;
  494. }
  495. }
  496. //
  497. // Reject if its a Class E address.
  498. //
  499. if (IN_CLASSE(uIpAddr)) {
  500. return FALSE;
  501. }
  502. //
  503. // Reject if the first octet is zero.
  504. //
  505. if (!(IN_CLASSA_NET & uIpAddr)) {
  506. return FALSE;
  507. }
  508. //
  509. // Use default mask based on Class when none is provided.
  510. //
  511. if (IN_CLASSA(uIpAddr)) {
  512. uHostMask = IN_CLASSA_HOST;
  513. }
  514. else if (IN_CLASSB(uIpAddr)) {
  515. uHostMask = IN_CLASSB_HOST;
  516. }
  517. else if (IN_CLASSC(uIpAddr)) {
  518. uHostMask = IN_CLASSC_HOST;
  519. }
  520. //
  521. // Accept address when host portion is non-zero.
  522. //
  523. if (uHostMask & uIpAddr) {
  524. return TRUE;
  525. }
  526. return FALSE;
  527. }
  528. BOOL
  529. bIsValidSubnet(
  530. ULONG uIpAddr,
  531. ULONG uMask,
  532. BOOL bIsDesAddr
  533. )
  534. {
  535. ULONG uHostMask = 0;
  536. //
  537. // Reject if its a multicast address and is not the
  538. // destination address.
  539. //
  540. if (IN_CLASSD(uIpAddr)) {
  541. if (!bIsDesAddr) {
  542. return FALSE;
  543. }
  544. }
  545. //
  546. // Reject if its a Class E address.
  547. //
  548. if (IN_CLASSE(uIpAddr)) {
  549. return FALSE;
  550. }
  551. //
  552. // Reject if the first octet is zero.
  553. //
  554. if (!(IN_CLASSA_NET & uIpAddr)) {
  555. return FALSE;
  556. }
  557. //
  558. // If the mask is invalid then return.
  559. //
  560. if (!bIsValidIPMask(uMask)) {
  561. return FALSE;
  562. }
  563. //
  564. // Use the provided subnet mask to generate the host mask.
  565. //
  566. uHostMask = 0xFFFFFFFF ^ uMask;
  567. //
  568. // Accept address only when the host portion is zero, network
  569. // portion is non-zero and first octet is non-zero.
  570. //
  571. if (!(uHostMask & uIpAddr) &&
  572. (uMask & uIpAddr) &&
  573. (IN_CLASSA_NET & uIpAddr)) {
  574. return TRUE;
  575. }
  576. return FALSE;
  577. }
  578. // AddressesConflict requires addresses in network byte order.
  579. //
  580. BOOL
  581. AddressesConflict(
  582. ADDR SrcAddr,
  583. ADDR DesAddr
  584. )
  585. {
  586. if ((SrcAddr.AddrType == IP_ADDR_UNIQUE) &&
  587. (DesAddr.AddrType == IP_ADDR_UNIQUE) &&
  588. (SrcAddr.uIpAddr == DesAddr.uIpAddr))
  589. {
  590. return (TRUE);
  591. }
  592. if ((SrcAddr.AddrType == IP_ADDR_INTERFACE) &&
  593. (DesAddr.AddrType == IP_ADDR_INTERFACE))
  594. {
  595. return (TRUE);
  596. }
  597. if (IsSpecialServer(&SrcAddr) &&
  598. (SrcAddr.AddrType == DesAddr.AddrType))
  599. {
  600. return (TRUE);
  601. }
  602. if (IsMulticastAddress(&SrcAddr) || IsBroadcastAddress(&SrcAddr))
  603. {
  604. return (TRUE);
  605. }
  606. return (FALSE);
  607. }
  608. // Special version of AddressesConflict for Winipsec.dll functions that requires
  609. // addresses in Host order. This fix was made for bug# 708188
  610. // "IPSec validation functions must convert from host to network order before validation"
  611. // for RC2 and given test and dev resources was the safest and quickest fix.
  612. BOOL
  613. HtoNAddressesConflict(
  614. ADDR SrcAddr,
  615. ADDR DesAddr
  616. )
  617. {
  618. SrcAddr.uIpAddr=htonl(SrcAddr.uIpAddr);
  619. SrcAddr.uSubNetMask=htonl(SrcAddr.uSubNetMask);
  620. DesAddr.uIpAddr=htonl(DesAddr.uIpAddr);
  621. DesAddr.uSubNetMask=htonl(DesAddr.uSubNetMask);
  622. return AddressesConflict(SrcAddr, DesAddr);
  623. }
  624. DWORD
  625. ValidateTransportFilter(
  626. PTRANSPORT_FILTER pTransportFilter
  627. )
  628. {
  629. DWORD dwError = 0;
  630. BOOL bConflicts = FALSE;
  631. if (!pTransportFilter) {
  632. dwError = ERROR_INVALID_PARAMETER;
  633. BAIL_ON_WIN32_ERROR(dwError);
  634. }
  635. dwError = VerifyAddresses(&(pTransportFilter->SrcAddr), TRUE, FALSE);
  636. BAIL_ON_WIN32_ERROR(dwError);
  637. dwError = VerifyAddresses(&(pTransportFilter->DesAddr), TRUE, TRUE);
  638. BAIL_ON_WIN32_ERROR(dwError);
  639. bConflicts = HtoNAddressesConflict(
  640. pTransportFilter->SrcAddr,
  641. pTransportFilter->DesAddr
  642. );
  643. if (bConflicts) {
  644. dwError = ERROR_INVALID_PARAMETER;
  645. BAIL_ON_WIN32_ERROR(dwError);
  646. }
  647. dwError = VerifyProtocols(pTransportFilter->Protocol);
  648. BAIL_ON_WIN32_ERROR(dwError);
  649. dwError = VerifyPortsForProtocol(
  650. pTransportFilter->SrcPort,
  651. pTransportFilter->Protocol
  652. );
  653. BAIL_ON_WIN32_ERROR(dwError);
  654. dwError = VerifyPortsForProtocol(
  655. pTransportFilter->DesPort,
  656. pTransportFilter->Protocol
  657. );
  658. BAIL_ON_WIN32_ERROR(dwError);
  659. if (!(pTransportFilter->pszFilterName) || !(*(pTransportFilter->pszFilterName))) {
  660. dwError = ERROR_INVALID_PARAMETER;
  661. BAIL_ON_WIN32_ERROR(dwError);
  662. }
  663. dwError = ValidateInterfaceType(pTransportFilter->InterfaceType);
  664. BAIL_ON_WIN32_ERROR(dwError);
  665. dwError = ValidateFilterAction(pTransportFilter->InboundFilterAction);
  666. BAIL_ON_WIN32_ERROR(dwError);
  667. dwError = ValidateFilterAction(pTransportFilter->OutboundFilterAction);
  668. BAIL_ON_WIN32_ERROR(dwError);
  669. if (pTransportFilter->dwFlags &&
  670. !(pTransportFilter->dwFlags & IPSEC_QM_POLICY_DEFAULT_POLICY)) {
  671. dwError = ERROR_INVALID_PARAMETER;
  672. BAIL_ON_WIN32_ERROR(dwError);
  673. }
  674. dwError = ApplyMulticastFilterValidation(
  675. pTransportFilter->DesAddr,
  676. pTransportFilter->bCreateMirror
  677. );
  678. BAIL_ON_WIN32_ERROR(dwError);
  679. error:
  680. return (dwError);
  681. }
  682. DWORD
  683. ValidateIPSecQMFilter(
  684. PIPSEC_QM_FILTER pQMFilter
  685. )
  686. {
  687. DWORD dwError = 0;
  688. BOOL bConflicts = FALSE;
  689. if (!pQMFilter) {
  690. dwError = ERROR_INVALID_PARAMETER;
  691. BAIL_ON_WIN32_ERROR(dwError);
  692. }
  693. dwError = VerifyAddresses(&(pQMFilter->SrcAddr), FALSE, FALSE);
  694. BAIL_ON_WIN32_ERROR(dwError);
  695. dwError = VerifyAddresses(&(pQMFilter->DesAddr), FALSE, TRUE);
  696. BAIL_ON_WIN32_ERROR(dwError);
  697. bConflicts = HtoNAddressesConflict(
  698. pQMFilter->SrcAddr,
  699. pQMFilter->DesAddr
  700. );
  701. if (bConflicts) {
  702. dwError = ERROR_INVALID_PARAMETER;
  703. BAIL_ON_WIN32_ERROR(dwError);
  704. }
  705. dwError = VerifyProtocols(pQMFilter->Protocol);
  706. BAIL_ON_WIN32_ERROR(dwError);
  707. dwError = VerifyPortsForProtocol(
  708. pQMFilter->SrcPort,
  709. pQMFilter->Protocol
  710. );
  711. BAIL_ON_WIN32_ERROR(dwError);
  712. dwError = VerifyPortsForProtocol(
  713. pQMFilter->DesPort,
  714. pQMFilter->Protocol
  715. );
  716. BAIL_ON_WIN32_ERROR(dwError);
  717. if (pQMFilter->QMFilterType == QM_TUNNEL_FILTER) {
  718. if (pQMFilter->MyTunnelEndpt.AddrType != IP_ADDR_UNIQUE) {
  719. dwError=ERROR_INVALID_PARAMETER;
  720. BAIL_ON_WIN32_ERROR(dwError);
  721. }
  722. if (pQMFilter->PeerTunnelEndpt.AddrType != IP_ADDR_UNIQUE) {
  723. dwError=ERROR_INVALID_PARAMETER;
  724. BAIL_ON_WIN32_ERROR(dwError);
  725. }
  726. dwError = VerifyAddresses(&(pQMFilter->MyTunnelEndpt), FALSE, FALSE);
  727. BAIL_ON_WIN32_ERROR(dwError);
  728. dwError = VerifyAddresses(&(pQMFilter->PeerTunnelEndpt), FALSE, FALSE);
  729. BAIL_ON_WIN32_ERROR(dwError);
  730. }
  731. else {
  732. dwError = ValidateAddr(&(pQMFilter->MyTunnelEndpt));
  733. BAIL_ON_WIN32_ERROR(dwError);
  734. dwError = ValidateAddr(&(pQMFilter->PeerTunnelEndpt));
  735. BAIL_ON_WIN32_ERROR(dwError);
  736. }
  737. if (pQMFilter->QMFilterType != QM_TUNNEL_FILTER &&
  738. pQMFilter->QMFilterType != QM_TRANSPORT_FILTER) {
  739. dwError=ERROR_INVALID_PARAMETER;
  740. BAIL_ON_WIN32_ERROR(dwError);
  741. }
  742. error:
  743. return (dwError);
  744. }
  745. DWORD
  746. VerifyProtocols(
  747. PROTOCOL Protocol
  748. )
  749. {
  750. DWORD dwError = 0;
  751. switch (Protocol.ProtocolType) {
  752. case PROTOCOL_UNIQUE:
  753. if (Protocol.dwProtocol > 255) {
  754. dwError = ERROR_INVALID_PARAMETER;
  755. BAIL_ON_WIN32_ERROR(dwError);
  756. }
  757. break;
  758. default:
  759. dwError = ERROR_INVALID_PARAMETER;
  760. BAIL_ON_WIN32_ERROR(dwError);
  761. break;
  762. }
  763. error:
  764. return (dwError);
  765. }
  766. DWORD
  767. VerifyPortsForProtocol(
  768. PORT Port,
  769. PROTOCOL Protocol
  770. )
  771. {
  772. DWORD dwError = 0;
  773. switch (Port.PortType) {
  774. case PORT_UNIQUE:
  775. if (Port.wPort < 0) {
  776. dwError = ERROR_INVALID_PARAMETER;
  777. BAIL_ON_WIN32_ERROR(dwError);
  778. }
  779. switch (Protocol.ProtocolType) {
  780. case PROTOCOL_UNIQUE:
  781. if ((Protocol.dwProtocol != IPPROTO_TCP) &&
  782. (Protocol.dwProtocol != IPPROTO_UDP)) {
  783. if (Port.wPort != 0) {
  784. dwError = ERROR_INVALID_PARAMETER;
  785. BAIL_ON_WIN32_ERROR(dwError);
  786. }
  787. }
  788. break;
  789. default:
  790. dwError = ERROR_INVALID_PARAMETER;
  791. BAIL_ON_WIN32_ERROR(dwError);
  792. break;
  793. }
  794. break;
  795. default:
  796. dwError = ERROR_INVALID_PARAMETER;
  797. BAIL_ON_WIN32_ERROR(dwError);
  798. break;
  799. }
  800. error:
  801. return (dwError);
  802. }
  803. DWORD
  804. ValidateMMFilterTemplate(
  805. PMM_FILTER pMMFilter
  806. )
  807. {
  808. DWORD dwError = 0;
  809. BOOL bConflicts = FALSE;
  810. if (!pMMFilter) {
  811. dwError = ERROR_INVALID_PARAMETER;
  812. BAIL_ON_WIN32_ERROR(dwError);
  813. }
  814. dwError = VerifyAddresses(&(pMMFilter->SrcAddr), TRUE, FALSE);
  815. BAIL_ON_WIN32_ERROR(dwError);
  816. dwError = VerifyAddresses(&(pMMFilter->DesAddr), TRUE, TRUE);
  817. BAIL_ON_WIN32_ERROR(dwError);
  818. bConflicts = HtoNAddressesConflict(
  819. pMMFilter->SrcAddr,
  820. pMMFilter->DesAddr
  821. );
  822. if (bConflicts) {
  823. dwError = ERROR_INVALID_PARAMETER;
  824. BAIL_ON_WIN32_ERROR(dwError);
  825. }
  826. if (pMMFilter->dwDirection) {
  827. if ((pMMFilter->dwDirection != FILTER_DIRECTION_INBOUND) &&
  828. (pMMFilter->dwDirection != FILTER_DIRECTION_OUTBOUND)) {
  829. dwError = ERROR_INVALID_PARAMETER;
  830. BAIL_ON_WIN32_ERROR(dwError);
  831. }
  832. }
  833. error:
  834. return (dwError);
  835. }
  836. DWORD
  837. ValidateTxFilterTemplate(
  838. PTRANSPORT_FILTER pTxFilter
  839. )
  840. {
  841. DWORD dwError = 0;
  842. BOOL bConflicts = FALSE;
  843. if (!pTxFilter) {
  844. dwError = ERROR_INVALID_PARAMETER;
  845. BAIL_ON_WIN32_ERROR(dwError);
  846. }
  847. dwError = VerifyAddresses(&(pTxFilter->SrcAddr), TRUE, FALSE);
  848. BAIL_ON_WIN32_ERROR(dwError);
  849. dwError = VerifyAddresses(&(pTxFilter->DesAddr), TRUE, TRUE);
  850. BAIL_ON_WIN32_ERROR(dwError);
  851. bConflicts = HtoNAddressesConflict(
  852. pTxFilter->SrcAddr,
  853. pTxFilter->DesAddr
  854. );
  855. if (bConflicts) {
  856. dwError = ERROR_INVALID_PARAMETER;
  857. BAIL_ON_WIN32_ERROR(dwError);
  858. }
  859. dwError = VerifyProtocols(pTxFilter->Protocol);
  860. BAIL_ON_WIN32_ERROR(dwError);
  861. dwError = VerifyPortsForProtocol(
  862. pTxFilter->SrcPort,
  863. pTxFilter->Protocol
  864. );
  865. BAIL_ON_WIN32_ERROR(dwError);
  866. dwError = VerifyPortsForProtocol(
  867. pTxFilter->DesPort,
  868. pTxFilter->Protocol
  869. );
  870. BAIL_ON_WIN32_ERROR(dwError);
  871. if (pTxFilter->dwDirection) {
  872. if ((pTxFilter->dwDirection != FILTER_DIRECTION_INBOUND) &&
  873. (pTxFilter->dwDirection != FILTER_DIRECTION_OUTBOUND)) {
  874. dwError = ERROR_INVALID_PARAMETER;
  875. BAIL_ON_WIN32_ERROR(dwError);
  876. }
  877. }
  878. error:
  879. return (dwError);
  880. }
  881. DWORD
  882. ValidateTunnelFilter(
  883. PTUNNEL_FILTER pTunnelFilter
  884. )
  885. {
  886. DWORD dwError = 0;
  887. BOOL bConflicts = FALSE;
  888. if (!pTunnelFilter) {
  889. dwError = ERROR_INVALID_PARAMETER;
  890. BAIL_ON_WIN32_ERROR(dwError);
  891. }
  892. dwError = VerifyAddresses(&(pTunnelFilter->SrcAddr), TRUE, FALSE);
  893. BAIL_ON_WIN32_ERROR(dwError);
  894. dwError = VerifyAddresses(&(pTunnelFilter->DesAddr), TRUE, TRUE);
  895. BAIL_ON_WIN32_ERROR(dwError);
  896. bConflicts = HtoNAddressesConflict(
  897. pTunnelFilter->SrcAddr,
  898. pTunnelFilter->DesAddr
  899. );
  900. if (bConflicts) {
  901. dwError = ERROR_INVALID_PARAMETER;
  902. BAIL_ON_WIN32_ERROR(dwError);
  903. }
  904. dwError = ValidateAddr(&(pTunnelFilter->SrcTunnelAddr));
  905. BAIL_ON_WIN32_ERROR(dwError);
  906. dwError = VerifyAddresses(&(pTunnelFilter->DesTunnelAddr), TRUE, FALSE);
  907. BAIL_ON_WIN32_ERROR(dwError);
  908. dwError = VerifyProtocols(pTunnelFilter->Protocol);
  909. BAIL_ON_WIN32_ERROR(dwError);
  910. dwError = VerifyPortsForProtocol(
  911. pTunnelFilter->SrcPort,
  912. pTunnelFilter->Protocol
  913. );
  914. BAIL_ON_WIN32_ERROR(dwError);
  915. dwError = VerifyPortsForProtocol(
  916. pTunnelFilter->DesPort,
  917. pTunnelFilter->Protocol
  918. );
  919. BAIL_ON_WIN32_ERROR(dwError);
  920. if (!(pTunnelFilter->pszFilterName) || !(*(pTunnelFilter->pszFilterName))) {
  921. dwError = ERROR_INVALID_PARAMETER;
  922. BAIL_ON_WIN32_ERROR(dwError);
  923. }
  924. dwError = ValidateInterfaceType(pTunnelFilter->InterfaceType);
  925. BAIL_ON_WIN32_ERROR(dwError);
  926. if (pTunnelFilter->bCreateMirror) {
  927. dwError = ERROR_INVALID_PARAMETER;
  928. BAIL_ON_WIN32_ERROR(dwError);
  929. }
  930. dwError = ValidateFilterAction(pTunnelFilter->InboundFilterAction);
  931. BAIL_ON_WIN32_ERROR(dwError);
  932. dwError = ValidateFilterAction(pTunnelFilter->OutboundFilterAction);
  933. BAIL_ON_WIN32_ERROR(dwError);
  934. if (pTunnelFilter->dwFlags &&
  935. !(pTunnelFilter->dwFlags & IPSEC_QM_POLICY_DEFAULT_POLICY)) {
  936. dwError = ERROR_INVALID_PARAMETER;
  937. BAIL_ON_WIN32_ERROR(dwError);
  938. }
  939. //
  940. // No need to call ApplyMulticastFilterValidation as bCreateMirror
  941. // is always false for a tunnel filter.
  942. //
  943. error:
  944. return (dwError);
  945. }
  946. DWORD
  947. ValidateTnFilterTemplate(
  948. PTUNNEL_FILTER pTnFilter
  949. )
  950. {
  951. DWORD dwError = 0;
  952. BOOL bConflicts = FALSE;
  953. if (!pTnFilter) {
  954. dwError = ERROR_INVALID_PARAMETER;
  955. BAIL_ON_WIN32_ERROR(dwError);
  956. }
  957. dwError = VerifyAddresses(&(pTnFilter->SrcAddr), TRUE, FALSE);
  958. BAIL_ON_WIN32_ERROR(dwError);
  959. dwError = VerifyAddresses(&(pTnFilter->DesAddr), TRUE, TRUE);
  960. BAIL_ON_WIN32_ERROR(dwError);
  961. bConflicts = HtoNAddressesConflict(
  962. pTnFilter->SrcAddr,
  963. pTnFilter->DesAddr
  964. );
  965. if (bConflicts) {
  966. dwError = ERROR_INVALID_PARAMETER;
  967. BAIL_ON_WIN32_ERROR(dwError);
  968. }
  969. dwError = ValidateAddr(&(pTnFilter->SrcTunnelAddr));
  970. BAIL_ON_WIN32_ERROR(dwError);
  971. dwError = VerifyAddresses(&(pTnFilter->DesTunnelAddr), TRUE, FALSE);
  972. BAIL_ON_WIN32_ERROR(dwError);
  973. dwError = VerifyProtocols(pTnFilter->Protocol);
  974. BAIL_ON_WIN32_ERROR(dwError);
  975. dwError = VerifyPortsForProtocol(
  976. pTnFilter->SrcPort,
  977. pTnFilter->Protocol
  978. );
  979. BAIL_ON_WIN32_ERROR(dwError);
  980. dwError = VerifyPortsForProtocol(
  981. pTnFilter->DesPort,
  982. pTnFilter->Protocol
  983. );
  984. BAIL_ON_WIN32_ERROR(dwError);
  985. if (pTnFilter->dwDirection) {
  986. if ((pTnFilter->dwDirection != FILTER_DIRECTION_INBOUND) &&
  987. (pTnFilter->dwDirection != FILTER_DIRECTION_OUTBOUND)) {
  988. dwError = ERROR_INVALID_PARAMETER;
  989. BAIL_ON_WIN32_ERROR(dwError);
  990. }
  991. }
  992. error:
  993. return (dwError);
  994. }
  995. DWORD
  996. ApplyMulticastFilterValidation(
  997. ADDR Addr,
  998. BOOL bCreateMirror
  999. )
  1000. {
  1001. DWORD dwError = 0;
  1002. if (((Addr.AddrType == IP_ADDR_UNIQUE) ||
  1003. (Addr.AddrType == IP_ADDR_SUBNET)) &&
  1004. (IN_CLASSD(ntohl(Addr.uIpAddr))) &&
  1005. bCreateMirror) {
  1006. dwError = ERROR_INVALID_PARAMETER;
  1007. BAIL_ON_WIN32_ERROR(dwError);
  1008. }
  1009. error:
  1010. return (dwError);
  1011. }
  1012. DWORD
  1013. ValidateQMFilterAddresses(
  1014. PIPSEC_QM_FILTER pIpsecQMFilter
  1015. )
  1016. {
  1017. DWORD dwError = 0;
  1018. dwError = ValidateAddr(&(pIpsecQMFilter->SrcAddr));
  1019. BAIL_ON_WIN32_ERROR(dwError);
  1020. dwError = ValidateAddr(&(pIpsecQMFilter->DesAddr));
  1021. BAIL_ON_WIN32_ERROR(dwError);
  1022. dwError = ValidateAddr(&(pIpsecQMFilter->MyTunnelEndpt));
  1023. BAIL_ON_WIN32_ERROR(dwError);
  1024. dwError = ValidateAddr(&(pIpsecQMFilter->PeerTunnelEndpt));
  1025. BAIL_ON_WIN32_ERROR(dwError);
  1026. error:
  1027. return (dwError);
  1028. }