54 "NAT44_OUT2IN: sw_if_index %d, next index %d, session index %d",
66 s =
format (s,
"NAT44_OUT2IN_FAST: sw_if_index %d, next index %d",
75 #define foreach_snat_out2in_error \ 76 _(UNSUPPORTED_PROTOCOL, "Unsupported protocol") \ 77 _(OUT2IN_PACKETS, "Good out2in packets processed") \ 78 _(OUT_OF_PORTS, "Out of ports") \ 79 _(BAD_ICMP_TYPE, "unsupported ICMP type") \ 80 _(NO_TRANSLATION, "No translation") \ 81 _(MAX_SESSIONS_EXCEEDED, "Maximum sessions exceeded") \ 82 _(DROP_FRAGMENT, "Drop fragment") \ 83 _(MAX_REASS, "Maximum reassemblies exceeded") \ 84 _(MAX_FRAG, "Maximum fragments per reassembly exceeded") 88 #define _(sym,str) SNAT_OUT2IN_ERROR_##sym, 95 #define _(sym,string) string, 115 u64 sess_timeout_time;
122 if (ctx->
now >= sess_timeout_time)
124 s_kv.
key = s->in2out.as_u64;
125 if (clib_bihash_add_del_8_8 (&tsm->
in2out, &s_kv, 0))
129 s->out2in.addr.as_u32,
133 s->in2out.fib_index);
160 static inline snat_session_t *
166 u32 thread_index,
f64 now)
177 b0->
error = node->
errors[SNAT_OUT2IN_ERROR_MAX_SESSIONS_EXCEEDED];
212 kv0.
key = s->in2out.as_u64;
214 if (clib_bihash_add_or_overwrite_stale_8_8
219 kv0.
key = s->out2in.as_u64;
221 if (clib_bihash_add_or_overwrite_stale_8_8
228 s->out2in.addr.as_u32,
231 s->out2in.port, s->in2out.fib_index);
239 icmp46_header_t *icmp0;
244 icmp46_header_t *inner_icmp0;
263 case SNAT_PROTOCOL_ICMP:
264 inner_icmp0 = (icmp46_header_t *) l4_header;
268 case SNAT_PROTOCOL_UDP:
269 case SNAT_PROTOCOL_TCP:
273 return SNAT_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL;
299 u8 * p_dont_translate,
void *d,
void *e)
301 icmp46_header_t *icmp0;
306 snat_session_t *s0 = 0;
307 u8 dont_translate = 0;
323 b0->
error = node->
errors[SNAT_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL];
337 (sm, key0, &sm0, 1, &is_addr_only, 0, 0, 0, &identity_nat))
348 b0->
error = node->
errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
360 (icmp0->type != ICMP4_echo_request
363 b0->
error = node->
errors[SNAT_OUT2IN_ERROR_BAD_ICMP_TYPE];
387 icmp0->type != ICMP4_echo_request &&
390 b0->
error = node->
errors[SNAT_OUT2IN_ERROR_BAD_ICMP_TYPE];
402 *p_value = s0->in2out;
403 *p_dont_translate = dont_translate;
405 *(snat_session_t **) d = s0;
427 u8 * p_dont_translate,
void *d,
void *e)
429 icmp46_header_t *icmp0;
434 u8 dont_translate = 0;
453 (sm, key0, &sm0, 1, &is_addr_only, 0, 0, 0, 0))
461 b0->
error = node->
errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
467 (icmp0->type != ICMP4_echo_request || !is_addr_only) &&
470 b0->
error = node->
errors[SNAT_OUT2IN_ERROR_BAD_ICMP_TYPE];
479 *p_dont_translate = dont_translate;
487 icmp46_header_t * icmp0,
491 u32 next0,
u32 thread_index,
void *d,
void *e)
498 icmp46_header_t *inner_icmp0;
500 u32 new_addr0, old_addr0;
501 u16 old_id0, new_id0;
509 &protocol, &sm0, &dont_translate, d,
524 if (checksum0 != 0 && checksum0 != 0xffff)
540 if (icmp0->checksum == 0)
541 icmp0->checksum = 0xffff;
552 sum0 = icmp0->checksum;
573 sum0 = icmp0->checksum;
580 case SNAT_PROTOCOL_ICMP:
581 inner_icmp0 = (icmp46_header_t *) l4_header;
588 sum0 = icmp0->checksum;
593 case SNAT_PROTOCOL_UDP:
594 case SNAT_PROTOCOL_TCP:
599 sum0 = icmp0->checksum;
618 icmp46_header_t * icmp0,
623 u32 thread_index, snat_session_t ** p_s0)
625 next0 =
icmp_out2in (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
626 next0, thread_index, p_s0, 0);
627 snat_session_t *s0 = *p_s0;
648 u32 old_addr, new_addr;
675 u32 n_left_from, *from, *to_next;
677 u32 pkts_processed = 0;
686 while (n_left_from > 0)
692 while (n_left_from >= 4 && n_left_to_next >= 2)
698 u32 sw_if_index0, sw_if_index1;
701 u32 new_addr0, old_addr0;
702 u16 new_port0, old_port0;
703 u32 new_addr1, old_addr1;
704 u16 new_port1, old_port1;
707 icmp46_header_t *icmp0, *icmp1;
709 u32 rx_fib_index0, rx_fib_index1;
711 snat_session_t *s0 = 0, *s1 = 0;
713 u8 identity_nat0, identity_nat1;
730 to_next[0] = bi0 = from[0];
731 to_next[1] = bi1 = from[1];
746 icmp0 = (icmp46_header_t *) udp0;
756 ICMP4_time_exceeded_ttl_exceeded_in_transit,
771 node->
errors[SNAT_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL];
787 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
788 next0, now, thread_index, &s0);
799 if (clib_bihash_search_8_8
805 (sm, key0, &sm0, 1, 0, 0, 0, 0, &identity_nat0))
814 (UDP_DST_PORT_dhcp_to_client))))
823 node->
errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
859 old_port0 = tcp0->dst_port;
860 tcp0->dst_port = s0->in2out.port;
861 new_port0 = tcp0->dst_port;
863 sum0 = tcp0->checksum;
889 && (b0->
flags & VLIB_BUFFER_IS_TRACED)))
907 icmp1 = (icmp46_header_t *) udp1;
917 ICMP4_time_exceeded_ttl_exceeded_in_transit,
932 node->
errors[SNAT_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL];
948 (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node,
949 next1, now, thread_index, &s1);
960 if (clib_bihash_search_8_8
966 (sm, key1, &sm1, 1, 0, 0, 0, 0, &identity_nat1))
975 (UDP_DST_PORT_dhcp_to_client))))
984 node->
errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
1020 old_port1 = tcp1->dst_port;
1021 tcp1->dst_port = s1->in2out.port;
1022 new_port1 = tcp1->dst_port;
1024 sum1 = tcp1->checksum;
1050 && (b1->
flags & VLIB_BUFFER_IS_TRACED)))
1066 to_next, n_left_to_next,
1067 bi0, bi1, next0, next1);
1070 while (n_left_from > 0 && n_left_to_next > 0)
1078 u32 new_addr0, old_addr0;
1079 u16 new_port0, old_port0;
1082 icmp46_header_t *icmp0;
1086 snat_session_t *s0 = 0;
1096 n_left_to_next -= 1;
1105 icmp0 = (icmp46_header_t *) udp0;
1120 node->
errors[SNAT_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL];
1131 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1146 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1147 next0, now, thread_index, &s0);
1158 if (clib_bihash_search_8_8
1164 (sm, key0, &sm0, 1, 0, 0, 0, 0, &identity_nat0))
1172 clib_host_to_net_u16
1173 (UDP_DST_PORT_dhcp_to_client))))
1182 node->
errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
1218 old_port0 = tcp0->dst_port;
1219 tcp0->dst_port = s0->in2out.port;
1220 new_port0 = tcp0->dst_port;
1222 sum0 = tcp0->checksum;
1248 && (b0->
flags & VLIB_BUFFER_IS_TRACED)))
1264 to_next, n_left_to_next,
1272 SNAT_OUT2IN_ERROR_OUT2IN_PACKETS,
1280 .name =
"nat44-out2in",
1281 .vector_size =
sizeof (
u32),
1308 u32 n_left_from, *from, *to_next;
1310 u32 pkts_processed = 0;
1316 u32 *fragments_to_drop = 0;
1317 u32 *fragments_to_loopback = 0;
1323 while (n_left_from > 0)
1329 while (n_left_from > 0 && n_left_to_next > 0)
1331 u32 bi0, sw_if_index0, proto0, rx_fib_index0, new_addr0, old_addr0;
1336 nat_reass_ip4_t *reass0;
1339 icmp46_header_t *icmp0;
1342 snat_session_t *s0 = 0;
1343 u16 old_port0, new_port0;
1353 n_left_to_next -= 1;
1366 b0->
error = node->
errors[SNAT_OUT2IN_ERROR_DROP_FRAGMENT];
1373 icmp0 = (icmp46_header_t *) udp0;
1380 1, &fragments_to_drop);
1385 b0->
error = node->
errors[SNAT_OUT2IN_ERROR_MAX_REASS];
1395 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1396 next0, now, thread_index, &s0);
1401 reass0->sess_index = s0 - per_thread_data->
sessions;
1404 reass0->thread_index = thread_index;
1406 &fragments_to_loopback);
1418 if (clib_bihash_search_8_8
1419 (&per_thread_data->
out2in, &kv0, &value0))
1424 (sm, key0, &sm0, 1, 0, 0, 0, 0, &identity_nat0))
1433 clib_host_to_net_u16
1434 (UDP_DST_PORT_dhcp_to_client))))
1443 node->
errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
1450 &fragments_to_loopback);
1466 node->
errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
1470 reass0->sess_index = s0 - per_thread_data->
sessions;
1471 reass0->thread_index = thread_index;
1477 reass0->sess_index = value0.
value;
1488 (reass0, bi0, &fragments_to_drop))
1490 b0->
error = node->
errors[SNAT_OUT2IN_ERROR_MAX_FRAG];
1492 (
"maximum fragments per reassembly exceeded");
1500 reass0->sess_index);
1518 old_port0 = tcp0->dst_port;
1519 tcp0->dst_port = s0->in2out.port;
1520 new_port0 = tcp0->dst_port;
1522 sum0 = tcp0->checksum;
1549 && (b0->
flags & VLIB_BUFFER_IS_TRACED)))
1569 to_next, n_left_to_next,
1573 if (n_left_from == 0 &&
vec_len (fragments_to_loopback))
1580 sizeof (
u32) * len);
1587 fragments_to_loopback + (len -
1600 SNAT_OUT2IN_ERROR_OUT2IN_PACKETS,
1604 &node->
errors[SNAT_OUT2IN_ERROR_DROP_FRAGMENT],
1615 .name =
"nat44-out2in-reass",
1616 .vector_size =
sizeof (
u32),
1642 u32 n_left_from, *from, *to_next;
1644 u32 pkts_processed = 0;
1651 while (n_left_from > 0)
1657 while (n_left_from > 0 && n_left_to_next > 0)
1665 u32 new_addr0, old_addr0;
1666 u16 new_port0, old_port0;
1669 icmp46_header_t *icmp0;
1680 n_left_to_next -= 1;
1687 icmp0 = (icmp46_header_t *) udp0;
1699 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1712 next0 =
icmp_out2in (sm, b0, ip0, icmp0, sw_if_index0,
1713 rx_fib_index0, node, next0, ~0, 0, 0);
1723 b0->
error = node->
errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
1728 new_port0 = sm0.
port;
1743 old_port0 = tcp0->dst_port;
1744 tcp0->dst_port = new_port0;
1746 sum0 = tcp0->checksum;
1767 sum0 = tcp0->checksum;
1779 && (b0->
flags & VLIB_BUFFER_IS_TRACED)))
1791 to_next, n_left_to_next,
1799 SNAT_OUT2IN_ERROR_OUT2IN_PACKETS,
1807 .name =
"nat44-out2in-fast",
1808 .vector_size =
sizeof (
u32),
vlib_node_registration_t snat_out2in_fast_node
(constructor) VLIB_REGISTER_NODE (snat_out2in_fast_node)
static ip_csum_t ip_incremental_checksum_buffer(vlib_main_t *vm, vlib_buffer_t *first_buffer, u32 first_buffer_offset, u32 n_bytes_to_checksum, ip_csum_t sum)
VLIB_NODE_FUNCTION_MULTIARCH(snat_out2in_node, snat_out2in_node_fn)
#define snat_is_session_static(s)
Check if SNAT session is created from static mapping.
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...
static u8 * format_snat_out2in_fast_trace(u8 *s, va_list *args)
static u32 nat44_session_get_timeout(snat_main_t *sm, snat_session_t *s)
static u32 icmp_out2in_slow_path(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, f64 now, u32 thread_index, snat_session_t **p_s0)
static_always_inline u8 icmp_is_error_message(icmp46_header_t *icmp)
static f64 vlib_time_now(vlib_main_t *vm)
u32 fib_table_get_index_for_sw_if_index(fib_protocol_t proto, u32 sw_if_index)
Get the index of the FIB bound to the interface.
#define nat_log_warn(...)
u32 * fib_index_by_sw_if_index
Table index indexed by software interface.
vlib_error_t * errors
Vector of errors for this node.
int nat44_o2i_is_idle_session_cb(clib_bihash_kv_8_8_t *kv, void *arg)
static uword vlib_buffer_length_in_chain(vlib_main_t *vm, vlib_buffer_t *b)
Get length in bytes of the buffer chain.
struct _tcp_header tcp_header_t
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
static int ip4_is_fragment(const ip4_header_t *i)
u32 ip4_fib_table_get_index_for_sw_if_index(u32 sw_if_index)
#define static_always_inline
static uword ip4_header_checksum_is_valid(ip4_header_t *i)
#define vlib_prefetch_buffer_header(b, type)
Prefetch buffer metadata.
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
#define NAT_REASS_FLAG_ED_DONT_TRANSLATE
static void * ip4_next_header(ip4_header_t *i)
#define foreach_snat_out2in_error
static uword nat44_out2in_reass_node_fn(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
static u8 maximum_sessions_exceeded(snat_main_t *sm, u32 thread_index)
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.
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.
static void nat44_delete_session(snat_main_t *sm, snat_session_t *ses, u32 thread_index)
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.
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
snat_static_mapping_t * static_mappings
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
void snat_ipfix_logging_nat44_ses_delete(u32 src_ip, u32 nat_src_ip, snat_protocol_t snat_proto, u16 src_port, u16 nat_src_port, u32 vrf_id)
Generate NAT44 session delete event.
void snat_ipfix_logging_nat44_ses_create(u32 src_ip, u32 nat_src_ip, snat_protocol_t snat_proto, u16 src_port, u16 nat_src_port, u32 vrf_id)
Generate NAT44 session create event.
clib_bihash_8_8_t static_mapping_by_external
vl_api_address_union_t src_address
#define vlib_validate_buffer_enqueue_x2(vm, node, next_index, to_next, n_left_to_next, bi0, bi1, next0, next1)
Finish enqueueing two buffers forward in the graph.
vlib_node_registration_t snat_out2in_node
(constructor) VLIB_REGISTER_NODE (snat_out2in_node)
#define vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next, n_left_to_next, bi0, next0)
Finish enqueueing one buffer forward in the graph.
#define vlib_get_next_frame(vm, node, next_index, vectors, n_vectors_left)
Get pointer to next frame vector data by (vlib_node_runtime_t, next_index).
#define nat_log_notice(...)
vlib_error_t error
Error code for buffers to be enqueued to error handler.
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
The fine-grained event logger allows lightweight, thread-safe event logging at minimum cost...
static void nat44_delete_user_with_no_session(snat_main_t *sm, snat_user_t *u, u32 thread_index)
#define VLIB_REGISTER_NODE(x,...)
u8 nat_reass_is_drop_frag(u8 is_ip6)
Get status of virtual fragmentation reassembly.
#define CLIB_PREFETCH(addr, size, type)
#define vec_free(V)
Free vector's memory (no header).
static_always_inline void vnet_feature_next(u32 *next0, vlib_buffer_t *b0)
void icmp4_error_set_vnet_buffer(vlib_buffer_t *b, u8 type, u8 code, u32 data)
#define clib_memcpy(a, b, c)
static void nat44_session_update_counters(snat_session_t *s, f64 now, uword bytes)
8 octet key, 8 octet key value pair
void vlib_put_next_frame(vlib_main_t *vm, vlib_node_runtime_t *r, u32 next_index, u32 n_vectors_left)
Release pointer to next frame vector data.
u16 cached_next_index
Next frame index that vector arguments were last enqueued to last time this node ran.
snat_icmp_match_function_t * icmp_match_out2in_cb
static void nat44_session_update_lru(snat_main_t *sm, snat_session_t *s, u32 thread_index)
Per-user LRU list maintenance.
vlib_node_registration_t nat44_out2in_reass_node
(constructor) VLIB_REGISTER_NODE (nat44_out2in_reass_node)
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.
static void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
#define vec_elt(v, i)
Get vector value at index i.
struct _vlib_node_registration vlib_node_registration_t
format_function_t format_nat44_reass_trace
static int ip4_is_first_fragment(const ip4_header_t *i)
static u32 ip_proto_to_snat_proto(u8 ip_proto)
The NAT inline functions.
static void user_session_increment(snat_main_t *sm, snat_user_t *u, u8 is_static)
static snat_session_t * create_session_for_static_mapping(snat_main_t *sm, vlib_buffer_t *b0, snat_session_key_t in2out, snat_session_key_t out2in, vlib_node_runtime_t *node, u32 thread_index, f64 now)
Create session for static mapping.
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
static char * snat_out2in_error_strings[]
static int nat_out2in_sm_unknown_proto(snat_main_t *sm, vlib_buffer_t *b, ip4_header_t *ip, u32 rx_fib_index)
snat_main_per_thread_data_t * per_thread_data
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
#define ip_csum_update(sum, old, new, type, field)
static u8 is_interface_addr(snat_main_t *sm, vlib_node_runtime_t *node, u32 sw_if_index0, u32 ip4_addr)
static_always_inline snat_out2in_error_t icmp_get_key(ip4_header_t *ip0, snat_session_key_t *p_key0)
static u8 * format_snat_out2in_trace(u8 *s, va_list *args)
nat_reass_ip4_t * nat_ip4_reass_find_or_create(ip4_address_t src, ip4_address_t dst, u16 frag_id, u8 proto, u8 reset_timeout, u32 **bi_to_drop)
Find or create reassembly.
snat_address_t * addresses
static uword snat_out2in_fast_node_fn(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
#define SNAT_SESSION_FLAG_STATIC_MAPPING
static uword snat_out2in_node_fn(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
int nat_ip4_reass_add_fragment(nat_reass_ip4_t *reass, u32 bi, u32 **bi_to_drop)
Cache fragment.
u16 flags
Copy of main node flags.
static void nat_send_all_to_node(vlib_main_t *vm, u32 *bi_vector, vlib_node_runtime_t *node, vlib_error_t *error, u32 next)
static int ip4_header_bytes(const ip4_header_t *i)
void nat_ip4_reass_get_frags(nat_reass_ip4_t *reass, u32 **bi)
Get cached fragments.
NAT plugin virtual fragmentation reassembly.
#define VLIB_NODE_FLAG_TRACE
#define CLIB_CACHE_LINE_BYTES
u32 flags
buffer flags: VLIB_BUFFER_FREE_LIST_INDEX_MASK: bits used to store free list index, VLIB_BUFFER_IS_TRACED: trace this buffer.
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)
int nat44_i2o_is_idle_session_cb(clib_bihash_kv_8_8_t *kv, void *arg)
snat_session_t * sessions
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
static u16 ip_csum_fold(ip_csum_t c)
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.