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