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