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