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