]>
Commit | Line | Data |
---|---|---|
772db4bb | 1 | /** @file\r |
3e8c18da | 2 | IP4 input process.\r |
3 | \r | |
75dce340 | 4 | Copyright (c) 2005 - 2012, Intel Corporation. All rights reserved.<BR>\r |
e5eed7d3 | 5 | This program and the accompanying materials\r |
772db4bb | 6 | are licensed and made available under the terms and conditions of the BSD License\r |
7 | which accompanies this distribution. The full text of the license may be found at\r | |
8 | http://opensource.org/licenses/bsd-license.php\r | |
9 | \r | |
10 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
11 | WITHOUT 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 | 32 | IP4_ASSEMBLE_ENTRY *\r |
33 | Ip4CreateAssembleEntry (\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 | 72 | VOID\r |
73 | Ip4FreeAssembleEntry (\r | |
74 | IN IP4_ASSEMBLE_ENTRY *Assemble\r | |
75 | )\r | |
76 | {\r | |
e48e37fc | 77 | LIST_ENTRY *Entry;\r |
78 | LIST_ENTRY *Next;\r | |
772db4bb | 79 | NET_BUF *Fragment;\r |
80 | \r | |
81 | NET_LIST_FOR_EACH_SAFE (Entry, Next, &Assemble->Fragments) {\r | |
82 | Fragment = NET_LIST_USER_STRUCT (Entry, NET_BUF, List);\r | |
83 | \r | |
e48e37fc | 84 | RemoveEntryList (Entry);\r |
772db4bb | 85 | NetbufFree (Fragment);\r |
86 | }\r | |
87 | \r | |
766c7483 | 88 | FreePool (Assemble);\r |
772db4bb | 89 | }\r |
90 | \r | |
91 | \r | |
92 | /**\r | |
93 | Initialize an already allocated assemble table. This is generally\r | |
94 | the assemble table embedded in the IP4 service instance.\r | |
95 | \r | |
3e8c18da | 96 | @param[in, out] Table The assemble table to initialize.\r |
772db4bb | 97 | \r |
98 | **/\r | |
99 | VOID\r | |
100 | Ip4InitAssembleTable (\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 | |
119 | VOID\r | |
120 | Ip4CleanAssembleTable (\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 | 149 | VOID\r |
150 | Ip4TrimPacket (\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 | 190 | VOID\r |
e798cd87 | 191 | EFIAPI\r |
772db4bb | 192 | Ip4OnFreeFragments (\r |
193 | IN VOID *Arg\r | |
194 | )\r | |
195 | {\r | |
196 | Ip4FreeAssembleEntry ((IP4_ASSEMBLE_ENTRY *) Arg);\r | |
197 | }\r | |
198 | \r | |
199 | \r | |
200 | /**\r | |
201 | Reassemble the IP fragments. If all the fragments of the packet\r | |
202 | have been received, it will wrap the packet in a net buffer then\r | |
203 | return it to caller. If the packet can't be assembled, NULL is\r | |
204 | return.\r | |
205 | \r | |
96e1079f | 206 | @param Table The assemble table used. New assemble entry will be created\r |
207 | if the Packet is from a new chain of fragments.\r | |
208 | @param Packet The fragment to assemble. It might be freed if the fragment\r | |
209 | can't be re-assembled.\r | |
772db4bb | 210 | \r |
211 | @return NULL if the packet can't be reassemble. The point to just assembled\r | |
96e1079f | 212 | packet if all the fragments of the packet have arrived.\r |
772db4bb | 213 | \r |
214 | **/\r | |
772db4bb | 215 | NET_BUF *\r |
216 | Ip4Reassemble (\r | |
96e1079f | 217 | IN OUT IP4_ASSEMBLE_TABLE *Table,\r |
218 | IN OUT NET_BUF *Packet\r | |
772db4bb | 219 | )\r |
220 | {\r | |
221 | IP4_HEAD *IpHead;\r | |
222 | IP4_CLIP_INFO *This;\r | |
223 | IP4_CLIP_INFO *Node;\r | |
224 | IP4_ASSEMBLE_ENTRY *Assemble;\r | |
e48e37fc | 225 | LIST_ENTRY *Head;\r |
226 | LIST_ENTRY *Prev;\r | |
227 | LIST_ENTRY *Cur;\r | |
772db4bb | 228 | NET_BUF *Fragment;\r |
229 | NET_BUF *NewPacket;\r | |
230 | INTN Index;\r | |
231 | \r | |
f6b7393c | 232 | IpHead = Packet->Ip.Ip4;\r |
772db4bb | 233 | This = IP4_GET_CLIP_INFO (Packet);\r |
234 | \r | |
235 | ASSERT (IpHead != NULL);\r | |
236 | \r | |
237 | //\r | |
238 | // First: find the related assemble entry\r | |
239 | //\r | |
240 | Assemble = NULL;\r | |
241 | Index = IP4_ASSEMBLE_HASH (IpHead->Dst, IpHead->Src, IpHead->Id, IpHead->Protocol);\r | |
242 | \r | |
243 | NET_LIST_FOR_EACH (Cur, &Table->Bucket[Index]) {\r | |
244 | Assemble = NET_LIST_USER_STRUCT (Cur, IP4_ASSEMBLE_ENTRY, Link);\r | |
245 | \r | |
246 | if ((Assemble->Dst == IpHead->Dst) && (Assemble->Src == IpHead->Src) &&\r | |
247 | (Assemble->Id == IpHead->Id) && (Assemble->Protocol == IpHead->Protocol)) {\r | |
248 | break;\r | |
249 | }\r | |
250 | }\r | |
251 | \r | |
252 | //\r | |
253 | // Create a new assemble entry if no assemble entry is related to this packet\r | |
254 | //\r | |
255 | if (Cur == &Table->Bucket[Index]) {\r | |
256 | Assemble = Ip4CreateAssembleEntry (\r | |
257 | IpHead->Dst,\r | |
258 | IpHead->Src,\r | |
259 | IpHead->Id,\r | |
260 | IpHead->Protocol\r | |
261 | );\r | |
262 | \r | |
263 | if (Assemble == NULL) {\r | |
264 | goto DROP;\r | |
265 | }\r | |
266 | \r | |
e48e37fc | 267 | InsertHeadList (&Table->Bucket[Index], &Assemble->Link);\r |
772db4bb | 268 | }\r |
894d038a | 269 | //\r |
270 | // Assemble shouldn't be NULL here\r | |
271 | //\r | |
272 | ASSERT (Assemble != NULL);\r | |
772db4bb | 273 | \r |
274 | //\r | |
275 | // Find the point to insert the packet: before the first\r | |
276 | // fragment with THIS.Start < CUR.Start. the previous one\r | |
277 | // has PREV.Start <= THIS.Start < CUR.Start.\r | |
278 | //\r | |
279 | Head = &Assemble->Fragments;\r | |
280 | \r | |
281 | NET_LIST_FOR_EACH (Cur, Head) {\r | |
282 | Fragment = NET_LIST_USER_STRUCT (Cur, NET_BUF, List);\r | |
283 | \r | |
284 | if (This->Start < IP4_GET_CLIP_INFO (Fragment)->Start) {\r | |
285 | break;\r | |
286 | }\r | |
287 | }\r | |
288 | \r | |
289 | //\r | |
290 | // Check whether the current fragment overlaps with the previous one.\r | |
291 | // It holds that: PREV.Start <= THIS.Start < THIS.End. Only need to\r | |
292 | // check whether THIS.Start < PREV.End for overlap. If two fragments\r | |
293 | // overlaps, trim the overlapped part off THIS fragment.\r | |
294 | //\r | |
a167ecb1 | 295 | if ((Cur != Head) && ((Prev = Cur->BackLink) != Head)) {\r |
772db4bb | 296 | Fragment = NET_LIST_USER_STRUCT (Prev, NET_BUF, List);\r |
297 | Node = IP4_GET_CLIP_INFO (Fragment);\r | |
298 | \r | |
299 | if (This->Start < Node->End) {\r | |
300 | if (This->End <= Node->End) {\r | |
301 | NetbufFree (Packet);\r | |
302 | return NULL;\r | |
303 | }\r | |
304 | \r | |
305 | Ip4TrimPacket (Packet, Node->End, This->End);\r | |
306 | }\r | |
307 | }\r | |
308 | \r | |
309 | //\r | |
310 | // Insert the fragment into the packet. The fragment may be removed\r | |
311 | // from the list by the following checks.\r | |
312 | //\r | |
313 | NetListInsertBefore (Cur, &Packet->List);\r | |
314 | \r | |
315 | //\r | |
316 | // Check the packets after the insert point. It holds that:\r | |
317 | // THIS.Start <= NODE.Start < NODE.End. The equality holds\r | |
318 | // if PREV and NEXT are continuous. THIS fragment may fill\r | |
319 | // several holes. Remove the completely overlapped fragments\r | |
320 | //\r | |
321 | while (Cur != Head) {\r | |
322 | Fragment = NET_LIST_USER_STRUCT (Cur, NET_BUF, List);\r | |
323 | Node = IP4_GET_CLIP_INFO (Fragment);\r | |
324 | \r | |
325 | //\r | |
326 | // Remove fragments completely overlapped by this fragment\r | |
327 | //\r | |
328 | if (Node->End <= This->End) {\r | |
329 | Cur = Cur->ForwardLink;\r | |
330 | \r | |
e48e37fc | 331 | RemoveEntryList (&Fragment->List);\r |
772db4bb | 332 | Assemble->CurLen -= Node->Length;\r |
333 | \r | |
334 | NetbufFree (Fragment);\r | |
335 | continue;\r | |
336 | }\r | |
337 | \r | |
338 | //\r | |
339 | // The conditions are: THIS.Start <= NODE.Start, and THIS.End <\r | |
340 | // NODE.End. Two fragments overlaps if NODE.Start < THIS.End.\r | |
341 | // If two fragments start at the same offset, remove THIS fragment\r | |
342 | // because ((THIS.Start == NODE.Start) && (THIS.End < NODE.End)).\r | |
343 | //\r | |
344 | if (Node->Start < This->End) {\r | |
345 | if (This->Start == Node->Start) {\r | |
e48e37fc | 346 | RemoveEntryList (&Packet->List);\r |
772db4bb | 347 | goto DROP;\r |
348 | }\r | |
349 | \r | |
350 | Ip4TrimPacket (Packet, This->Start, Node->Start);\r | |
351 | }\r | |
352 | \r | |
353 | break;\r | |
354 | }\r | |
355 | \r | |
356 | //\r | |
357 | // Update the assemble info: increase the current length. If it is\r | |
358 | // the frist fragment, update the packet's IP head and per packet\r | |
359 | // info. If it is the last fragment, update the total length.\r | |
360 | //\r | |
361 | Assemble->CurLen += This->Length;\r | |
362 | \r | |
363 | if (This->Start == 0) {\r | |
364 | //\r | |
365 | // Once the first fragment is enqueued, it can't be removed\r | |
366 | // from the fragment list. So, Assemble->Head always point\r | |
367 | // to valid memory area.\r | |
368 | //\r | |
369 | ASSERT (Assemble->Head == NULL);\r | |
370 | \r | |
371 | Assemble->Head = IpHead;\r | |
372 | Assemble->Info = IP4_GET_CLIP_INFO (Packet);\r | |
373 | }\r | |
374 | \r | |
375 | //\r | |
376 | // Don't update the length more than once.\r | |
377 | //\r | |
378 | if (IP4_LAST_FRAGMENT (IpHead->Fragment) && (Assemble->TotalLen == 0)) {\r | |
379 | Assemble->TotalLen = This->End;\r | |
380 | }\r | |
381 | \r | |
382 | //\r | |
383 | // Deliver the whole packet if all the fragments received.\r | |
384 | // All fragments received if:\r | |
96e1079f | 385 | // 1. received the last one, so, the total length is know\r |
772db4bb | 386 | // 2. received all the data. If the last fragment on the\r |
387 | // queue ends at the total length, all data is received.\r | |
388 | //\r | |
389 | if ((Assemble->TotalLen != 0) && (Assemble->CurLen >= Assemble->TotalLen)) {\r | |
390 | \r | |
e48e37fc | 391 | RemoveEntryList (&Assemble->Link);\r |
772db4bb | 392 | \r |
393 | //\r | |
394 | // If the packet is properly formated, the last fragment's End\r | |
395 | // equals to the packet's total length. Otherwise, the packet\r | |
396 | // is a fake, drop it now.\r | |
397 | //\r | |
398 | Fragment = NET_LIST_USER_STRUCT (Head->BackLink, NET_BUF, List);\r | |
399 | \r | |
400 | if (IP4_GET_CLIP_INFO (Fragment)->End != Assemble->TotalLen) {\r | |
401 | Ip4FreeAssembleEntry (Assemble);\r | |
402 | return NULL;\r | |
403 | }\r | |
404 | \r | |
405 | //\r | |
406 | // Wrap the packet in a net buffer then deliver it up\r | |
407 | //\r | |
408 | NewPacket = NetbufFromBufList (\r | |
409 | &Assemble->Fragments,\r | |
410 | 0,\r | |
411 | 0,\r | |
412 | Ip4OnFreeFragments,\r | |
413 | Assemble\r | |
414 | );\r | |
415 | \r | |
416 | if (NewPacket == NULL) {\r | |
417 | Ip4FreeAssembleEntry (Assemble);\r | |
418 | return NULL;\r | |
419 | }\r | |
420 | \r | |
f6b7393c | 421 | NewPacket->Ip.Ip4 = Assemble->Head;\r |
04eb20aa ED |
422 | \r |
423 | ASSERT (Assemble->Info != NULL);\r | |
424 | \r | |
425 | CopyMem (\r | |
426 | IP4_GET_CLIP_INFO (NewPacket),\r | |
427 | Assemble->Info,\r | |
428 | sizeof (*IP4_GET_CLIP_INFO (NewPacket))\r | |
429 | );\r | |
430 | \r | |
772db4bb | 431 | return NewPacket;\r |
432 | }\r | |
433 | \r | |
434 | return NULL;\r | |
435 | \r | |
436 | DROP:\r | |
437 | NetbufFree (Packet);\r | |
438 | return NULL;\r | |
439 | }\r | |
440 | \r | |
a1503a32 | 441 | /**\r |
442 | The callback function for the net buffer which wraps the packet processed by \r | |
443 | IPsec. It releases the wrap packet and also signals IPsec to free the resources. \r | |
444 | \r | |
445 | @param[in] Arg The wrap context\r | |
446 | \r | |
447 | **/\r | |
448 | VOID\r | |
e798cd87 | 449 | EFIAPI\r |
a1503a32 | 450 | Ip4IpSecFree (\r |
451 | IN VOID *Arg\r | |
452 | )\r | |
453 | {\r | |
454 | IP4_IPSEC_WRAP *Wrap;\r | |
455 | \r | |
456 | Wrap = (IP4_IPSEC_WRAP *) Arg;\r | |
457 | \r | |
458 | if (Wrap->IpSecRecycleSignal != NULL) {\r | |
459 | gBS->SignalEvent (Wrap->IpSecRecycleSignal);\r | |
460 | }\r | |
461 | \r | |
462 | NetbufFree (Wrap->Packet);\r | |
463 | \r | |
464 | FreePool (Wrap);\r | |
465 | \r | |
466 | return;\r | |
467 | }\r | |
468 | \r | |
469 | /**\r | |
470 | The work function to locate IPsec protocol to process the inbound or \r | |
471 | outbound IP packets. The process routine handls the packet with following\r | |
472 | actions: bypass the packet, discard the packet, or protect the packet. \r | |
473 | \r | |
705f53a9 | 474 | @param[in] IpSb The IP4 service instance.\r |
475 | @param[in, out] Head The The caller supplied IP4 header.\r | |
476 | @param[in, out] Netbuf The IP4 packet to be processed by IPsec.\r | |
477 | @param[in, out] Options The caller supplied options.\r | |
478 | @param[in, out] OptionsLen The length of the option.\r | |
a1503a32 | 479 | @param[in] Direction The directionality in an SPD entry, \r |
705f53a9 | 480 | EfiIPsecInBound or EfiIPsecOutBound.\r |
481 | @param[in] Context The token's wrap.\r | |
a1503a32 | 482 | \r |
483 | @retval EFI_SUCCESS The IPsec protocol is not available or disabled.\r | |
484 | @retval EFI_SUCCESS The packet was bypassed and all buffers remain the same.\r | |
485 | @retval EFI_SUCCESS The packet was protected.\r | |
486 | @retval EFI_ACCESS_DENIED The packet was discarded. \r | |
487 | @retval EFI_OUT_OF_RESOURCES There is no suffcient resource to complete the operation.\r | |
488 | @retval EFI_BUFFER_TOO_SMALL The number of non-empty block is bigger than the \r | |
489 | number of input data blocks when build a fragment table.\r | |
490 | \r | |
491 | **/\r | |
492 | EFI_STATUS\r | |
493 | Ip4IpSecProcessPacket (\r | |
705f53a9 | 494 | IN IP4_SERVICE *IpSb,\r |
495 | IN OUT IP4_HEAD **Head,\r | |
496 | IN OUT NET_BUF **Netbuf,\r | |
497 | IN OUT UINT8 **Options,\r | |
498 | IN OUT UINT32 *OptionsLen,\r | |
499 | IN EFI_IPSEC_TRAFFIC_DIR Direction,\r | |
500 | IN VOID *Context\r | |
a1503a32 | 501 | )\r |
502 | {\r | |
503 | NET_FRAGMENT *FragmentTable;\r | |
705f53a9 | 504 | NET_FRAGMENT *OriginalFragmentTable;\r |
a1503a32 | 505 | UINT32 FragmentCount;\r |
705f53a9 | 506 | UINT32 OriginalFragmentCount;\r |
a1503a32 | 507 | EFI_EVENT RecycleEvent;\r |
508 | NET_BUF *Packet;\r | |
509 | IP4_TXTOKEN_WRAP *TxWrap;\r | |
510 | IP4_IPSEC_WRAP *IpSecWrap;\r | |
511 | EFI_STATUS Status;\r | |
705f53a9 | 512 | IP4_HEAD ZeroHead;\r |
a1503a32 | 513 | \r |
514 | Status = EFI_SUCCESS;\r | |
515 | Packet = *Netbuf;\r | |
516 | RecycleEvent = NULL;\r | |
517 | IpSecWrap = NULL;\r | |
518 | FragmentTable = NULL;\r | |
519 | TxWrap = (IP4_TXTOKEN_WRAP *) Context; \r | |
520 | FragmentCount = Packet->BlockOpNum;\r | |
705f53a9 | 521 | \r |
522 | ZeroMem (&ZeroHead, sizeof (IP4_HEAD));\r | |
a1503a32 | 523 | \r |
524 | if (mIpSec == NULL) {\r | |
0a7294f7 | 525 | gBS->LocateProtocol (&gEfiIpSec2ProtocolGuid, NULL, (VOID **) &mIpSec);\r |
9e375eb1 | 526 | if (mIpSec == NULL) {\r |
527 | goto ON_EXIT;\r | |
a1503a32 | 528 | }\r |
529 | }\r | |
530 | \r | |
a1503a32 | 531 | //\r |
532 | // Check whether the IPsec enable variable is set.\r | |
533 | //\r | |
534 | if (mIpSec->DisabledFlag) {\r | |
535 | //\r | |
536 | // If IPsec is disabled, restore the original MTU\r | |
537 | // \r | |
538 | IpSb->MaxPacketSize = IpSb->OldMaxPacketSize;\r | |
539 | goto ON_EXIT;\r | |
540 | } else {\r | |
541 | //\r | |
542 | // If IPsec is enabled, use the MTU which reduce the IPsec header length. \r | |
543 | //\r | |
544 | IpSb->MaxPacketSize = IpSb->OldMaxPacketSize - IP4_MAX_IPSEC_HEADLEN; \r | |
545 | }\r | |
546 | \r | |
547 | //\r | |
548 | // Rebuild fragment table from netbuf to ease IPsec process.\r | |
549 | //\r | |
550 | FragmentTable = AllocateZeroPool (FragmentCount * sizeof (NET_FRAGMENT));\r | |
551 | \r | |
552 | if (FragmentTable == NULL) {\r | |
553 | Status = EFI_OUT_OF_RESOURCES;\r | |
554 | goto ON_EXIT;\r | |
555 | }\r | |
556 | \r | |
557 | Status = NetbufBuildExt (Packet, FragmentTable, &FragmentCount);\r | |
705f53a9 | 558 | \r |
559 | //\r | |
560 | // Record the original FragmentTable and count.\r | |
561 | //\r | |
562 | OriginalFragmentTable = FragmentTable;\r | |
563 | OriginalFragmentCount = FragmentCount;\r | |
a1503a32 | 564 | \r |
565 | if (EFI_ERROR (Status)) {\r | |
566 | FreePool (FragmentTable);\r | |
567 | goto ON_EXIT;\r | |
568 | }\r | |
569 | \r | |
570 | //\r | |
571 | // Convert host byte order to network byte order\r | |
572 | //\r | |
705f53a9 | 573 | Ip4NtohHead (*Head);\r |
a1503a32 | 574 | \r |
705f53a9 | 575 | Status = mIpSec->ProcessExt (\r |
a1503a32 | 576 | mIpSec,\r |
577 | IpSb->Controller,\r | |
578 | IP_VERSION_4,\r | |
705f53a9 | 579 | (VOID *) (*Head),\r |
580 | &(*Head)->Protocol,\r | |
da7ff698 | 581 | (VOID **) Options,\r |
705f53a9 | 582 | OptionsLen,\r |
a1503a32 | 583 | (EFI_IPSEC_FRAGMENT_DATA **) (&FragmentTable),\r |
584 | &FragmentCount,\r | |
585 | Direction,\r | |
586 | &RecycleEvent\r | |
587 | );\r | |
588 | //\r | |
589 | // Convert back to host byte order\r | |
590 | //\r | |
705f53a9 | 591 | Ip4NtohHead (*Head);\r |
a1503a32 | 592 | \r |
593 | if (EFI_ERROR (Status)) {\r | |
94b928ca | 594 | FreePool (OriginalFragmentTable);\r |
a1503a32 | 595 | goto ON_EXIT;\r |
596 | }\r | |
597 | \r | |
705f53a9 | 598 | if (OriginalFragmentTable == FragmentTable && OriginalFragmentCount == FragmentCount) {\r |
94b928ca | 599 | //\r |
600 | // For ByPass Packet\r | |
601 | //\r | |
602 | FreePool (FragmentTable);\r | |
705f53a9 | 603 | goto ON_EXIT;\r |
94b928ca | 604 | } else {\r |
605 | //\r | |
606 | // Free the FragmentTable which allocated before calling the IPsec.\r | |
607 | //\r | |
608 | FreePool (OriginalFragmentTable);\r | |
705f53a9 | 609 | }\r |
610 | \r | |
a1503a32 | 611 | if (Direction == EfiIPsecOutBound && TxWrap != NULL) {\r |
612 | \r | |
613 | TxWrap->IpSecRecycleSignal = RecycleEvent;\r | |
614 | TxWrap->Packet = NetbufFromExt (\r | |
615 | FragmentTable,\r | |
616 | FragmentCount,\r | |
617 | IP4_MAX_HEADLEN,\r | |
618 | 0,\r | |
619 | Ip4FreeTxToken,\r | |
620 | TxWrap\r | |
621 | );\r | |
622 | if (TxWrap->Packet == NULL) {\r | |
94b928ca | 623 | //\r |
624 | // Recover the TxWrap->Packet, if meet a error, and the caller will free\r | |
625 | // the TxWrap.\r | |
626 | //\r | |
627 | TxWrap->Packet = *Netbuf;\r | |
a1503a32 | 628 | Status = EFI_OUT_OF_RESOURCES;\r |
629 | goto ON_EXIT;\r | |
630 | }\r | |
631 | \r | |
705f53a9 | 632 | //\r |
633 | // Free orginal Netbuf.\r | |
634 | //\r | |
635 | NetIpSecNetbufFree (*Netbuf);\r | |
a1503a32 | 636 | *Netbuf = TxWrap->Packet;\r |
637 | \r | |
638 | } else {\r | |
639 | \r | |
640 | IpSecWrap = AllocateZeroPool (sizeof (IP4_IPSEC_WRAP));\r | |
641 | \r | |
642 | if (IpSecWrap == NULL) {\r | |
94b928ca | 643 | Status = EFI_OUT_OF_RESOURCES;\r |
644 | gBS->SignalEvent (RecycleEvent);\r | |
a1503a32 | 645 | goto ON_EXIT;\r |
646 | }\r | |
647 | \r | |
648 | IpSecWrap->IpSecRecycleSignal = RecycleEvent;\r | |
649 | IpSecWrap->Packet = Packet;\r | |
650 | Packet = NetbufFromExt (\r | |
651 | FragmentTable, \r | |
652 | FragmentCount, \r | |
653 | IP4_MAX_HEADLEN, \r | |
654 | 0, \r | |
655 | Ip4IpSecFree, \r | |
656 | IpSecWrap\r | |
657 | );\r | |
658 | \r | |
659 | if (Packet == NULL) {\r | |
94b928ca | 660 | Packet = IpSecWrap->Packet;\r |
661 | gBS->SignalEvent (RecycleEvent);\r | |
662 | FreePool (IpSecWrap);\r | |
a1503a32 | 663 | Status = EFI_OUT_OF_RESOURCES;\r |
664 | goto ON_EXIT;\r | |
665 | }\r | |
666 | \r | |
705f53a9 | 667 | if (Direction == EfiIPsecInBound && 0 != CompareMem (*Head, &ZeroHead, sizeof (IP4_HEAD))) {\r |
668 | Ip4PrependHead (Packet, *Head, *Options, *OptionsLen);\r | |
a1503a32 | 669 | Ip4NtohHead (Packet->Ip.Ip4);\r |
705f53a9 | 670 | NetbufTrim (Packet, ((*Head)->HeadLen << 2), TRUE);\r |
a1503a32 | 671 | \r |
672 | CopyMem (\r | |
673 | IP4_GET_CLIP_INFO (Packet),\r | |
674 | IP4_GET_CLIP_INFO (IpSecWrap->Packet),\r | |
675 | sizeof (IP4_CLIP_INFO)\r | |
676 | );\r | |
677 | }\r | |
a1503a32 | 678 | *Netbuf = Packet;\r |
679 | }\r | |
680 | \r | |
681 | ON_EXIT:\r | |
682 | return Status;\r | |
683 | }\r | |
772db4bb | 684 | \r |
685 | /**\r | |
705f53a9 | 686 | Pre-process the IPv4 packet. First validates the IPv4 packet, and\r |
687 | then reassembles packet if it is necessary.\r | |
688 | \r | |
689 | @param[in] IpSb Pointer to IP4_SERVICE.\r | |
690 | @param[in, out] Packet Pointer to the Packet to be processed.\r | |
691 | @param[in] Head Pointer to the IP4_HEAD.\r | |
692 | @param[in] Option Pointer to a buffer which contains the IPv4 option.\r | |
693 | @param[in] OptionLen The length of Option in bytes.\r | |
694 | @param[in] Flag The link layer flag for the packet received, such\r | |
695 | as multicast.\r | |
772db4bb | 696 | \r |
705f53a9 | 697 | @retval EFI_SEUCCESS The recieved packet is in well form.\r |
698 | @retval EFI_INVAILD_PARAMETER The recieved packet is malformed. \r | |
772db4bb | 699 | \r |
700 | **/\r | |
705f53a9 | 701 | EFI_STATUS\r |
702 | Ip4PreProcessPacket (\r | |
703 | IN IP4_SERVICE *IpSb,\r | |
704 | IN OUT NET_BUF **Packet,\r | |
705 | IN IP4_HEAD *Head,\r | |
706 | IN UINT8 *Option,\r | |
707 | IN UINT32 OptionLen, \r | |
708 | IN UINT32 Flag\r | |
709 | ) \r | |
772db4bb | 710 | {\r |
772db4bb | 711 | IP4_CLIP_INFO *Info;\r |
772db4bb | 712 | UINT32 HeadLen;\r |
772db4bb | 713 | UINT32 TotalLen;\r |
714 | UINT16 Checksum;\r | |
772db4bb | 715 | \r |
716 | //\r | |
96e1079f | 717 | // Check that the IP4 header is correctly formatted\r |
772db4bb | 718 | //\r |
705f53a9 | 719 | if ((*Packet)->TotalSize < IP4_MIN_HEADLEN) {\r |
720 | return EFI_INVALID_PARAMETER;\r | |
772db4bb | 721 | }\r |
705f53a9 | 722 | \r |
772db4bb | 723 | HeadLen = (Head->HeadLen << 2);\r |
724 | TotalLen = NTOHS (Head->TotalLen);\r | |
725 | \r | |
726 | //\r | |
727 | // Mnp may deliver frame trailer sequence up, trim it off.\r | |
728 | //\r | |
705f53a9 | 729 | if (TotalLen < (*Packet)->TotalSize) {\r |
730 | NetbufTrim (*Packet, (*Packet)->TotalSize - TotalLen, FALSE);\r | |
772db4bb | 731 | }\r |
732 | \r | |
733 | if ((Head->Ver != 4) || (HeadLen < IP4_MIN_HEADLEN) ||\r | |
705f53a9 | 734 | (TotalLen < HeadLen) || (TotalLen != (*Packet)->TotalSize)) {\r |
735 | return EFI_INVALID_PARAMETER;\r | |
772db4bb | 736 | }\r |
737 | \r | |
738 | //\r | |
739 | // Some OS may send IP packets without checksum.\r | |
740 | //\r | |
687a2e5f | 741 | Checksum = (UINT16) (~NetblockChecksum ((UINT8 *) Head, HeadLen));\r |
772db4bb | 742 | \r |
743 | if ((Head->Checksum != 0) && (Checksum != 0)) {\r | |
705f53a9 | 744 | return EFI_INVALID_PARAMETER;\r |
772db4bb | 745 | }\r |
746 | \r | |
747 | //\r | |
748 | // Convert the IP header to host byte order, then get the per packet info.\r | |
749 | //\r | |
705f53a9 | 750 | (*Packet)->Ip.Ip4 = Ip4NtohHead (Head);\r |
772db4bb | 751 | \r |
705f53a9 | 752 | Info = IP4_GET_CLIP_INFO (*Packet);\r |
772db4bb | 753 | Info->LinkFlag = Flag;\r |
754 | Info->CastType = Ip4GetHostCast (IpSb, Head->Dst, Head->Src);\r | |
755 | Info->Start = (Head->Fragment & IP4_HEAD_OFFSET_MASK) << 3;\r | |
756 | Info->Length = Head->TotalLen - HeadLen;\r | |
757 | Info->End = Info->Start + Info->Length;\r | |
758 | Info->Status = EFI_SUCCESS;\r | |
759 | \r | |
760 | //\r | |
761 | // The packet is destinated to us if the CastType is non-zero.\r | |
762 | //\r | |
763 | if ((Info->CastType == 0) || (Info->End > IP4_MAX_PACKET_SIZE)) {\r | |
705f53a9 | 764 | return EFI_INVALID_PARAMETER;\r |
772db4bb | 765 | }\r |
766 | \r | |
767 | //\r | |
768 | // Validate the options. Don't call the Ip4OptionIsValid if\r | |
769 | // there is no option to save some CPU process.\r | |
770 | //\r | |
705f53a9 | 771 | \r |
772 | if ((OptionLen > 0) && !Ip4OptionIsValid (Option, OptionLen, TRUE)) {\r | |
773 | return EFI_INVALID_PARAMETER;\r | |
772db4bb | 774 | }\r |
775 | \r | |
776 | //\r | |
777 | // Trim the head off, after this point, the packet is headless.\r | |
778 | // and Packet->TotalLen == Info->Length.\r | |
779 | //\r | |
705f53a9 | 780 | NetbufTrim (*Packet, HeadLen, TRUE);\r |
772db4bb | 781 | \r |
782 | //\r | |
783 | // Reassemble the packet if this is a fragment. The packet is a\r | |
784 | // fragment if its head has MF (more fragment) set, or it starts\r | |
785 | // at non-zero byte.\r | |
786 | //\r | |
b2c0a175 | 787 | if (((Head->Fragment & IP4_HEAD_MF_MASK) != 0) || (Info->Start != 0)) {\r |
772db4bb | 788 | //\r |
789 | // Drop the fragment if DF is set but it is fragmented. Gateway\r | |
790 | // need to send a type 4 destination unreache ICMP message here.\r | |
791 | //\r | |
b2c0a175 | 792 | if ((Head->Fragment & IP4_HEAD_DF_MASK) != 0) {\r |
705f53a9 | 793 | return EFI_INVALID_PARAMETER;\r |
772db4bb | 794 | }\r |
795 | \r | |
796 | //\r | |
797 | // The length of all but the last fragments is in the unit of 8 bytes.\r | |
798 | //\r | |
b2c0a175 | 799 | if (((Head->Fragment & IP4_HEAD_MF_MASK) != 0) && (Info->Length % 8 != 0)) {\r |
705f53a9 | 800 | return EFI_INVALID_PARAMETER;\r |
772db4bb | 801 | }\r |
802 | \r | |
705f53a9 | 803 | *Packet = Ip4Reassemble (&IpSb->Assemble, *Packet);\r |
772db4bb | 804 | \r |
805 | //\r | |
806 | // Packet assembly isn't complete, start receive more packet.\r | |
807 | //\r | |
705f53a9 | 808 | if (*Packet == NULL) {\r |
809 | return EFI_INVALID_PARAMETER;\r | |
772db4bb | 810 | }\r |
811 | }\r | |
705f53a9 | 812 | \r |
813 | return EFI_SUCCESS;\r | |
814 | }\r | |
815 | \r | |
816 | /**\r | |
817 | The IP4 input routine. It is called by the IP4_INTERFACE when a\r | |
818 | IP4 fragment is received from MNP.\r | |
819 | \r | |
820 | @param[in] Ip4Instance The IP4 child that request the receive, most like\r | |
821 | it is NULL.\r | |
822 | @param[in] Packet The IP4 packet received.\r | |
823 | @param[in] IoStatus The return status of receive request.\r | |
824 | @param[in] Flag The link layer flag for the packet received, such\r | |
825 | as multicast.\r | |
826 | @param[in] Context The IP4 service instance that own the MNP.\r | |
827 | \r | |
828 | **/\r | |
829 | VOID\r | |
830 | Ip4AccpetFrame (\r | |
831 | IN IP4_PROTOCOL *Ip4Instance,\r | |
832 | IN NET_BUF *Packet,\r | |
833 | IN EFI_STATUS IoStatus,\r | |
834 | IN UINT32 Flag,\r | |
835 | IN VOID *Context\r | |
836 | )\r | |
837 | {\r | |
838 | IP4_SERVICE *IpSb;\r | |
839 | IP4_HEAD *Head;\r | |
840 | EFI_STATUS Status;\r | |
841 | IP4_HEAD ZeroHead;\r | |
842 | UINT8 *Option;\r | |
843 | UINT32 OptionLen;\r | |
844 | \r | |
845 | IpSb = (IP4_SERVICE *) Context;\r | |
846 | Option = NULL;\r | |
847 | \r | |
75dce340 | 848 | if (EFI_ERROR (IoStatus) || (IpSb->State == IP4_SERVICE_DESTROY)) {\r |
705f53a9 | 849 | goto DROP;\r |
850 | }\r | |
851 | \r | |
852 | Head = (IP4_HEAD *) NetbufGetByte (Packet, 0, NULL); \r | |
853 | OptionLen = (Head->HeadLen << 2) - IP4_MIN_HEADLEN;\r | |
854 | if (OptionLen > 0) {\r | |
855 | Option = (UINT8 *) (Head + 1);\r | |
856 | }\r | |
857 | \r | |
858 | //\r | |
859 | // Validate packet format and reassemble packet if it is necessary.\r | |
860 | //\r | |
861 | Status = Ip4PreProcessPacket (\r | |
862 | IpSb, \r | |
863 | &Packet, \r | |
864 | Head, \r | |
865 | Option,\r | |
866 | OptionLen, \r | |
867 | Flag\r | |
868 | );\r | |
869 | \r | |
870 | if (EFI_ERROR (Status)) {\r | |
871 | goto RESTART;\r | |
872 | }\r | |
772db4bb | 873 | \r |
874 | //\r | |
a1503a32 | 875 | // After trim off, the packet is a esp/ah/udp/tcp/icmp6 net buffer,\r |
876 | // and no need consider any other ahead ext headers.\r | |
877 | //\r | |
878 | Status = Ip4IpSecProcessPacket (\r | |
94b928ca | 879 | IpSb,\r |
880 | &Head,\r | |
881 | &Packet,\r | |
705f53a9 | 882 | &Option,\r |
94b928ca | 883 | &OptionLen,\r |
a1503a32 | 884 | EfiIPsecInBound,\r |
885 | NULL\r | |
886 | );\r | |
887 | \r | |
705f53a9 | 888 | if (EFI_ERROR (Status)) {\r |
a1503a32 | 889 | goto RESTART;\r |
890 | }\r | |
705f53a9 | 891 | \r |
892 | //\r | |
893 | // If the packet is protected by tunnel mode, parse the inner Ip Packet.\r | |
894 | //\r | |
895 | ZeroMem (&ZeroHead, sizeof (IP4_HEAD));\r | |
896 | if (0 == CompareMem (Head, &ZeroHead, sizeof (IP4_HEAD))) {\r | |
772db4bb | 897 | // Packet may have been changed. Head, HeadLen, TotalLen, and\r |
898 | // info must be reloaded bofore use. The ownership of the packet\r | |
899 | // is transfered to the packet process logic.\r | |
900 | //\r | |
705f53a9 | 901 | Head = (IP4_HEAD *) NetbufGetByte (Packet, 0, NULL);\r |
902 | Status = Ip4PreProcessPacket (\r | |
94b928ca | 903 | IpSb,\r |
904 | &Packet,\r | |
905 | Head,\r | |
705f53a9 | 906 | Option,\r |
94b928ca | 907 | OptionLen,\r |
705f53a9 | 908 | Flag\r |
909 | );\r | |
910 | if (EFI_ERROR (Status)) {\r | |
911 | goto RESTART;\r | |
912 | }\r | |
913 | }\r | |
fbe12b79 ED |
914 | \r |
915 | ASSERT (Packet != NULL);\r | |
f6b7393c | 916 | Head = Packet->Ip.Ip4;\r |
772db4bb | 917 | IP4_GET_CLIP_INFO (Packet)->Status = EFI_SUCCESS;\r |
918 | \r | |
919 | switch (Head->Protocol) {\r | |
f6b7393c | 920 | case EFI_IP_PROTO_ICMP:\r |
772db4bb | 921 | Ip4IcmpHandle (IpSb, Head, Packet);\r |
922 | break;\r | |
923 | \r | |
924 | case IP4_PROTO_IGMP:\r | |
925 | Ip4IgmpHandle (IpSb, Head, Packet);\r | |
926 | break;\r | |
927 | \r | |
928 | default:\r | |
929 | Ip4Demultiplex (IpSb, Head, Packet);\r | |
930 | }\r | |
931 | \r | |
932 | Packet = NULL;\r | |
933 | \r | |
36ee91ca | 934 | //\r |
935 | // Dispatch the DPCs queued by the NotifyFunction of the rx token's events\r | |
936 | // which are signaled with received data.\r | |
937 | //\r | |
d8d26fb2 | 938 | DispatchDpc ();\r |
36ee91ca | 939 | \r |
772db4bb | 940 | RESTART:\r |
941 | Ip4ReceiveFrame (IpSb->DefaultInterface, NULL, Ip4AccpetFrame, IpSb);\r | |
942 | \r | |
943 | DROP:\r | |
944 | if (Packet != NULL) {\r | |
945 | NetbufFree (Packet);\r | |
946 | }\r | |
947 | \r | |
948 | return ;\r | |
949 | }\r | |
950 | \r | |
951 | \r | |
952 | /**\r | |
953 | Check whether this IP child accepts the packet.\r | |
954 | \r | |
3e8c18da | 955 | @param[in] IpInstance The IP child to check\r |
956 | @param[in] Head The IP header of the packet\r | |
957 | @param[in] Packet The data of the packet\r | |
772db4bb | 958 | \r |
96e1079f | 959 | @retval TRUE If the child wants to receive the packet.\r |
960 | @retval FALSE Otherwise.\r | |
772db4bb | 961 | \r |
962 | **/\r | |
963 | BOOLEAN\r | |
964 | Ip4InstanceFrameAcceptable (\r | |
965 | IN IP4_PROTOCOL *IpInstance,\r | |
966 | IN IP4_HEAD *Head,\r | |
967 | IN NET_BUF *Packet\r | |
968 | )\r | |
969 | {\r | |
970 | IP4_ICMP_ERROR_HEAD Icmp;\r | |
971 | EFI_IP4_CONFIG_DATA *Config;\r | |
972 | IP4_CLIP_INFO *Info;\r | |
973 | UINT16 Proto;\r | |
974 | UINT32 Index;\r | |
975 | \r | |
976 | Config = &IpInstance->ConfigData;\r | |
977 | \r | |
978 | //\r | |
979 | // Dirty trick for the Tiano UEFI network stack implmentation. If\r | |
980 | // ReceiveTimeout == -1, the receive of the packet for this instance\r | |
96e1079f | 981 | // is disabled. The UEFI spec don't have such capability. We add\r |
772db4bb | 982 | // this to improve the performance because IP will make a copy of\r |
983 | // the received packet for each accepting instance. Some IP instances\r | |
984 | // used by UDP/TCP only send packets, they don't wants to receive.\r | |
985 | //\r | |
986 | if (Config->ReceiveTimeout == (UINT32)(-1)) {\r | |
987 | return FALSE;\r | |
988 | }\r | |
989 | \r | |
990 | if (Config->AcceptPromiscuous) {\r | |
991 | return TRUE;\r | |
992 | }\r | |
993 | \r | |
994 | //\r | |
995 | // Use protocol from the IP header embedded in the ICMP error\r | |
996 | // message to filter, instead of ICMP itself. ICMP handle will\r | |
9899d8b6 | 997 | // call Ip4Demultiplex to deliver ICMP errors.\r |
772db4bb | 998 | //\r |
999 | Proto = Head->Protocol;\r | |
1000 | \r | |
9899d8b6 | 1001 | if ((Proto == EFI_IP_PROTO_ICMP) && (!Config->AcceptAnyProtocol) && (Proto != Config->DefaultProtocol)) {\r |
772db4bb | 1002 | NetbufCopy (Packet, 0, sizeof (Icmp.Head), (UINT8 *) &Icmp.Head);\r |
1003 | \r | |
1004 | if (mIcmpClass[Icmp.Head.Type].IcmpClass == ICMP_ERROR_MESSAGE) {\r | |
1005 | if (!Config->AcceptIcmpErrors) {\r | |
1006 | return FALSE;\r | |
1007 | }\r | |
1008 | \r | |
1009 | NetbufCopy (Packet, 0, sizeof (Icmp), (UINT8 *) &Icmp);\r | |
1010 | Proto = Icmp.IpHead.Protocol;\r | |
1011 | }\r | |
1012 | }\r | |
1013 | \r | |
1014 | //\r | |
1015 | // Match the protocol\r | |
1016 | //\r | |
1017 | if (!Config->AcceptAnyProtocol && (Proto != Config->DefaultProtocol)) {\r | |
1018 | return FALSE;\r | |
1019 | }\r | |
1020 | \r | |
1021 | //\r | |
1022 | // Check for broadcast, the caller has computed the packet's\r | |
1023 | // cast type for this child's interface.\r | |
1024 | //\r | |
1025 | Info = IP4_GET_CLIP_INFO (Packet);\r | |
1026 | \r | |
1027 | if (IP4_IS_BROADCAST (Info->CastType)) {\r | |
1028 | return Config->AcceptBroadcast;\r | |
1029 | }\r | |
1030 | \r | |
1031 | //\r | |
1032 | // If it is a multicast packet, check whether we are in the group.\r | |
1033 | //\r | |
1034 | if (Info->CastType == IP4_MULTICAST) {\r | |
1035 | //\r | |
1036 | // Receive the multicast if the instance wants to receive all packets.\r | |
1037 | //\r | |
1038 | if (!IpInstance->ConfigData.UseDefaultAddress && (IpInstance->Interface->Ip == 0)) {\r | |
1039 | return TRUE;\r | |
1040 | }\r | |
1041 | \r | |
1042 | for (Index = 0; Index < IpInstance->GroupCount; Index++) {\r | |
1043 | if (IpInstance->Groups[Index] == HTONL (Head->Dst)) {\r | |
1044 | break;\r | |
1045 | }\r | |
1046 | }\r | |
1047 | \r | |
1048 | return (BOOLEAN)(Index < IpInstance->GroupCount);\r | |
1049 | }\r | |
1050 | \r | |
1051 | return TRUE;\r | |
1052 | }\r | |
1053 | \r | |
1054 | \r | |
1055 | /**\r | |
1056 | Enqueue a shared copy of the packet to the IP4 child if the\r | |
1057 | packet is acceptable to it. Here the data of the packet is\r | |
1058 | shared, but the net buffer isn't.\r | |
1059 | \r | |
3e8c18da | 1060 | @param[in] IpInstance The IP4 child to enqueue the packet to\r |
1061 | @param[in] Head The IP header of the received packet\r | |
1062 | @param[in] Packet The data of the received packet\r | |
772db4bb | 1063 | \r |
1064 | @retval EFI_NOT_STARTED The IP child hasn't been configured.\r | |
1065 | @retval EFI_INVALID_PARAMETER The child doesn't want to receive the packet\r | |
1066 | @retval EFI_OUT_OF_RESOURCES Failed to allocate some resource\r | |
1067 | @retval EFI_SUCCESS A shared copy the packet is enqueued to the child.\r | |
1068 | \r | |
1069 | **/\r | |
1070 | EFI_STATUS\r | |
1071 | Ip4InstanceEnquePacket (\r | |
1072 | IN IP4_PROTOCOL *IpInstance,\r | |
1073 | IN IP4_HEAD *Head,\r | |
1074 | IN NET_BUF *Packet\r | |
1075 | )\r | |
1076 | {\r | |
1077 | IP4_CLIP_INFO *Info;\r | |
1078 | NET_BUF *Clone;\r | |
1079 | \r | |
1080 | //\r | |
1081 | // Check whether the packet is acceptable to this instance.\r | |
1082 | //\r | |
1083 | if (IpInstance->State != IP4_STATE_CONFIGED) {\r | |
1084 | return EFI_NOT_STARTED;\r | |
1085 | }\r | |
1086 | \r | |
1087 | if (!Ip4InstanceFrameAcceptable (IpInstance, Head, Packet)) {\r | |
1088 | return EFI_INVALID_PARAMETER;\r | |
1089 | }\r | |
1090 | \r | |
1091 | //\r | |
1092 | // Enque a shared copy of the packet.\r | |
1093 | //\r | |
1094 | Clone = NetbufClone (Packet);\r | |
1095 | \r | |
1096 | if (Clone == NULL) {\r | |
1097 | return EFI_OUT_OF_RESOURCES;\r | |
1098 | }\r | |
1099 | \r | |
1100 | //\r | |
1101 | // Set the receive time out for the assembled packet. If it expires,\r | |
1102 | // packet will be removed from the queue.\r | |
1103 | //\r | |
1104 | Info = IP4_GET_CLIP_INFO (Clone);\r | |
1105 | Info->Life = IP4_US_TO_SEC (IpInstance->ConfigData.ReceiveTimeout);\r | |
1106 | \r | |
e48e37fc | 1107 | InsertTailList (&IpInstance->Received, &Clone->List);\r |
772db4bb | 1108 | return EFI_SUCCESS;\r |
1109 | }\r | |
1110 | \r | |
1111 | \r | |
1112 | /**\r | |
1113 | The signal handle of IP4's recycle event. It is called back\r | |
1114 | when the upper layer release the packet.\r | |
1115 | \r | |
3e8c18da | 1116 | @param Event The IP4's recycle event.\r |
1117 | @param Context The context of the handle, which is a\r | |
1118 | IP4_RXDATA_WRAP\r | |
772db4bb | 1119 | \r |
1120 | **/\r | |
772db4bb | 1121 | VOID\r |
1122 | EFIAPI\r | |
1123 | Ip4OnRecyclePacket (\r | |
1124 | IN EFI_EVENT Event,\r | |
1125 | IN VOID *Context\r | |
1126 | )\r | |
1127 | {\r | |
1128 | IP4_RXDATA_WRAP *Wrap;\r | |
1129 | \r | |
1130 | Wrap = (IP4_RXDATA_WRAP *) Context;\r | |
1131 | \r | |
e48e37fc | 1132 | EfiAcquireLockOrFail (&Wrap->IpInstance->RecycleLock);\r |
1133 | RemoveEntryList (&Wrap->Link);\r | |
1134 | EfiReleaseLock (&Wrap->IpInstance->RecycleLock);\r | |
772db4bb | 1135 | \r |
1136 | ASSERT (!NET_BUF_SHARED (Wrap->Packet));\r | |
1137 | NetbufFree (Wrap->Packet);\r | |
1138 | \r | |
1139 | gBS->CloseEvent (Wrap->RxData.RecycleSignal);\r | |
766c7483 | 1140 | FreePool (Wrap);\r |
772db4bb | 1141 | }\r |
1142 | \r | |
1143 | \r | |
1144 | /**\r | |
1145 | Wrap the received packet to a IP4_RXDATA_WRAP, which will be\r | |
1146 | delivered to the upper layer. Each IP4 child that accepts the\r | |
1147 | packet will get a not-shared copy of the packet which is wrapped\r | |
1148 | in the IP4_RXDATA_WRAP. The IP4_RXDATA_WRAP->RxData is passed\r | |
1149 | to the upper layer. Upper layer will signal the recycle event in\r | |
1150 | it when it is done with the packet.\r | |
1151 | \r | |
3e8c18da | 1152 | @param[in] IpInstance The IP4 child to receive the packet\r |
1153 | @param[in] Packet The packet to deliver up.\r | |
772db4bb | 1154 | \r |
3e8c18da | 1155 | @retval Wrap if warp the packet succeed.\r |
1156 | @retval NULL failed to wrap the packet .\r | |
772db4bb | 1157 | \r |
1158 | **/\r | |
1159 | IP4_RXDATA_WRAP *\r | |
1160 | Ip4WrapRxData (\r | |
1161 | IN IP4_PROTOCOL *IpInstance,\r | |
1162 | IN NET_BUF *Packet\r | |
1163 | )\r | |
1164 | {\r | |
1165 | IP4_RXDATA_WRAP *Wrap;\r | |
1166 | EFI_IP4_RECEIVE_DATA *RxData;\r | |
1167 | EFI_STATUS Status;\r | |
1168 | \r | |
e48e37fc | 1169 | Wrap = AllocatePool (IP4_RXDATA_WRAP_SIZE (Packet->BlockOpNum));\r |
772db4bb | 1170 | \r |
1171 | if (Wrap == NULL) {\r | |
1172 | return NULL;\r | |
1173 | }\r | |
1174 | \r | |
e48e37fc | 1175 | InitializeListHead (&Wrap->Link);\r |
772db4bb | 1176 | \r |
1177 | Wrap->IpInstance = IpInstance;\r | |
1178 | Wrap->Packet = Packet;\r | |
1179 | RxData = &Wrap->RxData;\r | |
1180 | \r | |
e48e37fc | 1181 | ZeroMem (&RxData->TimeStamp, sizeof (EFI_TIME));\r |
772db4bb | 1182 | \r |
1183 | Status = gBS->CreateEvent (\r | |
1184 | EVT_NOTIFY_SIGNAL,\r | |
e48e37fc | 1185 | TPL_NOTIFY,\r |
772db4bb | 1186 | Ip4OnRecyclePacket,\r |
1187 | Wrap,\r | |
1188 | &RxData->RecycleSignal\r | |
1189 | );\r | |
1190 | \r | |
1191 | if (EFI_ERROR (Status)) {\r | |
766c7483 | 1192 | FreePool (Wrap);\r |
772db4bb | 1193 | return NULL;\r |
1194 | }\r | |
1195 | \r | |
f6b7393c | 1196 | ASSERT (Packet->Ip.Ip4 != NULL);\r |
772db4bb | 1197 | \r |
1198 | //\r | |
1199 | // The application expects a network byte order header.\r | |
1200 | //\r | |
f6b7393c | 1201 | RxData->HeaderLength = (Packet->Ip.Ip4->HeadLen << 2);\r |
1202 | RxData->Header = (EFI_IP4_HEADER *) Ip4NtohHead (Packet->Ip.Ip4);\r | |
772db4bb | 1203 | \r |
1204 | RxData->OptionsLength = RxData->HeaderLength - IP4_MIN_HEADLEN;\r | |
1205 | RxData->Options = NULL;\r | |
1206 | \r | |
1207 | if (RxData->OptionsLength != 0) {\r | |
1208 | RxData->Options = (VOID *) (RxData->Header + 1);\r | |
1209 | }\r | |
1210 | \r | |
1211 | RxData->DataLength = Packet->TotalSize;\r | |
1212 | \r | |
1213 | //\r | |
1214 | // Build the fragment table to be delivered up.\r | |
1215 | //\r | |
1216 | RxData->FragmentCount = Packet->BlockOpNum;\r | |
1217 | NetbufBuildExt (Packet, (NET_FRAGMENT *) RxData->FragmentTable, &RxData->FragmentCount);\r | |
1218 | \r | |
1219 | return Wrap;\r | |
1220 | }\r | |
1221 | \r | |
1222 | \r | |
1223 | /**\r | |
1224 | Deliver the received packets to upper layer if there are both received\r | |
1225 | requests and enqueued packets. If the enqueued packet is shared, it will\r | |
1226 | duplicate it to a non-shared packet, release the shared packet, then\r | |
1227 | deliver the non-shared packet up.\r | |
1228 | \r | |
3e8c18da | 1229 | @param[in] IpInstance The IP child to deliver the packet up.\r |
772db4bb | 1230 | \r |
1231 | @retval EFI_OUT_OF_RESOURCES Failed to allocate resources to deliver the\r | |
1232 | packets.\r | |
1233 | @retval EFI_SUCCESS All the enqueued packets that can be delivered\r | |
1234 | are delivered up.\r | |
1235 | \r | |
1236 | **/\r | |
1237 | EFI_STATUS\r | |
1238 | Ip4InstanceDeliverPacket (\r | |
1239 | IN IP4_PROTOCOL *IpInstance\r | |
1240 | )\r | |
1241 | {\r | |
1242 | EFI_IP4_COMPLETION_TOKEN *Token;\r | |
1243 | IP4_RXDATA_WRAP *Wrap;\r | |
1244 | NET_BUF *Packet;\r | |
1245 | NET_BUF *Dup;\r | |
1246 | UINT8 *Head;\r | |
1247 | \r | |
1248 | //\r | |
1249 | // Deliver a packet if there are both a packet and a receive token.\r | |
1250 | //\r | |
e48e37fc | 1251 | while (!IsListEmpty (&IpInstance->Received) &&\r |
772db4bb | 1252 | !NetMapIsEmpty (&IpInstance->RxTokens)) {\r |
1253 | \r | |
1254 | Packet = NET_LIST_HEAD (&IpInstance->Received, NET_BUF, List);\r | |
1255 | \r | |
1256 | if (!NET_BUF_SHARED (Packet)) {\r | |
1257 | //\r | |
1258 | // If this is the only instance that wants the packet, wrap it up.\r | |
1259 | //\r | |
1260 | Wrap = Ip4WrapRxData (IpInstance, Packet);\r | |
1261 | \r | |
1262 | if (Wrap == NULL) {\r | |
1263 | return EFI_OUT_OF_RESOURCES;\r | |
1264 | }\r | |
1265 | \r | |
e48e37fc | 1266 | RemoveEntryList (&Packet->List);\r |
772db4bb | 1267 | \r |
1268 | } else {\r | |
1269 | //\r | |
1270 | // Create a duplicated packet if this packet is shared\r | |
1271 | //\r | |
1272 | Dup = NetbufDuplicate (Packet, NULL, IP4_MAX_HEADLEN);\r | |
1273 | \r | |
1274 | if (Dup == NULL) {\r | |
1275 | return EFI_OUT_OF_RESOURCES;\r | |
1276 | }\r | |
1277 | \r | |
1278 | //\r | |
1279 | // Copy the IP head over. The packet to deliver up is\r | |
1280 | // headless. Trim the head off after copy. The IP head\r | |
1281 | // may be not continuous before the data.\r | |
1282 | //\r | |
04eb20aa ED |
1283 | Head = NetbufAllocSpace (Dup, IP4_MAX_HEADLEN, NET_BUF_HEAD);\r |
1284 | ASSERT (Head != NULL);\r | |
1285 | \r | |
f6b7393c | 1286 | Dup->Ip.Ip4 = (IP4_HEAD *) Head;\r |
772db4bb | 1287 | \r |
f6b7393c | 1288 | CopyMem (Head, Packet->Ip.Ip4, Packet->Ip.Ip4->HeadLen << 2);\r |
772db4bb | 1289 | NetbufTrim (Dup, IP4_MAX_HEADLEN, TRUE);\r |
1290 | \r | |
1291 | Wrap = Ip4WrapRxData (IpInstance, Dup);\r | |
1292 | \r | |
1293 | if (Wrap == NULL) {\r | |
1294 | NetbufFree (Dup);\r | |
1295 | return EFI_OUT_OF_RESOURCES;\r | |
1296 | }\r | |
1297 | \r | |
e48e37fc | 1298 | RemoveEntryList (&Packet->List);\r |
772db4bb | 1299 | NetbufFree (Packet);\r |
1300 | \r | |
1301 | Packet = Dup;\r | |
1302 | }\r | |
1303 | \r | |
1304 | //\r | |
1305 | // Insert it into the delivered packet, then get a user's\r | |
1306 | // receive token, pass the wrapped packet up.\r | |
1307 | //\r | |
e48e37fc | 1308 | EfiAcquireLockOrFail (&IpInstance->RecycleLock);\r |
1309 | InsertHeadList (&IpInstance->Delivered, &Wrap->Link);\r | |
1310 | EfiReleaseLock (&IpInstance->RecycleLock);\r | |
772db4bb | 1311 | \r |
1312 | Token = NetMapRemoveHead (&IpInstance->RxTokens, NULL);\r | |
1313 | Token->Status = IP4_GET_CLIP_INFO (Packet)->Status;\r | |
1314 | Token->Packet.RxData = &Wrap->RxData;\r | |
1315 | \r | |
1316 | gBS->SignalEvent (Token->Event);\r | |
1317 | }\r | |
1318 | \r | |
1319 | return EFI_SUCCESS;\r | |
1320 | }\r | |
1321 | \r | |
1322 | \r | |
1323 | /**\r | |
1324 | Enqueue a received packet to all the IP children that share\r | |
1325 | the same interface.\r | |
1326 | \r | |
3e8c18da | 1327 | @param[in] IpSb The IP4 service instance that receive the packet\r |
1328 | @param[in] Head The header of the received packet\r | |
1329 | @param[in] Packet The data of the received packet\r | |
1330 | @param[in] IpIf The interface to enqueue the packet to\r | |
772db4bb | 1331 | \r |
1332 | @return The number of the IP4 children that accepts the packet\r | |
1333 | \r | |
1334 | **/\r | |
1335 | INTN\r | |
1336 | Ip4InterfaceEnquePacket (\r | |
1337 | IN IP4_SERVICE *IpSb,\r | |
1338 | IN IP4_HEAD *Head,\r | |
1339 | IN NET_BUF *Packet,\r | |
1340 | IN IP4_INTERFACE *IpIf\r | |
1341 | )\r | |
1342 | {\r | |
1343 | IP4_PROTOCOL *IpInstance;\r | |
1344 | IP4_CLIP_INFO *Info;\r | |
e48e37fc | 1345 | LIST_ENTRY *Entry;\r |
772db4bb | 1346 | INTN Enqueued;\r |
1347 | INTN LocalType;\r | |
1348 | INTN SavedType;\r | |
1349 | \r | |
1350 | //\r | |
1351 | // First, check that the packet is acceptable to this interface\r | |
1352 | // and find the local cast type for the interface. A packet sent\r | |
1353 | // to say 192.168.1.1 should NOT be delliever to 10.0.0.1 unless\r | |
1354 | // promiscuous receiving.\r | |
1355 | //\r | |
1356 | LocalType = 0;\r | |
1357 | Info = IP4_GET_CLIP_INFO (Packet);\r | |
1358 | \r | |
1359 | if ((Info->CastType == IP4_MULTICAST) || (Info->CastType == IP4_LOCAL_BROADCAST)) {\r | |
1360 | //\r | |
1361 | // If the CastType is multicast, don't need to filter against\r | |
1362 | // the group address here, Ip4InstanceFrameAcceptable will do\r | |
1363 | // that later.\r | |
1364 | //\r | |
1365 | LocalType = Info->CastType;\r | |
1366 | \r | |
1367 | } else {\r | |
1368 | //\r | |
1369 | // Check the destination againist local IP. If the station\r | |
1370 | // address is 0.0.0.0, it means receiving all the IP destined\r | |
1371 | // to local non-zero IP. Otherwise, it is necessary to compare\r | |
1372 | // the destination to the interface's IP address.\r | |
1373 | //\r | |
1374 | if (IpIf->Ip == IP4_ALLZERO_ADDRESS) {\r | |
1375 | LocalType = IP4_LOCAL_HOST;\r | |
1376 | \r | |
1377 | } else {\r | |
1378 | LocalType = Ip4GetNetCast (Head->Dst, IpIf);\r | |
1379 | \r | |
1380 | if ((LocalType == 0) && IpIf->PromiscRecv) {\r | |
1381 | LocalType = IP4_PROMISCUOUS;\r | |
1382 | }\r | |
1383 | }\r | |
1384 | }\r | |
1385 | \r | |
1386 | if (LocalType == 0) {\r | |
1387 | return 0;\r | |
1388 | }\r | |
1389 | \r | |
1390 | //\r | |
1391 | // Iterate through the ip instances on the interface, enqueue\r | |
1392 | // the packet if filter passed. Save the original cast type,\r | |
1393 | // and pass the local cast type to the IP children on the\r | |
1394 | // interface. The global cast type will be restored later.\r | |
1395 | //\r | |
1396 | SavedType = Info->CastType;\r | |
1397 | Info->CastType = LocalType;\r | |
1398 | \r | |
1399 | Enqueued = 0;\r | |
1400 | \r | |
1401 | NET_LIST_FOR_EACH (Entry, &IpIf->IpInstances) {\r | |
1402 | IpInstance = NET_LIST_USER_STRUCT (Entry, IP4_PROTOCOL, AddrLink);\r | |
1403 | NET_CHECK_SIGNATURE (IpInstance, IP4_PROTOCOL_SIGNATURE);\r | |
1404 | \r | |
1405 | if (Ip4InstanceEnquePacket (IpInstance, Head, Packet) == EFI_SUCCESS) {\r | |
1406 | Enqueued++;\r | |
1407 | }\r | |
1408 | }\r | |
1409 | \r | |
1410 | Info->CastType = SavedType;\r | |
1411 | return Enqueued;\r | |
1412 | }\r | |
1413 | \r | |
1414 | \r | |
1415 | /**\r | |
1416 | Deliver the packet for each IP4 child on the interface.\r | |
1417 | \r | |
3e8c18da | 1418 | @param[in] IpSb The IP4 service instance that received the packet\r |
1419 | @param[in] IpIf The IP4 interface to deliver the packet.\r | |
772db4bb | 1420 | \r |
1421 | @retval EFI_SUCCESS It always returns EFI_SUCCESS now\r | |
1422 | \r | |
1423 | **/\r | |
1424 | EFI_STATUS\r | |
1425 | Ip4InterfaceDeliverPacket (\r | |
1426 | IN IP4_SERVICE *IpSb,\r | |
1427 | IN IP4_INTERFACE *IpIf\r | |
1428 | )\r | |
1429 | {\r | |
1430 | IP4_PROTOCOL *Ip4Instance;\r | |
e48e37fc | 1431 | LIST_ENTRY *Entry;\r |
772db4bb | 1432 | \r |
1433 | NET_LIST_FOR_EACH (Entry, &IpIf->IpInstances) {\r | |
1434 | Ip4Instance = NET_LIST_USER_STRUCT (Entry, IP4_PROTOCOL, AddrLink);\r | |
1435 | Ip4InstanceDeliverPacket (Ip4Instance);\r | |
1436 | }\r | |
1437 | \r | |
1438 | return EFI_SUCCESS;\r | |
1439 | }\r | |
1440 | \r | |
1441 | \r | |
1442 | /**\r | |
1443 | Demultiple the packet. the packet delivery is processed in two\r | |
1444 | passes. The first pass will enque a shared copy of the packet\r | |
1445 | to each IP4 child that accepts the packet. The second pass will\r | |
1446 | deliver a non-shared copy of the packet to each IP4 child that\r | |
1447 | has pending receive requests. Data is copied if more than one\r | |
96e1079f | 1448 | child wants to consume the packet because each IP child needs\r |
772db4bb | 1449 | its own copy of the packet to make changes.\r |
1450 | \r | |
3e8c18da | 1451 | @param[in] IpSb The IP4 service instance that received the packet\r |
1452 | @param[in] Head The header of the received packet\r | |
1453 | @param[in] Packet The data of the received packet\r | |
772db4bb | 1454 | \r |
1455 | @retval EFI_NOT_FOUND No IP child accepts the packet\r | |
1456 | @retval EFI_SUCCESS The packet is enqueued or delivered to some IP\r | |
1457 | children.\r | |
1458 | \r | |
1459 | **/\r | |
1460 | EFI_STATUS\r | |
1461 | Ip4Demultiplex (\r | |
1462 | IN IP4_SERVICE *IpSb,\r | |
1463 | IN IP4_HEAD *Head,\r | |
1464 | IN NET_BUF *Packet\r | |
1465 | )\r | |
1466 | {\r | |
e48e37fc | 1467 | LIST_ENTRY *Entry;\r |
772db4bb | 1468 | IP4_INTERFACE *IpIf;\r |
1469 | INTN Enqueued;\r | |
1470 | \r | |
1471 | //\r | |
1472 | // Two pass delivery: first, enque a shared copy of the packet\r | |
1473 | // to each instance that accept the packet.\r | |
1474 | //\r | |
1475 | Enqueued = 0;\r | |
1476 | \r | |
1477 | NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) {\r | |
1478 | IpIf = NET_LIST_USER_STRUCT (Entry, IP4_INTERFACE, Link);\r | |
1479 | \r | |
1480 | if (IpIf->Configured) {\r | |
1481 | Enqueued += Ip4InterfaceEnquePacket (IpSb, Head, Packet, IpIf);\r | |
1482 | }\r | |
1483 | }\r | |
1484 | \r | |
1485 | //\r | |
1486 | // Second: deliver a duplicate of the packet to each instance.\r | |
1487 | // Release the local reference first, so that the last instance\r | |
1488 | // getting the packet will not copy the data.\r | |
1489 | //\r | |
1490 | NetbufFree (Packet);\r | |
1491 | \r | |
1492 | if (Enqueued == 0) {\r | |
1493 | return EFI_NOT_FOUND;\r | |
1494 | }\r | |
1495 | \r | |
1496 | NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) {\r | |
1497 | IpIf = NET_LIST_USER_STRUCT (Entry, IP4_INTERFACE, Link);\r | |
1498 | \r | |
1499 | if (IpIf->Configured) {\r | |
1500 | Ip4InterfaceDeliverPacket (IpSb, IpIf);\r | |
1501 | }\r | |
1502 | }\r | |
1503 | \r | |
1504 | return EFI_SUCCESS;\r | |
1505 | }\r | |
1506 | \r | |
1507 | \r | |
1508 | /**\r | |
1509 | Timeout the fragment and enqueued packets.\r | |
1510 | \r | |
3e8c18da | 1511 | @param[in] IpSb The IP4 service instance to timeout\r |
772db4bb | 1512 | \r |
1513 | **/\r | |
1514 | VOID\r | |
1515 | Ip4PacketTimerTicking (\r | |
1516 | IN IP4_SERVICE *IpSb\r | |
1517 | )\r | |
1518 | {\r | |
e48e37fc | 1519 | LIST_ENTRY *InstanceEntry;\r |
1520 | LIST_ENTRY *Entry;\r | |
1521 | LIST_ENTRY *Next;\r | |
772db4bb | 1522 | IP4_PROTOCOL *IpInstance;\r |
1523 | IP4_ASSEMBLE_ENTRY *Assemble;\r | |
1524 | NET_BUF *Packet;\r | |
1525 | IP4_CLIP_INFO *Info;\r | |
1526 | UINT32 Index;\r | |
1527 | \r | |
1528 | //\r | |
1529 | // First, time out the fragments. The packet's life is counting down\r | |
1530 | // once the first-arrived fragment was received.\r | |
1531 | //\r | |
1532 | for (Index = 0; Index < IP4_ASSEMLE_HASH_SIZE; Index++) {\r | |
1533 | NET_LIST_FOR_EACH_SAFE (Entry, Next, &IpSb->Assemble.Bucket[Index]) {\r | |
1534 | Assemble = NET_LIST_USER_STRUCT (Entry, IP4_ASSEMBLE_ENTRY, Link);\r | |
1535 | \r | |
1536 | if ((Assemble->Life > 0) && (--Assemble->Life == 0)) {\r | |
e48e37fc | 1537 | RemoveEntryList (Entry);\r |
772db4bb | 1538 | Ip4FreeAssembleEntry (Assemble);\r |
1539 | }\r | |
1540 | }\r | |
1541 | }\r | |
1542 | \r | |
1543 | NET_LIST_FOR_EACH (InstanceEntry, &IpSb->Children) {\r | |
1544 | IpInstance = NET_LIST_USER_STRUCT (InstanceEntry, IP4_PROTOCOL, Link);\r | |
1545 | \r | |
1546 | //\r | |
1547 | // Second, time out the assembled packets enqueued on each IP child.\r | |
1548 | //\r | |
1549 | NET_LIST_FOR_EACH_SAFE (Entry, Next, &IpInstance->Received) {\r | |
1550 | Packet = NET_LIST_USER_STRUCT (Entry, NET_BUF, List);\r | |
1551 | Info = IP4_GET_CLIP_INFO (Packet);\r | |
1552 | \r | |
1553 | if ((Info->Life > 0) && (--Info->Life == 0)) {\r | |
e48e37fc | 1554 | RemoveEntryList (Entry);\r |
772db4bb | 1555 | NetbufFree (Packet);\r |
1556 | }\r | |
1557 | }\r | |
1558 | \r | |
1559 | //\r | |
1560 | // Third: time out the transmitted packets.\r | |
1561 | //\r | |
1562 | NetMapIterate (&IpInstance->TxTokens, Ip4SentPacketTicking, NULL);\r | |
1563 | }\r | |
1564 | }\r |