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 static char *nat_in2out_ed_error_strings[] = {
35 #define _(sym,string) string,
36 foreach_nat_in2out_ed_error
42 NAT_IN2OUT_ED_NEXT_LOOKUP,
43 NAT_IN2OUT_ED_NEXT_DROP,
44 NAT_IN2OUT_ED_NEXT_ICMP_ERROR,
45 NAT_IN2OUT_ED_NEXT_SLOW_PATH,
46 NAT_IN2OUT_ED_NEXT_REASS,
48 } nat_in2out_ed_next_t;
56 } nat_in2out_ed_trace_t;
59 format_nat_in2out_ed_trace (u8 * s, va_list * args)
61 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
62 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
63 nat_in2out_ed_trace_t *t = va_arg (*args, nat_in2out_ed_trace_t *);
67 t->is_slow_path ? "NAT44_IN2OUT_ED_SLOW_PATH" :
68 "NAT44_IN2OUT_ED_FAST_PATH";
70 s = format (s, "%s: sw_if_index %d, next index %d, session %d", tag,
71 t->sw_if_index, t->next_index, t->session_index);
76 #ifndef CLIB_MARCH_VARIANT
78 nat44_i2o_ed_is_idle_session_cb (clib_bihash_kv_16_8_t * kv, void *arg)
80 snat_main_t *sm = &snat_main;
81 nat44_is_idle_session_ctx_t *ctx = arg;
83 u64 sess_timeout_time;
84 nat_ed_ses_key_t ed_key;
85 clib_bihash_kv_16_8_t ed_kv;
88 snat_session_key_t key;
89 snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
92 s = pool_elt_at_index (tsm->sessions, kv->value);
93 sess_timeout_time = s->last_heard + (f64) nat44_session_get_timeout (sm, s);
94 if (ctx->now >= sess_timeout_time)
96 if (is_fwd_bypass_session (s))
99 ed_key.l_addr = s->out2in.addr;
100 ed_key.r_addr = s->ext_host_addr;
101 ed_key.fib_index = s->out2in.fib_index;
102 if (snat_is_unk_proto_session (s))
104 ed_key.proto = s->in2out.port;
110 ed_key.proto = snat_proto_to_ip_proto (s->in2out.protocol);
111 ed_key.l_port = s->out2in.port;
112 ed_key.r_port = s->ext_host_port;
114 ed_kv.key[0] = ed_key.as_u64[0];
115 ed_kv.key[1] = ed_key.as_u64[1];
116 if (clib_bihash_add_del_16_8 (&tsm->out2in_ed, &ed_kv, 0))
117 nat_elog_warn ("out2in_ed key del failed");
119 if (snat_is_unk_proto_session (s))
122 snat_ipfix_logging_nat44_ses_delete (ctx->thread_index,
123 s->in2out.addr.as_u32,
124 s->out2in.addr.as_u32,
128 s->in2out.fib_index);
130 nat_syslog_nat44_sdel (s->user_index, s->in2out.fib_index,
131 &s->in2out.addr, s->in2out.port,
132 &s->ext_host_nat_addr, s->ext_host_nat_port,
133 &s->out2in.addr, s->out2in.port,
134 &s->ext_host_addr, s->ext_host_port,
135 s->in2out.protocol, is_twice_nat_session (s));
137 nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
138 s->ext_host_port, s->out2in.protocol, s->out2in.fib_index,
141 if (is_twice_nat_session (s))
143 for (i = 0; i < vec_len (sm->twice_nat_addresses); i++)
145 key.protocol = s->in2out.protocol;
146 key.port = s->ext_host_nat_port;
147 a = sm->twice_nat_addresses + i;
148 if (a->addr.as_u32 == s->ext_host_nat_addr.as_u32)
150 snat_free_outside_address_and_port (sm->twice_nat_addresses,
158 if (snat_is_session_static (s))
161 snat_free_outside_address_and_port (sm->addresses, ctx->thread_index,
164 nat44_delete_session (sm, s, ctx->thread_index);
173 icmp_in2out_ed_slow_path (snat_main_t * sm, vlib_buffer_t * b0,
174 ip4_header_t * ip0, icmp46_header_t * icmp0,
175 u32 sw_if_index0, u32 rx_fib_index0,
176 vlib_node_runtime_t * node, u32 next0, f64 now,
177 u32 thread_index, snat_session_t ** p_s0)
179 next0 = icmp_in2out (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
180 next0, thread_index, p_s0, 0);
181 snat_session_t *s0 = *p_s0;
182 if (PREDICT_TRUE (next0 != NAT_IN2OUT_ED_NEXT_DROP && s0))
185 nat44_session_update_counters (s0, now,
186 vlib_buffer_length_in_chain
187 (sm->vlib_main, b0), thread_index);
188 /* Per-user LRU list maintenance */
189 nat44_session_update_lru (sm, s0, thread_index);
195 slow_path_ed (snat_main_t * sm,
198 clib_bihash_kv_16_8_t * kv,
199 snat_session_t ** sessionp,
200 vlib_node_runtime_t * node, u32 next, u32 thread_index, f64 now,
203 snat_session_t *s = 0;
205 snat_session_key_t key0, key1;
206 lb_nat_type_t lb = 0, is_sm = 0;
207 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
208 nat_ed_ses_key_t *key = (nat_ed_ses_key_t *) kv->key;
209 u32 proto = ip_proto_to_snat_proto (key->proto);
210 nat_outside_fib_t *outside_fib;
211 fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
214 .fp_proto = FIB_PROTOCOL_IP4,
217 .ip4.as_u32 = key->r_addr.as_u32,
220 nat44_is_idle_session_ctx_t ctx;
222 if (PREDICT_FALSE (maximum_sessions_exceeded (sm, thread_index)))
224 b->error = node->errors[NAT_IN2OUT_ED_ERROR_MAX_SESSIONS_EXCEEDED];
225 nat_ipfix_logging_max_sessions (thread_index, sm->max_translations);
226 nat_elog_notice ("maximum sessions exceeded");
227 return NAT_IN2OUT_ED_NEXT_DROP;
230 key0.addr = key->l_addr;
231 key0.port = key->l_port;
232 key1.protocol = key0.protocol = proto;
233 key0.fib_index = rx_fib_index;
234 key1.fib_index = sm->outside_fib_index;
235 /* First try to match static mapping by local address and port */
236 if (snat_static_mapping_match
237 (sm, key0, &key1, 0, 0, 0, &lb, 0, &identity_nat))
239 /* Try to create dynamic translation */
240 if (snat_alloc_outside_address_and_port (sm->addresses, rx_fib_index,
243 tsm->snat_thread_index))
245 nat_elog_notice ("addresses exhausted");
246 b->error = node->errors[NAT_IN2OUT_ED_ERROR_OUT_OF_PORTS];
247 return NAT_IN2OUT_ED_NEXT_DROP;
252 if (PREDICT_FALSE (identity_nat))
261 if (proto == SNAT_PROTOCOL_TCP)
263 if (!tcp_is_init (tcp))
265 b->error = node->errors[NAT_IN2OUT_ED_ERROR_NON_SYN];
266 return NAT_IN2OUT_ED_NEXT_DROP;
270 u = nat_user_get_or_create (sm, &key->l_addr, rx_fib_index, thread_index);
273 nat_elog_warn ("create NAT user failed");
275 snat_free_outside_address_and_port (sm->addresses,
276 thread_index, &key1);
277 return NAT_IN2OUT_ED_NEXT_DROP;
280 s = nat_ed_session_alloc (sm, u, thread_index, now);
283 nat44_delete_user_with_no_session (sm, u, thread_index);
284 nat_elog_warn ("create NAT session failed");
286 snat_free_outside_address_and_port (sm->addresses,
287 thread_index, &key1);
288 return NAT_IN2OUT_ED_NEXT_DROP;
291 user_session_increment (sm, u, is_sm);
293 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
295 s->flags |= SNAT_SESSION_FLAG_LOAD_BALANCING;
296 s->flags |= SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT;
297 s->ext_host_addr = key->r_addr;
298 s->ext_host_port = key->r_port;
301 s->out2in.protocol = key0.protocol;
303 switch (vec_len (sm->outside_fibs))
306 s->out2in.fib_index = sm->outside_fib_index;
309 s->out2in.fib_index = sm->outside_fibs[0].fib_index;
313 vec_foreach (outside_fib, sm->outside_fibs)
315 fei = fib_table_lookup (outside_fib->fib_index, &pfx);
316 if (FIB_NODE_INDEX_INVALID != fei)
318 if (fib_entry_get_resolving_interface (fei) != ~0)
320 s->out2in.fib_index = outside_fib->fib_index;
329 /* Add to lookup tables */
330 kv->value = s - tsm->sessions;
332 ctx.thread_index = thread_index;
333 if (clib_bihash_add_or_overwrite_stale_16_8 (&tsm->in2out_ed, kv,
334 nat44_i2o_ed_is_idle_session_cb,
336 nat_elog_notice ("in2out-ed key add failed");
338 make_ed_kv (kv, &key1.addr, &key->r_addr, key->proto, s->out2in.fib_index,
339 key1.port, key->r_port);
340 kv->value = s - tsm->sessions;
341 if (clib_bihash_add_or_overwrite_stale_16_8 (&tsm->out2in_ed, kv,
342 nat44_o2i_ed_is_idle_session_cb,
344 nat_elog_notice ("out2in-ed key add failed");
349 snat_ipfix_logging_nat44_ses_create (thread_index,
350 s->in2out.addr.as_u32,
351 s->out2in.addr.as_u32,
354 s->out2in.port, s->in2out.fib_index);
356 nat_syslog_nat44_sadd (s->user_index, s->in2out.fib_index,
357 &s->in2out.addr, s->in2out.port,
358 &s->ext_host_nat_addr, s->ext_host_nat_port,
359 &s->out2in.addr, s->out2in.port,
360 &s->ext_host_addr, s->ext_host_port,
361 s->in2out.protocol, 0);
363 nat_ha_sadd (&s->in2out.addr, s->in2out.port, &s->out2in.addr,
364 s->out2in.port, &s->ext_host_addr, s->ext_host_port,
365 &s->ext_host_nat_addr, s->ext_host_nat_port,
366 s->in2out.protocol, s->in2out.fib_index, s->flags,
372 static_always_inline int
373 nat44_ed_not_translate (snat_main_t * sm, vlib_node_runtime_t * node,
374 u32 sw_if_index, ip4_header_t * ip, u32 proto,
375 u32 rx_fib_index, u32 thread_index)
377 udp_header_t *udp = ip4_next_header (ip);
378 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
379 clib_bihash_kv_16_8_t kv, value;
380 snat_session_key_t key0, key1;
382 make_ed_kv (&kv, &ip->dst_address, &ip->src_address, ip->protocol,
383 sm->outside_fib_index, udp->dst_port, udp->src_port);
385 /* NAT packet aimed at external address if */
386 /* has active sessions */
387 if (clib_bihash_search_16_8 (&tsm->out2in_ed, &kv, &value))
389 key0.addr = ip->dst_address;
390 key0.port = udp->dst_port;
391 key0.protocol = proto;
392 key0.fib_index = sm->outside_fib_index;
393 /* or is static mappings */
394 if (!snat_static_mapping_match (sm, key0, &key1, 1, 0, 0, 0, 0, 0))
400 if (sm->forwarding_enabled)
403 return snat_not_translate_fast (sm, node, sw_if_index, ip, proto,
407 static_always_inline int
408 nat_not_translate_output_feature_fwd (snat_main_t * sm, ip4_header_t * ip,
409 u32 thread_index, f64 now,
410 vlib_main_t * vm, vlib_buffer_t * b)
412 nat_ed_ses_key_t key;
413 clib_bihash_kv_16_8_t kv, value;
415 snat_session_t *s = 0;
416 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
418 if (!sm->forwarding_enabled)
421 if (ip->protocol == IP_PROTOCOL_ICMP)
423 key.as_u64[0] = key.as_u64[1] = 0;
424 if (get_icmp_i2o_ed_key (ip, &key))
427 kv.key[0] = key.as_u64[0];
428 kv.key[1] = key.as_u64[1];
430 else if (ip->protocol == IP_PROTOCOL_UDP || ip->protocol == IP_PROTOCOL_TCP)
432 udp = ip4_next_header (ip);
433 make_ed_kv (&kv, &ip->src_address, &ip->dst_address, ip->protocol, 0,
434 udp->src_port, udp->dst_port);
438 make_ed_kv (&kv, &ip->src_address, &ip->dst_address, ip->protocol, 0, 0,
442 if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
444 s = pool_elt_at_index (tsm->sessions, value.value);
445 if (is_fwd_bypass_session (s))
447 if (ip->protocol == IP_PROTOCOL_TCP)
449 tcp_header_t *tcp = ip4_next_header (ip);
450 if (nat44_set_tcp_session_state_i2o (sm, s, tcp, thread_index))
454 nat44_session_update_counters (s, now,
455 vlib_buffer_length_in_chain (vm, b),
457 /* Per-user LRU list maintenance */
458 nat44_session_update_lru (sm, s, thread_index);
468 static_always_inline int
469 nat44_ed_not_translate_output_feature (snat_main_t * sm, ip4_header_t * ip,
470 u8 proto, u16 src_port, u16 dst_port,
471 u32 thread_index, u32 rx_sw_if_index,
474 clib_bihash_kv_16_8_t kv, value;
475 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
478 u32 rx_fib_index = ip4_fib_table_get_index_for_sw_if_index (rx_sw_if_index);
479 u32 tx_fib_index = ip4_fib_table_get_index_for_sw_if_index (tx_sw_if_index);
482 make_ed_kv (&kv, &ip->src_address, &ip->dst_address, proto, tx_fib_index,
484 if (!clib_bihash_search_16_8 (&tsm->out2in_ed, &kv, &value))
486 s = pool_elt_at_index (tsm->sessions, value.value);
487 if (nat44_is_ses_closed (s))
489 nat_free_session_data (sm, s, thread_index, 0);
490 nat44_delete_session (sm, s, thread_index);
493 s->flags |= SNAT_SESSION_FLAG_OUTPUT_FEATURE;
498 make_ed_kv (&kv, &ip->dst_address, &ip->src_address, proto, rx_fib_index,
500 if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
502 s = pool_elt_at_index (tsm->sessions, value.value);
503 if (is_fwd_bypass_session (s))
508 pool_foreach (i, sm->output_feature_interfaces,
510 if ((nat_interface_is_inside (i)) && (rx_sw_if_index == i->sw_if_index))
520 #ifndef CLIB_MARCH_VARIANT
522 icmp_match_in2out_ed (snat_main_t * sm, vlib_node_runtime_t * node,
523 u32 thread_index, vlib_buffer_t * b, ip4_header_t * ip,
524 u8 * p_proto, snat_session_key_t * p_value,
525 u8 * p_dont_translate, void *d, void *e)
527 icmp46_header_t *icmp;
530 nat_ed_ses_key_t key;
531 snat_session_t *s = 0;
532 u8 dont_translate = 0;
533 clib_bihash_kv_16_8_t kv, value;
536 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
538 icmp = (icmp46_header_t *) ip4_next_header (ip);
539 sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
540 rx_fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
542 key.as_u64[0] = key.as_u64[1] = 0;
543 err = get_icmp_i2o_ed_key (ip, &key);
546 b->error = node->errors[err];
547 next = NAT_IN2OUT_ED_NEXT_DROP;
550 key.fib_index = rx_fib_index;
552 kv.key[0] = key.as_u64[0];
553 kv.key[1] = key.as_u64[1];
555 if (clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
557 if (vnet_buffer (b)->sw_if_index[VLIB_TX] != ~0)
559 if (PREDICT_FALSE (nat44_ed_not_translate_output_feature (sm, ip,
578 if (PREDICT_FALSE (nat44_ed_not_translate (sm, node, sw_if_index,
579 ip, SNAT_PROTOCOL_ICMP,
588 if (PREDICT_FALSE (icmp_is_error_message (icmp)))
590 b->error = node->errors[NAT_IN2OUT_ED_ERROR_BAD_ICMP_TYPE];
591 next = NAT_IN2OUT_ED_NEXT_DROP;
595 next = slow_path_ed (sm, b, rx_fib_index, &kv, &s, node, next,
596 thread_index, vlib_time_now (sm->vlib_main), 0);
598 if (PREDICT_FALSE (next == NAT_IN2OUT_ED_NEXT_DROP))
609 if (PREDICT_FALSE (icmp->type != ICMP4_echo_request &&
610 icmp->type != ICMP4_echo_reply &&
611 !icmp_is_error_message (icmp)))
613 b->error = node->errors[NAT_IN2OUT_ED_ERROR_BAD_ICMP_TYPE];
614 next = NAT_IN2OUT_ED_NEXT_DROP;
618 s = pool_elt_at_index (tsm->sessions, value.value);
621 *p_proto = ip_proto_to_snat_proto (key.proto);
624 *p_value = s->out2in;
625 *p_dont_translate = dont_translate;
627 *(snat_session_t **) d = s;
632 static snat_session_t *
633 nat44_ed_in2out_unknown_proto (snat_main_t * sm,
639 vlib_main_t * vm, vlib_node_runtime_t * node)
641 clib_bihash_kv_8_8_t kv, value;
642 clib_bihash_kv_16_8_t s_kv, s_value;
643 snat_static_mapping_t *m;
644 u32 old_addr, new_addr = 0;
647 dlist_elt_t *head, *elt;
648 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
649 u32 elt_index, head_index, ses_index;
651 u32 outside_fib_index = sm->outside_fib_index;
654 nat_outside_fib_t *outside_fib;
655 fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
657 .fp_proto = FIB_PROTOCOL_IP4,
660 .ip4.as_u32 = ip->dst_address.as_u32,
664 switch (vec_len (sm->outside_fibs))
667 outside_fib_index = sm->outside_fib_index;
670 outside_fib_index = sm->outside_fibs[0].fib_index;
674 vec_foreach (outside_fib, sm->outside_fibs)
676 fei = fib_table_lookup (outside_fib->fib_index, &pfx);
677 if (FIB_NODE_INDEX_INVALID != fei)
679 if (fib_entry_get_resolving_interface (fei) != ~0)
681 outside_fib_index = outside_fib->fib_index;
689 old_addr = ip->src_address.as_u32;
691 make_ed_kv (&s_kv, &ip->src_address, &ip->dst_address, ip->protocol,
694 if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &s_kv, &s_value))
696 s = pool_elt_at_index (tsm->sessions, s_value.value);
697 new_addr = ip->src_address.as_u32 = s->out2in.addr.as_u32;
701 if (PREDICT_FALSE (maximum_sessions_exceeded (sm, thread_index)))
703 b->error = node->errors[NAT_IN2OUT_ED_ERROR_MAX_SESSIONS_EXCEEDED];
704 nat_ipfix_logging_max_sessions (thread_index, sm->max_translations);
705 nat_elog_notice ("maximum sessions exceeded");
709 u = nat_user_get_or_create (sm, &ip->src_address, rx_fib_index,
713 nat_elog_warn ("create NAT user failed");
717 make_sm_kv (&kv, &ip->src_address, 0, rx_fib_index, 0);
719 /* Try to find static mapping first */
720 if (!clib_bihash_search_8_8 (&sm->static_mapping_by_local, &kv, &value))
722 m = pool_elt_at_index (sm->static_mappings, value.value);
723 new_addr = ip->src_address.as_u32 = m->external_addr.as_u32;
727 /* Fallback to 3-tuple key */
730 /* Choose same out address as for TCP/UDP session to same destination */
731 head_index = u->sessions_per_user_list_head_index;
732 head = pool_elt_at_index (tsm->list_pool, head_index);
733 elt_index = head->next;
734 if (PREDICT_FALSE (elt_index == ~0))
738 elt = pool_elt_at_index (tsm->list_pool, elt_index);
739 ses_index = elt->value;
742 while (ses_index != ~0)
744 s = pool_elt_at_index (tsm->sessions, ses_index);
745 elt_index = elt->next;
746 elt = pool_elt_at_index (tsm->list_pool, elt_index);
747 ses_index = elt->value;
749 if (s->ext_host_addr.as_u32 == ip->dst_address.as_u32)
751 new_addr = ip->src_address.as_u32 = s->out2in.addr.as_u32;
753 make_ed_kv (&s_kv, &s->out2in.addr, &ip->dst_address,
754 ip->protocol, outside_fib_index, 0, 0);
755 if (clib_bihash_search_16_8
756 (&tsm->out2in_ed, &s_kv, &s_value))
763 for (i = 0; i < vec_len (sm->addresses); i++)
765 make_ed_kv (&s_kv, &sm->addresses[i].addr, &ip->dst_address,
766 ip->protocol, outside_fib_index, 0, 0);
767 if (clib_bihash_search_16_8 (&tsm->out2in_ed, &s_kv, &s_value))
769 new_addr = ip->src_address.as_u32 =
770 sm->addresses[i].addr.as_u32;
778 s = nat_ed_session_alloc (sm, u, thread_index, now);
781 nat44_delete_user_with_no_session (sm, u, thread_index);
782 nat_elog_warn ("create NAT session failed");
786 s->ext_host_addr.as_u32 = ip->dst_address.as_u32;
787 s->flags |= SNAT_SESSION_FLAG_UNKNOWN_PROTO;
788 s->flags |= SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT;
789 s->out2in.addr.as_u32 = new_addr;
790 s->out2in.fib_index = outside_fib_index;
791 s->in2out.addr.as_u32 = old_addr;
792 s->in2out.fib_index = rx_fib_index;
793 s->in2out.port = s->out2in.port = ip->protocol;
795 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
796 user_session_increment (sm, u, is_sm);
798 /* Add to lookup tables */
799 make_ed_kv (&s_kv, &s->in2out.addr, &ip->dst_address, ip->protocol,
801 s_kv.value = s - tsm->sessions;
802 if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &s_kv, 1))
803 nat_elog_notice ("in2out key add failed");
805 make_ed_kv (&s_kv, &s->out2in.addr, &ip->dst_address, ip->protocol,
806 outside_fib_index, 0, 0);
807 s_kv.value = s - tsm->sessions;
808 if (clib_bihash_add_del_16_8 (&tsm->out2in_ed, &s_kv, 1))
809 nat_elog_notice ("out2in key add failed");
812 /* Update IP checksum */
814 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, src_address);
815 ip->checksum = ip_csum_fold (sum);
818 nat44_session_update_counters (s, now, vlib_buffer_length_in_chain (vm, b),
820 /* Per-user LRU list maintenance */
821 nat44_session_update_lru (sm, s, thread_index);
824 if (vnet_buffer (b)->sw_if_index[VLIB_TX] == ~0)
825 nat44_ed_hairpinning_unknown_proto (sm, b, ip);
827 if (vnet_buffer (b)->sw_if_index[VLIB_TX] == ~0)
828 vnet_buffer (b)->sw_if_index[VLIB_TX] = outside_fib_index;
834 nat44_ed_in2out_node_fn_inline (vlib_main_t * vm,
835 vlib_node_runtime_t * node,
836 vlib_frame_t * frame, int is_slow_path,
837 int is_output_feature)
839 u32 n_left_from, *from, *to_next, pkts_processed = 0, stats_node_index;
840 nat_in2out_ed_next_t next_index;
841 snat_main_t *sm = &snat_main;
842 f64 now = vlib_time_now (vm);
843 u32 thread_index = vm->thread_index;
844 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
845 u32 tcp_packets = 0, udp_packets = 0, icmp_packets = 0, other_packets =
848 stats_node_index = is_slow_path ? sm->ed_in2out_slowpath_node_index :
849 sm->ed_in2out_node_index;
851 from = vlib_frame_vector_args (frame);
852 n_left_from = frame->n_vectors;
853 next_index = node->cached_next_index;
855 while (n_left_from > 0)
859 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
861 while (n_left_from >= 4 && n_left_to_next >= 2)
864 vlib_buffer_t *b0, *b1;
865 u32 next0, sw_if_index0, rx_fib_index0, iph_offset0 = 0, proto0,
866 new_addr0, old_addr0;
867 u32 next1, sw_if_index1, rx_fib_index1, iph_offset1 = 0, proto1,
868 new_addr1, old_addr1;
869 u16 old_port0, new_port0, old_port1, new_port1;
870 ip4_header_t *ip0, *ip1;
871 udp_header_t *udp0, *udp1;
872 tcp_header_t *tcp0, *tcp1;
873 icmp46_header_t *icmp0, *icmp1;
874 snat_session_t *s0 = 0, *s1 = 0;
875 clib_bihash_kv_16_8_t kv0, value0, kv1, value1;
876 ip_csum_t sum0, sum1;
878 /* Prefetch next iteration. */
880 vlib_buffer_t *p2, *p3;
882 p2 = vlib_get_buffer (vm, from[2]);
883 p3 = vlib_get_buffer (vm, from[3]);
885 vlib_prefetch_buffer_header (p2, LOAD);
886 vlib_prefetch_buffer_header (p3, LOAD);
888 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
889 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
892 /* speculatively enqueue b0 and b1 to the current next frame */
893 to_next[0] = bi0 = from[0];
894 to_next[1] = bi1 = from[1];
900 b0 = vlib_get_buffer (vm, bi0);
901 b1 = vlib_get_buffer (vm, bi1);
903 next0 = NAT_IN2OUT_ED_NEXT_LOOKUP;
905 if (is_output_feature)
906 iph_offset0 = vnet_buffer (b0)->ip.save_rewrite_length;
908 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
911 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
913 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
916 if (PREDICT_FALSE (ip0->ttl == 1))
918 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
919 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
920 ICMP4_time_exceeded_ttl_exceeded_in_transit,
922 next0 = NAT_IN2OUT_ED_NEXT_ICMP_ERROR;
926 udp0 = ip4_next_header (ip0);
927 tcp0 = (tcp_header_t *) udp0;
928 icmp0 = (icmp46_header_t *) udp0;
929 proto0 = ip_proto_to_snat_proto (ip0->protocol);
933 if (PREDICT_FALSE (proto0 == ~0))
935 s0 = nat44_ed_in2out_unknown_proto (sm, b0, ip0,
937 thread_index, now, vm,
940 next0 = NAT_IN2OUT_ED_NEXT_DROP;
945 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
947 next0 = icmp_in2out_ed_slow_path
948 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
949 next0, now, thread_index, &s0);
956 if (PREDICT_FALSE (proto0 == ~0))
958 next0 = NAT_IN2OUT_ED_NEXT_SLOW_PATH;
962 if (ip4_is_fragment (ip0))
964 next0 = NAT_IN2OUT_ED_NEXT_REASS;
969 if (is_output_feature)
972 (nat_not_translate_output_feature_fwd
973 (sm, ip0, thread_index, now, vm, b0)))
977 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
979 next0 = NAT_IN2OUT_ED_NEXT_SLOW_PATH;
984 make_ed_kv (&kv0, &ip0->src_address, &ip0->dst_address,
985 ip0->protocol, rx_fib_index0, udp0->src_port,
988 if (clib_bihash_search_16_8 (&tsm->in2out_ed, &kv0, &value0))
992 if (is_output_feature)
995 (nat44_ed_not_translate_output_feature
996 (sm, ip0, ip0->protocol, udp0->src_port,
997 udp0->dst_port, thread_index, sw_if_index0,
998 vnet_buffer (b0)->sw_if_index[VLIB_TX])))
1003 if (PREDICT_FALSE (nat44_ed_not_translate (sm, node,
1012 slow_path_ed (sm, b0, rx_fib_index0, &kv0, &s0, node,
1013 next0, thread_index, now, tcp0);
1015 if (PREDICT_FALSE (next0 == NAT_IN2OUT_ED_NEXT_DROP))
1018 if (PREDICT_FALSE (!s0))
1023 next0 = NAT_IN2OUT_ED_NEXT_SLOW_PATH;
1029 s0 = pool_elt_at_index (tsm->sessions, value0.value);
1032 b0->flags |= VNET_BUFFER_F_IS_NATED;
1034 if (!is_output_feature)
1035 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1037 old_addr0 = ip0->src_address.as_u32;
1038 new_addr0 = ip0->src_address.as_u32 = s0->out2in.addr.as_u32;
1039 sum0 = ip0->checksum;
1040 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1042 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1043 sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
1044 s0->ext_host_addr.as_u32, ip4_header_t,
1046 ip0->checksum = ip_csum_fold (sum0);
1048 old_port0 = udp0->src_port;
1049 new_port0 = udp0->src_port = s0->out2in.port;
1051 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1053 sum0 = tcp0->checksum;
1054 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1056 sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
1058 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1060 sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
1061 s0->ext_host_addr.as_u32,
1062 ip4_header_t, dst_address);
1063 sum0 = ip_csum_update (sum0, tcp0->dst_port,
1064 s0->ext_host_port, ip4_header_t,
1066 tcp0->dst_port = s0->ext_host_port;
1067 ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
1069 mss_clamping (sm, tcp0, &sum0);
1070 tcp0->checksum = ip_csum_fold (sum0);
1072 if (nat44_set_tcp_session_state_i2o
1073 (sm, s0, tcp0, thread_index))
1076 else if (udp0->checksum)
1078 sum0 = udp0->checksum;
1079 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1081 sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
1083 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1085 sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
1086 s0->ext_host_addr.as_u32,
1087 ip4_header_t, dst_address);
1088 sum0 = ip_csum_update (sum0, tcp0->dst_port,
1089 s0->ext_host_port, ip4_header_t,
1091 udp0->dst_port = s0->ext_host_port;
1092 ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
1094 udp0->checksum = ip_csum_fold (sum0);
1099 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1101 udp0->dst_port = s0->ext_host_port;
1102 ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
1108 nat44_session_update_counters (s0, now,
1109 vlib_buffer_length_in_chain (vm,
1112 /* Per-user LRU list maintenance */
1113 nat44_session_update_lru (sm, s0, thread_index);
1116 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1117 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1119 nat_in2out_ed_trace_t *t =
1120 vlib_add_trace (vm, node, b0, sizeof (*t));
1121 t->is_slow_path = is_slow_path;
1122 t->sw_if_index = sw_if_index0;
1123 t->next_index = next0;
1124 t->session_index = ~0;
1126 t->session_index = s0 - tsm->sessions;
1129 pkts_processed += next0 == NAT_IN2OUT_ED_NEXT_LOOKUP;
1132 next1 = NAT_IN2OUT_ED_NEXT_LOOKUP;
1134 if (is_output_feature)
1135 iph_offset1 = vnet_buffer (b1)->ip.save_rewrite_length;
1137 ip1 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b1) +
1140 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
1142 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1145 if (PREDICT_FALSE (ip1->ttl == 1))
1147 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1148 icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
1149 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1151 next1 = NAT_IN2OUT_ED_NEXT_ICMP_ERROR;
1155 udp1 = ip4_next_header (ip1);
1156 tcp1 = (tcp_header_t *) udp1;
1157 icmp1 = (icmp46_header_t *) udp1;
1158 proto1 = ip_proto_to_snat_proto (ip1->protocol);
1162 if (PREDICT_FALSE (proto1 == ~0))
1164 s1 = nat44_ed_in2out_unknown_proto (sm, b1, ip1,
1166 thread_index, now, vm,
1169 next1 = NAT_IN2OUT_ED_NEXT_DROP;
1174 if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
1176 next1 = icmp_in2out_ed_slow_path
1177 (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node,
1178 next1, now, thread_index, &s1);
1185 if (PREDICT_FALSE (proto1 == ~0))
1187 next1 = NAT_IN2OUT_ED_NEXT_SLOW_PATH;
1191 if (ip4_is_fragment (ip1))
1193 next1 = NAT_IN2OUT_ED_NEXT_REASS;
1198 if (is_output_feature)
1201 (nat_not_translate_output_feature_fwd
1202 (sm, ip1, thread_index, now, vm, b1)))
1206 if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
1208 next1 = NAT_IN2OUT_ED_NEXT_SLOW_PATH;
1213 make_ed_kv (&kv1, &ip1->src_address, &ip1->dst_address,
1214 ip1->protocol, rx_fib_index1, udp1->src_port,
1217 if (clib_bihash_search_16_8 (&tsm->in2out_ed, &kv1, &value1))
1221 if (is_output_feature)
1224 (nat44_ed_not_translate_output_feature
1225 (sm, ip1, ip1->protocol, udp1->src_port,
1226 udp1->dst_port, thread_index, sw_if_index1,
1227 vnet_buffer (b1)->sw_if_index[VLIB_TX])))
1232 if (PREDICT_FALSE (nat44_ed_not_translate (sm, node,
1241 slow_path_ed (sm, b1, rx_fib_index1, &kv1, &s1, node,
1242 next1, thread_index, now, tcp1);
1244 if (PREDICT_FALSE (next1 == NAT_IN2OUT_ED_NEXT_DROP))
1247 if (PREDICT_FALSE (!s1))
1252 next1 = NAT_IN2OUT_ED_NEXT_SLOW_PATH;
1258 s1 = pool_elt_at_index (tsm->sessions, value1.value);
1261 b1->flags |= VNET_BUFFER_F_IS_NATED;
1263 if (!is_output_feature)
1264 vnet_buffer (b1)->sw_if_index[VLIB_TX] = s1->out2in.fib_index;
1266 old_addr1 = ip1->src_address.as_u32;
1267 new_addr1 = ip1->src_address.as_u32 = s1->out2in.addr.as_u32;
1268 sum1 = ip1->checksum;
1269 sum1 = ip_csum_update (sum1, old_addr1, new_addr1, ip4_header_t,
1271 if (PREDICT_FALSE (is_twice_nat_session (s1)))
1272 sum1 = ip_csum_update (sum1, ip1->dst_address.as_u32,
1273 s1->ext_host_addr.as_u32, ip4_header_t,
1275 ip1->checksum = ip_csum_fold (sum1);
1277 old_port1 = udp1->src_port;
1278 new_port1 = udp1->src_port = s1->out2in.port;
1280 if (PREDICT_TRUE (proto1 == SNAT_PROTOCOL_TCP))
1282 sum1 = tcp1->checksum;
1283 sum1 = ip_csum_update (sum1, old_addr1, new_addr1, ip4_header_t,
1285 sum1 = ip_csum_update (sum1, old_port1, new_port1, ip4_header_t,
1287 if (PREDICT_FALSE (is_twice_nat_session (s1)))
1289 sum1 = ip_csum_update (sum1, ip1->dst_address.as_u32,
1290 s1->ext_host_addr.as_u32,
1291 ip4_header_t, dst_address);
1292 sum1 = ip_csum_update (sum1, tcp1->dst_port,
1293 s1->ext_host_port, ip4_header_t,
1295 tcp1->dst_port = s1->ext_host_port;
1296 ip1->dst_address.as_u32 = s1->ext_host_addr.as_u32;
1298 tcp1->checksum = ip_csum_fold (sum1);
1299 mss_clamping (sm, tcp1, &sum1);
1301 if (nat44_set_tcp_session_state_i2o
1302 (sm, s1, tcp1, thread_index))
1305 else if (udp1->checksum)
1307 sum1 = udp1->checksum;
1308 sum1 = ip_csum_update (sum1, old_addr1, new_addr1, ip4_header_t,
1310 sum1 = ip_csum_update (sum1, old_port1, new_port1, ip4_header_t,
1313 if (PREDICT_FALSE (is_twice_nat_session (s1)))
1315 sum1 = ip_csum_update (sum1, ip1->dst_address.as_u32,
1316 s1->ext_host_addr.as_u32,
1317 ip4_header_t, dst_address);
1318 sum1 = ip_csum_update (sum1, tcp1->dst_port,
1319 s1->ext_host_port, ip4_header_t,
1321 udp1->dst_port = s1->ext_host_port;
1322 ip1->dst_address.as_u32 = s1->ext_host_addr.as_u32;
1324 udp1->checksum = ip_csum_fold (sum1);
1329 if (PREDICT_FALSE (is_twice_nat_session (s1)))
1331 udp1->dst_port = s1->ext_host_port;
1332 ip1->dst_address.as_u32 = s1->ext_host_addr.as_u32;
1338 nat44_session_update_counters (s1, now,
1339 vlib_buffer_length_in_chain (vm, b1),
1341 /* Per-user LRU list maintenance */
1342 nat44_session_update_lru (sm, s1, thread_index);
1345 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1346 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
1348 nat_in2out_ed_trace_t *t =
1349 vlib_add_trace (vm, node, b1, sizeof (*t));
1350 t->is_slow_path = is_slow_path;
1351 t->sw_if_index = sw_if_index1;
1352 t->next_index = next1;
1353 t->session_index = ~0;
1355 t->session_index = s1 - tsm->sessions;
1358 pkts_processed += next1 == NAT_IN2OUT_ED_NEXT_LOOKUP;
1360 /* verify speculative enqueues, maybe switch current next frame */
1361 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1362 to_next, n_left_to_next,
1363 bi0, bi1, next0, next1);
1366 while (n_left_from > 0 && n_left_to_next > 0)
1370 u32 next0, sw_if_index0, rx_fib_index0, iph_offset0 = 0, proto0,
1371 new_addr0, old_addr0;
1372 u16 old_port0, new_port0;
1376 icmp46_header_t *icmp0;
1377 snat_session_t *s0 = 0;
1378 clib_bihash_kv_16_8_t kv0, value0;
1381 /* speculatively enqueue b0 to the current next frame */
1387 n_left_to_next -= 1;
1389 b0 = vlib_get_buffer (vm, bi0);
1390 next0 = NAT_IN2OUT_ED_NEXT_LOOKUP;
1392 if (is_output_feature)
1393 iph_offset0 = vnet_buffer (b0)->ip.save_rewrite_length;
1395 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
1398 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1400 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1403 if (PREDICT_FALSE (ip0->ttl == 1))
1405 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1406 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1407 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1409 next0 = NAT_IN2OUT_ED_NEXT_ICMP_ERROR;
1413 udp0 = ip4_next_header (ip0);
1414 tcp0 = (tcp_header_t *) udp0;
1415 icmp0 = (icmp46_header_t *) udp0;
1416 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1420 if (PREDICT_FALSE (proto0 == ~0))
1422 s0 = nat44_ed_in2out_unknown_proto (sm, b0, ip0,
1424 thread_index, now, vm,
1427 next0 = NAT_IN2OUT_ED_NEXT_DROP;
1432 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1434 next0 = icmp_in2out_ed_slow_path
1435 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1436 next0, now, thread_index, &s0);
1443 if (PREDICT_FALSE (proto0 == ~0))
1445 next0 = NAT_IN2OUT_ED_NEXT_SLOW_PATH;
1449 if (ip4_is_fragment (ip0))
1451 next0 = NAT_IN2OUT_ED_NEXT_REASS;
1456 if (is_output_feature)
1459 (nat_not_translate_output_feature_fwd
1460 (sm, ip0, thread_index, now, vm, b0)))
1464 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1466 next0 = NAT_IN2OUT_ED_NEXT_SLOW_PATH;
1471 make_ed_kv (&kv0, &ip0->src_address, &ip0->dst_address,
1472 ip0->protocol, rx_fib_index0, udp0->src_port,
1475 if (clib_bihash_search_16_8 (&tsm->in2out_ed, &kv0, &value0))
1479 if (is_output_feature)
1482 (nat44_ed_not_translate_output_feature
1483 (sm, ip0, ip0->protocol, udp0->src_port,
1484 udp0->dst_port, thread_index, sw_if_index0,
1485 vnet_buffer (b0)->sw_if_index[VLIB_TX])))
1490 if (PREDICT_FALSE (nat44_ed_not_translate (sm, node,
1499 slow_path_ed (sm, b0, rx_fib_index0, &kv0, &s0, node,
1500 next0, thread_index, now, tcp0);
1502 if (PREDICT_FALSE (next0 == NAT_IN2OUT_ED_NEXT_DROP))
1505 if (PREDICT_FALSE (!s0))
1510 next0 = NAT_IN2OUT_ED_NEXT_SLOW_PATH;
1516 s0 = pool_elt_at_index (tsm->sessions, value0.value);
1519 b0->flags |= VNET_BUFFER_F_IS_NATED;
1521 if (!is_output_feature)
1522 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1524 old_addr0 = ip0->src_address.as_u32;
1525 new_addr0 = ip0->src_address.as_u32 = s0->out2in.addr.as_u32;
1526 sum0 = ip0->checksum;
1527 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1529 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1530 sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
1531 s0->ext_host_addr.as_u32, ip4_header_t,
1533 ip0->checksum = ip_csum_fold (sum0);
1535 old_port0 = udp0->src_port;
1536 new_port0 = udp0->src_port = s0->out2in.port;
1538 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1540 sum0 = tcp0->checksum;
1541 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1543 sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
1545 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1547 sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
1548 s0->ext_host_addr.as_u32,
1549 ip4_header_t, dst_address);
1550 sum0 = ip_csum_update (sum0, tcp0->dst_port,
1551 s0->ext_host_port, ip4_header_t,
1553 tcp0->dst_port = s0->ext_host_port;
1554 ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
1556 mss_clamping (sm, tcp0, &sum0);
1557 tcp0->checksum = ip_csum_fold (sum0);
1559 if (nat44_set_tcp_session_state_i2o
1560 (sm, s0, tcp0, thread_index))
1563 else if (udp0->checksum)
1565 sum0 = udp0->checksum;
1566 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1568 sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
1570 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1572 sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
1573 s0->ext_host_addr.as_u32,
1574 ip4_header_t, dst_address);
1575 sum0 = ip_csum_update (sum0, tcp0->dst_port,
1576 s0->ext_host_port, ip4_header_t,
1578 udp0->dst_port = s0->ext_host_port;
1579 ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
1581 udp0->checksum = ip_csum_fold (sum0);
1586 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1588 udp0->dst_port = s0->ext_host_port;
1589 ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
1595 nat44_session_update_counters (s0, now,
1596 vlib_buffer_length_in_chain (vm, b0),
1598 /* Per-user LRU list maintenance */
1599 nat44_session_update_lru (sm, s0, thread_index);
1602 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1603 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1605 nat_in2out_ed_trace_t *t =
1606 vlib_add_trace (vm, node, b0, sizeof (*t));
1607 t->is_slow_path = is_slow_path;
1608 t->sw_if_index = sw_if_index0;
1609 t->next_index = next0;
1610 t->session_index = ~0;
1612 t->session_index = s0 - tsm->sessions;
1615 pkts_processed += next0 == NAT_IN2OUT_ED_NEXT_LOOKUP;
1617 /* verify speculative enqueue, maybe switch current next frame */
1618 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1619 to_next, n_left_to_next,
1623 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1626 vlib_node_increment_counter (vm, stats_node_index,
1627 NAT_IN2OUT_ED_ERROR_IN2OUT_PACKETS,
1629 vlib_node_increment_counter (vm, stats_node_index,
1630 NAT_IN2OUT_ED_ERROR_TCP_PACKETS, tcp_packets);
1631 vlib_node_increment_counter (vm, stats_node_index,
1632 NAT_IN2OUT_ED_ERROR_UDP_PACKETS, tcp_packets);
1633 vlib_node_increment_counter (vm, stats_node_index,
1634 NAT_IN2OUT_ED_ERROR_ICMP_PACKETS,
1636 vlib_node_increment_counter (vm, stats_node_index,
1637 NAT_IN2OUT_ED_ERROR_OTHER_PACKETS,
1639 vlib_node_increment_counter (vm, stats_node_index,
1640 NAT_IN2OUT_ED_ERROR_FRAGMENTS, fragments);
1642 return frame->n_vectors;
1645 VLIB_NODE_FN (nat44_ed_in2out_node) (vlib_main_t * vm,
1646 vlib_node_runtime_t * node,
1647 vlib_frame_t * frame)
1649 return nat44_ed_in2out_node_fn_inline (vm, node, frame, 0, 0);
1653 VLIB_REGISTER_NODE (nat44_ed_in2out_node) = {
1654 .name = "nat44-ed-in2out",
1655 .vector_size = sizeof (u32),
1656 .format_trace = format_nat_in2out_ed_trace,
1657 .type = VLIB_NODE_TYPE_INTERNAL,
1658 .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
1659 .error_strings = nat_in2out_ed_error_strings,
1660 .runtime_data_bytes = sizeof (snat_runtime_t),
1661 .n_next_nodes = NAT_IN2OUT_ED_N_NEXT,
1663 [NAT_IN2OUT_ED_NEXT_DROP] = "error-drop",
1664 [NAT_IN2OUT_ED_NEXT_LOOKUP] = "ip4-lookup",
1665 [NAT_IN2OUT_ED_NEXT_SLOW_PATH] = "nat44-ed-in2out-slowpath",
1666 [NAT_IN2OUT_ED_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1667 [NAT_IN2OUT_ED_NEXT_REASS] = "nat44-ed-in2out-reass",
1672 VLIB_NODE_FN (nat44_ed_in2out_output_node) (vlib_main_t * vm,
1673 vlib_node_runtime_t * node,
1674 vlib_frame_t * frame)
1676 return nat44_ed_in2out_node_fn_inline (vm, node, frame, 0, 1);
1680 VLIB_REGISTER_NODE (nat44_ed_in2out_output_node) = {
1681 .name = "nat44-ed-in2out-output",
1682 .vector_size = sizeof (u32),
1683 .format_trace = format_nat_in2out_ed_trace,
1684 .type = VLIB_NODE_TYPE_INTERNAL,
1685 .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
1686 .error_strings = nat_in2out_ed_error_strings,
1687 .runtime_data_bytes = sizeof (snat_runtime_t),
1688 .n_next_nodes = NAT_IN2OUT_ED_N_NEXT,
1690 [NAT_IN2OUT_ED_NEXT_DROP] = "error-drop",
1691 [NAT_IN2OUT_ED_NEXT_LOOKUP] = "interface-output",
1692 [NAT_IN2OUT_ED_NEXT_SLOW_PATH] = "nat44-ed-in2out-output-slowpath",
1693 [NAT_IN2OUT_ED_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1694 [NAT_IN2OUT_ED_NEXT_REASS] = "nat44-ed-in2out-reass-output",
1699 VLIB_NODE_FN (nat44_ed_in2out_slowpath_node) (vlib_main_t * vm,
1700 vlib_node_runtime_t * node,
1701 vlib_frame_t * frame)
1703 return nat44_ed_in2out_node_fn_inline (vm, node, frame, 1, 0);
1707 VLIB_REGISTER_NODE (nat44_ed_in2out_slowpath_node) = {
1708 .name = "nat44-ed-in2out-slowpath",
1709 .vector_size = sizeof (u32),
1710 .format_trace = format_nat_in2out_ed_trace,
1711 .type = VLIB_NODE_TYPE_INTERNAL,
1712 .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
1713 .error_strings = nat_in2out_ed_error_strings,
1714 .runtime_data_bytes = sizeof (snat_runtime_t),
1715 .n_next_nodes = NAT_IN2OUT_ED_N_NEXT,
1717 [NAT_IN2OUT_ED_NEXT_DROP] = "error-drop",
1718 [NAT_IN2OUT_ED_NEXT_LOOKUP] = "ip4-lookup",
1719 [NAT_IN2OUT_ED_NEXT_SLOW_PATH] = "nat44-ed-in2out-slowpath",
1720 [NAT_IN2OUT_ED_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1721 [NAT_IN2OUT_ED_NEXT_REASS] = "nat44-ed-in2out-reass",
1726 VLIB_NODE_FN (nat44_ed_in2out_output_slowpath_node) (vlib_main_t * vm,
1727 vlib_node_runtime_t *
1729 vlib_frame_t * frame)
1731 return nat44_ed_in2out_node_fn_inline (vm, node, frame, 1, 1);
1735 VLIB_REGISTER_NODE (nat44_ed_in2out_output_slowpath_node) = {
1736 .name = "nat44-ed-in2out-output-slowpath",
1737 .vector_size = sizeof (u32),
1738 .format_trace = format_nat_in2out_ed_trace,
1739 .type = VLIB_NODE_TYPE_INTERNAL,
1740 .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
1741 .error_strings = nat_in2out_ed_error_strings,
1742 .runtime_data_bytes = sizeof (snat_runtime_t),
1743 .n_next_nodes = NAT_IN2OUT_ED_N_NEXT,
1745 [NAT_IN2OUT_ED_NEXT_DROP] = "error-drop",
1746 [NAT_IN2OUT_ED_NEXT_LOOKUP] = "interface-output",
1747 [NAT_IN2OUT_ED_NEXT_SLOW_PATH] = "nat44-ed-in2out-output-slowpath",
1748 [NAT_IN2OUT_ED_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1749 [NAT_IN2OUT_ED_NEXT_REASS] = "nat44-ed-in2out-reass",
1755 nat44_ed_in2out_reass_node_fn_inline (vlib_main_t * vm,
1756 vlib_node_runtime_t * node,
1757 vlib_frame_t * frame,
1758 int is_output_feature)
1760 u32 n_left_from, *from, *to_next;
1761 nat_in2out_ed_next_t next_index;
1762 u32 pkts_processed = 0, cached_fragments = 0;
1763 snat_main_t *sm = &snat_main;
1764 f64 now = vlib_time_now (vm);
1765 u32 thread_index = vm->thread_index;
1766 snat_main_per_thread_data_t *per_thread_data =
1767 &sm->per_thread_data[thread_index];
1768 u32 *fragments_to_drop = 0;
1769 u32 *fragments_to_loopback = 0;
1771 from = vlib_frame_vector_args (frame);
1772 n_left_from = frame->n_vectors;
1773 next_index = node->cached_next_index;
1775 while (n_left_from > 0)
1779 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1781 while (n_left_from > 0 && n_left_to_next > 0)
1783 u32 bi0, sw_if_index0, proto0, rx_fib_index0, new_addr0, old_addr0;
1784 u32 iph_offset0 = 0;
1788 ip4_header_t *ip0 = 0;
1789 nat_reass_ip4_t *reass0;
1792 icmp46_header_t *icmp0;
1793 clib_bihash_kv_16_8_t kv0, value0;
1794 snat_session_t *s0 = 0;
1795 u16 old_port0, new_port0;
1798 /* speculatively enqueue b0 to the current next frame */
1804 n_left_to_next -= 1;
1806 b0 = vlib_get_buffer (vm, bi0);
1808 next0 = NAT_IN2OUT_ED_NEXT_LOOKUP;
1810 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1812 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1815 if (PREDICT_FALSE (nat_reass_is_drop_frag (0)))
1817 next0 = NAT_IN2OUT_ED_NEXT_DROP;
1818 b0->error = node->errors[NAT_IN2OUT_ED_ERROR_DROP_FRAGMENT];
1822 if (is_output_feature)
1823 iph_offset0 = vnet_buffer (b0)->ip.save_rewrite_length;
1825 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
1828 udp0 = ip4_next_header (ip0);
1829 tcp0 = (tcp_header_t *) udp0;
1830 icmp0 = (icmp46_header_t *) udp0;
1831 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1833 reass0 = nat_ip4_reass_find_or_create (ip0->src_address,
1837 1, &fragments_to_drop);
1839 if (PREDICT_FALSE (!reass0))
1841 next0 = NAT_IN2OUT_ED_NEXT_DROP;
1842 b0->error = node->errors[NAT_IN2OUT_ED_ERROR_MAX_REASS];
1843 nat_elog_notice ("maximum reassemblies exceeded");
1847 if (PREDICT_FALSE (ip4_is_first_fragment (ip0)))
1849 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1851 if (is_output_feature)
1854 (nat_not_translate_output_feature_fwd
1855 (sm, ip0, thread_index, now, vm, b0)))
1856 reass0->flags |= NAT_REASS_FLAG_ED_DONT_TRANSLATE;
1860 next0 = icmp_in2out_ed_slow_path
1861 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1862 next0, now, thread_index, &s0);
1864 if (PREDICT_TRUE (next0 != NAT_IN2OUT_ED_NEXT_DROP))
1867 reass0->sess_index = s0 - per_thread_data->sessions;
1869 reass0->flags |= NAT_REASS_FLAG_ED_DONT_TRANSLATE;
1870 nat_ip4_reass_get_frags (reass0,
1871 &fragments_to_loopback);
1877 make_ed_kv (&kv0, &ip0->src_address, &ip0->dst_address,
1878 ip0->protocol, rx_fib_index0, udp0->src_port,
1881 if (clib_bihash_search_16_8
1882 (&per_thread_data->in2out_ed, &kv0, &value0))
1884 if (is_output_feature)
1887 (nat44_ed_not_translate_output_feature
1888 (sm, ip0, ip0->protocol, udp0->src_port,
1889 udp0->dst_port, thread_index, sw_if_index0,
1890 vnet_buffer (b0)->sw_if_index[VLIB_TX])))
1892 reass0->flags |= NAT_REASS_FLAG_ED_DONT_TRANSLATE;
1893 nat_ip4_reass_get_frags (reass0,
1894 &fragments_to_loopback);
1900 if (PREDICT_FALSE (nat44_ed_not_translate (sm, node,
1906 reass0->flags |= NAT_REASS_FLAG_ED_DONT_TRANSLATE;
1907 nat_ip4_reass_get_frags (reass0,
1908 &fragments_to_loopback);
1913 next0 = slow_path_ed (sm, b0, rx_fib_index0, &kv0,
1914 &s0, node, next0, thread_index, now,
1917 if (PREDICT_FALSE (next0 == NAT_IN2OUT_ED_NEXT_DROP))
1920 if (PREDICT_FALSE (!s0))
1922 reass0->flags |= NAT_REASS_FLAG_ED_DONT_TRANSLATE;
1926 reass0->sess_index = s0 - per_thread_data->sessions;
1930 s0 = pool_elt_at_index (per_thread_data->sessions,
1932 reass0->sess_index = value0.value;
1934 nat_ip4_reass_get_frags (reass0, &fragments_to_loopback);
1938 if (reass0->flags & NAT_REASS_FLAG_ED_DONT_TRANSLATE)
1940 if (PREDICT_FALSE (reass0->sess_index == (u32) ~ 0))
1942 if (nat_ip4_reass_add_fragment
1943 (thread_index, reass0, bi0, &fragments_to_drop))
1945 b0->error = node->errors[NAT_IN2OUT_ED_ERROR_MAX_FRAG];
1947 ("maximum fragments per reassembly exceeded");
1948 next0 = NAT_IN2OUT_ED_NEXT_DROP;
1954 s0 = pool_elt_at_index (per_thread_data->sessions,
1955 reass0->sess_index);
1958 old_addr0 = ip0->src_address.as_u32;
1959 ip0->src_address = s0->out2in.addr;
1960 new_addr0 = ip0->src_address.as_u32;
1961 if (!is_output_feature)
1962 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1964 sum0 = ip0->checksum;
1965 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1967 src_address /* changed member */ );
1968 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1969 sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
1970 s0->ext_host_addr.as_u32, ip4_header_t,
1972 ip0->checksum = ip_csum_fold (sum0);
1974 if (PREDICT_FALSE (ip4_is_first_fragment (ip0)))
1976 old_port0 = udp0->src_port;
1977 new_port0 = udp0->src_port = s0->out2in.port;
1979 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1981 sum0 = tcp0->checksum;
1982 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1984 dst_address /* changed member */ );
1985 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1986 ip4_header_t /* cheat */ ,
1987 length /* changed member */ );
1988 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1990 sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
1991 s0->ext_host_addr.as_u32,
1992 ip4_header_t, dst_address);
1993 sum0 = ip_csum_update (sum0, tcp0->dst_port,
1994 s0->ext_host_port, ip4_header_t,
1996 tcp0->dst_port = s0->ext_host_port;
1997 ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
1999 tcp0->checksum = ip_csum_fold (sum0);
2001 else if (udp0->checksum)
2003 sum0 = udp0->checksum;
2005 ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
2008 ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
2010 if (PREDICT_FALSE (is_twice_nat_session (s0)))
2012 sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
2013 s0->ext_host_addr.as_u32,
2014 ip4_header_t, dst_address);
2015 sum0 = ip_csum_update (sum0, tcp0->dst_port,
2016 s0->ext_host_port, ip4_header_t,
2018 udp0->dst_port = s0->ext_host_port;
2019 ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
2021 udp0->checksum = ip_csum_fold (sum0);
2025 if (PREDICT_FALSE (is_twice_nat_session (s0)))
2027 udp0->dst_port = s0->ext_host_port;
2028 ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
2034 nat44_reass_hairpinning (sm, b0, ip0, s0->out2in.port,
2035 s0->ext_host_port, proto0, 1);
2038 nat44_session_update_counters (s0, now,
2039 vlib_buffer_length_in_chain (vm, b0),
2041 /* Per-user LRU list maintenance */
2042 nat44_session_update_lru (sm, s0, thread_index);
2045 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
2046 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
2048 nat44_reass_trace_t *t =
2049 vlib_add_trace (vm, node, b0, sizeof (*t));
2050 t->cached = cached0;
2051 t->sw_if_index = sw_if_index0;
2052 t->next_index = next0;
2063 pkts_processed += next0 != NAT_IN2OUT_ED_NEXT_DROP;
2065 /* verify speculative enqueue, maybe switch current next frame */
2066 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2067 to_next, n_left_to_next,
2071 if (n_left_from == 0 && vec_len (fragments_to_loopback))
2073 from = vlib_frame_vector_args (frame);
2074 u32 len = vec_len (fragments_to_loopback);
2075 if (len <= VLIB_FRAME_SIZE)
2077 clib_memcpy_fast (from, fragments_to_loopback,
2078 sizeof (u32) * len);
2080 vec_reset_length (fragments_to_loopback);
2084 clib_memcpy_fast (from, fragments_to_loopback +
2085 (len - VLIB_FRAME_SIZE),
2086 sizeof (u32) * VLIB_FRAME_SIZE);
2087 n_left_from = VLIB_FRAME_SIZE;
2088 _vec_len (fragments_to_loopback) = len - VLIB_FRAME_SIZE;
2093 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2096 vlib_node_increment_counter (vm, sm->ed_in2out_reass_node_index,
2097 NAT_IN2OUT_ED_ERROR_PROCESSED_FRAGMENTS,
2099 vlib_node_increment_counter (vm, sm->ed_in2out_reass_node_index,
2100 NAT_IN2OUT_ED_ERROR_CACHED_FRAGMENTS,
2103 nat_send_all_to_node (vm, fragments_to_drop, node,
2104 &node->errors[NAT_IN2OUT_ED_ERROR_DROP_FRAGMENT],
2105 NAT_IN2OUT_ED_NEXT_DROP);
2107 vec_free (fragments_to_drop);
2108 vec_free (fragments_to_loopback);
2109 return frame->n_vectors;
2112 VLIB_NODE_FN (nat44_ed_in2out_reass_node) (vlib_main_t * vm,
2113 vlib_node_runtime_t * node,
2114 vlib_frame_t * frame)
2116 return nat44_ed_in2out_reass_node_fn_inline (vm, node, frame, 0);
2120 VLIB_REGISTER_NODE (nat44_ed_in2out_reass_node) = {
2121 .name = "nat44-ed-in2out-reass",
2122 .vector_size = sizeof (u32),
2123 .format_trace = format_nat44_reass_trace,
2124 .type = VLIB_NODE_TYPE_INTERNAL,
2125 .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
2126 .error_strings = nat_in2out_ed_error_strings,
2127 .n_next_nodes = NAT_IN2OUT_ED_N_NEXT,
2129 [NAT_IN2OUT_ED_NEXT_DROP] = "error-drop",
2130 [NAT_IN2OUT_ED_NEXT_LOOKUP] = "ip4-lookup",
2131 [NAT_IN2OUT_ED_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
2132 [NAT_IN2OUT_ED_NEXT_ICMP_ERROR] = "ip4-icmp-error",
2133 [NAT_IN2OUT_ED_NEXT_REASS] = "nat44-ed-in2out-reass",
2138 VLIB_NODE_FN (nat44_ed_in2out_reass_output_node) (vlib_main_t * vm,
2139 vlib_node_runtime_t * node,
2140 vlib_frame_t * frame)
2142 return nat44_ed_in2out_reass_node_fn_inline (vm, node, frame, 1);
2146 VLIB_REGISTER_NODE (nat44_ed_in2out_reass_output_node) = {
2147 .name = "nat44-ed-in2out-reass-output",
2148 .vector_size = sizeof (u32),
2149 .format_trace = format_nat44_reass_trace,
2150 .type = VLIB_NODE_TYPE_INTERNAL,
2151 .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
2152 .error_strings = nat_in2out_ed_error_strings,
2153 .n_next_nodes = NAT_IN2OUT_ED_N_NEXT,
2155 [NAT_IN2OUT_ED_NEXT_DROP] = "error-drop",
2156 [NAT_IN2OUT_ED_NEXT_LOOKUP] = "interface-output",
2157 [NAT_IN2OUT_ED_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
2158 [NAT_IN2OUT_ED_NEXT_ICMP_ERROR] = "ip4-icmp-error",
2159 [NAT_IN2OUT_ED_NEXT_REASS] = "nat44-ed-in2out-reass",
2165 * fd.io coding-style-patch-verification: ON
2168 * eval: (c-set-style "gnu")