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 db NAT64 DB.
164 * @param in_addr Inside IPv6 address.
165 * @param out_addr Outside IPv4 address.
166 * @param in_port Inside port number.
167 * @param out_port Outside port number.
168 * @param fib_index FIB index.
169 * @param proto L4 protocol.
170 * @param is_static 1 if static, 0 if dynamic.
172 * @returns BIB entry on success, 0 otherwise.
174 nat64_db_bib_entry_t *nat64_db_bib_entry_create (nat64_db_t * db,
175 ip6_address_t * in_addr,
176 ip4_address_t * out_addr,
177 u16 in_port, u16 out_port,
179 u8 proto, u8 is_static);
182 * @brief Free NAT64 BIB entry.
184 * @param db NAT64 DB.
185 * @param bibe BIB entry.
187 void nat64_db_bib_entry_free (nat64_db_t * db, nat64_db_bib_entry_t * bibe);
190 * @brief Call back function when walking NAT64 BIB, non-zero
191 * return value stop walk.
193 typedef int (*nat64_db_bib_walk_fn_t) (nat64_db_bib_entry_t * bibe,
196 * @brief Walk NAT64 BIB.
198 * @param db NAT64 DB.
199 * @param proto BIB L4 protocol:
204 * - otherwise "unknown" protocol BIB
205 * @param fn The function to invoke on each entry visited.
206 * @param ctx A context passed in the visit function.
208 void nat64_db_bib_walk (nat64_db_t * db, u8 proto,
209 nat64_db_bib_walk_fn_t fn, void *ctx);
212 * @brief Find NAT64 BIB entry.
214 * @param db NAT64 DB.
215 * @param addr IP address.
216 * @param port Port number.
217 * @param proto L4 protocol.
218 * @param fib_index FIB index.
219 * @param is_ip6 1 if find by IPv6 (inside) address, 0 by IPv4 (outside).
221 * @return BIB entry if found.
223 nat64_db_bib_entry_t *nat64_db_bib_entry_find (nat64_db_t * db,
224 ip46_address_t * addr,
227 u32 fib_index, u8 is_ip6);
230 * @brief Get BIB entry by index and protocol.
232 * @param db NAT64 DB.
233 * @param proto L4 protocol.
234 * @param bibe_index BIB entry index.
236 * @return BIB entry if found.
238 nat64_db_bib_entry_t *nat64_db_bib_entry_by_index (nat64_db_t * db,
239 u8 proto, u32 bibe_index);
241 * @brief Create new NAT64 session table entry.
243 * @param db NAT64 DB.
244 * @param bibe Corresponding BIB entry.
245 * @param in_r_addr Inside IPv6 address of the remote host.
246 * @param out_r_addr Outside IPv4 address of the remote host.
247 * @param r_port Remote host port number.
249 * @returns BIB entry on success, 0 otherwise.
251 nat64_db_st_entry_t *nat64_db_st_entry_create (nat64_db_t * db,
252 nat64_db_bib_entry_t * bibe,
253 ip6_address_t * in_r_addr,
254 ip4_address_t * out_r_addr,
258 * @brief Free NAT64 session table entry.
260 * @param db NAT64 DB.
261 * @param ste Session table entry.
263 void nat64_db_st_entry_free (nat64_db_t * db, nat64_db_st_entry_t * ste);
266 * @brief Find NAT64 session table entry.
268 * @param db NAT64 DB.
269 * @param l_addr Local host address.
270 * @param r_addr Remote host address.
271 * @param l_port Local host port number.
272 * @param r_port Remote host port number.
273 * @param proto L4 protocol.
274 * @param fib_index FIB index.
275 * @param is_ip6 1 if find by IPv6 (inside) address, 0 by IPv4 (outside).
277 * @return BIB entry if found.
279 nat64_db_st_entry_t *nat64_db_st_entry_find (nat64_db_t * db,
280 ip46_address_t * l_addr,
281 ip46_address_t * r_addr,
282 u16 l_port, u16 r_port,
284 u32 fib_index, u8 is_ip6);
287 * @brief Call back function when walking NAT64 session table, non-zero
288 * return value stop walk.
290 typedef int (*nat64_db_st_walk_fn_t) (nat64_db_st_entry_t * ste, void *ctx);
293 * @brief Walk NAT64 session table.
295 * @param db NAT64 DB.
296 * @param proto L4 protocol:
297 * - 255 all session tables
298 * - 6 TCP session table
299 * - 17 UDP session table
300 * - 1/58 ICMP session table
301 * - otherwise "unknown" protocol session table
302 * @param fn The function to invoke on each entry visited.
303 * @param ctx A context passed in the visit function.
305 void nat64_db_st_walk (nat64_db_t * db, u8 proto,
306 nat64_db_st_walk_fn_t fn, void *ctx);
309 * @brief Free expired session entries in session tables.
311 * @param db NAT64 DB.
312 * @param now Current time.
314 void nad64_db_st_free_expired (nat64_db_t * db, u32 now);
317 * @brief Free sessions using specific outside address.
319 * @param db NAT64 DB.
320 * @param out_addr Outside address to match.
322 void nat64_db_free_out_addr (nat64_db_t * db, ip4_address_t * out_addr);
325 * @brief Get ST entry index.
327 * @param db NAT64 DB.
328 * @param ste ST entry.
330 * @return ST entry index on success, ~0 otherwise.
332 u32 nat64_db_st_entry_get_index (nat64_db_t * db, nat64_db_st_entry_t * ste);
335 * @brief Get ST entry by index and protocol.
337 * @param db NAT64 DB.
338 * @param proto L4 protocol.
339 * @param bibe_index ST entry index.
341 * @return BIB entry if found.
343 nat64_db_st_entry_t *nat64_db_st_entry_by_index (nat64_db_t * db,
344 u8 proto, u32 ste_index);
345 #endif /* __included_nat64_db_h__ */
348 * fd.io coding-style-patch-verification: ON
351 * eval: (c-set-style "gnu")