18 #ifndef __included_nat_h__ 19 #define __included_nat_h__ 34 #define SNAT_UDP_TIMEOUT 300 35 #define SNAT_TCP_TRANSITORY_TIMEOUT 240 36 #define SNAT_TCP_ESTABLISHED_TIMEOUT 7440 37 #define SNAT_ICMP_TIMEOUT 60 40 #define NAT_FQ_NELTS 64 43 #define SNAT_FLAG_HAIRPINNING (1 << 0) 114 #define foreach_nat_addr_and_port_alloc_alg \ 115 _(0, DEFAULT, "default") \ 116 _(1, MAPE, "map-e") \ 117 _(2, RANGE, "port-range") 121 #define _(v, N, s) NAT_ADDR_AND_PORT_ALLOC_ALG_##N = v, 128 #define foreach_snat_protocol \ 129 _(UDP, 0, udp, "udp") \ 130 _(TCP, 1, tcp, "tcp") \ 131 _(ICMP, 2, icmp, "icmp") 135 #define _(N, i, n, s) SNAT_PROTOCOL_##N = i, 142 #define foreach_snat_session_state \ 143 _(0, UNKNOWN, "unknown") \ 144 _(1, UDP_ACTIVE, "udp-active") \ 145 _(2, TCP_SYN_SENT, "tcp-syn-sent") \ 146 _(3, TCP_ESTABLISHED, "tcp-established") \ 147 _(4, TCP_FIN_WAIT, "tcp-fin-wait") \ 148 _(5, TCP_CLOSE_WAIT, "tcp-close-wait") \ 149 _(6, TCP_CLOSING, "tcp-closing") \ 150 _(7, TCP_LAST_ACK, "tcp-last-ack") \ 151 _(8, TCP_CLOSED, "tcp-closed") \ 152 _(9, ICMP_ACTIVE, "icmp-active") 156 #define _(v, N, s) SNAT_SESSION_##N = v, 162 #define NAT44_SES_I2O_FIN 1 163 #define NAT44_SES_O2I_FIN 2 164 #define NAT44_SES_I2O_FIN_ACK 4 165 #define NAT44_SES_O2I_FIN_ACK 8 166 #define NAT44_SES_I2O_SYN 16 167 #define NAT44_SES_O2I_SYN 32 168 #define NAT44_SES_RST 64 171 #define SNAT_SESSION_FLAG_STATIC_MAPPING 1 172 #define SNAT_SESSION_FLAG_UNKNOWN_PROTO 2 173 #define SNAT_SESSION_FLAG_LOAD_BALANCING 4 174 #define SNAT_SESSION_FLAG_TWICE_NAT 8 175 #define SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT 16 176 #define SNAT_SESSION_FLAG_FWD_BYPASS 32 177 #define SNAT_SESSION_FLAG_AFFINITY 64 178 #define SNAT_SESSION_FLAG_OUTPUT_FEATURE 128 181 #define NAT_INTERFACE_FLAG_IS_INSIDE 1 182 #define NAT_INTERFACE_FLAG_IS_OUTSIDE 2 185 #define NAT_STATIC_MAPPING_FLAG_ADDR_ONLY 1 186 #define NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY 2 187 #define NAT_STATIC_MAPPING_FLAG_IDENTITY_NAT 4 188 #define NAT_STATIC_MAPPING_FLAG_LB 8 204 u32 per_user_list_head_index;
219 u16 ext_host_nat_port;
246 #define _(N, i, n, s) \ 247 u16 busy_##n##_ports; \ 248 u16 * busy_##n##_ports_per_thread; \ 249 uword * busy_##n##_port_bitmap; 414 u8 * p_dont_translate,
void *d,
428 u32 snat_thread_index);
605 #define snat_is_session_static(s) (s->flags & SNAT_SESSION_FLAG_STATIC_MAPPING) 611 #define snat_is_unk_proto_session(s) (s->flags & SNAT_SESSION_FLAG_UNKNOWN_PROTO) 617 #define is_twice_nat_session(s) (s->flags & SNAT_SESSION_FLAG_TWICE_NAT) 623 #define is_lb_session(s) (s->flags & SNAT_SESSION_FLAG_LOAD_BALANCING) 629 #define is_fwd_bypass_session(s) (s->flags & SNAT_SESSION_FLAG_FWD_BYPASS) 635 #define is_ed_session(s) (s->flags & SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT) 641 #define is_affinity_sessions(s) (s->flags & SNAT_SESSION_FLAG_AFFINITY) 647 #define nat_interface_is_inside(i) i->flags & NAT_INTERFACE_FLAG_IS_INSIDE 653 #define nat_interface_is_outside(i) i->flags & NAT_INTERFACE_FLAG_IS_OUTSIDE 659 #define nat44_is_ses_closed(s) s->state == 0xf 665 #define is_addr_only_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_ADDR_ONLY) 671 #define is_out2in_only_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY) 677 #define is_identity_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_IDENTITY_NAT) 683 #define is_lb_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_LB) 689 #define tcp_is_init(t) ((t->flags & TCP_FLAG_SYN) && !(t->flags & TCP_FLAG_ACK)) 692 #define nat_log_err(...) \ 693 vlib_log(VLIB_LOG_LEVEL_ERR, snat_main.log_class, __VA_ARGS__) 694 #define nat_log_warn(...) \ 695 vlib_log(VLIB_LOG_LEVEL_WARNING, snat_main.log_class, __VA_ARGS__) 696 #define nat_log_notice(...) \ 697 vlib_log(VLIB_LOG_LEVEL_NOTICE, snat_main.log_class, __VA_ARGS__) 698 #define nat_log_info(...) \ 699 vlib_log(VLIB_LOG_LEVEL_INFO, snat_main.log_class, __VA_ARGS__) 700 #define nat_log_debug(...)\ 701 vlib_log(VLIB_LOG_LEVEL_DEBUG, snat_main.log_class, __VA_ARGS__) 708 u8 * p_dont_translate,
void *d,
void *e);
713 u8 * p_dont_translate,
void *d,
void *e);
718 u8 * p_dont_translate,
void *d,
void *e);
723 u8 * p_dont_translate,
void *d,
void *e);
730 u8 * p_dont_translate,
void *d,
void *e);
735 u8 * p_dont_translate,
void *d,
void *e);
742 u8 * p_dont_translate,
void *d,
void *e);
747 u8 * p_dont_translate,
void *d,
void *e);
750 icmp46_header_t * icmp0,
u32 sw_if_index0,
u32 rx_fib_index0,
755 icmp46_header_t * icmp0,
u32 sw_if_index0,
u32 rx_fib_index0,
772 u32 proto0,
int is_ed);
841 snat_protocol_t proto,
int is_add,
842 twice_nat_type_t twice_nat,
u8 out2in_only,
843 u8 * tag,
u8 identity_nat);
861 snat_protocol_t proto,
863 twice_nat_type_t twice_nat,
864 u8 out2in_only,
u8 * tag,
u32 affinity);
868 snat_protocol_t proto,
u32 vrf_id,
869 u8 probability,
u8 is_add);
929 snat_protocol_t proto,
u32 vrf_id,
int is_in);
944 u32 vrf_id,
int is_in);
965 u32 fib_index,
u32 thread_index);
988 u32 thread_index,
f64 now);
1040 u16 port_per_thread,
1041 u32 snat_thread_index);
1062 twice_nat_type_t * twice_nat,
1065 u8 * is_identity_nat);
ip4_address_t external_addr
int snat_del_address(snat_main_t *sm, ip4_address_t addr, u8 delete_sm, u8 twice_nat)
Delete external address from NAT44 pool.
clib_error_t * snat_api_init(vlib_main_t *vm, snat_main_t *sm)
#define foreach_nat_addr_and_port_alloc_alg
nat_outside_fib_t * outside_fibs
vlib_node_registration_t snat_hairpin_src_node
(constructor) VLIB_REGISTER_NODE (snat_hairpin_src_node)
nat_addr_and_port_alloc_alg_t
void nat_free_session_data(snat_main_t *sm, snat_session_t *s, u32 thread_index)
Free NAT44 session data (lookup keys, external addrres port)
u32 sessions_per_user_list_head_index
int snat_interface_add_del(u32 sw_if_index, u8 is_inside, int is_del)
Enable/disable NAT44 feature on the interface.
u32 icmp_out2in(snat_main_t *sm, vlib_buffer_t *b0, ip4_header_t *ip0, icmp46_header_t *icmp0, u32 sw_if_index0, u32 rx_fib_index0, vlib_node_runtime_t *node, u32 next0, u32 thread_index, void *d, void *e)
u32 fq_in2out_output_index
u32 icmp_match_in2out_det(snat_main_t *sm, vlib_node_runtime_t *node, u32 thread_index, vlib_buffer_t *b0, ip4_header_t *ip0, u8 *p_proto, snat_session_key_t *p_value, u8 *p_dont_translate, void *d, void *e)
Get address and port values to be used for ICMP packet translation and create session if needed...
#define foreach_snat_session_state
int snat_add_interface_address(snat_main_t *sm, u32 sw_if_index, int is_del, u8 twice_nat)
Add/delete NAT44 pool address from specific interfce.
u32 icmp_match_out2in_slow(snat_main_t *sm, vlib_node_runtime_t *node, u32 thread_index, vlib_buffer_t *b0, ip4_header_t *ip0, u8 *p_proto, snat_session_key_t *p_value, u8 *p_dont_translate, void *d, void *e)
Get address and port values to be used for ICMP packet translation and create session if needed...
vlib_node_registration_t nat44_ed_in2out_node
(constructor) VLIB_REGISTER_NODE (nat44_ed_in2out_node)
format_function_t format_det_map_ses
vlib_node_registration_t snat_det_out2in_node
(constructor) VLIB_REGISTER_NODE (snat_det_out2in_node)
int nat44_i2o_ed_is_idle_session_cb(clib_bihash_kv_16_8_t *kv, void *arg)
vlib_node_registration_t nat44_ed_hairpin_src_node
(constructor) VLIB_REGISTER_NODE (nat44_ed_hairpin_src_node)
format_function_t format_snat_static_mapping
format_function_t format_snat_key
snat_det_map_t * det_maps
nat_alloc_out_addr_and_port_function_t * alloc_addr_and_port
#define foreach_snat_protocol
struct _tcp_header tcp_header_t
int snat_hairpinning(snat_main_t *sm, vlib_buffer_t *b0, ip4_header_t *ip0, udp_header_t *udp0, tcp_header_t *tcp0, u32 proto0, int is_ed)
u32 icmp_match_out2in_ed(snat_main_t *sm, vlib_node_runtime_t *node, u32 thread_index, vlib_buffer_t *b0, ip4_header_t *ip0, u8 *p_proto, snat_session_key_t *p_value, u8 *p_dont_translate, void *d, void *e)
nat44_lb_addr_port_t * locals
int snat_set_workers(uword *bitmap)
Set NAT plugin workers.
typedef CLIB_PACKED(struct { snat_session_key_t out2in;snat_session_key_t in2out;u32 flags;u32 per_user_index;u32 per_user_list_head_index;f64 last_heard;u64 total_bytes;u32 total_pkts;ip4_address_t ext_host_addr;u16 ext_host_port;ip4_address_t ext_host_nat_addr;u16 ext_host_nat_port;u8 state;u32 i2o_fin_seq;u32 o2i_fin_seq;u32 user_index;}) snat_session_t
clib_bihash_8_8_t user_hash
void nat_set_alloc_addr_and_port_mape(u16 psid, u16 psid_offset, u16 psid_length)
Set address and port assignment algorithm for MAP-E CE.
u32 max_translations_per_user
u32 in2out_output_node_index
vlib_node_registration_t snat_out2in_node
(constructor) VLIB_REGISTER_NODE (snat_out2in_node)
ip4_address_t ext_host_addr
int snat_add_address(snat_main_t *sm, ip4_address_t *addr, u32 vrf_id, u8 twice_nat)
Add external address to NAT44 pool.
vhost_vring_state_t state
A collection of simple counters.
vlib_node_registration_t snat_in2out_output_node
(constructor) VLIB_REGISTER_NODE (snat_in2out_output_node)
vlib_node_registration_t snat_in2out_output_worker_handoff_node
(constructor) VLIB_REGISTER_NODE (snat_in2out_output_worker_handoff_node)
u32 icmp_match_out2in_det(snat_main_t *sm, vlib_node_runtime_t *node, u32 thread_index, vlib_buffer_t *b0, ip4_header_t *ip0, u8 *p_proto, snat_session_key_t *p_value, u8 *p_dont_translate, void *d, void *e)
Get address and port values to be used for ICMP packet translation and create session if needed...
vlib_node_registration_t snat_out2in_fast_node
(constructor) VLIB_REGISTER_NODE (snat_out2in_fast_node)
twice_nat_type_t twice_nat
u32 * auto_add_sw_if_indices_twice_nat
vlib_node_registration_t nat44_ed_out2in_node
(constructor) VLIB_REGISTER_NODE (nat44_ed_out2in_node)
vlib_node_registration_t snat_hairpin_dst_node
(constructor) VLIB_REGISTER_NODE (snat_hairpin_dst_node)
nat_addr_and_port_alloc_alg_t addr_and_port_alloc_alg
clib_bihash_16_8_t out2in_ed
snat_det_session_t * sessions
snat_static_mapping_t * static_mappings
int nat44_del_ed_session(snat_main_t *sm, ip4_address_t *addr, u16 port, ip4_address_t *eh_addr, u16 eh_port, u8 proto, u32 vrf_id, int is_in)
Delete NAT44 endpoint-dependent session.
vlib_node_registration_t nat44_ed_out2in_worker_handoff_node
void nat_set_alloc_addr_and_port_default(void)
Set address and port assignment algorithm to default/standard.
int nat44_lb_static_mapping_add_del_local(ip4_address_t e_addr, u16 e_port, ip4_address_t l_addr, u16 l_port, snat_protocol_t proto, u32 vrf_id, u8 probability, u8 is_add)
void snat_add_del_addr_to_fib(ip4_address_t *addr, u8 p_len, u32 sw_if_index, int is_add)
Add/del NAT address to FIB.
clib_bihash_8_8_t static_mapping_by_external
struct snat_main_s snat_main_t
u32() snat_get_worker_function_t(ip4_header_t *ip, u32 rx_fib_index)
snat_session_t * nat_session_alloc_or_recycle(snat_main_t *sm, snat_user_t *u, u32 thread_index)
Allocate new NAT session or recycle last used.
vlib_node_registration_t nat44_ed_in2out_worker_handoff_node
u32 snat_icmp_hairpinning(snat_main_t *sm, vlib_buffer_t *b0, ip4_header_t *ip0, icmp46_header_t *icmp0, int is_ed)
API main structure, used by both vpp and binary API clients.
int nat44_o2i_is_idle_session_cb(clib_bihash_kv_8_8_t *kv, void *arg)
snat_interface_t * output_feature_interfaces
int snat_add_static_mapping(ip4_address_t l_addr, ip4_address_t e_addr, u16 l_port, u16 e_port, u32 vrf_id, int addr_only, u32 sw_if_index, snat_protocol_t proto, int is_add, twice_nat_type_t twice_nat, u8 out2in_only, u8 *tag, u8 identity_nat)
Add/delete NAT44 static mapping.
vlib_node_registration_t snat_det_in2out_node
(constructor) VLIB_REGISTER_NODE (snat_det_in2out_node)
int nat44_o2i_ed_is_idle_session_cb(clib_bihash_kv_16_8_t *kv, void *arg)
u8 static_mapping_connection_tracking
snat_get_worker_function_t * worker_in2out_cb
format_function_t format_snat_static_map_to_resolve
format_function_t format_nat_addr_and_port_alloc_alg
int snat_static_mapping_match(snat_main_t *sm, snat_session_key_t match, snat_session_key_t *mapping, u8 by_external, u8 *is_addr_only, twice_nat_type_t *twice_nat, lb_nat_type_t *lb, ip4_address_t *ext_host_addr, u8 *is_identity_nat)
Match NAT44 static mapping.
8 octet key, 8 octet key value pair
snat_user_t * nat_user_get_or_create(snat_main_t *sm, ip4_address_t *addr, u32 fib_index, u32 thread_index)
Find or create NAT user.
void increment_v4_address(ip4_address_t *a)
Increment IPv4 address.
u32 tcp_transitory_timeout
u32 * auto_add_sw_if_indices
void nat44_add_del_address_dpo(ip4_address_t addr, u8 is_add)
Add/delete external address to FIB DPO (out2in DPO mode)
u32 icmp_match_in2out_slow(snat_main_t *sm, vlib_node_runtime_t *node, u32 thread_index, vlib_buffer_t *b0, ip4_header_t *ip0, u8 *p_proto, snat_session_key_t *p_value, u8 *p_dont_translate, void *d, void *e)
Get address and port values to be used for ICMP packet translation and create session if needed...
unformat_function_t unformat_snat_protocol
void snat_free_outside_address_and_port(snat_address_t *addresses, u32 thread_index, snat_session_key_t *k)
Free outside address and port pair.
snat_get_worker_function_t * worker_out2in_cb
snat_icmp_match_function_t * icmp_match_out2in_cb
vlib_log_class_t log_class
vlib_node_registration_t snat_out2in_worker_handoff_node
(constructor) VLIB_REGISTER_NODE (snat_out2in_worker_handoff_node)
format_function_t format_snat_protocol
snat_address_t * twice_nat_addresses
u32 icmp_match_out2in_fast(snat_main_t *sm, vlib_node_runtime_t *node, u32 thread_index, vlib_buffer_t *b0, ip4_header_t *ip0, u8 *p_proto, snat_session_key_t *p_value, u8 *p_dont_translate, void *d, void *e)
Get address and port values to be used for ICMP packet translation.
format_function_t format_snat_session
struct _vlib_node_registration vlib_node_registration_t
format_function_t format_nat44_reass_trace
vlib_node_registration_t nat44_ed_hairpin_dst_node
(constructor) VLIB_REGISTER_NODE (nat44_ed_hairpin_dst_node)
vlib_simple_counter_main_t total_users
void nat_hairpinning_sm_unknown_proto(snat_main_t *sm, vlib_buffer_t *b, ip4_header_t *ip)
format_function_t format_static_mapping_key
snat_main_per_thread_data_t * per_thread_data
int nat44_i2o_is_idle_session_cb(clib_bihash_kv_8_8_t *kv, void *arg)
u32 icmp_match_in2out_ed(snat_main_t *sm, vlib_node_runtime_t *node, u32 thread_index, vlib_buffer_t *b0, ip4_header_t *ip0, u8 *p_proto, snat_session_key_t *p_value, u8 *p_dont_translate, void *d, void *e)
u32 affinity_per_service_list_head_index
snat_address_t * addresses
void nat_set_alloc_addr_and_port_range(u16 start_port, u16 end_port)
Set address and port assignment algorithm for port range.
format_function_t format_snat_user
snat_static_map_resolve_t * to_resolve
void nat44_ed_hairpinning_unknown_proto(snat_main_t *sm, vlib_buffer_t *b, ip4_header_t *ip)
int nat44_add_del_lb_static_mapping(ip4_address_t e_addr, u16 e_port, snat_protocol_t proto, nat44_lb_addr_port_t *locals, u8 is_add, twice_nat_type_t twice_nat, u8 out2in_only, u8 *tag, u32 affinity)
Add/delete static mapping with load-balancing (multiple backends)
vlib_node_registration_t snat_in2out_worker_handoff_node
(constructor) VLIB_REGISTER_NODE (snat_in2out_worker_handoff_node)
vlib_node_registration_t snat_in2out_fast_node
(constructor) VLIB_REGISTER_NODE (snat_in2out_fast_node)
u32 translation_memory_size
int snat_alloc_outside_address_and_port(snat_address_t *addresses, u32 fib_index, u32 thread_index, snat_session_key_t *k, u16 port_per_thread, u32 snat_thread_index)
Alloc outside address and port.
int snat_interface_add_del_output_feature(u32 sw_if_index, u8 is_inside, int is_del)
Enable/disable NAT44 output feature on the interface (postrouting NAT)
u32 icmp_in2out(snat_main_t *sm, vlib_buffer_t *b0, ip4_header_t *ip0, icmp46_header_t *icmp0, u32 sw_if_index0, u32 rx_fib_index0, vlib_node_runtime_t *node, u32 next0, u32 thread_index, void *d, void *e)
vlib_node_registration_t nat44_ed_in2out_output_worker_handoff_node
clib_bihash_16_8_t in2out_ed
vlib_node_registration_t snat_in2out_node
(constructor) VLIB_REGISTER_NODE (snat_in2out_node)
vlib_node_registration_t nat44_ed_in2out_output_node
(constructor) VLIB_REGISTER_NODE (nat44_ed_in2out_output_node)
vlib_simple_counter_main_t total_sessions
ip_lookup_main_t * ip4_lookup_main
u32 icmp_match_in2out_fast(snat_main_t *sm, vlib_node_runtime_t *node, u32 thread_index, vlib_buffer_t *b0, ip4_header_t *ip0, u8 *p_proto, snat_session_key_t *p_value, u8 *p_dont_translate, void *d, void *e)
Get address and port values to be used for ICMP packet translation.
snat_session_t * sessions
snat_icmp_match_function_t * icmp_match_in2out_cb
snat_session_t * nat_ed_session_alloc(snat_main_t *sm, snat_user_t *u, u32 thread_index, f64 now)
Allocate NAT endpoint-dependent session.
u32() snat_icmp_match_function_t(struct snat_main_s *sm, vlib_node_runtime_t *node, u32 thread_index, vlib_buffer_t *b0, ip4_header_t *ip0, u8 *p_proto, snat_session_key_t *p_value, u8 *p_dont_translate, void *d, void *e)
clib_bihash_8_8_t static_mapping_by_local
int nat44_del_session(snat_main_t *sm, ip4_address_t *addr, u16 port, snat_protocol_t proto, u32 vrf_id, int is_in)
Delete NAT44 session.
void nat44_reass_hairpinning(snat_main_t *sm, vlib_buffer_t *b0, ip4_header_t *ip0, u16 sport, u16 dport, u32 proto0, int is_ed)
snat_interface_t * interfaces
u32 tcp_established_timeout
int() nat_alloc_out_addr_and_port_function_t(snat_address_t *addresses, u32 fib_index, u32 thread_index, snat_session_key_t *k, u16 port_per_thread, u32 snat_thread_index)