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>
32 #define foreach_nat_in2out_ed_error \
33 _(UNSUPPORTED_PROTOCOL, "Unsupported protocol") \
34 _(IN2OUT_PACKETS, "Good in2out packets processed") \
35 _(OUT_OF_PORTS, "Out of ports") \
36 _(BAD_ICMP_TYPE, "unsupported ICMP type") \
37 _(MAX_SESSIONS_EXCEEDED, "Maximum sessions exceeded") \
38 _(DROP_FRAGMENT, "Drop fragment") \
39 _(MAX_REASS, "Maximum reassemblies exceeded") \
40 _(MAX_FRAG, "Maximum fragments per reassembly exceeded")\
41 _(NON_SYN, "non-SYN packet try to create session")
45 #define _(sym,str) NAT_IN2OUT_ED_ERROR_##sym,
46 foreach_nat_in2out_ed_error
48 NAT_IN2OUT_ED_N_ERROR,
49 } nat_in2out_ed_error_t;
51 static char *nat_in2out_ed_error_strings[] = {
52 #define _(sym,string) string,
53 foreach_nat_in2out_ed_error
59 NAT_IN2OUT_ED_NEXT_LOOKUP,
60 NAT_IN2OUT_ED_NEXT_DROP,
61 NAT_IN2OUT_ED_NEXT_ICMP_ERROR,
62 NAT_IN2OUT_ED_NEXT_SLOW_PATH,
63 NAT_IN2OUT_ED_NEXT_REASS,
65 } nat_in2out_ed_next_t;
73 } nat_in2out_ed_trace_t;
75 vlib_node_registration_t nat44_ed_in2out_node;
76 vlib_node_registration_t nat44_ed_in2out_slowpath_node;
77 vlib_node_registration_t nat44_ed_in2out_output_node;
78 vlib_node_registration_t nat44_ed_in2out_output_slowpath_node;
79 vlib_node_registration_t nat44_ed_in2out_reass_node;
82 format_nat_in2out_ed_trace (u8 * s, va_list * args)
84 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
85 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
86 nat_in2out_ed_trace_t *t = va_arg (*args, nat_in2out_ed_trace_t *);
90 t->is_slow_path ? "NAT44_IN2OUT_ED_SLOW_PATH" :
91 "NAT44_IN2OUT_ED_FAST_PATH";
93 s = format (s, "%s: sw_if_index %d, next index %d, session %d", tag,
94 t->sw_if_index, t->next_index, t->session_index);
99 static_always_inline int
100 icmp_get_ed_key (ip4_header_t * ip0, nat_ed_ses_key_t * p_key0)
102 icmp46_header_t *icmp0;
103 nat_ed_ses_key_t key0;
104 icmp_echo_header_t *echo0, *inner_echo0 = 0;
105 ip4_header_t *inner_ip0 = 0;
107 icmp46_header_t *inner_icmp0;
109 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
110 echo0 = (icmp_echo_header_t *) (icmp0 + 1);
112 if (!icmp_is_error_message (icmp0))
114 key0.proto = IP_PROTOCOL_ICMP;
115 key0.l_addr = ip0->src_address;
116 key0.r_addr = ip0->dst_address;
117 key0.l_port = echo0->identifier;
122 inner_ip0 = (ip4_header_t *) (echo0 + 1);
123 l4_header = ip4_next_header (inner_ip0);
124 key0.proto = inner_ip0->protocol;
125 key0.r_addr = inner_ip0->src_address;
126 key0.l_addr = inner_ip0->dst_address;
127 switch (ip_proto_to_snat_proto (inner_ip0->protocol))
129 case SNAT_PROTOCOL_ICMP:
130 inner_icmp0 = (icmp46_header_t *) l4_header;
131 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
133 key0.l_port = inner_echo0->identifier;
135 case SNAT_PROTOCOL_UDP:
136 case SNAT_PROTOCOL_TCP:
137 key0.l_port = ((tcp_udp_header_t *) l4_header)->dst_port;
138 key0.r_port = ((tcp_udp_header_t *) l4_header)->src_port;
141 return NAT_IN2OUT_ED_ERROR_UNSUPPORTED_PROTOCOL;
149 nat44_i2o_ed_is_idle_session_cb (clib_bihash_kv_16_8_t * kv, void *arg)
151 snat_main_t *sm = &snat_main;
152 nat44_is_idle_session_ctx_t *ctx = arg;
154 u64 sess_timeout_time;
155 nat_ed_ses_key_t ed_key;
156 clib_bihash_kv_16_8_t ed_kv;
159 snat_session_key_t key;
160 snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
163 s = pool_elt_at_index (tsm->sessions, kv->value);
164 sess_timeout_time = s->last_heard + (f64) nat44_session_get_timeout (sm, s);
165 if (ctx->now >= sess_timeout_time)
167 if (is_fwd_bypass_session (s))
170 ed_key.l_addr = s->out2in.addr;
171 ed_key.r_addr = s->ext_host_addr;
172 ed_key.fib_index = s->out2in.fib_index;
173 if (snat_is_unk_proto_session (s))
175 ed_key.proto = s->in2out.port;
181 ed_key.proto = snat_proto_to_ip_proto (s->in2out.protocol);
182 ed_key.l_port = s->out2in.port;
183 ed_key.r_port = s->ext_host_port;
185 ed_kv.key[0] = ed_key.as_u64[0];
186 ed_kv.key[1] = ed_key.as_u64[1];
187 if (clib_bihash_add_del_16_8 (&tsm->out2in_ed, &ed_kv, 0))
188 nat_log_warn ("out2in_ed key del failed");
190 if (snat_is_unk_proto_session (s))
193 snat_ipfix_logging_nat44_ses_delete (s->in2out.addr.as_u32,
194 s->out2in.addr.as_u32,
198 s->in2out.fib_index);
200 if (is_twice_nat_session (s))
202 for (i = 0; i < vec_len (sm->twice_nat_addresses); i++)
204 key.protocol = s->in2out.protocol;
205 key.port = s->ext_host_nat_port;
206 a = sm->twice_nat_addresses + i;
207 if (a->addr.as_u32 == s->ext_host_nat_addr.as_u32)
209 snat_free_outside_address_and_port (sm->twice_nat_addresses,
217 if (snat_is_session_static (s))
220 snat_free_outside_address_and_port (sm->addresses, ctx->thread_index,
223 nat44_delete_session (sm, s, ctx->thread_index);
231 icmp_in2out_ed_slow_path (snat_main_t * sm, vlib_buffer_t * b0,
232 ip4_header_t * ip0, icmp46_header_t * icmp0,
233 u32 sw_if_index0, u32 rx_fib_index0,
234 vlib_node_runtime_t * node, u32 next0, f64 now,
235 u32 thread_index, snat_session_t ** p_s0)
237 next0 = icmp_in2out (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
238 next0, thread_index, p_s0, 0);
239 snat_session_t *s0 = *p_s0;
240 if (PREDICT_TRUE (next0 != NAT_IN2OUT_ED_NEXT_DROP && s0))
243 nat44_session_update_counters (s0, now,
244 vlib_buffer_length_in_chain
245 (sm->vlib_main, b0));
246 /* Per-user LRU list maintenance */
247 nat44_session_update_lru (sm, s0, thread_index);
253 slow_path_ed (snat_main_t * sm,
256 clib_bihash_kv_16_8_t * kv,
257 snat_session_t ** sessionp,
258 vlib_node_runtime_t * node, u32 next, u32 thread_index, f64 now)
260 snat_session_t *s = 0;
262 snat_session_key_t key0, key1;
263 lb_nat_type_t lb = 0, is_sm = 0;
264 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
265 nat_ed_ses_key_t *key = (nat_ed_ses_key_t *) kv->key;
266 u32 proto = ip_proto_to_snat_proto (key->proto);
267 nat_outside_fib_t *outside_fib;
268 fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
271 .fp_proto = FIB_PROTOCOL_IP4,
274 .ip4.as_u32 = key->r_addr.as_u32,
277 nat44_is_idle_session_ctx_t ctx;
279 if (PREDICT_FALSE (maximum_sessions_exceeded (sm, thread_index)))
281 b->error = node->errors[NAT_IN2OUT_ED_ERROR_MAX_SESSIONS_EXCEEDED];
282 nat_ipfix_logging_max_sessions (sm->max_translations);
283 nat_log_notice ("maximum sessions exceeded");
284 return NAT_IN2OUT_ED_NEXT_DROP;
287 key0.addr = key->l_addr;
288 key0.port = key->l_port;
289 key1.protocol = key0.protocol = proto;
290 key0.fib_index = rx_fib_index;
291 key1.fib_index = sm->outside_fib_index;
292 /* First try to match static mapping by local address and port */
293 if (snat_static_mapping_match
294 (sm, key0, &key1, 0, 0, 0, &lb, 0, &identity_nat))
296 /* Try to create dynamic translation */
297 if (snat_alloc_outside_address_and_port (sm->addresses, rx_fib_index,
300 tsm->snat_thread_index))
302 nat_log_notice ("addresses exhausted");
303 b->error = node->errors[NAT_IN2OUT_ED_ERROR_OUT_OF_PORTS];
304 return NAT_IN2OUT_ED_NEXT_DROP;
309 if (PREDICT_FALSE (identity_nat))
318 u = nat_user_get_or_create (sm, &key->l_addr, rx_fib_index, thread_index);
321 nat_log_warn ("create NAT user failed");
323 snat_free_outside_address_and_port (sm->addresses,
324 thread_index, &key1);
325 return NAT_IN2OUT_ED_NEXT_DROP;
328 s = nat_ed_session_alloc (sm, u, thread_index, now);
331 nat44_delete_user_with_no_session (sm, u, thread_index);
332 nat_log_warn ("create NAT session failed");
334 snat_free_outside_address_and_port (sm->addresses,
335 thread_index, &key1);
336 return NAT_IN2OUT_ED_NEXT_DROP;
339 user_session_increment (sm, u, is_sm);
341 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
343 s->flags |= SNAT_SESSION_FLAG_LOAD_BALANCING;
344 s->flags |= SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT;
345 s->ext_host_addr = key->r_addr;
346 s->ext_host_port = key->r_port;
349 s->out2in.protocol = key0.protocol;
351 switch (vec_len (sm->outside_fibs))
354 s->out2in.fib_index = sm->outside_fib_index;
357 s->out2in.fib_index = sm->outside_fibs[0].fib_index;
361 vec_foreach (outside_fib, sm->outside_fibs)
363 fei = fib_table_lookup (outside_fib->fib_index, &pfx);
364 if (FIB_NODE_INDEX_INVALID != fei)
366 if (fib_entry_get_resolving_interface (fei) != ~0)
368 s->out2in.fib_index = outside_fib->fib_index;
377 /* Add to lookup tables */
378 kv->value = s - tsm->sessions;
380 ctx.thread_index = thread_index;
381 if (clib_bihash_add_or_overwrite_stale_16_8 (&tsm->in2out_ed, kv,
382 nat44_i2o_ed_is_idle_session_cb,
384 nat_log_notice ("in2out-ed key add failed");
386 make_ed_kv (kv, &key1.addr, &key->r_addr, key->proto, s->out2in.fib_index,
387 key1.port, key->r_port);
388 kv->value = s - tsm->sessions;
389 if (clib_bihash_add_or_overwrite_stale_16_8 (&tsm->out2in_ed, kv,
390 nat44_o2i_ed_is_idle_session_cb,
392 nat_log_notice ("out2in-ed key add failed");
397 snat_ipfix_logging_nat44_ses_create (s->in2out.addr.as_u32,
398 s->out2in.addr.as_u32,
401 s->out2in.port, s->in2out.fib_index);
405 static_always_inline int
406 nat44_ed_not_translate (snat_main_t * sm, vlib_node_runtime_t * node,
407 u32 sw_if_index, ip4_header_t * ip, u32 proto,
408 u32 rx_fib_index, u32 thread_index)
410 udp_header_t *udp = ip4_next_header (ip);
411 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
412 clib_bihash_kv_16_8_t kv, value;
413 snat_session_key_t key0, key1;
415 make_ed_kv (&kv, &ip->dst_address, &ip->src_address, ip->protocol,
416 sm->outside_fib_index, udp->dst_port, udp->src_port);
418 /* NAT packet aimed at external address if */
419 /* has active sessions */
420 if (clib_bihash_search_16_8 (&tsm->out2in_ed, &kv, &value))
422 key0.addr = ip->dst_address;
423 key0.port = udp->dst_port;
424 key0.protocol = proto;
425 key0.fib_index = sm->outside_fib_index;
426 /* or is static mappings */
427 if (!snat_static_mapping_match (sm, key0, &key1, 1, 0, 0, 0, 0, 0))
433 if (sm->forwarding_enabled)
436 return snat_not_translate_fast (sm, node, sw_if_index, ip, proto,
440 static_always_inline int
441 nat_not_translate_output_feature_fwd (snat_main_t * sm, ip4_header_t * ip,
442 u32 thread_index, f64 now,
443 vlib_main_t * vm, vlib_buffer_t * b)
445 nat_ed_ses_key_t key;
446 clib_bihash_kv_16_8_t kv, value;
448 snat_session_t *s = 0;
449 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
451 if (!sm->forwarding_enabled)
454 if (ip->protocol == IP_PROTOCOL_ICMP)
456 key.as_u64[0] = key.as_u64[1] = 0;
457 if (icmp_get_ed_key (ip, &key))
460 kv.key[0] = key.as_u64[0];
461 kv.key[1] = key.as_u64[1];
463 else if (ip->protocol == IP_PROTOCOL_UDP || ip->protocol == IP_PROTOCOL_TCP)
465 udp = ip4_next_header (ip);
466 make_ed_kv (&kv, &ip->src_address, &ip->dst_address, ip->protocol, 0,
467 udp->src_port, udp->dst_port);
471 make_ed_kv (&kv, &ip->src_address, &ip->dst_address, ip->protocol, 0, 0,
475 if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
477 s = pool_elt_at_index (tsm->sessions, value.value);
478 if (is_fwd_bypass_session (s))
480 if (ip->protocol == IP_PROTOCOL_TCP)
482 tcp_header_t *tcp = ip4_next_header (ip);
483 if (nat44_set_tcp_session_state_i2o (sm, s, tcp, thread_index))
487 nat44_session_update_counters (s, now,
488 vlib_buffer_length_in_chain (vm, b));
489 /* Per-user LRU list maintenance */
490 nat44_session_update_lru (sm, s, thread_index);
500 static_always_inline int
501 nat44_ed_not_translate_output_feature (snat_main_t * sm, ip4_header_t * ip,
502 u8 proto, u16 src_port, u16 dst_port,
503 u32 thread_index, u32 rx_sw_if_index,
506 clib_bihash_kv_16_8_t kv, value;
507 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
510 u32 rx_fib_index = ip4_fib_table_get_index_for_sw_if_index (rx_sw_if_index);
511 u32 tx_fib_index = ip4_fib_table_get_index_for_sw_if_index (tx_sw_if_index);
514 make_ed_kv (&kv, &ip->src_address, &ip->dst_address, proto, tx_fib_index,
516 if (!clib_bihash_search_16_8 (&tsm->out2in_ed, &kv, &value))
518 s = pool_elt_at_index (tsm->sessions, value.value);
519 if (nat44_is_ses_closed (s))
521 nat_log_debug ("TCP close connection %U", format_snat_session,
522 &sm->per_thread_data[thread_index], s);
523 nat_free_session_data (sm, s, thread_index);
524 nat44_delete_session (sm, s, thread_index);
527 s->flags |= SNAT_SESSION_FLAG_OUTPUT_FEATURE;
532 make_ed_kv (&kv, &ip->dst_address, &ip->src_address, proto, rx_fib_index,
534 if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
536 s = pool_elt_at_index (tsm->sessions, value.value);
537 if (is_fwd_bypass_session (s))
542 pool_foreach (i, sm->output_feature_interfaces,
544 if ((nat_interface_is_inside (i)) && (rx_sw_if_index == i->sw_if_index))
555 icmp_match_in2out_ed (snat_main_t * sm, vlib_node_runtime_t * node,
556 u32 thread_index, vlib_buffer_t * b, ip4_header_t * ip,
557 u8 * p_proto, snat_session_key_t * p_value,
558 u8 * p_dont_translate, void *d, void *e)
560 icmp46_header_t *icmp;
563 nat_ed_ses_key_t key;
564 snat_session_t *s = 0;
565 u8 dont_translate = 0;
566 clib_bihash_kv_16_8_t kv, value;
569 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
571 icmp = (icmp46_header_t *) ip4_next_header (ip);
572 sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
573 rx_fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
575 key.as_u64[0] = key.as_u64[1] = 0;
576 err = icmp_get_ed_key (ip, &key);
579 b->error = node->errors[err];
580 next = NAT_IN2OUT_ED_NEXT_DROP;
583 key.fib_index = rx_fib_index;
585 kv.key[0] = key.as_u64[0];
586 kv.key[1] = key.as_u64[1];
588 if (clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
590 if (vnet_buffer (b)->sw_if_index[VLIB_TX] != ~0)
592 if (PREDICT_FALSE (nat44_ed_not_translate_output_feature (sm, ip,
611 if (PREDICT_FALSE (nat44_ed_not_translate (sm, node, sw_if_index,
612 ip, SNAT_PROTOCOL_ICMP,
621 if (PREDICT_FALSE (icmp_is_error_message (icmp)))
623 b->error = node->errors[NAT_IN2OUT_ED_ERROR_BAD_ICMP_TYPE];
624 next = NAT_IN2OUT_ED_NEXT_DROP;
628 next = slow_path_ed (sm, b, rx_fib_index, &kv, &s, node, next,
629 thread_index, vlib_time_now (sm->vlib_main));
631 if (PREDICT_FALSE (next == NAT_IN2OUT_ED_NEXT_DROP))
642 if (PREDICT_FALSE (icmp->type != ICMP4_echo_request &&
643 icmp->type != ICMP4_echo_reply &&
644 !icmp_is_error_message (icmp)))
646 b->error = node->errors[NAT_IN2OUT_ED_ERROR_BAD_ICMP_TYPE];
647 next = NAT_IN2OUT_ED_NEXT_DROP;
651 s = pool_elt_at_index (tsm->sessions, value.value);
654 *p_proto = ip_proto_to_snat_proto (key.proto);
657 *p_value = s->out2in;
658 *p_dont_translate = dont_translate;
660 *(snat_session_t **) d = s;
664 static snat_session_t *
665 nat44_ed_in2out_unknown_proto (snat_main_t * sm,
671 vlib_main_t * vm, vlib_node_runtime_t * node)
673 clib_bihash_kv_8_8_t kv, value;
674 clib_bihash_kv_16_8_t s_kv, s_value;
675 snat_static_mapping_t *m;
676 u32 old_addr, new_addr = 0;
679 dlist_elt_t *head, *elt;
680 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
681 u32 elt_index, head_index, ses_index;
683 u32 outside_fib_index = sm->outside_fib_index;
686 nat_outside_fib_t *outside_fib;
687 fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
689 .fp_proto = FIB_PROTOCOL_IP4,
692 .ip4.as_u32 = ip->dst_address.as_u32,
696 switch (vec_len (sm->outside_fibs))
699 outside_fib_index = sm->outside_fib_index;
702 outside_fib_index = sm->outside_fibs[0].fib_index;
706 vec_foreach (outside_fib, sm->outside_fibs)
708 fei = fib_table_lookup (outside_fib->fib_index, &pfx);
709 if (FIB_NODE_INDEX_INVALID != fei)
711 if (fib_entry_get_resolving_interface (fei) != ~0)
713 outside_fib_index = outside_fib->fib_index;
721 old_addr = ip->src_address.as_u32;
723 make_ed_kv (&s_kv, &ip->src_address, &ip->dst_address, ip->protocol,
726 if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &s_kv, &s_value))
728 s = pool_elt_at_index (tsm->sessions, s_value.value);
729 new_addr = ip->src_address.as_u32 = s->out2in.addr.as_u32;
733 if (PREDICT_FALSE (maximum_sessions_exceeded (sm, thread_index)))
735 b->error = node->errors[NAT_IN2OUT_ED_ERROR_MAX_SESSIONS_EXCEEDED];
736 nat_ipfix_logging_max_sessions (sm->max_translations);
737 nat_log_notice ("maximum sessions exceeded");
741 u = nat_user_get_or_create (sm, &ip->src_address, rx_fib_index,
745 nat_log_warn ("create NAT user failed");
749 make_sm_kv (&kv, &ip->src_address, 0, rx_fib_index, 0);
751 /* Try to find static mapping first */
752 if (!clib_bihash_search_8_8 (&sm->static_mapping_by_local, &kv, &value))
754 m = pool_elt_at_index (sm->static_mappings, value.value);
755 new_addr = ip->src_address.as_u32 = m->external_addr.as_u32;
759 /* Fallback to 3-tuple key */
762 /* Choose same out address as for TCP/UDP session to same destination */
763 head_index = u->sessions_per_user_list_head_index;
764 head = pool_elt_at_index (tsm->list_pool, head_index);
765 elt_index = head->next;
766 if (PREDICT_FALSE (elt_index == ~0))
770 elt = pool_elt_at_index (tsm->list_pool, elt_index);
771 ses_index = elt->value;
774 while (ses_index != ~0)
776 s = pool_elt_at_index (tsm->sessions, ses_index);
777 elt_index = elt->next;
778 elt = pool_elt_at_index (tsm->list_pool, elt_index);
779 ses_index = elt->value;
781 if (s->ext_host_addr.as_u32 == ip->dst_address.as_u32)
783 new_addr = ip->src_address.as_u32 = s->out2in.addr.as_u32;
785 make_ed_kv (&s_kv, &s->out2in.addr, &ip->dst_address,
786 ip->protocol, outside_fib_index, 0, 0);
787 if (clib_bihash_search_16_8
788 (&tsm->out2in_ed, &s_kv, &s_value))
795 for (i = 0; i < vec_len (sm->addresses); i++)
797 make_ed_kv (&s_kv, &sm->addresses[i].addr, &ip->dst_address,
798 ip->protocol, outside_fib_index, 0, 0);
799 if (clib_bihash_search_16_8 (&tsm->out2in_ed, &s_kv, &s_value))
801 new_addr = ip->src_address.as_u32 =
802 sm->addresses[i].addr.as_u32;
810 s = nat_ed_session_alloc (sm, u, thread_index, now);
813 nat44_delete_user_with_no_session (sm, u, thread_index);
814 nat_log_warn ("create NAT session failed");
818 s->ext_host_addr.as_u32 = ip->dst_address.as_u32;
819 s->flags |= SNAT_SESSION_FLAG_UNKNOWN_PROTO;
820 s->flags |= SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT;
821 s->out2in.addr.as_u32 = new_addr;
822 s->out2in.fib_index = outside_fib_index;
823 s->in2out.addr.as_u32 = old_addr;
824 s->in2out.fib_index = rx_fib_index;
825 s->in2out.port = s->out2in.port = ip->protocol;
827 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
828 user_session_increment (sm, u, is_sm);
830 /* Add to lookup tables */
831 make_ed_kv (&s_kv, &s->in2out.addr, &ip->dst_address, ip->protocol,
833 s_kv.value = s - tsm->sessions;
834 if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &s_kv, 1))
835 nat_log_notice ("in2out key add failed");
837 make_ed_kv (&s_kv, &s->out2in.addr, &ip->dst_address, ip->protocol,
838 outside_fib_index, 0, 0);
839 s_kv.value = s - tsm->sessions;
840 if (clib_bihash_add_del_16_8 (&tsm->out2in_ed, &s_kv, 1))
841 nat_log_notice ("out2in key add failed");
844 /* Update IP checksum */
846 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, src_address);
847 ip->checksum = ip_csum_fold (sum);
850 nat44_session_update_counters (s, now, vlib_buffer_length_in_chain (vm, b));
851 /* Per-user LRU list maintenance */
852 nat44_session_update_lru (sm, s, thread_index);
855 if (vnet_buffer (b)->sw_if_index[VLIB_TX] == ~0)
856 nat44_ed_hairpinning_unknown_proto (sm, b, ip);
858 if (vnet_buffer (b)->sw_if_index[VLIB_TX] == ~0)
859 vnet_buffer (b)->sw_if_index[VLIB_TX] = outside_fib_index;
865 nat44_ed_in2out_node_fn_inline (vlib_main_t * vm,
866 vlib_node_runtime_t * node,
867 vlib_frame_t * frame, int is_slow_path,
868 int is_output_feature)
870 u32 n_left_from, *from, *to_next, pkts_processed = 0, stats_node_index;
871 nat_in2out_ed_next_t next_index;
872 snat_main_t *sm = &snat_main;
873 f64 now = vlib_time_now (vm);
874 u32 thread_index = vm->thread_index;
875 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
877 stats_node_index = is_slow_path ? nat44_ed_in2out_slowpath_node.index :
878 nat44_ed_in2out_node.index;
880 from = vlib_frame_vector_args (frame);
881 n_left_from = frame->n_vectors;
882 next_index = node->cached_next_index;
884 while (n_left_from > 0)
888 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
890 while (n_left_from >= 4 && n_left_to_next >= 2)
893 vlib_buffer_t *b0, *b1;
894 u32 next0, sw_if_index0, rx_fib_index0, iph_offset0 = 0, proto0,
895 new_addr0, old_addr0;
896 u32 next1, sw_if_index1, rx_fib_index1, iph_offset1 = 0, proto1,
897 new_addr1, old_addr1;
898 u16 old_port0, new_port0, old_port1, new_port1;
899 ip4_header_t *ip0, *ip1;
900 udp_header_t *udp0, *udp1;
901 tcp_header_t *tcp0, *tcp1;
902 icmp46_header_t *icmp0, *icmp1;
903 snat_session_t *s0 = 0, *s1 = 0;
904 clib_bihash_kv_16_8_t kv0, value0, kv1, value1;
905 ip_csum_t sum0, sum1;
907 /* Prefetch next iteration. */
909 vlib_buffer_t *p2, *p3;
911 p2 = vlib_get_buffer (vm, from[2]);
912 p3 = vlib_get_buffer (vm, from[3]);
914 vlib_prefetch_buffer_header (p2, LOAD);
915 vlib_prefetch_buffer_header (p3, LOAD);
917 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
918 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
921 /* speculatively enqueue b0 and b1 to the current next frame */
922 to_next[0] = bi0 = from[0];
923 to_next[1] = bi1 = from[1];
929 b0 = vlib_get_buffer (vm, bi0);
930 b1 = vlib_get_buffer (vm, bi1);
932 next0 = NAT_IN2OUT_ED_NEXT_LOOKUP;
934 if (is_output_feature)
935 iph_offset0 = vnet_buffer (b0)->ip.save_rewrite_length;
937 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
940 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
942 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
945 if (PREDICT_FALSE (ip0->ttl == 1))
947 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
948 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
949 ICMP4_time_exceeded_ttl_exceeded_in_transit,
951 next0 = NAT_IN2OUT_ED_NEXT_ICMP_ERROR;
955 udp0 = ip4_next_header (ip0);
956 tcp0 = (tcp_header_t *) udp0;
957 icmp0 = (icmp46_header_t *) udp0;
958 proto0 = ip_proto_to_snat_proto (ip0->protocol);
962 if (PREDICT_FALSE (proto0 == ~0))
964 s0 = nat44_ed_in2out_unknown_proto (sm, b0, ip0,
966 thread_index, now, vm,
969 next0 = NAT_IN2OUT_ED_NEXT_DROP;
973 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
975 next0 = icmp_in2out_ed_slow_path
976 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
977 next0, now, thread_index, &s0);
983 if (PREDICT_FALSE (proto0 == ~0))
985 next0 = NAT_IN2OUT_ED_NEXT_SLOW_PATH;
989 if (ip4_is_fragment (ip0))
991 next0 = NAT_IN2OUT_ED_NEXT_REASS;
995 if (is_output_feature)
998 (nat_not_translate_output_feature_fwd
999 (sm, ip0, thread_index, now, vm, b0)))
1003 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1005 next0 = NAT_IN2OUT_ED_NEXT_SLOW_PATH;
1010 make_ed_kv (&kv0, &ip0->src_address, &ip0->dst_address,
1011 ip0->protocol, rx_fib_index0, udp0->src_port,
1014 if (clib_bihash_search_16_8 (&tsm->in2out_ed, &kv0, &value0))
1018 if (is_output_feature)
1021 (nat44_ed_not_translate_output_feature
1022 (sm, ip0, ip0->protocol, udp0->src_port,
1023 udp0->dst_port, thread_index, sw_if_index0,
1024 vnet_buffer (b0)->sw_if_index[VLIB_TX])))
1029 if (PREDICT_FALSE (nat44_ed_not_translate (sm, node,
1037 if ((proto0 == SNAT_PROTOCOL_TCP) && !tcp_is_init (tcp0))
1039 b0->error = node->errors[NAT_IN2OUT_ED_ERROR_NON_SYN];
1040 next0 = NAT_IN2OUT_ED_NEXT_DROP;
1045 slow_path_ed (sm, b0, rx_fib_index0, &kv0, &s0, node,
1046 next0, thread_index, now);
1048 if (PREDICT_FALSE (next0 == NAT_IN2OUT_ED_NEXT_DROP))
1051 if (PREDICT_FALSE (!s0))
1056 next0 = NAT_IN2OUT_ED_NEXT_SLOW_PATH;
1062 s0 = pool_elt_at_index (tsm->sessions, value0.value);
1065 b0->flags |= VNET_BUFFER_F_IS_NATED;
1067 if (!is_output_feature)
1068 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1070 old_addr0 = ip0->src_address.as_u32;
1071 new_addr0 = ip0->src_address.as_u32 = s0->out2in.addr.as_u32;
1072 sum0 = ip0->checksum;
1073 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1075 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1076 sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
1077 s0->ext_host_addr.as_u32, ip4_header_t,
1079 ip0->checksum = ip_csum_fold (sum0);
1081 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1083 old_port0 = tcp0->src_port;
1084 new_port0 = tcp0->src_port = s0->out2in.port;
1086 sum0 = tcp0->checksum;
1087 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1089 sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
1091 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1093 sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
1094 s0->ext_host_addr.as_u32,
1095 ip4_header_t, dst_address);
1096 sum0 = ip_csum_update (sum0, tcp0->dst_port,
1097 s0->ext_host_port, ip4_header_t,
1099 tcp0->dst_port = s0->ext_host_port;
1100 ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
1102 mss_clamping (sm, tcp0, &sum0);
1103 tcp0->checksum = ip_csum_fold (sum0);
1104 if (nat44_set_tcp_session_state_i2o
1105 (sm, s0, tcp0, thread_index))
1110 udp0->src_port = s0->out2in.port;
1112 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1114 udp0->dst_port = s0->ext_host_port;
1115 ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
1120 nat44_session_update_counters (s0, now,
1121 vlib_buffer_length_in_chain (vm,
1123 /* Per-user LRU list maintenance */
1124 nat44_session_update_lru (sm, s0, thread_index);
1127 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1128 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1130 nat_in2out_ed_trace_t *t =
1131 vlib_add_trace (vm, node, b0, sizeof (*t));
1132 t->is_slow_path = is_slow_path;
1133 t->sw_if_index = sw_if_index0;
1134 t->next_index = next0;
1135 t->session_index = ~0;
1137 t->session_index = s0 - tsm->sessions;
1140 pkts_processed += next0 != NAT_IN2OUT_ED_NEXT_DROP;
1143 next1 = NAT_IN2OUT_ED_NEXT_LOOKUP;
1145 if (is_output_feature)
1146 iph_offset1 = vnet_buffer (b1)->ip.save_rewrite_length;
1148 ip1 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b1) +
1151 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
1153 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1156 if (PREDICT_FALSE (ip1->ttl == 1))
1158 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1159 icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
1160 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1162 next1 = NAT_IN2OUT_ED_NEXT_ICMP_ERROR;
1166 udp1 = ip4_next_header (ip1);
1167 tcp1 = (tcp_header_t *) udp1;
1168 icmp1 = (icmp46_header_t *) udp1;
1169 proto1 = ip_proto_to_snat_proto (ip1->protocol);
1173 if (PREDICT_FALSE (proto1 == ~0))
1175 s1 = nat44_ed_in2out_unknown_proto (sm, b1, ip1,
1177 thread_index, now, vm,
1180 next1 = NAT_IN2OUT_ED_NEXT_DROP;
1184 if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
1186 next1 = icmp_in2out_ed_slow_path
1187 (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node,
1188 next1, now, thread_index, &s1);
1194 if (PREDICT_FALSE (proto1 == ~0))
1196 next1 = NAT_IN2OUT_ED_NEXT_SLOW_PATH;
1200 if (ip4_is_fragment (ip1))
1202 next1 = NAT_IN2OUT_ED_NEXT_REASS;
1206 if (is_output_feature)
1209 (nat_not_translate_output_feature_fwd
1210 (sm, ip1, thread_index, now, vm, b1)))
1214 if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
1216 next1 = NAT_IN2OUT_ED_NEXT_SLOW_PATH;
1221 make_ed_kv (&kv1, &ip1->src_address, &ip1->dst_address,
1222 ip1->protocol, rx_fib_index1, udp1->src_port,
1225 if (clib_bihash_search_16_8 (&tsm->in2out_ed, &kv1, &value1))
1229 if (is_output_feature)
1232 (nat44_ed_not_translate_output_feature
1233 (sm, ip1, ip1->protocol, udp1->src_port,
1234 udp1->dst_port, thread_index, sw_if_index1,
1235 vnet_buffer (b1)->sw_if_index[VLIB_TX])))
1240 if (PREDICT_FALSE (nat44_ed_not_translate (sm, node,
1248 if ((proto1 == SNAT_PROTOCOL_TCP) && !tcp_is_init (tcp1))
1250 b1->error = node->errors[NAT_IN2OUT_ED_ERROR_NON_SYN];
1251 next1 = NAT_IN2OUT_ED_NEXT_DROP;
1256 slow_path_ed (sm, b1, rx_fib_index1, &kv1, &s1, node,
1257 next1, thread_index, now);
1259 if (PREDICT_FALSE (next1 == NAT_IN2OUT_ED_NEXT_DROP))
1262 if (PREDICT_FALSE (!s1))
1267 next1 = NAT_IN2OUT_ED_NEXT_SLOW_PATH;
1273 s1 = pool_elt_at_index (tsm->sessions, value1.value);
1276 b1->flags |= VNET_BUFFER_F_IS_NATED;
1278 if (!is_output_feature)
1279 vnet_buffer (b1)->sw_if_index[VLIB_TX] = s1->out2in.fib_index;
1281 old_addr1 = ip1->src_address.as_u32;
1282 new_addr1 = ip1->src_address.as_u32 = s1->out2in.addr.as_u32;
1283 sum1 = ip1->checksum;
1284 sum1 = ip_csum_update (sum1, old_addr1, new_addr1, ip4_header_t,
1286 if (PREDICT_FALSE (is_twice_nat_session (s1)))
1287 sum1 = ip_csum_update (sum1, ip1->dst_address.as_u32,
1288 s1->ext_host_addr.as_u32, ip4_header_t,
1290 ip1->checksum = ip_csum_fold (sum1);
1292 if (PREDICT_TRUE (proto1 == SNAT_PROTOCOL_TCP))
1294 old_port1 = tcp1->src_port;
1295 new_port1 = tcp1->src_port = s1->out2in.port;
1297 sum1 = tcp1->checksum;
1298 sum1 = ip_csum_update (sum1, old_addr1, new_addr1, ip4_header_t,
1300 sum1 = ip_csum_update (sum1, old_port1, new_port1, ip4_header_t,
1302 if (PREDICT_FALSE (is_twice_nat_session (s1)))
1304 sum1 = ip_csum_update (sum1, ip1->dst_address.as_u32,
1305 s1->ext_host_addr.as_u32,
1306 ip4_header_t, dst_address);
1307 sum1 = ip_csum_update (sum1, tcp1->dst_port,
1308 s1->ext_host_port, ip4_header_t,
1310 tcp1->dst_port = s1->ext_host_port;
1311 ip1->dst_address.as_u32 = s1->ext_host_addr.as_u32;
1313 tcp1->checksum = ip_csum_fold (sum1);
1314 mss_clamping (sm, tcp1, &sum1);
1315 if (nat44_set_tcp_session_state_i2o
1316 (sm, s1, tcp1, thread_index))
1321 udp1->src_port = s1->out2in.port;
1323 if (PREDICT_FALSE (is_twice_nat_session (s1)))
1325 udp1->dst_port = s1->ext_host_port;
1326 ip1->dst_address.as_u32 = s1->ext_host_addr.as_u32;
1331 nat44_session_update_counters (s1, now,
1332 vlib_buffer_length_in_chain (vm,
1334 /* Per-user LRU list maintenance */
1335 nat44_session_update_lru (sm, s1, thread_index);
1338 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1339 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
1341 nat_in2out_ed_trace_t *t =
1342 vlib_add_trace (vm, node, b1, sizeof (*t));
1343 t->is_slow_path = is_slow_path;
1344 t->sw_if_index = sw_if_index1;
1345 t->next_index = next1;
1346 t->session_index = ~0;
1348 t->session_index = s1 - tsm->sessions;
1351 pkts_processed += next1 != NAT_IN2OUT_ED_NEXT_DROP;
1353 /* verify speculative enqueues, maybe switch current next frame */
1354 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1355 to_next, n_left_to_next,
1356 bi0, bi1, next0, next1);
1359 while (n_left_from > 0 && n_left_to_next > 0)
1363 u32 next0, sw_if_index0, rx_fib_index0, iph_offset0 = 0, proto0,
1364 new_addr0, old_addr0;
1365 u16 old_port0, new_port0;
1369 icmp46_header_t *icmp0;
1370 snat_session_t *s0 = 0;
1371 clib_bihash_kv_16_8_t kv0, value0;
1374 /* speculatively enqueue b0 to the current next frame */
1380 n_left_to_next -= 1;
1382 b0 = vlib_get_buffer (vm, bi0);
1383 next0 = NAT_IN2OUT_ED_NEXT_LOOKUP;
1385 if (is_output_feature)
1386 iph_offset0 = vnet_buffer (b0)->ip.save_rewrite_length;
1388 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
1391 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1393 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1396 if (PREDICT_FALSE (ip0->ttl == 1))
1398 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1399 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1400 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1402 next0 = NAT_IN2OUT_ED_NEXT_ICMP_ERROR;
1406 udp0 = ip4_next_header (ip0);
1407 tcp0 = (tcp_header_t *) udp0;
1408 icmp0 = (icmp46_header_t *) udp0;
1409 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1413 if (PREDICT_FALSE (proto0 == ~0))
1415 s0 = nat44_ed_in2out_unknown_proto (sm, b0, ip0,
1417 thread_index, now, vm,
1420 next0 = NAT_IN2OUT_ED_NEXT_DROP;
1424 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1426 next0 = icmp_in2out_ed_slow_path
1427 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1428 next0, now, thread_index, &s0);
1434 if (PREDICT_FALSE (proto0 == ~0))
1436 next0 = NAT_IN2OUT_ED_NEXT_SLOW_PATH;
1440 if (ip4_is_fragment (ip0))
1442 next0 = NAT_IN2OUT_ED_NEXT_REASS;
1446 if (is_output_feature)
1449 (nat_not_translate_output_feature_fwd
1450 (sm, ip0, thread_index, now, vm, b0)))
1454 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1456 next0 = NAT_IN2OUT_ED_NEXT_SLOW_PATH;
1461 make_ed_kv (&kv0, &ip0->src_address, &ip0->dst_address,
1462 ip0->protocol, rx_fib_index0, udp0->src_port,
1465 if (clib_bihash_search_16_8 (&tsm->in2out_ed, &kv0, &value0))
1469 if (is_output_feature)
1472 (nat44_ed_not_translate_output_feature
1473 (sm, ip0, ip0->protocol, udp0->src_port,
1474 udp0->dst_port, thread_index, sw_if_index0,
1475 vnet_buffer (b0)->sw_if_index[VLIB_TX])))
1480 if (PREDICT_FALSE (nat44_ed_not_translate (sm, node,
1488 if ((proto0 == SNAT_PROTOCOL_TCP) && !tcp_is_init (tcp0))
1490 b0->error = node->errors[NAT_IN2OUT_ED_ERROR_NON_SYN];
1491 next0 = NAT_IN2OUT_ED_NEXT_DROP;
1496 slow_path_ed (sm, b0, rx_fib_index0, &kv0, &s0, node,
1497 next0, thread_index, now);
1499 if (PREDICT_FALSE (next0 == NAT_IN2OUT_ED_NEXT_DROP))
1502 if (PREDICT_FALSE (!s0))
1507 next0 = NAT_IN2OUT_ED_NEXT_SLOW_PATH;
1513 s0 = pool_elt_at_index (tsm->sessions, value0.value);
1516 b0->flags |= VNET_BUFFER_F_IS_NATED;
1518 if (!is_output_feature)
1519 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1521 old_addr0 = ip0->src_address.as_u32;
1522 new_addr0 = ip0->src_address.as_u32 = s0->out2in.addr.as_u32;
1523 sum0 = ip0->checksum;
1524 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1526 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1527 sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
1528 s0->ext_host_addr.as_u32, ip4_header_t,
1530 ip0->checksum = ip_csum_fold (sum0);
1532 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1534 old_port0 = tcp0->src_port;
1535 new_port0 = tcp0->src_port = s0->out2in.port;
1537 sum0 = tcp0->checksum;
1538 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1540 sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
1542 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1544 sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
1545 s0->ext_host_addr.as_u32,
1546 ip4_header_t, dst_address);
1547 sum0 = ip_csum_update (sum0, tcp0->dst_port,
1548 s0->ext_host_port, ip4_header_t,
1550 tcp0->dst_port = s0->ext_host_port;
1551 ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
1553 mss_clamping (sm, tcp0, &sum0);
1554 tcp0->checksum = ip_csum_fold (sum0);
1555 if (nat44_set_tcp_session_state_i2o
1556 (sm, s0, tcp0, thread_index))
1561 udp0->src_port = s0->out2in.port;
1563 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1565 udp0->dst_port = s0->ext_host_port;
1566 ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
1571 nat44_session_update_counters (s0, now,
1572 vlib_buffer_length_in_chain (vm,
1574 /* Per-user LRU list maintenance */
1575 nat44_session_update_lru (sm, s0, thread_index);
1578 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1579 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1581 nat_in2out_ed_trace_t *t =
1582 vlib_add_trace (vm, node, b0, sizeof (*t));
1583 t->is_slow_path = is_slow_path;
1584 t->sw_if_index = sw_if_index0;
1585 t->next_index = next0;
1586 t->session_index = ~0;
1588 t->session_index = s0 - tsm->sessions;
1591 pkts_processed += next0 != NAT_IN2OUT_ED_NEXT_DROP;
1593 /* verify speculative enqueue, maybe switch current next frame */
1594 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1595 to_next, n_left_to_next,
1599 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1602 vlib_node_increment_counter (vm, stats_node_index,
1603 NAT_IN2OUT_ED_ERROR_IN2OUT_PACKETS,
1605 return frame->n_vectors;
1609 nat44_ed_in2out_fast_path_fn (vlib_main_t * vm,
1610 vlib_node_runtime_t * node,
1611 vlib_frame_t * frame)
1613 return nat44_ed_in2out_node_fn_inline (vm, node, frame, 0, 0);
1617 VLIB_REGISTER_NODE (nat44_ed_in2out_node) = {
1618 .function = nat44_ed_in2out_fast_path_fn,
1619 .name = "nat44-ed-in2out",
1620 .vector_size = sizeof (u32),
1621 .format_trace = format_nat_in2out_ed_trace,
1622 .type = VLIB_NODE_TYPE_INTERNAL,
1623 .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
1624 .error_strings = nat_in2out_ed_error_strings,
1625 .runtime_data_bytes = sizeof (snat_runtime_t),
1626 .n_next_nodes = NAT_IN2OUT_ED_N_NEXT,
1628 [NAT_IN2OUT_ED_NEXT_DROP] = "error-drop",
1629 [NAT_IN2OUT_ED_NEXT_LOOKUP] = "ip4-lookup",
1630 [NAT_IN2OUT_ED_NEXT_SLOW_PATH] = "nat44-ed-in2out-slowpath",
1631 [NAT_IN2OUT_ED_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1632 [NAT_IN2OUT_ED_NEXT_REASS] = "nat44-ed-in2out-reass",
1637 VLIB_NODE_FUNCTION_MULTIARCH (nat44_ed_in2out_node,
1638 nat44_ed_in2out_fast_path_fn);
1641 nat44_ed_in2out_output_fast_path_fn (vlib_main_t * vm,
1642 vlib_node_runtime_t * node,
1643 vlib_frame_t * frame)
1645 return nat44_ed_in2out_node_fn_inline (vm, node, frame, 0, 1);
1649 VLIB_REGISTER_NODE (nat44_ed_in2out_output_node) = {
1650 .function = nat44_ed_in2out_output_fast_path_fn,
1651 .name = "nat44-ed-in2out-output",
1652 .vector_size = sizeof (u32),
1653 .format_trace = format_nat_in2out_ed_trace,
1654 .type = VLIB_NODE_TYPE_INTERNAL,
1655 .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
1656 .error_strings = nat_in2out_ed_error_strings,
1657 .runtime_data_bytes = sizeof (snat_runtime_t),
1658 .n_next_nodes = NAT_IN2OUT_ED_N_NEXT,
1660 [NAT_IN2OUT_ED_NEXT_DROP] = "error-drop",
1661 [NAT_IN2OUT_ED_NEXT_LOOKUP] = "interface-output",
1662 [NAT_IN2OUT_ED_NEXT_SLOW_PATH] = "nat44-ed-in2out-output-slowpath",
1663 [NAT_IN2OUT_ED_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1664 [NAT_IN2OUT_ED_NEXT_REASS] = "nat44-ed-in2out-reass-output",
1669 VLIB_NODE_FUNCTION_MULTIARCH (nat44_ed_in2out_output_node,
1670 nat44_ed_in2out_output_fast_path_fn);
1673 nat44_ed_in2out_slow_path_fn (vlib_main_t * vm,
1674 vlib_node_runtime_t * node,
1675 vlib_frame_t * frame)
1677 return nat44_ed_in2out_node_fn_inline (vm, node, frame, 1, 0);
1681 VLIB_REGISTER_NODE (nat44_ed_in2out_slowpath_node) = {
1682 .function = nat44_ed_in2out_slow_path_fn,
1683 .name = "nat44-ed-in2out-slowpath",
1684 .vector_size = sizeof (u32),
1685 .format_trace = format_nat_in2out_ed_trace,
1686 .type = VLIB_NODE_TYPE_INTERNAL,
1687 .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
1688 .error_strings = nat_in2out_ed_error_strings,
1689 .runtime_data_bytes = sizeof (snat_runtime_t),
1690 .n_next_nodes = NAT_IN2OUT_ED_N_NEXT,
1692 [NAT_IN2OUT_ED_NEXT_DROP] = "error-drop",
1693 [NAT_IN2OUT_ED_NEXT_LOOKUP] = "ip4-lookup",
1694 [NAT_IN2OUT_ED_NEXT_SLOW_PATH] = "nat44-ed-in2out-slowpath",
1695 [NAT_IN2OUT_ED_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1696 [NAT_IN2OUT_ED_NEXT_REASS] = "nat44-ed-in2out-reass",
1701 VLIB_NODE_FUNCTION_MULTIARCH (nat44_ed_in2out_slowpath_node,
1702 nat44_ed_in2out_slow_path_fn);
1705 nat44_ed_in2out_output_slow_path_fn (vlib_main_t * vm,
1706 vlib_node_runtime_t * node,
1707 vlib_frame_t * frame)
1709 return nat44_ed_in2out_node_fn_inline (vm, node, frame, 1, 1);
1713 VLIB_REGISTER_NODE (nat44_ed_in2out_output_slowpath_node) = {
1714 .function = nat44_ed_in2out_output_slow_path_fn,
1715 .name = "nat44-ed-in2out-output-slowpath",
1716 .vector_size = sizeof (u32),
1717 .format_trace = format_nat_in2out_ed_trace,
1718 .type = VLIB_NODE_TYPE_INTERNAL,
1719 .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
1720 .error_strings = nat_in2out_ed_error_strings,
1721 .runtime_data_bytes = sizeof (snat_runtime_t),
1722 .n_next_nodes = NAT_IN2OUT_ED_N_NEXT,
1724 [NAT_IN2OUT_ED_NEXT_DROP] = "error-drop",
1725 [NAT_IN2OUT_ED_NEXT_LOOKUP] = "interface-output",
1726 [NAT_IN2OUT_ED_NEXT_SLOW_PATH] = "nat44-ed-in2out-output-slowpath",
1727 [NAT_IN2OUT_ED_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1728 [NAT_IN2OUT_ED_NEXT_REASS] = "nat44-ed-in2out-reass",
1733 VLIB_NODE_FUNCTION_MULTIARCH (nat44_ed_in2out_output_slowpath_node,
1734 nat44_ed_in2out_output_slow_path_fn);
1737 nat44_ed_in2out_reass_node_fn_inline (vlib_main_t * vm,
1738 vlib_node_runtime_t * node,
1739 vlib_frame_t * frame,
1740 int is_output_feature)
1742 u32 n_left_from, *from, *to_next;
1743 nat_in2out_ed_next_t next_index;
1744 u32 pkts_processed = 0;
1745 snat_main_t *sm = &snat_main;
1746 f64 now = vlib_time_now (vm);
1747 u32 thread_index = vm->thread_index;
1748 snat_main_per_thread_data_t *per_thread_data =
1749 &sm->per_thread_data[thread_index];
1750 u32 *fragments_to_drop = 0;
1751 u32 *fragments_to_loopback = 0;
1753 from = vlib_frame_vector_args (frame);
1754 n_left_from = frame->n_vectors;
1755 next_index = node->cached_next_index;
1757 while (n_left_from > 0)
1761 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1763 while (n_left_from > 0 && n_left_to_next > 0)
1765 u32 bi0, sw_if_index0, proto0, rx_fib_index0, new_addr0, old_addr0;
1766 u32 iph_offset0 = 0;
1770 ip4_header_t *ip0 = 0;
1771 nat_reass_ip4_t *reass0;
1774 icmp46_header_t *icmp0;
1775 clib_bihash_kv_16_8_t kv0, value0;
1776 snat_session_t *s0 = 0;
1777 u16 old_port0, new_port0;
1780 /* speculatively enqueue b0 to the current next frame */
1786 n_left_to_next -= 1;
1788 b0 = vlib_get_buffer (vm, bi0);
1790 next0 = NAT_IN2OUT_ED_NEXT_LOOKUP;
1792 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1794 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1797 if (PREDICT_FALSE (nat_reass_is_drop_frag (0)))
1799 next0 = NAT_IN2OUT_ED_NEXT_DROP;
1800 b0->error = node->errors[NAT_IN2OUT_ED_ERROR_DROP_FRAGMENT];
1804 if (is_output_feature)
1805 iph_offset0 = vnet_buffer (b0)->ip.save_rewrite_length;
1807 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
1810 udp0 = ip4_next_header (ip0);
1811 tcp0 = (tcp_header_t *) udp0;
1812 icmp0 = (icmp46_header_t *) udp0;
1813 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1815 reass0 = nat_ip4_reass_find_or_create (ip0->src_address,
1819 1, &fragments_to_drop);
1821 if (PREDICT_FALSE (!reass0))
1823 next0 = NAT_IN2OUT_ED_NEXT_DROP;
1824 b0->error = node->errors[NAT_IN2OUT_ED_ERROR_MAX_REASS];
1825 nat_log_notice ("maximum reassemblies exceeded");
1829 if (PREDICT_FALSE (ip4_is_first_fragment (ip0)))
1831 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1833 if (is_output_feature)
1836 (nat_not_translate_output_feature_fwd
1837 (sm, ip0, thread_index, now, vm, b0)))
1838 reass0->flags |= NAT_REASS_FLAG_ED_DONT_TRANSLATE;
1842 next0 = icmp_in2out_ed_slow_path
1843 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1844 next0, now, thread_index, &s0);
1846 if (PREDICT_TRUE (next0 != NAT_IN2OUT_ED_NEXT_DROP))
1849 reass0->sess_index = s0 - per_thread_data->sessions;
1851 reass0->flags |= NAT_REASS_FLAG_ED_DONT_TRANSLATE;
1852 nat_ip4_reass_get_frags (reass0,
1853 &fragments_to_loopback);
1859 make_ed_kv (&kv0, &ip0->src_address, &ip0->dst_address,
1860 ip0->protocol, rx_fib_index0, udp0->src_port,
1863 if (clib_bihash_search_16_8
1864 (&per_thread_data->in2out_ed, &kv0, &value0))
1866 if (is_output_feature)
1869 (nat44_ed_not_translate_output_feature
1870 (sm, ip0, ip0->protocol, udp0->src_port,
1871 udp0->dst_port, thread_index, sw_if_index0,
1872 vnet_buffer (b0)->sw_if_index[VLIB_TX])))
1874 reass0->flags |= NAT_REASS_FLAG_ED_DONT_TRANSLATE;
1875 nat_ip4_reass_get_frags (reass0,
1876 &fragments_to_loopback);
1882 if (PREDICT_FALSE (nat44_ed_not_translate (sm, node,
1888 reass0->flags |= NAT_REASS_FLAG_ED_DONT_TRANSLATE;
1889 nat_ip4_reass_get_frags (reass0,
1890 &fragments_to_loopback);
1895 if ((proto0 == SNAT_PROTOCOL_TCP) && !tcp_is_init (tcp0))
1897 b0->error = node->errors[NAT_IN2OUT_ED_ERROR_NON_SYN];
1898 next0 = NAT_IN2OUT_ED_NEXT_DROP;
1902 next0 = slow_path_ed (sm, b0, rx_fib_index0, &kv0,
1903 &s0, node, next0, thread_index, now);
1905 if (PREDICT_FALSE (next0 == NAT_IN2OUT_ED_NEXT_DROP))
1908 if (PREDICT_FALSE (!s0))
1910 reass0->flags |= NAT_REASS_FLAG_ED_DONT_TRANSLATE;
1914 reass0->sess_index = s0 - per_thread_data->sessions;
1918 s0 = pool_elt_at_index (per_thread_data->sessions,
1920 reass0->sess_index = value0.value;
1922 nat_ip4_reass_get_frags (reass0, &fragments_to_loopback);
1926 if (reass0->flags & NAT_REASS_FLAG_ED_DONT_TRANSLATE)
1928 if (PREDICT_FALSE (reass0->sess_index == (u32) ~ 0))
1930 if (nat_ip4_reass_add_fragment
1931 (reass0, bi0, &fragments_to_drop))
1933 b0->error = node->errors[NAT_IN2OUT_ED_ERROR_MAX_FRAG];
1935 ("maximum fragments per reassembly exceeded");
1936 next0 = NAT_IN2OUT_ED_NEXT_DROP;
1942 s0 = pool_elt_at_index (per_thread_data->sessions,
1943 reass0->sess_index);
1946 old_addr0 = ip0->src_address.as_u32;
1947 ip0->src_address = s0->out2in.addr;
1948 new_addr0 = ip0->src_address.as_u32;
1949 if (!is_output_feature)
1950 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1952 sum0 = ip0->checksum;
1953 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1955 src_address /* changed member */ );
1956 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1957 sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
1958 s0->ext_host_addr.as_u32, ip4_header_t,
1960 ip0->checksum = ip_csum_fold (sum0);
1962 if (PREDICT_FALSE (ip4_is_first_fragment (ip0)))
1964 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1966 old_port0 = tcp0->src_port;
1967 tcp0->src_port = s0->out2in.port;
1968 new_port0 = tcp0->src_port;
1970 sum0 = tcp0->checksum;
1971 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1973 dst_address /* changed member */ );
1974 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1975 ip4_header_t /* cheat */ ,
1976 length /* changed member */ );
1977 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1979 sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
1980 s0->ext_host_addr.as_u32,
1981 ip4_header_t, dst_address);
1982 sum0 = ip_csum_update (sum0, tcp0->dst_port,
1983 s0->ext_host_port, ip4_header_t,
1985 tcp0->dst_port = s0->ext_host_port;
1986 ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
1988 tcp0->checksum = ip_csum_fold (sum0);
1992 old_port0 = udp0->src_port;
1993 udp0->src_port = s0->out2in.port;
1995 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1997 udp0->dst_port = s0->ext_host_port;
1998 ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
2004 nat44_reass_hairpinning (sm, b0, ip0, s0->out2in.port,
2005 s0->ext_host_port, proto0, 1);
2008 nat44_session_update_counters (s0, now,
2009 vlib_buffer_length_in_chain (vm,
2011 /* Per-user LRU list maintenance */
2012 nat44_session_update_lru (sm, s0, thread_index);
2015 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
2016 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
2018 nat44_reass_trace_t *t =
2019 vlib_add_trace (vm, node, b0, sizeof (*t));
2020 t->cached = cached0;
2021 t->sw_if_index = sw_if_index0;
2022 t->next_index = next0;
2032 pkts_processed += next0 != NAT_IN2OUT_ED_NEXT_DROP;
2034 /* verify speculative enqueue, maybe switch current next frame */
2035 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2036 to_next, n_left_to_next,
2040 if (n_left_from == 0 && vec_len (fragments_to_loopback))
2042 from = vlib_frame_vector_args (frame);
2043 u32 len = vec_len (fragments_to_loopback);
2044 if (len <= VLIB_FRAME_SIZE)
2046 clib_memcpy_fast (from, fragments_to_loopback,
2047 sizeof (u32) * len);
2049 vec_reset_length (fragments_to_loopback);
2053 clib_memcpy_fast (from, fragments_to_loopback +
2054 (len - VLIB_FRAME_SIZE),
2055 sizeof (u32) * VLIB_FRAME_SIZE);
2056 n_left_from = VLIB_FRAME_SIZE;
2057 _vec_len (fragments_to_loopback) = len - VLIB_FRAME_SIZE;
2062 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2065 vlib_node_increment_counter (vm, nat44_ed_in2out_reass_node.index,
2066 NAT_IN2OUT_ED_ERROR_IN2OUT_PACKETS,
2069 nat_send_all_to_node (vm, fragments_to_drop, node,
2070 &node->errors[NAT_IN2OUT_ED_ERROR_DROP_FRAGMENT],
2071 NAT_IN2OUT_ED_NEXT_DROP);
2073 vec_free (fragments_to_drop);
2074 vec_free (fragments_to_loopback);
2075 return frame->n_vectors;
2079 nat44_ed_in2out_reass_node_fn (vlib_main_t * vm,
2080 vlib_node_runtime_t * node,
2081 vlib_frame_t * frame)
2083 return nat44_ed_in2out_reass_node_fn_inline (vm, node, frame, 0);
2087 VLIB_REGISTER_NODE (nat44_ed_in2out_reass_node) = {
2088 .function = nat44_ed_in2out_reass_node_fn,
2089 .name = "nat44-ed-in2out-reass",
2090 .vector_size = sizeof (u32),
2091 .format_trace = format_nat44_reass_trace,
2092 .type = VLIB_NODE_TYPE_INTERNAL,
2093 .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
2094 .error_strings = nat_in2out_ed_error_strings,
2095 .n_next_nodes = NAT_IN2OUT_ED_N_NEXT,
2097 [NAT_IN2OUT_ED_NEXT_DROP] = "error-drop",
2098 [NAT_IN2OUT_ED_NEXT_LOOKUP] = "ip4-lookup",
2099 [NAT_IN2OUT_ED_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
2100 [NAT_IN2OUT_ED_NEXT_ICMP_ERROR] = "ip4-icmp-error",
2101 [NAT_IN2OUT_ED_NEXT_REASS] = "nat44-ed-in2out-reass",
2106 VLIB_NODE_FUNCTION_MULTIARCH (nat44_ed_in2out_reass_node,
2107 nat44_ed_in2out_reass_node_fn);
2110 nat44_ed_in2out_reass_output_node_fn (vlib_main_t * vm,
2111 vlib_node_runtime_t * node,
2112 vlib_frame_t * frame)
2114 return nat44_ed_in2out_reass_node_fn_inline (vm, node, frame, 1);
2118 VLIB_REGISTER_NODE (nat44_ed_in2out_reass_output_node) = {
2119 .function = nat44_ed_in2out_reass_output_node_fn,
2120 .name = "nat44-ed-in2out-reass-output",
2121 .vector_size = sizeof (u32),
2122 .format_trace = format_nat44_reass_trace,
2123 .type = VLIB_NODE_TYPE_INTERNAL,
2124 .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
2125 .error_strings = nat_in2out_ed_error_strings,
2126 .n_next_nodes = NAT_IN2OUT_ED_N_NEXT,
2128 [NAT_IN2OUT_ED_NEXT_DROP] = "error-drop",
2129 [NAT_IN2OUT_ED_NEXT_LOOKUP] = "interface-output",
2130 [NAT_IN2OUT_ED_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
2131 [NAT_IN2OUT_ED_NEXT_ICMP_ERROR] = "ip4-icmp-error",
2132 [NAT_IN2OUT_ED_NEXT_REASS] = "nat44-ed-in2out-reass",
2137 VLIB_NODE_FUNCTION_MULTIARCH (nat44_ed_in2out_reass_output_node,
2138 nat44_ed_in2out_reass_output_node_fn);
2141 * fd.io coding-style-patch-verification: ON
2144 * eval: (c-set-style "gnu")