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 vlib_main_t *vm = vlib_get_main ();
76 next0 = icmp_out2in (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
77 next0, thread_index, p_s0, 0);
78 snat_session_t *s0 = *p_s0;
79 if (PREDICT_TRUE (next0 != NAT_NEXT_DROP && s0))
82 nat44_session_update_counters (s0, now,
83 vlib_buffer_length_in_chain
84 (vm, b0), thread_index);
85 /* Per-user LRU list maintenance */
86 nat44_session_update_lru (sm, s0, thread_index);
91 #ifndef CLIB_MARCH_VARIANT
93 nat44_o2i_ed_is_idle_session_cb (clib_bihash_kv_16_8_t * kv, void *arg)
95 snat_main_t *sm = &snat_main;
96 nat44_is_idle_session_ctx_t *ctx = arg;
98 u64 sess_timeout_time;
101 ip4_address_t *l_addr, *r_addr;
103 clib_bihash_kv_16_8_t ed_kv;
106 snat_session_key_t key;
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->in2out.protocol);
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 make_ed_kv (l_addr, r_addr, proto, fib_index, l_port, r_port, ~0ULL,
136 if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &ed_kv, 0))
137 nat_elog_warn ("in2out_ed key del failed");
139 if (snat_is_unk_proto_session (s))
142 snat_ipfix_logging_nat44_ses_delete (ctx->thread_index,
143 s->in2out.addr.as_u32,
144 s->out2in.addr.as_u32,
148 s->in2out.fib_index);
150 nat_syslog_nat44_sdel (s->user_index, s->in2out.fib_index,
151 &s->in2out.addr, s->in2out.port,
152 &s->ext_host_nat_addr, s->ext_host_nat_port,
153 &s->out2in.addr, s->out2in.port,
154 &s->ext_host_addr, s->ext_host_port,
155 s->in2out.protocol, is_twice_nat_session (s));
157 nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
158 s->ext_host_port, s->out2in.protocol, s->out2in.fib_index,
161 if (is_twice_nat_session (s))
163 for (i = 0; i < vec_len (sm->twice_nat_addresses); i++)
165 key.protocol = s->in2out.protocol;
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,
178 if (snat_is_session_static (s))
181 snat_free_outside_address_and_port (sm->addresses, ctx->thread_index,
184 nat44_ed_delete_session (sm, s, ctx->thread_index, 1);
192 static snat_session_t *
193 create_session_for_static_mapping_ed (snat_main_t * sm,
195 snat_session_key_t l_key,
196 snat_session_key_t e_key,
197 vlib_node_runtime_t * node,
200 twice_nat_type_t twice_nat,
201 lb_nat_type_t lb_nat, f64 now)
206 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
207 clib_bihash_kv_16_8_t kv;
208 snat_session_key_t eh_key;
209 nat44_is_idle_session_ctx_t ctx;
212 (nat44_ed_maximum_sessions_exceeded (sm, rx_fib_index, thread_index)))
214 b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_SESSIONS_EXCEEDED];
215 nat_elog_notice ("maximum sessions exceeded");
219 s = nat_ed_session_alloc (sm, thread_index, now);
222 b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_USER_SESS_EXCEEDED];
223 nat_elog_warn ("create NAT session failed");
227 ip = vlib_buffer_get_current (b);
228 udp = ip4_next_header (ip);
230 s->ext_host_addr.as_u32 = ip->src_address.as_u32;
231 s->ext_host_port = e_key.protocol == NAT_PROTOCOL_ICMP ? 0 : udp->src_port;
232 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
234 s->flags |= SNAT_SESSION_FLAG_LOAD_BALANCING;
235 if (lb_nat == AFFINITY_LB_NAT)
236 s->flags |= SNAT_SESSION_FLAG_AFFINITY;
237 s->flags |= SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT;
240 s->in2out.protocol = s->out2in.protocol;
242 /* Add to lookup tables */
243 make_ed_kv (&e_key.addr, &s->ext_host_addr, ip->protocol,
244 e_key.fib_index, e_key.port, s->ext_host_port,
245 s - tsm->sessions, &kv);
247 ctx.thread_index = thread_index;
248 if (clib_bihash_add_or_overwrite_stale_16_8 (&tsm->out2in_ed, &kv,
249 nat44_o2i_ed_is_idle_session_cb,
251 nat_elog_notice ("out2in-ed key add failed");
253 if (twice_nat == TWICE_NAT || (twice_nat == TWICE_NAT_SELF &&
254 ip->src_address.as_u32 == l_key.addr.as_u32))
256 eh_key.protocol = e_key.protocol;
257 if (snat_alloc_outside_address_and_port (sm->twice_nat_addresses, 0,
258 thread_index, &eh_key,
260 tsm->snat_thread_index))
262 b->error = node->errors[NAT_OUT2IN_ED_ERROR_OUT_OF_PORTS];
263 nat44_ed_delete_session (sm, s, thread_index, 1);
264 if (clib_bihash_add_del_16_8 (&tsm->out2in_ed, &kv, 0))
265 nat_elog_notice ("out2in-ed key del failed");
268 s->ext_host_nat_addr.as_u32 = eh_key.addr.as_u32;
269 s->ext_host_nat_port = eh_key.port;
270 s->flags |= SNAT_SESSION_FLAG_TWICE_NAT;
271 make_ed_kv (&l_key.addr, &s->ext_host_nat_addr, ip->protocol,
272 l_key.fib_index, l_key.port, s->ext_host_nat_port,
273 s - tsm->sessions, &kv);
277 make_ed_kv (&l_key.addr, &s->ext_host_addr, ip->protocol,
278 l_key.fib_index, l_key.port, s->ext_host_port,
279 s - tsm->sessions, &kv);
281 if (clib_bihash_add_or_overwrite_stale_16_8 (&tsm->in2out_ed, &kv,
282 nat44_i2o_ed_is_idle_session_cb,
284 nat_elog_notice ("in2out-ed key add failed");
286 snat_ipfix_logging_nat44_ses_create (thread_index,
287 s->in2out.addr.as_u32,
288 s->out2in.addr.as_u32,
291 s->out2in.port, s->in2out.fib_index);
293 nat_syslog_nat44_sadd (s->user_index, s->in2out.fib_index,
294 &s->in2out.addr, s->in2out.port,
295 &s->ext_host_nat_addr, s->ext_host_nat_port,
296 &s->out2in.addr, s->out2in.port,
297 &s->ext_host_addr, s->ext_host_port,
298 s->in2out.protocol, is_twice_nat_session (s));
300 nat_ha_sadd (&s->in2out.addr, s->in2out.port, &s->out2in.addr,
301 s->out2in.port, &s->ext_host_addr, s->ext_host_port,
302 &s->ext_host_nat_addr, s->ext_host_nat_port,
303 s->in2out.protocol, s->in2out.fib_index, s->flags,
310 next_src_nat (snat_main_t * sm, ip4_header_t * ip, u16 src_port,
311 u16 dst_port, u32 thread_index, u32 rx_fib_index)
313 clib_bihash_kv_16_8_t kv, value;
314 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
316 make_ed_kv (&ip->src_address, &ip->dst_address, ip->protocol,
317 rx_fib_index, src_port, dst_port, ~0ULL, &kv);
318 if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
325 create_bypass_for_fwd (snat_main_t * sm, vlib_buffer_t * b, ip4_header_t * ip,
326 u32 rx_fib_index, u32 thread_index)
328 clib_bihash_kv_16_8_t kv, value;
330 snat_session_t *s = 0;
331 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
332 vlib_main_t *vm = vlib_get_main ();
333 f64 now = vlib_time_now (vm);
336 if (ip->protocol == IP_PROTOCOL_ICMP)
338 if (get_icmp_o2i_ed_key
339 (b, ip, rx_fib_index, ~0ULL, 0, &l_port, &r_port, &kv))
344 if (ip->protocol == IP_PROTOCOL_UDP || ip->protocol == IP_PROTOCOL_TCP)
346 udp = ip4_next_header (ip);
347 l_port = udp->dst_port;
348 r_port = udp->src_port;
355 make_ed_kv (&ip->dst_address, &ip->src_address, ip->protocol,
356 rx_fib_index, l_port, r_port, ~0ULL, &kv);
359 if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
361 s = pool_elt_at_index (tsm->sessions, value.value);
368 (nat44_ed_maximum_sessions_exceeded
369 (sm, rx_fib_index, thread_index)))
372 s = nat_ed_session_alloc (sm, thread_index, now);
375 nat_elog_warn ("create NAT session failed");
379 proto = ip_proto_to_nat_proto (ip->protocol);
381 s->ext_host_addr = ip->src_address;
382 s->ext_host_port = r_port;
383 s->flags |= SNAT_SESSION_FLAG_FWD_BYPASS;
384 s->out2in.addr = ip->dst_address;
385 s->out2in.port = l_port;
386 s->out2in.protocol = proto;
387 if (proto == NAT_PROTOCOL_OTHER)
389 s->flags |= SNAT_SESSION_FLAG_UNKNOWN_PROTO;
390 s->out2in.port = ip->protocol;
392 s->out2in.fib_index = 0;
393 s->in2out = s->out2in;
395 kv.value = s - tsm->sessions;
396 if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &kv, 1))
397 nat_elog_notice ("in2out_ed key add failed");
400 if (ip->protocol == IP_PROTOCOL_TCP)
402 tcp_header_t *tcp = ip4_next_header (ip);
403 if (nat44_set_tcp_session_state_o2i
404 (sm, now, s, tcp->flags, tcp->ack_number, tcp->seq_number,
410 nat44_session_update_counters (s, now, 0, thread_index);
411 /* Per-user LRU list maintenance */
412 nat44_session_update_lru (sm, s, thread_index);
416 create_bypass_for_fwd_worker (snat_main_t * sm, vlib_buffer_t * b,
417 ip4_header_t * ip, u32 rx_fib_index)
419 ip4_header_t ip_wkr = {
420 .src_address = ip->dst_address,
422 u32 thread_index = sm->worker_in2out_cb (&ip_wkr, rx_fib_index, 0);
424 create_bypass_for_fwd (sm, b, ip, rx_fib_index, thread_index);
427 #ifndef CLIB_MARCH_VARIANT
429 icmp_match_out2in_ed (snat_main_t * sm, vlib_node_runtime_t * node,
430 u32 thread_index, vlib_buffer_t * b, ip4_header_t * ip,
431 u8 * p_proto, snat_session_key_t * p_value,
432 u8 * p_dont_translate, void *d, void *e)
434 u32 next = ~0, sw_if_index, rx_fib_index;
435 clib_bihash_kv_16_8_t kv, value;
436 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
437 snat_session_t *s = 0;
438 u8 dont_translate = 0, is_addr_only, identity_nat;
439 snat_session_key_t e_key, l_key;
441 vlib_main_t *vm = vlib_get_main ();
443 sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
444 rx_fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
446 if (get_icmp_o2i_ed_key
447 (b, ip, rx_fib_index, ~0ULL, p_proto, &l_port, &r_port, &kv))
449 b->error = node->errors[NAT_OUT2IN_ED_ERROR_UNSUPPORTED_PROTOCOL];
450 next = NAT_NEXT_DROP;
454 if (clib_bihash_search_16_8 (&tsm->out2in_ed, &kv, &value))
456 /* Try to match static mapping */
457 e_key.addr = ip->dst_address;
459 e_key.protocol = ip_proto_to_nat_proto (ip->protocol);
460 e_key.fib_index = rx_fib_index;
461 if (snat_static_mapping_match
462 (sm, e_key, &l_key, 1, &is_addr_only, 0, 0, 0, &identity_nat))
464 if (!sm->forwarding_enabled)
466 /* Don't NAT packet aimed at the intfc address */
467 if (PREDICT_FALSE (is_interface_addr (sm, node, sw_if_index,
468 ip->dst_address.as_u32)))
473 b->error = node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
474 next = NAT_NEXT_DROP;
480 if (next_src_nat (sm, ip, l_port, r_port,
481 thread_index, rx_fib_index))
483 next = NAT_NEXT_IN2OUT_ED_FAST_PATH;
486 if (sm->num_workers > 1)
487 create_bypass_for_fwd_worker (sm, b, ip, rx_fib_index);
489 create_bypass_for_fwd (sm, b, ip, rx_fib_index, thread_index);
495 (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags !=
497 && (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags !=
498 ICMP4_echo_request || !is_addr_only)))
500 b->error = node->errors[NAT_OUT2IN_ED_ERROR_BAD_ICMP_TYPE];
501 next = NAT_NEXT_DROP;
505 if (PREDICT_FALSE (identity_nat))
511 /* Create session initiated by host from external network */
512 s = create_session_for_static_mapping_ed (sm, b, l_key, e_key, node,
513 rx_fib_index, thread_index, 0,
514 0, vlib_time_now (vm));
518 next = NAT_NEXT_DROP;
525 (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags !=
527 && vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags !=
529 && !icmp_type_is_error_message (vnet_buffer (b)->ip.
530 reass.icmp_type_or_tcp_flags)))
532 b->error = node->errors[NAT_OUT2IN_ED_ERROR_BAD_ICMP_TYPE];
533 next = NAT_NEXT_DROP;
537 s = pool_elt_at_index (tsm->sessions, value.value);
541 *p_value = s->in2out;
542 *p_dont_translate = dont_translate;
544 *(snat_session_t **) d = s;
549 static snat_session_t *
550 nat44_ed_out2in_unknown_proto (snat_main_t * sm,
556 vlib_main_t * vm, vlib_node_runtime_t * node)
558 clib_bihash_kv_8_8_t kv, value;
559 clib_bihash_kv_16_8_t s_kv, s_value;
560 snat_static_mapping_t *m;
561 u32 old_addr, new_addr;
564 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
566 old_addr = ip->dst_address.as_u32;
568 make_ed_kv (&ip->dst_address, &ip->src_address, ip->protocol, rx_fib_index,
571 if (!clib_bihash_search_16_8 (&tsm->out2in_ed, &s_kv, &s_value))
573 s = pool_elt_at_index (tsm->sessions, s_value.value);
574 new_addr = ip->dst_address.as_u32 = s->in2out.addr.as_u32;
579 (nat44_ed_maximum_sessions_exceeded
580 (sm, rx_fib_index, thread_index)))
582 b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_SESSIONS_EXCEEDED];
583 nat_elog_notice ("maximum sessions exceeded");
587 make_sm_kv (&kv, &ip->dst_address, 0, 0, 0);
588 if (clib_bihash_search_8_8
589 (&sm->static_mapping_by_external, &kv, &value))
591 b->error = node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
595 m = pool_elt_at_index (sm->static_mappings, value.value);
597 new_addr = ip->dst_address.as_u32 = m->local_addr.as_u32;
599 /* Create a new session */
600 s = nat_ed_session_alloc (sm, thread_index, now);
603 b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_USER_SESS_EXCEEDED];
604 nat_elog_warn ("create NAT session failed");
608 s->ext_host_addr.as_u32 = ip->src_address.as_u32;
609 s->flags |= SNAT_SESSION_FLAG_UNKNOWN_PROTO;
610 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
611 s->flags |= SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT;
612 s->out2in.addr.as_u32 = old_addr;
613 s->out2in.fib_index = rx_fib_index;
614 s->in2out.addr.as_u32 = new_addr;
615 s->in2out.fib_index = m->fib_index;
616 s->in2out.port = s->out2in.port = ip->protocol;
618 /* Add to lookup tables */
619 s_kv.value = s - tsm->sessions;
620 if (clib_bihash_add_del_16_8 (&tsm->out2in_ed, &s_kv, 1))
621 nat_elog_notice ("out2in key add failed");
623 make_ed_kv (&ip->dst_address, &ip->src_address, ip->protocol,
624 m->fib_index, 0, 0, s - tsm->sessions, &s_kv);
625 if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &s_kv, 1))
626 nat_elog_notice ("in2out key add failed");
629 /* Update IP checksum */
631 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, dst_address);
632 ip->checksum = ip_csum_fold (sum);
634 vnet_buffer (b)->sw_if_index[VLIB_TX] = s->in2out.fib_index;
637 nat44_session_update_counters (s, now, vlib_buffer_length_in_chain (vm, b),
639 /* Per-user LRU list maintenance */
640 nat44_session_update_lru (sm, s, thread_index);
646 nat44_ed_out2in_fast_path_node_fn_inline (vlib_main_t * vm,
647 vlib_node_runtime_t * node,
648 vlib_frame_t * frame)
650 u32 n_left_from, *from, *to_next, pkts_processed = 0, stats_node_index;
651 nat_next_t next_index;
652 snat_main_t *sm = &snat_main;
653 f64 now = vlib_time_now (vm);
654 u32 thread_index = vm->thread_index;
655 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
656 u32 tcp_packets = 0, udp_packets = 0, icmp_packets = 0, other_packets =
659 stats_node_index = sm->ed_out2in_node_index;
661 from = vlib_frame_vector_args (frame);
662 n_left_from = frame->n_vectors;
663 next_index = node->cached_next_index;
665 while (n_left_from > 0)
669 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
671 while (n_left_from > 0 && n_left_to_next > 0)
675 u32 next0, sw_if_index0, rx_fib_index0, proto0, old_addr0,
677 u16 old_port0, new_port0;
681 snat_session_t *s0 = 0;
682 clib_bihash_kv_16_8_t kv0, value0;
685 /* speculatively enqueue b0 to the current next frame */
693 b0 = vlib_get_buffer (vm, bi0);
694 next0 = vnet_buffer2 (b0)->nat.arc_next;
696 vnet_buffer (b0)->snat.flags = 0;
697 ip0 = vlib_buffer_get_current (b0);
699 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
701 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
704 if (PREDICT_FALSE (ip0->ttl == 1))
706 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
707 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
708 ICMP4_time_exceeded_ttl_exceeded_in_transit,
710 next0 = NAT_NEXT_ICMP_ERROR;
714 udp0 = ip4_next_header (ip0);
715 tcp0 = (tcp_header_t *) udp0;
716 proto0 = ip_proto_to_nat_proto (ip0->protocol);
718 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
720 next0 = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
724 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
726 next0 = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
730 make_ed_kv (&ip0->dst_address, &ip0->src_address,
731 ip0->protocol, rx_fib_index0,
732 vnet_buffer (b0)->ip.reass.l4_dst_port,
733 vnet_buffer (b0)->ip.reass.l4_src_port, ~0ULL, &kv0);
735 if (clib_bihash_search_16_8 (&tsm->out2in_ed, &kv0, &value0))
737 next0 = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
740 s0 = pool_elt_at_index (tsm->sessions, value0.value);
742 if (s0->tcp_close_timestamp)
744 if (now >= s0->tcp_close_timestamp)
746 // session is closed, go slow path
747 next0 = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
751 // session in transitory timeout, drop
752 b0->error = node->errors[NAT_OUT2IN_ED_ERROR_TCP_CLOSED];
753 next0 = NAT_NEXT_DROP;
758 // drop if session expired
759 u64 sess_timeout_time;
760 sess_timeout_time = s0->last_heard +
761 (f64) nat44_session_get_timeout (sm, s0);
762 if (now >= sess_timeout_time)
764 // session is closed, go slow path
765 nat_free_session_data (sm, s0, thread_index, 0);
766 nat44_ed_delete_session (sm, s0, thread_index, 1);
767 next0 = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
772 old_addr0 = ip0->dst_address.as_u32;
773 new_addr0 = ip0->dst_address.as_u32 = s0->in2out.addr.as_u32;
774 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
776 sum0 = ip0->checksum;
777 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
779 if (PREDICT_FALSE (is_twice_nat_session (s0)))
780 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
781 s0->ext_host_nat_addr.as_u32, ip4_header_t,
783 ip0->checksum = ip_csum_fold (sum0);
785 old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
787 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
789 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
791 new_port0 = udp0->dst_port = s0->in2out.port;
792 sum0 = tcp0->checksum;
794 ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
797 ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
799 if (is_twice_nat_session (s0))
801 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
802 s0->ext_host_nat_addr.as_u32,
803 ip4_header_t, dst_address);
805 ip_csum_update (sum0,
806 vnet_buffer (b0)->ip.
808 s0->ext_host_nat_port, ip4_header_t,
810 tcp0->src_port = s0->ext_host_nat_port;
811 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
813 tcp0->checksum = ip_csum_fold (sum0);
816 if (nat44_set_tcp_session_state_o2i
818 vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags,
819 vnet_buffer (b0)->ip.reass.tcp_ack_number,
820 vnet_buffer (b0)->ip.reass.tcp_seq_number, thread_index))
823 else if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment
826 new_port0 = udp0->dst_port = s0->in2out.port;
827 sum0 = udp0->checksum;
828 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
830 sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
832 if (PREDICT_FALSE (is_twice_nat_session (s0)))
834 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
835 s0->ext_host_nat_addr.as_u32,
836 ip4_header_t, dst_address);
838 ip_csum_update (sum0,
839 vnet_buffer (b0)->ip.reass.l4_src_port,
840 s0->ext_host_nat_port, ip4_header_t,
842 udp0->src_port = s0->ext_host_nat_port;
843 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
845 udp0->checksum = ip_csum_fold (sum0);
850 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
852 new_port0 = udp0->dst_port = s0->in2out.port;
853 if (PREDICT_FALSE (is_twice_nat_session (s0)))
855 udp0->src_port = s0->ext_host_nat_port;
856 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
863 nat44_session_update_counters (s0, now,
864 vlib_buffer_length_in_chain (vm, b0),
866 /* Per-user LRU list maintenance */
867 nat44_session_update_lru (sm, s0, thread_index);
870 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
871 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
873 nat44_ed_out2in_trace_t *t =
874 vlib_add_trace (vm, node, b0, sizeof (*t));
875 t->sw_if_index = sw_if_index0;
876 t->next_index = next0;
880 t->session_index = s0 - tsm->sessions;
882 t->session_index = ~0;
885 pkts_processed += next0 == vnet_buffer2 (b0)->nat.arc_next;
886 /* verify speculative enqueue, maybe switch current next frame */
887 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
888 to_next, n_left_to_next,
892 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
895 vlib_node_increment_counter (vm, stats_node_index,
896 NAT_OUT2IN_ED_ERROR_OUT2IN_PACKETS,
898 vlib_node_increment_counter (vm, stats_node_index,
899 NAT_OUT2IN_ED_ERROR_TCP_PACKETS, tcp_packets);
900 vlib_node_increment_counter (vm, stats_node_index,
901 NAT_OUT2IN_ED_ERROR_UDP_PACKETS, udp_packets);
902 vlib_node_increment_counter (vm, stats_node_index,
903 NAT_OUT2IN_ED_ERROR_ICMP_PACKETS,
905 vlib_node_increment_counter (vm, stats_node_index,
906 NAT_OUT2IN_ED_ERROR_OTHER_PACKETS,
908 vlib_node_increment_counter (vm, stats_node_index,
909 NAT_OUT2IN_ED_ERROR_FRAGMENTS, fragments);
910 return frame->n_vectors;
914 nat44_ed_out2in_slow_path_node_fn_inline (vlib_main_t * vm,
915 vlib_node_runtime_t * node,
916 vlib_frame_t * frame)
918 u32 n_left_from, *from, *to_next, pkts_processed = 0, stats_node_index;
919 nat_next_t next_index;
920 snat_main_t *sm = &snat_main;
922 f64 now = vlib_time_now (vm);
923 u32 thread_index = vm->thread_index;
924 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
925 u32 tcp_packets = 0, udp_packets = 0, icmp_packets = 0, other_packets =
928 stats_node_index = sm->ed_out2in_slowpath_node_index;
930 from = vlib_frame_vector_args (frame);
931 n_left_from = frame->n_vectors;
932 next_index = node->cached_next_index;
934 while (n_left_from > 0)
938 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
940 while (n_left_from > 0 && n_left_to_next > 0)
944 u32 next0, sw_if_index0, rx_fib_index0, proto0, old_addr0,
946 u16 old_port0, new_port0;
950 icmp46_header_t *icmp0;
951 snat_session_t *s0 = 0;
952 clib_bihash_kv_16_8_t kv0, value0;
954 snat_session_key_t e_key0, l_key0;
955 lb_nat_type_t lb_nat0;
956 twice_nat_type_t twice_nat0;
959 /* speculatively enqueue b0 to the current next frame */
967 b0 = vlib_get_buffer (vm, bi0);
968 next0 = vnet_buffer2 (b0)->nat.arc_next;
970 vnet_buffer (b0)->snat.flags = 0;
971 ip0 = vlib_buffer_get_current (b0);
973 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
975 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
978 if (PREDICT_FALSE (ip0->ttl == 1))
980 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
981 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
982 ICMP4_time_exceeded_ttl_exceeded_in_transit,
984 next0 = NAT_NEXT_ICMP_ERROR;
988 udp0 = ip4_next_header (ip0);
989 tcp0 = (tcp_header_t *) udp0;
990 icmp0 = (icmp46_header_t *) udp0;
991 proto0 = ip_proto_to_nat_proto (ip0->protocol);
993 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
996 nat44_ed_out2in_unknown_proto (sm, b0, ip0, rx_fib_index0,
997 thread_index, now, vm, node);
998 if (!sm->forwarding_enabled)
1001 next0 = NAT_NEXT_DROP;
1007 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1009 next0 = icmp_out2in_ed_slow_path
1010 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1011 next0, now, thread_index, &s0);
1016 make_ed_kv (&ip0->dst_address, &ip0->src_address,
1017 ip0->protocol, rx_fib_index0,
1018 vnet_buffer (b0)->ip.reass.l4_dst_port,
1019 vnet_buffer (b0)->ip.reass.l4_src_port, ~0ULL, &kv0);
1022 if (!clib_bihash_search_16_8 (&tsm->out2in_ed, &kv0, &value0))
1024 s0 = pool_elt_at_index (tsm->sessions, value0.value);
1026 if (s0->tcp_close_timestamp && now >= s0->tcp_close_timestamp)
1028 nat_free_session_data (sm, s0, thread_index, 0);
1029 nat44_ed_delete_session (sm, s0, thread_index, 1);
1036 /* Try to match static mapping by external address and port,
1037 destination address and port in packet */
1038 e_key0.addr = ip0->dst_address;
1039 e_key0.port = vnet_buffer (b0)->ip.reass.l4_dst_port;
1040 e_key0.protocol = proto0;
1041 e_key0.fib_index = rx_fib_index0;
1043 if (snat_static_mapping_match (sm, e_key0, &l_key0, 1, 0,
1044 &twice_nat0, &lb_nat0,
1049 * Send DHCP packets to the ipv4 stack, or we won't
1050 * be able to use dhcp client on the outside interface
1052 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_UDP
1053 && (vnet_buffer (b0)->ip.
1054 reass.l4_dst_port ==
1055 clib_host_to_net_u16
1056 (UDP_DST_PORT_dhcp_to_client))))
1061 if (!sm->forwarding_enabled)
1064 node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
1065 next0 = NAT_NEXT_DROP;
1070 (sm, ip0, vnet_buffer (b0)->ip.reass.l4_src_port,
1071 vnet_buffer (b0)->ip.reass.l4_dst_port,
1072 thread_index, rx_fib_index0))
1074 next0 = NAT_NEXT_IN2OUT_ED_FAST_PATH;
1077 if (sm->num_workers > 1)
1078 create_bypass_for_fwd_worker (sm, b0, ip0,
1081 create_bypass_for_fwd (sm, b0, ip0, rx_fib_index0,
1087 if (PREDICT_FALSE (identity_nat0))
1090 if ((proto0 == NAT_PROTOCOL_TCP)
1091 && !tcp_flags_is_init (vnet_buffer (b0)->ip.
1092 reass.icmp_type_or_tcp_flags))
1094 b0->error = node->errors[NAT_OUT2IN_ED_ERROR_NON_SYN];
1095 next0 = NAT_NEXT_DROP;
1099 /* Create session initiated by host from external network */
1100 s0 = create_session_for_static_mapping_ed (sm, b0, l_key0,
1108 next0 = NAT_NEXT_DROP;
1113 old_addr0 = ip0->dst_address.as_u32;
1114 new_addr0 = ip0->dst_address.as_u32 = s0->in2out.addr.as_u32;
1115 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
1117 sum0 = ip0->checksum;
1118 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1120 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1121 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1122 s0->ext_host_nat_addr.as_u32, ip4_header_t,
1124 ip0->checksum = ip_csum_fold (sum0);
1126 old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
1128 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1130 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1132 new_port0 = udp0->dst_port = s0->in2out.port;
1133 sum0 = tcp0->checksum;
1135 ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1138 ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
1140 if (is_twice_nat_session (s0))
1142 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1143 s0->ext_host_nat_addr.as_u32,
1144 ip4_header_t, dst_address);
1146 ip_csum_update (sum0,
1147 vnet_buffer (b0)->ip.
1149 s0->ext_host_nat_port, ip4_header_t,
1151 tcp0->src_port = s0->ext_host_nat_port;
1152 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1154 tcp0->checksum = ip_csum_fold (sum0);
1157 if (nat44_set_tcp_session_state_o2i
1159 vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags,
1160 vnet_buffer (b0)->ip.reass.tcp_ack_number,
1161 vnet_buffer (b0)->ip.reass.tcp_seq_number, thread_index))
1164 else if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment
1167 new_port0 = udp0->dst_port = s0->in2out.port;
1168 sum0 = udp0->checksum;
1169 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1171 sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
1173 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1175 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1176 s0->ext_host_nat_addr.as_u32,
1177 ip4_header_t, dst_address);
1179 ip_csum_update (sum0,
1180 vnet_buffer (b0)->ip.reass.l4_src_port,
1181 s0->ext_host_nat_port, ip4_header_t,
1183 udp0->src_port = s0->ext_host_nat_port;
1184 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1186 udp0->checksum = ip_csum_fold (sum0);
1191 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1193 new_port0 = udp0->dst_port = s0->in2out.port;
1194 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1196 udp0->src_port = s0->ext_host_nat_port;
1197 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1204 nat44_session_update_counters (s0, now,
1205 vlib_buffer_length_in_chain (vm, b0),
1207 /* Per-user LRU list maintenance */
1208 nat44_session_update_lru (sm, s0, thread_index);
1211 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1212 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1214 nat44_ed_out2in_trace_t *t =
1215 vlib_add_trace (vm, node, b0, sizeof (*t));
1216 t->sw_if_index = sw_if_index0;
1217 t->next_index = next0;
1218 t->is_slow_path = 1;
1221 t->session_index = s0 - tsm->sessions;
1223 t->session_index = ~0;
1226 pkts_processed += next0 == vnet_buffer2 (b0)->nat.arc_next;
1227 /* verify speculative enqueue, maybe switch current next frame */
1228 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1229 to_next, n_left_to_next,
1233 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1236 vlib_node_increment_counter (vm, stats_node_index,
1237 NAT_OUT2IN_ED_ERROR_OUT2IN_PACKETS,
1239 vlib_node_increment_counter (vm, stats_node_index,
1240 NAT_OUT2IN_ED_ERROR_TCP_PACKETS, tcp_packets);
1241 vlib_node_increment_counter (vm, stats_node_index,
1242 NAT_OUT2IN_ED_ERROR_UDP_PACKETS, udp_packets);
1243 vlib_node_increment_counter (vm, stats_node_index,
1244 NAT_OUT2IN_ED_ERROR_ICMP_PACKETS,
1246 vlib_node_increment_counter (vm, stats_node_index,
1247 NAT_OUT2IN_ED_ERROR_OTHER_PACKETS,
1249 vlib_node_increment_counter (vm, stats_node_index,
1250 NAT_OUT2IN_ED_ERROR_FRAGMENTS, fragments);
1251 return frame->n_vectors;
1254 VLIB_NODE_FN (nat44_ed_out2in_node) (vlib_main_t * vm,
1255 vlib_node_runtime_t * node,
1256 vlib_frame_t * frame)
1258 return nat44_ed_out2in_fast_path_node_fn_inline (vm, node, frame);
1262 VLIB_REGISTER_NODE (nat44_ed_out2in_node) = {
1263 .name = "nat44-ed-out2in",
1264 .vector_size = sizeof (u32),
1265 .sibling_of = "nat-default",
1266 .format_trace = format_nat44_ed_out2in_trace,
1267 .type = VLIB_NODE_TYPE_INTERNAL,
1268 .n_errors = ARRAY_LEN(nat_out2in_ed_error_strings),
1269 .error_strings = nat_out2in_ed_error_strings,
1270 .runtime_data_bytes = sizeof (snat_runtime_t),
1274 VLIB_NODE_FN (nat44_ed_out2in_slowpath_node) (vlib_main_t * vm,
1275 vlib_node_runtime_t * node,
1276 vlib_frame_t * frame)
1278 return nat44_ed_out2in_slow_path_node_fn_inline (vm, node, frame);
1282 VLIB_REGISTER_NODE (nat44_ed_out2in_slowpath_node) = {
1283 .name = "nat44-ed-out2in-slowpath",
1284 .vector_size = sizeof (u32),
1285 .sibling_of = "nat-default",
1286 .format_trace = format_nat44_ed_out2in_trace,
1287 .type = VLIB_NODE_TYPE_INTERNAL,
1288 .n_errors = ARRAY_LEN(nat_out2in_ed_error_strings),
1289 .error_strings = nat_out2in_ed_error_strings,
1290 .runtime_data_bytes = sizeof (snat_runtime_t),
1295 format_nat_pre_trace (u8 * s, va_list * args)
1297 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1298 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1299 nat_pre_trace_t *t = va_arg (*args, nat_pre_trace_t *);
1300 return format (s, "out2in next_index %d arc_next_index %d", t->next_index,
1304 VLIB_NODE_FN (nat_pre_out2in_node) (vlib_main_t * vm,
1305 vlib_node_runtime_t * node,
1306 vlib_frame_t * frame)
1308 return nat_pre_node_fn_inline (vm, node, frame,
1309 NAT_NEXT_OUT2IN_ED_FAST_PATH);
1313 VLIB_REGISTER_NODE (nat_pre_out2in_node) = {
1314 .name = "nat-pre-out2in",
1315 .vector_size = sizeof (u32),
1316 .sibling_of = "nat-default",
1317 .format_trace = format_nat_pre_trace,
1318 .type = VLIB_NODE_TYPE_INTERNAL,
1324 * fd.io coding-style-patch-verification: ON
1327 * eval: (c-set-style "gnu")