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