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