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