Ip4Impl.c 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311
  1. /** @file
  2. Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.<BR>
  3. SPDX-License-Identifier: BSD-2-Clause-Patent
  4. **/
  5. #include "Ip4Impl.h"
  6. EFI_IPSEC2_PROTOCOL *mIpSec = NULL;
  7. /**
  8. Gets the current operational settings for this instance of the EFI IPv4 Protocol driver.
  9. The GetModeData() function returns the current operational mode data for this
  10. driver instance. The data fields in EFI_IP4_MODE_DATA are read only. This
  11. function is used optionally to retrieve the operational mode data of underlying
  12. networks or drivers.
  13. @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.
  14. @param[out] Ip4ModeData Pointer to the EFI IPv4 Protocol mode data structure.
  15. @param[out] MnpConfigData Pointer to the managed network configuration data structure.
  16. @param[out] SnpModeData Pointer to the simple network mode data structure.
  17. @retval EFI_SUCCESS The operation completed successfully.
  18. @retval EFI_INVALID_PARAMETER This is NULL.
  19. @retval EFI_OUT_OF_RESOURCES The required mode data could not be allocated.
  20. **/
  21. EFI_STATUS
  22. EFIAPI
  23. EfiIp4GetModeData (
  24. IN CONST EFI_IP4_PROTOCOL *This,
  25. OUT EFI_IP4_MODE_DATA *Ip4ModeData OPTIONAL,
  26. OUT EFI_MANAGED_NETWORK_CONFIG_DATA *MnpConfigData OPTIONAL,
  27. OUT EFI_SIMPLE_NETWORK_MODE *SnpModeData OPTIONAL
  28. );
  29. /**
  30. Assigns an IPv4 address and subnet mask to this EFI IPv4 Protocol driver instance.
  31. The Configure() function is used to set, change, or reset the operational
  32. parameters and filter settings for this EFI IPv4 Protocol instance. Until these
  33. parameters have been set, no network traffic can be sent or received by this
  34. instance. Once the parameters have been reset (by calling this function with
  35. IpConfigData set to NULL), no more traffic can be sent or received until these
  36. parameters have been set again. Each EFI IPv4 Protocol instance can be started
  37. and stopped independently of each other by enabling or disabling their receive
  38. filter settings with the Configure() function.
  39. When IpConfigData.UseDefaultAddress is set to FALSE, the new station address will
  40. be appended as an alias address into the addresses list in the EFI IPv4 Protocol
  41. driver. While set to TRUE, Configure() will trigger the EFI_IP4_CONFIG_PROTOCOL
  42. to retrieve the default IPv4 address if it is not available yet. Clients could
  43. frequently call GetModeData() to check the status to ensure that the default IPv4
  44. address is ready.
  45. If operational parameters are reset or changed, any pending transmit and receive
  46. requests will be cancelled. Their completion token status will be set to EFI_ABORTED
  47. and their events will be signaled.
  48. @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.
  49. @param[in] IpConfigData Pointer to the EFI IPv4 Protocol configuration data structure.
  50. @retval EFI_SUCCESS The driver instance was successfully opened.
  51. @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP,
  52. RARP, etc.) is not finished yet.
  53. @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
  54. @retval EFI_UNSUPPORTED One or more of the following conditions is TRUE:
  55. A configuration protocol (DHCP, BOOTP, RARP, etc.) could
  56. not be located when clients choose to use the default IPv4
  57. address. This EFI IPv4 Protocol implementation does not
  58. support this requested filter or timeout setting.
  59. @retval EFI_OUT_OF_RESOURCES The EFI IPv4 Protocol driver instance data could not be allocated.
  60. @retval EFI_ALREADY_STARTED The interface is already open and must be stopped before the
  61. IPv4 address or subnet mask can be changed. The interface must
  62. also be stopped when switching to/from raw packet mode.
  63. @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. The EFI IPv4
  64. Protocol driver instance is not opened.
  65. **/
  66. EFI_STATUS
  67. EFIAPI
  68. EfiIp4Configure (
  69. IN EFI_IP4_PROTOCOL *This,
  70. IN EFI_IP4_CONFIG_DATA *IpConfigData OPTIONAL
  71. );
  72. /**
  73. Joins and leaves multicast groups.
  74. The Groups() function is used to join and leave multicast group sessions. Joining
  75. a group will enable reception of matching multicast packets. Leaving a group will
  76. disable the multicast packet reception.
  77. If JoinFlag is FALSE and GroupAddress is NULL, all joined groups will be left.
  78. @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.
  79. @param[in] JoinFlag Set to TRUE to join the multicast group session and FALSE to leave.
  80. @param[in] GroupAddress Pointer to the IPv4 multicast address.
  81. @retval EFI_SUCCESS The operation completed successfully.
  82. @retval EFI_INVALID_PARAMETER One or more of the following is TRUE:
  83. - This is NULL.
  84. - JoinFlag is TRUE and GroupAddress is NULL.
  85. - GroupAddress is not NULL and *GroupAddress is
  86. not a multicast IPv4 address.
  87. @retval EFI_NOT_STARTED This instance has not been started.
  88. @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP,
  89. RARP, etc.) is not finished yet.
  90. @retval EFI_OUT_OF_RESOURCES System resources could not be allocated.
  91. @retval EFI_UNSUPPORTED This EFI IPv4 Protocol implementation does not support multicast groups.
  92. @retval EFI_ALREADY_STARTED The group address is already in the group table (when
  93. JoinFlag is TRUE).
  94. @retval EFI_NOT_FOUND The group address is not in the group table (when JoinFlag is FALSE).
  95. @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
  96. **/
  97. EFI_STATUS
  98. EFIAPI
  99. EfiIp4Groups (
  100. IN EFI_IP4_PROTOCOL *This,
  101. IN BOOLEAN JoinFlag,
  102. IN EFI_IPv4_ADDRESS *GroupAddress OPTIONAL
  103. );
  104. /**
  105. Adds and deletes routing table entries.
  106. The Routes() function adds a route to or deletes a route from the routing table.
  107. Routes are determined by comparing the SubnetAddress with the destination IPv4
  108. address arithmetically AND-ed with the SubnetMask. The gateway address must be
  109. on the same subnet as the configured station address.
  110. The default route is added with SubnetAddress and SubnetMask both set to 0.0.0.0.
  111. The default route matches all destination IPv4 addresses that do not match any
  112. other routes.
  113. A GatewayAddress that is zero is a nonroute. Packets are sent to the destination
  114. IP address if it can be found in the ARP cache or on the local subnet. One automatic
  115. nonroute entry will be inserted into the routing table for outgoing packets that
  116. are addressed to a local subnet (gateway address of 0.0.0.0).
  117. Each EFI IPv4 Protocol instance has its own independent routing table. Those EFI
  118. IPv4 Protocol instances that use the default IPv4 address will also have copies
  119. of the routing table that was provided by the EFI_IP4_CONFIG_PROTOCOL, and these
  120. copies will be updated whenever the EIF IPv4 Protocol driver reconfigures its
  121. instances. As a result, client modification to the routing table will be lost.
  122. @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.
  123. @param[in] DeleteRoute Set to TRUE to delete this route from the routing table. Set to
  124. FALSE to add this route to the routing table. SubnetAddress
  125. and SubnetMask are used as the key to each route entry.
  126. @param[in] SubnetAddress The address of the subnet that needs to be routed.
  127. @param[in] SubnetMask The subnet mask of SubnetAddress.
  128. @param[in] GatewayAddress The unicast gateway IPv4 address for this route.
  129. @retval EFI_SUCCESS The operation completed successfully.
  130. @retval EFI_NOT_STARTED The driver instance has not been started.
  131. @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP,
  132. RARP, etc.) is not finished yet.
  133. @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
  134. - This is NULL.
  135. - SubnetAddress is NULL.
  136. - SubnetMask is NULL.
  137. - GatewayAddress is NULL.
  138. - *SubnetAddress is not a valid subnet address.
  139. - *SubnetMask is not a valid subnet mask.
  140. - *GatewayAddress is not a valid unicast IPv4 address.
  141. @retval EFI_OUT_OF_RESOURCES Could not add the entry to the routing table.
  142. @retval EFI_NOT_FOUND This route is not in the routing table (when DeleteRoute is TRUE).
  143. @retval EFI_ACCESS_DENIED The route is already defined in the routing table (when
  144. DeleteRoute is FALSE).
  145. **/
  146. EFI_STATUS
  147. EFIAPI
  148. EfiIp4Routes (
  149. IN EFI_IP4_PROTOCOL *This,
  150. IN BOOLEAN DeleteRoute,
  151. IN EFI_IPv4_ADDRESS *SubnetAddress,
  152. IN EFI_IPv4_ADDRESS *SubnetMask,
  153. IN EFI_IPv4_ADDRESS *GatewayAddress
  154. );
  155. /**
  156. Places outgoing data packets into the transmit queue.
  157. The Transmit() function places a sending request in the transmit queue of this
  158. EFI IPv4 Protocol instance. Whenever the packet in the token is sent out or some
  159. errors occur, the event in the token will be signaled and the status is updated.
  160. @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.
  161. @param[in] Token Pointer to the transmit token.
  162. @retval EFI_SUCCESS The data has been queued for transmission.
  163. @retval EFI_NOT_STARTED This instance has not been started.
  164. @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP,
  165. RARP, etc.) is not finished yet.
  166. @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
  167. @retval EFI_ACCESS_DENIED The transmit completion token with the same Token.Event
  168. was already in the transmit queue.
  169. @retval EFI_NOT_READY The completion token could not be queued because the transmit
  170. queue is full.
  171. @retval EFI_NOT_FOUND Not route is found to destination address.
  172. @retval EFI_OUT_OF_RESOURCES Could not queue the transmit data.
  173. @retval EFI_BUFFER_TOO_SMALL Token.Packet.TxData.TotalDataLength is too
  174. short to transmit.
  175. @retval EFI_BAD_BUFFER_SIZE The length of the IPv4 header + option length + total data length is
  176. greater than MTU (or greater than the maximum packet size if
  177. Token.Packet.TxData.OverrideData.
  178. DoNotFragment is TRUE.)
  179. **/
  180. EFI_STATUS
  181. EFIAPI
  182. EfiIp4Transmit (
  183. IN EFI_IP4_PROTOCOL *This,
  184. IN EFI_IP4_COMPLETION_TOKEN *Token
  185. );
  186. /**
  187. Places a receiving request into the receiving queue.
  188. The Receive() function places a completion token into the receive packet queue.
  189. This function is always asynchronous.
  190. The Token.Event field in the completion token must be filled in by the caller
  191. and cannot be NULL. When the receive operation completes, the EFI IPv4 Protocol
  192. driver updates the Token.Status and Token.Packet.RxData fields and the Token.Event
  193. is signaled.
  194. @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.
  195. @param[in] Token Pointer to a token that is associated with the receive data descriptor.
  196. @retval EFI_SUCCESS The receive completion token was cached.
  197. @retval EFI_NOT_STARTED This EFI IPv4 Protocol instance has not been started.
  198. @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP, RARP, etc.)
  199. is not finished yet.
  200. @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
  201. - This is NULL.
  202. - Token is NULL.
  203. - Token.Event is NULL.
  204. @retval EFI_OUT_OF_RESOURCES The receive completion token could not be queued due to a lack of system
  205. resources (usually memory).
  206. @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
  207. The EFI IPv4 Protocol instance has been reset to startup defaults.
  208. EFI_ACCESS_DENIED The receive completion token with the same Token.Event was already
  209. in the receive queue.
  210. @retval EFI_NOT_READY The receive request could not be queued because the receive queue is full.
  211. @retval EFI_ICMP_ERROR An ICMP error packet was received.
  212. **/
  213. EFI_STATUS
  214. EFIAPI
  215. EfiIp4Receive (
  216. IN EFI_IP4_PROTOCOL *This,
  217. IN EFI_IP4_COMPLETION_TOKEN *Token
  218. );
  219. /**
  220. Abort an asynchronous transmit or receive request.
  221. The Cancel() function is used to abort a pending transmit or receive request.
  222. If the token is in the transmit or receive request queues, after calling this
  223. function, Token->Status will be set to EFI_ABORTED and then Token->Event will
  224. be signaled. If the token is not in one of the queues, which usually means the
  225. asynchronous operation has completed, this function will not signal the token
  226. and EFI_NOT_FOUND is returned.
  227. @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.
  228. @param[in] Token Pointer to a token that has been issued by
  229. EFI_IP4_PROTOCOL.Transmit() or
  230. EFI_IP4_PROTOCOL.Receive(). If NULL, all pending
  231. tokens are aborted. Type EFI_IP4_COMPLETION_TOKEN is
  232. defined in EFI_IP4_PROTOCOL.Transmit().
  233. @retval EFI_SUCCESS The asynchronous I/O request was aborted and
  234. Token.->Event was signaled. When Token is NULL, all
  235. pending requests were aborted and their events were signaled.
  236. @retval EFI_INVALID_PARAMETER This is NULL.
  237. @retval EFI_NOT_STARTED This instance has not been started.
  238. @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP,
  239. RARP, etc.) is not finished yet.
  240. @retval EFI_NOT_FOUND When Token is not NULL, the asynchronous I/O request was
  241. not found in the transmit or receive queue. It has either completed
  242. or was not issued by Transmit() and Receive().
  243. **/
  244. EFI_STATUS
  245. EFIAPI
  246. EfiIp4Cancel (
  247. IN EFI_IP4_PROTOCOL *This,
  248. IN EFI_IP4_COMPLETION_TOKEN *Token OPTIONAL
  249. );
  250. /**
  251. Polls for incoming data packets and processes outgoing data packets.
  252. The Poll() function polls for incoming data packets and processes outgoing data
  253. packets. Network drivers and applications can call the EFI_IP4_PROTOCOL.Poll()
  254. function to increase the rate that data packets are moved between the communications
  255. device and the transmit and receive queues.
  256. In some systems the periodic timer event may not poll the underlying communications
  257. device fast enough to transmit and/or receive all data packets without missing
  258. incoming packets or dropping outgoing packets. Drivers and applications that are
  259. experiencing packet loss should try calling the EFI_IP4_PROTOCOL.Poll() function
  260. more often.
  261. @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.
  262. @retval EFI_SUCCESS Incoming or outgoing data was processed.
  263. @retval EFI_NOT_STARTED This EFI IPv4 Protocol instance has not been started.
  264. @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP,
  265. RARP, etc.) is not finished yet.
  266. @retval EFI_INVALID_PARAMETER This is NULL.
  267. @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
  268. @retval EFI_NOT_READY No incoming or outgoing data is processed.
  269. @retval EFI_TIMEOUT Data was dropped out of the transmit and/or receive queue.
  270. Consider increasing the polling rate.
  271. **/
  272. EFI_STATUS
  273. EFIAPI
  274. EfiIp4Poll (
  275. IN EFI_IP4_PROTOCOL *This
  276. );
  277. EFI_IP4_PROTOCOL
  278. mEfiIp4ProtocolTemplete = {
  279. EfiIp4GetModeData,
  280. EfiIp4Configure,
  281. EfiIp4Groups,
  282. EfiIp4Routes,
  283. EfiIp4Transmit,
  284. EfiIp4Receive,
  285. EfiIp4Cancel,
  286. EfiIp4Poll
  287. };
  288. /**
  289. Gets the current operational settings for this instance of the EFI IPv4 Protocol driver.
  290. The GetModeData() function returns the current operational mode data for this
  291. driver instance. The data fields in EFI_IP4_MODE_DATA are read only. This
  292. function is used optionally to retrieve the operational mode data of underlying
  293. networks or drivers.
  294. @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.
  295. @param[out] Ip4ModeData Pointer to the EFI IPv4 Protocol mode data structure.
  296. @param[out] MnpConfigData Pointer to the managed network configuration data structure.
  297. @param[out] SnpModeData Pointer to the simple network mode data structure.
  298. @retval EFI_SUCCESS The operation completed successfully.
  299. @retval EFI_INVALID_PARAMETER This is NULL.
  300. @retval EFI_OUT_OF_RESOURCES The required mode data could not be allocated.
  301. **/
  302. EFI_STATUS
  303. EFIAPI
  304. EfiIp4GetModeData (
  305. IN CONST EFI_IP4_PROTOCOL *This,
  306. OUT EFI_IP4_MODE_DATA *Ip4ModeData OPTIONAL,
  307. OUT EFI_MANAGED_NETWORK_CONFIG_DATA *MnpConfigData OPTIONAL,
  308. OUT EFI_SIMPLE_NETWORK_MODE *SnpModeData OPTIONAL
  309. )
  310. {
  311. IP4_PROTOCOL *IpInstance;
  312. IP4_SERVICE *IpSb;
  313. EFI_IP4_CONFIG_DATA *Config;
  314. EFI_STATUS Status;
  315. EFI_TPL OldTpl;
  316. IP4_ADDR Ip;
  317. if (This == NULL) {
  318. return EFI_INVALID_PARAMETER;
  319. }
  320. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  321. IpInstance = IP4_INSTANCE_FROM_PROTOCOL (This);
  322. IpSb = IpInstance->Service;
  323. if (Ip4ModeData != NULL) {
  324. //
  325. // IsStarted is "whether the EfiIp4Configure has been called".
  326. // IsConfigured is "whether the station address has been configured"
  327. //
  328. Ip4ModeData->IsStarted = (BOOLEAN)(IpInstance->State == IP4_STATE_CONFIGED);
  329. CopyMem (&Ip4ModeData->ConfigData, &IpInstance->ConfigData, sizeof (Ip4ModeData->ConfigData));
  330. Ip4ModeData->IsConfigured = FALSE;
  331. Ip4ModeData->GroupCount = IpInstance->GroupCount;
  332. Ip4ModeData->GroupTable = (EFI_IPv4_ADDRESS *)IpInstance->Groups;
  333. Ip4ModeData->IcmpTypeCount = 23;
  334. Ip4ModeData->IcmpTypeList = mIp4SupportedIcmp;
  335. Ip4ModeData->RouteTable = NULL;
  336. Ip4ModeData->RouteCount = 0;
  337. Ip4ModeData->MaxPacketSize = IpSb->MaxPacketSize;
  338. //
  339. // return the current station address for this IP child. So,
  340. // the user can get the default address through this. Some
  341. // application wants to know it station address even it is
  342. // using the default one, such as a ftp server.
  343. //
  344. if (Ip4ModeData->IsStarted) {
  345. Config = &Ip4ModeData->ConfigData;
  346. Ip = HTONL (IpInstance->Interface->Ip);
  347. CopyMem (&Config->StationAddress, &Ip, sizeof (EFI_IPv4_ADDRESS));
  348. Ip = HTONL (IpInstance->Interface->SubnetMask);
  349. CopyMem (&Config->SubnetMask, &Ip, sizeof (EFI_IPv4_ADDRESS));
  350. Ip4ModeData->IsConfigured = IpInstance->Interface->Configured;
  351. //
  352. // Build a EFI route table for user from the internal route table.
  353. //
  354. Status = Ip4BuildEfiRouteTable (IpInstance);
  355. if (EFI_ERROR (Status)) {
  356. gBS->RestoreTPL (OldTpl);
  357. return Status;
  358. }
  359. Ip4ModeData->RouteTable = IpInstance->EfiRouteTable;
  360. Ip4ModeData->RouteCount = IpInstance->EfiRouteCount;
  361. }
  362. }
  363. //
  364. // Get fresh mode data from MNP, since underlying media status may change
  365. //
  366. Status = IpSb->Mnp->GetModeData (IpSb->Mnp, MnpConfigData, SnpModeData);
  367. gBS->RestoreTPL (OldTpl);
  368. return Status;
  369. }
  370. /**
  371. Config the MNP parameter used by IP. The IP driver use one MNP
  372. child to transmit/receive frames. By default, it configures MNP
  373. to receive unicast/multicast/broadcast. And it will enable/disable
  374. the promiscous receive according to whether there is IP child
  375. enable that or not. If Force is FALSE, it will iterate through
  376. all the IP children to check whether the promiscuous receive
  377. setting has been changed. If it hasn't been changed, it won't
  378. reconfigure the MNP. If Force is TRUE, the MNP is configured no
  379. matter whether that is changed or not.
  380. @param[in] IpSb The IP4 service instance that is to be changed.
  381. @param[in] Force Force the configuration or not.
  382. @retval EFI_SUCCESS The MNP is successfully configured/reconfigured.
  383. @retval Others Configuration failed.
  384. **/
  385. EFI_STATUS
  386. Ip4ServiceConfigMnp (
  387. IN IP4_SERVICE *IpSb,
  388. IN BOOLEAN Force
  389. )
  390. {
  391. LIST_ENTRY *Entry;
  392. LIST_ENTRY *ProtoEntry;
  393. IP4_INTERFACE *IpIf;
  394. IP4_PROTOCOL *IpInstance;
  395. BOOLEAN Reconfig;
  396. BOOLEAN PromiscReceive;
  397. EFI_STATUS Status;
  398. Reconfig = FALSE;
  399. PromiscReceive = FALSE;
  400. if (!Force) {
  401. //
  402. // Iterate through the IP children to check whether promiscuous
  403. // receive setting has been changed. Update the interface's receive
  404. // filter also.
  405. //
  406. NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) {
  407. IpIf = NET_LIST_USER_STRUCT (Entry, IP4_INTERFACE, Link);
  408. IpIf->PromiscRecv = FALSE;
  409. NET_LIST_FOR_EACH (ProtoEntry, &IpIf->IpInstances) {
  410. IpInstance = NET_LIST_USER_STRUCT (ProtoEntry, IP4_PROTOCOL, AddrLink);
  411. if (IpInstance->ConfigData.AcceptPromiscuous) {
  412. IpIf->PromiscRecv = TRUE;
  413. PromiscReceive = TRUE;
  414. }
  415. }
  416. }
  417. //
  418. // If promiscuous receive isn't changed, it isn't necessary to reconfigure.
  419. //
  420. if (PromiscReceive == IpSb->MnpConfigData.EnablePromiscuousReceive) {
  421. return EFI_SUCCESS;
  422. }
  423. Reconfig = TRUE;
  424. IpSb->MnpConfigData.EnablePromiscuousReceive = PromiscReceive;
  425. }
  426. Status = IpSb->Mnp->Configure (IpSb->Mnp, &IpSb->MnpConfigData);
  427. //
  428. // recover the original configuration if failed to set the configure.
  429. //
  430. if (EFI_ERROR (Status) && Reconfig) {
  431. IpSb->MnpConfigData.EnablePromiscuousReceive = (BOOLEAN) !PromiscReceive;
  432. }
  433. return Status;
  434. }
  435. /**
  436. Initialize the IP4_PROTOCOL structure to the unconfigured states.
  437. @param IpSb The IP4 service instance.
  438. @param IpInstance The IP4 child instance.
  439. **/
  440. VOID
  441. Ip4InitProtocol (
  442. IN IP4_SERVICE *IpSb,
  443. IN OUT IP4_PROTOCOL *IpInstance
  444. )
  445. {
  446. ASSERT ((IpSb != NULL) && (IpInstance != NULL));
  447. ZeroMem (IpInstance, sizeof (IP4_PROTOCOL));
  448. IpInstance->Signature = IP4_PROTOCOL_SIGNATURE;
  449. CopyMem (&IpInstance->Ip4Proto, &mEfiIp4ProtocolTemplete, sizeof (IpInstance->Ip4Proto));
  450. IpInstance->State = IP4_STATE_UNCONFIGED;
  451. IpInstance->InDestroy = FALSE;
  452. IpInstance->Service = IpSb;
  453. InitializeListHead (&IpInstance->Link);
  454. NetMapInit (&IpInstance->RxTokens);
  455. NetMapInit (&IpInstance->TxTokens);
  456. InitializeListHead (&IpInstance->Received);
  457. InitializeListHead (&IpInstance->Delivered);
  458. InitializeListHead (&IpInstance->AddrLink);
  459. EfiInitializeLock (&IpInstance->RecycleLock, TPL_NOTIFY);
  460. }
  461. /**
  462. Configure the IP4 child. If the child is already configured,
  463. change the configuration parameter. Otherwise configure it
  464. for the first time. The caller should validate the configuration
  465. before deliver them to it. It also don't do configure NULL.
  466. @param[in, out] IpInstance The IP4 child to configure.
  467. @param[in] Config The configure data.
  468. @retval EFI_SUCCESS The IP4 child is successfully configured.
  469. @retval EFI_DEVICE_ERROR Failed to free the pending transive or to
  470. configure underlying MNP or other errors.
  471. @retval EFI_NO_MAPPING The IP4 child is configured to use default
  472. address, but the default address hasn't been
  473. configured. The IP4 child doesn't need to be
  474. reconfigured when default address is configured.
  475. @retval EFI_OUT_OF_RESOURCES No more memory space is available.
  476. @retval other Other error occurs.
  477. **/
  478. EFI_STATUS
  479. Ip4ConfigProtocol (
  480. IN OUT IP4_PROTOCOL *IpInstance,
  481. IN EFI_IP4_CONFIG_DATA *Config
  482. )
  483. {
  484. IP4_SERVICE *IpSb;
  485. IP4_INTERFACE *IpIf;
  486. EFI_STATUS Status;
  487. IP4_ADDR Ip;
  488. IP4_ADDR Netmask;
  489. EFI_ARP_PROTOCOL *Arp;
  490. EFI_IP4_CONFIG2_PROTOCOL *Ip4Config2;
  491. EFI_IP4_CONFIG2_POLICY Policy;
  492. IpSb = IpInstance->Service;
  493. Ip4Config2 = NULL;
  494. //
  495. // User is changing packet filters. It must be stopped
  496. // before the station address can be changed.
  497. //
  498. if (IpInstance->State == IP4_STATE_CONFIGED) {
  499. //
  500. // Cancel all the pending transmit/receive from upper layer
  501. //
  502. Status = Ip4Cancel (IpInstance, NULL);
  503. if (EFI_ERROR (Status)) {
  504. return EFI_DEVICE_ERROR;
  505. }
  506. CopyMem (&IpInstance->ConfigData, Config, sizeof (IpInstance->ConfigData));
  507. return EFI_SUCCESS;
  508. }
  509. //
  510. // Configure a fresh IP4 protocol instance. Create a route table.
  511. // Each IP child has its own route table, which may point to the
  512. // default table if it is using default address.
  513. //
  514. Status = EFI_OUT_OF_RESOURCES;
  515. IpInstance->RouteTable = Ip4CreateRouteTable ();
  516. if (IpInstance->RouteTable == NULL) {
  517. return Status;
  518. }
  519. //
  520. // Set up the interface.
  521. //
  522. CopyMem (&Ip, &Config->StationAddress, sizeof (IP4_ADDR));
  523. CopyMem (&Netmask, &Config->SubnetMask, sizeof (IP4_ADDR));
  524. Ip = NTOHL (Ip);
  525. Netmask = NTOHL (Netmask);
  526. if (!Config->UseDefaultAddress) {
  527. //
  528. // Find whether there is already an interface with the same
  529. // station address. All the instances with the same station
  530. // address shares one interface.
  531. //
  532. IpIf = Ip4FindStationAddress (IpSb, Ip, Netmask);
  533. if (IpIf != NULL) {
  534. NET_GET_REF (IpIf);
  535. } else {
  536. IpIf = Ip4CreateInterface (IpSb->Mnp, IpSb->Controller, IpSb->Image);
  537. if (IpIf == NULL) {
  538. goto ON_ERROR;
  539. }
  540. Status = Ip4SetAddress (IpIf, Ip, Netmask);
  541. if (EFI_ERROR (Status)) {
  542. Status = EFI_DEVICE_ERROR;
  543. Ip4FreeInterface (IpIf, IpInstance);
  544. goto ON_ERROR;
  545. }
  546. InsertTailList (&IpSb->Interfaces, &IpIf->Link);
  547. }
  548. //
  549. // Add a route to this connected network in the instance route table.
  550. //
  551. Ip4AddRoute (
  552. IpInstance->RouteTable,
  553. Ip & Netmask,
  554. Netmask,
  555. IP4_ALLZERO_ADDRESS
  556. );
  557. } else {
  558. //
  559. // Use the default address. Check the state.
  560. //
  561. if (IpSb->State == IP4_SERVICE_UNSTARTED) {
  562. //
  563. // Trigger the EFI_IP4_CONFIG2_PROTOCOL to retrieve the
  564. // default IPv4 address if it is not available yet.
  565. //
  566. Policy = IpSb->Ip4Config2Instance.Policy;
  567. if (Policy != Ip4Config2PolicyDhcp) {
  568. Ip4Config2 = &IpSb->Ip4Config2Instance.Ip4Config2;
  569. Policy = Ip4Config2PolicyDhcp;
  570. Status = Ip4Config2->SetData (
  571. Ip4Config2,
  572. Ip4Config2DataTypePolicy,
  573. sizeof (EFI_IP4_CONFIG2_POLICY),
  574. &Policy
  575. );
  576. if (EFI_ERROR (Status)) {
  577. goto ON_ERROR;
  578. }
  579. }
  580. }
  581. IpIf = IpSb->DefaultInterface;
  582. NET_GET_REF (IpSb->DefaultInterface);
  583. //
  584. // If default address is used, so is the default route table.
  585. // Any route set by the instance has the precedence over the
  586. // routes in the default route table. Link the default table
  587. // after the instance's table. Routing will search the local
  588. // table first.
  589. //
  590. NET_GET_REF (IpSb->DefaultRouteTable);
  591. IpInstance->RouteTable->Next = IpSb->DefaultRouteTable;
  592. }
  593. IpInstance->Interface = IpIf;
  594. if (IpIf->Arp != NULL) {
  595. Arp = NULL;
  596. Status = gBS->OpenProtocol (
  597. IpIf->ArpHandle,
  598. &gEfiArpProtocolGuid,
  599. (VOID **)&Arp,
  600. gIp4DriverBinding.DriverBindingHandle,
  601. IpInstance->Handle,
  602. EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
  603. );
  604. if (EFI_ERROR (Status)) {
  605. Ip4FreeInterface (IpIf, IpInstance);
  606. goto ON_ERROR;
  607. }
  608. }
  609. InsertTailList (&IpIf->IpInstances, &IpInstance->AddrLink);
  610. CopyMem (&IpInstance->ConfigData, Config, sizeof (IpInstance->ConfigData));
  611. IpInstance->State = IP4_STATE_CONFIGED;
  612. //
  613. // Although EFI_NO_MAPPING is an error code, the IP child has been
  614. // successfully configured and doesn't need reconfiguration when
  615. // default address is acquired.
  616. //
  617. if (Config->UseDefaultAddress && IP4_NO_MAPPING (IpInstance)) {
  618. return EFI_NO_MAPPING;
  619. }
  620. return EFI_SUCCESS;
  621. ON_ERROR:
  622. Ip4FreeRouteTable (IpInstance->RouteTable);
  623. IpInstance->RouteTable = NULL;
  624. return Status;
  625. }
  626. /**
  627. Clean up the IP4 child, release all the resources used by it.
  628. @param[in] IpInstance The IP4 child to clean up.
  629. @retval EFI_SUCCESS The IP4 child is cleaned up.
  630. @retval EFI_DEVICE_ERROR Some resources failed to be released.
  631. **/
  632. EFI_STATUS
  633. Ip4CleanProtocol (
  634. IN IP4_PROTOCOL *IpInstance
  635. )
  636. {
  637. if (EFI_ERROR (Ip4Cancel (IpInstance, NULL))) {
  638. return EFI_DEVICE_ERROR;
  639. }
  640. if (EFI_ERROR (Ip4Groups (IpInstance, FALSE, NULL))) {
  641. return EFI_DEVICE_ERROR;
  642. }
  643. //
  644. // Some packets haven't been recycled. It is because either the
  645. // user forgets to recycle the packets, or because the callback
  646. // hasn't been called. Just leave it alone.
  647. //
  648. if (!IsListEmpty (&IpInstance->Delivered)) {
  649. }
  650. if (IpInstance->Interface != NULL) {
  651. RemoveEntryList (&IpInstance->AddrLink);
  652. if (IpInstance->Interface->Arp != NULL) {
  653. gBS->CloseProtocol (
  654. IpInstance->Interface->ArpHandle,
  655. &gEfiArpProtocolGuid,
  656. gIp4DriverBinding.DriverBindingHandle,
  657. IpInstance->Handle
  658. );
  659. }
  660. Ip4FreeInterface (IpInstance->Interface, IpInstance);
  661. IpInstance->Interface = NULL;
  662. }
  663. if (IpInstance->RouteTable != NULL) {
  664. if (IpInstance->RouteTable->Next != NULL) {
  665. Ip4FreeRouteTable (IpInstance->RouteTable->Next);
  666. }
  667. Ip4FreeRouteTable (IpInstance->RouteTable);
  668. IpInstance->RouteTable = NULL;
  669. }
  670. if (IpInstance->EfiRouteTable != NULL) {
  671. FreePool (IpInstance->EfiRouteTable);
  672. IpInstance->EfiRouteTable = NULL;
  673. IpInstance->EfiRouteCount = 0;
  674. }
  675. if (IpInstance->Groups != NULL) {
  676. FreePool (IpInstance->Groups);
  677. IpInstance->Groups = NULL;
  678. IpInstance->GroupCount = 0;
  679. }
  680. NetMapClean (&IpInstance->TxTokens);
  681. NetMapClean (&IpInstance->RxTokens);
  682. return EFI_SUCCESS;
  683. }
  684. /**
  685. Assigns an IPv4 address and subnet mask to this EFI IPv4 Protocol driver instance.
  686. The Configure() function is used to set, change, or reset the operational
  687. parameters and filter settings for this EFI IPv4 Protocol instance. Until these
  688. parameters have been set, no network traffic can be sent or received by this
  689. instance. Once the parameters have been reset (by calling this function with
  690. IpConfigData set to NULL), no more traffic can be sent or received until these
  691. parameters have been set again. Each EFI IPv4 Protocol instance can be started
  692. and stopped independently of each other by enabling or disabling their receive
  693. filter settings with the Configure() function.
  694. When IpConfigData.UseDefaultAddress is set to FALSE, the new station address will
  695. be appended as an alias address into the addresses list in the EFI IPv4 Protocol
  696. driver. While set to TRUE, Configure() will trigger the EFI_IP4_CONFIG_PROTOCOL
  697. to retrieve the default IPv4 address if it is not available yet. Clients could
  698. frequently call GetModeData() to check the status to ensure that the default IPv4
  699. address is ready.
  700. If operational parameters are reset or changed, any pending transmit and receive
  701. requests will be cancelled. Their completion token status will be set to EFI_ABORTED
  702. and their events will be signaled.
  703. @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.
  704. @param[in] IpConfigData Pointer to the EFI IPv4 Protocol configuration data structure.
  705. @retval EFI_SUCCESS The driver instance was successfully opened.
  706. @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP,
  707. RARP, etc.) is not finished yet.
  708. @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
  709. @retval EFI_UNSUPPORTED One or more of the following conditions is TRUE:
  710. A configuration protocol (DHCP, BOOTP, RARP, etc.) could
  711. not be located when clients choose to use the default IPv4
  712. address. This EFI IPv4 Protocol implementation does not
  713. support this requested filter or timeout setting.
  714. @retval EFI_OUT_OF_RESOURCES The EFI IPv4 Protocol driver instance data could not be allocated.
  715. @retval EFI_ALREADY_STARTED The interface is already open and must be stopped before the
  716. IPv4 address or subnet mask can be changed. The interface must
  717. also be stopped when switching to/from raw packet mode.
  718. @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. The EFI IPv4
  719. Protocol driver instance is not opened.
  720. **/
  721. EFI_STATUS
  722. EFIAPI
  723. EfiIp4Configure (
  724. IN EFI_IP4_PROTOCOL *This,
  725. IN EFI_IP4_CONFIG_DATA *IpConfigData OPTIONAL
  726. )
  727. {
  728. IP4_PROTOCOL *IpInstance;
  729. EFI_IP4_CONFIG_DATA *Current;
  730. EFI_TPL OldTpl;
  731. EFI_STATUS Status;
  732. BOOLEAN AddrOk;
  733. IP4_ADDR IpAddress;
  734. IP4_ADDR SubnetMask;
  735. //
  736. // First, validate the parameters
  737. //
  738. if (This == NULL) {
  739. return EFI_INVALID_PARAMETER;
  740. }
  741. IpInstance = IP4_INSTANCE_FROM_PROTOCOL (This);
  742. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  743. //
  744. // Validate the configuration first.
  745. //
  746. if (IpConfigData != NULL) {
  747. CopyMem (&IpAddress, &IpConfigData->StationAddress, sizeof (IP4_ADDR));
  748. CopyMem (&SubnetMask, &IpConfigData->SubnetMask, sizeof (IP4_ADDR));
  749. IpAddress = NTOHL (IpAddress);
  750. SubnetMask = NTOHL (SubnetMask);
  751. //
  752. // Check whether the station address is a valid unicast address
  753. //
  754. if (!IpConfigData->UseDefaultAddress) {
  755. AddrOk = Ip4StationAddressValid (IpAddress, SubnetMask);
  756. if (!AddrOk) {
  757. Status = EFI_INVALID_PARAMETER;
  758. goto ON_EXIT;
  759. }
  760. }
  761. //
  762. // User can only update packet filters when already configured.
  763. // If it wants to change the station address, it must configure(NULL)
  764. // the instance first.
  765. //
  766. if (IpInstance->State == IP4_STATE_CONFIGED) {
  767. Current = &IpInstance->ConfigData;
  768. if (Current->UseDefaultAddress != IpConfigData->UseDefaultAddress) {
  769. Status = EFI_ALREADY_STARTED;
  770. goto ON_EXIT;
  771. }
  772. if (!Current->UseDefaultAddress &&
  773. (!EFI_IP4_EQUAL (&Current->StationAddress, &IpConfigData->StationAddress) ||
  774. !EFI_IP4_EQUAL (&Current->SubnetMask, &IpConfigData->SubnetMask)))
  775. {
  776. Status = EFI_ALREADY_STARTED;
  777. goto ON_EXIT;
  778. }
  779. if (Current->UseDefaultAddress && IP4_NO_MAPPING (IpInstance)) {
  780. Status = EFI_NO_MAPPING;
  781. goto ON_EXIT;
  782. }
  783. }
  784. }
  785. //
  786. // Configure the instance or clean it up.
  787. //
  788. if (IpConfigData != NULL) {
  789. Status = Ip4ConfigProtocol (IpInstance, IpConfigData);
  790. } else {
  791. Status = Ip4CleanProtocol (IpInstance);
  792. //
  793. // Consider the following valid sequence: Mnp is unloaded-->Ip Stopped-->Udp Stopped,
  794. // Configure (ThisIp, NULL). If the state is changed to UNCONFIGED,
  795. // the unload fails miserably.
  796. //
  797. if (IpInstance->State == IP4_STATE_CONFIGED) {
  798. IpInstance->State = IP4_STATE_UNCONFIGED;
  799. }
  800. }
  801. //
  802. // Update the MNP's configure data. Ip4ServiceConfigMnp will check
  803. // whether it is necessary to reconfigure the MNP.
  804. //
  805. Ip4ServiceConfigMnp (IpInstance->Service, FALSE);
  806. ON_EXIT:
  807. gBS->RestoreTPL (OldTpl);
  808. return Status;
  809. }
  810. /**
  811. Change the IP4 child's multicast setting. The caller
  812. should make sure that the parameters is valid.
  813. @param[in] IpInstance The IP4 child to change the setting.
  814. @param[in] JoinFlag TRUE to join the group, otherwise leave it.
  815. @param[in] GroupAddress The target group address.
  816. @retval EFI_ALREADY_STARTED Want to join the group, but already a member of it.
  817. @retval EFI_OUT_OF_RESOURCES Failed to allocate some resources.
  818. @retval EFI_DEVICE_ERROR Failed to set the group configuration.
  819. @retval EFI_SUCCESS Successfully updated the group setting.
  820. @retval EFI_NOT_FOUND Try to leave the group which it isn't a member.
  821. **/
  822. EFI_STATUS
  823. Ip4Groups (
  824. IN IP4_PROTOCOL *IpInstance,
  825. IN BOOLEAN JoinFlag,
  826. IN EFI_IPv4_ADDRESS *GroupAddress OPTIONAL
  827. )
  828. {
  829. IP4_ADDR *Members;
  830. IP4_ADDR Group;
  831. UINT32 Index;
  832. //
  833. // Add it to the instance's Groups, and join the group by IGMP.
  834. // IpInstance->Groups is in network byte order. IGMP operates in
  835. // host byte order
  836. //
  837. if (JoinFlag) {
  838. //
  839. // When JoinFlag is TRUE, GroupAddress shouldn't be NULL.
  840. //
  841. ASSERT (GroupAddress != NULL);
  842. CopyMem (&Group, GroupAddress, sizeof (IP4_ADDR));
  843. for (Index = 0; Index < IpInstance->GroupCount; Index++) {
  844. if (IpInstance->Groups[Index] == Group) {
  845. return EFI_ALREADY_STARTED;
  846. }
  847. }
  848. Members = Ip4CombineGroups (IpInstance->Groups, IpInstance->GroupCount, Group);
  849. if (Members == NULL) {
  850. return EFI_OUT_OF_RESOURCES;
  851. }
  852. if (EFI_ERROR (Ip4JoinGroup (IpInstance, NTOHL (Group)))) {
  853. FreePool (Members);
  854. return EFI_DEVICE_ERROR;
  855. }
  856. if (IpInstance->Groups != NULL) {
  857. FreePool (IpInstance->Groups);
  858. }
  859. IpInstance->Groups = Members;
  860. IpInstance->GroupCount++;
  861. return EFI_SUCCESS;
  862. }
  863. //
  864. // Leave the group. Leave all the groups if GroupAddress is NULL.
  865. // Must iterate from the end to the beginning because the GroupCount
  866. // is decremented each time an address is removed..
  867. //
  868. for (Index = IpInstance->GroupCount; Index > 0; Index--) {
  869. ASSERT (IpInstance->Groups != NULL);
  870. Group = IpInstance->Groups[Index - 1];
  871. if ((GroupAddress == NULL) || EFI_IP4_EQUAL (&Group, GroupAddress)) {
  872. if (EFI_ERROR (Ip4LeaveGroup (IpInstance, NTOHL (Group)))) {
  873. return EFI_DEVICE_ERROR;
  874. }
  875. Ip4RemoveGroupAddr (IpInstance->Groups, IpInstance->GroupCount, Group);
  876. IpInstance->GroupCount--;
  877. if (IpInstance->GroupCount == 0) {
  878. ASSERT (Index == 1);
  879. FreePool (IpInstance->Groups);
  880. IpInstance->Groups = NULL;
  881. }
  882. if (GroupAddress != NULL) {
  883. return EFI_SUCCESS;
  884. }
  885. }
  886. }
  887. return ((GroupAddress != NULL) ? EFI_NOT_FOUND : EFI_SUCCESS);
  888. }
  889. /**
  890. Joins and leaves multicast groups.
  891. The Groups() function is used to join and leave multicast group sessions. Joining
  892. a group will enable reception of matching multicast packets. Leaving a group will
  893. disable the multicast packet reception.
  894. If JoinFlag is FALSE and GroupAddress is NULL, all joined groups will be left.
  895. @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.
  896. @param[in] JoinFlag Set to TRUE to join the multicast group session and FALSE to leave.
  897. @param[in] GroupAddress Pointer to the IPv4 multicast address.
  898. @retval EFI_SUCCESS The operation completed successfully.
  899. @retval EFI_INVALID_PARAMETER One or more of the following is TRUE:
  900. - This is NULL.
  901. - JoinFlag is TRUE and GroupAddress is NULL.
  902. - GroupAddress is not NULL and *GroupAddress is
  903. not a multicast IPv4 address.
  904. @retval EFI_NOT_STARTED This instance has not been started.
  905. @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP,
  906. RARP, etc.) is not finished yet.
  907. @retval EFI_OUT_OF_RESOURCES System resources could not be allocated.
  908. @retval EFI_UNSUPPORTED This EFI IPv4 Protocol implementation does not support multicast groups.
  909. @retval EFI_ALREADY_STARTED The group address is already in the group table (when
  910. JoinFlag is TRUE).
  911. @retval EFI_NOT_FOUND The group address is not in the group table (when JoinFlag is FALSE).
  912. @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
  913. **/
  914. EFI_STATUS
  915. EFIAPI
  916. EfiIp4Groups (
  917. IN EFI_IP4_PROTOCOL *This,
  918. IN BOOLEAN JoinFlag,
  919. IN EFI_IPv4_ADDRESS *GroupAddress OPTIONAL
  920. )
  921. {
  922. IP4_PROTOCOL *IpInstance;
  923. EFI_STATUS Status;
  924. EFI_TPL OldTpl;
  925. IP4_ADDR McastIp;
  926. if ((This == NULL) || (JoinFlag && (GroupAddress == NULL))) {
  927. return EFI_INVALID_PARAMETER;
  928. }
  929. if (GroupAddress != NULL) {
  930. CopyMem (&McastIp, GroupAddress, sizeof (IP4_ADDR));
  931. if (!IP4_IS_MULTICAST (NTOHL (McastIp))) {
  932. return EFI_INVALID_PARAMETER;
  933. }
  934. }
  935. IpInstance = IP4_INSTANCE_FROM_PROTOCOL (This);
  936. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  937. if (IpInstance->State != IP4_STATE_CONFIGED) {
  938. Status = EFI_NOT_STARTED;
  939. goto ON_EXIT;
  940. }
  941. if (IpInstance->ConfigData.UseDefaultAddress && IP4_NO_MAPPING (IpInstance)) {
  942. Status = EFI_NO_MAPPING;
  943. goto ON_EXIT;
  944. }
  945. Status = Ip4Groups (IpInstance, JoinFlag, GroupAddress);
  946. ON_EXIT:
  947. gBS->RestoreTPL (OldTpl);
  948. return Status;
  949. }
  950. /**
  951. Adds and deletes routing table entries.
  952. The Routes() function adds a route to or deletes a route from the routing table.
  953. Routes are determined by comparing the SubnetAddress with the destination IPv4
  954. address arithmetically AND-ed with the SubnetMask. The gateway address must be
  955. on the same subnet as the configured station address.
  956. The default route is added with SubnetAddress and SubnetMask both set to 0.0.0.0.
  957. The default route matches all destination IPv4 addresses that do not match any
  958. other routes.
  959. A GatewayAddress that is zero is a nonroute. Packets are sent to the destination
  960. IP address if it can be found in the ARP cache or on the local subnet. One automatic
  961. nonroute entry will be inserted into the routing table for outgoing packets that
  962. are addressed to a local subnet (gateway address of 0.0.0.0).
  963. Each EFI IPv4 Protocol instance has its own independent routing table. Those EFI
  964. IPv4 Protocol instances that use the default IPv4 address will also have copies
  965. of the routing table that was provided by the EFI_IP4_CONFIG_PROTOCOL, and these
  966. copies will be updated whenever the EIF IPv4 Protocol driver reconfigures its
  967. instances. As a result, client modification to the routing table will be lost.
  968. @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.
  969. @param[in] DeleteRoute Set to TRUE to delete this route from the routing table. Set to
  970. FALSE to add this route to the routing table. SubnetAddress
  971. and SubnetMask are used as the key to each route entry.
  972. @param[in] SubnetAddress The address of the subnet that needs to be routed.
  973. @param[in] SubnetMask The subnet mask of SubnetAddress.
  974. @param[in] GatewayAddress The unicast gateway IPv4 address for this route.
  975. @retval EFI_SUCCESS The operation completed successfully.
  976. @retval EFI_NOT_STARTED The driver instance has not been started.
  977. @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP,
  978. RARP, etc.) is not finished yet.
  979. @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
  980. - This is NULL.
  981. - SubnetAddress is NULL.
  982. - SubnetMask is NULL.
  983. - GatewayAddress is NULL.
  984. - *SubnetAddress is not a valid subnet address.
  985. - *SubnetMask is not a valid subnet mask.
  986. - *GatewayAddress is not a valid unicast IPv4 address.
  987. @retval EFI_OUT_OF_RESOURCES Could not add the entry to the routing table.
  988. @retval EFI_NOT_FOUND This route is not in the routing table (when DeleteRoute is TRUE).
  989. @retval EFI_ACCESS_DENIED The route is already defined in the routing table (when
  990. DeleteRoute is FALSE).
  991. **/
  992. EFI_STATUS
  993. EFIAPI
  994. EfiIp4Routes (
  995. IN EFI_IP4_PROTOCOL *This,
  996. IN BOOLEAN DeleteRoute,
  997. IN EFI_IPv4_ADDRESS *SubnetAddress,
  998. IN EFI_IPv4_ADDRESS *SubnetMask,
  999. IN EFI_IPv4_ADDRESS *GatewayAddress
  1000. )
  1001. {
  1002. IP4_PROTOCOL *IpInstance;
  1003. IP4_INTERFACE *IpIf;
  1004. IP4_ADDR Dest;
  1005. IP4_ADDR Netmask;
  1006. IP4_ADDR Nexthop;
  1007. EFI_STATUS Status;
  1008. EFI_TPL OldTpl;
  1009. //
  1010. // First, validate the parameters
  1011. //
  1012. if ((This == NULL) || (SubnetAddress == NULL) ||
  1013. (SubnetMask == NULL) || (GatewayAddress == NULL))
  1014. {
  1015. return EFI_INVALID_PARAMETER;
  1016. }
  1017. IpInstance = IP4_INSTANCE_FROM_PROTOCOL (This);
  1018. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  1019. if (IpInstance->State != IP4_STATE_CONFIGED) {
  1020. Status = EFI_NOT_STARTED;
  1021. goto ON_EXIT;
  1022. }
  1023. if (IpInstance->ConfigData.UseDefaultAddress && IP4_NO_MAPPING (IpInstance)) {
  1024. Status = EFI_NO_MAPPING;
  1025. goto ON_EXIT;
  1026. }
  1027. CopyMem (&Dest, SubnetAddress, sizeof (IP4_ADDR));
  1028. CopyMem (&Netmask, SubnetMask, sizeof (IP4_ADDR));
  1029. CopyMem (&Nexthop, GatewayAddress, sizeof (IP4_ADDR));
  1030. Dest = NTOHL (Dest);
  1031. Netmask = NTOHL (Netmask);
  1032. Nexthop = NTOHL (Nexthop);
  1033. IpIf = IpInstance->Interface;
  1034. if (!IP4_IS_VALID_NETMASK (Netmask)) {
  1035. Status = EFI_INVALID_PARAMETER;
  1036. goto ON_EXIT;
  1037. }
  1038. //
  1039. // the gateway address must be a unicast on the connected network if not zero.
  1040. //
  1041. if ((Nexthop != IP4_ALLZERO_ADDRESS) &&
  1042. (((IpIf->SubnetMask != IP4_ALLONE_ADDRESS) && !IP4_NET_EQUAL (Nexthop, IpIf->Ip, IpIf->SubnetMask)) ||
  1043. IP4_IS_BROADCAST (Ip4GetNetCast (Nexthop, IpIf))))
  1044. {
  1045. Status = EFI_INVALID_PARAMETER;
  1046. goto ON_EXIT;
  1047. }
  1048. if (DeleteRoute) {
  1049. Status = Ip4DelRoute (IpInstance->RouteTable, Dest, Netmask, Nexthop);
  1050. } else {
  1051. Status = Ip4AddRoute (IpInstance->RouteTable, Dest, Netmask, Nexthop);
  1052. }
  1053. ON_EXIT:
  1054. gBS->RestoreTPL (OldTpl);
  1055. return Status;
  1056. }
  1057. /**
  1058. Check whether the user's token or event has already
  1059. been enqueued on IP4's list.
  1060. @param[in] Map The container of either user's transmit or receive
  1061. token.
  1062. @param[in] Item Current item to check against.
  1063. @param[in] Context The Token to check against.
  1064. @retval EFI_ACCESS_DENIED The token or event has already been enqueued in IP.
  1065. @retval EFI_SUCCESS The current item isn't the same token/event as the
  1066. context.
  1067. **/
  1068. EFI_STATUS
  1069. EFIAPI
  1070. Ip4TokenExist (
  1071. IN NET_MAP *Map,
  1072. IN NET_MAP_ITEM *Item,
  1073. IN VOID *Context
  1074. )
  1075. {
  1076. EFI_IP4_COMPLETION_TOKEN *Token;
  1077. EFI_IP4_COMPLETION_TOKEN *TokenInItem;
  1078. Token = (EFI_IP4_COMPLETION_TOKEN *)Context;
  1079. TokenInItem = (EFI_IP4_COMPLETION_TOKEN *)Item->Key;
  1080. if ((Token == TokenInItem) || (Token->Event == TokenInItem->Event)) {
  1081. return EFI_ACCESS_DENIED;
  1082. }
  1083. return EFI_SUCCESS;
  1084. }
  1085. /**
  1086. Validate the user's token against current station address.
  1087. @param[in] Token User's token to validate.
  1088. @param[in] IpIf The IP4 child's interface.
  1089. @param[in] RawData Set to TRUE to send unformatted packets.
  1090. @retval EFI_INVALID_PARAMETER Some parameters are invalid.
  1091. @retval EFI_BAD_BUFFER_SIZE The user's option/data is too long.
  1092. @retval EFI_SUCCESS The token is valid.
  1093. **/
  1094. EFI_STATUS
  1095. Ip4TxTokenValid (
  1096. IN EFI_IP4_COMPLETION_TOKEN *Token,
  1097. IN IP4_INTERFACE *IpIf,
  1098. IN BOOLEAN RawData
  1099. )
  1100. {
  1101. EFI_IP4_TRANSMIT_DATA *TxData;
  1102. EFI_IP4_OVERRIDE_DATA *Override;
  1103. IP4_ADDR Src;
  1104. IP4_ADDR Gateway;
  1105. UINT32 Offset;
  1106. UINT32 Index;
  1107. UINT32 HeadLen;
  1108. if ((Token == NULL) || (Token->Event == NULL) || (Token->Packet.TxData == NULL)) {
  1109. return EFI_INVALID_PARAMETER;
  1110. }
  1111. TxData = Token->Packet.TxData;
  1112. //
  1113. // Check the fragment table: no empty fragment, and length isn't bogus.
  1114. //
  1115. if ((TxData->TotalDataLength == 0) || (TxData->FragmentCount == 0)) {
  1116. return EFI_INVALID_PARAMETER;
  1117. }
  1118. Offset = TxData->TotalDataLength;
  1119. if (Offset > IP4_MAX_PACKET_SIZE) {
  1120. return EFI_BAD_BUFFER_SIZE;
  1121. }
  1122. for (Index = 0; Index < TxData->FragmentCount; Index++) {
  1123. if ((TxData->FragmentTable[Index].FragmentBuffer == NULL) ||
  1124. (TxData->FragmentTable[Index].FragmentLength == 0))
  1125. {
  1126. return EFI_INVALID_PARAMETER;
  1127. }
  1128. Offset -= TxData->FragmentTable[Index].FragmentLength;
  1129. }
  1130. if (Offset != 0) {
  1131. return EFI_INVALID_PARAMETER;
  1132. }
  1133. //
  1134. // NOTE that OptionsLength/OptionsBuffer/OverrideData are ignored if RawData
  1135. // is TRUE.
  1136. //
  1137. if (RawData) {
  1138. return EFI_SUCCESS;
  1139. }
  1140. //
  1141. // Check the IP options: no more than 40 bytes and format is OK
  1142. //
  1143. if (TxData->OptionsLength != 0) {
  1144. if ((TxData->OptionsLength > 40) || (TxData->OptionsBuffer == NULL)) {
  1145. return EFI_INVALID_PARAMETER;
  1146. }
  1147. if (!Ip4OptionIsValid (TxData->OptionsBuffer, TxData->OptionsLength, FALSE)) {
  1148. return EFI_INVALID_PARAMETER;
  1149. }
  1150. }
  1151. //
  1152. // Check the source and gateway: they must be a valid unicast.
  1153. // Gateway must also be on the connected network.
  1154. //
  1155. if (TxData->OverrideData != NULL) {
  1156. Override = TxData->OverrideData;
  1157. CopyMem (&Src, &Override->SourceAddress, sizeof (IP4_ADDR));
  1158. CopyMem (&Gateway, &Override->GatewayAddress, sizeof (IP4_ADDR));
  1159. Src = NTOHL (Src);
  1160. Gateway = NTOHL (Gateway);
  1161. if ((NetGetIpClass (Src) > IP4_ADDR_CLASSC) ||
  1162. (Src == IP4_ALLONE_ADDRESS) ||
  1163. IP4_IS_BROADCAST (Ip4GetNetCast (Src, IpIf)))
  1164. {
  1165. return EFI_INVALID_PARAMETER;
  1166. }
  1167. //
  1168. // If gateway isn't zero, it must be a unicast address, and
  1169. // on the connected network.
  1170. //
  1171. if ((Gateway != IP4_ALLZERO_ADDRESS) &&
  1172. ((NetGetIpClass (Gateway) > IP4_ADDR_CLASSC) ||
  1173. !IP4_NET_EQUAL (Gateway, IpIf->Ip, IpIf->SubnetMask) ||
  1174. IP4_IS_BROADCAST (Ip4GetNetCast (Gateway, IpIf))))
  1175. {
  1176. return EFI_INVALID_PARAMETER;
  1177. }
  1178. }
  1179. //
  1180. // Check the packet length: Head length and packet length all has a limit
  1181. //
  1182. HeadLen = sizeof (IP4_HEAD) + ((TxData->OptionsLength + 3) &~0x03);
  1183. if ((HeadLen > IP4_MAX_HEADLEN) ||
  1184. (TxData->TotalDataLength + HeadLen > IP4_MAX_PACKET_SIZE))
  1185. {
  1186. return EFI_BAD_BUFFER_SIZE;
  1187. }
  1188. return EFI_SUCCESS;
  1189. }
  1190. /**
  1191. The callback function for the net buffer which wraps the user's
  1192. transmit token. Although it seems this function is pretty simple,
  1193. there are some subtle things.
  1194. When user requests the IP to transmit a packet by passing it a
  1195. token, the token is wrapped in an IP4_TXTOKEN_WRAP and the data
  1196. is wrapped in an net buffer. the net buffer's Free function is
  1197. set to Ip4FreeTxToken. The Token and token wrap are added to the
  1198. IP child's TxToken map. Then the buffer is passed to Ip4Output for
  1199. transmission. If something error happened before that, the buffer
  1200. is freed, which in turn will free the token wrap. The wrap may
  1201. have been added to the TxToken map or not, and the user's event
  1202. shouldn't be fired because we are still in the EfiIp4Transmit. If
  1203. the buffer has been sent by Ip4Output, it should be removed from
  1204. the TxToken map and user's event signaled. The token wrap and buffer
  1205. are bound together. Check the comments in Ip4Output for information
  1206. about IP fragmentation.
  1207. @param[in] Context The token's wrap.
  1208. **/
  1209. VOID
  1210. EFIAPI
  1211. Ip4FreeTxToken (
  1212. IN VOID *Context
  1213. )
  1214. {
  1215. IP4_TXTOKEN_WRAP *Wrap;
  1216. NET_MAP_ITEM *Item;
  1217. Wrap = (IP4_TXTOKEN_WRAP *)Context;
  1218. //
  1219. // Signal IpSecRecycleEvent to inform IPsec free the memory
  1220. //
  1221. if (Wrap->IpSecRecycleSignal != NULL) {
  1222. gBS->SignalEvent (Wrap->IpSecRecycleSignal);
  1223. }
  1224. //
  1225. // Find the token in the instance's map. EfiIp4Transmit put the
  1226. // token to the map. If that failed, NetMapFindKey will return NULL.
  1227. //
  1228. Item = NetMapFindKey (&Wrap->IpInstance->TxTokens, Wrap->Token);
  1229. if (Item != NULL) {
  1230. NetMapRemoveItem (&Wrap->IpInstance->TxTokens, Item, NULL);
  1231. }
  1232. if (Wrap->Sent) {
  1233. gBS->SignalEvent (Wrap->Token->Event);
  1234. //
  1235. // Dispatch the DPC queued by the NotifyFunction of Token->Event.
  1236. //
  1237. DispatchDpc ();
  1238. }
  1239. FreePool (Wrap);
  1240. }
  1241. /**
  1242. The callback function to Ip4Output to update the transmit status.
  1243. @param Ip4Instance The Ip4Instance that request the transmit.
  1244. @param Packet The user's transmit request.
  1245. @param IoStatus The result of the transmission.
  1246. @param Flag Not used during transmission.
  1247. @param Context The token's wrap.
  1248. **/
  1249. VOID
  1250. Ip4OnPacketSent (
  1251. IP4_PROTOCOL *Ip4Instance,
  1252. NET_BUF *Packet,
  1253. EFI_STATUS IoStatus,
  1254. UINT32 Flag,
  1255. VOID *Context
  1256. )
  1257. {
  1258. IP4_TXTOKEN_WRAP *Wrap;
  1259. //
  1260. // This is the transmission request from upper layer,
  1261. // not the IP4 driver itself.
  1262. //
  1263. ASSERT (Ip4Instance != NULL);
  1264. //
  1265. // The first fragment of the packet has been sent. Update
  1266. // the token's status. That is, if fragmented, the transmit's
  1267. // status is the first fragment's status. The Wrap will be
  1268. // release when all the fragments are release. Check the comments
  1269. // in Ip4FreeTxToken and Ip4Output for information.
  1270. //
  1271. Wrap = (IP4_TXTOKEN_WRAP *)Context;
  1272. Wrap->Token->Status = IoStatus;
  1273. NetbufFree (Wrap->Packet);
  1274. }
  1275. /**
  1276. Places outgoing data packets into the transmit queue.
  1277. The Transmit() function places a sending request in the transmit queue of this
  1278. EFI IPv4 Protocol instance. Whenever the packet in the token is sent out or some
  1279. errors occur, the event in the token will be signaled and the status is updated.
  1280. @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.
  1281. @param[in] Token Pointer to the transmit token.
  1282. @retval EFI_SUCCESS The data has been queued for transmission.
  1283. @retval EFI_NOT_STARTED This instance has not been started.
  1284. @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP,
  1285. RARP, etc.) is not finished yet.
  1286. @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
  1287. @retval EFI_ACCESS_DENIED The transmit completion token with the same Token.Event
  1288. was already in the transmit queue.
  1289. @retval EFI_NOT_READY The completion token could not be queued because the transmit
  1290. queue is full.
  1291. @retval EFI_NOT_FOUND Not route is found to destination address.
  1292. @retval EFI_OUT_OF_RESOURCES Could not queue the transmit data.
  1293. @retval EFI_BUFFER_TOO_SMALL Token.Packet.TxData.TotalDataLength is too
  1294. short to transmit.
  1295. @retval EFI_BAD_BUFFER_SIZE The length of the IPv4 header + option length + total data length is
  1296. greater than MTU (or greater than the maximum packet size if
  1297. Token.Packet.TxData.OverrideData.
  1298. DoNotFragment is TRUE).
  1299. **/
  1300. EFI_STATUS
  1301. EFIAPI
  1302. EfiIp4Transmit (
  1303. IN EFI_IP4_PROTOCOL *This,
  1304. IN EFI_IP4_COMPLETION_TOKEN *Token
  1305. )
  1306. {
  1307. IP4_SERVICE *IpSb;
  1308. IP4_PROTOCOL *IpInstance;
  1309. IP4_INTERFACE *IpIf;
  1310. IP4_TXTOKEN_WRAP *Wrap;
  1311. EFI_IP4_TRANSMIT_DATA *TxData;
  1312. EFI_IP4_CONFIG_DATA *Config;
  1313. EFI_IP4_OVERRIDE_DATA *Override;
  1314. IP4_HEAD Head;
  1315. IP4_ADDR GateWay;
  1316. EFI_STATUS Status;
  1317. EFI_TPL OldTpl;
  1318. BOOLEAN DontFragment;
  1319. UINT32 HeadLen;
  1320. UINT8 RawHdrLen;
  1321. UINT32 OptionsLength;
  1322. UINT8 *OptionsBuffer;
  1323. VOID *FirstFragment;
  1324. if (This == NULL) {
  1325. return EFI_INVALID_PARAMETER;
  1326. }
  1327. IpInstance = IP4_INSTANCE_FROM_PROTOCOL (This);
  1328. if (IpInstance->State != IP4_STATE_CONFIGED) {
  1329. return EFI_NOT_STARTED;
  1330. }
  1331. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  1332. IpSb = IpInstance->Service;
  1333. IpIf = IpInstance->Interface;
  1334. Config = &IpInstance->ConfigData;
  1335. if (Config->UseDefaultAddress && IP4_NO_MAPPING (IpInstance)) {
  1336. Status = EFI_NO_MAPPING;
  1337. goto ON_EXIT;
  1338. }
  1339. //
  1340. // make sure that token is properly formatted
  1341. //
  1342. Status = Ip4TxTokenValid (Token, IpIf, Config->RawData);
  1343. if (EFI_ERROR (Status)) {
  1344. goto ON_EXIT;
  1345. }
  1346. //
  1347. // Check whether the token or signal already existed.
  1348. //
  1349. if (EFI_ERROR (NetMapIterate (&IpInstance->TxTokens, Ip4TokenExist, Token))) {
  1350. Status = EFI_ACCESS_DENIED;
  1351. goto ON_EXIT;
  1352. }
  1353. //
  1354. // Build the IP header, need to fill in the Tos, TotalLen, Id,
  1355. // fragment, Ttl, protocol, Src, and Dst.
  1356. //
  1357. TxData = Token->Packet.TxData;
  1358. FirstFragment = NULL;
  1359. if (Config->RawData) {
  1360. //
  1361. // When RawData is TRUE, first buffer in FragmentTable points to a raw
  1362. // IPv4 fragment including IPv4 header and options.
  1363. //
  1364. FirstFragment = TxData->FragmentTable[0].FragmentBuffer;
  1365. CopyMem (&RawHdrLen, FirstFragment, sizeof (UINT8));
  1366. RawHdrLen = (UINT8)(RawHdrLen & 0x0f);
  1367. if (RawHdrLen < 5) {
  1368. Status = EFI_INVALID_PARAMETER;
  1369. goto ON_EXIT;
  1370. }
  1371. RawHdrLen = (UINT8)(RawHdrLen << 2);
  1372. CopyMem (&Head, FirstFragment, IP4_MIN_HEADLEN);
  1373. Ip4NtohHead (&Head);
  1374. HeadLen = 0;
  1375. DontFragment = IP4_DO_NOT_FRAGMENT (Head.Fragment);
  1376. if (!DontFragment) {
  1377. Status = EFI_INVALID_PARAMETER;
  1378. goto ON_EXIT;
  1379. }
  1380. GateWay = IP4_ALLZERO_ADDRESS;
  1381. //
  1382. // Get IPv4 options from first fragment.
  1383. //
  1384. if (RawHdrLen == IP4_MIN_HEADLEN) {
  1385. OptionsLength = 0;
  1386. OptionsBuffer = NULL;
  1387. } else {
  1388. OptionsLength = RawHdrLen - IP4_MIN_HEADLEN;
  1389. OptionsBuffer = (UINT8 *)FirstFragment + IP4_MIN_HEADLEN;
  1390. }
  1391. //
  1392. // Trim off IPv4 header and options from first fragment.
  1393. //
  1394. TxData->FragmentTable[0].FragmentBuffer = (UINT8 *)FirstFragment + RawHdrLen;
  1395. TxData->FragmentTable[0].FragmentLength = TxData->FragmentTable[0].FragmentLength - RawHdrLen;
  1396. } else {
  1397. CopyMem (&Head.Dst, &TxData->DestinationAddress, sizeof (IP4_ADDR));
  1398. Head.Dst = NTOHL (Head.Dst);
  1399. if (TxData->OverrideData != NULL) {
  1400. Override = TxData->OverrideData;
  1401. Head.Protocol = Override->Protocol;
  1402. Head.Tos = Override->TypeOfService;
  1403. Head.Ttl = Override->TimeToLive;
  1404. DontFragment = Override->DoNotFragment;
  1405. CopyMem (&Head.Src, &Override->SourceAddress, sizeof (IP4_ADDR));
  1406. CopyMem (&GateWay, &Override->GatewayAddress, sizeof (IP4_ADDR));
  1407. Head.Src = NTOHL (Head.Src);
  1408. GateWay = NTOHL (GateWay);
  1409. } else {
  1410. Head.Src = IpIf->Ip;
  1411. GateWay = IP4_ALLZERO_ADDRESS;
  1412. Head.Protocol = Config->DefaultProtocol;
  1413. Head.Tos = Config->TypeOfService;
  1414. Head.Ttl = Config->TimeToLive;
  1415. DontFragment = Config->DoNotFragment;
  1416. }
  1417. Head.Fragment = IP4_HEAD_FRAGMENT_FIELD (DontFragment, FALSE, 0);
  1418. HeadLen = (TxData->OptionsLength + 3) & (~0x03);
  1419. OptionsLength = TxData->OptionsLength;
  1420. OptionsBuffer = (UINT8 *)(TxData->OptionsBuffer);
  1421. }
  1422. //
  1423. // If don't fragment and fragment needed, return error
  1424. //
  1425. if (DontFragment && (TxData->TotalDataLength + HeadLen > IpSb->MaxPacketSize)) {
  1426. Status = EFI_BAD_BUFFER_SIZE;
  1427. goto ON_EXIT;
  1428. }
  1429. //
  1430. // OK, it survives all the validation check. Wrap the token in
  1431. // a IP4_TXTOKEN_WRAP and the data in a netbuf
  1432. //
  1433. Status = EFI_OUT_OF_RESOURCES;
  1434. Wrap = AllocateZeroPool (sizeof (IP4_TXTOKEN_WRAP));
  1435. if (Wrap == NULL) {
  1436. goto ON_EXIT;
  1437. }
  1438. Wrap->IpInstance = IpInstance;
  1439. Wrap->Token = Token;
  1440. Wrap->Sent = FALSE;
  1441. Wrap->Life = IP4_US_TO_SEC (Config->TransmitTimeout);
  1442. Wrap->Packet = NetbufFromExt (
  1443. (NET_FRAGMENT *)TxData->FragmentTable,
  1444. TxData->FragmentCount,
  1445. IP4_MAX_HEADLEN,
  1446. 0,
  1447. Ip4FreeTxToken,
  1448. Wrap
  1449. );
  1450. if (Wrap->Packet == NULL) {
  1451. FreePool (Wrap);
  1452. goto ON_EXIT;
  1453. }
  1454. Token->Status = EFI_NOT_READY;
  1455. if (EFI_ERROR (NetMapInsertTail (&IpInstance->TxTokens, Token, Wrap))) {
  1456. //
  1457. // NetbufFree will call Ip4FreeTxToken, which in turn will
  1458. // free the IP4_TXTOKEN_WRAP. Now, the token wrap hasn't been
  1459. // enqueued.
  1460. //
  1461. if (Config->RawData) {
  1462. //
  1463. // Restore pointer of first fragment in RawData mode.
  1464. //
  1465. TxData->FragmentTable[0].FragmentBuffer = (UINT8 *)FirstFragment;
  1466. }
  1467. NetbufFree (Wrap->Packet);
  1468. goto ON_EXIT;
  1469. }
  1470. //
  1471. // Mark the packet sent before output it. Mark it not sent again if the
  1472. // returned status is not EFI_SUCCESS;
  1473. //
  1474. Wrap->Sent = TRUE;
  1475. Status = Ip4Output (
  1476. IpSb,
  1477. IpInstance,
  1478. Wrap->Packet,
  1479. &Head,
  1480. OptionsBuffer,
  1481. OptionsLength,
  1482. GateWay,
  1483. Ip4OnPacketSent,
  1484. Wrap
  1485. );
  1486. if (EFI_ERROR (Status)) {
  1487. Wrap->Sent = FALSE;
  1488. if (Config->RawData) {
  1489. //
  1490. // Restore pointer of first fragment in RawData mode.
  1491. //
  1492. TxData->FragmentTable[0].FragmentBuffer = (UINT8 *)FirstFragment;
  1493. }
  1494. NetbufFree (Wrap->Packet);
  1495. }
  1496. if (Config->RawData) {
  1497. //
  1498. // Restore pointer of first fragment in RawData mode.
  1499. //
  1500. TxData->FragmentTable[0].FragmentBuffer = (UINT8 *)FirstFragment;
  1501. }
  1502. ON_EXIT:
  1503. gBS->RestoreTPL (OldTpl);
  1504. return Status;
  1505. }
  1506. /**
  1507. Places a receiving request into the receiving queue.
  1508. The Receive() function places a completion token into the receive packet queue.
  1509. This function is always asynchronous.
  1510. The Token.Event field in the completion token must be filled in by the caller
  1511. and cannot be NULL. When the receive operation completes, the EFI IPv4 Protocol
  1512. driver updates the Token.Status and Token.Packet.RxData fields and the Token.Event
  1513. is signaled.
  1514. @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.
  1515. @param[in] Token Pointer to a token that is associated with the receive data descriptor.
  1516. @retval EFI_SUCCESS The receive completion token was cached.
  1517. @retval EFI_NOT_STARTED This EFI IPv4 Protocol instance has not been started.
  1518. @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP, RARP, etc.)
  1519. is not finished yet.
  1520. @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
  1521. - This is NULL.
  1522. - Token is NULL.
  1523. - Token.Event is NULL.
  1524. @retval EFI_OUT_OF_RESOURCES The receive completion token could not be queued due to a lack of system
  1525. resources (usually memory).
  1526. @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
  1527. The EFI IPv4 Protocol instance has been reset to startup defaults.
  1528. EFI_ACCESS_DENIED The receive completion token with the same Token.Event was already
  1529. in the receive queue.
  1530. @retval EFI_NOT_READY The receive request could not be queued because the receive queue is full.
  1531. @retval EFI_ICMP_ERROR An ICMP error packet was received.
  1532. **/
  1533. EFI_STATUS
  1534. EFIAPI
  1535. EfiIp4Receive (
  1536. IN EFI_IP4_PROTOCOL *This,
  1537. IN EFI_IP4_COMPLETION_TOKEN *Token
  1538. )
  1539. {
  1540. IP4_PROTOCOL *IpInstance;
  1541. EFI_STATUS Status;
  1542. EFI_TPL OldTpl;
  1543. //
  1544. // First validate the parameters
  1545. //
  1546. if ((This == NULL) || (Token == NULL) || (Token->Event == NULL)) {
  1547. return EFI_INVALID_PARAMETER;
  1548. }
  1549. IpInstance = IP4_INSTANCE_FROM_PROTOCOL (This);
  1550. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  1551. if (IpInstance->State != IP4_STATE_CONFIGED) {
  1552. Status = EFI_NOT_STARTED;
  1553. goto ON_EXIT;
  1554. }
  1555. //
  1556. // Check whether the toke is already on the receive queue.
  1557. //
  1558. Status = NetMapIterate (&IpInstance->RxTokens, Ip4TokenExist, Token);
  1559. if (EFI_ERROR (Status)) {
  1560. Status = EFI_ACCESS_DENIED;
  1561. goto ON_EXIT;
  1562. }
  1563. //
  1564. // Queue the token then check whether there is pending received packet.
  1565. //
  1566. Status = NetMapInsertTail (&IpInstance->RxTokens, Token, NULL);
  1567. if (EFI_ERROR (Status)) {
  1568. goto ON_EXIT;
  1569. }
  1570. Status = Ip4InstanceDeliverPacket (IpInstance);
  1571. //
  1572. // Dispatch the DPC queued by the NotifyFunction of this instane's receive
  1573. // event.
  1574. //
  1575. DispatchDpc ();
  1576. ON_EXIT:
  1577. gBS->RestoreTPL (OldTpl);
  1578. return Status;
  1579. }
  1580. /**
  1581. Cancel the transmitted but not recycled packet. If a matching
  1582. token is found, it will call Ip4CancelPacket to cancel the
  1583. packet. Ip4CancelPacket will cancel all the fragments of the
  1584. packet. When all the fragments are freed, the IP4_TXTOKEN_WRAP
  1585. will be deleted from the Map, and user's event signalled.
  1586. Because Ip4CancelPacket and other functions are all called in
  1587. line, so, after Ip4CancelPacket returns, the Item has been freed.
  1588. @param[in] Map The IP4 child's transmit queue.
  1589. @param[in] Item The current transmitted packet to test.
  1590. @param[in] Context The user's token to cancel.
  1591. @retval EFI_SUCCESS Continue to check the next Item.
  1592. @retval EFI_ABORTED The user's Token (Token != NULL) is cancelled.
  1593. **/
  1594. EFI_STATUS
  1595. EFIAPI
  1596. Ip4CancelTxTokens (
  1597. IN NET_MAP *Map,
  1598. IN NET_MAP_ITEM *Item,
  1599. IN VOID *Context
  1600. )
  1601. {
  1602. EFI_IP4_COMPLETION_TOKEN *Token;
  1603. IP4_TXTOKEN_WRAP *Wrap;
  1604. Token = (EFI_IP4_COMPLETION_TOKEN *)Context;
  1605. //
  1606. // Return EFI_SUCCESS to check the next item in the map if
  1607. // this one doesn't match.
  1608. //
  1609. if ((Token != NULL) && (Token != Item->Key)) {
  1610. return EFI_SUCCESS;
  1611. }
  1612. Wrap = (IP4_TXTOKEN_WRAP *)Item->Value;
  1613. ASSERT (Wrap != NULL);
  1614. //
  1615. // Don't access the Item, Wrap and Token's members after this point.
  1616. // Item and wrap has been freed. And we no longer own the Token.
  1617. //
  1618. Ip4CancelPacket (Wrap->IpInstance->Interface, Wrap->Packet, EFI_ABORTED);
  1619. //
  1620. // If only one item is to be cancel, return EFI_ABORTED to stop
  1621. // iterating the map any more.
  1622. //
  1623. if (Token != NULL) {
  1624. return EFI_ABORTED;
  1625. }
  1626. return EFI_SUCCESS;
  1627. }
  1628. /**
  1629. Cancel the receive request. This is quiet simple, because
  1630. it is only enqueued in our local receive map.
  1631. @param[in] Map The IP4 child's receive queue.
  1632. @param[in] Item Current receive request to cancel.
  1633. @param[in] Context The user's token to cancel.
  1634. @retval EFI_SUCCESS Continue to check the next receive request on the
  1635. queue.
  1636. @retval EFI_ABORTED The user's token (token != NULL) has been
  1637. cancelled.
  1638. **/
  1639. EFI_STATUS
  1640. EFIAPI
  1641. Ip4CancelRxTokens (
  1642. IN NET_MAP *Map,
  1643. IN NET_MAP_ITEM *Item,
  1644. IN VOID *Context
  1645. )
  1646. {
  1647. EFI_IP4_COMPLETION_TOKEN *Token;
  1648. EFI_IP4_COMPLETION_TOKEN *This;
  1649. Token = (EFI_IP4_COMPLETION_TOKEN *)Context;
  1650. This = Item->Key;
  1651. if ((Token != NULL) && (Token != This)) {
  1652. return EFI_SUCCESS;
  1653. }
  1654. NetMapRemoveItem (Map, Item, NULL);
  1655. This->Status = EFI_ABORTED;
  1656. This->Packet.RxData = NULL;
  1657. gBS->SignalEvent (This->Event);
  1658. if (Token != NULL) {
  1659. return EFI_ABORTED;
  1660. }
  1661. return EFI_SUCCESS;
  1662. }
  1663. /**
  1664. Cancel the user's receive/transmit request.
  1665. @param[in] IpInstance The IP4 child.
  1666. @param[in] Token The token to cancel. If NULL, all token will be
  1667. cancelled.
  1668. @retval EFI_SUCCESS The token is cancelled.
  1669. @retval EFI_NOT_FOUND The token isn't found on either the
  1670. transmit/receive queue.
  1671. @retval EFI_DEVICE_ERROR Not all token is cancelled when Token is NULL.
  1672. **/
  1673. EFI_STATUS
  1674. Ip4Cancel (
  1675. IN IP4_PROTOCOL *IpInstance,
  1676. IN EFI_IP4_COMPLETION_TOKEN *Token OPTIONAL
  1677. )
  1678. {
  1679. EFI_STATUS Status;
  1680. //
  1681. // First check the transmitted packet. Ip4CancelTxTokens returns
  1682. // EFI_ABORTED to mean that the token has been cancelled when
  1683. // token != NULL. So, return EFI_SUCCESS for this condition.
  1684. //
  1685. Status = NetMapIterate (&IpInstance->TxTokens, Ip4CancelTxTokens, Token);
  1686. if (EFI_ERROR (Status)) {
  1687. if ((Token != NULL) && (Status == EFI_ABORTED)) {
  1688. return EFI_SUCCESS;
  1689. }
  1690. return Status;
  1691. }
  1692. //
  1693. // Check the receive queue. Ip4CancelRxTokens also returns EFI_ABORT
  1694. // for Token!=NULL and it is cancelled.
  1695. //
  1696. Status = NetMapIterate (&IpInstance->RxTokens, Ip4CancelRxTokens, Token);
  1697. //
  1698. // Dispatch the DPCs queued by the NotifyFunction of the canceled rx token's
  1699. // events.
  1700. //
  1701. DispatchDpc ();
  1702. if (EFI_ERROR (Status)) {
  1703. if ((Token != NULL) && (Status == EFI_ABORTED)) {
  1704. return EFI_SUCCESS;
  1705. }
  1706. return Status;
  1707. }
  1708. //
  1709. // OK, if the Token is found when Token != NULL, the NetMapIterate
  1710. // will return EFI_ABORTED, which has been interrupted as EFI_SUCCESS.
  1711. //
  1712. if (Token != NULL) {
  1713. return EFI_NOT_FOUND;
  1714. }
  1715. //
  1716. // If Token == NULL, cancel all the tokens. return error if no
  1717. // all of them are cancelled.
  1718. //
  1719. if (!NetMapIsEmpty (&IpInstance->TxTokens) ||
  1720. !NetMapIsEmpty (&IpInstance->RxTokens))
  1721. {
  1722. return EFI_DEVICE_ERROR;
  1723. }
  1724. return EFI_SUCCESS;
  1725. }
  1726. /**
  1727. Abort an asynchronous transmit or receive request.
  1728. The Cancel() function is used to abort a pending transmit or receive request.
  1729. If the token is in the transmit or receive request queues, after calling this
  1730. function, Token->Status will be set to EFI_ABORTED and then Token->Event will
  1731. be signaled. If the token is not in one of the queues, which usually means the
  1732. asynchronous operation has completed, this function will not signal the token
  1733. and EFI_NOT_FOUND is returned.
  1734. @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.
  1735. @param[in] Token Pointer to a token that has been issued by
  1736. EFI_IP4_PROTOCOL.Transmit() or
  1737. EFI_IP4_PROTOCOL.Receive(). If NULL, all pending
  1738. tokens are aborted. Type EFI_IP4_COMPLETION_TOKEN is
  1739. defined in EFI_IP4_PROTOCOL.Transmit().
  1740. @retval EFI_SUCCESS The asynchronous I/O request was aborted and
  1741. Token.->Event was signaled. When Token is NULL, all
  1742. pending requests were aborted and their events were signaled.
  1743. @retval EFI_INVALID_PARAMETER This is NULL.
  1744. @retval EFI_NOT_STARTED This instance has not been started.
  1745. @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP,
  1746. RARP, etc.) is not finished yet.
  1747. @retval EFI_NOT_FOUND When Token is not NULL, the asynchronous I/O request was
  1748. not found in the transmit or receive queue. It has either completed
  1749. or was not issued by Transmit() and Receive().
  1750. **/
  1751. EFI_STATUS
  1752. EFIAPI
  1753. EfiIp4Cancel (
  1754. IN EFI_IP4_PROTOCOL *This,
  1755. IN EFI_IP4_COMPLETION_TOKEN *Token OPTIONAL
  1756. )
  1757. {
  1758. IP4_PROTOCOL *IpInstance;
  1759. EFI_STATUS Status;
  1760. EFI_TPL OldTpl;
  1761. if (This == NULL) {
  1762. return EFI_INVALID_PARAMETER;
  1763. }
  1764. IpInstance = IP4_INSTANCE_FROM_PROTOCOL (This);
  1765. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  1766. if (IpInstance->State != IP4_STATE_CONFIGED) {
  1767. Status = EFI_NOT_STARTED;
  1768. goto ON_EXIT;
  1769. }
  1770. if (IpInstance->ConfigData.UseDefaultAddress && IP4_NO_MAPPING (IpInstance)) {
  1771. Status = EFI_NO_MAPPING;
  1772. goto ON_EXIT;
  1773. }
  1774. Status = Ip4Cancel (IpInstance, Token);
  1775. ON_EXIT:
  1776. gBS->RestoreTPL (OldTpl);
  1777. return Status;
  1778. }
  1779. /**
  1780. Polls for incoming data packets and processes outgoing data packets.
  1781. The Poll() function polls for incoming data packets and processes outgoing data
  1782. packets. Network drivers and applications can call the EFI_IP4_PROTOCOL.Poll()
  1783. function to increase the rate that data packets are moved between the communications
  1784. device and the transmit and receive queues.
  1785. In some systems the periodic timer event may not poll the underlying communications
  1786. device fast enough to transmit and/or receive all data packets without missing
  1787. incoming packets or dropping outgoing packets. Drivers and applications that are
  1788. experiencing packet loss should try calling the EFI_IP4_PROTOCOL.Poll() function
  1789. more often.
  1790. @param[in] This Pointer to the EFI_IP4_PROTOCOL instance.
  1791. @retval EFI_SUCCESS Incoming or outgoing data was processed.
  1792. @retval EFI_NOT_STARTED This EFI IPv4 Protocol instance has not been started.
  1793. @retval EFI_NO_MAPPING When using the default address, configuration (DHCP, BOOTP,
  1794. RARP, etc.) is not finished yet.
  1795. @retval EFI_INVALID_PARAMETER This is NULL.
  1796. @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
  1797. @retval EFI_NOT_READY No incoming or outgoing data is processed.
  1798. @retval EFI_TIMEOUT Data was dropped out of the transmit and/or receive queue.
  1799. Consider increasing the polling rate.
  1800. **/
  1801. EFI_STATUS
  1802. EFIAPI
  1803. EfiIp4Poll (
  1804. IN EFI_IP4_PROTOCOL *This
  1805. )
  1806. {
  1807. IP4_PROTOCOL *IpInstance;
  1808. EFI_MANAGED_NETWORK_PROTOCOL *Mnp;
  1809. if (This == NULL) {
  1810. return EFI_INVALID_PARAMETER;
  1811. }
  1812. IpInstance = IP4_INSTANCE_FROM_PROTOCOL (This);
  1813. if (IpInstance->State == IP4_STATE_UNCONFIGED) {
  1814. return EFI_NOT_STARTED;
  1815. }
  1816. Mnp = IpInstance->Service->Mnp;
  1817. //
  1818. // Don't lock the Poll function to enable the deliver of
  1819. // the packet polled up.
  1820. //
  1821. return Mnp->Poll (Mnp);
  1822. }
  1823. /**
  1824. Decrease the life of the transmitted packets. If it is
  1825. decreased to zero, cancel the packet. This function is
  1826. called by Ip4PacketTimerTicking which time out both the
  1827. received-but-not-delivered and transmitted-but-not-recycle
  1828. packets.
  1829. @param[in] Map The IP4 child's transmit map.
  1830. @param[in] Item Current transmitted packet.
  1831. @param[in] Context Not used.
  1832. @retval EFI_SUCCESS Always returns EFI_SUCCESS.
  1833. **/
  1834. EFI_STATUS
  1835. EFIAPI
  1836. Ip4SentPacketTicking (
  1837. IN NET_MAP *Map,
  1838. IN NET_MAP_ITEM *Item,
  1839. IN VOID *Context
  1840. )
  1841. {
  1842. IP4_TXTOKEN_WRAP *Wrap;
  1843. Wrap = (IP4_TXTOKEN_WRAP *)Item->Value;
  1844. ASSERT (Wrap != NULL);
  1845. if ((Wrap->Life > 0) && (--Wrap->Life == 0)) {
  1846. Ip4CancelPacket (Wrap->IpInstance->Interface, Wrap->Packet, EFI_ABORTED);
  1847. }
  1848. return EFI_SUCCESS;
  1849. }
  1850. /**
  1851. This heart beat timer of IP4 service instance times out all of its IP4 children's
  1852. received-but-not-delivered and transmitted-but-not-recycle packets, and provides
  1853. time input for its IGMP protocol.
  1854. @param[in] Event The IP4 service instance's heart beat timer.
  1855. @param[in] Context The IP4 service instance.
  1856. **/
  1857. VOID
  1858. EFIAPI
  1859. Ip4TimerTicking (
  1860. IN EFI_EVENT Event,
  1861. IN VOID *Context
  1862. )
  1863. {
  1864. IP4_SERVICE *IpSb;
  1865. IpSb = (IP4_SERVICE *)Context;
  1866. NET_CHECK_SIGNATURE (IpSb, IP4_SERVICE_SIGNATURE);
  1867. Ip4PacketTimerTicking (IpSb);
  1868. Ip4IgmpTicking (IpSb);
  1869. }
  1870. /**
  1871. This dedicated timer is used to poll underlying network media status. In case
  1872. of cable swap or wireless network switch, a new round auto configuration will
  1873. be initiated. The timer will signal the IP4 to run DHCP configuration again.
  1874. IP4 driver will free old IP address related resource, such as route table and
  1875. Interface, then initiate a DHCP process to acquire new IP, eventually create
  1876. route table for new IP address.
  1877. @param[in] Event The IP4 service instance's heart beat timer.
  1878. @param[in] Context The IP4 service instance.
  1879. **/
  1880. VOID
  1881. EFIAPI
  1882. Ip4TimerReconfigChecking (
  1883. IN EFI_EVENT Event,
  1884. IN VOID *Context
  1885. )
  1886. {
  1887. IP4_SERVICE *IpSb;
  1888. BOOLEAN OldMediaPresent;
  1889. EFI_STATUS Status;
  1890. EFI_SIMPLE_NETWORK_MODE SnpModeData;
  1891. IpSb = (IP4_SERVICE *)Context;
  1892. NET_CHECK_SIGNATURE (IpSb, IP4_SERVICE_SIGNATURE);
  1893. OldMediaPresent = IpSb->MediaPresent;
  1894. //
  1895. // Get fresh mode data from MNP, since underlying media status may change.
  1896. // Here, it needs to mention that the MediaPresent can also be checked even if
  1897. // EFI_NOT_STARTED returned while this MNP child driver instance isn't configured.
  1898. //
  1899. Status = IpSb->Mnp->GetModeData (IpSb->Mnp, NULL, &SnpModeData);
  1900. if (EFI_ERROR (Status) && (Status != EFI_NOT_STARTED)) {
  1901. return;
  1902. }
  1903. IpSb->MediaPresent = SnpModeData.MediaPresent;
  1904. //
  1905. // Media transimit Unpresent to Present means new link movement is detected.
  1906. //
  1907. if (!OldMediaPresent && IpSb->MediaPresent && (IpSb->Ip4Config2Instance.Policy == Ip4Config2PolicyDhcp)) {
  1908. //
  1909. // Signal the IP4 to run the dhcp configuration again. IP4 driver will free
  1910. // old IP address related resource, such as route table and Interface, then
  1911. // initiate a DHCP round to acquire new IP, eventually
  1912. // create route table for new IP address.
  1913. //
  1914. if (IpSb->ReconfigEvent != NULL) {
  1915. Status = gBS->SignalEvent (IpSb->ReconfigEvent);
  1916. DispatchDpc ();
  1917. }
  1918. }
  1919. }