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/nat44/inlines.h>
31 #include <nat/lib/nat_syslog.h>
32 #include <nat/nat_ha.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 (!snat_static_mapping_match
135 (sm, ip0->dst_address, udp0->dst_port, sm->outside_fib_index,
136 proto0, &placeholder_addr, &placeholder_port,
137 &placeholder_fib_index, 1, 0, 0, 0, 0, 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_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 (snat_static_mapping_match
275 (sm, i2o_addr, i2o_port, rx_fib_index0, nat_proto, &sm_addr,
276 &sm_port, &sm_fib_index, 0, 0, 0, 0, 0, &identity_nat, 0))
278 /* Try to create dynamic translation */
279 if (snat_alloc_outside_address_and_port (sm->addresses, rx_fib_index0,
285 [thread_index].snat_thread_index))
287 b0->error = node->errors[SNAT_IN2OUT_ERROR_OUT_OF_PORTS];
288 return SNAT_IN2OUT_NEXT_DROP;
293 if (PREDICT_FALSE (identity_nat))
302 u = nat_user_get_or_create (sm, &ip0->src_address, rx_fib_index0,
306 b0->error = node->errors[SNAT_IN2OUT_ERROR_CANNOT_CREATE_USER];
307 return SNAT_IN2OUT_NEXT_DROP;
310 s = nat_session_alloc_or_recycle (sm, u, thread_index, now);
313 nat44_delete_user_with_no_session (sm, u, thread_index);
314 nat_elog_warn ("create NAT session failed");
315 return SNAT_IN2OUT_NEXT_DROP;
319 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
320 user_session_increment (sm, u, is_sm);
321 s->in2out.addr = i2o_addr;
322 s->in2out.port = i2o_port;
323 s->in2out.fib_index = rx_fib_index0;
324 s->nat_proto = nat_proto;
325 s->out2in.addr = sm_addr;
326 s->out2in.port = sm_port;
327 s->out2in.fib_index = sm->outside_fib_index;
328 switch (vec_len (sm->outside_fibs))
331 s->out2in.fib_index = sm->outside_fib_index;
334 s->out2in.fib_index = sm->outside_fibs[0].fib_index;
338 vec_foreach (outside_fib, sm->outside_fibs)
340 fei = fib_table_lookup (outside_fib->fib_index, &pfx);
341 if (FIB_NODE_INDEX_INVALID != fei)
343 if (fib_entry_get_resolving_interface (fei) != ~0)
345 s->out2in.fib_index = outside_fib->fib_index;
353 s->ext_host_addr.as_u32 = ip0->dst_address.as_u32;
354 s->ext_host_port = vnet_buffer (b0)->ip.reass.l4_dst_port;
357 /* Add to translation hashes */
359 ctx0.thread_index = thread_index;
360 init_nat_i2o_kv (&kv0, s, s - sm->per_thread_data[thread_index].sessions);
361 if (clib_bihash_add_or_overwrite_stale_8_8
362 (&sm->per_thread_data[thread_index].in2out, &kv0,
363 nat44_i2o_is_idle_session_cb, &ctx0))
364 nat_elog_notice ("in2out key add failed");
366 init_nat_o2i_kv (&kv0, s, s - sm->per_thread_data[thread_index].sessions);
367 if (clib_bihash_add_or_overwrite_stale_8_8
368 (&sm->per_thread_data[thread_index].out2in, &kv0,
369 nat44_o2i_is_idle_session_cb, &ctx0))
370 nat_elog_notice ("out2in key add failed");
373 nat_ipfix_logging_nat44_ses_create (thread_index,
374 s->in2out.addr.as_u32,
375 s->out2in.addr.as_u32,
378 s->out2in.port, s->in2out.fib_index);
380 nat_syslog_nat44_apmadd (s->user_index, s->in2out.fib_index,
381 &s->in2out.addr, s->in2out.port, &s->out2in.addr,
382 s->out2in.port, s->nat_proto);
384 nat_ha_sadd (&s->in2out.addr, s->in2out.port, &s->out2in.addr,
385 s->out2in.port, &s->ext_host_addr, s->ext_host_port,
386 &s->ext_host_nat_addr, s->ext_host_nat_port,
387 s->nat_proto, s->in2out.fib_index, s->flags, thread_index, 0);
392 #ifndef CLIB_MARCH_VARIANT
393 static_always_inline snat_in2out_error_t
394 icmp_get_key (vlib_buffer_t * b, ip4_header_t * ip0,
395 ip4_address_t * addr, u16 * port, nat_protocol_t * nat_proto)
397 icmp46_header_t *icmp0;
398 icmp_echo_header_t *echo0, *inner_echo0 = 0;
399 ip4_header_t *inner_ip0 = 0;
401 icmp46_header_t *inner_icmp0;
403 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
404 echo0 = (icmp_echo_header_t *) (icmp0 + 1);
406 if (!icmp_type_is_error_message
407 (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags))
409 *nat_proto = NAT_PROTOCOL_ICMP;
410 *addr = ip0->src_address;
411 *port = vnet_buffer (b)->ip.reass.l4_src_port;
415 inner_ip0 = (ip4_header_t *) (echo0 + 1);
416 l4_header = ip4_next_header (inner_ip0);
417 *nat_proto = ip_proto_to_nat_proto (inner_ip0->protocol);
418 *addr = inner_ip0->dst_address;
421 case NAT_PROTOCOL_ICMP:
422 inner_icmp0 = (icmp46_header_t *) l4_header;
423 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
424 *port = inner_echo0->identifier;
426 case NAT_PROTOCOL_UDP:
427 case NAT_PROTOCOL_TCP:
428 *port = ((tcp_udp_header_t *) l4_header)->dst_port;
431 return SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL;
434 return -1; /* success */
438 * Get address and port values to be used for ICMP packet translation
439 * and create session if needed
441 * @param[in,out] sm NAT main
442 * @param[in,out] node NAT node runtime
443 * @param[in] thread_index thread index
444 * @param[in,out] b0 buffer containing packet to be translated
445 * @param[in,out] ip0 ip header
446 * @param[out] p_proto protocol used for matching
447 * @param[out] p_value address and port after NAT translation
448 * @param[out] p_dont_translate if packet should not be translated
449 * @param d optional parameter
450 * @param e optional parameter
453 icmp_match_in2out_slow (snat_main_t * sm, vlib_node_runtime_t * node,
454 u32 thread_index, vlib_buffer_t * b0,
455 ip4_header_t * ip0, ip4_address_t * addr, u16 * port,
456 u32 * fib_index, nat_protocol_t * proto, void *d,
457 void *e, u8 * dont_translate)
459 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
461 snat_session_t *s0 = 0;
462 clib_bihash_kv_8_8_t kv0, value0;
465 vlib_main_t *vm = vlib_get_main ();
468 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
469 *fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
471 err = icmp_get_key (b0, ip0, addr, port, proto);
474 b0->error = node->errors[err];
475 next0 = SNAT_IN2OUT_NEXT_DROP;
479 init_nat_k (&kv0, *addr, *port, *fib_index, *proto);
480 if (clib_bihash_search_8_8 (&tsm->in2out, &kv0, &value0))
482 if (vnet_buffer (b0)->sw_if_index[VLIB_TX] != ~0)
485 (nat_not_translate_output_feature
486 (sm, ip0, *proto, *port, *port, thread_index, sw_if_index0)))
494 if (PREDICT_FALSE (snat_not_translate (sm, node, sw_if_index0,
495 ip0, NAT_PROTOCOL_ICMP,
496 *fib_index, thread_index)))
504 (icmp_type_is_error_message
505 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags)))
507 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
508 next0 = SNAT_IN2OUT_NEXT_DROP;
513 slow_path (sm, b0, ip0, *addr, *port, *fib_index, *proto, &s0, node,
514 next0, thread_index, vlib_time_now (vm));
516 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
528 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
530 && vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
532 && !icmp_type_is_error_message (vnet_buffer (b0)->ip.
533 reass.icmp_type_or_tcp_flags)))
535 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
536 next0 = SNAT_IN2OUT_NEXT_DROP;
540 s0 = pool_elt_at_index (tsm->sessions, value0.value);
546 *addr = s0->out2in.addr;
547 *port = s0->out2in.port;
548 *fib_index = s0->out2in.fib_index;
551 *(snat_session_t **) (d) = s0;
556 #ifndef CLIB_MARCH_VARIANT
558 * Get address and port values to be used for ICMP packet translation
560 * @param[in] sm NAT main
561 * @param[in,out] node NAT node runtime
562 * @param[in] thread_index thread index
563 * @param[in,out] b0 buffer containing packet to be translated
564 * @param[in,out] ip0 ip header
565 * @param[out] p_proto protocol used for matching
566 * @param[out] p_value address and port after NAT translation
567 * @param[out] p_dont_translate if packet should not be translated
568 * @param d optional parameter
569 * @param e optional parameter
572 icmp_match_in2out_fast (snat_main_t * sm, vlib_node_runtime_t * node,
573 u32 thread_index, vlib_buffer_t * b0,
574 ip4_header_t * ip0, ip4_address_t * addr, u16 * port,
575 u32 * fib_index, nat_protocol_t * proto, void *d,
576 void *e, u8 * dont_translate)
584 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
585 *fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
587 err = icmp_get_key (b0, ip0, addr, port, proto);
590 b0->error = node->errors[err];
591 next0 = SNAT_IN2OUT_NEXT_DROP;
595 ip4_address_t sm_addr;
599 if (snat_static_mapping_match
600 (sm, *addr, *port, *fib_index, *proto, &sm_addr, &sm_port,
601 &sm_fib_index, 0, &is_addr_only, 0, 0, 0, 0, 0))
603 if (PREDICT_FALSE (snat_not_translate_fast (sm, node, sw_if_index0, ip0,
611 if (icmp_type_is_error_message
612 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags))
614 next0 = SNAT_IN2OUT_NEXT_DROP;
618 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
619 next0 = SNAT_IN2OUT_NEXT_DROP;
624 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags != ICMP4_echo_request
625 && (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
626 ICMP4_echo_reply || !is_addr_only)
627 && !icmp_type_is_error_message (vnet_buffer (b0)->ip.
628 reass.icmp_type_or_tcp_flags)))
630 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
631 next0 = SNAT_IN2OUT_NEXT_DROP;
640 #ifndef CLIB_MARCH_VARIANT
642 icmp_in2out (snat_main_t * sm,
645 icmp46_header_t * icmp0,
648 vlib_node_runtime_t * node,
649 u32 next0, u32 thread_index, void *d, void *e)
651 vlib_main_t *vm = vlib_get_main ();
655 nat_protocol_t protocol;
656 icmp_echo_header_t *echo0, *inner_echo0 = 0;
657 ip4_header_t *inner_ip0;
659 icmp46_header_t *inner_icmp0;
661 u32 new_addr0, old_addr0;
662 u16 old_id0, new_id0;
663 u16 old_checksum0, new_checksum0;
668 echo0 = (icmp_echo_header_t *) (icmp0 + 1);
671 sm->icmp_match_in2out_cb (sm, node, thread_index, b0, ip0, &addr, &port,
672 &fib_index, &protocol, d, e, &dont_translate);
675 if (next0 == SNAT_IN2OUT_NEXT_DROP || dont_translate)
678 if (PREDICT_TRUE (!ip4_is_fragment (ip0)))
681 ip_incremental_checksum_buffer (vm, b0,
683 (u8 *) vlib_buffer_get_current (b0),
684 ntohs (ip0->length) -
685 ip4_header_bytes (ip0), 0);
686 checksum0 = ~ip_csum_fold (sum0);
687 if (PREDICT_FALSE (checksum0 != 0 && checksum0 != 0xffff))
689 next0 = SNAT_IN2OUT_NEXT_DROP;
694 old_addr0 = ip0->src_address.as_u32;
695 new_addr0 = ip0->src_address.as_u32 = addr.as_u32;
697 sum0 = ip0->checksum;
698 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
699 src_address /* changed member */ );
700 ip0->checksum = ip_csum_fold (sum0);
702 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
704 if (icmp0->checksum == 0)
705 icmp0->checksum = 0xffff;
707 if (!icmp_type_is_error_message (icmp0->type))
710 if (PREDICT_FALSE (new_id0 != echo0->identifier))
712 old_id0 = echo0->identifier;
714 echo0->identifier = new_id0;
716 sum0 = icmp0->checksum;
718 ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
720 icmp0->checksum = ip_csum_fold (sum0);
725 inner_ip0 = (ip4_header_t *) (echo0 + 1);
726 l4_header = ip4_next_header (inner_ip0);
728 if (!ip4_header_checksum_is_valid (inner_ip0))
730 next0 = SNAT_IN2OUT_NEXT_DROP;
734 /* update inner destination IP address */
735 old_addr0 = inner_ip0->dst_address.as_u32;
736 inner_ip0->dst_address = addr;
737 new_addr0 = inner_ip0->dst_address.as_u32;
738 sum0 = icmp0->checksum;
739 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
740 dst_address /* changed member */ );
741 icmp0->checksum = ip_csum_fold (sum0);
743 /* update inner IP header checksum */
744 old_checksum0 = inner_ip0->checksum;
745 sum0 = inner_ip0->checksum;
746 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
747 dst_address /* changed member */ );
748 inner_ip0->checksum = ip_csum_fold (sum0);
749 new_checksum0 = inner_ip0->checksum;
750 sum0 = icmp0->checksum;
752 ip_csum_update (sum0, old_checksum0, new_checksum0, ip4_header_t,
754 icmp0->checksum = ip_csum_fold (sum0);
758 case NAT_PROTOCOL_ICMP:
759 inner_icmp0 = (icmp46_header_t *) l4_header;
760 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
762 old_id0 = inner_echo0->identifier;
764 inner_echo0->identifier = new_id0;
766 sum0 = icmp0->checksum;
768 ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
770 icmp0->checksum = ip_csum_fold (sum0);
772 case NAT_PROTOCOL_UDP:
773 case NAT_PROTOCOL_TCP:
774 old_id0 = ((tcp_udp_header_t *) l4_header)->dst_port;
776 ((tcp_udp_header_t *) l4_header)->dst_port = new_id0;
778 sum0 = icmp0->checksum;
779 sum0 = ip_csum_update (sum0, old_id0, new_id0, tcp_udp_header_t,
781 icmp0->checksum = ip_csum_fold (sum0);
789 if (vnet_buffer (b0)->sw_if_index[VLIB_TX] == ~0)
791 if (0 != snat_icmp_hairpinning (sm, b0, ip0, icmp0,
792 sm->endpoint_dependent))
793 vnet_buffer (b0)->sw_if_index[VLIB_TX] = fib_index;
802 icmp_in2out_slow_path (snat_main_t * sm,
805 icmp46_header_t * icmp0,
808 vlib_node_runtime_t * node,
810 f64 now, u32 thread_index, snat_session_t ** p_s0)
812 vlib_main_t *vm = vlib_get_main ();
814 next0 = icmp_in2out (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
815 next0, thread_index, p_s0, 0);
816 snat_session_t *s0 = *p_s0;
817 if (PREDICT_TRUE (next0 != SNAT_IN2OUT_NEXT_DROP && s0))
820 nat44_session_update_counters (s0, now,
821 vlib_buffer_length_in_chain
822 (vm, b0), thread_index);
823 /* Per-user LRU list maintenance */
824 nat44_session_update_lru (sm, s0, thread_index);
830 nat_in2out_sm_unknown_proto (snat_main_t * sm,
832 ip4_header_t * ip, u32 rx_fib_index)
834 clib_bihash_kv_8_8_t kv, value;
835 snat_static_mapping_t *m;
836 u32 old_addr, new_addr;
839 init_nat_k (&kv, ip->src_address, 0, rx_fib_index, 0);
840 if (clib_bihash_search_8_8 (&sm->static_mapping_by_local, &kv, &value))
843 m = pool_elt_at_index (sm->static_mappings, value.value);
845 old_addr = ip->src_address.as_u32;
846 new_addr = ip->src_address.as_u32 = m->external_addr.as_u32;
848 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, src_address);
849 ip->checksum = ip_csum_fold (sum);
853 if (vnet_buffer (b)->sw_if_index[VLIB_TX] == ~0)
855 vnet_buffer (b)->sw_if_index[VLIB_TX] = m->fib_index;
856 nat_hairpinning_sm_unknown_proto (sm, b, ip);
863 snat_in2out_node_fn_inline (vlib_main_t * vm,
864 vlib_node_runtime_t * node,
865 vlib_frame_t * frame, int is_slow_path,
866 int is_output_feature)
868 u32 n_left_from, *from;
869 snat_main_t *sm = &snat_main;
870 f64 now = vlib_time_now (vm);
871 u32 thread_index = vm->thread_index;
873 from = vlib_frame_vector_args (frame);
874 n_left_from = frame->n_vectors;
876 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
877 u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
878 vlib_get_buffers (vm, from, b, n_left_from);
880 while (n_left_from >= 2)
882 vlib_buffer_t *b0, *b1;
884 u32 sw_if_index0, sw_if_index1;
885 ip4_header_t *ip0, *ip1;
886 ip_csum_t sum0, sum1;
887 u32 new_addr0, old_addr0, new_addr1, old_addr1;
888 u16 old_port0, new_port0, old_port1, new_port1;
889 udp_header_t *udp0, *udp1;
890 tcp_header_t *tcp0, *tcp1;
891 icmp46_header_t *icmp0, *icmp1;
892 u32 rx_fib_index0, rx_fib_index1;
894 snat_session_t *s0 = 0, *s1 = 0;
895 clib_bihash_kv_8_8_t kv0, value0, kv1, value1;
896 u32 iph_offset0 = 0, iph_offset1 = 0;
903 /* Prefetch next iteration. */
904 if (PREDICT_TRUE (n_left_from >= 4))
906 vlib_buffer_t *p2, *p3;
911 vlib_prefetch_buffer_header (p2, LOAD);
912 vlib_prefetch_buffer_header (p3, LOAD);
914 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, LOAD);
915 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, LOAD);
918 if (is_output_feature)
919 iph_offset0 = vnet_buffer (b0)->ip.reass.save_rewrite_length;
921 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
924 udp0 = ip4_next_header (ip0);
925 tcp0 = (tcp_header_t *) udp0;
926 icmp0 = (icmp46_header_t *) udp0;
928 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
929 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
932 next0 = next1 = SNAT_IN2OUT_NEXT_LOOKUP;
934 if (PREDICT_FALSE (ip0->ttl == 1))
936 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
937 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
938 ICMP4_time_exceeded_ttl_exceeded_in_transit,
940 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
944 proto0 = ip_proto_to_nat_proto (ip0->protocol);
946 /* Next configured feature, probably ip4-lookup */
949 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
951 if (nat_in2out_sm_unknown_proto (sm, b0, ip0, rx_fib_index0))
953 next0 = SNAT_IN2OUT_NEXT_DROP;
955 node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
957 vlib_increment_simple_counter (is_slow_path ? &sm->
958 counters.slowpath.in2out.
959 other : &sm->counters.fastpath.
960 in2out.other, thread_index,
965 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
967 next0 = icmp_in2out_slow_path
968 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0,
969 node, next0, now, thread_index, &s0);
970 vlib_increment_simple_counter (is_slow_path ? &sm->
971 counters.slowpath.in2out.
972 icmp : &sm->counters.fastpath.
973 in2out.icmp, thread_index,
980 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
982 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
986 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
988 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
993 init_nat_k (&kv0, ip0->src_address,
994 vnet_buffer (b0)->ip.reass.l4_src_port, rx_fib_index0,
997 (clib_bihash_search_8_8
998 (&sm->per_thread_data[thread_index].in2out, &kv0, &value0) != 0))
1002 if (is_output_feature)
1005 (nat_not_translate_output_feature
1007 vnet_buffer (b0)->ip.reass.l4_src_port,
1008 vnet_buffer (b0)->ip.reass.l4_dst_port,
1009 thread_index, sw_if_index0)))
1013 * Send DHCP packets to the ipv4 stack, or we won't
1014 * be able to use dhcp client on the outside interface
1017 (proto0 == NAT_PROTOCOL_UDP
1018 && (vnet_buffer (b0)->ip.reass.l4_dst_port ==
1019 clib_host_to_net_u16
1020 (UDP_DST_PORT_dhcp_to_server))
1021 && ip0->dst_address.as_u32 == 0xffffffff))
1028 (sm, node, sw_if_index0, ip0, proto0,
1029 rx_fib_index0, thread_index)))
1033 next0 = slow_path (sm, b0, ip0,
1035 vnet_buffer (b0)->ip.reass.l4_src_port,
1037 proto0, &s0, node, next0, thread_index, now);
1038 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
1041 if (PREDICT_FALSE (!s0))
1046 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1052 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1055 b0->flags |= VNET_BUFFER_F_IS_NATED;
1057 old_addr0 = ip0->src_address.as_u32;
1058 ip0->src_address = s0->out2in.addr;
1059 new_addr0 = ip0->src_address.as_u32;
1060 if (!is_output_feature)
1061 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1063 sum0 = ip0->checksum;
1064 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1065 ip4_header_t, src_address /* changed member */ );
1066 ip0->checksum = ip_csum_fold (sum0);
1069 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1071 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1073 old_port0 = vnet_buffer (b0)->ip.reass.l4_src_port;
1074 new_port0 = udp0->src_port = s0->out2in.port;
1075 sum0 = tcp0->checksum;
1076 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1078 dst_address /* changed member */ );
1079 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1080 ip4_header_t /* cheat */ ,
1081 length /* changed member */ );
1082 mss_clamping (sm->mss_clamping, tcp0, &sum0);
1083 tcp0->checksum = ip_csum_fold (sum0);
1085 vlib_increment_simple_counter (is_slow_path ? &sm->
1086 counters.slowpath.in2out.tcp : &sm->
1087 counters.fastpath.in2out.tcp,
1088 thread_index, sw_if_index0, 1);
1092 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1094 udp0->src_port = s0->out2in.port;
1095 if (PREDICT_FALSE (udp0->checksum))
1097 old_port0 = vnet_buffer (b0)->ip.reass.l4_src_port;
1098 new_port0 = udp0->src_port;
1099 sum0 = udp0->checksum;
1100 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t, dst_address /* changed member */
1103 ip_csum_update (sum0, old_port0, new_port0,
1104 ip4_header_t /* cheat */ ,
1105 length /* changed member */ );
1106 udp0->checksum = ip_csum_fold (sum0);
1109 vlib_increment_simple_counter (is_slow_path ? &sm->
1110 counters.slowpath.in2out.udp : &sm->
1111 counters.fastpath.in2out.udp,
1112 thread_index, sw_if_index0, 1);
1116 nat44_session_update_counters (s0, now,
1117 vlib_buffer_length_in_chain (vm, b0),
1119 /* Per-user LRU list maintenance */
1120 nat44_session_update_lru (sm, s0, thread_index);
1123 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1124 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1126 snat_in2out_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
1127 t->is_slow_path = is_slow_path;
1128 t->sw_if_index = sw_if_index0;
1129 t->next_index = next0;
1130 t->session_index = ~0;
1133 s0 - sm->per_thread_data[thread_index].sessions;
1136 if (next0 == SNAT_IN2OUT_NEXT_DROP)
1138 vlib_increment_simple_counter (is_slow_path ? &sm->
1139 counters.slowpath.in2out.
1140 drops : &sm->counters.fastpath.
1141 in2out.drops, thread_index,
1145 if (is_output_feature)
1146 iph_offset1 = vnet_buffer (b1)->ip.reass.save_rewrite_length;
1148 ip1 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b1) +
1151 udp1 = ip4_next_header (ip1);
1152 tcp1 = (tcp_header_t *) udp1;
1153 icmp1 = (icmp46_header_t *) udp1;
1155 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
1156 rx_fib_index1 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1159 if (PREDICT_FALSE (ip1->ttl == 1))
1161 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1162 icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
1163 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1165 next1 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1169 proto1 = ip_proto_to_nat_proto (ip1->protocol);
1171 /* Next configured feature, probably ip4-lookup */
1174 if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_OTHER))
1176 if (nat_in2out_sm_unknown_proto (sm, b1, ip1, rx_fib_index1))
1178 next1 = SNAT_IN2OUT_NEXT_DROP;
1180 node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
1182 vlib_increment_simple_counter (is_slow_path ? &sm->
1183 counters.slowpath.in2out.
1184 other : &sm->counters.fastpath.
1185 in2out.other, thread_index,
1190 if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_ICMP))
1192 next1 = icmp_in2out_slow_path
1193 (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node,
1194 next1, now, thread_index, &s1);
1195 vlib_increment_simple_counter (is_slow_path ? &sm->
1196 counters.slowpath.in2out.
1197 icmp : &sm->counters.fastpath.
1198 in2out.icmp, thread_index,
1205 if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_OTHER))
1207 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1211 if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_ICMP))
1213 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1218 init_nat_k (&kv1, ip1->src_address,
1219 vnet_buffer (b1)->ip.reass.l4_src_port, rx_fib_index1,
1222 (clib_bihash_search_8_8
1223 (&sm->per_thread_data[thread_index].in2out, &kv1, &value1) != 0))
1227 if (is_output_feature)
1230 (nat_not_translate_output_feature
1232 vnet_buffer (b1)->ip.reass.l4_src_port,
1233 vnet_buffer (b1)->ip.reass.l4_dst_port,
1234 thread_index, sw_if_index1)))
1238 * Send DHCP packets to the ipv4 stack, or we won't
1239 * be able to use dhcp client on the outside interface
1242 (proto1 == NAT_PROTOCOL_UDP
1243 && (vnet_buffer (b1)->ip.reass.l4_dst_port ==
1244 clib_host_to_net_u16
1245 (UDP_DST_PORT_dhcp_to_server))
1246 && ip1->dst_address.as_u32 == 0xffffffff))
1253 (sm, node, sw_if_index1, ip1, proto1,
1254 rx_fib_index1, thread_index)))
1259 slow_path (sm, b1, ip1, ip1->src_address,
1260 vnet_buffer (b1)->ip.reass.l4_src_port,
1261 rx_fib_index1, proto1, &s1, node, next1,
1263 if (PREDICT_FALSE (next1 == SNAT_IN2OUT_NEXT_DROP))
1266 if (PREDICT_FALSE (!s1))
1271 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1277 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1280 b1->flags |= VNET_BUFFER_F_IS_NATED;
1282 old_addr1 = ip1->src_address.as_u32;
1283 ip1->src_address = s1->out2in.addr;
1284 new_addr1 = ip1->src_address.as_u32;
1285 if (!is_output_feature)
1286 vnet_buffer (b1)->sw_if_index[VLIB_TX] = s1->out2in.fib_index;
1288 sum1 = ip1->checksum;
1289 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1290 ip4_header_t, src_address /* changed member */ );
1291 ip1->checksum = ip_csum_fold (sum1);
1293 if (PREDICT_TRUE (proto1 == NAT_PROTOCOL_TCP))
1295 if (!vnet_buffer (b1)->ip.reass.is_non_first_fragment)
1297 old_port1 = vnet_buffer (b1)->ip.reass.l4_src_port;
1298 new_port1 = udp1->src_port = s1->out2in.port;
1299 sum1 = tcp1->checksum;
1300 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1302 dst_address /* changed member */ );
1303 sum1 = ip_csum_update (sum1, old_port1, new_port1,
1304 ip4_header_t /* cheat */ ,
1305 length /* changed member */ );
1306 mss_clamping (sm->mss_clamping, tcp1, &sum1);
1307 tcp1->checksum = ip_csum_fold (sum1);
1309 vlib_increment_simple_counter (is_slow_path ? &sm->
1310 counters.slowpath.in2out.tcp : &sm->
1311 counters.fastpath.in2out.tcp,
1312 thread_index, sw_if_index1, 1);
1316 if (!vnet_buffer (b1)->ip.reass.is_non_first_fragment)
1318 udp1->src_port = s1->out2in.port;
1319 if (PREDICT_FALSE (udp1->checksum))
1321 old_port1 = vnet_buffer (b1)->ip.reass.l4_src_port;
1322 new_port1 = udp1->src_port;
1323 sum1 = udp1->checksum;
1324 sum1 = ip_csum_update (sum1, old_addr1, new_addr1, ip4_header_t, dst_address /* changed member */
1327 ip_csum_update (sum1, old_port1, new_port1,
1328 ip4_header_t /* cheat */ ,
1329 length /* changed member */ );
1330 udp1->checksum = ip_csum_fold (sum1);
1333 vlib_increment_simple_counter (is_slow_path ? &sm->
1334 counters.slowpath.in2out.udp : &sm->
1335 counters.fastpath.in2out.udp,
1336 thread_index, sw_if_index1, 1);
1340 nat44_session_update_counters (s1, now,
1341 vlib_buffer_length_in_chain (vm, b1),
1343 /* Per-user LRU list maintenance */
1344 nat44_session_update_lru (sm, s1, thread_index);
1347 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1348 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
1350 snat_in2out_trace_t *t = vlib_add_trace (vm, node, b1, sizeof (*t));
1351 t->sw_if_index = sw_if_index1;
1352 t->next_index = next1;
1353 t->session_index = ~0;
1356 s1 - sm->per_thread_data[thread_index].sessions;
1359 if (next1 == SNAT_IN2OUT_NEXT_DROP)
1361 vlib_increment_simple_counter (is_slow_path ? &sm->
1362 counters.slowpath.in2out.
1363 drops : &sm->counters.fastpath.
1364 in2out.drops, thread_index,
1374 while (n_left_from > 0)
1381 u32 new_addr0, old_addr0;
1382 u16 old_port0, new_port0;
1385 icmp46_header_t *icmp0;
1388 snat_session_t *s0 = 0;
1389 clib_bihash_kv_8_8_t kv0, value0;
1390 u32 iph_offset0 = 0;
1394 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
1396 if (is_output_feature)
1397 iph_offset0 = vnet_buffer (b0)->ip.reass.save_rewrite_length;
1399 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
1402 udp0 = ip4_next_header (ip0);
1403 tcp0 = (tcp_header_t *) udp0;
1404 icmp0 = (icmp46_header_t *) udp0;
1406 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1407 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1410 if (PREDICT_FALSE (ip0->ttl == 1))
1412 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1413 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1414 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1416 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1420 proto0 = ip_proto_to_nat_proto (ip0->protocol);
1422 /* Next configured feature, probably ip4-lookup */
1425 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1427 if (nat_in2out_sm_unknown_proto (sm, b0, ip0, rx_fib_index0))
1429 next0 = SNAT_IN2OUT_NEXT_DROP;
1431 node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
1433 vlib_increment_simple_counter (is_slow_path ? &sm->
1434 counters.slowpath.in2out.
1435 other : &sm->counters.fastpath.
1436 in2out.other, thread_index,
1441 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1443 next0 = icmp_in2out_slow_path
1444 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1445 next0, now, thread_index, &s0);
1446 vlib_increment_simple_counter (is_slow_path ? &sm->
1447 counters.slowpath.in2out.
1448 icmp : &sm->counters.fastpath.
1449 in2out.icmp, thread_index,
1456 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1458 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1462 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1464 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1469 init_nat_k (&kv0, ip0->src_address,
1470 vnet_buffer (b0)->ip.reass.l4_src_port, rx_fib_index0,
1473 if (clib_bihash_search_8_8
1474 (&sm->per_thread_data[thread_index].in2out, &kv0, &value0))
1478 if (is_output_feature)
1481 (nat_not_translate_output_feature
1483 vnet_buffer (b0)->ip.reass.l4_src_port,
1484 vnet_buffer (b0)->ip.reass.l4_dst_port,
1485 thread_index, sw_if_index0)))
1489 * Send DHCP packets to the ipv4 stack, or we won't
1490 * be able to use dhcp client on the outside interface
1493 (proto0 == NAT_PROTOCOL_UDP
1494 && (vnet_buffer (b0)->ip.reass.l4_dst_port ==
1495 clib_host_to_net_u16
1496 (UDP_DST_PORT_dhcp_to_server))
1497 && ip0->dst_address.as_u32 == 0xffffffff))
1504 (sm, node, sw_if_index0, ip0, proto0, rx_fib_index0,
1510 slow_path (sm, b0, ip0, ip0->src_address,
1511 vnet_buffer (b0)->ip.reass.l4_src_port,
1512 rx_fib_index0, proto0, &s0, node, next0,
1515 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
1518 if (PREDICT_FALSE (!s0))
1523 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1529 pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1532 b0->flags |= VNET_BUFFER_F_IS_NATED;
1534 old_addr0 = ip0->src_address.as_u32;
1535 ip0->src_address = s0->out2in.addr;
1536 new_addr0 = ip0->src_address.as_u32;
1537 if (!is_output_feature)
1538 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1540 sum0 = ip0->checksum;
1541 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1542 ip4_header_t, src_address /* changed member */ );
1543 ip0->checksum = ip_csum_fold (sum0);
1545 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1547 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1549 old_port0 = vnet_buffer (b0)->ip.reass.l4_src_port;
1550 new_port0 = udp0->src_port = s0->out2in.port;
1551 sum0 = tcp0->checksum;
1553 ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1554 dst_address /* changed member */ );
1556 ip_csum_update (sum0, old_port0, new_port0,
1557 ip4_header_t /* cheat */ ,
1558 length /* changed member */ );
1559 mss_clamping (sm->mss_clamping, tcp0, &sum0);
1560 tcp0->checksum = ip_csum_fold (sum0);
1562 vlib_increment_simple_counter (is_slow_path ? &sm->
1563 counters.slowpath.in2out.tcp : &sm->
1564 counters.fastpath.in2out.tcp,
1565 thread_index, sw_if_index0, 1);
1569 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1571 udp0->src_port = s0->out2in.port;
1572 if (PREDICT_FALSE (udp0->checksum))
1574 old_port0 = vnet_buffer (b0)->ip.reass.l4_src_port;
1575 new_port0 = udp0->src_port;
1576 sum0 = udp0->checksum;
1578 ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1579 dst_address /* changed member */ );
1581 ip_csum_update (sum0, old_port0, new_port0,
1582 ip4_header_t /* cheat */ ,
1583 length /* changed member */ );
1584 udp0->checksum = ip_csum_fold (sum0);
1587 vlib_increment_simple_counter (is_slow_path ? &sm->
1588 counters.slowpath.in2out.udp : &sm->
1589 counters.fastpath.in2out.udp,
1590 thread_index, sw_if_index0, 1);
1594 nat44_session_update_counters (s0, now,
1595 vlib_buffer_length_in_chain (vm, b0),
1597 /* Per-user LRU list maintenance */
1598 nat44_session_update_lru (sm, s0, thread_index);
1601 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1602 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1604 snat_in2out_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
1605 t->is_slow_path = is_slow_path;
1606 t->sw_if_index = sw_if_index0;
1607 t->next_index = next0;
1608 t->session_index = ~0;
1611 s0 - sm->per_thread_data[thread_index].sessions;
1614 if (next0 == SNAT_IN2OUT_NEXT_DROP)
1616 vlib_increment_simple_counter (is_slow_path ? &sm->
1617 counters.slowpath.in2out.
1618 drops : &sm->counters.fastpath.
1619 in2out.drops, thread_index,
1628 vlib_buffer_enqueue_to_next (vm, node, from, (u16 *) nexts,
1630 return frame->n_vectors;
1633 VLIB_NODE_FN (snat_in2out_node) (vlib_main_t * vm,
1634 vlib_node_runtime_t * node,
1635 vlib_frame_t * frame)
1637 return snat_in2out_node_fn_inline (vm, node, frame, 0 /* is_slow_path */ ,
1642 VLIB_REGISTER_NODE (snat_in2out_node) = {
1643 .name = "nat44-in2out",
1644 .vector_size = sizeof (u32),
1645 .format_trace = format_snat_in2out_trace,
1646 .type = VLIB_NODE_TYPE_INTERNAL,
1648 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1649 .error_strings = snat_in2out_error_strings,
1651 .runtime_data_bytes = sizeof (snat_runtime_t),
1653 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1655 /* edit / add dispositions here */
1657 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1658 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1659 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
1660 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1665 VLIB_NODE_FN (snat_in2out_output_node) (vlib_main_t * vm,
1666 vlib_node_runtime_t * node,
1667 vlib_frame_t * frame)
1669 return snat_in2out_node_fn_inline (vm, node, frame, 0 /* is_slow_path */ ,
1674 VLIB_REGISTER_NODE (snat_in2out_output_node) = {
1675 .name = "nat44-in2out-output",
1676 .vector_size = sizeof (u32),
1677 .format_trace = format_snat_in2out_trace,
1678 .type = VLIB_NODE_TYPE_INTERNAL,
1680 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1681 .error_strings = snat_in2out_error_strings,
1683 .runtime_data_bytes = sizeof (snat_runtime_t),
1685 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1687 /* edit / add dispositions here */
1689 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1690 [SNAT_IN2OUT_NEXT_LOOKUP] = "interface-output",
1691 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-output-slowpath",
1692 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1697 VLIB_NODE_FN (snat_in2out_slowpath_node) (vlib_main_t * vm,
1698 vlib_node_runtime_t * node,
1699 vlib_frame_t * frame)
1701 return snat_in2out_node_fn_inline (vm, node, frame, 1 /* is_slow_path */ ,
1706 VLIB_REGISTER_NODE (snat_in2out_slowpath_node) = {
1707 .name = "nat44-in2out-slowpath",
1708 .vector_size = sizeof (u32),
1709 .format_trace = format_snat_in2out_trace,
1710 .type = VLIB_NODE_TYPE_INTERNAL,
1712 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1713 .error_strings = snat_in2out_error_strings,
1715 .runtime_data_bytes = sizeof (snat_runtime_t),
1717 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1719 /* edit / add dispositions here */
1721 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1722 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1723 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
1724 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1729 VLIB_NODE_FN (snat_in2out_output_slowpath_node) (vlib_main_t * vm,
1730 vlib_node_runtime_t * node,
1731 vlib_frame_t * frame)
1733 return snat_in2out_node_fn_inline (vm, node, frame, 1 /* is_slow_path */ ,
1738 VLIB_REGISTER_NODE (snat_in2out_output_slowpath_node) = {
1739 .name = "nat44-in2out-output-slowpath",
1740 .vector_size = sizeof (u32),
1741 .format_trace = format_snat_in2out_trace,
1742 .type = VLIB_NODE_TYPE_INTERNAL,
1744 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1745 .error_strings = snat_in2out_error_strings,
1747 .runtime_data_bytes = sizeof (snat_runtime_t),
1749 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1751 /* edit / add dispositions here */
1753 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1754 [SNAT_IN2OUT_NEXT_LOOKUP] = "interface-output",
1755 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-output-slowpath",
1756 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1761 VLIB_NODE_FN (snat_in2out_fast_node) (vlib_main_t * vm,
1762 vlib_node_runtime_t * node,
1763 vlib_frame_t * frame)
1765 u32 n_left_from, *from, *to_next;
1766 snat_in2out_next_t next_index;
1767 snat_main_t *sm = &snat_main;
1768 int is_hairpinning = 0;
1770 from = vlib_frame_vector_args (frame);
1771 n_left_from = frame->n_vectors;
1772 next_index = node->cached_next_index;
1774 while (n_left_from > 0)
1778 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1780 while (n_left_from > 0 && n_left_to_next > 0)
1788 u32 new_addr0, old_addr0;
1789 u16 old_port0, new_port0;
1792 icmp46_header_t *icmp0;
1795 ip4_address_t sm0_addr;
1800 /* speculatively enqueue b0 to the current next frame */
1806 n_left_to_next -= 1;
1808 b0 = vlib_get_buffer (vm, bi0);
1809 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
1811 ip0 = vlib_buffer_get_current (b0);
1812 udp0 = ip4_next_header (ip0);
1813 tcp0 = (tcp_header_t *) udp0;
1814 icmp0 = (icmp46_header_t *) udp0;
1816 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1818 ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
1820 if (PREDICT_FALSE (ip0->ttl == 1))
1822 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1823 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1824 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1826 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1830 proto0 = ip_proto_to_nat_proto (ip0->protocol);
1832 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1835 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1837 next0 = icmp_in2out (sm, b0, ip0, icmp0, sw_if_index0,
1838 rx_fib_index0, node, next0, ~0, 0, 0);
1842 if (snat_static_mapping_match
1843 (sm, ip0->src_address, udp0->src_port, rx_fib_index0, proto0,
1844 &sm0_addr, &sm0_port, &sm0_fib_index, 0, 0, 0, 0, 0, 0, 0))
1846 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
1847 next0 = SNAT_IN2OUT_NEXT_DROP;
1851 new_addr0 = sm0_addr.as_u32;
1852 new_port0 = sm0_port;
1853 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm0_fib_index;
1854 old_addr0 = ip0->src_address.as_u32;
1855 ip0->src_address.as_u32 = new_addr0;
1857 sum0 = ip0->checksum;
1858 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1860 src_address /* changed member */ );
1861 ip0->checksum = ip_csum_fold (sum0);
1863 if (PREDICT_FALSE (new_port0 != udp0->dst_port))
1865 old_port0 = udp0->src_port;
1866 udp0->src_port = new_port0;
1868 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1870 sum0 = tcp0->checksum;
1871 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1873 dst_address /* changed member */ );
1874 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1875 ip4_header_t /* cheat */ ,
1876 length /* changed member */ );
1877 mss_clamping (sm->mss_clamping, tcp0, &sum0);
1878 tcp0->checksum = ip_csum_fold (sum0);
1880 else if (udp0->checksum)
1882 sum0 = udp0->checksum;
1883 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1885 dst_address /* changed member */ );
1886 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1887 ip4_header_t /* cheat */ ,
1888 length /* changed member */ );
1889 udp0->checksum = ip_csum_fold (sum0);
1894 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1896 sum0 = tcp0->checksum;
1897 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1899 dst_address /* changed member */ );
1900 mss_clamping (sm->mss_clamping, tcp0, &sum0);
1901 tcp0->checksum = ip_csum_fold (sum0);
1903 else if (udp0->checksum)
1905 sum0 = udp0->checksum;
1906 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1908 dst_address /* changed member */ );
1909 udp0->checksum = ip_csum_fold (sum0);
1915 snat_hairpinning (vm, node, sm, b0, ip0, udp0, tcp0, proto0, 0,
1919 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1920 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1922 snat_in2out_trace_t *t =
1923 vlib_add_trace (vm, node, b0, sizeof (*t));
1924 t->sw_if_index = sw_if_index0;
1925 t->next_index = next0;
1926 t->is_hairpinning = is_hairpinning;
1929 if (next0 != SNAT_IN2OUT_NEXT_DROP)
1932 vlib_increment_simple_counter (&sm->counters.fastpath.
1933 in2out.other, sw_if_index0,
1934 vm->thread_index, 1);
1937 /* verify speculative enqueue, maybe switch current next frame */
1938 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1939 to_next, n_left_to_next,
1943 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1946 return frame->n_vectors;
1951 VLIB_REGISTER_NODE (snat_in2out_fast_node) = {
1952 .name = "nat44-in2out-fast",
1953 .vector_size = sizeof (u32),
1954 .format_trace = format_snat_in2out_fast_trace,
1955 .type = VLIB_NODE_TYPE_INTERNAL,
1957 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1958 .error_strings = snat_in2out_error_strings,
1960 .runtime_data_bytes = sizeof (snat_runtime_t),
1962 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1964 /* edit / add dispositions here */
1966 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1967 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1968 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
1969 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1975 * fd.io coding-style-patch-verification: ON
1978 * eval: (c-set-style "gnu")