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.

622 lines
13 KiB

  1. #include"precomp.h"
  2. DWORD
  3. VerifyAddresses(
  4. PADDR pAddr,
  5. BOOL bAcceptMe,
  6. BOOL bIsDesAddr
  7. )
  8. {
  9. DWORD dwError = 0;
  10. BOOL bIsValid = FALSE;
  11. switch (pAddr->AddrType) {
  12. case IP_ADDR_UNIQUE:
  13. bIsValid = bIsValidIPAddress(
  14. ntohl(pAddr->uIpAddr),
  15. bAcceptMe,
  16. bIsDesAddr
  17. );
  18. if (!bIsValid) {
  19. dwError = ERROR_INVALID_PARAMETER;
  20. BAIL_ON_WIN32_ERROR(dwError);
  21. }
  22. if (pAddr->pgInterfaceID) {
  23. dwError = ERROR_INVALID_PARAMETER;
  24. BAIL_ON_WIN32_ERROR(dwError);
  25. }
  26. break;
  27. case IP_ADDR_SUBNET:
  28. dwError = VerifySubNetAddress(
  29. ntohl(pAddr->uIpAddr),
  30. ntohl(pAddr->uSubNetMask),
  31. bIsDesAddr
  32. );
  33. BAIL_ON_WIN32_ERROR(dwError);
  34. if (pAddr->pgInterfaceID) {
  35. dwError = ERROR_INVALID_PARAMETER;
  36. BAIL_ON_WIN32_ERROR(dwError);
  37. }
  38. break;
  39. case IP_ADDR_INTERFACE:
  40. if (pAddr->uIpAddr) {
  41. dwError = ERROR_INVALID_PARAMETER;
  42. BAIL_ON_WIN32_ERROR(dwError);
  43. }
  44. if (!(pAddr->pgInterfaceID)) {
  45. dwError = ERROR_INVALID_PARAMETER;
  46. BAIL_ON_WIN32_ERROR(dwError);
  47. }
  48. break;
  49. default:
  50. if (!IsSpecialServ(pAddr->AddrType)) {
  51. dwError = ERROR_INVALID_PARAMETER;
  52. BAIL_ON_WIN32_ERROR(dwError);
  53. }
  54. break;
  55. }
  56. error:
  57. return (dwError);
  58. }
  59. BOOL
  60. EqualAddresses(
  61. IN ADDR_V4 OldAddr,
  62. IN ADDR_V4 NewAddr
  63. )
  64. {
  65. BOOL bMatches = FALSE;
  66. if (OldAddr.AddrType == NewAddr.AddrType) {
  67. switch(OldAddr.AddrType) {
  68. case IP_ADDR_UNIQUE:
  69. if (OldAddr.uIpAddr == NewAddr.uIpAddr) {
  70. bMatches = TRUE;
  71. }
  72. break;
  73. case IP_ADDR_SUBNET:
  74. if ((OldAddr.uIpAddr == NewAddr.uIpAddr) &&
  75. (OldAddr.uSubNetMask == NewAddr.uSubNetMask)) {
  76. bMatches = TRUE;
  77. }
  78. break;
  79. case IP_ADDR_INTERFACE:
  80. if (!memcmp(
  81. &OldAddr.gInterfaceID,
  82. &NewAddr.gInterfaceID,
  83. sizeof(GUID)) &&
  84. (OldAddr.uIpAddr == NewAddr.uIpAddr)) {
  85. bMatches = TRUE;
  86. }
  87. break;
  88. }
  89. }
  90. return (bMatches);
  91. }
  92. VOID
  93. CopyAddresses(
  94. IN ADDR_V4 InAddr,
  95. OUT PADDR_V4 pOutAddr
  96. )
  97. {
  98. pOutAddr->AddrType = InAddr.AddrType;
  99. switch (InAddr.AddrType) {
  100. case IP_ADDR_UNIQUE:
  101. pOutAddr->uIpAddr = InAddr.uIpAddr;
  102. pOutAddr->uSubNetMask = IP_ADDRESS_MASK_NONE;
  103. memset(&pOutAddr->gInterfaceID, 0, sizeof(GUID));
  104. break;
  105. case IP_ADDR_SUBNET:
  106. pOutAddr->uIpAddr = InAddr.uIpAddr;
  107. pOutAddr->uSubNetMask = InAddr.uSubNetMask;
  108. memset(&pOutAddr->gInterfaceID, 0, sizeof(GUID));
  109. break;
  110. case IP_ADDR_INTERFACE:
  111. pOutAddr->uIpAddr = InAddr.uIpAddr;
  112. pOutAddr->uSubNetMask = IP_ADDRESS_MASK_NONE;
  113. memcpy(
  114. &pOutAddr->gInterfaceID,
  115. &InAddr.gInterfaceID,
  116. sizeof(GUID)
  117. );
  118. break;
  119. default:
  120. if (IsSpecialServ(InAddr.AddrType)) {
  121. pOutAddr->uIpAddr = InAddr.uIpAddr;
  122. pOutAddr->uSubNetMask = IP_ADDRESS_MASK_NONE;
  123. memset(&pOutAddr->gInterfaceID, 0, sizeof(GUID));
  124. }
  125. break;
  126. }
  127. }
  128. BOOL
  129. AddressesConflict(
  130. ADDR SrcAddr,
  131. ADDR DesAddr
  132. )
  133. {
  134. if ((SrcAddr.AddrType == IP_ADDR_UNIQUE) &&
  135. (DesAddr.AddrType == IP_ADDR_UNIQUE)) {
  136. if (SrcAddr.uIpAddr == DesAddr.uIpAddr) {
  137. return (TRUE);
  138. }
  139. }
  140. if ((SrcAddr.AddrType == IP_ADDR_INTERFACE) &&
  141. (DesAddr.AddrType == IP_ADDR_INTERFACE)) {
  142. return (TRUE);
  143. }
  144. if (IsSpecialServ(SrcAddr.AddrType) &&
  145. (SrcAddr.AddrType == DesAddr.AddrType)) {
  146. return (TRUE);
  147. }
  148. return (FALSE);
  149. }
  150. VOID
  151. FreeAddresses(
  152. ADDR Addr
  153. )
  154. {
  155. switch (Addr.AddrType) {
  156. case (IP_ADDR_UNIQUE):
  157. case (IP_ADDR_SUBNET):
  158. case (IP_ADDR_INTERFACE):
  159. break;
  160. }
  161. }
  162. DWORD
  163. VerifySubNetAddress(
  164. ULONG uSubNetAddr,
  165. ULONG uSubNetMask,
  166. BOOL bIsDesAddr
  167. )
  168. {
  169. DWORD dwError = 0;
  170. BOOL bIsValid = FALSE;
  171. if (uSubNetAddr == SUBNET_ADDRESS_ANY) {
  172. if (uSubNetMask != SUBNET_MASK_ANY) {
  173. dwError = ERROR_INVALID_PARAMETER;
  174. BAIL_ON_WIN32_ERROR(dwError);
  175. }
  176. }
  177. else {
  178. bIsValid = bIsValidSubnet(
  179. uSubNetAddr,
  180. uSubNetMask,
  181. bIsDesAddr
  182. );
  183. if (!bIsValid) {
  184. dwError = ERROR_INVALID_PARAMETER;
  185. BAIL_ON_WIN32_ERROR(dwError);
  186. }
  187. }
  188. error:
  189. return (dwError);
  190. }
  191. BOOL
  192. bIsValidIPMask(
  193. ULONG uMask
  194. )
  195. {
  196. BOOL bValidMask = FALSE;
  197. ULONG uTestMask = 0;
  198. //
  199. // Mask must be contiguous bits.
  200. //
  201. for (uTestMask = 0xFFFFFFFF; uTestMask; uTestMask <<= 1) {
  202. if (uTestMask == uMask) {
  203. bValidMask = TRUE;
  204. break;
  205. }
  206. }
  207. return (bValidMask);
  208. }
  209. BOOL
  210. bIsValidIPAddress(
  211. ULONG uIpAddr,
  212. BOOL bAcceptMe,
  213. BOOL bIsDesAddr
  214. )
  215. {
  216. ULONG uHostMask = IN_CLASSA_HOST; // Default host mask.
  217. //
  218. // Accept the address if its "me".
  219. //
  220. if (bAcceptMe) {
  221. if (uIpAddr == IP_ADDRESS_ME) {
  222. return TRUE;
  223. }
  224. }
  225. //
  226. // Reject if its a multicast address and is not the
  227. // destination address.
  228. //
  229. if (IN_CLASSD(uIpAddr)) {
  230. if (bIsDesAddr) {
  231. return TRUE;
  232. }
  233. else {
  234. return FALSE;
  235. }
  236. }
  237. //
  238. // Reject if its a Class E address.
  239. //
  240. if (IN_CLASSE(uIpAddr)) {
  241. return FALSE;
  242. }
  243. //
  244. // Reject if the first octet is zero.
  245. //
  246. if (!(IN_CLASSA_NET & uIpAddr)) {
  247. return FALSE;
  248. }
  249. return TRUE;
  250. }
  251. BOOL
  252. bIsValidSubnet(
  253. ULONG uIpAddr,
  254. ULONG uMask,
  255. BOOL bIsDesAddr
  256. )
  257. {
  258. ULONG uHostMask = 0;
  259. //
  260. // Reject if its a multicast address and is not the
  261. // destination address.
  262. //
  263. if (IN_CLASSD(uIpAddr)) {
  264. if (!bIsDesAddr) {
  265. return FALSE;
  266. }
  267. }
  268. //
  269. // Reject if its a Class E address.
  270. //
  271. if (IN_CLASSE(uIpAddr)) {
  272. return FALSE;
  273. }
  274. //
  275. // Reject if the first octet is zero.
  276. //
  277. if (!(IN_CLASSA_NET & uIpAddr)) {
  278. return FALSE;
  279. }
  280. //
  281. // If the mask is invalid then return.
  282. //
  283. if (!bIsValidIPMask(uMask)) {
  284. return FALSE;
  285. }
  286. //
  287. // Use the provided subnet mask to generate the host mask.
  288. //
  289. uHostMask = 0xFFFFFFFF ^ uMask;
  290. //
  291. // Accept address only when the host portion is zero, network
  292. // portion is non-zero and first octet is non-zero.
  293. //
  294. if (!(uHostMask & uIpAddr) &&
  295. (uMask & uIpAddr) &&
  296. (IN_CLASSA_NET & uIpAddr)) {
  297. return TRUE;
  298. }
  299. return FALSE;
  300. }
  301. BOOL
  302. MatchAddresses(
  303. ADDR_V4 AddrToMatch,
  304. ADDR AddrTemplate
  305. )
  306. {
  307. switch (AddrTemplate.AddrType) {
  308. case IP_ADDR_UNIQUE:
  309. if ((AddrToMatch.uIpAddr & AddrToMatch.uSubNetMask) !=
  310. (AddrTemplate.uIpAddr & AddrToMatch.uSubNetMask)) {
  311. return (FALSE);
  312. }
  313. break;
  314. case IP_ADDR_SUBNET:
  315. if ((AddrToMatch.uIpAddr & AddrToMatch.uSubNetMask) !=
  316. ((AddrTemplate.uIpAddr & AddrTemplate.uSubNetMask)
  317. & AddrToMatch.uSubNetMask)) {
  318. return (FALSE);
  319. }
  320. break;
  321. case IP_ADDR_INTERFACE:
  322. if (memcmp(
  323. &AddrToMatch.gInterfaceID,
  324. AddrTemplate.pgInterfaceID,
  325. sizeof(GUID))) {
  326. return (FALSE);
  327. }
  328. break;
  329. default:
  330. if (IsSpecialServ(AddrTemplate.AddrType)
  331. && IsSpecialServ(AddrToMatch.AddrType)) {
  332. return (AddrTemplate.AddrType == AddrToMatch.AddrType);
  333. }
  334. break;
  335. }
  336. return (TRUE);
  337. }
  338. DWORD
  339. ApplyMulticastFilterValidation(
  340. ADDR Addr,
  341. BOOL bCreateMirror
  342. )
  343. {
  344. DWORD dwError = 0;
  345. if (((Addr.AddrType == IP_ADDR_UNIQUE) ||
  346. (Addr.AddrType == IP_ADDR_SUBNET)) &&
  347. (IN_CLASSD(ntohl(Addr.uIpAddr))) &&
  348. bCreateMirror) {
  349. dwError = ERROR_INVALID_PARAMETER;
  350. BAIL_ON_WIN32_ERROR(dwError);
  351. }
  352. error:
  353. return (dwError);
  354. }
  355. BOOL
  356. EqualExtIntAddresses(
  357. IN ADDR_V4 OldAddr,
  358. IN ADDR NewAddr
  359. )
  360. {
  361. BOOL bMatches = FALSE;
  362. if (OldAddr.AddrType == NewAddr.AddrType) {
  363. switch(OldAddr.AddrType) {
  364. case IP_ADDR_UNIQUE:
  365. if (OldAddr.uIpAddr == NewAddr.uIpAddr) {
  366. bMatches = TRUE;
  367. }
  368. break;
  369. case IP_ADDR_SUBNET:
  370. if ((OldAddr.uIpAddr == NewAddr.uIpAddr) &&
  371. (OldAddr.uSubNetMask == NewAddr.uSubNetMask)) {
  372. bMatches = TRUE;
  373. }
  374. break;
  375. case IP_ADDR_INTERFACE:
  376. if (!memcmp(
  377. &OldAddr.gInterfaceID,
  378. NewAddr.pgInterfaceID,
  379. sizeof(GUID)) &&
  380. (OldAddr.uIpAddr == NewAddr.uIpAddr)) {
  381. bMatches = TRUE;
  382. }
  383. break;
  384. default:
  385. if (IsSpecialServ(OldAddr.AddrType)) {
  386. bMatches = TRUE;
  387. }
  388. break;
  389. }
  390. }
  391. return (bMatches);
  392. }
  393. VOID
  394. CopyExtToIntAddresses(
  395. IN ADDR InAddr,
  396. OUT PADDR_V4 pOutAddr
  397. )
  398. {
  399. pOutAddr->AddrType = InAddr.AddrType;
  400. switch (InAddr.AddrType) {
  401. case IP_ADDR_UNIQUE:
  402. pOutAddr->uIpAddr = InAddr.uIpAddr;
  403. pOutAddr->uSubNetMask = IP_ADDRESS_MASK_NONE;
  404. memset(&pOutAddr->gInterfaceID, 0, sizeof(GUID));
  405. break;
  406. case IP_ADDR_SUBNET:
  407. pOutAddr->uIpAddr = InAddr.uIpAddr;
  408. pOutAddr->uSubNetMask = InAddr.uSubNetMask;
  409. memset(&pOutAddr->gInterfaceID, 0, sizeof(GUID));
  410. break;
  411. case IP_ADDR_INTERFACE:
  412. pOutAddr->uIpAddr = InAddr.uIpAddr;
  413. pOutAddr->uSubNetMask = IP_ADDRESS_MASK_NONE;
  414. memcpy(
  415. &pOutAddr->gInterfaceID,
  416. InAddr.pgInterfaceID,
  417. sizeof(GUID)
  418. );
  419. break;
  420. default:
  421. if (IsSpecialServ(InAddr.AddrType)) {
  422. pOutAddr->uIpAddr = InAddr.uIpAddr;
  423. pOutAddr->uSubNetMask = IP_ADDRESS_MASK_NONE;
  424. memset(&pOutAddr->gInterfaceID, 0, sizeof(GUID));
  425. }
  426. break;
  427. }
  428. }
  429. DWORD
  430. CopyIntToExtAddresses(
  431. IN ADDR_V4 InAddr,
  432. OUT PADDR pOutAddr
  433. )
  434. {
  435. DWORD dwError = 0;
  436. pOutAddr->AddrType = InAddr.AddrType;
  437. switch (InAddr.AddrType) {
  438. case IP_ADDR_UNIQUE:
  439. pOutAddr->uIpAddr = InAddr.uIpAddr;
  440. pOutAddr->uSubNetMask = IP_ADDRESS_MASK_NONE;
  441. pOutAddr->pgInterfaceID = NULL;
  442. break;
  443. case IP_ADDR_SUBNET:
  444. pOutAddr->uIpAddr = InAddr.uIpAddr;
  445. pOutAddr->uSubNetMask = InAddr.uSubNetMask;
  446. pOutAddr->pgInterfaceID = NULL;
  447. break;
  448. case IP_ADDR_INTERFACE:
  449. pOutAddr->uIpAddr = InAddr.uIpAddr;
  450. pOutAddr->uSubNetMask = IP_ADDRESS_MASK_NONE;
  451. dwError = SPDApiBufferAllocate(
  452. sizeof(GUID),
  453. &(pOutAddr->pgInterfaceID)
  454. );
  455. BAIL_ON_WIN32_ERROR(dwError);
  456. memcpy(
  457. pOutAddr->pgInterfaceID,
  458. &InAddr.gInterfaceID,
  459. sizeof(GUID)
  460. );
  461. break;
  462. default:
  463. if (IsSpecialServ(InAddr.AddrType)) {
  464. pOutAddr->uIpAddr = InAddr.uIpAddr;
  465. pOutAddr->uSubNetMask = IP_ADDRESS_MASK_NONE;
  466. pOutAddr->pgInterfaceID = NULL;
  467. }
  468. break;
  469. }
  470. error:
  471. return (dwError);
  472. }
  473. DWORD
  474. ValidateAddr(
  475. PADDR pAddr
  476. )
  477. {
  478. DWORD dwError = 0;
  479. __try {
  480. if (pAddr->AddrType == IP_ADDR_INTERFACE) {
  481. if (!(pAddr->pgInterfaceID)) {
  482. dwError = ERROR_INVALID_PARAMETER;
  483. BAIL_ON_WIN32_ERROR(dwError);
  484. }
  485. }
  486. else {
  487. if (pAddr->pgInterfaceID) {
  488. dwError = ERROR_INVALID_PARAMETER;
  489. BAIL_ON_WIN32_ERROR(dwError);
  490. }
  491. }
  492. } __except(EXCEPTION_EXECUTE_HANDLER) {
  493. dwError = ERROR_INVALID_PARAMETER;
  494. }
  495. error:
  496. return (dwError);
  497. }
  498. DWORD
  499. ValidateQMFilterAddresses(
  500. PIPSEC_QM_FILTER pIpsecQMFilter
  501. )
  502. {
  503. DWORD dwError = 0;
  504. dwError = ValidateAddr(&(pIpsecQMFilter->SrcAddr));
  505. BAIL_ON_WIN32_ERROR(dwError);
  506. dwError = ValidateAddr(&(pIpsecQMFilter->DesAddr));
  507. BAIL_ON_WIN32_ERROR(dwError);
  508. dwError = ValidateAddr(&(pIpsecQMFilter->MyTunnelEndpt));
  509. BAIL_ON_WIN32_ERROR(dwError);
  510. dwError = ValidateAddr(&(pIpsecQMFilter->PeerTunnelEndpt));
  511. BAIL_ON_WIN32_ERROR(dwError);
  512. error:
  513. return (dwError);
  514. }