]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Network/Ip4Dxe/Ip4Input.c
Fixed the issue of that IP4driver can’t correctly reassemble the Out of Sequence...
[mirror_edk2.git] / MdeModulePkg / Universal / Network / Ip4Dxe / Ip4Input.c
CommitLineData
772db4bb 1/** @file\r
3e8c18da 2 IP4 input process.\r
3 \r
e5eed7d3
HT
4Copyright (c) 2005 - 2010, Intel Corporation. All rights reserved.<BR>\r
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
a167ecb1 295 if ((Cur != Head) && ((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
687a2e5f 422 CopyMem (IP4_GET_CLIP_INFO (NewPacket), Assemble->Info, sizeof (*IP4_GET_CLIP_INFO (NewPacket)));\r
772db4bb 423 return NewPacket;\r
424 }\r
425\r
426 return NULL;\r
427\r
428DROP:\r
429 NetbufFree (Packet);\r
430 return NULL;\r
431}\r
432\r
a1503a32 433/**\r
434 The callback function for the net buffer which wraps the packet processed by \r
435 IPsec. It releases the wrap packet and also signals IPsec to free the resources. \r
436\r
437 @param[in] Arg The wrap context\r
438\r
439**/\r
440VOID\r
e798cd87 441EFIAPI\r
a1503a32 442Ip4IpSecFree (\r
443 IN VOID *Arg\r
444 )\r
445{\r
446 IP4_IPSEC_WRAP *Wrap;\r
447\r
448 Wrap = (IP4_IPSEC_WRAP *) Arg;\r
449\r
450 if (Wrap->IpSecRecycleSignal != NULL) {\r
451 gBS->SignalEvent (Wrap->IpSecRecycleSignal);\r
452 }\r
453\r
454 NetbufFree (Wrap->Packet);\r
455\r
456 FreePool (Wrap);\r
457\r
458 return;\r
459}\r
460\r
461/**\r
462 The work function to locate IPsec protocol to process the inbound or \r
463 outbound IP packets. The process routine handls the packet with following\r
464 actions: bypass the packet, discard the packet, or protect the packet. \r
465\r
466 @param[in] IpSb The IP4 service instance\r
467 @param[in] Head The The caller supplied IP4 header.\r
468 @param[in, out] Netbuf The IP4 packet to be processed by IPsec\r
469 @param[in] Options The caller supplied options\r
470 @param[in] OptionsLen The length of the option\r
471 @param[in] Direction The directionality in an SPD entry, \r
472 EfiIPsecInBound or EfiIPsecOutBound\r
473 @param[in] Context The token's wrap\r
474\r
475 @retval EFI_SUCCESS The IPsec protocol is not available or disabled.\r
476 @retval EFI_SUCCESS The packet was bypassed and all buffers remain the same.\r
477 @retval EFI_SUCCESS The packet was protected.\r
478 @retval EFI_ACCESS_DENIED The packet was discarded. \r
479 @retval EFI_OUT_OF_RESOURCES There is no suffcient resource to complete the operation.\r
480 @retval EFI_BUFFER_TOO_SMALL The number of non-empty block is bigger than the \r
481 number of input data blocks when build a fragment table.\r
482\r
483**/\r
484EFI_STATUS\r
485Ip4IpSecProcessPacket (\r
486 IN IP4_SERVICE *IpSb,\r
487 IN IP4_HEAD *Head,\r
488 IN OUT NET_BUF **Netbuf,\r
489 IN UINT8 *Options,\r
490 IN UINT32 OptionsLen,\r
491 IN EFI_IPSEC_TRAFFIC_DIR Direction,\r
492 IN VOID *Context\r
493 )\r
494{\r
495 NET_FRAGMENT *FragmentTable;\r
496 UINT32 FragmentCount;\r
497 EFI_EVENT RecycleEvent;\r
498 NET_BUF *Packet;\r
499 IP4_TXTOKEN_WRAP *TxWrap;\r
500 IP4_IPSEC_WRAP *IpSecWrap;\r
501 EFI_STATUS Status;\r
502\r
503 Status = EFI_SUCCESS;\r
504 Packet = *Netbuf;\r
505 RecycleEvent = NULL;\r
506 IpSecWrap = NULL;\r
507 FragmentTable = NULL;\r
508 TxWrap = (IP4_TXTOKEN_WRAP *) Context; \r
509 FragmentCount = Packet->BlockOpNum;\r
510 \r
511 if (mIpSec == NULL) {\r
512 gBS->LocateProtocol (&gEfiIpSecProtocolGuid, NULL, (VOID **) &mIpSec);\r
513 if (mIpSec != NULL) {\r
514 //\r
515 // Save the original MTU\r
516 //\r
517 IpSb->OldMaxPacketSize = IpSb->MaxPacketSize; \r
518 }\r
519 }\r
520\r
521 //\r
522 // Check whether the IPsec protocol is available.\r
523 //\r
524 if (mIpSec == NULL) {\r
525 goto ON_EXIT;\r
526 }\r
527 //\r
528 // Check whether the IPsec enable variable is set.\r
529 //\r
530 if (mIpSec->DisabledFlag) {\r
531 //\r
532 // If IPsec is disabled, restore the original MTU\r
533 // \r
534 IpSb->MaxPacketSize = IpSb->OldMaxPacketSize;\r
535 goto ON_EXIT;\r
536 } else {\r
537 //\r
538 // If IPsec is enabled, use the MTU which reduce the IPsec header length. \r
539 //\r
540 IpSb->MaxPacketSize = IpSb->OldMaxPacketSize - IP4_MAX_IPSEC_HEADLEN; \r
541 }\r
542\r
543 //\r
544 // Rebuild fragment table from netbuf to ease IPsec process.\r
545 //\r
546 FragmentTable = AllocateZeroPool (FragmentCount * sizeof (NET_FRAGMENT));\r
547\r
548 if (FragmentTable == NULL) {\r
549 Status = EFI_OUT_OF_RESOURCES;\r
550 goto ON_EXIT;\r
551 }\r
552 \r
553 Status = NetbufBuildExt (Packet, FragmentTable, &FragmentCount);\r
554\r
555 if (EFI_ERROR (Status)) {\r
556 FreePool (FragmentTable);\r
557 goto ON_EXIT;\r
558 }\r
559\r
560 //\r
561 // Convert host byte order to network byte order\r
562 //\r
563 Ip4NtohHead (Head);\r
564 \r
565 Status = mIpSec->Process (\r
566 mIpSec,\r
567 IpSb->Controller,\r
568 IP_VERSION_4,\r
569 (VOID *) Head,\r
570 &Head->Protocol,\r
571 NULL,\r
572 0,\r
573 (EFI_IPSEC_FRAGMENT_DATA **) (&FragmentTable),\r
574 &FragmentCount,\r
575 Direction,\r
576 &RecycleEvent\r
577 );\r
578 //\r
579 // Convert back to host byte order\r
580 //\r
581 Ip4NtohHead (Head);\r
582 \r
583 if (EFI_ERROR (Status)) {\r
584 goto ON_EXIT;\r
585 }\r
586\r
587 if (Direction == EfiIPsecOutBound && TxWrap != NULL) {\r
588 \r
589 TxWrap->IpSecRecycleSignal = RecycleEvent;\r
590 TxWrap->Packet = NetbufFromExt (\r
591 FragmentTable,\r
592 FragmentCount,\r
593 IP4_MAX_HEADLEN,\r
594 0,\r
595 Ip4FreeTxToken,\r
596 TxWrap\r
597 );\r
598 if (TxWrap->Packet == NULL) {\r
599 Status = EFI_OUT_OF_RESOURCES;\r
600 goto ON_EXIT;\r
601 }\r
602\r
603 *Netbuf = TxWrap->Packet;\r
604 \r
605 } else {\r
606 \r
607 IpSecWrap = AllocateZeroPool (sizeof (IP4_IPSEC_WRAP));\r
608 \r
609 if (IpSecWrap == NULL) {\r
610 goto ON_EXIT;\r
611 }\r
612 \r
613 IpSecWrap->IpSecRecycleSignal = RecycleEvent;\r
614 IpSecWrap->Packet = Packet;\r
615 Packet = NetbufFromExt (\r
616 FragmentTable, \r
617 FragmentCount, \r
618 IP4_MAX_HEADLEN, \r
619 0, \r
620 Ip4IpSecFree, \r
621 IpSecWrap\r
622 );\r
623 \r
624 if (Packet == NULL) {\r
625 Status = EFI_OUT_OF_RESOURCES;\r
626 goto ON_EXIT;\r
627 }\r
628\r
629 if (Direction == EfiIPsecInBound) {\r
630 Ip4PrependHead (Packet, Head, Options, OptionsLen);\r
631 Ip4NtohHead (Packet->Ip.Ip4);\r
632 NetbufTrim (Packet, (Head->HeadLen << 2), TRUE);\r
633\r
634 CopyMem (\r
635 IP4_GET_CLIP_INFO (Packet),\r
636 IP4_GET_CLIP_INFO (IpSecWrap->Packet),\r
637 sizeof (IP4_CLIP_INFO)\r
638 );\r
639 }\r
640\r
641 *Netbuf = Packet;\r
642 }\r
643\r
644ON_EXIT:\r
645 return Status;\r
646}\r
772db4bb 647\r
648/**\r
649 The IP4 input routine. It is called by the IP4_INTERFACE when a\r
650 IP4 fragment is received from MNP.\r
651\r
3e8c18da 652 @param[in] Ip4Instance The IP4 child that request the receive, most like\r
772db4bb 653 it is NULL.\r
3e8c18da 654 @param[in] Packet The IP4 packet received.\r
655 @param[in] IoStatus The return status of receive request.\r
656 @param[in] Flag The link layer flag for the packet received, such\r
772db4bb 657 as multicast.\r
3e8c18da 658 @param[in] Context The IP4 service instance that own the MNP.\r
772db4bb 659\r
660**/\r
661VOID\r
662Ip4AccpetFrame (\r
663 IN IP4_PROTOCOL *Ip4Instance,\r
664 IN NET_BUF *Packet,\r
665 IN EFI_STATUS IoStatus,\r
666 IN UINT32 Flag,\r
667 IN VOID *Context\r
668 )\r
669{\r
670 IP4_SERVICE *IpSb;\r
671 IP4_CLIP_INFO *Info;\r
672 IP4_HEAD *Head;\r
673 UINT32 HeadLen;\r
674 UINT32 OptionLen;\r
675 UINT32 TotalLen;\r
676 UINT16 Checksum;\r
a1503a32 677 EFI_STATUS Status;\r
772db4bb 678\r
679 IpSb = (IP4_SERVICE *) Context;\r
680\r
681 if (EFI_ERROR (IoStatus) || (IpSb->State == IP4_SERVICE_DESTORY)) {\r
682 goto DROP;\r
683 }\r
684\r
685 //\r
96e1079f 686 // Check that the IP4 header is correctly formatted\r
772db4bb 687 //\r
688 if (Packet->TotalSize < IP4_MIN_HEADLEN) {\r
689 goto RESTART;\r
690 }\r
691\r
692 Head = (IP4_HEAD *) NetbufGetByte (Packet, 0, NULL);\r
693 HeadLen = (Head->HeadLen << 2);\r
694 TotalLen = NTOHS (Head->TotalLen);\r
695\r
696 //\r
697 // Mnp may deliver frame trailer sequence up, trim it off.\r
698 //\r
699 if (TotalLen < Packet->TotalSize) {\r
700 NetbufTrim (Packet, Packet->TotalSize - TotalLen, FALSE);\r
701 }\r
702\r
703 if ((Head->Ver != 4) || (HeadLen < IP4_MIN_HEADLEN) ||\r
704 (TotalLen < HeadLen) || (TotalLen != Packet->TotalSize)) {\r
705 goto RESTART;\r
706 }\r
707\r
708 //\r
709 // Some OS may send IP packets without checksum.\r
710 //\r
687a2e5f 711 Checksum = (UINT16) (~NetblockChecksum ((UINT8 *) Head, HeadLen));\r
772db4bb 712\r
713 if ((Head->Checksum != 0) && (Checksum != 0)) {\r
714 goto RESTART;\r
715 }\r
716\r
717 //\r
718 // Convert the IP header to host byte order, then get the per packet info.\r
719 //\r
f6b7393c 720 Packet->Ip.Ip4 = Ip4NtohHead (Head);\r
772db4bb 721\r
722 Info = IP4_GET_CLIP_INFO (Packet);\r
723 Info->LinkFlag = Flag;\r
724 Info->CastType = Ip4GetHostCast (IpSb, Head->Dst, Head->Src);\r
725 Info->Start = (Head->Fragment & IP4_HEAD_OFFSET_MASK) << 3;\r
726 Info->Length = Head->TotalLen - HeadLen;\r
727 Info->End = Info->Start + Info->Length;\r
728 Info->Status = EFI_SUCCESS;\r
729\r
730 //\r
731 // The packet is destinated to us if the CastType is non-zero.\r
732 //\r
733 if ((Info->CastType == 0) || (Info->End > IP4_MAX_PACKET_SIZE)) {\r
734 goto RESTART;\r
735 }\r
736\r
737 //\r
738 // Validate the options. Don't call the Ip4OptionIsValid if\r
739 // there is no option to save some CPU process.\r
740 //\r
741 OptionLen = HeadLen - IP4_MIN_HEADLEN;\r
742\r
743 if ((OptionLen > 0) && !Ip4OptionIsValid ((UINT8 *) (Head + 1), OptionLen, TRUE)) {\r
744 goto RESTART;\r
745 }\r
746\r
747 //\r
748 // Trim the head off, after this point, the packet is headless.\r
749 // and Packet->TotalLen == Info->Length.\r
750 //\r
751 NetbufTrim (Packet, HeadLen, TRUE);\r
752\r
753 //\r
754 // Reassemble the packet if this is a fragment. The packet is a\r
755 // fragment if its head has MF (more fragment) set, or it starts\r
756 // at non-zero byte.\r
757 //\r
b2c0a175 758 if (((Head->Fragment & IP4_HEAD_MF_MASK) != 0) || (Info->Start != 0)) {\r
772db4bb 759 //\r
760 // Drop the fragment if DF is set but it is fragmented. Gateway\r
761 // need to send a type 4 destination unreache ICMP message here.\r
762 //\r
b2c0a175 763 if ((Head->Fragment & IP4_HEAD_DF_MASK) != 0) {\r
772db4bb 764 goto RESTART;\r
765 }\r
766\r
767 //\r
768 // The length of all but the last fragments is in the unit of 8 bytes.\r
769 //\r
b2c0a175 770 if (((Head->Fragment & IP4_HEAD_MF_MASK) != 0) && (Info->Length % 8 != 0)) {\r
772db4bb 771 goto RESTART;\r
772 }\r
773\r
774 Packet = Ip4Reassemble (&IpSb->Assemble, Packet);\r
775\r
776 //\r
777 // Packet assembly isn't complete, start receive more packet.\r
778 //\r
779 if (Packet == NULL) {\r
780 goto RESTART;\r
781 }\r
782 }\r
783\r
784 //\r
a1503a32 785 // After trim off, the packet is a esp/ah/udp/tcp/icmp6 net buffer,\r
786 // and no need consider any other ahead ext headers.\r
787 //\r
788 Status = Ip4IpSecProcessPacket (\r
789 IpSb, \r
790 Head, \r
791 &Packet, \r
792 NULL,\r
793 0, \r
794 EfiIPsecInBound,\r
795 NULL\r
796 );\r
797\r
798 if (EFI_ERROR(Status)) {\r
799 goto RESTART;\r
800 }\r
772db4bb 801 // Packet may have been changed. Head, HeadLen, TotalLen, and\r
802 // info must be reloaded bofore use. The ownership of the packet\r
803 // is transfered to the packet process logic.\r
804 //\r
f6b7393c 805 Head = Packet->Ip.Ip4;\r
772db4bb 806 IP4_GET_CLIP_INFO (Packet)->Status = EFI_SUCCESS;\r
807\r
808 switch (Head->Protocol) {\r
f6b7393c 809 case EFI_IP_PROTO_ICMP:\r
772db4bb 810 Ip4IcmpHandle (IpSb, Head, Packet);\r
811 break;\r
812\r
813 case IP4_PROTO_IGMP:\r
814 Ip4IgmpHandle (IpSb, Head, Packet);\r
815 break;\r
816\r
817 default:\r
818 Ip4Demultiplex (IpSb, Head, Packet);\r
819 }\r
820\r
821 Packet = NULL;\r
822\r
36ee91ca 823 //\r
824 // Dispatch the DPCs queued by the NotifyFunction of the rx token's events\r
825 // which are signaled with received data.\r
826 //\r
d8d26fb2 827 DispatchDpc ();\r
36ee91ca 828\r
772db4bb 829RESTART:\r
830 Ip4ReceiveFrame (IpSb->DefaultInterface, NULL, Ip4AccpetFrame, IpSb);\r
831\r
832DROP:\r
833 if (Packet != NULL) {\r
834 NetbufFree (Packet);\r
835 }\r
836\r
837 return ;\r
838}\r
839\r
840\r
841/**\r
842 Check whether this IP child accepts the packet.\r
843\r
3e8c18da 844 @param[in] IpInstance The IP child to check\r
845 @param[in] Head The IP header of the packet\r
846 @param[in] Packet The data of the packet\r
772db4bb 847\r
96e1079f 848 @retval TRUE If the child wants to receive the packet.\r
849 @retval FALSE Otherwise.\r
772db4bb 850\r
851**/\r
852BOOLEAN\r
853Ip4InstanceFrameAcceptable (\r
854 IN IP4_PROTOCOL *IpInstance,\r
855 IN IP4_HEAD *Head,\r
856 IN NET_BUF *Packet\r
857 )\r
858{\r
859 IP4_ICMP_ERROR_HEAD Icmp;\r
860 EFI_IP4_CONFIG_DATA *Config;\r
861 IP4_CLIP_INFO *Info;\r
862 UINT16 Proto;\r
863 UINT32 Index;\r
864\r
865 Config = &IpInstance->ConfigData;\r
866\r
867 //\r
868 // Dirty trick for the Tiano UEFI network stack implmentation. If\r
869 // ReceiveTimeout == -1, the receive of the packet for this instance\r
96e1079f 870 // is disabled. The UEFI spec don't have such capability. We add\r
772db4bb 871 // this to improve the performance because IP will make a copy of\r
872 // the received packet for each accepting instance. Some IP instances\r
873 // used by UDP/TCP only send packets, they don't wants to receive.\r
874 //\r
875 if (Config->ReceiveTimeout == (UINT32)(-1)) {\r
876 return FALSE;\r
877 }\r
878\r
879 if (Config->AcceptPromiscuous) {\r
880 return TRUE;\r
881 }\r
882\r
883 //\r
884 // Use protocol from the IP header embedded in the ICMP error\r
885 // message to filter, instead of ICMP itself. ICMP handle will\r
9899d8b6 886 // call Ip4Demultiplex to deliver ICMP errors.\r
772db4bb 887 //\r
888 Proto = Head->Protocol;\r
889\r
9899d8b6 890 if ((Proto == EFI_IP_PROTO_ICMP) && (!Config->AcceptAnyProtocol) && (Proto != Config->DefaultProtocol)) {\r
772db4bb 891 NetbufCopy (Packet, 0, sizeof (Icmp.Head), (UINT8 *) &Icmp.Head);\r
892\r
893 if (mIcmpClass[Icmp.Head.Type].IcmpClass == ICMP_ERROR_MESSAGE) {\r
894 if (!Config->AcceptIcmpErrors) {\r
895 return FALSE;\r
896 }\r
897\r
898 NetbufCopy (Packet, 0, sizeof (Icmp), (UINT8 *) &Icmp);\r
899 Proto = Icmp.IpHead.Protocol;\r
900 }\r
901 }\r
902\r
903 //\r
904 // Match the protocol\r
905 //\r
906 if (!Config->AcceptAnyProtocol && (Proto != Config->DefaultProtocol)) {\r
907 return FALSE;\r
908 }\r
909\r
910 //\r
911 // Check for broadcast, the caller has computed the packet's\r
912 // cast type for this child's interface.\r
913 //\r
914 Info = IP4_GET_CLIP_INFO (Packet);\r
915\r
916 if (IP4_IS_BROADCAST (Info->CastType)) {\r
917 return Config->AcceptBroadcast;\r
918 }\r
919\r
920 //\r
921 // If it is a multicast packet, check whether we are in the group.\r
922 //\r
923 if (Info->CastType == IP4_MULTICAST) {\r
924 //\r
925 // Receive the multicast if the instance wants to receive all packets.\r
926 //\r
927 if (!IpInstance->ConfigData.UseDefaultAddress && (IpInstance->Interface->Ip == 0)) {\r
928 return TRUE;\r
929 }\r
930\r
931 for (Index = 0; Index < IpInstance->GroupCount; Index++) {\r
932 if (IpInstance->Groups[Index] == HTONL (Head->Dst)) {\r
933 break;\r
934 }\r
935 }\r
936\r
937 return (BOOLEAN)(Index < IpInstance->GroupCount);\r
938 }\r
939\r
940 return TRUE;\r
941}\r
942\r
943\r
944/**\r
945 Enqueue a shared copy of the packet to the IP4 child if the\r
946 packet is acceptable to it. Here the data of the packet is\r
947 shared, but the net buffer isn't.\r
948\r
3e8c18da 949 @param[in] IpInstance The IP4 child to enqueue the packet to\r
950 @param[in] Head The IP header of the received packet\r
951 @param[in] Packet The data of the received packet\r
772db4bb 952\r
953 @retval EFI_NOT_STARTED The IP child hasn't been configured.\r
954 @retval EFI_INVALID_PARAMETER The child doesn't want to receive the packet\r
955 @retval EFI_OUT_OF_RESOURCES Failed to allocate some resource\r
956 @retval EFI_SUCCESS A shared copy the packet is enqueued to the child.\r
957\r
958**/\r
959EFI_STATUS\r
960Ip4InstanceEnquePacket (\r
961 IN IP4_PROTOCOL *IpInstance,\r
962 IN IP4_HEAD *Head,\r
963 IN NET_BUF *Packet\r
964 )\r
965{\r
966 IP4_CLIP_INFO *Info;\r
967 NET_BUF *Clone;\r
968\r
969 //\r
970 // Check whether the packet is acceptable to this instance.\r
971 //\r
972 if (IpInstance->State != IP4_STATE_CONFIGED) {\r
973 return EFI_NOT_STARTED;\r
974 }\r
975\r
976 if (!Ip4InstanceFrameAcceptable (IpInstance, Head, Packet)) {\r
977 return EFI_INVALID_PARAMETER;\r
978 }\r
979\r
980 //\r
981 // Enque a shared copy of the packet.\r
982 //\r
983 Clone = NetbufClone (Packet);\r
984\r
985 if (Clone == NULL) {\r
986 return EFI_OUT_OF_RESOURCES;\r
987 }\r
988\r
989 //\r
990 // Set the receive time out for the assembled packet. If it expires,\r
991 // packet will be removed from the queue.\r
992 //\r
993 Info = IP4_GET_CLIP_INFO (Clone);\r
994 Info->Life = IP4_US_TO_SEC (IpInstance->ConfigData.ReceiveTimeout);\r
995\r
e48e37fc 996 InsertTailList (&IpInstance->Received, &Clone->List);\r
772db4bb 997 return EFI_SUCCESS;\r
998}\r
999\r
1000\r
1001/**\r
1002 The signal handle of IP4's recycle event. It is called back\r
1003 when the upper layer release the packet.\r
1004\r
3e8c18da 1005 @param Event The IP4's recycle event.\r
1006 @param Context The context of the handle, which is a\r
1007 IP4_RXDATA_WRAP\r
772db4bb 1008\r
1009**/\r
772db4bb 1010VOID\r
1011EFIAPI\r
1012Ip4OnRecyclePacket (\r
1013 IN EFI_EVENT Event,\r
1014 IN VOID *Context\r
1015 )\r
1016{\r
1017 IP4_RXDATA_WRAP *Wrap;\r
1018\r
1019 Wrap = (IP4_RXDATA_WRAP *) Context;\r
1020\r
e48e37fc 1021 EfiAcquireLockOrFail (&Wrap->IpInstance->RecycleLock);\r
1022 RemoveEntryList (&Wrap->Link);\r
1023 EfiReleaseLock (&Wrap->IpInstance->RecycleLock);\r
772db4bb 1024\r
1025 ASSERT (!NET_BUF_SHARED (Wrap->Packet));\r
1026 NetbufFree (Wrap->Packet);\r
1027\r
1028 gBS->CloseEvent (Wrap->RxData.RecycleSignal);\r
766c7483 1029 FreePool (Wrap);\r
772db4bb 1030}\r
1031\r
1032\r
1033/**\r
1034 Wrap the received packet to a IP4_RXDATA_WRAP, which will be\r
1035 delivered to the upper layer. Each IP4 child that accepts the\r
1036 packet will get a not-shared copy of the packet which is wrapped\r
1037 in the IP4_RXDATA_WRAP. The IP4_RXDATA_WRAP->RxData is passed\r
1038 to the upper layer. Upper layer will signal the recycle event in\r
1039 it when it is done with the packet.\r
1040\r
3e8c18da 1041 @param[in] IpInstance The IP4 child to receive the packet\r
1042 @param[in] Packet The packet to deliver up.\r
772db4bb 1043\r
3e8c18da 1044 @retval Wrap if warp the packet succeed.\r
1045 @retval NULL failed to wrap the packet .\r
772db4bb 1046\r
1047**/\r
1048IP4_RXDATA_WRAP *\r
1049Ip4WrapRxData (\r
1050 IN IP4_PROTOCOL *IpInstance,\r
1051 IN NET_BUF *Packet\r
1052 )\r
1053{\r
1054 IP4_RXDATA_WRAP *Wrap;\r
1055 EFI_IP4_RECEIVE_DATA *RxData;\r
1056 EFI_STATUS Status;\r
1057\r
e48e37fc 1058 Wrap = AllocatePool (IP4_RXDATA_WRAP_SIZE (Packet->BlockOpNum));\r
772db4bb 1059\r
1060 if (Wrap == NULL) {\r
1061 return NULL;\r
1062 }\r
1063\r
e48e37fc 1064 InitializeListHead (&Wrap->Link);\r
772db4bb 1065\r
1066 Wrap->IpInstance = IpInstance;\r
1067 Wrap->Packet = Packet;\r
1068 RxData = &Wrap->RxData;\r
1069\r
e48e37fc 1070 ZeroMem (&RxData->TimeStamp, sizeof (EFI_TIME));\r
772db4bb 1071\r
1072 Status = gBS->CreateEvent (\r
1073 EVT_NOTIFY_SIGNAL,\r
e48e37fc 1074 TPL_NOTIFY,\r
772db4bb 1075 Ip4OnRecyclePacket,\r
1076 Wrap,\r
1077 &RxData->RecycleSignal\r
1078 );\r
1079\r
1080 if (EFI_ERROR (Status)) {\r
766c7483 1081 FreePool (Wrap);\r
772db4bb 1082 return NULL;\r
1083 }\r
1084\r
f6b7393c 1085 ASSERT (Packet->Ip.Ip4 != NULL);\r
772db4bb 1086\r
1087 //\r
1088 // The application expects a network byte order header.\r
1089 //\r
f6b7393c 1090 RxData->HeaderLength = (Packet->Ip.Ip4->HeadLen << 2);\r
1091 RxData->Header = (EFI_IP4_HEADER *) Ip4NtohHead (Packet->Ip.Ip4);\r
772db4bb 1092\r
1093 RxData->OptionsLength = RxData->HeaderLength - IP4_MIN_HEADLEN;\r
1094 RxData->Options = NULL;\r
1095\r
1096 if (RxData->OptionsLength != 0) {\r
1097 RxData->Options = (VOID *) (RxData->Header + 1);\r
1098 }\r
1099\r
1100 RxData->DataLength = Packet->TotalSize;\r
1101\r
1102 //\r
1103 // Build the fragment table to be delivered up.\r
1104 //\r
1105 RxData->FragmentCount = Packet->BlockOpNum;\r
1106 NetbufBuildExt (Packet, (NET_FRAGMENT *) RxData->FragmentTable, &RxData->FragmentCount);\r
1107\r
1108 return Wrap;\r
1109}\r
1110\r
1111\r
1112/**\r
1113 Deliver the received packets to upper layer if there are both received\r
1114 requests and enqueued packets. If the enqueued packet is shared, it will\r
1115 duplicate it to a non-shared packet, release the shared packet, then\r
1116 deliver the non-shared packet up.\r
1117\r
3e8c18da 1118 @param[in] IpInstance The IP child to deliver the packet up.\r
772db4bb 1119\r
1120 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources to deliver the\r
1121 packets.\r
1122 @retval EFI_SUCCESS All the enqueued packets that can be delivered\r
1123 are delivered up.\r
1124\r
1125**/\r
1126EFI_STATUS\r
1127Ip4InstanceDeliverPacket (\r
1128 IN IP4_PROTOCOL *IpInstance\r
1129 )\r
1130{\r
1131 EFI_IP4_COMPLETION_TOKEN *Token;\r
1132 IP4_RXDATA_WRAP *Wrap;\r
1133 NET_BUF *Packet;\r
1134 NET_BUF *Dup;\r
1135 UINT8 *Head;\r
1136\r
1137 //\r
1138 // Deliver a packet if there are both a packet and a receive token.\r
1139 //\r
e48e37fc 1140 while (!IsListEmpty (&IpInstance->Received) &&\r
772db4bb 1141 !NetMapIsEmpty (&IpInstance->RxTokens)) {\r
1142\r
1143 Packet = NET_LIST_HEAD (&IpInstance->Received, NET_BUF, List);\r
1144\r
1145 if (!NET_BUF_SHARED (Packet)) {\r
1146 //\r
1147 // If this is the only instance that wants the packet, wrap it up.\r
1148 //\r
1149 Wrap = Ip4WrapRxData (IpInstance, Packet);\r
1150\r
1151 if (Wrap == NULL) {\r
1152 return EFI_OUT_OF_RESOURCES;\r
1153 }\r
1154\r
e48e37fc 1155 RemoveEntryList (&Packet->List);\r
772db4bb 1156\r
1157 } else {\r
1158 //\r
1159 // Create a duplicated packet if this packet is shared\r
1160 //\r
1161 Dup = NetbufDuplicate (Packet, NULL, IP4_MAX_HEADLEN);\r
1162\r
1163 if (Dup == NULL) {\r
1164 return EFI_OUT_OF_RESOURCES;\r
1165 }\r
1166\r
1167 //\r
1168 // Copy the IP head over. The packet to deliver up is\r
1169 // headless. Trim the head off after copy. The IP head\r
1170 // may be not continuous before the data.\r
1171 //\r
1172 Head = NetbufAllocSpace (Dup, IP4_MAX_HEADLEN, NET_BUF_HEAD);\r
f6b7393c 1173 Dup->Ip.Ip4 = (IP4_HEAD *) Head;\r
772db4bb 1174\r
f6b7393c 1175 CopyMem (Head, Packet->Ip.Ip4, Packet->Ip.Ip4->HeadLen << 2);\r
772db4bb 1176 NetbufTrim (Dup, IP4_MAX_HEADLEN, TRUE);\r
1177\r
1178 Wrap = Ip4WrapRxData (IpInstance, Dup);\r
1179\r
1180 if (Wrap == NULL) {\r
1181 NetbufFree (Dup);\r
1182 return EFI_OUT_OF_RESOURCES;\r
1183 }\r
1184\r
e48e37fc 1185 RemoveEntryList (&Packet->List);\r
772db4bb 1186 NetbufFree (Packet);\r
1187\r
1188 Packet = Dup;\r
1189 }\r
1190\r
1191 //\r
1192 // Insert it into the delivered packet, then get a user's\r
1193 // receive token, pass the wrapped packet up.\r
1194 //\r
e48e37fc 1195 EfiAcquireLockOrFail (&IpInstance->RecycleLock);\r
1196 InsertHeadList (&IpInstance->Delivered, &Wrap->Link);\r
1197 EfiReleaseLock (&IpInstance->RecycleLock);\r
772db4bb 1198\r
1199 Token = NetMapRemoveHead (&IpInstance->RxTokens, NULL);\r
1200 Token->Status = IP4_GET_CLIP_INFO (Packet)->Status;\r
1201 Token->Packet.RxData = &Wrap->RxData;\r
1202\r
1203 gBS->SignalEvent (Token->Event);\r
1204 }\r
1205\r
1206 return EFI_SUCCESS;\r
1207}\r
1208\r
1209\r
1210/**\r
1211 Enqueue a received packet to all the IP children that share\r
1212 the same interface.\r
1213\r
3e8c18da 1214 @param[in] IpSb The IP4 service instance that receive the packet\r
1215 @param[in] Head The header of the received packet\r
1216 @param[in] Packet The data of the received packet\r
1217 @param[in] IpIf The interface to enqueue the packet to\r
772db4bb 1218\r
1219 @return The number of the IP4 children that accepts the packet\r
1220\r
1221**/\r
1222INTN\r
1223Ip4InterfaceEnquePacket (\r
1224 IN IP4_SERVICE *IpSb,\r
1225 IN IP4_HEAD *Head,\r
1226 IN NET_BUF *Packet,\r
1227 IN IP4_INTERFACE *IpIf\r
1228 )\r
1229{\r
1230 IP4_PROTOCOL *IpInstance;\r
1231 IP4_CLIP_INFO *Info;\r
e48e37fc 1232 LIST_ENTRY *Entry;\r
772db4bb 1233 INTN Enqueued;\r
1234 INTN LocalType;\r
1235 INTN SavedType;\r
1236\r
1237 //\r
1238 // First, check that the packet is acceptable to this interface\r
1239 // and find the local cast type for the interface. A packet sent\r
1240 // to say 192.168.1.1 should NOT be delliever to 10.0.0.1 unless\r
1241 // promiscuous receiving.\r
1242 //\r
1243 LocalType = 0;\r
1244 Info = IP4_GET_CLIP_INFO (Packet);\r
1245\r
1246 if ((Info->CastType == IP4_MULTICAST) || (Info->CastType == IP4_LOCAL_BROADCAST)) {\r
1247 //\r
1248 // If the CastType is multicast, don't need to filter against\r
1249 // the group address here, Ip4InstanceFrameAcceptable will do\r
1250 // that later.\r
1251 //\r
1252 LocalType = Info->CastType;\r
1253\r
1254 } else {\r
1255 //\r
1256 // Check the destination againist local IP. If the station\r
1257 // address is 0.0.0.0, it means receiving all the IP destined\r
1258 // to local non-zero IP. Otherwise, it is necessary to compare\r
1259 // the destination to the interface's IP address.\r
1260 //\r
1261 if (IpIf->Ip == IP4_ALLZERO_ADDRESS) {\r
1262 LocalType = IP4_LOCAL_HOST;\r
1263\r
1264 } else {\r
1265 LocalType = Ip4GetNetCast (Head->Dst, IpIf);\r
1266\r
1267 if ((LocalType == 0) && IpIf->PromiscRecv) {\r
1268 LocalType = IP4_PROMISCUOUS;\r
1269 }\r
1270 }\r
1271 }\r
1272\r
1273 if (LocalType == 0) {\r
1274 return 0;\r
1275 }\r
1276\r
1277 //\r
1278 // Iterate through the ip instances on the interface, enqueue\r
1279 // the packet if filter passed. Save the original cast type,\r
1280 // and pass the local cast type to the IP children on the\r
1281 // interface. The global cast type will be restored later.\r
1282 //\r
1283 SavedType = Info->CastType;\r
1284 Info->CastType = LocalType;\r
1285\r
1286 Enqueued = 0;\r
1287\r
1288 NET_LIST_FOR_EACH (Entry, &IpIf->IpInstances) {\r
1289 IpInstance = NET_LIST_USER_STRUCT (Entry, IP4_PROTOCOL, AddrLink);\r
1290 NET_CHECK_SIGNATURE (IpInstance, IP4_PROTOCOL_SIGNATURE);\r
1291\r
1292 if (Ip4InstanceEnquePacket (IpInstance, Head, Packet) == EFI_SUCCESS) {\r
1293 Enqueued++;\r
1294 }\r
1295 }\r
1296\r
1297 Info->CastType = SavedType;\r
1298 return Enqueued;\r
1299}\r
1300\r
1301\r
1302/**\r
1303 Deliver the packet for each IP4 child on the interface.\r
1304\r
3e8c18da 1305 @param[in] IpSb The IP4 service instance that received the packet\r
1306 @param[in] IpIf The IP4 interface to deliver the packet.\r
772db4bb 1307\r
1308 @retval EFI_SUCCESS It always returns EFI_SUCCESS now\r
1309\r
1310**/\r
1311EFI_STATUS\r
1312Ip4InterfaceDeliverPacket (\r
1313 IN IP4_SERVICE *IpSb,\r
1314 IN IP4_INTERFACE *IpIf\r
1315 )\r
1316{\r
1317 IP4_PROTOCOL *Ip4Instance;\r
e48e37fc 1318 LIST_ENTRY *Entry;\r
772db4bb 1319\r
1320 NET_LIST_FOR_EACH (Entry, &IpIf->IpInstances) {\r
1321 Ip4Instance = NET_LIST_USER_STRUCT (Entry, IP4_PROTOCOL, AddrLink);\r
1322 Ip4InstanceDeliverPacket (Ip4Instance);\r
1323 }\r
1324\r
1325 return EFI_SUCCESS;\r
1326}\r
1327\r
1328\r
1329/**\r
1330 Demultiple the packet. the packet delivery is processed in two\r
1331 passes. The first pass will enque a shared copy of the packet\r
1332 to each IP4 child that accepts the packet. The second pass will\r
1333 deliver a non-shared copy of the packet to each IP4 child that\r
1334 has pending receive requests. Data is copied if more than one\r
96e1079f 1335 child wants to consume the packet because each IP child needs\r
772db4bb 1336 its own copy of the packet to make changes.\r
1337\r
3e8c18da 1338 @param[in] IpSb The IP4 service instance that received the packet\r
1339 @param[in] Head The header of the received packet\r
1340 @param[in] Packet The data of the received packet\r
772db4bb 1341\r
1342 @retval EFI_NOT_FOUND No IP child accepts the packet\r
1343 @retval EFI_SUCCESS The packet is enqueued or delivered to some IP\r
1344 children.\r
1345\r
1346**/\r
1347EFI_STATUS\r
1348Ip4Demultiplex (\r
1349 IN IP4_SERVICE *IpSb,\r
1350 IN IP4_HEAD *Head,\r
1351 IN NET_BUF *Packet\r
1352 )\r
1353{\r
e48e37fc 1354 LIST_ENTRY *Entry;\r
772db4bb 1355 IP4_INTERFACE *IpIf;\r
1356 INTN Enqueued;\r
1357\r
1358 //\r
1359 // Two pass delivery: first, enque a shared copy of the packet\r
1360 // to each instance that accept the packet.\r
1361 //\r
1362 Enqueued = 0;\r
1363\r
1364 NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) {\r
1365 IpIf = NET_LIST_USER_STRUCT (Entry, IP4_INTERFACE, Link);\r
1366\r
1367 if (IpIf->Configured) {\r
1368 Enqueued += Ip4InterfaceEnquePacket (IpSb, Head, Packet, IpIf);\r
1369 }\r
1370 }\r
1371\r
1372 //\r
1373 // Second: deliver a duplicate of the packet to each instance.\r
1374 // Release the local reference first, so that the last instance\r
1375 // getting the packet will not copy the data.\r
1376 //\r
1377 NetbufFree (Packet);\r
1378\r
1379 if (Enqueued == 0) {\r
1380 return EFI_NOT_FOUND;\r
1381 }\r
1382\r
1383 NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) {\r
1384 IpIf = NET_LIST_USER_STRUCT (Entry, IP4_INTERFACE, Link);\r
1385\r
1386 if (IpIf->Configured) {\r
1387 Ip4InterfaceDeliverPacket (IpSb, IpIf);\r
1388 }\r
1389 }\r
1390\r
1391 return EFI_SUCCESS;\r
1392}\r
1393\r
1394\r
1395/**\r
1396 Timeout the fragment and enqueued packets.\r
1397\r
3e8c18da 1398 @param[in] IpSb The IP4 service instance to timeout\r
772db4bb 1399\r
1400**/\r
1401VOID\r
1402Ip4PacketTimerTicking (\r
1403 IN IP4_SERVICE *IpSb\r
1404 )\r
1405{\r
e48e37fc 1406 LIST_ENTRY *InstanceEntry;\r
1407 LIST_ENTRY *Entry;\r
1408 LIST_ENTRY *Next;\r
772db4bb 1409 IP4_PROTOCOL *IpInstance;\r
1410 IP4_ASSEMBLE_ENTRY *Assemble;\r
1411 NET_BUF *Packet;\r
1412 IP4_CLIP_INFO *Info;\r
1413 UINT32 Index;\r
1414\r
1415 //\r
1416 // First, time out the fragments. The packet's life is counting down\r
1417 // once the first-arrived fragment was received.\r
1418 //\r
1419 for (Index = 0; Index < IP4_ASSEMLE_HASH_SIZE; Index++) {\r
1420 NET_LIST_FOR_EACH_SAFE (Entry, Next, &IpSb->Assemble.Bucket[Index]) {\r
1421 Assemble = NET_LIST_USER_STRUCT (Entry, IP4_ASSEMBLE_ENTRY, Link);\r
1422\r
1423 if ((Assemble->Life > 0) && (--Assemble->Life == 0)) {\r
e48e37fc 1424 RemoveEntryList (Entry);\r
772db4bb 1425 Ip4FreeAssembleEntry (Assemble);\r
1426 }\r
1427 }\r
1428 }\r
1429\r
1430 NET_LIST_FOR_EACH (InstanceEntry, &IpSb->Children) {\r
1431 IpInstance = NET_LIST_USER_STRUCT (InstanceEntry, IP4_PROTOCOL, Link);\r
1432\r
1433 //\r
1434 // Second, time out the assembled packets enqueued on each IP child.\r
1435 //\r
1436 NET_LIST_FOR_EACH_SAFE (Entry, Next, &IpInstance->Received) {\r
1437 Packet = NET_LIST_USER_STRUCT (Entry, NET_BUF, List);\r
1438 Info = IP4_GET_CLIP_INFO (Packet);\r
1439\r
1440 if ((Info->Life > 0) && (--Info->Life == 0)) {\r
e48e37fc 1441 RemoveEntryList (Entry);\r
772db4bb 1442 NetbufFree (Packet);\r
1443 }\r
1444 }\r
1445\r
1446 //\r
1447 // Third: time out the transmitted packets.\r
1448 //\r
1449 NetMapIterate (&IpInstance->TxTokens, Ip4SentPacketTicking, NULL);\r
1450 }\r
1451}\r