NAT44 virtual fragmentation reassembly for endpoint-dependent mode (VPP-1325)
[vpp.git] / src / plugins / nat / nat_reass.h
1 /*
2  * Copyright (c) 2017 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 /**
16  * @file
17  * @brief NAT plugin virtual fragmentation reassembly
18  */
19 #ifndef __included_nat_reass_h__
20 #define __included_nat_reass_h__
21
22 #include <vnet/vnet.h>
23 #include <vnet/ip/ip.h>
24 #include <vppinfra/bihash_16_8.h>
25 #include <vppinfra/bihash_48_8.h>
26 #include <vppinfra/dlist.h>
27
28 #define NAT_REASS_TIMEOUT_DEFAULT 2
29 #define NAT_MAX_REASS_DEAFULT 1024
30 #define NAT_MAX_FRAG_DEFAULT 5
31 #define NAT_REASS_HT_LOAD_FACTOR (0.75)
32
33 #define NAT_REASS_FLAG_MAX_FRAG_DROP 1
34 #define NAT_REASS_FLAG_CLASSIFY_ED_CONTINUE 2
35 #define NAT_REASS_FLAG_ED_DONT_TRANSLATE 4
36
37 typedef struct
38 {
39   union
40   {
41     struct
42     {
43       ip4_address_t src;
44       ip4_address_t dst;
45       /* align by making this 4 octets even though its a 2 octets field */
46       u32 frag_id;
47       /* align by making this 4 octets even though its a 1 octet field */
48       u32 proto;
49     };
50     u64 as_u64[2];
51   };
52 } nat_reass_ip4_key_t;
53
54 enum
55 {
56   NAT_REASS_IP4_CLASSIFY_NONE,
57   NAT_REASS_IP4_CLASSIFY_NEXT_IN2OUT,
58   NAT_REASS_IP4_CLASSIFY_NEXT_OUT2IN
59 };
60
61 /* *INDENT-OFF* */
62 typedef CLIB_PACKED(struct
63 {
64   nat_reass_ip4_key_t key;
65   u32 lru_list_index;
66   u32 sess_index;
67   u32 thread_index;
68   f64 last_heard;
69   u32 frags_per_reass_list_head_index;
70   u8 frag_n;
71   u8 flags;
72   u8 classify_next;
73 }) nat_reass_ip4_t;
74 /* *INDENT-ON* */
75
76 typedef struct
77 {
78   union
79   {
80     struct
81     {
82       ip6_address_t src;
83       ip6_address_t dst;
84       u32 frag_id;
85       /* align by making this 4 octets even though its a 1 octet field */
86       u32 proto;
87       u64 unused;
88     };
89     u64 as_u64[6];
90   };
91 } nat_reass_ip6_key_t;
92
93 /* *INDENT-OFF* */
94 typedef CLIB_PACKED(struct
95 {
96   nat_reass_ip6_key_t key;
97   u32 lru_list_index;
98   u32 sess_index;
99   f64 last_heard;
100   u32 frags_per_reass_list_head_index;
101   u8 frag_n;
102   u8 flags;
103 }) nat_reass_ip6_t;
104 /* *INDENT-ON* */
105
106 typedef struct
107 {
108   /* IPv4 config */
109   u32 ip4_timeout;
110   u16 ip4_max_reass;
111   u8 ip4_max_frag;
112   u8 ip4_drop_frag;
113
114   /* IPv6 config */
115   u32 ip6_timeout;
116   u16 ip6_max_reass;
117   u8 ip6_max_frag;
118   u8 ip6_drop_frag;
119
120   /* IPv4 runtime */
121   nat_reass_ip4_t *ip4_reass_pool;
122   clib_bihash_16_8_t ip4_reass_hash;
123   dlist_elt_t *ip4_reass_lru_list_pool;
124   dlist_elt_t *ip4_frags_list_pool;
125   u32 ip4_reass_head_index;
126   u16 ip4_reass_n;
127   clib_spinlock_t ip4_reass_lock;
128
129   /* IPv6 runtime */
130   nat_reass_ip6_t *ip6_reass_pool;
131   clib_bihash_48_8_t ip6_reass_hash;
132   dlist_elt_t *ip6_reass_lru_list_pool;
133   dlist_elt_t *ip6_frags_list_pool;
134   u32 ip6_reass_head_index;
135   u16 ip6_reass_n;
136   clib_spinlock_t ip6_reass_lock;
137
138   /* convenience */
139   vlib_main_t *vlib_main;
140   vnet_main_t *vnet_main;
141 } nat_reass_main_t;
142
143 /**
144  * @brief Set NAT virtual fragmentation reassembly configuration.
145  *
146  * @param timeout   Reassembly timeout.
147  * @param max_reass Maximum number of concurrent reassemblies.
148  * @param max_frag  Maximum number of fragmets per reassembly
149  * @param drop_frag If zero translate fragments, otherwise drop fragments.
150  * @param is_ip6    1 if IPv6, 0 if IPv4.
151  *
152  * @returns 0 on success, non-zero value otherwise.
153  */
154 int nat_reass_set (u32 timeout, u16 max_reass, u8 max_frag, u8 drop_frag,
155                    u8 is_ip6);
156
157 /**
158  * @brief Get reassembly timeout.
159  *
160  * @param is_ip6 1 if IPv6, 0 if IPv4.
161  *
162  * @returns reassembly timeout.
163  */
164 u32 nat_reass_get_timeout (u8 is_ip6);
165
166 /**
167  * @brief Get maximum number of concurrent reassemblies.
168  *
169  * @param is_ip6 1 if IPv6, 0 if IPv4.
170  *
171  * @returns maximum number of concurrent reassemblies.
172  */
173 u16 nat_reass_get_max_reass (u8 is_ip6);
174
175 /**
176  * @brief Get maximum number of fragmets per reassembly.
177  *
178  * @param is_ip6 1 if IPv6, 0 if IPv4.
179  *
180  * @returns maximum number of fragmets per reassembly.
181  */
182 u8 nat_reass_get_max_frag (u8 is_ip6);
183
184 /**
185  * @brief Get status of virtual fragmentation reassembly.
186  *
187  * @param is_ip6 1 if IPv6, 0 if IPv4.
188  *
189  * @returns zero if translate fragments, non-zero value if drop fragments.
190  */
191 u8 nat_reass_is_drop_frag (u8 is_ip6);
192
193 /**
194  * @brief Initialize NAT virtual fragmentation reassembly.
195  *
196  * @param vm vlib main.
197  *
198  * @return error code.
199  */
200 clib_error_t *nat_reass_init (vlib_main_t * vm);
201
202 /**
203  * @brief Find reassembly.
204  *
205  * @param src Source IPv4 address.
206  * @param dst Destination IPv4 address.
207  * @param frag_id Fragment ID.
208  * @param proto L4 protocol.
209  *
210  * @returns Reassembly data or 0 if not found.
211  */
212 nat_reass_ip4_t *nat_ip4_reass_find (ip4_address_t src,
213                                      ip4_address_t dst,
214                                      u16 frag_id, u8 proto);
215
216 /**
217  * @brief Find or create reassembly.
218  *
219  * @param src Source IPv4 address.
220  * @param dst Destination IPv4 address.
221  * @param frag_id Fragment ID.
222  * @param proto L4 protocol.
223  * @param reset_timeout If non-zero value reset timeout.
224  * @param bi_to_drop Fragments to drop.
225  *
226  * @returns Reassembly data or 0 on failure.
227  */
228 nat_reass_ip4_t *nat_ip4_reass_find_or_create (ip4_address_t src,
229                                                ip4_address_t dst,
230                                                u16 frag_id, u8 proto,
231                                                u8 reset_timeout,
232                                                u32 ** bi_to_drop);
233
234 /**
235  * @brief Cache fragment.
236  *
237  * @param reass Reassembly data.
238  * @param bi Buffer index.
239  * @param bi_to_drop Fragments to drop.
240  *
241  * @returns 0 on success, non-zero value otherwise.
242  */
243 int nat_ip4_reass_add_fragment (nat_reass_ip4_t * reass, u32 bi,
244                                 u32 ** bi_to_drop);
245
246 /**
247  * @brief Get cached fragments.
248  *
249  * @param reass Reassembly data.
250  * @param bi Vector of buffer indexes.
251  */
252 void nat_ip4_reass_get_frags (nat_reass_ip4_t * reass, u32 ** bi);
253
254 /**
255  * @breif Call back function when walking IPv4 reassemblies, non-zero return
256  * value stop walk.
257  */
258 typedef int (*nat_ip4_reass_walk_fn_t) (nat_reass_ip4_t * reass, void *ctx);
259
260 /**
261  * @brief Walk IPv4 reassemblies.
262  *
263  * @param fn The function to invoke on each entry visited.
264  * @param ctx A context passed in the visit function.
265  */
266 void nat_ip4_reass_walk (nat_ip4_reass_walk_fn_t fn, void *ctx);
267
268 /**
269  * @brief Find or create reassembly.
270  *
271  * @param src Source IPv6 address.
272  * @param dst Destination IPv6 address.
273  * @param frag_id Fragment ID.
274  * @param proto L4 protocol.
275  * @param reset_timeout If non-zero value reset timeout.
276  * @param bi_to_drop Fragments to drop.
277  *
278  * @returns Reassembly data or 0 on failure.
279  */
280 nat_reass_ip6_t *nat_ip6_reass_find_or_create (ip6_address_t src,
281                                                ip6_address_t dst,
282                                                u32 frag_id, u8 proto,
283                                                u8 reset_timeout,
284                                                u32 ** bi_to_drop);
285 /**
286  * @brief Cache fragment.
287  *
288  * @param reass Reassembly data.
289  * @param bi Buffer index.
290  * @param bi_to_drop Fragments to drop.
291  *
292  * @returns 0 on success, non-zero value otherwise.
293  */
294 int nat_ip6_reass_add_fragment (nat_reass_ip6_t * reass, u32 bi,
295                                 u32 ** bi_to_drop);
296
297 /**
298  * @brief Get cached fragments.
299  *
300  * @param reass Reassembly data.
301  * @param bi Vector of buffer indexes.
302  */
303 void nat_ip6_reass_get_frags (nat_reass_ip6_t * reass, u32 ** bi);
304
305 /**
306  * @breif Call back function when walking IPv6 reassemblies, non-zero return
307  * value stop walk.
308  */
309 typedef int (*nat_ip6_reass_walk_fn_t) (nat_reass_ip6_t * reass, void *ctx);
310
311 /**
312  * @brief Walk IPv6 reassemblies.
313  *
314  * @param fn The function to invoke on each entry visited.
315  * @param ctx A context passed in the visit function.
316  */
317 void nat_ip6_reass_walk (nat_ip6_reass_walk_fn_t fn, void *ctx);
318
319 #endif /* __included_nat_reass_h__ */
320
321 /*
322  * fd.io coding-style-patch-verification: ON
323  *
324  * Local Variables:
325  * eval: (c-set-style "gnu")
326  * End:
327  */