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_delete_session (sm, s, ctx->thread_index);
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)
201 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
202 clib_bihash_kv_16_8_t kv;
203 snat_session_key_t eh_key;
204 nat44_is_idle_session_ctx_t ctx;
206 if (PREDICT_FALSE (nat44_maximum_sessions_exceeded (sm, thread_index)))
208 b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_SESSIONS_EXCEEDED];
209 nat_elog_notice ("maximum sessions exceeded");
213 u = nat_user_get_or_create (sm, &l_key.addr, l_key.fib_index, thread_index);
216 nat_elog_warn ("create NAT user failed");
220 s = nat_ed_session_alloc (sm, u, thread_index, now);
223 nat44_delete_user_with_no_session (sm, u, thread_index);
224 nat_elog_warn ("create NAT session failed");
228 ip = vlib_buffer_get_current (b);
229 udp = ip4_next_header (ip);
231 s->ext_host_addr.as_u32 = ip->src_address.as_u32;
232 s->ext_host_port = e_key.protocol == SNAT_PROTOCOL_ICMP ? 0 : udp->src_port;
233 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
235 s->flags |= SNAT_SESSION_FLAG_LOAD_BALANCING;
236 if (lb_nat == AFFINITY_LB_NAT)
237 s->flags |= SNAT_SESSION_FLAG_AFFINITY;
238 s->flags |= SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT;
241 s->in2out.protocol = s->out2in.protocol;
242 user_session_increment (sm, u, 1);
244 /* Add to lookup tables */
245 make_ed_kv (&kv, &e_key.addr, &s->ext_host_addr, ip->protocol,
246 e_key.fib_index, e_key.port, s->ext_host_port);
247 kv.value = s - tsm->sessions;
249 ctx.thread_index = thread_index;
250 if (clib_bihash_add_or_overwrite_stale_16_8 (&tsm->out2in_ed, &kv,
251 nat44_o2i_ed_is_idle_session_cb,
253 nat_elog_notice ("out2in-ed key add failed");
255 if (twice_nat == TWICE_NAT || (twice_nat == TWICE_NAT_SELF &&
256 ip->src_address.as_u32 == l_key.addr.as_u32))
258 eh_key.protocol = e_key.protocol;
259 if (snat_alloc_outside_address_and_port (sm->twice_nat_addresses, 0,
260 thread_index, &eh_key,
262 tsm->snat_thread_index))
264 b->error = node->errors[NAT_OUT2IN_ED_ERROR_OUT_OF_PORTS];
265 nat44_delete_session (sm, s, thread_index);
266 if (clib_bihash_add_del_16_8 (&tsm->out2in_ed, &kv, 0))
267 nat_elog_notice ("out2in-ed key del failed");
270 s->ext_host_nat_addr.as_u32 = eh_key.addr.as_u32;
271 s->ext_host_nat_port = eh_key.port;
272 s->flags |= SNAT_SESSION_FLAG_TWICE_NAT;
273 make_ed_kv (&kv, &l_key.addr, &s->ext_host_nat_addr, ip->protocol,
274 l_key.fib_index, l_key.port, s->ext_host_nat_port);
278 make_ed_kv (&kv, &l_key.addr, &s->ext_host_addr, ip->protocol,
279 l_key.fib_index, l_key.port, s->ext_host_port);
281 kv.value = s - tsm->sessions;
282 if (clib_bihash_add_or_overwrite_stale_16_8 (&tsm->in2out_ed, &kv,
283 nat44_i2o_ed_is_idle_session_cb,
285 nat_elog_notice ("in2out-ed key add failed");
287 snat_ipfix_logging_nat44_ses_create (thread_index,
288 s->in2out.addr.as_u32,
289 s->out2in.addr.as_u32,
292 s->out2in.port, s->in2out.fib_index);
294 nat_syslog_nat44_sadd (s->user_index, s->in2out.fib_index,
295 &s->in2out.addr, s->in2out.port,
296 &s->ext_host_nat_addr, s->ext_host_nat_port,
297 &s->out2in.addr, s->out2in.port,
298 &s->ext_host_addr, s->ext_host_port,
299 s->in2out.protocol, is_twice_nat_session (s));
301 nat_ha_sadd (&s->in2out.addr, s->in2out.port, &s->out2in.addr,
302 s->out2in.port, &s->ext_host_addr, s->ext_host_port,
303 &s->ext_host_nat_addr, s->ext_host_nat_port,
304 s->in2out.protocol, s->in2out.fib_index, s->flags,
311 next_src_nat (snat_main_t * sm, ip4_header_t * ip, u8 proto, u16 src_port,
312 u16 dst_port, u32 thread_index, u32 rx_fib_index)
314 clib_bihash_kv_16_8_t kv, value;
315 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
317 make_ed_kv (&kv, &ip->src_address, &ip->dst_address, proto,
318 rx_fib_index, src_port, dst_port);
319 if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
326 create_bypass_for_fwd (snat_main_t * sm, vlib_buffer_t * b, ip4_header_t * ip,
327 u32 rx_fib_index, u32 thread_index)
329 nat_ed_ses_key_t key;
330 clib_bihash_kv_16_8_t kv, value;
333 snat_session_t *s = 0;
334 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
335 f64 now = vlib_time_now (sm->vlib_main);
337 if (ip->protocol == IP_PROTOCOL_ICMP)
339 if (get_icmp_o2i_ed_key (b, ip, &key))
342 else if (ip->protocol == IP_PROTOCOL_UDP || ip->protocol == IP_PROTOCOL_TCP)
344 udp = ip4_next_header (ip);
345 key.r_addr = ip->src_address;
346 key.l_addr = ip->dst_address;
347 key.proto = ip->protocol;
348 key.l_port = udp->dst_port;
349 key.r_port = udp->src_port;
353 key.r_addr = ip->src_address;
354 key.l_addr = ip->dst_address;
355 key.proto = ip->protocol;
356 key.l_port = key.r_port = 0;
359 kv.key[0] = key.as_u64[0];
360 kv.key[1] = key.as_u64[1];
362 if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
364 s = pool_elt_at_index (tsm->sessions, value.value);
370 if (PREDICT_FALSE (nat44_maximum_sessions_exceeded (sm, thread_index)))
373 u = nat_user_get_or_create (sm, &ip->dst_address, sm->inside_fib_index,
377 nat_elog_warn ("create NAT user failed");
381 s = nat_ed_session_alloc (sm, u, thread_index, now);
384 nat44_delete_user_with_no_session (sm, u, thread_index);
385 nat_elog_warn ("create NAT session failed");
389 proto = ip_proto_to_snat_proto (key.proto);
391 s->ext_host_addr = key.r_addr;
392 s->ext_host_port = key.r_port;
393 s->flags |= SNAT_SESSION_FLAG_FWD_BYPASS;
394 s->out2in.addr = key.l_addr;
395 s->out2in.port = key.l_port;
396 s->out2in.protocol = proto;
399 s->flags |= SNAT_SESSION_FLAG_UNKNOWN_PROTO;
400 s->out2in.port = ip->protocol;
402 s->out2in.fib_index = 0;
403 s->in2out = s->out2in;
404 user_session_increment (sm, u, 0);
406 kv.value = s - tsm->sessions;
407 if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &kv, 1))
408 nat_elog_notice ("in2out_ed key add failed");
411 if (ip->protocol == IP_PROTOCOL_TCP)
413 tcp_header_t *tcp = ip4_next_header (ip);
414 if (nat44_set_tcp_session_state_o2i
415 (sm, s, tcp->flags, tcp->ack_number, tcp->seq_number, thread_index))
420 nat44_session_update_counters (s, now, 0, thread_index);
421 /* Per-user LRU list maintenance */
422 nat44_session_update_lru (sm, s, thread_index);
426 create_bypass_for_fwd_worker (snat_main_t * sm, vlib_buffer_t * b,
427 ip4_header_t * ip, u32 rx_fib_index)
429 ip4_header_t ip_wkr = {
430 .src_address = ip->dst_address,
432 u32 thread_index = sm->worker_in2out_cb (&ip_wkr, rx_fib_index, 0);
434 create_bypass_for_fwd (sm, b, ip, rx_fib_index, thread_index);
437 #ifndef CLIB_MARCH_VARIANT
439 icmp_match_out2in_ed (snat_main_t * sm, vlib_node_runtime_t * node,
440 u32 thread_index, vlib_buffer_t * b, ip4_header_t * ip,
441 u8 * p_proto, snat_session_key_t * p_value,
442 u8 * p_dont_translate, void *d, void *e)
444 u32 next = ~0, sw_if_index, rx_fib_index;
445 nat_ed_ses_key_t key;
446 clib_bihash_kv_16_8_t kv, value;
447 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
448 snat_session_t *s = 0;
449 u8 dont_translate = 0, is_addr_only, identity_nat;
450 snat_session_key_t e_key, l_key;
452 sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
453 rx_fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
455 if (get_icmp_o2i_ed_key (b, ip, &key))
457 b->error = node->errors[NAT_OUT2IN_ED_ERROR_UNSUPPORTED_PROTOCOL];
458 next = NAT_NEXT_DROP;
461 key.fib_index = rx_fib_index;
462 kv.key[0] = key.as_u64[0];
463 kv.key[1] = key.as_u64[1];
465 if (clib_bihash_search_16_8 (&tsm->out2in_ed, &kv, &value))
467 /* Try to match static mapping */
468 e_key.addr = ip->dst_address;
469 e_key.port = key.l_port;
470 e_key.protocol = ip_proto_to_snat_proto (key.proto);
471 e_key.fib_index = rx_fib_index;
472 if (snat_static_mapping_match
473 (sm, e_key, &l_key, 1, &is_addr_only, 0, 0, 0, &identity_nat))
475 if (!sm->forwarding_enabled)
477 /* Don't NAT packet aimed at the intfc address */
478 if (PREDICT_FALSE (is_interface_addr (sm, node, sw_if_index,
479 ip->dst_address.as_u32)))
484 b->error = node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
485 next = NAT_NEXT_DROP;
491 if (next_src_nat (sm, ip, key.proto, key.l_port, key.r_port,
492 thread_index, rx_fib_index))
494 next = NAT_NEXT_IN2OUT_ED_FAST_PATH;
497 if (sm->num_workers > 1)
498 create_bypass_for_fwd_worker (sm, b, ip, rx_fib_index);
500 create_bypass_for_fwd (sm, b, ip, rx_fib_index, thread_index);
506 (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags !=
508 && (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags !=
509 ICMP4_echo_request || !is_addr_only)))
511 b->error = node->errors[NAT_OUT2IN_ED_ERROR_BAD_ICMP_TYPE];
512 next = NAT_NEXT_DROP;
516 if (PREDICT_FALSE (identity_nat))
522 /* Create session initiated by host from external network */
523 s = create_session_for_static_mapping_ed (sm, b, l_key, e_key, node,
530 next = NAT_NEXT_DROP;
537 (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags !=
539 && vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags !=
541 && !icmp_type_is_error_message (vnet_buffer (b)->ip.
542 reass.icmp_type_or_tcp_flags)))
544 b->error = node->errors[NAT_OUT2IN_ED_ERROR_BAD_ICMP_TYPE];
545 next = NAT_NEXT_DROP;
549 s = pool_elt_at_index (tsm->sessions, value.value);
552 *p_proto = ip_proto_to_snat_proto (key.proto);
555 *p_value = s->in2out;
556 *p_dont_translate = dont_translate;
558 *(snat_session_t **) d = s;
563 static snat_session_t *
564 nat44_ed_out2in_unknown_proto (snat_main_t * sm,
570 vlib_main_t * vm, vlib_node_runtime_t * node)
572 clib_bihash_kv_8_8_t kv, value;
573 clib_bihash_kv_16_8_t s_kv, s_value;
574 snat_static_mapping_t *m;
575 u32 old_addr, new_addr;
578 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
581 old_addr = ip->dst_address.as_u32;
583 make_ed_kv (&s_kv, &ip->dst_address, &ip->src_address, ip->protocol,
586 if (!clib_bihash_search_16_8 (&tsm->out2in_ed, &s_kv, &s_value))
588 s = pool_elt_at_index (tsm->sessions, s_value.value);
589 new_addr = ip->dst_address.as_u32 = s->in2out.addr.as_u32;
593 if (PREDICT_FALSE (nat44_maximum_sessions_exceeded (sm, thread_index)))
595 b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_SESSIONS_EXCEEDED];
596 nat_elog_notice ("maximum sessions exceeded");
600 make_sm_kv (&kv, &ip->dst_address, 0, 0, 0);
601 if (clib_bihash_search_8_8
602 (&sm->static_mapping_by_external, &kv, &value))
604 b->error = node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
608 m = pool_elt_at_index (sm->static_mappings, value.value);
610 new_addr = ip->dst_address.as_u32 = m->local_addr.as_u32;
612 u = nat_user_get_or_create (sm, &m->local_addr, m->fib_index,
616 nat_elog_warn ("create NAT user failed");
620 /* Create a new session */
621 s = nat_ed_session_alloc (sm, u, thread_index, now);
624 nat44_delete_user_with_no_session (sm, u, thread_index);
625 nat_elog_warn ("create NAT session failed");
629 s->ext_host_addr.as_u32 = ip->src_address.as_u32;
630 s->flags |= SNAT_SESSION_FLAG_UNKNOWN_PROTO;
631 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
632 s->flags |= SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT;
633 s->out2in.addr.as_u32 = old_addr;
634 s->out2in.fib_index = rx_fib_index;
635 s->in2out.addr.as_u32 = new_addr;
636 s->in2out.fib_index = m->fib_index;
637 s->in2out.port = s->out2in.port = ip->protocol;
638 user_session_increment (sm, u, 1);
640 /* Add to lookup tables */
641 s_kv.value = s - tsm->sessions;
642 if (clib_bihash_add_del_16_8 (&tsm->out2in_ed, &s_kv, 1))
643 nat_elog_notice ("out2in key add failed");
645 make_ed_kv (&s_kv, &ip->dst_address, &ip->src_address, ip->protocol,
647 s_kv.value = s - tsm->sessions;
648 if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &s_kv, 1))
649 nat_elog_notice ("in2out key add failed");
652 /* Update IP checksum */
654 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, dst_address);
655 ip->checksum = ip_csum_fold (sum);
657 vnet_buffer (b)->sw_if_index[VLIB_TX] = s->in2out.fib_index;
660 nat44_session_update_counters (s, now, vlib_buffer_length_in_chain (vm, b),
662 /* Per-user LRU list maintenance */
663 nat44_session_update_lru (sm, s, thread_index);
669 nat44_ed_out2in_fast_path_node_fn_inline (vlib_main_t * vm,
670 vlib_node_runtime_t * node,
671 vlib_frame_t * frame)
673 u32 n_left_from, *from, *to_next, pkts_processed = 0, stats_node_index;
674 nat_next_t next_index;
675 snat_main_t *sm = &snat_main;
676 f64 now = vlib_time_now (vm);
677 u32 thread_index = vm->thread_index;
678 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
679 u32 tcp_packets = 0, udp_packets = 0, icmp_packets = 0, other_packets =
682 stats_node_index = sm->ed_out2in_node_index;
684 from = vlib_frame_vector_args (frame);
685 n_left_from = frame->n_vectors;
686 next_index = node->cached_next_index;
688 while (n_left_from > 0)
692 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
694 while (n_left_from > 0 && n_left_to_next > 0)
698 u32 next0, sw_if_index0, rx_fib_index0, proto0, old_addr0,
700 u16 old_port0, new_port0;
704 snat_session_t *s0 = 0;
705 clib_bihash_kv_16_8_t kv0, value0;
708 /* speculatively enqueue b0 to the current next frame */
716 b0 = vlib_get_buffer (vm, bi0);
717 next0 = nat_buffer_opaque (b0)->arc_next;
719 vnet_buffer (b0)->snat.flags = 0;
720 ip0 = vlib_buffer_get_current (b0);
722 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
724 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
727 if (PREDICT_FALSE (ip0->ttl == 1))
729 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
730 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
731 ICMP4_time_exceeded_ttl_exceeded_in_transit,
733 next0 = NAT_NEXT_ICMP_ERROR;
737 udp0 = ip4_next_header (ip0);
738 tcp0 = (tcp_header_t *) udp0;
739 proto0 = ip_proto_to_snat_proto (ip0->protocol);
741 if (PREDICT_FALSE (proto0 == ~0))
743 next0 = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
747 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
749 next0 = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
753 make_ed_kv (&kv0, &ip0->dst_address, &ip0->src_address,
754 ip0->protocol, rx_fib_index0,
755 vnet_buffer (b0)->ip.reass.l4_dst_port,
756 vnet_buffer (b0)->ip.reass.l4_src_port);
758 if (clib_bihash_search_16_8 (&tsm->out2in_ed, &kv0, &value0))
760 next0 = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
763 s0 = pool_elt_at_index (tsm->sessions, value0.value);
765 // drop if session expired
766 u64 sess_timeout_time;
767 sess_timeout_time = s0->last_heard +
768 (f64) nat44_session_get_timeout (sm, s0);
769 if (now >= sess_timeout_time)
772 nat_free_session_data (sm, s0, thread_index, 0);
773 nat44_delete_session (sm, s0, thread_index);
775 next0 = NAT_NEXT_DROP;
780 old_addr0 = ip0->dst_address.as_u32;
781 new_addr0 = ip0->dst_address.as_u32 = s0->in2out.addr.as_u32;
782 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
784 sum0 = ip0->checksum;
785 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
787 if (PREDICT_FALSE (is_twice_nat_session (s0)))
788 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
789 s0->ext_host_nat_addr.as_u32, ip4_header_t,
791 ip0->checksum = ip_csum_fold (sum0);
793 old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
795 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
797 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
799 new_port0 = udp0->dst_port = s0->in2out.port;
800 sum0 = tcp0->checksum;
802 ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
805 ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
807 if (is_twice_nat_session (s0))
809 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
810 s0->ext_host_nat_addr.as_u32,
811 ip4_header_t, dst_address);
813 ip_csum_update (sum0,
814 vnet_buffer (b0)->ip.
816 s0->ext_host_nat_port, ip4_header_t,
818 tcp0->src_port = s0->ext_host_nat_port;
819 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
821 tcp0->checksum = ip_csum_fold (sum0);
824 if (nat44_set_tcp_session_state_o2i
825 (sm, s0, vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags,
826 vnet_buffer (b0)->ip.reass.tcp_ack_number,
827 vnet_buffer (b0)->ip.reass.tcp_seq_number, thread_index))
830 else if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment
833 new_port0 = udp0->dst_port = s0->in2out.port;
834 sum0 = udp0->checksum;
835 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
837 sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
839 if (PREDICT_FALSE (is_twice_nat_session (s0)))
841 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
842 s0->ext_host_nat_addr.as_u32,
843 ip4_header_t, dst_address);
845 ip_csum_update (sum0,
846 vnet_buffer (b0)->ip.reass.l4_src_port,
847 s0->ext_host_nat_port, ip4_header_t,
849 udp0->src_port = s0->ext_host_nat_port;
850 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
852 udp0->checksum = ip_csum_fold (sum0);
857 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
859 new_port0 = udp0->dst_port = s0->in2out.port;
860 if (PREDICT_FALSE (is_twice_nat_session (s0)))
862 udp0->src_port = s0->ext_host_nat_port;
863 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
870 nat44_session_update_counters (s0, now,
871 vlib_buffer_length_in_chain (vm, b0),
873 /* Per-user LRU list maintenance */
874 nat44_session_update_lru (sm, s0, thread_index);
877 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
878 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
880 nat44_ed_out2in_trace_t *t =
881 vlib_add_trace (vm, node, b0, sizeof (*t));
882 t->sw_if_index = sw_if_index0;
883 t->next_index = next0;
887 t->session_index = s0 - tsm->sessions;
889 t->session_index = ~0;
892 pkts_processed += next0 == nat_buffer_opaque (b0)->arc_next;
893 /* verify speculative enqueue, maybe switch current next frame */
894 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
895 to_next, n_left_to_next,
899 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
902 vlib_node_increment_counter (vm, stats_node_index,
903 NAT_OUT2IN_ED_ERROR_OUT2IN_PACKETS,
905 vlib_node_increment_counter (vm, stats_node_index,
906 NAT_OUT2IN_ED_ERROR_TCP_PACKETS, tcp_packets);
907 vlib_node_increment_counter (vm, stats_node_index,
908 NAT_OUT2IN_ED_ERROR_UDP_PACKETS, udp_packets);
909 vlib_node_increment_counter (vm, stats_node_index,
910 NAT_OUT2IN_ED_ERROR_ICMP_PACKETS,
912 vlib_node_increment_counter (vm, stats_node_index,
913 NAT_OUT2IN_ED_ERROR_OTHER_PACKETS,
915 vlib_node_increment_counter (vm, stats_node_index,
916 NAT_OUT2IN_ED_ERROR_FRAGMENTS, fragments);
917 return frame->n_vectors;
921 nat44_ed_out2in_slow_path_node_fn_inline (vlib_main_t * vm,
922 vlib_node_runtime_t * node,
923 vlib_frame_t * frame)
925 u32 n_left_from, *from, *to_next, pkts_processed = 0, stats_node_index;
926 nat_next_t next_index;
927 snat_main_t *sm = &snat_main;
928 f64 now = vlib_time_now (vm);
929 u32 thread_index = vm->thread_index;
930 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
931 u32 tcp_packets = 0, udp_packets = 0, icmp_packets = 0, other_packets =
934 stats_node_index = sm->ed_out2in_slowpath_node_index;
936 from = vlib_frame_vector_args (frame);
937 n_left_from = frame->n_vectors;
938 next_index = node->cached_next_index;
940 while (n_left_from > 0)
944 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
946 while (n_left_from > 0 && n_left_to_next > 0)
950 u32 next0, sw_if_index0, rx_fib_index0, proto0, old_addr0,
952 u16 old_port0, new_port0;
956 icmp46_header_t *icmp0;
957 snat_session_t *s0 = 0;
958 clib_bihash_kv_16_8_t kv0, value0;
960 snat_session_key_t e_key0, l_key0;
961 lb_nat_type_t lb_nat0;
962 twice_nat_type_t twice_nat0;
965 /* speculatively enqueue b0 to the current next frame */
973 b0 = vlib_get_buffer (vm, bi0);
974 next0 = nat_buffer_opaque (b0)->arc_next;
976 vnet_buffer (b0)->snat.flags = 0;
977 ip0 = vlib_buffer_get_current (b0);
979 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
981 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
984 if (PREDICT_FALSE (ip0->ttl == 1))
986 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
987 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
988 ICMP4_time_exceeded_ttl_exceeded_in_transit,
990 next0 = NAT_NEXT_ICMP_ERROR;
994 udp0 = ip4_next_header (ip0);
995 tcp0 = (tcp_header_t *) udp0;
996 icmp0 = (icmp46_header_t *) udp0;
997 proto0 = ip_proto_to_snat_proto (ip0->protocol);
999 if (PREDICT_FALSE (proto0 == ~0))
1002 nat44_ed_out2in_unknown_proto (sm, b0, ip0, rx_fib_index0,
1003 thread_index, now, vm, node);
1005 if (!sm->forwarding_enabled)
1008 next0 = NAT_NEXT_DROP;
1013 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1015 next0 = icmp_out2in_ed_slow_path
1016 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1017 next0, now, thread_index, &s0);
1022 make_ed_kv (&kv0, &ip0->dst_address, &ip0->src_address,
1023 ip0->protocol, rx_fib_index0,
1024 vnet_buffer (b0)->ip.reass.l4_dst_port,
1025 vnet_buffer (b0)->ip.reass.l4_src_port);
1027 if (clib_bihash_search_16_8 (&tsm->out2in_ed, &kv0, &value0))
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;
1108 s0 = pool_elt_at_index (tsm->sessions, value0.value);
1111 old_addr0 = ip0->dst_address.as_u32;
1112 new_addr0 = ip0->dst_address.as_u32 = s0->in2out.addr.as_u32;
1113 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
1115 sum0 = ip0->checksum;
1116 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1118 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1119 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1120 s0->ext_host_nat_addr.as_u32, ip4_header_t,
1122 ip0->checksum = ip_csum_fold (sum0);
1124 old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
1126 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1128 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1130 new_port0 = udp0->dst_port = s0->in2out.port;
1131 sum0 = tcp0->checksum;
1133 ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1136 ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
1138 if (is_twice_nat_session (s0))
1140 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1141 s0->ext_host_nat_addr.as_u32,
1142 ip4_header_t, dst_address);
1144 ip_csum_update (sum0,
1145 vnet_buffer (b0)->ip.
1147 s0->ext_host_nat_port, ip4_header_t,
1149 tcp0->src_port = s0->ext_host_nat_port;
1150 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1152 tcp0->checksum = ip_csum_fold (sum0);
1155 if (nat44_set_tcp_session_state_o2i
1156 (sm, s0, vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags,
1157 vnet_buffer (b0)->ip.reass.tcp_ack_number,
1158 vnet_buffer (b0)->ip.reass.tcp_seq_number, thread_index))
1161 else if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment
1164 new_port0 = udp0->dst_port = s0->in2out.port;
1165 sum0 = udp0->checksum;
1166 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1168 sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
1170 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1172 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1173 s0->ext_host_nat_addr.as_u32,
1174 ip4_header_t, dst_address);
1176 ip_csum_update (sum0,
1177 vnet_buffer (b0)->ip.reass.l4_src_port,
1178 s0->ext_host_nat_port, ip4_header_t,
1180 udp0->src_port = s0->ext_host_nat_port;
1181 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1183 udp0->checksum = ip_csum_fold (sum0);
1188 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1190 new_port0 = udp0->dst_port = s0->in2out.port;
1191 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1193 udp0->src_port = s0->ext_host_nat_port;
1194 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1201 nat44_session_update_counters (s0, now,
1202 vlib_buffer_length_in_chain (vm, b0),
1204 /* Per-user LRU list maintenance */
1205 nat44_session_update_lru (sm, s0, thread_index);
1208 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1209 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1211 nat44_ed_out2in_trace_t *t =
1212 vlib_add_trace (vm, node, b0, sizeof (*t));
1213 t->sw_if_index = sw_if_index0;
1214 t->next_index = next0;
1215 t->is_slow_path = 1;
1218 t->session_index = s0 - tsm->sessions;
1220 t->session_index = ~0;
1223 pkts_processed += next0 == nat_buffer_opaque (b0)->arc_next;
1224 /* verify speculative enqueue, maybe switch current next frame */
1225 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1226 to_next, n_left_to_next,
1230 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1233 vlib_node_increment_counter (vm, stats_node_index,
1234 NAT_OUT2IN_ED_ERROR_OUT2IN_PACKETS,
1236 vlib_node_increment_counter (vm, stats_node_index,
1237 NAT_OUT2IN_ED_ERROR_TCP_PACKETS, tcp_packets);
1238 vlib_node_increment_counter (vm, stats_node_index,
1239 NAT_OUT2IN_ED_ERROR_UDP_PACKETS, udp_packets);
1240 vlib_node_increment_counter (vm, stats_node_index,
1241 NAT_OUT2IN_ED_ERROR_ICMP_PACKETS,
1243 vlib_node_increment_counter (vm, stats_node_index,
1244 NAT_OUT2IN_ED_ERROR_OTHER_PACKETS,
1246 vlib_node_increment_counter (vm, stats_node_index,
1247 NAT_OUT2IN_ED_ERROR_FRAGMENTS, fragments);
1248 return frame->n_vectors;
1251 VLIB_NODE_FN (nat44_ed_out2in_node) (vlib_main_t * vm,
1252 vlib_node_runtime_t * node,
1253 vlib_frame_t * frame)
1255 return nat44_ed_out2in_fast_path_node_fn_inline (vm, node, frame);
1259 VLIB_REGISTER_NODE (nat44_ed_out2in_node) = {
1260 .name = "nat44-ed-out2in",
1261 .vector_size = sizeof (u32),
1262 .sibling_of = "nat-default",
1263 .format_trace = format_nat44_ed_out2in_trace,
1264 .type = VLIB_NODE_TYPE_INTERNAL,
1265 .n_errors = ARRAY_LEN(nat_out2in_ed_error_strings),
1266 .error_strings = nat_out2in_ed_error_strings,
1267 .runtime_data_bytes = sizeof (snat_runtime_t),
1271 VLIB_NODE_FN (nat44_ed_out2in_slowpath_node) (vlib_main_t * vm,
1272 vlib_node_runtime_t * node,
1273 vlib_frame_t * frame)
1275 return nat44_ed_out2in_slow_path_node_fn_inline (vm, node, frame);
1279 VLIB_REGISTER_NODE (nat44_ed_out2in_slowpath_node) = {
1280 .name = "nat44-ed-out2in-slowpath",
1281 .vector_size = sizeof (u32),
1282 .sibling_of = "nat-default",
1283 .format_trace = format_nat44_ed_out2in_trace,
1284 .type = VLIB_NODE_TYPE_INTERNAL,
1285 .n_errors = ARRAY_LEN(nat_out2in_ed_error_strings),
1286 .error_strings = nat_out2in_ed_error_strings,
1287 .runtime_data_bytes = sizeof (snat_runtime_t),
1292 format_nat_pre_trace (u8 * s, va_list * args)
1294 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1295 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1296 nat_pre_trace_t *t = va_arg (*args, nat_pre_trace_t *);
1297 return format (s, "out2in next_index %d", t->next_index);
1300 VLIB_NODE_FN (nat_pre_out2in_node) (vlib_main_t * vm,
1301 vlib_node_runtime_t * node,
1302 vlib_frame_t * frame)
1304 return nat_pre_node_fn_inline (vm, node, frame,
1305 NAT_NEXT_OUT2IN_ED_FAST_PATH);
1309 VLIB_REGISTER_NODE (nat_pre_out2in_node) = {
1310 .name = "nat-pre-out2in",
1311 .vector_size = sizeof (u32),
1312 .sibling_of = "nat-default",
1313 .format_trace = format_nat_pre_trace,
1314 .type = VLIB_NODE_TYPE_INTERNAL,
1320 * fd.io coding-style-patch-verification: ON
1323 * eval: (c-set-style "gnu")