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