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