2 Ihis library is only intended to be used by UEFI network stack modules.
3 It provides basic functions for the UEFI network stack.
5 Copyright (c) 2005 - 2009, Intel Corporation
6 All rights reserved. This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 #include <Protocol/Ip6.h>
21 typedef UINT32 IP4_ADDR
;
22 typedef UINT32 TCP_SEQNO
;
23 typedef UINT16 TCP_PORTNO
;
26 #define NET_ETHER_ADDR_LEN 6
27 #define NET_IFTYPE_ETHERNET 0x01
29 #define EFI_IP_PROTO_UDP 0x11
30 #define EFI_IP_PROTO_TCP 0x06
31 #define EFI_IP_PROTO_ICMP 0x01
32 #define IP4_PROTO_IGMP 0x02
36 // The address classification
38 #define IP4_ADDR_CLASSA 1
39 #define IP4_ADDR_CLASSB 2
40 #define IP4_ADDR_CLASSC 3
41 #define IP4_ADDR_CLASSD 4
42 #define IP4_ADDR_CLASSE 5
44 #define IP4_MASK_NUM 33
45 #define IP6_PREFIX_NUM 129
47 #define IP6_HOP_BY_HOP 0
48 #define IP6_DESTINATION 60
49 #define IP6_FRAGMENT 44
52 #define IP6_NO_NEXT_HEADER 59
54 #define IP_VERSION_4 4
55 #define IP_VERSION_6 6
60 // Ethernet head definition
63 UINT8 DstMac
[NET_ETHER_ADDR_LEN
];
64 UINT8 SrcMac
[NET_ETHER_ADDR_LEN
];
70 // The EFI_IP4_HEADER is hard to use because the source and
71 // destination address are defined as EFI_IPv4_ADDRESS, which
72 // is a structure. Two structures can't be compared or masked
73 // directly. This is why there is an internal representation.
91 // ICMP head definition. Each ICMP message is categorized as either an error
92 // message or query message. Two message types have their own head format.
102 UINT32 Fourth
; // 4th filed of the head, it depends on Type.
104 } IP4_ICMP_ERROR_HEAD
;
110 } IP4_ICMP_QUERY_HEAD
;
121 EFI_IP6_HEADER IpHead
;
122 } IP6_ICMP_ERROR_HEAD
;
127 } IP6_ICMP_INFORMATION_HEAD
;
130 // UDP header definition
140 // TCP header definition
157 #define NET_MAC_EQUAL(pMac1, pMac2, Len) \
158 (CompareMem ((pMac1), (pMac2), Len) == 0)
160 #define NET_MAC_IS_MULTICAST(Mac, BMac, Len) \
161 (((*((UINT8 *) Mac) & 0x01) == 0x01) && (!NET_MAC_EQUAL (Mac, BMac, Len)))
163 #define NTOHL(x) (UINT32)((((UINT32) (x) & 0xff) << 24) | \
164 (((UINT32) (x) & 0xff00) << 8) | \
165 (((UINT32) (x) & 0xff0000) >> 8) | \
166 (((UINT32) (x) & 0xff000000) >> 24))
168 #define HTONL(x) NTOHL(x)
170 #define NTOHS(x) (UINT16)((((UINT16) (x) & 0xff) << 8) | \
171 (((UINT16) (x) & 0xff00) >> 8))
173 #define HTONS(x) NTOHS(x)
174 #define NTOHLL(x) SwapBytes64 (x)
175 #define HTONLL(x) NTOHLL(x)
176 #define NTOHLLL(x) Ip6Swap128 (x)
177 #define HTONLLL(x) NTOHLLL(x)
180 // Test the IP's attribute, All the IPs are in host byte order.
182 #define IP4_IS_MULTICAST(Ip) (((Ip) & 0xF0000000) == 0xE0000000)
183 #define IP4_IS_LOCAL_BROADCAST(Ip) ((Ip) == 0xFFFFFFFF)
184 #define IP4_NET_EQUAL(Ip1, Ip2, NetMask) (((Ip1) & (NetMask)) == ((Ip2) & (NetMask)))
185 #define IP4_IS_VALID_NETMASK(Ip) (NetGetMaskLength (Ip) != IP4_MASK_NUM)
187 #define IP6_IS_MULTICAST(Ip6) (((Ip6)->Addr[0]) == 0xFF)
190 // Convert the EFI_IP4_ADDRESS to plain UINT32 IP4 address.
192 #define EFI_IP4(EfiIpAddr) (*(IP4_ADDR *) ((EfiIpAddr).Addr))
193 #define EFI_NTOHL(EfiIp) (NTOHL (EFI_IP4 ((EfiIp))))
194 #define EFI_IP4_EQUAL(Ip1, Ip2) (CompareMem ((Ip1), (Ip2), sizeof (EFI_IPv4_ADDRESS)) == 0)
196 #define EFI_IP6_EQUAL(Ip1, Ip2) (CompareMem ((Ip1), (Ip2), sizeof (EFI_IPv6_ADDRESS)) == 0)
198 #define IP6_COPY_ADDRESS(Dest, Src) (CopyMem ((Dest), (Src), sizeof (EFI_IPv6_ADDRESS)))
199 #define IP6_COPY_LINK_ADDRESS(Mac1, Mac2) (CopyMem ((Mac1), (Mac2), sizeof (EFI_MAC_ADDRESS)))
202 // The debug level definition. This value is also used as the
203 // syslog's servity level. Don't change it.
205 #define NETDEBUG_LEVEL_TRACE 5
206 #define NETDEBUG_LEVEL_WARNING 4
207 #define NETDEBUG_LEVEL_ERROR 3
210 // Network debug message is sent out as syslog packet.
212 #define NET_SYSLOG_FACILITY 16 // Syslog local facility local use
213 #define NET_SYSLOG_PACKET_LEN 512
214 #define NET_SYSLOG_TX_TIMEOUT 500 *1000 *10 // 500ms
215 #define NET_DEBUG_MSG_LEN 470 // 512 - (ether+ip4+udp4 head length)
218 // The debug output expects the ASCII format string, Use %a to print ASCII
219 // string, and %s to print UNICODE string. PrintArg must be enclosed in ().
220 // For example: NET_DEBUG_TRACE ("Tcp", ("State transit to %a\n", Name));
222 #define NET_DEBUG_TRACE(Module, PrintArg) \
224 NETDEBUG_LEVEL_TRACE, \
228 NetDebugASPrint PrintArg \
231 #define NET_DEBUG_WARNING(Module, PrintArg) \
233 NETDEBUG_LEVEL_WARNING, \
237 NetDebugASPrint PrintArg \
240 #define NET_DEBUG_ERROR(Module, PrintArg) \
242 NETDEBUG_LEVEL_ERROR, \
246 NetDebugASPrint PrintArg \
250 Allocate a buffer, then format the message to it. This is a
251 help function for the NET_DEBUG_XXX macros. The PrintArg of
252 these macros treats the variable length print parameters as a
253 single parameter, and pass it to the NetDebugASPrint. For
254 example, NET_DEBUG_TRACE ("Tcp", ("State transit to %a\n", Name))
258 NETDEBUG_LEVEL_TRACE,
262 NetDebugASPrint ("State transit to %a\n", Name)
265 @param Format The ASCII format string.
266 @param ... The variable length parameter whose format is determined
267 by the Format string.
269 @return The buffer containing the formatted message,
270 or NULL if failed to allocate memory.
280 Builds an UDP4 syslog packet and send it using SNP.
282 This function will locate a instance of SNP then send the message through it.
283 Because it isn't open the SNP BY_DRIVER, apply caution when using it.
285 @param Level The servity level of the message.
286 @param Module The Moudle that generates the log.
287 @param File The file that contains the log.
288 @param Line The exact line that contains the log.
289 @param Message The user message to log.
291 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
292 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the packet
293 @retval EFI_SUCCESS The log is discard because that it is more verbose
294 than the mNetDebugLevelMax. Or, it has been sent out.
307 Return the length of the mask.
309 Return the length of the mask. Valid values are 0 to 32.
310 If the mask is invalid, return the invalid length 33, which is IP4_MASK_NUM.
311 NetMask is in the host byte order.
313 @param[in] NetMask The netmask to get the length from.
315 @return The length of the netmask, or IP4_MASK_NUM (33) if the mask is invalid.
325 Return the class of the IP address, such as class A, B, C.
326 Addr is in host byte order.
328 The address of class A starts with 0.
329 If the address belong to class A, return IP4_ADDR_CLASSA.
330 The address of class B starts with 10.
331 If the address belong to class B, return IP4_ADDR_CLASSB.
332 The address of class C starts with 110.
333 If the address belong to class C, return IP4_ADDR_CLASSC.
334 The address of class D starts with 1110.
335 If the address belong to class D, return IP4_ADDR_CLASSD.
336 The address of class E starts with 1111.
337 If the address belong to class E, return IP4_ADDR_CLASSE.
340 @param[in] Addr The address to get the class from.
342 @return IP address class, such as IP4_ADDR_CLASSA.
352 Check whether the IP is a valid unicast address according to
353 the netmask. If NetMask is zero, use the IP address's class to get the default mask.
355 If Ip is 0, IP is not a valid unicast address.
356 Class D address is used for multicasting and class E address is reserved for future. If Ip
357 belongs to class D or class E, Ip is not a valid unicast address.
358 If all bits of the host address of Ip are 0 or 1, Ip is not a valid unicast address.
360 @param[in] Ip The IP to check against.
361 @param[in] NetMask The mask of the IP.
363 @return TRUE if Ip is a valid unicast address on the network, otherwise FALSE.
374 Check whether the incoming IPv6 address is a valid unicast address.
376 If the address is a multicast address has binary 0xFF at the start, it is not
377 a valid unicast address. If the address is unspecified ::, it is not a valid
378 unicast address to be assigned to any node. If the address is loopback address
379 ::1, it is also not a valid unicast address to be assigned to any physical
382 @param[in] Ip6 The IPv6 address to check against.
384 @return TRUE if Ip6 is a valid unicast address on the network, otherwise FALSE.
388 NetIp6IsValidUnicast (
389 IN EFI_IPv6_ADDRESS
*Ip6
394 Check whether the incoming Ipv6 address is the unspecified address or not.
396 @param[in] Ip6 - Ip6 address, in network order.
398 @retval TRUE - Yes, unspecified
403 NetIp6IsUnspecifiedAddr (
404 IN EFI_IPv6_ADDRESS
*Ip6
408 Check whether the incoming Ipv6 address is a link-local address.
410 @param[in] Ip6 - Ip6 address, in network order.
412 @retval TRUE - Yes, link-local address
417 NetIp6IsLinkLocalAddr (
418 IN EFI_IPv6_ADDRESS
*Ip6
422 Check whether the Ipv6 address1 and address2 are on the connected network.
424 @param[in] Ip1 - Ip6 address1, in network order.
425 @param[in] Ip2 - Ip6 address2, in network order.
426 @param[in] PrefixLength - The prefix length of the checking net.
428 @retval TRUE - Yes, connected.
434 EFI_IPv6_ADDRESS
*Ip1
,
435 EFI_IPv6_ADDRESS
*Ip2
,
440 Switches the endianess of an IPv6 address
442 This function swaps the bytes in a 128-bit IPv6 address to switch the value
443 from little endian to big endian or vice versa. The byte swapped value is
446 @param Ip6 Points to an IPv6 address
448 @return The byte swapped IPv6 address.
453 EFI_IPv6_ADDRESS
*Ip6
456 extern IP4_ADDR gIp4AllMasks
[IP4_MASK_NUM
];
459 extern EFI_IPv4_ADDRESS mZeroIp4Addr
;
461 #define NET_IS_DIGIT(Ch) (('0' <= (Ch)) && ((Ch) <= '9'))
462 #define NET_ROUNDUP(size, unit) (((size) + (unit) - 1) & (~((unit) - 1)))
463 #define NET_IS_LOWER_CASE_CHAR(Ch) (('a' <= (Ch)) && ((Ch) <= 'z'))
464 #define NET_IS_UPPER_CASE_CHAR(Ch) (('A' <= (Ch)) && ((Ch) <= 'Z'))
466 #define TICKS_PER_MS 10000U
467 #define TICKS_PER_SECOND 10000000U
469 #define NET_RANDOM(Seed) ((UINT32) ((UINT32) (Seed) * 1103515245UL + 12345) % 4294967295UL)
472 Extract a UINT32 from a byte stream.
474 This function copies a UINT32 from a byte stream, and then converts it from Network
475 byte order to host byte order. Use this function to avoid alignment error.
477 @param[in] Buf The buffer to extract the UINT32.
479 @return The UINT32 extracted.
489 Puts a UINT32 into the byte stream in network byte order.
491 Converts a UINT32 from host byte order to network byte order, and then copies it to the
494 @param[in, out] Buf The buffer to put the UINT32.
495 @param[in] Data The data to put.
506 Initialize a random seed using current time.
508 Get current time first. Then initialize a random seed based on some basic
509 mathematical operations on the hour, day, minute, second, nanosecond and year
512 @return The random seed, initialized with current time.
522 #define NET_LIST_USER_STRUCT(Entry, Type, Field) \
523 BASE_CR(Entry, Type, Field)
525 #define NET_LIST_USER_STRUCT_S(Entry, Type, Field, Sig) \
526 CR(Entry, Type, Field, Sig)
529 // Iterate through the double linked list. It is NOT delete safe
531 #define NET_LIST_FOR_EACH(Entry, ListHead) \
532 for(Entry = (ListHead)->ForwardLink; Entry != (ListHead); Entry = Entry->ForwardLink)
535 // Iterate through the double linked list. This is delete-safe.
536 // Don't touch NextEntry. Also, don't use this macro if list
537 // entries other than the Entry may be deleted when processing
538 // the current Entry.
540 #define NET_LIST_FOR_EACH_SAFE(Entry, NextEntry, ListHead) \
541 for(Entry = (ListHead)->ForwardLink, NextEntry = Entry->ForwardLink; \
542 Entry != (ListHead); \
543 Entry = NextEntry, NextEntry = Entry->ForwardLink \
547 // Make sure the list isn't empty before getting the first/last record.
549 #define NET_LIST_HEAD(ListHead, Type, Field) \
550 NET_LIST_USER_STRUCT((ListHead)->ForwardLink, Type, Field)
552 #define NET_LIST_TAIL(ListHead, Type, Field) \
553 NET_LIST_USER_STRUCT((ListHead)->BackLink, Type, Field)
557 Remove the first node entry on the list, and return the removed node entry.
559 Removes the first node entry from a doubly linked list. It is up to the caller of
560 this function to release the memory used by the first node, if that is required. On
561 exit, the removed node is returned.
563 If Head is NULL, then ASSERT().
564 If Head was not initialized, then ASSERT().
565 If PcdMaximumLinkedListLength is not zero, and the number of nodes in the
566 linked list including the head node is greater than or equal to PcdMaximumLinkedListLength,
569 @param[in, out] Head The list header.
571 @return The first node entry that is removed from the list, NULL if the list is empty.
577 IN OUT LIST_ENTRY
*Head
581 Remove the last node entry on the list and return the removed node entry.
583 Removes the last node entry from a doubly linked list. It is up to the caller of
584 this function to release the memory used by the first node, if that is required. On
585 exit, the removed node is returned.
587 If Head is NULL, then ASSERT().
588 If Head was not initialized, then ASSERT().
589 If PcdMaximumLinkedListLength is not zero, and the number of nodes in the
590 linked list including the head node is greater than or equal to PcdMaximumLinkedListLength,
593 @param[in, out] Head The list head.
595 @return The last node entry that is removed from the list, NULL if the list is empty.
601 IN OUT LIST_ENTRY
*Head
605 Insert a new node entry after a designated node entry of a doubly linked list.
607 Inserts a new node entry designated by NewEntry after the node entry designated by PrevEntry
608 of the doubly linked list.
610 @param[in, out] PrevEntry The entry after which to insert.
611 @param[in, out] NewEntry The new entry to insert.
617 IN OUT LIST_ENTRY
*PrevEntry
,
618 IN OUT LIST_ENTRY
*NewEntry
622 Insert a new node entry before a designated node entry of a doubly linked list.
624 Inserts a new node entry designated by NewEntry before the node entry designated by PostEntry
625 of the doubly linked list.
627 @param[in, out] PostEntry The entry to insert before.
628 @param[in, out] NewEntry The new entry to insert.
633 NetListInsertBefore (
634 IN OUT LIST_ENTRY
*PostEntry
,
635 IN OUT LIST_ENTRY
*NewEntry
640 // Object container: EFI network stack spec defines various kinds of
641 // tokens. The drivers can share code to manage those objects.
655 #define NET_MAP_INCREAMENT 64
658 Initialize the netmap. Netmap is a reposity to keep the <Key, Value> pairs.
660 Initialize the forward and backward links of two head nodes donated by Map->Used
661 and Map->Recycled of two doubly linked lists.
662 Initializes the count of the <Key, Value> pairs in the netmap to zero.
664 If Map is NULL, then ASSERT().
665 If the address of Map->Used is NULL, then ASSERT().
666 If the address of Map->Recycled is NULl, then ASSERT().
668 @param[in, out] Map The netmap to initialize.
678 To clean up the netmap, that is, release allocated memories.
680 Removes all nodes of the Used doubly linked list and frees memory of all related netmap items.
681 Removes all nodes of the Recycled doubly linked list and free memory of all related netmap items.
682 The number of the <Key, Value> pairs in the netmap is set to zero.
684 If Map is NULL, then ASSERT().
686 @param[in, out] Map The netmap to clean up.
696 Test whether the netmap is empty and return true if it is.
698 If the number of the <Key, Value> pairs in the netmap is zero, return TRUE.
700 If Map is NULL, then ASSERT().
703 @param[in] Map The net map to test.
705 @return TRUE if the netmap is empty, otherwise FALSE.
715 Return the number of the <Key, Value> pairs in the netmap.
717 @param[in] Map The netmap to get the entry number.
719 @return The entry number in the netmap.
729 Allocate an item to save the <Key, Value> pair to the head of the netmap.
731 Allocate an item to save the <Key, Value> pair and add corresponding node entry
732 to the beginning of the Used doubly linked list. The number of the <Key, Value>
733 pairs in the netmap increase by 1.
735 If Map is NULL, then ASSERT().
737 @param[in, out] Map The netmap to insert into.
738 @param[in] Key The user's key.
739 @param[in] Value The user's value for the key.
741 @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the item.
742 @retval EFI_SUCCESS The item is inserted to the head.
750 IN VOID
*Value OPTIONAL
754 Allocate an item to save the <Key, Value> pair to the tail of the netmap.
756 Allocate an item to save the <Key, Value> pair and add corresponding node entry
757 to the tail of the Used doubly linked list. The number of the <Key, Value>
758 pairs in the netmap increase by 1.
760 If Map is NULL, then ASSERT().
762 @param[in, out] Map The netmap to insert into.
763 @param[in] Key The user's key.
764 @param[in] Value The user's value for the key.
766 @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the item.
767 @retval EFI_SUCCESS The item is inserted to the tail.
775 IN VOID
*Value OPTIONAL
779 Finds the key in the netmap and returns the point to the item containing the Key.
781 Iterate the Used doubly linked list of the netmap to get every item. Compare the key of every
782 item with the key to search. It returns the point to the item contains the Key if found.
784 If Map is NULL, then ASSERT().
786 @param[in] Map The netmap to search within.
787 @param[in] Key The key to search.
789 @return The point to the item contains the Key, or NULL if Key isn't in the map.
800 Remove the node entry of the item from the netmap and return the key of the removed item.
802 Remove the node entry of the item from the Used doubly linked list of the netmap.
803 The number of the <Key, Value> pairs in the netmap decrease by 1. Then add the node
804 entry of the item to the Recycled doubly linked list of the netmap. If Value is not NULL,
805 Value will point to the value of the item. It returns the key of the removed item.
807 If Map is NULL, then ASSERT().
808 If Item is NULL, then ASSERT().
809 if item in not in the netmap, then ASSERT().
811 @param[in, out] Map The netmap to remove the item from.
812 @param[in, out] Item The item to remove.
813 @param[out] Value The variable to receive the value if not NULL.
815 @return The key of the removed item.
822 IN OUT NET_MAP_ITEM
*Item
,
823 OUT VOID
**Value OPTIONAL
827 Remove the first node entry on the netmap and return the key of the removed item.
829 Remove the first node entry from the Used doubly linked list of the netmap.
830 The number of the <Key, Value> pairs in the netmap decrease by 1. Then add the node
831 entry to the Recycled doubly linked list of the netmap. If parameter Value is not NULL,
832 parameter Value will point to the value of the item. It returns the key of the removed item.
834 If Map is NULL, then ASSERT().
835 If the Used doubly linked list is empty, then ASSERT().
837 @param[in, out] Map The netmap to remove the head from.
838 @param[out] Value The variable to receive the value if not NULL.
840 @return The key of the item removed.
847 OUT VOID
**Value OPTIONAL
851 Remove the last node entry on the netmap and return the key of the removed item.
853 Remove the last node entry from the Used doubly linked list of the netmap.
854 The number of the <Key, Value> pairs in the netmap decrease by 1. Then add the node
855 entry to the Recycled doubly linked list of the netmap. If parameter Value is not NULL,
856 parameter Value will point to the value of the item. It returns the key of the removed item.
858 If Map is NULL, then ASSERT().
859 If the Used doubly linked list is empty, then ASSERT().
861 @param[in, out] Map The netmap to remove the tail from.
862 @param[out] Value The variable to receive the value if not NULL.
864 @return The key of the item removed.
871 OUT VOID
**Value OPTIONAL
876 (*NET_MAP_CALLBACK
) (
878 IN NET_MAP_ITEM
*Item
,
883 Iterate through the netmap and call CallBack for each item.
885 It will contiue the traverse if CallBack returns EFI_SUCCESS, otherwise, break
886 from the loop. It returns the CallBack's last return value. This function is
887 delete safe for the current item.
889 If Map is NULL, then ASSERT().
890 If CallBack is NULL, then ASSERT().
892 @param[in] Map The Map to iterate through.
893 @param[in] CallBack The callback function to call for each item.
894 @param[in] Arg The opaque parameter to the callback.
896 @retval EFI_SUCCESS There is no item in the netmap or CallBack for each item
898 @retval Others It returns the CallBack's last return value.
905 IN NET_MAP_CALLBACK CallBack
,
906 IN VOID
*Arg OPTIONAL
911 // Helper functions to implement driver binding and service binding protocols.
914 Create a child of the service that is identified by ServiceBindingGuid.
916 Get the ServiceBinding Protocol first, then use it to create a child.
918 If ServiceBindingGuid is NULL, then ASSERT().
919 If ChildHandle is NULL, then ASSERT().
921 @param[in] Controller The controller which has the service installed.
922 @param[in] Image The image handle used to open service.
923 @param[in] ServiceBindingGuid The service's Guid.
924 @param[in, out] ChildHandle The handle to receive the created child.
926 @retval EFI_SUCCESS The child was successfully created.
927 @retval Others Failed to create the child.
932 NetLibCreateServiceChild (
933 IN EFI_HANDLE Controller
,
935 IN EFI_GUID
*ServiceBindingGuid
,
936 IN OUT EFI_HANDLE
*ChildHandle
940 Destroy a child of the service that is identified by ServiceBindingGuid.
942 Get the ServiceBinding Protocol first, then use it to destroy a child.
944 If ServiceBindingGuid is NULL, then ASSERT().
946 @param[in] Controller The controller which has the service installed.
947 @param[in] Image The image handle used to open service.
948 @param[in] ServiceBindingGuid The service's Guid.
949 @param[in] ChildHandle The child to destroy.
951 @retval EFI_SUCCESS The child is successfully destroyed.
952 @retval Others Failed to destroy the child.
957 NetLibDestroyServiceChild (
958 IN EFI_HANDLE Controller
,
960 IN EFI_GUID
*ServiceBindingGuid
,
961 IN EFI_HANDLE ChildHandle
965 Convert the mac address of the simple network protocol installed on
966 SnpHandle to a unicode string. Callers are responsible for freeing the
969 Get the mac address of the Simple Network protocol from the SnpHandle. Then convert
970 the mac address into a unicode string. It takes 2 unicode characters to represent
971 a 1 byte binary buffer, plus one unicode character for the null terminator.
974 @param[in] SnpHandle The handle on which the simple network protocol is
976 @param[in] ImageHandle The image handle to act as the agent handle to
977 get the simple network protocol.
978 @param[out] MacString The pointer to store the address of the string
979 representation of the mac address.
981 @retval EFI_SUCCESS Converted the mac address a unicode string successfully.
982 @retval EFI_OUT_OF_RESOURCES There are not enough memory resources.
983 @retval Others Failed to open the simple network protocol.
989 IN EFI_HANDLE SnpHandle
,
990 IN EFI_HANDLE ImageHandle
,
991 OUT CHAR16
**MacString
995 Create an IPv4 device path node.
997 The header type of IPv4 device path node is MESSAGING_DEVICE_PATH.
998 The header subtype of IPv4 device path node is MSG_IPv4_DP.
999 The length of the IPv4 device path node in bytes is 19.
1000 Get other info from parameters to make up the whole IPv4 device path node.
1002 @param[in, out] Node Pointer to the IPv4 device path node.
1003 @param[in] Controller The controller handle.
1004 @param[in] LocalIp The local IPv4 address.
1005 @param[in] LocalPort The local port.
1006 @param[in] RemoteIp The remote IPv4 address.
1007 @param[in] RemotePort The remote port.
1008 @param[in] Protocol The protocol type in the IP header.
1009 @param[in] UseDefaultAddress Whether this instance is using default address or not.
1014 NetLibCreateIPv4DPathNode (
1015 IN OUT IPv4_DEVICE_PATH
*Node
,
1016 IN EFI_HANDLE Controller
,
1017 IN IP4_ADDR LocalIp
,
1018 IN UINT16 LocalPort
,
1019 IN IP4_ADDR RemoteIp
,
1020 IN UINT16 RemotePort
,
1022 IN BOOLEAN UseDefaultAddress
1026 Create an IPv6 device path node.
1028 The header type of IPv6 device path node is MESSAGING_DEVICE_PATH.
1029 The header subtype of IPv6 device path node is MSG_IPv6_DP.
1030 The length of the IPv6 device path node in bytes is 43.
1031 Get other info from parameters to make up the whole IPv6 device path node.
1033 @param[in, out] Node Pointer to the IPv6 device path node.
1034 @param[in] Controller The controller handle.
1035 @param[in] LocalIp The local IPv6 address.
1036 @param[in] LocalPort The local port.
1037 @param[in] RemoteIp The remote IPv6 address.
1038 @param[in] RemotePort The remote port.
1039 @param[in] Protocol The protocol type in the IP header.
1044 NetLibCreateIPv6DPathNode (
1045 IN OUT IPv6_DEVICE_PATH
*Node
,
1046 IN EFI_HANDLE Controller
,
1047 IN EFI_IPv6_ADDRESS
*LocalIp
,
1048 IN UINT16 LocalPort
,
1049 IN EFI_IPv6_ADDRESS
*RemoteIp
,
1050 IN UINT16 RemotePort
,
1056 Find the UNDI/SNP handle from controller and protocol GUID.
1058 For example, IP will open an MNP child to transmit/receive
1059 packets. When MNP is stopped, IP should also be stopped. IP
1060 needs to find its own private data which is related the IP's
1061 service binding instance that is install on UNDI/SNP handle.
1062 Now, the controller is either a MNP or ARP child handle. But
1063 IP opens these handle BY_DRIVER, use that info, we can get the
1066 @param[in] Controller Then protocol handle to check.
1067 @param[in] ProtocolGuid The protocol that is related with the handle.
1069 @return The UNDI/SNP handle or NULL for errors.
1074 NetLibGetNicHandle (
1075 IN EFI_HANDLE Controller
,
1076 IN EFI_GUID
*ProtocolGuid
1080 This is the default unload handle for all the network drivers.
1082 Disconnect the driver specified by ImageHandle from all the devices in the handle database.
1083 Uninstall all the protocols installed in the driver entry point.
1085 @param[in] ImageHandle The drivers' driver image.
1087 @retval EFI_SUCCESS The image is unloaded.
1088 @retval Others Failed to unload the image.
1093 NetLibDefaultUnload (
1094 IN EFI_HANDLE ImageHandle
1099 //Various signatures
1101 #define NET_BUF_SIGNATURE SIGNATURE_32 ('n', 'b', 'u', 'f')
1102 #define NET_VECTOR_SIGNATURE SIGNATURE_32 ('n', 'v', 'e', 'c')
1103 #define NET_QUE_SIGNATURE SIGNATURE_32 ('n', 'b', 'q', 'u')
1106 #define NET_PROTO_DATA 64 // Opaque buffer for protocols
1107 #define NET_BUF_HEAD 1 // Trim or allocate space from head
1108 #define NET_BUF_TAIL 0 // Trim or allocate space from tail
1109 #define NET_VECTOR_OWN_FIRST 0x01 // We allocated the 1st block in the vector
1111 #define NET_CHECK_SIGNATURE(PData, SIGNATURE) \
1112 ASSERT (((PData) != NULL) && ((PData)->Signature == (SIGNATURE)))
1114 #define NET_SWAP_SHORT(Value) \
1115 ((((Value) & 0xff) << 8) | (((Value) >> 8) & 0xff))
1118 // Single memory block in the vector.
1121 UINT32 Len
; // The block's length
1122 UINT8
*Bulk
; // The block's Data
1125 typedef VOID (*NET_VECTOR_EXT_FREE
) (VOID
*Arg
);
1128 //NET_VECTOR contains several blocks to hold all packet's
1129 //fragments and other house-keeping stuff for sharing. It
1130 //doesn't specify the where actual packet fragment begins.
1134 INTN RefCnt
; // Reference count to share NET_VECTOR.
1135 NET_VECTOR_EXT_FREE Free
; // external function to free NET_VECTOR
1136 VOID
*Arg
; // opeque argument to Free
1137 UINT32 Flag
; // Flags, NET_VECTOR_OWN_FIRST
1138 UINT32 Len
; // Total length of the assocated BLOCKs
1145 //NET_BLOCK_OP operates on the NET_BLOCK. It specifies
1146 //where the actual fragment begins and ends
1149 UINT8
*BlockHead
; // Block's head, or the smallest valid Head
1150 UINT8
*BlockTail
; // Block's tail. BlockTail-BlockHead=block length
1151 UINT8
*Head
; // 1st byte of the data in the block
1152 UINT8
*Tail
; // Tail of the data in the block, Tail-Head=Size
1153 UINT32 Size
; // The size of the data
1158 EFI_IP6_HEADER
*Ip6
;
1162 //NET_BUF is the buffer manage structure used by the
1163 //network stack. Every network packet may be fragmented. The Vector points to
1164 //memory blocks used by each fragment, and BlockOp
1165 //specifies where each fragment begins and ends.
1167 //It also contains an opaque area for the protocol to store
1168 //per-packet information. Protocol must be careful not
1169 //to overwrite the members after that.
1174 LIST_ENTRY List
; // The List this NET_BUF is on
1176 NET_IP_HEAD Ip
; // Network layer header, for fast access
1177 TCP_HEAD
*Tcp
; // Transport layer header, for fast access
1178 EFI_UDP_HEADER
*Udp
; // User Datagram Protocol header
1179 UINT8 ProtoData
[NET_PROTO_DATA
]; //Protocol specific data
1181 NET_VECTOR
*Vector
; // The vector containing the packet
1183 UINT32 BlockOpNum
; // Total number of BlockOp in the buffer
1184 UINT32 TotalSize
; // Total size of the actual packet
1185 NET_BLOCK_OP BlockOp
[1]; // Specify the position of actual packet
1189 //A queue of NET_BUFs. It is a thin extension of
1190 //NET_BUF functions.
1195 LIST_ENTRY List
; // The List this buffer queue is on
1197 LIST_ENTRY BufList
; // list of queued buffers
1198 UINT32 BufSize
; // total length of DATA in the buffers
1199 UINT32 BufNum
; // total number of buffers on the chain
1203 // Pseudo header for TCP and UDP checksum
1215 EFI_IPv6_ADDRESS SrcIp
;
1216 EFI_IPv6_ADDRESS DstIp
;
1219 UINT32 NextHeader
:8;
1220 } NET_IP6_PSEUDO_HDR
;
1224 // The fragment entry table used in network interfaces. This is
1225 // the same as NET_BLOCK now. Use two different to distinguish
1226 // the two in case that NET_BLOCK be enhanced later.
1233 #define NET_GET_REF(PData) ((PData)->RefCnt++)
1234 #define NET_PUT_REF(PData) ((PData)->RefCnt--)
1235 #define NETBUF_FROM_PROTODATA(Info) BASE_CR((Info), NET_BUF, ProtoData)
1237 #define NET_BUF_SHARED(Buf) \
1238 (((Buf)->RefCnt > 1) || ((Buf)->Vector->RefCnt > 1))
1240 #define NET_VECTOR_SIZE(BlockNum) \
1241 (sizeof (NET_VECTOR) + ((BlockNum) - 1) * sizeof (NET_BLOCK))
1243 #define NET_BUF_SIZE(BlockOpNum) \
1244 (sizeof (NET_BUF) + ((BlockOpNum) - 1) * sizeof (NET_BLOCK_OP))
1246 #define NET_HEADSPACE(BlockOp) \
1247 (UINTN)((BlockOp)->Head - (BlockOp)->BlockHead)
1249 #define NET_TAILSPACE(BlockOp) \
1250 (UINTN)((BlockOp)->BlockTail - (BlockOp)->Tail)
1253 Allocate a single block NET_BUF. Upon allocation, all the
1254 free space is in the tail room.
1256 @param[in] Len The length of the block.
1258 @return Pointer to the allocated NET_BUF, or NULL if the
1259 allocation failed due to resource limit.
1269 Free the net buffer and its associated NET_VECTOR.
1271 Decrease the reference count of the net buffer by one. Free the associated net
1272 vector and itself if the reference count of the net buffer is decreased to 0.
1273 The net vector free operation decreases the reference count of the net
1274 vector by one, and performs the resource free operation when the reference count
1275 of the net vector is 0.
1277 @param[in] Nbuf Pointer to the NET_BUF to be freed.
1287 Get the index of NET_BLOCK_OP that contains the byte at Offset in the net
1290 For example, this function can be used to retrieve the IP header in the packet. It
1291 also can be used to get the fragment that contains the byte used
1292 mainly by the library implementation itself.
1294 @param[in] Nbuf Pointer to the net buffer.
1295 @param[in] Offset The offset of the byte.
1296 @param[out] Index Index of the NET_BLOCK_OP that contains the byte at
1299 @return Pointer to the Offset'th byte of data in the net buffer, or NULL
1300 if there is no such data in the net buffer.
1308 OUT UINT32
*Index OPTIONAL
1312 Create a copy of the net buffer that shares the associated net vector.
1314 The reference count of the newly created net buffer is set to 1. The reference
1315 count of the associated net vector is increased by one.
1317 @param[in] Nbuf Pointer to the net buffer to be cloned.
1319 @return Pointer to the cloned net buffer, or NULL if the
1320 allocation failed due to resource limit.
1330 Create a duplicated copy of the net buffer with data copied and HeadSpace
1331 bytes of head space reserved.
1333 The duplicated net buffer will allocate its own memory to hold the data of the
1336 @param[in] Nbuf Pointer to the net buffer to be duplicated from.
1337 @param[in, out] Duplicate Pointer to the net buffer to duplicate to, if
1338 NULL a new net buffer is allocated.
1339 @param[in] HeadSpace Length of the head space to reserve.
1341 @return Pointer to the duplicated net buffer, or NULL if
1342 the allocation failed due to resource limit.
1349 IN OUT NET_BUF
*Duplicate OPTIONAL
,
1354 Create a NET_BUF structure which contains Len byte data of Nbuf starting from
1357 A new NET_BUF structure will be created but the associated data in NET_VECTOR
1358 is shared. This function exists to do IP packet fragmentation.
1360 @param[in] Nbuf Pointer to the net buffer to be extracted.
1361 @param[in] Offset Starting point of the data to be included in the new
1363 @param[in] Len Bytes of data to be included in the new net buffer.
1364 @param[in] HeadSpace Bytes of head space to reserve for protocol header.
1366 @return Pointer to the cloned net buffer, or NULL if the
1367 allocation failed due to resource limit.
1380 Reserve some space in the header room of the net buffer.
1382 Upon allocation, all the space is in the tail room of the buffer. Call this
1383 function to move some space to the header room. This function is quite limited
1384 in that it can only reserve space from the first block of an empty NET_BUF not
1385 built from the external. But it should be enough for the network stack.
1387 @param[in, out] Nbuf Pointer to the net buffer.
1388 @param[in] Len The length of buffer to be reserved from the header.
1394 IN OUT NET_BUF
*Nbuf
,
1399 Allocate Len bytes of space from the header or tail of the buffer.
1401 @param[in, out] Nbuf Pointer to the net buffer.
1402 @param[in] Len The length of the buffer to be allocated.
1403 @param[in] FromHead The flag to indicate whether reserve the data
1404 from head (TRUE) or tail (FALSE).
1406 @return Pointer to the first byte of the allocated buffer,
1407 or NULL if there is no sufficient space.
1413 IN OUT NET_BUF
*Nbuf
,
1419 Trim Len bytes from the header or tail of the net buffer.
1421 @param[in, out] Nbuf Pointer to the net buffer.
1422 @param[in] Len The length of the data to be trimmed.
1423 @param[in] FromHead The flag to indicate whether trim data from head
1424 (TRUE) or tail (FALSE).
1426 @return Length of the actually trimmed data, which may be less
1427 than Len if the TotalSize of Nbuf is less than Len.
1433 IN OUT NET_BUF
*Nbuf
,
1439 Copy Len bytes of data from the specific offset of the net buffer to the
1442 The Len bytes of data may cross several fragments of the net buffer.
1444 @param[in] Nbuf Pointer to the net buffer.
1445 @param[in] Offset The sequence number of the first byte to copy.
1446 @param[in] Len Length of the data to copy.
1447 @param[in] Dest The destination of the data to copy to.
1449 @return The length of the actual copied data, or 0 if the offset
1450 specified exceeds the total size of net buffer.
1463 Build a NET_BUF from external blocks.
1465 A new NET_BUF structure will be created from external blocks. An additional block
1466 of memory will be allocated to hold reserved HeadSpace bytes of header room
1467 and existing HeadLen bytes of header, but the external blocks are shared by the
1468 net buffer to avoid data copying.
1470 @param[in] ExtFragment Pointer to the data block.
1471 @param[in] ExtNum The number of the data blocks.
1472 @param[in] HeadSpace The head space to be reserved.
1473 @param[in] HeadLen The length of the protocol header. The function
1474 pulls this amount of data into a linear block.
1475 @param[in] ExtFree Pointer to the caller-provided free function.
1476 @param[in] Arg The argument passed to ExtFree when ExtFree is
1479 @return Pointer to the net buffer built from the data blocks,
1480 or NULL if the allocation failed due to resource
1487 IN NET_FRAGMENT
*ExtFragment
,
1489 IN UINT32 HeadSpace
,
1491 IN NET_VECTOR_EXT_FREE ExtFree
,
1492 IN VOID
*Arg OPTIONAL
1496 Build a fragment table to contain the fragments in the net buffer. This is the
1497 opposite operation of the NetbufFromExt.
1499 @param[in] Nbuf Point to the net buffer.
1500 @param[in, out] ExtFragment Pointer to the data block.
1501 @param[in, out] ExtNum The number of the data blocks.
1503 @retval EFI_BUFFER_TOO_SMALL The number of non-empty blocks is bigger than
1505 @retval EFI_SUCCESS Fragment table is built successfully.
1512 IN OUT NET_FRAGMENT
*ExtFragment
,
1513 IN OUT UINT32
*ExtNum
1517 Build a net buffer from a list of net buffers.
1519 All the fragments will be collected from the list of NEW_BUF and then a new
1520 net buffer will be created through NetbufFromExt.
1522 @param[in] BufList A List of the net buffer.
1523 @param[in] HeadSpace The head space to be reserved.
1524 @param[in] HeaderLen The length of the protocol header. The function
1525 pulls this amount of data into a linear block.
1526 @param[in] ExtFree Pointer to the caller provided free function.
1527 @param[in] Arg The argument passed to ExtFree when ExtFree is called.
1529 @return Pointer to the net buffer built from the list of net
1536 IN LIST_ENTRY
*BufList
,
1537 IN UINT32 HeadSpace
,
1538 IN UINT32 HeaderLen
,
1539 IN NET_VECTOR_EXT_FREE ExtFree
,
1540 IN VOID
*Arg OPTIONAL
1544 Free a list of net buffers.
1546 @param[in, out] Head Pointer to the head of linked net buffers.
1552 IN OUT LIST_ENTRY
*Head
1556 Initiate the net buffer queue.
1558 @param[in, out] NbufQue Pointer to the net buffer queue to be initialized.
1564 IN OUT NET_BUF_QUEUE
*NbufQue
1568 Allocate and initialize a net buffer queue.
1570 @return Pointer to the allocated net buffer queue, or NULL if the
1571 allocation failed due to resource limit.
1581 Free a net buffer queue.
1583 Decrease the reference count of the net buffer queue by one. The real resource
1584 free operation isn't performed until the reference count of the net buffer
1585 queue is decreased to 0.
1587 @param[in] NbufQue Pointer to the net buffer queue to be freed.
1593 IN NET_BUF_QUEUE
*NbufQue
1597 Remove a net buffer from the head in the specific queue and return it.
1599 @param[in, out] NbufQue Pointer to the net buffer queue.
1601 @return Pointer to the net buffer removed from the specific queue,
1602 or NULL if there is no net buffer in the specific queue.
1608 IN OUT NET_BUF_QUEUE
*NbufQue
1612 Append a net buffer to the net buffer queue.
1614 @param[in, out] NbufQue Pointer to the net buffer queue.
1615 @param[in, out] Nbuf Pointer to the net buffer to be appended.
1621 IN OUT NET_BUF_QUEUE
*NbufQue
,
1622 IN OUT NET_BUF
*Nbuf
1626 Copy Len bytes of data from the net buffer queue at the specific offset to the
1629 The copying operation is the same as NetbufCopy but applies to the net buffer
1630 queue instead of the net buffer.
1632 @param[in] NbufQue Pointer to the net buffer queue.
1633 @param[in] Offset The sequence number of the first byte to copy.
1634 @param[in] Len Length of the data to copy.
1635 @param[out] Dest The destination of the data to copy to.
1637 @return The length of the actual copied data, or 0 if the offset
1638 specified exceeds the total size of net buffer queue.
1644 IN NET_BUF_QUEUE
*NbufQue
,
1651 Trim Len bytes of data from the queue header and release any net buffer
1652 that is trimmed wholely.
1654 The trimming operation is the same as NetbufTrim but applies to the net buffer
1655 queue instead of the net buffer.
1657 @param[in, out] NbufQue Pointer to the net buffer queue.
1658 @param[in] Len Length of the data to trim.
1660 @return The actual length of the data trimmed.
1666 IN OUT NET_BUF_QUEUE
*NbufQue
,
1672 Flush the net buffer queue.
1674 @param[in, out] NbufQue Pointer to the queue to be flushed.
1680 IN OUT NET_BUF_QUEUE
*NbufQue
1684 Compute the checksum for a bulk of data.
1686 @param[in] Bulk Pointer to the data.
1687 @param[in] Len Length of the data, in bytes.
1689 @return The computed checksum.
1702 @param[in] Checksum1 The first checksum to be added.
1703 @param[in] Checksum2 The second checksum to be added.
1705 @return The new checksum.
1711 IN UINT16 Checksum1
,
1716 Compute the checksum for a NET_BUF.
1718 @param[in] Nbuf Pointer to the net buffer.
1720 @return The computed checksum.
1730 Compute the checksum for TCP/UDP pseudo header.
1732 Src and Dst are in network byte order, and Len is in host byte order.
1734 @param[in] Src The source address of the packet.
1735 @param[in] Dst The destination address of the packet.
1736 @param[in] Proto The protocol type of the packet.
1737 @param[in] Len The length of the packet.
1739 @return The computed checksum.
1744 NetPseudoHeadChecksum (
1752 Compute the checksum for TCP6/UDP6 pseudo header.
1754 Src and Dst are in network byte order, and Len is in host byte order.
1756 @param[in] Src The source address of the packet.
1757 @param[in] Dst The destination address of the packet.
1758 @param[in] NextHeader The protocol type of the packet.
1759 @param[in] Len The length of the packet.
1761 @return The computed checksum.
1765 NetIp6PseudoHeadChecksum (
1766 IN EFI_IPv6_ADDRESS
*Src
,
1767 IN EFI_IPv6_ADDRESS
*Dst
,
1768 IN UINT8 NextHeader
,