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