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