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.
17 * @brief NAT64 global declarations
19 #ifndef __included_nat64_h__
20 #define __included_nat64_h__
23 #include <nat/nat64_db.h>
25 #define foreach_nat64_tcp_ses_state \
26 _(0, CLOSED, "closed") \
27 _(1, V4_INIT, "v4-init") \
28 _(2, V6_INIT, "v6-init") \
29 _(3, ESTABLISHED, "established") \
30 _(4, V4_FIN_RCV, "v4-fin-rcv") \
31 _(5, V6_FIN_RCV, "v6-fin-rcv") \
32 _(6, V6_FIN_V4_FIN_RCV, "v6-fin-v4-fin-rcv") \
37 #define _(v, N, s) NAT64_TCP_STATE_##N = v,
38 foreach_nat64_tcp_ses_state
40 } nat64_tcp_ses_state_t;
44 NAT64_CLEANER_RESCHEDULE = 1,
45 } nat64_cleaner_process_event_e;
57 ip6_address_t in_addr;
59 ip4_address_t out_addr;
66 } nat64_static_bib_to_update_t;
71 snat_interface_t *interfaces;
73 /** Address pool vector */
74 snat_address_t *addr_pool;
76 /** sw_if_indices whose interface addresses should be auto-added */
77 u32 *auto_add_sw_if_indices;
80 nat64_prefix_t *pref64;
82 /** BIB and session DB per thread */
89 /** Pool of static BIB entries to be added/deleted in worker threads */
90 nat64_static_bib_to_update_t *static_bibs;
92 /** config parameters */
94 uword bib_memory_size;
98 /** values of various timeouts */
101 u32 tcp_trans_timeout;
104 /* Total count of interfaces enabled */
105 u32 total_enabled_count;
106 /* The process node which orcherstrates the cleanup */
107 u32 nat64_expire_walk_node_index;
109 /* counters/gauges */
110 vlib_simple_counter_main_t total_bibs;
111 vlib_simple_counter_main_t total_sessions;
114 u32 error_node_index;
116 u32 in2out_node_index;
117 u32 in2out_slowpath_node_index;
119 u32 out2in_node_index;
121 ip4_main_t *ip4_main;
124 #define _(x) vlib_simple_counter_main_t x;
141 extern nat64_main_t nat64_main;
142 extern vlib_node_registration_t nat64_in2out_node;
143 extern vlib_node_registration_t nat64_out2in_node;
146 * @brief Add/delete address to NAT64 pool.
148 * @param thread_index Thread index used by ipfix nat logging (not address per thread).
149 * @param addr IPv4 address.
150 * @param vrf_id VRF id of tenant, ~0 means independent of VRF.
151 * @param is_add 1 if add, 0 if delete.
153 * @returns 0 on success, non-zero value otherwise.
155 int nat64_add_del_pool_addr (u32 thread_index,
156 ip4_address_t * addr, u32 vrf_id, u8 is_add);
159 * @brief Call back function when walking addresses in NAT64 pool, non-zero
160 * return value stop walk.
162 typedef int (*nat64_pool_addr_walk_fn_t) (snat_address_t * addr, void *ctx);
165 * @brief Walk NAT64 pool.
167 * @param fn The function to invoke on each entry visited.
168 * @param ctx A context passed in the visit function.
170 void nat64_pool_addr_walk (nat64_pool_addr_walk_fn_t fn, void *ctx);
173 * @brief NAT64 pool address from specific (DHCP addressed) interface.
175 * @param sw_if_index Index of the interface.
176 * @param is_add 1 if add, 0 if delete.
178 * @returns 0 on success, non-zero value otherwise.
180 int nat64_add_interface_address (u32 sw_if_index, int is_add);
183 * @brief Enable/disable NAT64 feature on the interface.
185 * @param sw_if_index Index of the interface.
186 * @param is_inside 1 if inside, 0 if outside.
187 * @param is_add 1 if add, 0 if delete.
189 * @returns 0 on success, non-zero value otherwise.
191 int nat64_add_del_interface (u32 sw_if_index, u8 is_inside, u8 is_add);
194 * @brief Call back function when walking interfaces with NAT64 feature,
195 * non-zero return value stop walk.
197 typedef int (*nat64_interface_walk_fn_t) (snat_interface_t * i, void *ctx);
200 * @brief Walk NAT64 interfaces.
202 * @param fn The function to invoke on each entry visited.
203 * @param ctx A context passed in the visit function.
205 void nat64_interfaces_walk (nat64_interface_walk_fn_t fn, void *ctx);
208 * @brief Initialize NAT64.
210 * @param vm vlib main.
212 * @return error code.
214 clib_error_t *nat64_init (vlib_main_t * vm);
217 * @brief Add/delete static NAT64 BIB entry.
219 * @param in_addr Inside IPv6 address.
220 * @param out_addr Outside IPv4 address.
221 * @param in_port Inside port number.
222 * @param out_port Outside port number.
223 * @param proto L4 protocol.
224 * @param vrf_id VRF id of tenant.
225 * @param is_add 1 if add, 0 if delete.
227 * @returns 0 on success, non-zero value otherwise.
229 int nat64_add_del_static_bib_entry (ip6_address_t * in_addr,
230 ip4_address_t * out_addr, u16 in_port,
231 u16 out_port, u8 proto, u32 vrf_id,
235 * @brief Alloce IPv4 address and port pair from NAT64 pool.
237 * @param fib_index FIB index of tenant.
238 * @param proto L4 protocol.
239 * @param addr Allocated IPv4 address.
240 * @param port Allocated port number.
241 * @param thread_index Thread index.
243 * @returns 0 on success, non-zero value otherwise.
245 int nat64_alloc_out_addr_and_port (u32 fib_index, nat_protocol_t proto,
246 ip4_address_t * addr, u16 * port,
250 * @brief Set UDP session timeout.
252 * @param timeout Timeout value in seconds (if 0 reset to default value 300sec).
254 * @returns 0 on success, non-zero value otherwise.
256 int nat64_set_udp_timeout (u32 timeout);
259 * @brief Get UDP session timeout.
261 * @returns UDP session timeout in seconds.
263 u32 nat64_get_udp_timeout (void);
266 * @brief Set ICMP session timeout.
268 * @param timeout Timeout value in seconds (if 0 reset to default value 60sec).
270 * @returns 0 on success, non-zero value otherwise.
272 int nat64_set_icmp_timeout (u32 timeout);
275 * @brief Get ICMP session timeout.
277 * @returns ICMP session timeout in seconds.
279 u32 nat64_get_icmp_timeout (void);
282 * @brief Set TCP session timeouts.
284 * @param trans Transitory timeout in seconds (if 0 reset to default value 240sec).
285 * @param est Established timeout in seconds (if 0 reset to default value 7440sec).
287 * @returns 0 on success, non-zero value otherwise.
289 int nat64_set_tcp_timeouts (u32 trans, u32 est);
292 * @brief Get TCP transitory timeout.
294 * @returns TCP transitory timeout in seconds.
296 u32 nat64_get_tcp_trans_timeout (void);
299 * @brief Get TCP established timeout.
301 * @returns TCP established timeout in seconds.
303 u32 nat64_get_tcp_est_timeout (void);
306 * @brief Reset NAT64 session timeout.
308 * @param ste Session table entry.
309 * @param vm VLIB main.
311 void nat64_session_reset_timeout (nat64_db_st_entry_t * ste,
315 * @brief Set NAT64 TCP session state.
317 * @param ste Session table entry.
318 * @param tcp TCP header.
319 * @param is_ip6 1 if IPv6 packet, 0 if IPv4.
321 void nat64_tcp_session_set_state (nat64_db_st_entry_t * ste,
322 tcp_header_t * tcp, u8 is_ip6);
325 * @brief Add/delete NAT64 prefix.
327 * @param prefix NAT64 prefix.
328 * @param plen Prefix length.
329 * @param vrf_id VRF id of tenant.
330 * @param is_add 1 if add, 0 if delete.
332 * @returns 0 on success, non-zero value otherwise.
334 int nat64_add_del_prefix (ip6_address_t * prefix, u8 plen, u32 vrf_id,
338 * @brief Call back function when walking addresses in NAT64 prefixes, non-zero
339 * return value stop walk.
341 typedef int (*nat64_prefix_walk_fn_t) (nat64_prefix_t * pref64, void *ctx);
344 * @brief Walk NAT64 prefixes.
346 * @param fn The function to invoke on each entry visited.
347 * @param ctx A context passed in the visit function.
349 void nat64_prefix_walk (nat64_prefix_walk_fn_t fn, void *ctx);
352 * Compose IPv4-embedded IPv6 addresses.
353 * @param ip6 IPv4-embedded IPv6 addresses.
354 * @param ip4 IPv4 address.
355 * @param fib_index Tenant FIB index.
357 void nat64_compose_ip6 (ip6_address_t * ip6, ip4_address_t * ip4,
361 * Extract IPv4 address from the IPv4-embedded IPv6 addresses.
363 * @param ip6 IPv4-embedded IPv6 addresses.
364 * @param ip4 IPv4 address.
365 * @param fib_index Tenant FIB index.
367 void nat64_extract_ip4 (ip6_address_t * ip6, ip4_address_t * ip4,
371 * @brief Set NAT64 hash tables configuration.
373 * @param bib_buckets Number of BIB hash buckets.
374 * @param bib_memory_size Memory size of BIB hash.
375 * @param st_buckets Number of session table hash buckets.
376 * @param st_memory_size Memory size of session table hash.
378 void nat64_set_hash (u32 bib_buckets, uword bib_memory_size, u32 st_buckets,
379 uword st_memory_size);
382 * @brief Get worker thread index for NAT64 in2out.
384 * @param addr IPv6 src address.
386 * @returns worker thread index.
388 u32 nat64_get_worker_in2out (ip6_address_t * addr);
391 * @brief Get worker thread index for NAT64 out2in.
393 * @param ip IPv4 header.
395 * @returns worker thread index.
397 u32 nat64_get_worker_out2in (vlib_buffer_t * b, ip4_header_t * ip);
399 #endif /* __included_nat64_h__ */
402 * fd.io coding-style-patch-verification: ON
405 * eval: (c-set-style "gnu")