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