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