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;
50 typedef CLIB_PACKED(struct
52 ip6_address_t in_addr;
54 ip4_address_t out_addr;
60 }) nat64_db_bib_entry_t;
65 #define _(N, i, n, s) \
66 nat64_db_bib_entry_t *_##n##_bib;
69 nat64_db_bib_entry_t *_unk_proto_bib;
72 clib_bihash_24_8_t in2out;
73 clib_bihash_24_8_t out2in;
85 ip46_address_t l_addr;
86 ip46_address_t r_addr;
95 } nat64_db_st_entry_key_t;
97 typedef CLIB_PACKED(struct
99 ip6_address_t in_r_addr;
100 ip4_address_t out_r_addr;
106 }) nat64_db_st_entry_t;
111 #define _(N, i, n, s) \
112 nat64_db_st_entry_t *_##n##_st;
115 nat64_db_st_entry_t *_unk_proto_st;
118 clib_bihash_48_8_t in2out;
119 clib_bihash_48_8_t out2in;
128 * @brief Call back function to free NAT64 pool address and port when BIB
131 typedef void (*nat64_db_free_addr_port_function_t) (struct nat64_db_s * db,
132 ip4_address_t * addr,
135 typedef struct nat64_db_s
139 nat64_db_free_addr_port_function_t free_addr_port_cb;
144 * @brief Initialize NAT64 DB.
146 * @param db NAT64 DB.
147 * @param c.bib_buckets Number of BIB hash buckets.
148 * @param c.bib_memory_size Memory size of BIB hash.
149 * @param c.st_buckets Number of session table hash buckets.
150 * @param c.st_memory_size Memory size of session table hash.
151 * @param free_addr_port_cb Call back function to free address and port.
153 * @returns 0 on success, non-zero value otherwise.
155 int nat64_db_init (nat64_db_t * db, nat64_config_t c,
156 nat64_db_free_addr_port_function_t free_addr_port_cb);
159 * @brief Free NAT64 DB.
161 * @param db NAT64 DB.
163 * @returns 0 on success, non-zero value otherwise.
165 int nat64_db_free (nat64_db_t * db);
169 * @brief Create new NAT64 BIB entry.
171 * @param thread_index thread index.
172 * @param db NAT64 DB.
173 * @param in_addr Inside IPv6 address.
174 * @param out_addr Outside IPv4 address.
175 * @param in_port Inside port number.
176 * @param out_port Outside port number.
177 * @param fib_index FIB index.
178 * @param proto L4 protocol.
179 * @param is_static 1 if static, 0 if dynamic.
181 * @returns BIB entry on success, 0 otherwise.
183 nat64_db_bib_entry_t *nat64_db_bib_entry_create (u32 thread_index,
185 ip6_address_t * in_addr,
186 ip4_address_t * out_addr,
187 u16 in_port, u16 out_port,
188 u32 fib_index, u8 proto,
192 * @brief Free NAT64 BIB entry.
194 * @param thread_index thread index.
195 * @param db NAT64 DB.
196 * @param bibe BIB entry.
198 void nat64_db_bib_entry_free (u32 thread_index, nat64_db_t * db,
199 nat64_db_bib_entry_t * bibe);
202 * @brief Call back function when walking NAT64 BIB, non-zero
203 * return value stop walk.
205 typedef int (*nat64_db_bib_walk_fn_t) (nat64_db_bib_entry_t * bibe,
208 * @brief Walk NAT64 BIB.
210 * @param db NAT64 DB.
211 * @param proto BIB L4 protocol:
217 * u - otherwise "unknown" protocol BIB
218 * @param fn The function to invoke on each entry visited.
219 * @param ctx A context passed in the visit function.
221 void nat64_db_bib_walk (nat64_db_t * db, u8 proto,
222 nat64_db_bib_walk_fn_t fn, void *ctx);
225 * @brief Find NAT64 BIB entry.
227 * @param db NAT64 DB.
228 * @param addr IP address.
229 * @param port Port number.
230 * @param proto L4 protocol.
231 * @param fib_index FIB index.
232 * @param is_ip6 1 if find by IPv6 (inside) address, 0 by IPv4 (outside).
234 * @return BIB entry if found.
236 nat64_db_bib_entry_t *nat64_db_bib_entry_find (nat64_db_t * db,
237 ip46_address_t * addr,
240 u32 fib_index, u8 is_ip6);
243 * @brief Get BIB entry by index and protocol.
245 * @param db NAT64 DB.
246 * @param proto L4 protocol.
247 * @param bibe_index BIB entry index.
249 * @return BIB entry if found.
251 nat64_db_bib_entry_t *nat64_db_bib_entry_by_index (nat64_db_t * db,
252 u8 proto, u32 bibe_index);
254 * @brief Create new NAT64 session table entry.
256 * @param thread_index thread index.
257 * @param db NAT64 DB.
258 * @param bibe Corresponding BIB entry.
259 * @param in_r_addr Inside IPv6 address of the remote host.
260 * @param out_r_addr Outside IPv4 address of the remote host.
261 * @param r_port Remote host port number.
263 * @returns BIB entry on success, 0 otherwise.
265 nat64_db_st_entry_t *nat64_db_st_entry_create (u32 thread_index,
267 nat64_db_bib_entry_t * bibe,
268 ip6_address_t * in_r_addr,
269 ip4_address_t * out_r_addr,
273 * @brief Free NAT64 session table entry.
275 * @param thread_index thread index.
276 * @param db NAT64 DB.
277 * @param ste Session table entry.
279 void nat64_db_st_entry_free (u32 thread_index, nat64_db_t * db,
280 nat64_db_st_entry_t * ste);
283 * @brief Find NAT64 session table entry.
285 * @param db NAT64 DB.
286 * @param l_addr Local host address.
287 * @param r_addr Remote host address.
288 * @param l_port Local host port number.
289 * @param r_port Remote host port number.
290 * @param proto L4 protocol.
291 * @param fib_index FIB index.
292 * @param is_ip6 1 if find by IPv6 (inside) address, 0 by IPv4 (outside).
294 * @return BIB entry if found.
296 nat64_db_st_entry_t *nat64_db_st_entry_find (nat64_db_t * db,
297 ip46_address_t * l_addr,
298 ip46_address_t * r_addr,
299 u16 l_port, u16 r_port,
301 u32 fib_index, u8 is_ip6);
304 * @brief Call back function when walking NAT64 session table, non-zero
305 * return value stop walk.
307 typedef int (*nat64_db_st_walk_fn_t) (nat64_db_st_entry_t * ste, void *ctx);
310 * @brief Walk NAT64 session table.
312 * @param db NAT64 DB.
313 * @param proto L4 protocol:
314 * - 255 all session tables
315 * - 6 TCP session table
316 * - 17 UDP session table
317 * - 1/58 ICMP session table
318 * - otherwise "unknown" protocol session table
319 * @param fn The function to invoke on each entry visited.
320 * @param ctx A context passed in the visit function.
322 void nat64_db_st_walk (nat64_db_t * db, u8 proto,
323 nat64_db_st_walk_fn_t fn, void *ctx);
326 * @brief Free expired session entries in session tables.
328 * @param thread_index thread index.
329 * @param db NAT64 DB.
330 * @param now Current time.
332 void nad64_db_st_free_expired (u32 thread_index, nat64_db_t * db, u32 now);
335 * @brief Free sessions using specific outside address.
337 * @param thread_index thread index.
338 * @param db NAT64 DB.
339 * @param out_addr Outside address to match.
341 void nat64_db_free_out_addr (u32 thread_index, nat64_db_t * db,
342 ip4_address_t * out_addr);
345 * @brief Get ST entry index.
347 * @param db NAT64 DB.
348 * @param ste ST entry.
350 * @return ST entry index on success, ~0 otherwise.
352 u32 nat64_db_st_entry_get_index (nat64_db_t * db, nat64_db_st_entry_t * ste);
355 * @brief Get ST entry by index and protocol.
357 * @param db NAT64 DB.
358 * @param proto L4 protocol.
359 * @param bibe_index ST entry index.
361 * @return BIB entry if found.
363 nat64_db_st_entry_t *nat64_db_st_entry_by_index (nat64_db_t * db,
364 u8 proto, u32 ste_index);
365 #endif /* __included_nat64_db_h__ */
368 * fd.io coding-style-patch-verification: ON
371 * eval: (c-set-style "gnu")