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