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