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