X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=MdeModulePkg%2FInclude%2FLibrary%2FNetLib.h;h=2eab4a33295c8355c7b275ae36ce3e2f85c2f552;hp=c1519fea23e8f1d6c55c63151fd5fe615e55d7c0;hb=9ae650ef6ca9372199adb31fb3432ed8cde353ad;hpb=e9b67286ad4428d63e2495f38b71273b81e0846f diff --git a/MdeModulePkg/Include/Library/NetLib.h b/MdeModulePkg/Include/Library/NetLib.h index c1519fea23..2eab4a3329 100644 --- a/MdeModulePkg/Include/Library/NetLib.h +++ b/MdeModulePkg/Include/Library/NetLib.h @@ -2,7 +2,7 @@ Ihis library is only intended to be used by UEFI network stack modules. It provides basic functions for the UEFI network stack. -Copyright (c) 2005 - 2008, Intel Corporation +Copyright (c) 2005 - 2009, 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 @@ -16,31 +16,43 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #ifndef _NET_LIB_H_ #define _NET_LIB_H_ -#include +#include typedef UINT32 IP4_ADDR; typedef UINT32 TCP_SEQNO; typedef UINT16 TCP_PORTNO; -typedef 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, +#define NET_ETHER_ADDR_LEN 6 +#define NET_IFTYPE_ETHERNET 0x01 - // - // The address classification - // - IP4_ADDR_CLASSA = 1, - IP4_ADDR_CLASSB, - IP4_ADDR_CLASSC, - IP4_ADDR_CLASSD, - IP4_ADDR_CLASSE, +#define EFI_IP_PROTO_UDP 0x11 +#define EFI_IP_PROTO_TCP 0x06 +#define EFI_IP_PROTO_ICMP 0x01 +#define IP4_PROTO_IGMP 0x02 +#define IP6_ICMP 58 - IP4_MASK_NUM = 33 -} IP4_CLASS_TYPE; +// +// The address classification +// +#define IP4_ADDR_CLASSA 1 +#define IP4_ADDR_CLASSB 2 +#define IP4_ADDR_CLASSC 3 +#define IP4_ADDR_CLASSD 4 +#define IP4_ADDR_CLASSE 5 + +#define IP4_MASK_NUM 33 +#define IP6_PREFIX_NUM 129 + +#define IP6_HOP_BY_HOP 0 +#define IP6_DESTINATION 60 +#define IP6_FRAGMENT 44 +#define IP6_AH 51 +#define IP6_ESP 50 +#define IP6_NO_NEXT_HEADER 59 + +#define IP_VERSION_4 4 +#define IP_VERSION_6 6 #pragma pack(1) @@ -97,6 +109,22 @@ typedef struct { UINT16 Seq; } IP4_ICMP_QUERY_HEAD; +typedef struct { + UINT8 Type; + UINT8 Code; + UINT16 Checksum; +} IP6_ICMP_HEAD; + +typedef struct { + IP6_ICMP_HEAD Head; + UINT32 Fourth; + EFI_IP6_HEADER IpHead; +} IP6_ICMP_ERROR_HEAD; + +typedef struct { + IP6_ICMP_HEAD Head; + UINT32 Fourth; +} IP6_ICMP_INFORMATION_HEAD; // // UDP header definition @@ -106,8 +134,7 @@ typedef struct { UINT16 DstPort; UINT16 Length; UINT16 Checksum; -} EFI_UDP4_HEADER; - +} EFI_UDP_HEADER; // // TCP header definition @@ -143,7 +170,11 @@ typedef struct { #define NTOHS(x) (UINT16)((((UINT16) (x) & 0xff) << 8) | \ (((UINT16) (x) & 0xff00) >> 8)) -#define HTONS(x) NTOHS(x) +#define HTONS(x) NTOHS(x) +#define NTOHLL(x) SwapBytes64 (x) +#define HTONLL(x) NTOHLL(x) +#define NTOHLLL(x) Ip6Swap128 (x) +#define HTONLLL(x) NTOHLLL(x) // // Test the IP's attribute, All the IPs are in host byte order. @@ -153,6 +184,8 @@ typedef struct { #define IP4_NET_EQUAL(Ip1, Ip2, NetMask) (((Ip1) & (NetMask)) == ((Ip2) & (NetMask))) #define IP4_IS_VALID_NETMASK(Ip) (NetGetMaskLength (Ip) != IP4_MASK_NUM) +#define IP6_IS_MULTICAST(Ip6) (((Ip6)->Addr[0]) == 0xFF) + // // Convert the EFI_IP4_ADDRESS to plain UINT32 IP4 address. // @@ -160,6 +193,116 @@ typedef struct { #define EFI_NTOHL(EfiIp) (NTOHL (EFI_IP4 ((EfiIp)))) #define EFI_IP4_EQUAL(Ip1, Ip2) (CompareMem ((Ip1), (Ip2), sizeof (EFI_IPv4_ADDRESS)) == 0) +#define EFI_IP6_EQUAL(Ip1, Ip2) (CompareMem ((Ip1), (Ip2), sizeof (EFI_IPv6_ADDRESS)) == 0) + +#define IP6_COPY_ADDRESS(Dest, Src) (CopyMem ((Dest), (Src), sizeof (EFI_IPv6_ADDRESS))) +#define IP6_COPY_LINK_ADDRESS(Mac1, Mac2) (CopyMem ((Mac1), (Mac2), sizeof (EFI_MAC_ADDRESS))) + +// +// The debug level definition. This value is also used as the +// syslog's servity level. Don't change it. +// +#define NETDEBUG_LEVEL_TRACE 5 +#define NETDEBUG_LEVEL_WARNING 4 +#define NETDEBUG_LEVEL_ERROR 3 + +// +// Network debug message is sent out as syslog packet. +// +#define NET_SYSLOG_FACILITY 16 // Syslog local facility local use +#define NET_SYSLOG_PACKET_LEN 512 +#define NET_SYSLOG_TX_TIMEOUT 500 *1000 *10 // 500ms +#define NET_DEBUG_MSG_LEN 470 // 512 - (ether+ip4+udp4 head length) + +// +// The debug output expects the ASCII format string, Use %a to print ASCII +// string, and %s to print UNICODE string. PrintArg must be enclosed in (). +// For example: NET_DEBUG_TRACE ("Tcp", ("State transit to %a\n", Name)); +// +#define NET_DEBUG_TRACE(Module, PrintArg) \ + NetDebugOutput ( \ + NETDEBUG_LEVEL_TRACE, \ + Module, \ + __FILE__, \ + __LINE__, \ + NetDebugASPrint PrintArg \ + ) + +#define NET_DEBUG_WARNING(Module, PrintArg) \ + NetDebugOutput ( \ + NETDEBUG_LEVEL_WARNING, \ + Module, \ + __FILE__, \ + __LINE__, \ + NetDebugASPrint PrintArg \ + ) + +#define NET_DEBUG_ERROR(Module, PrintArg) \ + NetDebugOutput ( \ + NETDEBUG_LEVEL_ERROR, \ + Module, \ + __FILE__, \ + __LINE__, \ + NetDebugASPrint PrintArg \ + ) + +/** + Allocate a buffer, then format the message to it. This is a + help function for the NET_DEBUG_XXX macros. The PrintArg of + these macros treats the variable length print parameters as a + single parameter, and pass it to the NetDebugASPrint. For + example, NET_DEBUG_TRACE ("Tcp", ("State transit to %a\n", Name)) + if extracted to: + + NetDebugOutput ( + NETDEBUG_LEVEL_TRACE, + "Tcp", + __FILE__, + __LINE__, + NetDebugASPrint ("State transit to %a\n", Name) + ) + + @param Format The ASCII format string. + @param ... The variable length parameter whose format is determined + by the Format string. + + @return The buffer containing the formatted message, + or NULL if failed to allocate memory. + +**/ +CHAR8 * +NetDebugASPrint ( + IN CHAR8 *Format, + ... + ); + +/** + Builds an UDP4 syslog packet and send it using SNP. + + This function will locate a instance of SNP then send the message through it. + Because it isn't open the SNP BY_DRIVER, apply caution when using it. + + @param Level The servity level of the message. + @param Module The Moudle that generates the log. + @param File The file that contains the log. + @param Line The exact line that contains the log. + @param Message The user message to log. + + @retval EFI_INVALID_PARAMETER Any input parameter is invalid. + @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the packet + @retval EFI_SUCCESS The log is discard because that it is more verbose + than the mNetDebugLevelMax. Or, it has been sent out. +**/ +EFI_STATUS +NetDebugOutput ( + IN UINT32 Level, + IN UINT8 *Module, + IN UINT8 *File, + IN UINT32 Line, + IN UINT8 *Message + ); + + /** Return the length of the mask. @@ -222,11 +365,94 @@ NetGetIpClass ( **/ BOOLEAN EFIAPI -Ip4IsUnicast ( +NetIp4IsUnicast ( IN IP4_ADDR Ip, IN IP4_ADDR NetMask ); +/** + Check whether the incoming IPv6 address is a valid unicast address. + + If the address is a multicast address has binary 0xFF at the start, it is not + a valid unicast address. If the address is unspecified ::, it is not a valid + unicast address to be assigned to any node. If the address is loopback address + ::1, it is also not a valid unicast address to be assigned to any physical + interface. + + @param[in] Ip6 The IPv6 address to check against. + + @return TRUE if Ip6 is a valid unicast address on the network, otherwise FALSE. + +**/ +BOOLEAN +NetIp6IsValidUnicast ( + IN EFI_IPv6_ADDRESS *Ip6 + ); + + +/** + Check whether the incoming Ipv6 address is the unspecified address or not. + + @param[in] Ip6 - Ip6 address, in network order. + + @retval TRUE - Yes, unspecified + @retval FALSE - No + +**/ +BOOLEAN +NetIp6IsUnspecifiedAddr ( + IN EFI_IPv6_ADDRESS *Ip6 + ); + +/** + Check whether the incoming Ipv6 address is a link-local address. + + @param[in] Ip6 - Ip6 address, in network order. + + @retval TRUE - Yes, link-local address + @retval FALSE - No + +**/ +BOOLEAN +NetIp6IsLinkLocalAddr ( + IN EFI_IPv6_ADDRESS *Ip6 + ); + +/** + Check whether the Ipv6 address1 and address2 are on the connected network. + + @param[in] Ip1 - Ip6 address1, in network order. + @param[in] Ip2 - Ip6 address2, in network order. + @param[in] PrefixLength - The prefix length of the checking net. + + @retval TRUE - Yes, connected. + @retval FALSE - No. + +**/ +BOOLEAN +NetIp6IsNetEqual ( + EFI_IPv6_ADDRESS *Ip1, + EFI_IPv6_ADDRESS *Ip2, + UINT8 PrefixLength + ); + +/** + Switches the endianess of an IPv6 address + + This function swaps the bytes in a 128-bit IPv6 address to switch the value + from little endian to big endian or vice versa. The byte swapped value is + returned. + + @param Ip6 Points to an IPv6 address + + @return The byte swapped IPv6 address. + +**/ +EFI_IPv6_ADDRESS * +Ip6Swap128 ( + EFI_IPv6_ADDRESS *Ip6 + ); + extern IP4_ADDR gIp4AllMasks[IP4_MASK_NUM]; @@ -677,7 +903,7 @@ EFIAPI NetMapIterate ( IN NET_MAP *Map, IN NET_MAP_CALLBACK CallBack, - IN VOID *Arg + IN VOID *Arg OPTIONAL ); @@ -774,7 +1000,7 @@ NetLibGetMacString ( Get other info from parameters to make up the whole IPv4 device path node. @param[in, out] Node Pointer to the IPv4 device path node. - @param[in] Controller The handle where the NIC IP4 config protocol resides. + @param[in] Controller The controller handle. @param[in] LocalIp The local IPv4 address. @param[in] LocalPort The local port. @param[in] RemoteIp The remote IPv4 address. @@ -796,6 +1022,36 @@ NetLibCreateIPv4DPathNode ( IN BOOLEAN UseDefaultAddress ); +/** + Create an IPv6 device path node. + + The header type of IPv6 device path node is MESSAGING_DEVICE_PATH. + The header subtype of IPv6 device path node is MSG_IPv6_DP. + The length of the IPv6 device path node in bytes is 43. + Get other info from parameters to make up the whole IPv6 device path node. + + @param[in, out] Node Pointer to the IPv6 device path node. + @param[in] Controller The controller handle. + @param[in] LocalIp The local IPv6 address. + @param[in] LocalPort The local port. + @param[in] RemoteIp The remote IPv6 address. + @param[in] RemotePort The remote port. + @param[in] Protocol The protocol type in the IP header. + +**/ +VOID +EFIAPI +NetLibCreateIPv6DPathNode ( + IN OUT IPv6_DEVICE_PATH *Node, + IN EFI_HANDLE Controller, + IN EFI_IPv6_ADDRESS *LocalIp, + IN UINT16 LocalPort, + IN EFI_IPv6_ADDRESS *RemoteIp, + IN UINT16 RemotePort, + IN UINT16 Protocol + ); + + /** Find the UNDI/SNP handle from controller and protocol GUID. @@ -820,45 +1076,6 @@ NetLibGetNicHandle ( IN EFI_GUID *ProtocolGuid ); -/** - Add a Deferred Procedure Call to the end of the DPC queue. - - @param[in] DpcTpl The EFI_TPL that the DPC should be invoked. - @param[in] DpcProcedure Pointer to the DPC's function. - @param[in] DpcContext Pointer to the DPC's context. Passed to DpcProcedure - when DpcProcedure is invoked. - - @retval EFI_SUCCESS The DPC was queued. - @retval EFI_INVALID_PARAMETER DpcTpl is not a valid EFI_TPL, or DpcProcedure - is NULL. - @retval EFI_OUT_OF_RESOURCES There are not enough resources available to - add the DPC to the queue. - -**/ -EFI_STATUS -EFIAPI -NetLibQueueDpc ( - IN EFI_TPL DpcTpl, - IN EFI_DPC_PROCEDURE DpcProcedure, - IN VOID *DpcContext OPTIONAL - ); - -/** - Dispatch the queue of DPCs. ALL DPCs that have been queued with a DpcTpl - value greater than or equal to the current TPL are invoked in the order that - they were queued. DPCs with higher DpcTpl values are invoked before DPCs with - lower DpcTpl values. - - @retval EFI_SUCCESS One or more DPCs were invoked. - @retval EFI_NOT_FOUND No DPCs were invoked. - -**/ -EFI_STATUS -EFIAPI -NetLibDispatchDpc ( - VOID - ); - /** This is the default unload handle for all the network drivers. @@ -877,20 +1094,19 @@ NetLibDefaultUnload ( IN EFI_HANDLE ImageHandle ); -typedef enum { - // - //Various signatures - // - NET_BUF_SIGNATURE = SIGNATURE_32 ('n', 'b', 'u', 'f'), - NET_VECTOR_SIGNATURE = SIGNATURE_32 ('n', 'v', 'e', 'c'), - NET_QUE_SIGNATURE = SIGNATURE_32 ('n', 'b', 'q', 'u'), + +// +//Various signatures +// +#define NET_BUF_SIGNATURE SIGNATURE_32 ('n', 'b', 'u', 'f') +#define NET_VECTOR_SIGNATURE SIGNATURE_32 ('n', 'v', 'e', 'c') +#define NET_QUE_SIGNATURE SIGNATURE_32 ('n', 'b', 'q', 'u') - NET_PROTO_DATA = 64, // Opaque buffer for protocols - NET_BUF_HEAD = 1, // Trim or allocate space from head - NET_BUF_TAIL = 0, // Trim or allocate space from tail - NET_VECTOR_OWN_FIRST = 0x01 // We allocated the 1st block in the vector -} NET_SIGNATURE_TYPE; +#define NET_PROTO_DATA 64 // Opaque buffer for protocols +#define NET_BUF_HEAD 1 // Trim or allocate space from head +#define NET_BUF_TAIL 0 // Trim or allocate space from tail +#define NET_VECTOR_OWN_FIRST 0x01 // We allocated the 1st block in the vector #define NET_CHECK_SIGNATURE(PData, SIGNATURE) \ ASSERT (((PData) != NULL) && ((PData)->Signature == (SIGNATURE))) @@ -937,6 +1153,10 @@ typedef struct { UINT32 Size; // The size of the data } NET_BLOCK_OP; +typedef union { + IP4_HEAD *Ip4; + EFI_IP6_HEADER *Ip6; +} NET_IP_HEAD; // //NET_BUF is the buffer manage structure used by the @@ -949,22 +1169,22 @@ typedef struct { //to overwrite the members after that. // typedef struct { - UINT32 Signature; - INTN RefCnt; - LIST_ENTRY List; // The List this NET_BUF is on + UINT32 Signature; + INTN RefCnt; + LIST_ENTRY List; // The List this NET_BUF is on - IP4_HEAD *Ip; // Network layer header, for fast access - TCP_HEAD *Tcp; // Transport layer header, for fast access - UINT8 ProtoData [NET_PROTO_DATA]; //Protocol specific data + NET_IP_HEAD Ip; // Network layer header, for fast access + TCP_HEAD *Tcp; // Transport layer header, for fast access + EFI_UDP_HEADER *Udp; // User Datagram Protocol header + UINT8 ProtoData [NET_PROTO_DATA]; //Protocol specific data - NET_VECTOR *Vector; // The vector containing the packet + NET_VECTOR *Vector; // The vector containing the packet - UINT32 BlockOpNum; // Total number of BlockOp in the buffer - UINT32 TotalSize; // Total size of the actual packet - NET_BLOCK_OP BlockOp[1]; // Specify the position of actual packet + UINT32 BlockOpNum; // Total number of BlockOp in the buffer + UINT32 TotalSize; // Total size of the actual packet + NET_BLOCK_OP BlockOp[1]; // Specify the position of actual packet } NET_BUF; - // //A queue of NET_BUFs. It is a thin extension of //NET_BUF functions. @@ -990,6 +1210,14 @@ typedef struct { UINT8 Protocol; UINT16 Len; } NET_PSEUDO_HDR; + +typedef struct { + EFI_IPv6_ADDRESS SrcIp; + EFI_IPv6_ADDRESS DstIp; + UINT32 Len; + UINT32 Reserved:24; + UINT32 NextHeader:8; +} NET_IP6_PSEUDO_HDR; #pragma pack() // @@ -1520,4 +1748,24 @@ NetPseudoHeadChecksum ( IN UINT16 Len ); +/** + Compute the checksum for TCP6/UDP6 pseudo header. + + Src and Dst are in network byte order, and Len is in host byte order. + + @param[in] Src The source address of the packet. + @param[in] Dst The destination address of the packet. + @param[in] NextHeader The protocol type of the packet. + @param[in] Len The length of the packet. + + @return The computed checksum. + +**/ +UINT16 +NetIp6PseudoHeadChecksum ( + IN EFI_IPv6_ADDRESS *Src, + IN EFI_IPv6_ADDRESS *Dst, + IN UINT8 NextHeader, + IN UINT32 Len + ); #endif