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