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_reass.h>
31 #include <nat/nat_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
43 NAT44_ED_OUT2IN_NEXT_DROP,
44 NAT44_ED_OUT2IN_NEXT_LOOKUP,
45 NAT44_ED_OUT2IN_NEXT_ICMP_ERROR,
46 NAT44_ED_OUT2IN_NEXT_IN2OUT,
47 NAT44_ED_OUT2IN_NEXT_SLOW_PATH,
48 NAT44_ED_OUT2IN_NEXT_REASS,
49 NAT44_ED_OUT2IN_N_NEXT,
50 } nat44_ed_out2in_next_t;
58 } nat44_ed_out2in_trace_t;
61 format_nat44_ed_out2in_trace (u8 * s, va_list * args)
63 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
64 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
65 nat44_ed_out2in_trace_t *t = va_arg (*args, nat44_ed_out2in_trace_t *);
69 t->is_slow_path ? "NAT44_OUT2IN_ED_SLOW_PATH" :
70 "NAT44_OUT2IN_ED_FAST_PATH";
72 s = format (s, "%s: sw_if_index %d, next index %d, session %d", tag,
73 t->sw_if_index, t->next_index, t->session_index);
79 icmp_out2in_ed_slow_path (snat_main_t * sm, vlib_buffer_t * b0,
80 ip4_header_t * ip0, icmp46_header_t * icmp0,
81 u32 sw_if_index0, u32 rx_fib_index0,
82 vlib_node_runtime_t * node, u32 next0, f64 now,
83 u32 thread_index, snat_session_t ** p_s0)
85 next0 = icmp_out2in (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
86 next0, thread_index, p_s0, 0);
87 snat_session_t *s0 = *p_s0;
88 if (PREDICT_TRUE (next0 != NAT44_ED_OUT2IN_NEXT_DROP && s0))
91 nat44_session_update_counters (s0, now,
92 vlib_buffer_length_in_chain
93 (sm->vlib_main, b0), thread_index);
94 /* Per-user LRU list maintenance */
95 nat44_session_update_lru (sm, s0, thread_index);
100 #ifndef CLIB_MARCH_VARIANT
102 nat44_o2i_ed_is_idle_session_cb (clib_bihash_kv_16_8_t * kv, void *arg)
104 snat_main_t *sm = &snat_main;
105 nat44_is_idle_session_ctx_t *ctx = arg;
107 u64 sess_timeout_time;
108 nat_ed_ses_key_t ed_key;
109 clib_bihash_kv_16_8_t ed_kv;
112 snat_session_key_t key;
113 snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
116 s = pool_elt_at_index (tsm->sessions, kv->value);
117 sess_timeout_time = s->last_heard + (f64) nat44_session_get_timeout (sm, s);
118 if (ctx->now >= sess_timeout_time)
120 ed_key.l_addr = s->in2out.addr;
121 ed_key.r_addr = s->ext_host_addr;
122 ed_key.fib_index = s->in2out.fib_index;
123 if (snat_is_unk_proto_session (s))
125 ed_key.proto = s->in2out.port;
131 ed_key.proto = snat_proto_to_ip_proto (s->in2out.protocol);
132 ed_key.l_port = s->in2out.port;
133 ed_key.r_port = s->ext_host_port;
135 if (is_twice_nat_session (s))
137 ed_key.r_addr = s->ext_host_nat_addr;
138 ed_key.r_port = s->ext_host_nat_port;
140 ed_kv.key[0] = ed_key.as_u64[0];
141 ed_kv.key[1] = ed_key.as_u64[1];
142 if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &ed_kv, 0))
143 nat_elog_warn ("in2out_ed key del failed");
145 if (snat_is_unk_proto_session (s))
148 snat_ipfix_logging_nat44_ses_delete (ctx->thread_index,
149 s->in2out.addr.as_u32,
150 s->out2in.addr.as_u32,
154 s->in2out.fib_index);
156 nat_syslog_nat44_sdel (s->user_index, s->in2out.fib_index,
157 &s->in2out.addr, s->in2out.port,
158 &s->ext_host_nat_addr, s->ext_host_nat_port,
159 &s->out2in.addr, s->out2in.port,
160 &s->ext_host_addr, s->ext_host_port,
161 s->in2out.protocol, is_twice_nat_session (s));
163 nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
164 s->ext_host_port, s->out2in.protocol, s->out2in.fib_index,
167 if (is_twice_nat_session (s))
169 for (i = 0; i < vec_len (sm->twice_nat_addresses); i++)
171 key.protocol = s->in2out.protocol;
172 key.port = s->ext_host_nat_port;
173 a = sm->twice_nat_addresses + i;
174 if (a->addr.as_u32 == s->ext_host_nat_addr.as_u32)
176 snat_free_outside_address_and_port (sm->twice_nat_addresses,
184 if (snat_is_session_static (s))
187 snat_free_outside_address_and_port (sm->addresses, ctx->thread_index,
190 nat44_delete_session (sm, s, ctx->thread_index);
198 static snat_session_t *
199 create_session_for_static_mapping_ed (snat_main_t * sm,
201 snat_session_key_t l_key,
202 snat_session_key_t e_key,
203 vlib_node_runtime_t * node,
205 twice_nat_type_t twice_nat,
206 lb_nat_type_t lb_nat, f64 now)
212 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
213 clib_bihash_kv_16_8_t kv;
214 snat_session_key_t eh_key;
215 nat44_is_idle_session_ctx_t ctx;
217 if (PREDICT_FALSE (maximum_sessions_exceeded (sm, thread_index)))
219 b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_SESSIONS_EXCEEDED];
220 nat_elog_notice ("maximum sessions exceeded");
224 u = nat_user_get_or_create (sm, &l_key.addr, l_key.fib_index, thread_index);
227 nat_elog_warn ("create NAT user failed");
231 s = nat_ed_session_alloc (sm, u, thread_index, now);
234 nat44_delete_user_with_no_session (sm, u, thread_index);
235 nat_elog_warn ("create NAT session failed");
239 ip = vlib_buffer_get_current (b);
240 udp = ip4_next_header (ip);
242 s->ext_host_addr.as_u32 = ip->src_address.as_u32;
243 s->ext_host_port = e_key.protocol == SNAT_PROTOCOL_ICMP ? 0 : udp->src_port;
244 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
246 s->flags |= SNAT_SESSION_FLAG_LOAD_BALANCING;
247 if (lb_nat == AFFINITY_LB_NAT)
248 s->flags |= SNAT_SESSION_FLAG_AFFINITY;
249 s->flags |= SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT;
252 s->in2out.protocol = s->out2in.protocol;
253 user_session_increment (sm, u, 1);
255 /* Add to lookup tables */
256 make_ed_kv (&kv, &e_key.addr, &s->ext_host_addr, ip->protocol,
257 e_key.fib_index, e_key.port, s->ext_host_port);
258 kv.value = s - tsm->sessions;
260 ctx.thread_index = thread_index;
261 if (clib_bihash_add_or_overwrite_stale_16_8 (&tsm->out2in_ed, &kv,
262 nat44_o2i_ed_is_idle_session_cb,
264 nat_elog_notice ("out2in-ed key add failed");
266 if (twice_nat == TWICE_NAT || (twice_nat == TWICE_NAT_SELF &&
267 ip->src_address.as_u32 == l_key.addr.as_u32))
269 eh_key.protocol = e_key.protocol;
270 if (snat_alloc_outside_address_and_port (sm->twice_nat_addresses, 0,
271 thread_index, &eh_key,
273 tsm->snat_thread_index))
275 b->error = node->errors[NAT_OUT2IN_ED_ERROR_OUT_OF_PORTS];
276 nat44_delete_session (sm, s, thread_index);
277 if (clib_bihash_add_del_16_8 (&tsm->out2in_ed, &kv, 0))
278 nat_elog_notice ("out2in-ed key del failed");
281 s->ext_host_nat_addr.as_u32 = eh_key.addr.as_u32;
282 s->ext_host_nat_port = eh_key.port;
283 s->flags |= SNAT_SESSION_FLAG_TWICE_NAT;
284 make_ed_kv (&kv, &l_key.addr, &s->ext_host_nat_addr, ip->protocol,
285 l_key.fib_index, l_key.port, s->ext_host_nat_port);
289 make_ed_kv (&kv, &l_key.addr, &s->ext_host_addr, ip->protocol,
290 l_key.fib_index, l_key.port, s->ext_host_port);
292 kv.value = s - tsm->sessions;
293 if (clib_bihash_add_or_overwrite_stale_16_8 (&tsm->in2out_ed, &kv,
294 nat44_i2o_ed_is_idle_session_cb,
296 nat_elog_notice ("in2out-ed key add failed");
298 snat_ipfix_logging_nat44_ses_create (thread_index,
299 s->in2out.addr.as_u32,
300 s->out2in.addr.as_u32,
303 s->out2in.port, s->in2out.fib_index);
305 nat_syslog_nat44_sadd (s->user_index, s->in2out.fib_index,
306 &s->in2out.addr, s->in2out.port,
307 &s->ext_host_nat_addr, s->ext_host_nat_port,
308 &s->out2in.addr, s->out2in.port,
309 &s->ext_host_addr, s->ext_host_port,
310 s->in2out.protocol, is_twice_nat_session (s));
312 nat_ha_sadd (&s->in2out.addr, s->in2out.port, &s->out2in.addr,
313 s->out2in.port, &s->ext_host_addr, s->ext_host_port,
314 &s->ext_host_nat_addr, s->ext_host_nat_port,
315 s->in2out.protocol, s->in2out.fib_index, s->flags,
322 next_src_nat (snat_main_t * sm, ip4_header_t * ip, u8 proto, u16 src_port,
323 u16 dst_port, u32 thread_index, u32 rx_fib_index)
325 clib_bihash_kv_16_8_t kv, value;
326 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
328 make_ed_kv (&kv, &ip->src_address, &ip->dst_address, proto,
329 rx_fib_index, src_port, dst_port);
330 if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
337 create_bypass_for_fwd (snat_main_t * sm, ip4_header_t * ip, u32 rx_fib_index,
340 nat_ed_ses_key_t key;
341 clib_bihash_kv_16_8_t kv, value;
344 snat_session_t *s = 0;
345 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
346 f64 now = vlib_time_now (sm->vlib_main);
348 if (ip->protocol == IP_PROTOCOL_ICMP)
350 if (get_icmp_o2i_ed_key (ip, &key))
353 else if (ip->protocol == IP_PROTOCOL_UDP || ip->protocol == IP_PROTOCOL_TCP)
355 udp = ip4_next_header (ip);
356 key.r_addr = ip->src_address;
357 key.l_addr = ip->dst_address;
358 key.proto = ip->protocol;
359 key.l_port = udp->dst_port;
360 key.r_port = udp->src_port;
364 key.r_addr = ip->src_address;
365 key.l_addr = ip->dst_address;
366 key.proto = ip->protocol;
367 key.l_port = key.r_port = 0;
370 kv.key[0] = key.as_u64[0];
371 kv.key[1] = key.as_u64[1];
373 if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
375 s = pool_elt_at_index (tsm->sessions, value.value);
381 if (PREDICT_FALSE (maximum_sessions_exceeded (sm, thread_index)))
384 u = nat_user_get_or_create (sm, &ip->dst_address, sm->inside_fib_index,
388 nat_elog_warn ("create NAT user failed");
392 s = nat_ed_session_alloc (sm, u, thread_index, now);
395 nat44_delete_user_with_no_session (sm, u, thread_index);
396 nat_elog_warn ("create NAT session failed");
400 proto = ip_proto_to_snat_proto (key.proto);
402 s->ext_host_addr = key.r_addr;
403 s->ext_host_port = key.r_port;
404 s->flags |= SNAT_SESSION_FLAG_FWD_BYPASS;
405 s->out2in.addr = key.l_addr;
406 s->out2in.port = key.l_port;
407 s->out2in.protocol = proto;
410 s->flags |= SNAT_SESSION_FLAG_UNKNOWN_PROTO;
411 s->out2in.port = ip->protocol;
413 s->out2in.fib_index = 0;
414 s->in2out = s->out2in;
415 user_session_increment (sm, u, 0);
417 kv.value = s - tsm->sessions;
418 if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &kv, 1))
419 nat_elog_notice ("in2out_ed key add failed");
422 if (ip->protocol == IP_PROTOCOL_TCP)
424 tcp_header_t *tcp = ip4_next_header (ip);
425 if (nat44_set_tcp_session_state_o2i (sm, s, tcp, thread_index))
430 nat44_session_update_counters (s, now, 0, thread_index);
431 /* Per-user LRU list maintenance */
432 nat44_session_update_lru (sm, s, thread_index);
436 create_bypass_for_fwd_worker (snat_main_t * sm, ip4_header_t * ip,
439 ip4_header_t ip_wkr = {
440 .src_address = ip->dst_address,
442 u32 thread_index = sm->worker_in2out_cb (&ip_wkr, rx_fib_index, 0);
444 create_bypass_for_fwd (sm, ip, rx_fib_index, thread_index);
447 #ifndef CLIB_MARCH_VARIANT
449 icmp_match_out2in_ed (snat_main_t * sm, vlib_node_runtime_t * node,
450 u32 thread_index, vlib_buffer_t * b, ip4_header_t * ip,
451 u8 * p_proto, snat_session_key_t * p_value,
452 u8 * p_dont_translate, void *d, void *e)
454 u32 next = ~0, sw_if_index, rx_fib_index;
455 icmp46_header_t *icmp;
456 nat_ed_ses_key_t key;
457 clib_bihash_kv_16_8_t kv, value;
458 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
459 snat_session_t *s = 0;
460 u8 dont_translate = 0, is_addr_only, identity_nat;
461 snat_session_key_t e_key, l_key;
463 icmp = (icmp46_header_t *) ip4_next_header (ip);
464 sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
465 rx_fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
467 if (get_icmp_o2i_ed_key (ip, &key))
469 b->error = node->errors[NAT_OUT2IN_ED_ERROR_UNSUPPORTED_PROTOCOL];
470 next = NAT44_ED_OUT2IN_NEXT_DROP;
473 key.fib_index = rx_fib_index;
474 kv.key[0] = key.as_u64[0];
475 kv.key[1] = key.as_u64[1];
477 if (clib_bihash_search_16_8 (&tsm->out2in_ed, &kv, &value))
479 /* Try to match static mapping */
480 e_key.addr = ip->dst_address;
481 e_key.port = key.l_port;
482 e_key.protocol = ip_proto_to_snat_proto (key.proto);
483 e_key.fib_index = rx_fib_index;
484 if (snat_static_mapping_match
485 (sm, e_key, &l_key, 1, &is_addr_only, 0, 0, 0, &identity_nat))
487 if (!sm->forwarding_enabled)
489 /* Don't NAT packet aimed at the intfc address */
490 if (PREDICT_FALSE (is_interface_addr (sm, node, sw_if_index,
491 ip->dst_address.as_u32)))
496 b->error = node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
497 next = NAT44_ED_OUT2IN_NEXT_DROP;
503 if (next_src_nat (sm, ip, key.proto, key.l_port, key.r_port,
504 thread_index, rx_fib_index))
506 next = NAT44_ED_OUT2IN_NEXT_IN2OUT;
509 if (sm->num_workers > 1)
510 create_bypass_for_fwd_worker (sm, ip, rx_fib_index);
512 create_bypass_for_fwd (sm, ip, rx_fib_index, thread_index);
517 if (PREDICT_FALSE (icmp->type != ICMP4_echo_reply &&
518 (icmp->type != ICMP4_echo_request || !is_addr_only)))
520 b->error = node->errors[NAT_OUT2IN_ED_ERROR_BAD_ICMP_TYPE];
521 next = NAT44_ED_OUT2IN_NEXT_DROP;
525 if (PREDICT_FALSE (identity_nat))
531 /* Create session initiated by host from external network */
532 s = create_session_for_static_mapping_ed (sm, b, l_key, e_key, node,
539 next = NAT44_ED_OUT2IN_NEXT_DROP;
545 if (PREDICT_FALSE (icmp->type != ICMP4_echo_reply &&
546 icmp->type != ICMP4_echo_request &&
547 !icmp_is_error_message (icmp)))
549 b->error = node->errors[NAT_OUT2IN_ED_ERROR_BAD_ICMP_TYPE];
550 next = NAT44_ED_OUT2IN_NEXT_DROP;
554 s = pool_elt_at_index (tsm->sessions, value.value);
557 *p_proto = ip_proto_to_snat_proto (key.proto);
560 *p_value = s->in2out;
561 *p_dont_translate = dont_translate;
563 *(snat_session_t **) d = s;
568 static snat_session_t *
569 nat44_ed_out2in_unknown_proto (snat_main_t * sm,
575 vlib_main_t * vm, vlib_node_runtime_t * node)
577 clib_bihash_kv_8_8_t kv, value;
578 clib_bihash_kv_16_8_t s_kv, s_value;
579 snat_static_mapping_t *m;
580 u32 old_addr, new_addr;
583 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
586 old_addr = ip->dst_address.as_u32;
588 make_ed_kv (&s_kv, &ip->dst_address, &ip->src_address, ip->protocol,
591 if (!clib_bihash_search_16_8 (&tsm->out2in_ed, &s_kv, &s_value))
593 s = pool_elt_at_index (tsm->sessions, s_value.value);
594 new_addr = ip->dst_address.as_u32 = s->in2out.addr.as_u32;
598 if (PREDICT_FALSE (maximum_sessions_exceeded (sm, thread_index)))
600 b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_SESSIONS_EXCEEDED];
601 nat_elog_notice ("maximum sessions exceeded");
605 make_sm_kv (&kv, &ip->dst_address, 0, 0, 0);
606 if (clib_bihash_search_8_8
607 (&sm->static_mapping_by_external, &kv, &value))
609 b->error = node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
613 m = pool_elt_at_index (sm->static_mappings, value.value);
615 new_addr = ip->dst_address.as_u32 = m->local_addr.as_u32;
617 u = nat_user_get_or_create (sm, &m->local_addr, m->fib_index,
621 nat_elog_warn ("create NAT user failed");
625 /* Create a new session */
626 s = nat_ed_session_alloc (sm, u, thread_index, now);
629 nat44_delete_user_with_no_session (sm, u, thread_index);
630 nat_elog_warn ("create NAT session failed");
634 s->ext_host_addr.as_u32 = ip->src_address.as_u32;
635 s->flags |= SNAT_SESSION_FLAG_UNKNOWN_PROTO;
636 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
637 s->flags |= SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT;
638 s->out2in.addr.as_u32 = old_addr;
639 s->out2in.fib_index = rx_fib_index;
640 s->in2out.addr.as_u32 = new_addr;
641 s->in2out.fib_index = m->fib_index;
642 s->in2out.port = s->out2in.port = ip->protocol;
643 user_session_increment (sm, u, 1);
645 /* Add to lookup tables */
646 s_kv.value = s - tsm->sessions;
647 if (clib_bihash_add_del_16_8 (&tsm->out2in_ed, &s_kv, 1))
648 nat_elog_notice ("out2in key add failed");
650 make_ed_kv (&s_kv, &ip->dst_address, &ip->src_address, ip->protocol,
652 s_kv.value = s - tsm->sessions;
653 if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &s_kv, 1))
654 nat_elog_notice ("in2out key add failed");
657 /* Update IP checksum */
659 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, dst_address);
660 ip->checksum = ip_csum_fold (sum);
662 vnet_buffer (b)->sw_if_index[VLIB_TX] = s->in2out.fib_index;
665 nat44_session_update_counters (s, now, vlib_buffer_length_in_chain (vm, b),
667 /* Per-user LRU list maintenance */
668 nat44_session_update_lru (sm, s, thread_index);
674 nat44_ed_out2in_node_fn_inline (vlib_main_t * vm,
675 vlib_node_runtime_t * node,
676 vlib_frame_t * frame, int is_slow_path)
678 u32 n_left_from, *from, *to_next, pkts_processed = 0, stats_node_index;
679 nat44_ed_out2in_next_t next_index;
680 snat_main_t *sm = &snat_main;
681 f64 now = vlib_time_now (vm);
682 u32 thread_index = vm->thread_index;
683 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
684 u32 tcp_packets = 0, udp_packets = 0, icmp_packets = 0, other_packets =
687 stats_node_index = is_slow_path ? sm->ed_out2in_slowpath_node_index :
688 sm->ed_out2in_node_index;
690 from = vlib_frame_vector_args (frame);
691 n_left_from = frame->n_vectors;
692 next_index = node->cached_next_index;
694 while (n_left_from > 0)
698 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
700 while (n_left_from >= 4 && n_left_to_next >= 2)
703 vlib_buffer_t *b0, *b1;
704 u32 next0, sw_if_index0, rx_fib_index0, proto0, old_addr0,
706 u32 next1, sw_if_index1, rx_fib_index1, proto1, old_addr1,
708 u16 old_port0, new_port0, old_port1, new_port1;
709 ip4_header_t *ip0, *ip1;
710 udp_header_t *udp0, *udp1;
711 tcp_header_t *tcp0, *tcp1;
712 icmp46_header_t *icmp0, *icmp1;
713 snat_session_t *s0 = 0, *s1 = 0;
714 clib_bihash_kv_16_8_t kv0, value0, kv1, value1;
715 ip_csum_t sum0, sum1;
716 snat_session_key_t e_key0, l_key0, e_key1, l_key1;
717 lb_nat_type_t lb_nat0, lb_nat1;
718 twice_nat_type_t twice_nat0, twice_nat1;
719 u8 identity_nat0, identity_nat1;
721 /* Prefetch next iteration. */
723 vlib_buffer_t *p2, *p3;
725 p2 = vlib_get_buffer (vm, from[2]);
726 p3 = vlib_get_buffer (vm, from[3]);
728 vlib_prefetch_buffer_header (p2, LOAD);
729 vlib_prefetch_buffer_header (p3, LOAD);
731 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
732 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
735 /* speculatively enqueue b0 and b1 to the current next frame */
736 to_next[0] = bi0 = from[0];
737 to_next[1] = bi1 = from[1];
743 b0 = vlib_get_buffer (vm, bi0);
744 b1 = vlib_get_buffer (vm, bi1);
746 next0 = NAT44_ED_OUT2IN_NEXT_LOOKUP;
747 vnet_buffer (b0)->snat.flags = 0;
748 ip0 = vlib_buffer_get_current (b0);
750 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
752 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
755 if (PREDICT_FALSE (ip0->ttl == 1))
757 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
758 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
759 ICMP4_time_exceeded_ttl_exceeded_in_transit,
761 next0 = NAT44_ED_OUT2IN_NEXT_ICMP_ERROR;
765 udp0 = ip4_next_header (ip0);
766 tcp0 = (tcp_header_t *) udp0;
767 icmp0 = (icmp46_header_t *) udp0;
768 proto0 = ip_proto_to_snat_proto (ip0->protocol);
772 if (PREDICT_FALSE (proto0 == ~0))
775 nat44_ed_out2in_unknown_proto (sm, b0, ip0, rx_fib_index0,
776 thread_index, now, vm,
779 if (!sm->forwarding_enabled)
782 next0 = NAT44_ED_OUT2IN_NEXT_DROP;
787 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
789 next0 = icmp_out2in_ed_slow_path
790 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
791 next0, now, thread_index, &s0);
798 if (PREDICT_FALSE (proto0 == ~0))
800 next0 = NAT44_ED_OUT2IN_NEXT_SLOW_PATH;
804 if (ip4_is_fragment (ip0))
806 next0 = NAT44_ED_OUT2IN_NEXT_REASS;
811 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
813 next0 = NAT44_ED_OUT2IN_NEXT_SLOW_PATH;
818 make_ed_kv (&kv0, &ip0->dst_address, &ip0->src_address,
819 ip0->protocol, rx_fib_index0, udp0->dst_port,
822 if (clib_bihash_search_16_8 (&tsm->out2in_ed, &kv0, &value0))
826 /* Try to match static mapping by external address and port,
827 destination address and port in packet */
828 e_key0.addr = ip0->dst_address;
829 e_key0.port = udp0->dst_port;
830 e_key0.protocol = proto0;
831 e_key0.fib_index = rx_fib_index0;
832 if (snat_static_mapping_match (sm, e_key0, &l_key0, 1, 0,
833 &twice_nat0, &lb_nat0,
838 * Send DHCP packets to the ipv4 stack, or we won't
839 * be able to use dhcp client on the outside interface
841 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_UDP
842 && (udp0->dst_port ==
844 (UDP_DST_PORT_dhcp_to_client))))
846 vnet_feature_next (&next0, b0);
850 if (!sm->forwarding_enabled)
853 node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
854 next0 = NAT44_ED_OUT2IN_NEXT_DROP;
858 if (next_src_nat (sm, ip0, ip0->protocol,
859 udp0->src_port, udp0->dst_port,
860 thread_index, rx_fib_index0))
862 next0 = NAT44_ED_OUT2IN_NEXT_IN2OUT;
865 if (sm->num_workers > 1)
866 create_bypass_for_fwd_worker (sm, ip0,
869 create_bypass_for_fwd (sm, ip0, rx_fib_index0,
875 if (PREDICT_FALSE (identity_nat0))
878 if ((proto0 == SNAT_PROTOCOL_TCP) && !tcp_is_init (tcp0))
880 b0->error = node->errors[NAT_OUT2IN_ED_ERROR_NON_SYN];
881 next0 = NAT44_ED_OUT2IN_NEXT_DROP;
885 /* Create session initiated by host from external network */
886 s0 = create_session_for_static_mapping_ed (sm, b0, l_key0,
894 next0 = NAT44_ED_OUT2IN_NEXT_DROP;
900 next0 = NAT44_ED_OUT2IN_NEXT_SLOW_PATH;
906 s0 = pool_elt_at_index (tsm->sessions, value0.value);
909 old_addr0 = ip0->dst_address.as_u32;
910 new_addr0 = ip0->dst_address.as_u32 = s0->in2out.addr.as_u32;
911 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
913 sum0 = ip0->checksum;
914 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
916 if (PREDICT_FALSE (is_twice_nat_session (s0)))
917 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
918 s0->ext_host_nat_addr.as_u32, ip4_header_t,
920 ip0->checksum = ip_csum_fold (sum0);
922 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
924 old_port0 = tcp0->dst_port;
925 new_port0 = tcp0->dst_port = s0->in2out.port;
927 sum0 = tcp0->checksum;
928 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
930 sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
932 if (is_twice_nat_session (s0))
934 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
935 s0->ext_host_nat_addr.as_u32,
936 ip4_header_t, dst_address);
937 sum0 = ip_csum_update (sum0, tcp0->src_port,
938 s0->ext_host_nat_port, ip4_header_t,
940 tcp0->src_port = s0->ext_host_nat_port;
941 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
943 tcp0->checksum = ip_csum_fold (sum0);
945 if (nat44_set_tcp_session_state_o2i
946 (sm, s0, tcp0, thread_index))
951 udp0->dst_port = s0->in2out.port;
952 if (is_twice_nat_session (s0))
954 udp0->src_port = s0->ext_host_nat_port;
955 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
962 nat44_session_update_counters (s0, now,
963 vlib_buffer_length_in_chain (vm, b0),
965 /* Per-user LRU list maintenance */
966 nat44_session_update_lru (sm, s0, thread_index);
969 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
970 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
972 nat44_ed_out2in_trace_t *t =
973 vlib_add_trace (vm, node, b0, sizeof (*t));
974 t->is_slow_path = is_slow_path;
975 t->sw_if_index = sw_if_index0;
976 t->next_index = next0;
977 t->session_index = ~0;
979 t->session_index = s0 - tsm->sessions;
982 pkts_processed += next0 == NAT44_ED_OUT2IN_NEXT_LOOKUP;
984 next1 = NAT44_ED_OUT2IN_NEXT_LOOKUP;
985 vnet_buffer (b1)->snat.flags = 0;
986 ip1 = vlib_buffer_get_current (b1);
988 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
990 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
993 if (PREDICT_FALSE (ip1->ttl == 1))
995 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
996 icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
997 ICMP4_time_exceeded_ttl_exceeded_in_transit,
999 next1 = NAT44_ED_OUT2IN_NEXT_ICMP_ERROR;
1003 udp1 = ip4_next_header (ip1);
1004 tcp1 = (tcp_header_t *) udp1;
1005 icmp1 = (icmp46_header_t *) udp1;
1006 proto1 = ip_proto_to_snat_proto (ip1->protocol);
1010 if (PREDICT_FALSE (proto1 == ~0))
1013 nat44_ed_out2in_unknown_proto (sm, b1, ip1, rx_fib_index1,
1014 thread_index, now, vm,
1017 if (!sm->forwarding_enabled)
1020 next1 = NAT44_ED_OUT2IN_NEXT_DROP;
1025 if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
1027 next1 = icmp_out2in_ed_slow_path
1028 (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node,
1029 next1, now, thread_index, &s1);
1036 if (PREDICT_FALSE (proto1 == ~0))
1038 next1 = NAT44_ED_OUT2IN_NEXT_SLOW_PATH;
1042 if (ip4_is_fragment (ip1))
1044 next1 = NAT44_ED_OUT2IN_NEXT_REASS;
1049 if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
1051 next1 = NAT44_ED_OUT2IN_NEXT_SLOW_PATH;
1056 make_ed_kv (&kv1, &ip1->dst_address, &ip1->src_address,
1057 ip1->protocol, rx_fib_index1, udp1->dst_port,
1060 if (clib_bihash_search_16_8 (&tsm->out2in_ed, &kv1, &value1))
1064 /* Try to match static mapping by external address and port,
1065 destination address and port in packet */
1066 e_key1.addr = ip1->dst_address;
1067 e_key1.port = udp1->dst_port;
1068 e_key1.protocol = proto1;
1069 e_key1.fib_index = rx_fib_index1;
1070 if (snat_static_mapping_match (sm, e_key1, &l_key1, 1, 0,
1071 &twice_nat1, &lb_nat1,
1076 * Send DHCP packets to the ipv4 stack, or we won't
1077 * be able to use dhcp client on the outside interface
1079 if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_UDP
1080 && (udp1->dst_port ==
1081 clib_host_to_net_u16
1082 (UDP_DST_PORT_dhcp_to_client))))
1084 vnet_feature_next (&next1, b1);
1088 if (!sm->forwarding_enabled)
1091 node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
1092 next1 = NAT44_ED_OUT2IN_NEXT_DROP;
1096 if (next_src_nat (sm, ip1, ip1->protocol,
1097 udp1->src_port, udp1->dst_port,
1098 thread_index, rx_fib_index1))
1100 next1 = NAT44_ED_OUT2IN_NEXT_IN2OUT;
1103 if (sm->num_workers > 1)
1104 create_bypass_for_fwd_worker (sm, ip1,
1107 create_bypass_for_fwd (sm, ip1, rx_fib_index1,
1113 if (PREDICT_FALSE (identity_nat1))
1116 if ((proto1 == SNAT_PROTOCOL_TCP) && !tcp_is_init (tcp1))
1118 b1->error = node->errors[NAT_OUT2IN_ED_ERROR_NON_SYN];
1119 next1 = NAT44_ED_OUT2IN_NEXT_DROP;
1123 /* Create session initiated by host from external network */
1124 s1 = create_session_for_static_mapping_ed (sm, b1, l_key1,
1132 next1 = NAT44_ED_OUT2IN_NEXT_DROP;
1138 next1 = NAT44_ED_OUT2IN_NEXT_SLOW_PATH;
1144 s1 = pool_elt_at_index (tsm->sessions, value1.value);
1147 old_addr1 = ip1->dst_address.as_u32;
1148 new_addr1 = ip1->dst_address.as_u32 = s1->in2out.addr.as_u32;
1149 vnet_buffer (b1)->sw_if_index[VLIB_TX] = s1->in2out.fib_index;
1151 sum1 = ip1->checksum;
1152 sum1 = ip_csum_update (sum1, old_addr1, new_addr1, ip4_header_t,
1154 if (PREDICT_FALSE (is_twice_nat_session (s1)))
1155 sum1 = ip_csum_update (sum1, ip1->src_address.as_u32,
1156 s1->ext_host_nat_addr.as_u32, ip4_header_t,
1158 ip1->checksum = ip_csum_fold (sum1);
1160 if (PREDICT_TRUE (proto1 == SNAT_PROTOCOL_TCP))
1162 old_port1 = tcp1->dst_port;
1163 new_port1 = tcp1->dst_port = s1->in2out.port;
1165 sum1 = tcp1->checksum;
1166 sum1 = ip_csum_update (sum1, old_addr1, new_addr1, ip4_header_t,
1168 sum1 = ip_csum_update (sum1, old_port1, new_port1, ip4_header_t,
1170 if (is_twice_nat_session (s1))
1172 sum1 = ip_csum_update (sum1, ip1->src_address.as_u32,
1173 s1->ext_host_nat_addr.as_u32,
1174 ip4_header_t, dst_address);
1175 sum1 = ip_csum_update (sum1, tcp1->src_port,
1176 s1->ext_host_nat_port, ip4_header_t,
1178 tcp1->src_port = s1->ext_host_nat_port;
1179 ip1->src_address.as_u32 = s1->ext_host_nat_addr.as_u32;
1181 tcp1->checksum = ip_csum_fold (sum1);
1183 if (nat44_set_tcp_session_state_o2i
1184 (sm, s1, tcp1, thread_index))
1189 udp1->dst_port = s1->in2out.port;
1190 if (is_twice_nat_session (s1))
1192 udp1->src_port = s1->ext_host_nat_port;
1193 ip1->src_address.as_u32 = s1->ext_host_nat_addr.as_u32;
1200 nat44_session_update_counters (s1, now,
1201 vlib_buffer_length_in_chain (vm, b1),
1203 /* Per-user LRU list maintenance */
1204 nat44_session_update_lru (sm, s1, thread_index);
1207 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1208 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
1210 nat44_ed_out2in_trace_t *t =
1211 vlib_add_trace (vm, node, b1, sizeof (*t));
1212 t->is_slow_path = is_slow_path;
1213 t->sw_if_index = sw_if_index1;
1214 t->next_index = next1;
1215 t->session_index = ~0;
1217 t->session_index = s1 - tsm->sessions;
1220 pkts_processed += next1 == NAT44_ED_OUT2IN_NEXT_LOOKUP;
1222 /* verify speculative enqueues, maybe switch current next frame */
1223 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1224 to_next, n_left_to_next,
1225 bi0, bi1, next0, next1);
1228 while (n_left_from > 0 && n_left_to_next > 0)
1232 u32 next0, sw_if_index0, rx_fib_index0, proto0, old_addr0,
1234 u16 old_port0, new_port0;
1238 icmp46_header_t *icmp0;
1239 snat_session_t *s0 = 0;
1240 clib_bihash_kv_16_8_t kv0, value0;
1242 snat_session_key_t e_key0, l_key0;
1243 lb_nat_type_t lb_nat0;
1244 twice_nat_type_t twice_nat0;
1247 /* speculatively enqueue b0 to the current next frame */
1253 n_left_to_next -= 1;
1255 b0 = vlib_get_buffer (vm, bi0);
1256 next0 = NAT44_ED_OUT2IN_NEXT_LOOKUP;
1257 vnet_buffer (b0)->snat.flags = 0;
1258 ip0 = vlib_buffer_get_current (b0);
1260 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1262 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1265 if (PREDICT_FALSE (ip0->ttl == 1))
1267 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1268 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1269 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1271 next0 = NAT44_ED_OUT2IN_NEXT_ICMP_ERROR;
1275 udp0 = ip4_next_header (ip0);
1276 tcp0 = (tcp_header_t *) udp0;
1277 icmp0 = (icmp46_header_t *) udp0;
1278 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1282 if (PREDICT_FALSE (proto0 == ~0))
1285 nat44_ed_out2in_unknown_proto (sm, b0, ip0, rx_fib_index0,
1286 thread_index, now, vm,
1289 if (!sm->forwarding_enabled)
1292 next0 = NAT44_ED_OUT2IN_NEXT_DROP;
1297 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1299 next0 = icmp_out2in_ed_slow_path
1300 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1301 next0, now, thread_index, &s0);
1308 if (PREDICT_FALSE (proto0 == ~0))
1310 next0 = NAT44_ED_OUT2IN_NEXT_SLOW_PATH;
1314 if (ip4_is_fragment (ip0))
1316 next0 = NAT44_ED_OUT2IN_NEXT_REASS;
1321 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1323 next0 = NAT44_ED_OUT2IN_NEXT_SLOW_PATH;
1328 make_ed_kv (&kv0, &ip0->dst_address, &ip0->src_address,
1329 ip0->protocol, rx_fib_index0, udp0->dst_port,
1332 if (clib_bihash_search_16_8 (&tsm->out2in_ed, &kv0, &value0))
1336 /* Try to match static mapping by external address and port,
1337 destination address and port in packet */
1338 e_key0.addr = ip0->dst_address;
1339 e_key0.port = udp0->dst_port;
1340 e_key0.protocol = proto0;
1341 e_key0.fib_index = rx_fib_index0;
1342 if (snat_static_mapping_match (sm, e_key0, &l_key0, 1, 0,
1343 &twice_nat0, &lb_nat0,
1348 * Send DHCP packets to the ipv4 stack, or we won't
1349 * be able to use dhcp client on the outside interface
1351 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_UDP
1352 && (udp0->dst_port ==
1353 clib_host_to_net_u16
1354 (UDP_DST_PORT_dhcp_to_client))))
1356 vnet_feature_next (&next0, b0);
1360 if (!sm->forwarding_enabled)
1363 node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
1364 next0 = NAT44_ED_OUT2IN_NEXT_DROP;
1368 if (next_src_nat (sm, ip0, ip0->protocol,
1369 udp0->src_port, udp0->dst_port,
1370 thread_index, rx_fib_index0))
1372 next0 = NAT44_ED_OUT2IN_NEXT_IN2OUT;
1375 if (sm->num_workers > 1)
1376 create_bypass_for_fwd_worker (sm, ip0,
1379 create_bypass_for_fwd (sm, ip0, rx_fib_index0,
1385 if (PREDICT_FALSE (identity_nat0))
1388 if ((proto0 == SNAT_PROTOCOL_TCP) && !tcp_is_init (tcp0))
1390 b0->error = node->errors[NAT_OUT2IN_ED_ERROR_NON_SYN];
1391 next0 = NAT44_ED_OUT2IN_NEXT_DROP;
1395 /* Create session initiated by host from external network */
1396 s0 = create_session_for_static_mapping_ed (sm, b0, l_key0,
1404 next0 = NAT44_ED_OUT2IN_NEXT_DROP;
1410 next0 = NAT44_ED_OUT2IN_NEXT_SLOW_PATH;
1416 s0 = pool_elt_at_index (tsm->sessions, value0.value);
1419 old_addr0 = ip0->dst_address.as_u32;
1420 new_addr0 = ip0->dst_address.as_u32 = s0->in2out.addr.as_u32;
1421 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
1423 sum0 = ip0->checksum;
1424 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1426 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1427 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1428 s0->ext_host_nat_addr.as_u32, ip4_header_t,
1430 ip0->checksum = ip_csum_fold (sum0);
1432 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1434 old_port0 = tcp0->dst_port;
1435 new_port0 = tcp0->dst_port = s0->in2out.port;
1437 sum0 = tcp0->checksum;
1438 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1440 sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
1442 if (is_twice_nat_session (s0))
1444 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1445 s0->ext_host_nat_addr.as_u32,
1446 ip4_header_t, dst_address);
1447 sum0 = ip_csum_update (sum0, tcp0->src_port,
1448 s0->ext_host_nat_port, ip4_header_t,
1450 tcp0->src_port = s0->ext_host_nat_port;
1451 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1453 tcp0->checksum = ip_csum_fold (sum0);
1455 if (nat44_set_tcp_session_state_o2i
1456 (sm, s0, tcp0, thread_index))
1461 udp0->dst_port = s0->in2out.port;
1462 if (is_twice_nat_session (s0))
1464 udp0->src_port = s0->ext_host_nat_port;
1465 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1472 nat44_session_update_counters (s0, now,
1473 vlib_buffer_length_in_chain (vm, b0),
1475 /* Per-user LRU list maintenance */
1476 nat44_session_update_lru (sm, s0, thread_index);
1479 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1480 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1482 nat44_ed_out2in_trace_t *t =
1483 vlib_add_trace (vm, node, b0, sizeof (*t));
1484 t->is_slow_path = is_slow_path;
1485 t->sw_if_index = sw_if_index0;
1486 t->next_index = next0;
1487 t->session_index = ~0;
1489 t->session_index = s0 - tsm->sessions;
1492 pkts_processed += next0 == NAT44_ED_OUT2IN_NEXT_LOOKUP;
1493 /* verify speculative enqueue, maybe switch current next frame */
1494 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1495 to_next, n_left_to_next,
1499 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1502 vlib_node_increment_counter (vm, stats_node_index,
1503 NAT_OUT2IN_ED_ERROR_OUT2IN_PACKETS,
1505 vlib_node_increment_counter (vm, stats_node_index,
1506 NAT_OUT2IN_ED_ERROR_TCP_PACKETS, tcp_packets);
1507 vlib_node_increment_counter (vm, stats_node_index,
1508 NAT_OUT2IN_ED_ERROR_UDP_PACKETS, udp_packets);
1509 vlib_node_increment_counter (vm, stats_node_index,
1510 NAT_OUT2IN_ED_ERROR_ICMP_PACKETS,
1512 vlib_node_increment_counter (vm, stats_node_index,
1513 NAT_OUT2IN_ED_ERROR_OTHER_PACKETS,
1515 vlib_node_increment_counter (vm, stats_node_index,
1516 NAT_OUT2IN_ED_ERROR_FRAGMENTS, fragments);
1517 return frame->n_vectors;
1520 VLIB_NODE_FN (nat44_ed_out2in_node) (vlib_main_t * vm,
1521 vlib_node_runtime_t * node,
1522 vlib_frame_t * frame)
1524 return nat44_ed_out2in_node_fn_inline (vm, node, frame, 0);
1528 VLIB_REGISTER_NODE (nat44_ed_out2in_node) = {
1529 .name = "nat44-ed-out2in",
1530 .vector_size = sizeof (u32),
1531 .format_trace = format_nat44_ed_out2in_trace,
1532 .type = VLIB_NODE_TYPE_INTERNAL,
1533 .n_errors = ARRAY_LEN(nat_out2in_ed_error_strings),
1534 .error_strings = nat_out2in_ed_error_strings,
1535 .runtime_data_bytes = sizeof (snat_runtime_t),
1536 .n_next_nodes = NAT44_ED_OUT2IN_N_NEXT,
1538 [NAT44_ED_OUT2IN_NEXT_DROP] = "error-drop",
1539 [NAT44_ED_OUT2IN_NEXT_LOOKUP] = "ip4-lookup",
1540 [NAT44_ED_OUT2IN_NEXT_SLOW_PATH] = "nat44-ed-out2in-slowpath",
1541 [NAT44_ED_OUT2IN_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1542 [NAT44_ED_OUT2IN_NEXT_IN2OUT] = "nat44-ed-in2out",
1543 [NAT44_ED_OUT2IN_NEXT_REASS] = "nat44-ed-out2in-reass",
1548 VLIB_NODE_FN (nat44_ed_out2in_slowpath_node) (vlib_main_t * vm,
1549 vlib_node_runtime_t * node,
1550 vlib_frame_t * frame)
1552 return nat44_ed_out2in_node_fn_inline (vm, node, frame, 1);
1556 VLIB_REGISTER_NODE (nat44_ed_out2in_slowpath_node) = {
1557 .name = "nat44-ed-out2in-slowpath",
1558 .vector_size = sizeof (u32),
1559 .format_trace = format_nat44_ed_out2in_trace,
1560 .type = VLIB_NODE_TYPE_INTERNAL,
1561 .n_errors = ARRAY_LEN(nat_out2in_ed_error_strings),
1562 .error_strings = nat_out2in_ed_error_strings,
1563 .runtime_data_bytes = sizeof (snat_runtime_t),
1564 .n_next_nodes = NAT44_ED_OUT2IN_N_NEXT,
1566 [NAT44_ED_OUT2IN_NEXT_DROP] = "error-drop",
1567 [NAT44_ED_OUT2IN_NEXT_LOOKUP] = "ip4-lookup",
1568 [NAT44_ED_OUT2IN_NEXT_SLOW_PATH] = "nat44-ed-out2in-slowpath",
1569 [NAT44_ED_OUT2IN_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1570 [NAT44_ED_OUT2IN_NEXT_IN2OUT] = "nat44-ed-in2out",
1571 [NAT44_ED_OUT2IN_NEXT_REASS] = "nat44-ed-out2in-reass",
1576 VLIB_NODE_FN (nat44_ed_out2in_reass_node) (vlib_main_t * vm,
1577 vlib_node_runtime_t * node,
1578 vlib_frame_t * frame)
1580 u32 n_left_from, *from, *to_next;
1581 nat44_ed_out2in_next_t next_index;
1582 u32 pkts_processed = 0;
1583 snat_main_t *sm = &snat_main;
1584 f64 now = vlib_time_now (vm);
1585 u32 thread_index = vm->thread_index;
1586 snat_main_per_thread_data_t *per_thread_data =
1587 &sm->per_thread_data[thread_index];
1588 u32 *fragments_to_drop = 0;
1589 u32 *fragments_to_loopback = 0;
1591 from = vlib_frame_vector_args (frame);
1592 n_left_from = frame->n_vectors;
1593 next_index = node->cached_next_index;
1595 while (n_left_from > 0)
1599 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1601 while (n_left_from > 0 && n_left_to_next > 0)
1603 u32 bi0, sw_if_index0, proto0, rx_fib_index0, new_addr0, old_addr0;
1608 nat_reass_ip4_t *reass0;
1611 icmp46_header_t *icmp0;
1612 clib_bihash_kv_16_8_t kv0, value0;
1613 snat_session_t *s0 = 0;
1614 u16 old_port0, new_port0;
1616 snat_session_key_t e_key0, l_key0;
1618 twice_nat_type_t twice_nat0;
1621 /* speculatively enqueue b0 to the current next frame */
1627 n_left_to_next -= 1;
1629 b0 = vlib_get_buffer (vm, bi0);
1630 next0 = NAT44_ED_OUT2IN_NEXT_LOOKUP;
1632 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1634 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1637 if (PREDICT_FALSE (nat_reass_is_drop_frag (0)))
1639 next0 = NAT44_ED_OUT2IN_NEXT_DROP;
1640 b0->error = node->errors[NAT_OUT2IN_ED_ERROR_DROP_FRAGMENT];
1644 ip0 = (ip4_header_t *) vlib_buffer_get_current (b0);
1645 udp0 = ip4_next_header (ip0);
1646 tcp0 = (tcp_header_t *) udp0;
1647 icmp0 = (icmp46_header_t *) udp0;
1648 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1650 reass0 = nat_ip4_reass_find_or_create (ip0->src_address,
1654 1, &fragments_to_drop);
1656 if (PREDICT_FALSE (!reass0))
1658 next0 = NAT44_ED_OUT2IN_NEXT_DROP;
1659 b0->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_REASS];
1660 nat_elog_notice ("maximum reassemblies exceeded");
1664 if (PREDICT_FALSE (ip4_is_first_fragment (ip0)))
1666 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1668 next0 = icmp_out2in_ed_slow_path
1669 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1670 next0, now, thread_index, &s0);
1672 if (PREDICT_TRUE (next0 != NAT44_ED_OUT2IN_NEXT_DROP))
1675 reass0->sess_index = s0 - per_thread_data->sessions;
1677 reass0->flags |= NAT_REASS_FLAG_ED_DONT_TRANSLATE;
1678 reass0->thread_index = thread_index;
1679 nat_ip4_reass_get_frags (reass0,
1680 &fragments_to_loopback);
1686 make_ed_kv (&kv0, &ip0->dst_address, &ip0->src_address,
1687 ip0->protocol, rx_fib_index0, udp0->dst_port,
1690 if (clib_bihash_search_16_8
1691 (&per_thread_data->out2in_ed, &kv0, &value0))
1693 /* Try to match static mapping by external address and port,
1694 destination address and port in packet */
1695 e_key0.addr = ip0->dst_address;
1696 e_key0.port = udp0->dst_port;
1697 e_key0.protocol = proto0;
1698 e_key0.fib_index = rx_fib_index0;
1699 if (snat_static_mapping_match (sm, e_key0, &l_key0, 1, 0,
1700 &twice_nat0, &lb0, 0,
1704 * Send DHCP packets to the ipv4 stack, or we won't
1705 * be able to use dhcp client on the outside interface
1707 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_UDP
1710 clib_host_to_net_u16
1711 (UDP_DST_PORT_dhcp_to_client))))
1713 vnet_feature_next (&next0, b0);
1717 if (!sm->forwarding_enabled)
1720 node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
1721 next0 = NAT44_ED_OUT2IN_NEXT_DROP;
1725 if (next_src_nat (sm, ip0, ip0->protocol,
1726 udp0->src_port, udp0->dst_port,
1727 thread_index, rx_fib_index0))
1729 next0 = NAT44_ED_OUT2IN_NEXT_IN2OUT;
1732 if (sm->num_workers > 1)
1733 create_bypass_for_fwd_worker (sm, ip0,
1736 create_bypass_for_fwd (sm, ip0, rx_fib_index0,
1738 reass0->flags |= NAT_REASS_FLAG_ED_DONT_TRANSLATE;
1739 nat_ip4_reass_get_frags (reass0,
1740 &fragments_to_loopback);
1745 if (PREDICT_FALSE (identity_nat0))
1747 reass0->flags |= NAT_REASS_FLAG_ED_DONT_TRANSLATE;
1751 if ((proto0 == SNAT_PROTOCOL_TCP) && !tcp_is_init (tcp0))
1753 b0->error = node->errors[NAT_OUT2IN_ED_ERROR_NON_SYN];
1754 next0 = NAT44_ED_OUT2IN_NEXT_DROP;
1758 /* Create session initiated by host from external network */
1759 s0 = create_session_for_static_mapping_ed (sm, b0, l_key0,
1767 node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
1768 next0 = NAT44_ED_OUT2IN_NEXT_DROP;
1771 reass0->sess_index = s0 - per_thread_data->sessions;
1772 reass0->thread_index = thread_index;
1776 s0 = pool_elt_at_index (per_thread_data->sessions,
1778 reass0->sess_index = value0.value;
1780 nat_ip4_reass_get_frags (reass0, &fragments_to_loopback);
1784 if (reass0->flags & NAT_REASS_FLAG_ED_DONT_TRANSLATE)
1786 if (PREDICT_FALSE (reass0->sess_index == (u32) ~ 0))
1788 if (nat_ip4_reass_add_fragment
1789 (thread_index, reass0, bi0, &fragments_to_drop))
1791 b0->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_FRAG];
1793 ("maximum fragments per reassembly exceeded");
1794 next0 = NAT44_ED_OUT2IN_NEXT_DROP;
1800 s0 = pool_elt_at_index (per_thread_data->sessions,
1801 reass0->sess_index);
1804 old_addr0 = ip0->dst_address.as_u32;
1805 ip0->dst_address = s0->in2out.addr;
1806 new_addr0 = ip0->dst_address.as_u32;
1807 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
1809 sum0 = ip0->checksum;
1810 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1812 dst_address /* changed member */ );
1813 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1814 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1815 s0->ext_host_nat_addr.as_u32, ip4_header_t,
1817 ip0->checksum = ip_csum_fold (sum0);
1819 if (PREDICT_FALSE (ip4_is_first_fragment (ip0)))
1821 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1823 old_port0 = tcp0->dst_port;
1824 tcp0->dst_port = s0->in2out.port;
1825 new_port0 = tcp0->dst_port;
1827 sum0 = tcp0->checksum;
1828 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1830 dst_address /* changed member */ );
1832 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1833 ip4_header_t /* cheat */ ,
1834 length /* changed member */ );
1835 if (is_twice_nat_session (s0))
1837 sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1838 s0->ext_host_nat_addr.as_u32,
1839 ip4_header_t, dst_address);
1840 sum0 = ip_csum_update (sum0, tcp0->src_port,
1841 s0->ext_host_nat_port,
1842 ip4_header_t, length);
1843 tcp0->src_port = s0->ext_host_nat_port;
1844 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1846 tcp0->checksum = ip_csum_fold (sum0);
1850 old_port0 = udp0->dst_port;
1851 udp0->dst_port = s0->in2out.port;
1852 if (is_twice_nat_session (s0))
1854 udp0->src_port = s0->ext_host_nat_port;
1855 ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1862 nat44_session_update_counters (s0, now,
1863 vlib_buffer_length_in_chain (vm, b0),
1865 /* Per-user LRU list maintenance */
1866 nat44_session_update_lru (sm, s0, thread_index);
1869 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1870 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1872 nat44_reass_trace_t *t =
1873 vlib_add_trace (vm, node, b0, sizeof (*t));
1874 t->cached = cached0;
1875 t->sw_if_index = sw_if_index0;
1876 t->next_index = next0;
1886 pkts_processed += next0 != NAT44_ED_OUT2IN_NEXT_DROP;
1888 /* verify speculative enqueue, maybe switch current next frame */
1889 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1890 to_next, n_left_to_next,
1894 if (n_left_from == 0 && vec_len (fragments_to_loopback))
1896 from = vlib_frame_vector_args (frame);
1897 u32 len = vec_len (fragments_to_loopback);
1898 if (len <= VLIB_FRAME_SIZE)
1900 clib_memcpy_fast (from, fragments_to_loopback,
1901 sizeof (u32) * len);
1903 vec_reset_length (fragments_to_loopback);
1907 clib_memcpy_fast (from, fragments_to_loopback +
1908 (len - VLIB_FRAME_SIZE),
1909 sizeof (u32) * VLIB_FRAME_SIZE);
1910 n_left_from = VLIB_FRAME_SIZE;
1911 _vec_len (fragments_to_loopback) = len - VLIB_FRAME_SIZE;
1916 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1919 vlib_node_increment_counter (vm, sm->ed_out2in_reass_node_index,
1920 NAT_OUT2IN_ED_ERROR_OUT2IN_PACKETS,
1923 nat_send_all_to_node (vm, fragments_to_drop, node,
1924 &node->errors[NAT_OUT2IN_ED_ERROR_DROP_FRAGMENT],
1925 NAT44_ED_OUT2IN_NEXT_DROP);
1927 vec_free (fragments_to_drop);
1928 vec_free (fragments_to_loopback);
1929 return frame->n_vectors;
1933 VLIB_REGISTER_NODE (nat44_ed_out2in_reass_node) = {
1934 .name = "nat44-ed-out2in-reass",
1935 .vector_size = sizeof (u32),
1936 .format_trace = format_nat44_reass_trace,
1937 .type = VLIB_NODE_TYPE_INTERNAL,
1938 .n_errors = ARRAY_LEN(nat_out2in_ed_error_strings),
1939 .error_strings = nat_out2in_ed_error_strings,
1940 .n_next_nodes = NAT44_ED_OUT2IN_N_NEXT,
1942 [NAT44_ED_OUT2IN_NEXT_DROP] = "error-drop",
1943 [NAT44_ED_OUT2IN_NEXT_LOOKUP] = "ip4-lookup",
1944 [NAT44_ED_OUT2IN_NEXT_SLOW_PATH] = "nat44-ed-out2in-slowpath",
1945 [NAT44_ED_OUT2IN_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1946 [NAT44_ED_OUT2IN_NEXT_IN2OUT] = "nat44-ed-in2out",
1947 [NAT44_ED_OUT2IN_NEXT_REASS] = "nat44-ed-out2in-reass",
1953 * fd.io coding-style-patch-verification: ON
1956 * eval: (c-set-style "gnu")