]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Network/Ip4Dxe/Ip4Input.c
1. Sync the latest network stack. Add NetLibCreateIPv4DPathNode () in netlib library.
[mirror_edk2.git] / MdeModulePkg / Universal / Network / Ip4Dxe / Ip4Input.c
CommitLineData
772db4bb 1/** @file\r
2\r
3Copyright (c) 2005 - 2006, Intel Corporation\r
4All rights reserved. This program and the accompanying materials\r
5are licensed and made available under the terms and conditions of the BSD License\r
6which accompanies this distribution. The full text of the license may be found at\r
7http://opensource.org/licenses/bsd-license.php\r
8\r
9THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
10WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
11\r
12Module Name:\r
13\r
14 Ip4Input.c\r
15\r
16Abstract:\r
17\r
18 IP4 input process.\r
19\r
20\r
21**/\r
22\r
23#include "Ip4Impl.h"\r
24\r
25\r
26/**\r
27 Create a empty assemble entry for the packet identified by\r
28 (Dst, Src, Id, Protocol). The default life for the packet is\r
29 120 seconds.\r
30\r
31 @param Dst The destination address\r
32 @param Src The source address\r
33 @param Id The ID field in IP header\r
34 @param Protocol The protocol field in IP header\r
35\r
36 @return NULL if failed to allocate memory for the entry, otherwise\r
37 @return the point to just created reassemble entry.\r
38\r
39**/\r
40STATIC\r
41IP4_ASSEMBLE_ENTRY *\r
42Ip4CreateAssembleEntry (\r
43 IN IP4_ADDR Dst,\r
44 IN IP4_ADDR Src,\r
45 IN UINT16 Id,\r
46 IN UINT8 Protocol\r
47 )\r
48{\r
49\r
50 IP4_ASSEMBLE_ENTRY *Assemble;\r
51\r
52 Assemble = NetAllocatePool (sizeof (IP4_ASSEMBLE_ENTRY));\r
53\r
54 if (Assemble == NULL) {\r
55 return NULL;\r
56 }\r
57\r
58 NetListInit (&Assemble->Link);\r
59 NetListInit (&Assemble->Fragments);\r
60\r
61 Assemble->Dst = Dst;\r
62 Assemble->Src = Src;\r
63 Assemble->Id = Id;\r
64 Assemble->Protocol = Protocol;\r
65 Assemble->TotalLen = 0;\r
66 Assemble->CurLen = 0;\r
67 Assemble->Head = NULL;\r
68 Assemble->Info = NULL;\r
69 Assemble->Life = IP4_FRAGMENT_LIFE;\r
70\r
71 return Assemble;\r
72}\r
73\r
74\r
75/**\r
76 Release all the fragments of a packet, then free the assemble entry\r
77\r
78 @param Assemble The assemble entry to free\r
79\r
80 @return None\r
81\r
82**/\r
83STATIC\r
84VOID\r
85Ip4FreeAssembleEntry (\r
86 IN IP4_ASSEMBLE_ENTRY *Assemble\r
87 )\r
88{\r
89 NET_LIST_ENTRY *Entry;\r
90 NET_LIST_ENTRY *Next;\r
91 NET_BUF *Fragment;\r
92\r
93 NET_LIST_FOR_EACH_SAFE (Entry, Next, &Assemble->Fragments) {\r
94 Fragment = NET_LIST_USER_STRUCT (Entry, NET_BUF, List);\r
95\r
96 NetListRemoveEntry (Entry);\r
97 NetbufFree (Fragment);\r
98 }\r
99\r
100 NetFreePool (Assemble);\r
101}\r
102\r
103\r
104/**\r
105 Initialize an already allocated assemble table. This is generally\r
106 the assemble table embedded in the IP4 service instance.\r
107\r
108 @param Table The assemble table to initialize.\r
109\r
110 @return NONE\r
111\r
112**/\r
113VOID\r
114Ip4InitAssembleTable (\r
115 IN IP4_ASSEMBLE_TABLE *Table\r
116 )\r
117{\r
118 UINT32 Index;\r
119\r
120 for (Index = 0; Index < IP4_ASSEMLE_HASH_SIZE; Index++) {\r
121 NetListInit (&Table->Bucket[Index]);\r
122 }\r
123}\r
124\r
125\r
126/**\r
127 Clean up the assemble table: remove all the fragments\r
128 and assemble entries.\r
129\r
130 @param Table The assemble table to clean up\r
131\r
132 @return None\r
133\r
134**/\r
135VOID\r
136Ip4CleanAssembleTable (\r
137 IN IP4_ASSEMBLE_TABLE *Table\r
138 )\r
139{\r
140 NET_LIST_ENTRY *Entry;\r
141 NET_LIST_ENTRY *Next;\r
142 IP4_ASSEMBLE_ENTRY *Assemble;\r
143 UINT32 Index;\r
144\r
145 for (Index = 0; Index < IP4_ASSEMLE_HASH_SIZE; Index++) {\r
146 NET_LIST_FOR_EACH_SAFE (Entry, Next, &Table->Bucket[Index]) {\r
147 Assemble = NET_LIST_USER_STRUCT (Entry, IP4_ASSEMBLE_ENTRY, Link);\r
148\r
149 NetListRemoveEntry (Entry);\r
150 Ip4FreeAssembleEntry (Assemble);\r
151 }\r
152 }\r
153}\r
154\r
155\r
156/**\r
157 Trim the packet to fit in [Start, End), and update the per\r
158 packet information.\r
159\r
160 @param Packet Packet to trim\r
161 @param Start The sequence of the first byte to fit in\r
162 @param End One beyond the sequence of last byte to fit in.\r
163\r
164 @return None\r
165\r
166**/\r
167STATIC\r
168VOID\r
169Ip4TrimPacket (\r
170 IN NET_BUF *Packet,\r
171 IN INTN Start,\r
172 IN INTN End\r
173 )\r
174{\r
175 IP4_CLIP_INFO *Info;\r
176 INTN Len;\r
177\r
178 Info = IP4_GET_CLIP_INFO (Packet);\r
179\r
180 ASSERT (Info->Start + Info->Length == Info->End);\r
181 ASSERT ((Info->Start < End) && (Start < Info->End));\r
182\r
183 if (Info->Start < Start) {\r
184 Len = Start - Info->Start;\r
185\r
186 NetbufTrim (Packet, (UINT32) Len, NET_BUF_HEAD);\r
187 Info->Start = Start;\r
188 Info->Length -= Len;\r
189 }\r
190\r
191 if (End < Info->End) {\r
192 Len = End - Info->End;\r
193\r
194 NetbufTrim (Packet, (UINT32) Len, NET_BUF_TAIL);\r
195 Info->End = End;\r
196 Info->Length -= Len;\r
197 }\r
198}\r
199\r
200\r
201/**\r
202 Release all the fragments of the packet. This is the callback for\r
203 the assembled packet's OnFree. It will free the assemble entry,\r
204 which in turn will free all the fragments of the packet.\r
205\r
206 @param Arg The assemble entry to free\r
207\r
208 @return None\r
209\r
210**/\r
211STATIC\r
212VOID\r
213Ip4OnFreeFragments (\r
214 IN VOID *Arg\r
215 )\r
216{\r
217 Ip4FreeAssembleEntry ((IP4_ASSEMBLE_ENTRY *) Arg);\r
218}\r
219\r
220\r
221/**\r
222 Reassemble the IP fragments. If all the fragments of the packet\r
223 have been received, it will wrap the packet in a net buffer then\r
224 return it to caller. If the packet can't be assembled, NULL is\r
225 return.\r
226\r
227 @param Table The assemble table used.\r
228 @param Packet The fragment to assemble\r
229\r
230 @return NULL if the packet can't be reassemble. The point to just assembled\r
231 @return packet if all the fragments of the packet have arrived.\r
232\r
233**/\r
234STATIC\r
235NET_BUF *\r
236Ip4Reassemble (\r
237 IN IP4_ASSEMBLE_TABLE *Table,\r
238 IN NET_BUF *Packet\r
239 )\r
240{\r
241 IP4_HEAD *IpHead;\r
242 IP4_CLIP_INFO *This;\r
243 IP4_CLIP_INFO *Node;\r
244 IP4_ASSEMBLE_ENTRY *Assemble;\r
245 NET_LIST_ENTRY *Head;\r
246 NET_LIST_ENTRY *Prev;\r
247 NET_LIST_ENTRY *Cur;\r
248 NET_BUF *Fragment;\r
249 NET_BUF *NewPacket;\r
250 INTN Index;\r
251\r
252 IpHead = Packet->Ip;\r
253 This = IP4_GET_CLIP_INFO (Packet);\r
254\r
255 ASSERT (IpHead != NULL);\r
256\r
257 //\r
258 // First: find the related assemble entry\r
259 //\r
260 Assemble = NULL;\r
261 Index = IP4_ASSEMBLE_HASH (IpHead->Dst, IpHead->Src, IpHead->Id, IpHead->Protocol);\r
262\r
263 NET_LIST_FOR_EACH (Cur, &Table->Bucket[Index]) {\r
264 Assemble = NET_LIST_USER_STRUCT (Cur, IP4_ASSEMBLE_ENTRY, Link);\r
265\r
266 if ((Assemble->Dst == IpHead->Dst) && (Assemble->Src == IpHead->Src) &&\r
267 (Assemble->Id == IpHead->Id) && (Assemble->Protocol == IpHead->Protocol)) {\r
268 break;\r
269 }\r
270 }\r
271\r
272 //\r
273 // Create a new assemble entry if no assemble entry is related to this packet\r
274 //\r
275 if (Cur == &Table->Bucket[Index]) {\r
276 Assemble = Ip4CreateAssembleEntry (\r
277 IpHead->Dst,\r
278 IpHead->Src,\r
279 IpHead->Id,\r
280 IpHead->Protocol\r
281 );\r
282\r
283 if (Assemble == NULL) {\r
284 goto DROP;\r
285 }\r
286\r
287 NetListInsertHead (&Table->Bucket[Index], &Assemble->Link);\r
288 }\r
289\r
290 //\r
291 // Find the point to insert the packet: before the first\r
292 // fragment with THIS.Start < CUR.Start. the previous one\r
293 // has PREV.Start <= THIS.Start < CUR.Start.\r
294 //\r
295 Head = &Assemble->Fragments;\r
296\r
297 NET_LIST_FOR_EACH (Cur, Head) {\r
298 Fragment = NET_LIST_USER_STRUCT (Cur, NET_BUF, List);\r
299\r
300 if (This->Start < IP4_GET_CLIP_INFO (Fragment)->Start) {\r
301 break;\r
302 }\r
303 }\r
304\r
305 //\r
306 // Check whether the current fragment overlaps with the previous one.\r
307 // It holds that: PREV.Start <= THIS.Start < THIS.End. Only need to\r
308 // check whether THIS.Start < PREV.End for overlap. If two fragments\r
309 // overlaps, trim the overlapped part off THIS fragment.\r
310 //\r
311 if ((Prev = Cur->ForwardLink) != Head) {\r
312 Fragment = NET_LIST_USER_STRUCT (Prev, NET_BUF, List);\r
313 Node = IP4_GET_CLIP_INFO (Fragment);\r
314\r
315 if (This->Start < Node->End) {\r
316 if (This->End <= Node->End) {\r
317 NetbufFree (Packet);\r
318 return NULL;\r
319 }\r
320\r
321 Ip4TrimPacket (Packet, Node->End, This->End);\r
322 }\r
323 }\r
324\r
325 //\r
326 // Insert the fragment into the packet. The fragment may be removed\r
327 // from the list by the following checks.\r
328 //\r
329 NetListInsertBefore (Cur, &Packet->List);\r
330\r
331 //\r
332 // Check the packets after the insert point. It holds that:\r
333 // THIS.Start <= NODE.Start < NODE.End. The equality holds\r
334 // if PREV and NEXT are continuous. THIS fragment may fill\r
335 // several holes. Remove the completely overlapped fragments\r
336 //\r
337 while (Cur != Head) {\r
338 Fragment = NET_LIST_USER_STRUCT (Cur, NET_BUF, List);\r
339 Node = IP4_GET_CLIP_INFO (Fragment);\r
340\r
341 //\r
342 // Remove fragments completely overlapped by this fragment\r
343 //\r
344 if (Node->End <= This->End) {\r
345 Cur = Cur->ForwardLink;\r
346\r
347 NetListRemoveEntry (&Fragment->List);\r
348 Assemble->CurLen -= Node->Length;\r
349\r
350 NetbufFree (Fragment);\r
351 continue;\r
352 }\r
353\r
354 //\r
355 // The conditions are: THIS.Start <= NODE.Start, and THIS.End <\r
356 // NODE.End. Two fragments overlaps if NODE.Start < THIS.End.\r
357 // If two fragments start at the same offset, remove THIS fragment\r
358 // because ((THIS.Start == NODE.Start) && (THIS.End < NODE.End)).\r
359 //\r
360 if (Node->Start < This->End) {\r
361 if (This->Start == Node->Start) {\r
362 NetListRemoveEntry (&Packet->List);\r
363 goto DROP;\r
364 }\r
365\r
366 Ip4TrimPacket (Packet, This->Start, Node->Start);\r
367 }\r
368\r
369 break;\r
370 }\r
371\r
372 //\r
373 // Update the assemble info: increase the current length. If it is\r
374 // the frist fragment, update the packet's IP head and per packet\r
375 // info. If it is the last fragment, update the total length.\r
376 //\r
377 Assemble->CurLen += This->Length;\r
378\r
379 if (This->Start == 0) {\r
380 //\r
381 // Once the first fragment is enqueued, it can't be removed\r
382 // from the fragment list. So, Assemble->Head always point\r
383 // to valid memory area.\r
384 //\r
385 ASSERT (Assemble->Head == NULL);\r
386\r
387 Assemble->Head = IpHead;\r
388 Assemble->Info = IP4_GET_CLIP_INFO (Packet);\r
389 }\r
390\r
391 //\r
392 // Don't update the length more than once.\r
393 //\r
394 if (IP4_LAST_FRAGMENT (IpHead->Fragment) && (Assemble->TotalLen == 0)) {\r
395 Assemble->TotalLen = This->End;\r
396 }\r
397\r
398 //\r
399 // Deliver the whole packet if all the fragments received.\r
400 // All fragments received if:\r
401 // 1. received the last one, so, the totoal length is know\r
402 // 2. received all the data. If the last fragment on the\r
403 // queue ends at the total length, all data is received.\r
404 //\r
405 if ((Assemble->TotalLen != 0) && (Assemble->CurLen >= Assemble->TotalLen)) {\r
406\r
407 NetListRemoveEntry (&Assemble->Link);\r
408\r
409 //\r
410 // If the packet is properly formated, the last fragment's End\r
411 // equals to the packet's total length. Otherwise, the packet\r
412 // is a fake, drop it now.\r
413 //\r
414 Fragment = NET_LIST_USER_STRUCT (Head->BackLink, NET_BUF, List);\r
415\r
416 if (IP4_GET_CLIP_INFO (Fragment)->End != Assemble->TotalLen) {\r
417 Ip4FreeAssembleEntry (Assemble);\r
418 return NULL;\r
419 }\r
420\r
421 //\r
422 // Wrap the packet in a net buffer then deliver it up\r
423 //\r
424 NewPacket = NetbufFromBufList (\r
425 &Assemble->Fragments,\r
426 0,\r
427 0,\r
428 Ip4OnFreeFragments,\r
429 Assemble\r
430 );\r
431\r
432 if (NewPacket == NULL) {\r
433 Ip4FreeAssembleEntry (Assemble);\r
434 return NULL;\r
435 }\r
436\r
437 NewPacket->Ip = Assemble->Head;\r
687a2e5f 438 CopyMem (IP4_GET_CLIP_INFO (NewPacket), Assemble->Info, sizeof (*IP4_GET_CLIP_INFO (NewPacket)));\r
772db4bb 439 return NewPacket;\r
440 }\r
441\r
442 return NULL;\r
443\r
444DROP:\r
445 NetbufFree (Packet);\r
446 return NULL;\r
447}\r
448\r
449\r
450/**\r
451 The IP4 input routine. It is called by the IP4_INTERFACE when a\r
452 IP4 fragment is received from MNP.\r
453\r
454 @param Ip4Instance The IP4 child that request the receive, most like\r
455 it is NULL.\r
456 @param Packet The IP4 packet received.\r
457 @param IoStatus The return status of receive request.\r
458 @param Flag The link layer flag for the packet received, such\r
459 as multicast.\r
460 @param Context The IP4 service instance that own the MNP.\r
461\r
462 @return None\r
463\r
464**/\r
465VOID\r
466Ip4AccpetFrame (\r
467 IN IP4_PROTOCOL *Ip4Instance,\r
468 IN NET_BUF *Packet,\r
469 IN EFI_STATUS IoStatus,\r
470 IN UINT32 Flag,\r
471 IN VOID *Context\r
472 )\r
473{\r
474 IP4_SERVICE *IpSb;\r
475 IP4_CLIP_INFO *Info;\r
476 IP4_HEAD *Head;\r
477 UINT32 HeadLen;\r
478 UINT32 OptionLen;\r
479 UINT32 TotalLen;\r
480 UINT16 Checksum;\r
481\r
482 IpSb = (IP4_SERVICE *) Context;\r
483\r
484 if (EFI_ERROR (IoStatus) || (IpSb->State == IP4_SERVICE_DESTORY)) {\r
485 goto DROP;\r
486 }\r
487\r
488 //\r
489 // Check that the IP4 header is correctly formated\r
490 //\r
491 if (Packet->TotalSize < IP4_MIN_HEADLEN) {\r
492 goto RESTART;\r
493 }\r
494\r
495 Head = (IP4_HEAD *) NetbufGetByte (Packet, 0, NULL);\r
496 HeadLen = (Head->HeadLen << 2);\r
497 TotalLen = NTOHS (Head->TotalLen);\r
498\r
499 //\r
500 // Mnp may deliver frame trailer sequence up, trim it off.\r
501 //\r
502 if (TotalLen < Packet->TotalSize) {\r
503 NetbufTrim (Packet, Packet->TotalSize - TotalLen, FALSE);\r
504 }\r
505\r
506 if ((Head->Ver != 4) || (HeadLen < IP4_MIN_HEADLEN) ||\r
507 (TotalLen < HeadLen) || (TotalLen != Packet->TotalSize)) {\r
508 goto RESTART;\r
509 }\r
510\r
511 //\r
512 // Some OS may send IP packets without checksum.\r
513 //\r
687a2e5f 514 Checksum = (UINT16) (~NetblockChecksum ((UINT8 *) Head, HeadLen));\r
772db4bb 515\r
516 if ((Head->Checksum != 0) && (Checksum != 0)) {\r
517 goto RESTART;\r
518 }\r
519\r
520 //\r
521 // Convert the IP header to host byte order, then get the per packet info.\r
522 //\r
523 Packet->Ip = Ip4NtohHead (Head);\r
524\r
525 Info = IP4_GET_CLIP_INFO (Packet);\r
526 Info->LinkFlag = Flag;\r
527 Info->CastType = Ip4GetHostCast (IpSb, Head->Dst, Head->Src);\r
528 Info->Start = (Head->Fragment & IP4_HEAD_OFFSET_MASK) << 3;\r
529 Info->Length = Head->TotalLen - HeadLen;\r
530 Info->End = Info->Start + Info->Length;\r
531 Info->Status = EFI_SUCCESS;\r
532\r
533 //\r
534 // The packet is destinated to us if the CastType is non-zero.\r
535 //\r
536 if ((Info->CastType == 0) || (Info->End > IP4_MAX_PACKET_SIZE)) {\r
537 goto RESTART;\r
538 }\r
539\r
540 //\r
541 // Validate the options. Don't call the Ip4OptionIsValid if\r
542 // there is no option to save some CPU process.\r
543 //\r
544 OptionLen = HeadLen - IP4_MIN_HEADLEN;\r
545\r
546 if ((OptionLen > 0) && !Ip4OptionIsValid ((UINT8 *) (Head + 1), OptionLen, TRUE)) {\r
547 goto RESTART;\r
548 }\r
549\r
550 //\r
551 // Trim the head off, after this point, the packet is headless.\r
552 // and Packet->TotalLen == Info->Length.\r
553 //\r
554 NetbufTrim (Packet, HeadLen, TRUE);\r
555\r
556 //\r
557 // Reassemble the packet if this is a fragment. The packet is a\r
558 // fragment if its head has MF (more fragment) set, or it starts\r
559 // at non-zero byte.\r
560 //\r
561 if ((Head->Fragment & IP4_HEAD_MF_MASK) || (Info->Start != 0)) {\r
562 //\r
563 // Drop the fragment if DF is set but it is fragmented. Gateway\r
564 // need to send a type 4 destination unreache ICMP message here.\r
565 //\r
566 if (Head->Fragment & IP4_HEAD_DF_MASK) {\r
567 goto RESTART;\r
568 }\r
569\r
570 //\r
571 // The length of all but the last fragments is in the unit of 8 bytes.\r
572 //\r
573 if ((Head->Fragment & IP4_HEAD_MF_MASK) && (Info->Length % 8 != 0)) {\r
574 goto RESTART;\r
575 }\r
576\r
577 Packet = Ip4Reassemble (&IpSb->Assemble, Packet);\r
578\r
579 //\r
580 // Packet assembly isn't complete, start receive more packet.\r
581 //\r
582 if (Packet == NULL) {\r
583 goto RESTART;\r
584 }\r
585 }\r
586\r
587 //\r
588 // Packet may have been changed. Head, HeadLen, TotalLen, and\r
589 // info must be reloaded bofore use. The ownership of the packet\r
590 // is transfered to the packet process logic.\r
591 //\r
592 Head = Packet->Ip;\r
593 IP4_GET_CLIP_INFO (Packet)->Status = EFI_SUCCESS;\r
594\r
595 switch (Head->Protocol) {\r
596 case IP4_PROTO_ICMP:\r
597 Ip4IcmpHandle (IpSb, Head, Packet);\r
598 break;\r
599\r
600 case IP4_PROTO_IGMP:\r
601 Ip4IgmpHandle (IpSb, Head, Packet);\r
602 break;\r
603\r
604 default:\r
605 Ip4Demultiplex (IpSb, Head, Packet);\r
606 }\r
607\r
608 Packet = NULL;\r
609\r
610RESTART:\r
611 Ip4ReceiveFrame (IpSb->DefaultInterface, NULL, Ip4AccpetFrame, IpSb);\r
612\r
613DROP:\r
614 if (Packet != NULL) {\r
615 NetbufFree (Packet);\r
616 }\r
617\r
618 return ;\r
619}\r
620\r
621\r
622/**\r
623 Check whether this IP child accepts the packet.\r
624\r
625 @param IpInstance The IP child to check\r
626 @param Head The IP header of the packet\r
627 @param Packet The data of the packet\r
628\r
629 @return TRUE if the child wants to receive the packet, otherwise return FALSE.\r
630\r
631**/\r
632BOOLEAN\r
633Ip4InstanceFrameAcceptable (\r
634 IN IP4_PROTOCOL *IpInstance,\r
635 IN IP4_HEAD *Head,\r
636 IN NET_BUF *Packet\r
637 )\r
638{\r
639 IP4_ICMP_ERROR_HEAD Icmp;\r
640 EFI_IP4_CONFIG_DATA *Config;\r
641 IP4_CLIP_INFO *Info;\r
642 UINT16 Proto;\r
643 UINT32 Index;\r
644\r
645 Config = &IpInstance->ConfigData;\r
646\r
647 //\r
648 // Dirty trick for the Tiano UEFI network stack implmentation. If\r
649 // ReceiveTimeout == -1, the receive of the packet for this instance\r
650 // is disabled. The UEFI spec don't have such captibility. We add\r
651 // this to improve the performance because IP will make a copy of\r
652 // the received packet for each accepting instance. Some IP instances\r
653 // used by UDP/TCP only send packets, they don't wants to receive.\r
654 //\r
655 if (Config->ReceiveTimeout == (UINT32)(-1)) {\r
656 return FALSE;\r
657 }\r
658\r
659 if (Config->AcceptPromiscuous) {\r
660 return TRUE;\r
661 }\r
662\r
663 //\r
664 // Use protocol from the IP header embedded in the ICMP error\r
665 // message to filter, instead of ICMP itself. ICMP handle will\r
666 // can Ip4Demultiplex to deliver ICMP errors.\r
667 //\r
668 Proto = Head->Protocol;\r
669\r
670 if (Proto == IP4_PROTO_ICMP) {\r
671 NetbufCopy (Packet, 0, sizeof (Icmp.Head), (UINT8 *) &Icmp.Head);\r
672\r
673 if (mIcmpClass[Icmp.Head.Type].IcmpClass == ICMP_ERROR_MESSAGE) {\r
674 if (!Config->AcceptIcmpErrors) {\r
675 return FALSE;\r
676 }\r
677\r
678 NetbufCopy (Packet, 0, sizeof (Icmp), (UINT8 *) &Icmp);\r
679 Proto = Icmp.IpHead.Protocol;\r
680 }\r
681 }\r
682\r
683 //\r
684 // Match the protocol\r
685 //\r
686 if (!Config->AcceptAnyProtocol && (Proto != Config->DefaultProtocol)) {\r
687 return FALSE;\r
688 }\r
689\r
690 //\r
691 // Check for broadcast, the caller has computed the packet's\r
692 // cast type for this child's interface.\r
693 //\r
694 Info = IP4_GET_CLIP_INFO (Packet);\r
695\r
696 if (IP4_IS_BROADCAST (Info->CastType)) {\r
697 return Config->AcceptBroadcast;\r
698 }\r
699\r
700 //\r
701 // If it is a multicast packet, check whether we are in the group.\r
702 //\r
703 if (Info->CastType == IP4_MULTICAST) {\r
704 //\r
705 // Receive the multicast if the instance wants to receive all packets.\r
706 //\r
707 if (!IpInstance->ConfigData.UseDefaultAddress && (IpInstance->Interface->Ip == 0)) {\r
708 return TRUE;\r
709 }\r
710\r
711 for (Index = 0; Index < IpInstance->GroupCount; Index++) {\r
712 if (IpInstance->Groups[Index] == HTONL (Head->Dst)) {\r
713 break;\r
714 }\r
715 }\r
716\r
717 return (BOOLEAN)(Index < IpInstance->GroupCount);\r
718 }\r
719\r
720 return TRUE;\r
721}\r
722\r
723\r
724/**\r
725 Enqueue a shared copy of the packet to the IP4 child if the\r
726 packet is acceptable to it. Here the data of the packet is\r
727 shared, but the net buffer isn't.\r
728\r
729 @param IpInstance The IP4 child to enqueue the packet to\r
730 @param Head The IP header of the received packet\r
731 @param Packet The data of the received packet\r
732\r
733 @retval EFI_NOT_STARTED The IP child hasn't been configured.\r
734 @retval EFI_INVALID_PARAMETER The child doesn't want to receive the packet\r
735 @retval EFI_OUT_OF_RESOURCES Failed to allocate some resource\r
736 @retval EFI_SUCCESS A shared copy the packet is enqueued to the child.\r
737\r
738**/\r
739EFI_STATUS\r
740Ip4InstanceEnquePacket (\r
741 IN IP4_PROTOCOL *IpInstance,\r
742 IN IP4_HEAD *Head,\r
743 IN NET_BUF *Packet\r
744 )\r
745{\r
746 IP4_CLIP_INFO *Info;\r
747 NET_BUF *Clone;\r
748\r
749 //\r
750 // Check whether the packet is acceptable to this instance.\r
751 //\r
752 if (IpInstance->State != IP4_STATE_CONFIGED) {\r
753 return EFI_NOT_STARTED;\r
754 }\r
755\r
756 if (!Ip4InstanceFrameAcceptable (IpInstance, Head, Packet)) {\r
757 return EFI_INVALID_PARAMETER;\r
758 }\r
759\r
760 //\r
761 // Enque a shared copy of the packet.\r
762 //\r
763 Clone = NetbufClone (Packet);\r
764\r
765 if (Clone == NULL) {\r
766 return EFI_OUT_OF_RESOURCES;\r
767 }\r
768\r
769 //\r
770 // Set the receive time out for the assembled packet. If it expires,\r
771 // packet will be removed from the queue.\r
772 //\r
773 Info = IP4_GET_CLIP_INFO (Clone);\r
774 Info->Life = IP4_US_TO_SEC (IpInstance->ConfigData.ReceiveTimeout);\r
775\r
776 NetListInsertTail (&IpInstance->Received, &Clone->List);\r
777 return EFI_SUCCESS;\r
778}\r
779\r
780\r
781/**\r
782 The signal handle of IP4's recycle event. It is called back\r
783 when the upper layer release the packet.\r
784\r
785 @param Event The IP4's recycle event.\r
786 @param Context The context of the handle, which is a\r
787 IP4_RXDATA_WRAP\r
788\r
789 @return None\r
790\r
791**/\r
792STATIC\r
793VOID\r
794EFIAPI\r
795Ip4OnRecyclePacket (\r
796 IN EFI_EVENT Event,\r
797 IN VOID *Context\r
798 )\r
799{\r
800 IP4_RXDATA_WRAP *Wrap;\r
801\r
802 Wrap = (IP4_RXDATA_WRAP *) Context;\r
803\r
804 NET_TRYLOCK (&Wrap->IpInstance->RecycleLock);\r
805 NetListRemoveEntry (&Wrap->Link);\r
806 NET_UNLOCK (&Wrap->IpInstance->RecycleLock);\r
807\r
808 ASSERT (!NET_BUF_SHARED (Wrap->Packet));\r
809 NetbufFree (Wrap->Packet);\r
810\r
811 gBS->CloseEvent (Wrap->RxData.RecycleSignal);\r
812 NetFreePool (Wrap);\r
813}\r
814\r
815\r
816/**\r
817 Wrap the received packet to a IP4_RXDATA_WRAP, which will be\r
818 delivered to the upper layer. Each IP4 child that accepts the\r
819 packet will get a not-shared copy of the packet which is wrapped\r
820 in the IP4_RXDATA_WRAP. The IP4_RXDATA_WRAP->RxData is passed\r
821 to the upper layer. Upper layer will signal the recycle event in\r
822 it when it is done with the packet.\r
823\r
824 @param IpInstance The IP4 child to receive the packet\r
825 @param Packet The packet to deliver up.\r
826\r
827 @return NULL if failed to wrap the packet, otherwise the wrapper.\r
828\r
829**/\r
830IP4_RXDATA_WRAP *\r
831Ip4WrapRxData (\r
832 IN IP4_PROTOCOL *IpInstance,\r
833 IN NET_BUF *Packet\r
834 )\r
835{\r
836 IP4_RXDATA_WRAP *Wrap;\r
837 EFI_IP4_RECEIVE_DATA *RxData;\r
838 EFI_STATUS Status;\r
839\r
840 Wrap = NetAllocatePool (IP4_RXDATA_WRAP_SIZE (Packet->BlockOpNum));\r
841\r
842 if (Wrap == NULL) {\r
843 return NULL;\r
844 }\r
845\r
846 NetListInit (&Wrap->Link);\r
847\r
848 Wrap->IpInstance = IpInstance;\r
849 Wrap->Packet = Packet;\r
850 RxData = &Wrap->RxData;\r
851\r
852 NetZeroMem (&RxData->TimeStamp, sizeof (EFI_TIME));\r
853\r
854 Status = gBS->CreateEvent (\r
855 EVT_NOTIFY_SIGNAL,\r
856 NET_TPL_RECYCLE,\r
857 Ip4OnRecyclePacket,\r
858 Wrap,\r
859 &RxData->RecycleSignal\r
860 );\r
861\r
862 if (EFI_ERROR (Status)) {\r
863 NetFreePool (Wrap);\r
864 return NULL;\r
865 }\r
866\r
867 ASSERT (Packet->Ip != NULL);\r
868\r
869 //\r
870 // The application expects a network byte order header.\r
871 //\r
872 RxData->HeaderLength = (Packet->Ip->HeadLen << 2);\r
873 RxData->Header = (EFI_IP4_HEADER *) Ip4NtohHead (Packet->Ip);\r
874\r
875 RxData->OptionsLength = RxData->HeaderLength - IP4_MIN_HEADLEN;\r
876 RxData->Options = NULL;\r
877\r
878 if (RxData->OptionsLength != 0) {\r
879 RxData->Options = (VOID *) (RxData->Header + 1);\r
880 }\r
881\r
882 RxData->DataLength = Packet->TotalSize;\r
883\r
884 //\r
885 // Build the fragment table to be delivered up.\r
886 //\r
887 RxData->FragmentCount = Packet->BlockOpNum;\r
888 NetbufBuildExt (Packet, (NET_FRAGMENT *) RxData->FragmentTable, &RxData->FragmentCount);\r
889\r
890 return Wrap;\r
891}\r
892\r
893\r
894/**\r
895 Deliver the received packets to upper layer if there are both received\r
896 requests and enqueued packets. If the enqueued packet is shared, it will\r
897 duplicate it to a non-shared packet, release the shared packet, then\r
898 deliver the non-shared packet up.\r
899\r
900 @param IpInstance The IP child to deliver the packet up.\r
901\r
902 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources to deliver the\r
903 packets.\r
904 @retval EFI_SUCCESS All the enqueued packets that can be delivered\r
905 are delivered up.\r
906\r
907**/\r
908EFI_STATUS\r
909Ip4InstanceDeliverPacket (\r
910 IN IP4_PROTOCOL *IpInstance\r
911 )\r
912{\r
913 EFI_IP4_COMPLETION_TOKEN *Token;\r
914 IP4_RXDATA_WRAP *Wrap;\r
915 NET_BUF *Packet;\r
916 NET_BUF *Dup;\r
917 UINT8 *Head;\r
918\r
919 //\r
920 // Deliver a packet if there are both a packet and a receive token.\r
921 //\r
922 while (!NetListIsEmpty (&IpInstance->Received) &&\r
923 !NetMapIsEmpty (&IpInstance->RxTokens)) {\r
924\r
925 Packet = NET_LIST_HEAD (&IpInstance->Received, NET_BUF, List);\r
926\r
927 if (!NET_BUF_SHARED (Packet)) {\r
928 //\r
929 // If this is the only instance that wants the packet, wrap it up.\r
930 //\r
931 Wrap = Ip4WrapRxData (IpInstance, Packet);\r
932\r
933 if (Wrap == NULL) {\r
934 return EFI_OUT_OF_RESOURCES;\r
935 }\r
936\r
937 NetListRemoveEntry (&Packet->List);\r
938\r
939 } else {\r
940 //\r
941 // Create a duplicated packet if this packet is shared\r
942 //\r
943 Dup = NetbufDuplicate (Packet, NULL, IP4_MAX_HEADLEN);\r
944\r
945 if (Dup == NULL) {\r
946 return EFI_OUT_OF_RESOURCES;\r
947 }\r
948\r
949 //\r
950 // Copy the IP head over. The packet to deliver up is\r
951 // headless. Trim the head off after copy. The IP head\r
952 // may be not continuous before the data.\r
953 //\r
954 Head = NetbufAllocSpace (Dup, IP4_MAX_HEADLEN, NET_BUF_HEAD);\r
955 Dup->Ip = (IP4_HEAD *) Head;\r
956\r
957 NetCopyMem (Head, Packet->Ip, Packet->Ip->HeadLen << 2);\r
958 NetbufTrim (Dup, IP4_MAX_HEADLEN, TRUE);\r
959\r
960 Wrap = Ip4WrapRxData (IpInstance, Dup);\r
961\r
962 if (Wrap == NULL) {\r
963 NetbufFree (Dup);\r
964 return EFI_OUT_OF_RESOURCES;\r
965 }\r
966\r
967 NetListRemoveEntry (&Packet->List);\r
968 NetbufFree (Packet);\r
969\r
970 Packet = Dup;\r
971 }\r
972\r
973 //\r
974 // Insert it into the delivered packet, then get a user's\r
975 // receive token, pass the wrapped packet up.\r
976 //\r
977 NET_TRYLOCK (&IpInstance->RecycleLock);\r
978 NetListInsertHead (&IpInstance->Delivered, &Wrap->Link);\r
979 NET_UNLOCK (&IpInstance->RecycleLock);\r
980\r
981 Token = NetMapRemoveHead (&IpInstance->RxTokens, NULL);\r
982 Token->Status = IP4_GET_CLIP_INFO (Packet)->Status;\r
983 Token->Packet.RxData = &Wrap->RxData;\r
984\r
985 gBS->SignalEvent (Token->Event);\r
986 }\r
987\r
988 return EFI_SUCCESS;\r
989}\r
990\r
991\r
992/**\r
993 Enqueue a received packet to all the IP children that share\r
994 the same interface.\r
995\r
996 @param IpSb The IP4 service instance that receive the packet\r
997 @param Head The header of the received packet\r
998 @param Packet The data of the received packet\r
999 @param IpIf The interface to enqueue the packet to\r
1000\r
1001 @return The number of the IP4 children that accepts the packet\r
1002\r
1003**/\r
1004INTN\r
1005Ip4InterfaceEnquePacket (\r
1006 IN IP4_SERVICE *IpSb,\r
1007 IN IP4_HEAD *Head,\r
1008 IN NET_BUF *Packet,\r
1009 IN IP4_INTERFACE *IpIf\r
1010 )\r
1011{\r
1012 IP4_PROTOCOL *IpInstance;\r
1013 IP4_CLIP_INFO *Info;\r
1014 NET_LIST_ENTRY *Entry;\r
1015 INTN Enqueued;\r
1016 INTN LocalType;\r
1017 INTN SavedType;\r
1018\r
1019 //\r
1020 // First, check that the packet is acceptable to this interface\r
1021 // and find the local cast type for the interface. A packet sent\r
1022 // to say 192.168.1.1 should NOT be delliever to 10.0.0.1 unless\r
1023 // promiscuous receiving.\r
1024 //\r
1025 LocalType = 0;\r
1026 Info = IP4_GET_CLIP_INFO (Packet);\r
1027\r
1028 if ((Info->CastType == IP4_MULTICAST) || (Info->CastType == IP4_LOCAL_BROADCAST)) {\r
1029 //\r
1030 // If the CastType is multicast, don't need to filter against\r
1031 // the group address here, Ip4InstanceFrameAcceptable will do\r
1032 // that later.\r
1033 //\r
1034 LocalType = Info->CastType;\r
1035\r
1036 } else {\r
1037 //\r
1038 // Check the destination againist local IP. If the station\r
1039 // address is 0.0.0.0, it means receiving all the IP destined\r
1040 // to local non-zero IP. Otherwise, it is necessary to compare\r
1041 // the destination to the interface's IP address.\r
1042 //\r
1043 if (IpIf->Ip == IP4_ALLZERO_ADDRESS) {\r
1044 LocalType = IP4_LOCAL_HOST;\r
1045\r
1046 } else {\r
1047 LocalType = Ip4GetNetCast (Head->Dst, IpIf);\r
1048\r
1049 if ((LocalType == 0) && IpIf->PromiscRecv) {\r
1050 LocalType = IP4_PROMISCUOUS;\r
1051 }\r
1052 }\r
1053 }\r
1054\r
1055 if (LocalType == 0) {\r
1056 return 0;\r
1057 }\r
1058\r
1059 //\r
1060 // Iterate through the ip instances on the interface, enqueue\r
1061 // the packet if filter passed. Save the original cast type,\r
1062 // and pass the local cast type to the IP children on the\r
1063 // interface. The global cast type will be restored later.\r
1064 //\r
1065 SavedType = Info->CastType;\r
1066 Info->CastType = LocalType;\r
1067\r
1068 Enqueued = 0;\r
1069\r
1070 NET_LIST_FOR_EACH (Entry, &IpIf->IpInstances) {\r
1071 IpInstance = NET_LIST_USER_STRUCT (Entry, IP4_PROTOCOL, AddrLink);\r
1072 NET_CHECK_SIGNATURE (IpInstance, IP4_PROTOCOL_SIGNATURE);\r
1073\r
1074 if (Ip4InstanceEnquePacket (IpInstance, Head, Packet) == EFI_SUCCESS) {\r
1075 Enqueued++;\r
1076 }\r
1077 }\r
1078\r
1079 Info->CastType = SavedType;\r
1080 return Enqueued;\r
1081}\r
1082\r
1083\r
1084/**\r
1085 Deliver the packet for each IP4 child on the interface.\r
1086\r
1087 @param IpSb The IP4 service instance that received the packet\r
1088 @param IpIf The IP4 interface to deliver the packet.\r
1089\r
1090 @retval EFI_SUCCESS It always returns EFI_SUCCESS now\r
1091\r
1092**/\r
1093EFI_STATUS\r
1094Ip4InterfaceDeliverPacket (\r
1095 IN IP4_SERVICE *IpSb,\r
1096 IN IP4_INTERFACE *IpIf\r
1097 )\r
1098{\r
1099 IP4_PROTOCOL *Ip4Instance;\r
1100 NET_LIST_ENTRY *Entry;\r
1101\r
1102 NET_LIST_FOR_EACH (Entry, &IpIf->IpInstances) {\r
1103 Ip4Instance = NET_LIST_USER_STRUCT (Entry, IP4_PROTOCOL, AddrLink);\r
1104 Ip4InstanceDeliverPacket (Ip4Instance);\r
1105 }\r
1106\r
1107 return EFI_SUCCESS;\r
1108}\r
1109\r
1110\r
1111/**\r
1112 Demultiple the packet. the packet delivery is processed in two\r
1113 passes. The first pass will enque a shared copy of the packet\r
1114 to each IP4 child that accepts the packet. The second pass will\r
1115 deliver a non-shared copy of the packet to each IP4 child that\r
1116 has pending receive requests. Data is copied if more than one\r
1117 child wants to consume the packet bacause each IP child need\r
1118 its own copy of the packet to make changes.\r
1119\r
1120 @param IpSb The IP4 service instance that received the packet\r
1121 @param Head The header of the received packet\r
1122 @param Packet The data of the received packet\r
1123\r
1124 @retval EFI_NOT_FOUND No IP child accepts the packet\r
1125 @retval EFI_SUCCESS The packet is enqueued or delivered to some IP\r
1126 children.\r
1127\r
1128**/\r
1129EFI_STATUS\r
1130Ip4Demultiplex (\r
1131 IN IP4_SERVICE *IpSb,\r
1132 IN IP4_HEAD *Head,\r
1133 IN NET_BUF *Packet\r
1134 )\r
1135{\r
1136 NET_LIST_ENTRY *Entry;\r
1137 IP4_INTERFACE *IpIf;\r
1138 INTN Enqueued;\r
1139\r
1140 //\r
1141 // Two pass delivery: first, enque a shared copy of the packet\r
1142 // to each instance that accept the packet.\r
1143 //\r
1144 Enqueued = 0;\r
1145\r
1146 NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) {\r
1147 IpIf = NET_LIST_USER_STRUCT (Entry, IP4_INTERFACE, Link);\r
1148\r
1149 if (IpIf->Configured) {\r
1150 Enqueued += Ip4InterfaceEnquePacket (IpSb, Head, Packet, IpIf);\r
1151 }\r
1152 }\r
1153\r
1154 //\r
1155 // Second: deliver a duplicate of the packet to each instance.\r
1156 // Release the local reference first, so that the last instance\r
1157 // getting the packet will not copy the data.\r
1158 //\r
1159 NetbufFree (Packet);\r
1160\r
1161 if (Enqueued == 0) {\r
1162 return EFI_NOT_FOUND;\r
1163 }\r
1164\r
1165 NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) {\r
1166 IpIf = NET_LIST_USER_STRUCT (Entry, IP4_INTERFACE, Link);\r
1167\r
1168 if (IpIf->Configured) {\r
1169 Ip4InterfaceDeliverPacket (IpSb, IpIf);\r
1170 }\r
1171 }\r
1172\r
1173 return EFI_SUCCESS;\r
1174}\r
1175\r
1176\r
1177/**\r
1178 Timeout the fragment and enqueued packets.\r
1179\r
1180 @param IpSb The IP4 service instance to timeout\r
1181\r
1182 @return None\r
1183\r
1184**/\r
1185VOID\r
1186Ip4PacketTimerTicking (\r
1187 IN IP4_SERVICE *IpSb\r
1188 )\r
1189{\r
1190 NET_LIST_ENTRY *InstanceEntry;\r
1191 NET_LIST_ENTRY *Entry;\r
1192 NET_LIST_ENTRY *Next;\r
1193 IP4_PROTOCOL *IpInstance;\r
1194 IP4_ASSEMBLE_ENTRY *Assemble;\r
1195 NET_BUF *Packet;\r
1196 IP4_CLIP_INFO *Info;\r
1197 UINT32 Index;\r
1198\r
1199 //\r
1200 // First, time out the fragments. The packet's life is counting down\r
1201 // once the first-arrived fragment was received.\r
1202 //\r
1203 for (Index = 0; Index < IP4_ASSEMLE_HASH_SIZE; Index++) {\r
1204 NET_LIST_FOR_EACH_SAFE (Entry, Next, &IpSb->Assemble.Bucket[Index]) {\r
1205 Assemble = NET_LIST_USER_STRUCT (Entry, IP4_ASSEMBLE_ENTRY, Link);\r
1206\r
1207 if ((Assemble->Life > 0) && (--Assemble->Life == 0)) {\r
1208 NetListRemoveEntry (Entry);\r
1209 Ip4FreeAssembleEntry (Assemble);\r
1210 }\r
1211 }\r
1212 }\r
1213\r
1214 NET_LIST_FOR_EACH (InstanceEntry, &IpSb->Children) {\r
1215 IpInstance = NET_LIST_USER_STRUCT (InstanceEntry, IP4_PROTOCOL, Link);\r
1216\r
1217 //\r
1218 // Second, time out the assembled packets enqueued on each IP child.\r
1219 //\r
1220 NET_LIST_FOR_EACH_SAFE (Entry, Next, &IpInstance->Received) {\r
1221 Packet = NET_LIST_USER_STRUCT (Entry, NET_BUF, List);\r
1222 Info = IP4_GET_CLIP_INFO (Packet);\r
1223\r
1224 if ((Info->Life > 0) && (--Info->Life == 0)) {\r
1225 NetListRemoveEntry (Entry);\r
1226 NetbufFree (Packet);\r
1227 }\r
1228 }\r
1229\r
1230 //\r
1231 // Third: time out the transmitted packets.\r
1232 //\r
1233 NetMapIterate (&IpInstance->TxTokens, Ip4SentPacketTicking, NULL);\r
1234 }\r
1235}\r