]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Network/Udp4Dxe/Udp4Main.c
Retire NetLibQueueDpc() and NetLibDispatchDpc() and use QueueDpc() and DispatchDpc...
[mirror_edk2.git] / MdeModulePkg / Universal / Network / Udp4Dxe / Udp4Main.c
CommitLineData
8a67d61d 1/** @file\r
2\r
894d038a 3Copyright (c) 2006 - 2009, Intel Corporation.<BR>\r
8a67d61d 4All rights reserved. This program and the accompanying materials\r
5are licensed and made available under the terms and conditions of the BSD License\r
6which accompanies this distribution. The full text of the license may be found at\r
7http://opensource.org/licenses/bsd-license.php\r
8\r
9THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
10WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
11\r
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
36 @param This Pointer to the EFI_UDP4_PROTOCOL instance.\r
37 @param Udp4ConfigData Pointer to the buffer to receive the current configuration data.\r
38 @param Ip4ModeData Pointer to the EFI IPv4 Protocol mode data structure.\r
39 @param MnpConfigData Pointer to the managed network configuration data structure.\r
40 @param SnpModeData Pointer to the simple network mode data structure.\r
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
82 Ip = Instance->IpInfo->Ip;\r
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
174 !((StationAddress == 0) || Ip4IsUnicast (StationAddress, SubnetMask)) ||\r
175 !((RemoteAddress == 0) || Ip4IsUnicast (RemoteAddress, 0)))) {\r
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
283\r
284 Udp4SetVariableData (Instance->Udp4Service);\r
285\r
286ON_EXIT:\r
287\r
e48e37fc 288 gBS->RestoreTPL (OldTpl);\r
8a67d61d 289\r
290 return Status;\r
291}\r
292\r
293\r
294/**\r
bab52709 295 Joins and leaves multicast groups.\r
296 \r
297 The Groups() function is used to enable and disable the multicast group\r
298 filtering. If the JoinFlag is FALSE and the MulticastAddress is NULL, then all\r
299 currently joined groups are left.\r
300\r
3e8c18da 301 @param[in] This Pointer to the EFI_UDP4_PROTOCOL instance.\r
302 @param[in] JoinFlag Set to TRUE to join a multicast group. Set to FALSE to leave one\r
303 or all multicast groups.\r
304 @param[in] MulticastAddress Pointer to multicast group address to join or leave.\r
bab52709 305\r
306 @retval EFI_SUCCESS The operation completed successfully.\r
307 @retval EFI_NOT_STARTED The EFI UDPv4 Protocol instance has not been started.\r
308 @retval EFI_NO_MAPPING When using a default address, configuration (DHCP, BOOTP,\r
309 RARP, etc.) is not finished yet.\r
310 @retval EFI_OUT_OF_RESOURCES Could not allocate resources to join the group.\r
311 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:\r
312 - This is NULL.\r
313 - JoinFlag is TRUE and MulticastAddress is NULL.\r
314 - JoinFlag is TRUE and *MulticastAddress is not\r
315 a valid multicast address.\r
316 @retval EFI_ALREADY_STARTED The group address is already in the group table (when\r
317 JoinFlag is TRUE).\r
318 @retval EFI_NOT_FOUND The group address is not in the group table (when JoinFlag is\r
319 FALSE).\r
320 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.\r
8a67d61d 321\r
322**/\r
323EFI_STATUS\r
324EFIAPI\r
325Udp4Groups (\r
326 IN EFI_UDP4_PROTOCOL *This,\r
327 IN BOOLEAN JoinFlag,\r
328 IN EFI_IPv4_ADDRESS *MulticastAddress OPTIONAL\r
329 )\r
330{\r
331 EFI_STATUS Status;\r
332 UDP4_INSTANCE_DATA *Instance;\r
333 EFI_IP4_PROTOCOL *Ip;\r
334 EFI_TPL OldTpl;\r
772db4bb 335 IP4_ADDR McastIp;\r
8a67d61d 336\r
772db4bb 337 if ((This == NULL) || (JoinFlag && (MulticastAddress == NULL))) {\r
8a67d61d 338 return EFI_INVALID_PARAMETER;\r
339 }\r
340\r
772db4bb 341 McastIp = 0;\r
342 if (JoinFlag) {\r
e48e37fc 343 CopyMem (&McastIp, MulticastAddress, sizeof (IP4_ADDR));\r
772db4bb 344\r
687a2e5f 345 if (!IP4_IS_MULTICAST (NTOHL (McastIp))) {\r
772db4bb 346 return EFI_INVALID_PARAMETER;\r
347 }\r
348 }\r
349\r
8a67d61d 350 Instance = UDP4_INSTANCE_DATA_FROM_THIS (This);\r
351\r
352 if (Instance->IsNoMapping) {\r
353 return EFI_NO_MAPPING;\r
354 }\r
355\r
356 if (!Instance->Configured) {\r
357 return EFI_NOT_STARTED;\r
358 }\r
359\r
360 Ip = Instance->IpInfo->Ip;\r
361\r
e48e37fc 362 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
8a67d61d 363\r
364 //\r
365 // Invoke the Ip instance the Udp4 instance consumes to do the group operation.\r
366 //\r
367 Status = Ip->Groups (Ip, JoinFlag, MulticastAddress);\r
368\r
369 if (EFI_ERROR (Status)) {\r
370 goto ON_EXIT;\r
371 }\r
372\r
373 //\r
374 // Keep a local copy of the configured multicast IPs because IpIo receives\r
375 // datagrams from the 0 station address IP instance and then UDP delivers to\r
376 // the matched instance. This copy of multicast IPs is used to avoid receive\r
bab52709 377 // the mutlicast datagrams destined to multicast IPs the other instances configured.\r
8a67d61d 378 //\r
379 if (JoinFlag) {\r
380\r
772db4bb 381 NetMapInsertTail (&Instance->McastIps, (VOID *) (UINTN) McastIp, NULL);\r
8a67d61d 382 } else {\r
383\r
384 NetMapIterate (&Instance->McastIps, Udp4LeaveGroup, MulticastAddress);\r
385 }\r
386\r
387ON_EXIT:\r
388\r
e48e37fc 389 gBS->RestoreTPL (OldTpl);\r
8a67d61d 390\r
391 return Status;\r
392}\r
393\r
394\r
395/**\r
bab52709 396 Adds and deletes routing table entries.\r
397 \r
398 The Routes() function adds a route to or deletes a route from the routing table.\r
399 Routes are determined by comparing the SubnetAddress with the destination IP\r
400 address and arithmetically AND-ing it with the SubnetMask. The gateway address\r
401 must be on the same subnet as the configured station address.\r
402 The default route is added with SubnetAddress and SubnetMask both set to 0.0.0.0.\r
403 The default route matches all destination IP addresses that do not match any\r
404 other routes.\r
405 A zero GatewayAddress is a nonroute. Packets are sent to the destination IP\r
406 address if it can be found in the Address Resolution Protocol (ARP) cache or\r
407 on the local subnet. One automatic nonroute entry will be inserted into the\r
408 routing table for outgoing packets that are addressed to a local subnet\r
409 (gateway address of 0.0.0.0).\r
410 Each instance of the EFI UDPv4 Protocol has its own independent routing table.\r
411 Instances of the EFI UDPv4 Protocol that use the default IP address will also\r
412 have copies of the routing table provided by the EFI_IP4_CONFIG_PROTOCOL. These\r
413 copies will be updated automatically whenever the IP driver reconfigures its\r
414 instances; as a result, the previous modification to these copies will be lost.\r
415\r
3e8c18da 416 @param[in] This Pointer to the EFI_UDP4_PROTOCOL instance.\r
417 @param[in] DeleteRoute Set to TRUE to delete this route from the routing table.\r
418 Set to FALSE to add this route to the routing table.\r
419 @param[in] SubnetAddress The destination network address that needs to be routed.\r
420 @param[in] SubnetMask The subnet mask of SubnetAddress.\r
421 @param[in] GatewayAddress The gateway IP address for this route.\r
bab52709 422\r
423 @retval EFI_SUCCESS The operation completed successfully.\r
424 @retval EFI_NOT_STARTED The EFI UDPv4 Protocol instance has not been started.\r
425 @retval EFI_NO_MAPPING When using a default address, configuration (DHCP, BOOTP,\r
426 - RARP, etc.) is not finished yet.\r
427 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
428 @retval EFI_OUT_OF_RESOURCES Could not add the entry to the routing table.\r
429 @retval EFI_NOT_FOUND This route is not in the routing table.\r
430 @retval EFI_ACCESS_DENIED The route is already defined in the routing table.\r
8a67d61d 431\r
432**/\r
433EFI_STATUS\r
434EFIAPI\r
435Udp4Routes (\r
436 IN EFI_UDP4_PROTOCOL *This,\r
437 IN BOOLEAN DeleteRoute,\r
438 IN EFI_IPv4_ADDRESS *SubnetAddress,\r
439 IN EFI_IPv4_ADDRESS *SubnetMask,\r
440 IN EFI_IPv4_ADDRESS *GatewayAddress\r
441 )\r
442{\r
443 UDP4_INSTANCE_DATA *Instance;\r
444 EFI_IP4_PROTOCOL *Ip;\r
445\r
446 if (This == NULL) {\r
447 return EFI_INVALID_PARAMETER;\r
448 }\r
449\r
450 Instance = UDP4_INSTANCE_DATA_FROM_THIS (This);\r
451\r
452 if (Instance->IsNoMapping) {\r
453 return EFI_NO_MAPPING;\r
454 }\r
455\r
456 if (!Instance->Configured) {\r
457 return EFI_NOT_STARTED;\r
458 }\r
459\r
460 Ip = Instance->IpInfo->Ip;\r
461\r
462 //\r
463 // Invoke the Ip instance the Udp4 instance consumes to do the actual operation.\r
464 //\r
465 return Ip->Routes (Ip, DeleteRoute, SubnetAddress, SubnetMask, GatewayAddress);\r
466}\r
467\r
468\r
469/**\r
bab52709 470 Queues outgoing data packets into the transmit queue.\r
471 \r
472 The Transmit() function places a sending request to this instance of the EFI\r
473 UDPv4 Protocol, alongside the transmit data that was filled by the user. Whenever\r
474 the packet in the token is sent out or some errors occur, the Token.Event will\r
475 be signaled and Token.Status is updated. Providing a proper notification function\r
476 and context for the event will enable the user to receive the notification and\r
477 transmitting status.\r
478\r
3e8c18da 479 @param[in] This Pointer to the EFI_UDP4_PROTOCOL instance.\r
480 @param[in] Token Pointer to the completion token that will be placed into the\r
481 transmit queue.\r
bab52709 482\r
483 @retval EFI_SUCCESS The data has been queued for transmission.\r
484 @retval EFI_NOT_STARTED This EFI UDPv4 Protocol instance has not been started.\r
485 @retval EFI_NO_MAPPING When using a default address, configuration (DHCP, BOOTP,\r
486 RARP, etc.) is not finished yet.\r
487 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
488 @retval EFI_ACCESS_DENIED The transmit completion token with the same\r
489 Token.Event was already in the transmit queue.\r
490 @retval EFI_NOT_READY The completion token could not be queued because the\r
491 transmit queue is full.\r
492 @retval EFI_OUT_OF_RESOURCES Could not queue the transmit data.\r
493 @retval EFI_NOT_FOUND There is no route to the destination network or address.\r
494 @retval EFI_BAD_BUFFER_SIZE The data length is greater than the maximum UDP packet\r
495 size. Or the length of the IP header + UDP header + data\r
496 length is greater than MTU if DoNotFragment is TRUE.\r
8a67d61d 497\r
498**/\r
499EFI_STATUS\r
500EFIAPI\r
501Udp4Transmit (\r
502 IN EFI_UDP4_PROTOCOL *This,\r
503 IN EFI_UDP4_COMPLETION_TOKEN *Token\r
504 )\r
505{\r
506 EFI_STATUS Status;\r
507 UDP4_INSTANCE_DATA *Instance;\r
508 EFI_TPL OldTpl;\r
509 NET_BUF *Packet;\r
510 EFI_UDP4_HEADER *Udp4Header;\r
511 EFI_UDP4_CONFIG_DATA *ConfigData;\r
772db4bb 512 IP4_ADDR Source;\r
8a67d61d 513 IP4_ADDR Destination;\r
514 EFI_UDP4_TRANSMIT_DATA *TxData;\r
515 EFI_UDP4_SESSION_DATA *UdpSessionData;\r
516 UDP4_SERVICE_DATA *Udp4Service;\r
517 IP_IO_OVERRIDE Override;\r
518 UINT16 HeadSum;\r
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
578 Udp4Header = (EFI_UDP4_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
588 Udp4Header->Length = HTONS (Packet->TotalSize);\r
589 Udp4Header->Checksum = 0;\r
590\r
591 UdpSessionData = TxData->UdpSessionData;\r
592 Override.SourceAddress = ConfigData->StationAddress;\r
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
e48e37fc 600 CopyMem (&Override.SourceAddress, &UdpSessionData->SourceAddress, sizeof (EFI_IPv4_ADDRESS));\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
e48e37fc 611 CopyMem (&Source, &Override.SourceAddress, sizeof (IP4_ADDR));\r
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
e48e37fc 647 CopyMem (&Override.GatewayAddress, TxData->GatewayAddress, sizeof (EFI_IPv4_ADDRESS));\r
772db4bb 648 } else {\r
e48e37fc 649 ZeroMem (&Override.GatewayAddress, sizeof (EFI_IPv4_ADDRESS));\r
772db4bb 650 }\r
651\r
8a67d61d 652 Override.Protocol = EFI_IP_PROTO_UDP;\r
653 Override.TypeOfService = ConfigData->TypeOfService;\r
654 Override.TimeToLive = ConfigData->TimeToLive;\r
655 Override.DoNotFragment = ConfigData->DoNotFragment;\r
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
668 Status = IpIoSend (\r
669 Udp4Service->IpIo,\r
670 Packet,\r
671 Instance->IpInfo,\r
672 Instance,\r
673 Token,\r
674 Destination,\r
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
901 Ip = Instance->IpInfo->Ip;\r
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