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