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