]> git.proxmox.com Git - mirror_edk2.git/blame - NetworkPkg/Udp6Dxe/Udp6Main.c
NetworkPkg:Fix NULL pointer dereference issues.
[mirror_edk2.git] / NetworkPkg / Udp6Dxe / Udp6Main.c
CommitLineData
a3bcde70
HT
1/** @file\r
2 Contains all EFI_UDP6_PROTOCOL interfaces.\r
3\r
d551cc64 4 Copyright (c) 2009 - 2014, 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
354 return EFI_NOT_STARTED;\r
355 }\r
356\r
357 Ip = Instance->IpInfo->Ip.Ip6;\r
358\r
359 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
360\r
361 //\r
362 // Invoke the Ip instance the Udp6 instance consumes to do the group operation.\r
363 //\r
364 Status = Ip->Groups (Ip, JoinFlag, MulticastAddress);\r
365\r
366 if (EFI_ERROR (Status)) {\r
367 goto ON_EXIT;\r
368 }\r
369\r
370 //\r
371 // Keep a local copy of the configured multicast IPs because IpIo receives\r
372 // datagrams from the 0 station address IP instance and then UDP delivers to\r
373 // the matched instance. This copy of multicast IPs is used to avoid receive\r
374 // the mutlicast datagrams destinated to multicast IPs the other instances configured.\r
375 //\r
376 if (JoinFlag) {\r
377\r
378 Status = NetMapInsertTail (&Instance->McastIps, (VOID *) McastIp, NULL);\r
379 } else {\r
380\r
381 NetMapIterate (&Instance->McastIps, Udp6LeaveGroup, MulticastAddress);\r
382 }\r
383\r
384ON_EXIT:\r
385\r
386 gBS->RestoreTPL (OldTpl);\r
387\r
388 if (EFI_ERROR (Status)) {\r
389 if (McastIp != NULL) {\r
390 FreePool (McastIp);\r
391 }\r
392 }\r
393\r
394 return Status;\r
395}\r
396\r
397\r
398\r
399/**\r
400 This function places a sending request to this instance of the EFI UDPv6 Protocol,\r
401 alongside the transmit data that was filled by the user.\r
402\r
403 @param[in] This Pointer to the EFI_UDP6_PROTOCOL instance.\r
404 @param[in] Token Pointer to the completion token that will be\r
405 placed into the transmit queue.\r
406\r
407 @retval EFI_SUCCESS The data was queued for transmission.\r
408 @retval EFI_NOT_STARTED This EFI UDPv6 Protocol instance has not been\r
409 started.\r
410 @retval EFI_NO_MAPPING The under-layer IPv6 driver was responsible for\r
411 choosing a source address for this instance, but\r
412 no source address was available for use.\r
413 @retval EFI_INVALID_PARAMETER One or more of the following are TRUE:\r
414 This is NULL.\r
415 Token is NULL. Token.Event is NULL.\r
416 Token.Packet.TxData is NULL.\r
417 Token.Packet.TxData.FragmentCount is zero.\r
418 Token.Packet.TxData.DataLength is not equal to the\r
419 sum of fragment lengths.\r
420 One or more of the\r
421 Token.Packet.TxData.FragmentTable[].FragmentLength\r
422 fields is zero.\r
423 One or more of the\r
424 Token.Packet.TxData.FragmentTable[].FragmentBuffer\r
425 fields is NULL. One or more of the\r
426 Token.Packet.TxData.UdpSessionData.DestinationAddres\r
427 are not valid unicast IPv6\r
428 addresses if the UdpSessionData is not NULL.\r
429 Token.Packet.TxData.UdpSessionData.\r
430 DestinationAddress is NULL\r
431 Token.Packet.TxData.UdpSessionData.\r
432 DestinatioPort\r
433 is zero.\r
434 Token.Packet.TxData.UdpSessionData is NULL and this\r
435 instance's UdpConfigData.RemoteAddress is unspecified.\r
436 @retval EFI_ACCESS_DENIED The transmit completion token with the same\r
437 Token.Event is already in the transmit queue.\r
438 @retval EFI_NOT_READY The completion token could not be queued because\r
439 the transmit queue is full.\r
440 @retval EFI_OUT_OF_RESOURCES Could not queue the transmit data.\r
441 @retval EFI_NOT_FOUND There is no route to the destination network or\r
442 address.\r
443 @retval EFI_BAD_BUFFER_SIZE The data length is greater than the maximum UDP\r
444 packet size. Or, the length of the IP header + UDP\r
445 header + data length is greater than MTU if\r
446 DoNotFragment is TRUE.\r
447\r
448**/\r
449EFI_STATUS\r
450EFIAPI\r
451Udp6Transmit (\r
452 IN EFI_UDP6_PROTOCOL *This,\r
453 IN EFI_UDP6_COMPLETION_TOKEN *Token\r
454 )\r
455{\r
456 EFI_STATUS Status;\r
457 UDP6_INSTANCE_DATA *Instance;\r
458 EFI_TPL OldTpl;\r
459 NET_BUF *Packet;\r
460 EFI_UDP_HEADER *Udp6Header;\r
461 EFI_UDP6_CONFIG_DATA *ConfigData;\r
462 EFI_IPv6_ADDRESS Source;\r
463 EFI_IPv6_ADDRESS Destination;\r
464 EFI_UDP6_TRANSMIT_DATA *TxData;\r
465 EFI_UDP6_SESSION_DATA *UdpSessionData;\r
466 UDP6_SERVICE_DATA *Udp6Service;\r
467 IP_IO_OVERRIDE Override;\r
468 UINT16 HeadSum;\r
469 EFI_IP_ADDRESS IpDestAddr;\r
470\r
471 if ((This == NULL) || (Token == NULL)) {\r
472 return EFI_INVALID_PARAMETER;\r
473 }\r
474\r
475 Instance = UDP6_INSTANCE_DATA_FROM_THIS (This);\r
476\r
477 if (!Instance->Configured) {\r
478 return EFI_NOT_STARTED;\r
479 }\r
480\r
481 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
482\r
483 //\r
484 // Validate the Token, if the token is invalid return the error code.\r
485 //\r
486 Status = Udp6ValidateTxToken (Instance, Token);\r
487 if (EFI_ERROR (Status)) {\r
488 goto ON_EXIT;\r
489 }\r
490\r
491 if (EFI_ERROR (NetMapIterate (&Instance->TxTokens, Udp6TokenExist, Token)) ||\r
492 EFI_ERROR (NetMapIterate (&Instance->RxTokens, Udp6TokenExist, Token))\r
493 ){\r
494 //\r
495 // Try to find a duplicate token in the two token maps, if found, return\r
496 // EFI_ACCESS_DENIED.\r
497 //\r
498 Status = EFI_ACCESS_DENIED;\r
499 goto ON_EXIT;\r
500 }\r
501\r
502 TxData = Token->Packet.TxData;\r
503\r
504 //\r
505 // Create a net buffer to hold the user buffer and the udp header.\r
506 //\r
507 Packet = NetbufFromExt (\r
508 (NET_FRAGMENT *) TxData->FragmentTable,\r
509 TxData->FragmentCount,\r
510 UDP6_HEADER_SIZE,\r
511 0,\r
512 Udp6NetVectorExtFree,\r
513 NULL\r
514 );\r
515 if (Packet == NULL) {\r
516 Status = EFI_OUT_OF_RESOURCES;\r
517 goto ON_EXIT;\r
518 }\r
519\r
520 //\r
521 // Store the IpIo in ProtoData.\r
522 //\r
523 Udp6Service = Instance->Udp6Service;\r
524 *((UINTN *) &Packet->ProtoData[0]) = (UINTN) (Udp6Service->IpIo);\r
525\r
526 Udp6Header = (EFI_UDP_HEADER *) NetbufAllocSpace (Packet, UDP6_HEADER_SIZE, TRUE);\r
527 ASSERT (Udp6Header != NULL);\r
528 ConfigData = &Instance->ConfigData;\r
529\r
530 //\r
531 // Fill the udp header.\r
532 //\r
533 Udp6Header->SrcPort = HTONS (ConfigData->StationPort);\r
534 Udp6Header->DstPort = HTONS (ConfigData->RemotePort);\r
535 Udp6Header->Length = HTONS ((UINT16) Packet->TotalSize);\r
536 Udp6Header->Checksum = 0;\r
537 //\r
538 // Set the UDP Header in NET_BUF, this UDP header is for IP6 can fast get the\r
539 // Udp header for pseudoHeadCheckSum.\r
540 //\r
541 Packet->Udp = Udp6Header;\r
542 UdpSessionData = TxData->UdpSessionData;\r
543\r
544 if (UdpSessionData != NULL) {\r
545 //\r
546 // Set the Destination according to the specified\r
547 // UdpSessionData.\r
548 //\r
549\r
550 if (UdpSessionData->DestinationPort != 0) {\r
551 Udp6Header->DstPort = HTONS (UdpSessionData->DestinationPort);\r
552 }\r
553\r
554 IP6_COPY_ADDRESS (&Source, &ConfigData->StationAddress);\r
555 if (!NetIp6IsUnspecifiedAddr (&UdpSessionData->DestinationAddress)) {\r
556 IP6_COPY_ADDRESS (&Destination, &UdpSessionData->DestinationAddress);\r
557 } else {\r
558 IP6_COPY_ADDRESS (&Destination, &ConfigData->RemoteAddress);\r
559 }\r
560\r
561 //\r
562 //Calculate the pseudo head checksum using the overridden parameters.\r
563 //\r
564 if (!NetIp6IsUnspecifiedAddr (&ConfigData->StationAddress)) {\r
565 HeadSum = NetIp6PseudoHeadChecksum (\r
566 &Source,\r
567 &Destination,\r
568 EFI_IP_PROTO_UDP,\r
569 0\r
570 );\r
571\r
572 //\r
573 // calculate the checksum.\r
574 //\r
575 Udp6Header->Checksum = Udp6Checksum (Packet, HeadSum);\r
576 if (Udp6Header->Checksum == 0) {\r
577 //\r
578 // If the calculated checksum is 0, fill the Checksum field with all ones.\r
579 //\r
580 Udp6Header->Checksum = 0XFFFF;\r
581 }\r
582 } else {\r
583 //\r
584 // Set the checksum is zero if the ConfigData->StationAddress is unspcified\r
585 // and the Ipv6 will fill the correct value of this checksum.\r
586 //\r
587 Udp6Header->Checksum = 0;\r
588\r
589 }\r
590 } else {\r
591 //\r
592 // UdpSessionData is NULL, use the address and port information previously configured.\r
593 //\r
594 IP6_COPY_ADDRESS (&Destination, &ConfigData->RemoteAddress);\r
595\r
596 HeadSum = Instance->HeadSum;\r
597 //\r
598 // calculate the checksum.\r
599 //\r
600 Udp6Header->Checksum = Udp6Checksum (Packet, HeadSum);\r
601 if (Udp6Header->Checksum == 0) {\r
602 //\r
603 // If the calculated checksum is 0, fill the Checksum field with all ones.\r
604 //\r
605 Udp6Header->Checksum = 0xffff;\r
606 }\r
607 }\r
608\r
609\r
610\r
611 //\r
612 // Fill the IpIo Override data.\r
613 //\r
614 Override.Ip6OverrideData.Protocol = EFI_IP_PROTO_UDP;\r
615 Override.Ip6OverrideData.HopLimit = ConfigData->HopLimit;\r
616 Override.Ip6OverrideData.FlowLabel = 0;\r
617\r
618 //\r
619 // Save the token into the TxToken map.\r
620 //\r
621 Status = NetMapInsertTail (&Instance->TxTokens, Token, Packet);\r
622 if (EFI_ERROR (Status)) {\r
623 goto FREE_PACKET;\r
624 }\r
625\r
626 //\r
627 // Send out this datagram through IpIo.\r
628 //\r
629 if (UdpSessionData != NULL){\r
630 IP6_COPY_ADDRESS (&(IpDestAddr.v6), &Destination);\r
631 } else {\r
632 ZeroMem (&IpDestAddr.v6, sizeof (EFI_IPv6_ADDRESS));\r
633 }\r
634\r
635 Status = IpIoSend (\r
636 Udp6Service->IpIo,\r
637 Packet,\r
638 Instance->IpInfo,\r
639 Instance,\r
640 Token,\r
641 &IpDestAddr,\r
642 &Override\r
643 );\r
644 if (EFI_ERROR (Status)) {\r
645 //\r
646 // Remove this token from the TxTokens.\r
647 //\r
648 Udp6RemoveToken (&Instance->TxTokens, Token);\r
649 }\r
650\r
651FREE_PACKET:\r
652\r
653 NetbufFree (Packet);\r
654\r
655ON_EXIT:\r
656\r
657 gBS->RestoreTPL (OldTpl);\r
658\r
659 return Status;\r
660}\r
661\r
662\r
663/**\r
664 This function places a completion token into the receive packet queue. This function\r
665 is always asynchronous.\r
666\r
667 @param[in] This Pointer to the EFI_UDP6_PROTOCOL instance.\r
668 @param[in] Token Pointer to a token that is associated with the\r
669 receive data descriptor.\r
670\r
671 @retval EFI_SUCCESS The receive completion token was cached.\r
672 @retval EFI_NOT_STARTED This EFI UDPv6 Protocol instance has not been\r
673 started.\r
674 @retval EFI_NO_MAPPING When using a default address, configuration (DHCP,\r
675 BOOTP, RARP, etc.) is not finished yet.\r
676 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:\r
677 This is NULL. Token is NULL. Token.Event is NULL.\r
678 @retval EFI_OUT_OF_RESOURCES The receive completion token could not be queued\r
679 due to a lack of system resources (usually\r
680 memory).\r
681 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.\r
682 The EFI UDPv6 Protocol instance has been reset to\r
683 startup defaults.\r
684 @retval EFI_ACCESS_DENIED A receive completion token with the same\r
685 Token.Event is already in the receive queue.\r
686 @retval EFI_NOT_READY The receive request could not be queued because\r
687 the receive queue is full.\r
688\r
689**/\r
690EFI_STATUS\r
691EFIAPI\r
692Udp6Receive (\r
693 IN EFI_UDP6_PROTOCOL *This,\r
694 IN EFI_UDP6_COMPLETION_TOKEN *Token\r
695 )\r
696{\r
697 EFI_STATUS Status;\r
698 UDP6_INSTANCE_DATA *Instance;\r
699 EFI_TPL OldTpl;\r
700\r
701 if ((This == NULL) || (Token == NULL) || (Token->Event == NULL)) {\r
702 return EFI_INVALID_PARAMETER;\r
703 }\r
704\r
705 Instance = UDP6_INSTANCE_DATA_FROM_THIS (This);\r
706\r
707 if (!Instance->Configured) {\r
708 return EFI_NOT_STARTED;\r
709 }\r
710\r
711 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
712\r
713 if (EFI_ERROR (NetMapIterate (&Instance->RxTokens, Udp6TokenExist, Token)) ||\r
714 EFI_ERROR (NetMapIterate (&Instance->TxTokens, Udp6TokenExist, Token))\r
715 ){\r
716 //\r
717 // Return EFI_ACCESS_DENIED if the specified token is already in the TxTokens or\r
718 // RxTokens map.\r
719 //\r
720 Status = EFI_ACCESS_DENIED;\r
721 goto ON_EXIT;\r
722 }\r
723\r
724 Token->Packet.RxData = NULL;\r
725\r
726 //\r
727 // Save the token into the RxTokens map.\r
728 //\r
729 Status = NetMapInsertTail (&Instance->RxTokens, Token, NULL);\r
730 if (EFI_ERROR (Status)) {\r
731 Status = EFI_NOT_READY;\r
732 goto ON_EXIT;\r
733 }\r
734\r
735 //\r
736 // If there is an icmp error, report it.\r
737 //\r
738 Udp6ReportIcmpError (Instance);\r
739\r
740 //\r
741 // Try to delivered the received datagrams.\r
742 //\r
743 Udp6InstanceDeliverDgram (Instance);\r
744\r
745 //\r
746 // Dispatch the DPC queued by the NotifyFunction of Token->Event.\r
747 //\r
748 DispatchDpc ();\r
749\r
750ON_EXIT:\r
751\r
752 gBS->RestoreTPL (OldTpl);\r
753\r
754 return Status;\r
755}\r
756\r
757\r
758/**\r
759 This function is used to abort a pending transmit or receive request.\r
760\r
761 @param[in] This Pointer to the EFI_UDP6_PROTOCOL instance.\r
762 @param[in] Token Pointer to a token that has been issued by\r
763 EFI_UDP6_PROTOCOL.Transmit() or\r
764 EFI_UDP6_PROTOCOL.Receive(). This parameter is\r
765 optional and may be NULL.\r
766\r
767 @retval EFI_SUCCESS The asynchronous I/O request was aborted, and\r
768 Token.Event was signaled. When Token is NULL, all\r
769 pending requests are aborted and their events are\r
770 signaled.\r
771 @retval EFI_INVALID_PARAMETER This is NULL.\r
772 @retval EFI_NOT_STARTED This instance has not been started.\r
773 @retval EFI_NO_MAPPING When using the default address, configuration\r
774 (DHCP, BOOTP, RARP, etc.) is not finished yet.\r
775 @retval EFI_NOT_FOUND When Token is not NULL, the asynchronous I/O\r
776 request is not found in the transmit or receive\r
777 queue. It is either completed or not issued by\r
778 Transmit() or Receive().\r
779\r
780**/\r
781EFI_STATUS\r
782EFIAPI\r
783Udp6Cancel (\r
784 IN EFI_UDP6_PROTOCOL *This,\r
785 IN EFI_UDP6_COMPLETION_TOKEN *Token OPTIONAL\r
786 )\r
787{\r
788 EFI_STATUS Status;\r
789 UDP6_INSTANCE_DATA *Instance;\r
790 EFI_TPL OldTpl;\r
791\r
792 if (This == NULL) {\r
793 return EFI_INVALID_PARAMETER;\r
794 }\r
795\r
796 Instance = UDP6_INSTANCE_DATA_FROM_THIS (This);\r
797\r
798 if (!Instance->Configured) {\r
799 return EFI_NOT_STARTED;\r
800 }\r
801\r
802 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
803\r
804 //\r
805 // Cancle the tokens specified by Token for this instance.\r
806 //\r
807 Status = Udp6InstanceCancelToken (Instance, Token);\r
808\r
809 //\r
810 // Dispatch the DPC queued by the NotifyFunction of the canceled token's events.\r
811 //\r
812 DispatchDpc ();\r
813\r
814 gBS->RestoreTPL (OldTpl);\r
815\r
816 return Status;\r
817}\r
818\r
819\r
820/**\r
821 This function can be used by network drivers and applications to increase the rate that\r
822 data packets are moved between the communications device and the transmit/receive queues.\r
823\r
824 @param[in] This Pointer to the EFI_UDP6_PROTOCOL instance.\r
825\r
826 @retval EFI_SUCCESS Incoming or outgoing data was processed.\r
827 @retval EFI_INVALID_PARAMETER This is NULL.\r
828 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.\r
829 @retval EFI_TIMEOUT Data was dropped out of the transmit and/or\r
830 receive queue.\r
831\r
832**/\r
833EFI_STATUS\r
834EFIAPI\r
835Udp6Poll (\r
836 IN EFI_UDP6_PROTOCOL *This\r
837 )\r
838{\r
839 UDP6_INSTANCE_DATA *Instance;\r
840 EFI_IP6_PROTOCOL *Ip;\r
841\r
842 if (This == NULL) {\r
843 return EFI_INVALID_PARAMETER;\r
844 }\r
845\r
846 Instance = UDP6_INSTANCE_DATA_FROM_THIS (This);\r
847 Ip = Instance->IpInfo->Ip.Ip6;\r
848\r
849 //\r
850 // Invode the Ip instance consumed by the udp instance to do the poll operation.\r
851 //\r
852 return Ip->Poll (Ip);\r
853}\r