]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Network/Udp4Dxe/Udp4Main.c
Fixed one bug when calculating timeout value in timeout function for UDP protocol.
[mirror_edk2.git] / MdeModulePkg / Universal / Network / Udp4Dxe / Udp4Main.c
CommitLineData
8a67d61d 1/** @file\r
2\r
3Copyright (c) 2006 - 2007, Intel Corporation\r
4All rights reserved. This program and the accompanying materials\r
5are licensed and made available under the terms and conditions of the BSD License\r
6which accompanies this distribution. The full text of the license may be found at\r
7http://opensource.org/licenses/bsd-license.php\r
8\r
9THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
10WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
11\r
12Module Name:\r
13\r
14 Udp4Main.c\r
15\r
16Abstract:\r
17\r
18\r
19**/\r
20\r
21#include "Udp4Impl.h"\r
22\r
8a67d61d 23EFI_UDP4_PROTOCOL mUdp4Protocol = {\r
24 Udp4GetModeData,\r
25 Udp4Configure,\r
26 Udp4Groups,\r
27 Udp4Routes,\r
28 Udp4Transmit,\r
29 Udp4Receive,\r
30 Udp4Cancel,\r
31 Udp4Poll\r
32};\r
33\r
34\r
35/**\r
bab52709 36 Reads the current operational settings.\r
37\r
38 The GetModeData() function copies the current operational settings of this EFI\r
39 UDPv4 Protocol instance into user-supplied buffers. This function is used\r
40 optionally to retrieve the operational mode data of underlying networks or\r
41 drivers.\r
42\r
43 @param This Pointer to the EFI_UDP4_PROTOCOL instance.\r
44 @param Udp4ConfigData Pointer to the buffer to receive the current configuration data.\r
45 @param Ip4ModeData Pointer to the EFI IPv4 Protocol mode data structure.\r
46 @param MnpConfigData Pointer to the managed network configuration data structure.\r
47 @param SnpModeData Pointer to the simple network mode data structure.\r
48\r
49 @retval EFI_SUCCESS The mode data was read.\r
50 @retval EFI_NOT_STARTED When Udp4ConfigData is queried, no configuration data is\r
51 available because this instance has not been started.\r
52 @retval EFI_INVALID_PARAMETER This is NULL.\r
8a67d61d 53\r
54**/\r
55EFI_STATUS\r
56EFIAPI\r
57Udp4GetModeData (\r
58 IN EFI_UDP4_PROTOCOL *This,\r
59 OUT EFI_UDP4_CONFIG_DATA *Udp4ConfigData OPTIONAL,\r
60 OUT EFI_IP4_MODE_DATA *Ip4ModeData OPTIONAL,\r
61 OUT EFI_MANAGED_NETWORK_CONFIG_DATA *MnpConfigData OPTIONAL,\r
62 OUT EFI_SIMPLE_NETWORK_MODE *SnpModeData OPTIONAL\r
63 )\r
64{\r
65 UDP4_INSTANCE_DATA *Instance;\r
66 EFI_IP4_PROTOCOL *Ip;\r
67 EFI_TPL OldTpl;\r
68 EFI_STATUS Status;\r
69\r
70 if (This == NULL) {\r
71 return EFI_INVALID_PARAMETER;\r
72 }\r
73\r
74 Instance = UDP4_INSTANCE_DATA_FROM_THIS (This);\r
75\r
76 if (!Instance->Configured && (Udp4ConfigData != NULL)) {\r
77 return EFI_NOT_STARTED;\r
78 }\r
79\r
e48e37fc 80 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
8a67d61d 81\r
82 if (Udp4ConfigData != NULL) {\r
83 //\r
84 // Set the Udp4ConfigData.\r
85 //\r
687a2e5f 86 CopyMem (Udp4ConfigData, &Instance->ConfigData, sizeof (*Udp4ConfigData));\r
8a67d61d 87 }\r
88\r
89 Ip = Instance->IpInfo->Ip;\r
90\r
91 //\r
92 // Get the underlying Ip4ModeData, MnpConfigData and SnpModeData.\r
93 //\r
94 Status = Ip->GetModeData (Ip, Ip4ModeData, MnpConfigData, SnpModeData);\r
95\r
e48e37fc 96 gBS->RestoreTPL (OldTpl);\r
8a67d61d 97\r
98 return Status;\r
99}\r
100\r
101\r
102/**\r
bab52709 103 Initializes, changes, or resets the operational parameters for this instance of the EFI UDPv4\r
104 Protocol.\r
105 \r
106 The Configure() function is used to do the following:\r
107 * Initialize and start this instance of the EFI UDPv4 Protocol.\r
108 * Change the filtering rules and operational parameters.\r
109 * Reset this instance of the EFI UDPv4 Protocol.\r
110 Until these parameters are initialized, no network traffic can be sent or\r
111 received by this instance. This instance can be also reset by calling Configure()\r
112 with UdpConfigData set to NULL. Once reset, the receiving queue and transmitting\r
113 queue are flushed and no traffic is allowed through this instance.\r
114 With different parameters in UdpConfigData, Configure() can be used to bind\r
115 this instance to specified port.\r
116\r
117 @param This Pointer to the EFI_UDP4_PROTOCOL instance.\r
118 @param UdpConfigData Pointer to the buffer to receive the current configuration data.\r
119\r
120 @retval EFI_SUCCESS The configuration settings were set, changed, or reset successfully.\r
121 @retval EFI_NO_MAPPING When using a default address, configuration (DHCP, BOOTP,\r
122 RARP, etc.) is not finished yet.\r
123 @retval EFI_INVALID_PARAMETER One or more following conditions are TRUE:\r
124 @retval EFI_ALREADY_STARTED The EFI UDPv4 Protocol instance is already started/configured\r
125 and must be stopped/reset before it can be reconfigured.\r
126 @retval EFI_ACCESS_DENIED UdpConfigData. AllowDuplicatePort is FALSE\r
127 and UdpConfigData.StationPort is already used by\r
128 other instance.\r
129 @retval EFI_OUT_OF_RESOURCES The EFI UDPv4 Protocol driver cannot allocate memory for this\r
130 EFI UDPv4 Protocol instance.\r
131 @retval EFI_DEVICE_ERROR An unexpected network or system error occurred and this instance\r
132 was not opened. \r
8a67d61d 133\r
134**/\r
135EFI_STATUS\r
136EFIAPI\r
137Udp4Configure (\r
138 IN EFI_UDP4_PROTOCOL *This,\r
139 IN EFI_UDP4_CONFIG_DATA *UdpConfigData OPTIONAL\r
140 )\r
141{\r
142 EFI_STATUS Status;\r
143 UDP4_INSTANCE_DATA *Instance;\r
144 UDP4_SERVICE_DATA *Udp4Service;\r
145 EFI_TPL OldTpl;\r
146 IP4_ADDR StationAddress;\r
147 IP4_ADDR SubnetMask;\r
148 IP4_ADDR RemoteAddress;\r
149 EFI_IP4_CONFIG_DATA Ip4ConfigData;\r
772db4bb 150 IP4_ADDR LocalAddr;\r
151 IP4_ADDR RemoteAddr;\r
8a67d61d 152\r
153 if (This == NULL) {\r
154 return EFI_INVALID_PARAMETER;\r
155 }\r
156\r
157 Instance = UDP4_INSTANCE_DATA_FROM_THIS (This);\r
158\r
159 if (!Instance->Configured && (UdpConfigData == NULL)) {\r
160 return EFI_SUCCESS;\r
161 }\r
162\r
163 Udp4Service = Instance->Udp4Service;\r
164 Status = EFI_SUCCESS;\r
165\r
e48e37fc 166 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
8a67d61d 167\r
168 if (UdpConfigData != NULL) {\r
169\r
e48e37fc 170 CopyMem (&StationAddress, &UdpConfigData->StationAddress, sizeof (IP4_ADDR));\r
171 CopyMem (&SubnetMask, &UdpConfigData->SubnetMask, sizeof (IP4_ADDR));\r
172 CopyMem (&RemoteAddress, &UdpConfigData->RemoteAddress, sizeof (IP4_ADDR));\r
772db4bb 173\r
174 StationAddress = NTOHL (StationAddress);\r
175 SubnetMask = NTOHL (SubnetMask);\r
176 RemoteAddress = NTOHL (RemoteAddress);\r
36ee91ca 177\r
8a67d61d 178\r
179 if (!UdpConfigData->UseDefaultAddress &&\r
180 (!IP4_IS_VALID_NETMASK (SubnetMask) ||\r
181 !((StationAddress == 0) || Ip4IsUnicast (StationAddress, SubnetMask)) ||\r
182 !((RemoteAddress == 0) || Ip4IsUnicast (RemoteAddress, 0)))) {\r
183 //\r
184 // Don't use default address, and subnet mask is invalid or StationAddress is not\r
185 // a valid unicast IPv4 address or RemoteAddress is not a valid unicast IPv4 address\r
186 // if it is not 0.\r
187 //\r
188 Status = EFI_INVALID_PARAMETER;\r
189 goto ON_EXIT;\r
190 }\r
191\r
192 if (Instance->Configured) {\r
193 //\r
194 // The instance is already configured, try to do the re-configuration.\r
195 //\r
196 if (!Udp4IsReconfigurable (&Instance->ConfigData, UdpConfigData)) {\r
197 //\r
198 // If the new configuration data wants to change some unreconfigurable\r
199 // settings, return EFI_ALREADY_STARTED.\r
200 //\r
201 Status = EFI_ALREADY_STARTED;\r
202 goto ON_EXIT;\r
203 }\r
204\r
205 //\r
206 // Save the reconfigurable parameters.\r
207 //\r
208 Instance->ConfigData.TypeOfService = UdpConfigData->TypeOfService;\r
209 Instance->ConfigData.TimeToLive = UdpConfigData->TimeToLive;\r
210 Instance->ConfigData.DoNotFragment = UdpConfigData->DoNotFragment;\r
211 Instance->ConfigData.ReceiveTimeout = UdpConfigData->ReceiveTimeout;\r
212 Instance->ConfigData.TransmitTimeout = UdpConfigData->TransmitTimeout;\r
213 } else {\r
214 //\r
215 // Construct the Ip configuration data from the UdpConfigData.\r
216 //\r
217 Udp4BuildIp4ConfigData (UdpConfigData, &Ip4ConfigData);\r
218\r
219 //\r
220 // Configure the Ip instance wrapped in the IpInfo.\r
221 //\r
222 Status = IpIoConfigIp (Instance->IpInfo, &Ip4ConfigData);\r
223 if (EFI_ERROR (Status)) {\r
224 if (Status == EFI_NO_MAPPING) {\r
225 Instance->IsNoMapping = TRUE;\r
226 }\r
227\r
228 goto ON_EXIT;\r
229 }\r
230\r
231 Instance->IsNoMapping = FALSE;\r
232\r
233 //\r
234 // Save the configuration data.\r
235 //\r
687a2e5f 236 CopyMem (&Instance->ConfigData, UdpConfigData, sizeof (Instance->ConfigData));\r
8a67d61d 237 Instance->ConfigData.StationAddress = Ip4ConfigData.StationAddress;\r
238 Instance->ConfigData.SubnetMask = Ip4ConfigData.SubnetMask;\r
239\r
240 //\r
241 // Try to allocate the required port resource.\r
242 //\r
243 Status = Udp4Bind (&Udp4Service->ChildrenList, &Instance->ConfigData);\r
244 if (EFI_ERROR (Status)) {\r
245 //\r
246 // Reset the ip instance if bind fails.\r
247 //\r
248 IpIoConfigIp (Instance->IpInfo, NULL);\r
249 goto ON_EXIT;\r
250 }\r
251\r
252 //\r
253 // Pre calculate the checksum for the pseudo head, ignore the UDP length first.\r
254 //\r
e48e37fc 255 CopyMem (&LocalAddr, &Instance->ConfigData.StationAddress, sizeof (IP4_ADDR));\r
256 CopyMem (&RemoteAddr, &Instance->ConfigData.RemoteAddress, sizeof (IP4_ADDR));\r
8a67d61d 257 Instance->HeadSum = NetPseudoHeadChecksum (\r
772db4bb 258 LocalAddr,\r
259 RemoteAddr,\r
8a67d61d 260 EFI_IP_PROTO_UDP,\r
261 0\r
262 );\r
263\r
264 Instance->Configured = TRUE;\r
265 }\r
266 } else {\r
267 //\r
268 // UdpConfigData is NULL, reset the instance.\r
269 //\r
270 Instance->Configured = FALSE;\r
271 Instance->IsNoMapping = FALSE;\r
272\r
273 //\r
274 // Reset the Ip instance wrapped in the IpInfo.\r
275 //\r
276 IpIoConfigIp (Instance->IpInfo, NULL);\r
277\r
278 //\r
279 // Cancel all the user tokens.\r
280 //\r
36ee91ca 281 Instance->Udp4Proto.Cancel (&Instance->Udp4Proto, NULL);\r
8a67d61d 282\r
283 //\r
284 // Remove the buffered RxData for this instance.\r
285 //\r
36ee91ca 286 Udp4FlushRcvdDgram (Instance);\r
287\r
e48e37fc 288 ASSERT (IsListEmpty (&Instance->DeliveredDgramQue));\r
8a67d61d 289 }\r
290\r
291 Udp4SetVariableData (Instance->Udp4Service);\r
292\r
293ON_EXIT:\r
294\r
e48e37fc 295 gBS->RestoreTPL (OldTpl);\r
8a67d61d 296\r
297 return Status;\r
298}\r
299\r
300\r
301/**\r
bab52709 302 Joins and leaves multicast groups.\r
303 \r
304 The Groups() function is used to enable and disable the multicast group\r
305 filtering. If the JoinFlag is FALSE and the MulticastAddress is NULL, then all\r
306 currently joined groups are left.\r
307\r
308 @param This Pointer to the EFI_UDP4_PROTOCOL instance.\r
309 @param JoinFlag Set to TRUE to join a multicast group. Set to FALSE to leave one\r
310 or all multicast groups.\r
311 @param MulticastAddress Pointer to multicast group address to join or leave.\r
312\r
313 @retval EFI_SUCCESS The operation completed successfully.\r
314 @retval EFI_NOT_STARTED The EFI UDPv4 Protocol instance has not been started.\r
315 @retval EFI_NO_MAPPING When using a default address, configuration (DHCP, BOOTP,\r
316 RARP, etc.) is not finished yet.\r
317 @retval EFI_OUT_OF_RESOURCES Could not allocate resources to join the group.\r
318 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:\r
319 - This is NULL.\r
320 - JoinFlag is TRUE and MulticastAddress is NULL.\r
321 - JoinFlag is TRUE and *MulticastAddress is not\r
322 a valid multicast address.\r
323 @retval EFI_ALREADY_STARTED The group address is already in the group table (when\r
324 JoinFlag is TRUE).\r
325 @retval EFI_NOT_FOUND The group address is not in the group table (when JoinFlag is\r
326 FALSE).\r
327 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.\r
8a67d61d 328\r
329**/\r
330EFI_STATUS\r
331EFIAPI\r
332Udp4Groups (\r
333 IN EFI_UDP4_PROTOCOL *This,\r
334 IN BOOLEAN JoinFlag,\r
335 IN EFI_IPv4_ADDRESS *MulticastAddress OPTIONAL\r
336 )\r
337{\r
338 EFI_STATUS Status;\r
339 UDP4_INSTANCE_DATA *Instance;\r
340 EFI_IP4_PROTOCOL *Ip;\r
341 EFI_TPL OldTpl;\r
772db4bb 342 IP4_ADDR McastIp;\r
8a67d61d 343\r
772db4bb 344 if ((This == NULL) || (JoinFlag && (MulticastAddress == NULL))) {\r
8a67d61d 345 return EFI_INVALID_PARAMETER;\r
346 }\r
347\r
772db4bb 348 McastIp = 0;\r
349 if (JoinFlag) {\r
e48e37fc 350 CopyMem (&McastIp, MulticastAddress, sizeof (IP4_ADDR));\r
772db4bb 351\r
687a2e5f 352 if (!IP4_IS_MULTICAST (NTOHL (McastIp))) {\r
772db4bb 353 return EFI_INVALID_PARAMETER;\r
354 }\r
355 }\r
356\r
8a67d61d 357 Instance = UDP4_INSTANCE_DATA_FROM_THIS (This);\r
358\r
359 if (Instance->IsNoMapping) {\r
360 return EFI_NO_MAPPING;\r
361 }\r
362\r
363 if (!Instance->Configured) {\r
364 return EFI_NOT_STARTED;\r
365 }\r
366\r
367 Ip = Instance->IpInfo->Ip;\r
368\r
e48e37fc 369 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
8a67d61d 370\r
371 //\r
372 // Invoke the Ip instance the Udp4 instance consumes to do the group operation.\r
373 //\r
374 Status = Ip->Groups (Ip, JoinFlag, MulticastAddress);\r
375\r
376 if (EFI_ERROR (Status)) {\r
377 goto ON_EXIT;\r
378 }\r
379\r
380 //\r
381 // Keep a local copy of the configured multicast IPs because IpIo receives\r
382 // datagrams from the 0 station address IP instance and then UDP delivers to\r
383 // the matched instance. This copy of multicast IPs is used to avoid receive\r
bab52709 384 // the mutlicast datagrams destined to multicast IPs the other instances configured.\r
8a67d61d 385 //\r
386 if (JoinFlag) {\r
387\r
772db4bb 388 NetMapInsertTail (&Instance->McastIps, (VOID *) (UINTN) McastIp, NULL);\r
8a67d61d 389 } else {\r
390\r
391 NetMapIterate (&Instance->McastIps, Udp4LeaveGroup, MulticastAddress);\r
392 }\r
393\r
394ON_EXIT:\r
395\r
e48e37fc 396 gBS->RestoreTPL (OldTpl);\r
8a67d61d 397\r
398 return Status;\r
399}\r
400\r
401\r
402/**\r
bab52709 403 Adds and deletes routing table entries.\r
404 \r
405 The Routes() function adds a route to or deletes a route from the routing table.\r
406 Routes are determined by comparing the SubnetAddress with the destination IP\r
407 address and arithmetically AND-ing it with the SubnetMask. The gateway address\r
408 must be on the same subnet as the configured station address.\r
409 The default route is added with SubnetAddress and SubnetMask both set to 0.0.0.0.\r
410 The default route matches all destination IP addresses that do not match any\r
411 other routes.\r
412 A zero GatewayAddress is a nonroute. Packets are sent to the destination IP\r
413 address if it can be found in the Address Resolution Protocol (ARP) cache or\r
414 on the local subnet. One automatic nonroute entry will be inserted into the\r
415 routing table for outgoing packets that are addressed to a local subnet\r
416 (gateway address of 0.0.0.0).\r
417 Each instance of the EFI UDPv4 Protocol has its own independent routing table.\r
418 Instances of the EFI UDPv4 Protocol that use the default IP address will also\r
419 have copies of the routing table provided by the EFI_IP4_CONFIG_PROTOCOL. These\r
420 copies will be updated automatically whenever the IP driver reconfigures its\r
421 instances; as a result, the previous modification to these copies will be lost.\r
422\r
423 @param This Pointer to the EFI_UDP4_PROTOCOL instance.\r
424 @param DeleteRoute Set to TRUE to delete this route from the routing table.\r
425 Set to FALSE to add this route to the routing table.\r
426 @param SubnetAddress The destination network address that needs to be routed.\r
427 @param SubnetMask The subnet mask of SubnetAddress.\r
428 @param GatewayAddress The gateway IP address for this route.\r
429\r
430 @retval EFI_SUCCESS The operation completed successfully.\r
431 @retval EFI_NOT_STARTED The EFI UDPv4 Protocol instance has not been started.\r
432 @retval EFI_NO_MAPPING When using a default address, configuration (DHCP, BOOTP,\r
433 - RARP, etc.) is not finished yet.\r
434 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
435 @retval EFI_OUT_OF_RESOURCES Could not add the entry to the routing table.\r
436 @retval EFI_NOT_FOUND This route is not in the routing table.\r
437 @retval EFI_ACCESS_DENIED The route is already defined in the routing table.\r
8a67d61d 438\r
439**/\r
440EFI_STATUS\r
441EFIAPI\r
442Udp4Routes (\r
443 IN EFI_UDP4_PROTOCOL *This,\r
444 IN BOOLEAN DeleteRoute,\r
445 IN EFI_IPv4_ADDRESS *SubnetAddress,\r
446 IN EFI_IPv4_ADDRESS *SubnetMask,\r
447 IN EFI_IPv4_ADDRESS *GatewayAddress\r
448 )\r
449{\r
450 UDP4_INSTANCE_DATA *Instance;\r
451 EFI_IP4_PROTOCOL *Ip;\r
452\r
453 if (This == NULL) {\r
454 return EFI_INVALID_PARAMETER;\r
455 }\r
456\r
457 Instance = UDP4_INSTANCE_DATA_FROM_THIS (This);\r
458\r
459 if (Instance->IsNoMapping) {\r
460 return EFI_NO_MAPPING;\r
461 }\r
462\r
463 if (!Instance->Configured) {\r
464 return EFI_NOT_STARTED;\r
465 }\r
466\r
467 Ip = Instance->IpInfo->Ip;\r
468\r
469 //\r
470 // Invoke the Ip instance the Udp4 instance consumes to do the actual operation.\r
471 //\r
472 return Ip->Routes (Ip, DeleteRoute, SubnetAddress, SubnetMask, GatewayAddress);\r
473}\r
474\r
475\r
476/**\r
bab52709 477 Queues outgoing data packets into the transmit queue.\r
478 \r
479 The Transmit() function places a sending request to this instance of the EFI\r
480 UDPv4 Protocol, alongside the transmit data that was filled by the user. Whenever\r
481 the packet in the token is sent out or some errors occur, the Token.Event will\r
482 be signaled and Token.Status is updated. Providing a proper notification function\r
483 and context for the event will enable the user to receive the notification and\r
484 transmitting status.\r
485\r
486 @param This Pointer to the EFI_UDP4_PROTOCOL instance.\r
487 @param Token Pointer to the completion token that will be placed into the\r
488 transmit queue.\r
489\r
490 @retval EFI_SUCCESS The data has been queued for transmission.\r
491 @retval EFI_NOT_STARTED This EFI UDPv4 Protocol instance has not been started.\r
492 @retval EFI_NO_MAPPING When using a default address, configuration (DHCP, BOOTP,\r
493 RARP, etc.) is not finished yet.\r
494 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
495 @retval EFI_ACCESS_DENIED The transmit completion token with the same\r
496 Token.Event was already in the transmit queue.\r
497 @retval EFI_NOT_READY The completion token could not be queued because the\r
498 transmit queue is full.\r
499 @retval EFI_OUT_OF_RESOURCES Could not queue the transmit data.\r
500 @retval EFI_NOT_FOUND There is no route to the destination network or address.\r
501 @retval EFI_BAD_BUFFER_SIZE The data length is greater than the maximum UDP packet\r
502 size. Or the length of the IP header + UDP header + data\r
503 length is greater than MTU if DoNotFragment is TRUE.\r
8a67d61d 504\r
505**/\r
506EFI_STATUS\r
507EFIAPI\r
508Udp4Transmit (\r
509 IN EFI_UDP4_PROTOCOL *This,\r
510 IN EFI_UDP4_COMPLETION_TOKEN *Token\r
511 )\r
512{\r
513 EFI_STATUS Status;\r
514 UDP4_INSTANCE_DATA *Instance;\r
515 EFI_TPL OldTpl;\r
516 NET_BUF *Packet;\r
517 EFI_UDP4_HEADER *Udp4Header;\r
518 EFI_UDP4_CONFIG_DATA *ConfigData;\r
772db4bb 519 IP4_ADDR Source;\r
8a67d61d 520 IP4_ADDR Destination;\r
521 EFI_UDP4_TRANSMIT_DATA *TxData;\r
522 EFI_UDP4_SESSION_DATA *UdpSessionData;\r
523 UDP4_SERVICE_DATA *Udp4Service;\r
524 IP_IO_OVERRIDE Override;\r
525 UINT16 HeadSum;\r
526\r
527 if ((This == NULL) || (Token == NULL)) {\r
528 return EFI_INVALID_PARAMETER;\r
529 }\r
530\r
531 Instance = UDP4_INSTANCE_DATA_FROM_THIS (This);\r
532\r
533 if (Instance->IsNoMapping) {\r
534 return EFI_NO_MAPPING;\r
535 }\r
536\r
537 if (!Instance->Configured) {\r
538 return EFI_NOT_STARTED;\r
539 }\r
540\r
e48e37fc 541 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
8a67d61d 542\r
543 //\r
544 // Validate the Token, if the token is invalid return the error code.\r
545 //\r
546 Status = Udp4ValidateTxToken (Instance, Token);\r
547 if (EFI_ERROR (Status)) {\r
548 goto ON_EXIT;\r
549 }\r
550\r
551 if (EFI_ERROR (NetMapIterate (&Instance->TxTokens, Udp4TokenExist, Token)) ||\r
552 EFI_ERROR (NetMapIterate (&Instance->RxTokens, Udp4TokenExist, Token))) {\r
553 //\r
554 // Try to find a duplicate token in the two token maps, if found, return\r
555 // EFI_ACCESS_DENIED.\r
556 //\r
557 Status = EFI_ACCESS_DENIED;\r
558 goto ON_EXIT;\r
559 }\r
560\r
561 TxData = Token->Packet.TxData;\r
562\r
563 //\r
564 // Create a net buffer to hold the user buffer and the udp header.\r
565 //\r
566 Packet = NetbufFromExt (\r
567 (NET_FRAGMENT *)TxData->FragmentTable,\r
568 TxData->FragmentCount,\r
569 UDP4_HEADER_SIZE,\r
570 0,\r
571 Udp4NetVectorExtFree,\r
572 NULL\r
573 );\r
574 if (Packet == NULL) {\r
575 Status = EFI_OUT_OF_RESOURCES;\r
576 goto ON_EXIT;\r
577 }\r
578\r
579 //\r
580 // Store the IpIo in ProtoData.\r
581 //\r
582 Udp4Service = Instance->Udp4Service;\r
583 *((UINTN *) &Packet->ProtoData[0]) = (UINTN) (Udp4Service->IpIo);\r
584\r
585 Udp4Header = (EFI_UDP4_HEADER *) NetbufAllocSpace (Packet, UDP4_HEADER_SIZE, TRUE);\r
586 ConfigData = &Instance->ConfigData;\r
587\r
588 //\r
589 // Fill the udp header.\r
590 //\r
591 Udp4Header->SrcPort = HTONS (ConfigData->StationPort);\r
592 Udp4Header->DstPort = HTONS (ConfigData->RemotePort);\r
593 Udp4Header->Length = HTONS (Packet->TotalSize);\r
594 Udp4Header->Checksum = 0;\r
595\r
596 UdpSessionData = TxData->UdpSessionData;\r
597 Override.SourceAddress = ConfigData->StationAddress;\r
598\r
599 if (UdpSessionData != NULL) {\r
600 //\r
601 // Set the SourceAddress, SrcPort and Destination according to the specified\r
602 // UdpSessionData.\r
603 //\r
84b5c78e 604 if (!EFI_IP4_EQUAL (&UdpSessionData->SourceAddress, &mZeroIp4Addr)) {\r
e48e37fc 605 CopyMem (&Override.SourceAddress, &UdpSessionData->SourceAddress, sizeof (EFI_IPv4_ADDRESS));\r
8a67d61d 606 }\r
607\r
608 if (UdpSessionData->SourcePort != 0) {\r
609 Udp4Header->SrcPort = HTONS (UdpSessionData->SourcePort);\r
610 }\r
611\r
8a67d61d 612 if (UdpSessionData->DestinationPort != 0) {\r
613 Udp4Header->DstPort = HTONS (UdpSessionData->DestinationPort);\r
614 }\r
615\r
e48e37fc 616 CopyMem (&Source, &Override.SourceAddress, sizeof (IP4_ADDR));\r
617 CopyMem (&Destination, &UdpSessionData->DestinationAddress, sizeof (IP4_ADDR));\r
772db4bb 618\r
8a67d61d 619 //\r
620 // calculate the pseudo head checksum using the overridden parameters.\r
621 //\r
622 HeadSum = NetPseudoHeadChecksum (\r
772db4bb 623 Source,\r
8a67d61d 624 Destination,\r
625 EFI_IP_PROTO_UDP,\r
626 0\r
627 );\r
628 } else {\r
629 //\r
630 // UdpSessionData is NULL, use the address and port information previously configured.\r
631 //\r
e48e37fc 632 CopyMem (&Destination, &ConfigData->RemoteAddress, sizeof (IP4_ADDR));\r
772db4bb 633\r
634 HeadSum = Instance->HeadSum;\r
8a67d61d 635 }\r
636\r
637 //\r
638 // calculate the checksum.\r
639 //\r
640 Udp4Header->Checksum = Udp4Checksum (Packet, HeadSum);\r
641 if (Udp4Header->Checksum == 0) {\r
642 //\r
643 // If the calculated checksum is 0, fill the Checksum field with all ones.\r
644 //\r
645 Udp4Header->Checksum = 0xffff;\r
646 }\r
647\r
648 //\r
649 // Fill the IpIo Override data.\r
650 //\r
772db4bb 651 if (TxData->GatewayAddress != NULL) {\r
e48e37fc 652 CopyMem (&Override.GatewayAddress, TxData->GatewayAddress, sizeof (EFI_IPv4_ADDRESS));\r
772db4bb 653 } else {\r
e48e37fc 654 ZeroMem (&Override.GatewayAddress, sizeof (EFI_IPv4_ADDRESS));\r
772db4bb 655 }\r
656\r
8a67d61d 657 Override.Protocol = EFI_IP_PROTO_UDP;\r
658 Override.TypeOfService = ConfigData->TypeOfService;\r
659 Override.TimeToLive = ConfigData->TimeToLive;\r
660 Override.DoNotFragment = ConfigData->DoNotFragment;\r
661\r
662 //\r
663 // Save the token into the TxToken map.\r
664 //\r
665 Status = NetMapInsertTail (&Instance->TxTokens, Token, Packet);\r
666 if (EFI_ERROR (Status)) {\r
667 goto FREE_PACKET;\r
668 }\r
669\r
670 //\r
671 // Send out this datagram through IpIo.\r
672 //\r
673 Status = IpIoSend (\r
674 Udp4Service->IpIo,\r
675 Packet,\r
676 Instance->IpInfo,\r
677 Instance,\r
678 Token,\r
679 Destination,\r
680 &Override\r
681 );\r
682 if (EFI_ERROR (Status)) {\r
683 //\r
684 // Remove this token from the TxTokens.\r
685 //\r
686 Udp4RemoveToken (&Instance->TxTokens, Token);\r
687 }\r
688\r
689FREE_PACKET:\r
690\r
691 NetbufFree (Packet);\r
692\r
693ON_EXIT:\r
694\r
e48e37fc 695 gBS->RestoreTPL (OldTpl);\r
8a67d61d 696\r
697 return Status;\r
698}\r
699\r
700\r
701/**\r
bab52709 702 Places an asynchronous receive request into the receiving queue.\r
703 \r
704 The Receive() function places a completion token into the receive packet queue.\r
705 This function is always asynchronous.\r
706 The caller must fill in the Token.Event field in the completion token, and this\r
707 field cannot be NULL. When the receive operation completes, the EFI UDPv4 Protocol\r
708 driver updates the Token.Status and Token.Packet.RxData fields and the Token.Event\r
709 is signaled. Providing a proper notification function and context for the event\r
710 will enable the user to receive the notification and receiving status. That\r
711 notification function is guaranteed to not be re-entered.\r
712\r
713 @param This Pointer to the EFI_UDP4_PROTOCOL instance.\r
714 @param Token Pointer to a token that is associated with the receive data\r
715 descriptor.\r
716\r
717 @retval EFI_SUCCESS The receive completion token was cached.\r
718 @retval EFI_NOT_STARTED This EFI UDPv4 Protocol instance has not been started.\r
719 @retval EFI_NO_MAPPING When using a default address, configuration (DHCP, BOOTP, RARP, etc.)\r
720 is not finished yet.\r
721 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:\r
722 @retval EFI_OUT_OF_RESOURCES The receive completion token could not be queued due to a lack of system\r
723 resources (usually memory).\r
724 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.\r
725 @retval EFI_ACCESS_DENIED A receive completion token with the same Token.Event was already in\r
726 the receive queue.\r
727 @retval EFI_NOT_READY The receive request could not be queued because the receive queue is full.\r
8a67d61d 728\r
729**/\r
730EFI_STATUS\r
731EFIAPI\r
732Udp4Receive (\r
733 IN EFI_UDP4_PROTOCOL *This,\r
734 IN EFI_UDP4_COMPLETION_TOKEN *Token\r
735 )\r
736{\r
737 EFI_STATUS Status;\r
738 UDP4_INSTANCE_DATA *Instance;\r
739 EFI_TPL OldTpl;\r
740\r
741 if ((This == NULL) || (Token == NULL) || (Token->Event == NULL)) {\r
742 return EFI_INVALID_PARAMETER;\r
743 }\r
744\r
745 Instance = UDP4_INSTANCE_DATA_FROM_THIS (This);\r
746\r
747 if (Instance->IsNoMapping) {\r
748 return EFI_NO_MAPPING;\r
749 }\r
750\r
751 if (!Instance->Configured) {\r
752 return EFI_NOT_STARTED;\r
753 }\r
754\r
e48e37fc 755 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
8a67d61d 756\r
757 if (EFI_ERROR (NetMapIterate (&Instance->RxTokens, Udp4TokenExist, Token))||\r
758 EFI_ERROR (NetMapIterate (&Instance->TxTokens, Udp4TokenExist, Token))) {\r
759 //\r
760 // Return EFI_ACCESS_DENIED if the specified token is already in the TxTokens or\r
761 // RxTokens map.\r
762 //\r
763 Status = EFI_ACCESS_DENIED;\r
764 goto ON_EXIT;\r
765 }\r
766\r
767 Token->Packet.RxData = NULL;\r
768\r
769 //\r
770 // Save the token into the RxTokens map.\r
771 //\r
772 Status = NetMapInsertTail (&Instance->RxTokens, Token, NULL);\r
773 if (EFI_ERROR (Status)) {\r
36ee91ca 774 Status = EFI_NOT_READY;\r
775 goto ON_EXIT;\r
8a67d61d 776 }\r
777\r
778 //\r
779 // If there is an icmp error, report it.\r
780 //\r
781 Udp4ReportIcmpError (Instance);\r
782\r
783 //\r
bab52709 784 // Try to deliver the received datagrams.\r
8a67d61d 785 //\r
786 Udp4InstanceDeliverDgram (Instance);\r
787\r
36ee91ca 788 //\r
789 // Dispatch the DPC queued by the NotifyFunction of Token->Event.\r
790 //\r
791 NetLibDispatchDpc ();\r
792\r
8a67d61d 793ON_EXIT:\r
794\r
e48e37fc 795 gBS->RestoreTPL (OldTpl);\r
8a67d61d 796\r
797 return Status;\r
798}\r
799\r
800\r
801/**\r
bab52709 802 Aborts an asynchronous transmit or receive request.\r
803 \r
804 The Cancel() function is used to abort a pending transmit or receive request.\r
805 If the token is in the transmit or receive request queues, after calling this\r
806 function, Token.Status will be set to EFI_ABORTED and then Token.Event will be\r
807 signaled. If the token is not in one of the queues, which usually means that\r
808 the asynchronous operation has completed, this function will not signal the\r
809 token and EFI_NOT_FOUND is returned.\r
810\r
811 @param This Pointer to the EFI_UDP4_PROTOCOL instance.\r
812 @param Token Pointer to a token that has been issued by\r
813 EFI_UDP4_PROTOCOL.Transmit() or\r
814 EFI_UDP4_PROTOCOL.Receive().If NULL, all pending\r
815 tokens are aborted.\r
816\r
817 @retval EFI_SUCCESS The asynchronous I/O request was aborted and Token.Event\r
818 was signaled. When Token is NULL, all pending requests are\r
819 aborted and their events are signaled.\r
820 @retval EFI_INVALID_PARAMETER This is NULL.\r
821 @retval EFI_NOT_STARTED This instance has not been started.\r
822 @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP,\r
823 RARP, etc.) is not finished yet.\r
824 @retval EFI_NOT_FOUND When Token is not NULL, the asynchronous I/O request was\r
825 not found in the transmit or receive queue. It has either completed\r
826 or was not issued by Transmit() and Receive().\r
8a67d61d 827\r
828**/\r
829EFI_STATUS\r
830EFIAPI\r
831Udp4Cancel (\r
832 IN EFI_UDP4_PROTOCOL *This,\r
833 IN EFI_UDP4_COMPLETION_TOKEN *Token OPTIONAL\r
834 )\r
835{\r
836 EFI_STATUS Status;\r
837 UDP4_INSTANCE_DATA *Instance;\r
838 EFI_TPL OldTpl;\r
839\r
840 if (This == NULL) {\r
841 return EFI_INVALID_PARAMETER;\r
842 }\r
843\r
844 Instance = UDP4_INSTANCE_DATA_FROM_THIS (This);\r
845\r
846 if (Instance->IsNoMapping) {\r
847 return EFI_NO_MAPPING;\r
848 }\r
849\r
850 if (!Instance->Configured) {\r
851 return EFI_NOT_STARTED;\r
852 }\r
853\r
e48e37fc 854 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
8a67d61d 855\r
856 //\r
857 // Cancle the tokens specified by Token for this instance.\r
858 //\r
859 Status = Udp4InstanceCancelToken (Instance, Token);\r
860\r
36ee91ca 861 //\r
bab52709 862 // Dispatch the DPC queued by the NotifyFunction of the cancelled token's events.\r
36ee91ca 863 //\r
864 NetLibDispatchDpc ();\r
865\r
e48e37fc 866 gBS->RestoreTPL (OldTpl);\r
8a67d61d 867\r
868 return Status;\r
869}\r
870\r
871\r
872/**\r
bab52709 873 Polls for incoming data packets and processes outgoing data packets.\r
874 \r
875 The Poll() function can be used by network drivers and applications to increase\r
876 the rate that data packets are moved between the communications device and the\r
877 transmit and receive queues.\r
878 In some systems, the periodic timer event in the managed network driver may not\r
879 poll the underlying communications device fast enough to transmit and/or receive\r
880 all data packets without missing incoming packets or dropping outgoing packets.\r
881 Drivers and applications that are experiencing packet loss should try calling\r
882 the Poll() function more often.\r
883\r
884 @param This Pointer to the EFI_UDP4_PROTOCOL instance.\r
885\r
886 @retval EFI_SUCCESS Incoming or outgoing data was processed.\r
887 @retval EFI_INVALID_PARAMETER This is NULL.\r
888 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.\r
889 @retval EFI_TIMEOUT Data was dropped out of the transmit and/or receive queue.\r
8a67d61d 890\r
891**/\r
892EFI_STATUS\r
893EFIAPI\r
894Udp4Poll (\r
895 IN EFI_UDP4_PROTOCOL *This\r
896 )\r
897{\r
898 UDP4_INSTANCE_DATA *Instance;\r
899 EFI_IP4_PROTOCOL *Ip;\r
900\r
901 if (This == NULL) {\r
902 return EFI_INVALID_PARAMETER;\r
903 }\r
904\r
905 Instance = UDP4_INSTANCE_DATA_FROM_THIS (This);\r
906 Ip = Instance->IpInfo->Ip;\r
907\r
908 //\r
909 // Invode the Ip instance consumed by the udp instance to do the poll operation.\r
910 //\r
911 return Ip->Poll (Ip);\r
912}\r