]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Network/Udp4Dxe/Udp4Impl.c
Import SnpDxe, Tcp4Dxe, Udp4Dxe and MnpDxe.
[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
175 OpenData.IpConfigData = mIpIoDefaultIpConfigData;\r
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
195 NET_TPL_FAST_TIMER,\r
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
362 Instance->Udp4Proto = mUdp4Protocol;\r
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
427 if (EFI_IP_EQUAL (ConfigData->StationAddress, *Address) &&\r
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
569 (!EFI_IP_EQUAL (NewConfigData->StationAddress, OldConfigData->StationAddress) ||\r
570 !EFI_IP_EQUAL (NewConfigData->SubnetMask, OldConfigData->SubnetMask))) {\r
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
579 if (!EFI_IP_EQUAL (NewConfigData->RemoteAddress, OldConfigData->RemoteAddress)) {\r
580 //\r
581 // The remoteaddress is not the same.\r
582 //\r
583 return FALSE;\r
584 }\r
585\r
586 if ((EFI_IP4 (NewConfigData->RemoteAddress) != 0) &&\r
587 (NewConfigData->RemotePort != OldConfigData->RemotePort)) {\r
588 //\r
589 // The RemotePort differs if it's designated in the configdata.\r
590 //\r
591 return FALSE;\r
592 }\r
593\r
594 //\r
595 // All checks pass, return TRUE.\r
596 //\r
597 return TRUE;\r
598}\r
599\r
600\r
601/**\r
602 This function builds the Ip4 configdata from the Udp4ConfigData.\r
603\r
604 @param Udp4ConfigData Pointer to the EFI_UDP4_CONFIG_DATA.\r
605 @param Ip4ConfigData Pointer to the EFI_IP4_CONFIG_DATA.\r
606\r
607 @return None.\r
608\r
609**/\r
610VOID\r
611Udp4BuildIp4ConfigData (\r
612 IN EFI_UDP4_CONFIG_DATA *Udp4ConfigData,\r
613 IN EFI_IP4_CONFIG_DATA *Ip4ConfigData\r
614 )\r
615{\r
616 *Ip4ConfigData = mIpIoDefaultIpConfigData;\r
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
621 Ip4ConfigData->StationAddress = Udp4ConfigData->StationAddress;\r
622 Ip4ConfigData->SubnetMask = Udp4ConfigData->SubnetMask;\r
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
669\r
670 if (TxToken->Event == NULL) {\r
671 return EFI_INVALID_PARAMETER;\r
672 }\r
673\r
674 TxData = TxToken->Packet.TxData;\r
675\r
676 if ((TxData == NULL) || (TxData->FragmentCount == 0)) {\r
677 return EFI_INVALID_PARAMETER;\r
678 }\r
679\r
680 TotalLen = 0;\r
681 for (Index = 0; Index < TxData->FragmentCount; Index++) {\r
682\r
683 if ((TxData->FragmentTable[Index].FragmentBuffer == NULL) ||\r
684 (TxData->FragmentTable[Index].FragmentLength == 0)) {\r
685 //\r
686 // if the FragmentBuffer is NULL or the FragmentLeng is zero.\r
687 //\r
688 return EFI_INVALID_PARAMETER;\r
689 }\r
690\r
691 TotalLen += TxData->FragmentTable[Index].FragmentLength;\r
692 }\r
693\r
694 if (TotalLen != TxData->DataLength) {\r
695 //\r
696 // The TotalLen calculated by adding all the FragmentLeng doesn't equal to the\r
697 // DataLength.\r
698 //\r
699 return EFI_INVALID_PARAMETER;\r
700 }\r
701\r
702 if ((TxData->GatewayAddress != NULL) &&\r
703 !Ip4IsUnicast(EFI_NTOHL (*(TxData->GatewayAddress)), 0)) {\r
704 //\r
705 // The specified GatewayAddress is not a unicast IPv4 address while it's not 0.\r
706 //\r
707 return EFI_INVALID_PARAMETER;\r
708 }\r
709\r
710 ConfigData = &Instance->ConfigData;\r
711 UdpSessionData = TxData->UdpSessionData;\r
712\r
713 if (UdpSessionData != NULL) {\r
714\r
715 SourceAddress = EFI_NTOHL (UdpSessionData->SourceAddress);\r
716\r
717 if ((SourceAddress != 0) && !Ip4IsUnicast (SourceAddress, 0)) {\r
718 //\r
719 // Check whether SourceAddress is a valid IPv4 address in case it's not zero.\r
720 // The configured station address is used if SourceAddress is zero.\r
721 //\r
722 return EFI_INVALID_PARAMETER;\r
723 }\r
724\r
725 if ((UdpSessionData->DestinationPort == 0) && (ConfigData->RemotePort == 0)) {\r
726 //\r
727 // Ambiguous, no avalaible DestinationPort for this token.\r
728 //\r
729 return EFI_INVALID_PARAMETER;\r
730 }\r
731\r
732 if (EFI_IP4 (UdpSessionData->DestinationAddress) == 0) {\r
733 //\r
734 // The DestinationAddress specified in the UdpSessionData is 0.\r
735 //\r
736 return EFI_INVALID_PARAMETER;\r
737 }\r
738 } else if (EFI_IP4 (ConfigData->RemoteAddress) == 0) {\r
739 //\r
740 // the configured RemoteAddress is all zero, and the user doens't override the\r
741 // destination address.\r
742 //\r
743 return EFI_INVALID_PARAMETER;\r
744 }\r
745\r
746 if (TxData->DataLength > UDP4_MAX_DATA_SIZE) {\r
747 return EFI_BAD_BUFFER_SIZE;\r
748 }\r
749\r
750 return EFI_SUCCESS;\r
751}\r
752\r
753\r
754/**\r
755 This function checks whether the specified Token duplicates with the one in the Map.\r
756\r
757 @param Map Pointer to the NET_MAP.\r
758 @param Item Pointer to the NET_MAP_ITEM contain the pointer to\r
759 the Token.\r
760 @param Context Pointer to the Token to be checked.\r
761\r
762 @retval EFI_SUCCESS The Token specified by Context differs from the\r
763 one in the Item.\r
764 @retval EFI_ACCESS_DENIED The Token duplicates with the one in the Item.\r
765\r
766**/\r
767EFI_STATUS\r
768Udp4TokenExist (\r
769 IN NET_MAP *Map,\r
770 IN NET_MAP_ITEM *Item,\r
771 IN VOID *Context\r
772 )\r
773{\r
774 EFI_UDP4_COMPLETION_TOKEN *Token;\r
775 EFI_UDP4_COMPLETION_TOKEN *TokenInItem;\r
776\r
777 Token = (EFI_UDP4_COMPLETION_TOKEN*) Context;\r
778 TokenInItem = (EFI_UDP4_COMPLETION_TOKEN*) Item->Key;\r
779\r
780 if ((Token == TokenInItem) || (Token->Event == TokenInItem->Event)) {\r
781 //\r
782 // The Token duplicates with the TokenInItem in case either the two pointers are the\r
783 // same or the Events of these two tokens are the same.\r
784 //\r
785 return EFI_ACCESS_DENIED;\r
786 }\r
787\r
788 return EFI_SUCCESS;\r
789}\r
790\r
791\r
792/**\r
793 This function calculates the checksum for the Packet, utilizing the pre-calculated\r
794 pseudo HeadSum to reduce some overhead.\r
795\r
796 @param Packet Pointer to the NET_BUF contains the udp datagram.\r
797 @param HeadSum Checksum of the pseudo header execpt the length\r
798 field.\r
799\r
800 @return The 16-bit checksum of this udp datagram.\r
801\r
802**/\r
803UINT16\r
804Udp4Checksum (\r
805 IN NET_BUF *Packet,\r
806 IN UINT16 HeadSum\r
807 )\r
808{\r
809 UINT16 Checksum;\r
810\r
811 Checksum = NetbufChecksum (Packet);\r
812 Checksum = NetAddChecksum (Checksum, HeadSum);\r
813\r
814 Checksum = NetAddChecksum (Checksum, HTONS ((UINT16) Packet->TotalSize));\r
815\r
816 return ~Checksum;\r
817}\r
818\r
819\r
820/**\r
821 This function removes the specified Token from the TokenMap.\r
822\r
823 @param TokenMap Pointer to the NET_MAP containing the tokens.\r
824 @param Token Pointer to the Token to be removed.\r
825\r
826 @retval EFI_SUCCESS The specified Token is removed from the TokenMap.\r
827 @retval EFI_NOT_FOUND The specified Token is not found in the TokenMap.\r
828\r
829**/\r
830EFI_STATUS\r
831Udp4RemoveToken (\r
832 IN NET_MAP *TokenMap,\r
833 IN EFI_UDP4_COMPLETION_TOKEN *Token\r
834 )\r
835{\r
836 NET_MAP_ITEM *Item;\r
837\r
838 //\r
839 // Find the Token first.\r
840 //\r
841 Item = NetMapFindKey (TokenMap, (VOID *) Token);\r
842\r
843 if (Item != NULL) {\r
844 //\r
845 // Remove the token if it's found in the map.\r
846 //\r
847 NetMapRemoveItem (TokenMap, Item, NULL);\r
848\r
849 return EFI_SUCCESS;\r
850 }\r
851\r
852 return EFI_NOT_FOUND;\r
853}\r
854\r
855\r
856/**\r
857 This function is the packet transmitting notify function registered to the IpIo\r
858 interface. It's called to signal the udp TxToken when IpIo layer completes the\r
859 transmitting of the udp datagram.\r
860\r
861 @param Status The completion status of the output udp datagram.\r
862 @param Context Pointer to the context data.\r
863 @param Sender Pointer to the Ip sender of the udp datagram.\r
864 @param NotifyData Pointer to the notify data.\r
865\r
866 @return None.\r
867\r
868**/\r
869STATIC\r
870VOID\r
871Udp4DgramSent (\r
872 IN EFI_STATUS Status,\r
873 IN VOID *Context,\r
874 IN VOID *Sender,\r
875 IN VOID *NotifyData\r
876 )\r
877{\r
878 UDP4_INSTANCE_DATA *Instance;\r
879 EFI_UDP4_COMPLETION_TOKEN *Token;\r
880\r
881 Instance = (UDP4_INSTANCE_DATA *) Context;\r
882 Token = (EFI_UDP4_COMPLETION_TOKEN *) NotifyData;\r
883\r
884 if (Udp4RemoveToken (&Instance->TxTokens, Token) == EFI_SUCCESS) {\r
885 //\r
886 // The token may be cancelled. Only signal it if the remove operation succeeds.\r
887 //\r
888 Token->Status = Status;\r
889 gBS->SignalEvent (Token->Event);\r
890 }\r
891}\r
892\r
893\r
894/**\r
895 This function processes the received datagram passed up by the IpIo layer.\r
896\r
897 @param Status The status of this udp datagram.\r
898 @param IcmpError The IcmpError code, only available when Status is\r
899 EFI_ICMP_ERROR.\r
900 @param NetSession Pointer to the EFI_NET_SESSION_DATA.\r
901 @param Packet Pointer to the NET_BUF containing the received udp\r
902 datagram.\r
903 @param Context Pointer to the context data.\r
904\r
905 @return None.\r
906\r
907**/\r
908STATIC\r
909VOID\r
910Udp4DgramRcvd (\r
911 IN EFI_STATUS Status,\r
912 IN ICMP_ERROR IcmpError,\r
913 IN EFI_NET_SESSION_DATA *NetSession,\r
914 IN NET_BUF *Packet,\r
915 IN VOID *Context\r
916 )\r
917{\r
918 NET_CHECK_SIGNATURE (Packet, NET_BUF_SIGNATURE);\r
919\r
920 //\r
921 // IpIo only passes received packets with Status EFI_SUCCESS or EFI_ICMP_ERROR.\r
922 //\r
923 if (Status == EFI_SUCCESS) {\r
924 //\r
925 // Demultiplex the received datagram.\r
926 //\r
927 Udp4Demultiplex ((UDP4_SERVICE_DATA *) Context, NetSession, Packet);\r
928 } else {\r
929 //\r
930 // Handle the ICMP_ERROR packet.\r
931 //\r
932 Udp4IcmpHandler ((UDP4_SERVICE_DATA *) Context, IcmpError, NetSession, Packet);\r
933 }\r
934}\r
935\r
936\r
937/**\r
938 This function removes the multicast group specified by Arg from the Map.\r
939\r
940 @param Map Pointer to the NET_MAP.\r
941 @param Item Pointer to the NET_MAP_ITEM.\r
942 @param Arg Pointer to the Arg, it's the pointer to a\r
943 multicast IPv4 Address.\r
944\r
945 @retval EFI_SUCCESS The multicast address is removed.\r
946 @retval EFI_ABORTED The specified multicast address is removed and the\r
947 Arg is not NULL.\r
948\r
949**/\r
950EFI_STATUS\r
951Udp4LeaveGroup (\r
952 IN NET_MAP *Map,\r
953 IN NET_MAP_ITEM *Item,\r
954 IN VOID *Arg OPTIONAL\r
955 )\r
956{\r
957 EFI_IPv4_ADDRESS *McastIp;\r
958\r
959 McastIp = Arg;\r
960\r
961 if ((McastIp != NULL) && ((UINTN) EFI_IP4 (*McastIp) != (UINTN) (Item->Key))) {\r
962 //\r
963 // McastIp is not NULL and the multicast address contained in the Item\r
964 // is not the same as McastIp.\r
965 //\r
966 return EFI_SUCCESS;\r
967 }\r
968\r
969 //\r
970 // Remove this Item.\r
971 //\r
972 NetMapRemoveItem (Map, Item, NULL);\r
973\r
974 if (McastIp != NULL) {\r
975 //\r
976 // Return EFI_ABORTED in case McastIp is not NULL to terminate the iteration.\r
977 //\r
978 return EFI_ABORTED;\r
979 }\r
980\r
981 return EFI_SUCCESS;\r
982}\r
983\r
984\r
985/**\r
986 This function cancle the token specified by Arg in the Map.\r
987\r
988 @param Map Pointer to the NET_MAP.\r
989 @param Item Pointer to the NET_MAP_ITEM.\r
990 @param Arg Pointer to the token to be cancelled, if NULL, all\r
991 the tokens in this Map will be cancelled.\r
992\r
993 @retval EFI_SUCCESS The token is cancelled if Arg is NULL or the token\r
994 is not the same as that in the Item if Arg is not\r
995 NULL.\r
996 @retval EFI_ABORTED Arg is not NULL, and the token specified by Arg is\r
997 cancelled.\r
998\r
999**/\r
1000STATIC\r
1001EFI_STATUS\r
1002Udp4CancelTokens (\r
1003 IN NET_MAP *Map,\r
1004 IN NET_MAP_ITEM *Item,\r
1005 IN VOID *Arg OPTIONAL\r
1006 )\r
1007{\r
1008 EFI_UDP4_COMPLETION_TOKEN *TokenToCancel;\r
1009 NET_BUF *Packet;\r
1010 IP_IO *IpIo;\r
1011\r
1012 if ((Arg != NULL) && (Item->Key != Arg)) {\r
1013 return EFI_SUCCESS;\r
1014 }\r
1015\r
1016 if (Item->Value != NULL) {\r
1017 //\r
1018 // If the token is a transmit token, the corresponding Packet is recorded in\r
1019 // Item->Value, invoke IpIo to cancel this packet first. The IpIoCancelTxToken\r
1020 // will invoke Udp4DgramSent, the token will be signaled and this Item will\r
1021 // be removed from the Map there.\r
1022 //\r
1023 Packet = (NET_BUF *) (Item->Value);\r
1024 IpIo = (IP_IO *) (*((UINTN *) &Packet->ProtoData[0]));\r
1025\r
1026 IpIoCancelTxToken (IpIo, Packet);\r
1027 } else {\r
1028 //\r
1029 // The token is a receive token. Abort it and remove it from the Map.\r
1030 //\r
1031 TokenToCancel = (EFI_UDP4_COMPLETION_TOKEN *) Item->Key;\r
1032\r
1033 TokenToCancel->Status = EFI_ABORTED;\r
1034 gBS->SignalEvent (TokenToCancel->Event);\r
1035\r
1036 NetMapRemoveItem (Map, Item, NULL);\r
1037 }\r
1038\r
1039 if (Arg != NULL) {\r
1040 return EFI_ABORTED;\r
1041 }\r
1042\r
1043 return EFI_SUCCESS;\r
1044}\r
1045\r
1046\r
1047/**\r
1048 This function removes all the Wrap datas in the RcvdDgramQue.\r
1049\r
1050 @param RcvdDgramQue Pointer to the list containing all the Wrap datas.\r
1051\r
1052 @return None.\r
1053\r
1054**/\r
1055VOID\r
1056Udp4FlushRxData (\r
1057 IN NET_LIST_ENTRY *RcvdDgramQue\r
1058 )\r
1059{\r
1060 UDP4_RXDATA_WRAP *Wrap;\r
1061 EFI_TPL OldTpl;\r
1062\r
1063 OldTpl = NET_RAISE_TPL (NET_TPL_RECYCLE);\r
1064\r
1065 while (!NetListIsEmpty (RcvdDgramQue)) {\r
1066 //\r
1067 // Iterate all the Wraps in the RcvdDgramQue.\r
1068 //\r
1069 Wrap = NET_LIST_HEAD (RcvdDgramQue, UDP4_RXDATA_WRAP, Link);\r
1070\r
1071 //\r
1072 // The Wrap will be removed from the RcvdDgramQue by this function call.\r
1073 //\r
1074 Udp4RecycleRxDataWrap (NULL, (VOID *) Wrap);\r
1075 }\r
1076\r
1077 NET_RESTORE_TPL (OldTpl);\r
1078}\r
1079\r
1080\r
1081\r
1082/**\r
1083\r
1084 @param Instance Pointer to the udp instance context data.\r
1085 @param Token Pointer to the token to be canceled, if NULL, all\r
1086 tokens in this instance will be cancelled.\r
1087\r
1088 @retval EFI_SUCCESS The Token is cancelled.\r
1089 @retval EFI_NOT_FOUND The Token is not found.\r
1090\r
1091**/\r
1092EFI_STATUS\r
1093Udp4InstanceCancelToken (\r
1094 IN UDP4_INSTANCE_DATA *Instance,\r
1095 IN EFI_UDP4_COMPLETION_TOKEN *Token OPTIONAL\r
1096 )\r
1097{\r
1098 EFI_STATUS Status;\r
1099\r
1100 //\r
1101 // Cancle this token from the TxTokens map.\r
1102 //\r
1103 Status = NetMapIterate (&Instance->TxTokens, Udp4CancelTokens, Token);\r
1104\r
1105 if ((Token != NULL) && (Status == EFI_ABORTED)) {\r
1106 //\r
1107 // If Token isn't NULL and Status is EFI_ABORTED, the token is cancelled from\r
1108 // the TxTokens, just return success.\r
1109 //\r
1110 return EFI_SUCCESS;\r
1111 }\r
1112\r
1113 //\r
1114 // Try to cancel this token from the RxTokens map in condition either the Token\r
1115 // is NULL or the specified Token is not in TxTokens.\r
1116 //\r
1117 Status = NetMapIterate (&Instance->RxTokens, Udp4CancelTokens, Token);\r
1118\r
1119 if ((Token != NULL) && (Status == EFI_SUCCESS)) {\r
1120 //\r
1121 // If Token isn't NULL and Status is EFI_SUCCESS, the token is neither in the\r
1122 // TxTokens nor the RxTokens, or say, it's not found.\r
1123 //\r
1124 return EFI_NOT_FOUND;\r
1125 }\r
1126\r
1127 ASSERT ((Token != NULL) || ((0 == NetMapGetCount (&Instance->TxTokens))\r
1128 && (0 == NetMapGetCount (&Instance->RxTokens))));\r
1129\r
1130 return EFI_SUCCESS;\r
1131}\r
1132\r
1133\r
1134/**\r
1135 This function matches the received udp datagram with the Instance.\r
1136\r
1137 @param Instance Pointer to the udp instance context data.\r
1138 @param Udp4Session Pointer to the EFI_UDP4_SESSION_DATA abstracted\r
1139 from the received udp datagram.\r
1140\r
1141 @return The udp datagram matches the receiving requirments of the Instance or not.\r
1142\r
1143**/\r
1144STATIC\r
1145BOOLEAN\r
1146Udp4MatchDgram (\r
1147 IN UDP4_INSTANCE_DATA *Instance,\r
1148 IN EFI_UDP4_SESSION_DATA *Udp4Session\r
1149 )\r
1150{\r
1151 EFI_UDP4_CONFIG_DATA *ConfigData;\r
1152 IP4_ADDR Destination;\r
1153\r
1154 ConfigData = &Instance->ConfigData;\r
1155\r
1156 if (ConfigData->AcceptPromiscuous) {\r
1157 //\r
1158 // Always matches if this instance is in the promiscuous state.\r
1159 //\r
1160 return TRUE;\r
1161 }\r
1162\r
1163 if ((!ConfigData->AcceptAnyPort && (Udp4Session->DestinationPort != ConfigData->StationPort)) ||\r
1164 ((ConfigData->RemotePort != 0) && (Udp4Session->SourcePort != ConfigData->RemotePort))) {\r
1165 //\r
1166 // The local port or the remote port doesn't match.\r
1167 //\r
1168 return FALSE;\r
1169 }\r
1170\r
1171 if ((EFI_IP4 (ConfigData->RemoteAddress) != 0) &&\r
1172 !EFI_IP_EQUAL (ConfigData->RemoteAddress, Udp4Session->SourceAddress)) {\r
1173 //\r
1174 // This datagram doesn't come from the instance's specified sender.\r
1175 //\r
1176 return FALSE;\r
1177 }\r
1178\r
1179 if ((EFI_IP4 (ConfigData->StationAddress) == 0) ||\r
1180 EFI_IP_EQUAL (Udp4Session->DestinationAddress, ConfigData->StationAddress)) {\r
1181 //\r
1182 // The instance is configured to receive datagrams destinated to any station IP or\r
1183 // the destination address of this datagram matches the configured station IP.\r
1184 //\r
1185 return TRUE;\r
1186 }\r
1187\r
1188 Destination = EFI_IP4 (Udp4Session->DestinationAddress);\r
1189\r
1190 if (IP4_IS_LOCAL_BROADCAST (Destination) && ConfigData->AcceptBroadcast) {\r
1191 //\r
1192 // The instance is configured to receive broadcast and this is a broadcast packet.\r
1193 //\r
1194 return TRUE;\r
1195 }\r
1196\r
1197 if (IP4_IS_MULTICAST (NTOHL (Destination)) &&\r
1198 (NULL != NetMapFindKey (&Instance->McastIps, (VOID *) (UINTN) Destination))) {\r
1199 //\r
1200 // It's a multicast packet and the multicast address is accepted by this instance.\r
1201 //\r
1202 return TRUE;\r
1203 }\r
1204\r
1205 return FALSE;\r
1206}\r
1207\r
1208\r
1209/**\r
1210 This function removes the Wrap specified by Context and release relevant resources.\r
1211\r
1212 @param Event The Event this notify function registered to.\r
1213 @param Context Pointer to the context data.\r
1214\r
1215 @return None.\r
1216\r
1217**/\r
1218STATIC\r
1219VOID\r
1220EFIAPI\r
1221Udp4RecycleRxDataWrap (\r
1222 IN EFI_EVENT Event,\r
1223 IN VOID *Context\r
1224 )\r
1225{\r
1226 UDP4_RXDATA_WRAP *Wrap;\r
1227\r
1228 Wrap = (UDP4_RXDATA_WRAP *) Context;\r
1229\r
1230 //\r
1231 // Remove the Wrap from the list it belongs to.\r
1232 //\r
1233 NetListRemoveEntry (&Wrap->Link);\r
1234\r
1235 //\r
1236 // Free the Packet associated with this Wrap.\r
1237 //\r
1238 NetbufFree (Wrap->Packet);\r
1239\r
1240 //\r
1241 // Close the event.\r
1242 //\r
1243 gBS->CloseEvent (Wrap->RxData.RecycleSignal);\r
1244\r
1245 NetFreePool (Wrap);\r
1246}\r
1247\r
1248\r
1249/**\r
1250 This function wraps the Packet and the RxData.\r
1251\r
1252 @param Instance Pointer to the instance context data.\r
1253 @param Packet Pointer to the buffer containing the received\r
1254 datagram.\r
1255 @param RxData Pointer to the EFI_UDP4_RECEIVE_DATA of this\r
1256 datagram.\r
1257\r
1258 @return Pointer to the structure wrapping the RxData and the Packet.\r
1259\r
1260**/\r
1261STATIC\r
1262UDP4_RXDATA_WRAP *\r
1263Udp4WrapRxData (\r
1264 IN UDP4_INSTANCE_DATA *Instance,\r
1265 IN NET_BUF *Packet,\r
1266 IN EFI_UDP4_RECEIVE_DATA *RxData\r
1267 )\r
1268{\r
1269 EFI_STATUS Status;\r
1270 UDP4_RXDATA_WRAP *Wrap;\r
1271\r
1272 //\r
1273 // Allocate buffer for the Wrap.\r
1274 //\r
1275 Wrap = NetAllocatePool (sizeof (UDP4_RXDATA_WRAP) +\r
1276 (Packet->BlockOpNum - 1) * sizeof (EFI_UDP4_FRAGMENT_DATA));\r
1277 if (Wrap == NULL) {\r
1278 return NULL;\r
1279 }\r
1280\r
1281 NetListInit (&Wrap->Link);\r
1282\r
1283 Wrap->RxData = *RxData;\r
1284\r
1285 //\r
1286 // Create the Recycle event.\r
1287 //\r
1288 Status = gBS->CreateEvent (\r
1289 EVT_NOTIFY_SIGNAL,\r
1290 NET_TPL_RECYCLE,\r
1291 Udp4RecycleRxDataWrap,\r
1292 Wrap,\r
1293 &Wrap->RxData.RecycleSignal\r
1294 );\r
1295 if (EFI_ERROR (Status)) {\r
1296 NetFreePool (Wrap);\r
1297 return NULL;\r
1298 }\r
1299\r
1300 Wrap->Packet = Packet;\r
1301 Wrap->TimeoutTick = Instance->ConfigData.ReceiveTimeout;\r
1302\r
1303 return Wrap;\r
1304}\r
1305\r
1306\r
1307/**\r
1308 This function enqueues the received datagram into the instances' receiving queues.\r
1309\r
1310 @param Udp4Service Pointer to the udp service context data.\r
1311 @param Packet Pointer to the buffer containing the received\r
1312 datagram.\r
1313 @param RxData Pointer to the EFI_UDP4_RECEIVE_DATA of this\r
1314 datagram.\r
1315\r
1316 @return The times this datagram is enqueued.\r
1317\r
1318**/\r
1319STATIC\r
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
1327 NET_LIST_ENTRY *Entry;\r
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
1348 Wrap = Udp4WrapRxData (Instance, Packet, RxData);\r
1349 if (Wrap == NULL) {\r
1350 continue;\r
1351 }\r
1352\r
1353 NET_GET_REF (Packet);\r
1354\r
1355 NetListInsertTail (&Instance->RcvdDgramQue, &Wrap->Link);\r
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
1382\r
1383 if (!NetListIsEmpty (&Instance->RcvdDgramQue) &&\r
1384 !NetMapIsEmpty (&Instance->RxTokens)) {\r
1385\r
1386 Wrap = NET_LIST_HEAD (&Instance->RcvdDgramQue, UDP4_RXDATA_WRAP, Link);\r
1387\r
1388 if (NET_BUF_SHARED (Wrap->Packet)) {\r
1389 //\r
1390 // Duplicate the Packet if it is shared between instances.\r
1391 //\r
1392 Dup = NetbufDuplicate (Wrap->Packet, NULL, 0);\r
1393 if (Dup == NULL) {\r
1394 return;\r
1395 }\r
1396\r
1397 NetbufFree (Wrap->Packet);\r
1398\r
1399 Wrap->Packet = Dup;\r
1400 }\r
1401\r
1402 NetListRemoveHead (&Instance->RcvdDgramQue);\r
1403\r
1404 Token = (EFI_UDP4_COMPLETION_TOKEN *) NetMapRemoveHead (&Instance->RxTokens, NULL);\r
1405\r
1406 //\r
1407 // Build the FragmentTable and set the FragmentCount in RxData.\r
1408 //\r
1409 RxData = &Wrap->RxData;\r
1410 RxData->FragmentCount = Wrap->Packet->BlockOpNum;\r
1411\r
1412 NetbufBuildExt (\r
1413 Wrap->Packet,\r
1414 (NET_FRAGMENT *) RxData->FragmentTable,\r
1415 &RxData->FragmentCount\r
1416 );\r
1417\r
1418 Token->Status = EFI_SUCCESS;\r
1419 Token->Packet.RxData = &Wrap->RxData;\r
1420\r
1421 gBS->SignalEvent (Token->Event);\r
1422\r
1423 NetListInsertTail (&Instance->DeliveredDgramQue, &Wrap->Link);\r
1424 }\r
1425}\r
1426\r
1427\r
1428/**\r
1429 This function delivers the datagrams enqueued in the instances.\r
1430\r
1431 @param Udp4Service Pointer to the udp service context data.\r
1432\r
1433 @return None.\r
1434\r
1435**/\r
1436STATIC\r
1437VOID\r
1438Udp4DeliverDgram (\r
1439 IN UDP4_SERVICE_DATA *Udp4Service\r
1440 )\r
1441{\r
1442 NET_LIST_ENTRY *Entry;\r
1443 UDP4_INSTANCE_DATA *Instance;\r
1444\r
1445 NET_LIST_FOR_EACH (Entry, &Udp4Service->ChildrenList) {\r
1446 //\r
1447 // Iterate the instances.\r
1448 //\r
1449 Instance = NET_LIST_USER_STRUCT (Entry, UDP4_INSTANCE_DATA, Link);\r
1450\r
1451 if (!Instance->Configured) {\r
1452 continue;\r
1453 }\r
1454\r
1455 //\r
1456 // Deliver the datagrams of this instance.\r
1457 //\r
1458 Udp4InstanceDeliverDgram (Instance);\r
1459 }\r
1460}\r
1461\r
1462\r
1463/**\r
1464 This function demultiplexes the received udp datagram to the apropriate instances.\r
1465\r
1466 @param Udp4Service Pointer to the udp service context data.\r
1467 @param NetSession Pointer to the EFI_NET_SESSION_DATA abstrated from\r
1468 the received datagram.\r
1469 @param Packet Pointer to the buffer containing the received udp\r
1470 datagram.\r
1471\r
1472 @return None.\r
1473\r
1474**/\r
1475STATIC\r
1476VOID\r
1477Udp4Demultiplex (\r
1478 IN UDP4_SERVICE_DATA *Udp4Service,\r
1479 IN EFI_NET_SESSION_DATA *NetSession,\r
1480 IN NET_BUF *Packet\r
1481 )\r
1482{\r
1483 EFI_UDP4_HEADER *Udp4Header;\r
1484 UINT16 HeadSum;\r
1485 EFI_UDP4_RECEIVE_DATA RxData;\r
1486 EFI_UDP4_SESSION_DATA *Udp4Session;\r
1487 UINTN Enqueued;\r
1488\r
1489 //\r
1490 // Get the datagram header from the packet buffer.\r
1491 //\r
1492 Udp4Header = (EFI_UDP4_HEADER *) NetbufGetByte (Packet, 0, NULL);\r
1493\r
1494 if (Udp4Header->Checksum != 0) {\r
1495 //\r
1496 // check the checksum.\r
1497 //\r
1498 HeadSum = NetPseudoHeadChecksum (\r
1499 NetSession->Source,\r
1500 NetSession->Dest,\r
1501 EFI_IP_PROTO_UDP,\r
1502 0\r
1503 );\r
1504\r
1505 if (Udp4Checksum (Packet, HeadSum) != 0) {\r
1506 //\r
1507 // Wrong checksum.\r
1508 //\r
1509 return;\r
1510 }\r
1511 }\r
1512\r
1513 gRT->GetTime (&RxData.TimeStamp, NULL);\r
1514\r
1515 Udp4Session = &RxData.UdpSession;\r
1516 EFI_IP4 (Udp4Session->SourceAddress) = NetSession->Source;\r
1517 EFI_IP4 (Udp4Session->DestinationAddress) = NetSession->Dest;\r
1518 Udp4Session->SourcePort = NTOHS (Udp4Header->SrcPort);\r
1519 Udp4Session->DestinationPort = NTOHS (Udp4Header->DstPort);\r
1520\r
1521 //\r
1522 // Trim the UDP header.\r
1523 //\r
1524 NetbufTrim (Packet, UDP4_HEADER_SIZE, TRUE);\r
1525\r
1526 RxData.DataLength = (UINT32) Packet->TotalSize;\r
1527\r
1528 //\r
1529 // Try to enqueue this datagram into the instances.\r
1530 //\r
1531 Enqueued = Udp4EnqueueDgram (Udp4Service, Packet, &RxData);\r
1532\r
1533 if (Enqueued == 0) {\r
1534 //\r
1535 // Send the port unreachable ICMP packet before we free this NET_BUF\r
1536 //\r
1537 Udp4SendPortUnreach (Udp4Service->IpIo, NetSession, Udp4Header);\r
1538 }\r
1539\r
1540 //\r
1541 // Try to free the packet before deliver it.\r
1542 //\r
1543 NetbufFree (Packet);\r
1544\r
1545 if (Enqueued > 0) {\r
1546 //\r
1547 // Deliver the datagram.\r
1548 //\r
1549 Udp4DeliverDgram (Udp4Service);\r
1550 }\r
1551}\r
1552\r
1553\r
1554/**\r
1555 This function builds and sends out a icmp port unreachable message.\r
1556\r
1557 @param IpIo Pointer to the IP_IO instance.\r
1558 @param NetSession Pointer to the EFI_NET_SESSION_DATA of the packet\r
1559 causes this icmp error message.\r
1560 @param Udp4Header Pointer to the udp header of the datagram causes\r
1561 this icmp error message.\r
1562\r
1563 @return None.\r
1564\r
1565**/\r
1566STATIC\r
1567VOID\r
1568Udp4SendPortUnreach (\r
1569 IN IP_IO *IpIo,\r
1570 IN EFI_NET_SESSION_DATA *NetSession,\r
1571 IN VOID *Udp4Header\r
1572 )\r
1573{\r
1574 NET_BUF *Packet;\r
1575 UINT32 Len;\r
1576 IP4_ICMP_ERROR_HEAD *IcmpErrHdr;\r
1577 EFI_IP4_HEADER *IpHdr;\r
1578 UINT8 *Ptr;\r
1579 IP_IO_OVERRIDE Override;\r
1580 IP_IO_IP_INFO *IpSender;\r
1581\r
1582 IpSender = IpIoFindSender (&IpIo, NetSession->Dest);\r
1583 if (IpSender == NULL) {\r
1584 //\r
1585 // No apropriate sender, since we cannot send out the ICMP message through\r
1586 // the default zero station address IP instance, abort.\r
1587 //\r
1588 return;\r
1589 }\r
1590\r
1591 IpHdr = NetSession->IpHdr;\r
1592\r
1593 //\r
1594 // Calculate the requried length of the icmp error message.\r
1595 //\r
1596 Len = sizeof (IP4_ICMP_ERROR_HEAD) + (EFI_IP4_HEADER_LEN (IpHdr) -\r
1597 sizeof (IP4_HEAD)) + ICMP_ERROR_PACKET_LENGTH;\r
1598\r
1599 //\r
1600 // Allocate buffer for the icmp error message.\r
1601 //\r
1602 Packet = NetbufAlloc (Len);\r
1603 if (Packet == NULL) {\r
1604 return;\r
1605 }\r
1606\r
1607 //\r
1608 // Allocate space for the IP4_ICMP_ERROR_HEAD.\r
1609 //\r
1610 IcmpErrHdr = (IP4_ICMP_ERROR_HEAD *) NetbufAllocSpace (Packet, Len, FALSE);\r
1611\r
1612 //\r
1613 // Set the required fields for the icmp port unreachable message.\r
1614 //\r
1615 IcmpErrHdr->Head.Type = ICMP_TYPE_UNREACH;\r
1616 IcmpErrHdr->Head.Code = ICMP_CODE_UNREACH_PORT;\r
1617 IcmpErrHdr->Head.Checksum = 0;\r
1618 IcmpErrHdr->Fourth = 0;\r
1619\r
1620 //\r
1621 // Copy the IP header of the datagram tragged the error.\r
1622 //\r
1623 NetCopyMem (&IcmpErrHdr->IpHead, IpHdr, EFI_IP4_HEADER_LEN (IpHdr));\r
1624\r
1625 //\r
1626 // Copy the UDP header.\r
1627 //\r
1628 Ptr = (UINT8 *) &IcmpErrHdr->IpHead + EFI_IP4_HEADER_LEN (IpHdr);\r
1629 NetCopyMem (Ptr, Udp4Header, ICMP_ERROR_PACKET_LENGTH);\r
1630\r
1631 //\r
1632 // Calculate the checksum.\r
1633 //\r
1634 IcmpErrHdr->Head.Checksum = ~(NetbufChecksum (Packet));\r
1635\r
1636 //\r
1637 // Fill the override data.\r
1638 //\r
1639 Override.DoNotFragment = FALSE;\r
1640 Override.TypeOfService = 0;\r
1641 Override.TimeToLive = 255;\r
1642 Override.Protocol = EFI_IP_PROTO_ICMP;\r
1643 EFI_IP4 (Override.SourceAddress) = NetSession->Dest;\r
1644 EFI_IP4 (Override.GatewayAddress) = 0;\r
1645\r
1646 //\r
1647 // Send out this icmp packet.\r
1648 //\r
1649 IpIoSend (IpIo, Packet, IpSender, NULL, NULL, NetSession->Source, &Override);\r
1650\r
1651 NetbufFree (Packet);\r
1652}\r
1653\r
1654\r
1655/**\r
1656 This function handles the received Icmp Error message and demultiplexes it to the\r
1657 instance.\r
1658\r
1659 @param Udp4Service Pointer to the udp service context data.\r
1660 @param IcmpError The icmp error code.\r
1661 @param NetSession Pointer to the EFI_NET_SESSION_DATA abstracted\r
1662 from the received Icmp Error packet.\r
1663 @param Packet Pointer to the Icmp Error packet.\r
1664\r
1665 @return None.\r
1666\r
1667**/\r
1668STATIC\r
1669VOID\r
1670Udp4IcmpHandler (\r
1671 IN UDP4_SERVICE_DATA *Udp4Service,\r
1672 IN ICMP_ERROR IcmpError,\r
1673 IN EFI_NET_SESSION_DATA *NetSession,\r
1674 IN NET_BUF *Packet\r
1675 )\r
1676{\r
1677 EFI_UDP4_HEADER *Udp4Header;\r
1678 EFI_UDP4_SESSION_DATA Udp4Session;\r
1679 NET_LIST_ENTRY *Entry;\r
1680 UDP4_INSTANCE_DATA *Instance;\r
1681\r
1682 Udp4Header = (EFI_UDP4_HEADER *) NetbufGetByte (Packet, 0, NULL);\r
1683\r
1684 EFI_IP4 (Udp4Session.SourceAddress) = NetSession->Source;\r
1685 EFI_IP4 (Udp4Session.DestinationAddress) = NetSession->Dest;\r
1686 Udp4Session.SourcePort = NTOHS (Udp4Header->DstPort);\r
1687 Udp4Session.DestinationPort = NTOHS (Udp4Header->SrcPort);\r
1688\r
1689 NET_LIST_FOR_EACH (Entry, &Udp4Service->ChildrenList) {\r
1690 //\r
1691 // Iterate all the instances.\r
1692 //\r
1693 Instance = NET_LIST_USER_STRUCT (Entry, UDP4_INSTANCE_DATA, Link);\r
1694\r
1695 if (!Instance->Configured ||\r
1696 Instance->ConfigData.AcceptPromiscuous ||\r
1697 Instance->ConfigData.AcceptAnyPort ||\r
1698 (EFI_IP4 (Instance->ConfigData.StationAddress) == 0)) {\r
1699 //\r
1700 // Don't try to deliver the ICMP error to this instance if it is not configured,\r
1701 // or it's configured to be promiscuous or accept any port or accept all the\r
1702 // datagrams.\r
1703 //\r
1704 continue;\r
1705 }\r
1706\r
1707 if (Udp4MatchDgram (Instance, &Udp4Session)) {\r
1708 //\r
1709 // Translate the Icmp Error code according to the udp spec.\r
1710 //\r
1711 Instance->IcmpError = IpIoGetIcmpErrStatus (IcmpError, NULL, NULL);\r
1712\r
1713 if (IcmpError > ICMP_ERR_UNREACH_PORT) {\r
1714 Instance->IcmpError = EFI_ICMP_ERROR;\r
1715 }\r
1716\r
1717 //\r
1718 // Notify the instance with the received Icmp Error.\r
1719 //\r
1720 Udp4ReportIcmpError (Instance);\r
1721\r
1722 break;\r
1723 }\r
1724 }\r
1725\r
1726 NetbufFree (Packet);\r
1727}\r
1728\r
1729\r
1730/**\r
1731 This function reports the received ICMP error.\r
1732\r
1733 @param Instance Pointer to the udp instance context data.\r
1734\r
1735 @return None.\r
1736\r
1737**/\r
1738VOID\r
1739Udp4ReportIcmpError (\r
1740 IN UDP4_INSTANCE_DATA *Instance\r
1741 )\r
1742{\r
1743 EFI_UDP4_COMPLETION_TOKEN *Token;\r
1744\r
1745 if (NetMapIsEmpty (&Instance->RxTokens)) {\r
1746 //\r
1747 // There are no receive tokens to deliver the ICMP error.\r
1748 //\r
1749 return;\r
1750 }\r
1751\r
1752 if (EFI_ERROR (Instance->IcmpError)) {\r
1753 //\r
1754 // Try to get a RxToken from the RxTokens map.\r
1755 //\r
1756 Token = (EFI_UDP4_COMPLETION_TOKEN *) NetMapRemoveHead (&Instance->RxTokens, NULL);\r
1757\r
1758 if (Token != NULL) {\r
1759 //\r
1760 // Report the error through the Token.\r
1761 //\r
1762 Token->Status = Instance->IcmpError;\r
1763 gBS->SignalEvent (Token->Event);\r
1764\r
1765 //\r
1766 // Clear the IcmpError.\r
1767 //\r
1768 Instance->IcmpError = EFI_SUCCESS;\r
1769 }\r
1770 }\r
1771}\r
1772\r
1773\r
1774/**\r
1775 This function is a dummy ext-free function for the NET_BUF created for the output\r
1776 udp datagram.\r
1777\r
1778 @param Context Pointer to the context data.\r
1779\r
1780 @return None.\r
1781\r
1782**/\r
1783VOID\r
1784Udp4NetVectorExtFree (\r
1785 VOID *Context\r
1786 )\r
1787{\r
1788}\r
1789\r
1790\r
1791/**\r
1792 Set the Udp4 variable data.\r
1793\r
1794 @param Udp4Service Udp4 service data.\r
1795\r
1796 @retval EFI_OUT_OF_RESOURCES There are not enough resources to set the\r
1797 variable.\r
1798 @retval other Set variable failed.\r
1799\r
1800**/\r
1801EFI_STATUS\r
1802Udp4SetVariableData (\r
1803 IN UDP4_SERVICE_DATA *Udp4Service\r
1804 )\r
1805{\r
1806 UINT32 NumConfiguredInstance;\r
1807 NET_LIST_ENTRY *Entry;\r
1808 UINTN VariableDataSize;\r
1809 EFI_UDP4_VARIABLE_DATA *Udp4VariableData;\r
1810 EFI_UDP4_SERVICE_POINT *Udp4ServicePoint;\r
1811 UDP4_INSTANCE_DATA *Udp4Instance;\r
1812 CHAR16 *NewMacString;\r
1813 EFI_STATUS Status;\r
1814\r
1815 NumConfiguredInstance = 0;\r
1816\r
1817 //\r
1818 // Go through the children list to count the configured children.\r
1819 //\r
1820 NET_LIST_FOR_EACH (Entry, &Udp4Service->ChildrenList) {\r
1821 Udp4Instance = NET_LIST_USER_STRUCT_S (\r
1822 Entry,\r
1823 UDP4_INSTANCE_DATA,\r
1824 Link,\r
1825 UDP4_INSTANCE_DATA_SIGNATURE\r
1826 );\r
1827\r
1828 if (Udp4Instance->Configured) {\r
1829 NumConfiguredInstance++;\r
1830 }\r
1831 }\r
1832\r
1833 //\r
1834 // Calculate the size of the Udp4VariableData. As there may be no Udp4 child,\r
1835 // we should add extra buffer for the service points only if the number of configured\r
1836 // children is more than 1.\r
1837 //\r
1838 VariableDataSize = sizeof (EFI_UDP4_VARIABLE_DATA);\r
1839\r
1840 if (NumConfiguredInstance > 1) {\r
1841 VariableDataSize += sizeof (EFI_UDP4_SERVICE_POINT) * (NumConfiguredInstance - 1);\r
1842 }\r
1843\r
1844 Udp4VariableData = NetAllocatePool (VariableDataSize);\r
1845 if (Udp4VariableData == NULL) {\r
1846 return EFI_OUT_OF_RESOURCES;\r
1847 }\r
1848\r
1849 Udp4VariableData->DriverHandle = Udp4Service->ImageHandle;\r
1850 Udp4VariableData->ServiceCount = NumConfiguredInstance;\r
1851\r
1852 Udp4ServicePoint = &Udp4VariableData->Services[0];\r
1853\r
1854 //\r
1855 // Go through the children list to fill the configured children's address pairs.\r
1856 //\r
1857 NET_LIST_FOR_EACH (Entry, &Udp4Service->ChildrenList) {\r
1858 Udp4Instance = NET_LIST_USER_STRUCT_S (\r
1859 Entry,\r
1860 UDP4_INSTANCE_DATA,\r
1861 Link,\r
1862 UDP4_INSTANCE_DATA_SIGNATURE\r
1863 );\r
1864\r
1865 if (Udp4Instance->Configured) {\r
1866 Udp4ServicePoint->InstanceHandle = Udp4Instance->ChildHandle;\r
1867 Udp4ServicePoint->LocalAddress = Udp4Instance->ConfigData.StationAddress;\r
1868 Udp4ServicePoint->LocalPort = Udp4Instance->ConfigData.StationPort;\r
1869 Udp4ServicePoint->RemoteAddress = Udp4Instance->ConfigData.RemoteAddress;\r
1870 Udp4ServicePoint->RemotePort = Udp4Instance->ConfigData.RemotePort;\r
1871\r
1872 Udp4ServicePoint++;\r
1873 }\r
1874 }\r
1875\r
1876 //\r
1877 // Get the mac string.\r
1878 //\r
1879 Status = NetLibGetMacString (\r
1880 Udp4Service->ControllerHandle,\r
1881 Udp4Service->ImageHandle,\r
1882 &NewMacString\r
1883 );\r
1884 if (EFI_ERROR (Status)) {\r
1885 goto ON_ERROR;\r
1886 }\r
1887\r
1888 if (Udp4Service->MacString != NULL) {\r
1889 //\r
1890 // The variable is set already, we're going to update it.\r
1891 //\r
1892 if (StrCmp (Udp4Service->MacString, NewMacString) != 0) {\r
1893 //\r
1894 // The mac address is changed, delete the previous variable first.\r
1895 //\r
1896 gRT->SetVariable (\r
1897 Udp4Service->MacString,\r
1898 &gEfiUdp4ServiceBindingProtocolGuid,\r
1899 EFI_VARIABLE_BOOTSERVICE_ACCESS,\r
1900 0,\r
1901 NULL\r
1902 );\r
1903 }\r
1904\r
1905 NetFreePool (Udp4Service->MacString);\r
1906 }\r
1907\r
1908 Udp4Service->MacString = NewMacString;\r
1909\r
1910 Status = gRT->SetVariable (\r
1911 Udp4Service->MacString,\r
1912 &gEfiUdp4ServiceBindingProtocolGuid,\r
1913 EFI_VARIABLE_BOOTSERVICE_ACCESS,\r
1914 VariableDataSize,\r
1915 (VOID *) Udp4VariableData\r
1916 );\r
1917\r
1918ON_ERROR:\r
1919\r
1920 NetFreePool (Udp4VariableData);\r
1921\r
1922 return Status;\r
1923}\r
1924\r
1925\r
1926/**\r
1927 Clear the variable and free the resource.\r
1928\r
1929 @param Udp4Service Udp4 service data.\r
1930\r
1931 @return None.\r
1932\r
1933**/\r
1934VOID\r
1935Udp4ClearVariableData (\r
1936 IN UDP4_SERVICE_DATA *Udp4Service\r
1937 )\r
1938{\r
1939 ASSERT (Udp4Service->MacString != NULL);\r
1940\r
1941 gRT->SetVariable (\r
1942 Udp4Service->MacString,\r
1943 &gEfiUdp4ServiceBindingProtocolGuid,\r
1944 EFI_VARIABLE_BOOTSERVICE_ACCESS,\r
1945 0,\r
1946 NULL\r
1947 );\r
1948\r
1949 NetFreePool (Udp4Service->MacString);\r
1950 Udp4Service->MacString = NULL;\r
1951}\r