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