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