SNAT: IP fragmentation (VPP-890)
[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 typedef struct
34 {
35   union
36   {
37     struct
38     {
39       ip4_address_t src;
40       ip4_address_t dst;
41       /* align by making this 4 octets even though its a 2 octets field */
42       u32 frag_id;
43       /* align by making this 4 octets even though its a 1 octet field */
44       u32 proto;
45     };
46     u64 as_u64[2];
47   };
48 } nat_reass_ip4_key_t;
49
50 /* *INDENT-OFF* */
51 typedef CLIB_PACKED(struct
52 {
53   nat_reass_ip4_key_t key;
54   u32 lru_list_index;
55   u32 sess_index;
56   f64 last_heard;
57   u32 frags_per_reass_list_head_index;
58   u8 frag_n;
59 }) nat_reass_ip4_t;
60 /* *INDENT-ON* */
61
62 typedef struct
63 {
64   union
65   {
66     struct
67     {
68       ip6_address_t src;
69       ip6_address_t dst;
70       u32 frag_id;
71       /* align by making this 4 octets even though its a 1 octet field */
72       u32 proto;
73       u64 unused;
74     };
75     u64 as_u64[6];
76   };
77 } nat_reass_ip6_key_t;
78
79 /* *INDENT-OFF* */
80 typedef CLIB_PACKED(struct
81 {
82   nat_reass_ip6_key_t key;
83   u32 lru_list_index;
84   u32 sess_index;
85   f64 last_heard;
86   u32 frags_per_reass_list_head_index;
87   u8 frag_n;
88 }) nat_reass_ip6_t;
89 /* *INDENT-ON* */
90
91 typedef struct
92 {
93   /* IPv4 config */
94   u32 ip4_timeout;
95   u16 ip4_max_reass;
96   u8 ip4_max_frag;
97   u8 ip4_drop_frag;
98
99   /* IPv6 config */
100   u32 ip6_timeout;
101   u16 ip6_max_reass;
102   u8 ip6_max_frag;
103   u8 ip6_drop_frag;
104
105   /* IPv4 runtime */
106   nat_reass_ip4_t *ip4_reass_pool;
107   clib_bihash_16_8_t ip4_reass_hash;
108   dlist_elt_t *ip4_reass_lru_list_pool;
109   dlist_elt_t *ip4_frags_list_pool;
110   u32 ip4_reass_head_index;
111   u16 ip4_reass_n;
112   clib_spinlock_t ip4_reass_lock;
113
114   /* IPv6 runtime */
115   nat_reass_ip6_t *ip6_reass_pool;
116   clib_bihash_48_8_t ip6_reass_hash;
117   dlist_elt_t *ip6_reass_lru_list_pool;
118   dlist_elt_t *ip6_frags_list_pool;
119   u32 ip6_reass_head_index;
120   u16 ip6_reass_n;
121   clib_spinlock_t ip6_reass_lock;
122
123   /* convenience */
124   vlib_main_t *vlib_main;
125   vnet_main_t *vnet_main;
126 } nat_reass_main_t;
127
128 /**
129  * @brief Set NAT virtual fragmentation reassembly configuration.
130  *
131  * @param timeout   Reassembly timeout.
132  * @param max_reass Maximum number of concurrent reassemblies.
133  * @param max_frag  Maximum number of fragmets per reassembly
134  * @param drop_frag If zero translate fragments, otherwise drop fragments.
135  * @param is_ip6    1 if IPv6, 0 if IPv4.
136  *
137  * @returns 0 on success, non-zero value otherwise.
138  */
139 int nat_reass_set (u32 timeout, u16 max_reass, u8 max_frag, u8 drop_frag,
140                    u8 is_ip6);
141
142 /**
143  * @brief Get reassembly timeout.
144  *
145  * @param is_ip6 1 if IPv6, 0 if IPv4.
146  *
147  * @returns reassembly timeout.
148  */
149 u32 nat_reass_get_timeout (u8 is_ip6);
150
151 /**
152  * @brief Get maximum number of concurrent reassemblies.
153  *
154  * @param is_ip6 1 if IPv6, 0 if IPv4.
155  *
156  * @returns maximum number of concurrent reassemblies.
157  */
158 u16 nat_reass_get_max_reass (u8 is_ip6);
159
160 /**
161  * @brief Get maximum number of fragmets per reassembly.
162  *
163  * @param is_ip6 1 if IPv6, 0 if IPv4.
164  *
165  * @returns maximum number of fragmets per reassembly.
166  */
167 u8 nat_reass_get_max_frag (u8 is_ip6);
168
169 /**
170  * @brief Get status of virtual fragmentation reassembly.
171  *
172  * @param is_ip6 1 if IPv6, 0 if IPv4.
173  *
174  * @returns zero if translate fragments, non-zero value if drop fragments.
175  */
176 u8 nat_reass_is_drop_frag (u8 is_ip6);
177
178 /**
179  * @brief Initialize NAT virtual fragmentation reassembly.
180  *
181  * @param vm vlib main.
182  *
183  * @return error code.
184  */
185 clib_error_t *nat_reass_init (vlib_main_t * vm);
186
187 /**
188  * @brief Find or create reassembly.
189  *
190  * @param src Source IPv4 address.
191  * @param dst Destination IPv4 address.
192  * @param frag_id Fragment ID.
193  * @param proto L4 protocol.
194  * @param reset_timeout If non-zero value reset timeout.
195  * @param bi_to_drop Fragments to drop.
196  *
197  * @returns Reassembly data or 0 on failure.
198  */
199 nat_reass_ip4_t *nat_ip4_reass_find_or_create (ip4_address_t src,
200                                                ip4_address_t dst,
201                                                u16 frag_id, u8 proto,
202                                                u8 reset_timeout,
203                                                u32 ** bi_to_drop);
204 /**
205  * @brief Cache fragment.
206  *
207  * @param reass Reassembly data.
208  * @param bi Buffer index.
209  *
210  * @returns 0 on success, non-zero value otherwise.
211  */
212 int nat_ip4_reass_add_fragment (nat_reass_ip4_t * reass, u32 bi);
213
214 /**
215  * @brief Get cached fragments.
216  *
217  * @param reass Reassembly data.
218  * @param bi Vector of buffer indexes.
219  */
220 void nat_ip4_reass_get_frags (nat_reass_ip4_t * reass, u32 ** bi);
221
222 /**
223  * @breif Call back function when walking IPv4 reassemblies, non-zero return
224  * value stop walk.
225  */
226 typedef int (*nat_ip4_reass_walk_fn_t) (nat_reass_ip4_t * reass, void *ctx);
227
228 /**
229  * @brief Walk IPv4 reassemblies.
230  *
231  * @param fn The function to invoke on each entry visited.
232  * @param ctx A context passed in the visit function.
233  */
234 void nat_ip4_reass_walk (nat_ip4_reass_walk_fn_t fn, void *ctx);
235
236 /**
237  * @brief Find or create reassembly.
238  *
239  * @param src Source IPv6 address.
240  * @param dst Destination IPv6 address.
241  * @param frag_id Fragment ID.
242  * @param proto L4 protocol.
243  * @param reset_timeout If non-zero value reset timeout.
244  * @param bi_to_drop Fragments to drop.
245  *
246  * @returns Reassembly data or 0 on failure.
247  */
248 nat_reass_ip6_t *nat_ip6_reass_find_or_create (ip6_address_t src,
249                                                ip6_address_t dst,
250                                                u32 frag_id, u8 proto,
251                                                u8 reset_timeout,
252                                                u32 ** bi_to_drop);
253 /**
254  * @brief Cache fragment.
255  *
256  * @param reass Reassembly data.
257  * @param bi Buffer index.
258  *
259  * @returns 0 on success, non-zero value otherwise.
260  */
261 int nat_ip6_reass_add_fragment (nat_reass_ip6_t * reass, u32 bi);
262
263 /**
264  * @brief Get cached fragments.
265  *
266  * @param reass Reassembly data.
267  * @param bi Vector of buffer indexes.
268  */
269 void nat_ip6_reass_get_frags (nat_reass_ip6_t * reass, u32 ** bi);
270
271 /**
272  * @breif Call back function when walking IPv6 reassemblies, non-zero return
273  * value stop walk.
274  */
275 typedef int (*nat_ip6_reass_walk_fn_t) (nat_reass_ip6_t * reass, void *ctx);
276
277 /**
278  * @brief Walk IPv6 reassemblies.
279  *
280  * @param fn The function to invoke on each entry visited.
281  * @param ctx A context passed in the visit function.
282  */
283 void nat_ip6_reass_walk (nat_ip6_reass_walk_fn_t fn, void *ctx);
284
285 #endif /* __included_nat_reass_h__ */
286
287 /*
288  * fd.io coding-style-patch-verification: ON
289  *
290  * Local Variables:
291  * eval: (c-set-style "gnu")
292  * End:
293  */