]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Include/Library/NetLib.h
d19e3ac9b186b0390e460278b4af7d2211f824de
[mirror_edk2.git] / MdeModulePkg / 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 - 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 #include <Library/BaseLib.h>
22
23 typedef UINT32 IP4_ADDR;
24 typedef UINT32 TCP_SEQNO;
25 typedef UINT16 TCP_PORTNO;
26
27
28 #define NET_ETHER_ADDR_LEN 6
29 #define NET_IFTYPE_ETHERNET 0x01
30
31 #define EFI_IP_PROTO_UDP 0x11
32 #define EFI_IP_PROTO_TCP 0x06
33 #define EFI_IP_PROTO_ICMP 0x01
34 #define IP4_PROTO_IGMP 0x02
35 #define IP6_ICMP 58
36
37 //
38 // The address classification
39 //
40 #define IP4_ADDR_CLASSA 1
41 #define IP4_ADDR_CLASSB 2
42 #define IP4_ADDR_CLASSC 3
43 #define IP4_ADDR_CLASSD 4
44 #define IP4_ADDR_CLASSE 5
45
46 #define IP4_MASK_NUM 33
47 #define IP6_PREFIX_NUM 129
48
49 #define IP6_HOP_BY_HOP 0
50 #define IP6_DESTINATION 60
51 #define IP6_FRAGMENT 44
52 #define IP6_AH 51
53 #define IP6_ESP 50
54 #define IP6_NO_NEXT_HEADER 59
55
56 #define IP_VERSION_4 4
57 #define IP_VERSION_6 6
58
59 #pragma pack(1)
60
61 //
62 // Ethernet head definition
63 //
64 typedef struct {
65 UINT8 DstMac [NET_ETHER_ADDR_LEN];
66 UINT8 SrcMac [NET_ETHER_ADDR_LEN];
67 UINT16 EtherType;
68 } ETHER_HEAD;
69
70
71 //
72 // The EFI_IP4_HEADER is hard to use because the source and
73 // destination address are defined as EFI_IPv4_ADDRESS, which
74 // is a structure. Two structures can't be compared or masked
75 // directly. This is why there is an internal representation.
76 //
77 typedef struct {
78 UINT8 HeadLen : 4;
79 UINT8 Ver : 4;
80 UINT8 Tos;
81 UINT16 TotalLen;
82 UINT16 Id;
83 UINT16 Fragment;
84 UINT8 Ttl;
85 UINT8 Protocol;
86 UINT16 Checksum;
87 IP4_ADDR Src;
88 IP4_ADDR Dst;
89 } IP4_HEAD;
90
91
92 //
93 // ICMP head definition. Each ICMP message is categorized as either an error
94 // message or query message. Two message types have their own head format.
95 //
96 typedef struct {
97 UINT8 Type;
98 UINT8 Code;
99 UINT16 Checksum;
100 } IP4_ICMP_HEAD;
101
102 typedef struct {
103 IP4_ICMP_HEAD Head;
104 UINT32 Fourth; // 4th filed of the head, it depends on Type.
105 IP4_HEAD IpHead;
106 } IP4_ICMP_ERROR_HEAD;
107
108 typedef struct {
109 IP4_ICMP_HEAD Head;
110 UINT16 Id;
111 UINT16 Seq;
112 } IP4_ICMP_QUERY_HEAD;
113
114 typedef struct {
115 UINT8 Type;
116 UINT8 Code;
117 UINT16 Checksum;
118 } IP6_ICMP_HEAD;
119
120 typedef struct {
121 IP6_ICMP_HEAD Head;
122 UINT32 Fourth;
123 EFI_IP6_HEADER IpHead;
124 } IP6_ICMP_ERROR_HEAD;
125
126 typedef struct {
127 IP6_ICMP_HEAD Head;
128 UINT32 Fourth;
129 } IP6_ICMP_INFORMATION_HEAD;
130
131 //
132 // UDP header definition
133 //
134 typedef struct {
135 UINT16 SrcPort;
136 UINT16 DstPort;
137 UINT16 Length;
138 UINT16 Checksum;
139 } EFI_UDP_HEADER;
140
141 //
142 // TCP header definition
143 //
144 typedef struct {
145 TCP_PORTNO SrcPort;
146 TCP_PORTNO DstPort;
147 TCP_SEQNO Seq;
148 TCP_SEQNO Ack;
149 UINT8 Res : 4;
150 UINT8 HeadLen : 4;
151 UINT8 Flag;
152 UINT16 Wnd;
153 UINT16 Checksum;
154 UINT16 Urg;
155 } TCP_HEAD;
156
157 #pragma pack()
158
159 #define NET_MAC_EQUAL(pMac1, pMac2, Len) \
160 (CompareMem ((pMac1), (pMac2), Len) == 0)
161
162 #define NET_MAC_IS_MULTICAST(Mac, BMac, Len) \
163 (((*((UINT8 *) Mac) & 0x01) == 0x01) && (!NET_MAC_EQUAL (Mac, BMac, Len)))
164
165 #define NTOHL(x) SwapBytes32 (x)
166
167 #define HTONL(x) NTOHL(x)
168
169 #define NTOHS(x) SwapBytes16 (x)
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 Convert the mac address a unicode string successfully.
980 @retval EFI_OUT_OF_RESOURCES There are not enough memory resource.
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 //
1113 // Single memory block in the vector.
1114 //
1115 typedef struct {
1116 UINT32 Len; // The block's length
1117 UINT8 *Bulk; // The block's Data
1118 } NET_BLOCK;
1119
1120 typedef VOID (*NET_VECTOR_EXT_FREE) (VOID *Arg);
1121
1122 //
1123 //NET_VECTOR contains several blocks to hold all packet's
1124 //fragments and other house-keeping stuff for sharing. It
1125 //doesn't specify the where actual packet fragment begins.
1126 //
1127 typedef struct {
1128 UINT32 Signature;
1129 INTN RefCnt; // Reference count to share NET_VECTOR.
1130 NET_VECTOR_EXT_FREE Free; // external function to free NET_VECTOR
1131 VOID *Arg; // opeque argument to Free
1132 UINT32 Flag; // Flags, NET_VECTOR_OWN_FIRST
1133 UINT32 Len; // Total length of the assocated BLOCKs
1134
1135 UINT32 BlockNum;
1136 NET_BLOCK Block[1];
1137 } NET_VECTOR;
1138
1139 //
1140 //NET_BLOCK_OP operates on the NET_BLOCK. It specifies
1141 //where the actual fragment begins and ends
1142 //
1143 typedef struct {
1144 UINT8 *BlockHead; // Block's head, or the smallest valid Head
1145 UINT8 *BlockTail; // Block's tail. BlockTail-BlockHead=block length
1146 UINT8 *Head; // 1st byte of the data in the block
1147 UINT8 *Tail; // Tail of the data in the block, Tail-Head=Size
1148 UINT32 Size; // The size of the data
1149 } NET_BLOCK_OP;
1150
1151 typedef union {
1152 IP4_HEAD *Ip4;
1153 EFI_IP6_HEADER *Ip6;
1154 } NET_IP_HEAD;
1155
1156 //
1157 //NET_BUF is the buffer manage structure used by the
1158 //network stack. Every network packet may be fragmented. The Vector points to
1159 //memory blocks used by each fragment, and BlockOp
1160 //specifies where each fragment begins and ends.
1161 //
1162 //It also contains an opaque area for the protocol to store
1163 //per-packet information. Protocol must be careful not
1164 //to overwrite the members after that.
1165 //
1166 typedef struct {
1167 UINT32 Signature;
1168 INTN RefCnt;
1169 LIST_ENTRY List; // The List this NET_BUF is on
1170
1171 NET_IP_HEAD Ip; // Network layer header, for fast access
1172 TCP_HEAD *Tcp; // Transport layer header, for fast access
1173 EFI_UDP_HEADER *Udp; // User Datagram Protocol header
1174 UINT8 ProtoData [NET_PROTO_DATA]; //Protocol specific data
1175
1176 NET_VECTOR *Vector; // The vector containing the packet
1177
1178 UINT32 BlockOpNum; // Total number of BlockOp in the buffer
1179 UINT32 TotalSize; // Total size of the actual packet
1180 NET_BLOCK_OP BlockOp[1]; // Specify the position of actual packet
1181 } NET_BUF;
1182
1183 //
1184 //A queue of NET_BUFs. It is a thin extension of
1185 //NET_BUF functions.
1186 //
1187 typedef struct {
1188 UINT32 Signature;
1189 INTN RefCnt;
1190 LIST_ENTRY List; // The List this buffer queue is on
1191
1192 LIST_ENTRY BufList; // list of queued buffers
1193 UINT32 BufSize; // total length of DATA in the buffers
1194 UINT32 BufNum; // total number of buffers on the chain
1195 } NET_BUF_QUEUE;
1196
1197 //
1198 // Pseudo header for TCP and UDP checksum
1199 //
1200 #pragma pack(1)
1201 typedef struct {
1202 IP4_ADDR SrcIp;
1203 IP4_ADDR DstIp;
1204 UINT8 Reserved;
1205 UINT8 Protocol;
1206 UINT16 Len;
1207 } NET_PSEUDO_HDR;
1208
1209 typedef struct {
1210 EFI_IPv6_ADDRESS SrcIp;
1211 EFI_IPv6_ADDRESS DstIp;
1212 UINT32 Len;
1213 UINT32 Reserved:24;
1214 UINT32 NextHeader:8;
1215 } NET_IP6_PSEUDO_HDR;
1216 #pragma pack()
1217
1218 //
1219 // The fragment entry table used in network interfaces. This is
1220 // the same as NET_BLOCK now. Use two different to distinguish
1221 // the two in case that NET_BLOCK be enhanced later.
1222 //
1223 typedef struct {
1224 UINT32 Len;
1225 UINT8 *Bulk;
1226 } NET_FRAGMENT;
1227
1228 #define NET_GET_REF(PData) ((PData)->RefCnt++)
1229 #define NET_PUT_REF(PData) ((PData)->RefCnt--)
1230 #define NETBUF_FROM_PROTODATA(Info) BASE_CR((Info), NET_BUF, ProtoData)
1231
1232 #define NET_BUF_SHARED(Buf) \
1233 (((Buf)->RefCnt > 1) || ((Buf)->Vector->RefCnt > 1))
1234
1235 #define NET_VECTOR_SIZE(BlockNum) \
1236 (sizeof (NET_VECTOR) + ((BlockNum) - 1) * sizeof (NET_BLOCK))
1237
1238 #define NET_BUF_SIZE(BlockOpNum) \
1239 (sizeof (NET_BUF) + ((BlockOpNum) - 1) * sizeof (NET_BLOCK_OP))
1240
1241 #define NET_HEADSPACE(BlockOp) \
1242 (UINTN)((BlockOp)->Head - (BlockOp)->BlockHead)
1243
1244 #define NET_TAILSPACE(BlockOp) \
1245 (UINTN)((BlockOp)->BlockTail - (BlockOp)->Tail)
1246
1247 /**
1248 Allocate a single block NET_BUF. Upon allocation, all the
1249 free space is in the tail room.
1250
1251 @param[in] Len The length of the block.
1252
1253 @return Pointer to the allocated NET_BUF, or NULL if the
1254 allocation failed due to resource limit.
1255
1256 **/
1257 NET_BUF *
1258 EFIAPI
1259 NetbufAlloc (
1260 IN UINT32 Len
1261 );
1262
1263 /**
1264 Free the net buffer and its associated NET_VECTOR.
1265
1266 Decrease the reference count of the net buffer by one. Free the associated net
1267 vector and itself if the reference count of the net buffer is decreased to 0.
1268 The net vector free operation decreases the reference count of the net
1269 vector by one, and performs the resource free operation when the reference count
1270 of the net vector is 0.
1271
1272 @param[in] Nbuf Pointer to the NET_BUF to be freed.
1273
1274 **/
1275 VOID
1276 EFIAPI
1277 NetbufFree (
1278 IN NET_BUF *Nbuf
1279 );
1280
1281 /**
1282 Get the index of NET_BLOCK_OP that contains the byte at Offset in the net
1283 buffer.
1284
1285 For example, this function can be used to retrieve the IP header in the packet. It
1286 also can be used to get the fragment that contains the byte used
1287 mainly by the library implementation itself.
1288
1289 @param[in] Nbuf Pointer to the net buffer.
1290 @param[in] Offset The offset of the byte.
1291 @param[out] Index Index of the NET_BLOCK_OP that contains the byte at
1292 Offset.
1293
1294 @return Pointer to the Offset'th byte of data in the net buffer, or NULL
1295 if there is no such data in the net buffer.
1296
1297 **/
1298 UINT8 *
1299 EFIAPI
1300 NetbufGetByte (
1301 IN NET_BUF *Nbuf,
1302 IN UINT32 Offset,
1303 OUT UINT32 *Index OPTIONAL
1304 );
1305
1306 /**
1307 Create a copy of the net buffer that shares the associated net vector.
1308
1309 The reference count of the newly created net buffer is set to 1. The reference
1310 count of the associated net vector is increased by one.
1311
1312 @param[in] Nbuf Pointer to the net buffer to be cloned.
1313
1314 @return Pointer to the cloned net buffer, or NULL if the
1315 allocation failed due to resource limit.
1316
1317 **/
1318 NET_BUF *
1319 EFIAPI
1320 NetbufClone (
1321 IN NET_BUF *Nbuf
1322 );
1323
1324 /**
1325 Create a duplicated copy of the net buffer with data copied and HeadSpace
1326 bytes of head space reserved.
1327
1328 The duplicated net buffer will allocate its own memory to hold the data of the
1329 source net buffer.
1330
1331 @param[in] Nbuf Pointer to the net buffer to be duplicated from.
1332 @param[in, out] Duplicate Pointer to the net buffer to duplicate to, if
1333 NULL a new net buffer is allocated.
1334 @param[in] HeadSpace Length of the head space to reserve.
1335
1336 @return Pointer to the duplicated net buffer, or NULL if
1337 the allocation failed due to resource limit.
1338
1339 **/
1340 NET_BUF *
1341 EFIAPI
1342 NetbufDuplicate (
1343 IN NET_BUF *Nbuf,
1344 IN OUT NET_BUF *Duplicate OPTIONAL,
1345 IN UINT32 HeadSpace
1346 );
1347
1348 /**
1349 Create a NET_BUF structure which contains Len byte data of Nbuf starting from
1350 Offset.
1351
1352 A new NET_BUF structure will be created but the associated data in NET_VECTOR
1353 is shared. This function exists to do IP packet fragmentation.
1354
1355 @param[in] Nbuf Pointer to the net buffer to be extracted.
1356 @param[in] Offset Starting point of the data to be included in the new
1357 net buffer.
1358 @param[in] Len Bytes of data to be included in the new net buffer.
1359 @param[in] HeadSpace Bytes of head space to reserve for protocol header.
1360
1361 @return Pointer to the cloned net buffer, or NULL if the
1362 allocation failed due to resource limit.
1363
1364 **/
1365 NET_BUF *
1366 EFIAPI
1367 NetbufGetFragment (
1368 IN NET_BUF *Nbuf,
1369 IN UINT32 Offset,
1370 IN UINT32 Len,
1371 IN UINT32 HeadSpace
1372 );
1373
1374 /**
1375 Reserve some space in the header room of the net buffer.
1376
1377 Upon allocation, all the space is in the tail room of the buffer. Call this
1378 function to move some space to the header room. This function is quite limited
1379 in that it can only reserve space from the first block of an empty NET_BUF not
1380 built from the external. But it should be enough for the network stack.
1381
1382 @param[in, out] Nbuf Pointer to the net buffer.
1383 @param[in] Len The length of buffer to be reserved from the header.
1384
1385 **/
1386 VOID
1387 EFIAPI
1388 NetbufReserve (
1389 IN OUT NET_BUF *Nbuf,
1390 IN UINT32 Len
1391 );
1392
1393 /**
1394 Allocate Len bytes of space from the header or tail of the buffer.
1395
1396 @param[in, out] Nbuf Pointer to the net buffer.
1397 @param[in] Len The length of the buffer to be allocated.
1398 @param[in] FromHead The flag to indicate whether reserve the data
1399 from head (TRUE) or tail (FALSE).
1400
1401 @return Pointer to the first byte of the allocated buffer,
1402 or NULL if there is no sufficient space.
1403
1404 **/
1405 UINT8*
1406 EFIAPI
1407 NetbufAllocSpace (
1408 IN OUT NET_BUF *Nbuf,
1409 IN UINT32 Len,
1410 IN BOOLEAN FromHead
1411 );
1412
1413 /**
1414 Trim Len bytes from the header or tail of the net buffer.
1415
1416 @param[in, out] Nbuf Pointer to the net buffer.
1417 @param[in] Len The length of the data to be trimmed.
1418 @param[in] FromHead The flag to indicate whether trim data from head
1419 (TRUE) or tail (FALSE).
1420
1421 @return Length of the actually trimmed data, which may be less
1422 than Len if the TotalSize of Nbuf is less than Len.
1423
1424 **/
1425 UINT32
1426 EFIAPI
1427 NetbufTrim (
1428 IN OUT NET_BUF *Nbuf,
1429 IN UINT32 Len,
1430 IN BOOLEAN FromHead
1431 );
1432
1433 /**
1434 Copy Len bytes of data from the specific offset of the net buffer to the
1435 destination memory.
1436
1437 The Len bytes of data may cross several fragments of the net buffer.
1438
1439 @param[in] Nbuf Pointer to the net buffer.
1440 @param[in] Offset The sequence number of the first byte to copy.
1441 @param[in] Len Length of the data to copy.
1442 @param[in] Dest The destination of the data to copy to.
1443
1444 @return The length of the actual copied data, or 0 if the offset
1445 specified exceeds the total size of net buffer.
1446
1447 **/
1448 UINT32
1449 EFIAPI
1450 NetbufCopy (
1451 IN NET_BUF *Nbuf,
1452 IN UINT32 Offset,
1453 IN UINT32 Len,
1454 IN UINT8 *Dest
1455 );
1456
1457 /**
1458 Build a NET_BUF from external blocks.
1459
1460 A new NET_BUF structure will be created from external blocks. An additional block
1461 of memory will be allocated to hold reserved HeadSpace bytes of header room
1462 and existing HeadLen bytes of header, but the external blocks are shared by the
1463 net buffer to avoid data copying.
1464
1465 @param[in] ExtFragment Pointer to the data block.
1466 @param[in] ExtNum The number of the data blocks.
1467 @param[in] HeadSpace The head space to be reserved.
1468 @param[in] HeadLen The length of the protocol header. The function
1469 pulls this amount of data into a linear block.
1470 @param[in] ExtFree Pointer to the caller-provided free function.
1471 @param[in] Arg The argument passed to ExtFree when ExtFree is
1472 called.
1473
1474 @return Pointer to the net buffer built from the data blocks,
1475 or NULL if the allocation failed due to resource
1476 limit.
1477
1478 **/
1479 NET_BUF *
1480 EFIAPI
1481 NetbufFromExt (
1482 IN NET_FRAGMENT *ExtFragment,
1483 IN UINT32 ExtNum,
1484 IN UINT32 HeadSpace,
1485 IN UINT32 HeadLen,
1486 IN NET_VECTOR_EXT_FREE ExtFree,
1487 IN VOID *Arg OPTIONAL
1488 );
1489
1490 /**
1491 Build a fragment table to contain the fragments in the net buffer. This is the
1492 opposite operation of the NetbufFromExt.
1493
1494 @param[in] Nbuf Point to the net buffer.
1495 @param[in, out] ExtFragment Pointer to the data block.
1496 @param[in, out] ExtNum The number of the data blocks.
1497
1498 @retval EFI_BUFFER_TOO_SMALL The number of non-empty blocks is bigger than
1499 ExtNum.
1500 @retval EFI_SUCCESS Fragment table is built successfully.
1501
1502 **/
1503 EFI_STATUS
1504 EFIAPI
1505 NetbufBuildExt (
1506 IN NET_BUF *Nbuf,
1507 IN OUT NET_FRAGMENT *ExtFragment,
1508 IN OUT UINT32 *ExtNum
1509 );
1510
1511 /**
1512 Build a net buffer from a list of net buffers.
1513
1514 All the fragments will be collected from the list of NEW_BUF and then a new
1515 net buffer will be created through NetbufFromExt.
1516
1517 @param[in] BufList A List of the net buffer.
1518 @param[in] HeadSpace The head space to be reserved.
1519 @param[in] HeaderLen The length of the protocol header. The function
1520 pulls this amount of data into a linear block.
1521 @param[in] ExtFree Pointer to the caller provided free function.
1522 @param[in] Arg The argument passed to ExtFree when ExtFree is called.
1523
1524 @return Pointer to the net buffer built from the list of net
1525 buffers.
1526
1527 **/
1528 NET_BUF *
1529 EFIAPI
1530 NetbufFromBufList (
1531 IN LIST_ENTRY *BufList,
1532 IN UINT32 HeadSpace,
1533 IN UINT32 HeaderLen,
1534 IN NET_VECTOR_EXT_FREE ExtFree,
1535 IN VOID *Arg OPTIONAL
1536 );
1537
1538 /**
1539 Free a list of net buffers.
1540
1541 @param[in, out] Head Pointer to the head of linked net buffers.
1542
1543 **/
1544 VOID
1545 EFIAPI
1546 NetbufFreeList (
1547 IN OUT LIST_ENTRY *Head
1548 );
1549
1550 /**
1551 Initiate the net buffer queue.
1552
1553 @param[in, out] NbufQue Pointer to the net buffer queue to be initialized.
1554
1555 **/
1556 VOID
1557 EFIAPI
1558 NetbufQueInit (
1559 IN OUT NET_BUF_QUEUE *NbufQue
1560 );
1561
1562 /**
1563 Allocate and initialize a net buffer queue.
1564
1565 @return Pointer to the allocated net buffer queue, or NULL if the
1566 allocation failed due to resource limit.
1567
1568 **/
1569 NET_BUF_QUEUE *
1570 EFIAPI
1571 NetbufQueAlloc (
1572 VOID
1573 );
1574
1575 /**
1576 Free a net buffer queue.
1577
1578 Decrease the reference count of the net buffer queue by one. The real resource
1579 free operation isn't performed until the reference count of the net buffer
1580 queue is decreased to 0.
1581
1582 @param[in] NbufQue Pointer to the net buffer queue to be freed.
1583
1584 **/
1585 VOID
1586 EFIAPI
1587 NetbufQueFree (
1588 IN NET_BUF_QUEUE *NbufQue
1589 );
1590
1591 /**
1592 Remove a net buffer from the head in the specific queue and return it.
1593
1594 @param[in, out] NbufQue Pointer to the net buffer queue.
1595
1596 @return Pointer to the net buffer removed from the specific queue,
1597 or NULL if there is no net buffer in the specific queue.
1598
1599 **/
1600 NET_BUF *
1601 EFIAPI
1602 NetbufQueRemove (
1603 IN OUT NET_BUF_QUEUE *NbufQue
1604 );
1605
1606 /**
1607 Append a net buffer to the net buffer queue.
1608
1609 @param[in, out] NbufQue Pointer to the net buffer queue.
1610 @param[in, out] Nbuf Pointer to the net buffer to be appended.
1611
1612 **/
1613 VOID
1614 EFIAPI
1615 NetbufQueAppend (
1616 IN OUT NET_BUF_QUEUE *NbufQue,
1617 IN OUT NET_BUF *Nbuf
1618 );
1619
1620 /**
1621 Copy Len bytes of data from the net buffer queue at the specific offset to the
1622 destination memory.
1623
1624 The copying operation is the same as NetbufCopy but applies to the net buffer
1625 queue instead of the net buffer.
1626
1627 @param[in] NbufQue Pointer to the net buffer queue.
1628 @param[in] Offset The sequence number of the first byte to copy.
1629 @param[in] Len Length of the data to copy.
1630 @param[out] Dest The destination of the data to copy to.
1631
1632 @return The length of the actual copied data, or 0 if the offset
1633 specified exceeds the total size of net buffer queue.
1634
1635 **/
1636 UINT32
1637 EFIAPI
1638 NetbufQueCopy (
1639 IN NET_BUF_QUEUE *NbufQue,
1640 IN UINT32 Offset,
1641 IN UINT32 Len,
1642 OUT UINT8 *Dest
1643 );
1644
1645 /**
1646 Trim Len bytes of data from the queue header and release any net buffer
1647 that is trimmed wholely.
1648
1649 The trimming operation is the same as NetbufTrim but applies to the net buffer
1650 queue instead of the net buffer.
1651
1652 @param[in, out] NbufQue Pointer to the net buffer queue.
1653 @param[in] Len Length of the data to trim.
1654
1655 @return The actual length of the data trimmed.
1656
1657 **/
1658 UINT32
1659 EFIAPI
1660 NetbufQueTrim (
1661 IN OUT NET_BUF_QUEUE *NbufQue,
1662 IN UINT32 Len
1663 );
1664
1665
1666 /**
1667 Flush the net buffer queue.
1668
1669 @param[in, out] NbufQue Pointer to the queue to be flushed.
1670
1671 **/
1672 VOID
1673 EFIAPI
1674 NetbufQueFlush (
1675 IN OUT NET_BUF_QUEUE *NbufQue
1676 );
1677
1678 /**
1679 Compute the checksum for a bulk of data.
1680
1681 @param[in] Bulk Pointer to the data.
1682 @param[in] Len Length of the data, in bytes.
1683
1684 @return The computed checksum.
1685
1686 **/
1687 UINT16
1688 EFIAPI
1689 NetblockChecksum (
1690 IN UINT8 *Bulk,
1691 IN UINT32 Len
1692 );
1693
1694 /**
1695 Add two checksums.
1696
1697 @param[in] Checksum1 The first checksum to be added.
1698 @param[in] Checksum2 The second checksum to be added.
1699
1700 @return The new checksum.
1701
1702 **/
1703 UINT16
1704 EFIAPI
1705 NetAddChecksum (
1706 IN UINT16 Checksum1,
1707 IN UINT16 Checksum2
1708 );
1709
1710 /**
1711 Compute the checksum for a NET_BUF.
1712
1713 @param[in] Nbuf Pointer to the net buffer.
1714
1715 @return The computed checksum.
1716
1717 **/
1718 UINT16
1719 EFIAPI
1720 NetbufChecksum (
1721 IN NET_BUF *Nbuf
1722 );
1723
1724 /**
1725 Compute the checksum for TCP/UDP pseudo header.
1726
1727 Src and Dst are in network byte order, and Len is in host byte order.
1728
1729 @param[in] Src The source address of the packet.
1730 @param[in] Dst The destination address of the packet.
1731 @param[in] Proto The protocol type of the packet.
1732 @param[in] Len The length of the packet.
1733
1734 @return The computed checksum.
1735
1736 **/
1737 UINT16
1738 EFIAPI
1739 NetPseudoHeadChecksum (
1740 IN IP4_ADDR Src,
1741 IN IP4_ADDR Dst,
1742 IN UINT8 Proto,
1743 IN UINT16 Len
1744 );
1745
1746 /**
1747 Compute the checksum for TCP6/UDP6 pseudo header.
1748
1749 Src and Dst are in network byte order, and Len is in host byte order.
1750
1751 @param[in] Src The source address of the packet.
1752 @param[in] Dst The destination address of the packet.
1753 @param[in] NextHeader The protocol type of the packet.
1754 @param[in] Len The length of the packet.
1755
1756 @return The computed checksum.
1757
1758 **/
1759 UINT16
1760 NetIp6PseudoHeadChecksum (
1761 IN EFI_IPv6_ADDRESS *Src,
1762 IN EFI_IPv6_ADDRESS *Dst,
1763 IN UINT8 NextHeader,
1764 IN UINT32 Len
1765 );
1766 #endif