]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Network/Ip4Dxe/Ip4Input.c
MdeModulePkg Ip4Dxe: Remove a redundant function
[mirror_edk2.git] / MdeModulePkg / Universal / Network / Ip4Dxe / Ip4Input.c
CommitLineData
772db4bb 1/** @file\r
3e8c18da 2 IP4 input process.\r
d1102dba
LG
3\r
4Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.<BR>\r
c79de074
SEHM
5(C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>\r
6\r
e5eed7d3 7This program and the accompanying materials\r
772db4bb 8are licensed and made available under the terms and conditions of the BSD License\r
9which accompanies this distribution. The full text of the license may be found at\r
10http://opensource.org/licenses/bsd-license.php\r
11\r
12THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
13WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
14\r
772db4bb 15**/\r
16\r
17#include "Ip4Impl.h"\r
18\r
19\r
20/**\r
96e1079f 21 Create an empty assemble entry for the packet identified by\r
772db4bb 22 (Dst, Src, Id, Protocol). The default life for the packet is\r
23 120 seconds.\r
24\r
3e8c18da 25 @param[in] Dst The destination address\r
26 @param[in] Src The source address\r
27 @param[in] Id The ID field in IP header\r
28 @param[in] Protocol The protocol field in IP header\r
772db4bb 29\r
30 @return NULL if failed to allocate memory for the entry, otherwise\r
3e8c18da 31 the point to just created reassemble entry.\r
772db4bb 32\r
33**/\r
772db4bb 34IP4_ASSEMBLE_ENTRY *\r
35Ip4CreateAssembleEntry (\r
36 IN IP4_ADDR Dst,\r
37 IN IP4_ADDR Src,\r
38 IN UINT16 Id,\r
39 IN UINT8 Protocol\r
40 )\r
41{\r
42\r
43 IP4_ASSEMBLE_ENTRY *Assemble;\r
44\r
e48e37fc 45 Assemble = AllocatePool (sizeof (IP4_ASSEMBLE_ENTRY));\r
772db4bb 46\r
47 if (Assemble == NULL) {\r
48 return NULL;\r
49 }\r
50\r
e48e37fc 51 InitializeListHead (&Assemble->Link);\r
52 InitializeListHead (&Assemble->Fragments);\r
772db4bb 53\r
54 Assemble->Dst = Dst;\r
55 Assemble->Src = Src;\r
56 Assemble->Id = Id;\r
57 Assemble->Protocol = Protocol;\r
58 Assemble->TotalLen = 0;\r
59 Assemble->CurLen = 0;\r
60 Assemble->Head = NULL;\r
61 Assemble->Info = NULL;\r
62 Assemble->Life = IP4_FRAGMENT_LIFE;\r
63\r
64 return Assemble;\r
65}\r
66\r
67\r
68/**\r
96e1079f 69 Release all the fragments of a packet, then free the assemble entry.\r
772db4bb 70\r
3e8c18da 71 @param[in] Assemble The assemble entry to free\r
772db4bb 72\r
73**/\r
772db4bb 74VOID\r
75Ip4FreeAssembleEntry (\r
76 IN IP4_ASSEMBLE_ENTRY *Assemble\r
77 )\r
78{\r
e48e37fc 79 LIST_ENTRY *Entry;\r
80 LIST_ENTRY *Next;\r
772db4bb 81 NET_BUF *Fragment;\r
82\r
83 NET_LIST_FOR_EACH_SAFE (Entry, Next, &Assemble->Fragments) {\r
84 Fragment = NET_LIST_USER_STRUCT (Entry, NET_BUF, List);\r
85\r
e48e37fc 86 RemoveEntryList (Entry);\r
772db4bb 87 NetbufFree (Fragment);\r
88 }\r
89\r
766c7483 90 FreePool (Assemble);\r
772db4bb 91}\r
92\r
93\r
94/**\r
95 Initialize an already allocated assemble table. This is generally\r
96 the assemble table embedded in the IP4 service instance.\r
97\r
3e8c18da 98 @param[in, out] Table The assemble table to initialize.\r
772db4bb 99\r
100**/\r
101VOID\r
102Ip4InitAssembleTable (\r
96e1079f 103 IN OUT IP4_ASSEMBLE_TABLE *Table\r
772db4bb 104 )\r
105{\r
106 UINT32 Index;\r
107\r
108 for (Index = 0; Index < IP4_ASSEMLE_HASH_SIZE; Index++) {\r
e48e37fc 109 InitializeListHead (&Table->Bucket[Index]);\r
772db4bb 110 }\r
111}\r
112\r
113\r
114/**\r
115 Clean up the assemble table: remove all the fragments\r
116 and assemble entries.\r
117\r
3e8c18da 118 @param[in] Table The assemble table to clean up\r
772db4bb 119\r
120**/\r
121VOID\r
122Ip4CleanAssembleTable (\r
123 IN IP4_ASSEMBLE_TABLE *Table\r
124 )\r
125{\r
e48e37fc 126 LIST_ENTRY *Entry;\r
127 LIST_ENTRY *Next;\r
772db4bb 128 IP4_ASSEMBLE_ENTRY *Assemble;\r
129 UINT32 Index;\r
130\r
131 for (Index = 0; Index < IP4_ASSEMLE_HASH_SIZE; Index++) {\r
132 NET_LIST_FOR_EACH_SAFE (Entry, Next, &Table->Bucket[Index]) {\r
133 Assemble = NET_LIST_USER_STRUCT (Entry, IP4_ASSEMBLE_ENTRY, Link);\r
134\r
e48e37fc 135 RemoveEntryList (Entry);\r
772db4bb 136 Ip4FreeAssembleEntry (Assemble);\r
137 }\r
138 }\r
139}\r
140\r
141\r
142/**\r
143 Trim the packet to fit in [Start, End), and update the per\r
144 packet information.\r
145\r
146 @param Packet Packet to trim\r
147 @param Start The sequence of the first byte to fit in\r
148 @param End One beyond the sequence of last byte to fit in.\r
149\r
772db4bb 150**/\r
772db4bb 151VOID\r
152Ip4TrimPacket (\r
96e1079f 153 IN OUT NET_BUF *Packet,\r
154 IN INTN Start,\r
155 IN INTN End\r
772db4bb 156 )\r
157{\r
158 IP4_CLIP_INFO *Info;\r
159 INTN Len;\r
160\r
161 Info = IP4_GET_CLIP_INFO (Packet);\r
162\r
163 ASSERT (Info->Start + Info->Length == Info->End);\r
164 ASSERT ((Info->Start < End) && (Start < Info->End));\r
165\r
166 if (Info->Start < Start) {\r
167 Len = Start - Info->Start;\r
168\r
169 NetbufTrim (Packet, (UINT32) Len, NET_BUF_HEAD);\r
170 Info->Start = Start;\r
171 Info->Length -= Len;\r
172 }\r
173\r
174 if (End < Info->End) {\r
175 Len = End - Info->End;\r
176\r
177 NetbufTrim (Packet, (UINT32) Len, NET_BUF_TAIL);\r
178 Info->End = End;\r
179 Info->Length -= Len;\r
180 }\r
181}\r
182\r
183\r
184/**\r
185 Release all the fragments of the packet. This is the callback for\r
186 the assembled packet's OnFree. It will free the assemble entry,\r
187 which in turn will free all the fragments of the packet.\r
188\r
3e8c18da 189 @param[in] Arg The assemble entry to free\r
772db4bb 190\r
191**/\r
772db4bb 192VOID\r
e798cd87 193EFIAPI\r
772db4bb 194Ip4OnFreeFragments (\r
195 IN VOID *Arg\r
196 )\r
197{\r
198 Ip4FreeAssembleEntry ((IP4_ASSEMBLE_ENTRY *) Arg);\r
199}\r
200\r
201\r
202/**\r
203 Reassemble the IP fragments. If all the fragments of the packet\r
204 have been received, it will wrap the packet in a net buffer then\r
205 return it to caller. If the packet can't be assembled, NULL is\r
206 return.\r
207\r
96e1079f 208 @param Table The assemble table used. New assemble entry will be created\r
209 if the Packet is from a new chain of fragments.\r
210 @param Packet The fragment to assemble. It might be freed if the fragment\r
211 can't be re-assembled.\r
772db4bb 212\r
213 @return NULL if the packet can't be reassemble. The point to just assembled\r
96e1079f 214 packet if all the fragments of the packet have arrived.\r
772db4bb 215\r
216**/\r
772db4bb 217NET_BUF *\r
218Ip4Reassemble (\r
96e1079f 219 IN OUT IP4_ASSEMBLE_TABLE *Table,\r
220 IN OUT NET_BUF *Packet\r
772db4bb 221 )\r
222{\r
223 IP4_HEAD *IpHead;\r
224 IP4_CLIP_INFO *This;\r
225 IP4_CLIP_INFO *Node;\r
226 IP4_ASSEMBLE_ENTRY *Assemble;\r
e48e37fc 227 LIST_ENTRY *Head;\r
228 LIST_ENTRY *Prev;\r
229 LIST_ENTRY *Cur;\r
772db4bb 230 NET_BUF *Fragment;\r
231 NET_BUF *NewPacket;\r
232 INTN Index;\r
233\r
f6b7393c 234 IpHead = Packet->Ip.Ip4;\r
772db4bb 235 This = IP4_GET_CLIP_INFO (Packet);\r
236\r
237 ASSERT (IpHead != NULL);\r
238\r
239 //\r
240 // First: find the related assemble entry\r
241 //\r
242 Assemble = NULL;\r
243 Index = IP4_ASSEMBLE_HASH (IpHead->Dst, IpHead->Src, IpHead->Id, IpHead->Protocol);\r
244\r
245 NET_LIST_FOR_EACH (Cur, &Table->Bucket[Index]) {\r
246 Assemble = NET_LIST_USER_STRUCT (Cur, IP4_ASSEMBLE_ENTRY, Link);\r
247\r
248 if ((Assemble->Dst == IpHead->Dst) && (Assemble->Src == IpHead->Src) &&\r
249 (Assemble->Id == IpHead->Id) && (Assemble->Protocol == IpHead->Protocol)) {\r
250 break;\r
251 }\r
252 }\r
253\r
254 //\r
255 // Create a new assemble entry if no assemble entry is related to this packet\r
256 //\r
257 if (Cur == &Table->Bucket[Index]) {\r
258 Assemble = Ip4CreateAssembleEntry (\r
259 IpHead->Dst,\r
260 IpHead->Src,\r
261 IpHead->Id,\r
262 IpHead->Protocol\r
263 );\r
264\r
265 if (Assemble == NULL) {\r
266 goto DROP;\r
267 }\r
268\r
e48e37fc 269 InsertHeadList (&Table->Bucket[Index], &Assemble->Link);\r
772db4bb 270 }\r
894d038a 271 //\r
272 // Assemble shouldn't be NULL here\r
273 //\r
274 ASSERT (Assemble != NULL);\r
772db4bb 275\r
276 //\r
277 // Find the point to insert the packet: before the first\r
278 // fragment with THIS.Start < CUR.Start. the previous one\r
279 // has PREV.Start <= THIS.Start < CUR.Start.\r
280 //\r
281 Head = &Assemble->Fragments;\r
282\r
283 NET_LIST_FOR_EACH (Cur, Head) {\r
284 Fragment = NET_LIST_USER_STRUCT (Cur, NET_BUF, List);\r
285\r
286 if (This->Start < IP4_GET_CLIP_INFO (Fragment)->Start) {\r
287 break;\r
288 }\r
289 }\r
290\r
291 //\r
292 // Check whether the current fragment overlaps with the previous one.\r
293 // It holds that: PREV.Start <= THIS.Start < THIS.End. Only need to\r
294 // check whether THIS.Start < PREV.End for overlap. If two fragments\r
295 // overlaps, trim the overlapped part off THIS fragment.\r
296 //\r
cc0b145e 297 if ((Prev = Cur->BackLink) != Head) {\r
772db4bb 298 Fragment = NET_LIST_USER_STRUCT (Prev, NET_BUF, List);\r
299 Node = IP4_GET_CLIP_INFO (Fragment);\r
300\r
301 if (This->Start < Node->End) {\r
302 if (This->End <= Node->End) {\r
303 NetbufFree (Packet);\r
304 return NULL;\r
305 }\r
306\r
307 Ip4TrimPacket (Packet, Node->End, This->End);\r
308 }\r
309 }\r
310\r
311 //\r
312 // Insert the fragment into the packet. The fragment may be removed\r
313 // from the list by the following checks.\r
314 //\r
315 NetListInsertBefore (Cur, &Packet->List);\r
316\r
317 //\r
318 // Check the packets after the insert point. It holds that:\r
319 // THIS.Start <= NODE.Start < NODE.End. The equality holds\r
320 // if PREV and NEXT are continuous. THIS fragment may fill\r
321 // several holes. Remove the completely overlapped fragments\r
322 //\r
323 while (Cur != Head) {\r
324 Fragment = NET_LIST_USER_STRUCT (Cur, NET_BUF, List);\r
325 Node = IP4_GET_CLIP_INFO (Fragment);\r
326\r
327 //\r
328 // Remove fragments completely overlapped by this fragment\r
329 //\r
330 if (Node->End <= This->End) {\r
331 Cur = Cur->ForwardLink;\r
332\r
e48e37fc 333 RemoveEntryList (&Fragment->List);\r
772db4bb 334 Assemble->CurLen -= Node->Length;\r
335\r
336 NetbufFree (Fragment);\r
337 continue;\r
338 }\r
339\r
340 //\r
341 // The conditions are: THIS.Start <= NODE.Start, and THIS.End <\r
342 // NODE.End. Two fragments overlaps if NODE.Start < THIS.End.\r
343 // If two fragments start at the same offset, remove THIS fragment\r
344 // because ((THIS.Start == NODE.Start) && (THIS.End < NODE.End)).\r
345 //\r
346 if (Node->Start < This->End) {\r
347 if (This->Start == Node->Start) {\r
e48e37fc 348 RemoveEntryList (&Packet->List);\r
772db4bb 349 goto DROP;\r
350 }\r
351\r
352 Ip4TrimPacket (Packet, This->Start, Node->Start);\r
353 }\r
354\r
355 break;\r
356 }\r
357\r
358 //\r
359 // Update the assemble info: increase the current length. If it is\r
360 // the frist fragment, update the packet's IP head and per packet\r
361 // info. If it is the last fragment, update the total length.\r
362 //\r
363 Assemble->CurLen += This->Length;\r
364\r
365 if (This->Start == 0) {\r
366 //\r
367 // Once the first fragment is enqueued, it can't be removed\r
368 // from the fragment list. So, Assemble->Head always point\r
369 // to valid memory area.\r
370 //\r
371 ASSERT (Assemble->Head == NULL);\r
372\r
373 Assemble->Head = IpHead;\r
374 Assemble->Info = IP4_GET_CLIP_INFO (Packet);\r
375 }\r
376\r
377 //\r
378 // Don't update the length more than once.\r
379 //\r
380 if (IP4_LAST_FRAGMENT (IpHead->Fragment) && (Assemble->TotalLen == 0)) {\r
381 Assemble->TotalLen = This->End;\r
382 }\r
383\r
384 //\r
385 // Deliver the whole packet if all the fragments received.\r
386 // All fragments received if:\r
96e1079f 387 // 1. received the last one, so, the total length is know\r
772db4bb 388 // 2. received all the data. If the last fragment on the\r
389 // queue ends at the total length, all data is received.\r
390 //\r
391 if ((Assemble->TotalLen != 0) && (Assemble->CurLen >= Assemble->TotalLen)) {\r
392\r
e48e37fc 393 RemoveEntryList (&Assemble->Link);\r
772db4bb 394\r
395 //\r
396 // If the packet is properly formated, the last fragment's End\r
397 // equals to the packet's total length. Otherwise, the packet\r
398 // is a fake, drop it now.\r
399 //\r
400 Fragment = NET_LIST_USER_STRUCT (Head->BackLink, NET_BUF, List);\r
401\r
402 if (IP4_GET_CLIP_INFO (Fragment)->End != Assemble->TotalLen) {\r
403 Ip4FreeAssembleEntry (Assemble);\r
404 return NULL;\r
405 }\r
406\r
407 //\r
408 // Wrap the packet in a net buffer then deliver it up\r
409 //\r
410 NewPacket = NetbufFromBufList (\r
411 &Assemble->Fragments,\r
412 0,\r
413 0,\r
414 Ip4OnFreeFragments,\r
415 Assemble\r
416 );\r
417\r
418 if (NewPacket == NULL) {\r
419 Ip4FreeAssembleEntry (Assemble);\r
420 return NULL;\r
421 }\r
422\r
f6b7393c 423 NewPacket->Ip.Ip4 = Assemble->Head;\r
04eb20aa
ED
424\r
425 ASSERT (Assemble->Info != NULL);\r
426\r
427 CopyMem (\r
428 IP4_GET_CLIP_INFO (NewPacket),\r
429 Assemble->Info,\r
430 sizeof (*IP4_GET_CLIP_INFO (NewPacket))\r
431 );\r
432\r
772db4bb 433 return NewPacket;\r
434 }\r
435\r
436 return NULL;\r
437\r
438DROP:\r
439 NetbufFree (Packet);\r
440 return NULL;\r
441}\r
442\r
a1503a32 443/**\r
d1102dba
LG
444 The callback function for the net buffer which wraps the packet processed by\r
445 IPsec. It releases the wrap packet and also signals IPsec to free the resources.\r
a1503a32 446\r
447 @param[in] Arg The wrap context\r
448\r
449**/\r
450VOID\r
e798cd87 451EFIAPI\r
a1503a32 452Ip4IpSecFree (\r
453 IN VOID *Arg\r
454 )\r
455{\r
456 IP4_IPSEC_WRAP *Wrap;\r
457\r
458 Wrap = (IP4_IPSEC_WRAP *) Arg;\r
459\r
460 if (Wrap->IpSecRecycleSignal != NULL) {\r
461 gBS->SignalEvent (Wrap->IpSecRecycleSignal);\r
462 }\r
463\r
464 NetbufFree (Wrap->Packet);\r
465\r
466 FreePool (Wrap);\r
467\r
468 return;\r
469}\r
470\r
471/**\r
d1102dba 472 The work function to locate IPsec protocol to process the inbound or\r
a1503a32 473 outbound IP packets. The process routine handls the packet with following\r
d1102dba 474 actions: bypass the packet, discard the packet, or protect the packet.\r
a1503a32 475\r
705f53a9 476 @param[in] IpSb The IP4 service instance.\r
477 @param[in, out] Head The The caller supplied IP4 header.\r
478 @param[in, out] Netbuf The IP4 packet to be processed by IPsec.\r
479 @param[in, out] Options The caller supplied options.\r
480 @param[in, out] OptionsLen The length of the option.\r
d1102dba 481 @param[in] Direction The directionality in an SPD entry,\r
705f53a9 482 EfiIPsecInBound or EfiIPsecOutBound.\r
483 @param[in] Context The token's wrap.\r
a1503a32 484\r
485 @retval EFI_SUCCESS The IPsec protocol is not available or disabled.\r
486 @retval EFI_SUCCESS The packet was bypassed and all buffers remain the same.\r
487 @retval EFI_SUCCESS The packet was protected.\r
d1102dba 488 @retval EFI_ACCESS_DENIED The packet was discarded.\r
a1503a32 489 @retval EFI_OUT_OF_RESOURCES There is no suffcient resource to complete the operation.\r
d1102dba 490 @retval EFI_BUFFER_TOO_SMALL The number of non-empty block is bigger than the\r
a1503a32 491 number of input data blocks when build a fragment table.\r
492\r
493**/\r
494EFI_STATUS\r
495Ip4IpSecProcessPacket (\r
705f53a9 496 IN IP4_SERVICE *IpSb,\r
497 IN OUT IP4_HEAD **Head,\r
498 IN OUT NET_BUF **Netbuf,\r
499 IN OUT UINT8 **Options,\r
500 IN OUT UINT32 *OptionsLen,\r
501 IN EFI_IPSEC_TRAFFIC_DIR Direction,\r
502 IN VOID *Context\r
a1503a32 503 )\r
504{\r
505 NET_FRAGMENT *FragmentTable;\r
705f53a9 506 NET_FRAGMENT *OriginalFragmentTable;\r
a1503a32 507 UINT32 FragmentCount;\r
705f53a9 508 UINT32 OriginalFragmentCount;\r
a1503a32 509 EFI_EVENT RecycleEvent;\r
510 NET_BUF *Packet;\r
511 IP4_TXTOKEN_WRAP *TxWrap;\r
512 IP4_IPSEC_WRAP *IpSecWrap;\r
513 EFI_STATUS Status;\r
705f53a9 514 IP4_HEAD ZeroHead;\r
a1503a32 515\r
516 Status = EFI_SUCCESS;\r
c79de074
SEHM
517\r
518 if (!mIpSec2Installed) {\r
519 goto ON_EXIT;\r
520 }\r
5aae2d35 521 ASSERT (mIpSec != NULL);\r
d1102dba 522\r
a1503a32 523 Packet = *Netbuf;\r
524 RecycleEvent = NULL;\r
525 IpSecWrap = NULL;\r
526 FragmentTable = NULL;\r
d1102dba 527 TxWrap = (IP4_TXTOKEN_WRAP *) Context;\r
a1503a32 528 FragmentCount = Packet->BlockOpNum;\r
705f53a9 529\r
530 ZeroMem (&ZeroHead, sizeof (IP4_HEAD));\r
a1503a32 531\r
a1503a32 532 //\r
533 // Check whether the IPsec enable variable is set.\r
534 //\r
535 if (mIpSec->DisabledFlag) {\r
536 //\r
537 // If IPsec is disabled, restore the original MTU\r
d1102dba 538 //\r
a1503a32 539 IpSb->MaxPacketSize = IpSb->OldMaxPacketSize;\r
540 goto ON_EXIT;\r
541 } else {\r
542 //\r
d1102dba 543 // If IPsec is enabled, use the MTU which reduce the IPsec header length.\r
a1503a32 544 //\r
d1102dba 545 IpSb->MaxPacketSize = IpSb->OldMaxPacketSize - IP4_MAX_IPSEC_HEADLEN;\r
a1503a32 546 }\r
547\r
548 //\r
549 // Rebuild fragment table from netbuf to ease IPsec process.\r
550 //\r
551 FragmentTable = AllocateZeroPool (FragmentCount * sizeof (NET_FRAGMENT));\r
552\r
553 if (FragmentTable == NULL) {\r
554 Status = EFI_OUT_OF_RESOURCES;\r
555 goto ON_EXIT;\r
556 }\r
d1102dba 557\r
a1503a32 558 Status = NetbufBuildExt (Packet, FragmentTable, &FragmentCount);\r
d1102dba 559\r
705f53a9 560 //\r
561 // Record the original FragmentTable and count.\r
562 //\r
563 OriginalFragmentTable = FragmentTable;\r
564 OriginalFragmentCount = FragmentCount;\r
a1503a32 565\r
566 if (EFI_ERROR (Status)) {\r
567 FreePool (FragmentTable);\r
568 goto ON_EXIT;\r
569 }\r
570\r
571 //\r
572 // Convert host byte order to network byte order\r
573 //\r
705f53a9 574 Ip4NtohHead (*Head);\r
d1102dba 575\r
705f53a9 576 Status = mIpSec->ProcessExt (\r
a1503a32 577 mIpSec,\r
578 IpSb->Controller,\r
579 IP_VERSION_4,\r
705f53a9 580 (VOID *) (*Head),\r
581 &(*Head)->Protocol,\r
da7ff698 582 (VOID **) Options,\r
705f53a9 583 OptionsLen,\r
a1503a32 584 (EFI_IPSEC_FRAGMENT_DATA **) (&FragmentTable),\r
585 &FragmentCount,\r
586 Direction,\r
587 &RecycleEvent\r
588 );\r
589 //\r
590 // Convert back to host byte order\r
591 //\r
705f53a9 592 Ip4NtohHead (*Head);\r
d1102dba 593\r
a1503a32 594 if (EFI_ERROR (Status)) {\r
94b928ca 595 FreePool (OriginalFragmentTable);\r
a1503a32 596 goto ON_EXIT;\r
597 }\r
598\r
705f53a9 599 if (OriginalFragmentTable == FragmentTable && OriginalFragmentCount == FragmentCount) {\r
94b928ca 600 //\r
601 // For ByPass Packet\r
602 //\r
603 FreePool (FragmentTable);\r
705f53a9 604 goto ON_EXIT;\r
94b928ca 605 } else {\r
606 //\r
607 // Free the FragmentTable which allocated before calling the IPsec.\r
608 //\r
609 FreePool (OriginalFragmentTable);\r
705f53a9 610 }\r
611\r
a1503a32 612 if (Direction == EfiIPsecOutBound && TxWrap != NULL) {\r
d1102dba 613\r
a1503a32 614 TxWrap->IpSecRecycleSignal = RecycleEvent;\r
615 TxWrap->Packet = NetbufFromExt (\r
616 FragmentTable,\r
617 FragmentCount,\r
618 IP4_MAX_HEADLEN,\r
619 0,\r
620 Ip4FreeTxToken,\r
621 TxWrap\r
622 );\r
623 if (TxWrap->Packet == NULL) {\r
94b928ca 624 //\r
625 // Recover the TxWrap->Packet, if meet a error, and the caller will free\r
626 // the TxWrap.\r
627 //\r
628 TxWrap->Packet = *Netbuf;\r
a1503a32 629 Status = EFI_OUT_OF_RESOURCES;\r
630 goto ON_EXIT;\r
631 }\r
632\r
705f53a9 633 //\r
634 // Free orginal Netbuf.\r
635 //\r
636 NetIpSecNetbufFree (*Netbuf);\r
a1503a32 637 *Netbuf = TxWrap->Packet;\r
d1102dba 638\r
a1503a32 639 } else {\r
d1102dba 640\r
a1503a32 641 IpSecWrap = AllocateZeroPool (sizeof (IP4_IPSEC_WRAP));\r
d1102dba 642\r
a1503a32 643 if (IpSecWrap == NULL) {\r
94b928ca 644 Status = EFI_OUT_OF_RESOURCES;\r
645 gBS->SignalEvent (RecycleEvent);\r
a1503a32 646 goto ON_EXIT;\r
647 }\r
d1102dba 648\r
a1503a32 649 IpSecWrap->IpSecRecycleSignal = RecycleEvent;\r
650 IpSecWrap->Packet = Packet;\r
651 Packet = NetbufFromExt (\r
d1102dba
LG
652 FragmentTable,\r
653 FragmentCount,\r
654 IP4_MAX_HEADLEN,\r
655 0,\r
656 Ip4IpSecFree,\r
a1503a32 657 IpSecWrap\r
658 );\r
d1102dba 659\r
a1503a32 660 if (Packet == NULL) {\r
94b928ca 661 Packet = IpSecWrap->Packet;\r
662 gBS->SignalEvent (RecycleEvent);\r
663 FreePool (IpSecWrap);\r
a1503a32 664 Status = EFI_OUT_OF_RESOURCES;\r
665 goto ON_EXIT;\r
666 }\r
667\r
705f53a9 668 if (Direction == EfiIPsecInBound && 0 != CompareMem (*Head, &ZeroHead, sizeof (IP4_HEAD))) {\r
669 Ip4PrependHead (Packet, *Head, *Options, *OptionsLen);\r
a1503a32 670 Ip4NtohHead (Packet->Ip.Ip4);\r
705f53a9 671 NetbufTrim (Packet, ((*Head)->HeadLen << 2), TRUE);\r
a1503a32 672\r
673 CopyMem (\r
674 IP4_GET_CLIP_INFO (Packet),\r
675 IP4_GET_CLIP_INFO (IpSecWrap->Packet),\r
676 sizeof (IP4_CLIP_INFO)\r
677 );\r
678 }\r
a1503a32 679 *Netbuf = Packet;\r
680 }\r
681\r
682ON_EXIT:\r
683 return Status;\r
684}\r
772db4bb 685\r
686/**\r
705f53a9 687 Pre-process the IPv4 packet. First validates the IPv4 packet, and\r
688 then reassembles packet if it is necessary.\r
d1102dba 689\r
705f53a9 690 @param[in] IpSb Pointer to IP4_SERVICE.\r
691 @param[in, out] Packet Pointer to the Packet to be processed.\r
692 @param[in] Head Pointer to the IP4_HEAD.\r
693 @param[in] Option Pointer to a buffer which contains the IPv4 option.\r
694 @param[in] OptionLen The length of Option in bytes.\r
695 @param[in] Flag The link layer flag for the packet received, such\r
696 as multicast.\r
772db4bb 697\r
705f53a9 698 @retval EFI_SEUCCESS The recieved packet is in well form.\r
d1102dba 699 @retval EFI_INVAILD_PARAMETER The recieved packet is malformed.\r
772db4bb 700\r
701**/\r
705f53a9 702EFI_STATUS\r
703Ip4PreProcessPacket (\r
704 IN IP4_SERVICE *IpSb,\r
705 IN OUT NET_BUF **Packet,\r
706 IN IP4_HEAD *Head,\r
707 IN UINT8 *Option,\r
d1102dba 708 IN UINT32 OptionLen,\r
705f53a9 709 IN UINT32 Flag\r
d1102dba 710 )\r
772db4bb 711{\r
772db4bb 712 IP4_CLIP_INFO *Info;\r
772db4bb 713 UINT32 HeadLen;\r
772db4bb 714 UINT32 TotalLen;\r
715 UINT16 Checksum;\r
772db4bb 716\r
717 //\r
216f7970 718 // Check if the IP4 header is correctly formatted.\r
772db4bb 719 //\r
705f53a9 720 if ((*Packet)->TotalSize < IP4_MIN_HEADLEN) {\r
721 return EFI_INVALID_PARAMETER;\r
772db4bb 722 }\r
d1102dba 723\r
772db4bb 724 HeadLen = (Head->HeadLen << 2);\r
725 TotalLen = NTOHS (Head->TotalLen);\r
726\r
727 //\r
728 // Mnp may deliver frame trailer sequence up, trim it off.\r
729 //\r
705f53a9 730 if (TotalLen < (*Packet)->TotalSize) {\r
731 NetbufTrim (*Packet, (*Packet)->TotalSize - TotalLen, FALSE);\r
772db4bb 732 }\r
733\r
734 if ((Head->Ver != 4) || (HeadLen < IP4_MIN_HEADLEN) ||\r
705f53a9 735 (TotalLen < HeadLen) || (TotalLen != (*Packet)->TotalSize)) {\r
736 return EFI_INVALID_PARAMETER;\r
772db4bb 737 }\r
738\r
739 //\r
740 // Some OS may send IP packets without checksum.\r
741 //\r
687a2e5f 742 Checksum = (UINT16) (~NetblockChecksum ((UINT8 *) Head, HeadLen));\r
772db4bb 743\r
744 if ((Head->Checksum != 0) && (Checksum != 0)) {\r
705f53a9 745 return EFI_INVALID_PARAMETER;\r
772db4bb 746 }\r
747\r
748 //\r
749 // Convert the IP header to host byte order, then get the per packet info.\r
750 //\r
705f53a9 751 (*Packet)->Ip.Ip4 = Ip4NtohHead (Head);\r
772db4bb 752\r
705f53a9 753 Info = IP4_GET_CLIP_INFO (*Packet);\r
772db4bb 754 Info->LinkFlag = Flag;\r
755 Info->CastType = Ip4GetHostCast (IpSb, Head->Dst, Head->Src);\r
756 Info->Start = (Head->Fragment & IP4_HEAD_OFFSET_MASK) << 3;\r
757 Info->Length = Head->TotalLen - HeadLen;\r
758 Info->End = Info->Start + Info->Length;\r
759 Info->Status = EFI_SUCCESS;\r
760\r
761 //\r
762 // The packet is destinated to us if the CastType is non-zero.\r
763 //\r
764 if ((Info->CastType == 0) || (Info->End > IP4_MAX_PACKET_SIZE)) {\r
705f53a9 765 return EFI_INVALID_PARAMETER;\r
772db4bb 766 }\r
767\r
768 //\r
769 // Validate the options. Don't call the Ip4OptionIsValid if\r
770 // there is no option to save some CPU process.\r
771 //\r
d1102dba 772\r
705f53a9 773 if ((OptionLen > 0) && !Ip4OptionIsValid (Option, OptionLen, TRUE)) {\r
774 return EFI_INVALID_PARAMETER;\r
772db4bb 775 }\r
776\r
777 //\r
216f7970 778 // Trim the head off, after this point, the packet is headless,\r
772db4bb 779 // and Packet->TotalLen == Info->Length.\r
780 //\r
705f53a9 781 NetbufTrim (*Packet, HeadLen, TRUE);\r
772db4bb 782\r
783 //\r
784 // Reassemble the packet if this is a fragment. The packet is a\r
785 // fragment if its head has MF (more fragment) set, or it starts\r
786 // at non-zero byte.\r
787 //\r
b2c0a175 788 if (((Head->Fragment & IP4_HEAD_MF_MASK) != 0) || (Info->Start != 0)) {\r
772db4bb 789 //\r
790 // Drop the fragment if DF is set but it is fragmented. Gateway\r
791 // need to send a type 4 destination unreache ICMP message here.\r
792 //\r
b2c0a175 793 if ((Head->Fragment & IP4_HEAD_DF_MASK) != 0) {\r
705f53a9 794 return EFI_INVALID_PARAMETER;\r
772db4bb 795 }\r
796\r
797 //\r
798 // The length of all but the last fragments is in the unit of 8 bytes.\r
799 //\r
b2c0a175 800 if (((Head->Fragment & IP4_HEAD_MF_MASK) != 0) && (Info->Length % 8 != 0)) {\r
705f53a9 801 return EFI_INVALID_PARAMETER;\r
772db4bb 802 }\r
803\r
705f53a9 804 *Packet = Ip4Reassemble (&IpSb->Assemble, *Packet);\r
772db4bb 805\r
806 //\r
807 // Packet assembly isn't complete, start receive more packet.\r
808 //\r
705f53a9 809 if (*Packet == NULL) {\r
810 return EFI_INVALID_PARAMETER;\r
772db4bb 811 }\r
812 }\r
d1102dba 813\r
705f53a9 814 return EFI_SUCCESS;\r
815}\r
816\r
817/**\r
818 The IP4 input routine. It is called by the IP4_INTERFACE when a\r
819 IP4 fragment is received from MNP.\r
820\r
821 @param[in] Ip4Instance The IP4 child that request the receive, most like\r
822 it is NULL.\r
823 @param[in] Packet The IP4 packet received.\r
824 @param[in] IoStatus The return status of receive request.\r
825 @param[in] Flag The link layer flag for the packet received, such\r
826 as multicast.\r
827 @param[in] Context The IP4 service instance that own the MNP.\r
828\r
829**/\r
830VOID\r
831Ip4AccpetFrame (\r
832 IN IP4_PROTOCOL *Ip4Instance,\r
833 IN NET_BUF *Packet,\r
834 IN EFI_STATUS IoStatus,\r
835 IN UINT32 Flag,\r
836 IN VOID *Context\r
837 )\r
838{\r
839 IP4_SERVICE *IpSb;\r
840 IP4_HEAD *Head;\r
841 EFI_STATUS Status;\r
842 IP4_HEAD ZeroHead;\r
843 UINT8 *Option;\r
844 UINT32 OptionLen;\r
d1102dba 845\r
705f53a9 846 IpSb = (IP4_SERVICE *) Context;\r
847 Option = NULL;\r
848\r
75dce340 849 if (EFI_ERROR (IoStatus) || (IpSb->State == IP4_SERVICE_DESTROY)) {\r
705f53a9 850 goto DROP;\r
851 }\r
852\r
a56b6e03
ED
853 Head = (IP4_HEAD *) NetbufGetByte (Packet, 0, NULL);\r
854 ASSERT (Head != NULL);\r
705f53a9 855 OptionLen = (Head->HeadLen << 2) - IP4_MIN_HEADLEN;\r
856 if (OptionLen > 0) {\r
857 Option = (UINT8 *) (Head + 1);\r
858 }\r
859\r
860 //\r
861 // Validate packet format and reassemble packet if it is necessary.\r
862 //\r
863 Status = Ip4PreProcessPacket (\r
d1102dba
LG
864 IpSb,\r
865 &Packet,\r
866 Head,\r
705f53a9 867 Option,\r
d1102dba 868 OptionLen,\r
705f53a9 869 Flag\r
870 );\r
871\r
872 if (EFI_ERROR (Status)) {\r
873 goto RESTART;\r
874 }\r
772db4bb 875\r
876 //\r
a1503a32 877 // After trim off, the packet is a esp/ah/udp/tcp/icmp6 net buffer,\r
878 // and no need consider any other ahead ext headers.\r
879 //\r
880 Status = Ip4IpSecProcessPacket (\r
94b928ca 881 IpSb,\r
882 &Head,\r
883 &Packet,\r
705f53a9 884 &Option,\r
94b928ca 885 &OptionLen,\r
a1503a32 886 EfiIPsecInBound,\r
887 NULL\r
888 );\r
889\r
705f53a9 890 if (EFI_ERROR (Status)) {\r
a1503a32 891 goto RESTART;\r
892 }\r
d1102dba 893\r
705f53a9 894 //\r
895 // If the packet is protected by tunnel mode, parse the inner Ip Packet.\r
896 //\r
897 ZeroMem (&ZeroHead, sizeof (IP4_HEAD));\r
898 if (0 == CompareMem (Head, &ZeroHead, sizeof (IP4_HEAD))) {\r
772db4bb 899 // Packet may have been changed. Head, HeadLen, TotalLen, and\r
900 // info must be reloaded bofore use. The ownership of the packet\r
901 // is transfered to the packet process logic.\r
902 //\r
705f53a9 903 Head = (IP4_HEAD *) NetbufGetByte (Packet, 0, NULL);\r
a56b6e03 904 ASSERT (Head != NULL);\r
705f53a9 905 Status = Ip4PreProcessPacket (\r
94b928ca 906 IpSb,\r
907 &Packet,\r
908 Head,\r
705f53a9 909 Option,\r
94b928ca 910 OptionLen,\r
705f53a9 911 Flag\r
912 );\r
913 if (EFI_ERROR (Status)) {\r
914 goto RESTART;\r
915 }\r
916 }\r
d1102dba 917\r
fbe12b79 918 ASSERT (Packet != NULL);\r
f6b7393c 919 Head = Packet->Ip.Ip4;\r
772db4bb 920 IP4_GET_CLIP_INFO (Packet)->Status = EFI_SUCCESS;\r
921\r
922 switch (Head->Protocol) {\r
f6b7393c 923 case EFI_IP_PROTO_ICMP:\r
772db4bb 924 Ip4IcmpHandle (IpSb, Head, Packet);\r
925 break;\r
926\r
927 case IP4_PROTO_IGMP:\r
928 Ip4IgmpHandle (IpSb, Head, Packet);\r
929 break;\r
930\r
931 default:\r
216f7970 932 Ip4Demultiplex (IpSb, Head, Packet, Option, OptionLen);\r
772db4bb 933 }\r
934\r
935 Packet = NULL;\r
936\r
36ee91ca 937 //\r
938 // Dispatch the DPCs queued by the NotifyFunction of the rx token's events\r
939 // which are signaled with received data.\r
940 //\r
d8d26fb2 941 DispatchDpc ();\r
36ee91ca 942\r
772db4bb 943RESTART:\r
944 Ip4ReceiveFrame (IpSb->DefaultInterface, NULL, Ip4AccpetFrame, IpSb);\r
945\r
946DROP:\r
947 if (Packet != NULL) {\r
948 NetbufFree (Packet);\r
949 }\r
950\r
951 return ;\r
952}\r
953\r
954\r
955/**\r
956 Check whether this IP child accepts the packet.\r
957\r
3e8c18da 958 @param[in] IpInstance The IP child to check\r
959 @param[in] Head The IP header of the packet\r
960 @param[in] Packet The data of the packet\r
772db4bb 961\r
96e1079f 962 @retval TRUE If the child wants to receive the packet.\r
963 @retval FALSE Otherwise.\r
772db4bb 964\r
965**/\r
966BOOLEAN\r
967Ip4InstanceFrameAcceptable (\r
968 IN IP4_PROTOCOL *IpInstance,\r
969 IN IP4_HEAD *Head,\r
970 IN NET_BUF *Packet\r
971 )\r
972{\r
973 IP4_ICMP_ERROR_HEAD Icmp;\r
974 EFI_IP4_CONFIG_DATA *Config;\r
975 IP4_CLIP_INFO *Info;\r
976 UINT16 Proto;\r
977 UINT32 Index;\r
978\r
979 Config = &IpInstance->ConfigData;\r
980\r
981 //\r
982 // Dirty trick for the Tiano UEFI network stack implmentation. If\r
983 // ReceiveTimeout == -1, the receive of the packet for this instance\r
96e1079f 984 // is disabled. The UEFI spec don't have such capability. We add\r
772db4bb 985 // this to improve the performance because IP will make a copy of\r
986 // the received packet for each accepting instance. Some IP instances\r
987 // used by UDP/TCP only send packets, they don't wants to receive.\r
988 //\r
989 if (Config->ReceiveTimeout == (UINT32)(-1)) {\r
990 return FALSE;\r
991 }\r
992\r
993 if (Config->AcceptPromiscuous) {\r
994 return TRUE;\r
995 }\r
996\r
997 //\r
998 // Use protocol from the IP header embedded in the ICMP error\r
999 // message to filter, instead of ICMP itself. ICMP handle will\r
9899d8b6 1000 // call Ip4Demultiplex to deliver ICMP errors.\r
772db4bb 1001 //\r
1002 Proto = Head->Protocol;\r
1003\r
9899d8b6 1004 if ((Proto == EFI_IP_PROTO_ICMP) && (!Config->AcceptAnyProtocol) && (Proto != Config->DefaultProtocol)) {\r
772db4bb 1005 NetbufCopy (Packet, 0, sizeof (Icmp.Head), (UINT8 *) &Icmp.Head);\r
1006\r
1007 if (mIcmpClass[Icmp.Head.Type].IcmpClass == ICMP_ERROR_MESSAGE) {\r
1008 if (!Config->AcceptIcmpErrors) {\r
1009 return FALSE;\r
1010 }\r
1011\r
1012 NetbufCopy (Packet, 0, sizeof (Icmp), (UINT8 *) &Icmp);\r
1013 Proto = Icmp.IpHead.Protocol;\r
1014 }\r
1015 }\r
1016\r
1017 //\r
1018 // Match the protocol\r
1019 //\r
1020 if (!Config->AcceptAnyProtocol && (Proto != Config->DefaultProtocol)) {\r
1021 return FALSE;\r
1022 }\r
1023\r
1024 //\r
1025 // Check for broadcast, the caller has computed the packet's\r
1026 // cast type for this child's interface.\r
1027 //\r
1028 Info = IP4_GET_CLIP_INFO (Packet);\r
1029\r
1030 if (IP4_IS_BROADCAST (Info->CastType)) {\r
1031 return Config->AcceptBroadcast;\r
1032 }\r
1033\r
1034 //\r
1035 // If it is a multicast packet, check whether we are in the group.\r
1036 //\r
1037 if (Info->CastType == IP4_MULTICAST) {\r
1038 //\r
1039 // Receive the multicast if the instance wants to receive all packets.\r
1040 //\r
1041 if (!IpInstance->ConfigData.UseDefaultAddress && (IpInstance->Interface->Ip == 0)) {\r
1042 return TRUE;\r
1043 }\r
1044\r
1045 for (Index = 0; Index < IpInstance->GroupCount; Index++) {\r
1046 if (IpInstance->Groups[Index] == HTONL (Head->Dst)) {\r
1047 break;\r
1048 }\r
1049 }\r
1050\r
1051 return (BOOLEAN)(Index < IpInstance->GroupCount);\r
1052 }\r
1053\r
1054 return TRUE;\r
1055}\r
1056\r
1057\r
1058/**\r
1059 Enqueue a shared copy of the packet to the IP4 child if the\r
1060 packet is acceptable to it. Here the data of the packet is\r
1061 shared, but the net buffer isn't.\r
1062\r
3e8c18da 1063 @param[in] IpInstance The IP4 child to enqueue the packet to\r
1064 @param[in] Head The IP header of the received packet\r
1065 @param[in] Packet The data of the received packet\r
772db4bb 1066\r
1067 @retval EFI_NOT_STARTED The IP child hasn't been configured.\r
1068 @retval EFI_INVALID_PARAMETER The child doesn't want to receive the packet\r
1069 @retval EFI_OUT_OF_RESOURCES Failed to allocate some resource\r
1070 @retval EFI_SUCCESS A shared copy the packet is enqueued to the child.\r
1071\r
1072**/\r
1073EFI_STATUS\r
1074Ip4InstanceEnquePacket (\r
1075 IN IP4_PROTOCOL *IpInstance,\r
1076 IN IP4_HEAD *Head,\r
1077 IN NET_BUF *Packet\r
1078 )\r
1079{\r
1080 IP4_CLIP_INFO *Info;\r
1081 NET_BUF *Clone;\r
1082\r
1083 //\r
1084 // Check whether the packet is acceptable to this instance.\r
1085 //\r
1086 if (IpInstance->State != IP4_STATE_CONFIGED) {\r
1087 return EFI_NOT_STARTED;\r
1088 }\r
1089\r
1090 if (!Ip4InstanceFrameAcceptable (IpInstance, Head, Packet)) {\r
1091 return EFI_INVALID_PARAMETER;\r
1092 }\r
1093\r
1094 //\r
1095 // Enque a shared copy of the packet.\r
1096 //\r
1097 Clone = NetbufClone (Packet);\r
1098\r
1099 if (Clone == NULL) {\r
1100 return EFI_OUT_OF_RESOURCES;\r
1101 }\r
1102\r
1103 //\r
1104 // Set the receive time out for the assembled packet. If it expires,\r
1105 // packet will be removed from the queue.\r
1106 //\r
1107 Info = IP4_GET_CLIP_INFO (Clone);\r
1108 Info->Life = IP4_US_TO_SEC (IpInstance->ConfigData.ReceiveTimeout);\r
1109\r
e48e37fc 1110 InsertTailList (&IpInstance->Received, &Clone->List);\r
772db4bb 1111 return EFI_SUCCESS;\r
1112}\r
1113\r
1114\r
1115/**\r
1116 The signal handle of IP4's recycle event. It is called back\r
1117 when the upper layer release the packet.\r
1118\r
3e8c18da 1119 @param Event The IP4's recycle event.\r
1120 @param Context The context of the handle, which is a\r
1121 IP4_RXDATA_WRAP\r
772db4bb 1122\r
1123**/\r
772db4bb 1124VOID\r
1125EFIAPI\r
1126Ip4OnRecyclePacket (\r
1127 IN EFI_EVENT Event,\r
1128 IN VOID *Context\r
1129 )\r
1130{\r
1131 IP4_RXDATA_WRAP *Wrap;\r
1132\r
1133 Wrap = (IP4_RXDATA_WRAP *) Context;\r
1134\r
e48e37fc 1135 EfiAcquireLockOrFail (&Wrap->IpInstance->RecycleLock);\r
1136 RemoveEntryList (&Wrap->Link);\r
1137 EfiReleaseLock (&Wrap->IpInstance->RecycleLock);\r
772db4bb 1138\r
1139 ASSERT (!NET_BUF_SHARED (Wrap->Packet));\r
1140 NetbufFree (Wrap->Packet);\r
1141\r
1142 gBS->CloseEvent (Wrap->RxData.RecycleSignal);\r
766c7483 1143 FreePool (Wrap);\r
772db4bb 1144}\r
1145\r
1146\r
1147/**\r
1148 Wrap the received packet to a IP4_RXDATA_WRAP, which will be\r
1149 delivered to the upper layer. Each IP4 child that accepts the\r
1150 packet will get a not-shared copy of the packet which is wrapped\r
1151 in the IP4_RXDATA_WRAP. The IP4_RXDATA_WRAP->RxData is passed\r
1152 to the upper layer. Upper layer will signal the recycle event in\r
1153 it when it is done with the packet.\r
1154\r
216f7970 1155 @param[in] IpInstance The IP4 child to receive the packet.\r
1156 @param[in] Packet The packet to deliver up.\r
772db4bb 1157\r
3e8c18da 1158 @retval Wrap if warp the packet succeed.\r
1159 @retval NULL failed to wrap the packet .\r
772db4bb 1160\r
1161**/\r
1162IP4_RXDATA_WRAP *\r
1163Ip4WrapRxData (\r
1164 IN IP4_PROTOCOL *IpInstance,\r
1165 IN NET_BUF *Packet\r
1166 )\r
1167{\r
1168 IP4_RXDATA_WRAP *Wrap;\r
1169 EFI_IP4_RECEIVE_DATA *RxData;\r
1170 EFI_STATUS Status;\r
216f7970 1171 BOOLEAN RawData;\r
772db4bb 1172\r
e48e37fc 1173 Wrap = AllocatePool (IP4_RXDATA_WRAP_SIZE (Packet->BlockOpNum));\r
772db4bb 1174\r
1175 if (Wrap == NULL) {\r
1176 return NULL;\r
1177 }\r
1178\r
e48e37fc 1179 InitializeListHead (&Wrap->Link);\r
772db4bb 1180\r
1181 Wrap->IpInstance = IpInstance;\r
1182 Wrap->Packet = Packet;\r
1183 RxData = &Wrap->RxData;\r
1184\r
216f7970 1185 ZeroMem (RxData, sizeof (EFI_IP4_RECEIVE_DATA));\r
772db4bb 1186\r
1187 Status = gBS->CreateEvent (\r
1188 EVT_NOTIFY_SIGNAL,\r
e48e37fc 1189 TPL_NOTIFY,\r
772db4bb 1190 Ip4OnRecyclePacket,\r
1191 Wrap,\r
1192 &RxData->RecycleSignal\r
1193 );\r
1194\r
1195 if (EFI_ERROR (Status)) {\r
766c7483 1196 FreePool (Wrap);\r
772db4bb 1197 return NULL;\r
1198 }\r
1199\r
f6b7393c 1200 ASSERT (Packet->Ip.Ip4 != NULL);\r
772db4bb 1201\r
216f7970 1202 ASSERT (IpInstance != NULL);\r
1203 RawData = IpInstance->ConfigData.RawData;\r
1204\r
772db4bb 1205 //\r
1206 // The application expects a network byte order header.\r
1207 //\r
216f7970 1208 if (!RawData) {\r
1209 RxData->HeaderLength = (Packet->Ip.Ip4->HeadLen << 2);\r
1210 RxData->Header = (EFI_IP4_HEADER *) Ip4NtohHead (Packet->Ip.Ip4);\r
1211 RxData->OptionsLength = RxData->HeaderLength - IP4_MIN_HEADLEN;\r
1212 RxData->Options = NULL;\r
772db4bb 1213\r
216f7970 1214 if (RxData->OptionsLength != 0) {\r
1215 RxData->Options = (VOID *) (RxData->Header + 1);\r
1216 }\r
772db4bb 1217 }\r
1218\r
1219 RxData->DataLength = Packet->TotalSize;\r
1220\r
1221 //\r
1222 // Build the fragment table to be delivered up.\r
1223 //\r
1224 RxData->FragmentCount = Packet->BlockOpNum;\r
1225 NetbufBuildExt (Packet, (NET_FRAGMENT *) RxData->FragmentTable, &RxData->FragmentCount);\r
1226\r
1227 return Wrap;\r
1228}\r
1229\r
1230\r
1231/**\r
1232 Deliver the received packets to upper layer if there are both received\r
1233 requests and enqueued packets. If the enqueued packet is shared, it will\r
1234 duplicate it to a non-shared packet, release the shared packet, then\r
1235 deliver the non-shared packet up.\r
1236\r
3e8c18da 1237 @param[in] IpInstance The IP child to deliver the packet up.\r
772db4bb 1238\r
1239 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources to deliver the\r
1240 packets.\r
1241 @retval EFI_SUCCESS All the enqueued packets that can be delivered\r
1242 are delivered up.\r
1243\r
1244**/\r
1245EFI_STATUS\r
1246Ip4InstanceDeliverPacket (\r
1247 IN IP4_PROTOCOL *IpInstance\r
1248 )\r
1249{\r
1250 EFI_IP4_COMPLETION_TOKEN *Token;\r
1251 IP4_RXDATA_WRAP *Wrap;\r
1252 NET_BUF *Packet;\r
1253 NET_BUF *Dup;\r
1254 UINT8 *Head;\r
216f7970 1255 UINT32 HeadLen;\r
772db4bb 1256\r
1257 //\r
1258 // Deliver a packet if there are both a packet and a receive token.\r
1259 //\r
e48e37fc 1260 while (!IsListEmpty (&IpInstance->Received) &&\r
772db4bb 1261 !NetMapIsEmpty (&IpInstance->RxTokens)) {\r
1262\r
1263 Packet = NET_LIST_HEAD (&IpInstance->Received, NET_BUF, List);\r
1264\r
1265 if (!NET_BUF_SHARED (Packet)) {\r
1266 //\r
1267 // If this is the only instance that wants the packet, wrap it up.\r
1268 //\r
1269 Wrap = Ip4WrapRxData (IpInstance, Packet);\r
1270\r
1271 if (Wrap == NULL) {\r
1272 return EFI_OUT_OF_RESOURCES;\r
1273 }\r
1274\r
e48e37fc 1275 RemoveEntryList (&Packet->List);\r
772db4bb 1276\r
1277 } else {\r
1278 //\r
1279 // Create a duplicated packet if this packet is shared\r
1280 //\r
216f7970 1281 if (IpInstance->ConfigData.RawData) {\r
1282 HeadLen = 0;\r
1283 } else {\r
1284 HeadLen = IP4_MAX_HEADLEN;\r
1285 }\r
1286\r
1287 Dup = NetbufDuplicate (Packet, NULL, HeadLen);\r
772db4bb 1288\r
1289 if (Dup == NULL) {\r
1290 return EFI_OUT_OF_RESOURCES;\r
1291 }\r
1292\r
216f7970 1293 if (!IpInstance->ConfigData.RawData) {\r
1294 //\r
1295 // Copy the IP head over. The packet to deliver up is\r
1296 // headless. Trim the head off after copy. The IP head\r
1297 // may be not continuous before the data.\r
1298 //\r
1299 Head = NetbufAllocSpace (Dup, IP4_MAX_HEADLEN, NET_BUF_HEAD);\r
1300 ASSERT (Head != NULL);\r
d1102dba 1301\r
216f7970 1302 Dup->Ip.Ip4 = (IP4_HEAD *) Head;\r
1303\r
1304 CopyMem (Head, Packet->Ip.Ip4, Packet->Ip.Ip4->HeadLen << 2);\r
1305 NetbufTrim (Dup, IP4_MAX_HEADLEN, TRUE);\r
1306 }\r
772db4bb 1307\r
1308 Wrap = Ip4WrapRxData (IpInstance, Dup);\r
1309\r
1310 if (Wrap == NULL) {\r
1311 NetbufFree (Dup);\r
1312 return EFI_OUT_OF_RESOURCES;\r
1313 }\r
1314\r
e48e37fc 1315 RemoveEntryList (&Packet->List);\r
772db4bb 1316 NetbufFree (Packet);\r
1317\r
1318 Packet = Dup;\r
1319 }\r
1320\r
1321 //\r
1322 // Insert it into the delivered packet, then get a user's\r
1323 // receive token, pass the wrapped packet up.\r
1324 //\r
e48e37fc 1325 EfiAcquireLockOrFail (&IpInstance->RecycleLock);\r
1326 InsertHeadList (&IpInstance->Delivered, &Wrap->Link);\r
1327 EfiReleaseLock (&IpInstance->RecycleLock);\r
772db4bb 1328\r
1329 Token = NetMapRemoveHead (&IpInstance->RxTokens, NULL);\r
1330 Token->Status = IP4_GET_CLIP_INFO (Packet)->Status;\r
1331 Token->Packet.RxData = &Wrap->RxData;\r
1332\r
1333 gBS->SignalEvent (Token->Event);\r
1334 }\r
1335\r
1336 return EFI_SUCCESS;\r
1337}\r
1338\r
1339\r
1340/**\r
1341 Enqueue a received packet to all the IP children that share\r
1342 the same interface.\r
1343\r
216f7970 1344 @param[in] IpSb The IP4 service instance that receive the packet.\r
1345 @param[in] Head The header of the received packet.\r
1346 @param[in] Packet The data of the received packet.\r
1347 @param[in] Option Point to the IP4 packet header options.\r
d1102dba 1348 @param[in] OptionLen Length of the IP4 packet header options.\r
216f7970 1349 @param[in] IpIf The interface to enqueue the packet to.\r
772db4bb 1350\r
1351 @return The number of the IP4 children that accepts the packet\r
1352\r
1353**/\r
1354INTN\r
1355Ip4InterfaceEnquePacket (\r
1356 IN IP4_SERVICE *IpSb,\r
1357 IN IP4_HEAD *Head,\r
1358 IN NET_BUF *Packet,\r
216f7970 1359 IN UINT8 *Option,\r
1360 IN UINT32 OptionLen,\r
772db4bb 1361 IN IP4_INTERFACE *IpIf\r
1362 )\r
1363{\r
1364 IP4_PROTOCOL *IpInstance;\r
1365 IP4_CLIP_INFO *Info;\r
e48e37fc 1366 LIST_ENTRY *Entry;\r
772db4bb 1367 INTN Enqueued;\r
1368 INTN LocalType;\r
1369 INTN SavedType;\r
1370\r
1371 //\r
1372 // First, check that the packet is acceptable to this interface\r
1373 // and find the local cast type for the interface. A packet sent\r
1374 // to say 192.168.1.1 should NOT be delliever to 10.0.0.1 unless\r
1375 // promiscuous receiving.\r
1376 //\r
1377 LocalType = 0;\r
1378 Info = IP4_GET_CLIP_INFO (Packet);\r
1379\r
1380 if ((Info->CastType == IP4_MULTICAST) || (Info->CastType == IP4_LOCAL_BROADCAST)) {\r
1381 //\r
1382 // If the CastType is multicast, don't need to filter against\r
1383 // the group address here, Ip4InstanceFrameAcceptable will do\r
1384 // that later.\r
1385 //\r
1386 LocalType = Info->CastType;\r
1387\r
1388 } else {\r
1389 //\r
1390 // Check the destination againist local IP. If the station\r
1391 // address is 0.0.0.0, it means receiving all the IP destined\r
1392 // to local non-zero IP. Otherwise, it is necessary to compare\r
1393 // the destination to the interface's IP address.\r
1394 //\r
1395 if (IpIf->Ip == IP4_ALLZERO_ADDRESS) {\r
1396 LocalType = IP4_LOCAL_HOST;\r
1397\r
1398 } else {\r
1399 LocalType = Ip4GetNetCast (Head->Dst, IpIf);\r
1400\r
1401 if ((LocalType == 0) && IpIf->PromiscRecv) {\r
1402 LocalType = IP4_PROMISCUOUS;\r
1403 }\r
1404 }\r
1405 }\r
1406\r
1407 if (LocalType == 0) {\r
1408 return 0;\r
1409 }\r
1410\r
1411 //\r
1412 // Iterate through the ip instances on the interface, enqueue\r
1413 // the packet if filter passed. Save the original cast type,\r
1414 // and pass the local cast type to the IP children on the\r
1415 // interface. The global cast type will be restored later.\r
1416 //\r
1417 SavedType = Info->CastType;\r
1418 Info->CastType = LocalType;\r
1419\r
1420 Enqueued = 0;\r
1421\r
1422 NET_LIST_FOR_EACH (Entry, &IpIf->IpInstances) {\r
1423 IpInstance = NET_LIST_USER_STRUCT (Entry, IP4_PROTOCOL, AddrLink);\r
1424 NET_CHECK_SIGNATURE (IpInstance, IP4_PROTOCOL_SIGNATURE);\r
1425\r
216f7970 1426 //\r
1427 // In RawData mode, add IPv4 headers and options back to packet.\r
1428 //\r
1429 if ((IpInstance->ConfigData.RawData) && (Option != NULL) && (OptionLen != 0)){\r
1430 Ip4PrependHead (Packet, Head, Option, OptionLen);\r
1431 }\r
1432\r
772db4bb 1433 if (Ip4InstanceEnquePacket (IpInstance, Head, Packet) == EFI_SUCCESS) {\r
1434 Enqueued++;\r
1435 }\r
1436 }\r
1437\r
1438 Info->CastType = SavedType;\r
1439 return Enqueued;\r
1440}\r
1441\r
1442\r
1443/**\r
1444 Deliver the packet for each IP4 child on the interface.\r
1445\r
3e8c18da 1446 @param[in] IpSb The IP4 service instance that received the packet\r
1447 @param[in] IpIf The IP4 interface to deliver the packet.\r
772db4bb 1448\r
1449 @retval EFI_SUCCESS It always returns EFI_SUCCESS now\r
1450\r
1451**/\r
1452EFI_STATUS\r
1453Ip4InterfaceDeliverPacket (\r
1454 IN IP4_SERVICE *IpSb,\r
1455 IN IP4_INTERFACE *IpIf\r
1456 )\r
1457{\r
1458 IP4_PROTOCOL *Ip4Instance;\r
e48e37fc 1459 LIST_ENTRY *Entry;\r
772db4bb 1460\r
1461 NET_LIST_FOR_EACH (Entry, &IpIf->IpInstances) {\r
1462 Ip4Instance = NET_LIST_USER_STRUCT (Entry, IP4_PROTOCOL, AddrLink);\r
1463 Ip4InstanceDeliverPacket (Ip4Instance);\r
1464 }\r
1465\r
1466 return EFI_SUCCESS;\r
1467}\r
1468\r
1469\r
1470/**\r
1471 Demultiple the packet. the packet delivery is processed in two\r
1472 passes. The first pass will enque a shared copy of the packet\r
1473 to each IP4 child that accepts the packet. The second pass will\r
1474 deliver a non-shared copy of the packet to each IP4 child that\r
1475 has pending receive requests. Data is copied if more than one\r
96e1079f 1476 child wants to consume the packet because each IP child needs\r
772db4bb 1477 its own copy of the packet to make changes.\r
1478\r
216f7970 1479 @param[in] IpSb The IP4 service instance that received the packet.\r
1480 @param[in] Head The header of the received packet.\r
1481 @param[in] Packet The data of the received packet.\r
1482 @param[in] Option Point to the IP4 packet header options.\r
1483 @param[in] OptionLen Length of the IP4 packet header options.\r
772db4bb 1484\r
216f7970 1485 @retval EFI_NOT_FOUND No IP child accepts the packet.\r
772db4bb 1486 @retval EFI_SUCCESS The packet is enqueued or delivered to some IP\r
1487 children.\r
1488\r
1489**/\r
1490EFI_STATUS\r
1491Ip4Demultiplex (\r
1492 IN IP4_SERVICE *IpSb,\r
1493 IN IP4_HEAD *Head,\r
216f7970 1494 IN NET_BUF *Packet,\r
1495 IN UINT8 *Option,\r
1496 IN UINT32 OptionLen\r
772db4bb 1497 )\r
1498{\r
e48e37fc 1499 LIST_ENTRY *Entry;\r
772db4bb 1500 IP4_INTERFACE *IpIf;\r
1501 INTN Enqueued;\r
1502\r
1503 //\r
1504 // Two pass delivery: first, enque a shared copy of the packet\r
1505 // to each instance that accept the packet.\r
1506 //\r
1507 Enqueued = 0;\r
1508\r
1509 NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) {\r
1510 IpIf = NET_LIST_USER_STRUCT (Entry, IP4_INTERFACE, Link);\r
1511\r
1512 if (IpIf->Configured) {\r
216f7970 1513 Enqueued += Ip4InterfaceEnquePacket (\r
1514 IpSb,\r
1515 Head,\r
1516 Packet,\r
1517 Option,\r
1518 OptionLen,\r
1519 IpIf\r
1520 );\r
772db4bb 1521 }\r
1522 }\r
1523\r
1524 //\r
1525 // Second: deliver a duplicate of the packet to each instance.\r
1526 // Release the local reference first, so that the last instance\r
1527 // getting the packet will not copy the data.\r
1528 //\r
1529 NetbufFree (Packet);\r
1530\r
1531 if (Enqueued == 0) {\r
1532 return EFI_NOT_FOUND;\r
1533 }\r
1534\r
1535 NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) {\r
1536 IpIf = NET_LIST_USER_STRUCT (Entry, IP4_INTERFACE, Link);\r
1537\r
1538 if (IpIf->Configured) {\r
1539 Ip4InterfaceDeliverPacket (IpSb, IpIf);\r
1540 }\r
1541 }\r
1542\r
1543 return EFI_SUCCESS;\r
1544}\r
1545\r
1546\r
1547/**\r
1548 Timeout the fragment and enqueued packets.\r
1549\r
3e8c18da 1550 @param[in] IpSb The IP4 service instance to timeout\r
772db4bb 1551\r
1552**/\r
1553VOID\r
1554Ip4PacketTimerTicking (\r
1555 IN IP4_SERVICE *IpSb\r
1556 )\r
1557{\r
e48e37fc 1558 LIST_ENTRY *InstanceEntry;\r
1559 LIST_ENTRY *Entry;\r
1560 LIST_ENTRY *Next;\r
772db4bb 1561 IP4_PROTOCOL *IpInstance;\r
1562 IP4_ASSEMBLE_ENTRY *Assemble;\r
1563 NET_BUF *Packet;\r
1564 IP4_CLIP_INFO *Info;\r
1565 UINT32 Index;\r
1566\r
1567 //\r
1568 // First, time out the fragments. The packet's life is counting down\r
1569 // once the first-arrived fragment was received.\r
1570 //\r
1571 for (Index = 0; Index < IP4_ASSEMLE_HASH_SIZE; Index++) {\r
1572 NET_LIST_FOR_EACH_SAFE (Entry, Next, &IpSb->Assemble.Bucket[Index]) {\r
1573 Assemble = NET_LIST_USER_STRUCT (Entry, IP4_ASSEMBLE_ENTRY, Link);\r
1574\r
1575 if ((Assemble->Life > 0) && (--Assemble->Life == 0)) {\r
e48e37fc 1576 RemoveEntryList (Entry);\r
772db4bb 1577 Ip4FreeAssembleEntry (Assemble);\r
1578 }\r
1579 }\r
1580 }\r
1581\r
1582 NET_LIST_FOR_EACH (InstanceEntry, &IpSb->Children) {\r
1583 IpInstance = NET_LIST_USER_STRUCT (InstanceEntry, IP4_PROTOCOL, Link);\r
1584\r
1585 //\r
1586 // Second, time out the assembled packets enqueued on each IP child.\r
1587 //\r
1588 NET_LIST_FOR_EACH_SAFE (Entry, Next, &IpInstance->Received) {\r
1589 Packet = NET_LIST_USER_STRUCT (Entry, NET_BUF, List);\r
1590 Info = IP4_GET_CLIP_INFO (Packet);\r
1591\r
1592 if ((Info->Life > 0) && (--Info->Life == 0)) {\r
e48e37fc 1593 RemoveEntryList (Entry);\r
772db4bb 1594 NetbufFree (Packet);\r
1595 }\r
1596 }\r
1597\r
1598 //\r
1599 // Third: time out the transmitted packets.\r
1600 //\r
1601 NetMapIterate (&IpInstance->TxTokens, Ip4SentPacketTicking, NULL);\r
1602 }\r
1603}\r