]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Network/Udp4Dxe/Udp4Main.c
Update the copyright notice format
[mirror_edk2.git] / MdeModulePkg / Universal / Network / Udp4Dxe / Udp4Main.c
CommitLineData
8a67d61d 1/** @file\r
2\r
e5eed7d3
HT
3Copyright (c) 2006 - 2009, Intel Corporation. All rights reserved.<BR>\r
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
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
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
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
fb115c61 510 EFI_UDP_HEADER *Udp4Header;\r
8a67d61d 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
fb115c61 519 EFI_IP_ADDRESS IpDestAddr;\r
8a67d61d 520\r
521 if ((This == NULL) || (Token == NULL)) {\r
522 return EFI_INVALID_PARAMETER;\r
523 }\r
524\r
525 Instance = UDP4_INSTANCE_DATA_FROM_THIS (This);\r
526\r
527 if (Instance->IsNoMapping) {\r
528 return EFI_NO_MAPPING;\r
529 }\r
530\r
531 if (!Instance->Configured) {\r
532 return EFI_NOT_STARTED;\r
533 }\r
534\r
e48e37fc 535 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
8a67d61d 536\r
537 //\r
538 // Validate the Token, if the token is invalid return the error code.\r
539 //\r
540 Status = Udp4ValidateTxToken (Instance, Token);\r
541 if (EFI_ERROR (Status)) {\r
542 goto ON_EXIT;\r
543 }\r
544\r
545 if (EFI_ERROR (NetMapIterate (&Instance->TxTokens, Udp4TokenExist, Token)) ||\r
546 EFI_ERROR (NetMapIterate (&Instance->RxTokens, Udp4TokenExist, Token))) {\r
547 //\r
548 // Try to find a duplicate token in the two token maps, if found, return\r
549 // EFI_ACCESS_DENIED.\r
550 //\r
551 Status = EFI_ACCESS_DENIED;\r
552 goto ON_EXIT;\r
553 }\r
554\r
555 TxData = Token->Packet.TxData;\r
556\r
557 //\r
558 // Create a net buffer to hold the user buffer and the udp header.\r
559 //\r
560 Packet = NetbufFromExt (\r
561 (NET_FRAGMENT *)TxData->FragmentTable,\r
562 TxData->FragmentCount,\r
563 UDP4_HEADER_SIZE,\r
564 0,\r
565 Udp4NetVectorExtFree,\r
566 NULL\r
567 );\r
568 if (Packet == NULL) {\r
569 Status = EFI_OUT_OF_RESOURCES;\r
570 goto ON_EXIT;\r
571 }\r
572\r
573 //\r
574 // Store the IpIo in ProtoData.\r
575 //\r
576 Udp4Service = Instance->Udp4Service;\r
577 *((UINTN *) &Packet->ProtoData[0]) = (UINTN) (Udp4Service->IpIo);\r
578\r
fb115c61 579 Udp4Header = (EFI_UDP_HEADER *) NetbufAllocSpace (Packet, UDP4_HEADER_SIZE, TRUE);\r
894d038a 580 ASSERT (Udp4Header != NULL);\r
581\r
8a67d61d 582 ConfigData = &Instance->ConfigData;\r
583\r
584 //\r
585 // Fill the udp header.\r
586 //\r
587 Udp4Header->SrcPort = HTONS (ConfigData->StationPort);\r
588 Udp4Header->DstPort = HTONS (ConfigData->RemotePort);\r
1204fe83 589 Udp4Header->Length = HTONS ((UINT16) Packet->TotalSize);\r
8a67d61d 590 Udp4Header->Checksum = 0;\r
591\r
592 UdpSessionData = TxData->UdpSessionData;\r
fb115c61 593 Override.Ip4OverrideData.SourceAddress = ConfigData->StationAddress;\r
8a67d61d 594\r
595 if (UdpSessionData != NULL) {\r
596 //\r
597 // Set the SourceAddress, SrcPort and Destination according to the specified\r
598 // UdpSessionData.\r
599 //\r
84b5c78e 600 if (!EFI_IP4_EQUAL (&UdpSessionData->SourceAddress, &mZeroIp4Addr)) {\r
fb115c61 601 CopyMem (&Override.Ip4OverrideData.SourceAddress, &UdpSessionData->SourceAddress, sizeof (EFI_IPv4_ADDRESS));\r
8a67d61d 602 }\r
603\r
604 if (UdpSessionData->SourcePort != 0) {\r
605 Udp4Header->SrcPort = HTONS (UdpSessionData->SourcePort);\r
606 }\r
607\r
8a67d61d 608 if (UdpSessionData->DestinationPort != 0) {\r
609 Udp4Header->DstPort = HTONS (UdpSessionData->DestinationPort);\r
610 }\r
611\r
fb115c61 612 CopyMem (&Source, &Override.Ip4OverrideData.SourceAddress, sizeof (IP4_ADDR));\r
e48e37fc 613 CopyMem (&Destination, &UdpSessionData->DestinationAddress, sizeof (IP4_ADDR));\r
772db4bb 614\r
8a67d61d 615 //\r
616 // calculate the pseudo head checksum using the overridden parameters.\r
617 //\r
618 HeadSum = NetPseudoHeadChecksum (\r
772db4bb 619 Source,\r
8a67d61d 620 Destination,\r
621 EFI_IP_PROTO_UDP,\r
622 0\r
623 );\r
624 } else {\r
625 //\r
626 // UdpSessionData is NULL, use the address and port information previously configured.\r
627 //\r
e48e37fc 628 CopyMem (&Destination, &ConfigData->RemoteAddress, sizeof (IP4_ADDR));\r
772db4bb 629\r
630 HeadSum = Instance->HeadSum;\r
8a67d61d 631 }\r
632\r
633 //\r
634 // calculate the checksum.\r
635 //\r
636 Udp4Header->Checksum = Udp4Checksum (Packet, HeadSum);\r
637 if (Udp4Header->Checksum == 0) {\r
638 //\r
639 // If the calculated checksum is 0, fill the Checksum field with all ones.\r
640 //\r
641 Udp4Header->Checksum = 0xffff;\r
642 }\r
643\r
644 //\r
645 // Fill the IpIo Override data.\r
646 //\r
772db4bb 647 if (TxData->GatewayAddress != NULL) {\r
fb115c61 648 CopyMem (&Override.Ip4OverrideData.GatewayAddress, TxData->GatewayAddress, sizeof (EFI_IPv4_ADDRESS));\r
772db4bb 649 } else {\r
fb115c61 650 ZeroMem (&Override.Ip4OverrideData.GatewayAddress, sizeof (EFI_IPv4_ADDRESS));\r
772db4bb 651 }\r
652\r
fb115c61 653 Override.Ip4OverrideData.Protocol = EFI_IP_PROTO_UDP;\r
654 Override.Ip4OverrideData.TypeOfService = ConfigData->TypeOfService;\r
655 Override.Ip4OverrideData.TimeToLive = ConfigData->TimeToLive;\r
656 Override.Ip4OverrideData.DoNotFragment = ConfigData->DoNotFragment;\r
8a67d61d 657\r
658 //\r
659 // Save the token into the TxToken map.\r
660 //\r
661 Status = NetMapInsertTail (&Instance->TxTokens, Token, Packet);\r
662 if (EFI_ERROR (Status)) {\r
663 goto FREE_PACKET;\r
664 }\r
665\r
666 //\r
667 // Send out this datagram through IpIo.\r
668 //\r
fb115c61 669 IpDestAddr.Addr[0] = Destination;\r
8a67d61d 670 Status = IpIoSend (\r
671 Udp4Service->IpIo,\r
672 Packet,\r
673 Instance->IpInfo,\r
674 Instance,\r
675 Token,\r
fb115c61 676 &IpDestAddr,\r
8a67d61d 677 &Override\r
678 );\r
679 if (EFI_ERROR (Status)) {\r
680 //\r
681 // Remove this token from the TxTokens.\r
682 //\r
683 Udp4RemoveToken (&Instance->TxTokens, Token);\r
684 }\r
685\r
686FREE_PACKET:\r
687\r
688 NetbufFree (Packet);\r
689\r
690ON_EXIT:\r
691\r
e48e37fc 692 gBS->RestoreTPL (OldTpl);\r
8a67d61d 693\r
694 return Status;\r
695}\r
696\r
697\r
698/**\r
bab52709 699 Places an asynchronous receive request into the receiving queue.\r
700 \r
701 The Receive() function places a completion token into the receive packet queue.\r
702 This function is always asynchronous.\r
703 The caller must fill in the Token.Event field in the completion token, and this\r
704 field cannot be NULL. When the receive operation completes, the EFI UDPv4 Protocol\r
705 driver updates the Token.Status and Token.Packet.RxData fields and the Token.Event\r
706 is signaled. Providing a proper notification function and context for the event\r
707 will enable the user to receive the notification and receiving status. That\r
708 notification function is guaranteed to not be re-entered.\r
709\r
3e8c18da 710 @param[in] This Pointer to the EFI_UDP4_PROTOCOL instance.\r
711 @param[in] Token Pointer to a token that is associated with\r
712 the receive data descriptor.\r
bab52709 713\r
714 @retval EFI_SUCCESS The receive completion token was cached.\r
715 @retval EFI_NOT_STARTED This EFI UDPv4 Protocol instance has not been started.\r
716 @retval EFI_NO_MAPPING When using a default address, configuration (DHCP, BOOTP, RARP, etc.)\r
717 is not finished yet.\r
718 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:\r
719 @retval EFI_OUT_OF_RESOURCES The receive completion token could not be queued due to a lack of system\r
720 resources (usually memory).\r
721 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.\r
722 @retval EFI_ACCESS_DENIED A receive completion token with the same Token.Event was already in\r
723 the receive queue.\r
724 @retval EFI_NOT_READY The receive request could not be queued because the receive queue is full.\r
8a67d61d 725\r
726**/\r
727EFI_STATUS\r
728EFIAPI\r
729Udp4Receive (\r
730 IN EFI_UDP4_PROTOCOL *This,\r
731 IN EFI_UDP4_COMPLETION_TOKEN *Token\r
732 )\r
733{\r
734 EFI_STATUS Status;\r
735 UDP4_INSTANCE_DATA *Instance;\r
736 EFI_TPL OldTpl;\r
737\r
738 if ((This == NULL) || (Token == NULL) || (Token->Event == NULL)) {\r
739 return EFI_INVALID_PARAMETER;\r
740 }\r
741\r
742 Instance = UDP4_INSTANCE_DATA_FROM_THIS (This);\r
743\r
744 if (Instance->IsNoMapping) {\r
745 return EFI_NO_MAPPING;\r
746 }\r
747\r
748 if (!Instance->Configured) {\r
749 return EFI_NOT_STARTED;\r
750 }\r
751\r
e48e37fc 752 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
8a67d61d 753\r
754 if (EFI_ERROR (NetMapIterate (&Instance->RxTokens, Udp4TokenExist, Token))||\r
755 EFI_ERROR (NetMapIterate (&Instance->TxTokens, Udp4TokenExist, Token))) {\r
756 //\r
757 // Return EFI_ACCESS_DENIED if the specified token is already in the TxTokens or\r
758 // RxTokens map.\r
759 //\r
760 Status = EFI_ACCESS_DENIED;\r
761 goto ON_EXIT;\r
762 }\r
763\r
764 Token->Packet.RxData = NULL;\r
765\r
766 //\r
767 // Save the token into the RxTokens map.\r
768 //\r
769 Status = NetMapInsertTail (&Instance->RxTokens, Token, NULL);\r
770 if (EFI_ERROR (Status)) {\r
36ee91ca 771 Status = EFI_NOT_READY;\r
772 goto ON_EXIT;\r
8a67d61d 773 }\r
774\r
775 //\r
776 // If there is an icmp error, report it.\r
777 //\r
778 Udp4ReportIcmpError (Instance);\r
779\r
780 //\r
bab52709 781 // Try to deliver the received datagrams.\r
8a67d61d 782 //\r
783 Udp4InstanceDeliverDgram (Instance);\r
784\r
36ee91ca 785 //\r
786 // Dispatch the DPC queued by the NotifyFunction of Token->Event.\r
787 //\r
d8d26fb2 788 DispatchDpc ();\r
36ee91ca 789\r
8a67d61d 790ON_EXIT:\r
791\r
e48e37fc 792 gBS->RestoreTPL (OldTpl);\r
8a67d61d 793\r
794 return Status;\r
795}\r
796\r
797\r
798/**\r
bab52709 799 Aborts an asynchronous transmit or receive request.\r
800 \r
801 The Cancel() function is used to abort a pending transmit or receive request.\r
802 If the token is in the transmit or receive request queues, after calling this\r
803 function, Token.Status will be set to EFI_ABORTED and then Token.Event will be\r
804 signaled. If the token is not in one of the queues, which usually means that\r
805 the asynchronous operation has completed, this function will not signal the\r
806 token and EFI_NOT_FOUND is returned.\r
807\r
3e8c18da 808 @param[in] This Pointer to the EFI_UDP4_PROTOCOL instance.\r
809 @param[in] Token Pointer to a token that has been issued by\r
810 EFI_UDP4_PROTOCOL.Transmit() or\r
811 EFI_UDP4_PROTOCOL.Receive().If NULL, all pending\r
812 tokens are aborted.\r
bab52709 813\r
814 @retval EFI_SUCCESS The asynchronous I/O request was aborted and Token.Event\r
815 was signaled. When Token is NULL, all pending requests are\r
816 aborted and their events are signaled.\r
817 @retval EFI_INVALID_PARAMETER This is NULL.\r
818 @retval EFI_NOT_STARTED This instance has not been started.\r
819 @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP,\r
820 RARP, etc.) is not finished yet.\r
821 @retval EFI_NOT_FOUND When Token is not NULL, the asynchronous I/O request was\r
822 not found in the transmit or receive queue. It has either completed\r
823 or was not issued by Transmit() and Receive().\r
8a67d61d 824\r
825**/\r
826EFI_STATUS\r
827EFIAPI\r
828Udp4Cancel (\r
829 IN EFI_UDP4_PROTOCOL *This,\r
830 IN EFI_UDP4_COMPLETION_TOKEN *Token OPTIONAL\r
831 )\r
832{\r
833 EFI_STATUS Status;\r
834 UDP4_INSTANCE_DATA *Instance;\r
835 EFI_TPL OldTpl;\r
836\r
837 if (This == NULL) {\r
838 return EFI_INVALID_PARAMETER;\r
839 }\r
840\r
841 Instance = UDP4_INSTANCE_DATA_FROM_THIS (This);\r
842\r
843 if (Instance->IsNoMapping) {\r
844 return EFI_NO_MAPPING;\r
845 }\r
846\r
847 if (!Instance->Configured) {\r
848 return EFI_NOT_STARTED;\r
849 }\r
850\r
e48e37fc 851 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
8a67d61d 852\r
853 //\r
854 // Cancle the tokens specified by Token for this instance.\r
855 //\r
856 Status = Udp4InstanceCancelToken (Instance, Token);\r
857\r
36ee91ca 858 //\r
bab52709 859 // Dispatch the DPC queued by the NotifyFunction of the cancelled token's events.\r
36ee91ca 860 //\r
d8d26fb2 861 DispatchDpc ();\r
36ee91ca 862\r
e48e37fc 863 gBS->RestoreTPL (OldTpl);\r
8a67d61d 864\r
865 return Status;\r
866}\r
867\r
868\r
869/**\r
bab52709 870 Polls for incoming data packets and processes outgoing data packets.\r
871 \r
872 The Poll() function can be used by network drivers and applications to increase\r
873 the rate that data packets are moved between the communications device and the\r
874 transmit and receive queues.\r
875 In some systems, the periodic timer event in the managed network driver may not\r
876 poll the underlying communications device fast enough to transmit and/or receive\r
877 all data packets without missing incoming packets or dropping outgoing packets.\r
878 Drivers and applications that are experiencing packet loss should try calling\r
879 the Poll() function more often.\r
880\r
3e8c18da 881 @param[in] This Pointer to the EFI_UDP4_PROTOCOL instance.\r
bab52709 882\r
883 @retval EFI_SUCCESS Incoming or outgoing data was processed.\r
884 @retval EFI_INVALID_PARAMETER This is NULL.\r
885 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.\r
886 @retval EFI_TIMEOUT Data was dropped out of the transmit and/or receive queue.\r
8a67d61d 887\r
888**/\r
889EFI_STATUS\r
890EFIAPI\r
891Udp4Poll (\r
892 IN EFI_UDP4_PROTOCOL *This\r
893 )\r
894{\r
895 UDP4_INSTANCE_DATA *Instance;\r
896 EFI_IP4_PROTOCOL *Ip;\r
897\r
898 if (This == NULL) {\r
899 return EFI_INVALID_PARAMETER;\r
900 }\r
901\r
902 Instance = UDP4_INSTANCE_DATA_FROM_THIS (This);\r
903 Ip = Instance->IpInfo->Ip;\r
904\r
905 //\r
906 // Invode the Ip instance consumed by the udp instance to do the poll operation.\r
907 //\r
908 return Ip->Poll (Ip);\r
909}\r