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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @brief NAT plugin virtual fragmentation reassembly
19 #ifndef __included_nat_reass_h__
20 #define __included_nat_reass_h__
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>
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)
33 #define NAT_REASS_FLAG_MAX_FRAG_DROP 1
43 /* align by making this 4 octets even though its a 2 octets field */
45 /* align by making this 4 octets even though its a 1 octet field */
50 } nat_reass_ip4_key_t;
53 typedef CLIB_PACKED(struct
55 nat_reass_ip4_key_t key;
60 u32 frags_per_reass_list_head_index;
75 /* align by making this 4 octets even though its a 1 octet field */
81 } nat_reass_ip6_key_t;
84 typedef CLIB_PACKED(struct
86 nat_reass_ip6_key_t key;
90 u32 frags_per_reass_list_head_index;
111 nat_reass_ip4_t *ip4_reass_pool;
112 clib_bihash_16_8_t ip4_reass_hash;
113 dlist_elt_t *ip4_reass_lru_list_pool;
114 dlist_elt_t *ip4_frags_list_pool;
115 u32 ip4_reass_head_index;
117 clib_spinlock_t ip4_reass_lock;
120 nat_reass_ip6_t *ip6_reass_pool;
121 clib_bihash_48_8_t ip6_reass_hash;
122 dlist_elt_t *ip6_reass_lru_list_pool;
123 dlist_elt_t *ip6_frags_list_pool;
124 u32 ip6_reass_head_index;
126 clib_spinlock_t ip6_reass_lock;
129 vlib_main_t *vlib_main;
130 vnet_main_t *vnet_main;
134 * @brief Set NAT virtual fragmentation reassembly configuration.
136 * @param timeout Reassembly timeout.
137 * @param max_reass Maximum number of concurrent reassemblies.
138 * @param max_frag Maximum number of fragmets per reassembly
139 * @param drop_frag If zero translate fragments, otherwise drop fragments.
140 * @param is_ip6 1 if IPv6, 0 if IPv4.
142 * @returns 0 on success, non-zero value otherwise.
144 int nat_reass_set (u32 timeout, u16 max_reass, u8 max_frag, u8 drop_frag,
148 * @brief Get reassembly timeout.
150 * @param is_ip6 1 if IPv6, 0 if IPv4.
152 * @returns reassembly timeout.
154 u32 nat_reass_get_timeout (u8 is_ip6);
157 * @brief Get maximum number of concurrent reassemblies.
159 * @param is_ip6 1 if IPv6, 0 if IPv4.
161 * @returns maximum number of concurrent reassemblies.
163 u16 nat_reass_get_max_reass (u8 is_ip6);
166 * @brief Get maximum number of fragmets per reassembly.
168 * @param is_ip6 1 if IPv6, 0 if IPv4.
170 * @returns maximum number of fragmets per reassembly.
172 u8 nat_reass_get_max_frag (u8 is_ip6);
175 * @brief Get status of virtual fragmentation reassembly.
177 * @param is_ip6 1 if IPv6, 0 if IPv4.
179 * @returns zero if translate fragments, non-zero value if drop fragments.
181 u8 nat_reass_is_drop_frag (u8 is_ip6);
184 * @brief Initialize NAT virtual fragmentation reassembly.
186 * @param vm vlib main.
188 * @return error code.
190 clib_error_t *nat_reass_init (vlib_main_t * vm);
193 * @brief Find reassembly.
195 * @param src Source IPv4 address.
196 * @param dst Destination IPv4 address.
197 * @param frag_id Fragment ID.
198 * @param proto L4 protocol.
200 * @returns Reassembly data or 0 if not found.
202 nat_reass_ip4_t *nat_ip4_reass_find (ip4_address_t src,
204 u16 frag_id, u8 proto);
207 * @brief Find or create reassembly.
209 * @param src Source IPv4 address.
210 * @param dst Destination IPv4 address.
211 * @param frag_id Fragment ID.
212 * @param proto L4 protocol.
213 * @param reset_timeout If non-zero value reset timeout.
214 * @param bi_to_drop Fragments to drop.
216 * @returns Reassembly data or 0 on failure.
218 nat_reass_ip4_t *nat_ip4_reass_find_or_create (ip4_address_t src,
220 u16 frag_id, u8 proto,
225 * @brief Cache fragment.
227 * @param reass Reassembly data.
228 * @param bi Buffer index.
229 * @param bi_to_drop Fragments to drop.
231 * @returns 0 on success, non-zero value otherwise.
233 int nat_ip4_reass_add_fragment (nat_reass_ip4_t * reass, u32 bi,
237 * @brief Get cached fragments.
239 * @param reass Reassembly data.
240 * @param bi Vector of buffer indexes.
242 void nat_ip4_reass_get_frags (nat_reass_ip4_t * reass, u32 ** bi);
245 * @breif Call back function when walking IPv4 reassemblies, non-zero return
248 typedef int (*nat_ip4_reass_walk_fn_t) (nat_reass_ip4_t * reass, void *ctx);
251 * @brief Walk IPv4 reassemblies.
253 * @param fn The function to invoke on each entry visited.
254 * @param ctx A context passed in the visit function.
256 void nat_ip4_reass_walk (nat_ip4_reass_walk_fn_t fn, void *ctx);
259 * @brief Find or create reassembly.
261 * @param src Source IPv6 address.
262 * @param dst Destination IPv6 address.
263 * @param frag_id Fragment ID.
264 * @param proto L4 protocol.
265 * @param reset_timeout If non-zero value reset timeout.
266 * @param bi_to_drop Fragments to drop.
268 * @returns Reassembly data or 0 on failure.
270 nat_reass_ip6_t *nat_ip6_reass_find_or_create (ip6_address_t src,
272 u32 frag_id, u8 proto,
276 * @brief Cache fragment.
278 * @param reass Reassembly data.
279 * @param bi Buffer index.
280 * @param bi_to_drop Fragments to drop.
282 * @returns 0 on success, non-zero value otherwise.
284 int nat_ip6_reass_add_fragment (nat_reass_ip6_t * reass, u32 bi,
288 * @brief Get cached fragments.
290 * @param reass Reassembly data.
291 * @param bi Vector of buffer indexes.
293 void nat_ip6_reass_get_frags (nat_reass_ip6_t * reass, u32 ** bi);
296 * @breif Call back function when walking IPv6 reassemblies, non-zero return
299 typedef int (*nat_ip6_reass_walk_fn_t) (nat_reass_ip6_t * reass, void *ctx);
302 * @brief Walk IPv6 reassemblies.
304 * @param fn The function to invoke on each entry visited.
305 * @param ctx A context passed in the visit function.
307 void nat_ip6_reass_walk (nat_ip6_reass_walk_fn_t fn, void *ctx);
309 #endif /* __included_nat_reass_h__ */
312 * fd.io coding-style-patch-verification: ON
315 * eval: (c-set-style "gnu")