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