3 Copyright (c) 2005 - 2007, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 Library for the UEFI network stack.
27 #include <Library/BaseMemoryLib.h>
28 #include <Library/MemoryAllocationLib.h>
29 #include <Protocol/DriverBinding.h>
30 #include <Protocol/ComponentName.h>
31 #include <Protocol/DriverConfiguration.h>
32 #include <Protocol/DriverDiagnostics.h>
33 #include <Protocol/Dpc.h>
35 typedef UINT32 IP4_ADDR
;
36 typedef UINT32 TCP_SEQNO
;
37 typedef UINT16 TCP_PORTNO
;
40 NET_ETHER_ADDR_LEN
= 6,
41 NET_IFTYPE_ETHERNET
= 0x01,
43 EFI_IP_PROTO_UDP
= 0x11,
44 EFI_IP_PROTO_TCP
= 0x06,
45 EFI_IP_PROTO_ICMP
= 0x01,
48 // The address classfication
62 // Ethernet head definition
65 UINT8 DstMac
[NET_ETHER_ADDR_LEN
];
66 UINT8 SrcMac
[NET_ETHER_ADDR_LEN
];
72 // The EFI_IP4_HEADER is hard to use because the source and
73 // destination address are defined as EFI_IPv4_ADDRESS, which
74 // is a structure. Two structures can't be compared or masked
75 // directly. This is why there is an internal representation.
93 // ICMP head definition. ICMP message is categoried as either an error
94 // message or query message. Two message types have their own head format.
104 UINT32 Fourth
; // 4th filed of the head, it depends on Type.
106 } IP4_ICMP_ERROR_HEAD
;
112 } IP4_ICMP_QUERY_HEAD
;
116 // UDP header definition
127 // TCP header definition
144 #define NET_MAC_EQUAL(pMac1, pMac2, Len) \
145 (CompareMem ((pMac1), (pMac2), Len) == 0)
147 #define NET_MAC_IS_MULTICAST(Mac, BMac, Len) \
148 (((*((UINT8 *) Mac) & 0x01) == 0x01) && (!NET_MAC_EQUAL (Mac, BMac, Len)))
150 #define NTOHL(x) (UINT32)((((UINT32) (x) & 0xff) << 24) | \
151 (((UINT32) (x) & 0xff00) << 8) | \
152 (((UINT32) (x) & 0xff0000) >> 8) | \
153 (((UINT32) (x) & 0xff000000) >> 24))
155 #define HTONL(x) NTOHL(x)
157 #define NTOHS(x) (UINT16)((((UINT16) (x) & 0xff) << 8) | \
158 (((UINT16) (x) & 0xff00) >> 8))
160 #define HTONS(x) NTOHS(x)
163 // Test the IP's attribute, All the IPs are in host byte order.
165 #define IP4_IS_MULTICAST(Ip) (((Ip) & 0xF0000000) == 0xE0000000)
166 #define IP4_IS_LOCAL_BROADCAST(Ip) ((Ip) == 0xFFFFFFFF)
167 #define IP4_NET_EQUAL(Ip1, Ip2, NetMask) (((Ip1) & (NetMask)) == ((Ip2) & (NetMask)))
168 #define IP4_IS_VALID_NETMASK(Ip) (NetGetMaskLength (Ip) != IP4_MASK_NUM)
171 // Convert the EFI_IP4_ADDRESS to plain UINT32 IP4 address.
173 #define EFI_IP4(EfiIpAddr) (*(IP4_ADDR *) ((EfiIpAddr).Addr))
174 #define EFI_NTOHL(EfiIp) (NTOHL (EFI_IP4 ((EfiIp))))
175 #define EFI_IP4_EQUAL(Ip1, Ip2) (CompareMem ((Ip1), (Ip2), sizeof (EFI_IPv4_ADDRESS)) == 0)
193 extern IP4_ADDR mIp4AllMasks
[IP4_MASK_NUM
];
196 extern EFI_IPv4_ADDRESS mZeroIp4Addr
;
198 #define NET_IS_DIGIT(Ch) (('0' <= (Ch)) && ((Ch) <= '9'))
199 #define NET_ROUNDUP(size, unit) (((size) + (unit) - 1) & (~((unit) - 1)))
200 #define NET_IS_LOWER_CASE_CHAR(Ch) (('a' <= (Ch)) && ((Ch) <= 'z'))
201 #define NET_IS_UPPER_CASE_CHAR(Ch) (('A' <= (Ch)) && ((Ch) <= 'Z'))
204 // Lock primitives: the stack implements its lock primitives according
205 // to the standard EFI enviornment. It will NOT consider multiprocessor.
207 //#define NET_TPL_LOCK TPL_CALLBACK
208 //#define NET_TPL_EVENT TPL_NOTIFY
209 //#define NET_TPL_RECYCLE TPL_NOTIFY
210 //#define NET_TPL_TIMER NET_TPL_LOCK
212 //#define NET_LOCK EFI_LOCK
213 //#define NET_LOCK_INIT(x) EfiInitializeLock (x, NET_TPL_LOCK)
214 //#define NET_RECYCLE_LOCK_INIT(x) EfiInitializeLock (x, NET_TPL_RECYCLE)
215 //#define NET_TRYLOCK(x) EfiAcquireLockOrFail (x)
216 //#define NET_UNLOCK(x) EfiReleaseLock (x)
218 //#define NET_RAISE_TPL(x) (gBS->RaiseTPL (x))
219 //#define NET_RESTORE_TPL(x) (gBS->RestoreTPL (x))
221 #define TICKS_PER_MS 10000U
222 #define TICKS_PER_SECOND 10000000U
224 #define NET_RANDOM(Seed) ((UINT32) ((UINT32) (Seed) * 1103515245UL + 12345) % 4294967295UL)
245 // Double linked list entry functions, this extends the
246 // EFI list functions.
248 //typedef LIST_ENTRY LIST_ENTRY;
250 //#define NetListInit(Head) InitializeListHead(Head)
251 //#define NetListInsertHead(Head, Entry) InsertHeadList((Head), (Entry))
252 //#define NetListInsertTail(Head, Entry) InsertTailList((Head), (Entry))
253 //#define NetListIsEmpty(List) IsListEmpty(List)
255 #define NET_LIST_USER_STRUCT(Entry, Type, Field) \
256 _CR(Entry, Type, Field)
258 #define NET_LIST_USER_STRUCT_S(Entry, Type, Field, Sig) \
259 CR(Entry, Type, Field, Sig)
262 // Iterate through the doule linked list. It is NOT delete safe
264 #define NET_LIST_FOR_EACH(Entry, ListHead) \
265 for(Entry = (ListHead)->ForwardLink; Entry != (ListHead); Entry = Entry->ForwardLink)
268 // Iterate through the doule linked list. This is delete-safe.
269 // Don't touch NextEntry. Also, don't use this macro if list
270 // entries other than the Entry may be deleted when processing
271 // the current Entry.
273 #define NET_LIST_FOR_EACH_SAFE(Entry, NextEntry, ListHead) \
274 for(Entry = (ListHead)->ForwardLink, NextEntry = Entry->ForwardLink; \
275 Entry != (ListHead); \
276 Entry = NextEntry, NextEntry = Entry->ForwardLink \
280 // Make sure the list isn't empty before get the frist/last record.
282 #define NET_LIST_HEAD(ListHead, Type, Field) \
283 NET_LIST_USER_STRUCT((ListHead)->ForwardLink, Type, Field)
285 #define NET_LIST_TAIL(ListHead, Type, Field) \
286 NET_LIST_USER_STRUCT((ListHead)->BackLink, Type, Field)
288 //#define NetListRemoveEntry(Entry) RemoveEntryList (Entry)
302 IN LIST_ENTRY
*PrevEntry
,
303 IN LIST_ENTRY
*NewEntry
307 NetListInsertBefore (
308 IN LIST_ENTRY
*PostEntry
,
309 IN LIST_ENTRY
*NewEntry
314 // Object container: EFI network stack spec defines various kinds of
315 // tokens. The drivers can share code to manage those objects.
329 #define NET_MAP_INCREAMENT 64
355 IN VOID
*Value OPTIONAL
362 IN VOID
*Value OPTIONAL
374 IN NET_MAP_ITEM
*Item
,
375 OUT VOID
**Value OPTIONAL
381 OUT VOID
**Value OPTIONAL
387 OUT VOID
**Value OPTIONAL
392 (*NET_MAP_CALLBACK
) (
394 IN NET_MAP_ITEM
*Item
,
401 IN NET_MAP_CALLBACK CallBack
,
402 IN VOID
*Arg OPTIONAL
407 // Helper functions to implement driver binding and service binding protocols.
410 NetLibCreateServiceChild (
411 IN EFI_HANDLE ControllerHandle
,
412 IN EFI_HANDLE ImageHandle
,
413 IN EFI_GUID
*ServiceBindingGuid
,
414 OUT EFI_HANDLE
*ChildHandle
418 NetLibDestroyServiceChild (
419 IN EFI_HANDLE ControllerHandle
,
420 IN EFI_HANDLE ImageHandle
,
421 IN EFI_GUID
*ServiceBindingGuid
,
422 IN EFI_HANDLE ChildHandle
427 IN EFI_HANDLE SnpHandle
,
428 IN EFI_HANDLE ImageHandle
,
429 IN OUT CHAR16
**MacString
433 NetLibCreateIPv4DPathNode (
434 IN OUT IPv4_DEVICE_PATH
*Node
,
435 IN EFI_HANDLE Controller
,
438 IN IP4_ADDR RemoteIp
,
439 IN UINT16 RemotePort
,
441 IN BOOLEAN UseDefaultAddress
446 IN EFI_HANDLE Controller
,
447 IN EFI_GUID
*ProtocolGuid
453 IN EFI_DPC_PROCEDURE DpcProcedure
,
454 IN VOID
*DpcContext OPTIONAL
464 NetLibDefaultUnload (
465 IN EFI_HANDLE ImageHandle
472 NET_BUF_SIGNATURE
= EFI_SIGNATURE_32 ('n', 'b', 'u', 'f'),
473 NET_VECTOR_SIGNATURE
= EFI_SIGNATURE_32 ('n', 'v', 'e', 'c'),
474 NET_QUE_SIGNATURE
= EFI_SIGNATURE_32 ('n', 'b', 'q', 'u'),
477 NET_PROTO_DATA
= 64, // Opaque buffer for protocols
478 NET_BUF_HEAD
= 1, // Trim or allocate space from head
479 NET_BUF_TAIL
= 0, // Trim or allocate space from tail
480 NET_VECTOR_OWN_FIRST
= 0x01, // We allocated the 1st block in the vector
483 #define NET_CHECK_SIGNATURE(PData, SIGNATURE) \
484 ASSERT (((PData) != NULL) && ((PData)->Signature == (SIGNATURE)))
486 #define NET_SWAP_SHORT(Value) \
487 ((((Value) & 0xff) << 8) | (((Value) >> 8) & 0xff))
490 // Single memory block in the vector.
493 UINT32 Len
; // The block's length
494 UINT8
*Bulk
; // The block's Data
497 typedef VOID (*NET_VECTOR_EXT_FREE
) (VOID
*Arg
);
500 //NET_VECTOR contains several blocks to hold all packet's
501 //fragments and other house-keeping stuff for sharing. It
502 //doesn't specify the where actual packet fragment begins.
506 INTN RefCnt
; // Reference count to share NET_VECTOR.
507 NET_VECTOR_EXT_FREE Free
; // external function to free NET_VECTOR
508 VOID
*Arg
; // opeque argument to Free
509 UINT32 Flag
; // Flags, NET_VECTOR_OWN_FIRST
510 UINT32 Len
; // Total length of the assocated BLOCKs
517 //NET_BLOCK_OP operate on the NET_BLOCK, It specifies
518 //where the actual fragment begins and where it ends
521 UINT8
*BlockHead
; // Block's head, or the smallest valid Head
522 UINT8
*BlockTail
; // Block's tail. BlockTail-BlockHead=block length
523 UINT8
*Head
; // 1st byte of the data in the block
524 UINT8
*Tail
; // Tail of the data in the block, Tail-Head=Size
525 UINT32 Size
; // The size of the data
530 //NET_BUF is the buffer manage structure used by the
531 //network stack. Every network packet may be fragmented,
532 //and contains multiple fragments. The Vector points to
533 //memory blocks used by the each fragment, and BlockOp
534 //specifies where each fragment begins and ends.
536 //It also contains a opaque area for protocol to store
537 //per-packet informations. Protocol must be caution not
538 //to overwrite the members after that.
543 LIST_ENTRY List
; // The List this NET_BUF is on
545 IP4_HEAD
*Ip
; // Network layer header, for fast access
546 TCP_HEAD
*Tcp
; // Transport layer header, for fast access
547 UINT8 ProtoData
[NET_PROTO_DATA
]; //Protocol specific data
549 NET_VECTOR
*Vector
; // The vector containing the packet
551 UINT32 BlockOpNum
; // Total number of BlockOp in the buffer
552 UINT32 TotalSize
; // Total size of the actual packet
553 NET_BLOCK_OP BlockOp
[1]; // Specify the position of actual packet
558 //A queue of NET_BUFs, It is just a thin extension of
564 LIST_ENTRY List
; // The List this buffer queue is on
566 LIST_ENTRY BufList
; // list of queued buffers
567 UINT32 BufSize
; // total length of DATA in the buffers
568 UINT32 BufNum
; // total number of buffers on the chain
572 // Pseudo header for TCP and UDP checksum
585 // The fragment entry table used in network interfaces. This is
586 // the same as NET_BLOCK now. Use two different to distinguish
587 // the two in case that NET_BLOCK be enhanced later.
594 #define NET_GET_REF(PData) ((PData)->RefCnt++)
595 #define NET_PUT_REF(PData) ((PData)->RefCnt--)
596 #define NETBUF_FROM_PROTODATA(Info) _CR((Info), NET_BUF, ProtoData)
598 #define NET_BUF_SHARED(Buf) \
599 (((Buf)->RefCnt > 1) || ((Buf)->Vector->RefCnt > 1))
601 #define NET_VECTOR_SIZE(BlockNum) \
602 (sizeof (NET_VECTOR) + ((BlockNum) - 1) * sizeof (NET_BLOCK))
604 #define NET_BUF_SIZE(BlockOpNum) \
605 (sizeof (NET_BUF) + ((BlockOpNum) - 1) * sizeof (NET_BLOCK_OP))
607 #define NET_HEADSPACE(BlockOp) \
608 (UINTN)((BlockOp)->Head - (BlockOp)->BlockHead)
610 #define NET_TAILSPACE(BlockOp) \
611 (UINTN)((BlockOp)->BlockTail - (BlockOp)->Tail)
628 OUT UINT32
*Index OPTIONAL
639 IN NET_BUF
*Duplicate OPTIONAL
,
681 IN NET_FRAGMENT
*ExtFragment
,
685 IN NET_VECTOR_EXT_FREE ExtFree
,
686 IN VOID
*Arg OPTIONAL
692 IN NET_FRAGMENT
*ExtFragment
,
698 IN LIST_ENTRY
*BufList
,
701 IN NET_VECTOR_EXT_FREE ExtFree
,
702 IN VOID
*Arg OPTIONAL
712 IN NET_BUF_QUEUE
*NbufQue
722 IN NET_BUF_QUEUE
*NbufQue
727 IN NET_BUF_QUEUE
*NbufQue
732 IN NET_BUF_QUEUE
*NbufQue
,
738 IN NET_BUF_QUEUE
*NbufQue
,
746 IN NET_BUF_QUEUE
*NbufQue
,
752 IN NET_BUF_QUEUE
*NbufQue
773 NetPseudoHeadChecksum (