2 * Copyright (c) 2018 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
17 * @brief NAT44 endpoint-dependent outside to inside network translation
20 #include <vlib/vlib.h>
21 #include <vnet/vnet.h>
22 #include <vnet/pg/pg.h>
23 #include <vnet/ip/ip.h>
24 #include <vnet/ethernet/ethernet.h>
25 #include <vnet/fib/ip4_fib.h>
26 #include <vnet/udp/udp.h>
27 #include <vppinfra/error.h>
29 #include <nat/nat_ipfix_logging.h>
30 #include <nat/nat_inlines.h>
31 #include <nat/nat44/inlines.h>
32 #include <nat/nat_syslog.h>
33 #include <nat/nat_ha.h>
35 static char *nat_out2in_ed_error_strings[] = {
36 #define _(sym,string) string,
37 foreach_nat_out2in_ed_error
47 } nat44_ed_out2in_trace_t;
50 format_nat44_ed_out2in_trace (u8 * s, va_list * args)
52 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
53 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
54 nat44_ed_out2in_trace_t *t = va_arg (*args, nat44_ed_out2in_trace_t *);
58 t->is_slow_path ? "NAT44_OUT2IN_ED_SLOW_PATH" :
59 "NAT44_OUT2IN_ED_FAST_PATH";
61 s = format (s, "%s: sw_if_index %d, next index %d, session %d", tag,
62 t->sw_if_index, t->next_index, t->session_index);
68 icmp_out2in_ed_slow_path (snat_main_t * sm, vlib_buffer_t * b0,
69 ip4_header_t * ip0, icmp46_header_t * icmp0,
70 u32 sw_if_index0, u32 rx_fib_index0,
71 vlib_node_runtime_t * node, u32 next0, f64 now,
72 u32 thread_index, snat_session_t ** p_s0)
74 next0 = icmp_out2in (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
75 next0, thread_index, p_s0, 0);
76 snat_session_t *s0 = *p_s0;
77 if (PREDICT_TRUE (next0 != NAT_NEXT_DROP && s0))
80 nat44_session_update_counters (s0, now,
81 vlib_buffer_length_in_chain
82 (sm->vlib_main, b0), thread_index);
83 /* Per-user LRU list maintenance */
84 nat44_session_update_lru (sm, s0, thread_index);
89 #ifndef CLIB_MARCH_VARIANT
91 nat44_o2i_ed_is_idle_session_cb (clib_bihash_kv_16_8_t * kv, void *arg)
93 snat_main_t *sm = &snat_main;
94 nat44_is_idle_session_ctx_t *ctx = arg;
96 u64 sess_timeout_time;
97 nat_ed_ses_key_t ed_key;
98 clib_bihash_kv_16_8_t ed_kv;
101 snat_session_key_t key;
102 snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
105 s = pool_elt_at_index (tsm->sessions, kv->value);
106 sess_timeout_time = s->last_heard + (f64) nat44_session_get_timeout (sm, s);
107 if (ctx->now >= sess_timeout_time)
109 ed_key.l_addr = s->in2out.addr;
110 ed_key.r_addr = s->ext_host_addr;
111 ed_key.fib_index = s->in2out.fib_index;
112 if (snat_is_unk_proto_session (s))
114 ed_key.proto = s->in2out.port;
120 ed_key.proto = snat_proto_to_ip_proto (s->in2out.protocol);
121 ed_key.l_port = s->in2out.port;
122 ed_key.r_port = s->ext_host_port;
124 if (is_twice_nat_session (s))
126 ed_key.r_addr = s->ext_host_nat_addr;
127 ed_key.r_port = s->ext_host_nat_port;
129 ed_kv.key[0] = ed_key.as_u64[0];
130 ed_kv.key[1] = ed_key.as_u64[1];
131 if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &ed_kv, 0))
132 nat_elog_warn ("in2out_ed key del failed");
134 if (snat_is_unk_proto_session (s))
137 snat_ipfix_logging_nat44_ses_delete (ctx->thread_index,
138 s->in2out.addr.as_u32,
139 s->out2in.addr.as_u32,
143 s->in2out.fib_index);
145 nat_syslog_nat44_sdel (s->user_index, s->in2out.fib_index,
146 &s->in2out.addr, s->in2out.port,
147 &s->ext_host_nat_addr, s->ext_host_nat_port,
148 &s->out2in.addr, s->out2in.port,
149 &s->ext_host_addr, s->ext_host_port,
150 s->in2out.protocol, is_twice_nat_session (s));
152 nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
153 s->ext_host_port, s->out2in.protocol, s->out2in.fib_index,
156 if (is_twice_nat_session (s))
158 for (i = 0; i < vec_len (sm->twice_nat_addresses); i++)
160 key.protocol = s->in2out.protocol;
161 key.port = s->ext_host_nat_port;
162 a = sm->twice_nat_addresses + i;
163 if (a->addr.as_u32 == s->ext_host_nat_addr.as_u32)
165 snat_free_outside_address_and_port (sm->twice_nat_addresses,
173 if (snat_is_session_static (s))
176 snat_free_outside_address_and_port (sm->addresses, ctx->thread_index,
179 nat44_delete_session (sm, s, ctx->thread_index);
187 static snat_session_t *
188 create_session_for_static_mapping_ed (snat_main_t * sm,
190 snat_session_key_t l_key,
191 snat_session_key_t e_key,
192 vlib_node_runtime_t * node,
194 twice_nat_type_t twice_nat,
195 lb_nat_type_t lb_nat, f64 now)
201 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
202 clib_bihash_kv_16_8_t kv;
203 snat_session_key_t eh_key;
204 nat44_is_idle_session_ctx_t ctx;
206 if (PREDICT_FALSE (nat44_maximum_sessions_exceeded (sm, thread_index)))
208 b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_SESSIONS_EXCEEDED];
209 nat_elog_notice ("maximum sessions exceeded");
213 u = nat_user_get_or_create (sm, &l_key.addr, l_key.fib_index, thread_index);
216 b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_SESSIONS_EXCEEDED];
217 nat_elog_warn ("create NAT user failed");
221 s = nat_ed_session_alloc (sm, u, thread_index, now);
224 b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_USER_SESS_EXCEEDED];
225 nat44_delete_user_with_no_session (sm, u, thread_index);
226 nat_elog_warn ("create NAT session failed");
230 ip = vlib_buffer_get_current (b);
231 udp = ip4_next_header (ip);
233 s->ext_host_addr.as_u32 = ip->src_address.as_u32;
234 s->ext_host_port = e_key.protocol == SNAT_PROTOCOL_ICMP ? 0 : udp->src_port;
235 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
237 s->flags |= SNAT_SESSION_FLAG_LOAD_BALANCING;
238 if (lb_nat == AFFINITY_LB_NAT)
239 s->flags |= SNAT_SESSION_FLAG_AFFINITY;
240 s->flags |= SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT;
243 s->in2out.protocol = s->out2in.protocol;
244 user_session_increment (sm, u, 1);
246 /* Add to lookup tables */
247 make_ed_kv (&kv, &e_key.addr, &s->ext_host_addr, ip->protocol,
248 e_key.fib_index, e_key.port, s->ext_host_port);
249 kv.value = s - tsm->sessions;
251 ctx.thread_index = thread_index;
252 if (clib_bihash_add_or_overwrite_stale_16_8 (&tsm->out2in_ed, &kv,
253 nat44_o2i_ed_is_idle_session_cb,
255 nat_elog_notice ("out2in-ed key add failed");
257 if (twice_nat == TWICE_NAT || (twice_nat == TWICE_NAT_SELF &&
258 ip->src_address.as_u32 == l_key.addr.as_u32))
260 eh_key.protocol = e_key.protocol;
261 if (snat_alloc_outside_address_and_port (sm->twice_nat_addresses, 0,
262 thread_index, &eh_key,
264 tsm->snat_thread_index))
266 b->error = node->errors[NAT_OUT2IN_ED_ERROR_OUT_OF_PORTS];
267 nat44_delete_session (sm, s, thread_index);
268 if (clib_bihash_add_del_16_8 (&tsm->out2in_ed, &kv, 0))
269 nat_elog_notice ("out2in-ed key del failed");
272 s->ext_host_nat_addr.as_u32 = eh_key.addr.as_u32;
273 s->ext_host_nat_port = eh_key.port;
274 s->flags |= SNAT_SESSION_FLAG_TWICE_NAT;
275 make_ed_kv (&kv, &l_key.addr, &s->ext_host_nat_addr, ip->protocol,
276 l_key.fib_index, l_key.port, s->ext_host_nat_port);
280 make_ed_kv (&kv, &l_key.addr, &s->ext_host_addr, ip->protocol,
281 l_key.fib_index, l_key.port, s->ext_host_port);
283 kv.value = s - tsm->sessions;
284 if (clib_bihash_add_or_overwrite_stale_16_8 (&tsm->in2out_ed, &kv,
285 nat44_i2o_ed_is_idle_session_cb,
287 nat_elog_notice ("in2out-ed key add failed");
289 snat_ipfix_logging_nat44_ses_create (thread_index,
290 s->in2out.addr.as_u32,
291 s->out2in.addr.as_u32,
294 s->out2in.port, s->in2out.fib_index);
296 nat_syslog_nat44_sadd (s->user_index, s->in2out.fib_index,
297 &s->in2out.addr, s->in2out.port,
298 &s->ext_host_nat_addr, s->ext_host_nat_port,
299 &s->out2in.addr, s->out2in.port,
300 &s->ext_host_addr, s->ext_host_port,
301 s->in2out.protocol, is_twice_nat_session (s));
303 nat_ha_sadd (&s->in2out.addr, s->in2out.port, &s->out2in.addr,
304 s->out2in.port, &s->ext_host_addr, s->ext_host_port,
305 &s->ext_host_nat_addr, s->ext_host_nat_port,
306 s->in2out.protocol, s->in2out.fib_index, s->flags,
313 next_src_nat (snat_main_t * sm, ip4_header_t * ip, u8 proto, u16 src_port,
314 u16 dst_port, u32 thread_index, u32 rx_fib_index)
316 clib_bihash_kv_16_8_t kv, value;
317 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
319 make_ed_kv (&kv, &ip->src_address, &ip->dst_address, proto,
320 rx_fib_index, src_port, dst_port);
321 if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
328 create_bypass_for_fwd (snat_main_t * sm, vlib_buffer_t * b, ip4_header_t * ip,
329 u32 rx_fib_index, u32 thread_index)
331 nat_ed_ses_key_t key;
332 clib_bihash_kv_16_8_t kv, value;
335 snat_session_t *s = 0;
336 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
337 f64 now = vlib_time_now (sm->vlib_main);
339 if (ip->protocol == IP_PROTOCOL_ICMP)
341 if (get_icmp_o2i_ed_key (b, ip, &key))
344 else if (ip->protocol == IP_PROTOCOL_UDP || ip->protocol == IP_PROTOCOL_TCP)
346 udp = ip4_next_header (ip);
347 key.r_addr = ip->src_address;
348 key.l_addr = ip->dst_address;
349 key.proto = ip->protocol;
350 key.l_port = udp->dst_port;
351 key.r_port = udp->src_port;
355 key.r_addr = ip->src_address;
356 key.l_addr = ip->dst_address;
357 key.proto = ip->protocol;
358 key.l_port = key.r_port = 0;
361 kv.key[0] = key.as_u64[0];
362 kv.key[1] = key.as_u64[1];
364 if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
366 s = pool_elt_at_index (tsm->sessions, value.value);
372 if (PREDICT_FALSE (nat44_maximum_sessions_exceeded (sm, thread_index)))
375 u = nat_user_get_or_create (sm, &ip->dst_address, sm->inside_fib_index,
379 nat_elog_warn ("create NAT user failed");
383 s = nat_ed_session_alloc (sm, u, thread_index, now);
386 nat44_delete_user_with_no_session (sm, u, thread_index);
387 nat_elog_warn ("create NAT session failed");
391 proto = ip_proto_to_snat_proto (key.proto);
393 s->ext_host_addr = key.r_addr;
394 s->ext_host_port = key.r_port;
395 s->flags |= SNAT_SESSION_FLAG_FWD_BYPASS;
396 s->out2in.addr = key.l_addr;
397 s->out2in.port = key.l_port;
398 s->out2in.protocol = proto;
401 s->flags |= SNAT_SESSION_FLAG_UNKNOWN_PROTO;
402 s->out2in.port = ip->protocol;
404 s->out2in.fib_index = 0;
405 s->in2out = s->out2in;
406 user_session_increment (sm, u, 0);
408 kv.value = s - tsm->sessions;
409 if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &kv, 1))
410 nat_elog_notice ("in2out_ed key add failed");
413 if (ip->protocol == IP_PROTOCOL_TCP)
415 tcp_header_t *tcp = ip4_next_header (ip);
416 if (nat44_set_tcp_session_state_o2i
417 (sm, s, tcp->flags, tcp->ack_number, tcp->seq_number, thread_index))
422 nat44_session_update_counters (s, now, 0, thread_index);
423 /* Per-user LRU list maintenance */
424 nat44_session_update_lru (sm, s, thread_index);
428 create_bypass_for_fwd_worker (snat_main_t * sm, vlib_buffer_t * b,
429 ip4_header_t * ip, u32 rx_fib_index)
431 ip4_header_t ip_wkr = {
432 .src_address = ip->dst_address,
434 u32 thread_index = sm->worker_in2out_cb (&ip_wkr, rx_fib_index, 0);
436 create_bypass_for_fwd (sm, b, ip, rx_fib_index, thread_index);
439 #ifndef CLIB_MARCH_VARIANT
441 icmp_match_out2in_ed (snat_main_t * sm, vlib_node_runtime_t * node,
442 u32 thread_index, vlib_buffer_t * b, ip4_header_t * ip,
443 u8 * p_proto, snat_session_key_t * p_value,
444 u8 * p_dont_translate, void *d, void *e)
446 u32 next = ~0, sw_if_index, rx_fib_index;
447 nat_ed_ses_key_t key;
448 clib_bihash_kv_16_8_t kv, value;
449 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
450 snat_session_t *s = 0;
451 u8 dont_translate = 0, is_addr_only, identity_nat;
452 snat_session_key_t e_key, l_key;
454 sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
455 rx_fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
457 if (get_icmp_o2i_ed_key (b, ip, &key))
459 b->error = node->errors[NAT_OUT2IN_ED_ERROR_UNSUPPORTED_PROTOCOL];
460 next = NAT_NEXT_DROP;
463 key.fib_index = rx_fib_index;
464 kv.key[0] = key.as_u64[0];
465 kv.key[1] = key.as_u64[1];
467 if (clib_bihash_search_16_8 (&tsm->out2in_ed, &kv, &value))
469 /* Try to match static mapping */
470 e_key.addr = ip->dst_address;
471 e_key.port = key.l_port;
472 e_key.protocol = ip_proto_to_snat_proto (key.proto);
473 e_key.fib_index = rx_fib_index;
474 if (snat_static_mapping_match
475 (sm, e_key, &l_key, 1, &is_addr_only, 0, 0, 0, &identity_nat))
477 if (!sm->forwarding_enabled)
479 /* Don't NAT packet aimed at the intfc address */
480 if (PREDICT_FALSE (is_interface_addr (sm, node, sw_if_index,
481 ip->dst_address.as_u32)))
486 b->error = node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
487 next = NAT_NEXT_DROP;
493 if (next_src_nat (sm, ip, key.proto, key.l_port, key.r_port,
494 thread_index, rx_fib_index))
496 next = NAT_NEXT_IN2OUT_ED_FAST_PATH;
499 if (sm->num_workers > 1)
500 create_bypass_for_fwd_worker (sm, b, ip, rx_fib_index);
502 create_bypass_for_fwd (sm, b, ip, rx_fib_index, thread_index);
508 (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags !=
510 && (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags !=
511 ICMP4_echo_request || !is_addr_only)))
513 b->error = node->errors[NAT_OUT2IN_ED_ERROR_BAD_ICMP_TYPE];
514 next = NAT_NEXT_DROP;
518 if (PREDICT_FALSE (identity_nat))
524 /* Create session initiated by host from external network */
525 s = create_session_for_static_mapping_ed (sm, b, l_key, e_key, node,
532 next = NAT_NEXT_DROP;
539 (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags !=
541 && vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags !=
543 && !icmp_type_is_error_message (vnet_buffer (b)->ip.
544 reass.icmp_type_or_tcp_flags)))
546 b->error = node->errors[NAT_OUT2IN_ED_ERROR_BAD_ICMP_TYPE];
547 next = NAT_NEXT_DROP;
551 s = pool_elt_at_index (tsm->sessions, value.value);
554 *p_proto = ip_proto_to_snat_proto (key.proto);
557 *p_value = s->in2out;
558 *p_dont_translate = dont_translate;
560 *(snat_session_t **) d = s;
565 static snat_session_t *
566 nat44_ed_out2in_unknown_proto (snat_main_t * sm,
572 vlib_main_t * vm, vlib_node_runtime_t * node)
574 clib_bihash_kv_8_8_t kv, value;
575 clib_bihash_kv_16_8_t s_kv, s_value;
576 snat_static_mapping_t *m;
577 u32 old_addr, new_addr;
580 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
583 old_addr = ip->dst_address.as_u32;
585 make_ed_kv (&s_kv, &ip->dst_address, &ip->src_address, ip->protocol,
588 if (!clib_bihash_search_16_8 (&tsm->out2in_ed, &s_kv, &s_value))
590 s = pool_elt_at_index (tsm->sessions, s_value.value);
591 new_addr = ip->dst_address.as_u32 = s->in2out.addr.as_u32;
595 if (PREDICT_FALSE (nat44_maximum_sessions_exceeded (sm, thread_index)))
597 b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_SESSIONS_EXCEEDED];
598 nat_elog_notice ("maximum sessions exceeded");
602 make_sm_kv (&kv, &ip->dst_address, 0, 0, 0);
603 if (clib_bihash_search_8_8
604 (&sm->static_mapping_by_external, &kv, &value))
606 b->error = node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
610 m = pool_elt_at_index (sm->static_mappings, value.value);
612 new_addr = ip->dst_address.as_u32 = m->local_addr.as_u32;
614 u = nat_user_get_or_create (sm, &m->local_addr, m->fib_index,
618 b->error = node->errors[NAT_OUT2IN_ED_ERROR_CANNOT_CREATE_USER];
619 nat_elog_warn ("create NAT user failed");
623 /* Create a new session */
624 s = nat_ed_session_alloc (sm, u, thread_index, now);
627 b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_USER_SESS_EXCEEDED];
628 nat44_delete_user_with_no_session (sm, u, thread_index);
629 nat_elog_warn ("create NAT session failed");
633 s->ext_host_addr.as_u32 = ip->src_address.as_u32;
634 s->flags |= SNAT_SESSION_FLAG_UNKNOWN_PROTO;
635 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
636 s->flags |= SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT;
637 s->out2in.addr.as_u32 = old_addr;
638 s->out2in.fib_index = rx_fib_index;
639 s->in2out.addr.as_u32 = new_addr;
640 s->in2out.fib_index = m->fib_index;
641 s->in2out.port = s->out2in.port = ip->protocol;
642 user_session_increment (sm, u, 1);
644 /* Add to lookup tables */
645 s_kv.value = s - tsm->sessions;
646 if (clib_bihash_add_del_16_8 (&tsm->out2in_ed, &s_kv, 1))
647 nat_elog_notice ("out2in key add failed");
649 make_ed_kv (&s_kv, &ip->dst_address, &ip->src_address, ip->protocol,
651 s_kv.value = s - tsm->sessions;
652 if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &s_kv, 1))
653 nat_elog_notice ("in2out key add failed");
656 /* Update IP checksum */
658 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, dst_address);
659 ip->checksum = ip_csum_fold (sum);
661 vnet_buffer (b)->sw_if_index[VLIB_TX] = s->in2out.fib_index;
664 nat44_session_update_counters (s, now, vlib_buffer_length_in_chain (vm, b),
666 /* Per-user LRU list maintenance */
667 nat44_session_update_lru (sm, s, thread_index);
673 nat44_ed_out2in_fast_path_node_fn_inline (vlib_main_t * vm,
674 vlib_node_runtime_t * node,
675 vlib_frame_t * frame)
677 u32 n_left_from, *from, *to_next, pkts_processed = 0, stats_node_index;
678 nat_next_t next_index;
679 snat_main_t *sm = &snat_main;
680 f64 now = vlib_time_now (vm);
681 u32 thread_index = vm->thread_index;
682 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
683 u32 tcp_packets = 0, udp_packets = 0, icmp_packets = 0, other_packets =
686 stats_node_index = sm->ed_out2in_node_index;
688 from = vlib_frame_vector_args (frame);
689 n_left_from = frame->n_vectors;
690 next_index = node->cached_next_index;
692 while (n_left_from > 0)
696 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
698 while (n_left_from > 0 && n_left_to_next > 0)
702 u32 next0, sw_if_index0, rx_fib_index0, proto0, old_addr0,
704 u16 old_port0, new_port0;
708 snat_session_t *s0 = 0;
709 clib_bihash_kv_16_8_t kv0, value0;
712 /* speculatively enqueue b0 to the current next frame */
720 b0 = vlib_get_buffer (vm, bi0);
721 next0 = nat_buffer_opaque (b0)->arc_next;
723 vnet_buffer (b0)->snat.flags = 0;
724 ip0 = vlib_buffer_get_current (b0);
726 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
728 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
731 if (PREDICT_FALSE (ip0->ttl == 1))
733 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
734 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
735 ICMP4_time_exceeded_ttl_exceeded_in_transit,
737 next0 = NAT_NEXT_ICMP_ERROR;
741 udp0 = ip4_next_header (ip0);
742 tcp0 = (tcp_header_t *) udp0;
743 proto0 = ip_proto_to_snat_proto (ip0->protocol);
745 if (PREDICT_FALSE (proto0 == ~0))
747 next0 = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
751 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
753 next0 = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
757 make_ed_kv (&kv0, &ip0->dst_address, &ip0->src_address,
758 ip0->protocol, rx_fib_index0,
759 vnet_buffer (b0)->ip.reass.l4_dst_port,
760 vnet_buffer (b0)->ip.reass.l4_src_port);
762 if (clib_bihash_search_16_8 (&tsm->out2in_ed, &kv0, &value0))
764 next0 = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
767 s0 = pool_elt_at_index (tsm->sessions, value0.value);
769 // drop if session expired
770 u64 sess_timeout_time;
771 sess_timeout_time = s0->last_heard +
772 (f64) nat44_session_get_timeout (sm, s0);
773 if (now >= sess_timeout_time)
776 nat_free_session_data (sm, s0, thread_index, 0);
777 nat44_delete_session (sm, s0, thread_index);
779 b0->error = node->errors[NAT_OUT2IN_ED_ERROR_SESS_EXPIRED];
780 next0 = NAT_NEXT_DROP;
785 old_addr0 = ip0->dst_address.as_u32;
786 new_addr0 = ip0->dst_address.as_u32 = s0->in2out.addr.as_u32;
787 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
789 sum0 = ip0->checksum;
790 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
792 if (PREDICT_FALSE (is_twice_nat_session (s0)))
793 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
794 s0->ext_host_nat_addr.as_u32, ip4_header_t,
796 ip0->checksum = ip_csum_fold (sum0);
798 old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
800 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
802 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
804 new_port0 = udp0->dst_port = s0->in2out.port;
805 sum0 = tcp0->checksum;
807 ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
810 ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
812 if (is_twice_nat_session (s0))
814 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
815 s0->ext_host_nat_addr.as_u32,
816 ip4_header_t, dst_address);
818 ip_csum_update (sum0,
819 vnet_buffer (b0)->ip.
821 s0->ext_host_nat_port, ip4_header_t,
823 tcp0->src_port = s0->ext_host_nat_port;
824 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
826 tcp0->checksum = ip_csum_fold (sum0);
829 if (nat44_set_tcp_session_state_o2i
830 (sm, s0, vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags,
831 vnet_buffer (b0)->ip.reass.tcp_ack_number,
832 vnet_buffer (b0)->ip.reass.tcp_seq_number, thread_index))
835 else if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment
838 new_port0 = udp0->dst_port = s0->in2out.port;
839 sum0 = udp0->checksum;
840 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
842 sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
844 if (PREDICT_FALSE (is_twice_nat_session (s0)))
846 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
847 s0->ext_host_nat_addr.as_u32,
848 ip4_header_t, dst_address);
850 ip_csum_update (sum0,
851 vnet_buffer (b0)->ip.reass.l4_src_port,
852 s0->ext_host_nat_port, ip4_header_t,
854 udp0->src_port = s0->ext_host_nat_port;
855 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
857 udp0->checksum = ip_csum_fold (sum0);
862 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
864 new_port0 = udp0->dst_port = s0->in2out.port;
865 if (PREDICT_FALSE (is_twice_nat_session (s0)))
867 udp0->src_port = s0->ext_host_nat_port;
868 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
875 nat44_session_update_counters (s0, now,
876 vlib_buffer_length_in_chain (vm, b0),
878 /* Per-user LRU list maintenance */
879 nat44_session_update_lru (sm, s0, thread_index);
882 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
883 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
885 nat44_ed_out2in_trace_t *t =
886 vlib_add_trace (vm, node, b0, sizeof (*t));
887 t->sw_if_index = sw_if_index0;
888 t->next_index = next0;
892 t->session_index = s0 - tsm->sessions;
894 t->session_index = ~0;
897 pkts_processed += next0 == nat_buffer_opaque (b0)->arc_next;
898 /* verify speculative enqueue, maybe switch current next frame */
899 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
900 to_next, n_left_to_next,
904 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
907 vlib_node_increment_counter (vm, stats_node_index,
908 NAT_OUT2IN_ED_ERROR_OUT2IN_PACKETS,
910 vlib_node_increment_counter (vm, stats_node_index,
911 NAT_OUT2IN_ED_ERROR_TCP_PACKETS, tcp_packets);
912 vlib_node_increment_counter (vm, stats_node_index,
913 NAT_OUT2IN_ED_ERROR_UDP_PACKETS, udp_packets);
914 vlib_node_increment_counter (vm, stats_node_index,
915 NAT_OUT2IN_ED_ERROR_ICMP_PACKETS,
917 vlib_node_increment_counter (vm, stats_node_index,
918 NAT_OUT2IN_ED_ERROR_OTHER_PACKETS,
920 vlib_node_increment_counter (vm, stats_node_index,
921 NAT_OUT2IN_ED_ERROR_FRAGMENTS, fragments);
922 return frame->n_vectors;
926 nat44_ed_out2in_slow_path_node_fn_inline (vlib_main_t * vm,
927 vlib_node_runtime_t * node,
928 vlib_frame_t * frame)
930 u32 n_left_from, *from, *to_next, pkts_processed = 0, stats_node_index;
931 nat_next_t next_index;
932 snat_main_t *sm = &snat_main;
933 f64 now = vlib_time_now (vm);
934 u32 thread_index = vm->thread_index;
935 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
936 u32 tcp_packets = 0, udp_packets = 0, icmp_packets = 0, other_packets =
939 stats_node_index = sm->ed_out2in_slowpath_node_index;
941 from = vlib_frame_vector_args (frame);
942 n_left_from = frame->n_vectors;
943 next_index = node->cached_next_index;
945 while (n_left_from > 0)
949 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
951 while (n_left_from > 0 && n_left_to_next > 0)
955 u32 next0, sw_if_index0, rx_fib_index0, proto0, old_addr0,
957 u16 old_port0, new_port0;
961 icmp46_header_t *icmp0;
962 snat_session_t *s0 = 0;
963 clib_bihash_kv_16_8_t kv0, value0;
965 snat_session_key_t e_key0, l_key0;
966 lb_nat_type_t lb_nat0;
967 twice_nat_type_t twice_nat0;
970 /* speculatively enqueue b0 to the current next frame */
978 b0 = vlib_get_buffer (vm, bi0);
979 next0 = nat_buffer_opaque (b0)->arc_next;
981 vnet_buffer (b0)->snat.flags = 0;
982 ip0 = vlib_buffer_get_current (b0);
984 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
986 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
989 if (PREDICT_FALSE (ip0->ttl == 1))
991 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
992 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
993 ICMP4_time_exceeded_ttl_exceeded_in_transit,
995 next0 = NAT_NEXT_ICMP_ERROR;
999 udp0 = ip4_next_header (ip0);
1000 tcp0 = (tcp_header_t *) udp0;
1001 icmp0 = (icmp46_header_t *) udp0;
1002 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1004 if (PREDICT_FALSE (proto0 == ~0))
1007 nat44_ed_out2in_unknown_proto (sm, b0, ip0, rx_fib_index0,
1008 thread_index, now, vm, node);
1010 if (!sm->forwarding_enabled)
1013 next0 = NAT_NEXT_DROP;
1018 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1020 next0 = icmp_out2in_ed_slow_path
1021 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1022 next0, now, thread_index, &s0);
1027 make_ed_kv (&kv0, &ip0->dst_address, &ip0->src_address,
1028 ip0->protocol, rx_fib_index0,
1029 vnet_buffer (b0)->ip.reass.l4_dst_port,
1030 vnet_buffer (b0)->ip.reass.l4_src_port);
1032 if (clib_bihash_search_16_8 (&tsm->out2in_ed, &kv0, &value0))
1034 /* Try to match static mapping by external address and port,
1035 destination address and port in packet */
1036 e_key0.addr = ip0->dst_address;
1037 e_key0.port = vnet_buffer (b0)->ip.reass.l4_dst_port;
1038 e_key0.protocol = proto0;
1039 e_key0.fib_index = rx_fib_index0;
1041 if (snat_static_mapping_match (sm, e_key0, &l_key0, 1, 0,
1042 &twice_nat0, &lb_nat0,
1047 * Send DHCP packets to the ipv4 stack, or we won't
1048 * be able to use dhcp client on the outside interface
1050 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_UDP
1051 && (vnet_buffer (b0)->ip.
1052 reass.l4_dst_port ==
1053 clib_host_to_net_u16
1054 (UDP_DST_PORT_dhcp_to_client))))
1059 if (!sm->forwarding_enabled)
1062 node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
1063 next0 = NAT_NEXT_DROP;
1067 if (next_src_nat (sm, ip0, ip0->protocol,
1068 vnet_buffer (b0)->ip.
1070 vnet_buffer (b0)->ip.
1071 reass.l4_dst_port, thread_index,
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 == SNAT_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,
1107 next0 = NAT_NEXT_DROP;
1113 s0 = pool_elt_at_index (tsm->sessions, value0.value);
1116 old_addr0 = ip0->dst_address.as_u32;
1117 new_addr0 = ip0->dst_address.as_u32 = s0->in2out.addr.as_u32;
1118 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
1120 sum0 = ip0->checksum;
1121 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1123 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1124 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1125 s0->ext_host_nat_addr.as_u32, ip4_header_t,
1127 ip0->checksum = ip_csum_fold (sum0);
1129 old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
1131 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1133 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1135 new_port0 = udp0->dst_port = s0->in2out.port;
1136 sum0 = tcp0->checksum;
1138 ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1141 ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
1143 if (is_twice_nat_session (s0))
1145 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1146 s0->ext_host_nat_addr.as_u32,
1147 ip4_header_t, dst_address);
1149 ip_csum_update (sum0,
1150 vnet_buffer (b0)->ip.
1152 s0->ext_host_nat_port, ip4_header_t,
1154 tcp0->src_port = s0->ext_host_nat_port;
1155 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1157 tcp0->checksum = ip_csum_fold (sum0);
1160 if (nat44_set_tcp_session_state_o2i
1161 (sm, s0, vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags,
1162 vnet_buffer (b0)->ip.reass.tcp_ack_number,
1163 vnet_buffer (b0)->ip.reass.tcp_seq_number, thread_index))
1166 else if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment
1169 new_port0 = udp0->dst_port = s0->in2out.port;
1170 sum0 = udp0->checksum;
1171 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1173 sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
1175 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1177 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1178 s0->ext_host_nat_addr.as_u32,
1179 ip4_header_t, dst_address);
1181 ip_csum_update (sum0,
1182 vnet_buffer (b0)->ip.reass.l4_src_port,
1183 s0->ext_host_nat_port, ip4_header_t,
1185 udp0->src_port = s0->ext_host_nat_port;
1186 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1188 udp0->checksum = ip_csum_fold (sum0);
1193 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1195 new_port0 = udp0->dst_port = s0->in2out.port;
1196 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1198 udp0->src_port = s0->ext_host_nat_port;
1199 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1206 nat44_session_update_counters (s0, now,
1207 vlib_buffer_length_in_chain (vm, b0),
1209 /* Per-user LRU list maintenance */
1210 nat44_session_update_lru (sm, s0, thread_index);
1213 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1214 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1216 nat44_ed_out2in_trace_t *t =
1217 vlib_add_trace (vm, node, b0, sizeof (*t));
1218 t->sw_if_index = sw_if_index0;
1219 t->next_index = next0;
1220 t->is_slow_path = 1;
1223 t->session_index = s0 - tsm->sessions;
1225 t->session_index = ~0;
1228 pkts_processed += next0 == nat_buffer_opaque (b0)->arc_next;
1229 /* verify speculative enqueue, maybe switch current next frame */
1230 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1231 to_next, n_left_to_next,
1235 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1238 vlib_node_increment_counter (vm, stats_node_index,
1239 NAT_OUT2IN_ED_ERROR_OUT2IN_PACKETS,
1241 vlib_node_increment_counter (vm, stats_node_index,
1242 NAT_OUT2IN_ED_ERROR_TCP_PACKETS, tcp_packets);
1243 vlib_node_increment_counter (vm, stats_node_index,
1244 NAT_OUT2IN_ED_ERROR_UDP_PACKETS, udp_packets);
1245 vlib_node_increment_counter (vm, stats_node_index,
1246 NAT_OUT2IN_ED_ERROR_ICMP_PACKETS,
1248 vlib_node_increment_counter (vm, stats_node_index,
1249 NAT_OUT2IN_ED_ERROR_OTHER_PACKETS,
1251 vlib_node_increment_counter (vm, stats_node_index,
1252 NAT_OUT2IN_ED_ERROR_FRAGMENTS, fragments);
1253 return frame->n_vectors;
1256 VLIB_NODE_FN (nat44_ed_out2in_node) (vlib_main_t * vm,
1257 vlib_node_runtime_t * node,
1258 vlib_frame_t * frame)
1260 return nat44_ed_out2in_fast_path_node_fn_inline (vm, node, frame);
1264 VLIB_REGISTER_NODE (nat44_ed_out2in_node) = {
1265 .name = "nat44-ed-out2in",
1266 .vector_size = sizeof (u32),
1267 .sibling_of = "nat-default",
1268 .format_trace = format_nat44_ed_out2in_trace,
1269 .type = VLIB_NODE_TYPE_INTERNAL,
1270 .n_errors = ARRAY_LEN(nat_out2in_ed_error_strings),
1271 .error_strings = nat_out2in_ed_error_strings,
1272 .runtime_data_bytes = sizeof (snat_runtime_t),
1276 VLIB_NODE_FN (nat44_ed_out2in_slowpath_node) (vlib_main_t * vm,
1277 vlib_node_runtime_t * node,
1278 vlib_frame_t * frame)
1280 return nat44_ed_out2in_slow_path_node_fn_inline (vm, node, frame);
1284 VLIB_REGISTER_NODE (nat44_ed_out2in_slowpath_node) = {
1285 .name = "nat44-ed-out2in-slowpath",
1286 .vector_size = sizeof (u32),
1287 .sibling_of = "nat-default",
1288 .format_trace = format_nat44_ed_out2in_trace,
1289 .type = VLIB_NODE_TYPE_INTERNAL,
1290 .n_errors = ARRAY_LEN(nat_out2in_ed_error_strings),
1291 .error_strings = nat_out2in_ed_error_strings,
1292 .runtime_data_bytes = sizeof (snat_runtime_t),
1297 format_nat_pre_trace (u8 * s, va_list * args)
1299 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1300 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1301 nat_pre_trace_t *t = va_arg (*args, nat_pre_trace_t *);
1302 return format (s, "out2in next_index %d", t->next_index);
1305 VLIB_NODE_FN (nat_pre_out2in_node) (vlib_main_t * vm,
1306 vlib_node_runtime_t * node,
1307 vlib_frame_t * frame)
1309 return nat_pre_node_fn_inline (vm, node, frame,
1310 NAT_NEXT_OUT2IN_ED_FAST_PATH);
1314 VLIB_REGISTER_NODE (nat_pre_out2in_node) = {
1315 .name = "nat-pre-out2in",
1316 .vector_size = sizeof (u32),
1317 .sibling_of = "nat-default",
1318 .format_trace = format_nat_pre_trace,
1319 .type = VLIB_NODE_TYPE_INTERNAL,
1325 * fd.io coding-style-patch-verification: ON
1328 * eval: (c-set-style "gnu")