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