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