2 * Copyright (c) 2016 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 inside to outside network translation
20 #include <vlib/vlib.h>
21 #include <vnet/vnet.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_local.h>
28 #include <nat/lib/ipfix_logging.h>
29 #include <nat/nat_inlines.h>
30 #include <nat/lib/nat_syslog.h>
31 #include <nat/nat44-ei/nat44_ei_inlines.h>
32 #include <nat/nat44-ei/nat44_ei.h>
34 #include <vppinfra/hash.h>
35 #include <vppinfra/error.h>
36 #include <vppinfra/elog.h>
37 #include <nat/lib/nat_inlines.h>
46 } snat_in2out_trace_t;
48 /* packet trace format function */
50 format_snat_in2out_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 snat_in2out_trace_t *t = va_arg (*args, snat_in2out_trace_t *);
57 tag = t->is_slow_path ? "NAT44_IN2OUT_SLOW_PATH" : "NAT44_IN2OUT_FAST_PATH";
59 s = format (s, "%s: sw_if_index %d, next index %d, session %d", tag,
60 t->sw_if_index, t->next_index, t->session_index);
61 if (t->is_hairpinning)
63 s = format (s, ", with-hairpinning");
70 format_snat_in2out_fast_trace (u8 * s, va_list * args)
72 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
73 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
74 snat_in2out_trace_t *t = va_arg (*args, snat_in2out_trace_t *);
76 s = format (s, "NAT44_IN2OUT_FAST: sw_if_index %d, next index %d",
77 t->sw_if_index, t->next_index);
82 #define foreach_snat_in2out_error \
83 _(UNSUPPORTED_PROTOCOL, "unsupported protocol") \
84 _(OUT_OF_PORTS, "out of ports") \
85 _(BAD_OUTSIDE_FIB, "outside VRF ID not found") \
86 _(BAD_ICMP_TYPE, "unsupported ICMP type") \
87 _(NO_TRANSLATION, "no translation") \
88 _(MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded") \
89 _(CANNOT_CREATE_USER, "cannot create NAT user")
93 #define _(sym,str) SNAT_IN2OUT_ERROR_##sym,
94 foreach_snat_in2out_error
97 } snat_in2out_error_t;
99 static char *snat_in2out_error_strings[] = {
100 #define _(sym,string) string,
101 foreach_snat_in2out_error
107 SNAT_IN2OUT_NEXT_LOOKUP,
108 SNAT_IN2OUT_NEXT_DROP,
109 SNAT_IN2OUT_NEXT_ICMP_ERROR,
110 SNAT_IN2OUT_NEXT_SLOW_PATH,
112 } snat_in2out_next_t;
115 snat_not_translate (snat_main_t * sm, vlib_node_runtime_t * node,
116 u32 sw_if_index0, ip4_header_t * ip0, u32 proto0,
117 u32 rx_fib_index0, u32 thread_index)
119 udp_header_t *udp0 = ip4_next_header (ip0);
120 clib_bihash_kv_8_8_t kv0, value0;
122 init_nat_k (&kv0, ip0->dst_address, udp0->dst_port, sm->outside_fib_index,
125 /* NAT packet aimed at external address if */
126 /* has active sessions */
127 if (clib_bihash_search_8_8 (&sm->per_thread_data[thread_index].out2in, &kv0,
130 /* or is static mappings */
131 ip4_address_t placeholder_addr;
132 u16 placeholder_port;
133 u32 placeholder_fib_index;
134 if (!nat44_ei_static_mapping_match (ip0->dst_address, udp0->dst_port,
135 sm->outside_fib_index, proto0,
136 &placeholder_addr, &placeholder_port,
137 &placeholder_fib_index, 1, 0, 0))
143 if (sm->forwarding_enabled)
146 return snat_not_translate_fast (sm, node, sw_if_index0, ip0, proto0,
151 nat_not_translate_output_feature (snat_main_t * sm, ip4_header_t * ip0,
152 u32 proto0, u16 src_port, u16 dst_port,
153 u32 thread_index, u32 sw_if_index)
155 clib_bihash_kv_8_8_t kv0, value0;
159 init_nat_k (&kv0, ip0->src_address, src_port,
160 ip4_fib_table_get_index_for_sw_if_index (sw_if_index), proto0);
162 if (!clib_bihash_search_8_8
163 (&sm->per_thread_data[thread_index].out2in, &kv0, &value0))
167 init_nat_k (&kv0, ip0->dst_address, dst_port,
168 ip4_fib_table_get_index_for_sw_if_index (sw_if_index), proto0);
169 if (!clib_bihash_search_8_8
170 (&sm->per_thread_data[thread_index].in2out, &kv0, &value0))
174 pool_foreach (i, sm->output_feature_interfaces)
176 if ((nat_interface_is_inside(i)) && (sw_if_index == i->sw_if_index))
186 #ifndef CLIB_MARCH_VARIANT
188 nat44_i2o_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void *arg)
190 snat_main_t *sm = &snat_main;
191 nat44_is_idle_session_ctx_t *ctx = arg;
193 u64 sess_timeout_time;
194 snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
196 clib_bihash_kv_8_8_t s_kv;
198 s = pool_elt_at_index (tsm->sessions, kv->value);
199 sess_timeout_time = s->last_heard + (f64) nat44_session_get_timeout (sm, s);
200 if (ctx->now >= sess_timeout_time)
202 init_nat_o2i_k (&s_kv, s);
203 if (clib_bihash_add_del_8_8 (&tsm->out2in, &s_kv, 0))
204 nat_elog_warn ("out2in key del failed");
206 nat_ipfix_logging_nat44_ses_delete (ctx->thread_index,
207 s->in2out.addr.as_u32,
208 s->out2in.addr.as_u32,
212 s->in2out.fib_index);
214 nat_syslog_nat44_apmdel (s->user_index, s->in2out.fib_index,
215 &s->in2out.addr, s->in2out.port,
216 &s->out2in.addr, s->out2in.port, s->nat_proto);
218 nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
219 s->ext_host_port, s->nat_proto, s->out2in.fib_index,
222 if (!snat_is_session_static (s))
223 snat_free_outside_address_and_port (sm->addresses, ctx->thread_index,
225 s->out2in.port, s->nat_proto);
227 nat44_delete_session (sm, s, ctx->thread_index);
236 slow_path (snat_main_t * sm, vlib_buffer_t * b0,
238 ip4_address_t i2o_addr,
241 nat_protocol_t nat_proto,
242 snat_session_t ** sessionp,
243 vlib_node_runtime_t * node, u32 next0, u32 thread_index, f64 now)
246 snat_session_t *s = 0;
247 clib_bihash_kv_8_8_t kv0;
249 nat_outside_fib_t *outside_fib;
250 fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
253 .fp_proto = FIB_PROTOCOL_IP4,
256 .ip4.as_u32 = ip0->dst_address.as_u32,
259 nat44_is_idle_session_ctx_t ctx0;
260 ip4_address_t sm_addr;
264 if (PREDICT_FALSE (nat44_ei_maximum_sessions_exceeded (sm, thread_index)))
266 b0->error = node->errors[SNAT_IN2OUT_ERROR_MAX_SESSIONS_EXCEEDED];
267 nat_ipfix_logging_max_sessions (thread_index,
268 sm->max_translations_per_thread);
269 nat_elog_notice ("maximum sessions exceeded");
270 return SNAT_IN2OUT_NEXT_DROP;
273 /* First try to match static mapping by local address and port */
274 if (nat44_ei_static_mapping_match (i2o_addr, i2o_port, rx_fib_index0,
275 nat_proto, &sm_addr, &sm_port,
276 &sm_fib_index, 0, 0, &identity_nat))
278 /* Try to create dynamic translation */
279 if (sm->alloc_addr_and_port (
280 sm->addresses, rx_fib_index0, thread_index, nat_proto, &sm_addr,
281 &sm_port, sm->port_per_thread,
282 sm->per_thread_data[thread_index].snat_thread_index))
284 b0->error = node->errors[SNAT_IN2OUT_ERROR_OUT_OF_PORTS];
285 return SNAT_IN2OUT_NEXT_DROP;
290 if (PREDICT_FALSE (identity_nat))
299 u = nat_user_get_or_create (sm, &ip0->src_address, rx_fib_index0,
303 b0->error = node->errors[SNAT_IN2OUT_ERROR_CANNOT_CREATE_USER];
304 return SNAT_IN2OUT_NEXT_DROP;
307 s = nat_session_alloc_or_recycle (sm, u, thread_index, now);
310 nat44_delete_user_with_no_session (sm, u, thread_index);
311 nat_elog_warn ("create NAT session failed");
312 return SNAT_IN2OUT_NEXT_DROP;
316 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
317 user_session_increment (sm, u, is_sm);
318 s->in2out.addr = i2o_addr;
319 s->in2out.port = i2o_port;
320 s->in2out.fib_index = rx_fib_index0;
321 s->nat_proto = nat_proto;
322 s->out2in.addr = sm_addr;
323 s->out2in.port = sm_port;
324 s->out2in.fib_index = sm->outside_fib_index;
325 switch (vec_len (sm->outside_fibs))
328 s->out2in.fib_index = sm->outside_fib_index;
331 s->out2in.fib_index = sm->outside_fibs[0].fib_index;
335 vec_foreach (outside_fib, sm->outside_fibs)
337 fei = fib_table_lookup (outside_fib->fib_index, &pfx);
338 if (FIB_NODE_INDEX_INVALID != fei)
340 if (fib_entry_get_resolving_interface (fei) != ~0)
342 s->out2in.fib_index = outside_fib->fib_index;
350 s->ext_host_addr.as_u32 = ip0->dst_address.as_u32;
351 s->ext_host_port = vnet_buffer (b0)->ip.reass.l4_dst_port;
354 /* Add to translation hashes */
356 ctx0.thread_index = thread_index;
357 init_nat_i2o_kv (&kv0, s, s - sm->per_thread_data[thread_index].sessions);
358 if (clib_bihash_add_or_overwrite_stale_8_8
359 (&sm->per_thread_data[thread_index].in2out, &kv0,
360 nat44_i2o_is_idle_session_cb, &ctx0))
361 nat_elog_notice ("in2out key add failed");
363 init_nat_o2i_kv (&kv0, s, s - sm->per_thread_data[thread_index].sessions);
364 if (clib_bihash_add_or_overwrite_stale_8_8
365 (&sm->per_thread_data[thread_index].out2in, &kv0,
366 nat44_o2i_is_idle_session_cb, &ctx0))
367 nat_elog_notice ("out2in key add failed");
370 nat_ipfix_logging_nat44_ses_create (thread_index,
371 s->in2out.addr.as_u32,
372 s->out2in.addr.as_u32,
375 s->out2in.port, s->in2out.fib_index);
377 nat_syslog_nat44_apmadd (s->user_index, s->in2out.fib_index,
378 &s->in2out.addr, s->in2out.port, &s->out2in.addr,
379 s->out2in.port, s->nat_proto);
381 nat_ha_sadd (&s->in2out.addr, s->in2out.port, &s->out2in.addr,
382 s->out2in.port, &s->ext_host_addr, s->ext_host_port,
383 &s->ext_host_nat_addr, s->ext_host_nat_port,
384 s->nat_proto, s->in2out.fib_index, s->flags, thread_index, 0);
389 #ifndef CLIB_MARCH_VARIANT
390 static_always_inline snat_in2out_error_t
391 icmp_get_key (vlib_buffer_t * b, ip4_header_t * ip0,
392 ip4_address_t * addr, u16 * port, nat_protocol_t * nat_proto)
394 icmp46_header_t *icmp0;
395 icmp_echo_header_t *echo0, *inner_echo0 = 0;
396 ip4_header_t *inner_ip0 = 0;
398 icmp46_header_t *inner_icmp0;
400 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
401 echo0 = (icmp_echo_header_t *) (icmp0 + 1);
403 if (!icmp_type_is_error_message
404 (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags))
406 *nat_proto = NAT_PROTOCOL_ICMP;
407 *addr = ip0->src_address;
408 *port = vnet_buffer (b)->ip.reass.l4_src_port;
412 inner_ip0 = (ip4_header_t *) (echo0 + 1);
413 l4_header = ip4_next_header (inner_ip0);
414 *nat_proto = ip_proto_to_nat_proto (inner_ip0->protocol);
415 *addr = inner_ip0->dst_address;
418 case NAT_PROTOCOL_ICMP:
419 inner_icmp0 = (icmp46_header_t *) l4_header;
420 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
421 *port = inner_echo0->identifier;
423 case NAT_PROTOCOL_UDP:
424 case NAT_PROTOCOL_TCP:
425 *port = ((tcp_udp_header_t *) l4_header)->dst_port;
428 return SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL;
431 return -1; /* success */
435 * Get address and port values to be used for ICMP packet translation
436 * and create session if needed
438 * @param[in,out] sm NAT main
439 * @param[in,out] node NAT node runtime
440 * @param[in] thread_index thread index
441 * @param[in,out] b0 buffer containing packet to be translated
442 * @param[in,out] ip0 ip header
443 * @param[out] p_proto protocol used for matching
444 * @param[out] p_value address and port after NAT translation
445 * @param[out] p_dont_translate if packet should not be translated
446 * @param d optional parameter
447 * @param e optional parameter
450 icmp_match_in2out_slow (snat_main_t * sm, vlib_node_runtime_t * node,
451 u32 thread_index, vlib_buffer_t * b0,
452 ip4_header_t * ip0, ip4_address_t * addr, u16 * port,
453 u32 * fib_index, nat_protocol_t * proto, void *d,
454 void *e, u8 * dont_translate)
456 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
458 snat_session_t *s0 = 0;
459 clib_bihash_kv_8_8_t kv0, value0;
462 vlib_main_t *vm = vlib_get_main ();
465 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
466 *fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
468 err = icmp_get_key (b0, ip0, addr, port, proto);
471 b0->error = node->errors[err];
472 next0 = SNAT_IN2OUT_NEXT_DROP;
476 init_nat_k (&kv0, *addr, *port, *fib_index, *proto);
477 if (clib_bihash_search_8_8 (&tsm->in2out, &kv0, &value0))
479 if (vnet_buffer (b0)->sw_if_index[VLIB_TX] != ~0)
482 (nat_not_translate_output_feature
483 (sm, ip0, *proto, *port, *port, thread_index, sw_if_index0)))
491 if (PREDICT_FALSE (snat_not_translate (sm, node, sw_if_index0,
492 ip0, NAT_PROTOCOL_ICMP,
493 *fib_index, thread_index)))
501 (icmp_type_is_error_message
502 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags)))
504 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
505 next0 = SNAT_IN2OUT_NEXT_DROP;
510 slow_path (sm, b0, ip0, *addr, *port, *fib_index, *proto, &s0, node,
511 next0, thread_index, vlib_time_now (vm));
513 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
525 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
527 && vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
529 && !icmp_type_is_error_message (vnet_buffer (b0)->ip.
530 reass.icmp_type_or_tcp_flags)))
532 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
533 next0 = SNAT_IN2OUT_NEXT_DROP;
537 s0 = pool_elt_at_index (tsm->sessions, value0.value);
543 *addr = s0->out2in.addr;
544 *port = s0->out2in.port;
545 *fib_index = s0->out2in.fib_index;
548 *(snat_session_t **) (d) = s0;
553 #ifndef CLIB_MARCH_VARIANT
555 * Get address and port values to be used for ICMP packet translation
557 * @param[in] sm NAT main
558 * @param[in,out] node NAT node runtime
559 * @param[in] thread_index thread index
560 * @param[in,out] b0 buffer containing packet to be translated
561 * @param[in,out] ip0 ip header
562 * @param[out] p_proto protocol used for matching
563 * @param[out] p_value address and port after NAT translation
564 * @param[out] p_dont_translate if packet should not be translated
565 * @param d optional parameter
566 * @param e optional parameter
569 icmp_match_in2out_fast (snat_main_t * sm, vlib_node_runtime_t * node,
570 u32 thread_index, vlib_buffer_t * b0,
571 ip4_header_t * ip0, ip4_address_t * addr, u16 * port,
572 u32 * fib_index, nat_protocol_t * proto, void *d,
573 void *e, u8 * dont_translate)
581 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
582 *fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
584 err = icmp_get_key (b0, ip0, addr, port, proto);
587 b0->error = node->errors[err];
588 next0 = SNAT_IN2OUT_NEXT_DROP;
592 ip4_address_t sm_addr;
596 if (nat44_ei_static_mapping_match (*addr, *port, *fib_index, *proto,
597 &sm_addr, &sm_port, &sm_fib_index, 0,
600 if (PREDICT_FALSE (snat_not_translate_fast (sm, node, sw_if_index0, ip0,
608 if (icmp_type_is_error_message
609 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags))
611 next0 = SNAT_IN2OUT_NEXT_DROP;
615 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
616 next0 = SNAT_IN2OUT_NEXT_DROP;
621 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags != ICMP4_echo_request
622 && (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
623 ICMP4_echo_reply || !is_addr_only)
624 && !icmp_type_is_error_message (vnet_buffer (b0)->ip.
625 reass.icmp_type_or_tcp_flags)))
627 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
628 next0 = SNAT_IN2OUT_NEXT_DROP;
637 #ifndef CLIB_MARCH_VARIANT
639 icmp_in2out (snat_main_t * sm,
642 icmp46_header_t * icmp0,
645 vlib_node_runtime_t * node,
646 u32 next0, u32 thread_index, void *d, void *e)
648 vlib_main_t *vm = vlib_get_main ();
652 nat_protocol_t protocol;
653 icmp_echo_header_t *echo0, *inner_echo0 = 0;
654 ip4_header_t *inner_ip0;
656 icmp46_header_t *inner_icmp0;
658 u32 new_addr0, old_addr0;
659 u16 old_id0, new_id0;
660 u16 old_checksum0, new_checksum0;
665 echo0 = (icmp_echo_header_t *) (icmp0 + 1);
668 sm->icmp_match_in2out_cb (sm, node, thread_index, b0, ip0, &addr, &port,
669 &fib_index, &protocol, d, e, &dont_translate);
672 if (next0 == SNAT_IN2OUT_NEXT_DROP || dont_translate)
675 if (PREDICT_TRUE (!ip4_is_fragment (ip0)))
678 ip_incremental_checksum_buffer (vm, b0,
680 (u8 *) vlib_buffer_get_current (b0),
681 ntohs (ip0->length) -
682 ip4_header_bytes (ip0), 0);
683 checksum0 = ~ip_csum_fold (sum0);
684 if (PREDICT_FALSE (checksum0 != 0 && checksum0 != 0xffff))
686 next0 = SNAT_IN2OUT_NEXT_DROP;
691 old_addr0 = ip0->src_address.as_u32;
692 new_addr0 = ip0->src_address.as_u32 = addr.as_u32;
694 sum0 = ip0->checksum;
695 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
696 src_address /* changed member */ );
697 ip0->checksum = ip_csum_fold (sum0);
699 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
701 if (icmp0->checksum == 0)
702 icmp0->checksum = 0xffff;
704 if (!icmp_type_is_error_message (icmp0->type))
707 if (PREDICT_FALSE (new_id0 != echo0->identifier))
709 old_id0 = echo0->identifier;
711 echo0->identifier = new_id0;
713 sum0 = icmp0->checksum;
715 ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
717 icmp0->checksum = ip_csum_fold (sum0);
722 inner_ip0 = (ip4_header_t *) (echo0 + 1);
723 l4_header = ip4_next_header (inner_ip0);
725 if (!ip4_header_checksum_is_valid (inner_ip0))
727 next0 = SNAT_IN2OUT_NEXT_DROP;
731 /* update inner destination IP address */
732 old_addr0 = inner_ip0->dst_address.as_u32;
733 inner_ip0->dst_address = addr;
734 new_addr0 = inner_ip0->dst_address.as_u32;
735 sum0 = icmp0->checksum;
736 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
737 dst_address /* changed member */ );
738 icmp0->checksum = ip_csum_fold (sum0);
740 /* update inner IP header checksum */
741 old_checksum0 = inner_ip0->checksum;
742 sum0 = inner_ip0->checksum;
743 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
744 dst_address /* changed member */ );
745 inner_ip0->checksum = ip_csum_fold (sum0);
746 new_checksum0 = inner_ip0->checksum;
747 sum0 = icmp0->checksum;
749 ip_csum_update (sum0, old_checksum0, new_checksum0, ip4_header_t,
751 icmp0->checksum = ip_csum_fold (sum0);
755 case NAT_PROTOCOL_ICMP:
756 inner_icmp0 = (icmp46_header_t *) l4_header;
757 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
759 old_id0 = inner_echo0->identifier;
761 inner_echo0->identifier = new_id0;
763 sum0 = icmp0->checksum;
765 ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
767 icmp0->checksum = ip_csum_fold (sum0);
769 case NAT_PROTOCOL_UDP:
770 case NAT_PROTOCOL_TCP:
771 old_id0 = ((tcp_udp_header_t *) l4_header)->dst_port;
773 ((tcp_udp_header_t *) l4_header)->dst_port = new_id0;
775 sum0 = icmp0->checksum;
776 sum0 = ip_csum_update (sum0, old_id0, new_id0, tcp_udp_header_t,
778 icmp0->checksum = ip_csum_fold (sum0);
786 if (vnet_buffer (b0)->sw_if_index[VLIB_TX] == ~0)
788 if (0 != snat_icmp_hairpinning (sm, b0, ip0, icmp0,
789 sm->endpoint_dependent))
790 vnet_buffer (b0)->sw_if_index[VLIB_TX] = fib_index;
799 icmp_in2out_slow_path (snat_main_t * sm,
802 icmp46_header_t * icmp0,
805 vlib_node_runtime_t * node,
807 f64 now, u32 thread_index, snat_session_t ** p_s0)
809 vlib_main_t *vm = vlib_get_main ();
811 next0 = icmp_in2out (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
812 next0, thread_index, p_s0, 0);
813 snat_session_t *s0 = *p_s0;
814 if (PREDICT_TRUE (next0 != SNAT_IN2OUT_NEXT_DROP && s0))
817 nat44_ei_session_update_counters (
818 s0, now, vlib_buffer_length_in_chain (vm, b0), thread_index);
819 /* Per-user LRU list maintenance */
820 nat44_session_update_lru (sm, s0, thread_index);
826 nat_in2out_sm_unknown_proto (snat_main_t * sm,
828 ip4_header_t * ip, u32 rx_fib_index)
830 clib_bihash_kv_8_8_t kv, value;
831 snat_static_mapping_t *m;
832 u32 old_addr, new_addr;
835 init_nat_k (&kv, ip->src_address, 0, rx_fib_index, 0);
836 if (clib_bihash_search_8_8 (&sm->static_mapping_by_local, &kv, &value))
839 m = pool_elt_at_index (sm->static_mappings, value.value);
841 old_addr = ip->src_address.as_u32;
842 new_addr = ip->src_address.as_u32 = m->external_addr.as_u32;
844 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, src_address);
845 ip->checksum = ip_csum_fold (sum);
849 if (vnet_buffer (b)->sw_if_index[VLIB_TX] == ~0)
851 vnet_buffer (b)->sw_if_index[VLIB_TX] = m->fib_index;
852 nat_hairpinning_sm_unknown_proto (sm, b, ip);
859 snat_in2out_node_fn_inline (vlib_main_t * vm,
860 vlib_node_runtime_t * node,
861 vlib_frame_t * frame, int is_slow_path,
862 int is_output_feature)
864 u32 n_left_from, *from;
865 snat_main_t *sm = &snat_main;
866 f64 now = vlib_time_now (vm);
867 u32 thread_index = vm->thread_index;
869 from = vlib_frame_vector_args (frame);
870 n_left_from = frame->n_vectors;
872 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
873 u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
874 vlib_get_buffers (vm, from, b, n_left_from);
876 while (n_left_from >= 2)
878 vlib_buffer_t *b0, *b1;
880 u32 sw_if_index0, sw_if_index1;
881 ip4_header_t *ip0, *ip1;
882 ip_csum_t sum0, sum1;
883 u32 new_addr0, old_addr0, new_addr1, old_addr1;
884 u16 old_port0, new_port0, old_port1, new_port1;
885 udp_header_t *udp0, *udp1;
886 tcp_header_t *tcp0, *tcp1;
887 icmp46_header_t *icmp0, *icmp1;
888 u32 rx_fib_index0, rx_fib_index1;
890 snat_session_t *s0 = 0, *s1 = 0;
891 clib_bihash_kv_8_8_t kv0, value0, kv1, value1;
892 u32 iph_offset0 = 0, iph_offset1 = 0;
899 /* Prefetch next iteration. */
900 if (PREDICT_TRUE (n_left_from >= 4))
902 vlib_buffer_t *p2, *p3;
907 vlib_prefetch_buffer_header (p2, LOAD);
908 vlib_prefetch_buffer_header (p3, LOAD);
910 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, LOAD);
911 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, LOAD);
914 if (is_output_feature)
915 iph_offset0 = vnet_buffer (b0)->ip.reass.save_rewrite_length;
917 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
920 udp0 = ip4_next_header (ip0);
921 tcp0 = (tcp_header_t *) udp0;
922 icmp0 = (icmp46_header_t *) udp0;
924 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
925 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
928 next0 = next1 = SNAT_IN2OUT_NEXT_LOOKUP;
930 if (PREDICT_FALSE (ip0->ttl == 1))
932 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
933 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
934 ICMP4_time_exceeded_ttl_exceeded_in_transit,
936 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
940 proto0 = ip_proto_to_nat_proto (ip0->protocol);
942 /* Next configured feature, probably ip4-lookup */
945 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
947 if (nat_in2out_sm_unknown_proto (sm, b0, ip0, rx_fib_index0))
949 next0 = SNAT_IN2OUT_NEXT_DROP;
951 node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
953 vlib_increment_simple_counter (is_slow_path ? &sm->
954 counters.slowpath.in2out.
955 other : &sm->counters.fastpath.
956 in2out.other, thread_index,
961 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
963 next0 = icmp_in2out_slow_path
964 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0,
965 node, next0, now, thread_index, &s0);
966 vlib_increment_simple_counter (is_slow_path ? &sm->
967 counters.slowpath.in2out.
968 icmp : &sm->counters.fastpath.
969 in2out.icmp, thread_index,
976 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
978 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
982 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
984 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
989 init_nat_k (&kv0, ip0->src_address,
990 vnet_buffer (b0)->ip.reass.l4_src_port, rx_fib_index0,
993 (clib_bihash_search_8_8
994 (&sm->per_thread_data[thread_index].in2out, &kv0, &value0) != 0))
998 if (is_output_feature)
1001 (nat_not_translate_output_feature
1003 vnet_buffer (b0)->ip.reass.l4_src_port,
1004 vnet_buffer (b0)->ip.reass.l4_dst_port,
1005 thread_index, sw_if_index0)))
1009 * Send DHCP packets to the ipv4 stack, or we won't
1010 * be able to use dhcp client on the outside interface
1013 (proto0 == NAT_PROTOCOL_UDP
1014 && (vnet_buffer (b0)->ip.reass.l4_dst_port ==
1015 clib_host_to_net_u16
1016 (UDP_DST_PORT_dhcp_to_server))
1017 && ip0->dst_address.as_u32 == 0xffffffff))
1024 (sm, node, sw_if_index0, ip0, proto0,
1025 rx_fib_index0, thread_index)))
1029 next0 = slow_path (sm, b0, ip0,
1031 vnet_buffer (b0)->ip.reass.l4_src_port,
1033 proto0, &s0, node, next0, thread_index, now);
1034 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
1037 if (PREDICT_FALSE (!s0))
1042 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1048 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1051 b0->flags |= VNET_BUFFER_F_IS_NATED;
1053 old_addr0 = ip0->src_address.as_u32;
1054 ip0->src_address = s0->out2in.addr;
1055 new_addr0 = ip0->src_address.as_u32;
1056 if (!is_output_feature)
1057 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1059 sum0 = ip0->checksum;
1060 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1061 ip4_header_t, src_address /* changed member */ );
1062 ip0->checksum = ip_csum_fold (sum0);
1065 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1067 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1069 old_port0 = vnet_buffer (b0)->ip.reass.l4_src_port;
1070 new_port0 = udp0->src_port = s0->out2in.port;
1071 sum0 = tcp0->checksum;
1072 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1074 dst_address /* changed member */ );
1075 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1076 ip4_header_t /* cheat */ ,
1077 length /* changed member */ );
1078 mss_clamping (sm->mss_clamping, tcp0, &sum0);
1079 tcp0->checksum = ip_csum_fold (sum0);
1081 vlib_increment_simple_counter (is_slow_path ? &sm->
1082 counters.slowpath.in2out.tcp : &sm->
1083 counters.fastpath.in2out.tcp,
1084 thread_index, sw_if_index0, 1);
1088 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1090 udp0->src_port = s0->out2in.port;
1091 if (PREDICT_FALSE (udp0->checksum))
1093 old_port0 = vnet_buffer (b0)->ip.reass.l4_src_port;
1094 new_port0 = udp0->src_port;
1095 sum0 = udp0->checksum;
1096 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t, dst_address /* changed member */
1099 ip_csum_update (sum0, old_port0, new_port0,
1100 ip4_header_t /* cheat */ ,
1101 length /* changed member */ );
1102 udp0->checksum = ip_csum_fold (sum0);
1105 vlib_increment_simple_counter (is_slow_path ? &sm->
1106 counters.slowpath.in2out.udp : &sm->
1107 counters.fastpath.in2out.udp,
1108 thread_index, sw_if_index0, 1);
1112 nat44_ei_session_update_counters (
1113 s0, now, vlib_buffer_length_in_chain (vm, b0), thread_index);
1114 /* Per-user LRU list maintenance */
1115 nat44_session_update_lru (sm, s0, thread_index);
1118 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1119 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1121 snat_in2out_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
1122 t->is_slow_path = is_slow_path;
1123 t->sw_if_index = sw_if_index0;
1124 t->next_index = next0;
1125 t->session_index = ~0;
1128 s0 - sm->per_thread_data[thread_index].sessions;
1131 if (next0 == SNAT_IN2OUT_NEXT_DROP)
1133 vlib_increment_simple_counter (is_slow_path ? &sm->
1134 counters.slowpath.in2out.
1135 drops : &sm->counters.fastpath.
1136 in2out.drops, thread_index,
1140 if (is_output_feature)
1141 iph_offset1 = vnet_buffer (b1)->ip.reass.save_rewrite_length;
1143 ip1 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b1) +
1146 udp1 = ip4_next_header (ip1);
1147 tcp1 = (tcp_header_t *) udp1;
1148 icmp1 = (icmp46_header_t *) udp1;
1150 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
1151 rx_fib_index1 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1154 if (PREDICT_FALSE (ip1->ttl == 1))
1156 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1157 icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
1158 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1160 next1 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1164 proto1 = ip_proto_to_nat_proto (ip1->protocol);
1166 /* Next configured feature, probably ip4-lookup */
1169 if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_OTHER))
1171 if (nat_in2out_sm_unknown_proto (sm, b1, ip1, rx_fib_index1))
1173 next1 = SNAT_IN2OUT_NEXT_DROP;
1175 node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
1177 vlib_increment_simple_counter (is_slow_path ? &sm->
1178 counters.slowpath.in2out.
1179 other : &sm->counters.fastpath.
1180 in2out.other, thread_index,
1185 if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_ICMP))
1187 next1 = icmp_in2out_slow_path
1188 (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node,
1189 next1, now, thread_index, &s1);
1190 vlib_increment_simple_counter (is_slow_path ? &sm->
1191 counters.slowpath.in2out.
1192 icmp : &sm->counters.fastpath.
1193 in2out.icmp, thread_index,
1200 if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_OTHER))
1202 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1206 if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_ICMP))
1208 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1213 init_nat_k (&kv1, ip1->src_address,
1214 vnet_buffer (b1)->ip.reass.l4_src_port, rx_fib_index1,
1217 (clib_bihash_search_8_8
1218 (&sm->per_thread_data[thread_index].in2out, &kv1, &value1) != 0))
1222 if (is_output_feature)
1225 (nat_not_translate_output_feature
1227 vnet_buffer (b1)->ip.reass.l4_src_port,
1228 vnet_buffer (b1)->ip.reass.l4_dst_port,
1229 thread_index, sw_if_index1)))
1233 * Send DHCP packets to the ipv4 stack, or we won't
1234 * be able to use dhcp client on the outside interface
1237 (proto1 == NAT_PROTOCOL_UDP
1238 && (vnet_buffer (b1)->ip.reass.l4_dst_port ==
1239 clib_host_to_net_u16
1240 (UDP_DST_PORT_dhcp_to_server))
1241 && ip1->dst_address.as_u32 == 0xffffffff))
1248 (sm, node, sw_if_index1, ip1, proto1,
1249 rx_fib_index1, thread_index)))
1254 slow_path (sm, b1, ip1, ip1->src_address,
1255 vnet_buffer (b1)->ip.reass.l4_src_port,
1256 rx_fib_index1, proto1, &s1, node, next1,
1258 if (PREDICT_FALSE (next1 == SNAT_IN2OUT_NEXT_DROP))
1261 if (PREDICT_FALSE (!s1))
1266 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1272 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1275 b1->flags |= VNET_BUFFER_F_IS_NATED;
1277 old_addr1 = ip1->src_address.as_u32;
1278 ip1->src_address = s1->out2in.addr;
1279 new_addr1 = ip1->src_address.as_u32;
1280 if (!is_output_feature)
1281 vnet_buffer (b1)->sw_if_index[VLIB_TX] = s1->out2in.fib_index;
1283 sum1 = ip1->checksum;
1284 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1285 ip4_header_t, src_address /* changed member */ );
1286 ip1->checksum = ip_csum_fold (sum1);
1288 if (PREDICT_TRUE (proto1 == NAT_PROTOCOL_TCP))
1290 if (!vnet_buffer (b1)->ip.reass.is_non_first_fragment)
1292 old_port1 = vnet_buffer (b1)->ip.reass.l4_src_port;
1293 new_port1 = udp1->src_port = s1->out2in.port;
1294 sum1 = tcp1->checksum;
1295 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1297 dst_address /* changed member */ );
1298 sum1 = ip_csum_update (sum1, old_port1, new_port1,
1299 ip4_header_t /* cheat */ ,
1300 length /* changed member */ );
1301 mss_clamping (sm->mss_clamping, tcp1, &sum1);
1302 tcp1->checksum = ip_csum_fold (sum1);
1304 vlib_increment_simple_counter (is_slow_path ? &sm->
1305 counters.slowpath.in2out.tcp : &sm->
1306 counters.fastpath.in2out.tcp,
1307 thread_index, sw_if_index1, 1);
1311 if (!vnet_buffer (b1)->ip.reass.is_non_first_fragment)
1313 udp1->src_port = s1->out2in.port;
1314 if (PREDICT_FALSE (udp1->checksum))
1316 old_port1 = vnet_buffer (b1)->ip.reass.l4_src_port;
1317 new_port1 = udp1->src_port;
1318 sum1 = udp1->checksum;
1319 sum1 = ip_csum_update (sum1, old_addr1, new_addr1, ip4_header_t, dst_address /* changed member */
1322 ip_csum_update (sum1, old_port1, new_port1,
1323 ip4_header_t /* cheat */ ,
1324 length /* changed member */ );
1325 udp1->checksum = ip_csum_fold (sum1);
1328 vlib_increment_simple_counter (is_slow_path ? &sm->
1329 counters.slowpath.in2out.udp : &sm->
1330 counters.fastpath.in2out.udp,
1331 thread_index, sw_if_index1, 1);
1335 nat44_ei_session_update_counters (
1336 s1, now, vlib_buffer_length_in_chain (vm, b1), thread_index);
1337 /* Per-user LRU list maintenance */
1338 nat44_session_update_lru (sm, s1, thread_index);
1341 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1342 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
1344 snat_in2out_trace_t *t = vlib_add_trace (vm, node, b1, sizeof (*t));
1345 t->sw_if_index = sw_if_index1;
1346 t->next_index = next1;
1347 t->session_index = ~0;
1350 s1 - sm->per_thread_data[thread_index].sessions;
1353 if (next1 == SNAT_IN2OUT_NEXT_DROP)
1355 vlib_increment_simple_counter (is_slow_path ? &sm->
1356 counters.slowpath.in2out.
1357 drops : &sm->counters.fastpath.
1358 in2out.drops, thread_index,
1368 while (n_left_from > 0)
1375 u32 new_addr0, old_addr0;
1376 u16 old_port0, new_port0;
1379 icmp46_header_t *icmp0;
1382 snat_session_t *s0 = 0;
1383 clib_bihash_kv_8_8_t kv0, value0;
1384 u32 iph_offset0 = 0;
1388 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
1390 if (is_output_feature)
1391 iph_offset0 = vnet_buffer (b0)->ip.reass.save_rewrite_length;
1393 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
1396 udp0 = ip4_next_header (ip0);
1397 tcp0 = (tcp_header_t *) udp0;
1398 icmp0 = (icmp46_header_t *) udp0;
1400 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1401 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1404 if (PREDICT_FALSE (ip0->ttl == 1))
1406 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1407 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1408 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1410 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1414 proto0 = ip_proto_to_nat_proto (ip0->protocol);
1416 /* Next configured feature, probably ip4-lookup */
1419 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1421 if (nat_in2out_sm_unknown_proto (sm, b0, ip0, rx_fib_index0))
1423 next0 = SNAT_IN2OUT_NEXT_DROP;
1425 node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
1427 vlib_increment_simple_counter (is_slow_path ? &sm->
1428 counters.slowpath.in2out.
1429 other : &sm->counters.fastpath.
1430 in2out.other, thread_index,
1435 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1437 next0 = icmp_in2out_slow_path
1438 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1439 next0, now, thread_index, &s0);
1440 vlib_increment_simple_counter (is_slow_path ? &sm->
1441 counters.slowpath.in2out.
1442 icmp : &sm->counters.fastpath.
1443 in2out.icmp, thread_index,
1450 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1452 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1456 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1458 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1463 init_nat_k (&kv0, ip0->src_address,
1464 vnet_buffer (b0)->ip.reass.l4_src_port, rx_fib_index0,
1467 if (clib_bihash_search_8_8
1468 (&sm->per_thread_data[thread_index].in2out, &kv0, &value0))
1472 if (is_output_feature)
1475 (nat_not_translate_output_feature
1477 vnet_buffer (b0)->ip.reass.l4_src_port,
1478 vnet_buffer (b0)->ip.reass.l4_dst_port,
1479 thread_index, sw_if_index0)))
1483 * Send DHCP packets to the ipv4 stack, or we won't
1484 * be able to use dhcp client on the outside interface
1487 (proto0 == NAT_PROTOCOL_UDP
1488 && (vnet_buffer (b0)->ip.reass.l4_dst_port ==
1489 clib_host_to_net_u16
1490 (UDP_DST_PORT_dhcp_to_server))
1491 && ip0->dst_address.as_u32 == 0xffffffff))
1498 (sm, node, sw_if_index0, ip0, proto0, rx_fib_index0,
1504 slow_path (sm, b0, ip0, ip0->src_address,
1505 vnet_buffer (b0)->ip.reass.l4_src_port,
1506 rx_fib_index0, proto0, &s0, node, next0,
1509 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
1512 if (PREDICT_FALSE (!s0))
1517 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1523 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1526 b0->flags |= VNET_BUFFER_F_IS_NATED;
1528 old_addr0 = ip0->src_address.as_u32;
1529 ip0->src_address = s0->out2in.addr;
1530 new_addr0 = ip0->src_address.as_u32;
1531 if (!is_output_feature)
1532 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1534 sum0 = ip0->checksum;
1535 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1536 ip4_header_t, src_address /* changed member */ );
1537 ip0->checksum = ip_csum_fold (sum0);
1539 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1541 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1543 old_port0 = vnet_buffer (b0)->ip.reass.l4_src_port;
1544 new_port0 = udp0->src_port = s0->out2in.port;
1545 sum0 = tcp0->checksum;
1547 ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1548 dst_address /* changed member */ );
1550 ip_csum_update (sum0, old_port0, new_port0,
1551 ip4_header_t /* cheat */ ,
1552 length /* changed member */ );
1553 mss_clamping (sm->mss_clamping, tcp0, &sum0);
1554 tcp0->checksum = ip_csum_fold (sum0);
1556 vlib_increment_simple_counter (is_slow_path ? &sm->
1557 counters.slowpath.in2out.tcp : &sm->
1558 counters.fastpath.in2out.tcp,
1559 thread_index, sw_if_index0, 1);
1563 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1565 udp0->src_port = s0->out2in.port;
1566 if (PREDICT_FALSE (udp0->checksum))
1568 old_port0 = vnet_buffer (b0)->ip.reass.l4_src_port;
1569 new_port0 = udp0->src_port;
1570 sum0 = udp0->checksum;
1572 ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1573 dst_address /* changed member */ );
1575 ip_csum_update (sum0, old_port0, new_port0,
1576 ip4_header_t /* cheat */ ,
1577 length /* changed member */ );
1578 udp0->checksum = ip_csum_fold (sum0);
1581 vlib_increment_simple_counter (is_slow_path ? &sm->
1582 counters.slowpath.in2out.udp : &sm->
1583 counters.fastpath.in2out.udp,
1584 thread_index, sw_if_index0, 1);
1588 nat44_ei_session_update_counters (
1589 s0, now, vlib_buffer_length_in_chain (vm, b0), thread_index);
1590 /* Per-user LRU list maintenance */
1591 nat44_session_update_lru (sm, s0, thread_index);
1594 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1595 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1597 snat_in2out_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
1598 t->is_slow_path = is_slow_path;
1599 t->sw_if_index = sw_if_index0;
1600 t->next_index = next0;
1601 t->session_index = ~0;
1604 s0 - sm->per_thread_data[thread_index].sessions;
1607 if (next0 == SNAT_IN2OUT_NEXT_DROP)
1609 vlib_increment_simple_counter (is_slow_path ? &sm->
1610 counters.slowpath.in2out.
1611 drops : &sm->counters.fastpath.
1612 in2out.drops, thread_index,
1621 vlib_buffer_enqueue_to_next (vm, node, from, (u16 *) nexts,
1623 return frame->n_vectors;
1626 VLIB_NODE_FN (snat_in2out_node) (vlib_main_t * vm,
1627 vlib_node_runtime_t * node,
1628 vlib_frame_t * frame)
1630 return snat_in2out_node_fn_inline (vm, node, frame, 0 /* is_slow_path */ ,
1635 VLIB_REGISTER_NODE (snat_in2out_node) = {
1636 .name = "nat44-in2out",
1637 .vector_size = sizeof (u32),
1638 .format_trace = format_snat_in2out_trace,
1639 .type = VLIB_NODE_TYPE_INTERNAL,
1641 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1642 .error_strings = snat_in2out_error_strings,
1644 .runtime_data_bytes = sizeof (snat_runtime_t),
1646 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1648 /* edit / add dispositions here */
1650 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1651 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1652 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
1653 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1658 VLIB_NODE_FN (snat_in2out_output_node) (vlib_main_t * vm,
1659 vlib_node_runtime_t * node,
1660 vlib_frame_t * frame)
1662 return snat_in2out_node_fn_inline (vm, node, frame, 0 /* is_slow_path */ ,
1667 VLIB_REGISTER_NODE (snat_in2out_output_node) = {
1668 .name = "nat44-in2out-output",
1669 .vector_size = sizeof (u32),
1670 .format_trace = format_snat_in2out_trace,
1671 .type = VLIB_NODE_TYPE_INTERNAL,
1673 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1674 .error_strings = snat_in2out_error_strings,
1676 .runtime_data_bytes = sizeof (snat_runtime_t),
1678 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1680 /* edit / add dispositions here */
1682 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1683 [SNAT_IN2OUT_NEXT_LOOKUP] = "interface-output",
1684 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-output-slowpath",
1685 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1690 VLIB_NODE_FN (snat_in2out_slowpath_node) (vlib_main_t * vm,
1691 vlib_node_runtime_t * node,
1692 vlib_frame_t * frame)
1694 return snat_in2out_node_fn_inline (vm, node, frame, 1 /* is_slow_path */ ,
1699 VLIB_REGISTER_NODE (snat_in2out_slowpath_node) = {
1700 .name = "nat44-in2out-slowpath",
1701 .vector_size = sizeof (u32),
1702 .format_trace = format_snat_in2out_trace,
1703 .type = VLIB_NODE_TYPE_INTERNAL,
1705 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1706 .error_strings = snat_in2out_error_strings,
1708 .runtime_data_bytes = sizeof (snat_runtime_t),
1710 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1712 /* edit / add dispositions here */
1714 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1715 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1716 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
1717 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1722 VLIB_NODE_FN (snat_in2out_output_slowpath_node) (vlib_main_t * vm,
1723 vlib_node_runtime_t * node,
1724 vlib_frame_t * frame)
1726 return snat_in2out_node_fn_inline (vm, node, frame, 1 /* is_slow_path */ ,
1731 VLIB_REGISTER_NODE (snat_in2out_output_slowpath_node) = {
1732 .name = "nat44-in2out-output-slowpath",
1733 .vector_size = sizeof (u32),
1734 .format_trace = format_snat_in2out_trace,
1735 .type = VLIB_NODE_TYPE_INTERNAL,
1737 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1738 .error_strings = snat_in2out_error_strings,
1740 .runtime_data_bytes = sizeof (snat_runtime_t),
1742 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1744 /* edit / add dispositions here */
1746 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1747 [SNAT_IN2OUT_NEXT_LOOKUP] = "interface-output",
1748 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-output-slowpath",
1749 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1754 VLIB_NODE_FN (snat_in2out_fast_node) (vlib_main_t * vm,
1755 vlib_node_runtime_t * node,
1756 vlib_frame_t * frame)
1758 u32 n_left_from, *from, *to_next;
1759 snat_in2out_next_t next_index;
1760 snat_main_t *sm = &snat_main;
1761 int is_hairpinning = 0;
1763 from = vlib_frame_vector_args (frame);
1764 n_left_from = frame->n_vectors;
1765 next_index = node->cached_next_index;
1767 while (n_left_from > 0)
1771 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1773 while (n_left_from > 0 && n_left_to_next > 0)
1781 u32 new_addr0, old_addr0;
1782 u16 old_port0, new_port0;
1785 icmp46_header_t *icmp0;
1788 ip4_address_t sm0_addr;
1793 /* speculatively enqueue b0 to the current next frame */
1799 n_left_to_next -= 1;
1801 b0 = vlib_get_buffer (vm, bi0);
1802 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
1804 ip0 = vlib_buffer_get_current (b0);
1805 udp0 = ip4_next_header (ip0);
1806 tcp0 = (tcp_header_t *) udp0;
1807 icmp0 = (icmp46_header_t *) udp0;
1809 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1811 ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
1813 if (PREDICT_FALSE (ip0->ttl == 1))
1815 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1816 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1817 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1819 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1823 proto0 = ip_proto_to_nat_proto (ip0->protocol);
1825 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1828 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1830 next0 = icmp_in2out (sm, b0, ip0, icmp0, sw_if_index0,
1831 rx_fib_index0, node, next0, ~0, 0, 0);
1835 if (nat44_ei_static_mapping_match (
1836 ip0->src_address, udp0->src_port, rx_fib_index0, proto0,
1837 &sm0_addr, &sm0_port, &sm0_fib_index, 0, 0, 0))
1839 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
1840 next0 = SNAT_IN2OUT_NEXT_DROP;
1844 new_addr0 = sm0_addr.as_u32;
1845 new_port0 = sm0_port;
1846 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm0_fib_index;
1847 old_addr0 = ip0->src_address.as_u32;
1848 ip0->src_address.as_u32 = new_addr0;
1850 sum0 = ip0->checksum;
1851 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1853 src_address /* changed member */ );
1854 ip0->checksum = ip_csum_fold (sum0);
1856 if (PREDICT_FALSE (new_port0 != udp0->dst_port))
1858 old_port0 = udp0->src_port;
1859 udp0->src_port = new_port0;
1861 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1863 sum0 = tcp0->checksum;
1864 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1866 dst_address /* changed member */ );
1867 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1868 ip4_header_t /* cheat */ ,
1869 length /* changed member */ );
1870 mss_clamping (sm->mss_clamping, tcp0, &sum0);
1871 tcp0->checksum = ip_csum_fold (sum0);
1873 else if (udp0->checksum)
1875 sum0 = udp0->checksum;
1876 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1878 dst_address /* changed member */ );
1879 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1880 ip4_header_t /* cheat */ ,
1881 length /* changed member */ );
1882 udp0->checksum = ip_csum_fold (sum0);
1887 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1889 sum0 = tcp0->checksum;
1890 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1892 dst_address /* changed member */ );
1893 mss_clamping (sm->mss_clamping, tcp0, &sum0);
1894 tcp0->checksum = ip_csum_fold (sum0);
1896 else if (udp0->checksum)
1898 sum0 = udp0->checksum;
1899 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1901 dst_address /* changed member */ );
1902 udp0->checksum = ip_csum_fold (sum0);
1908 snat_hairpinning (vm, node, sm, b0, ip0, udp0, tcp0, proto0, 0,
1912 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1913 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1915 snat_in2out_trace_t *t =
1916 vlib_add_trace (vm, node, b0, sizeof (*t));
1917 t->sw_if_index = sw_if_index0;
1918 t->next_index = next0;
1919 t->is_hairpinning = is_hairpinning;
1922 if (next0 != SNAT_IN2OUT_NEXT_DROP)
1925 vlib_increment_simple_counter (&sm->counters.fastpath.
1926 in2out.other, sw_if_index0,
1927 vm->thread_index, 1);
1930 /* verify speculative enqueue, maybe switch current next frame */
1931 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1932 to_next, n_left_to_next,
1936 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1939 return frame->n_vectors;
1944 VLIB_REGISTER_NODE (snat_in2out_fast_node) = {
1945 .name = "nat44-in2out-fast",
1946 .vector_size = sizeof (u32),
1947 .format_trace = format_snat_in2out_fast_trace,
1948 .type = VLIB_NODE_TYPE_INTERNAL,
1950 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1951 .error_strings = snat_in2out_error_strings,
1953 .runtime_data_bytes = sizeof (snat_runtime_t),
1955 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1957 /* edit / add dispositions here */
1959 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1960 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1961 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
1962 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1968 * fd.io coding-style-patch-verification: ON
1971 * eval: (c-set-style "gnu")