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