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