]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Network/Ip4Dxe/Ip4If.c
Get ParentDevicePath by using attribute EFI_OPEN_PROTOCOL_GET_PROTOCOL instead of...
[mirror_edk2.git] / MdeModulePkg / Universal / Network / Ip4Dxe / Ip4If.c
CommitLineData
772db4bb 1/** @file\r
3e8c18da 2 Implement IP4 pesudo interface.\r
3 \r
f1491294 4Copyright (c) 2005 - 2013, Intel Corporation. All rights reserved.<BR>\r
e5eed7d3 5This program and the accompanying materials\r
772db4bb 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
772db4bb 13**/\r
14\r
15#include "Ip4Impl.h"\r
16\r
17//\r
18// Mac address with all zero, used to determine whethter the ARP\r
19// resolve succeeded. Failed ARP requests zero the MAC address buffer.\r
20//\r
fe1e36e5 21EFI_MAC_ADDRESS mZeroMacAddress;\r
772db4bb 22\r
2ff29212 23/**\r
24 Callback funtion when frame transmission is finished. It will\r
25 call the frame owner's callback function to tell it the result.\r
26\r
3e8c18da 27 @param[in] Context Context which is point to the token.\r
2ff29212 28\r
29**/\r
36ee91ca 30VOID\r
31EFIAPI\r
32Ip4OnFrameSentDpc (\r
2ff29212 33 IN VOID *Context\r
36ee91ca 34 );\r
35\r
2ff29212 36/**\r
37 Request Ip4OnFrameSentDpc as a DPC at TPL_CALLBACK.\r
38\r
3e8c18da 39 @param[in] Event The transmit token's event.\r
40 @param[in] Context Context which is point to the token.\r
2ff29212 41\r
42**/\r
772db4bb 43VOID\r
44EFIAPI\r
45Ip4OnFrameSent (\r
2ff29212 46 IN EFI_EVENT Event,\r
47 IN VOID *Context\r
772db4bb 48 );\r
49\r
2ff29212 50/**\r
51 Callback function when ARP request are finished. It will cancelled\r
52 all the queued frame if the ARP requests failed. Or transmit them\r
53 if the request succeed.\r
54\r
3e8c18da 55 @param[in] Context The context of the callback, a point to the ARP\r
2ff29212 56 queue\r
57\r
2ff29212 58**/\r
36ee91ca 59VOID\r
60EFIAPI\r
61Ip4OnArpResolvedDpc (\r
62 IN VOID *Context\r
63 );\r
64\r
2ff29212 65/**\r
66 Request Ip4OnArpResolvedDpc as a DPC at TPL_CALLBACK.\r
67\r
3e8c18da 68 @param Event The Arp request event.\r
69 @param Context The context of the callback, a point to the ARP\r
70 queue.\r
71 \r
2ff29212 72**/\r
772db4bb 73VOID\r
74EFIAPI\r
75Ip4OnArpResolved (\r
76 IN EFI_EVENT Event,\r
77 IN VOID *Context\r
78 );\r
79\r
2ff29212 80/**\r
81 Received a frame from MNP, wrap it in net buffer then deliver\r
82 it to IP's input function. The ownship of the packet also\r
83 transferred to IP. When Ip is finished with this packet, it\r
84 will call NetbufFree to release the packet, NetbufFree will\r
85 again call the Ip4RecycleFrame to signal MNP's event and free\r
86 the token used.\r
87\r
88 @param Context Context for the callback.\r
89\r
2ff29212 90**/\r
36ee91ca 91VOID\r
92EFIAPI\r
93Ip4OnFrameReceivedDpc (\r
2ff29212 94 IN VOID *Context\r
36ee91ca 95 );\r
96\r
2ff29212 97/**\r
2ff29212 98 Request Ip4OnFrameReceivedDpc as a DPC at TPL_CALLBACK.\r
99\r
100 @param Event The receive event delivered to MNP for receive.\r
101 @param Context Context for the callback.\r
2ff29212 102\r
103**/\r
772db4bb 104VOID\r
105EFIAPI\r
106Ip4OnFrameReceived (\r
2ff29212 107 IN EFI_EVENT Event,\r
108 IN VOID *Context\r
772db4bb 109 );\r
110\r
2ff29212 111/**\r
112 Remove all the frames on the ARP queue that pass the FrameToCancel,\r
113 that is, either FrameToCancel is NULL or it returns true for the frame.\r
114\r
3e8c18da 115 @param[in] ArpQue ARP frame to remove the frames from.\r
116 @param[in] IoStatus The status returned to the cancelled frames'\r
2ff29212 117 callback function.\r
3e8c18da 118 @param[in] FrameToCancel Function to select which frame to cancel.\r
119 @param[in] Context Opaque parameter to the FrameToCancel.\r
2ff29212 120\r
121**/\r
772db4bb 122VOID\r
123Ip4CancelFrameArp (\r
124 IN IP4_ARP_QUE *ArpQue,\r
125 IN EFI_STATUS IoStatus,\r
24af132f 126 IN IP4_FRAME_TO_CANCEL FrameToCancel OPTIONAL,\r
772db4bb 127 IN VOID *Context\r
128 );\r
129\r
130\r
131/**\r
132 Wrap a transmit request into a newly allocated IP4_LINK_TX_TOKEN.\r
133\r
3e8c18da 134 @param[in] Interface The interface to send out to.\r
135 @param[in] IpInstance The IpInstance that transmit the packet. NULL if\r
772db4bb 136 the packet is sent by the IP4 driver itself.\r
3e8c18da 137 @param[in] Packet The packet to transmit\r
138 @param[in] CallBack Call back function to execute if transmission\r
772db4bb 139 finished.\r
3e8c18da 140 @param[in] Context Opaque parameter to the call back.\r
772db4bb 141\r
3e8c18da 142 @retval Token The wrapped token if succeed \r
143 @retval NULL The wrapped token if NULL\r
772db4bb 144\r
145**/\r
772db4bb 146IP4_LINK_TX_TOKEN *\r
147Ip4WrapLinkTxToken (\r
148 IN IP4_INTERFACE *Interface,\r
24af132f 149 IN IP4_PROTOCOL *IpInstance OPTIONAL,\r
772db4bb 150 IN NET_BUF *Packet,\r
151 IN IP4_FRAME_CALLBACK CallBack,\r
152 IN VOID *Context\r
153 )\r
154{\r
155 EFI_MANAGED_NETWORK_COMPLETION_TOKEN *MnpToken;\r
156 EFI_MANAGED_NETWORK_TRANSMIT_DATA *MnpTxData;\r
157 IP4_LINK_TX_TOKEN *Token;\r
158 EFI_STATUS Status;\r
159 UINT32 Count;\r
160\r
e48e37fc 161 Token = AllocatePool (sizeof (IP4_LINK_TX_TOKEN) + \\r
772db4bb 162 (Packet->BlockOpNum - 1) * sizeof (EFI_MANAGED_NETWORK_FRAGMENT_DATA));\r
163\r
164 if (Token == NULL) {\r
165 return NULL;\r
166 }\r
167\r
168 Token->Signature = IP4_FRAME_TX_SIGNATURE;\r
e48e37fc 169 InitializeListHead (&Token->Link);\r
772db4bb 170\r
171 Token->Interface = Interface;\r
172 Token->IpInstance = IpInstance;\r
173 Token->CallBack = CallBack;\r
174 Token->Packet = Packet;\r
175 Token->Context = Context;\r
687a2e5f 176 CopyMem (&Token->DstMac, &mZeroMacAddress, sizeof (Token->DstMac));\r
177 CopyMem (&Token->SrcMac, &Interface->Mac, sizeof (Token->SrcMac));\r
772db4bb 178\r
179 MnpToken = &(Token->MnpToken);\r
180 MnpToken->Status = EFI_NOT_READY;\r
181\r
182 Status = gBS->CreateEvent (\r
183 EVT_NOTIFY_SIGNAL,\r
e48e37fc 184 TPL_NOTIFY,\r
772db4bb 185 Ip4OnFrameSent,\r
186 Token,\r
187 &MnpToken->Event\r
188 );\r
189\r
190 if (EFI_ERROR (Status)) {\r
766c7483 191 FreePool (Token);\r
772db4bb 192 return NULL;\r
193 }\r
194\r
195 MnpTxData = &Token->MnpTxData;\r
196 MnpToken->Packet.TxData = MnpTxData;\r
197\r
198 MnpTxData->DestinationAddress = &Token->DstMac;\r
199 MnpTxData->SourceAddress = &Token->SrcMac;\r
200 MnpTxData->ProtocolType = IP4_ETHER_PROTO;\r
201 MnpTxData->DataLength = Packet->TotalSize;\r
202 MnpTxData->HeaderLength = 0;\r
203\r
204 Count = Packet->BlockOpNum;\r
205\r
206 NetbufBuildExt (Packet, (NET_FRAGMENT *) MnpTxData->FragmentTable, &Count);\r
207 MnpTxData->FragmentCount = (UINT16)Count;\r
208\r
209 return Token;\r
210}\r
211\r
212\r
213/**\r
214 Free the link layer transmit token. It will close the event\r
215 then free the memory used.\r
216\r
3e8c18da 217 @param[in] Token Token to free\r
772db4bb 218\r
219**/\r
772db4bb 220VOID\r
221Ip4FreeLinkTxToken (\r
222 IN IP4_LINK_TX_TOKEN *Token\r
223 )\r
224{\r
225 NET_CHECK_SIGNATURE (Token, IP4_FRAME_TX_SIGNATURE);\r
226\r
227 gBS->CloseEvent (Token->MnpToken.Event);\r
766c7483 228 FreePool (Token);\r
772db4bb 229}\r
230\r
231\r
232/**\r
233 Create an IP_ARP_QUE structure to request ARP service.\r
234\r
3e8c18da 235 @param[in] Interface The interface to send ARP from.\r
236 @param[in] DestIp The destination IP (host byte order) to request MAC\r
772db4bb 237 for\r
238\r
239 @return Point to newly created IP4_ARP_QUE if succeed, otherwise NULL.\r
240\r
241**/\r
772db4bb 242IP4_ARP_QUE *\r
243Ip4CreateArpQue (\r
244 IN IP4_INTERFACE *Interface,\r
245 IN IP4_ADDR DestIp\r
246 )\r
247{\r
248 IP4_ARP_QUE *ArpQue;\r
249 EFI_STATUS Status;\r
250\r
e48e37fc 251 ArpQue = AllocatePool (sizeof (IP4_ARP_QUE));\r
772db4bb 252\r
253 if (ArpQue == NULL) {\r
254 return NULL;\r
255 }\r
256\r
257 ArpQue->Signature = IP4_FRAME_ARP_SIGNATURE;\r
e48e37fc 258 InitializeListHead (&ArpQue->Link);\r
772db4bb 259\r
e48e37fc 260 InitializeListHead (&ArpQue->Frames);\r
772db4bb 261 ArpQue->Interface = Interface;\r
262\r
263 Status = gBS->CreateEvent (\r
264 EVT_NOTIFY_SIGNAL,\r
e48e37fc 265 TPL_NOTIFY,\r
772db4bb 266 Ip4OnArpResolved,\r
267 ArpQue,\r
268 &ArpQue->OnResolved\r
269 );\r
270\r
271 if (EFI_ERROR (Status)) {\r
766c7483 272 FreePool (ArpQue);\r
772db4bb 273 return NULL;\r
274 }\r
275\r
276 ArpQue->Ip = DestIp;\r
687a2e5f 277 CopyMem (&ArpQue->Mac, &mZeroMacAddress, sizeof (ArpQue->Mac));\r
772db4bb 278\r
279 return ArpQue;\r
280}\r
281\r
282\r
283/**\r
284 Remove all the transmit requests queued on the ARP queue, then free it.\r
285\r
3e8c18da 286 @param[in] ArpQue Arp queue to free\r
287 @param[in] IoStatus The transmit status returned to transmit requests'\r
772db4bb 288 callback.\r
289\r
772db4bb 290**/\r
772db4bb 291VOID\r
292Ip4FreeArpQue (\r
293 IN IP4_ARP_QUE *ArpQue,\r
294 IN EFI_STATUS IoStatus\r
295 )\r
296{\r
297 NET_CHECK_SIGNATURE (ArpQue, IP4_FRAME_ARP_SIGNATURE);\r
298\r
299 //\r
300 // Remove all the frame waiting the ARP response\r
301 //\r
302 Ip4CancelFrameArp (ArpQue, IoStatus, NULL, NULL);\r
303\r
304 gBS->CloseEvent (ArpQue->OnResolved);\r
766c7483 305 FreePool (ArpQue);\r
772db4bb 306}\r
307\r
308\r
309/**\r
310 Create a link layer receive token to wrap the receive request\r
311\r
3e8c18da 312 @param[in] Interface The interface to receive from\r
313 @param[in] IpInstance The instance that request the receive (NULL for IP4\r
772db4bb 314 driver itself)\r
3e8c18da 315 @param[in] CallBack Call back function to execute when finished.\r
316 @param[in] Context Opaque parameters to the callback\r
772db4bb 317\r
318 @return Point to created IP4_LINK_RX_TOKEN if succeed, otherwise NULL.\r
319\r
320**/\r
772db4bb 321IP4_LINK_RX_TOKEN *\r
322Ip4CreateLinkRxToken (\r
323 IN IP4_INTERFACE *Interface,\r
324 IN IP4_PROTOCOL *IpInstance,\r
325 IN IP4_FRAME_CALLBACK CallBack,\r
326 IN VOID *Context\r
327 )\r
328{\r
329 EFI_MANAGED_NETWORK_COMPLETION_TOKEN *MnpToken;\r
330 IP4_LINK_RX_TOKEN *Token;\r
331 EFI_STATUS Status;\r
332\r
e48e37fc 333 Token = AllocatePool (sizeof (IP4_LINK_RX_TOKEN));\r
772db4bb 334 if (Token == NULL) {\r
335 return NULL;\r
336 }\r
337\r
338 Token->Signature = IP4_FRAME_RX_SIGNATURE;\r
339 Token->Interface = Interface;\r
340 Token->IpInstance = IpInstance;\r
341 Token->CallBack = CallBack;\r
342 Token->Context = Context;\r
343\r
344 MnpToken = &Token->MnpToken;\r
345 MnpToken->Status = EFI_NOT_READY;\r
346\r
347 Status = gBS->CreateEvent (\r
348 EVT_NOTIFY_SIGNAL,\r
e48e37fc 349 TPL_NOTIFY,\r
772db4bb 350 Ip4OnFrameReceived,\r
351 Token,\r
352 &MnpToken->Event\r
353 );\r
354\r
355 if (EFI_ERROR (Status)) {\r
766c7483 356 FreePool (Token);\r
772db4bb 357 return NULL;\r
358 }\r
359\r
360 MnpToken->Packet.RxData = NULL;\r
361 return Token;\r
362}\r
363\r
364\r
365/**\r
366 Free the link layer request token. It will close the event\r
367 then free the memory used.\r
368\r
3e8c18da 369 @param[in] Token Request token to free.\r
772db4bb 370\r
371**/\r
772db4bb 372VOID\r
373Ip4FreeFrameRxToken (\r
374 IN IP4_LINK_RX_TOKEN *Token\r
375 )\r
376{\r
377\r
378 NET_CHECK_SIGNATURE (Token, IP4_FRAME_RX_SIGNATURE);\r
379\r
380 gBS->CloseEvent (Token->MnpToken.Event);\r
766c7483 381 FreePool (Token);\r
772db4bb 382}\r
383\r
384\r
385/**\r
386 Remove all the frames on the ARP queue that pass the FrameToCancel,\r
387 that is, either FrameToCancel is NULL or it returns true for the frame.\r
388\r
3e8c18da 389 @param[in] ArpQue ARP frame to remove the frames from.\r
390 @param[in] IoStatus The status returned to the cancelled frames'\r
772db4bb 391 callback function.\r
3e8c18da 392 @param[in] FrameToCancel Function to select which frame to cancel.\r
393 @param[in] Context Opaque parameter to the FrameToCancel.\r
772db4bb 394\r
395**/\r
772db4bb 396VOID\r
397Ip4CancelFrameArp (\r
398 IN IP4_ARP_QUE *ArpQue,\r
399 IN EFI_STATUS IoStatus,\r
24af132f 400 IN IP4_FRAME_TO_CANCEL FrameToCancel OPTIONAL,\r
772db4bb 401 IN VOID *Context\r
402 )\r
403{\r
e48e37fc 404 LIST_ENTRY *Entry;\r
405 LIST_ENTRY *Next;\r
772db4bb 406 IP4_LINK_TX_TOKEN *Token;\r
407\r
408 NET_LIST_FOR_EACH_SAFE (Entry, Next, &ArpQue->Frames) {\r
409 Token = NET_LIST_USER_STRUCT (Entry, IP4_LINK_TX_TOKEN, Link);\r
410\r
411 if ((FrameToCancel == NULL) || FrameToCancel (Token, Context)) {\r
e48e37fc 412 RemoveEntryList (Entry);\r
772db4bb 413\r
414 Token->CallBack (Token->IpInstance, Token->Packet, IoStatus, 0, Token->Context);\r
415 Ip4FreeLinkTxToken (Token);\r
416 }\r
417 }\r
418}\r
419\r
420\r
421/**\r
422 Remove all the frames on the interface that pass the FrameToCancel,\r
423 either queued on ARP queues or that have already been delivered to\r
424 MNP and not yet recycled.\r
425\r
3e8c18da 426 @param[in] Interface Interface to remove the frames from\r
427 @param[in] IoStatus The transmit status returned to the frames'\r
772db4bb 428 callback\r
3e8c18da 429 @param[in] FrameToCancel Function to select the frame to cancel, NULL to\r
772db4bb 430 select all\r
3e8c18da 431 @param[in] Context Opaque parameters passed to FrameToCancel\r
772db4bb 432\r
433**/\r
434VOID\r
435Ip4CancelFrames (\r
436 IN IP4_INTERFACE *Interface,\r
437 IN EFI_STATUS IoStatus,\r
24af132f 438 IN IP4_FRAME_TO_CANCEL FrameToCancel OPTIONAL,\r
772db4bb 439 IN VOID *Context\r
440 )\r
441{\r
e48e37fc 442 LIST_ENTRY *Entry;\r
443 LIST_ENTRY *Next;\r
772db4bb 444 IP4_ARP_QUE *ArpQue;\r
445 IP4_LINK_TX_TOKEN *Token;\r
446\r
447 //\r
448 // Cancel all the pending frames on ARP requests\r
449 //\r
450 NET_LIST_FOR_EACH_SAFE (Entry, Next, &Interface->ArpQues) {\r
451 ArpQue = NET_LIST_USER_STRUCT (Entry, IP4_ARP_QUE, Link);\r
452\r
453 Ip4CancelFrameArp (ArpQue, IoStatus, FrameToCancel, Context);\r
454\r
e48e37fc 455 if (IsListEmpty (&ArpQue->Frames)) {\r
772db4bb 456 Interface->Arp->Cancel (Interface->Arp, &ArpQue->Ip, ArpQue->OnResolved);\r
772db4bb 457 }\r
458 }\r
459\r
460 //\r
461 // Cancel all the frames that have been delivered to MNP\r
462 // but not yet recycled.\r
463 //\r
464 NET_LIST_FOR_EACH_SAFE (Entry, Next, &Interface->SentFrames) {\r
465 Token = NET_LIST_USER_STRUCT (Entry, IP4_LINK_TX_TOKEN, Link);\r
466\r
467 if ((FrameToCancel == NULL) || FrameToCancel (Token, Context)) {\r
772db4bb 468 Interface->Mnp->Cancel (Interface->Mnp, &Token->MnpToken);\r
772db4bb 469 }\r
470 }\r
471}\r
472\r
473\r
474/**\r
475 Create an IP4_INTERFACE. Delay the creation of ARP instance until\r
476 the interface is configured.\r
477\r
3e8c18da 478 @param[in] Mnp The shared MNP child of this IP4 service binding\r
772db4bb 479 instance\r
3e8c18da 480 @param[in] Controller The controller this IP4 service binding instance\r
772db4bb 481 is installed. Most like the UNDI handle.\r
3e8c18da 482 @param[in] ImageHandle This driver's image handle\r
772db4bb 483\r
484 @return Point to the created IP4_INTERFACE, otherwise NULL.\r
485\r
486**/\r
487IP4_INTERFACE *\r
488Ip4CreateInterface (\r
489 IN EFI_MANAGED_NETWORK_PROTOCOL *Mnp,\r
490 IN EFI_HANDLE Controller,\r
491 IN EFI_HANDLE ImageHandle\r
492 )\r
493{\r
494 IP4_INTERFACE *Interface;\r
495 EFI_SIMPLE_NETWORK_MODE SnpMode;\r
496\r
e48e37fc 497 Interface = AllocatePool (sizeof (IP4_INTERFACE));\r
772db4bb 498\r
499 if ((Interface == NULL) || (Mnp == NULL)) {\r
500 return NULL;\r
501 }\r
502\r
503 Interface->Signature = IP4_INTERFACE_SIGNATURE;\r
e48e37fc 504 InitializeListHead (&Interface->Link);\r
772db4bb 505 Interface->RefCnt = 1;\r
506\r
507 Interface->Ip = IP4_ALLZERO_ADDRESS;\r
508 Interface->SubnetMask = IP4_ALLZERO_ADDRESS;\r
509 Interface->Configured = FALSE;\r
510\r
511 Interface->Controller = Controller;\r
512 Interface->Image = ImageHandle;\r
513 Interface->Mnp = Mnp;\r
514 Interface->Arp = NULL;\r
515 Interface->ArpHandle = NULL;\r
516\r
e48e37fc 517 InitializeListHead (&Interface->ArpQues);\r
518 InitializeListHead (&Interface->SentFrames);\r
772db4bb 519\r
520 Interface->RecvRequest = NULL;\r
521\r
522 //\r
523 // Get the interface's Mac address and broadcast mac address from SNP\r
524 //\r
525 if (EFI_ERROR (Mnp->GetModeData (Mnp, NULL, &SnpMode))) {\r
766c7483 526 FreePool (Interface);\r
772db4bb 527 return NULL;\r
528 }\r
529\r
687a2e5f 530 CopyMem (&Interface->Mac, &SnpMode.CurrentAddress, sizeof (Interface->Mac));\r
531 CopyMem (&Interface->BroadcastMac, &SnpMode.BroadcastAddress, sizeof (Interface->BroadcastMac));\r
772db4bb 532 Interface->HwaddrLen = SnpMode.HwAddressSize;\r
533\r
e48e37fc 534 InitializeListHead (&Interface->IpInstances);\r
772db4bb 535 Interface->PromiscRecv = FALSE;\r
536\r
537 return Interface;\r
538}\r
539\r
540\r
541/**\r
542 Set the interface's address, create and configure\r
543 the ARP child if necessary.\r
544\r
3e8c18da 545 @param Interface The interface to set the address\r
546 @param IpAddr The interface's IP address\r
547 @param SubnetMask The interface's netmask\r
772db4bb 548\r
549 @retval EFI_SUCCESS The interface is configured with Ip/netmask pair,\r
550 and a ARP is created for it.\r
551 @retval Others Failed to set the interface's address.\r
552\r
553**/\r
554EFI_STATUS\r
555Ip4SetAddress (\r
5405e9a6 556 IN OUT IP4_INTERFACE *Interface,\r
557 IN IP4_ADDR IpAddr,\r
558 IN IP4_ADDR SubnetMask\r
772db4bb 559 )\r
560{\r
561 EFI_ARP_CONFIG_DATA ArpConfig;\r
562 EFI_STATUS Status;\r
563 INTN Type;\r
564 INTN Len;\r
565 IP4_ADDR Netmask;\r
566\r
567 NET_CHECK_SIGNATURE (Interface, IP4_INTERFACE_SIGNATURE);\r
568\r
569 ASSERT (!Interface->Configured);\r
570\r
571 //\r
572 // Set the ip/netmask, then compute the subnet broadcast\r
573 // and network broadcast for easy access. When computing\r
574 // nework broadcast, the subnet mask is most like longer\r
575 // than the default netmask (not subneted) as defined in\r
576 // RFC793. If that isn't the case, we are aggregating the\r
577 // networks, use the subnet's mask instead.\r
578 //\r
579 Interface->Ip = IpAddr;\r
580 Interface->SubnetMask = SubnetMask;\r
581 Interface->SubnetBrdcast = (IpAddr | ~SubnetMask);\r
582\r
583 Type = NetGetIpClass (IpAddr);\r
584 Len = NetGetMaskLength (SubnetMask);\r
894d038a 585 Netmask = gIp4AllMasks[MIN ((Len - 1), Type << 3)];\r
772db4bb 586 Interface->NetBrdcast = (IpAddr | ~Netmask);\r
587\r
588 //\r
589 // If the address is NOT all zero, create then configure an ARP child.\r
590 // Pay attention: DHCP configures its station address as 0.0.0.0/0\r
591 //\r
592 Interface->Arp = NULL;\r
593 Interface->ArpHandle = NULL;\r
594\r
595 if (IpAddr != IP4_ALLZERO_ADDRESS) {\r
596 Status = NetLibCreateServiceChild (\r
597 Interface->Controller,\r
598 Interface->Image,\r
599 &gEfiArpServiceBindingProtocolGuid,\r
600 &Interface->ArpHandle\r
601 );\r
602\r
603 if (EFI_ERROR (Status)) {\r
604 return Status;;\r
605 }\r
606\r
607 Status = gBS->OpenProtocol (\r
608 Interface->ArpHandle,\r
609 &gEfiArpProtocolGuid,\r
610 (VOID **) &Interface->Arp,\r
611 Interface->Image,\r
612 Interface->Controller,\r
613 EFI_OPEN_PROTOCOL_BY_DRIVER\r
614 );\r
615\r
616 if (EFI_ERROR (Status)) {\r
617 goto ON_ERROR;\r
618 }\r
619\r
620 IpAddr = HTONL (IpAddr);\r
621 ArpConfig.SwAddressType = IP4_ETHER_PROTO;\r
622 ArpConfig.SwAddressLength = 4;\r
623 ArpConfig.StationAddress = &IpAddr;\r
624 ArpConfig.EntryTimeOut = 0;\r
625 ArpConfig.RetryCount = 0;\r
626 ArpConfig.RetryTimeOut = 0;\r
627\r
628 Status = Interface->Arp->Configure (Interface->Arp, &ArpConfig);\r
629\r
630 if (EFI_ERROR (Status)) {\r
631 gBS->CloseProtocol (\r
632 Interface->ArpHandle,\r
633 &gEfiArpProtocolGuid,\r
634 Interface->Image,\r
635 Interface->Controller\r
636 );\r
637\r
638 goto ON_ERROR;\r
639 }\r
640 }\r
641\r
642 Interface->Configured = TRUE;\r
643 return EFI_SUCCESS;\r
644\r
645ON_ERROR:\r
646 NetLibDestroyServiceChild (\r
647 Interface->Controller,\r
648 Interface->Image,\r
649 &gEfiArpServiceBindingProtocolGuid,\r
650 &Interface->ArpHandle\r
651 );\r
652\r
653 return Status;\r
654}\r
655\r
656\r
657/**\r
5405e9a6 658 Filter function to cancel all the frame related to an IP instance.\r
772db4bb 659\r
3e8c18da 660 @param[in] Frame The transmit request to test whether to cancel\r
661 @param[in] Context The context which is the Ip instance that issued\r
772db4bb 662 the transmit.\r
663\r
664 @retval TRUE The frame belongs to this instance and is to be\r
665 removed\r
666 @retval FALSE The frame doesn't belong to this instance.\r
667\r
668**/\r
772db4bb 669BOOLEAN\r
670Ip4CancelInstanceFrame (\r
671 IN IP4_LINK_TX_TOKEN *Frame,\r
672 IN VOID *Context\r
673 )\r
674{\r
675 if (Frame->IpInstance == (IP4_PROTOCOL *) Context) {\r
676 return TRUE;\r
677 }\r
678\r
679 return FALSE;\r
680}\r
681\r
682\r
683\r
684/**\r
685 If there is a pending receive request, cancel it. Don't call\r
686 the receive request's callback because this function can be only\r
687 called if the instance or driver is tearing itself down. It\r
688 doesn't make sense to call it back. But it is necessary to call\r
689 the transmit token's callback to give it a chance to free the\r
690 packet and update the upper layer's transmit request status, say\r
691 that from the UDP.\r
692\r
3e8c18da 693 @param[in] Interface The interface used by the IpInstance\r
772db4bb 694\r
695**/\r
696VOID\r
697Ip4CancelReceive (\r
698 IN IP4_INTERFACE *Interface\r
699 )\r
700{\r
701 EFI_TPL OldTpl;\r
702 IP4_LINK_RX_TOKEN *Token;\r
703\r
704 if ((Token = Interface->RecvRequest) != NULL) {\r
e48e37fc 705 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
772db4bb 706\r
707 Interface->RecvRequest = NULL;\r
708 Interface->Mnp->Cancel (Interface->Mnp, &Token->MnpToken);\r
772db4bb 709\r
e48e37fc 710 gBS->RestoreTPL (OldTpl);\r
772db4bb 711 }\r
712}\r
713\r
714\r
715/**\r
716 Free the interface used by IpInstance. All the IP instance with\r
717 the same Ip/Netmask pair share the same interface. It is reference\r
718 counted. All the frames haven't been sent will be cancelled.\r
719 Because the IpInstance is optional, the caller must remove\r
720 IpInstance from the interface's instance list itself.\r
721\r
3e8c18da 722 @param[in] Interface The interface used by the IpInstance\r
723 @param[in] IpInstance The Ip instance that free the interface. NULL if\r
5405e9a6 724 the Ip driver is releasing the default interface.\r
772db4bb 725\r
726 @retval EFI_SUCCESS The interface use IpInstance is freed.\r
727\r
728**/\r
729EFI_STATUS\r
730Ip4FreeInterface (\r
731 IN IP4_INTERFACE *Interface,\r
732 IN IP4_PROTOCOL *IpInstance OPTIONAL\r
733 )\r
734{\r
735 NET_CHECK_SIGNATURE (Interface, IP4_INTERFACE_SIGNATURE);\r
736 ASSERT (Interface->RefCnt > 0);\r
737\r
738 //\r
739 // Remove all the pending transmit token related to this IP instance.\r
740 //\r
741 Ip4CancelFrames (Interface, EFI_ABORTED, Ip4CancelInstanceFrame, IpInstance);\r
742\r
772db4bb 743 //\r
f1491294 744 // Destroy the ARP instance if this is the last IP instance that\r
745 // has the address.\r
772db4bb 746 //\r
f1491294 747 if (Interface->Arp != NULL && IsListEmpty (&Interface->IpInstances)) {\r
772db4bb 748 gBS->CloseProtocol (\r
749 Interface->ArpHandle,\r
750 &gEfiArpProtocolGuid,\r
751 Interface->Image,\r
752 Interface->Controller\r
753 );\r
754\r
755 NetLibDestroyServiceChild (\r
756 Interface->Controller,\r
757 Interface->Image,\r
758 &gEfiArpServiceBindingProtocolGuid,\r
759 Interface->ArpHandle\r
760 );\r
f1491294 761 Interface->Arp = NULL;\r
762 }\r
763 \r
764 if (--Interface->RefCnt > 0) {\r
765 return EFI_SUCCESS;\r
772db4bb 766 }\r
767\r
f1491294 768 //\r
769 // Destroy the interface if it is not referenced by any IP instance (for common Interface)\r
770 // or the IP service (for the DefaultInterface). Remove all the system transmitted packets\r
771 // from this interface, cancel the receive request if there is one.\r
772 //\r
773 Ip4CancelFrames (Interface, EFI_ABORTED, Ip4CancelInstanceFrame, NULL);\r
774 Ip4CancelReceive (Interface);\r
775\r
776 ASSERT (IsListEmpty (&Interface->IpInstances));\r
777 ASSERT (IsListEmpty (&Interface->ArpQues));\r
778 ASSERT (IsListEmpty (&Interface->SentFrames));\r
779\r
e48e37fc 780 RemoveEntryList (&Interface->Link);\r
766c7483 781 FreePool (Interface);\r
772db4bb 782\r
783 return EFI_SUCCESS;\r
784}\r
785\r
786\r
787/**\r
788 Callback function when ARP request are finished. It will cancelled\r
789 all the queued frame if the ARP requests failed. Or transmit them\r
790 if the request succeed.\r
791\r
3e8c18da 792 @param[in] Context The context of the callback, a point to the ARP\r
772db4bb 793 queue\r
794\r
772db4bb 795**/\r
772db4bb 796VOID\r
797EFIAPI\r
36ee91ca 798Ip4OnArpResolvedDpc (\r
772db4bb 799 IN VOID *Context\r
800 )\r
801{\r
e48e37fc 802 LIST_ENTRY *Entry;\r
803 LIST_ENTRY *Next;\r
772db4bb 804 IP4_ARP_QUE *ArpQue;\r
805 IP4_INTERFACE *Interface;\r
806 IP4_LINK_TX_TOKEN *Token;\r
807 EFI_STATUS Status;\r
808\r
809 ArpQue = (IP4_ARP_QUE *) Context;\r
810 NET_CHECK_SIGNATURE (ArpQue, IP4_FRAME_ARP_SIGNATURE);\r
811\r
e48e37fc 812 RemoveEntryList (&ArpQue->Link);\r
772db4bb 813\r
814 //\r
815 // ARP resolve failed for some reason. Release all the frame\r
816 // and ARP queue itself. Ip4FreeArpQue will call the frame's\r
817 // owner back.\r
818 //\r
819 if (NET_MAC_EQUAL (&ArpQue->Mac, &mZeroMacAddress, ArpQue->Interface->HwaddrLen)) {\r
820 Ip4FreeArpQue (ArpQue, EFI_NO_MAPPING);\r
821\r
822 return ;\r
823 }\r
824\r
825 //\r
826 // ARP resolve succeeded, Transmit all the frame. Release the ARP\r
827 // queue. It isn't necessary for us to cache the ARP binding because\r
828 // we always check the ARP cache first before transmit.\r
829 //\r
830 Interface = ArpQue->Interface;\r
831\r
832 NET_LIST_FOR_EACH_SAFE (Entry, Next, &ArpQue->Frames) {\r
e48e37fc 833 RemoveEntryList (Entry);\r
772db4bb 834\r
835 Token = NET_LIST_USER_STRUCT (Entry, IP4_LINK_TX_TOKEN, Link);\r
687a2e5f 836 CopyMem (&Token->DstMac, &ArpQue->Mac, sizeof (Token->DstMac));\r
772db4bb 837\r
36ee91ca 838 //\r
839 // Insert the tx token before transmitting it via MNP as the FrameSentDpc\r
840 // may be called before Mnp->Transmit returns which will remove this tx\r
841 // token from the SentFrames list. Remove it from the list if the returned\r
842 // Status of Mnp->Transmit is not EFI_SUCCESS as in this case the\r
843 // FrameSentDpc won't be queued.\r
844 //\r
e48e37fc 845 InsertTailList (&Interface->SentFrames, &Token->Link);\r
772db4bb 846\r
36ee91ca 847 Status = Interface->Mnp->Transmit (Interface->Mnp, &Token->MnpToken);\r
772db4bb 848 if (EFI_ERROR (Status)) {\r
e48e37fc 849 RemoveEntryList (Entry);\r
772db4bb 850 Token->CallBack (Token->IpInstance, Token->Packet, Status, 0, Token->Context);\r
851\r
852 Ip4FreeLinkTxToken (Token);\r
853 continue;\r
854 }\r
772db4bb 855 }\r
856\r
857 Ip4FreeArpQue (ArpQue, EFI_SUCCESS);\r
858}\r
859\r
5405e9a6 860/**\r
861 Request Ip4OnArpResolvedDpc as a DPC at TPL_CALLBACK.\r
862\r
3e8c18da 863 @param Event The Arp request event.\r
864 @param Context The context of the callback, a point to the ARP\r
865 queue.\r
866 \r
5405e9a6 867**/\r
36ee91ca 868VOID\r
869EFIAPI\r
870Ip4OnArpResolved (\r
871 IN EFI_EVENT Event,\r
872 IN VOID *Context\r
873 )\r
36ee91ca 874{\r
875 //\r
876 // Request Ip4OnArpResolvedDpc as a DPC at TPL_CALLBACK\r
877 //\r
d8d26fb2 878 QueueDpc (TPL_CALLBACK, Ip4OnArpResolvedDpc, Context);\r
36ee91ca 879}\r
880\r
881\r
772db4bb 882\r
883/**\r
884 Callback funtion when frame transmission is finished. It will\r
885 call the frame owner's callback function to tell it the result.\r
886\r
3e8c18da 887 @param[in] Context Context which is point to the token.\r
772db4bb 888\r
889**/\r
772db4bb 890VOID\r
891EFIAPI\r
36ee91ca 892Ip4OnFrameSentDpc (\r
772db4bb 893 IN VOID *Context\r
894 )\r
895{\r
896 IP4_LINK_TX_TOKEN *Token;\r
897\r
898 Token = (IP4_LINK_TX_TOKEN *) Context;\r
899 NET_CHECK_SIGNATURE (Token, IP4_FRAME_TX_SIGNATURE);\r
900\r
e48e37fc 901 RemoveEntryList (&Token->Link);\r
772db4bb 902\r
903 Token->CallBack (\r
904 Token->IpInstance,\r
905 Token->Packet,\r
906 Token->MnpToken.Status,\r
907 0,\r
908 Token->Context\r
909 );\r
910\r
911 Ip4FreeLinkTxToken (Token);\r
912}\r
913\r
5405e9a6 914/**\r
915 Request Ip4OnFrameSentDpc as a DPC at TPL_CALLBACK.\r
916\r
3e8c18da 917 @param[in] Event The transmit token's event.\r
918 @param[in] Context Context which is point to the token.\r
5405e9a6 919\r
920**/\r
36ee91ca 921VOID\r
922EFIAPI\r
923Ip4OnFrameSent (\r
924 IN EFI_EVENT Event,\r
925 IN VOID *Context\r
926 )\r
36ee91ca 927{\r
928 //\r
929 // Request Ip4OnFrameSentDpc as a DPC at TPL_CALLBACK\r
930 //\r
d8d26fb2 931 QueueDpc (TPL_CALLBACK, Ip4OnFrameSentDpc, Context);\r
36ee91ca 932}\r
933\r
772db4bb 934\r
935\r
936/**\r
937 Send a frame from the interface. If the next hop is broadcast or\r
938 multicast address, it is transmitted immediately. If the next hop\r
939 is a unicast, it will consult ARP to resolve the NextHop's MAC.\r
940 If some error happened, the CallBack won't be called. So, the caller\r
941 must test the return value, and take action when there is an error.\r
942\r
3e8c18da 943 @param[in] Interface The interface to send the frame from\r
944 @param[in] IpInstance The IP child that request the transmission. NULL\r
772db4bb 945 if it is the IP4 driver itself.\r
3e8c18da 946 @param[in] Packet The packet to transmit.\r
947 @param[in] NextHop The immediate destination to transmit the packet\r
772db4bb 948 to.\r
3e8c18da 949 @param[in] CallBack Function to call back when transmit finished.\r
950 @param[in] Context Opaque parameter to the call back.\r
772db4bb 951\r
952 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource to send the frame\r
953 @retval EFI_NO_MAPPING Can't resolve the MAC for the nexthop\r
954 @retval EFI_SUCCESS The packet is successfully transmitted.\r
5405e9a6 955 @retval other Other error occurs.\r
772db4bb 956\r
957**/\r
958EFI_STATUS\r
959Ip4SendFrame (\r
960 IN IP4_INTERFACE *Interface,\r
24af132f 961 IN IP4_PROTOCOL *IpInstance OPTIONAL,\r
772db4bb 962 IN NET_BUF *Packet,\r
963 IN IP4_ADDR NextHop,\r
964 IN IP4_FRAME_CALLBACK CallBack,\r
965 IN VOID *Context\r
966 )\r
967{\r
968 IP4_LINK_TX_TOKEN *Token;\r
e48e37fc 969 LIST_ENTRY *Entry;\r
772db4bb 970 IP4_ARP_QUE *ArpQue;\r
971 EFI_ARP_PROTOCOL *Arp;\r
972 EFI_STATUS Status;\r
973\r
974 ASSERT (Interface->Configured);\r
975\r
976 Token = Ip4WrapLinkTxToken (Interface, IpInstance, Packet, CallBack, Context);\r
977\r
978 if (Token == NULL) {\r
979 return EFI_OUT_OF_RESOURCES;\r
980 }\r
981\r
982 //\r
983 // Get the destination MAC address for multicast and broadcasts.\r
984 // Don't depend on ARP to solve the address since there maybe no\r
985 // ARP at all. Ip4Output has set NextHop to 255.255.255.255 for\r
986 // all the broadcasts.\r
987 //\r
988 if (NextHop == IP4_ALLONE_ADDRESS) {\r
687a2e5f 989 CopyMem (&Token->DstMac, &Interface->BroadcastMac, sizeof (Token->DstMac));\r
772db4bb 990 goto SEND_NOW;\r
991\r
992 } else if (IP4_IS_MULTICAST (NextHop)) {\r
993\r
994 Status = Ip4GetMulticastMac (Interface->Mnp, NextHop, &Token->DstMac);\r
995\r
996 if (EFI_ERROR (Status)) {\r
997 goto ON_ERROR;\r
998 }\r
999\r
1000 goto SEND_NOW;\r
1001 }\r
1002\r
1003 //\r
1004 // Can only send out multicast/broadcast if the IP address is zero\r
1005 //\r
1006 if ((Arp = Interface->Arp) == NULL) {\r
1007 Status = EFI_NO_MAPPING;\r
1008 goto ON_ERROR;\r
1009 }\r
1010\r
1011 //\r
1012 // First check whether this binding is in the ARP cache.\r
1013 //\r
1014 NextHop = HTONL (NextHop);\r
1015 Status = Arp->Request (Arp, &NextHop, NULL, &Token->DstMac);\r
1016\r
1017 if (Status == EFI_SUCCESS) {\r
1018 goto SEND_NOW;\r
1019\r
1020 } else if (Status != EFI_NOT_READY) {\r
1021 goto ON_ERROR;\r
1022 }\r
1023\r
1024 //\r
1025 // Have to do asynchronous ARP resolution. First check\r
1026 // whether there is already a pending request.\r
1027 //\r
1028 ArpQue = NULL;\r
1029\r
1030 NET_LIST_FOR_EACH (Entry, &Interface->ArpQues) {\r
1031 ArpQue = NET_LIST_USER_STRUCT (Entry, IP4_ARP_QUE, Link);\r
1032\r
1033 if (ArpQue->Ip == NextHop) {\r
1034 break;\r
1035 }\r
1036 }\r
1037\r
1038 //\r
1039 // Found a pending ARP request, enqueue the frame then return\r
1040 //\r
1041 if (Entry != &Interface->ArpQues) {\r
e48e37fc 1042 InsertTailList (&ArpQue->Frames, &Token->Link);\r
772db4bb 1043 return EFI_SUCCESS;\r
1044 }\r
1045\r
1046 //\r
1047 // First frame to NextHop, issue an asynchronous ARP requests\r
1048 //\r
1049 ArpQue = Ip4CreateArpQue (Interface, NextHop);\r
1050\r
1051 if (ArpQue == NULL) {\r
1052 Status = EFI_OUT_OF_RESOURCES;\r
1053 goto ON_ERROR;\r
1054 }\r
1055\r
1056 Status = Arp->Request (Arp, &ArpQue->Ip, ArpQue->OnResolved, ArpQue->Mac.Addr);\r
1057\r
1058 if (EFI_ERROR (Status) && (Status != EFI_NOT_READY)) {\r
1059 Ip4FreeArpQue (ArpQue, EFI_NO_MAPPING);\r
1060 goto ON_ERROR;\r
1061 }\r
1062\r
e48e37fc 1063 InsertHeadList (&ArpQue->Frames, &Token->Link);\r
1064 InsertHeadList (&Interface->ArpQues, &ArpQue->Link);\r
772db4bb 1065 return EFI_SUCCESS;\r
1066\r
1067SEND_NOW:\r
36ee91ca 1068 //\r
1069 // Insert the tx token into the SentFrames list before calling Mnp->Transmit.\r
1070 // Remove it if the returned status is not EFI_SUCCESS.\r
1071 //\r
e48e37fc 1072 InsertTailList (&Interface->SentFrames, &Token->Link);\r
772db4bb 1073 Status = Interface->Mnp->Transmit (Interface->Mnp, &Token->MnpToken);\r
772db4bb 1074 if (EFI_ERROR (Status)) {\r
e48e37fc 1075 RemoveEntryList (&Interface->SentFrames);\r
772db4bb 1076 goto ON_ERROR;\r
1077 }\r
1078\r
772db4bb 1079 return EFI_SUCCESS;\r
1080\r
1081ON_ERROR:\r
1082 Ip4FreeLinkTxToken (Token);\r
1083 return Status;\r
1084}\r
1085\r
1086\r
1087/**\r
1088 Call back function when the received packet is freed.\r
1089 Check Ip4OnFrameReceived for information.\r
1090\r
3e8c18da 1091 @param Context Context, which is the IP4_LINK_RX_TOKEN.\r
772db4bb 1092\r
1093**/\r
772db4bb 1094VOID\r
e798cd87 1095EFIAPI\r
772db4bb 1096Ip4RecycleFrame (\r
1097 IN VOID *Context\r
1098 )\r
1099{\r
1100 IP4_LINK_RX_TOKEN *Frame;\r
1101\r
1102 Frame = (IP4_LINK_RX_TOKEN *) Context;\r
1103 NET_CHECK_SIGNATURE (Frame, IP4_FRAME_RX_SIGNATURE);\r
1104\r
1105 gBS->SignalEvent (Frame->MnpToken.Packet.RxData->RecycleEvent);\r
1106 Ip4FreeFrameRxToken (Frame);\r
1107}\r
1108\r
1109\r
1110/**\r
1111 Received a frame from MNP, wrap it in net buffer then deliver\r
1112 it to IP's input function. The ownship of the packet also\r
1113 transferred to IP. When Ip is finished with this packet, it\r
1114 will call NetbufFree to release the packet, NetbufFree will\r
1115 again call the Ip4RecycleFrame to signal MNP's event and free\r
1116 the token used.\r
1117\r
772db4bb 1118 @param Context Context for the callback.\r
1119\r
772db4bb 1120**/\r
772db4bb 1121VOID\r
1122EFIAPI\r
36ee91ca 1123Ip4OnFrameReceivedDpc (\r
772db4bb 1124 IN VOID *Context\r
1125 )\r
1126{\r
1127 EFI_MANAGED_NETWORK_COMPLETION_TOKEN *MnpToken;\r
1128 EFI_MANAGED_NETWORK_RECEIVE_DATA *MnpRxData;\r
1129 IP4_LINK_RX_TOKEN *Token;\r
1130 NET_FRAGMENT Netfrag;\r
1131 NET_BUF *Packet;\r
1132 UINT32 Flag;\r
1133\r
1134 Token = (IP4_LINK_RX_TOKEN *) Context;\r
1135 NET_CHECK_SIGNATURE (Token, IP4_FRAME_RX_SIGNATURE);\r
1136\r
1137 //\r
1138 // First clear the interface's receive request in case the\r
1139 // caller wants to call Ip4ReceiveFrame in the callback.\r
1140 //\r
1141 Token->Interface->RecvRequest = NULL;\r
1142\r
1143 MnpToken = &Token->MnpToken;\r
1144 MnpRxData = MnpToken->Packet.RxData;\r
1145\r
1146 if (EFI_ERROR (MnpToken->Status) || (MnpRxData == NULL)) {\r
1147 Token->CallBack (Token->IpInstance, NULL, MnpToken->Status, 0, Token->Context);\r
1148 Ip4FreeFrameRxToken (Token);\r
1149\r
1150 return ;\r
1151 }\r
1152\r
1153 //\r
1154 // Wrap the frame in a net buffer then deliever it to IP input.\r
1155 // IP will reassemble the packet, and deliver it to upper layer\r
1156 //\r
1157 Netfrag.Len = MnpRxData->DataLength;\r
1158 Netfrag.Bulk = MnpRxData->PacketData;\r
1159\r
1160 Packet = NetbufFromExt (&Netfrag, 1, 0, IP4_MAX_HEADLEN, Ip4RecycleFrame, Token);\r
1161\r
1162 if (Packet == NULL) {\r
1163 gBS->SignalEvent (MnpRxData->RecycleEvent);\r
1164\r
1165 Token->CallBack (Token->IpInstance, NULL, EFI_OUT_OF_RESOURCES, 0, Token->Context);\r
1166 Ip4FreeFrameRxToken (Token);\r
1167\r
1168 return ;\r
1169 }\r
1170\r
1171 Flag = (MnpRxData->BroadcastFlag ? IP4_LINK_BROADCAST : 0);\r
1172 Flag |= (MnpRxData->MulticastFlag ? IP4_LINK_MULTICAST : 0);\r
1173 Flag |= (MnpRxData->PromiscuousFlag ? IP4_LINK_PROMISC : 0);\r
1174\r
1175 Token->CallBack (Token->IpInstance, Packet, EFI_SUCCESS, Flag, Token->Context);\r
1176}\r
1177\r
2ff29212 1178/**\r
2ff29212 1179 Request Ip4OnFrameReceivedDpc as a DPC at TPL_CALLBACK.\r
1180\r
1181 @param Event The receive event delivered to MNP for receive.\r
1182 @param Context Context for the callback.\r
2ff29212 1183\r
1184**/\r
36ee91ca 1185VOID\r
1186EFIAPI\r
1187Ip4OnFrameReceived (\r
1188 IN EFI_EVENT Event,\r
1189 IN VOID *Context\r
1190 )\r
36ee91ca 1191{\r
1192 //\r
1193 // Request Ip4OnFrameReceivedDpc as a DPC at TPL_CALLBACK\r
1194 //\r
d8d26fb2 1195 QueueDpc (TPL_CALLBACK, Ip4OnFrameReceivedDpc, Context);\r
36ee91ca 1196}\r
1197\r
772db4bb 1198\r
1199/**\r
1200 Request to receive the packet from the interface.\r
1201\r
3e8c18da 1202 @param[in] Interface The interface to receive the frames from\r
1203 @param[in] IpInstance The instance that requests the receive. NULL for\r
772db4bb 1204 the driver itself.\r
3e8c18da 1205 @param[in] CallBack Function to call when receive finished.\r
1206 @param[in] Context Opaque parameter to the callback\r
772db4bb 1207\r
1208 @retval EFI_ALREADY_STARTED There is already a pending receive request.\r
1209 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource to receive\r
1210 @retval EFI_SUCCESS The recieve request has been started.\r
5405e9a6 1211 @retval other Other error occurs.\r
772db4bb 1212\r
1213**/\r
1214EFI_STATUS\r
1215Ip4ReceiveFrame (\r
1216 IN IP4_INTERFACE *Interface,\r
24af132f 1217 IN IP4_PROTOCOL *IpInstance OPTIONAL,\r
772db4bb 1218 IN IP4_FRAME_CALLBACK CallBack,\r
1219 IN VOID *Context\r
1220 )\r
1221{\r
1222 IP4_LINK_RX_TOKEN *Token;\r
1223 EFI_STATUS Status;\r
1224\r
1225 NET_CHECK_SIGNATURE (Interface, IP4_INTERFACE_SIGNATURE);\r
1226\r
1227 if (Interface->RecvRequest != NULL) {\r
1228 return EFI_ALREADY_STARTED;\r
1229 }\r
1230\r
1231 Token = Ip4CreateLinkRxToken (Interface, IpInstance, CallBack, Context);\r
1232\r
1233 if (Token == NULL) {\r
1234 return EFI_OUT_OF_RESOURCES;\r
1235 }\r
1236\r
36ee91ca 1237 Interface->RecvRequest = Token;\r
772db4bb 1238 Status = Interface->Mnp->Receive (Interface->Mnp, &Token->MnpToken);\r
772db4bb 1239 if (EFI_ERROR (Status)) {\r
36ee91ca 1240 Interface->RecvRequest = NULL;\r
772db4bb 1241 Ip4FreeFrameRxToken (Token);\r
1242 return Status;\r
1243 }\r
772db4bb 1244 return EFI_SUCCESS;\r
1245}\r