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 <Protocol/DriverBinding.h>
28 #include <Protocol/ComponentName.h>
29 #include <Protocol/DriverConfiguration.h>
30 #include <Protocol/DriverDiagnostics.h>
31 #include <Protocol/Dpc.h>
33 #define EFI_NET_LITTLE_ENDIAN
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.
78 #ifdef EFI_NET_LITTLE_ENDIAN
98 // ICMP head definition. ICMP message is categoried as either an error
99 // message or query message. Two message types have their own head format.
109 UINT32 Fourth
; // 4th filed of the head, it depends on Type.
111 } IP4_ICMP_ERROR_HEAD
;
117 } IP4_ICMP_QUERY_HEAD
;
121 // UDP header definition
132 // TCP header definition
139 #ifdef EFI_NET_LITTLE_ENDIAN
154 #define NET_MAC_EQUAL(pMac1, pMac2, Len) \
155 (NetCompareMem ((pMac1), (pMac2), Len) == 0)
157 #define NET_MAC_IS_MULTICAST(Mac, BMac, Len) \
158 (((*((UINT8 *) Mac) & 0x01) == 0x01) && (!NET_MAC_EQUAL (Mac, BMac, Len)))
160 #ifdef EFI_NET_LITTLE_ENDIAN
161 #define NTOHL(x) (UINT32)((((UINT32) (x) & 0xff) << 24) | \
162 (((UINT32) (x) & 0xff00) << 8) | \
163 (((UINT32) (x) & 0xff0000) >> 8) | \
164 (((UINT32) (x) & 0xff000000) >> 24))
166 #define HTONL(x) NTOHL(x)
168 #define NTOHS(x) (UINT16)((((UINT16) (x) & 0xff) << 8) | \
169 (((UINT16) (x) & 0xff00) >> 8))
171 #define HTONS(x) NTOHS(x)
173 #define NTOHL(x) (UINT32)(x)
174 #define HTONL(x) (UINT32)(x)
175 #define NTOHS(x) (UINT16)(x)
176 #define HTONS(x) (UINT16)(x)
180 // Test the IP's attribute, All the IPs are in host byte order.
182 #define IP4_IS_MULTICAST(Ip) (((Ip) & 0xF0000000) == 0xE0000000)
183 #define IP4_IS_LOCAL_BROADCAST(Ip) ((Ip) == 0xFFFFFFFF)
184 #define IP4_NET_EQUAL(Ip1, Ip2, NetMask) (((Ip1) & (NetMask)) == ((Ip2) & (NetMask)))
185 #define IP4_IS_VALID_NETMASK(Ip) (NetGetMaskLength (Ip) != IP4_MASK_NUM)
188 // Convert the EFI_IP4_ADDRESS to plain UINT32 IP4 address.
190 #define EFI_IP4(EfiIpAddr) (*(IP4_ADDR *) ((EfiIpAddr).Addr))
191 #define EFI_NTOHL(EfiIp) (NTOHL (EFI_IP4 ((EfiIp))))
192 #define EFI_IP4_EQUAL(Ip1, Ip2) (NetCompareMem ((Ip1), (Ip2), sizeof (EFI_IPv4_ADDRESS)) == 0)
210 extern IP4_ADDR mIp4AllMasks
[IP4_MASK_NUM
];
213 extern EFI_IPv4_ADDRESS mZeroIp4Addr
;
215 #define NET_IS_DIGIT(Ch) (('0' <= (Ch)) && ((Ch) <= '9'))
216 #define NET_ROUNDUP(size, unit) (((size) + (unit) - 1) & (~((unit) - 1)))
219 // Wrap functions to ease the impact of EFI library changes.
221 #define NetAllocateZeroPool AllocateZeroPool
222 #define NetAllocatePool AllocatePool
223 #define NetFreePool gBS->FreePool
224 #define NetCopyMem CopyMem
225 #define NetSetMem SetMem
226 #define NetZeroMem(Dest, Len) SetMem ((Dest), (Len), 0)
227 #define NetCompareMem CompareMem
230 // Lock primitives: the stack implements its lock primitives according
231 // to the standard EFI enviornment. It will NOT consider multiprocessor.
233 #define NET_TPL_LOCK TPL_CALLBACK
234 #define NET_TPL_EVENT TPL_NOTIFY
235 #define NET_TPL_RECYCLE TPL_NOTIFY
236 #define NET_TPL_TIMER NET_TPL_LOCK
238 #define NET_LOCK EFI_LOCK
239 #define NET_LOCK_INIT(x) EfiInitializeLock (x, NET_TPL_LOCK)
240 #define NET_RECYCLE_LOCK_INIT(x) EfiInitializeLock (x, NET_TPL_RECYCLE)
241 #define NET_TRYLOCK(x) EfiAcquireLockOrFail (x)
242 #define NET_UNLOCK(x) EfiReleaseLock (x)
244 #define NET_RAISE_TPL(x) (gBS->RaiseTPL (x))
245 #define NET_RESTORE_TPL(x) (gBS->RestoreTPL (x))
247 #define TICKS_PER_MS 10000U
248 #define TICKS_PER_SECOND 10000000U
250 #define NET_RANDOM(Seed) ((UINT32) ((UINT32) (Seed) * 1103515245UL + 12345) % 4294967295UL)
271 // Double linked list entry functions, this extends the
272 // EFI list functions.
274 typedef LIST_ENTRY NET_LIST_ENTRY
;
276 #define NetListInit(Head) InitializeListHead(Head)
277 #define NetListInsertHead(Head, Entry) InsertHeadList((Head), (Entry))
278 #define NetListInsertTail(Head, Entry) InsertTailList((Head), (Entry))
279 #define NetListIsEmpty(List) IsListEmpty(List)
281 #define NET_LIST_USER_STRUCT(Entry, Type, Field) \
282 _CR(Entry, Type, Field)
284 #define NET_LIST_USER_STRUCT_S(Entry, Type, Field, Sig) \
285 CR(Entry, Type, Field, Sig)
288 // Iterate through the doule linked list. It is NOT delete safe
290 #define NET_LIST_FOR_EACH(Entry, ListHead) \
291 for(Entry = (ListHead)->ForwardLink; Entry != (ListHead); Entry = Entry->ForwardLink)
294 // Iterate through the doule linked list. This is delete-safe.
295 // Don't touch NextEntry. Also, don't use this macro if list
296 // entries other than the Entry may be deleted when processing
297 // the current Entry.
299 #define NET_LIST_FOR_EACH_SAFE(Entry, NextEntry, ListHead) \
300 for(Entry = (ListHead)->ForwardLink, NextEntry = Entry->ForwardLink; \
301 Entry != (ListHead); \
302 Entry = NextEntry, NextEntry = Entry->ForwardLink \
306 // Make sure the list isn't empty before get the frist/last record.
308 #define NET_LIST_HEAD(ListHead, Type, Field) \
309 NET_LIST_USER_STRUCT((ListHead)->ForwardLink, Type, Field)
311 #define NET_LIST_TAIL(ListHead, Type, Field) \
312 NET_LIST_USER_STRUCT((ListHead)->BackLink, Type, Field)
314 #define NetListRemoveEntry(Entry) RemoveEntryList (Entry)
328 IN NET_LIST_ENTRY
*PrevEntry
,
329 IN NET_LIST_ENTRY
*NewEntry
333 NetListInsertBefore (
334 IN NET_LIST_ENTRY
*PostEntry
,
335 IN NET_LIST_ENTRY
*NewEntry
340 // Object container: EFI network stack spec defines various kinds of
341 // tokens. The drivers can share code to manage those objects.
351 NET_LIST_ENTRY Recycled
;
355 #define NET_MAP_INCREAMENT 64
381 IN VOID
*Value OPTIONAL
388 IN VOID
*Value OPTIONAL
400 IN NET_MAP_ITEM
*Item
,
401 OUT VOID
**Value OPTIONAL
407 OUT VOID
**Value OPTIONAL
413 OUT VOID
**Value OPTIONAL
418 (*NET_MAP_CALLBACK
) (
420 IN NET_MAP_ITEM
*Item
,
427 IN NET_MAP_CALLBACK CallBack
,
428 IN VOID
*Arg OPTIONAL
433 // Helper functions to implement driver binding and service binding protocols.
436 NetLibCreateServiceChild (
437 IN EFI_HANDLE ControllerHandle
,
438 IN EFI_HANDLE ImageHandle
,
439 IN EFI_GUID
*ServiceBindingGuid
,
440 OUT EFI_HANDLE
*ChildHandle
444 NetLibDestroyServiceChild (
445 IN EFI_HANDLE ControllerHandle
,
446 IN EFI_HANDLE ImageHandle
,
447 IN EFI_GUID
*ServiceBindingGuid
,
448 IN EFI_HANDLE ChildHandle
453 IN EFI_HANDLE SnpHandle
,
454 IN EFI_HANDLE ImageHandle
,
455 IN OUT CHAR16
**MacString
459 NetLibCreateIPv4DPathNode (
460 IN OUT IPv4_DEVICE_PATH
*Node
,
461 IN EFI_HANDLE Controller
,
464 IN IP4_ADDR RemoteIp
,
465 IN UINT16 RemotePort
,
467 IN BOOLEAN UseDefaultAddress
472 IN EFI_HANDLE Controller
,
473 IN EFI_GUID
*ProtocolGuid
479 IN EFI_DPC_PROCEDURE DpcProcedure
,
480 IN VOID
*DpcContext OPTIONAL
490 NetLibDefaultUnload (
491 IN EFI_HANDLE ImageHandle
498 NET_BUF_SIGNATURE
= EFI_SIGNATURE_32 ('n', 'b', 'u', 'f'),
499 NET_VECTOR_SIGNATURE
= EFI_SIGNATURE_32 ('n', 'v', 'e', 'c'),
500 NET_QUE_SIGNATURE
= EFI_SIGNATURE_32 ('n', 'b', 'q', 'u'),
503 NET_PROTO_DATA
= 64, // Opaque buffer for protocols
504 NET_BUF_HEAD
= 1, // Trim or allocate space from head
505 NET_BUF_TAIL
= 0, // Trim or allocate space from tail
506 NET_VECTOR_OWN_FIRST
= 0x01, // We allocated the 1st block in the vector
509 #define NET_CHECK_SIGNATURE(PData, SIGNATURE) \
510 ASSERT (((PData) != NULL) && ((PData)->Signature == (SIGNATURE)))
512 #define NET_SWAP_SHORT(Value) \
513 ((((Value) & 0xff) << 8) | (((Value) >> 8) & 0xff))
516 // Single memory block in the vector.
519 UINT32 Len
; // The block's length
520 UINT8
*Bulk
; // The block's Data
523 typedef VOID (*NET_VECTOR_EXT_FREE
) (VOID
*Arg
);
526 //NET_VECTOR contains several blocks to hold all packet's
527 //fragments and other house-keeping stuff for sharing. It
528 //doesn't specify the where actual packet fragment begins.
532 INTN RefCnt
; // Reference count to share NET_VECTOR.
533 NET_VECTOR_EXT_FREE Free
; // external function to free NET_VECTOR
534 VOID
*Arg
; // opeque argument to Free
535 UINT32 Flag
; // Flags, NET_VECTOR_OWN_FIRST
536 UINT32 Len
; // Total length of the assocated BLOCKs
543 //NET_BLOCK_OP operate on the NET_BLOCK, It specifies
544 //where the actual fragment begins and where it ends
547 UINT8
*BlockHead
; // Block's head, or the smallest valid Head
548 UINT8
*BlockTail
; // Block's tail. BlockTail-BlockHead=block length
549 UINT8
*Head
; // 1st byte of the data in the block
550 UINT8
*Tail
; // Tail of the data in the block, Tail-Head=Size
551 UINT32 Size
; // The size of the data
556 //NET_BUF is the buffer manage structure used by the
557 //network stack. Every network packet may be fragmented,
558 //and contains multiple fragments. The Vector points to
559 //memory blocks used by the each fragment, and BlockOp
560 //specifies where each fragment begins and ends.
562 //It also contains a opaque area for protocol to store
563 //per-packet informations. Protocol must be caution not
564 //to overwrite the members after that.
569 NET_LIST_ENTRY List
; // The List this NET_BUF is on
571 IP4_HEAD
*Ip
; // Network layer header, for fast access
572 TCP_HEAD
*Tcp
; // Transport layer header, for fast access
573 UINT8 ProtoData
[NET_PROTO_DATA
]; //Protocol specific data
575 NET_VECTOR
*Vector
; // The vector containing the packet
577 UINT32 BlockOpNum
; // Total number of BlockOp in the buffer
578 UINT32 TotalSize
; // Total size of the actual packet
579 NET_BLOCK_OP BlockOp
[1]; // Specify the position of actual packet
584 //A queue of NET_BUFs, It is just a thin extension of
590 NET_LIST_ENTRY List
; // The List this buffer queue is on
592 NET_LIST_ENTRY BufList
; // list of queued buffers
593 UINT32 BufSize
; // total length of DATA in the buffers
594 UINT32 BufNum
; // total number of buffers on the chain
598 // Pseudo header for TCP and UDP checksum
611 // The fragment entry table used in network interfaces. This is
612 // the same as NET_BLOCK now. Use two different to distinguish
613 // the two in case that NET_BLOCK be enhanced later.
620 #define NET_GET_REF(PData) ((PData)->RefCnt++)
621 #define NET_PUT_REF(PData) ((PData)->RefCnt--)
622 #define NETBUF_FROM_PROTODATA(Info) _CR((Info), NET_BUF, ProtoData)
624 #define NET_BUF_SHARED(Buf) \
625 (((Buf)->RefCnt > 1) || ((Buf)->Vector->RefCnt > 1))
627 #define NET_VECTOR_SIZE(BlockNum) \
628 (sizeof (NET_VECTOR) + ((BlockNum) - 1) * sizeof (NET_BLOCK))
630 #define NET_BUF_SIZE(BlockOpNum) \
631 (sizeof (NET_BUF) + ((BlockOpNum) - 1) * sizeof (NET_BLOCK_OP))
633 #define NET_HEADSPACE(BlockOp) \
634 (UINTN)((BlockOp)->Head - (BlockOp)->BlockHead)
636 #define NET_TAILSPACE(BlockOp) \
637 (UINTN)((BlockOp)->BlockTail - (BlockOp)->Tail)
654 OUT UINT32
*Index OPTIONAL
665 IN NET_BUF
*Duplicate OPTIONAL
,
707 IN NET_FRAGMENT
*ExtFragment
,
711 IN NET_VECTOR_EXT_FREE ExtFree
,
712 IN VOID
*Arg OPTIONAL
718 IN NET_FRAGMENT
*ExtFragment
,
724 IN NET_LIST_ENTRY
*BufList
,
727 IN NET_VECTOR_EXT_FREE ExtFree
,
728 IN VOID
*Arg OPTIONAL
733 IN NET_LIST_ENTRY
*Head
738 IN NET_BUF_QUEUE
*NbufQue
748 IN NET_BUF_QUEUE
*NbufQue
753 IN NET_BUF_QUEUE
*NbufQue
758 IN NET_BUF_QUEUE
*NbufQue
,
764 IN NET_BUF_QUEUE
*NbufQue
,
772 IN NET_BUF_QUEUE
*NbufQue
,
778 IN NET_BUF_QUEUE
*NbufQue
799 NetPseudoHeadChecksum (
807 // The debug level definition. This value is also used as the
808 // syslog's servity level. Don't change it.
811 NETDEBUG_LEVEL_TRACE
= 5,
812 NETDEBUG_LEVEL_WARNING
= 4,
813 NETDEBUG_LEVEL_ERROR
= 3,
816 #ifdef EFI_NETWORK_STACK_DEBUG
819 // The debug output expects the ASCII format string, Use %a to print ASCII
820 // string, and %s to print UNICODE string. PrintArg must be enclosed in ().
821 // For example: NET_DEBUG_TRACE ("Tcp", ("State transit to %a\n", Name));
823 #define NET_DEBUG_TRACE(Module, PrintArg) \
825 NETDEBUG_LEVEL_TRACE, \
829 NetDebugASPrint PrintArg \
832 #define NET_DEBUG_WARNING(Module, PrintArg) \
834 NETDEBUG_LEVEL_WARNING, \
838 NetDebugASPrint PrintArg \
841 #define NET_DEBUG_ERROR(Module, PrintArg) \
843 NETDEBUG_LEVEL_ERROR, \
847 NetDebugASPrint PrintArg \
851 #define NET_DEBUG_TRACE(Module, PrintString)
852 #define NET_DEBUG_WARNING(Module, PrintString)
853 #define NET_DEBUG_ERROR(Module, PrintString)
872 // Network debug message is sent out as syslog.
875 NET_SYSLOG_FACILITY
= 16, // Syslog local facility local use
876 NET_SYSLOG_PACKET_LEN
= 512,
877 NET_DEBUG_MSG_LEN
= 470, // 512 - (ether+ip+udp head length)
878 NET_SYSLOG_TX_TIMEOUT
= 500 *1000 *10, // 500ms