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