]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Include/Library/NetLib.h
Sync EDKII BaseTools to BaseTools project r2068.
[mirror_edk2.git] / MdeModulePkg / Include / Library / NetLib.h
CommitLineData
97b38d4e 1/** @file\r
1204fe83 2 This library is only intended to be used by UEFI network stack modules.\r
e9b67286 3 It provides basic functions for the UEFI network stack.\r
97b38d4e 4\r
cd5ebaa0
HT
5Copyright (c) 2005 - 2010, Intel Corporation. All rights reserved.<BR>\r
6This program and the accompanying materials\r
97b38d4e 7are licensed and made available under the terms and conditions of the BSD License\r
64a80549 8which accompanies this distribution. The full text of the license may be found at<BR>\r
97b38d4e 9http://opensource.org/licenses/bsd-license.php\r
10\r
11THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
14**/\r
15\r
16#ifndef _NET_LIB_H_\r
17#define _NET_LIB_H_\r
18\r
fb115c61 19#include <Protocol/Ip6.h>\r
20\r
1204fe83 21#include <Library/BaseLib.h>\r
22\r
97b38d4e 23typedef UINT32 IP4_ADDR;\r
24typedef UINT32 TCP_SEQNO;\r
25typedef UINT16 TCP_PORTNO;\r
26\r
b45b45b2 27\r
28#define NET_ETHER_ADDR_LEN 6\r
29#define NET_IFTYPE_ETHERNET 0x01\r
30\r
779ae357 31#define NET_VLAN_TAG_LEN 4\r
32#define ETHER_TYPE_VLAN 0x8100\r
33\r
b45b45b2 34#define EFI_IP_PROTO_UDP 0x11\r
35#define EFI_IP_PROTO_TCP 0x06\r
36#define EFI_IP_PROTO_ICMP 0x01\r
f6b7393c 37#define IP4_PROTO_IGMP 0x02\r
38#define IP6_ICMP 58\r
b45b45b2 39\r
40//\r
41// The address classification\r
42//\r
43#define IP4_ADDR_CLASSA 1\r
44#define IP4_ADDR_CLASSB 2\r
45#define IP4_ADDR_CLASSC 3\r
46#define IP4_ADDR_CLASSD 4\r
47#define IP4_ADDR_CLASSE 5\r
48\r
49#define IP4_MASK_NUM 33\r
f6b7393c 50#define IP6_PREFIX_NUM 129\r
b45b45b2 51\r
52#define IP6_HOP_BY_HOP 0\r
53#define IP6_DESTINATION 60\r
25400c63 54#define IP6_ROUTING 43\r
b45b45b2 55#define IP6_FRAGMENT 44\r
56#define IP6_AH 51\r
57#define IP6_ESP 50\r
58#define IP6_NO_NEXT_HEADER 59\r
59\r
a1503a32 60#define IP_VERSION_4 4\r
61#define IP_VERSION_6 6\r
fb115c61 62\r
97b38d4e 63#pragma pack(1)\r
64\r
65//\r
66// Ethernet head definition\r
67//\r
68typedef struct {\r
69 UINT8 DstMac [NET_ETHER_ADDR_LEN];\r
70 UINT8 SrcMac [NET_ETHER_ADDR_LEN];\r
71 UINT16 EtherType;\r
72} ETHER_HEAD;\r
73\r
779ae357 74//\r
75// 802.1Q VLAN Tag Control Information\r
76//\r
77typedef union {\r
78 struct {\r
79 UINT16 Vid : 12; // Unique VLAN identifier (0 to 4094)\r
80 UINT16 Cfi : 1; // Canonical Format Indicator\r
81 UINT16 Priority : 3; // 802.1Q priority level (0 to 7)\r
82 } Bits;\r
83 UINT16 Uint16;\r
84} VLAN_TCI;\r
85\r
86#define VLAN_TCI_CFI_CANONICAL_MAC 0\r
87#define VLAN_TCI_CFI_NON_CANONICAL_MAC 1\r
97b38d4e 88\r
89//\r
90// The EFI_IP4_HEADER is hard to use because the source and\r
91// destination address are defined as EFI_IPv4_ADDRESS, which\r
92// is a structure. Two structures can't be compared or masked\r
93// directly. This is why there is an internal representation.\r
94//\r
95typedef struct {\r
96 UINT8 HeadLen : 4;\r
97 UINT8 Ver : 4;\r
98 UINT8 Tos;\r
99 UINT16 TotalLen;\r
100 UINT16 Id;\r
101 UINT16 Fragment;\r
102 UINT8 Ttl;\r
103 UINT8 Protocol;\r
104 UINT16 Checksum;\r
105 IP4_ADDR Src;\r
106 IP4_ADDR Dst;\r
107} IP4_HEAD;\r
108\r
109\r
110//\r
e9b67286 111// ICMP head definition. Each ICMP message is categorized as either an error\r
97b38d4e 112// message or query message. Two message types have their own head format.\r
113//\r
114typedef struct {\r
115 UINT8 Type;\r
116 UINT8 Code;\r
117 UINT16 Checksum;\r
118} IP4_ICMP_HEAD;\r
119\r
120typedef struct {\r
121 IP4_ICMP_HEAD Head;\r
122 UINT32 Fourth; // 4th filed of the head, it depends on Type.\r
123 IP4_HEAD IpHead;\r
124} IP4_ICMP_ERROR_HEAD;\r
125\r
126typedef struct {\r
127 IP4_ICMP_HEAD Head;\r
128 UINT16 Id;\r
129 UINT16 Seq;\r
130} IP4_ICMP_QUERY_HEAD;\r
131\r
fb115c61 132typedef struct {\r
133 UINT8 Type;\r
134 UINT8 Code;\r
135 UINT16 Checksum;\r
136} IP6_ICMP_HEAD;\r
137\r
138typedef struct {\r
139 IP6_ICMP_HEAD Head;\r
140 UINT32 Fourth;\r
141 EFI_IP6_HEADER IpHead;\r
142} IP6_ICMP_ERROR_HEAD;\r
143\r
144typedef struct {\r
145 IP6_ICMP_HEAD Head;\r
146 UINT32 Fourth;\r
147} IP6_ICMP_INFORMATION_HEAD;\r
97b38d4e 148\r
149//\r
150// UDP header definition\r
151//\r
152typedef struct {\r
153 UINT16 SrcPort;\r
154 UINT16 DstPort;\r
155 UINT16 Length;\r
156 UINT16 Checksum;\r
fb115c61 157} EFI_UDP_HEADER;\r
97b38d4e 158\r
159//\r
160// TCP header definition\r
161//\r
162typedef struct {\r
163 TCP_PORTNO SrcPort;\r
164 TCP_PORTNO DstPort;\r
165 TCP_SEQNO Seq;\r
166 TCP_SEQNO Ack;\r
167 UINT8 Res : 4;\r
168 UINT8 HeadLen : 4;\r
169 UINT8 Flag;\r
170 UINT16 Wnd;\r
171 UINT16 Checksum;\r
172 UINT16 Urg;\r
173} TCP_HEAD;\r
174\r
175#pragma pack()\r
176\r
177#define NET_MAC_EQUAL(pMac1, pMac2, Len) \\r
178 (CompareMem ((pMac1), (pMac2), Len) == 0)\r
179\r
180#define NET_MAC_IS_MULTICAST(Mac, BMac, Len) \\r
181 (((*((UINT8 *) Mac) & 0x01) == 0x01) && (!NET_MAC_EQUAL (Mac, BMac, Len)))\r
182\r
1204fe83 183#define NTOHL(x) SwapBytes32 (x)\r
97b38d4e 184\r
185#define HTONL(x) NTOHL(x)\r
186\r
1204fe83 187#define NTOHS(x) SwapBytes16 (x)\r
97b38d4e 188\r
f6b7393c 189#define HTONS(x) NTOHS(x)\r
190#define NTOHLL(x) SwapBytes64 (x)\r
191#define HTONLL(x) NTOHLL(x)\r
192#define NTOHLLL(x) Ip6Swap128 (x)\r
193#define HTONLLL(x) NTOHLLL(x)\r
97b38d4e 194\r
195//\r
196// Test the IP's attribute, All the IPs are in host byte order.\r
197//\r
198#define IP4_IS_MULTICAST(Ip) (((Ip) & 0xF0000000) == 0xE0000000)\r
199#define IP4_IS_LOCAL_BROADCAST(Ip) ((Ip) == 0xFFFFFFFF)\r
200#define IP4_NET_EQUAL(Ip1, Ip2, NetMask) (((Ip1) & (NetMask)) == ((Ip2) & (NetMask)))\r
201#define IP4_IS_VALID_NETMASK(Ip) (NetGetMaskLength (Ip) != IP4_MASK_NUM)\r
202\r
3a15fd52 203#define IP6_IS_MULTICAST(Ip6) (((Ip6)->Addr[0]) == 0xFF)\r
204\r
97b38d4e 205//\r
206// Convert the EFI_IP4_ADDRESS to plain UINT32 IP4 address.\r
207//\r
208#define EFI_IP4(EfiIpAddr) (*(IP4_ADDR *) ((EfiIpAddr).Addr))\r
209#define EFI_NTOHL(EfiIp) (NTOHL (EFI_IP4 ((EfiIp))))\r
210#define EFI_IP4_EQUAL(Ip1, Ip2) (CompareMem ((Ip1), (Ip2), sizeof (EFI_IPv4_ADDRESS)) == 0)\r
211\r
fb115c61 212#define EFI_IP6_EQUAL(Ip1, Ip2) (CompareMem ((Ip1), (Ip2), sizeof (EFI_IPv6_ADDRESS)) == 0)\r
213\r
f6b7393c 214#define IP6_COPY_ADDRESS(Dest, Src) (CopyMem ((Dest), (Src), sizeof (EFI_IPv6_ADDRESS)))\r
215#define IP6_COPY_LINK_ADDRESS(Mac1, Mac2) (CopyMem ((Mac1), (Mac2), sizeof (EFI_MAC_ADDRESS)))\r
216\r
217//\r
1204fe83 218// The debug level definition. This value is also used as the\r
219// syslog's servity level. Don't change it.\r
f6b7393c 220//\r
221#define NETDEBUG_LEVEL_TRACE 5\r
222#define NETDEBUG_LEVEL_WARNING 4\r
223#define NETDEBUG_LEVEL_ERROR 3\r
224\r
225//\r
1204fe83 226// Network debug message is sent out as syslog packet.\r
f6b7393c 227//\r
1204fe83 228#define NET_SYSLOG_FACILITY 16 // Syslog local facility local use\r
229#define NET_SYSLOG_PACKET_LEN 512\r
230#define NET_SYSLOG_TX_TIMEOUT (500 * 1000 * 10) // 500ms\r
231#define NET_DEBUG_MSG_LEN 470 // 512 - (ether+ip4+udp4 head length)\r
f6b7393c 232\r
233//\r
1204fe83 234// The debug output expects the ASCII format string, Use %a to print ASCII\r
235// string, and %s to print UNICODE string. PrintArg must be enclosed in ().\r
f6b7393c 236// For example: NET_DEBUG_TRACE ("Tcp", ("State transit to %a\n", Name));\r
237//\r
238#define NET_DEBUG_TRACE(Module, PrintArg) \\r
239 NetDebugOutput ( \\r
240 NETDEBUG_LEVEL_TRACE, \\r
241 Module, \\r
242 __FILE__, \\r
243 __LINE__, \\r
244 NetDebugASPrint PrintArg \\r
245 )\r
246\r
247#define NET_DEBUG_WARNING(Module, PrintArg) \\r
248 NetDebugOutput ( \\r
249 NETDEBUG_LEVEL_WARNING, \\r
250 Module, \\r
251 __FILE__, \\r
252 __LINE__, \\r
253 NetDebugASPrint PrintArg \\r
254 )\r
255\r
256#define NET_DEBUG_ERROR(Module, PrintArg) \\r
257 NetDebugOutput ( \\r
258 NETDEBUG_LEVEL_ERROR, \\r
259 Module, \\r
260 __FILE__, \\r
261 __LINE__, \\r
262 NetDebugASPrint PrintArg \\r
263 )\r
264\r
265/**\r
1204fe83 266 Allocate a buffer, then format the message to it. This is a\r
267 help function for the NET_DEBUG_XXX macros. The PrintArg of\r
268 these macros treats the variable length print parameters as a\r
f6b7393c 269 single parameter, and pass it to the NetDebugASPrint. For\r
270 example, NET_DEBUG_TRACE ("Tcp", ("State transit to %a\n", Name))\r
1204fe83 271 if extracted to:\r
272\r
f6b7393c 273 NetDebugOutput (\r
1204fe83 274 NETDEBUG_LEVEL_TRACE,\r
275 "Tcp",\r
f6b7393c 276 __FILE__,\r
277 __LINE__,\r
1204fe83 278 NetDebugASPrint ("State transit to %a\n", Name)\r
279 )\r
280\r
f6b7393c 281 @param Format The ASCII format string.\r
1204fe83 282 @param ... The variable length parameter whose format is determined\r
f6b7393c 283 by the Format string.\r
284\r
285 @return The buffer containing the formatted message,\r
64a80549 286 or NULL if memory allocation failed.\r
f6b7393c 287\r
288**/\r
289CHAR8 *\r
e798cd87 290EFIAPI\r
f6b7393c 291NetDebugASPrint (\r
292 IN CHAR8 *Format,\r
293 ...\r
294 );\r
295\r
296/**\r
297 Builds an UDP4 syslog packet and send it using SNP.\r
298\r
299 This function will locate a instance of SNP then send the message through it.\r
300 Because it isn't open the SNP BY_DRIVER, apply caution when using it.\r
301\r
302 @param Level The servity level of the message.\r
303 @param Module The Moudle that generates the log.\r
304 @param File The file that contains the log.\r
305 @param Line The exact line that contains the log.\r
306 @param Message The user message to log.\r
307\r
308 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.\r
309 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the packet\r
1204fe83 310 @retval EFI_SUCCESS The log is discard because that it is more verbose\r
f6b7393c 311 than the mNetDebugLevelMax. Or, it has been sent out.\r
1204fe83 312**/\r
f6b7393c 313EFI_STATUS\r
e798cd87 314EFIAPI\r
f6b7393c 315NetDebugOutput (\r
1204fe83 316 IN UINT32 Level,\r
f6b7393c 317 IN UINT8 *Module,\r
318 IN UINT8 *File,\r
319 IN UINT32 Line,\r
320 IN UINT8 *Message\r
321 );\r
322\r
fb115c61 323\r
97b38d4e 324/**\r
1204fe83 325 Return the length of the mask.\r
326\r
e9b67286 327 Return the length of the mask. Valid values are 0 to 32.\r
3a1ab4bc 328 If the mask is invalid, return the invalid length 33, which is IP4_MASK_NUM.\r
97b38d4e 329 NetMask is in the host byte order.\r
330\r
ae213b7d 331 @param[in] NetMask The netmask to get the length from.\r
97b38d4e 332\r
e9b67286 333 @return The length of the netmask, or IP4_MASK_NUM (33) if the mask is invalid.\r
1204fe83 334\r
97b38d4e 335**/\r
336INTN\r
337EFIAPI\r
338NetGetMaskLength (\r
ae213b7d 339 IN IP4_ADDR NetMask\r
97b38d4e 340 );\r
341\r
342/**\r
3a1ab4bc 343 Return the class of the IP address, such as class A, B, C.\r
97b38d4e 344 Addr is in host byte order.\r
1204fe83 345\r
3a1ab4bc 346 The address of class A starts with 0.\r
347 If the address belong to class A, return IP4_ADDR_CLASSA.\r
1204fe83 348 The address of class B starts with 10.\r
3a1ab4bc 349 If the address belong to class B, return IP4_ADDR_CLASSB.\r
1204fe83 350 The address of class C starts with 110.\r
3a1ab4bc 351 If the address belong to class C, return IP4_ADDR_CLASSC.\r
1204fe83 352 The address of class D starts with 1110.\r
3a1ab4bc 353 If the address belong to class D, return IP4_ADDR_CLASSD.\r
354 The address of class E starts with 1111.\r
355 If the address belong to class E, return IP4_ADDR_CLASSE.\r
97b38d4e 356\r
1204fe83 357\r
ae213b7d 358 @param[in] Addr The address to get the class from.\r
97b38d4e 359\r
ae213b7d 360 @return IP address class, such as IP4_ADDR_CLASSA.\r
97b38d4e 361\r
362**/\r
363INTN\r
364EFIAPI\r
365NetGetIpClass (\r
366 IN IP4_ADDR Addr\r
367 );\r
368\r
369/**\r
370 Check whether the IP is a valid unicast address according to\r
3a1ab4bc 371 the netmask. If NetMask is zero, use the IP address's class to get the default mask.\r
1204fe83 372\r
3a1ab4bc 373 If Ip is 0, IP is not a valid unicast address.\r
374 Class D address is used for multicasting and class E address is reserved for future. If Ip\r
1204fe83 375 belongs to class D or class E, Ip is not a valid unicast address.\r
e9b67286 376 If all bits of the host address of Ip are 0 or 1, Ip is not a valid unicast address.\r
97b38d4e 377\r
ae213b7d 378 @param[in] Ip The IP to check against.\r
379 @param[in] NetMask The mask of the IP.\r
97b38d4e 380\r
e9b67286 381 @return TRUE if Ip is a valid unicast address on the network, otherwise FALSE.\r
97b38d4e 382\r
383**/\r
384BOOLEAN\r
ae213b7d 385EFIAPI\r
f6b7393c 386NetIp4IsUnicast (\r
97b38d4e 387 IN IP4_ADDR Ip,\r
388 IN IP4_ADDR NetMask\r
389 );\r
390\r
fb115c61 391/**\r
392 Check whether the incoming IPv6 address is a valid unicast address.\r
393\r
394 If the address is a multicast address has binary 0xFF at the start, it is not\r
395 a valid unicast address. If the address is unspecified ::, it is not a valid\r
396 unicast address to be assigned to any node. If the address is loopback address\r
397 ::1, it is also not a valid unicast address to be assigned to any physical\r
1204fe83 398 interface.\r
fb115c61 399\r
400 @param[in] Ip6 The IPv6 address to check against.\r
401\r
402 @return TRUE if Ip6 is a valid unicast address on the network, otherwise FALSE.\r
403\r
1204fe83 404**/\r
fb115c61 405BOOLEAN\r
e798cd87 406EFIAPI\r
f6b7393c 407NetIp6IsValidUnicast (\r
408 IN EFI_IPv6_ADDRESS *Ip6\r
409 );\r
410\r
411\r
412/**\r
413 Check whether the incoming Ipv6 address is the unspecified address or not.\r
414\r
415 @param[in] Ip6 - Ip6 address, in network order.\r
416\r
64a80549 417 @retval TRUE - Yes, incoming Ipv6 address is the unspecified address.\r
418 @retval FALSE - The incoming Ipv6 address is not the unspecified address\r
1204fe83 419\r
f6b7393c 420**/\r
421BOOLEAN\r
e798cd87 422EFIAPI\r
f6b7393c 423NetIp6IsUnspecifiedAddr (\r
fb115c61 424 IN EFI_IPv6_ADDRESS *Ip6\r
425 );\r
426\r
f6b7393c 427/**\r
428 Check whether the incoming Ipv6 address is a link-local address.\r
429\r
430 @param[in] Ip6 - Ip6 address, in network order.\r
431\r
64a80549 432 @retval TRUE - The incoming Ipv6 address is a link-local address.\r
433 @retval FALSE - The incoming Ipv6 address is not a link-local address.\r
1204fe83 434\r
f6b7393c 435**/\r
436BOOLEAN\r
e798cd87 437EFIAPI\r
f6b7393c 438NetIp6IsLinkLocalAddr (\r
439 IN EFI_IPv6_ADDRESS *Ip6\r
440 );\r
441\r
442/**\r
443 Check whether the Ipv6 address1 and address2 are on the connected network.\r
444\r
445 @param[in] Ip1 - Ip6 address1, in network order.\r
446 @param[in] Ip2 - Ip6 address2, in network order.\r
447 @param[in] PrefixLength - The prefix length of the checking net.\r
448\r
64a80549 449 @retval TRUE - Yes, the Ipv6 address1 and address2 are connected.\r
450 @retval FALSE - No the Ipv6 address1 and address2 are not connected.\r
1204fe83 451\r
f6b7393c 452**/\r
453BOOLEAN\r
e798cd87 454EFIAPI\r
f6b7393c 455NetIp6IsNetEqual (\r
456 EFI_IPv6_ADDRESS *Ip1,\r
457 EFI_IPv6_ADDRESS *Ip2,\r
458 UINT8 PrefixLength\r
459 );\r
460\r
b45b45b2 461/**\r
64a80549 462 Switches the endianess of an IPv6 address.\r
b45b45b2 463\r
464 This function swaps the bytes in a 128-bit IPv6 address to switch the value\r
465 from little endian to big endian or vice versa. The byte swapped value is\r
466 returned.\r
467\r
64a80549 468 @param Ip6 Points to an IPv6 address.\r
b45b45b2 469\r
470 @return The byte swapped IPv6 address.\r
471\r
472**/\r
473EFI_IPv6_ADDRESS *\r
e798cd87 474EFIAPI\r
b45b45b2 475Ip6Swap128 (\r
476 EFI_IPv6_ADDRESS *Ip6\r
477 );\r
478\r
8d7e5af1 479extern IP4_ADDR gIp4AllMasks[IP4_MASK_NUM];\r
97b38d4e 480\r
481\r
482extern EFI_IPv4_ADDRESS mZeroIp4Addr;\r
483\r
484#define NET_IS_DIGIT(Ch) (('0' <= (Ch)) && ((Ch) <= '9'))\r
485#define NET_ROUNDUP(size, unit) (((size) + (unit) - 1) & (~((unit) - 1)))\r
486#define NET_IS_LOWER_CASE_CHAR(Ch) (('a' <= (Ch)) && ((Ch) <= 'z'))\r
487#define NET_IS_UPPER_CASE_CHAR(Ch) (('A' <= (Ch)) && ((Ch) <= 'Z'))\r
488\r
489#define TICKS_PER_MS 10000U\r
490#define TICKS_PER_SECOND 10000000U\r
491\r
492#define NET_RANDOM(Seed) ((UINT32) ((UINT32) (Seed) * 1103515245UL + 12345) % 4294967295UL)\r
493\r
494/**\r
3a1ab4bc 495 Extract a UINT32 from a byte stream.\r
1204fe83 496\r
497 This function copies a UINT32 from a byte stream, and then converts it from Network\r
3a1ab4bc 498 byte order to host byte order. Use this function to avoid alignment error.\r
97b38d4e 499\r
ae213b7d 500 @param[in] Buf The buffer to extract the UINT32.\r
97b38d4e 501\r
502 @return The UINT32 extracted.\r
503\r
504**/\r
505UINT32\r
506EFIAPI\r
507NetGetUint32 (\r
508 IN UINT8 *Buf\r
509 );\r
510\r
511/**\r
1204fe83 512 Puts a UINT32 into the byte stream in network byte order.\r
513\r
64a80549 514 Converts a UINT32 from host byte order to network byte order, then copies it to the\r
3a1ab4bc 515 byte stream.\r
97b38d4e 516\r
64a80549 517 @param[in, out] Buf The buffer in which to put the UINT32.\r
3b1464d5 518 @param[in] Data The data to be converted and put into the byte stream.\r
1204fe83 519\r
97b38d4e 520**/\r
521VOID\r
522EFIAPI\r
523NetPutUint32 (\r
ae213b7d 524 IN OUT UINT8 *Buf,\r
525 IN UINT32 Data\r
97b38d4e 526 );\r
527\r
528/**\r
529 Initialize a random seed using current time.\r
1204fe83 530\r
531 Get current time first. Then initialize a random seed based on some basic\r
532 mathematical operations on the hour, day, minute, second, nanosecond and year\r
3a1ab4bc 533 of the current time.\r
1204fe83 534\r
e9b67286 535 @return The random seed, initialized with current time.\r
97b38d4e 536\r
537**/\r
538UINT32\r
539EFIAPI\r
540NetRandomInitSeed (\r
541 VOID\r
542 );\r
543\r
544\r
545#define NET_LIST_USER_STRUCT(Entry, Type, Field) \\r
50d7ebad 546 BASE_CR(Entry, Type, Field)\r
97b38d4e 547\r
548#define NET_LIST_USER_STRUCT_S(Entry, Type, Field, Sig) \\r
549 CR(Entry, Type, Field, Sig)\r
550\r
551//\r
e9b67286 552// Iterate through the double linked list. It is NOT delete safe\r
97b38d4e 553//\r
554#define NET_LIST_FOR_EACH(Entry, ListHead) \\r
555 for(Entry = (ListHead)->ForwardLink; Entry != (ListHead); Entry = Entry->ForwardLink)\r
556\r
557//\r
e9b67286 558// Iterate through the double linked list. This is delete-safe.\r
97b38d4e 559// Don't touch NextEntry. Also, don't use this macro if list\r
560// entries other than the Entry may be deleted when processing\r
561// the current Entry.\r
562//\r
563#define NET_LIST_FOR_EACH_SAFE(Entry, NextEntry, ListHead) \\r
564 for(Entry = (ListHead)->ForwardLink, NextEntry = Entry->ForwardLink; \\r
565 Entry != (ListHead); \\r
566 Entry = NextEntry, NextEntry = Entry->ForwardLink \\r
567 )\r
568\r
569//\r
e9b67286 570// Make sure the list isn't empty before getting the first/last record.\r
97b38d4e 571//\r
572#define NET_LIST_HEAD(ListHead, Type, Field) \\r
573 NET_LIST_USER_STRUCT((ListHead)->ForwardLink, Type, Field)\r
574\r
575#define NET_LIST_TAIL(ListHead, Type, Field) \\r
576 NET_LIST_USER_STRUCT((ListHead)->BackLink, Type, Field)\r
577\r
578\r
579/**\r
3a1ab4bc 580 Remove the first node entry on the list, and return the removed node entry.\r
1204fe83 581\r
e9b67286 582 Removes the first node entry from a doubly linked list. It is up to the caller of\r
583 this function to release the memory used by the first node, if that is required. On\r
1204fe83 584 exit, the removed node is returned.\r
3a1ab4bc 585\r
586 If Head is NULL, then ASSERT().\r
587 If Head was not initialized, then ASSERT().\r
588 If PcdMaximumLinkedListLength is not zero, and the number of nodes in the\r
589 linked list including the head node is greater than or equal to PcdMaximumLinkedListLength,\r
1204fe83 590 then ASSERT().\r
97b38d4e 591\r
ae213b7d 592 @param[in, out] Head The list header.\r
97b38d4e 593\r
3a1ab4bc 594 @return The first node entry that is removed from the list, NULL if the list is empty.\r
97b38d4e 595\r
596**/\r
597LIST_ENTRY *\r
598EFIAPI\r
599NetListRemoveHead (\r
ae213b7d 600 IN OUT LIST_ENTRY *Head\r
97b38d4e 601 );\r
602\r
603/**\r
e9b67286 604 Remove the last node entry on the list and return the removed node entry.\r
3a1ab4bc 605\r
606 Removes the last node entry from a doubly linked list. It is up to the caller of\r
e9b67286 607 this function to release the memory used by the first node, if that is required. On\r
1204fe83 608 exit, the removed node is returned.\r
97b38d4e 609\r
3a1ab4bc 610 If Head is NULL, then ASSERT().\r
611 If Head was not initialized, then ASSERT().\r
612 If PcdMaximumLinkedListLength is not zero, and the number of nodes in the\r
613 linked list including the head node is greater than or equal to PcdMaximumLinkedListLength,\r
1204fe83 614 then ASSERT().\r
615\r
ae213b7d 616 @param[in, out] Head The list head.\r
97b38d4e 617\r
3a1ab4bc 618 @return The last node entry that is removed from the list, NULL if the list is empty.\r
97b38d4e 619\r
620**/\r
621LIST_ENTRY *\r
622EFIAPI\r
623NetListRemoveTail (\r
ae213b7d 624 IN OUT LIST_ENTRY *Head\r
97b38d4e 625 );\r
626\r
627/**\r
3a1ab4bc 628 Insert a new node entry after a designated node entry of a doubly linked list.\r
1204fe83 629\r
e9b67286 630 Inserts a new node entry designated by NewEntry after the node entry designated by PrevEntry\r
3a1ab4bc 631 of the doubly linked list.\r
1204fe83 632\r
633 @param[in, out] PrevEntry The entry after which to insert.\r
ae213b7d 634 @param[in, out] NewEntry The new entry to insert.\r
97b38d4e 635\r
636**/\r
637VOID\r
638EFIAPI\r
639NetListInsertAfter (\r
ae213b7d 640 IN OUT LIST_ENTRY *PrevEntry,\r
641 IN OUT LIST_ENTRY *NewEntry\r
97b38d4e 642 );\r
643\r
644/**\r
3a1ab4bc 645 Insert a new node entry before a designated node entry of a doubly linked list.\r
1204fe83 646\r
e9b67286 647 Inserts a new node entry designated by NewEntry before the node entry designated by PostEntry\r
3a1ab4bc 648 of the doubly linked list.\r
1204fe83 649\r
ae213b7d 650 @param[in, out] PostEntry The entry to insert before.\r
651 @param[in, out] NewEntry The new entry to insert.\r
97b38d4e 652\r
653**/\r
654VOID\r
655EFIAPI\r
656NetListInsertBefore (\r
ae213b7d 657 IN OUT LIST_ENTRY *PostEntry,\r
658 IN OUT LIST_ENTRY *NewEntry\r
97b38d4e 659 );\r
660\r
661\r
662//\r
663// Object container: EFI network stack spec defines various kinds of\r
664// tokens. The drivers can share code to manage those objects.\r
665//\r
666typedef struct {\r
667 LIST_ENTRY Link;\r
668 VOID *Key;\r
669 VOID *Value;\r
670} NET_MAP_ITEM;\r
671\r
672typedef struct {\r
673 LIST_ENTRY Used;\r
674 LIST_ENTRY Recycled;\r
675 UINTN Count;\r
676} NET_MAP;\r
677\r
678#define NET_MAP_INCREAMENT 64\r
679\r
680/**\r
681 Initialize the netmap. Netmap is a reposity to keep the <Key, Value> pairs.\r
1204fe83 682\r
683 Initialize the forward and backward links of two head nodes donated by Map->Used\r
3a1ab4bc 684 and Map->Recycled of two doubly linked lists.\r
685 Initializes the count of the <Key, Value> pairs in the netmap to zero.\r
1204fe83 686\r
3a1ab4bc 687 If Map is NULL, then ASSERT().\r
688 If the address of Map->Used is NULL, then ASSERT().\r
689 If the address of Map->Recycled is NULl, then ASSERT().\r
1204fe83 690\r
ae213b7d 691 @param[in, out] Map The netmap to initialize.\r
97b38d4e 692\r
693**/\r
694VOID\r
695EFIAPI\r
696NetMapInit (\r
ae213b7d 697 IN OUT NET_MAP *Map\r
97b38d4e 698 );\r
699\r
700/**\r
701 To clean up the netmap, that is, release allocated memories.\r
1204fe83 702\r
e9b67286 703 Removes all nodes of the Used doubly linked list and frees memory of all related netmap items.\r
3a1ab4bc 704 Removes all nodes of the Recycled doubly linked list and free memory of all related netmap items.\r
e9b67286 705 The number of the <Key, Value> pairs in the netmap is set to zero.\r
1204fe83 706\r
3a1ab4bc 707 If Map is NULL, then ASSERT().\r
1204fe83 708\r
ae213b7d 709 @param[in, out] Map The netmap to clean up.\r
97b38d4e 710\r
711**/\r
712VOID\r
713EFIAPI\r
714NetMapClean (\r
ae213b7d 715 IN OUT NET_MAP *Map\r
97b38d4e 716 );\r
717\r
718/**\r
3a1ab4bc 719 Test whether the netmap is empty and return true if it is.\r
1204fe83 720\r
3a1ab4bc 721 If the number of the <Key, Value> pairs in the netmap is zero, return TRUE.\r
1204fe83 722\r
3a1ab4bc 723 If Map is NULL, then ASSERT().\r
1204fe83 724\r
725\r
ae213b7d 726 @param[in] Map The net map to test.\r
97b38d4e 727\r
728 @return TRUE if the netmap is empty, otherwise FALSE.\r
729\r
730**/\r
731BOOLEAN\r
732EFIAPI\r
733NetMapIsEmpty (\r
734 IN NET_MAP *Map\r
735 );\r
736\r
737/**\r
738 Return the number of the <Key, Value> pairs in the netmap.\r
739\r
ae213b7d 740 @param[in] Map The netmap to get the entry number.\r
97b38d4e 741\r
742 @return The entry number in the netmap.\r
743\r
744**/\r
745UINTN\r
746EFIAPI\r
747NetMapGetCount (\r
748 IN NET_MAP *Map\r
749 );\r
750\r
751/**\r
752 Allocate an item to save the <Key, Value> pair to the head of the netmap.\r
1204fe83 753\r
3a1ab4bc 754 Allocate an item to save the <Key, Value> pair and add corresponding node entry\r
1204fe83 755 to the beginning of the Used doubly linked list. The number of the <Key, Value>\r
3a1ab4bc 756 pairs in the netmap increase by 1.\r
97b38d4e 757\r
3a1ab4bc 758 If Map is NULL, then ASSERT().\r
1204fe83 759\r
ae213b7d 760 @param[in, out] Map The netmap to insert into.\r
761 @param[in] Key The user's key.\r
762 @param[in] Value The user's value for the key.\r
97b38d4e 763\r
ae213b7d 764 @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the item.\r
765 @retval EFI_SUCCESS The item is inserted to the head.\r
97b38d4e 766\r
767**/\r
768EFI_STATUS\r
769EFIAPI\r
770NetMapInsertHead (\r
ae213b7d 771 IN OUT NET_MAP *Map,\r
97b38d4e 772 IN VOID *Key,\r
773 IN VOID *Value OPTIONAL\r
774 );\r
775\r
776/**\r
777 Allocate an item to save the <Key, Value> pair to the tail of the netmap.\r
778\r
3a1ab4bc 779 Allocate an item to save the <Key, Value> pair and add corresponding node entry\r
1204fe83 780 to the tail of the Used doubly linked list. The number of the <Key, Value>\r
3a1ab4bc 781 pairs in the netmap increase by 1.\r
782\r
783 If Map is NULL, then ASSERT().\r
1204fe83 784\r
ae213b7d 785 @param[in, out] Map The netmap to insert into.\r
786 @param[in] Key The user's key.\r
787 @param[in] Value The user's value for the key.\r
97b38d4e 788\r
ae213b7d 789 @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the item.\r
790 @retval EFI_SUCCESS The item is inserted to the tail.\r
97b38d4e 791\r
792**/\r
793EFI_STATUS\r
794EFIAPI\r
795NetMapInsertTail (\r
ae213b7d 796 IN OUT NET_MAP *Map,\r
97b38d4e 797 IN VOID *Key,\r
798 IN VOID *Value OPTIONAL\r
799 );\r
800\r
801/**\r
e9b67286 802 Finds the key in the netmap and returns the point to the item containing the Key.\r
1204fe83 803\r
804 Iterate the Used doubly linked list of the netmap to get every item. Compare the key of every\r
3a1ab4bc 805 item with the key to search. It returns the point to the item contains the Key if found.\r
97b38d4e 806\r
3a1ab4bc 807 If Map is NULL, then ASSERT().\r
1204fe83 808\r
ae213b7d 809 @param[in] Map The netmap to search within.\r
810 @param[in] Key The key to search.\r
97b38d4e 811\r
812 @return The point to the item contains the Key, or NULL if Key isn't in the map.\r
813\r
814**/\r
ae213b7d 815NET_MAP_ITEM *\r
97b38d4e 816EFIAPI\r
817NetMapFindKey (\r
818 IN NET_MAP *Map,\r
819 IN VOID *Key\r
820 );\r
821\r
822/**\r
3a1ab4bc 823 Remove the node entry of the item from the netmap and return the key of the removed item.\r
1204fe83 824\r
825 Remove the node entry of the item from the Used doubly linked list of the netmap.\r
826 The number of the <Key, Value> pairs in the netmap decrease by 1. Then add the node\r
3a1ab4bc 827 entry of the item to the Recycled doubly linked list of the netmap. If Value is not NULL,\r
828 Value will point to the value of the item. It returns the key of the removed item.\r
1204fe83 829\r
3a1ab4bc 830 If Map is NULL, then ASSERT().\r
831 If Item is NULL, then ASSERT().\r
832 if item in not in the netmap, then ASSERT().\r
1204fe83 833\r
ae213b7d 834 @param[in, out] Map The netmap to remove the item from.\r
835 @param[in, out] Item The item to remove.\r
836 @param[out] Value The variable to receive the value if not NULL.\r
97b38d4e 837\r
ae213b7d 838 @return The key of the removed item.\r
97b38d4e 839\r
840**/\r
841VOID *\r
842EFIAPI\r
843NetMapRemoveItem (\r
ae213b7d 844 IN OUT NET_MAP *Map,\r
845 IN OUT NET_MAP_ITEM *Item,\r
846 OUT VOID **Value OPTIONAL\r
97b38d4e 847 );\r
848\r
849/**\r
3a1ab4bc 850 Remove the first node entry on the netmap and return the key of the removed item.\r
97b38d4e 851\r
1204fe83 852 Remove the first node entry from the Used doubly linked list of the netmap.\r
853 The number of the <Key, Value> pairs in the netmap decrease by 1. Then add the node\r
3a1ab4bc 854 entry to the Recycled doubly linked list of the netmap. If parameter Value is not NULL,\r
855 parameter Value will point to the value of the item. It returns the key of the removed item.\r
1204fe83 856\r
3a1ab4bc 857 If Map is NULL, then ASSERT().\r
858 If the Used doubly linked list is empty, then ASSERT().\r
1204fe83 859\r
ae213b7d 860 @param[in, out] Map The netmap to remove the head from.\r
861 @param[out] Value The variable to receive the value if not NULL.\r
97b38d4e 862\r
ae213b7d 863 @return The key of the item removed.\r
97b38d4e 864\r
865**/\r
866VOID *\r
867EFIAPI\r
868NetMapRemoveHead (\r
ae213b7d 869 IN OUT NET_MAP *Map,\r
870 OUT VOID **Value OPTIONAL\r
97b38d4e 871 );\r
872\r
873/**\r
3a1ab4bc 874 Remove the last node entry on the netmap and return the key of the removed item.\r
97b38d4e 875\r
1204fe83 876 Remove the last node entry from the Used doubly linked list of the netmap.\r
877 The number of the <Key, Value> pairs in the netmap decrease by 1. Then add the node\r
3a1ab4bc 878 entry to the Recycled doubly linked list of the netmap. If parameter Value is not NULL,\r
879 parameter Value will point to the value of the item. It returns the key of the removed item.\r
1204fe83 880\r
3a1ab4bc 881 If Map is NULL, then ASSERT().\r
882 If the Used doubly linked list is empty, then ASSERT().\r
1204fe83 883\r
ae213b7d 884 @param[in, out] Map The netmap to remove the tail from.\r
885 @param[out] Value The variable to receive the value if not NULL.\r
97b38d4e 886\r
ae213b7d 887 @return The key of the item removed.\r
97b38d4e 888\r
889**/\r
890VOID *\r
891EFIAPI\r
892NetMapRemoveTail (\r
ae213b7d 893 IN OUT NET_MAP *Map,\r
894 OUT VOID **Value OPTIONAL\r
97b38d4e 895 );\r
896\r
897typedef\r
898EFI_STATUS\r
e798cd87 899(EFIAPI *NET_MAP_CALLBACK) (\r
97b38d4e 900 IN NET_MAP *Map,\r
901 IN NET_MAP_ITEM *Item,\r
902 IN VOID *Arg\r
903 );\r
904\r
905/**\r
3a1ab4bc 906 Iterate through the netmap and call CallBack for each item.\r
1204fe83 907\r
3a1ab4bc 908 It will contiue the traverse if CallBack returns EFI_SUCCESS, otherwise, break\r
1204fe83 909 from the loop. It returns the CallBack's last return value. This function is\r
3a1ab4bc 910 delete safe for the current item.\r
97b38d4e 911\r
3a1ab4bc 912 If Map is NULL, then ASSERT().\r
913 If CallBack is NULL, then ASSERT().\r
1204fe83 914\r
ae213b7d 915 @param[in] Map The Map to iterate through.\r
916 @param[in] CallBack The callback function to call for each item.\r
917 @param[in] Arg The opaque parameter to the callback.\r
97b38d4e 918\r
64a80549 919 @retval EFI_SUCCESS There is no item in the netmap, or CallBack for each item\r
920 returns EFI_SUCCESS.\r
ae213b7d 921 @retval Others It returns the CallBack's last return value.\r
97b38d4e 922\r
923**/\r
924EFI_STATUS\r
925EFIAPI\r
926NetMapIterate (\r
927 IN NET_MAP *Map,\r
928 IN NET_MAP_CALLBACK CallBack,\r
f6b7393c 929 IN VOID *Arg OPTIONAL\r
97b38d4e 930 );\r
931\r
932\r
933//\r
934// Helper functions to implement driver binding and service binding protocols.\r
935//\r
936/**\r
937 Create a child of the service that is identified by ServiceBindingGuid.\r
1204fe83 938\r
3a1ab4bc 939 Get the ServiceBinding Protocol first, then use it to create a child.\r
97b38d4e 940\r
3a1ab4bc 941 If ServiceBindingGuid is NULL, then ASSERT().\r
942 If ChildHandle is NULL, then ASSERT().\r
1204fe83 943\r
ae213b7d 944 @param[in] Controller The controller which has the service installed.\r
945 @param[in] Image The image handle used to open service.\r
946 @param[in] ServiceBindingGuid The service's Guid.\r
e9b67286 947 @param[in, out] ChildHandle The handle to receive the created child.\r
97b38d4e 948\r
e9b67286 949 @retval EFI_SUCCESS The child was successfully created.\r
97b38d4e 950 @retval Others Failed to create the child.\r
951\r
952**/\r
953EFI_STATUS\r
954EFIAPI\r
955NetLibCreateServiceChild (\r
ae213b7d 956 IN EFI_HANDLE Controller,\r
957 IN EFI_HANDLE Image,\r
97b38d4e 958 IN EFI_GUID *ServiceBindingGuid,\r
ae213b7d 959 IN OUT EFI_HANDLE *ChildHandle\r
97b38d4e 960 );\r
961\r
962/**\r
e9b67286 963 Destroy a child of the service that is identified by ServiceBindingGuid.\r
1204fe83 964\r
3a1ab4bc 965 Get the ServiceBinding Protocol first, then use it to destroy a child.\r
1204fe83 966\r
3a1ab4bc 967 If ServiceBindingGuid is NULL, then ASSERT().\r
1204fe83 968\r
ae213b7d 969 @param[in] Controller The controller which has the service installed.\r
970 @param[in] Image The image handle used to open service.\r
971 @param[in] ServiceBindingGuid The service's Guid.\r
e9b67286 972 @param[in] ChildHandle The child to destroy.\r
97b38d4e 973\r
64a80549 974 @retval EFI_SUCCESS The child was destroyed.\r
e9b67286 975 @retval Others Failed to destroy the child.\r
97b38d4e 976\r
977**/\r
978EFI_STATUS\r
979EFIAPI\r
980NetLibDestroyServiceChild (\r
ae213b7d 981 IN EFI_HANDLE Controller,\r
982 IN EFI_HANDLE Image,\r
97b38d4e 983 IN EFI_GUID *ServiceBindingGuid,\r
984 IN EFI_HANDLE ChildHandle\r
985 );\r
986\r
987/**\r
779ae357 988 Get handle with Simple Network Protocol installed on it.\r
989\r
990 There should be MNP Service Binding Protocol installed on the input ServiceHandle.\r
991 If Simple Network Protocol is already installed on the ServiceHandle, the\r
992 ServiceHandle will be returned. If SNP is not installed on the ServiceHandle,\r
993 try to find its parent handle with SNP installed.\r
994\r
995 @param[in] ServiceHandle The handle where network service binding protocols are\r
996 installed on.\r
997 @param[out] Snp The pointer to store the address of the SNP instance.\r
998 This is an optional parameter that may be NULL.\r
999\r
1000 @return The SNP handle, or NULL if not found.\r
1001\r
1002**/\r
1003EFI_HANDLE\r
1004EFIAPI\r
1005NetLibGetSnpHandle (\r
1006 IN EFI_HANDLE ServiceHandle,\r
1007 OUT EFI_SIMPLE_NETWORK_PROTOCOL **Snp OPTIONAL\r
1008 );\r
1009\r
1010/**\r
1011 Retrieve VLAN ID of a VLAN device handle.\r
1012\r
1013 Search VLAN device path node in Device Path of specified ServiceHandle and\r
1014 return its VLAN ID. If no VLAN device path node found, then this ServiceHandle\r
1015 is not a VLAN device handle, and 0 will be returned.\r
1016\r
1017 @param[in] ServiceHandle The handle where network service binding protocols are\r
1018 installed on.\r
1019\r
1020 @return VLAN ID of the device handle, or 0 if not a VLAN device.\r
97b38d4e 1021\r
779ae357 1022**/\r
1023UINT16\r
1024EFIAPI\r
1025NetLibGetVlanId (\r
1026 IN EFI_HANDLE ServiceHandle\r
1027 );\r
1028\r
1029/**\r
1030 Find VLAN device handle with specified VLAN ID.\r
1031\r
1032 The VLAN child device handle is created by VLAN Config Protocol on ControllerHandle.\r
1033 This function will append VLAN device path node to the parent device path,\r
1034 and then use LocateDevicePath() to find the correct VLAN device handle.\r
1035\r
e2851998 1036 @param[in] ControllerHandle The handle where network service binding protocols are\r
779ae357 1037 installed on.\r
e2851998 1038 @param[in] VlanId The configured VLAN ID for the VLAN device.\r
779ae357 1039\r
1040 @return The VLAN device handle, or NULL if not found.\r
1041\r
1042**/\r
1043EFI_HANDLE\r
1044EFIAPI\r
1045NetLibGetVlanHandle (\r
1046 IN EFI_HANDLE ControllerHandle,\r
1047 IN UINT16 VlanId\r
1048 );\r
1049\r
1050/**\r
1051 Get MAC address associated with the network service handle.\r
1052\r
1053 There should be MNP Service Binding Protocol installed on the input ServiceHandle.\r
1054 If SNP is installed on the ServiceHandle or its parent handle, MAC address will\r
1055 be retrieved from SNP. If no SNP found, try to get SNP mode data use MNP.\r
1056\r
1057 @param[in] ServiceHandle The handle where network service binding protocols are\r
1058 installed on.\r
1059 @param[out] MacAddress The pointer to store the returned MAC address.\r
1060 @param[out] AddressSize The length of returned MAC address.\r
1061\r
64a80549 1062 @retval EFI_SUCCESS MAC address was returned successfully.\r
779ae357 1063 @retval Others Failed to get SNP mode data.\r
1064\r
1065**/\r
1066EFI_STATUS\r
1067EFIAPI\r
1068NetLibGetMacAddress (\r
1069 IN EFI_HANDLE ServiceHandle,\r
1070 OUT EFI_MAC_ADDRESS *MacAddress,\r
1071 OUT UINTN *AddressSize\r
1072 );\r
1073\r
1074/**\r
1075 Convert MAC address of the NIC associated with specified Service Binding Handle\r
1076 to a unicode string. Callers are responsible for freeing the string storage.\r
3a1ab4bc 1077\r
779ae357 1078 Locate simple network protocol associated with the Service Binding Handle and\r
1079 get the mac address from SNP. Then convert the mac address into a unicode\r
1080 string. It takes 2 unicode characters to represent a 1 byte binary buffer.\r
1081 Plus one unicode character for the null-terminator.\r
3a1ab4bc 1082\r
779ae357 1083 @param[in] ServiceHandle The handle where network service binding protocol is\r
64a80549 1084 installed.\r
779ae357 1085 @param[in] ImageHandle The image handle used to act as the agent handle to\r
ae213b7d 1086 get the simple network protocol.\r
1087 @param[out] MacString The pointer to store the address of the string\r
1088 representation of the mac address.\r
1204fe83 1089\r
64a80549 1090 @retval EFI_SUCCESS Converted the mac address a unicode string successfully.\r
1091 @retval EFI_OUT_OF_RESOURCES There are not enough memory resources.\r
ae213b7d 1092 @retval Others Failed to open the simple network protocol.\r
97b38d4e 1093\r
1094**/\r
1095EFI_STATUS\r
1096EFIAPI\r
1097NetLibGetMacString (\r
779ae357 1098 IN EFI_HANDLE ServiceHandle,\r
ae213b7d 1099 IN EFI_HANDLE ImageHandle,\r
1100 OUT CHAR16 **MacString\r
97b38d4e 1101 );\r
1102\r
dd29f3ed 1103/**\r
1104 Detect media status for specified network device.\r
1105\r
1106 The underlying UNDI driver may or may not support reporting media status from\r
1107 GET_STATUS command (PXE_STATFLAGS_GET_STATUS_NO_MEDIA_SUPPORTED). This routine\r
3b1464d5 1108 will try to invoke Snp->GetStatus() to get the media status. If media is already\r
1109 present, it returns directly. If media is not present, it will stop SNP and then\r
1110 restart SNP to get the latest media status. This provides an opportunity to get \r
64a80549 1111 the correct media status for old UNDI driver, which doesn't support reporting \r
1112 media status from GET_STATUS command.\r
1113 Note: there are two limitations for the current algorithm:\r
1114 1) For UNDI with this capability, when the cable is not attached, there will\r
1115 be an redundant Stop/Start() process.\r
3b1464d5 1116 2) for UNDI without this capability, in case that network cable is attached when\r
1117 Snp->Initialize() is invoked while network cable is unattached later,\r
1118 NetLibDetectMedia() will report MediaPresent as TRUE, causing upper layer\r
1119 apps to wait for timeout time.\r
dd29f3ed 1120\r
1121 @param[in] ServiceHandle The handle where network service binding protocols are\r
64a80549 1122 installed.\r
dd29f3ed 1123 @param[out] MediaPresent The pointer to store the media status.\r
1124\r
1125 @retval EFI_SUCCESS Media detection success.\r
64a80549 1126 @retval EFI_INVALID_PARAMETER ServiceHandle is not a valid network device handle.\r
1127 @retval EFI_UNSUPPORTED The network device does not support media detection.\r
1128 @retval EFI_DEVICE_ERROR SNP is in an unknown state.\r
dd29f3ed 1129\r
1130**/\r
1131EFI_STATUS\r
1132EFIAPI\r
1133NetLibDetectMedia (\r
1134 IN EFI_HANDLE ServiceHandle,\r
1135 OUT BOOLEAN *MediaPresent\r
1136 );\r
1137\r
97b38d4e 1138/**\r
1139 Create an IPv4 device path node.\r
1204fe83 1140\r
3a1ab4bc 1141 The header type of IPv4 device path node is MESSAGING_DEVICE_PATH.\r
1142 The header subtype of IPv4 device path node is MSG_IPv4_DP.\r
1143 The length of the IPv4 device path node in bytes is 19.\r
64a80549 1144 Get other information from parameters to make up the whole IPv4 device path node.\r
97b38d4e 1145\r
64a80549 1146 @param[in, out] Node The pointer to the IPv4 device path node.\r
f6b7393c 1147 @param[in] Controller The controller handle.\r
ae213b7d 1148 @param[in] LocalIp The local IPv4 address.\r
1149 @param[in] LocalPort The local port.\r
1150 @param[in] RemoteIp The remote IPv4 address.\r
1151 @param[in] RemotePort The remote port.\r
1152 @param[in] Protocol The protocol type in the IP header.\r
1153 @param[in] UseDefaultAddress Whether this instance is using default address or not.\r
97b38d4e 1154\r
97b38d4e 1155**/\r
1156VOID\r
1157EFIAPI\r
1158NetLibCreateIPv4DPathNode (\r
1159 IN OUT IPv4_DEVICE_PATH *Node,\r
1160 IN EFI_HANDLE Controller,\r
1161 IN IP4_ADDR LocalIp,\r
1162 IN UINT16 LocalPort,\r
1163 IN IP4_ADDR RemoteIp,\r
1164 IN UINT16 RemotePort,\r
1165 IN UINT16 Protocol,\r
1166 IN BOOLEAN UseDefaultAddress\r
1167 );\r
1168\r
f6b7393c 1169/**\r
1170 Create an IPv6 device path node.\r
1204fe83 1171\r
f6b7393c 1172 The header type of IPv6 device path node is MESSAGING_DEVICE_PATH.\r
1173 The header subtype of IPv6 device path node is MSG_IPv6_DP.\r
1174 The length of the IPv6 device path node in bytes is 43.\r
64a80549 1175 Get other information from parameters to make up the whole IPv6 device path node.\r
f6b7393c 1176\r
64a80549 1177 @param[in, out] Node The pointer to the IPv6 device path node.\r
f6b7393c 1178 @param[in] Controller The controller handle.\r
1179 @param[in] LocalIp The local IPv6 address.\r
1180 @param[in] LocalPort The local port.\r
1181 @param[in] RemoteIp The remote IPv6 address.\r
1182 @param[in] RemotePort The remote port.\r
1183 @param[in] Protocol The protocol type in the IP header.\r
1184\r
1185**/\r
1186VOID\r
1187EFIAPI\r
1188NetLibCreateIPv6DPathNode (\r
1189 IN OUT IPv6_DEVICE_PATH *Node,\r
1190 IN EFI_HANDLE Controller,\r
1191 IN EFI_IPv6_ADDRESS *LocalIp,\r
1192 IN UINT16 LocalPort,\r
1193 IN EFI_IPv6_ADDRESS *RemoteIp,\r
1194 IN UINT16 RemotePort,\r
1195 IN UINT16 Protocol\r
1196 );\r
1197\r
1198\r
97b38d4e 1199/**\r
1200 Find the UNDI/SNP handle from controller and protocol GUID.\r
1204fe83 1201\r
e9b67286 1202 For example, IP will open an MNP child to transmit/receive\r
1203 packets. When MNP is stopped, IP should also be stopped. IP\r
64a80549 1204 needs to find its own private data that is related the IP's\r
1205 service binding instance that is installed on the UNDI/SNP handle.\r
1206 The controller is then either an MNP or an ARP child handle. Note that\r
1207 IP opens these handles using BY_DRIVER. Use that infomation to get the\r
97b38d4e 1208 UNDI/SNP handle.\r
1209\r
64a80549 1210 @param[in] Controller The protocol handle to check.\r
ae213b7d 1211 @param[in] ProtocolGuid The protocol that is related with the handle.\r
97b38d4e 1212\r
ae213b7d 1213 @return The UNDI/SNP handle or NULL for errors.\r
97b38d4e 1214\r
1215**/\r
1216EFI_HANDLE\r
1217EFIAPI\r
1218NetLibGetNicHandle (\r
1219 IN EFI_HANDLE Controller,\r
1220 IN EFI_GUID *ProtocolGuid\r
1221 );\r
1222\r
97b38d4e 1223/**\r
1224 This is the default unload handle for all the network drivers.\r
1225\r
3a1ab4bc 1226 Disconnect the driver specified by ImageHandle from all the devices in the handle database.\r
1227 Uninstall all the protocols installed in the driver entry point.\r
1204fe83 1228\r
ae213b7d 1229 @param[in] ImageHandle The drivers' driver image.\r
97b38d4e 1230\r
1231 @retval EFI_SUCCESS The image is unloaded.\r
1232 @retval Others Failed to unload the image.\r
1233\r
1234**/\r
1235EFI_STATUS\r
1236EFIAPI\r
1237NetLibDefaultUnload (\r
1238 IN EFI_HANDLE ImageHandle\r
1239 );\r
1240\r
e4ef0031 1241/**\r
1242 Convert one Null-terminated ASCII string (decimal dotted) to EFI_IPv4_ADDRESS.\r
1243\r
1244 @param[in] String The pointer to the Ascii string.\r
1245 @param[out] Ip4Address The pointer to the converted IPv4 address.\r
1246\r
64a80549 1247 @retval EFI_SUCCESS Converted to an IPv4 address successfully.\r
1248 @retval EFI_INVALID_PARAMETER The string is malformated, or Ip4Address is NULL.\r
e4ef0031 1249\r
1250**/\r
1251EFI_STATUS\r
e798cd87 1252EFIAPI\r
e4ef0031 1253NetLibAsciiStrToIp4 (\r
1254 IN CONST CHAR8 *String,\r
1255 OUT EFI_IPv4_ADDRESS *Ip4Address\r
1256 );\r
1257\r
1258/**\r
1259 Convert one Null-terminated ASCII string to EFI_IPv6_ADDRESS. The format of the\r
1260 string is defined in RFC 4291 - Text Pepresentation of Addresses.\r
1261\r
1262 @param[in] String The pointer to the Ascii string.\r
1263 @param[out] Ip6Address The pointer to the converted IPv6 address.\r
1264\r
64a80549 1265 @retval EFI_SUCCESS Converted to an IPv6 address successfully.\r
1266 @retval EFI_INVALID_PARAMETER The string is malformated, or Ip6Address is NULL.\r
e4ef0031 1267\r
1268**/\r
1269EFI_STATUS\r
e798cd87 1270EFIAPI\r
e4ef0031 1271NetLibAsciiStrToIp6 (\r
1272 IN CONST CHAR8 *String,\r
1273 OUT EFI_IPv6_ADDRESS *Ip6Address\r
1274 );\r
1275\r
1276/**\r
1277 Convert one Null-terminated Unicode string (decimal dotted) to EFI_IPv4_ADDRESS.\r
1278\r
1279 @param[in] String The pointer to the Ascii string.\r
1280 @param[out] Ip4Address The pointer to the converted IPv4 address.\r
1281\r
64a80549 1282 @retval EFI_SUCCESS Converted to an IPv4 address successfully.\r
e4ef0031 1283 @retval EFI_INVALID_PARAMETER The string is mal-formated or Ip4Address is NULL.\r
64a80549 1284 @retval EFI_OUT_OF_RESOURCES Failed to perform the operation due to lack of resources.\r
e4ef0031 1285\r
1286**/\r
1287EFI_STATUS\r
e798cd87 1288EFIAPI\r
e4ef0031 1289NetLibStrToIp4 (\r
1290 IN CONST CHAR16 *String,\r
1291 OUT EFI_IPv4_ADDRESS *Ip4Address\r
1292 );\r
1293\r
1294/**\r
1295 Convert one Null-terminated Unicode string to EFI_IPv6_ADDRESS. The format of\r
1296 the string is defined in RFC 4291 - Text Pepresentation of Addresses.\r
1297\r
1298 @param[in] String The pointer to the Ascii string.\r
1299 @param[out] Ip6Address The pointer to the converted IPv6 address.\r
1300\r
64a80549 1301 @retval EFI_SUCCESS Converted to an IPv6 address successfully.\r
1302 @retval EFI_INVALID_PARAMETER The string is malformated or Ip6Address is NULL.\r
1303 @retval EFI_OUT_OF_RESOURCES Failed to perform the operation due to a lack of resources.\r
e4ef0031 1304\r
1305**/\r
1306EFI_STATUS\r
e798cd87 1307EFIAPI\r
e4ef0031 1308NetLibStrToIp6 (\r
1309 IN CONST CHAR16 *String,\r
1310 OUT EFI_IPv6_ADDRESS *Ip6Address\r
1311 );\r
1312\r
1313/**\r
1314 Convert one Null-terminated Unicode string to EFI_IPv6_ADDRESS and prefix length.\r
1315 The format of the string is defined in RFC 4291 - Text Pepresentation of Addresses\r
1316 Prefixes: ipv6-address/prefix-length.\r
1317\r
1318 @param[in] String The pointer to the Ascii string.\r
1319 @param[out] Ip6Address The pointer to the converted IPv6 address.\r
1320 @param[out] PrefixLength The pointer to the converted prefix length.\r
1321\r
64a80549 1322 @retval EFI_SUCCESS Converted to an IPv6 address successfully.\r
1323 @retval EFI_INVALID_PARAMETER The string is malformated, or Ip6Address is NULL.\r
1324 @retval EFI_OUT_OF_RESOURCES Failed to perform the operation due to a lack of resources.\r
e4ef0031 1325\r
1326**/\r
1327EFI_STATUS\r
e798cd87 1328EFIAPI\r
e4ef0031 1329NetLibStrToIp6andPrefix (\r
1330 IN CONST CHAR16 *String,\r
1331 OUT EFI_IPv6_ADDRESS *Ip6Address,\r
1332 OUT UINT8 *PrefixLength\r
1333 );\r
b45b45b2 1334\r
1335//\r
e4ef0031 1336// Various signatures\r
b45b45b2 1337//\r
1338#define NET_BUF_SIGNATURE SIGNATURE_32 ('n', 'b', 'u', 'f')\r
1339#define NET_VECTOR_SIGNATURE SIGNATURE_32 ('n', 'v', 'e', 'c')\r
1340#define NET_QUE_SIGNATURE SIGNATURE_32 ('n', 'b', 'q', 'u')\r
97b38d4e 1341\r
1342\r
b45b45b2 1343#define NET_PROTO_DATA 64 // Opaque buffer for protocols\r
1344#define NET_BUF_HEAD 1 // Trim or allocate space from head\r
1345#define NET_BUF_TAIL 0 // Trim or allocate space from tail\r
1346#define NET_VECTOR_OWN_FIRST 0x01 // We allocated the 1st block in the vector\r
97b38d4e 1347\r
1348#define NET_CHECK_SIGNATURE(PData, SIGNATURE) \\r
1349 ASSERT (((PData) != NULL) && ((PData)->Signature == (SIGNATURE)))\r
1350\r
97b38d4e 1351//\r
1352// Single memory block in the vector.\r
1353//\r
1354typedef struct {\r
1355 UINT32 Len; // The block's length\r
1356 UINT8 *Bulk; // The block's Data\r
1357} NET_BLOCK;\r
1358\r
e798cd87 1359typedef VOID (EFIAPI *NET_VECTOR_EXT_FREE) (VOID *Arg);\r
97b38d4e 1360\r
1361//\r
1362//NET_VECTOR contains several blocks to hold all packet's\r
1363//fragments and other house-keeping stuff for sharing. It\r
1364//doesn't specify the where actual packet fragment begins.\r
1365//\r
1366typedef struct {\r
1367 UINT32 Signature;\r
1368 INTN RefCnt; // Reference count to share NET_VECTOR.\r
1369 NET_VECTOR_EXT_FREE Free; // external function to free NET_VECTOR\r
1370 VOID *Arg; // opeque argument to Free\r
1371 UINT32 Flag; // Flags, NET_VECTOR_OWN_FIRST\r
1372 UINT32 Len; // Total length of the assocated BLOCKs\r
1373\r
1374 UINT32 BlockNum;\r
1375 NET_BLOCK Block[1];\r
1376} NET_VECTOR;\r
1377\r
1378//\r
e9b67286 1379//NET_BLOCK_OP operates on the NET_BLOCK. It specifies\r
1380//where the actual fragment begins and ends\r
97b38d4e 1381//\r
1382typedef struct {\r
1383 UINT8 *BlockHead; // Block's head, or the smallest valid Head\r
1384 UINT8 *BlockTail; // Block's tail. BlockTail-BlockHead=block length\r
1385 UINT8 *Head; // 1st byte of the data in the block\r
1386 UINT8 *Tail; // Tail of the data in the block, Tail-Head=Size\r
1387 UINT32 Size; // The size of the data\r
1388} NET_BLOCK_OP;\r
1389\r
f6b7393c 1390typedef union {\r
1391 IP4_HEAD *Ip4;\r
1392 EFI_IP6_HEADER *Ip6;\r
1393} NET_IP_HEAD;\r
97b38d4e 1394\r
1395//\r
1396//NET_BUF is the buffer manage structure used by the\r
e9b67286 1397//network stack. Every network packet may be fragmented. The Vector points to\r
1398//memory blocks used by each fragment, and BlockOp\r
97b38d4e 1399//specifies where each fragment begins and ends.\r
1400//\r
e9b67286 1401//It also contains an opaque area for the protocol to store\r
1402//per-packet information. Protocol must be careful not\r
97b38d4e 1403//to overwrite the members after that.\r
1404//\r
1405typedef struct {\r
f6b7393c 1406 UINT32 Signature;\r
1407 INTN RefCnt;\r
1408 LIST_ENTRY List; // The List this NET_BUF is on\r
97b38d4e 1409\r
f6b7393c 1410 NET_IP_HEAD Ip; // Network layer header, for fast access\r
1411 TCP_HEAD *Tcp; // Transport layer header, for fast access\r
1412 EFI_UDP_HEADER *Udp; // User Datagram Protocol header\r
1413 UINT8 ProtoData [NET_PROTO_DATA]; //Protocol specific data\r
97b38d4e 1414\r
f6b7393c 1415 NET_VECTOR *Vector; // The vector containing the packet\r
97b38d4e 1416\r
f6b7393c 1417 UINT32 BlockOpNum; // Total number of BlockOp in the buffer\r
1418 UINT32 TotalSize; // Total size of the actual packet\r
1419 NET_BLOCK_OP BlockOp[1]; // Specify the position of actual packet\r
97b38d4e 1420} NET_BUF;\r
1421\r
97b38d4e 1422//\r
e9b67286 1423//A queue of NET_BUFs. It is a thin extension of\r
97b38d4e 1424//NET_BUF functions.\r
1425//\r
1426typedef struct {\r
1427 UINT32 Signature;\r
1428 INTN RefCnt;\r
1429 LIST_ENTRY List; // The List this buffer queue is on\r
1430\r
1431 LIST_ENTRY BufList; // list of queued buffers\r
1432 UINT32 BufSize; // total length of DATA in the buffers\r
1433 UINT32 BufNum; // total number of buffers on the chain\r
1434} NET_BUF_QUEUE;\r
1435\r
1436//\r
1437// Pseudo header for TCP and UDP checksum\r
1438//\r
1439#pragma pack(1)\r
1440typedef struct {\r
1441 IP4_ADDR SrcIp;\r
1442 IP4_ADDR DstIp;\r
1443 UINT8 Reserved;\r
1444 UINT8 Protocol;\r
1445 UINT16 Len;\r
1446} NET_PSEUDO_HDR;\r
f6b7393c 1447\r
1448typedef struct {\r
1449 EFI_IPv6_ADDRESS SrcIp;\r
1450 EFI_IPv6_ADDRESS DstIp;\r
1451 UINT32 Len;\r
1452 UINT32 Reserved:24;\r
1453 UINT32 NextHeader:8;\r
1454} NET_IP6_PSEUDO_HDR;\r
97b38d4e 1455#pragma pack()\r
1456\r
1457//\r
1458// The fragment entry table used in network interfaces. This is\r
1459// the same as NET_BLOCK now. Use two different to distinguish\r
1460// the two in case that NET_BLOCK be enhanced later.\r
1461//\r
1462typedef struct {\r
1463 UINT32 Len;\r
1464 UINT8 *Bulk;\r
1465} NET_FRAGMENT;\r
1466\r
1467#define NET_GET_REF(PData) ((PData)->RefCnt++)\r
1468#define NET_PUT_REF(PData) ((PData)->RefCnt--)\r
50d7ebad 1469#define NETBUF_FROM_PROTODATA(Info) BASE_CR((Info), NET_BUF, ProtoData)\r
97b38d4e 1470\r
1471#define NET_BUF_SHARED(Buf) \\r
1472 (((Buf)->RefCnt > 1) || ((Buf)->Vector->RefCnt > 1))\r
1473\r
1474#define NET_VECTOR_SIZE(BlockNum) \\r
1475 (sizeof (NET_VECTOR) + ((BlockNum) - 1) * sizeof (NET_BLOCK))\r
1476\r
1477#define NET_BUF_SIZE(BlockOpNum) \\r
1478 (sizeof (NET_BUF) + ((BlockOpNum) - 1) * sizeof (NET_BLOCK_OP))\r
1479\r
1480#define NET_HEADSPACE(BlockOp) \\r
1481 (UINTN)((BlockOp)->Head - (BlockOp)->BlockHead)\r
1482\r
1483#define NET_TAILSPACE(BlockOp) \\r
1484 (UINTN)((BlockOp)->BlockTail - (BlockOp)->Tail)\r
1485\r
1486/**\r
1487 Allocate a single block NET_BUF. Upon allocation, all the\r
1488 free space is in the tail room.\r
1489\r
ae213b7d 1490 @param[in] Len The length of the block.\r
97b38d4e 1491\r
64a80549 1492 @return The pointer to the allocated NET_BUF, or NULL if the\r
1493 allocation failed due to resource limitations.\r
97b38d4e 1494\r
1495**/\r
1496NET_BUF *\r
1497EFIAPI\r
1498NetbufAlloc (\r
1499 IN UINT32 Len\r
1500 );\r
1501\r
1502/**\r
7557df4d 1503 Free the net buffer and its associated NET_VECTOR.\r
1204fe83 1504\r
7557df4d 1505 Decrease the reference count of the net buffer by one. Free the associated net\r
1204fe83 1506 vector and itself if the reference count of the net buffer is decreased to 0.\r
1507 The net vector free operation decreases the reference count of the net\r
e9b67286 1508 vector by one, and performs the resource free operation when the reference count\r
1204fe83 1509 of the net vector is 0.\r
1510\r
64a80549 1511 @param[in] Nbuf The pointer to the NET_BUF to be freed.\r
97b38d4e 1512\r
1513**/\r
1514VOID\r
1515EFIAPI\r
1516NetbufFree (\r
1517 IN NET_BUF *Nbuf\r
1518 );\r
1519\r
1520/**\r
1204fe83 1521 Get the index of NET_BLOCK_OP that contains the byte at Offset in the net\r
1522 buffer.\r
1523\r
1524 For example, this function can be used to retrieve the IP header in the packet. It\r
1525 also can be used to get the fragment that contains the byte used\r
1526 mainly by the library implementation itself.\r
97b38d4e 1527\r
64a80549 1528 @param[in] Nbuf The pointer to the net buffer.\r
7557df4d 1529 @param[in] Offset The offset of the byte.\r
1204fe83 1530 @param[out] Index Index of the NET_BLOCK_OP that contains the byte at\r
7557df4d 1531 Offset.\r
97b38d4e 1532\r
64a80549 1533 @return The pointer to the Offset'th byte of data in the net buffer, or NULL\r
7557df4d 1534 if there is no such data in the net buffer.\r
97b38d4e 1535\r
1536**/\r
1537UINT8 *\r
1538EFIAPI\r
1539NetbufGetByte (\r
1540 IN NET_BUF *Nbuf,\r
1541 IN UINT32 Offset,\r
ae213b7d 1542 OUT UINT32 *Index OPTIONAL\r
97b38d4e 1543 );\r
1544\r
1545/**\r
1204fe83 1546 Create a copy of the net buffer that shares the associated net vector.\r
1547\r
1548 The reference count of the newly created net buffer is set to 1. The reference\r
1549 count of the associated net vector is increased by one.\r
97b38d4e 1550\r
64a80549 1551 @param[in] Nbuf The pointer to the net buffer to be cloned.\r
97b38d4e 1552\r
64a80549 1553 @return The pointer to the cloned net buffer, or NULL if the\r
1554 allocation failed due to resource limitations.\r
97b38d4e 1555\r
1556**/\r
7557df4d 1557NET_BUF *\r
97b38d4e 1558EFIAPI\r
1559NetbufClone (\r
1560 IN NET_BUF *Nbuf\r
1561 );\r
1562\r
1563/**\r
7557df4d 1564 Create a duplicated copy of the net buffer with data copied and HeadSpace\r
1565 bytes of head space reserved.\r
1204fe83 1566\r
7557df4d 1567 The duplicated net buffer will allocate its own memory to hold the data of the\r
1568 source net buffer.\r
1204fe83 1569\r
64a80549 1570 @param[in] Nbuf The pointer to the net buffer to be duplicated from.\r
1571 @param[in, out] Duplicate The pointer to the net buffer to duplicate to. If\r
1572 NULL, a new net buffer is allocated.\r
1573 @param[in] HeadSpace The length of the head space to reserve.\r
7557df4d 1574\r
64a80549 1575 @return The pointer to the duplicated net buffer, or NULL if\r
1576 the allocation failed due to resource limitations.\r
97b38d4e 1577\r
1578**/\r
1579NET_BUF *\r
1580EFIAPI\r
1581NetbufDuplicate (\r
1582 IN NET_BUF *Nbuf,\r
ae213b7d 1583 IN OUT NET_BUF *Duplicate OPTIONAL,\r
97b38d4e 1584 IN UINT32 HeadSpace\r
1585 );\r
1586\r
1587/**\r
1204fe83 1588 Create a NET_BUF structure which contains Len byte data of Nbuf starting from\r
1589 Offset.\r
1590\r
1591 A new NET_BUF structure will be created but the associated data in NET_VECTOR\r
64a80549 1592 is shared. This function exists to perform IP packet fragmentation.\r
7557df4d 1593\r
64a80549 1594 @param[in] Nbuf The pointer to the net buffer to be extracted.\r
1204fe83 1595 @param[in] Offset Starting point of the data to be included in the new\r
7557df4d 1596 net buffer.\r
64a80549 1597 @param[in] Len The bytes of data to be included in the new net buffer.\r
1598 @param[in] HeadSpace The bytes of the head space to reserve for the protocol header.\r
7557df4d 1599\r
64a80549 1600 @return The pointer to the cloned net buffer, or NULL if the\r
1601 allocation failed due to resource limitations.\r
97b38d4e 1602\r
1603**/\r
1604NET_BUF *\r
1605EFIAPI\r
1606NetbufGetFragment (\r
1607 IN NET_BUF *Nbuf,\r
1608 IN UINT32 Offset,\r
1609 IN UINT32 Len,\r
1610 IN UINT32 HeadSpace\r
1611 );\r
1612\r
1613/**\r
7557df4d 1614 Reserve some space in the header room of the net buffer.\r
1615\r
1204fe83 1616 Upon allocation, all the space is in the tail room of the buffer. Call this\r
64a80549 1617 function to move space to the header room. This function is quite limited\r
1204fe83 1618 in that it can only reserve space from the first block of an empty NET_BUF not\r
64a80549 1619 built from the external. However, it should be enough for the network stack.\r
97b38d4e 1620\r
64a80549 1621 @param[in, out] Nbuf The pointer to the net buffer.\r
7557df4d 1622 @param[in] Len The length of buffer to be reserved from the header.\r
97b38d4e 1623\r
1624**/\r
1625VOID\r
1626EFIAPI\r
1627NetbufReserve (\r
ae213b7d 1628 IN OUT NET_BUF *Nbuf,\r
97b38d4e 1629 IN UINT32 Len\r
1630 );\r
1631\r
1632/**\r
1204fe83 1633 Allocate Len bytes of space from the header or tail of the buffer.\r
97b38d4e 1634\r
64a80549 1635 @param[in, out] Nbuf The pointer to the net buffer.\r
7557df4d 1636 @param[in] Len The length of the buffer to be allocated.\r
64a80549 1637 @param[in] FromHead The flag to indicate whether to reserve the data\r
7557df4d 1638 from head (TRUE) or tail (FALSE).\r
97b38d4e 1639\r
64a80549 1640 @return The pointer to the first byte of the allocated buffer,\r
1641 or NULL, if there is no sufficient space.\r
97b38d4e 1642\r
1643**/\r
7557df4d 1644UINT8*\r
97b38d4e 1645EFIAPI\r
1646NetbufAllocSpace (\r
ae213b7d 1647 IN OUT NET_BUF *Nbuf,\r
97b38d4e 1648 IN UINT32 Len,\r
1649 IN BOOLEAN FromHead\r
1650 );\r
1651\r
1652/**\r
64a80549 1653 Trim Len bytes from the header or the tail of the net buffer.\r
97b38d4e 1654\r
64a80549 1655 @param[in, out] Nbuf The pointer to the net buffer.\r
7557df4d 1656 @param[in] Len The length of the data to be trimmed.\r
64a80549 1657 @param[in] FromHead The flag to indicate whether trim data is from the \r
1658 head (TRUE) or the tail (FALSE).\r
97b38d4e 1659\r
64a80549 1660 @return The length of the actual trimmed data, which may be less\r
e9b67286 1661 than Len if the TotalSize of Nbuf is less than Len.\r
97b38d4e 1662\r
1663**/\r
1664UINT32\r
1665EFIAPI\r
1666NetbufTrim (\r
ae213b7d 1667 IN OUT NET_BUF *Nbuf,\r
97b38d4e 1668 IN UINT32 Len,\r
1669 IN BOOLEAN FromHead\r
1670 );\r
1671\r
1672/**\r
1204fe83 1673 Copy Len bytes of data from the specific offset of the net buffer to the\r
7557df4d 1674 destination memory.\r
1204fe83 1675\r
e9b67286 1676 The Len bytes of data may cross several fragments of the net buffer.\r
1204fe83 1677\r
64a80549 1678 @param[in] Nbuf The pointer to the net buffer.\r
7557df4d 1679 @param[in] Offset The sequence number of the first byte to copy.\r
64a80549 1680 @param[in] Len The length of the data to copy.\r
7557df4d 1681 @param[in] Dest The destination of the data to copy to.\r
1682\r
1683 @return The length of the actual copied data, or 0 if the offset\r
361468ed 1684 specified exceeds the total size of net buffer.\r
97b38d4e 1685\r
1686**/\r
1687UINT32\r
1688EFIAPI\r
1689NetbufCopy (\r
1690 IN NET_BUF *Nbuf,\r
1691 IN UINT32 Offset,\r
1692 IN UINT32 Len,\r
1693 IN UINT8 *Dest\r
1694 );\r
1695\r
1696/**\r
1204fe83 1697 Build a NET_BUF from external blocks.\r
1698\r
e9b67286 1699 A new NET_BUF structure will be created from external blocks. An additional block\r
7557df4d 1700 of memory will be allocated to hold reserved HeadSpace bytes of header room\r
e9b67286 1701 and existing HeadLen bytes of header, but the external blocks are shared by the\r
7557df4d 1702 net buffer to avoid data copying.\r
97b38d4e 1703\r
64a80549 1704 @param[in] ExtFragment The pointer to the data block.\r
7557df4d 1705 @param[in] ExtNum The number of the data blocks.\r
ae213b7d 1706 @param[in] HeadSpace The head space to be reserved.\r
e9b67286 1707 @param[in] HeadLen The length of the protocol header. The function\r
1708 pulls this amount of data into a linear block.\r
64a80549 1709 @param[in] ExtFree The pointer to the caller-provided free function.\r
ae213b7d 1710 @param[in] Arg The argument passed to ExtFree when ExtFree is\r
1711 called.\r
97b38d4e 1712\r
64a80549 1713 @return The pointer to the net buffer built from the data blocks,\r
7557df4d 1714 or NULL if the allocation failed due to resource\r
1715 limit.\r
97b38d4e 1716\r
1717**/\r
1718NET_BUF *\r
1719EFIAPI\r
1720NetbufFromExt (\r
1721 IN NET_FRAGMENT *ExtFragment,\r
1722 IN UINT32 ExtNum,\r
1723 IN UINT32 HeadSpace,\r
1724 IN UINT32 HeadLen,\r
1725 IN NET_VECTOR_EXT_FREE ExtFree,\r
1726 IN VOID *Arg OPTIONAL\r
1727 );\r
1728\r
1729/**\r
7557df4d 1730 Build a fragment table to contain the fragments in the net buffer. This is the\r
1204fe83 1731 opposite operation of the NetbufFromExt.\r
1732\r
64a80549 1733 @param[in] Nbuf Points to the net buffer.\r
1734 @param[in, out] ExtFragment The pointer to the data block.\r
7557df4d 1735 @param[in, out] ExtNum The number of the data blocks.\r
97b38d4e 1736\r
1204fe83 1737 @retval EFI_BUFFER_TOO_SMALL The number of non-empty blocks is bigger than\r
7557df4d 1738 ExtNum.\r
64a80549 1739 @retval EFI_SUCCESS The fragment table was built successfully.\r
97b38d4e 1740\r
1741**/\r
1742EFI_STATUS\r
1743EFIAPI\r
1744NetbufBuildExt (\r
1745 IN NET_BUF *Nbuf,\r
ae213b7d 1746 IN OUT NET_FRAGMENT *ExtFragment,\r
1747 IN OUT UINT32 *ExtNum\r
97b38d4e 1748 );\r
1749\r
1750/**\r
7557df4d 1751 Build a net buffer from a list of net buffers.\r
1204fe83 1752\r
64a80549 1753 All the fragments will be collected from the list of NEW_BUF, and then a new\r
1204fe83 1754 net buffer will be created through NetbufFromExt.\r
1755\r
7557df4d 1756 @param[in] BufList A List of the net buffer.\r
1757 @param[in] HeadSpace The head space to be reserved.\r
e9b67286 1758 @param[in] HeaderLen The length of the protocol header. The function\r
1759 pulls this amount of data into a linear block.\r
64a80549 1760 @param[in] ExtFree The pointer to the caller provided free function.\r
7557df4d 1761 @param[in] Arg The argument passed to ExtFree when ExtFree is called.\r
1762\r
64a80549 1763 @return The pointer to the net buffer built from the list of net\r
7557df4d 1764 buffers.\r
97b38d4e 1765\r
1766**/\r
1767NET_BUF *\r
1768EFIAPI\r
1769NetbufFromBufList (\r
1770 IN LIST_ENTRY *BufList,\r
1771 IN UINT32 HeadSpace,\r
1772 IN UINT32 HeaderLen,\r
1773 IN NET_VECTOR_EXT_FREE ExtFree,\r
ae213b7d 1774 IN VOID *Arg OPTIONAL\r
97b38d4e 1775 );\r
1776\r
1777/**\r
1778 Free a list of net buffers.\r
1779\r
64a80549 1780 @param[in, out] Head The pointer to the head of linked net buffers.\r
97b38d4e 1781\r
1782**/\r
1783VOID\r
1784EFIAPI\r
1785NetbufFreeList (\r
ae213b7d 1786 IN OUT LIST_ENTRY *Head\r
97b38d4e 1787 );\r
1788\r
1789/**\r
1790 Initiate the net buffer queue.\r
1791\r
64a80549 1792 @param[in, out] NbufQue The pointer to the net buffer queue to be initialized.\r
97b38d4e 1793\r
1794**/\r
1795VOID\r
1796EFIAPI\r
1797NetbufQueInit (\r
ae213b7d 1798 IN OUT NET_BUF_QUEUE *NbufQue\r
97b38d4e 1799 );\r
1800\r
1801/**\r
7557df4d 1802 Allocate and initialize a net buffer queue.\r
97b38d4e 1803\r
64a80549 1804 @return The pointer to the allocated net buffer queue, or NULL if the\r
7557df4d 1805 allocation failed due to resource limit.\r
97b38d4e 1806\r
1807**/\r
1808NET_BUF_QUEUE *\r
1809EFIAPI\r
1810NetbufQueAlloc (\r
1811 VOID\r
1812 );\r
1813\r
1814/**\r
1204fe83 1815 Free a net buffer queue.\r
1816\r
7557df4d 1817 Decrease the reference count of the net buffer queue by one. The real resource\r
1204fe83 1818 free operation isn't performed until the reference count of the net buffer\r
7557df4d 1819 queue is decreased to 0.\r
97b38d4e 1820\r
64a80549 1821 @param[in] NbufQue The pointer to the net buffer queue to be freed.\r
97b38d4e 1822\r
1823**/\r
1824VOID\r
1825EFIAPI\r
1826NetbufQueFree (\r
1827 IN NET_BUF_QUEUE *NbufQue\r
1828 );\r
1829\r
1830/**\r
7557df4d 1831 Remove a net buffer from the head in the specific queue and return it.\r
97b38d4e 1832\r
64a80549 1833 @param[in, out] NbufQue The pointer to the net buffer queue.\r
97b38d4e 1834\r
64a80549 1835 @return The pointer to the net buffer removed from the specific queue,\r
7557df4d 1836 or NULL if there is no net buffer in the specific queue.\r
97b38d4e 1837\r
1838**/\r
1839NET_BUF *\r
1840EFIAPI\r
1841NetbufQueRemove (\r
ae213b7d 1842 IN OUT NET_BUF_QUEUE *NbufQue\r
97b38d4e 1843 );\r
1844\r
1845/**\r
7557df4d 1846 Append a net buffer to the net buffer queue.\r
97b38d4e 1847\r
64a80549 1848 @param[in, out] NbufQue The pointer to the net buffer queue.\r
1849 @param[in, out] Nbuf The pointer to the net buffer to be appended.\r
97b38d4e 1850\r
1851**/\r
1852VOID\r
1853EFIAPI\r
1854NetbufQueAppend (\r
ae213b7d 1855 IN OUT NET_BUF_QUEUE *NbufQue,\r
1856 IN OUT NET_BUF *Nbuf\r
97b38d4e 1857 );\r
1858\r
1859/**\r
7557df4d 1860 Copy Len bytes of data from the net buffer queue at the specific offset to the\r
1861 destination memory.\r
1204fe83 1862\r
64a80549 1863 The copying operation is the same as NetbufCopy, but applies to the net buffer\r
7557df4d 1864 queue instead of the net buffer.\r
1204fe83 1865\r
64a80549 1866 @param[in] NbufQue The pointer to the net buffer queue.\r
7557df4d 1867 @param[in] Offset The sequence number of the first byte to copy.\r
64a80549 1868 @param[in] Len The length of the data to copy.\r
7557df4d 1869 @param[out] Dest The destination of the data to copy to.\r
1870\r
1204fe83 1871 @return The length of the actual copied data, or 0 if the offset\r
7557df4d 1872 specified exceeds the total size of net buffer queue.\r
97b38d4e 1873\r
1874**/\r
1875UINT32\r
1876EFIAPI\r
1877NetbufQueCopy (\r
1878 IN NET_BUF_QUEUE *NbufQue,\r
1879 IN UINT32 Offset,\r
1880 IN UINT32 Len,\r
ae213b7d 1881 OUT UINT8 *Dest\r
97b38d4e 1882 );\r
1883\r
1884/**\r
3b1464d5 1885 Trim Len bytes of data from the buffer queue and free any net buffer\r
1886 that is completely trimmed.\r
1204fe83 1887\r
7557df4d 1888 The trimming operation is the same as NetbufTrim but applies to the net buffer\r
1889 queue instead of the net buffer.\r
97b38d4e 1890\r
64a80549 1891 @param[in, out] NbufQue The pointer to the net buffer queue.\r
1892 @param[in] Len The length of the data to trim.\r
97b38d4e 1893\r
7557df4d 1894 @return The actual length of the data trimmed.\r
97b38d4e 1895\r
1896**/\r
1897UINT32\r
1898EFIAPI\r
1899NetbufQueTrim (\r
ae213b7d 1900 IN OUT NET_BUF_QUEUE *NbufQue,\r
97b38d4e 1901 IN UINT32 Len\r
1902 );\r
1903\r
1904\r
1905/**\r
1906 Flush the net buffer queue.\r
1907\r
64a80549 1908 @param[in, out] NbufQue The pointer to the queue to be flushed.\r
97b38d4e 1909\r
1910**/\r
1911VOID\r
1912EFIAPI\r
1913NetbufQueFlush (\r
ae213b7d 1914 IN OUT NET_BUF_QUEUE *NbufQue\r
97b38d4e 1915 );\r
1916\r
1917/**\r
7557df4d 1918 Compute the checksum for a bulk of data.\r
97b38d4e 1919\r
64a80549 1920 @param[in] Bulk The pointer to the data.\r
1921 @param[in] Len The length of the data, in bytes.\r
97b38d4e 1922\r
ae213b7d 1923 @return The computed checksum.\r
97b38d4e 1924\r
1925**/\r
1926UINT16\r
1927EFIAPI\r
1928NetblockChecksum (\r
1929 IN UINT8 *Bulk,\r
1930 IN UINT32 Len\r
1931 );\r
1932\r
1933/**\r
1934 Add two checksums.\r
1935\r
ae213b7d 1936 @param[in] Checksum1 The first checksum to be added.\r
1937 @param[in] Checksum2 The second checksum to be added.\r
97b38d4e 1938\r
ae213b7d 1939 @return The new checksum.\r
97b38d4e 1940\r
1941**/\r
1942UINT16\r
1943EFIAPI\r
1944NetAddChecksum (\r
1945 IN UINT16 Checksum1,\r
1946 IN UINT16 Checksum2\r
1947 );\r
1948\r
1949/**\r
1950 Compute the checksum for a NET_BUF.\r
1951\r
64a80549 1952 @param[in] Nbuf The pointer to the net buffer.\r
97b38d4e 1953\r
ae213b7d 1954 @return The computed checksum.\r
97b38d4e 1955\r
1956**/\r
1957UINT16\r
1958EFIAPI\r
1959NetbufChecksum (\r
1960 IN NET_BUF *Nbuf\r
1961 );\r
1962\r
1963/**\r
1204fe83 1964 Compute the checksum for TCP/UDP pseudo header.\r
1965\r
7557df4d 1966 Src and Dst are in network byte order, and Len is in host byte order.\r
97b38d4e 1967\r
ae213b7d 1968 @param[in] Src The source address of the packet.\r
1969 @param[in] Dst The destination address of the packet.\r
1970 @param[in] Proto The protocol type of the packet.\r
1971 @param[in] Len The length of the packet.\r
97b38d4e 1972\r
ae213b7d 1973 @return The computed checksum.\r
97b38d4e 1974\r
1975**/\r
1976UINT16\r
1977EFIAPI\r
1978NetPseudoHeadChecksum (\r
1979 IN IP4_ADDR Src,\r
1980 IN IP4_ADDR Dst,\r
1981 IN UINT8 Proto,\r
1982 IN UINT16 Len\r
1983 );\r
1984\r
f6b7393c 1985/**\r
64a80549 1986 Compute the checksum for the TCP6/UDP6 pseudo header.\r
1204fe83 1987\r
f6b7393c 1988 Src and Dst are in network byte order, and Len is in host byte order.\r
1989\r
1990 @param[in] Src The source address of the packet.\r
1991 @param[in] Dst The destination address of the packet.\r
1992 @param[in] NextHeader The protocol type of the packet.\r
1993 @param[in] Len The length of the packet.\r
1994\r
1995 @return The computed checksum.\r
1996\r
1997**/\r
1998UINT16\r
e798cd87 1999EFIAPI\r
f6b7393c 2000NetIp6PseudoHeadChecksum (\r
2001 IN EFI_IPv6_ADDRESS *Src,\r
2002 IN EFI_IPv6_ADDRESS *Dst,\r
2003 IN UINT8 NextHeader,\r
2004 IN UINT32 Len\r
2005 );\r
97b38d4e 2006#endif\r