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>
22 #include <vppinfra/bihash_24_8.h>
23 #include <vppinfra/bihash_48_8.h>
47 } 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;
67 #define _(N, i, n, s) \
68 nat64_db_bib_entry_t *_##n##_bib;
72 nat64_db_bib_entry_t *_unk_proto_bib;
75 clib_bihash_24_8_t in2out;
76 clib_bihash_24_8_t out2in;
88 ip46_address_t l_addr;
89 ip46_address_t r_addr;
98 } nat64_db_st_entry_key_t;
101 typedef CLIB_PACKED(struct
103 ip6_address_t in_r_addr;
104 ip4_address_t out_r_addr;
110 }) nat64_db_st_entry_t;
117 #define _(N, i, n, s) \
118 nat64_db_st_entry_t *_##n##_st;
122 nat64_db_st_entry_t *_unk_proto_st;
125 clib_bihash_48_8_t in2out;
126 clib_bihash_48_8_t out2in;
135 * @brief Call back function to free NAT64 pool address and port when BIB
138 typedef void (*nat64_db_free_addr_port_function_t) (struct nat64_db_s * db,
139 ip4_address_t * addr,
142 typedef struct nat64_db_s
146 nat64_db_free_addr_port_function_t free_addr_port_cb;
151 * @brief Initialize NAT64 DB.
153 * @param db NAT64 DB.
154 * @param c.bib_buckets Number of BIB hash buckets.
155 * @param c.bib_memory_size Memory size of BIB hash.
156 * @param c.st_buckets Number of session table hash buckets.
157 * @param c.st_memory_size Memory size of session table hash.
158 * @param free_addr_port_cb Call back function to free address and port.
160 * @returns 0 on success, non-zero value otherwise.
162 int nat64_db_init (nat64_db_t * db, nat64_config_t c,
163 nat64_db_free_addr_port_function_t free_addr_port_cb);
166 * @brief Free NAT64 DB.
168 * @param db NAT64 DB.
170 * @returns 0 on success, non-zero value otherwise.
172 int nat64_db_free (nat64_db_t * db);
176 * @brief Create new NAT64 BIB entry.
178 * @param thread_index thread index.
179 * @param db NAT64 DB.
180 * @param in_addr Inside IPv6 address.
181 * @param out_addr Outside IPv4 address.
182 * @param in_port Inside port number.
183 * @param out_port Outside port number.
184 * @param fib_index FIB index.
185 * @param proto L4 protocol.
186 * @param is_static 1 if static, 0 if dynamic.
188 * @returns BIB entry on success, 0 otherwise.
190 nat64_db_bib_entry_t *nat64_db_bib_entry_create (u32 thread_index,
192 ip6_address_t * in_addr,
193 ip4_address_t * out_addr,
194 u16 in_port, u16 out_port,
195 u32 fib_index, u8 proto,
199 * @brief Free NAT64 BIB entry.
201 * @param thread_index thread index.
202 * @param db NAT64 DB.
203 * @param bibe BIB entry.
205 void nat64_db_bib_entry_free (u32 thread_index, nat64_db_t * db,
206 nat64_db_bib_entry_t * bibe);
209 * @brief Call back function when walking NAT64 BIB, non-zero
210 * return value stop walk.
212 typedef int (*nat64_db_bib_walk_fn_t) (nat64_db_bib_entry_t * bibe,
215 * @brief Walk NAT64 BIB.
217 * @param db NAT64 DB.
218 * @param proto BIB L4 protocol:
224 * u - otherwise "unknown" protocol BIB
225 * @param fn The function to invoke on each entry visited.
226 * @param ctx A context passed in the visit function.
228 void nat64_db_bib_walk (nat64_db_t * db, u8 proto,
229 nat64_db_bib_walk_fn_t fn, void *ctx);
232 * @brief Find NAT64 BIB entry.
234 * @param db NAT64 DB.
235 * @param addr IP address.
236 * @param port Port number.
237 * @param proto L4 protocol.
238 * @param fib_index FIB index.
239 * @param is_ip6 1 if find by IPv6 (inside) address, 0 by IPv4 (outside).
241 * @return BIB entry if found.
243 nat64_db_bib_entry_t *nat64_db_bib_entry_find (nat64_db_t * db,
244 ip46_address_t * addr,
247 u32 fib_index, u8 is_ip6);
250 * @brief Get BIB entry by index and protocol.
252 * @param db NAT64 DB.
253 * @param proto L4 protocol.
254 * @param bibe_index BIB entry index.
256 * @return BIB entry if found.
258 nat64_db_bib_entry_t *nat64_db_bib_entry_by_index (nat64_db_t * db,
259 u8 proto, u32 bibe_index);
261 * @brief Create new NAT64 session table entry.
263 * @param thread_index thread index.
264 * @param db NAT64 DB.
265 * @param bibe Corresponding BIB entry.
266 * @param in_r_addr Inside IPv6 address of the remote host.
267 * @param out_r_addr Outside IPv4 address of the remote host.
268 * @param r_port Remote host port number.
270 * @returns BIB entry on success, 0 otherwise.
272 nat64_db_st_entry_t *nat64_db_st_entry_create (u32 thread_index,
274 nat64_db_bib_entry_t * bibe,
275 ip6_address_t * in_r_addr,
276 ip4_address_t * out_r_addr,
280 * @brief Free NAT64 session table entry.
282 * @param thread_index thread index.
283 * @param db NAT64 DB.
284 * @param ste Session table entry.
286 void nat64_db_st_entry_free (u32 thread_index, nat64_db_t * db,
287 nat64_db_st_entry_t * ste);
290 * @brief Find NAT64 session table entry.
292 * @param db NAT64 DB.
293 * @param l_addr Local host address.
294 * @param r_addr Remote host address.
295 * @param l_port Local host port number.
296 * @param r_port Remote host port number.
297 * @param proto L4 protocol.
298 * @param fib_index FIB index.
299 * @param is_ip6 1 if find by IPv6 (inside) address, 0 by IPv4 (outside).
301 * @return BIB entry if found.
303 nat64_db_st_entry_t *nat64_db_st_entry_find (nat64_db_t * db,
304 ip46_address_t * l_addr,
305 ip46_address_t * r_addr,
306 u16 l_port, u16 r_port,
308 u32 fib_index, u8 is_ip6);
311 * @brief Call back function when walking NAT64 session table, non-zero
312 * return value stop walk.
314 typedef int (*nat64_db_st_walk_fn_t) (nat64_db_st_entry_t * ste, void *ctx);
317 * @brief Walk NAT64 session table.
319 * @param db NAT64 DB.
320 * @param proto L4 protocol:
321 * - 255 all session tables
322 * - 6 TCP session table
323 * - 17 UDP session table
324 * - 1/58 ICMP session table
325 * - otherwise "unknown" protocol session table
326 * @param fn The function to invoke on each entry visited.
327 * @param ctx A context passed in the visit function.
329 void nat64_db_st_walk (nat64_db_t * db, u8 proto,
330 nat64_db_st_walk_fn_t fn, void *ctx);
333 * @brief Free expired session entries in session tables.
335 * @param thread_index thread index.
336 * @param db NAT64 DB.
337 * @param now Current time.
339 void nad64_db_st_free_expired (u32 thread_index, nat64_db_t * db, u32 now);
342 * @brief Free sessions using specific outside address.
344 * @param thread_index thread index.
345 * @param db NAT64 DB.
346 * @param out_addr Outside address to match.
348 void nat64_db_free_out_addr (u32 thread_index, nat64_db_t * db,
349 ip4_address_t * out_addr);
352 * @brief Get ST entry index.
354 * @param db NAT64 DB.
355 * @param ste ST entry.
357 * @return ST entry index on success, ~0 otherwise.
359 u32 nat64_db_st_entry_get_index (nat64_db_t * db, nat64_db_st_entry_t * ste);
362 * @brief Get ST entry by index and protocol.
364 * @param db NAT64 DB.
365 * @param proto L4 protocol.
366 * @param bibe_index ST entry index.
368 * @return BIB entry if found.
370 nat64_db_st_entry_t *nat64_db_st_entry_by_index (nat64_db_t * db,
371 u8 proto, u32 ste_index);
372 #endif /* __included_nat64_db_h__ */
375 * fd.io coding-style-patch-verification: ON
378 * eval: (c-set-style "gnu")