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