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