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