2 * Copyright (c) 2020 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.
15 #ifndef __included_nat64_db_h__
16 #define __included_nat64_db_h__
18 #include <vnet/vnet.h>
19 #include <vnet/ip/ip.h>
20 #include <vnet/fib/fib_source.h>
21 #include <nat/lib/nat_proto.h>
23 #include <vppinfra/bihash_24_8.h>
24 #include <vppinfra/bihash_48_8.h>
48 } nat64_db_bib_entry_key_t;
51 typedef CLIB_PACKED(struct
53 ip6_address_t in_addr;
55 ip4_address_t out_addr;
61 }) nat64_db_bib_entry_t;
68 #define _(N, i, n, s) \
69 nat64_db_bib_entry_t *_##n##_bib;
73 nat64_db_bib_entry_t *_unk_proto_bib;
76 clib_bihash_24_8_t in2out;
77 clib_bihash_24_8_t out2in;
89 ip46_address_t l_addr;
90 ip46_address_t r_addr;
99 } nat64_db_st_entry_key_t;
102 typedef CLIB_PACKED(struct
104 ip6_address_t in_r_addr;
105 ip4_address_t out_r_addr;
111 }) nat64_db_st_entry_t;
118 #define _(N, i, n, s) \
119 nat64_db_st_entry_t *_##n##_st;
123 nat64_db_st_entry_t *_unk_proto_st;
126 clib_bihash_48_8_t in2out;
127 clib_bihash_48_8_t out2in;
136 * @brief Call back function to free NAT64 pool address and port when BIB
139 typedef void (*nat64_db_free_addr_port_function_t) (struct nat64_db_s * db,
140 ip4_address_t * addr,
143 typedef struct nat64_db_s
147 nat64_db_free_addr_port_function_t free_addr_port_cb;
152 * @brief Initialize NAT64 DB.
154 * @param db NAT64 DB.
155 * @param c.bib_buckets Number of BIB hash buckets.
156 * @param c.bib_memory_size Memory size of BIB hash.
157 * @param c.st_buckets Number of session table hash buckets.
158 * @param c.st_memory_size Memory size of session table hash.
159 * @param free_addr_port_cb Call back function to free address and port.
161 * @returns 0 on success, non-zero value otherwise.
163 int nat64_db_init (nat64_db_t * db, nat64_config_t c,
164 nat64_db_free_addr_port_function_t free_addr_port_cb);
167 * @brief Free NAT64 DB.
169 * @param db NAT64 DB.
171 * @returns 0 on success, non-zero value otherwise.
173 int nat64_db_free (nat64_db_t * db);
177 * @brief Create new NAT64 BIB entry.
179 * @param thread_index thread index.
180 * @param db NAT64 DB.
181 * @param in_addr Inside IPv6 address.
182 * @param out_addr Outside IPv4 address.
183 * @param in_port Inside port number.
184 * @param out_port Outside port number.
185 * @param fib_index FIB index.
186 * @param proto L4 protocol.
187 * @param is_static 1 if static, 0 if dynamic.
189 * @returns BIB entry on success, 0 otherwise.
191 nat64_db_bib_entry_t *nat64_db_bib_entry_create (u32 thread_index,
193 ip6_address_t * in_addr,
194 ip4_address_t * out_addr,
195 u16 in_port, u16 out_port,
196 u32 fib_index, u8 proto,
200 * @brief Free NAT64 BIB entry.
202 * @param thread_index thread index.
203 * @param db NAT64 DB.
204 * @param bibe BIB entry.
206 void nat64_db_bib_entry_free (u32 thread_index, nat64_db_t * db,
207 nat64_db_bib_entry_t * bibe);
210 * @brief Call back function when walking NAT64 BIB, non-zero
211 * return value stop walk.
213 typedef int (*nat64_db_bib_walk_fn_t) (nat64_db_bib_entry_t * bibe,
216 * @brief Walk NAT64 BIB.
218 * @param db NAT64 DB.
219 * @param proto BIB L4 protocol:
225 * u - otherwise "unknown" protocol BIB
226 * @param fn The function to invoke on each entry visited.
227 * @param ctx A context passed in the visit function.
229 void nat64_db_bib_walk (nat64_db_t * db, u8 proto,
230 nat64_db_bib_walk_fn_t fn, void *ctx);
233 * @brief Find NAT64 BIB entry.
235 * @param db NAT64 DB.
236 * @param addr IP address.
237 * @param port Port number.
238 * @param proto L4 protocol.
239 * @param fib_index FIB index.
240 * @param is_ip6 1 if find by IPv6 (inside) address, 0 by IPv4 (outside).
242 * @return BIB entry if found.
244 nat64_db_bib_entry_t *nat64_db_bib_entry_find (nat64_db_t * db,
245 ip46_address_t * addr,
248 u32 fib_index, u8 is_ip6);
251 * @brief Get BIB entry by index and protocol.
253 * @param db NAT64 DB.
254 * @param proto L4 protocol.
255 * @param bibe_index BIB entry index.
257 * @return BIB entry if found.
259 nat64_db_bib_entry_t *nat64_db_bib_entry_by_index (nat64_db_t * db,
260 u8 proto, u32 bibe_index);
262 * @brief Create new NAT64 session table entry.
264 * @param thread_index thread index.
265 * @param db NAT64 DB.
266 * @param bibe Corresponding BIB entry.
267 * @param in_r_addr Inside IPv6 address of the remote host.
268 * @param out_r_addr Outside IPv4 address of the remote host.
269 * @param r_port Remote host port number.
271 * @returns BIB entry on success, 0 otherwise.
273 nat64_db_st_entry_t *nat64_db_st_entry_create (u32 thread_index,
275 nat64_db_bib_entry_t * bibe,
276 ip6_address_t * in_r_addr,
277 ip4_address_t * out_r_addr,
281 * @brief Free NAT64 session table entry.
283 * @param thread_index thread index.
284 * @param db NAT64 DB.
285 * @param ste Session table entry.
287 void nat64_db_st_entry_free (u32 thread_index, nat64_db_t * db,
288 nat64_db_st_entry_t * ste);
291 * @brief Find NAT64 session table entry.
293 * @param db NAT64 DB.
294 * @param l_addr Local host address.
295 * @param r_addr Remote host address.
296 * @param l_port Local host port number.
297 * @param r_port Remote host port number.
298 * @param proto L4 protocol.
299 * @param fib_index FIB index.
300 * @param is_ip6 1 if find by IPv6 (inside) address, 0 by IPv4 (outside).
302 * @return BIB entry if found.
304 nat64_db_st_entry_t *nat64_db_st_entry_find (nat64_db_t * db,
305 ip46_address_t * l_addr,
306 ip46_address_t * r_addr,
307 u16 l_port, u16 r_port,
309 u32 fib_index, u8 is_ip6);
312 * @brief Call back function when walking NAT64 session table, non-zero
313 * return value stop walk.
315 typedef int (*nat64_db_st_walk_fn_t) (nat64_db_st_entry_t * ste, void *ctx);
318 * @brief Walk NAT64 session table.
320 * @param db NAT64 DB.
321 * @param proto L4 protocol:
322 * - 255 all session tables
323 * - 6 TCP session table
324 * - 17 UDP session table
325 * - 1/58 ICMP session table
326 * - otherwise "unknown" protocol session table
327 * @param fn The function to invoke on each entry visited.
328 * @param ctx A context passed in the visit function.
330 void nat64_db_st_walk (nat64_db_t * db, u8 proto,
331 nat64_db_st_walk_fn_t fn, void *ctx);
334 * @brief Free expired session entries in session tables.
336 * @param thread_index thread index.
337 * @param db NAT64 DB.
338 * @param now Current time.
340 void nad64_db_st_free_expired (u32 thread_index, nat64_db_t * db, u32 now);
343 * @brief Free sessions using specific outside address.
345 * @param thread_index thread index.
346 * @param db NAT64 DB.
347 * @param out_addr Outside address to match.
349 void nat64_db_free_out_addr (u32 thread_index, nat64_db_t * db,
350 ip4_address_t * out_addr);
353 * @brief Get ST entry index.
355 * @param db NAT64 DB.
356 * @param ste ST entry.
358 * @return ST entry index on success, ~0 otherwise.
360 u32 nat64_db_st_entry_get_index (nat64_db_t * db, nat64_db_st_entry_t * ste);
363 * @brief Get ST entry by index and protocol.
365 * @param db NAT64 DB.
366 * @param proto L4 protocol.
367 * @param bibe_index ST entry index.
369 * @return BIB entry if found.
371 nat64_db_st_entry_t *nat64_db_st_entry_by_index (nat64_db_t * db,
372 u8 proto, u32 ste_index);
373 #endif /* __included_nat64_db_h__ */
376 * fd.io coding-style-patch-verification: ON
379 * eval: (c-set-style "gnu")