]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Network/Udp4Dxe/Udp4Main.c
Fixed EBC build issues.
[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
4eb65aff 89 CopyMem (Udp4ConfigData, &Instance->ConfigData, sizeof (EFI_UDP4_CONFIG_DATA));\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
156\r
157 if (This == NULL) {\r
158 return EFI_INVALID_PARAMETER;\r
159 }\r
160\r
161 Instance = UDP4_INSTANCE_DATA_FROM_THIS (This);\r
162\r
163 if (!Instance->Configured && (UdpConfigData == NULL)) {\r
164 return EFI_SUCCESS;\r
165 }\r
166\r
167 Udp4Service = Instance->Udp4Service;\r
168 Status = EFI_SUCCESS;\r
169\r
170 OldTpl = NET_RAISE_TPL (NET_TPL_LOCK);\r
171\r
172 if (UdpConfigData != NULL) {\r
173\r
174 StationAddress = EFI_NTOHL (UdpConfigData->StationAddress);\r
175 SubnetMask = EFI_NTOHL (UdpConfigData->SubnetMask);\r
176 RemoteAddress = EFI_NTOHL (UdpConfigData->RemoteAddress);\r
177\r
178 if (!UdpConfigData->UseDefaultAddress &&\r
179 (!IP4_IS_VALID_NETMASK (SubnetMask) ||\r
180 !((StationAddress == 0) || Ip4IsUnicast (StationAddress, SubnetMask)) ||\r
181 !((RemoteAddress == 0) || Ip4IsUnicast (RemoteAddress, 0)))) {\r
182 //\r
183 // Don't use default address, and subnet mask is invalid or StationAddress is not\r
184 // a valid unicast IPv4 address or RemoteAddress is not a valid unicast IPv4 address\r
185 // if it is not 0.\r
186 //\r
187 Status = EFI_INVALID_PARAMETER;\r
188 goto ON_EXIT;\r
189 }\r
190\r
191 if (Instance->Configured) {\r
192 //\r
193 // The instance is already configured, try to do the re-configuration.\r
194 //\r
195 if (!Udp4IsReconfigurable (&Instance->ConfigData, UdpConfigData)) {\r
196 //\r
197 // If the new configuration data wants to change some unreconfigurable\r
198 // settings, return EFI_ALREADY_STARTED.\r
199 //\r
200 Status = EFI_ALREADY_STARTED;\r
201 goto ON_EXIT;\r
202 }\r
203\r
204 //\r
205 // Save the reconfigurable parameters.\r
206 //\r
207 Instance->ConfigData.TypeOfService = UdpConfigData->TypeOfService;\r
208 Instance->ConfigData.TimeToLive = UdpConfigData->TimeToLive;\r
209 Instance->ConfigData.DoNotFragment = UdpConfigData->DoNotFragment;\r
210 Instance->ConfigData.ReceiveTimeout = UdpConfigData->ReceiveTimeout;\r
211 Instance->ConfigData.TransmitTimeout = UdpConfigData->TransmitTimeout;\r
212 } else {\r
213 //\r
214 // Construct the Ip configuration data from the UdpConfigData.\r
215 //\r
216 Udp4BuildIp4ConfigData (UdpConfigData, &Ip4ConfigData);\r
217\r
218 //\r
219 // Configure the Ip instance wrapped in the IpInfo.\r
220 //\r
221 Status = IpIoConfigIp (Instance->IpInfo, &Ip4ConfigData);\r
222 if (EFI_ERROR (Status)) {\r
223 if (Status == EFI_NO_MAPPING) {\r
224 Instance->IsNoMapping = TRUE;\r
225 }\r
226\r
227 goto ON_EXIT;\r
228 }\r
229\r
230 Instance->IsNoMapping = FALSE;\r
231\r
232 //\r
233 // Save the configuration data.\r
234 //\r
4eb65aff 235 CopyMem (&Instance->ConfigData, UdpConfigData, sizeof (EFI_UDP4_CONFIG_DATA));\r
8a67d61d 236 Instance->ConfigData.StationAddress = Ip4ConfigData.StationAddress;\r
237 Instance->ConfigData.SubnetMask = Ip4ConfigData.SubnetMask;\r
238\r
239 //\r
240 // Try to allocate the required port resource.\r
241 //\r
242 Status = Udp4Bind (&Udp4Service->ChildrenList, &Instance->ConfigData);\r
243 if (EFI_ERROR (Status)) {\r
244 //\r
245 // Reset the ip instance if bind fails.\r
246 //\r
247 IpIoConfigIp (Instance->IpInfo, NULL);\r
248 goto ON_EXIT;\r
249 }\r
250\r
251 //\r
252 // Pre calculate the checksum for the pseudo head, ignore the UDP length first.\r
253 //\r
254 Instance->HeadSum = NetPseudoHeadChecksum (\r
255 EFI_IP4 (Instance->ConfigData.StationAddress),\r
256 EFI_IP4 (Instance->ConfigData.RemoteAddress),\r
257 EFI_IP_PROTO_UDP,\r
258 0\r
259 );\r
260\r
261 Instance->Configured = TRUE;\r
262 }\r
263 } else {\r
264 //\r
265 // UdpConfigData is NULL, reset the instance.\r
266 //\r
267 Instance->Configured = FALSE;\r
268 Instance->IsNoMapping = FALSE;\r
269\r
270 //\r
271 // Reset the Ip instance wrapped in the IpInfo.\r
272 //\r
273 IpIoConfigIp (Instance->IpInfo, NULL);\r
274\r
275 //\r
276 // Cancel all the user tokens.\r
277 //\r
278 Udp4InstanceCancelToken (Instance, NULL);\r
279\r
280 //\r
281 // Remove the buffered RxData for this instance.\r
282 //\r
283 Udp4FlushRxData (&Instance->RcvdDgramQue);\r
284 }\r
285\r
286 Udp4SetVariableData (Instance->Udp4Service);\r
287\r
288ON_EXIT:\r
289\r
290 NET_RESTORE_TPL (OldTpl);\r
291\r
292 return Status;\r
293}\r
294\r
295\r
296/**\r
297 This function is used to enable and disable the multicast group filtering.\r
298\r
299 @param This Pointer to the EFI_UDP4_PROTOCOL instance.\r
300 @param JoinFlag Set to TRUE to join a multicast group. Set to\r
301 FALSE to leave one or all multicast groups.\r
302 @param MulticastAddress Pointer to multicast group address to join or\r
303 leave.\r
304\r
305 @retval EFI_SUCCESS The operation completed successfully.\r
306 @retval EFI_NOT_STARTED The EFI UDPv4 Protocol instance has not been\r
307 started.\r
308 @retval EFI_NO_MAPPING When using a default address, configuration (DHCP,\r
309 BOOTP, 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. JoinFlag is TRUE and\r
313 MulticastAddress is NULL. JoinFlag is TRUE and\r
314 *MulticastAddress is not a valid multicast\r
315 address.\r
316 @retval EFI_ALREADY_STARTED The group address is already in the group table\r
317 (when JoinFlag is TRUE).\r
318 @retval EFI_NOT_FOUND The group address is not in the group table (when\r
319 JoinFlag is FALSE).\r
320 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.\r
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
335\r
336 if ((This == NULL) ||\r
337 (JoinFlag && (MulticastAddress == NULL)) ||\r
338 (JoinFlag && !IP4_IS_MULTICAST (EFI_NTOHL (*MulticastAddress)))) {\r
339 return EFI_INVALID_PARAMETER;\r
340 }\r
341\r
342 Instance = UDP4_INSTANCE_DATA_FROM_THIS (This);\r
343\r
344 if (Instance->IsNoMapping) {\r
345 return EFI_NO_MAPPING;\r
346 }\r
347\r
348 if (!Instance->Configured) {\r
349 return EFI_NOT_STARTED;\r
350 }\r
351\r
352 Ip = Instance->IpInfo->Ip;\r
353\r
354 OldTpl = NET_RAISE_TPL (NET_TPL_LOCK);\r
355\r
356 //\r
357 // Invoke the Ip instance the Udp4 instance consumes to do the group operation.\r
358 //\r
359 Status = Ip->Groups (Ip, JoinFlag, MulticastAddress);\r
360\r
361 if (EFI_ERROR (Status)) {\r
362 goto ON_EXIT;\r
363 }\r
364\r
365 //\r
366 // Keep a local copy of the configured multicast IPs because IpIo receives\r
367 // datagrams from the 0 station address IP instance and then UDP delivers to\r
368 // the matched instance. This copy of multicast IPs is used to avoid receive\r
369 // the mutlicast datagrams destinated to multicast IPs the other instances configured.\r
370 //\r
371 if (JoinFlag) {\r
372\r
373 NetMapInsertTail (\r
374 &Instance->McastIps,\r
375 (VOID *) (UINTN) EFI_IP4 (*MulticastAddress),\r
376 NULL\r
377 );\r
378 } else {\r
379\r
380 NetMapIterate (&Instance->McastIps, Udp4LeaveGroup, MulticastAddress);\r
381 }\r
382\r
383ON_EXIT:\r
384\r
385 NET_RESTORE_TPL (OldTpl);\r
386\r
387 return Status;\r
388}\r
389\r
390\r
391/**\r
392 This function adds a route to or deletes a route from the routing table.\r
393\r
394 @param This Pointer to the EFI_UDP4_PROTOCOL instance.\r
395 @param DeleteRoute Set to TRUE to delete this route from the routing\r
396 table. Set to FALSE to add this route to the\r
397 routing table.\r
398 @param SubnetAddress The destination network address that needs to be\r
399 routed.\r
400 @param SubnetMask The subnet mask of SubnetAddress.\r
401 @param GatewayAddress The gateway IP address for this route.\r
402\r
403 @retval EFI_SUCCESS The operation completed successfully.\r
404 @retval EFI_NOT_STARTED The EFI UDPv4 Protocol instance has not been\r
405 started.\r
406 @retval EFI_NO_MAPPING When using a default address, configuration (DHCP,\r
407 BOOTP, RARP, etc.) is not finished yet.\r
408 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:\r
409 This is NULL. SubnetAddress is NULL. SubnetMask is\r
410 NULL. GatewayAddress is NULL. SubnetAddress is not\r
411 a valid subnet address. SubnetMask is not a valid\r
412 subnet mask. GatewayAddress is not a valid unicast\r
413 IP address.\r
414 @retval EFI_OUT_OF_RESOURCES Could not add the entry to the routing table.\r
415 @retval EFI_NOT_FOUND This route is not in the routing table.\r
416 @retval EFI_ACCESS_DENIED The route is already defined in the routing table.\r
417\r
418**/\r
419EFI_STATUS\r
420EFIAPI\r
421Udp4Routes (\r
422 IN EFI_UDP4_PROTOCOL *This,\r
423 IN BOOLEAN DeleteRoute,\r
424 IN EFI_IPv4_ADDRESS *SubnetAddress,\r
425 IN EFI_IPv4_ADDRESS *SubnetMask,\r
426 IN EFI_IPv4_ADDRESS *GatewayAddress\r
427 )\r
428{\r
429 UDP4_INSTANCE_DATA *Instance;\r
430 EFI_IP4_PROTOCOL *Ip;\r
431\r
432 if (This == NULL) {\r
433 return EFI_INVALID_PARAMETER;\r
434 }\r
435\r
436 Instance = UDP4_INSTANCE_DATA_FROM_THIS (This);\r
437\r
438 if (Instance->IsNoMapping) {\r
439 return EFI_NO_MAPPING;\r
440 }\r
441\r
442 if (!Instance->Configured) {\r
443 return EFI_NOT_STARTED;\r
444 }\r
445\r
446 Ip = Instance->IpInfo->Ip;\r
447\r
448 //\r
449 // Invoke the Ip instance the Udp4 instance consumes to do the actual operation.\r
450 //\r
451 return Ip->Routes (Ip, DeleteRoute, SubnetAddress, SubnetMask, GatewayAddress);\r
452}\r
453\r
454\r
455/**\r
456 This function places a sending request to this instance of the EFI UDPv4 Protocol,\r
457 alongside the transmit data that was filled by the user.\r
458\r
459 @param This Pointer to the EFI_UDP4_PROTOCOL instance.\r
460 @param Token Pointer to the completion token that will be\r
461 placed into the transmit queue.\r
462\r
463 @retval EFI_SUCCESS The data has been queued for transmission.\r
464 @retval EFI_NOT_STARTED This EFI UDPv4 Protocol instance has not been\r
465 started.\r
466 @retval EFI_NO_MAPPING When using a default address, configuration (DHCP,\r
467 BOOTP, RARP, etc.) is not finished yet.\r
468 @retval EFI_INVALID_PARAMETER One or more of the following are TRUE: This is\r
469 NULL. Token is NULL. Token.Event is NULL.\r
470 Token.Packet.TxData is NULL.\r
471 Token.Packet.TxData.FragmentCount is zero.\r
472 Token.Packet.TxData.DataLength is not equal to the\r
473 sum of fragment lengths. One or more of the\r
474 Token.Packet.TxData.FragmentTable[].\r
475 FragmentLength fields is zero. One or more of the\r
476 Token.Packet.TxData.FragmentTable[].\r
477 FragmentBuffer fields is NULL.\r
478 Token.Packet.TxData. GatewayAddress is not a\r
479 unicast IPv4 address if it is not NULL. One or\r
480 more IPv4 addresses in Token.Packet.TxData.\r
481 UdpSessionData are not valid unicast IPv4\r
482 addresses if the UdpSessionData is not NULL.\r
483 @retval EFI_ACCESS_DENIED The transmit completion token with the same\r
484 Token.Event is already in the transmit queue.\r
485 @retval EFI_NOT_READY The completion token could not be queued because\r
486 the transmit queue is full.\r
487 @retval EFI_OUT_OF_RESOURCES Could not queue the transmit data.\r
488 @retval EFI_NOT_FOUND There is no route to the destination network or\r
489 address.\r
490 @retval EFI_BAD_BUFFER_SIZE The data length is greater than the maximum UDP\r
491 packet size. Or the length of the IP header + UDP\r
492 header + data length is greater than MTU if\r
493 DoNotFragment is TRUE.\r
494\r
495**/\r
496EFI_STATUS\r
497EFIAPI\r
498Udp4Transmit (\r
499 IN EFI_UDP4_PROTOCOL *This,\r
500 IN EFI_UDP4_COMPLETION_TOKEN *Token\r
501 )\r
502{\r
503 EFI_STATUS Status;\r
504 UDP4_INSTANCE_DATA *Instance;\r
505 EFI_TPL OldTpl;\r
506 NET_BUF *Packet;\r
507 EFI_UDP4_HEADER *Udp4Header;\r
508 EFI_UDP4_CONFIG_DATA *ConfigData;\r
509 IP4_ADDR Destination;\r
510 EFI_UDP4_TRANSMIT_DATA *TxData;\r
511 EFI_UDP4_SESSION_DATA *UdpSessionData;\r
512 UDP4_SERVICE_DATA *Udp4Service;\r
513 IP_IO_OVERRIDE Override;\r
514 UINT16 HeadSum;\r
515\r
516 if ((This == NULL) || (Token == NULL)) {\r
517 return EFI_INVALID_PARAMETER;\r
518 }\r
519\r
520 Instance = UDP4_INSTANCE_DATA_FROM_THIS (This);\r
521\r
522 if (Instance->IsNoMapping) {\r
523 return EFI_NO_MAPPING;\r
524 }\r
525\r
526 if (!Instance->Configured) {\r
527 return EFI_NOT_STARTED;\r
528 }\r
529\r
530 OldTpl = NET_RAISE_TPL (NET_TPL_LOCK);\r
531\r
532 //\r
533 // Validate the Token, if the token is invalid return the error code.\r
534 //\r
535 Status = Udp4ValidateTxToken (Instance, Token);\r
536 if (EFI_ERROR (Status)) {\r
537 goto ON_EXIT;\r
538 }\r
539\r
540 if (EFI_ERROR (NetMapIterate (&Instance->TxTokens, Udp4TokenExist, Token)) ||\r
541 EFI_ERROR (NetMapIterate (&Instance->RxTokens, Udp4TokenExist, Token))) {\r
542 //\r
543 // Try to find a duplicate token in the two token maps, if found, return\r
544 // EFI_ACCESS_DENIED.\r
545 //\r
546 Status = EFI_ACCESS_DENIED;\r
547 goto ON_EXIT;\r
548 }\r
549\r
550 TxData = Token->Packet.TxData;\r
551\r
552 //\r
553 // Create a net buffer to hold the user buffer and the udp header.\r
554 //\r
555 Packet = NetbufFromExt (\r
556 (NET_FRAGMENT *)TxData->FragmentTable,\r
557 TxData->FragmentCount,\r
558 UDP4_HEADER_SIZE,\r
559 0,\r
560 Udp4NetVectorExtFree,\r
561 NULL\r
562 );\r
563 if (Packet == NULL) {\r
564 Status = EFI_OUT_OF_RESOURCES;\r
565 goto ON_EXIT;\r
566 }\r
567\r
568 //\r
569 // Store the IpIo in ProtoData.\r
570 //\r
571 Udp4Service = Instance->Udp4Service;\r
572 *((UINTN *) &Packet->ProtoData[0]) = (UINTN) (Udp4Service->IpIo);\r
573\r
574 Udp4Header = (EFI_UDP4_HEADER *) NetbufAllocSpace (Packet, UDP4_HEADER_SIZE, TRUE);\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 (Packet->TotalSize);\r
583 Udp4Header->Checksum = 0;\r
584\r
585 UdpSessionData = TxData->UdpSessionData;\r
586 Override.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 (UdpSessionData->SourceAddress) != 0) {\r
594 Override.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 Destination = EFI_IP4 (UdpSessionData->DestinationAddress);\r
602\r
603 if (UdpSessionData->DestinationPort != 0) {\r
604 Udp4Header->DstPort = HTONS (UdpSessionData->DestinationPort);\r
605 }\r
606\r
607 //\r
608 // calculate the pseudo head checksum using the overridden parameters.\r
609 //\r
610 HeadSum = NetPseudoHeadChecksum (\r
611 EFI_IP4 (Override.SourceAddress),\r
612 Destination,\r
613 EFI_IP_PROTO_UDP,\r
614 0\r
615 );\r
616 } else {\r
617 //\r
618 // UdpSessionData is NULL, use the address and port information previously configured.\r
619 //\r
620 Destination = EFI_IP4 (ConfigData->RemoteAddress);\r
621 HeadSum = Instance->HeadSum;\r
622 }\r
623\r
624 //\r
625 // calculate the checksum.\r
626 //\r
627 Udp4Header->Checksum = Udp4Checksum (Packet, HeadSum);\r
628 if (Udp4Header->Checksum == 0) {\r
629 //\r
630 // If the calculated checksum is 0, fill the Checksum field with all ones.\r
631 //\r
632 Udp4Header->Checksum = 0xffff;\r
633 }\r
634\r
635 //\r
636 // Fill the IpIo Override data.\r
637 //\r
638 EFI_IP4 (Override.GatewayAddress) = (TxData->GatewayAddress != NULL) ?\r
639 EFI_IP4 (*(TxData->GatewayAddress)) : 0;\r
640 Override.Protocol = EFI_IP_PROTO_UDP;\r
641 Override.TypeOfService = ConfigData->TypeOfService;\r
642 Override.TimeToLive = ConfigData->TimeToLive;\r
643 Override.DoNotFragment = ConfigData->DoNotFragment;\r
644\r
645 //\r
646 // Save the token into the TxToken map.\r
647 //\r
648 Status = NetMapInsertTail (&Instance->TxTokens, Token, Packet);\r
649 if (EFI_ERROR (Status)) {\r
650 goto FREE_PACKET;\r
651 }\r
652\r
653 //\r
654 // Send out this datagram through IpIo.\r
655 //\r
656 Status = IpIoSend (\r
657 Udp4Service->IpIo,\r
658 Packet,\r
659 Instance->IpInfo,\r
660 Instance,\r
661 Token,\r
662 Destination,\r
663 &Override\r
664 );\r
665 if (EFI_ERROR (Status)) {\r
666 //\r
667 // Remove this token from the TxTokens.\r
668 //\r
669 Udp4RemoveToken (&Instance->TxTokens, Token);\r
670 }\r
671\r
672FREE_PACKET:\r
673\r
674 NetbufFree (Packet);\r
675\r
676ON_EXIT:\r
677\r
678 NET_RESTORE_TPL (OldTpl);\r
679\r
680 return Status;\r
681}\r
682\r
683\r
684/**\r
685 This function places a completion token into the receive packet queue. This function\r
686 is always asynchronous.\r
687\r
688 @param This Pointer to the EFI_UDP4_PROTOCOL instance.\r
689 @param Token Pointer to a token that is associated with the\r
690 receive data descriptor.\r
691\r
692 @retval EFI_SUCCESS The receive completion token is cached.\r
693 @retval EFI_NOT_STARTED This EFI UDPv4 Protocol instance has not been\r
694 started.\r
695 @retval EFI_NO_MAPPING When using a default address, configuration (DHCP,\r
696 BOOTP, RARP, etc.) is not finished yet.\r
697 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:\r
698 This is NULL. Token is NULL. Token.Event is NULL.\r
699 @retval EFI_OUT_OF_RESOURCES The receive completion token could not be queued\r
700 due to a lack of system resources (usually\r
701 memory).\r
702 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.\r
703 The EFI UDPv4 Protocol instance has been reset to\r
704 startup defaults.\r
705 @retval EFI_ACCESS_DENIED A receive completion token with the same\r
706 Token.Event is already in the receive queue.\r
707 @retval EFI_NOT_READY The receive request could not be queued because\r
708 the receive queue is full.\r
709\r
710**/\r
711EFI_STATUS\r
712EFIAPI\r
713Udp4Receive (\r
714 IN EFI_UDP4_PROTOCOL *This,\r
715 IN EFI_UDP4_COMPLETION_TOKEN *Token\r
716 )\r
717{\r
718 EFI_STATUS Status;\r
719 UDP4_INSTANCE_DATA *Instance;\r
720 EFI_TPL OldTpl;\r
721\r
722 if ((This == NULL) || (Token == NULL) || (Token->Event == NULL)) {\r
723 return EFI_INVALID_PARAMETER;\r
724 }\r
725\r
726 Instance = UDP4_INSTANCE_DATA_FROM_THIS (This);\r
727\r
728 if (Instance->IsNoMapping) {\r
729 return EFI_NO_MAPPING;\r
730 }\r
731\r
732 if (!Instance->Configured) {\r
733 return EFI_NOT_STARTED;\r
734 }\r
735\r
736 OldTpl = NET_RAISE_TPL (NET_TPL_LOCK);\r
737\r
738 if (EFI_ERROR (NetMapIterate (&Instance->RxTokens, Udp4TokenExist, Token))||\r
739 EFI_ERROR (NetMapIterate (&Instance->TxTokens, Udp4TokenExist, Token))) {\r
740 //\r
741 // Return EFI_ACCESS_DENIED if the specified token is already in the TxTokens or\r
742 // RxTokens map.\r
743 //\r
744 Status = EFI_ACCESS_DENIED;\r
745 goto ON_EXIT;\r
746 }\r
747\r
748 Token->Packet.RxData = NULL;\r
749\r
750 //\r
751 // Save the token into the RxTokens map.\r
752 //\r
753 Status = NetMapInsertTail (&Instance->RxTokens, Token, NULL);\r
754 if (EFI_ERROR (Status)) {\r
755 return EFI_NOT_READY;\r
756 }\r
757\r
758 //\r
759 // If there is an icmp error, report it.\r
760 //\r
761 Udp4ReportIcmpError (Instance);\r
762\r
763 //\r
764 // Try to delivered the received datagrams.\r
765 //\r
766 Udp4InstanceDeliverDgram (Instance);\r
767\r
768ON_EXIT:\r
769\r
770 NET_RESTORE_TPL (OldTpl);\r
771\r
772 return Status;\r
773}\r
774\r
775\r
776/**\r
777 This function is used to abort a pending transmit or receive request.\r
778\r
779 @param This Pointer to the EFI_UDP4_PROTOCOL instance.\r
780 @param Token Pointer to a token that has been issued by\r
781 EFI_UDP4_PROTOCOL.Transmit() or\r
782 EFI_UDP4_PROTOCOL.Receive().\r
783\r
784 @retval EFI_SUCCESS The asynchronous I/O request is aborted and\r
785 Token.Event is signaled. When Token is NULL, all\r
786 pending requests are aborted and their events are\r
787 signaled.\r
788 @retval EFI_INVALID_PARAMETER This is NULL.\r
789 @retval EFI_NOT_STARTED This instance has not been started.\r
790 @retval EFI_NO_MAPPING When using the default address, configuration\r
791 (DHCP, BOOTP, RARP, etc.) is not finished yet.\r
792 @retval EFI_NOT_FOUND When Token is not NULL, the asynchronous I/O\r
793 request is not found in the transmit or receive\r
794 queue. It is either completed or not issued by\r
795 Transmit() or Receive().\r
796\r
797**/\r
798EFI_STATUS\r
799EFIAPI\r
800Udp4Cancel (\r
801 IN EFI_UDP4_PROTOCOL *This,\r
802 IN EFI_UDP4_COMPLETION_TOKEN *Token OPTIONAL\r
803 )\r
804{\r
805 EFI_STATUS Status;\r
806 UDP4_INSTANCE_DATA *Instance;\r
807 EFI_TPL OldTpl;\r
808\r
809 if (This == NULL) {\r
810 return EFI_INVALID_PARAMETER;\r
811 }\r
812\r
813 Instance = UDP4_INSTANCE_DATA_FROM_THIS (This);\r
814\r
815 if (Instance->IsNoMapping) {\r
816 return EFI_NO_MAPPING;\r
817 }\r
818\r
819 if (!Instance->Configured) {\r
820 return EFI_NOT_STARTED;\r
821 }\r
822\r
823 OldTpl = NET_RAISE_TPL (NET_TPL_LOCK);\r
824\r
825 //\r
826 // Cancle the tokens specified by Token for this instance.\r
827 //\r
828 Status = Udp4InstanceCancelToken (Instance, Token);\r
829\r
830 NET_RESTORE_TPL (OldTpl);\r
831\r
832 return Status;\r
833}\r
834\r
835\r
836/**\r
837 This function can be used by network drivers and applications to increase the rate that\r
838 data packets are moved between the communications device and the transmit/receive queues.\r
839 Argumens:\r
840 This - Pointer to the EFI_UDP4_PROTOCOL instance.\r
841\r
842 @retval EFI_SUCCESS Incoming or outgoing data was processed.\r
843 @retval EFI_INVALID_PARAMETER This is NULL.\r
844 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.\r
845 @retval EFI_TIMEOUT Data was dropped out of the transmit and/or\r
846 receive queue.\r
847\r
848**/\r
849EFI_STATUS\r
850EFIAPI\r
851Udp4Poll (\r
852 IN EFI_UDP4_PROTOCOL *This\r
853 )\r
854{\r
855 UDP4_INSTANCE_DATA *Instance;\r
856 EFI_IP4_PROTOCOL *Ip;\r
857\r
858 if (This == NULL) {\r
859 return EFI_INVALID_PARAMETER;\r
860 }\r
861\r
862 Instance = UDP4_INSTANCE_DATA_FROM_THIS (This);\r
863 Ip = Instance->IpInfo->Ip;\r
864\r
865 //\r
866 // Invode the Ip instance consumed by the udp instance to do the poll operation.\r
867 //\r
868 return Ip->Poll (Ip);\r
869}\r