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