VirtualBox

source: vbox/trunk/src/VBox/Devices/EFI/FirmwareNew/NetworkPkg/Udp6Dxe/Udp6Impl.c@ 77662

Last change on this file since 77662 was 77662, checked in by vboxsync, 6 years ago

EFI: First step in UDK2018 merge. Does not build yet.

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette