]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Include/Library/NetLib.h
add back CAPSULE_HOB_INFO definition for back-compatible.
[mirror_edk2.git] / MdeModulePkg / Include / Library / NetLib.h
CommitLineData
97b38d4e 1/** @file\r
e4b99ad9 2 Ihis 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
3a15fd52 5Copyright (c) 2005 - 2009, Intel Corporation\r
97b38d4e 6All rights reserved. This program and the accompanying materials\r
7are licensed and made available under the terms and conditions of the BSD License\r
8which accompanies this distribution. The full text of the license may be found at\r
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
97b38d4e 21typedef UINT32 IP4_ADDR;\r
22typedef UINT32 TCP_SEQNO;\r
23typedef UINT16 TCP_PORTNO;\r
24\r
25typedef enum {\r
26 NET_ETHER_ADDR_LEN = 6,\r
27 NET_IFTYPE_ETHERNET = 0x01,\r
28\r
29 EFI_IP_PROTO_UDP = 0x11,\r
30 EFI_IP_PROTO_TCP = 0x06,\r
31 EFI_IP_PROTO_ICMP = 0x01,\r
32\r
33 //\r
d7db0902 34 // The address classification\r
97b38d4e 35 //\r
36 IP4_ADDR_CLASSA = 1,\r
37 IP4_ADDR_CLASSB,\r
38 IP4_ADDR_CLASSC,\r
39 IP4_ADDR_CLASSD,\r
40 IP4_ADDR_CLASSE,\r
41\r
42 IP4_MASK_NUM = 33\r
43} IP4_CLASS_TYPE;\r
44\r
fb115c61 45typedef enum {\r
46 IP6_HOP_BY_HOP = 0,\r
47 IP6_DESTINATION = 60,\r
48 IP6_FRAGMENT = 44,\r
49 IP6_AH = 51,\r
50 IP6_ESP = 50,\r
51 IP6_NO_NEXT_HEADER = 59\r
52} IP6_EXTENSION_HEADER_TYPE;\r
53\r
97b38d4e 54#pragma pack(1)\r
55\r
56//\r
57// Ethernet head definition\r
58//\r
59typedef struct {\r
60 UINT8 DstMac [NET_ETHER_ADDR_LEN];\r
61 UINT8 SrcMac [NET_ETHER_ADDR_LEN];\r
62 UINT16 EtherType;\r
63} ETHER_HEAD;\r
64\r
65\r
66//\r
67// The EFI_IP4_HEADER is hard to use because the source and\r
68// destination address are defined as EFI_IPv4_ADDRESS, which\r
69// is a structure. Two structures can't be compared or masked\r
70// directly. This is why there is an internal representation.\r
71//\r
72typedef struct {\r
73 UINT8 HeadLen : 4;\r
74 UINT8 Ver : 4;\r
75 UINT8 Tos;\r
76 UINT16 TotalLen;\r
77 UINT16 Id;\r
78 UINT16 Fragment;\r
79 UINT8 Ttl;\r
80 UINT8 Protocol;\r
81 UINT16 Checksum;\r
82 IP4_ADDR Src;\r
83 IP4_ADDR Dst;\r
84} IP4_HEAD;\r
85\r
86\r
87//\r
e9b67286 88// ICMP head definition. Each ICMP message is categorized as either an error\r
97b38d4e 89// message or query message. Two message types have their own head format.\r
90//\r
91typedef struct {\r
92 UINT8 Type;\r
93 UINT8 Code;\r
94 UINT16 Checksum;\r
95} IP4_ICMP_HEAD;\r
96\r
97typedef struct {\r
98 IP4_ICMP_HEAD Head;\r
99 UINT32 Fourth; // 4th filed of the head, it depends on Type.\r
100 IP4_HEAD IpHead;\r
101} IP4_ICMP_ERROR_HEAD;\r
102\r
103typedef struct {\r
104 IP4_ICMP_HEAD Head;\r
105 UINT16 Id;\r
106 UINT16 Seq;\r
107} IP4_ICMP_QUERY_HEAD;\r
108\r
fb115c61 109typedef struct {\r
110 UINT8 Type;\r
111 UINT8 Code;\r
112 UINT16 Checksum;\r
113} IP6_ICMP_HEAD;\r
114\r
115typedef struct {\r
116 IP6_ICMP_HEAD Head;\r
117 UINT32 Fourth;\r
118 EFI_IP6_HEADER IpHead;\r
119} IP6_ICMP_ERROR_HEAD;\r
120\r
121typedef struct {\r
122 IP6_ICMP_HEAD Head;\r
123 UINT32 Fourth;\r
124} IP6_ICMP_INFORMATION_HEAD;\r
97b38d4e 125\r
126//\r
127// UDP header definition\r
128//\r
129typedef struct {\r
130 UINT16 SrcPort;\r
131 UINT16 DstPort;\r
132 UINT16 Length;\r
133 UINT16 Checksum;\r
fb115c61 134} EFI_UDP_HEADER;\r
97b38d4e 135\r
136//\r
137// TCP header definition\r
138//\r
139typedef struct {\r
140 TCP_PORTNO SrcPort;\r
141 TCP_PORTNO DstPort;\r
142 TCP_SEQNO Seq;\r
143 TCP_SEQNO Ack;\r
144 UINT8 Res : 4;\r
145 UINT8 HeadLen : 4;\r
146 UINT8 Flag;\r
147 UINT16 Wnd;\r
148 UINT16 Checksum;\r
149 UINT16 Urg;\r
150} TCP_HEAD;\r
151\r
152#pragma pack()\r
153\r
154#define NET_MAC_EQUAL(pMac1, pMac2, Len) \\r
155 (CompareMem ((pMac1), (pMac2), Len) == 0)\r
156\r
157#define NET_MAC_IS_MULTICAST(Mac, BMac, Len) \\r
158 (((*((UINT8 *) Mac) & 0x01) == 0x01) && (!NET_MAC_EQUAL (Mac, BMac, Len)))\r
159\r
160#define NTOHL(x) (UINT32)((((UINT32) (x) & 0xff) << 24) | \\r
161 (((UINT32) (x) & 0xff00) << 8) | \\r
162 (((UINT32) (x) & 0xff0000) >> 8) | \\r
163 (((UINT32) (x) & 0xff000000) >> 24))\r
164\r
165#define HTONL(x) NTOHL(x)\r
166\r
167#define NTOHS(x) (UINT16)((((UINT16) (x) & 0xff) << 8) | \\r
168 (((UINT16) (x) & 0xff00) >> 8))\r
169\r
170#define HTONS(x) NTOHS(x)\r
171\r
172//\r
173// Test the IP's attribute, All the IPs are in host byte order.\r
174//\r
175#define IP4_IS_MULTICAST(Ip) (((Ip) & 0xF0000000) == 0xE0000000)\r
176#define IP4_IS_LOCAL_BROADCAST(Ip) ((Ip) == 0xFFFFFFFF)\r
177#define IP4_NET_EQUAL(Ip1, Ip2, NetMask) (((Ip1) & (NetMask)) == ((Ip2) & (NetMask)))\r
178#define IP4_IS_VALID_NETMASK(Ip) (NetGetMaskLength (Ip) != IP4_MASK_NUM)\r
179\r
3a15fd52 180#define IP6_IS_MULTICAST(Ip6) (((Ip6)->Addr[0]) == 0xFF)\r
181\r
97b38d4e 182//\r
183// Convert the EFI_IP4_ADDRESS to plain UINT32 IP4 address.\r
184//\r
185#define EFI_IP4(EfiIpAddr) (*(IP4_ADDR *) ((EfiIpAddr).Addr))\r
186#define EFI_NTOHL(EfiIp) (NTOHL (EFI_IP4 ((EfiIp))))\r
187#define EFI_IP4_EQUAL(Ip1, Ip2) (CompareMem ((Ip1), (Ip2), sizeof (EFI_IPv4_ADDRESS)) == 0)\r
188\r
fb115c61 189#define EFI_IP6_EQUAL(Ip1, Ip2) (CompareMem ((Ip1), (Ip2), sizeof (EFI_IPv6_ADDRESS)) == 0)\r
190\r
191\r
97b38d4e 192/**\r
3a1ab4bc 193 Return the length of the mask. \r
194 \r
e9b67286 195 Return the length of the mask. Valid values are 0 to 32.\r
3a1ab4bc 196 If the mask is invalid, return the invalid length 33, which is IP4_MASK_NUM.\r
97b38d4e 197 NetMask is in the host byte order.\r
198\r
ae213b7d 199 @param[in] NetMask The netmask to get the length from.\r
97b38d4e 200\r
e9b67286 201 @return The length of the netmask, or IP4_MASK_NUM (33) if the mask is invalid.\r
ae213b7d 202 \r
97b38d4e 203**/\r
204INTN\r
205EFIAPI\r
206NetGetMaskLength (\r
ae213b7d 207 IN IP4_ADDR NetMask\r
97b38d4e 208 );\r
209\r
210/**\r
3a1ab4bc 211 Return the class of the IP address, such as class A, B, C.\r
97b38d4e 212 Addr is in host byte order.\r
3a1ab4bc 213 \r
214 The address of class A starts with 0.\r
215 If the address belong to class A, return IP4_ADDR_CLASSA.\r
216 The address of class B starts with 10. \r
217 If the address belong to class B, return IP4_ADDR_CLASSB.\r
218 The address of class C starts with 110. \r
219 If the address belong to class C, return IP4_ADDR_CLASSC.\r
220 The address of class D starts with 1110. \r
221 If the address belong to class D, return IP4_ADDR_CLASSD.\r
222 The address of class E starts with 1111.\r
223 If the address belong to class E, return IP4_ADDR_CLASSE.\r
97b38d4e 224\r
3a1ab4bc 225 \r
ae213b7d 226 @param[in] Addr The address to get the class from.\r
97b38d4e 227\r
ae213b7d 228 @return IP address class, such as IP4_ADDR_CLASSA.\r
97b38d4e 229\r
230**/\r
231INTN\r
232EFIAPI\r
233NetGetIpClass (\r
234 IN IP4_ADDR Addr\r
235 );\r
236\r
237/**\r
238 Check whether the IP is a valid unicast address according to\r
3a1ab4bc 239 the netmask. If NetMask is zero, use the IP address's class to get the default mask.\r
240 \r
241 If Ip is 0, IP is not a valid unicast address.\r
242 Class D address is used for multicasting and class E address is reserved for future. If Ip\r
e9b67286 243 belongs to class D or class E, Ip is not a valid unicast address. \r
244 If all bits of the host address of Ip are 0 or 1, Ip is not a valid unicast address.\r
97b38d4e 245\r
ae213b7d 246 @param[in] Ip The IP to check against.\r
247 @param[in] NetMask The mask of the IP.\r
97b38d4e 248\r
e9b67286 249 @return TRUE if Ip is a valid unicast address on the network, otherwise FALSE.\r
97b38d4e 250\r
251**/\r
252BOOLEAN\r
ae213b7d 253EFIAPI\r
97b38d4e 254Ip4IsUnicast (\r
255 IN IP4_ADDR Ip,\r
256 IN IP4_ADDR NetMask\r
257 );\r
258\r
fb115c61 259/**\r
260 Check whether the incoming IPv6 address is a valid unicast address.\r
261\r
262 If the address is a multicast address has binary 0xFF at the start, it is not\r
263 a valid unicast address. If the address is unspecified ::, it is not a valid\r
264 unicast address to be assigned to any node. If the address is loopback address\r
265 ::1, it is also not a valid unicast address to be assigned to any physical\r
266 interface. \r
267\r
268 @param[in] Ip6 The IPv6 address to check against.\r
269\r
270 @return TRUE if Ip6 is a valid unicast address on the network, otherwise FALSE.\r
271\r
272**/ \r
273BOOLEAN\r
274Ip6IsValidUnicast (\r
275 IN EFI_IPv6_ADDRESS *Ip6\r
276 );\r
277\r
8d7e5af1 278extern IP4_ADDR gIp4AllMasks[IP4_MASK_NUM];\r
97b38d4e 279\r
280\r
281extern EFI_IPv4_ADDRESS mZeroIp4Addr;\r
282\r
283#define NET_IS_DIGIT(Ch) (('0' <= (Ch)) && ((Ch) <= '9'))\r
284#define NET_ROUNDUP(size, unit) (((size) + (unit) - 1) & (~((unit) - 1)))\r
285#define NET_IS_LOWER_CASE_CHAR(Ch) (('a' <= (Ch)) && ((Ch) <= 'z'))\r
286#define NET_IS_UPPER_CASE_CHAR(Ch) (('A' <= (Ch)) && ((Ch) <= 'Z'))\r
287\r
288#define TICKS_PER_MS 10000U\r
289#define TICKS_PER_SECOND 10000000U\r
290\r
291#define NET_RANDOM(Seed) ((UINT32) ((UINT32) (Seed) * 1103515245UL + 12345) % 4294967295UL)\r
292\r
293/**\r
3a1ab4bc 294 Extract a UINT32 from a byte stream.\r
295 \r
e9b67286 296 This function copies a UINT32 from a byte stream, and then converts it from Network \r
3a1ab4bc 297 byte order to host byte order. Use this function to avoid alignment error.\r
97b38d4e 298\r
ae213b7d 299 @param[in] Buf The buffer to extract the UINT32.\r
97b38d4e 300\r
301 @return The UINT32 extracted.\r
302\r
303**/\r
304UINT32\r
305EFIAPI\r
306NetGetUint32 (\r
307 IN UINT8 *Buf\r
308 );\r
309\r
310/**\r
e9b67286 311 Puts a UINT32 into the byte stream in network byte order. \r
3a1ab4bc 312 \r
e9b67286 313 Converts a UINT32 from host byte order to network byte order, and then copies it to the \r
3a1ab4bc 314 byte stream.\r
97b38d4e 315\r
ae213b7d 316 @param[in, out] Buf The buffer to put the UINT32.\r
317 @param[in] Data The data to put.\r
318 \r
97b38d4e 319**/\r
320VOID\r
321EFIAPI\r
322NetPutUint32 (\r
ae213b7d 323 IN OUT UINT8 *Buf,\r
324 IN UINT32 Data\r
97b38d4e 325 );\r
326\r
327/**\r
328 Initialize a random seed using current time.\r
3a1ab4bc 329 \r
330 Get current time first. Then initialize a random seed based on some basic \r
e9b67286 331 mathematical operations on the hour, day, minute, second, nanosecond and year \r
3a1ab4bc 332 of the current time.\r
333 \r
e9b67286 334 @return The random seed, initialized with current time.\r
97b38d4e 335\r
336**/\r
337UINT32\r
338EFIAPI\r
339NetRandomInitSeed (\r
340 VOID\r
341 );\r
342\r
343\r
344#define NET_LIST_USER_STRUCT(Entry, Type, Field) \\r
50d7ebad 345 BASE_CR(Entry, Type, Field)\r
97b38d4e 346\r
347#define NET_LIST_USER_STRUCT_S(Entry, Type, Field, Sig) \\r
348 CR(Entry, Type, Field, Sig)\r
349\r
350//\r
e9b67286 351// Iterate through the double linked list. It is NOT delete safe\r
97b38d4e 352//\r
353#define NET_LIST_FOR_EACH(Entry, ListHead) \\r
354 for(Entry = (ListHead)->ForwardLink; Entry != (ListHead); Entry = Entry->ForwardLink)\r
355\r
356//\r
e9b67286 357// Iterate through the double linked list. This is delete-safe.\r
97b38d4e 358// Don't touch NextEntry. Also, don't use this macro if list\r
359// entries other than the Entry may be deleted when processing\r
360// the current Entry.\r
361//\r
362#define NET_LIST_FOR_EACH_SAFE(Entry, NextEntry, ListHead) \\r
363 for(Entry = (ListHead)->ForwardLink, NextEntry = Entry->ForwardLink; \\r
364 Entry != (ListHead); \\r
365 Entry = NextEntry, NextEntry = Entry->ForwardLink \\r
366 )\r
367\r
368//\r
e9b67286 369// Make sure the list isn't empty before getting the first/last record.\r
97b38d4e 370//\r
371#define NET_LIST_HEAD(ListHead, Type, Field) \\r
372 NET_LIST_USER_STRUCT((ListHead)->ForwardLink, Type, Field)\r
373\r
374#define NET_LIST_TAIL(ListHead, Type, Field) \\r
375 NET_LIST_USER_STRUCT((ListHead)->BackLink, Type, Field)\r
376\r
377\r
378/**\r
3a1ab4bc 379 Remove the first node entry on the list, and return the removed node entry.\r
380 \r
e9b67286 381 Removes the first node entry from a doubly linked list. It is up to the caller of\r
382 this function to release the memory used by the first node, if that is required. On\r
3a1ab4bc 383 exit, the removed node is returned. \r
384\r
385 If Head is NULL, then ASSERT().\r
386 If Head was not initialized, then ASSERT().\r
387 If PcdMaximumLinkedListLength is not zero, and the number of nodes in the\r
388 linked list including the head node is greater than or equal to PcdMaximumLinkedListLength,\r
389 then ASSERT(). \r
97b38d4e 390\r
ae213b7d 391 @param[in, out] Head The list header.\r
97b38d4e 392\r
3a1ab4bc 393 @return The first node entry that is removed from the list, NULL if the list is empty.\r
97b38d4e 394\r
395**/\r
396LIST_ENTRY *\r
397EFIAPI\r
398NetListRemoveHead (\r
ae213b7d 399 IN OUT LIST_ENTRY *Head\r
97b38d4e 400 );\r
401\r
402/**\r
e9b67286 403 Remove the last node entry on the list and return the removed node entry.\r
3a1ab4bc 404\r
405 Removes the last node entry from a doubly linked list. It is up to the caller of\r
e9b67286 406 this function to release the memory used by the first node, if that is required. On\r
3a1ab4bc 407 exit, the removed node is returned. \r
97b38d4e 408\r
3a1ab4bc 409 If Head is NULL, then ASSERT().\r
410 If Head was not initialized, then ASSERT().\r
411 If PcdMaximumLinkedListLength is not zero, and the number of nodes in the\r
412 linked list including the head node is greater than or equal to PcdMaximumLinkedListLength,\r
413 then ASSERT(). \r
414 \r
ae213b7d 415 @param[in, out] Head The list head.\r
97b38d4e 416\r
3a1ab4bc 417 @return The last node entry that is removed from the list, NULL if the list is empty.\r
97b38d4e 418\r
419**/\r
420LIST_ENTRY *\r
421EFIAPI\r
422NetListRemoveTail (\r
ae213b7d 423 IN OUT LIST_ENTRY *Head\r
97b38d4e 424 );\r
425\r
426/**\r
3a1ab4bc 427 Insert a new node entry after a designated node entry of a doubly linked list.\r
428 \r
e9b67286 429 Inserts a new node entry designated by NewEntry after the node entry designated by PrevEntry\r
3a1ab4bc 430 of the doubly linked list.\r
431 \r
e9b67286 432 @param[in, out] PrevEntry The entry after which to insert. \r
ae213b7d 433 @param[in, out] NewEntry The new entry to insert.\r
97b38d4e 434\r
435**/\r
436VOID\r
437EFIAPI\r
438NetListInsertAfter (\r
ae213b7d 439 IN OUT LIST_ENTRY *PrevEntry,\r
440 IN OUT LIST_ENTRY *NewEntry\r
97b38d4e 441 );\r
442\r
443/**\r
3a1ab4bc 444 Insert a new node entry before a designated node entry of a doubly linked list.\r
445 \r
e9b67286 446 Inserts a new node entry designated by NewEntry before the node entry designated by PostEntry\r
3a1ab4bc 447 of the doubly linked list.\r
448 \r
ae213b7d 449 @param[in, out] PostEntry The entry to insert before.\r
450 @param[in, out] NewEntry The new entry to insert.\r
97b38d4e 451\r
452**/\r
453VOID\r
454EFIAPI\r
455NetListInsertBefore (\r
ae213b7d 456 IN OUT LIST_ENTRY *PostEntry,\r
457 IN OUT LIST_ENTRY *NewEntry\r
97b38d4e 458 );\r
459\r
460\r
461//\r
462// Object container: EFI network stack spec defines various kinds of\r
463// tokens. The drivers can share code to manage those objects.\r
464//\r
465typedef struct {\r
466 LIST_ENTRY Link;\r
467 VOID *Key;\r
468 VOID *Value;\r
469} NET_MAP_ITEM;\r
470\r
471typedef struct {\r
472 LIST_ENTRY Used;\r
473 LIST_ENTRY Recycled;\r
474 UINTN Count;\r
475} NET_MAP;\r
476\r
477#define NET_MAP_INCREAMENT 64\r
478\r
479/**\r
480 Initialize the netmap. Netmap is a reposity to keep the <Key, Value> pairs.\r
3a1ab4bc 481 \r
482 Initialize the forward and backward links of two head nodes donated by Map->Used \r
483 and Map->Recycled of two doubly linked lists.\r
484 Initializes the count of the <Key, Value> pairs in the netmap to zero.\r
485 \r
486 If Map is NULL, then ASSERT().\r
487 If the address of Map->Used is NULL, then ASSERT().\r
488 If the address of Map->Recycled is NULl, then ASSERT().\r
489 \r
ae213b7d 490 @param[in, out] Map The netmap to initialize.\r
97b38d4e 491\r
492**/\r
493VOID\r
494EFIAPI\r
495NetMapInit (\r
ae213b7d 496 IN OUT NET_MAP *Map\r
97b38d4e 497 );\r
498\r
499/**\r
500 To clean up the netmap, that is, release allocated memories.\r
3a1ab4bc 501 \r
e9b67286 502 Removes all nodes of the Used doubly linked list and frees memory of all related netmap items.\r
3a1ab4bc 503 Removes all nodes of the Recycled doubly linked list and free memory of all related netmap items.\r
e9b67286 504 The number of the <Key, Value> pairs in the netmap is set to zero.\r
3a1ab4bc 505 \r
506 If Map is NULL, then ASSERT().\r
507 \r
ae213b7d 508 @param[in, out] Map The netmap to clean up.\r
97b38d4e 509\r
510**/\r
511VOID\r
512EFIAPI\r
513NetMapClean (\r
ae213b7d 514 IN OUT NET_MAP *Map\r
97b38d4e 515 );\r
516\r
517/**\r
3a1ab4bc 518 Test whether the netmap is empty and return true if it is.\r
519 \r
520 If the number of the <Key, Value> pairs in the netmap is zero, return TRUE.\r
521 \r
522 If Map is NULL, then ASSERT().\r
523 \r
524 \r
ae213b7d 525 @param[in] Map The net map to test.\r
97b38d4e 526\r
527 @return TRUE if the netmap is empty, otherwise FALSE.\r
528\r
529**/\r
530BOOLEAN\r
531EFIAPI\r
532NetMapIsEmpty (\r
533 IN NET_MAP *Map\r
534 );\r
535\r
536/**\r
537 Return the number of the <Key, Value> pairs in the netmap.\r
538\r
ae213b7d 539 @param[in] Map The netmap to get the entry number.\r
97b38d4e 540\r
541 @return The entry number in the netmap.\r
542\r
543**/\r
544UINTN\r
545EFIAPI\r
546NetMapGetCount (\r
547 IN NET_MAP *Map\r
548 );\r
549\r
550/**\r
551 Allocate an item to save the <Key, Value> pair to the head of the netmap.\r
3a1ab4bc 552 \r
553 Allocate an item to save the <Key, Value> pair and add corresponding node entry\r
554 to the beginning of the Used doubly linked list. The number of the <Key, Value> \r
555 pairs in the netmap increase by 1.\r
97b38d4e 556\r
3a1ab4bc 557 If Map is NULL, then ASSERT().\r
558 \r
ae213b7d 559 @param[in, out] Map The netmap to insert into.\r
560 @param[in] Key The user's key.\r
561 @param[in] Value The user's value for the key.\r
97b38d4e 562\r
ae213b7d 563 @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the item.\r
564 @retval EFI_SUCCESS The item is inserted to the head.\r
97b38d4e 565\r
566**/\r
567EFI_STATUS\r
568EFIAPI\r
569NetMapInsertHead (\r
ae213b7d 570 IN OUT NET_MAP *Map,\r
97b38d4e 571 IN VOID *Key,\r
572 IN VOID *Value OPTIONAL\r
573 );\r
574\r
575/**\r
576 Allocate an item to save the <Key, Value> pair to the tail of the netmap.\r
577\r
3a1ab4bc 578 Allocate an item to save the <Key, Value> pair and add corresponding node entry\r
579 to the tail of the Used doubly linked list. The number of the <Key, Value> \r
580 pairs in the netmap increase by 1.\r
581\r
582 If Map is NULL, then ASSERT().\r
583 \r
ae213b7d 584 @param[in, out] Map The netmap to insert into.\r
585 @param[in] Key The user's key.\r
586 @param[in] Value The user's value for the key.\r
97b38d4e 587\r
ae213b7d 588 @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the item.\r
589 @retval EFI_SUCCESS The item is inserted to the tail.\r
97b38d4e 590\r
591**/\r
592EFI_STATUS\r
593EFIAPI\r
594NetMapInsertTail (\r
ae213b7d 595 IN OUT NET_MAP *Map,\r
97b38d4e 596 IN VOID *Key,\r
597 IN VOID *Value OPTIONAL\r
598 );\r
599\r
600/**\r
e9b67286 601 Finds the key in the netmap and returns the point to the item containing the Key.\r
3a1ab4bc 602 \r
603 Iterate the Used doubly linked list of the netmap to get every item. Compare the key of every \r
604 item with the key to search. It returns the point to the item contains the Key if found.\r
97b38d4e 605\r
3a1ab4bc 606 If Map is NULL, then ASSERT().\r
607 \r
ae213b7d 608 @param[in] Map The netmap to search within.\r
609 @param[in] Key The key to search.\r
97b38d4e 610\r
611 @return The point to the item contains the Key, or NULL if Key isn't in the map.\r
612\r
613**/\r
ae213b7d 614NET_MAP_ITEM *\r
97b38d4e 615EFIAPI\r
616NetMapFindKey (\r
617 IN NET_MAP *Map,\r
618 IN VOID *Key\r
619 );\r
620\r
621/**\r
3a1ab4bc 622 Remove the node entry of the item from the netmap and return the key of the removed item.\r
623 \r
624 Remove the node entry of the item from the Used doubly linked list of the netmap. \r
625 The number of the <Key, Value> pairs in the netmap decrease by 1. Then add the node \r
626 entry of the item to the Recycled doubly linked list of the netmap. If Value is not NULL,\r
627 Value will point to the value of the item. It returns the key of the removed item.\r
628 \r
629 If Map is NULL, then ASSERT().\r
630 If Item is NULL, then ASSERT().\r
631 if item in not in the netmap, then ASSERT().\r
632 \r
ae213b7d 633 @param[in, out] Map The netmap to remove the item from.\r
634 @param[in, out] Item The item to remove.\r
635 @param[out] Value The variable to receive the value if not NULL.\r
97b38d4e 636\r
ae213b7d 637 @return The key of the removed item.\r
97b38d4e 638\r
639**/\r
640VOID *\r
641EFIAPI\r
642NetMapRemoveItem (\r
ae213b7d 643 IN OUT NET_MAP *Map,\r
644 IN OUT NET_MAP_ITEM *Item,\r
645 OUT VOID **Value OPTIONAL\r
97b38d4e 646 );\r
647\r
648/**\r
3a1ab4bc 649 Remove the first node entry on the netmap and return the key of the removed item.\r
97b38d4e 650\r
3a1ab4bc 651 Remove the first node entry from the Used doubly linked list of the netmap. \r
652 The number of the <Key, Value> pairs in the netmap decrease by 1. Then add the node \r
653 entry to the Recycled doubly linked list of the netmap. If parameter Value is not NULL,\r
654 parameter Value will point to the value of the item. It returns the key of the removed item.\r
655 \r
656 If Map is NULL, then ASSERT().\r
657 If the Used doubly linked list is empty, then ASSERT().\r
658 \r
ae213b7d 659 @param[in, out] Map The netmap to remove the head from.\r
660 @param[out] Value The variable to receive the value if not NULL.\r
97b38d4e 661\r
ae213b7d 662 @return The key of the item removed.\r
97b38d4e 663\r
664**/\r
665VOID *\r
666EFIAPI\r
667NetMapRemoveHead (\r
ae213b7d 668 IN OUT NET_MAP *Map,\r
669 OUT VOID **Value OPTIONAL\r
97b38d4e 670 );\r
671\r
672/**\r
3a1ab4bc 673 Remove the last node entry on the netmap and return the key of the removed item.\r
97b38d4e 674\r
3a1ab4bc 675 Remove the last node entry from the Used doubly linked list of the netmap. \r
676 The number of the <Key, Value> pairs in the netmap decrease by 1. Then add the node \r
677 entry to the Recycled doubly linked list of the netmap. If parameter Value is not NULL,\r
678 parameter Value will point to the value of the item. It returns the key of the removed item.\r
679 \r
680 If Map is NULL, then ASSERT().\r
681 If the Used doubly linked list is empty, then ASSERT().\r
682 \r
ae213b7d 683 @param[in, out] Map The netmap to remove the tail from.\r
684 @param[out] Value The variable to receive the value if not NULL.\r
97b38d4e 685\r
ae213b7d 686 @return The key of the item removed.\r
97b38d4e 687\r
688**/\r
689VOID *\r
690EFIAPI\r
691NetMapRemoveTail (\r
ae213b7d 692 IN OUT NET_MAP *Map,\r
693 OUT VOID **Value OPTIONAL\r
97b38d4e 694 );\r
695\r
696typedef\r
697EFI_STATUS\r
698(*NET_MAP_CALLBACK) (\r
699 IN NET_MAP *Map,\r
700 IN NET_MAP_ITEM *Item,\r
701 IN VOID *Arg\r
702 );\r
703\r
704/**\r
3a1ab4bc 705 Iterate through the netmap and call CallBack for each item.\r
706 \r
707 It will contiue the traverse if CallBack returns EFI_SUCCESS, otherwise, break\r
708 from the loop. It returns the CallBack's last return value. This function is \r
709 delete safe for the current item.\r
97b38d4e 710\r
3a1ab4bc 711 If Map is NULL, then ASSERT().\r
712 If CallBack is NULL, then ASSERT().\r
713 \r
ae213b7d 714 @param[in] Map The Map to iterate through.\r
715 @param[in] CallBack The callback function to call for each item.\r
716 @param[in] Arg The opaque parameter to the callback.\r
97b38d4e 717\r
ae213b7d 718 @retval EFI_SUCCESS There is no item in the netmap or CallBack for each item\r
719 return EFI_SUCCESS.\r
720 @retval Others It returns the CallBack's last return value.\r
97b38d4e 721\r
722**/\r
723EFI_STATUS\r
724EFIAPI\r
725NetMapIterate (\r
726 IN NET_MAP *Map,\r
727 IN NET_MAP_CALLBACK CallBack,\r
ae213b7d 728 IN VOID *Arg\r
97b38d4e 729 );\r
730\r
731\r
732//\r
733// Helper functions to implement driver binding and service binding protocols.\r
734//\r
735/**\r
736 Create a child of the service that is identified by ServiceBindingGuid.\r
3a1ab4bc 737 \r
738 Get the ServiceBinding Protocol first, then use it to create a child.\r
97b38d4e 739\r
3a1ab4bc 740 If ServiceBindingGuid is NULL, then ASSERT().\r
741 If ChildHandle is NULL, then ASSERT().\r
742 \r
ae213b7d 743 @param[in] Controller The controller which has the service installed.\r
744 @param[in] Image The image handle used to open service.\r
745 @param[in] ServiceBindingGuid The service's Guid.\r
e9b67286 746 @param[in, out] ChildHandle The handle to receive the created child.\r
97b38d4e 747\r
e9b67286 748 @retval EFI_SUCCESS The child was successfully created.\r
97b38d4e 749 @retval Others Failed to create the child.\r
750\r
751**/\r
752EFI_STATUS\r
753EFIAPI\r
754NetLibCreateServiceChild (\r
ae213b7d 755 IN EFI_HANDLE Controller,\r
756 IN EFI_HANDLE Image,\r
97b38d4e 757 IN EFI_GUID *ServiceBindingGuid,\r
ae213b7d 758 IN OUT EFI_HANDLE *ChildHandle\r
97b38d4e 759 );\r
760\r
761/**\r
e9b67286 762 Destroy a child of the service that is identified by ServiceBindingGuid.\r
3a1ab4bc 763 \r
764 Get the ServiceBinding Protocol first, then use it to destroy a child.\r
765 \r
766 If ServiceBindingGuid is NULL, then ASSERT().\r
767 \r
ae213b7d 768 @param[in] Controller The controller which has the service installed.\r
769 @param[in] Image The image handle used to open service.\r
770 @param[in] ServiceBindingGuid The service's Guid.\r
e9b67286 771 @param[in] ChildHandle The child to destroy.\r
97b38d4e 772\r
e9b67286 773 @retval EFI_SUCCESS The child is successfully destroyed.\r
774 @retval Others Failed to destroy the child.\r
97b38d4e 775\r
776**/\r
777EFI_STATUS\r
778EFIAPI\r
779NetLibDestroyServiceChild (\r
ae213b7d 780 IN EFI_HANDLE Controller,\r
781 IN EFI_HANDLE Image,\r
97b38d4e 782 IN EFI_GUID *ServiceBindingGuid,\r
783 IN EFI_HANDLE ChildHandle\r
784 );\r
785\r
786/**\r
787 Convert the mac address of the simple network protocol installed on\r
788 SnpHandle to a unicode string. Callers are responsible for freeing the\r
789 string storage.\r
790\r
3a1ab4bc 791 Get the mac address of the Simple Network protocol from the SnpHandle. Then convert\r
792 the mac address into a unicode string. It takes 2 unicode characters to represent \r
e9b67286 793 a 1 byte binary buffer, plus one unicode character for the null terminator.\r
3a1ab4bc 794\r
795\r
e9b67286 796 @param[in] SnpHandle The handle on which the simple network protocol is\r
797 installed.\r
798 @param[in] ImageHandle The image handle to act as the agent handle to\r
ae213b7d 799 get the simple network protocol.\r
800 @param[out] MacString The pointer to store the address of the string\r
801 representation of the mac address.\r
802 \r
e9b67286 803 @retval EFI_SUCCESS Converted the mac address a unicode string successfully.\r
804 @retval EFI_OUT_OF_RESOURCES There are not enough memory resources.\r
ae213b7d 805 @retval Others Failed to open the simple network protocol.\r
97b38d4e 806\r
807**/\r
808EFI_STATUS\r
809EFIAPI\r
810NetLibGetMacString (\r
ae213b7d 811 IN EFI_HANDLE SnpHandle,\r
812 IN EFI_HANDLE ImageHandle,\r
813 OUT CHAR16 **MacString\r
97b38d4e 814 );\r
815\r
816/**\r
817 Create an IPv4 device path node.\r
3a1ab4bc 818 \r
819 The header type of IPv4 device path node is MESSAGING_DEVICE_PATH.\r
820 The header subtype of IPv4 device path node is MSG_IPv4_DP.\r
821 The length of the IPv4 device path node in bytes is 19.\r
822 Get other info from parameters to make up the whole IPv4 device path node.\r
97b38d4e 823\r
ae213b7d 824 @param[in, out] Node Pointer to the IPv4 device path node.\r
825 @param[in] Controller The handle where the NIC IP4 config protocol resides.\r
826 @param[in] LocalIp The local IPv4 address.\r
827 @param[in] LocalPort The local port.\r
828 @param[in] RemoteIp The remote IPv4 address.\r
829 @param[in] RemotePort The remote port.\r
830 @param[in] Protocol The protocol type in the IP header.\r
831 @param[in] UseDefaultAddress Whether this instance is using default address or not.\r
97b38d4e 832\r
97b38d4e 833**/\r
834VOID\r
835EFIAPI\r
836NetLibCreateIPv4DPathNode (\r
837 IN OUT IPv4_DEVICE_PATH *Node,\r
838 IN EFI_HANDLE Controller,\r
839 IN IP4_ADDR LocalIp,\r
840 IN UINT16 LocalPort,\r
841 IN IP4_ADDR RemoteIp,\r
842 IN UINT16 RemotePort,\r
843 IN UINT16 Protocol,\r
844 IN BOOLEAN UseDefaultAddress\r
845 );\r
846\r
847/**\r
848 Find the UNDI/SNP handle from controller and protocol GUID.\r
3a1ab4bc 849 \r
e9b67286 850 For example, IP will open an MNP child to transmit/receive\r
851 packets. When MNP is stopped, IP should also be stopped. IP\r
97b38d4e 852 needs to find its own private data which is related the IP's\r
853 service binding instance that is install on UNDI/SNP handle.\r
854 Now, the controller is either a MNP or ARP child handle. But\r
855 IP opens these handle BY_DRIVER, use that info, we can get the\r
856 UNDI/SNP handle.\r
857\r
ae213b7d 858 @param[in] Controller Then protocol handle to check.\r
859 @param[in] ProtocolGuid The protocol that is related with the handle.\r
97b38d4e 860\r
ae213b7d 861 @return The UNDI/SNP handle or NULL for errors.\r
97b38d4e 862\r
863**/\r
864EFI_HANDLE\r
865EFIAPI\r
866NetLibGetNicHandle (\r
867 IN EFI_HANDLE Controller,\r
868 IN EFI_GUID *ProtocolGuid\r
869 );\r
870\r
97b38d4e 871/**\r
872 This is the default unload handle for all the network drivers.\r
873\r
3a1ab4bc 874 Disconnect the driver specified by ImageHandle from all the devices in the handle database.\r
875 Uninstall all the protocols installed in the driver entry point.\r
876 \r
ae213b7d 877 @param[in] ImageHandle The drivers' driver image.\r
97b38d4e 878\r
879 @retval EFI_SUCCESS The image is unloaded.\r
880 @retval Others Failed to unload the image.\r
881\r
882**/\r
883EFI_STATUS\r
884EFIAPI\r
885NetLibDefaultUnload (\r
886 IN EFI_HANDLE ImageHandle\r
887 );\r
888\r
889typedef enum {\r
890 //\r
891 //Various signatures\r
892 //\r
f3f2e05d 893 NET_BUF_SIGNATURE = SIGNATURE_32 ('n', 'b', 'u', 'f'),\r
894 NET_VECTOR_SIGNATURE = SIGNATURE_32 ('n', 'v', 'e', 'c'),\r
895 NET_QUE_SIGNATURE = SIGNATURE_32 ('n', 'b', 'q', 'u'),\r
97b38d4e 896\r
897\r
898 NET_PROTO_DATA = 64, // Opaque buffer for protocols\r
899 NET_BUF_HEAD = 1, // Trim or allocate space from head\r
900 NET_BUF_TAIL = 0, // Trim or allocate space from tail\r
901 NET_VECTOR_OWN_FIRST = 0x01 // We allocated the 1st block in the vector\r
902} NET_SIGNATURE_TYPE;\r
903\r
904#define NET_CHECK_SIGNATURE(PData, SIGNATURE) \\r
905 ASSERT (((PData) != NULL) && ((PData)->Signature == (SIGNATURE)))\r
906\r
907#define NET_SWAP_SHORT(Value) \\r
908 ((((Value) & 0xff) << 8) | (((Value) >> 8) & 0xff))\r
909\r
910//\r
911// Single memory block in the vector.\r
912//\r
913typedef struct {\r
914 UINT32 Len; // The block's length\r
915 UINT8 *Bulk; // The block's Data\r
916} NET_BLOCK;\r
917\r
918typedef VOID (*NET_VECTOR_EXT_FREE) (VOID *Arg);\r
919\r
920//\r
921//NET_VECTOR contains several blocks to hold all packet's\r
922//fragments and other house-keeping stuff for sharing. It\r
923//doesn't specify the where actual packet fragment begins.\r
924//\r
925typedef struct {\r
926 UINT32 Signature;\r
927 INTN RefCnt; // Reference count to share NET_VECTOR.\r
928 NET_VECTOR_EXT_FREE Free; // external function to free NET_VECTOR\r
929 VOID *Arg; // opeque argument to Free\r
930 UINT32 Flag; // Flags, NET_VECTOR_OWN_FIRST\r
931 UINT32 Len; // Total length of the assocated BLOCKs\r
932\r
933 UINT32 BlockNum;\r
934 NET_BLOCK Block[1];\r
935} NET_VECTOR;\r
936\r
937//\r
e9b67286 938//NET_BLOCK_OP operates on the NET_BLOCK. It specifies\r
939//where the actual fragment begins and ends\r
97b38d4e 940//\r
941typedef struct {\r
942 UINT8 *BlockHead; // Block's head, or the smallest valid Head\r
943 UINT8 *BlockTail; // Block's tail. BlockTail-BlockHead=block length\r
944 UINT8 *Head; // 1st byte of the data in the block\r
945 UINT8 *Tail; // Tail of the data in the block, Tail-Head=Size\r
946 UINT32 Size; // The size of the data\r
947} NET_BLOCK_OP;\r
948\r
949\r
950//\r
951//NET_BUF is the buffer manage structure used by the\r
e9b67286 952//network stack. Every network packet may be fragmented. The Vector points to\r
953//memory blocks used by each fragment, and BlockOp\r
97b38d4e 954//specifies where each fragment begins and ends.\r
955//\r
e9b67286 956//It also contains an opaque area for the protocol to store\r
957//per-packet information. Protocol must be careful not\r
97b38d4e 958//to overwrite the members after that.\r
959//\r
960typedef struct {\r
961 UINT32 Signature;\r
962 INTN RefCnt;\r
963 LIST_ENTRY List; // The List this NET_BUF is on\r
964\r
965 IP4_HEAD *Ip; // Network layer header, for fast access\r
966 TCP_HEAD *Tcp; // Transport layer header, for fast access\r
967 UINT8 ProtoData [NET_PROTO_DATA]; //Protocol specific data\r
968\r
969 NET_VECTOR *Vector; // The vector containing the packet\r
970\r
971 UINT32 BlockOpNum; // Total number of BlockOp in the buffer\r
972 UINT32 TotalSize; // Total size of the actual packet\r
973 NET_BLOCK_OP BlockOp[1]; // Specify the position of actual packet\r
974} NET_BUF;\r
975\r
976\r
977//\r
e9b67286 978//A queue of NET_BUFs. It is a thin extension of\r
97b38d4e 979//NET_BUF functions.\r
980//\r
981typedef struct {\r
982 UINT32 Signature;\r
983 INTN RefCnt;\r
984 LIST_ENTRY List; // The List this buffer queue is on\r
985\r
986 LIST_ENTRY BufList; // list of queued buffers\r
987 UINT32 BufSize; // total length of DATA in the buffers\r
988 UINT32 BufNum; // total number of buffers on the chain\r
989} NET_BUF_QUEUE;\r
990\r
991//\r
992// Pseudo header for TCP and UDP checksum\r
993//\r
994#pragma pack(1)\r
995typedef struct {\r
996 IP4_ADDR SrcIp;\r
997 IP4_ADDR DstIp;\r
998 UINT8 Reserved;\r
999 UINT8 Protocol;\r
1000 UINT16 Len;\r
1001} NET_PSEUDO_HDR;\r
1002#pragma pack()\r
1003\r
1004//\r
1005// The fragment entry table used in network interfaces. This is\r
1006// the same as NET_BLOCK now. Use two different to distinguish\r
1007// the two in case that NET_BLOCK be enhanced later.\r
1008//\r
1009typedef struct {\r
1010 UINT32 Len;\r
1011 UINT8 *Bulk;\r
1012} NET_FRAGMENT;\r
1013\r
1014#define NET_GET_REF(PData) ((PData)->RefCnt++)\r
1015#define NET_PUT_REF(PData) ((PData)->RefCnt--)\r
50d7ebad 1016#define NETBUF_FROM_PROTODATA(Info) BASE_CR((Info), NET_BUF, ProtoData)\r
97b38d4e 1017\r
1018#define NET_BUF_SHARED(Buf) \\r
1019 (((Buf)->RefCnt > 1) || ((Buf)->Vector->RefCnt > 1))\r
1020\r
1021#define NET_VECTOR_SIZE(BlockNum) \\r
1022 (sizeof (NET_VECTOR) + ((BlockNum) - 1) * sizeof (NET_BLOCK))\r
1023\r
1024#define NET_BUF_SIZE(BlockOpNum) \\r
1025 (sizeof (NET_BUF) + ((BlockOpNum) - 1) * sizeof (NET_BLOCK_OP))\r
1026\r
1027#define NET_HEADSPACE(BlockOp) \\r
1028 (UINTN)((BlockOp)->Head - (BlockOp)->BlockHead)\r
1029\r
1030#define NET_TAILSPACE(BlockOp) \\r
1031 (UINTN)((BlockOp)->BlockTail - (BlockOp)->Tail)\r
1032\r
1033/**\r
1034 Allocate a single block NET_BUF. Upon allocation, all the\r
1035 free space is in the tail room.\r
1036\r
ae213b7d 1037 @param[in] Len The length of the block.\r
97b38d4e 1038\r
7557df4d 1039 @return Pointer to the allocated NET_BUF, or NULL if the \r
1040 allocation failed due to resource limit.\r
97b38d4e 1041\r
1042**/\r
1043NET_BUF *\r
1044EFIAPI\r
1045NetbufAlloc (\r
1046 IN UINT32 Len\r
1047 );\r
1048\r
1049/**\r
7557df4d 1050 Free the net buffer and its associated NET_VECTOR.\r
1051 \r
1052 Decrease the reference count of the net buffer by one. Free the associated net\r
1053 vector and itself if the reference count of the net buffer is decreased to 0. \r
e9b67286 1054 The net vector free operation decreases the reference count of the net \r
1055 vector by one, and performs the resource free operation when the reference count\r
7557df4d 1056 of the net vector is 0. \r
1057 \r
ae213b7d 1058 @param[in] Nbuf Pointer to the NET_BUF to be freed.\r
97b38d4e 1059\r
1060**/\r
1061VOID\r
1062EFIAPI\r
1063NetbufFree (\r
1064 IN NET_BUF *Nbuf\r
1065 );\r
1066\r
1067/**\r
7557df4d 1068 Get the index of NET_BLOCK_OP that contains the byte at Offset in the net \r
1069 buffer. \r
1070 \r
e9b67286 1071 For example, this function can be used to retrieve the IP header in the packet. It \r
1072 also can be used to get the fragment that contains the byte used \r
7557df4d 1073 mainly by the library implementation itself. \r
97b38d4e 1074\r
7557df4d 1075 @param[in] Nbuf Pointer to the net buffer.\r
1076 @param[in] Offset The offset of the byte.\r
1077 @param[out] Index Index of the NET_BLOCK_OP that contains the byte at \r
1078 Offset.\r
97b38d4e 1079\r
7557df4d 1080 @return Pointer to the Offset'th byte of data in the net buffer, or NULL\r
1081 if there is no such data in the net buffer.\r
97b38d4e 1082\r
1083**/\r
1084UINT8 *\r
1085EFIAPI\r
1086NetbufGetByte (\r
1087 IN NET_BUF *Nbuf,\r
1088 IN UINT32 Offset,\r
ae213b7d 1089 OUT UINT32 *Index OPTIONAL\r
97b38d4e 1090 );\r
1091\r
1092/**\r
7557df4d 1093 Create a copy of the net buffer that shares the associated net vector. \r
1094 \r
1095 The reference count of the newly created net buffer is set to 1. The reference \r
1096 count of the associated net vector is increased by one. \r
97b38d4e 1097\r
ae213b7d 1098 @param[in] Nbuf Pointer to the net buffer to be cloned.\r
97b38d4e 1099\r
7557df4d 1100 @return Pointer to the cloned net buffer, or NULL if the\r
ae213b7d 1101 allocation failed due to resource limit.\r
97b38d4e 1102\r
1103**/\r
7557df4d 1104NET_BUF *\r
97b38d4e 1105EFIAPI\r
1106NetbufClone (\r
1107 IN NET_BUF *Nbuf\r
1108 );\r
1109\r
1110/**\r
7557df4d 1111 Create a duplicated copy of the net buffer with data copied and HeadSpace\r
1112 bytes of head space reserved.\r
1113 \r
1114 The duplicated net buffer will allocate its own memory to hold the data of the\r
1115 source net buffer.\r
1116 \r
1117 @param[in] Nbuf Pointer to the net buffer to be duplicated from.\r
1118 @param[in, out] Duplicate Pointer to the net buffer to duplicate to, if \r
1119 NULL a new net buffer is allocated.\r
1120 @param[in] HeadSpace Length of the head space to reserve.\r
1121\r
1122 @return Pointer to the duplicated net buffer, or NULL if\r
1123 the allocation failed due to resource limit.\r
97b38d4e 1124\r
1125**/\r
1126NET_BUF *\r
1127EFIAPI\r
1128NetbufDuplicate (\r
1129 IN NET_BUF *Nbuf,\r
ae213b7d 1130 IN OUT NET_BUF *Duplicate OPTIONAL,\r
97b38d4e 1131 IN UINT32 HeadSpace\r
1132 );\r
1133\r
1134/**\r
7557df4d 1135 Create a NET_BUF structure which contains Len byte data of Nbuf starting from \r
1136 Offset. \r
1137 \r
1138 A new NET_BUF structure will be created but the associated data in NET_VECTOR \r
1139 is shared. This function exists to do IP packet fragmentation. \r
1140\r
1141 @param[in] Nbuf Pointer to the net buffer to be extracted.\r
1142 @param[in] Offset Starting point of the data to be included in the new \r
1143 net buffer.\r
1144 @param[in] Len Bytes of data to be included in the new net buffer. \r
1145 @param[in] HeadSpace Bytes of head space to reserve for protocol header. \r
1146\r
1147 @return Pointer to the cloned net buffer, or NULL if the \r
1148 allocation failed due to resource limit.\r
97b38d4e 1149\r
1150**/\r
1151NET_BUF *\r
1152EFIAPI\r
1153NetbufGetFragment (\r
1154 IN NET_BUF *Nbuf,\r
1155 IN UINT32 Offset,\r
1156 IN UINT32 Len,\r
1157 IN UINT32 HeadSpace\r
1158 );\r
1159\r
1160/**\r
7557df4d 1161 Reserve some space in the header room of the net buffer.\r
1162\r
e9b67286 1163 Upon allocation, all the space is in the tail room of the buffer. Call this \r
7557df4d 1164 function to move some space to the header room. This function is quite limited\r
1165 in that it can only reserve space from the first block of an empty NET_BUF not \r
1166 built from the external. But it should be enough for the network stack. \r
97b38d4e 1167\r
7557df4d 1168 @param[in, out] Nbuf Pointer to the net buffer.\r
1169 @param[in] Len The length of buffer to be reserved from the header.\r
97b38d4e 1170\r
1171**/\r
1172VOID\r
1173EFIAPI\r
1174NetbufReserve (\r
ae213b7d 1175 IN OUT NET_BUF *Nbuf,\r
97b38d4e 1176 IN UINT32 Len\r
1177 );\r
1178\r
1179/**\r
7557df4d 1180 Allocate Len bytes of space from the header or tail of the buffer. \r
97b38d4e 1181\r
7557df4d 1182 @param[in, out] Nbuf Pointer to the net buffer.\r
1183 @param[in] Len The length of the buffer to be allocated.\r
1184 @param[in] FromHead The flag to indicate whether reserve the data \r
1185 from head (TRUE) or tail (FALSE).\r
97b38d4e 1186\r
7557df4d 1187 @return Pointer to the first byte of the allocated buffer, \r
1188 or NULL if there is no sufficient space.\r
97b38d4e 1189\r
1190**/\r
7557df4d 1191UINT8*\r
97b38d4e 1192EFIAPI\r
1193NetbufAllocSpace (\r
ae213b7d 1194 IN OUT NET_BUF *Nbuf,\r
97b38d4e 1195 IN UINT32 Len,\r
1196 IN BOOLEAN FromHead\r
1197 );\r
1198\r
1199/**\r
7557df4d 1200 Trim Len bytes from the header or tail of the net buffer. \r
97b38d4e 1201\r
7557df4d 1202 @param[in, out] Nbuf Pointer to the net buffer.\r
1203 @param[in] Len The length of the data to be trimmed.\r
1204 @param[in] FromHead The flag to indicate whether trim data from head \r
1205 (TRUE) or tail (FALSE).\r
97b38d4e 1206\r
e9b67286 1207 @return Length of the actually trimmed data, which may be less \r
1208 than Len if the TotalSize of Nbuf is less than Len.\r
97b38d4e 1209\r
1210**/\r
1211UINT32\r
1212EFIAPI\r
1213NetbufTrim (\r
ae213b7d 1214 IN OUT NET_BUF *Nbuf,\r
97b38d4e 1215 IN UINT32 Len,\r
1216 IN BOOLEAN FromHead\r
1217 );\r
1218\r
1219/**\r
7557df4d 1220 Copy Len bytes of data from the specific offset of the net buffer to the \r
1221 destination memory.\r
1222 \r
e9b67286 1223 The Len bytes of data may cross several fragments of the net buffer.\r
7557df4d 1224 \r
1225 @param[in] Nbuf Pointer to the net buffer.\r
1226 @param[in] Offset The sequence number of the first byte to copy.\r
1227 @param[in] Len Length of the data to copy.\r
1228 @param[in] Dest The destination of the data to copy to.\r
1229\r
1230 @return The length of the actual copied data, or 0 if the offset\r
361468ed 1231 specified exceeds the total size of net buffer.\r
97b38d4e 1232\r
1233**/\r
1234UINT32\r
1235EFIAPI\r
1236NetbufCopy (\r
1237 IN NET_BUF *Nbuf,\r
1238 IN UINT32 Offset,\r
1239 IN UINT32 Len,\r
1240 IN UINT8 *Dest\r
1241 );\r
1242\r
1243/**\r
7557df4d 1244 Build a NET_BUF from external blocks. \r
1245 \r
e9b67286 1246 A new NET_BUF structure will be created from external blocks. An additional block\r
7557df4d 1247 of memory will be allocated to hold reserved HeadSpace bytes of header room\r
e9b67286 1248 and existing HeadLen bytes of header, but the external blocks are shared by the\r
7557df4d 1249 net buffer to avoid data copying.\r
97b38d4e 1250\r
ae213b7d 1251 @param[in] ExtFragment Pointer to the data block.\r
7557df4d 1252 @param[in] ExtNum The number of the data blocks.\r
ae213b7d 1253 @param[in] HeadSpace The head space to be reserved.\r
e9b67286 1254 @param[in] HeadLen The length of the protocol header. The function\r
1255 pulls this amount of data into a linear block.\r
1256 @param[in] ExtFree Pointer to the caller-provided free function.\r
ae213b7d 1257 @param[in] Arg The argument passed to ExtFree when ExtFree is\r
1258 called.\r
97b38d4e 1259\r
7557df4d 1260 @return Pointer to the net buffer built from the data blocks, \r
1261 or NULL if the allocation failed due to resource\r
1262 limit.\r
97b38d4e 1263\r
1264**/\r
1265NET_BUF *\r
1266EFIAPI\r
1267NetbufFromExt (\r
1268 IN NET_FRAGMENT *ExtFragment,\r
1269 IN UINT32 ExtNum,\r
1270 IN UINT32 HeadSpace,\r
1271 IN UINT32 HeadLen,\r
1272 IN NET_VECTOR_EXT_FREE ExtFree,\r
1273 IN VOID *Arg OPTIONAL\r
1274 );\r
1275\r
1276/**\r
7557df4d 1277 Build a fragment table to contain the fragments in the net buffer. This is the\r
1278 opposite operation of the NetbufFromExt. \r
1279 \r
ae213b7d 1280 @param[in] Nbuf Point to the net buffer.\r
1281 @param[in, out] ExtFragment Pointer to the data block.\r
7557df4d 1282 @param[in, out] ExtNum The number of the data blocks.\r
97b38d4e 1283\r
e9b67286 1284 @retval EFI_BUFFER_TOO_SMALL The number of non-empty blocks is bigger than \r
7557df4d 1285 ExtNum.\r
1286 @retval EFI_SUCCESS Fragment table is built successfully.\r
97b38d4e 1287\r
1288**/\r
1289EFI_STATUS\r
1290EFIAPI\r
1291NetbufBuildExt (\r
1292 IN NET_BUF *Nbuf,\r
ae213b7d 1293 IN OUT NET_FRAGMENT *ExtFragment,\r
1294 IN OUT UINT32 *ExtNum\r
97b38d4e 1295 );\r
1296\r
1297/**\r
7557df4d 1298 Build a net buffer from a list of net buffers.\r
1299 \r
1300 All the fragments will be collected from the list of NEW_BUF and then a new \r
1301 net buffer will be created through NetbufFromExt. \r
1302 \r
1303 @param[in] BufList A List of the net buffer.\r
1304 @param[in] HeadSpace The head space to be reserved.\r
e9b67286 1305 @param[in] HeaderLen The length of the protocol header. The function\r
1306 pulls this amount of data into a linear block.\r
7557df4d 1307 @param[in] ExtFree Pointer to the caller provided free function.\r
1308 @param[in] Arg The argument passed to ExtFree when ExtFree is called.\r
1309\r
1310 @return Pointer to the net buffer built from the list of net \r
1311 buffers.\r
97b38d4e 1312\r
1313**/\r
1314NET_BUF *\r
1315EFIAPI\r
1316NetbufFromBufList (\r
1317 IN LIST_ENTRY *BufList,\r
1318 IN UINT32 HeadSpace,\r
1319 IN UINT32 HeaderLen,\r
1320 IN NET_VECTOR_EXT_FREE ExtFree,\r
ae213b7d 1321 IN VOID *Arg OPTIONAL\r
97b38d4e 1322 );\r
1323\r
1324/**\r
1325 Free a list of net buffers.\r
1326\r
ae213b7d 1327 @param[in, out] Head Pointer to the head of linked net buffers.\r
97b38d4e 1328\r
1329**/\r
1330VOID\r
1331EFIAPI\r
1332NetbufFreeList (\r
ae213b7d 1333 IN OUT LIST_ENTRY *Head\r
97b38d4e 1334 );\r
1335\r
1336/**\r
1337 Initiate the net buffer queue.\r
1338\r
7557df4d 1339 @param[in, out] NbufQue Pointer to the net buffer queue to be initialized.\r
97b38d4e 1340\r
1341**/\r
1342VOID\r
1343EFIAPI\r
1344NetbufQueInit (\r
ae213b7d 1345 IN OUT NET_BUF_QUEUE *NbufQue\r
97b38d4e 1346 );\r
1347\r
1348/**\r
7557df4d 1349 Allocate and initialize a net buffer queue.\r
97b38d4e 1350\r
7557df4d 1351 @return Pointer to the allocated net buffer queue, or NULL if the\r
1352 allocation failed due to resource limit.\r
97b38d4e 1353\r
1354**/\r
1355NET_BUF_QUEUE *\r
1356EFIAPI\r
1357NetbufQueAlloc (\r
1358 VOID\r
1359 );\r
1360\r
1361/**\r
7557df4d 1362 Free a net buffer queue. \r
1363 \r
1364 Decrease the reference count of the net buffer queue by one. The real resource\r
1365 free operation isn't performed until the reference count of the net buffer \r
1366 queue is decreased to 0.\r
97b38d4e 1367\r
7557df4d 1368 @param[in] NbufQue Pointer to the net buffer queue to be freed.\r
97b38d4e 1369\r
1370**/\r
1371VOID\r
1372EFIAPI\r
1373NetbufQueFree (\r
1374 IN NET_BUF_QUEUE *NbufQue\r
1375 );\r
1376\r
1377/**\r
7557df4d 1378 Remove a net buffer from the head in the specific queue and return it.\r
97b38d4e 1379\r
ae213b7d 1380 @param[in, out] NbufQue Pointer to the net buffer queue.\r
97b38d4e 1381\r
7557df4d 1382 @return Pointer to the net buffer removed from the specific queue, \r
1383 or NULL if there is no net buffer in the specific queue.\r
97b38d4e 1384\r
1385**/\r
1386NET_BUF *\r
1387EFIAPI\r
1388NetbufQueRemove (\r
ae213b7d 1389 IN OUT NET_BUF_QUEUE *NbufQue\r
97b38d4e 1390 );\r
1391\r
1392/**\r
7557df4d 1393 Append a net buffer to the net buffer queue.\r
97b38d4e 1394\r
7557df4d 1395 @param[in, out] NbufQue Pointer to the net buffer queue.\r
1396 @param[in, out] Nbuf Pointer to the net buffer to be appended.\r
97b38d4e 1397\r
1398**/\r
1399VOID\r
1400EFIAPI\r
1401NetbufQueAppend (\r
ae213b7d 1402 IN OUT NET_BUF_QUEUE *NbufQue,\r
1403 IN OUT NET_BUF *Nbuf\r
97b38d4e 1404 );\r
1405\r
1406/**\r
7557df4d 1407 Copy Len bytes of data from the net buffer queue at the specific offset to the\r
1408 destination memory.\r
1409 \r
1410 The copying operation is the same as NetbufCopy but applies to the net buffer\r
1411 queue instead of the net buffer.\r
1412 \r
1413 @param[in] NbufQue Pointer to the net buffer queue.\r
1414 @param[in] Offset The sequence number of the first byte to copy.\r
1415 @param[in] Len Length of the data to copy.\r
1416 @param[out] Dest The destination of the data to copy to.\r
1417\r
1418 @return The length of the actual copied data, or 0 if the offset \r
1419 specified exceeds the total size of net buffer queue.\r
97b38d4e 1420\r
1421**/\r
1422UINT32\r
1423EFIAPI\r
1424NetbufQueCopy (\r
1425 IN NET_BUF_QUEUE *NbufQue,\r
1426 IN UINT32 Offset,\r
1427 IN UINT32 Len,\r
ae213b7d 1428 OUT UINT8 *Dest\r
97b38d4e 1429 );\r
1430\r
1431/**\r
e9b67286 1432 Trim Len bytes of data from the queue header and release any net buffer \r
1433 that is trimmed wholely.\r
7557df4d 1434 \r
1435 The trimming operation is the same as NetbufTrim but applies to the net buffer\r
1436 queue instead of the net buffer.\r
97b38d4e 1437\r
ae213b7d 1438 @param[in, out] NbufQue Pointer to the net buffer queue.\r
1439 @param[in] Len Length of the data to trim.\r
97b38d4e 1440\r
7557df4d 1441 @return The actual length of the data trimmed.\r
97b38d4e 1442\r
1443**/\r
1444UINT32\r
1445EFIAPI\r
1446NetbufQueTrim (\r
ae213b7d 1447 IN OUT NET_BUF_QUEUE *NbufQue,\r
97b38d4e 1448 IN UINT32 Len\r
1449 );\r
1450\r
1451\r
1452/**\r
1453 Flush the net buffer queue.\r
1454\r
ae213b7d 1455 @param[in, out] NbufQue Pointer to the queue to be flushed.\r
97b38d4e 1456\r
1457**/\r
1458VOID\r
1459EFIAPI\r
1460NetbufQueFlush (\r
ae213b7d 1461 IN OUT NET_BUF_QUEUE *NbufQue\r
97b38d4e 1462 );\r
1463\r
1464/**\r
7557df4d 1465 Compute the checksum for a bulk of data.\r
97b38d4e 1466\r
ae213b7d 1467 @param[in] Bulk Pointer to the data.\r
1468 @param[in] Len Length of the data, in bytes.\r
97b38d4e 1469\r
ae213b7d 1470 @return The computed checksum.\r
97b38d4e 1471\r
1472**/\r
1473UINT16\r
1474EFIAPI\r
1475NetblockChecksum (\r
1476 IN UINT8 *Bulk,\r
1477 IN UINT32 Len\r
1478 );\r
1479\r
1480/**\r
1481 Add two checksums.\r
1482\r
ae213b7d 1483 @param[in] Checksum1 The first checksum to be added.\r
1484 @param[in] Checksum2 The second checksum to be added.\r
97b38d4e 1485\r
ae213b7d 1486 @return The new checksum.\r
97b38d4e 1487\r
1488**/\r
1489UINT16\r
1490EFIAPI\r
1491NetAddChecksum (\r
1492 IN UINT16 Checksum1,\r
1493 IN UINT16 Checksum2\r
1494 );\r
1495\r
1496/**\r
1497 Compute the checksum for a NET_BUF.\r
1498\r
ae213b7d 1499 @param[in] Nbuf Pointer to the net buffer.\r
97b38d4e 1500\r
ae213b7d 1501 @return The computed checksum.\r
97b38d4e 1502\r
1503**/\r
1504UINT16\r
1505EFIAPI\r
1506NetbufChecksum (\r
1507 IN NET_BUF *Nbuf\r
1508 );\r
1509\r
1510/**\r
7557df4d 1511 Compute the checksum for TCP/UDP pseudo header. \r
1512 \r
1513 Src and Dst are in network byte order, and Len is in host byte order.\r
97b38d4e 1514\r
ae213b7d 1515 @param[in] Src The source address of the packet.\r
1516 @param[in] Dst The destination address of the packet.\r
1517 @param[in] Proto The protocol type of the packet.\r
1518 @param[in] Len The length of the packet.\r
97b38d4e 1519\r
ae213b7d 1520 @return The computed checksum.\r
97b38d4e 1521\r
1522**/\r
1523UINT16\r
1524EFIAPI\r
1525NetPseudoHeadChecksum (\r
1526 IN IP4_ADDR Src,\r
1527 IN IP4_ADDR Dst,\r
1528 IN UINT8 Proto,\r
1529 IN UINT16 Len\r
1530 );\r
1531\r
1532#endif\r