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