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.
131 * @returns 0 on success, non-zero value otherwise.
133 int nat64_db_init (nat64_db_t * db);
136 * @brief Create new NAT64 BIB entry.
138 * @param db NAT64 DB.
139 * @param in_addr Inside IPv6 address.
140 * @param out_addr Outside IPv4 address.
141 * @param in_port Inside port number.
142 * @param out_port Outside port number.
143 * @param fib_index FIB index.
144 * @param proto L4 protocol.
145 * @param is_static 1 if static, 0 if dynamic.
147 * @returns BIB entry on success, 0 otherwise.
149 nat64_db_bib_entry_t *nat64_db_bib_entry_create (nat64_db_t * db,
150 ip6_address_t * in_addr,
151 ip4_address_t * out_addr,
152 u16 in_port, u16 out_port,
154 u8 proto, u8 is_static);
157 * @brief Free NAT64 BIB entry.
159 * @param db NAT64 DB.
160 * @param bibe BIB entry.
162 void nat64_db_bib_entry_free (nat64_db_t * db, nat64_db_bib_entry_t * bibe);
165 * @brief Call back function when walking NAT64 BIB, non-zero
166 * return value stop walk.
168 typedef int (*nat64_db_bib_walk_fn_t) (nat64_db_bib_entry_t * bibe,
171 * @brief Walk NAT64 BIB.
173 * @param db NAT64 DB.
174 * @param proto BIB L4 protocol:
179 * - otherwise "unknown" protocol BIB
180 * @param fn The function to invoke on each entry visited.
181 * @param ctx A context passed in the visit function.
183 void nat64_db_bib_walk (nat64_db_t * db, u8 proto,
184 nat64_db_bib_walk_fn_t fn, void *ctx);
187 * @brief Find NAT64 BIB entry.
189 * @param db NAT64 DB.
190 * @param addr IP address.
191 * @param port Port number.
192 * @param proto L4 protocol.
193 * @param fib_index FIB index.
194 * @param is_ip6 1 if find by IPv6 (inside) address, 0 by IPv4 (outside).
196 * @return BIB entry if found.
198 nat64_db_bib_entry_t *nat64_db_bib_entry_find (nat64_db_t * db,
199 ip46_address_t * addr,
202 u32 fib_index, u8 is_ip6);
205 * @brief Get BIB entry by index and protocol.
207 * @param db NAT64 DB.
208 * @param proto L4 protocol.
209 * @param bibe_index BIB entry index.
211 * @return BIB entry if found.
213 nat64_db_bib_entry_t *nat64_db_bib_entry_by_index (nat64_db_t * db,
214 u8 proto, u32 bibe_index);
216 * @brief Create new NAT64 session table entry.
218 * @param db NAT64 DB.
219 * @param bibe Corresponding BIB entry.
220 * @param in_r_addr Inside IPv6 address of the remote host.
221 * @param out_r_addr Outside IPv4 address of the remote host.
222 * @param r_port Remote host port number.
224 * @returns BIB entry on success, 0 otherwise.
226 nat64_db_st_entry_t *nat64_db_st_entry_create (nat64_db_t * db,
227 nat64_db_bib_entry_t * bibe,
228 ip6_address_t * in_r_addr,
229 ip4_address_t * out_r_addr,
233 * @brief Free NAT64 session table entry.
235 * @param db NAT64 DB.
236 * @param ste Session table entry.
238 void nat64_db_st_entry_free (nat64_db_t * db, nat64_db_st_entry_t * ste);
241 * @brief Find NAT64 session table entry.
243 * @param db NAT64 DB.
244 * @param l_addr Local host address.
245 * @param r_addr Remote host address.
246 * @param l_port Local host port number.
247 * @param r_port Remote host port number.
248 * @param proto L4 protocol.
249 * @param fib_index FIB index.
250 * @param is_ip6 1 if find by IPv6 (inside) address, 0 by IPv4 (outside).
252 * @return BIB entry if found.
254 nat64_db_st_entry_t *nat64_db_st_entry_find (nat64_db_t * db,
255 ip46_address_t * l_addr,
256 ip46_address_t * r_addr,
257 u16 l_port, u16 r_port,
259 u32 fib_index, u8 is_ip6);
262 * @brief Call back function when walking NAT64 session table, non-zero
263 * return value stop walk.
265 typedef int (*nat64_db_st_walk_fn_t) (nat64_db_st_entry_t * ste, void *ctx);
268 * @brief Walk NAT64 session table.
270 * @param db NAT64 DB.
271 * @param proto L4 protocol:
272 * - 255 all session tables
273 * - 6 TCP session table
274 * - 17 UDP session table
275 * - 1/58 ICMP session table
276 * - otherwise "unknown" protocol session table
277 * @param fn The function to invoke on each entry visited.
278 * @param ctx A context passed in the visit function.
280 void nat64_db_st_walk (nat64_db_t * db, u8 proto,
281 nat64_db_st_walk_fn_t fn, void *ctx);
284 * @brief Free expired session entries in session tables.
286 * @param db NAT64 DB.
287 * @param now Current time.
289 void nad64_db_st_free_expired (nat64_db_t * db, u32 now);
292 * @brief Free sessions using specific outside address.
294 * @param db NAT64 DB.
295 * @param out_addr Outside address to match.
297 void nat64_db_free_out_addr (nat64_db_t * db, ip4_address_t * out_addr);
299 #endif /* __included_nat64_db_h__ */
302 * fd.io coding-style-patch-verification: ON
305 * eval: (c-set-style "gnu")