Ip4Impl.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /** @file
  2. Ip4 internal functions and type definitions.
  3. Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.<BR>
  4. (C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>
  5. SPDX-License-Identifier: BSD-2-Clause-Patent
  6. **/
  7. #ifndef __EFI_IP4_IMPL_H__
  8. #define __EFI_IP4_IMPL_H__
  9. #include <Uefi.h>
  10. #include <Protocol/IpSec.h>
  11. #include <Protocol/Ip4.h>
  12. #include <Protocol/Ip4Config2.h>
  13. #include <Protocol/Arp.h>
  14. #include <Protocol/ManagedNetwork.h>
  15. #include <Protocol/Dhcp4.h>
  16. #include <Protocol/HiiConfigRouting.h>
  17. #include <Protocol/HiiConfigAccess.h>
  18. #include <IndustryStandard/Dhcp.h>
  19. #include <Library/DebugLib.h>
  20. #include <Library/UefiRuntimeServicesTableLib.h>
  21. #include <Library/UefiDriverEntryPoint.h>
  22. #include <Library/UefiBootServicesTableLib.h>
  23. #include <Library/BaseLib.h>
  24. #include <Library/UefiLib.h>
  25. #include <Library/NetLib.h>
  26. #include <Library/BaseMemoryLib.h>
  27. #include <Library/MemoryAllocationLib.h>
  28. #include <Library/DpcLib.h>
  29. #include <Library/PrintLib.h>
  30. #include <Library/DevicePathLib.h>
  31. #include <Library/HiiLib.h>
  32. #include <Library/UefiHiiServicesLib.h>
  33. #include "Ip4Common.h"
  34. #include "Ip4Driver.h"
  35. #include "Ip4If.h"
  36. #include "Ip4Icmp.h"
  37. #include "Ip4Option.h"
  38. #include "Ip4Igmp.h"
  39. #include "Ip4Route.h"
  40. #include "Ip4Input.h"
  41. #include "Ip4Output.h"
  42. #include "Ip4Config2Impl.h"
  43. #include "Ip4Config2Nv.h"
  44. #include "Ip4NvData.h"
  45. #define IP4_PROTOCOL_SIGNATURE SIGNATURE_32 ('I', 'P', '4', 'P')
  46. #define IP4_SERVICE_SIGNATURE SIGNATURE_32 ('I', 'P', '4', 'S')
  47. //
  48. // The state of IP4 protocol. It starts from UNCONFIGED. if it is
  49. // successfully configured, it goes to CONFIGED. if configure NULL
  50. // is called, it becomes UNCONFIGED again.
  51. //
  52. #define IP4_STATE_UNCONFIGED 0
  53. #define IP4_STATE_CONFIGED 1
  54. //
  55. // The state of IP4 service. It starts from UNSTARTED. It transits
  56. // to STARTED if autoconfigure is started. If default address is
  57. // configured, it becomes CONFIGED. and if partly destroyed, it goes
  58. // to DESTROY.
  59. //
  60. #define IP4_SERVICE_UNSTARTED 0
  61. #define IP4_SERVICE_STARTED 1
  62. #define IP4_SERVICE_CONFIGED 2
  63. #define IP4_SERVICE_DESTROY 3
  64. ///
  65. /// IP4_TXTOKEN_WRAP wraps the upper layer's transmit token.
  66. /// The user's data is kept in the Packet. When fragment is
  67. /// needed, each fragment of the Packet has a reference to the
  68. /// Packet, no data is actually copied. The Packet will be
  69. /// released when all the fragments of it have been recycled by
  70. /// MNP. Upon then, the IP4_TXTOKEN_WRAP will be released, and
  71. /// user's event signalled.
  72. ///
  73. typedef struct {
  74. IP4_PROTOCOL *IpInstance;
  75. EFI_IP4_COMPLETION_TOKEN *Token;
  76. EFI_EVENT IpSecRecycleSignal;
  77. NET_BUF *Packet;
  78. BOOLEAN Sent;
  79. INTN Life;
  80. } IP4_TXTOKEN_WRAP;
  81. ///
  82. /// IP4_IPSEC_WRAP wraps the packet received from MNP layer. The packet
  83. /// will be released after it has been processed by the receiver. Upon then,
  84. /// the IP4_IPSEC_WRAP will be released, and the IpSecRecycleSignal will be signaled
  85. /// to notice IPsec to free the resources.
  86. ///
  87. typedef struct {
  88. EFI_EVENT IpSecRecycleSignal;
  89. NET_BUF *Packet;
  90. } IP4_IPSEC_WRAP;
  91. ///
  92. /// IP4_RXDATA_WRAP wraps the data IP4 child delivers to the
  93. /// upper layers. The received packet is kept in the Packet.
  94. /// The Packet itself may be constructured from some fragments.
  95. /// All the fragments of the Packet is organized by a
  96. /// IP4_ASSEMBLE_ENTRY structure. If the Packet is recycled by
  97. /// the upper layer, the assemble entry and its associated
  98. /// fragments will be freed at last.
  99. ///
  100. typedef struct {
  101. LIST_ENTRY Link;
  102. IP4_PROTOCOL *IpInstance;
  103. NET_BUF *Packet;
  104. EFI_IP4_RECEIVE_DATA RxData;
  105. } IP4_RXDATA_WRAP;
  106. struct _IP4_PROTOCOL {
  107. UINT32 Signature;
  108. EFI_IP4_PROTOCOL Ip4Proto;
  109. EFI_HANDLE Handle;
  110. INTN State;
  111. BOOLEAN InDestroy;
  112. IP4_SERVICE *Service;
  113. LIST_ENTRY Link; // Link to all the IP protocol from the service
  114. //
  115. // User's transmit/receive tokens, and received/delivered packets
  116. //
  117. NET_MAP RxTokens;
  118. NET_MAP TxTokens; // map between (User's Token, IP4_TXTOKE_WRAP)
  119. LIST_ENTRY Received; // Received but not delivered packet
  120. LIST_ENTRY Delivered; // Delivered and to be recycled packets
  121. EFI_LOCK RecycleLock;
  122. //
  123. // Instance's address and route tables. There are two route tables.
  124. // RouteTable is used by the IP4 driver to route packet. EfiRouteTable
  125. // is used to communicate the current route info to the upper layer.
  126. //
  127. IP4_INTERFACE *Interface;
  128. LIST_ENTRY AddrLink; // Ip instances with the same IP address.
  129. IP4_ROUTE_TABLE *RouteTable;
  130. EFI_IP4_ROUTE_TABLE *EfiRouteTable;
  131. UINT32 EfiRouteCount;
  132. //
  133. // IGMP data for this instance
  134. //
  135. IP4_ADDR *Groups; // stored in network byte order
  136. UINT32 GroupCount;
  137. EFI_IP4_CONFIG_DATA ConfigData;
  138. };
  139. struct _IP4_SERVICE {
  140. UINT32 Signature;
  141. EFI_SERVICE_BINDING_PROTOCOL ServiceBinding;
  142. INTN State;
  143. //
  144. // List of all the IP instances and interfaces, and default
  145. // interface and route table and caches.
  146. //
  147. UINTN NumChildren;
  148. LIST_ENTRY Children;
  149. LIST_ENTRY Interfaces;
  150. IP4_INTERFACE *DefaultInterface;
  151. IP4_ROUTE_TABLE *DefaultRouteTable;
  152. //
  153. // Ip reassemble utilities, and IGMP data
  154. //
  155. IP4_ASSEMBLE_TABLE Assemble;
  156. IGMP_SERVICE_DATA IgmpCtrl;
  157. //
  158. // Low level protocol used by this service instance
  159. //
  160. EFI_HANDLE Image;
  161. EFI_HANDLE Controller;
  162. EFI_HANDLE MnpChildHandle;
  163. EFI_MANAGED_NETWORK_PROTOCOL *Mnp;
  164. EFI_MANAGED_NETWORK_CONFIG_DATA MnpConfigData;
  165. EFI_SIMPLE_NETWORK_MODE SnpMode;
  166. EFI_EVENT Timer;
  167. EFI_EVENT ReconfigCheckTimer;
  168. EFI_EVENT ReconfigEvent;
  169. BOOLEAN Reconfig;
  170. //
  171. // Underlying media present status.
  172. //
  173. BOOLEAN MediaPresent;
  174. //
  175. // IPv4 Configuration II Protocol instance
  176. //
  177. IP4_CONFIG2_INSTANCE Ip4Config2Instance;
  178. CHAR16 *MacString;
  179. UINT32 MaxPacketSize;
  180. UINT32 OldMaxPacketSize; ///< The MTU before IPsec enable.
  181. };
  182. #define IP4_INSTANCE_FROM_PROTOCOL(Ip4) \
  183. CR ((Ip4), IP4_PROTOCOL, Ip4Proto, IP4_PROTOCOL_SIGNATURE)
  184. #define IP4_SERVICE_FROM_PROTOCOL(Sb) \
  185. CR ((Sb), IP4_SERVICE, ServiceBinding, IP4_SERVICE_SIGNATURE)
  186. #define IP4_SERVICE_FROM_CONFIG2_INSTANCE(This) \
  187. CR (This, IP4_SERVICE, Ip4Config2Instance, IP4_SERVICE_SIGNATURE)
  188. #define IP4_NO_MAPPING(IpInstance) (!(IpInstance)->Interface->Configured)
  189. extern EFI_IP4_PROTOCOL mEfiIp4ProtocolTemplete;
  190. /**
  191. Config the MNP parameter used by IP. The IP driver use one MNP
  192. child to transmit/receive frames. By default, it configures MNP
  193. to receive unicast/multicast/broadcast. And it will enable/disable
  194. the promiscous receive according to whether there is IP child
  195. enable that or not. If Force is FALSE, it will iterate through
  196. all the IP children to check whether the promiscuous receive
  197. setting has been changed. If it hasn't been changed, it won't
  198. reconfigure the MNP. If Force is TRUE, the MNP is configured no
  199. matter whether that is changed or not.
  200. @param[in] IpSb The IP4 service instance that is to be changed.
  201. @param[in] Force Force the configuration or not.
  202. @retval EFI_SUCCESS The MNP is successfully configured/reconfigured.
  203. @retval Others Configuration failed.
  204. **/
  205. EFI_STATUS
  206. Ip4ServiceConfigMnp (
  207. IN IP4_SERVICE *IpSb,
  208. IN BOOLEAN Force
  209. );
  210. /**
  211. Initialize the IP4_PROTOCOL structure to the unconfigured states.
  212. @param IpSb The IP4 service instance.
  213. @param IpInstance The IP4 child instance.
  214. **/
  215. VOID
  216. Ip4InitProtocol (
  217. IN IP4_SERVICE *IpSb,
  218. IN OUT IP4_PROTOCOL *IpInstance
  219. );
  220. /**
  221. Clean up the IP4 child, release all the resources used by it.
  222. @param[in] IpInstance The IP4 child to clean up.
  223. @retval EFI_SUCCESS The IP4 child is cleaned up.
  224. @retval EFI_DEVICE_ERROR Some resources failed to be released.
  225. **/
  226. EFI_STATUS
  227. Ip4CleanProtocol (
  228. IN IP4_PROTOCOL *IpInstance
  229. );
  230. /**
  231. Cancel the user's receive/transmit request.
  232. @param[in] IpInstance The IP4 child.
  233. @param[in] Token The token to cancel. If NULL, all token will be
  234. cancelled.
  235. @retval EFI_SUCCESS The token is cancelled.
  236. @retval EFI_NOT_FOUND The token isn't found on either the
  237. transmit/receive queue.
  238. @retval EFI_DEVICE_ERROR Not all token is cancelled when Token is NULL.
  239. **/
  240. EFI_STATUS
  241. Ip4Cancel (
  242. IN IP4_PROTOCOL *IpInstance,
  243. IN EFI_IP4_COMPLETION_TOKEN *Token OPTIONAL
  244. );
  245. /**
  246. Change the IP4 child's multicast setting. The caller
  247. should make sure that the parameters is valid.
  248. @param[in] IpInstance The IP4 child to change the setting.
  249. @param[in] JoinFlag TRUE to join the group, otherwise leave it
  250. @param[in] GroupAddress The target group address
  251. @retval EFI_ALREADY_STARTED Want to join the group, but already a member of it
  252. @retval EFI_OUT_OF_RESOURCES Failed to allocate some resources.
  253. @retval EFI_DEVICE_ERROR Failed to set the group configuration
  254. @retval EFI_SUCCESS Successfully updated the group setting.
  255. @retval EFI_NOT_FOUND Try to leave the group which it isn't a member.
  256. **/
  257. EFI_STATUS
  258. Ip4Groups (
  259. IN IP4_PROTOCOL *IpInstance,
  260. IN BOOLEAN JoinFlag,
  261. IN EFI_IPv4_ADDRESS *GroupAddress OPTIONAL
  262. );
  263. /**
  264. This heart beat timer of IP4 service instance times out all of its IP4 children's
  265. received-but-not-delivered and transmitted-but-not-recycle packets, and provides
  266. time input for its IGMP protocol.
  267. @param[in] Event The IP4 service instance's heart beat timer.
  268. @param[in] Context The IP4 service instance.
  269. **/
  270. VOID
  271. EFIAPI
  272. Ip4TimerTicking (
  273. IN EFI_EVENT Event,
  274. IN VOID *Context
  275. );
  276. /**
  277. This dedicated timer is used to poll underlying network media status. In case
  278. of cable swap or wireless network switch, a new round auto configuration will
  279. be initiated. The timer will signal the IP4 to run DHCP configuration again.
  280. IP4 driver will free old IP address related resource, such as route table and
  281. Interface, then initiate a DHCP process to acquire new IP, eventually create
  282. route table for new IP address.
  283. @param[in] Event The IP4 service instance's heart beat timer.
  284. @param[in] Context The IP4 service instance.
  285. **/
  286. VOID
  287. EFIAPI
  288. Ip4TimerReconfigChecking (
  289. IN EFI_EVENT Event,
  290. IN VOID *Context
  291. );
  292. /**
  293. Decrease the life of the transmitted packets. If it is
  294. decreased to zero, cancel the packet. This function is
  295. called by Ip4PacketTimerTicking which time out both the
  296. received-but-not-delivered and transmitted-but-not-recycle
  297. packets.
  298. @param[in] Map The IP4 child's transmit map.
  299. @param[in] Item Current transmitted packet.
  300. @param[in] Context Not used.
  301. @retval EFI_SUCCESS Always returns EFI_SUCCESS.
  302. **/
  303. EFI_STATUS
  304. EFIAPI
  305. Ip4SentPacketTicking (
  306. IN NET_MAP *Map,
  307. IN NET_MAP_ITEM *Item,
  308. IN VOID *Context
  309. );
  310. /**
  311. The callback function for the net buffer which wraps the user's
  312. transmit token. Although it seems this function is pretty simple,
  313. there are some subtle things.
  314. When user requests the IP to transmit a packet by passing it a
  315. token, the token is wrapped in an IP4_TXTOKEN_WRAP and the data
  316. is wrapped in an net buffer. the net buffer's Free function is
  317. set to Ip4FreeTxToken. The Token and token wrap are added to the
  318. IP child's TxToken map. Then the buffer is passed to Ip4Output for
  319. transmission. If something error happened before that, the buffer
  320. is freed, which in turn will free the token wrap. The wrap may
  321. have been added to the TxToken map or not, and the user's event
  322. shouldn't be fired because we are still in the EfiIp4Transmit. If
  323. the buffer has been sent by Ip4Output, it should be removed from
  324. the TxToken map and user's event signaled. The token wrap and buffer
  325. are bound together. Check the comments in Ip4Output for information
  326. about IP fragmentation.
  327. @param[in] Context The token's wrap.
  328. **/
  329. VOID
  330. EFIAPI
  331. Ip4FreeTxToken (
  332. IN VOID *Context
  333. );
  334. extern EFI_IPSEC2_PROTOCOL *mIpSec;
  335. extern BOOLEAN mIpSec2Installed;
  336. #endif