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>
34 #include <nat/nat44/ed_inlines.h>
36 static char *nat_out2in_ed_error_strings[] = {
37 #define _(sym,string) string,
38 foreach_nat_out2in_ed_error
48 } nat44_ed_out2in_trace_t;
51 format_nat44_ed_out2in_trace (u8 * s, va_list * args)
53 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
54 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
55 nat44_ed_out2in_trace_t *t = va_arg (*args, nat44_ed_out2in_trace_t *);
59 t->is_slow_path ? "NAT44_OUT2IN_ED_SLOW_PATH" :
60 "NAT44_OUT2IN_ED_FAST_PATH";
62 s = format (s, "%s: sw_if_index %d, next index %d, session %d", tag,
63 t->sw_if_index, t->next_index, t->session_index);
69 icmp_out2in_ed_slow_path (snat_main_t * sm, vlib_buffer_t * b0,
70 ip4_header_t * ip0, icmp46_header_t * icmp0,
71 u32 sw_if_index0, u32 rx_fib_index0,
72 vlib_node_runtime_t * node, u32 next0, f64 now,
73 u32 thread_index, snat_session_t ** p_s0)
75 vlib_main_t *vm = vlib_get_main ();
77 next0 = icmp_out2in (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
78 next0, thread_index, p_s0, 0);
79 snat_session_t *s0 = *p_s0;
80 if (PREDICT_TRUE (next0 != NAT_NEXT_DROP && s0))
83 nat44_session_update_counters (s0, now,
84 vlib_buffer_length_in_chain
85 (vm, b0), thread_index);
86 /* Per-user LRU list maintenance */
87 nat44_session_update_lru (sm, s0, thread_index);
92 #ifndef CLIB_MARCH_VARIANT
94 nat44_o2i_ed_is_idle_session_cb (clib_bihash_kv_16_8_t * kv, void *arg)
96 snat_main_t *sm = &snat_main;
97 nat44_is_idle_session_ctx_t *ctx = arg;
99 u64 sess_timeout_time;
102 ip4_address_t *l_addr, *r_addr;
104 clib_bihash_kv_16_8_t ed_kv;
107 snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
110 s = pool_elt_at_index (tsm->sessions, kv->value);
111 sess_timeout_time = s->last_heard + (f64) nat44_session_get_timeout (sm, s);
112 if (ctx->now >= sess_timeout_time)
114 l_addr = &s->in2out.addr;
115 r_addr = &s->ext_host_addr;
116 fib_index = s->in2out.fib_index;
117 if (snat_is_unk_proto_session (s))
119 proto = s->in2out.port;
125 proto = nat_proto_to_ip_proto (s->nat_proto);
126 l_port = s->in2out.port;
127 r_port = s->ext_host_port;
129 if (is_twice_nat_session (s))
131 r_addr = &s->ext_host_nat_addr;
132 r_port = s->ext_host_nat_port;
134 init_ed_k (&ed_kv, *l_addr, l_port, *r_addr, r_port, fib_index, proto);
135 if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &ed_kv, 0))
136 nat_elog_warn ("in2out_ed key del failed");
138 if (snat_is_unk_proto_session (s))
141 snat_ipfix_logging_nat44_ses_delete (ctx->thread_index,
142 s->in2out.addr.as_u32,
143 s->out2in.addr.as_u32,
147 s->in2out.fib_index);
149 nat_syslog_nat44_sdel (s->user_index, s->in2out.fib_index,
150 &s->in2out.addr, s->in2out.port,
151 &s->ext_host_nat_addr, s->ext_host_nat_port,
152 &s->out2in.addr, s->out2in.port,
153 &s->ext_host_addr, s->ext_host_port,
154 s->nat_proto, is_twice_nat_session (s));
156 nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
157 s->ext_host_port, s->nat_proto, s->out2in.fib_index,
160 if (is_twice_nat_session (s))
162 for (i = 0; i < vec_len (sm->twice_nat_addresses); i++)
164 // FIXME TODO this is obviously wrong code ... needs fix!
165 // key.protocol = s->nat_proto;
166 // key.port = s->ext_host_nat_port;
167 // a = sm->twice_nat_addresses + i;
168 // if (a->addr.as_u32 == s->ext_host_nat_addr.as_u32)
170 // snat_free_outside_address_and_port (sm->twice_nat_addresses,
171 // ctx->thread_index,
178 if (snat_is_session_static (s))
181 snat_free_outside_address_and_port (sm->addresses, ctx->thread_index,
182 &s->out2in.addr, s->out2in.port,
185 nat_ed_session_delete (sm, s, ctx->thread_index, 1);
193 static snat_session_t *
194 create_session_for_static_mapping_ed (snat_main_t * sm,
196 ip4_address_t i2o_addr,
199 ip4_address_t o2i_addr,
202 nat_protocol_t nat_proto,
203 vlib_node_runtime_t * node,
206 twice_nat_type_t twice_nat,
207 lb_nat_type_t lb_nat, f64 now)
212 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
213 clib_bihash_kv_16_8_t kv;
214 nat44_is_idle_session_ctx_t ctx;
217 (nat44_ed_maximum_sessions_exceeded (sm, rx_fib_index, thread_index)))
219 b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_SESSIONS_EXCEEDED];
220 nat_elog_notice ("maximum sessions exceeded");
224 s = nat_ed_session_alloc (sm, thread_index, now, nat_proto);
227 b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_USER_SESS_EXCEEDED];
228 nat_elog_warn ("create NAT session failed");
232 ip = vlib_buffer_get_current (b);
233 udp = ip4_next_header (ip);
235 s->ext_host_addr.as_u32 = ip->src_address.as_u32;
236 s->ext_host_port = nat_proto == NAT_PROTOCOL_ICMP ? 0 : udp->src_port;
237 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
239 s->flags |= SNAT_SESSION_FLAG_LOAD_BALANCING;
240 if (lb_nat == AFFINITY_LB_NAT)
241 s->flags |= SNAT_SESSION_FLAG_AFFINITY;
242 s->flags |= SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT;
243 s->out2in.addr = o2i_addr;
244 s->out2in.port = o2i_port;
245 s->out2in.fib_index = o2i_fib_index;
246 s->in2out.addr = i2o_addr;
247 s->in2out.port = i2o_port;
248 s->in2out.fib_index = i2o_fib_index;
249 s->nat_proto = nat_proto;
251 /* Add to lookup tables */
252 init_ed_kv (&kv, o2i_addr, o2i_port, s->ext_host_addr, s->ext_host_port,
253 o2i_fib_index, ip->protocol, thread_index, s - tsm->sessions);
255 ctx.thread_index = thread_index;
256 if (clib_bihash_add_or_overwrite_stale_16_8 (&sm->out2in_ed, &kv,
257 nat44_o2i_ed_is_idle_session_cb,
259 nat_elog_notice ("out2in-ed key add failed");
261 if (twice_nat == TWICE_NAT || (twice_nat == TWICE_NAT_SELF &&
262 ip->src_address.as_u32 == i2o_addr.as_u32))
264 if (snat_alloc_outside_address_and_port (sm->twice_nat_addresses, 0,
267 &s->ext_host_nat_addr,
268 &s->ext_host_nat_port,
270 tsm->snat_thread_index))
272 b->error = node->errors[NAT_OUT2IN_ED_ERROR_OUT_OF_PORTS];
273 nat_ed_session_delete (sm, s, thread_index, 1);
274 if (clib_bihash_add_del_16_8 (&sm->out2in_ed, &kv, 0))
275 nat_elog_notice ("out2in-ed key del failed");
278 s->flags |= SNAT_SESSION_FLAG_TWICE_NAT;
279 init_ed_kv (&kv, i2o_addr, i2o_port, s->ext_host_nat_addr,
280 s->ext_host_nat_port, i2o_fib_index, ip->protocol,
281 thread_index, s - tsm->sessions);
285 init_ed_kv (&kv, i2o_addr, i2o_port, s->ext_host_addr,
286 s->ext_host_port, i2o_fib_index, ip->protocol,
287 thread_index, s - tsm->sessions);
289 if (clib_bihash_add_or_overwrite_stale_16_8 (&tsm->in2out_ed, &kv,
290 nat44_i2o_ed_is_idle_session_cb,
292 nat_elog_notice ("in2out-ed key add failed");
294 snat_ipfix_logging_nat44_ses_create (thread_index,
295 s->in2out.addr.as_u32,
296 s->out2in.addr.as_u32,
299 s->out2in.port, s->in2out.fib_index);
301 nat_syslog_nat44_sadd (s->user_index, s->in2out.fib_index,
302 &s->in2out.addr, s->in2out.port,
303 &s->ext_host_nat_addr, s->ext_host_nat_port,
304 &s->out2in.addr, s->out2in.port,
305 &s->ext_host_addr, s->ext_host_port,
306 s->nat_proto, is_twice_nat_session (s));
308 nat_ha_sadd (&s->in2out.addr, s->in2out.port, &s->out2in.addr,
309 s->out2in.port, &s->ext_host_addr, s->ext_host_port,
310 &s->ext_host_nat_addr, s->ext_host_nat_port,
311 s->nat_proto, s->in2out.fib_index, s->flags, thread_index, 0);
317 next_src_nat (snat_main_t * sm, ip4_header_t * ip, u16 src_port,
318 u16 dst_port, u32 thread_index, u32 rx_fib_index)
320 clib_bihash_kv_16_8_t kv, value;
321 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
323 init_ed_k (&kv, ip->src_address, src_port, ip->dst_address, dst_port,
324 rx_fib_index, ip->protocol);
325 if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
332 create_bypass_for_fwd (snat_main_t * sm, vlib_buffer_t * b, ip4_header_t * ip,
333 u32 rx_fib_index, u32 thread_index)
335 clib_bihash_kv_16_8_t kv, value;
337 snat_session_t *s = 0;
338 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
339 vlib_main_t *vm = vlib_get_main ();
340 f64 now = vlib_time_now (vm);
343 if (ip->protocol == IP_PROTOCOL_ICMP)
345 if (get_icmp_o2i_ed_key
346 (b, ip, rx_fib_index, ~0, ~0, 0, &l_port, &r_port, &kv))
351 if (ip->protocol == IP_PROTOCOL_UDP || ip->protocol == IP_PROTOCOL_TCP)
353 udp = ip4_next_header (ip);
354 l_port = udp->dst_port;
355 r_port = udp->src_port;
362 init_ed_k (&kv, ip->dst_address, l_port, ip->src_address, r_port,
363 rx_fib_index, ip->protocol);
366 if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
368 ASSERT (thread_index == ed_value_get_thread_index (&value));
370 pool_elt_at_index (tsm->sessions,
371 ed_value_get_session_index (&value));
378 (nat44_ed_maximum_sessions_exceeded
379 (sm, rx_fib_index, thread_index)))
382 s = nat_ed_session_alloc (sm, thread_index, now, ip->protocol);
385 nat_elog_warn ("create NAT session failed");
389 proto = ip_proto_to_nat_proto (ip->protocol);
391 s->ext_host_addr = ip->src_address;
392 s->ext_host_port = r_port;
393 s->flags |= SNAT_SESSION_FLAG_FWD_BYPASS;
394 s->out2in.addr = ip->dst_address;
395 s->out2in.port = l_port;
396 s->nat_proto = proto;
397 if (proto == NAT_PROTOCOL_OTHER)
399 s->flags |= SNAT_SESSION_FLAG_UNKNOWN_PROTO;
400 s->out2in.port = ip->protocol;
402 s->out2in.fib_index = 0;
403 s->in2out.addr = s->out2in.addr;
404 s->in2out.port = s->out2in.port;
405 s->in2out.fib_index = s->out2in.fib_index;
407 kv.value = s - tsm->sessions;
408 if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &kv, 1))
409 nat_elog_notice ("in2out_ed key add failed");
412 if (ip->protocol == IP_PROTOCOL_TCP)
414 tcp_header_t *tcp = ip4_next_header (ip);
415 if (nat44_set_tcp_session_state_o2i
416 (sm, now, s, tcp->flags, tcp->ack_number, tcp->seq_number,
422 nat44_session_update_counters (s, now, 0, thread_index);
423 /* Per-user LRU list maintenance */
424 nat44_session_update_lru (sm, s, thread_index);
428 create_bypass_for_fwd_worker (snat_main_t * sm, vlib_buffer_t * b,
429 ip4_header_t * ip, u32 rx_fib_index)
431 ip4_header_t ip_wkr = {
432 .src_address = ip->dst_address,
434 u32 thread_index = sm->worker_in2out_cb (&ip_wkr, rx_fib_index, 0);
436 create_bypass_for_fwd (sm, b, ip, rx_fib_index, thread_index);
439 #ifndef CLIB_MARCH_VARIANT
441 icmp_match_out2in_ed (snat_main_t * sm, vlib_node_runtime_t * node,
442 u32 thread_index, vlib_buffer_t * b,
443 ip4_header_t * ip, ip4_address_t * addr,
444 u16 * port, u32 * fib_index, nat_protocol_t * proto,
445 void *d, void *e, u8 * dont_translate)
447 u32 next = ~0, sw_if_index, rx_fib_index;
448 clib_bihash_kv_16_8_t kv, value;
449 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
450 snat_session_t *s = 0;
451 u8 is_addr_only, identity_nat;
453 vlib_main_t *vm = vlib_get_main ();
454 ip4_address_t sm_addr;
459 sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
460 rx_fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
462 if (get_icmp_o2i_ed_key
463 (b, ip, rx_fib_index, ~0, ~0, proto, &l_port, &r_port, &kv))
465 b->error = node->errors[NAT_OUT2IN_ED_ERROR_UNSUPPORTED_PROTOCOL];
466 next = NAT_NEXT_DROP;
470 if (clib_bihash_search_16_8 (&sm->out2in_ed, &kv, &value))
472 /* Try to match static mapping */
473 if (snat_static_mapping_match
474 (sm, ip->dst_address, l_port, rx_fib_index,
475 ip_proto_to_nat_proto (ip->protocol), &sm_addr, &sm_port,
476 &sm_fib_index, 1, &is_addr_only, 0, 0, 0, &identity_nat))
478 if (!sm->forwarding_enabled)
480 /* Don't NAT packet aimed at the intfc address */
481 if (PREDICT_FALSE (is_interface_addr (sm, node, sw_if_index,
482 ip->dst_address.as_u32)))
487 b->error = node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
488 next = NAT_NEXT_DROP;
494 if (next_src_nat (sm, ip, l_port, r_port,
495 thread_index, rx_fib_index))
497 next = NAT_NEXT_IN2OUT_ED_FAST_PATH;
500 if (sm->num_workers > 1)
501 create_bypass_for_fwd_worker (sm, b, ip, rx_fib_index);
503 create_bypass_for_fwd (sm, b, ip, rx_fib_index, thread_index);
509 (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags !=
511 && (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags !=
512 ICMP4_echo_request || !is_addr_only)))
514 b->error = node->errors[NAT_OUT2IN_ED_ERROR_BAD_ICMP_TYPE];
515 next = NAT_NEXT_DROP;
519 if (PREDICT_FALSE (identity_nat))
525 /* Create session initiated by host from external network */
527 create_session_for_static_mapping_ed (sm, b, sm_addr, sm_port,
528 sm_fib_index, ip->dst_address,
529 l_port, rx_fib_index, *proto,
536 next = NAT_NEXT_DROP;
543 (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags !=
545 && vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags !=
547 && !icmp_type_is_error_message (vnet_buffer (b)->ip.
548 reass.icmp_type_or_tcp_flags)))
550 b->error = node->errors[NAT_OUT2IN_ED_ERROR_BAD_ICMP_TYPE];
551 next = NAT_NEXT_DROP;
555 ASSERT (thread_index == ed_value_get_thread_index (&value));
557 pool_elt_at_index (tsm->sessions,
558 ed_value_get_session_index (&value));
563 *addr = s->in2out.addr;
564 *port = s->in2out.port;
565 *fib_index = s->in2out.fib_index;
568 *(snat_session_t **) d = s;
573 static snat_session_t *
574 nat44_ed_out2in_unknown_proto (snat_main_t * sm,
580 vlib_main_t * vm, vlib_node_runtime_t * node)
582 clib_bihash_kv_8_8_t kv, value;
583 clib_bihash_kv_16_8_t s_kv, s_value;
584 snat_static_mapping_t *m;
585 u32 old_addr, new_addr;
588 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
590 old_addr = ip->dst_address.as_u32;
592 init_ed_k (&s_kv, ip->dst_address, 0, ip->src_address, 0, rx_fib_index,
595 if (!clib_bihash_search_16_8 (&sm->out2in_ed, &s_kv, &s_value))
597 ASSERT (thread_index == ed_value_get_thread_index (&s_value));
599 pool_elt_at_index (tsm->sessions,
600 ed_value_get_session_index (&s_value));
601 new_addr = ip->dst_address.as_u32 = s->in2out.addr.as_u32;
606 (nat44_ed_maximum_sessions_exceeded
607 (sm, rx_fib_index, thread_index)))
609 b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_SESSIONS_EXCEEDED];
610 nat_elog_notice ("maximum sessions exceeded");
614 init_nat_k (&kv, ip->dst_address, 0, 0, 0);
615 if (clib_bihash_search_8_8
616 (&sm->static_mapping_by_external, &kv, &value))
618 b->error = node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
622 m = pool_elt_at_index (sm->static_mappings, value.value);
624 new_addr = ip->dst_address.as_u32 = m->local_addr.as_u32;
626 /* Create a new session */
627 s = nat_ed_session_alloc (sm, thread_index, now, ip->protocol);
630 b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_USER_SESS_EXCEEDED];
631 nat_elog_warn ("create NAT session failed");
635 s->ext_host_addr.as_u32 = ip->src_address.as_u32;
636 s->flags |= SNAT_SESSION_FLAG_UNKNOWN_PROTO;
637 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
638 s->flags |= SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT;
639 s->out2in.addr.as_u32 = old_addr;
640 s->out2in.fib_index = rx_fib_index;
641 s->in2out.addr.as_u32 = new_addr;
642 s->in2out.fib_index = m->fib_index;
643 s->in2out.port = s->out2in.port = ip->protocol;
645 /* Add to lookup tables */
646 s_kv.value = s - tsm->sessions;
647 if (clib_bihash_add_del_16_8 (&sm->out2in_ed, &s_kv, 1))
648 nat_elog_notice ("out2in key add failed");
650 init_ed_kv (&s_kv, ip->dst_address, 0, ip->src_address, 0, m->fib_index,
651 ip->protocol, thread_index, s - tsm->sessions);
652 if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &s_kv, 1))
653 nat_elog_notice ("in2out key add failed");
656 /* Update IP checksum */
658 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, dst_address);
659 ip->checksum = ip_csum_fold (sum);
661 vnet_buffer (b)->sw_if_index[VLIB_TX] = s->in2out.fib_index;
664 nat44_session_update_counters (s, now, vlib_buffer_length_in_chain (vm, b),
666 /* Per-user LRU list maintenance */
667 nat44_session_update_lru (sm, s, thread_index);
673 nat44_ed_out2in_fast_path_node_fn_inline (vlib_main_t * vm,
674 vlib_node_runtime_t * node,
675 vlib_frame_t * frame,
678 u32 n_left_from, *from, pkts_processed = 0, stats_node_index;
679 snat_main_t *sm = &snat_main;
680 f64 now = vlib_time_now (vm);
681 u32 thread_index = vm->thread_index;
682 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
683 u32 tcp_packets = 0, udp_packets = 0, icmp_packets = 0, other_packets =
686 stats_node_index = sm->ed_out2in_node_index;
688 from = vlib_frame_vector_args (frame);
689 n_left_from = frame->n_vectors;
691 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
692 u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
693 vlib_get_buffers (vm, from, b, n_left_from);
695 while (n_left_from > 0)
698 u32 sw_if_index0, rx_fib_index0, proto0, old_addr0, new_addr0;
699 u16 old_port0, new_port0;
703 snat_session_t *s0 = 0;
704 clib_bihash_kv_16_8_t kv0, value0;
710 /* Prefetch next iteration. */
711 if (PREDICT_TRUE (n_left_from >= 2))
717 vlib_prefetch_buffer_header (p2, LOAD);
719 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, LOAD);
722 next[0] = vnet_buffer2 (b0)->nat.arc_next;
724 vnet_buffer (b0)->snat.flags = 0;
725 ip0 = vlib_buffer_get_current (b0);
727 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
729 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4, sw_if_index0);
731 if (PREDICT_FALSE (ip0->ttl == 1))
733 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
734 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
735 ICMP4_time_exceeded_ttl_exceeded_in_transit,
737 next[0] = NAT_NEXT_ICMP_ERROR;
741 udp0 = ip4_next_header (ip0);
742 tcp0 = (tcp_header_t *) udp0;
743 proto0 = ip_proto_to_nat_proto (ip0->protocol);
745 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
747 next[0] = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
751 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
753 next[0] = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
757 init_ed_k (&kv0, ip0->dst_address,
758 vnet_buffer (b0)->ip.reass.l4_dst_port, ip0->src_address,
759 vnet_buffer (b0)->ip.reass.l4_src_port, rx_fib_index0,
762 /* there is a stashed index in vnet_buffer2 from handoff node,
763 * see if we can use it */
766 PREDICT_TRUE (!pool_is_free_index
768 vnet_buffer2 (b0)->nat.ed_out2in_nat_session_index)))
770 s0 = pool_elt_at_index (tsm->sessions,
772 nat.ed_out2in_nat_session_index);
774 (s0->out2in.addr.as_u32 == ip0->dst_address.as_u32
775 && s0->out2in.port == vnet_buffer (b0)->ip.reass.l4_dst_port
776 && s0->nat_proto == ip_proto_to_nat_proto (ip0->protocol)
777 && s0->out2in.fib_index == rx_fib_index0
778 && s0->ext_host_addr.as_u32 == ip0->src_address.as_u32
779 && s0->ext_host_port ==
780 vnet_buffer (b0)->ip.reass.l4_src_port))
782 /* yes, this is the droid we're looking for */
787 if (clib_bihash_search_16_8 (&sm->out2in_ed, &kv0, &value0))
789 next[0] = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
792 ASSERT (thread_index == ed_value_get_thread_index (&value0));
794 pool_elt_at_index (tsm->sessions,
795 ed_value_get_session_index (&value0));
798 if (s0->tcp_closed_timestamp)
800 if (now >= s0->tcp_closed_timestamp)
802 // session is closed, go slow path
803 next[0] = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
807 // session in transitory timeout, drop
808 b0->error = node->errors[NAT_OUT2IN_ED_ERROR_TCP_CLOSED];
809 next[0] = NAT_NEXT_DROP;
814 // drop if session expired
815 u64 sess_timeout_time;
817 s0->last_heard + (f64) nat44_session_get_timeout (sm, s0);
818 if (now >= sess_timeout_time)
820 // session is closed, go slow path
821 nat_free_session_data (sm, s0, thread_index, 0);
822 nat_ed_session_delete (sm, s0, thread_index, 1);
823 next[0] = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
828 old_addr0 = ip0->dst_address.as_u32;
829 new_addr0 = ip0->dst_address.as_u32 = s0->in2out.addr.as_u32;
830 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
832 sum0 = ip0->checksum;
833 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
835 if (PREDICT_FALSE (is_twice_nat_session (s0)))
836 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
837 s0->ext_host_nat_addr.as_u32, ip4_header_t,
839 ip0->checksum = ip_csum_fold (sum0);
841 old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
843 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
845 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
847 new_port0 = udp0->dst_port = s0->in2out.port;
848 sum0 = tcp0->checksum;
850 ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
853 ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
855 if (is_twice_nat_session (s0))
857 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
858 s0->ext_host_nat_addr.as_u32,
859 ip4_header_t, dst_address);
861 ip_csum_update (sum0,
862 vnet_buffer (b0)->ip.reass.l4_src_port,
863 s0->ext_host_nat_port, ip4_header_t,
865 tcp0->src_port = s0->ext_host_nat_port;
866 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
868 tcp0->checksum = ip_csum_fold (sum0);
871 if (nat44_set_tcp_session_state_o2i
873 vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags,
874 vnet_buffer (b0)->ip.reass.tcp_ack_number,
875 vnet_buffer (b0)->ip.reass.tcp_seq_number, thread_index))
878 else if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment
881 new_port0 = udp0->dst_port = s0->in2out.port;
882 sum0 = udp0->checksum;
884 ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
887 ip_csum_update (sum0, old_port0, new_port0, ip4_header_t, length);
888 if (PREDICT_FALSE (is_twice_nat_session (s0)))
891 ip_csum_update (sum0, ip0->src_address.as_u32,
892 s0->ext_host_nat_addr.as_u32, ip4_header_t,
895 ip_csum_update (sum0, vnet_buffer (b0)->ip.reass.l4_src_port,
896 s0->ext_host_nat_port, ip4_header_t, length);
897 udp0->src_port = s0->ext_host_nat_port;
898 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
900 udp0->checksum = ip_csum_fold (sum0);
905 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
907 new_port0 = udp0->dst_port = s0->in2out.port;
908 if (PREDICT_FALSE (is_twice_nat_session (s0)))
910 udp0->src_port = s0->ext_host_nat_port;
911 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
918 nat44_session_update_counters (s0, now,
919 vlib_buffer_length_in_chain (vm, b0),
921 /* Per-user LRU list maintenance */
922 nat44_session_update_lru (sm, s0, thread_index);
925 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
926 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
928 nat44_ed_out2in_trace_t *t =
929 vlib_add_trace (vm, node, b0, sizeof (*t));
930 t->sw_if_index = sw_if_index0;
931 t->next_index = next[0];
935 t->session_index = s0 - tsm->sessions;
937 t->session_index = ~0;
940 pkts_processed += next[0] == vnet_buffer2 (b0)->nat.arc_next;
946 vlib_buffer_enqueue_to_next (vm, node, from, (u16 *) nexts,
949 vlib_node_increment_counter (vm, stats_node_index,
950 NAT_OUT2IN_ED_ERROR_OUT2IN_PACKETS,
952 vlib_node_increment_counter (vm, stats_node_index,
953 NAT_OUT2IN_ED_ERROR_TCP_PACKETS, tcp_packets);
954 vlib_node_increment_counter (vm, stats_node_index,
955 NAT_OUT2IN_ED_ERROR_UDP_PACKETS, udp_packets);
956 vlib_node_increment_counter (vm, stats_node_index,
957 NAT_OUT2IN_ED_ERROR_ICMP_PACKETS,
959 vlib_node_increment_counter (vm, stats_node_index,
960 NAT_OUT2IN_ED_ERROR_OTHER_PACKETS,
962 vlib_node_increment_counter (vm, stats_node_index,
963 NAT_OUT2IN_ED_ERROR_FRAGMENTS, fragments);
964 return frame->n_vectors;
968 nat44_ed_out2in_slow_path_node_fn_inline (vlib_main_t * vm,
969 vlib_node_runtime_t * node,
970 vlib_frame_t * frame)
972 u32 n_left_from, *from, pkts_processed = 0, stats_node_index;
973 snat_main_t *sm = &snat_main;
974 f64 now = vlib_time_now (vm);
975 u32 thread_index = vm->thread_index;
976 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
977 u32 tcp_packets = 0, udp_packets = 0, icmp_packets = 0, other_packets =
980 stats_node_index = sm->ed_out2in_slowpath_node_index;
982 from = vlib_frame_vector_args (frame);
983 n_left_from = frame->n_vectors;
985 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
986 u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
987 vlib_get_buffers (vm, from, b, n_left_from);
989 while (n_left_from > 0)
992 u32 sw_if_index0, rx_fib_index0, proto0, old_addr0, new_addr0;
993 u16 old_port0, new_port0;
997 icmp46_header_t *icmp0;
998 snat_session_t *s0 = 0;
999 clib_bihash_kv_16_8_t kv0, value0;
1001 lb_nat_type_t lb_nat0;
1002 twice_nat_type_t twice_nat0;
1004 ip4_address_t sm_addr;
1009 next[0] = vnet_buffer2 (b0)->nat.arc_next;
1011 vnet_buffer (b0)->snat.flags = 0;
1012 ip0 = vlib_buffer_get_current (b0);
1014 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1016 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4, sw_if_index0);
1018 if (PREDICT_FALSE (ip0->ttl == 1))
1020 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1021 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1022 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1024 next[0] = NAT_NEXT_ICMP_ERROR;
1028 udp0 = ip4_next_header (ip0);
1029 tcp0 = (tcp_header_t *) udp0;
1030 icmp0 = (icmp46_header_t *) udp0;
1031 proto0 = ip_proto_to_nat_proto (ip0->protocol);
1033 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1036 nat44_ed_out2in_unknown_proto (sm, b0, ip0, rx_fib_index0,
1037 thread_index, now, vm, node);
1038 if (!sm->forwarding_enabled)
1041 next[0] = NAT_NEXT_DROP;
1047 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1049 next[0] = icmp_out2in_ed_slow_path
1050 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1051 next[0], now, thread_index, &s0);
1056 init_ed_k (&kv0, ip0->dst_address,
1057 vnet_buffer (b0)->ip.reass.l4_dst_port, ip0->src_address,
1058 vnet_buffer (b0)->ip.reass.l4_src_port, rx_fib_index0,
1062 if (!clib_bihash_search_16_8 (&sm->out2in_ed, &kv0, &value0))
1064 ASSERT (thread_index == ed_value_get_thread_index (&value0));
1066 pool_elt_at_index (tsm->sessions,
1067 ed_value_get_session_index (&value0));
1069 if (s0->tcp_closed_timestamp && now >= s0->tcp_closed_timestamp)
1071 nat_free_session_data (sm, s0, thread_index, 0);
1072 nat_ed_session_delete (sm, s0, thread_index, 1);
1079 /* Try to match static mapping by external address and port,
1080 destination address and port in packet */
1082 if (snat_static_mapping_match
1083 (sm, ip0->dst_address,
1084 vnet_buffer (b0)->ip.reass.l4_dst_port, rx_fib_index0,
1085 proto0, &sm_addr, &sm_port, &sm_fib_index, 1, 0,
1086 &twice_nat0, &lb_nat0, &ip0->src_address, &identity_nat0))
1089 * Send DHCP packets to the ipv4 stack, or we won't
1090 * be able to use dhcp client on the outside interface
1092 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_UDP
1093 && (vnet_buffer (b0)->ip.reass.l4_dst_port ==
1094 clib_host_to_net_u16
1095 (UDP_DST_PORT_dhcp_to_client))))
1100 if (!sm->forwarding_enabled)
1103 node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
1104 next[0] = NAT_NEXT_DROP;
1109 (sm, ip0, vnet_buffer (b0)->ip.reass.l4_src_port,
1110 vnet_buffer (b0)->ip.reass.l4_dst_port,
1111 thread_index, rx_fib_index0))
1113 next[0] = NAT_NEXT_IN2OUT_ED_FAST_PATH;
1116 if (sm->num_workers > 1)
1117 create_bypass_for_fwd_worker (sm, b0, ip0, rx_fib_index0);
1119 create_bypass_for_fwd (sm, b0, ip0, rx_fib_index0,
1125 if (PREDICT_FALSE (identity_nat0))
1128 if ((proto0 == NAT_PROTOCOL_TCP)
1129 && !tcp_flags_is_init (vnet_buffer (b0)->ip.
1130 reass.icmp_type_or_tcp_flags))
1132 b0->error = node->errors[NAT_OUT2IN_ED_ERROR_NON_SYN];
1133 next[0] = NAT_NEXT_DROP;
1137 /* Create session initiated by host from external network */
1138 s0 = create_session_for_static_mapping_ed (sm, b0,
1143 ip.reass.l4_dst_port,
1144 rx_fib_index0, proto0,
1145 node, rx_fib_index0,
1146 thread_index, twice_nat0,
1150 next[0] = NAT_NEXT_DROP;
1155 old_addr0 = ip0->dst_address.as_u32;
1156 new_addr0 = ip0->dst_address.as_u32 = s0->in2out.addr.as_u32;
1157 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
1159 sum0 = ip0->checksum;
1160 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1162 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1163 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1164 s0->ext_host_nat_addr.as_u32, ip4_header_t,
1166 ip0->checksum = ip_csum_fold (sum0);
1168 old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
1170 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1172 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1174 new_port0 = udp0->dst_port = s0->in2out.port;
1175 sum0 = tcp0->checksum;
1177 ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1180 ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
1182 if (is_twice_nat_session (s0))
1184 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1185 s0->ext_host_nat_addr.as_u32,
1186 ip4_header_t, dst_address);
1188 ip_csum_update (sum0,
1189 vnet_buffer (b0)->ip.reass.l4_src_port,
1190 s0->ext_host_nat_port, ip4_header_t,
1192 tcp0->src_port = s0->ext_host_nat_port;
1193 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1195 tcp0->checksum = ip_csum_fold (sum0);
1198 if (nat44_set_tcp_session_state_o2i
1200 vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags,
1201 vnet_buffer (b0)->ip.reass.tcp_ack_number,
1202 vnet_buffer (b0)->ip.reass.tcp_seq_number, thread_index))
1205 else if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment
1208 new_port0 = udp0->dst_port = s0->in2out.port;
1209 sum0 = udp0->checksum;
1210 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1212 sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
1214 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1216 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1217 s0->ext_host_nat_addr.as_u32,
1218 ip4_header_t, dst_address);
1220 ip_csum_update (sum0,
1221 vnet_buffer (b0)->ip.reass.l4_src_port,
1222 s0->ext_host_nat_port, ip4_header_t, length);
1223 udp0->src_port = s0->ext_host_nat_port;
1224 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1226 udp0->checksum = ip_csum_fold (sum0);
1231 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1233 new_port0 = udp0->dst_port = s0->in2out.port;
1234 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1236 udp0->src_port = s0->ext_host_nat_port;
1237 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1244 nat44_session_update_counters (s0, now,
1245 vlib_buffer_length_in_chain (vm, b0),
1247 /* Per-user LRU list maintenance */
1248 nat44_session_update_lru (sm, s0, thread_index);
1251 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1252 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1254 nat44_ed_out2in_trace_t *t =
1255 vlib_add_trace (vm, node, b0, sizeof (*t));
1256 t->sw_if_index = sw_if_index0;
1257 t->next_index = next[0];
1258 t->is_slow_path = 1;
1261 t->session_index = s0 - tsm->sessions;
1263 t->session_index = ~0;
1266 pkts_processed += next[0] == vnet_buffer2 (b0)->nat.arc_next;
1273 vlib_buffer_enqueue_to_next (vm, node, from, (u16 *) nexts,
1276 vlib_node_increment_counter (vm, stats_node_index,
1277 NAT_OUT2IN_ED_ERROR_OUT2IN_PACKETS,
1279 vlib_node_increment_counter (vm, stats_node_index,
1280 NAT_OUT2IN_ED_ERROR_TCP_PACKETS, tcp_packets);
1281 vlib_node_increment_counter (vm, stats_node_index,
1282 NAT_OUT2IN_ED_ERROR_UDP_PACKETS, udp_packets);
1283 vlib_node_increment_counter (vm, stats_node_index,
1284 NAT_OUT2IN_ED_ERROR_ICMP_PACKETS,
1286 vlib_node_increment_counter (vm, stats_node_index,
1287 NAT_OUT2IN_ED_ERROR_OTHER_PACKETS,
1289 vlib_node_increment_counter (vm, stats_node_index,
1290 NAT_OUT2IN_ED_ERROR_FRAGMENTS, fragments);
1291 return frame->n_vectors;
1294 VLIB_NODE_FN (nat44_ed_out2in_node) (vlib_main_t * vm,
1295 vlib_node_runtime_t * node,
1296 vlib_frame_t * frame)
1298 if (snat_main.num_workers > 1)
1300 return nat44_ed_out2in_fast_path_node_fn_inline (vm, node, frame, 1);
1304 return nat44_ed_out2in_fast_path_node_fn_inline (vm, node, frame, 0);
1309 VLIB_REGISTER_NODE (nat44_ed_out2in_node) = {
1310 .name = "nat44-ed-out2in",
1311 .vector_size = sizeof (u32),
1312 .sibling_of = "nat-default",
1313 .format_trace = format_nat44_ed_out2in_trace,
1314 .type = VLIB_NODE_TYPE_INTERNAL,
1315 .n_errors = ARRAY_LEN(nat_out2in_ed_error_strings),
1316 .error_strings = nat_out2in_ed_error_strings,
1317 .runtime_data_bytes = sizeof (snat_runtime_t),
1321 VLIB_NODE_FN (nat44_ed_out2in_slowpath_node) (vlib_main_t * vm,
1322 vlib_node_runtime_t * node,
1323 vlib_frame_t * frame)
1325 return nat44_ed_out2in_slow_path_node_fn_inline (vm, node, frame);
1329 VLIB_REGISTER_NODE (nat44_ed_out2in_slowpath_node) = {
1330 .name = "nat44-ed-out2in-slowpath",
1331 .vector_size = sizeof (u32),
1332 .sibling_of = "nat-default",
1333 .format_trace = format_nat44_ed_out2in_trace,
1334 .type = VLIB_NODE_TYPE_INTERNAL,
1335 .n_errors = ARRAY_LEN(nat_out2in_ed_error_strings),
1336 .error_strings = nat_out2in_ed_error_strings,
1337 .runtime_data_bytes = sizeof (snat_runtime_t),
1342 format_nat_pre_trace (u8 * s, va_list * args)
1344 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1345 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1346 nat_pre_trace_t *t = va_arg (*args, nat_pre_trace_t *);
1347 return format (s, "out2in next_index %d arc_next_index %d", t->next_index,
1351 VLIB_NODE_FN (nat_pre_out2in_node) (vlib_main_t * vm,
1352 vlib_node_runtime_t * node,
1353 vlib_frame_t * frame)
1355 return nat_pre_node_fn_inline (vm, node, frame,
1356 NAT_NEXT_OUT2IN_ED_FAST_PATH);
1360 VLIB_REGISTER_NODE (nat_pre_out2in_node) = {
1361 .name = "nat-pre-out2in",
1362 .vector_size = sizeof (u32),
1363 .sibling_of = "nat-default",
1364 .format_trace = format_nat_pre_trace,
1365 .type = VLIB_NODE_TYPE_INTERNAL,
1371 * fd.io coding-style-patch-verification: ON
1374 * eval: (c-set-style "gnu")