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