NAT64: change not supported multi threading behaviour
[vpp.git] / src / plugins / snat / 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 <snat/snat.h>
23 #include <snat/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   /** Interface pool */
45   snat_interface_t *interfaces;
46
47   /** Address pool vector */
48   snat_address_t *addr_pool;
49
50   /** BIB and session DB **/
51   nat64_db_t db;
52
53   /* values of various timeouts */
54   u32 udp_timeout;
55   u32 icmp_timeout;
56   u32 tcp_trans_timeout;
57   u32 tcp_est_timeout;
58   u32 tcp_incoming_syn_timeout;
59
60   u8 is_disabled;
61
62   snat_main_t *sm;
63 } nat64_main_t;
64
65 extern nat64_main_t nat64_main;
66 extern vlib_node_registration_t nat64_in2out_node;
67 extern vlib_node_registration_t nat64_out2in_node;
68
69 /**
70  * @brief Add/delete address to NAT64 pool.
71  *
72  * @param addr   IPv4 address.
73  * @param vrf_id VRF id of tenant, ~0 means independent of VRF.
74  * @param is_add 1 if add, 0 if delete.
75  *
76  * @returns 0 on success, non-zero value otherwise.
77  */
78 int nat64_add_del_pool_addr (ip4_address_t * addr, u32 vrf_id, u8 is_add);
79
80 /**
81  * @brief Call back function when walking addresses in NAT64 pool, non-zero
82  * return value stop walk.
83  */
84 typedef int (*nat64_pool_addr_walk_fn_t) (snat_address_t * addr, void *ctx);
85
86 /**
87  * @brief Walk NAT64 pool.
88  *
89  * @param fn The function to invoke on each entry visited.
90  * @param ctx A context passed in the visit function.
91  */
92 void nat64_pool_addr_walk (nat64_pool_addr_walk_fn_t fn, void *ctx);
93
94 /**
95  * @brief Enable/disable NAT64 feature on the interface.
96  *
97  * @param sw_if_index Index of the interface.
98  * @param is_inside   1 if inside, 0 if outside.
99  * @param is_add      1 if add, 0 if delete.
100  *
101  * @returns 0 on success, non-zero value otherwise.
102  */
103 int nat64_add_del_interface (u32 sw_if_index, u8 is_inside, u8 is_add);
104
105 /**
106  * @brief Call back function when walking interfaces with NAT64 feature,
107  * non-zero return value stop walk.
108  */
109 typedef int (*nat64_interface_walk_fn_t) (snat_interface_t * i, void *ctx);
110
111 /**
112  * @brief Walk NAT64 interfaces.
113  *
114  * @param fn The function to invoke on each entry visited.
115  * @param ctx A context passed in the visit function.
116  */
117 void nat64_interfaces_walk (nat64_interface_walk_fn_t fn, void *ctx);
118
119 /**
120  * @brief Initialize NAT64.
121  *
122  * @param vm vlib main.
123  *
124  * @return error code.
125  */
126 clib_error_t *nat64_init (vlib_main_t * vm);
127
128 /**
129  * @brief Add/delete static NAT64 BIB entry.
130  *
131  * @param in_addr  Inside IPv6 address.
132  * @param out_addr Outside IPv4 address.
133  * @param in_port  Inside port number.
134  * @param out_port Outside port number.
135  * @param proto    L4 protocol.
136  * @param vrf_id   VRF id of tenant.
137  * @param is_add   1 if add, 0 if delete.
138  *
139  * @returns 0 on success, non-zero value otherwise.
140  */
141 int nat64_add_del_static_bib_entry (ip6_address_t * in_addr,
142                                     ip4_address_t * out_addr, u16 in_port,
143                                     u16 out_port, u8 proto, u32 vrf_id,
144                                     u8 is_add);
145
146 /**
147  * @brief Alloce IPv4 address and port pair from NAT64 pool.
148  *
149  * @param fib_index FIB index of tenant.
150  * @param proto     L4 protocol.
151  * @param addr      Allocated IPv4 address.
152  * @param port      Allocated port number.
153  *
154  * @returns 0 on success, non-zero value otherwise.
155  */
156 int nat64_alloc_out_addr_and_port (u32 fib_index, snat_protocol_t proto,
157                                    ip4_address_t * addr, u16 * port);
158
159 /**
160  * @brief Free IPv4 address and port pair from NAT64 pool.
161  *
162  * @param addr  IPv4 address to free.
163  * @param port  Port number to free.
164  * @param proto L4 protocol.
165  *
166  * @returns 0 on success, non-zero value otherwise.
167  */
168 void nat64_free_out_addr_and_port (ip4_address_t * addr, u16 port,
169                                    snat_protocol_t proto);
170
171 /**
172  * @brief Set UDP session timeout.
173  *
174  * @param timeout Timeout value in seconds (if 0 reset to default value 300sec).
175  *
176  * @returns 0 on success, non-zero value otherwise.
177  */
178 int nat64_set_udp_timeout (u32 timeout);
179
180 /**
181  * @brief Get UDP session timeout.
182  *
183  * @returns UDP session timeout in seconds.
184  */
185 u32 nat64_get_udp_timeout (void);
186
187 /**
188  * @brief Set ICMP session timeout.
189  *
190  * @param timeout Timeout value in seconds (if 0 reset to default value 60sec).
191  *
192  * @returns 0 on success, non-zero value otherwise.
193  */
194 int nat64_set_icmp_timeout (u32 timeout);
195
196 /**
197  * @brief Get ICMP session timeout.
198  *
199  * @returns ICMP session timeout in seconds.
200  */
201 u32 nat64_get_icmp_timeout (void);
202
203 /**
204  * @brief Set TCP session timeouts.
205  *
206  * @param trans Transitory timeout in seconds (if 0 reset to default value 240sec).
207  * @param est Established timeout in seconds (if 0 reset to default value 7440sec).
208  * @param incoming_syn Incoming SYN timeout in seconds (if 0 reset to default value 6sec).
209  *
210  * @returns 0 on success, non-zero value otherwise.
211  */
212 int nat64_set_tcp_timeouts (u32 trans, u32 est, u32 incoming_syn);
213
214 /**
215  * @brief Get TCP transitory timeout.
216  *
217  * @returns TCP transitory timeout in seconds.
218  */
219 u32 nat64_get_tcp_trans_timeout (void);
220
221 /**
222  * @brief Get TCP established timeout.
223  *
224  * @returns TCP established timeout in seconds.
225  */
226 u32 nat64_get_tcp_est_timeout (void);
227
228 /**
229  * @brief Get TCP incoming SYN timeout.
230  *
231  * @returns TCP incoming SYN timeout in seconds.
232  */
233 u32 nat64_get_tcp_incoming_syn_timeout (void);
234
235 /**
236  * @brief Reset NAT64 session timeout.
237  *
238  * @param ste Session table entry.
239  * @param vm VLIB main.
240  **/
241 void nat64_session_reset_timeout (nat64_db_st_entry_t * ste,
242                                   vlib_main_t * vm);
243
244 /**
245  * @brief Set NAT64 TCP session state.
246  *
247  * @param ste Session table entry.
248  * @param tcp TCP header.
249  * @param is_ip6 1 if IPv6 packet, 0 if IPv4.
250  */
251 void nat64_tcp_session_set_state (nat64_db_st_entry_t * ste,
252                                   tcp_header_t * tcp, u8 is_ip6);
253
254 #define u8_ptr_add(ptr, index) (((u8 *)ptr) + index)
255 #define u16_net_add(u, val) clib_host_to_net_u16(clib_net_to_host_u16(u) + (val))
256
257 #endif /* __included_nat64_h__ */
258
259 /*
260  * fd.io coding-style-patch-verification: ON
261  *
262  * Local Variables:
263  * eval: (c-set-style "gnu")
264  * End:
265  */