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);
313 per_vrf_sessions_register_session (s, thread_index);
319 next_src_nat (snat_main_t * sm, ip4_header_t * ip, u16 src_port,
320 u16 dst_port, u32 thread_index, u32 rx_fib_index)
322 clib_bihash_kv_16_8_t kv, value;
323 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
325 init_ed_k (&kv, ip->src_address, src_port, ip->dst_address, dst_port,
326 rx_fib_index, ip->protocol);
327 if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
334 create_bypass_for_fwd (snat_main_t * sm, vlib_buffer_t * b, ip4_header_t * ip,
335 u32 rx_fib_index, u32 thread_index)
337 clib_bihash_kv_16_8_t kv, value;
339 snat_session_t *s = 0;
340 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
341 vlib_main_t *vm = vlib_get_main ();
342 f64 now = vlib_time_now (vm);
345 if (ip->protocol == IP_PROTOCOL_ICMP)
347 if (get_icmp_o2i_ed_key
348 (b, ip, rx_fib_index, ~0, ~0, 0, &l_port, &r_port, &kv))
353 if (ip->protocol == IP_PROTOCOL_UDP || ip->protocol == IP_PROTOCOL_TCP)
355 udp = ip4_next_header (ip);
356 l_port = udp->dst_port;
357 r_port = udp->src_port;
364 init_ed_k (&kv, ip->dst_address, l_port, ip->src_address, r_port,
365 rx_fib_index, ip->protocol);
368 if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
370 ASSERT (thread_index == ed_value_get_thread_index (&value));
372 pool_elt_at_index (tsm->sessions,
373 ed_value_get_session_index (&value));
380 (nat44_ed_maximum_sessions_exceeded
381 (sm, rx_fib_index, thread_index)))
384 s = nat_ed_session_alloc (sm, thread_index, now, ip->protocol);
387 nat_elog_warn ("create NAT session failed");
391 proto = ip_proto_to_nat_proto (ip->protocol);
393 s->ext_host_addr = ip->src_address;
394 s->ext_host_port = r_port;
395 s->flags |= SNAT_SESSION_FLAG_FWD_BYPASS;
396 s->out2in.addr = ip->dst_address;
397 s->out2in.port = l_port;
398 s->nat_proto = proto;
399 if (proto == NAT_PROTOCOL_OTHER)
401 s->flags |= SNAT_SESSION_FLAG_UNKNOWN_PROTO;
402 s->out2in.port = ip->protocol;
404 s->out2in.fib_index = 0;
405 s->in2out.addr = s->out2in.addr;
406 s->in2out.port = s->out2in.port;
407 s->in2out.fib_index = s->out2in.fib_index;
409 kv.value = s - tsm->sessions;
410 if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &kv, 1))
411 nat_elog_notice ("in2out_ed key add failed");
413 per_vrf_sessions_register_session (s, thread_index);
416 if (ip->protocol == IP_PROTOCOL_TCP)
418 tcp_header_t *tcp = ip4_next_header (ip);
419 if (nat44_set_tcp_session_state_o2i
420 (sm, now, s, tcp->flags, tcp->ack_number, tcp->seq_number,
426 nat44_session_update_counters (s, now, 0, thread_index);
427 /* Per-user LRU list maintenance */
428 nat44_session_update_lru (sm, s, thread_index);
432 create_bypass_for_fwd_worker (snat_main_t * sm, vlib_buffer_t * b,
433 ip4_header_t * ip, u32 rx_fib_index)
435 ip4_header_t ip_wkr = {
436 .src_address = ip->dst_address,
438 u32 thread_index = sm->worker_in2out_cb (&ip_wkr, rx_fib_index, 0);
440 create_bypass_for_fwd (sm, b, ip, rx_fib_index, thread_index);
443 #ifndef CLIB_MARCH_VARIANT
445 icmp_match_out2in_ed (snat_main_t * sm, vlib_node_runtime_t * node,
446 u32 thread_index, vlib_buffer_t * b,
447 ip4_header_t * ip, ip4_address_t * addr,
448 u16 * port, u32 * fib_index, nat_protocol_t * proto,
449 void *d, void *e, u8 * dont_translate)
451 u32 next = ~0, sw_if_index, rx_fib_index;
452 clib_bihash_kv_16_8_t kv, value;
453 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
454 snat_session_t *s = 0;
455 u8 is_addr_only, identity_nat;
457 vlib_main_t *vm = vlib_get_main ();
458 ip4_address_t sm_addr;
463 sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
464 rx_fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
466 if (get_icmp_o2i_ed_key
467 (b, ip, rx_fib_index, ~0, ~0, proto, &l_port, &r_port, &kv))
469 b->error = node->errors[NAT_OUT2IN_ED_ERROR_UNSUPPORTED_PROTOCOL];
470 next = NAT_NEXT_DROP;
474 if (clib_bihash_search_16_8 (&sm->out2in_ed, &kv, &value))
476 /* Try to match static mapping */
477 if (snat_static_mapping_match
478 (sm, ip->dst_address, l_port, rx_fib_index,
479 ip_proto_to_nat_proto (ip->protocol), &sm_addr, &sm_port,
480 &sm_fib_index, 1, &is_addr_only, 0, 0, 0, &identity_nat))
482 if (!sm->forwarding_enabled)
484 /* Don't NAT packet aimed at the intfc address */
485 if (PREDICT_FALSE (is_interface_addr (sm, node, sw_if_index,
486 ip->dst_address.as_u32)))
491 b->error = node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
492 next = NAT_NEXT_DROP;
498 if (next_src_nat (sm, ip, l_port, r_port,
499 thread_index, rx_fib_index))
501 next = NAT_NEXT_IN2OUT_ED_FAST_PATH;
504 if (sm->num_workers > 1)
505 create_bypass_for_fwd_worker (sm, b, ip, rx_fib_index);
507 create_bypass_for_fwd (sm, b, ip, rx_fib_index, thread_index);
513 (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags !=
515 && (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags !=
516 ICMP4_echo_request || !is_addr_only)))
518 b->error = node->errors[NAT_OUT2IN_ED_ERROR_BAD_ICMP_TYPE];
519 next = NAT_NEXT_DROP;
523 if (PREDICT_FALSE (identity_nat))
529 /* Create session initiated by host from external network */
531 create_session_for_static_mapping_ed (sm, b, sm_addr, sm_port,
532 sm_fib_index, ip->dst_address,
533 l_port, rx_fib_index, *proto,
540 next = NAT_NEXT_DROP;
547 (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags !=
549 && vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags !=
551 && !icmp_type_is_error_message (vnet_buffer (b)->ip.
552 reass.icmp_type_or_tcp_flags)))
554 b->error = node->errors[NAT_OUT2IN_ED_ERROR_BAD_ICMP_TYPE];
555 next = NAT_NEXT_DROP;
559 ASSERT (thread_index == ed_value_get_thread_index (&value));
561 pool_elt_at_index (tsm->sessions,
562 ed_value_get_session_index (&value));
567 *addr = s->in2out.addr;
568 *port = s->in2out.port;
569 *fib_index = s->in2out.fib_index;
572 *(snat_session_t **) d = s;
577 static snat_session_t *
578 nat44_ed_out2in_unknown_proto (snat_main_t * sm,
584 vlib_main_t * vm, vlib_node_runtime_t * node)
586 clib_bihash_kv_8_8_t kv, value;
587 clib_bihash_kv_16_8_t s_kv, s_value;
588 snat_static_mapping_t *m;
589 u32 old_addr, new_addr;
592 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
594 old_addr = ip->dst_address.as_u32;
596 init_ed_k (&s_kv, ip->dst_address, 0, ip->src_address, 0, rx_fib_index,
599 if (!clib_bihash_search_16_8 (&sm->out2in_ed, &s_kv, &s_value))
601 ASSERT (thread_index == ed_value_get_thread_index (&s_value));
603 pool_elt_at_index (tsm->sessions,
604 ed_value_get_session_index (&s_value));
605 new_addr = ip->dst_address.as_u32 = s->in2out.addr.as_u32;
610 (nat44_ed_maximum_sessions_exceeded
611 (sm, rx_fib_index, thread_index)))
613 b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_SESSIONS_EXCEEDED];
614 nat_elog_notice ("maximum sessions exceeded");
618 init_nat_k (&kv, ip->dst_address, 0, 0, 0);
619 if (clib_bihash_search_8_8
620 (&sm->static_mapping_by_external, &kv, &value))
622 b->error = node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
626 m = pool_elt_at_index (sm->static_mappings, value.value);
628 new_addr = ip->dst_address.as_u32 = m->local_addr.as_u32;
630 /* Create a new session */
631 s = nat_ed_session_alloc (sm, thread_index, now, ip->protocol);
634 b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_USER_SESS_EXCEEDED];
635 nat_elog_warn ("create NAT session failed");
639 s->ext_host_addr.as_u32 = ip->src_address.as_u32;
640 s->flags |= SNAT_SESSION_FLAG_UNKNOWN_PROTO;
641 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
642 s->flags |= SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT;
643 s->out2in.addr.as_u32 = old_addr;
644 s->out2in.fib_index = rx_fib_index;
645 s->in2out.addr.as_u32 = new_addr;
646 s->in2out.fib_index = m->fib_index;
647 s->in2out.port = s->out2in.port = ip->protocol;
649 /* Add to lookup tables */
650 s_kv.value = s - tsm->sessions;
651 if (clib_bihash_add_del_16_8 (&sm->out2in_ed, &s_kv, 1))
652 nat_elog_notice ("out2in key add failed");
654 init_ed_kv (&s_kv, ip->dst_address, 0, ip->src_address, 0, m->fib_index,
655 ip->protocol, thread_index, s - tsm->sessions);
656 if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &s_kv, 1))
657 nat_elog_notice ("in2out key add failed");
659 per_vrf_sessions_register_session (s, thread_index);
662 /* Update IP checksum */
664 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, dst_address);
665 ip->checksum = ip_csum_fold (sum);
667 vnet_buffer (b)->sw_if_index[VLIB_TX] = s->in2out.fib_index;
670 nat44_session_update_counters (s, now, vlib_buffer_length_in_chain (vm, b),
672 /* Per-user LRU list maintenance */
673 nat44_session_update_lru (sm, s, thread_index);
679 nat44_ed_out2in_fast_path_node_fn_inline (vlib_main_t * vm,
680 vlib_node_runtime_t * node,
681 vlib_frame_t * frame,
684 u32 n_left_from, *from;
685 snat_main_t *sm = &snat_main;
686 f64 now = vlib_time_now (vm);
687 u32 thread_index = vm->thread_index;
688 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
690 from = vlib_frame_vector_args (frame);
691 n_left_from = frame->n_vectors;
693 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
694 u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
695 vlib_get_buffers (vm, from, b, n_left_from);
697 while (n_left_from > 0)
700 u32 sw_if_index0, rx_fib_index0, proto0, old_addr0, new_addr0;
701 u16 old_port0, new_port0;
705 snat_session_t *s0 = 0;
706 clib_bihash_kv_16_8_t kv0, value0;
712 /* Prefetch next iteration. */
713 if (PREDICT_TRUE (n_left_from >= 2))
719 vlib_prefetch_buffer_header (p2, LOAD);
721 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, LOAD);
724 next[0] = vnet_buffer2 (b0)->nat.arc_next;
726 vnet_buffer (b0)->snat.flags = 0;
727 ip0 = vlib_buffer_get_current (b0);
729 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
731 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4, sw_if_index0);
733 if (PREDICT_FALSE (ip0->ttl == 1))
735 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
736 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
737 ICMP4_time_exceeded_ttl_exceeded_in_transit,
739 next[0] = NAT_NEXT_ICMP_ERROR;
743 udp0 = ip4_next_header (ip0);
744 tcp0 = (tcp_header_t *) udp0;
745 proto0 = ip_proto_to_nat_proto (ip0->protocol);
747 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
749 next[0] = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
753 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
755 next[0] = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
759 init_ed_k (&kv0, ip0->dst_address,
760 vnet_buffer (b0)->ip.reass.l4_dst_port, ip0->src_address,
761 vnet_buffer (b0)->ip.reass.l4_src_port, rx_fib_index0,
764 /* there is a stashed index in vnet_buffer2 from handoff node,
765 * see if we can use it */
768 PREDICT_TRUE (!pool_is_free_index
770 vnet_buffer2 (b0)->nat.ed_out2in_nat_session_index)))
772 s0 = pool_elt_at_index (tsm->sessions,
774 nat.ed_out2in_nat_session_index);
776 (s0->out2in.addr.as_u32 == ip0->dst_address.as_u32
777 && s0->out2in.port == vnet_buffer (b0)->ip.reass.l4_dst_port
778 && s0->nat_proto == ip_proto_to_nat_proto (ip0->protocol)
779 && s0->out2in.fib_index == rx_fib_index0
780 && s0->ext_host_addr.as_u32 == ip0->src_address.as_u32
781 && s0->ext_host_port ==
782 vnet_buffer (b0)->ip.reass.l4_src_port))
784 /* yes, this is the droid we're looking for */
789 // lookup for session
790 if (clib_bihash_search_16_8 (&sm->out2in_ed, &kv0, &value0))
792 // session does not exist go slow path
793 next[0] = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
796 ASSERT (thread_index == ed_value_get_thread_index (&value0));
798 pool_elt_at_index (tsm->sessions,
799 ed_value_get_session_index (&value0));
803 if (PREDICT_FALSE (per_vrf_sessions_is_expired (s0, thread_index)))
805 // session is closed, go slow path
806 nat_free_session_data (sm, s0, thread_index, 0);
807 nat_ed_session_delete (sm, s0, thread_index, 1);
808 next[0] = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
812 if (s0->tcp_closed_timestamp)
814 if (now >= s0->tcp_closed_timestamp)
816 // session is closed, go slow path, freed in slow path
817 next[0] = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
821 // session in transitory timeout, drop
822 b0->error = node->errors[NAT_OUT2IN_ED_ERROR_TCP_CLOSED];
823 next[0] = NAT_NEXT_DROP;
828 // drop if session expired
829 u64 sess_timeout_time;
831 s0->last_heard + (f64) nat44_session_get_timeout (sm, s0);
832 if (now >= sess_timeout_time)
834 // session is closed, go slow path
835 nat_free_session_data (sm, s0, thread_index, 0);
836 nat_ed_session_delete (sm, s0, thread_index, 1);
837 next[0] = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
841 old_addr0 = ip0->dst_address.as_u32;
842 new_addr0 = ip0->dst_address.as_u32 = s0->in2out.addr.as_u32;
843 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
845 sum0 = ip0->checksum;
846 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
848 if (PREDICT_FALSE (is_twice_nat_session (s0)))
849 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
850 s0->ext_host_nat_addr.as_u32, ip4_header_t,
852 ip0->checksum = ip_csum_fold (sum0);
854 old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
856 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
858 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
860 new_port0 = udp0->dst_port = s0->in2out.port;
861 sum0 = tcp0->checksum;
863 ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
866 ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
868 if (is_twice_nat_session (s0))
870 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
871 s0->ext_host_nat_addr.as_u32,
872 ip4_header_t, dst_address);
874 ip_csum_update (sum0,
875 vnet_buffer (b0)->ip.reass.l4_src_port,
876 s0->ext_host_nat_port, ip4_header_t,
878 tcp0->src_port = s0->ext_host_nat_port;
879 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
881 tcp0->checksum = ip_csum_fold (sum0);
883 vlib_increment_simple_counter (&sm->counters.fastpath.out2in_ed.tcp,
884 thread_index, sw_if_index0, 1);
885 if (nat44_set_tcp_session_state_o2i
886 (sm, now, s0, vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags,
887 vnet_buffer (b0)->ip.reass.tcp_ack_number,
888 vnet_buffer (b0)->ip.reass.tcp_seq_number, thread_index))
891 else if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment
894 new_port0 = udp0->dst_port = s0->in2out.port;
895 sum0 = udp0->checksum;
897 ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
900 ip_csum_update (sum0, old_port0, new_port0, ip4_header_t, length);
901 if (PREDICT_FALSE (is_twice_nat_session (s0)))
904 ip_csum_update (sum0, ip0->src_address.as_u32,
905 s0->ext_host_nat_addr.as_u32, ip4_header_t,
908 ip_csum_update (sum0, vnet_buffer (b0)->ip.reass.l4_src_port,
909 s0->ext_host_nat_port, ip4_header_t, length);
910 udp0->src_port = s0->ext_host_nat_port;
911 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
913 udp0->checksum = ip_csum_fold (sum0);
914 vlib_increment_simple_counter (&sm->counters.fastpath.out2in_ed.udp,
915 thread_index, sw_if_index0, 1);
919 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
921 new_port0 = udp0->dst_port = s0->in2out.port;
922 if (PREDICT_FALSE (is_twice_nat_session (s0)))
924 udp0->src_port = s0->ext_host_nat_port;
925 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
928 vlib_increment_simple_counter (&sm->counters.fastpath.out2in_ed.udp,
929 thread_index, sw_if_index0, 1);
933 nat44_session_update_counters (s0, now,
934 vlib_buffer_length_in_chain (vm, b0),
936 /* Per-user LRU list maintenance */
937 nat44_session_update_lru (sm, s0, thread_index);
940 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
941 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
943 nat44_ed_out2in_trace_t *t =
944 vlib_add_trace (vm, node, b0, sizeof (*t));
945 t->sw_if_index = sw_if_index0;
946 t->next_index = next[0];
950 t->session_index = s0 - tsm->sessions;
952 t->session_index = ~0;
955 if (next[0] == NAT_NEXT_DROP)
957 vlib_increment_simple_counter (&sm->counters.fastpath.
958 out2in_ed.drops, thread_index,
966 vlib_buffer_enqueue_to_next (vm, node, from, (u16 *) nexts,
968 return frame->n_vectors;
972 nat44_ed_out2in_slow_path_node_fn_inline (vlib_main_t * vm,
973 vlib_node_runtime_t * node,
974 vlib_frame_t * frame)
976 u32 n_left_from, *from;
977 snat_main_t *sm = &snat_main;
978 f64 now = vlib_time_now (vm);
979 u32 thread_index = vm->thread_index;
980 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_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;
1043 vlib_increment_simple_counter (&sm->counters.slowpath.
1044 out2in_ed.other, thread_index,
1049 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1051 next[0] = icmp_out2in_ed_slow_path
1052 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1053 next[0], now, thread_index, &s0);
1054 vlib_increment_simple_counter (&sm->counters.slowpath.
1055 out2in_ed.icmp, thread_index,
1060 init_ed_k (&kv0, ip0->dst_address,
1061 vnet_buffer (b0)->ip.reass.l4_dst_port, ip0->src_address,
1062 vnet_buffer (b0)->ip.reass.l4_src_port, rx_fib_index0,
1066 if (!clib_bihash_search_16_8 (&sm->out2in_ed, &kv0, &value0))
1068 ASSERT (thread_index == ed_value_get_thread_index (&value0));
1070 pool_elt_at_index (tsm->sessions,
1071 ed_value_get_session_index (&value0));
1073 if (s0->tcp_closed_timestamp && now >= s0->tcp_closed_timestamp)
1075 nat_free_session_data (sm, s0, thread_index, 0);
1076 nat_ed_session_delete (sm, s0, thread_index, 1);
1083 /* Try to match static mapping by external address and port,
1084 destination address and port in packet */
1086 if (snat_static_mapping_match
1087 (sm, ip0->dst_address,
1088 vnet_buffer (b0)->ip.reass.l4_dst_port, rx_fib_index0,
1089 proto0, &sm_addr, &sm_port, &sm_fib_index, 1, 0,
1090 &twice_nat0, &lb_nat0, &ip0->src_address, &identity_nat0))
1093 * Send DHCP packets to the ipv4 stack, or we won't
1094 * be able to use dhcp client on the outside interface
1096 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_UDP
1097 && (vnet_buffer (b0)->ip.reass.l4_dst_port ==
1098 clib_host_to_net_u16
1099 (UDP_DST_PORT_dhcp_to_client))))
1104 if (!sm->forwarding_enabled)
1107 node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
1108 next[0] = NAT_NEXT_DROP;
1113 (sm, ip0, vnet_buffer (b0)->ip.reass.l4_src_port,
1114 vnet_buffer (b0)->ip.reass.l4_dst_port,
1115 thread_index, rx_fib_index0))
1117 next[0] = NAT_NEXT_IN2OUT_ED_FAST_PATH;
1120 if (sm->num_workers > 1)
1121 create_bypass_for_fwd_worker (sm, b0, ip0, rx_fib_index0);
1123 create_bypass_for_fwd (sm, b0, ip0, rx_fib_index0,
1129 if (PREDICT_FALSE (identity_nat0))
1132 if ((proto0 == NAT_PROTOCOL_TCP)
1133 && !tcp_flags_is_init (vnet_buffer (b0)->ip.
1134 reass.icmp_type_or_tcp_flags))
1136 b0->error = node->errors[NAT_OUT2IN_ED_ERROR_NON_SYN];
1137 next[0] = NAT_NEXT_DROP;
1141 /* Create session initiated by host from external network */
1142 s0 = create_session_for_static_mapping_ed (sm, b0,
1147 ip.reass.l4_dst_port,
1148 rx_fib_index0, proto0,
1149 node, rx_fib_index0,
1150 thread_index, twice_nat0,
1154 next[0] = NAT_NEXT_DROP;
1159 old_addr0 = ip0->dst_address.as_u32;
1160 new_addr0 = ip0->dst_address.as_u32 = s0->in2out.addr.as_u32;
1161 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
1163 sum0 = ip0->checksum;
1164 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1166 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1167 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1168 s0->ext_host_nat_addr.as_u32, ip4_header_t,
1170 ip0->checksum = ip_csum_fold (sum0);
1172 old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
1174 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1176 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1178 new_port0 = udp0->dst_port = s0->in2out.port;
1179 sum0 = tcp0->checksum;
1181 ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1184 ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
1186 if (is_twice_nat_session (s0))
1188 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1189 s0->ext_host_nat_addr.as_u32,
1190 ip4_header_t, dst_address);
1192 ip_csum_update (sum0,
1193 vnet_buffer (b0)->ip.reass.l4_src_port,
1194 s0->ext_host_nat_port, ip4_header_t,
1196 tcp0->src_port = s0->ext_host_nat_port;
1197 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1199 tcp0->checksum = ip_csum_fold (sum0);
1201 vlib_increment_simple_counter (&sm->counters.slowpath.out2in_ed.tcp,
1202 thread_index, sw_if_index0, 1);
1203 if (nat44_set_tcp_session_state_o2i
1204 (sm, now, s0, vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags,
1205 vnet_buffer (b0)->ip.reass.tcp_ack_number,
1206 vnet_buffer (b0)->ip.reass.tcp_seq_number, thread_index))
1209 else if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment
1212 new_port0 = udp0->dst_port = s0->in2out.port;
1213 sum0 = udp0->checksum;
1214 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1216 sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
1218 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1220 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1221 s0->ext_host_nat_addr.as_u32,
1222 ip4_header_t, dst_address);
1224 ip_csum_update (sum0,
1225 vnet_buffer (b0)->ip.reass.l4_src_port,
1226 s0->ext_host_nat_port, ip4_header_t, length);
1227 udp0->src_port = s0->ext_host_nat_port;
1228 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1230 udp0->checksum = ip_csum_fold (sum0);
1231 vlib_increment_simple_counter (&sm->counters.slowpath.out2in_ed.udp,
1232 thread_index, sw_if_index0, 1);
1236 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1238 new_port0 = udp0->dst_port = s0->in2out.port;
1239 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1241 udp0->src_port = s0->ext_host_nat_port;
1242 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1245 vlib_increment_simple_counter (&sm->counters.slowpath.out2in_ed.udp,
1246 thread_index, sw_if_index0, 1);
1250 nat44_session_update_counters (s0, now,
1251 vlib_buffer_length_in_chain (vm, b0),
1253 /* Per-user LRU list maintenance */
1254 nat44_session_update_lru (sm, s0, thread_index);
1257 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1258 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1260 nat44_ed_out2in_trace_t *t =
1261 vlib_add_trace (vm, node, b0, sizeof (*t));
1262 t->sw_if_index = sw_if_index0;
1263 t->next_index = next[0];
1264 t->is_slow_path = 1;
1267 t->session_index = s0 - tsm->sessions;
1269 t->session_index = ~0;
1272 if (next[0] == NAT_NEXT_DROP)
1274 vlib_increment_simple_counter (&sm->counters.slowpath.
1275 out2in_ed.drops, thread_index,
1284 vlib_buffer_enqueue_to_next (vm, node, from, (u16 *) nexts,
1287 return frame->n_vectors;
1290 VLIB_NODE_FN (nat44_ed_out2in_node) (vlib_main_t * vm,
1291 vlib_node_runtime_t * node,
1292 vlib_frame_t * frame)
1294 if (snat_main.num_workers > 1)
1296 return nat44_ed_out2in_fast_path_node_fn_inline (vm, node, frame, 1);
1300 return nat44_ed_out2in_fast_path_node_fn_inline (vm, node, frame, 0);
1305 VLIB_REGISTER_NODE (nat44_ed_out2in_node) = {
1306 .name = "nat44-ed-out2in",
1307 .vector_size = sizeof (u32),
1308 .sibling_of = "nat-default",
1309 .format_trace = format_nat44_ed_out2in_trace,
1310 .type = VLIB_NODE_TYPE_INTERNAL,
1311 .n_errors = ARRAY_LEN(nat_out2in_ed_error_strings),
1312 .error_strings = nat_out2in_ed_error_strings,
1313 .runtime_data_bytes = sizeof (snat_runtime_t),
1317 VLIB_NODE_FN (nat44_ed_out2in_slowpath_node) (vlib_main_t * vm,
1318 vlib_node_runtime_t * node,
1319 vlib_frame_t * frame)
1321 return nat44_ed_out2in_slow_path_node_fn_inline (vm, node, frame);
1325 VLIB_REGISTER_NODE (nat44_ed_out2in_slowpath_node) = {
1326 .name = "nat44-ed-out2in-slowpath",
1327 .vector_size = sizeof (u32),
1328 .sibling_of = "nat-default",
1329 .format_trace = format_nat44_ed_out2in_trace,
1330 .type = VLIB_NODE_TYPE_INTERNAL,
1331 .n_errors = ARRAY_LEN(nat_out2in_ed_error_strings),
1332 .error_strings = nat_out2in_ed_error_strings,
1333 .runtime_data_bytes = sizeof (snat_runtime_t),
1338 format_nat_pre_trace (u8 * s, va_list * args)
1340 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1341 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1342 nat_pre_trace_t *t = va_arg (*args, nat_pre_trace_t *);
1343 return format (s, "out2in next_index %d arc_next_index %d", t->next_index,
1347 VLIB_NODE_FN (nat_pre_out2in_node) (vlib_main_t * vm,
1348 vlib_node_runtime_t * node,
1349 vlib_frame_t * frame)
1351 return nat_pre_node_fn_inline (vm, node, frame,
1352 NAT_NEXT_OUT2IN_ED_FAST_PATH);
1356 VLIB_REGISTER_NODE (nat_pre_out2in_node) = {
1357 .name = "nat-pre-out2in",
1358 .vector_size = sizeof (u32),
1359 .sibling_of = "nat-default",
1360 .format_trace = format_nat_pre_trace,
1361 .type = VLIB_NODE_TYPE_INTERNAL,
1367 * fd.io coding-style-patch-verification: ON
1370 * eval: (c-set-style "gnu")