Ip4If.c 37 KB

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