NAT: Rename snat plugin to nat (VPP-955)
[vpp.git] / src / plugins / nat / nat64.h
1 /*
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15 /**
16  * @file
17  * @brief NAT64 global declarations
18  */
19 #ifndef __included_nat64_h__
20 #define __included_nat64_h__
21
22 #include <nat/nat.h>
23 #include <nat/nat64_db.h>
24
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") \
33   _(7, TRANS, "trans")
34
35 typedef enum
36 {
37 #define _(v, N, s) NAT64_TCP_STATE_##N = v,
38   foreach_nat64_tcp_ses_state
39 #undef _
40 } nat64_tcp_ses_state_t;
41
42 typedef struct
43 {
44   ip6_address_t prefix;
45   u8 plen;
46   u32 vrf_id;
47   u32 fib_index;
48 } nat64_prefix_t;
49
50 typedef struct
51 {
52   /** Interface pool */
53   snat_interface_t *interfaces;
54
55   /** Address pool vector */
56   snat_address_t *addr_pool;
57
58   /** Pref64 vector */
59   nat64_prefix_t *pref64;
60
61   /** BIB and session DB */
62   nat64_db_t db;
63
64   /* values of various timeouts */
65   u32 udp_timeout;
66   u32 icmp_timeout;
67   u32 tcp_trans_timeout;
68   u32 tcp_est_timeout;
69   u32 tcp_incoming_syn_timeout;
70
71   u8 is_disabled;
72
73   snat_main_t *sm;
74 } nat64_main_t;
75
76 extern nat64_main_t nat64_main;
77 extern vlib_node_registration_t nat64_in2out_node;
78 extern vlib_node_registration_t nat64_out2in_node;
79
80 /**
81  * @brief Add/delete address to NAT64 pool.
82  *
83  * @param addr   IPv4 address.
84  * @param vrf_id VRF id of tenant, ~0 means independent of VRF.
85  * @param is_add 1 if add, 0 if delete.
86  *
87  * @returns 0 on success, non-zero value otherwise.
88  */
89 int nat64_add_del_pool_addr (ip4_address_t * addr, u32 vrf_id, u8 is_add);
90
91 /**
92  * @brief Call back function when walking addresses in NAT64 pool, non-zero
93  * return value stop walk.
94  */
95 typedef int (*nat64_pool_addr_walk_fn_t) (snat_address_t * addr, void *ctx);
96
97 /**
98  * @brief Walk NAT64 pool.
99  *
100  * @param fn The function to invoke on each entry visited.
101  * @param ctx A context passed in the visit function.
102  */
103 void nat64_pool_addr_walk (nat64_pool_addr_walk_fn_t fn, void *ctx);
104
105 /**
106  * @brief Enable/disable NAT64 feature on the interface.
107  *
108  * @param sw_if_index Index of the interface.
109  * @param is_inside   1 if inside, 0 if outside.
110  * @param is_add      1 if add, 0 if delete.
111  *
112  * @returns 0 on success, non-zero value otherwise.
113  */
114 int nat64_add_del_interface (u32 sw_if_index, u8 is_inside, u8 is_add);
115
116 /**
117  * @brief Call back function when walking interfaces with NAT64 feature,
118  * non-zero return value stop walk.
119  */
120 typedef int (*nat64_interface_walk_fn_t) (snat_interface_t * i, void *ctx);
121
122 /**
123  * @brief Walk NAT64 interfaces.
124  *
125  * @param fn The function to invoke on each entry visited.
126  * @param ctx A context passed in the visit function.
127  */
128 void nat64_interfaces_walk (nat64_interface_walk_fn_t fn, void *ctx);
129
130 /**
131  * @brief Initialize NAT64.
132  *
133  * @param vm vlib main.
134  *
135  * @return error code.
136  */
137 clib_error_t *nat64_init (vlib_main_t * vm);
138
139 /**
140  * @brief Add/delete static NAT64 BIB entry.
141  *
142  * @param in_addr  Inside IPv6 address.
143  * @param out_addr Outside IPv4 address.
144  * @param in_port  Inside port number.
145  * @param out_port Outside port number.
146  * @param proto    L4 protocol.
147  * @param vrf_id   VRF id of tenant.
148  * @param is_add   1 if add, 0 if delete.
149  *
150  * @returns 0 on success, non-zero value otherwise.
151  */
152 int nat64_add_del_static_bib_entry (ip6_address_t * in_addr,
153                                     ip4_address_t * out_addr, u16 in_port,
154                                     u16 out_port, u8 proto, u32 vrf_id,
155                                     u8 is_add);
156
157 /**
158  * @brief Alloce IPv4 address and port pair from NAT64 pool.
159  *
160  * @param fib_index FIB index of tenant.
161  * @param proto     L4 protocol.
162  * @param addr      Allocated IPv4 address.
163  * @param port      Allocated port number.
164  *
165  * @returns 0 on success, non-zero value otherwise.
166  */
167 int nat64_alloc_out_addr_and_port (u32 fib_index, snat_protocol_t proto,
168                                    ip4_address_t * addr, u16 * port);
169
170 /**
171  * @brief Free IPv4 address and port pair from NAT64 pool.
172  *
173  * @param addr  IPv4 address to free.
174  * @param port  Port number to free.
175  * @param proto L4 protocol.
176  *
177  * @returns 0 on success, non-zero value otherwise.
178  */
179 void nat64_free_out_addr_and_port (ip4_address_t * addr, u16 port,
180                                    snat_protocol_t proto);
181
182 /**
183  * @brief Set UDP session timeout.
184  *
185  * @param timeout Timeout value in seconds (if 0 reset to default value 300sec).
186  *
187  * @returns 0 on success, non-zero value otherwise.
188  */
189 int nat64_set_udp_timeout (u32 timeout);
190
191 /**
192  * @brief Get UDP session timeout.
193  *
194  * @returns UDP session timeout in seconds.
195  */
196 u32 nat64_get_udp_timeout (void);
197
198 /**
199  * @brief Set ICMP session timeout.
200  *
201  * @param timeout Timeout value in seconds (if 0 reset to default value 60sec).
202  *
203  * @returns 0 on success, non-zero value otherwise.
204  */
205 int nat64_set_icmp_timeout (u32 timeout);
206
207 /**
208  * @brief Get ICMP session timeout.
209  *
210  * @returns ICMP session timeout in seconds.
211  */
212 u32 nat64_get_icmp_timeout (void);
213
214 /**
215  * @brief Set TCP session timeouts.
216  *
217  * @param trans Transitory timeout in seconds (if 0 reset to default value 240sec).
218  * @param est Established timeout in seconds (if 0 reset to default value 7440sec).
219  * @param incoming_syn Incoming SYN timeout in seconds (if 0 reset to default value 6sec).
220  *
221  * @returns 0 on success, non-zero value otherwise.
222  */
223 int nat64_set_tcp_timeouts (u32 trans, u32 est, u32 incoming_syn);
224
225 /**
226  * @brief Get TCP transitory timeout.
227  *
228  * @returns TCP transitory timeout in seconds.
229  */
230 u32 nat64_get_tcp_trans_timeout (void);
231
232 /**
233  * @brief Get TCP established timeout.
234  *
235  * @returns TCP established timeout in seconds.
236  */
237 u32 nat64_get_tcp_est_timeout (void);
238
239 /**
240  * @brief Get TCP incoming SYN timeout.
241  *
242  * @returns TCP incoming SYN timeout in seconds.
243  */
244 u32 nat64_get_tcp_incoming_syn_timeout (void);
245
246 /**
247  * @brief Reset NAT64 session timeout.
248  *
249  * @param ste Session table entry.
250  * @param vm VLIB main.
251  **/
252 void nat64_session_reset_timeout (nat64_db_st_entry_t * ste,
253                                   vlib_main_t * vm);
254
255 /**
256  * @brief Set NAT64 TCP session state.
257  *
258  * @param ste Session table entry.
259  * @param tcp TCP header.
260  * @param is_ip6 1 if IPv6 packet, 0 if IPv4.
261  */
262 void nat64_tcp_session_set_state (nat64_db_st_entry_t * ste,
263                                   tcp_header_t * tcp, u8 is_ip6);
264
265 /**
266  * @brief Add/delete NAT64 prefix.
267  *
268  * @param prefix NAT64 prefix.
269  * @param plen Prefix length.
270  * @param vrf_id VRF id of tenant.
271  * @param is_add 1 if add, 0 if delete.
272  *
273  * @returns 0 on success, non-zero value otherwise.
274  */
275 int nat64_add_del_prefix (ip6_address_t * prefix, u8 plen, u32 vrf_id,
276                           u8 is_add);
277
278 /**
279  * @brief Call back function when walking addresses in NAT64 prefixes, non-zero
280  * return value stop walk.
281  */
282 typedef int (*nat64_prefix_walk_fn_t) (nat64_prefix_t * pref64, void *ctx);
283
284 /**
285  * @brief Walk NAT64 prefixes.
286  *
287  * @param fn The function to invoke on each entry visited.
288  * @param ctx A context passed in the visit function.
289  */
290 void nat64_prefix_walk (nat64_prefix_walk_fn_t fn, void *ctx);
291
292 /**
293  * Compose IPv4-embedded IPv6 addresses.
294  * @param ip6 IPv4-embedded IPv6 addresses.
295  * @param ip4 IPv4 address.
296  * @param fib_index Tenant FIB index.
297  */
298 void nat64_compose_ip6 (ip6_address_t * ip6, ip4_address_t * ip4,
299                         u32 fib_index);
300
301 /**
302  * Extract IPv4 address from the IPv4-embedded IPv6 addresses.
303  *
304  * @param ip6 IPv4-embedded IPv6 addresses.
305  * @param ip4 IPv4 address.
306  * @param fib_index Tenant FIB index.
307  */
308 void nat64_extract_ip4 (ip6_address_t * ip6, ip4_address_t * ip4,
309                         u32 fib_index);
310
311 #define u8_ptr_add(ptr, index) (((u8 *)ptr) + index)
312 #define u16_net_add(u, val) clib_host_to_net_u16(clib_net_to_host_u16(u) + (val))
313
314 #endif /* __included_nat64_h__ */
315
316 /*
317  * fd.io coding-style-patch-verification: ON
318  *
319  * Local Variables:
320  * eval: (c-set-style "gnu")
321  * End:
322  */