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