]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - NetworkPkg/Include/Library/NetLib.h
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / NetworkPkg / 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 - 2018, Intel Corporation. All rights reserved.<BR>\r
6SPDX-License-Identifier: BSD-2-Clause-Patent\r
7\r
8**/\r
9\r
10#ifndef _NET_LIB_H_\r
11#define _NET_LIB_H_\r
12\r
13#include <Protocol/Ip6.h>\r
14\r
15#include <Library/BaseLib.h>\r
16#include <Library/BaseMemoryLib.h>\r
17\r
18typedef UINT32 IP4_ADDR;\r
19typedef UINT32 TCP_SEQNO;\r
20typedef UINT16 TCP_PORTNO;\r
21\r
22#define NET_ETHER_ADDR_LEN 6\r
23#define NET_IFTYPE_ETHERNET 0x01\r
24\r
25#define NET_VLAN_TAG_LEN 4\r
26#define ETHER_TYPE_VLAN 0x8100\r
27\r
28#define EFI_IP_PROTO_UDP 0x11\r
29#define EFI_IP_PROTO_TCP 0x06\r
30#define EFI_IP_PROTO_ICMP 0x01\r
31#define IP4_PROTO_IGMP 0x02\r
32#define IP6_ICMP 58\r
33#define DNS_MAX_NAME_SIZE 255\r
34#define DNS_MAX_MESSAGE_SIZE 512\r
35\r
36//\r
37// The address classification\r
38//\r
39#define IP4_ADDR_CLASSA 1 // Deprecated\r
40#define IP4_ADDR_CLASSB 2 // Deprecated\r
41#define IP4_ADDR_CLASSC 3 // Deprecated\r
42#define IP4_ADDR_CLASSD 4\r
43#define IP4_ADDR_CLASSE 5\r
44\r
45#define IP4_MASK_NUM 33\r
46#define IP6_PREFIX_NUM 129\r
47\r
48#define IP4_MASK_MAX 32\r
49#define IP6_PREFIX_MAX 128\r
50\r
51#define IP6_HOP_BY_HOP 0\r
52#define IP6_DESTINATION 60\r
53#define IP6_ROUTING 43\r
54#define IP6_FRAGMENT 44\r
55#define IP6_AH 51\r
56#define IP6_ESP 50\r
57#define IP6_NO_NEXT_HEADER 59\r
58\r
59#define IP_VERSION_4 4\r
60#define IP_VERSION_6 6\r
61\r
62#define IP6_PREFIX_LENGTH 64\r
63\r
64//\r
65// DNS QTYPE values\r
66//\r
67#define DNS_TYPE_A 1\r
68#define DNS_TYPE_NS 2\r
69#define DNS_TYPE_CNAME 5\r
70#define DNS_TYPE_SOA 6\r
71#define DNS_TYPE_WKS 11\r
72#define DNS_TYPE_PTR 12\r
73#define DNS_TYPE_HINFO 13\r
74#define DNS_TYPE_MINFO 14\r
75#define DNS_TYPE_MX 15\r
76#define DNS_TYPE_TXT 16\r
77#define DNS_TYPE_AAAA 28\r
78#define DNS_TYPE_SRV_RR 33\r
79#define DNS_TYPE_AXFR 252\r
80#define DNS_TYPE_MAILB 253\r
81#define DNS_TYPE_ANY 255\r
82\r
83//\r
84// DNS QCLASS values\r
85//\r
86#define DNS_CLASS_INET 1\r
87#define DNS_CLASS_CH 3\r
88#define DNS_CLASS_HS 4\r
89#define DNS_CLASS_ANY 255\r
90\r
91//\r
92// Number of 100ns units time Interval for network media state detect\r
93//\r
94#define MEDIA_STATE_DETECT_TIME_INTERVAL 1000000U\r
95\r
96#pragma pack(1)\r
97\r
98//\r
99// Ethernet head definition\r
100//\r
101typedef struct {\r
102 UINT8 DstMac[NET_ETHER_ADDR_LEN];\r
103 UINT8 SrcMac[NET_ETHER_ADDR_LEN];\r
104 UINT16 EtherType;\r
105} ETHER_HEAD;\r
106\r
107//\r
108// 802.1Q VLAN Tag Control Information\r
109//\r
110typedef union {\r
111 struct {\r
112 UINT16 Vid : 12; // Unique VLAN identifier (0 to 4094)\r
113 UINT16 Cfi : 1; // Canonical Format Indicator\r
114 UINT16 Priority : 3; // 802.1Q priority level (0 to 7)\r
115 } Bits;\r
116 UINT16 Uint16;\r
117} VLAN_TCI;\r
118\r
119#define VLAN_TCI_CFI_CANONICAL_MAC 0\r
120#define VLAN_TCI_CFI_NON_CANONICAL_MAC 1\r
121\r
122//\r
123// The EFI_IP4_HEADER is hard to use because the source and\r
124// destination address are defined as EFI_IPv4_ADDRESS, which\r
125// is a structure. Two structures can't be compared or masked\r
126// directly. This is why there is an internal representation.\r
127//\r
128typedef struct {\r
129 UINT8 HeadLen : 4;\r
130 UINT8 Ver : 4;\r
131 UINT8 Tos;\r
132 UINT16 TotalLen;\r
133 UINT16 Id;\r
134 UINT16 Fragment;\r
135 UINT8 Ttl;\r
136 UINT8 Protocol;\r
137 UINT16 Checksum;\r
138 IP4_ADDR Src;\r
139 IP4_ADDR Dst;\r
140} IP4_HEAD;\r
141\r
142//\r
143// ICMP head definition. Each ICMP message is categorized as either an error\r
144// message or query message. Two message types have their own head format.\r
145//\r
146typedef struct {\r
147 UINT8 Type;\r
148 UINT8 Code;\r
149 UINT16 Checksum;\r
150} IP4_ICMP_HEAD;\r
151\r
152typedef struct {\r
153 IP4_ICMP_HEAD Head;\r
154 UINT32 Fourth; // 4th filed of the head, it depends on Type.\r
155 IP4_HEAD IpHead;\r
156} IP4_ICMP_ERROR_HEAD;\r
157\r
158typedef struct {\r
159 IP4_ICMP_HEAD Head;\r
160 UINT16 Id;\r
161 UINT16 Seq;\r
162} IP4_ICMP_QUERY_HEAD;\r
163\r
164typedef struct {\r
165 UINT8 Type;\r
166 UINT8 Code;\r
167 UINT16 Checksum;\r
168} IP6_ICMP_HEAD;\r
169\r
170typedef struct {\r
171 IP6_ICMP_HEAD Head;\r
172 UINT32 Fourth;\r
173 EFI_IP6_HEADER IpHead;\r
174} IP6_ICMP_ERROR_HEAD;\r
175\r
176typedef struct {\r
177 IP6_ICMP_HEAD Head;\r
178 UINT32 Fourth;\r
179} IP6_ICMP_INFORMATION_HEAD;\r
180\r
181//\r
182// UDP header definition\r
183//\r
184typedef struct {\r
185 UINT16 SrcPort;\r
186 UINT16 DstPort;\r
187 UINT16 Length;\r
188 UINT16 Checksum;\r
189} EFI_UDP_HEADER;\r
190\r
191//\r
192// TCP header definition\r
193//\r
194typedef struct {\r
195 TCP_PORTNO SrcPort;\r
196 TCP_PORTNO DstPort;\r
197 TCP_SEQNO Seq;\r
198 TCP_SEQNO Ack;\r
199 UINT8 Res : 4;\r
200 UINT8 HeadLen : 4;\r
201 UINT8 Flag;\r
202 UINT16 Wnd;\r
203 UINT16 Checksum;\r
204 UINT16 Urg;\r
205} TCP_HEAD;\r
206\r
207#pragma pack()\r
208\r
209#define NET_MAC_EQUAL(pMac1, pMac2, Len) \\r
210 (CompareMem ((pMac1), (pMac2), Len) == 0)\r
211\r
212#define NET_MAC_IS_MULTICAST(Mac, BMac, Len) \\r
213 (((*((UINT8 *) Mac) & 0x01) == 0x01) && (!NET_MAC_EQUAL (Mac, BMac, Len)))\r
214\r
215#define NTOHL(x) SwapBytes32 (x)\r
216\r
217#define HTONL(x) NTOHL(x)\r
218\r
219#define NTOHS(x) SwapBytes16 (x)\r
220\r
221#define HTONS(x) NTOHS(x)\r
222#define NTOHLL(x) SwapBytes64 (x)\r
223#define HTONLL(x) NTOHLL(x)\r
224#define NTOHLLL(x) Ip6Swap128 (x)\r
225#define HTONLLL(x) NTOHLLL(x)\r
226\r
227//\r
228// Test the IP's attribute, All the IPs are in host byte order.\r
229//\r
230#define IP4_IS_MULTICAST(Ip) (((Ip) & 0xF0000000) == 0xE0000000)\r
231#define IP4_IS_UNSPECIFIED(Ip) ((Ip) == 0)\r
232#define IP4_IS_LOCAL_BROADCAST(Ip) ((Ip) == 0xFFFFFFFF)\r
233#define IP4_NET_EQUAL(Ip1, Ip2, NetMask) (((Ip1) & (NetMask)) == ((Ip2) & (NetMask)))\r
234#define IP4_IS_VALID_NETMASK(Ip) (NetGetMaskLength (Ip) != (IP4_MASK_MAX + 1))\r
235\r
236#define IP6_IS_MULTICAST(Ip6) (((Ip6)->Addr[0]) == 0xFF)\r
237\r
238//\r
239// Convert the EFI_IP4_ADDRESS to plain UINT32 IP4 address.\r
240//\r
241#define EFI_IP4(EfiIpAddr) (*(IP4_ADDR *) ((EfiIpAddr).Addr))\r
242#define EFI_NTOHL(EfiIp) (NTOHL (EFI_IP4 ((EfiIp))))\r
243#define EFI_IP4_EQUAL(Ip1, Ip2) (CompareMem ((Ip1), (Ip2), sizeof (EFI_IPv4_ADDRESS)) == 0)\r
244\r
245#define EFI_IP6_EQUAL(Ip1, Ip2) (CompareMem ((Ip1), (Ip2), sizeof (EFI_IPv6_ADDRESS)) == 0)\r
246\r
247#define IP4_COPY_ADDRESS(Dest, Src) (CopyMem ((Dest), (Src), sizeof (EFI_IPv4_ADDRESS)))\r
248#define IP6_COPY_ADDRESS(Dest, Src) (CopyMem ((Dest), (Src), sizeof (EFI_IPv6_ADDRESS)))\r
249#define IP6_COPY_LINK_ADDRESS(Mac1, Mac2) (CopyMem ((Mac1), (Mac2), sizeof (EFI_MAC_ADDRESS)))\r
250\r
251//\r
252// The debug level definition. This value is also used as the\r
253// syslog's severity level. Don't change it.\r
254//\r
255#define NETDEBUG_LEVEL_TRACE 5\r
256#define NETDEBUG_LEVEL_WARNING 4\r
257#define NETDEBUG_LEVEL_ERROR 3\r
258\r
259//\r
260// Network debug message is sent out as syslog packet.\r
261//\r
262#define NET_SYSLOG_FACILITY 16 // Syslog local facility local use\r
263#define NET_SYSLOG_PACKET_LEN 512\r
264#define NET_SYSLOG_TX_TIMEOUT (500 * 1000 * 10) // 500ms\r
265#define NET_DEBUG_MSG_LEN 470 // 512 - (ether+ip4+udp4 head length)\r
266\r
267//\r
268// The debug output expects the ASCII format string, Use %a to print ASCII\r
269// string, and %s to print UNICODE string. PrintArg must be enclosed in ().\r
270// For example: NET_DEBUG_TRACE ("Tcp", ("State transit to %a\n", Name));\r
271//\r
272#define NET_DEBUG_TRACE(Module, PrintArg) \\r
273 NetDebugOutput ( \\r
274 NETDEBUG_LEVEL_TRACE, \\r
275 Module, \\r
276 __FILE__, \\r
277 DEBUG_LINE_NUMBER, \\r
278 NetDebugASPrint PrintArg \\r
279 )\r
280\r
281#define NET_DEBUG_WARNING(Module, PrintArg) \\r
282 NetDebugOutput ( \\r
283 NETDEBUG_LEVEL_WARNING, \\r
284 Module, \\r
285 __FILE__, \\r
286 DEBUG_LINE_NUMBER, \\r
287 NetDebugASPrint PrintArg \\r
288 )\r
289\r
290#define NET_DEBUG_ERROR(Module, PrintArg) \\r
291 NetDebugOutput ( \\r
292 NETDEBUG_LEVEL_ERROR, \\r
293 Module, \\r
294 __FILE__, \\r
295 DEBUG_LINE_NUMBER, \\r
296 NetDebugASPrint PrintArg \\r
297 )\r
298\r
299/**\r
300 Allocate a buffer, then format the message to it. This is a\r
301 help function for the NET_DEBUG_XXX macros. The PrintArg of\r
302 these macros treats the variable length print parameters as a\r
303 single parameter, and pass it to the NetDebugASPrint. For\r
304 example, NET_DEBUG_TRACE ("Tcp", ("State transit to %a\n", Name))\r
305 if extracted to:\r
306\r
307 NetDebugOutput (\r
308 NETDEBUG_LEVEL_TRACE,\r
309 "Tcp",\r
310 __FILE__,\r
311 DEBUG_LINE_NUMBER,\r
312 NetDebugASPrint ("State transit to %a\n", Name)\r
313 )\r
314\r
315 @param Format The ASCII format string.\r
316 @param ... The variable length parameter whose format is determined\r
317 by the Format string.\r
318\r
319 @return The buffer containing the formatted message,\r
320 or NULL if memory allocation failed.\r
321\r
322**/\r
323CHAR8 *\r
324EFIAPI\r
325NetDebugASPrint (\r
326 IN CHAR8 *Format,\r
327 ...\r
328 );\r
329\r
330/**\r
331 Builds an UDP4 syslog packet and send it using SNP.\r
332\r
333 This function will locate a instance of SNP then send the message through it.\r
334 Because it isn't open the SNP BY_DRIVER, apply caution when using it.\r
335\r
336 @param Level The severity level of the message.\r
337 @param Module The Module that generates the log.\r
338 @param File The file that contains the log.\r
339 @param Line The exact line that contains the log.\r
340 @param Message The user message to log.\r
341\r
342 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.\r
343 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the packet\r
344 @retval EFI_SUCCESS The log is discard because that it is more verbose\r
345 than the mNetDebugLevelMax. Or, it has been sent out.\r
346**/\r
347EFI_STATUS\r
348EFIAPI\r
349NetDebugOutput (\r
350 IN UINT32 Level,\r
351 IN UINT8 *Module,\r
352 IN UINT8 *File,\r
353 IN UINT32 Line,\r
354 IN UINT8 *Message\r
355 );\r
356\r
357/**\r
358 Return the length of the mask.\r
359\r
360 Return the length of the mask. Valid values are 0 to 32.\r
361 If the mask is invalid, return the invalid length 33, which is IP4_MASK_NUM.\r
362 NetMask is in the host byte order.\r
363\r
364 @param[in] NetMask The netmask to get the length from.\r
365\r
366 @return The length of the netmask, or IP4_MASK_NUM (33) if the mask is invalid.\r
367\r
368**/\r
369INTN\r
370EFIAPI\r
371NetGetMaskLength (\r
372 IN IP4_ADDR NetMask\r
373 );\r
374\r
375/**\r
376 Return the class of the IP address, such as class A, B, C.\r
377 Addr is in host byte order.\r
378\r
379 [ATTENTION]\r
380 Classful addressing (IP class A/B/C) has been deprecated according to RFC4632.\r
381 Caller of this function could only check the returned value against\r
382 IP4_ADDR_CLASSD (multicast) or IP4_ADDR_CLASSE (reserved) now.\r
383\r
384 The address of class A starts with 0.\r
385 If the address belong to class A, return IP4_ADDR_CLASSA.\r
386 The address of class B starts with 10.\r
387 If the address belong to class B, return IP4_ADDR_CLASSB.\r
388 The address of class C starts with 110.\r
389 If the address belong to class C, return IP4_ADDR_CLASSC.\r
390 The address of class D starts with 1110.\r
391 If the address belong to class D, return IP4_ADDR_CLASSD.\r
392 The address of class E starts with 1111.\r
393 If the address belong to class E, return IP4_ADDR_CLASSE.\r
394\r
395\r
396 @param[in] Addr The address to get the class from.\r
397\r
398 @return IP address class, such as IP4_ADDR_CLASSA.\r
399\r
400**/\r
401INTN\r
402EFIAPI\r
403NetGetIpClass (\r
404 IN IP4_ADDR Addr\r
405 );\r
406\r
407/**\r
408 Check whether the IP is a valid unicast address according to\r
409 the netmask.\r
410\r
411 ASSERT if NetMask is zero.\r
412\r
413 If all bits of the host address of IP are 0 or 1, IP is also not a valid unicast address,\r
414 except when the originator is one of the endpoints of a point-to-point link with a 31-bit\r
415 mask (RFC3021), or a 32bit NetMask (all 0xFF) is used for special network environment (e.g.\r
416 PPP link).\r
417\r
418 @param[in] Ip The IP to check against.\r
419 @param[in] NetMask The mask of the IP.\r
420\r
421 @return TRUE if IP is a valid unicast address on the network, otherwise FALSE.\r
422\r
423**/\r
424BOOLEAN\r
425EFIAPI\r
426NetIp4IsUnicast (\r
427 IN IP4_ADDR Ip,\r
428 IN IP4_ADDR NetMask\r
429 );\r
430\r
431/**\r
432 Check whether the incoming IPv6 address is a valid unicast address.\r
433\r
434 ASSERT if Ip6 is NULL.\r
435\r
436 If the address is a multicast address has binary 0xFF at the start, it is not\r
437 a valid unicast address. If the address is unspecified ::, it is not a valid\r
438 unicast address to be assigned to any node. If the address is loopback address\r
439 ::1, it is also not a valid unicast address to be assigned to any physical\r
440 interface.\r
441\r
442 @param[in] Ip6 The IPv6 address to check against.\r
443\r
444 @return TRUE if Ip6 is a valid unicast address on the network, otherwise FALSE.\r
445\r
446**/\r
447BOOLEAN\r
448EFIAPI\r
449NetIp6IsValidUnicast (\r
450 IN EFI_IPv6_ADDRESS *Ip6\r
451 );\r
452\r
453/**\r
454 Check whether the incoming Ipv6 address is the unspecified address or not.\r
455\r
456 ASSERT if Ip6 is NULL.\r
457\r
458 @param[in] Ip6 - Ip6 address, in network order.\r
459\r
460 @retval TRUE - Yes, incoming Ipv6 address is the unspecified address.\r
461 @retval FALSE - The incoming Ipv6 address is not the unspecified address\r
462\r
463**/\r
464BOOLEAN\r
465EFIAPI\r
466NetIp6IsUnspecifiedAddr (\r
467 IN EFI_IPv6_ADDRESS *Ip6\r
468 );\r
469\r
470/**\r
471 Check whether the incoming Ipv6 address is a link-local address.\r
472\r
473 ASSERT if Ip6 is NULL.\r
474\r
475 @param[in] Ip6 - Ip6 address, in network order.\r
476\r
477 @retval TRUE - The incoming Ipv6 address is a link-local address.\r
478 @retval FALSE - The incoming Ipv6 address is not a link-local address.\r
479\r
480**/\r
481BOOLEAN\r
482EFIAPI\r
483NetIp6IsLinkLocalAddr (\r
484 IN EFI_IPv6_ADDRESS *Ip6\r
485 );\r
486\r
487/**\r
488 Check whether the Ipv6 address1 and address2 are on the connected network.\r
489\r
490 ASSERT if Ip1 or Ip2 is NULL.\r
491 ASSERT if PrefixLength exceeds or equals to IP6_PREFIX_MAX.\r
492\r
493 @param[in] Ip1 - Ip6 address1, in network order.\r
494 @param[in] Ip2 - Ip6 address2, in network order.\r
495 @param[in] PrefixLength - The prefix length of the checking net.\r
496\r
497 @retval TRUE - Yes, the Ipv6 address1 and address2 are connected.\r
498 @retval FALSE - No the Ipv6 address1 and address2 are not connected.\r
499\r
500**/\r
501BOOLEAN\r
502EFIAPI\r
503NetIp6IsNetEqual (\r
504 EFI_IPv6_ADDRESS *Ip1,\r
505 EFI_IPv6_ADDRESS *Ip2,\r
506 UINT8 PrefixLength\r
507 );\r
508\r
509/**\r
510 Switches the endianess of an IPv6 address.\r
511\r
512 ASSERT if Ip6 is NULL.\r
513\r
514 This function swaps the bytes in a 128-bit IPv6 address to switch the value\r
515 from little endian to big endian or vice versa. The byte swapped value is\r
516 returned.\r
517\r
518 @param Ip6 Points to an IPv6 address.\r
519\r
520 @return The byte swapped IPv6 address.\r
521\r
522**/\r
523EFI_IPv6_ADDRESS *\r
524EFIAPI\r
525Ip6Swap128 (\r
526 EFI_IPv6_ADDRESS *Ip6\r
527 );\r
528\r
529extern IP4_ADDR gIp4AllMasks[IP4_MASK_NUM];\r
530\r
531extern EFI_IPv4_ADDRESS mZeroIp4Addr;\r
532\r
533#define NET_IS_DIGIT(Ch) (('0' <= (Ch)) && ((Ch) <= '9'))\r
534#define NET_IS_HEX(Ch) ((('0' <= (Ch)) && ((Ch) <= '9')) || (('A' <= (Ch)) && ((Ch) <= 'F')) || (('a' <= (Ch)) && ((Ch) <= 'f')))\r
535#define NET_ROUNDUP(size, unit) (((size) + (unit) - 1) & (~((unit) - 1)))\r
536#define NET_IS_LOWER_CASE_CHAR(Ch) (('a' <= (Ch)) && ((Ch) <= 'z'))\r
537#define NET_IS_UPPER_CASE_CHAR(Ch) (('A' <= (Ch)) && ((Ch) <= 'Z'))\r
538\r
539#define TICKS_PER_MS 10000U\r
540#define TICKS_PER_SECOND 10000000U\r
541\r
542#define NET_RANDOM(Seed) ((UINT32) ((UINT32) (Seed) * 1103515245UL + 12345) % 4294967295UL)\r
543\r
544/**\r
545 Extract a UINT32 from a byte stream.\r
546\r
547 ASSERT if Buf is NULL.\r
548\r
549 This function copies a UINT32 from a byte stream, and then converts it from Network\r
550 byte order to host byte order. Use this function to avoid alignment error.\r
551\r
552 @param[in] Buf The buffer to extract the UINT32.\r
553\r
554 @return The UINT32 extracted.\r
555\r
556**/\r
557UINT32\r
558EFIAPI\r
559NetGetUint32 (\r
560 IN UINT8 *Buf\r
561 );\r
562\r
563/**\r
564 Puts a UINT32 into the byte stream in network byte order.\r
565\r
566 ASSERT if Buf is NULL.\r
567\r
568 Converts a UINT32 from host byte order to network byte order, then copies it to the\r
569 byte stream.\r
570\r
571 @param[in, out] Buf The buffer in which to put the UINT32.\r
572 @param[in] Data The data to be converted and put into the byte stream.\r
573\r
574**/\r
575VOID\r
576EFIAPI\r
577NetPutUint32 (\r
578 IN OUT UINT8 *Buf,\r
579 IN UINT32 Data\r
580 );\r
581\r
582/**\r
583 Initialize a random seed using current time and monotonic count.\r
584\r
585 Get current time and monotonic count first. Then initialize a random seed\r
586 based on some basic mathematics operation on the hour, day, minute, second,\r
587 nanosecond and year of the current time and the monotonic count value.\r
588\r
589 @return The random seed initialized with current time.\r
590\r
591**/\r
592UINT32\r
593EFIAPI\r
594NetRandomInitSeed (\r
595 VOID\r
596 );\r
597\r
598#define NET_LIST_USER_STRUCT(Entry, Type, Field) \\r
599 BASE_CR(Entry, Type, Field)\r
600\r
601#define NET_LIST_USER_STRUCT_S(Entry, Type, Field, Sig) \\r
602 CR(Entry, Type, Field, Sig)\r
603\r
604//\r
605// Iterate through the double linked list. It is NOT delete safe\r
606//\r
607#define NET_LIST_FOR_EACH(Entry, ListHead) \\r
608 for(Entry = (ListHead)->ForwardLink; Entry != (ListHead); Entry = Entry->ForwardLink)\r
609\r
610//\r
611// Iterate through the double linked list. This is delete-safe.\r
612// Don't touch NextEntry. Also, don't use this macro if list\r
613// entries other than the Entry may be deleted when processing\r
614// the current Entry.\r
615//\r
616#define NET_LIST_FOR_EACH_SAFE(Entry, NextEntry, ListHead) \\r
617 for(Entry = (ListHead)->ForwardLink, NextEntry = Entry->ForwardLink; \\r
618 Entry != (ListHead); \\r
619 Entry = NextEntry, NextEntry = Entry->ForwardLink \\r
620 )\r
621\r
622//\r
623// Make sure the list isn't empty before getting the first/last record.\r
624//\r
625#define NET_LIST_HEAD(ListHead, Type, Field) \\r
626 NET_LIST_USER_STRUCT((ListHead)->ForwardLink, Type, Field)\r
627\r
628#define NET_LIST_TAIL(ListHead, Type, Field) \\r
629 NET_LIST_USER_STRUCT((ListHead)->BackLink, Type, Field)\r
630\r
631/**\r
632 Remove the first node entry on the list, and return the removed node entry.\r
633\r
634 Removes the first node entry from a doubly linked list. It is up to the caller of\r
635 this function to release the memory used by the first node, if that is required. On\r
636 exit, the removed node is returned.\r
637\r
638 If Head is NULL, then ASSERT().\r
639 If Head was not initialized, then ASSERT().\r
640 If PcdMaximumLinkedListLength is not zero, and the number of nodes in the\r
641 linked list including the head node is greater than or equal to PcdMaximumLinkedListLength,\r
642 then ASSERT().\r
643\r
644 @param[in, out] Head The list header.\r
645\r
646 @return The first node entry that is removed from the list, NULL if the list is empty.\r
647\r
648**/\r
649LIST_ENTRY *\r
650EFIAPI\r
651NetListRemoveHead (\r
652 IN OUT LIST_ENTRY *Head\r
653 );\r
654\r
655/**\r
656 Remove the last node entry on the list and return the removed node entry.\r
657\r
658 Removes the last node entry from a doubly linked list. It is up to the caller of\r
659 this function to release the memory used by the first node, if that is required. On\r
660 exit, the removed node is returned.\r
661\r
662 If Head is NULL, then ASSERT().\r
663 If Head was not initialized, then ASSERT().\r
664 If PcdMaximumLinkedListLength is not zero, and the number of nodes in the\r
665 linked list including the head node is greater than or equal to PcdMaximumLinkedListLength,\r
666 then ASSERT().\r
667\r
668 @param[in, out] Head The list head.\r
669\r
670 @return The last node entry that is removed from the list, NULL if the list is empty.\r
671\r
672**/\r
673LIST_ENTRY *\r
674EFIAPI\r
675NetListRemoveTail (\r
676 IN OUT LIST_ENTRY *Head\r
677 );\r
678\r
679/**\r
680 Insert a new node entry after a designated node entry of a doubly linked list.\r
681\r
682 ASSERT if PrevEntry or NewEntry is NULL.\r
683\r
684 Inserts a new node entry designated by NewEntry after the node entry designated by PrevEntry\r
685 of the doubly linked list.\r
686\r
687 @param[in, out] PrevEntry The entry after which to insert.\r
688 @param[in, out] NewEntry The new entry to insert.\r
689\r
690**/\r
691VOID\r
692EFIAPI\r
693NetListInsertAfter (\r
694 IN OUT LIST_ENTRY *PrevEntry,\r
695 IN OUT LIST_ENTRY *NewEntry\r
696 );\r
697\r
698/**\r
699 Insert a new node entry before a designated node entry of a doubly linked list.\r
700\r
701 ASSERT if PostEntry or NewEntry is NULL.\r
702\r
703 Inserts a new node entry designated by NewEntry before the node entry designated by PostEntry\r
704 of the doubly linked list.\r
705\r
706 @param[in, out] PostEntry The entry to insert before.\r
707 @param[in, out] NewEntry The new entry to insert.\r
708\r
709**/\r
710VOID\r
711EFIAPI\r
712NetListInsertBefore (\r
713 IN OUT LIST_ENTRY *PostEntry,\r
714 IN OUT LIST_ENTRY *NewEntry\r
715 );\r
716\r
717/**\r
718 Callback function which provided by user to remove one node in NetDestroyLinkList process.\r
719\r
720 @param[in] Entry The entry to be removed.\r
721 @param[in] Context Pointer to the callback context corresponds to the Context in NetDestroyLinkList.\r
722\r
723 @retval EFI_SUCCESS The entry has been removed successfully.\r
724 @retval Others Fail to remove the entry.\r
725\r
726**/\r
727typedef\r
728EFI_STATUS\r
729(EFIAPI *NET_DESTROY_LINK_LIST_CALLBACK)(\r
730 IN LIST_ENTRY *Entry,\r
731 IN VOID *Context OPTIONAL\r
732 );\r
733\r
734/**\r
735 Safe destroy nodes in a linked list, and return the length of the list after all possible operations finished.\r
736\r
737 Destroy network children list by list traversals is not safe due to graph dependencies between nodes.\r
738 This function performs a safe traversal to destroy these nodes by checking to see if the node being destroyed\r
739 has been removed from the list or not.\r
740 If it has been removed, then restart the traversal from the head.\r
741 If it hasn't been removed, then continue with the next node directly.\r
742 This function will end the iterate and return the CallBack's last return value if error happens,\r
743 or return EFI_SUCCESS if 2 complete passes are made with no changes in the number of children in the list.\r
744\r
745 @param[in] List The head of the list.\r
746 @param[in] CallBack Pointer to the callback function to destroy one node in the list.\r
747 @param[in] Context Pointer to the callback function's context: corresponds to the\r
748 parameter Context in NET_DESTROY_LINK_LIST_CALLBACK.\r
749 @param[out] ListLength The length of the link list if the function returns successfully.\r
750\r
751 @retval EFI_SUCCESS Two complete passes are made with no changes in the number of children.\r
752 @retval EFI_INVALID_PARAMETER The input parameter is invalid.\r
753 @retval Others Return the CallBack's last return value.\r
754\r
755**/\r
756EFI_STATUS\r
757EFIAPI\r
758NetDestroyLinkList (\r
759 IN LIST_ENTRY *List,\r
760 IN NET_DESTROY_LINK_LIST_CALLBACK CallBack,\r
761 IN VOID *Context OPTIONAL,\r
762 OUT UINTN *ListLength OPTIONAL\r
763 );\r
764\r
765/**\r
766 This function checks the input Handle to see if it's one of these handles in ChildHandleBuffer.\r
767\r
768 @param[in] Handle Handle to be checked.\r
769 @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer.\r
770 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL\r
771 if NumberOfChildren is 0.\r
772\r
773 @retval TRUE Found the input Handle in ChildHandleBuffer.\r
774 @retval FALSE Can't find the input Handle in ChildHandleBuffer.\r
775\r
776**/\r
777BOOLEAN\r
778EFIAPI\r
779NetIsInHandleBuffer (\r
780 IN EFI_HANDLE Handle,\r
781 IN UINTN NumberOfChildren,\r
782 IN EFI_HANDLE *ChildHandleBuffer OPTIONAL\r
783 );\r
784\r
785//\r
786// Object container: EFI network stack spec defines various kinds of\r
787// tokens. The drivers can share code to manage those objects.\r
788//\r
789typedef struct {\r
790 LIST_ENTRY Link;\r
791 VOID *Key;\r
792 VOID *Value;\r
793} NET_MAP_ITEM;\r
794\r
795typedef struct {\r
796 LIST_ENTRY Used;\r
797 LIST_ENTRY Recycled;\r
798 UINTN Count;\r
799} NET_MAP;\r
800\r
801#define NET_MAP_INCREAMENT 64\r
802\r
803/**\r
804 Initialize the netmap. Netmap is a reposity to keep the <Key, Value> pairs.\r
805\r
806 Initialize the forward and backward links of two head nodes donated by Map->Used\r
807 and Map->Recycled of two doubly linked lists.\r
808 Initializes the count of the <Key, Value> pairs in the netmap to zero.\r
809\r
810 If Map is NULL, then ASSERT().\r
811 If the address of Map->Used is NULL, then ASSERT().\r
812 If the address of Map->Recycled is NULl, then ASSERT().\r
813\r
814 @param[in, out] Map The netmap to initialize.\r
815\r
816**/\r
817VOID\r
818EFIAPI\r
819NetMapInit (\r
820 IN OUT NET_MAP *Map\r
821 );\r
822\r
823/**\r
824 To clean up the netmap, that is, release allocated memories.\r
825\r
826 Removes all nodes of the Used doubly linked list and frees memory of all related netmap items.\r
827 Removes all nodes of the Recycled doubly linked list and free memory of all related netmap items.\r
828 The number of the <Key, Value> pairs in the netmap is set to zero.\r
829\r
830 If Map is NULL, then ASSERT().\r
831\r
832 @param[in, out] Map The netmap to clean up.\r
833\r
834**/\r
835VOID\r
836EFIAPI\r
837NetMapClean (\r
838 IN OUT NET_MAP *Map\r
839 );\r
840\r
841/**\r
842 Test whether the netmap is empty and return true if it is.\r
843\r
844 If the number of the <Key, Value> pairs in the netmap is zero, return TRUE.\r
845\r
846 If Map is NULL, then ASSERT().\r
847\r
848 @param[in] Map The net map to test.\r
849\r
850 @return TRUE if the netmap is empty, otherwise FALSE.\r
851\r
852**/\r
853BOOLEAN\r
854EFIAPI\r
855NetMapIsEmpty (\r
856 IN NET_MAP *Map\r
857 );\r
858\r
859/**\r
860 Return the number of the <Key, Value> pairs in the netmap.\r
861\r
862 If Map is NULL, then ASSERT().\r
863\r
864 @param[in] Map The netmap to get the entry number.\r
865\r
866 @return The entry number in the netmap.\r
867\r
868**/\r
869UINTN\r
870EFIAPI\r
871NetMapGetCount (\r
872 IN NET_MAP *Map\r
873 );\r
874\r
875/**\r
876 Allocate an item to save the <Key, Value> pair to the head of the netmap.\r
877\r
878 Allocate an item to save the <Key, Value> pair and add corresponding node entry\r
879 to the beginning of the Used doubly linked list. The number of the <Key, Value>\r
880 pairs in the netmap increase by 1.\r
881\r
882 If Map is NULL, then ASSERT().\r
883 If Key is NULL, then ASSERT().\r
884\r
885 @param[in, out] Map The netmap to insert into.\r
886 @param[in] Key The user's key.\r
887 @param[in] Value The user's value for the key.\r
888\r
889 @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the item.\r
890 @retval EFI_SUCCESS The item is inserted to the head.\r
891\r
892**/\r
893EFI_STATUS\r
894EFIAPI\r
895NetMapInsertHead (\r
896 IN OUT NET_MAP *Map,\r
897 IN VOID *Key,\r
898 IN VOID *Value OPTIONAL\r
899 );\r
900\r
901/**\r
902 Allocate an item to save the <Key, Value> pair to the tail of the netmap.\r
903\r
904 Allocate an item to save the <Key, Value> pair and add corresponding node entry\r
905 to the tail of the Used doubly linked list. The number of the <Key, Value>\r
906 pairs in the netmap increase by 1.\r
907\r
908 If Map is NULL, then ASSERT().\r
909 If Key is NULL, then ASSERT().\r
910\r
911 @param[in, out] Map The netmap to insert into.\r
912 @param[in] Key The user's key.\r
913 @param[in] Value The user's value for the key.\r
914\r
915 @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the item.\r
916 @retval EFI_SUCCESS The item is inserted to the tail.\r
917\r
918**/\r
919EFI_STATUS\r
920EFIAPI\r
921NetMapInsertTail (\r
922 IN OUT NET_MAP *Map,\r
923 IN VOID *Key,\r
924 IN VOID *Value OPTIONAL\r
925 );\r
926\r
927/**\r
928 Finds the key in the netmap and returns the point to the item containing the Key.\r
929\r
930 Iterate the Used doubly linked list of the netmap to get every item. Compare the key of every\r
931 item with the key to search. It returns the point to the item contains the Key if found.\r
932\r
933 If Map is NULL, then ASSERT().\r
934 If Key is NULL, then ASSERT().\r
935\r
936 @param[in] Map The netmap to search within.\r
937 @param[in] Key The key to search.\r
938\r
939 @return The point to the item contains the Key, or NULL if Key isn't in the map.\r
940\r
941**/\r
942NET_MAP_ITEM *\r
943EFIAPI\r
944NetMapFindKey (\r
945 IN NET_MAP *Map,\r
946 IN VOID *Key\r
947 );\r
948\r
949/**\r
950 Remove the node entry of the item from the netmap and return the key of the removed item.\r
951\r
952 Remove the node entry of the item from the Used doubly linked list of the netmap.\r
953 The number of the <Key, Value> pairs in the netmap decrease by 1. Then add the node\r
954 entry of the item to the Recycled doubly linked list of the netmap. If Value is not NULL,\r
955 Value will point to the value of the item. It returns the key of the removed item.\r
956\r
957 If Map is NULL, then ASSERT().\r
958 If Item is NULL, then ASSERT().\r
959 if item in not in the netmap, then ASSERT().\r
960\r
961 @param[in, out] Map The netmap to remove the item from.\r
962 @param[in, out] Item The item to remove.\r
963 @param[out] Value The variable to receive the value if not NULL.\r
964\r
965 @return The key of the removed item.\r
966\r
967**/\r
968VOID *\r
969EFIAPI\r
970NetMapRemoveItem (\r
971 IN OUT NET_MAP *Map,\r
972 IN OUT NET_MAP_ITEM *Item,\r
973 OUT VOID **Value OPTIONAL\r
974 );\r
975\r
976/**\r
977 Remove the first node entry on the netmap and return the key of the removed item.\r
978\r
979 Remove the first node entry from the Used doubly linked list of the netmap.\r
980 The number of the <Key, Value> pairs in the netmap decrease by 1. Then add the node\r
981 entry to the Recycled doubly linked list of the netmap. If parameter Value is not NULL,\r
982 parameter Value will point to the value of the item. It returns the key of the removed item.\r
983\r
984 If Map is NULL, then ASSERT().\r
985 If the Used doubly linked list is empty, then ASSERT().\r
986\r
987 @param[in, out] Map The netmap to remove the head from.\r
988 @param[out] Value The variable to receive the value if not NULL.\r
989\r
990 @return The key of the item removed.\r
991\r
992**/\r
993VOID *\r
994EFIAPI\r
995NetMapRemoveHead (\r
996 IN OUT NET_MAP *Map,\r
997 OUT VOID **Value OPTIONAL\r
998 );\r
999\r
1000/**\r
1001 Remove the last node entry on the netmap and return the key of the removed item.\r
1002\r
1003 Remove the last node entry from the Used doubly linked list of the netmap.\r
1004 The number of the <Key, Value> pairs in the netmap decrease by 1. Then add the node\r
1005 entry to the Recycled doubly linked list of the netmap. If parameter Value is not NULL,\r
1006 parameter Value will point to the value of the item. It returns the key of the removed item.\r
1007\r
1008 If Map is NULL, then ASSERT().\r
1009 If the Used doubly linked list is empty, then ASSERT().\r
1010\r
1011 @param[in, out] Map The netmap to remove the tail from.\r
1012 @param[out] Value The variable to receive the value if not NULL.\r
1013\r
1014 @return The key of the item removed.\r
1015\r
1016**/\r
1017VOID *\r
1018EFIAPI\r
1019NetMapRemoveTail (\r
1020 IN OUT NET_MAP *Map,\r
1021 OUT VOID **Value OPTIONAL\r
1022 );\r
1023\r
1024typedef\r
1025EFI_STATUS\r
1026(EFIAPI *NET_MAP_CALLBACK)(\r
1027 IN NET_MAP *Map,\r
1028 IN NET_MAP_ITEM *Item,\r
1029 IN VOID *Arg\r
1030 );\r
1031\r
1032/**\r
1033 Iterate through the netmap and call CallBack for each item.\r
1034\r
1035 It will continue the traverse if CallBack returns EFI_SUCCESS, otherwise, break\r
1036 from the loop. It returns the CallBack's last return value. This function is\r
1037 delete safe for the current item.\r
1038\r
1039 If Map is NULL, then ASSERT().\r
1040 If CallBack is NULL, then ASSERT().\r
1041\r
1042 @param[in] Map The Map to iterate through.\r
1043 @param[in] CallBack The callback function to call for each item.\r
1044 @param[in] Arg The opaque parameter to the callback.\r
1045\r
1046 @retval EFI_SUCCESS There is no item in the netmap, or CallBack for each item\r
1047 returns EFI_SUCCESS.\r
1048 @retval Others It returns the CallBack's last return value.\r
1049\r
1050**/\r
1051EFI_STATUS\r
1052EFIAPI\r
1053NetMapIterate (\r
1054 IN NET_MAP *Map,\r
1055 IN NET_MAP_CALLBACK CallBack,\r
1056 IN VOID *Arg OPTIONAL\r
1057 );\r
1058\r
1059//\r
1060// Helper functions to implement driver binding and service binding protocols.\r
1061//\r
1062\r
1063/**\r
1064 Create a child of the service that is identified by ServiceBindingGuid.\r
1065\r
1066 Get the ServiceBinding Protocol first, then use it to create a child.\r
1067\r
1068 If ServiceBindingGuid is NULL, then ASSERT().\r
1069 If ChildHandle is NULL, then ASSERT().\r
1070\r
1071 @param[in] Controller The controller which has the service installed.\r
1072 @param[in] Image The image handle used to open service.\r
1073 @param[in] ServiceBindingGuid The service's Guid.\r
1074 @param[in, out] ChildHandle The handle to receive the created child.\r
1075\r
1076 @retval EFI_SUCCESS The child was successfully created.\r
1077 @retval Others Failed to create the child.\r
1078\r
1079**/\r
1080EFI_STATUS\r
1081EFIAPI\r
1082NetLibCreateServiceChild (\r
1083 IN EFI_HANDLE Controller,\r
1084 IN EFI_HANDLE Image,\r
1085 IN EFI_GUID *ServiceBindingGuid,\r
1086 IN OUT EFI_HANDLE *ChildHandle\r
1087 );\r
1088\r
1089/**\r
1090 Destroy a child of the service that is identified by ServiceBindingGuid.\r
1091\r
1092 Get the ServiceBinding Protocol first, then use it to destroy a child.\r
1093\r
1094 If ServiceBindingGuid is NULL, then ASSERT().\r
1095\r
1096 @param[in] Controller The controller which has the service installed.\r
1097 @param[in] Image The image handle used to open service.\r
1098 @param[in] ServiceBindingGuid The service's Guid.\r
1099 @param[in] ChildHandle The child to destroy.\r
1100\r
1101 @retval EFI_SUCCESS The child was destroyed.\r
1102 @retval Others Failed to destroy the child.\r
1103\r
1104**/\r
1105EFI_STATUS\r
1106EFIAPI\r
1107NetLibDestroyServiceChild (\r
1108 IN EFI_HANDLE Controller,\r
1109 IN EFI_HANDLE Image,\r
1110 IN EFI_GUID *ServiceBindingGuid,\r
1111 IN EFI_HANDLE ChildHandle\r
1112 );\r
1113\r
1114/**\r
1115 Get handle with Simple Network Protocol installed on it.\r
1116\r
1117 There should be MNP Service Binding Protocol installed on the input ServiceHandle.\r
1118 If Simple Network Protocol is already installed on the ServiceHandle, the\r
1119 ServiceHandle will be returned. If SNP is not installed on the ServiceHandle,\r
1120 try to find its parent handle with SNP installed.\r
1121\r
1122 @param[in] ServiceHandle The handle where network service binding protocols are\r
1123 installed on.\r
1124 @param[out] Snp The pointer to store the address of the SNP instance.\r
1125 This is an optional parameter that may be NULL.\r
1126\r
1127 @return The SNP handle, or NULL if not found.\r
1128\r
1129**/\r
1130EFI_HANDLE\r
1131EFIAPI\r
1132NetLibGetSnpHandle (\r
1133 IN EFI_HANDLE ServiceHandle,\r
1134 OUT EFI_SIMPLE_NETWORK_PROTOCOL **Snp OPTIONAL\r
1135 );\r
1136\r
1137/**\r
1138 Retrieve VLAN ID of a VLAN device handle.\r
1139\r
1140 Search VLAN device path node in Device Path of specified ServiceHandle and\r
1141 return its VLAN ID. If no VLAN device path node found, then this ServiceHandle\r
1142 is not a VLAN device handle, and 0 will be returned.\r
1143\r
1144 @param[in] ServiceHandle The handle where network service binding protocols are\r
1145 installed on.\r
1146\r
1147 @return VLAN ID of the device handle, or 0 if not a VLAN device.\r
1148\r
1149**/\r
1150UINT16\r
1151EFIAPI\r
1152NetLibGetVlanId (\r
1153 IN EFI_HANDLE ServiceHandle\r
1154 );\r
1155\r
1156/**\r
1157 Find VLAN device handle with specified VLAN ID.\r
1158\r
1159 The VLAN child device handle is created by VLAN Config Protocol on ControllerHandle.\r
1160 This function will append VLAN device path node to the parent device path,\r
1161 and then use LocateDevicePath() to find the correct VLAN device handle.\r
1162\r
1163 @param[in] ControllerHandle The handle where network service binding protocols are\r
1164 installed on.\r
1165 @param[in] VlanId The configured VLAN ID for the VLAN device.\r
1166\r
1167 @return The VLAN device handle, or NULL if not found.\r
1168\r
1169**/\r
1170EFI_HANDLE\r
1171EFIAPI\r
1172NetLibGetVlanHandle (\r
1173 IN EFI_HANDLE ControllerHandle,\r
1174 IN UINT16 VlanId\r
1175 );\r
1176\r
1177/**\r
1178 Get MAC address associated with the network service handle.\r
1179\r
1180 If MacAddress is NULL, then ASSERT().\r
1181 If AddressSize is NULL, then ASSERT().\r
1182\r
1183 There should be MNP Service Binding Protocol installed on the input ServiceHandle.\r
1184 If SNP is installed on the ServiceHandle or its parent handle, MAC address will\r
1185 be retrieved from SNP. If no SNP found, try to get SNP mode data use MNP.\r
1186\r
1187 @param[in] ServiceHandle The handle where network service binding protocols are\r
1188 installed on.\r
1189 @param[out] MacAddress The pointer to store the returned MAC address.\r
1190 @param[out] AddressSize The length of returned MAC address.\r
1191\r
1192 @retval EFI_SUCCESS MAC address was returned successfully.\r
1193 @retval Others Failed to get SNP mode data.\r
1194\r
1195**/\r
1196EFI_STATUS\r
1197EFIAPI\r
1198NetLibGetMacAddress (\r
1199 IN EFI_HANDLE ServiceHandle,\r
1200 OUT EFI_MAC_ADDRESS *MacAddress,\r
1201 OUT UINTN *AddressSize\r
1202 );\r
1203\r
1204/**\r
1205 Convert MAC address of the NIC associated with specified Service Binding Handle\r
1206 to a unicode string. Callers are responsible for freeing the string storage.\r
1207\r
1208 If MacString is NULL, then ASSERT().\r
1209\r
1210 Locate simple network protocol associated with the Service Binding Handle and\r
1211 get the mac address from SNP. Then convert the mac address into a unicode\r
1212 string. It takes 2 unicode characters to represent a 1 byte binary buffer.\r
1213 Plus one unicode character for the null-terminator.\r
1214\r
1215 @param[in] ServiceHandle The handle where network service binding protocol is\r
1216 installed.\r
1217 @param[in] ImageHandle The image handle used to act as the agent handle to\r
1218 get the simple network protocol. This parameter is\r
1219 optional and may be NULL.\r
1220 @param[out] MacString The pointer to store the address of the string\r
1221 representation of the mac address.\r
1222\r
1223 @retval EFI_SUCCESS Converted the mac address a unicode string successfully.\r
1224 @retval EFI_OUT_OF_RESOURCES There are not enough memory resources.\r
1225 @retval Others Failed to open the simple network protocol.\r
1226\r
1227**/\r
1228EFI_STATUS\r
1229EFIAPI\r
1230NetLibGetMacString (\r
1231 IN EFI_HANDLE ServiceHandle,\r
1232 IN EFI_HANDLE ImageHandle OPTIONAL,\r
1233 OUT CHAR16 **MacString\r
1234 );\r
1235\r
1236/**\r
1237 Detect media status for specified network device.\r
1238\r
1239 If MediaPresent is NULL, then ASSERT().\r
1240\r
1241 The underlying UNDI driver may or may not support reporting media status from\r
1242 GET_STATUS command (PXE_STATFLAGS_GET_STATUS_NO_MEDIA_SUPPORTED). This routine\r
1243 will try to invoke Snp->GetStatus() to get the media status. If media is already\r
1244 present, it returns directly. If media is not present, it will stop SNP and then\r
1245 restart SNP to get the latest media status. This provides an opportunity to get\r
1246 the correct media status for old UNDI driver, which doesn't support reporting\r
1247 media status from GET_STATUS command.\r
1248 Note: there are two limitations for the current algorithm:\r
1249 1) For UNDI with this capability, when the cable is not attached, there will\r
1250 be an redundant Stop/Start() process.\r
1251 2) for UNDI without this capability, in case that network cable is attached when\r
1252 Snp->Initialize() is invoked while network cable is unattached later,\r
1253 NetLibDetectMedia() will report MediaPresent as TRUE, causing upper layer\r
1254 apps to wait for timeout time.\r
1255\r
1256 @param[in] ServiceHandle The handle where network service binding protocols are\r
1257 installed.\r
1258 @param[out] MediaPresent The pointer to store the media status.\r
1259\r
1260 @retval EFI_SUCCESS Media detection success.\r
1261 @retval EFI_INVALID_PARAMETER ServiceHandle is not a valid network device handle.\r
1262 @retval EFI_UNSUPPORTED The network device does not support media detection.\r
1263 @retval EFI_DEVICE_ERROR SNP is in an unknown state.\r
1264\r
1265**/\r
1266EFI_STATUS\r
1267EFIAPI\r
1268NetLibDetectMedia (\r
1269 IN EFI_HANDLE ServiceHandle,\r
1270 OUT BOOLEAN *MediaPresent\r
1271 );\r
1272\r
1273/**\r
1274 Detect media state for a network device. This routine will wait for a period of time at\r
1275 a specified checking interval when a certain network is under connecting until connection\r
1276 process finishes or timeout. If Aip protocol is supported by low layer drivers, three kinds\r
1277 of media states can be detected: EFI_SUCCESS, EFI_NOT_READY and EFI_NO_MEDIA, represents\r
1278 connected state, connecting state and no media state respectively. When function detects\r
1279 the current state is EFI_NOT_READY, it will loop to wait for next time's check until state\r
1280 turns to be EFI_SUCCESS or EFI_NO_MEDIA. If Aip protocol is not supported, function will\r
1281 call NetLibDetectMedia() and return state directly.\r
1282\r
1283 @param[in] ServiceHandle The handle where network service binding protocols are\r
1284 installed on.\r
1285 @param[in] Timeout The maximum number of 100ns units to wait when network\r
1286 is connecting. Zero value means detect once and return\r
1287 immediately.\r
1288 @param[out] MediaState The pointer to the detected media state.\r
1289\r
1290 @retval EFI_SUCCESS Media detection success.\r
1291 @retval EFI_INVALID_PARAMETER ServiceHandle is not a valid network device handle or\r
1292 MediaState pointer is NULL.\r
1293 @retval EFI_DEVICE_ERROR A device error occurred.\r
1294 @retval EFI_TIMEOUT Network is connecting but timeout.\r
1295\r
1296**/\r
1297EFI_STATUS\r
1298EFIAPI\r
1299NetLibDetectMediaWaitTimeout (\r
1300 IN EFI_HANDLE ServiceHandle,\r
1301 IN UINT64 Timeout,\r
1302 OUT EFI_STATUS *MediaState\r
1303 );\r
1304\r
1305/**\r
1306 Create an IPv4 device path node.\r
1307\r
1308 If Node is NULL, then ASSERT().\r
1309\r
1310 The header type of IPv4 device path node is MESSAGING_DEVICE_PATH.\r
1311 The header subtype of IPv4 device path node is MSG_IPv4_DP.\r
1312 The length of the IPv4 device path node in bytes is 19.\r
1313 Get other information from parameters to make up the whole IPv4 device path node.\r
1314\r
1315 @param[in, out] Node The pointer to the IPv4 device path node.\r
1316 @param[in] Controller The controller handle.\r
1317 @param[in] LocalIp The local IPv4 address.\r
1318 @param[in] LocalPort The local port.\r
1319 @param[in] RemoteIp The remote IPv4 address.\r
1320 @param[in] RemotePort The remote port.\r
1321 @param[in] Protocol The protocol type in the IP header.\r
1322 @param[in] UseDefaultAddress Whether this instance is using default address or not.\r
1323\r
1324**/\r
1325VOID\r
1326EFIAPI\r
1327NetLibCreateIPv4DPathNode (\r
1328 IN OUT IPv4_DEVICE_PATH *Node,\r
1329 IN EFI_HANDLE Controller,\r
1330 IN IP4_ADDR LocalIp,\r
1331 IN UINT16 LocalPort,\r
1332 IN IP4_ADDR RemoteIp,\r
1333 IN UINT16 RemotePort,\r
1334 IN UINT16 Protocol,\r
1335 IN BOOLEAN UseDefaultAddress\r
1336 );\r
1337\r
1338/**\r
1339 Create an IPv6 device path node.\r
1340\r
1341 If Node is NULL, then ASSERT().\r
1342 If LocalIp is NULL, then ASSERT().\r
1343 If RemoteIp is NULL, then ASSERT().\r
1344\r
1345 The header type of IPv6 device path node is MESSAGING_DEVICE_PATH.\r
1346 The header subtype of IPv6 device path node is MSG_IPv6_DP.\r
1347 The length of the IPv6 device path node in bytes is 43.\r
1348 Get other information from parameters to make up the whole IPv6 device path node.\r
1349\r
1350 @param[in, out] Node The pointer to the IPv6 device path node.\r
1351 @param[in] Controller The controller handle.\r
1352 @param[in] LocalIp The local IPv6 address.\r
1353 @param[in] LocalPort The local port.\r
1354 @param[in] RemoteIp The remote IPv6 address.\r
1355 @param[in] RemotePort The remote port.\r
1356 @param[in] Protocol The protocol type in the IP header.\r
1357\r
1358**/\r
1359VOID\r
1360EFIAPI\r
1361NetLibCreateIPv6DPathNode (\r
1362 IN OUT IPv6_DEVICE_PATH *Node,\r
1363 IN EFI_HANDLE Controller,\r
1364 IN EFI_IPv6_ADDRESS *LocalIp,\r
1365 IN UINT16 LocalPort,\r
1366 IN EFI_IPv6_ADDRESS *RemoteIp,\r
1367 IN UINT16 RemotePort,\r
1368 IN UINT16 Protocol\r
1369 );\r
1370\r
1371/**\r
1372 Find the UNDI/SNP handle from controller and protocol GUID.\r
1373\r
1374 If ProtocolGuid is NULL, then ASSERT().\r
1375\r
1376 For example, IP will open an MNP child to transmit/receive\r
1377 packets. When MNP is stopped, IP should also be stopped. IP\r
1378 needs to find its own private data that is related the IP's\r
1379 service binding instance that is installed on the UNDI/SNP handle.\r
1380 The controller is then either an MNP or an ARP child handle. Note that\r
1381 IP opens these handles using BY_DRIVER. Use that information to get the\r
1382 UNDI/SNP handle.\r
1383\r
1384 @param[in] Controller The protocol handle to check.\r
1385 @param[in] ProtocolGuid The protocol that is related with the handle.\r
1386\r
1387 @return The UNDI/SNP handle or NULL for errors.\r
1388\r
1389**/\r
1390EFI_HANDLE\r
1391EFIAPI\r
1392NetLibGetNicHandle (\r
1393 IN EFI_HANDLE Controller,\r
1394 IN EFI_GUID *ProtocolGuid\r
1395 );\r
1396\r
1397/**\r
1398 This is the default unload handle for all the network drivers.\r
1399\r
1400 Disconnect the driver specified by ImageHandle from all the devices in the handle database.\r
1401 Uninstall all the protocols installed in the driver entry point.\r
1402\r
1403 @param[in] ImageHandle The drivers' driver image.\r
1404\r
1405 @retval EFI_SUCCESS The image is unloaded.\r
1406 @retval Others Failed to unload the image.\r
1407\r
1408**/\r
1409EFI_STATUS\r
1410EFIAPI\r
1411NetLibDefaultUnload (\r
1412 IN EFI_HANDLE ImageHandle\r
1413 );\r
1414\r
1415/**\r
1416 Convert one Null-terminated ASCII string (decimal dotted) to EFI_IPv4_ADDRESS.\r
1417\r
1418 @param[in] String The pointer to the Ascii string.\r
1419 @param[out] Ip4Address The pointer to the converted IPv4 address.\r
1420\r
1421 @retval EFI_SUCCESS Converted to an IPv4 address successfully.\r
1422 @retval EFI_INVALID_PARAMETER The string is malformatted, or Ip4Address is NULL.\r
1423\r
1424**/\r
1425EFI_STATUS\r
1426EFIAPI\r
1427NetLibAsciiStrToIp4 (\r
1428 IN CONST CHAR8 *String,\r
1429 OUT EFI_IPv4_ADDRESS *Ip4Address\r
1430 );\r
1431\r
1432/**\r
1433 Convert one Null-terminated ASCII string to EFI_IPv6_ADDRESS. The format of the\r
1434 string is defined in RFC 4291 - Text Representation of Addresses.\r
1435\r
1436 @param[in] String The pointer to the Ascii string.\r
1437 @param[out] Ip6Address The pointer to the converted IPv6 address.\r
1438\r
1439 @retval EFI_SUCCESS Converted to an IPv6 address successfully.\r
1440 @retval EFI_INVALID_PARAMETER The string is malformatted, or Ip6Address is NULL.\r
1441\r
1442**/\r
1443EFI_STATUS\r
1444EFIAPI\r
1445NetLibAsciiStrToIp6 (\r
1446 IN CONST CHAR8 *String,\r
1447 OUT EFI_IPv6_ADDRESS *Ip6Address\r
1448 );\r
1449\r
1450/**\r
1451 Convert one Null-terminated Unicode string (decimal dotted) to EFI_IPv4_ADDRESS.\r
1452\r
1453 @param[in] String The pointer to the Ascii string.\r
1454 @param[out] Ip4Address The pointer to the converted IPv4 address.\r
1455\r
1456 @retval EFI_SUCCESS Converted to an IPv4 address successfully.\r
1457 @retval EFI_INVALID_PARAMETER The string is mal-formatted or Ip4Address is NULL.\r
1458\r
1459**/\r
1460EFI_STATUS\r
1461EFIAPI\r
1462NetLibStrToIp4 (\r
1463 IN CONST CHAR16 *String,\r
1464 OUT EFI_IPv4_ADDRESS *Ip4Address\r
1465 );\r
1466\r
1467/**\r
1468 Convert one Null-terminated Unicode string to EFI_IPv6_ADDRESS. The format of\r
1469 the string is defined in RFC 4291 - Text Representation of Addresses.\r
1470\r
1471 @param[in] String The pointer to the Ascii string.\r
1472 @param[out] Ip6Address The pointer to the converted IPv6 address.\r
1473\r
1474 @retval EFI_SUCCESS Converted to an IPv6 address successfully.\r
1475 @retval EFI_INVALID_PARAMETER The string is malformatted or Ip6Address is NULL.\r
1476\r
1477**/\r
1478EFI_STATUS\r
1479EFIAPI\r
1480NetLibStrToIp6 (\r
1481 IN CONST CHAR16 *String,\r
1482 OUT EFI_IPv6_ADDRESS *Ip6Address\r
1483 );\r
1484\r
1485/**\r
1486 Convert one Null-terminated Unicode string to EFI_IPv6_ADDRESS and prefix length.\r
1487 The format of the string is defined in RFC 4291 - Text Representation of Addresses\r
1488 Prefixes: ipv6-address/prefix-length.\r
1489\r
1490 @param[in] String The pointer to the Ascii string.\r
1491 @param[out] Ip6Address The pointer to the converted IPv6 address.\r
1492 @param[out] PrefixLength The pointer to the converted prefix length.\r
1493\r
1494 @retval EFI_SUCCESS Converted to an IPv6 address successfully.\r
1495 @retval EFI_INVALID_PARAMETER The string is malformatted, or Ip6Address is NULL.\r
1496\r
1497**/\r
1498EFI_STATUS\r
1499EFIAPI\r
1500NetLibStrToIp6andPrefix (\r
1501 IN CONST CHAR16 *String,\r
1502 OUT EFI_IPv6_ADDRESS *Ip6Address,\r
1503 OUT UINT8 *PrefixLength\r
1504 );\r
1505\r
1506/**\r
1507\r
1508 Convert one EFI_IPv6_ADDRESS to Null-terminated Unicode string.\r
1509 The text representation of address is defined in RFC 4291.\r
1510\r
1511 @param[in] Ip6Address The pointer to the IPv6 address.\r
1512 @param[out] String The buffer to return the converted string.\r
1513 @param[in] StringSize The length in bytes of the input String.\r
1514\r
1515 @retval EFI_SUCCESS Convert to string successfully.\r
1516 @retval EFI_INVALID_PARAMETER The input parameter is invalid.\r
1517 @retval EFI_BUFFER_TOO_SMALL The BufferSize is too small for the result. BufferSize has been\r
1518 updated with the size needed to complete the request.\r
1519**/\r
1520EFI_STATUS\r
1521EFIAPI\r
1522NetLibIp6ToStr (\r
1523 IN EFI_IPv6_ADDRESS *Ip6Address,\r
1524 OUT CHAR16 *String,\r
1525 IN UINTN StringSize\r
1526 );\r
1527\r
1528//\r
1529// Various signatures\r
1530//\r
1531#define NET_BUF_SIGNATURE SIGNATURE_32 ('n', 'b', 'u', 'f')\r
1532#define NET_VECTOR_SIGNATURE SIGNATURE_32 ('n', 'v', 'e', 'c')\r
1533#define NET_QUE_SIGNATURE SIGNATURE_32 ('n', 'b', 'q', 'u')\r
1534\r
1535#define NET_PROTO_DATA 64 // Opaque buffer for protocols\r
1536#define NET_BUF_HEAD 1 // Trim or allocate space from head\r
1537#define NET_BUF_TAIL 0 // Trim or allocate space from tail\r
1538#define NET_VECTOR_OWN_FIRST 0x01 // We allocated the 1st block in the vector\r
1539\r
1540#define NET_CHECK_SIGNATURE(PData, SIGNATURE) \\r
1541 ASSERT (((PData) != NULL) && ((PData)->Signature == (SIGNATURE)))\r
1542\r
1543//\r
1544// Single memory block in the vector.\r
1545//\r
1546typedef struct {\r
1547 UINT32 Len; // The block's length\r
1548 UINT8 *Bulk; // The block's Data\r
1549} NET_BLOCK;\r
1550\r
1551typedef VOID (EFIAPI *NET_VECTOR_EXT_FREE)(VOID *Arg);\r
1552\r
1553//\r
1554// NET_VECTOR contains several blocks to hold all packet's\r
1555// fragments and other house-keeping stuff for sharing. It\r
1556// doesn't specify the where actual packet fragment begins.\r
1557//\r
1558typedef struct {\r
1559 UINT32 Signature;\r
1560 INTN RefCnt; // Reference count to share NET_VECTOR.\r
1561 NET_VECTOR_EXT_FREE Free; // external function to free NET_VECTOR\r
1562 VOID *Arg; // opaque argument to Free\r
1563 UINT32 Flag; // Flags, NET_VECTOR_OWN_FIRST\r
1564 UINT32 Len; // Total length of the associated BLOCKs\r
1565\r
1566 UINT32 BlockNum;\r
1567 NET_BLOCK Block[1];\r
1568} NET_VECTOR;\r
1569\r
1570//\r
1571// NET_BLOCK_OP operates on the NET_BLOCK. It specifies\r
1572// where the actual fragment begins and ends\r
1573//\r
1574typedef struct {\r
1575 UINT8 *BlockHead; // Block's head, or the smallest valid Head\r
1576 UINT8 *BlockTail; // Block's tail. BlockTail-BlockHead=block length\r
1577 UINT8 *Head; // 1st byte of the data in the block\r
1578 UINT8 *Tail; // Tail of the data in the block, Tail-Head=Size\r
1579 UINT32 Size; // The size of the data\r
1580} NET_BLOCK_OP;\r
1581\r
1582typedef union {\r
1583 IP4_HEAD *Ip4;\r
1584 EFI_IP6_HEADER *Ip6;\r
1585} NET_IP_HEAD;\r
1586\r
1587//\r
1588// NET_BUF is the buffer manage structure used by the\r
1589// network stack. Every network packet may be fragmented. The Vector points to\r
1590// memory blocks used by each fragment, and BlockOp\r
1591// specifies where each fragment begins and ends.\r
1592//\r
1593// It also contains an opaque area for the protocol to store\r
1594// per-packet information. Protocol must be careful not\r
1595// to overwrite the members after that.\r
1596//\r
1597typedef struct {\r
1598 UINT32 Signature;\r
1599 INTN RefCnt;\r
1600 LIST_ENTRY List; // The List this NET_BUF is on\r
1601\r
1602 NET_IP_HEAD Ip; // Network layer header, for fast access\r
1603 TCP_HEAD *Tcp; // Transport layer header, for fast access\r
1604 EFI_UDP_HEADER *Udp; // User Datagram Protocol header\r
1605 UINT8 ProtoData[NET_PROTO_DATA]; // Protocol specific data\r
1606\r
1607 NET_VECTOR *Vector; // The vector containing the packet\r
1608\r
1609 UINT32 BlockOpNum; // Total number of BlockOp in the buffer\r
1610 UINT32 TotalSize; // Total size of the actual packet\r
1611 NET_BLOCK_OP BlockOp[1]; // Specify the position of actual packet\r
1612} NET_BUF;\r
1613\r
1614//\r
1615// A queue of NET_BUFs. It is a thin extension of\r
1616// NET_BUF functions.\r
1617//\r
1618typedef struct {\r
1619 UINT32 Signature;\r
1620 INTN RefCnt;\r
1621 LIST_ENTRY List; // The List this buffer queue is on\r
1622\r
1623 LIST_ENTRY BufList; // list of queued buffers\r
1624 UINT32 BufSize; // total length of DATA in the buffers\r
1625 UINT32 BufNum; // total number of buffers on the chain\r
1626} NET_BUF_QUEUE;\r
1627\r
1628//\r
1629// Pseudo header for TCP and UDP checksum\r
1630//\r
1631#pragma pack(1)\r
1632typedef struct {\r
1633 IP4_ADDR SrcIp;\r
1634 IP4_ADDR DstIp;\r
1635 UINT8 Reserved;\r
1636 UINT8 Protocol;\r
1637 UINT16 Len;\r
1638} NET_PSEUDO_HDR;\r
1639\r
1640typedef struct {\r
1641 EFI_IPv6_ADDRESS SrcIp;\r
1642 EFI_IPv6_ADDRESS DstIp;\r
1643 UINT32 Len;\r
1644 UINT32 Reserved : 24;\r
1645 UINT32 NextHeader : 8;\r
1646} NET_IP6_PSEUDO_HDR;\r
1647#pragma pack()\r
1648\r
1649//\r
1650// The fragment entry table used in network interfaces. This is\r
1651// the same as NET_BLOCK now. Use two different to distinguish\r
1652// the two in case that NET_BLOCK be enhanced later.\r
1653//\r
1654typedef struct {\r
1655 UINT32 Len;\r
1656 UINT8 *Bulk;\r
1657} NET_FRAGMENT;\r
1658\r
1659#define NET_GET_REF(PData) ((PData)->RefCnt++)\r
1660#define NET_PUT_REF(PData) ((PData)->RefCnt--)\r
1661#define NETBUF_FROM_PROTODATA(Info) BASE_CR((Info), NET_BUF, ProtoData)\r
1662\r
1663#define NET_BUF_SHARED(Buf) \\r
1664 (((Buf)->RefCnt > 1) || ((Buf)->Vector->RefCnt > 1))\r
1665\r
1666#define NET_VECTOR_SIZE(BlockNum) \\r
1667 (sizeof (NET_VECTOR) + ((BlockNum) - 1) * sizeof (NET_BLOCK))\r
1668\r
1669#define NET_BUF_SIZE(BlockOpNum) \\r
1670 (sizeof (NET_BUF) + ((BlockOpNum) - 1) * sizeof (NET_BLOCK_OP))\r
1671\r
1672#define NET_HEADSPACE(BlockOp) \\r
1673 ((UINTN)((BlockOp)->Head) - (UINTN)((BlockOp)->BlockHead))\r
1674\r
1675#define NET_TAILSPACE(BlockOp) \\r
1676 ((UINTN)((BlockOp)->BlockTail) - (UINTN)((BlockOp)->Tail))\r
1677\r
1678/**\r
1679 Allocate a single block NET_BUF. Upon allocation, all the\r
1680 free space is in the tail room.\r
1681\r
1682 @param[in] Len The length of the block.\r
1683\r
1684 @return The pointer to the allocated NET_BUF, or NULL if the\r
1685 allocation failed due to resource limitations.\r
1686\r
1687**/\r
1688NET_BUF *\r
1689EFIAPI\r
1690NetbufAlloc (\r
1691 IN UINT32 Len\r
1692 );\r
1693\r
1694/**\r
1695 Free the net buffer and its associated NET_VECTOR.\r
1696\r
1697 Decrease the reference count of the net buffer by one. Free the associated net\r
1698 vector and itself if the reference count of the net buffer is decreased to 0.\r
1699 The net vector free operation decreases the reference count of the net\r
1700 vector by one, and performs the resource free operation when the reference count\r
1701 of the net vector is 0.\r
1702\r
1703 @param[in] Nbuf The pointer to the NET_BUF to be freed.\r
1704\r
1705**/\r
1706VOID\r
1707EFIAPI\r
1708NetbufFree (\r
1709 IN NET_BUF *Nbuf\r
1710 );\r
1711\r
1712/**\r
1713 Get the index of NET_BLOCK_OP that contains the byte at Offset in the net\r
1714 buffer.\r
1715\r
1716 For example, this function can be used to retrieve the IP header in the packet. It\r
1717 also can be used to get the fragment that contains the byte used\r
1718 mainly by the library implementation itself.\r
1719\r
1720 @param[in] Nbuf The pointer to the net buffer.\r
1721 @param[in] Offset The offset of the byte.\r
1722 @param[out] Index Index of the NET_BLOCK_OP that contains the byte at\r
1723 Offset.\r
1724\r
1725 @return The pointer to the Offset'th byte of data in the net buffer, or NULL\r
1726 if there is no such data in the net buffer.\r
1727\r
1728**/\r
1729UINT8 *\r
1730EFIAPI\r
1731NetbufGetByte (\r
1732 IN NET_BUF *Nbuf,\r
1733 IN UINT32 Offset,\r
1734 OUT UINT32 *Index OPTIONAL\r
1735 );\r
1736\r
1737/**\r
1738 Create a copy of the net buffer that shares the associated net vector.\r
1739\r
1740 The reference count of the newly created net buffer is set to 1. The reference\r
1741 count of the associated net vector is increased by one.\r
1742\r
1743 @param[in] Nbuf The pointer to the net buffer to be cloned.\r
1744\r
1745 @return The pointer to the cloned net buffer, or NULL if the\r
1746 allocation failed due to resource limitations.\r
1747\r
1748**/\r
1749NET_BUF *\r
1750EFIAPI\r
1751NetbufClone (\r
1752 IN NET_BUF *Nbuf\r
1753 );\r
1754\r
1755/**\r
1756 Create a duplicated copy of the net buffer with data copied and HeadSpace\r
1757 bytes of head space reserved.\r
1758\r
1759 The duplicated net buffer will allocate its own memory to hold the data of the\r
1760 source net buffer.\r
1761\r
1762 @param[in] Nbuf The pointer to the net buffer to be duplicated from.\r
1763 @param[in, out] Duplicate The pointer to the net buffer to duplicate to. If\r
1764 NULL, a new net buffer is allocated.\r
1765 @param[in] HeadSpace The length of the head space to reserve.\r
1766\r
1767 @return The pointer to the duplicated net buffer, or NULL if\r
1768 the allocation failed due to resource limitations.\r
1769\r
1770**/\r
1771NET_BUF *\r
1772EFIAPI\r
1773NetbufDuplicate (\r
1774 IN NET_BUF *Nbuf,\r
1775 IN OUT NET_BUF *Duplicate OPTIONAL,\r
1776 IN UINT32 HeadSpace\r
1777 );\r
1778\r
1779/**\r
1780 Create a NET_BUF structure which contains Len byte data of Nbuf starting from\r
1781 Offset.\r
1782\r
1783 A new NET_BUF structure will be created but the associated data in NET_VECTOR\r
1784 is shared. This function exists to perform IP packet fragmentation.\r
1785\r
1786 @param[in] Nbuf The pointer to the net buffer to be extracted.\r
1787 @param[in] Offset Starting point of the data to be included in the new\r
1788 net buffer.\r
1789 @param[in] Len The bytes of data to be included in the new net buffer.\r
1790 @param[in] HeadSpace The bytes of the head space to reserve for the protocol header.\r
1791\r
1792 @return The pointer to the cloned net buffer, or NULL if the\r
1793 allocation failed due to resource limitations.\r
1794\r
1795**/\r
1796NET_BUF *\r
1797EFIAPI\r
1798NetbufGetFragment (\r
1799 IN NET_BUF *Nbuf,\r
1800 IN UINT32 Offset,\r
1801 IN UINT32 Len,\r
1802 IN UINT32 HeadSpace\r
1803 );\r
1804\r
1805/**\r
1806 Reserve some space in the header room of the net buffer.\r
1807\r
1808 Upon allocation, all the space is in the tail room of the buffer. Call this\r
1809 function to move space to the header room. This function is quite limited\r
1810 in that it can only reserve space from the first block of an empty NET_BUF not\r
1811 built from the external. However, it should be enough for the network stack.\r
1812\r
1813 @param[in, out] Nbuf The pointer to the net buffer.\r
1814 @param[in] Len The length of buffer to be reserved from the header.\r
1815\r
1816**/\r
1817VOID\r
1818EFIAPI\r
1819NetbufReserve (\r
1820 IN OUT NET_BUF *Nbuf,\r
1821 IN UINT32 Len\r
1822 );\r
1823\r
1824/**\r
1825 Allocate Len bytes of space from the header or tail of the buffer.\r
1826\r
1827 @param[in, out] Nbuf The pointer to the net buffer.\r
1828 @param[in] Len The length of the buffer to be allocated.\r
1829 @param[in] FromHead The flag to indicate whether to reserve the data\r
1830 from head (TRUE) or tail (FALSE).\r
1831\r
1832 @return The pointer to the first byte of the allocated buffer,\r
1833 or NULL, if there is no sufficient space.\r
1834\r
1835**/\r
1836UINT8 *\r
1837EFIAPI\r
1838NetbufAllocSpace (\r
1839 IN OUT NET_BUF *Nbuf,\r
1840 IN UINT32 Len,\r
1841 IN BOOLEAN FromHead\r
1842 );\r
1843\r
1844/**\r
1845 Trim Len bytes from the header or the tail of the net buffer.\r
1846\r
1847 @param[in, out] Nbuf The pointer to the net buffer.\r
1848 @param[in] Len The length of the data to be trimmed.\r
1849 @param[in] FromHead The flag to indicate whether trim data is from the\r
1850 head (TRUE) or the tail (FALSE).\r
1851\r
1852 @return The length of the actual trimmed data, which may be less\r
1853 than Len if the TotalSize of Nbuf is less than Len.\r
1854\r
1855**/\r
1856UINT32\r
1857EFIAPI\r
1858NetbufTrim (\r
1859 IN OUT NET_BUF *Nbuf,\r
1860 IN UINT32 Len,\r
1861 IN BOOLEAN FromHead\r
1862 );\r
1863\r
1864/**\r
1865 Copy Len bytes of data from the specific offset of the net buffer to the\r
1866 destination memory.\r
1867\r
1868 The Len bytes of data may cross several fragments of the net buffer.\r
1869\r
1870 @param[in] Nbuf The pointer to the net buffer.\r
1871 @param[in] Offset The sequence number of the first byte to copy.\r
1872 @param[in] Len The length of the data to copy.\r
1873 @param[in] Dest The destination of the data to copy to.\r
1874\r
1875 @return The length of the actual copied data, or 0 if the offset\r
1876 specified exceeds the total size of net buffer.\r
1877\r
1878**/\r
1879UINT32\r
1880EFIAPI\r
1881NetbufCopy (\r
1882 IN NET_BUF *Nbuf,\r
1883 IN UINT32 Offset,\r
1884 IN UINT32 Len,\r
1885 IN UINT8 *Dest\r
1886 );\r
1887\r
1888/**\r
1889 Build a NET_BUF from external blocks.\r
1890\r
1891 A new NET_BUF structure will be created from external blocks. An additional block\r
1892 of memory will be allocated to hold reserved HeadSpace bytes of header room\r
1893 and existing HeadLen bytes of header, but the external blocks are shared by the\r
1894 net buffer to avoid data copying.\r
1895\r
1896 @param[in] ExtFragment The pointer to the data block.\r
1897 @param[in] ExtNum The number of the data blocks.\r
1898 @param[in] HeadSpace The head space to be reserved.\r
1899 @param[in] HeadLen The length of the protocol header. The function\r
1900 pulls this amount of data into a linear block.\r
1901 @param[in] ExtFree The pointer to the caller-provided free function.\r
1902 @param[in] Arg The argument passed to ExtFree when ExtFree is\r
1903 called.\r
1904\r
1905 @return The pointer to the net buffer built from the data blocks,\r
1906 or NULL if the allocation failed due to resource\r
1907 limit.\r
1908\r
1909**/\r
1910NET_BUF *\r
1911EFIAPI\r
1912NetbufFromExt (\r
1913 IN NET_FRAGMENT *ExtFragment,\r
1914 IN UINT32 ExtNum,\r
1915 IN UINT32 HeadSpace,\r
1916 IN UINT32 HeadLen,\r
1917 IN NET_VECTOR_EXT_FREE ExtFree,\r
1918 IN VOID *Arg OPTIONAL\r
1919 );\r
1920\r
1921/**\r
1922 Build a fragment table to contain the fragments in the net buffer. This is the\r
1923 opposite operation of the NetbufFromExt.\r
1924\r
1925 @param[in] Nbuf Points to the net buffer.\r
1926 @param[in, out] ExtFragment The pointer to the data block.\r
1927 @param[in, out] ExtNum The number of the data blocks.\r
1928\r
1929 @retval EFI_BUFFER_TOO_SMALL The number of non-empty blocks is bigger than\r
1930 ExtNum.\r
1931 @retval EFI_SUCCESS The fragment table was built successfully.\r
1932\r
1933**/\r
1934EFI_STATUS\r
1935EFIAPI\r
1936NetbufBuildExt (\r
1937 IN NET_BUF *Nbuf,\r
1938 IN OUT NET_FRAGMENT *ExtFragment,\r
1939 IN OUT UINT32 *ExtNum\r
1940 );\r
1941\r
1942/**\r
1943 Build a net buffer from a list of net buffers.\r
1944\r
1945 All the fragments will be collected from the list of NEW_BUF, and then a new\r
1946 net buffer will be created through NetbufFromExt.\r
1947\r
1948 @param[in] BufList A List of the net buffer.\r
1949 @param[in] HeadSpace The head space to be reserved.\r
1950 @param[in] HeaderLen The length of the protocol header. The function\r
1951 pulls this amount of data into a linear block.\r
1952 @param[in] ExtFree The pointer to the caller provided free function.\r
1953 @param[in] Arg The argument passed to ExtFree when ExtFree is called.\r
1954\r
1955 @return The pointer to the net buffer built from the list of net\r
1956 buffers.\r
1957\r
1958**/\r
1959NET_BUF *\r
1960EFIAPI\r
1961NetbufFromBufList (\r
1962 IN LIST_ENTRY *BufList,\r
1963 IN UINT32 HeadSpace,\r
1964 IN UINT32 HeaderLen,\r
1965 IN NET_VECTOR_EXT_FREE ExtFree,\r
1966 IN VOID *Arg OPTIONAL\r
1967 );\r
1968\r
1969/**\r
1970 Free a list of net buffers.\r
1971\r
1972 @param[in, out] Head The pointer to the head of linked net buffers.\r
1973\r
1974**/\r
1975VOID\r
1976EFIAPI\r
1977NetbufFreeList (\r
1978 IN OUT LIST_ENTRY *Head\r
1979 );\r
1980\r
1981/**\r
1982 Initiate the net buffer queue.\r
1983\r
1984 @param[in, out] NbufQue The pointer to the net buffer queue to be initialized.\r
1985\r
1986**/\r
1987VOID\r
1988EFIAPI\r
1989NetbufQueInit (\r
1990 IN OUT NET_BUF_QUEUE *NbufQue\r
1991 );\r
1992\r
1993/**\r
1994 Allocate and initialize a net buffer queue.\r
1995\r
1996 @return The pointer to the allocated net buffer queue, or NULL if the\r
1997 allocation failed due to resource limit.\r
1998\r
1999**/\r
2000NET_BUF_QUEUE *\r
2001EFIAPI\r
2002NetbufQueAlloc (\r
2003 VOID\r
2004 );\r
2005\r
2006/**\r
2007 Free a net buffer queue.\r
2008\r
2009 Decrease the reference count of the net buffer queue by one. The real resource\r
2010 free operation isn't performed until the reference count of the net buffer\r
2011 queue is decreased to 0.\r
2012\r
2013 @param[in] NbufQue The pointer to the net buffer queue to be freed.\r
2014\r
2015**/\r
2016VOID\r
2017EFIAPI\r
2018NetbufQueFree (\r
2019 IN NET_BUF_QUEUE *NbufQue\r
2020 );\r
2021\r
2022/**\r
2023 Remove a net buffer from the head in the specific queue and return it.\r
2024\r
2025 @param[in, out] NbufQue The pointer to the net buffer queue.\r
2026\r
2027 @return The pointer to the net buffer removed from the specific queue,\r
2028 or NULL if there is no net buffer in the specific queue.\r
2029\r
2030**/\r
2031NET_BUF *\r
2032EFIAPI\r
2033NetbufQueRemove (\r
2034 IN OUT NET_BUF_QUEUE *NbufQue\r
2035 );\r
2036\r
2037/**\r
2038 Append a net buffer to the net buffer queue.\r
2039\r
2040 @param[in, out] NbufQue The pointer to the net buffer queue.\r
2041 @param[in, out] Nbuf The pointer to the net buffer to be appended.\r
2042\r
2043**/\r
2044VOID\r
2045EFIAPI\r
2046NetbufQueAppend (\r
2047 IN OUT NET_BUF_QUEUE *NbufQue,\r
2048 IN OUT NET_BUF *Nbuf\r
2049 );\r
2050\r
2051/**\r
2052 Copy Len bytes of data from the net buffer queue at the specific offset to the\r
2053 destination memory.\r
2054\r
2055 The copying operation is the same as NetbufCopy, but applies to the net buffer\r
2056 queue instead of the net buffer.\r
2057\r
2058 @param[in] NbufQue The pointer to the net buffer queue.\r
2059 @param[in] Offset The sequence number of the first byte to copy.\r
2060 @param[in] Len The length of the data to copy.\r
2061 @param[out] Dest The destination of the data to copy to.\r
2062\r
2063 @return The length of the actual copied data, or 0 if the offset\r
2064 specified exceeds the total size of net buffer queue.\r
2065\r
2066**/\r
2067UINT32\r
2068EFIAPI\r
2069NetbufQueCopy (\r
2070 IN NET_BUF_QUEUE *NbufQue,\r
2071 IN UINT32 Offset,\r
2072 IN UINT32 Len,\r
2073 OUT UINT8 *Dest\r
2074 );\r
2075\r
2076/**\r
2077 Trim Len bytes of data from the buffer queue and free any net buffer\r
2078 that is completely trimmed.\r
2079\r
2080 The trimming operation is the same as NetbufTrim but applies to the net buffer\r
2081 queue instead of the net buffer.\r
2082\r
2083 @param[in, out] NbufQue The pointer to the net buffer queue.\r
2084 @param[in] Len The length of the data to trim.\r
2085\r
2086 @return The actual length of the data trimmed.\r
2087\r
2088**/\r
2089UINT32\r
2090EFIAPI\r
2091NetbufQueTrim (\r
2092 IN OUT NET_BUF_QUEUE *NbufQue,\r
2093 IN UINT32 Len\r
2094 );\r
2095\r
2096/**\r
2097 Flush the net buffer queue.\r
2098\r
2099 @param[in, out] NbufQue The pointer to the queue to be flushed.\r
2100\r
2101**/\r
2102VOID\r
2103EFIAPI\r
2104NetbufQueFlush (\r
2105 IN OUT NET_BUF_QUEUE *NbufQue\r
2106 );\r
2107\r
2108/**\r
2109 Compute the checksum for a bulk of data.\r
2110\r
2111 @param[in] Bulk The pointer to the data.\r
2112 @param[in] Len The length of the data, in bytes.\r
2113\r
2114 @return The computed checksum.\r
2115\r
2116**/\r
2117UINT16\r
2118EFIAPI\r
2119NetblockChecksum (\r
2120 IN UINT8 *Bulk,\r
2121 IN UINT32 Len\r
2122 );\r
2123\r
2124/**\r
2125 Add two checksums.\r
2126\r
2127 @param[in] Checksum1 The first checksum to be added.\r
2128 @param[in] Checksum2 The second checksum to be added.\r
2129\r
2130 @return The new checksum.\r
2131\r
2132**/\r
2133UINT16\r
2134EFIAPI\r
2135NetAddChecksum (\r
2136 IN UINT16 Checksum1,\r
2137 IN UINT16 Checksum2\r
2138 );\r
2139\r
2140/**\r
2141 Compute the checksum for a NET_BUF.\r
2142\r
2143 @param[in] Nbuf The pointer to the net buffer.\r
2144\r
2145 @return The computed checksum.\r
2146\r
2147**/\r
2148UINT16\r
2149EFIAPI\r
2150NetbufChecksum (\r
2151 IN NET_BUF *Nbuf\r
2152 );\r
2153\r
2154/**\r
2155 Compute the checksum for TCP/UDP pseudo header.\r
2156\r
2157 Src and Dst are in network byte order, and Len is in host byte order.\r
2158\r
2159 @param[in] Src The source address of the packet.\r
2160 @param[in] Dst The destination address of the packet.\r
2161 @param[in] Proto The protocol type of the packet.\r
2162 @param[in] Len The length of the packet.\r
2163\r
2164 @return The computed checksum.\r
2165\r
2166**/\r
2167UINT16\r
2168EFIAPI\r
2169NetPseudoHeadChecksum (\r
2170 IN IP4_ADDR Src,\r
2171 IN IP4_ADDR Dst,\r
2172 IN UINT8 Proto,\r
2173 IN UINT16 Len\r
2174 );\r
2175\r
2176/**\r
2177 Compute the checksum for the TCP6/UDP6 pseudo header.\r
2178\r
2179 Src and Dst are in network byte order, and Len is in host byte order.\r
2180\r
2181 @param[in] Src The source address of the packet.\r
2182 @param[in] Dst The destination address of the packet.\r
2183 @param[in] NextHeader The protocol type of the packet.\r
2184 @param[in] Len The length of the packet.\r
2185\r
2186 @return The computed checksum.\r
2187\r
2188**/\r
2189UINT16\r
2190EFIAPI\r
2191NetIp6PseudoHeadChecksum (\r
2192 IN EFI_IPv6_ADDRESS *Src,\r
2193 IN EFI_IPv6_ADDRESS *Dst,\r
2194 IN UINT8 NextHeader,\r
2195 IN UINT32 Len\r
2196 );\r
2197\r
2198/**\r
2199 The function frees the net buffer which allocated by the IP protocol. It releases\r
2200 only the net buffer and doesn't call the external free function.\r
2201\r
2202 This function should be called after finishing the process of mIpSec->ProcessExt()\r
2203 for outbound traffic. The (EFI_IPSEC2_PROTOCOL)->ProcessExt() allocates a new\r
2204 buffer for the ESP, so there needs a function to free the old net buffer.\r
2205\r
2206 @param[in] Nbuf The network buffer to be freed.\r
2207\r
2208**/\r
2209VOID\r
2210NetIpSecNetbufFree (\r
2211 NET_BUF *Nbuf\r
2212 );\r
2213\r
2214/**\r
2215 This function obtains the system guid from the smbios table.\r
2216\r
2217 If SystemGuid is NULL, then ASSERT().\r
2218\r
2219 @param[out] SystemGuid The pointer of the returned system guid.\r
2220\r
2221 @retval EFI_SUCCESS Successfully obtained the system guid.\r
2222 @retval EFI_NOT_FOUND Did not find the SMBIOS table.\r
2223\r
2224**/\r
2225EFI_STATUS\r
2226EFIAPI\r
2227NetLibGetSystemGuid (\r
2228 OUT EFI_GUID *SystemGuid\r
2229 );\r
2230\r
2231/**\r
2232 Create Dns QName according the queried domain name.\r
2233\r
2234 If DomainName is NULL, then ASSERT().\r
2235\r
2236 QName is a domain name represented as a sequence of labels,\r
2237 where each label consists of a length octet followed by that\r
2238 number of octets. The QName terminates with the zero\r
2239 length octet for the null label of the root. Caller should\r
2240 take responsibility to free the buffer in returned pointer.\r
2241\r
2242 @param DomainName The pointer to the queried domain name string.\r
2243\r
2244 @retval NULL Failed to fill QName.\r
2245 @return QName filled successfully.\r
2246\r
2247**/\r
2248CHAR8 *\r
2249EFIAPI\r
2250NetLibCreateDnsQName (\r
2251 IN CHAR16 *DomainName\r
2252 );\r
2253\r
2254#endif\r