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