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