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