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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
15 #ifndef __included_nat64_h__
16 #define __included_nat64_h__
18 #include <vnet/vnet.h>
19 #include <vnet/ip/ip.h>
20 #include <vnet/ethernet/ethernet.h>
21 #include <vnet/ip/icmp46_packet.h>
22 #include <vnet/api_errno.h>
23 #include <vnet/fib/fib_source.h>
24 #include <vppinfra/dlist.h>
25 #include <vppinfra/error.h>
26 #include <vlibapi/api.h>
28 #include <vnet/fib/fib_table.h>
29 #include <vnet/fib/ip4_fib.h>
30 #include <vnet/ip/reass/ip4_sv_reass.h>
32 #include <nat/lib/lib.h>
33 #include <nat/lib/inlines.h>
34 #include <nat/lib/nat_inlines.h>
36 #include <nat/nat64/nat64_db.h>
46 u16 src_port, dst_port;
49 #define foreach_nat64_tcp_ses_state \
50 _(0, CLOSED, "closed") \
51 _(1, V4_INIT, "v4-init") \
52 _(2, V6_INIT, "v6-init") \
53 _(3, ESTABLISHED, "established") \
54 _(4, V4_FIN_RCV, "v4-fin-rcv") \
55 _(5, V6_FIN_RCV, "v6-fin-rcv") \
56 _(6, V6_FIN_V4_FIN_RCV, "v6-fin-v4-fin-rcv") \
61 #define _(v, N, s) NAT64_TCP_STATE_##N = v,
62 foreach_nat64_tcp_ses_state
64 } nat64_tcp_ses_state_t;
68 NAT64_CLEANER_RESCHEDULE = 1,
69 } nat64_cleaner_process_event_e;
81 ip6_address_t in_addr;
83 ip4_address_t out_addr;
90 } nat64_static_bib_to_update_t;
97 #define _(N, i, n, s) \
98 u16 busy_##n##_ports; \
99 u16 * busy_##n##_ports_per_thread; \
100 u32 busy_##n##_port_refcounts[65535];
116 nat64_config_t config;
118 /* API message ID base */
122 vlib_log_class_t log_class;
124 /** Interface pool */
125 nat64_interface_t *interfaces;
127 /** Address pool vector */
128 nat64_address_t *addr_pool;
130 /** sw_if_indices whose interface addresses should be auto-added */
131 u32 *auto_add_sw_if_indices;
134 nat64_prefix_t *pref64;
136 /** BIB and session DB per thread */
139 /** Worker handoff */
143 /** Pool of static BIB entries to be added/deleted in worker threads */
144 nat64_static_bib_to_update_t *static_bibs;
146 /** config parameters */
148 uword bib_memory_size;
150 uword st_memory_size;
152 /** values of various timeouts */
155 u32 tcp_trans_timeout;
158 /* Total count of interfaces enabled */
159 u32 total_enabled_count;
161 /* Expire walk process node index */
162 u32 expire_walk_node_index;
164 /* Expire worker walk process node index */
165 u32 expire_worker_walk_node_index;
167 /* counters/gauges */
168 vlib_simple_counter_main_t total_bibs;
169 vlib_simple_counter_main_t total_sessions;
172 u32 error_node_index;
174 u32 in2out_node_index;
175 u32 in2out_slowpath_node_index;
177 u32 out2in_node_index;
179 #define _(x) vlib_simple_counter_main_t x;
195 ip4_main_t *ip4_main;
198 vnet_main_t *vnet_main;
200 /* Randomize port allocation order */
203 /* TCP MSS clamping */
206 fib_source_t fib_src_hi;
207 fib_source_t fib_src_low;
209 /* Thread settings */
211 u32 first_worker_index;
217 extern nat64_main_t nat64_main;
218 extern vlib_node_registration_t nat64_in2out_node;
219 extern vlib_node_registration_t nat64_out2in_node;
222 * @brief Add/delete address to NAT64 pool.
224 * @param thread_index Thread index used by ipfix nat logging (not address per thread).
225 * @param addr IPv4 address.
226 * @param vrf_id VRF id of tenant, ~0 means independent of VRF.
227 * @param is_add 1 if add, 0 if delete.
229 * @returns 0 on success, non-zero value otherwise.
231 int nat64_add_del_pool_addr (u32 thread_index,
232 ip4_address_t * addr, u32 vrf_id, u8 is_add);
235 * @brief Call back function when walking addresses in NAT64 pool, non-zero
236 * return value stop walk.
238 typedef int (*nat64_pool_addr_walk_fn_t) (nat64_address_t * addr, void *ctx);
241 * @brief Walk NAT64 pool.
243 * @param fn The function to invoke on each entry visited.
244 * @param ctx A context passed in the visit function.
246 void nat64_pool_addr_walk (nat64_pool_addr_walk_fn_t fn, void *ctx);
249 * @brief NAT64 pool address from specific (DHCP addressed) interface.
251 * @param sw_if_index Index of the interface.
252 * @param is_add 1 if add, 0 if delete.
254 * @returns 0 on success, non-zero value otherwise.
256 int nat64_add_interface_address (u32 sw_if_index, int is_add);
259 * @brief Enable/disable NAT64 feature on the interface.
261 * @param sw_if_index Index of the interface.
262 * @param is_inside 1 if inside, 0 if outside.
263 * @param is_add 1 if add, 0 if delete.
265 * @returns 0 on success, non-zero value otherwise.
267 int nat64_interface_add_del (u32 sw_if_index, u8 is_inside, u8 is_add);
270 * @brief Call back function when walking interfaces with NAT64 feature,
271 * non-zero return value stop walk.
273 typedef int (*nat64_interface_walk_fn_t) (nat64_interface_t * i, void *ctx);
276 * @brief Walk NAT64 interfaces.
278 * @param fn The function to invoke on each entry visited.
279 * @param ctx A context passed in the visit function.
281 void nat64_interfaces_walk (nat64_interface_walk_fn_t fn, void *ctx);
284 * @brief Initialize NAT64.
286 * @param vm vlib main.
288 * @return error code.
290 clib_error_t *nat64_init (vlib_main_t * vm);
293 * @brief Add/delete static NAT64 BIB entry.
295 * @param in_addr Inside IPv6 address.
296 * @param out_addr Outside IPv4 address.
297 * @param in_port Inside port number.
298 * @param out_port Outside port number.
299 * @param proto L4 protocol.
300 * @param vrf_id VRF id of tenant.
301 * @param is_add 1 if add, 0 if delete.
303 * @returns 0 on success, non-zero value otherwise.
305 int nat64_add_del_static_bib_entry (ip6_address_t * in_addr,
306 ip4_address_t * out_addr, u16 in_port,
307 u16 out_port, u8 proto, u32 vrf_id,
311 * @brief Alloce IPv4 address and port pair from NAT64 pool.
313 * @param fib_index FIB index of tenant.
314 * @param proto L4 protocol.
315 * @param addr Allocated IPv4 address.
316 * @param port Allocated port number.
317 * @param thread_index Thread index.
319 * @returns 0 on success, non-zero value otherwise.
321 int nat64_alloc_out_addr_and_port (u32 fib_index, nat_protocol_t proto,
322 ip4_address_t * addr, u16 * port,
326 * @brief Set UDP session timeout.
328 * @param timeout Timeout value in seconds (if 0 reset to default value 300sec).
330 * @returns 0 on success, non-zero value otherwise.
332 int nat64_set_udp_timeout (u32 timeout);
335 * @brief Get UDP session timeout.
337 * @returns UDP session timeout in seconds.
339 u32 nat64_get_udp_timeout (void);
342 * @brief Set ICMP session timeout.
344 * @param timeout Timeout value in seconds (if 0 reset to default value 60sec).
346 * @returns 0 on success, non-zero value otherwise.
348 int nat64_set_icmp_timeout (u32 timeout);
351 * @brief Get ICMP session timeout.
353 * @returns ICMP session timeout in seconds.
355 u32 nat64_get_icmp_timeout (void);
358 * @brief Set TCP session timeouts.
360 * @param trans Transitory timeout in seconds (if 0 reset to default value 240sec).
361 * @param est Established timeout in seconds (if 0 reset to default value 7440sec).
363 * @returns 0 on success, non-zero value otherwise.
365 int nat64_set_tcp_timeouts (u32 trans, u32 est);
368 * @brief Get TCP transitory timeout.
370 * @returns TCP transitory timeout in seconds.
372 u32 nat64_get_tcp_trans_timeout (void);
375 * @brief Get TCP established timeout.
377 * @returns TCP established timeout in seconds.
379 u32 nat64_get_tcp_est_timeout (void);
382 * @brief Reset NAT64 session timeout.
384 * @param ste Session table entry.
385 * @param vm VLIB main.
387 void nat64_session_reset_timeout (nat64_db_st_entry_t * ste,
391 * @brief Set NAT64 TCP session state.
393 * @param ste Session table entry.
394 * @param tcp TCP header.
395 * @param is_ip6 1 if IPv6 packet, 0 if IPv4.
397 void nat64_tcp_session_set_state (nat64_db_st_entry_t * ste,
398 tcp_header_t * tcp, u8 is_ip6);
401 * @brief Add/delete NAT64 prefix.
403 * @param prefix NAT64 prefix.
404 * @param plen Prefix length.
405 * @param vrf_id VRF id of tenant.
406 * @param is_add 1 if add, 0 if delete.
408 * @returns 0 on success, non-zero value otherwise.
410 int nat64_add_del_prefix (ip6_address_t * prefix, u8 plen, u32 vrf_id,
414 * @brief Call back function when walking addresses in NAT64 prefixes, non-zero
415 * return value stop walk.
417 typedef int (*nat64_prefix_walk_fn_t) (nat64_prefix_t * pref64, void *ctx);
420 * @brief Walk NAT64 prefixes.
422 * @param fn The function to invoke on each entry visited.
423 * @param ctx A context passed in the visit function.
425 void nat64_prefix_walk (nat64_prefix_walk_fn_t fn, void *ctx);
428 * Compose IPv4-embedded IPv6 addresses.
429 * @param ip6 IPv4-embedded IPv6 addresses.
430 * @param ip4 IPv4 address.
431 * @param fib_index Tenant FIB index.
433 void nat64_compose_ip6 (ip6_address_t * ip6, ip4_address_t * ip4,
437 * Extract IPv4 address from the IPv4-embedded IPv6 addresses.
439 * @param ip6 IPv4-embedded IPv6 addresses.
440 * @param ip4 IPv4 address.
441 * @param fib_index Tenant FIB index.
443 void nat64_extract_ip4 (ip6_address_t * ip6, ip4_address_t * ip4,
447 * @brief Set NAT64 hash tables configuration.
449 * @param bib_buckets Number of BIB hash buckets.
450 * @param bib_memory_size Memory size of BIB hash.
451 * @param st_buckets Number of session table hash buckets.
452 * @param st_memory_size Memory size of session table hash.
454 void nat64_set_hash (u32 bib_buckets, uword bib_memory_size, u32 st_buckets,
455 uword st_memory_size);
458 * @brief Get worker thread index for NAT64 in2out.
460 * @param addr IPv6 src address.
462 * @returns worker thread index.
464 u32 nat64_get_worker_in2out (ip6_address_t * addr);
467 * @brief Get worker thread index for NAT64 out2in.
469 * @param ip IPv4 header.
471 * @returns worker thread index.
473 u32 nat64_get_worker_out2in (vlib_buffer_t * b, ip4_header_t * ip);
475 /* NAT64 interface flags */
476 #define NAT64_INTERFACE_FLAG_IS_INSIDE 1
477 #define NAT64_INTERFACE_FLAG_IS_OUTSIDE 2
479 /** \brief Check if NAT64 interface is inside.
480 @param i NAT64 interface
481 @return 1 if inside interface
483 #define nat64_interface_is_inside(i) i->flags & NAT64_INTERFACE_FLAG_IS_INSIDE
485 /** \brief Check if NAT64 interface is outside.
486 @param i NAT64 interface
487 @return 1 if outside interface
489 #define nat64_interface_is_outside(i) i->flags & NAT64_INTERFACE_FLAG_IS_OUTSIDE
491 static_always_inline u8
494 nat64_main_t *nm = &nat64_main;
499 nat64_add_del_addr_to_fib (ip4_address_t * addr, u8 p_len, u32 sw_if_index,
502 int nat64_plugin_enable (nat64_config_t c);
503 int nat64_plugin_disable ();
504 void nat64_reset_timeouts ();
506 format_function_t format_nat_protocol;
507 unformat_function_t unformat_nat_protocol;
510 #define nat64_log_err(...) \
511 vlib_log(VLIB_LOG_LEVEL_ERR, nat64_main.log_class, __VA_ARGS__)
512 #define nat64_log_warn(...) \
513 vlib_log(VLIB_LOG_LEVEL_WARNING, nat64_main.log_class, __VA_ARGS__)
514 #define nat64_log_notice(...) \
515 vlib_log(VLIB_LOG_LEVEL_NOTICE, nat64_main.log_class, __VA_ARGS__)
516 #define nat64_log_info(...) \
517 vlib_log(VLIB_LOG_LEVEL_INFO, nat64_main.log_class, __VA_ARGS__)
518 #define nat64_log_debug(...)\
519 vlib_log(VLIB_LOG_LEVEL_DEBUG, nat64_main.log_class, __VA_ARGS__)
521 clib_error_t *nat64_api_hookup (vlib_main_t * vm);
523 #endif /* __included_nat64_h__ */
526 * fd.io coding-style-patch-verification: ON
529 * eval: (c-set-style "gnu")