]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Network/Udp4Dxe/Udp4Impl.c
Import SnpDxe, Tcp4Dxe, Udp4Dxe and MnpDxe.
[mirror_edk2.git] / MdeModulePkg / Universal / Network / Udp4Dxe / Udp4Impl.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 Udp4Impl.c
15
16 Abstract:
17
18 The implementation of the Udp4 protocol.
19
20
21 **/
22
23
24 #include "Udp4Impl.h"
25
26 UINT16 mUdp4RandomPort;
27
28 STATIC
29 VOID
30 EFIAPI
31 Udp4CheckTimeout (
32 IN EFI_EVENT Event,
33 IN VOID *Context
34 );
35
36 STATIC
37 BOOLEAN
38 Udp4FindInstanceByPort (
39 IN NET_LIST_ENTRY *InstanceList,
40 IN EFI_IPv4_ADDRESS *Address,
41 IN UINT16 Port
42 );
43
44 STATIC
45 VOID
46 Udp4DgramSent (
47 IN EFI_STATUS Status,
48 IN VOID *Context,
49 IN VOID *Sender,
50 IN VOID *NotifyData
51 );
52
53 STATIC
54 VOID
55 Udp4DgramRcvd (
56 IN EFI_STATUS Status,
57 IN ICMP_ERROR IcmpError,
58 IN EFI_NET_SESSION_DATA *NetSession,
59 IN NET_BUF *Packet,
60 IN VOID *Context
61 );
62
63 STATIC
64 EFI_STATUS
65 Udp4CancelTokens (
66 IN NET_MAP *Map,
67 IN NET_MAP_ITEM *Item,
68 IN VOID *Arg OPTIONAL
69 );
70
71 STATIC
72 BOOLEAN
73 Udp4MatchDgram (
74 IN UDP4_INSTANCE_DATA *Instance,
75 IN EFI_UDP4_SESSION_DATA *Udp4Session
76 );
77
78 STATIC
79 VOID
80 EFIAPI
81 Udp4RecycleRxDataWrap (
82 IN EFI_EVENT Event,
83 IN VOID *Context
84 );
85
86 STATIC
87 UDP4_RXDATA_WRAP *
88 Udp4WrapRxData (
89 IN UDP4_INSTANCE_DATA *Instance,
90 IN NET_BUF *Packet,
91 IN EFI_UDP4_RECEIVE_DATA *RxData
92 );
93
94 STATIC
95 UINTN
96 Udp4EnqueueDgram (
97 IN UDP4_SERVICE_DATA *Udp4Service,
98 IN NET_BUF *Packet,
99 IN EFI_UDP4_RECEIVE_DATA *RxData
100 );
101
102 STATIC
103 VOID
104 Udp4DeliverDgram (
105 IN UDP4_SERVICE_DATA *Udp4Service
106 );
107
108 STATIC
109 VOID
110 Udp4Demultiplex (
111 IN UDP4_SERVICE_DATA *Udp4Service,
112 IN EFI_NET_SESSION_DATA *NetSession,
113 IN NET_BUF *Packet
114 );
115
116 STATIC
117 VOID
118 Udp4IcmpHandler (
119 IN UDP4_SERVICE_DATA *Udp4Service,
120 IN ICMP_ERROR IcmpError,
121 IN EFI_NET_SESSION_DATA *NetSession,
122 IN NET_BUF *Packet
123 );
124
125 STATIC
126 VOID
127 Udp4SendPortUnreach (
128 IN IP_IO *IpIo,
129 IN EFI_NET_SESSION_DATA *NetSession,
130 IN VOID *Udp4Header
131 );
132
133
134 /**
135 Create the Udp service context data.
136
137 @param Udp4Service Pointer to the UDP4_SERVICE_DATA.
138 @param ImageHandle The image handle of this udp4 driver.
139 @param ControllerHandle The controller handle this udp4 driver binds on.
140
141 @retval EFI_SUCCESS The udp4 service context data is created and
142 initialized.
143 @retval EFI_OUT_OF_RESOURCES Cannot allocate memory.
144
145 **/
146 EFI_STATUS
147 Udp4CreateService (
148 IN UDP4_SERVICE_DATA *Udp4Service,
149 IN EFI_HANDLE ImageHandle,
150 IN EFI_HANDLE ControllerHandle
151 )
152 {
153 EFI_STATUS Status;
154 IP_IO_OPEN_DATA OpenData;
155
156 Udp4Service->Signature = UDP4_SERVICE_DATA_SIGNATURE;
157 Udp4Service->ServiceBinding = mUdp4ServiceBinding;
158 Udp4Service->ImageHandle = ImageHandle;
159 Udp4Service->ControllerHandle = ControllerHandle;
160 Udp4Service->ChildrenNumber = 0;
161
162 NetListInit (&Udp4Service->ChildrenList);
163
164 //
165 // Create the IpIo for this service context.
166 //
167 Udp4Service->IpIo = IpIoCreate (ImageHandle, ControllerHandle);
168 if (Udp4Service->IpIo == NULL) {
169 return EFI_OUT_OF_RESOURCES;
170 }
171
172 //
173 // Set the OpenData used to open the IpIo.
174 //
175 OpenData.IpConfigData = mIpIoDefaultIpConfigData;
176 OpenData.IpConfigData.AcceptBroadcast = TRUE;
177 OpenData.RcvdContext = (VOID *) Udp4Service;
178 OpenData.SndContext = NULL;
179 OpenData.PktRcvdNotify = Udp4DgramRcvd;
180 OpenData.PktSentNotify = Udp4DgramSent;
181
182 //
183 // Configure and start the IpIo.
184 //
185 Status = IpIoOpen (Udp4Service->IpIo, &OpenData);
186 if (EFI_ERROR (Status)) {
187 goto RELEASE_IPIO;
188 }
189
190 //
191 // Create the event for Udp timeout checking.
192 //
193 Status = gBS->CreateEvent (
194 EVT_TIMER | EVT_NOTIFY_SIGNAL,
195 NET_TPL_FAST_TIMER,
196 Udp4CheckTimeout,
197 Udp4Service,
198 &Udp4Service->TimeoutEvent
199 );
200 if (EFI_ERROR (Status)) {
201 goto RELEASE_IPIO;
202 }
203
204 //
205 // Start the timeout timer event.
206 //
207 Status = gBS->SetTimer (
208 Udp4Service->TimeoutEvent,
209 TimerPeriodic,
210 UDP4_TIMEOUT_INTERVAL
211 );
212 if (EFI_ERROR (Status)) {
213 goto RELEASE_ALL;
214 }
215
216 Udp4Service->MacString = NULL;
217
218 return EFI_SUCCESS;
219
220 RELEASE_ALL:
221
222 gBS->CloseEvent (Udp4Service->TimeoutEvent);
223
224 RELEASE_IPIO:
225
226 IpIoDestroy (Udp4Service->IpIo);
227
228 return Status;
229 }
230
231
232 /**
233 Clean the Udp service context data.
234
235 @param Udp4Service Pointer to the UDP4_SERVICE_DATA.
236
237 @return None.
238
239 **/
240 VOID
241 Udp4CleanService (
242 IN UDP4_SERVICE_DATA *Udp4Service
243 )
244 {
245 //
246 // Cancel the TimeoutEvent timer.
247 //
248 gBS->SetTimer (Udp4Service->TimeoutEvent, TimerCancel, 0);
249
250 //
251 // Close the TimeoutEvent timer.
252 //
253 gBS->CloseEvent (Udp4Service->TimeoutEvent);
254
255 //
256 // Destroy the IpIo.
257 //
258 IpIoDestroy (Udp4Service->IpIo);
259 }
260
261
262 /**
263 This function checks and timeouts the I/O datagrams holding by the corresponding
264 service context.
265
266 @param Event The event this function registered to.
267 @param Conext The context data registered during the creation of
268 the Event.
269
270 @return None.
271
272 **/
273 STATIC
274 VOID
275 EFIAPI
276 Udp4CheckTimeout (
277 IN EFI_EVENT Event,
278 IN VOID *Context
279 )
280 {
281 UDP4_SERVICE_DATA *Udp4Service;
282 NET_LIST_ENTRY *Entry;
283 UDP4_INSTANCE_DATA *Instance;
284 NET_LIST_ENTRY *WrapEntry;
285 NET_LIST_ENTRY *NextEntry;
286 UDP4_RXDATA_WRAP *Wrap;
287
288 Udp4Service = (UDP4_SERVICE_DATA *) Context;
289 NET_CHECK_SIGNATURE (Udp4Service, UDP4_SERVICE_DATA_SIGNATURE);
290
291 NET_LIST_FOR_EACH (Entry, &Udp4Service->ChildrenList) {
292 //
293 // Iterate all the instances belonging to this service context.
294 //
295 Instance = NET_LIST_USER_STRUCT (Entry, UDP4_INSTANCE_DATA, Link);
296 NET_CHECK_SIGNATURE (Instance, UDP4_INSTANCE_DATA_SIGNATURE);
297
298 if (!Instance->Configured || (Instance->ConfigData.ReceiveTimeout == 0)) {
299 //
300 // Skip this instance if it's not configured or no receive timeout.
301 //
302 continue;
303 }
304
305 NET_LIST_FOR_EACH_SAFE (WrapEntry, NextEntry, &Instance->RcvdDgramQue) {
306 //
307 // Iterate all the rxdatas belonging to this udp instance.
308 //
309 Wrap = NET_LIST_USER_STRUCT (Entry, UDP4_RXDATA_WRAP, Link);
310
311 if (Wrap->TimeoutTick <= UDP4_TIMEOUT_INTERVAL / 1000) {
312 //
313 // Remove this RxData if it timeouts.
314 //
315 Udp4RecycleRxDataWrap (NULL, (VOID *) Wrap);
316 } else {
317 Wrap->TimeoutTick -= UDP4_TIMEOUT_INTERVAL / 1000;
318 }
319 }
320 }
321 }
322
323
324 /**
325 This function intializes the new created udp instance.
326
327 @param Udp4Service Pointer to the UDP4_SERVICE_DATA.
328 @param Instance Pointer to the un-initialized UDP4_INSTANCE_DATA.
329
330 @return None.
331
332 **/
333 VOID
334 Udp4InitInstance (
335 IN UDP4_SERVICE_DATA *Udp4Service,
336 IN UDP4_INSTANCE_DATA *Instance
337 )
338 {
339 //
340 // Set the signature.
341 //
342 Instance->Signature = UDP4_INSTANCE_DATA_SIGNATURE;
343
344 //
345 // Init the lists.
346 //
347 NetListInit (&Instance->Link);
348 NetListInit (&Instance->RcvdDgramQue);
349 NetListInit (&Instance->DeliveredDgramQue);
350
351 //
352 // Init the NET_MAPs.
353 //
354 NetMapInit (&Instance->TxTokens);
355 NetMapInit (&Instance->RxTokens);
356 NetMapInit (&Instance->McastIps);
357
358 //
359 // Save the pointer to the UDP4_SERVICE_DATA, and initialize other members.
360 //
361 Instance->Udp4Service = Udp4Service;
362 Instance->Udp4Proto = mUdp4Protocol;
363 Instance->IcmpError = EFI_SUCCESS;
364 Instance->Configured = FALSE;
365 Instance->IsNoMapping = FALSE;
366 Instance->Destroyed = FALSE;
367 }
368
369
370 /**
371 This function cleans the udp instance.
372
373 @param Instance Pointer to the UDP4_INSTANCE_DATA to clean.
374
375 @return None.
376
377 **/
378 VOID
379 Udp4CleanInstance (
380 IN UDP4_INSTANCE_DATA *Instance
381 )
382 {
383 NetMapClean (&Instance->McastIps);
384 NetMapClean (&Instance->RxTokens);
385 NetMapClean (&Instance->TxTokens);
386 }
387
388
389 /**
390 This function finds the udp instance by the specified <Address, Port> pair.
391
392 @param InstanceList Pointer to the head of the list linking the udp
393 instances.
394 @param Address Pointer to the specified IPv4 address.
395 @param Port The udp port number.
396
397 @return Is the specified <Address, Port> pair found or not.
398
399 **/
400 STATIC
401 BOOLEAN
402 Udp4FindInstanceByPort (
403 IN NET_LIST_ENTRY *InstanceList,
404 IN EFI_IPv4_ADDRESS *Address,
405 IN UINT16 Port
406 )
407 {
408 NET_LIST_ENTRY *Entry;
409 UDP4_INSTANCE_DATA *Instance;
410 EFI_UDP4_CONFIG_DATA *ConfigData;
411
412 NET_LIST_FOR_EACH (Entry, InstanceList) {
413 //
414 // Iterate all the udp instances.
415 //
416 Instance = NET_LIST_USER_STRUCT (Entry, UDP4_INSTANCE_DATA, Link);
417 ConfigData = &Instance->ConfigData;
418
419 if (!Instance->Configured || ConfigData->AcceptAnyPort) {
420 //
421 // If the instance is not configured or the configdata of the instance indicates
422 // this instance accepts any port, skip it.
423 //
424 continue;
425 }
426
427 if (EFI_IP_EQUAL (ConfigData->StationAddress, *Address) &&
428 (ConfigData->StationPort == Port)) {
429 //
430 // if both the address and the port are the same, return TRUE.
431 //
432 return TRUE;
433 }
434 }
435
436 //
437 // return FALSE when matching fails.
438 //
439 return FALSE;
440 }
441
442
443 /**
444 This function tries to bind the udp instance according to the configured port
445 allocation stragety.
446
447 @param InstanceList Pointer to the head of the list linking the udp
448 instances.
449 @param ConfigData Pointer to the ConfigData of the instance to be
450 bound.
451
452 @retval EFI_SUCCESS The bound operation is completed successfully.
453 @retval EFI_ACCESS_DENIED The <Address, Port> specified by the ConfigData is
454 already used by other instance.
455 @retval EFI_OUT_OF_RESOURCES No available port resources.
456
457 **/
458 EFI_STATUS
459 Udp4Bind (
460 IN NET_LIST_ENTRY *InstanceList,
461 IN EFI_UDP4_CONFIG_DATA *ConfigData
462 )
463 {
464 EFI_IPv4_ADDRESS *StationAddress;
465 UINT16 StartPort;
466
467 if (ConfigData->AcceptAnyPort) {
468 return EFI_SUCCESS;
469 }
470
471 StationAddress = &ConfigData->StationAddress;
472
473 if (ConfigData->StationPort != 0) {
474
475 if (!ConfigData->AllowDuplicatePort &&
476 Udp4FindInstanceByPort (InstanceList, StationAddress, ConfigData->StationPort)) {
477 //
478 // Do not allow duplicate port and the port is already used by other instance.
479 //
480 return EFI_ACCESS_DENIED;
481 }
482 } else {
483 //
484 // select a random port for this instance;
485 //
486
487 if (ConfigData->AllowDuplicatePort) {
488 //
489 // Just pick up the random port if the instance allows duplicate port.
490 //
491 ConfigData->StationPort = mUdp4RandomPort;
492 } else {
493
494 StartPort = mUdp4RandomPort;
495
496 while (Udp4FindInstanceByPort(InstanceList, StationAddress, mUdp4RandomPort)) {
497
498 mUdp4RandomPort++;
499 if (mUdp4RandomPort == 0) {
500 mUdp4RandomPort = UDP4_PORT_KNOWN;
501 }
502
503 if (mUdp4RandomPort == StartPort) {
504 //
505 // No available port.
506 //
507 return EFI_OUT_OF_RESOURCES;
508 }
509 }
510
511 ConfigData->StationPort = mUdp4RandomPort;
512 }
513
514 mUdp4RandomPort++;
515 if (mUdp4RandomPort == 0) {
516 mUdp4RandomPort = UDP4_PORT_KNOWN;
517 }
518 }
519
520 return EFI_SUCCESS;
521 }
522
523
524 /**
525 This function is used to check whether the NewConfigData has any un-reconfigurable
526 parameters changed compared to the OldConfigData.
527
528 @param OldConfigData Pointer to the current ConfigData the udp instance
529 uses.
530 @param NewConfigData Pointer to the new ConfigData.
531
532 @return The instance is reconfigurable or not according to the NewConfigData.
533
534 **/
535 BOOLEAN
536 Udp4IsReconfigurable (
537 IN EFI_UDP4_CONFIG_DATA *OldConfigData,
538 IN EFI_UDP4_CONFIG_DATA *NewConfigData
539 )
540 {
541 if ((NewConfigData->AcceptAnyPort != OldConfigData->AcceptAnyPort) ||
542 (NewConfigData->AcceptBroadcast != OldConfigData->AcceptBroadcast) ||
543 (NewConfigData->AcceptPromiscuous != OldConfigData->AcceptPromiscuous) ||
544 (NewConfigData->AllowDuplicatePort != OldConfigData->AllowDuplicatePort)) {
545 //
546 // The receiving filter parameters cannot be changed.
547 //
548 return FALSE;
549 }
550
551 if ((!NewConfigData->AcceptAnyPort) &&
552 (NewConfigData->StationPort != OldConfigData->StationPort)) {
553 //
554 // The port is not changeable.
555 //
556 return FALSE;
557 }
558
559 if (!NewConfigData->AcceptPromiscuous) {
560
561 if (NewConfigData->UseDefaultAddress != OldConfigData->UseDefaultAddress) {
562 //
563 // The NewConfigData differs to the old one on the UseDefaultAddress.
564 //
565 return FALSE;
566 }
567
568 if (!NewConfigData->UseDefaultAddress &&
569 (!EFI_IP_EQUAL (NewConfigData->StationAddress, OldConfigData->StationAddress) ||
570 !EFI_IP_EQUAL (NewConfigData->SubnetMask, OldConfigData->SubnetMask))) {
571 //
572 // If the instance doesn't use the default address, and the new address or
573 // new subnet mask is different from the old values.
574 //
575 return FALSE;
576 }
577 }
578
579 if (!EFI_IP_EQUAL (NewConfigData->RemoteAddress, OldConfigData->RemoteAddress)) {
580 //
581 // The remoteaddress is not the same.
582 //
583 return FALSE;
584 }
585
586 if ((EFI_IP4 (NewConfigData->RemoteAddress) != 0) &&
587 (NewConfigData->RemotePort != OldConfigData->RemotePort)) {
588 //
589 // The RemotePort differs if it's designated in the configdata.
590 //
591 return FALSE;
592 }
593
594 //
595 // All checks pass, return TRUE.
596 //
597 return TRUE;
598 }
599
600
601 /**
602 This function builds the Ip4 configdata from the Udp4ConfigData.
603
604 @param Udp4ConfigData Pointer to the EFI_UDP4_CONFIG_DATA.
605 @param Ip4ConfigData Pointer to the EFI_IP4_CONFIG_DATA.
606
607 @return None.
608
609 **/
610 VOID
611 Udp4BuildIp4ConfigData (
612 IN EFI_UDP4_CONFIG_DATA *Udp4ConfigData,
613 IN EFI_IP4_CONFIG_DATA *Ip4ConfigData
614 )
615 {
616 *Ip4ConfigData = mIpIoDefaultIpConfigData;
617 Ip4ConfigData->DefaultProtocol = EFI_IP_PROTO_UDP;
618 Ip4ConfigData->AcceptBroadcast = Udp4ConfigData->AcceptBroadcast;
619 Ip4ConfigData->AcceptPromiscuous = Udp4ConfigData->AcceptPromiscuous;
620 Ip4ConfigData->UseDefaultAddress = Udp4ConfigData->UseDefaultAddress;
621 Ip4ConfigData->StationAddress = Udp4ConfigData->StationAddress;
622 Ip4ConfigData->SubnetMask = Udp4ConfigData->SubnetMask;
623
624 //
625 // use the -1 magic number to disable the receiving process of the ip instance.
626 //
627 Ip4ConfigData->ReceiveTimeout = (UINT32) (-1);
628 }
629
630
631 /**
632 This function validates the TxToken, it returns the error code according to the spec.
633
634 @param Instance Pointer to the udp instance context data.
635 @param TxToken Pointer to the token to be checked.
636
637 @retval EFI_SUCCESS The TxToken is valid.
638 @retval EFI_INVALID_PARAMETER One or more of the following are TRUE: This is
639 NULL. Token is NULL. Token.Event is NULL.
640 Token.Packet.TxData is NULL.
641 Token.Packet.TxData.FragmentCount is zero.
642 Token.Packet.TxData.DataLength is not equal to the
643 sum of fragment lengths. One or more of the
644 Token.Packet.TxData.FragmentTable[].
645 FragmentLength fields is zero. One or more of the
646 Token.Packet.TxData.FragmentTable[].
647 FragmentBuffer fields is NULL.
648 Token.Packet.TxData. GatewayAddress is not a
649 unicast IPv4 address if it is not NULL. One or
650 more IPv4 addresses in Token.Packet.TxData.
651 UdpSessionData are not valid unicast IPv4
652 addresses if the UdpSessionData is not NULL.
653 @retval EFI_BAD_BUFFER_SIZE The data length is greater than the maximum UDP
654 packet size.
655
656 **/
657 EFI_STATUS
658 Udp4ValidateTxToken (
659 IN UDP4_INSTANCE_DATA *Instance,
660 IN EFI_UDP4_COMPLETION_TOKEN *TxToken
661 )
662 {
663 EFI_UDP4_TRANSMIT_DATA *TxData;
664 UINT32 Index;
665 UINT32 TotalLen;
666 EFI_UDP4_CONFIG_DATA *ConfigData;
667 EFI_UDP4_SESSION_DATA *UdpSessionData;
668 IP4_ADDR SourceAddress;
669
670 if (TxToken->Event == NULL) {
671 return EFI_INVALID_PARAMETER;
672 }
673
674 TxData = TxToken->Packet.TxData;
675
676 if ((TxData == NULL) || (TxData->FragmentCount == 0)) {
677 return EFI_INVALID_PARAMETER;
678 }
679
680 TotalLen = 0;
681 for (Index = 0; Index < TxData->FragmentCount; Index++) {
682
683 if ((TxData->FragmentTable[Index].FragmentBuffer == NULL) ||
684 (TxData->FragmentTable[Index].FragmentLength == 0)) {
685 //
686 // if the FragmentBuffer is NULL or the FragmentLeng is zero.
687 //
688 return EFI_INVALID_PARAMETER;
689 }
690
691 TotalLen += TxData->FragmentTable[Index].FragmentLength;
692 }
693
694 if (TotalLen != TxData->DataLength) {
695 //
696 // The TotalLen calculated by adding all the FragmentLeng doesn't equal to the
697 // DataLength.
698 //
699 return EFI_INVALID_PARAMETER;
700 }
701
702 if ((TxData->GatewayAddress != NULL) &&
703 !Ip4IsUnicast(EFI_NTOHL (*(TxData->GatewayAddress)), 0)) {
704 //
705 // The specified GatewayAddress is not a unicast IPv4 address while it's not 0.
706 //
707 return EFI_INVALID_PARAMETER;
708 }
709
710 ConfigData = &Instance->ConfigData;
711 UdpSessionData = TxData->UdpSessionData;
712
713 if (UdpSessionData != NULL) {
714
715 SourceAddress = EFI_NTOHL (UdpSessionData->SourceAddress);
716
717 if ((SourceAddress != 0) && !Ip4IsUnicast (SourceAddress, 0)) {
718 //
719 // Check whether SourceAddress is a valid IPv4 address in case it's not zero.
720 // The configured station address is used if SourceAddress is zero.
721 //
722 return EFI_INVALID_PARAMETER;
723 }
724
725 if ((UdpSessionData->DestinationPort == 0) && (ConfigData->RemotePort == 0)) {
726 //
727 // Ambiguous, no avalaible DestinationPort for this token.
728 //
729 return EFI_INVALID_PARAMETER;
730 }
731
732 if (EFI_IP4 (UdpSessionData->DestinationAddress) == 0) {
733 //
734 // The DestinationAddress specified in the UdpSessionData is 0.
735 //
736 return EFI_INVALID_PARAMETER;
737 }
738 } else if (EFI_IP4 (ConfigData->RemoteAddress) == 0) {
739 //
740 // the configured RemoteAddress is all zero, and the user doens't override the
741 // destination address.
742 //
743 return EFI_INVALID_PARAMETER;
744 }
745
746 if (TxData->DataLength > UDP4_MAX_DATA_SIZE) {
747 return EFI_BAD_BUFFER_SIZE;
748 }
749
750 return EFI_SUCCESS;
751 }
752
753
754 /**
755 This function checks whether the specified Token duplicates with the one in the Map.
756
757 @param Map Pointer to the NET_MAP.
758 @param Item Pointer to the NET_MAP_ITEM contain the pointer to
759 the Token.
760 @param Context Pointer to the Token to be checked.
761
762 @retval EFI_SUCCESS The Token specified by Context differs from the
763 one in the Item.
764 @retval EFI_ACCESS_DENIED The Token duplicates with the one in the Item.
765
766 **/
767 EFI_STATUS
768 Udp4TokenExist (
769 IN NET_MAP *Map,
770 IN NET_MAP_ITEM *Item,
771 IN VOID *Context
772 )
773 {
774 EFI_UDP4_COMPLETION_TOKEN *Token;
775 EFI_UDP4_COMPLETION_TOKEN *TokenInItem;
776
777 Token = (EFI_UDP4_COMPLETION_TOKEN*) Context;
778 TokenInItem = (EFI_UDP4_COMPLETION_TOKEN*) Item->Key;
779
780 if ((Token == TokenInItem) || (Token->Event == TokenInItem->Event)) {
781 //
782 // The Token duplicates with the TokenInItem in case either the two pointers are the
783 // same or the Events of these two tokens are the same.
784 //
785 return EFI_ACCESS_DENIED;
786 }
787
788 return EFI_SUCCESS;
789 }
790
791
792 /**
793 This function calculates the checksum for the Packet, utilizing the pre-calculated
794 pseudo HeadSum to reduce some overhead.
795
796 @param Packet Pointer to the NET_BUF contains the udp datagram.
797 @param HeadSum Checksum of the pseudo header execpt the length
798 field.
799
800 @return The 16-bit checksum of this udp datagram.
801
802 **/
803 UINT16
804 Udp4Checksum (
805 IN NET_BUF *Packet,
806 IN UINT16 HeadSum
807 )
808 {
809 UINT16 Checksum;
810
811 Checksum = NetbufChecksum (Packet);
812 Checksum = NetAddChecksum (Checksum, HeadSum);
813
814 Checksum = NetAddChecksum (Checksum, HTONS ((UINT16) Packet->TotalSize));
815
816 return ~Checksum;
817 }
818
819
820 /**
821 This function removes the specified Token from the TokenMap.
822
823 @param TokenMap Pointer to the NET_MAP containing the tokens.
824 @param Token Pointer to the Token to be removed.
825
826 @retval EFI_SUCCESS The specified Token is removed from the TokenMap.
827 @retval EFI_NOT_FOUND The specified Token is not found in the TokenMap.
828
829 **/
830 EFI_STATUS
831 Udp4RemoveToken (
832 IN NET_MAP *TokenMap,
833 IN EFI_UDP4_COMPLETION_TOKEN *Token
834 )
835 {
836 NET_MAP_ITEM *Item;
837
838 //
839 // Find the Token first.
840 //
841 Item = NetMapFindKey (TokenMap, (VOID *) Token);
842
843 if (Item != NULL) {
844 //
845 // Remove the token if it's found in the map.
846 //
847 NetMapRemoveItem (TokenMap, Item, NULL);
848
849 return EFI_SUCCESS;
850 }
851
852 return EFI_NOT_FOUND;
853 }
854
855
856 /**
857 This function is the packet transmitting notify function registered to the IpIo
858 interface. It's called to signal the udp TxToken when IpIo layer completes the
859 transmitting of the udp datagram.
860
861 @param Status The completion status of the output udp datagram.
862 @param Context Pointer to the context data.
863 @param Sender Pointer to the Ip sender of the udp datagram.
864 @param NotifyData Pointer to the notify data.
865
866 @return None.
867
868 **/
869 STATIC
870 VOID
871 Udp4DgramSent (
872 IN EFI_STATUS Status,
873 IN VOID *Context,
874 IN VOID *Sender,
875 IN VOID *NotifyData
876 )
877 {
878 UDP4_INSTANCE_DATA *Instance;
879 EFI_UDP4_COMPLETION_TOKEN *Token;
880
881 Instance = (UDP4_INSTANCE_DATA *) Context;
882 Token = (EFI_UDP4_COMPLETION_TOKEN *) NotifyData;
883
884 if (Udp4RemoveToken (&Instance->TxTokens, Token) == EFI_SUCCESS) {
885 //
886 // The token may be cancelled. Only signal it if the remove operation succeeds.
887 //
888 Token->Status = Status;
889 gBS->SignalEvent (Token->Event);
890 }
891 }
892
893
894 /**
895 This function processes the received datagram passed up by the IpIo layer.
896
897 @param Status The status of this udp datagram.
898 @param IcmpError The IcmpError code, only available when Status is
899 EFI_ICMP_ERROR.
900 @param NetSession Pointer to the EFI_NET_SESSION_DATA.
901 @param Packet Pointer to the NET_BUF containing the received udp
902 datagram.
903 @param Context Pointer to the context data.
904
905 @return None.
906
907 **/
908 STATIC
909 VOID
910 Udp4DgramRcvd (
911 IN EFI_STATUS Status,
912 IN ICMP_ERROR IcmpError,
913 IN EFI_NET_SESSION_DATA *NetSession,
914 IN NET_BUF *Packet,
915 IN VOID *Context
916 )
917 {
918 NET_CHECK_SIGNATURE (Packet, NET_BUF_SIGNATURE);
919
920 //
921 // IpIo only passes received packets with Status EFI_SUCCESS or EFI_ICMP_ERROR.
922 //
923 if (Status == EFI_SUCCESS) {
924 //
925 // Demultiplex the received datagram.
926 //
927 Udp4Demultiplex ((UDP4_SERVICE_DATA *) Context, NetSession, Packet);
928 } else {
929 //
930 // Handle the ICMP_ERROR packet.
931 //
932 Udp4IcmpHandler ((UDP4_SERVICE_DATA *) Context, IcmpError, NetSession, Packet);
933 }
934 }
935
936
937 /**
938 This function removes the multicast group specified by Arg from the Map.
939
940 @param Map Pointer to the NET_MAP.
941 @param Item Pointer to the NET_MAP_ITEM.
942 @param Arg Pointer to the Arg, it's the pointer to a
943 multicast IPv4 Address.
944
945 @retval EFI_SUCCESS The multicast address is removed.
946 @retval EFI_ABORTED The specified multicast address is removed and the
947 Arg is not NULL.
948
949 **/
950 EFI_STATUS
951 Udp4LeaveGroup (
952 IN NET_MAP *Map,
953 IN NET_MAP_ITEM *Item,
954 IN VOID *Arg OPTIONAL
955 )
956 {
957 EFI_IPv4_ADDRESS *McastIp;
958
959 McastIp = Arg;
960
961 if ((McastIp != NULL) && ((UINTN) EFI_IP4 (*McastIp) != (UINTN) (Item->Key))) {
962 //
963 // McastIp is not NULL and the multicast address contained in the Item
964 // is not the same as McastIp.
965 //
966 return EFI_SUCCESS;
967 }
968
969 //
970 // Remove this Item.
971 //
972 NetMapRemoveItem (Map, Item, NULL);
973
974 if (McastIp != NULL) {
975 //
976 // Return EFI_ABORTED in case McastIp is not NULL to terminate the iteration.
977 //
978 return EFI_ABORTED;
979 }
980
981 return EFI_SUCCESS;
982 }
983
984
985 /**
986 This function cancle the token specified by Arg in the Map.
987
988 @param Map Pointer to the NET_MAP.
989 @param Item Pointer to the NET_MAP_ITEM.
990 @param Arg Pointer to the token to be cancelled, if NULL, all
991 the tokens in this Map will be cancelled.
992
993 @retval EFI_SUCCESS The token is cancelled if Arg is NULL or the token
994 is not the same as that in the Item if Arg is not
995 NULL.
996 @retval EFI_ABORTED Arg is not NULL, and the token specified by Arg is
997 cancelled.
998
999 **/
1000 STATIC
1001 EFI_STATUS
1002 Udp4CancelTokens (
1003 IN NET_MAP *Map,
1004 IN NET_MAP_ITEM *Item,
1005 IN VOID *Arg OPTIONAL
1006 )
1007 {
1008 EFI_UDP4_COMPLETION_TOKEN *TokenToCancel;
1009 NET_BUF *Packet;
1010 IP_IO *IpIo;
1011
1012 if ((Arg != NULL) && (Item->Key != Arg)) {
1013 return EFI_SUCCESS;
1014 }
1015
1016 if (Item->Value != NULL) {
1017 //
1018 // If the token is a transmit token, the corresponding Packet is recorded in
1019 // Item->Value, invoke IpIo to cancel this packet first. The IpIoCancelTxToken
1020 // will invoke Udp4DgramSent, the token will be signaled and this Item will
1021 // be removed from the Map there.
1022 //
1023 Packet = (NET_BUF *) (Item->Value);
1024 IpIo = (IP_IO *) (*((UINTN *) &Packet->ProtoData[0]));
1025
1026 IpIoCancelTxToken (IpIo, Packet);
1027 } else {
1028 //
1029 // The token is a receive token. Abort it and remove it from the Map.
1030 //
1031 TokenToCancel = (EFI_UDP4_COMPLETION_TOKEN *) Item->Key;
1032
1033 TokenToCancel->Status = EFI_ABORTED;
1034 gBS->SignalEvent (TokenToCancel->Event);
1035
1036 NetMapRemoveItem (Map, Item, NULL);
1037 }
1038
1039 if (Arg != NULL) {
1040 return EFI_ABORTED;
1041 }
1042
1043 return EFI_SUCCESS;
1044 }
1045
1046
1047 /**
1048 This function removes all the Wrap datas in the RcvdDgramQue.
1049
1050 @param RcvdDgramQue Pointer to the list containing all the Wrap datas.
1051
1052 @return None.
1053
1054 **/
1055 VOID
1056 Udp4FlushRxData (
1057 IN NET_LIST_ENTRY *RcvdDgramQue
1058 )
1059 {
1060 UDP4_RXDATA_WRAP *Wrap;
1061 EFI_TPL OldTpl;
1062
1063 OldTpl = NET_RAISE_TPL (NET_TPL_RECYCLE);
1064
1065 while (!NetListIsEmpty (RcvdDgramQue)) {
1066 //
1067 // Iterate all the Wraps in the RcvdDgramQue.
1068 //
1069 Wrap = NET_LIST_HEAD (RcvdDgramQue, UDP4_RXDATA_WRAP, Link);
1070
1071 //
1072 // The Wrap will be removed from the RcvdDgramQue by this function call.
1073 //
1074 Udp4RecycleRxDataWrap (NULL, (VOID *) Wrap);
1075 }
1076
1077 NET_RESTORE_TPL (OldTpl);
1078 }
1079
1080
1081
1082 /**
1083
1084 @param Instance Pointer to the udp instance context data.
1085 @param Token Pointer to the token to be canceled, if NULL, all
1086 tokens in this instance will be cancelled.
1087
1088 @retval EFI_SUCCESS The Token is cancelled.
1089 @retval EFI_NOT_FOUND The Token is not found.
1090
1091 **/
1092 EFI_STATUS
1093 Udp4InstanceCancelToken (
1094 IN UDP4_INSTANCE_DATA *Instance,
1095 IN EFI_UDP4_COMPLETION_TOKEN *Token OPTIONAL
1096 )
1097 {
1098 EFI_STATUS Status;
1099
1100 //
1101 // Cancle this token from the TxTokens map.
1102 //
1103 Status = NetMapIterate (&Instance->TxTokens, Udp4CancelTokens, Token);
1104
1105 if ((Token != NULL) && (Status == EFI_ABORTED)) {
1106 //
1107 // If Token isn't NULL and Status is EFI_ABORTED, the token is cancelled from
1108 // the TxTokens, just return success.
1109 //
1110 return EFI_SUCCESS;
1111 }
1112
1113 //
1114 // Try to cancel this token from the RxTokens map in condition either the Token
1115 // is NULL or the specified Token is not in TxTokens.
1116 //
1117 Status = NetMapIterate (&Instance->RxTokens, Udp4CancelTokens, Token);
1118
1119 if ((Token != NULL) && (Status == EFI_SUCCESS)) {
1120 //
1121 // If Token isn't NULL and Status is EFI_SUCCESS, the token is neither in the
1122 // TxTokens nor the RxTokens, or say, it's not found.
1123 //
1124 return EFI_NOT_FOUND;
1125 }
1126
1127 ASSERT ((Token != NULL) || ((0 == NetMapGetCount (&Instance->TxTokens))
1128 && (0 == NetMapGetCount (&Instance->RxTokens))));
1129
1130 return EFI_SUCCESS;
1131 }
1132
1133
1134 /**
1135 This function matches the received udp datagram with the Instance.
1136
1137 @param Instance Pointer to the udp instance context data.
1138 @param Udp4Session Pointer to the EFI_UDP4_SESSION_DATA abstracted
1139 from the received udp datagram.
1140
1141 @return The udp datagram matches the receiving requirments of the Instance or not.
1142
1143 **/
1144 STATIC
1145 BOOLEAN
1146 Udp4MatchDgram (
1147 IN UDP4_INSTANCE_DATA *Instance,
1148 IN EFI_UDP4_SESSION_DATA *Udp4Session
1149 )
1150 {
1151 EFI_UDP4_CONFIG_DATA *ConfigData;
1152 IP4_ADDR Destination;
1153
1154 ConfigData = &Instance->ConfigData;
1155
1156 if (ConfigData->AcceptPromiscuous) {
1157 //
1158 // Always matches if this instance is in the promiscuous state.
1159 //
1160 return TRUE;
1161 }
1162
1163 if ((!ConfigData->AcceptAnyPort && (Udp4Session->DestinationPort != ConfigData->StationPort)) ||
1164 ((ConfigData->RemotePort != 0) && (Udp4Session->SourcePort != ConfigData->RemotePort))) {
1165 //
1166 // The local port or the remote port doesn't match.
1167 //
1168 return FALSE;
1169 }
1170
1171 if ((EFI_IP4 (ConfigData->RemoteAddress) != 0) &&
1172 !EFI_IP_EQUAL (ConfigData->RemoteAddress, Udp4Session->SourceAddress)) {
1173 //
1174 // This datagram doesn't come from the instance's specified sender.
1175 //
1176 return FALSE;
1177 }
1178
1179 if ((EFI_IP4 (ConfigData->StationAddress) == 0) ||
1180 EFI_IP_EQUAL (Udp4Session->DestinationAddress, ConfigData->StationAddress)) {
1181 //
1182 // The instance is configured to receive datagrams destinated to any station IP or
1183 // the destination address of this datagram matches the configured station IP.
1184 //
1185 return TRUE;
1186 }
1187
1188 Destination = EFI_IP4 (Udp4Session->DestinationAddress);
1189
1190 if (IP4_IS_LOCAL_BROADCAST (Destination) && ConfigData->AcceptBroadcast) {
1191 //
1192 // The instance is configured to receive broadcast and this is a broadcast packet.
1193 //
1194 return TRUE;
1195 }
1196
1197 if (IP4_IS_MULTICAST (NTOHL (Destination)) &&
1198 (NULL != NetMapFindKey (&Instance->McastIps, (VOID *) (UINTN) Destination))) {
1199 //
1200 // It's a multicast packet and the multicast address is accepted by this instance.
1201 //
1202 return TRUE;
1203 }
1204
1205 return FALSE;
1206 }
1207
1208
1209 /**
1210 This function removes the Wrap specified by Context and release relevant resources.
1211
1212 @param Event The Event this notify function registered to.
1213 @param Context Pointer to the context data.
1214
1215 @return None.
1216
1217 **/
1218 STATIC
1219 VOID
1220 EFIAPI
1221 Udp4RecycleRxDataWrap (
1222 IN EFI_EVENT Event,
1223 IN VOID *Context
1224 )
1225 {
1226 UDP4_RXDATA_WRAP *Wrap;
1227
1228 Wrap = (UDP4_RXDATA_WRAP *) Context;
1229
1230 //
1231 // Remove the Wrap from the list it belongs to.
1232 //
1233 NetListRemoveEntry (&Wrap->Link);
1234
1235 //
1236 // Free the Packet associated with this Wrap.
1237 //
1238 NetbufFree (Wrap->Packet);
1239
1240 //
1241 // Close the event.
1242 //
1243 gBS->CloseEvent (Wrap->RxData.RecycleSignal);
1244
1245 NetFreePool (Wrap);
1246 }
1247
1248
1249 /**
1250 This function wraps the Packet and the RxData.
1251
1252 @param Instance Pointer to the instance context data.
1253 @param Packet Pointer to the buffer containing the received
1254 datagram.
1255 @param RxData Pointer to the EFI_UDP4_RECEIVE_DATA of this
1256 datagram.
1257
1258 @return Pointer to the structure wrapping the RxData and the Packet.
1259
1260 **/
1261 STATIC
1262 UDP4_RXDATA_WRAP *
1263 Udp4WrapRxData (
1264 IN UDP4_INSTANCE_DATA *Instance,
1265 IN NET_BUF *Packet,
1266 IN EFI_UDP4_RECEIVE_DATA *RxData
1267 )
1268 {
1269 EFI_STATUS Status;
1270 UDP4_RXDATA_WRAP *Wrap;
1271
1272 //
1273 // Allocate buffer for the Wrap.
1274 //
1275 Wrap = NetAllocatePool (sizeof (UDP4_RXDATA_WRAP) +
1276 (Packet->BlockOpNum - 1) * sizeof (EFI_UDP4_FRAGMENT_DATA));
1277 if (Wrap == NULL) {
1278 return NULL;
1279 }
1280
1281 NetListInit (&Wrap->Link);
1282
1283 Wrap->RxData = *RxData;
1284
1285 //
1286 // Create the Recycle event.
1287 //
1288 Status = gBS->CreateEvent (
1289 EVT_NOTIFY_SIGNAL,
1290 NET_TPL_RECYCLE,
1291 Udp4RecycleRxDataWrap,
1292 Wrap,
1293 &Wrap->RxData.RecycleSignal
1294 );
1295 if (EFI_ERROR (Status)) {
1296 NetFreePool (Wrap);
1297 return NULL;
1298 }
1299
1300 Wrap->Packet = Packet;
1301 Wrap->TimeoutTick = Instance->ConfigData.ReceiveTimeout;
1302
1303 return Wrap;
1304 }
1305
1306
1307 /**
1308 This function enqueues the received datagram into the instances' receiving queues.
1309
1310 @param Udp4Service Pointer to the udp service context data.
1311 @param Packet Pointer to the buffer containing the received
1312 datagram.
1313 @param RxData Pointer to the EFI_UDP4_RECEIVE_DATA of this
1314 datagram.
1315
1316 @return The times this datagram is enqueued.
1317
1318 **/
1319 STATIC
1320 UINTN
1321 Udp4EnqueueDgram (
1322 IN UDP4_SERVICE_DATA *Udp4Service,
1323 IN NET_BUF *Packet,
1324 IN EFI_UDP4_RECEIVE_DATA *RxData
1325 )
1326 {
1327 NET_LIST_ENTRY *Entry;
1328 UDP4_INSTANCE_DATA *Instance;
1329 UDP4_RXDATA_WRAP *Wrap;
1330 UINTN Enqueued;
1331
1332 Enqueued = 0;
1333
1334 NET_LIST_FOR_EACH (Entry, &Udp4Service->ChildrenList) {
1335 //
1336 // Iterate the instances.
1337 //
1338 Instance = NET_LIST_USER_STRUCT (Entry, UDP4_INSTANCE_DATA, Link);
1339
1340 if (!Instance->Configured) {
1341 continue;
1342 }
1343
1344 if (Udp4MatchDgram (Instance, &RxData->UdpSession)) {
1345 //
1346 // Wrap the RxData and put this Wrap into the instances RcvdDgramQue.
1347 //
1348 Wrap = Udp4WrapRxData (Instance, Packet, RxData);
1349 if (Wrap == NULL) {
1350 continue;
1351 }
1352
1353 NET_GET_REF (Packet);
1354
1355 NetListInsertTail (&Instance->RcvdDgramQue, &Wrap->Link);
1356
1357 Enqueued++;
1358 }
1359 }
1360
1361 return Enqueued;
1362 }
1363
1364
1365 /**
1366 This function delivers the received datagrams for the specified instance.
1367
1368 @param Instance Pointer to the instance context data.
1369
1370 @return None.
1371
1372 **/
1373 VOID
1374 Udp4InstanceDeliverDgram (
1375 IN UDP4_INSTANCE_DATA *Instance
1376 )
1377 {
1378 UDP4_RXDATA_WRAP *Wrap;
1379 EFI_UDP4_COMPLETION_TOKEN *Token;
1380 NET_BUF *Dup;
1381 EFI_UDP4_RECEIVE_DATA *RxData;
1382
1383 if (!NetListIsEmpty (&Instance->RcvdDgramQue) &&
1384 !NetMapIsEmpty (&Instance->RxTokens)) {
1385
1386 Wrap = NET_LIST_HEAD (&Instance->RcvdDgramQue, UDP4_RXDATA_WRAP, Link);
1387
1388 if (NET_BUF_SHARED (Wrap->Packet)) {
1389 //
1390 // Duplicate the Packet if it is shared between instances.
1391 //
1392 Dup = NetbufDuplicate (Wrap->Packet, NULL, 0);
1393 if (Dup == NULL) {
1394 return;
1395 }
1396
1397 NetbufFree (Wrap->Packet);
1398
1399 Wrap->Packet = Dup;
1400 }
1401
1402 NetListRemoveHead (&Instance->RcvdDgramQue);
1403
1404 Token = (EFI_UDP4_COMPLETION_TOKEN *) NetMapRemoveHead (&Instance->RxTokens, NULL);
1405
1406 //
1407 // Build the FragmentTable and set the FragmentCount in RxData.
1408 //
1409 RxData = &Wrap->RxData;
1410 RxData->FragmentCount = Wrap->Packet->BlockOpNum;
1411
1412 NetbufBuildExt (
1413 Wrap->Packet,
1414 (NET_FRAGMENT *) RxData->FragmentTable,
1415 &RxData->FragmentCount
1416 );
1417
1418 Token->Status = EFI_SUCCESS;
1419 Token->Packet.RxData = &Wrap->RxData;
1420
1421 gBS->SignalEvent (Token->Event);
1422
1423 NetListInsertTail (&Instance->DeliveredDgramQue, &Wrap->Link);
1424 }
1425 }
1426
1427
1428 /**
1429 This function delivers the datagrams enqueued in the instances.
1430
1431 @param Udp4Service Pointer to the udp service context data.
1432
1433 @return None.
1434
1435 **/
1436 STATIC
1437 VOID
1438 Udp4DeliverDgram (
1439 IN UDP4_SERVICE_DATA *Udp4Service
1440 )
1441 {
1442 NET_LIST_ENTRY *Entry;
1443 UDP4_INSTANCE_DATA *Instance;
1444
1445 NET_LIST_FOR_EACH (Entry, &Udp4Service->ChildrenList) {
1446 //
1447 // Iterate the instances.
1448 //
1449 Instance = NET_LIST_USER_STRUCT (Entry, UDP4_INSTANCE_DATA, Link);
1450
1451 if (!Instance->Configured) {
1452 continue;
1453 }
1454
1455 //
1456 // Deliver the datagrams of this instance.
1457 //
1458 Udp4InstanceDeliverDgram (Instance);
1459 }
1460 }
1461
1462
1463 /**
1464 This function demultiplexes the received udp datagram to the apropriate instances.
1465
1466 @param Udp4Service Pointer to the udp service context data.
1467 @param NetSession Pointer to the EFI_NET_SESSION_DATA abstrated from
1468 the received datagram.
1469 @param Packet Pointer to the buffer containing the received udp
1470 datagram.
1471
1472 @return None.
1473
1474 **/
1475 STATIC
1476 VOID
1477 Udp4Demultiplex (
1478 IN UDP4_SERVICE_DATA *Udp4Service,
1479 IN EFI_NET_SESSION_DATA *NetSession,
1480 IN NET_BUF *Packet
1481 )
1482 {
1483 EFI_UDP4_HEADER *Udp4Header;
1484 UINT16 HeadSum;
1485 EFI_UDP4_RECEIVE_DATA RxData;
1486 EFI_UDP4_SESSION_DATA *Udp4Session;
1487 UINTN Enqueued;
1488
1489 //
1490 // Get the datagram header from the packet buffer.
1491 //
1492 Udp4Header = (EFI_UDP4_HEADER *) NetbufGetByte (Packet, 0, NULL);
1493
1494 if (Udp4Header->Checksum != 0) {
1495 //
1496 // check the checksum.
1497 //
1498 HeadSum = NetPseudoHeadChecksum (
1499 NetSession->Source,
1500 NetSession->Dest,
1501 EFI_IP_PROTO_UDP,
1502 0
1503 );
1504
1505 if (Udp4Checksum (Packet, HeadSum) != 0) {
1506 //
1507 // Wrong checksum.
1508 //
1509 return;
1510 }
1511 }
1512
1513 gRT->GetTime (&RxData.TimeStamp, NULL);
1514
1515 Udp4Session = &RxData.UdpSession;
1516 EFI_IP4 (Udp4Session->SourceAddress) = NetSession->Source;
1517 EFI_IP4 (Udp4Session->DestinationAddress) = NetSession->Dest;
1518 Udp4Session->SourcePort = NTOHS (Udp4Header->SrcPort);
1519 Udp4Session->DestinationPort = NTOHS (Udp4Header->DstPort);
1520
1521 //
1522 // Trim the UDP header.
1523 //
1524 NetbufTrim (Packet, UDP4_HEADER_SIZE, TRUE);
1525
1526 RxData.DataLength = (UINT32) Packet->TotalSize;
1527
1528 //
1529 // Try to enqueue this datagram into the instances.
1530 //
1531 Enqueued = Udp4EnqueueDgram (Udp4Service, Packet, &RxData);
1532
1533 if (Enqueued == 0) {
1534 //
1535 // Send the port unreachable ICMP packet before we free this NET_BUF
1536 //
1537 Udp4SendPortUnreach (Udp4Service->IpIo, NetSession, Udp4Header);
1538 }
1539
1540 //
1541 // Try to free the packet before deliver it.
1542 //
1543 NetbufFree (Packet);
1544
1545 if (Enqueued > 0) {
1546 //
1547 // Deliver the datagram.
1548 //
1549 Udp4DeliverDgram (Udp4Service);
1550 }
1551 }
1552
1553
1554 /**
1555 This function builds and sends out a icmp port unreachable message.
1556
1557 @param IpIo Pointer to the IP_IO instance.
1558 @param NetSession Pointer to the EFI_NET_SESSION_DATA of the packet
1559 causes this icmp error message.
1560 @param Udp4Header Pointer to the udp header of the datagram causes
1561 this icmp error message.
1562
1563 @return None.
1564
1565 **/
1566 STATIC
1567 VOID
1568 Udp4SendPortUnreach (
1569 IN IP_IO *IpIo,
1570 IN EFI_NET_SESSION_DATA *NetSession,
1571 IN VOID *Udp4Header
1572 )
1573 {
1574 NET_BUF *Packet;
1575 UINT32 Len;
1576 IP4_ICMP_ERROR_HEAD *IcmpErrHdr;
1577 EFI_IP4_HEADER *IpHdr;
1578 UINT8 *Ptr;
1579 IP_IO_OVERRIDE Override;
1580 IP_IO_IP_INFO *IpSender;
1581
1582 IpSender = IpIoFindSender (&IpIo, NetSession->Dest);
1583 if (IpSender == NULL) {
1584 //
1585 // No apropriate sender, since we cannot send out the ICMP message through
1586 // the default zero station address IP instance, abort.
1587 //
1588 return;
1589 }
1590
1591 IpHdr = NetSession->IpHdr;
1592
1593 //
1594 // Calculate the requried length of the icmp error message.
1595 //
1596 Len = sizeof (IP4_ICMP_ERROR_HEAD) + (EFI_IP4_HEADER_LEN (IpHdr) -
1597 sizeof (IP4_HEAD)) + ICMP_ERROR_PACKET_LENGTH;
1598
1599 //
1600 // Allocate buffer for the icmp error message.
1601 //
1602 Packet = NetbufAlloc (Len);
1603 if (Packet == NULL) {
1604 return;
1605 }
1606
1607 //
1608 // Allocate space for the IP4_ICMP_ERROR_HEAD.
1609 //
1610 IcmpErrHdr = (IP4_ICMP_ERROR_HEAD *) NetbufAllocSpace (Packet, Len, FALSE);
1611
1612 //
1613 // Set the required fields for the icmp port unreachable message.
1614 //
1615 IcmpErrHdr->Head.Type = ICMP_TYPE_UNREACH;
1616 IcmpErrHdr->Head.Code = ICMP_CODE_UNREACH_PORT;
1617 IcmpErrHdr->Head.Checksum = 0;
1618 IcmpErrHdr->Fourth = 0;
1619
1620 //
1621 // Copy the IP header of the datagram tragged the error.
1622 //
1623 NetCopyMem (&IcmpErrHdr->IpHead, IpHdr, EFI_IP4_HEADER_LEN (IpHdr));
1624
1625 //
1626 // Copy the UDP header.
1627 //
1628 Ptr = (UINT8 *) &IcmpErrHdr->IpHead + EFI_IP4_HEADER_LEN (IpHdr);
1629 NetCopyMem (Ptr, Udp4Header, ICMP_ERROR_PACKET_LENGTH);
1630
1631 //
1632 // Calculate the checksum.
1633 //
1634 IcmpErrHdr->Head.Checksum = ~(NetbufChecksum (Packet));
1635
1636 //
1637 // Fill the override data.
1638 //
1639 Override.DoNotFragment = FALSE;
1640 Override.TypeOfService = 0;
1641 Override.TimeToLive = 255;
1642 Override.Protocol = EFI_IP_PROTO_ICMP;
1643 EFI_IP4 (Override.SourceAddress) = NetSession->Dest;
1644 EFI_IP4 (Override.GatewayAddress) = 0;
1645
1646 //
1647 // Send out this icmp packet.
1648 //
1649 IpIoSend (IpIo, Packet, IpSender, NULL, NULL, NetSession->Source, &Override);
1650
1651 NetbufFree (Packet);
1652 }
1653
1654
1655 /**
1656 This function handles the received Icmp Error message and demultiplexes it to the
1657 instance.
1658
1659 @param Udp4Service Pointer to the udp service context data.
1660 @param IcmpError The icmp error code.
1661 @param NetSession Pointer to the EFI_NET_SESSION_DATA abstracted
1662 from the received Icmp Error packet.
1663 @param Packet Pointer to the Icmp Error packet.
1664
1665 @return None.
1666
1667 **/
1668 STATIC
1669 VOID
1670 Udp4IcmpHandler (
1671 IN UDP4_SERVICE_DATA *Udp4Service,
1672 IN ICMP_ERROR IcmpError,
1673 IN EFI_NET_SESSION_DATA *NetSession,
1674 IN NET_BUF *Packet
1675 )
1676 {
1677 EFI_UDP4_HEADER *Udp4Header;
1678 EFI_UDP4_SESSION_DATA Udp4Session;
1679 NET_LIST_ENTRY *Entry;
1680 UDP4_INSTANCE_DATA *Instance;
1681
1682 Udp4Header = (EFI_UDP4_HEADER *) NetbufGetByte (Packet, 0, NULL);
1683
1684 EFI_IP4 (Udp4Session.SourceAddress) = NetSession->Source;
1685 EFI_IP4 (Udp4Session.DestinationAddress) = NetSession->Dest;
1686 Udp4Session.SourcePort = NTOHS (Udp4Header->DstPort);
1687 Udp4Session.DestinationPort = NTOHS (Udp4Header->SrcPort);
1688
1689 NET_LIST_FOR_EACH (Entry, &Udp4Service->ChildrenList) {
1690 //
1691 // Iterate all the instances.
1692 //
1693 Instance = NET_LIST_USER_STRUCT (Entry, UDP4_INSTANCE_DATA, Link);
1694
1695 if (!Instance->Configured ||
1696 Instance->ConfigData.AcceptPromiscuous ||
1697 Instance->ConfigData.AcceptAnyPort ||
1698 (EFI_IP4 (Instance->ConfigData.StationAddress) == 0)) {
1699 //
1700 // Don't try to deliver the ICMP error to this instance if it is not configured,
1701 // or it's configured to be promiscuous or accept any port or accept all the
1702 // datagrams.
1703 //
1704 continue;
1705 }
1706
1707 if (Udp4MatchDgram (Instance, &Udp4Session)) {
1708 //
1709 // Translate the Icmp Error code according to the udp spec.
1710 //
1711 Instance->IcmpError = IpIoGetIcmpErrStatus (IcmpError, NULL, NULL);
1712
1713 if (IcmpError > ICMP_ERR_UNREACH_PORT) {
1714 Instance->IcmpError = EFI_ICMP_ERROR;
1715 }
1716
1717 //
1718 // Notify the instance with the received Icmp Error.
1719 //
1720 Udp4ReportIcmpError (Instance);
1721
1722 break;
1723 }
1724 }
1725
1726 NetbufFree (Packet);
1727 }
1728
1729
1730 /**
1731 This function reports the received ICMP error.
1732
1733 @param Instance Pointer to the udp instance context data.
1734
1735 @return None.
1736
1737 **/
1738 VOID
1739 Udp4ReportIcmpError (
1740 IN UDP4_INSTANCE_DATA *Instance
1741 )
1742 {
1743 EFI_UDP4_COMPLETION_TOKEN *Token;
1744
1745 if (NetMapIsEmpty (&Instance->RxTokens)) {
1746 //
1747 // There are no receive tokens to deliver the ICMP error.
1748 //
1749 return;
1750 }
1751
1752 if (EFI_ERROR (Instance->IcmpError)) {
1753 //
1754 // Try to get a RxToken from the RxTokens map.
1755 //
1756 Token = (EFI_UDP4_COMPLETION_TOKEN *) NetMapRemoveHead (&Instance->RxTokens, NULL);
1757
1758 if (Token != NULL) {
1759 //
1760 // Report the error through the Token.
1761 //
1762 Token->Status = Instance->IcmpError;
1763 gBS->SignalEvent (Token->Event);
1764
1765 //
1766 // Clear the IcmpError.
1767 //
1768 Instance->IcmpError = EFI_SUCCESS;
1769 }
1770 }
1771 }
1772
1773
1774 /**
1775 This function is a dummy ext-free function for the NET_BUF created for the output
1776 udp datagram.
1777
1778 @param Context Pointer to the context data.
1779
1780 @return None.
1781
1782 **/
1783 VOID
1784 Udp4NetVectorExtFree (
1785 VOID *Context
1786 )
1787 {
1788 }
1789
1790
1791 /**
1792 Set the Udp4 variable data.
1793
1794 @param Udp4Service Udp4 service data.
1795
1796 @retval EFI_OUT_OF_RESOURCES There are not enough resources to set the
1797 variable.
1798 @retval other Set variable failed.
1799
1800 **/
1801 EFI_STATUS
1802 Udp4SetVariableData (
1803 IN UDP4_SERVICE_DATA *Udp4Service
1804 )
1805 {
1806 UINT32 NumConfiguredInstance;
1807 NET_LIST_ENTRY *Entry;
1808 UINTN VariableDataSize;
1809 EFI_UDP4_VARIABLE_DATA *Udp4VariableData;
1810 EFI_UDP4_SERVICE_POINT *Udp4ServicePoint;
1811 UDP4_INSTANCE_DATA *Udp4Instance;
1812 CHAR16 *NewMacString;
1813 EFI_STATUS Status;
1814
1815 NumConfiguredInstance = 0;
1816
1817 //
1818 // Go through the children list to count the configured children.
1819 //
1820 NET_LIST_FOR_EACH (Entry, &Udp4Service->ChildrenList) {
1821 Udp4Instance = NET_LIST_USER_STRUCT_S (
1822 Entry,
1823 UDP4_INSTANCE_DATA,
1824 Link,
1825 UDP4_INSTANCE_DATA_SIGNATURE
1826 );
1827
1828 if (Udp4Instance->Configured) {
1829 NumConfiguredInstance++;
1830 }
1831 }
1832
1833 //
1834 // Calculate the size of the Udp4VariableData. As there may be no Udp4 child,
1835 // we should add extra buffer for the service points only if the number of configured
1836 // children is more than 1.
1837 //
1838 VariableDataSize = sizeof (EFI_UDP4_VARIABLE_DATA);
1839
1840 if (NumConfiguredInstance > 1) {
1841 VariableDataSize += sizeof (EFI_UDP4_SERVICE_POINT) * (NumConfiguredInstance - 1);
1842 }
1843
1844 Udp4VariableData = NetAllocatePool (VariableDataSize);
1845 if (Udp4VariableData == NULL) {
1846 return EFI_OUT_OF_RESOURCES;
1847 }
1848
1849 Udp4VariableData->DriverHandle = Udp4Service->ImageHandle;
1850 Udp4VariableData->ServiceCount = NumConfiguredInstance;
1851
1852 Udp4ServicePoint = &Udp4VariableData->Services[0];
1853
1854 //
1855 // Go through the children list to fill the configured children's address pairs.
1856 //
1857 NET_LIST_FOR_EACH (Entry, &Udp4Service->ChildrenList) {
1858 Udp4Instance = NET_LIST_USER_STRUCT_S (
1859 Entry,
1860 UDP4_INSTANCE_DATA,
1861 Link,
1862 UDP4_INSTANCE_DATA_SIGNATURE
1863 );
1864
1865 if (Udp4Instance->Configured) {
1866 Udp4ServicePoint->InstanceHandle = Udp4Instance->ChildHandle;
1867 Udp4ServicePoint->LocalAddress = Udp4Instance->ConfigData.StationAddress;
1868 Udp4ServicePoint->LocalPort = Udp4Instance->ConfigData.StationPort;
1869 Udp4ServicePoint->RemoteAddress = Udp4Instance->ConfigData.RemoteAddress;
1870 Udp4ServicePoint->RemotePort = Udp4Instance->ConfigData.RemotePort;
1871
1872 Udp4ServicePoint++;
1873 }
1874 }
1875
1876 //
1877 // Get the mac string.
1878 //
1879 Status = NetLibGetMacString (
1880 Udp4Service->ControllerHandle,
1881 Udp4Service->ImageHandle,
1882 &NewMacString
1883 );
1884 if (EFI_ERROR (Status)) {
1885 goto ON_ERROR;
1886 }
1887
1888 if (Udp4Service->MacString != NULL) {
1889 //
1890 // The variable is set already, we're going to update it.
1891 //
1892 if (StrCmp (Udp4Service->MacString, NewMacString) != 0) {
1893 //
1894 // The mac address is changed, delete the previous variable first.
1895 //
1896 gRT->SetVariable (
1897 Udp4Service->MacString,
1898 &gEfiUdp4ServiceBindingProtocolGuid,
1899 EFI_VARIABLE_BOOTSERVICE_ACCESS,
1900 0,
1901 NULL
1902 );
1903 }
1904
1905 NetFreePool (Udp4Service->MacString);
1906 }
1907
1908 Udp4Service->MacString = NewMacString;
1909
1910 Status = gRT->SetVariable (
1911 Udp4Service->MacString,
1912 &gEfiUdp4ServiceBindingProtocolGuid,
1913 EFI_VARIABLE_BOOTSERVICE_ACCESS,
1914 VariableDataSize,
1915 (VOID *) Udp4VariableData
1916 );
1917
1918 ON_ERROR:
1919
1920 NetFreePool (Udp4VariableData);
1921
1922 return Status;
1923 }
1924
1925
1926 /**
1927 Clear the variable and free the resource.
1928
1929 @param Udp4Service Udp4 service data.
1930
1931 @return None.
1932
1933 **/
1934 VOID
1935 Udp4ClearVariableData (
1936 IN UDP4_SERVICE_DATA *Udp4Service
1937 )
1938 {
1939 ASSERT (Udp4Service->MacString != NULL);
1940
1941 gRT->SetVariable (
1942 Udp4Service->MacString,
1943 &gEfiUdp4ServiceBindingProtocolGuid,
1944 EFI_VARIABLE_BOOTSERVICE_ACCESS,
1945 0,
1946 NULL
1947 );
1948
1949 NetFreePool (Udp4Service->MacString);
1950 Udp4Service->MacString = NULL;
1951 }