Ip4Common.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /** @file
  2. Common definition for IP4.
  3. Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #ifndef __EFI_IP4_COMMON_H__
  7. #define __EFI_IP4_COMMON_H__
  8. typedef struct _IP4_INTERFACE IP4_INTERFACE;
  9. typedef struct _IP4_PROTOCOL IP4_PROTOCOL;
  10. typedef struct _IP4_SERVICE IP4_SERVICE;
  11. #define IP4_ETHER_PROTO 0x0800
  12. //
  13. // The packet is received as link level broadcast/multicast/promiscuous.
  14. //
  15. #define IP4_LINK_BROADCAST 0x00000001
  16. #define IP4_LINK_MULTICAST 0x00000002
  17. #define IP4_LINK_PROMISC 0x00000004
  18. //
  19. // IP4 address cast type classification. Keep it true that any
  20. // type bigger than or equal to LOCAL_BROADCAST is broadcast.
  21. //
  22. #define IP4_PROMISCUOUS 1
  23. #define IP4_LOCAL_HOST 2
  24. #define IP4_MULTICAST 3
  25. #define IP4_LOCAL_BROADCAST 4 // Destination is 255.255.255.255
  26. #define IP4_SUBNET_BROADCAST 5
  27. #define IP4_NET_BROADCAST 6
  28. //
  29. // IP4 header flags
  30. //
  31. #define IP4_HEAD_DF_MASK 0x4000
  32. #define IP4_HEAD_MF_MASK 0x2000
  33. #define IP4_HEAD_OFFSET_MASK 0x1fff
  34. #define IP4_ALLZERO_ADDRESS 0x00000000u
  35. #define IP4_ALLONE_ADDRESS 0xFFFFFFFFu
  36. #define IP4_ALLSYSTEM_ADDRESS 0xE0000001u
  37. #define IP4_ALLROUTER_ADDRESS 0xE0000002u
  38. ///
  39. /// Compose the fragment field to be used in the IP4 header.
  40. ///
  41. #define IP4_HEAD_FRAGMENT_FIELD(Df, Mf, Offset) \
  42. ((UINT16)(((Df) ? IP4_HEAD_DF_MASK : 0) | ((Mf) ? IP4_HEAD_MF_MASK : 0) | (((Offset) >> 3) & IP4_HEAD_OFFSET_MASK)))
  43. #define IP4_LAST_FRAGMENT(FragmentField) \
  44. (((FragmentField) & IP4_HEAD_MF_MASK) == 0)
  45. #define IP4_FIRST_FRAGMENT(FragmentField) \
  46. ((BOOLEAN)(((FragmentField) & IP4_HEAD_OFFSET_MASK) == 0))
  47. #define IP4_DO_NOT_FRAGMENT(FragmentField) \
  48. ((BOOLEAN)(((FragmentField) & IP4_HEAD_DF_MASK) == IP4_HEAD_DF_MASK))
  49. #define IP4_IS_BROADCAST(CastType) ((CastType) >= IP4_LOCAL_BROADCAST)
  50. ///
  51. /// Convert the Microsecond to second. IP transmit/receive time is
  52. /// in the unit of microsecond. IP ticks once per second.
  53. ///
  54. #define IP4_US_TO_SEC(Us) (((Us) + 999999) / 1000000)
  55. /**
  56. Return the cast type (Unicast/Broadcast) specific to an
  57. interface. All the addresses are host byte ordered.
  58. @param[in] IpAddr The IP address to classify in host byte order
  59. @param[in] IpIf The interface that IpAddr received from
  60. @return The cast type of this IP address specific to the interface.
  61. @retval IP4_LOCAL_HOST The IpAddr equals to the interface's address
  62. @retval IP4_SUBNET_BROADCAST The IpAddr is a directed subnet broadcast to the
  63. interface
  64. @retval IP4_NET_BROADCAST The IpAddr is a network broadcast to the interface
  65. @retval 0 Otherwise.
  66. **/
  67. INTN
  68. Ip4GetNetCast (
  69. IN IP4_ADDR IpAddr,
  70. IN IP4_INTERFACE *IpIf
  71. );
  72. /**
  73. Find the cast type of the packet related to the local host.
  74. This isn't the same as link layer cast type. For example, DHCP
  75. server may send local broadcast to the local unicast MAC.
  76. @param[in] IpSb The IP4 service binding instance that received the
  77. packet
  78. @param[in] Dst The destination address in the packet (host byte
  79. order)
  80. @param[in] Src The source address in the packet (host byte order)
  81. @return The cast type for the Dst, it will return on the first non-promiscuous
  82. cast type to a configured interface. If the packet doesn't match any of
  83. the interface, multicast address and local broadcast address are checked.
  84. **/
  85. INTN
  86. Ip4GetHostCast (
  87. IN IP4_SERVICE *IpSb,
  88. IN IP4_ADDR Dst,
  89. IN IP4_ADDR Src
  90. );
  91. /**
  92. Find an interface whose configured IP address is Ip.
  93. @param[in] IpSb The IP4 service binding instance
  94. @param[in] Ip The Ip address (host byte order) to find
  95. @return The IP4_INTERFACE point if found, otherwise NULL
  96. **/
  97. IP4_INTERFACE *
  98. Ip4FindInterface (
  99. IN IP4_SERVICE *IpSb,
  100. IN IP4_ADDR Ip
  101. );
  102. /**
  103. Find an interface that Ip is on that connected network.
  104. @param[in] IpSb The IP4 service binding instance
  105. @param[in] Ip The Ip address (host byte order) to find
  106. @return The IP4_INTERFACE point if found, otherwise NULL
  107. **/
  108. IP4_INTERFACE *
  109. Ip4FindNet (
  110. IN IP4_SERVICE *IpSb,
  111. IN IP4_ADDR Ip
  112. );
  113. /**
  114. Find an interface of the service with the same Ip/Netmask pair.
  115. @param[in] IpSb Ip4 service binding instance
  116. @param[in] Ip The Ip address to find (host byte order)
  117. @param[in] Netmask The network to find (host byte order)
  118. @return The IP4_INTERFACE point if found, otherwise NULL
  119. **/
  120. IP4_INTERFACE *
  121. Ip4FindStationAddress (
  122. IN IP4_SERVICE *IpSb,
  123. IN IP4_ADDR Ip,
  124. IN IP4_ADDR Netmask
  125. );
  126. /**
  127. Get the MAC address for a multicast IP address. Call
  128. Mnp's McastIpToMac to find the MAC address in stead of
  129. hard code the NIC to be Ethernet.
  130. @param[in] Mnp The Mnp instance to get the MAC address.
  131. @param[in] Multicast The multicast IP address to translate.
  132. @param[out] Mac The buffer to hold the translated address.
  133. @retval EFI_SUCCESS if the multicast IP is successfully translated to a
  134. multicast MAC address.
  135. @retval other Otherwise some error.
  136. **/
  137. EFI_STATUS
  138. Ip4GetMulticastMac (
  139. IN EFI_MANAGED_NETWORK_PROTOCOL *Mnp,
  140. IN IP4_ADDR Multicast,
  141. OUT EFI_MAC_ADDRESS *Mac
  142. );
  143. /**
  144. Convert the multibyte field in IP header's byter order.
  145. In spite of its name, it can also be used to convert from
  146. host to network byte order.
  147. @param[in] Head The IP head to convert
  148. @return Point to the converted IP head
  149. **/
  150. IP4_HEAD *
  151. Ip4NtohHead (
  152. IN IP4_HEAD *Head
  153. );
  154. /**
  155. Validate that Ip/Netmask pair is OK to be used as station
  156. address. Only continuous netmasks are supported. and check
  157. that StationAddress is a unicast address on the network.
  158. @param[in] Ip The IP address to validate.
  159. @param[in] Netmask The netmask of the IP.
  160. @retval TRUE The Ip/Netmask pair is valid.
  161. @retval FALSE The Ip/Netmask pair is invalid.
  162. **/
  163. BOOLEAN
  164. Ip4StationAddressValid (
  165. IN IP4_ADDR Ip,
  166. IN IP4_ADDR Netmask
  167. );
  168. #endif