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