]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Network/Ip4Dxe/Ip4Input.c
1. Add DPC protocol and DpcLib library in MdeModulePkg.
[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
36ee91ca 610 //\r
611 // Dispatch the DPCs queued by the NotifyFunction of the rx token's events\r
612 // which are signaled with received data.\r
613 //\r
614 NetLibDispatchDpc ();\r
615\r
772db4bb 616RESTART:\r
617 Ip4ReceiveFrame (IpSb->DefaultInterface, NULL, Ip4AccpetFrame, IpSb);\r
618\r
619DROP:\r
620 if (Packet != NULL) {\r
621 NetbufFree (Packet);\r
622 }\r
623\r
624 return ;\r
625}\r
626\r
627\r
628/**\r
629 Check whether this IP child accepts the packet.\r
630\r
631 @param IpInstance The IP child to check\r
632 @param Head The IP header of the packet\r
633 @param Packet The data of the packet\r
634\r
635 @return TRUE if the child wants to receive the packet, otherwise return FALSE.\r
636\r
637**/\r
638BOOLEAN\r
639Ip4InstanceFrameAcceptable (\r
640 IN IP4_PROTOCOL *IpInstance,\r
641 IN IP4_HEAD *Head,\r
642 IN NET_BUF *Packet\r
643 )\r
644{\r
645 IP4_ICMP_ERROR_HEAD Icmp;\r
646 EFI_IP4_CONFIG_DATA *Config;\r
647 IP4_CLIP_INFO *Info;\r
648 UINT16 Proto;\r
649 UINT32 Index;\r
650\r
651 Config = &IpInstance->ConfigData;\r
652\r
653 //\r
654 // Dirty trick for the Tiano UEFI network stack implmentation. If\r
655 // ReceiveTimeout == -1, the receive of the packet for this instance\r
656 // is disabled. The UEFI spec don't have such captibility. We add\r
657 // this to improve the performance because IP will make a copy of\r
658 // the received packet for each accepting instance. Some IP instances\r
659 // used by UDP/TCP only send packets, they don't wants to receive.\r
660 //\r
661 if (Config->ReceiveTimeout == (UINT32)(-1)) {\r
662 return FALSE;\r
663 }\r
664\r
665 if (Config->AcceptPromiscuous) {\r
666 return TRUE;\r
667 }\r
668\r
669 //\r
670 // Use protocol from the IP header embedded in the ICMP error\r
671 // message to filter, instead of ICMP itself. ICMP handle will\r
672 // can Ip4Demultiplex to deliver ICMP errors.\r
673 //\r
674 Proto = Head->Protocol;\r
675\r
676 if (Proto == IP4_PROTO_ICMP) {\r
677 NetbufCopy (Packet, 0, sizeof (Icmp.Head), (UINT8 *) &Icmp.Head);\r
678\r
679 if (mIcmpClass[Icmp.Head.Type].IcmpClass == ICMP_ERROR_MESSAGE) {\r
680 if (!Config->AcceptIcmpErrors) {\r
681 return FALSE;\r
682 }\r
683\r
684 NetbufCopy (Packet, 0, sizeof (Icmp), (UINT8 *) &Icmp);\r
685 Proto = Icmp.IpHead.Protocol;\r
686 }\r
687 }\r
688\r
689 //\r
690 // Match the protocol\r
691 //\r
692 if (!Config->AcceptAnyProtocol && (Proto != Config->DefaultProtocol)) {\r
693 return FALSE;\r
694 }\r
695\r
696 //\r
697 // Check for broadcast, the caller has computed the packet's\r
698 // cast type for this child's interface.\r
699 //\r
700 Info = IP4_GET_CLIP_INFO (Packet);\r
701\r
702 if (IP4_IS_BROADCAST (Info->CastType)) {\r
703 return Config->AcceptBroadcast;\r
704 }\r
705\r
706 //\r
707 // If it is a multicast packet, check whether we are in the group.\r
708 //\r
709 if (Info->CastType == IP4_MULTICAST) {\r
710 //\r
711 // Receive the multicast if the instance wants to receive all packets.\r
712 //\r
713 if (!IpInstance->ConfigData.UseDefaultAddress && (IpInstance->Interface->Ip == 0)) {\r
714 return TRUE;\r
715 }\r
716\r
717 for (Index = 0; Index < IpInstance->GroupCount; Index++) {\r
718 if (IpInstance->Groups[Index] == HTONL (Head->Dst)) {\r
719 break;\r
720 }\r
721 }\r
722\r
723 return (BOOLEAN)(Index < IpInstance->GroupCount);\r
724 }\r
725\r
726 return TRUE;\r
727}\r
728\r
729\r
730/**\r
731 Enqueue a shared copy of the packet to the IP4 child if the\r
732 packet is acceptable to it. Here the data of the packet is\r
733 shared, but the net buffer isn't.\r
734\r
735 @param IpInstance The IP4 child to enqueue the packet to\r
736 @param Head The IP header of the received packet\r
737 @param Packet The data of the received packet\r
738\r
739 @retval EFI_NOT_STARTED The IP child hasn't been configured.\r
740 @retval EFI_INVALID_PARAMETER The child doesn't want to receive the packet\r
741 @retval EFI_OUT_OF_RESOURCES Failed to allocate some resource\r
742 @retval EFI_SUCCESS A shared copy the packet is enqueued to the child.\r
743\r
744**/\r
745EFI_STATUS\r
746Ip4InstanceEnquePacket (\r
747 IN IP4_PROTOCOL *IpInstance,\r
748 IN IP4_HEAD *Head,\r
749 IN NET_BUF *Packet\r
750 )\r
751{\r
752 IP4_CLIP_INFO *Info;\r
753 NET_BUF *Clone;\r
754\r
755 //\r
756 // Check whether the packet is acceptable to this instance.\r
757 //\r
758 if (IpInstance->State != IP4_STATE_CONFIGED) {\r
759 return EFI_NOT_STARTED;\r
760 }\r
761\r
762 if (!Ip4InstanceFrameAcceptable (IpInstance, Head, Packet)) {\r
763 return EFI_INVALID_PARAMETER;\r
764 }\r
765\r
766 //\r
767 // Enque a shared copy of the packet.\r
768 //\r
769 Clone = NetbufClone (Packet);\r
770\r
771 if (Clone == NULL) {\r
772 return EFI_OUT_OF_RESOURCES;\r
773 }\r
774\r
775 //\r
776 // Set the receive time out for the assembled packet. If it expires,\r
777 // packet will be removed from the queue.\r
778 //\r
779 Info = IP4_GET_CLIP_INFO (Clone);\r
780 Info->Life = IP4_US_TO_SEC (IpInstance->ConfigData.ReceiveTimeout);\r
781\r
782 NetListInsertTail (&IpInstance->Received, &Clone->List);\r
783 return EFI_SUCCESS;\r
784}\r
785\r
786\r
787/**\r
788 The signal handle of IP4's recycle event. It is called back\r
789 when the upper layer release the packet.\r
790\r
791 @param Event The IP4's recycle event.\r
792 @param Context The context of the handle, which is a\r
793 IP4_RXDATA_WRAP\r
794\r
795 @return None\r
796\r
797**/\r
798STATIC\r
799VOID\r
800EFIAPI\r
801Ip4OnRecyclePacket (\r
802 IN EFI_EVENT Event,\r
803 IN VOID *Context\r
804 )\r
805{\r
806 IP4_RXDATA_WRAP *Wrap;\r
807\r
808 Wrap = (IP4_RXDATA_WRAP *) Context;\r
809\r
810 NET_TRYLOCK (&Wrap->IpInstance->RecycleLock);\r
811 NetListRemoveEntry (&Wrap->Link);\r
812 NET_UNLOCK (&Wrap->IpInstance->RecycleLock);\r
813\r
814 ASSERT (!NET_BUF_SHARED (Wrap->Packet));\r
815 NetbufFree (Wrap->Packet);\r
816\r
817 gBS->CloseEvent (Wrap->RxData.RecycleSignal);\r
818 NetFreePool (Wrap);\r
819}\r
820\r
821\r
822/**\r
823 Wrap the received packet to a IP4_RXDATA_WRAP, which will be\r
824 delivered to the upper layer. Each IP4 child that accepts the\r
825 packet will get a not-shared copy of the packet which is wrapped\r
826 in the IP4_RXDATA_WRAP. The IP4_RXDATA_WRAP->RxData is passed\r
827 to the upper layer. Upper layer will signal the recycle event in\r
828 it when it is done with the packet.\r
829\r
830 @param IpInstance The IP4 child to receive the packet\r
831 @param Packet The packet to deliver up.\r
832\r
833 @return NULL if failed to wrap the packet, otherwise the wrapper.\r
834\r
835**/\r
836IP4_RXDATA_WRAP *\r
837Ip4WrapRxData (\r
838 IN IP4_PROTOCOL *IpInstance,\r
839 IN NET_BUF *Packet\r
840 )\r
841{\r
842 IP4_RXDATA_WRAP *Wrap;\r
843 EFI_IP4_RECEIVE_DATA *RxData;\r
844 EFI_STATUS Status;\r
845\r
846 Wrap = NetAllocatePool (IP4_RXDATA_WRAP_SIZE (Packet->BlockOpNum));\r
847\r
848 if (Wrap == NULL) {\r
849 return NULL;\r
850 }\r
851\r
852 NetListInit (&Wrap->Link);\r
853\r
854 Wrap->IpInstance = IpInstance;\r
855 Wrap->Packet = Packet;\r
856 RxData = &Wrap->RxData;\r
857\r
858 NetZeroMem (&RxData->TimeStamp, sizeof (EFI_TIME));\r
859\r
860 Status = gBS->CreateEvent (\r
861 EVT_NOTIFY_SIGNAL,\r
862 NET_TPL_RECYCLE,\r
863 Ip4OnRecyclePacket,\r
864 Wrap,\r
865 &RxData->RecycleSignal\r
866 );\r
867\r
868 if (EFI_ERROR (Status)) {\r
869 NetFreePool (Wrap);\r
870 return NULL;\r
871 }\r
872\r
873 ASSERT (Packet->Ip != NULL);\r
874\r
875 //\r
876 // The application expects a network byte order header.\r
877 //\r
878 RxData->HeaderLength = (Packet->Ip->HeadLen << 2);\r
879 RxData->Header = (EFI_IP4_HEADER *) Ip4NtohHead (Packet->Ip);\r
880\r
881 RxData->OptionsLength = RxData->HeaderLength - IP4_MIN_HEADLEN;\r
882 RxData->Options = NULL;\r
883\r
884 if (RxData->OptionsLength != 0) {\r
885 RxData->Options = (VOID *) (RxData->Header + 1);\r
886 }\r
887\r
888 RxData->DataLength = Packet->TotalSize;\r
889\r
890 //\r
891 // Build the fragment table to be delivered up.\r
892 //\r
893 RxData->FragmentCount = Packet->BlockOpNum;\r
894 NetbufBuildExt (Packet, (NET_FRAGMENT *) RxData->FragmentTable, &RxData->FragmentCount);\r
895\r
896 return Wrap;\r
897}\r
898\r
899\r
900/**\r
901 Deliver the received packets to upper layer if there are both received\r
902 requests and enqueued packets. If the enqueued packet is shared, it will\r
903 duplicate it to a non-shared packet, release the shared packet, then\r
904 deliver the non-shared packet up.\r
905\r
906 @param IpInstance The IP child to deliver the packet up.\r
907\r
908 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources to deliver the\r
909 packets.\r
910 @retval EFI_SUCCESS All the enqueued packets that can be delivered\r
911 are delivered up.\r
912\r
913**/\r
914EFI_STATUS\r
915Ip4InstanceDeliverPacket (\r
916 IN IP4_PROTOCOL *IpInstance\r
917 )\r
918{\r
919 EFI_IP4_COMPLETION_TOKEN *Token;\r
920 IP4_RXDATA_WRAP *Wrap;\r
921 NET_BUF *Packet;\r
922 NET_BUF *Dup;\r
923 UINT8 *Head;\r
924\r
925 //\r
926 // Deliver a packet if there are both a packet and a receive token.\r
927 //\r
928 while (!NetListIsEmpty (&IpInstance->Received) &&\r
929 !NetMapIsEmpty (&IpInstance->RxTokens)) {\r
930\r
931 Packet = NET_LIST_HEAD (&IpInstance->Received, NET_BUF, List);\r
932\r
933 if (!NET_BUF_SHARED (Packet)) {\r
934 //\r
935 // If this is the only instance that wants the packet, wrap it up.\r
936 //\r
937 Wrap = Ip4WrapRxData (IpInstance, Packet);\r
938\r
939 if (Wrap == NULL) {\r
940 return EFI_OUT_OF_RESOURCES;\r
941 }\r
942\r
943 NetListRemoveEntry (&Packet->List);\r
944\r
945 } else {\r
946 //\r
947 // Create a duplicated packet if this packet is shared\r
948 //\r
949 Dup = NetbufDuplicate (Packet, NULL, IP4_MAX_HEADLEN);\r
950\r
951 if (Dup == NULL) {\r
952 return EFI_OUT_OF_RESOURCES;\r
953 }\r
954\r
955 //\r
956 // Copy the IP head over. The packet to deliver up is\r
957 // headless. Trim the head off after copy. The IP head\r
958 // may be not continuous before the data.\r
959 //\r
960 Head = NetbufAllocSpace (Dup, IP4_MAX_HEADLEN, NET_BUF_HEAD);\r
961 Dup->Ip = (IP4_HEAD *) Head;\r
962\r
963 NetCopyMem (Head, Packet->Ip, Packet->Ip->HeadLen << 2);\r
964 NetbufTrim (Dup, IP4_MAX_HEADLEN, TRUE);\r
965\r
966 Wrap = Ip4WrapRxData (IpInstance, Dup);\r
967\r
968 if (Wrap == NULL) {\r
969 NetbufFree (Dup);\r
970 return EFI_OUT_OF_RESOURCES;\r
971 }\r
972\r
973 NetListRemoveEntry (&Packet->List);\r
974 NetbufFree (Packet);\r
975\r
976 Packet = Dup;\r
977 }\r
978\r
979 //\r
980 // Insert it into the delivered packet, then get a user's\r
981 // receive token, pass the wrapped packet up.\r
982 //\r
983 NET_TRYLOCK (&IpInstance->RecycleLock);\r
984 NetListInsertHead (&IpInstance->Delivered, &Wrap->Link);\r
985 NET_UNLOCK (&IpInstance->RecycleLock);\r
986\r
987 Token = NetMapRemoveHead (&IpInstance->RxTokens, NULL);\r
988 Token->Status = IP4_GET_CLIP_INFO (Packet)->Status;\r
989 Token->Packet.RxData = &Wrap->RxData;\r
990\r
991 gBS->SignalEvent (Token->Event);\r
992 }\r
993\r
994 return EFI_SUCCESS;\r
995}\r
996\r
997\r
998/**\r
999 Enqueue a received packet to all the IP children that share\r
1000 the same interface.\r
1001\r
1002 @param IpSb The IP4 service instance that receive the packet\r
1003 @param Head The header of the received packet\r
1004 @param Packet The data of the received packet\r
1005 @param IpIf The interface to enqueue the packet to\r
1006\r
1007 @return The number of the IP4 children that accepts the packet\r
1008\r
1009**/\r
1010INTN\r
1011Ip4InterfaceEnquePacket (\r
1012 IN IP4_SERVICE *IpSb,\r
1013 IN IP4_HEAD *Head,\r
1014 IN NET_BUF *Packet,\r
1015 IN IP4_INTERFACE *IpIf\r
1016 )\r
1017{\r
1018 IP4_PROTOCOL *IpInstance;\r
1019 IP4_CLIP_INFO *Info;\r
1020 NET_LIST_ENTRY *Entry;\r
1021 INTN Enqueued;\r
1022 INTN LocalType;\r
1023 INTN SavedType;\r
1024\r
1025 //\r
1026 // First, check that the packet is acceptable to this interface\r
1027 // and find the local cast type for the interface. A packet sent\r
1028 // to say 192.168.1.1 should NOT be delliever to 10.0.0.1 unless\r
1029 // promiscuous receiving.\r
1030 //\r
1031 LocalType = 0;\r
1032 Info = IP4_GET_CLIP_INFO (Packet);\r
1033\r
1034 if ((Info->CastType == IP4_MULTICAST) || (Info->CastType == IP4_LOCAL_BROADCAST)) {\r
1035 //\r
1036 // If the CastType is multicast, don't need to filter against\r
1037 // the group address here, Ip4InstanceFrameAcceptable will do\r
1038 // that later.\r
1039 //\r
1040 LocalType = Info->CastType;\r
1041\r
1042 } else {\r
1043 //\r
1044 // Check the destination againist local IP. If the station\r
1045 // address is 0.0.0.0, it means receiving all the IP destined\r
1046 // to local non-zero IP. Otherwise, it is necessary to compare\r
1047 // the destination to the interface's IP address.\r
1048 //\r
1049 if (IpIf->Ip == IP4_ALLZERO_ADDRESS) {\r
1050 LocalType = IP4_LOCAL_HOST;\r
1051\r
1052 } else {\r
1053 LocalType = Ip4GetNetCast (Head->Dst, IpIf);\r
1054\r
1055 if ((LocalType == 0) && IpIf->PromiscRecv) {\r
1056 LocalType = IP4_PROMISCUOUS;\r
1057 }\r
1058 }\r
1059 }\r
1060\r
1061 if (LocalType == 0) {\r
1062 return 0;\r
1063 }\r
1064\r
1065 //\r
1066 // Iterate through the ip instances on the interface, enqueue\r
1067 // the packet if filter passed. Save the original cast type,\r
1068 // and pass the local cast type to the IP children on the\r
1069 // interface. The global cast type will be restored later.\r
1070 //\r
1071 SavedType = Info->CastType;\r
1072 Info->CastType = LocalType;\r
1073\r
1074 Enqueued = 0;\r
1075\r
1076 NET_LIST_FOR_EACH (Entry, &IpIf->IpInstances) {\r
1077 IpInstance = NET_LIST_USER_STRUCT (Entry, IP4_PROTOCOL, AddrLink);\r
1078 NET_CHECK_SIGNATURE (IpInstance, IP4_PROTOCOL_SIGNATURE);\r
1079\r
1080 if (Ip4InstanceEnquePacket (IpInstance, Head, Packet) == EFI_SUCCESS) {\r
1081 Enqueued++;\r
1082 }\r
1083 }\r
1084\r
1085 Info->CastType = SavedType;\r
1086 return Enqueued;\r
1087}\r
1088\r
1089\r
1090/**\r
1091 Deliver the packet for each IP4 child on the interface.\r
1092\r
1093 @param IpSb The IP4 service instance that received the packet\r
1094 @param IpIf The IP4 interface to deliver the packet.\r
1095\r
1096 @retval EFI_SUCCESS It always returns EFI_SUCCESS now\r
1097\r
1098**/\r
1099EFI_STATUS\r
1100Ip4InterfaceDeliverPacket (\r
1101 IN IP4_SERVICE *IpSb,\r
1102 IN IP4_INTERFACE *IpIf\r
1103 )\r
1104{\r
1105 IP4_PROTOCOL *Ip4Instance;\r
1106 NET_LIST_ENTRY *Entry;\r
1107\r
1108 NET_LIST_FOR_EACH (Entry, &IpIf->IpInstances) {\r
1109 Ip4Instance = NET_LIST_USER_STRUCT (Entry, IP4_PROTOCOL, AddrLink);\r
1110 Ip4InstanceDeliverPacket (Ip4Instance);\r
1111 }\r
1112\r
1113 return EFI_SUCCESS;\r
1114}\r
1115\r
1116\r
1117/**\r
1118 Demultiple the packet. the packet delivery is processed in two\r
1119 passes. The first pass will enque a shared copy of the packet\r
1120 to each IP4 child that accepts the packet. The second pass will\r
1121 deliver a non-shared copy of the packet to each IP4 child that\r
1122 has pending receive requests. Data is copied if more than one\r
1123 child wants to consume the packet bacause each IP child need\r
1124 its own copy of the packet to make changes.\r
1125\r
1126 @param IpSb The IP4 service instance that received the packet\r
1127 @param Head The header of the received packet\r
1128 @param Packet The data of the received packet\r
1129\r
1130 @retval EFI_NOT_FOUND No IP child accepts the packet\r
1131 @retval EFI_SUCCESS The packet is enqueued or delivered to some IP\r
1132 children.\r
1133\r
1134**/\r
1135EFI_STATUS\r
1136Ip4Demultiplex (\r
1137 IN IP4_SERVICE *IpSb,\r
1138 IN IP4_HEAD *Head,\r
1139 IN NET_BUF *Packet\r
1140 )\r
1141{\r
1142 NET_LIST_ENTRY *Entry;\r
1143 IP4_INTERFACE *IpIf;\r
1144 INTN Enqueued;\r
1145\r
1146 //\r
1147 // Two pass delivery: first, enque a shared copy of the packet\r
1148 // to each instance that accept the packet.\r
1149 //\r
1150 Enqueued = 0;\r
1151\r
1152 NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) {\r
1153 IpIf = NET_LIST_USER_STRUCT (Entry, IP4_INTERFACE, Link);\r
1154\r
1155 if (IpIf->Configured) {\r
1156 Enqueued += Ip4InterfaceEnquePacket (IpSb, Head, Packet, IpIf);\r
1157 }\r
1158 }\r
1159\r
1160 //\r
1161 // Second: deliver a duplicate of the packet to each instance.\r
1162 // Release the local reference first, so that the last instance\r
1163 // getting the packet will not copy the data.\r
1164 //\r
1165 NetbufFree (Packet);\r
1166\r
1167 if (Enqueued == 0) {\r
1168 return EFI_NOT_FOUND;\r
1169 }\r
1170\r
1171 NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) {\r
1172 IpIf = NET_LIST_USER_STRUCT (Entry, IP4_INTERFACE, Link);\r
1173\r
1174 if (IpIf->Configured) {\r
1175 Ip4InterfaceDeliverPacket (IpSb, IpIf);\r
1176 }\r
1177 }\r
1178\r
1179 return EFI_SUCCESS;\r
1180}\r
1181\r
1182\r
1183/**\r
1184 Timeout the fragment and enqueued packets.\r
1185\r
1186 @param IpSb The IP4 service instance to timeout\r
1187\r
1188 @return None\r
1189\r
1190**/\r
1191VOID\r
1192Ip4PacketTimerTicking (\r
1193 IN IP4_SERVICE *IpSb\r
1194 )\r
1195{\r
1196 NET_LIST_ENTRY *InstanceEntry;\r
1197 NET_LIST_ENTRY *Entry;\r
1198 NET_LIST_ENTRY *Next;\r
1199 IP4_PROTOCOL *IpInstance;\r
1200 IP4_ASSEMBLE_ENTRY *Assemble;\r
1201 NET_BUF *Packet;\r
1202 IP4_CLIP_INFO *Info;\r
1203 UINT32 Index;\r
1204\r
1205 //\r
1206 // First, time out the fragments. The packet's life is counting down\r
1207 // once the first-arrived fragment was received.\r
1208 //\r
1209 for (Index = 0; Index < IP4_ASSEMLE_HASH_SIZE; Index++) {\r
1210 NET_LIST_FOR_EACH_SAFE (Entry, Next, &IpSb->Assemble.Bucket[Index]) {\r
1211 Assemble = NET_LIST_USER_STRUCT (Entry, IP4_ASSEMBLE_ENTRY, Link);\r
1212\r
1213 if ((Assemble->Life > 0) && (--Assemble->Life == 0)) {\r
1214 NetListRemoveEntry (Entry);\r
1215 Ip4FreeAssembleEntry (Assemble);\r
1216 }\r
1217 }\r
1218 }\r
1219\r
1220 NET_LIST_FOR_EACH (InstanceEntry, &IpSb->Children) {\r
1221 IpInstance = NET_LIST_USER_STRUCT (InstanceEntry, IP4_PROTOCOL, Link);\r
1222\r
1223 //\r
1224 // Second, time out the assembled packets enqueued on each IP child.\r
1225 //\r
1226 NET_LIST_FOR_EACH_SAFE (Entry, Next, &IpInstance->Received) {\r
1227 Packet = NET_LIST_USER_STRUCT (Entry, NET_BUF, List);\r
1228 Info = IP4_GET_CLIP_INFO (Packet);\r
1229\r
1230 if ((Info->Life > 0) && (--Info->Life == 0)) {\r
1231 NetListRemoveEntry (Entry);\r
1232 NetbufFree (Packet);\r
1233 }\r
1234 }\r
1235\r
1236 //\r
1237 // Third: time out the transmitted packets.\r
1238 //\r
1239 NetMapIterate (&IpInstance->TxTokens, Ip4SentPacketTicking, NULL);\r
1240 }\r
1241}\r