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