NAT44: add support for session timeout (VPP-1272)
[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 enum
43 {
44   NAT64_CLEANER_RESCHEDULE = 1,
45 } nat64_cleaner_process_event_e;
46
47 typedef struct
48 {
49   ip6_address_t prefix;
50   u8 plen;
51   u32 vrf_id;
52   u32 fib_index;
53 } nat64_prefix_t;
54
55 typedef struct
56 {
57   ip6_address_t in_addr;
58   u16 in_port;
59   ip4_address_t out_addr;
60   u16 out_port;
61   u32 fib_index;
62   u32 thread_index;
63   u8 proto;
64   u8 is_add;
65   u8 done;
66 } nat64_static_bib_to_update_t;
67
68 typedef struct
69 {
70   /** Interface pool */
71   snat_interface_t *interfaces;
72
73   /** Address pool vector */
74   snat_address_t *addr_pool;
75
76   /** sw_if_indices whose interface addresses should be auto-added */
77   u32 *auto_add_sw_if_indices;
78
79   /** Pref64 vector */
80   nat64_prefix_t *pref64;
81
82   /** BIB and session DB per thread */
83   nat64_db_t *db;
84
85   /** Worker handoff */
86   u32 fq_in2out_index;
87   u32 fq_out2in_index;
88
89   /** Pool of static BIB entries to be added/deleted in worker threads */
90   nat64_static_bib_to_update_t *static_bibs;
91
92   u32 error_node_index;
93
94   /** config parameters */
95   u32 bib_buckets;
96   u32 bib_memory_size;
97   u32 st_buckets;
98   u32 st_memory_size;
99
100   /** values of various timeouts */
101   u32 udp_timeout;
102   u32 icmp_timeout;
103   u32 tcp_trans_timeout;
104   u32 tcp_est_timeout;
105
106   /* Total count of interfaces enabled */
107   u32 total_enabled_count;
108   /* The process node which orcherstrates the cleanup */
109   u32 nat64_expire_walk_node_index;
110
111   ip4_main_t *ip4_main;
112   snat_main_t *sm;
113 } nat64_main_t;
114
115 extern nat64_main_t nat64_main;
116 extern vlib_node_registration_t nat64_in2out_node;
117 extern vlib_node_registration_t nat64_out2in_node;
118
119 /**
120  * @brief Add/delete address to NAT64 pool.
121  *
122  * @param addr   IPv4 address.
123  * @param vrf_id VRF id of tenant, ~0 means independent of VRF.
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_pool_addr (ip4_address_t * addr, u32 vrf_id, u8 is_add);
129
130 /**
131  * @brief Call back function when walking addresses in NAT64 pool, non-zero
132  * return value stop walk.
133  */
134 typedef int (*nat64_pool_addr_walk_fn_t) (snat_address_t * addr, void *ctx);
135
136 /**
137  * @brief Walk NAT64 pool.
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_pool_addr_walk (nat64_pool_addr_walk_fn_t fn, void *ctx);
143
144 /**
145  * @brief NAT64 pool address from specific (DHCP addressed) interface.
146  *
147  * @param sw_if_index Index of the interface.
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_interface_address (u32 sw_if_index, int is_add);
153
154 /**
155  * @brief Enable/disable NAT64 feature on the interface.
156  *
157  * @param sw_if_index Index of the interface.
158  * @param is_inside   1 if inside, 0 if outside.
159  * @param is_add      1 if add, 0 if delete.
160  *
161  * @returns 0 on success, non-zero value otherwise.
162  */
163 int nat64_add_del_interface (u32 sw_if_index, u8 is_inside, u8 is_add);
164
165 /**
166  * @brief Call back function when walking interfaces with NAT64 feature,
167  * non-zero return value stop walk.
168  */
169 typedef int (*nat64_interface_walk_fn_t) (snat_interface_t * i, void *ctx);
170
171 /**
172  * @brief Walk NAT64 interfaces.
173  *
174  * @param fn The function to invoke on each entry visited.
175  * @param ctx A context passed in the visit function.
176  */
177 void nat64_interfaces_walk (nat64_interface_walk_fn_t fn, void *ctx);
178
179 /**
180  * @brief Initialize NAT64.
181  *
182  * @param vm vlib main.
183  *
184  * @return error code.
185  */
186 clib_error_t *nat64_init (vlib_main_t * vm);
187
188 /**
189  * @brief Add/delete static NAT64 BIB entry.
190  *
191  * @param in_addr  Inside IPv6 address.
192  * @param out_addr Outside IPv4 address.
193  * @param in_port  Inside port number.
194  * @param out_port Outside port number.
195  * @param proto    L4 protocol.
196  * @param vrf_id   VRF id of tenant.
197  * @param is_add   1 if add, 0 if delete.
198  *
199  * @returns 0 on success, non-zero value otherwise.
200  */
201 int nat64_add_del_static_bib_entry (ip6_address_t * in_addr,
202                                     ip4_address_t * out_addr, u16 in_port,
203                                     u16 out_port, u8 proto, u32 vrf_id,
204                                     u8 is_add);
205
206 /**
207  * @brief Alloce IPv4 address and port pair from NAT64 pool.
208  *
209  * @param fib_index    FIB index of tenant.
210  * @param proto        L4 protocol.
211  * @param addr         Allocated IPv4 address.
212  * @param port         Allocated port number.
213  * @param thread_index Thread index.
214  *
215  * @returns 0 on success, non-zero value otherwise.
216  */
217 int nat64_alloc_out_addr_and_port (u32 fib_index, snat_protocol_t proto,
218                                    ip4_address_t * addr, u16 * port,
219                                    u32 thread_index);
220
221 /**
222  * @brief Set UDP session timeout.
223  *
224  * @param timeout Timeout value in seconds (if 0 reset to default value 300sec).
225  *
226  * @returns 0 on success, non-zero value otherwise.
227  */
228 int nat64_set_udp_timeout (u32 timeout);
229
230 /**
231  * @brief Get UDP session timeout.
232  *
233  * @returns UDP session timeout in seconds.
234  */
235 u32 nat64_get_udp_timeout (void);
236
237 /**
238  * @brief Set ICMP session timeout.
239  *
240  * @param timeout Timeout value in seconds (if 0 reset to default value 60sec).
241  *
242  * @returns 0 on success, non-zero value otherwise.
243  */
244 int nat64_set_icmp_timeout (u32 timeout);
245
246 /**
247  * @brief Get ICMP session timeout.
248  *
249  * @returns ICMP session timeout in seconds.
250  */
251 u32 nat64_get_icmp_timeout (void);
252
253 /**
254  * @brief Set TCP session timeouts.
255  *
256  * @param trans Transitory timeout in seconds (if 0 reset to default value 240sec).
257  * @param est Established timeout in seconds (if 0 reset to default value 7440sec).
258  *
259  * @returns 0 on success, non-zero value otherwise.
260  */
261 int nat64_set_tcp_timeouts (u32 trans, u32 est);
262
263 /**
264  * @brief Get TCP transitory timeout.
265  *
266  * @returns TCP transitory timeout in seconds.
267  */
268 u32 nat64_get_tcp_trans_timeout (void);
269
270 /**
271  * @brief Get TCP established timeout.
272  *
273  * @returns TCP established timeout in seconds.
274  */
275 u32 nat64_get_tcp_est_timeout (void);
276
277 /**
278  * @brief Reset NAT64 session timeout.
279  *
280  * @param ste Session table entry.
281  * @param vm VLIB main.
282  **/
283 void nat64_session_reset_timeout (nat64_db_st_entry_t * ste,
284                                   vlib_main_t * vm);
285
286 /**
287  * @brief Set NAT64 TCP session state.
288  *
289  * @param ste Session table entry.
290  * @param tcp TCP header.
291  * @param is_ip6 1 if IPv6 packet, 0 if IPv4.
292  */
293 void nat64_tcp_session_set_state (nat64_db_st_entry_t * ste,
294                                   tcp_header_t * tcp, u8 is_ip6);
295
296 /**
297  * @brief Add/delete NAT64 prefix.
298  *
299  * @param prefix NAT64 prefix.
300  * @param plen Prefix length.
301  * @param vrf_id VRF id of tenant.
302  * @param is_add 1 if add, 0 if delete.
303  *
304  * @returns 0 on success, non-zero value otherwise.
305  */
306 int nat64_add_del_prefix (ip6_address_t * prefix, u8 plen, u32 vrf_id,
307                           u8 is_add);
308
309 /**
310  * @brief Call back function when walking addresses in NAT64 prefixes, non-zero
311  * return value stop walk.
312  */
313 typedef int (*nat64_prefix_walk_fn_t) (nat64_prefix_t * pref64, void *ctx);
314
315 /**
316  * @brief Walk NAT64 prefixes.
317  *
318  * @param fn The function to invoke on each entry visited.
319  * @param ctx A context passed in the visit function.
320  */
321 void nat64_prefix_walk (nat64_prefix_walk_fn_t fn, void *ctx);
322
323 /**
324  * Compose IPv4-embedded IPv6 addresses.
325  * @param ip6 IPv4-embedded IPv6 addresses.
326  * @param ip4 IPv4 address.
327  * @param fib_index Tenant FIB index.
328  */
329 void nat64_compose_ip6 (ip6_address_t * ip6, ip4_address_t * ip4,
330                         u32 fib_index);
331
332 /**
333  * Extract IPv4 address from the IPv4-embedded IPv6 addresses.
334  *
335  * @param ip6 IPv4-embedded IPv6 addresses.
336  * @param ip4 IPv4 address.
337  * @param fib_index Tenant FIB index.
338  */
339 void nat64_extract_ip4 (ip6_address_t * ip6, ip4_address_t * ip4,
340                         u32 fib_index);
341
342 /**
343  * @brief Set NAT64 hash tables configuration.
344  *
345  * @param bib_buckets Number of BIB hash buckets.
346  * @param bib_memory_size Memory size of BIB hash.
347  * @param st_buckets Number of session table hash buckets.
348  * @param st_memory_size Memory size of session table hash.
349  */
350 void nat64_set_hash (u32 bib_buckets, u32 bib_memory_size, u32 st_buckets,
351                      u32 st_memory_size);
352
353 /**
354  * @brief Get worker thread index for NAT64 in2out.
355  *
356  * @param addr IPv6 src address.
357  *
358  * @returns worker thread index.
359  */
360 u32 nat64_get_worker_in2out (ip6_address_t * addr);
361
362 /**
363  * @brief Get worker thread index for NAT64 out2in.
364  *
365  * @param ip IPv4 header.
366  *
367  * @returns worker thread index.
368  */
369 u32 nat64_get_worker_out2in (ip4_header_t * ip);
370
371 #endif /* __included_nat64_h__ */
372
373 /*
374  * fd.io coding-style-patch-verification: ON
375  *
376  * Local Variables:
377  * eval: (c-set-style "gnu")
378  * End:
379  */