]> git.proxmox.com Git - mirror_edk2.git/blame - NetworkPkg/Udp6Dxe/Udp6Main.c
NetworkPkg: Fix a memory leak issue in UDP6 driver
[mirror_edk2.git] / NetworkPkg / Udp6Dxe / Udp6Main.c
CommitLineData
a3bcde70
HT
1/** @file\r
2 Contains all EFI_UDP6_PROTOCOL interfaces.\r
3\r
2c4a45b3 4 Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>\r
a3bcde70
HT
5\r
6 This program and the accompanying materials\r
7 are licensed and made available under the terms and conditions of the BSD License\r
8 which accompanies this distribution. The full text of the license may be found at\r
9 http://opensource.org/licenses/bsd-license.php.\r
10\r
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
14**/\r
15\r
16#include "Udp6Impl.h"\r
17\r
18EFI_UDP6_PROTOCOL mUdp6Protocol = {\r
19 Udp6GetModeData,\r
20 Udp6Configure,\r
21 Udp6Groups,\r
22 Udp6Transmit,\r
23 Udp6Receive,\r
24 Udp6Cancel,\r
25 Udp6Poll\r
26};\r
27\r
28\r
29/**\r
30 This function copies the current operational settings of this EFI UDPv6 Protocol\r
31 instance into user-supplied buffers. This function is used optionally to retrieve\r
32 the operational mode data of underlying networks or drivers.\r
33\r
34 @param[in] This Pointer to the EFI_UDP6_PROTOCOL instance.\r
35 @param[out] Udp6ConfigData The buffer in which the current UDP configuration\r
36 data is returned. This parameter is optional and\r
37 may be NULL.\r
38 @param[out] Ip6ModeData The buffer in which the current EFI IPv6 Protocol\r
39 mode data is returned. This parameter is optional\r
40 and may be NULL.\r
41 @param[out] MnpConfigData The buffer in which the current managed network\r
42 configuration data is returned. This parameter is\r
43 optional and may be NULL.\r
44 @param[out] SnpModeData The buffer in which the simple network mode data\r
45 is returned. This parameter is optional and may be NULL.\r
46\r
47 @retval EFI_SUCCESS The mode data was read.\r
48 @retval EFI_NOT_STARTED When Udp6ConfigData is queried, no configuration\r
49 data is available because this instance has not\r
50 been started.\r
51 @retval EFI_INVALID_PARAMETER This is NULL.\r
52\r
53**/\r
54EFI_STATUS\r
55EFIAPI\r
56Udp6GetModeData (\r
57 IN EFI_UDP6_PROTOCOL *This,\r
58 OUT EFI_UDP6_CONFIG_DATA *Udp6ConfigData OPTIONAL,\r
59 OUT EFI_IP6_MODE_DATA *Ip6ModeData OPTIONAL,\r
60 OUT EFI_MANAGED_NETWORK_CONFIG_DATA *MnpConfigData OPTIONAL,\r
61 OUT EFI_SIMPLE_NETWORK_MODE *SnpModeData OPTIONAL\r
62 )\r
63{\r
64 UDP6_INSTANCE_DATA *Instance;\r
65 EFI_IP6_PROTOCOL *Ip;\r
66 EFI_TPL OldTpl;\r
67 EFI_STATUS Status;\r
68\r
69 if (This == NULL) {\r
70 return EFI_INVALID_PARAMETER;\r
71 }\r
72\r
73 Instance = UDP6_INSTANCE_DATA_FROM_THIS (This);\r
74\r
75 if (!Instance->Configured && (Udp6ConfigData != NULL)) {\r
76 return EFI_NOT_STARTED;\r
77 }\r
78\r
79 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
80\r
81 if (Udp6ConfigData != NULL) {\r
82 //\r
83 // Set the Udp6ConfigData.\r
84 //\r
85 CopyMem (Udp6ConfigData, &Instance->ConfigData, sizeof (EFI_UDP6_CONFIG_DATA));\r
86 }\r
87\r
88 Ip = Instance->IpInfo->Ip.Ip6;\r
89\r
90 //\r
91 // Get the underlying Ip6ModeData, MnpConfigData and SnpModeData.\r
92 //\r
93 Status = Ip->GetModeData (Ip, Ip6ModeData, MnpConfigData, SnpModeData);\r
94\r
95 gBS->RestoreTPL (OldTpl);\r
96\r
97 return Status;\r
98}\r
99\r
100\r
101/**\r
102 This function is used to do the following:\r
103 Initialize and start this instance of the EFI UDPv6 Protocol.\r
104 Change the filtering rules and operational parameters.\r
105 Reset this instance of the EFI UDPv6 Protocol.\r
106\r
107 @param[in] This Pointer to the EFI_UDP6_PROTOCOL instance.\r
108 @param[in] UdpConfigData Pointer to the buffer to set the configuration\r
109 data. This parameter is optional and may be NULL.\r
110\r
111 @retval EFI_SUCCESS The configuration settings were set, changed, or\r
112 reset successfully.\r
113 @retval EFI_NO_MAPPING When the UdpConifgData.UseAnyStationAddress is set\r
114 to true and there is no address available for the IP6\r
115 driver to bind a source address to this instance.\r
116 @retval EFI_INVALID_PARAMETER One or more following conditions are TRUE:\r
117 This is NULL.\r
118 UdpConfigData.StationAddress is not a valid\r
119 unicast IPv6 address.\r
120 UdpConfigData.RemoteAddress is not a valid unicast\r
121 IPv6 address if it is not zero.\r
122 @retval EFI_ALREADY_STARTED The EFI UDPv6 Protocol instance is already\r
123 started/configured and must be stopped/reset\r
124 before it can be reconfigured. Only TrafficClass,\r
125 HopLimit, ReceiveTimeout, and TransmitTimeout can\r
126 be reconfigured without stopping the current\r
127 instance of the EFI UDPv6 Protocol.\r
128 @retval EFI_ACCESS_DENIED UdpConfigData.AllowDuplicatePort is FALSE and\r
129 UdpConfigData.StationPort is already used by another\r
130 instance.\r
131 @retval EFI_OUT_OF_RESOURCES The EFI UDPv6 Protocol driver cannot allocate\r
132 memory for this EFI UDPv6 Protocol instance.\r
133 @retval EFI_DEVICE_ERROR An unexpected network or system error occurred, and\r
134 this instance was not opened.\r
135\r
136**/\r
137EFI_STATUS\r
138EFIAPI\r
139Udp6Configure (\r
140 IN EFI_UDP6_PROTOCOL *This,\r
141 IN EFI_UDP6_CONFIG_DATA *UdpConfigData OPTIONAL\r
142 )\r
143{\r
144 EFI_STATUS Status;\r
145 UDP6_INSTANCE_DATA *Instance;\r
146 UDP6_SERVICE_DATA *Udp6Service;\r
147 EFI_TPL OldTpl;\r
148 EFI_IPv6_ADDRESS StationAddress;\r
149 EFI_IPv6_ADDRESS RemoteAddress;\r
150 EFI_IP6_CONFIG_DATA Ip6ConfigData;\r
151 EFI_IPv6_ADDRESS LocalAddr;\r
152 EFI_IPv6_ADDRESS RemoteAddr;\r
153\r
154 if (This == NULL) {\r
155 return EFI_INVALID_PARAMETER;\r
156 }\r
157\r
158 Instance = UDP6_INSTANCE_DATA_FROM_THIS (This);\r
159\r
160 if (!Instance->Configured && (UdpConfigData == NULL)) {\r
161 return EFI_SUCCESS;\r
162 }\r
163\r
164 Udp6Service = Instance->Udp6Service;\r
165 Status = EFI_SUCCESS;\r
166 ASSERT (Udp6Service != NULL);\r
167\r
168 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
169\r
170 if (UdpConfigData != NULL) {\r
171\r
172 IP6_COPY_ADDRESS (&StationAddress, &UdpConfigData->StationAddress);\r
173 IP6_COPY_ADDRESS (&RemoteAddress, &UdpConfigData->RemoteAddress);\r
174\r
175 if ((!NetIp6IsUnspecifiedAddr (&StationAddress) && !NetIp6IsValidUnicast (&StationAddress)) ||\r
176 (!NetIp6IsUnspecifiedAddr (&RemoteAddress) && !NetIp6IsValidUnicast (&RemoteAddress))\r
177 ){\r
178 //\r
179 // If not use default address, and StationAddress is not a valid unicast\r
180 // if it is not IPv6 address or RemoteAddress is not a valid unicast IPv6\r
181 // address if it is not 0.\r
182 //\r
183 Status = EFI_INVALID_PARAMETER;\r
184 goto ON_EXIT;\r
185 }\r
186\r
187 if (Instance->Configured) {\r
188 //\r
189 // The instance is already configured, try to do the re-configuration.\r
190 //\r
191 if (!Udp6IsReconfigurable (&Instance->ConfigData, UdpConfigData)) {\r
192 //\r
193 // If the new configuration data wants to change some unreconfigurable\r
194 // settings, return EFI_ALREADY_STARTED.\r
195 //\r
196 Status = EFI_ALREADY_STARTED;\r
197 goto ON_EXIT;\r
198 }\r
199\r
200 //\r
201 // Save the reconfigurable parameters.\r
202 //\r
203 Instance->ConfigData.TrafficClass = UdpConfigData->TrafficClass;\r
204 Instance->ConfigData.HopLimit = UdpConfigData->HopLimit;\r
205 Instance->ConfigData.ReceiveTimeout = UdpConfigData->ReceiveTimeout;\r
206 Instance->ConfigData.TransmitTimeout = UdpConfigData->TransmitTimeout;\r
207 } else {\r
208 //\r
209 // Construct the Ip configuration data from the UdpConfigData.\r
210 //\r
211 Udp6BuildIp6ConfigData (UdpConfigData, &Ip6ConfigData);\r
212\r
213 //\r
214 // Configure the Ip instance wrapped in the IpInfo.\r
215 //\r
216 Status = IpIoConfigIp (Instance->IpInfo, &Ip6ConfigData);\r
217 if (EFI_ERROR (Status)) {\r
218 if (Status == EFI_NO_MAPPING) {\r
219 Instance->IsNoMapping = TRUE;\r
220 }\r
221\r
222 goto ON_EXIT;\r
223 }\r
224\r
225 Instance->IsNoMapping = FALSE;\r
226\r
227 //\r
228 // Save the configuration data.\r
229 //\r
230 CopyMem (\r
231 &Instance->ConfigData,\r
232 UdpConfigData,\r
233 sizeof (EFI_UDP6_CONFIG_DATA)\r
234 );\r
235 IP6_COPY_ADDRESS (&Instance->ConfigData.StationAddress, &Ip6ConfigData.StationAddress);\r
236 //\r
237 // Try to allocate the required port resource.\r
238 //\r
239 Status = Udp6Bind (&Udp6Service->ChildrenList, &Instance->ConfigData);\r
240 if (EFI_ERROR (Status)) {\r
241 //\r
242 // Reset the ip instance if bind fails.\r
243 //\r
244 IpIoConfigIp (Instance->IpInfo, NULL);\r
245 goto ON_EXIT;\r
246 }\r
247\r
248 //\r
249 // Pre calculate the checksum for the pseudo head, ignore the UDP length first.\r
250 //\r
251 IP6_COPY_ADDRESS (&LocalAddr, &Instance->ConfigData.StationAddress);\r
252 IP6_COPY_ADDRESS (&RemoteAddr, &Instance->ConfigData.RemoteAddress);\r
253\r
254 Instance->HeadSum = NetIp6PseudoHeadChecksum (\r
255 &LocalAddr,\r
256 &RemoteAddr,\r
257 EFI_IP_PROTO_UDP,\r
258 0\r
259 );\r
260\r
261 Instance->Configured = TRUE;\r
262 }\r
263 } else {\r
264 //\r
265 // UdpConfigData is NULL, reset the instance.\r
266 //\r
267 Instance->Configured = FALSE;\r
268 Instance->IsNoMapping = FALSE;\r
269\r
270 //\r
271 // Reset the Ip instance wrapped in the IpInfo.\r
272 //\r
273 IpIoConfigIp (Instance->IpInfo, NULL);\r
274\r
275 //\r
276 // Cancel all the user tokens.\r
277 //\r
d551cc64 278 Instance->Udp6Proto.Cancel (&Instance->Udp6Proto, NULL);\r
a3bcde70
HT
279\r
280 //\r
281 // Remove the buffered RxData for this instance.\r
282 //\r
283 Udp6FlushRcvdDgram (Instance);\r
284\r
285 ASSERT (IsListEmpty (&Instance->DeliveredDgramQue));\r
286 }\r
d551cc64 287 \r
a3bcde70
HT
288ON_EXIT:\r
289\r
290 gBS->RestoreTPL (OldTpl);\r
291\r
292 return Status;\r
293}\r
294\r
295\r
296/**\r
297 This function is used to enable and disable the multicast group filtering.\r
298\r
299 @param[in] This Pointer to the EFI_UDP6_PROTOCOL instance.\r
300 @param[in] JoinFlag Set to TRUE to join a multicast group. Set to\r
301 FALSE to leave one or all multicast groups.\r
302 @param[in] MulticastAddress Pointer to multicast group address to join or\r
303 leave. This parameter is optional and may be NULL.\r
304\r
305 @retval EFI_SUCCESS The operation completed successfully.\r
306 @retval EFI_NOT_STARTED The EFI UDPv6 Protocol instance has not been\r
307 started.\r
308 @retval EFI_OUT_OF_RESOURCES Could not allocate resources to join the group.\r
309 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:\r
310 This is NULL.\r
311 JoinFlag is TRUE and MulticastAddress is NULL.\r
312 JoinFlag is TRUE and *MulticastAddress is not a\r
313 valid multicast address.\r
314 @retval EFI_ALREADY_STARTED The group address is already in the group table\r
315 (when JoinFlag is TRUE).\r
316 @retval EFI_NOT_FOUND The group address is not in the group table (when\r
317 JoinFlag is FALSE).\r
318 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.\r
319\r
320**/\r
321EFI_STATUS\r
322EFIAPI\r
323Udp6Groups (\r
324 IN EFI_UDP6_PROTOCOL *This,\r
325 IN BOOLEAN JoinFlag,\r
326 IN EFI_IPv6_ADDRESS *MulticastAddress OPTIONAL\r
327 )\r
328{\r
329 EFI_STATUS Status;\r
330 UDP6_INSTANCE_DATA *Instance;\r
331 EFI_IP6_PROTOCOL *Ip;\r
332 EFI_TPL OldTpl;\r
333 EFI_IPv6_ADDRESS *McastIp;\r
334\r
335 if ((This == NULL) || (JoinFlag && (MulticastAddress == NULL))) {\r
336 return EFI_INVALID_PARAMETER;\r
337 }\r
338\r
339 McastIp = NULL;\r
340\r
341 if (JoinFlag) {\r
342 if (!IP6_IS_MULTICAST (MulticastAddress)) {\r
343 return EFI_INVALID_PARAMETER;\r
344 }\r
345\r
346 McastIp = AllocateCopyPool (sizeof (EFI_IPv6_ADDRESS), MulticastAddress);\r
347 if (McastIp == NULL) {\r
348 return EFI_OUT_OF_RESOURCES;\r
349 }\r
350 }\r
351\r
352 Instance = UDP6_INSTANCE_DATA_FROM_THIS (This);\r
353 if (!Instance->Configured) {\r
24ed9cfd
WF
354 if (McastIp != NULL) {\r
355 FreePool (McastIp);\r
356 }\r
a3bcde70
HT
357 return EFI_NOT_STARTED;\r
358 }\r
359\r
360 Ip = Instance->IpInfo->Ip.Ip6;\r
361\r
362 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
363\r
364 //\r
365 // Invoke the Ip instance the Udp6 instance consumes to do the group operation.\r
366 //\r
367 Status = Ip->Groups (Ip, JoinFlag, MulticastAddress);\r
368\r
369 if (EFI_ERROR (Status)) {\r
370 goto ON_EXIT;\r
371 }\r
372\r
373 //\r
374 // Keep a local copy of the configured multicast IPs because IpIo receives\r
375 // datagrams from the 0 station address IP instance and then UDP delivers to\r
376 // the matched instance. This copy of multicast IPs is used to avoid receive\r
377 // the mutlicast datagrams destinated to multicast IPs the other instances configured.\r
378 //\r
379 if (JoinFlag) {\r
380\r
381 Status = NetMapInsertTail (&Instance->McastIps, (VOID *) McastIp, NULL);\r
382 } else {\r
383\r
384 NetMapIterate (&Instance->McastIps, Udp6LeaveGroup, MulticastAddress);\r
385 }\r
386\r
387ON_EXIT:\r
388\r
389 gBS->RestoreTPL (OldTpl);\r
390\r
391 if (EFI_ERROR (Status)) {\r
392 if (McastIp != NULL) {\r
393 FreePool (McastIp);\r
394 }\r
395 }\r
396\r
397 return Status;\r
398}\r
399\r
400\r
401\r
402/**\r
403 This function places a sending request to this instance of the EFI UDPv6 Protocol,\r
404 alongside the transmit data that was filled by the user.\r
405\r
406 @param[in] This Pointer to the EFI_UDP6_PROTOCOL instance.\r
407 @param[in] Token Pointer to the completion token that will be\r
408 placed into the transmit queue.\r
409\r
410 @retval EFI_SUCCESS The data was queued for transmission.\r
411 @retval EFI_NOT_STARTED This EFI UDPv6 Protocol instance has not been\r
412 started.\r
413 @retval EFI_NO_MAPPING The under-layer IPv6 driver was responsible for\r
414 choosing a source address for this instance, but\r
415 no source address was available for use.\r
416 @retval EFI_INVALID_PARAMETER One or more of the following are TRUE:\r
417 This is NULL.\r
418 Token is NULL. Token.Event is NULL.\r
419 Token.Packet.TxData is NULL.\r
420 Token.Packet.TxData.FragmentCount is zero.\r
421 Token.Packet.TxData.DataLength is not equal to the\r
422 sum of fragment lengths.\r
423 One or more of the\r
424 Token.Packet.TxData.FragmentTable[].FragmentLength\r
425 fields is zero.\r
426 One or more of the\r
427 Token.Packet.TxData.FragmentTable[].FragmentBuffer\r
428 fields is NULL. One or more of the\r
429 Token.Packet.TxData.UdpSessionData.DestinationAddres\r
430 are not valid unicast IPv6\r
431 addresses if the UdpSessionData is not NULL.\r
432 Token.Packet.TxData.UdpSessionData.\r
433 DestinationAddress is NULL\r
434 Token.Packet.TxData.UdpSessionData.\r
435 DestinatioPort\r
436 is zero.\r
437 Token.Packet.TxData.UdpSessionData is NULL and this\r
438 instance's UdpConfigData.RemoteAddress is unspecified.\r
439 @retval EFI_ACCESS_DENIED The transmit completion token with the same\r
440 Token.Event is already in the transmit queue.\r
441 @retval EFI_NOT_READY The completion token could not be queued because\r
442 the transmit queue is full.\r
443 @retval EFI_OUT_OF_RESOURCES Could not queue the transmit data.\r
444 @retval EFI_NOT_FOUND There is no route to the destination network or\r
445 address.\r
446 @retval EFI_BAD_BUFFER_SIZE The data length is greater than the maximum UDP\r
447 packet size. Or, the length of the IP header + UDP\r
448 header + data length is greater than MTU if\r
449 DoNotFragment is TRUE.\r
450\r
451**/\r
452EFI_STATUS\r
453EFIAPI\r
454Udp6Transmit (\r
455 IN EFI_UDP6_PROTOCOL *This,\r
456 IN EFI_UDP6_COMPLETION_TOKEN *Token\r
457 )\r
458{\r
459 EFI_STATUS Status;\r
460 UDP6_INSTANCE_DATA *Instance;\r
461 EFI_TPL OldTpl;\r
462 NET_BUF *Packet;\r
463 EFI_UDP_HEADER *Udp6Header;\r
464 EFI_UDP6_CONFIG_DATA *ConfigData;\r
465 EFI_IPv6_ADDRESS Source;\r
466 EFI_IPv6_ADDRESS Destination;\r
467 EFI_UDP6_TRANSMIT_DATA *TxData;\r
468 EFI_UDP6_SESSION_DATA *UdpSessionData;\r
469 UDP6_SERVICE_DATA *Udp6Service;\r
470 IP_IO_OVERRIDE Override;\r
471 UINT16 HeadSum;\r
472 EFI_IP_ADDRESS IpDestAddr;\r
473\r
474 if ((This == NULL) || (Token == NULL)) {\r
475 return EFI_INVALID_PARAMETER;\r
476 }\r
477\r
478 Instance = UDP6_INSTANCE_DATA_FROM_THIS (This);\r
479\r
480 if (!Instance->Configured) {\r
481 return EFI_NOT_STARTED;\r
482 }\r
483\r
484 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
485\r
486 //\r
487 // Validate the Token, if the token is invalid return the error code.\r
488 //\r
489 Status = Udp6ValidateTxToken (Instance, Token);\r
490 if (EFI_ERROR (Status)) {\r
491 goto ON_EXIT;\r
492 }\r
493\r
494 if (EFI_ERROR (NetMapIterate (&Instance->TxTokens, Udp6TokenExist, Token)) ||\r
495 EFI_ERROR (NetMapIterate (&Instance->RxTokens, Udp6TokenExist, Token))\r
496 ){\r
497 //\r
498 // Try to find a duplicate token in the two token maps, if found, return\r
499 // EFI_ACCESS_DENIED.\r
500 //\r
501 Status = EFI_ACCESS_DENIED;\r
502 goto ON_EXIT;\r
503 }\r
504\r
505 TxData = Token->Packet.TxData;\r
506\r
507 //\r
508 // Create a net buffer to hold the user buffer and the udp header.\r
509 //\r
510 Packet = NetbufFromExt (\r
511 (NET_FRAGMENT *) TxData->FragmentTable,\r
512 TxData->FragmentCount,\r
513 UDP6_HEADER_SIZE,\r
514 0,\r
515 Udp6NetVectorExtFree,\r
516 NULL\r
517 );\r
518 if (Packet == NULL) {\r
519 Status = EFI_OUT_OF_RESOURCES;\r
520 goto ON_EXIT;\r
521 }\r
522\r
523 //\r
524 // Store the IpIo in ProtoData.\r
525 //\r
526 Udp6Service = Instance->Udp6Service;\r
527 *((UINTN *) &Packet->ProtoData[0]) = (UINTN) (Udp6Service->IpIo);\r
528\r
529 Udp6Header = (EFI_UDP_HEADER *) NetbufAllocSpace (Packet, UDP6_HEADER_SIZE, TRUE);\r
530 ASSERT (Udp6Header != NULL);\r
2c4a45b3
WF
531 if (Udp6Header == NULL) {\r
532 Status = EFI_OUT_OF_RESOURCES;\r
533 goto ON_EXIT;\r
534 }\r
535 \r
a3bcde70
HT
536 ConfigData = &Instance->ConfigData;\r
537\r
538 //\r
539 // Fill the udp header.\r
540 //\r
541 Udp6Header->SrcPort = HTONS (ConfigData->StationPort);\r
542 Udp6Header->DstPort = HTONS (ConfigData->RemotePort);\r
543 Udp6Header->Length = HTONS ((UINT16) Packet->TotalSize);\r
544 Udp6Header->Checksum = 0;\r
545 //\r
546 // Set the UDP Header in NET_BUF, this UDP header is for IP6 can fast get the\r
547 // Udp header for pseudoHeadCheckSum.\r
548 //\r
549 Packet->Udp = Udp6Header;\r
550 UdpSessionData = TxData->UdpSessionData;\r
551\r
552 if (UdpSessionData != NULL) {\r
553 //\r
554 // Set the Destination according to the specified\r
555 // UdpSessionData.\r
556 //\r
557\r
558 if (UdpSessionData->DestinationPort != 0) {\r
559 Udp6Header->DstPort = HTONS (UdpSessionData->DestinationPort);\r
560 }\r
561\r
562 IP6_COPY_ADDRESS (&Source, &ConfigData->StationAddress);\r
563 if (!NetIp6IsUnspecifiedAddr (&UdpSessionData->DestinationAddress)) {\r
564 IP6_COPY_ADDRESS (&Destination, &UdpSessionData->DestinationAddress);\r
565 } else {\r
566 IP6_COPY_ADDRESS (&Destination, &ConfigData->RemoteAddress);\r
567 }\r
568\r
569 //\r
570 //Calculate the pseudo head checksum using the overridden parameters.\r
571 //\r
572 if (!NetIp6IsUnspecifiedAddr (&ConfigData->StationAddress)) {\r
573 HeadSum = NetIp6PseudoHeadChecksum (\r
574 &Source,\r
575 &Destination,\r
576 EFI_IP_PROTO_UDP,\r
577 0\r
578 );\r
579\r
580 //\r
581 // calculate the checksum.\r
582 //\r
583 Udp6Header->Checksum = Udp6Checksum (Packet, HeadSum);\r
584 if (Udp6Header->Checksum == 0) {\r
585 //\r
586 // If the calculated checksum is 0, fill the Checksum field with all ones.\r
587 //\r
588 Udp6Header->Checksum = 0XFFFF;\r
589 }\r
590 } else {\r
591 //\r
592 // Set the checksum is zero if the ConfigData->StationAddress is unspcified\r
593 // and the Ipv6 will fill the correct value of this checksum.\r
594 //\r
595 Udp6Header->Checksum = 0;\r
596\r
597 }\r
598 } else {\r
599 //\r
600 // UdpSessionData is NULL, use the address and port information previously configured.\r
601 //\r
602 IP6_COPY_ADDRESS (&Destination, &ConfigData->RemoteAddress);\r
603\r
604 HeadSum = Instance->HeadSum;\r
605 //\r
606 // calculate the checksum.\r
607 //\r
608 Udp6Header->Checksum = Udp6Checksum (Packet, HeadSum);\r
609 if (Udp6Header->Checksum == 0) {\r
610 //\r
611 // If the calculated checksum is 0, fill the Checksum field with all ones.\r
612 //\r
613 Udp6Header->Checksum = 0xffff;\r
614 }\r
615 }\r
616\r
617\r
618\r
619 //\r
620 // Fill the IpIo Override data.\r
621 //\r
622 Override.Ip6OverrideData.Protocol = EFI_IP_PROTO_UDP;\r
623 Override.Ip6OverrideData.HopLimit = ConfigData->HopLimit;\r
624 Override.Ip6OverrideData.FlowLabel = 0;\r
625\r
626 //\r
627 // Save the token into the TxToken map.\r
628 //\r
629 Status = NetMapInsertTail (&Instance->TxTokens, Token, Packet);\r
630 if (EFI_ERROR (Status)) {\r
631 goto FREE_PACKET;\r
632 }\r
633\r
634 //\r
635 // Send out this datagram through IpIo.\r
636 //\r
637 if (UdpSessionData != NULL){\r
638 IP6_COPY_ADDRESS (&(IpDestAddr.v6), &Destination);\r
639 } else {\r
640 ZeroMem (&IpDestAddr.v6, sizeof (EFI_IPv6_ADDRESS));\r
641 }\r
642\r
643 Status = IpIoSend (\r
644 Udp6Service->IpIo,\r
645 Packet,\r
646 Instance->IpInfo,\r
647 Instance,\r
648 Token,\r
649 &IpDestAddr,\r
650 &Override\r
651 );\r
652 if (EFI_ERROR (Status)) {\r
653 //\r
654 // Remove this token from the TxTokens.\r
655 //\r
656 Udp6RemoveToken (&Instance->TxTokens, Token);\r
657 }\r
658\r
659FREE_PACKET:\r
660\r
661 NetbufFree (Packet);\r
662\r
663ON_EXIT:\r
664\r
665 gBS->RestoreTPL (OldTpl);\r
666\r
667 return Status;\r
668}\r
669\r
670\r
671/**\r
672 This function places a completion token into the receive packet queue. This function\r
673 is always asynchronous.\r
674\r
675 @param[in] This Pointer to the EFI_UDP6_PROTOCOL instance.\r
676 @param[in] Token Pointer to a token that is associated with the\r
677 receive data descriptor.\r
678\r
679 @retval EFI_SUCCESS The receive completion token was cached.\r
680 @retval EFI_NOT_STARTED This EFI UDPv6 Protocol instance has not been\r
681 started.\r
682 @retval EFI_NO_MAPPING When using a default address, configuration (DHCP,\r
683 BOOTP, RARP, etc.) is not finished yet.\r
684 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:\r
685 This is NULL. Token is NULL. Token.Event is NULL.\r
686 @retval EFI_OUT_OF_RESOURCES The receive completion token could not be queued\r
687 due to a lack of system resources (usually\r
688 memory).\r
689 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.\r
690 The EFI UDPv6 Protocol instance has been reset to\r
691 startup defaults.\r
692 @retval EFI_ACCESS_DENIED A receive completion token with the same\r
693 Token.Event is already in the receive queue.\r
694 @retval EFI_NOT_READY The receive request could not be queued because\r
695 the receive queue is full.\r
696\r
697**/\r
698EFI_STATUS\r
699EFIAPI\r
700Udp6Receive (\r
701 IN EFI_UDP6_PROTOCOL *This,\r
702 IN EFI_UDP6_COMPLETION_TOKEN *Token\r
703 )\r
704{\r
705 EFI_STATUS Status;\r
706 UDP6_INSTANCE_DATA *Instance;\r
707 EFI_TPL OldTpl;\r
708\r
709 if ((This == NULL) || (Token == NULL) || (Token->Event == NULL)) {\r
710 return EFI_INVALID_PARAMETER;\r
711 }\r
712\r
713 Instance = UDP6_INSTANCE_DATA_FROM_THIS (This);\r
714\r
715 if (!Instance->Configured) {\r
716 return EFI_NOT_STARTED;\r
717 }\r
718\r
719 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
720\r
721 if (EFI_ERROR (NetMapIterate (&Instance->RxTokens, Udp6TokenExist, Token)) ||\r
722 EFI_ERROR (NetMapIterate (&Instance->TxTokens, Udp6TokenExist, Token))\r
723 ){\r
724 //\r
725 // Return EFI_ACCESS_DENIED if the specified token is already in the TxTokens or\r
726 // RxTokens map.\r
727 //\r
728 Status = EFI_ACCESS_DENIED;\r
729 goto ON_EXIT;\r
730 }\r
731\r
732 Token->Packet.RxData = NULL;\r
733\r
734 //\r
735 // Save the token into the RxTokens map.\r
736 //\r
737 Status = NetMapInsertTail (&Instance->RxTokens, Token, NULL);\r
738 if (EFI_ERROR (Status)) {\r
739 Status = EFI_NOT_READY;\r
740 goto ON_EXIT;\r
741 }\r
742\r
743 //\r
744 // If there is an icmp error, report it.\r
745 //\r
746 Udp6ReportIcmpError (Instance);\r
747\r
748 //\r
749 // Try to delivered the received datagrams.\r
750 //\r
751 Udp6InstanceDeliverDgram (Instance);\r
752\r
753 //\r
754 // Dispatch the DPC queued by the NotifyFunction of Token->Event.\r
755 //\r
756 DispatchDpc ();\r
757\r
758ON_EXIT:\r
759\r
760 gBS->RestoreTPL (OldTpl);\r
761\r
762 return Status;\r
763}\r
764\r
765\r
766/**\r
767 This function is used to abort a pending transmit or receive request.\r
768\r
769 @param[in] This Pointer to the EFI_UDP6_PROTOCOL instance.\r
770 @param[in] Token Pointer to a token that has been issued by\r
771 EFI_UDP6_PROTOCOL.Transmit() or\r
772 EFI_UDP6_PROTOCOL.Receive(). This parameter is\r
773 optional and may be NULL.\r
774\r
775 @retval EFI_SUCCESS The asynchronous I/O request was aborted, and\r
776 Token.Event was signaled. When Token is NULL, all\r
777 pending requests are aborted and their events are\r
778 signaled.\r
779 @retval EFI_INVALID_PARAMETER This is NULL.\r
780 @retval EFI_NOT_STARTED This instance has not been started.\r
781 @retval EFI_NO_MAPPING When using the default address, configuration\r
782 (DHCP, BOOTP, RARP, etc.) is not finished yet.\r
783 @retval EFI_NOT_FOUND When Token is not NULL, the asynchronous I/O\r
784 request is not found in the transmit or receive\r
785 queue. It is either completed or not issued by\r
786 Transmit() or Receive().\r
787\r
788**/\r
789EFI_STATUS\r
790EFIAPI\r
791Udp6Cancel (\r
792 IN EFI_UDP6_PROTOCOL *This,\r
793 IN EFI_UDP6_COMPLETION_TOKEN *Token OPTIONAL\r
794 )\r
795{\r
796 EFI_STATUS Status;\r
797 UDP6_INSTANCE_DATA *Instance;\r
798 EFI_TPL OldTpl;\r
799\r
800 if (This == NULL) {\r
801 return EFI_INVALID_PARAMETER;\r
802 }\r
803\r
804 Instance = UDP6_INSTANCE_DATA_FROM_THIS (This);\r
805\r
806 if (!Instance->Configured) {\r
807 return EFI_NOT_STARTED;\r
808 }\r
809\r
810 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
811\r
812 //\r
813 // Cancle the tokens specified by Token for this instance.\r
814 //\r
815 Status = Udp6InstanceCancelToken (Instance, Token);\r
816\r
817 //\r
818 // Dispatch the DPC queued by the NotifyFunction of the canceled token's events.\r
819 //\r
820 DispatchDpc ();\r
821\r
822 gBS->RestoreTPL (OldTpl);\r
823\r
824 return Status;\r
825}\r
826\r
827\r
828/**\r
829 This function can be used by network drivers and applications to increase the rate that\r
830 data packets are moved between the communications device and the transmit/receive queues.\r
831\r
832 @param[in] This Pointer to the EFI_UDP6_PROTOCOL instance.\r
833\r
834 @retval EFI_SUCCESS Incoming or outgoing data was processed.\r
835 @retval EFI_INVALID_PARAMETER This is NULL.\r
836 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.\r
837 @retval EFI_TIMEOUT Data was dropped out of the transmit and/or\r
838 receive queue.\r
839\r
840**/\r
841EFI_STATUS\r
842EFIAPI\r
843Udp6Poll (\r
844 IN EFI_UDP6_PROTOCOL *This\r
845 )\r
846{\r
847 UDP6_INSTANCE_DATA *Instance;\r
848 EFI_IP6_PROTOCOL *Ip;\r
849\r
850 if (This == NULL) {\r
851 return EFI_INVALID_PARAMETER;\r
852 }\r
853\r
854 Instance = UDP6_INSTANCE_DATA_FROM_THIS (This);\r
855 Ip = Instance->IpInfo->Ip.Ip6;\r
856\r
857 //\r
858 // Invode the Ip instance consumed by the udp instance to do the poll operation.\r
859 //\r
860 return Ip->Poll (Ip);\r
861}\r