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