]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Network/Udp4Dxe/Udp4Impl.c
Use Mde library and definition instead of some native definitions in NetLib, to simpl...
[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 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 ZeroMem (Udp4Service, sizeof (UDP4_SERVICE_DATA));
157
158 Udp4Service->Signature = UDP4_SERVICE_DATA_SIGNATURE;
159 Udp4Service->ServiceBinding = mUdp4ServiceBinding;
160 Udp4Service->ImageHandle = ImageHandle;
161 Udp4Service->ControllerHandle = ControllerHandle;
162 Udp4Service->ChildrenNumber = 0;
163
164 InitializeListHead (&Udp4Service->ChildrenList);
165
166 //
167 // Create the IpIo for this service context.
168 //
169 Udp4Service->IpIo = IpIoCreate (ImageHandle, ControllerHandle);
170 if (Udp4Service->IpIo == NULL) {
171 return EFI_OUT_OF_RESOURCES;
172 }
173
174 //
175 // Set the OpenData used to open the IpIo.
176 //
177 CopyMem (&OpenData.IpConfigData, &mIpIoDefaultIpConfigData, sizeof (OpenData.IpConfigData));
178 OpenData.IpConfigData.AcceptBroadcast = TRUE;
179 OpenData.RcvdContext = (VOID *) Udp4Service;
180 OpenData.SndContext = NULL;
181 OpenData.PktRcvdNotify = Udp4DgramRcvd;
182 OpenData.PktSentNotify = Udp4DgramSent;
183
184 //
185 // Configure and start the IpIo.
186 //
187 Status = IpIoOpen (Udp4Service->IpIo, &OpenData);
188 if (EFI_ERROR (Status)) {
189 goto ON_ERROR;
190 }
191
192 //
193 // Create the event for Udp timeout checking.
194 //
195 Status = gBS->CreateEvent (
196 EVT_TIMER | EVT_NOTIFY_SIGNAL,
197 TPL_CALLBACK,
198 Udp4CheckTimeout,
199 Udp4Service,
200 &Udp4Service->TimeoutEvent
201 );
202 if (EFI_ERROR (Status)) {
203 goto ON_ERROR;
204 }
205
206 //
207 // Start the timeout timer event.
208 //
209 Status = gBS->SetTimer (
210 Udp4Service->TimeoutEvent,
211 TimerPeriodic,
212 UDP4_TIMEOUT_INTERVAL
213 );
214 if (EFI_ERROR (Status)) {
215 goto ON_ERROR;
216 }
217
218 return EFI_SUCCESS;
219
220 ON_ERROR:
221
222 if (Udp4Service->TimeoutEvent != NULL) {
223 gBS->CloseEvent (Udp4Service->TimeoutEvent);
224 }
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 LIST_ENTRY *Entry;
283 UDP4_INSTANCE_DATA *Instance;
284 LIST_ENTRY *WrapEntry;
285 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 InitializeListHead (&Instance->Link);
348 InitializeListHead (&Instance->RcvdDgramQue);
349 InitializeListHead (&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 CopyMem (&Instance->Udp4Proto, &mUdp4Protocol, sizeof (Instance->Udp4Proto));
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 LIST_ENTRY *InstanceList,
404 IN EFI_IPv4_ADDRESS *Address,
405 IN UINT16 Port
406 )
407 {
408 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_IP4_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 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_IP4_EQUAL (&NewConfigData->StationAddress, &OldConfigData->StationAddress) ||
570 !EFI_IP4_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_IP4_EQUAL (&NewConfigData->RemoteAddress, &OldConfigData->RemoteAddress)) {
580 //
581 // The remoteaddress is not the same.
582 //
583 return FALSE;
584 }
585
586 if (!EFI_IP4_EQUAL (&NewConfigData->RemoteAddress, &mZeroIp4Addr) && (NewConfigData->RemotePort != OldConfigData->RemotePort)) {
587 //
588 // The RemotePort differs if it's designated in the configdata.
589 //
590 return FALSE;
591 }
592
593 //
594 // All checks pass, return TRUE.
595 //
596 return TRUE;
597 }
598
599
600 /**
601 This function builds the Ip4 configdata from the Udp4ConfigData.
602
603 @param Udp4ConfigData Pointer to the EFI_UDP4_CONFIG_DATA.
604 @param Ip4ConfigData Pointer to the EFI_IP4_CONFIG_DATA.
605
606 @return None.
607
608 **/
609 VOID
610 Udp4BuildIp4ConfigData (
611 IN EFI_UDP4_CONFIG_DATA *Udp4ConfigData,
612 IN EFI_IP4_CONFIG_DATA *Ip4ConfigData
613 )
614 {
615 CopyMem (Ip4ConfigData, &mIpIoDefaultIpConfigData, sizeof (*Ip4ConfigData));
616
617 Ip4ConfigData->DefaultProtocol = EFI_IP_PROTO_UDP;
618 Ip4ConfigData->AcceptBroadcast = Udp4ConfigData->AcceptBroadcast;
619 Ip4ConfigData->AcceptPromiscuous = Udp4ConfigData->AcceptPromiscuous;
620 Ip4ConfigData->UseDefaultAddress = Udp4ConfigData->UseDefaultAddress;
621 CopyMem (&Ip4ConfigData->StationAddress, &Udp4ConfigData->StationAddress, sizeof (EFI_IPv4_ADDRESS));
622 CopyMem (&Ip4ConfigData->SubnetMask, &Udp4ConfigData->SubnetMask, sizeof (EFI_IPv4_ADDRESS));
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 IP4_ADDR GatewayAddress;
670
671 if (TxToken->Event == NULL) {
672 return EFI_INVALID_PARAMETER;
673 }
674
675 TxData = TxToken->Packet.TxData;
676
677 if ((TxData == NULL) || (TxData->FragmentCount == 0)) {
678 return EFI_INVALID_PARAMETER;
679 }
680
681 TotalLen = 0;
682 for (Index = 0; Index < TxData->FragmentCount; Index++) {
683
684 if ((TxData->FragmentTable[Index].FragmentBuffer == NULL) ||
685 (TxData->FragmentTable[Index].FragmentLength == 0)) {
686 //
687 // if the FragmentBuffer is NULL or the FragmentLeng is zero.
688 //
689 return EFI_INVALID_PARAMETER;
690 }
691
692 TotalLen += TxData->FragmentTable[Index].FragmentLength;
693 }
694
695 if (TotalLen != TxData->DataLength) {
696 //
697 // The TotalLen calculated by adding all the FragmentLeng doesn't equal to the
698 // DataLength.
699 //
700 return EFI_INVALID_PARAMETER;
701 }
702
703 if (TxData->GatewayAddress != NULL) {
704 CopyMem (&GatewayAddress, TxData->GatewayAddress, sizeof (IP4_ADDR));
705
706 if (!Ip4IsUnicast (NTOHL (GatewayAddress), 0)) {
707 //
708 // The specified GatewayAddress is not a unicast IPv4 address while it's not 0.
709 //
710 return EFI_INVALID_PARAMETER;
711 }
712 }
713
714 ConfigData = &Instance->ConfigData;
715 UdpSessionData = TxData->UdpSessionData;
716
717 if (UdpSessionData != NULL) {
718
719 CopyMem (&SourceAddress, &UdpSessionData->SourceAddress, sizeof (IP4_ADDR));
720
721 if ((SourceAddress != 0) && !Ip4IsUnicast (HTONL (SourceAddress), 0)) {
722 //
723 // Check whether SourceAddress is a valid IPv4 address in case it's not zero.
724 // The configured station address is used if SourceAddress is zero.
725 //
726 return EFI_INVALID_PARAMETER;
727 }
728
729 if ((UdpSessionData->DestinationPort == 0) && (ConfigData->RemotePort == 0)) {
730 //
731 // Ambiguous, no avalaible DestinationPort for this token.
732 //
733 return EFI_INVALID_PARAMETER;
734 }
735
736 if (EFI_IP4_EQUAL (&UdpSessionData->DestinationAddress, &mZeroIp4Addr)) {
737 //
738 // The DestinationAddress specified in the UdpSessionData is 0.
739 //
740 return EFI_INVALID_PARAMETER;
741 }
742 } else if (EFI_IP4_EQUAL (&ConfigData->RemoteAddress, &mZeroIp4Addr)) {
743 //
744 // the configured RemoteAddress is all zero, and the user doens't override the
745 // destination address.
746 //
747 return EFI_INVALID_PARAMETER;
748 }
749
750 if (TxData->DataLength > UDP4_MAX_DATA_SIZE) {
751 return EFI_BAD_BUFFER_SIZE;
752 }
753
754 return EFI_SUCCESS;
755 }
756
757
758 /**
759 This function checks whether the specified Token duplicates with the one in the Map.
760
761 @param Map Pointer to the NET_MAP.
762 @param Item Pointer to the NET_MAP_ITEM contain the pointer to
763 the Token.
764 @param Context Pointer to the Token to be checked.
765
766 @retval EFI_SUCCESS The Token specified by Context differs from the
767 one in the Item.
768 @retval EFI_ACCESS_DENIED The Token duplicates with the one in the Item.
769
770 **/
771 EFI_STATUS
772 Udp4TokenExist (
773 IN NET_MAP *Map,
774 IN NET_MAP_ITEM *Item,
775 IN VOID *Context
776 )
777 {
778 EFI_UDP4_COMPLETION_TOKEN *Token;
779 EFI_UDP4_COMPLETION_TOKEN *TokenInItem;
780
781 Token = (EFI_UDP4_COMPLETION_TOKEN*) Context;
782 TokenInItem = (EFI_UDP4_COMPLETION_TOKEN*) Item->Key;
783
784 if ((Token == TokenInItem) || (Token->Event == TokenInItem->Event)) {
785 //
786 // The Token duplicates with the TokenInItem in case either the two pointers are the
787 // same or the Events of these two tokens are the same.
788 //
789 return EFI_ACCESS_DENIED;
790 }
791
792 return EFI_SUCCESS;
793 }
794
795
796 /**
797 This function calculates the checksum for the Packet, utilizing the pre-calculated
798 pseudo HeadSum to reduce some overhead.
799
800 @param Packet Pointer to the NET_BUF contains the udp datagram.
801 @param HeadSum Checksum of the pseudo header execpt the length
802 field.
803
804 @return The 16-bit checksum of this udp datagram.
805
806 **/
807 UINT16
808 Udp4Checksum (
809 IN NET_BUF *Packet,
810 IN UINT16 HeadSum
811 )
812 {
813 UINT16 Checksum;
814
815 Checksum = NetbufChecksum (Packet);
816 Checksum = NetAddChecksum (Checksum, HeadSum);
817
818 Checksum = NetAddChecksum (Checksum, HTONS ((UINT16) Packet->TotalSize));
819
820 return (UINT16) ~Checksum;
821 }
822
823
824 /**
825 This function removes the specified Token from the TokenMap.
826
827 @param TokenMap Pointer to the NET_MAP containing the tokens.
828 @param Token Pointer to the Token to be removed.
829
830 @retval EFI_SUCCESS The specified Token is removed from the TokenMap.
831 @retval EFI_NOT_FOUND The specified Token is not found in the TokenMap.
832
833 **/
834 EFI_STATUS
835 Udp4RemoveToken (
836 IN NET_MAP *TokenMap,
837 IN EFI_UDP4_COMPLETION_TOKEN *Token
838 )
839 {
840 NET_MAP_ITEM *Item;
841
842 //
843 // Find the Token first.
844 //
845 Item = NetMapFindKey (TokenMap, (VOID *) Token);
846
847 if (Item != NULL) {
848 //
849 // Remove the token if it's found in the map.
850 //
851 NetMapRemoveItem (TokenMap, Item, NULL);
852
853 return EFI_SUCCESS;
854 }
855
856 return EFI_NOT_FOUND;
857 }
858
859
860 /**
861 This function is the packet transmitting notify function registered to the IpIo
862 interface. It's called to signal the udp TxToken when IpIo layer completes the
863 transmitting of the udp datagram.
864
865 @param Status The completion status of the output udp datagram.
866 @param Context Pointer to the context data.
867 @param Sender Pointer to the Ip sender of the udp datagram.
868 @param NotifyData Pointer to the notify data.
869
870 @return None.
871
872 **/
873 STATIC
874 VOID
875 Udp4DgramSent (
876 IN EFI_STATUS Status,
877 IN VOID *Context,
878 IN VOID *Sender,
879 IN VOID *NotifyData
880 )
881 {
882 UDP4_INSTANCE_DATA *Instance;
883 EFI_UDP4_COMPLETION_TOKEN *Token;
884
885 Instance = (UDP4_INSTANCE_DATA *) Context;
886 Token = (EFI_UDP4_COMPLETION_TOKEN *) NotifyData;
887
888 if (Udp4RemoveToken (&Instance->TxTokens, Token) == EFI_SUCCESS) {
889 //
890 // The token may be cancelled. Only signal it if the remove operation succeeds.
891 //
892 Token->Status = Status;
893 gBS->SignalEvent (Token->Event);
894 NetLibDispatchDpc ();
895 }
896 }
897
898
899 /**
900 This function processes the received datagram passed up by the IpIo layer.
901
902 @param Status The status of this udp datagram.
903 @param IcmpError The IcmpError code, only available when Status is
904 EFI_ICMP_ERROR.
905 @param NetSession Pointer to the EFI_NET_SESSION_DATA.
906 @param Packet Pointer to the NET_BUF containing the received udp
907 datagram.
908 @param Context Pointer to the context data.
909
910 @return None.
911
912 **/
913 STATIC
914 VOID
915 Udp4DgramRcvd (
916 IN EFI_STATUS Status,
917 IN ICMP_ERROR IcmpError,
918 IN EFI_NET_SESSION_DATA *NetSession,
919 IN NET_BUF *Packet,
920 IN VOID *Context
921 )
922 {
923 NET_CHECK_SIGNATURE (Packet, NET_BUF_SIGNATURE);
924
925 //
926 // IpIo only passes received packets with Status EFI_SUCCESS or EFI_ICMP_ERROR.
927 //
928 if (Status == EFI_SUCCESS) {
929 //
930 // Demultiplex the received datagram.
931 //
932 Udp4Demultiplex ((UDP4_SERVICE_DATA *) Context, NetSession, Packet);
933 } else {
934 //
935 // Handle the ICMP_ERROR packet.
936 //
937 Udp4IcmpHandler ((UDP4_SERVICE_DATA *) Context, IcmpError, NetSession, Packet);
938 }
939
940 //
941 // Dispatch the DPC queued by the NotifyFunction of the rx token's events
942 // which are signaled with received data.
943 //
944 NetLibDispatchDpc ();
945 }
946
947
948 /**
949 This function removes the multicast group specified by Arg from the Map.
950
951 @param Map Pointer to the NET_MAP.
952 @param Item Pointer to the NET_MAP_ITEM.
953 @param Arg Pointer to the Arg, it's the pointer to a
954 multicast IPv4 Address.
955
956 @retval EFI_SUCCESS The multicast address is removed.
957 @retval EFI_ABORTED The specified multicast address is removed and the
958 Arg is not NULL.
959
960 **/
961 EFI_STATUS
962 Udp4LeaveGroup (
963 IN NET_MAP *Map,
964 IN NET_MAP_ITEM *Item,
965 IN VOID *Arg OPTIONAL
966 )
967 {
968 EFI_IPv4_ADDRESS *McastIp;
969
970 McastIp = Arg;
971
972 if ((McastIp != NULL) && (!EFI_IP4_EQUAL (McastIp, &(Item->Key)))) {
973 //
974 // McastIp is not NULL and the multicast address contained in the Item
975 // is not the same as McastIp.
976 //
977 return EFI_SUCCESS;
978 }
979
980 //
981 // Remove this Item.
982 //
983 NetMapRemoveItem (Map, Item, NULL);
984
985 if (McastIp != NULL) {
986 //
987 // Return EFI_ABORTED in case McastIp is not NULL to terminate the iteration.
988 //
989 return EFI_ABORTED;
990 }
991
992 return EFI_SUCCESS;
993 }
994
995
996 /**
997 This function cancle the token specified by Arg in the Map.
998
999 @param Map Pointer to the NET_MAP.
1000 @param Item Pointer to the NET_MAP_ITEM.
1001 @param Arg Pointer to the token to be cancelled, if NULL, all
1002 the tokens in this Map will be cancelled.
1003
1004 @retval EFI_SUCCESS The token is cancelled if Arg is NULL or the token
1005 is not the same as that in the Item if Arg is not
1006 NULL.
1007 @retval EFI_ABORTED Arg is not NULL, and the token specified by Arg is
1008 cancelled.
1009
1010 **/
1011 STATIC
1012 EFI_STATUS
1013 Udp4CancelTokens (
1014 IN NET_MAP *Map,
1015 IN NET_MAP_ITEM *Item,
1016 IN VOID *Arg OPTIONAL
1017 )
1018 {
1019 EFI_UDP4_COMPLETION_TOKEN *TokenToCancel;
1020 NET_BUF *Packet;
1021 IP_IO *IpIo;
1022
1023 if ((Arg != NULL) && (Item->Key != Arg)) {
1024 return EFI_SUCCESS;
1025 }
1026
1027 if (Item->Value != NULL) {
1028 //
1029 // If the token is a transmit token, the corresponding Packet is recorded in
1030 // Item->Value, invoke IpIo to cancel this packet first. The IpIoCancelTxToken
1031 // will invoke Udp4DgramSent, the token will be signaled and this Item will
1032 // be removed from the Map there.
1033 //
1034 Packet = (NET_BUF *) (Item->Value);
1035 IpIo = (IP_IO *) (*((UINTN *) &Packet->ProtoData[0]));
1036
1037 IpIoCancelTxToken (IpIo, Packet);
1038 } else {
1039 //
1040 // The token is a receive token. Abort it and remove it from the Map.
1041 //
1042 TokenToCancel = (EFI_UDP4_COMPLETION_TOKEN *) Item->Key;
1043 NetMapRemoveItem (Map, Item, NULL);
1044
1045 TokenToCancel->Status = EFI_ABORTED;
1046 gBS->SignalEvent (TokenToCancel->Event);
1047 }
1048
1049 if (Arg != NULL) {
1050 return EFI_ABORTED;
1051 }
1052
1053 return EFI_SUCCESS;
1054 }
1055
1056
1057 /**
1058 This function removes all the Wrap datas in the RcvdDgramQue.
1059
1060 @param RcvdDgramQue Pointer to the list containing all the Wrap datas.
1061
1062 @return None.
1063
1064 **/
1065 VOID
1066 Udp4FlushRcvdDgram (
1067 IN UDP4_INSTANCE_DATA *Instance
1068 )
1069 {
1070 UDP4_RXDATA_WRAP *Wrap;
1071
1072 while (!IsListEmpty (&Instance->RcvdDgramQue)) {
1073 //
1074 // Iterate all the Wraps in the RcvdDgramQue.
1075 //
1076 Wrap = NET_LIST_HEAD (&Instance->RcvdDgramQue, UDP4_RXDATA_WRAP, Link);
1077
1078 //
1079 // The Wrap will be removed from the RcvdDgramQue by this function call.
1080 //
1081 Udp4RecycleRxDataWrap (NULL, (VOID *) Wrap);
1082 }
1083 }
1084
1085
1086
1087 /**
1088
1089 @param Instance Pointer to the udp instance context data.
1090 @param Token Pointer to the token to be canceled, if NULL, all
1091 tokens in this instance will be cancelled.
1092
1093 @retval EFI_SUCCESS The Token is cancelled.
1094 @retval EFI_NOT_FOUND The Token is not found.
1095
1096 **/
1097 EFI_STATUS
1098 Udp4InstanceCancelToken (
1099 IN UDP4_INSTANCE_DATA *Instance,
1100 IN EFI_UDP4_COMPLETION_TOKEN *Token OPTIONAL
1101 )
1102 {
1103 EFI_STATUS Status;
1104
1105 //
1106 // Cancle this token from the TxTokens map.
1107 //
1108 Status = NetMapIterate (&Instance->TxTokens, Udp4CancelTokens, Token);
1109
1110 if ((Token != NULL) && (Status == EFI_ABORTED)) {
1111 //
1112 // If Token isn't NULL and Status is EFI_ABORTED, the token is cancelled from
1113 // the TxTokens, just return success.
1114 //
1115 return EFI_SUCCESS;
1116 }
1117
1118 //
1119 // Try to cancel this token from the RxTokens map in condition either the Token
1120 // is NULL or the specified Token is not in TxTokens.
1121 //
1122 Status = NetMapIterate (&Instance->RxTokens, Udp4CancelTokens, Token);
1123
1124 if ((Token != NULL) && (Status == EFI_SUCCESS)) {
1125 //
1126 // If Token isn't NULL and Status is EFI_SUCCESS, the token is neither in the
1127 // TxTokens nor the RxTokens, or say, it's not found.
1128 //
1129 return EFI_NOT_FOUND;
1130 }
1131
1132 ASSERT ((Token != NULL) || ((0 == NetMapGetCount (&Instance->TxTokens))
1133 && (0 == NetMapGetCount (&Instance->RxTokens))));
1134
1135 return EFI_SUCCESS;
1136 }
1137
1138
1139 /**
1140 This function matches the received udp datagram with the Instance.
1141
1142 @param Instance Pointer to the udp instance context data.
1143 @param Udp4Session Pointer to the EFI_UDP4_SESSION_DATA abstracted
1144 from the received udp datagram.
1145
1146 @return The udp datagram matches the receiving requirments of the Instance or not.
1147
1148 **/
1149 STATIC
1150 BOOLEAN
1151 Udp4MatchDgram (
1152 IN UDP4_INSTANCE_DATA *Instance,
1153 IN EFI_UDP4_SESSION_DATA *Udp4Session
1154 )
1155 {
1156 EFI_UDP4_CONFIG_DATA *ConfigData;
1157 IP4_ADDR Destination;
1158
1159 ConfigData = &Instance->ConfigData;
1160
1161 if (ConfigData->AcceptPromiscuous) {
1162 //
1163 // Always matches if this instance is in the promiscuous state.
1164 //
1165 return TRUE;
1166 }
1167
1168 if ((!ConfigData->AcceptAnyPort && (Udp4Session->DestinationPort != ConfigData->StationPort)) ||
1169 ((ConfigData->RemotePort != 0) && (Udp4Session->SourcePort != ConfigData->RemotePort))) {
1170 //
1171 // The local port or the remote port doesn't match.
1172 //
1173 return FALSE;
1174 }
1175
1176 if (!EFI_IP4_EQUAL (&ConfigData->RemoteAddress, &mZeroIp4Addr) &&
1177 !EFI_IP4_EQUAL (&ConfigData->RemoteAddress, &Udp4Session->SourceAddress)) {
1178 //
1179 // This datagram doesn't come from the instance's specified sender.
1180 //
1181 return FALSE;
1182 }
1183
1184 if (EFI_IP4_EQUAL (&ConfigData->StationAddress, &mZeroIp4Addr) ||
1185 EFI_IP4_EQUAL (&Udp4Session->DestinationAddress, &ConfigData->StationAddress)) {
1186 //
1187 // The instance is configured to receive datagrams destinated to any station IP or
1188 // the destination address of this datagram matches the configured station IP.
1189 //
1190 return TRUE;
1191 }
1192
1193 CopyMem (&Destination, &Udp4Session->DestinationAddress, sizeof (IP4_ADDR));
1194
1195 if (IP4_IS_LOCAL_BROADCAST (Destination) && ConfigData->AcceptBroadcast) {
1196 //
1197 // The instance is configured to receive broadcast and this is a broadcast packet.
1198 //
1199 return TRUE;
1200 }
1201
1202 if (IP4_IS_MULTICAST (NTOHL (Destination)) &&
1203 (NULL != NetMapFindKey (&Instance->McastIps, (VOID *) (UINTN) Destination))) {
1204 //
1205 // It's a multicast packet and the multicast address is accepted by this instance.
1206 //
1207 return TRUE;
1208 }
1209
1210 return FALSE;
1211 }
1212
1213
1214 /**
1215 This function removes the Wrap specified by Context and release relevant resources.
1216
1217 @param Event The Event this notify function registered to.
1218 @param Context Pointer to the context data.
1219
1220 @return None.
1221
1222 **/
1223 STATIC
1224 VOID
1225 EFIAPI
1226 Udp4RecycleRxDataWrap (
1227 IN EFI_EVENT Event,
1228 IN VOID *Context
1229 )
1230 {
1231 UDP4_RXDATA_WRAP *Wrap;
1232
1233 Wrap = (UDP4_RXDATA_WRAP *) Context;
1234
1235 //
1236 // Remove the Wrap from the list it belongs to.
1237 //
1238 RemoveEntryList (&Wrap->Link);
1239
1240 //
1241 // Free the Packet associated with this Wrap.
1242 //
1243 NetbufFree (Wrap->Packet);
1244
1245 //
1246 // Close the event.
1247 //
1248 gBS->CloseEvent (Wrap->RxData.RecycleSignal);
1249
1250 gBS->FreePool (Wrap);
1251 }
1252
1253
1254 /**
1255 This function wraps the Packet and the RxData.
1256
1257 @param Instance Pointer to the instance context data.
1258 @param Packet Pointer to the buffer containing the received
1259 datagram.
1260 @param RxData Pointer to the EFI_UDP4_RECEIVE_DATA of this
1261 datagram.
1262
1263 @return Pointer to the structure wrapping the RxData and the Packet.
1264
1265 **/
1266 STATIC
1267 UDP4_RXDATA_WRAP *
1268 Udp4WrapRxData (
1269 IN UDP4_INSTANCE_DATA *Instance,
1270 IN NET_BUF *Packet,
1271 IN EFI_UDP4_RECEIVE_DATA *RxData
1272 )
1273 {
1274 EFI_STATUS Status;
1275 UDP4_RXDATA_WRAP *Wrap;
1276
1277 //
1278 // Allocate buffer for the Wrap.
1279 //
1280 Wrap = AllocatePool (sizeof (UDP4_RXDATA_WRAP) +
1281 (Packet->BlockOpNum - 1) * sizeof (EFI_UDP4_FRAGMENT_DATA));
1282 if (Wrap == NULL) {
1283 return NULL;
1284 }
1285
1286 InitializeListHead (&Wrap->Link);
1287
1288 CopyMem (&Wrap->RxData, RxData, sizeof (Wrap->RxData));
1289
1290 //
1291 // Create the Recycle event.
1292 //
1293 Status = gBS->CreateEvent (
1294 EVT_NOTIFY_SIGNAL,
1295 TPL_NOTIFY,
1296 Udp4RecycleRxDataWrap,
1297 Wrap,
1298 &Wrap->RxData.RecycleSignal
1299 );
1300 if (EFI_ERROR (Status)) {
1301 gBS->FreePool (Wrap);
1302 return NULL;
1303 }
1304
1305 Wrap->Packet = Packet;
1306 Wrap->TimeoutTick = Instance->ConfigData.ReceiveTimeout;
1307
1308 return Wrap;
1309 }
1310
1311
1312 /**
1313 This function enqueues the received datagram into the instances' receiving queues.
1314
1315 @param Udp4Service Pointer to the udp service context data.
1316 @param Packet Pointer to the buffer containing the received
1317 datagram.
1318 @param RxData Pointer to the EFI_UDP4_RECEIVE_DATA of this
1319 datagram.
1320
1321 @return The times this datagram is enqueued.
1322
1323 **/
1324 STATIC
1325 UINTN
1326 Udp4EnqueueDgram (
1327 IN UDP4_SERVICE_DATA *Udp4Service,
1328 IN NET_BUF *Packet,
1329 IN EFI_UDP4_RECEIVE_DATA *RxData
1330 )
1331 {
1332 LIST_ENTRY *Entry;
1333 UDP4_INSTANCE_DATA *Instance;
1334 UDP4_RXDATA_WRAP *Wrap;
1335 UINTN Enqueued;
1336
1337 Enqueued = 0;
1338
1339 NET_LIST_FOR_EACH (Entry, &Udp4Service->ChildrenList) {
1340 //
1341 // Iterate the instances.
1342 //
1343 Instance = NET_LIST_USER_STRUCT (Entry, UDP4_INSTANCE_DATA, Link);
1344
1345 if (!Instance->Configured) {
1346 continue;
1347 }
1348
1349 if (Udp4MatchDgram (Instance, &RxData->UdpSession)) {
1350 //
1351 // Wrap the RxData and put this Wrap into the instances RcvdDgramQue.
1352 //
1353 Wrap = Udp4WrapRxData (Instance, Packet, RxData);
1354 if (Wrap == NULL) {
1355 continue;
1356 }
1357
1358 NET_GET_REF (Packet);
1359
1360 InsertTailList (&Instance->RcvdDgramQue, &Wrap->Link);
1361
1362 Enqueued++;
1363 }
1364 }
1365
1366 return Enqueued;
1367 }
1368
1369
1370 /**
1371 This function delivers the received datagrams for the specified instance.
1372
1373 @param Instance Pointer to the instance context data.
1374
1375 @return None.
1376
1377 **/
1378 VOID
1379 Udp4InstanceDeliverDgram (
1380 IN UDP4_INSTANCE_DATA *Instance
1381 )
1382 {
1383 UDP4_RXDATA_WRAP *Wrap;
1384 EFI_UDP4_COMPLETION_TOKEN *Token;
1385 NET_BUF *Dup;
1386 EFI_UDP4_RECEIVE_DATA *RxData;
1387 EFI_TPL OldTpl;
1388
1389 if (!IsListEmpty (&Instance->RcvdDgramQue) &&
1390 !NetMapIsEmpty (&Instance->RxTokens)) {
1391
1392 Wrap = NET_LIST_HEAD (&Instance->RcvdDgramQue, UDP4_RXDATA_WRAP, Link);
1393
1394 if (NET_BUF_SHARED (Wrap->Packet)) {
1395 //
1396 // Duplicate the Packet if it is shared between instances.
1397 //
1398 Dup = NetbufDuplicate (Wrap->Packet, NULL, 0);
1399 if (Dup == NULL) {
1400 return;
1401 }
1402
1403 NetbufFree (Wrap->Packet);
1404
1405 Wrap->Packet = Dup;
1406 }
1407
1408 NetListRemoveHead (&Instance->RcvdDgramQue);
1409
1410 Token = (EFI_UDP4_COMPLETION_TOKEN *) NetMapRemoveHead (&Instance->RxTokens, NULL);
1411
1412 //
1413 // Build the FragmentTable and set the FragmentCount in RxData.
1414 //
1415 RxData = &Wrap->RxData;
1416 RxData->FragmentCount = Wrap->Packet->BlockOpNum;
1417
1418 NetbufBuildExt (
1419 Wrap->Packet,
1420 (NET_FRAGMENT *) RxData->FragmentTable,
1421 &RxData->FragmentCount
1422 );
1423
1424 Token->Status = EFI_SUCCESS;
1425 Token->Packet.RxData = &Wrap->RxData;
1426
1427 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
1428 InsertTailList (&Instance->DeliveredDgramQue, &Wrap->Link);
1429 gBS->RestoreTPL (OldTpl);
1430
1431 gBS->SignalEvent (Token->Event);
1432 }
1433 }
1434
1435
1436 /**
1437 This function delivers the datagrams enqueued in the instances.
1438
1439 @param Udp4Service Pointer to the udp service context data.
1440
1441 @return None.
1442
1443 **/
1444 STATIC
1445 VOID
1446 Udp4DeliverDgram (
1447 IN UDP4_SERVICE_DATA *Udp4Service
1448 )
1449 {
1450 LIST_ENTRY *Entry;
1451 UDP4_INSTANCE_DATA *Instance;
1452
1453 NET_LIST_FOR_EACH (Entry, &Udp4Service->ChildrenList) {
1454 //
1455 // Iterate the instances.
1456 //
1457 Instance = NET_LIST_USER_STRUCT (Entry, UDP4_INSTANCE_DATA, Link);
1458
1459 if (!Instance->Configured) {
1460 continue;
1461 }
1462
1463 //
1464 // Deliver the datagrams of this instance.
1465 //
1466 Udp4InstanceDeliverDgram (Instance);
1467 }
1468 }
1469
1470
1471 /**
1472 This function demultiplexes the received udp datagram to the apropriate instances.
1473
1474 @param Udp4Service Pointer to the udp service context data.
1475 @param NetSession Pointer to the EFI_NET_SESSION_DATA abstrated from
1476 the received datagram.
1477 @param Packet Pointer to the buffer containing the received udp
1478 datagram.
1479
1480 @return None.
1481
1482 **/
1483 STATIC
1484 VOID
1485 Udp4Demultiplex (
1486 IN UDP4_SERVICE_DATA *Udp4Service,
1487 IN EFI_NET_SESSION_DATA *NetSession,
1488 IN NET_BUF *Packet
1489 )
1490 {
1491 EFI_UDP4_HEADER *Udp4Header;
1492 UINT16 HeadSum;
1493 EFI_UDP4_RECEIVE_DATA RxData;
1494 EFI_UDP4_SESSION_DATA *Udp4Session;
1495 UINTN Enqueued;
1496
1497 //
1498 // Get the datagram header from the packet buffer.
1499 //
1500 Udp4Header = (EFI_UDP4_HEADER *) NetbufGetByte (Packet, 0, NULL);
1501
1502 if (Udp4Header->Checksum != 0) {
1503 //
1504 // check the checksum.
1505 //
1506 HeadSum = NetPseudoHeadChecksum (
1507 NetSession->Source,
1508 NetSession->Dest,
1509 EFI_IP_PROTO_UDP,
1510 0
1511 );
1512
1513 if (Udp4Checksum (Packet, HeadSum) != 0) {
1514 //
1515 // Wrong checksum.
1516 //
1517 return;
1518 }
1519 }
1520
1521 gRT->GetTime (&RxData.TimeStamp, NULL);
1522
1523 Udp4Session = &RxData.UdpSession;
1524 Udp4Session->SourcePort = NTOHS (Udp4Header->SrcPort);
1525 Udp4Session->DestinationPort = NTOHS (Udp4Header->DstPort);
1526
1527 CopyMem (&Udp4Session->SourceAddress, &NetSession->Source, sizeof (EFI_IPv4_ADDRESS));
1528 CopyMem (&Udp4Session->DestinationAddress, &NetSession->Dest, sizeof (EFI_IPv4_ADDRESS));
1529
1530 //
1531 // Trim the UDP header.
1532 //
1533 NetbufTrim (Packet, UDP4_HEADER_SIZE, TRUE);
1534
1535 RxData.DataLength = (UINT32) Packet->TotalSize;
1536
1537 //
1538 // Try to enqueue this datagram into the instances.
1539 //
1540 Enqueued = Udp4EnqueueDgram (Udp4Service, Packet, &RxData);
1541
1542 if (Enqueued == 0) {
1543 //
1544 // Send the port unreachable ICMP packet before we free this NET_BUF
1545 //
1546 Udp4SendPortUnreach (Udp4Service->IpIo, NetSession, Udp4Header);
1547 }
1548
1549 //
1550 // Try to free the packet before deliver it.
1551 //
1552 NetbufFree (Packet);
1553
1554 if (Enqueued > 0) {
1555 //
1556 // Deliver the datagram.
1557 //
1558 Udp4DeliverDgram (Udp4Service);
1559 }
1560 }
1561
1562
1563 /**
1564 This function builds and sends out a icmp port unreachable message.
1565
1566 @param IpIo Pointer to the IP_IO instance.
1567 @param NetSession Pointer to the EFI_NET_SESSION_DATA of the packet
1568 causes this icmp error message.
1569 @param Udp4Header Pointer to the udp header of the datagram causes
1570 this icmp error message.
1571
1572 @return None.
1573
1574 **/
1575 STATIC
1576 VOID
1577 Udp4SendPortUnreach (
1578 IN IP_IO *IpIo,
1579 IN EFI_NET_SESSION_DATA *NetSession,
1580 IN VOID *Udp4Header
1581 )
1582 {
1583 NET_BUF *Packet;
1584 UINT32 Len;
1585 IP4_ICMP_ERROR_HEAD *IcmpErrHdr;
1586 EFI_IP4_HEADER *IpHdr;
1587 UINT8 *Ptr;
1588 IP_IO_OVERRIDE Override;
1589 IP_IO_IP_INFO *IpSender;
1590
1591 IpSender = IpIoFindSender (&IpIo, NetSession->Dest);
1592 if (IpSender == NULL) {
1593 //
1594 // No apropriate sender, since we cannot send out the ICMP message through
1595 // the default zero station address IP instance, abort.
1596 //
1597 return;
1598 }
1599
1600 IpHdr = NetSession->IpHdr;
1601
1602 //
1603 // Calculate the requried length of the icmp error message.
1604 //
1605 Len = sizeof (IP4_ICMP_ERROR_HEAD) + (EFI_IP4_HEADER_LEN (IpHdr) -
1606 sizeof (IP4_HEAD)) + ICMP_ERROR_PACKET_LENGTH;
1607
1608 //
1609 // Allocate buffer for the icmp error message.
1610 //
1611 Packet = NetbufAlloc (Len);
1612 if (Packet == NULL) {
1613 return;
1614 }
1615
1616 //
1617 // Allocate space for the IP4_ICMP_ERROR_HEAD.
1618 //
1619 IcmpErrHdr = (IP4_ICMP_ERROR_HEAD *) NetbufAllocSpace (Packet, Len, FALSE);
1620
1621 //
1622 // Set the required fields for the icmp port unreachable message.
1623 //
1624 IcmpErrHdr->Head.Type = ICMP_TYPE_UNREACH;
1625 IcmpErrHdr->Head.Code = ICMP_CODE_UNREACH_PORT;
1626 IcmpErrHdr->Head.Checksum = 0;
1627 IcmpErrHdr->Fourth = 0;
1628
1629 //
1630 // Copy the IP header of the datagram tragged the error.
1631 //
1632 CopyMem (&IcmpErrHdr->IpHead, IpHdr, EFI_IP4_HEADER_LEN (IpHdr));
1633
1634 //
1635 // Copy the UDP header.
1636 //
1637 Ptr = (UINT8 *) &IcmpErrHdr->IpHead + EFI_IP4_HEADER_LEN (IpHdr);
1638 CopyMem (Ptr, Udp4Header, ICMP_ERROR_PACKET_LENGTH);
1639
1640 //
1641 // Calculate the checksum.
1642 //
1643 IcmpErrHdr->Head.Checksum = (UINT16) ~(NetbufChecksum (Packet));
1644
1645 //
1646 // Fill the override data.
1647 //
1648 Override.DoNotFragment = FALSE;
1649 Override.TypeOfService = 0;
1650 Override.TimeToLive = 255;
1651 Override.Protocol = EFI_IP_PROTO_ICMP;
1652
1653 CopyMem (&Override.SourceAddress, &NetSession->Dest, sizeof (EFI_IPv4_ADDRESS));
1654 ZeroMem (&Override.GatewayAddress, sizeof (EFI_IPv4_ADDRESS));
1655
1656 //
1657 // Send out this icmp packet.
1658 //
1659 IpIoSend (IpIo, Packet, IpSender, NULL, NULL, NetSession->Source, &Override);
1660
1661 NetbufFree (Packet);
1662 }
1663
1664
1665 /**
1666 This function handles the received Icmp Error message and demultiplexes it to the
1667 instance.
1668
1669 @param Udp4Service Pointer to the udp service context data.
1670 @param IcmpError The icmp error code.
1671 @param NetSession Pointer to the EFI_NET_SESSION_DATA abstracted
1672 from the received Icmp Error packet.
1673 @param Packet Pointer to the Icmp Error packet.
1674
1675 @return None.
1676
1677 **/
1678 STATIC
1679 VOID
1680 Udp4IcmpHandler (
1681 IN UDP4_SERVICE_DATA *Udp4Service,
1682 IN ICMP_ERROR IcmpError,
1683 IN EFI_NET_SESSION_DATA *NetSession,
1684 IN NET_BUF *Packet
1685 )
1686 {
1687 EFI_UDP4_HEADER *Udp4Header;
1688 EFI_UDP4_SESSION_DATA Udp4Session;
1689 LIST_ENTRY *Entry;
1690 UDP4_INSTANCE_DATA *Instance;
1691
1692 Udp4Header = (EFI_UDP4_HEADER *) NetbufGetByte (Packet, 0, NULL);
1693
1694 CopyMem (&Udp4Session.SourceAddress, &NetSession->Source, sizeof (EFI_IPv4_ADDRESS));
1695 CopyMem (&Udp4Session.DestinationAddress, &NetSession->Dest, sizeof (EFI_IPv4_ADDRESS));
1696
1697 Udp4Session.SourcePort = NTOHS (Udp4Header->DstPort);
1698 Udp4Session.DestinationPort = NTOHS (Udp4Header->SrcPort);
1699
1700 NET_LIST_FOR_EACH (Entry, &Udp4Service->ChildrenList) {
1701 //
1702 // Iterate all the instances.
1703 //
1704 Instance = NET_LIST_USER_STRUCT (Entry, UDP4_INSTANCE_DATA, Link);
1705
1706 if (!Instance->Configured ||
1707 Instance->ConfigData.AcceptPromiscuous ||
1708 Instance->ConfigData.AcceptAnyPort ||
1709 EFI_IP4_EQUAL (&Instance->ConfigData.StationAddress, &mZeroIp4Addr)) {
1710 //
1711 // Don't try to deliver the ICMP error to this instance if it is not configured,
1712 // or it's configured to be promiscuous or accept any port or accept all the
1713 // datagrams.
1714 //
1715 continue;
1716 }
1717
1718 if (Udp4MatchDgram (Instance, &Udp4Session)) {
1719 //
1720 // Translate the Icmp Error code according to the udp spec.
1721 //
1722 Instance->IcmpError = IpIoGetIcmpErrStatus (IcmpError, NULL, NULL);
1723
1724 if (IcmpError > ICMP_ERR_UNREACH_PORT) {
1725 Instance->IcmpError = EFI_ICMP_ERROR;
1726 }
1727
1728 //
1729 // Notify the instance with the received Icmp Error.
1730 //
1731 Udp4ReportIcmpError (Instance);
1732
1733 break;
1734 }
1735 }
1736
1737 NetbufFree (Packet);
1738 }
1739
1740
1741 /**
1742 This function reports the received ICMP error.
1743
1744 @param Instance Pointer to the udp instance context data.
1745
1746 @return None.
1747
1748 **/
1749 VOID
1750 Udp4ReportIcmpError (
1751 IN UDP4_INSTANCE_DATA *Instance
1752 )
1753 {
1754 EFI_UDP4_COMPLETION_TOKEN *Token;
1755
1756 if (NetMapIsEmpty (&Instance->RxTokens)) {
1757 //
1758 // There are no receive tokens to deliver the ICMP error.
1759 //
1760 return;
1761 }
1762
1763 if (EFI_ERROR (Instance->IcmpError)) {
1764 //
1765 // Try to get a RxToken from the RxTokens map.
1766 //
1767 Token = (EFI_UDP4_COMPLETION_TOKEN *) NetMapRemoveHead (&Instance->RxTokens, NULL);
1768
1769 if (Token != NULL) {
1770 //
1771 // Report the error through the Token.
1772 //
1773 Token->Status = Instance->IcmpError;
1774 gBS->SignalEvent (Token->Event);
1775
1776 //
1777 // Clear the IcmpError.
1778 //
1779 Instance->IcmpError = EFI_SUCCESS;
1780 }
1781 }
1782 }
1783
1784
1785 /**
1786 This function is a dummy ext-free function for the NET_BUF created for the output
1787 udp datagram.
1788
1789 @param Context Pointer to the context data.
1790
1791 @return None.
1792
1793 **/
1794 VOID
1795 Udp4NetVectorExtFree (
1796 VOID *Context
1797 )
1798 {
1799 }
1800
1801
1802 /**
1803 Set the Udp4 variable data.
1804
1805 @param Udp4Service Udp4 service data.
1806
1807 @retval EFI_OUT_OF_RESOURCES There are not enough resources to set the
1808 variable.
1809 @retval other Set variable failed.
1810
1811 **/
1812 EFI_STATUS
1813 Udp4SetVariableData (
1814 IN UDP4_SERVICE_DATA *Udp4Service
1815 )
1816 {
1817 UINT32 NumConfiguredInstance;
1818 LIST_ENTRY *Entry;
1819 UINTN VariableDataSize;
1820 EFI_UDP4_VARIABLE_DATA *Udp4VariableData;
1821 EFI_UDP4_SERVICE_POINT *Udp4ServicePoint;
1822 UDP4_INSTANCE_DATA *Udp4Instance;
1823 CHAR16 *NewMacString;
1824 EFI_STATUS Status;
1825
1826 NumConfiguredInstance = 0;
1827
1828 //
1829 // Go through the children list to count the configured children.
1830 //
1831 NET_LIST_FOR_EACH (Entry, &Udp4Service->ChildrenList) {
1832 Udp4Instance = NET_LIST_USER_STRUCT_S (
1833 Entry,
1834 UDP4_INSTANCE_DATA,
1835 Link,
1836 UDP4_INSTANCE_DATA_SIGNATURE
1837 );
1838
1839 if (Udp4Instance->Configured) {
1840 NumConfiguredInstance++;
1841 }
1842 }
1843
1844 //
1845 // Calculate the size of the Udp4VariableData. As there may be no Udp4 child,
1846 // we should add extra buffer for the service points only if the number of configured
1847 // children is more than 1.
1848 //
1849 VariableDataSize = sizeof (EFI_UDP4_VARIABLE_DATA);
1850
1851 if (NumConfiguredInstance > 1) {
1852 VariableDataSize += sizeof (EFI_UDP4_SERVICE_POINT) * (NumConfiguredInstance - 1);
1853 }
1854
1855 Udp4VariableData = AllocatePool (VariableDataSize);
1856 if (Udp4VariableData == NULL) {
1857 return EFI_OUT_OF_RESOURCES;
1858 }
1859
1860 Udp4VariableData->DriverHandle = Udp4Service->ImageHandle;
1861 Udp4VariableData->ServiceCount = NumConfiguredInstance;
1862
1863 Udp4ServicePoint = &Udp4VariableData->Services[0];
1864
1865 //
1866 // Go through the children list to fill the configured children's address pairs.
1867 //
1868 NET_LIST_FOR_EACH (Entry, &Udp4Service->ChildrenList) {
1869 Udp4Instance = NET_LIST_USER_STRUCT_S (
1870 Entry,
1871 UDP4_INSTANCE_DATA,
1872 Link,
1873 UDP4_INSTANCE_DATA_SIGNATURE
1874 );
1875
1876 if (Udp4Instance->Configured) {
1877 Udp4ServicePoint->InstanceHandle = Udp4Instance->ChildHandle;
1878 Udp4ServicePoint->LocalAddress = Udp4Instance->ConfigData.StationAddress;
1879 Udp4ServicePoint->LocalPort = Udp4Instance->ConfigData.StationPort;
1880 Udp4ServicePoint->RemoteAddress = Udp4Instance->ConfigData.RemoteAddress;
1881 Udp4ServicePoint->RemotePort = Udp4Instance->ConfigData.RemotePort;
1882
1883 Udp4ServicePoint++;
1884 }
1885 }
1886
1887 //
1888 // Get the mac string.
1889 //
1890 Status = NetLibGetMacString (
1891 Udp4Service->ControllerHandle,
1892 Udp4Service->ImageHandle,
1893 &NewMacString
1894 );
1895 if (EFI_ERROR (Status)) {
1896 goto ON_ERROR;
1897 }
1898
1899 if (Udp4Service->MacString != NULL) {
1900 //
1901 // The variable is set already, we're going to update it.
1902 //
1903 if (StrCmp (Udp4Service->MacString, NewMacString) != 0) {
1904 //
1905 // The mac address is changed, delete the previous variable first.
1906 //
1907 gRT->SetVariable (
1908 Udp4Service->MacString,
1909 &gEfiUdp4ServiceBindingProtocolGuid,
1910 EFI_VARIABLE_BOOTSERVICE_ACCESS,
1911 0,
1912 NULL
1913 );
1914 }
1915
1916 gBS->FreePool (Udp4Service->MacString);
1917 }
1918
1919 Udp4Service->MacString = NewMacString;
1920
1921 Status = gRT->SetVariable (
1922 Udp4Service->MacString,
1923 &gEfiUdp4ServiceBindingProtocolGuid,
1924 EFI_VARIABLE_BOOTSERVICE_ACCESS,
1925 VariableDataSize,
1926 (VOID *) Udp4VariableData
1927 );
1928
1929 ON_ERROR:
1930
1931 gBS->FreePool (Udp4VariableData);
1932
1933 return Status;
1934 }
1935
1936
1937 /**
1938 Clear the variable and free the resource.
1939
1940 @param Udp4Service Udp4 service data.
1941
1942 @return None.
1943
1944 **/
1945 VOID
1946 Udp4ClearVariableData (
1947 IN UDP4_SERVICE_DATA *Udp4Service
1948 )
1949 {
1950 ASSERT (Udp4Service->MacString != NULL);
1951
1952 gRT->SetVariable (
1953 Udp4Service->MacString,
1954 &gEfiUdp4ServiceBindingProtocolGuid,
1955 EFI_VARIABLE_BOOTSERVICE_ACCESS,
1956 0,
1957 NULL
1958 );
1959
1960 gBS->FreePool (Udp4Service->MacString);
1961 Udp4Service->MacString = NULL;
1962 }