Ip4Input.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /** @file
  2. Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.<BR>
  3. SPDX-License-Identifier: BSD-2-Clause-Patent
  4. **/
  5. #ifndef __EFI_IP4_INPUT_H__
  6. #define __EFI_IP4_INPUT_H__
  7. #define IP4_MIN_HEADLEN 20
  8. #define IP4_MAX_HEADLEN 60
  9. ///
  10. /// 8(ESP header) + 16(max IV) + 16(max padding) + 2(ESP tail) + 12(max ICV) = 54
  11. ///
  12. #define IP4_MAX_IPSEC_HEADLEN 54
  13. #define IP4_ASSEMLE_HASH_SIZE 31
  14. #define IP4_FRAGMENT_LIFE 120
  15. #define IP4_MAX_PACKET_SIZE 65535
  16. ///
  17. /// Per packet information for input process. LinkFlag specifies whether
  18. /// the packet is received as Link layer unicast, multicast or broadcast.
  19. /// The CastType is the IP layer cast type, such as IP multicast or unicast.
  20. /// Start, End and Length are staffs used to assemble the packets. Start
  21. /// is the sequence number of the first byte of data in the packet. Length
  22. /// is the number of bytes of data. End = Start + Length, that is, the
  23. /// sequence number of last byte + 1. Each assembled packet has a count down
  24. /// life. If it isn't consumed before Life reaches zero, the packet is released.
  25. ///
  26. typedef struct {
  27. UINTN LinkFlag;
  28. INTN CastType;
  29. INTN Start;
  30. INTN End;
  31. INTN Length;
  32. UINT32 Life;
  33. EFI_STATUS Status;
  34. } IP4_CLIP_INFO;
  35. ///
  36. /// Structure used to assemble IP packets.
  37. ///
  38. typedef struct {
  39. LIST_ENTRY Link;
  40. //
  41. // Identity of one IP4 packet. Each fragment of a packet has
  42. // the same (Dst, Src, Id, Protocol).
  43. //
  44. IP4_ADDR Dst;
  45. IP4_ADDR Src;
  46. UINT16 Id;
  47. UINT8 Protocol;
  48. INTN TotalLen;
  49. INTN CurLen;
  50. LIST_ENTRY Fragments; // List of all the fragments of this packet
  51. IP4_HEAD *Head; // IP head of the first fragment
  52. IP4_CLIP_INFO *Info; // Per packet info of the first fragment
  53. INTN Life; // Count down life for the packet.
  54. } IP4_ASSEMBLE_ENTRY;
  55. ///
  56. /// Each Ip service instance has an assemble table to reassemble
  57. /// the packets before delivery to its children. It is organized
  58. /// as hash table.
  59. ///
  60. typedef struct {
  61. LIST_ENTRY Bucket[IP4_ASSEMLE_HASH_SIZE];
  62. } IP4_ASSEMBLE_TABLE;
  63. #define IP4_GET_CLIP_INFO(Packet) ((IP4_CLIP_INFO *) ((Packet)->ProtoData))
  64. #define IP4_ASSEMBLE_HASH(Dst, Src, Id, Proto) \
  65. (((Dst) + (Src) + ((Id) << 16) + (Proto)) % IP4_ASSEMLE_HASH_SIZE)
  66. #define IP4_RXDATA_WRAP_SIZE(NumFrag) \
  67. (sizeof (IP4_RXDATA_WRAP) + sizeof (EFI_IP4_FRAGMENT_DATA) * ((NumFrag) - 1))
  68. /**
  69. Initialize an already allocated assemble table. This is generally
  70. the assemble table embedded in the IP4 service instance.
  71. @param[in, out] Table The assemble table to initialize.
  72. **/
  73. VOID
  74. Ip4InitAssembleTable (
  75. IN OUT IP4_ASSEMBLE_TABLE *Table
  76. );
  77. /**
  78. Clean up the assemble table: remove all the fragments
  79. and assemble entries.
  80. @param[in] Table The assemble table to clean up
  81. **/
  82. VOID
  83. Ip4CleanAssembleTable (
  84. IN IP4_ASSEMBLE_TABLE *Table
  85. );
  86. /**
  87. The IP4 input routine. It is called by the IP4_INTERFACE when a
  88. IP4 fragment is received from MNP.
  89. @param[in] Ip4Instance The IP4 child that request the receive, most like
  90. it is NULL.
  91. @param[in] Packet The IP4 packet received.
  92. @param[in] IoStatus The return status of receive request.
  93. @param[in] Flag The link layer flag for the packet received, such
  94. as multicast.
  95. @param[in] Context The IP4 service instance that own the MNP.
  96. **/
  97. VOID
  98. Ip4AccpetFrame (
  99. IN IP4_PROTOCOL *Ip4Instance,
  100. IN NET_BUF *Packet,
  101. IN EFI_STATUS IoStatus,
  102. IN UINT32 Flag,
  103. IN VOID *Context
  104. );
  105. /**
  106. Demultiple the packet. the packet delivery is processed in two
  107. passes. The first pass will enqueue a shared copy of the packet
  108. to each IP4 child that accepts the packet. The second pass will
  109. deliver a non-shared copy of the packet to each IP4 child that
  110. has pending receive requests. Data is copied if more than one
  111. child wants to consume the packet because each IP child needs
  112. its own copy of the packet to make changes.
  113. @param[in] IpSb The IP4 service instance that received the packet.
  114. @param[in] Head The header of the received packet.
  115. @param[in] Packet The data of the received packet.
  116. @param[in] Option Point to the IP4 packet header options.
  117. @param[in] OptionLen Length of the IP4 packet header options.
  118. @retval EFI_NOT_FOUND No IP child accepts the packet.
  119. @retval EFI_SUCCESS The packet is enqueued or delivered to some IP
  120. children.
  121. **/
  122. EFI_STATUS
  123. Ip4Demultiplex (
  124. IN IP4_SERVICE *IpSb,
  125. IN IP4_HEAD *Head,
  126. IN NET_BUF *Packet,
  127. IN UINT8 *Option,
  128. IN UINT32 OptionLen
  129. );
  130. /**
  131. Enqueue a received packet to all the IP children that share
  132. the same interface.
  133. @param[in] IpSb The IP4 service instance that receive the packet.
  134. @param[in] Head The header of the received packet.
  135. @param[in] Packet The data of the received packet.
  136. @param[in] Option Point to the IP4 packet header options.
  137. @param[in] OptionLen Length of the IP4 packet header options.
  138. @param[in] IpIf The interface to enqueue the packet to.
  139. @return The number of the IP4 children that accepts the packet
  140. **/
  141. INTN
  142. Ip4InterfaceEnquePacket (
  143. IN IP4_SERVICE *IpSb,
  144. IN IP4_HEAD *Head,
  145. IN NET_BUF *Packet,
  146. IN UINT8 *Option,
  147. IN UINT32 OptionLen,
  148. IN IP4_INTERFACE *IpIf
  149. );
  150. /**
  151. Deliver the received packets to upper layer if there are both received
  152. requests and enqueued packets. If the enqueued packet is shared, it will
  153. duplicate it to a non-shared packet, release the shared packet, then
  154. deliver the non-shared packet up.
  155. @param[in] IpInstance The IP child to deliver the packet up.
  156. @retval EFI_OUT_OF_RESOURCES Failed to allocate resources to deliver the
  157. packets.
  158. @retval EFI_SUCCESS All the enqueued packets that can be delivered
  159. are delivered up.
  160. **/
  161. EFI_STATUS
  162. Ip4InstanceDeliverPacket (
  163. IN IP4_PROTOCOL *IpInstance
  164. );
  165. /**
  166. Timeout the fragment and enqueued packets.
  167. @param[in] IpSb The IP4 service instance to timeout
  168. **/
  169. VOID
  170. Ip4PacketTimerTicking (
  171. IN IP4_SERVICE *IpSb
  172. );
  173. /**
  174. The work function to locate IPsec protocol to process the inbound or
  175. outbound IP packets. The process routine handls the packet with following
  176. actions: bypass the packet, discard the packet, or protect the packet.
  177. @param[in] IpSb The IP4 service instance.
  178. @param[in, out] Head The caller supplied IP4 header.
  179. @param[in, out] Netbuf The IP4 packet to be processed by IPsec.
  180. @param[in, out] Options The caller supplied options.
  181. @param[in, out] OptionsLen The length of the option.
  182. @param[in] Direction The directionality in an SPD entry,
  183. EfiIPsecInBound or EfiIPsecOutBound.
  184. @param[in] Context The token's wrap.
  185. @retval EFI_SUCCESS The IPsec protocol is not available or disabled.
  186. @retval EFI_SUCCESS The packet was bypassed and all buffers remain the same.
  187. @retval EFI_SUCCESS The packet was protected.
  188. @retval EFI_ACCESS_DENIED The packet was discarded.
  189. @retval EFI_OUT_OF_RESOURCES There is no sufficient resource to complete the operation.
  190. @retval EFI_BUFFER_TOO_SMALL The number of non-empty block is bigger than the
  191. number of input data blocks when build a fragment table.
  192. **/
  193. EFI_STATUS
  194. Ip4IpSecProcessPacket (
  195. IN IP4_SERVICE *IpSb,
  196. IN OUT IP4_HEAD **Head,
  197. IN OUT NET_BUF **Netbuf,
  198. IN OUT UINT8 **Options,
  199. IN OUT UINT32 *OptionsLen,
  200. IN EFI_IPSEC_TRAFFIC_DIR Direction,
  201. IN VOID *Context
  202. );
  203. #endif