FD.io VPP  v20.09-64-g4f7b92f0a
Vector Packet Processing
nat64.h
Go to the documentation of this file.
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,
39 #undef _
41 
42 typedef enum
43 {
46 
47 typedef struct
48 {
49  ip6_address_t prefix;
54 
55 typedef struct
56 {
57  ip6_address_t in_addr;
67 
68 typedef struct
69 {
70  /** Interface pool */
72 
73  /** Address pool vector */
75 
76  /** sw_if_indices whose interface addresses should be auto-added */
78 
79  /** Pref64 vector */
81 
82  /** BIB and session DB per thread */
84 
85  /** Worker handoff */
88 
89  /** Pool of static BIB entries to be added/deleted in worker threads */
91 
92  /** config parameters */
97 
98  /** values of various timeouts */
103 
104  /* Total count of interfaces enabled */
106  /* The process node which orcherstrates the cleanup */
108 
109  /* counters/gauges */
112 
113  /** node index **/
115 
118 
120 
123 
124 #define _(x) vlib_simple_counter_main_t x;
125  struct
126  {
127  struct
128  {
130  } in2out;
131 
132  struct
133  {
135  } out2in;
136  } counters;
137 #undef _
138 
139 } nat64_main_t;
140 
141 extern nat64_main_t nat64_main;
144 
145 /**
146  * @brief Add/delete address to NAT64 pool.
147  *
148  * @param thread_index Thread index used by ipfix nat logging (not address per thread).
149  * @param addr IPv4 address.
150  * @param vrf_id VRF id of tenant, ~0 means independent of VRF.
151  * @param is_add 1 if add, 0 if delete.
152  *
153  * @returns 0 on success, non-zero value otherwise.
154  */
155 int nat64_add_del_pool_addr (u32 thread_index,
156  ip4_address_t * addr, u32 vrf_id, u8 is_add);
157 
158 /**
159  * @brief Call back function when walking addresses in NAT64 pool, non-zero
160  * return value stop walk.
161  */
163 
164 /**
165  * @brief Walk NAT64 pool.
166  *
167  * @param fn The function to invoke on each entry visited.
168  * @param ctx A context passed in the visit function.
169  */
171 
172 /**
173  * @brief NAT64 pool address from specific (DHCP addressed) interface.
174  *
175  * @param sw_if_index Index of the interface.
176  * @param is_add 1 if add, 0 if delete.
177  *
178  * @returns 0 on success, non-zero value otherwise.
179  */
181 
182 /**
183  * @brief Enable/disable NAT64 feature on the interface.
184  *
185  * @param sw_if_index Index of the interface.
186  * @param is_inside 1 if inside, 0 if outside.
187  * @param is_add 1 if add, 0 if delete.
188  *
189  * @returns 0 on success, non-zero value otherwise.
190  */
191 int nat64_add_del_interface (u32 sw_if_index, u8 is_inside, u8 is_add);
192 
193 /**
194  * @brief Call back function when walking interfaces with NAT64 feature,
195  * non-zero return value stop walk.
196  */
198 
199 /**
200  * @brief Walk NAT64 interfaces.
201  *
202  * @param fn The function to invoke on each entry visited.
203  * @param ctx A context passed in the visit function.
204  */
206 
207 /**
208  * @brief Initialize NAT64.
209  *
210  * @param vm vlib main.
211  *
212  * @return error code.
213  */
215 
216 /**
217  * @brief Add/delete static NAT64 BIB entry.
218  *
219  * @param in_addr Inside IPv6 address.
220  * @param out_addr Outside IPv4 address.
221  * @param in_port Inside port number.
222  * @param out_port Outside port number.
223  * @param proto L4 protocol.
224  * @param vrf_id VRF id of tenant.
225  * @param is_add 1 if add, 0 if delete.
226  *
227  * @returns 0 on success, non-zero value otherwise.
228  */
229 int nat64_add_del_static_bib_entry (ip6_address_t * in_addr,
230  ip4_address_t * out_addr, u16 in_port,
231  u16 out_port, u8 proto, u32 vrf_id,
232  u8 is_add);
233 
234 /**
235  * @brief Alloce IPv4 address and port pair from NAT64 pool.
236  *
237  * @param fib_index FIB index of tenant.
238  * @param proto L4 protocol.
239  * @param addr Allocated IPv4 address.
240  * @param port Allocated port number.
241  * @param thread_index Thread index.
242  *
243  * @returns 0 on success, non-zero value otherwise.
244  */
246  ip4_address_t * addr, u16 * port,
247  u32 thread_index);
248 
249 /**
250  * @brief Set UDP session timeout.
251  *
252  * @param timeout Timeout value in seconds (if 0 reset to default value 300sec).
253  *
254  * @returns 0 on success, non-zero value otherwise.
255  */
256 int nat64_set_udp_timeout (u32 timeout);
257 
258 /**
259  * @brief Get UDP session timeout.
260  *
261  * @returns UDP session timeout in seconds.
262  */
264 
265 /**
266  * @brief Set ICMP session timeout.
267  *
268  * @param timeout Timeout value in seconds (if 0 reset to default value 60sec).
269  *
270  * @returns 0 on success, non-zero value otherwise.
271  */
272 int nat64_set_icmp_timeout (u32 timeout);
273 
274 /**
275  * @brief Get ICMP session timeout.
276  *
277  * @returns ICMP session timeout in seconds.
278  */
280 
281 /**
282  * @brief Set TCP session timeouts.
283  *
284  * @param trans Transitory timeout in seconds (if 0 reset to default value 240sec).
285  * @param est Established timeout in seconds (if 0 reset to default value 7440sec).
286  *
287  * @returns 0 on success, non-zero value otherwise.
288  */
289 int nat64_set_tcp_timeouts (u32 trans, u32 est);
290 
291 /**
292  * @brief Get TCP transitory timeout.
293  *
294  * @returns TCP transitory timeout in seconds.
295  */
297 
298 /**
299  * @brief Get TCP established timeout.
300  *
301  * @returns TCP established timeout in seconds.
302  */
304 
305 /**
306  * @brief Reset NAT64 session timeout.
307  *
308  * @param ste Session table entry.
309  * @param vm VLIB main.
310  **/
311 void nat64_session_reset_timeout (nat64_db_st_entry_t * ste,
312  vlib_main_t * vm);
313 
314 /**
315  * @brief Set NAT64 TCP session state.
316  *
317  * @param ste Session table entry.
318  * @param tcp TCP header.
319  * @param is_ip6 1 if IPv6 packet, 0 if IPv4.
320  */
321 void nat64_tcp_session_set_state (nat64_db_st_entry_t * ste,
322  tcp_header_t * tcp, u8 is_ip6);
323 
324 /**
325  * @brief Add/delete NAT64 prefix.
326  *
327  * @param prefix NAT64 prefix.
328  * @param plen Prefix length.
329  * @param vrf_id VRF id of tenant.
330  * @param is_add 1 if add, 0 if delete.
331  *
332  * @returns 0 on success, non-zero value otherwise.
333  */
334 int nat64_add_del_prefix (ip6_address_t * prefix, u8 plen, u32 vrf_id,
335  u8 is_add);
336 
337 /**
338  * @brief Call back function when walking addresses in NAT64 prefixes, non-zero
339  * return value stop walk.
340  */
341 typedef int (*nat64_prefix_walk_fn_t) (nat64_prefix_t * pref64, void *ctx);
342 
343 /**
344  * @brief Walk NAT64 prefixes.
345  *
346  * @param fn The function to invoke on each entry visited.
347  * @param ctx A context passed in the visit function.
348  */
350 
351 /**
352  * Compose IPv4-embedded IPv6 addresses.
353  * @param ip6 IPv4-embedded IPv6 addresses.
354  * @param ip4 IPv4 address.
355  * @param fib_index Tenant FIB index.
356  */
357 void nat64_compose_ip6 (ip6_address_t * ip6, ip4_address_t * ip4,
358  u32 fib_index);
359 
360 /**
361  * Extract IPv4 address from the IPv4-embedded IPv6 addresses.
362  *
363  * @param ip6 IPv4-embedded IPv6 addresses.
364  * @param ip4 IPv4 address.
365  * @param fib_index Tenant FIB index.
366  */
367 void nat64_extract_ip4 (ip6_address_t * ip6, ip4_address_t * ip4,
368  u32 fib_index);
369 
370 /**
371  * @brief Set NAT64 hash tables configuration.
372  *
373  * @param bib_buckets Number of BIB hash buckets.
374  * @param bib_memory_size Memory size of BIB hash.
375  * @param st_buckets Number of session table hash buckets.
376  * @param st_memory_size Memory size of session table hash.
377  */
378 void nat64_set_hash (u32 bib_buckets, uword bib_memory_size, u32 st_buckets,
379  uword st_memory_size);
380 
381 /**
382  * @brief Get worker thread index for NAT64 in2out.
383  *
384  * @param addr IPv6 src address.
385  *
386  * @returns worker thread index.
387  */
388 u32 nat64_get_worker_in2out (ip6_address_t * addr);
389 
390 /**
391  * @brief Get worker thread index for NAT64 out2in.
392  *
393  * @param ip IPv4 header.
394  *
395  * @returns worker thread index.
396  */
398 
399 #endif /* __included_nat64_h__ */
400 
401 /*
402  * fd.io coding-style-patch-verification: ON
403  *
404  * Local Variables:
405  * eval: (c-set-style "gnu")
406  * End:
407  */
nat64_db_t * db
BIB and session DB per thread.
Definition: nat64.h:83
u32 icmp_timeout
Definition: nat64.h:100
u32 out2in_node_index
Definition: nat64.h:119
nat64_main_t nat64_main
Definition: nat64.c:29
foreach_nat_counter
Definition: nat64.h:129
ip6_address_t prefix
Definition: nat64.h:49
snat_address_t * addr_pool
Address pool vector.
Definition: nat64.h:74
u32 st_buckets
Definition: nat64.h:95
u32 nat64_expire_walk_node_index
Definition: nat64.h:107
void nat64_set_hash(u32 bib_buckets, uword bib_memory_size, u32 st_buckets, uword st_memory_size)
Set NAT64 hash tables configuration.
Definition: nat64.c:266
int nat64_add_interface_address(u32 sw_if_index, int is_add)
NAT64 pool address from specific (DHCP addressed) interface.
Definition: nat64.c:379
int nat64_set_tcp_timeouts(u32 trans, u32 est)
Set TCP session timeouts.
Definition: nat64.c:846
vlib_main_t * vm
Definition: in2out_ed.c:1582
u32 tcp_trans_timeout
Definition: nat64.h:101
nat_protocol_t
Definition: lib.h:41
u32 in2out_node_index
Definition: nat64.h:116
vl_api_prefix_t prefix
Definition: ip.api:144
struct _tcp_header tcp_header_t
vhost_vring_addr_t addr
Definition: vhost_user.h:111
u32 * auto_add_sw_if_indices
sw_if_indices whose interface addresses should be auto-added
Definition: nat64.h:77
unsigned char u8
Definition: types.h:56
int nat64_alloc_out_addr_and_port(u32 fib_index, nat_protocol_t proto, ip4_address_t *addr, u16 *port, u32 thread_index)
Alloce IPv4 address and port pair from NAT64 pool.
Definition: nat64.c:547
int nat64_add_del_interface(u32 sw_if_index, u8 is_inside, u8 is_add)
Enable/disable NAT64 feature on the interface.
Definition: nat64.c:433
#define foreach_nat64_tcp_ses_state
Definition: nat64.h:25
vl_api_ip6_address_t ip6
Definition: one.api:424
u32 total_enabled_count
Definition: nat64.h:105
u32 tcp_est_timeout
Definition: nat64.h:102
int nat64_add_del_static_bib_entry(ip6_address_t *in_addr, ip4_address_t *out_addr, u16 in_port, u16 out_port, u8 proto, u32 vrf_id, u8 is_add)
Add/delete static NAT64 BIB entry.
Definition: nat64.c:670
snat_main_t * sm
Definition: nat64.h:122
unsigned int u32
Definition: types.h:88
vlib_node_registration_t nat64_in2out_node
(constructor) VLIB_REGISTER_NODE (nat64_in2out_node)
A collection of simple counters.
Definition: counter.h:57
snat_interface_t * interfaces
Interface pool.
Definition: nat64.h:71
bool is_ip6
Definition: ip.api:43
u32 in2out_slowpath_node_index
Definition: nat64.h:117
u32 fib_index
Definition: nat64.h:52
u32 udp_timeout
values of various timeouts
Definition: nat64.h:99
u32 nat64_get_icmp_timeout(void)
Get ICMP session timeout.
Definition: nat64.c:838
ip4_address_t out_addr
Definition: nat64.h:59
void nat64_tcp_session_set_state(nat64_db_st_entry_t *ste, tcp_header_t *tcp, u8 is_ip6)
Set NAT64 TCP session state.
Definition: nat64.c:919
nat64_prefix_t * pref64
Pref64 vector.
Definition: nat64.h:80
ip6_address_t in_addr
Definition: nat64.h:57
u32 nat64_get_worker_in2out(ip6_address_t *addr)
Get worker thread index for NAT64 in2out.
Definition: nat64.c:105
vl_api_ip_proto_t proto
Definition: acl_types.api:50
long ctx[MAX_CONNS]
Definition: main.c:144
unsigned short u16
Definition: types.h:57
int nat64_add_del_prefix(ip6_address_t *prefix, u8 plen, u32 vrf_id, u8 is_add)
Add/delete NAT64 prefix.
Definition: nat64.c:986
int nat64_set_udp_timeout(u32 timeout)
Set UDP session timeout.
Definition: nat64.c:804
uword st_memory_size
Definition: nat64.h:96
vl_api_ip4_address_t ip4
Definition: one.api:376
nat64_tcp_ses_state_t
Definition: nat64.h:35
void nat64_pool_addr_walk(nat64_pool_addr_walk_fn_t fn, void *ctx)
Walk NAT64 pool.
Definition: nat64.c:364
vlib_simple_counter_main_t total_sessions
Definition: nat64.h:111
vlib_node_registration_t nat64_out2in_node
(constructor) VLIB_REGISTER_NODE (nat64_out2in_node)
Definition: nat64_out2in.c:648
u32 nat64_get_tcp_trans_timeout(void)
Get TCP transitory timeout.
Definition: nat64.c:864
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:317
u32 nat64_get_udp_timeout(void)
Get UDP session timeout.
Definition: nat64.c:817
u32 nat64_get_tcp_est_timeout(void)
Get TCP established timeout.
Definition: nat64.c:872
u32 bib_buckets
config parameters
Definition: nat64.h:93
void nat64_interfaces_walk(nat64_interface_walk_fn_t fn, void *ctx)
Walk NAT64 interfaces.
Definition: nat64.c:532
clib_error_t * nat64_init(vlib_main_t *vm)
Initialize NAT64.
Definition: nat64.c:201
u32 fq_out2in_index
Definition: nat64.h:87
#define foreach_nat_counter
Definition: nat.h:487
u32 vrf_id
Definition: nat64.h:51
u32 fq_in2out_index
Worker handoff.
Definition: nat64.h:86
int nat64_add_del_pool_addr(u32 thread_index, ip4_address_t *addr, u32 vrf_id, u8 is_add)
Add/delete address to NAT64 pool.
Definition: nat64.c:288
uword bib_memory_size
Definition: nat64.h:94
nat64_static_bib_to_update_t * static_bibs
Pool of static BIB entries to be added/deleted in worker threads.
Definition: nat64.h:90
IPv4 main type.
Definition: ip4.h:106
u32 nat64_get_worker_out2in(vlib_buffer_t *b, ip4_header_t *ip)
Get worker thread index for NAT64 out2in.
Definition: nat64.c:128
int nat64_set_icmp_timeout(u32 timeout)
Set ICMP session timeout.
Definition: nat64.c:825
nat64_cleaner_process_event_e
Definition: nat64.h:42
struct _vlib_node_registration vlib_node_registration_t
ip4_main_t * ip4_main
Definition: nat64.h:121
vl_api_address_t ip
Definition: l2.api:501
VLIB buffer representation.
Definition: buffer.h:102
u64 uword
Definition: types.h:112
u32 error_node_index
node index
Definition: nat64.h:114
void nat64_session_reset_timeout(nat64_db_st_entry_t *ste, vlib_main_t *vm)
Reset NAT64 session timeout.
Definition: nat64.c:880
u16 port
Definition: lb_types.api:72
NAT64 DB.
void nat64_extract_ip4(ip6_address_t *ip6, ip4_address_t *ip4, u32 fib_index)
Extract IPv4 address from the IPv4-embedded IPv6 addresses.
Definition: nat64.c:1119
void nat64_prefix_walk(nat64_prefix_walk_fn_t fn, void *ctx)
Walk NAT64 prefixes.
Definition: nat64.c:1034
u32 vrf_id
Definition: nat.api:944
void nat64_compose_ip6(ip6_address_t *ip6, ip4_address_t *ip4, u32 fib_index)
Compose IPv4-embedded IPv6 addresses.
Definition: nat64.c:1049
int(* nat64_prefix_walk_fn_t)(nat64_prefix_t *pref64, void *ctx)
Call back function when walking addresses in NAT64 prefixes, non-zero return value stop walk...
Definition: nat64.h:341
vl_api_interface_index_t sw_if_index
Definition: wireguard.api:33
int(* nat64_interface_walk_fn_t)(snat_interface_t *i, void *ctx)
Call back function when walking interfaces with NAT64 feature, non-zero return value stop walk...
Definition: nat64.h:197
vlib_simple_counter_main_t total_bibs
Definition: nat64.h:110
int(* nat64_pool_addr_walk_fn_t)(snat_address_t *addr, void *ctx)
Call back function when walking addresses in NAT64 pool, non-zero return value stop walk...
Definition: nat64.h:162