]> git.proxmox.com Git - mirror_edk2.git/blame - NetworkPkg/Udp6Dxe/Udp6Impl.c
NetworkPkg: Add more parameter or return status check in UDP6 driver
[mirror_edk2.git] / NetworkPkg / Udp6Dxe / Udp6Impl.c
CommitLineData
a3bcde70
HT
1/** @file\r
2 Udp6 driver's whole implementation.\r
3\r
2c4a45b3 4 Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>\r
a3bcde70
HT
5\r
6 This program and the accompanying materials\r
7 are licensed and made available under the terms and conditions of the BSD License\r
8 which accompanies this distribution. The full text of the license may be found at\r
9 http://opensource.org/licenses/bsd-license.php.\r
10\r
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
14**/\r
15\r
16#include "Udp6Impl.h"\r
17\r
18UINT16 mUdp6RandomPort;\r
19\r
20/**\r
21 This function checks and timeouts the I/O datagrams holding by the corresponding\r
22 service context.\r
23\r
24 @param[in] Event The event this function is registered to.\r
25 @param[in] Context The context data registered during the creation of\r
26 the Event.\r
27\r
28**/\r
29VOID\r
30EFIAPI\r
31Udp6CheckTimeout (\r
32 IN EFI_EVENT Event,\r
33 IN VOID *Context\r
34 );\r
35\r
36/**\r
37 This function finds the udp instance by the specified <Address, Port> pair.\r
38\r
39 @param[in] InstanceList Pointer to the head of the list linking the udp\r
40 instances.\r
41 @param[in] Address Pointer to the specified IPv6 address.\r
42 @param[in] Port The udp port number.\r
43\r
44 @retval TRUE The specified <Address, Port> pair is found.\r
45 @retval FALSE Otherwise.\r
46\r
47**/\r
48BOOLEAN\r
49Udp6FindInstanceByPort (\r
50 IN LIST_ENTRY *InstanceList,\r
51 IN EFI_IPv6_ADDRESS *Address,\r
52 IN UINT16 Port\r
53 );\r
54\r
55/**\r
56 This function is the packet transmitting notify function registered to the IpIo\r
57 interface. It's called to signal the udp TxToken when the IpIo layer completes\r
58 transmitting of the udp datagram.\r
59\r
ceec3638
WF
60 If Context is NULL, then ASSERT().\r
61 If NotifyData is NULL, then ASSERT().\r
62\r
a3bcde70
HT
63 @param[in] Status The completion status of the output udp datagram.\r
64 @param[in] Context Pointer to the context data.\r
65 @param[in] Sender Specify a EFI_IP6_PROTOCOL for sending.\r
66 @param[in] NotifyData Pointer to the notify data.\r
67\r
68**/\r
69VOID\r
70EFIAPI\r
71Udp6DgramSent (\r
72 IN EFI_STATUS Status,\r
73 IN VOID *Context,\r
74 IN IP_IO_IP_PROTOCOL Sender,\r
75 IN VOID *NotifyData\r
76 );\r
77\r
78/**\r
79 This function processes the received datagram passed up by the IpIo layer.\r
80\r
ceec3638
WF
81 If NetSession is NULL, then ASSERT().\r
82 If Packet is NULL, then ASSERT().\r
83 If Context is NULL, then ASSERT().\r
84\r
a3bcde70
HT
85 @param[in] Status The status of this udp datagram.\r
86 @param[in] IcmpError The IcmpError code, only available when Status is\r
87 EFI_ICMP_ERROR.\r
88 @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA.\r
89 @param[in] Packet Pointer to the NET_BUF containing the received udp\r
90 datagram.\r
91 @param[in] Context Pointer to the context data.\r
92\r
93**/\r
94VOID\r
95EFIAPI\r
96Udp6DgramRcvd (\r
97 IN EFI_STATUS Status,\r
98 IN UINT8 IcmpError,\r
99 IN EFI_NET_SESSION_DATA *NetSession,\r
100 IN NET_BUF *Packet,\r
101 IN VOID *Context\r
102 );\r
103\r
104/**\r
105 This function cancle the token specified by Arg in the Map.\r
106\r
107 @param[in] Map Pointer to the NET_MAP.\r
108 @param[in] Item Pointer to the NET_MAP_ITEM.\r
109 @param[in] Arg Pointer to the token to be cancelled, if NULL, all\r
110 the tokens in this Map will be cancelled.\r
111 This parameter is optional and may be NULL.\r
112\r
113 @retval EFI_SUCCESS The token is cancelled if Arg is NULL or the token\r
114 is not the same as that in the Item if Arg is not\r
115 NULL.\r
116 @retval EFI_ABORTED Arg is not NULL, and the token specified by Arg is\r
117 cancelled.\r
118\r
119**/\r
120EFI_STATUS\r
121EFIAPI\r
122Udp6CancelTokens (\r
123 IN NET_MAP *Map,\r
124 IN NET_MAP_ITEM *Item,\r
125 IN VOID *Arg OPTIONAL\r
126 );\r
127\r
128/**\r
129 This function check if the received udp datagram matches with the Instance.\r
130\r
131 @param[in] Instance Pointer to the udp instance context data.\r
132 @param[in] Udp6Session Pointer to the EFI_UDP6_SESSION_DATA abstracted\r
133 from the received udp datagram.\r
134\r
135 @retval TRUE The udp datagram matches the receiving requirements of the Instance.\r
136 @retval FALSE The udp datagram doe not match the receiving requirements of the Instance.\r
137\r
138**/\r
139BOOLEAN\r
140Udp6MatchDgram (\r
141 IN UDP6_INSTANCE_DATA *Instance,\r
142 IN EFI_UDP6_SESSION_DATA *Udp6Session\r
143 );\r
144\r
145/**\r
146 This function removes the Wrap specified by Context and releases relevant resources.\r
147\r
148 @param[in] Event The Event this notify function is registered to.\r
149 @param[in] Context Pointer to the context data.\r
150\r
151**/\r
152VOID\r
153EFIAPI\r
154Udp6RecycleRxDataWrap (\r
155 IN EFI_EVENT Event,\r
156 IN VOID *Context\r
157 );\r
158\r
159/**\r
160 This function wraps the Packet into RxData.\r
161\r
162 @param[in] Instance Pointer to the instance context data.\r
163 @param[in] Packet Pointer to the buffer containing the received\r
164 datagram.\r
165 @param[in] RxData Pointer to the EFI_UDP6_RECEIVE_DATA of this\r
166 datagram.\r
167\r
0f333664
WF
168 @return Pointer to the structure wrapping the RxData and the Packet. NULL will\r
169 be returned if any error occurs.\r
a3bcde70
HT
170\r
171**/\r
172UDP6_RXDATA_WRAP *\r
173Udp6WrapRxData (\r
174 IN UDP6_INSTANCE_DATA *Instance,\r
175 IN NET_BUF *Packet,\r
176 IN EFI_UDP6_RECEIVE_DATA *RxData\r
177 );\r
178\r
179/**\r
180 This function enqueues the received datagram into the instances' receiving queues.\r
181\r
182 @param[in] Udp6Service Pointer to the udp service context data.\r
183 @param[in] Packet Pointer to the buffer containing the received\r
184 datagram.\r
185 @param[in] RxData Pointer to the EFI_UDP6_RECEIVE_DATA of this\r
186 datagram.\r
187\r
188 @return The times this datagram is enqueued.\r
189\r
190**/\r
191UINTN\r
192Udp6EnqueueDgram (\r
193 IN UDP6_SERVICE_DATA *Udp6Service,\r
194 IN NET_BUF *Packet,\r
195 IN EFI_UDP6_RECEIVE_DATA *RxData\r
196 );\r
197\r
198/**\r
199 This function delivers the datagrams enqueued in the instances.\r
200\r
201 @param[in] Udp6Service Pointer to the udp service context data.\r
202\r
203**/\r
204VOID\r
205Udp6DeliverDgram (\r
206 IN UDP6_SERVICE_DATA *Udp6Service\r
207 );\r
208\r
209/**\r
c2adf51f 210 This function demultiplexes the received udp datagram to the appropriate instances.\r
a3bcde70
HT
211\r
212 @param[in] Udp6Service Pointer to the udp service context data.\r
c2adf51f 213 @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA abstracted from\r
a3bcde70
HT
214 the received datagram.\r
215 @param[in] Packet Pointer to the buffer containing the received udp\r
216 datagram.\r
217\r
218**/\r
219VOID\r
220Udp6Demultiplex (\r
221 IN UDP6_SERVICE_DATA *Udp6Service,\r
222 IN EFI_NET_SESSION_DATA *NetSession,\r
223 IN NET_BUF *Packet\r
224 );\r
225\r
226/**\r
227 This function handles the received Icmp Error message and demultiplexes it to the\r
228 instance.\r
229\r
230 @param[in] Udp6Service Pointer to the udp service context data.\r
231 @param[in] IcmpError The icmp error code.\r
232 @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA abstracted\r
233 from the received Icmp Error packet.\r
234 @param[in, out] Packet Pointer to the Icmp Error packet.\r
235\r
236**/\r
237VOID\r
238Udp6IcmpHandler (\r
239 IN UDP6_SERVICE_DATA *Udp6Service,\r
240 IN UINT8 IcmpError,\r
241 IN EFI_NET_SESSION_DATA *NetSession,\r
242 IN OUT NET_BUF *Packet\r
243 );\r
244\r
245/**\r
246 This function builds and sends out a icmp port unreachable message.\r
247\r
248 @param[in] IpIo Pointer to the IP_IO instance.\r
249 @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA of the packet\r
250 causes this icmp error message.\r
251 @param[in] Udp6Header Pointer to the udp header of the datagram causes\r
252 this icmp error message.\r
253\r
254**/\r
255VOID\r
256Udp6SendPortUnreach (\r
257 IN IP_IO *IpIo,\r
258 IN EFI_NET_SESSION_DATA *NetSession,\r
259 IN VOID *Udp6Header\r
260 );\r
261\r
262/**\r
263 Find the key in the netmap\r
264\r
265 @param[in] Map The netmap to search within.\r
266 @param[in] Key The key to search.\r
267\r
268 @return The point to the item contains the Key, or NULL if Key isn't in the map.\r
269\r
270**/\r
271NET_MAP_ITEM *\r
272Udp6MapMultiCastAddr (\r
273 IN NET_MAP *Map,\r
274 IN VOID *Key\r
275 );\r
276\r
277/**\r
278 Create the Udp service context data.\r
279\r
280 @param[in] Udp6Service Pointer to the UDP6_SERVICE_DATA.\r
281 @param[in] ImageHandle The image handle of this udp6 driver.\r
282 @param[in] ControllerHandle The controller handle this udp6 driver binds on.\r
283\r
284 @retval EFI_SUCCESS The udp6 service context data was created and\r
285 initialized.\r
286 @retval EFI_OUT_OF_RESOURCES Cannot allocate memory.\r
287 @retval Others An error condition occurred.\r
288\r
289**/\r
290EFI_STATUS\r
291Udp6CreateService (\r
292 IN UDP6_SERVICE_DATA *Udp6Service,\r
293 IN EFI_HANDLE ImageHandle,\r
294 IN EFI_HANDLE ControllerHandle\r
295 )\r
296{\r
297 EFI_STATUS Status;\r
298 IP_IO_OPEN_DATA OpenData;\r
299\r
300 ZeroMem (Udp6Service, sizeof (UDP6_SERVICE_DATA));\r
301\r
302 Udp6Service->Signature = UDP6_SERVICE_DATA_SIGNATURE;\r
303 Udp6Service->ServiceBinding = mUdp6ServiceBinding;\r
304 Udp6Service->ImageHandle = ImageHandle;\r
305 Udp6Service->ControllerHandle = ControllerHandle;\r
306 Udp6Service->ChildrenNumber = 0;\r
307\r
308 InitializeListHead (&Udp6Service->ChildrenList);\r
309\r
310 //\r
311 // Create the IpIo for this service context.\r
312 //\r
313 Udp6Service->IpIo = IpIoCreate (ImageHandle, ControllerHandle, IP_VERSION_6);\r
314 if (Udp6Service->IpIo == NULL) {\r
315 return EFI_OUT_OF_RESOURCES;\r
316 }\r
317\r
318 //\r
319 // Set the OpenData used to open the IpIo.\r
320 //\r
321 CopyMem (\r
322 &OpenData.IpConfigData.Ip6CfgData,\r
323 &mIp6IoDefaultIpConfigData,\r
324 sizeof (EFI_IP6_CONFIG_DATA)\r
325 );\r
326 OpenData.RcvdContext = (VOID *) Udp6Service;\r
327 OpenData.SndContext = NULL;\r
328 OpenData.PktRcvdNotify = Udp6DgramRcvd;\r
329 OpenData.PktSentNotify = Udp6DgramSent;\r
330\r
331 //\r
332 // Configure and start the IpIo.\r
333 //\r
334 Status = IpIoOpen (Udp6Service->IpIo, &OpenData);\r
335 if (EFI_ERROR (Status)) {\r
336 goto ON_ERROR;\r
337 }\r
338\r
339 //\r
340 // Create the event for Udp timeout checking.\r
341 //\r
342 Status = gBS->CreateEvent (\r
343 EVT_TIMER | EVT_NOTIFY_SIGNAL,\r
344 TPL_CALLBACK,\r
345 Udp6CheckTimeout,\r
346 Udp6Service,\r
347 &Udp6Service->TimeoutEvent\r
348 );\r
349 if (EFI_ERROR (Status)) {\r
350 goto ON_ERROR;\r
351 }\r
352\r
353 //\r
354 // Start the timeout timer event.\r
355 //\r
356 Status = gBS->SetTimer (\r
357 Udp6Service->TimeoutEvent,\r
358 TimerPeriodic,\r
359 UDP6_TIMEOUT_INTERVAL\r
360 );\r
361 if (EFI_ERROR (Status)) {\r
362 goto ON_ERROR;\r
363 }\r
364\r
365 return EFI_SUCCESS;\r
366\r
367ON_ERROR:\r
368\r
369 if (Udp6Service->TimeoutEvent != NULL) {\r
370 gBS->CloseEvent (Udp6Service->TimeoutEvent);\r
371 }\r
372\r
373 IpIoDestroy (Udp6Service->IpIo);\r
216f7970 374 Udp6Service->IpIo = NULL;\r
375 \r
a3bcde70
HT
376 return Status;\r
377}\r
378\r
379\r
380/**\r
381 Clean the Udp service context data.\r
382\r
383 @param[in, out] Udp6Service Pointer to the UDP6_SERVICE_DATA.\r
384\r
385**/\r
386VOID\r
387Udp6CleanService (\r
388 IN OUT UDP6_SERVICE_DATA *Udp6Service\r
389 )\r
390{\r
391 //\r
392 // Close the TimeoutEvent timer.\r
393 //\r
394 gBS->CloseEvent (Udp6Service->TimeoutEvent);\r
395\r
396 //\r
397 // Destroy the IpIo.\r
398 //\r
399 IpIoDestroy (Udp6Service->IpIo);\r
216f7970 400 Udp6Service->IpIo = NULL;\r
401 \r
402 ZeroMem (Udp6Service, sizeof (UDP6_SERVICE_DATA));\r
a3bcde70
HT
403}\r
404\r
405\r
406/**\r
407 This function checks and times out the I/O datagrams listed in the\r
408 UDP6_SERVICE_DATA which is specified by the input parameter Context.\r
409\r
410\r
411 @param[in] Event The event this function registered to.\r
412 @param[in] Context The context data registered during the creation of\r
413 the Event.\r
414\r
415**/\r
416VOID\r
417EFIAPI\r
418Udp6CheckTimeout (\r
419 IN EFI_EVENT Event,\r
420 IN VOID *Context\r
421 )\r
422{\r
423 UDP6_SERVICE_DATA *Udp6Service;\r
424 LIST_ENTRY *Entry;\r
425 UDP6_INSTANCE_DATA *Instance;\r
426 LIST_ENTRY *WrapEntry;\r
427 LIST_ENTRY *NextEntry;\r
428 UDP6_RXDATA_WRAP *Wrap;\r
429\r
430 Udp6Service = (UDP6_SERVICE_DATA *) Context;\r
431 NET_CHECK_SIGNATURE (Udp6Service, UDP6_SERVICE_DATA_SIGNATURE);\r
432\r
433 NET_LIST_FOR_EACH (Entry, &Udp6Service->ChildrenList) {\r
434 //\r
435 // Iterate all the instances belonging to this service context.\r
436 //\r
437 Instance = NET_LIST_USER_STRUCT (Entry, UDP6_INSTANCE_DATA, Link);\r
438 NET_CHECK_SIGNATURE (Instance, UDP6_INSTANCE_DATA_SIGNATURE);\r
439\r
440 if (!Instance->Configured || (Instance->ConfigData.ReceiveTimeout == 0)) {\r
441 //\r
442 // Skip this instance if it's not configured or no receive timeout.\r
443 //\r
444 continue;\r
445 }\r
446\r
447 NET_LIST_FOR_EACH_SAFE (WrapEntry, NextEntry, &Instance->RcvdDgramQue) {\r
448 //\r
449 // Iterate all the rxdatas belonging to this udp instance.\r
450 //\r
451 Wrap = NET_LIST_USER_STRUCT (WrapEntry, UDP6_RXDATA_WRAP, Link);\r
452\r
453 if (Wrap->TimeoutTick < UDP6_TIMEOUT_INTERVAL / 10) {\r
454 //\r
455 // Remove this RxData if it timeouts.\r
456 //\r
457 Udp6RecycleRxDataWrap (NULL, (VOID *) Wrap);\r
458 } else {\r
459 Wrap->TimeoutTick -= UDP6_TIMEOUT_INTERVAL / 10;\r
460 }\r
461 }\r
462 }\r
463}\r
464\r
465\r
466/**\r
467 This function intializes the new created udp instance.\r
468\r
469 @param[in] Udp6Service Pointer to the UDP6_SERVICE_DATA.\r
470 @param[in, out] Instance Pointer to the un-initialized UDP6_INSTANCE_DATA.\r
471\r
472**/\r
473VOID\r
474Udp6InitInstance (\r
475 IN UDP6_SERVICE_DATA *Udp6Service,\r
476 IN OUT UDP6_INSTANCE_DATA *Instance\r
477 )\r
478{\r
479 //\r
480 // Set the signature.\r
481 //\r
482 Instance->Signature = UDP6_INSTANCE_DATA_SIGNATURE;\r
483\r
484 //\r
485 // Init the lists.\r
486 //\r
487 InitializeListHead (&Instance->Link);\r
488 InitializeListHead (&Instance->RcvdDgramQue);\r
489 InitializeListHead (&Instance->DeliveredDgramQue);\r
490\r
491 //\r
492 // Init the NET_MAPs.\r
493 //\r
494 NetMapInit (&Instance->TxTokens);\r
495 NetMapInit (&Instance->RxTokens);\r
496 NetMapInit (&Instance->McastIps);\r
497\r
498 //\r
499 // Save the pointer to the UDP6_SERVICE_DATA, and initialize other members.\r
500 //\r
501 Instance->Udp6Service = Udp6Service;\r
502 CopyMem (&Instance->Udp6Proto, &mUdp6Protocol, sizeof (EFI_UDP6_PROTOCOL));\r
503 Instance->IcmpError = EFI_SUCCESS;\r
504 Instance->Configured = FALSE;\r
505 Instance->IsNoMapping = FALSE;\r
216f7970 506 Instance->InDestroy = FALSE;\r
a3bcde70
HT
507}\r
508\r
509\r
510/**\r
511 This function cleans the udp instance.\r
512\r
513 @param[in, out] Instance Pointer to the UDP6_INSTANCE_DATA to clean.\r
514\r
515**/\r
516VOID\r
517Udp6CleanInstance (\r
518 IN OUT UDP6_INSTANCE_DATA *Instance\r
519 )\r
520{\r
521 NetMapClean (&Instance->McastIps);\r
522 NetMapClean (&Instance->RxTokens);\r
523 NetMapClean (&Instance->TxTokens);\r
524}\r
525\r
526\r
527/**\r
528 This function finds the udp instance by the specified <Address, Port> pair.\r
529\r
530 @param[in] InstanceList Pointer to the head of the list linking the udp\r
531 instances.\r
532 @param[in] Address Pointer to the specified IPv6 address.\r
533 @param[in] Port The udp port number.\r
534\r
535 @retval TRUE The specified <Address, Port> pair is found.\r
536 @retval FALSE Otherwise.\r
537\r
538**/\r
539BOOLEAN\r
540Udp6FindInstanceByPort (\r
541 IN LIST_ENTRY *InstanceList,\r
542 IN EFI_IPv6_ADDRESS *Address,\r
543 IN UINT16 Port\r
544 )\r
545{\r
546 LIST_ENTRY *Entry;\r
547 UDP6_INSTANCE_DATA *Instance;\r
548 EFI_UDP6_CONFIG_DATA *ConfigData;\r
549\r
550 NET_LIST_FOR_EACH (Entry, InstanceList) {\r
551 //\r
552 // Iterate all the udp instances.\r
553 //\r
554 Instance = NET_LIST_USER_STRUCT (Entry, UDP6_INSTANCE_DATA, Link);\r
555 ConfigData = &Instance->ConfigData;\r
556\r
557 if (!Instance->Configured || ConfigData->AcceptAnyPort) {\r
558 //\r
559 // If the instance is not configured, or the configdata of the instance indicates\r
560 // this instance accepts any port, skip it.\r
561 //\r
562 continue;\r
563 }\r
564\r
565 if (EFI_IP6_EQUAL (&ConfigData->StationAddress, Address) &&\r
566 (ConfigData->StationPort == Port)\r
567 ) {\r
568 //\r
569 // If both the address and the port are the same, return TRUE.\r
570 //\r
571 return TRUE;\r
572 }\r
573 }\r
574\r
575 //\r
576 // Return FALSE when matching fails.\r
577 //\r
578 return FALSE;\r
579}\r
580\r
581\r
582/**\r
583 This function tries to bind the udp instance according to the configured port\r
584 allocation stragety.\r
585\r
586 @param[in] InstanceList Pointer to the head of the list linking the udp\r
587 instances.\r
588 @param[in] ConfigData Pointer to the ConfigData of the instance to be\r
589 bound.\r
590\r
591 @retval EFI_SUCCESS The bound operation completed successfully.\r
592 @retval EFI_ACCESS_DENIED The <Address, Port> specified by the ConfigData is\r
593 already used by other instance.\r
594 @retval EFI_OUT_OF_RESOURCES No available port resources.\r
595\r
596**/\r
597EFI_STATUS\r
598Udp6Bind (\r
599 IN LIST_ENTRY *InstanceList,\r
600 IN EFI_UDP6_CONFIG_DATA *ConfigData\r
601 )\r
602{\r
603 EFI_IPv6_ADDRESS *StationAddress;\r
604 UINT16 StartPort;\r
605\r
606 if (ConfigData->AcceptAnyPort) {\r
607 return EFI_SUCCESS;\r
608 }\r
609\r
610 StationAddress = &ConfigData->StationAddress;\r
611\r
612 if (ConfigData->StationPort != 0) {\r
613\r
614 if (!ConfigData->AllowDuplicatePort &&\r
615 Udp6FindInstanceByPort (InstanceList, StationAddress, ConfigData->StationPort)\r
616 ) {\r
617 //\r
618 // Do not allow duplicate ports and the port is already used by other instance.\r
619 //\r
620 return EFI_ACCESS_DENIED;\r
621 }\r
622 } else {\r
623 //\r
624 // Select a random port for this instance.\r
625 //\r
626 if (ConfigData->AllowDuplicatePort) {\r
627 //\r
628 // Just pick up the random port if the instance allows duplicate port.\r
629 //\r
630 ConfigData->StationPort = mUdp6RandomPort;\r
631 } else {\r
632\r
633 StartPort = mUdp6RandomPort;\r
634\r
635 while (Udp6FindInstanceByPort (InstanceList, StationAddress, mUdp6RandomPort)) {\r
636\r
637 mUdp6RandomPort++;\r
638 if (mUdp6RandomPort == 0) {\r
639 mUdp6RandomPort = UDP6_PORT_KNOWN;\r
640 }\r
641\r
642 if (mUdp6RandomPort == StartPort) {\r
643 //\r
644 // No available port.\r
645 //\r
646 return EFI_OUT_OF_RESOURCES;\r
647 }\r
648 }\r
649\r
650 ConfigData->StationPort = mUdp6RandomPort;\r
651 }\r
652\r
653 mUdp6RandomPort++;\r
654 if (mUdp6RandomPort == 0) {\r
655 mUdp6RandomPort = UDP6_PORT_KNOWN;\r
656 }\r
657 }\r
658 return EFI_SUCCESS;\r
659}\r
660\r
661\r
662/**\r
663 This function is used to check whether the NewConfigData has any un-reconfigurable\r
664 parameters changed compared to the OldConfigData.\r
665\r
666 @param[in] OldConfigData Pointer to the current ConfigData the udp instance\r
667 uses.\r
668 @param[in] NewConfigData Pointer to the new ConfigData.\r
669\r
670 @retval TRUE The instance is reconfigurable according to the NewConfigData.\r
671 @retval FALSE Otherwise.\r
672\r
673**/\r
674BOOLEAN\r
675Udp6IsReconfigurable (\r
676 IN EFI_UDP6_CONFIG_DATA *OldConfigData,\r
677 IN EFI_UDP6_CONFIG_DATA *NewConfigData\r
678 )\r
679{\r
680 if ((NewConfigData->AcceptAnyPort != OldConfigData->AcceptAnyPort) ||\r
681 (NewConfigData->AcceptPromiscuous != OldConfigData->AcceptPromiscuous) ||\r
682 (NewConfigData->AllowDuplicatePort != OldConfigData->AllowDuplicatePort)\r
683 ) {\r
684 //\r
685 // The receiving filter parameters cannot be changed.\r
686 //\r
687 return FALSE;\r
688 }\r
689\r
690 if ((!NewConfigData->AcceptAnyPort) &&\r
691 (NewConfigData->StationPort != OldConfigData->StationPort)\r
692 ) {\r
693 //\r
694 // The port is not changeable.\r
695 //\r
696 return FALSE;\r
697 }\r
698\r
699 if (!EFI_IP6_EQUAL (&NewConfigData->StationAddress, &OldConfigData->StationAddress)) {\r
700 //\r
701 // The StationAddress is not the same.\r
702 //\r
703 return FALSE;\r
704 }\r
705\r
706\r
707 if (!EFI_IP6_EQUAL (&NewConfigData->RemoteAddress, &OldConfigData->RemoteAddress)) {\r
708 //\r
709 // The remoteaddress is not the same.\r
710 //\r
711 return FALSE;\r
712 }\r
713\r
714 if (!NetIp6IsUnspecifiedAddr (&NewConfigData->RemoteAddress) &&\r
715 (NewConfigData->RemotePort != OldConfigData->RemotePort)\r
716 ) {\r
717 //\r
718 // The RemotePort differs if it's designated in the configdata.\r
719 //\r
720 return FALSE;\r
721 }\r
722\r
723 //\r
724 // All checks pass, return TRUE.\r
725 //\r
726 return TRUE;\r
727}\r
728\r
729\r
730/**\r
731 This function builds the Ip6 configdata from the Udp6ConfigData.\r
732\r
733 @param[in] Udp6ConfigData Pointer to the EFI_UDP6_CONFIG_DATA.\r
734 @param[in, out] Ip6ConfigData Pointer to the EFI_IP6_CONFIG_DATA.\r
735\r
736**/\r
737VOID\r
738Udp6BuildIp6ConfigData (\r
739 IN EFI_UDP6_CONFIG_DATA *Udp6ConfigData,\r
740 IN OUT EFI_IP6_CONFIG_DATA *Ip6ConfigData\r
741 )\r
742{\r
743 CopyMem (\r
744 Ip6ConfigData,\r
745 &mIp6IoDefaultIpConfigData,\r
746 sizeof (EFI_IP6_CONFIG_DATA)\r
747 );\r
748 Ip6ConfigData->DefaultProtocol = EFI_IP_PROTO_UDP;\r
749 Ip6ConfigData->AcceptPromiscuous = Udp6ConfigData->AcceptPromiscuous;\r
750 IP6_COPY_ADDRESS (&Ip6ConfigData->StationAddress, &Udp6ConfigData->StationAddress);\r
751 IP6_COPY_ADDRESS (&Ip6ConfigData->DestinationAddress, &Udp6ConfigData->RemoteAddress);\r
752 //\r
753 // Use the -1 magic number to disable the receiving process of the ip instance.\r
754 //\r
755 Ip6ConfigData->ReceiveTimeout = (UINT32) (-1);\r
756}\r
757\r
758\r
759/**\r
760 This function validates the TxToken. It returns the error code according to the spec.\r
761\r
762 @param[in] Instance Pointer to the udp instance context data.\r
763 @param[in] TxToken Pointer to the token to be checked.\r
764\r
765 @retval EFI_SUCCESS The TxToken is valid.\r
766 @retval EFI_INVALID_PARAMETER One or more of the following are TRUE:\r
767 Token.Event is NULL;\r
768 Token.Packet.TxData is NULL;\r
769 Token.Packet.TxData.FragmentCount is zero;\r
770 Token.Packet.TxData.DataLength is not equal to the\r
771 sum of fragment lengths;\r
772 One or more of the\r
773 Token.Packet.TxData.FragmentTable[].FragmentLength\r
774 fields is zero;\r
775 One or more of the\r
776 Token.Packet.TxData.FragmentTable[].FragmentBuffer\r
777 fields is NULL;\r
778 UdpSessionData.DestinationAddress are not valid\r
779 unicast IPv6 addresses if the UdpSessionData is\r
780 not NULL;\r
781 UdpSessionData.DestinationPort and\r
782 ConfigData.RemotePort are all zero if the\r
783 UdpSessionData is not NULL.\r
784 @retval EFI_BAD_BUFFER_SIZE The data length is greater than the maximum UDP\r
785 packet size.\r
786\r
787**/\r
788EFI_STATUS\r
789Udp6ValidateTxToken (\r
790 IN UDP6_INSTANCE_DATA *Instance,\r
791 IN EFI_UDP6_COMPLETION_TOKEN *TxToken\r
792 )\r
793{\r
794 EFI_UDP6_TRANSMIT_DATA *TxData;\r
795 UINT32 Index;\r
796 UINT32 TotalLen;\r
797 EFI_UDP6_CONFIG_DATA *ConfigData;\r
798 EFI_UDP6_SESSION_DATA *UdpSessionData;\r
799\r
800\r
801 if (TxToken->Event == NULL) {\r
802 return EFI_INVALID_PARAMETER;\r
803 }\r
804\r
805 TxData = TxToken->Packet.TxData;\r
806\r
807 if ((TxData == NULL) || (TxData->FragmentCount == 0)) {\r
808 return EFI_INVALID_PARAMETER;\r
809 }\r
810\r
811 TotalLen = 0;\r
812 for (Index = 0; Index < TxData->FragmentCount; Index++) {\r
813\r
814 if ((TxData->FragmentTable[Index].FragmentBuffer == NULL) ||\r
815 (TxData->FragmentTable[Index].FragmentLength == 0)\r
816 ) {\r
817 //\r
818 // If the FragmentBuffer is NULL, or the FragmentLeng is zero.\r
819 //\r
820 return EFI_INVALID_PARAMETER;\r
821 }\r
822\r
823 TotalLen += TxData->FragmentTable[Index].FragmentLength;\r
824 }\r
825\r
826 if (TotalLen != TxData->DataLength) {\r
827 //\r
828 // The TotalLen calculated by adding all the FragmentLeng doesn't equal to the\r
829 // DataLength.\r
830 //\r
831 return EFI_INVALID_PARAMETER;\r
832 }\r
833\r
834 ConfigData = &Instance->ConfigData;\r
835 UdpSessionData = TxData->UdpSessionData;\r
836\r
837 if (UdpSessionData != NULL) {\r
838\r
839 if ((UdpSessionData->DestinationPort == 0) && (ConfigData->RemotePort == 0)) {\r
840 //\r
841 // Ambiguous; no avalaible DestinationPort for this token.\r
842 //\r
843 return EFI_INVALID_PARAMETER;\r
844 }\r
845\r
846 if (NetIp6IsUnspecifiedAddr (&UdpSessionData->DestinationAddress) &&\r
847 NetIp6IsUnspecifiedAddr (&ConfigData->RemoteAddress)\r
848 ) {\r
849 //\r
850 // The DestinationAddress is not specificed.\r
851 //\r
852 return EFI_INVALID_PARAMETER;\r
853 }\r
854\r
855 if (!NetIp6IsUnspecifiedAddr (&UdpSessionData->DestinationAddress) &&\r
856 !NetIp6IsUnspecifiedAddr (&ConfigData->RemoteAddress)\r
857 ) {\r
858 //\r
859 // The ConfigData.RemoteAddress is not zero and the UdpSessionData.DestinationAddress\r
860 // is not zero too.\r
861 //\r
862 return EFI_INVALID_PARAMETER;\r
863 }\r
864 } else if (NetIp6IsUnspecifiedAddr (&ConfigData->RemoteAddress)) {\r
865 //\r
866 // The configured RemoteAddress is all zero, and the user doesn't override the\r
867 // destination address.\r
868 //\r
869 return EFI_INVALID_PARAMETER;\r
870 }\r
871\r
872 if (TxData->DataLength > UDP6_MAX_DATA_SIZE) {\r
873 return EFI_BAD_BUFFER_SIZE;\r
874 }\r
875\r
876 return EFI_SUCCESS;\r
877}\r
878\r
879\r
880/**\r
881 This function checks whether the specified Token duplicates the one in the Map.\r
882\r
883 @param[in] Map Pointer to the NET_MAP.\r
884 @param[in] Item Pointer to the NET_MAP_ITEM contain the pointer to\r
885 the Token.\r
886 @param[in] Context Pointer to the Token to be checked.\r
887\r
888 @retval EFI_SUCCESS The Token specified by Context differs from the\r
889 one in the Item.\r
890 @retval EFI_ACCESS_DENIED The Token duplicates with the one in the Item.\r
891\r
892**/\r
893EFI_STATUS\r
894EFIAPI\r
895Udp6TokenExist (\r
896 IN NET_MAP *Map,\r
897 IN NET_MAP_ITEM *Item,\r
898 IN VOID *Context\r
899 )\r
900{\r
901 EFI_UDP6_COMPLETION_TOKEN *Token;\r
902 EFI_UDP6_COMPLETION_TOKEN *TokenInItem;\r
903\r
904 Token = (EFI_UDP6_COMPLETION_TOKEN *) Context;\r
905 TokenInItem = (EFI_UDP6_COMPLETION_TOKEN *) Item->Key;\r
906\r
907 if ((Token == TokenInItem) || (Token->Event == TokenInItem->Event)) {\r
908 //\r
909 // The Token duplicates with the TokenInItem in case either the two pointers are the\r
910 // same, or the Events of these two tokens are the same.\r
911 //\r
912 return EFI_ACCESS_DENIED;\r
913 }\r
914\r
915 return EFI_SUCCESS;\r
916}\r
917\r
918\r
919/**\r
920 This function calculates the checksum for the Packet, utilizing the pre-calculated\r
921 pseudo HeadSum to reduce some overhead.\r
922\r
923 @param[in] Packet Pointer to the NET_BUF contains the udp datagram.\r
924 @param[in] HeadSum Checksum of the pseudo header, execpt the length\r
925 field.\r
926\r
927 @return The 16-bit checksum of this udp datagram.\r
928\r
929**/\r
930UINT16\r
931Udp6Checksum (\r
932 IN NET_BUF *Packet,\r
933 IN UINT16 HeadSum\r
934 )\r
935{\r
936 UINT16 Checksum;\r
937\r
938 Checksum = NetbufChecksum (Packet);\r
939 Checksum = NetAddChecksum (Checksum, HeadSum);\r
940\r
941 Checksum = NetAddChecksum (Checksum, HTONS ((UINT16) Packet->TotalSize));\r
942 Checksum = (UINT16) (~Checksum);\r
943 return Checksum;\r
944}\r
945\r
946\r
947/**\r
948 This function removes the specified Token from the TokenMap.\r
949\r
950 @param[in] TokenMap Pointer to the NET_MAP containing the tokens.\r
951 @param[in] Token Pointer to the Token to be removed.\r
952\r
953 @retval EFI_SUCCESS The specified Token is removed from the TokenMap.\r
954 @retval EFI_NOT_FOUND The specified Token is not found in the TokenMap.\r
955\r
956**/\r
957EFI_STATUS\r
958Udp6RemoveToken (\r
959 IN NET_MAP *TokenMap,\r
960 IN EFI_UDP6_COMPLETION_TOKEN *Token\r
961 )\r
962{\r
963 NET_MAP_ITEM *Item;\r
964\r
965 //\r
966 // Find the Token first.\r
967 //\r
968 Item = NetMapFindKey (TokenMap, (VOID *) Token);\r
969\r
970 if (Item != NULL) {\r
971 //\r
972 // Remove the token if it's found in the map.\r
973 //\r
974 NetMapRemoveItem (TokenMap, Item, NULL);\r
975\r
976 return EFI_SUCCESS;\r
977 }\r
978 return EFI_NOT_FOUND;\r
979}\r
980\r
981\r
982/**\r
983 This function is the packet transmitting notify function registered to the IpIo\r
984 interface. It's called to signal the udp TxToken when IpIo layer completes the\r
985 transmitting of the udp datagram.\r
986\r
ceec3638
WF
987 If Context is NULL, then ASSERT().\r
988 If NotifyData is NULL, then ASSERT().\r
989\r
a3bcde70
HT
990 @param[in] Status The completion status of the output udp datagram.\r
991 @param[in] Context Pointer to the context data.\r
992 @param[in] Sender Specify a EFI_IP6_PROTOCOL for sending.\r
993 @param[in] NotifyData Pointer to the notify data.\r
994\r
995**/\r
996VOID\r
997EFIAPI\r
998Udp6DgramSent (\r
999 IN EFI_STATUS Status,\r
1000 IN VOID *Context,\r
1001 IN IP_IO_IP_PROTOCOL Sender,\r
1002 IN VOID *NotifyData\r
1003 )\r
1004{\r
1005 UDP6_INSTANCE_DATA *Instance;\r
1006 EFI_UDP6_COMPLETION_TOKEN *Token;\r
1007\r
ceec3638
WF
1008 ASSERT (Context != NULL && NotifyData != NULL);\r
1009\r
a3bcde70
HT
1010 Instance = (UDP6_INSTANCE_DATA *) Context;\r
1011 Token = (EFI_UDP6_COMPLETION_TOKEN *) NotifyData;\r
1012\r
1013 if (Udp6RemoveToken (&Instance->TxTokens, Token) == EFI_SUCCESS) {\r
1014 //\r
1015 // The token may be cancelled. Only signal it if the remove operation succeeds.\r
1016 //\r
1017 Token->Status = Status;\r
1018 gBS->SignalEvent (Token->Event);\r
1019 DispatchDpc ();\r
1020 }\r
1021}\r
1022\r
1023\r
1024/**\r
1025 This function processes the received datagram passed up by the IpIo layer.\r
1026\r
ceec3638
WF
1027 If NetSession is NULL, then ASSERT().\r
1028 If Packet is NULL, then ASSERT().\r
1029 If Context is NULL, then ASSERT().\r
1030\r
a3bcde70
HT
1031 @param[in] Status The status of this udp datagram.\r
1032 @param[in] IcmpError The IcmpError code, only available when Status is\r
1033 EFI_ICMP_ERROR.\r
1034 @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA.\r
1035 @param[in] Packet Pointer to the NET_BUF containing the received udp\r
1036 datagram.\r
1037 @param[in] Context Pointer to the context data.\r
1038\r
1039**/\r
1040VOID\r
1041EFIAPI\r
1042Udp6DgramRcvd (\r
1043 IN EFI_STATUS Status,\r
1044 IN UINT8 IcmpError,\r
1045 IN EFI_NET_SESSION_DATA *NetSession,\r
1046 IN NET_BUF *Packet,\r
1047 IN VOID *Context\r
1048 )\r
1049{\r
ceec3638 1050 ASSERT (NetSession != NULL && Packet != NULL && Context != NULL);\r
a3bcde70
HT
1051 NET_CHECK_SIGNATURE (Packet, NET_BUF_SIGNATURE);\r
1052\r
1053 //\r
1054 // IpIo only passes received packets with Status EFI_SUCCESS or EFI_ICMP_ERROR.\r
1055 //\r
1056 if (Status == EFI_SUCCESS) {\r
1057\r
1058 //\r
1059 // Demultiplex the received datagram.\r
1060 //\r
1061 Udp6Demultiplex ((UDP6_SERVICE_DATA *) Context, NetSession, Packet);\r
1062 } else {\r
1063 //\r
1064 // Handle the ICMP6 Error packet.\r
1065 //\r
1066 Udp6IcmpHandler ((UDP6_SERVICE_DATA *) Context, IcmpError, NetSession, Packet);\r
1067 }\r
1068\r
1069 //\r
1070 // Dispatch the DPC queued by the NotifyFunction of the rx token's events\r
1071 // that are signaled with received data.\r
1072 //\r
1073 DispatchDpc ();\r
1074}\r
1075\r
1076\r
1077/**\r
1078 This function removes the multicast group specified by Arg from the Map.\r
1079\r
1080 @param[in] Map Pointer to the NET_MAP.\r
1081 @param[in] Item Pointer to the NET_MAP_ITEM.\r
1082 @param[in] Arg Pointer to the Arg, it's the pointer to a\r
1083 multicast IPv6 Address. This parameter is\r
1084 optional and may be NULL.\r
1085\r
1086 @retval EFI_SUCCESS The multicast address is removed.\r
1087 @retval EFI_ABORTED The specified multicast address is removed, and the\r
1088 Arg is not NULL.\r
1089\r
1090**/\r
1091EFI_STATUS\r
1092EFIAPI\r
1093Udp6LeaveGroup (\r
1094 IN NET_MAP *Map,\r
1095 IN NET_MAP_ITEM *Item,\r
1096 IN VOID *Arg OPTIONAL\r
1097 )\r
1098{\r
1099 EFI_IPv6_ADDRESS *McastIp;\r
1100\r
1101 McastIp = Arg;\r
1102\r
1103 if ((McastIp != NULL) &&\r
1104 !EFI_IP6_EQUAL (McastIp, ((EFI_IPv6_ADDRESS *)Item->Key))\r
1105 ) {\r
1106 //\r
1107 // McastIp is not NULL and the multicast address contained in the Item\r
1108 // is not the same as McastIp.\r
1109 //\r
1110 return EFI_SUCCESS;\r
1111 }\r
1112\r
1113 FreePool (Item->Key);\r
1114\r
1115 //\r
1116 // Remove this Item.\r
1117 //\r
1118 NetMapRemoveItem (Map, Item, NULL);\r
1119\r
1120 if (McastIp != NULL) {\r
1121 //\r
1122 // Return EFI_ABORTED in case McastIp is not NULL to terminate the iteration.\r
1123 //\r
1124 return EFI_ABORTED;\r
1125 }\r
1126\r
1127 return EFI_SUCCESS;\r
1128}\r
1129\r
1130\r
1131/**\r
1132 This function cancle the token specified by Arg in the Map.\r
1133\r
1134 @param[in] Map Pointer to the NET_MAP.\r
1135 @param[in] Item Pointer to the NET_MAP_ITEM.\r
1136 @param[in] Arg Pointer to the token to be cancelled. If NULL, all\r
1137 the tokens in this Map will be cancelled.\r
1138 This parameter is optional and may be NULL.\r
1139\r
1140 @retval EFI_SUCCESS The token is cancelled if Arg is NULL, or the token\r
1141 is not the same as that in the Item, if Arg is not\r
1142 NULL.\r
1143 @retval EFI_ABORTED Arg is not NULL, and the token specified by Arg is\r
1144 cancelled.\r
1145\r
1146**/\r
1147EFI_STATUS\r
1148EFIAPI\r
1149Udp6CancelTokens (\r
1150 IN NET_MAP *Map,\r
1151 IN NET_MAP_ITEM *Item,\r
1152 IN VOID *Arg OPTIONAL\r
1153 )\r
1154{\r
1155 EFI_UDP6_COMPLETION_TOKEN *TokenToCancel;\r
1156 NET_BUF *Packet;\r
1157 IP_IO *IpIo;\r
1158\r
1159 if ((Arg != NULL) && (Item->Key != Arg)) {\r
1160 return EFI_SUCCESS;\r
1161 }\r
1162\r
1163 if (Item->Value != NULL) {\r
1164 //\r
1165 // If the token is a transmit token, the corresponding Packet is recorded in\r
1166 // Item->Value, invoke IpIo to cancel this packet first. The IpIoCancelTxToken\r
1167 // will invoke Udp6DgramSent, the token will be signaled and this Item will\r
1168 // be removed from the Map there.\r
1169 //\r
1170 Packet = (NET_BUF *) (Item->Value);\r
1171 IpIo = (IP_IO *) (*((UINTN *) &Packet->ProtoData[0]));\r
1172\r
1173 IpIoCancelTxToken (IpIo, Packet);\r
1174 } else {\r
1175 //\r
1176 // The token is a receive token. Abort it and remove it from the Map.\r
1177 //\r
1178 TokenToCancel = (EFI_UDP6_COMPLETION_TOKEN *) Item->Key;\r
1179 NetMapRemoveItem (Map, Item, NULL);\r
1180\r
1181 TokenToCancel->Status = EFI_ABORTED;\r
1182 gBS->SignalEvent (TokenToCancel->Event);\r
1183 }\r
1184\r
1185 if (Arg != NULL) {\r
1186 return EFI_ABORTED;\r
1187 }\r
1188\r
1189 return EFI_SUCCESS;\r
1190}\r
1191\r
1192\r
1193/**\r
1194 This function removes all the Wrap datas in the RcvdDgramQue.\r
1195\r
1196 @param[in] Instance Pointer to the Udp6 Instance.\r
1197\r
1198**/\r
1199VOID\r
1200Udp6FlushRcvdDgram (\r
1201 IN UDP6_INSTANCE_DATA *Instance\r
1202 )\r
1203{\r
1204 UDP6_RXDATA_WRAP *Wrap;\r
1205\r
1206 while (!IsListEmpty (&Instance->RcvdDgramQue)) {\r
1207 //\r
1208 // Iterate all the Wraps in the RcvdDgramQue.\r
1209 //\r
1210 Wrap = NET_LIST_HEAD (&Instance->RcvdDgramQue, UDP6_RXDATA_WRAP, Link);\r
1211\r
1212 //\r
1213 // The Wrap will be removed from the RcvdDgramQue by this function call.\r
1214 //\r
1215 Udp6RecycleRxDataWrap (NULL, (VOID *) Wrap);\r
1216 }\r
1217}\r
1218\r
1219\r
1220\r
1221/**\r
1222 Cancel Udp6 tokens from the Udp6 instance.\r
1223\r
1224 @param[in] Instance Pointer to the udp instance context data.\r
1225 @param[in] Token Pointer to the token to be canceled. If NULL, all\r
1226 tokens in this instance will be cancelled.\r
1227 This parameter is optional and may be NULL.\r
1228\r
1229 @retval EFI_SUCCESS The Token is cancelled.\r
1230 @retval EFI_NOT_FOUND The Token is not found.\r
1231\r
1232**/\r
1233EFI_STATUS\r
1234Udp6InstanceCancelToken (\r
1235 IN UDP6_INSTANCE_DATA *Instance,\r
1236 IN EFI_UDP6_COMPLETION_TOKEN *Token OPTIONAL\r
1237 )\r
1238{\r
1239 EFI_STATUS Status;\r
1240\r
1241 //\r
1242 // Cancel this token from the TxTokens map.\r
1243 //\r
1244 Status = NetMapIterate (&Instance->TxTokens, Udp6CancelTokens, Token);\r
1245\r
1246 if ((Token != NULL) && (Status == EFI_ABORTED)) {\r
1247 //\r
1248 // If Token isn't NULL and Status is EFI_ABORTED, the token is cancelled from\r
1249 // the TxTokens and returns success.\r
1250 //\r
1251 return EFI_SUCCESS;\r
1252 }\r
1253\r
1254 //\r
1255 // Try to cancel this token from the RxTokens map in condition either the Token\r
1256 // is NULL or the specified Token is not in TxTokens.\r
1257 //\r
1258 Status = NetMapIterate (&Instance->RxTokens, Udp6CancelTokens, Token);\r
1259\r
1260 if ((Token != NULL) && (Status == EFI_SUCCESS)) {\r
1261 //\r
1262 // If Token isn't NULL and Status is EFI_SUCCESS, the token is neither in the\r
1263 // TxTokens nor the RxTokens, or say, it's not found.\r
1264 //\r
1265 return EFI_NOT_FOUND;\r
1266 }\r
1267\r
1268 ASSERT ((Token != NULL) ||\r
1269 ((0 == NetMapGetCount (&Instance->TxTokens)) &&\r
1270 (0 == NetMapGetCount (&Instance->RxTokens)))\r
1271 );\r
1272\r
1273 return EFI_SUCCESS;\r
1274}\r
1275\r
1276\r
1277/**\r
1278 This function checks if the received udp datagram matches with the Instance.\r
1279\r
1280 @param[in] Instance Pointer to the udp instance context data.\r
1281 @param[in] Udp6Session Pointer to the EFI_UDP6_SESSION_DATA abstracted\r
1282 from the received udp datagram.\r
1283\r
1284 @retval TRUE The udp datagram matches the receiving requirements of the Instance.\r
1285 @retval FALSE The udp datagram does not matche the receiving requirements of the Instance.\r
1286\r
1287**/\r
1288BOOLEAN\r
1289Udp6MatchDgram (\r
1290 IN UDP6_INSTANCE_DATA *Instance,\r
1291 IN EFI_UDP6_SESSION_DATA *Udp6Session\r
1292 )\r
1293{\r
1294 EFI_UDP6_CONFIG_DATA *ConfigData;\r
1295 EFI_IPv6_ADDRESS Destination;\r
1296\r
1297 ConfigData = &Instance->ConfigData;\r
1298\r
1299 if (ConfigData->AcceptPromiscuous) {\r
1300 //\r
1301 // Always matches if this instance is in the promiscuous state.\r
1302 //\r
1303 return TRUE;\r
1304 }\r
1305\r
1306 if ((!ConfigData->AcceptAnyPort && (Udp6Session->DestinationPort != ConfigData->StationPort)) ||\r
1307 ((ConfigData->RemotePort != 0) && (Udp6Session->SourcePort != ConfigData->RemotePort))\r
1308 ) {\r
1309 //\r
1310 // The local port or the remote port doesn't match.\r
1311 //\r
1312 return FALSE;\r
1313 }\r
1314\r
1315 if (!NetIp6IsUnspecifiedAddr (&ConfigData->RemoteAddress) &&\r
1316 !EFI_IP6_EQUAL (&ConfigData->RemoteAddress, &Udp6Session->SourceAddress)\r
1317 ) {\r
1318 //\r
1319 // This datagram doesn't come from the instance's specified sender.\r
1320 //\r
1321 return FALSE;\r
1322 }\r
1323\r
1324 if (NetIp6IsUnspecifiedAddr (&ConfigData->StationAddress) ||\r
1325 EFI_IP6_EQUAL (&Udp6Session->DestinationAddress, &ConfigData->StationAddress)\r
1326 ) {\r
1327 //\r
1328 // The instance is configured to receive datagrams destinated to any station IP or\r
1329 // the destination address of this datagram matches the configured station IP.\r
1330 //\r
1331 return TRUE;\r
1332 }\r
1333\r
1334 IP6_COPY_ADDRESS (&Destination, &Udp6Session->DestinationAddress);\r
1335\r
1336 if (IP6_IS_MULTICAST (&Destination) &&\r
1337 (NULL != Udp6MapMultiCastAddr (&Instance->McastIps, &Destination))\r
1338 ) {\r
1339 //\r
1340 // It's a multicast packet and the multicast address is accepted by this instance.\r
1341 //\r
1342 return TRUE;\r
1343 }\r
1344\r
1345 return FALSE;\r
1346}\r
1347\r
1348\r
1349/**\r
1350 This function removes the Wrap specified by Context and release relevant resources.\r
1351\r
1352 @param[in] Event The Event this notify function registered to.\r
1353 @param[in] Context Pointer to the context data.\r
1354\r
1355**/\r
1356VOID\r
1357EFIAPI\r
1358Udp6RecycleRxDataWrap (\r
1359 IN EFI_EVENT Event,\r
1360 IN VOID *Context\r
1361 )\r
1362{\r
1363 UDP6_RXDATA_WRAP *Wrap;\r
1364\r
1365 Wrap = (UDP6_RXDATA_WRAP *) Context;\r
1366\r
1367 //\r
1368 // Remove the Wrap from the list it belongs to.\r
1369 //\r
1370 RemoveEntryList (&Wrap->Link);\r
1371\r
1372 //\r
1373 // Free the Packet associated with this Wrap.\r
1374 //\r
1375 NetbufFree (Wrap->Packet);\r
1376\r
1377 //\r
1378 // Close the event.\r
1379 //\r
1380 gBS->CloseEvent (Wrap->RxData.RecycleSignal);\r
1381\r
1382 FreePool (Wrap);\r
1383}\r
1384\r
1385\r
1386/**\r
1387 This function wraps the Packet into RxData.\r
1388\r
1389 @param[in] Instance Pointer to the instance context data.\r
1390 @param[in] Packet Pointer to the buffer containing the received\r
1391 datagram.\r
1392 @param[in] RxData Pointer to the EFI_UDP6_RECEIVE_DATA of this\r
1393 datagram.\r
1394\r
0f333664
WF
1395 @return Pointer to the structure wrapping the RxData and the Packet. NULL will\r
1396 be returned if any error occurs.\r
a3bcde70
HT
1397\r
1398**/\r
1399UDP6_RXDATA_WRAP *\r
1400Udp6WrapRxData (\r
1401 IN UDP6_INSTANCE_DATA *Instance,\r
1402 IN NET_BUF *Packet,\r
1403 IN EFI_UDP6_RECEIVE_DATA *RxData\r
1404 )\r
1405{\r
1406 EFI_STATUS Status;\r
1407 UDP6_RXDATA_WRAP *Wrap;\r
1408\r
1409 //\r
1410 // Allocate buffer for the Wrap.\r
1411 //\r
1412 Wrap = AllocateZeroPool (sizeof (UDP6_RXDATA_WRAP) +\r
1413 (Packet->BlockOpNum - 1) * sizeof (EFI_UDP6_FRAGMENT_DATA));\r
1414 if (Wrap == NULL) {\r
1415 return NULL;\r
1416 }\r
1417\r
1418 InitializeListHead (&Wrap->Link);\r
1419\r
1420 CopyMem (&Wrap->RxData, RxData, sizeof(EFI_UDP6_RECEIVE_DATA));\r
1421 //\r
1422 // Create the Recycle event.\r
1423 //\r
1424 Status = gBS->CreateEvent (\r
1425 EVT_NOTIFY_SIGNAL,\r
1426 TPL_NOTIFY,\r
1427 Udp6RecycleRxDataWrap,\r
1428 Wrap,\r
1429 &Wrap->RxData.RecycleSignal\r
1430 );\r
1431 if (EFI_ERROR (Status)) {\r
1432 FreePool (Wrap);\r
1433 return NULL;\r
1434 }\r
1435\r
1436 Wrap->Packet = Packet;\r
1437 Wrap->TimeoutTick = Instance->ConfigData.ReceiveTimeout;\r
1438\r
1439 return Wrap;\r
1440}\r
1441\r
1442\r
1443/**\r
1444 This function enqueues the received datagram into the instances' receiving queues.\r
1445\r
1446 @param[in] Udp6Service Pointer to the udp service context data.\r
1447 @param[in] Packet Pointer to the buffer containing the received\r
1448 datagram.\r
1449 @param[in] RxData Pointer to the EFI_UDP6_RECEIVE_DATA of this\r
1450 datagram.\r
1451\r
1452 @return The times this datagram is enqueued.\r
1453\r
1454**/\r
1455UINTN\r
1456Udp6EnqueueDgram (\r
1457 IN UDP6_SERVICE_DATA *Udp6Service,\r
1458 IN NET_BUF *Packet,\r
1459 IN EFI_UDP6_RECEIVE_DATA *RxData\r
1460 )\r
1461{\r
1462 LIST_ENTRY *Entry;\r
1463 UDP6_INSTANCE_DATA *Instance;\r
1464 UDP6_RXDATA_WRAP *Wrap;\r
1465 UINTN Enqueued;\r
1466\r
1467 Enqueued = 0;\r
1468\r
1469 NET_LIST_FOR_EACH (Entry, &Udp6Service->ChildrenList) {\r
1470 //\r
1471 // Iterate the instances.\r
1472 //\r
1473 Instance = NET_LIST_USER_STRUCT (Entry, UDP6_INSTANCE_DATA, Link);\r
1474\r
1475 if (!Instance->Configured) {\r
1476 continue;\r
1477 }\r
1478\r
1479 if (Udp6MatchDgram (Instance, &RxData->UdpSession)) {\r
1480 //\r
1481 // Wrap the RxData and put this Wrap into the instances RcvdDgramQue.\r
1482 //\r
1483 Wrap = Udp6WrapRxData (Instance, Packet, RxData);\r
1484 if (Wrap == NULL) {\r
1485 continue;\r
1486 }\r
1487\r
1488 NET_GET_REF (Packet);\r
1489\r
1490 InsertTailList (&Instance->RcvdDgramQue, &Wrap->Link);\r
1491\r
1492 Enqueued++;\r
1493 }\r
1494 }\r
1495\r
1496 return Enqueued;\r
1497}\r
1498\r
1499\r
1500/**\r
1501 This function delivers the received datagrams to the specified instance.\r
1502\r
1503 @param[in] Instance Pointer to the instance context data.\r
1504\r
1505**/\r
1506VOID\r
1507Udp6InstanceDeliverDgram (\r
1508 IN UDP6_INSTANCE_DATA *Instance\r
1509 )\r
1510{\r
1511 UDP6_RXDATA_WRAP *Wrap;\r
1512 EFI_UDP6_COMPLETION_TOKEN *Token;\r
1513 NET_BUF *Dup;\r
1514 EFI_UDP6_RECEIVE_DATA *RxData;\r
1515 EFI_TPL OldTpl;\r
1516\r
1517 if (!IsListEmpty (&Instance->RcvdDgramQue) &&\r
1518 !NetMapIsEmpty (&Instance->RxTokens)\r
1519 ) {\r
1520\r
1521 Wrap = NET_LIST_HEAD (&Instance->RcvdDgramQue, UDP6_RXDATA_WRAP, Link);\r
1522\r
1523 if (NET_BUF_SHARED (Wrap->Packet)) {\r
1524 //\r
1525 // Duplicate the Packet if it is shared between instances.\r
1526 //\r
1527 Dup = NetbufDuplicate (Wrap->Packet, NULL, 0);\r
1528 if (Dup == NULL) {\r
1529 return;\r
1530 }\r
1531\r
1532 NetbufFree (Wrap->Packet);\r
1533\r
1534 Wrap->Packet = Dup;\r
1535 }\r
1536\r
1537 NetListRemoveHead (&Instance->RcvdDgramQue);\r
1538\r
1539 Token = (EFI_UDP6_COMPLETION_TOKEN *) NetMapRemoveHead (&Instance->RxTokens, NULL);\r
1540\r
1541 //\r
1542 // Build the FragmentTable and set the FragmentCount in RxData.\r
1543 //\r
1544 RxData = &Wrap->RxData;\r
1545 RxData->FragmentCount = Wrap->Packet->BlockOpNum;\r
1546\r
1547 NetbufBuildExt (\r
1548 Wrap->Packet,\r
1549 (NET_FRAGMENT *) RxData->FragmentTable,\r
1550 &RxData->FragmentCount\r
1551 );\r
1552\r
1553 Token->Status = EFI_SUCCESS;\r
1554 Token->Packet.RxData = &Wrap->RxData;\r
1555\r
1556 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
1557 InsertTailList (&Instance->DeliveredDgramQue, &Wrap->Link);\r
1558 gBS->RestoreTPL (OldTpl);\r
1559\r
1560 gBS->SignalEvent (Token->Event);\r
1561 }\r
1562}\r
1563\r
1564\r
1565/**\r
1566 This function delivers the datagrams enqueued in the instances.\r
1567\r
1568 @param[in] Udp6Service Pointer to the udp service context data.\r
1569\r
1570**/\r
1571VOID\r
1572Udp6DeliverDgram (\r
1573 IN UDP6_SERVICE_DATA *Udp6Service\r
1574 )\r
1575{\r
1576 LIST_ENTRY *Entry;\r
1577 UDP6_INSTANCE_DATA *Instance;\r
1578\r
1579 NET_LIST_FOR_EACH (Entry, &Udp6Service->ChildrenList) {\r
1580 //\r
1581 // Iterate the instances.\r
1582 //\r
1583 Instance = NET_LIST_USER_STRUCT (Entry, UDP6_INSTANCE_DATA, Link);\r
1584\r
1585 if (!Instance->Configured) {\r
1586 continue;\r
1587 }\r
1588\r
1589 //\r
1590 // Deliver the datagrams of this instance.\r
1591 //\r
1592 Udp6InstanceDeliverDgram (Instance);\r
1593 }\r
1594}\r
1595\r
1596\r
1597/**\r
1598 This function demultiplexes the received udp datagram to the appropriate instances.\r
1599\r
1600 @param[in] Udp6Service Pointer to the udp service context data.\r
c2adf51f 1601 @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA abstracted from\r
a3bcde70
HT
1602 the received datagram.\r
1603 @param[in] Packet Pointer to the buffer containing the received udp\r
1604 datagram.\r
1605\r
1606**/\r
1607VOID\r
1608Udp6Demultiplex (\r
1609 IN UDP6_SERVICE_DATA *Udp6Service,\r
1610 IN EFI_NET_SESSION_DATA *NetSession,\r
1611 IN NET_BUF *Packet\r
1612 )\r
1613{\r
1614 EFI_UDP_HEADER *Udp6Header;\r
1615 UINT16 HeadSum;\r
1616 EFI_UDP6_RECEIVE_DATA RxData;\r
1617 EFI_UDP6_SESSION_DATA *Udp6Session;\r
1618 UINTN Enqueued;\r
1619\r
0f333664 1620 if (Packet->TotalSize < UDP6_HEADER_SIZE) {\r
37b68011
FS
1621 NetbufFree (Packet);\r
1622 return;\r
1623 }\r
1624 \r
a3bcde70
HT
1625 //\r
1626 // Get the datagram header from the packet buffer.\r
1627 //\r
1628 Udp6Header = (EFI_UDP_HEADER *) NetbufGetByte (Packet, 0, NULL);\r
1629 ASSERT (Udp6Header != NULL);\r
2c4a45b3
WF
1630 if (Udp6Header == NULL) {\r
1631 NetbufFree (Packet);\r
1632 return;\r
1633 }\r
a3bcde70
HT
1634\r
1635 if (Udp6Header->Checksum != 0) {\r
1636 //\r
1637 // check the checksum.\r
1638 //\r
1639 HeadSum = NetIp6PseudoHeadChecksum (\r
1640 &NetSession->Source.v6,\r
1641 &NetSession->Dest.v6,\r
1642 EFI_IP_PROTO_UDP,\r
1643 0\r
1644 );\r
1645\r
1646 if (Udp6Checksum (Packet, HeadSum) != 0) {\r
1647 //\r
1648 // Wrong checksum.\r
1649 //\r
37b68011 1650 NetbufFree (Packet);\r
a3bcde70
HT
1651 return;\r
1652 }\r
1653 }\r
1654\r
a3bcde70
HT
1655 Udp6Session = &RxData.UdpSession;\r
1656 Udp6Session->SourcePort = NTOHS (Udp6Header->SrcPort);\r
1657 Udp6Session->DestinationPort = NTOHS (Udp6Header->DstPort);\r
1658\r
1659 IP6_COPY_ADDRESS (&Udp6Session->SourceAddress, &NetSession->Source);\r
1660 IP6_COPY_ADDRESS (&Udp6Session->DestinationAddress, &NetSession->Dest);\r
1661\r
1662 //\r
1663 // Trim the UDP header.\r
1664 //\r
1665 NetbufTrim (Packet, UDP6_HEADER_SIZE, TRUE);\r
1666\r
1667 RxData.DataLength = (UINT32) Packet->TotalSize;\r
1668\r
1669 //\r
1670 // Try to enqueue this datagram into the instances.\r
1671 //\r
1672 Enqueued = Udp6EnqueueDgram (Udp6Service, Packet, &RxData);\r
1673\r
1674 if (Enqueued == 0) {\r
1675 //\r
1676 // Send the port unreachable ICMP packet before we free this NET_BUF\r
1677 //\r
1678 Udp6SendPortUnreach (Udp6Service->IpIo, NetSession, Udp6Header);\r
1679 }\r
1680\r
1681 //\r
1682 // Try to free the packet before deliver it.\r
1683 //\r
1684 NetbufFree (Packet);\r
1685\r
1686 if (Enqueued > 0) {\r
1687 //\r
1688 // Deliver the datagram.\r
1689 //\r
1690 Udp6DeliverDgram (Udp6Service);\r
1691 }\r
1692}\r
1693\r
1694\r
1695/**\r
1696 This function builds and sends out a icmp port unreachable message.\r
1697\r
1698 @param[in] IpIo Pointer to the IP_IO instance.\r
1699 @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA of the packet\r
1700 causes this icmp error message.\r
1701 @param[in] Udp6Header Pointer to the udp header of the datagram causes\r
1702 this icmp error message.\r
1703\r
1704**/\r
1705VOID\r
1706Udp6SendPortUnreach (\r
1707 IN IP_IO *IpIo,\r
1708 IN EFI_NET_SESSION_DATA *NetSession,\r
1709 IN VOID *Udp6Header\r
1710 )\r
1711{\r
1712 NET_BUF *Packet;\r
1713 UINT32 Len;\r
1714 IP6_ICMP_ERROR_HEAD *IcmpErrHdr;\r
1715 UINT8 *Ptr;\r
1716 IP_IO_OVERRIDE Override;\r
1717 IP_IO_IP_INFO *IpSender;\r
1718 EFI_IP6_MODE_DATA *Ip6ModeData;\r
1719 EFI_STATUS Status;\r
1720 EFI_IP6_PROTOCOL *Ip6Protocol;\r
1721\r
1722 Ip6ModeData = NULL;\r
1723\r
1724 //\r
1725 // An ICMPv6 error message MUST NOT be originated as A packet destined to\r
1726 // 1) an IPv6 multicast address 2) The IPv6 Unspecified Address\r
1727 //\r
1728 if (NetSession->IpVersion == IP_VERSION_6) {\r
1729 if (NetIp6IsUnspecifiedAddr (&NetSession->Dest.v6) ||\r
1730 IP6_IS_MULTICAST (&NetSession->Dest.v6)\r
1731 ) {\r
1732 goto EXIT;\r
1733 }\r
1734 }\r
1735\r
1736\r
1737 IpSender = IpIoFindSender (&IpIo, NetSession->IpVersion, &NetSession->Dest);\r
1738\r
1739 //\r
1740 // Get the Ipv6 Mode Data.\r
1741 //\r
1742 Ip6ModeData = AllocateZeroPool (sizeof (EFI_IP6_MODE_DATA));\r
1743 ASSERT (Ip6ModeData != NULL);\r
2c4a45b3
WF
1744 if (Ip6ModeData == NULL) {\r
1745 goto EXIT;\r
1746 }\r
a3bcde70
HT
1747\r
1748 //\r
1749 // If not finding the related IpSender use the default IpIo to send out\r
1750 // the port unreachable ICMP message.\r
1751 //\r
1752 if (IpSender == NULL) {\r
1753 Ip6Protocol = IpIo->Ip.Ip6;\r
1754 } else {\r
1755 Ip6Protocol = IpSender->Ip.Ip6;\r
1756 }\r
1757\r
1758 Status = Ip6Protocol->GetModeData (\r
1759 Ip6Protocol,\r
1760 Ip6ModeData,\r
1761 NULL,\r
1762 NULL\r
1763 );\r
1764\r
1765 if (EFI_ERROR (Status)) {\r
1766 goto EXIT;\r
1767 }\r
1768 //\r
1769 // The ICMP6 packet length, includes whole invoking packet and ICMP6 error header.\r
1770 //\r
1771 Len = NetSession->IpHdrLen +\r
1772 NTOHS(((EFI_UDP_HEADER *) Udp6Header)->Length) +\r
1773 sizeof (IP6_ICMP_ERROR_HEAD);\r
1774\r
1775 //\r
1776 // If the ICMP6 packet length larger than IP MTU, adjust its length to MTU.\r
1777 //\r
1778 if (Ip6ModeData->MaxPacketSize < Len) {\r
1779 Len = Ip6ModeData->MaxPacketSize;\r
1780 }\r
1781\r
1782 //\r
1783 // Allocate buffer for the icmp error message.\r
1784 //\r
1785 Packet = NetbufAlloc (Len);\r
1786 if (Packet == NULL) {\r
1787 goto EXIT;\r
1788 }\r
1789\r
1790 //\r
1791 // Allocate space for the IP6_ICMP_ERROR_HEAD.\r
1792 //\r
1793 IcmpErrHdr = (IP6_ICMP_ERROR_HEAD *) NetbufAllocSpace (Packet, Len, FALSE);\r
1794 ASSERT (IcmpErrHdr != NULL);\r
2c4a45b3
WF
1795 if (IcmpErrHdr == NULL) {\r
1796 goto EXIT;\r
1797 }\r
a3bcde70
HT
1798\r
1799 //\r
1800 // Set the required fields for the icmp port unreachable message.\r
1801 //\r
1802 IcmpErrHdr->Head.Type = ICMP_V6_DEST_UNREACHABLE;\r
1803 IcmpErrHdr->Head.Code = ICMP_V6_PORT_UNREACHABLE;\r
1804 IcmpErrHdr->Head.Checksum = 0;\r
1805 IcmpErrHdr->Fourth = 0;\r
1806\r
1807 //\r
1808 // Copy as much of invoking Packet as possible without the ICMPv6 packet\r
1809 // exceeding the minimum Ipv6 MTU. The length of IP6_ICMP_ERROR_HEAD contains\r
1810 // the length of EFI_IP6_HEADER, so when using the length of IP6_ICMP_ERROR_HEAD\r
1811 // for pointer movement that fact should be considered.\r
1812 //\r
1813 Ptr = (VOID *) &IcmpErrHdr->Head;\r
1814 Ptr = (UINT8 *) (UINTN) ((UINTN) Ptr + sizeof (IP6_ICMP_ERROR_HEAD) - sizeof (EFI_IP6_HEADER));\r
1815 CopyMem (Ptr, NetSession->IpHdr.Ip6Hdr, NetSession->IpHdrLen);\r
1816 CopyMem (\r
1817 Ptr + NetSession->IpHdrLen,\r
1818 Udp6Header,\r
1819 Len - NetSession->IpHdrLen - sizeof (IP6_ICMP_ERROR_HEAD) + sizeof (EFI_IP6_HEADER)\r
1820 );\r
1821\r
1822 //\r
1823 // Set the checksum as zero, and IP6 driver will calcuate it with pseudo header.\r
1824 //\r
1825 IcmpErrHdr->Head.Checksum = 0;\r
1826\r
1827 //\r
1828 // Fill the override data.\r
1829 //\r
1830 Override.Ip6OverrideData.FlowLabel = 0;\r
1831 Override.Ip6OverrideData.HopLimit = 255;\r
1832 Override.Ip6OverrideData.Protocol = IP6_ICMP;\r
1833\r
1834 //\r
1835 // Send out this icmp packet.\r
1836 //\r
1837 IpIoSend (IpIo, Packet, IpSender, NULL, NULL, &NetSession->Source, &Override);\r
1838\r
1839 NetbufFree (Packet);\r
1840\r
1841EXIT:\r
1842 if (Ip6ModeData != NULL) {\r
1843 FreePool (Ip6ModeData);\r
1844 }\r
1845}\r
1846\r
1847\r
1848/**\r
1849 This function handles the received Icmp Error message and de-multiplexes it to the\r
1850 instance.\r
1851\r
1852 @param[in] Udp6Service Pointer to the udp service context data.\r
1853 @param[in] IcmpError The icmp error code.\r
1854 @param[in] NetSession Pointer to the EFI_NET_SESSION_DATA abstracted\r
1855 from the received Icmp Error packet.\r
1856 @param[in, out] Packet Pointer to the Icmp Error packet.\r
1857\r
1858**/\r
1859VOID\r
1860Udp6IcmpHandler (\r
1861 IN UDP6_SERVICE_DATA *Udp6Service,\r
1862 IN UINT8 IcmpError,\r
1863 IN EFI_NET_SESSION_DATA *NetSession,\r
1864 IN OUT NET_BUF *Packet\r
1865 )\r
1866{\r
1867 EFI_UDP_HEADER *Udp6Header;\r
1868 EFI_UDP6_SESSION_DATA Udp6Session;\r
1869 LIST_ENTRY *Entry;\r
1870 UDP6_INSTANCE_DATA *Instance;\r
1871\r
0f333664 1872 if (Packet->TotalSize < UDP6_HEADER_SIZE) {\r
37b68011
FS
1873 NetbufFree (Packet);\r
1874 return;\r
1875 }\r
1876 \r
a3bcde70
HT
1877 Udp6Header = (EFI_UDP_HEADER *) NetbufGetByte (Packet, 0, NULL);\r
1878 ASSERT (Udp6Header != NULL);\r
2c4a45b3
WF
1879 if (Udp6Header == NULL) {\r
1880 NetbufFree (Packet);\r
1881 return;\r
1882 }\r
a3bcde70
HT
1883\r
1884 IP6_COPY_ADDRESS (&Udp6Session.SourceAddress, &NetSession->Source);\r
1885 IP6_COPY_ADDRESS (&Udp6Session.DestinationAddress, &NetSession->Dest);\r
1886\r
1887 Udp6Session.SourcePort = NTOHS (Udp6Header->DstPort);\r
1888 Udp6Session.DestinationPort = NTOHS (Udp6Header->SrcPort);\r
1889\r
1890 NET_LIST_FOR_EACH (Entry, &Udp6Service->ChildrenList) {\r
1891 //\r
1892 // Iterate all the instances.\r
1893 //\r
1894 Instance = NET_LIST_USER_STRUCT (Entry, UDP6_INSTANCE_DATA, Link);\r
1895\r
1896 if (!Instance->Configured) {\r
1897 continue;\r
1898 }\r
1899\r
1900 if (Udp6MatchDgram (Instance, &Udp6Session)) {\r
1901 //\r
1902 // Translate the Icmp Error code according to the udp spec.\r
1903 //\r
1904 Instance->IcmpError = IpIoGetIcmpErrStatus (IcmpError, IP_VERSION_6, NULL, NULL);\r
1905\r
1906 if (IcmpError > ICMP_ERR_UNREACH_PORT) {\r
1907 Instance->IcmpError = EFI_ICMP_ERROR;\r
1908 }\r
1909\r
1910 //\r
1911 // Notify the instance with the received Icmp Error.\r
1912 //\r
1913 Udp6ReportIcmpError (Instance);\r
1914\r
1915 break;\r
1916 }\r
1917 }\r
1918\r
1919 NetbufFree (Packet);\r
1920}\r
1921\r
1922\r
1923/**\r
1924 This function reports the received ICMP error.\r
1925\r
1926 @param[in] Instance Pointer to the udp instance context data.\r
1927\r
1928**/\r
1929VOID\r
1930Udp6ReportIcmpError (\r
1931 IN UDP6_INSTANCE_DATA *Instance\r
1932 )\r
1933{\r
1934 EFI_UDP6_COMPLETION_TOKEN *Token;\r
1935\r
1936 if (NetMapIsEmpty (&Instance->RxTokens)) {\r
1937 //\r
1938 // There are no receive tokens to deliver the ICMP error.\r
1939 //\r
1940 return;\r
1941 }\r
1942\r
1943 if (EFI_ERROR (Instance->IcmpError)) {\r
1944 //\r
1945 // Try to get a RxToken from the RxTokens map.\r
1946 //\r
1947 Token = (EFI_UDP6_COMPLETION_TOKEN *) NetMapRemoveHead (&Instance->RxTokens, NULL);\r
1948\r
1949 if (Token != NULL) {\r
1950 //\r
1951 // Report the error through the Token.\r
1952 //\r
1953 Token->Status = Instance->IcmpError;\r
1954 gBS->SignalEvent (Token->Event);\r
1955\r
1956 //\r
1957 // Clear the IcmpError.\r
1958 //\r
1959 Instance->IcmpError = EFI_SUCCESS;\r
1960 }\r
1961 }\r
1962}\r
1963\r
1964\r
1965/**\r
1966 This function is a dummy ext-free function for the NET_BUF created for the output\r
1967 udp datagram.\r
1968\r
1969 @param[in] Context Pointer to the context data.\r
1970\r
1971**/\r
1972VOID\r
1973EFIAPI\r
1974Udp6NetVectorExtFree (\r
1975 IN VOID *Context\r
1976 )\r
1977{\r
d551cc64 1978} \r
a3bcde70
HT
1979\r
1980/**\r
1981 Find the key in the netmap.\r
1982\r
1983 @param[in] Map The netmap to search within.\r
1984 @param[in] Key The key to search.\r
1985\r
1986 @return The point to the item contains the Key, or NULL, if Key isn't in the map.\r
1987\r
1988**/\r
1989NET_MAP_ITEM *\r
1990Udp6MapMultiCastAddr (\r
1991 IN NET_MAP *Map,\r
1992 IN VOID *Key\r
1993 )\r
1994{\r
1995 LIST_ENTRY *Entry;\r
1996 NET_MAP_ITEM *Item;\r
1997 EFI_IPv6_ADDRESS *Addr;\r
1998\r
1999 ASSERT (Map != NULL);\r
2000 NET_LIST_FOR_EACH (Entry, &Map->Used) {\r
2001 Item = NET_LIST_USER_STRUCT (Entry, NET_MAP_ITEM, Link);\r
2002 Addr = (EFI_IPv6_ADDRESS *) Item->Key;\r
2003 if (EFI_IP6_EQUAL (Addr, Key)) {\r
2004 return Item;\r
2005 }\r
2006 }\r
2007 return NULL;\r
2008}\r
2009\r