Match.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /** @file
  2. The implementation of match policy entry function in IpSecConfig application.
  3. Copyright (c) 2009 - 2016, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include "IpSecConfig.h"
  7. #include "Indexer.h"
  8. #include "Match.h"
  9. /**
  10. Private function to validate a buffer that should be filled with zero.
  11. @param[in] Memory The pointer to the buffer.
  12. @param[in] Size The size of the buffer.
  13. @retval TRUE The memory is filled with zero.
  14. @retval FALSE The memory isn't filled with zero.
  15. **/
  16. BOOLEAN
  17. IsMemoryZero (
  18. IN VOID *Memory,
  19. IN UINTN Size
  20. )
  21. {
  22. UINTN Index;
  23. for (Index = 0; Index < Size; Index++) {
  24. if (*((UINT8 *) Memory + Index) != 0) {
  25. return FALSE;
  26. }
  27. }
  28. return TRUE;
  29. }
  30. /**
  31. Find the matching SPD with Indexer.
  32. @param[in] Selector The pointer to the EFI_IPSEC_SPD_SELECTOR structure.
  33. @param[in] Data The pointer to the EFI_IPSEC_SPD_DATA structure.
  34. @param[in] Indexer The pointer to the SPD_ENTRY_INDEXER structure.
  35. @retval TRUE The matched SPD is found.
  36. @retval FALSE The matched SPD is not found.
  37. **/
  38. BOOLEAN
  39. MatchSpdEntry (
  40. IN EFI_IPSEC_SPD_SELECTOR *Selector,
  41. IN EFI_IPSEC_SPD_DATA *Data,
  42. IN SPD_ENTRY_INDEXER *Indexer
  43. )
  44. {
  45. BOOLEAN Match;
  46. Match = FALSE;
  47. if (!IsMemoryZero (Indexer->Name, MAX_PEERID_LEN)) {
  48. if ((Data->Name != NULL) && (AsciiStrCmp ((CHAR8 *) Indexer->Name, (CHAR8 *) Data->Name) == 0)) {
  49. Match = TRUE;
  50. }
  51. } else {
  52. if (Indexer->Index == 0) {
  53. Match = TRUE;
  54. }
  55. Indexer->Index--;
  56. }
  57. return Match;
  58. }
  59. /**
  60. Find the matching SAD with Indexer.
  61. @param[in] SaId The pointer to the EFI_IPSEC_SA_ID structure.
  62. @param[in] Data The pointer to the EFI_IPSEC_SA_DATA2 structure.
  63. @param[in] Indexer The pointer to the SPD_ENTRY_INDEXER structure.
  64. @retval TRUE The matched SAD is found.
  65. @retval FALSE The matched SAD is not found.
  66. **/
  67. BOOLEAN
  68. MatchSadEntry (
  69. IN EFI_IPSEC_SA_ID *SaId,
  70. IN EFI_IPSEC_SA_DATA2 *Data,
  71. IN SAD_ENTRY_INDEXER *Indexer
  72. )
  73. {
  74. BOOLEAN Match;
  75. Match = FALSE;
  76. if (!IsMemoryZero (&Indexer->SaId, sizeof (EFI_IPSEC_SA_ID))) {
  77. Match = (BOOLEAN) (CompareMem (&Indexer->SaId, SaId, sizeof (EFI_IPSEC_SA_ID)) == 0);
  78. } else {
  79. if (Indexer->Index == 0) {
  80. Match = TRUE;
  81. }
  82. Indexer->Index--;
  83. }
  84. return Match;
  85. }
  86. /**
  87. Find the matching PAD with Indexer.
  88. @param[in] PadId The pointer to the EFI_IPSEC_PAD_ID structure.
  89. @param[in] Data The pointer to the EFI_IPSEC_PAD_DATA structure.
  90. @param[in] Indexer The pointer to the SPD_ENTRY_INDEXER structure.
  91. @retval TRUE The matched PAD is found.
  92. @retval FALSE The matched PAD is not found.
  93. **/
  94. BOOLEAN
  95. MatchPadEntry (
  96. IN EFI_IPSEC_PAD_ID *PadId,
  97. IN EFI_IPSEC_PAD_DATA *Data,
  98. IN PAD_ENTRY_INDEXER *Indexer
  99. )
  100. {
  101. BOOLEAN Match;
  102. Match = FALSE;
  103. if (!IsMemoryZero (&Indexer->PadId, sizeof (EFI_IPSEC_PAD_ID))) {
  104. Match = (BOOLEAN) ((Indexer->PadId.PeerIdValid == PadId->PeerIdValid) &&
  105. ((PadId->PeerIdValid &&
  106. (StrCmp (
  107. (CONST CHAR16 *) Indexer->PadId.Id.PeerId,
  108. (CONST CHAR16 *) PadId->Id.PeerId
  109. ) == 0)) ||
  110. ((!PadId->PeerIdValid) &&
  111. (Indexer->PadId.Id.IpAddress.PrefixLength == PadId->Id.IpAddress.PrefixLength) &&
  112. (CompareMem (
  113. &Indexer->PadId.Id.IpAddress.Address,
  114. &PadId->Id.IpAddress.Address,
  115. sizeof (EFI_IP_ADDRESS)
  116. ) == 0))));
  117. } else {
  118. if (Indexer->Index == 0) {
  119. Match = TRUE;
  120. }
  121. Indexer->Index--;
  122. }
  123. return Match;
  124. }
  125. MATCH_POLICY_ENTRY mMatchPolicyEntry[] = {
  126. (MATCH_POLICY_ENTRY) MatchSpdEntry,
  127. (MATCH_POLICY_ENTRY) MatchSadEntry,
  128. (MATCH_POLICY_ENTRY) MatchPadEntry
  129. };