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