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