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;
96 #define _(N, i, n, s) \
97 u16 busy_##n##_ports; \
98 u16 * busy_##n##_ports_per_thread; \
99 u32 busy_##n##_port_refcounts[65535];
114 nat64_config_t config;
116 /* API message ID base */
120 vlib_log_class_t log_class;
122 /** Interface pool */
123 nat64_interface_t *interfaces;
125 /** Address pool vector */
126 nat64_address_t *addr_pool;
128 /** sw_if_indices whose interface addresses should be auto-added */
129 u32 *auto_add_sw_if_indices;
132 nat64_prefix_t *pref64;
134 /** BIB and session DB per thread */
137 /** Worker handoff */
141 /** Pool of static BIB entries to be added/deleted in worker threads */
142 nat64_static_bib_to_update_t *static_bibs;
144 /** config parameters */
146 uword bib_memory_size;
148 uword st_memory_size;
150 /** values of various timeouts */
153 u32 tcp_trans_timeout;
156 /* Total count of interfaces enabled */
157 u32 total_enabled_count;
159 /* Expire walk process node index */
160 u32 expire_walk_node_index;
162 /* Expire worker walk process node index */
163 u32 expire_worker_walk_node_index;
165 /* counters/gauges */
166 vlib_simple_counter_main_t total_bibs;
167 vlib_simple_counter_main_t total_sessions;
170 u32 error_node_index;
172 u32 in2out_node_index;
173 u32 in2out_slowpath_node_index;
175 u32 out2in_node_index;
177 #define _(x) vlib_simple_counter_main_t x;
193 ip4_main_t *ip4_main;
196 vnet_main_t *vnet_main;
198 /* Randomize port allocation order */
201 /* TCP MSS clamping */
204 fib_source_t fib_src_hi;
205 fib_source_t fib_src_low;
207 /* Thread settings */
209 u32 first_worker_index;
215 extern nat64_main_t nat64_main;
216 extern vlib_node_registration_t nat64_in2out_node;
217 extern vlib_node_registration_t nat64_out2in_node;
220 * @brief Add/delete address to NAT64 pool.
222 * @param thread_index Thread index used by ipfix nat logging (not address per thread).
223 * @param addr IPv4 address.
224 * @param vrf_id VRF id of tenant, ~0 means independent of VRF.
225 * @param is_add 1 if add, 0 if delete.
227 * @returns 0 on success, non-zero value otherwise.
229 int nat64_add_del_pool_addr (u32 thread_index,
230 ip4_address_t * addr, u32 vrf_id, u8 is_add);
233 * @brief Call back function when walking addresses in NAT64 pool, non-zero
234 * return value stop walk.
236 typedef int (*nat64_pool_addr_walk_fn_t) (nat64_address_t * addr, void *ctx);
239 * @brief Walk NAT64 pool.
241 * @param fn The function to invoke on each entry visited.
242 * @param ctx A context passed in the visit function.
244 void nat64_pool_addr_walk (nat64_pool_addr_walk_fn_t fn, void *ctx);
247 * @brief NAT64 pool address from specific (DHCP addressed) interface.
249 * @param sw_if_index Index of the interface.
250 * @param is_add 1 if add, 0 if delete.
252 * @returns 0 on success, non-zero value otherwise.
254 int nat64_add_interface_address (u32 sw_if_index, int is_add);
257 * @brief Enable/disable NAT64 feature on the interface.
259 * @param sw_if_index Index of the interface.
260 * @param is_inside 1 if inside, 0 if outside.
261 * @param is_add 1 if add, 0 if delete.
263 * @returns 0 on success, non-zero value otherwise.
265 int nat64_interface_add_del (u32 sw_if_index, u8 is_inside, u8 is_add);
268 * @brief Call back function when walking interfaces with NAT64 feature,
269 * non-zero return value stop walk.
271 typedef int (*nat64_interface_walk_fn_t) (nat64_interface_t * i, void *ctx);
274 * @brief Walk NAT64 interfaces.
276 * @param fn The function to invoke on each entry visited.
277 * @param ctx A context passed in the visit function.
279 void nat64_interfaces_walk (nat64_interface_walk_fn_t fn, void *ctx);
282 * @brief Initialize NAT64.
284 * @param vm vlib main.
286 * @return error code.
288 clib_error_t *nat64_init (vlib_main_t * vm);
291 * @brief Add/delete static NAT64 BIB entry.
293 * @param in_addr Inside IPv6 address.
294 * @param out_addr Outside IPv4 address.
295 * @param in_port Inside port number.
296 * @param out_port Outside port number.
297 * @param proto L4 protocol.
298 * @param vrf_id VRF id of tenant.
299 * @param is_add 1 if add, 0 if delete.
301 * @returns 0 on success, non-zero value otherwise.
303 int nat64_add_del_static_bib_entry (ip6_address_t * in_addr,
304 ip4_address_t * out_addr, u16 in_port,
305 u16 out_port, u8 proto, u32 vrf_id,
309 * @brief Alloce IPv4 address and port pair from NAT64 pool.
311 * @param fib_index FIB index of tenant.
312 * @param proto L4 protocol.
313 * @param addr Allocated IPv4 address.
314 * @param port Allocated port number.
315 * @param thread_index Thread index.
317 * @returns 0 on success, non-zero value otherwise.
319 int nat64_alloc_out_addr_and_port (u32 fib_index, nat_protocol_t proto,
320 ip4_address_t * addr, u16 * port,
324 * @brief Set UDP session timeout.
326 * @param timeout Timeout value in seconds (if 0 reset to default value 300sec).
328 * @returns 0 on success, non-zero value otherwise.
330 int nat64_set_udp_timeout (u32 timeout);
333 * @brief Get UDP session timeout.
335 * @returns UDP session timeout in seconds.
337 u32 nat64_get_udp_timeout (void);
340 * @brief Set ICMP session timeout.
342 * @param timeout Timeout value in seconds (if 0 reset to default value 60sec).
344 * @returns 0 on success, non-zero value otherwise.
346 int nat64_set_icmp_timeout (u32 timeout);
349 * @brief Get ICMP session timeout.
351 * @returns ICMP session timeout in seconds.
353 u32 nat64_get_icmp_timeout (void);
356 * @brief Set TCP session timeouts.
358 * @param trans Transitory timeout in seconds (if 0 reset to default value 240sec).
359 * @param est Established timeout in seconds (if 0 reset to default value 7440sec).
361 * @returns 0 on success, non-zero value otherwise.
363 int nat64_set_tcp_timeouts (u32 trans, u32 est);
366 * @brief Get TCP transitory timeout.
368 * @returns TCP transitory timeout in seconds.
370 u32 nat64_get_tcp_trans_timeout (void);
373 * @brief Get TCP established timeout.
375 * @returns TCP established timeout in seconds.
377 u32 nat64_get_tcp_est_timeout (void);
380 * @brief Reset NAT64 session timeout.
382 * @param ste Session table entry.
383 * @param vm VLIB main.
385 void nat64_session_reset_timeout (nat64_db_st_entry_t * ste,
389 * @brief Set NAT64 TCP session state.
391 * @param ste Session table entry.
392 * @param tcp TCP header.
393 * @param is_ip6 1 if IPv6 packet, 0 if IPv4.
395 void nat64_tcp_session_set_state (nat64_db_st_entry_t * ste,
396 tcp_header_t * tcp, u8 is_ip6);
399 * @brief Add/delete NAT64 prefix.
401 * @param prefix NAT64 prefix.
402 * @param plen Prefix length.
403 * @param vrf_id VRF id of tenant.
404 * @param is_add 1 if add, 0 if delete.
406 * @returns 0 on success, non-zero value otherwise.
408 int nat64_add_del_prefix (ip6_address_t * prefix, u8 plen, u32 vrf_id,
412 * @brief Call back function when walking addresses in NAT64 prefixes, non-zero
413 * return value stop walk.
415 typedef int (*nat64_prefix_walk_fn_t) (nat64_prefix_t * pref64, void *ctx);
418 * @brief Walk NAT64 prefixes.
420 * @param fn The function to invoke on each entry visited.
421 * @param ctx A context passed in the visit function.
423 void nat64_prefix_walk (nat64_prefix_walk_fn_t fn, void *ctx);
426 * Compose IPv4-embedded IPv6 addresses.
427 * @param ip6 IPv4-embedded IPv6 addresses.
428 * @param ip4 IPv4 address.
429 * @param fib_index Tenant FIB index.
431 void nat64_compose_ip6 (ip6_address_t * ip6, ip4_address_t * ip4,
435 * Extract IPv4 address from the IPv4-embedded IPv6 addresses.
437 * @param ip6 IPv4-embedded IPv6 addresses.
438 * @param ip4 IPv4 address.
439 * @param fib_index Tenant FIB index.
441 void nat64_extract_ip4 (ip6_address_t * ip6, ip4_address_t * ip4,
445 * @brief Set NAT64 hash tables configuration.
447 * @param bib_buckets Number of BIB hash buckets.
448 * @param bib_memory_size Memory size of BIB hash.
449 * @param st_buckets Number of session table hash buckets.
450 * @param st_memory_size Memory size of session table hash.
452 void nat64_set_hash (u32 bib_buckets, uword bib_memory_size, u32 st_buckets,
453 uword st_memory_size);
456 * @brief Get worker thread index for NAT64 in2out.
458 * @param addr IPv6 src address.
460 * @returns worker thread index.
462 u32 nat64_get_worker_in2out (ip6_address_t * addr);
465 * @brief Get worker thread index for NAT64 out2in.
467 * @param ip IPv4 header.
469 * @returns worker thread index.
471 u32 nat64_get_worker_out2in (vlib_buffer_t * b, ip4_header_t * ip);
473 /* NAT64 interface flags */
474 #define NAT64_INTERFACE_FLAG_IS_INSIDE 1
475 #define NAT64_INTERFACE_FLAG_IS_OUTSIDE 2
477 /** \brief Check if NAT64 interface is inside.
478 @param i NAT64 interface
479 @return 1 if inside interface
481 #define nat64_interface_is_inside(i) i->flags & NAT64_INTERFACE_FLAG_IS_INSIDE
483 /** \brief Check if NAT64 interface is outside.
484 @param i NAT64 interface
485 @return 1 if outside interface
487 #define nat64_interface_is_outside(i) i->flags & NAT64_INTERFACE_FLAG_IS_OUTSIDE
489 static_always_inline u8
492 nat64_main_t *nm = &nat64_main;
497 nat64_add_del_addr_to_fib (ip4_address_t * addr, u8 p_len, u32 sw_if_index,
500 int nat64_plugin_enable (nat64_config_t c);
501 int nat64_plugin_disable ();
502 void nat64_reset_timeouts ();
504 format_function_t format_nat_protocol;
505 unformat_function_t unformat_nat_protocol;
508 #define nat64_log_err(...) \
509 vlib_log(VLIB_LOG_LEVEL_ERR, nat64_main.log_class, __VA_ARGS__)
510 #define nat64_log_warn(...) \
511 vlib_log(VLIB_LOG_LEVEL_WARNING, nat64_main.log_class, __VA_ARGS__)
512 #define nat64_log_notice(...) \
513 vlib_log(VLIB_LOG_LEVEL_NOTICE, nat64_main.log_class, __VA_ARGS__)
514 #define nat64_log_info(...) \
515 vlib_log(VLIB_LOG_LEVEL_INFO, nat64_main.log_class, __VA_ARGS__)
516 #define nat64_log_debug(...)\
517 vlib_log(VLIB_LOG_LEVEL_DEBUG, nat64_main.log_class, __VA_ARGS__)
519 clib_error_t *nat64_api_hookup (vlib_main_t * vm);
521 #endif /* __included_nat64_h__ */
524 * fd.io coding-style-patch-verification: ON
527 * eval: (c-set-style "gnu")