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