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_log_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_log_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_log_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_log_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_log_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_log_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_log_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_log_debug ("TCP close connection %U", format_snat_session,
566 &sm->per_thread_data[thread_index], s);
567 nat_free_session_data (sm, s, thread_index, 0);
568 nat44_delete_session (sm, s, thread_index);
571 s->flags |= SNAT_SESSION_FLAG_OUTPUT_FEATURE;
576 make_ed_kv (&kv, &ip->dst_address, &ip->src_address, proto, rx_fib_index,
578 if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
580 s = pool_elt_at_index (tsm->sessions, value.value);
581 if (is_fwd_bypass_session (s))
586 pool_foreach (i, sm->output_feature_interfaces,
588 if ((nat_interface_is_inside (i)) && (rx_sw_if_index == i->sw_if_index))
598 #ifndef CLIB_MARCH_VARIANT
600 icmp_match_in2out_ed (snat_main_t * sm, vlib_node_runtime_t * node,
601 u32 thread_index, vlib_buffer_t * b, ip4_header_t * ip,
602 u8 * p_proto, snat_session_key_t * p_value,
603 u8 * p_dont_translate, void *d, void *e)
605 icmp46_header_t *icmp;
608 nat_ed_ses_key_t key;
609 snat_session_t *s = 0;
610 u8 dont_translate = 0;
611 clib_bihash_kv_16_8_t kv, value;
614 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
616 icmp = (icmp46_header_t *) ip4_next_header (ip);
617 sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
618 rx_fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
620 key.as_u64[0] = key.as_u64[1] = 0;
621 err = icmp_get_ed_key (ip, &key);
624 b->error = node->errors[err];
625 next = NAT_IN2OUT_ED_NEXT_DROP;
628 key.fib_index = rx_fib_index;
630 kv.key[0] = key.as_u64[0];
631 kv.key[1] = key.as_u64[1];
633 if (clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
635 if (vnet_buffer (b)->sw_if_index[VLIB_TX] != ~0)
637 if (PREDICT_FALSE (nat44_ed_not_translate_output_feature (sm, ip,
656 if (PREDICT_FALSE (nat44_ed_not_translate (sm, node, sw_if_index,
657 ip, SNAT_PROTOCOL_ICMP,
666 if (PREDICT_FALSE (icmp_is_error_message (icmp)))
668 b->error = node->errors[NAT_IN2OUT_ED_ERROR_BAD_ICMP_TYPE];
669 next = NAT_IN2OUT_ED_NEXT_DROP;
673 next = slow_path_ed (sm, b, rx_fib_index, &kv, &s, node, next,
674 thread_index, vlib_time_now (sm->vlib_main), 0);
676 if (PREDICT_FALSE (next == NAT_IN2OUT_ED_NEXT_DROP))
687 if (PREDICT_FALSE (icmp->type != ICMP4_echo_request &&
688 icmp->type != ICMP4_echo_reply &&
689 !icmp_is_error_message (icmp)))
691 b->error = node->errors[NAT_IN2OUT_ED_ERROR_BAD_ICMP_TYPE];
692 next = NAT_IN2OUT_ED_NEXT_DROP;
696 s = pool_elt_at_index (tsm->sessions, value.value);
699 *p_proto = ip_proto_to_snat_proto (key.proto);
702 *p_value = s->out2in;
703 *p_dont_translate = dont_translate;
705 *(snat_session_t **) d = s;
710 static snat_session_t *
711 nat44_ed_in2out_unknown_proto (snat_main_t * sm,
717 vlib_main_t * vm, vlib_node_runtime_t * node)
719 clib_bihash_kv_8_8_t kv, value;
720 clib_bihash_kv_16_8_t s_kv, s_value;
721 snat_static_mapping_t *m;
722 u32 old_addr, new_addr = 0;
725 dlist_elt_t *head, *elt;
726 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
727 u32 elt_index, head_index, ses_index;
729 u32 outside_fib_index = sm->outside_fib_index;
732 nat_outside_fib_t *outside_fib;
733 fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
735 .fp_proto = FIB_PROTOCOL_IP4,
738 .ip4.as_u32 = ip->dst_address.as_u32,
742 switch (vec_len (sm->outside_fibs))
745 outside_fib_index = sm->outside_fib_index;
748 outside_fib_index = sm->outside_fibs[0].fib_index;
752 vec_foreach (outside_fib, sm->outside_fibs)
754 fei = fib_table_lookup (outside_fib->fib_index, &pfx);
755 if (FIB_NODE_INDEX_INVALID != fei)
757 if (fib_entry_get_resolving_interface (fei) != ~0)
759 outside_fib_index = outside_fib->fib_index;
767 old_addr = ip->src_address.as_u32;
769 make_ed_kv (&s_kv, &ip->src_address, &ip->dst_address, ip->protocol,
772 if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &s_kv, &s_value))
774 s = pool_elt_at_index (tsm->sessions, s_value.value);
775 new_addr = ip->src_address.as_u32 = s->out2in.addr.as_u32;
779 if (PREDICT_FALSE (maximum_sessions_exceeded (sm, thread_index)))
781 b->error = node->errors[NAT_IN2OUT_ED_ERROR_MAX_SESSIONS_EXCEEDED];
782 nat_ipfix_logging_max_sessions (thread_index, sm->max_translations);
783 nat_log_notice ("maximum sessions exceeded");
787 u = nat_user_get_or_create (sm, &ip->src_address, rx_fib_index,
791 nat_log_warn ("create NAT user failed");
795 make_sm_kv (&kv, &ip->src_address, 0, rx_fib_index, 0);
797 /* Try to find static mapping first */
798 if (!clib_bihash_search_8_8 (&sm->static_mapping_by_local, &kv, &value))
800 m = pool_elt_at_index (sm->static_mappings, value.value);
801 new_addr = ip->src_address.as_u32 = m->external_addr.as_u32;
805 /* Fallback to 3-tuple key */
808 /* Choose same out address as for TCP/UDP session to same destination */
809 head_index = u->sessions_per_user_list_head_index;
810 head = pool_elt_at_index (tsm->list_pool, head_index);
811 elt_index = head->next;
812 if (PREDICT_FALSE (elt_index == ~0))
816 elt = pool_elt_at_index (tsm->list_pool, elt_index);
817 ses_index = elt->value;
820 while (ses_index != ~0)
822 s = pool_elt_at_index (tsm->sessions, ses_index);
823 elt_index = elt->next;
824 elt = pool_elt_at_index (tsm->list_pool, elt_index);
825 ses_index = elt->value;
827 if (s->ext_host_addr.as_u32 == ip->dst_address.as_u32)
829 new_addr = ip->src_address.as_u32 = s->out2in.addr.as_u32;
831 make_ed_kv (&s_kv, &s->out2in.addr, &ip->dst_address,
832 ip->protocol, outside_fib_index, 0, 0);
833 if (clib_bihash_search_16_8
834 (&tsm->out2in_ed, &s_kv, &s_value))
841 for (i = 0; i < vec_len (sm->addresses); i++)
843 make_ed_kv (&s_kv, &sm->addresses[i].addr, &ip->dst_address,
844 ip->protocol, outside_fib_index, 0, 0);
845 if (clib_bihash_search_16_8 (&tsm->out2in_ed, &s_kv, &s_value))
847 new_addr = ip->src_address.as_u32 =
848 sm->addresses[i].addr.as_u32;
856 s = nat_ed_session_alloc (sm, u, thread_index, now);
859 nat44_delete_user_with_no_session (sm, u, thread_index);
860 nat_log_warn ("create NAT session failed");
864 s->ext_host_addr.as_u32 = ip->dst_address.as_u32;
865 s->flags |= SNAT_SESSION_FLAG_UNKNOWN_PROTO;
866 s->flags |= SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT;
867 s->out2in.addr.as_u32 = new_addr;
868 s->out2in.fib_index = outside_fib_index;
869 s->in2out.addr.as_u32 = old_addr;
870 s->in2out.fib_index = rx_fib_index;
871 s->in2out.port = s->out2in.port = ip->protocol;
873 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
874 user_session_increment (sm, u, is_sm);
876 /* Add to lookup tables */
877 make_ed_kv (&s_kv, &s->in2out.addr, &ip->dst_address, ip->protocol,
879 s_kv.value = s - tsm->sessions;
880 if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &s_kv, 1))
881 nat_log_notice ("in2out key add failed");
883 make_ed_kv (&s_kv, &s->out2in.addr, &ip->dst_address, ip->protocol,
884 outside_fib_index, 0, 0);
885 s_kv.value = s - tsm->sessions;
886 if (clib_bihash_add_del_16_8 (&tsm->out2in_ed, &s_kv, 1))
887 nat_log_notice ("out2in key add failed");
890 /* Update IP checksum */
892 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, src_address);
893 ip->checksum = ip_csum_fold (sum);
896 nat44_session_update_counters (s, now, vlib_buffer_length_in_chain (vm, b),
898 /* Per-user LRU list maintenance */
899 nat44_session_update_lru (sm, s, thread_index);
902 if (vnet_buffer (b)->sw_if_index[VLIB_TX] == ~0)
903 nat44_ed_hairpinning_unknown_proto (sm, b, ip);
905 if (vnet_buffer (b)->sw_if_index[VLIB_TX] == ~0)
906 vnet_buffer (b)->sw_if_index[VLIB_TX] = outside_fib_index;
912 nat44_ed_in2out_node_fn_inline (vlib_main_t * vm,
913 vlib_node_runtime_t * node,
914 vlib_frame_t * frame, int is_slow_path,
915 int is_output_feature)
917 u32 n_left_from, *from, *to_next, pkts_processed = 0, stats_node_index;
918 nat_in2out_ed_next_t next_index;
919 snat_main_t *sm = &snat_main;
920 f64 now = vlib_time_now (vm);
921 u32 thread_index = vm->thread_index;
922 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
923 u32 tcp_packets = 0, udp_packets = 0, icmp_packets = 0, other_packets =
926 stats_node_index = is_slow_path ? sm->ed_in2out_slowpath_node_index :
927 sm->ed_in2out_node_index;
929 from = vlib_frame_vector_args (frame);
930 n_left_from = frame->n_vectors;
931 next_index = node->cached_next_index;
933 while (n_left_from > 0)
937 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
939 while (n_left_from >= 4 && n_left_to_next >= 2)
942 vlib_buffer_t *b0, *b1;
943 u32 next0, sw_if_index0, rx_fib_index0, iph_offset0 = 0, proto0,
944 new_addr0, old_addr0;
945 u32 next1, sw_if_index1, rx_fib_index1, iph_offset1 = 0, proto1,
946 new_addr1, old_addr1;
947 u16 old_port0, new_port0, old_port1, new_port1;
948 ip4_header_t *ip0, *ip1;
949 udp_header_t *udp0, *udp1;
950 tcp_header_t *tcp0, *tcp1;
951 icmp46_header_t *icmp0, *icmp1;
952 snat_session_t *s0 = 0, *s1 = 0;
953 clib_bihash_kv_16_8_t kv0, value0, kv1, value1;
954 ip_csum_t sum0, sum1;
956 /* Prefetch next iteration. */
958 vlib_buffer_t *p2, *p3;
960 p2 = vlib_get_buffer (vm, from[2]);
961 p3 = vlib_get_buffer (vm, from[3]);
963 vlib_prefetch_buffer_header (p2, LOAD);
964 vlib_prefetch_buffer_header (p3, LOAD);
966 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
967 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
970 /* speculatively enqueue b0 and b1 to the current next frame */
971 to_next[0] = bi0 = from[0];
972 to_next[1] = bi1 = from[1];
978 b0 = vlib_get_buffer (vm, bi0);
979 b1 = vlib_get_buffer (vm, bi1);
981 next0 = NAT_IN2OUT_ED_NEXT_LOOKUP;
983 if (is_output_feature)
984 iph_offset0 = vnet_buffer (b0)->ip.save_rewrite_length;
986 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
989 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
991 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
994 if (PREDICT_FALSE (ip0->ttl == 1))
996 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
997 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
998 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1000 next0 = NAT_IN2OUT_ED_NEXT_ICMP_ERROR;
1004 udp0 = ip4_next_header (ip0);
1005 tcp0 = (tcp_header_t *) udp0;
1006 icmp0 = (icmp46_header_t *) udp0;
1007 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1011 if (PREDICT_FALSE (proto0 == ~0))
1013 s0 = nat44_ed_in2out_unknown_proto (sm, b0, ip0,
1015 thread_index, now, vm,
1018 next0 = NAT_IN2OUT_ED_NEXT_DROP;
1023 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1025 next0 = icmp_in2out_ed_slow_path
1026 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1027 next0, now, thread_index, &s0);
1034 if (PREDICT_FALSE (proto0 == ~0))
1036 next0 = NAT_IN2OUT_ED_NEXT_SLOW_PATH;
1040 if (ip4_is_fragment (ip0))
1042 next0 = NAT_IN2OUT_ED_NEXT_REASS;
1047 if (is_output_feature)
1050 (nat_not_translate_output_feature_fwd
1051 (sm, ip0, thread_index, now, vm, b0)))
1055 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1057 next0 = NAT_IN2OUT_ED_NEXT_SLOW_PATH;
1062 make_ed_kv (&kv0, &ip0->src_address, &ip0->dst_address,
1063 ip0->protocol, rx_fib_index0, udp0->src_port,
1066 if (clib_bihash_search_16_8 (&tsm->in2out_ed, &kv0, &value0))
1070 if (is_output_feature)
1073 (nat44_ed_not_translate_output_feature
1074 (sm, ip0, ip0->protocol, udp0->src_port,
1075 udp0->dst_port, thread_index, sw_if_index0,
1076 vnet_buffer (b0)->sw_if_index[VLIB_TX])))
1081 if (PREDICT_FALSE (nat44_ed_not_translate (sm, node,
1090 slow_path_ed (sm, b0, rx_fib_index0, &kv0, &s0, node,
1091 next0, thread_index, now, tcp0);
1093 if (PREDICT_FALSE (next0 == NAT_IN2OUT_ED_NEXT_DROP))
1096 if (PREDICT_FALSE (!s0))
1101 next0 = NAT_IN2OUT_ED_NEXT_SLOW_PATH;
1107 s0 = pool_elt_at_index (tsm->sessions, value0.value);
1110 b0->flags |= VNET_BUFFER_F_IS_NATED;
1112 if (!is_output_feature)
1113 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1115 old_addr0 = ip0->src_address.as_u32;
1116 new_addr0 = ip0->src_address.as_u32 = s0->out2in.addr.as_u32;
1117 sum0 = ip0->checksum;
1118 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1120 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1121 sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
1122 s0->ext_host_addr.as_u32, ip4_header_t,
1124 ip0->checksum = ip_csum_fold (sum0);
1126 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1128 old_port0 = tcp0->src_port;
1129 new_port0 = tcp0->src_port = s0->out2in.port;
1131 sum0 = tcp0->checksum;
1132 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1134 sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
1136 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1138 sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
1139 s0->ext_host_addr.as_u32,
1140 ip4_header_t, dst_address);
1141 sum0 = ip_csum_update (sum0, tcp0->dst_port,
1142 s0->ext_host_port, ip4_header_t,
1144 tcp0->dst_port = s0->ext_host_port;
1145 ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
1147 mss_clamping (sm, tcp0, &sum0);
1148 tcp0->checksum = ip_csum_fold (sum0);
1150 if (nat44_set_tcp_session_state_i2o
1151 (sm, s0, tcp0, thread_index))
1156 udp0->src_port = s0->out2in.port;
1158 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1160 udp0->dst_port = s0->ext_host_port;
1161 ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
1167 nat44_session_update_counters (s0, now,
1168 vlib_buffer_length_in_chain (vm,
1171 /* Per-user LRU list maintenance */
1172 nat44_session_update_lru (sm, s0, thread_index);
1175 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1176 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1178 nat_in2out_ed_trace_t *t =
1179 vlib_add_trace (vm, node, b0, sizeof (*t));
1180 t->is_slow_path = is_slow_path;
1181 t->sw_if_index = sw_if_index0;
1182 t->next_index = next0;
1183 t->session_index = ~0;
1185 t->session_index = s0 - tsm->sessions;
1188 pkts_processed += next0 == NAT_IN2OUT_ED_NEXT_LOOKUP;
1191 next1 = NAT_IN2OUT_ED_NEXT_LOOKUP;
1193 if (is_output_feature)
1194 iph_offset1 = vnet_buffer (b1)->ip.save_rewrite_length;
1196 ip1 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b1) +
1199 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
1201 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1204 if (PREDICT_FALSE (ip1->ttl == 1))
1206 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1207 icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
1208 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1210 next1 = NAT_IN2OUT_ED_NEXT_ICMP_ERROR;
1214 udp1 = ip4_next_header (ip1);
1215 tcp1 = (tcp_header_t *) udp1;
1216 icmp1 = (icmp46_header_t *) udp1;
1217 proto1 = ip_proto_to_snat_proto (ip1->protocol);
1221 if (PREDICT_FALSE (proto1 == ~0))
1223 s1 = nat44_ed_in2out_unknown_proto (sm, b1, ip1,
1225 thread_index, now, vm,
1228 next1 = NAT_IN2OUT_ED_NEXT_DROP;
1233 if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
1235 next1 = icmp_in2out_ed_slow_path
1236 (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node,
1237 next1, now, thread_index, &s1);
1244 if (PREDICT_FALSE (proto1 == ~0))
1246 next1 = NAT_IN2OUT_ED_NEXT_SLOW_PATH;
1250 if (ip4_is_fragment (ip1))
1252 next1 = NAT_IN2OUT_ED_NEXT_REASS;
1257 if (is_output_feature)
1260 (nat_not_translate_output_feature_fwd
1261 (sm, ip1, thread_index, now, vm, b1)))
1265 if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
1267 next1 = NAT_IN2OUT_ED_NEXT_SLOW_PATH;
1272 make_ed_kv (&kv1, &ip1->src_address, &ip1->dst_address,
1273 ip1->protocol, rx_fib_index1, udp1->src_port,
1276 if (clib_bihash_search_16_8 (&tsm->in2out_ed, &kv1, &value1))
1280 if (is_output_feature)
1283 (nat44_ed_not_translate_output_feature
1284 (sm, ip1, ip1->protocol, udp1->src_port,
1285 udp1->dst_port, thread_index, sw_if_index1,
1286 vnet_buffer (b1)->sw_if_index[VLIB_TX])))
1291 if (PREDICT_FALSE (nat44_ed_not_translate (sm, node,
1300 slow_path_ed (sm, b1, rx_fib_index1, &kv1, &s1, node,
1301 next1, thread_index, now, tcp1);
1303 if (PREDICT_FALSE (next1 == NAT_IN2OUT_ED_NEXT_DROP))
1306 if (PREDICT_FALSE (!s1))
1311 next1 = NAT_IN2OUT_ED_NEXT_SLOW_PATH;
1317 s1 = pool_elt_at_index (tsm->sessions, value1.value);
1320 b1->flags |= VNET_BUFFER_F_IS_NATED;
1322 if (!is_output_feature)
1323 vnet_buffer (b1)->sw_if_index[VLIB_TX] = s1->out2in.fib_index;
1325 old_addr1 = ip1->src_address.as_u32;
1326 new_addr1 = ip1->src_address.as_u32 = s1->out2in.addr.as_u32;
1327 sum1 = ip1->checksum;
1328 sum1 = ip_csum_update (sum1, old_addr1, new_addr1, ip4_header_t,
1330 if (PREDICT_FALSE (is_twice_nat_session (s1)))
1331 sum1 = ip_csum_update (sum1, ip1->dst_address.as_u32,
1332 s1->ext_host_addr.as_u32, ip4_header_t,
1334 ip1->checksum = ip_csum_fold (sum1);
1336 if (PREDICT_TRUE (proto1 == SNAT_PROTOCOL_TCP))
1338 old_port1 = tcp1->src_port;
1339 new_port1 = tcp1->src_port = s1->out2in.port;
1341 sum1 = tcp1->checksum;
1342 sum1 = ip_csum_update (sum1, old_addr1, new_addr1, ip4_header_t,
1344 sum1 = ip_csum_update (sum1, old_port1, new_port1, ip4_header_t,
1346 if (PREDICT_FALSE (is_twice_nat_session (s1)))
1348 sum1 = ip_csum_update (sum1, ip1->dst_address.as_u32,
1349 s1->ext_host_addr.as_u32,
1350 ip4_header_t, dst_address);
1351 sum1 = ip_csum_update (sum1, tcp1->dst_port,
1352 s1->ext_host_port, ip4_header_t,
1354 tcp1->dst_port = s1->ext_host_port;
1355 ip1->dst_address.as_u32 = s1->ext_host_addr.as_u32;
1357 tcp1->checksum = ip_csum_fold (sum1);
1358 mss_clamping (sm, tcp1, &sum1);
1360 if (nat44_set_tcp_session_state_i2o
1361 (sm, s1, tcp1, thread_index))
1366 udp1->src_port = s1->out2in.port;
1368 if (PREDICT_FALSE (is_twice_nat_session (s1)))
1370 udp1->dst_port = s1->ext_host_port;
1371 ip1->dst_address.as_u32 = s1->ext_host_addr.as_u32;
1377 nat44_session_update_counters (s1, now,
1378 vlib_buffer_length_in_chain (vm, b1),
1380 /* Per-user LRU list maintenance */
1381 nat44_session_update_lru (sm, s1, thread_index);
1384 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1385 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
1387 nat_in2out_ed_trace_t *t =
1388 vlib_add_trace (vm, node, b1, sizeof (*t));
1389 t->is_slow_path = is_slow_path;
1390 t->sw_if_index = sw_if_index1;
1391 t->next_index = next1;
1392 t->session_index = ~0;
1394 t->session_index = s1 - tsm->sessions;
1397 pkts_processed += next1 == NAT_IN2OUT_ED_NEXT_LOOKUP;
1399 /* verify speculative enqueues, maybe switch current next frame */
1400 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1401 to_next, n_left_to_next,
1402 bi0, bi1, next0, next1);
1405 while (n_left_from > 0 && n_left_to_next > 0)
1409 u32 next0, sw_if_index0, rx_fib_index0, iph_offset0 = 0, proto0,
1410 new_addr0, old_addr0;
1411 u16 old_port0, new_port0;
1415 icmp46_header_t *icmp0;
1416 snat_session_t *s0 = 0;
1417 clib_bihash_kv_16_8_t kv0, value0;
1420 /* speculatively enqueue b0 to the current next frame */
1426 n_left_to_next -= 1;
1428 b0 = vlib_get_buffer (vm, bi0);
1429 next0 = NAT_IN2OUT_ED_NEXT_LOOKUP;
1431 if (is_output_feature)
1432 iph_offset0 = vnet_buffer (b0)->ip.save_rewrite_length;
1434 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
1437 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1439 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1442 if (PREDICT_FALSE (ip0->ttl == 1))
1444 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1445 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1446 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1448 next0 = NAT_IN2OUT_ED_NEXT_ICMP_ERROR;
1452 udp0 = ip4_next_header (ip0);
1453 tcp0 = (tcp_header_t *) udp0;
1454 icmp0 = (icmp46_header_t *) udp0;
1455 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1459 if (PREDICT_FALSE (proto0 == ~0))
1461 s0 = nat44_ed_in2out_unknown_proto (sm, b0, ip0,
1463 thread_index, now, vm,
1466 next0 = NAT_IN2OUT_ED_NEXT_DROP;
1471 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1473 next0 = icmp_in2out_ed_slow_path
1474 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1475 next0, now, thread_index, &s0);
1482 if (PREDICT_FALSE (proto0 == ~0))
1484 next0 = NAT_IN2OUT_ED_NEXT_SLOW_PATH;
1488 if (ip4_is_fragment (ip0))
1490 next0 = NAT_IN2OUT_ED_NEXT_REASS;
1495 if (is_output_feature)
1498 (nat_not_translate_output_feature_fwd
1499 (sm, ip0, thread_index, now, vm, b0)))
1503 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1505 next0 = NAT_IN2OUT_ED_NEXT_SLOW_PATH;
1510 make_ed_kv (&kv0, &ip0->src_address, &ip0->dst_address,
1511 ip0->protocol, rx_fib_index0, udp0->src_port,
1514 if (clib_bihash_search_16_8 (&tsm->in2out_ed, &kv0, &value0))
1518 if (is_output_feature)
1521 (nat44_ed_not_translate_output_feature
1522 (sm, ip0, ip0->protocol, udp0->src_port,
1523 udp0->dst_port, thread_index, sw_if_index0,
1524 vnet_buffer (b0)->sw_if_index[VLIB_TX])))
1529 if (PREDICT_FALSE (nat44_ed_not_translate (sm, node,
1538 slow_path_ed (sm, b0, rx_fib_index0, &kv0, &s0, node,
1539 next0, thread_index, now, tcp0);
1541 if (PREDICT_FALSE (next0 == NAT_IN2OUT_ED_NEXT_DROP))
1544 if (PREDICT_FALSE (!s0))
1549 next0 = NAT_IN2OUT_ED_NEXT_SLOW_PATH;
1555 s0 = pool_elt_at_index (tsm->sessions, value0.value);
1558 b0->flags |= VNET_BUFFER_F_IS_NATED;
1560 if (!is_output_feature)
1561 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1563 old_addr0 = ip0->src_address.as_u32;
1564 new_addr0 = ip0->src_address.as_u32 = s0->out2in.addr.as_u32;
1565 sum0 = ip0->checksum;
1566 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1568 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1569 sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
1570 s0->ext_host_addr.as_u32, ip4_header_t,
1572 ip0->checksum = ip_csum_fold (sum0);
1574 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1576 old_port0 = tcp0->src_port;
1577 new_port0 = tcp0->src_port = s0->out2in.port;
1579 sum0 = tcp0->checksum;
1580 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1582 sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
1584 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1586 sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
1587 s0->ext_host_addr.as_u32,
1588 ip4_header_t, dst_address);
1589 sum0 = ip_csum_update (sum0, tcp0->dst_port,
1590 s0->ext_host_port, ip4_header_t,
1592 tcp0->dst_port = s0->ext_host_port;
1593 ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
1595 mss_clamping (sm, tcp0, &sum0);
1596 tcp0->checksum = ip_csum_fold (sum0);
1598 if (nat44_set_tcp_session_state_i2o
1599 (sm, s0, tcp0, thread_index))
1604 udp0->src_port = s0->out2in.port;
1606 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1608 udp0->dst_port = s0->ext_host_port;
1609 ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
1615 nat44_session_update_counters (s0, now,
1616 vlib_buffer_length_in_chain (vm, b0),
1618 /* Per-user LRU list maintenance */
1619 nat44_session_update_lru (sm, s0, thread_index);
1622 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1623 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1625 nat_in2out_ed_trace_t *t =
1626 vlib_add_trace (vm, node, b0, sizeof (*t));
1627 t->is_slow_path = is_slow_path;
1628 t->sw_if_index = sw_if_index0;
1629 t->next_index = next0;
1630 t->session_index = ~0;
1632 t->session_index = s0 - tsm->sessions;
1635 pkts_processed += next0 == NAT_IN2OUT_ED_NEXT_LOOKUP;
1637 /* verify speculative enqueue, maybe switch current next frame */
1638 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1639 to_next, n_left_to_next,
1643 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1646 vlib_node_increment_counter (vm, stats_node_index,
1647 NAT_IN2OUT_ED_ERROR_IN2OUT_PACKETS,
1649 vlib_node_increment_counter (vm, stats_node_index,
1650 NAT_IN2OUT_ED_ERROR_TCP_PACKETS, tcp_packets);
1651 vlib_node_increment_counter (vm, stats_node_index,
1652 NAT_IN2OUT_ED_ERROR_UDP_PACKETS, tcp_packets);
1653 vlib_node_increment_counter (vm, stats_node_index,
1654 NAT_IN2OUT_ED_ERROR_ICMP_PACKETS,
1656 vlib_node_increment_counter (vm, stats_node_index,
1657 NAT_IN2OUT_ED_ERROR_OTHER_PACKETS,
1659 vlib_node_increment_counter (vm, stats_node_index,
1660 NAT_IN2OUT_ED_ERROR_FRAGMENTS, fragments);
1662 return frame->n_vectors;
1665 VLIB_NODE_FN (nat44_ed_in2out_node) (vlib_main_t * vm,
1666 vlib_node_runtime_t * node,
1667 vlib_frame_t * frame)
1669 return nat44_ed_in2out_node_fn_inline (vm, node, frame, 0, 0);
1673 VLIB_REGISTER_NODE (nat44_ed_in2out_node) = {
1674 .name = "nat44-ed-in2out",
1675 .vector_size = sizeof (u32),
1676 .format_trace = format_nat_in2out_ed_trace,
1677 .type = VLIB_NODE_TYPE_INTERNAL,
1678 .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
1679 .error_strings = nat_in2out_ed_error_strings,
1680 .runtime_data_bytes = sizeof (snat_runtime_t),
1681 .n_next_nodes = NAT_IN2OUT_ED_N_NEXT,
1683 [NAT_IN2OUT_ED_NEXT_DROP] = "error-drop",
1684 [NAT_IN2OUT_ED_NEXT_LOOKUP] = "ip4-lookup",
1685 [NAT_IN2OUT_ED_NEXT_SLOW_PATH] = "nat44-ed-in2out-slowpath",
1686 [NAT_IN2OUT_ED_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1687 [NAT_IN2OUT_ED_NEXT_REASS] = "nat44-ed-in2out-reass",
1692 VLIB_NODE_FN (nat44_ed_in2out_output_node) (vlib_main_t * vm,
1693 vlib_node_runtime_t * node,
1694 vlib_frame_t * frame)
1696 return nat44_ed_in2out_node_fn_inline (vm, node, frame, 0, 1);
1700 VLIB_REGISTER_NODE (nat44_ed_in2out_output_node) = {
1701 .name = "nat44-ed-in2out-output",
1702 .vector_size = sizeof (u32),
1703 .format_trace = format_nat_in2out_ed_trace,
1704 .type = VLIB_NODE_TYPE_INTERNAL,
1705 .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
1706 .error_strings = nat_in2out_ed_error_strings,
1707 .runtime_data_bytes = sizeof (snat_runtime_t),
1708 .n_next_nodes = NAT_IN2OUT_ED_N_NEXT,
1710 [NAT_IN2OUT_ED_NEXT_DROP] = "error-drop",
1711 [NAT_IN2OUT_ED_NEXT_LOOKUP] = "interface-output",
1712 [NAT_IN2OUT_ED_NEXT_SLOW_PATH] = "nat44-ed-in2out-output-slowpath",
1713 [NAT_IN2OUT_ED_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1714 [NAT_IN2OUT_ED_NEXT_REASS] = "nat44-ed-in2out-reass-output",
1719 VLIB_NODE_FN (nat44_ed_in2out_slowpath_node) (vlib_main_t * vm,
1720 vlib_node_runtime_t * node,
1721 vlib_frame_t * frame)
1723 return nat44_ed_in2out_node_fn_inline (vm, node, frame, 1, 0);
1727 VLIB_REGISTER_NODE (nat44_ed_in2out_slowpath_node) = {
1728 .name = "nat44-ed-in2out-slowpath",
1729 .vector_size = sizeof (u32),
1730 .format_trace = format_nat_in2out_ed_trace,
1731 .type = VLIB_NODE_TYPE_INTERNAL,
1732 .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
1733 .error_strings = nat_in2out_ed_error_strings,
1734 .runtime_data_bytes = sizeof (snat_runtime_t),
1735 .n_next_nodes = NAT_IN2OUT_ED_N_NEXT,
1737 [NAT_IN2OUT_ED_NEXT_DROP] = "error-drop",
1738 [NAT_IN2OUT_ED_NEXT_LOOKUP] = "ip4-lookup",
1739 [NAT_IN2OUT_ED_NEXT_SLOW_PATH] = "nat44-ed-in2out-slowpath",
1740 [NAT_IN2OUT_ED_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1741 [NAT_IN2OUT_ED_NEXT_REASS] = "nat44-ed-in2out-reass",
1746 VLIB_NODE_FN (nat44_ed_in2out_output_slowpath_node) (vlib_main_t * vm,
1747 vlib_node_runtime_t *
1749 vlib_frame_t * frame)
1751 return nat44_ed_in2out_node_fn_inline (vm, node, frame, 1, 1);
1755 VLIB_REGISTER_NODE (nat44_ed_in2out_output_slowpath_node) = {
1756 .name = "nat44-ed-in2out-output-slowpath",
1757 .vector_size = sizeof (u32),
1758 .format_trace = format_nat_in2out_ed_trace,
1759 .type = VLIB_NODE_TYPE_INTERNAL,
1760 .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
1761 .error_strings = nat_in2out_ed_error_strings,
1762 .runtime_data_bytes = sizeof (snat_runtime_t),
1763 .n_next_nodes = NAT_IN2OUT_ED_N_NEXT,
1765 [NAT_IN2OUT_ED_NEXT_DROP] = "error-drop",
1766 [NAT_IN2OUT_ED_NEXT_LOOKUP] = "interface-output",
1767 [NAT_IN2OUT_ED_NEXT_SLOW_PATH] = "nat44-ed-in2out-output-slowpath",
1768 [NAT_IN2OUT_ED_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1769 [NAT_IN2OUT_ED_NEXT_REASS] = "nat44-ed-in2out-reass",
1775 nat44_ed_in2out_reass_node_fn_inline (vlib_main_t * vm,
1776 vlib_node_runtime_t * node,
1777 vlib_frame_t * frame,
1778 int is_output_feature)
1780 u32 n_left_from, *from, *to_next;
1781 nat_in2out_ed_next_t next_index;
1782 u32 pkts_processed = 0, cached_fragments = 0;
1783 snat_main_t *sm = &snat_main;
1784 f64 now = vlib_time_now (vm);
1785 u32 thread_index = vm->thread_index;
1786 snat_main_per_thread_data_t *per_thread_data =
1787 &sm->per_thread_data[thread_index];
1788 u32 *fragments_to_drop = 0;
1789 u32 *fragments_to_loopback = 0;
1791 from = vlib_frame_vector_args (frame);
1792 n_left_from = frame->n_vectors;
1793 next_index = node->cached_next_index;
1795 while (n_left_from > 0)
1799 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1801 while (n_left_from > 0 && n_left_to_next > 0)
1803 u32 bi0, sw_if_index0, proto0, rx_fib_index0, new_addr0, old_addr0;
1804 u32 iph_offset0 = 0;
1808 ip4_header_t *ip0 = 0;
1809 nat_reass_ip4_t *reass0;
1812 icmp46_header_t *icmp0;
1813 clib_bihash_kv_16_8_t kv0, value0;
1814 snat_session_t *s0 = 0;
1815 u16 old_port0, new_port0;
1818 /* speculatively enqueue b0 to the current next frame */
1824 n_left_to_next -= 1;
1826 b0 = vlib_get_buffer (vm, bi0);
1828 next0 = NAT_IN2OUT_ED_NEXT_LOOKUP;
1830 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1832 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1835 if (PREDICT_FALSE (nat_reass_is_drop_frag (0)))
1837 next0 = NAT_IN2OUT_ED_NEXT_DROP;
1838 b0->error = node->errors[NAT_IN2OUT_ED_ERROR_DROP_FRAGMENT];
1842 if (is_output_feature)
1843 iph_offset0 = vnet_buffer (b0)->ip.save_rewrite_length;
1845 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
1848 udp0 = ip4_next_header (ip0);
1849 tcp0 = (tcp_header_t *) udp0;
1850 icmp0 = (icmp46_header_t *) udp0;
1851 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1853 reass0 = nat_ip4_reass_find_or_create (ip0->src_address,
1857 1, &fragments_to_drop);
1859 if (PREDICT_FALSE (!reass0))
1861 next0 = NAT_IN2OUT_ED_NEXT_DROP;
1862 b0->error = node->errors[NAT_IN2OUT_ED_ERROR_MAX_REASS];
1863 nat_log_notice ("maximum reassemblies exceeded");
1867 if (PREDICT_FALSE (ip4_is_first_fragment (ip0)))
1869 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1871 if (is_output_feature)
1874 (nat_not_translate_output_feature_fwd
1875 (sm, ip0, thread_index, now, vm, b0)))
1876 reass0->flags |= NAT_REASS_FLAG_ED_DONT_TRANSLATE;
1880 next0 = icmp_in2out_ed_slow_path
1881 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1882 next0, now, thread_index, &s0);
1884 if (PREDICT_TRUE (next0 != NAT_IN2OUT_ED_NEXT_DROP))
1887 reass0->sess_index = s0 - per_thread_data->sessions;
1889 reass0->flags |= NAT_REASS_FLAG_ED_DONT_TRANSLATE;
1890 nat_ip4_reass_get_frags (reass0,
1891 &fragments_to_loopback);
1897 make_ed_kv (&kv0, &ip0->src_address, &ip0->dst_address,
1898 ip0->protocol, rx_fib_index0, udp0->src_port,
1901 if (clib_bihash_search_16_8
1902 (&per_thread_data->in2out_ed, &kv0, &value0))
1904 if (is_output_feature)
1907 (nat44_ed_not_translate_output_feature
1908 (sm, ip0, ip0->protocol, udp0->src_port,
1909 udp0->dst_port, thread_index, sw_if_index0,
1910 vnet_buffer (b0)->sw_if_index[VLIB_TX])))
1912 reass0->flags |= NAT_REASS_FLAG_ED_DONT_TRANSLATE;
1913 nat_ip4_reass_get_frags (reass0,
1914 &fragments_to_loopback);
1920 if (PREDICT_FALSE (nat44_ed_not_translate (sm, node,
1926 reass0->flags |= NAT_REASS_FLAG_ED_DONT_TRANSLATE;
1927 nat_ip4_reass_get_frags (reass0,
1928 &fragments_to_loopback);
1933 next0 = slow_path_ed (sm, b0, rx_fib_index0, &kv0,
1934 &s0, node, next0, thread_index, now,
1937 if (PREDICT_FALSE (next0 == NAT_IN2OUT_ED_NEXT_DROP))
1940 if (PREDICT_FALSE (!s0))
1942 reass0->flags |= NAT_REASS_FLAG_ED_DONT_TRANSLATE;
1946 reass0->sess_index = s0 - per_thread_data->sessions;
1950 s0 = pool_elt_at_index (per_thread_data->sessions,
1952 reass0->sess_index = value0.value;
1954 nat_ip4_reass_get_frags (reass0, &fragments_to_loopback);
1958 if (reass0->flags & NAT_REASS_FLAG_ED_DONT_TRANSLATE)
1960 if (PREDICT_FALSE (reass0->sess_index == (u32) ~ 0))
1962 if (nat_ip4_reass_add_fragment
1963 (thread_index, reass0, bi0, &fragments_to_drop))
1965 b0->error = node->errors[NAT_IN2OUT_ED_ERROR_MAX_FRAG];
1967 ("maximum fragments per reassembly exceeded");
1968 next0 = NAT_IN2OUT_ED_NEXT_DROP;
1974 s0 = pool_elt_at_index (per_thread_data->sessions,
1975 reass0->sess_index);
1978 old_addr0 = ip0->src_address.as_u32;
1979 ip0->src_address = s0->out2in.addr;
1980 new_addr0 = ip0->src_address.as_u32;
1981 if (!is_output_feature)
1982 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1984 sum0 = ip0->checksum;
1985 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1987 src_address /* changed member */ );
1988 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1989 sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
1990 s0->ext_host_addr.as_u32, ip4_header_t,
1992 ip0->checksum = ip_csum_fold (sum0);
1994 if (PREDICT_FALSE (ip4_is_first_fragment (ip0)))
1996 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1998 old_port0 = tcp0->src_port;
1999 tcp0->src_port = s0->out2in.port;
2000 new_port0 = tcp0->src_port;
2002 sum0 = tcp0->checksum;
2003 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
2005 dst_address /* changed member */ );
2006 sum0 = ip_csum_update (sum0, old_port0, new_port0,
2007 ip4_header_t /* cheat */ ,
2008 length /* changed member */ );
2009 if (PREDICT_FALSE (is_twice_nat_session (s0)))
2011 sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
2012 s0->ext_host_addr.as_u32,
2013 ip4_header_t, dst_address);
2014 sum0 = ip_csum_update (sum0, tcp0->dst_port,
2015 s0->ext_host_port, ip4_header_t,
2017 tcp0->dst_port = s0->ext_host_port;
2018 ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
2020 tcp0->checksum = ip_csum_fold (sum0);
2024 old_port0 = udp0->src_port;
2025 udp0->src_port = s0->out2in.port;
2027 if (PREDICT_FALSE (is_twice_nat_session (s0)))
2029 udp0->dst_port = s0->ext_host_port;
2030 ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
2036 nat44_reass_hairpinning (sm, b0, ip0, s0->out2in.port,
2037 s0->ext_host_port, proto0, 1);
2040 nat44_session_update_counters (s0, now,
2041 vlib_buffer_length_in_chain (vm, b0),
2043 /* Per-user LRU list maintenance */
2044 nat44_session_update_lru (sm, s0, thread_index);
2047 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
2048 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
2050 nat44_reass_trace_t *t =
2051 vlib_add_trace (vm, node, b0, sizeof (*t));
2052 t->cached = cached0;
2053 t->sw_if_index = sw_if_index0;
2054 t->next_index = next0;
2065 pkts_processed += next0 != NAT_IN2OUT_ED_NEXT_DROP;
2067 /* verify speculative enqueue, maybe switch current next frame */
2068 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2069 to_next, n_left_to_next,
2073 if (n_left_from == 0 && vec_len (fragments_to_loopback))
2075 from = vlib_frame_vector_args (frame);
2076 u32 len = vec_len (fragments_to_loopback);
2077 if (len <= VLIB_FRAME_SIZE)
2079 clib_memcpy_fast (from, fragments_to_loopback,
2080 sizeof (u32) * len);
2082 vec_reset_length (fragments_to_loopback);
2086 clib_memcpy_fast (from, fragments_to_loopback +
2087 (len - VLIB_FRAME_SIZE),
2088 sizeof (u32) * VLIB_FRAME_SIZE);
2089 n_left_from = VLIB_FRAME_SIZE;
2090 _vec_len (fragments_to_loopback) = len - VLIB_FRAME_SIZE;
2095 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2098 vlib_node_increment_counter (vm, sm->ed_in2out_reass_node_index,
2099 NAT_IN2OUT_ED_ERROR_PROCESSED_FRAGMENTS,
2101 vlib_node_increment_counter (vm, sm->ed_in2out_reass_node_index,
2102 NAT_IN2OUT_ED_ERROR_CACHED_FRAGMENTS,
2105 nat_send_all_to_node (vm, fragments_to_drop, node,
2106 &node->errors[NAT_IN2OUT_ED_ERROR_DROP_FRAGMENT],
2107 NAT_IN2OUT_ED_NEXT_DROP);
2109 vec_free (fragments_to_drop);
2110 vec_free (fragments_to_loopback);
2111 return frame->n_vectors;
2114 VLIB_NODE_FN (nat44_ed_in2out_reass_node) (vlib_main_t * vm,
2115 vlib_node_runtime_t * node,
2116 vlib_frame_t * frame)
2118 return nat44_ed_in2out_reass_node_fn_inline (vm, node, frame, 0);
2122 VLIB_REGISTER_NODE (nat44_ed_in2out_reass_node) = {
2123 .name = "nat44-ed-in2out-reass",
2124 .vector_size = sizeof (u32),
2125 .format_trace = format_nat44_reass_trace,
2126 .type = VLIB_NODE_TYPE_INTERNAL,
2127 .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
2128 .error_strings = nat_in2out_ed_error_strings,
2129 .n_next_nodes = NAT_IN2OUT_ED_N_NEXT,
2131 [NAT_IN2OUT_ED_NEXT_DROP] = "error-drop",
2132 [NAT_IN2OUT_ED_NEXT_LOOKUP] = "ip4-lookup",
2133 [NAT_IN2OUT_ED_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
2134 [NAT_IN2OUT_ED_NEXT_ICMP_ERROR] = "ip4-icmp-error",
2135 [NAT_IN2OUT_ED_NEXT_REASS] = "nat44-ed-in2out-reass",
2140 VLIB_NODE_FN (nat44_ed_in2out_reass_output_node) (vlib_main_t * vm,
2141 vlib_node_runtime_t * node,
2142 vlib_frame_t * frame)
2144 return nat44_ed_in2out_reass_node_fn_inline (vm, node, frame, 1);
2148 VLIB_REGISTER_NODE (nat44_ed_in2out_reass_output_node) = {
2149 .name = "nat44-ed-in2out-reass-output",
2150 .vector_size = sizeof (u32),
2151 .format_trace = format_nat44_reass_trace,
2152 .type = VLIB_NODE_TYPE_INTERNAL,
2153 .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
2154 .error_strings = nat_in2out_ed_error_strings,
2155 .n_next_nodes = NAT_IN2OUT_ED_N_NEXT,
2157 [NAT_IN2OUT_ED_NEXT_DROP] = "error-drop",
2158 [NAT_IN2OUT_ED_NEXT_LOOKUP] = "interface-output",
2159 [NAT_IN2OUT_ED_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
2160 [NAT_IN2OUT_ED_NEXT_ICMP_ERROR] = "ip4-icmp-error",
2161 [NAT_IN2OUT_ED_NEXT_REASS] = "nat44-ed-in2out-reass",
2167 * fd.io coding-style-patch-verification: ON
2170 * eval: (c-set-style "gnu")