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