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;
99 ip4_address_t *l_addr, *r_addr;
101 clib_bihash_kv_16_8_t ed_kv;
104 snat_session_key_t key;
105 snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
108 s = pool_elt_at_index (tsm->sessions, kv->value);
109 sess_timeout_time = s->last_heard + (f64) nat44_session_get_timeout (sm, s);
110 if (ctx->now >= sess_timeout_time)
112 l_addr = &s->in2out.addr;
113 r_addr = &s->ext_host_addr;
114 fib_index = s->in2out.fib_index;
115 if (snat_is_unk_proto_session (s))
117 proto = s->in2out.port;
123 proto = snat_proto_to_ip_proto (s->in2out.protocol);
124 l_port = s->in2out.port;
125 r_port = s->ext_host_port;
127 if (is_twice_nat_session (s))
129 r_addr = &s->ext_host_nat_addr;
130 r_port = s->ext_host_nat_port;
132 make_ed_kv (l_addr, r_addr, proto, fib_index, l_port, r_port, ~0ULL,
134 if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &ed_kv, 0))
135 nat_elog_warn ("in2out_ed key del failed");
137 if (snat_is_unk_proto_session (s))
140 snat_ipfix_logging_nat44_ses_delete (ctx->thread_index,
141 s->in2out.addr.as_u32,
142 s->out2in.addr.as_u32,
146 s->in2out.fib_index);
148 nat_syslog_nat44_sdel (s->user_index, s->in2out.fib_index,
149 &s->in2out.addr, s->in2out.port,
150 &s->ext_host_nat_addr, s->ext_host_nat_port,
151 &s->out2in.addr, s->out2in.port,
152 &s->ext_host_addr, s->ext_host_port,
153 s->in2out.protocol, is_twice_nat_session (s));
155 nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
156 s->ext_host_port, s->out2in.protocol, s->out2in.fib_index,
159 if (is_twice_nat_session (s))
161 for (i = 0; i < vec_len (sm->twice_nat_addresses); i++)
163 key.protocol = s->in2out.protocol;
164 key.port = s->ext_host_nat_port;
165 a = sm->twice_nat_addresses + i;
166 if (a->addr.as_u32 == s->ext_host_nat_addr.as_u32)
168 snat_free_outside_address_and_port (sm->twice_nat_addresses,
176 if (snat_is_session_static (s))
179 snat_free_outside_address_and_port (sm->addresses, ctx->thread_index,
182 nat44_ed_delete_session (sm, s, ctx->thread_index, 1);
190 static snat_session_t *
191 create_session_for_static_mapping_ed (snat_main_t * sm,
193 snat_session_key_t l_key,
194 snat_session_key_t e_key,
195 vlib_node_runtime_t * node,
197 twice_nat_type_t twice_nat,
198 lb_nat_type_t lb_nat, f64 now)
203 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
204 clib_bihash_kv_16_8_t kv;
205 snat_session_key_t eh_key;
206 nat44_is_idle_session_ctx_t ctx;
208 if (PREDICT_FALSE (nat44_maximum_sessions_exceeded (sm, thread_index)))
210 b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_SESSIONS_EXCEEDED];
211 nat_elog_notice ("maximum sessions exceeded");
215 s = nat_ed_session_alloc (sm, thread_index, now);
218 b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_USER_SESS_EXCEEDED];
219 nat_elog_warn ("create NAT session failed");
223 ip = vlib_buffer_get_current (b);
224 udp = ip4_next_header (ip);
226 s->ext_host_addr.as_u32 = ip->src_address.as_u32;
227 s->ext_host_port = e_key.protocol == SNAT_PROTOCOL_ICMP ? 0 : udp->src_port;
228 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
230 s->flags |= SNAT_SESSION_FLAG_LOAD_BALANCING;
231 if (lb_nat == AFFINITY_LB_NAT)
232 s->flags |= SNAT_SESSION_FLAG_AFFINITY;
233 s->flags |= SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT;
236 s->in2out.protocol = s->out2in.protocol;
238 /* Add to lookup tables */
239 make_ed_kv (&e_key.addr, &s->ext_host_addr, ip->protocol,
240 e_key.fib_index, e_key.port, s->ext_host_port,
241 s - tsm->sessions, &kv);
243 ctx.thread_index = thread_index;
244 if (clib_bihash_add_or_overwrite_stale_16_8 (&tsm->out2in_ed, &kv,
245 nat44_o2i_ed_is_idle_session_cb,
247 nat_elog_notice ("out2in-ed key add failed");
249 if (twice_nat == TWICE_NAT || (twice_nat == TWICE_NAT_SELF &&
250 ip->src_address.as_u32 == l_key.addr.as_u32))
252 eh_key.protocol = e_key.protocol;
253 if (snat_alloc_outside_address_and_port (sm->twice_nat_addresses, 0,
254 thread_index, &eh_key,
256 tsm->snat_thread_index))
258 b->error = node->errors[NAT_OUT2IN_ED_ERROR_OUT_OF_PORTS];
259 nat44_ed_delete_session (sm, s, thread_index, 1);
260 if (clib_bihash_add_del_16_8 (&tsm->out2in_ed, &kv, 0))
261 nat_elog_notice ("out2in-ed key del failed");
264 s->ext_host_nat_addr.as_u32 = eh_key.addr.as_u32;
265 s->ext_host_nat_port = eh_key.port;
266 s->flags |= SNAT_SESSION_FLAG_TWICE_NAT;
267 make_ed_kv (&l_key.addr, &s->ext_host_nat_addr, ip->protocol,
268 l_key.fib_index, l_key.port, s->ext_host_nat_port,
269 s - tsm->sessions, &kv);
273 make_ed_kv (&l_key.addr, &s->ext_host_addr, ip->protocol,
274 l_key.fib_index, l_key.port, s->ext_host_port,
275 s - tsm->sessions, &kv);
277 if (clib_bihash_add_or_overwrite_stale_16_8 (&tsm->in2out_ed, &kv,
278 nat44_i2o_ed_is_idle_session_cb,
280 nat_elog_notice ("in2out-ed key add failed");
282 snat_ipfix_logging_nat44_ses_create (thread_index,
283 s->in2out.addr.as_u32,
284 s->out2in.addr.as_u32,
287 s->out2in.port, s->in2out.fib_index);
289 nat_syslog_nat44_sadd (s->user_index, s->in2out.fib_index,
290 &s->in2out.addr, s->in2out.port,
291 &s->ext_host_nat_addr, s->ext_host_nat_port,
292 &s->out2in.addr, s->out2in.port,
293 &s->ext_host_addr, s->ext_host_port,
294 s->in2out.protocol, is_twice_nat_session (s));
296 nat_ha_sadd (&s->in2out.addr, s->in2out.port, &s->out2in.addr,
297 s->out2in.port, &s->ext_host_addr, s->ext_host_port,
298 &s->ext_host_nat_addr, s->ext_host_nat_port,
299 s->in2out.protocol, s->in2out.fib_index, s->flags,
306 next_src_nat (snat_main_t * sm, ip4_header_t * ip, u16 src_port,
307 u16 dst_port, u32 thread_index, u32 rx_fib_index)
309 clib_bihash_kv_16_8_t kv, value;
310 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
312 make_ed_kv (&ip->src_address, &ip->dst_address, ip->protocol,
313 rx_fib_index, src_port, dst_port, ~0ULL, &kv);
314 if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
321 create_bypass_for_fwd (snat_main_t * sm, vlib_buffer_t * b, ip4_header_t * ip,
322 u32 rx_fib_index, u32 thread_index)
324 clib_bihash_kv_16_8_t kv, value;
326 snat_session_t *s = 0;
327 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
328 f64 now = vlib_time_now (sm->vlib_main);
331 if (ip->protocol == IP_PROTOCOL_ICMP)
333 if (get_icmp_o2i_ed_key
334 (b, ip, rx_fib_index, ~0ULL, 0, &l_port, &r_port, &kv))
339 if (ip->protocol == IP_PROTOCOL_UDP || ip->protocol == IP_PROTOCOL_TCP)
341 udp = ip4_next_header (ip);
342 l_port = udp->dst_port;
343 r_port = udp->src_port;
350 make_ed_kv (&ip->dst_address, &ip->src_address, ip->protocol,
351 rx_fib_index, l_port, r_port, ~0ULL, &kv);
354 if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
356 s = pool_elt_at_index (tsm->sessions, value.value);
362 if (PREDICT_FALSE (nat44_maximum_sessions_exceeded (sm, thread_index)))
365 s = nat_ed_session_alloc (sm, thread_index, now);
368 nat_elog_warn ("create NAT session failed");
372 proto = ip_proto_to_snat_proto (ip->protocol);
374 s->ext_host_addr = ip->src_address;
375 s->ext_host_port = r_port;
376 s->flags |= SNAT_SESSION_FLAG_FWD_BYPASS;
377 s->out2in.addr = ip->dst_address;
378 s->out2in.port = l_port;
379 s->out2in.protocol = proto;
382 s->flags |= SNAT_SESSION_FLAG_UNKNOWN_PROTO;
383 s->out2in.port = ip->protocol;
385 s->out2in.fib_index = 0;
386 s->in2out = s->out2in;
388 kv.value = s - tsm->sessions;
389 if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &kv, 1))
390 nat_elog_notice ("in2out_ed key add failed");
393 if (ip->protocol == IP_PROTOCOL_TCP)
395 tcp_header_t *tcp = ip4_next_header (ip);
396 if (nat44_set_tcp_session_state_o2i
397 (sm, now, s, tcp->flags, tcp->ack_number, tcp->seq_number,
403 nat44_session_update_counters (s, now, 0, thread_index);
404 /* Per-user LRU list maintenance */
405 nat44_session_update_lru (sm, s, thread_index);
409 create_bypass_for_fwd_worker (snat_main_t * sm, vlib_buffer_t * b,
410 ip4_header_t * ip, u32 rx_fib_index)
412 ip4_header_t ip_wkr = {
413 .src_address = ip->dst_address,
415 u32 thread_index = sm->worker_in2out_cb (&ip_wkr, rx_fib_index, 0);
417 create_bypass_for_fwd (sm, b, ip, rx_fib_index, thread_index);
420 #ifndef CLIB_MARCH_VARIANT
422 icmp_match_out2in_ed (snat_main_t * sm, vlib_node_runtime_t * node,
423 u32 thread_index, vlib_buffer_t * b, ip4_header_t * ip,
424 u8 * p_proto, snat_session_key_t * p_value,
425 u8 * p_dont_translate, void *d, void *e)
427 u32 next = ~0, sw_if_index, rx_fib_index;
428 clib_bihash_kv_16_8_t kv, value;
429 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
430 snat_session_t *s = 0;
431 u8 dont_translate = 0, is_addr_only, identity_nat;
432 snat_session_key_t e_key, l_key;
435 sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
436 rx_fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
438 if (get_icmp_o2i_ed_key
439 (b, ip, rx_fib_index, ~0ULL, p_proto, &l_port, &r_port, &kv))
441 b->error = node->errors[NAT_OUT2IN_ED_ERROR_UNSUPPORTED_PROTOCOL];
442 next = NAT_NEXT_DROP;
446 if (clib_bihash_search_16_8 (&tsm->out2in_ed, &kv, &value))
448 /* Try to match static mapping */
449 e_key.addr = ip->dst_address;
451 e_key.protocol = ip_proto_to_snat_proto (ip->protocol);
452 e_key.fib_index = rx_fib_index;
453 if (snat_static_mapping_match
454 (sm, e_key, &l_key, 1, &is_addr_only, 0, 0, 0, &identity_nat))
456 if (!sm->forwarding_enabled)
458 /* Don't NAT packet aimed at the intfc address */
459 if (PREDICT_FALSE (is_interface_addr (sm, node, sw_if_index,
460 ip->dst_address.as_u32)))
465 b->error = node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
466 next = NAT_NEXT_DROP;
472 if (next_src_nat (sm, ip, l_port, r_port,
473 thread_index, rx_fib_index))
475 next = NAT_NEXT_IN2OUT_ED_FAST_PATH;
478 if (sm->num_workers > 1)
479 create_bypass_for_fwd_worker (sm, b, ip, rx_fib_index);
481 create_bypass_for_fwd (sm, b, ip, rx_fib_index, thread_index);
487 (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags !=
489 && (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags !=
490 ICMP4_echo_request || !is_addr_only)))
492 b->error = node->errors[NAT_OUT2IN_ED_ERROR_BAD_ICMP_TYPE];
493 next = NAT_NEXT_DROP;
497 if (PREDICT_FALSE (identity_nat))
503 /* Create session initiated by host from external network */
504 s = create_session_for_static_mapping_ed (sm, b, l_key, e_key, node,
511 next = NAT_NEXT_DROP;
518 (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags !=
520 && vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags !=
522 && !icmp_type_is_error_message (vnet_buffer (b)->ip.
523 reass.icmp_type_or_tcp_flags)))
525 b->error = node->errors[NAT_OUT2IN_ED_ERROR_BAD_ICMP_TYPE];
526 next = NAT_NEXT_DROP;
530 s = pool_elt_at_index (tsm->sessions, value.value);
534 *p_value = s->in2out;
535 *p_dont_translate = dont_translate;
537 *(snat_session_t **) d = s;
542 static snat_session_t *
543 nat44_ed_out2in_unknown_proto (snat_main_t * sm,
549 vlib_main_t * vm, vlib_node_runtime_t * node)
551 clib_bihash_kv_8_8_t kv, value;
552 clib_bihash_kv_16_8_t s_kv, s_value;
553 snat_static_mapping_t *m;
554 u32 old_addr, new_addr;
557 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
559 old_addr = ip->dst_address.as_u32;
561 make_ed_kv (&ip->dst_address, &ip->src_address, ip->protocol, rx_fib_index,
564 if (!clib_bihash_search_16_8 (&tsm->out2in_ed, &s_kv, &s_value))
566 s = pool_elt_at_index (tsm->sessions, s_value.value);
567 new_addr = ip->dst_address.as_u32 = s->in2out.addr.as_u32;
571 if (PREDICT_FALSE (nat44_maximum_sessions_exceeded (sm, thread_index)))
573 b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_SESSIONS_EXCEEDED];
574 nat_elog_notice ("maximum sessions exceeded");
578 make_sm_kv (&kv, &ip->dst_address, 0, 0, 0);
579 if (clib_bihash_search_8_8
580 (&sm->static_mapping_by_external, &kv, &value))
582 b->error = node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
586 m = pool_elt_at_index (sm->static_mappings, value.value);
588 new_addr = ip->dst_address.as_u32 = m->local_addr.as_u32;
590 /* Create a new session */
591 s = nat_ed_session_alloc (sm, thread_index, now);
594 b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_USER_SESS_EXCEEDED];
595 nat_elog_warn ("create NAT session failed");
599 s->ext_host_addr.as_u32 = ip->src_address.as_u32;
600 s->flags |= SNAT_SESSION_FLAG_UNKNOWN_PROTO;
601 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
602 s->flags |= SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT;
603 s->out2in.addr.as_u32 = old_addr;
604 s->out2in.fib_index = rx_fib_index;
605 s->in2out.addr.as_u32 = new_addr;
606 s->in2out.fib_index = m->fib_index;
607 s->in2out.port = s->out2in.port = ip->protocol;
609 /* Add to lookup tables */
610 s_kv.value = s - tsm->sessions;
611 if (clib_bihash_add_del_16_8 (&tsm->out2in_ed, &s_kv, 1))
612 nat_elog_notice ("out2in key add failed");
614 make_ed_kv (&ip->dst_address, &ip->src_address, ip->protocol,
615 m->fib_index, 0, 0, s - tsm->sessions, &s_kv);
616 if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &s_kv, 1))
617 nat_elog_notice ("in2out key add failed");
620 /* Update IP checksum */
622 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, dst_address);
623 ip->checksum = ip_csum_fold (sum);
625 vnet_buffer (b)->sw_if_index[VLIB_TX] = s->in2out.fib_index;
628 nat44_session_update_counters (s, now, vlib_buffer_length_in_chain (vm, b),
630 /* Per-user LRU list maintenance */
631 nat44_session_update_lru (sm, s, thread_index);
637 nat44_ed_out2in_fast_path_node_fn_inline (vlib_main_t * vm,
638 vlib_node_runtime_t * node,
639 vlib_frame_t * frame)
641 u32 n_left_from, *from, *to_next, pkts_processed = 0, stats_node_index;
642 nat_next_t next_index;
643 snat_main_t *sm = &snat_main;
644 f64 now = vlib_time_now (vm);
645 u32 thread_index = vm->thread_index;
646 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
647 u32 tcp_packets = 0, udp_packets = 0, icmp_packets = 0, other_packets =
650 stats_node_index = sm->ed_out2in_node_index;
652 from = vlib_frame_vector_args (frame);
653 n_left_from = frame->n_vectors;
654 next_index = node->cached_next_index;
656 while (n_left_from > 0)
660 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
662 while (n_left_from > 0 && n_left_to_next > 0)
666 u32 next0, sw_if_index0, rx_fib_index0, proto0, old_addr0,
668 u16 old_port0, new_port0;
672 snat_session_t *s0 = 0;
673 clib_bihash_kv_16_8_t kv0, value0;
676 /* speculatively enqueue b0 to the current next frame */
684 b0 = vlib_get_buffer (vm, bi0);
685 next0 = vnet_buffer2 (b0)->nat.arc_next;
687 vnet_buffer (b0)->snat.flags = 0;
688 ip0 = vlib_buffer_get_current (b0);
690 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
692 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
695 if (PREDICT_FALSE (ip0->ttl == 1))
697 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
698 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
699 ICMP4_time_exceeded_ttl_exceeded_in_transit,
701 next0 = NAT_NEXT_ICMP_ERROR;
705 udp0 = ip4_next_header (ip0);
706 tcp0 = (tcp_header_t *) udp0;
707 proto0 = ip_proto_to_snat_proto (ip0->protocol);
709 if (PREDICT_FALSE (proto0 == ~0))
711 next0 = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
715 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
717 next0 = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
721 make_ed_kv (&ip0->dst_address, &ip0->src_address,
722 ip0->protocol, rx_fib_index0,
723 vnet_buffer (b0)->ip.reass.l4_dst_port,
724 vnet_buffer (b0)->ip.reass.l4_src_port, ~0ULL, &kv0);
726 if (clib_bihash_search_16_8 (&tsm->out2in_ed, &kv0, &value0))
728 next0 = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
731 s0 = pool_elt_at_index (tsm->sessions, value0.value);
733 if (s0->tcp_close_timestamp)
735 if (now >= s0->tcp_close_timestamp)
737 // session is closed, go slow path
738 next0 = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
742 // session in transitory timeout, drop
743 b0->error = node->errors[NAT_OUT2IN_ED_ERROR_TCP_CLOSED];
744 next0 = NAT_NEXT_DROP;
749 // drop if session expired
750 u64 sess_timeout_time;
751 sess_timeout_time = s0->last_heard +
752 (f64) nat44_session_get_timeout (sm, s0);
753 if (now >= sess_timeout_time)
755 // session is closed, go slow path
756 nat_free_session_data (sm, s0, thread_index, 0);
757 nat44_ed_delete_session (sm, s0, thread_index, 1);
758 next0 = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
763 old_addr0 = ip0->dst_address.as_u32;
764 new_addr0 = ip0->dst_address.as_u32 = s0->in2out.addr.as_u32;
765 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
767 sum0 = ip0->checksum;
768 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
770 if (PREDICT_FALSE (is_twice_nat_session (s0)))
771 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
772 s0->ext_host_nat_addr.as_u32, ip4_header_t,
774 ip0->checksum = ip_csum_fold (sum0);
776 old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
778 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
780 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
782 new_port0 = udp0->dst_port = s0->in2out.port;
783 sum0 = tcp0->checksum;
785 ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
788 ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
790 if (is_twice_nat_session (s0))
792 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
793 s0->ext_host_nat_addr.as_u32,
794 ip4_header_t, dst_address);
796 ip_csum_update (sum0,
797 vnet_buffer (b0)->ip.
799 s0->ext_host_nat_port, ip4_header_t,
801 tcp0->src_port = s0->ext_host_nat_port;
802 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
804 tcp0->checksum = ip_csum_fold (sum0);
807 if (nat44_set_tcp_session_state_o2i
809 vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags,
810 vnet_buffer (b0)->ip.reass.tcp_ack_number,
811 vnet_buffer (b0)->ip.reass.tcp_seq_number, thread_index))
814 else if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment
817 new_port0 = udp0->dst_port = s0->in2out.port;
818 sum0 = udp0->checksum;
819 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
821 sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
823 if (PREDICT_FALSE (is_twice_nat_session (s0)))
825 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
826 s0->ext_host_nat_addr.as_u32,
827 ip4_header_t, dst_address);
829 ip_csum_update (sum0,
830 vnet_buffer (b0)->ip.reass.l4_src_port,
831 s0->ext_host_nat_port, ip4_header_t,
833 udp0->src_port = s0->ext_host_nat_port;
834 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
836 udp0->checksum = ip_csum_fold (sum0);
841 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
843 new_port0 = udp0->dst_port = s0->in2out.port;
844 if (PREDICT_FALSE (is_twice_nat_session (s0)))
846 udp0->src_port = s0->ext_host_nat_port;
847 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
854 nat44_session_update_counters (s0, now,
855 vlib_buffer_length_in_chain (vm, b0),
857 /* Per-user LRU list maintenance */
858 nat44_session_update_lru (sm, s0, thread_index);
861 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
862 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
864 nat44_ed_out2in_trace_t *t =
865 vlib_add_trace (vm, node, b0, sizeof (*t));
866 t->sw_if_index = sw_if_index0;
867 t->next_index = next0;
871 t->session_index = s0 - tsm->sessions;
873 t->session_index = ~0;
876 pkts_processed += next0 == vnet_buffer2 (b0)->nat.arc_next;
877 /* verify speculative enqueue, maybe switch current next frame */
878 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
879 to_next, n_left_to_next,
883 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
886 vlib_node_increment_counter (vm, stats_node_index,
887 NAT_OUT2IN_ED_ERROR_OUT2IN_PACKETS,
889 vlib_node_increment_counter (vm, stats_node_index,
890 NAT_OUT2IN_ED_ERROR_TCP_PACKETS, tcp_packets);
891 vlib_node_increment_counter (vm, stats_node_index,
892 NAT_OUT2IN_ED_ERROR_UDP_PACKETS, udp_packets);
893 vlib_node_increment_counter (vm, stats_node_index,
894 NAT_OUT2IN_ED_ERROR_ICMP_PACKETS,
896 vlib_node_increment_counter (vm, stats_node_index,
897 NAT_OUT2IN_ED_ERROR_OTHER_PACKETS,
899 vlib_node_increment_counter (vm, stats_node_index,
900 NAT_OUT2IN_ED_ERROR_FRAGMENTS, fragments);
901 return frame->n_vectors;
905 nat44_ed_out2in_slow_path_node_fn_inline (vlib_main_t * vm,
906 vlib_node_runtime_t * node,
907 vlib_frame_t * frame)
909 u32 n_left_from, *from, *to_next, pkts_processed = 0, stats_node_index;
910 nat_next_t next_index;
911 snat_main_t *sm = &snat_main;
912 f64 now = vlib_time_now (vm);
913 u32 thread_index = vm->thread_index;
914 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
915 u32 tcp_packets = 0, udp_packets = 0, icmp_packets = 0, other_packets =
918 stats_node_index = sm->ed_out2in_slowpath_node_index;
920 from = vlib_frame_vector_args (frame);
921 n_left_from = frame->n_vectors;
922 next_index = node->cached_next_index;
924 while (n_left_from > 0)
928 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
930 while (n_left_from > 0 && n_left_to_next > 0)
934 u32 next0, sw_if_index0, rx_fib_index0, proto0, old_addr0,
936 u16 old_port0, new_port0;
940 icmp46_header_t *icmp0;
941 snat_session_t *s0 = 0;
942 clib_bihash_kv_16_8_t kv0, value0;
944 snat_session_key_t e_key0, l_key0;
945 lb_nat_type_t lb_nat0;
946 twice_nat_type_t twice_nat0;
949 /* speculatively enqueue b0 to the current next frame */
957 b0 = vlib_get_buffer (vm, bi0);
958 next0 = vnet_buffer2 (b0)->nat.arc_next;
960 vnet_buffer (b0)->snat.flags = 0;
961 ip0 = vlib_buffer_get_current (b0);
963 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
965 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
968 if (PREDICT_FALSE (ip0->ttl == 1))
970 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
971 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
972 ICMP4_time_exceeded_ttl_exceeded_in_transit,
974 next0 = NAT_NEXT_ICMP_ERROR;
978 udp0 = ip4_next_header (ip0);
979 tcp0 = (tcp_header_t *) udp0;
980 icmp0 = (icmp46_header_t *) udp0;
981 proto0 = ip_proto_to_snat_proto (ip0->protocol);
983 if (PREDICT_FALSE (proto0 == ~0))
986 nat44_ed_out2in_unknown_proto (sm, b0, ip0, rx_fib_index0,
987 thread_index, now, vm, node);
988 if (!sm->forwarding_enabled)
991 next0 = NAT_NEXT_DROP;
997 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
999 next0 = icmp_out2in_ed_slow_path
1000 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1001 next0, now, thread_index, &s0);
1006 make_ed_kv (&ip0->dst_address, &ip0->src_address,
1007 ip0->protocol, rx_fib_index0,
1008 vnet_buffer (b0)->ip.reass.l4_dst_port,
1009 vnet_buffer (b0)->ip.reass.l4_src_port, ~0ULL, &kv0);
1012 if (!clib_bihash_search_16_8 (&tsm->out2in_ed, &kv0, &value0))
1014 s0 = pool_elt_at_index (tsm->sessions, value0.value);
1016 if (s0->tcp_close_timestamp && now >= s0->tcp_close_timestamp)
1018 nat_free_session_data (sm, s0, thread_index, 0);
1019 nat44_ed_delete_session (sm, s0, thread_index, 1);
1026 /* Try to match static mapping by external address and port,
1027 destination address and port in packet */
1028 e_key0.addr = ip0->dst_address;
1029 e_key0.port = vnet_buffer (b0)->ip.reass.l4_dst_port;
1030 e_key0.protocol = proto0;
1031 e_key0.fib_index = rx_fib_index0;
1033 if (snat_static_mapping_match (sm, e_key0, &l_key0, 1, 0,
1034 &twice_nat0, &lb_nat0,
1039 * Send DHCP packets to the ipv4 stack, or we won't
1040 * be able to use dhcp client on the outside interface
1042 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_UDP
1043 && (vnet_buffer (b0)->ip.
1044 reass.l4_dst_port ==
1045 clib_host_to_net_u16
1046 (UDP_DST_PORT_dhcp_to_client))))
1051 if (!sm->forwarding_enabled)
1054 node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
1055 next0 = NAT_NEXT_DROP;
1060 (sm, ip0, vnet_buffer (b0)->ip.reass.l4_src_port,
1061 vnet_buffer (b0)->ip.reass.l4_dst_port,
1062 thread_index, rx_fib_index0))
1064 next0 = NAT_NEXT_IN2OUT_ED_FAST_PATH;
1067 if (sm->num_workers > 1)
1068 create_bypass_for_fwd_worker (sm, b0, ip0,
1071 create_bypass_for_fwd (sm, b0, ip0, rx_fib_index0,
1077 if (PREDICT_FALSE (identity_nat0))
1080 if ((proto0 == SNAT_PROTOCOL_TCP)
1081 && !tcp_flags_is_init (vnet_buffer (b0)->ip.
1082 reass.icmp_type_or_tcp_flags))
1084 b0->error = node->errors[NAT_OUT2IN_ED_ERROR_NON_SYN];
1085 next0 = NAT_NEXT_DROP;
1089 /* Create session initiated by host from external network */
1090 s0 = create_session_for_static_mapping_ed (sm, b0, l_key0,
1097 next0 = NAT_NEXT_DROP;
1102 old_addr0 = ip0->dst_address.as_u32;
1103 new_addr0 = ip0->dst_address.as_u32 = s0->in2out.addr.as_u32;
1104 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
1106 sum0 = ip0->checksum;
1107 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1109 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1110 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1111 s0->ext_host_nat_addr.as_u32, ip4_header_t,
1113 ip0->checksum = ip_csum_fold (sum0);
1115 old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
1117 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1119 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1121 new_port0 = udp0->dst_port = s0->in2out.port;
1122 sum0 = tcp0->checksum;
1124 ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1127 ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
1129 if (is_twice_nat_session (s0))
1131 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1132 s0->ext_host_nat_addr.as_u32,
1133 ip4_header_t, dst_address);
1135 ip_csum_update (sum0,
1136 vnet_buffer (b0)->ip.
1138 s0->ext_host_nat_port, ip4_header_t,
1140 tcp0->src_port = s0->ext_host_nat_port;
1141 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1143 tcp0->checksum = ip_csum_fold (sum0);
1146 if (nat44_set_tcp_session_state_o2i
1148 vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags,
1149 vnet_buffer (b0)->ip.reass.tcp_ack_number,
1150 vnet_buffer (b0)->ip.reass.tcp_seq_number, thread_index))
1153 else if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment
1156 new_port0 = udp0->dst_port = s0->in2out.port;
1157 sum0 = udp0->checksum;
1158 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1160 sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
1162 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1164 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1165 s0->ext_host_nat_addr.as_u32,
1166 ip4_header_t, dst_address);
1168 ip_csum_update (sum0,
1169 vnet_buffer (b0)->ip.reass.l4_src_port,
1170 s0->ext_host_nat_port, ip4_header_t,
1172 udp0->src_port = s0->ext_host_nat_port;
1173 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1175 udp0->checksum = ip_csum_fold (sum0);
1180 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1182 new_port0 = udp0->dst_port = s0->in2out.port;
1183 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1185 udp0->src_port = s0->ext_host_nat_port;
1186 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1193 nat44_session_update_counters (s0, now,
1194 vlib_buffer_length_in_chain (vm, b0),
1196 /* Per-user LRU list maintenance */
1197 nat44_session_update_lru (sm, s0, thread_index);
1200 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1201 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1203 nat44_ed_out2in_trace_t *t =
1204 vlib_add_trace (vm, node, b0, sizeof (*t));
1205 t->sw_if_index = sw_if_index0;
1206 t->next_index = next0;
1207 t->is_slow_path = 1;
1210 t->session_index = s0 - tsm->sessions;
1212 t->session_index = ~0;
1215 pkts_processed += next0 == vnet_buffer2 (b0)->nat.arc_next;
1216 /* verify speculative enqueue, maybe switch current next frame */
1217 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1218 to_next, n_left_to_next,
1222 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1225 vlib_node_increment_counter (vm, stats_node_index,
1226 NAT_OUT2IN_ED_ERROR_OUT2IN_PACKETS,
1228 vlib_node_increment_counter (vm, stats_node_index,
1229 NAT_OUT2IN_ED_ERROR_TCP_PACKETS, tcp_packets);
1230 vlib_node_increment_counter (vm, stats_node_index,
1231 NAT_OUT2IN_ED_ERROR_UDP_PACKETS, udp_packets);
1232 vlib_node_increment_counter (vm, stats_node_index,
1233 NAT_OUT2IN_ED_ERROR_ICMP_PACKETS,
1235 vlib_node_increment_counter (vm, stats_node_index,
1236 NAT_OUT2IN_ED_ERROR_OTHER_PACKETS,
1238 vlib_node_increment_counter (vm, stats_node_index,
1239 NAT_OUT2IN_ED_ERROR_FRAGMENTS, fragments);
1240 return frame->n_vectors;
1243 VLIB_NODE_FN (nat44_ed_out2in_node) (vlib_main_t * vm,
1244 vlib_node_runtime_t * node,
1245 vlib_frame_t * frame)
1247 return nat44_ed_out2in_fast_path_node_fn_inline (vm, node, frame);
1251 VLIB_REGISTER_NODE (nat44_ed_out2in_node) = {
1252 .name = "nat44-ed-out2in",
1253 .vector_size = sizeof (u32),
1254 .sibling_of = "nat-default",
1255 .format_trace = format_nat44_ed_out2in_trace,
1256 .type = VLIB_NODE_TYPE_INTERNAL,
1257 .n_errors = ARRAY_LEN(nat_out2in_ed_error_strings),
1258 .error_strings = nat_out2in_ed_error_strings,
1259 .runtime_data_bytes = sizeof (snat_runtime_t),
1263 VLIB_NODE_FN (nat44_ed_out2in_slowpath_node) (vlib_main_t * vm,
1264 vlib_node_runtime_t * node,
1265 vlib_frame_t * frame)
1267 return nat44_ed_out2in_slow_path_node_fn_inline (vm, node, frame);
1271 VLIB_REGISTER_NODE (nat44_ed_out2in_slowpath_node) = {
1272 .name = "nat44-ed-out2in-slowpath",
1273 .vector_size = sizeof (u32),
1274 .sibling_of = "nat-default",
1275 .format_trace = format_nat44_ed_out2in_trace,
1276 .type = VLIB_NODE_TYPE_INTERNAL,
1277 .n_errors = ARRAY_LEN(nat_out2in_ed_error_strings),
1278 .error_strings = nat_out2in_ed_error_strings,
1279 .runtime_data_bytes = sizeof (snat_runtime_t),
1284 format_nat_pre_trace (u8 * s, va_list * args)
1286 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1287 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1288 nat_pre_trace_t *t = va_arg (*args, nat_pre_trace_t *);
1289 return format (s, "out2in next_index %d", t->next_index);
1292 VLIB_NODE_FN (nat_pre_out2in_node) (vlib_main_t * vm,
1293 vlib_node_runtime_t * node,
1294 vlib_frame_t * frame)
1296 return nat_pre_node_fn_inline (vm, node, frame,
1297 NAT_NEXT_OUT2IN_ED_FAST_PATH);
1301 VLIB_REGISTER_NODE (nat_pre_out2in_node) = {
1302 .name = "nat-pre-out2in",
1303 .vector_size = sizeof (u32),
1304 .sibling_of = "nat-default",
1305 .format_trace = format_nat_pre_trace,
1306 .type = VLIB_NODE_TYPE_INTERNAL,
1312 * fd.io coding-style-patch-verification: ON
1315 * eval: (c-set-style "gnu")