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 <vnet/udp/udp.h>
27 #include <vppinfra/error.h>
29 #include <nat/nat_ipfix_logging.h>
30 #include <nat/nat_inlines.h>
31 #include <nat/nat44/inlines.h>
32 #include <nat/nat_syslog.h>
33 #include <nat/nat_ha.h>
35 static char *nat_in2out_ed_error_strings[] = {
36 #define _(sym,string) string,
37 foreach_nat_in2out_ed_error
47 } nat_in2out_ed_trace_t;
50 format_nat_in2out_ed_trace (u8 * s, va_list * args)
52 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
53 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
54 nat_in2out_ed_trace_t *t = va_arg (*args, nat_in2out_ed_trace_t *);
58 t->is_slow_path ? "NAT44_IN2OUT_ED_SLOW_PATH" :
59 "NAT44_IN2OUT_ED_FAST_PATH";
61 s = format (s, "%s: sw_if_index %d, next index %d, session %d", tag,
62 t->sw_if_index, t->next_index, t->session_index);
67 #ifndef CLIB_MARCH_VARIANT
69 nat44_i2o_ed_is_idle_session_cb (clib_bihash_kv_16_8_t * kv, void *arg)
71 snat_main_t *sm = &snat_main;
72 nat44_is_idle_session_ctx_t *ctx = arg;
74 u64 sess_timeout_time;
75 nat_ed_ses_key_t ed_key;
76 clib_bihash_kv_16_8_t ed_kv;
79 snat_session_key_t key;
80 snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
83 s = pool_elt_at_index (tsm->sessions, kv->value);
84 sess_timeout_time = s->last_heard + (f64) nat44_session_get_timeout (sm, s);
85 if (ctx->now >= sess_timeout_time)
87 if (is_fwd_bypass_session (s))
90 ed_key.l_addr = s->out2in.addr;
91 ed_key.r_addr = s->ext_host_addr;
92 ed_key.fib_index = s->out2in.fib_index;
93 if (snat_is_unk_proto_session (s))
95 ed_key.proto = s->in2out.port;
101 ed_key.proto = snat_proto_to_ip_proto (s->in2out.protocol);
102 ed_key.l_port = s->out2in.port;
103 ed_key.r_port = s->ext_host_port;
105 ed_kv.key[0] = ed_key.as_u64[0];
106 ed_kv.key[1] = ed_key.as_u64[1];
107 if (clib_bihash_add_del_16_8 (&tsm->out2in_ed, &ed_kv, 0))
108 nat_elog_warn ("out2in_ed key del failed");
110 if (snat_is_unk_proto_session (s))
113 snat_ipfix_logging_nat44_ses_delete (ctx->thread_index,
114 s->in2out.addr.as_u32,
115 s->out2in.addr.as_u32,
119 s->in2out.fib_index);
121 nat_syslog_nat44_sdel (s->user_index, s->in2out.fib_index,
122 &s->in2out.addr, s->in2out.port,
123 &s->ext_host_nat_addr, s->ext_host_nat_port,
124 &s->out2in.addr, s->out2in.port,
125 &s->ext_host_addr, s->ext_host_port,
126 s->in2out.protocol, is_twice_nat_session (s));
128 nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
129 s->ext_host_port, s->out2in.protocol, s->out2in.fib_index,
132 if (is_twice_nat_session (s))
134 for (i = 0; i < vec_len (sm->twice_nat_addresses); i++)
136 key.protocol = s->in2out.protocol;
137 key.port = s->ext_host_nat_port;
138 a = sm->twice_nat_addresses + i;
139 if (a->addr.as_u32 == s->ext_host_nat_addr.as_u32)
141 snat_free_outside_address_and_port (sm->twice_nat_addresses,
149 if (snat_is_session_static (s))
152 snat_free_outside_address_and_port (sm->addresses, ctx->thread_index,
155 nat44_delete_session (sm, s, ctx->thread_index);
164 icmp_in2out_ed_slow_path (snat_main_t * sm, vlib_buffer_t * b0,
165 ip4_header_t * ip0, icmp46_header_t * icmp0,
166 u32 sw_if_index0, u32 rx_fib_index0,
167 vlib_node_runtime_t * node, u32 next0, f64 now,
168 u32 thread_index, snat_session_t ** p_s0)
170 next0 = icmp_in2out (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
171 next0, thread_index, p_s0, 0);
172 snat_session_t *s0 = *p_s0;
173 if (PREDICT_TRUE (next0 != NAT_NEXT_DROP && s0))
176 nat44_session_update_counters (s0, now,
177 vlib_buffer_length_in_chain
178 (sm->vlib_main, b0), thread_index);
179 /* Per-user LRU list maintenance */
180 nat44_session_update_lru (sm, s0, thread_index);
186 slow_path_ed (snat_main_t * sm,
189 clib_bihash_kv_16_8_t * kv,
190 snat_session_t ** sessionp,
191 vlib_node_runtime_t * node, u32 next, u32 thread_index, f64 now)
193 snat_session_t *s = 0;
195 snat_session_key_t key0, key1;
196 lb_nat_type_t lb = 0, is_sm = 0;
197 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
198 nat_ed_ses_key_t *key = (nat_ed_ses_key_t *) kv->key;
199 u32 proto = ip_proto_to_snat_proto (key->proto);
200 nat_outside_fib_t *outside_fib;
201 fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
204 .fp_proto = FIB_PROTOCOL_IP4,
207 .ip4.as_u32 = key->r_addr.as_u32,
210 nat44_is_idle_session_ctx_t ctx;
212 nat44_session_try_cleanup (&key->l_addr, rx_fib_index, thread_index, now);
214 if (PREDICT_FALSE (nat44_maximum_sessions_exceeded (sm, thread_index)))
216 b->error = node->errors[NAT_IN2OUT_ED_ERROR_MAX_SESSIONS_EXCEEDED];
217 nat_ipfix_logging_max_sessions (thread_index, sm->max_translations);
218 nat_elog_notice ("maximum sessions exceeded");
219 return NAT_NEXT_DROP;
222 key0.addr = key->l_addr;
223 key0.port = key->l_port;
224 key1.protocol = key0.protocol = proto;
225 key0.fib_index = rx_fib_index;
226 key1.fib_index = sm->outside_fib_index;
227 /* First try to match static mapping by local address and port */
228 if (snat_static_mapping_match
229 (sm, key0, &key1, 0, 0, 0, &lb, 0, &identity_nat))
231 /* Try to create dynamic translation */
232 if (snat_alloc_outside_address_and_port (sm->addresses, rx_fib_index,
235 tsm->snat_thread_index))
237 nat_elog_notice ("addresses exhausted");
238 b->error = node->errors[NAT_IN2OUT_ED_ERROR_OUT_OF_PORTS];
239 return NAT_NEXT_DROP;
244 if (PREDICT_FALSE (identity_nat))
253 if (proto == SNAT_PROTOCOL_TCP)
255 if (!tcp_flags_is_init
256 (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags))
258 b->error = node->errors[NAT_IN2OUT_ED_ERROR_NON_SYN];
259 return NAT_NEXT_DROP;
263 u = nat_user_get_or_create (sm, &key->l_addr, rx_fib_index, thread_index);
266 nat_elog_warn ("create NAT user failed");
268 snat_free_outside_address_and_port (sm->addresses,
269 thread_index, &key1);
270 return NAT_NEXT_DROP;
273 s = nat_ed_session_alloc (sm, u, thread_index, now);
276 nat44_delete_user_with_no_session (sm, u, thread_index);
277 nat_elog_warn ("create NAT session failed");
279 snat_free_outside_address_and_port (sm->addresses,
280 thread_index, &key1);
281 return NAT_NEXT_DROP;
284 user_session_increment (sm, u, is_sm);
286 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
288 s->flags |= SNAT_SESSION_FLAG_LOAD_BALANCING;
289 s->flags |= SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT;
290 s->ext_host_addr = key->r_addr;
291 s->ext_host_port = key->r_port;
294 s->out2in.protocol = key0.protocol;
296 switch (vec_len (sm->outside_fibs))
299 s->out2in.fib_index = sm->outside_fib_index;
302 s->out2in.fib_index = sm->outside_fibs[0].fib_index;
306 vec_foreach (outside_fib, sm->outside_fibs)
308 fei = fib_table_lookup (outside_fib->fib_index, &pfx);
309 if (FIB_NODE_INDEX_INVALID != fei)
311 if (fib_entry_get_resolving_interface (fei) != ~0)
313 s->out2in.fib_index = outside_fib->fib_index;
322 /* Add to lookup tables */
323 kv->value = s - tsm->sessions;
325 ctx.thread_index = thread_index;
326 if (clib_bihash_add_or_overwrite_stale_16_8 (&tsm->in2out_ed, kv,
327 nat44_i2o_ed_is_idle_session_cb,
329 nat_elog_notice ("in2out-ed key add failed");
331 make_ed_kv (kv, &key1.addr, &key->r_addr, key->proto, s->out2in.fib_index,
332 key1.port, key->r_port);
333 kv->value = s - tsm->sessions;
334 if (clib_bihash_add_or_overwrite_stale_16_8 (&tsm->out2in_ed, kv,
335 nat44_o2i_ed_is_idle_session_cb,
337 nat_elog_notice ("out2in-ed key add failed");
342 snat_ipfix_logging_nat44_ses_create (thread_index,
343 s->in2out.addr.as_u32,
344 s->out2in.addr.as_u32,
347 s->out2in.port, s->in2out.fib_index);
349 nat_syslog_nat44_sadd (s->user_index, s->in2out.fib_index,
350 &s->in2out.addr, s->in2out.port,
351 &s->ext_host_nat_addr, s->ext_host_nat_port,
352 &s->out2in.addr, s->out2in.port,
353 &s->ext_host_addr, s->ext_host_port,
354 s->in2out.protocol, 0);
356 nat_ha_sadd (&s->in2out.addr, s->in2out.port, &s->out2in.addr,
357 s->out2in.port, &s->ext_host_addr, s->ext_host_port,
358 &s->ext_host_nat_addr, s->ext_host_nat_port,
359 s->in2out.protocol, s->in2out.fib_index, s->flags,
365 static_always_inline int
366 nat44_ed_not_translate (snat_main_t * sm, vlib_node_runtime_t * node,
367 u32 sw_if_index, ip4_header_t * ip, u32 proto,
368 u32 rx_fib_index, u32 thread_index)
370 udp_header_t *udp = ip4_next_header (ip);
371 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
372 clib_bihash_kv_16_8_t kv, value;
373 snat_session_key_t key0, key1;
375 make_ed_kv (&kv, &ip->dst_address, &ip->src_address, ip->protocol,
376 sm->outside_fib_index, udp->dst_port, udp->src_port);
378 /* NAT packet aimed at external address if */
379 /* has active sessions */
380 if (clib_bihash_search_16_8 (&tsm->out2in_ed, &kv, &value))
382 key0.addr = ip->dst_address;
383 key0.port = udp->dst_port;
384 key0.protocol = proto;
385 key0.fib_index = sm->outside_fib_index;
386 /* or is static mappings */
387 if (!snat_static_mapping_match (sm, key0, &key1, 1, 0, 0, 0, 0, 0))
393 if (sm->forwarding_enabled)
396 return snat_not_translate_fast (sm, node, sw_if_index, ip, proto,
400 static_always_inline int
401 nat_not_translate_output_feature_fwd (snat_main_t * sm, ip4_header_t * ip,
402 u32 thread_index, f64 now,
403 vlib_main_t * vm, vlib_buffer_t * b)
405 nat_ed_ses_key_t key;
406 clib_bihash_kv_16_8_t kv, value;
407 snat_session_t *s = 0;
408 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
410 if (!sm->forwarding_enabled)
413 if (ip->protocol == IP_PROTOCOL_ICMP)
415 key.as_u64[0] = key.as_u64[1] = 0;
416 if (get_icmp_i2o_ed_key (b, ip, &key))
419 kv.key[0] = key.as_u64[0];
420 kv.key[1] = key.as_u64[1];
422 else if (ip->protocol == IP_PROTOCOL_UDP || ip->protocol == IP_PROTOCOL_TCP)
424 make_ed_kv (&kv, &ip->src_address, &ip->dst_address, ip->protocol, 0,
425 vnet_buffer (b)->ip.reass.l4_src_port,
426 vnet_buffer (b)->ip.reass.l4_dst_port);
430 make_ed_kv (&kv, &ip->src_address, &ip->dst_address, ip->protocol, 0, 0,
434 if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
436 s = pool_elt_at_index (tsm->sessions, value.value);
437 if (is_fwd_bypass_session (s))
439 if (ip->protocol == IP_PROTOCOL_TCP)
441 if (nat44_set_tcp_session_state_i2o (sm, s, b, thread_index))
445 nat44_session_update_counters (s, now,
446 vlib_buffer_length_in_chain (vm, b),
448 /* Per-user LRU list maintenance */
449 nat44_session_update_lru (sm, s, thread_index);
459 static_always_inline int
460 nat44_ed_not_translate_output_feature (snat_main_t * sm, ip4_header_t * ip,
461 u8 proto, u16 src_port, u16 dst_port,
462 u32 thread_index, u32 rx_sw_if_index,
465 clib_bihash_kv_16_8_t kv, value;
466 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
469 u32 rx_fib_index = ip4_fib_table_get_index_for_sw_if_index (rx_sw_if_index);
470 u32 tx_fib_index = ip4_fib_table_get_index_for_sw_if_index (tx_sw_if_index);
473 make_ed_kv (&kv, &ip->src_address, &ip->dst_address, proto, tx_fib_index,
475 if (!clib_bihash_search_16_8 (&tsm->out2in_ed, &kv, &value))
477 s = pool_elt_at_index (tsm->sessions, value.value);
478 if (nat44_is_ses_closed (s))
480 nat_free_session_data (sm, s, thread_index, 0);
481 nat44_delete_session (sm, s, thread_index);
484 s->flags |= SNAT_SESSION_FLAG_OUTPUT_FEATURE;
489 make_ed_kv (&kv, &ip->dst_address, &ip->src_address, proto, rx_fib_index,
491 if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
493 s = pool_elt_at_index (tsm->sessions, value.value);
494 if (is_fwd_bypass_session (s))
499 pool_foreach (i, sm->output_feature_interfaces,
501 if ((nat_interface_is_inside (i)) && (rx_sw_if_index == i->sw_if_index))
511 #ifndef CLIB_MARCH_VARIANT
513 icmp_match_in2out_ed (snat_main_t * sm, vlib_node_runtime_t * node,
514 u32 thread_index, vlib_buffer_t * b, ip4_header_t * ip,
515 u8 * p_proto, snat_session_key_t * p_value,
516 u8 * p_dont_translate, void *d, void *e)
520 nat_ed_ses_key_t key;
521 snat_session_t *s = 0;
522 u8 dont_translate = 0;
523 clib_bihash_kv_16_8_t kv, value;
526 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
528 sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
529 rx_fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
531 key.as_u64[0] = key.as_u64[1] = 0;
532 err = get_icmp_i2o_ed_key (b, ip, &key);
535 b->error = node->errors[err];
536 next = NAT_NEXT_DROP;
539 key.fib_index = rx_fib_index;
541 kv.key[0] = key.as_u64[0];
542 kv.key[1] = key.as_u64[1];
544 if (clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
546 if (vnet_buffer (b)->sw_if_index[VLIB_TX] != ~0)
549 (nat44_ed_not_translate_output_feature
550 (sm, ip, key.proto, key.l_port, key.r_port, thread_index,
551 sw_if_index, vnet_buffer (b)->sw_if_index[VLIB_TX])))
559 if (PREDICT_FALSE (nat44_ed_not_translate (sm, node, sw_if_index,
560 ip, SNAT_PROTOCOL_ICMP,
570 (icmp_type_is_error_message
571 (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags)))
573 b->error = node->errors[NAT_IN2OUT_ED_ERROR_BAD_ICMP_TYPE];
574 next = NAT_NEXT_DROP;
578 next = slow_path_ed (sm, b, rx_fib_index, &kv, &s, node, next,
579 thread_index, vlib_time_now (sm->vlib_main));
581 if (PREDICT_FALSE (next == NAT_NEXT_DROP))
593 (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags !=
595 && vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags !=
597 && !icmp_type_is_error_message (vnet_buffer (b)->ip.
598 reass.icmp_type_or_tcp_flags)))
600 b->error = node->errors[NAT_IN2OUT_ED_ERROR_BAD_ICMP_TYPE];
601 next = NAT_NEXT_DROP;
605 s = pool_elt_at_index (tsm->sessions, value.value);
608 *p_proto = ip_proto_to_snat_proto (key.proto);
611 *p_value = s->out2in;
612 *p_dont_translate = dont_translate;
614 *(snat_session_t **) d = s;
619 static snat_session_t *
620 nat44_ed_in2out_unknown_proto (snat_main_t * sm,
626 vlib_main_t * vm, vlib_node_runtime_t * node)
628 clib_bihash_kv_8_8_t kv, value;
629 clib_bihash_kv_16_8_t s_kv, s_value;
630 snat_static_mapping_t *m;
631 u32 old_addr, new_addr = 0;
634 dlist_elt_t *head, *elt;
635 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
636 u32 elt_index, head_index, ses_index;
638 u32 outside_fib_index = sm->outside_fib_index;
641 nat_outside_fib_t *outside_fib;
642 fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
644 .fp_proto = FIB_PROTOCOL_IP4,
647 .ip4.as_u32 = ip->dst_address.as_u32,
651 switch (vec_len (sm->outside_fibs))
654 outside_fib_index = sm->outside_fib_index;
657 outside_fib_index = sm->outside_fibs[0].fib_index;
661 vec_foreach (outside_fib, sm->outside_fibs)
663 fei = fib_table_lookup (outside_fib->fib_index, &pfx);
664 if (FIB_NODE_INDEX_INVALID != fei)
666 if (fib_entry_get_resolving_interface (fei) != ~0)
668 outside_fib_index = outside_fib->fib_index;
676 old_addr = ip->src_address.as_u32;
678 make_ed_kv (&s_kv, &ip->src_address, &ip->dst_address, ip->protocol,
681 if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &s_kv, &s_value))
683 s = pool_elt_at_index (tsm->sessions, s_value.value);
684 new_addr = ip->src_address.as_u32 = s->out2in.addr.as_u32;
688 if (PREDICT_FALSE (nat44_maximum_sessions_exceeded (sm, thread_index)))
690 b->error = node->errors[NAT_IN2OUT_ED_ERROR_MAX_SESSIONS_EXCEEDED];
691 nat_ipfix_logging_max_sessions (thread_index, sm->max_translations);
692 nat_elog_notice ("maximum sessions exceeded");
696 u = nat_user_get_or_create (sm, &ip->src_address, rx_fib_index,
700 nat_elog_warn ("create NAT user failed");
704 make_sm_kv (&kv, &ip->src_address, 0, rx_fib_index, 0);
706 /* Try to find static mapping first */
707 if (!clib_bihash_search_8_8 (&sm->static_mapping_by_local, &kv, &value))
709 m = pool_elt_at_index (sm->static_mappings, value.value);
710 new_addr = ip->src_address.as_u32 = m->external_addr.as_u32;
714 /* Fallback to 3-tuple key */
717 /* Choose same out address as for TCP/UDP session to same destination */
718 head_index = u->sessions_per_user_list_head_index;
719 head = pool_elt_at_index (tsm->list_pool, head_index);
720 elt_index = head->next;
721 if (PREDICT_FALSE (elt_index == ~0))
725 elt = pool_elt_at_index (tsm->list_pool, elt_index);
726 ses_index = elt->value;
729 while (ses_index != ~0)
731 s = pool_elt_at_index (tsm->sessions, ses_index);
732 elt_index = elt->next;
733 elt = pool_elt_at_index (tsm->list_pool, elt_index);
734 ses_index = elt->value;
736 if (s->ext_host_addr.as_u32 == ip->dst_address.as_u32)
738 new_addr = ip->src_address.as_u32 = s->out2in.addr.as_u32;
740 make_ed_kv (&s_kv, &s->out2in.addr, &ip->dst_address,
741 ip->protocol, outside_fib_index, 0, 0);
742 if (clib_bihash_search_16_8
743 (&tsm->out2in_ed, &s_kv, &s_value))
750 for (i = 0; i < vec_len (sm->addresses); i++)
752 make_ed_kv (&s_kv, &sm->addresses[i].addr, &ip->dst_address,
753 ip->protocol, outside_fib_index, 0, 0);
754 if (clib_bihash_search_16_8 (&tsm->out2in_ed, &s_kv, &s_value))
756 new_addr = ip->src_address.as_u32 =
757 sm->addresses[i].addr.as_u32;
765 s = nat_ed_session_alloc (sm, u, thread_index, now);
768 nat44_delete_user_with_no_session (sm, u, thread_index);
769 nat_elog_warn ("create NAT session failed");
773 s->ext_host_addr.as_u32 = ip->dst_address.as_u32;
774 s->flags |= SNAT_SESSION_FLAG_UNKNOWN_PROTO;
775 s->flags |= SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT;
776 s->out2in.addr.as_u32 = new_addr;
777 s->out2in.fib_index = outside_fib_index;
778 s->in2out.addr.as_u32 = old_addr;
779 s->in2out.fib_index = rx_fib_index;
780 s->in2out.port = s->out2in.port = ip->protocol;
782 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
783 user_session_increment (sm, u, is_sm);
785 /* Add to lookup tables */
786 make_ed_kv (&s_kv, &s->in2out.addr, &ip->dst_address, ip->protocol,
788 s_kv.value = s - tsm->sessions;
789 if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &s_kv, 1))
790 nat_elog_notice ("in2out key add failed");
792 make_ed_kv (&s_kv, &s->out2in.addr, &ip->dst_address, ip->protocol,
793 outside_fib_index, 0, 0);
794 s_kv.value = s - tsm->sessions;
795 if (clib_bihash_add_del_16_8 (&tsm->out2in_ed, &s_kv, 1))
796 nat_elog_notice ("out2in key add failed");
799 /* Update IP checksum */
801 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, src_address);
802 ip->checksum = ip_csum_fold (sum);
805 nat44_session_update_counters (s, now, vlib_buffer_length_in_chain (vm, b),
807 /* Per-user LRU list maintenance */
808 nat44_session_update_lru (sm, s, thread_index);
811 if (vnet_buffer (b)->sw_if_index[VLIB_TX] == ~0)
812 nat44_ed_hairpinning_unknown_proto (sm, b, ip);
814 if (vnet_buffer (b)->sw_if_index[VLIB_TX] == ~0)
815 vnet_buffer (b)->sw_if_index[VLIB_TX] = outside_fib_index;
821 nat44_ed_in2out_fast_path_node_fn_inline (vlib_main_t * vm,
822 vlib_node_runtime_t * node,
823 vlib_frame_t * frame,
824 int is_output_feature)
826 u32 n_left_from, *from, *to_next, pkts_processed = 0, stats_node_index;
827 nat_next_t next_index;
828 snat_main_t *sm = &snat_main;
829 f64 now = vlib_time_now (vm);
830 u32 thread_index = vm->thread_index;
831 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
832 u32 tcp_packets = 0, udp_packets = 0, icmp_packets = 0, other_packets =
835 def_slow = is_output_feature ? NAT_NEXT_IN2OUT_ED_OUTPUT_SLOW_PATH :
836 NAT_NEXT_IN2OUT_ED_SLOW_PATH;
838 stats_node_index = sm->ed_in2out_node_index;
840 from = vlib_frame_vector_args (frame);
841 n_left_from = frame->n_vectors;
842 next_index = node->cached_next_index;
844 while (n_left_from > 0)
848 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
850 while (n_left_from > 0 && n_left_to_next > 0)
854 u32 next0, sw_if_index0, rx_fib_index0, iph_offset0 = 0, proto0,
855 new_addr0, old_addr0;
856 u16 old_port0, new_port0;
860 snat_session_t *s0 = 0;
861 clib_bihash_kv_16_8_t kv0, value0;
864 /* speculatively enqueue b0 to the current next frame */
872 b0 = vlib_get_buffer (vm, bi0);
874 if (is_output_feature)
876 vnet_feature_next (&nat_buffer_opaque (b0)->arc_next, b0);
877 iph_offset0 = vnet_buffer (b0)->ip.reass.save_rewrite_length;
880 next0 = nat_buffer_opaque (b0)->arc_next;
882 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
885 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
887 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
890 if (PREDICT_FALSE (ip0->ttl == 1))
892 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
893 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
894 ICMP4_time_exceeded_ttl_exceeded_in_transit,
896 next0 = NAT_NEXT_ICMP_ERROR;
900 udp0 = ip4_next_header (ip0);
901 tcp0 = (tcp_header_t *) udp0;
902 proto0 = ip_proto_to_snat_proto (ip0->protocol);
904 if (PREDICT_FALSE (proto0 == ~0))
910 if (is_output_feature)
912 if (PREDICT_FALSE (nat_not_translate_output_feature_fwd
913 (sm, ip0, thread_index, now, vm, b0)))
917 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
923 make_ed_kv (&kv0, &ip0->src_address, &ip0->dst_address,
924 ip0->protocol, rx_fib_index0,
925 vnet_buffer (b0)->ip.reass.l4_src_port,
926 vnet_buffer (b0)->ip.reass.l4_dst_port);
928 // lookup for session
929 if (clib_bihash_search_16_8 (&tsm->in2out_ed, &kv0, &value0))
931 // session does not exist go slow path
936 s0 = pool_elt_at_index (tsm->sessions, value0.value);
938 b0->flags |= VNET_BUFFER_F_IS_NATED;
940 if (!is_output_feature)
941 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
943 old_addr0 = ip0->src_address.as_u32;
944 new_addr0 = ip0->src_address.as_u32 = s0->out2in.addr.as_u32;
945 sum0 = ip0->checksum;
946 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
948 if (PREDICT_FALSE (is_twice_nat_session (s0)))
949 sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
950 s0->ext_host_addr.as_u32, ip4_header_t,
952 ip0->checksum = ip_csum_fold (sum0);
954 old_port0 = vnet_buffer (b0)->ip.reass.l4_src_port;
956 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
958 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
960 new_port0 = udp0->src_port = s0->out2in.port;
961 sum0 = tcp0->checksum;
963 ip_csum_update (sum0, old_addr0, new_addr0,
964 ip4_header_t, dst_address);
966 ip_csum_update (sum0, old_port0, new_port0,
967 ip4_header_t, length);
968 if (PREDICT_FALSE (is_twice_nat_session (s0)))
971 ip_csum_update (sum0, ip0->dst_address.as_u32,
972 s0->ext_host_addr.as_u32,
973 ip4_header_t, dst_address);
975 ip_csum_update (sum0,
976 vnet_buffer (b0)->ip.
977 reass.l4_dst_port, s0->ext_host_port,
978 ip4_header_t, length);
979 tcp0->dst_port = s0->ext_host_port;
980 ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
982 mss_clamping (sm, tcp0, &sum0);
983 tcp0->checksum = ip_csum_fold (sum0);
986 if (nat44_set_tcp_session_state_i2o (sm, s0, b0, thread_index))
989 else if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment
992 new_port0 = udp0->src_port = s0->out2in.port;
993 sum0 = udp0->checksum;
995 ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
998 ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
1000 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1002 sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
1003 s0->ext_host_addr.as_u32,
1004 ip4_header_t, dst_address);
1006 ip_csum_update (sum0,
1007 vnet_buffer (b0)->ip.reass.l4_dst_port,
1008 s0->ext_host_port, ip4_header_t, length);
1009 udp0->dst_port = s0->ext_host_port;
1010 ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
1012 udp0->checksum = ip_csum_fold (sum0);
1017 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1019 new_port0 = udp0->src_port = s0->out2in.port;
1020 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1022 udp0->dst_port = s0->ext_host_port;
1023 ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
1030 nat44_session_update_counters (s0, now,
1031 vlib_buffer_length_in_chain
1032 (vm, b0), thread_index);
1033 /* Per-user LRU list maintenance */
1034 nat44_session_update_lru (sm, s0, thread_index);
1037 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1038 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1040 nat_in2out_ed_trace_t *t =
1041 vlib_add_trace (vm, node, b0, sizeof (*t));
1042 t->sw_if_index = sw_if_index0;
1043 t->next_index = next0;
1044 t->is_slow_path = 0;
1047 t->session_index = s0 - tsm->sessions;
1049 t->session_index = ~0;
1052 pkts_processed += next0 == nat_buffer_opaque (b0)->arc_next;
1053 /* verify speculative enqueue, maybe switch current next frame */
1054 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1055 to_next, n_left_to_next,
1059 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1062 vlib_node_increment_counter (vm, stats_node_index,
1063 NAT_IN2OUT_ED_ERROR_IN2OUT_PACKETS,
1065 vlib_node_increment_counter (vm, stats_node_index,
1066 NAT_IN2OUT_ED_ERROR_TCP_PACKETS, tcp_packets);
1067 vlib_node_increment_counter (vm, stats_node_index,
1068 NAT_IN2OUT_ED_ERROR_UDP_PACKETS, udp_packets);
1069 vlib_node_increment_counter (vm, stats_node_index,
1070 NAT_IN2OUT_ED_ERROR_ICMP_PACKETS,
1072 vlib_node_increment_counter (vm, stats_node_index,
1073 NAT_IN2OUT_ED_ERROR_OTHER_PACKETS,
1075 return frame->n_vectors;
1079 nat44_ed_in2out_slow_path_node_fn_inline (vlib_main_t * vm,
1080 vlib_node_runtime_t * node,
1081 vlib_frame_t * frame,
1082 int is_output_feature)
1084 u32 n_left_from, *from, *to_next, pkts_processed = 0, stats_node_index;
1085 nat_next_t next_index;
1086 snat_main_t *sm = &snat_main;
1087 f64 now = vlib_time_now (vm);
1088 u32 thread_index = vm->thread_index;
1089 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
1090 u32 tcp_packets = 0, udp_packets = 0, icmp_packets = 0, other_packets = 0;
1092 stats_node_index = sm->ed_in2out_slowpath_node_index;
1094 from = vlib_frame_vector_args (frame);
1095 n_left_from = frame->n_vectors;
1096 next_index = node->cached_next_index;
1098 while (n_left_from > 0)
1102 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1104 while (n_left_from > 0 && n_left_to_next > 0)
1108 u32 next0, sw_if_index0, rx_fib_index0, iph_offset0 = 0, proto0,
1109 new_addr0, old_addr0;
1110 u16 old_port0, new_port0;
1114 icmp46_header_t *icmp0;
1115 snat_session_t *s0 = 0;
1116 clib_bihash_kv_16_8_t kv0, value0;
1119 /* speculatively enqueue b0 to the current next frame */
1125 n_left_to_next -= 1;
1127 b0 = vlib_get_buffer (vm, bi0);
1129 if (is_output_feature)
1130 iph_offset0 = vnet_buffer (b0)->ip.reass.save_rewrite_length;
1132 next0 = nat_buffer_opaque (b0)->arc_next;
1134 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
1137 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1139 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1142 if (PREDICT_FALSE (ip0->ttl == 1))
1144 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1145 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1146 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1148 next0 = NAT_NEXT_ICMP_ERROR;
1152 udp0 = ip4_next_header (ip0);
1153 tcp0 = (tcp_header_t *) udp0;
1154 icmp0 = (icmp46_header_t *) udp0;
1155 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1157 if (PREDICT_FALSE (proto0 == ~0))
1159 s0 = nat44_ed_in2out_unknown_proto (sm, b0, ip0,
1164 next0 = NAT_NEXT_DROP;
1170 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1172 next0 = icmp_in2out_ed_slow_path
1173 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0,
1174 node, next0, now, thread_index, &s0);
1180 make_ed_kv (&kv0, &ip0->src_address, &ip0->dst_address,
1181 ip0->protocol, rx_fib_index0,
1182 vnet_buffer (b0)->ip.reass.l4_src_port,
1183 vnet_buffer (b0)->ip.reass.l4_dst_port);
1185 if (clib_bihash_search_16_8 (&tsm->in2out_ed, &kv0, &value0))
1188 if (is_output_feature)
1191 (nat44_ed_not_translate_output_feature
1192 (sm, ip0, ip0->protocol,
1193 vnet_buffer (b0)->ip.reass.l4_src_port,
1194 vnet_buffer (b0)->ip.reass.l4_dst_port,
1195 thread_index, sw_if_index0,
1196 vnet_buffer (b0)->sw_if_index[VLIB_TX])))
1200 * Send DHCP packets to the ipv4 stack, or we won't
1201 * be able to use dhcp client on the outside interface
1204 (proto0 == SNAT_PROTOCOL_UDP
1205 && (vnet_buffer (b0)->ip.reass.l4_dst_port ==
1206 clib_host_to_net_u16 (UDP_DST_PORT_dhcp_to_server))
1207 && ip0->dst_address.as_u32 == 0xffffffff))
1213 (nat44_ed_not_translate
1214 (sm, node, sw_if_index0, ip0, proto0, rx_fib_index0,
1219 next0 = slow_path_ed (sm, b0, rx_fib_index0, &kv0, &s0, node,
1220 next0, thread_index, now);
1222 if (PREDICT_FALSE (next0 == NAT_NEXT_DROP))
1225 if (PREDICT_FALSE (!s0))
1231 s0 = pool_elt_at_index (tsm->sessions, value0.value);
1235 b0->flags |= VNET_BUFFER_F_IS_NATED;
1237 if (!is_output_feature)
1238 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1240 old_addr0 = ip0->src_address.as_u32;
1241 new_addr0 = ip0->src_address.as_u32 = s0->out2in.addr.as_u32;
1242 sum0 = ip0->checksum;
1243 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1245 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1246 sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
1247 s0->ext_host_addr.as_u32, ip4_header_t,
1249 ip0->checksum = ip_csum_fold (sum0);
1251 old_port0 = vnet_buffer (b0)->ip.reass.l4_src_port;
1253 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1255 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1257 new_port0 = udp0->src_port = s0->out2in.port;
1258 sum0 = tcp0->checksum;
1260 ip_csum_update (sum0, old_addr0, new_addr0,
1261 ip4_header_t, dst_address);
1263 ip_csum_update (sum0, old_port0, new_port0,
1264 ip4_header_t, length);
1265 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1268 ip_csum_update (sum0, ip0->dst_address.as_u32,
1269 s0->ext_host_addr.as_u32,
1270 ip4_header_t, dst_address);
1272 ip_csum_update (sum0,
1273 vnet_buffer (b0)->ip.
1274 reass.l4_dst_port, s0->ext_host_port,
1275 ip4_header_t, length);
1276 tcp0->dst_port = s0->ext_host_port;
1277 ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
1279 mss_clamping (sm, tcp0, &sum0);
1280 tcp0->checksum = ip_csum_fold (sum0);
1283 if (nat44_set_tcp_session_state_i2o (sm, s0, b0, thread_index))
1286 else if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment
1289 new_port0 = udp0->src_port = s0->out2in.port;
1290 sum0 = udp0->checksum;
1292 ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1295 ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
1297 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1299 sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
1300 s0->ext_host_addr.as_u32,
1301 ip4_header_t, dst_address);
1303 ip_csum_update (sum0,
1304 vnet_buffer (b0)->ip.reass.l4_dst_port,
1305 s0->ext_host_port, ip4_header_t, length);
1306 udp0->dst_port = s0->ext_host_port;
1307 ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
1309 udp0->checksum = ip_csum_fold (sum0);
1314 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1316 new_port0 = udp0->src_port = s0->out2in.port;
1317 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1319 udp0->dst_port = s0->ext_host_port;
1320 ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
1327 nat44_session_update_counters (s0, now,
1328 vlib_buffer_length_in_chain
1329 (vm, b0), thread_index);
1330 /* Per-user LRU list maintenance */
1331 nat44_session_update_lru (sm, s0, thread_index);
1334 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1335 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1337 nat_in2out_ed_trace_t *t =
1338 vlib_add_trace (vm, node, b0, sizeof (*t));
1339 t->sw_if_index = sw_if_index0;
1340 t->next_index = next0;
1341 t->is_slow_path = 1;
1344 t->session_index = s0 - tsm->sessions;
1346 t->session_index = ~0;
1349 pkts_processed += next0 == nat_buffer_opaque (b0)->arc_next;
1351 /* verify speculative enqueue, maybe switch current next frame */
1352 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1353 to_next, n_left_to_next,
1357 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1360 vlib_node_increment_counter (vm, stats_node_index,
1361 NAT_IN2OUT_ED_ERROR_IN2OUT_PACKETS,
1363 vlib_node_increment_counter (vm, stats_node_index,
1364 NAT_IN2OUT_ED_ERROR_TCP_PACKETS, tcp_packets);
1365 vlib_node_increment_counter (vm, stats_node_index,
1366 NAT_IN2OUT_ED_ERROR_UDP_PACKETS, udp_packets);
1367 vlib_node_increment_counter (vm, stats_node_index,
1368 NAT_IN2OUT_ED_ERROR_ICMP_PACKETS,
1370 vlib_node_increment_counter (vm, stats_node_index,
1371 NAT_IN2OUT_ED_ERROR_OTHER_PACKETS,
1373 return frame->n_vectors;
1376 VLIB_NODE_FN (nat44_ed_in2out_node) (vlib_main_t * vm,
1377 vlib_node_runtime_t * node,
1378 vlib_frame_t * frame)
1380 return nat44_ed_in2out_fast_path_node_fn_inline (vm, node, frame, 0);
1384 VLIB_REGISTER_NODE (nat44_ed_in2out_node) = {
1385 .name = "nat44-ed-in2out",
1386 .vector_size = sizeof (u32),
1387 .sibling_of = "nat-default",
1388 .format_trace = format_nat_in2out_ed_trace,
1389 .type = VLIB_NODE_TYPE_INTERNAL,
1390 .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
1391 .error_strings = nat_in2out_ed_error_strings,
1392 .runtime_data_bytes = sizeof (snat_runtime_t),
1396 VLIB_NODE_FN (nat44_ed_in2out_output_node) (vlib_main_t * vm,
1397 vlib_node_runtime_t * node,
1398 vlib_frame_t * frame)
1400 return nat44_ed_in2out_fast_path_node_fn_inline (vm, node, frame, 1);
1404 VLIB_REGISTER_NODE (nat44_ed_in2out_output_node) = {
1405 .name = "nat44-ed-in2out-output",
1406 .vector_size = sizeof (u32),
1407 .sibling_of = "nat-default",
1408 .format_trace = format_nat_in2out_ed_trace,
1409 .type = VLIB_NODE_TYPE_INTERNAL,
1410 .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
1411 .error_strings = nat_in2out_ed_error_strings,
1412 .runtime_data_bytes = sizeof (snat_runtime_t),
1416 VLIB_NODE_FN (nat44_ed_in2out_slowpath_node) (vlib_main_t * vm,
1417 vlib_node_runtime_t *
1418 node, vlib_frame_t * frame)
1420 return nat44_ed_in2out_slow_path_node_fn_inline (vm, node, frame, 0);
1424 VLIB_REGISTER_NODE (nat44_ed_in2out_slowpath_node) = {
1425 .name = "nat44-ed-in2out-slowpath",
1426 .vector_size = sizeof (u32),
1427 .sibling_of = "nat-default",
1428 .format_trace = format_nat_in2out_ed_trace,
1429 .type = VLIB_NODE_TYPE_INTERNAL,
1430 .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
1431 .error_strings = nat_in2out_ed_error_strings,
1432 .runtime_data_bytes = sizeof (snat_runtime_t),
1436 VLIB_NODE_FN (nat44_ed_in2out_output_slowpath_node) (vlib_main_t * vm,
1439 vlib_frame_t * frame)
1441 return nat44_ed_in2out_slow_path_node_fn_inline (vm, node, frame, 1);
1445 VLIB_REGISTER_NODE (nat44_ed_in2out_output_slowpath_node) = {
1446 .name = "nat44-ed-in2out-output-slowpath",
1447 .vector_size = sizeof (u32),
1448 .sibling_of = "nat-default",
1449 .format_trace = format_nat_in2out_ed_trace,
1450 .type = VLIB_NODE_TYPE_INTERNAL,
1451 .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
1452 .error_strings = nat_in2out_ed_error_strings,
1453 .runtime_data_bytes = sizeof (snat_runtime_t),
1458 format_nat_pre_trace (u8 * s, va_list * args)
1460 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1461 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1462 nat_pre_trace_t *t = va_arg (*args, nat_pre_trace_t *);
1463 return format (s, "in2out next_index %d", t->next_index);
1466 VLIB_NODE_FN (nat_pre_in2out_node)
1467 (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
1469 return nat_pre_node_fn_inline (vm, node, frame,
1470 NAT_NEXT_IN2OUT_ED_FAST_PATH);
1474 VLIB_REGISTER_NODE (nat_pre_in2out_node) = {
1475 .name = "nat-pre-in2out",
1476 .vector_size = sizeof (u32),
1477 .sibling_of = "nat-default",
1478 .format_trace = format_nat_pre_trace,
1479 .type = VLIB_NODE_TYPE_INTERNAL,
1485 * fd.io coding-style-patch-verification: ON
1488 * eval: (c-set-style "gnu")