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__
22 #include <snat/snat.h>
23 #include <snat/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;
45 snat_interface_t *interfaces;
47 /** Address pool vector */
48 snat_address_t *addr_pool;
50 /** BIB and session DB **/
53 /* values of various timeouts */
56 u32 tcp_trans_timeout;
58 u32 tcp_incoming_syn_timeout;
63 extern nat64_main_t nat64_main;
64 extern vlib_node_registration_t nat64_in2out_node;
65 extern vlib_node_registration_t nat64_out2in_node;
68 * @brief Add/delete address to NAT64 pool.
70 * @param addr IPv4 address.
71 * @param vrf_id VRF id of tenant, ~0 means independent of VRF.
72 * @param is_add 1 if add, 0 if delete.
74 * @returns 0 on success, non-zero value otherwise.
76 int nat64_add_del_pool_addr (ip4_address_t * addr, u32 vrf_id, u8 is_add);
79 * @brief Call back function when walking addresses in NAT64 pool, non-zero
80 * return value stop walk.
82 typedef int (*nat64_pool_addr_walk_fn_t) (snat_address_t * addr, void *ctx);
85 * @brief Walk NAT64 pool.
87 * @param fn The function to invoke on each entry visited.
88 * @param ctx A context passed in the visit function.
90 void nat64_pool_addr_walk (nat64_pool_addr_walk_fn_t fn, void *ctx);
93 * @brief Enable/disable NAT64 feature on the interface.
95 * @param sw_if_index Index of the interface.
96 * @param is_inside 1 if inside, 0 if outside.
97 * @param is_add 1 if add, 0 if delete.
99 * @returns 0 on success, non-zero value otherwise.
101 int nat64_add_del_interface (u32 sw_if_index, u8 is_inside, u8 is_add);
104 * @brief Call back function when walking interfaces with NAT64 feature,
105 * non-zero return value stop walk.
107 typedef int (*nat64_interface_walk_fn_t) (snat_interface_t * i, void *ctx);
110 * @brief Walk NAT64 interfaces.
112 * @param fn The function to invoke on each entry visited.
113 * @param ctx A context passed in the visit function.
115 void nat64_interfaces_walk (nat64_interface_walk_fn_t fn, void *ctx);
118 * @brief Initialize NAT64.
120 * @param vm vlib main.
122 * @return error code.
124 clib_error_t *nat64_init (vlib_main_t * vm);
127 * @brief Add/delete static NAT64 BIB entry.
129 * @param in_addr Inside IPv6 address.
130 * @param out_addr Outside IPv4 address.
131 * @param in_port Inside port number.
132 * @param out_port Outside port number.
133 * @param proto L4 protocol.
134 * @param vrf_id VRF id of tenant.
135 * @param is_add 1 if add, 0 if delete.
137 * @returns 0 on success, non-zero value otherwise.
139 int nat64_add_del_static_bib_entry (ip6_address_t * in_addr,
140 ip4_address_t * out_addr, u16 in_port,
141 u16 out_port, u8 proto, u32 vrf_id,
145 * @brief Alloce IPv4 address and port pair from NAT64 pool.
147 * @param fib_index FIB index of tenant.
148 * @param proto L4 protocol.
149 * @param addr Allocated IPv4 address.
150 * @param port Allocated port number.
152 * @returns 0 on success, non-zero value otherwise.
154 int nat64_alloc_out_addr_and_port (u32 fib_index, snat_protocol_t proto,
155 ip4_address_t * addr, u16 * port);
158 * @brief Free IPv4 address and port pair from NAT64 pool.
160 * @param addr IPv4 address to free.
161 * @param port Port number to free.
162 * @param proto L4 protocol.
164 * @returns 0 on success, non-zero value otherwise.
166 void nat64_free_out_addr_and_port (ip4_address_t * addr, u16 port,
167 snat_protocol_t proto);
170 * @brief Set UDP session timeout.
172 * @param timeout Timeout value in seconds (if 0 reset to default value 300sec).
174 * @returns 0 on success, non-zero value otherwise.
176 int nat64_set_udp_timeout (u32 timeout);
179 * @brief Get UDP session timeout.
181 * @returns UDP session timeout in seconds.
183 u32 nat64_get_udp_timeout (void);
186 * @brief Set ICMP session timeout.
188 * @param timeout Timeout value in seconds (if 0 reset to default value 60sec).
190 * @returns 0 on success, non-zero value otherwise.
192 int nat64_set_icmp_timeout (u32 timeout);
195 * @brief Get ICMP session timeout.
197 * @returns ICMP session timeout in seconds.
199 u32 nat64_get_icmp_timeout (void);
202 * @brief Set TCP session timeouts.
204 * @param trans Transitory timeout in seconds (if 0 reset to default value 240sec).
205 * @param est Established timeout in seconds (if 0 reset to default value 7440sec).
206 * @param incoming_syn Incoming SYN timeout in seconds (if 0 reset to default value 6sec).
208 * @returns 0 on success, non-zero value otherwise.
210 int nat64_set_tcp_timeouts (u32 trans, u32 est, u32 incoming_syn);
213 * @brief Get TCP transitory timeout.
215 * @returns TCP transitory timeout in seconds.
217 u32 nat64_get_tcp_trans_timeout (void);
220 * @brief Get TCP established timeout.
222 * @returns TCP established timeout in seconds.
224 u32 nat64_get_tcp_est_timeout (void);
227 * @brief Get TCP incoming SYN timeout.
229 * @returns TCP incoming SYN timeout in seconds.
231 u32 nat64_get_tcp_incoming_syn_timeout (void);
234 * @brief Reset NAT64 session timeout.
236 * @param ste Session table entry.
237 * @param vm VLIB main.
239 void nat64_session_reset_timeout (nat64_db_st_entry_t * ste,
243 * @brief Set NAT64 TCP session state.
245 * @param ste Session table entry.
246 * @param tcp TCP header.
247 * @param is_ip6 1 if IPv6 packet, 0 if IPv4.
249 void nat64_tcp_session_set_state (nat64_db_st_entry_t * ste,
250 tcp_header_t * tcp, u8 is_ip6);
252 #define u8_ptr_add(ptr, index) (((u8 *)ptr) + index)
253 #define u16_net_add(u, val) clib_host_to_net_u16(clib_net_to_host_u16(u) + (val))
255 #endif /* __included_nat64_h__ */
258 * fd.io coding-style-patch-verification: ON
261 * eval: (c-set-style "gnu")