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->out2in, &kv0, &value0))
129 /* or is static mappings */
130 ip4_address_t placeholder_addr;
131 u16 placeholder_port;
132 u32 placeholder_fib_index;
133 if (!nat44_ei_static_mapping_match (ip0->dst_address, udp0->dst_port,
134 sm->outside_fib_index, proto0,
135 &placeholder_addr, &placeholder_port,
136 &placeholder_fib_index, 1, 0, 0))
142 if (sm->forwarding_enabled)
145 return snat_not_translate_fast (sm, node, sw_if_index0, ip0, proto0,
150 nat_not_translate_output_feature (snat_main_t * sm, ip4_header_t * ip0,
151 u32 proto0, u16 src_port, u16 dst_port,
152 u32 thread_index, u32 sw_if_index)
154 clib_bihash_kv_8_8_t kv0, value0;
158 init_nat_k (&kv0, ip0->src_address, src_port,
159 ip4_fib_table_get_index_for_sw_if_index (sw_if_index), proto0);
161 if (!clib_bihash_search_8_8 (&sm->out2in, &kv0, &value0))
165 init_nat_k (&kv0, ip0->dst_address, dst_port,
166 ip4_fib_table_get_index_for_sw_if_index (sw_if_index), proto0);
167 if (!clib_bihash_search_8_8 (&sm->in2out, &kv0, &value0))
171 pool_foreach (i, sm->output_feature_interfaces)
173 if ((nat_interface_is_inside(i)) && (sw_if_index == i->sw_if_index))
183 #ifndef CLIB_MARCH_VARIANT
185 nat44_i2o_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void *arg)
187 snat_main_t *sm = &snat_main;
188 nat44_is_idle_session_ctx_t *ctx = arg;
190 u64 sess_timeout_time;
191 snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
193 clib_bihash_kv_8_8_t s_kv;
195 s = pool_elt_at_index (tsm->sessions, kv->value);
196 sess_timeout_time = s->last_heard + (f64) nat44_session_get_timeout (sm, s);
197 if (ctx->now >= sess_timeout_time)
199 init_nat_o2i_k (&s_kv, s);
200 if (clib_bihash_add_del_8_8 (&sm->out2in, &s_kv, 0))
201 nat_elog_warn ("out2in key del failed");
203 nat_ipfix_logging_nat44_ses_delete (ctx->thread_index,
204 s->in2out.addr.as_u32,
205 s->out2in.addr.as_u32,
209 s->in2out.fib_index);
211 nat_syslog_nat44_apmdel (s->user_index, s->in2out.fib_index,
212 &s->in2out.addr, s->in2out.port,
213 &s->out2in.addr, s->out2in.port, s->nat_proto);
215 nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
216 s->ext_host_port, s->nat_proto, s->out2in.fib_index,
219 if (!snat_is_session_static (s))
220 snat_free_outside_address_and_port (sm->addresses, ctx->thread_index,
222 s->out2in.port, s->nat_proto);
224 nat44_delete_session (sm, s, ctx->thread_index);
233 slow_path (snat_main_t * sm, vlib_buffer_t * b0,
235 ip4_address_t i2o_addr,
238 nat_protocol_t nat_proto,
239 snat_session_t ** sessionp,
240 vlib_node_runtime_t * node, u32 next0, u32 thread_index, f64 now)
243 snat_session_t *s = 0;
244 clib_bihash_kv_8_8_t kv0;
246 nat_outside_fib_t *outside_fib;
247 fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
250 .fp_proto = FIB_PROTOCOL_IP4,
253 .ip4.as_u32 = ip0->dst_address.as_u32,
256 nat44_is_idle_session_ctx_t ctx0;
257 ip4_address_t sm_addr;
261 if (PREDICT_FALSE (nat44_ei_maximum_sessions_exceeded (sm, thread_index)))
263 b0->error = node->errors[SNAT_IN2OUT_ERROR_MAX_SESSIONS_EXCEEDED];
264 nat_ipfix_logging_max_sessions (thread_index,
265 sm->max_translations_per_thread);
266 nat_elog_notice ("maximum sessions exceeded");
267 return SNAT_IN2OUT_NEXT_DROP;
270 /* First try to match static mapping by local address and port */
271 if (nat44_ei_static_mapping_match (i2o_addr, i2o_port, rx_fib_index0,
272 nat_proto, &sm_addr, &sm_port,
273 &sm_fib_index, 0, 0, &identity_nat))
275 /* Try to create dynamic translation */
276 if (sm->alloc_addr_and_port (
277 sm->addresses, rx_fib_index0, thread_index, nat_proto, &sm_addr,
278 &sm_port, sm->port_per_thread,
279 sm->per_thread_data[thread_index].snat_thread_index))
281 b0->error = node->errors[SNAT_IN2OUT_ERROR_OUT_OF_PORTS];
282 return SNAT_IN2OUT_NEXT_DROP;
287 if (PREDICT_FALSE (identity_nat))
296 u = nat_user_get_or_create (sm, &ip0->src_address, rx_fib_index0,
300 b0->error = node->errors[SNAT_IN2OUT_ERROR_CANNOT_CREATE_USER];
301 return SNAT_IN2OUT_NEXT_DROP;
304 s = nat_session_alloc_or_recycle (sm, u, thread_index, now);
307 nat44_delete_user_with_no_session (sm, u, thread_index);
308 nat_elog_warn ("create NAT session failed");
309 return SNAT_IN2OUT_NEXT_DROP;
313 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
314 user_session_increment (sm, u, is_sm);
315 s->in2out.addr = i2o_addr;
316 s->in2out.port = i2o_port;
317 s->in2out.fib_index = rx_fib_index0;
318 s->nat_proto = nat_proto;
319 s->out2in.addr = sm_addr;
320 s->out2in.port = sm_port;
321 s->out2in.fib_index = sm->outside_fib_index;
322 switch (vec_len (sm->outside_fibs))
325 s->out2in.fib_index = sm->outside_fib_index;
328 s->out2in.fib_index = sm->outside_fibs[0].fib_index;
332 vec_foreach (outside_fib, sm->outside_fibs)
334 fei = fib_table_lookup (outside_fib->fib_index, &pfx);
335 if (FIB_NODE_INDEX_INVALID != fei)
337 if (fib_entry_get_resolving_interface (fei) != ~0)
339 s->out2in.fib_index = outside_fib->fib_index;
347 s->ext_host_addr.as_u32 = ip0->dst_address.as_u32;
348 s->ext_host_port = vnet_buffer (b0)->ip.reass.l4_dst_port;
351 /* Add to translation hashes */
353 ctx0.thread_index = thread_index;
354 init_nat_i2o_kv (&kv0, s, thread_index,
355 s - sm->per_thread_data[thread_index].sessions);
356 if (clib_bihash_add_or_overwrite_stale_8_8 (
357 &sm->in2out, &kv0, nat44_i2o_is_idle_session_cb, &ctx0))
358 nat_elog_notice ("in2out key add failed");
360 init_nat_o2i_kv (&kv0, s, thread_index,
361 s - sm->per_thread_data[thread_index].sessions);
362 if (clib_bihash_add_or_overwrite_stale_8_8 (
363 &sm->out2in, &kv0, nat44_o2i_is_idle_session_cb, &ctx0))
364 nat_elog_notice ("out2in key add failed");
367 nat_ipfix_logging_nat44_ses_create (thread_index,
368 s->in2out.addr.as_u32,
369 s->out2in.addr.as_u32,
372 s->out2in.port, s->in2out.fib_index);
374 nat_syslog_nat44_apmadd (s->user_index, s->in2out.fib_index,
375 &s->in2out.addr, s->in2out.port, &s->out2in.addr,
376 s->out2in.port, s->nat_proto);
378 nat_ha_sadd (&s->in2out.addr, s->in2out.port, &s->out2in.addr,
379 s->out2in.port, &s->ext_host_addr, s->ext_host_port,
380 &s->ext_host_nat_addr, s->ext_host_nat_port,
381 s->nat_proto, s->in2out.fib_index, s->flags, thread_index, 0);
386 #ifndef CLIB_MARCH_VARIANT
387 static_always_inline snat_in2out_error_t
388 icmp_get_key (vlib_buffer_t * b, ip4_header_t * ip0,
389 ip4_address_t * addr, u16 * port, nat_protocol_t * nat_proto)
391 icmp46_header_t *icmp0;
392 icmp_echo_header_t *echo0, *inner_echo0 = 0;
393 ip4_header_t *inner_ip0 = 0;
395 icmp46_header_t *inner_icmp0;
397 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
398 echo0 = (icmp_echo_header_t *) (icmp0 + 1);
400 if (!icmp_type_is_error_message
401 (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags))
403 *nat_proto = NAT_PROTOCOL_ICMP;
404 *addr = ip0->src_address;
405 *port = vnet_buffer (b)->ip.reass.l4_src_port;
409 inner_ip0 = (ip4_header_t *) (echo0 + 1);
410 l4_header = ip4_next_header (inner_ip0);
411 *nat_proto = ip_proto_to_nat_proto (inner_ip0->protocol);
412 *addr = inner_ip0->dst_address;
415 case NAT_PROTOCOL_ICMP:
416 inner_icmp0 = (icmp46_header_t *) l4_header;
417 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
418 *port = inner_echo0->identifier;
420 case NAT_PROTOCOL_UDP:
421 case NAT_PROTOCOL_TCP:
422 *port = ((tcp_udp_header_t *) l4_header)->dst_port;
425 return SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL;
428 return -1; /* success */
432 * Get address and port values to be used for ICMP packet translation
433 * and create session if needed
435 * @param[in,out] sm NAT main
436 * @param[in,out] node NAT node runtime
437 * @param[in] thread_index thread index
438 * @param[in,out] b0 buffer containing packet to be translated
439 * @param[in,out] ip0 ip header
440 * @param[out] p_proto protocol used for matching
441 * @param[out] p_value address and port after NAT translation
442 * @param[out] p_dont_translate if packet should not be translated
443 * @param d optional parameter
444 * @param e optional parameter
447 icmp_match_in2out_slow (snat_main_t *sm, vlib_node_runtime_t *node,
448 u32 thread_index, vlib_buffer_t *b0, ip4_header_t *ip0,
449 ip4_address_t *addr, u16 *port, u32 *fib_index,
450 nat_protocol_t *proto, snat_session_t **p_s0,
453 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
455 snat_session_t *s0 = 0;
456 clib_bihash_kv_8_8_t kv0, value0;
459 vlib_main_t *vm = vlib_get_main ();
462 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
463 *fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
465 err = icmp_get_key (b0, ip0, addr, port, proto);
468 b0->error = node->errors[err];
469 next0 = SNAT_IN2OUT_NEXT_DROP;
473 init_nat_k (&kv0, *addr, *port, *fib_index, *proto);
474 if (clib_bihash_search_8_8 (&sm->in2out, &kv0, &value0))
476 if (vnet_buffer (b0)->sw_if_index[VLIB_TX] != ~0)
479 (nat_not_translate_output_feature
480 (sm, ip0, *proto, *port, *port, thread_index, sw_if_index0)))
488 if (PREDICT_FALSE (snat_not_translate (sm, node, sw_if_index0,
489 ip0, NAT_PROTOCOL_ICMP,
490 *fib_index, thread_index)))
498 (icmp_type_is_error_message
499 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags)))
501 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
502 next0 = SNAT_IN2OUT_NEXT_DROP;
507 slow_path (sm, b0, ip0, *addr, *port, *fib_index, *proto, &s0, node,
508 next0, thread_index, vlib_time_now (vm));
510 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
522 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
524 && vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
526 && !icmp_type_is_error_message (vnet_buffer (b0)->ip.
527 reass.icmp_type_or_tcp_flags)))
529 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
530 next0 = SNAT_IN2OUT_NEXT_DROP;
534 s0 = pool_elt_at_index (tsm->sessions,
535 nat_value_get_session_index (&value0));
541 *addr = s0->out2in.addr;
542 *port = s0->out2in.port;
543 *fib_index = s0->out2in.fib_index;
551 #ifndef CLIB_MARCH_VARIANT
553 * Get address and port values to be used for ICMP packet translation
555 * @param[in] sm NAT main
556 * @param[in,out] node NAT node runtime
557 * @param[in] thread_index thread index
558 * @param[in,out] b0 buffer containing packet to be translated
559 * @param[in,out] ip0 ip header
560 * @param[out] p_proto protocol used for matching
561 * @param[out] p_value address and port after NAT translation
562 * @param[out] p_dont_translate if packet should not be translated
563 * @param d optional parameter
564 * @param e optional parameter
567 icmp_match_in2out_fast (snat_main_t *sm, vlib_node_runtime_t *node,
568 u32 thread_index, vlib_buffer_t *b0, ip4_header_t *ip0,
569 ip4_address_t *addr, u16 *port, u32 *fib_index,
570 nat_protocol_t *proto, snat_session_t **s0,
579 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
580 *fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
582 err = icmp_get_key (b0, ip0, addr, port, proto);
585 b0->error = node->errors[err];
586 next0 = SNAT_IN2OUT_NEXT_DROP;
590 ip4_address_t sm_addr;
594 if (nat44_ei_static_mapping_match (*addr, *port, *fib_index, *proto,
595 &sm_addr, &sm_port, &sm_fib_index, 0,
598 if (PREDICT_FALSE (snat_not_translate_fast (sm, node, sw_if_index0, ip0,
606 if (icmp_type_is_error_message
607 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags))
609 next0 = SNAT_IN2OUT_NEXT_DROP;
613 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
614 next0 = SNAT_IN2OUT_NEXT_DROP;
619 (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags != ICMP4_echo_request
620 && (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags !=
621 ICMP4_echo_reply || !is_addr_only)
622 && !icmp_type_is_error_message (vnet_buffer (b0)->ip.
623 reass.icmp_type_or_tcp_flags)))
625 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
626 next0 = SNAT_IN2OUT_NEXT_DROP;
635 u32 icmp_in2out (snat_main_t *sm, vlib_buffer_t *b0, ip4_header_t *ip0,
636 icmp46_header_t *icmp0, u32 sw_if_index0, u32 rx_fib_index0,
637 vlib_node_runtime_t *node, u32 next0, u32 thread_index,
638 snat_session_t **p_s0);
640 #ifndef CLIB_MARCH_VARIANT
642 icmp_in2out (snat_main_t *sm, vlib_buffer_t *b0, ip4_header_t *ip0,
643 icmp46_header_t *icmp0, u32 sw_if_index0, u32 rx_fib_index0,
644 vlib_node_runtime_t *node, u32 next0, u32 thread_index,
645 snat_session_t **p_s0)
647 vlib_main_t *vm = vlib_get_main ();
651 nat_protocol_t protocol;
652 icmp_echo_header_t *echo0, *inner_echo0 = 0;
653 ip4_header_t *inner_ip0;
655 icmp46_header_t *inner_icmp0;
657 u32 new_addr0, old_addr0;
658 u16 old_id0, new_id0;
659 u16 old_checksum0, new_checksum0;
664 echo0 = (icmp_echo_header_t *) (icmp0 + 1);
667 sm->icmp_match_in2out_cb (sm, node, thread_index, b0, ip0, &addr, &port,
668 &fib_index, &protocol, p_s0, &dont_translate);
671 if (next0 == SNAT_IN2OUT_NEXT_DROP || dont_translate)
674 if (PREDICT_TRUE (!ip4_is_fragment (ip0)))
677 ip_incremental_checksum_buffer (vm, b0,
679 (u8 *) vlib_buffer_get_current (b0),
680 ntohs (ip0->length) -
681 ip4_header_bytes (ip0), 0);
682 checksum0 = ~ip_csum_fold (sum0);
683 if (PREDICT_FALSE (checksum0 != 0 && checksum0 != 0xffff))
685 next0 = SNAT_IN2OUT_NEXT_DROP;
690 old_addr0 = ip0->src_address.as_u32;
691 new_addr0 = ip0->src_address.as_u32 = addr.as_u32;
693 sum0 = ip0->checksum;
694 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
695 src_address /* changed member */ );
696 ip0->checksum = ip_csum_fold (sum0);
698 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
700 if (icmp0->checksum == 0)
701 icmp0->checksum = 0xffff;
703 if (!icmp_type_is_error_message (icmp0->type))
706 if (PREDICT_FALSE (new_id0 != echo0->identifier))
708 old_id0 = echo0->identifier;
710 echo0->identifier = new_id0;
712 sum0 = icmp0->checksum;
714 ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
716 icmp0->checksum = ip_csum_fold (sum0);
721 inner_ip0 = (ip4_header_t *) (echo0 + 1);
722 l4_header = ip4_next_header (inner_ip0);
724 if (!ip4_header_checksum_is_valid (inner_ip0))
726 next0 = SNAT_IN2OUT_NEXT_DROP;
730 /* update inner destination IP address */
731 old_addr0 = inner_ip0->dst_address.as_u32;
732 inner_ip0->dst_address = addr;
733 new_addr0 = inner_ip0->dst_address.as_u32;
734 sum0 = icmp0->checksum;
735 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
736 dst_address /* changed member */ );
737 icmp0->checksum = ip_csum_fold (sum0);
739 /* update inner IP header checksum */
740 old_checksum0 = inner_ip0->checksum;
741 sum0 = inner_ip0->checksum;
742 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
743 dst_address /* changed member */ );
744 inner_ip0->checksum = ip_csum_fold (sum0);
745 new_checksum0 = inner_ip0->checksum;
746 sum0 = icmp0->checksum;
748 ip_csum_update (sum0, old_checksum0, new_checksum0, ip4_header_t,
750 icmp0->checksum = ip_csum_fold (sum0);
754 case NAT_PROTOCOL_ICMP:
755 inner_icmp0 = (icmp46_header_t *) l4_header;
756 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
758 old_id0 = inner_echo0->identifier;
760 inner_echo0->identifier = new_id0;
762 sum0 = icmp0->checksum;
764 ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
766 icmp0->checksum = ip_csum_fold (sum0);
768 case NAT_PROTOCOL_UDP:
769 case NAT_PROTOCOL_TCP:
770 old_id0 = ((tcp_udp_header_t *) l4_header)->dst_port;
772 ((tcp_udp_header_t *) l4_header)->dst_port = new_id0;
774 sum0 = icmp0->checksum;
775 sum0 = ip_csum_update (sum0, old_id0, new_id0, tcp_udp_header_t,
777 icmp0->checksum = ip_csum_fold (sum0);
785 if (vnet_buffer (b0)->sw_if_index[VLIB_TX] == ~0)
787 if (0 != snat_icmp_hairpinning (sm, b0, ip0, icmp0))
788 vnet_buffer (b0)->sw_if_index[VLIB_TX] = fib_index;
797 icmp_in2out_slow_path (snat_main_t * sm,
800 icmp46_header_t * icmp0,
803 vlib_node_runtime_t * node,
805 f64 now, u32 thread_index, snat_session_t ** p_s0)
807 vlib_main_t *vm = vlib_get_main ();
809 next0 = icmp_in2out (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
810 next0, thread_index, p_s0);
811 snat_session_t *s0 = *p_s0;
812 if (PREDICT_TRUE (next0 != SNAT_IN2OUT_NEXT_DROP && s0))
815 nat44_ei_session_update_counters (
816 s0, now, vlib_buffer_length_in_chain (vm, b0), thread_index);
817 /* Per-user LRU list maintenance */
818 nat44_session_update_lru (sm, s0, thread_index);
824 nat_in2out_sm_unknown_proto (snat_main_t * sm,
826 ip4_header_t * ip, u32 rx_fib_index)
828 clib_bihash_kv_8_8_t kv, value;
829 snat_static_mapping_t *m;
830 u32 old_addr, new_addr;
833 init_nat_k (&kv, ip->src_address, 0, rx_fib_index, 0);
834 if (clib_bihash_search_8_8 (&sm->static_mapping_by_local, &kv, &value))
837 m = pool_elt_at_index (sm->static_mappings, value.value);
839 old_addr = ip->src_address.as_u32;
840 new_addr = ip->src_address.as_u32 = m->external_addr.as_u32;
842 sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, src_address);
843 ip->checksum = ip_csum_fold (sum);
847 if (vnet_buffer (b)->sw_if_index[VLIB_TX] == ~0)
849 vnet_buffer (b)->sw_if_index[VLIB_TX] = m->fib_index;
850 nat_hairpinning_sm_unknown_proto (sm, b, ip);
857 snat_in2out_node_fn_inline (vlib_main_t * vm,
858 vlib_node_runtime_t * node,
859 vlib_frame_t * frame, int is_slow_path,
860 int is_output_feature)
862 u32 n_left_from, *from;
863 snat_main_t *sm = &snat_main;
864 f64 now = vlib_time_now (vm);
865 u32 thread_index = vm->thread_index;
867 from = vlib_frame_vector_args (frame);
868 n_left_from = frame->n_vectors;
870 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
871 u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
872 vlib_get_buffers (vm, from, b, n_left_from);
874 while (n_left_from >= 2)
876 vlib_buffer_t *b0, *b1;
878 u32 sw_if_index0, sw_if_index1;
879 ip4_header_t *ip0, *ip1;
880 ip_csum_t sum0, sum1;
881 u32 new_addr0, old_addr0, new_addr1, old_addr1;
882 u16 old_port0, new_port0, old_port1, new_port1;
883 udp_header_t *udp0, *udp1;
884 tcp_header_t *tcp0, *tcp1;
885 icmp46_header_t *icmp0, *icmp1;
886 u32 rx_fib_index0, rx_fib_index1;
888 snat_session_t *s0 = 0, *s1 = 0;
889 clib_bihash_kv_8_8_t kv0, value0, kv1, value1;
890 u32 iph_offset0 = 0, iph_offset1 = 0;
897 /* Prefetch next iteration. */
898 if (PREDICT_TRUE (n_left_from >= 4))
900 vlib_buffer_t *p2, *p3;
905 vlib_prefetch_buffer_header (p2, LOAD);
906 vlib_prefetch_buffer_header (p3, LOAD);
908 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, LOAD);
909 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, LOAD);
912 if (is_output_feature)
913 iph_offset0 = vnet_buffer (b0)->ip.reass.save_rewrite_length;
915 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
918 udp0 = ip4_next_header (ip0);
919 tcp0 = (tcp_header_t *) udp0;
920 icmp0 = (icmp46_header_t *) udp0;
922 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
923 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
926 next0 = next1 = SNAT_IN2OUT_NEXT_LOOKUP;
928 if (PREDICT_FALSE (ip0->ttl == 1))
930 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
931 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
932 ICMP4_time_exceeded_ttl_exceeded_in_transit,
934 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
938 proto0 = ip_proto_to_nat_proto (ip0->protocol);
940 /* Next configured feature, probably ip4-lookup */
943 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
945 if (nat_in2out_sm_unknown_proto (sm, b0, ip0, rx_fib_index0))
947 next0 = SNAT_IN2OUT_NEXT_DROP;
949 node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
951 vlib_increment_simple_counter (is_slow_path ? &sm->
952 counters.slowpath.in2out.
953 other : &sm->counters.fastpath.
954 in2out.other, thread_index,
959 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
961 next0 = icmp_in2out_slow_path
962 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0,
963 node, next0, now, thread_index, &s0);
964 vlib_increment_simple_counter (is_slow_path ? &sm->
965 counters.slowpath.in2out.
966 icmp : &sm->counters.fastpath.
967 in2out.icmp, thread_index,
974 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
976 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
980 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
982 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
987 init_nat_k (&kv0, ip0->src_address,
988 vnet_buffer (b0)->ip.reass.l4_src_port, rx_fib_index0,
990 if (PREDICT_FALSE (clib_bihash_search_8_8 (&sm->in2out, &kv0, &value0) !=
995 if (is_output_feature)
998 (nat_not_translate_output_feature
1000 vnet_buffer (b0)->ip.reass.l4_src_port,
1001 vnet_buffer (b0)->ip.reass.l4_dst_port,
1002 thread_index, sw_if_index0)))
1006 * Send DHCP packets to the ipv4 stack, or we won't
1007 * be able to use dhcp client on the outside interface
1010 (proto0 == NAT_PROTOCOL_UDP
1011 && (vnet_buffer (b0)->ip.reass.l4_dst_port ==
1012 clib_host_to_net_u16
1013 (UDP_DST_PORT_dhcp_to_server))
1014 && ip0->dst_address.as_u32 == 0xffffffff))
1021 (sm, node, sw_if_index0, ip0, proto0,
1022 rx_fib_index0, thread_index)))
1026 next0 = slow_path (sm, b0, ip0,
1028 vnet_buffer (b0)->ip.reass.l4_src_port,
1030 proto0, &s0, node, next0, thread_index, now);
1031 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
1034 if (PREDICT_FALSE (!s0))
1039 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1044 s0 = pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1045 nat_value_get_session_index (&value0));
1047 b0->flags |= VNET_BUFFER_F_IS_NATED;
1049 old_addr0 = ip0->src_address.as_u32;
1050 ip0->src_address = s0->out2in.addr;
1051 new_addr0 = ip0->src_address.as_u32;
1052 if (!is_output_feature)
1053 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1055 sum0 = ip0->checksum;
1056 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1057 ip4_header_t, src_address /* changed member */ );
1058 ip0->checksum = ip_csum_fold (sum0);
1061 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1063 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1065 old_port0 = vnet_buffer (b0)->ip.reass.l4_src_port;
1066 new_port0 = udp0->src_port = s0->out2in.port;
1067 sum0 = tcp0->checksum;
1068 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1070 dst_address /* changed member */ );
1071 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1072 ip4_header_t /* cheat */ ,
1073 length /* changed member */ );
1074 mss_clamping (sm->mss_clamping, tcp0, &sum0);
1075 tcp0->checksum = ip_csum_fold (sum0);
1077 vlib_increment_simple_counter (is_slow_path ? &sm->
1078 counters.slowpath.in2out.tcp : &sm->
1079 counters.fastpath.in2out.tcp,
1080 thread_index, sw_if_index0, 1);
1084 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1086 udp0->src_port = s0->out2in.port;
1087 if (PREDICT_FALSE (udp0->checksum))
1089 old_port0 = vnet_buffer (b0)->ip.reass.l4_src_port;
1090 new_port0 = udp0->src_port;
1091 sum0 = udp0->checksum;
1092 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t, dst_address /* changed member */
1095 ip_csum_update (sum0, old_port0, new_port0,
1096 ip4_header_t /* cheat */ ,
1097 length /* changed member */ );
1098 udp0->checksum = ip_csum_fold (sum0);
1101 vlib_increment_simple_counter (is_slow_path ? &sm->
1102 counters.slowpath.in2out.udp : &sm->
1103 counters.fastpath.in2out.udp,
1104 thread_index, sw_if_index0, 1);
1108 nat44_ei_session_update_counters (
1109 s0, now, vlib_buffer_length_in_chain (vm, b0), thread_index);
1110 /* Per-user LRU list maintenance */
1111 nat44_session_update_lru (sm, s0, thread_index);
1114 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1115 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1117 snat_in2out_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
1118 t->is_slow_path = is_slow_path;
1119 t->sw_if_index = sw_if_index0;
1120 t->next_index = next0;
1121 t->session_index = ~0;
1124 s0 - sm->per_thread_data[thread_index].sessions;
1127 if (next0 == SNAT_IN2OUT_NEXT_DROP)
1129 vlib_increment_simple_counter (is_slow_path ? &sm->
1130 counters.slowpath.in2out.
1131 drops : &sm->counters.fastpath.
1132 in2out.drops, thread_index,
1136 if (is_output_feature)
1137 iph_offset1 = vnet_buffer (b1)->ip.reass.save_rewrite_length;
1139 ip1 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b1) +
1142 udp1 = ip4_next_header (ip1);
1143 tcp1 = (tcp_header_t *) udp1;
1144 icmp1 = (icmp46_header_t *) udp1;
1146 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
1147 rx_fib_index1 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1150 if (PREDICT_FALSE (ip1->ttl == 1))
1152 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1153 icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
1154 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1156 next1 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1160 proto1 = ip_proto_to_nat_proto (ip1->protocol);
1162 /* Next configured feature, probably ip4-lookup */
1165 if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_OTHER))
1167 if (nat_in2out_sm_unknown_proto (sm, b1, ip1, rx_fib_index1))
1169 next1 = SNAT_IN2OUT_NEXT_DROP;
1171 node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
1173 vlib_increment_simple_counter (is_slow_path ? &sm->
1174 counters.slowpath.in2out.
1175 other : &sm->counters.fastpath.
1176 in2out.other, thread_index,
1181 if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_ICMP))
1183 next1 = icmp_in2out_slow_path
1184 (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node,
1185 next1, now, thread_index, &s1);
1186 vlib_increment_simple_counter (is_slow_path ? &sm->
1187 counters.slowpath.in2out.
1188 icmp : &sm->counters.fastpath.
1189 in2out.icmp, thread_index,
1196 if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_OTHER))
1198 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1202 if (PREDICT_FALSE (proto1 == NAT_PROTOCOL_ICMP))
1204 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1209 init_nat_k (&kv1, ip1->src_address,
1210 vnet_buffer (b1)->ip.reass.l4_src_port, rx_fib_index1,
1212 if (PREDICT_FALSE (clib_bihash_search_8_8 (&sm->in2out, &kv1, &value1) !=
1217 if (is_output_feature)
1220 (nat_not_translate_output_feature
1222 vnet_buffer (b1)->ip.reass.l4_src_port,
1223 vnet_buffer (b1)->ip.reass.l4_dst_port,
1224 thread_index, sw_if_index1)))
1228 * Send DHCP packets to the ipv4 stack, or we won't
1229 * be able to use dhcp client on the outside interface
1232 (proto1 == NAT_PROTOCOL_UDP
1233 && (vnet_buffer (b1)->ip.reass.l4_dst_port ==
1234 clib_host_to_net_u16
1235 (UDP_DST_PORT_dhcp_to_server))
1236 && ip1->dst_address.as_u32 == 0xffffffff))
1243 (sm, node, sw_if_index1, ip1, proto1,
1244 rx_fib_index1, thread_index)))
1249 slow_path (sm, b1, ip1, ip1->src_address,
1250 vnet_buffer (b1)->ip.reass.l4_src_port,
1251 rx_fib_index1, proto1, &s1, node, next1,
1253 if (PREDICT_FALSE (next1 == SNAT_IN2OUT_NEXT_DROP))
1256 if (PREDICT_FALSE (!s1))
1261 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1266 s1 = pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1267 nat_value_get_session_index (&value1));
1269 b1->flags |= VNET_BUFFER_F_IS_NATED;
1271 old_addr1 = ip1->src_address.as_u32;
1272 ip1->src_address = s1->out2in.addr;
1273 new_addr1 = ip1->src_address.as_u32;
1274 if (!is_output_feature)
1275 vnet_buffer (b1)->sw_if_index[VLIB_TX] = s1->out2in.fib_index;
1277 sum1 = ip1->checksum;
1278 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1279 ip4_header_t, src_address /* changed member */ );
1280 ip1->checksum = ip_csum_fold (sum1);
1282 if (PREDICT_TRUE (proto1 == NAT_PROTOCOL_TCP))
1284 if (!vnet_buffer (b1)->ip.reass.is_non_first_fragment)
1286 old_port1 = vnet_buffer (b1)->ip.reass.l4_src_port;
1287 new_port1 = udp1->src_port = s1->out2in.port;
1288 sum1 = tcp1->checksum;
1289 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1291 dst_address /* changed member */ );
1292 sum1 = ip_csum_update (sum1, old_port1, new_port1,
1293 ip4_header_t /* cheat */ ,
1294 length /* changed member */ );
1295 mss_clamping (sm->mss_clamping, tcp1, &sum1);
1296 tcp1->checksum = ip_csum_fold (sum1);
1298 vlib_increment_simple_counter (is_slow_path ? &sm->
1299 counters.slowpath.in2out.tcp : &sm->
1300 counters.fastpath.in2out.tcp,
1301 thread_index, sw_if_index1, 1);
1305 if (!vnet_buffer (b1)->ip.reass.is_non_first_fragment)
1307 udp1->src_port = s1->out2in.port;
1308 if (PREDICT_FALSE (udp1->checksum))
1310 old_port1 = vnet_buffer (b1)->ip.reass.l4_src_port;
1311 new_port1 = udp1->src_port;
1312 sum1 = udp1->checksum;
1313 sum1 = ip_csum_update (sum1, old_addr1, new_addr1, ip4_header_t, dst_address /* changed member */
1316 ip_csum_update (sum1, old_port1, new_port1,
1317 ip4_header_t /* cheat */ ,
1318 length /* changed member */ );
1319 udp1->checksum = ip_csum_fold (sum1);
1322 vlib_increment_simple_counter (is_slow_path ? &sm->
1323 counters.slowpath.in2out.udp : &sm->
1324 counters.fastpath.in2out.udp,
1325 thread_index, sw_if_index1, 1);
1329 nat44_ei_session_update_counters (
1330 s1, now, vlib_buffer_length_in_chain (vm, b1), thread_index);
1331 /* Per-user LRU list maintenance */
1332 nat44_session_update_lru (sm, s1, thread_index);
1335 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1336 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
1338 snat_in2out_trace_t *t = vlib_add_trace (vm, node, b1, sizeof (*t));
1339 t->sw_if_index = sw_if_index1;
1340 t->next_index = next1;
1341 t->session_index = ~0;
1344 s1 - sm->per_thread_data[thread_index].sessions;
1347 if (next1 == SNAT_IN2OUT_NEXT_DROP)
1349 vlib_increment_simple_counter (is_slow_path ? &sm->
1350 counters.slowpath.in2out.
1351 drops : &sm->counters.fastpath.
1352 in2out.drops, thread_index,
1362 while (n_left_from > 0)
1369 u32 new_addr0, old_addr0;
1370 u16 old_port0, new_port0;
1373 icmp46_header_t *icmp0;
1376 snat_session_t *s0 = 0;
1377 clib_bihash_kv_8_8_t kv0, value0;
1378 u32 iph_offset0 = 0;
1382 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
1384 if (is_output_feature)
1385 iph_offset0 = vnet_buffer (b0)->ip.reass.save_rewrite_length;
1387 ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0) +
1390 udp0 = ip4_next_header (ip0);
1391 tcp0 = (tcp_header_t *) udp0;
1392 icmp0 = (icmp46_header_t *) udp0;
1394 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1395 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1398 if (PREDICT_FALSE (ip0->ttl == 1))
1400 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1401 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1402 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1404 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1408 proto0 = ip_proto_to_nat_proto (ip0->protocol);
1410 /* Next configured feature, probably ip4-lookup */
1413 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1415 if (nat_in2out_sm_unknown_proto (sm, b0, ip0, rx_fib_index0))
1417 next0 = SNAT_IN2OUT_NEXT_DROP;
1419 node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL];
1421 vlib_increment_simple_counter (is_slow_path ? &sm->
1422 counters.slowpath.in2out.
1423 other : &sm->counters.fastpath.
1424 in2out.other, thread_index,
1429 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1431 next0 = icmp_in2out_slow_path
1432 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1433 next0, now, thread_index, &s0);
1434 vlib_increment_simple_counter (is_slow_path ? &sm->
1435 counters.slowpath.in2out.
1436 icmp : &sm->counters.fastpath.
1437 in2out.icmp, thread_index,
1444 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1446 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1450 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1452 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1457 init_nat_k (&kv0, ip0->src_address,
1458 vnet_buffer (b0)->ip.reass.l4_src_port, rx_fib_index0,
1461 if (clib_bihash_search_8_8 (&sm->in2out, &kv0, &value0))
1465 if (is_output_feature)
1468 (nat_not_translate_output_feature
1470 vnet_buffer (b0)->ip.reass.l4_src_port,
1471 vnet_buffer (b0)->ip.reass.l4_dst_port,
1472 thread_index, sw_if_index0)))
1476 * Send DHCP packets to the ipv4 stack, or we won't
1477 * be able to use dhcp client on the outside interface
1480 (proto0 == NAT_PROTOCOL_UDP
1481 && (vnet_buffer (b0)->ip.reass.l4_dst_port ==
1482 clib_host_to_net_u16
1483 (UDP_DST_PORT_dhcp_to_server))
1484 && ip0->dst_address.as_u32 == 0xffffffff))
1491 (sm, node, sw_if_index0, ip0, proto0, rx_fib_index0,
1497 slow_path (sm, b0, ip0, ip0->src_address,
1498 vnet_buffer (b0)->ip.reass.l4_src_port,
1499 rx_fib_index0, proto0, &s0, node, next0,
1502 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
1505 if (PREDICT_FALSE (!s0))
1510 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1515 s0 = pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1516 nat_value_get_session_index (&value0));
1518 b0->flags |= VNET_BUFFER_F_IS_NATED;
1520 old_addr0 = ip0->src_address.as_u32;
1521 ip0->src_address = s0->out2in.addr;
1522 new_addr0 = ip0->src_address.as_u32;
1523 if (!is_output_feature)
1524 vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1526 sum0 = ip0->checksum;
1527 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1528 ip4_header_t, src_address /* changed member */ );
1529 ip0->checksum = ip_csum_fold (sum0);
1531 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1533 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1535 old_port0 = vnet_buffer (b0)->ip.reass.l4_src_port;
1536 new_port0 = udp0->src_port = s0->out2in.port;
1537 sum0 = tcp0->checksum;
1539 ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1540 dst_address /* changed member */ );
1542 ip_csum_update (sum0, old_port0, new_port0,
1543 ip4_header_t /* cheat */ ,
1544 length /* changed member */ );
1545 mss_clamping (sm->mss_clamping, tcp0, &sum0);
1546 tcp0->checksum = ip_csum_fold (sum0);
1548 vlib_increment_simple_counter (is_slow_path ? &sm->
1549 counters.slowpath.in2out.tcp : &sm->
1550 counters.fastpath.in2out.tcp,
1551 thread_index, sw_if_index0, 1);
1555 if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1557 udp0->src_port = s0->out2in.port;
1558 if (PREDICT_FALSE (udp0->checksum))
1560 old_port0 = vnet_buffer (b0)->ip.reass.l4_src_port;
1561 new_port0 = udp0->src_port;
1562 sum0 = udp0->checksum;
1564 ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1565 dst_address /* changed member */ );
1567 ip_csum_update (sum0, old_port0, new_port0,
1568 ip4_header_t /* cheat */ ,
1569 length /* changed member */ );
1570 udp0->checksum = ip_csum_fold (sum0);
1573 vlib_increment_simple_counter (is_slow_path ? &sm->
1574 counters.slowpath.in2out.udp : &sm->
1575 counters.fastpath.in2out.udp,
1576 thread_index, sw_if_index0, 1);
1580 nat44_ei_session_update_counters (
1581 s0, now, vlib_buffer_length_in_chain (vm, b0), thread_index);
1582 /* Per-user LRU list maintenance */
1583 nat44_session_update_lru (sm, s0, thread_index);
1586 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1587 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1589 snat_in2out_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
1590 t->is_slow_path = is_slow_path;
1591 t->sw_if_index = sw_if_index0;
1592 t->next_index = next0;
1593 t->session_index = ~0;
1596 s0 - sm->per_thread_data[thread_index].sessions;
1599 if (next0 == SNAT_IN2OUT_NEXT_DROP)
1601 vlib_increment_simple_counter (is_slow_path ? &sm->
1602 counters.slowpath.in2out.
1603 drops : &sm->counters.fastpath.
1604 in2out.drops, thread_index,
1613 vlib_buffer_enqueue_to_next (vm, node, from, (u16 *) nexts,
1615 return frame->n_vectors;
1618 VLIB_NODE_FN (snat_in2out_node) (vlib_main_t * vm,
1619 vlib_node_runtime_t * node,
1620 vlib_frame_t * frame)
1622 return snat_in2out_node_fn_inline (vm, node, frame, 0 /* is_slow_path */ ,
1627 VLIB_REGISTER_NODE (snat_in2out_node) = {
1628 .name = "nat44-in2out",
1629 .vector_size = sizeof (u32),
1630 .format_trace = format_snat_in2out_trace,
1631 .type = VLIB_NODE_TYPE_INTERNAL,
1633 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1634 .error_strings = snat_in2out_error_strings,
1636 .runtime_data_bytes = sizeof (snat_runtime_t),
1638 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1640 /* edit / add dispositions here */
1642 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1643 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1644 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
1645 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1650 VLIB_NODE_FN (snat_in2out_output_node) (vlib_main_t * vm,
1651 vlib_node_runtime_t * node,
1652 vlib_frame_t * frame)
1654 return snat_in2out_node_fn_inline (vm, node, frame, 0 /* is_slow_path */ ,
1659 VLIB_REGISTER_NODE (snat_in2out_output_node) = {
1660 .name = "nat44-in2out-output",
1661 .vector_size = sizeof (u32),
1662 .format_trace = format_snat_in2out_trace,
1663 .type = VLIB_NODE_TYPE_INTERNAL,
1665 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1666 .error_strings = snat_in2out_error_strings,
1668 .runtime_data_bytes = sizeof (snat_runtime_t),
1670 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1672 /* edit / add dispositions here */
1674 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1675 [SNAT_IN2OUT_NEXT_LOOKUP] = "interface-output",
1676 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-output-slowpath",
1677 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1682 VLIB_NODE_FN (snat_in2out_slowpath_node) (vlib_main_t * vm,
1683 vlib_node_runtime_t * node,
1684 vlib_frame_t * frame)
1686 return snat_in2out_node_fn_inline (vm, node, frame, 1 /* is_slow_path */ ,
1691 VLIB_REGISTER_NODE (snat_in2out_slowpath_node) = {
1692 .name = "nat44-in2out-slowpath",
1693 .vector_size = sizeof (u32),
1694 .format_trace = format_snat_in2out_trace,
1695 .type = VLIB_NODE_TYPE_INTERNAL,
1697 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1698 .error_strings = snat_in2out_error_strings,
1700 .runtime_data_bytes = sizeof (snat_runtime_t),
1702 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1704 /* edit / add dispositions here */
1706 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1707 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1708 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
1709 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1714 VLIB_NODE_FN (snat_in2out_output_slowpath_node) (vlib_main_t * vm,
1715 vlib_node_runtime_t * node,
1716 vlib_frame_t * frame)
1718 return snat_in2out_node_fn_inline (vm, node, frame, 1 /* is_slow_path */ ,
1723 VLIB_REGISTER_NODE (snat_in2out_output_slowpath_node) = {
1724 .name = "nat44-in2out-output-slowpath",
1725 .vector_size = sizeof (u32),
1726 .format_trace = format_snat_in2out_trace,
1727 .type = VLIB_NODE_TYPE_INTERNAL,
1729 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1730 .error_strings = snat_in2out_error_strings,
1732 .runtime_data_bytes = sizeof (snat_runtime_t),
1734 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1736 /* edit / add dispositions here */
1738 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1739 [SNAT_IN2OUT_NEXT_LOOKUP] = "interface-output",
1740 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-output-slowpath",
1741 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1746 VLIB_NODE_FN (snat_in2out_fast_node) (vlib_main_t * vm,
1747 vlib_node_runtime_t * node,
1748 vlib_frame_t * frame)
1750 u32 n_left_from, *from, *to_next;
1751 snat_in2out_next_t next_index;
1752 snat_main_t *sm = &snat_main;
1753 int is_hairpinning = 0;
1755 from = vlib_frame_vector_args (frame);
1756 n_left_from = frame->n_vectors;
1757 next_index = node->cached_next_index;
1759 while (n_left_from > 0)
1763 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1765 while (n_left_from > 0 && n_left_to_next > 0)
1773 u32 new_addr0, old_addr0;
1774 u16 old_port0, new_port0;
1777 icmp46_header_t *icmp0;
1780 ip4_address_t sm0_addr;
1785 /* speculatively enqueue b0 to the current next frame */
1791 n_left_to_next -= 1;
1793 b0 = vlib_get_buffer (vm, bi0);
1794 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
1796 ip0 = vlib_buffer_get_current (b0);
1797 udp0 = ip4_next_header (ip0);
1798 tcp0 = (tcp_header_t *) udp0;
1799 icmp0 = (icmp46_header_t *) udp0;
1801 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1803 ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
1805 if (PREDICT_FALSE (ip0->ttl == 1))
1807 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1808 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1809 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1811 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1815 proto0 = ip_proto_to_nat_proto (ip0->protocol);
1817 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
1820 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
1822 next0 = icmp_in2out (sm, b0, ip0, icmp0, sw_if_index0,
1823 rx_fib_index0, node, next0, ~0, 0);
1827 if (nat44_ei_static_mapping_match (
1828 ip0->src_address, udp0->src_port, rx_fib_index0, proto0,
1829 &sm0_addr, &sm0_port, &sm0_fib_index, 0, 0, 0))
1831 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
1832 next0 = SNAT_IN2OUT_NEXT_DROP;
1836 new_addr0 = sm0_addr.as_u32;
1837 new_port0 = sm0_port;
1838 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm0_fib_index;
1839 old_addr0 = ip0->src_address.as_u32;
1840 ip0->src_address.as_u32 = new_addr0;
1842 sum0 = ip0->checksum;
1843 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1845 src_address /* changed member */ );
1846 ip0->checksum = ip_csum_fold (sum0);
1848 if (PREDICT_FALSE (new_port0 != udp0->dst_port))
1850 old_port0 = udp0->src_port;
1851 udp0->src_port = new_port0;
1853 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1855 sum0 = tcp0->checksum;
1856 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1858 dst_address /* changed member */ );
1859 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1860 ip4_header_t /* cheat */ ,
1861 length /* changed member */ );
1862 mss_clamping (sm->mss_clamping, tcp0, &sum0);
1863 tcp0->checksum = ip_csum_fold (sum0);
1865 else if (udp0->checksum)
1867 sum0 = udp0->checksum;
1868 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1870 dst_address /* changed member */ );
1871 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1872 ip4_header_t /* cheat */ ,
1873 length /* changed member */ );
1874 udp0->checksum = ip_csum_fold (sum0);
1879 if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
1881 sum0 = tcp0->checksum;
1882 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1884 dst_address /* changed member */ );
1885 mss_clamping (sm->mss_clamping, tcp0, &sum0);
1886 tcp0->checksum = ip_csum_fold (sum0);
1888 else if (udp0->checksum)
1890 sum0 = udp0->checksum;
1891 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1893 dst_address /* changed member */ );
1894 udp0->checksum = ip_csum_fold (sum0);
1899 is_hairpinning = snat_hairpinning (vm, node, sm, b0, ip0, udp0, tcp0,
1900 proto0, 0 /* do_trace */);
1903 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1904 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1906 snat_in2out_trace_t *t =
1907 vlib_add_trace (vm, node, b0, sizeof (*t));
1908 t->sw_if_index = sw_if_index0;
1909 t->next_index = next0;
1910 t->is_hairpinning = is_hairpinning;
1913 if (next0 != SNAT_IN2OUT_NEXT_DROP)
1916 vlib_increment_simple_counter (&sm->counters.fastpath.
1917 in2out.other, sw_if_index0,
1918 vm->thread_index, 1);
1921 /* verify speculative enqueue, maybe switch current next frame */
1922 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1923 to_next, n_left_to_next,
1927 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1930 return frame->n_vectors;
1935 VLIB_REGISTER_NODE (snat_in2out_fast_node) = {
1936 .name = "nat44-in2out-fast",
1937 .vector_size = sizeof (u32),
1938 .format_trace = format_snat_in2out_fast_trace,
1939 .type = VLIB_NODE_TYPE_INTERNAL,
1941 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1942 .error_strings = snat_in2out_error_strings,
1944 .runtime_data_bytes = sizeof (snat_runtime_t),
1946 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1948 /* edit / add dispositions here */
1950 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1951 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1952 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "nat44-in2out-slowpath",
1953 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1959 * fd.io coding-style-patch-verification: ON
1962 * eval: (c-set-style "gnu")