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;
79 ip46_address_t l_addr;
80 ip46_address_t r_addr;
89 } nat64_db_st_entry_key_t;
92 typedef CLIB_PACKED(struct
94 ip6_address_t in_r_addr;
95 ip4_address_t out_r_addr;
101 }) nat64_db_st_entry_t;
108 #define _(N, i, n, s) \
109 nat64_db_st_entry_t *_##n##_st;
110 foreach_snat_protocol
113 nat64_db_st_entry_t *_unk_proto_st;
116 clib_bihash_48_8_t in2out;
117 clib_bihash_48_8_t out2in;
127 * @brief Initialize NAT64 DB.
129 * @param db NAT64 DB.
130 * @param bib_buckets Number of BIB hash buckets.
131 * @param bib_memory_size Memory size of BIB hash.
132 * @param st_buckets Number of session table hash buckets.
133 * @param st_memory_size Memory size of session table hash.
135 * @returns 0 on success, non-zero value otherwise.
137 int nat64_db_init (nat64_db_t * db, u32 bib_buckets, u32 bib_memory_size,
138 u32 st_buckets, u32 st_memory_size);
141 * @brief Create new NAT64 BIB entry.
143 * @param db NAT64 DB.
144 * @param in_addr Inside IPv6 address.
145 * @param out_addr Outside IPv4 address.
146 * @param in_port Inside port number.
147 * @param out_port Outside port number.
148 * @param fib_index FIB index.
149 * @param proto L4 protocol.
150 * @param is_static 1 if static, 0 if dynamic.
152 * @returns BIB entry on success, 0 otherwise.
154 nat64_db_bib_entry_t *nat64_db_bib_entry_create (nat64_db_t * db,
155 ip6_address_t * in_addr,
156 ip4_address_t * out_addr,
157 u16 in_port, u16 out_port,
159 u8 proto, u8 is_static);
162 * @brief Free NAT64 BIB entry.
164 * @param db NAT64 DB.
165 * @param bibe BIB entry.
167 void nat64_db_bib_entry_free (nat64_db_t * db, nat64_db_bib_entry_t * bibe);
170 * @brief Call back function when walking NAT64 BIB, non-zero
171 * return value stop walk.
173 typedef int (*nat64_db_bib_walk_fn_t) (nat64_db_bib_entry_t * bibe,
176 * @brief Walk NAT64 BIB.
178 * @param db NAT64 DB.
179 * @param proto BIB L4 protocol:
184 * - otherwise "unknown" protocol BIB
185 * @param fn The function to invoke on each entry visited.
186 * @param ctx A context passed in the visit function.
188 void nat64_db_bib_walk (nat64_db_t * db, u8 proto,
189 nat64_db_bib_walk_fn_t fn, void *ctx);
192 * @brief Find NAT64 BIB entry.
194 * @param db NAT64 DB.
195 * @param addr IP address.
196 * @param port Port number.
197 * @param proto L4 protocol.
198 * @param fib_index FIB index.
199 * @param is_ip6 1 if find by IPv6 (inside) address, 0 by IPv4 (outside).
201 * @return BIB entry if found.
203 nat64_db_bib_entry_t *nat64_db_bib_entry_find (nat64_db_t * db,
204 ip46_address_t * addr,
207 u32 fib_index, u8 is_ip6);
210 * @brief Get BIB entry by index and protocol.
212 * @param db NAT64 DB.
213 * @param proto L4 protocol.
214 * @param bibe_index BIB entry index.
216 * @return BIB entry if found.
218 nat64_db_bib_entry_t *nat64_db_bib_entry_by_index (nat64_db_t * db,
219 u8 proto, u32 bibe_index);
221 * @brief Create new NAT64 session table entry.
223 * @param db NAT64 DB.
224 * @param bibe Corresponding BIB entry.
225 * @param in_r_addr Inside IPv6 address of the remote host.
226 * @param out_r_addr Outside IPv4 address of the remote host.
227 * @param r_port Remote host port number.
229 * @returns BIB entry on success, 0 otherwise.
231 nat64_db_st_entry_t *nat64_db_st_entry_create (nat64_db_t * db,
232 nat64_db_bib_entry_t * bibe,
233 ip6_address_t * in_r_addr,
234 ip4_address_t * out_r_addr,
238 * @brief Free NAT64 session table entry.
240 * @param db NAT64 DB.
241 * @param ste Session table entry.
243 void nat64_db_st_entry_free (nat64_db_t * db, nat64_db_st_entry_t * ste);
246 * @brief Find NAT64 session table entry.
248 * @param db NAT64 DB.
249 * @param l_addr Local host address.
250 * @param r_addr Remote host address.
251 * @param l_port Local host port number.
252 * @param r_port Remote host port number.
253 * @param proto L4 protocol.
254 * @param fib_index FIB index.
255 * @param is_ip6 1 if find by IPv6 (inside) address, 0 by IPv4 (outside).
257 * @return BIB entry if found.
259 nat64_db_st_entry_t *nat64_db_st_entry_find (nat64_db_t * db,
260 ip46_address_t * l_addr,
261 ip46_address_t * r_addr,
262 u16 l_port, u16 r_port,
264 u32 fib_index, u8 is_ip6);
267 * @brief Call back function when walking NAT64 session table, non-zero
268 * return value stop walk.
270 typedef int (*nat64_db_st_walk_fn_t) (nat64_db_st_entry_t * ste, void *ctx);
273 * @brief Walk NAT64 session table.
275 * @param db NAT64 DB.
276 * @param proto L4 protocol:
277 * - 255 all session tables
278 * - 6 TCP session table
279 * - 17 UDP session table
280 * - 1/58 ICMP session table
281 * - otherwise "unknown" protocol session table
282 * @param fn The function to invoke on each entry visited.
283 * @param ctx A context passed in the visit function.
285 void nat64_db_st_walk (nat64_db_t * db, u8 proto,
286 nat64_db_st_walk_fn_t fn, void *ctx);
289 * @brief Free expired session entries in session tables.
291 * @param db NAT64 DB.
292 * @param now Current time.
294 void nad64_db_st_free_expired (nat64_db_t * db, u32 now);
297 * @brief Free sessions using specific outside address.
299 * @param db NAT64 DB.
300 * @param out_addr Outside address to match.
302 void nat64_db_free_out_addr (nat64_db_t * db, ip4_address_t * out_addr);
305 * @brief Get ST entry index.
307 * @param db NAT64 DB.
308 * @param ste ST entry.
310 * @return ST entry index on success, ~0 otherwise.
312 u32 nat64_db_st_entry_get_index (nat64_db_t * db, nat64_db_st_entry_t * ste);
315 * @brief Get ST entry by index and protocol.
317 * @param db NAT64 DB.
318 * @param proto L4 protocol.
319 * @param bibe_index ST entry index.
321 * @return BIB entry if found.
323 nat64_db_st_entry_t *nat64_db_st_entry_by_index (nat64_db_t * db,
324 u8 proto, u32 ste_index);
325 #endif /* __included_nat64_db_h__ */
328 * fd.io coding-style-patch-verification: ON
331 * eval: (c-set-style "gnu")