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