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