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