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