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.
19 #ifndef __included_nat64_db_h__
20 #define __included_nat64_db_h__
22 #include <vppinfra/bihash_24_8.h>
23 #include <vppinfra/bihash_48_8.h>
41 } nat64_db_bib_entry_key_t;
44 typedef CLIB_PACKED(struct
46 ip6_address_t in_addr;
48 ip4_address_t out_addr;
54 }) nat64_db_bib_entry_t;
61 #define _(N, i, n, s) \
62 nat64_db_bib_entry_t *_##n##_bib;
66 nat64_db_bib_entry_t *_unk_proto_bib;
69 clib_bihash_24_8_t in2out;
70 clib_bihash_24_8_t out2in;
82 ip46_address_t l_addr;
83 ip46_address_t r_addr;
92 } nat64_db_st_entry_key_t;
95 typedef CLIB_PACKED(struct
97 ip6_address_t in_r_addr;
98 ip4_address_t out_r_addr;
104 }) nat64_db_st_entry_t;
111 #define _(N, i, n, s) \
112 nat64_db_st_entry_t *_##n##_st;
113 foreach_snat_protocol
116 nat64_db_st_entry_t *_unk_proto_st;
119 clib_bihash_48_8_t in2out;
120 clib_bihash_48_8_t out2in;
129 * @brief Call back function to free NAT64 pool address and port when BIB
132 typedef void (*nat64_db_free_addr_port_function_t) (struct nat64_db_s * db,
133 ip4_address_t * addr,
136 typedef struct nat64_db_s
140 nat64_db_free_addr_port_function_t free_addr_port_cb;
145 * @brief Initialize NAT64 DB.
147 * @param db NAT64 DB.
148 * @param bib_buckets Number of BIB hash buckets.
149 * @param bib_memory_size Memory size of BIB hash.
150 * @param st_buckets Number of session table hash buckets.
151 * @param st_memory_size Memory size of session table hash.
152 * @param free_addr_port_cb Call back function to free address and port.
154 * @returns 0 on success, non-zero value otherwise.
156 int nat64_db_init (nat64_db_t * db, u32 bib_buckets, u32 bib_memory_size,
157 u32 st_buckets, u32 st_memory_size,
158 nat64_db_free_addr_port_function_t free_addr_port_cb);
161 * @brief Create new NAT64 BIB entry.
163 * @param thread_index thread index.
164 * @param db NAT64 DB.
165 * @param in_addr Inside IPv6 address.
166 * @param out_addr Outside IPv4 address.
167 * @param in_port Inside port number.
168 * @param out_port Outside port number.
169 * @param fib_index FIB index.
170 * @param proto L4 protocol.
171 * @param is_static 1 if static, 0 if dynamic.
173 * @returns BIB entry on success, 0 otherwise.
175 nat64_db_bib_entry_t *nat64_db_bib_entry_create (u32 thread_index,
177 ip6_address_t * in_addr,
178 ip4_address_t * out_addr,
179 u16 in_port, u16 out_port,
180 u32 fib_index, u8 proto,
184 * @brief Free NAT64 BIB entry.
186 * @param thread_index thread index.
187 * @param db NAT64 DB.
188 * @param bibe BIB entry.
190 void nat64_db_bib_entry_free (u32 thread_index, nat64_db_t * db,
191 nat64_db_bib_entry_t * bibe);
194 * @brief Call back function when walking NAT64 BIB, non-zero
195 * return value stop walk.
197 typedef int (*nat64_db_bib_walk_fn_t) (nat64_db_bib_entry_t * bibe,
200 * @brief Walk NAT64 BIB.
202 * @param db NAT64 DB.
203 * @param proto BIB L4 protocol:
209 * u - otherwise "unknown" protocol BIB
210 * @param fn The function to invoke on each entry visited.
211 * @param ctx A context passed in the visit function.
213 void nat64_db_bib_walk (nat64_db_t * db, u8 proto,
214 nat64_db_bib_walk_fn_t fn, void *ctx);
217 * @brief Find NAT64 BIB entry.
219 * @param db NAT64 DB.
220 * @param addr IP address.
221 * @param port Port number.
222 * @param proto L4 protocol.
223 * @param fib_index FIB index.
224 * @param is_ip6 1 if find by IPv6 (inside) address, 0 by IPv4 (outside).
226 * @return BIB entry if found.
228 nat64_db_bib_entry_t *nat64_db_bib_entry_find (nat64_db_t * db,
229 ip46_address_t * addr,
232 u32 fib_index, u8 is_ip6);
235 * @brief Get BIB entry by index and protocol.
237 * @param db NAT64 DB.
238 * @param proto L4 protocol.
239 * @param bibe_index BIB entry index.
241 * @return BIB entry if found.
243 nat64_db_bib_entry_t *nat64_db_bib_entry_by_index (nat64_db_t * db,
244 u8 proto, u32 bibe_index);
246 * @brief Create new NAT64 session table entry.
248 * @param thread_index thread index.
249 * @param db NAT64 DB.
250 * @param bibe Corresponding BIB entry.
251 * @param in_r_addr Inside IPv6 address of the remote host.
252 * @param out_r_addr Outside IPv4 address of the remote host.
253 * @param r_port Remote host port number.
255 * @returns BIB entry on success, 0 otherwise.
257 nat64_db_st_entry_t *nat64_db_st_entry_create (u32 thread_index,
259 nat64_db_bib_entry_t * bibe,
260 ip6_address_t * in_r_addr,
261 ip4_address_t * out_r_addr,
265 * @brief Free NAT64 session table entry.
267 * @param thread_index thread index.
268 * @param db NAT64 DB.
269 * @param ste Session table entry.
271 void nat64_db_st_entry_free (u32 thread_index, nat64_db_t * db,
272 nat64_db_st_entry_t * ste);
275 * @brief Find NAT64 session table entry.
277 * @param db NAT64 DB.
278 * @param l_addr Local host address.
279 * @param r_addr Remote host address.
280 * @param l_port Local host port number.
281 * @param r_port Remote host port number.
282 * @param proto L4 protocol.
283 * @param fib_index FIB index.
284 * @param is_ip6 1 if find by IPv6 (inside) address, 0 by IPv4 (outside).
286 * @return BIB entry if found.
288 nat64_db_st_entry_t *nat64_db_st_entry_find (nat64_db_t * db,
289 ip46_address_t * l_addr,
290 ip46_address_t * r_addr,
291 u16 l_port, u16 r_port,
293 u32 fib_index, u8 is_ip6);
296 * @brief Call back function when walking NAT64 session table, non-zero
297 * return value stop walk.
299 typedef int (*nat64_db_st_walk_fn_t) (nat64_db_st_entry_t * ste, void *ctx);
302 * @brief Walk NAT64 session table.
304 * @param db NAT64 DB.
305 * @param proto L4 protocol:
306 * - 255 all session tables
307 * - 6 TCP session table
308 * - 17 UDP session table
309 * - 1/58 ICMP session table
310 * - otherwise "unknown" protocol session table
311 * @param fn The function to invoke on each entry visited.
312 * @param ctx A context passed in the visit function.
314 void nat64_db_st_walk (nat64_db_t * db, u8 proto,
315 nat64_db_st_walk_fn_t fn, void *ctx);
318 * @brief Free expired session entries in session tables.
320 * @param thread_index thread index.
321 * @param db NAT64 DB.
322 * @param now Current time.
324 void nad64_db_st_free_expired (u32 thread_index, nat64_db_t * db, u32 now);
327 * @brief Free sessions using specific outside address.
329 * @param thread_index thread index.
330 * @param db NAT64 DB.
331 * @param out_addr Outside address to match.
333 void nat64_db_free_out_addr (u32 thread_index, nat64_db_t * db,
334 ip4_address_t * out_addr);
337 * @brief Get ST entry index.
339 * @param db NAT64 DB.
340 * @param ste ST entry.
342 * @return ST entry index on success, ~0 otherwise.
344 u32 nat64_db_st_entry_get_index (nat64_db_t * db, nat64_db_st_entry_t * ste);
347 * @brief Get ST entry by index and protocol.
349 * @param db NAT64 DB.
350 * @param proto L4 protocol.
351 * @param bibe_index ST entry index.
353 * @return BIB entry if found.
355 nat64_db_st_entry_t *nat64_db_st_entry_by_index (nat64_db_t * db,
356 u8 proto, u32 ste_index);
357 #endif /* __included_nat64_db_h__ */
360 * fd.io coding-style-patch-verification: ON
363 * eval: (c-set-style "gnu")