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