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