Dump.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. /** @file
  2. The implementation of dump policy entry function in IpSecConfig application.
  3. Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include "IpSecConfig.h"
  7. #include "Dump.h"
  8. #include "ForEach.h"
  9. #include "Helper.h"
  10. /**
  11. Private function called to get the version infomation from an EFI_IP_ADDRESS_INFO structure.
  12. @param[in] AddressInfo The pointer to the EFI_IP_ADDRESS_INFO structure.
  13. @return the value of version.
  14. **/
  15. UINTN
  16. GetVerFromAddrInfo (
  17. IN EFI_IP_ADDRESS_INFO *AddressInfo
  18. )
  19. {
  20. if((AddressInfo->PrefixLength <= 32) && (AddressInfo->Address.Addr[1] == 0) &&
  21. (AddressInfo->Address.Addr[2] == 0) && (AddressInfo->Address.Addr[3] == 0)) {
  22. return IP_VERSION_4;
  23. } else {
  24. return IP_VERSION_6;
  25. }
  26. }
  27. /**
  28. Private function called to get the version information from a EFI_IP_ADDRESS structure.
  29. @param[in] Address The pointer to the EFI_IP_ADDRESS structure.
  30. @return The value of the version.
  31. **/
  32. UINTN
  33. GetVerFromIpAddr (
  34. IN EFI_IP_ADDRESS *Address
  35. )
  36. {
  37. if ((Address->Addr[1] == 0) && (Address->Addr[2] == 0) && (Address->Addr[3] == 0)) {
  38. return IP_VERSION_4;
  39. } else {
  40. return IP_VERSION_6;
  41. }
  42. }
  43. /**
  44. Private function called to print an ASCII string in unicode char format.
  45. @param[in] Str The pointer to the ASCII string.
  46. @param[in] Length The value of the ASCII string length.
  47. **/
  48. VOID
  49. DumpAsciiString (
  50. IN CHAR8 *Str,
  51. IN UINTN Length
  52. )
  53. {
  54. UINTN Index;
  55. Print (L"\"");
  56. for (Index = 0; Index < Length; Index++) {
  57. Print (L"%c", (CHAR16) Str[Index]);
  58. }
  59. Print (L"\"");
  60. }
  61. /**
  62. Private function called to print a buffer in Hex format.
  63. @param[in] Data The pointer to the buffer.
  64. @param[in] Length The size of the buffer.
  65. **/
  66. VOID
  67. DumpBuf (
  68. IN UINT8 *Data,
  69. IN UINTN Length
  70. )
  71. {
  72. UINTN Index;
  73. for (Index = 0; Index < Length; Index++) {
  74. Print (L"%02x ", Data[Index]);
  75. }
  76. }
  77. /**
  78. Private function called to print EFI_IP_ADDRESS_INFO content.
  79. @param[in] AddressInfo The pointer to the EFI_IP_ADDRESS_INFO structure.
  80. **/
  81. VOID
  82. DumpAddressInfo (
  83. IN EFI_IP_ADDRESS_INFO *AddressInfo
  84. )
  85. {
  86. if (IP_VERSION_4 == GetVerFromAddrInfo (AddressInfo)) {
  87. Print (
  88. L"%d.%d.%d.%d",
  89. (UINTN) AddressInfo->Address.v4.Addr[0],
  90. (UINTN) AddressInfo->Address.v4.Addr[1],
  91. (UINTN) AddressInfo->Address.v4.Addr[2],
  92. (UINTN) AddressInfo->Address.v4.Addr[3]
  93. );
  94. if (AddressInfo->PrefixLength != 32) {
  95. Print (L"/%d", (UINTN) AddressInfo->PrefixLength);
  96. }
  97. }
  98. if (IP_VERSION_6 == GetVerFromAddrInfo (AddressInfo)) {
  99. Print (
  100. L"%x:%x:%x:%x:%x:%x:%x:%x",
  101. (((UINT16) AddressInfo->Address.v6.Addr[0]) << 8) | ((UINT16) AddressInfo->Address.v6.Addr[1]),
  102. (((UINT16) AddressInfo->Address.v6.Addr[2]) << 8) | ((UINT16) AddressInfo->Address.v6.Addr[3]),
  103. (((UINT16) AddressInfo->Address.v6.Addr[4]) << 8) | ((UINT16) AddressInfo->Address.v6.Addr[5]),
  104. (((UINT16) AddressInfo->Address.v6.Addr[6]) << 8) | ((UINT16) AddressInfo->Address.v6.Addr[7]),
  105. (((UINT16) AddressInfo->Address.v6.Addr[8]) << 8) | ((UINT16) AddressInfo->Address.v6.Addr[9]),
  106. (((UINT16) AddressInfo->Address.v6.Addr[10]) << 8) | ((UINT16) AddressInfo->Address.v6.Addr[11]),
  107. (((UINT16) AddressInfo->Address.v6.Addr[12]) << 8) | ((UINT16) AddressInfo->Address.v6.Addr[13]),
  108. (((UINT16) AddressInfo->Address.v6.Addr[14]) << 8) | ((UINT16) AddressInfo->Address.v6.Addr[15])
  109. );
  110. if (AddressInfo->PrefixLength != 128) {
  111. Print (L"/%d", AddressInfo->PrefixLength);
  112. }
  113. }
  114. }
  115. /**
  116. Private function called to print EFI_IP_ADDRESS content.
  117. @param[in] IpAddress The pointer to the EFI_IP_ADDRESS structure.
  118. **/
  119. VOID
  120. DumpIpAddress (
  121. IN EFI_IP_ADDRESS *IpAddress
  122. )
  123. {
  124. if (IP_VERSION_4 == GetVerFromIpAddr (IpAddress)) {
  125. Print (
  126. L"%d.%d.%d.%d",
  127. (UINTN) IpAddress->v4.Addr[0],
  128. (UINTN) IpAddress->v4.Addr[1],
  129. (UINTN) IpAddress->v4.Addr[2],
  130. (UINTN) IpAddress->v4.Addr[3]
  131. );
  132. }
  133. if (IP_VERSION_6 == GetVerFromIpAddr (IpAddress)) {
  134. Print (
  135. L"%x:%x:%x:%x:%x:%x:%x:%x",
  136. (((UINT16) IpAddress->v6.Addr[0]) << 8) | ((UINT16) IpAddress->v6.Addr[1]),
  137. (((UINT16) IpAddress->v6.Addr[2]) << 8) | ((UINT16) IpAddress->v6.Addr[3]),
  138. (((UINT16) IpAddress->v6.Addr[4]) << 8) | ((UINT16) IpAddress->v6.Addr[5]),
  139. (((UINT16) IpAddress->v6.Addr[6]) << 8) | ((UINT16) IpAddress->v6.Addr[7]),
  140. (((UINT16) IpAddress->v6.Addr[8]) << 8) | ((UINT16) IpAddress->v6.Addr[9]),
  141. (((UINT16) IpAddress->v6.Addr[10]) << 8) | ((UINT16) IpAddress->v6.Addr[11]),
  142. (((UINT16) IpAddress->v6.Addr[12]) << 8) | ((UINT16) IpAddress->v6.Addr[13]),
  143. (((UINT16) IpAddress->v6.Addr[14]) << 8) | ((UINT16) IpAddress->v6.Addr[15])
  144. );
  145. }
  146. }
  147. /**
  148. Private function called to print EFI_IPSEC_SPD_SELECTOR content.
  149. @param[in] Selector The pointer to the EFI_IPSEC_SPD_SELECTOR structure.
  150. **/
  151. VOID
  152. DumpSpdSelector (
  153. IN EFI_IPSEC_SPD_SELECTOR *Selector
  154. )
  155. {
  156. UINT32 Index;
  157. CHAR16 *Str;
  158. for (Index = 0; Index < Selector->LocalAddressCount; Index++) {
  159. if (Index > 0) {
  160. Print (L",");
  161. }
  162. DumpAddressInfo (&Selector->LocalAddress[Index]);
  163. }
  164. if (Index == 0) {
  165. Print (L"localhost");
  166. }
  167. Print (L" -> ");
  168. for (Index = 0; Index < Selector->RemoteAddressCount; Index++) {
  169. if (Index > 0) {
  170. Print (L",");
  171. }
  172. DumpAddressInfo (&Selector->RemoteAddress[Index]);
  173. }
  174. Str = MapIntegerToString (Selector->NextLayerProtocol, mMapIpProtocol);
  175. if (Str != NULL) {
  176. Print (L" %s", Str);
  177. } else {
  178. Print (L" proto:%d", (UINTN) Selector->NextLayerProtocol);
  179. }
  180. if ((Selector->NextLayerProtocol == EFI_IP4_PROTO_TCP) || (Selector->NextLayerProtocol == EFI_IP4_PROTO_UDP)) {
  181. Print (L" port:");
  182. if (Selector->LocalPort != EFI_IPSEC_ANY_PORT) {
  183. Print (L"%d", Selector->LocalPort);
  184. if (Selector->LocalPortRange != 0) {
  185. Print (L"~%d", (UINTN) Selector->LocalPort + Selector->LocalPortRange);
  186. }
  187. } else {
  188. Print (L"any");
  189. }
  190. Print (L" -> ");
  191. if (Selector->RemotePort != EFI_IPSEC_ANY_PORT) {
  192. Print (L"%d", Selector->RemotePort);
  193. if (Selector->RemotePortRange != 0) {
  194. Print (L"~%d", (UINTN) Selector->RemotePort + Selector->RemotePortRange);
  195. }
  196. } else {
  197. Print (L"any");
  198. }
  199. } else if (Selector->NextLayerProtocol == EFI_IP4_PROTO_ICMP) {
  200. Print (L" class/code:");
  201. if (Selector->LocalPort != 0) {
  202. Print (L"%d", (UINTN) (UINT8) Selector->LocalPort);
  203. } else {
  204. Print (L"any");
  205. }
  206. Print (L"/");
  207. if (Selector->RemotePort != 0) {
  208. Print (L"%d", (UINTN) (UINT8) Selector->RemotePort);
  209. } else {
  210. Print (L"any");
  211. }
  212. }
  213. }
  214. /**
  215. Print EFI_IPSEC_SPD_SELECTOR and EFI_IPSEC_SPD_DATA content.
  216. @param[in] Selector The pointer to the EFI_IPSEC_SPD_SELECTOR structure.
  217. @param[in] Data The pointer to the EFI_IPSEC_SPD_DATA structure.
  218. @param[in] EntryIndex The pointer to the Index in SPD Database.
  219. @retval EFI_SUCCESS Dump SPD information successfully.
  220. **/
  221. EFI_STATUS
  222. DumpSpdEntry (
  223. IN EFI_IPSEC_SPD_SELECTOR *Selector,
  224. IN EFI_IPSEC_SPD_DATA *Data,
  225. IN UINTN *EntryIndex
  226. )
  227. {
  228. BOOLEAN HasPre;
  229. CHAR16 DataName[128];
  230. CHAR16 *String1;
  231. CHAR16 *String2;
  232. CHAR16 *String3;
  233. UINT8 Index;
  234. Print (L"%d.", (*EntryIndex)++);
  235. //
  236. // xxx.xxx.xxx.xxx/yy -> xxx.xxx.xxx.xx/yy proto:23 port:100~300 -> 300~400
  237. // Protect PF:0x34323423 Name:First Entry
  238. // ext-sequence sequence-overflow fragcheck life:[B0,S1024,H3600]
  239. // ESP algo1 algo2 Tunnel [xxx.xxx.xxx.xxx xxx.xxx.xxx.xxx set]
  240. //
  241. DumpSpdSelector (Selector);
  242. Print (L"\n ");
  243. Print (L"%s ", MapIntegerToString (Data->Action, mMapIpSecAction));
  244. Print (L"PF:%08x ", Data->PackageFlag);
  245. Index = 0;
  246. while (Data->Name[Index] != 0) {
  247. DataName[Index] = (CHAR16) Data->Name[Index];
  248. Index++;
  249. ASSERT (Index < 128);
  250. }
  251. DataName[Index] = L'\0';
  252. Print (L"Name:%s", DataName);
  253. if (Data->Action == EfiIPsecActionProtect) {
  254. Print (L"\n ");
  255. if (Data->ProcessingPolicy->ExtSeqNum) {
  256. Print (L"ext-sequence ");
  257. }
  258. if (Data->ProcessingPolicy->SeqOverflow) {
  259. Print (L"sequence-overflow ");
  260. }
  261. if (Data->ProcessingPolicy->FragCheck) {
  262. Print (L"fragment-check ");
  263. }
  264. HasPre = FALSE;
  265. if (Data->ProcessingPolicy->SaLifetime.ByteCount != 0) {
  266. Print (HasPre ? L"," : L"life:[");
  267. Print (L"%lxB", Data->ProcessingPolicy->SaLifetime.ByteCount);
  268. HasPre = TRUE;
  269. }
  270. if (Data->ProcessingPolicy->SaLifetime.SoftLifetime != 0) {
  271. Print (HasPre ? L"," : L"life:[");
  272. Print (L"%lxs", Data->ProcessingPolicy->SaLifetime.SoftLifetime);
  273. HasPre = TRUE;
  274. }
  275. if (Data->ProcessingPolicy->SaLifetime.HardLifetime != 0) {
  276. Print (HasPre ? L"," : L"life:[");
  277. Print (L"%lxS", Data->ProcessingPolicy->SaLifetime.HardLifetime);
  278. HasPre = TRUE;
  279. }
  280. if (HasPre) {
  281. Print (L"]");
  282. }
  283. if (HasPre || Data->ProcessingPolicy->ExtSeqNum ||
  284. Data->ProcessingPolicy->SeqOverflow || Data->ProcessingPolicy->FragCheck) {
  285. Print (L"\n ");
  286. }
  287. String1 = MapIntegerToString (Data->ProcessingPolicy->Proto, mMapIpSecProtocol);
  288. String2 = MapIntegerToString (Data->ProcessingPolicy->AuthAlgoId, mMapAuthAlgo);
  289. String3 = MapIntegerToString (Data->ProcessingPolicy->EncAlgoId, mMapEncAlgo);
  290. Print (
  291. L"%s Auth:%s Encrypt:%s ",
  292. String1,
  293. String2,
  294. String3
  295. );
  296. Print (L"%s ", MapIntegerToString (Data->ProcessingPolicy->Mode, mMapIpSecMode));
  297. if (Data->ProcessingPolicy->Mode == EfiIPsecTunnel) {
  298. Print (L"[");
  299. DumpIpAddress (&Data->ProcessingPolicy->TunnelOption->LocalTunnelAddress);
  300. Print (L" -> ");
  301. DumpIpAddress (&Data->ProcessingPolicy->TunnelOption->RemoteTunnelAddress);
  302. Print (L" %s]", MapIntegerToString (Data->ProcessingPolicy->TunnelOption->DF, mMapDfOption));
  303. }
  304. }
  305. Print (L"\n");
  306. return EFI_SUCCESS;
  307. }
  308. /**
  309. Print EFI_IPSEC_SA_ID and EFI_IPSEC_SA_DATA2 content.
  310. @param[in] SaId The pointer to the EFI_IPSEC_SA_ID structure.
  311. @param[in] Data The pointer to the EFI_IPSEC_SA_DATA2 structure.
  312. @param[in] EntryIndex The pointer to the Index in the SAD Database.
  313. @retval EFI_SUCCESS Dump SAD information successfully.
  314. **/
  315. EFI_STATUS
  316. DumpSadEntry (
  317. IN EFI_IPSEC_SA_ID *SaId,
  318. IN EFI_IPSEC_SA_DATA2 *Data,
  319. IN UINTN *EntryIndex
  320. )
  321. {
  322. BOOLEAN HasPre;
  323. CHAR16 *AuthAlgoStr;
  324. CHAR16 *EncAlgoStr;
  325. AuthAlgoStr = NULL;
  326. EncAlgoStr = NULL;
  327. //
  328. // SPI:1234 ESP Destination:xxx.xxx.xxx.xxx
  329. // Mode:Transport SeqNum:134 AntiReplayWin:64 life:[0B,1023s,3400S] PathMTU:34
  330. // Auth:xxxx/password Encrypt:yyyy/password
  331. // xxx.xxx.xxx.xxx/yy -> xxx.xxx.xxx.xx/yy proto:23 port:100~300 -> 300~400
  332. //
  333. Print (L"%d.", (*EntryIndex)++);
  334. Print (L"0x%x %s ", (UINTN) SaId->Spi, MapIntegerToString (SaId->Proto, mMapIpSecProtocol));
  335. if (Data->Mode == EfiIPsecTunnel) {
  336. Print (L"TunnelSourceAddress:");
  337. DumpIpAddress (&Data->TunnelSourceAddress);
  338. Print (L"\n");
  339. Print (L" TunnelDestination:");
  340. DumpIpAddress (&Data->TunnelDestinationAddress);
  341. Print (L"\n");
  342. }
  343. Print (
  344. L" Mode:%s SeqNum:%lx AntiReplayWin:%d ",
  345. MapIntegerToString (Data->Mode, mMapIpSecMode),
  346. Data->SNCount,
  347. (UINTN) Data->AntiReplayWindows
  348. );
  349. HasPre = FALSE;
  350. if (Data->SaLifetime.ByteCount != 0) {
  351. Print (HasPre ? L"," : L"life:[");
  352. Print (L"%lxB", Data->SaLifetime.ByteCount);
  353. HasPre = TRUE;
  354. }
  355. if (Data->SaLifetime.SoftLifetime != 0) {
  356. Print (HasPre ? L"," : L"life:[");
  357. Print (L"%lxs", Data->SaLifetime.SoftLifetime);
  358. HasPre = TRUE;
  359. }
  360. if (Data->SaLifetime.HardLifetime != 0) {
  361. Print (HasPre ? L"," : L"life:[");
  362. Print (L"%lxS", Data->SaLifetime.HardLifetime);
  363. HasPre = TRUE;
  364. }
  365. if (HasPre) {
  366. Print (L"] ");
  367. }
  368. Print (L"PathMTU:%d\n", (UINTN) Data->PathMTU);
  369. if (SaId->Proto == EfiIPsecAH) {
  370. Print (
  371. L" Auth:%s/%s\n",
  372. MapIntegerToString (Data->AlgoInfo.AhAlgoInfo.AuthAlgoId, mMapAuthAlgo),
  373. Data->AlgoInfo.AhAlgoInfo.AuthKey
  374. );
  375. } else {
  376. AuthAlgoStr = MapIntegerToString (Data->AlgoInfo.EspAlgoInfo.AuthAlgoId, mMapAuthAlgo);
  377. EncAlgoStr = MapIntegerToString (Data->AlgoInfo.EspAlgoInfo.EncAlgoId, mMapEncAlgo);
  378. if (Data->ManualSet) {
  379. //
  380. // if the SAD is set manually the key is a Ascii string in most of time.
  381. // Print the Key in Ascii string format.
  382. //
  383. Print (L" Auth:%s/",AuthAlgoStr);
  384. DumpAsciiString (
  385. Data->AlgoInfo.EspAlgoInfo.AuthKey,
  386. Data->AlgoInfo.EspAlgoInfo.AuthKeyLength
  387. );
  388. Print (L"\n Encrypt:%s/",EncAlgoStr);
  389. DumpAsciiString (
  390. Data->AlgoInfo.EspAlgoInfo.EncKey,
  391. Data->AlgoInfo.EspAlgoInfo.EncKeyLength
  392. );
  393. } else {
  394. //
  395. // if the SAD is created by IKE, the key is a set of hex value in buffer.
  396. // Print the Key in Hex format.
  397. //
  398. Print (L" Auth:%s/",AuthAlgoStr);
  399. DumpBuf ((UINT8 *)(Data->AlgoInfo.EspAlgoInfo.AuthKey), Data->AlgoInfo.EspAlgoInfo.AuthKeyLength);
  400. Print (L"\n Encrypt:%s/",EncAlgoStr);
  401. DumpBuf ((UINT8 *)(Data->AlgoInfo.EspAlgoInfo.EncKey), Data->AlgoInfo.EspAlgoInfo.EncKeyLength);
  402. }
  403. }
  404. Print (L"\n");
  405. if (Data->SpdSelector != NULL) {
  406. Print (L" ");
  407. DumpSpdSelector (Data->SpdSelector);
  408. Print (L"\n");
  409. }
  410. return EFI_SUCCESS;
  411. }
  412. /**
  413. Print EFI_IPSEC_PAD_ID and EFI_IPSEC_PAD_DATA content.
  414. @param[in] PadId The pointer to the EFI_IPSEC_PAD_ID structure.
  415. @param[in] Data The pointer to the EFI_IPSEC_PAD_DATA structure.
  416. @param[in] EntryIndex The pointer to the Index in the PAD Database.
  417. @retval EFI_SUCCESS Dump PAD information successfully.
  418. **/
  419. EFI_STATUS
  420. DumpPadEntry (
  421. IN EFI_IPSEC_PAD_ID *PadId,
  422. IN EFI_IPSEC_PAD_DATA *Data,
  423. IN UINTN *EntryIndex
  424. )
  425. {
  426. CHAR16 *String1;
  427. CHAR16 *String2;
  428. //
  429. // ADDR:10.23.17.34/15
  430. // IDEv1 PreSharedSecret IKE-ID
  431. // password
  432. //
  433. Print (L"%d.", (*EntryIndex)++);
  434. if (PadId->PeerIdValid) {
  435. Print (L"ID:%s", PadId->Id.PeerId);
  436. } else {
  437. Print (L"ADDR:");
  438. DumpAddressInfo (&PadId->Id.IpAddress);
  439. }
  440. Print (L"\n");
  441. String1 = MapIntegerToString (Data->AuthProtocol, mMapAuthProto);
  442. String2 = MapIntegerToString (Data->AuthMethod, mMapAuthMethod);
  443. Print (
  444. L" %s %s",
  445. String1,
  446. String2
  447. );
  448. if (Data->IkeIdFlag) {
  449. Print (L"IKE-ID");
  450. }
  451. Print (L"\n");
  452. if (Data->AuthData != NULL) {
  453. DumpAsciiString (Data->AuthData, Data->AuthDataSize);
  454. Print (L"\n");
  455. }
  456. if (Data->RevocationData != NULL) {
  457. Print (L" %s\n", Data->RevocationData);
  458. }
  459. return EFI_SUCCESS;
  460. }
  461. VISIT_POLICY_ENTRY mDumpPolicyEntry[] = {
  462. (VISIT_POLICY_ENTRY) DumpSpdEntry,
  463. (VISIT_POLICY_ENTRY) DumpSadEntry,
  464. (VISIT_POLICY_ENTRY) DumpPadEntry
  465. };
  466. /**
  467. Print all entry information in the database according to datatype.
  468. @param[in] DataType The value of EFI_IPSEC_CONFIG_DATA_TYPE.
  469. @param[in] ParamPackage The pointer to the ParamPackage list.
  470. @retval EFI_SUCCESS Dump all information successfully.
  471. @retval Others Some mistaken case.
  472. **/
  473. EFI_STATUS
  474. ListPolicyEntry (
  475. IN EFI_IPSEC_CONFIG_DATA_TYPE DataType,
  476. IN LIST_ENTRY *ParamPackage
  477. )
  478. {
  479. UINTN EntryIndex;
  480. EntryIndex = 0;
  481. return ForeachPolicyEntry (DataType, mDumpPolicyEntry[DataType], &EntryIndex);
  482. }