2 * Copyright (c) 2018 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.
17 * @brief NAT44 endpoint-dependent outside to inside network translation
20 #include <vlib/vlib.h>
21 #include <vnet/vnet.h>
22 #include <vnet/pg/pg.h>
23 #include <vnet/ip/ip.h>
24 #include <vnet/ethernet/ethernet.h>
25 #include <vnet/fib/ip4_fib.h>
26 #include <vnet/udp/udp.h>
27 #include <vppinfra/error.h>
29 #include <nat/nat_ipfix_logging.h>
30 #include <nat/nat_inlines.h>
31 #include <nat/nat44/inlines.h>
32 #include <nat/nat_syslog.h>
33 #include <nat/nat_ha.h>
35 static char *nat_out2in_ed_error_strings[] = {
36 #define _(sym,string) string,
37 foreach_nat_out2in_ed_error
47 } nat44_ed_out2in_trace_t;
50 format_nat44_ed_out2in_trace (u8 * s, va_list * args)
52 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
53 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
54 nat44_ed_out2in_trace_t *t = va_arg (*args, nat44_ed_out2in_trace_t *);
58 t->is_slow_path ? "NAT44_OUT2IN_ED_SLOW_PATH" :
59 "NAT44_OUT2IN_ED_FAST_PATH";
61 s = format (s, "%s: sw_if_index %d, next index %d, session %d", tag,
62 t->sw_if_index, t->next_index, t->session_index);
68 icmp_out2in_ed_slow_path (snat_main_t * sm, vlib_buffer_t * b0,
69 ip4_header_t * ip0, icmp46_header_t * icmp0,
70 u32 sw_if_index0, u32 rx_fib_index0,
71 vlib_node_runtime_t * node, u32 next0, f64 now,
72 u32 thread_index, snat_session_t ** p_s0)
74 next0 = icmp_out2in (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
75 next0, thread_index, p_s0, 0);
76 snat_session_t *s0 = *p_s0;
77 if (PREDICT_TRUE (next0 != NAT_NEXT_DROP && s0))
80 nat44_session_update_counters (s0, now,
81 vlib_buffer_length_in_chain
82 (sm->vlib_main, b0), thread_index);
83 /* Per-user LRU list maintenance */
84 nat44_session_update_lru (sm, s0, thread_index);
89 #ifndef CLIB_MARCH_VARIANT
91 nat44_o2i_ed_is_idle_session_cb (clib_bihash_kv_16_8_t * kv, void *arg)
93 snat_main_t *sm = &snat_main;
94 nat44_is_idle_session_ctx_t *ctx = arg;
96 u64 sess_timeout_time;
97 nat_ed_ses_key_t ed_key;
98 clib_bihash_kv_16_8_t ed_kv;
101 snat_session_key_t key;
102 snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
105 s = pool_elt_at_index (tsm->sessions, kv->value);
106 sess_timeout_time = s->last_heard + (f64) nat44_session_get_timeout (sm, s);
107 if (ctx->now >= sess_timeout_time)
109 ed_key.l_addr = s->in2out.addr;
110 ed_key.r_addr = s->ext_host_addr;
111 ed_key.fib_index = s->in2out.fib_index;
112 if (snat_is_unk_proto_session (s))
114 ed_key.proto = s->in2out.port;
120 ed_key.proto = snat_proto_to_ip_proto (s->in2out.protocol);
121 ed_key.l_port = s->in2out.port;
122 ed_key.r_port = s->ext_host_port;
124 if (is_twice_nat_session (s))
126 ed_key.r_addr = s->ext_host_nat_addr;
127 ed_key.r_port = s->ext_host_nat_port;
129 ed_kv.key[0] = ed_key.as_u64[0];
130 ed_kv.key[1] = ed_key.as_u64[1];
131 if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &ed_kv, 0))
132 nat_elog_warn ("in2out_ed key del failed");
134 if (snat_is_unk_proto_session (s))
137 snat_ipfix_logging_nat44_ses_delete (ctx->thread_index,
138 s->in2out.addr.as_u32,
139 s->out2in.addr.as_u32,
143 s->in2out.fib_index);
145 nat_syslog_nat44_sdel (s->user_index, s->in2out.fib_index,
146 &s->in2out.addr, s->in2out.port,
147 &s->ext_host_nat_addr, s->ext_host_nat_port,
148 &s->out2in.addr, s->out2in.port,
149 &s->ext_host_addr, s->ext_host_port,
150 s->in2out.protocol, is_twice_nat_session (s));
152 nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
153 s->ext_host_port, s->out2in.protocol, s->out2in.fib_index,
156 if (is_twice_nat_session (s))
158 for (i = 0; i < vec_len (sm->twice_nat_addresses); i++)
160 key.protocol = s->in2out.protocol;
161 key.port = s->ext_host_nat_port;
162 a = sm->twice_nat_addresses + i;
163 if (a->addr.as_u32 == s->ext_host_nat_addr.as_u32)
165 snat_free_outside_address_and_port (sm->twice_nat_addresses,
173 if (snat_is_session_static (s))
176 snat_free_outside_address_and_port (sm->addresses, ctx->thread_index,
179 nat44_ed_delete_session (sm, s, ctx->thread_index, 1);
187 static snat_session_t *
188 create_session_for_static_mapping_ed (snat_main_t * sm,
190 snat_session_key_t l_key,
191 snat_session_key_t e_key,
192 vlib_node_runtime_t * node,
194 twice_nat_type_t twice_nat,
195 lb_nat_type_t lb_nat, f64 now)
200 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
201 clib_bihash_kv_16_8_t kv;
202 snat_session_key_t eh_key;
203 nat44_is_idle_session_ctx_t ctx;
205 if (PREDICT_FALSE (nat44_maximum_sessions_exceeded (sm, thread_index)))
207 b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_SESSIONS_EXCEEDED];
208 nat_elog_notice ("maximum sessions exceeded");
212 s = nat_ed_session_alloc (sm, thread_index, now);
215 b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_USER_SESS_EXCEEDED];
216 nat_elog_warn ("create NAT session failed");
220 ip = vlib_buffer_get_current (b);
221 udp = ip4_next_header (ip);
223 s->ext_host_addr.as_u32 = ip->src_address.as_u32;
224 s->ext_host_port = e_key.protocol == SNAT_PROTOCOL_ICMP ? 0 : udp->src_port;
225 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
227 s->flags |= SNAT_SESSION_FLAG_LOAD_BALANCING;
228 if (lb_nat == AFFINITY_LB_NAT)
229 s->flags |= SNAT_SESSION_FLAG_AFFINITY;
230 s->flags |= SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT;
233 s->in2out.protocol = s->out2in.protocol;
235 /* Add to lookup tables */
236 make_ed_kv (&kv, &e_key.addr, &s->ext_host_addr, ip->protocol,
237 e_key.fib_index, e_key.port, s->ext_host_port);
238 kv.value = s - tsm->sessions;
240 ctx.thread_index = thread_index;
241 if (clib_bihash_add_or_overwrite_stale_16_8 (&tsm->out2in_ed, &kv,
242 nat44_o2i_ed_is_idle_session_cb,
244 nat_elog_notice ("out2in-ed key add failed");
246 if (twice_nat == TWICE_NAT || (twice_nat == TWICE_NAT_SELF &&
247 ip->src_address.as_u32 == l_key.addr.as_u32))
249 eh_key.protocol = e_key.protocol;
250 if (snat_alloc_outside_address_and_port (sm->twice_nat_addresses, 0,
251 thread_index, &eh_key,
253 tsm->snat_thread_index))
255 b->error = node->errors[NAT_OUT2IN_ED_ERROR_OUT_OF_PORTS];
256 nat44_ed_delete_session (sm, s, thread_index, 1);
257 if (clib_bihash_add_del_16_8 (&tsm->out2in_ed, &kv, 0))
258 nat_elog_notice ("out2in-ed key del failed");
261 s->ext_host_nat_addr.as_u32 = eh_key.addr.as_u32;
262 s->ext_host_nat_port = eh_key.port;
263 s->flags |= SNAT_SESSION_FLAG_TWICE_NAT;
264 make_ed_kv (&kv, &l_key.addr, &s->ext_host_nat_addr, ip->protocol,
265 l_key.fib_index, l_key.port, s->ext_host_nat_port);
269 make_ed_kv (&kv, &l_key.addr, &s->ext_host_addr, ip->protocol,
270 l_key.fib_index, l_key.port, s->ext_host_port);
272 kv.value = s - tsm->sessions;
273 if (clib_bihash_add_or_overwrite_stale_16_8 (&tsm->in2out_ed, &kv,
274 nat44_i2o_ed_is_idle_session_cb,
276 nat_elog_notice ("in2out-ed key add failed");
278 snat_ipfix_logging_nat44_ses_create (thread_index,
279 s->in2out.addr.as_u32,
280 s->out2in.addr.as_u32,
283 s->out2in.port, s->in2out.fib_index);
285 nat_syslog_nat44_sadd (s->user_index, s->in2out.fib_index,
286 &s->in2out.addr, s->in2out.port,
287 &s->ext_host_nat_addr, s->ext_host_nat_port,
288 &s->out2in.addr, s->out2in.port,
289 &s->ext_host_addr, s->ext_host_port,
290 s->in2out.protocol, is_twice_nat_session (s));
292 nat_ha_sadd (&s->in2out.addr, s->in2out.port, &s->out2in.addr,
293 s->out2in.port, &s->ext_host_addr, s->ext_host_port,
294 &s->ext_host_nat_addr, s->ext_host_nat_port,
295 s->in2out.protocol, s->in2out.fib_index, s->flags,
302 next_src_nat (snat_main_t * sm, ip4_header_t * ip, u8 proto, u16 src_port,
303 u16 dst_port, u32 thread_index, u32 rx_fib_index)
305 clib_bihash_kv_16_8_t kv, value;
306 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
308 make_ed_kv (&kv, &ip->src_address, &ip->dst_address, proto,
309 rx_fib_index, src_port, dst_port);
310 if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
317 create_bypass_for_fwd (snat_main_t * sm, vlib_buffer_t * b, ip4_header_t * ip,
318 u32 rx_fib_index, u32 thread_index)
320 nat_ed_ses_key_t key;
321 clib_bihash_kv_16_8_t kv, value;
323 snat_session_t *s = 0;
324 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
325 f64 now = vlib_time_now (sm->vlib_main);
327 if (ip->protocol == IP_PROTOCOL_ICMP)
329 if (get_icmp_o2i_ed_key (b, ip, &key))
332 else if (ip->protocol == IP_PROTOCOL_UDP || ip->protocol == IP_PROTOCOL_TCP)
334 udp = ip4_next_header (ip);
335 key.r_addr = ip->src_address;
336 key.l_addr = ip->dst_address;
337 key.proto = ip->protocol;
338 key.l_port = udp->dst_port;
339 key.r_port = udp->src_port;
343 key.r_addr = ip->src_address;
344 key.l_addr = ip->dst_address;
345 key.proto = ip->protocol;
346 key.l_port = key.r_port = 0;
349 kv.key[0] = key.as_u64[0];
350 kv.key[1] = key.as_u64[1];
352 if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
354 s = pool_elt_at_index (tsm->sessions, value.value);
360 if (PREDICT_FALSE (nat44_maximum_sessions_exceeded (sm, thread_index)))
363 s = nat_ed_session_alloc (sm, thread_index, now);
366 nat_elog_warn ("create NAT session failed");
370 proto = ip_proto_to_snat_proto (key.proto);
372 s->ext_host_addr = key.r_addr;
373 s->ext_host_port = key.r_port;
374 s->flags |= SNAT_SESSION_FLAG_FWD_BYPASS;
375 s->out2in.addr = key.l_addr;
376 s->out2in.port = key.l_port;
377 s->out2in.protocol = proto;
380 s->flags |= SNAT_SESSION_FLAG_UNKNOWN_PROTO;
381 s->out2in.port = ip->protocol;
383 s->out2in.fib_index = 0;
384 s->in2out = s->out2in;
386 kv.value = s - tsm->sessions;
387 if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &kv, 1))
388 nat_elog_notice ("in2out_ed key add failed");
391 if (ip->protocol == IP_PROTOCOL_TCP)
393 tcp_header_t *tcp = ip4_next_header (ip);
394 if (nat44_set_tcp_session_state_o2i
395 (sm, now, s, tcp->flags, tcp->ack_number, tcp->seq_number,
401 nat44_session_update_counters (s, now, 0, thread_index);
402 /* Per-user LRU list maintenance */
403 nat44_session_update_lru (sm, s, thread_index);
407 create_bypass_for_fwd_worker (snat_main_t * sm, vlib_buffer_t * b,
408 ip4_header_t * ip, u32 rx_fib_index)
410 ip4_header_t ip_wkr = {
411 .src_address = ip->dst_address,
413 u32 thread_index = sm->worker_in2out_cb (&ip_wkr, rx_fib_index, 0);
415 create_bypass_for_fwd (sm, b, ip, rx_fib_index, thread_index);
418 #ifndef CLIB_MARCH_VARIANT
420 icmp_match_out2in_ed (snat_main_t * sm, vlib_node_runtime_t * node,
421 u32 thread_index, vlib_buffer_t * b, ip4_header_t * ip,
422 u8 * p_proto, snat_session_key_t * p_value,
423 u8 * p_dont_translate, void *d, void *e)
425 u32 next = ~0, sw_if_index, rx_fib_index;
426 nat_ed_ses_key_t key;
427 clib_bihash_kv_16_8_t kv, value;
428 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
429 snat_session_t *s = 0;
430 u8 dont_translate = 0, is_addr_only, identity_nat;
431 snat_session_key_t e_key, l_key;
433 sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
434 rx_fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
436 if (get_icmp_o2i_ed_key (b, ip, &key))
438 b->error = node->errors[NAT_OUT2IN_ED_ERROR_UNSUPPORTED_PROTOCOL];
439 next = NAT_NEXT_DROP;
442 key.fib_index = rx_fib_index;
443 kv.key[0] = key.as_u64[0];
444 kv.key[1] = key.as_u64[1];
446 if (clib_bihash_search_16_8 (&tsm->out2in_ed, &kv, &value))
448 /* Try to match static mapping */
449 e_key.addr = ip->dst_address;
450 e_key.port = key.l_port;
451 e_key.protocol = ip_proto_to_snat_proto (key.proto);
452 e_key.fib_index = rx_fib_index;
453 if (snat_static_mapping_match
454 (sm, e_key, &l_key, 1, &is_addr_only, 0, 0, 0, &identity_nat))
456 if (!sm->forwarding_enabled)
458 /* Don't NAT packet aimed at the intfc address */
459 if (PREDICT_FALSE (is_interface_addr (sm, node, sw_if_index,
460 ip->dst_address.as_u32)))
465 b->error = node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
466 next = NAT_NEXT_DROP;
472 if (next_src_nat (sm, ip, key.proto, key.l_port, key.r_port,
473 thread_index, rx_fib_index))
475 next = NAT_NEXT_IN2OUT_ED_FAST_PATH;
478 if (sm->num_workers > 1)
479 create_bypass_for_fwd_worker (sm, b, ip, rx_fib_index);
481 create_bypass_for_fwd (sm, b, ip, rx_fib_index, thread_index);
487 (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags !=
489 && (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags !=
490 ICMP4_echo_request || !is_addr_only)))
492 b->error = node->errors[NAT_OUT2IN_ED_ERROR_BAD_ICMP_TYPE];
493 next = NAT_NEXT_DROP;
497 if (PREDICT_FALSE (identity_nat))
503 /* Create session initiated by host from external network */
504 s = create_session_for_static_mapping_ed (sm, b, l_key, e_key, node,
511 next = NAT_NEXT_DROP;
518 (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags !=
520 && vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags !=
522 && !icmp_type_is_error_message (vnet_buffer (b)->ip.
523 reass.icmp_type_or_tcp_flags)))
525 b->error = node->errors[NAT_OUT2IN_ED_ERROR_BAD_ICMP_TYPE];
526 next = NAT_NEXT_DROP;
530 s = pool_elt_at_index (tsm->sessions, value.value);
533 *p_proto = ip_proto_to_snat_proto (key.proto);
536 *p_value = s->in2out;
537 *p_dont_translate = dont_translate;
539 *(snat_session_t **) d = s;
544 static snat_session_t *
545 nat44_ed_out2in_unknown_proto (snat_main_t * sm,
551 vlib_main_t * vm, vlib_node_runtime_t * node)
553 clib_bihash_kv_8_8_t kv, value;
554 clib_bihash_kv_16_8_t s_kv, s_value;
555 snat_static_mapping_t *m;
556 u32 old_addr, new_addr;
559 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
561 old_addr = ip->dst_address.as_u32;
563 make_ed_kv (&s_kv, &ip->dst_address, &ip->src_address, ip->protocol,
566 if (!clib_bihash_search_16_8 (&tsm->out2in_ed, &s_kv, &s_value))
568 s = pool_elt_at_index (tsm->sessions, s_value.value);
569 new_addr = ip->dst_address.as_u32 = s->in2out.addr.as_u32;
573 if (PREDICT_FALSE (nat44_maximum_sessions_exceeded (sm, thread_index)))
575 b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_SESSIONS_EXCEEDED];
576 nat_elog_notice ("maximum sessions exceeded");
580 make_sm_kv (&kv, &ip->dst_address, 0, 0, 0);
581 if (clib_bihash_search_8_8
582 (&sm->static_mapping_by_external, &kv, &value))
584 b->error = node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
588 m = pool_elt_at_index (sm->static_mappings, value.value);
590 new_addr = ip->dst_address.as_u32 = m->local_addr.as_u32;
592 /* Create a new session */
593 s = nat_ed_session_alloc (sm, thread_index, now);
596 b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_USER_SESS_EXCEEDED];
597 nat_elog_warn ("create NAT session failed");
601 s->ext_host_addr.as_u32 = ip->src_address.as_u32;
602 s->flags |= SNAT_SESSION_FLAG_UNKNOWN_PROTO;
603 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
604 s->flags |= SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT;
605 s->out2in.addr.as_u32 = old_addr;
606 s->out2in.fib_index = rx_fib_index;
607 s->in2out.addr.as_u32 = new_addr;
608 s->in2out.fib_index = m->fib_index;
609 s->in2out.port = s->out2in.port = ip->protocol;
611 /* Add to lookup tables */
612 s_kv.value = s - tsm->sessions;
613 if (clib_bihash_add_del_16_8 (&tsm->out2in_ed, &s_kv, 1))
614 nat_elog_notice ("out2in key add failed");
616 make_ed_kv (&s_kv, &ip->dst_address, &ip->src_address, ip->protocol,
618 s_kv.value = s - tsm->sessions;
619 if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &s_kv, 1))
620 nat_elog_notice ("in2out key add failed");
623 /* Update IP checksum */
625 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, dst_address);
626 ip->checksum = ip_csum_fold (sum);
628 vnet_buffer (b)->sw_if_index[VLIB_TX] = s->in2out.fib_index;
631 nat44_session_update_counters (s, now, vlib_buffer_length_in_chain (vm, b),
633 /* Per-user LRU list maintenance */
634 nat44_session_update_lru (sm, s, thread_index);
640 nat44_ed_out2in_fast_path_node_fn_inline (vlib_main_t * vm,
641 vlib_node_runtime_t * node,
642 vlib_frame_t * frame)
644 u32 n_left_from, *from, *to_next, pkts_processed = 0, stats_node_index;
645 nat_next_t next_index;
646 snat_main_t *sm = &snat_main;
647 f64 now = vlib_time_now (vm);
648 u32 thread_index = vm->thread_index;
649 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
650 u32 tcp_packets = 0, udp_packets = 0, icmp_packets = 0, other_packets =
653 stats_node_index = sm->ed_out2in_node_index;
655 from = vlib_frame_vector_args (frame);
656 n_left_from = frame->n_vectors;
657 next_index = node->cached_next_index;
659 while (n_left_from > 0)
663 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
665 while (n_left_from > 0 && n_left_to_next > 0)
669 u32 next0, sw_if_index0, rx_fib_index0, proto0, old_addr0,
671 u16 old_port0, new_port0;
675 snat_session_t *s0 = 0;
676 clib_bihash_kv_16_8_t kv0, value0;
679 /* speculatively enqueue b0 to the current next frame */
687 b0 = vlib_get_buffer (vm, bi0);
688 next0 = vnet_buffer2 (b0)->nat.arc_next;
690 vnet_buffer (b0)->snat.flags = 0;
691 ip0 = vlib_buffer_get_current (b0);
693 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
695 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
698 if (PREDICT_FALSE (ip0->ttl == 1))
700 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
701 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
702 ICMP4_time_exceeded_ttl_exceeded_in_transit,
704 next0 = NAT_NEXT_ICMP_ERROR;
708 udp0 = ip4_next_header (ip0);
709 tcp0 = (tcp_header_t *) udp0;
710 proto0 = ip_proto_to_snat_proto (ip0->protocol);
712 if (PREDICT_FALSE (proto0 == ~0))
714 next0 = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
718 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
720 next0 = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
724 make_ed_kv (&kv0, &ip0->dst_address, &ip0->src_address,
725 ip0->protocol, rx_fib_index0,
726 vnet_buffer (b0)->ip.reass.l4_dst_port,
727 vnet_buffer (b0)->ip.reass.l4_src_port);
729 if (clib_bihash_search_16_8 (&tsm->out2in_ed, &kv0, &value0))
731 next0 = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
734 s0 = pool_elt_at_index (tsm->sessions, value0.value);
736 if (s0->tcp_close_timestamp)
738 if (now >= s0->tcp_close_timestamp)
740 // session is closed, go slow path
741 next0 = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
745 // session in transitory timeout, drop
746 b0->error = node->errors[NAT_OUT2IN_ED_ERROR_TCP_CLOSED];
747 next0 = NAT_NEXT_DROP;
752 // drop if session expired
753 u64 sess_timeout_time;
754 sess_timeout_time = s0->last_heard +
755 (f64) nat44_session_get_timeout (sm, s0);
756 if (now >= sess_timeout_time)
758 // session is closed, go slow path
759 nat_free_session_data (sm, s0, thread_index, 0);
760 nat44_ed_delete_session (sm, s0, thread_index, 1);
761 next0 = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
766 old_addr0 = ip0->dst_address.as_u32;
767 new_addr0 = ip0->dst_address.as_u32 = s0->in2out.addr.as_u32;
768 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
770 sum0 = ip0->checksum;
771 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
773 if (PREDICT_FALSE (is_twice_nat_session (s0)))
774 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
775 s0->ext_host_nat_addr.as_u32, ip4_header_t,
777 ip0->checksum = ip_csum_fold (sum0);
779 old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
781 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
783 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
785 new_port0 = udp0->dst_port = s0->in2out.port;
786 sum0 = tcp0->checksum;
788 ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
791 ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
793 if (is_twice_nat_session (s0))
795 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
796 s0->ext_host_nat_addr.as_u32,
797 ip4_header_t, dst_address);
799 ip_csum_update (sum0,
800 vnet_buffer (b0)->ip.
802 s0->ext_host_nat_port, ip4_header_t,
804 tcp0->src_port = s0->ext_host_nat_port;
805 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
807 tcp0->checksum = ip_csum_fold (sum0);
810 if (nat44_set_tcp_session_state_o2i
812 vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags,
813 vnet_buffer (b0)->ip.reass.tcp_ack_number,
814 vnet_buffer (b0)->ip.reass.tcp_seq_number, thread_index))
817 else if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment
820 new_port0 = udp0->dst_port = s0->in2out.port;
821 sum0 = udp0->checksum;
822 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
824 sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
826 if (PREDICT_FALSE (is_twice_nat_session (s0)))
828 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
829 s0->ext_host_nat_addr.as_u32,
830 ip4_header_t, dst_address);
832 ip_csum_update (sum0,
833 vnet_buffer (b0)->ip.reass.l4_src_port,
834 s0->ext_host_nat_port, ip4_header_t,
836 udp0->src_port = s0->ext_host_nat_port;
837 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
839 udp0->checksum = ip_csum_fold (sum0);
844 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
846 new_port0 = udp0->dst_port = s0->in2out.port;
847 if (PREDICT_FALSE (is_twice_nat_session (s0)))
849 udp0->src_port = s0->ext_host_nat_port;
850 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
857 nat44_session_update_counters (s0, now,
858 vlib_buffer_length_in_chain (vm, b0),
860 /* Per-user LRU list maintenance */
861 nat44_session_update_lru (sm, s0, thread_index);
864 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
865 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
867 nat44_ed_out2in_trace_t *t =
868 vlib_add_trace (vm, node, b0, sizeof (*t));
869 t->sw_if_index = sw_if_index0;
870 t->next_index = next0;
874 t->session_index = s0 - tsm->sessions;
876 t->session_index = ~0;
879 pkts_processed += next0 == vnet_buffer2 (b0)->nat.arc_next;
880 /* verify speculative enqueue, maybe switch current next frame */
881 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
882 to_next, n_left_to_next,
886 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
889 vlib_node_increment_counter (vm, stats_node_index,
890 NAT_OUT2IN_ED_ERROR_OUT2IN_PACKETS,
892 vlib_node_increment_counter (vm, stats_node_index,
893 NAT_OUT2IN_ED_ERROR_TCP_PACKETS, tcp_packets);
894 vlib_node_increment_counter (vm, stats_node_index,
895 NAT_OUT2IN_ED_ERROR_UDP_PACKETS, udp_packets);
896 vlib_node_increment_counter (vm, stats_node_index,
897 NAT_OUT2IN_ED_ERROR_ICMP_PACKETS,
899 vlib_node_increment_counter (vm, stats_node_index,
900 NAT_OUT2IN_ED_ERROR_OTHER_PACKETS,
902 vlib_node_increment_counter (vm, stats_node_index,
903 NAT_OUT2IN_ED_ERROR_FRAGMENTS, fragments);
904 return frame->n_vectors;
908 nat44_ed_out2in_slow_path_node_fn_inline (vlib_main_t * vm,
909 vlib_node_runtime_t * node,
910 vlib_frame_t * frame)
912 u32 n_left_from, *from, *to_next, pkts_processed = 0, stats_node_index;
913 nat_next_t next_index;
914 snat_main_t *sm = &snat_main;
915 f64 now = vlib_time_now (vm);
916 u32 thread_index = vm->thread_index;
917 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
918 u32 tcp_packets = 0, udp_packets = 0, icmp_packets = 0, other_packets =
921 stats_node_index = sm->ed_out2in_slowpath_node_index;
923 from = vlib_frame_vector_args (frame);
924 n_left_from = frame->n_vectors;
925 next_index = node->cached_next_index;
927 while (n_left_from > 0)
931 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
933 while (n_left_from > 0 && n_left_to_next > 0)
937 u32 next0, sw_if_index0, rx_fib_index0, proto0, old_addr0,
939 u16 old_port0, new_port0;
943 icmp46_header_t *icmp0;
944 snat_session_t *s0 = 0;
945 clib_bihash_kv_16_8_t kv0, value0;
947 snat_session_key_t e_key0, l_key0;
948 lb_nat_type_t lb_nat0;
949 twice_nat_type_t twice_nat0;
952 /* speculatively enqueue b0 to the current next frame */
960 b0 = vlib_get_buffer (vm, bi0);
961 next0 = vnet_buffer2 (b0)->nat.arc_next;
963 vnet_buffer (b0)->snat.flags = 0;
964 ip0 = vlib_buffer_get_current (b0);
966 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
968 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
971 if (PREDICT_FALSE (ip0->ttl == 1))
973 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
974 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
975 ICMP4_time_exceeded_ttl_exceeded_in_transit,
977 next0 = NAT_NEXT_ICMP_ERROR;
981 udp0 = ip4_next_header (ip0);
982 tcp0 = (tcp_header_t *) udp0;
983 icmp0 = (icmp46_header_t *) udp0;
984 proto0 = ip_proto_to_snat_proto (ip0->protocol);
986 if (PREDICT_FALSE (proto0 == ~0))
989 nat44_ed_out2in_unknown_proto (sm, b0, ip0, rx_fib_index0,
990 thread_index, now, vm, node);
991 if (!sm->forwarding_enabled)
994 next0 = NAT_NEXT_DROP;
1000 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1002 next0 = icmp_out2in_ed_slow_path
1003 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1004 next0, now, thread_index, &s0);
1009 make_ed_kv (&kv0, &ip0->dst_address, &ip0->src_address,
1010 ip0->protocol, rx_fib_index0,
1011 vnet_buffer (b0)->ip.reass.l4_dst_port,
1012 vnet_buffer (b0)->ip.reass.l4_src_port);
1015 if (!clib_bihash_search_16_8 (&tsm->out2in_ed, &kv0, &value0))
1017 s0 = pool_elt_at_index (tsm->sessions, value0.value);
1019 if (s0->tcp_close_timestamp && now >= s0->tcp_close_timestamp)
1021 nat_free_session_data (sm, s0, thread_index, 0);
1022 nat44_ed_delete_session (sm, s0, thread_index, 1);
1029 /* Try to match static mapping by external address and port,
1030 destination address and port in packet */
1031 e_key0.addr = ip0->dst_address;
1032 e_key0.port = vnet_buffer (b0)->ip.reass.l4_dst_port;
1033 e_key0.protocol = proto0;
1034 e_key0.fib_index = rx_fib_index0;
1036 if (snat_static_mapping_match (sm, e_key0, &l_key0, 1, 0,
1037 &twice_nat0, &lb_nat0,
1042 * Send DHCP packets to the ipv4 stack, or we won't
1043 * be able to use dhcp client on the outside interface
1045 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_UDP
1046 && (vnet_buffer (b0)->ip.
1047 reass.l4_dst_port ==
1048 clib_host_to_net_u16
1049 (UDP_DST_PORT_dhcp_to_client))))
1054 if (!sm->forwarding_enabled)
1057 node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
1058 next0 = NAT_NEXT_DROP;
1062 if (next_src_nat (sm, ip0, ip0->protocol,
1063 vnet_buffer (b0)->ip.
1065 vnet_buffer (b0)->ip.
1066 reass.l4_dst_port, thread_index,
1069 next0 = NAT_NEXT_IN2OUT_ED_FAST_PATH;
1072 if (sm->num_workers > 1)
1073 create_bypass_for_fwd_worker (sm, b0, ip0,
1076 create_bypass_for_fwd (sm, b0, ip0, rx_fib_index0,
1082 if (PREDICT_FALSE (identity_nat0))
1085 if ((proto0 == SNAT_PROTOCOL_TCP)
1086 && !tcp_flags_is_init (vnet_buffer (b0)->ip.
1087 reass.icmp_type_or_tcp_flags))
1089 b0->error = node->errors[NAT_OUT2IN_ED_ERROR_NON_SYN];
1090 next0 = NAT_NEXT_DROP;
1094 /* Create session initiated by host from external network */
1095 s0 = create_session_for_static_mapping_ed (sm, b0, l_key0,
1102 next0 = NAT_NEXT_DROP;
1107 old_addr0 = ip0->dst_address.as_u32;
1108 new_addr0 = ip0->dst_address.as_u32 = s0->in2out.addr.as_u32;
1109 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
1111 sum0 = ip0->checksum;
1112 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1114 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1115 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1116 s0->ext_host_nat_addr.as_u32, ip4_header_t,
1118 ip0->checksum = ip_csum_fold (sum0);
1120 old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
1122 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1124 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1126 new_port0 = udp0->dst_port = s0->in2out.port;
1127 sum0 = tcp0->checksum;
1129 ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1132 ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
1134 if (is_twice_nat_session (s0))
1136 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1137 s0->ext_host_nat_addr.as_u32,
1138 ip4_header_t, dst_address);
1140 ip_csum_update (sum0,
1141 vnet_buffer (b0)->ip.
1143 s0->ext_host_nat_port, ip4_header_t,
1145 tcp0->src_port = s0->ext_host_nat_port;
1146 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1148 tcp0->checksum = ip_csum_fold (sum0);
1151 if (nat44_set_tcp_session_state_o2i
1153 vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags,
1154 vnet_buffer (b0)->ip.reass.tcp_ack_number,
1155 vnet_buffer (b0)->ip.reass.tcp_seq_number, thread_index))
1158 else if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment
1161 new_port0 = udp0->dst_port = s0->in2out.port;
1162 sum0 = udp0->checksum;
1163 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1165 sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
1167 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1169 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1170 s0->ext_host_nat_addr.as_u32,
1171 ip4_header_t, dst_address);
1173 ip_csum_update (sum0,
1174 vnet_buffer (b0)->ip.reass.l4_src_port,
1175 s0->ext_host_nat_port, ip4_header_t,
1177 udp0->src_port = s0->ext_host_nat_port;
1178 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1180 udp0->checksum = ip_csum_fold (sum0);
1185 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1187 new_port0 = udp0->dst_port = s0->in2out.port;
1188 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1190 udp0->src_port = s0->ext_host_nat_port;
1191 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1198 nat44_session_update_counters (s0, now,
1199 vlib_buffer_length_in_chain (vm, b0),
1201 /* Per-user LRU list maintenance */
1202 nat44_session_update_lru (sm, s0, thread_index);
1205 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1206 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1208 nat44_ed_out2in_trace_t *t =
1209 vlib_add_trace (vm, node, b0, sizeof (*t));
1210 t->sw_if_index = sw_if_index0;
1211 t->next_index = next0;
1212 t->is_slow_path = 1;
1215 t->session_index = s0 - tsm->sessions;
1217 t->session_index = ~0;
1220 pkts_processed += next0 == vnet_buffer2 (b0)->nat.arc_next;
1221 /* verify speculative enqueue, maybe switch current next frame */
1222 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1223 to_next, n_left_to_next,
1227 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1230 vlib_node_increment_counter (vm, stats_node_index,
1231 NAT_OUT2IN_ED_ERROR_OUT2IN_PACKETS,
1233 vlib_node_increment_counter (vm, stats_node_index,
1234 NAT_OUT2IN_ED_ERROR_TCP_PACKETS, tcp_packets);
1235 vlib_node_increment_counter (vm, stats_node_index,
1236 NAT_OUT2IN_ED_ERROR_UDP_PACKETS, udp_packets);
1237 vlib_node_increment_counter (vm, stats_node_index,
1238 NAT_OUT2IN_ED_ERROR_ICMP_PACKETS,
1240 vlib_node_increment_counter (vm, stats_node_index,
1241 NAT_OUT2IN_ED_ERROR_OTHER_PACKETS,
1243 vlib_node_increment_counter (vm, stats_node_index,
1244 NAT_OUT2IN_ED_ERROR_FRAGMENTS, fragments);
1245 return frame->n_vectors;
1248 VLIB_NODE_FN (nat44_ed_out2in_node) (vlib_main_t * vm,
1249 vlib_node_runtime_t * node,
1250 vlib_frame_t * frame)
1252 return nat44_ed_out2in_fast_path_node_fn_inline (vm, node, frame);
1256 VLIB_REGISTER_NODE (nat44_ed_out2in_node) = {
1257 .name = "nat44-ed-out2in",
1258 .vector_size = sizeof (u32),
1259 .sibling_of = "nat-default",
1260 .format_trace = format_nat44_ed_out2in_trace,
1261 .type = VLIB_NODE_TYPE_INTERNAL,
1262 .n_errors = ARRAY_LEN(nat_out2in_ed_error_strings),
1263 .error_strings = nat_out2in_ed_error_strings,
1264 .runtime_data_bytes = sizeof (snat_runtime_t),
1268 VLIB_NODE_FN (nat44_ed_out2in_slowpath_node) (vlib_main_t * vm,
1269 vlib_node_runtime_t * node,
1270 vlib_frame_t * frame)
1272 return nat44_ed_out2in_slow_path_node_fn_inline (vm, node, frame);
1276 VLIB_REGISTER_NODE (nat44_ed_out2in_slowpath_node) = {
1277 .name = "nat44-ed-out2in-slowpath",
1278 .vector_size = sizeof (u32),
1279 .sibling_of = "nat-default",
1280 .format_trace = format_nat44_ed_out2in_trace,
1281 .type = VLIB_NODE_TYPE_INTERNAL,
1282 .n_errors = ARRAY_LEN(nat_out2in_ed_error_strings),
1283 .error_strings = nat_out2in_ed_error_strings,
1284 .runtime_data_bytes = sizeof (snat_runtime_t),
1289 format_nat_pre_trace (u8 * s, va_list * args)
1291 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1292 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1293 nat_pre_trace_t *t = va_arg (*args, nat_pre_trace_t *);
1294 return format (s, "out2in next_index %d", t->next_index);
1297 VLIB_NODE_FN (nat_pre_out2in_node) (vlib_main_t * vm,
1298 vlib_node_runtime_t * node,
1299 vlib_frame_t * frame)
1301 return nat_pre_node_fn_inline (vm, node, frame,
1302 NAT_NEXT_OUT2IN_ED_FAST_PATH);
1306 VLIB_REGISTER_NODE (nat_pre_out2in_node) = {
1307 .name = "nat-pre-out2in",
1308 .vector_size = sizeof (u32),
1309 .sibling_of = "nat-default",
1310 .format_trace = format_nat_pre_trace,
1311 .type = VLIB_NODE_TYPE_INTERNAL,
1317 * fd.io coding-style-patch-verification: ON
1320 * eval: (c-set-style "gnu")