-/** @file
- This library provides basic functiosn for UEFI network stack.
-
-Copyright (c) 2005 - 2008, Intel Corporation
-All rights reserved. This program and the accompanying materials
-are licensed and made available under the terms and conditions of the BSD License
-which accompanies this distribution. The full text of the license may be found at
-http://opensource.org/licenses/bsd-license.php
-
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
-
-**/
-
-#ifndef _NET_LIB_H_
-#define _NET_LIB_H_
-
-#include <PiDxe.h>
-#include <Library/BaseMemoryLib.h>
-#include <Library/MemoryAllocationLib.h>
-#include <Protocol/DriverBinding.h>
-#include <Protocol/ComponentName.h>
-#include <Protocol/DriverConfiguration.h>
-#include <Protocol/DriverDiagnostics.h>
-#include <Protocol/Dpc.h>
-
-typedef UINT32 IP4_ADDR;
-typedef UINT32 TCP_SEQNO;
-typedef UINT16 TCP_PORTNO;
-
-enum {
- NET_ETHER_ADDR_LEN = 6,
- NET_IFTYPE_ETHERNET = 0x01,
-
- EFI_IP_PROTO_UDP = 0x11,
- EFI_IP_PROTO_TCP = 0x06,
- EFI_IP_PROTO_ICMP = 0x01,
-
- //
- // The address classfication
- //
- IP4_ADDR_CLASSA = 1,
- IP4_ADDR_CLASSB,
- IP4_ADDR_CLASSC,
- IP4_ADDR_CLASSD,
- IP4_ADDR_CLASSE,
-
- IP4_MASK_NUM = 33,
-};
-
-#pragma pack(1)
-
-//
-// Ethernet head definition
-//
-typedef struct {
- UINT8 DstMac [NET_ETHER_ADDR_LEN];
- UINT8 SrcMac [NET_ETHER_ADDR_LEN];
- UINT16 EtherType;
-} ETHER_HEAD;
-
-
-//
-// The EFI_IP4_HEADER is hard to use because the source and
-// destination address are defined as EFI_IPv4_ADDRESS, which
-// is a structure. Two structures can't be compared or masked
-// directly. This is why there is an internal representation.
-//
-typedef struct {
- UINT8 HeadLen : 4;
- UINT8 Ver : 4;
- UINT8 Tos;
- UINT16 TotalLen;
- UINT16 Id;
- UINT16 Fragment;
- UINT8 Ttl;
- UINT8 Protocol;
- UINT16 Checksum;
- IP4_ADDR Src;
- IP4_ADDR Dst;
-} IP4_HEAD;
-
-
-//
-// ICMP head definition. ICMP message is categoried as either an error
-// message or query message. Two message types have their own head format.
-//
-typedef struct {
- UINT8 Type;
- UINT8 Code;
- UINT16 Checksum;
-} IP4_ICMP_HEAD;
-
-typedef struct {
- IP4_ICMP_HEAD Head;
- UINT32 Fourth; // 4th filed of the head, it depends on Type.
- IP4_HEAD IpHead;
-} IP4_ICMP_ERROR_HEAD;
-
-typedef struct {
- IP4_ICMP_HEAD Head;
- UINT16 Id;
- UINT16 Seq;
-} IP4_ICMP_QUERY_HEAD;
-
-
-//
-// UDP header definition
-//
-typedef struct {
- UINT16 SrcPort;
- UINT16 DstPort;
- UINT16 Length;
- UINT16 Checksum;
-} EFI_UDP4_HEADER;
-
-
-//
-// TCP header definition
-//
-typedef struct {
- TCP_PORTNO SrcPort;
- TCP_PORTNO DstPort;
- TCP_SEQNO Seq;
- TCP_SEQNO Ack;
- UINT8 Res : 4;
- UINT8 HeadLen : 4;
- UINT8 Flag;
- UINT16 Wnd;
- UINT16 Checksum;
- UINT16 Urg;
-} TCP_HEAD;
-
-#pragma pack()
-
-#define NET_MAC_EQUAL(pMac1, pMac2, Len) \
- (CompareMem ((pMac1), (pMac2), Len) == 0)
-
-#define NET_MAC_IS_MULTICAST(Mac, BMac, Len) \
- (((*((UINT8 *) Mac) & 0x01) == 0x01) && (!NET_MAC_EQUAL (Mac, BMac, Len)))
-
-#define NTOHL(x) (UINT32)((((UINT32) (x) & 0xff) << 24) | \
- (((UINT32) (x) & 0xff00) << 8) | \
- (((UINT32) (x) & 0xff0000) >> 8) | \
- (((UINT32) (x) & 0xff000000) >> 24))
-
-#define HTONL(x) NTOHL(x)
-
-#define NTOHS(x) (UINT16)((((UINT16) (x) & 0xff) << 8) | \
- (((UINT16) (x) & 0xff00) >> 8))
-
-#define HTONS(x) NTOHS(x)
-
-//
-// Test the IP's attribute, All the IPs are in host byte order.
-//
-#define IP4_IS_MULTICAST(Ip) (((Ip) & 0xF0000000) == 0xE0000000)
-#define IP4_IS_LOCAL_BROADCAST(Ip) ((Ip) == 0xFFFFFFFF)
-#define IP4_NET_EQUAL(Ip1, Ip2, NetMask) (((Ip1) & (NetMask)) == ((Ip2) & (NetMask)))
-#define IP4_IS_VALID_NETMASK(Ip) (NetGetMaskLength (Ip) != IP4_MASK_NUM)
-
-//
-// Convert the EFI_IP4_ADDRESS to plain UINT32 IP4 address.
-//
-#define EFI_IP4(EfiIpAddr) (*(IP4_ADDR *) ((EfiIpAddr).Addr))
-#define EFI_NTOHL(EfiIp) (NTOHL (EFI_IP4 ((EfiIp))))
-#define EFI_IP4_EQUAL(Ip1, Ip2) (CompareMem ((Ip1), (Ip2), sizeof (EFI_IPv4_ADDRESS)) == 0)
-
-/**\r
- Return the length of the mask. If the mask is invalid,\r
- return the invalid length 33, which is IP4_MASK_NUM.\r
+/** @file\r
+ This library is only intended to be used by UEFI network stack modules.\r
+ It provides basic functions for the UEFI network stack.\r
+\r
+Copyright (c) 2005 - 2017, Intel Corporation. All rights reserved.<BR>\r
+This program and the accompanying materials\r
+are licensed and made available under the terms and conditions of the BSD License\r
+which accompanies this distribution. The full text of the license may be found at<BR>\r
+http://opensource.org/licenses/bsd-license.php\r
+\r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\r
+**/\r
+\r
+#ifndef _NET_LIB_H_\r
+#define _NET_LIB_H_\r
+\r
+#include <Protocol/Ip6.h>\r
+\r
+#include <Library/BaseLib.h>\r
+#include <Library/BaseMemoryLib.h>\r
+\r
+typedef UINT32 IP4_ADDR;\r
+typedef UINT32 TCP_SEQNO;\r
+typedef UINT16 TCP_PORTNO;\r
+\r
+\r
+#define NET_ETHER_ADDR_LEN 6\r
+#define NET_IFTYPE_ETHERNET 0x01\r
+\r
+#define NET_VLAN_TAG_LEN 4\r
+#define ETHER_TYPE_VLAN 0x8100\r
+\r
+#define EFI_IP_PROTO_UDP 0x11\r
+#define EFI_IP_PROTO_TCP 0x06\r
+#define EFI_IP_PROTO_ICMP 0x01\r
+#define IP4_PROTO_IGMP 0x02\r
+#define IP6_ICMP 58\r
+#define DNS_MAX_NAME_SIZE 255\r
+#define DNS_MAX_MESSAGE_SIZE 512\r
+\r
+//\r
+// The address classification\r
+//\r
+#define IP4_ADDR_CLASSA 1 // Deprecated\r
+#define IP4_ADDR_CLASSB 2 // Deprecated\r
+#define IP4_ADDR_CLASSC 3 // Deprecated\r
+#define IP4_ADDR_CLASSD 4\r
+#define IP4_ADDR_CLASSE 5\r
+\r
+#define IP4_MASK_NUM 33\r
+#define IP6_PREFIX_NUM 129\r
+\r
+#define IP4_MASK_MAX 32 \r
+#define IP6_PREFIX_MAX 128\r
+\r
+#define IP6_HOP_BY_HOP 0\r
+#define IP6_DESTINATION 60\r
+#define IP6_ROUTING 43\r
+#define IP6_FRAGMENT 44\r
+#define IP6_AH 51\r
+#define IP6_ESP 50\r
+#define IP6_NO_NEXT_HEADER 59\r
+\r
+#define IP_VERSION_4 4\r
+#define IP_VERSION_6 6\r
+\r
+#define IP6_PREFIX_LENGTH 64\r
+\r
+//\r
+// DNS QTYPE values\r
+//\r
+#define DNS_TYPE_A 1\r
+#define DNS_TYPE_NS 2\r
+#define DNS_TYPE_CNAME 5\r
+#define DNS_TYPE_SOA 6\r
+#define DNS_TYPE_WKS 11\r
+#define DNS_TYPE_PTR 12\r
+#define DNS_TYPE_HINFO 13\r
+#define DNS_TYPE_MINFO 14\r
+#define DNS_TYPE_MX 15\r
+#define DNS_TYPE_TXT 16\r
+#define DNS_TYPE_AAAA 28\r
+#define DNS_TYPE_SRV_RR 33\r
+#define DNS_TYPE_AXFR 252\r
+#define DNS_TYPE_MAILB 253\r
+#define DNS_TYPE_ANY 255\r
+\r
+//\r
+// DNS QCLASS values\r
+//\r
+#define DNS_CLASS_INET 1\r
+#define DNS_CLASS_CH 3\r
+#define DNS_CLASS_HS 4\r
+#define DNS_CLASS_ANY 255\r
+\r
+#pragma pack(1)\r
+\r
+//\r
+// Ethernet head definition\r
+//\r
+typedef struct {\r
+ UINT8 DstMac [NET_ETHER_ADDR_LEN];\r
+ UINT8 SrcMac [NET_ETHER_ADDR_LEN];\r
+ UINT16 EtherType;\r
+} ETHER_HEAD;\r
+\r
+//\r
+// 802.1Q VLAN Tag Control Information\r
+//\r
+typedef union {\r
+ struct {\r
+ UINT16 Vid : 12; // Unique VLAN identifier (0 to 4094)\r
+ UINT16 Cfi : 1; // Canonical Format Indicator\r
+ UINT16 Priority : 3; // 802.1Q priority level (0 to 7)\r
+ } Bits;\r
+ UINT16 Uint16;\r
+} VLAN_TCI;\r
+\r
+#define VLAN_TCI_CFI_CANONICAL_MAC 0\r
+#define VLAN_TCI_CFI_NON_CANONICAL_MAC 1\r
+\r
+//\r
+// The EFI_IP4_HEADER is hard to use because the source and\r
+// destination address are defined as EFI_IPv4_ADDRESS, which\r
+// is a structure. Two structures can't be compared or masked\r
+// directly. This is why there is an internal representation.\r
+//\r
+typedef struct {\r
+ UINT8 HeadLen : 4;\r
+ UINT8 Ver : 4;\r
+ UINT8 Tos;\r
+ UINT16 TotalLen;\r
+ UINT16 Id;\r
+ UINT16 Fragment;\r
+ UINT8 Ttl;\r
+ UINT8 Protocol;\r
+ UINT16 Checksum;\r
+ IP4_ADDR Src;\r
+ IP4_ADDR Dst;\r
+} IP4_HEAD;\r
+\r
+\r
+//\r
+// ICMP head definition. Each ICMP message is categorized as either an error\r
+// message or query message. Two message types have their own head format.\r
+//\r
+typedef struct {\r
+ UINT8 Type;\r
+ UINT8 Code;\r
+ UINT16 Checksum;\r
+} IP4_ICMP_HEAD;\r
+\r
+typedef struct {\r
+ IP4_ICMP_HEAD Head;\r
+ UINT32 Fourth; // 4th filed of the head, it depends on Type.\r
+ IP4_HEAD IpHead;\r
+} IP4_ICMP_ERROR_HEAD;\r
+\r
+typedef struct {\r
+ IP4_ICMP_HEAD Head;\r
+ UINT16 Id;\r
+ UINT16 Seq;\r
+} IP4_ICMP_QUERY_HEAD;\r
+\r
+typedef struct {\r
+ UINT8 Type;\r
+ UINT8 Code;\r
+ UINT16 Checksum;\r
+} IP6_ICMP_HEAD;\r
+\r
+typedef struct {\r
+ IP6_ICMP_HEAD Head;\r
+ UINT32 Fourth;\r
+ EFI_IP6_HEADER IpHead;\r
+} IP6_ICMP_ERROR_HEAD;\r
+\r
+typedef struct {\r
+ IP6_ICMP_HEAD Head;\r
+ UINT32 Fourth;\r
+} IP6_ICMP_INFORMATION_HEAD;\r
+\r
+//\r
+// UDP header definition\r
+//\r
+typedef struct {\r
+ UINT16 SrcPort;\r
+ UINT16 DstPort;\r
+ UINT16 Length;\r
+ UINT16 Checksum;\r
+} EFI_UDP_HEADER;\r
+\r
+//\r
+// TCP header definition\r
+//\r
+typedef struct {\r
+ TCP_PORTNO SrcPort;\r
+ TCP_PORTNO DstPort;\r
+ TCP_SEQNO Seq;\r
+ TCP_SEQNO Ack;\r
+ UINT8 Res : 4;\r
+ UINT8 HeadLen : 4;\r
+ UINT8 Flag;\r
+ UINT16 Wnd;\r
+ UINT16 Checksum;\r
+ UINT16 Urg;\r
+} TCP_HEAD;\r
+\r
+#pragma pack()\r
+\r
+#define NET_MAC_EQUAL(pMac1, pMac2, Len) \\r
+ (CompareMem ((pMac1), (pMac2), Len) == 0)\r
+\r
+#define NET_MAC_IS_MULTICAST(Mac, BMac, Len) \\r
+ (((*((UINT8 *) Mac) & 0x01) == 0x01) && (!NET_MAC_EQUAL (Mac, BMac, Len)))\r
+\r
+#define NTOHL(x) SwapBytes32 (x)\r
+\r
+#define HTONL(x) NTOHL(x)\r
+\r
+#define NTOHS(x) SwapBytes16 (x)\r
+\r
+#define HTONS(x) NTOHS(x)\r
+#define NTOHLL(x) SwapBytes64 (x)\r
+#define HTONLL(x) NTOHLL(x)\r
+#define NTOHLLL(x) Ip6Swap128 (x)\r
+#define HTONLLL(x) NTOHLLL(x)\r
+\r
+//\r
+// Test the IP's attribute, All the IPs are in host byte order.\r
+//\r
+#define IP4_IS_MULTICAST(Ip) (((Ip) & 0xF0000000) == 0xE0000000)\r
+#define IP4_IS_UNSPECIFIED(Ip) ((Ip) == 0)\r
+#define IP4_IS_LOCAL_BROADCAST(Ip) ((Ip) == 0xFFFFFFFF)\r
+#define IP4_NET_EQUAL(Ip1, Ip2, NetMask) (((Ip1) & (NetMask)) == ((Ip2) & (NetMask)))\r
+#define IP4_IS_VALID_NETMASK(Ip) (NetGetMaskLength (Ip) != (IP4_MASK_MAX + 1))\r
+\r
+#define IP6_IS_MULTICAST(Ip6) (((Ip6)->Addr[0]) == 0xFF)\r
+\r
+//\r
+// Convert the EFI_IP4_ADDRESS to plain UINT32 IP4 address.\r
+//\r
+#define EFI_IP4(EfiIpAddr) (*(IP4_ADDR *) ((EfiIpAddr).Addr))\r
+#define EFI_NTOHL(EfiIp) (NTOHL (EFI_IP4 ((EfiIp))))\r
+#define EFI_IP4_EQUAL(Ip1, Ip2) (CompareMem ((Ip1), (Ip2), sizeof (EFI_IPv4_ADDRESS)) == 0)\r
+\r
+#define EFI_IP6_EQUAL(Ip1, Ip2) (CompareMem ((Ip1), (Ip2), sizeof (EFI_IPv6_ADDRESS)) == 0)\r
+\r
+#define IP4_COPY_ADDRESS(Dest, Src) (CopyMem ((Dest), (Src), sizeof (EFI_IPv4_ADDRESS)))\r
+#define IP6_COPY_ADDRESS(Dest, Src) (CopyMem ((Dest), (Src), sizeof (EFI_IPv6_ADDRESS)))\r
+#define IP6_COPY_LINK_ADDRESS(Mac1, Mac2) (CopyMem ((Mac1), (Mac2), sizeof (EFI_MAC_ADDRESS)))\r
+\r
+//\r
+// The debug level definition. This value is also used as the\r
+// syslog's severity level. Don't change it.\r
+//\r
+#define NETDEBUG_LEVEL_TRACE 5\r
+#define NETDEBUG_LEVEL_WARNING 4\r
+#define NETDEBUG_LEVEL_ERROR 3\r
+\r
+//\r
+// Network debug message is sent out as syslog packet.\r
+//\r
+#define NET_SYSLOG_FACILITY 16 // Syslog local facility local use\r
+#define NET_SYSLOG_PACKET_LEN 512\r
+#define NET_SYSLOG_TX_TIMEOUT (500 * 1000 * 10) // 500ms\r
+#define NET_DEBUG_MSG_LEN 470 // 512 - (ether+ip4+udp4 head length)\r
+\r
+//\r
+// The debug output expects the ASCII format string, Use %a to print ASCII\r
+// string, and %s to print UNICODE string. PrintArg must be enclosed in ().\r
+// For example: NET_DEBUG_TRACE ("Tcp", ("State transit to %a\n", Name));\r
+//\r
+#define NET_DEBUG_TRACE(Module, PrintArg) \\r
+ NetDebugOutput ( \\r
+ NETDEBUG_LEVEL_TRACE, \\r
+ Module, \\r
+ __FILE__, \\r
+ __LINE__, \\r
+ NetDebugASPrint PrintArg \\r
+ )\r
+\r
+#define NET_DEBUG_WARNING(Module, PrintArg) \\r
+ NetDebugOutput ( \\r
+ NETDEBUG_LEVEL_WARNING, \\r
+ Module, \\r
+ __FILE__, \\r
+ __LINE__, \\r
+ NetDebugASPrint PrintArg \\r
+ )\r
+\r
+#define NET_DEBUG_ERROR(Module, PrintArg) \\r
+ NetDebugOutput ( \\r
+ NETDEBUG_LEVEL_ERROR, \\r
+ Module, \\r
+ __FILE__, \\r
+ __LINE__, \\r
+ NetDebugASPrint PrintArg \\r
+ )\r
+\r
+/**\r
+ Allocate a buffer, then format the message to it. This is a\r
+ help function for the NET_DEBUG_XXX macros. The PrintArg of\r
+ these macros treats the variable length print parameters as a\r
+ single parameter, and pass it to the NetDebugASPrint. For\r
+ example, NET_DEBUG_TRACE ("Tcp", ("State transit to %a\n", Name))\r
+ if extracted to:\r
+\r
+ NetDebugOutput (\r
+ NETDEBUG_LEVEL_TRACE,\r
+ "Tcp",\r
+ __FILE__,\r
+ __LINE__,\r
+ NetDebugASPrint ("State transit to %a\n", Name)\r
+ )\r
+\r
+ @param Format The ASCII format string.\r
+ @param ... The variable length parameter whose format is determined\r
+ by the Format string.\r
+\r
+ @return The buffer containing the formatted message,\r
+ or NULL if memory allocation failed.\r
+\r
+**/\r
+CHAR8 *\r
+EFIAPI\r
+NetDebugASPrint (\r
+ IN CHAR8 *Format,\r
+ ...\r
+ );\r
+\r
+/**\r
+ Builds an UDP4 syslog packet and send it using SNP.\r
+\r
+ This function will locate a instance of SNP then send the message through it.\r
+ Because it isn't open the SNP BY_DRIVER, apply caution when using it.\r
+\r
+ @param Level The severity level of the message.\r
+ @param Module The Module that generates the log.\r
+ @param File The file that contains the log.\r
+ @param Line The exact line that contains the log.\r
+ @param Message The user message to log.\r
+\r
+ @retval EFI_INVALID_PARAMETER Any input parameter is invalid.\r
+ @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the packet\r
+ @retval EFI_SUCCESS The log is discard because that it is more verbose\r
+ than the mNetDebugLevelMax. Or, it has been sent out.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+NetDebugOutput (\r
+ IN UINT32 Level,\r
+ IN UINT8 *Module,\r
+ IN UINT8 *File,\r
+ IN UINT32 Line,\r
+ IN UINT8 *Message\r
+ );\r
+\r
+\r
+/**\r
+ Return the length of the mask.\r
+\r
+ Return the length of the mask. Valid values are 0 to 32.\r
+ If the mask is invalid, return the invalid length 33, which is IP4_MASK_NUM.\r