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_node_fn_inline (vlib_main_t * vm,
822 vlib_node_runtime_t * node,
823 vlib_frame_t * frame, int is_slow_path,
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 = is_slow_path ? sm->ed_in2out_slowpath_node_index :
839 sm->ed_in2out_node_index;
841 from = vlib_frame_vector_args (frame);
842 n_left_from = frame->n_vectors;
843 next_index = node->cached_next_index;
845 while (n_left_from > 0)
849 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
851 while (n_left_from >= 4 && n_left_to_next >= 2)
854 vlib_buffer_t *b0, *b1;
855 u32 next0, sw_if_index0, rx_fib_index0, iph_offset0 = 0, proto0,
856 new_addr0, old_addr0;
857 u32 next1, sw_if_index1, rx_fib_index1, iph_offset1 = 0, proto1,
858 new_addr1, old_addr1;
859 u16 old_port0, new_port0, old_port1, new_port1;
860 ip4_header_t *ip0, *ip1;
861 udp_header_t *udp0, *udp1;
862 tcp_header_t *tcp0, *tcp1;
863 icmp46_header_t *icmp0, *icmp1;
864 snat_session_t *s0 = 0, *s1 = 0;
865 clib_bihash_kv_16_8_t kv0, value0, kv1, value1;
866 ip_csum_t sum0, sum1;
868 /* Prefetch next iteration. */
870 vlib_buffer_t *p2, *p3;
872 p2 = vlib_get_buffer (vm, from[2]);
873 p3 = vlib_get_buffer (vm, from[3]);
875 vlib_prefetch_buffer_header (p2, LOAD);
876 vlib_prefetch_buffer_header (p3, LOAD);
878 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
879 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
882 /* speculatively enqueue b0 and b1 to the current next frame */
883 to_next[0] = bi0 = from[0];
884 to_next[1] = bi1 = from[1];
890 b0 = vlib_get_buffer (vm, bi0);
891 b1 = vlib_get_buffer (vm, bi1);
893 if (is_output_feature)
895 // output feature fast path is enabled on the arc
896 // we need new arc_next feature
897 if (PREDICT_TRUE (!is_slow_path))
899 vnet_feature_next (&nat_buffer_opaque (b0)->arc_next, b0);
900 vnet_feature_next (&nat_buffer_opaque (b1)->arc_next, b1);
903 iph_offset0 = vnet_buffer (b0)->ip.reass.save_rewrite_length;
904 iph_offset1 = vnet_buffer (b1)->ip.reass.save_rewrite_length;
907 next0 = nat_buffer_opaque (b0)->arc_next;
908 next1 = nat_buffer_opaque (b1)->arc_next;
910 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
913 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
915 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
918 if (PREDICT_FALSE (ip0->ttl == 1))
920 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
921 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
922 ICMP4_time_exceeded_ttl_exceeded_in_transit,
924 next0 = NAT_NEXT_ICMP_ERROR;
928 udp0 = ip4_next_header (ip0);
929 tcp0 = (tcp_header_t *) udp0;
930 icmp0 = (icmp46_header_t *) udp0;
931 proto0 = ip_proto_to_snat_proto (ip0->protocol);
935 if (PREDICT_FALSE (proto0 == ~0))
937 s0 = nat44_ed_in2out_unknown_proto (sm, b0, ip0,
939 thread_index, now, vm,
942 next0 = NAT_NEXT_DROP;
947 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
949 next0 = icmp_in2out_ed_slow_path
950 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
951 next0, now, thread_index, &s0);
958 if (PREDICT_FALSE (proto0 == ~0))
964 if (is_output_feature)
967 (nat_not_translate_output_feature_fwd
968 (sm, ip0, thread_index, now, vm, b0)))
972 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
979 make_ed_kv (&kv0, &ip0->src_address, &ip0->dst_address,
980 ip0->protocol, rx_fib_index0,
981 vnet_buffer (b0)->ip.reass.l4_src_port,
982 vnet_buffer (b0)->ip.reass.l4_dst_port);
984 if (clib_bihash_search_16_8 (&tsm->in2out_ed, &kv0, &value0))
988 if (is_output_feature)
991 (nat44_ed_not_translate_output_feature
992 (sm, ip0, ip0->protocol,
993 vnet_buffer (b0)->ip.reass.l4_src_port,
994 vnet_buffer (b0)->ip.reass.l4_dst_port,
995 thread_index, sw_if_index0,
996 vnet_buffer (b0)->sw_if_index[VLIB_TX])))
1000 * Send DHCP packets to the ipv4 stack, or we won't
1001 * be able to use dhcp client on the outside interface
1004 (proto0 == SNAT_PROTOCOL_UDP
1005 && (vnet_buffer (b0)->ip.reass.l4_dst_port ==
1006 clib_host_to_net_u16
1007 (UDP_DST_PORT_dhcp_to_server))
1008 && ip0->dst_address.as_u32 == 0xffffffff))
1013 if (PREDICT_FALSE (nat44_ed_not_translate (sm, node,
1022 slow_path_ed (sm, b0, rx_fib_index0, &kv0, &s0, node,
1023 next0, thread_index, now);
1025 if (PREDICT_FALSE (next0 == NAT_NEXT_DROP))
1028 if (PREDICT_FALSE (!s0))
1039 s0 = pool_elt_at_index (tsm->sessions, value0.value);
1042 b0->flags |= VNET_BUFFER_F_IS_NATED;
1044 if (!is_output_feature)
1045 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1047 old_addr0 = ip0->src_address.as_u32;
1048 new_addr0 = ip0->src_address.as_u32 = s0->out2in.addr.as_u32;
1049 sum0 = ip0->checksum;
1050 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1052 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1053 sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
1054 s0->ext_host_addr.as_u32, ip4_header_t,
1056 ip0->checksum = ip_csum_fold (sum0);
1058 old_port0 = vnet_buffer (b0)->ip.reass.l4_src_port;
1060 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1062 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1064 new_port0 = udp0->src_port = s0->out2in.port;
1065 sum0 = tcp0->checksum;
1067 ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1070 ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
1072 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1074 sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
1075 s0->ext_host_addr.as_u32,
1076 ip4_header_t, dst_address);
1078 ip_csum_update (sum0,
1079 vnet_buffer (b0)->ip.
1080 reass.l4_dst_port, s0->ext_host_port,
1081 ip4_header_t, length);
1082 tcp0->dst_port = s0->ext_host_port;
1083 ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
1085 mss_clamping (sm, tcp0, &sum0);
1086 tcp0->checksum = ip_csum_fold (sum0);
1089 if (nat44_set_tcp_session_state_i2o (sm, s0, b0, thread_index))
1092 else if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment
1095 new_port0 = udp0->src_port = s0->out2in.port;
1096 sum0 = udp0->checksum;
1097 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1099 sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
1101 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1103 sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
1104 s0->ext_host_addr.as_u32,
1105 ip4_header_t, dst_address);
1107 ip_csum_update (sum0,
1108 vnet_buffer (b0)->ip.reass.l4_dst_port,
1109 s0->ext_host_port, ip4_header_t, length);
1110 udp0->dst_port = s0->ext_host_port;
1111 ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
1113 udp0->checksum = ip_csum_fold (sum0);
1118 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1120 new_port0 = udp0->src_port = s0->out2in.port;
1121 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1123 udp0->dst_port = s0->ext_host_port;
1124 ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
1131 nat44_session_update_counters (s0, now,
1132 vlib_buffer_length_in_chain (vm,
1135 /* Per-user LRU list maintenance */
1136 nat44_session_update_lru (sm, s0, thread_index);
1139 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1140 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1142 nat_in2out_ed_trace_t *t =
1143 vlib_add_trace (vm, node, b0, sizeof (*t));
1144 t->is_slow_path = is_slow_path;
1145 t->sw_if_index = sw_if_index0;
1146 t->next_index = next0;
1147 t->session_index = ~0;
1149 t->session_index = s0 - tsm->sessions;
1152 pkts_processed += next0 == nat_buffer_opaque (b0)->arc_next;
1154 ip1 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b1) +
1157 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
1159 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1162 if (PREDICT_FALSE (ip1->ttl == 1))
1164 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1165 icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
1166 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1168 next1 = NAT_NEXT_ICMP_ERROR;
1172 udp1 = ip4_next_header (ip1);
1173 tcp1 = (tcp_header_t *) udp1;
1174 icmp1 = (icmp46_header_t *) udp1;
1175 proto1 = ip_proto_to_snat_proto (ip1->protocol);
1179 if (PREDICT_FALSE (proto1 == ~0))
1181 s1 = nat44_ed_in2out_unknown_proto (sm, b1, ip1,
1186 next1 = NAT_NEXT_DROP;
1191 if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
1193 next1 = icmp_in2out_ed_slow_path
1194 (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1,
1195 node, next1, now, thread_index, &s1);
1202 if (PREDICT_FALSE (proto1 == ~0))
1208 if (is_output_feature)
1211 (nat_not_translate_output_feature_fwd
1212 (sm, ip1, thread_index, now, vm, b1)))
1216 if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
1223 make_ed_kv (&kv1, &ip1->src_address, &ip1->dst_address,
1224 ip1->protocol, rx_fib_index1,
1225 vnet_buffer (b1)->ip.reass.l4_src_port,
1226 vnet_buffer (b1)->ip.reass.l4_dst_port);
1228 if (clib_bihash_search_16_8 (&tsm->in2out_ed, &kv1, &value1))
1232 if (is_output_feature)
1235 (nat44_ed_not_translate_output_feature
1236 (sm, ip1, ip1->protocol,
1237 vnet_buffer (b1)->ip.reass.l4_src_port,
1238 vnet_buffer (b1)->ip.reass.l4_dst_port,
1239 thread_index, sw_if_index1,
1240 vnet_buffer (b1)->sw_if_index[VLIB_TX])))
1244 * Send DHCP packets to the ipv4 stack, or we won't
1245 * be able to use dhcp client on the outside interface
1248 (proto1 == SNAT_PROTOCOL_UDP
1249 && (vnet_buffer (b1)->ip.reass.l4_dst_port ==
1250 clib_host_to_net_u16
1251 (UDP_DST_PORT_dhcp_to_server))
1252 && ip1->dst_address.as_u32 == 0xffffffff))
1257 if (PREDICT_FALSE (nat44_ed_not_translate (sm, node,
1267 slow_path_ed (sm, b1, rx_fib_index1, &kv1, &s1, node,
1268 next1, thread_index, now);
1270 if (PREDICT_FALSE (next1 == NAT_NEXT_DROP))
1273 if (PREDICT_FALSE (!s1))
1284 s1 = pool_elt_at_index (tsm->sessions, value1.value);
1287 b1->flags |= VNET_BUFFER_F_IS_NATED;
1289 if (!is_output_feature)
1290 vnet_buffer (b1)->sw_if_index[VLIB_TX] = s1->out2in.fib_index;
1292 old_addr1 = ip1->src_address.as_u32;
1293 new_addr1 = ip1->src_address.as_u32 = s1->out2in.addr.as_u32;
1294 sum1 = ip1->checksum;
1295 sum1 = ip_csum_update (sum1, old_addr1, new_addr1, ip4_header_t,
1297 if (PREDICT_FALSE (is_twice_nat_session (s1)))
1298 sum1 = ip_csum_update (sum1, ip1->dst_address.as_u32,
1299 s1->ext_host_addr.as_u32, ip4_header_t,
1301 ip1->checksum = ip_csum_fold (sum1);
1303 old_port1 = vnet_buffer (b1)->ip.reass.l4_src_port;
1305 if (PREDICT_TRUE (proto1 == SNAT_PROTOCOL_TCP))
1307 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1309 new_port1 = udp1->src_port = s1->out2in.port;
1310 sum1 = tcp1->checksum;
1312 ip_csum_update (sum1, old_addr1, new_addr1,
1313 ip4_header_t, dst_address);
1315 ip_csum_update (sum1, old_port1, new_port1,
1316 ip4_header_t, length);
1317 if (PREDICT_FALSE (is_twice_nat_session (s1)))
1320 ip_csum_update (sum1, ip1->dst_address.as_u32,
1321 s1->ext_host_addr.as_u32,
1322 ip4_header_t, dst_address);
1324 ip_csum_update (sum1,
1325 vnet_buffer (b1)->ip.
1326 reass.l4_dst_port, s1->ext_host_port,
1327 ip4_header_t, length);
1328 tcp1->dst_port = s1->ext_host_port;
1329 ip1->dst_address.as_u32 = s1->ext_host_addr.as_u32;
1331 tcp1->checksum = ip_csum_fold (sum1);
1332 mss_clamping (sm, tcp1, &sum1);
1335 if (nat44_set_tcp_session_state_i2o (sm, s1, b1, thread_index))
1338 else if (!vnet_buffer (b1)->ip.reass.is_non_first_fragment
1341 new_port1 = udp1->src_port = s1->out2in.port;
1342 sum1 = udp1->checksum;
1344 ip_csum_update (sum1, old_addr1, new_addr1, ip4_header_t,
1347 ip_csum_update (sum1, old_port1, new_port1, ip4_header_t,
1350 if (PREDICT_FALSE (is_twice_nat_session (s1)))
1352 sum1 = ip_csum_update (sum1, ip1->dst_address.as_u32,
1353 s1->ext_host_addr.as_u32,
1354 ip4_header_t, dst_address);
1356 ip_csum_update (sum1,
1357 vnet_buffer (b1)->ip.reass.l4_dst_port,
1358 s1->ext_host_port, ip4_header_t, length);
1359 udp1->dst_port = s1->ext_host_port;
1360 ip1->dst_address.as_u32 = s1->ext_host_addr.as_u32;
1362 udp1->checksum = ip_csum_fold (sum1);
1367 if (!vnet_buffer (b1)->ip.reass.is_non_first_fragment)
1369 new_port1 = udp1->src_port = s1->out2in.port;
1370 if (PREDICT_FALSE (is_twice_nat_session (s1)))
1372 udp1->dst_port = s1->ext_host_port;
1373 ip1->dst_address.as_u32 = s1->ext_host_addr.as_u32;
1380 nat44_session_update_counters (s1, now,
1381 vlib_buffer_length_in_chain (vm,
1384 /* Per-user LRU list maintenance */
1385 nat44_session_update_lru (sm, s1, thread_index);
1388 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1389 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
1391 nat_in2out_ed_trace_t *t =
1392 vlib_add_trace (vm, node, b1, sizeof (*t));
1393 t->is_slow_path = is_slow_path;
1394 t->sw_if_index = sw_if_index1;
1395 t->next_index = next1;
1396 t->session_index = ~0;
1398 t->session_index = s1 - tsm->sessions;
1401 pkts_processed += next1 == nat_buffer_opaque (b1)->arc_next;
1404 /* verify speculative enqueues, maybe switch current next frame */
1405 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1406 to_next, n_left_to_next,
1407 bi0, bi1, next0, next1);
1410 while (n_left_from > 0 && n_left_to_next > 0)
1414 u32 next0, sw_if_index0, rx_fib_index0, iph_offset0 = 0, proto0,
1415 new_addr0, old_addr0;
1416 u16 old_port0, new_port0;
1420 icmp46_header_t *icmp0;
1421 snat_session_t *s0 = 0;
1422 clib_bihash_kv_16_8_t kv0, value0;
1425 /* speculatively enqueue b0 to the current next frame */
1431 n_left_to_next -= 1;
1433 b0 = vlib_get_buffer (vm, bi0);
1435 if (is_output_feature)
1437 // output feature fast path is enabled on the arc
1438 // we need new arc_next feature
1439 if (PREDICT_TRUE (!is_slow_path))
1440 vnet_feature_next (&nat_buffer_opaque (b0)->arc_next, b0);
1442 iph_offset0 = vnet_buffer (b0)->ip.reass.save_rewrite_length;
1445 next0 = nat_buffer_opaque (b0)->arc_next;
1447 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
1450 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1452 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1455 if (PREDICT_FALSE (ip0->ttl == 1))
1457 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1458 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1459 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1461 next0 = NAT_NEXT_ICMP_ERROR;
1465 udp0 = ip4_next_header (ip0);
1466 tcp0 = (tcp_header_t *) udp0;
1467 icmp0 = (icmp46_header_t *) udp0;
1468 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1472 if (PREDICT_FALSE (proto0 == ~0))
1474 s0 = nat44_ed_in2out_unknown_proto (sm, b0, ip0,
1479 next0 = NAT_NEXT_DROP;
1484 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1486 next0 = icmp_in2out_ed_slow_path
1487 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0,
1488 node, next0, now, thread_index, &s0);
1495 if (PREDICT_FALSE (proto0 == ~0))
1501 if (is_output_feature)
1504 (nat_not_translate_output_feature_fwd
1505 (sm, ip0, thread_index, now, vm, b0)))
1509 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1516 make_ed_kv (&kv0, &ip0->src_address, &ip0->dst_address,
1517 ip0->protocol, rx_fib_index0,
1518 vnet_buffer (b0)->ip.reass.l4_src_port,
1519 vnet_buffer (b0)->ip.reass.l4_dst_port);
1521 if (clib_bihash_search_16_8 (&tsm->in2out_ed, &kv0, &value0))
1525 if (is_output_feature)
1528 (nat44_ed_not_translate_output_feature
1529 (sm, ip0, ip0->protocol,
1530 vnet_buffer (b0)->ip.reass.l4_src_port,
1531 vnet_buffer (b0)->ip.reass.l4_dst_port,
1532 thread_index, sw_if_index0,
1533 vnet_buffer (b0)->sw_if_index[VLIB_TX])))
1537 * Send DHCP packets to the ipv4 stack, or we won't
1538 * be able to use dhcp client on the outside interface
1541 (proto0 == SNAT_PROTOCOL_UDP
1542 && (vnet_buffer (b0)->ip.reass.l4_dst_port ==
1543 clib_host_to_net_u16
1544 (UDP_DST_PORT_dhcp_to_server))
1545 && ip0->dst_address.as_u32 == 0xffffffff))
1550 if (PREDICT_FALSE (nat44_ed_not_translate (sm, node,
1560 slow_path_ed (sm, b0, rx_fib_index0, &kv0, &s0, node,
1561 next0, thread_index, now);
1563 if (PREDICT_FALSE (next0 == NAT_NEXT_DROP))
1566 if (PREDICT_FALSE (!s0))
1577 s0 = pool_elt_at_index (tsm->sessions, value0.value);
1580 b0->flags |= VNET_BUFFER_F_IS_NATED;
1582 if (!is_output_feature)
1583 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1585 old_addr0 = ip0->src_address.as_u32;
1586 new_addr0 = ip0->src_address.as_u32 = s0->out2in.addr.as_u32;
1587 sum0 = ip0->checksum;
1588 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1590 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1591 sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
1592 s0->ext_host_addr.as_u32, ip4_header_t,
1594 ip0->checksum = ip_csum_fold (sum0);
1596 old_port0 = vnet_buffer (b0)->ip.reass.l4_src_port;
1598 if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1600 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1602 new_port0 = udp0->src_port = s0->out2in.port;
1603 sum0 = tcp0->checksum;
1605 ip_csum_update (sum0, old_addr0, new_addr0,
1606 ip4_header_t, dst_address);
1608 ip_csum_update (sum0, old_port0, new_port0,
1609 ip4_header_t, length);
1610 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1613 ip_csum_update (sum0, ip0->dst_address.as_u32,
1614 s0->ext_host_addr.as_u32,
1615 ip4_header_t, dst_address);
1617 ip_csum_update (sum0,
1618 vnet_buffer (b0)->ip.
1619 reass.l4_dst_port, s0->ext_host_port,
1620 ip4_header_t, length);
1621 tcp0->dst_port = s0->ext_host_port;
1622 ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
1624 mss_clamping (sm, tcp0, &sum0);
1625 tcp0->checksum = ip_csum_fold (sum0);
1628 if (nat44_set_tcp_session_state_i2o (sm, s0, b0, thread_index))
1631 else if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment
1634 new_port0 = udp0->src_port = s0->out2in.port;
1635 sum0 = udp0->checksum;
1637 ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1640 ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
1642 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1644 sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32,
1645 s0->ext_host_addr.as_u32,
1646 ip4_header_t, dst_address);
1648 ip_csum_update (sum0,
1649 vnet_buffer (b0)->ip.reass.l4_dst_port,
1650 s0->ext_host_port, ip4_header_t, length);
1651 udp0->dst_port = s0->ext_host_port;
1652 ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
1654 udp0->checksum = ip_csum_fold (sum0);
1659 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1661 new_port0 = udp0->src_port = s0->out2in.port;
1662 if (PREDICT_FALSE (is_twice_nat_session (s0)))
1664 udp0->dst_port = s0->ext_host_port;
1665 ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32;
1672 nat44_session_update_counters (s0, now,
1673 vlib_buffer_length_in_chain
1674 (vm, b0), thread_index);
1675 /* Per-user LRU list maintenance */
1676 nat44_session_update_lru (sm, s0, thread_index);
1679 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1680 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1682 nat_in2out_ed_trace_t *t =
1683 vlib_add_trace (vm, node, b0, sizeof (*t));
1684 t->is_slow_path = is_slow_path;
1685 t->sw_if_index = sw_if_index0;
1686 t->next_index = next0;
1687 t->session_index = ~0;
1689 t->session_index = s0 - tsm->sessions;
1692 pkts_processed += next0 == nat_buffer_opaque (b0)->arc_next;
1694 /* verify speculative enqueue, maybe switch current next frame */
1695 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1696 to_next, n_left_to_next,
1700 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1703 vlib_node_increment_counter (vm, stats_node_index,
1704 NAT_IN2OUT_ED_ERROR_IN2OUT_PACKETS,
1706 vlib_node_increment_counter (vm, stats_node_index,
1707 NAT_IN2OUT_ED_ERROR_TCP_PACKETS, tcp_packets);
1708 vlib_node_increment_counter (vm, stats_node_index,
1709 NAT_IN2OUT_ED_ERROR_UDP_PACKETS, udp_packets);
1710 vlib_node_increment_counter (vm, stats_node_index,
1711 NAT_IN2OUT_ED_ERROR_ICMP_PACKETS,
1713 vlib_node_increment_counter (vm, stats_node_index,
1714 NAT_IN2OUT_ED_ERROR_OTHER_PACKETS,
1716 return frame->n_vectors;
1719 VLIB_NODE_FN (nat44_ed_in2out_node) (vlib_main_t * vm,
1720 vlib_node_runtime_t * node,
1721 vlib_frame_t * frame)
1723 return nat44_ed_in2out_node_fn_inline (vm, node, frame, 0, 0);
1727 VLIB_REGISTER_NODE (nat44_ed_in2out_node) = {
1728 .name = "nat44-ed-in2out",
1729 .vector_size = sizeof (u32),
1730 .sibling_of = "nat-default",
1731 .format_trace = format_nat_in2out_ed_trace,
1732 .type = VLIB_NODE_TYPE_INTERNAL,
1733 .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
1734 .error_strings = nat_in2out_ed_error_strings,
1735 .runtime_data_bytes = sizeof (snat_runtime_t),
1739 VLIB_NODE_FN (nat44_ed_in2out_output_node) (vlib_main_t * vm,
1740 vlib_node_runtime_t * node,
1741 vlib_frame_t * frame)
1743 return nat44_ed_in2out_node_fn_inline (vm, node, frame, 0, 1);
1747 VLIB_REGISTER_NODE (nat44_ed_in2out_output_node) = {
1748 .name = "nat44-ed-in2out-output",
1749 .vector_size = sizeof (u32),
1750 .sibling_of = "nat-default",
1751 .format_trace = format_nat_in2out_ed_trace,
1752 .type = VLIB_NODE_TYPE_INTERNAL,
1753 .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
1754 .error_strings = nat_in2out_ed_error_strings,
1755 .runtime_data_bytes = sizeof (snat_runtime_t),
1759 VLIB_NODE_FN (nat44_ed_in2out_slowpath_node) (vlib_main_t * vm,
1760 vlib_node_runtime_t *
1761 node, vlib_frame_t * frame)
1763 return nat44_ed_in2out_node_fn_inline (vm, node, frame, 1, 0);
1767 VLIB_REGISTER_NODE (nat44_ed_in2out_slowpath_node) = {
1768 .name = "nat44-ed-in2out-slowpath",
1769 .vector_size = sizeof (u32),
1770 .sibling_of = "nat-default",
1771 .format_trace = format_nat_in2out_ed_trace,
1772 .type = VLIB_NODE_TYPE_INTERNAL,
1773 .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
1774 .error_strings = nat_in2out_ed_error_strings,
1775 .runtime_data_bytes = sizeof (snat_runtime_t),
1779 VLIB_NODE_FN (nat44_ed_in2out_output_slowpath_node) (vlib_main_t * vm,
1782 vlib_frame_t * frame)
1784 return nat44_ed_in2out_node_fn_inline (vm, node, frame, 1, 1);
1788 VLIB_REGISTER_NODE (nat44_ed_in2out_output_slowpath_node) = {
1789 .name = "nat44-ed-in2out-output-slowpath",
1790 .vector_size = sizeof (u32),
1791 .sibling_of = "nat-default",
1792 .format_trace = format_nat_in2out_ed_trace,
1793 .type = VLIB_NODE_TYPE_INTERNAL,
1794 .n_errors = ARRAY_LEN (nat_in2out_ed_error_strings),
1795 .error_strings = nat_in2out_ed_error_strings,
1796 .runtime_data_bytes = sizeof (snat_runtime_t),
1801 format_nat_pre_trace (u8 * s, va_list * args)
1803 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1804 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1805 nat_pre_trace_t *t = va_arg (*args, nat_pre_trace_t *);
1806 return format (s, "in2out next_index %d", t->next_index);
1809 VLIB_NODE_FN (nat_pre_in2out_node)
1810 (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
1812 return nat_pre_node_fn_inline (vm, node, frame,
1813 NAT_NEXT_IN2OUT_ED_FAST_PATH);
1817 VLIB_REGISTER_NODE (nat_pre_in2out_node) = {
1818 .name = "nat-pre-in2out",
1819 .vector_size = sizeof (u32),
1820 .sibling_of = "nat-default",
1821 .format_trace = format_nat_pre_trace,
1822 .type = VLIB_NODE_TYPE_INTERNAL,
1828 * fd.io coding-style-patch-verification: ON
1831 * eval: (c-set-style "gnu")