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 inside to outside 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 <vppinfra/error.h>
28 #include <nat/nat_ipfix_logging.h>
29 #include <nat/nat_reass.h>
30 #include <nat/nat_inlines.h>
31 #include <nat/nat_syslog.h>
32 #include <nat/nat_ha.h>
34 #define foreach_nat_in2out_ed_error \
35 _(UNSUPPORTED_PROTOCOL, "unsupported protocol") \
36 _(IN2OUT_PACKETS, "good in2out packets processed") \
37 _(OUT_OF_PORTS, "out of ports") \
38 _(BAD_ICMP_TYPE, "unsupported ICMP type") \
39 _(MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded") \
40 _(DROP_FRAGMENT, "drop fragment") \
41 _(MAX_REASS, "maximum reassemblies exceeded") \
42 _(MAX_FRAG, "maximum fragments per reassembly exceeded")\
43 _(NON_SYN, "non-SYN packet try to create session") \
44 _(TCP_PACKETS, "TCP packets") \
45 _(UDP_PACKETS, "UDP packets") \
46 _(ICMP_PACKETS, "ICMP packets") \
47 _(OTHER_PACKETS, "other protocol packets") \
48 _(FRAGMENTS, "fragments") \
49 _(CACHED_FRAGMENTS, "cached fragments") \
50 _(PROCESSED_FRAGMENTS, "processed fragments")
55 #define _(sym,str) NAT_IN2OUT_ED_ERROR_##sym,
56 foreach_nat_in2out_ed_error
58 NAT_IN2OUT_ED_N_ERROR,
59 } nat_in2out_ed_error_t;
61 static char *nat_in2out_ed_error_strings[] = {
62 #define _(sym,string) string,
63 foreach_nat_in2out_ed_error
69 NAT_IN2OUT_ED_NEXT_LOOKUP,
70 NAT_IN2OUT_ED_NEXT_DROP,
71 NAT_IN2OUT_ED_NEXT_ICMP_ERROR,
72 NAT_IN2OUT_ED_NEXT_SLOW_PATH,
73 NAT_IN2OUT_ED_NEXT_REASS,
75 } nat_in2out_ed_next_t;
83 } nat_in2out_ed_trace_t;
86 format_nat_in2out_ed_trace (u8 * s, va_list * args)
88 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
89 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
90 nat_in2out_ed_trace_t *t = va_arg (*args, nat_in2out_ed_trace_t *);
94 t->is_slow_path ? "NAT44_IN2OUT_ED_SLOW_PATH" :
95 "NAT44_IN2OUT_ED_FAST_PATH";
97 s = format (s, "%s: sw_if_index %d, next index %d, session %d", tag,
98 t->sw_if_index, t->next_index, t->session_index);
103 static_always_inline int
104 icmp_get_ed_key (ip4_header_t * ip0, nat_ed_ses_key_t * p_key0)
106 icmp46_header_t *icmp0;
107 nat_ed_ses_key_t key0;
108 icmp_echo_header_t *echo0, *inner_echo0 = 0;
109 ip4_header_t *inner_ip0 = 0;
111 icmp46_header_t *inner_icmp0;
113 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
114 echo0 = (icmp_echo_header_t *) (icmp0 + 1);
116 if (!icmp_is_error_message (icmp0))
118 key0.proto = IP_PROTOCOL_ICMP;
119 key0.l_addr = ip0->src_address;
120 key0.r_addr = ip0->dst_address;
121 key0.l_port = echo0->identifier;
126 inner_ip0 = (ip4_header_t *) (echo0 + 1);
127 l4_header = ip4_next_header (inner_ip0);
128 key0.proto = inner_ip0->protocol;
129 key0.r_addr = inner_ip0->src_address;
130 key0.l_addr = inner_ip0->dst_address;
131 switch (ip_proto_to_snat_proto (inner_ip0->protocol))
133 case SNAT_PROTOCOL_ICMP:
134 inner_icmp0 = (icmp46_header_t *) l4_header;
135 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
137 key0.l_port = inner_echo0->identifier;
139 case SNAT_PROTOCOL_UDP:
140 case SNAT_PROTOCOL_TCP:
141 key0.l_port = ((tcp_udp_header_t *) l4_header)->dst_port;
142 key0.r_port = ((tcp_udp_header_t *) l4_header)->src_port;
145 return NAT_IN2OUT_ED_ERROR_UNSUPPORTED_PROTOCOL;
152 #ifndef CLIB_MARCH_VARIANT
154 nat44_i2o_ed_is_idle_session_cb (clib_bihash_kv_16_8_t * kv, void *arg)
156 snat_main_t *sm = &snat_main;
157 nat44_is_idle_session_ctx_t *ctx = arg;
159 u64 sess_timeout_time;
160 nat_ed_ses_key_t ed_key;
161 clib_bihash_kv_16_8_t ed_kv;
164 snat_session_key_t key;
165 snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
168 s = pool_elt_at_index (tsm->sessions, kv->value);
169 sess_timeout_time = s->last_heard + (f64) nat44_session_get_timeout (sm, s);
170 if (ctx->now >= sess_timeout_time)
172 if (is_fwd_bypass_session (s))
175 ed_key.l_addr = s->out2in.addr;
176 ed_key.r_addr = s->ext_host_addr;
177 ed_key.fib_index = s->out2in.fib_index;
178 if (snat_is_unk_proto_session (s))
180 ed_key.proto = s->in2out.port;
186 ed_key.proto = snat_proto_to_ip_proto (s->in2out.protocol);
187 ed_key.l_port = s->out2in.port;
188 ed_key.r_port = s->ext_host_port;
190 ed_kv.key[0] = ed_key.as_u64[0];
191 ed_kv.key[1] = ed_key.as_u64[1];
192 if (clib_bihash_add_del_16_8 (&tsm->out2in_ed, &ed_kv, 0))
193 nat_elog_warn ("out2in_ed key del failed");
195 if (snat_is_unk_proto_session (s))
198 snat_ipfix_logging_nat44_ses_delete (ctx->thread_index,
199 s->in2out.addr.as_u32,
200 s->out2in.addr.as_u32,
204 s->in2out.fib_index);
206 nat_syslog_nat44_sdel (s->user_index, s->in2out.fib_index,
207 &s->in2out.addr, s->in2out.port,
208 &s->ext_host_nat_addr, s->ext_host_nat_port,
209 &s->out2in.addr, s->out2in.port,
210 &s->ext_host_addr, s->ext_host_port,
211 s->in2out.protocol, is_twice_nat_session (s));
213 nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
214 s->ext_host_port, s->out2in.protocol, s->out2in.fib_index,
217 if (is_twice_nat_session (s))
219 for (i = 0; i < vec_len (sm->twice_nat_addresses); i++)
221 key.protocol = s->in2out.protocol;
222 key.port = s->ext_host_nat_port;
223 a = sm->twice_nat_addresses + i;
224 if (a->addr.as_u32 == s->ext_host_nat_addr.as_u32)
226 snat_free_outside_address_and_port (sm->twice_nat_addresses,
234 if (snat_is_session_static (s))
237 snat_free_outside_address_and_port (sm->addresses, ctx->thread_index,
240 nat44_delete_session (sm, s, ctx->thread_index);
249 icmp_in2out_ed_slow_path (snat_main_t * sm, vlib_buffer_t * b0,
250 ip4_header_t * ip0, icmp46_header_t * icmp0,
251 u32 sw_if_index0, u32 rx_fib_index0,
252 vlib_node_runtime_t * node, u32 next0, f64 now,
253 u32 thread_index, snat_session_t ** p_s0)
255 next0 = icmp_in2out (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
256 next0, thread_index, p_s0, 0);
257 snat_session_t *s0 = *p_s0;
258 if (PREDICT_TRUE (next0 != NAT_IN2OUT_ED_NEXT_DROP && s0))
261 nat44_session_update_counters (s0, now,
262 vlib_buffer_length_in_chain
263 (sm->vlib_main, b0), thread_index);
264 /* Per-user LRU list maintenance */
265 nat44_session_update_lru (sm, s0, thread_index);
271 slow_path_ed (snat_main_t * sm,
274 clib_bihash_kv_16_8_t * kv,
275 snat_session_t ** sessionp,
276 vlib_node_runtime_t * node, u32 next, u32 thread_index, f64 now,
279 snat_session_t *s = 0;
281 snat_session_key_t key0, key1;
282 lb_nat_type_t lb = 0, is_sm = 0;
283 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
284 nat_ed_ses_key_t *key = (nat_ed_ses_key_t *) kv->key;
285 u32 proto = ip_proto_to_snat_proto (key->proto);
286 nat_outside_fib_t *outside_fib;
287 fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
290 .fp_proto = FIB_PROTOCOL_IP4,
293 .ip4.as_u32 = key->r_addr.as_u32,
296 nat44_is_idle_session_ctx_t ctx;
298 if (PREDICT_FALSE (maximum_sessions_exceeded (sm, thread_index)))
300 b->error = node->errors[NAT_IN2OUT_ED_ERROR_MAX_SESSIONS_EXCEEDED];
301 nat_ipfix_logging_max_sessions (thread_index, sm->max_translations);
302 nat_elog_notice ("maximum sessions exceeded");
303 return NAT_IN2OUT_ED_NEXT_DROP;
306 key0.addr = key->l_addr;
307 key0.port = key->l_port;
308 key1.protocol = key0.protocol = proto;
309 key0.fib_index = rx_fib_index;
310 key1.fib_index = sm->outside_fib_index;
311 /* First try to match static mapping by local address and port */
312 if (snat_static_mapping_match
313 (sm, key0, &key1, 0, 0, 0, &lb, 0, &identity_nat))
315 /* Try to create dynamic translation */
316 if (snat_alloc_outside_address_and_port (sm->addresses, rx_fib_index,
319 tsm->snat_thread_index))
321 nat_elog_notice ("addresses exhausted");
322 b->error = node->errors[NAT_IN2OUT_ED_ERROR_OUT_OF_PORTS];
323 return NAT_IN2OUT_ED_NEXT_DROP;
328 if (PREDICT_FALSE (identity_nat))
337 if (proto == SNAT_PROTOCOL_TCP)
339 if (!tcp_is_init (tcp))
341 b->error = node->errors[NAT_IN2OUT_ED_ERROR_NON_SYN];
342 return NAT_IN2OUT_ED_NEXT_DROP;
346 u = nat_user_get_or_create (sm, &key->l_addr, rx_fib_index, thread_index);
349 nat_elog_warn ("create NAT user failed");
351 snat_free_outside_address_and_port (sm->addresses,
352 thread_index, &key1);
353 return NAT_IN2OUT_ED_NEXT_DROP;
356 s = nat_ed_session_alloc (sm, u, thread_index, now);
359 nat44_delete_user_with_no_session (sm, u, thread_index);
360 nat_elog_warn ("create NAT session failed");
362 snat_free_outside_address_and_port (sm->addresses,
363 thread_index, &key1);
364 return NAT_IN2OUT_ED_NEXT_DROP;
367 user_session_increment (sm, u, is_sm);
369 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
371 s->flags |= SNAT_SESSION_FLAG_LOAD_BALANCING;
372 s->flags |= SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT;
373 s->ext_host_addr = key->r_addr;
374 s->ext_host_port = key->r_port;
377 s->out2in.protocol = key0.protocol;
379 switch (vec_len (sm->outside_fibs))
382 s->out2in.fib_index = sm->outside_fib_index;
385 s->out2in.fib_index = sm->outside_fibs[0].fib_index;
389 vec_foreach (outside_fib, sm->outside_fibs)
391 fei = fib_table_lookup (outside_fib->fib_index, &pfx);
392 if (FIB_NODE_INDEX_INVALID != fei)
394 if (fib_entry_get_resolving_interface (fei) != ~0)
396 s->out2in.fib_index = outside_fib->fib_index;
405 /* Add to lookup tables */
406 kv->value = s - tsm->sessions;
408 ctx.thread_index = thread_index;
409 if (clib_bihash_add_or_overwrite_stale_16_8 (&tsm->in2out_ed, kv,
410 nat44_i2o_ed_is_idle_session_cb,
412 nat_elog_notice ("in2out-ed key add failed");
414 make_ed_kv (kv, &key1.addr, &key->r_addr, key->proto, s->out2in.fib_index,
415 key1.port, key->r_port);
416 kv->value = s - tsm->sessions;
417 if (clib_bihash_add_or_overwrite_stale_16_8 (&tsm->out2in_ed, kv,
418 nat44_o2i_ed_is_idle_session_cb,
420 nat_elog_notice ("out2in-ed key add failed");
425 snat_ipfix_logging_nat44_ses_create (thread_index,
426 s->in2out.addr.as_u32,
427 s->out2in.addr.as_u32,
430 s->out2in.port, s->in2out.fib_index);
432 nat_syslog_nat44_sadd (s->user_index, s->in2out.fib_index,
433 &s->in2out.addr, s->in2out.port,
434 &s->ext_host_nat_addr, s->ext_host_nat_port,
435 &s->out2in.addr, s->out2in.port,
436 &s->ext_host_addr, s->ext_host_port,
437 s->in2out.protocol, 0);
439 nat_ha_sadd (&s->in2out.addr, s->in2out.port, &s->out2in.addr,
440 s->out2in.port, &s->ext_host_addr, s->ext_host_port,
441 &s->ext_host_nat_addr, s->ext_host_nat_port,
442 s->in2out.protocol, s->in2out.fib_index, s->flags,
448 static_always_inline int
449 nat44_ed_not_translate (snat_main_t * sm, vlib_node_runtime_t * node,
450 u32 sw_if_index, ip4_header_t * ip, u32 proto,
451 u32 rx_fib_index, u32 thread_index)
453 udp_header_t *udp = ip4_next_header (ip);
454 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
455 clib_bihash_kv_16_8_t kv, value;
456 snat_session_key_t key0, key1;
458 make_ed_kv (&kv, &ip->dst_address, &ip->src_address, ip->protocol,
459 sm->outside_fib_index, udp->dst_port, udp->src_port);
461 /* NAT packet aimed at external address if */
462 /* has active sessions */
463 if (clib_bihash_search_16_8 (&tsm->out2in_ed, &kv, &value))
465 key0.addr = ip->dst_address;
466 key0.port = udp->dst_port;
467 key0.protocol = proto;
468 key0.fib_index = sm->outside_fib_index;
469 /* or is static mappings */
470 if (!snat_static_mapping_match (sm, key0, &key1, 1, 0, 0, 0, 0, 0))
476 if (sm->forwarding_enabled)
479 return snat_not_translate_fast (sm, node, sw_if_index, ip, proto,
483 static_always_inline int
484 nat_not_translate_output_feature_fwd (snat_main_t * sm, ip4_header_t * ip,
485 u32 thread_index, f64 now,
486 vlib_main_t * vm, vlib_buffer_t * b)
488 nat_ed_ses_key_t key;
489 clib_bihash_kv_16_8_t kv, value;
491 snat_session_t *s = 0;
492 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
494 if (!sm->forwarding_enabled)
497 if (ip->protocol == IP_PROTOCOL_ICMP)
499 key.as_u64[0] = key.as_u64[1] = 0;
500 if (icmp_get_ed_key (ip, &key))
503 kv.key[0] = key.as_u64[0];
504 kv.key[1] = key.as_u64[1];
506 else if (ip->protocol == IP_PROTOCOL_UDP || ip->protocol == IP_PROTOCOL_TCP)
508 udp = ip4_next_header (ip);
509 make_ed_kv (&kv, &ip->src_address, &ip->dst_address, ip->protocol, 0,
510 udp->src_port, udp->dst_port);
514 make_ed_kv (&kv, &ip->src_address, &ip->dst_address, ip->protocol, 0, 0,
518 if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
520 s = pool_elt_at_index (tsm->sessions, value.value);
521 if (is_fwd_bypass_session (s))
523 if (ip->protocol == IP_PROTOCOL_TCP)
525 tcp_header_t *tcp = ip4_next_header (ip);
526 if (nat44_set_tcp_session_state_i2o (sm, s, tcp, thread_index))
530 nat44_session_update_counters (s, now,
531 vlib_buffer_length_in_chain (vm, b),
533 /* Per-user LRU list maintenance */
534 nat44_session_update_lru (sm, s, thread_index);
544 static_always_inline int
545 nat44_ed_not_translate_output_feature (snat_main_t * sm, ip4_header_t * ip,
546 u8 proto, u16 src_port, u16 dst_port,
547 u32 thread_index, u32 rx_sw_if_index,
550 clib_bihash_kv_16_8_t kv, value;
551 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
554 u32 rx_fib_index = ip4_fib_table_get_index_for_sw_if_index (rx_sw_if_index);
555 u32 tx_fib_index = ip4_fib_table_get_index_for_sw_if_index (tx_sw_if_index);
558 make_ed_kv (&kv, &ip->src_address, &ip->dst_address, proto, tx_fib_index,
560 if (!clib_bihash_search_16_8 (&tsm->out2in_ed, &kv, &value))
562 s = pool_elt_at_index (tsm->sessions, value.value);
563 if (nat44_is_ses_closed (s))
565 nat_free_session_data (sm, s, thread_index, 0);
566 nat44_delete_session (sm, s, thread_index);
569 s->flags |= SNAT_SESSION_FLAG_OUTPUT_FEATURE;
574 make_ed_kv (&kv, &ip->dst_address, &ip->src_address, proto, rx_fib_index,
576 if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
578 s = pool_elt_at_index (tsm->sessions, value.value);
579 if (is_fwd_bypass_session (s))
584 pool_foreach (i, sm->output_feature_interfaces,
586 if ((nat_interface_is_inside (i)) && (rx_sw_if_index == i->sw_if_index))
596 #ifndef CLIB_MARCH_VARIANT
598 icmp_match_in2out_ed (snat_main_t * sm, vlib_node_runtime_t * node,
599 u32 thread_index, vlib_buffer_t * b, ip4_header_t * ip,
600 u8 * p_proto, snat_session_key_t * p_value,
601 u8 * p_dont_translate, void *d, void *e)
603 icmp46_header_t *icmp;
606 nat_ed_ses_key_t key;
607 snat_session_t *s = 0;
608 u8 dont_translate = 0;
609 clib_bihash_kv_16_8_t kv, value;
612 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
614 icmp = (icmp46_header_t *) ip4_next_header (ip);
615 sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
616 rx_fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
618 key.as_u64[0] = key.as_u64[1] = 0;
619 err = icmp_get_ed_key (ip, &key);
622 b->error = node->errors[err];
623 next = NAT_IN2OUT_ED_NEXT_DROP;
626 key.fib_index = rx_fib_index;
628 kv.key[0] = key.as_u64[0];
629 kv.key[1] = key.as_u64[1];
631 if (clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
633 if (vnet_buffer (b)->sw_if_index[VLIB_TX] != ~0)
635 if (PREDICT_FALSE (nat44_ed_not_translate_output_feature (sm, ip,
654 if (PREDICT_FALSE (nat44_ed_not_translate (sm, node, sw_if_index,
655 ip, SNAT_PROTOCOL_ICMP,
664 if (PREDICT_FALSE (icmp_is_error_message (icmp)))
666 b->error = node->errors[NAT_IN2OUT_ED_ERROR_BAD_ICMP_TYPE];
667 next = NAT_IN2OUT_ED_NEXT_DROP;
671 next = slow_path_ed (sm, b, rx_fib_index, &kv, &s, node, next,
672 thread_index, vlib_time_now (sm->vlib_main), 0);
674 if (PREDICT_FALSE (next == NAT_IN2OUT_ED_NEXT_DROP))
685 if (PREDICT_FALSE (icmp->type != ICMP4_echo_request &&
686 icmp->type != ICMP4_echo_reply &&
687 !icmp_is_error_message (icmp)))
689 b->error = node->errors[NAT_IN2OUT_ED_ERROR_BAD_ICMP_TYPE];
690 next = NAT_IN2OUT_ED_NEXT_DROP;
694 s = pool_elt_at_index (tsm->sessions, value.value);
697 *p_proto = ip_proto_to_snat_proto (key.proto);
700 *p_value = s->out2in;
701 *p_dont_translate = dont_translate;
703 *(snat_session_t **) d = s;
708 static snat_session_t *
709 nat44_ed_in2out_unknown_proto (snat_main_t * sm,
715 vlib_main_t * vm, vlib_node_runtime_t * node)
717 clib_bihash_kv_8_8_t kv, value;
718 clib_bihash_kv_16_8_t s_kv, s_value;
719 snat_static_mapping_t *m;
720 u32 old_addr, new_addr = 0;
723 dlist_elt_t *head, *elt;
724 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
725 u32 elt_index, head_index, ses_index;
727 u32 outside_fib_index = sm->outside_fib_index;
730 nat_outside_fib_t *outside_fib;
731 fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
733 .fp_proto = FIB_PROTOCOL_IP4,
736 .ip4.as_u32 = ip->dst_address.as_u32,
740 switch (vec_len (sm->outside_fibs))
743 outside_fib_index = sm->outside_fib_index;
746 outside_fib_index = sm->outside_fibs[0].fib_index;
750 vec_foreach (outside_fib, sm->outside_fibs)
752 fei = fib_table_lookup (outside_fib->fib_index, &pfx);
753 if (FIB_NODE_INDEX_INVALID != fei)
755 if (fib_entry_get_resolving_interface (fei) != ~0)
757 outside_fib_index = outside_fib->fib_index;
765 old_addr = ip->src_address.as_u32;
767 make_ed_kv (&s_kv, &ip->src_address, &ip->dst_address, ip->protocol,
770 if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &s_kv, &s_value))
772 s = pool_elt_at_index (tsm->sessions, s_value.value);
773 new_addr = ip->src_address.as_u32 = s->out2in.addr.as_u32;
777 if (PREDICT_FALSE (maximum_sessions_exceeded (sm, thread_index)))
779 b->error = node->errors[NAT_IN2OUT_ED_ERROR_MAX_SESSIONS_EXCEEDED];
780 nat_ipfix_logging_max_sessions (thread_index, sm->max_translations);
781 nat_elog_notice ("maximum sessions exceeded");
785 u = nat_user_get_or_create (sm, &ip->src_address, rx_fib_index,
789 nat_elog_warn ("create NAT user failed");
793 make_sm_kv (&kv, &ip->src_address, 0, rx_fib_index, 0);
795 /* Try to find static mapping first */
796 if (!clib_bihash_search_8_8 (&sm->static_mapping_by_local, &kv, &value))
798 m = pool_elt_at_index (sm->static_mappings, value.value);
799 new_addr = ip->src_address.as_u32 = m->external_addr.as_u32;
803 /* Fallback to 3-tuple key */
806 /* Choose same out address as for TCP/UDP session to same destination */
807 head_index = u->sessions_per_user_list_head_index;
808 head = pool_elt_at_index (tsm->list_pool, head_index);
809 elt_index = head->next;
810 if (PREDICT_FALSE (elt_index == ~0))
814 elt = pool_elt_at_index (tsm->list_pool, elt_index);
815 ses_index = elt->value;
818 while (ses_index != ~0)
820 s = pool_elt_at_index (tsm->sessions, ses_index);
821 elt_index = elt->next;
822 elt = pool_elt_at_index (tsm->list_pool, elt_index);
823 ses_index = elt->value;
825 if (s->ext_host_addr.as_u32 == ip->dst_address.as_u32)
827 new_addr = ip->src_address.as_u32 = s->out2in.addr.as_u32;
829 make_ed_kv (&s_kv, &s->out2in.addr, &ip->dst_address,
830 ip->protocol, outside_fib_index, 0, 0);
831 if (clib_bihash_search_16_8
832 (&tsm->out2in_ed, &s_kv, &s_value))
839 for (i = 0; i < vec_len (sm->addresses); i++)
841 make_ed_kv (&s_kv, &sm->addresses[i].addr, &ip->dst_address,
842 ip->protocol, outside_fib_index, 0, 0);
843 if (clib_bihash_search_16_8 (&tsm->out2in_ed, &s_kv, &s_value))
845 new_addr = ip->src_address.as_u32 =
846 sm->addresses[i].addr.as_u32;
854 s = nat_ed_session_alloc (sm, u, thread_index, now);
857 nat44_delete_user_with_no_session (sm, u, thread_index);
858 nat_elog_warn ("create NAT session failed");
862 s->ext_host_addr.as_u32 = ip->dst_address.as_u32;
863 s->flags |= SNAT_SESSION_FLAG_UNKNOWN_PROTO;
864 s->flags |= SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT;
865 s->out2in.addr.as_u32 = new_addr;
866 s->out2in.fib_index = outside_fib_index;
867 s->in2out.addr.as_u32 = old_addr;
868 s->in2out.fib_index = rx_fib_index;
869 s->in2out.port = s->out2in.port = ip->protocol;
871 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
872 user_session_increment (sm, u, is_sm);
874 /* Add to lookup tables */
875 make_ed_kv (&s_kv, &s->in2out.addr, &ip->dst_address, ip->protocol,
877 s_kv.value = s - tsm->sessions;
878 if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &s_kv, 1))
879 nat_elog_notice ("in2out key add failed");
881 make_ed_kv (&s_kv, &s->out2in.addr, &ip->dst_address, ip->protocol,
882 outside_fib_index, 0, 0);
883 s_kv.value = s - tsm->sessions;
884 if (clib_bihash_add_del_16_8 (&tsm->out2in_ed, &s_kv, 1))
885 nat_elog_notice ("out2in key add failed");
888 /* Update IP checksum */
890 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, src_address);
891 ip->checksum = ip_csum_fold (sum);
894 nat44_session_update_counters (s, now, vlib_buffer_length_in_chain (vm, b),
896 /* Per-user LRU list maintenance */
897 nat44_session_update_lru (sm, s, thread_index);
900 if (vnet_buffer (b)->sw_if_index[VLIB_TX] == ~0)
901 nat44_ed_hairpinning_unknown_proto (sm, b, ip);
903 if (vnet_buffer (b)->sw_if_index[VLIB_TX] == ~0)
904 vnet_buffer (b)->sw_if_index[VLIB_TX] = outside_fib_index;
910 nat44_ed_in2out_node_fn_inline (vlib_main_t * vm,
911 vlib_node_runtime_t * node,
912 vlib_frame_t * frame, int is_slow_path,
913 int is_output_feature)
915 u32 n_left_from, *from, *to_next, pkts_processed = 0, stats_node_index;
916 nat_in2out_ed_next_t next_index;
917 snat_main_t *sm = &snat_main;
918 f64 now = vlib_time_now (vm);
919 u32 thread_index = vm->thread_index;
920 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
921 u32 tcp_packets = 0, udp_packets = 0, icmp_packets = 0, other_packets =
924 stats_node_index = is_slow_path ? sm->ed_in2out_slowpath_node_index :
925 sm->ed_in2out_node_index;
927 from = vlib_frame_vector_args (frame);
928 n_left_from = frame->n_vectors;
929 next_index = node->cached_next_index;
931 while (n_left_from > 0)
935 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
937 while (n_left_from >= 4 && n_left_to_next >= 2)
940 vlib_buffer_t *b0, *b1;
941 u32 next0, sw_if_index0, rx_fib_index0, iph_offset0 = 0, proto0,
942 new_addr0, old_addr0;
943 u32 next1, sw_if_index1, rx_fib_index1, iph_offset1 = 0, proto1,
944 new_addr1, old_addr1;
945 u16 old_port0, new_port0, old_port1, new_port1;
946 ip4_header_t *ip0, *ip1;
947 udp_header_t *udp0, *udp1;
948 tcp_header_t *tcp0, *tcp1;
949 icmp46_header_t *icmp0, *icmp1;
950 snat_session_t *s0 = 0, *s1 = 0;
951 clib_bihash_kv_16_8_t kv0, value0, kv1, value1;
952 ip_csum_t sum0, sum1;
954 /* Prefetch next iteration. */
956 vlib_buffer_t *p2, *p3;
958 p2 = vlib_get_buffer (vm, from[2]);
959 p3 = vlib_get_buffer (vm, from[3]);
961 vlib_prefetch_buffer_header (p2, LOAD);
962 vlib_prefetch_buffer_header (p3, LOAD);
964 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
965 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
968 /* speculatively enqueue b0 and b1 to the current next frame */
969 to_next[0] = bi0 = from[0];
970 to_next[1] = bi1 = from[1];
976 b0 = vlib_get_buffer (vm, bi0);
977 b1 = vlib_get_buffer (vm, bi1);
979 next0 = NAT_IN2OUT_ED_NEXT_LOOKUP;
981 if (is_output_feature)
982 iph_offset0 = vnet_buffer (b0)->ip.save_rewrite_length;
984 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
987 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
989 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
992 if (PREDICT_FALSE (ip0->ttl == 1))
994 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
995 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
996 ICMP4_time_exceeded_ttl_exceeded_in_transit,
998 next0 = NAT_IN2OUT_ED_NEXT_ICMP_ERROR;
1002 udp0 = ip4_next_header (ip0);
1003 tcp0 = (tcp_header_t *) udp0;
1004 icmp0 = (icmp46_header_t *) udp0;
1005 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1009 if (PREDICT_FALSE (proto0 == ~0))
1011 s0 = nat44_ed_in2out_unknown_proto (sm, b0, ip0,
1013 thread_index, now, vm,
1016 next0 = NAT_IN2OUT_ED_NEXT_DROP;
1021 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1023 next0 = icmp_in2out_ed_slow_path
1024 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1025 next0, now, thread_index, &s0);
1032 if (PREDICT_FALSE (proto0 == ~0))
1034 next0 = NAT_IN2OUT_ED_NEXT_SLOW_PATH;
1038 if (ip4_is_fragment (ip0))
1040 next0 = NAT_IN2OUT_ED_NEXT_REASS;
1045 if (is_output_feature)
1048 (nat_not_translate_output_feature_fwd
1049 (sm, ip0, thread_index, now, vm, b0)))
1053 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1055 next0 = NAT_IN2OUT_ED_NEXT_SLOW_PATH;
1060 make_ed_kv (&kv0, &ip0->src_address, &ip0->dst_address,
1061 ip0->protocol, rx_fib_index0, udp0->src_port,
1064 if (clib_bihash_search_16_8 (&tsm->in2out_ed, &kv0, &value0))
1068 if (is_output_feature)
1071 (nat44_ed_not_translate_output_feature
1072 (sm, ip0, ip0->protocol, udp0->src_port,
1073 udp0->dst_port, thread_index, sw_if_index0,
1074 vnet_buffer (b0)->sw_if_index[VLIB_TX])))
1079 if (PREDICT_FALSE (nat44_ed_not_translate (sm, node,
1088 slow_path_ed (sm, b0, rx_fib_index0, &kv0, &s0, node,
1089 next0, thread_index, now, tcp0);
1091 if (PREDICT_FALSE (next0 == NAT_IN2OUT_ED_NEXT_DROP))
1094 if (PREDICT_FALSE (!s0))
1099 next0 = NAT_IN2OUT_ED_NEXT_SLOW_PATH;
1105 s0 = pool_elt_at_index (tsm->sessions, value0.value);
1108 b0->flags |= VNET_BUFFER_F_IS_NATED;
1110 if (!is_output_feature)
1111 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1113 old_addr0 = ip0->src_address.as_u32;
1114 new_addr0 = ip0->src_address.as_u32 = s0->out2in.addr.as_u32;
1115 sum0 = ip0->checksum;
1116 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1118 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1119 sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
1120 s0->ext_host_addr.as_u32, ip4_header_t,
1122 ip0->checksum = ip_csum_fold (sum0);
1124 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1126 old_port0 = tcp0->src_port;
1127 new_port0 = tcp0->src_port = s0->out2in.port;
1129 sum0 = tcp0->checksum;
1130 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1132 sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
1134 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1136 sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
1137 s0->ext_host_addr.as_u32,
1138 ip4_header_t, dst_address);
1139 sum0 = ip_csum_update (sum0, tcp0->dst_port,
1140 s0->ext_host_port, ip4_header_t,
1142 tcp0->dst_port = s0->ext_host_port;
1143 ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
1145 mss_clamping (sm, tcp0, &sum0);
1146 tcp0->checksum = ip_csum_fold (sum0);
1148 if (nat44_set_tcp_session_state_i2o
1149 (sm, s0, tcp0, thread_index))
1154 udp0->src_port = s0->out2in.port;
1156 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1158 udp0->dst_port = s0->ext_host_port;
1159 ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
1165 nat44_session_update_counters (s0, now,
1166 vlib_buffer_length_in_chain (vm,
1169 /* Per-user LRU list maintenance */
1170 nat44_session_update_lru (sm, s0, thread_index);
1173 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1174 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1176 nat_in2out_ed_trace_t *t =
1177 vlib_add_trace (vm, node, b0, sizeof (*t));
1178 t->is_slow_path = is_slow_path;
1179 t->sw_if_index = sw_if_index0;
1180 t->next_index = next0;
1181 t->session_index = ~0;
1183 t->session_index = s0 - tsm->sessions;
1186 pkts_processed += next0 == NAT_IN2OUT_ED_NEXT_LOOKUP;
1189 next1 = NAT_IN2OUT_ED_NEXT_LOOKUP;
1191 if (is_output_feature)
1192 iph_offset1 = vnet_buffer (b1)->ip.save_rewrite_length;
1194 ip1 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b1) +
1197 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
1199 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1202 if (PREDICT_FALSE (ip1->ttl == 1))
1204 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1205 icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
1206 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1208 next1 = NAT_IN2OUT_ED_NEXT_ICMP_ERROR;
1212 udp1 = ip4_next_header (ip1);
1213 tcp1 = (tcp_header_t *) udp1;
1214 icmp1 = (icmp46_header_t *) udp1;
1215 proto1 = ip_proto_to_snat_proto (ip1->protocol);
1219 if (PREDICT_FALSE (proto1 == ~0))
1221 s1 = nat44_ed_in2out_unknown_proto (sm, b1, ip1,
1223 thread_index, now, vm,
1226 next1 = NAT_IN2OUT_ED_NEXT_DROP;
1231 if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
1233 next1 = icmp_in2out_ed_slow_path
1234 (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node,
1235 next1, now, thread_index, &s1);
1242 if (PREDICT_FALSE (proto1 == ~0))
1244 next1 = NAT_IN2OUT_ED_NEXT_SLOW_PATH;
1248 if (ip4_is_fragment (ip1))
1250 next1 = NAT_IN2OUT_ED_NEXT_REASS;
1255 if (is_output_feature)
1258 (nat_not_translate_output_feature_fwd
1259 (sm, ip1, thread_index, now, vm, b1)))
1263 if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
1265 next1 = NAT_IN2OUT_ED_NEXT_SLOW_PATH;
1270 make_ed_kv (&kv1, &ip1->src_address, &ip1->dst_address,
1271 ip1->protocol, rx_fib_index1, udp1->src_port,
1274 if (clib_bihash_search_16_8 (&tsm->in2out_ed, &kv1, &value1))
1278 if (is_output_feature)
1281 (nat44_ed_not_translate_output_feature
1282 (sm, ip1, ip1->protocol, udp1->src_port,
1283 udp1->dst_port, thread_index, sw_if_index1,
1284 vnet_buffer (b1)->sw_if_index[VLIB_TX])))
1289 if (PREDICT_FALSE (nat44_ed_not_translate (sm, node,
1298 slow_path_ed (sm, b1, rx_fib_index1, &kv1, &s1, node,
1299 next1, thread_index, now, tcp1);
1301 if (PREDICT_FALSE (next1 == NAT_IN2OUT_ED_NEXT_DROP))
1304 if (PREDICT_FALSE (!s1))
1309 next1 = NAT_IN2OUT_ED_NEXT_SLOW_PATH;
1315 s1 = pool_elt_at_index (tsm->sessions, value1.value);
1318 b1->flags |= VNET_BUFFER_F_IS_NATED;
1320 if (!is_output_feature)
1321 vnet_buffer (b1)->sw_if_index[VLIB_TX] = s1->out2in.fib_index;
1323 old_addr1 = ip1->src_address.as_u32;
1324 new_addr1 = ip1->src_address.as_u32 = s1->out2in.addr.as_u32;
1325 sum1 = ip1->checksum;
1326 sum1 = ip_csum_update (sum1, old_addr1, new_addr1, ip4_header_t,
1328 if (PREDICT_FALSE (is_twice_nat_session (s1)))
1329 sum1 = ip_csum_update (sum1, ip1->dst_address.as_u32,
1330 s1->ext_host_addr.as_u32, ip4_header_t,
1332 ip1->checksum = ip_csum_fold (sum1);
1334 if (PREDICT_TRUE (proto1 == SNAT_PROTOCOL_TCP))
1336 old_port1 = tcp1->src_port;
1337 new_port1 = tcp1->src_port = s1->out2in.port;
1339 sum1 = tcp1->checksum;
1340 sum1 = ip_csum_update (sum1, old_addr1, new_addr1, ip4_header_t,
1342 sum1 = ip_csum_update (sum1, old_port1, new_port1, ip4_header_t,
1344 if (PREDICT_FALSE (is_twice_nat_session (s1)))
1346 sum1 = ip_csum_update (sum1, ip1->dst_address.as_u32,
1347 s1->ext_host_addr.as_u32,
1348 ip4_header_t, dst_address);
1349 sum1 = ip_csum_update (sum1, tcp1->dst_port,
1350 s1->ext_host_port, ip4_header_t,
1352 tcp1->dst_port = s1->ext_host_port;
1353 ip1->dst_address.as_u32 = s1->ext_host_addr.as_u32;
1355 tcp1->checksum = ip_csum_fold (sum1);
1356 mss_clamping (sm, tcp1, &sum1);
1358 if (nat44_set_tcp_session_state_i2o
1359 (sm, s1, tcp1, thread_index))
1364 udp1->src_port = s1->out2in.port;
1366 if (PREDICT_FALSE (is_twice_nat_session (s1)))
1368 udp1->dst_port = s1->ext_host_port;
1369 ip1->dst_address.as_u32 = s1->ext_host_addr.as_u32;
1375 nat44_session_update_counters (s1, now,
1376 vlib_buffer_length_in_chain (vm, b1),
1378 /* Per-user LRU list maintenance */
1379 nat44_session_update_lru (sm, s1, thread_index);
1382 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1383 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
1385 nat_in2out_ed_trace_t *t =
1386 vlib_add_trace (vm, node, b1, sizeof (*t));
1387 t->is_slow_path = is_slow_path;
1388 t->sw_if_index = sw_if_index1;
1389 t->next_index = next1;
1390 t->session_index = ~0;
1392 t->session_index = s1 - tsm->sessions;
1395 pkts_processed += next1 == NAT_IN2OUT_ED_NEXT_LOOKUP;
1397 /* verify speculative enqueues, maybe switch current next frame */
1398 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1399 to_next, n_left_to_next,
1400 bi0, bi1, next0, next1);
1403 while (n_left_from > 0 && n_left_to_next > 0)
1407 u32 next0, sw_if_index0, rx_fib_index0, iph_offset0 = 0, proto0,
1408 new_addr0, old_addr0;
1409 u16 old_port0, new_port0;
1413 icmp46_header_t *icmp0;
1414 snat_session_t *s0 = 0;
1415 clib_bihash_kv_16_8_t kv0, value0;
1418 /* speculatively enqueue b0 to the current next frame */
1424 n_left_to_next -= 1;
1426 b0 = vlib_get_buffer (vm, bi0);
1427 next0 = NAT_IN2OUT_ED_NEXT_LOOKUP;
1429 if (is_output_feature)
1430 iph_offset0 = vnet_buffer (b0)->ip.save_rewrite_length;
1432 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
1435 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1437 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1440 if (PREDICT_FALSE (ip0->ttl == 1))
1442 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1443 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1444 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1446 next0 = NAT_IN2OUT_ED_NEXT_ICMP_ERROR;
1450 udp0 = ip4_next_header (ip0);
1451 tcp0 = (tcp_header_t *) udp0;
1452 icmp0 = (icmp46_header_t *) udp0;
1453 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1457 if (PREDICT_FALSE (proto0 == ~0))
1459 s0 = nat44_ed_in2out_unknown_proto (sm, b0, ip0,
1461 thread_index, now, vm,
1464 next0 = NAT_IN2OUT_ED_NEXT_DROP;
1469 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1471 next0 = icmp_in2out_ed_slow_path
1472 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1473 next0, now, thread_index, &s0);
1480 if (PREDICT_FALSE (proto0 == ~0))
1482 next0 = NAT_IN2OUT_ED_NEXT_SLOW_PATH;
1486 if (ip4_is_fragment (ip0))
1488 next0 = NAT_IN2OUT_ED_NEXT_REASS;
1493 if (is_output_feature)
1496 (nat_not_translate_output_feature_fwd
1497 (sm, ip0, thread_index, now, vm, b0)))
1501 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1503 next0 = NAT_IN2OUT_ED_NEXT_SLOW_PATH;
1508 make_ed_kv (&kv0, &ip0->src_address, &ip0->dst_address,
1509 ip0->protocol, rx_fib_index0, udp0->src_port,
1512 if (clib_bihash_search_16_8 (&tsm->in2out_ed, &kv0, &value0))
1516 if (is_output_feature)
1519 (nat44_ed_not_translate_output_feature
1520 (sm, ip0, ip0->protocol, udp0->src_port,
1521 udp0->dst_port, thread_index, sw_if_index0,
1522 vnet_buffer (b0)->sw_if_index[VLIB_TX])))
1527 if (PREDICT_FALSE (nat44_ed_not_translate (sm, node,
1536 slow_path_ed (sm, b0, rx_fib_index0, &kv0, &s0, node,
1537 next0, thread_index, now, tcp0);
1539 if (PREDICT_FALSE (next0 == NAT_IN2OUT_ED_NEXT_DROP))
1542 if (PREDICT_FALSE (!s0))
1547 next0 = NAT_IN2OUT_ED_NEXT_SLOW_PATH;
1553 s0 = pool_elt_at_index (tsm->sessions, value0.value);
1556 b0->flags |= VNET_BUFFER_F_IS_NATED;
1558 if (!is_output_feature)
1559 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1561 old_addr0 = ip0->src_address.as_u32;
1562 new_addr0 = ip0->src_address.as_u32 = s0->out2in.addr.as_u32;
1563 sum0 = ip0->checksum;
1564 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1566 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1567 sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
1568 s0->ext_host_addr.as_u32, ip4_header_t,
1570 ip0->checksum = ip_csum_fold (sum0);
1572 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1574 old_port0 = tcp0->src_port;
1575 new_port0 = tcp0->src_port = s0->out2in.port;
1577 sum0 = tcp0->checksum;
1578 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1580 sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
1582 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1584 sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
1585 s0->ext_host_addr.as_u32,
1586 ip4_header_t, dst_address);
1587 sum0 = ip_csum_update (sum0, tcp0->dst_port,
1588 s0->ext_host_port, ip4_header_t,
1590 tcp0->dst_port = s0->ext_host_port;
1591 ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
1593 mss_clamping (sm, tcp0, &sum0);
1594 tcp0->checksum = ip_csum_fold (sum0);
1596 if (nat44_set_tcp_session_state_i2o
1597 (sm, s0, tcp0, thread_index))
1602 udp0->src_port = s0->out2in.port;
1604 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1606 udp0->dst_port = s0->ext_host_port;
1607 ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
1613 nat44_session_update_counters (s0, now,
1614 vlib_buffer_length_in_chain (vm, b0),
1616 /* Per-user LRU list maintenance */
1617 nat44_session_update_lru (sm, s0, thread_index);
1620 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1621 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1623 nat_in2out_ed_trace_t *t =
1624 vlib_add_trace (vm, node, b0, sizeof (*t));
1625 t->is_slow_path = is_slow_path;
1626 t->sw_if_index = sw_if_index0;
1627 t->next_index = next0;
1628 t->session_index = ~0;
1630 t->session_index = s0 - tsm->sessions;
1633 pkts_processed += next0 == NAT_IN2OUT_ED_NEXT_LOOKUP;
1635 /* verify speculative enqueue, maybe switch current next frame */
1636 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1637 to_next, n_left_to_next,
1641 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1644 vlib_node_increment_counter (vm, stats_node_index,
1645 NAT_IN2OUT_ED_ERROR_IN2OUT_PACKETS,
1647 vlib_node_increment_counter (vm, stats_node_index,
1648 NAT_IN2OUT_ED_ERROR_TCP_PACKETS, tcp_packets);
1649 vlib_node_increment_counter (vm, stats_node_index,
1650 NAT_IN2OUT_ED_ERROR_UDP_PACKETS, tcp_packets);
1651 vlib_node_increment_counter (vm, stats_node_index,
1652 NAT_IN2OUT_ED_ERROR_ICMP_PACKETS,
1654 vlib_node_increment_counter (vm, stats_node_index,
1655 NAT_IN2OUT_ED_ERROR_OTHER_PACKETS,
1657 vlib_node_increment_counter (vm, stats_node_index,
1658 NAT_IN2OUT_ED_ERROR_FRAGMENTS, fragments);
1660 return frame->n_vectors;
1663 VLIB_NODE_FN (nat44_ed_in2out_node) (vlib_main_t * vm,
1664 vlib_node_runtime_t * node,
1665 vlib_frame_t * frame)
1667 return nat44_ed_in2out_node_fn_inline (vm, node, frame, 0, 0);
1671 VLIB_REGISTER_NODE (nat44_ed_in2out_node) = {
1672 .name = "nat44-ed-in2out",
1673 .vector_size = sizeof (u32),
1674 .format_trace = format_nat_in2out_ed_trace,
1675 .type = VLIB_NODE_TYPE_INTERNAL,
1676 .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
1677 .error_strings = nat_in2out_ed_error_strings,
1678 .runtime_data_bytes = sizeof (snat_runtime_t),
1679 .n_next_nodes = NAT_IN2OUT_ED_N_NEXT,
1681 [NAT_IN2OUT_ED_NEXT_DROP] = "error-drop",
1682 [NAT_IN2OUT_ED_NEXT_LOOKUP] = "ip4-lookup",
1683 [NAT_IN2OUT_ED_NEXT_SLOW_PATH] = "nat44-ed-in2out-slowpath",
1684 [NAT_IN2OUT_ED_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1685 [NAT_IN2OUT_ED_NEXT_REASS] = "nat44-ed-in2out-reass",
1690 VLIB_NODE_FN (nat44_ed_in2out_output_node) (vlib_main_t * vm,
1691 vlib_node_runtime_t * node,
1692 vlib_frame_t * frame)
1694 return nat44_ed_in2out_node_fn_inline (vm, node, frame, 0, 1);
1698 VLIB_REGISTER_NODE (nat44_ed_in2out_output_node) = {
1699 .name = "nat44-ed-in2out-output",
1700 .vector_size = sizeof (u32),
1701 .format_trace = format_nat_in2out_ed_trace,
1702 .type = VLIB_NODE_TYPE_INTERNAL,
1703 .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
1704 .error_strings = nat_in2out_ed_error_strings,
1705 .runtime_data_bytes = sizeof (snat_runtime_t),
1706 .n_next_nodes = NAT_IN2OUT_ED_N_NEXT,
1708 [NAT_IN2OUT_ED_NEXT_DROP] = "error-drop",
1709 [NAT_IN2OUT_ED_NEXT_LOOKUP] = "interface-output",
1710 [NAT_IN2OUT_ED_NEXT_SLOW_PATH] = "nat44-ed-in2out-output-slowpath",
1711 [NAT_IN2OUT_ED_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1712 [NAT_IN2OUT_ED_NEXT_REASS] = "nat44-ed-in2out-reass-output",
1717 VLIB_NODE_FN (nat44_ed_in2out_slowpath_node) (vlib_main_t * vm,
1718 vlib_node_runtime_t * node,
1719 vlib_frame_t * frame)
1721 return nat44_ed_in2out_node_fn_inline (vm, node, frame, 1, 0);
1725 VLIB_REGISTER_NODE (nat44_ed_in2out_slowpath_node) = {
1726 .name = "nat44-ed-in2out-slowpath",
1727 .vector_size = sizeof (u32),
1728 .format_trace = format_nat_in2out_ed_trace,
1729 .type = VLIB_NODE_TYPE_INTERNAL,
1730 .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
1731 .error_strings = nat_in2out_ed_error_strings,
1732 .runtime_data_bytes = sizeof (snat_runtime_t),
1733 .n_next_nodes = NAT_IN2OUT_ED_N_NEXT,
1735 [NAT_IN2OUT_ED_NEXT_DROP] = "error-drop",
1736 [NAT_IN2OUT_ED_NEXT_LOOKUP] = "ip4-lookup",
1737 [NAT_IN2OUT_ED_NEXT_SLOW_PATH] = "nat44-ed-in2out-slowpath",
1738 [NAT_IN2OUT_ED_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1739 [NAT_IN2OUT_ED_NEXT_REASS] = "nat44-ed-in2out-reass",
1744 VLIB_NODE_FN (nat44_ed_in2out_output_slowpath_node) (vlib_main_t * vm,
1745 vlib_node_runtime_t *
1747 vlib_frame_t * frame)
1749 return nat44_ed_in2out_node_fn_inline (vm, node, frame, 1, 1);
1753 VLIB_REGISTER_NODE (nat44_ed_in2out_output_slowpath_node) = {
1754 .name = "nat44-ed-in2out-output-slowpath",
1755 .vector_size = sizeof (u32),
1756 .format_trace = format_nat_in2out_ed_trace,
1757 .type = VLIB_NODE_TYPE_INTERNAL,
1758 .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
1759 .error_strings = nat_in2out_ed_error_strings,
1760 .runtime_data_bytes = sizeof (snat_runtime_t),
1761 .n_next_nodes = NAT_IN2OUT_ED_N_NEXT,
1763 [NAT_IN2OUT_ED_NEXT_DROP] = "error-drop",
1764 [NAT_IN2OUT_ED_NEXT_LOOKUP] = "interface-output",
1765 [NAT_IN2OUT_ED_NEXT_SLOW_PATH] = "nat44-ed-in2out-output-slowpath",
1766 [NAT_IN2OUT_ED_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1767 [NAT_IN2OUT_ED_NEXT_REASS] = "nat44-ed-in2out-reass",
1773 nat44_ed_in2out_reass_node_fn_inline (vlib_main_t * vm,
1774 vlib_node_runtime_t * node,
1775 vlib_frame_t * frame,
1776 int is_output_feature)
1778 u32 n_left_from, *from, *to_next;
1779 nat_in2out_ed_next_t next_index;
1780 u32 pkts_processed = 0, cached_fragments = 0;
1781 snat_main_t *sm = &snat_main;
1782 f64 now = vlib_time_now (vm);
1783 u32 thread_index = vm->thread_index;
1784 snat_main_per_thread_data_t *per_thread_data =
1785 &sm->per_thread_data[thread_index];
1786 u32 *fragments_to_drop = 0;
1787 u32 *fragments_to_loopback = 0;
1789 from = vlib_frame_vector_args (frame);
1790 n_left_from = frame->n_vectors;
1791 next_index = node->cached_next_index;
1793 while (n_left_from > 0)
1797 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1799 while (n_left_from > 0 && n_left_to_next > 0)
1801 u32 bi0, sw_if_index0, proto0, rx_fib_index0, new_addr0, old_addr0;
1802 u32 iph_offset0 = 0;
1806 ip4_header_t *ip0 = 0;
1807 nat_reass_ip4_t *reass0;
1810 icmp46_header_t *icmp0;
1811 clib_bihash_kv_16_8_t kv0, value0;
1812 snat_session_t *s0 = 0;
1813 u16 old_port0, new_port0;
1816 /* speculatively enqueue b0 to the current next frame */
1822 n_left_to_next -= 1;
1824 b0 = vlib_get_buffer (vm, bi0);
1826 next0 = NAT_IN2OUT_ED_NEXT_LOOKUP;
1828 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1830 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1833 if (PREDICT_FALSE (nat_reass_is_drop_frag (0)))
1835 next0 = NAT_IN2OUT_ED_NEXT_DROP;
1836 b0->error = node->errors[NAT_IN2OUT_ED_ERROR_DROP_FRAGMENT];
1840 if (is_output_feature)
1841 iph_offset0 = vnet_buffer (b0)->ip.save_rewrite_length;
1843 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
1846 udp0 = ip4_next_header (ip0);
1847 tcp0 = (tcp_header_t *) udp0;
1848 icmp0 = (icmp46_header_t *) udp0;
1849 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1851 reass0 = nat_ip4_reass_find_or_create (ip0->src_address,
1855 1, &fragments_to_drop);
1857 if (PREDICT_FALSE (!reass0))
1859 next0 = NAT_IN2OUT_ED_NEXT_DROP;
1860 b0->error = node->errors[NAT_IN2OUT_ED_ERROR_MAX_REASS];
1861 nat_elog_notice ("maximum reassemblies exceeded");
1865 if (PREDICT_FALSE (ip4_is_first_fragment (ip0)))
1867 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1869 if (is_output_feature)
1872 (nat_not_translate_output_feature_fwd
1873 (sm, ip0, thread_index, now, vm, b0)))
1874 reass0->flags |= NAT_REASS_FLAG_ED_DONT_TRANSLATE;
1878 next0 = icmp_in2out_ed_slow_path
1879 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1880 next0, now, thread_index, &s0);
1882 if (PREDICT_TRUE (next0 != NAT_IN2OUT_ED_NEXT_DROP))
1885 reass0->sess_index = s0 - per_thread_data->sessions;
1887 reass0->flags |= NAT_REASS_FLAG_ED_DONT_TRANSLATE;
1888 nat_ip4_reass_get_frags (reass0,
1889 &fragments_to_loopback);
1895 make_ed_kv (&kv0, &ip0->src_address, &ip0->dst_address,
1896 ip0->protocol, rx_fib_index0, udp0->src_port,
1899 if (clib_bihash_search_16_8
1900 (&per_thread_data->in2out_ed, &kv0, &value0))
1902 if (is_output_feature)
1905 (nat44_ed_not_translate_output_feature
1906 (sm, ip0, ip0->protocol, udp0->src_port,
1907 udp0->dst_port, thread_index, sw_if_index0,
1908 vnet_buffer (b0)->sw_if_index[VLIB_TX])))
1910 reass0->flags |= NAT_REASS_FLAG_ED_DONT_TRANSLATE;
1911 nat_ip4_reass_get_frags (reass0,
1912 &fragments_to_loopback);
1918 if (PREDICT_FALSE (nat44_ed_not_translate (sm, node,
1924 reass0->flags |= NAT_REASS_FLAG_ED_DONT_TRANSLATE;
1925 nat_ip4_reass_get_frags (reass0,
1926 &fragments_to_loopback);
1931 next0 = slow_path_ed (sm, b0, rx_fib_index0, &kv0,
1932 &s0, node, next0, thread_index, now,
1935 if (PREDICT_FALSE (next0 == NAT_IN2OUT_ED_NEXT_DROP))
1938 if (PREDICT_FALSE (!s0))
1940 reass0->flags |= NAT_REASS_FLAG_ED_DONT_TRANSLATE;
1944 reass0->sess_index = s0 - per_thread_data->sessions;
1948 s0 = pool_elt_at_index (per_thread_data->sessions,
1950 reass0->sess_index = value0.value;
1952 nat_ip4_reass_get_frags (reass0, &fragments_to_loopback);
1956 if (reass0->flags & NAT_REASS_FLAG_ED_DONT_TRANSLATE)
1958 if (PREDICT_FALSE (reass0->sess_index == (u32) ~ 0))
1960 if (nat_ip4_reass_add_fragment
1961 (thread_index, reass0, bi0, &fragments_to_drop))
1963 b0->error = node->errors[NAT_IN2OUT_ED_ERROR_MAX_FRAG];
1965 ("maximum fragments per reassembly exceeded");
1966 next0 = NAT_IN2OUT_ED_NEXT_DROP;
1972 s0 = pool_elt_at_index (per_thread_data->sessions,
1973 reass0->sess_index);
1976 old_addr0 = ip0->src_address.as_u32;
1977 ip0->src_address = s0->out2in.addr;
1978 new_addr0 = ip0->src_address.as_u32;
1979 if (!is_output_feature)
1980 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1982 sum0 = ip0->checksum;
1983 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1985 src_address /* changed member */ );
1986 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1987 sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
1988 s0->ext_host_addr.as_u32, ip4_header_t,
1990 ip0->checksum = ip_csum_fold (sum0);
1992 if (PREDICT_FALSE (ip4_is_first_fragment (ip0)))
1994 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1996 old_port0 = tcp0->src_port;
1997 tcp0->src_port = s0->out2in.port;
1998 new_port0 = tcp0->src_port;
2000 sum0 = tcp0->checksum;
2001 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
2003 dst_address /* changed member */ );
2004 sum0 = ip_csum_update (sum0, old_port0, new_port0,
2005 ip4_header_t /* cheat */ ,
2006 length /* changed member */ );
2007 if (PREDICT_FALSE (is_twice_nat_session (s0)))
2009 sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
2010 s0->ext_host_addr.as_u32,
2011 ip4_header_t, dst_address);
2012 sum0 = ip_csum_update (sum0, tcp0->dst_port,
2013 s0->ext_host_port, ip4_header_t,
2015 tcp0->dst_port = s0->ext_host_port;
2016 ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
2018 tcp0->checksum = ip_csum_fold (sum0);
2022 old_port0 = udp0->src_port;
2023 udp0->src_port = s0->out2in.port;
2025 if (PREDICT_FALSE (is_twice_nat_session (s0)))
2027 udp0->dst_port = s0->ext_host_port;
2028 ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
2034 nat44_reass_hairpinning (sm, b0, ip0, s0->out2in.port,
2035 s0->ext_host_port, proto0, 1);
2038 nat44_session_update_counters (s0, now,
2039 vlib_buffer_length_in_chain (vm, b0),
2041 /* Per-user LRU list maintenance */
2042 nat44_session_update_lru (sm, s0, thread_index);
2045 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
2046 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
2048 nat44_reass_trace_t *t =
2049 vlib_add_trace (vm, node, b0, sizeof (*t));
2050 t->cached = cached0;
2051 t->sw_if_index = sw_if_index0;
2052 t->next_index = next0;
2063 pkts_processed += next0 != NAT_IN2OUT_ED_NEXT_DROP;
2065 /* verify speculative enqueue, maybe switch current next frame */
2066 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2067 to_next, n_left_to_next,
2071 if (n_left_from == 0 && vec_len (fragments_to_loopback))
2073 from = vlib_frame_vector_args (frame);
2074 u32 len = vec_len (fragments_to_loopback);
2075 if (len <= VLIB_FRAME_SIZE)
2077 clib_memcpy_fast (from, fragments_to_loopback,
2078 sizeof (u32) * len);
2080 vec_reset_length (fragments_to_loopback);
2084 clib_memcpy_fast (from, fragments_to_loopback +
2085 (len - VLIB_FRAME_SIZE),
2086 sizeof (u32) * VLIB_FRAME_SIZE);
2087 n_left_from = VLIB_FRAME_SIZE;
2088 _vec_len (fragments_to_loopback) = len - VLIB_FRAME_SIZE;
2093 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2096 vlib_node_increment_counter (vm, sm->ed_in2out_reass_node_index,
2097 NAT_IN2OUT_ED_ERROR_PROCESSED_FRAGMENTS,
2099 vlib_node_increment_counter (vm, sm->ed_in2out_reass_node_index,
2100 NAT_IN2OUT_ED_ERROR_CACHED_FRAGMENTS,
2103 nat_send_all_to_node (vm, fragments_to_drop, node,
2104 &node->errors[NAT_IN2OUT_ED_ERROR_DROP_FRAGMENT],
2105 NAT_IN2OUT_ED_NEXT_DROP);
2107 vec_free (fragments_to_drop);
2108 vec_free (fragments_to_loopback);
2109 return frame->n_vectors;
2112 VLIB_NODE_FN (nat44_ed_in2out_reass_node) (vlib_main_t * vm,
2113 vlib_node_runtime_t * node,
2114 vlib_frame_t * frame)
2116 return nat44_ed_in2out_reass_node_fn_inline (vm, node, frame, 0);
2120 VLIB_REGISTER_NODE (nat44_ed_in2out_reass_node) = {
2121 .name = "nat44-ed-in2out-reass",
2122 .vector_size = sizeof (u32),
2123 .format_trace = format_nat44_reass_trace,
2124 .type = VLIB_NODE_TYPE_INTERNAL,
2125 .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
2126 .error_strings = nat_in2out_ed_error_strings,
2127 .n_next_nodes = NAT_IN2OUT_ED_N_NEXT,
2129 [NAT_IN2OUT_ED_NEXT_DROP] = "error-drop",
2130 [NAT_IN2OUT_ED_NEXT_LOOKUP] = "ip4-lookup",
2131 [NAT_IN2OUT_ED_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
2132 [NAT_IN2OUT_ED_NEXT_ICMP_ERROR] = "ip4-icmp-error",
2133 [NAT_IN2OUT_ED_NEXT_REASS] = "nat44-ed-in2out-reass",
2138 VLIB_NODE_FN (nat44_ed_in2out_reass_output_node) (vlib_main_t * vm,
2139 vlib_node_runtime_t * node,
2140 vlib_frame_t * frame)
2142 return nat44_ed_in2out_reass_node_fn_inline (vm, node, frame, 1);
2146 VLIB_REGISTER_NODE (nat44_ed_in2out_reass_output_node) = {
2147 .name = "nat44-ed-in2out-reass-output",
2148 .vector_size = sizeof (u32),
2149 .format_trace = format_nat44_reass_trace,
2150 .type = VLIB_NODE_TYPE_INTERNAL,
2151 .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
2152 .error_strings = nat_in2out_ed_error_strings,
2153 .n_next_nodes = NAT_IN2OUT_ED_N_NEXT,
2155 [NAT_IN2OUT_ED_NEXT_DROP] = "error-drop",
2156 [NAT_IN2OUT_ED_NEXT_LOOKUP] = "interface-output",
2157 [NAT_IN2OUT_ED_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
2158 [NAT_IN2OUT_ED_NEXT_ICMP_ERROR] = "ip4-icmp-error",
2159 [NAT_IN2OUT_ED_NEXT_REASS] = "nat44-ed-in2out-reass",
2165 * fd.io coding-style-patch-verification: ON
2168 * eval: (c-set-style "gnu")