]>
Commit | Line | Data |
---|---|---|
cbf316f2 | 1 | /** @file |
2 | ||
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 | |
8 | ||
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. | |
11 | ||
12 | Module Name: | |
13 | ||
14 | NetLib.h | |
15 | ||
16 | Abstract: | |
17 | ||
18 | Library for the UEFI network stack. | |
19 | ||
20 | ||
21 | **/ | |
22 | ||
23 | #ifndef _NET_LIB_H_ | |
24 | #define _NET_LIB_H_ | |
25 | ||
26 | #include <PiDxe.h> | |
27 | #include <Protocol/DriverBinding.h> | |
28 | #include <Protocol/ComponentName.h> | |
29 | #include <Protocol/DriverConfiguration.h> | |
30 | #include <Protocol/DriverDiagnostics.h> | |
31 | ||
32 | #define EFI_NET_LITTLE_ENDIAN | |
33 | ||
34 | typedef UINT32 IP4_ADDR; | |
35 | typedef UINT32 TCP_SEQNO; | |
36 | typedef UINT16 TCP_PORTNO; | |
37 | ||
38 | enum { | |
39 | NET_ETHER_ADDR_LEN = 6, | |
40 | NET_IFTYPE_ETHERNET = 0x01, | |
41 | ||
42 | EFI_IP_PROTO_UDP = 0x11, | |
43 | EFI_IP_PROTO_TCP = 0x06, | |
44 | EFI_IP_PROTO_ICMP = 0x01, | |
45 | ||
46 | // | |
47 | // The address classfication | |
48 | // | |
49 | IP4_ADDR_CLASSA = 1, | |
50 | IP4_ADDR_CLASSB, | |
51 | IP4_ADDR_CLASSC, | |
52 | IP4_ADDR_CLASSD, | |
53 | IP4_ADDR_CLASSE, | |
54 | ||
55 | IP4_MASK_NUM = 33, | |
56 | }; | |
57 | ||
58 | #pragma pack(1) | |
59 | ||
60 | // | |
61 | // Ethernet head definition | |
62 | // | |
63 | typedef struct { | |
64 | UINT8 DstMac [NET_ETHER_ADDR_LEN]; | |
65 | UINT8 SrcMac [NET_ETHER_ADDR_LEN]; | |
66 | UINT16 EtherType; | |
67 | } ETHER_HEAD; | |
68 | ||
69 | ||
70 | // | |
71 | // The EFI_IP4_HEADER is hard to use because the source and | |
72 | // destination address are defined as EFI_IPv4_ADDRESS, which | |
73 | // is a structure. Two structures can't be compared or masked | |
74 | // directly. This is why there is an internal representation. | |
75 | // | |
76 | typedef struct { | |
77 | #ifdef EFI_NET_LITTLE_ENDIAN | |
78 | UINT8 HeadLen : 4; | |
79 | UINT8 Ver : 4; | |
80 | #else | |
81 | UINT8 Ver : 4; | |
82 | UINT8 HeadLen : 4; | |
83 | #endif | |
84 | UINT8 Tos; | |
85 | UINT16 TotalLen; | |
86 | UINT16 Id; | |
87 | UINT16 Fragment; | |
88 | UINT8 Ttl; | |
89 | UINT8 Protocol; | |
90 | UINT16 Checksum; | |
91 | IP4_ADDR Src; | |
92 | IP4_ADDR Dst; | |
93 | } IP4_HEAD; | |
94 | ||
95 | ||
96 | // | |
97 | // ICMP head definition. ICMP message is categoried as either an error | |
98 | // message or query message. Two message types have their own head format. | |
99 | // | |
100 | typedef struct { | |
101 | UINT8 Type; | |
102 | UINT8 Code; | |
103 | UINT16 Checksum; | |
104 | } IP4_ICMP_HEAD; | |
105 | ||
106 | typedef struct { | |
107 | IP4_ICMP_HEAD Head; | |
108 | UINT32 Fourth; // 4th filed of the head, it depends on Type. | |
109 | IP4_HEAD IpHead; | |
110 | } IP4_ICMP_ERROR_HEAD; | |
111 | ||
112 | typedef struct { | |
113 | IP4_ICMP_HEAD Head; | |
114 | UINT16 Id; | |
115 | UINT16 Seq; | |
116 | } IP4_ICMP_QUERY_HEAD; | |
117 | ||
118 | ||
119 | // | |
120 | // UDP header definition | |
121 | // | |
122 | typedef struct { | |
123 | UINT16 SrcPort; | |
124 | UINT16 DstPort; | |
125 | UINT16 Length; | |
126 | UINT16 Checksum; | |
127 | } EFI_UDP4_HEADER; | |
128 | ||
129 | ||
130 | // | |
131 | // TCP header definition | |
132 | // | |
133 | typedef struct { | |
134 | TCP_PORTNO SrcPort; | |
135 | TCP_PORTNO DstPort; | |
136 | TCP_SEQNO Seq; | |
137 | TCP_SEQNO Ack; | |
138 | #ifdef EFI_NET_LITTLE_ENDIAN | |
139 | UINT8 Res : 4; | |
140 | UINT8 HeadLen : 4; | |
141 | #else | |
142 | UINT8 HeadLen : 4; | |
143 | UINT8 Res : 4; | |
144 | #endif | |
145 | UINT8 Flag; | |
146 | UINT16 Wnd; | |
147 | UINT16 Checksum; | |
148 | UINT16 Urg; | |
149 | } TCP_HEAD; | |
150 | ||
151 | #pragma pack() | |
152 | ||
153 | #define NET_MAC_EQUAL(pMac1, pMac2, Len) \ | |
154 | (NetCompareMem ((pMac1), (pMac2), Len) == 0) | |
155 | ||
156 | #define NET_MAC_IS_MULTICAST(Mac, BMac, Len) \ | |
157 | (((*((UINT8 *) Mac) & 0x01) == 0x01) && (!NET_MAC_EQUAL (Mac, BMac, Len))) | |
158 | ||
159 | #ifdef EFI_NET_LITTLE_ENDIAN | |
160 | #define NTOHL(x) (UINT32)((((UINT32) (x) & 0xff) << 24) | \ | |
161 | (((UINT32) (x) & 0xff00) << 8) | \ | |
162 | (((UINT32) (x) & 0xff0000) >> 8) | \ | |
163 | (((UINT32) (x) & 0xff000000) >> 24)) | |
164 | ||
165 | #define HTONL(x) NTOHL(x) | |
166 | ||
167 | #define NTOHS(x) (UINT16)((((UINT16) (x) & 0xff) << 8) | \ | |
168 | (((UINT16) (x) & 0xff00) >> 8)) | |
169 | ||
170 | #define HTONS(x) NTOHS(x) | |
171 | #else | |
172 | #define NTOHL(x) (UINT32)(x) | |
173 | #define HTONL(x) (UINT32)(x) | |
174 | #define NTOHS(x) (UINT16)(x) | |
175 | #define HTONS(x) (UINT16)(x) | |
176 | #endif | |
177 | ||
178 | // | |
179 | // Test the IP's attribute, All the IPs are in host byte order. | |
180 | // | |
181 | #define IP4_IS_MULTICAST(Ip) (((Ip) & 0xF0000000) == 0xE0000000) | |
182 | #define IP4_IS_LOCAL_BROADCAST(Ip) ((Ip) == 0xFFFFFFFF) | |
183 | #define IP4_NET_EQUAL(Ip1, Ip2, NetMask) (((Ip1) & (NetMask)) == ((Ip2) & (NetMask))) | |
184 | #define IP4_IS_VALID_NETMASK(Ip) (NetGetMaskLength (Ip) != IP4_MASK_NUM) | |
185 | ||
186 | // | |
187 | // Convert the EFI_IP4_ADDRESS to plain UINT32 IP4 address. | |
188 | // | |
189 | #define EFI_IP4(EfiIpAddr) (*(IP4_ADDR *) ((EfiIpAddr).Addr)) | |
190 | #define EFI_NTOHL(EfiIp) (NTOHL (EFI_IP4 ((EfiIp)))) | |
191 | #define EFI_IP_EQUAL(Ip1, Ip2) (EFI_IP4 (Ip1) == EFI_IP4 (Ip2)) | |
192 | ||
193 | INTN | |
194 | NetGetMaskLength ( | |
195 | IN IP4_ADDR Mask | |
196 | ); | |
197 | ||
198 | INTN | |
199 | NetGetIpClass ( | |
200 | IN IP4_ADDR Addr | |
201 | ); | |
202 | ||
203 | BOOLEAN | |
204 | Ip4IsUnicast ( | |
205 | IN IP4_ADDR Ip, | |
206 | IN IP4_ADDR NetMask | |
207 | ); | |
208 | ||
209 | extern IP4_ADDR mIp4AllMasks [IP4_MASK_NUM]; | |
210 | ||
211 | //@MT:#include EFI_PROTOCOL_CONSUMER (LoadedImage) | |
212 | //@MT:#include EFI_PROTOCOL_CONSUMER (ServiceBinding) | |
213 | //@MT:#include EFI_PROTOCOL_CONSUMER (SimpleNetwork) | |
214 | ||
215 | // | |
216 | // Wrap functions to ease the impact of EFI library changes. | |
217 | // | |
218 | #define NetAllocateZeroPool AllocateZeroPool | |
219 | #define NetAllocatePool AllocatePool | |
220 | #define NetFreePool gBS->FreePool | |
221 | #define NetCopyMem CopyMem | |
222 | #define NetSetMem SetMem | |
223 | #define NetZeroMem(Dest, Len) SetMem ((Dest), (Len), 0) | |
224 | #define NetCompareMem CompareMem | |
225 | ||
226 | // | |
227 | // Lock primitives: the stack implements its lock primitives according | |
228 | // to the standard EFI enviornment. It will NOT consider multiprocessor. | |
229 | // | |
230 | #define NET_TPL_LOCK TPL_CALLBACK | |
231 | #define NET_TPL_RECYCLE_LOCK (NET_TPL_LOCK + 1) | |
232 | #define NET_TPL_EVENT TPL_CALLBACK | |
233 | #define NET_TPL_RECYCLE (NET_TPL_LOCK + 1) | |
234 | #define NET_TPL_SLOW_TIMER (TPL_CALLBACK - 1) | |
235 | #define NET_TPL_FAST_TIMER NET_TPL_RECYCLE | |
236 | #define NET_TPL_TIMER TPL_CALLBACK | |
237 | ||
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_LOCK) | |
241 | #define NET_TRYLOCK(x) EfiAcquireLockOrFail (x) | |
242 | #define NET_UNLOCK(x) EfiReleaseLock (x) | |
243 | ||
244 | #define NET_RAISE_TPL(x) (gBS->RaiseTPL (x)) | |
245 | #define NET_RESTORE_TPL(x) (gBS->RestoreTPL (x)) | |
246 | ||
247 | #define TICKS_PER_MS 10000U | |
248 | #define TICKS_PER_SECOND 10000000U | |
249 | ||
250 | #define NET_MIN(a, b) ((a) < (b) ? (a) : (b)) | |
251 | #define NET_MAX(a, b) ((a) > (b) ? (a) : (b)) | |
252 | #define NET_RANDOM(Seed) (((Seed) * 1103515245L + 12345) % 4294967295L) | |
253 | ||
254 | ||
255 | UINT32 | |
256 | NetGetUint32 ( | |
257 | IN UINT8 *Buf | |
258 | ); | |
259 | ||
260 | VOID | |
261 | NetPutUint32 ( | |
262 | IN UINT8 *Buf, | |
263 | IN UINT32 Data | |
264 | ); | |
265 | ||
266 | UINT32 | |
267 | NetRandomInitSeed ( | |
268 | VOID | |
269 | ); | |
270 | ||
271 | ||
272 | // | |
273 | // Double linked list entry functions, this extends the | |
274 | // EFI list functions. | |
275 | // | |
276 | typedef LIST_ENTRY NET_LIST_ENTRY; | |
277 | ||
278 | #define NetListInit(Head) InitializeListHead(Head) | |
279 | #define NetListInsertHead(Head, Entry) InsertHeadList((Head), (Entry)) | |
280 | #define NetListInsertTail(Head, Entry) InsertTailList((Head), (Entry)) | |
281 | #define NetListIsEmpty(List) IsListEmpty(List) | |
282 | ||
283 | #define NET_LIST_USER_STRUCT(Entry, Type, Field) \ | |
284 | _CR(Entry, Type, Field) | |
285 | ||
286 | #define NET_LIST_USER_STRUCT_S(Entry, Type, Field, Sig) \ | |
287 | CR(Entry, Type, Field, Sig) | |
288 | ||
289 | // | |
290 | // Iterate through the doule linked list. It is NOT delete safe | |
291 | // | |
292 | #define NET_LIST_FOR_EACH(Entry, ListHead) \ | |
293 | for(Entry = (ListHead)->ForwardLink; Entry != (ListHead); Entry = Entry->ForwardLink) | |
294 | ||
295 | // | |
296 | // Iterate through the doule linked list. This is delete-safe. | |
297 | // Don't touch NextEntry. Also, don't use this macro if list | |
298 | // entries other than the Entry may be deleted when processing | |
299 | // the current Entry. | |
300 | // | |
301 | #define NET_LIST_FOR_EACH_SAFE(Entry, NextEntry, ListHead) \ | |
302 | for(Entry = (ListHead)->ForwardLink, NextEntry = Entry->ForwardLink; \ | |
303 | Entry != (ListHead); \ | |
304 | Entry = NextEntry, NextEntry = Entry->ForwardLink \ | |
305 | ) | |
306 | ||
307 | // | |
308 | // Make sure the list isn't empty before get the frist/last record. | |
309 | // | |
310 | #define NET_LIST_HEAD(ListHead, Type, Field) \ | |
311 | NET_LIST_USER_STRUCT((ListHead)->ForwardLink, Type, Field) | |
312 | ||
313 | #define NET_LIST_TAIL(ListHead, Type, Field) \ | |
314 | NET_LIST_USER_STRUCT((ListHead)->BackLink, Type, Field) | |
315 | ||
316 | #define NetListRemoveEntry(Entry) RemoveEntryList (Entry) | |
317 | ||
318 | NET_LIST_ENTRY* | |
319 | NetListRemoveHead ( | |
320 | NET_LIST_ENTRY *Head | |
321 | ); | |
322 | ||
323 | NET_LIST_ENTRY* | |
324 | NetListRemoveTail ( | |
325 | NET_LIST_ENTRY *Head | |
326 | ); | |
327 | ||
328 | VOID | |
329 | NetListInsertAfter ( | |
330 | IN NET_LIST_ENTRY *PrevEntry, | |
331 | IN NET_LIST_ENTRY *NewEntry | |
332 | ); | |
333 | ||
334 | VOID | |
335 | NetListInsertBefore ( | |
336 | IN NET_LIST_ENTRY *PostEntry, | |
337 | IN NET_LIST_ENTRY *NewEntry | |
338 | ); | |
339 | ||
340 | ||
341 | // | |
342 | // Object container: EFI network stack spec defines various kinds of | |
343 | // tokens. The drivers can share code to manage those objects. | |
344 | // | |
345 | typedef struct { | |
346 | NET_LIST_ENTRY Link; | |
347 | VOID *Key; | |
348 | VOID *Value; | |
349 | } NET_MAP_ITEM; | |
350 | ||
351 | typedef struct { | |
352 | NET_LIST_ENTRY Used; | |
353 | NET_LIST_ENTRY Recycled; | |
354 | UINTN Count; | |
355 | } NET_MAP; | |
356 | ||
357 | #define NET_MAP_INCREAMENT 64 | |
358 | ||
359 | VOID | |
360 | NetMapInit ( | |
361 | IN NET_MAP *Map | |
362 | ); | |
363 | ||
364 | VOID | |
365 | NetMapClean ( | |
366 | IN NET_MAP *Map | |
367 | ); | |
368 | ||
369 | BOOLEAN | |
370 | NetMapIsEmpty ( | |
371 | IN NET_MAP *Map | |
372 | ); | |
373 | ||
374 | UINTN | |
375 | NetMapGetCount ( | |
376 | IN NET_MAP *Map | |
377 | ); | |
378 | ||
379 | EFI_STATUS | |
380 | NetMapInsertHead ( | |
381 | IN NET_MAP *Map, | |
382 | IN VOID *Key, | |
383 | IN VOID *Value OPTIONAL | |
384 | ); | |
385 | ||
386 | EFI_STATUS | |
387 | NetMapInsertTail ( | |
388 | IN NET_MAP *Map, | |
389 | IN VOID *Key, | |
390 | IN VOID *Value OPTIONAL | |
391 | ); | |
392 | ||
393 | NET_MAP_ITEM * | |
394 | NetMapFindKey ( | |
395 | IN NET_MAP *Map, | |
396 | IN VOID *Key | |
397 | ); | |
398 | ||
399 | VOID * | |
400 | NetMapRemoveItem ( | |
401 | IN NET_MAP *Map, | |
402 | IN NET_MAP_ITEM *Item, | |
403 | OUT VOID **Value OPTIONAL | |
404 | ); | |
405 | ||
406 | VOID * | |
407 | NetMapRemoveHead ( | |
408 | IN NET_MAP *Map, | |
409 | OUT VOID **Value OPTIONAL | |
410 | ); | |
411 | ||
412 | VOID * | |
413 | NetMapRemoveTail ( | |
414 | IN NET_MAP *Map, | |
415 | OUT VOID **Value OPTIONAL | |
416 | ); | |
417 | ||
418 | typedef | |
419 | EFI_STATUS | |
420 | (*NET_MAP_CALLBACK) ( | |
421 | IN NET_MAP *Map, | |
422 | IN NET_MAP_ITEM *Item, | |
423 | IN VOID *Arg | |
424 | ); | |
425 | ||
426 | EFI_STATUS | |
427 | NetMapIterate ( | |
428 | IN NET_MAP *Map, | |
429 | IN NET_MAP_CALLBACK CallBack, | |
430 | IN VOID *Arg OPTIONAL | |
431 | ); | |
432 | ||
433 | ||
434 | // | |
435 | // Helper functions to implement driver binding and service binding protocols. | |
436 | // | |
437 | EFI_STATUS | |
438 | NetLibCreateServiceChild ( | |
439 | IN EFI_HANDLE ControllerHandle, | |
440 | IN EFI_HANDLE ImageHandle, | |
441 | IN EFI_GUID *ServiceBindingGuid, | |
442 | OUT EFI_HANDLE *ChildHandle | |
443 | ); | |
444 | ||
445 | EFI_STATUS | |
446 | NetLibDestroyServiceChild ( | |
447 | IN EFI_HANDLE ControllerHandle, | |
448 | IN EFI_HANDLE ImageHandle, | |
449 | IN EFI_GUID *ServiceBindingGuid, | |
450 | IN EFI_HANDLE ChildHandle | |
451 | ); | |
452 | ||
453 | EFI_STATUS | |
454 | NetLibGetMacString ( | |
455 | IN EFI_HANDLE SnpHandle, | |
456 | IN EFI_HANDLE ImageHandle, | |
457 | IN OUT CONST CHAR16 **MacString | |
458 | ); | |
459 | ||
460 | EFI_HANDLE | |
461 | NetLibGetNicHandle ( | |
462 | IN EFI_HANDLE Controller, | |
463 | IN EFI_GUID *ProtocolGuid | |
464 | ); | |
465 | ||
466 | typedef | |
467 | EFI_STATUS | |
468 | (EFIAPI *NET_LIB_DRIVER_UNLOAD) ( | |
469 | IN EFI_HANDLE ImageHandle | |
470 | ); | |
471 | ||
472 | EFI_STATUS | |
473 | EFIAPI | |
474 | NetLibDefaultUnload ( | |
475 | IN EFI_HANDLE ImageHandle | |
476 | ); | |
477 | ||
478 | EFI_STATUS | |
479 | NetLibInstallAllDriverProtocolsWithUnload ( | |
480 | IN EFI_HANDLE ImageHandle, | |
481 | IN EFI_SYSTEM_TABLE *SystemTable, | |
482 | IN EFI_DRIVER_BINDING_PROTOCOL *DriverBinding, | |
483 | IN EFI_HANDLE DriverBindingHandle, | |
484 | IN EFI_COMPONENT_NAME_PROTOCOL *ComponentName, OPTIONAL | |
485 | IN EFI_DRIVER_CONFIGURATION_PROTOCOL *DriverConfiguration, OPTIONAL | |
486 | IN EFI_DRIVER_DIAGNOSTICS_PROTOCOL *DriverDiagnostics, OPTIONAL | |
487 | IN NET_LIB_DRIVER_UNLOAD CustomizedUnload | |
488 | ); | |
489 | ||
490 | EFI_STATUS | |
491 | NetLibInstallAllDriverProtocols ( | |
492 | IN EFI_HANDLE ImageHandle, | |
493 | IN EFI_SYSTEM_TABLE *SystemTable, | |
494 | IN EFI_DRIVER_BINDING_PROTOCOL *DriverBinding, | |
495 | IN EFI_HANDLE DriverBindingHandle, | |
496 | IN EFI_COMPONENT_NAME_PROTOCOL *ComponentName, OPTIONAL | |
497 | IN EFI_DRIVER_CONFIGURATION_PROTOCOL *DriverConfiguration, OPTIONAL | |
498 | IN EFI_DRIVER_DIAGNOSTICS_PROTOCOL *DriverDiagnostics OPTIONAL | |
499 | ); | |
500 | \r | |
501 | enum { | |
502 | // | |
503 | //Various signatures | |
504 | // | |
505 | NET_BUF_SIGNATURE = EFI_SIGNATURE_32 ('n', 'b', 'u', 'f'), | |
506 | NET_VECTOR_SIGNATURE = EFI_SIGNATURE_32 ('n', 'v', 'e', 'c'), | |
507 | NET_QUE_SIGNATURE = EFI_SIGNATURE_32 ('n', 'b', 'q', 'u'), | |
508 | ||
509 | ||
510 | NET_PROTO_DATA = 64, // Opaque buffer for protocols | |
511 | NET_BUF_HEAD = 1, // Trim or allocate space from head | |
512 | NET_BUF_TAIL = 0, // Trim or allocate space from tail | |
513 | NET_VECTOR_OWN_FIRST = 0x01, // We allocated the 1st block in the vector | |
514 | }; | |
515 | ||
516 | #define NET_CHECK_SIGNATURE(PData, SIGNATURE) \ | |
517 | ASSERT (((PData) != NULL) && ((PData)->Signature == (SIGNATURE))) | |
518 | ||
519 | #define NET_SWAP_SHORT(Value) \ | |
520 | ((((Value) & 0xff) << 8) | (((Value) >> 8) & 0xff)) | |
521 | ||
522 | // | |
523 | // Single memory block in the vector. | |
524 | // | |
525 | typedef struct { | |
526 | UINT32 Len; // The block's length | |
527 | UINT8 *Bulk; // The block's Data | |
528 | } NET_BLOCK; | |
529 | ||
530 | typedef VOID (*NET_VECTOR_EXT_FREE) (VOID *Arg); | |
531 | ||
532 | // | |
533 | //NET_VECTOR contains several blocks to hold all packet's | |
534 | //fragments and other house-keeping stuff for sharing. It | |
535 | //doesn't specify the where actual packet fragment begins. | |
536 | // | |
537 | typedef struct { | |
538 | UINT32 Signature; | |
539 | INTN RefCnt; // Reference count to share NET_VECTOR. | |
540 | NET_VECTOR_EXT_FREE Free; // external function to free NET_VECTOR | |
541 | VOID *Arg; // opeque argument to Free | |
542 | UINT32 Flag; // Flags, NET_VECTOR_OWN_FIRST | |
543 | UINT32 Len; // Total length of the assocated BLOCKs | |
544 | ||
545 | UINT32 BlockNum; | |
546 | NET_BLOCK Block[1]; | |
547 | } NET_VECTOR; | |
548 | ||
549 | // | |
550 | //NET_BLOCK_OP operate on the NET_BLOCK, It specifies | |
551 | //where the actual fragment begins and where it ends | |
552 | // | |
553 | typedef struct { | |
554 | UINT8 *BlockHead; // Block's head, or the smallest valid Head | |
555 | UINT8 *BlockTail; // Block's tail. BlockTail-BlockHead=block length | |
556 | UINT8 *Head; // 1st byte of the data in the block | |
557 | UINT8 *Tail; // Tail of the data in the block, Tail-Head=Size | |
558 | UINT32 Size; // The size of the data | |
559 | } NET_BLOCK_OP; | |
560 | ||
561 | ||
562 | // | |
563 | //NET_BUF is the buffer manage structure used by the | |
564 | //network stack. Every network packet may be fragmented, | |
565 | //and contains multiple fragments. The Vector points to | |
566 | //memory blocks used by the each fragment, and BlockOp | |
567 | //specifies where each fragment begins and ends. | |
568 | // | |
569 | //It also contains a opaque area for protocol to store | |
570 | //per-packet informations. Protocol must be caution not | |
571 | //to overwrite the members after that. | |
572 | // | |
573 | typedef struct { | |
574 | UINT32 Signature; | |
575 | INTN RefCnt; | |
576 | NET_LIST_ENTRY List; // The List this NET_BUF is on | |
577 | ||
578 | IP4_HEAD *Ip; // Network layer header, for fast access | |
579 | TCP_HEAD *Tcp; // Transport layer header, for fast access | |
580 | UINT8 ProtoData [NET_PROTO_DATA]; //Protocol specific data | |
581 | ||
582 | NET_VECTOR *Vector; // The vector containing the packet | |
583 | ||
584 | UINT32 BlockOpNum; // Total number of BlockOp in the buffer | |
585 | UINT32 TotalSize; // Total size of the actual packet | |
586 | NET_BLOCK_OP BlockOp[1]; // Specify the position of actual packet | |
587 | } NET_BUF; | |
588 | ||
589 | ||
590 | // | |
591 | //A queue of NET_BUFs, It is just a thin extension of | |
592 | //NET_BUF functions. | |
593 | // | |
594 | typedef struct { | |
595 | UINT32 Signature; | |
596 | INTN RefCnt; | |
597 | NET_LIST_ENTRY List; // The List this buffer queue is on | |
598 | ||
599 | NET_LIST_ENTRY BufList; // list of queued buffers | |
600 | UINT32 BufSize; // total length of DATA in the buffers | |
601 | UINT32 BufNum; // total number of buffers on the chain | |
602 | } NET_BUF_QUEUE; | |
603 | ||
604 | // | |
605 | // Pseudo header for TCP and UDP checksum | |
606 | // | |
607 | #pragma pack(1) | |
608 | typedef struct { | |
609 | IP4_ADDR SrcIp; | |
610 | IP4_ADDR DstIp; | |
611 | UINT8 Reserved; | |
612 | UINT8 Protocol; | |
613 | UINT16 Len; | |
614 | } NET_PSEUDO_HDR; | |
615 | #pragma pack() | |
616 | ||
617 | // | |
618 | // The fragment entry table used in network interfaces. This is | |
619 | // the same as NET_BLOCK now. Use two different to distinguish | |
620 | // the two in case that NET_BLOCK be enhanced later. | |
621 | // | |
622 | typedef struct { | |
623 | UINT32 Len; | |
624 | UINT8 *Bulk; | |
625 | } NET_FRAGMENT; | |
626 | ||
627 | #define NET_GET_REF(PData) ((PData)->RefCnt++) | |
628 | #define NET_PUT_REF(PData) ((PData)->RefCnt--) | |
629 | #define NETBUF_FROM_PROTODATA(Info) _CR((Info), NET_BUF, ProtoData) | |
630 | ||
631 | #define NET_BUF_SHARED(Buf) \ | |
632 | (((Buf)->RefCnt > 1) || ((Buf)->Vector->RefCnt > 1)) | |
633 | ||
634 | #define NET_VECTOR_SIZE(BlockNum) \ | |
635 | (sizeof (NET_VECTOR) + ((BlockNum) - 1) * sizeof (NET_BLOCK)) | |
636 | ||
637 | #define NET_BUF_SIZE(BlockOpNum) \ | |
638 | (sizeof (NET_BUF) + ((BlockOpNum) - 1) * sizeof (NET_BLOCK_OP)) | |
639 | ||
640 | #define NET_HEADSPACE(BlockOp) \ | |
641 | (UINTN)((BlockOp)->Head - (BlockOp)->BlockHead) | |
642 | ||
643 | #define NET_TAILSPACE(BlockOp) \ | |
644 | (UINTN)((BlockOp)->BlockTail - (BlockOp)->Tail) | |
645 | ||
646 | NET_BUF * | |
647 | NetbufAlloc ( | |
648 | IN UINT32 Len | |
649 | ); | |
650 | ||
651 | VOID | |
652 | NetbufFree ( | |
653 | IN NET_BUF *Nbuf | |
654 | ); | |
655 | ||
656 | ||
657 | UINT8 * | |
658 | NetbufGetByte ( | |
659 | IN NET_BUF *Nbuf, | |
660 | IN UINT32 Offset, | |
661 | OUT UINT32 *Index OPTIONAL | |
662 | ); | |
663 | ||
664 | NET_BUF * | |
665 | NetbufClone ( | |
666 | IN NET_BUF *Nbuf | |
667 | ); | |
668 | ||
669 | NET_BUF * | |
670 | NetbufDuplicate ( | |
671 | IN NET_BUF *Nbuf, | |
672 | IN NET_BUF *Duplicate OPTIONAL, | |
673 | IN UINT32 HeadSpace | |
674 | ); | |
675 | ||
676 | NET_BUF * | |
677 | NetbufGetFragment ( | |
678 | IN NET_BUF *Nbuf, | |
679 | IN UINT32 Offset, | |
680 | IN UINT32 Len, | |
681 | IN UINT32 HeadSpace | |
682 | ); | |
683 | ||
684 | VOID | |
685 | NetbufReserve ( | |
686 | IN NET_BUF *Nbuf, | |
687 | IN UINT32 Len | |
688 | ); | |
689 | ||
690 | UINT8 * | |
691 | NetbufAllocSpace ( | |
692 | IN NET_BUF *Nbuf, | |
693 | IN UINT32 Len, | |
694 | IN BOOLEAN FromHead | |
695 | ); | |
696 | ||
697 | UINT32 | |
698 | NetbufTrim ( | |
699 | IN NET_BUF *Nbuf, | |
700 | IN UINT32 Len, | |
701 | IN BOOLEAN FromHead | |
702 | ); | |
703 | ||
704 | UINT32 | |
705 | NetbufCopy ( | |
706 | IN NET_BUF *Nbuf, | |
707 | IN UINT32 Offset, | |
708 | IN UINT32 Len, | |
709 | IN UINT8 *Dest | |
710 | ); | |
711 | ||
712 | NET_BUF * | |
713 | NetbufFromExt ( | |
714 | IN NET_FRAGMENT *ExtFragment, | |
715 | IN UINT32 ExtNum, | |
716 | IN UINT32 HeadSpace, | |
717 | IN UINT32 HeadLen, | |
718 | IN NET_VECTOR_EXT_FREE ExtFree, | |
719 | IN VOID *Arg OPTIONAL | |
720 | ); | |
721 | ||
722 | EFI_STATUS | |
723 | NetbufBuildExt ( | |
724 | IN NET_BUF *Nbuf, | |
725 | IN NET_FRAGMENT *ExtFragment, | |
726 | IN UINT32 *ExtNum | |
727 | ); | |
728 | ||
729 | NET_BUF * | |
730 | NetbufFromBufList ( | |
731 | IN NET_LIST_ENTRY *BufList, | |
732 | IN UINT32 HeadSpace, | |
733 | IN UINT32 HeaderLen, | |
734 | IN NET_VECTOR_EXT_FREE ExtFree, | |
735 | IN VOID *Arg OPTIONAL | |
736 | ); | |
737 | ||
738 | VOID | |
739 | NetbufFreeList ( | |
740 | IN NET_LIST_ENTRY *Head | |
741 | ); | |
742 | ||
743 | VOID | |
744 | NetbufQueInit ( | |
745 | IN NET_BUF_QUEUE *NbufQue | |
746 | ); | |
747 | ||
748 | NET_BUF_QUEUE * | |
749 | NetbufQueAlloc ( | |
750 | VOID | |
751 | ); | |
752 | ||
753 | VOID | |
754 | NetbufQueFree ( | |
755 | IN NET_BUF_QUEUE *NbufQue | |
756 | ); | |
757 | ||
758 | NET_BUF * | |
759 | NetbufQueRemove ( | |
760 | IN NET_BUF_QUEUE *NbufQue | |
761 | ); | |
762 | ||
763 | VOID | |
764 | NetbufQueAppend ( | |
765 | IN NET_BUF_QUEUE *NbufQue, | |
766 | IN NET_BUF *Nbuf | |
767 | ); | |
768 | ||
769 | UINT32 | |
770 | NetbufQueCopy ( | |
771 | IN NET_BUF_QUEUE *NbufQue, | |
772 | IN UINT32 Offset, | |
773 | IN UINT32 Len, | |
774 | IN UINT8 *Dest | |
775 | ); | |
776 | ||
777 | UINT32 | |
778 | NetbufQueTrim ( | |
779 | IN NET_BUF_QUEUE *NbufQue, | |
780 | IN UINT32 Len | |
781 | ); | |
782 | ||
783 | VOID | |
784 | NetbufQueFlush ( | |
785 | IN NET_BUF_QUEUE *NbufQue | |
786 | ); | |
787 | ||
788 | UINT16 | |
789 | NetblockChecksum ( | |
790 | IN UINT8 *Bulk, | |
791 | IN UINT32 Len | |
792 | ); | |
793 | ||
794 | UINT16 | |
795 | NetAddChecksum ( | |
796 | IN UINT16 Checksum1, | |
797 | IN UINT16 Checksum2 | |
798 | ); | |
799 | ||
800 | UINT16 | |
801 | NetbufChecksum ( | |
802 | IN NET_BUF *Nbuf | |
803 | ); | |
804 | ||
805 | UINT16 | |
806 | NetPseudoHeadChecksum ( | |
807 | IN IP4_ADDR Src, | |
808 | IN IP4_ADDR Dst, | |
809 | IN UINT8 Proto, | |
810 | IN UINT16 Len | |
811 | ); | |
812 | #endif |